Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6
[linux-2.6] / drivers / media / video / dabusb.c
1 /*****************************************************************************/
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>
40 #include <linux/mutex.h>
41
42 #include "dabusb.h"
43 #include "dabfirmware.h"
44
45 /*
46  * Version Information
47  */
48 #define DRIVER_VERSION "v1.54"
49 #define DRIVER_AUTHOR "Deti Fliegl, deti@fliegl.de"
50 #define DRIVER_DESC "DAB-USB Interface Driver for Linux (c)1999"
51
52 /* --------------------------------------------------------------------- */
53
54 #ifdef CONFIG_USB_DYNAMIC_MINORS
55 #define NRDABUSB 256
56 #else
57 #define NRDABUSB 4
58 #endif
59
60 /*-------------------------------------------------------------------*/
61
62 static dabusb_t dabusb[NRDABUSB];
63 static int buffers = 256;
64 static struct usb_driver dabusb_driver;
65
66 /*-------------------------------------------------------------------*/
67
68 static int dabusb_add_buf_tail (pdabusb_t s, struct list_head *dst, struct list_head *src)
69 {
70         unsigned long flags;
71         struct list_head *tmp;
72         int ret = 0;
73
74         spin_lock_irqsave (&s->lock, flags);
75
76         if (list_empty (src)) {
77                 // no elements in source buffer
78                 ret = -1;
79                 goto err;
80         }
81         tmp = src->next;
82         list_move_tail (tmp, dst);
83
84   err:  spin_unlock_irqrestore (&s->lock, flags);
85         return ret;
86 }
87 /*-------------------------------------------------------------------*/
88 #ifdef DEBUG
89 static void dump_urb (struct urb *urb)
90 {
91         dbg("urb                   :%p", urb);
92         dbg("dev                   :%p", urb->dev);
93         dbg("pipe                  :%08X", urb->pipe);
94         dbg("status                :%d", urb->status);
95         dbg("transfer_flags        :%08X", urb->transfer_flags);
96         dbg("transfer_buffer       :%p", urb->transfer_buffer);
97         dbg("transfer_buffer_length:%d", urb->transfer_buffer_length);
98         dbg("actual_length         :%d", urb->actual_length);
99         dbg("setup_packet          :%p", urb->setup_packet);
100         dbg("start_frame           :%d", urb->start_frame);
101         dbg("number_of_packets     :%d", urb->number_of_packets);
102         dbg("interval              :%d", urb->interval);
103         dbg("error_count           :%d", urb->error_count);
104         dbg("context               :%p", urb->context);
105         dbg("complete              :%p", urb->complete);
106 }
107 #endif
108 /*-------------------------------------------------------------------*/
109 static int dabusb_cancel_queue (pdabusb_t s, struct list_head *q)
110 {
111         unsigned long flags;
112         pbuff_t b;
113
114         dbg("dabusb_cancel_queue");
115
116         spin_lock_irqsave (&s->lock, flags);
117
118         list_for_each_entry(b, q, buff_list) {
119 #ifdef DEBUG
120                 dump_urb(b->purb);
121 #endif
122                 usb_unlink_urb (b->purb);
123         }
124         spin_unlock_irqrestore (&s->lock, flags);
125         return 0;
126 }
127 /*-------------------------------------------------------------------*/
128 static int dabusb_free_queue (struct list_head *q)
129 {
130         struct list_head *tmp;
131         struct list_head *p;
132         pbuff_t b;
133
134         dbg("dabusb_free_queue");
135         for (p = q->next; p != q;) {
136                 b = list_entry (p, buff_t, buff_list);
137
138 #ifdef DEBUG
139                 dump_urb(b->purb);
140 #endif
141                 kfree(b->purb->transfer_buffer);
142                 usb_free_urb(b->purb);
143                 tmp = p->next;
144                 list_del (p);
145                 kfree (b);
146                 p = tmp;
147         }
148
149         return 0;
150 }
151 /*-------------------------------------------------------------------*/
152 static int dabusb_free_buffers (pdabusb_t s)
153 {
154         unsigned long flags;
155         dbg("dabusb_free_buffers");
156
157         spin_lock_irqsave(&s->lock, flags);
158
159         dabusb_free_queue (&s->free_buff_list);
160         dabusb_free_queue (&s->rec_buff_list);
161
162         spin_unlock_irqrestore(&s->lock, flags);
163
164         s->got_mem = 0;
165         return 0;
166 }
167 /*-------------------------------------------------------------------*/
168 static void dabusb_iso_complete (struct urb *purb)
169 {
170         pbuff_t b = purb->context;
171         pdabusb_t s = b->s;
172         int i;
173         int len;
174         int dst = 0;
175         void *buf = purb->transfer_buffer;
176
177         dbg("dabusb_iso_complete");
178
179         // process if URB was not killed
180         if (purb->status != -ENOENT) {
181                 unsigned int pipe = usb_rcvisocpipe (purb->dev, _DABUSB_ISOPIPE);
182                 int pipesize = usb_maxpacket (purb->dev, pipe, usb_pipeout (pipe));
183                 for (i = 0; i < purb->number_of_packets; i++)
184                         if (!purb->iso_frame_desc[i].status) {
185                                 len = purb->iso_frame_desc[i].actual_length;
186                                 if (len <= pipesize) {
187                                         memcpy (buf + dst, buf + purb->iso_frame_desc[i].offset, len);
188                                         dst += len;
189                                 }
190                                 else
191                                         err("dabusb_iso_complete: invalid len %d", len);
192                         }
193                         else
194                                 warn("dabusb_iso_complete: corrupted packet status: %d", purb->iso_frame_desc[i].status);
195                 if (dst != purb->actual_length)
196                         err("dst!=purb->actual_length:%d!=%d", dst, purb->actual_length);
197         }
198
199         if (atomic_dec_and_test (&s->pending_io) && !s->remove_pending && s->state != _stopped) {
200                 s->overruns++;
201                 err("overrun (%d)", s->overruns);
202         }
203         wake_up (&s->wait);
204 }
205 /*-------------------------------------------------------------------*/
206 static int dabusb_alloc_buffers (pdabusb_t s)
207 {
208         int transfer_len = 0;
209         pbuff_t b;
210         unsigned int pipe = usb_rcvisocpipe (s->usbdev, _DABUSB_ISOPIPE);
211         int pipesize = usb_maxpacket (s->usbdev, pipe, usb_pipeout (pipe));
212         int packets = _ISOPIPESIZE / pipesize;
213         int transfer_buffer_length = packets * pipesize;
214         int i;
215
216         dbg("dabusb_alloc_buffers pipesize:%d packets:%d transfer_buffer_len:%d",
217                  pipesize, packets, transfer_buffer_length);
218
219         while (transfer_len < (s->total_buffer_size << 10)) {
220                 b = kzalloc(sizeof (buff_t), GFP_KERNEL);
221                 if (!b) {
222                         err("kzalloc(sizeof(buff_t))==NULL");
223                         goto err;
224                 }
225                 b->s = s;
226                 b->purb = usb_alloc_urb(packets, GFP_KERNEL);
227                 if (!b->purb) {
228                         err("usb_alloc_urb == NULL");
229                         kfree (b);
230                         goto err;
231                 }
232
233                 b->purb->transfer_buffer = kmalloc (transfer_buffer_length, GFP_KERNEL);
234                 if (!b->purb->transfer_buffer) {
235                         kfree (b->purb);
236                         kfree (b);
237                         err("kmalloc(%d)==NULL", transfer_buffer_length);
238                         goto err;
239                 }
240
241                 b->purb->transfer_buffer_length = transfer_buffer_length;
242                 b->purb->number_of_packets = packets;
243                 b->purb->complete = dabusb_iso_complete;
244                 b->purb->context = b;
245                 b->purb->dev = s->usbdev;
246                 b->purb->pipe = pipe;
247                 b->purb->transfer_flags = URB_ISO_ASAP;
248
249                 for (i = 0; i < packets; i++) {
250                         b->purb->iso_frame_desc[i].offset = i * pipesize;
251                         b->purb->iso_frame_desc[i].length = pipesize;
252                 }
253
254                 transfer_len += transfer_buffer_length;
255                 list_add_tail (&b->buff_list, &s->free_buff_list);
256         }
257         s->got_mem = transfer_len;
258
259         return 0;
260
261         err:
262         dabusb_free_buffers (s);
263         return -ENOMEM;
264 }
265 /*-------------------------------------------------------------------*/
266 static int dabusb_bulk (pdabusb_t s, pbulk_transfer_t pb)
267 {
268         int ret;
269         unsigned int pipe;
270         int actual_length;
271
272         dbg("dabusb_bulk");
273
274         if (!pb->pipe)
275                 pipe = usb_rcvbulkpipe (s->usbdev, 2);
276         else
277                 pipe = usb_sndbulkpipe (s->usbdev, 2);
278
279         ret=usb_bulk_msg(s->usbdev, pipe, pb->data, pb->size, &actual_length, 100);
280         if(ret<0) {
281                 err("dabusb: usb_bulk_msg failed(%d)",ret);
282
283                 if (usb_set_interface (s->usbdev, _DABUSB_IF, 1) < 0) {
284                         err("set_interface failed");
285                         return -EINVAL;
286                 }
287
288         }
289
290         if( ret == -EPIPE ) {
291                 warn("CLEAR_FEATURE request to remove STALL condition.");
292                 if(usb_clear_halt(s->usbdev, usb_pipeendpoint(pipe)))
293                         err("request failed");
294         }
295
296         pb->size = actual_length;
297         return ret;
298 }
299 /* --------------------------------------------------------------------- */
300 static int dabusb_writemem (pdabusb_t s, int pos, unsigned char *data, int len)
301 {
302         int ret;
303         unsigned char *transfer_buffer =  kmalloc (len, GFP_KERNEL);
304
305         if (!transfer_buffer) {
306                 err("dabusb_writemem: kmalloc(%d) failed.", len);
307                 return -ENOMEM;
308         }
309
310         memcpy (transfer_buffer, data, len);
311
312         ret=usb_control_msg(s->usbdev, usb_sndctrlpipe( s->usbdev, 0 ), 0xa0, 0x40, pos, 0, transfer_buffer, len, 300);
313
314         kfree (transfer_buffer);
315         return ret;
316 }
317 /* --------------------------------------------------------------------- */
318 static int dabusb_8051_reset (pdabusb_t s, unsigned char reset_bit)
319 {
320         dbg("dabusb_8051_reset: %d",reset_bit);
321         return dabusb_writemem (s, CPUCS_REG, &reset_bit, 1);
322 }
323 /* --------------------------------------------------------------------- */
324 static int dabusb_loadmem (pdabusb_t s, const char *fname)
325 {
326         int ret;
327         PINTEL_HEX_RECORD ptr = firmware;
328
329         dbg("Enter dabusb_loadmem (internal)");
330
331         ret = dabusb_8051_reset (s, 1);
332         while (ptr->Type == 0) {
333
334                 dbg("dabusb_writemem: %04X %p %d)", ptr->Address, ptr->Data, ptr->Length);
335
336                 ret = dabusb_writemem (s, ptr->Address, ptr->Data, ptr->Length);
337                 if (ret < 0) {
338                         err("dabusb_writemem failed (%d %04X %p %d)", ret, ptr->Address, ptr->Data, ptr->Length);
339                         break;
340                 }
341                 ptr++;
342         }
343         ret = dabusb_8051_reset (s, 0);
344
345         dbg("dabusb_loadmem: exit");
346
347         return ret;
348 }
349 /* --------------------------------------------------------------------- */
350 static int dabusb_fpga_clear (pdabusb_t s, pbulk_transfer_t b)
351 {
352         b->size = 4;
353         b->data[0] = 0x2a;
354         b->data[1] = 0;
355         b->data[2] = 0;
356         b->data[3] = 0;
357
358         dbg("dabusb_fpga_clear");
359
360         return dabusb_bulk (s, b);
361 }
362 /* --------------------------------------------------------------------- */
363 static int dabusb_fpga_init (pdabusb_t s, pbulk_transfer_t b)
364 {
365         b->size = 4;
366         b->data[0] = 0x2c;
367         b->data[1] = 0;
368         b->data[2] = 0;
369         b->data[3] = 0;
370
371         dbg("dabusb_fpga_init");
372
373         return dabusb_bulk (s, b);
374 }
375 /* --------------------------------------------------------------------- */
376 static int dabusb_fpga_download (pdabusb_t s, const char *fname)
377 {
378         pbulk_transfer_t b = kmalloc (sizeof (bulk_transfer_t), GFP_KERNEL);
379         unsigned int blen, n;
380         int ret;
381         unsigned char *buf = bitstream;
382
383         dbg("Enter dabusb_fpga_download (internal)");
384
385         if (!b) {
386                 err("kmalloc(sizeof(bulk_transfer_t))==NULL");
387                 return -ENOMEM;
388         }
389
390         b->pipe = 1;
391         ret = dabusb_fpga_clear (s, b);
392         mdelay (10);
393         blen = buf[73] + (buf[72] << 8);
394
395         dbg("Bitstream len: %i", blen);
396
397         b->data[0] = 0x2b;
398         b->data[1] = 0;
399         b->data[2] = 0;
400         b->data[3] = 60;
401
402         for (n = 0; n <= blen + 60; n += 60) {
403                 // some cclks for startup
404                 b->size = 64;
405                 memcpy (b->data + 4, buf + 74 + n, 60);
406                 ret = dabusb_bulk (s, b);
407                 if (ret < 0) {
408                         err("dabusb_bulk failed.");
409                         break;
410                 }
411                 mdelay (1);
412         }
413
414         ret = dabusb_fpga_init (s, b);
415         kfree (b);
416
417         dbg("exit dabusb_fpga_download");
418
419         return ret;
420 }
421
422 static int dabusb_stop (pdabusb_t s)
423 {
424         dbg("dabusb_stop");
425
426         s->state = _stopped;
427         dabusb_cancel_queue (s, &s->rec_buff_list);
428
429         dbg("pending_io: %d", s->pending_io.counter);
430
431         s->pending_io.counter = 0;
432         return 0;
433 }
434
435 static int dabusb_startrek (pdabusb_t s)
436 {
437         if (!s->got_mem && s->state != _started) {
438
439                 dbg("dabusb_startrek");
440
441                 if (dabusb_alloc_buffers (s) < 0)
442                         return -ENOMEM;
443                 dabusb_stop (s);
444                 s->state = _started;
445                 s->readptr = 0;
446         }
447
448         if (!list_empty (&s->free_buff_list)) {
449                 pbuff_t end;
450                 int ret;
451
452         while (!dabusb_add_buf_tail (s, &s->rec_buff_list, &s->free_buff_list)) {
453
454                         dbg("submitting: end:%p s->rec_buff_list:%p", s->rec_buff_list.prev, &s->rec_buff_list);
455
456                         end = list_entry (s->rec_buff_list.prev, buff_t, buff_list);
457
458                         ret = usb_submit_urb (end->purb, GFP_KERNEL);
459                         if (ret) {
460                                 err("usb_submit_urb returned:%d", ret);
461                                 if (dabusb_add_buf_tail (s, &s->free_buff_list, &s->rec_buff_list))
462                                         err("startrek: dabusb_add_buf_tail failed");
463                                 break;
464                         }
465                         else
466                                 atomic_inc (&s->pending_io);
467                 }
468                 dbg("pending_io: %d",s->pending_io.counter);
469         }
470
471         return 0;
472 }
473
474 static ssize_t dabusb_read (struct file *file, char __user *buf, size_t count, loff_t * ppos)
475 {
476         pdabusb_t s = (pdabusb_t) file->private_data;
477         unsigned long flags;
478         unsigned ret = 0;
479         int rem;
480         int cnt;
481         pbuff_t b;
482         struct urb *purb = NULL;
483
484         dbg("dabusb_read");
485
486         if (*ppos)
487                 return -ESPIPE;
488
489         if (s->remove_pending)
490                 return -EIO;
491
492
493         if (!s->usbdev)
494                 return -EIO;
495
496         while (count > 0) {
497                 dabusb_startrek (s);
498
499                 spin_lock_irqsave (&s->lock, flags);
500
501                 if (list_empty (&s->rec_buff_list)) {
502
503                         spin_unlock_irqrestore(&s->lock, flags);
504
505                         err("error: rec_buf_list is empty");
506                         goto err;
507                 }
508
509                 b = list_entry (s->rec_buff_list.next, buff_t, buff_list);
510                 purb = b->purb;
511
512                 spin_unlock_irqrestore(&s->lock, flags);
513
514                 if (purb->status == -EINPROGRESS) {
515                         if (file->f_flags & O_NONBLOCK)         // return nonblocking
516                          {
517                                 if (!ret)
518                                         ret = -EAGAIN;
519                                 goto err;
520                         }
521
522                         interruptible_sleep_on (&s->wait);
523
524                         if (signal_pending (current)) {
525                                 if (!ret)
526                                         ret = -ERESTARTSYS;
527                                 goto err;
528                         }
529
530                         spin_lock_irqsave (&s->lock, flags);
531
532                         if (list_empty (&s->rec_buff_list)) {
533                                 spin_unlock_irqrestore(&s->lock, flags);
534                                 err("error: still no buffer available.");
535                                 goto err;
536                         }
537                         spin_unlock_irqrestore(&s->lock, flags);
538                         s->readptr = 0;
539                 }
540                 if (s->remove_pending) {
541                         ret = -EIO;
542                         goto err;
543                 }
544
545                 rem = purb->actual_length - s->readptr;         // set remaining bytes to copy
546
547                 if (count >= rem)
548                         cnt = rem;
549                 else
550                         cnt = count;
551
552                 dbg("copy_to_user:%p %p %d",buf, purb->transfer_buffer + s->readptr, cnt);
553
554                 if (copy_to_user (buf, purb->transfer_buffer + s->readptr, cnt)) {
555                         err("read: copy_to_user failed");
556                         if (!ret)
557                                 ret = -EFAULT;
558                         goto err;
559                 }
560
561                 s->readptr += cnt;
562                 count -= cnt;
563                 buf += cnt;
564                 ret += cnt;
565
566                 if (s->readptr == purb->actual_length) {
567                         // finished, take next buffer
568                         if (dabusb_add_buf_tail (s, &s->free_buff_list, &s->rec_buff_list))
569                                 err("read: dabusb_add_buf_tail failed");
570                         s->readptr = 0;
571                 }
572         }
573       err:                      //mutex_unlock(&s->mutex);
574         return ret;
575 }
576
577 static int dabusb_open (struct inode *inode, struct file *file)
578 {
579         int devnum = iminor(inode);
580         pdabusb_t s;
581
582         if (devnum < DABUSB_MINOR || devnum >= (DABUSB_MINOR + NRDABUSB))
583                 return -EIO;
584
585         s = &dabusb[devnum - DABUSB_MINOR];
586
587         dbg("dabusb_open");
588         mutex_lock(&s->mutex);
589
590         while (!s->usbdev || s->opened) {
591                 mutex_unlock(&s->mutex);
592
593                 if (file->f_flags & O_NONBLOCK) {
594                         return -EBUSY;
595                 }
596                 msleep_interruptible(500);
597
598                 if (signal_pending (current)) {
599                         return -EAGAIN;
600                 }
601                 mutex_lock(&s->mutex);
602         }
603         if (usb_set_interface (s->usbdev, _DABUSB_IF, 1) < 0) {
604                 mutex_unlock(&s->mutex);
605                 err("set_interface failed");
606                 return -EINVAL;
607         }
608         s->opened = 1;
609         mutex_unlock(&s->mutex);
610
611         file->f_pos = 0;
612         file->private_data = s;
613
614         return nonseekable_open(inode, file);
615 }
616
617 static int dabusb_release (struct inode *inode, struct file *file)
618 {
619         pdabusb_t s = (pdabusb_t) file->private_data;
620
621         dbg("dabusb_release");
622
623         mutex_lock(&s->mutex);
624         dabusb_stop (s);
625         dabusb_free_buffers (s);
626         mutex_unlock(&s->mutex);
627
628         if (!s->remove_pending) {
629                 if (usb_set_interface (s->usbdev, _DABUSB_IF, 0) < 0)
630                         err("set_interface failed");
631         }
632         else
633                 wake_up (&s->remove_ok);
634
635         s->opened = 0;
636         return 0;
637 }
638
639 static int dabusb_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
640 {
641         pdabusb_t s = (pdabusb_t) file->private_data;
642         pbulk_transfer_t pbulk;
643         int ret = 0;
644         int version = DABUSB_VERSION;
645
646         dbg("dabusb_ioctl");
647
648         if (s->remove_pending)
649                 return -EIO;
650
651         mutex_lock(&s->mutex);
652
653         if (!s->usbdev) {
654                 mutex_unlock(&s->mutex);
655                 return -EIO;
656         }
657
658         switch (cmd) {
659
660         case IOCTL_DAB_BULK:
661                 pbulk = kmalloc(sizeof (bulk_transfer_t), GFP_KERNEL);
662
663                 if (!pbulk) {
664                         ret = -ENOMEM;
665                         break;
666                 }
667
668                 if (copy_from_user (pbulk, (void __user *) arg, sizeof (bulk_transfer_t))) {
669                         ret = -EFAULT;
670                         kfree (pbulk);
671                         break;
672                 }
673
674                 ret=dabusb_bulk (s, pbulk);
675                 if(ret==0)
676                         if (copy_to_user((void __user *)arg, pbulk,
677                                          sizeof(bulk_transfer_t)))
678                                 ret = -EFAULT;
679                 kfree (pbulk);
680                 break;
681
682         case IOCTL_DAB_OVERRUNS:
683                 ret = put_user (s->overruns, (unsigned int __user *) arg);
684                 break;
685
686         case IOCTL_DAB_VERSION:
687                 ret = put_user (version, (unsigned int __user *) arg);
688                 break;
689
690         default:
691                 ret = -ENOIOCTLCMD;
692                 break;
693         }
694         mutex_unlock(&s->mutex);
695         return ret;
696 }
697
698 static const struct file_operations dabusb_fops =
699 {
700         .owner =        THIS_MODULE,
701         .llseek =       no_llseek,
702         .read =         dabusb_read,
703         .ioctl =        dabusb_ioctl,
704         .open =         dabusb_open,
705         .release =      dabusb_release,
706 };
707
708 static struct usb_class_driver dabusb_class = {
709         .name =         "dabusb%d",
710         .fops =         &dabusb_fops,
711         .minor_base =   DABUSB_MINOR,
712 };
713
714
715 /* --------------------------------------------------------------------- */
716 static int dabusb_probe (struct usb_interface *intf,
717                          const struct usb_device_id *id)
718 {
719         struct usb_device *usbdev = interface_to_usbdev(intf);
720         int retval;
721         pdabusb_t s;
722
723         dbg("dabusb: probe: vendor id 0x%x, device id 0x%x ifnum:%d",
724             le16_to_cpu(usbdev->descriptor.idVendor),
725             le16_to_cpu(usbdev->descriptor.idProduct),
726             intf->altsetting->desc.bInterfaceNumber);
727
728         /* We don't handle multiple configurations */
729         if (usbdev->descriptor.bNumConfigurations != 1)
730                 return -ENODEV;
731
732         if (intf->altsetting->desc.bInterfaceNumber != _DABUSB_IF &&
733             le16_to_cpu(usbdev->descriptor.idProduct) == 0x9999)
734                 return -ENODEV;
735
736
737
738         s = &dabusb[intf->minor];
739
740         mutex_lock(&s->mutex);
741         s->remove_pending = 0;
742         s->usbdev = usbdev;
743         s->devnum = intf->minor;
744
745         if (usb_reset_configuration (usbdev) < 0) {
746                 err("reset_configuration failed");
747                 goto reject;
748         }
749         if (le16_to_cpu(usbdev->descriptor.idProduct) == 0x2131) {
750                 dabusb_loadmem (s, NULL);
751                 goto reject;
752         }
753         else {
754                 dabusb_fpga_download (s, NULL);
755
756                 if (usb_set_interface (s->usbdev, _DABUSB_IF, 0) < 0) {
757                         err("set_interface failed");
758                         goto reject;
759                 }
760         }
761         dbg("bound to interface: %d", intf->altsetting->desc.bInterfaceNumber);
762         usb_set_intfdata (intf, s);
763         mutex_unlock(&s->mutex);
764
765         retval = usb_register_dev(intf, &dabusb_class);
766         if (retval) {
767                 usb_set_intfdata (intf, NULL);
768                 return -ENOMEM;
769         }
770
771         return 0;
772
773       reject:
774         mutex_unlock(&s->mutex);
775         s->usbdev = NULL;
776         return -ENODEV;
777 }
778
779 static void dabusb_disconnect (struct usb_interface *intf)
780 {
781         wait_queue_t __wait;
782         pdabusb_t s = usb_get_intfdata (intf);
783
784         dbg("dabusb_disconnect");
785
786         init_waitqueue_entry(&__wait, current);
787
788         usb_set_intfdata (intf, NULL);
789         if (s) {
790                 usb_deregister_dev (intf, &dabusb_class);
791                 s->remove_pending = 1;
792                 wake_up (&s->wait);
793                 add_wait_queue(&s->remove_ok, &__wait);
794                 set_current_state(TASK_UNINTERRUPTIBLE);
795                 if (s->state == _started)
796                         schedule();
797                 current->state = TASK_RUNNING;
798                 remove_wait_queue(&s->remove_ok, &__wait);
799
800                 s->usbdev = NULL;
801                 s->overruns = 0;
802         }
803 }
804
805 static struct usb_device_id dabusb_ids [] = {
806         // { USB_DEVICE(0x0547, 0x2131) },      /* An2131 chip, no boot ROM */
807         { USB_DEVICE(0x0547, 0x9999) },
808         { }                                             /* Terminating entry */
809 };
810
811 MODULE_DEVICE_TABLE (usb, dabusb_ids);
812
813 static struct usb_driver dabusb_driver = {
814         .name =         "dabusb",
815         .probe =        dabusb_probe,
816         .disconnect =   dabusb_disconnect,
817         .id_table =     dabusb_ids,
818 };
819
820 /* --------------------------------------------------------------------- */
821
822 static int __init dabusb_init (void)
823 {
824         int retval;
825         unsigned u;
826
827         /* initialize struct */
828         for (u = 0; u < NRDABUSB; u++) {
829                 pdabusb_t s = &dabusb[u];
830                 memset (s, 0, sizeof (dabusb_t));
831                 mutex_init (&s->mutex);
832                 s->usbdev = NULL;
833                 s->total_buffer_size = buffers;
834                 init_waitqueue_head (&s->wait);
835                 init_waitqueue_head (&s->remove_ok);
836                 spin_lock_init (&s->lock);
837                 INIT_LIST_HEAD (&s->free_buff_list);
838                 INIT_LIST_HEAD (&s->rec_buff_list);
839         }
840
841         /* register misc device */
842         retval = usb_register(&dabusb_driver);
843         if (retval)
844                 goto out;
845
846         dbg("dabusb_init: driver registered");
847
848         info(DRIVER_VERSION ":" DRIVER_DESC);
849
850 out:
851         return retval;
852 }
853
854 static void __exit dabusb_cleanup (void)
855 {
856         dbg("dabusb_cleanup");
857
858         usb_deregister (&dabusb_driver);
859 }
860
861 /* --------------------------------------------------------------------- */
862
863 MODULE_AUTHOR( DRIVER_AUTHOR );
864 MODULE_DESCRIPTION( DRIVER_DESC );
865 MODULE_LICENSE("GPL");
866
867 module_param(buffers, int, 0);
868 MODULE_PARM_DESC (buffers, "Number of buffers (default=256)");
869
870 module_init (dabusb_init);
871 module_exit (dabusb_cleanup);
872
873 /* --------------------------------------------------------------------- */