diff mbox series

[06/10] HID: surface-hid: Add support for hot-removal

Message ID 20220520183422.7185-7-luzmaximilian@gmail.com (mailing list archive)
State Superseded
Headers show
Series platform/surface: aggregator: Add support for client hot-removal | expand

Commit Message

Maximilian Luz May 20, 2022, 6:34 p.m. UTC
Add support for hot-removal of SSAM HID client devices.

Once a device has been hot-removed, further communication with it should
be avoided as it may fail and time out. While the device will be removed
as soon as we detect hot-removal, communication may still occur during
teardown, especially when unregistering notifiers.

While hot-removal is a surprise event that can happen at any time, try
to avoid communication as much as possible once it has been detected to
prevent timeouts that can slow down device removal and cause issues,
e.g. when quickly re-attaching the device.

Signed-off-by: Maximilian Luz <luzmaximilian@gmail.com>
 drivers/hid/surface-hid/surface_hid_core.c | 38 +++++++++++++++++++++-
 1 file changed, 37 insertions(+), 1 deletion(-)


kernel test robot May 26, 2022, 11:55 p.m. UTC | #1
Hi Maximilian,

I love your patch! Yet something to improve:

[auto build test ERROR on sre-power-supply/for-next]
[also build test ERROR on hid/for-next linus/master v5.18 next-20220526]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in

url:    https://github.com/intel-lab-lkp/linux/commits/Maximilian-Luz/platform-surface-aggregator-Add-support-for-client-hot-removal/20220521-024312
base:   https://git.kernel.org/pub/scm/linux/kernel/git/sre/linux-power-supply.git for-next
config: i386-randconfig-r014-20220516 (https://download.01.org/0day-ci/archive/20220527/202205270727.ZHBcTahI-lkp@intel.com/config)
compiler: gcc-11 (Debian 11.3.0-1) 11.3.0
reproduce (this is a W=1 build):
        # https://github.com/intel-lab-lkp/linux/commit/314a7da4f5af820a0475695017585a83226b05b5
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Maximilian-Luz/platform-surface-aggregator-Add-support-for-client-hot-removal/20220521-024312
        git checkout 314a7da4f5af820a0475695017585a83226b05b5
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        make W=1 O=build_dir ARCH=i386 SHELL=/bin/bash

If you fix the issue, kindly add following tag where applicable
Reported-by: kernel test robot <lkp@intel.com>

All errors (new ones prefixed by >>):

   ld: drivers/hid/surface-hid/surface_hid_core.o: in function `surface_hid_is_hot_removed.isra.0':
>> surface_hid_core.c:(.text+0x1e9): undefined reference to `ssam_device_type'
diff mbox series


diff --git a/drivers/hid/surface-hid/surface_hid_core.c b/drivers/hid/surface-hid/surface_hid_core.c
index e46330b2e561..87637f813de2 100644
--- a/drivers/hid/surface-hid/surface_hid_core.c
+++ b/drivers/hid/surface-hid/surface_hid_core.c
@@ -19,12 +19,30 @@ 
 #include "surface_hid_core.h"
+/* -- Utility functions. ---------------------------------------------------- */
+static bool surface_hid_is_hot_removed(struct surface_hid_device *shid)
+	/*
+	 * Non-ssam client devices, i.e. platform client devices, cannot be
+	 * hot-removed.
+	 */
+	if (!is_ssam_device(shid->dev))
+		return false;
+	return ssam_device_is_hot_removed(to_ssam_device(shid->dev));
 /* -- Device descriptor access. --------------------------------------------- */
 static int surface_hid_load_hid_descriptor(struct surface_hid_device *shid)
 	int status;
+	if (surface_hid_is_hot_removed(shid))
+		return -ENODEV;
 	status = shid->ops.get_descriptor(shid, SURFACE_HID_DESC_HID,
 			(u8 *)&shid->hid_desc, sizeof(shid->hid_desc));
 	if (status)
@@ -61,6 +79,9 @@  static int surface_hid_load_device_attributes(struct surface_hid_device *shid)
 	int status;
+	if (surface_hid_is_hot_removed(shid))
+		return -ENODEV;
 	status = shid->ops.get_descriptor(shid, SURFACE_HID_DESC_ATTRS,
 			(u8 *)&shid->attrs, sizeof(shid->attrs));
 	if (status)
@@ -88,9 +109,18 @@  static int surface_hid_start(struct hid_device *hid)
 static void surface_hid_stop(struct hid_device *hid)
 	struct surface_hid_device *shid = hid->driver_data;
+	bool hot_removed;
+	/*
+	 * Communication may fail for devices that have been hot-removed. This
+	 * also includes unregistration of HID events, so we need to check this
+	 * here. Only if the device has not been marked as hot-removed, we can
+	 * safely disable events.
+	 */
+	hot_removed = surface_hid_is_hot_removed(shid);
 	/* Note: This call will log errors for us, so ignore them here. */
-	ssam_notifier_unregister(shid->ctrl, &shid->notif);
+	__ssam_notifier_unregister(shid->ctrl, &shid->notif, !hot_removed);
 static int surface_hid_open(struct hid_device *hid)
@@ -109,6 +139,9 @@  static int surface_hid_parse(struct hid_device *hid)
 	u8 *buf;
 	int status;
+	if (surface_hid_is_hot_removed(shid))
+		return -ENODEV;
 	buf = kzalloc(len, GFP_KERNEL);
 	if (!buf)
 		return -ENOMEM;
@@ -126,6 +159,9 @@  static int surface_hid_raw_request(struct hid_device *hid, unsigned char reportn
 	struct surface_hid_device *shid = hid->driver_data;
+	if (surface_hid_is_hot_removed(shid))
+		return -ENODEV;
 	if (rtype == HID_OUTPUT_REPORT && reqtype == HID_REQ_SET_REPORT)
 		return shid->ops.output_report(shid, reportnum, buf, len);