Skip to contentSkip to conversation

Add functions for HID drivers to react on first open and last close call

PATCH Applied Werner Sembach 3 participants 7 messages Feb 11, 2025 – May 13, 2025 HID

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 Add functions for HID drivers to react on first open and last close call

Adds a new function to the hid_driver struct that is called when the userspace starts using the device, and another one that is called when userspace stop using the device. With this a hid driver can implement special suspend handling for devices currently not in use.

---
 drivers/hid/hid-core.c | 9 ++++++++-
 include/linux/hid.h    | 4 ++++
 2 files changed, 12 insertions(+), 1 deletion(-)
drivers/hid/hid-core.c +8 -1
index 4497b50799dbf..283917a79ac86 100644
@@ -2392,6 +2392,9 @@ int hid_hw_open(struct hid_device *hdev)
 		ret = hdev->ll_driver->open(hdev);
 		if (ret)
 			hdev->ll_open_count--;
+
+		if (hdev->driver->on_hid_hw_open)
+			hdev->driver->on_hid_hw_open(hdev);
 	}
 
 	mutex_unlock(&hdev->ll_open_lock);
@@ -2411,8 +2414,12 @@ EXPORT_SYMBOL_GPL(hid_hw_open);
 void hid_hw_close(struct hid_device *hdev)
 {
 	mutex_lock(&hdev->ll_open_lock);
-	if (!--hdev->ll_open_count)
+	if (!--hdev->ll_open_count) {
 		hdev->ll_driver->close(hdev);
+
+		if (hdev->driver->on_hid_hw_close)
+			hdev->driver->on_hid_hw_close(hdev);
+	}
 	mutex_unlock(&hdev->ll_open_lock);
 }
 EXPORT_SYMBOL_GPL(hid_hw_close);
