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