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).
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/signal.h>
30 #include <linux/sched.h>
31 #include <linux/errno.h>
32 #include <linux/miscdevice.h>
33 #include <linux/random.h>
34 #include <linux/poll.h>
35 #include <linux/init.h>
36 #include <linux/malloc.h>
37 #include <linux/spinlock.h>
38 #include <linux/usb.h>
40 #include "rio500_usb.h"
44 /* stall/wait timeout for rio */
45 #define NAK_TIMEOUT (HZ)
47 #define IBUF_SIZE 0x1000
49 /* Size of the rio buffer */
50 #define OBUF_SIZE 0x10000
53 struct usb_device
*rio_dev
; /* init: probe_rio */
54 unsigned int ifnum
; /* Interface number of the USB device */
55 int isopen
; /* nz if open */
56 int present
; /* Device is present on the bus */
57 char *obuf
, *ibuf
; /* transfer buffers */
58 char bulk_in_ep
, bulk_out_ep
; /* Endpoint assignments */
59 wait_queue_head_t wait_q
; /* for timeouts */
62 static struct rio_usb_data rio_instance
;
64 static int open_rio(struct inode
*inode
, struct file
*file
)
66 struct rio_usb_data
*rio
= &rio_instance
;
68 if (rio
->isopen
|| !rio
->present
) {
73 init_waitqueue_head(&rio
->wait_q
);
82 static int close_rio(struct inode
*inode
, struct file
*file
)
84 struct rio_usb_data
*rio
= &rio_instance
;
95 ioctl_rio(struct inode
*inode
, struct file
*file
, unsigned int cmd
,
98 struct RioCommand rio_cmd
;
99 struct rio_usb_data
*rio
= &rio_instance
;
101 unsigned char *buffer
;
102 int result
, requesttype
;
105 /* Sanity check to make sure rio is connected, powered, etc */
108 rio
->rio_dev
== NULL
)
112 case RIO_RECV_COMMAND
:
116 if (copy_from_user(&rio_cmd
, data
, sizeof(struct RioCommand
)))
118 if (rio_cmd
.length
> PAGE_SIZE
)
120 buffer
= (unsigned char *) __get_free_page(GFP_KERNEL
);
123 if (copy_from_user(buffer
, rio_cmd
.buffer
, rio_cmd
.length
))
126 requesttype
= rio_cmd
.requesttype
| USB_DIR_IN
|
127 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
;
129 ("sending command:reqtype=%0x req=%0x value=%0x index=%0x len=%0x",
130 requesttype
, rio_cmd
.request
, rio_cmd
.value
,
131 rio_cmd
.index
, rio_cmd
.length
);
132 /* Send rio control message */
135 result
= usb_control_msg(rio
->rio_dev
,
136 usb_rcvctrlpipe(rio
-> rio_dev
, 0),
140 rio_cmd
.index
, buffer
,
143 if (result
== -ETIMEDOUT
)
145 else if (result
< 0) {
146 err("Error executing ioctrl. code = %d",
147 le32_to_cpu(result
));
150 dbg("Executed ioctl. Result = %d (data=%04x)",
152 le32_to_cpu(*((long *) buffer
)));
153 if (copy_to_user(rio_cmd
.buffer
, buffer
,
159 /* rio_cmd.buffer contains a raw stream of single byte
160 data which has been returned from rio. Data is
161 interpreted at application level. For data that
162 will be cast to data types longer than 1 byte, data
163 will be little_endian and will potentially need to
164 be swapped at the app level */
167 free_page((unsigned long) buffer
);
170 case RIO_SEND_COMMAND
:
174 if (copy_from_user(&rio_cmd
, data
, sizeof(struct RioCommand
)))
176 if (rio_cmd
.length
> PAGE_SIZE
)
178 buffer
= (unsigned char *) __get_free_page(GFP_KERNEL
);
181 if (copy_from_user(buffer
, rio_cmd
.buffer
, rio_cmd
.length
))
184 requesttype
= rio_cmd
.requesttype
| USB_DIR_OUT
|
185 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
;
186 dbg("sending command: reqtype=%0x req=%0x value=%0x index=%0x len=%0x",
187 requesttype
, rio_cmd
.request
, rio_cmd
.value
,
188 rio_cmd
.index
, rio_cmd
.length
);
189 /* Send rio control message */
192 result
= usb_control_msg(rio
->rio_dev
,
193 usb_sndctrlpipe(rio
-> rio_dev
, 0),
197 rio_cmd
.index
, buffer
,
200 if (result
== -ETIMEDOUT
)
202 else if (result
< 0) {
203 err("Error executing ioctrl. code = %d",
204 le32_to_cpu(result
));
207 dbg("Executed ioctl. Result = %d",
208 le32_to_cpu(result
));
214 free_page((unsigned long) buffer
);
226 write_rio(struct file
*file
, const char *buffer
,
227 size_t count
, loff_t
* ppos
)
229 struct rio_usb_data
*rio
= &rio_instance
;
231 unsigned long copy_size
;
232 unsigned long bytes_written
= 0;
233 unsigned int partial
;
238 /* Sanity check to make sure rio is connected, powered, etc */
241 rio
->rio_dev
== NULL
)
245 unsigned long thistime
;
246 char *obuf
= rio
->obuf
;
248 thistime
= copy_size
=
249 (count
>= OBUF_SIZE
) ? OBUF_SIZE
: count
;
250 if (copy_from_user(rio
->obuf
, buffer
, copy_size
))
256 if (signal_pending(current
)) {
257 return bytes_written
? bytes_written
: -EINTR
;
260 result
= usb_bulk_msg(rio
->rio_dev
,
261 usb_sndbulkpipe(rio
->rio_dev
, 2),
262 obuf
, thistime
, &partial
, 5 * HZ
);
264 dbg("write stats: result:%d thistime:%lu partial:%u",
265 result
, thistime
, partial
);
267 if (result
== USB_ST_TIMEOUT
) { /* NAK - so hold for a while */
271 interruptible_sleep_on_timeout(&rio
-> wait_q
, NAK_TIMEOUT
);
273 } else if (!result
& partial
) {
280 err("Write Whoops - %x", result
);
283 bytes_written
+= copy_size
;
288 return bytes_written
? bytes_written
: -EIO
;
292 read_rio(struct file
*file
, char *buffer
, size_t count
, loff_t
* ppos
)
294 struct rio_usb_data
*rio
= &rio_instance
;
296 unsigned int partial
;
300 char *ibuf
= rio
->ibuf
;
302 /* Sanity check to make sure rio is connected, powered, etc */
305 rio
->rio_dev
== NULL
)
311 if (signal_pending(current
)) {
312 return read_count
? read_count
: -EINTR
;
316 this_read
= (count
>= IBUF_SIZE
) ? IBUF_SIZE
: count
;
318 result
= usb_bulk_msg(rio
->rio_dev
,
319 usb_rcvbulkpipe(rio
->rio_dev
, 1),
320 ibuf
, this_read
, &partial
,
323 dbg(KERN_DEBUG
"read stats: result:%d this_read:%u partial:%u",
324 result
, this_read
, partial
);
327 count
= this_read
= partial
;
328 } else if (result
== USB_ST_TIMEOUT
|| result
== 15) { /* FIXME: 15 ??? */
330 err("read_rio: maxretry timeout");
333 interruptible_sleep_on_timeout(&rio
->wait_q
,
336 } else if (result
!= USB_ST_DATAUNDERRUN
) {
337 err("Read Whoops - result:%u partial:%u this_read:%u",
338 result
, partial
, this_read
);
345 if (copy_to_user(buffer
, ibuf
, this_read
))
348 read_count
+= this_read
;
355 static void *probe_rio(struct usb_device
*dev
, unsigned int ifnum
)
357 struct rio_usb_data
*rio
= &rio_instance
;
359 if (dev
->descriptor
.idVendor
!= 0x841) {
363 if (dev
->descriptor
.idProduct
!= 0x1 /* RIO 500 */ ) {
364 warn(KERN_INFO
"Rio player model not supported/tested.");
368 info("USB Rio found at address %d", dev
->devnum
);
373 if (!(rio
->obuf
= (char *) kmalloc(OBUF_SIZE
, GFP_KERNEL
))) {
374 err("probe_rio: Not enough memory for the output buffer");
377 dbg("probe_rio: obuf address:%p", rio
->obuf
);
379 if (!(rio
->ibuf
= (char *) kmalloc(IBUF_SIZE
, GFP_KERNEL
))) {
380 err("probe_rio: Not enough memory for the input buffer");
384 dbg("probe_rio: ibuf address:%p", rio
->ibuf
);
389 static void disconnect_rio(struct usb_device
*dev
, void *ptr
)
391 struct rio_usb_data
*rio
= (struct rio_usb_data
*) ptr
;
395 /* better let it finish - the release will do whats needed */
402 info("USB Rio disconnected.");
408 file_operations usb_rio_fops
= {
417 usb_driver rio_driver
= {
426 int usb_rio_init(void)
428 if (usb_register(&rio_driver
) < 0)
431 info("USB Rio support registered.");
436 void usb_rio_cleanup(void)
438 struct rio_usb_data
*rio
= &rio_instance
;
441 usb_deregister(&rio_driver
);
446 module_init(usb_rio_init
);
447 module_exit(usb_rio_cleanup
);
449 MODULE_AUTHOR("Cesar Miquel <miquel@df.uba.ar>");
450 MODULE_DESCRIPTION("USB Rio 500 driver");