linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Re: The zero power level of the HID device in kernel 6.12 is not reported from the kernel to the upper layer.
       [not found] <d2cada7efe8d4436b6e638fa1e0aaefb@xiaomi.com>
@ 2025-09-19  8:40 ` José Expósito
  2025-09-19 15:04   ` Dmitry Torokhov
  0 siblings, 1 reply; 8+ messages in thread
From: José Expósito @ 2025-09-19  8:40 UTC (permalink / raw)
  To: 卢国宏, linux-input, linux-kernel, jikos,
	bentiss, dmitry.torokhov
  Cc: 李鹏, Fei1 Jiang 蒋飞,
	宋密密

Hi 卢国宏,

Thanks for reporting this issue.

In the furure, when reporting bugs, it is prefered to send them to the
mailing list (linux-input@vger.kernel.org and linux-kernel@vger.kernel.org)
to discuss them in public.

Let me forward your email to the mailing list and also CC Dmitry, the
author of that code, who might help us understand the problem.

On Tue, Sep 16, 2025 at 12:29:32PM +0000, 卢国宏 wrote:
> Hi, jose!
>
> We encountered a problem where the zero battery level of the HID device
> in kernel 6.12 was not reported from the kernel to the upper layer.
> I checked the HID protocol and it doesn't say that there is no need to
> report the zero power of the HID device. For details, see page 381 of
> the HID protocol, 31.4 Battery Measures. "Absolute State Of Charge DV
> The predicted remaining battery capacity expressed as a percentage of
> design capacity. (Units are %. The value may be greater than 100%.)".
> However, in the file hid-input.c in kernel 6.12, the following code:
> 
> static void hidinput_update_battery(struct hid_device *dev, unsigned int usage,
>                                     int value)
> {
>         int capacity;
> 
>         if (!dev->battery)
>                 return;
> 
>         if (hidinput_update_battery_charge_status(dev, usage, value)) {
>                 power_supply_changed(dev->battery);
>                 return;
>         }
> 
>         if (value == 0 || value < dev->battery_min || value > dev->battery_max)
>                 return;
> 
>         capacity = hidinput_scale_battery_capacity(dev, value);
> 
>          ......
> 
> }
> 
> The parameter value is the power level. When the value is 0, the above code
> returns without reporting.
> Is this a problem?
> We're currently experiencing this issue on Android 16. The upper layer of
> Android needs to receive a zero battery level before it can take appropriate
> action.
> Could you please help me evaluate whether we should remove the behavior of
> returning to zero battery?
>
> Thanks!
> #/******本邮件及其附件含有小米公司的保密信息,仅限于发送给上面地址中列出的个人或群组。禁止任何其他人以任何形式使用(包括但不限于全部或部分地泄露、复制、或散发)本邮件中的信息。如果您错收了本邮件,请您立即电话或邮件通知发件人并删除本邮件! 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!******/#

It indeed looks like it could be problematic.

Values are allowed ot be grater than 100, however, I didn't find
any references to negative values. Since it is a percentage, it
make sense to limit it to 0%, i.e., not allowing negative values.

I think that removing the "value == 0" check, or replacing it with
"value < 0" should fix the issue.

By the way, the "Fully Discharged" value (0x00850047), section 31.3.1,
is not handled by the kernel. Do you know if Android handles that
instead or in addition to a 0% battery?

Jose

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

