Linux Input/HID development
 help / color / mirror / Atom feed
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 2/2] HID: keychron: add battery reporting for the Keychron Link receiver
Date: Tue, 01 Dec 2026 10:15:37 +0600	[thread overview]
Message-ID: <20261201-compx-keychron-battery-v1-2-d5210d82dd67@gmail.com> (raw)
In-Reply-To: <20261201-compx-keychron-battery-v1-0-d5210d82dd67@gmail.com>

From: Zamiul Rashid <zamiulrashid1@gmail.com>

Keychron mice (M1, M2, M3, M6, M7 and others) connect through the
Keychron Link 2.4 GHz receiver. The receiver does not answer battery
requests, so the battery level never reaches userspace.

Instead, the receiver sends input report 0x54 on its second interface
whenever the mouse connects or wakes up:

  54 e2 01 <link> 00 <battery %> 02 02 00 ...

Add a driver that listens for this event and exposes the level as a
power_supply. The interface that carries the event also carries a
joystick, which desktops do not open, so the driver keeps it open
itself. Without that, usbhid never reads the event. The battery is only
registered once the first event has arrived, because UPower ignores a
battery that appears without a reading.

Only the Type-C receiver (3434:d031) is added, as it is the only one
tested. The Type-A receiver (3434:d030) likely works the same way.

Tested with a Keychron M6.

Signed-off-by: Zamiul Rashid <zamiulrashid1@gmail.com>
---
 drivers/hid/Kconfig        |  11 +++
 drivers/hid/Makefile       |   1 +
 drivers/hid/hid-ids.h      |   1 +
 drivers/hid/hid-keychron.c | 236 +++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 249 insertions(+)

diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
index c88426a5ab5..e3d9f04cc09 100644
--- a/drivers/hid/Kconfig
+++ b/drivers/hid/Kconfig
@@ -522,6 +522,17 @@ config HID_MSI
 	of Handheld Console Controllers. Say M here to compile this driver as a
 	module. The module will be called hid-msi.
 
+config HID_KEYCHRON
+	tristate "Keychron Link mouse receiver"
+	depends on USB_HID
+	select POWER_SUPPLY
+	help
+	Support for the battery level of Keychron mice (M1, M2, M3, M6, M7
+	and others) connected through the Keychron Link 2.4 GHz receiver.
+
+	To compile this driver as a module, choose M here: the module will
+	be called hid-keychron.
+
 config HID_KEYTOUCH
 	tristate "Keytouch HID devices"
 	help
diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile
index f5f68644846..b4959cf756e 100644
--- a/drivers/hid/Makefile
+++ b/drivers/hid/Makefile
@@ -73,6 +73,7 @@ obj-$(CONFIG_HID_ICADE)		+= hid-icade.o
 obj-$(CONFIG_HID_ITE)		+= hid-ite.o
 obj-$(CONFIG_HID_JABRA)		+= hid-jabra.o
 obj-$(CONFIG_HID_KENSINGTON)	+= hid-kensington.o
+obj-$(CONFIG_HID_KEYCHRON)	+= hid-keychron.o
 obj-$(CONFIG_HID_KEYTOUCH)	+= hid-keytouch.o
 obj-$(CONFIG_HID_KYE)		+= hid-kye.o
 obj-$(CONFIG_HID_LCPOWER)	+= hid-lcpower.o
diff --git a/drivers/hid/hid-ids.h b/drivers/hid/hid-ids.h
index fcc32ca153f..83ca3e090a4 100644
--- a/drivers/hid/hid-ids.h
+++ b/drivers/hid/hid-ids.h
@@ -812,6 +812,7 @@
 
 #define USB_VENDOR_ID_KEYCHRON		0x3434
 #define USB_DEVICE_ID_KEYCHRON_LINK	0xd030
+#define USB_DEVICE_ID_KEYCHRON_LINK_TYPE_C	0xd031
 
 #define USB_VENDOR_ID_KWORLD		0x1b80
 #define USB_DEVICE_ID_KWORLD_RADIO_FM700	0xd700
