Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/hid/hid
[sfrench/cifs-2.6.git] / drivers / usb / misc / rio500.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /* -*- linux-c -*- */
3
4 /* 
5  * Driver for USB Rio 500
6  *
7  * Cesar Miquel (miquel@df.uba.ar)
8  * 
9  * based on hp_scanner.c by David E. Nelson (dnelson@jump.net)
10  *
11  * Based upon mouse.c (Brad Keryan) and printer.c (Michael Gee).
12  *
13  * Changelog:
14  * 30/05/2003  replaced lock/unlock kernel with up/down
15  *             Daniele Bellucci  bellucda@tiscali.it
16  * */
17
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/signal.h>
21 #include <linux/sched/signal.h>
22 #include <linux/mutex.h>
23 #include <linux/errno.h>
24 #include <linux/random.h>
25 #include <linux/poll.h>
26 #include <linux/slab.h>
27 #include <linux/spinlock.h>
28 #include <linux/usb.h>
29 #include <linux/wait.h>
30
31 #include "rio500_usb.h"
32
33 #define DRIVER_AUTHOR "Cesar Miquel <miquel@df.uba.ar>"
34 #define DRIVER_DESC "USB Rio 500 driver"
35
36 #define RIO_MINOR       64
37
38 /* stall/wait timeout for rio */
39 #define NAK_TIMEOUT (HZ)
40
41 #define IBUF_SIZE 0x1000
42
43 /* Size of the rio buffer */
44 #define OBUF_SIZE 0x10000
45
46 struct rio_usb_data {
47         struct usb_device *rio_dev;     /* init: probe_rio */
48         unsigned int ifnum;             /* Interface number of the USB device */
49         int isopen;                     /* nz if open */
50         int present;                    /* Device is present on the bus */
51         char *obuf, *ibuf;              /* transfer buffers */
52         char bulk_in_ep, bulk_out_ep;   /* Endpoint assignments */
53         wait_queue_head_t wait_q;       /* for timeouts */
54         struct mutex lock;          /* general race avoidance */
55 };
56
57 static DEFINE_MUTEX(rio500_mutex);
58 static struct rio_usb_data rio_instance;
59
60 static int open_rio(struct inode *inode, struct file *file)
61 {
62         struct rio_usb_data *rio = &rio_instance;
63
64         /* against disconnect() */
65         mutex_lock(&rio500_mutex);
66         mutex_lock(&(rio->lock));
67
68         if (rio->isopen || !rio->present) {
69                 mutex_unlock(&(rio->lock));
70                 mutex_unlock(&rio500_mutex);
71                 return -EBUSY;
72         }
73         rio->isopen = 1;
74
75         init_waitqueue_head(&rio->wait_q);
76
77         mutex_unlock(&(rio->lock));
78
79         dev_info(&rio->rio_dev->dev, "Rio opened.\n");
80         mutex_unlock(&rio500_mutex);
81
82         return 0;
83 }
84
85 static int close_rio(struct inode *inode, struct file *file)
86 {
87         struct rio_usb_data *rio = &rio_instance;
88
89         /* against disconnect() */
90         mutex_lock(&rio500_mutex);
91         mutex_lock(&(rio->lock));
92
93         rio->isopen = 0;
94         if (!rio->present) {
95                 /* cleanup has been delayed */
96                 kfree(rio->ibuf);
97                 kfree(rio->obuf);
98                 rio->ibuf = NULL;
99                 rio->obuf = NULL;
100         } else {
101                 dev_info(&rio->rio_dev->dev, "Rio closed.\n");
102         }
103         mutex_unlock(&(rio->lock));
104         mutex_unlock(&rio500_mutex);
105         return 0;
106 }
107
108 static long ioctl_rio(struct file *file, unsigned int cmd, unsigned long arg)
109 {
110         struct RioCommand rio_cmd;
111         struct rio_usb_data *rio = &rio_instance;
112         void __user *data;
113         unsigned char *buffer;
114         int result, requesttype;
115         int retries;
116         int retval=0;
117
118         mutex_lock(&(rio->lock));
119         /* Sanity check to make sure rio is connected, powered, etc */
120         if (rio->present == 0 || rio->rio_dev == NULL) {
121                 retval = -ENODEV;
122                 goto err_out;
123         }
124
125         switch (cmd) {
126         case RIO_RECV_COMMAND:
127                 data = (void __user *) arg;
128                 if (data == NULL)
129                         break;
130                 if (copy_from_user(&rio_cmd, data, sizeof(struct RioCommand))) {
131                         retval = -EFAULT;
132                         goto err_out;
133                 }
134                 if (rio_cmd.length < 0 || rio_cmd.length > PAGE_SIZE) {
135                         retval = -EINVAL;
136                         goto err_out;
137                 }
138                 buffer = (unsigned char *) __get_free_page(GFP_KERNEL);
139                 if (buffer == NULL) {
140                         retval = -ENOMEM;
141                         goto err_out;
142                 }
143                 if (copy_from_user(buffer, rio_cmd.buffer, rio_cmd.length)) {
144                         retval = -EFAULT;
145                         free_page((unsigned long) buffer);
146                         goto err_out;
147                 }
148
149                 requesttype = rio_cmd.requesttype | USB_DIR_IN |
150                     USB_TYPE_VENDOR | USB_RECIP_DEVICE;
151                 dev_dbg(&rio->rio_dev->dev,
152                         "sending command:reqtype=%0x req=%0x value=%0x index=%0x len=%0x\n",
153                         requesttype, rio_cmd.request, rio_cmd.value,
154                         rio_cmd.index, rio_cmd.length);
155                 /* Send rio control message */
156                 retries = 3;
157                 while (retries) {
158                         result = usb_control_msg(rio->rio_dev,
159                                                  usb_rcvctrlpipe(rio-> rio_dev, 0),
160                                                  rio_cmd.request,
161                                                  requesttype,
162                                                  rio_cmd.value,
163                                                  rio_cmd.index, buffer,
164                                                  rio_cmd.length,
165                                                  jiffies_to_msecs(rio_cmd.timeout));
166                         if (result == -ETIMEDOUT)
167                                 retries--;
168                         else if (result < 0) {
169                                 dev_err(&rio->rio_dev->dev,
170                                         "Error executing ioctrl. code = %d\n",
171                                         result);
172                                 retries = 0;
173                         } else {
174                                 dev_dbg(&rio->rio_dev->dev,
175                                         "Executed ioctl. Result = %d (data=%02x)\n",
176                                         result, buffer[0]);
177                                 if (copy_to_user(rio_cmd.buffer, buffer,
178                                                  rio_cmd.length)) {
179                                         free_page((unsigned long) buffer);
180                                         retval = -EFAULT;
181                                         goto err_out;
182                                 }
183                                 retries = 0;
184                         }
185
186                         /* rio_cmd.buffer contains a raw stream of single byte
187                            data which has been returned from rio.  Data is
188                            interpreted at application level.  For data that
189                            will be cast to data types longer than 1 byte, data
190                            will be little_endian and will potentially need to
191                            be swapped at the app level */
192
193                 }
194                 free_page((unsigned long) buffer);
195                 break;
196
197         case RIO_SEND_COMMAND:
198                 data = (void __user *) arg;
199                 if (data == NULL)
200                         break;
201                 if (copy_from_user(&rio_cmd, data, sizeof(struct RioCommand))) {
202                         retval = -EFAULT;
203                         goto err_out;
204                 }
205                 if (rio_cmd.length < 0 || rio_cmd.length > PAGE_SIZE) {
206                         retval = -EINVAL;
207                         goto err_out;
208                 }
209                 buffer = (unsigned char *) __get_free_page(GFP_KERNEL);
210                 if (buffer == NULL) {
211                         retval = -ENOMEM;
212                         goto err_out;
213                 }
214                 if (copy_from_user(buffer, rio_cmd.buffer, rio_cmd.length)) {
215                         free_page((unsigned long)buffer);
216                         retval = -EFAULT;
217                         goto err_out;
218                 }
219
220                 requesttype = rio_cmd.requesttype | USB_DIR_OUT |
221                     USB_TYPE_VENDOR | USB_RECIP_DEVICE;
222                 dev_dbg(&rio->rio_dev->dev,
223                         "sending command: reqtype=%0x req=%0x value=%0x index=%0x len=%0x\n",
224                         requesttype, rio_cmd.request, rio_cmd.value,
225                         rio_cmd.index, rio_cmd.length);
226                 /* Send rio control message */
227                 retries = 3;
228                 while (retries) {
229                         result = usb_control_msg(rio->rio_dev,
230                                                  usb_sndctrlpipe(rio-> rio_dev, 0),
231                                                  rio_cmd.request,
232                                                  requesttype,
233                                                  rio_cmd.value,
234                                                  rio_cmd.index, buffer,
235                                                  rio_cmd.length,
236                                                  jiffies_to_msecs(rio_cmd.timeout));
237                         if (result == -ETIMEDOUT)
238                                 retries--;
239                         else if (result < 0) {
240                                 dev_err(&rio->rio_dev->dev,
241                                         "Error executing ioctrl. code = %d\n",
242                                         result);
243                                 retries = 0;
244                         } else {
245                                 dev_dbg(&rio->rio_dev->dev,
246                                         "Executed ioctl. Result = %d\n", result);
247                                 retries = 0;
248
249                         }
250
251                 }
252                 free_page((unsigned long) buffer);
253                 break;
254
255         default:
256                 retval = -ENOTTY;
257                 break;
258         }
259
260
261 err_out:
262         mutex_unlock(&(rio->lock));
263         return retval;
264 }
265
266 static ssize_t
267 write_rio(struct file *file, const char __user *buffer,
268           size_t count, loff_t * ppos)
269 {
270         DEFINE_WAIT(wait);
271         struct rio_usb_data *rio = &rio_instance;
272
273         unsigned long copy_size;
274         unsigned long bytes_written = 0;
275         unsigned int partial;
276
277         int result = 0;
278         int maxretry;
279         int errn = 0;
280         int intr;
281
282         intr = mutex_lock_interruptible(&(rio->lock));
283         if (intr)
284                 return -EINTR;
285         /* Sanity check to make sure rio is connected, powered, etc */
286         if (rio->present == 0 || rio->rio_dev == NULL) {
287                 mutex_unlock(&(rio->lock));
288                 return -ENODEV;
289         }
290
291
292
293         do {
294                 unsigned long thistime;
295                 char *obuf = rio->obuf;
296
297                 thistime = copy_size =
298                     (count >= OBUF_SIZE) ? OBUF_SIZE : count;
299                 if (copy_from_user(rio->obuf, buffer, copy_size)) {
300                         errn = -EFAULT;
301                         goto error;
302                 }
303                 maxretry = 5;
304                 while (thistime) {
305                         if (!rio->rio_dev) {
306                                 errn = -ENODEV;
307                                 goto error;
308                         }
309                         if (signal_pending(current)) {
310                                 mutex_unlock(&(rio->lock));
311                                 return bytes_written ? bytes_written : -EINTR;
312                         }
313
314                         result = usb_bulk_msg(rio->rio_dev,
315                                          usb_sndbulkpipe(rio->rio_dev, 2),
316                                          obuf, thistime, &partial, 5000);
317
318                         dev_dbg(&rio->rio_dev->dev,
319                                 "write stats: result:%d thistime:%lu partial:%u\n",
320                                 result, thistime, partial);
321
322                         if (result == -ETIMEDOUT) {     /* NAK - so hold for a while */
323                                 if (!maxretry--) {
324                                         errn = -ETIME;
325                                         goto error;
326                                 }
327                                 prepare_to_wait(&rio->wait_q, &wait, TASK_INTERRUPTIBLE);
328                                 schedule_timeout(NAK_TIMEOUT);
329                                 finish_wait(&rio->wait_q, &wait);
330                                 continue;
331                         } else if (!result && partial) {
332                                 obuf += partial;
333                                 thistime -= partial;
334                         } else
335                                 break;
336                 }
337                 if (result) {
338                         dev_err(&rio->rio_dev->dev, "Write Whoops - %x\n",
339                                 result);
340                         errn = -EIO;
341                         goto error;
342                 }
343                 bytes_written += copy_size;
344                 count -= copy_size;
345                 buffer += copy_size;
346         } while (count > 0);
347
348         mutex_unlock(&(rio->lock));
349
350         return bytes_written ? bytes_written : -EIO;
351
352 error:
353         mutex_unlock(&(rio->lock));
354         return errn;
355 }
356
357 static ssize_t
358 read_rio(struct file *file, char __user *buffer, size_t count, loff_t * ppos)
359 {
360         DEFINE_WAIT(wait);
361         struct rio_usb_data *rio = &rio_instance;
362         ssize_t read_count;
363         unsigned int partial;
364         int this_read;
365         int result;
366         int maxretry = 10;
367         char *ibuf;
368         int intr;
369
370         intr = mutex_lock_interruptible(&(rio->lock));
371         if (intr)
372                 return -EINTR;
373         /* Sanity check to make sure rio is connected, powered, etc */
374         if (rio->present == 0 || rio->rio_dev == NULL) {
375                 mutex_unlock(&(rio->lock));
376                 return -ENODEV;
377         }
378
379         ibuf = rio->ibuf;
380
381         read_count = 0;
382
383
384         while (count > 0) {
385                 if (signal_pending(current)) {
386                         mutex_unlock(&(rio->lock));
387                         return read_count ? read_count : -EINTR;
388                 }
389                 if (!rio->rio_dev) {
390                         mutex_unlock(&(rio->lock));
391                         return -ENODEV;
392                 }
393                 this_read = (count >= IBUF_SIZE) ? IBUF_SIZE : count;
394
395                 result = usb_bulk_msg(rio->rio_dev,
396                                       usb_rcvbulkpipe(rio->rio_dev, 1),
397                                       ibuf, this_read, &partial,
398                                       8000);
399
400                 dev_dbg(&rio->rio_dev->dev,
401                         "read stats: result:%d this_read:%u partial:%u\n",
402                         result, this_read, partial);
403
404                 if (partial) {
405                         count = this_read = partial;
406                 } else if (result == -ETIMEDOUT || result == 15) {      /* FIXME: 15 ??? */
407                         if (!maxretry--) {
408                                 mutex_unlock(&(rio->lock));
409                                 dev_err(&rio->rio_dev->dev,
410                                         "read_rio: maxretry timeout\n");
411                                 return -ETIME;
412                         }
413                         prepare_to_wait(&rio->wait_q, &wait, TASK_INTERRUPTIBLE);
414                         schedule_timeout(NAK_TIMEOUT);
415                         finish_wait(&rio->wait_q, &wait);
416                         continue;
417                 } else if (result != -EREMOTEIO) {
418                         mutex_unlock(&(rio->lock));
419                         dev_err(&rio->rio_dev->dev,
420                                 "Read Whoops - result:%d partial:%u this_read:%u\n",
421                                 result, partial, this_read);
422                         return -EIO;
423                 } else {
424                         mutex_unlock(&(rio->lock));
425                         return (0);
426                 }
427
428                 if (this_read) {
429                         if (copy_to_user(buffer, ibuf, this_read)) {
430                                 mutex_unlock(&(rio->lock));
431                                 return -EFAULT;
432                         }
433                         count -= this_read;
434                         read_count += this_read;
435                         buffer += this_read;
436                 }
437         }
438         mutex_unlock(&(rio->lock));
439         return read_count;
440 }
441
442 static const struct file_operations usb_rio_fops = {
443         .owner =        THIS_MODULE,
444         .read =         read_rio,
445         .write =        write_rio,
446         .unlocked_ioctl = ioctl_rio,
447         .open =         open_rio,
448         .release =      close_rio,
449         .llseek =       noop_llseek,
450 };
451
452 static struct usb_class_driver usb_rio_class = {
453         .name =         "rio500%d",
454         .fops =         &usb_rio_fops,
455         .minor_base =   RIO_MINOR,
456 };
457
458 static int probe_rio(struct usb_interface *intf,
459                      const struct usb_device_id *id)
460 {
461         struct usb_device *dev = interface_to_usbdev(intf);
462         struct rio_usb_data *rio = &rio_instance;
463         int retval = 0;
464
465         mutex_lock(&rio500_mutex);
466         if (rio->present) {
467                 dev_info(&intf->dev, "Second USB Rio at address %d refused\n", dev->devnum);
468                 retval = -EBUSY;
469                 goto bail_out;
470         } else {
471                 dev_info(&intf->dev, "USB Rio found at address %d\n", dev->devnum);
472         }
473
474         retval = usb_register_dev(intf, &usb_rio_class);
475         if (retval) {
476                 dev_err(&dev->dev,
477                         "Not able to get a minor for this device.\n");
478                 retval = -ENOMEM;
479                 goto bail_out;
480         }
481
482         rio->rio_dev = dev;
483
484         if (!(rio->obuf = kmalloc(OBUF_SIZE, GFP_KERNEL))) {
485                 dev_err(&dev->dev,
486                         "probe_rio: Not enough memory for the output buffer\n");
487                 usb_deregister_dev(intf, &usb_rio_class);
488                 retval = -ENOMEM;
489                 goto bail_out;
490         }
491         dev_dbg(&intf->dev, "obuf address:%p\n", rio->obuf);
492
493         if (!(rio->ibuf = kmalloc(IBUF_SIZE, GFP_KERNEL))) {
494                 dev_err(&dev->dev,
495                         "probe_rio: Not enough memory for the input buffer\n");
496                 usb_deregister_dev(intf, &usb_rio_class);
497                 kfree(rio->obuf);
498                 retval = -ENOMEM;
499                 goto bail_out;
500         }
501         dev_dbg(&intf->dev, "ibuf address:%p\n", rio->ibuf);
502
503         mutex_init(&(rio->lock));
504
505         usb_set_intfdata (intf, rio);
506         rio->present = 1;
507 bail_out:
508         mutex_unlock(&rio500_mutex);
509
510         return retval;
511 }
512
513 static void disconnect_rio(struct usb_interface *intf)
514 {
515         struct rio_usb_data *rio = usb_get_intfdata (intf);
516
517         usb_set_intfdata (intf, NULL);
518         mutex_lock(&rio500_mutex);
519         if (rio) {
520                 usb_deregister_dev(intf, &usb_rio_class);
521
522                 mutex_lock(&(rio->lock));
523                 if (rio->isopen) {
524                         rio->isopen = 0;
525                         /* better let it finish - the release will do whats needed */
526                         rio->rio_dev = NULL;
527                         mutex_unlock(&(rio->lock));
528                         mutex_unlock(&rio500_mutex);
529                         return;
530                 }
531                 kfree(rio->ibuf);
532                 kfree(rio->obuf);
533
534                 dev_info(&intf->dev, "USB Rio disconnected.\n");
535
536                 rio->present = 0;
537                 mutex_unlock(&(rio->lock));
538         }
539         mutex_unlock(&rio500_mutex);
540 }
541
542 static const struct usb_device_id rio_table[] = {
543         { USB_DEVICE(0x0841, 1) },              /* Rio 500 */
544         { }                                     /* Terminating entry */
545 };
546
547 MODULE_DEVICE_TABLE (usb, rio_table);
548
549 static struct usb_driver rio_driver = {
550         .name =         "rio500",
551         .probe =        probe_rio,
552         .disconnect =   disconnect_rio,
553         .id_table =     rio_table,
554 };
555
556 module_usb_driver(rio_driver);
557
558 MODULE_AUTHOR( DRIVER_AUTHOR );
559 MODULE_DESCRIPTION( DRIVER_DESC );
560 MODULE_LICENSE("GPL");
561