* Re: The zero power level of the HID device in kernel 6.12 is not reported from the kernel to the upper layer.
  2025-09-19  8:40 ` The zero power level of the HID device in kernel 6.12 is not reported from the kernel to the upper layer José Expósito
@ 2025-09-19 15:04   ` Dmitry Torokhov
  2025-09-22  9:29     ` 答复: [External Mail]Re: " 卢国宏
  0 siblings, 1 reply; 8+ messages in thread
From: Dmitry Torokhov @ 2025-09-19 15:04 UTC (permalink / raw)
  To: José Expósito
  Cc: 卢国宏, linux-input, linux-kernel, jikos,
	bentiss, 李鹏, Fei1 Jiang 蒋飞,
	宋密密

Hi,

On Fri, Sep 19, 2025 at 10:40:38AM +0200, José Expósito wrote:
> Hi 卢国宏,
> 
> Thanks for reporting this issue.
> 
> In the furure, when reporting bugs, it is prefered to send them to the
> mailing list (linux-input@vger.kernel.org and linux-kernel@vger.kernel.org)
> to discuss them in public.
> 
> Let me forward your email to the mailing list and also CC Dmitry, the
> author of that code, who might help us understand the problem.
> 
> On Tue, Sep 16, 2025 at 12:29:32PM +0000, 卢国宏 wrote:
> > Hi, jose!
> >
> > We encountered a problem where the zero battery level of the HID device
> > in kernel 6.12 was not reported from the kernel to the upper layer.
> > I checked the HID protocol and it doesn't say that there is no need to
> > report the zero power of the HID device. For details, see page 381 of
> > the HID protocol, 31.4 Battery Measures. "Absolute State Of Charge DV
> > The predicted remaining battery capacity expressed as a percentage of
> > design capacity. (Units are %. The value may be greater than 100%.)".
> > However, in the file hid-input.c in kernel 6.12, the following code:
> > 
> > static void hidinput_update_battery(struct hid_device *dev, unsigned int usage,
> >                                     int value)
> > {
> >         int capacity;
> > 
> >         if (!dev->battery)
> >                 return;
> > 
> >         if (hidinput_update_battery_charge_status(dev, usage, value)) {
> >                 power_supply_changed(dev->battery);
> >                 return;
> >         }
> > 
> >         if (value == 0 || value < dev->battery_min || value > dev->battery_max)
> >                 return;
> > 
> >         capacity = hidinput_scale_battery_capacity(dev, value);
> > 
> >          ......
> > 
> > }
> > 
> > The parameter value is the power level. When the value is 0, the above code
> > returns without reporting.
> > Is this a problem?
> > We're currently experiencing this issue on Android 16. The upper layer of
> > Android needs to receive a zero battery level before it can take appropriate
> > action.

What kind of action are we talking about? Section 31 of the HID
specification defines events for "Smart Battery" ("To comply with the
Smart Battery Specification, the Battery System must support the
functions defined in the Battery and Charger usage tables. For details,
see Section 4.2, “Battery System Page (x85).”) and is typically used for
"battery pack for cellular phones (principal source), the battery
pack(s) for notebook computers (auxiliary source), and the sealed
batteries in uninterruptible power supplies (auxiliary source)."

Is your use case main battery or battery in a stylus or some other
peripheral?

> > Could you please help me evaluate whether we should remove the behavior of
> > returning to zero battery?
> >
> > Thanks!
> > #/******本邮件及其附件含有小米公司的保密信息,仅限于发送给上面地址中列出的个人或群组。禁止任何其他人以任何形式使用(包括但不限于全部或部分地泄露、复制、或散发)本邮件中的信息。如果您错收了本邮件,请您立即电话或邮件通知发件人并删除本邮件! 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!******/#
> 
> It indeed looks like it could be problematic.
> 
> Values are allowed ot be grater than 100, however, I didn't find
> any references to negative values. Since it is a percentage, it
> make sense to limit it to 0%, i.e., not allowing negative values.
> 
> I think that removing the "value == 0" check, or replacing it with
> "value < 0" should fix the issue.

If we are dealing with peripherals (stylus for example) - for which this
piece of code was written - how a battery powered peripheral that is
fully discharged can communicate it's battery state of 0?

I think we have observed bogus reports with 0 values (IIRC trying to
query battery strength when stylus is not in proximity would yield
responses with 0 strength).

Thanks.

-- 
Dmitry

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

* 答复: [External Mail]Re: The zero power level of the HID device in kernel 6.12 is not reported from the kernel to the upper layer.
  2025-09-19 15:04   ` Dmitry Torokhov
