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>
2324#include <linux/i2c.h>
2425#include <linux/module.h>
26+ #include <linux/mutex.h>
2527#include <linux/nls.h>
2628#include <linux/string_choices.h>
2729#include <linux/usb/ch9.h>
@@ -185,7 +187,7 @@ static int cp2112_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
185187 u8 * buf = dev -> in_out_buffer ;
186188 int ret ;
187189
188- mutex_lock (& dev -> lock );
190+ guard ( mutex ) (& dev -> lock );
189191
190192 ret = hid_hw_raw_request (hdev , CP2112_GPIO_CONFIG , buf ,
191193 CP2112_GPIO_CONFIG_LENGTH , HID_FEATURE_REPORT ,
@@ -194,7 +196,7 @@ static int cp2112_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
194196 hid_err (hdev , "error requesting GPIO config: %d\n" , ret );
195197 if (ret >= 0 )
196198 ret = - EIO ;
197- goto exit ;
199+ return ret ;
198200 }
199201
200202 buf [1 ] &= ~BIT (offset );
@@ -207,25 +209,19 @@ static int cp2112_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
207209 hid_err (hdev , "error setting GPIO config: %d\n" , ret );
208210 if (ret >= 0 )
209211 ret = - EIO ;
210- goto exit ;
212+ return ret ;
211213 }
212214
213- ret = 0 ;
214-
215- exit :
216- mutex_unlock (& dev -> lock );
217- return ret ;
215+ return 0 ;
218216}
219217
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 )
221220{
222- struct cp2112_device * dev = gpiochip_get_data (chip );
223221 struct hid_device * hdev = dev -> hdev ;
224222 u8 * buf = dev -> in_out_buffer ;
225223 int ret ;
226224
227- mutex_lock (& dev -> lock );
228-
229225 buf [0 ] = CP2112_GPIO_SET ;
230226 buf [1 ] = value ? CP2112_GPIO_ALL_GPIO_MASK : 0 ;
231227 buf [2 ] = BIT (offset );
@@ -236,7 +232,17 @@ static void cp2112_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
236232 if (ret < 0 )
237233 hid_err (hdev , "error setting GPIO values: %d\n" , ret );
238234
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 );
240246}
241247
242248static int cp2112_gpio_get_all (struct gpio_chip * chip )
@@ -246,23 +252,17 @@ static int cp2112_gpio_get_all(struct gpio_chip *chip)
246252 u8 * buf = dev -> in_out_buffer ;
247253 int ret ;
248254
249- mutex_lock (& dev -> lock );
255+ guard ( mutex ) (& dev -> lock );
250256
251257 ret = hid_hw_raw_request (hdev , CP2112_GPIO_GET , buf ,
252258 CP2112_GPIO_GET_LENGTH , HID_FEATURE_REPORT ,
253259 HID_REQ_GET_REPORT );
254260 if (ret != CP2112_GPIO_GET_LENGTH ) {
255261 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 ;
258263 }
259264
260- ret = buf [1 ];
261-
262- exit :
263- mutex_unlock (& dev -> lock );
264-
265- return ret ;
265+ return buf [1 ];
266266}
267267
268268static 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,
284284 u8 * buf = dev -> in_out_buffer ;
285285 int ret ;
286286
287- mutex_lock (& dev -> lock );
287+ guard ( mutex ) (& dev -> lock );
288288
289289 ret = hid_hw_raw_request (hdev , CP2112_GPIO_CONFIG , buf ,
290290 CP2112_GPIO_CONFIG_LENGTH , HID_FEATURE_REPORT ,
291291 HID_REQ_GET_REPORT );
292292 if (ret != CP2112_GPIO_CONFIG_LENGTH ) {
293293 hid_err (hdev , "error requesting GPIO config: %d\n" , ret );
294- goto fail ;
294+ return ret < 0 ? ret : - EIO ;
295295 }
296296
297297 buf [1 ] |= 1 << offset ;
@@ -302,22 +302,16 @@ static int cp2112_gpio_direction_output(struct gpio_chip *chip,
302302 HID_REQ_SET_REPORT );
303303 if (ret < 0 ) {
304304 hid_err (hdev , "error setting GPIO config: %d\n" , ret );
305- goto fail ;
305+ return ret ;
306306 }
307307
308- mutex_unlock (& dev -> lock );
309-
310308 /*
311309 * Set gpio value when output direction is already set,
312310 * as specified in AN495, Rev. 0.2, cpt. 4.4
313311 */
314- cp2112_gpio_set ( chip , offset , value );
312+ cp2112_gpio_set_unlocked ( dev , offset , value );
315313
316314 return 0 ;
317-
318- fail :
319- mutex_unlock (& dev -> lock );
320- return ret < 0 ? ret : - EIO ;
321315}
322316
323317static 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)
12051199 if (!dev -> in_out_buffer )
12061200 return - ENOMEM ;
12071201
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+ }
12091207
12101208 ret = hid_parse (hdev );
12111209 if (ret ) {
@@ -1290,7 +1288,7 @@ static int cp2112_probe(struct hid_device *hdev, const struct hid_device_id *id)
12901288 dev -> gc .label = "cp2112_gpio" ;
12911289 dev -> gc .direction_input = cp2112_gpio_direction_input ;
12921290 dev -> gc .direction_output = cp2112_gpio_direction_output ;
1293- dev -> gc .set = cp2112_gpio_set ;
1291+ dev -> gc .set_rv = cp2112_gpio_set ;
12941292 dev -> gc .get = cp2112_gpio_get ;
12951293 dev -> gc .base = -1 ;
12961294 dev -> gc .ngpio = CP2112_GPIO_MAX_GPIO ;
0 commit comments