Skip to contentSkip to conversation

HID: input: map battery system charging

PATCH RESEND In review José Expósito 2 participants 3 messages Nov 24, 2022 – Dec 20, 2022 HID

lore
Share & tools
lore
#0 lore raw

cover HID: input: map battery system charging

Hi everyone,

This patch adds support for reporting battery status (charging/discharging) for devices with a charging usage in their HID descriptor:

	0x05, 0x85,         /*      Usage Page (Battery System),    */
	0x09, 0x44,         /*      Usage Page (Charging),          */

I tested it on Apple hardware (mouse, trackpad and keyboard) as well as on UCLogic tablets with battery [1].

On other hardware (HID_DC_BATTERYSTRENGTH or digitizers with battery) discharging should be reported for backwards compatibility. I did my best to keep the old behaviour by adding KUnit tests; However, I don't own any of those devices, so I'd appreciate any help testing the patch from someone with the actual hardware.

Best wishes,
José Expósito

[1] https://lore.kernel.org/linux-input/[email protected]/

José Expósito (1):
HID: input: map battery system charging

 drivers/hid/.kunitconfig     |  1 +
 drivers/hid/Kconfig          |  1 +
 drivers/hid/hid-input-test.c | 80 ++++++++++++++++++++++++++++++++++++
 drivers/hid/hid-input.c      | 36 +++++++++++++++-
 include/linux/hid.h          |  2 +
 5 files changed, 118 insertions(+), 2 deletions(-)
 create mode 100644 drivers/hid/hid-input-test.c
Signature · 1 line
2.38.1
#1 lore raw

PATCH RESEND 1/1 HID: input: map battery system charging

HID descriptors with Battery System (0x85) Charging (0x44) usage are ignored and POWER_SUPPLY_STATUS_DISCHARGING is always reported to user space, even when the device is charging.

Map this usage and when it is reported set the right charging status.

In addition, add KUnit tests to make sure that the charging status is correctly set and reported. They can be run with the usual command:

    $ ./tools/testing/kunit/kunit.py run --kunitconfig=drivers/hid
---
 drivers/hid/.kunitconfig     |  1 +
 drivers/hid/Kconfig          |  1 +
 drivers/hid/hid-input-test.c | 80 ++++++++++++++++++++++++++++++++++++
 drivers/hid/hid-input.c      | 36 +++++++++++++++-
 include/linux/hid.h          |  2 +
 5 files changed, 118 insertions(+), 2 deletions(-)
 create mode 100644 drivers/hid/hid-input-test.c
drivers/hid/.kunitconfig +1 -0
index 04daeff5c970..675a8209c7ae 100644
@@ -1,5 +1,6 @@
 CONFIG_KUNIT=y
 CONFIG_USB=y
 CONFIG_USB_HID=y
+CONFIG_HID_BATTERY_STRENGTH=y
 CONFIG_HID_UCLOGIC=y
 CONFIG_HID_KUNIT_TEST=y
drivers/hid/Kconfig +1 -0
index 185a077d59cd..644b4913d4d8 100644
@@ -1263,6 +1263,7 @@ config HID_MCP2221
 config HID_KUNIT_TEST
 	tristate "KUnit tests for HID" if !KUNIT_ALL_TESTS
 	depends on KUNIT=y
+	depends on HID_BATTERY_STRENGTH
 	depends on HID_UCLOGIC
 	default KUNIT_ALL_TESTS
 	help
drivers/hid/hid-input-test.c new file +80 -0

80 lines not on this page · raw message

