irqchip/s3c24xx: Mark init_eint as __maybe_unused
[linux/fpc-iii.git] / drivers / media / usb / ttusb-dec / ttusb_dec.c
bloba5de46f04247f82178d60366d8148a4140f7780e
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.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <linux/list.h>
24 #include <linux/module.h>
25 #include <linux/pci.h>
26 #include <linux/slab.h>
27 #include <linux/spinlock.h>
28 #include <linux/usb.h>
29 #include <linux/interrupt.h>
30 #include <linux/firmware.h>
31 #include <linux/crc32.h>
32 #include <linux/init.h>
33 #include <linux/input.h>
35 #include <linux/mutex.h>
37 #include "dmxdev.h"
38 #include "dvb_demux.h"
39 #include "dvb_filter.h"
40 #include "dvb_frontend.h"
41 #include "dvb_net.h"
42 #include "ttusbdecfe.h"
44 static int debug;
45 static int output_pva;
46 static int enable_rc;
48 module_param(debug, int, 0644);
49 MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
50 module_param(output_pva, int, 0444);
51 MODULE_PARM_DESC(output_pva, "Output PVA from dvr device (default:off)");
52 module_param(enable_rc, int, 0644);
53 MODULE_PARM_DESC(enable_rc, "Turn on/off IR remote control(default: off)");
55 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
57 #define dprintk if (debug) printk
59 #define DRIVER_NAME "TechnoTrend/Hauppauge DEC USB"
61 #define COMMAND_PIPE 0x03
62 #define RESULT_PIPE 0x04
63 #define IN_PIPE 0x08
64 #define OUT_PIPE 0x07
65 #define IRQ_PIPE 0x0A
67 #define COMMAND_PACKET_SIZE 0x3c
68 #define ARM_PACKET_SIZE 0x1000
69 #define IRQ_PACKET_SIZE 0x8
71 #define ISO_BUF_COUNT 0x04
72 #define FRAMES_PER_ISO_BUF 0x04
73 #define ISO_FRAME_SIZE 0x0380
75 #define MAX_PVA_LENGTH 6144
77 enum ttusb_dec_model {
78 TTUSB_DEC2000T,
79 TTUSB_DEC2540T,
80 TTUSB_DEC3000S
83 enum ttusb_dec_packet_type {
84 TTUSB_DEC_PACKET_PVA,
85 TTUSB_DEC_PACKET_SECTION,
86 TTUSB_DEC_PACKET_EMPTY
89 enum ttusb_dec_interface {
90 TTUSB_DEC_INTERFACE_INITIAL,
91 TTUSB_DEC_INTERFACE_IN,
92 TTUSB_DEC_INTERFACE_OUT
95 struct ttusb_dec {
96 enum ttusb_dec_model model;
97 char *model_name;
98 char *firmware_name;
99 int can_playback;
101 /* DVB bits */
102 struct dvb_adapter adapter;
103 struct dmxdev dmxdev;
104 struct dvb_demux demux;
105 struct dmx_frontend frontend;
106 struct dvb_net dvb_net;
107 struct dvb_frontend* fe;
109 u16 pid[DMX_PES_OTHER];
111 /* USB bits */
112 struct usb_device *udev;
113 u8 trans_count;
114 unsigned int command_pipe;
115 unsigned int result_pipe;
116 unsigned int in_pipe;
117 unsigned int out_pipe;
118 unsigned int irq_pipe;
119 enum ttusb_dec_interface interface;
120 struct mutex usb_mutex;
122 void *irq_buffer;
123 struct urb *irq_urb;
124 dma_addr_t irq_dma_handle;
125 void *iso_buffer;
126 dma_addr_t iso_dma_handle;
127 struct urb *iso_urb[ISO_BUF_COUNT];
128 int iso_stream_count;
129 struct mutex iso_mutex;
131 u8 packet[MAX_PVA_LENGTH + 4];
132 enum ttusb_dec_packet_type packet_type;
133 int packet_state;
134 int packet_length;
135 int packet_payload_length;
136 u16 next_packet_id;
138 int pva_stream_count;
139 int filter_stream_count;
141 struct dvb_filter_pes2ts a_pes2ts;
142 struct dvb_filter_pes2ts v_pes2ts;
144 u8 v_pes[16 + MAX_PVA_LENGTH];
145 int v_pes_length;
146 int v_pes_postbytes;
148 struct list_head urb_frame_list;
149 struct tasklet_struct urb_tasklet;
150 spinlock_t urb_frame_list_lock;
152 struct dvb_demux_filter *audio_filter;
153 struct dvb_demux_filter *video_filter;
154 struct list_head filter_info_list;
155 spinlock_t filter_info_list_lock;
157 struct input_dev *rc_input_dev;
158 char rc_phys[64];
160 int active; /* Loaded successfully */
163 struct urb_frame {
164 u8 data[ISO_FRAME_SIZE];
165 int length;
166 struct list_head urb_frame_list;
169 struct filter_info {
170 u8 stream_id;
171 struct dvb_demux_filter *filter;
172 struct list_head filter_info_list;
175 static u16 rc_keys[] = {
176 KEY_POWER,
177 KEY_MUTE,
178 KEY_1,
179 KEY_2,
180 KEY_3,
181 KEY_4,
182 KEY_5,
183 KEY_6,
184 KEY_7,
185 KEY_8,
186 KEY_9,
187 KEY_0,
188 KEY_CHANNELUP,
189 KEY_VOLUMEDOWN,
190 KEY_OK,
191 KEY_VOLUMEUP,
192 KEY_CHANNELDOWN,
193 KEY_PREVIOUS,
194 KEY_ESC,
195 KEY_RED,
196 KEY_GREEN,
197 KEY_YELLOW,
198 KEY_BLUE,
199 KEY_OPTION,
200 KEY_M,
201 KEY_RADIO
204 static void ttusb_dec_set_model(struct ttusb_dec *dec,
205 enum ttusb_dec_model model);
207 static void ttusb_dec_handle_irq( struct urb *urb)
209 struct ttusb_dec * dec = urb->context;
210 char *buffer = dec->irq_buffer;
211 int retval;
213 switch(urb->status) {
214 case 0: /*success*/
215 break;
216 case -ECONNRESET:
217 case -ENOENT:
218 case -ESHUTDOWN:
219 case -ETIME:
220 /* this urb is dead, cleanup */
221 dprintk("%s:urb shutting down with status: %d\n",
222 __func__, urb->status);
223 return;
224 default:
225 dprintk("%s:nonzero status received: %d\n",
226 __func__,urb->status);
227 goto exit;
230 if( (buffer[0] == 0x1) && (buffer[2] == 0x15) ) {
231 /* IR - Event */
232 /* this is an fact a bit too simple implementation;
233 * the box also reports a keyrepeat signal
234 * (with buffer[3] == 0x40) in an intervall of ~100ms.
235 * But to handle this correctly we had to imlemenent some
236 * kind of timer which signals a 'key up' event if no
237 * keyrepeat signal is received for lets say 200ms.
238 * this should/could be added later ...
239 * for now lets report each signal as a key down and up*/
240 dprintk("%s:rc signal:%d\n", __func__, buffer[4]);
241 input_report_key(dec->rc_input_dev, rc_keys[buffer[4] - 1], 1);
242 input_sync(dec->rc_input_dev);
243 input_report_key(dec->rc_input_dev, rc_keys[buffer[4] - 1], 0);
244 input_sync(dec->rc_input_dev);
247 exit: retval = usb_submit_urb(urb, GFP_ATOMIC);
248 if(retval)
249 printk("%s - usb_commit_urb failed with result: %d\n",
250 __func__, retval);
253 static u16 crc16(u16 crc, const u8 *buf, size_t len)
255 u16 tmp;
257 while (len--) {
258 crc ^= *buf++;
259 crc ^= (u8)crc >> 4;
260 tmp = (u8)crc;
261 crc ^= (tmp ^ (tmp << 1)) << 4;
263 return crc;
266 static int ttusb_dec_send_command(struct ttusb_dec *dec, const u8 command,
267 int param_length, const u8 params[],
268 int *result_length, u8 cmd_result[])
270 int result, actual_len, i;
271 u8 *b;
273 dprintk("%s\n", __func__);
275 b = kmalloc(COMMAND_PACKET_SIZE + 4, GFP_KERNEL);
276 if (!b)
277 return -ENOMEM;
279 if ((result = mutex_lock_interruptible(&dec->usb_mutex))) {
280 kfree(b);
281 printk("%s: Failed to lock usb mutex.\n", __func__);
282 return result;
285 b[0] = 0xaa;
286 b[1] = ++dec->trans_count;
287 b[2] = command;
288 b[3] = param_length;
290 if (params)
291 memcpy(&b[4], params, param_length);
293 if (debug) {
294 printk("%s: command: ", __func__);
295 for (i = 0; i < param_length + 4; i++)
296 printk("0x%02X ", b[i]);
297 printk("\n");
300 result = usb_bulk_msg(dec->udev, dec->command_pipe, b,
301 COMMAND_PACKET_SIZE + 4, &actual_len, 1000);
303 if (result) {
304 printk("%s: command bulk message failed: error %d\n",
305 __func__, result);
306 mutex_unlock(&dec->usb_mutex);
307 kfree(b);
308 return result;
311 result = usb_bulk_msg(dec->udev, dec->result_pipe, b,
312 COMMAND_PACKET_SIZE + 4, &actual_len, 1000);
314 if (result) {
315 printk("%s: result bulk message failed: error %d\n",
316 __func__, result);
317 mutex_unlock(&dec->usb_mutex);
318 kfree(b);
319 return result;
320 } else {
321 if (debug) {
322 printk("%s: result: ", __func__);
323 for (i = 0; i < actual_len; i++)
324 printk("0x%02X ", b[i]);
325 printk("\n");
328 if (result_length)
329 *result_length = b[3];
330 if (cmd_result && b[3] > 0)
331 memcpy(cmd_result, &b[4], b[3]);
333 mutex_unlock(&dec->usb_mutex);
335 kfree(b);
336 return 0;
340 static int ttusb_dec_get_stb_state (struct ttusb_dec *dec, unsigned int *mode,
341 unsigned int *model, unsigned int *version)
343 u8 c[COMMAND_PACKET_SIZE];
344 int c_length;
345 int result;
346 __be32 tmp;
348 dprintk("%s\n", __func__);
350 result = ttusb_dec_send_command(dec, 0x08, 0, NULL, &c_length, c);
351 if (result)
352 return result;
354 if (c_length >= 0x0c) {
355 if (mode != NULL) {
356 memcpy(&tmp, c, 4);
357 *mode = ntohl(tmp);
359 if (model != NULL) {
360 memcpy(&tmp, &c[4], 4);
361 *model = ntohl(tmp);
363 if (version != NULL) {
364 memcpy(&tmp, &c[8], 4);
365 *version = ntohl(tmp);
367 return 0;
368 } else {
369 return -ENOENT;
373 static int ttusb_dec_audio_pes2ts_cb(void *priv, unsigned char *data)
375 struct ttusb_dec *dec = priv;
377 dec->audio_filter->feed->cb.ts(data, 188, NULL, 0,
378 &dec->audio_filter->feed->feed.ts);
380 return 0;
383 static int ttusb_dec_video_pes2ts_cb(void *priv, unsigned char *data)
385 struct ttusb_dec *dec = priv;
387 dec->video_filter->feed->cb.ts(data, 188, NULL, 0,
388 &dec->video_filter->feed->feed.ts);
390 return 0;
393 static void ttusb_dec_set_pids(struct ttusb_dec *dec)
395 u8 b[] = { 0x00, 0x00, 0x00, 0x00,
396 0x00, 0x00, 0xff, 0xff,
397 0xff, 0xff, 0xff, 0xff };
399 __be16 pcr = htons(dec->pid[DMX_PES_PCR]);
400 __be16 audio = htons(dec->pid[DMX_PES_AUDIO]);
401 __be16 video = htons(dec->pid[DMX_PES_VIDEO]);
403 dprintk("%s\n", __func__);
405 memcpy(&b[0], &pcr, 2);
406 memcpy(&b[2], &audio, 2);
407 memcpy(&b[4], &video, 2);
409 ttusb_dec_send_command(dec, 0x50, sizeof(b), b, NULL, NULL);
411 dvb_filter_pes2ts_init(&dec->a_pes2ts, dec->pid[DMX_PES_AUDIO],
412 ttusb_dec_audio_pes2ts_cb, dec);
413 dvb_filter_pes2ts_init(&dec->v_pes2ts, dec->pid[DMX_PES_VIDEO],
414 ttusb_dec_video_pes2ts_cb, dec);
415 dec->v_pes_length = 0;
416 dec->v_pes_postbytes = 0;
419 static void ttusb_dec_process_pva(struct ttusb_dec *dec, u8 *pva, int length)
421 if (length < 8) {
422 printk("%s: packet too short - discarding\n", __func__);
423 return;
426 if (length > 8 + MAX_PVA_LENGTH) {
427 printk("%s: packet too long - discarding\n", __func__);
428 return;
431 switch (pva[2]) {
433 case 0x01: { /* VideoStream */
434 int prebytes = pva[5] & 0x03;
435 int postbytes = (pva[5] & 0x0c) >> 2;
436 __be16 v_pes_payload_length;
438 if (output_pva) {
439 dec->video_filter->feed->cb.ts(pva, length, NULL, 0,
440 &dec->video_filter->feed->feed.ts);
441 return;
444 if (dec->v_pes_postbytes > 0 &&
445 dec->v_pes_postbytes == prebytes) {
446 memcpy(&dec->v_pes[dec->v_pes_length],
447 &pva[12], prebytes);
449 dvb_filter_pes2ts(&dec->v_pes2ts, dec->v_pes,
450 dec->v_pes_length + prebytes, 1);
453 if (pva[5] & 0x10) {
454 dec->v_pes[7] = 0x80;
455 dec->v_pes[8] = 0x05;
457 dec->v_pes[9] = 0x21 | ((pva[8] & 0xc0) >> 5);
458 dec->v_pes[10] = ((pva[8] & 0x3f) << 2) |
459 ((pva[9] & 0xc0) >> 6);
460 dec->v_pes[11] = 0x01 |
461 ((pva[9] & 0x3f) << 2) |
462 ((pva[10] & 0x80) >> 6);
463 dec->v_pes[12] = ((pva[10] & 0x7f) << 1) |
464 ((pva[11] & 0xc0) >> 7);
465 dec->v_pes[13] = 0x01 | ((pva[11] & 0x7f) << 1);
467 memcpy(&dec->v_pes[14], &pva[12 + prebytes],
468 length - 12 - prebytes);
469 dec->v_pes_length = 14 + length - 12 - prebytes;
470 } else {
471 dec->v_pes[7] = 0x00;
472 dec->v_pes[8] = 0x00;
474 memcpy(&dec->v_pes[9], &pva[8], length - 8);
475 dec->v_pes_length = 9 + length - 8;
478 dec->v_pes_postbytes = postbytes;
480 if (dec->v_pes[9 + dec->v_pes[8]] == 0x00 &&
481 dec->v_pes[10 + dec->v_pes[8]] == 0x00 &&
482 dec->v_pes[11 + dec->v_pes[8]] == 0x01)
483 dec->v_pes[6] = 0x84;
484 else
485 dec->v_pes[6] = 0x80;
487 v_pes_payload_length = htons(dec->v_pes_length - 6 +
488 postbytes);
489 memcpy(&dec->v_pes[4], &v_pes_payload_length, 2);
491 if (postbytes == 0)
492 dvb_filter_pes2ts(&dec->v_pes2ts, dec->v_pes,
493 dec->v_pes_length, 1);
495 break;
498 case 0x02: /* MainAudioStream */
499 if (output_pva) {
500 dec->audio_filter->feed->cb.ts(pva, length, NULL, 0,
501 &dec->audio_filter->feed->feed.ts);
502 return;
505 dvb_filter_pes2ts(&dec->a_pes2ts, &pva[8], length - 8,
506 pva[5] & 0x10);
507 break;
509 default:
510 printk("%s: unknown PVA type: %02x.\n", __func__,
511 pva[2]);
512 break;
516 static void ttusb_dec_process_filter(struct ttusb_dec *dec, u8 *packet,
517 int length)
519 struct list_head *item;
520 struct filter_info *finfo;
521 struct dvb_demux_filter *filter = NULL;
522 unsigned long flags;
523 u8 sid;
525 sid = packet[1];
526 spin_lock_irqsave(&dec->filter_info_list_lock, flags);
527 for (item = dec->filter_info_list.next; item != &dec->filter_info_list;
528 item = item->next) {
529 finfo = list_entry(item, struct filter_info, filter_info_list);
530 if (finfo->stream_id == sid) {
531 filter = finfo->filter;
532 break;
535 spin_unlock_irqrestore(&dec->filter_info_list_lock, flags);
537 if (filter)
538 filter->feed->cb.sec(&packet[2], length - 2, NULL, 0,
539 &filter->filter);
542 static void ttusb_dec_process_packet(struct ttusb_dec *dec)
544 int i;
545 u16 csum = 0;
546 u16 packet_id;
548 if (dec->packet_length % 2) {
549 printk("%s: odd sized packet - discarding\n", __func__);
550 return;
553 for (i = 0; i < dec->packet_length; i += 2)
554 csum ^= ((dec->packet[i] << 8) + dec->packet[i + 1]);
556 if (csum) {
557 printk("%s: checksum failed - discarding\n", __func__);
558 return;
561 packet_id = dec->packet[dec->packet_length - 4] << 8;
562 packet_id += dec->packet[dec->packet_length - 3];
564 if ((packet_id != dec->next_packet_id) && dec->next_packet_id) {
565 printk("%s: warning: lost packets between %u and %u\n",
566 __func__, dec->next_packet_id - 1, packet_id);
569 if (packet_id == 0xffff)
570 dec->next_packet_id = 0x8000;
571 else
572 dec->next_packet_id = packet_id + 1;
574 switch (dec->packet_type) {
575 case TTUSB_DEC_PACKET_PVA:
576 if (dec->pva_stream_count)
577 ttusb_dec_process_pva(dec, dec->packet,
578 dec->packet_payload_length);
579 break;
581 case TTUSB_DEC_PACKET_SECTION:
582 if (dec->filter_stream_count)
583 ttusb_dec_process_filter(dec, dec->packet,
584 dec->packet_payload_length);
585 break;
587 case TTUSB_DEC_PACKET_EMPTY:
588 break;
592 static void swap_bytes(u8 *b, int length)
594 length -= length % 2;
595 for (; length; b += 2, length -= 2)
596 swap(*b, *(b + 1));
599 static void ttusb_dec_process_urb_frame(struct ttusb_dec *dec, u8 *b,
600 int length)
602 swap_bytes(b, length);
604 while (length) {
605 switch (dec->packet_state) {
607 case 0:
608 case 1:
609 case 2:
610 if (*b++ == 0xaa)
611 dec->packet_state++;
612 else
613 dec->packet_state = 0;
615 length--;
616 break;
618 case 3:
619 if (*b == 0x00) {
620 dec->packet_state++;
621 dec->packet_length = 0;
622 } else if (*b != 0xaa) {
623 dec->packet_state = 0;
626 b++;
627 length--;
628 break;
630 case 4:
631 dec->packet[dec->packet_length++] = *b++;
633 if (dec->packet_length == 2) {
634 if (dec->packet[0] == 'A' &&
635 dec->packet[1] == 'V') {
636 dec->packet_type =
637 TTUSB_DEC_PACKET_PVA;
638 dec->packet_state++;
639 } else if (dec->packet[0] == 'S') {
640 dec->packet_type =
641 TTUSB_DEC_PACKET_SECTION;
642 dec->packet_state++;
643 } else if (dec->packet[0] == 0x00) {
644 dec->packet_type =
645 TTUSB_DEC_PACKET_EMPTY;
646 dec->packet_payload_length = 2;
647 dec->packet_state = 7;
648 } else {
649 printk("%s: unknown packet type: "
650 "%02x%02x\n", __func__,
651 dec->packet[0], dec->packet[1]);
652 dec->packet_state = 0;
656 length--;
657 break;
659 case 5:
660 dec->packet[dec->packet_length++] = *b++;
662 if (dec->packet_type == TTUSB_DEC_PACKET_PVA &&
663 dec->packet_length == 8) {
664 dec->packet_state++;
665 dec->packet_payload_length = 8 +
666 (dec->packet[6] << 8) +
667 dec->packet[7];
668 } else if (dec->packet_type ==
669 TTUSB_DEC_PACKET_SECTION &&
670 dec->packet_length == 5) {
671 dec->packet_state++;
672 dec->packet_payload_length = 5 +
673 ((dec->packet[3] & 0x0f) << 8) +
674 dec->packet[4];
677 length--;
678 break;
680 case 6: {
681 int remainder = dec->packet_payload_length -
682 dec->packet_length;
684 if (length >= remainder) {
685 memcpy(dec->packet + dec->packet_length,
686 b, remainder);
687 dec->packet_length += remainder;
688 b += remainder;
689 length -= remainder;
690 dec->packet_state++;
691 } else {
692 memcpy(&dec->packet[dec->packet_length],
693 b, length);
694 dec->packet_length += length;
695 length = 0;
698 break;
701 case 7: {
702 int tail = 4;
704 dec->packet[dec->packet_length++] = *b++;
706 if (dec->packet_type == TTUSB_DEC_PACKET_SECTION &&
707 dec->packet_payload_length % 2)
708 tail++;
710 if (dec->packet_length ==
711 dec->packet_payload_length + tail) {
712 ttusb_dec_process_packet(dec);
713 dec->packet_state = 0;
716 length--;
717 break;
720 default:
721 printk("%s: illegal packet state encountered.\n",
722 __func__);
723 dec->packet_state = 0;
728 static void ttusb_dec_process_urb_frame_list(unsigned long data)
730 struct ttusb_dec *dec = (struct ttusb_dec *)data;
731 struct list_head *item;
732 struct urb_frame *frame;
733 unsigned long flags;
735 while (1) {
736 spin_lock_irqsave(&dec->urb_frame_list_lock, flags);
737 if ((item = dec->urb_frame_list.next) != &dec->urb_frame_list) {
738 frame = list_entry(item, struct urb_frame,
739 urb_frame_list);
740 list_del(&frame->urb_frame_list);
741 } else {
742 spin_unlock_irqrestore(&dec->urb_frame_list_lock,
743 flags);
744 return;
746 spin_unlock_irqrestore(&dec->urb_frame_list_lock, flags);
748 ttusb_dec_process_urb_frame(dec, frame->data, frame->length);
749 kfree(frame);
753 static void ttusb_dec_process_urb(struct urb *urb)
755 struct ttusb_dec *dec = urb->context;
757 if (!urb->status) {
758 int i;
760 for (i = 0; i < FRAMES_PER_ISO_BUF; i++) {
761 struct usb_iso_packet_descriptor *d;
762 u8 *b;
763 int length;
764 struct urb_frame *frame;
766 d = &urb->iso_frame_desc[i];
767 b = urb->transfer_buffer + d->offset;
768 length = d->actual_length;
770 if ((frame = kmalloc(sizeof(struct urb_frame),
771 GFP_ATOMIC))) {
772 unsigned long flags;
774 memcpy(frame->data, b, length);
775 frame->length = length;
777 spin_lock_irqsave(&dec->urb_frame_list_lock,
778 flags);
779 list_add_tail(&frame->urb_frame_list,
780 &dec->urb_frame_list);
781 spin_unlock_irqrestore(&dec->urb_frame_list_lock,
782 flags);
784 tasklet_schedule(&dec->urb_tasklet);
787 } else {
788 /* -ENOENT is expected when unlinking urbs */
789 if (urb->status != -ENOENT)
790 dprintk("%s: urb error: %d\n", __func__,
791 urb->status);
794 if (dec->iso_stream_count)
795 usb_submit_urb(urb, GFP_ATOMIC);
798 static void ttusb_dec_setup_urbs(struct ttusb_dec *dec)
800 int i, j, buffer_offset = 0;
802 dprintk("%s\n", __func__);
804 for (i = 0; i < ISO_BUF_COUNT; i++) {
805 int frame_offset = 0;
806 struct urb *urb = dec->iso_urb[i];
808 urb->dev = dec->udev;
809 urb->context = dec;
810 urb->complete = ttusb_dec_process_urb;
811 urb->pipe = dec->in_pipe;
812 urb->transfer_flags = URB_ISO_ASAP;
813 urb->interval = 1;
814 urb->number_of_packets = FRAMES_PER_ISO_BUF;
815 urb->transfer_buffer_length = ISO_FRAME_SIZE *
816 FRAMES_PER_ISO_BUF;
817 urb->transfer_buffer = dec->iso_buffer + buffer_offset;
818 buffer_offset += ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF;
820 for (j = 0; j < FRAMES_PER_ISO_BUF; j++) {
821 urb->iso_frame_desc[j].offset = frame_offset;
822 urb->iso_frame_desc[j].length = ISO_FRAME_SIZE;
823 frame_offset += ISO_FRAME_SIZE;
828 static void ttusb_dec_stop_iso_xfer(struct ttusb_dec *dec)
830 int i;
832 dprintk("%s\n", __func__);
834 if (mutex_lock_interruptible(&dec->iso_mutex))
835 return;
837 dec->iso_stream_count--;
839 if (!dec->iso_stream_count) {
840 for (i = 0; i < ISO_BUF_COUNT; i++)
841 usb_kill_urb(dec->iso_urb[i]);
844 mutex_unlock(&dec->iso_mutex);
847 /* Setting the interface of the DEC tends to take down the USB communications
848 * for a short period, so it's important not to call this function just before
849 * trying to talk to it.
851 static int ttusb_dec_set_interface(struct ttusb_dec *dec,
852 enum ttusb_dec_interface interface)
854 int result = 0;
855 u8 b[] = { 0x05 };
857 if (interface != dec->interface) {
858 switch (interface) {
859 case TTUSB_DEC_INTERFACE_INITIAL:
860 result = usb_set_interface(dec->udev, 0, 0);
861 break;
862 case TTUSB_DEC_INTERFACE_IN:
863 result = ttusb_dec_send_command(dec, 0x80, sizeof(b),
864 b, NULL, NULL);
865 if (result)
866 return result;
867 result = usb_set_interface(dec->udev, 0, 8);
868 break;
869 case TTUSB_DEC_INTERFACE_OUT:
870 result = usb_set_interface(dec->udev, 0, 1);
871 break;
874 if (result)
875 return result;
877 dec->interface = interface;
880 return 0;
883 static int ttusb_dec_start_iso_xfer(struct ttusb_dec *dec)
885 int i, result;
887 dprintk("%s\n", __func__);
889 if (mutex_lock_interruptible(&dec->iso_mutex))
890 return -EAGAIN;
892 if (!dec->iso_stream_count) {
893 ttusb_dec_setup_urbs(dec);
895 dec->packet_state = 0;
896 dec->v_pes_postbytes = 0;
897 dec->next_packet_id = 0;
899 for (i = 0; i < ISO_BUF_COUNT; i++) {
900 if ((result = usb_submit_urb(dec->iso_urb[i],
901 GFP_ATOMIC))) {
902 printk("%s: failed urb submission %d: "
903 "error %d\n", __func__, i, result);
905 while (i) {
906 usb_kill_urb(dec->iso_urb[i - 1]);
907 i--;
910 mutex_unlock(&dec->iso_mutex);
911 return result;
916 dec->iso_stream_count++;
918 mutex_unlock(&dec->iso_mutex);
920 return 0;
923 static int ttusb_dec_start_ts_feed(struct dvb_demux_feed *dvbdmxfeed)
925 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
926 struct ttusb_dec *dec = dvbdmx->priv;
927 u8 b0[] = { 0x05 };
928 int result = 0;
930 dprintk("%s\n", __func__);
932 dprintk(" ts_type:");
934 if (dvbdmxfeed->ts_type & TS_DECODER)
935 dprintk(" TS_DECODER");
937 if (dvbdmxfeed->ts_type & TS_PACKET)
938 dprintk(" TS_PACKET");
940 if (dvbdmxfeed->ts_type & TS_PAYLOAD_ONLY)
941 dprintk(" TS_PAYLOAD_ONLY");
943 dprintk("\n");
945 switch (dvbdmxfeed->pes_type) {
947 case DMX_PES_VIDEO:
948 dprintk(" pes_type: DMX_PES_VIDEO\n");
949 dec->pid[DMX_PES_PCR] = dvbdmxfeed->pid;
950 dec->pid[DMX_PES_VIDEO] = dvbdmxfeed->pid;
951 dec->video_filter = dvbdmxfeed->filter;
952 ttusb_dec_set_pids(dec);
953 break;
955 case DMX_PES_AUDIO:
956 dprintk(" pes_type: DMX_PES_AUDIO\n");
957 dec->pid[DMX_PES_AUDIO] = dvbdmxfeed->pid;
958 dec->audio_filter = dvbdmxfeed->filter;
959 ttusb_dec_set_pids(dec);
960 break;
962 case DMX_PES_TELETEXT:
963 dec->pid[DMX_PES_TELETEXT] = dvbdmxfeed->pid;
964 dprintk(" pes_type: DMX_PES_TELETEXT(not supported)\n");
965 return -ENOSYS;
967 case DMX_PES_PCR:
968 dprintk(" pes_type: DMX_PES_PCR\n");
969 dec->pid[DMX_PES_PCR] = dvbdmxfeed->pid;
970 ttusb_dec_set_pids(dec);
971 break;
973 case DMX_PES_OTHER:
974 dprintk(" pes_type: DMX_PES_OTHER(not supported)\n");
975 return -ENOSYS;
977 default:
978 dprintk(" pes_type: unknown (%d)\n", dvbdmxfeed->pes_type);
979 return -EINVAL;
983 result = ttusb_dec_send_command(dec, 0x80, sizeof(b0), b0, NULL, NULL);
984 if (result)
985 return result;
987 dec->pva_stream_count++;
988 return ttusb_dec_start_iso_xfer(dec);
991 static int ttusb_dec_start_sec_feed(struct dvb_demux_feed *dvbdmxfeed)
993 struct ttusb_dec *dec = dvbdmxfeed->demux->priv;
994 u8 b0[] = { 0x00, 0x00, 0x00, 0x01,
995 0x00, 0x00, 0x00, 0x00,
996 0x00, 0x00, 0x00, 0x00,
997 0x00, 0x00, 0x00, 0x00,
998 0x00, 0xff, 0x00, 0x00,
999 0x00, 0x00, 0x00, 0x00,
1000 0x00, 0x00, 0x00, 0x00,
1001 0x00 };
1002 __be16 pid;
1003 u8 c[COMMAND_PACKET_SIZE];
1004 int c_length;
1005 int result;
1006 struct filter_info *finfo;
1007 unsigned long flags;
1008 u8 x = 1;
1010 dprintk("%s\n", __func__);
1012 pid = htons(dvbdmxfeed->pid);
1013 memcpy(&b0[0], &pid, 2);
1014 memcpy(&b0[4], &x, 1);
1015 memcpy(&b0[5], &dvbdmxfeed->filter->filter.filter_value[0], 1);
1017 result = ttusb_dec_send_command(dec, 0x60, sizeof(b0), b0,
1018 &c_length, c);
1020 if (!result) {
1021 if (c_length == 2) {
1022 if (!(finfo = kmalloc(sizeof(struct filter_info),
1023 GFP_ATOMIC)))
1024 return -ENOMEM;
1026 finfo->stream_id = c[1];
1027 finfo->filter = dvbdmxfeed->filter;
1029 spin_lock_irqsave(&dec->filter_info_list_lock, flags);
1030 list_add_tail(&finfo->filter_info_list,
1031 &dec->filter_info_list);
1032 spin_unlock_irqrestore(&dec->filter_info_list_lock,
1033 flags);
1035 dvbdmxfeed->priv = finfo;
1037 dec->filter_stream_count++;
1038 return ttusb_dec_start_iso_xfer(dec);
1041 return -EAGAIN;
1042 } else
1043 return result;
1046 static int ttusb_dec_start_feed(struct dvb_demux_feed *dvbdmxfeed)
1048 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
1050 dprintk("%s\n", __func__);
1052 if (!dvbdmx->dmx.frontend)
1053 return -EINVAL;
1055 dprintk(" pid: 0x%04X\n", dvbdmxfeed->pid);
1057 switch (dvbdmxfeed->type) {
1059 case DMX_TYPE_TS:
1060 return ttusb_dec_start_ts_feed(dvbdmxfeed);
1061 break;
1063 case DMX_TYPE_SEC:
1064 return ttusb_dec_start_sec_feed(dvbdmxfeed);
1065 break;
1067 default:
1068 dprintk(" type: unknown (%d)\n", dvbdmxfeed->type);
1069 return -EINVAL;
1074 static int ttusb_dec_stop_ts_feed(struct dvb_demux_feed *dvbdmxfeed)
1076 struct ttusb_dec *dec = dvbdmxfeed->demux->priv;
1077 u8 b0[] = { 0x00 };
1079 ttusb_dec_send_command(dec, 0x81, sizeof(b0), b0, NULL, NULL);
1081 dec->pva_stream_count--;
1083 ttusb_dec_stop_iso_xfer(dec);
1085 return 0;
1088 static int ttusb_dec_stop_sec_feed(struct dvb_demux_feed *dvbdmxfeed)
1090 struct ttusb_dec *dec = dvbdmxfeed->demux->priv;
1091 u8 b0[] = { 0x00, 0x00 };
1092 struct filter_info *finfo = (struct filter_info *)dvbdmxfeed->priv;
1093 unsigned long flags;
1095 b0[1] = finfo->stream_id;
1096 spin_lock_irqsave(&dec->filter_info_list_lock, flags);
1097 list_del(&finfo->filter_info_list);
1098 spin_unlock_irqrestore(&dec->filter_info_list_lock, flags);
1099 kfree(finfo);
1100 ttusb_dec_send_command(dec, 0x62, sizeof(b0), b0, NULL, NULL);
1102 dec->filter_stream_count--;
1104 ttusb_dec_stop_iso_xfer(dec);
1106 return 0;
1109 static int ttusb_dec_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
1111 dprintk("%s\n", __func__);
1113 switch (dvbdmxfeed->type) {
1114 case DMX_TYPE_TS:
1115 return ttusb_dec_stop_ts_feed(dvbdmxfeed);
1116 break;
1118 case DMX_TYPE_SEC:
1119 return ttusb_dec_stop_sec_feed(dvbdmxfeed);
1120 break;
1123 return 0;
1126 static void ttusb_dec_free_iso_urbs(struct ttusb_dec *dec)
1128 int i;
1130 dprintk("%s\n", __func__);
1132 for (i = 0; i < ISO_BUF_COUNT; i++)
1133 usb_free_urb(dec->iso_urb[i]);
1135 pci_free_consistent(NULL,
1136 ISO_FRAME_SIZE * (FRAMES_PER_ISO_BUF *
1137 ISO_BUF_COUNT),
1138 dec->iso_buffer, dec->iso_dma_handle);
1141 static int ttusb_dec_alloc_iso_urbs(struct ttusb_dec *dec)
1143 int i;
1145 dprintk("%s\n", __func__);
1147 dec->iso_buffer = pci_zalloc_consistent(NULL,
1148 ISO_FRAME_SIZE * (FRAMES_PER_ISO_BUF * ISO_BUF_COUNT),
1149 &dec->iso_dma_handle);
1151 if (!dec->iso_buffer) {
1152 dprintk("%s: pci_alloc_consistent - not enough memory\n",
1153 __func__);
1154 return -ENOMEM;
1157 for (i = 0; i < ISO_BUF_COUNT; i++) {
1158 struct urb *urb;
1160 if (!(urb = usb_alloc_urb(FRAMES_PER_ISO_BUF, GFP_ATOMIC))) {
1161 ttusb_dec_free_iso_urbs(dec);
1162 return -ENOMEM;
1165 dec->iso_urb[i] = urb;
1168 ttusb_dec_setup_urbs(dec);
1170 return 0;
1173 static void ttusb_dec_init_tasklet(struct ttusb_dec *dec)
1175 spin_lock_init(&dec->urb_frame_list_lock);
1176 INIT_LIST_HEAD(&dec->urb_frame_list);
1177 tasklet_init(&dec->urb_tasklet, ttusb_dec_process_urb_frame_list,
1178 (unsigned long)dec);
1181 static int ttusb_init_rc( struct ttusb_dec *dec)
1183 struct input_dev *input_dev;
1184 u8 b[] = { 0x00, 0x01 };
1185 int i;
1186 int err;
1188 usb_make_path(dec->udev, dec->rc_phys, sizeof(dec->rc_phys));
1189 strlcat(dec->rc_phys, "/input0", sizeof(dec->rc_phys));
1191 input_dev = input_allocate_device();
1192 if (!input_dev)
1193 return -ENOMEM;
1195 input_dev->name = "ttusb_dec remote control";
1196 input_dev->phys = dec->rc_phys;
1197 input_dev->evbit[0] = BIT_MASK(EV_KEY);
1198 input_dev->keycodesize = sizeof(u16);
1199 input_dev->keycodemax = 0x1a;
1200 input_dev->keycode = rc_keys;
1202 for (i = 0; i < ARRAY_SIZE(rc_keys); i++)
1203 set_bit(rc_keys[i], input_dev->keybit);
1205 err = input_register_device(input_dev);
1206 if (err) {
1207 input_free_device(input_dev);
1208 return err;
1211 dec->rc_input_dev = input_dev;
1212 if (usb_submit_urb(dec->irq_urb, GFP_KERNEL))
1213 printk("%s: usb_submit_urb failed\n",__func__);
1214 /* enable irq pipe */
1215 ttusb_dec_send_command(dec,0xb0,sizeof(b),b,NULL,NULL);
1217 return 0;
1220 static void ttusb_dec_init_v_pes(struct ttusb_dec *dec)
1222 dprintk("%s\n", __func__);
1224 dec->v_pes[0] = 0x00;
1225 dec->v_pes[1] = 0x00;
1226 dec->v_pes[2] = 0x01;
1227 dec->v_pes[3] = 0xe0;
1230 static int ttusb_dec_init_usb(struct ttusb_dec *dec)
1232 int result;
1234 dprintk("%s\n", __func__);
1236 mutex_init(&dec->usb_mutex);
1237 mutex_init(&dec->iso_mutex);
1239 dec->command_pipe = usb_sndbulkpipe(dec->udev, COMMAND_PIPE);
1240 dec->result_pipe = usb_rcvbulkpipe(dec->udev, RESULT_PIPE);
1241 dec->in_pipe = usb_rcvisocpipe(dec->udev, IN_PIPE);
1242 dec->out_pipe = usb_sndisocpipe(dec->udev, OUT_PIPE);
1243 dec->irq_pipe = usb_rcvintpipe(dec->udev, IRQ_PIPE);
1245 if(enable_rc) {
1246 dec->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
1247 if(!dec->irq_urb) {
1248 return -ENOMEM;
1250 dec->irq_buffer = usb_alloc_coherent(dec->udev,IRQ_PACKET_SIZE,
1251 GFP_KERNEL, &dec->irq_dma_handle);
1252 if(!dec->irq_buffer) {
1253 usb_free_urb(dec->irq_urb);
1254 return -ENOMEM;
1256 usb_fill_int_urb(dec->irq_urb, dec->udev,dec->irq_pipe,
1257 dec->irq_buffer, IRQ_PACKET_SIZE,
1258 ttusb_dec_handle_irq, dec, 1);
1259 dec->irq_urb->transfer_dma = dec->irq_dma_handle;
1260 dec->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1263 result = ttusb_dec_alloc_iso_urbs(dec);
1264 if (result) {
1265 usb_free_urb(dec->irq_urb);
1266 usb_free_coherent(dec->udev, IRQ_PACKET_SIZE,
1267 dec->irq_buffer, dec->irq_dma_handle);
1269 return result;
1272 static int ttusb_dec_boot_dsp(struct ttusb_dec *dec)
1274 int i, j, actual_len, result, size, trans_count;
1275 u8 b0[] = { 0x00, 0x00, 0x00, 0x00,
1276 0x00, 0x00, 0x00, 0x00,
1277 0x61, 0x00 };
1278 u8 b1[] = { 0x61 };
1279 u8 *b;
1280 char idstring[21];
1281 const u8 *firmware = NULL;
1282 size_t firmware_size = 0;
1283 u16 firmware_csum = 0;
1284 __be16 firmware_csum_ns;
1285 __be32 firmware_size_nl;
1286 u32 crc32_csum, crc32_check;
1287 __be32 tmp;
1288 const struct firmware *fw_entry = NULL;
1290 dprintk("%s\n", __func__);
1292 result = request_firmware(&fw_entry, dec->firmware_name, &dec->udev->dev);
1293 if (result) {
1294 printk(KERN_ERR "%s: Firmware (%s) unavailable.\n",
1295 __func__, dec->firmware_name);
1296 return result;
1299 firmware = fw_entry->data;
1300 firmware_size = fw_entry->size;
1302 if (firmware_size < 60) {
1303 printk("%s: firmware size too small for DSP code (%zu < 60).\n",
1304 __func__, firmware_size);
1305 release_firmware(fw_entry);
1306 return -ENOENT;
1309 /* a 32 bit checksum over the first 56 bytes of the DSP Code is stored
1310 at offset 56 of file, so use it to check if the firmware file is
1311 valid. */
1312 crc32_csum = crc32(~0L, firmware, 56) ^ ~0L;
1313 memcpy(&tmp, &firmware[56], 4);
1314 crc32_check = ntohl(tmp);
1315 if (crc32_csum != crc32_check) {
1316 printk("%s: crc32 check of DSP code failed (calculated "
1317 "0x%08x != 0x%08x in file), file invalid.\n",
1318 __func__, crc32_csum, crc32_check);
1319 release_firmware(fw_entry);
1320 return -ENOENT;
1322 memcpy(idstring, &firmware[36], 20);
1323 idstring[20] = '\0';
1324 printk(KERN_INFO "ttusb_dec: found DSP code \"%s\".\n", idstring);
1326 firmware_size_nl = htonl(firmware_size);
1327 memcpy(b0, &firmware_size_nl, 4);
1328 firmware_csum = crc16(~0, firmware, firmware_size) ^ ~0;
1329 firmware_csum_ns = htons(firmware_csum);
1330 memcpy(&b0[6], &firmware_csum_ns, 2);
1332 result = ttusb_dec_send_command(dec, 0x41, sizeof(b0), b0, NULL, NULL);
1334 if (result) {
1335 release_firmware(fw_entry);
1336 return result;
1339 trans_count = 0;
1340 j = 0;
1342 b = kmalloc(ARM_PACKET_SIZE, GFP_KERNEL);
1343 if (b == NULL) {
1344 release_firmware(fw_entry);
1345 return -ENOMEM;
1348 for (i = 0; i < firmware_size; i += COMMAND_PACKET_SIZE) {
1349 size = firmware_size - i;
1350 if (size > COMMAND_PACKET_SIZE)
1351 size = COMMAND_PACKET_SIZE;
1353 b[j + 0] = 0xaa;
1354 b[j + 1] = trans_count++;
1355 b[j + 2] = 0xf0;
1356 b[j + 3] = size;
1357 memcpy(&b[j + 4], &firmware[i], size);
1359 j += COMMAND_PACKET_SIZE + 4;
1361 if (j >= ARM_PACKET_SIZE) {
1362 result = usb_bulk_msg(dec->udev, dec->command_pipe, b,
1363 ARM_PACKET_SIZE, &actual_len,
1364 100);
1365 j = 0;
1366 } else if (size < COMMAND_PACKET_SIZE) {
1367 result = usb_bulk_msg(dec->udev, dec->command_pipe, b,
1368 j - COMMAND_PACKET_SIZE + size,
1369 &actual_len, 100);
1373 result = ttusb_dec_send_command(dec, 0x43, sizeof(b1), b1, NULL, NULL);
1375 release_firmware(fw_entry);
1376 kfree(b);
1378 return result;
1381 static int ttusb_dec_init_stb(struct ttusb_dec *dec)
1383 int result;
1384 unsigned int mode = 0, model = 0, version = 0;
1386 dprintk("%s\n", __func__);
1388 result = ttusb_dec_get_stb_state(dec, &mode, &model, &version);
1389 if (result)
1390 return result;
1392 if (!mode) {
1393 if (version == 0xABCDEFAB)
1394 printk(KERN_INFO "ttusb_dec: no version "
1395 "info in Firmware\n");
1396 else
1397 printk(KERN_INFO "ttusb_dec: Firmware "
1398 "%x.%02x%c%c\n",
1399 version >> 24, (version >> 16) & 0xff,
1400 (version >> 8) & 0xff, version & 0xff);
1402 result = ttusb_dec_boot_dsp(dec);
1403 if (result)
1404 return result;
1405 } else {
1406 /* We can't trust the USB IDs that some firmwares
1407 give the box */
1408 switch (model) {
1409 case 0x00070001:
1410 case 0x00070008:
1411 case 0x0007000c:
1412 ttusb_dec_set_model(dec, TTUSB_DEC3000S);
1413 break;
1414 case 0x00070009:
1415 case 0x00070013:
1416 ttusb_dec_set_model(dec, TTUSB_DEC2000T);
1417 break;
1418 case 0x00070011:
1419 ttusb_dec_set_model(dec, TTUSB_DEC2540T);
1420 break;
1421 default:
1422 printk(KERN_ERR "%s: unknown model returned "
1423 "by firmware (%08x) - please report\n",
1424 __func__, model);
1425 return -ENOENT;
1427 if (version >= 0x01770000)
1428 dec->can_playback = 1;
1430 return 0;
1433 static int ttusb_dec_init_dvb(struct ttusb_dec *dec)
1435 int result;
1437 dprintk("%s\n", __func__);
1439 if ((result = dvb_register_adapter(&dec->adapter,
1440 dec->model_name, THIS_MODULE,
1441 &dec->udev->dev,
1442 adapter_nr)) < 0) {
1443 printk("%s: dvb_register_adapter failed: error %d\n",
1444 __func__, result);
1446 return result;
1449 dec->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING;
1451 dec->demux.priv = (void *)dec;
1452 dec->demux.filternum = 31;
1453 dec->demux.feednum = 31;
1454 dec->demux.start_feed = ttusb_dec_start_feed;
1455 dec->demux.stop_feed = ttusb_dec_stop_feed;
1456 dec->demux.write_to_decoder = NULL;
1458 if ((result = dvb_dmx_init(&dec->demux)) < 0) {
1459 printk("%s: dvb_dmx_init failed: error %d\n", __func__,
1460 result);
1462 dvb_unregister_adapter(&dec->adapter);
1464 return result;
1467 dec->dmxdev.filternum = 32;
1468 dec->dmxdev.demux = &dec->demux.dmx;
1469 dec->dmxdev.capabilities = 0;
1471 if ((result = dvb_dmxdev_init(&dec->dmxdev, &dec->adapter)) < 0) {
1472 printk("%s: dvb_dmxdev_init failed: error %d\n",
1473 __func__, result);
1475 dvb_dmx_release(&dec->demux);
1476 dvb_unregister_adapter(&dec->adapter);
1478 return result;
1481 dec->frontend.source = DMX_FRONTEND_0;
1483 if ((result = dec->demux.dmx.add_frontend(&dec->demux.dmx,
1484 &dec->frontend)) < 0) {
1485 printk("%s: dvb_dmx_init failed: error %d\n", __func__,
1486 result);
1488 dvb_dmxdev_release(&dec->dmxdev);
1489 dvb_dmx_release(&dec->demux);
1490 dvb_unregister_adapter(&dec->adapter);
1492 return result;
1495 if ((result = dec->demux.dmx.connect_frontend(&dec->demux.dmx,
1496 &dec->frontend)) < 0) {
1497 printk("%s: dvb_dmx_init failed: error %d\n", __func__,
1498 result);
1500 dec->demux.dmx.remove_frontend(&dec->demux.dmx, &dec->frontend);
1501 dvb_dmxdev_release(&dec->dmxdev);
1502 dvb_dmx_release(&dec->demux);
1503 dvb_unregister_adapter(&dec->adapter);
1505 return result;
1508 dvb_net_init(&dec->adapter, &dec->dvb_net, &dec->demux.dmx);
1510 return 0;
1513 static void ttusb_dec_exit_dvb(struct ttusb_dec *dec)
1515 dprintk("%s\n", __func__);
1517 dvb_net_release(&dec->dvb_net);
1518 dec->demux.dmx.close(&dec->demux.dmx);
1519 dec->demux.dmx.remove_frontend(&dec->demux.dmx, &dec->frontend);
1520 dvb_dmxdev_release(&dec->dmxdev);
1521 dvb_dmx_release(&dec->demux);
1522 if (dec->fe) {
1523 dvb_unregister_frontend(dec->fe);
1524 if (dec->fe->ops.release)
1525 dec->fe->ops.release(dec->fe);
1527 dvb_unregister_adapter(&dec->adapter);
1530 static void ttusb_dec_exit_rc(struct ttusb_dec *dec)
1532 dprintk("%s\n", __func__);
1534 if (dec->rc_input_dev) {
1535 input_unregister_device(dec->rc_input_dev);
1536 dec->rc_input_dev = NULL;
1541 static void ttusb_dec_exit_usb(struct ttusb_dec *dec)
1543 int i;
1545 dprintk("%s\n", __func__);
1547 if (enable_rc) {
1548 /* we have to check whether the irq URB is already submitted.
1549 * As the irq is submitted after the interface is changed,
1550 * this is the best method i figured out.
1551 * Any others?*/
1552 if (dec->interface == TTUSB_DEC_INTERFACE_IN)
1553 usb_kill_urb(dec->irq_urb);
1555 usb_free_urb(dec->irq_urb);
1557 usb_free_coherent(dec->udev, IRQ_PACKET_SIZE,
1558 dec->irq_buffer, dec->irq_dma_handle);
1561 dec->iso_stream_count = 0;
1563 for (i = 0; i < ISO_BUF_COUNT; i++)
1564 usb_kill_urb(dec->iso_urb[i]);
1566 ttusb_dec_free_iso_urbs(dec);
1569 static void ttusb_dec_exit_tasklet(struct ttusb_dec *dec)
1571 struct list_head *item;
1572 struct urb_frame *frame;
1574 tasklet_kill(&dec->urb_tasklet);
1576 while ((item = dec->urb_frame_list.next) != &dec->urb_frame_list) {
1577 frame = list_entry(item, struct urb_frame, urb_frame_list);
1578 list_del(&frame->urb_frame_list);
1579 kfree(frame);
1583 static void ttusb_dec_init_filters(struct ttusb_dec *dec)
1585 INIT_LIST_HEAD(&dec->filter_info_list);
1586 spin_lock_init(&dec->filter_info_list_lock);
1589 static void ttusb_dec_exit_filters(struct ttusb_dec *dec)
1591 struct list_head *item;
1592 struct filter_info *finfo;
1594 while ((item = dec->filter_info_list.next) != &dec->filter_info_list) {
1595 finfo = list_entry(item, struct filter_info, filter_info_list);
1596 list_del(&finfo->filter_info_list);
1597 kfree(finfo);
1601 static int fe_send_command(struct dvb_frontend* fe, const u8 command,
1602 int param_length, const u8 params[],
1603 int *result_length, u8 cmd_result[])
1605 struct ttusb_dec* dec = fe->dvb->priv;
1606 return ttusb_dec_send_command(dec, command, param_length, params, result_length, cmd_result);
1609 static struct ttusbdecfe_config fe_config = {
1610 .send_command = fe_send_command
1613 static int ttusb_dec_probe(struct usb_interface *intf,
1614 const struct usb_device_id *id)
1616 struct usb_device *udev;
1617 struct ttusb_dec *dec;
1618 int result;
1620 dprintk("%s\n", __func__);
1622 udev = interface_to_usbdev(intf);
1624 if (!(dec = kzalloc(sizeof(struct ttusb_dec), GFP_KERNEL))) {
1625 printk("%s: couldn't allocate memory.\n", __func__);
1626 return -ENOMEM;
1629 usb_set_intfdata(intf, (void *)dec);
1631 switch (id->idProduct) {
1632 case 0x1006:
1633 ttusb_dec_set_model(dec, TTUSB_DEC3000S);
1634 break;
1636 case 0x1008:
1637 ttusb_dec_set_model(dec, TTUSB_DEC2000T);
1638 break;
1640 case 0x1009:
1641 ttusb_dec_set_model(dec, TTUSB_DEC2540T);
1642 break;
1645 dec->udev = udev;
1647 result = ttusb_dec_init_usb(dec);
1648 if (result)
1649 goto err_usb;
1650 result = ttusb_dec_init_stb(dec);
1651 if (result)
1652 goto err_stb;
1653 result = ttusb_dec_init_dvb(dec);
1654 if (result)
1655 goto err_stb;
1657 dec->adapter.priv = dec;
1658 switch (id->idProduct) {
1659 case 0x1006:
1660 dec->fe = ttusbdecfe_dvbs_attach(&fe_config);
1661 break;
1663 case 0x1008:
1664 case 0x1009:
1665 dec->fe = ttusbdecfe_dvbt_attach(&fe_config);
1666 break;
1669 if (dec->fe == NULL) {
1670 printk("dvb-ttusb-dec: A frontend driver was not found for device [%04x:%04x]\n",
1671 le16_to_cpu(dec->udev->descriptor.idVendor),
1672 le16_to_cpu(dec->udev->descriptor.idProduct));
1673 } else {
1674 if (dvb_register_frontend(&dec->adapter, dec->fe)) {
1675 printk("budget-ci: Frontend registration failed!\n");
1676 if (dec->fe->ops.release)
1677 dec->fe->ops.release(dec->fe);
1678 dec->fe = NULL;
1682 ttusb_dec_init_v_pes(dec);
1683 ttusb_dec_init_filters(dec);
1684 ttusb_dec_init_tasklet(dec);
1686 dec->active = 1;
1688 ttusb_dec_set_interface(dec, TTUSB_DEC_INTERFACE_IN);
1690 if (enable_rc)
1691 ttusb_init_rc(dec);
1693 return 0;
1694 err_stb:
1695 ttusb_dec_exit_usb(dec);
1696 err_usb:
1697 kfree(dec);
1698 return result;
1701 static void ttusb_dec_disconnect(struct usb_interface *intf)
1703 struct ttusb_dec *dec = usb_get_intfdata(intf);
1705 usb_set_intfdata(intf, NULL);
1707 dprintk("%s\n", __func__);
1709 if (dec->active) {
1710 ttusb_dec_exit_tasklet(dec);
1711 ttusb_dec_exit_filters(dec);
1712 if(enable_rc)
1713 ttusb_dec_exit_rc(dec);
1714 ttusb_dec_exit_usb(dec);
1715 ttusb_dec_exit_dvb(dec);
1718 kfree(dec);
1721 static void ttusb_dec_set_model(struct ttusb_dec *dec,
1722 enum ttusb_dec_model model)
1724 dec->model = model;
1726 switch (model) {
1727 case TTUSB_DEC2000T:
1728 dec->model_name = "DEC2000-t";
1729 dec->firmware_name = "dvb-ttusb-dec-2000t.fw";
1730 break;
1732 case TTUSB_DEC2540T:
1733 dec->model_name = "DEC2540-t";
1734 dec->firmware_name = "dvb-ttusb-dec-2540t.fw";
1735 break;
1737 case TTUSB_DEC3000S:
1738 dec->model_name = "DEC3000-s";
1739 dec->firmware_name = "dvb-ttusb-dec-3000s.fw";
1740 break;
1744 static struct usb_device_id ttusb_dec_table[] = {
1745 {USB_DEVICE(0x0b48, 0x1006)}, /* DEC3000-s */
1746 /*{USB_DEVICE(0x0b48, 0x1007)}, Unconfirmed */
1747 {USB_DEVICE(0x0b48, 0x1008)}, /* DEC2000-t */
1748 {USB_DEVICE(0x0b48, 0x1009)}, /* DEC2540-t */
1752 static struct usb_driver ttusb_dec_driver = {
1753 .name = "ttusb-dec",
1754 .probe = ttusb_dec_probe,
1755 .disconnect = ttusb_dec_disconnect,
1756 .id_table = ttusb_dec_table,
1759 module_usb_driver(ttusb_dec_driver);
1761 MODULE_AUTHOR("Alex Woods <linux-dvb@giblets.org>");
1762 MODULE_DESCRIPTION(DRIVER_NAME);
1763 MODULE_LICENSE("GPL");
1764 MODULE_DEVICE_TABLE(usb, ttusb_dec_table);