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