@ 2025-09-22  9:29     ` 卢国宏
  2025-09-25  4:26       ` Dmitry Torokhov
  0 siblings, 1 reply; 8+ messages in thread
From: 卢国宏 @ 2025-09-22  9:29 UTC (permalink / raw)
  To: Dmitry Torokhov, José Expósito
  Cc: linux-input@vger.kernel.org, linux-kernel@vger.kernel.org,
	jikos@kernel.org, bentiss@kernel.org, 李鹏,
	Fei1 Jiang 蒋飞, 宋密密,
	卢国宏

Hi, jose!
I don't see any Android handling of ""Fully Discharged" value (0x00850047), section 31.3.1"".

________________________________________
发件人: Dmitry Torokhov <dmitry.torokhov@gmail.com>
发送时间: 2025年9月19日 23:04
收件人: José Expósito
抄送: 卢国宏; linux-input@vger.kernel.org; linux-kernel@vger.kernel.org; jikos@kernel.org; bentiss@kernel.org; 李鹏; Fei1 Jiang 蒋飞; 宋密密
主题: [External Mail]Re: The zero power level of the HID device in kernel 6.12 is not reported from the kernel to the upper layer.

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

Hi,

On Fri, Sep 19, 2025 at 10:40:38AM +0200, José Expósito wrote:
> Hi 卢国宏,
>
> Thanks for reporting this issue.
>
> In the furure, when reporting bugs, it is prefered to send them to the
> mailing list (linux-input@vger.kernel.org and linux-kernel@vger.kernel.org)
> to discuss them in public.
>
> Let me forward your email to the mailing list and also CC Dmitry, the
> author of that code, who might help us understand the problem.
>
> On Tue, Sep 16, 2025 at 12:29:32PM +0000, 卢国宏 wrote:
> > Hi, jose!
> >
> > We encountered a problem where the zero battery level of the HID device
> > in kernel 6.12 was not reported from the kernel to the upper layer.
> > I checked the HID protocol and it doesn't say that there is no need to
> > report the zero power of the HID device. For details, see page 381 of
> > the HID protocol, 31.4 Battery Measures. "Absolute State Of Charge DV
> > The predicted remaining battery capacity expressed as a percentage of
> > design capacity. (Units are %. The value may be greater than 100%.)".
> > However, in the file hid-input.c in kernel 6.12, the following code:
> >
> > static void hidinput_update_battery(struct hid_device *dev, unsigned int usage,
> >                                     int value)
> > {
> >         int capacity;
> >
> >         if (!dev->battery)
> >                 return;
> >
> >         if (hidinput_update_battery_charge_status(dev, usage, value)) {
> >                 power_supply_changed(dev->battery);
> >                 return;
> >         }
> >
> >         if (value == 0 || value < dev->battery_min || value > dev->battery_max)
> >                 return;
> >
> >         capacity = hidinput_scale_battery_capacity(dev, value);
> >
> >          ......
> >
> > }
> >
> > The parameter value is the power level. When the value is 0, the above code
> > returns without reporting.
> > Is this a problem?
> > We're currently experiencing this issue on Android 16. The upper layer of
> > Android needs to receive a zero battery level before it can take appropriate
> > action.

What kind of action are we talking about? Section 31 of the HID
specification defines events for "Smart Battery" ("To comply with the
Smart Battery Specification, the Battery System must support the
functions defined in the Battery and Charger usage tables. For details,
see Section 4.2, “Battery System Page (x85).”) and is typically used for
"battery pack for cellular phones (principal source), the battery
pack(s) for notebook computers (auxiliary source), and the sealed
batteries in uninterruptible power supplies (auxiliary source)."

Is your use case main battery or battery in a stylus or some other
peripheral?


