]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
Input: usbtouchscreen - use guard notation when acquiring mutexes
authorDmitry Torokhov <dmitry.torokhov@gmail.com>
Fri, 12 Jul 2024 05:18:49 +0000 (22:18 -0700)
committerDmitry Torokhov <dmitry.torokhov@gmail.com>
Tue, 16 Jul 2024 01:07:39 +0000 (18:07 -0700)
This makes the code more compact and error handling more robust.

Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Link: https://lore.kernel.org/r/20240712051851.3463657-7-dmitry.torokhov@gmail.com
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
drivers/input/touchscreen/usbtouchscreen.c

index ecde2eaf1f72a232585fcbf59be418304aea13c1..0015f0a6de012285a66169736e212a42b1d12458 100644 (file)
@@ -1349,6 +1349,20 @@ exit:
                        __func__, retval);
 }
 
+static int usbtouch_start_io(struct usbtouch_usb *usbtouch)
+{
+       guard(mutex)(&usbtouch->pm_mutex);
+
+       if (!usbtouch->type->irq_always)
+               if (usb_submit_urb(usbtouch->irq, GFP_KERNEL))
+                       return -EIO;
+
+       usbtouch->interface->needs_remote_wakeup = 1;
+       usbtouch->is_open = true;
+
+       return 0;
+}
+
 static int usbtouch_open(struct input_dev *input)
 {
        struct usbtouch_usb *usbtouch = input_get_drvdata(input);
@@ -1357,23 +1371,12 @@ static int usbtouch_open(struct input_dev *input)
        usbtouch->irq->dev = interface_to_usbdev(usbtouch->interface);
 
        r = usb_autopm_get_interface(usbtouch->interface) ? -EIO : 0;
-       if (r < 0)
-               goto out;
-
-       mutex_lock(&usbtouch->pm_mutex);
-       if (!usbtouch->type->irq_always) {
-               if (usb_submit_urb(usbtouch->irq, GFP_KERNEL)) {
-                       r = -EIO;
-                       goto out_put;
-               }
-       }
+       if (r)
+               return r;
+
+       r = usbtouch_start_io(usbtouch);
 
-       usbtouch->interface->needs_remote_wakeup = 1;
-       usbtouch->is_open = true;
-out_put:
-       mutex_unlock(&usbtouch->pm_mutex);
        usb_autopm_put_interface(usbtouch->interface);
-out:
        return r;
 }
 
@@ -1382,11 +1385,11 @@ static void usbtouch_close(struct input_dev *input)
        struct usbtouch_usb *usbtouch = input_get_drvdata(input);
        int r;
 
-       mutex_lock(&usbtouch->pm_mutex);
-       if (!usbtouch->type->irq_always)
-               usb_kill_urb(usbtouch->irq);
-       usbtouch->is_open = false;
-       mutex_unlock(&usbtouch->pm_mutex);
+       scoped_guard(mutex, &usbtouch->pm_mutex) {
+               if (!usbtouch->type->irq_always)
+                       usb_kill_urb(usbtouch->irq);
+               usbtouch->is_open = false;
+       }
 
        r = usb_autopm_get_interface(usbtouch->interface);
        usbtouch->interface->needs_remote_wakeup = 0;
@@ -1394,8 +1397,7 @@ static void usbtouch_close(struct input_dev *input)
                usb_autopm_put_interface(usbtouch->interface);
 }
 
-static int usbtouch_suspend
-(struct usb_interface *intf, pm_message_t message)
+static int usbtouch_suspend(struct usb_interface *intf, pm_message_t message)
 {
        struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
 
@@ -1407,20 +1409,19 @@ static int usbtouch_suspend
 static int usbtouch_resume(struct usb_interface *intf)
 {
        struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
-       int result = 0;
 
-       mutex_lock(&usbtouch->pm_mutex);
+       guard(mutex)(&usbtouch->pm_mutex);
+
        if (usbtouch->is_open || usbtouch->type->irq_always)
-               result = usb_submit_urb(usbtouch->irq, GFP_NOIO);
-       mutex_unlock(&usbtouch->pm_mutex);
+               return usb_submit_urb(usbtouch->irq, GFP_NOIO);
 
-       return result;
+       return 0;
 }
 
 static int usbtouch_reset_resume(struct usb_interface *intf)
 {
        struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
-       int err = 0;
+       int err;
 
        /* reinit the device */
        if (usbtouch->type->init) {
@@ -1434,12 +1435,12 @@ static int usbtouch_reset_resume(struct usb_interface *intf)
        }
 
        /* restart IO if needed */
-       mutex_lock(&usbtouch->pm_mutex);
+       guard(mutex)(&usbtouch->pm_mutex);
+
        if (usbtouch->is_open)
-               err = usb_submit_urb(usbtouch->irq, GFP_NOIO);
-       mutex_unlock(&usbtouch->pm_mutex);
+               return usb_submit_urb(usbtouch->irq, GFP_NOIO);
 
-       return err;
+       return 0;
 }
 
 static void usbtouch_free_buffers(struct usb_device *udev,