Linux Input/HID development
 help / color / mirror / Atom feed
From: Kevin Joyce <emujoyce@gmail.com>
To: Jiri Kosina <jikos@kernel.org>, Benjamin Tissoires <bentiss@kernel.org>
Cc: linux-input@vger.kernel.org, linux-kernel@vger.kernel.org,
	Kevin Joyce <emujoyce@gmail.com>
Subject: [PATCH] HID: compx: add battery driver for CompX 25a7 wireless mice
Date: Mon, 30 Nov 2026 23:12:10 -0500	[thread overview]
Message-ID: <20261201041210.435540-1-emujoyce@gmail.com> (raw)

CompX (Areson Technology Corp., VID 0x25a7) 2.4 GHz wireless and dual-mode
gaming mice (such as the Redragon Sniper Pro M801P-RGB) expose standard
mouse and keyboard HID interfaces alongside two 17-byte vendor collections
on interface 1:
  - Feature report 0x08 (usage page 0xff02): host requests
  - Input report   0x09 (usage page 0xff01): device replies

Add a HID battery driver that polls the receiver/mouse for link state
(command 0x03) and battery level and external USB power state (command
0x04) and exposes them through the power_supply subsystem.

Both commands are answered locally by the 2.4 GHz receiver without waking
a sleeping mouse, and the receiver caches the last reported battery state
across wireless idle sleeps. Retaining the last known capacity while
offline avoids UPower falling back to a synthetic 50% capacity level during
short wireless idle pauses while still reporting link state via ONLINE.

All raw reports are passed through to hidraw so userspace configuration
tools such as libratbag continue to work unchanged.

Tested on a Redragon Sniper Pro M801P-RGB over both the 2.4 GHz receiver
(25a7:fa09) and wired USB (25a7:fa0a), including awake/idle sleep
transitions, simultaneous dongle + cable attachment, and hot-unplug.

Assisted-by: Antigravity:gemini
Signed-off-by: Kevin Joyce <emujoyce@gmail.com>
---
 drivers/hid/Kconfig     |  16 ++
 drivers/hid/Makefile    |   1 +
 drivers/hid/hid-compx.c | 440 ++++++++++++++++++++++++++++++++++++++++
 drivers/hid/hid-ids.h   |   4 +
 4 files changed, 461 insertions(+)
 create mode 100644 drivers/hid/hid-compx.c

diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
index d17d960..afe443e 100644
--- a/drivers/hid/Kconfig
+++ b/drivers/hid/Kconfig
@@ -243,6 +243,22 @@ config HID_CHICONY
 	help
 	Support for Chicony Tactical pad and special keys on Chicony keyboards.
 
+config HID_COMPX
+	tristate "CompX wireless mice battery support"
+	depends on USB_HID
+	select POWER_SUPPLY
+	help
+	  Support for battery status reporting on CompX (VID 0x25a7) 2.4 GHz
+	  wireless and dual-mode mice that report battery state over vendor
+	  HID reports 0x08 and 0x09.
+
+	  Supported devices:
+	  - CompX 2.4G Wireless Receiver (25a7:fa09)
+	  - CompX 2.4G Dual Mode Mouse (25a7:fa0a)
+
+	  To compile this driver as a module, choose M here: the module will
+	  be called hid-compx.
+
 config HID_CORSAIR
 	tristate "Corsair devices"
 	depends on USB_HID && LEDS_CLASS
diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile
index dc30faa..f5f6864 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 0000000..af75c01
--- /dev/null
+++ b/drivers/hid/hid-compx.c
@@ -0,0 +1,440 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * HID battery driver for CompX (VID 0x25a7) wireless and dual-mode mice.
+ *
+ * Copyright (c) 2026 Kevin Joyce <emujoyce@gmail.com>
+ */
+
+#include <linux/delay.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"
+
+/*
+ * CompX 0x25a7 dual-mode mice and 2.4 GHz receivers expose two HID interfaces:
+ * interface 0 carries the mouse descriptor, while interface 1 carries keyboard
+ * and consumer collections plus two 17-byte vendor collections:
+ *   - Feature report 0x08 (usage page 0xff02): host -> device requests
+ *   - Input report   0x09 (usage page 0xff01): device -> host replies
+ *
+ * Byte 16 of every packet is a checksum: (0x55 - sum(bytes 0..15)) & 0xff.
+ *
+ * Relevant commands:
+ *   cmd 0x03 (online):  reply byte 5 = 0x01, byte 6 = 0 (asleep/off) | 1 (awake)
+ *   cmd 0x04 (battery): reply byte 5 = 0x02, byte 6 = level (0..10 -> 0..100%),
+ *                       byte 7 = 0 (battery) | 1 (USB power attached)
+ *
+ * Both commands are answered locally by the 2.4 GHz receiver in ~2 ms without
+ * waking a sleeping mouse; the receiver caches the last reported battery state
+ * across wireless idle sleeps.
+ */
+#define COMPX_USAGE_PAGE_ANSWER		0xff010000
+#define COMPX_USAGE_PAGE_REQUEST	0xff020000
+
+#define COMPX_REPORT_REQUEST		0x08
+#define COMPX_REPORT_ANSWER		0x09
+#define COMPX_REPORT_SIZE		17
+
+#define COMPX_CMD_ONLINE		0x03
+#define COMPX_CMD_BATTERY		0x04
+
+#define COMPX_OFFSET_CMD		1
+#define COMPX_OFFSET_STATUS		2
+#define COMPX_OFFSET_LEN		5
+#define COMPX_OFFSET_DATA		6
+
+#define COMPX_BATTERY_MAX_LEVEL		10
+#define COMPX_INITIAL_POLL_DELAY	msecs_to_jiffies(100)
+#define COMPX_POLL_INTERVAL		(30 * HZ)
+
+struct compx_drvdata {
+	struct hid_device *hdev;
+	struct power_supply *battery;
+	struct power_supply_desc desc;
+	struct delayed_work poll;
+	spinlock_t lock;	/* guards battery state and polling flag */
+	int capacity;
+	bool online;
+	bool ext_power;
+	bool has_battery;
+	bool polling;
+};
+
+static const enum power_supply_property compx_battery_props[] = {
+	POWER_SUPPLY_PROP_PRESENT,
+	POWER_SUPPLY_PROP_ONLINE,
+	POWER_SUPPLY_PROP_STATUS,
+	POWER_SUPPLY_PROP_CAPACITY,
+	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
+	POWER_SUPPLY_PROP_SCOPE,
+	POWER_SUPPLY_PROP_MODEL_NAME,
+	POWER_SUPPLY_PROP_MANUFACTURER,
+};
+
+static u8 compx_checksum(const u8 *data, size_t len)
+{
+	u8 sum = 0x55;
+	size_t i;
+
+	for (i = 0; i < len; i++)
+		sum -= data[i];
+
+	return sum;
+}
+
+static bool compx_has_vendor_reports(struct hid_device *hdev)
+{
+	struct hid_report *req, *ans;
+
+	req = hdev->report_enum[HID_FEATURE_REPORT]
+		.report_id_hash[COMPX_REPORT_REQUEST];
+	ans = hdev->report_enum[HID_INPUT_REPORT]
+		.report_id_hash[COMPX_REPORT_ANSWER];
+
+	if (!req || !ans || !req->maxfield || !ans->maxfield)
+		return false;
+
+	if ((req->field[0]->application & HID_USAGE_PAGE) !=
+	    COMPX_USAGE_PAGE_REQUEST)
+		return false;
+
+	if ((ans->field[0]->application & HID_USAGE_PAGE) !=
+	    COMPX_USAGE_PAGE_ANSWER)
+		return false;
+
+	return hid_report_len(req) == COMPX_REPORT_SIZE &&
+	       hid_report_len(ans) == COMPX_REPORT_SIZE;
+}
+
+static int compx_battery_get_property(struct power_supply *psy,
+				      enum power_supply_property prop,
+				      union power_supply_propval *val)
+{
+	struct compx_drvdata *compx = power_supply_get_drvdata(psy);
+	unsigned long flags;
+	int ret = 0;
+
+	spin_lock_irqsave(&compx->lock, flags);
+	switch (prop) {
+	case POWER_SUPPLY_PROP_PRESENT:
+		val->intval = compx->has_battery;
+		break;
+	case POWER_SUPPLY_PROP_ONLINE:
+		val->intval = compx->online;
+		break;
+	case POWER_SUPPLY_PROP_STATUS:
+		if (!compx->has_battery)
+			val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
+		else if (compx->ext_power && compx->capacity >= 100)
+			val->intval = POWER_SUPPLY_STATUS_FULL;
+		else if (compx->ext_power)
+			val->intval = POWER_SUPPLY_STATUS_CHARGING;
+		else
+			val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
+		break;
+	case POWER_SUPPLY_PROP_CAPACITY:
+		if (compx->has_battery)
+			val->intval = compx->capacity;
+		else
+			ret = -ENODATA;
+		break;
+	case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
+		if (!compx->has_battery)
+			val->intval = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
+		else if (compx->capacity >= 100)
+			val->intval = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
+		else if (compx->capacity >= 70)
+			val->intval = POWER_SUPPLY_CAPACITY_LEVEL_HIGH;
+		else if (compx->capacity >= 40)
+			val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
+		else if (compx->capacity >= 20)
+			val->intval = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
+		else
+			val->intval = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
+		break;
+	case POWER_SUPPLY_PROP_SCOPE:
+		val->intval = POWER_SUPPLY_SCOPE_DEVICE;
+		break;
+	case POWER_SUPPLY_PROP_MODEL_NAME:
+		val->strval = compx->hdev->name;
+		break;
+	case POWER_SUPPLY_PROP_MANUFACTURER:
+		val->strval = "CompX";
+		break;
+	default:
+		ret = -EINVAL;
+		break;
+	}
+	spin_unlock_irqrestore(&compx->lock, flags);
+
+	return ret;
+}
+
+static int compx_send_cmd(struct hid_device *hdev, u8 cmd)
+{
+	u8 *buf;
+	int ret;
+
+	buf = kzalloc(COMPX_REPORT_SIZE, GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
+
+	buf[0] = COMPX_REPORT_REQUEST;
+	buf[COMPX_OFFSET_CMD] = cmd;
+	buf[COMPX_REPORT_SIZE - 1] = compx_checksum(buf, COMPX_REPORT_SIZE - 1);
+
+	ret = hid_hw_raw_request(hdev, COMPX_REPORT_REQUEST, buf,
+				 COMPX_REPORT_SIZE, HID_FEATURE_REPORT,
+				 HID_REQ_SET_REPORT);
+	kfree(buf);
+
+	return ret < 0 ? ret : 0;
+}
+
+static void compx_poll_work(struct work_struct *work)
+{
+	struct compx_drvdata *compx = container_of(to_delayed_work(work),
+						   struct compx_drvdata, poll);
+	unsigned long flags;
+	bool polling;
+	int ret;
+
+	ret = compx_send_cmd(compx->hdev, COMPX_CMD_ONLINE);
+	if (ret < 0) {
+		hid_dbg(compx->hdev, "online query failed: %d\n", ret);
+		goto requeue;
+	}
+
+	/* Brief pause so the receiver returns the 0x03 reply before 0x04. */
+	fsleep(10 * USEC_PER_MSEC);
+
+	ret = compx_send_cmd(compx->hdev, COMPX_CMD_BATTERY);
+	if (ret < 0)
+		hid_dbg(compx->hdev, "battery query failed: %d\n", ret);
+
+requeue:
+	spin_lock_irqsave(&compx->lock, flags);
+	polling = compx->polling;
+	spin_unlock_irqrestore(&compx->lock, flags);
+
+	if (polling)
+		queue_delayed_work(system_dfl_wq, &compx->poll,
+				   COMPX_POLL_INTERVAL);
+}
+
+static int compx_raw_event(struct hid_device *hdev, struct hid_report *report,
+			   u8 *data, int size)
+{
+	struct compx_drvdata *compx = hid_get_drvdata(hdev);
+	unsigned long flags;
+	bool changed = false;
+
+	if (!compx || !compx->battery)
+		return 0;
+
+	if (size < COMPX_REPORT_SIZE || data[0] != COMPX_REPORT_ANSWER)
+		return 0;
+
+	if (data[COMPX_OFFSET_STATUS] != 0x00)
+		return 0;
+
+	if (data[COMPX_REPORT_SIZE - 1] !=
+	    compx_checksum(data, COMPX_REPORT_SIZE - 1))
+		return 0;
+
+	spin_lock_irqsave(&compx->lock, flags);
+	switch (data[COMPX_OFFSET_CMD]) {
+	case COMPX_CMD_ONLINE:
+		if (data[COMPX_OFFSET_LEN] >= 1) {
+			bool online = data[COMPX_OFFSET_DATA] != 0;
+
+			if (compx->online != online) {
+				compx->online = online;
+				changed = true;
+			}
+		}
+		break;
+	case COMPX_CMD_BATTERY:
+		if (data[COMPX_OFFSET_LEN] >= 2 &&
+		    data[COMPX_OFFSET_DATA] <= COMPX_BATTERY_MAX_LEVEL) {
+			int capacity = data[COMPX_OFFSET_DATA] * 10;
+			bool ext_power = data[COMPX_OFFSET_DATA + 1] != 0;
+
+			/*
+			 * Before the mouse has ever woken up, a freshly plugged
+			 * receiver can return level 0 while offline. Ignore a
+			 * zero reading until the device is online or we already
+			 * have a valid battery sample.
+			 */
+			if (!compx->online && !compx->has_battery && !capacity)
+				break;
+
+			if (!compx->has_battery ||
+			    compx->capacity != capacity ||
+			    compx->ext_power != ext_power) {
+				compx->capacity = capacity;
+				compx->ext_power = ext_power;
+				compx->has_battery = true;
+				changed = true;
+			}
+		}
+		break;
+	default:
+		break;
+	}
+	spin_unlock_irqrestore(&compx->lock, flags);
+
+	if (changed)
+		power_supply_changed(compx->battery);
+
+	/* Pass all reports through to hidraw so userspace tools keep working. */
+	return 0;
+}
+
+static int compx_probe(struct hid_device *hdev, const struct hid_device_id *id)
+{
+	struct power_supply_config cfg = {};
+	struct compx_drvdata *compx;
+	const char *name;
+	int ret;
+
+	compx = devm_kzalloc(&hdev->dev, sizeof(*compx), GFP_KERNEL);
+	if (!compx)
+		return -ENOMEM;
+
+	compx->hdev = hdev;
+	spin_lock_init(&compx->lock);
+	INIT_DELAYED_WORK(&compx->poll, compx_poll_work);
+	hid_set_drvdata(hdev, compx);
+
+	/* Preserve hid-generic's per-application input device split. */
+	hdev->quirks |= HID_QUIRK_INPUT_PER_APP;
+
+	ret = hid_parse(hdev);
+	if (ret)
+		return ret;
+
+	/* Only interface 1 carries the 0x08/0x09 vendor battery collections. */
+	if (!compx_has_vendor_reports(hdev))
+		return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
+
+	name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
+			      "compx-%04x-%d-battery", hdev->product, hdev->id);
+	if (!name)
+		return -ENOMEM;
+
+	compx->desc.name = name;
+	compx->desc.type = POWER_SUPPLY_TYPE_BATTERY;
+	compx->desc.properties = compx_battery_props;
+	compx->desc.num_properties = ARRAY_SIZE(compx_battery_props);
+	compx->desc.get_property = compx_battery_get_property;
+	cfg.drv_data = compx;
+
+	compx->battery = devm_power_supply_register(&hdev->dev, &compx->desc,
+						    &cfg);
+	if (IS_ERR(compx->battery))
+		return dev_err_probe(&hdev->dev, PTR_ERR(compx->battery),
+				     "failed to register battery power supply\n");
+
+	power_supply_powers(compx->battery, &hdev->dev);
+
+	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
+	if (ret)
+		return ret;
+
+	ret = hid_hw_open(hdev);
+	if (ret) {
+		hid_hw_stop(hdev);
+		return ret;
+	}
+
+	compx->polling = true;
+	queue_delayed_work(system_dfl_wq, &compx->poll,
+			   COMPX_INITIAL_POLL_DELAY);
+
+	return 0;
+}
+
+static void compx_stop_polling(struct compx_drvdata *compx)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&compx->lock, flags);
+	compx->polling = false;
+	spin_unlock_irqrestore(&compx->lock, flags);
+
+	cancel_delayed_work_sync(&compx->poll);
+}
+
+static void compx_remove(struct hid_device *hdev)
+{
+	struct compx_drvdata *compx = hid_get_drvdata(hdev);
+
+	if (compx && compx->battery) {
+		compx_stop_polling(compx);
+		hid_hw_close(hdev);
+	}
+
+	hid_hw_stop(hdev);
+}
+
+#ifdef CONFIG_PM
+static int compx_suspend(struct hid_device *hdev, pm_message_t message)
+{
+	struct compx_drvdata *compx = hid_get_drvdata(hdev);
+
+	if (compx && compx->battery)
+		compx_stop_polling(compx);
+
+	return 0;
+}
+
+static int compx_resume(struct hid_device *hdev)
+{
+	struct compx_drvdata *compx = hid_get_drvdata(hdev);
+	unsigned long flags;
+
+	if (compx && compx->battery) {
+		spin_lock_irqsave(&compx->lock, flags);
+		compx->polling = true;
+		spin_unlock_irqrestore(&compx->lock, flags);
+
+		queue_delayed_work(system_dfl_wq, &compx->poll,
+				   COMPX_INITIAL_POLL_DELAY);
+	}
+
+	return 0;
+}
+#endif
+
+static const struct hid_device_id compx_devices[] = {
+	{ HID_USB_DEVICE(USB_VENDOR_ID_COMPX,
+			 USB_DEVICE_ID_COMPX_2_4G_RECEIVER) },
+	{ HID_USB_DEVICE(USB_VENDOR_ID_COMPX,
+			 USB_DEVICE_ID_COMPX_2_4G_DUAL_MODE_MOUSE) },
+	{ }
+};
+MODULE_DEVICE_TABLE(hid, compx_devices);
+
+static struct hid_driver compx_driver = {
+	.name = "compx",
+	.id_table = compx_devices,
+	.probe = compx_probe,
+	.remove = compx_remove,
+	.raw_event = compx_raw_event,
+#ifdef CONFIG_PM
+	.suspend = compx_suspend,
+	.resume = compx_resume,
+	.reset_resume = compx_resume,
+#endif
+};
+module_hid_driver(compx_driver);
+
+MODULE_AUTHOR("Kevin Joyce <emujoyce@gmail.com>");
+MODULE_DESCRIPTION("HID battery driver for CompX 25a7 wireless mice");
+MODULE_LICENSE("GPL");
diff --git a/drivers/hid/hid-ids.h b/drivers/hid/hid-ids.h
index c40d818..9ba9a03 100644
--- a/drivers/hid/hid-ids.h
+++ b/drivers/hid/hid-ids.h
@@ -370,6 +370,10 @@
 #define USB_DEVICE_ID_CODEMERCS_IOW_FIRST	0x1500
 #define USB_DEVICE_ID_CODEMERCS_IOW_LAST	0x15ff
 
+#define USB_VENDOR_ID_COMPX				0x25a7
+#define USB_DEVICE_ID_COMPX_2_4G_RECEIVER		0xfa09
+#define USB_DEVICE_ID_COMPX_2_4G_DUAL_MODE_MOUSE	0xfa0a
+
 #define USB_VENDOR_ID_COOLER_MASTER	0x2516
 #define USB_DEVICE_ID_COOLER_MASTER_MICE_DONGLE	0x01b7
 
-- 
2.43.0


                 reply	other threads:[~2026-12-01  4:12 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261201041210.435540-1-emujoyce@gmail.com \
    --to=emujoyce@gmail.com \
    --cc=bentiss@kernel.org \
    --cc=jikos@kernel.org \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /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