include/linux/hid.h +4 -0
index cdc0dc13c87fe..3b7db4eb3f2f6 100644
@@ -790,6 +790,8 @@ struct hid_usage_id {
  * @suspend: invoked on suspend (NULL means nop)
  * @resume: invoked on resume if device was not reset (NULL means nop)
  * @reset_resume: invoked on resume if device was reset (NULL means nop)
+ * @on_hid_hw_open: invoked when hid core opens first instance (NULL means nop)
+ * @on_hid_hw_close: invoked when hid core closes last instance (NULL means nop)
  *
  * probe should return -errno on error, or 0 on success. During probe,
  * input will not be passed to raw_event unless hid_device_io_start is
@@ -845,6 +847,8 @@ struct hid_driver {
 	int (*suspend)(struct hid_device *hdev, pm_message_t message);
 	int (*resume)(struct hid_device *hdev);
 	int (*reset_resume)(struct hid_device *hdev);
+	void (*on_hid_hw_open)(struct hid_device *hdev);
+	void (*on_hid_hw_close)(struct hid_device *hdev);
 
 /* private: */
 	struct device_driver driver;
Signature · 1 line
2.43.0
#1 lore raw

PATCH 2/2 Disable touchpad on firmware level while not in use

Using the new on_hid_hw_open and on_hid_hw_close functions to disable the touchpad on firmware level while not being in use.

This safes some battery and triggers touchpad-disabled-leds hardwired to the touchpads firmware, that exist for example on some TongFang barebones.

For a lengthy discussion with all the details see
https://gitlab.freedesktop.org/libinput/libinput/-/issues/558

---
 drivers/hid/hid-multitouch.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
drivers/hid/hid-multitouch.c +12 -0
index 82900857bfd87..7289c04f47f17 100644
@@ -1884,6 +1884,16 @@ static void mt_remove(struct hid_device *hdev)
 	hid_hw_stop(hdev);
 }
 
+static void mt_on_hid_hw_open(struct hid_device *hdev)
+{
+	mt_set_modes(hdev, HID_LATENCY_NORMAL, TOUCHPAD_REPORT_ALL);
+}
+
+static void mt_on_hid_hw_close(struct hid_device *hdev)
+{
+	mt_set_modes(hdev, HID_LATENCY_HIGH, TOUCHPAD_REPORT_NONE);
+}
+
 /*
  * This list contains only:
  * - VID/PID of products not working with the default multitouch handling
@@ -2351,5 +2361,7 @@ static struct hid_driver mt_driver = {
 	.suspend = pm_ptr(mt_suspend),
 	.reset_resume = pm_ptr(mt_reset_resume),
 	.resume = pm_ptr(mt_resume),
+	.on_hid_hw_open = mt_on_hid_hw_open,
+	.on_hid_hw_close = mt_on_hid_hw_close,
 };
 module_hid_driver(mt_driver);
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH 2/2] Disable touchpad on firmware level while not in use

Hi,

Am 11.02.25 um 14:39 schrieb Werner Sembach:

Quoted 8 lines

Using the new on_hid_hw_open and on_hid_hw_close functions to disable the touchpad on firmware level while not being in use.

This safes some battery and triggers touchpad-disabled-leds hardwired to the touchpads firmware, that exist for example on some TongFang barebones.

For a lengthy discussion with all the details see
https://gitlab.freedesktop.org/libinput/libinput/-/issues/558

Any comments if this is a good idea or not?

Best regards,

Werner

Quoted 34 lines
---
  drivers/hid/hid-multitouch.c | 12 ++++++++++++
  1 file changed, 12 insertions(+)
drivers/hid/hid-multitouch.c +12 -0
index 82900857bfd87..7289c04f47f17 100644
@@ -1884,6 +1884,16 @@ static void mt_remove(struct hid_device *hdev)
  	hid_hw_stop(hdev);
  }
 
+static void mt_on_hid_hw_open(struct hid_device *hdev)
+{
+	mt_set_modes(hdev, HID_LATENCY_NORMAL, TOUCHPAD_REPORT_ALL);
+}
+
+static void mt_on_hid_hw_close(struct hid_device *hdev)
+{
+	mt_set_modes(hdev, HID_LATENCY_HIGH, TOUCHPAD_REPORT_NONE);
+}
+
  /*
   * This list contains only:
   * - VID/PID of products not working with the default multitouch handling
@@ -2351,5 +2361,7 @@ static struct hid_driver mt_driver = {
  	.suspend = pm_ptr(mt_suspend),
  	.reset_resume = pm_ptr(mt_reset_resume),
  	.resume = pm_ptr(mt_resume),
+	.on_hid_hw_open = mt_on_hid_hw_open,
+	.on_hid_hw_close = mt_on_hid_hw_close,
  };
  module_hid_driver(mt_driver);
maintainer
#3 lore raw

Re: [PATCH 2/2] Disable touchpad on firmware level while not in use

Quoted 10 lines · Werner Sembach
Quoted 8 lines

Using the new on_hid_hw_open and on_hid_hw_close functions to disable the touchpad on firmware level while not being in use.

This safes some battery and triggers touchpad-disabled-leds hardwired to the touchpads firmware, that exist for example on some TongFang barebones.

For a lengthy discussion with all the details see
https://gitlab.freedesktop.org/libinput/libinput/-/issues/558

Any comments if this is a good idea or not?

Sorry for the delay.

To me this looks like generally a good idea; Benjamin, any comments on the hid-mt part?

BTW you'd need to adjust the Subject line of the patches to conform to the subsystem style (i.e. HID: core: ....)

Thanks,

Signature · 2 lines
Jiri Kosina
SUSE Labs
#4 lore raw

Re: [PATCH 2/2] Disable touchpad on firmware level while not in use

Am 24.04.25 um 11:20 schrieb Jiri Kosina:

Quoted 5 lines · Werner Sembach
Quoted 4 lines
Quoted 4 lines

Using the new on_hid_hw_open and on_hid_hw_close functions to disable the touchpad on firmware level while not being in use.

Quoted 7 lines
Quoted 6 lines
Quoted 5 lines

This safes some battery and triggers touchpad-disabled-leds hardwired to the touchpads firmware, that exist for example on some TongFang barebones.

For a lengthy discussion with all the details see
https://gitlab.freedesktop.org/libinput/libinput/-/issues/558

Any comments if this is a good idea or not?

Sorry for the delay.

np

Quoted 6 lines

To me this looks like generally a good idea; Benjamin, any comments on the hid-mt part?

BTW you'd need to adjust the Subject line of the patches to conform to the subsystem style (i.e. HID: core: ....)

sorry an oversight when rewriting the commit log from local testing branch to upstream ready

will do in v2 after Benjamins reply

Thanks for looking at this,

Werner

Quoted 3 lines

Thanks,

#5 lore raw

Re: [PATCH 2/2] Disable touchpad on firmware level while not in use

In reply to Jiri Kosina
Quoted 11 lines · Jiri Kosina
Quoted 9 lines · Werner Sembach
Quoted 8 lines

Using the new on_hid_hw_open and on_hid_hw_close functions to disable the touchpad on firmware level while not being in use.

This safes some battery and triggers touchpad-disabled-leds hardwired to the touchpads firmware, that exist for example on some TongFang barebones.

For a lengthy discussion with all the details see
https://gitlab.freedesktop.org/libinput/libinput/-/issues/558

Quoted 6 lines
Quoted 1 line

Any comments if this is a good idea or not?

Sorry for the delay.

To me this looks like generally a good idea; Benjamin, any comments on the hid-mt part?

Looks good to me, though my CI is now taking way more time because it's not expecting to receive data from the device once the initial setup is done.

Quoted 3 lines

BTW you'd need to adjust the Subject line of the patches to conform to the subsystem style (i.e. HID: core: ....)

Amended locally and pushed (see the notification email).

Cheers,
Benjamin

Quoted 7 lines

Thanks,

Signature · 2 lines
Jiri Kosina
SUSE Labs
maintainer Applied
#6 lore raw
In reply to Werner Sembach
Quoted 6 lines · Werner Sembach

Adds a new function to the hid_driver struct that is called when the userspace starts using the device, and another one that is called when userspace stop using the device. With this a hid driver can implement special suspend handling for devices currently not in use.

Applied to hid/hid.git (for-6.16/core), thanks!

[1/2] Add functions for HID drivers to react on first open and last close call

      https://git.kernel.org/hid/hid/c/a98e892c6942
[2/2] Disable touchpad on firmware level while not in use
      https://git.kernel.org/hid/hid/c/6a9e76f75c1a

Cheers,

Signature · 1 line
Benjamin Tissoires <[email protected]>