Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / drivers / media / usb / ttusb-dec / ttusb_dec.c
bloba8900f5571f784014d273eb29058fa04ca08a55c
1 /*
2 * TTUSB DEC Driver
4 * Copyright (C) 2003-2004 Alex Woods <linux-dvb@giblets.org>
5 * IR support by Peter Beutner <p.beutner@gmx.net>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
19 #include <linux/list.h>
20 #include <linux/module.h>
21 #include <linux/pci.h>
22 #include <linux/slab.h>
23 #include <linux/spinlock.h>
24 #include <linux/usb.h>
25 #include <linux/interrupt.h>
26 #include <linux/firmware.h>
27 #include <linux/crc32.h>
28 #include <linux/init.h>
29 #include <linux/input.h>
31 #include <linux/mutex.h>
33 #include <media/dmxdev.h>
34 #include <media/dvb_demux.h>
35 #include <media/dvb_frontend.h>
36 #include <media/dvb_net.h>
37 #include "ttusbdecfe.h"
39 static int debug;
40 static int output_pva;
41 static int enable_rc;
43 module_param(debug, int, 0644);
44 MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
45 module_param(output_pva, int, 0444);
46 MODULE_PARM_DESC(output_pva, "Output PVA from dvr device (default:off)");
47 module_param(enable_rc, int, 0644);
48 MODULE_PARM_DESC(enable_rc, "Turn on/off IR remote control(default: off)");
50 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
52 #define dprintk if (debug) printk
54 #define DRIVER_NAME "TechnoTrend/Hauppauge DEC USB"
56 #define COMMAND_PIPE 0x03
57 #define RESULT_PIPE 0x04
58 #define IN_PIPE 0x08
59 #define OUT_PIPE 0x07
60 #define IRQ_PIPE 0x0A
62 #define COMMAND_PACKET_SIZE 0x3c
63 #define ARM_PACKET_SIZE 0x1000
64 #define IRQ_PACKET_SIZE 0x8
66 #define ISO_BUF_COUNT 0x04
67 #define FRAMES_PER_ISO_BUF 0x04
68 #define ISO_FRAME_SIZE 0x0380
70 #define MAX_PVA_LENGTH 6144
72 enum ttusb_dec_model {
73 TTUSB_DEC2000T,
74 TTUSB_DEC2540T,
75 TTUSB_DEC3000S
78 enum ttusb_dec_packet_type {
79 TTUSB_DEC_PACKET_PVA,
80 TTUSB_DEC_PACKET_SECTION,
81 TTUSB_DEC_PACKET_EMPTY
84 enum ttusb_dec_interface {
85 TTUSB_DEC_INTERFACE_INITIAL,
86 TTUSB_DEC_INTERFACE_IN,
87 TTUSB_DEC_INTERFACE_OUT
90 typedef int (dvb_filter_pes2ts_cb_t) (void *, unsigned char *);
92 struct dvb_filter_pes2ts {
93 unsigned char buf[188];
94 unsigned char cc;
95 dvb_filter_pes2ts_cb_t *cb;
96 void *priv;
99 struct ttusb_dec {
100 enum ttusb_dec_model model;
101 char *model_name;
102 char *firmware_name;
103 int can_playback;
105 /* DVB bits */
106 struct dvb_adapter adapter;
107 struct dmxdev dmxdev;
108 struct dvb_demux demux;
109 struct dmx_frontend frontend;
110 struct dvb_net dvb_net;
111 struct dvb_frontend* fe;
113 u16 pid[DMX_PES_OTHER];
115 /* USB bits */
116 struct usb_device *udev;
117 u8 trans_count;
118 unsigned int command_pipe;
119 unsigned int result_pipe;
120 unsigned int in_pipe;
121 unsigned int out_pipe;
122 unsigned int irq_pipe;
123 enum ttusb_dec_interface interface;
124 struct mutex usb_mutex;
126 void *irq_buffer;
127 struct urb *irq_urb;
128 dma_addr_t irq_dma_handle;
129 void *iso_buffer;
130 struct urb *iso_urb[ISO_BUF_COUNT];
131 int iso_stream_count;
132 struct mutex iso_mutex;
134 u8 packet[MAX_PVA_LENGTH + 4];
135 enum ttusb_dec_packet_type packet_type;
136 int packet_state;
137 int packet_length;
138 int packet_payload_length;
139 u16 next_packet_id;
141 int pva_stream_count;
142 int filter_stream_count;
144 struct dvb_filter_pes2ts a_pes2ts;
145 struct dvb_filter_pes2ts v_pes2ts;
147 u8 v_pes[16 + MAX_PVA_LENGTH];
148 int v_pes_length;
149 int v_pes_postbytes;
151 struct list_head urb_frame_list;
152 struct tasklet_struct urb_tasklet;
153 spinlock_t urb_frame_list_lock;
155 struct dvb_demux_filter *audio_filter;
156 struct dvb_demux_filter *video_filter;
157 struct list_head filter_info_list;
158 spinlock_t filter_info_list_lock;
160 struct input_dev *rc_input_dev;
161 char rc_phys[64];
163 int active; /* Loaded successfully */
166 struct urb_frame {
167 u8 data[ISO_FRAME_SIZE];
168 int length;
169 struct list_head urb_frame_list;
172 struct filter_info {
173 u8 stream_id;
174 struct dvb_demux_filter *filter;
175 struct list_head filter_info_list;
178 static u16 rc_keys[] = {
179 KEY_POWER,
180 KEY_MUTE,
181 KEY_1,
182 KEY_2,
183 KEY_3,
184 KEY_4,
185 KEY_5,
186 KEY_6,
187 KEY_7,
188 KEY_8,
189 KEY_9,
190 KEY_0,
191 KEY_CHANNELUP,
192 KEY_VOLUMEDOWN,
193 KEY_OK,
194 KEY_VOLUMEUP,
195 KEY_CHANNELDOWN,
196 KEY_PREVIOUS,
197 KEY_ESC,
198 KEY_RED,
199 KEY_GREEN,
200 KEY_YELLOW,
201 KEY_BLUE,
202 KEY_OPTION,
203 KEY_M,
204 KEY_RADIO
207 static void dvb_filter_pes2ts_init(struct dvb_filter_pes2ts *p2ts,
208 unsigned short pid,
209 dvb_filter_pes2ts_cb_t *cb, void *priv)
211 unsigned char *buf=p2ts->buf;
213 buf[0]=0x47;
214 buf[1]=(pid>>8);
215 buf[2]=pid&0xff;
216 p2ts->cc=0;
217 p2ts->cb=cb;
218 p2ts->priv=priv;
221 static int dvb_filter_pes2ts(struct dvb_filter_pes2ts *p2ts,
222 unsigned char *pes, int len, int payload_start)
224 unsigned char *buf=p2ts->buf;
225 int ret=0, rest;
227 //len=6+((pes[4]<<8)|pes[5]);
229 if (payload_start)
230 buf[1]|=0x40;
231 else
232 buf[1]&=~0x40;
233 while (len>=184) {
234 buf[3]=0x10|((p2ts->cc++)&0x0f);
235 memcpy(buf+4, pes, 184);
236 if ((ret=p2ts->cb(p2ts->priv, buf)))
237 return ret;
238 len-=184; pes+=184;
239 buf[1]&=~0x40;
241 if (!len)
242 return 0;
243 buf[3]=0x30|((p2ts->cc++)&0x0f);
244 rest=183-len;
245 if (rest) {
246 buf[5]=0x00;
247 if (rest-1)
248 memset(buf+6, 0xff, rest-1);
250 buf[4]=rest;
251 memcpy(buf+5+rest, pes, len);
252 return p2ts->cb(p2ts->priv, buf);
255 static void ttusb_dec_set_model(struct ttusb_dec *dec,
256 enum ttusb_dec_model model);
258 static void ttusb_dec_handle_irq( struct urb *urb)
260 struct ttusb_dec *dec = urb->context;
261 char *buffer = dec->irq_buffer;
262 int retval;
264 switch(urb->status) {
265 case 0: /*success*/
266 break;
267 case -ECONNRESET:
268 case -ENOENT:
269 case -ESHUTDOWN:
270 case -ETIME:
271 /* this urb is dead, cleanup */
272 dprintk("%s:urb shutting down with status: %d\n",
273 __func__, urb->status);
274 return;
275 default:
276 dprintk("%s:nonzero status received: %d\n",
277 __func__,urb->status);
278 goto exit;
281 if ((buffer[0] == 0x1) && (buffer[2] == 0x15)) {
283 * IR - Event
285 * this is an fact a bit too simple implementation;
286 * the box also reports a keyrepeat signal
287 * (with buffer[3] == 0x40) in an intervall of ~100ms.
288 * But to handle this correctly we had to imlemenent some
289 * kind of timer which signals a 'key up' event if no
290 * keyrepeat signal is received for lets say 200ms.
291 * this should/could be added later ...
292 * for now lets report each signal as a key down and up
294 if (buffer[4] - 1 < ARRAY_SIZE(rc_keys)) {
295 dprintk("%s:rc signal:%d\n", __func__, buffer[4]);
296 input_report_key(dec->rc_input_dev, rc_keys[buffer[4] - 1], 1);
297 input_sync(dec->rc_input_dev);
298 input_report_key(dec->rc_input_dev, rc_keys[buffer[4] - 1], 0);
299 input_sync(dec->rc_input_dev);
303 exit:
304 retval = usb_submit_urb(urb, GFP_ATOMIC);
305 if (retval)
306 printk("%s - usb_commit_urb failed with result: %d\n",
307 __func__, retval);
310 static u16 crc16(u16 crc, const u8 *buf, size_t len)
312 u16 tmp;
314 while (len--) {
315 crc ^= *buf++;
316 crc ^= (u8)crc >> 4;
317 tmp = (u8)crc;
318 crc ^= (tmp ^ (tmp << 1)) << 4;
320 return crc;
323 static int ttusb_dec_send_command(struct ttusb_dec *dec, const u8 command,
324 int param_length, const u8 params[],
325 int *result_length, u8 cmd_result[])
327 int result, actual_len;
328 u8 *b;
330 dprintk("%s\n", __func__);
332 b = kmalloc(COMMAND_PACKET_SIZE + 4, GFP_KERNEL);
333 if (!b)
334 return -ENOMEM;
336 if ((result = mutex_lock_interruptible(&dec->usb_mutex))) {
337 kfree(b);
338 printk("%s: Failed to lock usb mutex.\n", __func__);
339 return result;
342 b[0] = 0xaa;
343 b[1] = ++dec->trans_count;
344 b[2] = command;
345 b[3] = param_length;
347 if (params)
348 memcpy(&b[4], params, param_length);
350 if (debug) {
351 printk(KERN_DEBUG "%s: command: %*ph\n",
352 __func__, param_length, b);
355 result = usb_bulk_msg(dec->udev, dec->command_pipe, b,
356 COMMAND_PACKET_SIZE + 4, &actual_len, 1000);
358 if (result) {
359 printk("%s: command bulk message failed: error %d\n",
360 __func__, result);
361 mutex_unlock(&dec->usb_mutex);
362 kfree(b);
363 return result;
366 result = usb_bulk_msg(dec->udev, dec->result_pipe, b,
367 COMMAND_PACKET_SIZE + 4, &actual_len, 1000);
369 if (result) {
370 printk("%s: result bulk message failed: error %d\n",
371 __func__, result);
372 mutex_unlock(&dec->usb_mutex);
373 kfree(b);
374 return result;
375 } else {
376 if (debug) {
377 printk(KERN_DEBUG "%s: result: %*ph\n",
378 __func__, actual_len, b);
381 if (result_length)
382 *result_length = b[3];
383 if (cmd_result && b[3] > 0)
384 memcpy(cmd_result, &b[4], b[3]);
386 mutex_unlock(&dec->usb_mutex);
388 kfree(b);
389 return 0;
393 static int ttusb_dec_get_stb_state (struct ttusb_dec *dec, unsigned int *mode,
394 unsigned int *model, unsigned int *version)
396 u8 c[COMMAND_PACKET_SIZE];
397 int c_length;
398 int result;
399 __be32 tmp;
401 dprintk("%s\n", __func__);
403 result = ttusb_dec_send_command(dec, 0x08, 0, NULL, &c_length, c);
404 if (result)
405 return result;
407 if (c_length >= 0x0c) {
408 if (mode != NULL) {
409 memcpy(&tmp, c, 4);
410 *mode = ntohl(tmp);
412 if (model != NULL) {
413 memcpy(&tmp, &c[4], 4);
414 *model = ntohl(tmp);
416 if (version != NULL) {
417 memcpy(&tmp, &c[8], 4);
418 *version = ntohl(tmp);
420 return 0;
421 } else {
422 return -ENOENT;
426 static int ttusb_dec_audio_pes2ts_cb(void *priv, unsigned char *data)
428 struct ttusb_dec *dec = priv;
430 dec->audio_filter->feed->cb.ts(data, 188, NULL, 0,
431 &dec->audio_filter->feed->feed.ts);
433 return 0;
436 static int ttusb_dec_video_pes2ts_cb(void *priv, unsigned char *data)
438 struct ttusb_dec *dec = priv;
440 dec->video_filter->feed->cb.ts(data, 188, NULL, 0,
441 &dec->video_filter->feed->feed.ts);
443 return 0;
446 static void ttusb_dec_set_pids(struct ttusb_dec *dec)
448 u8 b[] = { 0x00, 0x00, 0x00, 0x00,
449 0x00, 0x00, 0xff, 0xff,
450 0xff, 0xff, 0xff, 0xff };
452 __be16 pcr = htons(dec->pid[DMX_PES_PCR]);
453 __be16 audio = htons(dec->pid[DMX_PES_AUDIO]);
454 __be16 video = htons(dec->pid[DMX_PES_VIDEO]);
456 dprintk("%s\n", __func__);
458 memcpy(&b[0], &pcr, 2);
459 memcpy(&b[2], &audio, 2);
460 memcpy(&b[4], &video, 2);
462 ttusb_dec_send_command(dec, 0x50, sizeof(b), b, NULL, NULL);
464 dvb_filter_pes2ts_init(&dec->a_pes2ts, dec->pid[DMX_PES_AUDIO],
465 ttusb_dec_audio_pes2ts_cb, dec);
466 dvb_filter_pes2ts_init(&dec->v_pes2ts, dec->pid[DMX_PES_VIDEO],
467 ttusb_dec_video_pes2ts_cb, dec);
468 dec->v_pes_length = 0;
469 dec->v_pes_postbytes = 0;
472 static void ttusb_dec_process_pva(struct ttusb_dec *dec, u8 *pva, int length)
474 if (length < 8) {
475 printk("%s: packet too short - discarding\n", __func__);
476 return;
479 if (length > 8 + MAX_PVA_LENGTH) {
480 printk("%s: packet too long - discarding\n", __func__);
481 return;
484 switch (pva[2]) {
486 case 0x01: { /* VideoStream */
487 int prebytes = pva[5] & 0x03;
488 int postbytes = (pva[5] & 0x0c) >> 2;
489 __be16 v_pes_payload_length;
491 if (output_pva) {
492 dec->video_filter->feed->cb.ts(pva, length, NULL, 0,
493 &dec->video_filter->feed->feed.ts);
494 return;
497 if (dec->v_pes_postbytes > 0 &&
498 dec->v_pes_postbytes == prebytes) {
499 memcpy(&dec->v_pes[dec->v_pes_length],
500 &pva[12], prebytes);
502 dvb_filter_pes2ts(&dec->v_pes2ts, dec->v_pes,
503 dec->v_pes_length + prebytes, 1);
506 if (pva[5] & 0x10) {
507 dec->v_pes[7] = 0x80;
508 dec->v_pes[8] = 0x05;
510 dec->v_pes[9] = 0x21 | ((pva[8] & 0xc0) >> 5);
511 dec->v_pes[10] = ((pva[8] & 0x3f) << 2) |
512 ((pva[9] & 0xc0) >> 6);
513 dec->v_pes[11] = 0x01 |
514 ((pva[9] & 0x3f) << 2) |
515 ((pva[10] & 0x80) >> 6);
516 dec->v_pes[12] = ((pva[10] & 0x7f) << 1) |
517 ((pva[11] & 0xc0) >> 7);
518 dec->v_pes[13] = 0x01 | ((pva[11] & 0x7f) << 1);
520 memcpy(&dec->v_pes[14], &pva[12 + prebytes],
521 length - 12 - prebytes);
522 dec->v_pes_length = 14 + length - 12 - prebytes;
523 } else {
524 dec->v_pes[7] = 0x00;
525 dec->v_pes[8] = 0x00;
527 memcpy(&dec->v_pes[9], &pva[8], length - 8);
528 dec->v_pes_length = 9 + length - 8;
531 dec->v_pes_postbytes = postbytes;
533 if (dec->v_pes[9 + dec->v_pes[8]] == 0x00 &&
534 dec->v_pes[10 + dec->v_pes[8]] == 0x00 &&
535 dec->v_pes[11 + dec->v_pes[8]] == 0x01)
536 dec->v_pes[6] = 0x84;
537 else
538 dec->v_pes[6] = 0x80;
540 v_pes_payload_length = htons(dec->v_pes_length - 6 +
541 postbytes);
542 memcpy(&dec->v_pes[4], &v_pes_payload_length, 2);
544 if (postbytes == 0)
545 dvb_filter_pes2ts(&dec->v_pes2ts, dec->v_pes,
546 dec->v_pes_length, 1);
548 break;
551 case 0x02: /* MainAudioStream */
552 if (output_pva) {
553 dec->audio_filter->feed->cb.ts(pva, length, NULL, 0,
554 &dec->audio_filter->feed->feed.ts);
555 return;
558 dvb_filter_pes2ts(&dec->a_pes2ts, &pva[8], length - 8,
559 pva[5] & 0x10);
560 break;
562 default:
563 printk("%s: unknown PVA type: %02x.\n", __func__,
564 pva[2]);
565 break;
569 static void ttusb_dec_process_filter(struct ttusb_dec *dec, u8 *packet,
570 int length)
572 struct list_head *item;
573 struct filter_info *finfo;
574 struct dvb_demux_filter *filter = NULL;
575 unsigned long flags;
576 u8 sid;
578 sid = packet[1];
579 spin_lock_irqsave(&dec->filter_info_list_lock, flags);
580 for (item = dec->filter_info_list.next; item != &dec->filter_info_list;
581 item = item->next) {
582 finfo = list_entry(item, struct filter_info, filter_info_list);
583 if (finfo->stream_id == sid) {
584 filter = finfo->filter;
585 break;
588 spin_unlock_irqrestore(&dec->filter_info_list_lock, flags);
590 if (filter)
591 filter->feed->cb.sec(&packet[2], length - 2, NULL, 0,
592 &filter->filter);
595 static void ttusb_dec_process_packet(struct ttusb_dec *dec)
597 int i;
598 u16 csum = 0;
599 u16 packet_id;
601 if (dec->packet_length % 2) {
602 printk("%s: odd sized packet - discarding\n", __func__);
603 return;
606 for (i = 0; i < dec->packet_length; i += 2)
607 csum ^= ((dec->packet[i] << 8) + dec->packet[i + 1]);
609 if (csum) {
610 printk("%s: checksum failed - discarding\n", __func__);
611 return;
614 packet_id = dec->packet[dec->packet_length - 4] << 8;
615 packet_id += dec->packet[dec->packet_length - 3];
617 if ((packet_id != dec->next_packet_id) && dec->next_packet_id) {
618 printk("%s: warning: lost packets between %u and %u\n",
619 __func__, dec->next_packet_id - 1, packet_id);
622 if (packet_id == 0xffff)
623 dec->next_packet_id = 0x8000;
624 else
625 dec->next_packet_id = packet_id + 1;
627 switch (dec->packet_type) {
628 case TTUSB_DEC_PACKET_PVA:
629 if (dec->pva_stream_count)
630 ttusb_dec_process_pva(dec, dec->packet,
631 dec->packet_payload_length);
632 break;
634 case TTUSB_DEC_PACKET_SECTION:
635 if (dec->filter_stream_count)
636 ttusb_dec_process_filter(dec, dec->packet,
637 dec->packet_payload_length);
638 break;
640 case TTUSB_DEC_PACKET_EMPTY:
641 break;
645 static void swap_bytes(u8 *b, int length)
647 length -= length % 2;
648 for (; length; b += 2, length -= 2)
649 swap(*b, *(b + 1));
652 static void ttusb_dec_process_urb_frame(struct ttusb_dec *dec, u8 *b,
653 int length)
655 swap_bytes(b, length);
657 while (length) {
658 switch (dec->packet_state) {
660 case 0:
661 case 1:
662 case 2:
663 if (*b++ == 0xaa)
664 dec->packet_state++;
665 else
666 dec->packet_state = 0;
668 length--;
669 break;
671 case 3:
672 if (*b == 0x00) {
673 dec->packet_state++;
674 dec->packet_length = 0;
675 } else if (*b != 0xaa) {
676 dec->packet_state = 0;
679 b++;
680 length--;
681 break;
683 case 4:
684 dec->packet[dec->packet_length++] = *b++;
686 if (dec->packet_length == 2) {
687 if (dec->packet[0] == 'A' &&
688 dec->packet[1] == 'V') {
689 dec->packet_type =
690 TTUSB_DEC_PACKET_PVA;
691 dec->packet_state++;
692 } else if (dec->packet[0] == 'S') {
693 dec->packet_type =
694 TTUSB_DEC_PACKET_SECTION;
695 dec->packet_state++;
696 } else if (dec->packet[0] == 0x00) {
697 dec->packet_type =
698 TTUSB_DEC_PACKET_EMPTY;
699 dec->packet_payload_length = 2;
700 dec->packet_state = 7;
701 } else {
702 printk("%s: unknown packet type: %02x%02x\n",
703 __func__,
704 dec->packet[0], dec->packet[1]);
705 dec->packet_state = 0;
709 length--;
710 break;
712 case 5:
713 dec->packet[dec->packet_length++] = *b++;
715 if (dec->packet_type == TTUSB_DEC_PACKET_PVA &&
716 dec->packet_length == 8) {
717 dec->packet_state++;
718 dec->packet_payload_length = 8 +
719 (dec->packet[6] << 8) +
720 dec->packet[7];
721 } else if (dec->packet_type ==
722 TTUSB_DEC_PACKET_SECTION &&
723 dec->packet_length == 5) {
724 dec->packet_state++;
725 dec->packet_payload_length = 5 +
726 ((dec->packet[3] & 0x0f) << 8) +
727 dec->packet[4];
730 length--;
731 break;
733 case 6: {
734 int remainder = dec->packet_payload_length -
735 dec->packet_length;
737 if (length >= remainder) {
738 memcpy(dec->packet + dec->packet_length,
739 b, remainder);
740 dec->packet_length += remainder;
741 b += remainder;
742 length -= remainder;
743 dec->packet_state++;
744 } else {
745 memcpy(&dec->packet[dec->packet_length],
746 b, length);
747 dec->packet_length += length;
748 length = 0;
751 break;
754 case 7: {
755 int tail = 4;
757 dec->packet[dec->packet_length++] = *b++;
759 if (dec->packet_type == TTUSB_DEC_PACKET_SECTION &&
760 dec->packet_payload_length % 2)
761 tail++;
763 if (dec->packet_length ==
764 dec->packet_payload_length + tail) {
765 ttusb_dec_process_packet(dec);
766 dec->packet_state = 0;
769 length--;
770 break;
773 default:
774 printk("%s: illegal packet state encountered.\n",
775 __func__);
776 dec->packet_state = 0;
781 static void ttusb_dec_process_urb_frame_list(unsigned long data)
783 struct ttusb_dec *dec = (struct ttusb_dec *)data;
784 struct list_head *item;
785 struct urb_frame *frame;
786 unsigned long flags;
788 while (1) {
789 spin_lock_irqsave(&dec->urb_frame_list_lock, flags);
790 if ((item = dec->urb_frame_list.next) != &dec->urb_frame_list) {
791 frame = list_entry(item, struct urb_frame,
792 urb_frame_list);
793 list_del(&frame->urb_frame_list);
794 } else {
795 spin_unlock_irqrestore(&dec->urb_frame_list_lock,
796 flags);
797 return;
799 spin_unlock_irqrestore(&dec->urb_frame_list_lock, flags);
801 ttusb_dec_process_urb_frame(dec, frame->data, frame->length);
802 kfree(frame);
806 static void ttusb_dec_process_urb(struct urb *urb)
808 struct ttusb_dec *dec = urb->context;
810 if (!urb->status) {
811 int i;
813 for (i = 0; i < FRAMES_PER_ISO_BUF; i++) {
814 struct usb_iso_packet_descriptor *d;
815 u8 *b;
816 int length;
817 struct urb_frame *frame;
819 d = &urb->iso_frame_desc[i];
820 b = urb->transfer_buffer + d->offset;
821 length = d->actual_length;
823 if ((frame = kmalloc(sizeof(struct urb_frame),
824 GFP_ATOMIC))) {
825 unsigned long flags;
827 memcpy(frame->data, b, length);
828 frame->length = length;
830 spin_lock_irqsave(&dec->urb_frame_list_lock,
831 flags);
832 list_add_tail(&frame->urb_frame_list,
833 &dec->urb_frame_list);
834 spin_unlock_irqrestore(&dec->urb_frame_list_lock,
835 flags);
837 tasklet_schedule(&dec->urb_tasklet);
840 } else {
841 /* -ENOENT is expected when unlinking urbs */
842 if (urb->status != -ENOENT)
843 dprintk("%s: urb error: %d\n", __func__,
844 urb->status);
847 if (dec->iso_stream_count)
848 usb_submit_urb(urb, GFP_ATOMIC);
851 static void ttusb_dec_setup_urbs(struct ttusb_dec *dec)
853 int i, j, buffer_offset = 0;
855 dprintk("%s\n", __func__);
857 for (i = 0; i < ISO_BUF_COUNT; i++) {
858 int frame_offset = 0;
859 struct urb *urb = dec->iso_urb[i];
861 urb->dev = dec->udev;
862 urb->context = dec;
863 urb->complete = ttusb_dec_process_urb;
864 urb->pipe = dec->in_pipe;
865 urb->transfer_flags = URB_ISO_ASAP;
866 urb->interval = 1;
867 urb->number_of_packets = FRAMES_PER_ISO_BUF;
868 urb->transfer_buffer_length = ISO_FRAME_SIZE *
869 FRAMES_PER_ISO_BUF;
870 urb->transfer_buffer = dec->iso_buffer + buffer_offset;
871 buffer_offset += ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF;
873 for (j = 0; j < FRAMES_PER_ISO_BUF; j++) {
874 urb->iso_frame_desc[j].offset = frame_offset;
875 urb->iso_frame_desc[j].length = ISO_FRAME_SIZE;
876 frame_offset += ISO_FRAME_SIZE;
881 static void ttusb_dec_stop_iso_xfer(struct ttusb_dec *dec)
883 int i;
885 dprintk("%s\n", __func__);
887 if (mutex_lock_interruptible(&dec->iso_mutex))
888 return;
890 dec->iso_stream_count--;
892 if (!dec->iso_stream_count) {
893 for (i = 0; i < ISO_BUF_COUNT; i++)
894 usb_kill_urb(dec->iso_urb[i]);
897 mutex_unlock(&dec->iso_mutex);
900 /* Setting the interface of the DEC tends to take down the USB communications
901 * for a short period, so it's important not to call this function just before
902 * trying to talk to it.
904 static int ttusb_dec_set_interface(struct ttusb_dec *dec,
905 enum ttusb_dec_interface interface)
907 int result = 0;
908 u8 b[] = { 0x05 };
910 if (interface != dec->interface) {
911 switch (interface) {
912 case TTUSB_DEC_INTERFACE_INITIAL:
913 result = usb_set_interface(dec->udev, 0, 0);
914 break;
915 case TTUSB_DEC_INTERFACE_IN:
916 result = ttusb_dec_send_command(dec, 0x80, sizeof(b),
917 b, NULL, NULL);
918 if (result)
919 return result;
920 result = usb_set_interface(dec->udev, 0, 8);
921 break;
922 case TTUSB_DEC_INTERFACE_OUT:
923 result = usb_set_interface(dec->udev, 0, 1);
924 break;
927 if (result)
928 return result;
930 dec->interface = interface;
933 return 0;
936 static int ttusb_dec_start_iso_xfer(struct ttusb_dec *dec)
938 int i, result;
940 dprintk("%s\n", __func__);
942 if (mutex_lock_interruptible(&dec->iso_mutex))
943 return -EAGAIN;
945 if (!dec->iso_stream_count) {
946 ttusb_dec_setup_urbs(dec);
948 dec->packet_state = 0;
949 dec->v_pes_postbytes = 0;
950 dec->next_packet_id = 0;
952 for (i = 0; i < ISO_BUF_COUNT; i++) {
953 if ((result = usb_submit_urb(dec->iso_urb[i],
954 GFP_ATOMIC))) {
955 printk("%s: failed urb submission %d: error %d\n",
956 __func__, i, result);
958 while (i) {
959 usb_kill_urb(dec->iso_urb[i - 1]);
960 i--;
963 mutex_unlock(&dec->iso_mutex);
964 return result;
969 dec->iso_stream_count++;
971 mutex_unlock(&dec->iso_mutex);
973 return 0;
976 static int ttusb_dec_start_ts_feed(struct dvb_demux_feed *dvbdmxfeed)
978 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
979 struct ttusb_dec *dec = dvbdmx->priv;
980 u8 b0[] = { 0x05 };
981 int result = 0;
983 dprintk("%s\n", __func__);
985 dprintk(" ts_type:");
987 if (dvbdmxfeed->ts_type & TS_DECODER)
988 dprintk(" TS_DECODER");
990 if (dvbdmxfeed->ts_type & TS_PACKET)
991 dprintk(" TS_PACKET");
993 if (dvbdmxfeed->ts_type & TS_PAYLOAD_ONLY)
994 dprintk(" TS_PAYLOAD_ONLY");
996 dprintk("\n");
998 switch (dvbdmxfeed->pes_type) {
1000 case DMX_PES_VIDEO:
1001 dprintk(" pes_type: DMX_PES_VIDEO\n");
1002 dec->pid[DMX_PES_PCR] = dvbdmxfeed->pid;
1003 dec->pid[DMX_PES_VIDEO] = dvbdmxfeed->pid;
1004 dec->video_filter = dvbdmxfeed->filter;
1005 ttusb_dec_set_pids(dec);
1006 break;
1008 case DMX_PES_AUDIO:
1009 dprintk(" pes_type: DMX_PES_AUDIO\n");
1010 dec->pid[DMX_PES_AUDIO] = dvbdmxfeed->pid;
1011 dec->audio_filter = dvbdmxfeed->filter;
1012 ttusb_dec_set_pids(dec);
1013 break;
1015 case DMX_PES_TELETEXT:
1016 dec->pid[DMX_PES_TELETEXT] = dvbdmxfeed->pid;
1017 dprintk(" pes_type: DMX_PES_TELETEXT(not supported)\n");
1018 return -ENOSYS;
1020 case DMX_PES_PCR:
1021 dprintk(" pes_type: DMX_PES_PCR\n");
1022 dec->pid[DMX_PES_PCR] = dvbdmxfeed->pid;
1023 ttusb_dec_set_pids(dec);
1024 break;
1026 case DMX_PES_OTHER:
1027 dprintk(" pes_type: DMX_PES_OTHER(not supported)\n");
1028 return -ENOSYS;
1030 default:
1031 dprintk(" pes_type: unknown (%d)\n", dvbdmxfeed->pes_type);
1032 return -EINVAL;
1036 result = ttusb_dec_send_command(dec, 0x80, sizeof(b0), b0, NULL, NULL);
1037 if (result)
1038 return result;
1040 dec->pva_stream_count++;
1041 return ttusb_dec_start_iso_xfer(dec);
1044 static int ttusb_dec_start_sec_feed(struct dvb_demux_feed *dvbdmxfeed)
1046 struct ttusb_dec *dec = dvbdmxfeed->demux->priv;
1047 u8 b0[] = { 0x00, 0x00, 0x00, 0x01,
1048 0x00, 0x00, 0x00, 0x00,
1049 0x00, 0x00, 0x00, 0x00,
1050 0x00, 0x00, 0x00, 0x00,
1051 0x00, 0xff, 0x00, 0x00,
1052 0x00, 0x00, 0x00, 0x00,
1053 0x00, 0x00, 0x00, 0x00,
1054 0x00 };
1055 __be16 pid;
1056 u8 c[COMMAND_PACKET_SIZE];
1057 int c_length;
1058 int result;
1059 struct filter_info *finfo;
1060 unsigned long flags;
1061 u8 x = 1;
1063 dprintk("%s\n", __func__);
1065 pid = htons(dvbdmxfeed->pid);
1066 memcpy(&b0[0], &pid, 2);
1067 memcpy(&b0[4], &x, 1);
1068 memcpy(&b0[5], &dvbdmxfeed->filter->filter.filter_value[0], 1);
1070 result = ttusb_dec_send_command(dec, 0x60, sizeof(b0), b0,
1071 &c_length, c);
1073 if (!result) {
1074 if (c_length == 2) {
1075 if (!(finfo = kmalloc(sizeof(struct filter_info),
1076 GFP_ATOMIC)))
1077 return -ENOMEM;
1079 finfo->stream_id = c[1];
1080 finfo->filter = dvbdmxfeed->filter;
1082 spin_lock_irqsave(&dec->filter_info_list_lock, flags);
1083 list_add_tail(&finfo->filter_info_list,
1084 &dec->filter_info_list);
1085 spin_unlock_irqrestore(&dec->filter_info_list_lock,
1086 flags);
1088 dvbdmxfeed->priv = finfo;
1090 dec->filter_stream_count++;
1091 return ttusb_dec_start_iso_xfer(dec);
1094 return -EAGAIN;
1095 } else
1096 return result;
1099 static int ttusb_dec_start_feed(struct dvb_demux_feed *dvbdmxfeed)
1101 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
1103 dprintk("%s\n", __func__);
1105 if (!dvbdmx->dmx.frontend)
1106 return -EINVAL;
1108 dprintk(" pid: 0x%04X\n", dvbdmxfeed->pid);
1110 switch (dvbdmxfeed->type) {
1112 case DMX_TYPE_TS:
1113 return ttusb_dec_start_ts_feed(dvbdmxfeed);
1114 break;
1116 case DMX_TYPE_SEC:
1117 return ttusb_dec_start_sec_feed(dvbdmxfeed);
1118 break;
1120 default:
1121 dprintk(" type: unknown (%d)\n", dvbdmxfeed->type);
1122 return -EINVAL;
1127 static int ttusb_dec_stop_ts_feed(struct dvb_demux_feed *dvbdmxfeed)
1129 struct ttusb_dec *dec = dvbdmxfeed->demux->priv;
1130 u8 b0[] = { 0x00 };
1132 ttusb_dec_send_command(dec, 0x81, sizeof(b0), b0, NULL, NULL);
1134 dec->pva_stream_count--;
1136 ttusb_dec_stop_iso_xfer(dec);
1138 return 0;
1141 static int ttusb_dec_stop_sec_feed(struct dvb_demux_feed *dvbdmxfeed)
1143 struct ttusb_dec *dec = dvbdmxfeed->demux->priv;
1144 u8 b0[] = { 0x00, 0x00 };
1145 struct filter_info *finfo = (struct filter_info *)dvbdmxfeed->priv;
1146 unsigned long flags;
1148 b0[1] = finfo->stream_id;
1149 spin_lock_irqsave(&dec->filter_info_list_lock, flags);
1150 list_del(&finfo->filter_info_list);
1151 spin_unlock_irqrestore(&dec->filter_info_list_lock, flags);
1152 kfree(finfo);
1153 ttusb_dec_send_command(dec, 0x62, sizeof(b0), b0, NULL, NULL);
1155 dec->filter_stream_count--;
1157 ttusb_dec_stop_iso_xfer(dec);
1159 return 0;
1162 static int ttusb_dec_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
1164 dprintk("%s\n", __func__);
1166 switch (dvbdmxfeed->type) {
1167 case DMX_TYPE_TS:
1168 return ttusb_dec_stop_ts_feed(dvbdmxfeed);
1169 break;
1171 case DMX_TYPE_SEC:
1172 return ttusb_dec_stop_sec_feed(dvbdmxfeed);
1173 break;
1176 return 0;
1179 static void ttusb_dec_free_iso_urbs(struct ttusb_dec *dec)
1181 int i;
1183 dprintk("%s\n", __func__);
1185 for (i = 0; i < ISO_BUF_COUNT; i++)
1186 usb_free_urb(dec->iso_urb[i]);
1187 kfree(dec->iso_buffer);
1190 static int ttusb_dec_alloc_iso_urbs(struct ttusb_dec *dec)
1192 int i;
1194 dprintk("%s\n", __func__);
1196 dec->iso_buffer = kcalloc(FRAMES_PER_ISO_BUF * ISO_BUF_COUNT,
1197 ISO_FRAME_SIZE, GFP_KERNEL);
1198 if (!dec->iso_buffer)
1199 return -ENOMEM;
1201 for (i = 0; i < ISO_BUF_COUNT; i++) {
1202 struct urb *urb;
1204 if (!(urb = usb_alloc_urb(FRAMES_PER_ISO_BUF, GFP_ATOMIC))) {
1205 ttusb_dec_free_iso_urbs(dec);
1206 return -ENOMEM;
1209 dec->iso_urb[i] = urb;
1212 ttusb_dec_setup_urbs(dec);
1214 return 0;
1217 static void ttusb_dec_init_tasklet(struct ttusb_dec *dec)
1219 spin_lock_init(&dec->urb_frame_list_lock);
1220 INIT_LIST_HEAD(&dec->urb_frame_list);
1221 tasklet_init(&dec->urb_tasklet, ttusb_dec_process_urb_frame_list,
1222 (unsigned long)dec);
1225 static int ttusb_init_rc( struct ttusb_dec *dec)
1227 struct input_dev *input_dev;
1228 u8 b[] = { 0x00, 0x01 };
1229 int i;
1230 int err;
1232 usb_make_path(dec->udev, dec->rc_phys, sizeof(dec->rc_phys));
1233 strlcat(dec->rc_phys, "/input0", sizeof(dec->rc_phys));
1235 input_dev = input_allocate_device();
1236 if (!input_dev)
1237 return -ENOMEM;
1239 input_dev->name = "ttusb_dec remote control";
1240 input_dev->phys = dec->rc_phys;
1241 input_dev->evbit[0] = BIT_MASK(EV_KEY);
1242 input_dev->keycodesize = sizeof(u16);
1243 input_dev->keycodemax = 0x1a;
1244 input_dev->keycode = rc_keys;
1246 for (i = 0; i < ARRAY_SIZE(rc_keys); i++)
1247 set_bit(rc_keys[i], input_dev->keybit);
1249 err = input_register_device(input_dev);
1250 if (err) {
1251 input_free_device(input_dev);
1252 return err;
1255 dec->rc_input_dev = input_dev;
1256 if (usb_submit_urb(dec->irq_urb, GFP_KERNEL))
1257 printk("%s: usb_submit_urb failed\n",__func__);
1258 /* enable irq pipe */
1259 ttusb_dec_send_command(dec,0xb0,sizeof(b),b,NULL,NULL);
1261 return 0;
1264 static void ttusb_dec_init_v_pes(struct ttusb_dec *dec)
1266 dprintk("%s\n", __func__);
1268 dec->v_pes[0] = 0x00;
1269 dec->v_pes[1] = 0x00;
1270 dec->v_pes[2] = 0x01;
1271 dec->v_pes[3] = 0xe0;
1274 static int ttusb_dec_init_usb(struct ttusb_dec *dec)
1276 int result;
1278 dprintk("%s\n", __func__);
1280 mutex_init(&dec->usb_mutex);
1281 mutex_init(&dec->iso_mutex);
1283 dec->command_pipe = usb_sndbulkpipe(dec->udev, COMMAND_PIPE);
1284 dec->result_pipe = usb_rcvbulkpipe(dec->udev, RESULT_PIPE);
1285 dec->in_pipe = usb_rcvisocpipe(dec->udev, IN_PIPE);
1286 dec->out_pipe = usb_sndisocpipe(dec->udev, OUT_PIPE);
1287 dec->irq_pipe = usb_rcvintpipe(dec->udev, IRQ_PIPE);
1289 if(enable_rc) {
1290 dec->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
1291 if(!dec->irq_urb) {
1292 return -ENOMEM;
1294 dec->irq_buffer = usb_alloc_coherent(dec->udev,IRQ_PACKET_SIZE,
1295 GFP_KERNEL, &dec->irq_dma_handle);
1296 if(!dec->irq_buffer) {
1297 usb_free_urb(dec->irq_urb);
1298 return -ENOMEM;
1300 usb_fill_int_urb(dec->irq_urb, dec->udev,dec->irq_pipe,
1301 dec->irq_buffer, IRQ_PACKET_SIZE,
1302 ttusb_dec_handle_irq, dec, 1);
1303 dec->irq_urb->transfer_dma = dec->irq_dma_handle;
1304 dec->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1307 result = ttusb_dec_alloc_iso_urbs(dec);
1308 if (result) {
1309 usb_free_urb(dec->irq_urb);
1310 usb_free_coherent(dec->udev, IRQ_PACKET_SIZE,
1311 dec->irq_buffer, dec->irq_dma_handle);
1313 return result;
1316 static int ttusb_dec_boot_dsp(struct ttusb_dec *dec)
1318 int i, j, actual_len, result, size, trans_count;
1319 u8 b0[] = { 0x00, 0x00, 0x00, 0x00,
1320 0x00, 0x00, 0x00, 0x00,
1321 0x61, 0x00 };
1322 u8 b1[] = { 0x61 };
1323 u8 *b;
1324 char idstring[21];
1325 const u8 *firmware = NULL;
1326 size_t firmware_size = 0;
1327 u16 firmware_csum = 0;
1328 __be16 firmware_csum_ns;
1329 __be32 firmware_size_nl;
1330 u32 crc32_csum, crc32_check;
1331 __be32 tmp;
1332 const struct firmware *fw_entry = NULL;
1334 dprintk("%s\n", __func__);
1336 result = request_firmware(&fw_entry, dec->firmware_name, &dec->udev->dev);
1337 if (result) {
1338 printk(KERN_ERR "%s: Firmware (%s) unavailable.\n",
1339 __func__, dec->firmware_name);
1340 return result;
1343 firmware = fw_entry->data;
1344 firmware_size = fw_entry->size;
1346 if (firmware_size < 60) {
1347 printk("%s: firmware size too small for DSP code (%zu < 60).\n",
1348 __func__, firmware_size);
1349 release_firmware(fw_entry);
1350 return -ENOENT;
1353 /* a 32 bit checksum over the first 56 bytes of the DSP Code is stored
1354 at offset 56 of file, so use it to check if the firmware file is
1355 valid. */
1356 crc32_csum = crc32(~0L, firmware, 56) ^ ~0L;
1357 memcpy(&tmp, &firmware[56], 4);
1358 crc32_check = ntohl(tmp);
1359 if (crc32_csum != crc32_check) {
1360 printk("%s: crc32 check of DSP code failed (calculated 0x%08x != 0x%08x in file), file invalid.\n",
1361 __func__, crc32_csum, crc32_check);
1362 release_firmware(fw_entry);
1363 return -ENOENT;
1365 memcpy(idstring, &firmware[36], 20);
1366 idstring[20] = '\0';
1367 printk(KERN_INFO "ttusb_dec: found DSP code \"%s\".\n", idstring);
1369 firmware_size_nl = htonl(firmware_size);
1370 memcpy(b0, &firmware_size_nl, 4);
1371 firmware_csum = crc16(~0, firmware, firmware_size) ^ ~0;
1372 firmware_csum_ns = htons(firmware_csum);
1373 memcpy(&b0[6], &firmware_csum_ns, 2);
1375 result = ttusb_dec_send_command(dec, 0x41, sizeof(b0), b0, NULL, NULL);
1377 if (result) {
1378 release_firmware(fw_entry);
1379 return result;
1382 trans_count = 0;
1383 j = 0;
1385 b = kmalloc(ARM_PACKET_SIZE, GFP_KERNEL);
1386 if (b == NULL) {
1387 release_firmware(fw_entry);
1388 return -ENOMEM;
1391 for (i = 0; i < firmware_size; i += COMMAND_PACKET_SIZE) {
1392 size = firmware_size - i;
1393 if (size > COMMAND_PACKET_SIZE)
1394 size = COMMAND_PACKET_SIZE;
1396 b[j + 0] = 0xaa;
1397 b[j + 1] = trans_count++;
1398 b[j + 2] = 0xf0;
1399 b[j + 3] = size;
1400 memcpy(&b[j + 4], &firmware[i], size);
1402 j += COMMAND_PACKET_SIZE + 4;
1404 if (j >= ARM_PACKET_SIZE) {
1405 result = usb_bulk_msg(dec->udev, dec->command_pipe, b,
1406 ARM_PACKET_SIZE, &actual_len,
1407 100);
1408 j = 0;
1409 } else if (size < COMMAND_PACKET_SIZE) {
1410 result = usb_bulk_msg(dec->udev, dec->command_pipe, b,
1411 j - COMMAND_PACKET_SIZE + size,
1412 &actual_len, 100);
1416 result = ttusb_dec_send_command(dec, 0x43, sizeof(b1), b1, NULL, NULL);
1418 release_firmware(fw_entry);
1419 kfree(b);
1421 return result;
1424 static int ttusb_dec_init_stb(struct ttusb_dec *dec)
1426 int result;
1427 unsigned int mode = 0, model = 0, version = 0;
1429 dprintk("%s\n", __func__);
1431 result = ttusb_dec_get_stb_state(dec, &mode, &model, &version);
1432 if (result)
1433 return result;
1435 if (!mode) {
1436 if (version == 0xABCDEFAB)
1437 printk(KERN_INFO "ttusb_dec: no version info in Firmware\n");
1438 else
1439 printk(KERN_INFO "ttusb_dec: Firmware %x.%02x%c%c\n",
1440 version >> 24, (version >> 16) & 0xff,
1441 (version >> 8) & 0xff, version & 0xff);
1443 result = ttusb_dec_boot_dsp(dec);
1444 if (result)
1445 return result;
1446 } else {
1447 /* We can't trust the USB IDs that some firmwares
1448 give the box */
1449 switch (model) {
1450 case 0x00070001:
1451 case 0x00070008:
1452 case 0x0007000c:
1453 ttusb_dec_set_model(dec, TTUSB_DEC3000S);
1454 break;
1455 case 0x00070009:
1456 case 0x00070013:
1457 ttusb_dec_set_model(dec, TTUSB_DEC2000T);
1458 break;
1459 case 0x00070011:
1460 ttusb_dec_set_model(dec, TTUSB_DEC2540T);
1461 break;
1462 default:
1463 printk(KERN_ERR "%s: unknown model returned by firmware (%08x) - please report\n",
1464 __func__, model);
1465 return -ENOENT;
1467 if (version >= 0x01770000)
1468 dec->can_playback = 1;
1470 return 0;
1473 static int ttusb_dec_init_dvb(struct ttusb_dec *dec)
1475 int result;
1477 dprintk("%s\n", __func__);
1479 if ((result = dvb_register_adapter(&dec->adapter,
1480 dec->model_name, THIS_MODULE,
1481 &dec->udev->dev,
1482 adapter_nr)) < 0) {
1483 printk("%s: dvb_register_adapter failed: error %d\n",
1484 __func__, result);
1486 return result;
1489 dec->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING;
1491 dec->demux.priv = (void *)dec;
1492 dec->demux.filternum = 31;
1493 dec->demux.feednum = 31;
1494 dec->demux.start_feed = ttusb_dec_start_feed;
1495 dec->demux.stop_feed = ttusb_dec_stop_feed;
1496 dec->demux.write_to_decoder = NULL;
1498 if ((result = dvb_dmx_init(&dec->demux)) < 0) {
1499 printk("%s: dvb_dmx_init failed: error %d\n", __func__,
1500 result);
1502 dvb_unregister_adapter(&dec->adapter);
1504 return result;
1507 dec->dmxdev.filternum = 32;
1508 dec->dmxdev.demux = &dec->demux.dmx;
1509 dec->dmxdev.capabilities = 0;
1511 if ((result = dvb_dmxdev_init(&dec->dmxdev, &dec->adapter)) < 0) {
1512 printk("%s: dvb_dmxdev_init failed: error %d\n",
1513 __func__, result);
1515 dvb_dmx_release(&dec->demux);
1516 dvb_unregister_adapter(&dec->adapter);
1518 return result;
1521 dec->frontend.source = DMX_FRONTEND_0;
1523 if ((result = dec->demux.dmx.add_frontend(&dec->demux.dmx,
1524 &dec->frontend)) < 0) {
1525 printk("%s: dvb_dmx_init failed: error %d\n", __func__,
1526 result);
1528 dvb_dmxdev_release(&dec->dmxdev);
1529 dvb_dmx_release(&dec->demux);
1530 dvb_unregister_adapter(&dec->adapter);
1532 return result;
1535 if ((result = dec->demux.dmx.connect_frontend(&dec->demux.dmx,
1536 &dec->frontend)) < 0) {
1537 printk("%s: dvb_dmx_init failed: error %d\n", __func__,
1538 result);
1540 dec->demux.dmx.remove_frontend(&dec->demux.dmx, &dec->frontend);
1541 dvb_dmxdev_release(&dec->dmxdev);
1542 dvb_dmx_release(&dec->demux);
1543 dvb_unregister_adapter(&dec->adapter);
1545 return result;
1548 dvb_net_init(&dec->adapter, &dec->dvb_net, &dec->demux.dmx);
1550 return 0;
1553 static void ttusb_dec_exit_dvb(struct ttusb_dec *dec)
1555 dprintk("%s\n", __func__);
1557 dvb_net_release(&dec->dvb_net);
1558 dec->demux.dmx.close(&dec->demux.dmx);
1559 dec->demux.dmx.remove_frontend(&dec->demux.dmx, &dec->frontend);
1560 dvb_dmxdev_release(&dec->dmxdev);
1561 dvb_dmx_release(&dec->demux);
1562 if (dec->fe) {
1563 dvb_unregister_frontend(dec->fe);
1564 if (dec->fe->ops.release)
1565 dec->fe->ops.release(dec->fe);
1567 dvb_unregister_adapter(&dec->adapter);
1570 static void ttusb_dec_exit_rc(struct ttusb_dec *dec)
1572 dprintk("%s\n", __func__);
1574 if (dec->rc_input_dev) {
1575 input_unregister_device(dec->rc_input_dev);
1576 dec->rc_input_dev = NULL;
1581 static void ttusb_dec_exit_usb(struct ttusb_dec *dec)
1583 int i;
1585 dprintk("%s\n", __func__);
1587 if (enable_rc) {
1588 /* we have to check whether the irq URB is already submitted.
1589 * As the irq is submitted after the interface is changed,
1590 * this is the best method i figured out.
1591 * Any others?*/
1592 if (dec->interface == TTUSB_DEC_INTERFACE_IN)
1593 usb_kill_urb(dec->irq_urb);
1595 usb_free_urb(dec->irq_urb);
1597 usb_free_coherent(dec->udev, IRQ_PACKET_SIZE,
1598 dec->irq_buffer, dec->irq_dma_handle);
1601 dec->iso_stream_count = 0;
1603 for (i = 0; i < ISO_BUF_COUNT; i++)
1604 usb_kill_urb(dec->iso_urb[i]);
1606 ttusb_dec_free_iso_urbs(dec);
1609 static void ttusb_dec_exit_tasklet(struct ttusb_dec *dec)
1611 struct list_head *item;
1612 struct urb_frame *frame;
1614 tasklet_kill(&dec->urb_tasklet);
1616 while ((item = dec->urb_frame_list.next) != &dec->urb_frame_list) {
1617 frame = list_entry(item, struct urb_frame, urb_frame_list);
1618 list_del(&frame->urb_frame_list);
1619 kfree(frame);
1623 static void ttusb_dec_init_filters(struct ttusb_dec *dec)
1625 INIT_LIST_HEAD(&dec->filter_info_list);
1626 spin_lock_init(&dec->filter_info_list_lock);
1629 static void ttusb_dec_exit_filters(struct ttusb_dec *dec)
1631 struct list_head *item;
1632 struct filter_info *finfo;
1634 while ((item = dec->filter_info_list.next) != &dec->filter_info_list) {
1635 finfo = list_entry(item, struct filter_info, filter_info_list);
1636 list_del(&finfo->filter_info_list);
1637 kfree(finfo);
1641 static int fe_send_command(struct dvb_frontend* fe, const u8 command,
1642 int param_length, const u8 params[],
1643 int *result_length, u8 cmd_result[])
1645 struct ttusb_dec* dec = fe->dvb->priv;
1646 return ttusb_dec_send_command(dec, command, param_length, params, result_length, cmd_result);
1649 static const struct ttusbdecfe_config fe_config = {
1650 .send_command = fe_send_command
1653 static int ttusb_dec_probe(struct usb_interface *intf,
1654 const struct usb_device_id *id)
1656 struct usb_device *udev;
1657 struct ttusb_dec *dec;
1658 int result;
1660 dprintk("%s\n", __func__);
1662 udev = interface_to_usbdev(intf);
1664 if (!(dec = kzalloc(sizeof(struct ttusb_dec), GFP_KERNEL))) {
1665 printk("%s: couldn't allocate memory.\n", __func__);
1666 return -ENOMEM;
1669 usb_set_intfdata(intf, (void *)dec);
1671 switch (id->idProduct) {
1672 case 0x1006:
1673 ttusb_dec_set_model(dec, TTUSB_DEC3000S);
1674 break;
1676 case 0x1008:
1677 ttusb_dec_set_model(dec, TTUSB_DEC2000T);
1678 break;
1680 case 0x1009:
1681 ttusb_dec_set_model(dec, TTUSB_DEC2540T);
1682 break;
1685 dec->udev = udev;
1687 result = ttusb_dec_init_usb(dec);
1688 if (result)
1689 goto err_usb;
1690 result = ttusb_dec_init_stb(dec);
1691 if (result)
1692 goto err_stb;
1693 result = ttusb_dec_init_dvb(dec);
1694 if (result)
1695 goto err_stb;
1697 dec->adapter.priv = dec;
1698 switch (id->idProduct) {
1699 case 0x1006:
1700 dec->fe = ttusbdecfe_dvbs_attach(&fe_config);
1701 break;
1703 case 0x1008:
1704 case 0x1009:
1705 dec->fe = ttusbdecfe_dvbt_attach(&fe_config);
1706 break;
1709 if (dec->fe == NULL) {
1710 printk("dvb-ttusb-dec: A frontend driver was not found for device [%04x:%04x]\n",
1711 le16_to_cpu(dec->udev->descriptor.idVendor),
1712 le16_to_cpu(dec->udev->descriptor.idProduct));
1713 } else {
1714 if (dvb_register_frontend(&dec->adapter, dec->fe)) {
1715 printk("budget-ci: Frontend registration failed!\n");
1716 if (dec->fe->ops.release)
1717 dec->fe->ops.release(dec->fe);
1718 dec->fe = NULL;
1722 ttusb_dec_init_v_pes(dec);
1723 ttusb_dec_init_filters(dec);
1724 ttusb_dec_init_tasklet(dec);
1726 dec->active = 1;
1728 ttusb_dec_set_interface(dec, TTUSB_DEC_INTERFACE_IN);
1730 if (enable_rc)
1731 ttusb_init_rc(dec);
1733 return 0;
1734 err_stb:
1735 ttusb_dec_exit_usb(dec);
1736 err_usb:
1737 kfree(dec);
1738 return result;
1741 static void ttusb_dec_disconnect(struct usb_interface *intf)
1743 struct ttusb_dec *dec = usb_get_intfdata(intf);
1745 usb_set_intfdata(intf, NULL);
1747 dprintk("%s\n", __func__);
1749 if (dec->active) {
1750 ttusb_dec_exit_tasklet(dec);
1751 ttusb_dec_exit_filters(dec);
1752 if(enable_rc)
1753 ttusb_dec_exit_rc(dec);
1754 ttusb_dec_exit_usb(dec);
1755 ttusb_dec_exit_dvb(dec);
1758 kfree(dec);
1761 static void ttusb_dec_set_model(struct ttusb_dec *dec,
1762 enum ttusb_dec_model model)
1764 dec->model = model;
1766 switch (model) {
1767 case TTUSB_DEC2000T:
1768 dec->model_name = "DEC2000-t";
1769 dec->firmware_name = "dvb-ttusb-dec-2000t.fw";
1770 break;
1772 case TTUSB_DEC2540T:
1773 dec->model_name = "DEC2540-t";
1774 dec->firmware_name = "dvb-ttusb-dec-2540t.fw";
1775 break;
1777 case TTUSB_DEC3000S:
1778 dec->model_name = "DEC3000-s";
1779 dec->firmware_name = "dvb-ttusb-dec-3000s.fw";
1780 break;
1784 static const struct usb_device_id ttusb_dec_table[] = {
1785 {USB_DEVICE(0x0b48, 0x1006)}, /* DEC3000-s */
1786 /*{USB_DEVICE(0x0b48, 0x1007)}, Unconfirmed */
1787 {USB_DEVICE(0x0b48, 0x1008)}, /* DEC2000-t */
1788 {USB_DEVICE(0x0b48, 0x1009)}, /* DEC2540-t */
1792 static struct usb_driver ttusb_dec_driver = {
1793 .name = "ttusb-dec",
1794 .probe = ttusb_dec_probe,
1795 .disconnect = ttusb_dec_disconnect,
1796 .id_table = ttusb_dec_table,
1799 module_usb_driver(ttusb_dec_driver);
1801 MODULE_AUTHOR("Alex Woods <linux-dvb@giblets.org>");
1802 MODULE_DESCRIPTION(DRIVER_NAME);
1803 MODULE_LICENSE("GPL");
1804 MODULE_DEVICE_TABLE(usb, ttusb_dec_table);