1 /*****************************************************************************/
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/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 #include <linux/firmware.h>
42 #include <linux/ihex.h>
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"
53 /* --------------------------------------------------------------------- */
55 #ifdef CONFIG_USB_DYNAMIC_MINORS
61 /*-------------------------------------------------------------------*/
63 static dabusb_t dabusb[NRDABUSB];
64 static int buffers = 256;
65 static struct usb_driver dabusb_driver;
67 /*-------------------------------------------------------------------*/
69 static int dabusb_add_buf_tail (pdabusb_t s, struct list_head *dst, struct list_head *src)
72 struct list_head *tmp;
75 spin_lock_irqsave (&s->lock, flags);
77 if (list_empty (src)) {
78 // no elements in source buffer
83 list_move_tail (tmp, dst);
85 err: spin_unlock_irqrestore (&s->lock, flags);
88 /*-------------------------------------------------------------------*/
90 static void dump_urb (struct urb *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);
109 /*-------------------------------------------------------------------*/
110 static int dabusb_cancel_queue (pdabusb_t s, struct list_head *q)
115 dbg("dabusb_cancel_queue");
117 spin_lock_irqsave (&s->lock, flags);
119 list_for_each_entry(b, q, buff_list) {
123 usb_unlink_urb (b->purb);
125 spin_unlock_irqrestore (&s->lock, flags);
128 /*-------------------------------------------------------------------*/
129 static int dabusb_free_queue (struct list_head *q)
131 struct list_head *tmp;
135 dbg("dabusb_free_queue");
136 for (p = q->next; p != q;) {
137 b = list_entry (p, buff_t, buff_list);
142 kfree(b->purb->transfer_buffer);
143 usb_free_urb(b->purb);
152 /*-------------------------------------------------------------------*/
153 static int dabusb_free_buffers (pdabusb_t s)
156 dbg("dabusb_free_buffers");
158 spin_lock_irqsave(&s->lock, flags);
160 dabusb_free_queue (&s->free_buff_list);
161 dabusb_free_queue (&s->rec_buff_list);
163 spin_unlock_irqrestore(&s->lock, flags);
168 /*-------------------------------------------------------------------*/
169 static void dabusb_iso_complete (struct urb *purb)
171 pbuff_t b = purb->context;
176 void *buf = purb->transfer_buffer;
178 dbg("dabusb_iso_complete");
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);
192 err("dabusb_iso_complete: invalid len %d", len);
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);
200 if (atomic_dec_and_test (&s->pending_io) && !s->remove_pending && s->state != _stopped) {
202 err("overrun (%d)", s->overruns);
206 /*-------------------------------------------------------------------*/
207 static int dabusb_alloc_buffers (pdabusb_t s)
209 int transfer_len = 0;
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;
217 dbg("dabusb_alloc_buffers pipesize:%d packets:%d transfer_buffer_len:%d",
218 pipesize, packets, transfer_buffer_length);
220 while (transfer_len < (s->total_buffer_size << 10)) {
221 b = kzalloc(sizeof (buff_t), GFP_KERNEL);
223 err("kzalloc(sizeof(buff_t))==NULL");
227 b->purb = usb_alloc_urb(packets, GFP_KERNEL);
229 err("usb_alloc_urb == NULL");
234 b->purb->transfer_buffer = kmalloc (transfer_buffer_length, GFP_KERNEL);
235 if (!b->purb->transfer_buffer) {
238 err("kmalloc(%d)==NULL", transfer_buffer_length);
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;
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;
255 transfer_len += transfer_buffer_length;
256 list_add_tail (&b->buff_list, &s->free_buff_list);
258 s->got_mem = transfer_len;
263 dabusb_free_buffers (s);
266 /*-------------------------------------------------------------------*/
267 static int dabusb_bulk (pdabusb_t s, pbulk_transfer_t pb)
276 pipe = usb_rcvbulkpipe (s->usbdev, 2);
278 pipe = usb_sndbulkpipe (s->usbdev, 2);
280 ret=usb_bulk_msg(s->usbdev, pipe, pb->data, pb->size, &actual_length, 100);
282 err("dabusb: usb_bulk_msg failed(%d)",ret);
284 if (usb_set_interface (s->usbdev, _DABUSB_IF, 1) < 0) {
285 err("set_interface failed");
291 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");
297 pb->size = actual_length;
300 /* --------------------------------------------------------------------- */
301 static int dabusb_writemem (pdabusb_t s, int pos, const unsigned char *data,
305 unsigned char *transfer_buffer = kmalloc (len, GFP_KERNEL);
307 if (!transfer_buffer) {
308 err("dabusb_writemem: kmalloc(%d) failed.", len);
312 memcpy (transfer_buffer, data, len);
314 ret=usb_control_msg(s->usbdev, usb_sndctrlpipe( s->usbdev, 0 ), 0xa0, 0x40, pos, 0, transfer_buffer, len, 300);
316 kfree (transfer_buffer);
319 /* --------------------------------------------------------------------- */
320 static int dabusb_8051_reset (pdabusb_t s, unsigned char reset_bit)
322 dbg("dabusb_8051_reset: %d",reset_bit);
323 return dabusb_writemem (s, CPUCS_REG, &reset_bit, 1);
325 /* --------------------------------------------------------------------- */
326 static int dabusb_loadmem (pdabusb_t s, const char *fname)
329 const struct ihex_binrec *rec;
330 const struct firmware *fw;
332 dbg("Enter dabusb_loadmem (internal)");
334 ret = request_ihex_firmware(&fw, "dabusb/firmware.fw", &s->usbdev->dev);
336 err("Failed to load \"dabusb/firmware.fw\": %d\n", ret);
339 ret = dabusb_8051_reset (s, 1);
341 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));
346 ret = dabusb_writemem(s, be32_to_cpu(rec->addr), rec->data,
347 be16_to_cpu(rec->len));
349 err("dabusb_writemem failed (%d %04X %p %d)", ret,
350 be32_to_cpu(rec->addr), rec->data,
351 be16_to_cpu(rec->len));
355 ret = dabusb_8051_reset (s, 0);
356 release_firmware(fw);
358 dbg("dabusb_loadmem: exit");
362 /* --------------------------------------------------------------------- */
363 static int dabusb_fpga_clear (pdabusb_t s, pbulk_transfer_t b)
371 dbg("dabusb_fpga_clear");
373 return dabusb_bulk (s, b);
375 /* --------------------------------------------------------------------- */
376 static int dabusb_fpga_init (pdabusb_t s, pbulk_transfer_t b)
384 dbg("dabusb_fpga_init");
386 return dabusb_bulk (s, b);
388 /* --------------------------------------------------------------------- */
389 static int dabusb_fpga_download (pdabusb_t s, const char *fname)
391 pbulk_transfer_t b = kmalloc (sizeof (bulk_transfer_t), GFP_KERNEL);
392 const struct firmware *fw;
393 unsigned int blen, n;
396 dbg("Enter dabusb_fpga_download (internal)");
399 err("kmalloc(sizeof(bulk_transfer_t))==NULL");
403 ret = request_firmware(&fw, "dabusb/bitstream.bin", &s->usbdev->dev);
405 err("Failed to load \"dabusb/bitstream.bin\": %d\n", ret);
411 ret = dabusb_fpga_clear (s, b);
413 blen = fw->data[73] + (fw->data[72] << 8);
415 dbg("Bitstream len: %i", blen);
422 for (n = 0; n <= blen + 60; n += 60) {
423 // some cclks for startup
425 memcpy (b->data + 4, fw->data + 74 + n, 60);
426 ret = dabusb_bulk (s, b);
428 err("dabusb_bulk failed.");
434 ret = dabusb_fpga_init (s, b);
436 release_firmware(fw);
438 dbg("exit dabusb_fpga_download");
443 static int dabusb_stop (pdabusb_t s)
448 dabusb_cancel_queue (s, &s->rec_buff_list);
450 dbg("pending_io: %d", s->pending_io.counter);
452 s->pending_io.counter = 0;
456 static int dabusb_startrek (pdabusb_t s)
458 if (!s->got_mem && s->state != _started) {
460 dbg("dabusb_startrek");
462 if (dabusb_alloc_buffers (s) < 0)
469 if (!list_empty (&s->free_buff_list)) {
473 while (!dabusb_add_buf_tail (s, &s->rec_buff_list, &s->free_buff_list)) {
475 dbg("submitting: end:%p s->rec_buff_list:%p", s->rec_buff_list.prev, &s->rec_buff_list);
477 end = list_entry (s->rec_buff_list.prev, buff_t, buff_list);
479 ret = usb_submit_urb (end->purb, GFP_KERNEL);
481 err("usb_submit_urb returned:%d", ret);
482 if (dabusb_add_buf_tail (s, &s->free_buff_list, &s->rec_buff_list))
483 err("startrek: dabusb_add_buf_tail failed");
487 atomic_inc (&s->pending_io);
489 dbg("pending_io: %d",s->pending_io.counter);
495 static ssize_t dabusb_read (struct file *file, char __user *buf, size_t count, loff_t * ppos)
497 pdabusb_t s = (pdabusb_t) file->private_data;
503 struct urb *purb = NULL;
510 if (s->remove_pending)
520 spin_lock_irqsave (&s->lock, flags);
522 if (list_empty (&s->rec_buff_list)) {
524 spin_unlock_irqrestore(&s->lock, flags);
526 err("error: rec_buf_list is empty");
530 b = list_entry (s->rec_buff_list.next, buff_t, buff_list);
533 spin_unlock_irqrestore(&s->lock, flags);
535 if (purb->status == -EINPROGRESS) {
536 if (file->f_flags & O_NONBLOCK) // return nonblocking
543 interruptible_sleep_on (&s->wait);
545 if (signal_pending (current)) {
551 spin_lock_irqsave (&s->lock, flags);
553 if (list_empty (&s->rec_buff_list)) {
554 spin_unlock_irqrestore(&s->lock, flags);
555 err("error: still no buffer available.");
558 spin_unlock_irqrestore(&s->lock, flags);
561 if (s->remove_pending) {
566 rem = purb->actual_length - s->readptr; // set remaining bytes to copy
573 dbg("copy_to_user:%p %p %d",buf, purb->transfer_buffer + s->readptr, cnt);
575 if (copy_to_user (buf, purb->transfer_buffer + s->readptr, cnt)) {
576 err("read: copy_to_user failed");
587 if (s->readptr == purb->actual_length) {
588 // finished, take next buffer
589 if (dabusb_add_buf_tail (s, &s->free_buff_list, &s->rec_buff_list))
590 err("read: dabusb_add_buf_tail failed");
594 err: //mutex_unlock(&s->mutex);
598 static int dabusb_open (struct inode *inode, struct file *file)
600 int devnum = iminor(inode);
603 if (devnum < DABUSB_MINOR || devnum >= (DABUSB_MINOR + NRDABUSB))
606 s = &dabusb[devnum - DABUSB_MINOR];
609 mutex_lock(&s->mutex);
611 while (!s->usbdev || s->opened) {
612 mutex_unlock(&s->mutex);
614 if (file->f_flags & O_NONBLOCK) {
617 msleep_interruptible(500);
619 if (signal_pending (current)) {
622 mutex_lock(&s->mutex);
624 if (usb_set_interface (s->usbdev, _DABUSB_IF, 1) < 0) {
625 mutex_unlock(&s->mutex);
626 err("set_interface failed");
630 mutex_unlock(&s->mutex);
633 file->private_data = s;
635 return nonseekable_open(inode, file);
638 static int dabusb_release (struct inode *inode, struct file *file)
640 pdabusb_t s = (pdabusb_t) file->private_data;
642 dbg("dabusb_release");
644 mutex_lock(&s->mutex);
646 dabusb_free_buffers (s);
647 mutex_unlock(&s->mutex);
649 if (!s->remove_pending) {
650 if (usb_set_interface (s->usbdev, _DABUSB_IF, 0) < 0)
651 err("set_interface failed");
654 wake_up (&s->remove_ok);
660 static int dabusb_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
662 pdabusb_t s = (pdabusb_t) file->private_data;
663 pbulk_transfer_t pbulk;
665 int version = DABUSB_VERSION;
669 if (s->remove_pending)
672 mutex_lock(&s->mutex);
675 mutex_unlock(&s->mutex);
682 pbulk = kmalloc(sizeof (bulk_transfer_t), GFP_KERNEL);
689 if (copy_from_user (pbulk, (void __user *) arg, sizeof (bulk_transfer_t))) {
695 ret=dabusb_bulk (s, pbulk);
697 if (copy_to_user((void __user *)arg, pbulk,
698 sizeof(bulk_transfer_t)))
703 case IOCTL_DAB_OVERRUNS:
704 ret = put_user (s->overruns, (unsigned int __user *) arg);
707 case IOCTL_DAB_VERSION:
708 ret = put_user (version, (unsigned int __user *) arg);
715 mutex_unlock(&s->mutex);
719 static const struct file_operations dabusb_fops =
721 .owner = THIS_MODULE,
724 .ioctl = dabusb_ioctl,
726 .release = dabusb_release,
729 static struct usb_class_driver dabusb_class = {
731 .fops = &dabusb_fops,
732 .minor_base = DABUSB_MINOR,
736 /* --------------------------------------------------------------------- */
737 static int dabusb_probe (struct usb_interface *intf,
738 const struct usb_device_id *id)
740 struct usb_device *usbdev = interface_to_usbdev(intf);
744 dbg("dabusb: probe: vendor id 0x%x, device id 0x%x ifnum:%d",
745 le16_to_cpu(usbdev->descriptor.idVendor),
746 le16_to_cpu(usbdev->descriptor.idProduct),
747 intf->altsetting->desc.bInterfaceNumber);
749 /* We don't handle multiple configurations */
750 if (usbdev->descriptor.bNumConfigurations != 1)
753 if (intf->altsetting->desc.bInterfaceNumber != _DABUSB_IF &&
754 le16_to_cpu(usbdev->descriptor.idProduct) == 0x9999)
759 s = &dabusb[intf->minor];
761 mutex_lock(&s->mutex);
762 s->remove_pending = 0;
764 s->devnum = intf->minor;
766 if (usb_reset_configuration (usbdev) < 0) {
767 err("reset_configuration failed");
770 if (le16_to_cpu(usbdev->descriptor.idProduct) == 0x2131) {
771 dabusb_loadmem (s, NULL);
775 dabusb_fpga_download (s, NULL);
777 if (usb_set_interface (s->usbdev, _DABUSB_IF, 0) < 0) {
778 err("set_interface failed");
782 dbg("bound to interface: %d", intf->altsetting->desc.bInterfaceNumber);
783 usb_set_intfdata (intf, s);
784 mutex_unlock(&s->mutex);
786 retval = usb_register_dev(intf, &dabusb_class);
788 usb_set_intfdata (intf, NULL);
795 mutex_unlock(&s->mutex);
800 static void dabusb_disconnect (struct usb_interface *intf)
803 pdabusb_t s = usb_get_intfdata (intf);
805 dbg("dabusb_disconnect");
807 init_waitqueue_entry(&__wait, current);
809 usb_set_intfdata (intf, NULL);
811 usb_deregister_dev (intf, &dabusb_class);
812 s->remove_pending = 1;
814 add_wait_queue(&s->remove_ok, &__wait);
815 set_current_state(TASK_UNINTERRUPTIBLE);
816 if (s->state == _started)
818 current->state = TASK_RUNNING;
819 remove_wait_queue(&s->remove_ok, &__wait);
826 static struct usb_device_id dabusb_ids [] = {
827 // { USB_DEVICE(0x0547, 0x2131) }, /* An2131 chip, no boot ROM */
828 { USB_DEVICE(0x0547, 0x9999) },
829 { } /* Terminating entry */
832 MODULE_DEVICE_TABLE (usb, dabusb_ids);
834 static struct usb_driver dabusb_driver = {
836 .probe = dabusb_probe,
837 .disconnect = dabusb_disconnect,
838 .id_table = dabusb_ids,
841 /* --------------------------------------------------------------------- */
843 static int __init dabusb_init (void)
848 /* initialize struct */
849 for (u = 0; u < NRDABUSB; u++) {
850 pdabusb_t s = &dabusb[u];
851 memset (s, 0, sizeof (dabusb_t));
852 mutex_init (&s->mutex);
854 s->total_buffer_size = buffers;
855 init_waitqueue_head (&s->wait);
856 init_waitqueue_head (&s->remove_ok);
857 spin_lock_init (&s->lock);
858 INIT_LIST_HEAD (&s->free_buff_list);
859 INIT_LIST_HEAD (&s->rec_buff_list);
862 /* register misc device */
863 retval = usb_register(&dabusb_driver);
867 dbg("dabusb_init: driver registered");
869 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
876 static void __exit dabusb_cleanup (void)
878 dbg("dabusb_cleanup");
880 usb_deregister (&dabusb_driver);
883 /* --------------------------------------------------------------------- */
885 MODULE_AUTHOR( DRIVER_AUTHOR );
886 MODULE_DESCRIPTION( DRIVER_DESC );
887 MODULE_LICENSE("GPL");
889 module_param(buffers, int, 0);
890 MODULE_PARM_DESC (buffers, "Number of buffers (default=256)");
892 module_init (dabusb_init);
893 module_exit (dabusb_cleanup);
895 /* --------------------------------------------------------------------- */