--->>>
What we are discussing is the code implementation of Section 31 of the HID protocol: 31 Battery System Page (0x85).
Our scenario is: an Android phone is connected to a handle via USB. The handle is a HID device with a battery. The power of the battery in the handle is sent to the bottom layer (kernel) of the phone via USB. The bottom layer of the phone then reports this power to the upper layer of Android through the HID driver.


> > Could you please help me evaluate whether we should remove the behavior of
> > returning to zero battery?
> >
> > Thanks!
> > #/******本邮件及其附件含有小米公司的保密信息,仅限于发送给上面地址中列出的个人或群组。禁止任何其他人以任何形式使用(包括但不限于全部或部分地泄露、复制、或散发)本邮件中的信息。如果您错收了本邮件,请您立即电话或邮件通知发件人并删除本邮件! 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!******/#
>
> It indeed looks like it could be problematic.
>
> Values are allowed ot be grater than 100, however, I didn't find
> any references to negative values. Since it is a percentage, it
> make sense to limit it to 0%, i.e., not allowing negative values.
>
> I think that removing the "value == 0" check, or replacing it with
> "value < 0" should fix the issue.

If we are dealing with peripherals (stylus for example) - for which this
piece of code was written - how a battery powered peripheral that is
fully discharged can communicate it's battery state of 0?


--->>>
Our controller is like a power bank. When the battery level is above 0, it will charge the phone. When the battery level reaches 0, it will stop charging the phone. Because the controller consumes very little power, it can still work for a while. However, you need to inform the phone of the 0 battery level so that the phone will display a relevant prompt.


I think we have observed bogus reports with 0 values (IIRC trying to
query battery strength when stylus is not in proximity would yield
responses with 0 strength).

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!******/#

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

* Re: 答复: [External Mail]Re: The zero power level of the HID device in kernel 6.12 is not reported from the kernel to the upper layer.
  2025-09-22  9:29     ` 答复: [External Mail]Re: " 卢国宏
@ 2025-09-25  4:26       ` Dmitry Torokhov
  2025-09-26 12:03         ` 答复: " 卢国宏
  0 siblings, 1 reply; 8+ messages in thread
From: Dmitry Torokhov @ 2025-09-25  4:26 UTC (permalink / raw)
  To: 卢国宏
  Cc: José Expósito, linux-input@vger.kernel.org,
	linux-kernel@vger.kernel.org, jikos@kernel.org,
	bentiss@kernel.org, 李鹏, Fei1 Jiang 蒋飞,
	宋密密

On Mon, Sep 22, 2025 at 09:29:20AM +0000, 卢国宏 wrote:
> 
> What kind of action are we talking about? Section 31 of the HID
> specification defines events for "Smart Battery" ("To comply with the
> Smart Battery Specification, the Battery System must support the
> functions defined in the Battery and Charger usage tables. For details,
> see Section 4.2, “Battery System Page (x85).”) and is typically used for
> "battery pack for cellular phones (principal source), the battery
> pack(s) for notebook computers (auxiliary source), and the sealed
> batteries in uninterruptible power supplies (auxiliary source)."
> 
> Is your use case main battery or battery in a stylus or some other
> peripheral?
> 
> 
> --->>>
> What we are discussing is the code implementation of Section 31 of the
> HID protocol: 31 Battery System Page (0x85). Our scenario is: an
> Android phone is connected to a handle via USB. The handle is a HID
> device with a battery. The power of the battery in the handle is sent
> to the bottom layer (kernel) of the phone via USB. The bottom layer of
> the phone then reports this power to the upper layer of Android
> through the HID driver.

I see. I guess we can try only filtering out 0 reports for the
digitizers, leaving other devices with batteries alone. Something like
this:


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;
 	}


Thanks.

-- 
Dmitry

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

* 答复: 答复: [External Mail]Re: The zero power level of the HID device in kernel 6.12 is not reported from the kernel to the upper layer.
  2025-09-25  4:26       ` Dmitry Torokhov