drivers/hid/hid-input.c +34 -2
index 859aeb07542e..587112e9efe2 100644
@@ -477,7 +477,7 @@ static int hidinput_get_battery_property(struct power_supply *psy,
 		if (dev->battery_status == HID_BATTERY_UNKNOWN)
 			val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
 		else
-			val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
+			val->intval = dev->battery_charge_status;
 		break;
 
 	case POWER_SUPPLY_PROP_SCOPE:
@@ -545,6 +545,7 @@ static int hidinput_setup_battery(struct hid_device *dev, unsigned report_type,
 	dev->battery_max = max;
 	dev->battery_report_type = report_type;
 	dev->battery_report_id = field->report->id;
+	dev->battery_charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
 
 	/*
 	 * Stylus is normally not connected to the device and thus we
@@ -608,6 +609,20 @@ static void hidinput_update_battery(struct hid_device *dev, int value)
 		power_supply_changed(dev->battery);
 	}
 }
+
+static bool hidinput_set_battery_charge_status(struct hid_device *dev,
+					       unsigned int usage, int value)
+{
+	switch (usage) {
+	case HID_BAT_CHARGING:
+		dev->battery_charge_status = value ?
+					     POWER_SUPPLY_STATUS_CHARGING :
+					     POWER_SUPPLY_STATUS_DISCHARGING;
+		return true;
+	}
+
+	return false;
+}
 #else  /* !CONFIG_HID_BATTERY_STRENGTH */
 static int hidinput_setup_battery(struct hid_device *dev, unsigned report_type,
 				  struct hid_field *field, bool is_percentage)
@@ -622,6 +637,12 @@ static void hidinput_cleanup_battery(struct hid_device *dev)
 static void hidinput_update_battery(struct hid_device *dev, int value)
 {
 }
+
+static bool hidinput_set_battery_charge_status(struct hid_device *dev,
+					       unsigned int usage, int value)
+{
+	return false;
+}
 #endif	/* CONFIG_HID_BATTERY_STRENGTH */
 
 static bool hidinput_field_in_collection(struct hid_device *device, struct hid_field *field,
@@ -1208,6 +1229,9 @@ static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_fiel
 			hidinput_setup_battery(device, HID_INPUT_REPORT, field, true);
 			usage->type = EV_PWR;
 			return;
+		case HID_BAT_CHARGING:
+			usage->type = EV_PWR;
+			return;
 		}
 		goto unknown;
 
@@ -1450,7 +1474,11 @@ void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct
 		return;
 
 	if (usage->type == EV_PWR) {
-		hidinput_update_battery(hid, value);
+		bool handled = hidinput_set_battery_charge_status(hid, usage->hid, value);
+
+		if (!handled)
+			hidinput_update_battery(hid, value);
+
 		return;
 	}
 
@@ -2306,3 +2334,7 @@ void hidinput_disconnect(struct hid_device *hid)
 	cancel_work_sync(&hid->led_work);
 }
 EXPORT_SYMBOL_GPL(hidinput_disconnect);
+
+#ifdef CONFIG_HID_KUNIT_TEST
+#include "hid-input-test.c"
+#endif
include/linux/hid.h +2 -0
index 8677ae38599e..1eb5408599cd 100644
@@ -312,6 +312,7 @@ struct hid_item {
 #define HID_DG_LATENCYMODE	0x000d0060
 
 #define HID_BAT_ABSOLUTESTATEOFCHARGE	0x00850065
+#define HID_BAT_CHARGING		0x00850044
 
 #define HID_VD_ASUS_CUSTOM_MEDIA_KEYS	0xff310076
 
@@ -611,6 +612,7 @@ struct hid_device {							/* device report descriptor */
 	__s32 battery_max;
 	__s32 battery_report_type;
 	__s32 battery_report_id;
+	__s32 battery_charge_status;
 	enum hid_battery_status battery_status;
 	bool battery_avoid_query;
 	ktime_t battery_ratelimit_time;
Signature · 1 line
2.38.1
maintainer
#2 lore raw
In reply to José Expósito
Quoted 8 lines · José Expósito

Hi everyone,

This patch adds support for reporting battery status (charging/discharging) for devices with a charging usage in their HID descriptor:

	0x05, 0x85,         /*      Usage Page (Battery System),    */
	0x09, 0x44,         /*      Usage Page (Charging),          */
Quoted 8 lines

I tested it on Apple hardware (mouse, trackpad and keyboard) as well as on UCLogic tablets with battery [1].

On other hardware (HID_DC_BATTERYSTRENGTH or digitizers with battery) discharging should be reported for backwards compatibility. I did my best to keep the old behaviour by adding KUnit tests; However, I don't own any of those devices, so I'd appreciate any help testing the patch from someone with the actual hardware.

I have applied this to hid.git#for-6.3/hid-core so that we get early testing coverage in linux-next as soon as 6.2-rc1 is out.

Thanks,

Signature · 2 lines
Jiri Kosina
SUSE Labs