drm/nouveau: consume the return of large GSP message
[drm/drm-misc.git] / drivers / media / usb / ttusb-dec / ttusb_dec.c
blobb4575fe89c952fc4279ec3e410f256622f5880cd
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * TTUSB DEC Driver
5 * Copyright (C) 2003-2004 Alex Woods <linux-dvb@giblets.org>
6 * IR support by Peter Beutner <p.beutner@gmx.net>
7 */
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>
22 #include <linux/workqueue.h>
24 #include <media/dmxdev.h>
25 #include <media/dvb_demux.h>
26 #include <media/dvb_frontend.h>
27 #include <media/dvb_net.h>
28 #include "ttusbdecfe.h"
30 static int debug;
31 static int output_pva;
32 static int enable_rc;
34 module_param(debug, int, 0644);
35 MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
36 module_param(output_pva, int, 0444);
37 MODULE_PARM_DESC(output_pva, "Output PVA from dvr device (default:off)");
38 module_param(enable_rc, int, 0644);
39 MODULE_PARM_DESC(enable_rc, "Turn on/off IR remote control(default: off)");
41 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
43 #define dprintk if (debug) printk
45 #define DRIVER_NAME "TechnoTrend/Hauppauge DEC USB"
47 #define COMMAND_PIPE 0x03
48 #define RESULT_PIPE 0x04
49 #define IN_PIPE 0x08
50 #define OUT_PIPE 0x07
51 #define IRQ_PIPE 0x0A
53 #define COMMAND_PACKET_SIZE 0x3c
54 #define ARM_PACKET_SIZE 0x1000
55 #define IRQ_PACKET_SIZE 0x8
57 #define ISO_BUF_COUNT 0x04
58 #define FRAMES_PER_ISO_BUF 0x04
59 #define ISO_FRAME_SIZE 0x0380
61 #define MAX_PVA_LENGTH 6144
63 enum ttusb_dec_model {
64 TTUSB_DEC2000T,
65 TTUSB_DEC2540T,
66 TTUSB_DEC3000S
69 enum ttusb_dec_packet_type {
70 TTUSB_DEC_PACKET_PVA,
71 TTUSB_DEC_PACKET_SECTION,
72 TTUSB_DEC_PACKET_EMPTY
75 enum ttusb_dec_interface {
76 TTUSB_DEC_INTERFACE_INITIAL,
77 TTUSB_DEC_INTERFACE_IN,
78 TTUSB_DEC_INTERFACE_OUT
81 typedef int (dvb_filter_pes2ts_cb_t) (void *, unsigned char *);
83 struct dvb_filter_pes2ts {
84 unsigned char buf[188];
85 unsigned char cc;
86 dvb_filter_pes2ts_cb_t *cb;
87 void *priv;
90 struct ttusb_dec {
91 enum ttusb_dec_model model;
92 char *model_name;
93 char *firmware_name;
94 int can_playback;
96 /* DVB bits */
97 struct dvb_adapter adapter;
98 struct dmxdev dmxdev;
99 struct dvb_demux demux;
100 struct dmx_frontend frontend;
101 struct dvb_net dvb_net;
102 struct dvb_frontend* fe;
104 u16 pid[DMX_PES_OTHER];
106 /* USB bits */
107 struct usb_device *udev;
108 u8 trans_count;
109 unsigned int command_pipe;
110 unsigned int result_pipe;
111 unsigned int in_pipe;
112 unsigned int out_pipe;
113 unsigned int irq_pipe;
114 enum ttusb_dec_interface interface;
115 struct mutex usb_mutex;
117 void *irq_buffer;
118 struct urb *irq_urb;
119 dma_addr_t irq_dma_handle;
120 void *iso_buffer;
121 struct urb *iso_urb[ISO_BUF_COUNT];
122 int iso_stream_count;
123 struct mutex iso_mutex;
125 u8 packet[MAX_PVA_LENGTH + 4];
126 enum ttusb_dec_packet_type packet_type;
127 int packet_state;
128 int packet_length;
129 int packet_payload_length;
130 u16 next_packet_id;
132 int pva_stream_count;
133 int filter_stream_count;
135 struct dvb_filter_pes2ts a_pes2ts;
136 struct dvb_filter_pes2ts v_pes2ts;
138 u8 v_pes[16 + MAX_PVA_LENGTH];
139 int v_pes_length;
140 int v_pes_postbytes;
142 struct list_head urb_frame_list;
143 struct work_struct urb_bh_work;
144 spinlock_t urb_frame_list_lock;
146 struct dvb_demux_filter *audio_filter;
147 struct dvb_demux_filter *video_filter;
148 struct list_head filter_info_list;
149 spinlock_t filter_info_list_lock;
151 struct input_dev *rc_input_dev;
152 char rc_phys[64];
154 int active; /* Loaded successfully */
157 struct urb_frame {
158 u8 data[ISO_FRAME_SIZE];
159 int length;
160 struct list_head urb_frame_list;
163 struct filter_info {
164 u8 stream_id;
165 struct dvb_demux_filter *filter;
166 struct list_head filter_info_list;
169 static u16 rc_keys[] = {
170 KEY_POWER,
171 KEY_MUTE,
172 KEY_1,
173 KEY_2,
174 KEY_3,
175 KEY_4,
176 KEY_5,
177 KEY_6,
178 KEY_7,
179 KEY_8,
180 KEY_9,
181 KEY_0,
182 KEY_CHANNELUP,
183 KEY_VOLUMEDOWN,
184 KEY_OK,
185 KEY_VOLUMEUP,
186 KEY_CHANNELDOWN,
187 KEY_PREVIOUS,
188 KEY_ESC,
189 KEY_RED,
190 KEY_GREEN,
191 KEY_YELLOW,
192 KEY_BLUE,
193 KEY_OPTION,
194 KEY_M,
195 KEY_RADIO
198 static void dvb_filter_pes2ts_init(struct dvb_filter_pes2ts *p2ts,
199 unsigned short pid,
200 dvb_filter_pes2ts_cb_t *cb, void *priv)
202 unsigned char *buf=p2ts->buf;
204 buf[0]=0x47;
205 buf[1]=(pid>>8);
206 buf[2]=pid&0xff;
207 p2ts->cc=0;
208 p2ts->cb=cb;
209 p2ts->priv=priv;
212 static int dvb_filter_pes2ts(struct dvb_filter_pes2ts *p2ts,
213 unsigned char *pes, int len, int payload_start)
215 unsigned char *buf=p2ts->buf;
216 int ret=0, rest;
218 //len=6+((pes[4]<<8)|pes[5]);
220 if (payload_start)
221 buf[1]|=0x40;
222 else
223 buf[1]&=~0x40;
224 while (len>=184) {
225 buf[3]=0x10|((p2ts->cc++)&0x0f);
226 memcpy(buf+4, pes, 184);
227 if ((ret=p2ts->cb(p2ts->priv, buf)))
228 return ret;
229 len-=184; pes+=184;
230 buf[1]&=~0x40;
232 if (!len)
233 return 0;
234 buf[3]=0x30|((p2ts->cc++)&0x0f);
235 rest=183-len;
236 if (rest) {
237 buf[5]=0x00;
238 if (rest-1)
239 memset(buf+6, 0xff, rest-1);
241 buf[4]=rest;
242 memcpy(buf+5+rest, pes, len);
243 return p2ts->cb(p2ts->priv, buf);
246 static void ttusb_dec_set_model(struct ttusb_dec *dec,
247 enum ttusb_dec_model model);
249 static void ttusb_dec_handle_irq( struct urb *urb)
251 struct ttusb_dec *dec = urb->context;
252 char *buffer = dec->irq_buffer;
253 int retval;
254 int index = buffer[4];
256 switch(urb->status) {
257 case 0: /*success*/
258 break;
259 case -ECONNRESET:
260 case -ENOENT:
261 case -ESHUTDOWN:
262 case -ETIME:
263 /* this urb is dead, cleanup */
264 dprintk("%s:urb shutting down with status: %d\n",
265 __func__, urb->status);
266 return;
267 default:
268 dprintk("%s:nonzero status received: %d\n",
269 __func__,urb->status);
270 goto exit;
273 if ((buffer[0] == 0x1) && (buffer[2] == 0x15)) {
275 * IR - Event
277 * this is an fact a bit too simple implementation;
278 * the box also reports a keyrepeat signal
279 * (with buffer[3] == 0x40) in an interval of ~100ms.
280 * But to handle this correctly we had to imlemenent some
281 * kind of timer which signals a 'key up' event if no
282 * keyrepeat signal is received for lets say 200ms.
283 * this should/could be added later ...
284 * for now lets report each signal as a key down and up
286 if (index - 1 < ARRAY_SIZE(rc_keys)) {
287 dprintk("%s:rc signal:%d\n", __func__, index);
288 input_report_key(dec->rc_input_dev, rc_keys[index - 1], 1);
289 input_sync(dec->rc_input_dev);
290 input_report_key(dec->rc_input_dev, rc_keys[index - 1], 0);
291 input_sync(dec->rc_input_dev);
295 exit:
296 retval = usb_submit_urb(urb, GFP_ATOMIC);
297 if (retval)
298 printk("%s - usb_commit_urb failed with result: %d\n",
299 __func__, retval);
302 static u16 crc16(u16 crc, const u8 *buf, size_t len)
304 u16 tmp;
306 while (len--) {
307 crc ^= *buf++;
308 crc ^= (u8)crc >> 4;
309 tmp = (u8)crc;
310 crc ^= (tmp ^ (tmp << 1)) << 4;
312 return crc;
315 static int ttusb_dec_send_command(struct ttusb_dec *dec, const u8 command,
316 int param_length, const u8 params[],
317 int *result_length, u8 cmd_result[])
319 int result, actual_len;
320 u8 *b;
322 dprintk("%s\n", __func__);
324 b = kzalloc(COMMAND_PACKET_SIZE + 4, GFP_KERNEL);
325 if (!b)
326 return -ENOMEM;
328 result = mutex_lock_interruptible(&dec->usb_mutex);
329 if (result) {
330 printk("%s: Failed to lock usb mutex.\n", __func__);
331 goto err_free;
334 b[0] = 0xaa;
335 b[1] = ++dec->trans_count;
336 b[2] = command;
337 b[3] = param_length;
339 if (params)
340 memcpy(&b[4], params, param_length);
342 if (debug) {
343 printk(KERN_DEBUG "%s: command: %*ph\n",
344 __func__, param_length, b);
347 result = usb_bulk_msg(dec->udev, dec->command_pipe, b,
348 COMMAND_PACKET_SIZE + 4, &actual_len, 1000);
350 if (result) {
351 printk("%s: command bulk message failed: error %d\n",
352 __func__, result);
353 goto err_mutex_unlock;
356 result = usb_bulk_msg(dec->udev, dec->result_pipe, b,
357 COMMAND_PACKET_SIZE + 4, &actual_len, 1000);
359 if (result) {
360 printk("%s: result bulk message failed: error %d\n",
361 __func__, result);
362 goto err_mutex_unlock;
363 } else {
364 if (debug) {
365 printk(KERN_DEBUG "%s: result: %*ph\n",
366 __func__, actual_len, b);
369 if (result_length)
370 *result_length = b[3];
371 if (cmd_result && b[3] > 0)
372 memcpy(cmd_result, &b[4], b[3]);
375 err_mutex_unlock:
376 mutex_unlock(&dec->usb_mutex);
377 err_free:
378 kfree(b);
379 return result;
382 static int ttusb_dec_get_stb_state (struct ttusb_dec *dec, unsigned int *mode,
383 unsigned int *model, unsigned int *version)
385 u8 c[COMMAND_PACKET_SIZE];
386 int c_length;
387 int result;
388 __be32 tmp;
390 dprintk("%s\n", __func__);
392 result = ttusb_dec_send_command(dec, 0x08, 0, NULL, &c_length, c);
393 if (result)
394 return result;
396 if (c_length >= 0x0c) {
397 if (mode != NULL) {
398 memcpy(&tmp, c, 4);
399 *mode = ntohl(tmp);
401 if (model != NULL) {
402 memcpy(&tmp, &c[4], 4);
403 *model = ntohl(tmp);
405 if (version != NULL) {
406 memcpy(&tmp, &c[8], 4);
407 *version = ntohl(tmp);
409 return 0;
410 } else {
411 return -ENOENT;
415 static int ttusb_dec_audio_pes2ts_cb(void *priv, unsigned char *data)
417 struct ttusb_dec *dec = priv;
419 dec->audio_filter->feed->cb.ts(data, 188, NULL, 0,
420 &dec->audio_filter->feed->feed.ts, NULL);
422 return 0;
425 static int ttusb_dec_video_pes2ts_cb(void *priv, unsigned char *data)
427 struct ttusb_dec *dec = priv;
429 dec->video_filter->feed->cb.ts(data, 188, NULL, 0,
430 &dec->video_filter->feed->feed.ts, NULL);
432 return 0;
435 static void ttusb_dec_set_pids(struct ttusb_dec *dec)
437 u8 b[] = { 0x00, 0x00, 0x00, 0x00,
438 0x00, 0x00, 0xff, 0xff,
439 0xff, 0xff, 0xff, 0xff };
441 __be16 pcr = htons(dec->pid[DMX_PES_PCR]);
442 __be16 audio = htons(dec->pid[DMX_PES_AUDIO]);
443 __be16 video = htons(dec->pid[DMX_PES_VIDEO]);
445 dprintk("%s\n", __func__);
447 memcpy(&b[0], &pcr, 2);
448 memcpy(&b[2], &audio, 2);
449 memcpy(&b[4], &video, 2);
451 ttusb_dec_send_command(dec, 0x50, sizeof(b), b, NULL, NULL);
453 dvb_filter_pes2ts_init(&dec->a_pes2ts, dec->pid[DMX_PES_AUDIO],
454 ttusb_dec_audio_pes2ts_cb, dec);
455 dvb_filter_pes2ts_init(&dec->v_pes2ts, dec->pid[DMX_PES_VIDEO],
456 ttusb_dec_video_pes2ts_cb, dec);
457 dec->v_pes_length = 0;
458 dec->v_pes_postbytes = 0;
461 static void ttusb_dec_process_pva(struct ttusb_dec *dec, u8 *pva, int length)
463 if (length < 8) {
464 printk("%s: packet too short - discarding\n", __func__);
465 return;
468 if (length > 8 + MAX_PVA_LENGTH) {
469 printk("%s: packet too long - discarding\n", __func__);
470 return;
473 switch (pva[2]) {
475 case 0x01: { /* VideoStream */
476 int prebytes = pva[5] & 0x03;
477 int postbytes = (pva[5] & 0x0c) >> 2;
478 __be16 v_pes_payload_length;
480 if (output_pva) {
481 dec->video_filter->feed->cb.ts(pva, length, NULL, 0,
482 &dec->video_filter->feed->feed.ts, NULL);
483 return;
486 if (dec->v_pes_postbytes > 0 &&
487 dec->v_pes_postbytes == prebytes) {
488 memcpy(&dec->v_pes[dec->v_pes_length],
489 &pva[12], prebytes);
491 dvb_filter_pes2ts(&dec->v_pes2ts, dec->v_pes,
492 dec->v_pes_length + prebytes, 1);
495 if (pva[5] & 0x10) {
496 dec->v_pes[7] = 0x80;
497 dec->v_pes[8] = 0x05;
499 dec->v_pes[9] = 0x21 | ((pva[8] & 0xc0) >> 5);
500 dec->v_pes[10] = ((pva[8] & 0x3f) << 2) |
501 ((pva[9] & 0xc0) >> 6);
502 dec->v_pes[11] = 0x01 |
503 ((pva[9] & 0x3f) << 2) |
504 ((pva[10] & 0x80) >> 6);
505 dec->v_pes[12] = ((pva[10] & 0x7f) << 1) |
506 ((pva[11] & 0xc0) >> 7);
507 dec->v_pes[13] = 0x01 | ((pva[11] & 0x7f) << 1);
509 memcpy(&dec->v_pes[14], &pva[12 + prebytes],
510 length - 12 - prebytes);
511 dec->v_pes_length = 14 + length - 12 - prebytes;
512 } else {
513 dec->v_pes[7] = 0x00;
514 dec->v_pes[8] = 0x00;
516 memcpy(&dec->v_pes[9], &pva[8], length - 8);
517 dec->v_pes_length = 9 + length - 8;
520 dec->v_pes_postbytes = postbytes;
522 if (dec->v_pes[9 + dec->v_pes[8]] == 0x00 &&
523 dec->v_pes[10 + dec->v_pes[8]] == 0x00 &&
524 dec->v_pes[11 + dec->v_pes[8]] == 0x01)
525 dec->v_pes[6] = 0x84;
526 else
527 dec->v_pes[6] = 0x80;
529 v_pes_payload_length = htons(dec->v_pes_length - 6 +
530 postbytes);
531 memcpy(&dec->v_pes[4], &v_pes_payload_length, 2);
533 if (postbytes == 0)
534 dvb_filter_pes2ts(&dec->v_pes2ts, dec->v_pes,
535 dec->v_pes_length, 1);
537 break;
540 case 0x02: /* MainAudioStream */
541 if (output_pva) {
542 dec->audio_filter->feed->cb.ts(pva, length, NULL, 0,
543 &dec->audio_filter->feed->feed.ts, NULL);
544 return;
547 dvb_filter_pes2ts(&dec->a_pes2ts, &pva[8], length - 8,
548 pva[5] & 0x10);
549 break;
551 default:
552 printk("%s: unknown PVA type: %02x.\n", __func__,
553 pva[2]);
554 break;
558 static void ttusb_dec_process_filter(struct ttusb_dec *dec, u8 *packet,
559 int length)
561 struct list_head *item;
562 struct filter_info *finfo;
563 struct dvb_demux_filter *filter = NULL;
564 unsigned long flags;
565 u8 sid;
567 sid = packet[1];
568 spin_lock_irqsave(&dec->filter_info_list_lock, flags);
569 for (item = dec->filter_info_list.next; item != &dec->filter_info_list;
570 item = item->next) {
571 finfo = list_entry(item, struct filter_info, filter_info_list);
572 if (finfo->stream_id == sid) {
573 filter = finfo->filter;
574 break;
577 spin_unlock_irqrestore(&dec->filter_info_list_lock, flags);
579 if (filter)
580 filter->feed->cb.sec(&packet[2], length - 2, NULL, 0,
581 &filter->filter, NULL);
584 static void ttusb_dec_process_packet(struct ttusb_dec *dec)
586 int i;
587 u16 csum = 0;
588 u16 packet_id;
590 if (dec->packet_length % 2) {
591 printk("%s: odd sized packet - discarding\n", __func__);
592 return;
595 for (i = 0; i < dec->packet_length; i += 2)
596 csum ^= ((dec->packet[i] << 8) + dec->packet[i + 1]);
598 if (csum) {
599 printk("%s: checksum failed - discarding\n", __func__);
600 return;
603 packet_id = dec->packet[dec->packet_length - 4] << 8;
604 packet_id += dec->packet[dec->packet_length - 3];
606 if ((packet_id != dec->next_packet_id) && dec->next_packet_id) {
607 printk("%s: warning: lost packets between %u and %u\n",
608 __func__, dec->next_packet_id - 1, packet_id);
611 if (packet_id == 0xffff)
612 dec->next_packet_id = 0x8000;
613 else
614 dec->next_packet_id = packet_id + 1;
616 switch (dec->packet_type) {
617 case TTUSB_DEC_PACKET_PVA:
618 if (dec->pva_stream_count)
619 ttusb_dec_process_pva(dec, dec->packet,
620 dec->packet_payload_length);
621 break;
623 case TTUSB_DEC_PACKET_SECTION:
624 if (dec->filter_stream_count)
625 ttusb_dec_process_filter(dec, dec->packet,
626 dec->packet_payload_length);
627 break;
629 case TTUSB_DEC_PACKET_EMPTY:
630 break;
634 static void swap_bytes(u8 *b, int length)
636 length -= length % 2;
637 for (; length; b += 2, length -= 2)
638 swap(*b, *(b + 1));
641 static void ttusb_dec_process_urb_frame(struct ttusb_dec *dec, u8 *b,
642 int length)
644 swap_bytes(b, length);
646 while (length) {
647 switch (dec->packet_state) {
649 case 0:
650 case 1:
651 case 2:
652 if (*b++ == 0xaa)
653 dec->packet_state++;
654 else
655 dec->packet_state = 0;
657 length--;
658 break;
660 case 3:
661 if (*b == 0x00) {
662 dec->packet_state++;
663 dec->packet_length = 0;
664 } else if (*b != 0xaa) {
665 dec->packet_state = 0;
668 b++;
669 length--;
670 break;
672 case 4:
673 dec->packet[dec->packet_length++] = *b++;
675 if (dec->packet_length == 2) {
676 if (dec->packet[0] == 'A' &&
677 dec->packet[1] == 'V') {
678 dec->packet_type =
679 TTUSB_DEC_PACKET_PVA;
680 dec->packet_state++;
681 } else if (dec->packet[0] == 'S') {
682 dec->packet_type =
683 TTUSB_DEC_PACKET_SECTION;
684 dec->packet_state++;
685 } else if (dec->packet[0] == 0x00) {
686 dec->packet_type =
687 TTUSB_DEC_PACKET_EMPTY;
688 dec->packet_payload_length = 2;
689 dec->packet_state = 7;
690 } else {
691 printk("%s: unknown packet type: %02x%02x\n",
692 __func__,
693 dec->packet[0], dec->packet[1]);
694 dec->packet_state = 0;
698 length--;
699 break;
701 case 5:
702 dec->packet[dec->packet_length++] = *b++;
704 if (dec->packet_type == TTUSB_DEC_PACKET_PVA &&
705 dec->packet_length == 8) {
706 dec->packet_state++;
707 dec->packet_payload_length = 8 +
708 (dec->packet[6] << 8) +
709 dec->packet[7];
710 } else if (dec->packet_type ==
711 TTUSB_DEC_PACKET_SECTION &&
712 dec->packet_length == 5) {
713 dec->packet_state++;
714 dec->packet_payload_length = 5 +
715 ((dec->packet[3] & 0x0f) << 8) +
716 dec->packet[4];
719 length--;
720 break;
722 case 6: {
723 int remainder = dec->packet_payload_length -
724 dec->packet_length;
726 if (length >= remainder) {
727 memcpy(dec->packet + dec->packet_length,
728 b, remainder);
729 dec->packet_length += remainder;
730 b += remainder;
731 length -= remainder;
732 dec->packet_state++;
733 } else {
734 memcpy(&dec->packet[dec->packet_length],
735 b, length);
736 dec->packet_length += length;
737 length = 0;
740 break;
743 case 7: {
744 int tail = 4;
746 dec->packet[dec->packet_length++] = *b++;
748 if (dec->packet_type == TTUSB_DEC_PACKET_SECTION &&
749 dec->packet_payload_length % 2)
750 tail++;
752 if (dec->packet_length ==
753 dec->packet_payload_length + tail) {
754 ttusb_dec_process_packet(dec);
755 dec->packet_state = 0;
758 length--;
759 break;
762 default:
763 printk("%s: illegal packet state encountered.\n",
764 __func__);
765 dec->packet_state = 0;
770 static void ttusb_dec_process_urb_frame_list(struct work_struct *t)
772 struct ttusb_dec *dec = from_work(dec, t, urb_bh_work);
773 struct list_head *item;
774 struct urb_frame *frame;
775 unsigned long flags;
777 while (1) {
778 spin_lock_irqsave(&dec->urb_frame_list_lock, flags);
779 if ((item = dec->urb_frame_list.next) != &dec->urb_frame_list) {
780 frame = list_entry(item, struct urb_frame,
781 urb_frame_list);
782 list_del(&frame->urb_frame_list);
783 } else {
784 spin_unlock_irqrestore(&dec->urb_frame_list_lock,
785 flags);
786 return;
788 spin_unlock_irqrestore(&dec->urb_frame_list_lock, flags);
790 ttusb_dec_process_urb_frame(dec, frame->data, frame->length);
791 kfree(frame);
795 static void ttusb_dec_process_urb(struct urb *urb)
797 struct ttusb_dec *dec = urb->context;
799 if (!urb->status) {
800 int i;
802 for (i = 0; i < FRAMES_PER_ISO_BUF; i++) {
803 struct usb_iso_packet_descriptor *d;
804 u8 *b;
805 int length;
806 struct urb_frame *frame;
808 d = &urb->iso_frame_desc[i];
809 b = urb->transfer_buffer + d->offset;
810 length = d->actual_length;
812 if ((frame = kmalloc(sizeof(struct urb_frame),
813 GFP_ATOMIC))) {
814 unsigned long flags;
816 memcpy(frame->data, b, length);
817 frame->length = length;
819 spin_lock_irqsave(&dec->urb_frame_list_lock,
820 flags);
821 list_add_tail(&frame->urb_frame_list,
822 &dec->urb_frame_list);
823 spin_unlock_irqrestore(&dec->urb_frame_list_lock,
824 flags);
826 queue_work(system_bh_wq, &dec->urb_bh_work);
829 } else {
830 /* -ENOENT is expected when unlinking urbs */
831 if (urb->status != -ENOENT)
832 dprintk("%s: urb error: %d\n", __func__,
833 urb->status);
836 if (dec->iso_stream_count)
837 usb_submit_urb(urb, GFP_ATOMIC);
840 static void ttusb_dec_setup_urbs(struct ttusb_dec *dec)
842 int i, j, buffer_offset = 0;
844 dprintk("%s\n", __func__);
846 for (i = 0; i < ISO_BUF_COUNT; i++) {
847 int frame_offset = 0;
848 struct urb *urb = dec->iso_urb[i];
850 urb->dev = dec->udev;
851 urb->context = dec;
852 urb->complete = ttusb_dec_process_urb;
853 urb->pipe = dec->in_pipe;
854 urb->transfer_flags = URB_ISO_ASAP;
855 urb->interval = 1;
856 urb->number_of_packets = FRAMES_PER_ISO_BUF;
857 urb->transfer_buffer_length = ISO_FRAME_SIZE *
858 FRAMES_PER_ISO_BUF;
859 urb->transfer_buffer = dec->iso_buffer + buffer_offset;
860 buffer_offset += ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF;
862 for (j = 0; j < FRAMES_PER_ISO_BUF; j++) {
863 urb->iso_frame_desc[j].offset = frame_offset;
864 urb->iso_frame_desc[j].length = ISO_FRAME_SIZE;
865 frame_offset += ISO_FRAME_SIZE;
870 static void ttusb_dec_stop_iso_xfer(struct ttusb_dec *dec)
872 int i;
874 dprintk("%s\n", __func__);
876 if (mutex_lock_interruptible(&dec->iso_mutex))
877 return;
879 dec->iso_stream_count--;
881 if (!dec->iso_stream_count) {
882 for (i = 0; i < ISO_BUF_COUNT; i++)
883 usb_kill_urb(dec->iso_urb[i]);
886 mutex_unlock(&dec->iso_mutex);
889 /* Setting the interface of the DEC tends to take down the USB communications
890 * for a short period, so it's important not to call this function just before
891 * trying to talk to it.
893 static int ttusb_dec_set_interface(struct ttusb_dec *dec,
894 enum ttusb_dec_interface interface)
896 int result = 0;
897 u8 b[] = { 0x05 };
899 if (interface != dec->interface) {
900 switch (interface) {
901 case TTUSB_DEC_INTERFACE_INITIAL:
902 result = usb_set_interface(dec->udev, 0, 0);
903 break;
904 case TTUSB_DEC_INTERFACE_IN:
905 result = ttusb_dec_send_command(dec, 0x80, sizeof(b),
906 b, NULL, NULL);
907 if (result)
908 return result;
909 result = usb_set_interface(dec->udev, 0, 8);
910 break;
911 case TTUSB_DEC_INTERFACE_OUT:
912 result = usb_set_interface(dec->udev, 0, 1);
913 break;
916 if (result)
917 return result;
919 dec->interface = interface;
922 return 0;
925 static int ttusb_dec_start_iso_xfer(struct ttusb_dec *dec)
927 int i, result;
929 dprintk("%s\n", __func__);
931 if (mutex_lock_interruptible(&dec->iso_mutex))
932 return -EAGAIN;
934 if (!dec->iso_stream_count) {
935 ttusb_dec_setup_urbs(dec);
937 dec->packet_state = 0;
938 dec->v_pes_postbytes = 0;
939 dec->next_packet_id = 0;
941 for (i = 0; i < ISO_BUF_COUNT; i++) {
942 if ((result = usb_submit_urb(dec->iso_urb[i],
943 GFP_ATOMIC))) {
944 printk("%s: failed urb submission %d: error %d\n",
945 __func__, i, result);
947 while (i) {
948 usb_kill_urb(dec->iso_urb[i - 1]);
949 i--;
952 mutex_unlock(&dec->iso_mutex);
953 return result;
958 dec->iso_stream_count++;
960 mutex_unlock(&dec->iso_mutex);
962 return 0;
965 static int ttusb_dec_start_ts_feed(struct dvb_demux_feed *dvbdmxfeed)
967 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
968 struct ttusb_dec *dec = dvbdmx->priv;
969 u8 b0[] = { 0x05 };
970 int result = 0;
972 dprintk("%s\n", __func__);
974 dprintk(" ts_type:");
976 if (dvbdmxfeed->ts_type & TS_DECODER)
977 dprintk(" TS_DECODER");
979 if (dvbdmxfeed->ts_type & TS_PACKET)
980 dprintk(" TS_PACKET");
982 if (dvbdmxfeed->ts_type & TS_PAYLOAD_ONLY)
983 dprintk(" TS_PAYLOAD_ONLY");
985 dprintk("\n");
987 switch (dvbdmxfeed->pes_type) {
989 case DMX_PES_VIDEO:
990 dprintk(" pes_type: DMX_PES_VIDEO\n");
991 dec->pid[DMX_PES_PCR] = dvbdmxfeed->pid;
992 dec->pid[DMX_PES_VIDEO] = dvbdmxfeed->pid;
993 dec->video_filter = dvbdmxfeed->filter;
994 ttusb_dec_set_pids(dec);
995 break;
997 case DMX_PES_AUDIO:
998 dprintk(" pes_type: DMX_PES_AUDIO\n");
999 dec->pid[DMX_PES_AUDIO] = dvbdmxfeed->pid;
1000 dec->audio_filter = dvbdmxfeed->filter;
1001 ttusb_dec_set_pids(dec);
1002 break;
1004 case DMX_PES_TELETEXT:
1005 dec->pid[DMX_PES_TELETEXT] = dvbdmxfeed->pid;
1006 dprintk(" pes_type: DMX_PES_TELETEXT(not supported)\n");
1007 return -ENOSYS;
1009 case DMX_PES_PCR:
1010 dprintk(" pes_type: DMX_PES_PCR\n");
1011 dec->pid[DMX_PES_PCR] = dvbdmxfeed->pid;
1012 ttusb_dec_set_pids(dec);
1013 break;
1015 case DMX_PES_OTHER:
1016 dprintk(" pes_type: DMX_PES_OTHER(not supported)\n");
1017 return -ENOSYS;
1019 default:
1020 dprintk(" pes_type: unknown (%d)\n", dvbdmxfeed->pes_type);
1021 return -EINVAL;
1025 result = ttusb_dec_send_command(dec, 0x80, sizeof(b0), b0, NULL, NULL);
1026 if (result)
1027 return result;
1029 dec->pva_stream_count++;
1030 return ttusb_dec_start_iso_xfer(dec);
1033 static int ttusb_dec_start_sec_feed(struct dvb_demux_feed *dvbdmxfeed)
1035 struct ttusb_dec *dec = dvbdmxfeed->demux->priv;
1036 u8 b0[] = { 0x00, 0x00, 0x00, 0x01,
1037 0x00, 0x00, 0x00, 0x00,
1038 0x00, 0x00, 0x00, 0x00,
1039 0x00, 0x00, 0x00, 0x00,
1040 0x00, 0xff, 0x00, 0x00,
1041 0x00, 0x00, 0x00, 0x00,
1042 0x00, 0x00, 0x00, 0x00,
1043 0x00 };
1044 __be16 pid;
1045 u8 c[COMMAND_PACKET_SIZE];
1046 int c_length;
1047 int result;
1048 struct filter_info *finfo;
1049 unsigned long flags;
1050 u8 x = 1;
1052 dprintk("%s\n", __func__);
1054 pid = htons(dvbdmxfeed->pid);
1055 memcpy(&b0[0], &pid, 2);
1056 memcpy(&b0[4], &x, 1);
1057 memcpy(&b0[5], &dvbdmxfeed->filter->filter.filter_value[0], 1);
1059 result = ttusb_dec_send_command(dec, 0x60, sizeof(b0), b0,
1060 &c_length, c);
1062 if (!result) {
1063 if (c_length == 2) {
1064 if (!(finfo = kmalloc(sizeof(struct filter_info),
1065 GFP_ATOMIC)))
1066 return -ENOMEM;
1068 finfo->stream_id = c[1];
1069 finfo->filter = dvbdmxfeed->filter;
1071 spin_lock_irqsave(&dec->filter_info_list_lock, flags);
1072 list_add_tail(&finfo->filter_info_list,
1073 &dec->filter_info_list);
1074 spin_unlock_irqrestore(&dec->filter_info_list_lock,
1075 flags);
1077 dvbdmxfeed->priv = finfo;
1079 dec->filter_stream_count++;
1080 return ttusb_dec_start_iso_xfer(dec);
1083 return -EAGAIN;
1084 } else
1085 return result;
1088 static int ttusb_dec_start_feed(struct dvb_demux_feed *dvbdmxfeed)
1090 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
1092 dprintk("%s\n", __func__);
1094 if (!dvbdmx->dmx.frontend)
1095 return -EINVAL;
1097 dprintk(" pid: 0x%04X\n", dvbdmxfeed->pid);
1099 switch (dvbdmxfeed->type) {
1101 case DMX_TYPE_TS:
1102 return ttusb_dec_start_ts_feed(dvbdmxfeed);
1104 case DMX_TYPE_SEC:
1105 return ttusb_dec_start_sec_feed(dvbdmxfeed);
1107 default:
1108 dprintk(" type: unknown (%d)\n", dvbdmxfeed->type);
1109 return -EINVAL;
1114 static int ttusb_dec_stop_ts_feed(struct dvb_demux_feed *dvbdmxfeed)
1116 struct ttusb_dec *dec = dvbdmxfeed->demux->priv;
1117 u8 b0[] = { 0x00 };
1119 ttusb_dec_send_command(dec, 0x81, sizeof(b0), b0, NULL, NULL);
1121 dec->pva_stream_count--;
1123 ttusb_dec_stop_iso_xfer(dec);
1125 return 0;
1128 static int ttusb_dec_stop_sec_feed(struct dvb_demux_feed *dvbdmxfeed)
1130 struct ttusb_dec *dec = dvbdmxfeed->demux->priv;
1131 u8 b0[] = { 0x00, 0x00 };
1132 struct filter_info *finfo = dvbdmxfeed->priv;
1133 unsigned long flags;
1135 b0[1] = finfo->stream_id;
1136 spin_lock_irqsave(&dec->filter_info_list_lock, flags);
1137 list_del(&finfo->filter_info_list);
1138 spin_unlock_irqrestore(&dec->filter_info_list_lock, flags);
1139 kfree(finfo);
1140 ttusb_dec_send_command(dec, 0x62, sizeof(b0), b0, NULL, NULL);
1142 dec->filter_stream_count--;
1144 ttusb_dec_stop_iso_xfer(dec);
1146 return 0;
1149 static int ttusb_dec_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
1151 dprintk("%s\n", __func__);
1153 switch (dvbdmxfeed->type) {
1154 case DMX_TYPE_TS:
1155 return ttusb_dec_stop_ts_feed(dvbdmxfeed);
1157 case DMX_TYPE_SEC:
1158 return ttusb_dec_stop_sec_feed(dvbdmxfeed);
1161 return 0;
1164 static void ttusb_dec_free_iso_urbs(struct ttusb_dec *dec)
1166 int i;
1168 dprintk("%s\n", __func__);
1170 for (i = 0; i < ISO_BUF_COUNT; i++)
1171 usb_free_urb(dec->iso_urb[i]);
1172 kfree(dec->iso_buffer);
1175 static int ttusb_dec_alloc_iso_urbs(struct ttusb_dec *dec)
1177 int i;
1179 dprintk("%s\n", __func__);
1181 dec->iso_buffer = kcalloc(FRAMES_PER_ISO_BUF * ISO_BUF_COUNT,
1182 ISO_FRAME_SIZE, GFP_KERNEL);
1183 if (!dec->iso_buffer)
1184 return -ENOMEM;
1186 for (i = 0; i < ISO_BUF_COUNT; i++) {
1187 struct urb *urb;
1189 if (!(urb = usb_alloc_urb(FRAMES_PER_ISO_BUF, GFP_ATOMIC))) {
1190 ttusb_dec_free_iso_urbs(dec);
1191 return -ENOMEM;
1194 dec->iso_urb[i] = urb;
1197 ttusb_dec_setup_urbs(dec);
1199 return 0;
1202 static void ttusb_dec_init_bh_work(struct ttusb_dec *dec)
1204 spin_lock_init(&dec->urb_frame_list_lock);
1205 INIT_LIST_HEAD(&dec->urb_frame_list);
1206 INIT_WORK(&dec->urb_bh_work, ttusb_dec_process_urb_frame_list);
1209 static int ttusb_init_rc( struct ttusb_dec *dec)
1211 struct input_dev *input_dev;
1212 u8 b[] = { 0x00, 0x01 };
1213 int i;
1214 int err;
1216 usb_make_path(dec->udev, dec->rc_phys, sizeof(dec->rc_phys));
1217 strlcat(dec->rc_phys, "/input0", sizeof(dec->rc_phys));
1219 input_dev = input_allocate_device();
1220 if (!input_dev)
1221 return -ENOMEM;
1223 input_dev->name = "ttusb_dec remote control";
1224 input_dev->phys = dec->rc_phys;
1225 input_dev->evbit[0] = BIT_MASK(EV_KEY);
1226 input_dev->keycodesize = sizeof(u16);
1227 input_dev->keycodemax = 0x1a;
1228 input_dev->keycode = rc_keys;
1230 for (i = 0; i < ARRAY_SIZE(rc_keys); i++)
1231 set_bit(rc_keys[i], input_dev->keybit);
1233 err = input_register_device(input_dev);
1234 if (err) {
1235 input_free_device(input_dev);
1236 return err;
1239 dec->rc_input_dev = input_dev;
1240 if (usb_submit_urb(dec->irq_urb, GFP_KERNEL))
1241 printk("%s: usb_submit_urb failed\n",__func__);
1242 /* enable irq pipe */
1243 ttusb_dec_send_command(dec,0xb0,sizeof(b),b,NULL,NULL);
1245 return 0;
1248 static void ttusb_dec_init_v_pes(struct ttusb_dec *dec)
1250 dprintk("%s\n", __func__);
1252 dec->v_pes[0] = 0x00;
1253 dec->v_pes[1] = 0x00;
1254 dec->v_pes[2] = 0x01;
1255 dec->v_pes[3] = 0xe0;
1258 static int ttusb_dec_init_usb(struct ttusb_dec *dec)
1260 int result;
1262 dprintk("%s\n", __func__);
1264 mutex_init(&dec->usb_mutex);
1265 mutex_init(&dec->iso_mutex);
1267 dec->command_pipe = usb_sndbulkpipe(dec->udev, COMMAND_PIPE);
1268 dec->result_pipe = usb_rcvbulkpipe(dec->udev, RESULT_PIPE);
1269 dec->in_pipe = usb_rcvisocpipe(dec->udev, IN_PIPE);
1270 dec->out_pipe = usb_sndisocpipe(dec->udev, OUT_PIPE);
1271 dec->irq_pipe = usb_rcvintpipe(dec->udev, IRQ_PIPE);
1273 if(enable_rc) {
1274 dec->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
1275 if(!dec->irq_urb) {
1276 return -ENOMEM;
1278 dec->irq_buffer = usb_alloc_coherent(dec->udev,IRQ_PACKET_SIZE,
1279 GFP_KERNEL, &dec->irq_dma_handle);
1280 if(!dec->irq_buffer) {
1281 usb_free_urb(dec->irq_urb);
1282 return -ENOMEM;
1284 usb_fill_int_urb(dec->irq_urb, dec->udev,dec->irq_pipe,
1285 dec->irq_buffer, IRQ_PACKET_SIZE,
1286 ttusb_dec_handle_irq, dec, 1);
1287 dec->irq_urb->transfer_dma = dec->irq_dma_handle;
1288 dec->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1291 result = ttusb_dec_alloc_iso_urbs(dec);
1292 if (result) {
1293 usb_free_urb(dec->irq_urb);
1294 usb_free_coherent(dec->udev, IRQ_PACKET_SIZE,
1295 dec->irq_buffer, dec->irq_dma_handle);
1297 return result;
1300 static int ttusb_dec_boot_dsp(struct ttusb_dec *dec)
1302 int i, j, actual_len, result, size, trans_count;
1303 u8 b0[] = { 0x00, 0x00, 0x00, 0x00,
1304 0x00, 0x00, 0x00, 0x00,
1305 0x61, 0x00 };
1306 u8 b1[] = { 0x61 };
1307 u8 *b;
1308 char idstring[21];
1309 const u8 *firmware = NULL;
1310 size_t firmware_size = 0;
1311 u16 firmware_csum = 0;
1312 __be16 firmware_csum_ns;
1313 __be32 firmware_size_nl;
1314 u32 crc32_csum, crc32_check;
1315 __be32 tmp;
1316 const struct firmware *fw_entry = NULL;
1318 dprintk("%s\n", __func__);
1320 result = request_firmware(&fw_entry, dec->firmware_name, &dec->udev->dev);
1321 if (result) {
1322 printk(KERN_ERR "%s: Firmware (%s) unavailable.\n",
1323 __func__, dec->firmware_name);
1324 return result;
1327 firmware = fw_entry->data;
1328 firmware_size = fw_entry->size;
1330 if (firmware_size < 60) {
1331 printk("%s: firmware size too small for DSP code (%zu < 60).\n",
1332 __func__, firmware_size);
1333 release_firmware(fw_entry);
1334 return -ENOENT;
1337 /* a 32 bit checksum over the first 56 bytes of the DSP Code is stored
1338 at offset 56 of file, so use it to check if the firmware file is
1339 valid. */
1340 crc32_csum = crc32(~0L, firmware, 56) ^ ~0L;
1341 memcpy(&tmp, &firmware[56], 4);
1342 crc32_check = ntohl(tmp);
1343 if (crc32_csum != crc32_check) {
1344 printk("%s: crc32 check of DSP code failed (calculated 0x%08x != 0x%08x in file), file invalid.\n",
1345 __func__, crc32_csum, crc32_check);
1346 release_firmware(fw_entry);
1347 return -ENOENT;
1349 memcpy(idstring, &firmware[36], 20);
1350 idstring[20] = '\0';
1351 printk(KERN_INFO "ttusb_dec: found DSP code \"%s\".\n", idstring);
1353 firmware_size_nl = htonl(firmware_size);
1354 memcpy(b0, &firmware_size_nl, 4);
1355 firmware_csum = crc16(~0, firmware, firmware_size) ^ ~0;
1356 firmware_csum_ns = htons(firmware_csum);
1357 memcpy(&b0[6], &firmware_csum_ns, 2);
1359 result = ttusb_dec_send_command(dec, 0x41, sizeof(b0), b0, NULL, NULL);
1361 if (result) {
1362 release_firmware(fw_entry);
1363 return result;
1366 trans_count = 0;
1367 j = 0;
1369 b = kmalloc(ARM_PACKET_SIZE, GFP_KERNEL);
1370 if (b == NULL) {
1371 release_firmware(fw_entry);
1372 return -ENOMEM;
1375 for (i = 0; i < firmware_size; i += COMMAND_PACKET_SIZE) {
1376 size = firmware_size - i;
1377 if (size > COMMAND_PACKET_SIZE)
1378 size = COMMAND_PACKET_SIZE;
1380 b[j + 0] = 0xaa;
1381 b[j + 1] = trans_count++;
1382 b[j + 2] = 0xf0;
1383 b[j + 3] = size;
1384 memcpy(&b[j + 4], &firmware[i], size);
1386 j += COMMAND_PACKET_SIZE + 4;
1388 if (j >= ARM_PACKET_SIZE) {
1389 result = usb_bulk_msg(dec->udev, dec->command_pipe, b,
1390 ARM_PACKET_SIZE, &actual_len,
1391 100);
1392 j = 0;
1393 } else if (size < COMMAND_PACKET_SIZE) {
1394 result = usb_bulk_msg(dec->udev, dec->command_pipe, b,
1395 j - COMMAND_PACKET_SIZE + size,
1396 &actual_len, 100);
1400 result = ttusb_dec_send_command(dec, 0x43, sizeof(b1), b1, NULL, NULL);
1402 release_firmware(fw_entry);
1403 kfree(b);
1405 return result;
1408 static int ttusb_dec_init_stb(struct ttusb_dec *dec)
1410 int result;
1411 unsigned int mode = 0, model = 0, version = 0;
1413 dprintk("%s\n", __func__);
1415 result = ttusb_dec_get_stb_state(dec, &mode, &model, &version);
1416 if (result)
1417 return result;
1419 if (!mode) {
1420 if (version == 0xABCDEFAB)
1421 printk(KERN_INFO "ttusb_dec: no version info in Firmware\n");
1422 else
1423 printk(KERN_INFO "ttusb_dec: Firmware %x.%02x%c%c\n",
1424 version >> 24, (version >> 16) & 0xff,
1425 (version >> 8) & 0xff, version & 0xff);
1427 result = ttusb_dec_boot_dsp(dec);
1428 if (result)
1429 return result;
1430 } else {
1431 /* We can't trust the USB IDs that some firmwares
1432 give the box */
1433 switch (model) {
1434 case 0x00070001:
1435 case 0x00070008:
1436 case 0x0007000c:
1437 ttusb_dec_set_model(dec, TTUSB_DEC3000S);
1438 break;
1439 case 0x00070009:
1440 case 0x00070013:
1441 ttusb_dec_set_model(dec, TTUSB_DEC2000T);
1442 break;
1443 case 0x00070011:
1444 ttusb_dec_set_model(dec, TTUSB_DEC2540T);
1445 break;
1446 default:
1447 printk(KERN_ERR "%s: unknown model returned by firmware (%08x) - please report\n",
1448 __func__, model);
1449 return -ENOENT;
1451 if (version >= 0x01770000)
1452 dec->can_playback = 1;
1454 return 0;
1457 static int ttusb_dec_init_dvb(struct ttusb_dec *dec)
1459 int result;
1461 dprintk("%s\n", __func__);
1463 if ((result = dvb_register_adapter(&dec->adapter,
1464 dec->model_name, THIS_MODULE,
1465 &dec->udev->dev,
1466 adapter_nr)) < 0) {
1467 printk("%s: dvb_register_adapter failed: error %d\n",
1468 __func__, result);
1470 return result;
1473 dec->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING;
1475 dec->demux.priv = (void *)dec;
1476 dec->demux.filternum = 31;
1477 dec->demux.feednum = 31;
1478 dec->demux.start_feed = ttusb_dec_start_feed;
1479 dec->demux.stop_feed = ttusb_dec_stop_feed;
1480 dec->demux.write_to_decoder = NULL;
1482 if ((result = dvb_dmx_init(&dec->demux)) < 0) {
1483 printk("%s: dvb_dmx_init failed: error %d\n", __func__,
1484 result);
1486 dvb_unregister_adapter(&dec->adapter);
1488 return result;
1491 dec->dmxdev.filternum = 32;
1492 dec->dmxdev.demux = &dec->demux.dmx;
1493 dec->dmxdev.capabilities = 0;
1495 if ((result = dvb_dmxdev_init(&dec->dmxdev, &dec->adapter)) < 0) {
1496 printk("%s: dvb_dmxdev_init failed: error %d\n",
1497 __func__, result);
1499 dvb_dmx_release(&dec->demux);
1500 dvb_unregister_adapter(&dec->adapter);
1502 return result;
1505 dec->frontend.source = DMX_FRONTEND_0;
1507 if ((result = dec->demux.dmx.add_frontend(&dec->demux.dmx,
1508 &dec->frontend)) < 0) {
1509 printk("%s: dvb_dmx_init failed: error %d\n", __func__,
1510 result);
1512 dvb_dmxdev_release(&dec->dmxdev);
1513 dvb_dmx_release(&dec->demux);
1514 dvb_unregister_adapter(&dec->adapter);
1516 return result;
1519 if ((result = dec->demux.dmx.connect_frontend(&dec->demux.dmx,
1520 &dec->frontend)) < 0) {
1521 printk("%s: dvb_dmx_init failed: error %d\n", __func__,
1522 result);
1524 dec->demux.dmx.remove_frontend(&dec->demux.dmx, &dec->frontend);
1525 dvb_dmxdev_release(&dec->dmxdev);
1526 dvb_dmx_release(&dec->demux);
1527 dvb_unregister_adapter(&dec->adapter);
1529 return result;
1532 dvb_net_init(&dec->adapter, &dec->dvb_net, &dec->demux.dmx);
1534 return 0;
1537 static void ttusb_dec_exit_dvb(struct ttusb_dec *dec)
1539 dprintk("%s\n", __func__);
1541 dvb_net_release(&dec->dvb_net);
1542 dec->demux.dmx.close(&dec->demux.dmx);
1543 dec->demux.dmx.remove_frontend(&dec->demux.dmx, &dec->frontend);
1544 dvb_dmxdev_release(&dec->dmxdev);
1545 dvb_dmx_release(&dec->demux);
1546 if (dec->fe) {
1547 dvb_unregister_frontend(dec->fe);
1548 dvb_frontend_detach(dec->fe);
1550 dvb_unregister_adapter(&dec->adapter);
1553 static void ttusb_dec_exit_rc(struct ttusb_dec *dec)
1555 dprintk("%s\n", __func__);
1557 if (dec->rc_input_dev) {
1558 input_unregister_device(dec->rc_input_dev);
1559 dec->rc_input_dev = NULL;
1564 static void ttusb_dec_exit_usb(struct ttusb_dec *dec)
1566 int i;
1568 dprintk("%s\n", __func__);
1570 if (enable_rc) {
1571 /* we have to check whether the irq URB is already submitted.
1572 * As the irq is submitted after the interface is changed,
1573 * this is the best method i figured out.
1574 * Any others?*/
1575 if (dec->interface == TTUSB_DEC_INTERFACE_IN)
1576 usb_kill_urb(dec->irq_urb);
1578 usb_free_urb(dec->irq_urb);
1580 usb_free_coherent(dec->udev, IRQ_PACKET_SIZE,
1581 dec->irq_buffer, dec->irq_dma_handle);
1584 dec->iso_stream_count = 0;
1586 for (i = 0; i < ISO_BUF_COUNT; i++)
1587 usb_kill_urb(dec->iso_urb[i]);
1589 ttusb_dec_free_iso_urbs(dec);
1592 static void ttusb_dec_exit_bh_work(struct ttusb_dec *dec)
1594 struct list_head *item;
1595 struct urb_frame *frame;
1597 cancel_work_sync(&dec->urb_bh_work);
1599 while ((item = dec->urb_frame_list.next) != &dec->urb_frame_list) {
1600 frame = list_entry(item, struct urb_frame, urb_frame_list);
1601 list_del(&frame->urb_frame_list);
1602 kfree(frame);
1606 static void ttusb_dec_init_filters(struct ttusb_dec *dec)
1608 INIT_LIST_HEAD(&dec->filter_info_list);
1609 spin_lock_init(&dec->filter_info_list_lock);
1612 static void ttusb_dec_exit_filters(struct ttusb_dec *dec)
1614 struct list_head *item;
1615 struct filter_info *finfo;
1617 while ((item = dec->filter_info_list.next) != &dec->filter_info_list) {
1618 finfo = list_entry(item, struct filter_info, filter_info_list);
1619 list_del(&finfo->filter_info_list);
1620 kfree(finfo);
1624 static int fe_send_command(struct dvb_frontend* fe, const u8 command,
1625 int param_length, const u8 params[],
1626 int *result_length, u8 cmd_result[])
1628 struct ttusb_dec* dec = fe->dvb->priv;
1629 return ttusb_dec_send_command(dec, command, param_length, params, result_length, cmd_result);
1632 static const struct ttusbdecfe_config fe_config = {
1633 .send_command = fe_send_command
1636 static int ttusb_dec_probe(struct usb_interface *intf,
1637 const struct usb_device_id *id)
1639 struct usb_device *udev;
1640 struct ttusb_dec *dec;
1641 int result;
1643 dprintk("%s\n", __func__);
1645 udev = interface_to_usbdev(intf);
1647 if (!(dec = kzalloc(sizeof(struct ttusb_dec), GFP_KERNEL))) {
1648 printk("%s: couldn't allocate memory.\n", __func__);
1649 return -ENOMEM;
1652 usb_set_intfdata(intf, (void *)dec);
1654 switch (id->idProduct) {
1655 case 0x1006:
1656 ttusb_dec_set_model(dec, TTUSB_DEC3000S);
1657 break;
1659 case 0x1008:
1660 ttusb_dec_set_model(dec, TTUSB_DEC2000T);
1661 break;
1663 case 0x1009:
1664 ttusb_dec_set_model(dec, TTUSB_DEC2540T);
1665 break;
1668 dec->udev = udev;
1670 result = ttusb_dec_init_usb(dec);
1671 if (result)
1672 goto err_usb;
1673 result = ttusb_dec_init_stb(dec);
1674 if (result)
1675 goto err_stb;
1676 result = ttusb_dec_init_dvb(dec);
1677 if (result)
1678 goto err_stb;
1680 dec->adapter.priv = dec;
1681 switch (id->idProduct) {
1682 case 0x1006:
1683 dec->fe = ttusbdecfe_dvbs_attach(&fe_config);
1684 break;
1686 case 0x1008:
1687 case 0x1009:
1688 dec->fe = ttusbdecfe_dvbt_attach(&fe_config);
1689 break;
1692 if (dec->fe == NULL) {
1693 printk("dvb-ttusb-dec: A frontend driver was not found for device [%04x:%04x]\n",
1694 le16_to_cpu(dec->udev->descriptor.idVendor),
1695 le16_to_cpu(dec->udev->descriptor.idProduct));
1696 } else {
1697 if (dvb_register_frontend(&dec->adapter, dec->fe)) {
1698 printk("budget-ci: Frontend registration failed!\n");
1699 if (dec->fe->ops.release)
1700 dec->fe->ops.release(dec->fe);
1701 dec->fe = NULL;
1705 ttusb_dec_init_v_pes(dec);
1706 ttusb_dec_init_filters(dec);
1707 ttusb_dec_init_bh_work(dec);
1709 dec->active = 1;
1711 ttusb_dec_set_interface(dec, TTUSB_DEC_INTERFACE_IN);
1713 if (enable_rc)
1714 ttusb_init_rc(dec);
1716 return 0;
1717 err_stb:
1718 ttusb_dec_exit_usb(dec);
1719 err_usb:
1720 kfree(dec);
1721 return result;
1724 static void ttusb_dec_disconnect(struct usb_interface *intf)
1726 struct ttusb_dec *dec = usb_get_intfdata(intf);
1728 usb_set_intfdata(intf, NULL);
1730 dprintk("%s\n", __func__);
1732 if (dec->active) {
1733 ttusb_dec_exit_bh_work(dec);
1734 ttusb_dec_exit_filters(dec);
1735 if(enable_rc)
1736 ttusb_dec_exit_rc(dec);
1737 ttusb_dec_exit_usb(dec);
1738 ttusb_dec_exit_dvb(dec);
1741 kfree(dec);
1744 static void ttusb_dec_set_model(struct ttusb_dec *dec,
1745 enum ttusb_dec_model model)
1747 dec->model = model;
1749 switch (model) {
1750 case TTUSB_DEC2000T:
1751 dec->model_name = "DEC2000-t";
1752 dec->firmware_name = "dvb-ttusb-dec-2000t.fw";
1753 break;
1755 case TTUSB_DEC2540T:
1756 dec->model_name = "DEC2540-t";
1757 dec->firmware_name = "dvb-ttusb-dec-2540t.fw";
1758 break;
1760 case TTUSB_DEC3000S:
1761 dec->model_name = "DEC3000-s";
1762 dec->firmware_name = "dvb-ttusb-dec-3000s.fw";
1763 break;
1767 static const struct usb_device_id ttusb_dec_table[] = {
1768 {USB_DEVICE(0x0b48, 0x1006)}, /* DEC3000-s */
1769 /*{USB_DEVICE(0x0b48, 0x1007)}, Unconfirmed */
1770 {USB_DEVICE(0x0b48, 0x1008)}, /* DEC2000-t */
1771 {USB_DEVICE(0x0b48, 0x1009)}, /* DEC2540-t */
1775 static struct usb_driver ttusb_dec_driver = {
1776 .name = "ttusb-dec",
1777 .probe = ttusb_dec_probe,
1778 .disconnect = ttusb_dec_disconnect,
1779 .id_table = ttusb_dec_table,
1782 module_usb_driver(ttusb_dec_driver);
1784 MODULE_AUTHOR("Alex Woods <linux-dvb@giblets.org>");
1785 MODULE_DESCRIPTION(DRIVER_NAME);
1786 MODULE_LICENSE("GPL");
1787 MODULE_DEVICE_TABLE(usb, ttusb_dec_table);