Skip to content

Commit 837b05f

Browse files
Bartosz GolaszewskiJiri Kosina
authored andcommitted
HID: cp2112: hold the lock for the entire direction_output() call
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>
1 parent a99548b commit 837b05f

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

drivers/hid/hid-cp2112.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818

1919
#include <linux/bitops.h>
20+
#include <linux/cleanup.h>
2021
#include <linux/gpio/driver.h>
2122
#include <linux/hid.h>
2223
#include <linux/hidraw.h>
@@ -218,15 +219,13 @@ static int cp2112_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
218219
return ret;
219220
}
220221

221-
static void cp2112_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
222+
static void cp2112_gpio_set_unlocked(struct cp2112_device *dev,
223+
unsigned int offset, int value)
222224
{
223-
struct cp2112_device *dev = gpiochip_get_data(chip);
224225
struct hid_device *hdev = dev->hdev;
225226
u8 *buf = dev->in_out_buffer;
226227
int ret;
227228

228-
mutex_lock(&dev->lock);
229-
230229
buf[0] = CP2112_GPIO_SET;
231230
buf[1] = value ? CP2112_GPIO_ALL_GPIO_MASK : 0;
232231
buf[2] = BIT(offset);
@@ -236,8 +235,16 @@ static void cp2112_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
236235
HID_REQ_SET_REPORT);
237236
if (ret < 0)
238237
hid_err(hdev, "error setting GPIO values: %d\n", ret);
238+
}
239239

240-
mutex_unlock(&dev->lock);
240+
static void cp2112_gpio_set(struct gpio_chip *chip, unsigned int offset,
241+
int value)
242+
{
243+
struct cp2112_device *dev = gpiochip_get_data(chip);
244+
245+
guard(mutex)(&dev->lock);
246+
247+
return cp2112_gpio_set_unlocked(dev, offset, value);
241248
}
242249

243250
static int cp2112_gpio_get_all(struct gpio_chip *chip)
@@ -306,13 +313,13 @@ static int cp2112_gpio_direction_output(struct gpio_chip *chip,
306313
goto fail;
307314
}
308315

309-
mutex_unlock(&dev->lock);
310-
311316
/*
312317
* Set gpio value when output direction is already set,
313318
* as specified in AN495, Rev. 0.2, cpt. 4.4
314319
*/
315-
cp2112_gpio_set(chip, offset, value);
320+
cp2112_gpio_set_unlocked(dev, offset, value);
321+
322+
mutex_unlock(&dev->lock);
316323

317324
return 0;
318325

0 commit comments

Comments
 (0)