lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite for Android: free password hash cracker in your pocket
[<prev] [next>] [day] [month] [year] [list]
Message-ID: <20250624130836.5743-1-huangzaiyang@oppo.com>
Date: Tue, 24 Jun 2025 21:08:36 +0800
From: huangzaiyang <huangzaiyang@...o.com>
To: <jikos@...nel.org>
CC: <bentiss@...nel.org>, <linux-input@...r.kernel.org>,
	<linux-kernel@...r.kernel.org>, huangzaiyang <huangzaiyang@...o.com>
Subject: [PATCH] drivers/hid: Implement a battery status polling mechanism for selected input devices.

Reading battery capacity and status could fail and end up with timeout
after 5s for some input devices, for example:8BitDo SN30 Pro+ gamepad.
Implement a battery status polling mechanism for selected input devices,
set HID_BATTERY_QUIRK_AVOID_QUERY and HID_BATTERY_QUIRK_POLLING_QUERY
for 8BitDo SN30 Pro+ gamepad.
to Avoid UI freezing when reading battery capacity/status.

Signed-off-by: huangzaiyang <huangzaiyang@...o.com>
---
 drivers/hid/hid-input.c | 63 +++++++++++++++++++++++++++++++++++++++++
 include/linux/hid.h     |  2 ++
 2 files changed, 65 insertions(+)

