linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] HID: hid-input: only ignore 0 battery events for digitizers
@ 2025-10-10  6:12 Dmitry Torokhov
  2025-10-10 10:43 ` 答复: [External Mail][PATCH] " 卢国宏
  2025-10-14 10:25 ` [PATCH] " Jiri Kosina
  0 siblings, 2 replies; 6+ messages in thread
From: Dmitry Torokhov @ 2025-10-10  6:12 UTC (permalink / raw)
  To: Jiri Kosina, Benjamin Tissoires
  Cc: 卢国宏, kenalba, linux-input, linux-kernel

Commit 581c4484769e ("HID: input: map digitizer battery usage") added
handling of battery events for digitizers (typically for batteries
presented in styli). Digitizers typically report correct battery levels
only when stylus is actively touching the surface, and in other cases
they may report battery level of 0. To avoid confusing consumers of the
battery information the code was added to filer out reports with 0
battery levels.

However there exist other kinds of devices that may legitimately report
0 battery levels. Fix this by filtering out 0-level reports only for
digitizer usages, and continue reporting them for other kinds of devices
(Smart Batteries, etc).

Reported-by: 卢国宏 <luguohong@xiaomi.com>
Tested-by: 卢国宏 <luguohong@xiaomi.com>
Fixes: 581c4484769e ("HID: input: map digitizer battery usage")
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
 drivers/hid/hid-input.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/drivers/hid/hid-input.c b/drivers/hid/hid-input.c
index ff1784b5c2a4..ba3f6655af9e 100644
--- a/drivers/hid/hid-input.c
+++ b/drivers/hid/hid-input.c
@@ -595,14 +595,18 @@ static void hidinput_cleanup_battery(struct hid_device *dev)
 	dev->battery = NULL;
 }
 
-static void hidinput_update_battery(struct hid_device *dev, int value)
+static void hidinput_update_battery(struct hid_device *dev,
+				    unsigned int usage, int value)
 {
 	int capacity;
 
 	if (!dev->battery)
 		return;
 
-	if (value == 0 || value < dev->battery_min || value > dev->battery_max)
+	if ((usage & HID_USAGE_PAGE) == HID_UP_DIGITIZER && value == 0)
+		return;
+
+	if (value < dev->battery_min || value > dev->battery_max)
 		return;
 
 	capacity = hidinput_scale_battery_capacity(dev, value);
@@ -1518,7 +1522,7 @@ void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct
 		bool handled = hidinput_set_battery_charge_status(hid, usage->hid, value);
 
 		if (!handled)
-			hidinput_update_battery(hid, value);
+			hidinput_update_battery(hid, usage->hid, value);
 
 		return;
 	}
-- 
2.51.0.740.g6adb054d12-goog


-- 
Dmitry

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* 答复: [External Mail][PATCH] HID: hid-input: only ignore 0 battery events for digitizers
  2025-10-10  6:12 [PATCH] HID: hid-input: only ignore 0 battery events for digitizers Dmitry Torokhov
