blob: 48f4b92a8f8b09a2f6b07418660ce2d7ce02e64f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*****************************************************************************/
2
3/*
4 * dabusb.c -- dab usb driver.
5 *
6 * Copyright (C) 1999 Deti Fliegl (deti@fliegl.de)
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 *
22 *
23 *
24 * $Id: dabusb.c,v 1.54 2000/07/24 21:39:39 deti Exp $
25 *
26 */
27
28/*****************************************************************************/
29
30#include <linux/module.h>
31#include <linux/socket.h>
32#include <linux/list.h>
33#include <linux/vmalloc.h>
34#include <linux/slab.h>
35#include <linux/init.h>
36#include <asm/uaccess.h>
37#include <asm/atomic.h>
38#include <linux/delay.h>
39#include <linux/usb.h>
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010040#include <linux/mutex.h>
David Woodhousec4667742008-06-23 11:41:04 +010041#include <linux/firmware.h>
42#include <linux/ihex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#include "dabusb.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46/*
47 * Version Information
48 */
49#define DRIVER_VERSION "v1.54"
50#define DRIVER_AUTHOR "Deti Fliegl, deti@fliegl.de"
51#define DRIVER_DESC "DAB-USB Interface Driver for Linux (c)1999"
52
53/* --------------------------------------------------------------------- */
54
55#ifdef CONFIG_USB_DYNAMIC_MINORS
56#define NRDABUSB 256
57#else
58#define NRDABUSB 4
59#endif
60
61/*-------------------------------------------------------------------*/
62
63static dabusb_t dabusb[NRDABUSB];
64static int buffers = 256;
65static struct usb_driver dabusb_driver;
66
67/*-------------------------------------------------------------------*/
68
69static int dabusb_add_buf_tail (pdabusb_t s, struct list_head *dst, struct list_head *src)
70{
71 unsigned long flags;
72 struct list_head *tmp;
73 int ret = 0;
74
75 spin_lock_irqsave (&s->lock, flags);
76
77 if (list_empty (src)) {
78 // no elements in source buffer
79 ret = -1;
80 goto err;
81 }
82 tmp = src->next;
83 list_move_tail (tmp, dst);
84
85 err: spin_unlock_irqrestore (&s->lock, flags);
86 return ret;
87}
88/*-------------------------------------------------------------------*/
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030089#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static void dump_urb (struct urb *urb)
91{
92 dbg("urb :%p", urb);
93 dbg("dev :%p", urb->dev);
94 dbg("pipe :%08X", urb->pipe);
95 dbg("status :%d", urb->status);
96 dbg("transfer_flags :%08X", urb->transfer_flags);
97 dbg("transfer_buffer :%p", urb->transfer_buffer);
98 dbg("transfer_buffer_length:%d", urb->transfer_buffer_length);
99 dbg("actual_length :%d", urb->actual_length);
100 dbg("setup_packet :%p", urb->setup_packet);
101 dbg("start_frame :%d", urb->start_frame);
102 dbg("number_of_packets :%d", urb->number_of_packets);
103 dbg("interval :%d", urb->interval);
104 dbg("error_count :%d", urb->error_count);
105 dbg("context :%p", urb->context);
106 dbg("complete :%p", urb->complete);
107}
108#endif
109/*-------------------------------------------------------------------*/
110static int dabusb_cancel_queue (pdabusb_t s, struct list_head *q)
111{
112 unsigned long flags;
113 pbuff_t b;
114
115 dbg("dabusb_cancel_queue");
116
117 spin_lock_irqsave (&s->lock, flags);
118
119 list_for_each_entry(b, q, buff_list) {
120#ifdef DEBUG
121 dump_urb(b->purb);
122#endif
123 usb_unlink_urb (b->purb);
124 }
125 spin_unlock_irqrestore (&s->lock, flags);
126 return 0;
127}
128/*-------------------------------------------------------------------*/
129static int dabusb_free_queue (struct list_head *q)
130{
131 struct list_head *tmp;
132 struct list_head *p;
133 pbuff_t b;
134
135 dbg("dabusb_free_queue");
136 for (p = q->next; p != q;) {
137 b = list_entry (p, buff_t, buff_list);
138
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300139#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 dump_urb(b->purb);
141#endif
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700142 kfree(b->purb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 usb_free_urb(b->purb);
144 tmp = p->next;
145 list_del (p);
146 kfree (b);
147 p = tmp;
148 }
149
150 return 0;
151}
152/*-------------------------------------------------------------------*/
153static int dabusb_free_buffers (pdabusb_t s)
154{
155 unsigned long flags;
156 dbg("dabusb_free_buffers");
157
158 spin_lock_irqsave(&s->lock, flags);
159
160 dabusb_free_queue (&s->free_buff_list);
161 dabusb_free_queue (&s->rec_buff_list);
162
163 spin_unlock_irqrestore(&s->lock, flags);
164
165 s->got_mem = 0;
166 return 0;
167}
168/*-------------------------------------------------------------------*/
David Howells7d12e782006-10-05 14:55:46 +0100169static void dabusb_iso_complete (struct urb *purb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
171 pbuff_t b = purb->context;
172 pdabusb_t s = b->s;
173 int i;
174 int len;
175 int dst = 0;
176 void *buf = purb->transfer_buffer;
177
178 dbg("dabusb_iso_complete");
179
180 // process if URB was not killed
181 if (purb->status != -ENOENT) {
182 unsigned int pipe = usb_rcvisocpipe (purb->dev, _DABUSB_ISOPIPE);
183 int pipesize = usb_maxpacket (purb->dev, pipe, usb_pipeout (pipe));
184 for (i = 0; i < purb->number_of_packets; i++)
185 if (!purb->iso_frame_desc[i].status) {
186 len = purb->iso_frame_desc[i].actual_length;
187 if (len <= pipesize) {
188 memcpy (buf + dst, buf + purb->iso_frame_desc[i].offset, len);
189 dst += len;
190 }
191 else
192 err("dabusb_iso_complete: invalid len %d", len);
193 }
194 else
195 warn("dabusb_iso_complete: corrupted packet status: %d", purb->iso_frame_desc[i].status);
196 if (dst != purb->actual_length)
197 err("dst!=purb->actual_length:%d!=%d", dst, purb->actual_length);
198 }
199
200 if (atomic_dec_and_test (&s->pending_io) && !s->remove_pending && s->state != _stopped) {
201 s->overruns++;
202 err("overrun (%d)", s->overruns);
203 }
204 wake_up (&s->wait);
205}
206/*-------------------------------------------------------------------*/
207static int dabusb_alloc_buffers (pdabusb_t s)
208{
Harvey Harrison6f289672008-04-22 14:45:32 -0300209 int transfer_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 pbuff_t b;
211 unsigned int pipe = usb_rcvisocpipe (s->usbdev, _DABUSB_ISOPIPE);
212 int pipesize = usb_maxpacket (s->usbdev, pipe, usb_pipeout (pipe));
213 int packets = _ISOPIPESIZE / pipesize;
214 int transfer_buffer_length = packets * pipesize;
215 int i;
216
217 dbg("dabusb_alloc_buffers pipesize:%d packets:%d transfer_buffer_len:%d",
218 pipesize, packets, transfer_buffer_length);
219
Harvey Harrison6f289672008-04-22 14:45:32 -0300220 while (transfer_len < (s->total_buffer_size << 10)) {
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800221 b = kzalloc(sizeof (buff_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 if (!b) {
Oliver Neukumd8e298d2006-01-06 21:01:47 +0100223 err("kzalloc(sizeof(buff_t))==NULL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 goto err;
225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 b->s = s;
227 b->purb = usb_alloc_urb(packets, GFP_KERNEL);
228 if (!b->purb) {
229 err("usb_alloc_urb == NULL");
230 kfree (b);
231 goto err;
232 }
233
234 b->purb->transfer_buffer = kmalloc (transfer_buffer_length, GFP_KERNEL);
235 if (!b->purb->transfer_buffer) {
236 kfree (b->purb);
237 kfree (b);
238 err("kmalloc(%d)==NULL", transfer_buffer_length);
239 goto err;
240 }
241
242 b->purb->transfer_buffer_length = transfer_buffer_length;
243 b->purb->number_of_packets = packets;
244 b->purb->complete = dabusb_iso_complete;
245 b->purb->context = b;
246 b->purb->dev = s->usbdev;
247 b->purb->pipe = pipe;
248 b->purb->transfer_flags = URB_ISO_ASAP;
249
250 for (i = 0; i < packets; i++) {
251 b->purb->iso_frame_desc[i].offset = i * pipesize;
252 b->purb->iso_frame_desc[i].length = pipesize;
253 }
254
Harvey Harrison6f289672008-04-22 14:45:32 -0300255 transfer_len += transfer_buffer_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 list_add_tail (&b->buff_list, &s->free_buff_list);
257 }
Harvey Harrison6f289672008-04-22 14:45:32 -0300258 s->got_mem = transfer_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 return 0;
261
262 err:
263 dabusb_free_buffers (s);
264 return -ENOMEM;
265}
266/*-------------------------------------------------------------------*/
267static int dabusb_bulk (pdabusb_t s, pbulk_transfer_t pb)
268{
269 int ret;
270 unsigned int pipe;
271 int actual_length;
272
273 dbg("dabusb_bulk");
274
275 if (!pb->pipe)
276 pipe = usb_rcvbulkpipe (s->usbdev, 2);
277 else
278 pipe = usb_sndbulkpipe (s->usbdev, 2);
279
280 ret=usb_bulk_msg(s->usbdev, pipe, pb->data, pb->size, &actual_length, 100);
281 if(ret<0) {
282 err("dabusb: usb_bulk_msg failed(%d)",ret);
283
284 if (usb_set_interface (s->usbdev, _DABUSB_IF, 1) < 0) {
285 err("set_interface failed");
286 return -EINVAL;
287 }
288
289 }
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if( ret == -EPIPE ) {
292 warn("CLEAR_FEATURE request to remove STALL condition.");
293 if(usb_clear_halt(s->usbdev, usb_pipeendpoint(pipe)))
294 err("request failed");
295 }
296
297 pb->size = actual_length;
298 return ret;
299}
300/* --------------------------------------------------------------------- */
David Woodhousec4667742008-06-23 11:41:04 +0100301static int dabusb_writemem (pdabusb_t s, int pos, const unsigned char *data,
302 int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
304 int ret;
305 unsigned char *transfer_buffer = kmalloc (len, GFP_KERNEL);
306
307 if (!transfer_buffer) {
308 err("dabusb_writemem: kmalloc(%d) failed.", len);
309 return -ENOMEM;
310 }
311
312 memcpy (transfer_buffer, data, len);
313
314 ret=usb_control_msg(s->usbdev, usb_sndctrlpipe( s->usbdev, 0 ), 0xa0, 0x40, pos, 0, transfer_buffer, len, 300);
315
316 kfree (transfer_buffer);
317 return ret;
318}
319/* --------------------------------------------------------------------- */
320static int dabusb_8051_reset (pdabusb_t s, unsigned char reset_bit)
321{
322 dbg("dabusb_8051_reset: %d",reset_bit);
323 return dabusb_writemem (s, CPUCS_REG, &reset_bit, 1);
324}
325/* --------------------------------------------------------------------- */
326static int dabusb_loadmem (pdabusb_t s, const char *fname)
327{
328 int ret;
David Woodhousec4667742008-06-23 11:41:04 +0100329 const struct ihex_binrec *rec;
330 const struct firmware *fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 dbg("Enter dabusb_loadmem (internal)");
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300333
David Woodhousec4667742008-06-23 11:41:04 +0100334 ret = request_ihex_firmware(&fw, "dabusb/firmware.fw", &s->usbdev->dev);
335 if (ret) {
336 err("Failed to load \"dabusb/firmware.fw\": %d\n", ret);
337 goto out;
338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 ret = dabusb_8051_reset (s, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
David Woodhousec4667742008-06-23 11:41:04 +0100341 for (rec = (const struct ihex_binrec *)fw->data; rec;
342 rec = ihex_next_binrec(rec)) {
343 dbg("dabusb_writemem: %04X %p %d)", be32_to_cpu(rec->addr),
344 rec->data, be16_to_cpu(rec->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
David Woodhousec4667742008-06-23 11:41:04 +0100346 ret = dabusb_writemem(s, be32_to_cpu(rec->addr), rec->data,
347 be16_to_cpu(rec->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (ret < 0) {
David Woodhousec4667742008-06-23 11:41:04 +0100349 err("dabusb_writemem failed (%d %04X %p %d)", ret,
350 be32_to_cpu(rec->addr), rec->data,
351 be16_to_cpu(rec->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 break;
353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
355 ret = dabusb_8051_reset (s, 0);
David Woodhousec4667742008-06-23 11:41:04 +0100356 release_firmware(fw);
357 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 dbg("dabusb_loadmem: exit");
359
360 return ret;
361}
362/* --------------------------------------------------------------------- */
363static int dabusb_fpga_clear (pdabusb_t s, pbulk_transfer_t b)
364{
365 b->size = 4;
366 b->data[0] = 0x2a;
367 b->data[1] = 0;
368 b->data[2] = 0;
369 b->data[3] = 0;
370
371 dbg("dabusb_fpga_clear");
372
373 return dabusb_bulk (s, b);
374}
375/* --------------------------------------------------------------------- */
376static int dabusb_fpga_init (pdabusb_t s, pbulk_transfer_t b)
377{
378 b->size = 4;
379 b->data[0] = 0x2c;
380 b->data[1] = 0;
381 b->data[2] = 0;
382 b->data[3] = 0;
383
384 dbg("dabusb_fpga_init");
385
386 return dabusb_bulk (s, b);
387}
388/* --------------------------------------------------------------------- */
389static int dabusb_fpga_download (pdabusb_t s, const char *fname)
390{
391 pbulk_transfer_t b = kmalloc (sizeof (bulk_transfer_t), GFP_KERNEL);
David Woodhousec4667742008-06-23 11:41:04 +0100392 const struct firmware *fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 unsigned int blen, n;
394 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 dbg("Enter dabusb_fpga_download (internal)");
397
398 if (!b) {
399 err("kmalloc(sizeof(bulk_transfer_t))==NULL");
400 return -ENOMEM;
401 }
402
David Woodhousec4667742008-06-23 11:41:04 +0100403 ret = request_firmware(&fw, "dabusb/bitstream.bin", &s->usbdev->dev);
404 if (ret) {
405 err("Failed to load \"dabusb/bitstream.bin\": %d\n", ret);
406 return ret;
407 }
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 b->pipe = 1;
410 ret = dabusb_fpga_clear (s, b);
411 mdelay (10);
David Woodhousec4667742008-06-23 11:41:04 +0100412 blen = fw->data[73] + (fw->data[72] << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 dbg("Bitstream len: %i", blen);
415
416 b->data[0] = 0x2b;
417 b->data[1] = 0;
418 b->data[2] = 0;
419 b->data[3] = 60;
420
421 for (n = 0; n <= blen + 60; n += 60) {
422 // some cclks for startup
423 b->size = 64;
David Woodhousec4667742008-06-23 11:41:04 +0100424 memcpy (b->data + 4, fw->data + 74 + n, 60);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 ret = dabusb_bulk (s, b);
426 if (ret < 0) {
427 err("dabusb_bulk failed.");
428 break;
429 }
430 mdelay (1);
431 }
432
433 ret = dabusb_fpga_init (s, b);
434 kfree (b);
David Woodhousec4667742008-06-23 11:41:04 +0100435 release_firmware(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 dbg("exit dabusb_fpga_download");
438
439 return ret;
440}
441
442static int dabusb_stop (pdabusb_t s)
443{
444 dbg("dabusb_stop");
445
446 s->state = _stopped;
447 dabusb_cancel_queue (s, &s->rec_buff_list);
448
449 dbg("pending_io: %d", s->pending_io.counter);
450
451 s->pending_io.counter = 0;
452 return 0;
453}
454
455static int dabusb_startrek (pdabusb_t s)
456{
457 if (!s->got_mem && s->state != _started) {
458
459 dbg("dabusb_startrek");
460
461 if (dabusb_alloc_buffers (s) < 0)
462 return -ENOMEM;
463 dabusb_stop (s);
464 s->state = _started;
465 s->readptr = 0;
466 }
467
468 if (!list_empty (&s->free_buff_list)) {
469 pbuff_t end;
470 int ret;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 while (!dabusb_add_buf_tail (s, &s->rec_buff_list, &s->free_buff_list)) {
473
474 dbg("submitting: end:%p s->rec_buff_list:%p", s->rec_buff_list.prev, &s->rec_buff_list);
475
476 end = list_entry (s->rec_buff_list.prev, buff_t, buff_list);
477
478 ret = usb_submit_urb (end->purb, GFP_KERNEL);
479 if (ret) {
480 err("usb_submit_urb returned:%d", ret);
481 if (dabusb_add_buf_tail (s, &s->free_buff_list, &s->rec_buff_list))
482 err("startrek: dabusb_add_buf_tail failed");
483 break;
484 }
485 else
486 atomic_inc (&s->pending_io);
487 }
488 dbg("pending_io: %d",s->pending_io.counter);
489 }
490
491 return 0;
492}
493
494static ssize_t dabusb_read (struct file *file, char __user *buf, size_t count, loff_t * ppos)
495{
496 pdabusb_t s = (pdabusb_t) file->private_data;
497 unsigned long flags;
498 unsigned ret = 0;
499 int rem;
500 int cnt;
501 pbuff_t b;
502 struct urb *purb = NULL;
503
504 dbg("dabusb_read");
505
506 if (*ppos)
507 return -ESPIPE;
508
509 if (s->remove_pending)
510 return -EIO;
511
512
513 if (!s->usbdev)
514 return -EIO;
515
516 while (count > 0) {
517 dabusb_startrek (s);
518
519 spin_lock_irqsave (&s->lock, flags);
520
521 if (list_empty (&s->rec_buff_list)) {
522
523 spin_unlock_irqrestore(&s->lock, flags);
524
525 err("error: rec_buf_list is empty");
526 goto err;
527 }
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 b = list_entry (s->rec_buff_list.next, buff_t, buff_list);
530 purb = b->purb;
531
532 spin_unlock_irqrestore(&s->lock, flags);
533
534 if (purb->status == -EINPROGRESS) {
535 if (file->f_flags & O_NONBLOCK) // return nonblocking
536 {
537 if (!ret)
538 ret = -EAGAIN;
539 goto err;
540 }
541
542 interruptible_sleep_on (&s->wait);
543
544 if (signal_pending (current)) {
545 if (!ret)
546 ret = -ERESTARTSYS;
547 goto err;
548 }
549
550 spin_lock_irqsave (&s->lock, flags);
551
552 if (list_empty (&s->rec_buff_list)) {
553 spin_unlock_irqrestore(&s->lock, flags);
554 err("error: still no buffer available.");
555 goto err;
556 }
557 spin_unlock_irqrestore(&s->lock, flags);
558 s->readptr = 0;
559 }
560 if (s->remove_pending) {
561 ret = -EIO;
562 goto err;
563 }
564
565 rem = purb->actual_length - s->readptr; // set remaining bytes to copy
566
567 if (count >= rem)
568 cnt = rem;
569 else
570 cnt = count;
571
572 dbg("copy_to_user:%p %p %d",buf, purb->transfer_buffer + s->readptr, cnt);
573
574 if (copy_to_user (buf, purb->transfer_buffer + s->readptr, cnt)) {
575 err("read: copy_to_user failed");
576 if (!ret)
577 ret = -EFAULT;
578 goto err;
579 }
580
581 s->readptr += cnt;
582 count -= cnt;
583 buf += cnt;
584 ret += cnt;
585
586 if (s->readptr == purb->actual_length) {
587 // finished, take next buffer
588 if (dabusb_add_buf_tail (s, &s->free_buff_list, &s->rec_buff_list))
589 err("read: dabusb_add_buf_tail failed");
590 s->readptr = 0;
591 }
592 }
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100593 err: //mutex_unlock(&s->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return ret;
595}
596
597static int dabusb_open (struct inode *inode, struct file *file)
598{
599 int devnum = iminor(inode);
600 pdabusb_t s;
601
602 if (devnum < DABUSB_MINOR || devnum >= (DABUSB_MINOR + NRDABUSB))
603 return -EIO;
604
605 s = &dabusb[devnum - DABUSB_MINOR];
606
607 dbg("dabusb_open");
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100608 mutex_lock(&s->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 while (!s->usbdev || s->opened) {
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100611 mutex_unlock(&s->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 if (file->f_flags & O_NONBLOCK) {
614 return -EBUSY;
615 }
616 msleep_interruptible(500);
617
618 if (signal_pending (current)) {
619 return -EAGAIN;
620 }
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100621 mutex_lock(&s->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
623 if (usb_set_interface (s->usbdev, _DABUSB_IF, 1) < 0) {
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100624 mutex_unlock(&s->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 err("set_interface failed");
626 return -EINVAL;
627 }
628 s->opened = 1;
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100629 mutex_unlock(&s->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 file->f_pos = 0;
632 file->private_data = s;
633
634 return nonseekable_open(inode, file);
635}
636
637static int dabusb_release (struct inode *inode, struct file *file)
638{
639 pdabusb_t s = (pdabusb_t) file->private_data;
640
641 dbg("dabusb_release");
642
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100643 mutex_lock(&s->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 dabusb_stop (s);
645 dabusb_free_buffers (s);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100646 mutex_unlock(&s->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 if (!s->remove_pending) {
649 if (usb_set_interface (s->usbdev, _DABUSB_IF, 0) < 0)
650 err("set_interface failed");
651 }
652 else
653 wake_up (&s->remove_ok);
654
655 s->opened = 0;
656 return 0;
657}
658
659static int dabusb_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
660{
661 pdabusb_t s = (pdabusb_t) file->private_data;
662 pbulk_transfer_t pbulk;
663 int ret = 0;
664 int version = DABUSB_VERSION;
665
666 dbg("dabusb_ioctl");
667
668 if (s->remove_pending)
669 return -EIO;
670
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100671 mutex_lock(&s->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 if (!s->usbdev) {
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100674 mutex_unlock(&s->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 return -EIO;
676 }
677
678 switch (cmd) {
679
680 case IOCTL_DAB_BULK:
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800681 pbulk = kmalloc(sizeof (bulk_transfer_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 if (!pbulk) {
684 ret = -ENOMEM;
685 break;
686 }
687
688 if (copy_from_user (pbulk, (void __user *) arg, sizeof (bulk_transfer_t))) {
689 ret = -EFAULT;
690 kfree (pbulk);
691 break;
692 }
693
694 ret=dabusb_bulk (s, pbulk);
695 if(ret==0)
696 if (copy_to_user((void __user *)arg, pbulk,
697 sizeof(bulk_transfer_t)))
698 ret = -EFAULT;
699 kfree (pbulk);
700 break;
701
702 case IOCTL_DAB_OVERRUNS:
703 ret = put_user (s->overruns, (unsigned int __user *) arg);
704 break;
705
706 case IOCTL_DAB_VERSION:
707 ret = put_user (version, (unsigned int __user *) arg);
708 break;
709
710 default:
711 ret = -ENOIOCTLCMD;
712 break;
713 }
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100714 mutex_unlock(&s->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return ret;
716}
717
Arjan van de Venfa027c22007-02-12 00:55:33 -0800718static const struct file_operations dabusb_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
720 .owner = THIS_MODULE,
721 .llseek = no_llseek,
722 .read = dabusb_read,
723 .ioctl = dabusb_ioctl,
724 .open = dabusb_open,
725 .release = dabusb_release,
726};
727
728static struct usb_class_driver dabusb_class = {
Greg Kroah-Hartmand6e5bcf2005-06-20 21:15:16 -0700729 .name = "dabusb%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 .fops = &dabusb_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 .minor_base = DABUSB_MINOR,
732};
733
734
735/* --------------------------------------------------------------------- */
736static int dabusb_probe (struct usb_interface *intf,
737 const struct usb_device_id *id)
738{
739 struct usb_device *usbdev = interface_to_usbdev(intf);
740 int retval;
741 pdabusb_t s;
742
743 dbg("dabusb: probe: vendor id 0x%x, device id 0x%x ifnum:%d",
744 le16_to_cpu(usbdev->descriptor.idVendor),
745 le16_to_cpu(usbdev->descriptor.idProduct),
746 intf->altsetting->desc.bInterfaceNumber);
747
748 /* We don't handle multiple configurations */
749 if (usbdev->descriptor.bNumConfigurations != 1)
750 return -ENODEV;
751
752 if (intf->altsetting->desc.bInterfaceNumber != _DABUSB_IF &&
753 le16_to_cpu(usbdev->descriptor.idProduct) == 0x9999)
754 return -ENODEV;
755
756
757
758 s = &dabusb[intf->minor];
759
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100760 mutex_lock(&s->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 s->remove_pending = 0;
762 s->usbdev = usbdev;
763 s->devnum = intf->minor;
764
765 if (usb_reset_configuration (usbdev) < 0) {
766 err("reset_configuration failed");
767 goto reject;
768 }
769 if (le16_to_cpu(usbdev->descriptor.idProduct) == 0x2131) {
770 dabusb_loadmem (s, NULL);
771 goto reject;
772 }
773 else {
774 dabusb_fpga_download (s, NULL);
775
776 if (usb_set_interface (s->usbdev, _DABUSB_IF, 0) < 0) {
777 err("set_interface failed");
778 goto reject;
779 }
780 }
781 dbg("bound to interface: %d", intf->altsetting->desc.bInterfaceNumber);
782 usb_set_intfdata (intf, s);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100783 mutex_unlock(&s->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 retval = usb_register_dev(intf, &dabusb_class);
786 if (retval) {
787 usb_set_intfdata (intf, NULL);
788 return -ENOMEM;
789 }
790
791 return 0;
792
793 reject:
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100794 mutex_unlock(&s->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 s->usbdev = NULL;
796 return -ENODEV;
797}
798
799static void dabusb_disconnect (struct usb_interface *intf)
800{
801 wait_queue_t __wait;
802 pdabusb_t s = usb_get_intfdata (intf);
803
804 dbg("dabusb_disconnect");
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 init_waitqueue_entry(&__wait, current);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 usb_set_intfdata (intf, NULL);
809 if (s) {
810 usb_deregister_dev (intf, &dabusb_class);
811 s->remove_pending = 1;
812 wake_up (&s->wait);
813 add_wait_queue(&s->remove_ok, &__wait);
814 set_current_state(TASK_UNINTERRUPTIBLE);
815 if (s->state == _started)
816 schedule();
817 current->state = TASK_RUNNING;
818 remove_wait_queue(&s->remove_ok, &__wait);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 s->usbdev = NULL;
821 s->overruns = 0;
822 }
823}
824
825static struct usb_device_id dabusb_ids [] = {
826 // { USB_DEVICE(0x0547, 0x2131) }, /* An2131 chip, no boot ROM */
827 { USB_DEVICE(0x0547, 0x9999) },
828 { } /* Terminating entry */
829};
830
831MODULE_DEVICE_TABLE (usb, dabusb_ids);
832
833static struct usb_driver dabusb_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 .name = "dabusb",
835 .probe = dabusb_probe,
836 .disconnect = dabusb_disconnect,
837 .id_table = dabusb_ids,
838};
839
840/* --------------------------------------------------------------------- */
841
842static int __init dabusb_init (void)
843{
844 int retval;
845 unsigned u;
846
847 /* initialize struct */
848 for (u = 0; u < NRDABUSB; u++) {
849 pdabusb_t s = &dabusb[u];
850 memset (s, 0, sizeof (dabusb_t));
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100851 mutex_init (&s->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 s->usbdev = NULL;
853 s->total_buffer_size = buffers;
854 init_waitqueue_head (&s->wait);
855 init_waitqueue_head (&s->remove_ok);
856 spin_lock_init (&s->lock);
857 INIT_LIST_HEAD (&s->free_buff_list);
858 INIT_LIST_HEAD (&s->rec_buff_list);
859 }
860
861 /* register misc device */
862 retval = usb_register(&dabusb_driver);
863 if (retval)
864 goto out;
865
866 dbg("dabusb_init: driver registered");
867
868 info(DRIVER_VERSION ":" DRIVER_DESC);
869
870out:
871 return retval;
872}
873
874static void __exit dabusb_cleanup (void)
875{
876 dbg("dabusb_cleanup");
877
878 usb_deregister (&dabusb_driver);
879}
880
881/* --------------------------------------------------------------------- */
882
883MODULE_AUTHOR( DRIVER_AUTHOR );
884MODULE_DESCRIPTION( DRIVER_DESC );
885MODULE_LICENSE("GPL");
886
887module_param(buffers, int, 0);
888MODULE_PARM_DESC (buffers, "Number of buffers (default=256)");
889
890module_init (dabusb_init);
891module_exit (dabusb_cleanup);
892
893/* --------------------------------------------------------------------- */