From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796098539; b=y7twlk4XIg6+amRbBYgLNa67lIhaCD7yOTqt6Ad66h6/Fp5Xa4k9R4yis/VnvAbI/fkG RHnDsYViVUY0cEVyZ6/4tkPlxX80mr8nnXPccVbOU7qoEcjz5dyNEZU8ISLnPVfm1dJ1p EwC/ymeIMTedmSMdygeBUnbMDZ3TiZ8WQVJWQf5ulD8b3ud6+j1bpfg1FdlLEsLU847UD 9b39JbggagqONPDRbV94ypIDbhjWeOKJ/ze3L8HpNlvoXiHsh50Gx6/1L7H4dHwVftpg7 s59a3ci2FxSBw6JUtACa+E81U8Em1hKgzLHdaiNvKBba16vXf4hRRtXS3Pt3MJrDplg== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796098539; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=qXseg3DtABHABtSaykbe6LSdX5SLYXy0yciXn/Rrv6g=; b=UNQRHrBrYG1K6Cvn7FsTAzbWRpOj5wK5zD4oEjmeHf6nE64UolTHwwW0EReYMimfbwy2 TutCpVXdmPiR6k3sB6Vvuzr5ZRfNhgBZyXi7eMUDbXmBFX48MYQpDFu8WNIEVcpNgXIxf 30268YX0s2CkzEqMJifjXwn0uluuACZBaD6kMlVZShLzyY7Wx/4js4HEg8orCEEnU4dLD OzijJe2RG71gpLdyb5sl8p/OpjtAEI73Sj3l8Zl1upCwJpXLPtADObi8p68jNDdgDQx18 tp5lvDQEutMqY4fAdEHUkCLWAO9f13pRXmI77Yiv74qatQDMnaljBEUM8GKTd+wUIRg== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=PTFff0lc; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E652F606A32A; Tue, 1 Dec 2026 04:15:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=PTFff0lc Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1796098538; b=VGvLhYRh7vlE2AtBmucY66EOy0IkWyh4RdChoKvfH9kR6c0I15oFZia5B74NvcrymO/0 1Jsi0mpPLpYcXJNgLiBUvRO0v3vjc7N0xR4wrteVnF4PImdVwwTwqADQiOX1MHBI09GEN nJ/VjJWXBTHHDGJWyvRM8btaSNeNYpU817rHR/XuR6rY6nwSswc9KoHuvkGyicc1rhRwn NMQ4YrXawLW67QnlC3lgwDe+cjNJZ2o8sSXa2+nF+hj55O2Lv+lUB2TYprp5xt2SrkxE3 29+bvM9CefrIPG4eB2n+eGpSpIi6GVmCjVcbSjgWpsrClfB9iowIDDoVSb9sKq4EgBA== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1796098538; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=qXseg3DtABHABtSaykbe6LSdX5SLYXy0yciXn/Rrv6g=; b=1FKTvMALom3Ng8OvnUWKenrrJOdEEZEYxibmXiZhGHehPlhBEZN/rzsbJ/PB9kqHAxQt 986QTn4GcVFJqiyyR68P8snS64+VtsbWQjWiEPlF5wkzezuYrITsfr8Ai4odKaXHXpHtr g3jO9WQmRMRQpOKO4/kePOM0+P7VnfD3sc2pPoEq2j6GYh9sob1+bRqq+mEkOzNISr2nm 1E9ZkkioFig26KMIrBSqaqbvdiPAiT2vsCyt6g11lyxDvTD3enLIv21eNq1GhTAlqfsHg oY96nnHI1skmRfsYu7jLG0L3T6J1oRUtFquofW9JofiJfJ3dIISZVz+kM0K392Pe9MA== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id A48EA1F000C1; Tue, 1 Dec 2026 04:15:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796098538; bh=qXseg3DtABHABtSaykbe6LSdX5SLYXy0yciXn/Rrv6g=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=PTFff0lcowrrdLpr2XFByR2rfLjPkE0a0B18j/gkEUrAWYqW4+MxolfXM3Wm2BK5V QIKVv4dRIvH9UfQp07CawYzH+QBsy3+wp+D1rXLWAugBfgCgXKJBYgC7X/F5PnYDnR DEC5FFJG3oEImdpRWRivNZ7Ez389yDT2qzPOljKGHAb8FAW/97UAZS+DPtvBmzFdNV QuhiA8QVjRSfxivXTWK87zM5lOWSs1q0mflC55gJWenVI+X5Pjs3OXcvDi6ELv9pZP YYW7jQ/eye7wgtdsQNDbx9qV7pJ+y2lstic3TDqvK+Sba2NOBMnT2E04gBfuN64WeR UGq6DnGTqRMrQ== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 2C41F4EB94; Tue, 1 Dec 2026 04:15:37 +0000 (UTC) From: Zamiul Rashid via B4 Relay Date: Tue, 01 Dec 2026 10:15:36 +0600 Subject: [PATCH 1/2] HID: compx: add battery reporting for Compx 2.4 GHz receivers X-Mailing-List: linux-input@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Message-Id: <20261201-compx-keychron-battery-v1-1-d5210d82dd67@gmail.com> References: <20261201-compx-keychron-battery-v1-0-d5210d82dd67@gmail.com> In-Reply-To: <20261201-compx-keychron-battery-v1-0-d5210d82dd67@gmail.com> To: Jiri Kosina , Benjamin Tissoires Cc: linux-kernel@vger.kernel.org, linux-input@vger.kernel.org, Zamiul Rashid X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1796098536; l=12265; i=zamiulrashid1@gmail.com; s=20261201; h=from:subject:message-id; bh=YOTkj5Y1SC5UGaFLms4dKoCwny3HqTudd9RIAbkZmtE=; b=2OltcyWfrmtJXGeMnwzGMSAa1Mw480DWTLaTI9BgMOf7lBdFbbyovOhsVCEH6udO6iegvzuKL uBiBuFPRPq/D4v6bxScCiZWR1UWTwTifRh/SG/gAmyxWRo9wGC7tBh9 X-Developer-Key: i=zamiulrashid1@gmail.com; a=ed25519; pk=oNKaK24DcWwTOvR2guXezYsgZ96DsNoOCT8zLoJuows= X-Endpoint-Received: by B4 Relay for zamiulrashid1@gmail.com/20261201 with auth_id=1708 X-Original-From: Zamiul Rashid Reply-To: zamiulrashid1@gmail.com Content-Transfer-Encoding: 7bit From: Zamiul Rashid Many low-cost 2.4 GHz mice (Ajazz, Delux, Genesis, Inphic and others) ship with a Compx receiver built on a Telink chip (248a:5b2f, "SSCYPL Wireless-Receiver"). The receiver does not describe the mouse battery with standard HID usages, so the battery level never reaches userspace. Its vendor interface answers a read-only status query instead: a SET_REPORT of feature report 0x0c with command 0x20, followed by a GET_REPORT of the same report, returns the battery level in percent and the charging state. Add a driver that polls this report every five minutes, or every 30 seconds while the mouse is asleep, and exposes the level as a power_supply. The battery is only registered once the first reading has arrived, because UPower ignores a battery that appears without one. A level of 0 is what the receiver answers before the mouse has reported, so it is treated as no reading. Tested with an Ajazz AJ179P. Signed-off-by: Zamiul Rashid --- drivers/hid/Kconfig | 11 ++ drivers/hid/Makefile | 1 + drivers/hid/hid-compx.c | 330 ++++++++++++++++++++++++++++++++++++++++++++++++ drivers/hid/hid-ids.h | 1 + 4 files changed, 343 insertions(+) diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig index d17d960ab5e..c88426a5ab5 100644 --- a/drivers/hid/Kconfig +++ b/drivers/hid/Kconfig @@ -296,6 +296,17 @@ config HID_CMEDIA Support for CMedia CM6533 HID audio jack controls and HS100B mute buttons. +config HID_COMPX + tristate "Compx 2.4 GHz mouse receivers" + depends on USB_HID + select POWER_SUPPLY + help + Support for the battery level of mice that use a Compx 2.4 GHz + receiver, sold with Ajazz, Delux, Genesis, Inphic and other mice. + + To compile this driver as a module, choose M here: the module will + be called hid-compx. + config HID_CP2112 tristate "Silicon Labs CP2112 HID USB-to-SMBus Bridge support" depends on USB_HID && HIDRAW && I2C && GPIOLIB diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile index dc30faaac96..f5f68644846 100644 --- a/drivers/hid/Makefile +++ b/drivers/hid/Makefile @@ -41,6 +41,7 @@ obj-$(CONFIG_HID_BIGBEN_FF) += hid-bigbenff.o obj-$(CONFIG_HID_CHERRY) += hid-cherry.o obj-$(CONFIG_HID_CHICONY) += hid-chicony.o obj-$(CONFIG_HID_CMEDIA) += hid-cmedia.o +obj-$(CONFIG_HID_COMPX) += hid-compx.o obj-$(CONFIG_HID_CORSAIR) += hid-corsair.o hid-corsair-void.o obj-$(CONFIG_HID_COUGAR) += hid-cougar.o obj-$(CONFIG_HID_CP2112) += hid-cp2112.o diff --git a/drivers/hid/hid-compx.c b/drivers/hid/hid-compx.c new file mode 100644 index 00000000000..bf4c4e7b0b9 --- /dev/null +++ b/drivers/hid/hid-compx.c @@ -0,0 +1,330 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * HID driver for Compx 2.4 GHz mouse receivers + * + * Mice from several brands (Ajazz, Delux, Genesis, Inphic, ...) ship with a + * Compx receiver. The receiver does not describe the mouse battery with + * standard HID usages. Its vendor collection answers a read-only status + * query on feature report 0x0c instead. This driver polls that report and + * exposes the battery as a power_supply. + * + * Copyright (c) 2026 Zamiul Rashid + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "hid-ids.h" + +#define COMPX_REPORT_ID 0x0c +#define COMPX_REPORT_LEN 33 /* including the report ID */ +#define COMPX_CMD_STATUS 0x20 + +/* Offsets in the status reply */ +#define COMPX_OFF_CMD 0x02 +#define COMPX_OFF_PAYLOAD_LEN 0x06 /* 0 = receiver not ready */ +#define COMPX_OFF_BATTERY 0x12 /* percent, 0 = no reading yet */ +#define COMPX_OFF_STATE 0x13 /* 1 = charging, 2 = full */ + +#define COMPX_STATE_CHARGING 1 +#define COMPX_STATE_FULL 2 + +#define COMPX_POLL_INTERVAL (300 * HZ) +#define COMPX_RETRY_INTERVAL (30 * HZ) +#define COMPX_REPLY_DELAY_MS 30 +#define COMPX_REPLY_TRIES 2 + +struct compx_device { + struct hid_device *hdev; + struct power_supply *battery; + struct power_supply_desc battery_desc; + struct delayed_work work; + u8 *buf; + + spinlock_t lock; /* protects the fields below */ + bool valid; + int capacity; + int status; +}; + +static enum power_supply_property compx_battery_props[] = { + POWER_SUPPLY_PROP_PRESENT, + POWER_SUPPLY_PROP_CAPACITY, + POWER_SUPPLY_PROP_STATUS, + POWER_SUPPLY_PROP_SCOPE, + POWER_SUPPLY_PROP_MODEL_NAME, +}; + +static int compx_battery_get_property(struct power_supply *psy, + enum power_supply_property psp, + union power_supply_propval *val) +{ + struct compx_device *cdev = power_supply_get_drvdata(psy); + unsigned long flags; + int ret = 0; + + /* The battery is only registered once a reading has arrived. */ + spin_lock_irqsave(&cdev->lock, flags); + switch (psp) { + case POWER_SUPPLY_PROP_PRESENT: + val->intval = 1; + break; + case POWER_SUPPLY_PROP_CAPACITY: + val->intval = cdev->capacity; + break; + case POWER_SUPPLY_PROP_STATUS: + val->intval = cdev->status; + break; + case POWER_SUPPLY_PROP_SCOPE: + val->intval = POWER_SUPPLY_SCOPE_DEVICE; + break; + case POWER_SUPPLY_PROP_MODEL_NAME: + val->strval = cdev->hdev->name; + break; + default: + ret = -EINVAL; + break; + } + spin_unlock_irqrestore(&cdev->lock, flags); + + return ret; +} + +/* Returns true and updates the cached state if @rx is a valid status reply. */ +static bool compx_parse_status(struct compx_device *cdev, const u8 *rx, int len, + bool *changed) +{ + unsigned long flags; + int capacity, status; + + if (len <= COMPX_OFF_STATE || rx[0] != COMPX_REPORT_ID || + rx[COMPX_OFF_CMD] != COMPX_CMD_STATUS || + rx[COMPX_OFF_PAYLOAD_LEN] == 0) + return false; + + /* + * A freshly plugged receiver answers 0 before the mouse has reported. + * A mouse that can answer is never really empty, so treat 0 as no + * reading yet. + */ + capacity = rx[COMPX_OFF_BATTERY]; + if (capacity == 0 || capacity > 100) + return false; + + switch (rx[COMPX_OFF_STATE]) { + case COMPX_STATE_CHARGING: + status = POWER_SUPPLY_STATUS_CHARGING; + break; + case COMPX_STATE_FULL: + status = POWER_SUPPLY_STATUS_FULL; + break; + default: + status = POWER_SUPPLY_STATUS_DISCHARGING; + break; + } + + spin_lock_irqsave(&cdev->lock, flags); + *changed = !cdev->valid || cdev->capacity != capacity || + cdev->status != status; + cdev->valid = true; + cdev->capacity = capacity; + cdev->status = status; + spin_unlock_irqrestore(&cdev->lock, flags); + + return true; +} + +static int compx_query_status(struct compx_device *cdev, bool *changed) +{ + struct hid_device *hdev = cdev->hdev; + u8 *buf = cdev->buf; + int i, ret; + + memset(buf, 0, COMPX_REPORT_LEN); + buf[0] = COMPX_REPORT_ID; + buf[1] = 0x01; + buf[2] = COMPX_CMD_STATUS; + buf[4] = 0x01; + + ret = hid_hw_raw_request(hdev, COMPX_REPORT_ID, buf, COMPX_REPORT_LEN, + HID_FEATURE_REPORT, HID_REQ_SET_REPORT); + if (ret < 0) + return ret; + + for (i = 0; i < COMPX_REPLY_TRIES; i++) { + msleep(COMPX_REPLY_DELAY_MS); + + memset(buf, 0, COMPX_REPORT_LEN); + buf[0] = COMPX_REPORT_ID; + ret = hid_hw_raw_request(hdev, COMPX_REPORT_ID, buf, + COMPX_REPORT_LEN, HID_FEATURE_REPORT, + HID_REQ_GET_REPORT); + if (ret < 0) + return ret; + + if (compx_parse_status(cdev, buf, ret, changed)) + return 0; + } + + /* The receiver answered, but the mouse is asleep or out of range. */ + return -ENODATA; +} + +static int compx_register_battery(struct compx_device *cdev) +{ + struct hid_device *hdev = cdev->hdev; + struct power_supply_config psy_cfg = { .drv_data = cdev }; + struct power_supply *battery; + + battery = power_supply_register(&hdev->dev, &cdev->battery_desc, + &psy_cfg); + if (IS_ERR(battery)) { + hid_err(hdev, "cannot register battery: %ld\n", + PTR_ERR(battery)); + return PTR_ERR(battery); + } + + power_supply_powers(battery, &hdev->dev); + cdev->battery = battery; + return 0; +} + +static void compx_work(struct work_struct *work) +{ + struct compx_device *cdev = container_of(to_delayed_work(work), + struct compx_device, work); + struct hid_device *hdev = cdev->hdev; + bool changed = false; + int ret; + + ret = hid_hw_power(hdev, PM_HINT_FULLON); + if (ret < 0) { + hid_dbg(hdev, "cannot wake receiver: %d\n", ret); + schedule_delayed_work(&cdev->work, COMPX_RETRY_INTERVAL); + return; + } + + ret = compx_query_status(cdev, &changed); + hid_hw_power(hdev, PM_HINT_NORMAL); + + if (ret < 0) { + /* Keep the last reading; the mouse is usually just asleep. */ + if (ret != -ENODATA) + hid_dbg(hdev, "status query failed: %d\n", ret); + schedule_delayed_work(&cdev->work, COMPX_RETRY_INTERVAL); + return; + } + + if (!cdev->battery) { + /* + * Register only now: userspace (UPower) skips a battery that + * appears without a reading and does not look at it again. + */ + ret = compx_register_battery(cdev); + if (ret) { + schedule_delayed_work(&cdev->work, COMPX_RETRY_INTERVAL); + return; + } + } else if (changed) { + power_supply_changed(cdev->battery); + } + + schedule_delayed_work(&cdev->work, COMPX_POLL_INTERVAL); +} + +static int compx_probe(struct hid_device *hdev, const struct hid_device_id *id) +{ + struct compx_device *cdev; + struct hid_report *report; + int ret; + + hid_set_drvdata(hdev, NULL); + + ret = hid_parse(hdev); + if (ret) + return ret; + + ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT); + if (ret) + return ret; + + /* Only the vendor interface carries the status report. */ + report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[COMPX_REPORT_ID]; + if (!report || hid_report_len(report) < COMPX_REPORT_LEN) + return 0; + + cdev = devm_kzalloc(&hdev->dev, sizeof(*cdev), GFP_KERNEL); + if (!cdev) { + ret = -ENOMEM; + goto err_stop; + } + + cdev->buf = devm_kzalloc(&hdev->dev, COMPX_REPORT_LEN, GFP_KERNEL); + if (!cdev->buf) { + ret = -ENOMEM; + goto err_stop; + } + + cdev->hdev = hdev; + spin_lock_init(&cdev->lock); + INIT_DELAYED_WORK(&cdev->work, compx_work); + + cdev->battery_desc.name = devm_kasprintf(&hdev->dev, GFP_KERNEL, + "hid-%s-battery", + dev_name(&hdev->dev)); + if (!cdev->battery_desc.name) { + ret = -ENOMEM; + goto err_stop; + } + cdev->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY; + cdev->battery_desc.properties = compx_battery_props; + cdev->battery_desc.num_properties = ARRAY_SIZE(compx_battery_props); + cdev->battery_desc.get_property = compx_battery_get_property; + + hid_set_drvdata(hdev, cdev); + schedule_delayed_work(&cdev->work, 0); + + return 0; + +err_stop: + hid_hw_stop(hdev); + return ret; +} + +static void compx_remove(struct hid_device *hdev) +{ + struct compx_device *cdev = hid_get_drvdata(hdev); + + if (cdev) { + cancel_delayed_work_sync(&cdev->work); + if (cdev->battery) + power_supply_unregister(cdev->battery); + } + + hid_hw_stop(hdev); +} + +static const struct hid_device_id compx_devices[] = { + { HID_USB_DEVICE(USB_VENDOR_ID_TELINK, + USB_DEVICE_ID_TELINK_COMPX_RECEIVER) }, + { } +}; +MODULE_DEVICE_TABLE(hid, compx_devices); + +static struct hid_driver compx_driver = { + .name = "compx", + .id_table = compx_devices, + .probe = compx_probe, + .remove = compx_remove, +}; +module_hid_driver(compx_driver); + +MODULE_AUTHOR("Zamiul Rashid"); +MODULE_DESCRIPTION("HID driver for Compx 2.4 GHz mouse receivers"); +MODULE_LICENSE("GPL"); diff --git a/drivers/hid/hid-ids.h b/drivers/hid/hid-ids.h index c40d818417a..fcc32ca153f 100644 --- a/drivers/hid/hid-ids.h +++ b/drivers/hid/hid-ids.h @@ -1489,6 +1489,7 @@ #define USB_DEVICE_ID_SYNAPTICS_ACER_SWITCH5 0x81a7 #define USB_VENDOR_ID_TELINK 0x248a +#define USB_DEVICE_ID_TELINK_COMPX_RECEIVER 0x5b2f #define USB_DEVICE_ID_TELINK_RAKK_DASIG_X 0xfb01 #define USB_DEVICE_ID_TELINK_RAKK_DASIG_X_DONGLE 0xfa02 #define USB_DEVICE_ID_TELINK_RAKK_DASIG_X_BT 0x8266 -- 2.53.0