@ 2025-09-26 12:03         ` 卢国宏
  2025-09-29 12:10           ` 卢国宏
  0 siblings, 1 reply; 8+ messages in thread
From: 卢国宏 @ 2025-09-26 12:03 UTC (permalink / raw)
  To: Dmitry Torokhov
  Cc: José Expósito, linux-input@vger.kernel.org,
	linux-kernel@vger.kernel.org, jikos@kernel.org,
	bentiss@kernel.org, 李鹏, Fei1 Jiang 蒋飞,
	宋密密, 卢国宏


Hi Dmitry,
After testing, we found that your proposed method can solve our problem. Please help merge this method into the Linux kernel as soon as possible! Please remember to send us the relevant information of the merged git so that we can contact Google and merge their Android GKI as well. Our project is looking forward to using this feature. Thank you very much!

________________________________________
发件人: Dmitry Torokhov <dmitry.torokhov@gmail.com>
发送时间: 2025年9月25日 12:26
收件人: 卢国宏
抄送: José Expósito; linux-input@vger.kernel.org; linux-kernel@vger.kernel.org; jikos@kernel.org; bentiss@kernel.org; 李鹏; Fei1 Jiang 蒋飞; 宋密密
主题: Re: 答复: [External Mail]Re: The zero power level of the HID device in kernel 6.12 is not reported from the kernel to the upper layer.

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

On Mon, Sep 22, 2025 at 09:29:20AM +0000, 卢国宏 wrote:
>
> What kind of action are we talking about? Section 31 of the HID
> specification defines events for "Smart Battery" ("To comply with the
> Smart Battery Specification, the Battery System must support the
> functions defined in the Battery and Charger usage tables. For details,
> see Section 4.2, “Battery System Page (x85).”) and is typically used for
> "battery pack for cellular phones (principal source), the battery
> pack(s) for notebook computers (auxiliary source), and the sealed
> batteries in uninterruptible power supplies (auxiliary source)."
>
> Is your use case main battery or battery in a stylus or some other
> peripheral?
>
>
> --->>>
> What we are discussing is the code implementation of Section 31 of the
> HID protocol: 31 Battery System Page (0x85). Our scenario is: an
> Android phone is connected to a handle via USB. The handle is a HID
> device with a battery. The power of the battery in the handle is sent
> to the bottom layer (kernel) of the phone via USB. The bottom layer of
> the phone then reports this power to the upper layer of Android
> through the HID driver.

I see. I guess we can try only filtering out 0 reports for the
digitizers, leaving other devices with batteries alone. Something like
this:


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;
        }


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!******/#

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

