HID: core: Add functions for HID drivers to react on first open and last close call
authorWerner Sembach <wse@tuxedocomputers.com>
Tue, 11 Feb 2025 13:39:05 +0000 (14:39 +0100)
committerBenjamin Tissoires <bentiss@kernel.org>
Tue, 13 May 2025 13:59:03 +0000 (15:59 +0200)
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.

Signed-off-by: Werner Sembach <wse@tuxedocomputers.com>
Link: https://patch.msgid.link/20250211133950.422232-1-wse@tuxedocomputers.com
Signed-off-by: Benjamin Tissoires <bentiss@kernel.org>
drivers/hid/hid-core.c
include/linux/hid.h

index 4741ff6267710b35c6fe1a78ca5e90f725b232a7..b348d0464314ca331da073128f0ec4e0a6a91ed1 100644 (file)
@@ -2396,6 +2396,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);
@@ -2415,8 +2418,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);
index a1305210b2fd839154b5b835db9fc08307da3ec3..568a9d8c749bc5547ff78d5abe6db7bce2f62d2b 100644 (file)
@@ -795,6 +795,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
@@ -850,6 +852,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;