diff --git a/drivers/hid/hid-keychron.c b/drivers/hid/hid-keychron.c
new file mode 100644
index 00000000000..8e3642f560b
--- /dev/null
+++ b/drivers/hid/hid-keychron.c
@@ -0,0 +1,236 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * HID driver for the Keychron Link 2.4 GHz mouse receiver
+ *
+ * The receiver used by Keychron mice (M1, M2, M3, M6, M7, ...) does not
+ * answer battery requests. Instead, its second interface sends input report
+ * 0x54 when the mouse connects or wakes up, and that report carries the
+ * battery level. This driver listens for the report and exposes the level
+ * as a power_supply.
+ *
+ * Copyright (c) 2026 Zamiul Rashid
+ */
+
+#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 KEYCHRON_EVENT_REPORT	0x54
+#define KEYCHRON_EV_STATUS	0xe2
+
+/* Offsets in the status event */
+#define KEYCHRON_OFF_EVENT	1
+#define KEYCHRON_OFF_BATTERY	5	/* percent */
+#define KEYCHRON_OFF_MARKER	6	/* 0x02 0x02 in a battery event */
+#define KEYCHRON_EVENT_MIN_LEN	8
+
+struct keychron_device {
+	struct hid_device *hdev;
+	struct power_supply *battery;
+	struct power_supply_desc battery_desc;
+	struct work_struct work;
+
+	spinlock_t lock;	/* protects capacity */
+	int capacity;
+};
+
+static enum power_supply_property keychron_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 keychron_battery_get_property(struct power_supply *psy,
+					 enum power_supply_property psp,
+					 union power_supply_propval *val)
+{
+	struct keychron_device *kdev = power_supply_get_drvdata(psy);
+	unsigned long flags;
+
+	switch (psp) {
+	case POWER_SUPPLY_PROP_PRESENT:
+		val->intval = 1;
+		break;
+	case POWER_SUPPLY_PROP_CAPACITY:
+		spin_lock_irqsave(&kdev->lock, flags);
+		val->intval = kdev->capacity;
+		spin_unlock_irqrestore(&kdev->lock, flags);
+		break;
+	case POWER_SUPPLY_PROP_STATUS:
+		/* The event does not say whether the mouse is charging. */
+		val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
+		break;
+	case POWER_SUPPLY_PROP_SCOPE:
+		val->intval = POWER_SUPPLY_SCOPE_DEVICE;
+		break;
+	case POWER_SUPPLY_PROP_MODEL_NAME:
+		val->strval = kdev->hdev->name;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static void keychron_work(struct work_struct *work)
+{
+	struct keychron_device *kdev = container_of(work, struct keychron_device,
+						    work);
+	struct hid_device *hdev = kdev->hdev;
+	struct power_supply_config psy_cfg = { .drv_data = kdev };
+	struct power_supply *battery;
+
+	if (kdev->battery) {
+		power_supply_changed(kdev->battery);
+		return;
+	}
+
+	/*
+	 * Register only once a level is known: userspace (UPower) skips a
+	 * battery that appears without a reading.
+	 */
+	battery = power_supply_register(&hdev->dev, &kdev->battery_desc,
+					&psy_cfg);
+	if (IS_ERR(battery)) {
+		hid_err(hdev, "cannot register battery: %ld\n",
+			PTR_ERR(battery));
+		return;
+	}
+
+	power_supply_powers(battery, &hdev->dev);
+	kdev->battery = battery;
+}
+
+static int keychron_raw_event(struct hid_device *hdev,
+			      struct hid_report *report, u8 *data, int size)
+{
+	struct keychron_device *kdev = hid_get_drvdata(hdev);
+	unsigned long flags;
+
+	if (!kdev || size < KEYCHRON_EVENT_MIN_LEN ||
+	    data[0] != KEYCHRON_EVENT_REPORT ||
+	    data[KEYCHRON_OFF_EVENT] != KEYCHRON_EV_STATUS ||
+	    data[KEYCHRON_OFF_MARKER] != 0x02 ||
+	    data[KEYCHRON_OFF_MARKER + 1] != 0x02 ||
+	    data[KEYCHRON_OFF_BATTERY] > 100)
+		return 0;
+
+	spin_lock_irqsave(&kdev->lock, flags);
+	kdev->capacity = data[KEYCHRON_OFF_BATTERY];
+	spin_unlock_irqrestore(&kdev->lock, flags);
+
+	/* Registering the battery may sleep; do it outside the IRQ path. */
+	schedule_work(&kdev->work);
+
+	return 0;
+}
+
+static int keychron_probe(struct hid_device *hdev,
+			  const struct hid_device_id *id)
+{
+	struct keychron_device *kdev = NULL;
+	int ret;
+
+	hid_set_drvdata(hdev, NULL);
+
+	ret = hid_parse(hdev);
+	if (ret)
+		return ret;
+
+	/* Only the interface that declares the status event gets a battery. */
+	if (hdev->report_enum[HID_INPUT_REPORT].report_id_hash[KEYCHRON_EVENT_REPORT]) {
+		kdev = devm_kzalloc(&hdev->dev, sizeof(*kdev), GFP_KERNEL);
+		if (!kdev)
+			return -ENOMEM;
+
+		kdev->battery_desc.name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
+							 "hid-%s-battery",
+							 dev_name(&hdev->dev));
+		if (!kdev->battery_desc.name)
+			return -ENOMEM;
+
+		kdev->hdev = hdev;
+		spin_lock_init(&kdev->lock);
+		INIT_WORK(&kdev->work, keychron_work);
+		kdev->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
+		kdev->battery_desc.properties = keychron_battery_props;
+		kdev->battery_desc.num_properties =
+			ARRAY_SIZE(keychron_battery_props);
+		kdev->battery_desc.get_property = keychron_battery_get_property;
+
+		/* Set before starting I/O: raw_event can run right away. */
+		hid_set_drvdata(hdev, kdev);
+	}
+
+	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
+	if (ret)
+		goto err_cancel;
+
+	if (!kdev)
+		return 0;
+
+	/*
+	 * This interface also carries a joystick, which desktops do not open.
+	 * Keep it open, or usbhid never reads the status event.
+	 */
+	ret = hid_hw_open(hdev);
+	if (ret) {
+		hid_hw_stop(hdev);
+		goto err_cancel;
+	}
+
+	return 0;
+
+err_cancel:
+	if (kdev) {
+		cancel_work_sync(&kdev->work);
+		if (kdev->battery)
+			power_supply_unregister(kdev->battery);
+	}
+	return ret;
+}
+
+static void keychron_remove(struct hid_device *hdev)
+{
+	struct keychron_device *kdev = hid_get_drvdata(hdev);
+
+	/* Stop I/O first so raw_event cannot queue the work again. */
+	if (kdev)
+		hid_hw_close(hdev);
+	hid_hw_stop(hdev);
+
+	if (kdev) {
+		cancel_work_sync(&kdev->work);
+		if (kdev->battery)
+			power_supply_unregister(kdev->battery);
+	}
+}
+
+static const struct hid_device_id keychron_devices[] = {
+	{ HID_USB_DEVICE(USB_VENDOR_ID_KEYCHRON,
+			 USB_DEVICE_ID_KEYCHRON_LINK_TYPE_C) },
+	{ }
+};
+MODULE_DEVICE_TABLE(hid, keychron_devices);
+
+static struct hid_driver keychron_driver = {
+	.name = "keychron",
+	.id_table = keychron_devices,
+	.probe = keychron_probe,
+	.remove = keychron_remove,
+	.raw_event = keychron_raw_event,
+};
+module_hid_driver(keychron_driver);
+
+MODULE_AUTHOR("Zamiul Rashid");
+MODULE_DESCRIPTION("HID driver for the Keychron Link mouse receiver");
+MODULE_LICENSE("GPL");

-- 
2.53.0



      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 ` [PATCH 1/2] HID: compx: add battery reporting for Compx 2.4 GHz receivers Zamiul Rashid via B4 Relay
2026-12-01  4:15 ` Zamiul Rashid via B4 Relay [this message]

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-2-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