* [PATCH 1/2] HID: compx: add battery reporting for Compx 2.4 GHz receivers
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
2026-12-01 4:15 ` [PATCH 2/2] HID: keychron: add battery reporting for the Keychron Link receiver Zamiul Rashid via B4 Relay
1 sibling, 0 replies; 3+ messages in thread
From: Zamiul Rashid via B4 Relay @ 2026-12-01 4:15 UTC (permalink / raw)
To: Jiri Kosina, Benjamin Tissoires; +Cc: linux-kernel, linux-input, Zamiul Rashid
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
^ permalink raw reply [flat|nested] 3+ messages in thread* [PATCH 2/2] HID: keychron: add battery reporting for the Keychron Link receiver
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
1 sibling, 0 replies; 3+ messages in thread
From: Zamiul Rashid via B4 Relay @ 2026-12-01 4:15 UTC (permalink / raw)
To: Jiri Kosina, Benjamin Tissoires; +Cc: linux-kernel, linux-input, Zamiul Rashid
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
^ permalink raw reply [flat|nested] 3+ messages in thread