17
17
*/
18
18
19
19
#include <linux/bitops.h>
20
+ #include <linux/cleanup.h>
20
21
#include <linux/gpio/driver.h>
21
22
#include <linux/hid.h>
22
23
#include <linux/hidraw.h>
23
24
#include <linux/i2c.h>
24
25
#include <linux/module.h>
26
+ #include <linux/mutex.h>
25
27
#include <linux/nls.h>
26
28
#include <linux/string_choices.h>
27
29
#include <linux/usb/ch9.h>
@@ -185,7 +187,7 @@ static int cp2112_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
185
187
u8 * buf = dev -> in_out_buffer ;
186
188
int ret ;
187
189
188
- mutex_lock (& dev -> lock );
190
+ guard ( mutex ) (& dev -> lock );
189
191
190
192
ret = hid_hw_raw_request (hdev , CP2112_GPIO_CONFIG , buf ,
191
193
CP2112_GPIO_CONFIG_LENGTH , HID_FEATURE_REPORT ,
@@ -194,7 +196,7 @@ static int cp2112_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
194
196
hid_err (hdev , "error requesting GPIO config: %d\n" , ret );
195
197
if (ret >= 0 )
196
198
ret = - EIO ;
197
- goto exit ;
199
+ return ret ;
198
200
}
199
201
200
202
buf [1 ] &= ~BIT (offset );
@@ -207,25 +209,19 @@ static int cp2112_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
207
209
hid_err (hdev , "error setting GPIO config: %d\n" , ret );
208
210
if (ret >= 0 )
209
211
ret = - EIO ;
210
- goto exit ;
212
+ return ret ;
211
213
}
212
214
213
- ret = 0 ;
214
-
215
- exit :
216
- mutex_unlock (& dev -> lock );
217
- return ret ;
215
+ return 0 ;
218
216
}
219
217
220
- static void cp2112_gpio_set (struct gpio_chip * chip , unsigned offset , int value )
218
+ static int cp2112_gpio_set_unlocked (struct cp2112_device * dev ,
219
+ unsigned int offset , int value )
221
220
{
222
- struct cp2112_device * dev = gpiochip_get_data (chip );
223
221
struct hid_device * hdev = dev -> hdev ;
224
222
u8 * buf = dev -> in_out_buffer ;
225
223
int ret ;
226
224
227
- mutex_lock (& dev -> lock );
228
-
229
225
buf [0 ] = CP2112_GPIO_SET ;
230
226
buf [1 ] = value ? CP2112_GPIO_ALL_GPIO_MASK : 0 ;
231
227
buf [2 ] = BIT (offset );
@@ -236,7 +232,17 @@ static void cp2112_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
236
232
if (ret < 0 )
237
233
hid_err (hdev , "error setting GPIO values: %d\n" , ret );
238
234
239
- mutex_unlock (& dev -> lock );
235
+ return ret ;
236
+ }
237
+
238
+ static int cp2112_gpio_set (struct gpio_chip * chip , unsigned int offset ,
239
+ int value )
240
+ {
241
+ struct cp2112_device * dev = gpiochip_get_data (chip );
242
+
243
+ guard (mutex )(& dev -> lock );
244
+
245
+ return cp2112_gpio_set_unlocked (dev , offset , value );
240
246
}
241
247
242
248
static int cp2112_gpio_get_all (struct gpio_chip * chip )
@@ -246,23 +252,17 @@ static int cp2112_gpio_get_all(struct gpio_chip *chip)
246
252
u8 * buf = dev -> in_out_buffer ;
247
253
int ret ;
248
254
249
- mutex_lock (& dev -> lock );
255
+ guard ( mutex ) (& dev -> lock );
250
256
251
257
ret = hid_hw_raw_request (hdev , CP2112_GPIO_GET , buf ,
252
258
CP2112_GPIO_GET_LENGTH , HID_FEATURE_REPORT ,
253
259
HID_REQ_GET_REPORT );
254
260
if (ret != CP2112_GPIO_GET_LENGTH ) {
255
261
hid_err (hdev , "error requesting GPIO values: %d\n" , ret );
256
- ret = ret < 0 ? ret : - EIO ;
257
- goto exit ;
262
+ return ret < 0 ? ret : - EIO ;
258
263
}
259
264
260
- ret = buf [1 ];
261
-
262
- exit :
263
- mutex_unlock (& dev -> lock );
264
-
265
- return ret ;
265
+ return buf [1 ];
266
266
}
267
267
268
268
static int cp2112_gpio_get (struct gpio_chip * chip , unsigned int offset )
@@ -284,14 +284,14 @@ static int cp2112_gpio_direction_output(struct gpio_chip *chip,
284
284
u8 * buf = dev -> in_out_buffer ;
285
285
int ret ;
286
286
287
- mutex_lock (& dev -> lock );
287
+ guard ( mutex ) (& dev -> lock );
288
288
289
289
ret = hid_hw_raw_request (hdev , CP2112_GPIO_CONFIG , buf ,
290
290
CP2112_GPIO_CONFIG_LENGTH , HID_FEATURE_REPORT ,
291
291
HID_REQ_GET_REPORT );
292
292
if (ret != CP2112_GPIO_CONFIG_LENGTH ) {
293
293
hid_err (hdev , "error requesting GPIO config: %d\n" , ret );
294
- goto fail ;
294
+ return ret < 0 ? ret : - EIO ;
295
295
}
296
296
297
297
buf [1 ] |= 1 << offset ;
@@ -302,22 +302,16 @@ static int cp2112_gpio_direction_output(struct gpio_chip *chip,
302
302
HID_REQ_SET_REPORT );
303
303
if (ret < 0 ) {
304
304
hid_err (hdev , "error setting GPIO config: %d\n" , ret );
305
- goto fail ;
305
+ return ret ;
306
306
}
307
307
308
- mutex_unlock (& dev -> lock );
309
-
310
308
/*
311
309
* Set gpio value when output direction is already set,
312
310
* as specified in AN495, Rev. 0.2, cpt. 4.4
313
311
*/
314
- cp2112_gpio_set ( chip , offset , value );
312
+ cp2112_gpio_set_unlocked ( dev , offset , value );
315
313
316
314
return 0 ;
317
-
318
- fail :
319
- mutex_unlock (& dev -> lock );
320
- return ret < 0 ? ret : - EIO ;
321
315
}
322
316
323
317
static int cp2112_hid_get (struct hid_device * hdev , unsigned char report_number ,
@@ -1205,7 +1199,11 @@ static int cp2112_probe(struct hid_device *hdev, const struct hid_device_id *id)
1205
1199
if (!dev -> in_out_buffer )
1206
1200
return - ENOMEM ;
1207
1201
1208
- mutex_init (& dev -> lock );
1202
+ ret = devm_mutex_init (& hdev -> dev , & dev -> lock );
1203
+ if (ret ) {
1204
+ hid_err (hdev , "mutex init failed\n" );
1205
+ return ret ;
1206
+ }
1209
1207
1210
1208
ret = hid_parse (hdev );
1211
1209
if (ret ) {
@@ -1290,7 +1288,7 @@ static int cp2112_probe(struct hid_device *hdev, const struct hid_device_id *id)
1290
1288
dev -> gc .label = "cp2112_gpio" ;
1291
1289
dev -> gc .direction_input = cp2112_gpio_direction_input ;
1292
1290
dev -> gc .direction_output = cp2112_gpio_direction_output ;
1293
- dev -> gc .set = cp2112_gpio_set ;
1291
+ dev -> gc .set_rv = cp2112_gpio_set ;
1294
1292
dev -> gc .get = cp2112_gpio_get ;
1295
1293
dev -> gc .base = -1 ;
1296
1294
dev -> gc .ngpio = CP2112_GPIO_MAX_GPIO ;
0 commit comments