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