4 * Driver for USB Rio 500
6 * Cesar Miquel (miquel@df.uba.ar)
8 * based on hp_scanner.c by David E. Nelson (dnelson@jump.net)
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of the
13 * License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 * Based upon mouse.c (Brad Keryan) and printer.c (Michael Gee).
28 #include <linux/module.h>
29 #include <linux/kernel.h>
30 #include <linux/signal.h>
31 #include <linux/sched.h>
32 #include <linux/errno.h>
33 #include <linux/miscdevice.h>
34 #include <linux/random.h>
35 #include <linux/poll.h>
36 #include <linux/init.h>
37 #include <linux/slab.h>
38 #include <linux/spinlock.h>
39 #include <linux/usb.h>
40 #include <linux/smp_lock.h>
41 #include <linux/devfs_fs_kernel.h>
43 #include "rio500_usb.h"
48 #define DRIVER_VERSION "v1.1"
49 #define DRIVER_AUTHOR "Cesar Miquel <miquel@df.uba.ar>"
50 #define DRIVER_DESC "USB Rio 500 driver"
54 /* stall/wait timeout for rio */
55 #define NAK_TIMEOUT (HZ)
57 #define IBUF_SIZE 0x1000
59 /* Size of the rio buffer */
60 #define OBUF_SIZE 0x10000
63 struct usb_device *rio_dev; /* init: probe_rio */
64 devfs_handle_t devfs; /* devfs device */
65 unsigned int ifnum; /* Interface number of the USB device */
66 int isopen; /* nz if open */
67 int present; /* Device is present on the bus */
68 char *obuf, *ibuf; /* transfer buffers */
69 char bulk_in_ep, bulk_out_ep; /* Endpoint assignments */
70 wait_queue_head_t wait_q; /* for timeouts */
71 struct semaphore lock; /* general race avoidance */
74 extern devfs_handle_t usb_devfs_handle; /* /dev/usb dir. */
76 static struct rio_usb_data rio_instance;
78 static int open_rio(struct inode *inode, struct file *file)
80 struct rio_usb_data *rio = &rio_instance;
84 if (rio->isopen || !rio->present) {
90 init_waitqueue_head(&rio->wait_q);
101 static int close_rio(struct inode *inode, struct file *file)
103 struct rio_usb_data *rio = &rio_instance;
114 ioctl_rio(struct inode *inode, struct file *file, unsigned int cmd,
117 struct RioCommand rio_cmd;
118 struct rio_usb_data *rio = &rio_instance;
120 unsigned char *buffer;
121 int result, requesttype;
126 /* Sanity check to make sure rio is connected, powered, etc */
129 rio->rio_dev == NULL )
136 case RIO_RECV_COMMAND:
140 if (copy_from_user(&rio_cmd, data, sizeof(struct RioCommand))) {
144 if (rio_cmd.length < 0 || rio_cmd.length > PAGE_SIZE) {
148 buffer = (unsigned char *) __get_free_page(GFP_KERNEL);
149 if (buffer == NULL) {
153 if (copy_from_user(buffer, rio_cmd.buffer, rio_cmd.length)) {
155 free_page((unsigned long) buffer);
159 requesttype = rio_cmd.requesttype | USB_DIR_IN |
160 USB_TYPE_VENDOR | USB_RECIP_DEVICE;
162 ("sending command:reqtype=%0x req=%0x value=%0x index=%0x len=%0x",
163 requesttype, rio_cmd.request, rio_cmd.value,
164 rio_cmd.index, rio_cmd.length);
165 /* Send rio control message */
168 result = usb_control_msg(rio->rio_dev,
169 usb_rcvctrlpipe(rio-> rio_dev, 0),
173 rio_cmd.index, buffer,
176 if (result == -ETIMEDOUT)
178 else if (result < 0) {
179 err("Error executing ioctrl. code = %d",
180 le32_to_cpu(result));
183 dbg("Executed ioctl. Result = %d (data=%04x)",
185 le32_to_cpu(*((long *) buffer)));
186 if (copy_to_user(rio_cmd.buffer, buffer,
188 free_page((unsigned long) buffer);
195 /* rio_cmd.buffer contains a raw stream of single byte
196 data which has been returned from rio. Data is
197 interpreted at application level. For data that
198 will be cast to data types longer than 1 byte, data
199 will be little_endian and will potentially need to
200 be swapped at the app level */
203 free_page((unsigned long) buffer);
206 case RIO_SEND_COMMAND:
210 if (copy_from_user(&rio_cmd, data, sizeof(struct RioCommand))) {
214 if (rio_cmd.length < 0 || rio_cmd.length > PAGE_SIZE) {
218 buffer = (unsigned char *) __get_free_page(GFP_KERNEL);
219 if (buffer == NULL) {
223 if (copy_from_user(buffer, rio_cmd.buffer, rio_cmd.length)) {
224 free_page((unsigned long)buffer);
229 requesttype = rio_cmd.requesttype | USB_DIR_OUT |
230 USB_TYPE_VENDOR | USB_RECIP_DEVICE;
231 dbg("sending command: reqtype=%0x req=%0x value=%0x index=%0x len=%0x",
232 requesttype, rio_cmd.request, rio_cmd.value,
233 rio_cmd.index, rio_cmd.length);
234 /* Send rio control message */
237 result = usb_control_msg(rio->rio_dev,
238 usb_sndctrlpipe(rio-> rio_dev, 0),
242 rio_cmd.index, buffer,
245 if (result == -ETIMEDOUT)
247 else if (result < 0) {
248 err("Error executing ioctrl. code = %d",
249 le32_to_cpu(result));
252 dbg("Executed ioctl. Result = %d",
253 le32_to_cpu(result));
259 free_page((unsigned long) buffer);
274 write_rio(struct file *file, const char *buffer,
275 size_t count, loff_t * ppos)
277 struct rio_usb_data *rio = &rio_instance;
279 unsigned long copy_size;
280 unsigned long bytes_written = 0;
281 unsigned int partial;
288 /* Sanity check to make sure rio is connected, powered, etc */
291 rio->rio_dev == NULL )
300 unsigned long thistime;
301 char *obuf = rio->obuf;
303 thistime = copy_size =
304 (count >= OBUF_SIZE) ? OBUF_SIZE : count;
305 if (copy_from_user(rio->obuf, buffer, copy_size)) {
315 if (signal_pending(current)) {
317 return bytes_written ? bytes_written : -EINTR;
320 result = usb_bulk_msg(rio->rio_dev,
321 usb_sndbulkpipe(rio->rio_dev, 2),
322 obuf, thistime, &partial, 5 * HZ);
324 dbg("write stats: result:%d thistime:%lu partial:%u",
325 result, thistime, partial);
327 if (result == USB_ST_TIMEOUT) { /* NAK - so hold for a while */
332 interruptible_sleep_on_timeout(&rio-> wait_q, NAK_TIMEOUT);
334 } else if (!result & partial) {
341 err("Write Whoops - %x", result);
345 bytes_written += copy_size;
352 return bytes_written ? bytes_written : -EIO;
360 read_rio(struct file *file, char *buffer, size_t count, loff_t * ppos)
362 struct rio_usb_data *rio = &rio_instance;
364 unsigned int partial;
371 /* Sanity check to make sure rio is connected, powered, etc */
374 rio->rio_dev == NULL )
386 if (signal_pending(current)) {
388 return read_count ? read_count : -EINTR;
394 this_read = (count >= IBUF_SIZE) ? IBUF_SIZE : count;
396 result = usb_bulk_msg(rio->rio_dev,
397 usb_rcvbulkpipe(rio->rio_dev, 1),
398 ibuf, this_read, &partial,
401 dbg(KERN_DEBUG "read stats: result:%d this_read:%u partial:%u",
402 result, this_read, partial);
405 count = this_read = partial;
406 } else if (result == USB_ST_TIMEOUT || result == 15) { /* FIXME: 15 ??? */
409 err("read_rio: maxretry timeout");
412 interruptible_sleep_on_timeout(&rio->wait_q,
415 } else if (result != USB_ST_DATAUNDERRUN) {
417 err("Read Whoops - result:%u partial:%u this_read:%u",
418 result, partial, this_read);
426 if (copy_to_user(buffer, ibuf, this_read)) {
431 read_count += this_read;
440 file_operations usb_rio_fops = {
448 static void *probe_rio(struct usb_device *dev, unsigned int ifnum,
449 const struct usb_device_id *id)
451 struct rio_usb_data *rio = &rio_instance;
453 info("USB Rio found at address %d", dev->devnum);
458 if (!(rio->obuf = (char *) kmalloc(OBUF_SIZE, GFP_KERNEL))) {
459 err("probe_rio: Not enough memory for the output buffer");
462 dbg("probe_rio: obuf address:%p", rio->obuf);
464 if (!(rio->ibuf = (char *) kmalloc(IBUF_SIZE, GFP_KERNEL))) {
465 err("probe_rio: Not enough memory for the input buffer");
469 dbg("probe_rio: ibuf address:%p", rio->ibuf);
471 rio->devfs = devfs_register(usb_devfs_handle, "rio500",
472 DEVFS_FL_DEFAULT, USB_MAJOR,
474 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP |
475 S_IWGRP, &usb_rio_fops, NULL);
476 if (rio->devfs == NULL)
477 dbg("probe_rio: device node registration failed");
479 init_MUTEX(&(rio->lock));
484 static void disconnect_rio(struct usb_device *dev, void *ptr)
486 struct rio_usb_data *rio = (struct rio_usb_data *) ptr;
488 devfs_unregister(rio->devfs);
493 /* better let it finish - the release will do whats needed */
501 info("USB Rio disconnected.");
507 static struct usb_device_id rio_table [] = {
508 { USB_DEVICE(0x0841, 1) }, /* Rio 500 */
509 { } /* Terminating entry */
512 MODULE_DEVICE_TABLE (usb, rio_table);
514 static struct usb_driver rio_driver = {
517 disconnect: disconnect_rio,
523 int usb_rio_init(void)
525 if (usb_register(&rio_driver) < 0)
528 info(DRIVER_VERSION ":" DRIVER_DESC);
534 void usb_rio_cleanup(void)
536 struct rio_usb_data *rio = &rio_instance;
539 usb_deregister(&rio_driver);
544 module_init(usb_rio_init);
545 module_exit(usb_rio_cleanup);
547 MODULE_AUTHOR( DRIVER_AUTHOR );
548 MODULE_DESCRIPTION( DRIVER_DESC );
549 MODULE_LICENSE("GPL");