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 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
57 #define dprintk if (debug) printk
59 #define DRIVER_NAME "TechnoTrend/Hauppauge DEC USB"
61 #define COMMAND_PIPE 0x03
62 #define RESULT_PIPE 0x04
67 #define COMMAND_PACKET_SIZE 0x3c
68 #define ARM_PACKET_SIZE 0x1000
69 #define IRQ_PACKET_SIZE 0x8
71 #define ISO_BUF_COUNT 0x04
72 #define FRAMES_PER_ISO_BUF 0x04
73 #define ISO_FRAME_SIZE 0x0380
75 #define MAX_PVA_LENGTH 6144
77 enum ttusb_dec_model {
83 enum ttusb_dec_packet_type {
85 TTUSB_DEC_PACKET_SECTION,
86 TTUSB_DEC_PACKET_EMPTY
89 enum ttusb_dec_interface {
90 TTUSB_DEC_INTERFACE_INITIAL,
91 TTUSB_DEC_INTERFACE_IN,
92 TTUSB_DEC_INTERFACE_OUT
96 enum ttusb_dec_model model;
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;
109 u16 pid[DMX_PES_OTHER];
112 struct usb_device *udev;
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;
124 dma_addr_t irq_dma_handle;
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;
131 u8 packet[MAX_PVA_LENGTH + 4];
132 enum ttusb_dec_packet_type packet_type;
135 int packet_payload_length;
138 int pva_stream_count;
139 int filter_stream_count;
141 struct dvb_filter_pes2ts a_pes2ts;
142 struct dvb_filter_pes2ts v_pes2ts;
144 u8 v_pes[16 + MAX_PVA_LENGTH];
148 struct list_head urb_frame_list;
149 struct tasklet_struct urb_tasklet;
150 spinlock_t urb_frame_list_lock;
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;
157 struct input_dev *rc_input_dev;
160 int active; /* Loaded successfully */
164 u8 data[ISO_FRAME_SIZE];
166 struct list_head urb_frame_list;
171 struct dvb_demux_filter *filter;
172 struct list_head filter_info_list;
175 static u16 rc_keys[] = {
204 static void ttusb_dec_set_model(struct ttusb_dec *dec,
205 enum ttusb_dec_model model);
207 static void ttusb_dec_handle_irq( struct urb *urb)
209 struct ttusb_dec * dec = urb->context;
210 char *buffer = dec->irq_buffer;
213 switch(urb->status) {
220 /* this urb is dead, cleanup */
221 dprintk("%s:urb shutting down with status: %d\n",
222 __func__, urb->status);
225 dprintk("%s:nonzero status received: %d\n",
226 __func__,urb->status);
230 if( (buffer[0] == 0x1) && (buffer[2] == 0x15) ) {
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 recieved 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);
247 exit: retval = usb_submit_urb(urb, GFP_ATOMIC);
249 printk("%s - usb_commit_urb failed with result: %d\n",
253 static u16 crc16(u16 crc, const u8 *buf, size_t len)
261 crc ^= (tmp ^ (tmp << 1)) << 4;
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[])
270 int result, actual_len, i;
273 dprintk("%s\n", __func__);
275 b = kmalloc(COMMAND_PACKET_SIZE + 4, GFP_KERNEL);
279 if ((result = mutex_lock_interruptible(&dec->usb_mutex))) {
281 printk("%s: Failed to lock usb mutex.\n", __func__);
286 b[1] = ++dec->trans_count;
291 memcpy(&b[4], params, param_length);
294 printk("%s: command: ", __func__);
295 for (i = 0; i < param_length + 4; i++)
296 printk("0x%02X ", b[i]);
300 result = usb_bulk_msg(dec->udev, dec->command_pipe, b,
301 COMMAND_PACKET_SIZE + 4, &actual_len, 1000);
304 printk("%s: command bulk message failed: error %d\n",
306 mutex_unlock(&dec->usb_mutex);
311 result = usb_bulk_msg(dec->udev, dec->result_pipe, b,
312 COMMAND_PACKET_SIZE + 4, &actual_len, 1000);
315 printk("%s: result bulk message failed: error %d\n",
317 mutex_unlock(&dec->usb_mutex);
322 printk("%s: result: ", __func__);
323 for (i = 0; i < actual_len; i++)
324 printk("0x%02X ", b[i]);
329 *result_length = b[3];
330 if (cmd_result && b[3] > 0)
331 memcpy(cmd_result, &b[4], b[3]);
333 mutex_unlock(&dec->usb_mutex);
340 static int ttusb_dec_get_stb_state (struct ttusb_dec *dec, unsigned int *mode,
341 unsigned int *model, unsigned int *version)
343 u8 c[COMMAND_PACKET_SIZE];
348 dprintk("%s\n", __func__);
350 result = ttusb_dec_send_command(dec, 0x08, 0, NULL, &c_length, c);
354 if (c_length >= 0x0c) {
360 memcpy(&tmp, &c[4], 4);
363 if (version != NULL) {
364 memcpy(&tmp, &c[8], 4);
365 *version = ntohl(tmp);
373 static int ttusb_dec_audio_pes2ts_cb(void *priv, unsigned char *data)
375 struct ttusb_dec *dec = priv;
377 dec->audio_filter->feed->cb.ts(data, 188, NULL, 0,
378 &dec->audio_filter->feed->feed.ts,
384 static int ttusb_dec_video_pes2ts_cb(void *priv, unsigned char *data)
386 struct ttusb_dec *dec = priv;
388 dec->video_filter->feed->cb.ts(data, 188, NULL, 0,
389 &dec->video_filter->feed->feed.ts,
395 static void ttusb_dec_set_pids(struct ttusb_dec *dec)
397 u8 b[] = { 0x00, 0x00, 0x00, 0x00,
398 0x00, 0x00, 0xff, 0xff,
399 0xff, 0xff, 0xff, 0xff };
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]);
405 dprintk("%s\n", __func__);
407 memcpy(&b[0], &pcr, 2);
408 memcpy(&b[2], &audio, 2);
409 memcpy(&b[4], &video, 2);
411 ttusb_dec_send_command(dec, 0x50, sizeof(b), b, NULL, NULL);
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;
421 static void ttusb_dec_process_pva(struct ttusb_dec *dec, u8 *pva, int length)
424 printk("%s: packet too short - discarding\n", __func__);
428 if (length > 8 + MAX_PVA_LENGTH) {
429 printk("%s: packet too long - discarding\n", __func__);
435 case 0x01: { /* VideoStream */
436 int prebytes = pva[5] & 0x03;
437 int postbytes = (pva[5] & 0x0c) >> 2;
438 __be16 v_pes_payload_length;
441 dec->video_filter->feed->cb.ts(pva, length, NULL, 0,
442 &dec->video_filter->feed->feed.ts, DMX_OK);
446 if (dec->v_pes_postbytes > 0 &&
447 dec->v_pes_postbytes == prebytes) {
448 memcpy(&dec->v_pes[dec->v_pes_length],
451 dvb_filter_pes2ts(&dec->v_pes2ts, dec->v_pes,
452 dec->v_pes_length + prebytes, 1);
456 dec->v_pes[7] = 0x80;
457 dec->v_pes[8] = 0x05;
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);
469 memcpy(&dec->v_pes[14], &pva[12 + prebytes],
470 length - 12 - prebytes);
471 dec->v_pes_length = 14 + length - 12 - prebytes;
473 dec->v_pes[7] = 0x00;
474 dec->v_pes[8] = 0x00;
476 memcpy(&dec->v_pes[9], &pva[8], length - 8);
477 dec->v_pes_length = 9 + length - 8;
480 dec->v_pes_postbytes = postbytes;
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;
487 dec->v_pes[6] = 0x80;
489 v_pes_payload_length = htons(dec->v_pes_length - 6 +
491 memcpy(&dec->v_pes[4], &v_pes_payload_length, 2);
494 dvb_filter_pes2ts(&dec->v_pes2ts, dec->v_pes,
495 dec->v_pes_length, 1);
500 case 0x02: /* MainAudioStream */
502 dec->audio_filter->feed->cb.ts(pva, length, NULL, 0,
503 &dec->audio_filter->feed->feed.ts, DMX_OK);
507 dvb_filter_pes2ts(&dec->a_pes2ts, &pva[8], length - 8,
512 printk("%s: unknown PVA type: %02x.\n", __func__,
518 static void ttusb_dec_process_filter(struct ttusb_dec *dec, u8 *packet,
521 struct list_head *item;
522 struct filter_info *finfo;
523 struct dvb_demux_filter *filter = NULL;
528 spin_lock_irqsave(&dec->filter_info_list_lock, flags);
529 for (item = dec->filter_info_list.next; item != &dec->filter_info_list;
531 finfo = list_entry(item, struct filter_info, filter_info_list);
532 if (finfo->stream_id == sid) {
533 filter = finfo->filter;
537 spin_unlock_irqrestore(&dec->filter_info_list_lock, flags);
540 filter->feed->cb.sec(&packet[2], length - 2, NULL, 0,
541 &filter->filter, DMX_OK);
544 static void ttusb_dec_process_packet(struct ttusb_dec *dec)
550 if (dec->packet_length % 2) {
551 printk("%s: odd sized packet - discarding\n", __func__);
555 for (i = 0; i < dec->packet_length; i += 2)
556 csum ^= ((dec->packet[i] << 8) + dec->packet[i + 1]);
559 printk("%s: checksum failed - discarding\n", __func__);
563 packet_id = dec->packet[dec->packet_length - 4] << 8;
564 packet_id += dec->packet[dec->packet_length - 3];
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);
571 if (packet_id == 0xffff)
572 dec->next_packet_id = 0x8000;
574 dec->next_packet_id = packet_id + 1;
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);
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);
589 case TTUSB_DEC_PACKET_EMPTY:
594 static void swap_bytes(u8 *b, int length)
598 length -= length % 2;
599 for (; length; b += 2, length -= 2) {
606 static void ttusb_dec_process_urb_frame(struct ttusb_dec *dec, u8 *b,
609 swap_bytes(b, length);
612 switch (dec->packet_state) {
620 dec->packet_state = 0;
628 dec->packet_length = 0;
629 } else if (*b != 0xaa) {
630 dec->packet_state = 0;
638 dec->packet[dec->packet_length++] = *b++;
640 if (dec->packet_length == 2) {
641 if (dec->packet[0] == 'A' &&
642 dec->packet[1] == 'V') {
644 TTUSB_DEC_PACKET_PVA;
646 } else if (dec->packet[0] == 'S') {
648 TTUSB_DEC_PACKET_SECTION;
650 } else if (dec->packet[0] == 0x00) {
652 TTUSB_DEC_PACKET_EMPTY;
653 dec->packet_payload_length = 2;
654 dec->packet_state = 7;
656 printk("%s: unknown packet type: "
657 "%02x%02x\n", __func__,
658 dec->packet[0], dec->packet[1]);
659 dec->packet_state = 0;
667 dec->packet[dec->packet_length++] = *b++;
669 if (dec->packet_type == TTUSB_DEC_PACKET_PVA &&
670 dec->packet_length == 8) {
672 dec->packet_payload_length = 8 +
673 (dec->packet[6] << 8) +
675 } else if (dec->packet_type ==
676 TTUSB_DEC_PACKET_SECTION &&
677 dec->packet_length == 5) {
679 dec->packet_payload_length = 5 +
680 ((dec->packet[3] & 0x0f) << 8) +
688 int remainder = dec->packet_payload_length -
691 if (length >= remainder) {
692 memcpy(dec->packet + dec->packet_length,
694 dec->packet_length += remainder;
699 memcpy(&dec->packet[dec->packet_length],
701 dec->packet_length += length;
711 dec->packet[dec->packet_length++] = *b++;
713 if (dec->packet_type == TTUSB_DEC_PACKET_SECTION &&
714 dec->packet_payload_length % 2)
717 if (dec->packet_length ==
718 dec->packet_payload_length + tail) {
719 ttusb_dec_process_packet(dec);
720 dec->packet_state = 0;
728 printk("%s: illegal packet state encountered.\n",
730 dec->packet_state = 0;
735 static void ttusb_dec_process_urb_frame_list(unsigned long data)
737 struct ttusb_dec *dec = (struct ttusb_dec *)data;
738 struct list_head *item;
739 struct urb_frame *frame;
743 spin_lock_irqsave(&dec->urb_frame_list_lock, flags);
744 if ((item = dec->urb_frame_list.next) != &dec->urb_frame_list) {
745 frame = list_entry(item, struct urb_frame,
747 list_del(&frame->urb_frame_list);
749 spin_unlock_irqrestore(&dec->urb_frame_list_lock,
753 spin_unlock_irqrestore(&dec->urb_frame_list_lock, flags);
755 ttusb_dec_process_urb_frame(dec, frame->data, frame->length);
760 static void ttusb_dec_process_urb(struct urb *urb)
762 struct ttusb_dec *dec = urb->context;
767 for (i = 0; i < FRAMES_PER_ISO_BUF; i++) {
768 struct usb_iso_packet_descriptor *d;
771 struct urb_frame *frame;
773 d = &urb->iso_frame_desc[i];
774 b = urb->transfer_buffer + d->offset;
775 length = d->actual_length;
777 if ((frame = kmalloc(sizeof(struct urb_frame),
781 memcpy(frame->data, b, length);
782 frame->length = length;
784 spin_lock_irqsave(&dec->urb_frame_list_lock,
786 list_add_tail(&frame->urb_frame_list,
787 &dec->urb_frame_list);
788 spin_unlock_irqrestore(&dec->urb_frame_list_lock,
791 tasklet_schedule(&dec->urb_tasklet);
795 /* -ENOENT is expected when unlinking urbs */
796 if (urb->status != -ENOENT)
797 dprintk("%s: urb error: %d\n", __func__,
801 if (dec->iso_stream_count)
802 usb_submit_urb(urb, GFP_ATOMIC);
805 static void ttusb_dec_setup_urbs(struct ttusb_dec *dec)
807 int i, j, buffer_offset = 0;
809 dprintk("%s\n", __func__);
811 for (i = 0; i < ISO_BUF_COUNT; i++) {
812 int frame_offset = 0;
813 struct urb *urb = dec->iso_urb[i];
815 urb->dev = dec->udev;
817 urb->complete = ttusb_dec_process_urb;
818 urb->pipe = dec->in_pipe;
819 urb->transfer_flags = URB_ISO_ASAP;
821 urb->number_of_packets = FRAMES_PER_ISO_BUF;
822 urb->transfer_buffer_length = ISO_FRAME_SIZE *
824 urb->transfer_buffer = dec->iso_buffer + buffer_offset;
825 buffer_offset += ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF;
827 for (j = 0; j < FRAMES_PER_ISO_BUF; j++) {
828 urb->iso_frame_desc[j].offset = frame_offset;
829 urb->iso_frame_desc[j].length = ISO_FRAME_SIZE;
830 frame_offset += ISO_FRAME_SIZE;
835 static void ttusb_dec_stop_iso_xfer(struct ttusb_dec *dec)
839 dprintk("%s\n", __func__);
841 if (mutex_lock_interruptible(&dec->iso_mutex))
844 dec->iso_stream_count--;
846 if (!dec->iso_stream_count) {
847 for (i = 0; i < ISO_BUF_COUNT; i++)
848 usb_kill_urb(dec->iso_urb[i]);
851 mutex_unlock(&dec->iso_mutex);
854 /* Setting the interface of the DEC tends to take down the USB communications
855 * for a short period, so it's important not to call this function just before
856 * trying to talk to it.
858 static int ttusb_dec_set_interface(struct ttusb_dec *dec,
859 enum ttusb_dec_interface interface)
864 if (interface != dec->interface) {
866 case TTUSB_DEC_INTERFACE_INITIAL:
867 result = usb_set_interface(dec->udev, 0, 0);
869 case TTUSB_DEC_INTERFACE_IN:
870 result = ttusb_dec_send_command(dec, 0x80, sizeof(b),
874 result = usb_set_interface(dec->udev, 0, 8);
876 case TTUSB_DEC_INTERFACE_OUT:
877 result = usb_set_interface(dec->udev, 0, 1);
884 dec->interface = interface;
890 static int ttusb_dec_start_iso_xfer(struct ttusb_dec *dec)
894 dprintk("%s\n", __func__);
896 if (mutex_lock_interruptible(&dec->iso_mutex))
899 if (!dec->iso_stream_count) {
900 ttusb_dec_setup_urbs(dec);
902 dec->packet_state = 0;
903 dec->v_pes_postbytes = 0;
904 dec->next_packet_id = 0;
906 for (i = 0; i < ISO_BUF_COUNT; i++) {
907 if ((result = usb_submit_urb(dec->iso_urb[i],
909 printk("%s: failed urb submission %d: "
910 "error %d\n", __func__, i, result);
913 usb_kill_urb(dec->iso_urb[i - 1]);
917 mutex_unlock(&dec->iso_mutex);
923 dec->iso_stream_count++;
925 mutex_unlock(&dec->iso_mutex);
930 static int ttusb_dec_start_ts_feed(struct dvb_demux_feed *dvbdmxfeed)
932 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
933 struct ttusb_dec *dec = dvbdmx->priv;
937 dprintk("%s\n", __func__);
939 dprintk(" ts_type:");
941 if (dvbdmxfeed->ts_type & TS_DECODER)
942 dprintk(" TS_DECODER");
944 if (dvbdmxfeed->ts_type & TS_PACKET)
945 dprintk(" TS_PACKET");
947 if (dvbdmxfeed->ts_type & TS_PAYLOAD_ONLY)
948 dprintk(" TS_PAYLOAD_ONLY");
952 switch (dvbdmxfeed->pes_type) {
954 case DMX_TS_PES_VIDEO:
955 dprintk(" pes_type: DMX_TS_PES_VIDEO\n");
956 dec->pid[DMX_PES_PCR] = dvbdmxfeed->pid;
957 dec->pid[DMX_PES_VIDEO] = dvbdmxfeed->pid;
958 dec->video_filter = dvbdmxfeed->filter;
959 ttusb_dec_set_pids(dec);
962 case DMX_TS_PES_AUDIO:
963 dprintk(" pes_type: DMX_TS_PES_AUDIO\n");
964 dec->pid[DMX_PES_AUDIO] = dvbdmxfeed->pid;
965 dec->audio_filter = dvbdmxfeed->filter;
966 ttusb_dec_set_pids(dec);
969 case DMX_TS_PES_TELETEXT:
970 dec->pid[DMX_PES_TELETEXT] = dvbdmxfeed->pid;
971 dprintk(" pes_type: DMX_TS_PES_TELETEXT(not supported)\n");
975 dprintk(" pes_type: DMX_TS_PES_PCR\n");
976 dec->pid[DMX_PES_PCR] = dvbdmxfeed->pid;
977 ttusb_dec_set_pids(dec);
980 case DMX_TS_PES_OTHER:
981 dprintk(" pes_type: DMX_TS_PES_OTHER(not supported)\n");
985 dprintk(" pes_type: unknown (%d)\n", dvbdmxfeed->pes_type);
990 result = ttusb_dec_send_command(dec, 0x80, sizeof(b0), b0, NULL, NULL);
994 dec->pva_stream_count++;
995 return ttusb_dec_start_iso_xfer(dec);
998 static int ttusb_dec_start_sec_feed(struct dvb_demux_feed *dvbdmxfeed)
1000 struct ttusb_dec *dec = dvbdmxfeed->demux->priv;
1001 u8 b0[] = { 0x00, 0x00, 0x00, 0x01,
1002 0x00, 0x00, 0x00, 0x00,
1003 0x00, 0x00, 0x00, 0x00,
1004 0x00, 0x00, 0x00, 0x00,
1005 0x00, 0xff, 0x00, 0x00,
1006 0x00, 0x00, 0x00, 0x00,
1007 0x00, 0x00, 0x00, 0x00,
1010 u8 c[COMMAND_PACKET_SIZE];
1013 struct filter_info *finfo;
1014 unsigned long flags;
1017 dprintk("%s\n", __func__);
1019 pid = htons(dvbdmxfeed->pid);
1020 memcpy(&b0[0], &pid, 2);
1021 memcpy(&b0[4], &x, 1);
1022 memcpy(&b0[5], &dvbdmxfeed->filter->filter.filter_value[0], 1);
1024 result = ttusb_dec_send_command(dec, 0x60, sizeof(b0), b0,
1028 if (c_length == 2) {
1029 if (!(finfo = kmalloc(sizeof(struct filter_info),
1033 finfo->stream_id = c[1];
1034 finfo->filter = dvbdmxfeed->filter;
1036 spin_lock_irqsave(&dec->filter_info_list_lock, flags);
1037 list_add_tail(&finfo->filter_info_list,
1038 &dec->filter_info_list);
1039 spin_unlock_irqrestore(&dec->filter_info_list_lock,
1042 dvbdmxfeed->priv = finfo;
1044 dec->filter_stream_count++;
1045 return ttusb_dec_start_iso_xfer(dec);
1053 static int ttusb_dec_start_feed(struct dvb_demux_feed *dvbdmxfeed)
1055 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
1057 dprintk("%s\n", __func__);
1059 if (!dvbdmx->dmx.frontend)
1062 dprintk(" pid: 0x%04X\n", dvbdmxfeed->pid);
1064 switch (dvbdmxfeed->type) {
1067 return ttusb_dec_start_ts_feed(dvbdmxfeed);
1071 return ttusb_dec_start_sec_feed(dvbdmxfeed);
1075 dprintk(" type: unknown (%d)\n", dvbdmxfeed->type);
1081 static int ttusb_dec_stop_ts_feed(struct dvb_demux_feed *dvbdmxfeed)
1083 struct ttusb_dec *dec = dvbdmxfeed->demux->priv;
1086 ttusb_dec_send_command(dec, 0x81, sizeof(b0), b0, NULL, NULL);
1088 dec->pva_stream_count--;
1090 ttusb_dec_stop_iso_xfer(dec);
1095 static int ttusb_dec_stop_sec_feed(struct dvb_demux_feed *dvbdmxfeed)
1097 struct ttusb_dec *dec = dvbdmxfeed->demux->priv;
1098 u8 b0[] = { 0x00, 0x00 };
1099 struct filter_info *finfo = (struct filter_info *)dvbdmxfeed->priv;
1100 unsigned long flags;
1102 b0[1] = finfo->stream_id;
1103 spin_lock_irqsave(&dec->filter_info_list_lock, flags);
1104 list_del(&finfo->filter_info_list);
1105 spin_unlock_irqrestore(&dec->filter_info_list_lock, flags);
1107 ttusb_dec_send_command(dec, 0x62, sizeof(b0), b0, NULL, NULL);
1109 dec->filter_stream_count--;
1111 ttusb_dec_stop_iso_xfer(dec);
1116 static int ttusb_dec_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
1118 dprintk("%s\n", __func__);
1120 switch (dvbdmxfeed->type) {
1122 return ttusb_dec_stop_ts_feed(dvbdmxfeed);
1126 return ttusb_dec_stop_sec_feed(dvbdmxfeed);
1133 static void ttusb_dec_free_iso_urbs(struct ttusb_dec *dec)
1137 dprintk("%s\n", __func__);
1139 for (i = 0; i < ISO_BUF_COUNT; i++)
1140 usb_free_urb(dec->iso_urb[i]);
1142 pci_free_consistent(NULL,
1143 ISO_FRAME_SIZE * (FRAMES_PER_ISO_BUF *
1145 dec->iso_buffer, dec->iso_dma_handle);
1148 static int ttusb_dec_alloc_iso_urbs(struct ttusb_dec *dec)
1152 dprintk("%s\n", __func__);
1154 dec->iso_buffer = pci_alloc_consistent(NULL,
1156 (FRAMES_PER_ISO_BUF *
1158 &dec->iso_dma_handle);
1160 memset(dec->iso_buffer, 0,
1161 ISO_FRAME_SIZE * (FRAMES_PER_ISO_BUF * ISO_BUF_COUNT));
1163 for (i = 0; i < ISO_BUF_COUNT; i++) {
1166 if (!(urb = usb_alloc_urb(FRAMES_PER_ISO_BUF, GFP_ATOMIC))) {
1167 ttusb_dec_free_iso_urbs(dec);
1171 dec->iso_urb[i] = urb;
1174 ttusb_dec_setup_urbs(dec);
1179 static void ttusb_dec_init_tasklet(struct ttusb_dec *dec)
1181 spin_lock_init(&dec->urb_frame_list_lock);
1182 INIT_LIST_HEAD(&dec->urb_frame_list);
1183 tasklet_init(&dec->urb_tasklet, ttusb_dec_process_urb_frame_list,
1184 (unsigned long)dec);
1187 static int ttusb_init_rc( struct ttusb_dec *dec)
1189 struct input_dev *input_dev;
1190 u8 b[] = { 0x00, 0x01 };
1194 usb_make_path(dec->udev, dec->rc_phys, sizeof(dec->rc_phys));
1195 strlcpy(dec->rc_phys, "/input0", sizeof(dec->rc_phys));
1197 input_dev = input_allocate_device();
1201 input_dev->name = "ttusb_dec remote control";
1202 input_dev->phys = dec->rc_phys;
1203 input_dev->evbit[0] = BIT_MASK(EV_KEY);
1204 input_dev->keycodesize = sizeof(u16);
1205 input_dev->keycodemax = 0x1a;
1206 input_dev->keycode = rc_keys;
1208 for (i = 0; i < ARRAY_SIZE(rc_keys); i++)
1209 set_bit(rc_keys[i], input_dev->keybit);
1211 err = input_register_device(input_dev);
1213 input_free_device(input_dev);
1217 dec->rc_input_dev = input_dev;
1218 if (usb_submit_urb(dec->irq_urb, GFP_KERNEL))
1219 printk("%s: usb_submit_urb failed\n",__func__);
1220 /* enable irq pipe */
1221 ttusb_dec_send_command(dec,0xb0,sizeof(b),b,NULL,NULL);
1226 static void ttusb_dec_init_v_pes(struct ttusb_dec *dec)
1228 dprintk("%s\n", __func__);
1230 dec->v_pes[0] = 0x00;
1231 dec->v_pes[1] = 0x00;
1232 dec->v_pes[2] = 0x01;
1233 dec->v_pes[3] = 0xe0;
1236 static int ttusb_dec_init_usb(struct ttusb_dec *dec)
1238 dprintk("%s\n", __func__);
1240 mutex_init(&dec->usb_mutex);
1241 mutex_init(&dec->iso_mutex);
1243 dec->command_pipe = usb_sndbulkpipe(dec->udev, COMMAND_PIPE);
1244 dec->result_pipe = usb_rcvbulkpipe(dec->udev, RESULT_PIPE);
1245 dec->in_pipe = usb_rcvisocpipe(dec->udev, IN_PIPE);
1246 dec->out_pipe = usb_sndisocpipe(dec->udev, OUT_PIPE);
1247 dec->irq_pipe = usb_rcvintpipe(dec->udev, IRQ_PIPE);
1250 dec->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
1254 dec->irq_buffer = usb_buffer_alloc(dec->udev,IRQ_PACKET_SIZE,
1255 GFP_ATOMIC, &dec->irq_dma_handle);
1256 if(!dec->irq_buffer) {
1259 usb_fill_int_urb(dec->irq_urb, dec->udev,dec->irq_pipe,
1260 dec->irq_buffer, IRQ_PACKET_SIZE,
1261 ttusb_dec_handle_irq, dec, 1);
1262 dec->irq_urb->transfer_dma = dec->irq_dma_handle;
1263 dec->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1266 return ttusb_dec_alloc_iso_urbs(dec);
1269 static int ttusb_dec_boot_dsp(struct ttusb_dec *dec)
1271 int i, j, actual_len, result, size, trans_count;
1272 u8 b0[] = { 0x00, 0x00, 0x00, 0x00,
1273 0x00, 0x00, 0x00, 0x00,
1278 const u8 *firmware = NULL;
1279 size_t firmware_size = 0;
1280 u16 firmware_csum = 0;
1281 __be16 firmware_csum_ns;
1282 __be32 firmware_size_nl;
1283 u32 crc32_csum, crc32_check;
1285 const struct firmware *fw_entry = NULL;
1287 dprintk("%s\n", __func__);
1289 if (request_firmware(&fw_entry, dec->firmware_name, &dec->udev->dev)) {
1290 printk(KERN_ERR "%s: Firmware (%s) unavailable.\n",
1291 __func__, dec->firmware_name);
1295 firmware = fw_entry->data;
1296 firmware_size = fw_entry->size;
1298 if (firmware_size < 60) {
1299 printk("%s: firmware size too small for DSP code (%zu < 60).\n",
1300 __func__, firmware_size);
1301 release_firmware(fw_entry);
1305 /* a 32 bit checksum over the first 56 bytes of the DSP Code is stored
1306 at offset 56 of file, so use it to check if the firmware file is
1308 crc32_csum = crc32(~0L, firmware, 56) ^ ~0L;
1309 memcpy(&tmp, &firmware[56], 4);
1310 crc32_check = ntohl(tmp);
1311 if (crc32_csum != crc32_check) {
1312 printk("%s: crc32 check of DSP code failed (calculated "
1313 "0x%08x != 0x%08x in file), file invalid.\n",
1314 __func__, crc32_csum, crc32_check);
1315 release_firmware(fw_entry);
1318 memcpy(idstring, &firmware[36], 20);
1319 idstring[20] = '\0';
1320 printk(KERN_INFO "ttusb_dec: found DSP code \"%s\".\n", idstring);
1322 firmware_size_nl = htonl(firmware_size);
1323 memcpy(b0, &firmware_size_nl, 4);
1324 firmware_csum = crc16(~0, firmware, firmware_size) ^ ~0;
1325 firmware_csum_ns = htons(firmware_csum);
1326 memcpy(&b0[6], &firmware_csum_ns, 2);
1328 result = ttusb_dec_send_command(dec, 0x41, sizeof(b0), b0, NULL, NULL);
1331 release_firmware(fw_entry);
1338 b = kmalloc(ARM_PACKET_SIZE, GFP_KERNEL);
1340 release_firmware(fw_entry);
1344 for (i = 0; i < firmware_size; i += COMMAND_PACKET_SIZE) {
1345 size = firmware_size - i;
1346 if (size > COMMAND_PACKET_SIZE)
1347 size = COMMAND_PACKET_SIZE;
1350 b[j + 1] = trans_count++;
1353 memcpy(&b[j + 4], &firmware[i], size);
1355 j += COMMAND_PACKET_SIZE + 4;
1357 if (j >= ARM_PACKET_SIZE) {
1358 result = usb_bulk_msg(dec->udev, dec->command_pipe, b,
1359 ARM_PACKET_SIZE, &actual_len,
1362 } else if (size < COMMAND_PACKET_SIZE) {
1363 result = usb_bulk_msg(dec->udev, dec->command_pipe, b,
1364 j - COMMAND_PACKET_SIZE + size,
1369 result = ttusb_dec_send_command(dec, 0x43, sizeof(b1), b1, NULL, NULL);
1371 release_firmware(fw_entry);
1377 static int ttusb_dec_init_stb(struct ttusb_dec *dec)
1380 unsigned int mode, model, version;
1382 dprintk("%s\n", __func__);
1384 result = ttusb_dec_get_stb_state(dec, &mode, &model, &version);
1388 if (version == 0xABCDEFAB)
1389 printk(KERN_INFO "ttusb_dec: no version "
1390 "info in Firmware\n");
1392 printk(KERN_INFO "ttusb_dec: Firmware "
1394 version >> 24, (version >> 16) & 0xff,
1395 (version >> 8) & 0xff, version & 0xff);
1397 result = ttusb_dec_boot_dsp(dec);
1403 /* We can't trust the USB IDs that some firmwares
1409 ttusb_dec_set_model(dec, TTUSB_DEC3000S);
1413 ttusb_dec_set_model(dec, TTUSB_DEC2000T);
1416 ttusb_dec_set_model(dec, TTUSB_DEC2540T);
1419 printk(KERN_ERR "%s: unknown model returned "
1420 "by firmware (%08x) - please report\n",
1426 if (version >= 0x01770000)
1427 dec->can_playback = 1;
1436 static int ttusb_dec_init_dvb(struct ttusb_dec *dec)
1440 dprintk("%s\n", __func__);
1442 if ((result = dvb_register_adapter(&dec->adapter,
1443 dec->model_name, THIS_MODULE,
1446 printk("%s: dvb_register_adapter failed: error %d\n",
1452 dec->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING;
1454 dec->demux.priv = (void *)dec;
1455 dec->demux.filternum = 31;
1456 dec->demux.feednum = 31;
1457 dec->demux.start_feed = ttusb_dec_start_feed;
1458 dec->demux.stop_feed = ttusb_dec_stop_feed;
1459 dec->demux.write_to_decoder = NULL;
1461 if ((result = dvb_dmx_init(&dec->demux)) < 0) {
1462 printk("%s: dvb_dmx_init failed: error %d\n", __func__,
1465 dvb_unregister_adapter(&dec->adapter);
1470 dec->dmxdev.filternum = 32;
1471 dec->dmxdev.demux = &dec->demux.dmx;
1472 dec->dmxdev.capabilities = 0;
1474 if ((result = dvb_dmxdev_init(&dec->dmxdev, &dec->adapter)) < 0) {
1475 printk("%s: dvb_dmxdev_init failed: error %d\n",
1478 dvb_dmx_release(&dec->demux);
1479 dvb_unregister_adapter(&dec->adapter);
1484 dec->frontend.source = DMX_FRONTEND_0;
1486 if ((result = dec->demux.dmx.add_frontend(&dec->demux.dmx,
1487 &dec->frontend)) < 0) {
1488 printk("%s: dvb_dmx_init failed: error %d\n", __func__,
1491 dvb_dmxdev_release(&dec->dmxdev);
1492 dvb_dmx_release(&dec->demux);
1493 dvb_unregister_adapter(&dec->adapter);
1498 if ((result = dec->demux.dmx.connect_frontend(&dec->demux.dmx,
1499 &dec->frontend)) < 0) {
1500 printk("%s: dvb_dmx_init failed: error %d\n", __func__,
1503 dec->demux.dmx.remove_frontend(&dec->demux.dmx, &dec->frontend);
1504 dvb_dmxdev_release(&dec->dmxdev);
1505 dvb_dmx_release(&dec->demux);
1506 dvb_unregister_adapter(&dec->adapter);
1511 dvb_net_init(&dec->adapter, &dec->dvb_net, &dec->demux.dmx);
1516 static void ttusb_dec_exit_dvb(struct ttusb_dec *dec)
1518 dprintk("%s\n", __func__);
1520 dvb_net_release(&dec->dvb_net);
1521 dec->demux.dmx.close(&dec->demux.dmx);
1522 dec->demux.dmx.remove_frontend(&dec->demux.dmx, &dec->frontend);
1523 dvb_dmxdev_release(&dec->dmxdev);
1524 dvb_dmx_release(&dec->demux);
1526 dvb_unregister_frontend(dec->fe);
1527 if (dec->fe->ops.release)
1528 dec->fe->ops.release(dec->fe);
1530 dvb_unregister_adapter(&dec->adapter);
1533 static void ttusb_dec_exit_rc(struct ttusb_dec *dec)
1536 dprintk("%s\n", __func__);
1537 /* we have to check whether the irq URB is already submitted.
1538 * As the irq is submitted after the interface is changed,
1539 * this is the best method i figured out.
1541 if (dec->interface == TTUSB_DEC_INTERFACE_IN)
1542 usb_kill_urb(dec->irq_urb);
1544 usb_free_urb(dec->irq_urb);
1546 usb_buffer_free(dec->udev,IRQ_PACKET_SIZE,
1547 dec->irq_buffer, dec->irq_dma_handle);
1549 if (dec->rc_input_dev) {
1550 input_unregister_device(dec->rc_input_dev);
1551 dec->rc_input_dev = NULL;
1556 static void ttusb_dec_exit_usb(struct ttusb_dec *dec)
1560 dprintk("%s\n", __func__);
1562 dec->iso_stream_count = 0;
1564 for (i = 0; i < ISO_BUF_COUNT; i++)
1565 usb_kill_urb(dec->iso_urb[i]);
1567 ttusb_dec_free_iso_urbs(dec);
1570 static void ttusb_dec_exit_tasklet(struct ttusb_dec *dec)
1572 struct list_head *item;
1573 struct urb_frame *frame;
1575 tasklet_kill(&dec->urb_tasklet);
1577 while ((item = dec->urb_frame_list.next) != &dec->urb_frame_list) {
1578 frame = list_entry(item, struct urb_frame, urb_frame_list);
1579 list_del(&frame->urb_frame_list);
1584 static void ttusb_dec_init_filters(struct ttusb_dec *dec)
1586 INIT_LIST_HEAD(&dec->filter_info_list);
1587 spin_lock_init(&dec->filter_info_list_lock);
1590 static void ttusb_dec_exit_filters(struct ttusb_dec *dec)
1592 struct list_head *item;
1593 struct filter_info *finfo;
1595 while ((item = dec->filter_info_list.next) != &dec->filter_info_list) {
1596 finfo = list_entry(item, struct filter_info, filter_info_list);
1597 list_del(&finfo->filter_info_list);
1602 static int fe_send_command(struct dvb_frontend* fe, const u8 command,
1603 int param_length, const u8 params[],
1604 int *result_length, u8 cmd_result[])
1606 struct ttusb_dec* dec = fe->dvb->priv;
1607 return ttusb_dec_send_command(dec, command, param_length, params, result_length, cmd_result);
1610 static struct ttusbdecfe_config fe_config = {
1611 .send_command = fe_send_command
1614 static int ttusb_dec_probe(struct usb_interface *intf,
1615 const struct usb_device_id *id)
1617 struct usb_device *udev;
1618 struct ttusb_dec *dec;
1620 dprintk("%s\n", __func__);
1622 udev = interface_to_usbdev(intf);
1624 if (!(dec = kzalloc(sizeof(struct ttusb_dec), GFP_KERNEL))) {
1625 printk("%s: couldn't allocate memory.\n", __func__);
1629 usb_set_intfdata(intf, (void *)dec);
1631 switch (id->idProduct) {
1633 ttusb_dec_set_model(dec, TTUSB_DEC3000S);
1637 ttusb_dec_set_model(dec, TTUSB_DEC2000T);
1641 ttusb_dec_set_model(dec, TTUSB_DEC2540T);
1647 if (ttusb_dec_init_usb(dec))
1649 if (ttusb_dec_init_stb(dec)) {
1650 ttusb_dec_exit_usb(dec);
1653 ttusb_dec_init_dvb(dec);
1655 dec->adapter.priv = dec;
1656 switch (id->idProduct) {
1658 dec->fe = ttusbdecfe_dvbs_attach(&fe_config);
1663 dec->fe = ttusbdecfe_dvbt_attach(&fe_config);
1667 if (dec->fe == NULL) {
1668 printk("dvb-ttusb-dec: A frontend driver was not found for device [%04x:%04x]\n",
1669 le16_to_cpu(dec->udev->descriptor.idVendor),
1670 le16_to_cpu(dec->udev->descriptor.idProduct));
1672 if (dvb_register_frontend(&dec->adapter, dec->fe)) {
1673 printk("budget-ci: Frontend registration failed!\n");
1674 if (dec->fe->ops.release)
1675 dec->fe->ops.release(dec->fe);
1680 ttusb_dec_init_v_pes(dec);
1681 ttusb_dec_init_filters(dec);
1682 ttusb_dec_init_tasklet(dec);
1686 ttusb_dec_set_interface(dec, TTUSB_DEC_INTERFACE_IN);
1694 static void ttusb_dec_disconnect(struct usb_interface *intf)
1696 struct ttusb_dec *dec = usb_get_intfdata(intf);
1698 usb_set_intfdata(intf, NULL);
1700 dprintk("%s\n", __func__);
1703 ttusb_dec_exit_tasklet(dec);
1704 ttusb_dec_exit_filters(dec);
1706 ttusb_dec_exit_rc(dec);
1707 ttusb_dec_exit_usb(dec);
1708 ttusb_dec_exit_dvb(dec);
1714 static void ttusb_dec_set_model(struct ttusb_dec *dec,
1715 enum ttusb_dec_model model)
1720 case TTUSB_DEC2000T:
1721 dec->model_name = "DEC2000-t";
1722 dec->firmware_name = "dvb-ttusb-dec-2000t.fw";
1725 case TTUSB_DEC2540T:
1726 dec->model_name = "DEC2540-t";
1727 dec->firmware_name = "dvb-ttusb-dec-2540t.fw";
1730 case TTUSB_DEC3000S:
1731 dec->model_name = "DEC3000-s";
1732 dec->firmware_name = "dvb-ttusb-dec-3000s.fw";
1737 static struct usb_device_id ttusb_dec_table[] = {
1738 {USB_DEVICE(0x0b48, 0x1006)}, /* DEC3000-s */
1739 /*{USB_DEVICE(0x0b48, 0x1007)}, Unconfirmed */
1740 {USB_DEVICE(0x0b48, 0x1008)}, /* DEC2000-t */
1741 {USB_DEVICE(0x0b48, 0x1009)}, /* DEC2540-t */
1745 static struct usb_driver ttusb_dec_driver = {
1746 .name = "ttusb-dec",
1747 .probe = ttusb_dec_probe,
1748 .disconnect = ttusb_dec_disconnect,
1749 .id_table = ttusb_dec_table,
1752 static int __init ttusb_dec_init(void)
1756 if ((result = usb_register(&ttusb_dec_driver)) < 0) {
1757 printk("%s: initialisation failed: error %d.\n", __func__,
1765 static void __exit ttusb_dec_exit(void)
1767 usb_deregister(&ttusb_dec_driver);
1770 module_init(ttusb_dec_init);
1771 module_exit(ttusb_dec_exit);
1773 MODULE_AUTHOR("Alex Woods <linux-dvb@giblets.org>");
1774 MODULE_DESCRIPTION(DRIVER_NAME);
1775 MODULE_LICENSE("GPL");
1776 MODULE_DEVICE_TABLE(usb, ttusb_dec_table);