* 答复: 答复: [External Mail]Re: The zero power level of the HID device in kernel 6.12 is not reported from the kernel to the upper layer.
  2025-09-26 12:03         ` 答复: " 卢国宏
@ 2025-09-29 12:10           ` 卢国宏
  2025-10-10  2:25             ` 卢国宏
  0 siblings, 1 reply; 8+ messages in thread
From: 卢国宏 @ 2025-09-29 12:10 UTC (permalink / raw)
  To: Dmitry Torokhov
  Cc: José Expósito, linux-input@vger.kernel.org,
	linux-kernel@vger.kernel.org, jikos@kernel.org,
	bentiss@kernel.org, 李鹏, Fei1 Jiang 蒋飞,
	宋密密, 卢国宏

Hi Dmitry,
Any updates on merging your code for reporting a zero battery level in HID devices? I look forward to hearing from you.
Thank you!
________________________________________
发件人: 卢国宏
发送时间: 2025年9月26日 20:03
收件人: Dmitry Torokhov
抄送: José Expósito; linux-input@vger.kernel.org; linux-kernel@vger.kernel.org; jikos@kernel.org; bentiss@kernel.org; 李鹏; Fei1 Jiang 蒋飞; 宋密密; 卢国宏
主题: 答复: 答复: [External Mail]Re: The zero power level of the HID device in kernel 6.12 is not reported from the kernel to the upper layer.

Hi Dmitry,
After testing, we found that your proposed method can solve our problem. Please help merge this method into the Linux kernel as soon as possible! Please remember to send us the relevant information of the merged git so that we can contact Google and merge their Android GKI as well. Our project is looking forward to using this feature. Thank you very much!

________________________________________
发件人: Dmitry Torokhov <dmitry.torokhov@gmail.com>
发送时间: 2025年9月25日 12:26
收件人: 卢国宏
抄送: José Expósito; linux-input@vger.kernel.org; linux-kernel@vger.kernel.org; jikos@kernel.org; bentiss@kernel.org; 李鹏; Fei1 Jiang 蒋飞; 宋密密
主题: Re: 答复: [External Mail]Re: The zero power level of the HID device in kernel 6.12 is not reported from the kernel to the upper layer.

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

On Mon, Sep 22, 2025 at 09:29:20AM +0000, 卢国宏 wrote:
>
> What kind of action are we talking about? Section 31 of the HID
> specification defines events for "Smart Battery" ("To comply with the
> Smart Battery Specification, the Battery System must support the
> functions defined in the Battery and Charger usage tables. For details,
> see Section 4.2, “Battery System Page (x85).”) and is typically used for
> "battery pack for cellular phones (principal source), the battery
> pack(s) for notebook computers (auxiliary source), and the sealed
> batteries in uninterruptible power supplies (auxiliary source)."
>
> Is your use case main battery or battery in a stylus or some other
> peripheral?
>
>
> --->>>
> What we are discussing is the code implementation of Section 31 of the
> HID protocol: 31 Battery System Page (0x85). Our scenario is: an
> Android phone is connected to a handle via USB. The handle is a HID
> device with a battery. The power of the battery in the handle is sent
> to the bottom layer (kernel) of the phone via USB. The bottom layer of
> the phone then reports this power to the upper layer of Android
> through the HID driver.

I see. I guess we can try only filtering out 0 reports for the
digitizers, leaving other devices with batteries alone. Something like
this:


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;
        }


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!******/#

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

* 答复: 答复: [External Mail]Re: The zero power level of the HID device in kernel 6.12 is not reported from the kernel to the upper layer.
  2025-09-29 12:10           ` 卢国宏
@ 2025-10-10  2:25             ` 卢国宏
  2025-10-10  6:13               ` Dmitry Torokhov
  0 siblings, 1 reply; 8+ messages in thread
From: 卢国宏 @ 2025-10-10  2:25 UTC (permalink / raw)
  To: Dmitry Torokhov
  Cc: José Expósito, linux-input@vger.kernel.org,
	linux-kernel@vger.kernel.org, jikos@kernel.org,
	bentiss@kernel.org, 李鹏, Fei1 Jiang 蒋飞,
	宋密密, 卢国宏

Hi Dmitry,
Please let us know whether you plan to merge this solution into the kernel or not! Thanks!
________________________________________
发件人: 卢国宏
发送时间: 2025年9月29日 20:10:42
收件人: Dmitry Torokhov
抄送: José Expósito; linux-input@vger.kernel.org; linux-kernel@vger.kernel.org; jikos@kernel.org; bentiss@kernel.org; 李鹏; Fei1 Jiang 蒋飞; 宋密密; 卢国宏
主题: 答复: 答复: [External Mail]Re: The zero power level of the HID device in kernel 6.12 is not reported from the kernel to the upper layer.

Hi Dmitry,
Any updates on merging your code for reporting a zero battery level in HID devices? I look forward to hearing from you.
Thank you!
________________________________________
发件人: 卢国宏
发送时间: 2025年9月26日 20:03
收件人: Dmitry Torokhov
抄送: José Expósito; linux-input@vger.kernel.org; linux-kernel@vger.kernel.org; jikos@kernel.org; bentiss@kernel.org; 李鹏; Fei1 Jiang 蒋飞; 宋密密; 卢国宏
主题: 答复: 答复: [External Mail]Re: The zero power level of the HID device in kernel 6.12 is not reported from the kernel to the upper layer.