@ 2025-10-10 10:43 ` 卢国宏
  2025-10-14  1:24   ` 卢国宏
  2025-10-14 10:25 ` [PATCH] " Jiri Kosina
  1 sibling, 1 reply; 6+ messages in thread
From: 卢国宏 @ 2025-10-10 10:43 UTC (permalink / raw)
  To: Dmitry Torokhov, Jiri Kosina, Benjamin Tissoires
  Cc: kenalba@google.com, linux-input@vger.kernel.org,
	linux-kernel@vger.kernel.org, 李鹏,
	宋密密, 卢国宏

Thanks, Dmitry.

Hi Jiri Kosina, Benjamin Tissoires, please let me know once you've merged this patch into the kernel. Thanks!

________________________________________
发件人: Dmitry Torokhov <dmitry.torokhov@gmail.com>
发送时间: 2025年10月10日 14:12
收件人: Jiri Kosina; Benjamin Tissoires
抄送: 卢国宏; kenalba@google.com; linux-input@vger.kernel.org; linux-kernel@vger.kernel.org
主题: [External Mail][PATCH] HID: hid-input: only ignore 0 battery events for digitizers

[外部邮件] 此邮件来源于小米公司外部,请谨慎处理。若对邮件安全性存疑,请将邮件转发给misec@xiaomi.com进行反馈

Commit 581c4484769e ("HID: input: map digitizer battery usage") added
handling of battery events for digitizers (typically for batteries
presented in styli). Digitizers typically report correct battery levels
only when stylus is actively touching the surface, and in other cases
they may report battery level of 0. To avoid confusing consumers of the
battery information the code was added to filer out reports with 0
battery levels.

However there exist other kinds of devices that may legitimately report
0 battery levels. Fix this by filtering out 0-level reports only for
digitizer usages, and continue reporting them for other kinds of devices
(Smart Batteries, etc).

Reported-by: 卢国宏 <luguohong@xiaomi.com>
Tested-by: 卢国宏 <luguohong@xiaomi.com>
Fixes: 581c4484769e ("HID: input: map digitizer battery usage")
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
 drivers/hid/hid-input.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/drivers/hid/hid-input.c b/drivers/hid/hid-input.c
index ff1784b5c2a4..ba3f6655af9e 100644
--- a/drivers/hid/hid-input.c
+++ b/drivers/hid/hid-input.c
@@ -595,14 +595,18 @@ static void hidinput_cleanup_battery(struct hid_device *dev)
        dev->battery = NULL;
 }

-static void hidinput_update_battery(struct hid_device *dev, int value)
+static void hidinput_update_battery(struct hid_device *dev,
+                                   unsigned int usage, int value)
 {
        int capacity;

        if (!dev->battery)
                return;

-       if (value == 0 || value < dev->battery_min || value > dev->battery_max)
+       if ((usage & HID_USAGE_PAGE) == HID_UP_DIGITIZER && value == 0)
+               return;
+
+       if (value < dev->battery_min || value > dev->battery_max)
                return;

        capacity = hidinput_scale_battery_capacity(dev, value);
@@ -1518,7 +1522,7 @@ void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct
                bool handled = hidinput_set_battery_charge_status(hid, usage->hid, value);

                if (!handled)
-                       hidinput_update_battery(hid, value);
+                       hidinput_update_battery(hid, usage->hid, value);

                return;
        }
--
2.51.0.740.g6adb054d12-goog


--
Dmitry
#/******本邮件及其附件含有小米公司的保密信息,仅限于发送给上面地址中列出的个人或群组。禁止任何其他人以任何形式使用(包括但不限于全部或部分地泄露、复制、或散发)本邮件中的信息。如果您错收了本邮件,请您立即电话或邮件通知发件人并删除本邮件! This e-mail and its attachments contain confidential information from XIAOMI, 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 receive this e-mail in error, please notify the sender by phone or email immediately and delete it!******/#

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* 答复: [External Mail][PATCH] HID: hid-input: only ignore 0 battery events for digitizers
  2025-10-10 10:43 ` 答复: [External Mail][PATCH] " 卢国宏
