1 // SPDX-License-Identifier: GPL-2.0+
5 * Driver for USB Rio 500
11 * Based upon mouse.c (Brad Keryan) and printer.c (Michael Gee).
14 * 30/05/2003 replaced lock/unlock kernel with up/down
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>
31 #include "rio500_usb.h"
34 #define DRIVER_DESC "USB Rio 500 driver"
38 /* stall/wait timeout for rio */
39 #define NAK_TIMEOUT (HZ)
41 #define IBUF_SIZE 0x1000
43 /* Size of the rio buffer */
44 #define OBUF_SIZE 0x10000
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 */
57 static DEFINE_MUTEX(rio500_mutex);
58 static struct rio_usb_data rio_instance;
60 static int open_rio(struct inode *inode, struct file *file)
62 struct rio_usb_data *rio = &rio_instance;
64 /* against disconnect() */
65 mutex_lock(&rio500_mutex);
66 mutex_lock(&(rio->lock));
68 if (rio->isopen || !rio->present) {
69 mutex_unlock(&(rio->lock));
70 mutex_unlock(&rio500_mutex);
75 init_waitqueue_head(&rio->wait_q);
77 mutex_unlock(&(rio->lock));
79 dev_info(&rio->rio_dev->dev, "Rio opened.\n");
80 mutex_unlock(&rio500_mutex);
85 static int close_rio(struct inode *inode, struct file *file)
87 struct rio_usb_data *rio = &rio_instance;
89 /* against disconnect() */
90 mutex_lock(&rio500_mutex);
91 mutex_lock(&(rio->lock));
95 /* cleanup has been delayed */
101 dev_info(&rio->rio_dev->dev, "Rio closed.\n");
103 mutex_unlock(&(rio->lock));
104 mutex_unlock(&rio500_mutex);
108 static long ioctl_rio(struct file *file, unsigned int cmd, unsigned long arg)
110 struct RioCommand rio_cmd;
111 struct rio_usb_data *rio = &rio_instance;
113 unsigned char *buffer;
114 int result, requesttype;
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) {
126 case RIO_RECV_COMMAND:
127 data = (void __user *) arg;
130 if (copy_from_user(&rio_cmd, data, sizeof(struct RioCommand))) {
134 if (rio_cmd.length < 0 || rio_cmd.length > PAGE_SIZE) {
138 buffer = (unsigned char *) __get_free_page(GFP_KERNEL);
139 if (buffer == NULL) {
143 if (copy_from_user(buffer, rio_cmd.buffer, rio_cmd.length)) {
145 free_page((unsigned long) buffer);
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 */
158 result = usb_control_msg(rio->rio_dev,
159 usb_rcvctrlpipe(rio-> rio_dev, 0),
163 rio_cmd.index, buffer,
165 jiffies_to_msecs(rio_cmd.timeout));
166 if (result == -ETIMEDOUT)
168 else if (result < 0) {
169 dev_err(&rio->rio_dev->dev,
170 "Error executing ioctrl. code = %d\n",
174 dev_dbg(&rio->rio_dev->dev,
175 "Executed ioctl. Result = %d (data=%02x)\n",
177 if (copy_to_user(rio_cmd.buffer, buffer,
179 free_page((unsigned long) buffer);
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 */
194 free_page((unsigned long) buffer);
197 case RIO_SEND_COMMAND:
198 data = (void __user *) arg;
201 if (copy_from_user(&rio_cmd, data, sizeof(struct RioCommand))) {
205 if (rio_cmd.length < 0 || rio_cmd.length > PAGE_SIZE) {
209 buffer = (unsigned char *) __get_free_page(GFP_KERNEL);
210 if (buffer == NULL) {
214 if (copy_from_user(buffer, rio_cmd.buffer, rio_cmd.length)) {
215 free_page((unsigned long)buffer);
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 */
229 result = usb_control_msg(rio->rio_dev,
230 usb_sndctrlpipe(rio-> rio_dev, 0),
234 rio_cmd.index, buffer,
236 jiffies_to_msecs(rio_cmd.timeout));
237 if (result == -ETIMEDOUT)
239 else if (result < 0) {
240 dev_err(&rio->rio_dev->dev,
241 "Error executing ioctrl. code = %d\n",
245 dev_dbg(&rio->rio_dev->dev,
246 "Executed ioctl. Result = %d\n", result);
252 free_page((unsigned long) buffer);
262 mutex_unlock(&(rio->lock));
267 write_rio(struct file *file, const char __user *buffer,
268 size_t count, loff_t * ppos)
271 struct rio_usb_data *rio = &rio_instance;
273 unsigned long copy_size;
274 unsigned long bytes_written = 0;
275 unsigned int partial;
282 intr = mutex_lock_interruptible(&(rio->lock));
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));
294 unsigned long thistime;
295 char *obuf = rio->obuf;
297 thistime = copy_size =
298 (count >= OBUF_SIZE) ? OBUF_SIZE : count;
299 if (copy_from_user(rio->obuf, buffer, copy_size)) {
309 if (signal_pending(current)) {
310 mutex_unlock(&(rio->lock));
311 return bytes_written ? bytes_written : -EINTR;
314 result = usb_bulk_msg(rio->rio_dev,
315 usb_sndbulkpipe(rio->rio_dev, 2),
316 obuf, thistime, &partial, 5000);
318 dev_dbg(&rio->rio_dev->dev,
319 "write stats: result:%d thistime:%lu partial:%u\n",
320 result, thistime, partial);
322 if (result == -ETIMEDOUT) { /* NAK - so hold for a while */
327 prepare_to_wait(&rio->wait_q, &wait, TASK_INTERRUPTIBLE);
328 schedule_timeout(NAK_TIMEOUT);
329 finish_wait(&rio->wait_q, &wait);
331 } else if (!result && partial) {
338 dev_err(&rio->rio_dev->dev, "Write Whoops - %x\n",
343 bytes_written += copy_size;
348 mutex_unlock(&(rio->lock));
350 return bytes_written ? bytes_written : -EIO;
353 mutex_unlock(&(rio->lock));
358 read_rio(struct file *file, char __user *buffer, size_t count, loff_t * ppos)
361 struct rio_usb_data *rio = &rio_instance;
363 unsigned int partial;
370 intr = mutex_lock_interruptible(&(rio->lock));
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));
385 if (signal_pending(current)) {
386 mutex_unlock(&(rio->lock));
387 return read_count ? read_count : -EINTR;
390 mutex_unlock(&(rio->lock));
393 this_read = (count >= IBUF_SIZE) ? IBUF_SIZE : count;
395 result = usb_bulk_msg(rio->rio_dev,
396 usb_rcvbulkpipe(rio->rio_dev, 1),
397 ibuf, this_read, &partial,
400 dev_dbg(&rio->rio_dev->dev,
401 "read stats: result:%d this_read:%u partial:%u\n",
402 result, this_read, partial);
405 count = this_read = partial;
406 } else if (result == -ETIMEDOUT || result == 15) { /* FIXME: 15 ??? */
408 mutex_unlock(&(rio->lock));
409 dev_err(&rio->rio_dev->dev,
410 "read_rio: maxretry timeout\n");
413 prepare_to_wait(&rio->wait_q, &wait, TASK_INTERRUPTIBLE);
414 schedule_timeout(NAK_TIMEOUT);
415 finish_wait(&rio->wait_q, &wait);
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);
424 mutex_unlock(&(rio->lock));
429 if (copy_to_user(buffer, ibuf, this_read)) {
430 mutex_unlock(&(rio->lock));
434 read_count += this_read;
438 mutex_unlock(&(rio->lock));
442 static const struct file_operations usb_rio_fops = {
443 .owner = THIS_MODULE,
446 .unlocked_ioctl = ioctl_rio,
448 .release = close_rio,
449 .llseek = noop_llseek,
452 static struct usb_class_driver usb_rio_class = {
454 .fops = &usb_rio_fops,
455 .minor_base = RIO_MINOR,
458 static int probe_rio(struct usb_interface *intf,
459 const struct usb_device_id *id)
461 struct usb_device *dev = interface_to_usbdev(intf);
462 struct rio_usb_data *rio = &rio_instance;
465 mutex_lock(&rio500_mutex);
467 dev_info(&intf->dev, "Second USB Rio at address %d refused\n", dev->devnum);
471 dev_info(&intf->dev, "USB Rio found at address %d\n", dev->devnum);
474 retval = usb_register_dev(intf, &usb_rio_class);
477 "Not able to get a minor for this device.\n");
484 if (!(rio->obuf = kmalloc(OBUF_SIZE, GFP_KERNEL))) {
486 "probe_rio: Not enough memory for the output buffer\n");
487 usb_deregister_dev(intf, &usb_rio_class);
491 dev_dbg(&intf->dev, "obuf address:%p\n", rio->obuf);
493 if (!(rio->ibuf = kmalloc(IBUF_SIZE, GFP_KERNEL))) {
495 "probe_rio: Not enough memory for the input buffer\n");
496 usb_deregister_dev(intf, &usb_rio_class);
501 dev_dbg(&intf->dev, "ibuf address:%p\n", rio->ibuf);
503 mutex_init(&(rio->lock));
505 usb_set_intfdata (intf, rio);
508 mutex_unlock(&rio500_mutex);
513 static void disconnect_rio(struct usb_interface *intf)
515 struct rio_usb_data *rio = usb_get_intfdata (intf);
517 usb_set_intfdata (intf, NULL);
518 mutex_lock(&rio500_mutex);
520 usb_deregister_dev(intf, &usb_rio_class);
522 mutex_lock(&(rio->lock));
525 /* better let it finish - the release will do whats needed */
527 mutex_unlock(&(rio->lock));
528 mutex_unlock(&rio500_mutex);
534 dev_info(&intf->dev, "USB Rio disconnected.\n");
537 mutex_unlock(&(rio->lock));
539 mutex_unlock(&rio500_mutex);
542 static const struct usb_device_id rio_table[] = {
543 { USB_DEVICE(0x0841, 1) }, /* Rio 500 */
544 { } /* Terminating entry */
547 MODULE_DEVICE_TABLE (usb, rio_table);
549 static struct usb_driver rio_driver = {
552 .disconnect = disconnect_rio,
553 .id_table = rio_table,
556 module_usb_driver(rio_driver);
558 MODULE_AUTHOR( DRIVER_AUTHOR );
559 MODULE_DESCRIPTION( DRIVER_DESC );
560 MODULE_LICENSE("GPL");