[ARM] pxa: Gumstix Verdex PCMCIA support
[linux-2.6/verdex.git] / drivers / media / video / dabusb.c
blobee43876adb065bde22517eecf6f0968140b7906d
1 /*****************************************************************************/
3 /*
4 * dabusb.c -- dab usb driver.
6 * Copyright (C) 1999 Deti Fliegl (deti@fliegl.de)
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.
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.
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.
24 * $Id: dabusb.c,v 1.54 2000/07/24 21:39:39 deti Exp $
28 /*****************************************************************************/
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/smp_lock.h>
36 #include <linux/init.h>
37 #include <asm/uaccess.h>
38 #include <asm/atomic.h>
39 #include <linux/delay.h>
40 #include <linux/usb.h>
41 #include <linux/mutex.h>
42 #include <linux/firmware.h>
43 #include <linux/ihex.h>
45 #include "dabusb.h"
48 * Version Information
50 #define DRIVER_VERSION "v1.54"
51 #define DRIVER_AUTHOR "Deti Fliegl, deti@fliegl.de"
52 #define DRIVER_DESC "DAB-USB Interface Driver for Linux (c)1999"
54 /* --------------------------------------------------------------------- */
56 #ifdef CONFIG_USB_DYNAMIC_MINORS
57 #define NRDABUSB 256
58 #else
59 #define NRDABUSB 4
60 #endif
62 /*-------------------------------------------------------------------*/
64 static dabusb_t dabusb[NRDABUSB];
65 static int buffers = 256;
66 static struct usb_driver dabusb_driver;
68 /*-------------------------------------------------------------------*/
70 static int dabusb_add_buf_tail (pdabusb_t s, struct list_head *dst, struct list_head *src)
72 unsigned long flags;
73 struct list_head *tmp;
74 int ret = 0;
76 spin_lock_irqsave (&s->lock, flags);
78 if (list_empty (src)) {
79 // no elements in source buffer
80 ret = -1;
81 goto err;
83 tmp = src->next;
84 list_move_tail (tmp, dst);
86 err: spin_unlock_irqrestore (&s->lock, flags);
87 return ret;
89 /*-------------------------------------------------------------------*/
90 #ifdef DEBUG
91 static void dump_urb (struct urb *urb)
93 dbg("urb :%p", urb);
94 dbg("dev :%p", urb->dev);
95 dbg("pipe :%08X", urb->pipe);
96 dbg("status :%d", urb->status);
97 dbg("transfer_flags :%08X", urb->transfer_flags);
98 dbg("transfer_buffer :%p", urb->transfer_buffer);
99 dbg("transfer_buffer_length:%d", urb->transfer_buffer_length);
100 dbg("actual_length :%d", urb->actual_length);
101 dbg("setup_packet :%p", urb->setup_packet);
102 dbg("start_frame :%d", urb->start_frame);
103 dbg("number_of_packets :%d", urb->number_of_packets);
104 dbg("interval :%d", urb->interval);
105 dbg("error_count :%d", urb->error_count);
106 dbg("context :%p", urb->context);
107 dbg("complete :%p", urb->complete);
109 #endif
110 /*-------------------------------------------------------------------*/
111 static int dabusb_cancel_queue (pdabusb_t s, struct list_head *q)
113 unsigned long flags;
114 pbuff_t b;
116 dbg("dabusb_cancel_queue");
118 spin_lock_irqsave (&s->lock, flags);
120 list_for_each_entry(b, q, buff_list) {
121 #ifdef DEBUG
122 dump_urb(b->purb);
123 #endif
124 usb_unlink_urb (b->purb);
126 spin_unlock_irqrestore (&s->lock, flags);
127 return 0;
129 /*-------------------------------------------------------------------*/
130 static int dabusb_free_queue (struct list_head *q)
132 struct list_head *tmp;
133 struct list_head *p;
134 pbuff_t b;
136 dbg("dabusb_free_queue");
137 for (p = q->next; p != q;) {
138 b = list_entry (p, buff_t, buff_list);
140 #ifdef DEBUG
141 dump_urb(b->purb);
142 #endif
143 kfree(b->purb->transfer_buffer);
144 usb_free_urb(b->purb);
145 tmp = p->next;
146 list_del (p);
147 kfree (b);
148 p = tmp;
151 return 0;
153 /*-------------------------------------------------------------------*/
154 static int dabusb_free_buffers (pdabusb_t s)
156 unsigned long flags;
157 dbg("dabusb_free_buffers");
159 spin_lock_irqsave(&s->lock, flags);
161 dabusb_free_queue (&s->free_buff_list);
162 dabusb_free_queue (&s->rec_buff_list);
164 spin_unlock_irqrestore(&s->lock, flags);
166 s->got_mem = 0;
167 return 0;
169 /*-------------------------------------------------------------------*/
170 static void dabusb_iso_complete (struct urb *purb)
172 pbuff_t b = purb->context;
173 pdabusb_t s = b->s;
174 int i;
175 int len;
176 int dst = 0;
177 void *buf = purb->transfer_buffer;
179 dbg("dabusb_iso_complete");
181 // process if URB was not killed
182 if (purb->status != -ENOENT) {
183 unsigned int pipe = usb_rcvisocpipe (purb->dev, _DABUSB_ISOPIPE);
184 int pipesize = usb_maxpacket (purb->dev, pipe, usb_pipeout (pipe));
185 for (i = 0; i < purb->number_of_packets; i++)
186 if (!purb->iso_frame_desc[i].status) {
187 len = purb->iso_frame_desc[i].actual_length;
188 if (len <= pipesize) {
189 memcpy (buf + dst, buf + purb->iso_frame_desc[i].offset, len);
190 dst += len;
192 else
193 dev_err(&purb->dev->dev,
194 "dabusb_iso_complete: invalid len %d\n", len);
196 else
197 dev_warn(&purb->dev->dev, "dabusb_iso_complete: corrupted packet status: %d\n", purb->iso_frame_desc[i].status);
198 if (dst != purb->actual_length)
199 dev_err(&purb->dev->dev,
200 "dst!=purb->actual_length:%d!=%d\n",
201 dst, purb->actual_length);
204 if (atomic_dec_and_test (&s->pending_io) && !s->remove_pending && s->state != _stopped) {
205 s->overruns++;
206 dev_err(&purb->dev->dev, "overrun (%d)\n", s->overruns);
208 wake_up (&s->wait);
210 /*-------------------------------------------------------------------*/
211 static int dabusb_alloc_buffers (pdabusb_t s)
213 int transfer_len = 0;
214 pbuff_t b;
215 unsigned int pipe = usb_rcvisocpipe (s->usbdev, _DABUSB_ISOPIPE);
216 int pipesize = usb_maxpacket (s->usbdev, pipe, usb_pipeout (pipe));
217 int packets = _ISOPIPESIZE / pipesize;
218 int transfer_buffer_length = packets * pipesize;
219 int i;
221 dbg("dabusb_alloc_buffers pipesize:%d packets:%d transfer_buffer_len:%d",
222 pipesize, packets, transfer_buffer_length);
224 while (transfer_len < (s->total_buffer_size << 10)) {
225 b = kzalloc(sizeof (buff_t), GFP_KERNEL);
226 if (!b) {
227 dev_err(&s->usbdev->dev,
228 "kzalloc(sizeof(buff_t))==NULL\n");
229 goto err;
231 b->s = s;
232 b->purb = usb_alloc_urb(packets, GFP_KERNEL);
233 if (!b->purb) {
234 dev_err(&s->usbdev->dev, "usb_alloc_urb == NULL\n");
235 kfree (b);
236 goto err;
239 b->purb->transfer_buffer = kmalloc (transfer_buffer_length, GFP_KERNEL);
240 if (!b->purb->transfer_buffer) {
241 kfree (b->purb);
242 kfree (b);
243 dev_err(&s->usbdev->dev,
244 "kmalloc(%d)==NULL\n", transfer_buffer_length);
245 goto err;
248 b->purb->transfer_buffer_length = transfer_buffer_length;
249 b->purb->number_of_packets = packets;
250 b->purb->complete = dabusb_iso_complete;
251 b->purb->context = b;
252 b->purb->dev = s->usbdev;
253 b->purb->pipe = pipe;
254 b->purb->transfer_flags = URB_ISO_ASAP;
256 for (i = 0; i < packets; i++) {
257 b->purb->iso_frame_desc[i].offset = i * pipesize;
258 b->purb->iso_frame_desc[i].length = pipesize;
261 transfer_len += transfer_buffer_length;
262 list_add_tail (&b->buff_list, &s->free_buff_list);
264 s->got_mem = transfer_len;
266 return 0;
268 err:
269 dabusb_free_buffers (s);
270 return -ENOMEM;
272 /*-------------------------------------------------------------------*/
273 static int dabusb_bulk (pdabusb_t s, pbulk_transfer_t pb)
275 int ret;
276 unsigned int pipe;
277 int actual_length;
279 dbg("dabusb_bulk");
281 if (!pb->pipe)
282 pipe = usb_rcvbulkpipe (s->usbdev, 2);
283 else
284 pipe = usb_sndbulkpipe (s->usbdev, 2);
286 ret=usb_bulk_msg(s->usbdev, pipe, pb->data, pb->size, &actual_length, 100);
287 if(ret<0) {
288 dev_err(&s->usbdev->dev,
289 "usb_bulk_msg failed(%d)\n", ret);
291 if (usb_set_interface (s->usbdev, _DABUSB_IF, 1) < 0) {
292 dev_err(&s->usbdev->dev, "set_interface failed\n");
293 return -EINVAL;
298 if( ret == -EPIPE ) {
299 dev_warn(&s->usbdev->dev, "CLEAR_FEATURE request to remove STALL condition.\n");
300 if(usb_clear_halt(s->usbdev, usb_pipeendpoint(pipe)))
301 dev_err(&s->usbdev->dev, "request failed\n");
304 pb->size = actual_length;
305 return ret;
307 /* --------------------------------------------------------------------- */
308 static int dabusb_writemem (pdabusb_t s, int pos, const unsigned char *data,
309 int len)
311 int ret;
312 unsigned char *transfer_buffer = kmalloc (len, GFP_KERNEL);
314 if (!transfer_buffer) {
315 dev_err(&s->usbdev->dev,
316 "dabusb_writemem: kmalloc(%d) failed.\n", len);
317 return -ENOMEM;
320 memcpy (transfer_buffer, data, len);
322 ret=usb_control_msg(s->usbdev, usb_sndctrlpipe( s->usbdev, 0 ), 0xa0, 0x40, pos, 0, transfer_buffer, len, 300);
324 kfree (transfer_buffer);
325 return ret;
327 /* --------------------------------------------------------------------- */
328 static int dabusb_8051_reset (pdabusb_t s, unsigned char reset_bit)
330 dbg("dabusb_8051_reset: %d",reset_bit);
331 return dabusb_writemem (s, CPUCS_REG, &reset_bit, 1);
333 /* --------------------------------------------------------------------- */
334 static int dabusb_loadmem (pdabusb_t s, const char *fname)
336 int ret;
337 const struct ihex_binrec *rec;
338 const struct firmware *uninitialized_var(fw);
340 dbg("Enter dabusb_loadmem (internal)");
342 ret = request_ihex_firmware(&fw, "dabusb/firmware.fw", &s->usbdev->dev);
343 if (ret) {
344 dev_err(&s->usbdev->dev,
345 "Failed to load \"dabusb/firmware.fw\": %d\n", ret);
346 goto out;
348 ret = dabusb_8051_reset (s, 1);
350 for (rec = (const struct ihex_binrec *)fw->data; rec;
351 rec = ihex_next_binrec(rec)) {
352 dbg("dabusb_writemem: %04X %p %d)", be32_to_cpu(rec->addr),
353 rec->data, be16_to_cpu(rec->len));
355 ret = dabusb_writemem(s, be32_to_cpu(rec->addr), rec->data,
356 be16_to_cpu(rec->len));
357 if (ret < 0) {
358 dev_err(&s->usbdev->dev,
359 "dabusb_writemem failed (%d %04X %p %d)\n",
360 ret, be32_to_cpu(rec->addr),
361 rec->data, be16_to_cpu(rec->len));
362 break;
365 ret = dabusb_8051_reset (s, 0);
366 release_firmware(fw);
367 out:
368 dbg("dabusb_loadmem: exit");
370 return ret;
372 /* --------------------------------------------------------------------- */
373 static int dabusb_fpga_clear (pdabusb_t s, pbulk_transfer_t b)
375 b->size = 4;
376 b->data[0] = 0x2a;
377 b->data[1] = 0;
378 b->data[2] = 0;
379 b->data[3] = 0;
381 dbg("dabusb_fpga_clear");
383 return dabusb_bulk (s, b);
385 /* --------------------------------------------------------------------- */
386 static int dabusb_fpga_init (pdabusb_t s, pbulk_transfer_t b)
388 b->size = 4;
389 b->data[0] = 0x2c;
390 b->data[1] = 0;
391 b->data[2] = 0;
392 b->data[3] = 0;
394 dbg("dabusb_fpga_init");
396 return dabusb_bulk (s, b);
398 /* --------------------------------------------------------------------- */
399 static int dabusb_fpga_download (pdabusb_t s, const char *fname)
401 pbulk_transfer_t b = kmalloc (sizeof (bulk_transfer_t), GFP_KERNEL);
402 const struct firmware *fw;
403 unsigned int blen, n;
404 int ret;
406 dbg("Enter dabusb_fpga_download (internal)");
408 if (!b) {
409 dev_err(&s->usbdev->dev,
410 "kmalloc(sizeof(bulk_transfer_t))==NULL\n");
411 return -ENOMEM;
414 ret = request_firmware(&fw, "dabusb/bitstream.bin", &s->usbdev->dev);
415 if (ret) {
416 dev_err(&s->usbdev->dev,
417 "Failed to load \"dabusb/bitstream.bin\": %d\n", ret);
418 kfree(b);
419 return ret;
422 b->pipe = 1;
423 ret = dabusb_fpga_clear (s, b);
424 mdelay (10);
425 blen = fw->data[73] + (fw->data[72] << 8);
427 dbg("Bitstream len: %i", blen);
429 b->data[0] = 0x2b;
430 b->data[1] = 0;
431 b->data[2] = 0;
432 b->data[3] = 60;
434 for (n = 0; n <= blen + 60; n += 60) {
435 // some cclks for startup
436 b->size = 64;
437 memcpy (b->data + 4, fw->data + 74 + n, 60);
438 ret = dabusb_bulk (s, b);
439 if (ret < 0) {
440 dev_err(&s->usbdev->dev, "dabusb_bulk failed.\n");
441 break;
443 mdelay (1);
446 ret = dabusb_fpga_init (s, b);
447 kfree (b);
448 release_firmware(fw);
450 dbg("exit dabusb_fpga_download");
452 return ret;
455 static int dabusb_stop (pdabusb_t s)
457 dbg("dabusb_stop");
459 s->state = _stopped;
460 dabusb_cancel_queue (s, &s->rec_buff_list);
462 dbg("pending_io: %d", s->pending_io.counter);
464 s->pending_io.counter = 0;
465 return 0;
468 static int dabusb_startrek (pdabusb_t s)
470 if (!s->got_mem && s->state != _started) {
472 dbg("dabusb_startrek");
474 if (dabusb_alloc_buffers (s) < 0)
475 return -ENOMEM;
476 dabusb_stop (s);
477 s->state = _started;
478 s->readptr = 0;
481 if (!list_empty (&s->free_buff_list)) {
482 pbuff_t end;
483 int ret;
485 while (!dabusb_add_buf_tail (s, &s->rec_buff_list, &s->free_buff_list)) {
487 dbg("submitting: end:%p s->rec_buff_list:%p", s->rec_buff_list.prev, &s->rec_buff_list);
489 end = list_entry (s->rec_buff_list.prev, buff_t, buff_list);
491 ret = usb_submit_urb (end->purb, GFP_KERNEL);
492 if (ret) {
493 dev_err(&s->usbdev->dev,
494 "usb_submit_urb returned:%d\n", ret);
495 if (dabusb_add_buf_tail (s, &s->free_buff_list, &s->rec_buff_list))
496 dev_err(&s->usbdev->dev,
497 "startrek: dabusb_add_buf_tail failed\n");
498 break;
500 else
501 atomic_inc (&s->pending_io);
503 dbg("pending_io: %d",s->pending_io.counter);
506 return 0;
509 static ssize_t dabusb_read (struct file *file, char __user *buf, size_t count, loff_t * ppos)
511 pdabusb_t s = (pdabusb_t) file->private_data;
512 unsigned long flags;
513 unsigned ret = 0;
514 int rem;
515 int cnt;
516 pbuff_t b;
517 struct urb *purb = NULL;
519 dbg("dabusb_read");
521 if (*ppos)
522 return -ESPIPE;
524 if (s->remove_pending)
525 return -EIO;
528 if (!s->usbdev)
529 return -EIO;
531 while (count > 0) {
532 dabusb_startrek (s);
534 spin_lock_irqsave (&s->lock, flags);
536 if (list_empty (&s->rec_buff_list)) {
538 spin_unlock_irqrestore(&s->lock, flags);
540 dev_err(&s->usbdev->dev,
541 "error: rec_buf_list is empty\n");
542 goto err;
545 b = list_entry (s->rec_buff_list.next, buff_t, buff_list);
546 purb = b->purb;
548 spin_unlock_irqrestore(&s->lock, flags);
550 if (purb->status == -EINPROGRESS) {
551 if (file->f_flags & O_NONBLOCK) // return nonblocking
553 if (!ret)
554 ret = -EAGAIN;
555 goto err;
558 interruptible_sleep_on (&s->wait);
560 if (signal_pending (current)) {
561 if (!ret)
562 ret = -ERESTARTSYS;
563 goto err;
566 spin_lock_irqsave (&s->lock, flags);
568 if (list_empty (&s->rec_buff_list)) {
569 spin_unlock_irqrestore(&s->lock, flags);
570 dev_err(&s->usbdev->dev,
571 "error: still no buffer available.\n");
572 goto err;
574 spin_unlock_irqrestore(&s->lock, flags);
575 s->readptr = 0;
577 if (s->remove_pending) {
578 ret = -EIO;
579 goto err;
582 rem = purb->actual_length - s->readptr; // set remaining bytes to copy
584 if (count >= rem)
585 cnt = rem;
586 else
587 cnt = count;
589 dbg("copy_to_user:%p %p %d",buf, purb->transfer_buffer + s->readptr, cnt);
591 if (copy_to_user (buf, purb->transfer_buffer + s->readptr, cnt)) {
592 dev_err(&s->usbdev->dev, "read: copy_to_user failed\n");
593 if (!ret)
594 ret = -EFAULT;
595 goto err;
598 s->readptr += cnt;
599 count -= cnt;
600 buf += cnt;
601 ret += cnt;
603 if (s->readptr == purb->actual_length) {
604 // finished, take next buffer
605 if (dabusb_add_buf_tail (s, &s->free_buff_list, &s->rec_buff_list))
606 dev_err(&s->usbdev->dev,
607 "read: dabusb_add_buf_tail failed\n");
608 s->readptr = 0;
611 err: //mutex_unlock(&s->mutex);
612 return ret;
615 static int dabusb_open (struct inode *inode, struct file *file)
617 int devnum = iminor(inode);
618 pdabusb_t s;
620 if (devnum < DABUSB_MINOR || devnum >= (DABUSB_MINOR + NRDABUSB))
621 return -EIO;
623 s = &dabusb[devnum - DABUSB_MINOR];
625 dbg("dabusb_open");
626 mutex_lock(&s->mutex);
628 while (!s->usbdev || s->opened) {
629 mutex_unlock(&s->mutex);
631 if (file->f_flags & O_NONBLOCK) {
632 return -EBUSY;
634 msleep_interruptible(500);
636 if (signal_pending (current)) {
637 return -EAGAIN;
639 mutex_lock(&s->mutex);
641 if (usb_set_interface (s->usbdev, _DABUSB_IF, 1) < 0) {
642 mutex_unlock(&s->mutex);
643 dev_err(&s->usbdev->dev, "set_interface failed\n");
644 return -EINVAL;
646 s->opened = 1;
647 mutex_unlock(&s->mutex);
649 file->f_pos = 0;
650 file->private_data = s;
652 return nonseekable_open(inode, file);
655 static int dabusb_release (struct inode *inode, struct file *file)
657 pdabusb_t s = (pdabusb_t) file->private_data;
659 dbg("dabusb_release");
661 mutex_lock(&s->mutex);
662 dabusb_stop (s);
663 dabusb_free_buffers (s);
664 mutex_unlock(&s->mutex);
666 if (!s->remove_pending) {
667 if (usb_set_interface (s->usbdev, _DABUSB_IF, 0) < 0)
668 dev_err(&s->usbdev->dev, "set_interface failed\n");
670 else
671 wake_up (&s->remove_ok);
673 s->opened = 0;
674 return 0;
677 static long dabusb_ioctl (struct file *file, unsigned int cmd, unsigned long arg)
679 pdabusb_t s = (pdabusb_t) file->private_data;
680 pbulk_transfer_t pbulk;
681 int ret = 0;
682 int version = DABUSB_VERSION;
684 dbg("dabusb_ioctl");
686 lock_kernel();
687 if (s->remove_pending) {
688 unlock_kernel();
689 return -EIO;
692 mutex_lock(&s->mutex);
694 if (!s->usbdev) {
695 mutex_unlock(&s->mutex);
696 unlock_kernel();
697 return -EIO;
700 switch (cmd) {
702 case IOCTL_DAB_BULK:
703 pbulk = kmalloc(sizeof (bulk_transfer_t), GFP_KERNEL);
705 if (!pbulk) {
706 ret = -ENOMEM;
707 break;
710 if (copy_from_user (pbulk, (void __user *) arg, sizeof (bulk_transfer_t))) {
711 ret = -EFAULT;
712 kfree (pbulk);
713 break;
716 ret=dabusb_bulk (s, pbulk);
717 if(ret==0)
718 if (copy_to_user((void __user *)arg, pbulk,
719 sizeof(bulk_transfer_t)))
720 ret = -EFAULT;
721 kfree (pbulk);
722 break;
724 case IOCTL_DAB_OVERRUNS:
725 ret = put_user (s->overruns, (unsigned int __user *) arg);
726 break;
728 case IOCTL_DAB_VERSION:
729 ret = put_user (version, (unsigned int __user *) arg);
730 break;
732 default:
733 ret = -ENOIOCTLCMD;
734 break;
736 mutex_unlock(&s->mutex);
737 unlock_kernel();
738 return ret;
741 static const struct file_operations dabusb_fops =
743 .owner = THIS_MODULE,
744 .llseek = no_llseek,
745 .read = dabusb_read,
746 .unlocked_ioctl = dabusb_ioctl,
747 .open = dabusb_open,
748 .release = dabusb_release,
751 static char *dabusb_devnode(struct device *dev, mode_t *mode)
753 return kasprintf(GFP_KERNEL, "usb/%s", dev_name(dev));
756 static struct usb_class_driver dabusb_class = {
757 .name = "dabusb%d",
758 .devnode = dabusb_devnode,
759 .fops = &dabusb_fops,
760 .minor_base = DABUSB_MINOR,
764 /* --------------------------------------------------------------------- */
765 static int dabusb_probe (struct usb_interface *intf,
766 const struct usb_device_id *id)
768 struct usb_device *usbdev = interface_to_usbdev(intf);
769 int retval;
770 pdabusb_t s;
772 dbg("dabusb: probe: vendor id 0x%x, device id 0x%x ifnum:%d",
773 le16_to_cpu(usbdev->descriptor.idVendor),
774 le16_to_cpu(usbdev->descriptor.idProduct),
775 intf->altsetting->desc.bInterfaceNumber);
777 /* We don't handle multiple configurations */
778 if (usbdev->descriptor.bNumConfigurations != 1)
779 return -ENODEV;
781 if (intf->altsetting->desc.bInterfaceNumber != _DABUSB_IF &&
782 le16_to_cpu(usbdev->descriptor.idProduct) == 0x9999)
783 return -ENODEV;
787 s = &dabusb[intf->minor];
789 mutex_lock(&s->mutex);
790 s->remove_pending = 0;
791 s->usbdev = usbdev;
792 s->devnum = intf->minor;
794 if (usb_reset_configuration (usbdev) < 0) {
795 dev_err(&intf->dev, "reset_configuration failed\n");
796 goto reject;
798 if (le16_to_cpu(usbdev->descriptor.idProduct) == 0x2131) {
799 dabusb_loadmem (s, NULL);
800 goto reject;
802 else {
803 dabusb_fpga_download (s, NULL);
805 if (usb_set_interface (s->usbdev, _DABUSB_IF, 0) < 0) {
806 dev_err(&intf->dev, "set_interface failed\n");
807 goto reject;
810 dbg("bound to interface: %d", intf->altsetting->desc.bInterfaceNumber);
811 usb_set_intfdata (intf, s);
812 mutex_unlock(&s->mutex);
814 retval = usb_register_dev(intf, &dabusb_class);
815 if (retval) {
816 usb_set_intfdata (intf, NULL);
817 return -ENOMEM;
820 return 0;
822 reject:
823 mutex_unlock(&s->mutex);
824 s->usbdev = NULL;
825 return -ENODEV;
828 static void dabusb_disconnect (struct usb_interface *intf)
830 wait_queue_t __wait;
831 pdabusb_t s = usb_get_intfdata (intf);
833 dbg("dabusb_disconnect");
835 init_waitqueue_entry(&__wait, current);
837 usb_set_intfdata (intf, NULL);
838 if (s) {
839 usb_deregister_dev (intf, &dabusb_class);
840 s->remove_pending = 1;
841 wake_up (&s->wait);
842 add_wait_queue(&s->remove_ok, &__wait);
843 set_current_state(TASK_UNINTERRUPTIBLE);
844 if (s->state == _started)
845 schedule();
846 current->state = TASK_RUNNING;
847 remove_wait_queue(&s->remove_ok, &__wait);
849 s->usbdev = NULL;
850 s->overruns = 0;
854 static struct usb_device_id dabusb_ids [] = {
855 // { USB_DEVICE(0x0547, 0x2131) }, /* An2131 chip, no boot ROM */
856 { USB_DEVICE(0x0547, 0x9999) },
857 { } /* Terminating entry */
860 MODULE_DEVICE_TABLE (usb, dabusb_ids);
862 static struct usb_driver dabusb_driver = {
863 .name = "dabusb",
864 .probe = dabusb_probe,
865 .disconnect = dabusb_disconnect,
866 .id_table = dabusb_ids,
869 /* --------------------------------------------------------------------- */
871 static int __init dabusb_init (void)
873 int retval;
874 unsigned u;
876 /* initialize struct */
877 for (u = 0; u < NRDABUSB; u++) {
878 pdabusb_t s = &dabusb[u];
879 memset (s, 0, sizeof (dabusb_t));
880 mutex_init (&s->mutex);
881 s->usbdev = NULL;
882 s->total_buffer_size = buffers;
883 init_waitqueue_head (&s->wait);
884 init_waitqueue_head (&s->remove_ok);
885 spin_lock_init (&s->lock);
886 INIT_LIST_HEAD (&s->free_buff_list);
887 INIT_LIST_HEAD (&s->rec_buff_list);
890 /* register misc device */
891 retval = usb_register(&dabusb_driver);
892 if (retval)
893 goto out;
895 dbg("dabusb_init: driver registered");
897 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
898 DRIVER_DESC "\n");
900 out:
901 return retval;
904 static void __exit dabusb_cleanup (void)
906 dbg("dabusb_cleanup");
908 usb_deregister (&dabusb_driver);
911 /* --------------------------------------------------------------------- */
913 MODULE_AUTHOR( DRIVER_AUTHOR );
914 MODULE_DESCRIPTION( DRIVER_DESC );
915 MODULE_LICENSE("GPL");
917 module_param(buffers, int, 0);
918 MODULE_PARM_DESC (buffers, "Number of buffers (default=256)");
920 module_init (dabusb_init);
921 module_exit (dabusb_cleanup);
923 /* --------------------------------------------------------------------- */