@ 2025-10-14  1:24   ` 卢国宏
  0 siblings, 0 replies; 6+ messages in thread
From: 卢国宏 @ 2025-10-14  1:24 UTC (permalink / raw)
  To: Dmitry Torokhov, Jiri Kosina, Benjamin Tissoires
  Cc: kenalba@google.com, linux-input@vger.kernel.org,
	linux-kernel@vger.kernel.org, 李鹏,
	宋密密, 卢国宏


Hi Jiri Kosina and Benjamin Tissoires! Is there any new progress on getting this patch into the kernel? I look forward to hearing from you. Thank you very much!

________________________________________
发件人: 卢国宏
发送时间: 2025年10月10日 18:43
收件人: Dmitry Torokhov; Jiri Kosina; Benjamin Tissoires
抄送: kenalba@google.com; linux-input@vger.kernel.org; linux-kernel@vger.kernel.org; 李鹏; 宋密密; 卢国宏
主题: 答复: [External Mail][PATCH] HID: hid-input: only ignore 0 battery events for digitizers

Thanks, Dmitry.

Hi Jiri Kosina, Benjamin Tissoires, please let me know once you've merged this patch into the kernel. Thanks!

________________________________________
发件人: Dmitry Torokhov <dmitry.torokhov@gmail.com>
发送时间: 2025年10月10日 14:12
收件人: Jiri Kosina; Benjamin Tissoires
抄送: 卢国宏; kenalba@google.com; linux-input@vger.kernel.org; linux-kernel@vger.kernel.org
主题: [External Mail][PATCH] HID: hid-input: only ignore 0 battery events for digitizers

[外部邮件] 此邮件来源于小米公司外部,请谨慎处理。若对邮件安全性存疑,请将邮件转发给misec@xiaomi.com进行反馈

Commit 581c4484769e ("HID: input: map digitizer battery usage") added
handling of battery events for digitizers (typically for batteries
presented in styli). Digitizers typically report correct battery levels
only when stylus is actively touching the surface, and in other cases
they may report battery level of 0. To avoid confusing consumers of the
battery information the code was added to filer out reports with 0
battery levels.

However there exist other kinds of devices that may legitimately report
0 battery levels. Fix this by filtering out 0-level reports only for
digitizer usages, and continue reporting them for other kinds of devices
(Smart Batteries, etc).

Reported-by: 卢国宏 <luguohong@xiaomi.com>
Tested-by: 卢国宏 <luguohong@xiaomi.com>
Fixes: 581c4484769e ("HID: input: map digitizer battery usage")
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
 drivers/hid/hid-input.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/drivers/hid/hid-input.c b/drivers/hid/hid-input.c
index ff1784b5c2a4..ba3f6655af9e 100644
--- a/drivers/hid/hid-input.c
+++ b/drivers/hid/hid-input.c
@@ -595,14 +595,18 @@ static void hidinput_cleanup_battery(struct hid_device *dev)
        dev->battery = NULL;
 }

-static void hidinput_update_battery(struct hid_device *dev, int value)
+static void hidinput_update_battery(struct hid_device *dev,
+                                   unsigned int usage, int value)
 {
        int capacity;

        if (!dev->battery)
                return;

-       if (value == 0 || value < dev->battery_min || value > dev->battery_max)
+       if ((usage & HID_USAGE_PAGE) == HID_UP_DIGITIZER && value == 0)
+               return;
+
+       if (value < dev->battery_min || value > dev->battery_max)
                return;

        capacity = hidinput_scale_battery_capacity(dev, value);
@@ -1518,7 +1522,7 @@ void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct
                bool handled = hidinput_set_battery_charge_status(hid, usage->hid, value);

                if (!handled)
-                       hidinput_update_battery(hid, value);
+                       hidinput_update_battery(hid, usage->hid, value);

                return;
        }
--
2.51.0.740.g6adb054d12-goog


--
Dmitry
#/******本邮件及其附件含有小米公司的保密信息,仅限于发送给上面地址中列出的个人或群组。禁止任何其他人以任何形式使用(包括但不限于全部或部分地泄露、复制、或散发)本邮件中的信息。如果您错收了本邮件,请您立即电话或邮件通知发件人并删除本邮件! This e-mail and its attachments contain confidential information from XIAOMI, 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 receive this e-mail in error, please notify the sender by phone or email immediately and delete it!******/#

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] HID: hid-input: only ignore 0 battery events for digitizers
  2025-10-10  6:12 [PATCH] HID: hid-input: only ignore 0 battery events for digitizers Dmitry Torokhov
  2025-10-10 10:43 ` 答复: [External Mail][PATCH] " 卢国宏
@ 2025-10-14 10:25 ` Jiri Kosina
  2025-10-15  4:31   ` Dmitry Torokhov
  1 sibling, 1 reply; 6+ messages in thread
