Merge tag 'regmap-fix-v4.9-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / media / usb / ttusb-dec / ttusb_dec.c
blob4e7671a3a1e4a2408c02d1e0a8c3d57038a60571
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)) {
232 * IR - Event
234 * this is an fact a bit too simple implementation;
235 * the box also reports a keyrepeat signal
236 * (with buffer[3] == 0x40) in an intervall of ~100ms.
237 * But to handle this correctly we had to imlemenent some
238 * kind of timer which signals a 'key up' event if no
239 * keyrepeat signal is received for lets say 200ms.
240 * this should/could be added later ...
241 * for now lets report each signal as a key down and up
243 if (buffer[4] - 1 < ARRAY_SIZE(rc_keys)) {
244 dprintk("%s:rc signal:%d\n", __func__, buffer[4]);
245 input_report_key(dec->rc_input_dev, rc_keys[buffer[4] - 1], 1);
246 input_sync(dec->rc_input_dev);
247 input_report_key(dec->rc_input_dev, rc_keys[buffer[4] - 1], 0);
248 input_sync(dec->rc_input_dev);
252 exit:
253 retval = usb_submit_urb(urb, GFP_ATOMIC);
254 if (retval)
255 printk("%s - usb_commit_urb failed with result: %d\n",
256 __func__, retval);
259 static u16 crc16(u16 crc, const u8 *buf, size_t len)
261 u16 tmp;
263 while (len--) {
264 crc ^= *buf++;
265 crc ^= (u8)crc >> 4;
266 tmp = (u8)crc;
267 crc ^= (tmp ^ (tmp << 1)) << 4;
269 return crc;
272 static int ttusb_dec_send_command(struct ttusb_dec *dec, const u8 command,
273 int param_length, const u8 params[],
274 int *result_length, u8 cmd_result[])
276 int result, actual_len, i;
277 u8 *b;
279 dprintk("%s\n", __func__);
281 b = kmalloc(COMMAND_PACKET_SIZE + 4, GFP_KERNEL);
282 if (!b)
283 return -ENOMEM;
285 if ((result = mutex_lock_interruptible(&dec->usb_mutex))) {
286 kfree(b);
287 printk("%s: Failed to lock usb mutex.\n", __func__);
288 return result;
291 b[0] = 0xaa;
292 b[1] = ++dec->trans_count;
293 b[2] = command;
294 b[3] = param_length;
296 if (params)
297 memcpy(&b[4], params, param_length);
299 if (debug) {
300 printk("%s: command: ", __func__);
301 for (i = 0; i < param_length + 4; i++)
302 printk("0x%02X ", b[i]);
303 printk("\n");
306 result = usb_bulk_msg(dec->udev, dec->command_pipe, b,
307 COMMAND_PACKET_SIZE + 4, &actual_len, 1000);
309 if (result) {
310 printk("%s: command bulk message failed: error %d\n",
311 __func__, result);
312 mutex_unlock(&dec->usb_mutex);
313 kfree(b);
314 return result;
317 result = usb_bulk_msg(dec->udev, dec->result_pipe, b,
318 COMMAND_PACKET_SIZE + 4, &actual_len, 1000);
320 if (result) {
321 printk("%s: result bulk message failed: error %d\n",
322 __func__, result);
323 mutex_unlock(&dec->usb_mutex);
324 kfree(b);
325 return result;
326 } else {
327 if (debug) {
328 printk("%s: result: ", __func__);
329 for (i = 0; i < actual_len; i++)
330 printk("0x%02X ", b[i]);
331 printk("\n");
334 if (result_length)
335 *result_length = b[3];
336 if (cmd_result && b[3] > 0)
337 memcpy(cmd_result, &b[4], b[3]);
339 mutex_unlock(&dec->usb_mutex);
341 kfree(b);
342 return 0;
346 static int ttusb_dec_get_stb_state (struct ttusb_dec *dec, unsigned int *mode,
347 unsigned int *model, unsigned int *version)
349 u8 c[COMMAND_PACKET_SIZE];
350 int c_length;
351 int result;
352 __be32 tmp;
354 dprintk("%s\n", __func__);
356 result = ttusb_dec_send_command(dec, 0x08, 0, NULL, &c_length, c);
357 if (result)
358 return result;
360 if (c_length >= 0x0c) {
361 if (mode != NULL) {
362 memcpy(&tmp, c, 4);
363 *mode = ntohl(tmp);
365 if (model != NULL) {
366 memcpy(&tmp, &c[4], 4);
367 *model = ntohl(tmp);
369 if (version != NULL) {
370 memcpy(&tmp, &c[8], 4);
371 *version = ntohl(tmp);
373 return 0;
374 } else {
375 return -ENOENT;
379 static int ttusb_dec_audio_pes2ts_cb(void *priv, unsigned char *data)
381 struct ttusb_dec *dec = priv;
383 dec->audio_filter->feed->cb.ts(data, 188, NULL, 0,
384 &dec->audio_filter->feed->feed.ts);
386 return 0;
389 static int ttusb_dec_video_pes2ts_cb(void *priv, unsigned char *data)
391 struct ttusb_dec *dec = priv;
393 dec->video_filter->feed->cb.ts(data, 188, NULL, 0,
394 &dec->video_filter->feed->feed.ts);
396 return 0;
399 static void ttusb_dec_set_pids(struct ttusb_dec *dec)
401 u8 b[] = { 0x00, 0x00, 0x00, 0x00,
402 0x00, 0x00, 0xff, 0xff,
403 0xff, 0xff, 0xff, 0xff };
405 __be16 pcr = htons(dec->pid[DMX_PES_PCR]);
406 __be16 audio = htons(dec->pid[DMX_PES_AUDIO]);
407 __be16 video = htons(dec->pid[DMX_PES_VIDEO]);
409 dprintk("%s\n", __func__);
411 memcpy(&b[0], &pcr, 2);
412 memcpy(&b[2], &audio, 2);
413 memcpy(&b[4], &video, 2);
415 ttusb_dec_send_command(dec, 0x50, sizeof(b), b, NULL, NULL);
417 dvb_filter_pes2ts_init(&dec->a_pes2ts, dec->pid[DMX_PES_AUDIO],
418 ttusb_dec_audio_pes2ts_cb, dec);
419 dvb_filter_pes2ts_init(&dec->v_pes2ts, dec->pid[DMX_PES_VIDEO],
420 ttusb_dec_video_pes2ts_cb, dec);
421 dec->v_pes_length = 0;
422 dec->v_pes_postbytes = 0;
425 static void ttusb_dec_process_pva(struct ttusb_dec *dec, u8 *pva, int length)
427 if (length < 8) {
428 printk("%s: packet too short - discarding\n", __func__);
429 return;
432 if (length > 8 + MAX_PVA_LENGTH) {
433 printk("%s: packet too long - discarding\n", __func__);
434 return;
437 switch (pva[2]) {
439 case 0x01: { /* VideoStream */
440 int prebytes = pva[5] & 0x03;
441 int postbytes = (pva[5] & 0x0c) >> 2;
442 __be16 v_pes_payload_length;
444 if (output_pva) {
445 dec->video_filter->feed->cb.ts(pva, length, NULL, 0,
446 &dec->video_filter->feed->feed.ts);
447 return;
450 if (dec->v_pes_postbytes > 0 &&
451 dec->v_pes_postbytes == prebytes) {
452 memcpy(&dec->v_pes[dec->v_pes_length],
453 &pva[12], prebytes);
455 dvb_filter_pes2ts(&dec->v_pes2ts, dec->v_pes,
456 dec->v_pes_length + prebytes, 1);
459 if (pva[5] & 0x10) {
460 dec->v_pes[7] = 0x80;
461 dec->v_pes[8] = 0x05;
463 dec->v_pes[9] = 0x21 | ((pva[8] & 0xc0) >> 5);
464 dec->v_pes[10] = ((pva[8] & 0x3f) << 2) |
465 ((pva[9] & 0xc0) >> 6);
466 dec->v_pes[11] = 0x01 |
467 ((pva[9] & 0x3f) << 2) |
468 ((pva[10] & 0x80) >> 6);
469 dec->v_pes[12] = ((pva[10] & 0x7f) << 1) |
470 ((pva[11] & 0xc0) >> 7);
471 dec->v_pes[13] = 0x01 | ((pva[11] & 0x7f) << 1);
473 memcpy(&dec->v_pes[14], &pva[12 + prebytes],
474 length - 12 - prebytes);
475 dec->v_pes_length = 14 + length - 12 - prebytes;
476 } else {
477 dec->v_pes[7] = 0x00;
478 dec->v_pes[8] = 0x00;
480 memcpy(&dec->v_pes[9], &pva[8], length - 8);
481 dec->v_pes_length = 9 + length - 8;
484 dec->v_pes_postbytes = postbytes;
486 if (dec->v_pes[9 + dec->v_pes[8]] == 0x00 &&
487 dec->v_pes[10 + dec->v_pes[8]] == 0x00 &&
488 dec->v_pes[11 + dec->v_pes[8]] == 0x01)
489 dec->v_pes[6] = 0x84;
490 else
491 dec->v_pes[6] = 0x80;
493 v_pes_payload_length = htons(dec->v_pes_length - 6 +
494 postbytes);
495 memcpy(&dec->v_pes[4], &v_pes_payload_length, 2);
497 if (postbytes == 0)
498 dvb_filter_pes2ts(&dec->v_pes2ts, dec->v_pes,
499 dec->v_pes_length, 1);
501 break;
504 case 0x02: /* MainAudioStream */
505 if (output_pva) {
506 dec->audio_filter->feed->cb.ts(pva, length, NULL, 0,
507 &dec->audio_filter->feed->feed.ts);
508 return;
511 dvb_filter_pes2ts(&dec->a_pes2ts, &pva[8], length - 8,
512 pva[5] & 0x10);
513 break;
515 default:
516 printk("%s: unknown PVA type: %02x.\n", __func__,
517 pva[2]);
518 break;
522 static void ttusb_dec_process_filter(struct ttusb_dec *dec, u8 *packet,
523 int length)
525 struct list_head *item;
526 struct filter_info *finfo;
527 struct dvb_demux_filter *filter = NULL;
528 unsigned long flags;
529 u8 sid;
531 sid = packet[1];
532 spin_lock_irqsave(&dec->filter_info_list_lock, flags);
533 for (item = dec->filter_info_list.next; item != &dec->filter_info_list;
534 item = item->next) {
535 finfo = list_entry(item, struct filter_info, filter_info_list);
536 if (finfo->stream_id == sid) {
537 filter = finfo->filter;
538 break;
541 spin_unlock_irqrestore(&dec->filter_info_list_lock, flags);
543 if (filter)
544 filter->feed->cb.sec(&packet[2], length - 2, NULL, 0,
545 &filter->filter);
548 static void ttusb_dec_process_packet(struct ttusb_dec *dec)
550 int i;
551 u16 csum = 0;
552 u16 packet_id;
554 if (dec->packet_length % 2) {
555 printk("%s: odd sized packet - discarding\n", __func__);
556 return;
559 for (i = 0; i < dec->packet_length; i += 2)
560 csum ^= ((dec->packet[i] << 8) + dec->packet[i + 1]);
562 if (csum) {
563 printk("%s: checksum failed - discarding\n", __func__);
564 return;
567 packet_id = dec->packet[dec->packet_length - 4] << 8;
568 packet_id += dec->packet[dec->packet_length - 3];
570 if ((packet_id != dec->next_packet_id) && dec->next_packet_id) {
571 printk("%s: warning: lost packets between %u and %u\n",
572 __func__, dec->next_packet_id - 1, packet_id);
575 if (packet_id == 0xffff)
576 dec->next_packet_id = 0x8000;
577 else
578 dec->next_packet_id = packet_id + 1;
580 switch (dec->packet_type) {
581 case TTUSB_DEC_PACKET_PVA:
582 if (dec->pva_stream_count)
583 ttusb_dec_process_pva(dec, dec->packet,
584 dec->packet_payload_length);
585 break;
587 case TTUSB_DEC_PACKET_SECTION:
588 if (dec->filter_stream_count)
589 ttusb_dec_process_filter(dec, dec->packet,
590 dec->packet_payload_length);
591 break;
593 case TTUSB_DEC_PACKET_EMPTY:
594 break;
598 static void swap_bytes(u8 *b, int length)
600 length -= length % 2;
601 for (; length; b += 2, length -= 2)
602 swap(*b, *(b + 1));
605 static void ttusb_dec_process_urb_frame(struct ttusb_dec *dec, u8 *b,
606 int length)
608 swap_bytes(b, length);
610 while (length) {
611 switch (dec->packet_state) {
613 case 0:
614 case 1:
615 case 2:
616 if (*b++ == 0xaa)
617 dec->packet_state++;
618 else
619 dec->packet_state = 0;
621 length--;
622 break;
624 case 3:
625 if (*b == 0x00) {
626 dec->packet_state++;
627 dec->packet_length = 0;
628 } else if (*b != 0xaa) {
629 dec->packet_state = 0;
632 b++;
633 length--;
634 break;
636 case 4:
637 dec->packet[dec->packet_length++] = *b++;
639 if (dec->packet_length == 2) {
640 if (dec->packet[0] == 'A' &&
641 dec->packet[1] == 'V') {
642 dec->packet_type =
643 TTUSB_DEC_PACKET_PVA;
644 dec->packet_state++;
645 } else if (dec->packet[0] == 'S') {
646 dec->packet_type =
647 TTUSB_DEC_PACKET_SECTION;
648 dec->packet_state++;
649 } else if (dec->packet[0] == 0x00) {
650 dec->packet_type =
651 TTUSB_DEC_PACKET_EMPTY;
652 dec->packet_payload_length = 2;
653 dec->packet_state = 7;
654 } else {
655 printk("%s: unknown packet type: "
656 "%02x%02x\n", __func__,
657 dec->packet[0], dec->packet[1]);
658 dec->packet_state = 0;
662 length--;
663 break;
665 case 5:
666 dec->packet[dec->packet_length++] = *b++;
668 if (dec->packet_type == TTUSB_DEC_PACKET_PVA &&
669 dec->packet_length == 8) {
670 dec->packet_state++;
671 dec->packet_payload_length = 8 +
672 (dec->packet[6] << 8) +
673 dec->packet[7];
674 } else if (dec->packet_type ==
675 TTUSB_DEC_PACKET_SECTION &&
676 dec->packet_length == 5) {
677 dec->packet_state++;
678 dec->packet_payload_length = 5 +
679 ((dec->packet[3] & 0x0f) << 8) +
680 dec->packet[4];
683 length--;
684 break;
686 case 6: {
687 int remainder = dec->packet_payload_length -
688 dec->packet_length;
690 if (length >= remainder) {
691 memcpy(dec->packet + dec->packet_length,
692 b, remainder);
693 dec->packet_length += remainder;
694 b += remainder;
695 length -= remainder;
696 dec->packet_state++;
697 } else {
698 memcpy(&dec->packet[dec->packet_length],
699 b, length);
700 dec->packet_length += length;
701 length = 0;
704 break;
707 case 7: {
708 int tail = 4;
710 dec->packet[dec->packet_length++] = *b++;
712 if (dec->packet_type == TTUSB_DEC_PACKET_SECTION &&
713 dec->packet_payload_length % 2)
714 tail++;
716 if (dec->packet_length ==
717 dec->packet_payload_length + tail) {
718 ttusb_dec_process_packet(dec);
719 dec->packet_state = 0;
722 length--;
723 break;
726 default:
727 printk("%s: illegal packet state encountered.\n",
728 __func__);
729 dec->packet_state = 0;
734 static void ttusb_dec_process_urb_frame_list(unsigned long data)
736 struct ttusb_dec *dec = (struct ttusb_dec *)data;
737 struct list_head *item;
738 struct urb_frame *frame;
739 unsigned long flags;
741 while (1) {
742 spin_lock_irqsave(&dec->urb_frame_list_lock, flags);
743 if ((item = dec->urb_frame_list.next) != &dec->urb_frame_list) {
744 frame = list_entry(item, struct urb_frame,
745 urb_frame_list);
746 list_del(&frame->urb_frame_list);
747 } else {
748 spin_unlock_irqrestore(&dec->urb_frame_list_lock,
749 flags);
750 return;
752 spin_unlock_irqrestore(&dec->urb_frame_list_lock, flags);
754 ttusb_dec_process_urb_frame(dec, frame->data, frame->length);
755 kfree(frame);
759 static void ttusb_dec_process_urb(struct urb *urb)
761 struct ttusb_dec *dec = urb->context;
763 if (!urb->status) {
764 int i;
766 for (i = 0; i < FRAMES_PER_ISO_BUF; i++) {
767 struct usb_iso_packet_descriptor *d;
768 u8 *b;
769 int length;
770 struct urb_frame *frame;
772 d = &urb->iso_frame_desc[i];
773 b = urb->transfer_buffer + d->offset;
774 length = d->actual_length;
776 if ((frame = kmalloc(sizeof(struct urb_frame),
777 GFP_ATOMIC))) {
778 unsigned long flags;
780 memcpy(frame->data, b, length);
781 frame->length = length;
783 spin_lock_irqsave(&dec->urb_frame_list_lock,
784 flags);
785 list_add_tail(&frame->urb_frame_list,
786 &dec->urb_frame_list);
787 spin_unlock_irqrestore(&dec->urb_frame_list_lock,
788 flags);
790 tasklet_schedule(&dec->urb_tasklet);
793 } else {
794 /* -ENOENT is expected when unlinking urbs */
795 if (urb->status != -ENOENT)
796 dprintk("%s: urb error: %d\n", __func__,
797 urb->status);
800 if (dec->iso_stream_count)
801 usb_submit_urb(urb, GFP_ATOMIC);
804 static void ttusb_dec_setup_urbs(struct ttusb_dec *dec)
806 int i, j, buffer_offset = 0;
808 dprintk("%s\n", __func__);
810 for (i = 0; i < ISO_BUF_COUNT; i++) {
811 int frame_offset = 0;
812 struct urb *urb = dec->iso_urb[i];
814 urb->dev = dec->udev;
815 urb->context = dec;
816 urb->complete = ttusb_dec_process_urb;
817 urb->pipe = dec->in_pipe;
818 urb->transfer_flags = URB_ISO_ASAP;
819 urb->interval = 1;
820 urb->number_of_packets = FRAMES_PER_ISO_BUF;
821 urb->transfer_buffer_length = ISO_FRAME_SIZE *
822 FRAMES_PER_ISO_BUF;
823 urb->transfer_buffer = dec->iso_buffer + buffer_offset;
824 buffer_offset += ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF;
826 for (j = 0; j < FRAMES_PER_ISO_BUF; j++) {
827 urb->iso_frame_desc[j].offset = frame_offset;
828 urb->iso_frame_desc[j].length = ISO_FRAME_SIZE;
829 frame_offset += ISO_FRAME_SIZE;
834 static void ttusb_dec_stop_iso_xfer(struct ttusb_dec *dec)
836 int i;
838 dprintk("%s\n", __func__);
840 if (mutex_lock_interruptible(&dec->iso_mutex))
841 return;
843 dec->iso_stream_count--;
845 if (!dec->iso_stream_count) {
846 for (i = 0; i < ISO_BUF_COUNT; i++)
847 usb_kill_urb(dec->iso_urb[i]);
850 mutex_unlock(&dec->iso_mutex);
853 /* Setting the interface of the DEC tends to take down the USB communications
854 * for a short period, so it's important not to call this function just before
855 * trying to talk to it.
857 static int ttusb_dec_set_interface(struct ttusb_dec *dec,
858 enum ttusb_dec_interface interface)
860 int result = 0;
861 u8 b[] = { 0x05 };
863 if (interface != dec->interface) {
864 switch (interface) {
865 case TTUSB_DEC_INTERFACE_INITIAL:
866 result = usb_set_interface(dec->udev, 0, 0);
867 break;
868 case TTUSB_DEC_INTERFACE_IN:
869 result = ttusb_dec_send_command(dec, 0x80, sizeof(b),
870 b, NULL, NULL);
871 if (result)
872 return result;
873 result = usb_set_interface(dec->udev, 0, 8);
874 break;
875 case TTUSB_DEC_INTERFACE_OUT:
876 result = usb_set_interface(dec->udev, 0, 1);
877 break;
880 if (result)
881 return result;
883 dec->interface = interface;
886 return 0;
889 static int ttusb_dec_start_iso_xfer(struct ttusb_dec *dec)
891 int i, result;
893 dprintk("%s\n", __func__);
895 if (mutex_lock_interruptible(&dec->iso_mutex))
896 return -EAGAIN;
898 if (!dec->iso_stream_count) {
899 ttusb_dec_setup_urbs(dec);
901 dec->packet_state = 0;
902 dec->v_pes_postbytes = 0;
903 dec->next_packet_id = 0;
905 for (i = 0; i < ISO_BUF_COUNT; i++) {
906 if ((result = usb_submit_urb(dec->iso_urb[i],
907 GFP_ATOMIC))) {
908 printk("%s: failed urb submission %d: "
909 "error %d\n", __func__, i, result);
911 while (i) {
912 usb_kill_urb(dec->iso_urb[i - 1]);
913 i--;
916 mutex_unlock(&dec->iso_mutex);
917 return result;
922 dec->iso_stream_count++;
924 mutex_unlock(&dec->iso_mutex);
926 return 0;
929 static int ttusb_dec_start_ts_feed(struct dvb_demux_feed *dvbdmxfeed)
931 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
932 struct ttusb_dec *dec = dvbdmx->priv;
933 u8 b0[] = { 0x05 };
934 int result = 0;
936 dprintk("%s\n", __func__);
938 dprintk(" ts_type:");
940 if (dvbdmxfeed->ts_type & TS_DECODER)
941 dprintk(" TS_DECODER");
943 if (dvbdmxfeed->ts_type & TS_PACKET)
944 dprintk(" TS_PACKET");
946 if (dvbdmxfeed->ts_type & TS_PAYLOAD_ONLY)
947 dprintk(" TS_PAYLOAD_ONLY");
949 dprintk("\n");
951 switch (dvbdmxfeed->pes_type) {
953 case DMX_PES_VIDEO:
954 dprintk(" pes_type: DMX_PES_VIDEO\n");
955 dec->pid[DMX_PES_PCR] = dvbdmxfeed->pid;
956 dec->pid[DMX_PES_VIDEO] = dvbdmxfeed->pid;
957 dec->video_filter = dvbdmxfeed->filter;
958 ttusb_dec_set_pids(dec);
959 break;
961 case DMX_PES_AUDIO:
962 dprintk(" pes_type: DMX_PES_AUDIO\n");
963 dec->pid[DMX_PES_AUDIO] = dvbdmxfeed->pid;
964 dec->audio_filter = dvbdmxfeed->filter;
965 ttusb_dec_set_pids(dec);
966 break;
968 case DMX_PES_TELETEXT:
969 dec->pid[DMX_PES_TELETEXT] = dvbdmxfeed->pid;
970 dprintk(" pes_type: DMX_PES_TELETEXT(not supported)\n");
971 return -ENOSYS;
973 case DMX_PES_PCR:
974 dprintk(" pes_type: DMX_PES_PCR\n");
975 dec->pid[DMX_PES_PCR] = dvbdmxfeed->pid;
976 ttusb_dec_set_pids(dec);
977 break;
979 case DMX_PES_OTHER:
980 dprintk(" pes_type: DMX_PES_OTHER(not supported)\n");
981 return -ENOSYS;
983 default:
984 dprintk(" pes_type: unknown (%d)\n", dvbdmxfeed->pes_type);
985 return -EINVAL;
989 result = ttusb_dec_send_command(dec, 0x80, sizeof(b0), b0, NULL, NULL);
990 if (result)
991 return result;
993 dec->pva_stream_count++;
994 return ttusb_dec_start_iso_xfer(dec);
997 static int ttusb_dec_start_sec_feed(struct dvb_demux_feed *dvbdmxfeed)
999 struct ttusb_dec *dec = dvbdmxfeed->demux->priv;
1000 u8 b0[] = { 0x00, 0x00, 0x00, 0x01,
1001 0x00, 0x00, 0x00, 0x00,
1002 0x00, 0x00, 0x00, 0x00,
1003 0x00, 0x00, 0x00, 0x00,
1004 0x00, 0xff, 0x00, 0x00,
1005 0x00, 0x00, 0x00, 0x00,
1006 0x00, 0x00, 0x00, 0x00,
1007 0x00 };
1008 __be16 pid;
1009 u8 c[COMMAND_PACKET_SIZE];
1010 int c_length;
1011 int result;
1012 struct filter_info *finfo;
1013 unsigned long flags;
1014 u8 x = 1;
1016 dprintk("%s\n", __func__);
1018 pid = htons(dvbdmxfeed->pid);
1019 memcpy(&b0[0], &pid, 2);
1020 memcpy(&b0[4], &x, 1);
1021 memcpy(&b0[5], &dvbdmxfeed->filter->filter.filter_value[0], 1);
1023 result = ttusb_dec_send_command(dec, 0x60, sizeof(b0), b0,
1024 &c_length, c);
1026 if (!result) {
1027 if (c_length == 2) {
1028 if (!(finfo = kmalloc(sizeof(struct filter_info),
1029 GFP_ATOMIC)))
1030 return -ENOMEM;
1032 finfo->stream_id = c[1];
1033 finfo->filter = dvbdmxfeed->filter;
1035 spin_lock_irqsave(&dec->filter_info_list_lock, flags);
1036 list_add_tail(&finfo->filter_info_list,
1037 &dec->filter_info_list);
1038 spin_unlock_irqrestore(&dec->filter_info_list_lock,
1039 flags);
1041 dvbdmxfeed->priv = finfo;
1043 dec->filter_stream_count++;
1044 return ttusb_dec_start_iso_xfer(dec);
1047 return -EAGAIN;
1048 } else
1049 return result;
1052 static int ttusb_dec_start_feed(struct dvb_demux_feed *dvbdmxfeed)
1054 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
1056 dprintk("%s\n", __func__);
1058 if (!dvbdmx->dmx.frontend)
1059 return -EINVAL;
1061 dprintk(" pid: 0x%04X\n", dvbdmxfeed->pid);
1063 switch (dvbdmxfeed->type) {
1065 case DMX_TYPE_TS:
1066 return ttusb_dec_start_ts_feed(dvbdmxfeed);
1067 break;
1069 case DMX_TYPE_SEC:
1070 return ttusb_dec_start_sec_feed(dvbdmxfeed);
1071 break;
1073 default:
1074 dprintk(" type: unknown (%d)\n", dvbdmxfeed->type);
1075 return -EINVAL;
1080 static int ttusb_dec_stop_ts_feed(struct dvb_demux_feed *dvbdmxfeed)
1082 struct ttusb_dec *dec = dvbdmxfeed->demux->priv;
1083 u8 b0[] = { 0x00 };
1085 ttusb_dec_send_command(dec, 0x81, sizeof(b0), b0, NULL, NULL);
1087 dec->pva_stream_count--;
1089 ttusb_dec_stop_iso_xfer(dec);
1091 return 0;
1094 static int ttusb_dec_stop_sec_feed(struct dvb_demux_feed *dvbdmxfeed)
1096 struct ttusb_dec *dec = dvbdmxfeed->demux->priv;
1097 u8 b0[] = { 0x00, 0x00 };
1098 struct filter_info *finfo = (struct filter_info *)dvbdmxfeed->priv;
1099 unsigned long flags;
1101 b0[1] = finfo->stream_id;
1102 spin_lock_irqsave(&dec->filter_info_list_lock, flags);
1103 list_del(&finfo->filter_info_list);
1104 spin_unlock_irqrestore(&dec->filter_info_list_lock, flags);
1105 kfree(finfo);
1106 ttusb_dec_send_command(dec, 0x62, sizeof(b0), b0, NULL, NULL);
1108 dec->filter_stream_count--;
1110 ttusb_dec_stop_iso_xfer(dec);
1112 return 0;
1115 static int ttusb_dec_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
1117 dprintk("%s\n", __func__);
1119 switch (dvbdmxfeed->type) {
1120 case DMX_TYPE_TS:
1121 return ttusb_dec_stop_ts_feed(dvbdmxfeed);
1122 break;
1124 case DMX_TYPE_SEC:
1125 return ttusb_dec_stop_sec_feed(dvbdmxfeed);
1126 break;
1129 return 0;
1132 static void ttusb_dec_free_iso_urbs(struct ttusb_dec *dec)
1134 int i;
1136 dprintk("%s\n", __func__);
1138 for (i = 0; i < ISO_BUF_COUNT; i++)
1139 usb_free_urb(dec->iso_urb[i]);
1141 pci_free_consistent(NULL,
1142 ISO_FRAME_SIZE * (FRAMES_PER_ISO_BUF *
1143 ISO_BUF_COUNT),
1144 dec->iso_buffer, dec->iso_dma_handle);
1147 static int ttusb_dec_alloc_iso_urbs(struct ttusb_dec *dec)
1149 int i;
1151 dprintk("%s\n", __func__);
1153 dec->iso_buffer = pci_zalloc_consistent(NULL,
1154 ISO_FRAME_SIZE * (FRAMES_PER_ISO_BUF * ISO_BUF_COUNT),
1155 &dec->iso_dma_handle);
1157 if (!dec->iso_buffer) {
1158 dprintk("%s: pci_alloc_consistent - not enough memory\n",
1159 __func__);
1160 return -ENOMEM;
1163 for (i = 0; i < ISO_BUF_COUNT; i++) {
1164 struct urb *urb;
1166 if (!(urb = usb_alloc_urb(FRAMES_PER_ISO_BUF, GFP_ATOMIC))) {
1167 ttusb_dec_free_iso_urbs(dec);
1168 return -ENOMEM;
1171 dec->iso_urb[i] = urb;
1174 ttusb_dec_setup_urbs(dec);
1176 return 0;
1179 static void ttusb_dec_init_tasklet(struct ttusb_dec *dec)
1181 spin_lock_init(&dec->urb_frame_list_lock);
1182 INIT_LIST_HEAD(&dec->urb_frame_list);
1183 tasklet_init(&dec->urb_tasklet, ttusb_dec_process_urb_frame_list,
1184 (unsigned long)dec);
1187 static int ttusb_init_rc( struct ttusb_dec *dec)
1189 struct input_dev *input_dev;
1190 u8 b[] = { 0x00, 0x01 };
1191 int i;
1192 int err;
1194 usb_make_path(dec->udev, dec->rc_phys, sizeof(dec->rc_phys));
1195 strlcat(dec->rc_phys, "/input0", sizeof(dec->rc_phys));
1197 input_dev = input_allocate_device();
1198 if (!input_dev)
1199 return -ENOMEM;
1201 input_dev->name = "ttusb_dec remote control";
1202 input_dev->phys = dec->rc_phys;
1203 input_dev->evbit[0] = BIT_MASK(EV_KEY);
1204 input_dev->keycodesize = sizeof(u16);
1205 input_dev->keycodemax = 0x1a;
1206 input_dev->keycode = rc_keys;
1208 for (i = 0; i < ARRAY_SIZE(rc_keys); i++)
1209 set_bit(rc_keys[i], input_dev->keybit);
1211 err = input_register_device(input_dev);
1212 if (err) {
1213 input_free_device(input_dev);
1214 return err;
1217 dec->rc_input_dev = input_dev;
1218 if (usb_submit_urb(dec->irq_urb, GFP_KERNEL))
1219 printk("%s: usb_submit_urb failed\n",__func__);
1220 /* enable irq pipe */
1221 ttusb_dec_send_command(dec,0xb0,sizeof(b),b,NULL,NULL);
1223 return 0;
1226 static void ttusb_dec_init_v_pes(struct ttusb_dec *dec)
1228 dprintk("%s\n", __func__);
1230 dec->v_pes[0] = 0x00;
1231 dec->v_pes[1] = 0x00;
1232 dec->v_pes[2] = 0x01;
1233 dec->v_pes[3] = 0xe0;
1236 static int ttusb_dec_init_usb(struct ttusb_dec *dec)
1238 int result;
1240 dprintk("%s\n", __func__);
1242 mutex_init(&dec->usb_mutex);
1243 mutex_init(&dec->iso_mutex);
1245 dec->command_pipe = usb_sndbulkpipe(dec->udev, COMMAND_PIPE);
1246 dec->result_pipe = usb_rcvbulkpipe(dec->udev, RESULT_PIPE);
1247 dec->in_pipe = usb_rcvisocpipe(dec->udev, IN_PIPE);
1248 dec->out_pipe = usb_sndisocpipe(dec->udev, OUT_PIPE);
1249 dec->irq_pipe = usb_rcvintpipe(dec->udev, IRQ_PIPE);
1251 if(enable_rc) {
1252 dec->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
1253 if(!dec->irq_urb) {
1254 return -ENOMEM;
1256 dec->irq_buffer = usb_alloc_coherent(dec->udev,IRQ_PACKET_SIZE,
1257 GFP_KERNEL, &dec->irq_dma_handle);
1258 if(!dec->irq_buffer) {
1259 usb_free_urb(dec->irq_urb);
1260 return -ENOMEM;
1262 usb_fill_int_urb(dec->irq_urb, dec->udev,dec->irq_pipe,
1263 dec->irq_buffer, IRQ_PACKET_SIZE,
1264 ttusb_dec_handle_irq, dec, 1);
1265 dec->irq_urb->transfer_dma = dec->irq_dma_handle;
1266 dec->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1269 result = ttusb_dec_alloc_iso_urbs(dec);
1270 if (result) {
1271 usb_free_urb(dec->irq_urb);
1272 usb_free_coherent(dec->udev, IRQ_PACKET_SIZE,
1273 dec->irq_buffer, dec->irq_dma_handle);
1275 return result;
1278 static int ttusb_dec_boot_dsp(struct ttusb_dec *dec)
1280 int i, j, actual_len, result, size, trans_count;
1281 u8 b0[] = { 0x00, 0x00, 0x00, 0x00,
1282 0x00, 0x00, 0x00, 0x00,
1283 0x61, 0x00 };
1284 u8 b1[] = { 0x61 };
1285 u8 *b;
1286 char idstring[21];
1287 const u8 *firmware = NULL;
1288 size_t firmware_size = 0;
1289 u16 firmware_csum = 0;
1290 __be16 firmware_csum_ns;
1291 __be32 firmware_size_nl;
1292 u32 crc32_csum, crc32_check;
1293 __be32 tmp;
1294 const struct firmware *fw_entry = NULL;
1296 dprintk("%s\n", __func__);
1298 result = request_firmware(&fw_entry, dec->firmware_name, &dec->udev->dev);
1299 if (result) {
1300 printk(KERN_ERR "%s: Firmware (%s) unavailable.\n",
1301 __func__, dec->firmware_name);
1302 return result;
1305 firmware = fw_entry->data;
1306 firmware_size = fw_entry->size;
1308 if (firmware_size < 60) {
1309 printk("%s: firmware size too small for DSP code (%zu < 60).\n",
1310 __func__, firmware_size);
1311 release_firmware(fw_entry);
1312 return -ENOENT;
1315 /* a 32 bit checksum over the first 56 bytes of the DSP Code is stored
1316 at offset 56 of file, so use it to check if the firmware file is
1317 valid. */
1318 crc32_csum = crc32(~0L, firmware, 56) ^ ~0L;
1319 memcpy(&tmp, &firmware[56], 4);
1320 crc32_check = ntohl(tmp);
1321 if (crc32_csum != crc32_check) {
1322 printk("%s: crc32 check of DSP code failed (calculated "
1323 "0x%08x != 0x%08x in file), file invalid.\n",
1324 __func__, crc32_csum, crc32_check);
1325 release_firmware(fw_entry);
1326 return -ENOENT;
1328 memcpy(idstring, &firmware[36], 20);
1329 idstring[20] = '\0';
1330 printk(KERN_INFO "ttusb_dec: found DSP code \"%s\".\n", idstring);
1332 firmware_size_nl = htonl(firmware_size);
1333 memcpy(b0, &firmware_size_nl, 4);
1334 firmware_csum = crc16(~0, firmware, firmware_size) ^ ~0;
1335 firmware_csum_ns = htons(firmware_csum);
1336 memcpy(&b0[6], &firmware_csum_ns, 2);
1338 result = ttusb_dec_send_command(dec, 0x41, sizeof(b0), b0, NULL, NULL);
1340 if (result) {
1341 release_firmware(fw_entry);
1342 return result;
1345 trans_count = 0;
1346 j = 0;
1348 b = kmalloc(ARM_PACKET_SIZE, GFP_KERNEL);
1349 if (b == NULL) {
1350 release_firmware(fw_entry);
1351 return -ENOMEM;
1354 for (i = 0; i < firmware_size; i += COMMAND_PACKET_SIZE) {
1355 size = firmware_size - i;
1356 if (size > COMMAND_PACKET_SIZE)
1357 size = COMMAND_PACKET_SIZE;
1359 b[j + 0] = 0xaa;
1360 b[j + 1] = trans_count++;
1361 b[j + 2] = 0xf0;
1362 b[j + 3] = size;
1363 memcpy(&b[j + 4], &firmware[i], size);
1365 j += COMMAND_PACKET_SIZE + 4;
1367 if (j >= ARM_PACKET_SIZE) {
1368 result = usb_bulk_msg(dec->udev, dec->command_pipe, b,
1369 ARM_PACKET_SIZE, &actual_len,
1370 100);
1371 j = 0;
1372 } else if (size < COMMAND_PACKET_SIZE) {
1373 result = usb_bulk_msg(dec->udev, dec->command_pipe, b,
1374 j - COMMAND_PACKET_SIZE + size,
1375 &actual_len, 100);
1379 result = ttusb_dec_send_command(dec, 0x43, sizeof(b1), b1, NULL, NULL);
1381 release_firmware(fw_entry);
1382 kfree(b);
1384 return result;
1387 static int ttusb_dec_init_stb(struct ttusb_dec *dec)
1389 int result;
1390 unsigned int mode = 0, model = 0, version = 0;
1392 dprintk("%s\n", __func__);
1394 result = ttusb_dec_get_stb_state(dec, &mode, &model, &version);
1395 if (result)
1396 return result;
1398 if (!mode) {
1399 if (version == 0xABCDEFAB)
1400 printk(KERN_INFO "ttusb_dec: no version "
1401 "info in Firmware\n");
1402 else
1403 printk(KERN_INFO "ttusb_dec: Firmware "
1404 "%x.%02x%c%c\n",
1405 version >> 24, (version >> 16) & 0xff,
1406 (version >> 8) & 0xff, version & 0xff);
1408 result = ttusb_dec_boot_dsp(dec);
1409 if (result)
1410 return result;
1411 } else {
1412 /* We can't trust the USB IDs that some firmwares
1413 give the box */
1414 switch (model) {
1415 case 0x00070001:
1416 case 0x00070008:
1417 case 0x0007000c:
1418 ttusb_dec_set_model(dec, TTUSB_DEC3000S);
1419 break;
1420 case 0x00070009:
1421 case 0x00070013:
1422 ttusb_dec_set_model(dec, TTUSB_DEC2000T);
1423 break;
1424 case 0x00070011:
1425 ttusb_dec_set_model(dec, TTUSB_DEC2540T);
1426 break;
1427 default:
1428 printk(KERN_ERR "%s: unknown model returned "
1429 "by firmware (%08x) - please report\n",
1430 __func__, model);
1431 return -ENOENT;
1433 if (version >= 0x01770000)
1434 dec->can_playback = 1;
1436 return 0;
1439 static int ttusb_dec_init_dvb(struct ttusb_dec *dec)
1441 int result;
1443 dprintk("%s\n", __func__);
1445 if ((result = dvb_register_adapter(&dec->adapter,
1446 dec->model_name, THIS_MODULE,
1447 &dec->udev->dev,
1448 adapter_nr)) < 0) {
1449 printk("%s: dvb_register_adapter failed: error %d\n",
1450 __func__, result);
1452 return result;
1455 dec->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING;
1457 dec->demux.priv = (void *)dec;
1458 dec->demux.filternum = 31;
1459 dec->demux.feednum = 31;
1460 dec->demux.start_feed = ttusb_dec_start_feed;
1461 dec->demux.stop_feed = ttusb_dec_stop_feed;
1462 dec->demux.write_to_decoder = NULL;
1464 if ((result = dvb_dmx_init(&dec->demux)) < 0) {
1465 printk("%s: dvb_dmx_init failed: error %d\n", __func__,
1466 result);
1468 dvb_unregister_adapter(&dec->adapter);
1470 return result;
1473 dec->dmxdev.filternum = 32;
1474 dec->dmxdev.demux = &dec->demux.dmx;
1475 dec->dmxdev.capabilities = 0;
1477 if ((result = dvb_dmxdev_init(&dec->dmxdev, &dec->adapter)) < 0) {
1478 printk("%s: dvb_dmxdev_init failed: error %d\n",
1479 __func__, result);
1481 dvb_dmx_release(&dec->demux);
1482 dvb_unregister_adapter(&dec->adapter);
1484 return result;
1487 dec->frontend.source = DMX_FRONTEND_0;
1489 if ((result = dec->demux.dmx.add_frontend(&dec->demux.dmx,
1490 &dec->frontend)) < 0) {
1491 printk("%s: dvb_dmx_init failed: error %d\n", __func__,
1492 result);
1494 dvb_dmxdev_release(&dec->dmxdev);
1495 dvb_dmx_release(&dec->demux);
1496 dvb_unregister_adapter(&dec->adapter);
1498 return result;
1501 if ((result = dec->demux.dmx.connect_frontend(&dec->demux.dmx,
1502 &dec->frontend)) < 0) {
1503 printk("%s: dvb_dmx_init failed: error %d\n", __func__,
1504 result);
1506 dec->demux.dmx.remove_frontend(&dec->demux.dmx, &dec->frontend);
1507 dvb_dmxdev_release(&dec->dmxdev);
1508 dvb_dmx_release(&dec->demux);
1509 dvb_unregister_adapter(&dec->adapter);
1511 return result;
1514 dvb_net_init(&dec->adapter, &dec->dvb_net, &dec->demux.dmx);
1516 return 0;
1519 static void ttusb_dec_exit_dvb(struct ttusb_dec *dec)
1521 dprintk("%s\n", __func__);
1523 dvb_net_release(&dec->dvb_net);
1524 dec->demux.dmx.close(&dec->demux.dmx);
1525 dec->demux.dmx.remove_frontend(&dec->demux.dmx, &dec->frontend);
1526 dvb_dmxdev_release(&dec->dmxdev);
1527 dvb_dmx_release(&dec->demux);
1528 if (dec->fe) {
1529 dvb_unregister_frontend(dec->fe);
1530 if (dec->fe->ops.release)
1531 dec->fe->ops.release(dec->fe);
1533 dvb_unregister_adapter(&dec->adapter);
1536 static void ttusb_dec_exit_rc(struct ttusb_dec *dec)
1538 dprintk("%s\n", __func__);
1540 if (dec->rc_input_dev) {
1541 input_unregister_device(dec->rc_input_dev);
1542 dec->rc_input_dev = NULL;
1547 static void ttusb_dec_exit_usb(struct ttusb_dec *dec)
1549 int i;
1551 dprintk("%s\n", __func__);
1553 if (enable_rc) {
1554 /* we have to check whether the irq URB is already submitted.
1555 * As the irq is submitted after the interface is changed,
1556 * this is the best method i figured out.
1557 * Any others?*/
1558 if (dec->interface == TTUSB_DEC_INTERFACE_IN)
1559 usb_kill_urb(dec->irq_urb);
1561 usb_free_urb(dec->irq_urb);
1563 usb_free_coherent(dec->udev, IRQ_PACKET_SIZE,
1564 dec->irq_buffer, dec->irq_dma_handle);
1567 dec->iso_stream_count = 0;
1569 for (i = 0; i < ISO_BUF_COUNT; i++)
1570 usb_kill_urb(dec->iso_urb[i]);
1572 ttusb_dec_free_iso_urbs(dec);
1575 static void ttusb_dec_exit_tasklet(struct ttusb_dec *dec)
1577 struct list_head *item;
1578 struct urb_frame *frame;
1580 tasklet_kill(&dec->urb_tasklet);
1582 while ((item = dec->urb_frame_list.next) != &dec->urb_frame_list) {
1583 frame = list_entry(item, struct urb_frame, urb_frame_list);
1584 list_del(&frame->urb_frame_list);
1585 kfree(frame);
1589 static void ttusb_dec_init_filters(struct ttusb_dec *dec)
1591 INIT_LIST_HEAD(&dec->filter_info_list);
1592 spin_lock_init(&dec->filter_info_list_lock);
1595 static void ttusb_dec_exit_filters(struct ttusb_dec *dec)
1597 struct list_head *item;
1598 struct filter_info *finfo;
1600 while ((item = dec->filter_info_list.next) != &dec->filter_info_list) {
1601 finfo = list_entry(item, struct filter_info, filter_info_list);
1602 list_del(&finfo->filter_info_list);
1603 kfree(finfo);
1607 static int fe_send_command(struct dvb_frontend* fe, const u8 command,
1608 int param_length, const u8 params[],
1609 int *result_length, u8 cmd_result[])
1611 struct ttusb_dec* dec = fe->dvb->priv;
1612 return ttusb_dec_send_command(dec, command, param_length, params, result_length, cmd_result);
1615 static const struct ttusbdecfe_config fe_config = {
1616 .send_command = fe_send_command
1619 static int ttusb_dec_probe(struct usb_interface *intf,
1620 const struct usb_device_id *id)
1622 struct usb_device *udev;
1623 struct ttusb_dec *dec;
1624 int result;
1626 dprintk("%s\n", __func__);
1628 udev = interface_to_usbdev(intf);
1630 if (!(dec = kzalloc(sizeof(struct ttusb_dec), GFP_KERNEL))) {
1631 printk("%s: couldn't allocate memory.\n", __func__);
1632 return -ENOMEM;
1635 usb_set_intfdata(intf, (void *)dec);
1637 switch (id->idProduct) {
1638 case 0x1006:
1639 ttusb_dec_set_model(dec, TTUSB_DEC3000S);
1640 break;
1642 case 0x1008:
1643 ttusb_dec_set_model(dec, TTUSB_DEC2000T);
1644 break;
1646 case 0x1009:
1647 ttusb_dec_set_model(dec, TTUSB_DEC2540T);
1648 break;
1651 dec->udev = udev;
1653 result = ttusb_dec_init_usb(dec);
1654 if (result)
1655 goto err_usb;
1656 result = ttusb_dec_init_stb(dec);
1657 if (result)
1658 goto err_stb;
1659 result = ttusb_dec_init_dvb(dec);
1660 if (result)
1661 goto err_stb;
1663 dec->adapter.priv = dec;
1664 switch (id->idProduct) {
1665 case 0x1006:
1666 dec->fe = ttusbdecfe_dvbs_attach(&fe_config);
1667 break;
1669 case 0x1008:
1670 case 0x1009:
1671 dec->fe = ttusbdecfe_dvbt_attach(&fe_config);
1672 break;
1675 if (dec->fe == NULL) {
1676 printk("dvb-ttusb-dec: A frontend driver was not found for device [%04x:%04x]\n",
1677 le16_to_cpu(dec->udev->descriptor.idVendor),
1678 le16_to_cpu(dec->udev->descriptor.idProduct));
1679 } else {
1680 if (dvb_register_frontend(&dec->adapter, dec->fe)) {
1681 printk("budget-ci: Frontend registration failed!\n");
1682 if (dec->fe->ops.release)
1683 dec->fe->ops.release(dec->fe);
1684 dec->fe = NULL;
1688 ttusb_dec_init_v_pes(dec);
1689 ttusb_dec_init_filters(dec);
1690 ttusb_dec_init_tasklet(dec);
1692 dec->active = 1;
1694 ttusb_dec_set_interface(dec, TTUSB_DEC_INTERFACE_IN);
1696 if (enable_rc)
1697 ttusb_init_rc(dec);
1699 return 0;
1700 err_stb:
1701 ttusb_dec_exit_usb(dec);
1702 err_usb:
1703 kfree(dec);
1704 return result;
1707 static void ttusb_dec_disconnect(struct usb_interface *intf)
1709 struct ttusb_dec *dec = usb_get_intfdata(intf);
1711 usb_set_intfdata(intf, NULL);
1713 dprintk("%s\n", __func__);
1715 if (dec->active) {
1716 ttusb_dec_exit_tasklet(dec);
1717 ttusb_dec_exit_filters(dec);
1718 if(enable_rc)
1719 ttusb_dec_exit_rc(dec);
1720 ttusb_dec_exit_usb(dec);
1721 ttusb_dec_exit_dvb(dec);
1724 kfree(dec);
1727 static void ttusb_dec_set_model(struct ttusb_dec *dec,
1728 enum ttusb_dec_model model)
1730 dec->model = model;
1732 switch (model) {
1733 case TTUSB_DEC2000T:
1734 dec->model_name = "DEC2000-t";
1735 dec->firmware_name = "dvb-ttusb-dec-2000t.fw";
1736 break;
1738 case TTUSB_DEC2540T:
1739 dec->model_name = "DEC2540-t";
1740 dec->firmware_name = "dvb-ttusb-dec-2540t.fw";
1741 break;
1743 case TTUSB_DEC3000S:
1744 dec->model_name = "DEC3000-s";
1745 dec->firmware_name = "dvb-ttusb-dec-3000s.fw";
1746 break;
1750 static struct usb_device_id ttusb_dec_table[] = {
1751 {USB_DEVICE(0x0b48, 0x1006)}, /* DEC3000-s */
1752 /*{USB_DEVICE(0x0b48, 0x1007)}, Unconfirmed */
1753 {USB_DEVICE(0x0b48, 0x1008)}, /* DEC2000-t */
1754 {USB_DEVICE(0x0b48, 0x1009)}, /* DEC2540-t */
1758 static struct usb_driver ttusb_dec_driver = {
1759 .name = "ttusb-dec",
1760 .probe = ttusb_dec_probe,
1761 .disconnect = ttusb_dec_disconnect,
1762 .id_table = ttusb_dec_table,
1765 module_usb_driver(ttusb_dec_driver);
1767 MODULE_AUTHOR("Alex Woods <linux-dvb@giblets.org>");
1768 MODULE_DESCRIPTION(DRIVER_NAME);
1769 MODULE_LICENSE("GPL");
1770 MODULE_DEVICE_TABLE(usb, ttusb_dec_table);