- pre2
[davej-history.git] / drivers / usb / rio500.c
blob59c97376e45861301bb59b703c4e88db1aabf21c
1 /* -*- linux-c -*- */
3 /*
4 * Driver for USB Rio 500
6 * Cesar Miquel (miquel@df.uba.ar)
7 *
8 * based on hp_scanner.c by David E. Nelson (dnelson@jump.net)
9 *
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).
26 * */
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"
42 #define RIO_MINOR 64
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
52 struct rio_usb_data {
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) {
69 return -EBUSY;
71 rio->isopen = 1;
73 init_waitqueue_head(&rio->wait_q);
75 MOD_INC_USE_COUNT;
77 info("Rio opened.");
79 return 0;
82 static int close_rio(struct inode *inode, struct file *file)
84 struct rio_usb_data *rio = &rio_instance;
86 rio->isopen = 0;
88 MOD_DEC_USE_COUNT;
90 info("Rio closed.");
91 return 0;
94 static int
95 ioctl_rio(struct inode *inode, struct file *file, unsigned int cmd,
96 unsigned long arg)
98 struct RioCommand rio_cmd;
99 struct rio_usb_data *rio = &rio_instance;
100 void *data;
101 unsigned char *buffer;
102 int result, requesttype;
103 int retries;
105 /* Sanity check to make sure rio is connected, powered, etc */
106 if ( rio == NULL ||
107 rio->present == 0 ||
108 rio->rio_dev == NULL )
109 return -1;
111 switch (cmd) {
112 case RIO_RECV_COMMAND:
113 data = (void *) arg;
114 if (data == NULL)
115 break;
116 if (copy_from_user(&rio_cmd, data, sizeof(struct RioCommand)))
117 return -EFAULT;
118 if (rio_cmd.length > PAGE_SIZE)
119 return -EINVAL;
120 buffer = (unsigned char *) __get_free_page(GFP_KERNEL);
121 if (buffer == NULL)
122 return -ENOMEM;
123 if (copy_from_user(buffer, rio_cmd.buffer, rio_cmd.length))
124 return -EFAULT;
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 */
133 retries = 3;
134 while (retries) {
135 result = usb_control_msg(rio->rio_dev,
136 usb_rcvctrlpipe(rio-> rio_dev, 0),
137 rio_cmd.request,
138 requesttype,
139 rio_cmd.value,
140 rio_cmd.index, buffer,
141 rio_cmd.length,
142 rio_cmd.timeout);
143 if (result == -ETIMEDOUT)
144 retries--;
145 else if (result < 0) {
146 err("Error executing ioctrl. code = %d",
147 le32_to_cpu(result));
148 retries = 0;
149 } else {
150 dbg("Executed ioctl. Result = %d (data=%04x)",
151 le32_to_cpu(result),
152 le32_to_cpu(*((long *) buffer)));
153 if (copy_to_user(rio_cmd.buffer, buffer,
154 rio_cmd.length))
155 return -EFAULT;
156 retries = 0;
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);
168 break;
170 case RIO_SEND_COMMAND:
171 data = (void *) arg;
172 if (data == NULL)
173 break;
174 if (copy_from_user(&rio_cmd, data, sizeof(struct RioCommand)))
175 return -EFAULT;
176 if (rio_cmd.length > PAGE_SIZE)
177 return -EINVAL;
178 buffer = (unsigned char *) __get_free_page(GFP_KERNEL);
179 if (buffer == NULL)
180 return -ENOMEM;
181 if (copy_from_user(buffer, rio_cmd.buffer, rio_cmd.length))
182 return -EFAULT;
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 */
190 retries = 3;
191 while (retries) {
192 result = usb_control_msg(rio->rio_dev,
193 usb_sndctrlpipe(rio-> rio_dev, 0),
194 rio_cmd.request,
195 requesttype,
196 rio_cmd.value,
197 rio_cmd.index, buffer,
198 rio_cmd.length,
199 rio_cmd.timeout);
200 if (result == -ETIMEDOUT)
201 retries--;
202 else if (result < 0) {
203 err("Error executing ioctrl. code = %d",
204 le32_to_cpu(result));
205 retries = 0;
206 } else {
207 dbg("Executed ioctl. Result = %d",
208 le32_to_cpu(result));
209 retries = 0;
214 free_page((unsigned long) buffer);
215 break;
217 default:
218 return -ENOIOCTLCMD;
219 break;
222 return 0;
225 static ssize_t
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;
235 int result = 0;
236 int maxretry;
238 /* Sanity check to make sure rio is connected, powered, etc */
239 if ( rio == NULL ||
240 rio->present == 0 ||
241 rio->rio_dev == NULL )
242 return -1;
244 do {
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))
251 return -EFAULT;
252 maxretry = 5;
253 while (thistime) {
254 if (!rio->rio_dev)
255 return -ENODEV;
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 */
268 if (!maxretry--) {
269 return -ETIME;
271 interruptible_sleep_on_timeout(&rio-> wait_q, NAK_TIMEOUT);
272 continue;
273 } else if (!result & partial) {
274 obuf += partial;
275 thistime -= partial;
276 } else
277 break;
279 if (result) {
280 err("Write Whoops - %x", result);
281 return -EIO;
283 bytes_written += copy_size;
284 count -= copy_size;
285 buffer += copy_size;
286 } while (count > 0);
288 return bytes_written ? bytes_written : -EIO;
291 static ssize_t
292 read_rio(struct file *file, char *buffer, size_t count, loff_t * ppos)
294 struct rio_usb_data *rio = &rio_instance;
295 ssize_t read_count;
296 unsigned int partial;
297 int this_read;
298 int result;
299 int maxretry = 10;
300 char *ibuf = rio->ibuf;
302 /* Sanity check to make sure rio is connected, powered, etc */
303 if ( rio == NULL ||
304 rio->present == 0 ||
305 rio->rio_dev == NULL )
306 return -1;
308 read_count = 0;
310 while (count > 0) {
311 if (signal_pending(current)) {
312 return read_count ? read_count : -EINTR;
314 if (!rio->rio_dev)
315 return -ENODEV;
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,
321 (int) (HZ * 8));
323 dbg(KERN_DEBUG "read stats: result:%d this_read:%u partial:%u",
324 result, this_read, partial);
326 if (partial) {
327 count = this_read = partial;
328 } else if (result == USB_ST_TIMEOUT || result == 15) { /* FIXME: 15 ??? */
329 if (!maxretry--) {
330 err("read_rio: maxretry timeout");
331 return -ETIME;
333 interruptible_sleep_on_timeout(&rio->wait_q,
334 NAK_TIMEOUT);
335 continue;
336 } else if (result != USB_ST_DATAUNDERRUN) {
337 err("Read Whoops - result:%u partial:%u this_read:%u",
338 result, partial, this_read);
339 return -EIO;
340 } else {
341 return (0);
344 if (this_read) {
345 if (copy_to_user(buffer, ibuf, this_read))
346 return -EFAULT;
347 count -= this_read;
348 read_count += this_read;
349 buffer += this_read;
352 return read_count;
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) {
360 return NULL;
363 if (dev->descriptor.idProduct != 0x1 /* RIO 500 */ ) {
364 warn(KERN_INFO "Rio player model not supported/tested.");
365 return NULL;
368 info("USB Rio found at address %d", dev->devnum);
370 rio->present = 1;
371 rio->rio_dev = dev;
373 if (!(rio->obuf = (char *) kmalloc(OBUF_SIZE, GFP_KERNEL))) {
374 err("probe_rio: Not enough memory for the output buffer");
375 return NULL;
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");
381 kfree(rio->obuf);
382 return NULL;
384 dbg("probe_rio: ibuf address:%p", rio->ibuf);
386 return rio;
389 static void disconnect_rio(struct usb_device *dev, void *ptr)
391 struct rio_usb_data *rio = (struct rio_usb_data *) ptr;
393 if (rio->isopen) {
394 rio->isopen = 0;
395 /* better let it finish - the release will do whats needed */
396 rio->rio_dev = NULL;
397 return;
399 kfree(rio->ibuf);
400 kfree(rio->obuf);
402 info("USB Rio disconnected.");
404 rio->present = 0;
407 static struct
408 file_operations usb_rio_fops = {
409 read: read_rio,
410 write: write_rio,
411 ioctl: ioctl_rio,
412 open: open_rio,
413 release: close_rio,
416 static struct
417 usb_driver rio_driver = {
418 "rio500",
419 probe_rio,
420 disconnect_rio,
421 {NULL, NULL},
422 &usb_rio_fops,
423 RIO_MINOR
426 int usb_rio_init(void)
428 if (usb_register(&rio_driver) < 0)
429 return -1;
431 info("USB Rio support registered.");
432 return 0;
436 void usb_rio_cleanup(void)
438 struct rio_usb_data *rio = &rio_instance;
440 rio->present = 0;
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");