From: Jiri Kosina @ 2025-10-14 10:25 UTC (permalink / raw)
  To: Dmitry Torokhov
  Cc: Benjamin Tissoires, 卢国宏, kenalba,
	linux-input, linux-kernel

On Thu, 9 Oct 2025, Dmitry Torokhov wrote:

> Commit 581c4484769e ("HID: input: map digitizer battery usage") added
> handling of battery events for digitizers (typically for batteries
> presented in styli). Digitizers typically report correct battery levels
> only when stylus is actively touching the surface, and in other cases
> they may report battery level of 0. To avoid confusing consumers of the
> battery information the code was added to filer out reports with 0
> battery levels.
> 
> However there exist other kinds of devices that may legitimately report
> 0 battery levels. Fix this by filtering out 0-level reports only for
> digitizer usages, and continue reporting them for other kinds of devices
> (Smart Batteries, etc).
> 
> Reported-by: 卢国宏 <luguohong@xiaomi.com>
> Tested-by: 卢国宏 <luguohong@xiaomi.com>
> Fixes: 581c4484769e ("HID: input: map digitizer battery usage")
> Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
> ---
>  drivers/hid/hid-input.c | 10 +++++++---
>  1 file changed, 7 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/hid/hid-input.c b/drivers/hid/hid-input.c
> index ff1784b5c2a4..ba3f6655af9e 100644
> --- a/drivers/hid/hid-input.c
> +++ b/drivers/hid/hid-input.c
> @@ -595,14 +595,18 @@ static void hidinput_cleanup_battery(struct hid_device *dev)
>  	dev->battery = NULL;
>  }
>  
> -static void hidinput_update_battery(struct hid_device *dev, int value)
> +static void hidinput_update_battery(struct hid_device *dev,
> +				    unsigned int usage, int value)
>  {
>  	int capacity;
>  
>  	if (!dev->battery)
>  		return;
>  
> -	if (value == 0 || value < dev->battery_min || value > dev->battery_max)
> +	if ((usage & HID_USAGE_PAGE) == HID_UP_DIGITIZER && value == 0)
> +		return;
> +
> +	if (value < dev->battery_min || value > dev->battery_max)
>  		return;
>  
>  	capacity = hidinput_scale_battery_capacity(dev, value);
> @@ -1518,7 +1522,7 @@ void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct
>  		bool handled = hidinput_set_battery_charge_status(hid, usage->hid, value);
>  
>  		if (!handled)
> -			hidinput_update_battery(hid, value);
> +			hidinput_update_battery(hid, usage->hid, value);
>  
>  		return;

Hi Dmitry,

thanks for the fix. It doesn't apply cleanly on a reasonably recent tree 
though, as since e94536e1d1818b09 we're already propagating usage to 
hidinput_update_battery(), and we're issuing explicit call to 
power_supply_changed() as well.

Could you please refresh on a more recent codebase and resubmit? I could 
do that myself, but I guess you also have a way to test the patch with 
your use-case ... ?

Thanks,

-- 
Jiri Kosina
SUSE Labs


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] HID: hid-input: only ignore 0 battery events for digitizers
  2025-10-14 10:25 ` [PATCH] " Jiri Kosina
@ 2025-10-15  4:31   ` Dmitry Torokhov
  2025-10-15  7:48     ` 答复: [External Mail]Re: " 卢国宏
  0 siblings, 1 reply; 6+ messages in thread
From: Dmitry Torokhov @ 2025-10-15  4:31 UTC (permalink / raw)
  To: Jiri Kosina
  Cc: Benjamin Tissoires, 卢国宏, kenalba,
	linux-input, linux-kernel

Hi Jiri,

On Tue, Oct 14, 2025 at 12:25:25PM +0200, Jiri Kosina wrote:
> 
> Hi Dmitry,
> 
> thanks for the fix. It doesn't apply cleanly on a reasonably recent tree 
> though, as since e94536e1d1818b09 we're already propagating usage to 
> hidinput_update_battery(), and we're issuing explicit call to 
> power_supply_changed() as well.
> 
> Could you please refresh on a more recent codebase and resubmit? I could 
> do that myself, but I guess you also have a way to test the patch with 
> your use-case ... ?

Sorry about that, I made the patch on top of my tree which lags behind.
I just sent a v2 rebased on top of linux-next.

I actually do not have the hardware to test, we have to rely on 卢国宏
to do it for us.

Thanks.

-- 
Dmitry

^ permalink raw reply	[flat|nested] 6+ messages in thread

* 答复: [External Mail]Re: [PATCH] HID: hid-input: only ignore 0 battery events for digitizers
  2025-10-15  4:31   ` Dmitry Torokhov
