[ARM] Support register switch in nommu mode
[linux-2.6/verdex.git] / drivers / media / dvb / ttusb-dec / ttusb_dec.c
blobdf831171e03ca3ba92e65fac70e78a8f9c6cc08e
1 /*
2 * TTUSB DEC Driver
4 * Copyright (C) 2003-2004 Alex Woods <linux-dvb@giblets.org>
5 * IR support by Peter Beutner <p.beutner@gmx.net>
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.
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.
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.
23 #include <asm/semaphore.h>
24 #include <linux/list.h>
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/pci.h>
28 #include <linux/slab.h>
29 #include <linux/spinlock.h>
30 #include <linux/usb.h>
31 #include <linux/interrupt.h>
32 #include <linux/firmware.h>
33 #include <linux/crc32.h>
34 #include <linux/init.h>
35 #include <linux/input.h>
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"
44 static int debug;
45 static int output_pva;
46 static int enable_rc;
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)");
55 #define dprintk if (debug) printk
57 #define DRIVER_NAME "TechnoTrend/Hauppauge DEC USB"
59 #define COMMAND_PIPE 0x03
60 #define RESULT_PIPE 0x04
61 #define IN_PIPE 0x08
62 #define OUT_PIPE 0x07
63 #define IRQ_PIPE 0x0A
65 #define COMMAND_PACKET_SIZE 0x3c
66 #define ARM_PACKET_SIZE 0x1000
67 #define IRQ_PACKET_SIZE 0x8
69 #define ISO_BUF_COUNT 0x04
70 #define FRAMES_PER_ISO_BUF 0x04
71 #define ISO_FRAME_SIZE 0x0380
73 #define MAX_PVA_LENGTH 6144
75 enum ttusb_dec_model {
76 TTUSB_DEC2000T,
77 TTUSB_DEC2540T,
78 TTUSB_DEC3000S
81 enum ttusb_dec_packet_type {
82 TTUSB_DEC_PACKET_PVA,
83 TTUSB_DEC_PACKET_SECTION,
84 TTUSB_DEC_PACKET_EMPTY
87 enum ttusb_dec_interface {
88 TTUSB_DEC_INTERFACE_INITIAL,
89 TTUSB_DEC_INTERFACE_IN,
90 TTUSB_DEC_INTERFACE_OUT
93 struct ttusb_dec {
94 enum ttusb_dec_model model;
95 char *model_name;
96 char *firmware_name;
97 int can_playback;
99 /* DVB bits */
100 struct dvb_adapter adapter;
101 struct dmxdev dmxdev;
102 struct dvb_demux demux;
103 struct dmx_frontend frontend;
104 struct dvb_net dvb_net;
105 struct dvb_frontend* fe;
107 u16 pid[DMX_PES_OTHER];
109 /* USB bits */
110 struct usb_device *udev;
111 u8 trans_count;
112 unsigned int command_pipe;
113 unsigned int result_pipe;
114 unsigned int in_pipe;
115 unsigned int out_pipe;
116 unsigned int irq_pipe;
117 enum ttusb_dec_interface interface;
118 struct semaphore usb_sem;
120 void *irq_buffer;
121 struct urb *irq_urb;
122 dma_addr_t irq_dma_handle;
123 void *iso_buffer;
124 dma_addr_t iso_dma_handle;
125 struct urb *iso_urb[ISO_BUF_COUNT];
126 int iso_stream_count;
127 struct semaphore iso_sem;
129 u8 packet[MAX_PVA_LENGTH + 4];
130 enum ttusb_dec_packet_type packet_type;
131 int packet_state;
132 int packet_length;
133 int packet_payload_length;
134 u16 next_packet_id;
136 int pva_stream_count;
137 int filter_stream_count;
139 struct dvb_filter_pes2ts a_pes2ts;
140 struct dvb_filter_pes2ts v_pes2ts;
142 u8 v_pes[16 + MAX_PVA_LENGTH];
143 int v_pes_length;
144 int v_pes_postbytes;
146 struct list_head urb_frame_list;
147 struct tasklet_struct urb_tasklet;
148 spinlock_t urb_frame_list_lock;
150 struct dvb_demux_filter *audio_filter;
151 struct dvb_demux_filter *video_filter;
152 struct list_head filter_info_list;
153 spinlock_t filter_info_list_lock;
155 struct input_dev *rc_input_dev;
156 char rc_phys[64];
158 int active; /* Loaded successfully */
161 struct urb_frame {
162 u8 data[ISO_FRAME_SIZE];
163 int length;
164 struct list_head urb_frame_list;
167 struct filter_info {
168 u8 stream_id;
169 struct dvb_demux_filter *filter;
170 struct list_head filter_info_list;
173 static u16 rc_keys[] = {
174 KEY_POWER,
175 KEY_MUTE,
176 KEY_1,
177 KEY_2,
178 KEY_3,
179 KEY_4,
180 KEY_5,
181 KEY_6,
182 KEY_7,
183 KEY_8,
184 KEY_9,
185 KEY_0,
186 KEY_CHANNELUP,
187 KEY_VOLUMEDOWN,
188 KEY_OK,
189 KEY_VOLUMEUP,
190 KEY_CHANNELDOWN,
191 KEY_PREVIOUS,
192 KEY_ESC,
193 KEY_RED,
194 KEY_GREEN,
195 KEY_YELLOW,
196 KEY_BLUE,
197 KEY_OPTION,
198 KEY_M,
199 KEY_RADIO
202 static void ttusb_dec_set_model(struct ttusb_dec *dec,
203 enum ttusb_dec_model model);
205 static void ttusb_dec_handle_irq( struct urb *urb, struct pt_regs *regs)
207 struct ttusb_dec * dec = urb->context;
208 char *buffer = dec->irq_buffer;
209 int retval;
211 switch(urb->status) {
212 case 0: /*success*/
213 break;
214 case -ECONNRESET:
215 case -ENOENT:
216 case -ESHUTDOWN:
217 case -ETIMEDOUT:
218 /* this urb is dead, cleanup */
219 dprintk("%s:urb shutting down with status: %d\n",
220 __FUNCTION__, urb->status);
221 return;
222 default:
223 dprintk("%s:nonzero status received: %d\n",
224 __FUNCTION__,urb->status);
225 goto exit;
228 if( (buffer[0] == 0x1) && (buffer[2] == 0x15) ) {
229 /* IR - Event */
230 /* this is an fact a bit too simple implementation;
231 * the box also reports a keyrepeat signal
232 * (with buffer[3] == 0x40) in an intervall of ~100ms.
233 * But to handle this correctly we had to imlemenent some
234 * kind of timer which signals a 'key up' event if no
235 * keyrepeat signal is recieved for lets say 200ms.
236 * this should/could be added later ...
237 * for now lets report each signal as a key down and up*/
238 dprintk("%s:rc signal:%d\n", __FUNCTION__, buffer[4]);
239 input_report_key(dec->rc_input_dev, rc_keys[buffer[4] - 1], 1);
240 input_report_key(dec->rc_input_dev, rc_keys[buffer[4] - 1], 0);
241 input_sync(dec->rc_input_dev);
244 exit: retval = usb_submit_urb(urb, GFP_ATOMIC);
245 if(retval)
246 printk("%s - usb_commit_urb failed with result: %d\n",
247 __FUNCTION__, retval);
250 static u16 crc16(u16 crc, const u8 *buf, size_t len)
252 u16 tmp;
254 while (len--) {
255 crc ^= *buf++;
256 crc ^= (u8)crc >> 4;
257 tmp = (u8)crc;
258 crc ^= (tmp ^ (tmp << 1)) << 4;
260 return crc;
263 static int ttusb_dec_send_command(struct ttusb_dec *dec, const u8 command,
264 int param_length, const u8 params[],
265 int *result_length, u8 cmd_result[])
267 int result, actual_len, i;
268 u8 *b;
270 dprintk("%s\n", __FUNCTION__);
272 b = kmalloc(COMMAND_PACKET_SIZE + 4, GFP_KERNEL);
273 if (!b)
274 return -ENOMEM;
276 if ((result = down_interruptible(&dec->usb_sem))) {
277 kfree(b);
278 printk("%s: Failed to down usb semaphore.\n", __FUNCTION__);
279 return result;
282 b[0] = 0xaa;
283 b[1] = ++dec->trans_count;
284 b[2] = command;
285 b[3] = param_length;
287 if (params)
288 memcpy(&b[4], params, param_length);
290 if (debug) {
291 printk("%s: command: ", __FUNCTION__);
292 for (i = 0; i < param_length + 4; i++)
293 printk("0x%02X ", b[i]);
294 printk("\n");
297 result = usb_bulk_msg(dec->udev, dec->command_pipe, b,
298 COMMAND_PACKET_SIZE + 4, &actual_len, 1000);
300 if (result) {
301 printk("%s: command bulk message failed: error %d\n",
302 __FUNCTION__, result);
303 up(&dec->usb_sem);
304 kfree(b);
305 return result;
308 result = usb_bulk_msg(dec->udev, dec->result_pipe, b,
309 COMMAND_PACKET_SIZE + 4, &actual_len, 1000);
311 if (result) {
312 printk("%s: result bulk message failed: error %d\n",
313 __FUNCTION__, result);
314 up(&dec->usb_sem);
315 kfree(b);
316 return result;
317 } else {
318 if (debug) {
319 printk("%s: result: ", __FUNCTION__);
320 for (i = 0; i < actual_len; i++)
321 printk("0x%02X ", b[i]);
322 printk("\n");
325 if (result_length)
326 *result_length = b[3];
327 if (cmd_result && b[3] > 0)
328 memcpy(cmd_result, &b[4], b[3]);
330 up(&dec->usb_sem);
332 kfree(b);
333 return 0;
337 static int ttusb_dec_get_stb_state (struct ttusb_dec *dec, unsigned int *mode,
338 unsigned int *model, unsigned int *version)
340 u8 c[COMMAND_PACKET_SIZE];
341 int c_length;
342 int result;
343 unsigned int tmp;
345 dprintk("%s\n", __FUNCTION__);
347 result = ttusb_dec_send_command(dec, 0x08, 0, NULL, &c_length, c);
348 if (result)
349 return result;
351 if (c_length >= 0x0c) {
352 if (mode != NULL) {
353 memcpy(&tmp, c, 4);
354 *mode = ntohl(tmp);
356 if (model != NULL) {
357 memcpy(&tmp, &c[4], 4);
358 *model = ntohl(tmp);
360 if (version != NULL) {
361 memcpy(&tmp, &c[8], 4);
362 *version = ntohl(tmp);
364 return 0;
365 } else {
366 return -1;
370 static int ttusb_dec_audio_pes2ts_cb(void *priv, unsigned char *data)
372 struct ttusb_dec *dec = priv;
374 dec->audio_filter->feed->cb.ts(data, 188, NULL, 0,
375 &dec->audio_filter->feed->feed.ts,
376 DMX_OK);
378 return 0;
381 static int ttusb_dec_video_pes2ts_cb(void *priv, unsigned char *data)
383 struct ttusb_dec *dec = priv;
385 dec->video_filter->feed->cb.ts(data, 188, NULL, 0,
386 &dec->video_filter->feed->feed.ts,
387 DMX_OK);
389 return 0;
392 static void ttusb_dec_set_pids(struct ttusb_dec *dec)
394 u8 b[] = { 0x00, 0x00, 0x00, 0x00,
395 0x00, 0x00, 0xff, 0xff,
396 0xff, 0xff, 0xff, 0xff };
398 u16 pcr = htons(dec->pid[DMX_PES_PCR]);
399 u16 audio = htons(dec->pid[DMX_PES_AUDIO]);
400 u16 video = htons(dec->pid[DMX_PES_VIDEO]);
402 dprintk("%s\n", __FUNCTION__);
404 memcpy(&b[0], &pcr, 2);
405 memcpy(&b[2], &audio, 2);
406 memcpy(&b[4], &video, 2);
408 ttusb_dec_send_command(dec, 0x50, sizeof(b), b, NULL, NULL);
410 dvb_filter_pes2ts_init(&dec->a_pes2ts, dec->pid[DMX_PES_AUDIO],
411 ttusb_dec_audio_pes2ts_cb, dec);
412 dvb_filter_pes2ts_init(&dec->v_pes2ts, dec->pid[DMX_PES_VIDEO],
413 ttusb_dec_video_pes2ts_cb, dec);
414 dec->v_pes_length = 0;
415 dec->v_pes_postbytes = 0;
418 static void ttusb_dec_process_pva(struct ttusb_dec *dec, u8 *pva, int length)
420 if (length < 8) {
421 printk("%s: packet too short - discarding\n", __FUNCTION__);
422 return;
425 if (length > 8 + MAX_PVA_LENGTH) {
426 printk("%s: packet too long - discarding\n", __FUNCTION__);
427 return;
430 switch (pva[2]) {
432 case 0x01: { /* VideoStream */
433 int prebytes = pva[5] & 0x03;
434 int postbytes = (pva[5] & 0x0c) >> 2;
435 u16 v_pes_payload_length;
437 if (output_pva) {
438 dec->video_filter->feed->cb.ts(pva, length, NULL, 0,
439 &dec->video_filter->feed->feed.ts, DMX_OK);
440 return;
443 if (dec->v_pes_postbytes > 0 &&
444 dec->v_pes_postbytes == prebytes) {
445 memcpy(&dec->v_pes[dec->v_pes_length],
446 &pva[12], prebytes);
448 dvb_filter_pes2ts(&dec->v_pes2ts, dec->v_pes,
449 dec->v_pes_length + prebytes, 1);
452 if (pva[5] & 0x10) {
453 dec->v_pes[7] = 0x80;
454 dec->v_pes[8] = 0x05;
456 dec->v_pes[9] = 0x21 | ((pva[8] & 0xc0) >> 5);
457 dec->v_pes[10] = ((pva[8] & 0x3f) << 2) |
458 ((pva[9] & 0xc0) >> 6);
459 dec->v_pes[11] = 0x01 |
460 ((pva[9] & 0x3f) << 2) |
461 ((pva[10] & 0x80) >> 6);
462 dec->v_pes[12] = ((pva[10] & 0x7f) << 1) |
463 ((pva[11] & 0xc0) >> 7);
464 dec->v_pes[13] = 0x01 | ((pva[11] & 0x7f) << 1);
466 memcpy(&dec->v_pes[14], &pva[12 + prebytes],
467 length - 12 - prebytes);
468 dec->v_pes_length = 14 + length - 12 - prebytes;
469 } else {
470 dec->v_pes[7] = 0x00;
471 dec->v_pes[8] = 0x00;
473 memcpy(&dec->v_pes[9], &pva[8], length - 8);
474 dec->v_pes_length = 9 + length - 8;
477 dec->v_pes_postbytes = postbytes;
479 if (dec->v_pes[9 + dec->v_pes[8]] == 0x00 &&
480 dec->v_pes[10 + dec->v_pes[8]] == 0x00 &&
481 dec->v_pes[11 + dec->v_pes[8]] == 0x01)
482 dec->v_pes[6] = 0x84;
483 else
484 dec->v_pes[6] = 0x80;
486 v_pes_payload_length = htons(dec->v_pes_length - 6 +
487 postbytes);
488 memcpy(&dec->v_pes[4], &v_pes_payload_length, 2);
490 if (postbytes == 0)
491 dvb_filter_pes2ts(&dec->v_pes2ts, dec->v_pes,
492 dec->v_pes_length, 1);
494 break;
497 case 0x02: /* MainAudioStream */
498 if (output_pva) {
499 dec->audio_filter->feed->cb.ts(pva, length, NULL, 0,
500 &dec->audio_filter->feed->feed.ts, DMX_OK);
501 return;
504 dvb_filter_pes2ts(&dec->a_pes2ts, &pva[8], length - 8,
505 pva[5] & 0x10);
506 break;
508 default:
509 printk("%s: unknown PVA type: %02x.\n", __FUNCTION__,
510 pva[2]);
511 break;
515 static void ttusb_dec_process_filter(struct ttusb_dec *dec, u8 *packet,
516 int length)
518 struct list_head *item;
519 struct filter_info *finfo;
520 struct dvb_demux_filter *filter = NULL;
521 unsigned long flags;
522 u8 sid;
524 sid = packet[1];
525 spin_lock_irqsave(&dec->filter_info_list_lock, flags);
526 for (item = dec->filter_info_list.next; item != &dec->filter_info_list;
527 item = item->next) {
528 finfo = list_entry(item, struct filter_info, filter_info_list);
529 if (finfo->stream_id == sid) {
530 filter = finfo->filter;
531 break;
534 spin_unlock_irqrestore(&dec->filter_info_list_lock, flags);
536 if (filter)
537 filter->feed->cb.sec(&packet[2], length - 2, NULL, 0,
538 &filter->filter, DMX_OK);
541 static void ttusb_dec_process_packet(struct ttusb_dec *dec)
543 int i;
544 u16 csum = 0;
545 u16 packet_id;
547 if (dec->packet_length % 2) {
548 printk("%s: odd sized packet - discarding\n", __FUNCTION__);
549 return;
552 for (i = 0; i < dec->packet_length; i += 2)
553 csum ^= ((dec->packet[i] << 8) + dec->packet[i + 1]);
555 if (csum) {
556 printk("%s: checksum failed - discarding\n", __FUNCTION__);
557 return;
560 packet_id = dec->packet[dec->packet_length - 4] << 8;
561 packet_id += dec->packet[dec->packet_length - 3];
563 if ((packet_id != dec->next_packet_id) && dec->next_packet_id) {
564 printk("%s: warning: lost packets between %u and %u\n",
565 __FUNCTION__, dec->next_packet_id - 1, packet_id);
568 if (packet_id == 0xffff)
569 dec->next_packet_id = 0x8000;
570 else
571 dec->next_packet_id = packet_id + 1;
573 switch (dec->packet_type) {
574 case TTUSB_DEC_PACKET_PVA:
575 if (dec->pva_stream_count)
576 ttusb_dec_process_pva(dec, dec->packet,
577 dec->packet_payload_length);
578 break;
580 case TTUSB_DEC_PACKET_SECTION:
581 if (dec->filter_stream_count)
582 ttusb_dec_process_filter(dec, dec->packet,
583 dec->packet_payload_length);
584 break;
586 case TTUSB_DEC_PACKET_EMPTY:
587 break;
591 static void swap_bytes(u8 *b, int length)
593 u8 c;
595 length -= length % 2;
596 for (; length; b += 2, length -= 2) {
597 c = *b;
598 *b = *(b + 1);
599 *(b + 1) = c;
603 static void ttusb_dec_process_urb_frame(struct ttusb_dec *dec, u8 *b,
604 int length)
606 swap_bytes(b, length);
608 while (length) {
609 switch (dec->packet_state) {
611 case 0:
612 case 1:
613 case 2:
614 if (*b++ == 0xaa)
615 dec->packet_state++;
616 else
617 dec->packet_state = 0;
619 length--;
620 break;
622 case 3:
623 if (*b == 0x00) {
624 dec->packet_state++;
625 dec->packet_length = 0;
626 } else if (*b != 0xaa) {
627 dec->packet_state = 0;
630 b++;
631 length--;
632 break;
634 case 4:
635 dec->packet[dec->packet_length++] = *b++;
637 if (dec->packet_length == 2) {
638 if (dec->packet[0] == 'A' &&
639 dec->packet[1] == 'V') {
640 dec->packet_type =
641 TTUSB_DEC_PACKET_PVA;
642 dec->packet_state++;
643 } else if (dec->packet[0] == 'S') {
644 dec->packet_type =
645 TTUSB_DEC_PACKET_SECTION;
646 dec->packet_state++;
647 } else if (dec->packet[0] == 0x00) {
648 dec->packet_type =
649 TTUSB_DEC_PACKET_EMPTY;
650 dec->packet_payload_length = 2;
651 dec->packet_state = 7;
652 } else {
653 printk("%s: unknown packet type: "
654 "%02x%02x\n", __FUNCTION__,
655 dec->packet[0], dec->packet[1]);
656 dec->packet_state = 0;
660 length--;
661 break;
663 case 5:
664 dec->packet[dec->packet_length++] = *b++;
666 if (dec->packet_type == TTUSB_DEC_PACKET_PVA &&
667 dec->packet_length == 8) {
668 dec->packet_state++;
669 dec->packet_payload_length = 8 +
670 (dec->packet[6] << 8) +
671 dec->packet[7];
672 } else if (dec->packet_type ==
673 TTUSB_DEC_PACKET_SECTION &&
674 dec->packet_length == 5) {
675 dec->packet_state++;
676 dec->packet_payload_length = 5 +
677 ((dec->packet[3] & 0x0f) << 8) +
678 dec->packet[4];
681 length--;
682 break;
684 case 6: {
685 int remainder = dec->packet_payload_length -
686 dec->packet_length;
688 if (length >= remainder) {
689 memcpy(dec->packet + dec->packet_length,
690 b, remainder);
691 dec->packet_length += remainder;
692 b += remainder;
693 length -= remainder;
694 dec->packet_state++;
695 } else {
696 memcpy(&dec->packet[dec->packet_length],
697 b, length);
698 dec->packet_length += length;
699 length = 0;
702 break;
705 case 7: {
706 int tail = 4;
708 dec->packet[dec->packet_length++] = *b++;
710 if (dec->packet_type == TTUSB_DEC_PACKET_SECTION &&
711 dec->packet_payload_length % 2)
712 tail++;
714 if (dec->packet_length ==
715 dec->packet_payload_length + tail) {
716 ttusb_dec_process_packet(dec);
717 dec->packet_state = 0;
720 length--;
721 break;
724 default:
725 printk("%s: illegal packet state encountered.\n",
726 __FUNCTION__);
727 dec->packet_state = 0;
732 static void ttusb_dec_process_urb_frame_list(unsigned long data)
734 struct ttusb_dec *dec = (struct ttusb_dec *)data;
735 struct list_head *item;
736 struct urb_frame *frame;
737 unsigned long flags;
739 while (1) {
740 spin_lock_irqsave(&dec->urb_frame_list_lock, flags);
741 if ((item = dec->urb_frame_list.next) != &dec->urb_frame_list) {
742 frame = list_entry(item, struct urb_frame,
743 urb_frame_list);
744 list_del(&frame->urb_frame_list);
745 } else {
746 spin_unlock_irqrestore(&dec->urb_frame_list_lock,
747 flags);
748 return;
750 spin_unlock_irqrestore(&dec->urb_frame_list_lock, flags);
752 ttusb_dec_process_urb_frame(dec, frame->data, frame->length);
753 kfree(frame);
757 static void ttusb_dec_process_urb(struct urb *urb, struct pt_regs *ptregs)
759 struct ttusb_dec *dec = urb->context;
761 if (!urb->status) {
762 int i;
764 for (i = 0; i < FRAMES_PER_ISO_BUF; i++) {
765 struct usb_iso_packet_descriptor *d;
766 u8 *b;
767 int length;
768 struct urb_frame *frame;
770 d = &urb->iso_frame_desc[i];
771 b = urb->transfer_buffer + d->offset;
772 length = d->actual_length;
774 if ((frame = kmalloc(sizeof(struct urb_frame),
775 GFP_ATOMIC))) {
776 unsigned long flags;
778 memcpy(frame->data, b, length);
779 frame->length = length;
781 spin_lock_irqsave(&dec->urb_frame_list_lock,
782 flags);
783 list_add_tail(&frame->urb_frame_list,
784 &dec->urb_frame_list);
785 spin_unlock_irqrestore(&dec->urb_frame_list_lock,
786 flags);
788 tasklet_schedule(&dec->urb_tasklet);
791 } else {
792 /* -ENOENT is expected when unlinking urbs */
793 if (urb->status != -ENOENT)
794 dprintk("%s: urb error: %d\n", __FUNCTION__,
795 urb->status);
798 if (dec->iso_stream_count)
799 usb_submit_urb(urb, GFP_ATOMIC);
802 static void ttusb_dec_setup_urbs(struct ttusb_dec *dec)
804 int i, j, buffer_offset = 0;
806 dprintk("%s\n", __FUNCTION__);
808 for (i = 0; i < ISO_BUF_COUNT; i++) {
809 int frame_offset = 0;
810 struct urb *urb = dec->iso_urb[i];
812 urb->dev = dec->udev;
813 urb->context = dec;
814 urb->complete = ttusb_dec_process_urb;
815 urb->pipe = dec->in_pipe;
816 urb->transfer_flags = URB_ISO_ASAP;
817 urb->interval = 1;
818 urb->number_of_packets = FRAMES_PER_ISO_BUF;
819 urb->transfer_buffer_length = ISO_FRAME_SIZE *
820 FRAMES_PER_ISO_BUF;
821 urb->transfer_buffer = dec->iso_buffer + buffer_offset;
822 buffer_offset += ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF;
824 for (j = 0; j < FRAMES_PER_ISO_BUF; j++) {
825 urb->iso_frame_desc[j].offset = frame_offset;
826 urb->iso_frame_desc[j].length = ISO_FRAME_SIZE;
827 frame_offset += ISO_FRAME_SIZE;
832 static void ttusb_dec_stop_iso_xfer(struct ttusb_dec *dec)
834 int i;
836 dprintk("%s\n", __FUNCTION__);
838 if (down_interruptible(&dec->iso_sem))
839 return;
841 dec->iso_stream_count--;
843 if (!dec->iso_stream_count) {
844 for (i = 0; i < ISO_BUF_COUNT; i++)
845 usb_kill_urb(dec->iso_urb[i]);
848 up(&dec->iso_sem);
851 /* Setting the interface of the DEC tends to take down the USB communications
852 * for a short period, so it's important not to call this function just before
853 * trying to talk to it.
855 static int ttusb_dec_set_interface(struct ttusb_dec *dec,
856 enum ttusb_dec_interface interface)
858 int result = 0;
859 u8 b[] = { 0x05 };
861 if (interface != dec->interface) {
862 switch (interface) {
863 case TTUSB_DEC_INTERFACE_INITIAL:
864 result = usb_set_interface(dec->udev, 0, 0);
865 break;
866 case TTUSB_DEC_INTERFACE_IN:
867 result = ttusb_dec_send_command(dec, 0x80, sizeof(b),
868 b, NULL, NULL);
869 if (result)
870 return result;
871 result = usb_set_interface(dec->udev, 0, 8);
872 break;
873 case TTUSB_DEC_INTERFACE_OUT:
874 result = usb_set_interface(dec->udev, 0, 1);
875 break;
878 if (result)
879 return result;
881 dec->interface = interface;
884 return 0;
887 static int ttusb_dec_start_iso_xfer(struct ttusb_dec *dec)
889 int i, result;
891 dprintk("%s\n", __FUNCTION__);
893 if (down_interruptible(&dec->iso_sem))
894 return -EAGAIN;
896 if (!dec->iso_stream_count) {
897 ttusb_dec_setup_urbs(dec);
899 dec->packet_state = 0;
900 dec->v_pes_postbytes = 0;
901 dec->next_packet_id = 0;
903 for (i = 0; i < ISO_BUF_COUNT; i++) {
904 if ((result = usb_submit_urb(dec->iso_urb[i],
905 GFP_ATOMIC))) {
906 printk("%s: failed urb submission %d: "
907 "error %d\n", __FUNCTION__, i, result);
909 while (i) {
910 usb_kill_urb(dec->iso_urb[i - 1]);
911 i--;
914 up(&dec->iso_sem);
915 return result;
920 dec->iso_stream_count++;
922 up(&dec->iso_sem);
924 return 0;
927 static int ttusb_dec_start_ts_feed(struct dvb_demux_feed *dvbdmxfeed)
929 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
930 struct ttusb_dec *dec = dvbdmx->priv;
931 u8 b0[] = { 0x05 };
932 int result = 0;
934 dprintk("%s\n", __FUNCTION__);
936 dprintk(" ts_type:");
938 if (dvbdmxfeed->ts_type & TS_DECODER)
939 dprintk(" TS_DECODER");
941 if (dvbdmxfeed->ts_type & TS_PACKET)
942 dprintk(" TS_PACKET");
944 if (dvbdmxfeed->ts_type & TS_PAYLOAD_ONLY)
945 dprintk(" TS_PAYLOAD_ONLY");
947 dprintk("\n");
949 switch (dvbdmxfeed->pes_type) {
951 case DMX_TS_PES_VIDEO:
952 dprintk(" pes_type: DMX_TS_PES_VIDEO\n");
953 dec->pid[DMX_PES_PCR] = dvbdmxfeed->pid;
954 dec->pid[DMX_PES_VIDEO] = dvbdmxfeed->pid;
955 dec->video_filter = dvbdmxfeed->filter;
956 ttusb_dec_set_pids(dec);
957 break;
959 case DMX_TS_PES_AUDIO:
960 dprintk(" pes_type: DMX_TS_PES_AUDIO\n");
961 dec->pid[DMX_PES_AUDIO] = dvbdmxfeed->pid;
962 dec->audio_filter = dvbdmxfeed->filter;
963 ttusb_dec_set_pids(dec);
964 break;
966 case DMX_TS_PES_TELETEXT:
967 dec->pid[DMX_PES_TELETEXT] = dvbdmxfeed->pid;
968 dprintk(" pes_type: DMX_TS_PES_TELETEXT(not supported)\n");
969 return -ENOSYS;
971 case DMX_TS_PES_PCR:
972 dprintk(" pes_type: DMX_TS_PES_PCR\n");
973 dec->pid[DMX_PES_PCR] = dvbdmxfeed->pid;
974 ttusb_dec_set_pids(dec);
975 break;
977 case DMX_TS_PES_OTHER:
978 dprintk(" pes_type: DMX_TS_PES_OTHER(not supported)\n");
979 return -ENOSYS;
981 default:
982 dprintk(" pes_type: unknown (%d)\n", dvbdmxfeed->pes_type);
983 return -EINVAL;
987 result = ttusb_dec_send_command(dec, 0x80, sizeof(b0), b0, NULL, NULL);
988 if (result)
989 return result;
991 dec->pva_stream_count++;
992 return ttusb_dec_start_iso_xfer(dec);
995 static int ttusb_dec_start_sec_feed(struct dvb_demux_feed *dvbdmxfeed)
997 struct ttusb_dec *dec = dvbdmxfeed->demux->priv;
998 u8 b0[] = { 0x00, 0x00, 0x00, 0x01,
999 0x00, 0x00, 0x00, 0x00,
1000 0x00, 0x00, 0x00, 0x00,
1001 0x00, 0x00, 0x00, 0x00,
1002 0x00, 0xff, 0x00, 0x00,
1003 0x00, 0x00, 0x00, 0x00,
1004 0x00, 0x00, 0x00, 0x00,
1005 0x00 };
1006 u16 pid;
1007 u8 c[COMMAND_PACKET_SIZE];
1008 int c_length;
1009 int result;
1010 struct filter_info *finfo;
1011 unsigned long flags;
1012 u8 x = 1;
1014 dprintk("%s\n", __FUNCTION__);
1016 pid = htons(dvbdmxfeed->pid);
1017 memcpy(&b0[0], &pid, 2);
1018 memcpy(&b0[4], &x, 1);
1019 memcpy(&b0[5], &dvbdmxfeed->filter->filter.filter_value[0], 1);
1021 result = ttusb_dec_send_command(dec, 0x60, sizeof(b0), b0,
1022 &c_length, c);
1024 if (!result) {
1025 if (c_length == 2) {
1026 if (!(finfo = kmalloc(sizeof(struct filter_info),
1027 GFP_ATOMIC)))
1028 return -ENOMEM;
1030 finfo->stream_id = c[1];
1031 finfo->filter = dvbdmxfeed->filter;
1033 spin_lock_irqsave(&dec->filter_info_list_lock, flags);
1034 list_add_tail(&finfo->filter_info_list,
1035 &dec->filter_info_list);
1036 spin_unlock_irqrestore(&dec->filter_info_list_lock,
1037 flags);
1039 dvbdmxfeed->priv = finfo;
1041 dec->filter_stream_count++;
1042 return ttusb_dec_start_iso_xfer(dec);
1045 return -EAGAIN;
1046 } else
1047 return result;
1050 static int ttusb_dec_start_feed(struct dvb_demux_feed *dvbdmxfeed)
1052 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
1054 dprintk("%s\n", __FUNCTION__);
1056 if (!dvbdmx->dmx.frontend)
1057 return -EINVAL;
1059 dprintk(" pid: 0x%04X\n", dvbdmxfeed->pid);
1061 switch (dvbdmxfeed->type) {
1063 case DMX_TYPE_TS:
1064 return ttusb_dec_start_ts_feed(dvbdmxfeed);
1065 break;
1067 case DMX_TYPE_SEC:
1068 return ttusb_dec_start_sec_feed(dvbdmxfeed);
1069 break;
1071 default:
1072 dprintk(" type: unknown (%d)\n", dvbdmxfeed->type);
1073 return -EINVAL;
1078 static int ttusb_dec_stop_ts_feed(struct dvb_demux_feed *dvbdmxfeed)
1080 struct ttusb_dec *dec = dvbdmxfeed->demux->priv;
1081 u8 b0[] = { 0x00 };
1083 ttusb_dec_send_command(dec, 0x81, sizeof(b0), b0, NULL, NULL);
1085 dec->pva_stream_count--;
1087 ttusb_dec_stop_iso_xfer(dec);
1089 return 0;
1092 static int ttusb_dec_stop_sec_feed(struct dvb_demux_feed *dvbdmxfeed)
1094 struct ttusb_dec *dec = dvbdmxfeed->demux->priv;
1095 u8 b0[] = { 0x00, 0x00 };
1096 struct filter_info *finfo = (struct filter_info *)dvbdmxfeed->priv;
1097 unsigned long flags;
1099 b0[1] = finfo->stream_id;
1100 spin_lock_irqsave(&dec->filter_info_list_lock, flags);
1101 list_del(&finfo->filter_info_list);
1102 spin_unlock_irqrestore(&dec->filter_info_list_lock, flags);
1103 kfree(finfo);
1104 ttusb_dec_send_command(dec, 0x62, sizeof(b0), b0, NULL, NULL);
1106 dec->filter_stream_count--;
1108 ttusb_dec_stop_iso_xfer(dec);
1110 return 0;
1113 static int ttusb_dec_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
1115 dprintk("%s\n", __FUNCTION__);
1117 switch (dvbdmxfeed->type) {
1118 case DMX_TYPE_TS:
1119 return ttusb_dec_stop_ts_feed(dvbdmxfeed);
1120 break;
1122 case DMX_TYPE_SEC:
1123 return ttusb_dec_stop_sec_feed(dvbdmxfeed);
1124 break;
1127 return 0;
1130 static void ttusb_dec_free_iso_urbs(struct ttusb_dec *dec)
1132 int i;
1134 dprintk("%s\n", __FUNCTION__);
1136 for (i = 0; i < ISO_BUF_COUNT; i++)
1137 if (dec->iso_urb[i])
1138 usb_free_urb(dec->iso_urb[i]);
1140 pci_free_consistent(NULL,
1141 ISO_FRAME_SIZE * (FRAMES_PER_ISO_BUF *
1142 ISO_BUF_COUNT),
1143 dec->iso_buffer, dec->iso_dma_handle);
1146 static int ttusb_dec_alloc_iso_urbs(struct ttusb_dec *dec)
1148 int i;
1150 dprintk("%s\n", __FUNCTION__);
1152 dec->iso_buffer = pci_alloc_consistent(NULL,
1153 ISO_FRAME_SIZE *
1154 (FRAMES_PER_ISO_BUF *
1155 ISO_BUF_COUNT),
1156 &dec->iso_dma_handle);
1158 memset(dec->iso_buffer, 0,
1159 ISO_FRAME_SIZE * (FRAMES_PER_ISO_BUF * ISO_BUF_COUNT));
1161 for (i = 0; i < ISO_BUF_COUNT; i++) {
1162 struct urb *urb;
1164 if (!(urb = usb_alloc_urb(FRAMES_PER_ISO_BUF, GFP_ATOMIC))) {
1165 ttusb_dec_free_iso_urbs(dec);
1166 return -ENOMEM;
1169 dec->iso_urb[i] = urb;
1172 ttusb_dec_setup_urbs(dec);
1174 return 0;
1177 static void ttusb_dec_init_tasklet(struct ttusb_dec *dec)
1179 spin_lock_init(&dec->urb_frame_list_lock);
1180 INIT_LIST_HEAD(&dec->urb_frame_list);
1181 tasklet_init(&dec->urb_tasklet, ttusb_dec_process_urb_frame_list,
1182 (unsigned long)dec);
1185 static int ttusb_init_rc( struct ttusb_dec *dec)
1187 struct input_dev *input_dev;
1188 u8 b[] = { 0x00, 0x01 };
1189 int i;
1191 usb_make_path(dec->udev, dec->rc_phys, sizeof(dec->rc_phys));
1192 strlcpy(dec->rc_phys, "/input0", sizeof(dec->rc_phys));
1194 dec->rc_input_dev = input_dev = input_allocate_device();
1195 if (!input_dev)
1196 return -ENOMEM;
1198 input_dev->name = "ttusb_dec remote control";
1199 input_dev->phys = dec->rc_phys;
1200 input_dev->evbit[0] = BIT(EV_KEY);
1201 input_dev->keycodesize = sizeof(u16);
1202 input_dev->keycodemax = 0x1a;
1203 input_dev->keycode = rc_keys;
1205 for (i = 0; i < ARRAY_SIZE(rc_keys); i++)
1206 set_bit(rc_keys[i], input_dev->keybit);
1208 input_register_device(input_dev);
1210 if (usb_submit_urb(dec->irq_urb, GFP_KERNEL))
1211 printk("%s: usb_submit_urb failed\n",__FUNCTION__);
1212 /* enable irq pipe */
1213 ttusb_dec_send_command(dec,0xb0,sizeof(b),b,NULL,NULL);
1215 return 0;
1218 static void ttusb_dec_init_v_pes(struct ttusb_dec *dec)
1220 dprintk("%s\n", __FUNCTION__);
1222 dec->v_pes[0] = 0x00;
1223 dec->v_pes[1] = 0x00;
1224 dec->v_pes[2] = 0x01;
1225 dec->v_pes[3] = 0xe0;
1228 static int ttusb_dec_init_usb(struct ttusb_dec *dec)
1230 dprintk("%s\n", __FUNCTION__);
1232 sema_init(&dec->usb_sem, 1);
1233 sema_init(&dec->iso_sem, 1);
1235 dec->command_pipe = usb_sndbulkpipe(dec->udev, COMMAND_PIPE);
1236 dec->result_pipe = usb_rcvbulkpipe(dec->udev, RESULT_PIPE);
1237 dec->in_pipe = usb_rcvisocpipe(dec->udev, IN_PIPE);
1238 dec->out_pipe = usb_sndisocpipe(dec->udev, OUT_PIPE);
1239 dec->irq_pipe = usb_rcvintpipe(dec->udev, IRQ_PIPE);
1241 if(enable_rc) {
1242 dec->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
1243 if(!dec->irq_urb) {
1244 return -ENOMEM;
1246 dec->irq_buffer = usb_buffer_alloc(dec->udev,IRQ_PACKET_SIZE,
1247 SLAB_ATOMIC, &dec->irq_dma_handle);
1248 if(!dec->irq_buffer) {
1249 return -ENOMEM;
1251 usb_fill_int_urb(dec->irq_urb, dec->udev,dec->irq_pipe,
1252 dec->irq_buffer, IRQ_PACKET_SIZE,
1253 ttusb_dec_handle_irq, dec, 1);
1254 dec->irq_urb->transfer_dma = dec->irq_dma_handle;
1255 dec->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1258 return ttusb_dec_alloc_iso_urbs(dec);
1261 static int ttusb_dec_boot_dsp(struct ttusb_dec *dec)
1263 int i, j, actual_len, result, size, trans_count;
1264 u8 b0[] = { 0x00, 0x00, 0x00, 0x00,
1265 0x00, 0x00, 0x00, 0x00,
1266 0x61, 0x00 };
1267 u8 b1[] = { 0x61 };
1268 u8 *b;
1269 char idstring[21];
1270 u8 *firmware = NULL;
1271 size_t firmware_size = 0;
1272 u16 firmware_csum = 0;
1273 u16 firmware_csum_ns;
1274 u32 firmware_size_nl;
1275 u32 crc32_csum, crc32_check, tmp;
1276 const struct firmware *fw_entry = NULL;
1278 dprintk("%s\n", __FUNCTION__);
1280 if (request_firmware(&fw_entry, dec->firmware_name, &dec->udev->dev)) {
1281 printk(KERN_ERR "%s: Firmware (%s) unavailable.\n",
1282 __FUNCTION__, dec->firmware_name);
1283 return 1;
1286 firmware = fw_entry->data;
1287 firmware_size = fw_entry->size;
1289 if (firmware_size < 60) {
1290 printk("%s: firmware size too small for DSP code (%zu < 60).\n",
1291 __FUNCTION__, firmware_size);
1292 release_firmware(fw_entry);
1293 return -1;
1296 /* a 32 bit checksum over the first 56 bytes of the DSP Code is stored
1297 at offset 56 of file, so use it to check if the firmware file is
1298 valid. */
1299 crc32_csum = crc32(~0L, firmware, 56) ^ ~0L;
1300 memcpy(&tmp, &firmware[56], 4);
1301 crc32_check = htonl(tmp);
1302 if (crc32_csum != crc32_check) {
1303 printk("%s: crc32 check of DSP code failed (calculated "
1304 "0x%08x != 0x%08x in file), file invalid.\n",
1305 __FUNCTION__, crc32_csum, crc32_check);
1306 release_firmware(fw_entry);
1307 return -1;
1309 memcpy(idstring, &firmware[36], 20);
1310 idstring[20] = '\0';
1311 printk(KERN_INFO "ttusb_dec: found DSP code \"%s\".\n", idstring);
1313 firmware_size_nl = htonl(firmware_size);
1314 memcpy(b0, &firmware_size_nl, 4);
1315 firmware_csum = crc16(~0, firmware, firmware_size) ^ ~0;
1316 firmware_csum_ns = htons(firmware_csum);
1317 memcpy(&b0[6], &firmware_csum_ns, 2);
1319 result = ttusb_dec_send_command(dec, 0x41, sizeof(b0), b0, NULL, NULL);
1321 if (result) {
1322 release_firmware(fw_entry);
1323 return result;
1326 trans_count = 0;
1327 j = 0;
1329 b = kmalloc(ARM_PACKET_SIZE, GFP_KERNEL);
1330 if (b == NULL) {
1331 release_firmware(fw_entry);
1332 return -ENOMEM;
1335 for (i = 0; i < firmware_size; i += COMMAND_PACKET_SIZE) {
1336 size = firmware_size - i;
1337 if (size > COMMAND_PACKET_SIZE)
1338 size = COMMAND_PACKET_SIZE;
1340 b[j + 0] = 0xaa;
1341 b[j + 1] = trans_count++;
1342 b[j + 2] = 0xf0;
1343 b[j + 3] = size;
1344 memcpy(&b[j + 4], &firmware[i], size);
1346 j += COMMAND_PACKET_SIZE + 4;
1348 if (j >= ARM_PACKET_SIZE) {
1349 result = usb_bulk_msg(dec->udev, dec->command_pipe, b,
1350 ARM_PACKET_SIZE, &actual_len,
1351 100);
1352 j = 0;
1353 } else if (size < COMMAND_PACKET_SIZE) {
1354 result = usb_bulk_msg(dec->udev, dec->command_pipe, b,
1355 j - COMMAND_PACKET_SIZE + size,
1356 &actual_len, 100);
1360 result = ttusb_dec_send_command(dec, 0x43, sizeof(b1), b1, NULL, NULL);
1362 release_firmware(fw_entry);
1363 kfree(b);
1365 return result;
1368 static int ttusb_dec_init_stb(struct ttusb_dec *dec)
1370 int result;
1371 unsigned int mode, model, version;
1373 dprintk("%s\n", __FUNCTION__);
1375 result = ttusb_dec_get_stb_state(dec, &mode, &model, &version);
1377 if (!result) {
1378 if (!mode) {
1379 if (version == 0xABCDEFAB)
1380 printk(KERN_INFO "ttusb_dec: no version "
1381 "info in Firmware\n");
1382 else
1383 printk(KERN_INFO "ttusb_dec: Firmware "
1384 "%x.%02x%c%c\n",
1385 version >> 24, (version >> 16) & 0xff,
1386 (version >> 8) & 0xff, version & 0xff);
1388 result = ttusb_dec_boot_dsp(dec);
1389 if (result)
1390 return result;
1391 else
1392 return 1;
1393 } else {
1394 /* We can't trust the USB IDs that some firmwares
1395 give the box */
1396 switch (model) {
1397 case 0x00070001:
1398 case 0x00070008:
1399 case 0x0007000c:
1400 ttusb_dec_set_model(dec, TTUSB_DEC3000S);
1401 break;
1402 case 0x00070009:
1403 case 0x00070013:
1404 ttusb_dec_set_model(dec, TTUSB_DEC2000T);
1405 break;
1406 case 0x00070011:
1407 ttusb_dec_set_model(dec, TTUSB_DEC2540T);
1408 break;
1409 default:
1410 printk(KERN_ERR "%s: unknown model returned "
1411 "by firmware (%08x) - please report\n",
1412 __FUNCTION__, model);
1413 return -1;
1414 break;
1417 if (version >= 0x01770000)
1418 dec->can_playback = 1;
1420 return 0;
1423 else
1424 return result;
1427 static int ttusb_dec_init_dvb(struct ttusb_dec *dec)
1429 int result;
1431 dprintk("%s\n", __FUNCTION__);
1433 if ((result = dvb_register_adapter(&dec->adapter,
1434 dec->model_name, THIS_MODULE)) < 0) {
1435 printk("%s: dvb_register_adapter failed: error %d\n",
1436 __FUNCTION__, result);
1438 return result;
1441 dec->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING;
1443 dec->demux.priv = (void *)dec;
1444 dec->demux.filternum = 31;
1445 dec->demux.feednum = 31;
1446 dec->demux.start_feed = ttusb_dec_start_feed;
1447 dec->demux.stop_feed = ttusb_dec_stop_feed;
1448 dec->demux.write_to_decoder = NULL;
1450 if ((result = dvb_dmx_init(&dec->demux)) < 0) {
1451 printk("%s: dvb_dmx_init failed: error %d\n", __FUNCTION__,
1452 result);
1454 dvb_unregister_adapter(&dec->adapter);
1456 return result;
1459 dec->dmxdev.filternum = 32;
1460 dec->dmxdev.demux = &dec->demux.dmx;
1461 dec->dmxdev.capabilities = 0;
1463 if ((result = dvb_dmxdev_init(&dec->dmxdev, &dec->adapter)) < 0) {
1464 printk("%s: dvb_dmxdev_init failed: error %d\n",
1465 __FUNCTION__, result);
1467 dvb_dmx_release(&dec->demux);
1468 dvb_unregister_adapter(&dec->adapter);
1470 return result;
1473 dec->frontend.source = DMX_FRONTEND_0;
1475 if ((result = dec->demux.dmx.add_frontend(&dec->demux.dmx,
1476 &dec->frontend)) < 0) {
1477 printk("%s: dvb_dmx_init failed: error %d\n", __FUNCTION__,
1478 result);
1480 dvb_dmxdev_release(&dec->dmxdev);
1481 dvb_dmx_release(&dec->demux);
1482 dvb_unregister_adapter(&dec->adapter);
1484 return result;
1487 if ((result = dec->demux.dmx.connect_frontend(&dec->demux.dmx,
1488 &dec->frontend)) < 0) {
1489 printk("%s: dvb_dmx_init failed: error %d\n", __FUNCTION__,
1490 result);
1492 dec->demux.dmx.remove_frontend(&dec->demux.dmx, &dec->frontend);
1493 dvb_dmxdev_release(&dec->dmxdev);
1494 dvb_dmx_release(&dec->demux);
1495 dvb_unregister_adapter(&dec->adapter);
1497 return result;
1500 dvb_net_init(&dec->adapter, &dec->dvb_net, &dec->demux.dmx);
1502 return 0;
1505 static void ttusb_dec_exit_dvb(struct ttusb_dec *dec)
1507 dprintk("%s\n", __FUNCTION__);
1509 dvb_net_release(&dec->dvb_net);
1510 dec->demux.dmx.close(&dec->demux.dmx);
1511 dec->demux.dmx.remove_frontend(&dec->demux.dmx, &dec->frontend);
1512 dvb_dmxdev_release(&dec->dmxdev);
1513 dvb_dmx_release(&dec->demux);
1514 if (dec->fe) dvb_unregister_frontend(dec->fe);
1515 dvb_unregister_adapter(&dec->adapter);
1518 static void ttusb_dec_exit_rc(struct ttusb_dec *dec)
1521 dprintk("%s\n", __FUNCTION__);
1522 /* we have to check whether the irq URB is already submitted.
1523 * As the irq is submitted after the interface is changed,
1524 * this is the best method i figured out.
1525 * Any others?*/
1526 if (dec->interface == TTUSB_DEC_INTERFACE_IN)
1527 usb_kill_urb(dec->irq_urb);
1529 usb_free_urb(dec->irq_urb);
1531 usb_buffer_free(dec->udev,IRQ_PACKET_SIZE,
1532 dec->irq_buffer, dec->irq_dma_handle);
1534 if (dec->rc_input_dev) {
1535 input_unregister_device(dec->rc_input_dev);
1536 dec->rc_input_dev = NULL;
1541 static void ttusb_dec_exit_usb(struct ttusb_dec *dec)
1543 int i;
1545 dprintk("%s\n", __FUNCTION__);
1547 dec->iso_stream_count = 0;
1549 for (i = 0; i < ISO_BUF_COUNT; i++)
1550 usb_kill_urb(dec->iso_urb[i]);
1552 ttusb_dec_free_iso_urbs(dec);
1555 static void ttusb_dec_exit_tasklet(struct ttusb_dec *dec)
1557 struct list_head *item;
1558 struct urb_frame *frame;
1560 tasklet_kill(&dec->urb_tasklet);
1562 while ((item = dec->urb_frame_list.next) != &dec->urb_frame_list) {
1563 frame = list_entry(item, struct urb_frame, urb_frame_list);
1564 list_del(&frame->urb_frame_list);
1565 kfree(frame);
1569 static void ttusb_dec_init_filters(struct ttusb_dec *dec)
1571 INIT_LIST_HEAD(&dec->filter_info_list);
1572 spin_lock_init(&dec->filter_info_list_lock);
1575 static void ttusb_dec_exit_filters(struct ttusb_dec *dec)
1577 struct list_head *item;
1578 struct filter_info *finfo;
1580 while ((item = dec->filter_info_list.next) != &dec->filter_info_list) {
1581 finfo = list_entry(item, struct filter_info, filter_info_list);
1582 list_del(&finfo->filter_info_list);
1583 kfree(finfo);
1587 static int fe_send_command(struct dvb_frontend* fe, const u8 command,
1588 int param_length, const u8 params[],
1589 int *result_length, u8 cmd_result[])
1591 struct ttusb_dec* dec = fe->dvb->priv;
1592 return ttusb_dec_send_command(dec, command, param_length, params, result_length, cmd_result);
1595 static struct ttusbdecfe_config fe_config = {
1596 .send_command = fe_send_command
1599 static int ttusb_dec_probe(struct usb_interface *intf,
1600 const struct usb_device_id *id)
1602 struct usb_device *udev;
1603 struct ttusb_dec *dec;
1605 dprintk("%s\n", __FUNCTION__);
1607 udev = interface_to_usbdev(intf);
1609 if (!(dec = kzalloc(sizeof(struct ttusb_dec), GFP_KERNEL))) {
1610 printk("%s: couldn't allocate memory.\n", __FUNCTION__);
1611 return -ENOMEM;
1614 usb_set_intfdata(intf, (void *)dec);
1616 switch (le16_to_cpu(id->idProduct)) {
1617 case 0x1006:
1618 ttusb_dec_set_model(dec, TTUSB_DEC3000S);
1619 break;
1621 case 0x1008:
1622 ttusb_dec_set_model(dec, TTUSB_DEC2000T);
1623 break;
1625 case 0x1009:
1626 ttusb_dec_set_model(dec, TTUSB_DEC2540T);
1627 break;
1630 dec->udev = udev;
1632 if (ttusb_dec_init_usb(dec))
1633 return 0;
1634 if (ttusb_dec_init_stb(dec)) {
1635 ttusb_dec_exit_usb(dec);
1636 return 0;
1638 ttusb_dec_init_dvb(dec);
1640 dec->adapter.priv = dec;
1641 switch (le16_to_cpu(id->idProduct)) {
1642 case 0x1006:
1643 dec->fe = ttusbdecfe_dvbs_attach(&fe_config);
1644 break;
1646 case 0x1008:
1647 case 0x1009:
1648 dec->fe = ttusbdecfe_dvbt_attach(&fe_config);
1649 break;
1652 if (dec->fe == NULL) {
1653 printk("dvb-ttusb-dec: A frontend driver was not found for device %04x/%04x\n",
1654 le16_to_cpu(dec->udev->descriptor.idVendor),
1655 le16_to_cpu(dec->udev->descriptor.idProduct));
1656 } else {
1657 if (dvb_register_frontend(&dec->adapter, dec->fe)) {
1658 printk("budget-ci: Frontend registration failed!\n");
1659 if (dec->fe->ops->release)
1660 dec->fe->ops->release(dec->fe);
1661 dec->fe = NULL;
1665 ttusb_dec_init_v_pes(dec);
1666 ttusb_dec_init_filters(dec);
1667 ttusb_dec_init_tasklet(dec);
1669 dec->active = 1;
1671 ttusb_dec_set_interface(dec, TTUSB_DEC_INTERFACE_IN);
1673 if (enable_rc)
1674 ttusb_init_rc(dec);
1676 return 0;
1679 static void ttusb_dec_disconnect(struct usb_interface *intf)
1681 struct ttusb_dec *dec = usb_get_intfdata(intf);
1683 usb_set_intfdata(intf, NULL);
1685 dprintk("%s\n", __FUNCTION__);
1687 if (dec->active) {
1688 ttusb_dec_exit_tasklet(dec);
1689 ttusb_dec_exit_filters(dec);
1690 if(enable_rc)
1691 ttusb_dec_exit_rc(dec);
1692 ttusb_dec_exit_usb(dec);
1693 ttusb_dec_exit_dvb(dec);
1696 kfree(dec);
1699 static void ttusb_dec_set_model(struct ttusb_dec *dec,
1700 enum ttusb_dec_model model)
1702 dec->model = model;
1704 switch (model) {
1705 case TTUSB_DEC2000T:
1706 dec->model_name = "DEC2000-t";
1707 dec->firmware_name = "dvb-ttusb-dec-2000t.fw";
1708 break;
1710 case TTUSB_DEC2540T:
1711 dec->model_name = "DEC2540-t";
1712 dec->firmware_name = "dvb-ttusb-dec-2540t.fw";
1713 break;
1715 case TTUSB_DEC3000S:
1716 dec->model_name = "DEC3000-s";
1717 dec->firmware_name = "dvb-ttusb-dec-3000s.fw";
1718 break;
1722 static struct usb_device_id ttusb_dec_table[] = {
1723 {USB_DEVICE(0x0b48, 0x1006)}, /* DEC3000-s */
1724 /*{USB_DEVICE(0x0b48, 0x1007)}, Unconfirmed */
1725 {USB_DEVICE(0x0b48, 0x1008)}, /* DEC2000-t */
1726 {USB_DEVICE(0x0b48, 0x1009)}, /* DEC2540-t */
1730 static struct usb_driver ttusb_dec_driver = {
1731 .name = "ttusb-dec",
1732 .probe = ttusb_dec_probe,
1733 .disconnect = ttusb_dec_disconnect,
1734 .id_table = ttusb_dec_table,
1737 static int __init ttusb_dec_init(void)
1739 int result;
1741 if ((result = usb_register(&ttusb_dec_driver)) < 0) {
1742 printk("%s: initialisation failed: error %d.\n", __FUNCTION__,
1743 result);
1744 return result;
1747 return 0;
1750 static void __exit ttusb_dec_exit(void)
1752 usb_deregister(&ttusb_dec_driver);
1755 module_init(ttusb_dec_init);
1756 module_exit(ttusb_dec_exit);
1758 MODULE_AUTHOR("Alex Woods <linux-dvb@giblets.org>");
1759 MODULE_DESCRIPTION(DRIVER_NAME);
1760 MODULE_LICENSE("GPL");
1761 MODULE_DEVICE_TABLE(usb, ttusb_dec_table);