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