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