Hi Dmitry,
After testing, we found that your proposed method can solve our problem. Please help merge this method into the Linux kernel as soon as possible! Please remember to send us the relevant information of the merged git so that we can contact Google and merge their Android GKI as well. Our project is looking forward to using this feature. Thank you very much!

________________________________________
发件人: Dmitry Torokhov <dmitry.torokhov@gmail.com>
发送时间: 2025年9月25日 12:26
收件人: 卢国宏
抄送: José Expósito; linux-input@vger.kernel.org; linux-kernel@vger.kernel.org; jikos@kernel.org; bentiss@kernel.org; 李鹏; Fei1 Jiang 蒋飞; 宋密密
主题: Re: 答复: [External Mail]Re: The zero power level of the HID device in kernel 6.12 is not reported from the kernel to the upper layer.

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

On Mon, Sep 22, 2025 at 09:29:20AM +0000, 卢国宏 wrote:
>
> What kind of action are we talking about? Section 31 of the HID
> specification defines events for "Smart Battery" ("To comply with the
> Smart Battery Specification, the Battery System must support the
> functions defined in the Battery and Charger usage tables. For details,
> see Section 4.2, “Battery System Page (x85).”) and is typically used for
> "battery pack for cellular phones (principal source), the battery
> pack(s) for notebook computers (auxiliary source), and the sealed
> batteries in uninterruptible power supplies (auxiliary source)."
>
> Is your use case main battery or battery in a stylus or some other
> peripheral?
>
>
> --->>>
> What we are discussing is the code implementation of Section 31 of the
> HID protocol: 31 Battery System Page (0x85). Our scenario is: an
> Android phone is connected to a handle via USB. The handle is a HID
> device with a battery. The power of the battery in the handle is sent
> to the bottom layer (kernel) of the phone via USB. The bottom layer of
> the phone then reports this power to the upper layer of Android
> through the HID driver.

I see. I guess we can try only filtering out 0 reports for the
digitizers, leaving other devices with batteries alone. Something like
this:


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;
        }


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!******/#

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

* Re: 答复: 答复: [External Mail]Re: The zero power level of the HID device in kernel 6.12 is not reported from the kernel to the upper layer.
  2025-10-10  2:25             ` 卢国宏
@ 2025-10-10  6:13               ` Dmitry Torokhov
  0 siblings, 0 replies; 8+ messages in thread
From: Dmitry Torokhov @ 2025-10-10  6:13 UTC (permalink / raw)
  To: 卢国宏
  Cc: José Expósito, linux-input@vger.kernel.org,
	linux-kernel@vger.kernel.org, jikos@kernel.org,
	bentiss@kernel.org, 李鹏, Fei1 Jiang 蒋飞,
	宋密密

Hi,

On Fri, Oct 10, 2025 at 02:25:55AM +0000, 卢国宏 wrote:
> Hi Dmitry,
> Please let us know whether you plan to merge this solution into the kernel or not! Thanks!

I just sent the patch to HID maintainers. You were CCed on it.

Thanks.

-- 
Dmitry

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

end of thread, other threads:[~2025-10-10  6:14 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <d2cada7efe8d4436b6e638fa1e0aaefb@xiaomi.com>
2025-09-19  8:40 ` The zero power level of the HID device in kernel 6.12 is not reported from the kernel to the upper layer José Expósito
2025-09-19 15:04   ` Dmitry Torokhov
2025-09-22  9:29     ` 答复: [External Mail]Re: " 卢国宏
2025-09-25  4:26       ` Dmitry Torokhov
2025-09-26 12:03         ` 答复: " 卢国宏
2025-09-29 12:10           ` 卢国宏
2025-10-10  2:25             ` 卢国宏
2025-10-10  6:13               ` Dmitry Torokhov

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).