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 <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>
35 #include <linux/mutex.h>
38 #include "dvb_demux.h"
39 #include "dvb_filter.h"
40 #include "dvb_frontend.h"
42 #include "ttusbdecfe.h"
45 static int output_pva;
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
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 {
81 enum ttusb_dec_packet_type {
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
94 enum ttusb_dec_model model;
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];
110 struct usb_device *udev;
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 mutex usb_mutex;
122 dma_addr_t irq_dma_handle;
124 dma_addr_t iso_dma_handle;
125 struct urb *iso_urb[ISO_BUF_COUNT];
126 int iso_stream_count;
127 struct mutex iso_mutex;
129 u8 packet[MAX_PVA_LENGTH + 4];
130 enum ttusb_dec_packet_type packet_type;
133 int packet_payload_length;
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];
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;
158 int active; /* Loaded successfully */
162 u8 data[ISO_FRAME_SIZE];
164 struct list_head urb_frame_list;
169 struct dvb_demux_filter *filter;
170 struct list_head filter_info_list;
173 static u16 rc_keys[] = {
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)
207 struct ttusb_dec * dec = urb->context;
208 char *buffer = dec->irq_buffer;
211 switch(urb->status) {
218 /* this urb is dead, cleanup */
219 dprintk("%s:urb shutting down with status: %d\n",
220 __func__, urb->status);
223 dprintk("%s:nonzero status received: %d\n",
224 __func__,urb->status);
228 if( (buffer[0] == 0x1) && (buffer[2] == 0x15) ) {
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", __func__, buffer[4]);
239 input_report_key(dec->rc_input_dev, rc_keys[buffer[4] - 1], 1);
240 input_sync(dec->rc_input_dev);
241 input_report_key(dec->rc_input_dev, rc_keys[buffer[4] - 1], 0);
242 input_sync(dec->rc_input_dev);
245 exit: retval = usb_submit_urb(urb, GFP_ATOMIC);
247 printk("%s - usb_commit_urb failed with result: %d\n",
251 static u16 crc16(u16 crc, const u8 *buf, size_t len)
259 crc ^= (tmp ^ (tmp << 1)) << 4;
264 static int ttusb_dec_send_command(struct ttusb_dec *dec, const u8 command,
265 int param_length, const u8 params[],
266 int *result_length, u8 cmd_result[])
268 int result, actual_len, i;
271 dprintk("%s\n", __func__);
273 b = kmalloc(COMMAND_PACKET_SIZE + 4, GFP_KERNEL);
277 if ((result = mutex_lock_interruptible(&dec->usb_mutex))) {
279 printk("%s: Failed to lock usb mutex.\n", __func__);
284 b[1] = ++dec->trans_count;
289 memcpy(&b[4], params, param_length);
292 printk("%s: command: ", __func__);
293 for (i = 0; i < param_length + 4; i++)
294 printk("0x%02X ", b[i]);
298 result = usb_bulk_msg(dec->udev, dec->command_pipe, b,
299 COMMAND_PACKET_SIZE + 4, &actual_len, 1000);
302 printk("%s: command bulk message failed: error %d\n",
304 mutex_unlock(&dec->usb_mutex);
309 result = usb_bulk_msg(dec->udev, dec->result_pipe, b,
310 COMMAND_PACKET_SIZE + 4, &actual_len, 1000);
313 printk("%s: result bulk message failed: error %d\n",
315 mutex_unlock(&dec->usb_mutex);
320 printk("%s: result: ", __func__);
321 for (i = 0; i < actual_len; i++)
322 printk("0x%02X ", b[i]);
327 *result_length = b[3];
328 if (cmd_result && b[3] > 0)
329 memcpy(cmd_result, &b[4], b[3]);
331 mutex_unlock(&dec->usb_mutex);
338 static int ttusb_dec_get_stb_state (struct ttusb_dec *dec, unsigned int *mode,
339 unsigned int *model, unsigned int *version)
341 u8 c[COMMAND_PACKET_SIZE];
346 dprintk("%s\n", __func__);
348 result = ttusb_dec_send_command(dec, 0x08, 0, NULL, &c_length, c);
352 if (c_length >= 0x0c) {
358 memcpy(&tmp, &c[4], 4);
361 if (version != NULL) {
362 memcpy(&tmp, &c[8], 4);
363 *version = ntohl(tmp);
371 static int ttusb_dec_audio_pes2ts_cb(void *priv, unsigned char *data)
373 struct ttusb_dec *dec = priv;
375 dec->audio_filter->feed->cb.ts(data, 188, NULL, 0,
376 &dec->audio_filter->feed->feed.ts,
382 static int ttusb_dec_video_pes2ts_cb(void *priv, unsigned char *data)
384 struct ttusb_dec *dec = priv;
386 dec->video_filter->feed->cb.ts(data, 188, NULL, 0,
387 &dec->video_filter->feed->feed.ts,
393 static void ttusb_dec_set_pids(struct ttusb_dec *dec)
395 u8 b[] = { 0x00, 0x00, 0x00, 0x00,
396 0x00, 0x00, 0xff, 0xff,
397 0xff, 0xff, 0xff, 0xff };
399 u16 pcr = htons(dec->pid[DMX_PES_PCR]);
400 u16 audio = htons(dec->pid[DMX_PES_AUDIO]);
401 u16 video = htons(dec->pid[DMX_PES_VIDEO]);
403 dprintk("%s\n", __func__);
405 memcpy(&b[0], &pcr, 2);
406 memcpy(&b[2], &audio, 2);
407 memcpy(&b[4], &video, 2);
409 ttusb_dec_send_command(dec, 0x50, sizeof(b), b, NULL, NULL);
411 dvb_filter_pes2ts_init(&dec->a_pes2ts, dec->pid[DMX_PES_AUDIO],
412 ttusb_dec_audio_pes2ts_cb, dec);
413 dvb_filter_pes2ts_init(&dec->v_pes2ts, dec->pid[DMX_PES_VIDEO],
414 ttusb_dec_video_pes2ts_cb, dec);
415 dec->v_pes_length = 0;
416 dec->v_pes_postbytes = 0;
419 static void ttusb_dec_process_pva(struct ttusb_dec *dec, u8 *pva, int length)
422 printk("%s: packet too short - discarding\n", __func__);
426 if (length > 8 + MAX_PVA_LENGTH) {
427 printk("%s: packet too long - discarding\n", __func__);
433 case 0x01: { /* VideoStream */
434 int prebytes = pva[5] & 0x03;
435 int postbytes = (pva[5] & 0x0c) >> 2;
436 u16 v_pes_payload_length;
439 dec->video_filter->feed->cb.ts(pva, length, NULL, 0,
440 &dec->video_filter->feed->feed.ts, DMX_OK);
444 if (dec->v_pes_postbytes > 0 &&
445 dec->v_pes_postbytes == prebytes) {
446 memcpy(&dec->v_pes[dec->v_pes_length],
449 dvb_filter_pes2ts(&dec->v_pes2ts, dec->v_pes,
450 dec->v_pes_length + prebytes, 1);
454 dec->v_pes[7] = 0x80;
455 dec->v_pes[8] = 0x05;
457 dec->v_pes[9] = 0x21 | ((pva[8] & 0xc0) >> 5);
458 dec->v_pes[10] = ((pva[8] & 0x3f) << 2) |
459 ((pva[9] & 0xc0) >> 6);
460 dec->v_pes[11] = 0x01 |
461 ((pva[9] & 0x3f) << 2) |
462 ((pva[10] & 0x80) >> 6);
463 dec->v_pes[12] = ((pva[10] & 0x7f) << 1) |
464 ((pva[11] & 0xc0) >> 7);
465 dec->v_pes[13] = 0x01 | ((pva[11] & 0x7f) << 1);
467 memcpy(&dec->v_pes[14], &pva[12 + prebytes],
468 length - 12 - prebytes);
469 dec->v_pes_length = 14 + length - 12 - prebytes;
471 dec->v_pes[7] = 0x00;
472 dec->v_pes[8] = 0x00;
474 memcpy(&dec->v_pes[9], &pva[8], length - 8);
475 dec->v_pes_length = 9 + length - 8;
478 dec->v_pes_postbytes = postbytes;
480 if (dec->v_pes[9 + dec->v_pes[8]] == 0x00 &&
481 dec->v_pes[10 + dec->v_pes[8]] == 0x00 &&
482 dec->v_pes[11 + dec->v_pes[8]] == 0x01)
483 dec->v_pes[6] = 0x84;
485 dec->v_pes[6] = 0x80;
487 v_pes_payload_length = htons(dec->v_pes_length - 6 +
489 memcpy(&dec->v_pes[4], &v_pes_payload_length, 2);
492 dvb_filter_pes2ts(&dec->v_pes2ts, dec->v_pes,
493 dec->v_pes_length, 1);
498 case 0x02: /* MainAudioStream */
500 dec->audio_filter->feed->cb.ts(pva, length, NULL, 0,
501 &dec->audio_filter->feed->feed.ts, DMX_OK);
505 dvb_filter_pes2ts(&dec->a_pes2ts, &pva[8], length - 8,
510 printk("%s: unknown PVA type: %02x.\n", __func__,
516 static void ttusb_dec_process_filter(struct ttusb_dec *dec, u8 *packet,
519 struct list_head *item;
520 struct filter_info *finfo;
521 struct dvb_demux_filter *filter = NULL;
526 spin_lock_irqsave(&dec->filter_info_list_lock, flags);
527 for (item = dec->filter_info_list.next; item != &dec->filter_info_list;
529 finfo = list_entry(item, struct filter_info, filter_info_list);
530 if (finfo->stream_id == sid) {
531 filter = finfo->filter;
535 spin_unlock_irqrestore(&dec->filter_info_list_lock, flags);
538 filter->feed->cb.sec(&packet[2], length - 2, NULL, 0,
539 &filter->filter, DMX_OK);
542 static void ttusb_dec_process_packet(struct ttusb_dec *dec)
548 if (dec->packet_length % 2) {
549 printk("%s: odd sized packet - discarding\n", __func__);
553 for (i = 0; i < dec->packet_length; i += 2)
554 csum ^= ((dec->packet[i] << 8) + dec->packet[i + 1]);
557 printk("%s: checksum failed - discarding\n", __func__);
561 packet_id = dec->packet[dec->packet_length - 4] << 8;
562 packet_id += dec->packet[dec->packet_length - 3];
564 if ((packet_id != dec->next_packet_id) && dec->next_packet_id) {
565 printk("%s: warning: lost packets between %u and %u\n",
566 __func__, dec->next_packet_id - 1, packet_id);
569 if (packet_id == 0xffff)
570 dec->next_packet_id = 0x8000;
572 dec->next_packet_id = packet_id + 1;
574 switch (dec->packet_type) {
575 case TTUSB_DEC_PACKET_PVA:
576 if (dec->pva_stream_count)
577 ttusb_dec_process_pva(dec, dec->packet,
578 dec->packet_payload_length);
581 case TTUSB_DEC_PACKET_SECTION:
582 if (dec->filter_stream_count)
583 ttusb_dec_process_filter(dec, dec->packet,
584 dec->packet_payload_length);
587 case TTUSB_DEC_PACKET_EMPTY:
592 static void swap_bytes(u8 *b, int length)
596 length -= length % 2;
597 for (; length; b += 2, length -= 2) {
604 static void ttusb_dec_process_urb_frame(struct ttusb_dec *dec, u8 *b,
607 swap_bytes(b, length);
610 switch (dec->packet_state) {
618 dec->packet_state = 0;
626 dec->packet_length = 0;
627 } else if (*b != 0xaa) {
628 dec->packet_state = 0;
636 dec->packet[dec->packet_length++] = *b++;
638 if (dec->packet_length == 2) {
639 if (dec->packet[0] == 'A' &&
640 dec->packet[1] == 'V') {
642 TTUSB_DEC_PACKET_PVA;
644 } else if (dec->packet[0] == 'S') {
646 TTUSB_DEC_PACKET_SECTION;
648 } else if (dec->packet[0] == 0x00) {
650 TTUSB_DEC_PACKET_EMPTY;
651 dec->packet_payload_length = 2;
652 dec->packet_state = 7;
654 printk("%s: unknown packet type: "
655 "%02x%02x\n", __func__,
656 dec->packet[0], dec->packet[1]);
657 dec->packet_state = 0;
665 dec->packet[dec->packet_length++] = *b++;
667 if (dec->packet_type == TTUSB_DEC_PACKET_PVA &&
668 dec->packet_length == 8) {
670 dec->packet_payload_length = 8 +
671 (dec->packet[6] << 8) +
673 } else if (dec->packet_type ==
674 TTUSB_DEC_PACKET_SECTION &&
675 dec->packet_length == 5) {
677 dec->packet_payload_length = 5 +
678 ((dec->packet[3] & 0x0f) << 8) +
686 int remainder = dec->packet_payload_length -
689 if (length >= remainder) {
690 memcpy(dec->packet + dec->packet_length,
692 dec->packet_length += remainder;
697 memcpy(&dec->packet[dec->packet_length],
699 dec->packet_length += length;
709 dec->packet[dec->packet_length++] = *b++;
711 if (dec->packet_type == TTUSB_DEC_PACKET_SECTION &&
712 dec->packet_payload_length % 2)
715 if (dec->packet_length ==
716 dec->packet_payload_length + tail) {
717 ttusb_dec_process_packet(dec);
718 dec->packet_state = 0;
726 printk("%s: illegal packet state encountered.\n",
728 dec->packet_state = 0;
733 static void ttusb_dec_process_urb_frame_list(unsigned long data)
735 struct ttusb_dec *dec = (struct ttusb_dec *)data;
736 struct list_head *item;
737 struct urb_frame *frame;
741 spin_lock_irqsave(&dec->urb_frame_list_lock, flags);
742 if ((item = dec->urb_frame_list.next) != &dec->urb_frame_list) {
743 frame = list_entry(item, struct urb_frame,
745 list_del(&frame->urb_frame_list);
747 spin_unlock_irqrestore(&dec->urb_frame_list_lock,
751 spin_unlock_irqrestore(&dec->urb_frame_list_lock, flags);
753 ttusb_dec_process_urb_frame(dec, frame->data, frame->length);
758 static void ttusb_dec_process_urb(struct urb *urb)
760 struct ttusb_dec *dec = urb->context;
765 for (i = 0; i < FRAMES_PER_ISO_BUF; i++) {
766 struct usb_iso_packet_descriptor *d;
769 struct urb_frame *frame;
771 d = &urb->iso_frame_desc[i];
772 b = urb->transfer_buffer + d->offset;
773 length = d->actual_length;
775 if ((frame = kmalloc(sizeof(struct urb_frame),
779 memcpy(frame->data, b, length);
780 frame->length = length;
782 spin_lock_irqsave(&dec->urb_frame_list_lock,
784 list_add_tail(&frame->urb_frame_list,
785 &dec->urb_frame_list);
786 spin_unlock_irqrestore(&dec->urb_frame_list_lock,
789 tasklet_schedule(&dec->urb_tasklet);
793 /* -ENOENT is expected when unlinking urbs */
794 if (urb->status != -ENOENT)
795 dprintk("%s: urb error: %d\n", __func__,
799 if (dec->iso_stream_count)
800 usb_submit_urb(urb, GFP_ATOMIC);
803 static void ttusb_dec_setup_urbs(struct ttusb_dec *dec)
805 int i, j, buffer_offset = 0;
807 dprintk("%s\n", __func__);
809 for (i = 0; i < ISO_BUF_COUNT; i++) {
810 int frame_offset = 0;
811 struct urb *urb = dec->iso_urb[i];
813 urb->dev = dec->udev;
815 urb->complete = ttusb_dec_process_urb;
816 urb->pipe = dec->in_pipe;
817 urb->transfer_flags = URB_ISO_ASAP;
819 urb->number_of_packets = FRAMES_PER_ISO_BUF;
820 urb->transfer_buffer_length = ISO_FRAME_SIZE *
822 urb->transfer_buffer = dec->iso_buffer + buffer_offset;
823 buffer_offset += ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF;
825 for (j = 0; j < FRAMES_PER_ISO_BUF; j++) {
826 urb->iso_frame_desc[j].offset = frame_offset;
827 urb->iso_frame_desc[j].length = ISO_FRAME_SIZE;
828 frame_offset += ISO_FRAME_SIZE;
833 static void ttusb_dec_stop_iso_xfer(struct ttusb_dec *dec)
837 dprintk("%s\n", __func__);
839 if (mutex_lock_interruptible(&dec->iso_mutex))
842 dec->iso_stream_count--;
844 if (!dec->iso_stream_count) {
845 for (i = 0; i < ISO_BUF_COUNT; i++)
846 usb_kill_urb(dec->iso_urb[i]);
849 mutex_unlock(&dec->iso_mutex);
852 /* Setting the interface of the DEC tends to take down the USB communications
853 * for a short period, so it's important not to call this function just before
854 * trying to talk to it.
856 static int ttusb_dec_set_interface(struct ttusb_dec *dec,
857 enum ttusb_dec_interface interface)
862 if (interface != dec->interface) {
864 case TTUSB_DEC_INTERFACE_INITIAL:
865 result = usb_set_interface(dec->udev, 0, 0);
867 case TTUSB_DEC_INTERFACE_IN:
868 result = ttusb_dec_send_command(dec, 0x80, sizeof(b),
872 result = usb_set_interface(dec->udev, 0, 8);
874 case TTUSB_DEC_INTERFACE_OUT:
875 result = usb_set_interface(dec->udev, 0, 1);
882 dec->interface = interface;
888 static int ttusb_dec_start_iso_xfer(struct ttusb_dec *dec)
892 dprintk("%s\n", __func__);
894 if (mutex_lock_interruptible(&dec->iso_mutex))
897 if (!dec->iso_stream_count) {
898 ttusb_dec_setup_urbs(dec);
900 dec->packet_state = 0;
901 dec->v_pes_postbytes = 0;
902 dec->next_packet_id = 0;
904 for (i = 0; i < ISO_BUF_COUNT; i++) {
905 if ((result = usb_submit_urb(dec->iso_urb[i],
907 printk("%s: failed urb submission %d: "
908 "error %d\n", __func__, i, result);
911 usb_kill_urb(dec->iso_urb[i - 1]);
915 mutex_unlock(&dec->iso_mutex);
921 dec->iso_stream_count++;
923 mutex_unlock(&dec->iso_mutex);
928 static int ttusb_dec_start_ts_feed(struct dvb_demux_feed *dvbdmxfeed)
930 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
931 struct ttusb_dec *dec = dvbdmx->priv;
935 dprintk("%s\n", __func__);
937 dprintk(" ts_type:");
939 if (dvbdmxfeed->ts_type & TS_DECODER)
940 dprintk(" TS_DECODER");
942 if (dvbdmxfeed->ts_type & TS_PACKET)
943 dprintk(" TS_PACKET");
945 if (dvbdmxfeed->ts_type & TS_PAYLOAD_ONLY)
946 dprintk(" TS_PAYLOAD_ONLY");
950 switch (dvbdmxfeed->pes_type) {
952 case DMX_TS_PES_VIDEO:
953 dprintk(" pes_type: DMX_TS_PES_VIDEO\n");
954 dec->pid[DMX_PES_PCR] = dvbdmxfeed->pid;
955 dec->pid[DMX_PES_VIDEO] = dvbdmxfeed->pid;
956 dec->video_filter = dvbdmxfeed->filter;
957 ttusb_dec_set_pids(dec);
960 case DMX_TS_PES_AUDIO:
961 dprintk(" pes_type: DMX_TS_PES_AUDIO\n");
962 dec->pid[DMX_PES_AUDIO] = dvbdmxfeed->pid;
963 dec->audio_filter = dvbdmxfeed->filter;
964 ttusb_dec_set_pids(dec);
967 case DMX_TS_PES_TELETEXT:
968 dec->pid[DMX_PES_TELETEXT] = dvbdmxfeed->pid;
969 dprintk(" pes_type: DMX_TS_PES_TELETEXT(not supported)\n");
973 dprintk(" pes_type: DMX_TS_PES_PCR\n");
974 dec->pid[DMX_PES_PCR] = dvbdmxfeed->pid;
975 ttusb_dec_set_pids(dec);
978 case DMX_TS_PES_OTHER:
979 dprintk(" pes_type: DMX_TS_PES_OTHER(not supported)\n");
983 dprintk(" pes_type: unknown (%d)\n", dvbdmxfeed->pes_type);
988 result = ttusb_dec_send_command(dec, 0x80, sizeof(b0), b0, NULL, NULL);
992 dec->pva_stream_count++;
993 return ttusb_dec_start_iso_xfer(dec);
996 static int ttusb_dec_start_sec_feed(struct dvb_demux_feed *dvbdmxfeed)
998 struct ttusb_dec *dec = dvbdmxfeed->demux->priv;
999 u8 b0[] = { 0x00, 0x00, 0x00, 0x01,
1000 0x00, 0x00, 0x00, 0x00,
1001 0x00, 0x00, 0x00, 0x00,
1002 0x00, 0x00, 0x00, 0x00,
1003 0x00, 0xff, 0x00, 0x00,
1004 0x00, 0x00, 0x00, 0x00,
1005 0x00, 0x00, 0x00, 0x00,
1008 u8 c[COMMAND_PACKET_SIZE];
1011 struct filter_info *finfo;
1012 unsigned long flags;
1015 dprintk("%s\n", __func__);
1017 pid = htons(dvbdmxfeed->pid);
1018 memcpy(&b0[0], &pid, 2);
1019 memcpy(&b0[4], &x, 1);
1020 memcpy(&b0[5], &dvbdmxfeed->filter->filter.filter_value[0], 1);
1022 result = ttusb_dec_send_command(dec, 0x60, sizeof(b0), b0,
1026 if (c_length == 2) {
1027 if (!(finfo = kmalloc(sizeof(struct filter_info),
1031 finfo->stream_id = c[1];
1032 finfo->filter = dvbdmxfeed->filter;
1034 spin_lock_irqsave(&dec->filter_info_list_lock, flags);
1035 list_add_tail(&finfo->filter_info_list,
1036 &dec->filter_info_list);
1037 spin_unlock_irqrestore(&dec->filter_info_list_lock,
1040 dvbdmxfeed->priv = finfo;
1042 dec->filter_stream_count++;
1043 return ttusb_dec_start_iso_xfer(dec);
1051 static int ttusb_dec_start_feed(struct dvb_demux_feed *dvbdmxfeed)
1053 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
1055 dprintk("%s\n", __func__);
1057 if (!dvbdmx->dmx.frontend)
1060 dprintk(" pid: 0x%04X\n", dvbdmxfeed->pid);
1062 switch (dvbdmxfeed->type) {
1065 return ttusb_dec_start_ts_feed(dvbdmxfeed);
1069 return ttusb_dec_start_sec_feed(dvbdmxfeed);
1073 dprintk(" type: unknown (%d)\n", dvbdmxfeed->type);
1079 static int ttusb_dec_stop_ts_feed(struct dvb_demux_feed *dvbdmxfeed)
1081 struct ttusb_dec *dec = dvbdmxfeed->demux->priv;
1084 ttusb_dec_send_command(dec, 0x81, sizeof(b0), b0, NULL, NULL);
1086 dec->pva_stream_count--;
1088 ttusb_dec_stop_iso_xfer(dec);
1093 static int ttusb_dec_stop_sec_feed(struct dvb_demux_feed *dvbdmxfeed)
1095 struct ttusb_dec *dec = dvbdmxfeed->demux->priv;
1096 u8 b0[] = { 0x00, 0x00 };
1097 struct filter_info *finfo = (struct filter_info *)dvbdmxfeed->priv;
1098 unsigned long flags;
1100 b0[1] = finfo->stream_id;
1101 spin_lock_irqsave(&dec->filter_info_list_lock, flags);
1102 list_del(&finfo->filter_info_list);
1103 spin_unlock_irqrestore(&dec->filter_info_list_lock, flags);
1105 ttusb_dec_send_command(dec, 0x62, sizeof(b0), b0, NULL, NULL);
1107 dec->filter_stream_count--;
1109 ttusb_dec_stop_iso_xfer(dec);
1114 static int ttusb_dec_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
1116 dprintk("%s\n", __func__);
1118 switch (dvbdmxfeed->type) {
1120 return ttusb_dec_stop_ts_feed(dvbdmxfeed);
1124 return ttusb_dec_stop_sec_feed(dvbdmxfeed);
1131 static void ttusb_dec_free_iso_urbs(struct ttusb_dec *dec)
1135 dprintk("%s\n", __func__);
1137 for (i = 0; i < ISO_BUF_COUNT; i++)
1138 usb_free_urb(dec->iso_urb[i]);
1140 pci_free_consistent(NULL,
1141 ISO_FRAME_SIZE * (FRAMES_PER_ISO_BUF *
1143 dec->iso_buffer, dec->iso_dma_handle);
1146 static int ttusb_dec_alloc_iso_urbs(struct ttusb_dec *dec)
1150 dprintk("%s\n", __func__);
1152 dec->iso_buffer = pci_alloc_consistent(NULL,
1154 (FRAMES_PER_ISO_BUF *
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++) {
1164 if (!(urb = usb_alloc_urb(FRAMES_PER_ISO_BUF, GFP_ATOMIC))) {
1165 ttusb_dec_free_iso_urbs(dec);
1169 dec->iso_urb[i] = urb;
1172 ttusb_dec_setup_urbs(dec);
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 };
1192 usb_make_path(dec->udev, dec->rc_phys, sizeof(dec->rc_phys));
1193 strlcpy(dec->rc_phys, "/input0", sizeof(dec->rc_phys));
1195 input_dev = input_allocate_device();
1199 input_dev->name = "ttusb_dec remote control";
1200 input_dev->phys = dec->rc_phys;
1201 input_dev->evbit[0] = BIT_MASK(EV_KEY);
1202 input_dev->keycodesize = sizeof(u16);
1203 input_dev->keycodemax = 0x1a;
1204 input_dev->keycode = rc_keys;
1206 for (i = 0; i < ARRAY_SIZE(rc_keys); i++)
1207 set_bit(rc_keys[i], input_dev->keybit);
1209 err = input_register_device(input_dev);
1211 input_free_device(input_dev);
1215 dec->rc_input_dev = input_dev;
1216 if (usb_submit_urb(dec->irq_urb, GFP_KERNEL))
1217 printk("%s: usb_submit_urb failed\n",__func__);
1218 /* enable irq pipe */
1219 ttusb_dec_send_command(dec,0xb0,sizeof(b),b,NULL,NULL);
1224 static void ttusb_dec_init_v_pes(struct ttusb_dec *dec)
1226 dprintk("%s\n", __func__);
1228 dec->v_pes[0] = 0x00;
1229 dec->v_pes[1] = 0x00;
1230 dec->v_pes[2] = 0x01;
1231 dec->v_pes[3] = 0xe0;
1234 static int ttusb_dec_init_usb(struct ttusb_dec *dec)
1236 dprintk("%s\n", __func__);
1238 mutex_init(&dec->usb_mutex);
1239 mutex_init(&dec->iso_mutex);
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);
1248 dec->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
1252 dec->irq_buffer = usb_buffer_alloc(dec->udev,IRQ_PACKET_SIZE,
1253 GFP_ATOMIC, &dec->irq_dma_handle);
1254 if(!dec->irq_buffer) {
1257 usb_fill_int_urb(dec->irq_urb, dec->udev,dec->irq_pipe,
1258 dec->irq_buffer, IRQ_PACKET_SIZE,
1259 ttusb_dec_handle_irq, dec, 1);
1260 dec->irq_urb->transfer_dma = dec->irq_dma_handle;
1261 dec->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1264 return ttusb_dec_alloc_iso_urbs(dec);
1267 static int ttusb_dec_boot_dsp(struct ttusb_dec *dec)
1269 int i, j, actual_len, result, size, trans_count;
1270 u8 b0[] = { 0x00, 0x00, 0x00, 0x00,
1271 0x00, 0x00, 0x00, 0x00,
1276 u8 *firmware = NULL;
1277 size_t firmware_size = 0;
1278 u16 firmware_csum = 0;
1279 u16 firmware_csum_ns;
1280 u32 firmware_size_nl;
1281 u32 crc32_csum, crc32_check, tmp;
1282 const struct firmware *fw_entry = NULL;
1284 dprintk("%s\n", __func__);
1286 if (request_firmware(&fw_entry, dec->firmware_name, &dec->udev->dev)) {
1287 printk(KERN_ERR "%s: Firmware (%s) unavailable.\n",
1288 __func__, dec->firmware_name);
1292 firmware = fw_entry->data;
1293 firmware_size = fw_entry->size;
1295 if (firmware_size < 60) {
1296 printk("%s: firmware size too small for DSP code (%zu < 60).\n",
1297 __func__, firmware_size);
1298 release_firmware(fw_entry);
1302 /* a 32 bit checksum over the first 56 bytes of the DSP Code is stored
1303 at offset 56 of file, so use it to check if the firmware file is
1305 crc32_csum = crc32(~0L, firmware, 56) ^ ~0L;
1306 memcpy(&tmp, &firmware[56], 4);
1307 crc32_check = htonl(tmp);
1308 if (crc32_csum != crc32_check) {
1309 printk("%s: crc32 check of DSP code failed (calculated "
1310 "0x%08x != 0x%08x in file), file invalid.\n",
1311 __func__, crc32_csum, crc32_check);
1312 release_firmware(fw_entry);
1315 memcpy(idstring, &firmware[36], 20);
1316 idstring[20] = '\0';
1317 printk(KERN_INFO "ttusb_dec: found DSP code \"%s\".\n", idstring);
1319 firmware_size_nl = htonl(firmware_size);
1320 memcpy(b0, &firmware_size_nl, 4);
1321 firmware_csum = crc16(~0, firmware, firmware_size) ^ ~0;
1322 firmware_csum_ns = htons(firmware_csum);
1323 memcpy(&b0[6], &firmware_csum_ns, 2);
1325 result = ttusb_dec_send_command(dec, 0x41, sizeof(b0), b0, NULL, NULL);
1328 release_firmware(fw_entry);
1335 b = kmalloc(ARM_PACKET_SIZE, GFP_KERNEL);
1337 release_firmware(fw_entry);
1341 for (i = 0; i < firmware_size; i += COMMAND_PACKET_SIZE) {
1342 size = firmware_size - i;
1343 if (size > COMMAND_PACKET_SIZE)
1344 size = COMMAND_PACKET_SIZE;
1347 b[j + 1] = trans_count++;
1350 memcpy(&b[j + 4], &firmware[i], size);
1352 j += COMMAND_PACKET_SIZE + 4;
1354 if (j >= ARM_PACKET_SIZE) {
1355 result = usb_bulk_msg(dec->udev, dec->command_pipe, b,
1356 ARM_PACKET_SIZE, &actual_len,
1359 } else if (size < COMMAND_PACKET_SIZE) {
1360 result = usb_bulk_msg(dec->udev, dec->command_pipe, b,
1361 j - COMMAND_PACKET_SIZE + size,
1366 result = ttusb_dec_send_command(dec, 0x43, sizeof(b1), b1, NULL, NULL);
1368 release_firmware(fw_entry);
1374 static int ttusb_dec_init_stb(struct ttusb_dec *dec)
1377 unsigned int mode, model, version;
1379 dprintk("%s\n", __func__);
1381 result = ttusb_dec_get_stb_state(dec, &mode, &model, &version);
1385 if (version == 0xABCDEFAB)
1386 printk(KERN_INFO "ttusb_dec: no version "
1387 "info in Firmware\n");
1389 printk(KERN_INFO "ttusb_dec: Firmware "
1391 version >> 24, (version >> 16) & 0xff,
1392 (version >> 8) & 0xff, version & 0xff);
1394 result = ttusb_dec_boot_dsp(dec);
1400 /* We can't trust the USB IDs that some firmwares
1406 ttusb_dec_set_model(dec, TTUSB_DEC3000S);
1410 ttusb_dec_set_model(dec, TTUSB_DEC2000T);
1413 ttusb_dec_set_model(dec, TTUSB_DEC2540T);
1416 printk(KERN_ERR "%s: unknown model returned "
1417 "by firmware (%08x) - please report\n",
1423 if (version >= 0x01770000)
1424 dec->can_playback = 1;
1433 static int ttusb_dec_init_dvb(struct ttusb_dec *dec)
1437 dprintk("%s\n", __func__);
1439 if ((result = dvb_register_adapter(&dec->adapter,
1440 dec->model_name, THIS_MODULE, &dec->udev->dev)) < 0) {
1441 printk("%s: dvb_register_adapter failed: error %d\n",
1447 dec->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING;
1449 dec->demux.priv = (void *)dec;
1450 dec->demux.filternum = 31;
1451 dec->demux.feednum = 31;
1452 dec->demux.start_feed = ttusb_dec_start_feed;
1453 dec->demux.stop_feed = ttusb_dec_stop_feed;
1454 dec->demux.write_to_decoder = NULL;
1456 if ((result = dvb_dmx_init(&dec->demux)) < 0) {
1457 printk("%s: dvb_dmx_init failed: error %d\n", __func__,
1460 dvb_unregister_adapter(&dec->adapter);
1465 dec->dmxdev.filternum = 32;
1466 dec->dmxdev.demux = &dec->demux.dmx;
1467 dec->dmxdev.capabilities = 0;
1469 if ((result = dvb_dmxdev_init(&dec->dmxdev, &dec->adapter)) < 0) {
1470 printk("%s: dvb_dmxdev_init failed: error %d\n",
1473 dvb_dmx_release(&dec->demux);
1474 dvb_unregister_adapter(&dec->adapter);
1479 dec->frontend.source = DMX_FRONTEND_0;
1481 if ((result = dec->demux.dmx.add_frontend(&dec->demux.dmx,
1482 &dec->frontend)) < 0) {
1483 printk("%s: dvb_dmx_init failed: error %d\n", __func__,
1486 dvb_dmxdev_release(&dec->dmxdev);
1487 dvb_dmx_release(&dec->demux);
1488 dvb_unregister_adapter(&dec->adapter);
1493 if ((result = dec->demux.dmx.connect_frontend(&dec->demux.dmx,
1494 &dec->frontend)) < 0) {
1495 printk("%s: dvb_dmx_init failed: error %d\n", __func__,
1498 dec->demux.dmx.remove_frontend(&dec->demux.dmx, &dec->frontend);
1499 dvb_dmxdev_release(&dec->dmxdev);
1500 dvb_dmx_release(&dec->demux);
1501 dvb_unregister_adapter(&dec->adapter);
1506 dvb_net_init(&dec->adapter, &dec->dvb_net, &dec->demux.dmx);
1511 static void ttusb_dec_exit_dvb(struct ttusb_dec *dec)
1513 dprintk("%s\n", __func__);
1515 dvb_net_release(&dec->dvb_net);
1516 dec->demux.dmx.close(&dec->demux.dmx);
1517 dec->demux.dmx.remove_frontend(&dec->demux.dmx, &dec->frontend);
1518 dvb_dmxdev_release(&dec->dmxdev);
1519 dvb_dmx_release(&dec->demux);
1521 dvb_unregister_frontend(dec->fe);
1522 if (dec->fe->ops.release)
1523 dec->fe->ops.release(dec->fe);
1525 dvb_unregister_adapter(&dec->adapter);
1528 static void ttusb_dec_exit_rc(struct ttusb_dec *dec)
1531 dprintk("%s\n", __func__);
1532 /* we have to check whether the irq URB is already submitted.
1533 * As the irq is submitted after the interface is changed,
1534 * this is the best method i figured out.
1536 if (dec->interface == TTUSB_DEC_INTERFACE_IN)
1537 usb_kill_urb(dec->irq_urb);
1539 usb_free_urb(dec->irq_urb);
1541 usb_buffer_free(dec->udev,IRQ_PACKET_SIZE,
1542 dec->irq_buffer, dec->irq_dma_handle);
1544 if (dec->rc_input_dev) {
1545 input_unregister_device(dec->rc_input_dev);
1546 dec->rc_input_dev = NULL;
1551 static void ttusb_dec_exit_usb(struct ttusb_dec *dec)
1555 dprintk("%s\n", __func__);
1557 dec->iso_stream_count = 0;
1559 for (i = 0; i < ISO_BUF_COUNT; i++)
1560 usb_kill_urb(dec->iso_urb[i]);
1562 ttusb_dec_free_iso_urbs(dec);
1565 static void ttusb_dec_exit_tasklet(struct ttusb_dec *dec)
1567 struct list_head *item;
1568 struct urb_frame *frame;
1570 tasklet_kill(&dec->urb_tasklet);
1572 while ((item = dec->urb_frame_list.next) != &dec->urb_frame_list) {
1573 frame = list_entry(item, struct urb_frame, urb_frame_list);
1574 list_del(&frame->urb_frame_list);
1579 static void ttusb_dec_init_filters(struct ttusb_dec *dec)
1581 INIT_LIST_HEAD(&dec->filter_info_list);
1582 spin_lock_init(&dec->filter_info_list_lock);
1585 static void ttusb_dec_exit_filters(struct ttusb_dec *dec)
1587 struct list_head *item;
1588 struct filter_info *finfo;
1590 while ((item = dec->filter_info_list.next) != &dec->filter_info_list) {
1591 finfo = list_entry(item, struct filter_info, filter_info_list);
1592 list_del(&finfo->filter_info_list);
1597 static int fe_send_command(struct dvb_frontend* fe, const u8 command,
1598 int param_length, const u8 params[],
1599 int *result_length, u8 cmd_result[])
1601 struct ttusb_dec* dec = fe->dvb->priv;
1602 return ttusb_dec_send_command(dec, command, param_length, params, result_length, cmd_result);
1605 static struct ttusbdecfe_config fe_config = {
1606 .send_command = fe_send_command
1609 static int ttusb_dec_probe(struct usb_interface *intf,
1610 const struct usb_device_id *id)
1612 struct usb_device *udev;
1613 struct ttusb_dec *dec;
1615 dprintk("%s\n", __func__);
1617 udev = interface_to_usbdev(intf);
1619 if (!(dec = kzalloc(sizeof(struct ttusb_dec), GFP_KERNEL))) {
1620 printk("%s: couldn't allocate memory.\n", __func__);
1624 usb_set_intfdata(intf, (void *)dec);
1626 switch (le16_to_cpu(id->idProduct)) {
1628 ttusb_dec_set_model(dec, TTUSB_DEC3000S);
1632 ttusb_dec_set_model(dec, TTUSB_DEC2000T);
1636 ttusb_dec_set_model(dec, TTUSB_DEC2540T);
1642 if (ttusb_dec_init_usb(dec))
1644 if (ttusb_dec_init_stb(dec)) {
1645 ttusb_dec_exit_usb(dec);
1648 ttusb_dec_init_dvb(dec);
1650 dec->adapter.priv = dec;
1651 switch (le16_to_cpu(id->idProduct)) {
1653 dec->fe = ttusbdecfe_dvbs_attach(&fe_config);
1658 dec->fe = ttusbdecfe_dvbt_attach(&fe_config);
1662 if (dec->fe == NULL) {
1663 printk("dvb-ttusb-dec: A frontend driver was not found for device %04x/%04x\n",
1664 le16_to_cpu(dec->udev->descriptor.idVendor),
1665 le16_to_cpu(dec->udev->descriptor.idProduct));
1667 if (dvb_register_frontend(&dec->adapter, dec->fe)) {
1668 printk("budget-ci: Frontend registration failed!\n");
1669 if (dec->fe->ops.release)
1670 dec->fe->ops.release(dec->fe);
1675 ttusb_dec_init_v_pes(dec);
1676 ttusb_dec_init_filters(dec);
1677 ttusb_dec_init_tasklet(dec);
1681 ttusb_dec_set_interface(dec, TTUSB_DEC_INTERFACE_IN);
1689 static void ttusb_dec_disconnect(struct usb_interface *intf)
1691 struct ttusb_dec *dec = usb_get_intfdata(intf);
1693 usb_set_intfdata(intf, NULL);
1695 dprintk("%s\n", __func__);
1698 ttusb_dec_exit_tasklet(dec);
1699 ttusb_dec_exit_filters(dec);
1701 ttusb_dec_exit_rc(dec);
1702 ttusb_dec_exit_usb(dec);
1703 ttusb_dec_exit_dvb(dec);
1709 static void ttusb_dec_set_model(struct ttusb_dec *dec,
1710 enum ttusb_dec_model model)
1715 case TTUSB_DEC2000T:
1716 dec->model_name = "DEC2000-t";
1717 dec->firmware_name = "dvb-ttusb-dec-2000t.fw";
1720 case TTUSB_DEC2540T:
1721 dec->model_name = "DEC2540-t";
1722 dec->firmware_name = "dvb-ttusb-dec-2540t.fw";
1725 case TTUSB_DEC3000S:
1726 dec->model_name = "DEC3000-s";
1727 dec->firmware_name = "dvb-ttusb-dec-3000s.fw";
1732 static struct usb_device_id ttusb_dec_table[] = {
1733 {USB_DEVICE(0x0b48, 0x1006)}, /* DEC3000-s */
1734 /*{USB_DEVICE(0x0b48, 0x1007)}, Unconfirmed */
1735 {USB_DEVICE(0x0b48, 0x1008)}, /* DEC2000-t */
1736 {USB_DEVICE(0x0b48, 0x1009)}, /* DEC2540-t */
1740 static struct usb_driver ttusb_dec_driver = {
1741 .name = "ttusb-dec",
1742 .probe = ttusb_dec_probe,
1743 .disconnect = ttusb_dec_disconnect,
1744 .id_table = ttusb_dec_table,
1747 static int __init ttusb_dec_init(void)
1751 if ((result = usb_register(&ttusb_dec_driver)) < 0) {
1752 printk("%s: initialisation failed: error %d.\n", __func__,
1760 static void __exit ttusb_dec_exit(void)
1762 usb_deregister(&ttusb_dec_driver);
1765 module_init(ttusb_dec_init);
1766 module_exit(ttusb_dec_exit);
1768 MODULE_AUTHOR("Alex Woods <linux-dvb@giblets.org>");
1769 MODULE_DESCRIPTION(DRIVER_NAME);
1770 MODULE_LICENSE("GPL");
1771 MODULE_DEVICE_TABLE(usb, ttusb_dec_table);