]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
HID: cp2112: hold the lock for the entire direction_output() call
authorBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Wed, 23 Apr 2025 08:55:40 +0000 (10:55 +0200)
committerJiri Kosina <jkosina@suse.com>
Thu, 24 Apr 2025 10:07:55 +0000 (12:07 +0200)
We currently take the lock, set direction to output, release it,
reacquire it and set the desired value. That doesn't look correct.
Introduce a helper function that sets the value without taking the lock
and use it where applicable in order to combine the critical sections.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
Signed-off-by: Jiri Kosina <jkosina@suse.com>
drivers/hid/hid-cp2112.c

index a001b9acd2d4a28a980205edf8efecdb1433936d..408c865efdd4554757dc800e36eea1f91761344d 100644 (file)
@@ -17,6 +17,7 @@
  */
 
 #include <linux/bitops.h>
+#include <linux/cleanup.h>
 #include <linux/gpio/driver.h>
 #include <linux/hid.h>
 #include <linux/hidraw.h>
@@ -218,15 +219,13 @@ exit:
        return ret;
 }
 
-static void cp2112_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
+static void cp2112_gpio_set_unlocked(struct cp2112_device *dev,
+                                    unsigned int offset, int value)
 {
-       struct cp2112_device *dev = gpiochip_get_data(chip);
        struct hid_device *hdev = dev->hdev;
        u8 *buf = dev->in_out_buffer;
        int ret;
 
-       mutex_lock(&dev->lock);
-
        buf[0] = CP2112_GPIO_SET;
        buf[1] = value ? CP2112_GPIO_ALL_GPIO_MASK : 0;
        buf[2] = BIT(offset);
@@ -236,8 +235,16 @@ static void cp2112_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
                                 HID_REQ_SET_REPORT);
        if (ret < 0)
                hid_err(hdev, "error setting GPIO values: %d\n", ret);
+}
 
-       mutex_unlock(&dev->lock);
+static void cp2112_gpio_set(struct gpio_chip *chip, unsigned int offset,
+                           int value)
+{
+       struct cp2112_device *dev = gpiochip_get_data(chip);
+
+       guard(mutex)(&dev->lock);
+
+       return cp2112_gpio_set_unlocked(dev, offset, value);
 }
 
 static int cp2112_gpio_get_all(struct gpio_chip *chip)
@@ -306,13 +313,13 @@ static int cp2112_gpio_direction_output(struct gpio_chip *chip,
                goto fail;
        }
 
-       mutex_unlock(&dev->lock);
-
        /*
         * Set gpio value when output direction is already set,
         * as specified in AN495, Rev. 0.2, cpt. 4.4
         */
-       cp2112_gpio_set(chip, offset, value);
+       cp2112_gpio_set_unlocked(dev, offset, value);
+
+       mutex_unlock(&dev->lock);
 
        return 0;