diff --git a/drivers/hid/hid-input.c b/drivers/hid/hid-input.c
index 9d80635a91eb..b113f5c49d03 100644
--- a/drivers/hid/hid-input.c
+++ b/drivers/hid/hid-input.c
@@ -341,6 +341,7 @@ static enum power_supply_property hidinput_battery_props[] = {
 #define HID_BATTERY_QUIRK_FEATURE      (1 << 1) /* ask for feature report */
 #define HID_BATTERY_QUIRK_IGNORE       (1 << 2) /* completely ignore the battery */
 #define HID_BATTERY_QUIRK_AVOID_QUERY  (1 << 3) /* do not query the battery */
+#define HID_BATTERY_QUIRK_POLLING_QUERY        (1 << 4) /* polling query the battery */

 static const struct hid_device_id hid_battery_quirks[] = {
        { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
@@ -390,6 +391,16 @@ static const struct hid_device_id hid_battery_quirks[] = {
         * set HID_BATTERY_QUIRK_IGNORE for all Elan I2C-HID devices.
         */
        { HID_I2C_DEVICE(USB_VENDOR_ID_ELAN, HID_ANY_ID), HID_BATTERY_QUIRK_IGNORE },
+       /*
+        * Reading battery capacity and status could fail and end up
+        * with timeout after 5s for some input devices, for example:
+        * 8BitDo SN30 Pro+ gamepad.
+        * set HID_BATTERY_QUIRK_AVOID_QUERY and HID_BATTERY_QUIRK_POLLING_QUERY
+        * for 8BitDo SN30 Pro+ gamepad.
+        * to Avoid UI freezing when reading battery capacity/status
+        */
+       { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_8BITDO_SN30_PRO_PLUS),
+         HID_BATTERY_QUIRK_AVOID_QUERY | HID_BATTERY_QUIRK_POLLING_QUERY },
        {}
 };

@@ -497,6 +508,33 @@ static int hidinput_get_battery_property(struct power_supply *psy,
        return ret;
 }

+/*
+ * hid input device battery polling handler
+ */
+static void hidinput_battery_polling_handler(struct work_struct *work)
+{
+       int value;
+       struct hid_device *dev = container_of(work, struct hid_device, battery_delayed_work.work);
+
+       dev->battery_status = HID_BATTERY_QUERIED;
+       dev->battery_avoid_query = true;
+       value = hidinput_query_battery_capacity(dev);
+       if (value < 0) {
+               dev->battery_status = HID_BATTERY_UNKNOWN;
+               hid_err(dev, "cannot get battery capacity from device!\n");
+       } else {
+               dev->battery_capacity = value;
+               dev->battery_avoid_query = false;
+               dev->battery_status = HID_BATTERY_REPORTED;
+               hid_err(dev, "get battery capacity from device:%d!\n", value);
+       }
+
+       /*keep polling period same to battery_ratelimit_time*/
+       queue_delayed_work(dev->battery_wq,
+                               &dev->battery_delayed_work,
+                               msecs_to_jiffies(30 * 1000));
+}
+
 static int hidinput_setup_battery(struct hid_device *dev, unsigned report_type,
                                  struct hid_field *field, bool is_percentage)
 {
@@ -571,6 +609,21 @@ static int hidinput_setup_battery(struct hid_device *dev, unsigned report_type,
        }

        power_supply_powers(dev->battery, &dev->dev);
+
+       if (quirks & HID_BATTERY_QUIRK_POLLING_QUERY) {
+               INIT_DELAYED_WORK(&dev->battery_delayed_work, hidinput_battery_polling_handler);
+               dev->battery_wq = create_singlethread_workqueue(dev_name(&dev->dev));
+               if (dev->battery_wq) {
+                       queue_delayed_work(dev->battery_wq,
+                                       &dev->battery_delayed_work,
+                                       msecs_to_jiffies(0));
+                       hid_warn(dev, "create battery poll work: %s\n", dev_name(&dev->dev));
+               } else {
+                       hid_err(dev, "can't create battery poll work: %ld\n",
+                                       PTR_ERR(dev->battery_wq));
+               }
+       }
+
        return 0;

 err_free_name:
@@ -583,11 +636,21 @@ static int hidinput_setup_battery(struct hid_device *dev, unsigned report_type,

 static void hidinput_cleanup_battery(struct hid_device *dev)
 {
+       unsigned quirks;
        const struct power_supply_desc *psy_desc;

        if (!dev->battery)
                return;

+       quirks = find_battery_quirk(dev);
+       if (quirks & HID_BATTERY_QUIRK_POLLING_QUERY) {
+               cancel_delayed_work_sync(&dev->battery_delayed_work);
+               if (dev->battery_wq) {
+                       destroy_workqueue(dev->battery_wq);
+                       hid_err(dev, "destroy battery poll work\n");
+               }
+       }
+
        psy_desc = dev->battery->desc;
        power_supply_unregister(dev->battery);
        kfree(psy_desc->name);
diff --git a/include/linux/hid.h b/include/linux/hid.h
index 568a9d8c749b..ff69aee70153 100644
--- a/include/linux/hid.h
+++ b/include/linux/hid.h
@@ -652,6 +652,8 @@ struct hid_device {
        enum hid_battery_status battery_status;
        bool battery_avoid_query;
        ktime_t battery_ratelimit_time;
+       struct delayed_work battery_delayed_work;
+       struct workqueue_struct *battery_wq;
 #endif

        unsigned long status;                                           /* see STAT flags above */
--
2.17.1

________________________________
OPPO

±¾µç×ÓÓʼþ¼°Æä¸½¼þº¬ÓÐOPPO¹«Ë¾µÄ±£ÃÜÐÅÏ¢£¬½öÏÞÓÚÓʼþÖ¸Ã÷µÄÊÕ¼þÈË£¨°üº¬¸öÈ˼°Èº×飩ʹÓ᣽ûÖ¹ÈκÎÈËÔÚδ¾­ÊÚȨµÄÇé¿öÏÂÒÔÈκÎÐÎʽʹÓá£Èç¹ûÄú´íÊÕÁ˱¾Óʼþ£¬ÇÐÎð´«²¥¡¢·Ö·¢¡¢¸´ÖÆ¡¢Ó¡Ë¢»òʹÓñ¾ÓʼþÖ®Èκβ¿·Ö»òÆäËùÔØÖ®ÈκÎÄÚÈÝ£¬²¢ÇëÁ¢¼´ÒÔµç×ÓÓʼþ֪ͨ·¢¼þÈ˲¢É¾³ý±¾Óʼþ¼°Æä¸½¼þ¡£
ÍøÂçͨѶ¹ÌÓÐȱÏÝ¿ÉÄܵ¼ÖÂÓʼþ±»½ØÁô¡¢Ð޸ġ¢¶ªÊ§¡¢ÆÆ»µ»ò°üº¬¼ÆËã»ú²¡¶¾µÈ²»°²È«Çé¿ö£¬OPPO¶Ô´ËÀà´íÎó»òÒÅ©¶øÒýÖÂÖ®ÈκÎËðʧ¸Å²»³Ðµ£ÔðÈβ¢±£ÁôÓë±¾ÓʼþÏà¹ØÖ®Ò»ÇÐȨÀû¡£
³ý·ÇÃ÷ȷ˵Ã÷£¬±¾Óʼþ¼°Æä¸½¼þÎÞÒâ×÷ΪÔÚÈκιú¼Ò»òµØÇøÖ®ÒªÔ¼¡¢ÕÐÀ¿»ò³Ðŵ£¬ÒàÎÞÒâ×÷ΪÈκν»Ò×»òºÏ֮ͬÕýʽȷÈÏ¡£ ·¢¼þÈË¡¢ÆäËùÊô»ú¹¹»òËùÊô»ú¹¹Ö®¹ØÁª»ú¹¹»òÈκÎÉÏÊö»ú¹¹Ö®¹É¶«¡¢¶­Ê¡¢¸ß¼¶¹ÜÀíÈËÔ±¡¢Ô±¹¤»òÆäËûÈκÎÈË£¨ÒÔϳơ°·¢¼þÈË¡±»ò¡°OPPO¡±£©²»Òò±¾ÓʼþÖ®ÎóËͶø·ÅÆúÆäËùÏíÖ®ÈκÎȨÀû£¬Ò಻¶ÔÒò¹ÊÒâ»ò¹ýʧʹÓøõÈÐÅÏ¢¶øÒý·¢»ò¿ÉÄÜÒý·¢µÄËðʧ³Ðµ£ÈκÎÔðÈΡ£
ÎÄ»¯²îÒìÅû¶£ºÒòÈ«ÇòÎÄ»¯²îÒìÓ°Ï죬µ¥´¿ÒÔYES\OK»òÆäËû¼òµ¥´Ê»ãµÄ»Ø¸´²¢²»¹¹³É·¢¼þÈ˶ÔÈκν»Ò×»òºÏ֮ͬÕýʽȷÈÏ»ò½ÓÊÜ£¬ÇëÓë·¢¼þÈËÔÙ´ÎÈ·ÈÏÒÔ»ñµÃÃ÷È·ÊéÃæÒâ¼û¡£·¢¼þÈ˲»¶ÔÈκÎÊÜÎÄ»¯²îÒìÓ°Ïì¶øµ¼Ö¹ÊÒâ»ò´íÎóʹÓøõÈÐÅÏ¢ËùÔì³ÉµÄÈκÎÖ±½Ó»ò¼ä½ÓË𺦳е£ÔðÈΡ£
This e-mail and its attachments contain confidential information from OPPO, which is intended only for the person or entity whose address is listed above. Any use of the information contained herein in any way (including, but not limited to, total or partial disclosure, reproduction, or dissemination) by persons other than the intended recipient(s) is prohibited. If you are not the intended recipient, please do not read, copy, distribute, or use this information. If you have received this transmission in error, please notify the sender immediately by reply e-mail and then delete this message.
Electronic communications may contain computer viruses or other defects inherently, may not be accurately and/or timely transmitted to other systems, or may be intercepted, modified ,delayed, deleted or interfered. OPPO shall not be liable for any damages that arise or may arise from such matter and reserves all rights in connection with the email.
Unless expressly stated, this e-mail and its attachments are provided without any warranty, acceptance or promise of any kind in any country or region, nor constitute a formal confirmation or acceptance of any transaction or contract. The sender, together with its affiliates or any shareholder, director, officer, employee or any other person of any such institution (hereinafter referred to as "sender" or "OPPO") does not waive any rights and shall not be liable for any damages that arise or may arise from the intentional or negligent use of such information.
Cultural Differences Disclosure: Due to global cultural differences, any reply with only YES\OK or other simple words does not constitute any confirmation or acceptance of any transaction or contract, please confirm with the sender again to ensure clear opinion in written form. The sender shall not be responsible for any direct or indirect damages resulting from the intentional or misuse of such information.
________________________________
OPPO

本电子邮件及其附件含有OPPO公司的保密信息,仅限于邮件指明的收件人(包含个人及群组)使用。禁止任何人在未经授权的情况下以任何形式使用。如果您错收了本邮件,切勿传播、分发、复制、印刷或使用本邮件之任何部分或其所载之任何内容,并请立即以电子邮件通知发件人并删除本邮件及其附件。
网络通讯固有缺陷可能导致邮件被截留、修改、丢失、破坏或包含计算机病毒等不安全情况,OPPO对此类错误或遗漏而引致之任何损失概不承担责任并保留与本邮件相关之一切权利。
除非明确说明,本邮件及其附件无意作为在任何国家或地区之要约、招揽或承诺,亦无意作为任何交易或合同之正式确认。 发件人、其所属机构或所属机构之关联机构或任何上述机构之股东、董事、高级管理人员、员工或其他任何人(以下称“发件人”或“OPPO”)不因本邮件之误送而放弃其所享之任何权利,亦不对因故意或过失使用该等信息而引发或可能引发的损失承担任何责任。
文化差异披露:因全球文化差异影响,单纯以YES\OK或其他简单词汇的回复并不构成发件人对任何交易或合同之正式确认或接受,请与发件人再次确认以获得明确书面意见。发件人不对任何受文化差异影响而导致故意或错误使用该等信息所造成的任何直接或间接损害承担责任。
This e-mail and its attachments contain confidential information from OPPO, which is intended only for the person or entity whose address is listed above. Any use of the information contained herein in any way (including, but not limited to, total or partial disclosure, reproduction, or dissemination) by persons other than the intended recipient(s) is prohibited. If you are not the intended recipient, please do not read, copy, distribute, or use this information. If you have received this transmission in error, please notify the sender immediately by reply e-mail and then delete this message.
Electronic communications may contain computer viruses or other defects inherently, may not be accurately and/or timely transmitted to other systems, or may be intercepted, modified ,delayed, deleted or interfered. OPPO shall not be liable for any damages that arise or may arise from such matter and reserves all rights in connection with the email.
Unless expressly stated, this e-mail and its attachments are provided without any warranty, acceptance or promise of any kind in any country or region, nor constitute a formal confirmation or acceptance of any transaction or contract. The sender, together with its affiliates or any shareholder, director, officer, employee or any other person of any such institution (hereinafter referred to as "sender" or "OPPO") does not waive any rights and shall not be liable for any damages that arise or may arise from the intentional or negligent use of such information.
Cultural Differences Disclosure: Due to global cultural differences, any reply with only YES\OK or other simple words does not constitute any confirmation or acceptance of any transaction or contract, please confirm with the sender again to ensure clear opinion in written form. The sender shall not be responsible for any direct or indirect damages resulting from the intentional or misuse of such information.

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