HID: cp2112: use lock guards
authorBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Wed, 23 Apr 2025 08:55:41 +0000 (10:55 +0200)
committerJiri Kosina <jkosina@suse.com>
Thu, 24 Apr 2025 10:07:55 +0000 (12:07 +0200)
Simplify the code by using the lock guards from linux/cleanup.h
throughout the driver.

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 408c865efdd4554757dc800e36eea1f91761344d..92dd891b7b597d4fb1a53af884972be284d99c0e 100644 (file)
@@ -187,7 +187,7 @@ static int cp2112_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
        u8 *buf = dev->in_out_buffer;
        int ret;
 
-       mutex_lock(&dev->lock);
+       guard(mutex)(&dev->lock);
 
        ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf,
                                 CP2112_GPIO_CONFIG_LENGTH, HID_FEATURE_REPORT,
@@ -196,7 +196,7 @@ static int cp2112_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
                hid_err(hdev, "error requesting GPIO config: %d\n", ret);
                if (ret >= 0)
                        ret = -EIO;
-               goto exit;
+               return ret;
        }
 
        buf[1] &= ~BIT(offset);
@@ -209,14 +209,10 @@ static int cp2112_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
                hid_err(hdev, "error setting GPIO config: %d\n", ret);
                if (ret >= 0)
                        ret = -EIO;
-               goto exit;
+               return ret;
        }
 
-       ret = 0;
-
-exit:
-       mutex_unlock(&dev->lock);
-       return ret;
+       return 0;
 }
 
 static void cp2112_gpio_set_unlocked(struct cp2112_device *dev,
@@ -244,7 +240,7 @@ static void cp2112_gpio_set(struct gpio_chip *chip, unsigned int offset,
 
        guard(mutex)(&dev->lock);
 
-       return cp2112_gpio_set_unlocked(dev, offset, value);
+       cp2112_gpio_set_unlocked(dev, offset, value);
 }
 
 static int cp2112_gpio_get_all(struct gpio_chip *chip)
@@ -254,23 +250,17 @@ static int cp2112_gpio_get_all(struct gpio_chip *chip)
        u8 *buf = dev->in_out_buffer;
        int ret;
 
-       mutex_lock(&dev->lock);
+       guard(mutex)(&dev->lock);
 
        ret = hid_hw_raw_request(hdev, CP2112_GPIO_GET, buf,
                                 CP2112_GPIO_GET_LENGTH, HID_FEATURE_REPORT,
                                 HID_REQ_GET_REPORT);
        if (ret != CP2112_GPIO_GET_LENGTH) {
                hid_err(hdev, "error requesting GPIO values: %d\n", ret);
-               ret = ret < 0 ? ret : -EIO;
-               goto exit;
+               return ret < 0 ? ret : -EIO;
        }
 
-       ret = buf[1];
-
-exit:
-       mutex_unlock(&dev->lock);
-
-       return ret;
+       return buf[1];
 }
 
 static int cp2112_gpio_get(struct gpio_chip *chip, unsigned int offset)
@@ -292,14 +282,14 @@ static int cp2112_gpio_direction_output(struct gpio_chip *chip,
        u8 *buf = dev->in_out_buffer;
        int ret;
 
-       mutex_lock(&dev->lock);
+       guard(mutex)(&dev->lock);
 
        ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf,
                                 CP2112_GPIO_CONFIG_LENGTH, HID_FEATURE_REPORT,
                                 HID_REQ_GET_REPORT);
        if (ret != CP2112_GPIO_CONFIG_LENGTH) {
                hid_err(hdev, "error requesting GPIO config: %d\n", ret);
-               goto fail;
+               return ret < 0 ? ret : -EIO;
        }
 
        buf[1] |= 1 << offset;
@@ -310,7 +300,7 @@ static int cp2112_gpio_direction_output(struct gpio_chip *chip,
                                 HID_REQ_SET_REPORT);
        if (ret < 0) {
                hid_err(hdev, "error setting GPIO config: %d\n", ret);
-               goto fail;
+               return ret;
        }
 
        /*
@@ -319,13 +309,7 @@ static int cp2112_gpio_direction_output(struct gpio_chip *chip,
         */
        cp2112_gpio_set_unlocked(dev, offset, value);
 
-       mutex_unlock(&dev->lock);
-
        return 0;
-
-fail:
-       mutex_unlock(&dev->lock);
-       return ret < 0 ? ret : -EIO;
 }
 
 static int cp2112_hid_get(struct hid_device *hdev, unsigned char report_number,