1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright (C) 2003-2004 Alex Woods <linux-dvb@giblets.org>
6 * IR support by Peter Beutner <p.beutner@gmx.net>
9 #include <linux/list.h>
10 #include <linux/module.h>
11 #include <linux/pci.h>
12 #include <linux/slab.h>
13 #include <linux/spinlock.h>
14 #include <linux/usb.h>
15 #include <linux/interrupt.h>
16 #include <linux/firmware.h>
17 #include <linux/crc32.h>
18 #include <linux/init.h>
19 #include <linux/input.h>
21 #include <linux/mutex.h>
23 #include <media/dmxdev.h>
24 #include <media/dvb_demux.h>
25 #include <media/dvb_frontend.h>
26 #include <media/dvb_net.h>
27 #include "ttusbdecfe.h"
30 static int output_pva;
33 module_param(debug, int, 0644);
34 MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
35 module_param(output_pva, int, 0444);
36 MODULE_PARM_DESC(output_pva, "Output PVA from dvr device (default:off)");
37 module_param(enable_rc, int, 0644);
38 MODULE_PARM_DESC(enable_rc, "Turn on/off IR remote control(default: off)");
40 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
42 #define dprintk if (debug) printk
44 #define DRIVER_NAME "TechnoTrend/Hauppauge DEC USB"
46 #define COMMAND_PIPE 0x03
47 #define RESULT_PIPE 0x04
52 #define COMMAND_PACKET_SIZE 0x3c
53 #define ARM_PACKET_SIZE 0x1000
54 #define IRQ_PACKET_SIZE 0x8
56 #define ISO_BUF_COUNT 0x04
57 #define FRAMES_PER_ISO_BUF 0x04
58 #define ISO_FRAME_SIZE 0x0380
60 #define MAX_PVA_LENGTH 6144
62 enum ttusb_dec_model {
68 enum ttusb_dec_packet_type {
70 TTUSB_DEC_PACKET_SECTION,
71 TTUSB_DEC_PACKET_EMPTY
74 enum ttusb_dec_interface {
75 TTUSB_DEC_INTERFACE_INITIAL,
76 TTUSB_DEC_INTERFACE_IN,
77 TTUSB_DEC_INTERFACE_OUT
80 typedef int (dvb_filter_pes2ts_cb_t) (void *, unsigned char *);
82 struct dvb_filter_pes2ts {
83 unsigned char buf[188];
85 dvb_filter_pes2ts_cb_t *cb;
90 enum ttusb_dec_model model;
96 struct dvb_adapter adapter;
98 struct dvb_demux demux;
99 struct dmx_frontend frontend;
100 struct dvb_net dvb_net;
101 struct dvb_frontend* fe;
103 u16 pid[DMX_PES_OTHER];
106 struct usb_device *udev;
108 unsigned int command_pipe;
109 unsigned int result_pipe;
110 unsigned int in_pipe;
111 unsigned int out_pipe;
112 unsigned int irq_pipe;
113 enum ttusb_dec_interface interface;
114 struct mutex usb_mutex;
118 dma_addr_t irq_dma_handle;
120 struct urb *iso_urb[ISO_BUF_COUNT];
121 int iso_stream_count;
122 struct mutex iso_mutex;
124 u8 packet[MAX_PVA_LENGTH + 4];
125 enum ttusb_dec_packet_type packet_type;
128 int packet_payload_length;
131 int pva_stream_count;
132 int filter_stream_count;
134 struct dvb_filter_pes2ts a_pes2ts;
135 struct dvb_filter_pes2ts v_pes2ts;
137 u8 v_pes[16 + MAX_PVA_LENGTH];
141 struct list_head urb_frame_list;
142 struct tasklet_struct urb_tasklet;
143 spinlock_t urb_frame_list_lock;
145 struct dvb_demux_filter *audio_filter;
146 struct dvb_demux_filter *video_filter;
147 struct list_head filter_info_list;
148 spinlock_t filter_info_list_lock;
150 struct input_dev *rc_input_dev;
153 int active; /* Loaded successfully */
157 u8 data[ISO_FRAME_SIZE];
159 struct list_head urb_frame_list;
164 struct dvb_demux_filter *filter;
165 struct list_head filter_info_list;
168 static u16 rc_keys[] = {
197 static void dvb_filter_pes2ts_init(struct dvb_filter_pes2ts *p2ts,
199 dvb_filter_pes2ts_cb_t *cb, void *priv)
201 unsigned char *buf=p2ts->buf;
211 static int dvb_filter_pes2ts(struct dvb_filter_pes2ts *p2ts,
212 unsigned char *pes, int len, int payload_start)
214 unsigned char *buf=p2ts->buf;
217 //len=6+((pes[4]<<8)|pes[5]);
224 buf[3]=0x10|((p2ts->cc++)&0x0f);
225 memcpy(buf+4, pes, 184);
226 if ((ret=p2ts->cb(p2ts->priv, buf)))
233 buf[3]=0x30|((p2ts->cc++)&0x0f);
238 memset(buf+6, 0xff, rest-1);
241 memcpy(buf+5+rest, pes, len);
242 return p2ts->cb(p2ts->priv, buf);
245 static void ttusb_dec_set_model(struct ttusb_dec *dec,
246 enum ttusb_dec_model model);
248 static void ttusb_dec_handle_irq( struct urb *urb)
250 struct ttusb_dec *dec = urb->context;
251 char *buffer = dec->irq_buffer;
253 int index = buffer[4];
255 switch(urb->status) {
262 /* this urb is dead, cleanup */
263 dprintk("%s:urb shutting down with status: %d\n",
264 __func__, urb->status);
267 dprintk("%s:nonzero status received: %d\n",
268 __func__,urb->status);
272 if ((buffer[0] == 0x1) && (buffer[2] == 0x15)) {
276 * this is an fact a bit too simple implementation;
277 * the box also reports a keyrepeat signal
278 * (with buffer[3] == 0x40) in an interval of ~100ms.
279 * But to handle this correctly we had to imlemenent some
280 * kind of timer which signals a 'key up' event if no
281 * keyrepeat signal is received for lets say 200ms.
282 * this should/could be added later ...
283 * for now lets report each signal as a key down and up
285 if (index - 1 < ARRAY_SIZE(rc_keys)) {
286 dprintk("%s:rc signal:%d\n", __func__, index);
287 input_report_key(dec->rc_input_dev, rc_keys[index - 1], 1);
288 input_sync(dec->rc_input_dev);
289 input_report_key(dec->rc_input_dev, rc_keys[index - 1], 0);
290 input_sync(dec->rc_input_dev);
295 retval = usb_submit_urb(urb, GFP_ATOMIC);
297 printk("%s - usb_commit_urb failed with result: %d\n",
301 static u16 crc16(u16 crc, const u8 *buf, size_t len)
309 crc ^= (tmp ^ (tmp << 1)) << 4;
314 static int ttusb_dec_send_command(struct ttusb_dec *dec, const u8 command,
315 int param_length, const u8 params[],
316 int *result_length, u8 cmd_result[])
318 int result, actual_len;
321 dprintk("%s\n", __func__);
323 b = kzalloc(COMMAND_PACKET_SIZE + 4, GFP_KERNEL);
327 if ((result = mutex_lock_interruptible(&dec->usb_mutex))) {
329 printk("%s: Failed to lock usb mutex.\n", __func__);
334 b[1] = ++dec->trans_count;
339 memcpy(&b[4], params, param_length);
342 printk(KERN_DEBUG "%s: command: %*ph\n",
343 __func__, param_length, b);
346 result = usb_bulk_msg(dec->udev, dec->command_pipe, b,
347 COMMAND_PACKET_SIZE + 4, &actual_len, 1000);
350 printk("%s: command bulk message failed: error %d\n",
352 mutex_unlock(&dec->usb_mutex);
357 result = usb_bulk_msg(dec->udev, dec->result_pipe, b,
358 COMMAND_PACKET_SIZE + 4, &actual_len, 1000);
361 printk("%s: result bulk message failed: error %d\n",
363 mutex_unlock(&dec->usb_mutex);
368 printk(KERN_DEBUG "%s: result: %*ph\n",
369 __func__, actual_len, b);
373 *result_length = b[3];
374 if (cmd_result && b[3] > 0)
375 memcpy(cmd_result, &b[4], b[3]);
377 mutex_unlock(&dec->usb_mutex);
384 static int ttusb_dec_get_stb_state (struct ttusb_dec *dec, unsigned int *mode,
385 unsigned int *model, unsigned int *version)
387 u8 c[COMMAND_PACKET_SIZE];
392 dprintk("%s\n", __func__);
394 result = ttusb_dec_send_command(dec, 0x08, 0, NULL, &c_length, c);
398 if (c_length >= 0x0c) {
404 memcpy(&tmp, &c[4], 4);
407 if (version != NULL) {
408 memcpy(&tmp, &c[8], 4);
409 *version = ntohl(tmp);
417 static int ttusb_dec_audio_pes2ts_cb(void *priv, unsigned char *data)
419 struct ttusb_dec *dec = priv;
421 dec->audio_filter->feed->cb.ts(data, 188, NULL, 0,
422 &dec->audio_filter->feed->feed.ts, NULL);
427 static int ttusb_dec_video_pes2ts_cb(void *priv, unsigned char *data)
429 struct ttusb_dec *dec = priv;
431 dec->video_filter->feed->cb.ts(data, 188, NULL, 0,
432 &dec->video_filter->feed->feed.ts, NULL);
437 static void ttusb_dec_set_pids(struct ttusb_dec *dec)
439 u8 b[] = { 0x00, 0x00, 0x00, 0x00,
440 0x00, 0x00, 0xff, 0xff,
441 0xff, 0xff, 0xff, 0xff };
443 __be16 pcr = htons(dec->pid[DMX_PES_PCR]);
444 __be16 audio = htons(dec->pid[DMX_PES_AUDIO]);
445 __be16 video = htons(dec->pid[DMX_PES_VIDEO]);
447 dprintk("%s\n", __func__);
449 memcpy(&b[0], &pcr, 2);
450 memcpy(&b[2], &audio, 2);
451 memcpy(&b[4], &video, 2);
453 ttusb_dec_send_command(dec, 0x50, sizeof(b), b, NULL, NULL);
455 dvb_filter_pes2ts_init(&dec->a_pes2ts, dec->pid[DMX_PES_AUDIO],
456 ttusb_dec_audio_pes2ts_cb, dec);
457 dvb_filter_pes2ts_init(&dec->v_pes2ts, dec->pid[DMX_PES_VIDEO],
458 ttusb_dec_video_pes2ts_cb, dec);
459 dec->v_pes_length = 0;
460 dec->v_pes_postbytes = 0;
463 static void ttusb_dec_process_pva(struct ttusb_dec *dec, u8 *pva, int length)
466 printk("%s: packet too short - discarding\n", __func__);
470 if (length > 8 + MAX_PVA_LENGTH) {
471 printk("%s: packet too long - discarding\n", __func__);
477 case 0x01: { /* VideoStream */
478 int prebytes = pva[5] & 0x03;
479 int postbytes = (pva[5] & 0x0c) >> 2;
480 __be16 v_pes_payload_length;
483 dec->video_filter->feed->cb.ts(pva, length, NULL, 0,
484 &dec->video_filter->feed->feed.ts, NULL);
488 if (dec->v_pes_postbytes > 0 &&
489 dec->v_pes_postbytes == prebytes) {
490 memcpy(&dec->v_pes[dec->v_pes_length],
493 dvb_filter_pes2ts(&dec->v_pes2ts, dec->v_pes,
494 dec->v_pes_length + prebytes, 1);
498 dec->v_pes[7] = 0x80;
499 dec->v_pes[8] = 0x05;
501 dec->v_pes[9] = 0x21 | ((pva[8] & 0xc0) >> 5);
502 dec->v_pes[10] = ((pva[8] & 0x3f) << 2) |
503 ((pva[9] & 0xc0) >> 6);
504 dec->v_pes[11] = 0x01 |
505 ((pva[9] & 0x3f) << 2) |
506 ((pva[10] & 0x80) >> 6);
507 dec->v_pes[12] = ((pva[10] & 0x7f) << 1) |
508 ((pva[11] & 0xc0) >> 7);
509 dec->v_pes[13] = 0x01 | ((pva[11] & 0x7f) << 1);
511 memcpy(&dec->v_pes[14], &pva[12 + prebytes],
512 length - 12 - prebytes);
513 dec->v_pes_length = 14 + length - 12 - prebytes;
515 dec->v_pes[7] = 0x00;
516 dec->v_pes[8] = 0x00;
518 memcpy(&dec->v_pes[9], &pva[8], length - 8);
519 dec->v_pes_length = 9 + length - 8;
522 dec->v_pes_postbytes = postbytes;
524 if (dec->v_pes[9 + dec->v_pes[8]] == 0x00 &&
525 dec->v_pes[10 + dec->v_pes[8]] == 0x00 &&
526 dec->v_pes[11 + dec->v_pes[8]] == 0x01)
527 dec->v_pes[6] = 0x84;
529 dec->v_pes[6] = 0x80;
531 v_pes_payload_length = htons(dec->v_pes_length - 6 +
533 memcpy(&dec->v_pes[4], &v_pes_payload_length, 2);
536 dvb_filter_pes2ts(&dec->v_pes2ts, dec->v_pes,
537 dec->v_pes_length, 1);
542 case 0x02: /* MainAudioStream */
544 dec->audio_filter->feed->cb.ts(pva, length, NULL, 0,
545 &dec->audio_filter->feed->feed.ts, NULL);
549 dvb_filter_pes2ts(&dec->a_pes2ts, &pva[8], length - 8,
554 printk("%s: unknown PVA type: %02x.\n", __func__,
560 static void ttusb_dec_process_filter(struct ttusb_dec *dec, u8 *packet,
563 struct list_head *item;
564 struct filter_info *finfo;
565 struct dvb_demux_filter *filter = NULL;
570 spin_lock_irqsave(&dec->filter_info_list_lock, flags);
571 for (item = dec->filter_info_list.next; item != &dec->filter_info_list;
573 finfo = list_entry(item, struct filter_info, filter_info_list);
574 if (finfo->stream_id == sid) {
575 filter = finfo->filter;
579 spin_unlock_irqrestore(&dec->filter_info_list_lock, flags);
582 filter->feed->cb.sec(&packet[2], length - 2, NULL, 0,
583 &filter->filter, NULL);
586 static void ttusb_dec_process_packet(struct ttusb_dec *dec)
592 if (dec->packet_length % 2) {
593 printk("%s: odd sized packet - discarding\n", __func__);
597 for (i = 0; i < dec->packet_length; i += 2)
598 csum ^= ((dec->packet[i] << 8) + dec->packet[i + 1]);
601 printk("%s: checksum failed - discarding\n", __func__);
605 packet_id = dec->packet[dec->packet_length - 4] << 8;
606 packet_id += dec->packet[dec->packet_length - 3];
608 if ((packet_id != dec->next_packet_id) && dec->next_packet_id) {
609 printk("%s: warning: lost packets between %u and %u\n",
610 __func__, dec->next_packet_id - 1, packet_id);
613 if (packet_id == 0xffff)
614 dec->next_packet_id = 0x8000;
616 dec->next_packet_id = packet_id + 1;
618 switch (dec->packet_type) {
619 case TTUSB_DEC_PACKET_PVA:
620 if (dec->pva_stream_count)
621 ttusb_dec_process_pva(dec, dec->packet,
622 dec->packet_payload_length);
625 case TTUSB_DEC_PACKET_SECTION:
626 if (dec->filter_stream_count)
627 ttusb_dec_process_filter(dec, dec->packet,
628 dec->packet_payload_length);
631 case TTUSB_DEC_PACKET_EMPTY:
636 static void swap_bytes(u8 *b, int length)
638 length -= length % 2;
639 for (; length; b += 2, length -= 2)
643 static void ttusb_dec_process_urb_frame(struct ttusb_dec *dec, u8 *b,
646 swap_bytes(b, length);
649 switch (dec->packet_state) {
657 dec->packet_state = 0;
665 dec->packet_length = 0;
666 } else if (*b != 0xaa) {
667 dec->packet_state = 0;
675 dec->packet[dec->packet_length++] = *b++;
677 if (dec->packet_length == 2) {
678 if (dec->packet[0] == 'A' &&
679 dec->packet[1] == 'V') {
681 TTUSB_DEC_PACKET_PVA;
683 } else if (dec->packet[0] == 'S') {
685 TTUSB_DEC_PACKET_SECTION;
687 } else if (dec->packet[0] == 0x00) {
689 TTUSB_DEC_PACKET_EMPTY;
690 dec->packet_payload_length = 2;
691 dec->packet_state = 7;
693 printk("%s: unknown packet type: %02x%02x\n",
695 dec->packet[0], dec->packet[1]);
696 dec->packet_state = 0;
704 dec->packet[dec->packet_length++] = *b++;
706 if (dec->packet_type == TTUSB_DEC_PACKET_PVA &&
707 dec->packet_length == 8) {
709 dec->packet_payload_length = 8 +
710 (dec->packet[6] << 8) +
712 } else if (dec->packet_type ==
713 TTUSB_DEC_PACKET_SECTION &&
714 dec->packet_length == 5) {
716 dec->packet_payload_length = 5 +
717 ((dec->packet[3] & 0x0f) << 8) +
725 int remainder = dec->packet_payload_length -
728 if (length >= remainder) {
729 memcpy(dec->packet + dec->packet_length,
731 dec->packet_length += remainder;
736 memcpy(&dec->packet[dec->packet_length],
738 dec->packet_length += length;
748 dec->packet[dec->packet_length++] = *b++;
750 if (dec->packet_type == TTUSB_DEC_PACKET_SECTION &&
751 dec->packet_payload_length % 2)
754 if (dec->packet_length ==
755 dec->packet_payload_length + tail) {
756 ttusb_dec_process_packet(dec);
757 dec->packet_state = 0;
765 printk("%s: illegal packet state encountered.\n",
767 dec->packet_state = 0;
772 static void ttusb_dec_process_urb_frame_list(struct tasklet_struct *t)
774 struct ttusb_dec *dec = from_tasklet(dec, t, urb_tasklet);
775 struct list_head *item;
776 struct urb_frame *frame;
780 spin_lock_irqsave(&dec->urb_frame_list_lock, flags);
781 if ((item = dec->urb_frame_list.next) != &dec->urb_frame_list) {
782 frame = list_entry(item, struct urb_frame,
784 list_del(&frame->urb_frame_list);
786 spin_unlock_irqrestore(&dec->urb_frame_list_lock,
790 spin_unlock_irqrestore(&dec->urb_frame_list_lock, flags);
792 ttusb_dec_process_urb_frame(dec, frame->data, frame->length);
797 static void ttusb_dec_process_urb(struct urb *urb)
799 struct ttusb_dec *dec = urb->context;
804 for (i = 0; i < FRAMES_PER_ISO_BUF; i++) {
805 struct usb_iso_packet_descriptor *d;
808 struct urb_frame *frame;
810 d = &urb->iso_frame_desc[i];
811 b = urb->transfer_buffer + d->offset;
812 length = d->actual_length;
814 if ((frame = kmalloc(sizeof(struct urb_frame),
818 memcpy(frame->data, b, length);
819 frame->length = length;
821 spin_lock_irqsave(&dec->urb_frame_list_lock,
823 list_add_tail(&frame->urb_frame_list,
824 &dec->urb_frame_list);
825 spin_unlock_irqrestore(&dec->urb_frame_list_lock,
828 tasklet_schedule(&dec->urb_tasklet);
832 /* -ENOENT is expected when unlinking urbs */
833 if (urb->status != -ENOENT)
834 dprintk("%s: urb error: %d\n", __func__,
838 if (dec->iso_stream_count)
839 usb_submit_urb(urb, GFP_ATOMIC);
842 static void ttusb_dec_setup_urbs(struct ttusb_dec *dec)
844 int i, j, buffer_offset = 0;
846 dprintk("%s\n", __func__);
848 for (i = 0; i < ISO_BUF_COUNT; i++) {
849 int frame_offset = 0;
850 struct urb *urb = dec->iso_urb[i];
852 urb->dev = dec->udev;
854 urb->complete = ttusb_dec_process_urb;
855 urb->pipe = dec->in_pipe;
856 urb->transfer_flags = URB_ISO_ASAP;
858 urb->number_of_packets = FRAMES_PER_ISO_BUF;
859 urb->transfer_buffer_length = ISO_FRAME_SIZE *
861 urb->transfer_buffer = dec->iso_buffer + buffer_offset;
862 buffer_offset += ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF;
864 for (j = 0; j < FRAMES_PER_ISO_BUF; j++) {
865 urb->iso_frame_desc[j].offset = frame_offset;
866 urb->iso_frame_desc[j].length = ISO_FRAME_SIZE;
867 frame_offset += ISO_FRAME_SIZE;
872 static void ttusb_dec_stop_iso_xfer(struct ttusb_dec *dec)
876 dprintk("%s\n", __func__);
878 if (mutex_lock_interruptible(&dec->iso_mutex))
881 dec->iso_stream_count--;
883 if (!dec->iso_stream_count) {
884 for (i = 0; i < ISO_BUF_COUNT; i++)
885 usb_kill_urb(dec->iso_urb[i]);
888 mutex_unlock(&dec->iso_mutex);
891 /* Setting the interface of the DEC tends to take down the USB communications
892 * for a short period, so it's important not to call this function just before
893 * trying to talk to it.
895 static int ttusb_dec_set_interface(struct ttusb_dec *dec,
896 enum ttusb_dec_interface interface)
901 if (interface != dec->interface) {
903 case TTUSB_DEC_INTERFACE_INITIAL:
904 result = usb_set_interface(dec->udev, 0, 0);
906 case TTUSB_DEC_INTERFACE_IN:
907 result = ttusb_dec_send_command(dec, 0x80, sizeof(b),
911 result = usb_set_interface(dec->udev, 0, 8);
913 case TTUSB_DEC_INTERFACE_OUT:
914 result = usb_set_interface(dec->udev, 0, 1);
921 dec->interface = interface;
927 static int ttusb_dec_start_iso_xfer(struct ttusb_dec *dec)
931 dprintk("%s\n", __func__);
933 if (mutex_lock_interruptible(&dec->iso_mutex))
936 if (!dec->iso_stream_count) {
937 ttusb_dec_setup_urbs(dec);
939 dec->packet_state = 0;
940 dec->v_pes_postbytes = 0;
941 dec->next_packet_id = 0;
943 for (i = 0; i < ISO_BUF_COUNT; i++) {
944 if ((result = usb_submit_urb(dec->iso_urb[i],
946 printk("%s: failed urb submission %d: error %d\n",
947 __func__, i, result);
950 usb_kill_urb(dec->iso_urb[i - 1]);
954 mutex_unlock(&dec->iso_mutex);
960 dec->iso_stream_count++;
962 mutex_unlock(&dec->iso_mutex);
967 static int ttusb_dec_start_ts_feed(struct dvb_demux_feed *dvbdmxfeed)
969 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
970 struct ttusb_dec *dec = dvbdmx->priv;
974 dprintk("%s\n", __func__);
976 dprintk(" ts_type:");
978 if (dvbdmxfeed->ts_type & TS_DECODER)
979 dprintk(" TS_DECODER");
981 if (dvbdmxfeed->ts_type & TS_PACKET)
982 dprintk(" TS_PACKET");
984 if (dvbdmxfeed->ts_type & TS_PAYLOAD_ONLY)
985 dprintk(" TS_PAYLOAD_ONLY");
989 switch (dvbdmxfeed->pes_type) {
992 dprintk(" pes_type: DMX_PES_VIDEO\n");
993 dec->pid[DMX_PES_PCR] = dvbdmxfeed->pid;
994 dec->pid[DMX_PES_VIDEO] = dvbdmxfeed->pid;
995 dec->video_filter = dvbdmxfeed->filter;
996 ttusb_dec_set_pids(dec);
1000 dprintk(" pes_type: DMX_PES_AUDIO\n");
1001 dec->pid[DMX_PES_AUDIO] = dvbdmxfeed->pid;
1002 dec->audio_filter = dvbdmxfeed->filter;
1003 ttusb_dec_set_pids(dec);
1006 case DMX_PES_TELETEXT:
1007 dec->pid[DMX_PES_TELETEXT] = dvbdmxfeed->pid;
1008 dprintk(" pes_type: DMX_PES_TELETEXT(not supported)\n");
1012 dprintk(" pes_type: DMX_PES_PCR\n");
1013 dec->pid[DMX_PES_PCR] = dvbdmxfeed->pid;
1014 ttusb_dec_set_pids(dec);
1018 dprintk(" pes_type: DMX_PES_OTHER(not supported)\n");
1022 dprintk(" pes_type: unknown (%d)\n", dvbdmxfeed->pes_type);
1027 result = ttusb_dec_send_command(dec, 0x80, sizeof(b0), b0, NULL, NULL);
1031 dec->pva_stream_count++;
1032 return ttusb_dec_start_iso_xfer(dec);
1035 static int ttusb_dec_start_sec_feed(struct dvb_demux_feed *dvbdmxfeed)
1037 struct ttusb_dec *dec = dvbdmxfeed->demux->priv;
1038 u8 b0[] = { 0x00, 0x00, 0x00, 0x01,
1039 0x00, 0x00, 0x00, 0x00,
1040 0x00, 0x00, 0x00, 0x00,
1041 0x00, 0x00, 0x00, 0x00,
1042 0x00, 0xff, 0x00, 0x00,
1043 0x00, 0x00, 0x00, 0x00,
1044 0x00, 0x00, 0x00, 0x00,
1047 u8 c[COMMAND_PACKET_SIZE];
1050 struct filter_info *finfo;
1051 unsigned long flags;
1054 dprintk("%s\n", __func__);
1056 pid = htons(dvbdmxfeed->pid);
1057 memcpy(&b0[0], &pid, 2);
1058 memcpy(&b0[4], &x, 1);
1059 memcpy(&b0[5], &dvbdmxfeed->filter->filter.filter_value[0], 1);
1061 result = ttusb_dec_send_command(dec, 0x60, sizeof(b0), b0,
1065 if (c_length == 2) {
1066 if (!(finfo = kmalloc(sizeof(struct filter_info),
1070 finfo->stream_id = c[1];
1071 finfo->filter = dvbdmxfeed->filter;
1073 spin_lock_irqsave(&dec->filter_info_list_lock, flags);
1074 list_add_tail(&finfo->filter_info_list,
1075 &dec->filter_info_list);
1076 spin_unlock_irqrestore(&dec->filter_info_list_lock,
1079 dvbdmxfeed->priv = finfo;
1081 dec->filter_stream_count++;
1082 return ttusb_dec_start_iso_xfer(dec);
1090 static int ttusb_dec_start_feed(struct dvb_demux_feed *dvbdmxfeed)
1092 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
1094 dprintk("%s\n", __func__);
1096 if (!dvbdmx->dmx.frontend)
1099 dprintk(" pid: 0x%04X\n", dvbdmxfeed->pid);
1101 switch (dvbdmxfeed->type) {
1104 return ttusb_dec_start_ts_feed(dvbdmxfeed);
1108 return ttusb_dec_start_sec_feed(dvbdmxfeed);
1112 dprintk(" type: unknown (%d)\n", dvbdmxfeed->type);
1118 static int ttusb_dec_stop_ts_feed(struct dvb_demux_feed *dvbdmxfeed)
1120 struct ttusb_dec *dec = dvbdmxfeed->demux->priv;
1123 ttusb_dec_send_command(dec, 0x81, sizeof(b0), b0, NULL, NULL);
1125 dec->pva_stream_count--;
1127 ttusb_dec_stop_iso_xfer(dec);
1132 static int ttusb_dec_stop_sec_feed(struct dvb_demux_feed *dvbdmxfeed)
1134 struct ttusb_dec *dec = dvbdmxfeed->demux->priv;
1135 u8 b0[] = { 0x00, 0x00 };
1136 struct filter_info *finfo = (struct filter_info *)dvbdmxfeed->priv;
1137 unsigned long flags;
1139 b0[1] = finfo->stream_id;
1140 spin_lock_irqsave(&dec->filter_info_list_lock, flags);
1141 list_del(&finfo->filter_info_list);
1142 spin_unlock_irqrestore(&dec->filter_info_list_lock, flags);
1144 ttusb_dec_send_command(dec, 0x62, sizeof(b0), b0, NULL, NULL);
1146 dec->filter_stream_count--;
1148 ttusb_dec_stop_iso_xfer(dec);
1153 static int ttusb_dec_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
1155 dprintk("%s\n", __func__);
1157 switch (dvbdmxfeed->type) {
1159 return ttusb_dec_stop_ts_feed(dvbdmxfeed);
1163 return ttusb_dec_stop_sec_feed(dvbdmxfeed);
1170 static void ttusb_dec_free_iso_urbs(struct ttusb_dec *dec)
1174 dprintk("%s\n", __func__);
1176 for (i = 0; i < ISO_BUF_COUNT; i++)
1177 usb_free_urb(dec->iso_urb[i]);
1178 kfree(dec->iso_buffer);
1181 static int ttusb_dec_alloc_iso_urbs(struct ttusb_dec *dec)
1185 dprintk("%s\n", __func__);
1187 dec->iso_buffer = kcalloc(FRAMES_PER_ISO_BUF * ISO_BUF_COUNT,
1188 ISO_FRAME_SIZE, GFP_KERNEL);
1189 if (!dec->iso_buffer)
1192 for (i = 0; i < ISO_BUF_COUNT; i++) {
1195 if (!(urb = usb_alloc_urb(FRAMES_PER_ISO_BUF, GFP_ATOMIC))) {
1196 ttusb_dec_free_iso_urbs(dec);
1200 dec->iso_urb[i] = urb;
1203 ttusb_dec_setup_urbs(dec);
1208 static void ttusb_dec_init_tasklet(struct ttusb_dec *dec)
1210 spin_lock_init(&dec->urb_frame_list_lock);
1211 INIT_LIST_HEAD(&dec->urb_frame_list);
1212 tasklet_setup(&dec->urb_tasklet, ttusb_dec_process_urb_frame_list);
1215 static int ttusb_init_rc( struct ttusb_dec *dec)
1217 struct input_dev *input_dev;
1218 u8 b[] = { 0x00, 0x01 };
1222 usb_make_path(dec->udev, dec->rc_phys, sizeof(dec->rc_phys));
1223 strlcat(dec->rc_phys, "/input0", sizeof(dec->rc_phys));
1225 input_dev = input_allocate_device();
1229 input_dev->name = "ttusb_dec remote control";
1230 input_dev->phys = dec->rc_phys;
1231 input_dev->evbit[0] = BIT_MASK(EV_KEY);
1232 input_dev->keycodesize = sizeof(u16);
1233 input_dev->keycodemax = 0x1a;
1234 input_dev->keycode = rc_keys;
1236 for (i = 0; i < ARRAY_SIZE(rc_keys); i++)
1237 set_bit(rc_keys[i], input_dev->keybit);
1239 err = input_register_device(input_dev);
1241 input_free_device(input_dev);
1245 dec->rc_input_dev = input_dev;
1246 if (usb_submit_urb(dec->irq_urb, GFP_KERNEL))
1247 printk("%s: usb_submit_urb failed\n",__func__);
1248 /* enable irq pipe */
1249 ttusb_dec_send_command(dec,0xb0,sizeof(b),b,NULL,NULL);
1254 static void ttusb_dec_init_v_pes(struct ttusb_dec *dec)
1256 dprintk("%s\n", __func__);
1258 dec->v_pes[0] = 0x00;
1259 dec->v_pes[1] = 0x00;
1260 dec->v_pes[2] = 0x01;
1261 dec->v_pes[3] = 0xe0;
1264 static int ttusb_dec_init_usb(struct ttusb_dec *dec)
1268 dprintk("%s\n", __func__);
1270 mutex_init(&dec->usb_mutex);
1271 mutex_init(&dec->iso_mutex);
1273 dec->command_pipe = usb_sndbulkpipe(dec->udev, COMMAND_PIPE);
1274 dec->result_pipe = usb_rcvbulkpipe(dec->udev, RESULT_PIPE);
1275 dec->in_pipe = usb_rcvisocpipe(dec->udev, IN_PIPE);
1276 dec->out_pipe = usb_sndisocpipe(dec->udev, OUT_PIPE);
1277 dec->irq_pipe = usb_rcvintpipe(dec->udev, IRQ_PIPE);
1280 dec->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
1284 dec->irq_buffer = usb_alloc_coherent(dec->udev,IRQ_PACKET_SIZE,
1285 GFP_KERNEL, &dec->irq_dma_handle);
1286 if(!dec->irq_buffer) {
1287 usb_free_urb(dec->irq_urb);
1290 usb_fill_int_urb(dec->irq_urb, dec->udev,dec->irq_pipe,
1291 dec->irq_buffer, IRQ_PACKET_SIZE,
1292 ttusb_dec_handle_irq, dec, 1);
1293 dec->irq_urb->transfer_dma = dec->irq_dma_handle;
1294 dec->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1297 result = ttusb_dec_alloc_iso_urbs(dec);
1299 usb_free_urb(dec->irq_urb);
1300 usb_free_coherent(dec->udev, IRQ_PACKET_SIZE,
1301 dec->irq_buffer, dec->irq_dma_handle);
1306 static int ttusb_dec_boot_dsp(struct ttusb_dec *dec)
1308 int i, j, actual_len, result, size, trans_count;
1309 u8 b0[] = { 0x00, 0x00, 0x00, 0x00,
1310 0x00, 0x00, 0x00, 0x00,
1315 const u8 *firmware = NULL;
1316 size_t firmware_size = 0;
1317 u16 firmware_csum = 0;
1318 __be16 firmware_csum_ns;
1319 __be32 firmware_size_nl;
1320 u32 crc32_csum, crc32_check;
1322 const struct firmware *fw_entry = NULL;
1324 dprintk("%s\n", __func__);
1326 result = reject_firmware(&fw_entry, dec->firmware_name, &dec->udev->dev);
1328 printk(KERN_ERR "%s: Firmware (%s) unavailable.\n",
1329 __func__, dec->firmware_name);
1333 firmware = fw_entry->data;
1334 firmware_size = fw_entry->size;
1336 if (firmware_size < 60) {
1337 printk("%s: firmware size too small for DSP code (%zu < 60).\n",
1338 __func__, firmware_size);
1339 release_firmware(fw_entry);
1343 /* a 32 bit checksum over the first 56 bytes of the DSP Code is stored
1344 at offset 56 of file, so use it to check if the firmware file is
1346 crc32_csum = crc32(~0L, firmware, 56) ^ ~0L;
1347 memcpy(&tmp, &firmware[56], 4);
1348 crc32_check = ntohl(tmp);
1349 if (crc32_csum != crc32_check) {
1350 printk("%s: crc32 check of DSP code failed (calculated 0x%08x != 0x%08x in file), file invalid.\n",
1351 __func__, crc32_csum, crc32_check);
1352 release_firmware(fw_entry);
1355 memcpy(idstring, &firmware[36], 20);
1356 idstring[20] = '\0';
1357 printk(KERN_INFO "ttusb_dec: found DSP code \"%s\".\n", idstring);
1359 firmware_size_nl = htonl(firmware_size);
1360 memcpy(b0, &firmware_size_nl, 4);
1361 firmware_csum = crc16(~0, firmware, firmware_size) ^ ~0;
1362 firmware_csum_ns = htons(firmware_csum);
1363 memcpy(&b0[6], &firmware_csum_ns, 2);
1365 result = ttusb_dec_send_command(dec, 0x41, sizeof(b0), b0, NULL, NULL);
1368 release_firmware(fw_entry);
1375 b = kmalloc(ARM_PACKET_SIZE, GFP_KERNEL);
1377 release_firmware(fw_entry);
1381 for (i = 0; i < firmware_size; i += COMMAND_PACKET_SIZE) {
1382 size = firmware_size - i;
1383 if (size > COMMAND_PACKET_SIZE)
1384 size = COMMAND_PACKET_SIZE;
1387 b[j + 1] = trans_count++;
1390 memcpy(&b[j + 4], &firmware[i], size);
1392 j += COMMAND_PACKET_SIZE + 4;
1394 if (j >= ARM_PACKET_SIZE) {
1395 result = usb_bulk_msg(dec->udev, dec->command_pipe, b,
1396 ARM_PACKET_SIZE, &actual_len,
1399 } else if (size < COMMAND_PACKET_SIZE) {
1400 result = usb_bulk_msg(dec->udev, dec->command_pipe, b,
1401 j - COMMAND_PACKET_SIZE + size,
1406 result = ttusb_dec_send_command(dec, 0x43, sizeof(b1), b1, NULL, NULL);
1408 release_firmware(fw_entry);
1414 static int ttusb_dec_init_stb(struct ttusb_dec *dec)
1417 unsigned int mode = 0, model = 0, version = 0;
1419 dprintk("%s\n", __func__);
1421 result = ttusb_dec_get_stb_state(dec, &mode, &model, &version);
1426 if (version == 0xABCDEFAB)
1427 printk(KERN_INFO "ttusb_dec: no version info in Firmware\n");
1429 printk(KERN_INFO "ttusb_dec: Firmware %x.%02x%c%c\n",
1430 version >> 24, (version >> 16) & 0xff,
1431 (version >> 8) & 0xff, version & 0xff);
1433 result = ttusb_dec_boot_dsp(dec);
1437 /* We can't trust the USB IDs that some firmwares
1443 ttusb_dec_set_model(dec, TTUSB_DEC3000S);
1447 ttusb_dec_set_model(dec, TTUSB_DEC2000T);
1450 ttusb_dec_set_model(dec, TTUSB_DEC2540T);
1453 printk(KERN_ERR "%s: unknown model returned by firmware (%08x) - please report\n",
1457 if (version >= 0x01770000)
1458 dec->can_playback = 1;
1463 static int ttusb_dec_init_dvb(struct ttusb_dec *dec)
1467 dprintk("%s\n", __func__);
1469 if ((result = dvb_register_adapter(&dec->adapter,
1470 dec->model_name, THIS_MODULE,
1473 printk("%s: dvb_register_adapter failed: error %d\n",
1479 dec->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING;
1481 dec->demux.priv = (void *)dec;
1482 dec->demux.filternum = 31;
1483 dec->demux.feednum = 31;
1484 dec->demux.start_feed = ttusb_dec_start_feed;
1485 dec->demux.stop_feed = ttusb_dec_stop_feed;
1486 dec->demux.write_to_decoder = NULL;
1488 if ((result = dvb_dmx_init(&dec->demux)) < 0) {
1489 printk("%s: dvb_dmx_init failed: error %d\n", __func__,
1492 dvb_unregister_adapter(&dec->adapter);
1497 dec->dmxdev.filternum = 32;
1498 dec->dmxdev.demux = &dec->demux.dmx;
1499 dec->dmxdev.capabilities = 0;
1501 if ((result = dvb_dmxdev_init(&dec->dmxdev, &dec->adapter)) < 0) {
1502 printk("%s: dvb_dmxdev_init failed: error %d\n",
1505 dvb_dmx_release(&dec->demux);
1506 dvb_unregister_adapter(&dec->adapter);
1511 dec->frontend.source = DMX_FRONTEND_0;
1513 if ((result = dec->demux.dmx.add_frontend(&dec->demux.dmx,
1514 &dec->frontend)) < 0) {
1515 printk("%s: dvb_dmx_init failed: error %d\n", __func__,
1518 dvb_dmxdev_release(&dec->dmxdev);
1519 dvb_dmx_release(&dec->demux);
1520 dvb_unregister_adapter(&dec->adapter);
1525 if ((result = dec->demux.dmx.connect_frontend(&dec->demux.dmx,
1526 &dec->frontend)) < 0) {
1527 printk("%s: dvb_dmx_init failed: error %d\n", __func__,
1530 dec->demux.dmx.remove_frontend(&dec->demux.dmx, &dec->frontend);
1531 dvb_dmxdev_release(&dec->dmxdev);
1532 dvb_dmx_release(&dec->demux);
1533 dvb_unregister_adapter(&dec->adapter);
1538 dvb_net_init(&dec->adapter, &dec->dvb_net, &dec->demux.dmx);
1543 static void ttusb_dec_exit_dvb(struct ttusb_dec *dec)
1545 dprintk("%s\n", __func__);
1547 dvb_net_release(&dec->dvb_net);
1548 dec->demux.dmx.close(&dec->demux.dmx);
1549 dec->demux.dmx.remove_frontend(&dec->demux.dmx, &dec->frontend);
1550 dvb_dmxdev_release(&dec->dmxdev);
1551 dvb_dmx_release(&dec->demux);
1553 dvb_unregister_frontend(dec->fe);
1554 dvb_frontend_detach(dec->fe);
1556 dvb_unregister_adapter(&dec->adapter);
1559 static void ttusb_dec_exit_rc(struct ttusb_dec *dec)
1561 dprintk("%s\n", __func__);
1563 if (dec->rc_input_dev) {
1564 input_unregister_device(dec->rc_input_dev);
1565 dec->rc_input_dev = NULL;
1570 static void ttusb_dec_exit_usb(struct ttusb_dec *dec)
1574 dprintk("%s\n", __func__);
1577 /* we have to check whether the irq URB is already submitted.
1578 * As the irq is submitted after the interface is changed,
1579 * this is the best method i figured out.
1581 if (dec->interface == TTUSB_DEC_INTERFACE_IN)
1582 usb_kill_urb(dec->irq_urb);
1584 usb_free_urb(dec->irq_urb);
1586 usb_free_coherent(dec->udev, IRQ_PACKET_SIZE,
1587 dec->irq_buffer, dec->irq_dma_handle);
1590 dec->iso_stream_count = 0;
1592 for (i = 0; i < ISO_BUF_COUNT; i++)
1593 usb_kill_urb(dec->iso_urb[i]);
1595 ttusb_dec_free_iso_urbs(dec);
1598 static void ttusb_dec_exit_tasklet(struct ttusb_dec *dec)
1600 struct list_head *item;
1601 struct urb_frame *frame;
1603 tasklet_kill(&dec->urb_tasklet);
1605 while ((item = dec->urb_frame_list.next) != &dec->urb_frame_list) {
1606 frame = list_entry(item, struct urb_frame, urb_frame_list);
1607 list_del(&frame->urb_frame_list);
1612 static void ttusb_dec_init_filters(struct ttusb_dec *dec)
1614 INIT_LIST_HEAD(&dec->filter_info_list);
1615 spin_lock_init(&dec->filter_info_list_lock);
1618 static void ttusb_dec_exit_filters(struct ttusb_dec *dec)
1620 struct list_head *item;
1621 struct filter_info *finfo;
1623 while ((item = dec->filter_info_list.next) != &dec->filter_info_list) {
1624 finfo = list_entry(item, struct filter_info, filter_info_list);
1625 list_del(&finfo->filter_info_list);
1630 static int fe_send_command(struct dvb_frontend* fe, const u8 command,
1631 int param_length, const u8 params[],
1632 int *result_length, u8 cmd_result[])
1634 struct ttusb_dec* dec = fe->dvb->priv;
1635 return ttusb_dec_send_command(dec, command, param_length, params, result_length, cmd_result);
1638 static const struct ttusbdecfe_config fe_config = {
1639 .send_command = fe_send_command
1642 static int ttusb_dec_probe(struct usb_interface *intf,
1643 const struct usb_device_id *id)
1645 struct usb_device *udev;
1646 struct ttusb_dec *dec;
1649 dprintk("%s\n", __func__);
1651 udev = interface_to_usbdev(intf);
1653 if (!(dec = kzalloc(sizeof(struct ttusb_dec), GFP_KERNEL))) {
1654 printk("%s: couldn't allocate memory.\n", __func__);
1658 usb_set_intfdata(intf, (void *)dec);
1660 switch (id->idProduct) {
1662 ttusb_dec_set_model(dec, TTUSB_DEC3000S);
1666 ttusb_dec_set_model(dec, TTUSB_DEC2000T);
1670 ttusb_dec_set_model(dec, TTUSB_DEC2540T);
1676 result = ttusb_dec_init_usb(dec);
1679 result = ttusb_dec_init_stb(dec);
1682 result = ttusb_dec_init_dvb(dec);
1686 dec->adapter.priv = dec;
1687 switch (id->idProduct) {
1689 dec->fe = ttusbdecfe_dvbs_attach(&fe_config);
1694 dec->fe = ttusbdecfe_dvbt_attach(&fe_config);
1698 if (dec->fe == NULL) {
1699 printk("dvb-ttusb-dec: A frontend driver was not found for device [%04x:%04x]\n",
1700 le16_to_cpu(dec->udev->descriptor.idVendor),
1701 le16_to_cpu(dec->udev->descriptor.idProduct));
1703 if (dvb_register_frontend(&dec->adapter, dec->fe)) {
1704 printk("budget-ci: Frontend registration failed!\n");
1705 if (dec->fe->ops.release)
1706 dec->fe->ops.release(dec->fe);
1711 ttusb_dec_init_v_pes(dec);
1712 ttusb_dec_init_filters(dec);
1713 ttusb_dec_init_tasklet(dec);
1717 ttusb_dec_set_interface(dec, TTUSB_DEC_INTERFACE_IN);
1724 ttusb_dec_exit_usb(dec);
1730 static void ttusb_dec_disconnect(struct usb_interface *intf)
1732 struct ttusb_dec *dec = usb_get_intfdata(intf);
1734 usb_set_intfdata(intf, NULL);
1736 dprintk("%s\n", __func__);
1739 ttusb_dec_exit_tasklet(dec);
1740 ttusb_dec_exit_filters(dec);
1742 ttusb_dec_exit_rc(dec);
1743 ttusb_dec_exit_usb(dec);
1744 ttusb_dec_exit_dvb(dec);
1750 static void ttusb_dec_set_model(struct ttusb_dec *dec,
1751 enum ttusb_dec_model model)
1756 case TTUSB_DEC2000T:
1757 dec->model_name = "DEC2000-t";
1758 dec->firmware_name = "/*(DEBLOBBED)*/";
1761 case TTUSB_DEC2540T:
1762 dec->model_name = "DEC2540-t";
1763 dec->firmware_name = "/*(DEBLOBBED)*/";
1766 case TTUSB_DEC3000S:
1767 dec->model_name = "DEC3000-s";
1768 dec->firmware_name = "/*(DEBLOBBED)*/";
1773 static const struct usb_device_id ttusb_dec_table[] = {
1774 {USB_DEVICE(0x0b48, 0x1006)}, /* DEC3000-s */
1775 /*{USB_DEVICE(0x0b48, 0x1007)}, Unconfirmed */
1776 {USB_DEVICE(0x0b48, 0x1008)}, /* DEC2000-t */
1777 {USB_DEVICE(0x0b48, 0x1009)}, /* DEC2540-t */
1781 static struct usb_driver ttusb_dec_driver = {
1782 .name = "ttusb-dec",
1783 .probe = ttusb_dec_probe,
1784 .disconnect = ttusb_dec_disconnect,
1785 .id_table = ttusb_dec_table,
1788 module_usb_driver(ttusb_dec_driver);
1790 MODULE_AUTHOR("Alex Woods <linux-dvb@giblets.org>");
1791 MODULE_DESCRIPTION(DRIVER_NAME);
1792 MODULE_LICENSE("GPL");
1793 MODULE_DEVICE_TABLE(usb, ttusb_dec_table);