]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/usb/misc/rio500.c
Merge branch 'timers-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[mirror_ubuntu-jammy-kernel.git] / drivers / usb / misc / rio500.c
CommitLineData
5fd54ace 1// SPDX-License-Identifier: GPL-2.0+
1da177e4
LT
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)
1da177e4
LT
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>
174cd4b1 21#include <linux/sched/signal.h>
925ce689 22#include <linux/mutex.h>
1da177e4
LT
23#include <linux/errno.h>
24#include <linux/random.h>
25#include <linux/poll.h>
1da177e4
LT
26#include <linux/slab.h>
27#include <linux/spinlock.h>
28#include <linux/usb.h>
1da177e4
LT
29#include <linux/wait.h>
30
31#include "rio500_usb.h"
32
1da177e4
LT
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
46struct 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 */
1da177e4
LT
54};
55
925ce689 56static DEFINE_MUTEX(rio500_mutex);
1da177e4
LT
57static struct rio_usb_data rio_instance;
58
59static int open_rio(struct inode *inode, struct file *file)
60{
61 struct rio_usb_data *rio = &rio_instance;
511e2d02
ON
62
63 /* against disconnect() */
925ce689 64 mutex_lock(&rio500_mutex);
1da177e4
LT
65
66 if (rio->isopen || !rio->present) {
925ce689 67 mutex_unlock(&rio500_mutex);
1da177e4
LT
68 return -EBUSY;
69 }
70 rio->isopen = 1;
71
72 init_waitqueue_head(&rio->wait_q);
73
1da177e4 74
1b29a375 75 dev_info(&rio->rio_dev->dev, "Rio opened.\n");
925ce689 76 mutex_unlock(&rio500_mutex);
1da177e4
LT
77
78 return 0;
79}
80
81static int close_rio(struct inode *inode, struct file *file)
82{
83 struct rio_usb_data *rio = &rio_instance;
84
e0feb734
ON
85 /* against disconnect() */
86 mutex_lock(&rio500_mutex);
1da177e4 87
e0feb734
ON
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 }
e0feb734 98 mutex_unlock(&rio500_mutex);
1da177e4
LT
99 return 0;
100}
101
54592157 102static long ioctl_rio(struct file *file, unsigned int cmd, unsigned long arg)
1da177e4
LT
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
d710734b 112 mutex_lock(&rio500_mutex);
1da177e4 113 /* Sanity check to make sure rio is connected, powered, etc */
3328d975 114 if (rio->present == 0 || rio->rio_dev == NULL) {
1da177e4
LT
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;
e1a344d3
GKH
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);
1da177e4
LT
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) {
c41fba13
GKH
163 dev_err(&rio->rio_dev->dev,
164 "Error executing ioctrl. code = %d\n",
165 result);
1da177e4
LT
166 retries = 0;
167 } else {
e1a344d3
GKH
168 dev_dbg(&rio->rio_dev->dev,
169 "Executed ioctl. Result = %d (data=%02x)\n",
170 result, buffer[0]);
1da177e4
LT
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;
e1a344d3
GKH
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);
1da177e4
LT
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) {
c41fba13
GKH
234 dev_err(&rio->rio_dev->dev,
235 "Error executing ioctrl. code = %d\n",
236 result);
1da177e4
LT
237 retries = 0;
238 } else {
e1a344d3
GKH
239 dev_dbg(&rio->rio_dev->dev,
240 "Executed ioctl. Result = %d\n", result);
1da177e4
LT
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
255err_out:
d710734b 256 mutex_unlock(&rio500_mutex);
1da177e4
LT
257 return retval;
258}
259
260static ssize_t
261write_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;
2cba72f0 274 int intr;
1da177e4 275
d710734b 276 intr = mutex_lock_interruptible(&rio500_mutex);
2cba72f0
ON
277 if (intr)
278 return -EINTR;
1da177e4 279 /* Sanity check to make sure rio is connected, powered, etc */
3328d975 280 if (rio->present == 0 || rio->rio_dev == NULL) {
d710734b 281 mutex_unlock(&rio500_mutex);
1da177e4
LT
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)) {
d710734b 304 mutex_unlock(&rio500_mutex);
1da177e4
LT
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
e1a344d3
GKH
312 dev_dbg(&rio->rio_dev->dev,
313 "write stats: result:%d thistime:%lu partial:%u\n",
314 result, thistime, partial);
1da177e4
LT
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;
17e67910 330 }
1da177e4 331 if (result) {
c41fba13
GKH
332 dev_err(&rio->rio_dev->dev, "Write Whoops - %x\n",
333 result);
1da177e4
LT
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
d710734b 342 mutex_unlock(&rio500_mutex);
1da177e4
LT
343
344 return bytes_written ? bytes_written : -EIO;
345
346error:
d710734b 347 mutex_unlock(&rio500_mutex);
1da177e4
LT
348 return errn;
349}
350
351static ssize_t
352read_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;
2cba72f0 362 int intr;
1da177e4 363
d710734b 364 intr = mutex_lock_interruptible(&rio500_mutex);
2cba72f0
ON
365 if (intr)
366 return -EINTR;
1da177e4 367 /* Sanity check to make sure rio is connected, powered, etc */
3328d975 368 if (rio->present == 0 || rio->rio_dev == NULL) {
d710734b 369 mutex_unlock(&rio500_mutex);
1da177e4
LT
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)) {
d710734b 380 mutex_unlock(&rio500_mutex);
1da177e4
LT
381 return read_count ? read_count : -EINTR;
382 }
383 if (!rio->rio_dev) {
d710734b 384 mutex_unlock(&rio500_mutex);
1da177e4
LT
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
e1a344d3
GKH
394 dev_dbg(&rio->rio_dev->dev,
395 "read stats: result:%d this_read:%u partial:%u\n",
396 result, this_read, partial);
1da177e4
LT
397
398 if (partial) {
399 count = this_read = partial;
400 } else if (result == -ETIMEDOUT || result == 15) { /* FIXME: 15 ??? */
401 if (!maxretry--) {
d710734b 402 mutex_unlock(&rio500_mutex);
c41fba13
GKH
403 dev_err(&rio->rio_dev->dev,
404 "read_rio: maxretry timeout\n");
1da177e4
LT
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) {
d710734b 412 mutex_unlock(&rio500_mutex);
c41fba13 413 dev_err(&rio->rio_dev->dev,
651b297c 414 "Read Whoops - result:%d partial:%u this_read:%u\n",
c41fba13 415 result, partial, this_read);
1da177e4
LT
416 return -EIO;
417 } else {
d710734b 418 mutex_unlock(&rio500_mutex);
1da177e4
LT
419 return (0);
420 }
421
422 if (this_read) {
423 if (copy_to_user(buffer, ibuf, this_read)) {
d710734b 424 mutex_unlock(&rio500_mutex);
1da177e4
LT
425 return -EFAULT;
426 }
427 count -= this_read;
428 read_count += this_read;
429 buffer += this_read;
430 }
431 }
d710734b 432 mutex_unlock(&rio500_mutex);
1da177e4
LT
433 return read_count;
434}
435
828c0950 436static const struct file_operations usb_rio_fops = {
1da177e4
LT
437 .owner = THIS_MODULE,
438 .read = read_rio,
439 .write = write_rio,
54592157 440 .unlocked_ioctl = ioctl_rio,
1da177e4
LT
441 .open = open_rio,
442 .release = close_rio,
6038f373 443 .llseek = noop_llseek,
1da177e4
LT
444};
445
446static struct usb_class_driver usb_rio_class = {
d6e5bcf4 447 .name = "rio500%d",
1da177e4 448 .fops = &usb_rio_fops,
1da177e4
LT
449 .minor_base = RIO_MINOR,
450};
451
452static 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;
3864d339 457 int retval = 0;
1da177e4 458
3864d339
ON
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 }
1da177e4
LT
467
468 retval = usb_register_dev(intf, &usb_rio_class);
469 if (retval) {
c41fba13
GKH
470 dev_err(&dev->dev,
471 "Not able to get a minor for this device.\n");
3864d339
ON
472 retval = -ENOMEM;
473 goto bail_out;
1da177e4
LT
474 }
475
476 rio->rio_dev = dev;
477
0e8eb0f0 478 if (!(rio->obuf = kmalloc(OBUF_SIZE, GFP_KERNEL))) {
c41fba13
GKH
479 dev_err(&dev->dev,
480 "probe_rio: Not enough memory for the output buffer\n");
1da177e4 481 usb_deregister_dev(intf, &usb_rio_class);
3864d339
ON
482 retval = -ENOMEM;
483 goto bail_out;
1da177e4 484 }
e1a344d3 485 dev_dbg(&intf->dev, "obuf address:%p\n", rio->obuf);
1da177e4 486
0e8eb0f0 487 if (!(rio->ibuf = kmalloc(IBUF_SIZE, GFP_KERNEL))) {
c41fba13
GKH
488 dev_err(&dev->dev,
489 "probe_rio: Not enough memory for the input buffer\n");
1da177e4
LT
490 usb_deregister_dev(intf, &usb_rio_class);
491 kfree(rio->obuf);
3864d339
ON
492 retval = -ENOMEM;
493 goto bail_out;
1da177e4 494 }
e1a344d3 495 dev_dbg(&intf->dev, "ibuf address:%p\n", rio->ibuf);
1da177e4 496
1da177e4
LT
497 usb_set_intfdata (intf, rio);
498 rio->present = 1;
3864d339
ON
499bail_out:
500 mutex_unlock(&rio500_mutex);
1da177e4 501
3864d339 502 return retval;
1da177e4
LT
503}
504
505static 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);
925ce689 510 mutex_lock(&rio500_mutex);
1da177e4
LT
511 if (rio) {
512 usb_deregister_dev(intf, &usb_rio_class);
513
1da177e4
LT
514 if (rio->isopen) {
515 rio->isopen = 0;
516 /* better let it finish - the release will do whats needed */
517 rio->rio_dev = NULL;
925ce689 518 mutex_unlock(&rio500_mutex);
1da177e4
LT
519 return;
520 }
521 kfree(rio->ibuf);
522 kfree(rio->obuf);
523
1b29a375 524 dev_info(&intf->dev, "USB Rio disconnected.\n");
1da177e4
LT
525
526 rio->present = 0;
1da177e4 527 }
925ce689 528 mutex_unlock(&rio500_mutex);
1da177e4
LT
529}
530
33b9e162 531static const struct usb_device_id rio_table[] = {
1da177e4
LT
532 { USB_DEVICE(0x0841, 1) }, /* Rio 500 */
533 { } /* Terminating entry */
534};
535
536MODULE_DEVICE_TABLE (usb, rio_table);
537
538static struct usb_driver rio_driver = {
1da177e4
LT
539 .name = "rio500",
540 .probe = probe_rio,
541 .disconnect = disconnect_rio,
542 .id_table = rio_table,
543};
544
65db4305 545module_usb_driver(rio_driver);
1da177e4
LT
546
547MODULE_AUTHOR( DRIVER_AUTHOR );
548MODULE_DESCRIPTION( DRIVER_DESC );
549MODULE_LICENSE("GPL");
550