HID: input: report battery status changes immediately

[ Upstream commit e94536e1d1818b0989aa19b443b7089f50133c35 ]

Previously, the battery status (charging/discharging) was not reported
immediately to user-space. 

For most input devices, this wasn't problematic because changing their
battery status requires connecting them to a different bus.
For example, a gamepad would report a discharging status while
connected via Bluetooth and a charging status while connected via USB.

However, certain devices are not connected or disconnected when their
battery status changes. For example, a phone battery changes its status
without connecting or disconnecting it.
In these cases, the battery status was not reported immediately to user
space.

Report battery status changes immediately to user space to support
these kinds of devices.

Fixes: a608dc1c06 ("HID: input: map battery system charging")
Reported-by: 卢国宏 <luguohong@xiaomi.com>
Closes: https://lore.kernel.org/linux-input/aI49Im0sGb6fpgc8@fedora/T/
Tested-by: 卢国宏 <luguohong@xiaomi.com>
Signed-off-by: José Expósito <jose.exposito89@gmail.com>
Signed-off-by: Jiri Kosina <jkosina@suse.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
This commit is contained in:
José Expósito 2025-08-14 12:39:40 +02:00 committed by Greg Kroah-Hartman
parent ab718b489f
commit 7bc9134607

View File

@ -623,13 +623,19 @@ static bool hidinput_update_battery_charge_status(struct hid_device *dev,
return false;
}
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 (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;
@ -656,13 +662,8 @@ static void hidinput_cleanup_battery(struct hid_device *dev)
{
}
static bool hidinput_update_battery_charge_status(struct hid_device *dev,
unsigned int usage, int value)
{
return false;
}
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)
{
}
#endif /* CONFIG_HID_BATTERY_STRENGTH */
@ -1509,11 +1510,7 @@ void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct
return;
if (usage->type == EV_PWR) {
bool handled = hidinput_update_battery_charge_status(hid, usage->hid, value);
if (!handled)
hidinput_update_battery(hid, value);
hidinput_update_battery(hid, usage->hid, value);
return;
}