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/smp_lock.h>
43 #include "dabfirmware.h"
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"
52 /* --------------------------------------------------------------------- */
54 #ifdef CONFIG_USB_DYNAMIC_MINORS
60 /*-------------------------------------------------------------------*/
62 static dabusb_t dabusb
[NRDABUSB
];
63 static int buffers
= 256;
64 static struct usb_driver dabusb_driver
;
66 /*-------------------------------------------------------------------*/
68 static int dabusb_add_buf_tail (pdabusb_t s
, struct list_head
*dst
, struct list_head
*src
)
71 struct list_head
*tmp
;
74 spin_lock_irqsave (&s
->lock
, flags
);
76 if (list_empty (src
)) {
77 // no elements in source buffer
82 list_move_tail (tmp
, dst
);
84 err
: spin_unlock_irqrestore (&s
->lock
, flags
);
87 /*-------------------------------------------------------------------*/
89 static void dump_urb (struct urb
*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
);
108 /*-------------------------------------------------------------------*/
109 static int dabusb_cancel_queue (pdabusb_t s
, struct list_head
*q
)
114 dbg("dabusb_cancel_queue");
116 spin_lock_irqsave (&s
->lock
, flags
);
118 list_for_each_entry(b
, q
, buff_list
) {
122 usb_unlink_urb (b
->purb
);
124 spin_unlock_irqrestore (&s
->lock
, flags
);
127 /*-------------------------------------------------------------------*/
128 static int dabusb_free_queue (struct list_head
*q
)
130 struct list_head
*tmp
;
134 dbg("dabusb_free_queue");
135 for (p
= q
->next
; p
!= q
;) {
136 b
= list_entry (p
, buff_t
, buff_list
);
141 kfree(b
->purb
->transfer_buffer
);
142 usb_free_urb(b
->purb
);
151 /*-------------------------------------------------------------------*/
152 static int dabusb_free_buffers (pdabusb_t s
)
155 dbg("dabusb_free_buffers");
157 spin_lock_irqsave(&s
->lock
, flags
);
159 dabusb_free_queue (&s
->free_buff_list
);
160 dabusb_free_queue (&s
->rec_buff_list
);
162 spin_unlock_irqrestore(&s
->lock
, flags
);
167 /*-------------------------------------------------------------------*/
168 static void dabusb_iso_complete (struct urb
*purb
, struct pt_regs
*regs
)
170 pbuff_t b
= purb
->context
;
175 void *buf
= purb
->transfer_buffer
;
177 dbg("dabusb_iso_complete");
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
);
191 err("dabusb_iso_complete: invalid len %d", len
);
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
);
199 if (atomic_dec_and_test (&s
->pending_io
) && !s
->remove_pending
&& s
->state
!= _stopped
) {
201 err("overrun (%d)", s
->overruns
);
205 /*-------------------------------------------------------------------*/
206 static int dabusb_alloc_buffers (pdabusb_t s
)
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
;
216 dbg("dabusb_alloc_buffers pipesize:%d packets:%d transfer_buffer_len:%d",
217 pipesize
, packets
, transfer_buffer_length
);
219 while (buffers
< (s
->total_buffer_size
<< 10)) {
220 b
= (pbuff_t
) kmalloc (sizeof (buff_t
), GFP_KERNEL
);
222 err("kmalloc(sizeof(buff_t))==NULL");
225 memset (b
, 0, sizeof (buff_t
));
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 buffers
+= transfer_buffer_length
;
256 list_add_tail (&b
->buff_list
, &s
->free_buff_list
);
258 s
->got_mem
= buffers
;
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
, unsigned char *data
, int len
)
304 unsigned char *transfer_buffer
= kmalloc (len
, GFP_KERNEL
);
306 if (!transfer_buffer
) {
307 err("dabusb_writemem: kmalloc(%d) failed.", len
);
311 memcpy (transfer_buffer
, data
, len
);
313 ret
=usb_control_msg(s
->usbdev
, usb_sndctrlpipe( s
->usbdev
, 0 ), 0xa0, 0x40, pos
, 0, transfer_buffer
, len
, 300);
315 kfree (transfer_buffer
);
318 /* --------------------------------------------------------------------- */
319 static int dabusb_8051_reset (pdabusb_t s
, unsigned char reset_bit
)
321 dbg("dabusb_8051_reset: %d",reset_bit
);
322 return dabusb_writemem (s
, CPUCS_REG
, &reset_bit
, 1);
324 /* --------------------------------------------------------------------- */
325 static int dabusb_loadmem (pdabusb_t s
, const char *fname
)
328 PINTEL_HEX_RECORD ptr
= firmware
;
330 dbg("Enter dabusb_loadmem (internal)");
332 ret
= dabusb_8051_reset (s
, 1);
333 while (ptr
->Type
== 0) {
335 dbg("dabusb_writemem: %04X %p %d)", ptr
->Address
, ptr
->Data
, ptr
->Length
);
337 ret
= dabusb_writemem (s
, ptr
->Address
, ptr
->Data
, ptr
->Length
);
339 err("dabusb_writemem failed (%d %04X %p %d)", ret
, ptr
->Address
, ptr
->Data
, ptr
->Length
);
344 ret
= dabusb_8051_reset (s
, 0);
346 dbg("dabusb_loadmem: exit");
350 /* --------------------------------------------------------------------- */
351 static int dabusb_fpga_clear (pdabusb_t s
, pbulk_transfer_t b
)
359 dbg("dabusb_fpga_clear");
361 return dabusb_bulk (s
, b
);
363 /* --------------------------------------------------------------------- */
364 static int dabusb_fpga_init (pdabusb_t s
, pbulk_transfer_t b
)
372 dbg("dabusb_fpga_init");
374 return dabusb_bulk (s
, b
);
376 /* --------------------------------------------------------------------- */
377 static int dabusb_fpga_download (pdabusb_t s
, const char *fname
)
379 pbulk_transfer_t b
= kmalloc (sizeof (bulk_transfer_t
), GFP_KERNEL
);
380 unsigned int blen
, n
;
382 unsigned char *buf
= bitstream
;
384 dbg("Enter dabusb_fpga_download (internal)");
387 err("kmalloc(sizeof(bulk_transfer_t))==NULL");
392 ret
= dabusb_fpga_clear (s
, b
);
394 blen
= buf
[73] + (buf
[72] << 8);
396 dbg("Bitstream len: %i", blen
);
403 for (n
= 0; n
<= blen
+ 60; n
+= 60) {
404 // some cclks for startup
406 memcpy (b
->data
+ 4, buf
+ 74 + n
, 60);
407 ret
= dabusb_bulk (s
, b
);
409 err("dabusb_bulk failed.");
415 ret
= dabusb_fpga_init (s
, b
);
418 dbg("exit dabusb_fpga_download");
423 static int dabusb_stop (pdabusb_t s
)
428 dabusb_cancel_queue (s
, &s
->rec_buff_list
);
430 dbg("pending_io: %d", s
->pending_io
.counter
);
432 s
->pending_io
.counter
= 0;
436 static int dabusb_startrek (pdabusb_t s
)
438 if (!s
->got_mem
&& s
->state
!= _started
) {
440 dbg("dabusb_startrek");
442 if (dabusb_alloc_buffers (s
) < 0)
449 if (!list_empty (&s
->free_buff_list
)) {
453 while (!dabusb_add_buf_tail (s
, &s
->rec_buff_list
, &s
->free_buff_list
)) {
455 dbg("submitting: end:%p s->rec_buff_list:%p", s
->rec_buff_list
.prev
, &s
->rec_buff_list
);
457 end
= list_entry (s
->rec_buff_list
.prev
, buff_t
, buff_list
);
459 ret
= usb_submit_urb (end
->purb
, GFP_KERNEL
);
461 err("usb_submit_urb returned:%d", ret
);
462 if (dabusb_add_buf_tail (s
, &s
->free_buff_list
, &s
->rec_buff_list
))
463 err("startrek: dabusb_add_buf_tail failed");
467 atomic_inc (&s
->pending_io
);
469 dbg("pending_io: %d",s
->pending_io
.counter
);
475 static ssize_t
dabusb_read (struct file
*file
, char __user
*buf
, size_t count
, loff_t
* ppos
)
477 pdabusb_t s
= (pdabusb_t
) file
->private_data
;
483 struct urb
*purb
= NULL
;
490 if (s
->remove_pending
)
500 spin_lock_irqsave (&s
->lock
, flags
);
502 if (list_empty (&s
->rec_buff_list
)) {
504 spin_unlock_irqrestore(&s
->lock
, flags
);
506 err("error: rec_buf_list is empty");
510 b
= list_entry (s
->rec_buff_list
.next
, buff_t
, buff_list
);
513 spin_unlock_irqrestore(&s
->lock
, flags
);
515 if (purb
->status
== -EINPROGRESS
) {
516 if (file
->f_flags
& O_NONBLOCK
) // return nonblocking
523 interruptible_sleep_on (&s
->wait
);
525 if (signal_pending (current
)) {
531 spin_lock_irqsave (&s
->lock
, flags
);
533 if (list_empty (&s
->rec_buff_list
)) {
534 spin_unlock_irqrestore(&s
->lock
, flags
);
535 err("error: still no buffer available.");
538 spin_unlock_irqrestore(&s
->lock
, flags
);
541 if (s
->remove_pending
) {
546 rem
= purb
->actual_length
- s
->readptr
; // set remaining bytes to copy
553 dbg("copy_to_user:%p %p %d",buf
, purb
->transfer_buffer
+ s
->readptr
, cnt
);
555 if (copy_to_user (buf
, purb
->transfer_buffer
+ s
->readptr
, cnt
)) {
556 err("read: copy_to_user failed");
567 if (s
->readptr
== purb
->actual_length
) {
568 // finished, take next buffer
569 if (dabusb_add_buf_tail (s
, &s
->free_buff_list
, &s
->rec_buff_list
))
570 err("read: dabusb_add_buf_tail failed");
574 err
: //up(&s->mutex);
578 static int dabusb_open (struct inode
*inode
, struct file
*file
)
580 int devnum
= iminor(inode
);
583 if (devnum
< DABUSB_MINOR
|| devnum
>= (DABUSB_MINOR
+ NRDABUSB
))
586 s
= &dabusb
[devnum
- DABUSB_MINOR
];
591 while (!s
->usbdev
|| s
->opened
) {
594 if (file
->f_flags
& O_NONBLOCK
) {
597 msleep_interruptible(500);
599 if (signal_pending (current
)) {
604 if (usb_set_interface (s
->usbdev
, _DABUSB_IF
, 1) < 0) {
606 err("set_interface failed");
613 file
->private_data
= s
;
615 return nonseekable_open(inode
, file
);
618 static int dabusb_release (struct inode
*inode
, struct file
*file
)
620 pdabusb_t s
= (pdabusb_t
) file
->private_data
;
622 dbg("dabusb_release");
626 dabusb_free_buffers (s
);
629 if (!s
->remove_pending
) {
630 if (usb_set_interface (s
->usbdev
, _DABUSB_IF
, 0) < 0)
631 err("set_interface failed");
634 wake_up (&s
->remove_ok
);
640 static int dabusb_ioctl (struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
642 pdabusb_t s
= (pdabusb_t
) file
->private_data
;
643 pbulk_transfer_t pbulk
;
645 int version
= DABUSB_VERSION
;
649 if (s
->remove_pending
)
662 pbulk
= (pbulk_transfer_t
) kmalloc (sizeof (bulk_transfer_t
), GFP_KERNEL
);
669 if (copy_from_user (pbulk
, (void __user
*) arg
, sizeof (bulk_transfer_t
))) {
675 ret
=dabusb_bulk (s
, pbulk
);
677 if (copy_to_user((void __user
*)arg
, pbulk
,
678 sizeof(bulk_transfer_t
)))
683 case IOCTL_DAB_OVERRUNS
:
684 ret
= put_user (s
->overruns
, (unsigned int __user
*) arg
);
687 case IOCTL_DAB_VERSION
:
688 ret
= put_user (version
, (unsigned int __user
*) arg
);
699 static struct file_operations dabusb_fops
=
701 .owner
= THIS_MODULE
,
704 .ioctl
= dabusb_ioctl
,
706 .release
= dabusb_release
,
709 static struct usb_class_driver dabusb_class
= {
710 .name
= "usb/dabusb%d",
711 .fops
= &dabusb_fops
,
712 .mode
= S_IFCHR
| S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
,
713 .minor_base
= DABUSB_MINOR
,
717 /* --------------------------------------------------------------------- */
718 static int dabusb_probe (struct usb_interface
*intf
,
719 const struct usb_device_id
*id
)
721 struct usb_device
*usbdev
= interface_to_usbdev(intf
);
725 dbg("dabusb: probe: vendor id 0x%x, device id 0x%x ifnum:%d",
726 le16_to_cpu(usbdev
->descriptor
.idVendor
),
727 le16_to_cpu(usbdev
->descriptor
.idProduct
),
728 intf
->altsetting
->desc
.bInterfaceNumber
);
730 /* We don't handle multiple configurations */
731 if (usbdev
->descriptor
.bNumConfigurations
!= 1)
734 if (intf
->altsetting
->desc
.bInterfaceNumber
!= _DABUSB_IF
&&
735 le16_to_cpu(usbdev
->descriptor
.idProduct
) == 0x9999)
740 s
= &dabusb
[intf
->minor
];
743 s
->remove_pending
= 0;
745 s
->devnum
= intf
->minor
;
747 if (usb_reset_configuration (usbdev
) < 0) {
748 err("reset_configuration failed");
751 if (le16_to_cpu(usbdev
->descriptor
.idProduct
) == 0x2131) {
752 dabusb_loadmem (s
, NULL
);
756 dabusb_fpga_download (s
, NULL
);
758 if (usb_set_interface (s
->usbdev
, _DABUSB_IF
, 0) < 0) {
759 err("set_interface failed");
763 dbg("bound to interface: %d", intf
->altsetting
->desc
.bInterfaceNumber
);
764 usb_set_intfdata (intf
, s
);
767 retval
= usb_register_dev(intf
, &dabusb_class
);
769 usb_set_intfdata (intf
, NULL
);
781 static void dabusb_disconnect (struct usb_interface
*intf
)
784 pdabusb_t s
= usb_get_intfdata (intf
);
786 dbg("dabusb_disconnect");
788 init_waitqueue_entry(&__wait
, current
);
790 usb_set_intfdata (intf
, NULL
);
792 usb_deregister_dev (intf
, &dabusb_class
);
793 s
->remove_pending
= 1;
795 add_wait_queue(&s
->remove_ok
, &__wait
);
796 set_current_state(TASK_UNINTERRUPTIBLE
);
797 if (s
->state
== _started
)
799 current
->state
= TASK_RUNNING
;
800 remove_wait_queue(&s
->remove_ok
, &__wait
);
807 static struct usb_device_id dabusb_ids
[] = {
808 // { USB_DEVICE(0x0547, 0x2131) }, /* An2131 chip, no boot ROM */
809 { USB_DEVICE(0x0547, 0x9999) },
810 { } /* Terminating entry */
813 MODULE_DEVICE_TABLE (usb
, dabusb_ids
);
815 static struct usb_driver dabusb_driver
= {
816 .owner
= THIS_MODULE
,
818 .probe
= dabusb_probe
,
819 .disconnect
= dabusb_disconnect
,
820 .id_table
= dabusb_ids
,
823 /* --------------------------------------------------------------------- */
825 static int __init
dabusb_init (void)
830 /* initialize struct */
831 for (u
= 0; u
< NRDABUSB
; u
++) {
832 pdabusb_t s
= &dabusb
[u
];
833 memset (s
, 0, sizeof (dabusb_t
));
834 init_MUTEX (&s
->mutex
);
836 s
->total_buffer_size
= buffers
;
837 init_waitqueue_head (&s
->wait
);
838 init_waitqueue_head (&s
->remove_ok
);
839 spin_lock_init (&s
->lock
);
840 INIT_LIST_HEAD (&s
->free_buff_list
);
841 INIT_LIST_HEAD (&s
->rec_buff_list
);
844 /* register misc device */
845 retval
= usb_register(&dabusb_driver
);
849 dbg("dabusb_init: driver registered");
851 info(DRIVER_VERSION
":" DRIVER_DESC
);
857 static void __exit
dabusb_cleanup (void)
859 dbg("dabusb_cleanup");
861 usb_deregister (&dabusb_driver
);
864 /* --------------------------------------------------------------------- */
866 MODULE_AUTHOR( DRIVER_AUTHOR
);
867 MODULE_DESCRIPTION( DRIVER_DESC
);
868 MODULE_LICENSE("GPL");
870 module_param(buffers
, int, 0);
871 MODULE_PARM_DESC (buffers
, "Number of buffers (default=256)");
873 module_init (dabusb_init
);
874 module_exit (dabusb_cleanup
);
876 /* --------------------------------------------------------------------- */