diff options
author | Oliver Neukum <oneukum@suse.com> | 2019-05-09 05:31:00 -0400 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@linuxfoundation.org> | 2019-09-03 14:18:18 -0400 |
commit | 95e29e9bbe28e636d2f089a2fa1cba9d0a436fe3 (patch) | |
tree | 86e1f727b11e12ede36158dce4209576927a35a6 | |
parent | 1ac91ac5d097c1928efe6691d72c619462a9e3c5 (diff) |
USB: rio500: simplify locking
Admitting that there can be only one device allows us to drop any
pretense about locking one device or a table of devices.
Signed-off-by: Oliver Neukum <oneukum@suse.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
-rw-r--r-- | drivers/usb/misc/rio500.c | 43 |
1 files changed, 16 insertions, 27 deletions
diff --git a/drivers/usb/misc/rio500.c b/drivers/usb/misc/rio500.c index a32d61a79ab8..27e9c78a791e 100644 --- a/drivers/usb/misc/rio500.c +++ b/drivers/usb/misc/rio500.c | |||
@@ -51,7 +51,6 @@ struct rio_usb_data { | |||
51 | char *obuf, *ibuf; /* transfer buffers */ | 51 | char *obuf, *ibuf; /* transfer buffers */ |
52 | char bulk_in_ep, bulk_out_ep; /* Endpoint assignments */ | 52 | char bulk_in_ep, bulk_out_ep; /* Endpoint assignments */ |
53 | wait_queue_head_t wait_q; /* for timeouts */ | 53 | wait_queue_head_t wait_q; /* for timeouts */ |
54 | struct mutex lock; /* general race avoidance */ | ||
55 | }; | 54 | }; |
56 | 55 | ||
57 | static DEFINE_MUTEX(rio500_mutex); | 56 | static DEFINE_MUTEX(rio500_mutex); |
@@ -63,10 +62,8 @@ static int open_rio(struct inode *inode, struct file *file) | |||
63 | 62 | ||
64 | /* against disconnect() */ | 63 | /* against disconnect() */ |
65 | mutex_lock(&rio500_mutex); | 64 | mutex_lock(&rio500_mutex); |
66 | mutex_lock(&(rio->lock)); | ||
67 | 65 | ||
68 | if (rio->isopen || !rio->present) { | 66 | if (rio->isopen || !rio->present) { |
69 | mutex_unlock(&(rio->lock)); | ||
70 | mutex_unlock(&rio500_mutex); | 67 | mutex_unlock(&rio500_mutex); |
71 | return -EBUSY; | 68 | return -EBUSY; |
72 | } | 69 | } |
@@ -74,7 +71,6 @@ static int open_rio(struct inode *inode, struct file *file) | |||
74 | 71 | ||
75 | init_waitqueue_head(&rio->wait_q); | 72 | init_waitqueue_head(&rio->wait_q); |
76 | 73 | ||
77 | mutex_unlock(&(rio->lock)); | ||
78 | 74 | ||
79 | dev_info(&rio->rio_dev->dev, "Rio opened.\n"); | 75 | dev_info(&rio->rio_dev->dev, "Rio opened.\n"); |
80 | mutex_unlock(&rio500_mutex); | 76 | mutex_unlock(&rio500_mutex); |
@@ -88,7 +84,6 @@ static int close_rio(struct inode *inode, struct file *file) | |||
88 | 84 | ||
89 | /* against disconnect() */ | 85 | /* against disconnect() */ |
90 | mutex_lock(&rio500_mutex); | 86 | mutex_lock(&rio500_mutex); |
91 | mutex_lock(&(rio->lock)); | ||
92 | 87 | ||
93 | rio->isopen = 0; | 88 | rio->isopen = 0; |
94 | if (!rio->present) { | 89 | if (!rio->present) { |
@@ -100,7 +95,6 @@ static int close_rio(struct inode *inode, struct file *file) | |||
100 | } else { | 95 | } else { |
101 | dev_info(&rio->rio_dev->dev, "Rio closed.\n"); | 96 | dev_info(&rio->rio_dev->dev, "Rio closed.\n"); |
102 | } | 97 | } |
103 | mutex_unlock(&(rio->lock)); | ||
104 | mutex_unlock(&rio500_mutex); | 98 | mutex_unlock(&rio500_mutex); |
105 | return 0; | 99 | return 0; |
106 | } | 100 | } |
@@ -115,7 +109,7 @@ static long ioctl_rio(struct file *file, unsigned int cmd, unsigned long arg) | |||
115 | int retries; | 109 | int retries; |
116 | int retval=0; | 110 | int retval=0; |
117 | 111 | ||
118 | mutex_lock(&(rio->lock)); | 112 | mutex_lock(&rio500_mutex); |
119 | /* Sanity check to make sure rio is connected, powered, etc */ | 113 | /* Sanity check to make sure rio is connected, powered, etc */ |
120 | if (rio->present == 0 || rio->rio_dev == NULL) { | 114 | if (rio->present == 0 || rio->rio_dev == NULL) { |
121 | retval = -ENODEV; | 115 | retval = -ENODEV; |
@@ -259,7 +253,7 @@ static long ioctl_rio(struct file *file, unsigned int cmd, unsigned long arg) | |||
259 | 253 | ||
260 | 254 | ||
261 | err_out: | 255 | err_out: |
262 | mutex_unlock(&(rio->lock)); | 256 | mutex_unlock(&rio500_mutex); |
263 | return retval; | 257 | return retval; |
264 | } | 258 | } |
265 | 259 | ||
@@ -279,12 +273,12 @@ write_rio(struct file *file, const char __user *buffer, | |||
279 | int errn = 0; | 273 | int errn = 0; |
280 | int intr; | 274 | int intr; |
281 | 275 | ||
282 | intr = mutex_lock_interruptible(&(rio->lock)); | 276 | intr = mutex_lock_interruptible(&rio500_mutex); |
283 | if (intr) | 277 | if (intr) |
284 | return -EINTR; | 278 | return -EINTR; |
285 | /* Sanity check to make sure rio is connected, powered, etc */ | 279 | /* Sanity check to make sure rio is connected, powered, etc */ |
286 | if (rio->present == 0 || rio->rio_dev == NULL) { | 280 | if (rio->present == 0 || rio->rio_dev == NULL) { |
287 | mutex_unlock(&(rio->lock)); | 281 | mutex_unlock(&rio500_mutex); |
288 | return -ENODEV; | 282 | return -ENODEV; |
289 | } | 283 | } |
290 | 284 | ||
@@ -307,7 +301,7 @@ write_rio(struct file *file, const char __user *buffer, | |||
307 | goto error; | 301 | goto error; |
308 | } | 302 | } |
309 | if (signal_pending(current)) { | 303 | if (signal_pending(current)) { |
310 | mutex_unlock(&(rio->lock)); | 304 | mutex_unlock(&rio500_mutex); |
311 | return bytes_written ? bytes_written : -EINTR; | 305 | return bytes_written ? bytes_written : -EINTR; |
312 | } | 306 | } |
313 | 307 | ||
@@ -345,12 +339,12 @@ write_rio(struct file *file, const char __user *buffer, | |||
345 | buffer += copy_size; | 339 | buffer += copy_size; |
346 | } while (count > 0); | 340 | } while (count > 0); |
347 | 341 | ||
348 | mutex_unlock(&(rio->lock)); | 342 | mutex_unlock(&rio500_mutex); |
349 | 343 | ||
350 | return bytes_written ? bytes_written : -EIO; | 344 | return bytes_written ? bytes_written : -EIO; |
351 | 345 | ||
352 | error: | 346 | error: |
353 | mutex_unlock(&(rio->lock)); | 347 | mutex_unlock(&rio500_mutex); |
354 | return errn; | 348 | return errn; |
355 | } | 349 | } |
356 | 350 | ||
@@ -367,12 +361,12 @@ read_rio(struct file *file, char __user *buffer, size_t count, loff_t * ppos) | |||
367 | char *ibuf; | 361 | char *ibuf; |
368 | int intr; | 362 | int intr; |
369 | 363 | ||
370 | intr = mutex_lock_interruptible(&(rio->lock)); | 364 | intr = mutex_lock_interruptible(&rio500_mutex); |
371 | if (intr) | 365 | if (intr) |
372 | return -EINTR; | 366 | return -EINTR; |
373 | /* Sanity check to make sure rio is connected, powered, etc */ | 367 | /* Sanity check to make sure rio is connected, powered, etc */ |
374 | if (rio->present == 0 || rio->rio_dev == NULL) { | 368 | if (rio->present == 0 || rio->rio_dev == NULL) { |
375 | mutex_unlock(&(rio->lock)); | 369 | mutex_unlock(&rio500_mutex); |
376 | return -ENODEV; | 370 | return -ENODEV; |
377 | } | 371 | } |
378 | 372 | ||
@@ -383,11 +377,11 @@ read_rio(struct file *file, char __user *buffer, size_t count, loff_t * ppos) | |||
383 | 377 | ||
384 | while (count > 0) { | 378 | while (count > 0) { |
385 | if (signal_pending(current)) { | 379 | if (signal_pending(current)) { |
386 | mutex_unlock(&(rio->lock)); | 380 | mutex_unlock(&rio500_mutex); |
387 | return read_count ? read_count : -EINTR; | 381 | return read_count ? read_count : -EINTR; |
388 | } | 382 | } |
389 | if (!rio->rio_dev) { | 383 | if (!rio->rio_dev) { |
390 | mutex_unlock(&(rio->lock)); | 384 | mutex_unlock(&rio500_mutex); |
391 | return -ENODEV; | 385 | return -ENODEV; |
392 | } | 386 | } |
393 | this_read = (count >= IBUF_SIZE) ? IBUF_SIZE : count; | 387 | this_read = (count >= IBUF_SIZE) ? IBUF_SIZE : count; |
@@ -405,7 +399,7 @@ read_rio(struct file *file, char __user *buffer, size_t count, loff_t * ppos) | |||
405 | count = this_read = partial; | 399 | count = this_read = partial; |
406 | } else if (result == -ETIMEDOUT || result == 15) { /* FIXME: 15 ??? */ | 400 | } else if (result == -ETIMEDOUT || result == 15) { /* FIXME: 15 ??? */ |
407 | if (!maxretry--) { | 401 | if (!maxretry--) { |
408 | mutex_unlock(&(rio->lock)); | 402 | mutex_unlock(&rio500_mutex); |
409 | dev_err(&rio->rio_dev->dev, | 403 | dev_err(&rio->rio_dev->dev, |
410 | "read_rio: maxretry timeout\n"); | 404 | "read_rio: maxretry timeout\n"); |
411 | return -ETIME; | 405 | return -ETIME; |
@@ -415,19 +409,19 @@ read_rio(struct file *file, char __user *buffer, size_t count, loff_t * ppos) | |||
415 | finish_wait(&rio->wait_q, &wait); | 409 | finish_wait(&rio->wait_q, &wait); |
416 | continue; | 410 | continue; |
417 | } else if (result != -EREMOTEIO) { | 411 | } else if (result != -EREMOTEIO) { |
418 | mutex_unlock(&(rio->lock)); | 412 | mutex_unlock(&rio500_mutex); |
419 | dev_err(&rio->rio_dev->dev, | 413 | dev_err(&rio->rio_dev->dev, |
420 | "Read Whoops - result:%d partial:%u this_read:%u\n", | 414 | "Read Whoops - result:%d partial:%u this_read:%u\n", |
421 | result, partial, this_read); | 415 | result, partial, this_read); |
422 | return -EIO; | 416 | return -EIO; |
423 | } else { | 417 | } else { |
424 | mutex_unlock(&(rio->lock)); | 418 | mutex_unlock(&rio500_mutex); |
425 | return (0); | 419 | return (0); |
426 | } | 420 | } |
427 | 421 | ||
428 | if (this_read) { | 422 | if (this_read) { |
429 | if (copy_to_user(buffer, ibuf, this_read)) { | 423 | if (copy_to_user(buffer, ibuf, this_read)) { |
430 | mutex_unlock(&(rio->lock)); | 424 | mutex_unlock(&rio500_mutex); |
431 | return -EFAULT; | 425 | return -EFAULT; |
432 | } | 426 | } |
433 | count -= this_read; | 427 | count -= this_read; |
@@ -435,7 +429,7 @@ read_rio(struct file *file, char __user *buffer, size_t count, loff_t * ppos) | |||
435 | buffer += this_read; | 429 | buffer += this_read; |
436 | } | 430 | } |
437 | } | 431 | } |
438 | mutex_unlock(&(rio->lock)); | 432 | mutex_unlock(&rio500_mutex); |
439 | return read_count; | 433 | return read_count; |
440 | } | 434 | } |
441 | 435 | ||
@@ -500,8 +494,6 @@ static int probe_rio(struct usb_interface *intf, | |||
500 | } | 494 | } |
501 | dev_dbg(&intf->dev, "ibuf address:%p\n", rio->ibuf); | 495 | dev_dbg(&intf->dev, "ibuf address:%p\n", rio->ibuf); |
502 | 496 | ||
503 | mutex_init(&(rio->lock)); | ||
504 | |||
505 | usb_set_intfdata (intf, rio); | 497 | usb_set_intfdata (intf, rio); |
506 | rio->present = 1; | 498 | rio->present = 1; |
507 | bail_out: | 499 | bail_out: |
@@ -519,12 +511,10 @@ static void disconnect_rio(struct usb_interface *intf) | |||
519 | if (rio) { | 511 | if (rio) { |
520 | usb_deregister_dev(intf, &usb_rio_class); | 512 | usb_deregister_dev(intf, &usb_rio_class); |
521 | 513 | ||
522 | mutex_lock(&(rio->lock)); | ||
523 | if (rio->isopen) { | 514 | if (rio->isopen) { |
524 | rio->isopen = 0; | 515 | rio->isopen = 0; |
525 | /* better let it finish - the release will do whats needed */ | 516 | /* better let it finish - the release will do whats needed */ |
526 | rio->rio_dev = NULL; | 517 | rio->rio_dev = NULL; |
527 | mutex_unlock(&(rio->lock)); | ||
528 | mutex_unlock(&rio500_mutex); | 518 | mutex_unlock(&rio500_mutex); |
529 | return; | 519 | return; |
530 | } | 520 | } |
@@ -534,7 +524,6 @@ static void disconnect_rio(struct usb_interface *intf) | |||
534 | dev_info(&intf->dev, "USB Rio disconnected.\n"); | 524 | dev_info(&intf->dev, "USB Rio disconnected.\n"); |
535 | 525 | ||
536 | rio->present = 0; | 526 | rio->present = 0; |
537 | mutex_unlock(&(rio->lock)); | ||
538 | } | 527 | } |
539 | mutex_unlock(&rio500_mutex); | 528 | mutex_unlock(&rio500_mutex); |
540 | } | 529 | } |