1 /* 2 * TTUSB DEC Driver 3 * 4 * Copyright (C) 2003-2004 Alex Woods <linux-dvb@giblets.org> 5 * IR support by Peter Beutner <p.beutner@gmx.net> 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20 * 21 */ 22 23 #include <linux/list.h> 24 #include <linux/module.h> 25 #include <linux/pci.h> 26 #include <linux/slab.h> 27 #include <linux/spinlock.h> 28 #include <linux/usb.h> 29 #include <linux/interrupt.h> 30 #include <linux/firmware.h> 31 #include <linux/crc32.h> 32 #include <linux/init.h> 33 #include <linux/input.h> 34 35 #include <linux/mutex.h> 36 37 #include "dmxdev.h" 38 #include "dvb_demux.h" 39 #include "dvb_filter.h" 40 #include "dvb_frontend.h" 41 #include "dvb_net.h" 42 #include "ttusbdecfe.h" 43 44 static int debug; 45 static int output_pva; 46 static int enable_rc; 47 48 module_param(debug, int, 0644); 49 MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off)."); 50 module_param(output_pva, int, 0444); 51 MODULE_PARM_DESC(output_pva, "Output PVA from dvr device (default:off)"); 52 module_param(enable_rc, int, 0644); 53 MODULE_PARM_DESC(enable_rc, "Turn on/off IR remote control(default: off)"); 54 55 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr); 56 57 #define dprintk if (debug) printk 58 59 #define DRIVER_NAME "TechnoTrend/Hauppauge DEC USB" 60 61 #define COMMAND_PIPE 0x03 62 #define RESULT_PIPE 0x04 63 #define IN_PIPE 0x08 64 #define OUT_PIPE 0x07 65 #define IRQ_PIPE 0x0A 66 67 #define COMMAND_PACKET_SIZE 0x3c 68 #define ARM_PACKET_SIZE 0x1000 69 #define IRQ_PACKET_SIZE 0x8 70 71 #define ISO_BUF_COUNT 0x04 72 #define FRAMES_PER_ISO_BUF 0x04 73 #define ISO_FRAME_SIZE 0x0380 74 75 #define MAX_PVA_LENGTH 6144 76 77 enum ttusb_dec_model { 78 TTUSB_DEC2000T, 79 TTUSB_DEC2540T, 80 TTUSB_DEC3000S 81 }; 82 83 enum ttusb_dec_packet_type { 84 TTUSB_DEC_PACKET_PVA, 85 TTUSB_DEC_PACKET_SECTION, 86 TTUSB_DEC_PACKET_EMPTY 87 }; 88 89 enum ttusb_dec_interface { 90 TTUSB_DEC_INTERFACE_INITIAL, 91 TTUSB_DEC_INTERFACE_IN, 92 TTUSB_DEC_INTERFACE_OUT 93 }; 94 95 struct ttusb_dec { 96 enum ttusb_dec_model model; 97 char *model_name; 98 char *firmware_name; 99 int can_playback; 100 101 /* DVB bits */ 102 struct dvb_adapter adapter; 103 struct dmxdev dmxdev; 104 struct dvb_demux demux; 105 struct dmx_frontend frontend; 106 struct dvb_net dvb_net; 107 struct dvb_frontend* fe; 108 109 u16 pid[DMX_PES_OTHER]; 110 111 /* USB bits */ 112 struct usb_device *udev; 113 u8 trans_count; 114 unsigned int command_pipe; 115 unsigned int result_pipe; 116 unsigned int in_pipe; 117 unsigned int out_pipe; 118 unsigned int irq_pipe; 119 enum ttusb_dec_interface interface; 120 struct mutex usb_mutex; 121 122 void *irq_buffer; 123 struct urb *irq_urb; 124 dma_addr_t irq_dma_handle; 125 void *iso_buffer; 126 dma_addr_t iso_dma_handle; 127 struct urb *iso_urb[ISO_BUF_COUNT]; 128 int iso_stream_count; 129 struct mutex iso_mutex; 130 131 u8 packet[MAX_PVA_LENGTH + 4]; 132 enum ttusb_dec_packet_type packet_type; 133 int packet_state; 134 int packet_length; 135 int packet_payload_length; 136 u16 next_packet_id; 137 138 int pva_stream_count; 139 int filter_stream_count; 140 141 struct dvb_filter_pes2ts a_pes2ts; 142 struct dvb_filter_pes2ts v_pes2ts; 143 144 u8 v_pes[16 + MAX_PVA_LENGTH]; 145 int v_pes_length; 146 int v_pes_postbytes; 147 148 struct list_head urb_frame_list; 149 struct tasklet_struct urb_tasklet; 150 spinlock_t urb_frame_list_lock; 151 152 struct dvb_demux_filter *audio_filter; 153 struct dvb_demux_filter *video_filter; 154 struct list_head filter_info_list; 155 spinlock_t filter_info_list_lock; 156 157 struct input_dev *rc_input_dev; 158 char rc_phys[64]; 159 160 int active; /* Loaded successfully */ 161 }; 162 163 struct urb_frame { 164 u8 data[ISO_FRAME_SIZE]; 165 int length; 166 struct list_head urb_frame_list; 167 }; 168 169 struct filter_info { 170 u8 stream_id; 171 struct dvb_demux_filter *filter; 172 struct list_head filter_info_list; 173 }; 174 175 static u16 rc_keys[] = { 176 KEY_POWER, 177 KEY_MUTE, 178 KEY_1, 179 KEY_2, 180 KEY_3, 181 KEY_4, 182 KEY_5, 183 KEY_6, 184 KEY_7, 185 KEY_8, 186 KEY_9, 187 KEY_0, 188 KEY_CHANNELUP, 189 KEY_VOLUMEDOWN, 190 KEY_OK, 191 KEY_VOLUMEUP, 192 KEY_CHANNELDOWN, 193 KEY_PREVIOUS, 194 KEY_ESC, 195 KEY_RED, 196 KEY_GREEN, 197 KEY_YELLOW, 198 KEY_BLUE, 199 KEY_OPTION, 200 KEY_M, 201 KEY_RADIO 202 }; 203 204 static void ttusb_dec_set_model(struct ttusb_dec *dec, 205 enum ttusb_dec_model model); 206 207 static void ttusb_dec_handle_irq( struct urb *urb) 208 { 209 struct ttusb_dec * dec = urb->context; 210 char *buffer = dec->irq_buffer; 211 int retval; 212 213 switch(urb->status) { 214 case 0: /*success*/ 215 break; 216 case -ECONNRESET: 217 case -ENOENT: 218 case -ESHUTDOWN: 219 case -ETIME: 220 /* this urb is dead, cleanup */ 221 dprintk("%s:urb shutting down with status: %d\n", 222 __func__, urb->status); 223 return; 224 default: 225 dprintk("%s:nonzero status received: %d\n", 226 __func__,urb->status); 227 goto exit; 228 } 229 230 if( (buffer[0] == 0x1) && (buffer[2] == 0x15) ) { 231 /* IR - Event */ 232 /* this is an fact a bit too simple implementation; 233 * the box also reports a keyrepeat signal 234 * (with buffer[3] == 0x40) in an intervall of ~100ms. 235 * But to handle this correctly we had to imlemenent some 236 * kind of timer which signals a 'key up' event if no 237 * keyrepeat signal is received for lets say 200ms. 238 * this should/could be added later ... 239 * for now lets report each signal as a key down and up*/ 240 dprintk("%s:rc signal:%d\n", __func__, buffer[4]); 241 input_report_key(dec->rc_input_dev, rc_keys[buffer[4] - 1], 1); 242 input_sync(dec->rc_input_dev); 243 input_report_key(dec->rc_input_dev, rc_keys[buffer[4] - 1], 0); 244 input_sync(dec->rc_input_dev); 245 } 246 247 exit: retval = usb_submit_urb(urb, GFP_ATOMIC); 248 if(retval) 249 printk("%s - usb_commit_urb failed with result: %d\n", 250 __func__, retval); 251 } 252 253 static u16 crc16(u16 crc, const u8 *buf, size_t len) 254 { 255 u16 tmp; 256 257 while (len--) { 258 crc ^= *buf++; 259 crc ^= (u8)crc >> 4; 260 tmp = (u8)crc; 261 crc ^= (tmp ^ (tmp << 1)) << 4; 262 } 263 return crc; 264 } 265 266 static int ttusb_dec_send_command(struct ttusb_dec *dec, const u8 command, 267 int param_length, const u8 params[], 268 int *result_length, u8 cmd_result[]) 269 { 270 int result, actual_len, i; 271 u8 *b; 272 273 dprintk("%s\n", __func__); 274 275 b = kmalloc(COMMAND_PACKET_SIZE + 4, GFP_KERNEL); 276 if (!b) 277 return -ENOMEM; 278 279 if ((result = mutex_lock_interruptible(&dec->usb_mutex))) { 280 kfree(b); 281 printk("%s: Failed to lock usb mutex.\n", __func__); 282 return result; 283 } 284 285 b[0] = 0xaa; 286 b[1] = ++dec->trans_count; 287 b[2] = command; 288 b[3] = param_length; 289 290 if (params) 291 memcpy(&b[4], params, param_length); 292 293 if (debug) { 294 printk("%s: command: ", __func__); 295 for (i = 0; i < param_length + 4; i++) 296 printk("0x%02X ", b[i]); 297 printk("\n"); 298 } 299 300 result = usb_bulk_msg(dec->udev, dec->command_pipe, b, 301 COMMAND_PACKET_SIZE + 4, &actual_len, 1000); 302 303 if (result) { 304 printk("%s: command bulk message failed: error %d\n", 305 __func__, result); 306 mutex_unlock(&dec->usb_mutex); 307 kfree(b); 308 return result; 309 } 310 311 result = usb_bulk_msg(dec->udev, dec->result_pipe, b, 312 COMMAND_PACKET_SIZE + 4, &actual_len, 1000); 313 314 if (result) { 315 printk("%s: result bulk message failed: error %d\n", 316 __func__, result); 317 mutex_unlock(&dec->usb_mutex); 318 kfree(b); 319 return result; 320 } else { 321 if (debug) { 322 printk("%s: result: ", __func__); 323 for (i = 0; i < actual_len; i++) 324 printk("0x%02X ", b[i]); 325 printk("\n"); 326 } 327 328 if (result_length) 329 *result_length = b[3]; 330 if (cmd_result && b[3] > 0) 331 memcpy(cmd_result, &b[4], b[3]); 332 333 mutex_unlock(&dec->usb_mutex); 334 335 kfree(b); 336 return 0; 337 } 338 } 339 340 static int ttusb_dec_get_stb_state (struct ttusb_dec *dec, unsigned int *mode, 341 unsigned int *model, unsigned int *version) 342 { 343 u8 c[COMMAND_PACKET_SIZE]; 344 int c_length; 345 int result; 346 __be32 tmp; 347 348 dprintk("%s\n", __func__); 349 350 result = ttusb_dec_send_command(dec, 0x08, 0, NULL, &c_length, c); 351 if (result) 352 return result; 353 354 if (c_length >= 0x0c) { 355 if (mode != NULL) { 356 memcpy(&tmp, c, 4); 357 *mode = ntohl(tmp); 358 } 359 if (model != NULL) { 360 memcpy(&tmp, &c[4], 4); 361 *model = ntohl(tmp); 362 } 363 if (version != NULL) { 364 memcpy(&tmp, &c[8], 4); 365 *version = ntohl(tmp); 366 } 367 return 0; 368 } else { 369 return -ENOENT; 370 } 371 } 372 373 static int ttusb_dec_audio_pes2ts_cb(void *priv, unsigned char *data) 374 { 375 struct ttusb_dec *dec = priv; 376 377 dec->audio_filter->feed->cb.ts(data, 188, NULL, 0, 378 &dec->audio_filter->feed->feed.ts, 379 DMX_OK); 380 381 return 0; 382 } 383 384 static int ttusb_dec_video_pes2ts_cb(void *priv, unsigned char *data) 385 { 386 struct ttusb_dec *dec = priv; 387 388 dec->video_filter->feed->cb.ts(data, 188, NULL, 0, 389 &dec->video_filter->feed->feed.ts, 390 DMX_OK); 391 392 return 0; 393 } 394 395 static void ttusb_dec_set_pids(struct ttusb_dec *dec) 396 { 397 u8 b[] = { 0x00, 0x00, 0x00, 0x00, 398 0x00, 0x00, 0xff, 0xff, 399 0xff, 0xff, 0xff, 0xff }; 400 401 __be16 pcr = htons(dec->pid[DMX_PES_PCR]); 402 __be16 audio = htons(dec->pid[DMX_PES_AUDIO]); 403 __be16 video = htons(dec->pid[DMX_PES_VIDEO]); 404 405 dprintk("%s\n", __func__); 406 407 memcpy(&b[0], &pcr, 2); 408 memcpy(&b[2], &audio, 2); 409 memcpy(&b[4], &video, 2); 410 411 ttusb_dec_send_command(dec, 0x50, sizeof(b), b, NULL, NULL); 412 413 dvb_filter_pes2ts_init(&dec->a_pes2ts, dec->pid[DMX_PES_AUDIO], 414 ttusb_dec_audio_pes2ts_cb, dec); 415 dvb_filter_pes2ts_init(&dec->v_pes2ts, dec->pid[DMX_PES_VIDEO], 416 ttusb_dec_video_pes2ts_cb, dec); 417 dec->v_pes_length = 0; 418 dec->v_pes_postbytes = 0; 419 } 420 421 static void ttusb_dec_process_pva(struct ttusb_dec *dec, u8 *pva, int length) 422 { 423 if (length < 8) { 424 printk("%s: packet too short - discarding\n", __func__); 425 return; 426 } 427 428 if (length > 8 + MAX_PVA_LENGTH) { 429 printk("%s: packet too long - discarding\n", __func__); 430 return; 431 } 432 433 switch (pva[2]) { 434 435 case 0x01: { /* VideoStream */ 436 int prebytes = pva[5] & 0x03; 437 int postbytes = (pva[5] & 0x0c) >> 2; 438 __be16 v_pes_payload_length; 439 440 if (output_pva) { 441 dec->video_filter->feed->cb.ts(pva, length, NULL, 0, 442 &dec->video_filter->feed->feed.ts, DMX_OK); 443 return; 444 } 445 446 if (dec->v_pes_postbytes > 0 && 447 dec->v_pes_postbytes == prebytes) { 448 memcpy(&dec->v_pes[dec->v_pes_length], 449 &pva[12], prebytes); 450 451 dvb_filter_pes2ts(&dec->v_pes2ts, dec->v_pes, 452 dec->v_pes_length + prebytes, 1); 453 } 454 455 if (pva[5] & 0x10) { 456 dec->v_pes[7] = 0x80; 457 dec->v_pes[8] = 0x05; 458 459 dec->v_pes[9] = 0x21 | ((pva[8] & 0xc0) >> 5); 460 dec->v_pes[10] = ((pva[8] & 0x3f) << 2) | 461 ((pva[9] & 0xc0) >> 6); 462 dec->v_pes[11] = 0x01 | 463 ((pva[9] & 0x3f) << 2) | 464 ((pva[10] & 0x80) >> 6); 465 dec->v_pes[12] = ((pva[10] & 0x7f) << 1) | 466 ((pva[11] & 0xc0) >> 7); 467 dec->v_pes[13] = 0x01 | ((pva[11] & 0x7f) << 1); 468 469 memcpy(&dec->v_pes[14], &pva[12 + prebytes], 470 length - 12 - prebytes); 471 dec->v_pes_length = 14 + length - 12 - prebytes; 472 } else { 473 dec->v_pes[7] = 0x00; 474 dec->v_pes[8] = 0x00; 475 476 memcpy(&dec->v_pes[9], &pva[8], length - 8); 477 dec->v_pes_length = 9 + length - 8; 478 } 479 480 dec->v_pes_postbytes = postbytes; 481 482 if (dec->v_pes[9 + dec->v_pes[8]] == 0x00 && 483 dec->v_pes[10 + dec->v_pes[8]] == 0x00 && 484 dec->v_pes[11 + dec->v_pes[8]] == 0x01) 485 dec->v_pes[6] = 0x84; 486 else 487 dec->v_pes[6] = 0x80; 488 489 v_pes_payload_length = htons(dec->v_pes_length - 6 + 490 postbytes); 491 memcpy(&dec->v_pes[4], &v_pes_payload_length, 2); 492 493 if (postbytes == 0) 494 dvb_filter_pes2ts(&dec->v_pes2ts, dec->v_pes, 495 dec->v_pes_length, 1); 496 497 break; 498 } 499 500 case 0x02: /* MainAudioStream */ 501 if (output_pva) { 502 dec->audio_filter->feed->cb.ts(pva, length, NULL, 0, 503 &dec->audio_filter->feed->feed.ts, DMX_OK); 504 return; 505 } 506 507 dvb_filter_pes2ts(&dec->a_pes2ts, &pva[8], length - 8, 508 pva[5] & 0x10); 509 break; 510 511 default: 512 printk("%s: unknown PVA type: %02x.\n", __func__, 513 pva[2]); 514 break; 515 } 516 } 517 518 static void ttusb_dec_process_filter(struct ttusb_dec *dec, u8 *packet, 519 int length) 520 { 521 struct list_head *item; 522 struct filter_info *finfo; 523 struct dvb_demux_filter *filter = NULL; 524 unsigned long flags; 525 u8 sid; 526 527 sid = packet[1]; 528 spin_lock_irqsave(&dec->filter_info_list_lock, flags); 529 for (item = dec->filter_info_list.next; item != &dec->filter_info_list; 530 item = item->next) { 531 finfo = list_entry(item, struct filter_info, filter_info_list); 532 if (finfo->stream_id == sid) { 533 filter = finfo->filter; 534 break; 535 } 536 } 537 spin_unlock_irqrestore(&dec->filter_info_list_lock, flags); 538 539 if (filter) 540 filter->feed->cb.sec(&packet[2], length - 2, NULL, 0, 541 &filter->filter, DMX_OK); 542 } 543 544 static void ttusb_dec_process_packet(struct ttusb_dec *dec) 545 { 546 int i; 547 u16 csum = 0; 548 u16 packet_id; 549 550 if (dec->packet_length % 2) { 551 printk("%s: odd sized packet - discarding\n", __func__); 552 return; 553 } 554 555 for (i = 0; i < dec->packet_length; i += 2) 556 csum ^= ((dec->packet[i] << 8) + dec->packet[i + 1]); 557 558 if (csum) { 559 printk("%s: checksum failed - discarding\n", __func__); 560 return; 561 } 562 563 packet_id = dec->packet[dec->packet_length - 4] << 8; 564 packet_id += dec->packet[dec->packet_length - 3]; 565 566 if ((packet_id != dec->next_packet_id) && dec->next_packet_id) { 567 printk("%s: warning: lost packets between %u and %u\n", 568 __func__, dec->next_packet_id - 1, packet_id); 569 } 570 571 if (packet_id == 0xffff) 572 dec->next_packet_id = 0x8000; 573 else 574 dec->next_packet_id = packet_id + 1; 575 576 switch (dec->packet_type) { 577 case TTUSB_DEC_PACKET_PVA: 578 if (dec->pva_stream_count) 579 ttusb_dec_process_pva(dec, dec->packet, 580 dec->packet_payload_length); 581 break; 582 583 case TTUSB_DEC_PACKET_SECTION: 584 if (dec->filter_stream_count) 585 ttusb_dec_process_filter(dec, dec->packet, 586 dec->packet_payload_length); 587 break; 588 589 case TTUSB_DEC_PACKET_EMPTY: 590 break; 591 } 592 } 593 594 static void swap_bytes(u8 *b, int length) 595 { 596 length -= length % 2; 597 for (; length; b += 2, length -= 2) 598 swap(*b, *(b + 1)); 599 } 600 601 static void ttusb_dec_process_urb_frame(struct ttusb_dec *dec, u8 *b, 602 int length) 603 { 604 swap_bytes(b, length); 605 606 while (length) { 607 switch (dec->packet_state) { 608 609 case 0: 610 case 1: 611 case 2: 612 if (*b++ == 0xaa) 613 dec->packet_state++; 614 else 615 dec->packet_state = 0; 616 617 length--; 618 break; 619 620 case 3: 621 if (*b == 0x00) { 622 dec->packet_state++; 623 dec->packet_length = 0; 624 } else if (*b != 0xaa) { 625 dec->packet_state = 0; 626 } 627 628 b++; 629 length--; 630 break; 631 632 case 4: 633 dec->packet[dec->packet_length++] = *b++; 634 635 if (dec->packet_length == 2) { 636 if (dec->packet[0] == 'A' && 637 dec->packet[1] == 'V') { 638 dec->packet_type = 639 TTUSB_DEC_PACKET_PVA; 640 dec->packet_state++; 641 } else if (dec->packet[0] == 'S') { 642 dec->packet_type = 643 TTUSB_DEC_PACKET_SECTION; 644 dec->packet_state++; 645 } else if (dec->packet[0] == 0x00) { 646 dec->packet_type = 647 TTUSB_DEC_PACKET_EMPTY; 648 dec->packet_payload_length = 2; 649 dec->packet_state = 7; 650 } else { 651 printk("%s: unknown packet type: " 652 "%02x%02x\n", __func__, 653 dec->packet[0], dec->packet[1]); 654 dec->packet_state = 0; 655 } 656 } 657 658 length--; 659 break; 660 661 case 5: 662 dec->packet[dec->packet_length++] = *b++; 663 664 if (dec->packet_type == TTUSB_DEC_PACKET_PVA && 665 dec->packet_length == 8) { 666 dec->packet_state++; 667 dec->packet_payload_length = 8 + 668 (dec->packet[6] << 8) + 669 dec->packet[7]; 670 } else if (dec->packet_type == 671 TTUSB_DEC_PACKET_SECTION && 672 dec->packet_length == 5) { 673 dec->packet_state++; 674 dec->packet_payload_length = 5 + 675 ((dec->packet[3] & 0x0f) << 8) + 676 dec->packet[4]; 677 } 678 679 length--; 680 break; 681 682 case 6: { 683 int remainder = dec->packet_payload_length - 684 dec->packet_length; 685 686 if (length >= remainder) { 687 memcpy(dec->packet + dec->packet_length, 688 b, remainder); 689 dec->packet_length += remainder; 690 b += remainder; 691 length -= remainder; 692 dec->packet_state++; 693 } else { 694 memcpy(&dec->packet[dec->packet_length], 695 b, length); 696 dec->packet_length += length; 697 length = 0; 698 } 699 700 break; 701 } 702 703 case 7: { 704 int tail = 4; 705 706 dec->packet[dec->packet_length++] = *b++; 707 708 if (dec->packet_type == TTUSB_DEC_PACKET_SECTION && 709 dec->packet_payload_length % 2) 710 tail++; 711 712 if (dec->packet_length == 713 dec->packet_payload_length + tail) { 714 ttusb_dec_process_packet(dec); 715 dec->packet_state = 0; 716 } 717 718 length--; 719 break; 720 } 721 722 default: 723 printk("%s: illegal packet state encountered.\n", 724 __func__); 725 dec->packet_state = 0; 726 } 727 } 728 } 729 730 static void ttusb_dec_process_urb_frame_list(unsigned long data) 731 { 732 struct ttusb_dec *dec = (struct ttusb_dec *)data; 733 struct list_head *item; 734 struct urb_frame *frame; 735 unsigned long flags; 736 737 while (1) { 738 spin_lock_irqsave(&dec->urb_frame_list_lock, flags); 739 if ((item = dec->urb_frame_list.next) != &dec->urb_frame_list) { 740 frame = list_entry(item, struct urb_frame, 741 urb_frame_list); 742 list_del(&frame->urb_frame_list); 743 } else { 744 spin_unlock_irqrestore(&dec->urb_frame_list_lock, 745 flags); 746 return; 747 } 748 spin_unlock_irqrestore(&dec->urb_frame_list_lock, flags); 749 750 ttusb_dec_process_urb_frame(dec, frame->data, frame->length); 751 kfree(frame); 752 } 753 } 754 755 static void ttusb_dec_process_urb(struct urb *urb) 756 { 757 struct ttusb_dec *dec = urb->context; 758 759 if (!urb->status) { 760 int i; 761 762 for (i = 0; i < FRAMES_PER_ISO_BUF; i++) { 763 struct usb_iso_packet_descriptor *d; 764 u8 *b; 765 int length; 766 struct urb_frame *frame; 767 768 d = &urb->iso_frame_desc[i]; 769 b = urb->transfer_buffer + d->offset; 770 length = d->actual_length; 771 772 if ((frame = kmalloc(sizeof(struct urb_frame), 773 GFP_ATOMIC))) { 774 unsigned long flags; 775 776 memcpy(frame->data, b, length); 777 frame->length = length; 778 779 spin_lock_irqsave(&dec->urb_frame_list_lock, 780 flags); 781 list_add_tail(&frame->urb_frame_list, 782 &dec->urb_frame_list); 783 spin_unlock_irqrestore(&dec->urb_frame_list_lock, 784 flags); 785 786 tasklet_schedule(&dec->urb_tasklet); 787 } 788 } 789 } else { 790 /* -ENOENT is expected when unlinking urbs */ 791 if (urb->status != -ENOENT) 792 dprintk("%s: urb error: %d\n", __func__, 793 urb->status); 794 } 795 796 if (dec->iso_stream_count) 797 usb_submit_urb(urb, GFP_ATOMIC); 798 } 799 800 static void ttusb_dec_setup_urbs(struct ttusb_dec *dec) 801 { 802 int i, j, buffer_offset = 0; 803 804 dprintk("%s\n", __func__); 805 806 for (i = 0; i < ISO_BUF_COUNT; i++) { 807 int frame_offset = 0; 808 struct urb *urb = dec->iso_urb[i]; 809 810 urb->dev = dec->udev; 811 urb->context = dec; 812 urb->complete = ttusb_dec_process_urb; 813 urb->pipe = dec->in_pipe; 814 urb->transfer_flags = URB_ISO_ASAP; 815 urb->interval = 1; 816 urb->number_of_packets = FRAMES_PER_ISO_BUF; 817 urb->transfer_buffer_length = ISO_FRAME_SIZE * 818 FRAMES_PER_ISO_BUF; 819 urb->transfer_buffer = dec->iso_buffer + buffer_offset; 820 buffer_offset += ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF; 821 822 for (j = 0; j < FRAMES_PER_ISO_BUF; j++) { 823 urb->iso_frame_desc[j].offset = frame_offset; 824 urb->iso_frame_desc[j].length = ISO_FRAME_SIZE; 825 frame_offset += ISO_FRAME_SIZE; 826 } 827 } 828 } 829 830 static void ttusb_dec_stop_iso_xfer(struct ttusb_dec *dec) 831 { 832 int i; 833 834 dprintk("%s\n", __func__); 835 836 if (mutex_lock_interruptible(&dec->iso_mutex)) 837 return; 838 839 dec->iso_stream_count--; 840 841 if (!dec->iso_stream_count) { 842 for (i = 0; i < ISO_BUF_COUNT; i++) 843 usb_kill_urb(dec->iso_urb[i]); 844 } 845 846 mutex_unlock(&dec->iso_mutex); 847 } 848 849 /* Setting the interface of the DEC tends to take down the USB communications 850 * for a short period, so it's important not to call this function just before 851 * trying to talk to it. 852 */ 853 static int ttusb_dec_set_interface(struct ttusb_dec *dec, 854 enum ttusb_dec_interface interface) 855 { 856 int result = 0; 857 u8 b[] = { 0x05 }; 858 859 if (interface != dec->interface) { 860 switch (interface) { 861 case TTUSB_DEC_INTERFACE_INITIAL: 862 result = usb_set_interface(dec->udev, 0, 0); 863 break; 864 case TTUSB_DEC_INTERFACE_IN: 865 result = ttusb_dec_send_command(dec, 0x80, sizeof(b), 866 b, NULL, NULL); 867 if (result) 868 return result; 869 result = usb_set_interface(dec->udev, 0, 8); 870 break; 871 case TTUSB_DEC_INTERFACE_OUT: 872 result = usb_set_interface(dec->udev, 0, 1); 873 break; 874 } 875 876 if (result) 877 return result; 878 879 dec->interface = interface; 880 } 881 882 return 0; 883 } 884 885 static int ttusb_dec_start_iso_xfer(struct ttusb_dec *dec) 886 { 887 int i, result; 888 889 dprintk("%s\n", __func__); 890 891 if (mutex_lock_interruptible(&dec->iso_mutex)) 892 return -EAGAIN; 893 894 if (!dec->iso_stream_count) { 895 ttusb_dec_setup_urbs(dec); 896 897 dec->packet_state = 0; 898 dec->v_pes_postbytes = 0; 899 dec->next_packet_id = 0; 900 901 for (i = 0; i < ISO_BUF_COUNT; i++) { 902 if ((result = usb_submit_urb(dec->iso_urb[i], 903 GFP_ATOMIC))) { 904 printk("%s: failed urb submission %d: " 905 "error %d\n", __func__, i, result); 906 907 while (i) { 908 usb_kill_urb(dec->iso_urb[i - 1]); 909 i--; 910 } 911 912 mutex_unlock(&dec->iso_mutex); 913 return result; 914 } 915 } 916 } 917 918 dec->iso_stream_count++; 919 920 mutex_unlock(&dec->iso_mutex); 921 922 return 0; 923 } 924 925 static int ttusb_dec_start_ts_feed(struct dvb_demux_feed *dvbdmxfeed) 926 { 927 struct dvb_demux *dvbdmx = dvbdmxfeed->demux; 928 struct ttusb_dec *dec = dvbdmx->priv; 929 u8 b0[] = { 0x05 }; 930 int result = 0; 931 932 dprintk("%s\n", __func__); 933 934 dprintk(" ts_type:"); 935 936 if (dvbdmxfeed->ts_type & TS_DECODER) 937 dprintk(" TS_DECODER"); 938 939 if (dvbdmxfeed->ts_type & TS_PACKET) 940 dprintk(" TS_PACKET"); 941 942 if (dvbdmxfeed->ts_type & TS_PAYLOAD_ONLY) 943 dprintk(" TS_PAYLOAD_ONLY"); 944 945 dprintk("\n"); 946 947 switch (dvbdmxfeed->pes_type) { 948 949 case DMX_PES_VIDEO: 950 dprintk(" pes_type: DMX_PES_VIDEO\n"); 951 dec->pid[DMX_PES_PCR] = dvbdmxfeed->pid; 952 dec->pid[DMX_PES_VIDEO] = dvbdmxfeed->pid; 953 dec->video_filter = dvbdmxfeed->filter; 954 ttusb_dec_set_pids(dec); 955 break; 956 957 case DMX_PES_AUDIO: 958 dprintk(" pes_type: DMX_PES_AUDIO\n"); 959 dec->pid[DMX_PES_AUDIO] = dvbdmxfeed->pid; 960 dec->audio_filter = dvbdmxfeed->filter; 961 ttusb_dec_set_pids(dec); 962 break; 963 964 case DMX_PES_TELETEXT: 965 dec->pid[DMX_PES_TELETEXT] = dvbdmxfeed->pid; 966 dprintk(" pes_type: DMX_PES_TELETEXT(not supported)\n"); 967 return -ENOSYS; 968 969 case DMX_PES_PCR: 970 dprintk(" pes_type: DMX_PES_PCR\n"); 971 dec->pid[DMX_PES_PCR] = dvbdmxfeed->pid; 972 ttusb_dec_set_pids(dec); 973 break; 974 975 case DMX_PES_OTHER: 976 dprintk(" pes_type: DMX_PES_OTHER(not supported)\n"); 977 return -ENOSYS; 978 979 default: 980 dprintk(" pes_type: unknown (%d)\n", dvbdmxfeed->pes_type); 981 return -EINVAL; 982 983 } 984 985 result = ttusb_dec_send_command(dec, 0x80, sizeof(b0), b0, NULL, NULL); 986 if (result) 987 return result; 988 989 dec->pva_stream_count++; 990 return ttusb_dec_start_iso_xfer(dec); 991 } 992 993 static int ttusb_dec_start_sec_feed(struct dvb_demux_feed *dvbdmxfeed) 994 { 995 struct ttusb_dec *dec = dvbdmxfeed->demux->priv; 996 u8 b0[] = { 0x00, 0x00, 0x00, 0x01, 997 0x00, 0x00, 0x00, 0x00, 998 0x00, 0x00, 0x00, 0x00, 999 0x00, 0x00, 0x00, 0x00, 1000 0x00, 0xff, 0x00, 0x00, 1001 0x00, 0x00, 0x00, 0x00, 1002 0x00, 0x00, 0x00, 0x00, 1003 0x00 }; 1004 __be16 pid; 1005 u8 c[COMMAND_PACKET_SIZE]; 1006 int c_length; 1007 int result; 1008 struct filter_info *finfo; 1009 unsigned long flags; 1010 u8 x = 1; 1011 1012 dprintk("%s\n", __func__); 1013 1014 pid = htons(dvbdmxfeed->pid); 1015 memcpy(&b0[0], &pid, 2); 1016 memcpy(&b0[4], &x, 1); 1017 memcpy(&b0[5], &dvbdmxfeed->filter->filter.filter_value[0], 1); 1018 1019 result = ttusb_dec_send_command(dec, 0x60, sizeof(b0), b0, 1020 &c_length, c); 1021 1022 if (!result) { 1023 if (c_length == 2) { 1024 if (!(finfo = kmalloc(sizeof(struct filter_info), 1025 GFP_ATOMIC))) 1026 return -ENOMEM; 1027 1028 finfo->stream_id = c[1]; 1029 finfo->filter = dvbdmxfeed->filter; 1030 1031 spin_lock_irqsave(&dec->filter_info_list_lock, flags); 1032 list_add_tail(&finfo->filter_info_list, 1033 &dec->filter_info_list); 1034 spin_unlock_irqrestore(&dec->filter_info_list_lock, 1035 flags); 1036 1037 dvbdmxfeed->priv = finfo; 1038 1039 dec->filter_stream_count++; 1040 return ttusb_dec_start_iso_xfer(dec); 1041 } 1042 1043 return -EAGAIN; 1044 } else 1045 return result; 1046 } 1047 1048 static int ttusb_dec_start_feed(struct dvb_demux_feed *dvbdmxfeed) 1049 { 1050 struct dvb_demux *dvbdmx = dvbdmxfeed->demux; 1051 1052 dprintk("%s\n", __func__); 1053 1054 if (!dvbdmx->dmx.frontend) 1055 return -EINVAL; 1056 1057 dprintk(" pid: 0x%04X\n", dvbdmxfeed->pid); 1058 1059 switch (dvbdmxfeed->type) { 1060 1061 case DMX_TYPE_TS: 1062 return ttusb_dec_start_ts_feed(dvbdmxfeed); 1063 break; 1064 1065 case DMX_TYPE_SEC: 1066 return ttusb_dec_start_sec_feed(dvbdmxfeed); 1067 break; 1068 1069 default: 1070 dprintk(" type: unknown (%d)\n", dvbdmxfeed->type); 1071 return -EINVAL; 1072 1073 } 1074 } 1075 1076 static int ttusb_dec_stop_ts_feed(struct dvb_demux_feed *dvbdmxfeed) 1077 { 1078 struct ttusb_dec *dec = dvbdmxfeed->demux->priv; 1079 u8 b0[] = { 0x00 }; 1080 1081 ttusb_dec_send_command(dec, 0x81, sizeof(b0), b0, NULL, NULL); 1082 1083 dec->pva_stream_count--; 1084 1085 ttusb_dec_stop_iso_xfer(dec); 1086 1087 return 0; 1088 } 1089 1090 static int ttusb_dec_stop_sec_feed(struct dvb_demux_feed *dvbdmxfeed) 1091 { 1092 struct ttusb_dec *dec = dvbdmxfeed->demux->priv; 1093 u8 b0[] = { 0x00, 0x00 }; 1094 struct filter_info *finfo = (struct filter_info *)dvbdmxfeed->priv; 1095 unsigned long flags; 1096 1097 b0[1] = finfo->stream_id; 1098 spin_lock_irqsave(&dec->filter_info_list_lock, flags); 1099 list_del(&finfo->filter_info_list); 1100 spin_unlock_irqrestore(&dec->filter_info_list_lock, flags); 1101 kfree(finfo); 1102 ttusb_dec_send_command(dec, 0x62, sizeof(b0), b0, NULL, NULL); 1103 1104 dec->filter_stream_count--; 1105 1106 ttusb_dec_stop_iso_xfer(dec); 1107 1108 return 0; 1109 } 1110 1111 static int ttusb_dec_stop_feed(struct dvb_demux_feed *dvbdmxfeed) 1112 { 1113 dprintk("%s\n", __func__); 1114 1115 switch (dvbdmxfeed->type) { 1116 case DMX_TYPE_TS: 1117 return ttusb_dec_stop_ts_feed(dvbdmxfeed); 1118 break; 1119 1120 case DMX_TYPE_SEC: 1121 return ttusb_dec_stop_sec_feed(dvbdmxfeed); 1122 break; 1123 } 1124 1125 return 0; 1126 } 1127 1128 static void ttusb_dec_free_iso_urbs(struct ttusb_dec *dec) 1129 { 1130 int i; 1131 1132 dprintk("%s\n", __func__); 1133 1134 for (i = 0; i < ISO_BUF_COUNT; i++) 1135 usb_free_urb(dec->iso_urb[i]); 1136 1137 pci_free_consistent(NULL, 1138 ISO_FRAME_SIZE * (FRAMES_PER_ISO_BUF * 1139 ISO_BUF_COUNT), 1140 dec->iso_buffer, dec->iso_dma_handle); 1141 } 1142 1143 static int ttusb_dec_alloc_iso_urbs(struct ttusb_dec *dec) 1144 { 1145 int i; 1146 1147 dprintk("%s\n", __func__); 1148 1149 dec->iso_buffer = pci_zalloc_consistent(NULL, 1150 ISO_FRAME_SIZE * (FRAMES_PER_ISO_BUF * ISO_BUF_COUNT), 1151 &dec->iso_dma_handle); 1152 1153 if (!dec->iso_buffer) { 1154 dprintk("%s: pci_alloc_consistent - not enough memory\n", 1155 __func__); 1156 return -ENOMEM; 1157 } 1158 1159 for (i = 0; i < ISO_BUF_COUNT; i++) { 1160 struct urb *urb; 1161 1162 if (!(urb = usb_alloc_urb(FRAMES_PER_ISO_BUF, GFP_ATOMIC))) { 1163 ttusb_dec_free_iso_urbs(dec); 1164 return -ENOMEM; 1165 } 1166 1167 dec->iso_urb[i] = urb; 1168 } 1169 1170 ttusb_dec_setup_urbs(dec); 1171 1172 return 0; 1173 } 1174 1175 static void ttusb_dec_init_tasklet(struct ttusb_dec *dec) 1176 { 1177 spin_lock_init(&dec->urb_frame_list_lock); 1178 INIT_LIST_HEAD(&dec->urb_frame_list); 1179 tasklet_init(&dec->urb_tasklet, ttusb_dec_process_urb_frame_list, 1180 (unsigned long)dec); 1181 } 1182 1183 static int ttusb_init_rc( struct ttusb_dec *dec) 1184 { 1185 struct input_dev *input_dev; 1186 u8 b[] = { 0x00, 0x01 }; 1187 int i; 1188 int err; 1189 1190 usb_make_path(dec->udev, dec->rc_phys, sizeof(dec->rc_phys)); 1191 strlcat(dec->rc_phys, "/input0", sizeof(dec->rc_phys)); 1192 1193 input_dev = input_allocate_device(); 1194 if (!input_dev) 1195 return -ENOMEM; 1196 1197 input_dev->name = "ttusb_dec remote control"; 1198 input_dev->phys = dec->rc_phys; 1199 input_dev->evbit[0] = BIT_MASK(EV_KEY); 1200 input_dev->keycodesize = sizeof(u16); 1201 input_dev->keycodemax = 0x1a; 1202 input_dev->keycode = rc_keys; 1203 1204 for (i = 0; i < ARRAY_SIZE(rc_keys); i++) 1205 set_bit(rc_keys[i], input_dev->keybit); 1206 1207 err = input_register_device(input_dev); 1208 if (err) { 1209 input_free_device(input_dev); 1210 return err; 1211 } 1212 1213 dec->rc_input_dev = input_dev; 1214 if (usb_submit_urb(dec->irq_urb, GFP_KERNEL)) 1215 printk("%s: usb_submit_urb failed\n",__func__); 1216 /* enable irq pipe */ 1217 ttusb_dec_send_command(dec,0xb0,sizeof(b),b,NULL,NULL); 1218 1219 return 0; 1220 } 1221 1222 static void ttusb_dec_init_v_pes(struct ttusb_dec *dec) 1223 { 1224 dprintk("%s\n", __func__); 1225 1226 dec->v_pes[0] = 0x00; 1227 dec->v_pes[1] = 0x00; 1228 dec->v_pes[2] = 0x01; 1229 dec->v_pes[3] = 0xe0; 1230 } 1231 1232 static int ttusb_dec_init_usb(struct ttusb_dec *dec) 1233 { 1234 int result; 1235 1236 dprintk("%s\n", __func__); 1237 1238 mutex_init(&dec->usb_mutex); 1239 mutex_init(&dec->iso_mutex); 1240 1241 dec->command_pipe = usb_sndbulkpipe(dec->udev, COMMAND_PIPE); 1242 dec->result_pipe = usb_rcvbulkpipe(dec->udev, RESULT_PIPE); 1243 dec->in_pipe = usb_rcvisocpipe(dec->udev, IN_PIPE); 1244 dec->out_pipe = usb_sndisocpipe(dec->udev, OUT_PIPE); 1245 dec->irq_pipe = usb_rcvintpipe(dec->udev, IRQ_PIPE); 1246 1247 if(enable_rc) { 1248 dec->irq_urb = usb_alloc_urb(0, GFP_KERNEL); 1249 if(!dec->irq_urb) { 1250 return -ENOMEM; 1251 } 1252 dec->irq_buffer = usb_alloc_coherent(dec->udev,IRQ_PACKET_SIZE, 1253 GFP_KERNEL, &dec->irq_dma_handle); 1254 if(!dec->irq_buffer) { 1255 usb_free_urb(dec->irq_urb); 1256 return -ENOMEM; 1257 } 1258 usb_fill_int_urb(dec->irq_urb, dec->udev,dec->irq_pipe, 1259 dec->irq_buffer, IRQ_PACKET_SIZE, 1260 ttusb_dec_handle_irq, dec, 1); 1261 dec->irq_urb->transfer_dma = dec->irq_dma_handle; 1262 dec->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 1263 } 1264 1265 result = ttusb_dec_alloc_iso_urbs(dec); 1266 if (result) { 1267 usb_free_urb(dec->irq_urb); 1268 usb_free_coherent(dec->udev, IRQ_PACKET_SIZE, 1269 dec->irq_buffer, dec->irq_dma_handle); 1270 } 1271 return result; 1272 } 1273 1274 static int ttusb_dec_boot_dsp(struct ttusb_dec *dec) 1275 { 1276 int i, j, actual_len, result, size, trans_count; 1277 u8 b0[] = { 0x00, 0x00, 0x00, 0x00, 1278 0x00, 0x00, 0x00, 0x00, 1279 0x61, 0x00 }; 1280 u8 b1[] = { 0x61 }; 1281 u8 *b; 1282 char idstring[21]; 1283 const u8 *firmware = NULL; 1284 size_t firmware_size = 0; 1285 u16 firmware_csum = 0; 1286 __be16 firmware_csum_ns; 1287 __be32 firmware_size_nl; 1288 u32 crc32_csum, crc32_check; 1289 __be32 tmp; 1290 const struct firmware *fw_entry = NULL; 1291 1292 dprintk("%s\n", __func__); 1293 1294 result = request_firmware(&fw_entry, dec->firmware_name, &dec->udev->dev); 1295 if (result) { 1296 printk(KERN_ERR "%s: Firmware (%s) unavailable.\n", 1297 __func__, dec->firmware_name); 1298 return result; 1299 } 1300 1301 firmware = fw_entry->data; 1302 firmware_size = fw_entry->size; 1303 1304 if (firmware_size < 60) { 1305 printk("%s: firmware size too small for DSP code (%zu < 60).\n", 1306 __func__, firmware_size); 1307 release_firmware(fw_entry); 1308 return -ENOENT; 1309 } 1310 1311 /* a 32 bit checksum over the first 56 bytes of the DSP Code is stored 1312 at offset 56 of file, so use it to check if the firmware file is 1313 valid. */ 1314 crc32_csum = crc32(~0L, firmware, 56) ^ ~0L; 1315 memcpy(&tmp, &firmware[56], 4); 1316 crc32_check = ntohl(tmp); 1317 if (crc32_csum != crc32_check) { 1318 printk("%s: crc32 check of DSP code failed (calculated " 1319 "0x%08x != 0x%08x in file), file invalid.\n", 1320 __func__, crc32_csum, crc32_check); 1321 release_firmware(fw_entry); 1322 return -ENOENT; 1323 } 1324 memcpy(idstring, &firmware[36], 20); 1325 idstring[20] = '\0'; 1326 printk(KERN_INFO "ttusb_dec: found DSP code \"%s\".\n", idstring); 1327 1328 firmware_size_nl = htonl(firmware_size); 1329 memcpy(b0, &firmware_size_nl, 4); 1330 firmware_csum = crc16(~0, firmware, firmware_size) ^ ~0; 1331 firmware_csum_ns = htons(firmware_csum); 1332 memcpy(&b0[6], &firmware_csum_ns, 2); 1333 1334 result = ttusb_dec_send_command(dec, 0x41, sizeof(b0), b0, NULL, NULL); 1335 1336 if (result) { 1337 release_firmware(fw_entry); 1338 return result; 1339 } 1340 1341 trans_count = 0; 1342 j = 0; 1343 1344 b = kmalloc(ARM_PACKET_SIZE, GFP_KERNEL); 1345 if (b == NULL) { 1346 release_firmware(fw_entry); 1347 return -ENOMEM; 1348 } 1349 1350 for (i = 0; i < firmware_size; i += COMMAND_PACKET_SIZE) { 1351 size = firmware_size - i; 1352 if (size > COMMAND_PACKET_SIZE) 1353 size = COMMAND_PACKET_SIZE; 1354 1355 b[j + 0] = 0xaa; 1356 b[j + 1] = trans_count++; 1357 b[j + 2] = 0xf0; 1358 b[j + 3] = size; 1359 memcpy(&b[j + 4], &firmware[i], size); 1360 1361 j += COMMAND_PACKET_SIZE + 4; 1362 1363 if (j >= ARM_PACKET_SIZE) { 1364 result = usb_bulk_msg(dec->udev, dec->command_pipe, b, 1365 ARM_PACKET_SIZE, &actual_len, 1366 100); 1367 j = 0; 1368 } else if (size < COMMAND_PACKET_SIZE) { 1369 result = usb_bulk_msg(dec->udev, dec->command_pipe, b, 1370 j - COMMAND_PACKET_SIZE + size, 1371 &actual_len, 100); 1372 } 1373 } 1374 1375 result = ttusb_dec_send_command(dec, 0x43, sizeof(b1), b1, NULL, NULL); 1376 1377 release_firmware(fw_entry); 1378 kfree(b); 1379 1380 return result; 1381 } 1382 1383 static int ttusb_dec_init_stb(struct ttusb_dec *dec) 1384 { 1385 int result; 1386 unsigned int mode = 0, model = 0, version = 0; 1387 1388 dprintk("%s\n", __func__); 1389 1390 result = ttusb_dec_get_stb_state(dec, &mode, &model, &version); 1391 if (result) 1392 return result; 1393 1394 if (!mode) { 1395 if (version == 0xABCDEFAB) 1396 printk(KERN_INFO "ttusb_dec: no version " 1397 "info in Firmware\n"); 1398 else 1399 printk(KERN_INFO "ttusb_dec: Firmware " 1400 "%x.%02x%c%c\n", 1401 version >> 24, (version >> 16) & 0xff, 1402 (version >> 8) & 0xff, version & 0xff); 1403 1404 result = ttusb_dec_boot_dsp(dec); 1405 if (result) 1406 return result; 1407 } else { 1408 /* We can't trust the USB IDs that some firmwares 1409 give the box */ 1410 switch (model) { 1411 case 0x00070001: 1412 case 0x00070008: 1413 case 0x0007000c: 1414 ttusb_dec_set_model(dec, TTUSB_DEC3000S); 1415 break; 1416 case 0x00070009: 1417 case 0x00070013: 1418 ttusb_dec_set_model(dec, TTUSB_DEC2000T); 1419 break; 1420 case 0x00070011: 1421 ttusb_dec_set_model(dec, TTUSB_DEC2540T); 1422 break; 1423 default: 1424 printk(KERN_ERR "%s: unknown model returned " 1425 "by firmware (%08x) - please report\n", 1426 __func__, model); 1427 return -ENOENT; 1428 } 1429 if (version >= 0x01770000) 1430 dec->can_playback = 1; 1431 } 1432 return 0; 1433 } 1434 1435 static int ttusb_dec_init_dvb(struct ttusb_dec *dec) 1436 { 1437 int result; 1438 1439 dprintk("%s\n", __func__); 1440 1441 if ((result = dvb_register_adapter(&dec->adapter, 1442 dec->model_name, THIS_MODULE, 1443 &dec->udev->dev, 1444 adapter_nr)) < 0) { 1445 printk("%s: dvb_register_adapter failed: error %d\n", 1446 __func__, result); 1447 1448 return result; 1449 } 1450 1451 dec->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING; 1452 1453 dec->demux.priv = (void *)dec; 1454 dec->demux.filternum = 31; 1455 dec->demux.feednum = 31; 1456 dec->demux.start_feed = ttusb_dec_start_feed; 1457 dec->demux.stop_feed = ttusb_dec_stop_feed; 1458 dec->demux.write_to_decoder = NULL; 1459 1460 if ((result = dvb_dmx_init(&dec->demux)) < 0) { 1461 printk("%s: dvb_dmx_init failed: error %d\n", __func__, 1462 result); 1463 1464 dvb_unregister_adapter(&dec->adapter); 1465 1466 return result; 1467 } 1468 1469 dec->dmxdev.filternum = 32; 1470 dec->dmxdev.demux = &dec->demux.dmx; 1471 dec->dmxdev.capabilities = 0; 1472 1473 if ((result = dvb_dmxdev_init(&dec->dmxdev, &dec->adapter)) < 0) { 1474 printk("%s: dvb_dmxdev_init failed: error %d\n", 1475 __func__, result); 1476 1477 dvb_dmx_release(&dec->demux); 1478 dvb_unregister_adapter(&dec->adapter); 1479 1480 return result; 1481 } 1482 1483 dec->frontend.source = DMX_FRONTEND_0; 1484 1485 if ((result = dec->demux.dmx.add_frontend(&dec->demux.dmx, 1486 &dec->frontend)) < 0) { 1487 printk("%s: dvb_dmx_init failed: error %d\n", __func__, 1488 result); 1489 1490 dvb_dmxdev_release(&dec->dmxdev); 1491 dvb_dmx_release(&dec->demux); 1492 dvb_unregister_adapter(&dec->adapter); 1493 1494 return result; 1495 } 1496 1497 if ((result = dec->demux.dmx.connect_frontend(&dec->demux.dmx, 1498 &dec->frontend)) < 0) { 1499 printk("%s: dvb_dmx_init failed: error %d\n", __func__, 1500 result); 1501 1502 dec->demux.dmx.remove_frontend(&dec->demux.dmx, &dec->frontend); 1503 dvb_dmxdev_release(&dec->dmxdev); 1504 dvb_dmx_release(&dec->demux); 1505 dvb_unregister_adapter(&dec->adapter); 1506 1507 return result; 1508 } 1509 1510 dvb_net_init(&dec->adapter, &dec->dvb_net, &dec->demux.dmx); 1511 1512 return 0; 1513 } 1514 1515 static void ttusb_dec_exit_dvb(struct ttusb_dec *dec) 1516 { 1517 dprintk("%s\n", __func__); 1518 1519 dvb_net_release(&dec->dvb_net); 1520 dec->demux.dmx.close(&dec->demux.dmx); 1521 dec->demux.dmx.remove_frontend(&dec->demux.dmx, &dec->frontend); 1522 dvb_dmxdev_release(&dec->dmxdev); 1523 dvb_dmx_release(&dec->demux); 1524 if (dec->fe) { 1525 dvb_unregister_frontend(dec->fe); 1526 if (dec->fe->ops.release) 1527 dec->fe->ops.release(dec->fe); 1528 } 1529 dvb_unregister_adapter(&dec->adapter); 1530 } 1531 1532 static void ttusb_dec_exit_rc(struct ttusb_dec *dec) 1533 { 1534 dprintk("%s\n", __func__); 1535 1536 if (dec->rc_input_dev) { 1537 input_unregister_device(dec->rc_input_dev); 1538 dec->rc_input_dev = NULL; 1539 } 1540 } 1541 1542 1543 static void ttusb_dec_exit_usb(struct ttusb_dec *dec) 1544 { 1545 int i; 1546 1547 dprintk("%s\n", __func__); 1548 1549 if (enable_rc) { 1550 /* we have to check whether the irq URB is already submitted. 1551 * As the irq is submitted after the interface is changed, 1552 * this is the best method i figured out. 1553 * Any others?*/ 1554 if (dec->interface == TTUSB_DEC_INTERFACE_IN) 1555 usb_kill_urb(dec->irq_urb); 1556 1557 usb_free_urb(dec->irq_urb); 1558 1559 usb_free_coherent(dec->udev, IRQ_PACKET_SIZE, 1560 dec->irq_buffer, dec->irq_dma_handle); 1561 } 1562 1563 dec->iso_stream_count = 0; 1564 1565 for (i = 0; i < ISO_BUF_COUNT; i++) 1566 usb_kill_urb(dec->iso_urb[i]); 1567 1568 ttusb_dec_free_iso_urbs(dec); 1569 } 1570 1571 static void ttusb_dec_exit_tasklet(struct ttusb_dec *dec) 1572 { 1573 struct list_head *item; 1574 struct urb_frame *frame; 1575 1576 tasklet_kill(&dec->urb_tasklet); 1577 1578 while ((item = dec->urb_frame_list.next) != &dec->urb_frame_list) { 1579 frame = list_entry(item, struct urb_frame, urb_frame_list); 1580 list_del(&frame->urb_frame_list); 1581 kfree(frame); 1582 } 1583 } 1584 1585 static void ttusb_dec_init_filters(struct ttusb_dec *dec) 1586 { 1587 INIT_LIST_HEAD(&dec->filter_info_list); 1588 spin_lock_init(&dec->filter_info_list_lock); 1589 } 1590 1591 static void ttusb_dec_exit_filters(struct ttusb_dec *dec) 1592 { 1593 struct list_head *item; 1594 struct filter_info *finfo; 1595 1596 while ((item = dec->filter_info_list.next) != &dec->filter_info_list) { 1597 finfo = list_entry(item, struct filter_info, filter_info_list); 1598 list_del(&finfo->filter_info_list); 1599 kfree(finfo); 1600 } 1601 } 1602 1603 static int fe_send_command(struct dvb_frontend* fe, const u8 command, 1604 int param_length, const u8 params[], 1605 int *result_length, u8 cmd_result[]) 1606 { 1607 struct ttusb_dec* dec = fe->dvb->priv; 1608 return ttusb_dec_send_command(dec, command, param_length, params, result_length, cmd_result); 1609 } 1610 1611 static struct ttusbdecfe_config fe_config = { 1612 .send_command = fe_send_command 1613 }; 1614 1615 static int ttusb_dec_probe(struct usb_interface *intf, 1616 const struct usb_device_id *id) 1617 { 1618 struct usb_device *udev; 1619 struct ttusb_dec *dec; 1620 int result; 1621 1622 dprintk("%s\n", __func__); 1623 1624 udev = interface_to_usbdev(intf); 1625 1626 if (!(dec = kzalloc(sizeof(struct ttusb_dec), GFP_KERNEL))) { 1627 printk("%s: couldn't allocate memory.\n", __func__); 1628 return -ENOMEM; 1629 } 1630 1631 usb_set_intfdata(intf, (void *)dec); 1632 1633 switch (id->idProduct) { 1634 case 0x1006: 1635 ttusb_dec_set_model(dec, TTUSB_DEC3000S); 1636 break; 1637 1638 case 0x1008: 1639 ttusb_dec_set_model(dec, TTUSB_DEC2000T); 1640 break; 1641 1642 case 0x1009: 1643 ttusb_dec_set_model(dec, TTUSB_DEC2540T); 1644 break; 1645 } 1646 1647 dec->udev = udev; 1648 1649 result = ttusb_dec_init_usb(dec); 1650 if (result) 1651 goto err_usb; 1652 result = ttusb_dec_init_stb(dec); 1653 if (result) 1654 goto err_stb; 1655 result = ttusb_dec_init_dvb(dec); 1656 if (result) 1657 goto err_stb; 1658 1659 dec->adapter.priv = dec; 1660 switch (id->idProduct) { 1661 case 0x1006: 1662 dec->fe = ttusbdecfe_dvbs_attach(&fe_config); 1663 break; 1664 1665 case 0x1008: 1666 case 0x1009: 1667 dec->fe = ttusbdecfe_dvbt_attach(&fe_config); 1668 break; 1669 } 1670 1671 if (dec->fe == NULL) { 1672 printk("dvb-ttusb-dec: A frontend driver was not found for device [%04x:%04x]\n", 1673 le16_to_cpu(dec->udev->descriptor.idVendor), 1674 le16_to_cpu(dec->udev->descriptor.idProduct)); 1675 } else { 1676 if (dvb_register_frontend(&dec->adapter, dec->fe)) { 1677 printk("budget-ci: Frontend registration failed!\n"); 1678 if (dec->fe->ops.release) 1679 dec->fe->ops.release(dec->fe); 1680 dec->fe = NULL; 1681 } 1682 } 1683 1684 ttusb_dec_init_v_pes(dec); 1685 ttusb_dec_init_filters(dec); 1686 ttusb_dec_init_tasklet(dec); 1687 1688 dec->active = 1; 1689 1690 ttusb_dec_set_interface(dec, TTUSB_DEC_INTERFACE_IN); 1691 1692 if (enable_rc) 1693 ttusb_init_rc(dec); 1694 1695 return 0; 1696 err_stb: 1697 ttusb_dec_exit_usb(dec); 1698 err_usb: 1699 kfree(dec); 1700 return result; 1701 } 1702 1703 static void ttusb_dec_disconnect(struct usb_interface *intf) 1704 { 1705 struct ttusb_dec *dec = usb_get_intfdata(intf); 1706 1707 usb_set_intfdata(intf, NULL); 1708 1709 dprintk("%s\n", __func__); 1710 1711 if (dec->active) { 1712 ttusb_dec_exit_tasklet(dec); 1713 ttusb_dec_exit_filters(dec); 1714 if(enable_rc) 1715 ttusb_dec_exit_rc(dec); 1716 ttusb_dec_exit_usb(dec); 1717 ttusb_dec_exit_dvb(dec); 1718 } 1719 1720 kfree(dec); 1721 } 1722 1723 static void ttusb_dec_set_model(struct ttusb_dec *dec, 1724 enum ttusb_dec_model model) 1725 { 1726 dec->model = model; 1727 1728 switch (model) { 1729 case TTUSB_DEC2000T: 1730 dec->model_name = "DEC2000-t"; 1731 dec->firmware_name = "dvb-ttusb-dec-2000t.fw"; 1732 break; 1733 1734 case TTUSB_DEC2540T: 1735 dec->model_name = "DEC2540-t"; 1736 dec->firmware_name = "dvb-ttusb-dec-2540t.fw"; 1737 break; 1738 1739 case TTUSB_DEC3000S: 1740 dec->model_name = "DEC3000-s"; 1741 dec->firmware_name = "dvb-ttusb-dec-3000s.fw"; 1742 break; 1743 } 1744 } 1745 1746 static struct usb_device_id ttusb_dec_table[] = { 1747 {USB_DEVICE(0x0b48, 0x1006)}, /* DEC3000-s */ 1748 /*{USB_DEVICE(0x0b48, 0x1007)}, Unconfirmed */ 1749 {USB_DEVICE(0x0b48, 0x1008)}, /* DEC2000-t */ 1750 {USB_DEVICE(0x0b48, 0x1009)}, /* DEC2540-t */ 1751 {} 1752 }; 1753 1754 static struct usb_driver ttusb_dec_driver = { 1755 .name = "ttusb-dec", 1756 .probe = ttusb_dec_probe, 1757 .disconnect = ttusb_dec_disconnect, 1758 .id_table = ttusb_dec_table, 1759 }; 1760 1761 module_usb_driver(ttusb_dec_driver); 1762 1763 MODULE_AUTHOR("Alex Woods <linux-dvb@giblets.org>"); 1764 MODULE_DESCRIPTION(DRIVER_NAME); 1765 MODULE_LICENSE("GPL"); 1766 MODULE_DEVICE_TABLE(usb, ttusb_dec_table); 1767