Skip to contentSkip to conversation

HID: Consolidate serializing ope/close in transport drivers

PATCH Applied Dmitry Torokhov 6 participants 15 messages Jun 7, 2017 – Jun 8, 2017

Subsystems (5)
lore
Share & tools
lore
#4 lore raw

PATCH v2 3/8 HID: usbhid: do not rely on hid->open when deciding to do IO

In reply to Dmitry Torokhov

Instead of checking hid->open (that we plan on having HID core manage) in hid_start_in(), let's allocate a couple of new flags: HID_IN_POLLING and HID_OPENED, and use them to decide whether we should be submitting URBs or not.

---
 drivers/hid/usbhid/hid-core.c | 25 ++++++++++++++++++-------
 drivers/hid/usbhid/usbhid.h   | 11 +++++++++++
 2 files changed, 29 insertions(+), 7 deletions(-)
drivers/hid/usbhid/hid-core.c +18 -7
index 62b660622265..d927fe4ba592 100644
@@ -85,10 +85,10 @@ static int hid_start_in(struct hid_device *hid)
 	struct usbhid_device *usbhid = hid->driver_data;
 
 	spin_lock_irqsave(&usbhid->lock, flags);
-	if ((hid->open > 0 || hid->quirks & HID_QUIRK_ALWAYS_POLL) &&
-			!test_bit(HID_DISCONNECTED, &usbhid->iofl) &&
-			!test_bit(HID_SUSPENDED, &usbhid->iofl) &&
-			!test_and_set_bit(HID_IN_RUNNING, &usbhid->iofl)) {
+	if (test_bit(HID_IN_POLLING, &usbhid->iofl) &&
+	    !test_bit(HID_DISCONNECTED, &usbhid->iofl) &&
+	    !test_bit(HID_SUSPENDED, &usbhid->iofl) &&
+	    !test_and_set_bit(HID_IN_RUNNING, &usbhid->iofl)) {
 		rc = usb_submit_urb(usbhid->urbin, GFP_ATOMIC);
 		if (rc != 0) {
 			clear_bit(HID_IN_RUNNING, &usbhid->iofl);
@@ -272,13 +272,13 @@ static int usbhid_restart_ctrl_queue(struct usbhid_device *usbhid)
 static void hid_irq_in(struct urb *urb)
 {
 	struct hid_device	*hid = urb->context;
-	struct usbhid_device 	*usbhid = hid->driver_data;
+	struct usbhid_device	*usbhid = hid->driver_data;
 	int			status;
 
 	switch (urb->status) {
 	case 0:			/* success */
 		usbhid->retry_delay = 0;
-		if ((hid->quirks & HID_QUIRK_ALWAYS_POLL) && !hid->open)
+		if (!test_bit(HID_OPENED, &usbhid->iofl))
 			break;
 		usbhid_mark_busy(usbhid);
 		if (!test_bit(HID_RESUME_RUNNING, &usbhid->iofl)) {
@@ -692,6 +692,8 @@ static int usbhid_open(struct hid_device *hid)
 			goto done;
 		}
 		usbhid->intf->needs_remote_wakeup = 1;
+		set_bit(HID_OPENED, &usbhid->iofl);
+		set_bit(HID_IN_POLLING, &usbhid->iofl);
 		set_bit(HID_RESUME_RUNNING, &usbhid->iofl);
 		res = hid_start_in(hid);
 		if (res) {
@@ -701,6 +703,9 @@ static int usbhid_open(struct hid_device *hid)
 			} else {
 				/* no use opening if resources are insufficient */
 				hid->open--;
+				clear_bit(HID_OPENED, &usbhid->iofl);
+				if (!(hid->quirks & HID_QUIRK_ALWAYS_POLL))
+					clear_bit(HID_IN_POLLING, &usbhid->iofl);
 				res = -EBUSY;
 				usbhid->intf->needs_remote_wakeup = 0;
 			}
@@ -734,6 +739,9 @@ static void usbhid_close(struct hid_device *hid)
 	 */
 	spin_lock_irq(&usbhid->lock);
 	if (!--hid->open) {
+		if (!(hid->quirks & HID_QUIRK_ALWAYS_POLL))
+			clear_bit(HID_IN_POLLING, &usbhid->iofl);
+		clear_bit(HID_OPENED, &usbhid->iofl);
 		spin_unlock_irq(&usbhid->lock);
 		hid_cancel_delayed_stuff(usbhid);
 		if (!(hid->quirks & HID_QUIRK_ALWAYS_POLL)) {
@@ -1135,6 +1143,7 @@ static int usbhid_start(struct hid_device *hid)
 		ret = usb_autopm_get_interface(usbhid->intf);
 		if (ret)
 			goto fail;
+		set_bit(HID_IN_POLLING, &usbhid->iofl);
 		usbhid->intf->needs_remote_wakeup = 1;
 		ret = hid_start_in(hid);
 		if (ret) {
@@ -1176,8 +1185,10 @@ static void usbhid_stop(struct hid_device *hid)
 	if (WARN_ON(!usbhid))
 		return;
 
-	if (hid->quirks & HID_QUIRK_ALWAYS_POLL)
+	if (hid->quirks & HID_QUIRK_ALWAYS_POLL) {
+		clear_bit(HID_IN_POLLING, &usbhid->iofl);
 		usbhid->intf->needs_remote_wakeup = 0;
+	}
 
 	clear_bit(HID_STARTED, &usbhid->iofl);
 	spin_lock_irq(&usbhid->lock);	/* Sync with error and led handlers */
drivers/hid/usbhid/usbhid.h +11 -0
index ffcd329b3c3b..da9c61d54be6 100644
@@ -49,6 +49,17 @@ struct usb_interface *usbhid_find_interface(int minor);
 #define HID_KEYS_PRESSED	10
 #define HID_NO_BANDWIDTH	11
 #define HID_RESUME_RUNNING	12
+/*
+ * The device is opened, meaning there is a client that is interested
+ * in data coming from the device.
+ */
+#define HID_OPENED		13
+/*
+ * We are polling input endpoint by [re]submitting IN URB, because
+ * either HID device is opened or ALWAYS POLL quirk is set for the
+ * device.
+ */
+#define HID_IN_POLLING		14
 
 /*
  * USB-specific HID struct, to be pointed to
Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
#5 lore raw

PATCH v2 4/8 HID: serialize hid_hw_open and hid_hw_close

In reply to Dmitry Torokhov

The HID transport drivers either re-implement exactly the same logic (usbhid, i2c-hid) or forget to implement it (usbhid) which causes issues when the same device is accessed via multiple interfaces (for example input device through evdev and also hidraw). Let's muve the locking logic into HID core to make sure the serialized behavior is always enforced.

Also let's uninline and move hid_hw_start() and hid_hw_stop() into hid-core as hid_hw_start() is somewhat large and do not believe we get any benefit from these two being inline.

---
 drivers/hid/hid-core.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/hid.h    | 72 +++++-----------------------------------
 2 files changed, 98 insertions(+), 63 deletions(-)
drivers/hid/hid-core.c +89 -0
index ca173801d2be..f2a2cd522e46 100644
@@ -1750,6 +1750,94 @@ void hid_disconnect(struct hid_device *hdev)
 }
 EXPORT_SYMBOL_GPL(hid_disconnect);
 
+/**
+ * hid_hw_start - start underlying HW
+ * @hdev: hid device
+ * @connect_mask: which outputs to connect, see HID_CONNECT_*
+ *
+ * Call this in probe function *after* hid_parse. This will setup HW
+ * buffers and start the device (if not defeirred to device open).
+ * hid_hw_stop must be called if this was successful.
+ */
+int hid_hw_start(struct hid_device *hdev, unsigned int connect_mask)
+{
+	int error;
+
+	error = hdev->ll_driver->start(hdev);
+	if (error)
+		return error;
+
+	if (connect_mask) {
+		error = hid_connect(hdev, connect_mask);
+		if (error) {
+			hdev->ll_driver->stop(hdev);
+			return error;
+		}
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(hid_hw_start);
+
+/**
+ * hid_hw_stop - stop underlying HW
+ * @hdev: hid device
+ *
+ * This is usually called from remove function or from probe when something
+ * failed and hid_hw_start was called already.
+ */
+void hid_hw_stop(struct hid_device *hdev)
+{
+	hid_disconnect(hdev);
+	hdev->ll_driver->stop(hdev);
+}
+EXPORT_SYMBOL_GPL(hid_hw_stop);
+
+/**
+ * hid_hw_open - signal underlying HW to start delivering events
+ * @hdev: hid device
+ *
+ * Tell underlying HW to start delivering events from the device.
+ * This function should be called sometime after successful call
+ * to hid_hiw_start().
+ */
+int hid_hw_open(struct hid_device *hdev)
+{
+	int ret;
+
+	ret = mutex_lock_killable(&hdev->ll_open_lock);
+	if (ret)
+		return ret;
+
+	if (!hdev->ll_open_count++) {
+		ret = hdev->ll_driver->open(hdev);
+		if (ret)
+			hdev->ll_open_count--;
+	}
+
+	mutex_unlock(&hdev->ll_open_lock);
+	return ret;
+}
+EXPORT_SYMBOL_GPL(hid_hw_open);
+
+/**
+ * hid_hw_close - signal underlaying HW to stop delivering events
+ *
+ * @hdev: hid device
+ *
+ * This function indicates that we are not interested in the events
+ * from this device anymore. Delivery of events may or may not stop,
+ * depending on the number of users still outstanding.
+ */
+void hid_hw_close(struct hid_device *hdev)
+{
+	mutex_lock(&hdev->ll_open_lock);
+	if (!--hdev->ll_open_count)
+		hdev->ll_driver->close(hdev);
+	mutex_unlock(&hdev->ll_open_lock);
+}
+EXPORT_SYMBOL_GPL(hid_hw_close);
+
 /*
  * A list of devices for which there is a specialized driver on HID bus.
  *
@@ -2748,6 +2836,7 @@ struct hid_device *hid_allocate_device(void)
 	spin_lock_init(&hdev->debug_list_lock);
 	sema_init(&hdev->driver_lock, 1);
 	sema_init(&hdev->driver_input_lock, 1);
+	mutex_init(&hdev->ll_open_lock);
 
 	return hdev;
 }
include/linux/hid.h +9 -63

91 lines not on this page · raw message

Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
#6 lore raw

PATCH v2 5/8 HID: i2c-hid: remove custom locking from i2c_hid_open/close

In reply to Dmitry Torokhov

Now that HID core enforces serialization of transport driver open/close calls we can remove custom locking from i2c-hid driver.

---
 drivers/hid/i2c-hid/i2c-hid.c | 32 +++++++++-----------------------
 1 file changed, 9 insertions(+), 23 deletions(-)
drivers/hid/i2c-hid/i2c-hid.c +9 -23
index fb55fb4c39fc..6355015ed249 100644
@@ -743,18 +743,12 @@ static int i2c_hid_open(struct hid_device *hid)
 	struct i2c_hid *ihid = i2c_get_clientdata(client);
 	int ret = 0;
 
-	mutex_lock(&i2c_hid_open_mut);
-	if (!hid->open++) {
-		ret = pm_runtime_get_sync(&client->dev);
-		if (ret < 0) {
-			hid->open--;
-			goto done;
-		}
-		set_bit(I2C_HID_STARTED, &ihid->flags);
-	}
-done:
-	mutex_unlock(&i2c_hid_open_mut);
-	return ret < 0 ? ret : 0;
+	ret = pm_runtime_get_sync(&client->dev);
+	if (ret < 0)
+		return ret;
+
+	set_bit(I2C_HID_STARTED, &ihid->flags);
+	return 0;
 }
 
 static void i2c_hid_close(struct hid_device *hid)
@@ -762,18 +756,10 @@ static void i2c_hid_close(struct hid_device *hid)
 	struct i2c_client *client = hid->driver_data;
 	struct i2c_hid *ihid = i2c_get_clientdata(client);
 
-	/* protecting hid->open to make sure we don't restart
-	 * data acquistion due to a resumption we no longer
-	 * care about
-	 */
-	mutex_lock(&i2c_hid_open_mut);
-	if (!--hid->open) {
-		clear_bit(I2C_HID_STARTED, &ihid->flags);
+	clear_bit(I2C_HID_STARTED, &ihid->flags);
 
-		/* Save some power */
-		pm_runtime_put(&client->dev);
-	}
-	mutex_unlock(&i2c_hid_open_mut);
+	/* Save some power */
+	pm_runtime_put(&client->dev);
 }
 
 static int i2c_hid_power(struct hid_device *hid, int lvl)
Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
#11 lore raw

PATCH v2 8/8 HID: remove no longer used hid->open field

In reply to Dmitry Torokhov

Now that all users have migrated to use hid->ll_open_count, we can remove hid->open field.

---
 include/linux/hid.h | 1 -
 1 file changed, 1 deletion(-)
include/linux/hid.h +0 -1
index 5501eb64dbc4..72e8ac667771 100644
@@ -548,7 +548,6 @@ struct hid_device {							/* device report descriptor */
 	void *hiddev;							/* The hiddev structure */
 	void *hidraw;
 
-	int open;							/* is the device open by anyone? */
 	char name[128];							/* Device name */
 	char phys[64];							/* Device physical location */
 	char uniq[64];							/* Device unique identifier (serial #) */
Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
#0 lore raw

v2 cover HID: Consolidate serializing ope/close in transport drivers

This originally came about as report of uhid sending duplicate open and premature close when hidraw was used alongside of input. After looking at the drivers I think we should consolidate user tracking inside of the HID core. While implementing this, there were a few cleanups as well.

V2:

- added greybus hid changes
- added error handling in hiddev around hid_hw_open which is __much_check

Dmitry Torokhov (8):
HID: hiddev: use hid_hw_open/close instead of usbhid_open/close
HID: hiddev: use hid_hw_power instead of usbhid_get/put_power
HID: usbhid: do not rely on hid->open when deciding to do IO
HID: serialize hid_hw_open and hid_hw_close
HID: i2c-hid: remove custom locking from i2c_hid_open/close
HID: usbhid: remove custom locking from usbhid_open/close
greybus: hid: remove custom locking from gb_hid_open/close
HID: remove no longer used hid->open field

 drivers/hid/hid-core.c        |  89 +++++++++++++++++++++++++
 drivers/hid/i2c-hid/i2c-hid.c |  32 +++------
 drivers/hid/usbhid/hid-core.c | 150 ++++++++++++++++++++----------------------
 drivers/hid/usbhid/hiddev.c   |  24 +++----
 drivers/hid/usbhid/usbhid.h   |  15 +++--
 drivers/staging/greybus/hid.c |  43 ++++--------
 include/linux/hid.h           |  73 +++-----------------
 7 files changed, 214 insertions(+), 212 deletions(-)

Thanks.

Signature · 1 line
Dmitry
#1 lore raw

PATCH v2 1/8 HID: hiddev: use hid_hw_open/close instead of usbhid_open/close

Instead of calling into usbhid code directly, let's use the standard accessors for the transport HID drivers, and stop clobbering their errors with -EIO.

This also allows us make usbhid_open and close static.

---
 drivers/hid/usbhid/hid-core.c |  4 ++--
 drivers/hid/usbhid/hiddev.c   | 16 +++++++++-------
 drivers/hid/usbhid/usbhid.h   |  2 --
 3 files changed, 11 insertions(+), 11 deletions(-)
drivers/hid/usbhid/hid-core.c +2 -2
index 83772fa7d92a..fb0cf5d70504 100644
@@ -677,7 +677,7 @@ static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
 	return result;
 }
 
-int usbhid_open(struct hid_device *hid)
+static int usbhid_open(struct hid_device *hid)
 {
 	struct usbhid_device *usbhid = hid->driver_data;
 	int res = 0;
@@ -722,7 +722,7 @@ int usbhid_open(struct hid_device *hid)
 	return res;
 }
 
-void usbhid_close(struct hid_device *hid)
+static void usbhid_close(struct hid_device *hid)
 {
 	struct usbhid_device *usbhid = hid->driver_data;
 
drivers/hid/usbhid/hiddev.c +9 -7
index 0e06368d1fbb..b4f714752245 100644
@@ -237,7 +237,7 @@ static int hiddev_release(struct inode * inode, struct file * file)
 	mutex_lock(&list->hiddev->existancelock);
 	if (!--list->hiddev->open) {
 		if (list->hiddev->exist) {
-			usbhid_close(list->hiddev->hid);
+			hid_hw_close(list->hiddev->hid);
 			usbhid_put_power(list->hiddev->hid);
 		} else {
 			mutex_unlock(&list->hiddev->existancelock);
@@ -282,11 +282,9 @@ static int hiddev_open(struct inode *inode, struct file *file)
 	 */
 	if (list->hiddev->exist) {
 		if (!list->hiddev->open++) {
-			res = usbhid_open(hiddev->hid);
-			if (res < 0) {
-				res = -EIO;
+			res = hid_hw_open(hiddev->hid);
+			if (res < 0)
 				goto bail;
-			}
 		}
 	} else {
 		res = -ENODEV;
@@ -306,10 +304,14 @@ static int hiddev_open(struct inode *inode, struct file *file)
 				res = -EIO;
 				goto bail_unlock;
 			}
-			usbhid_open(hid);
+			res = hid_hw_open(hid);
+			if (res < 0)
+				goto bail_put_power;
 		}
 	mutex_unlock(&hiddev->existancelock);
 	return 0;
+bail_put_power:
+	usbhid_put_power(hid);
 bail_unlock:
 	mutex_unlock(&hiddev->existancelock);
 bail:
@@ -935,7 +937,7 @@ void hiddev_disconnect(struct hid_device *hid)
 
 	if (hiddev->open) {
 		mutex_unlock(&hiddev->existancelock);
-		usbhid_close(hiddev->hid);
+		hid_hw_close(hiddev->hid);
 		wake_up_interruptible(&hiddev->wait);
 	} else {
 		mutex_unlock(&hiddev->existancelock);
drivers/hid/usbhid/usbhid.h +0 -2
index fa47d666cfcf..83ef5c14aa92 100644
@@ -34,8 +34,6 @@
 #include <linux/input.h>
 
 /*  API provided by hid-core.c for USB HID drivers */
-void usbhid_close(struct hid_device *hid);
-int usbhid_open(struct hid_device *hid);
 void usbhid_init_reports(struct hid_device *hid);
 int usbhid_get_power(struct hid_device *hid);
 void usbhid_put_power(struct hid_device *hid);
Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
#2 lore raw

PATCH v2 2/8 HID: hiddev: use hid_hw_power instead of usbhid_get/put_power

In reply to Dmitry Torokhov

Instead of calling into usbhid code directly, let's use the standard accessors for the transport HID drivers, and stop clobbering their error codes with -EIO.

This also allows us to remove usbhid_get/put_power(), leaving only usbhid_power().

---
 drivers/hid/usbhid/hid-core.c | 22 +++++-----------------
 drivers/hid/usbhid/hiddev.c   | 14 ++++++--------
 drivers/hid/usbhid/usbhid.h   |  2 --
 3 files changed, 11 insertions(+), 27 deletions(-)
drivers/hid/usbhid/hid-core.c +5 -17
index fb0cf5d70504..62b660622265 100644
@@ -1203,16 +1203,19 @@ static void usbhid_stop(struct hid_device *hid)
 
 static int usbhid_power(struct hid_device *hid, int lvl)
 {
+	struct usbhid_device *usbhid = hid->driver_data;
 	int r = 0;
 
 	switch (lvl) {
 	case PM_HINT_FULLON:
-		r = usbhid_get_power(hid);
+		r = usb_autopm_get_interface(usbhid->intf);
 		break;
+
 	case PM_HINT_NORMAL:
-		usbhid_put_power(hid);
+		usb_autopm_put_interface(usbhid->intf);
 		break;
 	}
+
 	return r;
 }
 
@@ -1492,21 +1495,6 @@ static int hid_post_reset(struct usb_interface *intf)
 	return 0;
 }
 
-int usbhid_get_power(struct hid_device *hid)
-{
-	struct usbhid_device *usbhid = hid->driver_data;
-
-	return usb_autopm_get_interface(usbhid->intf);
-}
-
-void usbhid_put_power(struct hid_device *hid)
-{
-	struct usbhid_device *usbhid = hid->driver_data;
-
-	usb_autopm_put_interface(usbhid->intf);
-}
-
-
 #ifdef CONFIG_PM
 static int hid_resume_common(struct hid_device *hid, bool driver_suspended)
 {
drivers/hid/usbhid/hiddev.c +6 -8
index b4f714752245..7d749b19c27c 100644
@@ -238,7 +238,7 @@ static int hiddev_release(struct inode * inode, struct file * file)
 	if (!--list->hiddev->open) {
 		if (list->hiddev->exist) {
 			hid_hw_close(list->hiddev->hid);
-			usbhid_put_power(list->hiddev->hid);
+			hid_hw_power(list->hiddev->hid, PM_HINT_NORMAL);
 		} else {
 			mutex_unlock(&list->hiddev->existancelock);
 			kfree(list->hiddev);
@@ -299,19 +299,17 @@ static int hiddev_open(struct inode *inode, struct file *file)
 	if (!list->hiddev->open++)
 		if (list->hiddev->exist) {
 			struct hid_device *hid = hiddev->hid;
-			res = usbhid_get_power(hid);
-			if (res < 0) {
-				res = -EIO;
+			res = hid_hw_power(hid, PM_HINT_FULLON);
+			if (res < 0)
 				goto bail_unlock;
-			}
 			res = hid_hw_open(hid);
 			if (res < 0)
-				goto bail_put_power;
+				goto bail_normal_power;
 		}
 	mutex_unlock(&hiddev->existancelock);
 	return 0;
-bail_put_power:
-	usbhid_put_power(hid);
+bail_normal_power:
+	hid_hw_power(hid, PM_HINT_NORMAL);
 bail_unlock:
 	mutex_unlock(&hiddev->existancelock);
 bail:
drivers/hid/usbhid/usbhid.h +0 -2
index 83ef5c14aa92..ffcd329b3c3b 100644
@@ -35,8 +35,6 @@
 
 /*  API provided by hid-core.c for USB HID drivers */
 void usbhid_init_reports(struct hid_device *hid);
-int usbhid_get_power(struct hid_device *hid);
-void usbhid_put_power(struct hid_device *hid);
 struct usb_interface *usbhid_find_interface(int minor);
 
 /* iofl flags */
Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
#8 lore raw

PATCH v2 7/8 greybus: hid: remove custom locking from gb_hid_open/close

In reply to Dmitry Torokhov

Now that HID core enforces serialization of transport driver open/close calls we can remove custom locking from greybus hid driver.

---
 drivers/staging/greybus/hid.c | 43 ++++++++++++++-----------------------------
 1 file changed, 14 insertions(+), 29 deletions(-)
drivers/staging/greybus/hid.c +14 -29
index 730d746fc4c2..465101bbab69 100644
@@ -32,8 +32,6 @@ struct gb_hid {
 	char				*inbuf;
 };
 
-static DEFINE_MUTEX(gb_hid_open_mutex);
-
 /* Routines to get controller's information over greybus */
 
 /* Operations performed on greybus */
@@ -346,19 +344,14 @@ static void gb_hid_stop(struct hid_device *hid)
 static int gb_hid_open(struct hid_device *hid)
 {
 	struct gb_hid *ghid = hid->driver_data;
-	int ret = 0;
-
-	mutex_lock(&gb_hid_open_mutex);
-	if (!hid->open++) {
-		ret = gb_hid_set_power(ghid, GB_HID_TYPE_PWR_ON);
-		if (ret < 0)
-			hid->open--;
-		else
-			set_bit(GB_HID_STARTED, &ghid->flags);
-	}
-	mutex_unlock(&gb_hid_open_mutex);
+	int ret;
 
-	return ret;
+	ret = gb_hid_set_power(ghid, GB_HID_TYPE_PWR_ON);
+	if (ret < 0)
+		return ret;
+
+	set_bit(GB_HID_STARTED, &ghid->flags);
+	return 0;
 }
 
 static void gb_hid_close(struct hid_device *hid)
@@ -366,21 +359,13 @@ static void gb_hid_close(struct hid_device *hid)
 	struct gb_hid *ghid = hid->driver_data;
 	int ret;
 
-	/*
-	 * Protecting hid->open to make sure we don't restart data acquistion
-	 * due to a resumption we no longer care about..
-	 */
-	mutex_lock(&gb_hid_open_mutex);
-	if (!--hid->open) {
-		clear_bit(GB_HID_STARTED, &ghid->flags);
-
-		/* Save some power */
-		ret = gb_hid_set_power(ghid, GB_HID_TYPE_PWR_OFF);
-		if (ret)
-			dev_err(&ghid->connection->bundle->dev,
-				"failed to power off (%d)\n", ret);
-	}
-	mutex_unlock(&gb_hid_open_mutex);
+	clear_bit(GB_HID_STARTED, &ghid->flags);
+
+	/* Save some power */
+	ret = gb_hid_set_power(ghid, GB_HID_TYPE_PWR_OFF);
+	if (ret)
+		dev_err(&ghid->connection->bundle->dev,
+			"failed to power off (%d)\n", ret);
 }
 
 static int gb_hid_power(struct hid_device *hid, int lvl)
Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
#7 lore raw

PATCH v2 6/8 HID: usbhid: remove custom locking from usbhid_open/close

In reply to Dmitry Torokhov

Now that HID core enforces serialization of transport driver open/close calls we can remove custom locking from usbhid driver.

---
 drivers/hid/usbhid/hid-core.c | 115 +++++++++++++++++++-----------------------
 1 file changed, 53 insertions(+), 62 deletions(-)
drivers/hid/usbhid/hid-core.c +53 -62
index d927fe4ba592..76013eb5cb7f 100644
@@ -70,8 +70,6 @@ MODULE_PARM_DESC(quirks, "Add/modify USB HID quirks by specifying "
 /*
  * Input submission and I/O error handler.
  */
-static DEFINE_MUTEX(hid_open_mut);
-
 static void hid_io_error(struct hid_device *hid);
 static int hid_submit_out(struct hid_device *hid);
 static int hid_submit_ctrl(struct hid_device *hid);
@@ -680,50 +678,48 @@ static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
 static int usbhid_open(struct hid_device *hid)
 {
 	struct usbhid_device *usbhid = hid->driver_data;
-	int res = 0;
-
-	mutex_lock(&hid_open_mut);
-	if (!hid->open++) {
-		res = usb_autopm_get_interface(usbhid->intf);
-		/* the device must be awake to reliably request remote wakeup */
-		if (res < 0) {
-			hid->open--;
-			res = -EIO;
-			goto done;
-		}
-		usbhid->intf->needs_remote_wakeup = 1;
-		set_bit(HID_OPENED, &usbhid->iofl);
-		set_bit(HID_IN_POLLING, &usbhid->iofl);
-		set_bit(HID_RESUME_RUNNING, &usbhid->iofl);
-		res = hid_start_in(hid);
-		if (res) {
-			if (res != -ENOSPC) {
-				hid_io_error(hid);
-				res = 0;
-			} else {
-				/* no use opening if resources are insufficient */
-				hid->open--;
-				clear_bit(HID_OPENED, &usbhid->iofl);
-				if (!(hid->quirks & HID_QUIRK_ALWAYS_POLL))
-					clear_bit(HID_IN_POLLING, &usbhid->iofl);
-				res = -EBUSY;
-				usbhid->intf->needs_remote_wakeup = 0;
-			}
-		}
-		usb_autopm_put_interface(usbhid->intf);
+	int res;
 
-		/*
-		 * In case events are generated while nobody was listening,
-		 * some are released when the device is re-opened.
-		 * Wait 50 msec for the queue to empty before allowing events
-		 * to go through hid.
-		 */
-		if (res == 0 && !(hid->quirks & HID_QUIRK_ALWAYS_POLL))
-			msleep(50);
-		clear_bit(HID_RESUME_RUNNING, &usbhid->iofl);
+	if (hid->quirks & HID_QUIRK_ALWAYS_POLL)
+		return 0;
+
+	res = usb_autopm_get_interface(usbhid->intf);
+	/* the device must be awake to reliably request remote wakeup */
+	if (res < 0)
+		return -EIO;
+
+	usbhid->intf->needs_remote_wakeup = 1;
+
+	set_bit(HID_RESUME_RUNNING, &usbhid->iofl);
+	set_bit(HID_OPENED, &usbhid->iofl);
+	set_bit(HID_IN_POLLING, &usbhid->iofl);
+
+	res = hid_start_in(hid);
+	if (res) {
+		if (res != -ENOSPC) {
+			hid_io_error(hid);
+			res = 0;
+		} else {
+			/* no use opening if resources are insufficient */
+			res = -EBUSY;
+			clear_bit(HID_OPENED, &usbhid->iofl);
+			clear_bit(HID_IN_POLLING, &usbhid->iofl);
+			usbhid->intf->needs_remote_wakeup = 0;
+		}
 	}
-done:
-	mutex_unlock(&hid_open_mut);
+
+	usb_autopm_put_interface(usbhid->intf);
+
+	/*
+	 * In case events are generated while nobody was listening,
+	 * some are released when the device is re-opened.
+	 * Wait 50 msec for the queue to empty before allowing events
+	 * to go through hid.
+	 */
+	if (res == 0)
+		msleep(50);
+
+	clear_bit(HID_RESUME_RUNNING, &usbhid->iofl);
 	return res;
 }
 
@@ -731,27 +727,22 @@ static void usbhid_close(struct hid_device *hid)
 {
 	struct usbhid_device *usbhid = hid->driver_data;
 
-	mutex_lock(&hid_open_mut);
+	if (hid->quirks & HID_QUIRK_ALWAYS_POLL)
+		return;
 
-	/* protecting hid->open to make sure we don't restart
-	 * data acquistion due to a resumption we no longer
-	 * care about
+	/*
+	 * Make sure we don't restart data acquisition due to
+	 * a resumption we no longer care about by avoiding racing
+	 * with hid_start_in().
 	 */
 	spin_lock_irq(&usbhid->lock);
-	if (!--hid->open) {
-		if (!(hid->quirks & HID_QUIRK_ALWAYS_POLL))
-			clear_bit(HID_IN_POLLING, &usbhid->iofl);
-		clear_bit(HID_OPENED, &usbhid->iofl);
-		spin_unlock_irq(&usbhid->lock);
-		hid_cancel_delayed_stuff(usbhid);
-		if (!(hid->quirks & HID_QUIRK_ALWAYS_POLL)) {
-			usb_kill_urb(usbhid->urbin);
-			usbhid->intf->needs_remote_wakeup = 0;
-		}
-	} else {
-		spin_unlock_irq(&usbhid->lock);
-	}
-	mutex_unlock(&hid_open_mut);
+	clear_bit(HID_IN_POLLING, &usbhid->iofl);
+	clear_bit(HID_OPENED, &usbhid->iofl);
+	spin_unlock_irq(&usbhid->lock);
+
+	hid_cancel_delayed_stuff(usbhid);
+	usb_kill_urb(usbhid->urbin);
+	usbhid->intf->needs_remote_wakeup = 0;
 }
 
 /*
Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
maintainer Acked-by on v2 7/8
#9 lore raw

Re: [PATCH v2 7/8] greybus: hid: remove custom locking from gb_hid_open/close

In reply to Dmitry Torokhov
Quoted 7 lines · Dmitry Torokhov

Now that HID core enforces serialization of transport driver open/close calls we can remove custom locking from greybus hid driver.

---
 drivers/staging/greybus/hid.c | 43 ++++++++++++++-----------------------------
 1 file changed, 14 insertions(+), 29 deletions(-)
#3 lore raw

Re: [PATCH v2 2/8] HID: hiddev: use hid_hw_power instead of usbhid_get/put_power

In reply to Dmitry Torokhov
Quoted 8 lines · Dmitry Torokhov

Instead of calling into usbhid code directly, let's use the standard accessors for the transport HID drivers, and stop clobbering their error codes with -EIO.

This also allows us to remove usbhid_get/put_power(), leaving only usbhid_power().

                break;

drivers/hid/usbhid/hid-core.c static void usbhid_stop(struct hid_device *hid) · line 1219

Quoted 6 lines
drivers/hid/usbhid/hid-core.c +2 -0
+
        case PM_HINT_NORMAL:
 
        }
+
        return r;

I think you can drop above changes.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
Reviewed-by on the v2 series
#12 lore raw
In reply to Dmitry Torokhov
Quoted 9 lines · Dmitry Torokhov

This originally came about as report of uhid sending duplicate open and premature close when hidraw was used alongside of input. After looking at the drivers I think we should consolidate user tracking inside of the HID core. While implementing this, there were a few cleanups as well.

V2:

- added greybus hid changes
- added error handling in hiddev around hid_hw_open which is __much_check

FWIW,

Quoted 25 lines

Dmitry Torokhov (8):
HID: hiddev: use hid_hw_open/close instead of usbhid_open/close
HID: hiddev: use hid_hw_power instead of usbhid_get/put_power
HID: usbhid: do not rely on hid->open when deciding to do IO
HID: serialize hid_hw_open and hid_hw_close
HID: i2c-hid: remove custom locking from i2c_hid_open/close
HID: usbhid: remove custom locking from usbhid_open/close
greybus: hid: remove custom locking from gb_hid_open/close
HID: remove no longer used hid->open field

 drivers/hid/hid-core.c        |  89 +++++++++++++++++++++++++
 drivers/hid/i2c-hid/i2c-hid.c |  32 +++------
 drivers/hid/usbhid/hid-core.c | 150 ++++++++++++++++++++----------------------
 drivers/hid/usbhid/hiddev.c   |  24 +++----
 drivers/hid/usbhid/usbhid.h   |  15 +++--
 drivers/staging/greybus/hid.c |  43 ++++--------
 include/linux/hid.h           |  73 +++-----------------
 7 files changed, 214 insertions(+), 212 deletions(-)

Thanks.

--
Dmitry

Signature · 2 lines
With Best Regards,
Andy Shevchenko
Acked-by on v2 7/8
#10 lore raw

Re: [PATCH v2 7/8] greybus: hid: remove custom locking from gb_hid_open/close

In reply to Dmitry Torokhov
Quoted 7 lines · Dmitry Torokhov

Now that HID core enforces serialization of transport driver open/close calls we can remove custom locking from greybus hid driver.

---
 drivers/staging/greybus/hid.c | 43 ++++++++++++++-----------------------------
 1 file changed, 14 insertions(+), 29 deletions(-)
Reviewed-by on the v2 series
#13 lore raw
In reply to Dmitry Torokhov
Quoted 10 lines · Dmitry Torokhov

This originally came about as report of uhid sending duplicate open and premature close when hidraw was used alongside of input. After looking at the drivers I think we should consolidate user tracking inside of the HID core. While implementing this, there were a few cleanups as well.

V2:

- added greybus hid changes
- added error handling in hiddev around hid_hw_open which is __much_check

Looks good to me:

Quoted 24 lines

Dmitry Torokhov (8):
HID: hiddev: use hid_hw_open/close instead of usbhid_open/close
HID: hiddev: use hid_hw_power instead of usbhid_get/put_power
HID: usbhid: do not rely on hid->open when deciding to do IO
HID: serialize hid_hw_open and hid_hw_close
HID: i2c-hid: remove custom locking from i2c_hid_open/close
HID: usbhid: remove custom locking from usbhid_open/close
greybus: hid: remove custom locking from gb_hid_open/close
HID: remove no longer used hid->open field

 drivers/hid/hid-core.c        |  89 +++++++++++++++++++++++++
 drivers/hid/i2c-hid/i2c-hid.c |  32 +++------
 drivers/hid/usbhid/hid-core.c | 150 ++++++++++++++++++++----------------------
 drivers/hid/usbhid/hiddev.c   |  24 +++----
 drivers/hid/usbhid/usbhid.h   |  15 +++--
 drivers/staging/greybus/hid.c |  43 ++++--------
 include/linux/hid.h           |  73 +++-----------------
 7 files changed, 214 insertions(+), 212 deletions(-)

Thanks.

Signature · 1 line
Dmitry
maintainer Applied
#14 lore raw
In reply to Dmitry Torokhov
Quoted 9 lines · Dmitry Torokhov

This originally came about as report of uhid sending duplicate open and premature close when hidraw was used alongside of input. After looking at the drivers I think we should consolidate user tracking inside of the HID core. While implementing this, there were a few cleanups as well.

V2:

- added greybus hid changes
- added error handling in hiddev around hid_hw_open which is __much_check

I've applied the series, thanks Dmitry.

Signature · 2 lines
Jiri Kosina
SUSE Labs