From: Zamiul Rashid via B4 Relay <devnull+zamiulrashid1.gmail.com@kernel.org>
To: Jiri Kosina <jikos@kernel.org>, Benjamin Tissoires <bentiss@kernel.org>
Cc: linux-kernel@vger.kernel.org, linux-input@vger.kernel.org,
Zamiul Rashid <zamiulrashid1@gmail.com>
Subject: [PATCH 1/2] HID: compx: add battery reporting for Compx 2.4 GHz receivers
Date: Tue, 01 Dec 2026 10:15:36 +0600 [thread overview]
Message-ID: <20261201-compx-keychron-battery-v1-1-d5210d82dd67@gmail.com> (raw)
In-Reply-To: <20261201-compx-keychron-battery-v1-0-d5210d82dd67@gmail.com>
From: Zamiul Rashid <zamiulrashid1@gmail.com>
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 <zamiulrashid1@gmail.com>
---
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 <linux/delay.h>
+#include <linux/device.h>
+#include <linux/hid.h>
+#include <linux/module.h>
+#include <linux/power_supply.h>
+#include <linux/slab.h>
+#include <linux/spinlock.h>
+#include <linux/workqueue.h>
+
+#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
next prev parent reply other threads:[~2026-12-01 4:15 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 4:15 [PATCH 0/2] HID: battery reporting for Compx and Keychron Link mouse receivers Zamiul Rashid via B4 Relay
2026-12-01 4:15 ` Zamiul Rashid via B4 Relay [this message]
2026-12-01 4:15 ` [PATCH 2/2] HID: keychron: add battery reporting for the Keychron Link receiver Zamiul Rashid via B4 Relay
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261201-compx-keychron-battery-v1-1-d5210d82dd67@gmail.com \
--to=devnull+zamiulrashid1.gmail.com@kernel.org \
--cc=bentiss@kernel.org \
--cc=jikos@kernel.org \
--cc=linux-input@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=zamiulrashid1@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®