@ 2025-10-15  7:48     ` 卢国宏
  0 siblings, 0 replies; 6+ messages in thread
From: 卢国宏 @ 2025-10-15  7:48 UTC (permalink / raw)
  To: Dmitry Torokhov, Jiri Kosina
  Cc: Benjamin Tissoires, kenalba@google.com,
	linux-input@vger.kernel.org, linux-kernel@vger.kernel.org,
	李鹏, 卢国宏

[-- Attachment #1: Type: text/plain, Size: 2247 bytes --]

Hi Jiri,
The last time I verified Dmitry's patch, it was based on the latest Linux code. The patch at that time is attached as "Patch reporting HID device zero battery.jpg". As I understand it, you can submit Dmitry's V2 patch directly to the kernel without retesting.
Thanks.

--
guohong
________________________________________
发件人: Dmitry Torokhov <dmitry.torokhov@gmail.com>
发送时间: 2025年10月15日 12:31:10
收件人: Jiri Kosina
抄送: Benjamin Tissoires; 卢国宏; kenalba@google.com; linux-input@vger.kernel.org; linux-kernel@vger.kernel.org
主题: [External Mail]Re: [PATCH] HID: hid-input: only ignore 0 battery events for digitizers

[外部邮件] 此邮件来源于小米公司外部,请谨慎处理。若对邮件安全性存疑,请将邮件转发给misec@xiaomi.com进行反馈

Hi Jiri,

On Tue, Oct 14, 2025 at 12:25:25PM +0200, Jiri Kosina wrote:
>
> Hi Dmitry,
>
> thanks for the fix. It doesn't apply cleanly on a reasonably recent tree
> though, as since e94536e1d1818b09 we're already propagating usage to
> hidinput_update_battery(), and we're issuing explicit call to
> power_supply_changed() as well.
>
> Could you please refresh on a more recent codebase and resubmit? I could
> do that myself, but I guess you also have a way to test the patch with
> your use-case ... ?

Sorry about that, I made the patch on top of my tree which lags behind.
I just sent a v2 rebased on top of linux-next.

I actually do not have the hardware to test, we have to rely on 卢国宏
to do it for us.

Thanks.

--
Dmitry
#/******本邮件及其附件含有小米公司的保密信息,仅限于发送给上面地址中列出的个人或群组。禁止任何其他人以任何形式使用(包括但不限于全部或部分地泄露、复制、或散发)本邮件中的信息。如果您错收了本邮件,请您立即电话或邮件通知发件人并删除本邮件! This e-mail and its attachments contain confidential information from XIAOMI, 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 receive this e-mail in error, please notify the sender by phone or email immediately and delete it!******/#

[-- Attachment #2: Patch reporting HID device zero battery.jpg --]
[-- Type: image/jpeg, Size: 482355 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2025-10-15  7:48 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-10-10  6:12 [PATCH] HID: hid-input: only ignore 0 battery events for digitizers Dmitry Torokhov
2025-10-10 10:43 ` 答复: [External Mail][PATCH] " 卢国宏
2025-10-14  1:24   ` 卢国宏
2025-10-14 10:25 ` [PATCH] " Jiri Kosina
2025-10-15  4:31   ` Dmitry Torokhov
2025-10-15  7:48     ` 答复: [External Mail]Re: " 卢国宏

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).