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> |
| 40 | #include <linux/smp_lock.h> |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 41 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | #include "dabusb.h" |
| 44 | #include "dabfirmware.h" |
| 45 | |
| 46 | /* |
| 47 | * Version Information |
| 48 | */ |
| 49 | #define DRIVER_VERSION "v1.54" |
| 50 | #define DRIVER_AUTHOR "Deti Fliegl, deti@fliegl.de" |
| 51 | #define DRIVER_DESC "DAB-USB Interface Driver for Linux (c)1999" |
| 52 | |
| 53 | /* --------------------------------------------------------------------- */ |
| 54 | |
| 55 | #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 | /*-------------------------------------------------------------------*/ |
| 169 | static void dabusb_iso_complete (struct urb *purb, struct pt_regs *regs) |
| 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 |
| 192 | err("dabusb_iso_complete: invalid len %d", len); |
| 193 | } |
| 194 | else |
| 195 | warn("dabusb_iso_complete: corrupted packet status: %d", purb->iso_frame_desc[i].status); |
| 196 | if (dst != purb->actual_length) |
| 197 | err("dst!=purb->actual_length:%d!=%d", dst, purb->actual_length); |
| 198 | } |
| 199 | |
| 200 | if (atomic_dec_and_test (&s->pending_io) && !s->remove_pending && s->state != _stopped) { |
| 201 | s->overruns++; |
| 202 | err("overrun (%d)", s->overruns); |
| 203 | } |
| 204 | wake_up (&s->wait); |
| 205 | } |
| 206 | /*-------------------------------------------------------------------*/ |
| 207 | static int dabusb_alloc_buffers (pdabusb_t s) |
| 208 | { |
| 209 | int buffers = 0; |
| 210 | pbuff_t b; |
| 211 | unsigned int pipe = usb_rcvisocpipe (s->usbdev, _DABUSB_ISOPIPE); |
| 212 | int pipesize = usb_maxpacket (s->usbdev, pipe, usb_pipeout (pipe)); |
| 213 | int packets = _ISOPIPESIZE / pipesize; |
| 214 | int transfer_buffer_length = packets * pipesize; |
| 215 | int i; |
| 216 | |
| 217 | dbg("dabusb_alloc_buffers pipesize:%d packets:%d transfer_buffer_len:%d", |
| 218 | pipesize, packets, transfer_buffer_length); |
| 219 | |
| 220 | while (buffers < (s->total_buffer_size << 10)) { |
Oliver Neukum | d8e298d | 2006-01-06 21:01:47 +0100 | [diff] [blame] | 221 | b = (pbuff_t) kzalloc (sizeof (buff_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | if (!b) { |
Oliver Neukum | d8e298d | 2006-01-06 21:01:47 +0100 | [diff] [blame] | 223 | err("kzalloc(sizeof(buff_t))==NULL"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | goto err; |
| 225 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | b->s = s; |
| 227 | b->purb = usb_alloc_urb(packets, GFP_KERNEL); |
| 228 | if (!b->purb) { |
| 229 | err("usb_alloc_urb == NULL"); |
| 230 | kfree (b); |
| 231 | goto err; |
| 232 | } |
| 233 | |
| 234 | b->purb->transfer_buffer = kmalloc (transfer_buffer_length, GFP_KERNEL); |
| 235 | if (!b->purb->transfer_buffer) { |
| 236 | kfree (b->purb); |
| 237 | kfree (b); |
| 238 | err("kmalloc(%d)==NULL", transfer_buffer_length); |
| 239 | goto err; |
| 240 | } |
| 241 | |
| 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; |
| 249 | |
| 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; |
| 253 | } |
| 254 | |
| 255 | buffers += transfer_buffer_length; |
| 256 | list_add_tail (&b->buff_list, &s->free_buff_list); |
| 257 | } |
| 258 | s->got_mem = buffers; |
| 259 | |
| 260 | return 0; |
| 261 | |
| 262 | err: |
| 263 | dabusb_free_buffers (s); |
| 264 | return -ENOMEM; |
| 265 | } |
| 266 | /*-------------------------------------------------------------------*/ |
| 267 | static int dabusb_bulk (pdabusb_t s, pbulk_transfer_t pb) |
| 268 | { |
| 269 | int ret; |
| 270 | unsigned int pipe; |
| 271 | int actual_length; |
| 272 | |
| 273 | dbg("dabusb_bulk"); |
| 274 | |
| 275 | if (!pb->pipe) |
| 276 | pipe = usb_rcvbulkpipe (s->usbdev, 2); |
| 277 | else |
| 278 | pipe = usb_sndbulkpipe (s->usbdev, 2); |
| 279 | |
| 280 | ret=usb_bulk_msg(s->usbdev, pipe, pb->data, pb->size, &actual_length, 100); |
| 281 | if(ret<0) { |
| 282 | err("dabusb: usb_bulk_msg failed(%d)",ret); |
| 283 | |
| 284 | if (usb_set_interface (s->usbdev, _DABUSB_IF, 1) < 0) { |
| 285 | err("set_interface failed"); |
| 286 | return -EINVAL; |
| 287 | } |
| 288 | |
| 289 | } |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame^] | 290 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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"); |
| 295 | } |
| 296 | |
| 297 | pb->size = actual_length; |
| 298 | return ret; |
| 299 | } |
| 300 | /* --------------------------------------------------------------------- */ |
| 301 | static int dabusb_writemem (pdabusb_t s, int pos, unsigned char *data, int len) |
| 302 | { |
| 303 | int ret; |
| 304 | unsigned char *transfer_buffer = kmalloc (len, GFP_KERNEL); |
| 305 | |
| 306 | if (!transfer_buffer) { |
| 307 | err("dabusb_writemem: kmalloc(%d) failed.", len); |
| 308 | return -ENOMEM; |
| 309 | } |
| 310 | |
| 311 | memcpy (transfer_buffer, data, len); |
| 312 | |
| 313 | ret=usb_control_msg(s->usbdev, usb_sndctrlpipe( s->usbdev, 0 ), 0xa0, 0x40, pos, 0, transfer_buffer, len, 300); |
| 314 | |
| 315 | kfree (transfer_buffer); |
| 316 | return ret; |
| 317 | } |
| 318 | /* --------------------------------------------------------------------- */ |
| 319 | static int dabusb_8051_reset (pdabusb_t s, unsigned char reset_bit) |
| 320 | { |
| 321 | dbg("dabusb_8051_reset: %d",reset_bit); |
| 322 | return dabusb_writemem (s, CPUCS_REG, &reset_bit, 1); |
| 323 | } |
| 324 | /* --------------------------------------------------------------------- */ |
| 325 | static int dabusb_loadmem (pdabusb_t s, const char *fname) |
| 326 | { |
| 327 | int ret; |
| 328 | PINTEL_HEX_RECORD ptr = firmware; |
| 329 | |
| 330 | dbg("Enter dabusb_loadmem (internal)"); |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame^] | 331 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | ret = dabusb_8051_reset (s, 1); |
| 333 | while (ptr->Type == 0) { |
| 334 | |
| 335 | dbg("dabusb_writemem: %04X %p %d)", ptr->Address, ptr->Data, ptr->Length); |
| 336 | |
| 337 | ret = dabusb_writemem (s, ptr->Address, ptr->Data, ptr->Length); |
| 338 | if (ret < 0) { |
| 339 | err("dabusb_writemem failed (%d %04X %p %d)", ret, ptr->Address, ptr->Data, ptr->Length); |
| 340 | break; |
| 341 | } |
| 342 | ptr++; |
| 343 | } |
| 344 | ret = dabusb_8051_reset (s, 0); |
| 345 | |
| 346 | dbg("dabusb_loadmem: exit"); |
| 347 | |
| 348 | return ret; |
| 349 | } |
| 350 | /* --------------------------------------------------------------------- */ |
| 351 | static int dabusb_fpga_clear (pdabusb_t s, pbulk_transfer_t b) |
| 352 | { |
| 353 | b->size = 4; |
| 354 | b->data[0] = 0x2a; |
| 355 | b->data[1] = 0; |
| 356 | b->data[2] = 0; |
| 357 | b->data[3] = 0; |
| 358 | |
| 359 | dbg("dabusb_fpga_clear"); |
| 360 | |
| 361 | return dabusb_bulk (s, b); |
| 362 | } |
| 363 | /* --------------------------------------------------------------------- */ |
| 364 | static int dabusb_fpga_init (pdabusb_t s, pbulk_transfer_t b) |
| 365 | { |
| 366 | b->size = 4; |
| 367 | b->data[0] = 0x2c; |
| 368 | b->data[1] = 0; |
| 369 | b->data[2] = 0; |
| 370 | b->data[3] = 0; |
| 371 | |
| 372 | dbg("dabusb_fpga_init"); |
| 373 | |
| 374 | return dabusb_bulk (s, b); |
| 375 | } |
| 376 | /* --------------------------------------------------------------------- */ |
| 377 | static int dabusb_fpga_download (pdabusb_t s, const char *fname) |
| 378 | { |
| 379 | pbulk_transfer_t b = kmalloc (sizeof (bulk_transfer_t), GFP_KERNEL); |
| 380 | unsigned int blen, n; |
| 381 | int ret; |
| 382 | unsigned char *buf = bitstream; |
| 383 | |
| 384 | dbg("Enter dabusb_fpga_download (internal)"); |
| 385 | |
| 386 | if (!b) { |
| 387 | err("kmalloc(sizeof(bulk_transfer_t))==NULL"); |
| 388 | return -ENOMEM; |
| 389 | } |
| 390 | |
| 391 | b->pipe = 1; |
| 392 | ret = dabusb_fpga_clear (s, b); |
| 393 | mdelay (10); |
| 394 | blen = buf[73] + (buf[72] << 8); |
| 395 | |
| 396 | dbg("Bitstream len: %i", blen); |
| 397 | |
| 398 | b->data[0] = 0x2b; |
| 399 | b->data[1] = 0; |
| 400 | b->data[2] = 0; |
| 401 | b->data[3] = 60; |
| 402 | |
| 403 | for (n = 0; n <= blen + 60; n += 60) { |
| 404 | // some cclks for startup |
| 405 | b->size = 64; |
| 406 | memcpy (b->data + 4, buf + 74 + n, 60); |
| 407 | ret = dabusb_bulk (s, b); |
| 408 | if (ret < 0) { |
| 409 | err("dabusb_bulk failed."); |
| 410 | break; |
| 411 | } |
| 412 | mdelay (1); |
| 413 | } |
| 414 | |
| 415 | ret = dabusb_fpga_init (s, b); |
| 416 | kfree (b); |
| 417 | |
| 418 | dbg("exit dabusb_fpga_download"); |
| 419 | |
| 420 | return ret; |
| 421 | } |
| 422 | |
| 423 | static int dabusb_stop (pdabusb_t s) |
| 424 | { |
| 425 | dbg("dabusb_stop"); |
| 426 | |
| 427 | s->state = _stopped; |
| 428 | dabusb_cancel_queue (s, &s->rec_buff_list); |
| 429 | |
| 430 | dbg("pending_io: %d", s->pending_io.counter); |
| 431 | |
| 432 | s->pending_io.counter = 0; |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | static int dabusb_startrek (pdabusb_t s) |
| 437 | { |
| 438 | if (!s->got_mem && s->state != _started) { |
| 439 | |
| 440 | dbg("dabusb_startrek"); |
| 441 | |
| 442 | if (dabusb_alloc_buffers (s) < 0) |
| 443 | return -ENOMEM; |
| 444 | dabusb_stop (s); |
| 445 | s->state = _started; |
| 446 | s->readptr = 0; |
| 447 | } |
| 448 | |
| 449 | if (!list_empty (&s->free_buff_list)) { |
| 450 | pbuff_t end; |
| 451 | int ret; |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame^] | 452 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | while (!dabusb_add_buf_tail (s, &s->rec_buff_list, &s->free_buff_list)) { |
| 454 | |
| 455 | dbg("submitting: end:%p s->rec_buff_list:%p", s->rec_buff_list.prev, &s->rec_buff_list); |
| 456 | |
| 457 | end = list_entry (s->rec_buff_list.prev, buff_t, buff_list); |
| 458 | |
| 459 | ret = usb_submit_urb (end->purb, GFP_KERNEL); |
| 460 | if (ret) { |
| 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"); |
| 464 | break; |
| 465 | } |
| 466 | else |
| 467 | atomic_inc (&s->pending_io); |
| 468 | } |
| 469 | dbg("pending_io: %d",s->pending_io.counter); |
| 470 | } |
| 471 | |
| 472 | return 0; |
| 473 | } |
| 474 | |
| 475 | static ssize_t dabusb_read (struct file *file, char __user *buf, size_t count, loff_t * ppos) |
| 476 | { |
| 477 | pdabusb_t s = (pdabusb_t) file->private_data; |
| 478 | unsigned long flags; |
| 479 | unsigned ret = 0; |
| 480 | int rem; |
| 481 | int cnt; |
| 482 | pbuff_t b; |
| 483 | struct urb *purb = NULL; |
| 484 | |
| 485 | dbg("dabusb_read"); |
| 486 | |
| 487 | if (*ppos) |
| 488 | return -ESPIPE; |
| 489 | |
| 490 | if (s->remove_pending) |
| 491 | return -EIO; |
| 492 | |
| 493 | |
| 494 | if (!s->usbdev) |
| 495 | return -EIO; |
| 496 | |
| 497 | while (count > 0) { |
| 498 | dabusb_startrek (s); |
| 499 | |
| 500 | spin_lock_irqsave (&s->lock, flags); |
| 501 | |
| 502 | if (list_empty (&s->rec_buff_list)) { |
| 503 | |
| 504 | spin_unlock_irqrestore(&s->lock, flags); |
| 505 | |
| 506 | err("error: rec_buf_list is empty"); |
| 507 | goto err; |
| 508 | } |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame^] | 509 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | b = list_entry (s->rec_buff_list.next, buff_t, buff_list); |
| 511 | purb = b->purb; |
| 512 | |
| 513 | spin_unlock_irqrestore(&s->lock, flags); |
| 514 | |
| 515 | if (purb->status == -EINPROGRESS) { |
| 516 | if (file->f_flags & O_NONBLOCK) // return nonblocking |
| 517 | { |
| 518 | if (!ret) |
| 519 | ret = -EAGAIN; |
| 520 | goto err; |
| 521 | } |
| 522 | |
| 523 | interruptible_sleep_on (&s->wait); |
| 524 | |
| 525 | if (signal_pending (current)) { |
| 526 | if (!ret) |
| 527 | ret = -ERESTARTSYS; |
| 528 | goto err; |
| 529 | } |
| 530 | |
| 531 | spin_lock_irqsave (&s->lock, flags); |
| 532 | |
| 533 | if (list_empty (&s->rec_buff_list)) { |
| 534 | spin_unlock_irqrestore(&s->lock, flags); |
| 535 | err("error: still no buffer available."); |
| 536 | goto err; |
| 537 | } |
| 538 | spin_unlock_irqrestore(&s->lock, flags); |
| 539 | s->readptr = 0; |
| 540 | } |
| 541 | if (s->remove_pending) { |
| 542 | ret = -EIO; |
| 543 | goto err; |
| 544 | } |
| 545 | |
| 546 | rem = purb->actual_length - s->readptr; // set remaining bytes to copy |
| 547 | |
| 548 | if (count >= rem) |
| 549 | cnt = rem; |
| 550 | else |
| 551 | cnt = count; |
| 552 | |
| 553 | dbg("copy_to_user:%p %p %d",buf, purb->transfer_buffer + s->readptr, cnt); |
| 554 | |
| 555 | if (copy_to_user (buf, purb->transfer_buffer + s->readptr, cnt)) { |
| 556 | err("read: copy_to_user failed"); |
| 557 | if (!ret) |
| 558 | ret = -EFAULT; |
| 559 | goto err; |
| 560 | } |
| 561 | |
| 562 | s->readptr += cnt; |
| 563 | count -= cnt; |
| 564 | buf += cnt; |
| 565 | ret += cnt; |
| 566 | |
| 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"); |
| 571 | s->readptr = 0; |
| 572 | } |
| 573 | } |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 574 | err: //mutex_unlock(&s->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | return ret; |
| 576 | } |
| 577 | |
| 578 | static int dabusb_open (struct inode *inode, struct file *file) |
| 579 | { |
| 580 | int devnum = iminor(inode); |
| 581 | pdabusb_t s; |
| 582 | |
| 583 | if (devnum < DABUSB_MINOR || devnum >= (DABUSB_MINOR + NRDABUSB)) |
| 584 | return -EIO; |
| 585 | |
| 586 | s = &dabusb[devnum - DABUSB_MINOR]; |
| 587 | |
| 588 | dbg("dabusb_open"); |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 589 | mutex_lock(&s->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | |
| 591 | while (!s->usbdev || s->opened) { |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 592 | mutex_unlock(&s->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | |
| 594 | if (file->f_flags & O_NONBLOCK) { |
| 595 | return -EBUSY; |
| 596 | } |
| 597 | msleep_interruptible(500); |
| 598 | |
| 599 | if (signal_pending (current)) { |
| 600 | return -EAGAIN; |
| 601 | } |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 602 | mutex_lock(&s->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | } |
| 604 | if (usb_set_interface (s->usbdev, _DABUSB_IF, 1) < 0) { |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 605 | mutex_unlock(&s->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | err("set_interface failed"); |
| 607 | return -EINVAL; |
| 608 | } |
| 609 | s->opened = 1; |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 610 | mutex_unlock(&s->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | |
| 612 | file->f_pos = 0; |
| 613 | file->private_data = s; |
| 614 | |
| 615 | return nonseekable_open(inode, file); |
| 616 | } |
| 617 | |
| 618 | static int dabusb_release (struct inode *inode, struct file *file) |
| 619 | { |
| 620 | pdabusb_t s = (pdabusb_t) file->private_data; |
| 621 | |
| 622 | dbg("dabusb_release"); |
| 623 | |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 624 | mutex_lock(&s->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | dabusb_stop (s); |
| 626 | dabusb_free_buffers (s); |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 627 | mutex_unlock(&s->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | |
| 629 | if (!s->remove_pending) { |
| 630 | if (usb_set_interface (s->usbdev, _DABUSB_IF, 0) < 0) |
| 631 | err("set_interface failed"); |
| 632 | } |
| 633 | else |
| 634 | wake_up (&s->remove_ok); |
| 635 | |
| 636 | s->opened = 0; |
| 637 | return 0; |
| 638 | } |
| 639 | |
| 640 | static int dabusb_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) |
| 641 | { |
| 642 | pdabusb_t s = (pdabusb_t) file->private_data; |
| 643 | pbulk_transfer_t pbulk; |
| 644 | int ret = 0; |
| 645 | int version = DABUSB_VERSION; |
| 646 | |
| 647 | dbg("dabusb_ioctl"); |
| 648 | |
| 649 | if (s->remove_pending) |
| 650 | return -EIO; |
| 651 | |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 652 | mutex_lock(&s->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | |
| 654 | if (!s->usbdev) { |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 655 | mutex_unlock(&s->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | return -EIO; |
| 657 | } |
| 658 | |
| 659 | switch (cmd) { |
| 660 | |
| 661 | case IOCTL_DAB_BULK: |
| 662 | pbulk = (pbulk_transfer_t) kmalloc (sizeof (bulk_transfer_t), GFP_KERNEL); |
| 663 | |
| 664 | if (!pbulk) { |
| 665 | ret = -ENOMEM; |
| 666 | break; |
| 667 | } |
| 668 | |
| 669 | if (copy_from_user (pbulk, (void __user *) arg, sizeof (bulk_transfer_t))) { |
| 670 | ret = -EFAULT; |
| 671 | kfree (pbulk); |
| 672 | break; |
| 673 | } |
| 674 | |
| 675 | ret=dabusb_bulk (s, pbulk); |
| 676 | if(ret==0) |
| 677 | if (copy_to_user((void __user *)arg, pbulk, |
| 678 | sizeof(bulk_transfer_t))) |
| 679 | ret = -EFAULT; |
| 680 | kfree (pbulk); |
| 681 | break; |
| 682 | |
| 683 | case IOCTL_DAB_OVERRUNS: |
| 684 | ret = put_user (s->overruns, (unsigned int __user *) arg); |
| 685 | break; |
| 686 | |
| 687 | case IOCTL_DAB_VERSION: |
| 688 | ret = put_user (version, (unsigned int __user *) arg); |
| 689 | break; |
| 690 | |
| 691 | default: |
| 692 | ret = -ENOIOCTLCMD; |
| 693 | break; |
| 694 | } |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 695 | mutex_unlock(&s->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | return ret; |
| 697 | } |
| 698 | |
| 699 | static struct file_operations dabusb_fops = |
| 700 | { |
| 701 | .owner = THIS_MODULE, |
| 702 | .llseek = no_llseek, |
| 703 | .read = dabusb_read, |
| 704 | .ioctl = dabusb_ioctl, |
| 705 | .open = dabusb_open, |
| 706 | .release = dabusb_release, |
| 707 | }; |
| 708 | |
| 709 | static struct usb_class_driver dabusb_class = { |
Greg Kroah-Hartman | d6e5bcf | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 710 | .name = "dabusb%d", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | .fops = &dabusb_fops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | .minor_base = DABUSB_MINOR, |
| 713 | }; |
| 714 | |
| 715 | |
| 716 | /* --------------------------------------------------------------------- */ |
| 717 | static int dabusb_probe (struct usb_interface *intf, |
| 718 | const struct usb_device_id *id) |
| 719 | { |
| 720 | struct usb_device *usbdev = interface_to_usbdev(intf); |
| 721 | int retval; |
| 722 | pdabusb_t s; |
| 723 | |
| 724 | dbg("dabusb: probe: vendor id 0x%x, device id 0x%x ifnum:%d", |
| 725 | le16_to_cpu(usbdev->descriptor.idVendor), |
| 726 | le16_to_cpu(usbdev->descriptor.idProduct), |
| 727 | intf->altsetting->desc.bInterfaceNumber); |
| 728 | |
| 729 | /* We don't handle multiple configurations */ |
| 730 | if (usbdev->descriptor.bNumConfigurations != 1) |
| 731 | return -ENODEV; |
| 732 | |
| 733 | if (intf->altsetting->desc.bInterfaceNumber != _DABUSB_IF && |
| 734 | le16_to_cpu(usbdev->descriptor.idProduct) == 0x9999) |
| 735 | return -ENODEV; |
| 736 | |
| 737 | |
| 738 | |
| 739 | s = &dabusb[intf->minor]; |
| 740 | |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 741 | mutex_lock(&s->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | s->remove_pending = 0; |
| 743 | s->usbdev = usbdev; |
| 744 | s->devnum = intf->minor; |
| 745 | |
| 746 | if (usb_reset_configuration (usbdev) < 0) { |
| 747 | err("reset_configuration failed"); |
| 748 | goto reject; |
| 749 | } |
| 750 | if (le16_to_cpu(usbdev->descriptor.idProduct) == 0x2131) { |
| 751 | dabusb_loadmem (s, NULL); |
| 752 | goto reject; |
| 753 | } |
| 754 | else { |
| 755 | dabusb_fpga_download (s, NULL); |
| 756 | |
| 757 | if (usb_set_interface (s->usbdev, _DABUSB_IF, 0) < 0) { |
| 758 | err("set_interface failed"); |
| 759 | goto reject; |
| 760 | } |
| 761 | } |
| 762 | dbg("bound to interface: %d", intf->altsetting->desc.bInterfaceNumber); |
| 763 | usb_set_intfdata (intf, s); |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 764 | mutex_unlock(&s->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | |
| 766 | retval = usb_register_dev(intf, &dabusb_class); |
| 767 | if (retval) { |
| 768 | usb_set_intfdata (intf, NULL); |
| 769 | return -ENOMEM; |
| 770 | } |
| 771 | |
| 772 | return 0; |
| 773 | |
| 774 | reject: |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 775 | mutex_unlock(&s->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | s->usbdev = NULL; |
| 777 | return -ENODEV; |
| 778 | } |
| 779 | |
| 780 | static void dabusb_disconnect (struct usb_interface *intf) |
| 781 | { |
| 782 | wait_queue_t __wait; |
| 783 | pdabusb_t s = usb_get_intfdata (intf); |
| 784 | |
| 785 | dbg("dabusb_disconnect"); |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame^] | 786 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | init_waitqueue_entry(&__wait, current); |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame^] | 788 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | usb_set_intfdata (intf, NULL); |
| 790 | if (s) { |
| 791 | usb_deregister_dev (intf, &dabusb_class); |
| 792 | s->remove_pending = 1; |
| 793 | wake_up (&s->wait); |
| 794 | add_wait_queue(&s->remove_ok, &__wait); |
| 795 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 796 | if (s->state == _started) |
| 797 | schedule(); |
| 798 | current->state = TASK_RUNNING; |
| 799 | remove_wait_queue(&s->remove_ok, &__wait); |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame^] | 800 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | s->usbdev = NULL; |
| 802 | s->overruns = 0; |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | static struct usb_device_id dabusb_ids [] = { |
| 807 | // { USB_DEVICE(0x0547, 0x2131) }, /* An2131 chip, no boot ROM */ |
| 808 | { USB_DEVICE(0x0547, 0x9999) }, |
| 809 | { } /* Terminating entry */ |
| 810 | }; |
| 811 | |
| 812 | MODULE_DEVICE_TABLE (usb, dabusb_ids); |
| 813 | |
| 814 | static struct usb_driver dabusb_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | .name = "dabusb", |
| 816 | .probe = dabusb_probe, |
| 817 | .disconnect = dabusb_disconnect, |
| 818 | .id_table = dabusb_ids, |
| 819 | }; |
| 820 | |
| 821 | /* --------------------------------------------------------------------- */ |
| 822 | |
| 823 | static int __init dabusb_init (void) |
| 824 | { |
| 825 | int retval; |
| 826 | unsigned u; |
| 827 | |
| 828 | /* initialize struct */ |
| 829 | for (u = 0; u < NRDABUSB; u++) { |
| 830 | pdabusb_t s = &dabusb[u]; |
| 831 | memset (s, 0, sizeof (dabusb_t)); |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 832 | mutex_init (&s->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | s->usbdev = NULL; |
| 834 | s->total_buffer_size = buffers; |
| 835 | init_waitqueue_head (&s->wait); |
| 836 | init_waitqueue_head (&s->remove_ok); |
| 837 | spin_lock_init (&s->lock); |
| 838 | INIT_LIST_HEAD (&s->free_buff_list); |
| 839 | INIT_LIST_HEAD (&s->rec_buff_list); |
| 840 | } |
| 841 | |
| 842 | /* register misc device */ |
| 843 | retval = usb_register(&dabusb_driver); |
| 844 | if (retval) |
| 845 | goto out; |
| 846 | |
| 847 | dbg("dabusb_init: driver registered"); |
| 848 | |
| 849 | info(DRIVER_VERSION ":" DRIVER_DESC); |
| 850 | |
| 851 | out: |
| 852 | return retval; |
| 853 | } |
| 854 | |
| 855 | static void __exit dabusb_cleanup (void) |
| 856 | { |
| 857 | dbg("dabusb_cleanup"); |
| 858 | |
| 859 | usb_deregister (&dabusb_driver); |
| 860 | } |
| 861 | |
| 862 | /* --------------------------------------------------------------------- */ |
| 863 | |
| 864 | MODULE_AUTHOR( DRIVER_AUTHOR ); |
| 865 | MODULE_DESCRIPTION( DRIVER_DESC ); |
| 866 | MODULE_LICENSE("GPL"); |
| 867 | |
| 868 | module_param(buffers, int, 0); |
| 869 | MODULE_PARM_DESC (buffers, "Number of buffers (default=256)"); |
| 870 | |
| 871 | module_init (dabusb_init); |
| 872 | module_exit (dabusb_cleanup); |
| 873 | |
| 874 | /* --------------------------------------------------------------------- */ |