1 /*
2  * TTUSB DEC Driver
3  *
4  * Copyright (C) 2003-2004 Alex Woods <linux-dvb@giblets.org>
5  * IR support by Peter Beutner <p.beutner@gmx.net>
6  *
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.
11  *
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.
16  *
17  */
18 
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>
30 
31 #include <linux/mutex.h>
32 
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"
38 
39 static int debug;
40 static int output_pva;
41 static int enable_rc;
42 
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)");
49 
50 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
51 
52 #define dprintk	if (debug) printk
53 
54 #define DRIVER_NAME		"TechnoTrend/Hauppauge DEC USB"
55 
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
61 
62 #define COMMAND_PACKET_SIZE	0x3c
63 #define ARM_PACKET_SIZE		0x1000
64 #define IRQ_PACKET_SIZE		0x8
65 
66 #define ISO_BUF_COUNT		0x04
67 #define FRAMES_PER_ISO_BUF	0x04
68 #define ISO_FRAME_SIZE		0x0380
69 
70 #define	MAX_PVA_LENGTH		6144
71 
72 enum ttusb_dec_model {
73 	TTUSB_DEC2000T,
74 	TTUSB_DEC2540T,
75 	TTUSB_DEC3000S
76 };
77 
78 enum ttusb_dec_packet_type {
79 	TTUSB_DEC_PACKET_PVA,
80 	TTUSB_DEC_PACKET_SECTION,
81 	TTUSB_DEC_PACKET_EMPTY
82 };
83 
84 enum ttusb_dec_interface {
85 	TTUSB_DEC_INTERFACE_INITIAL,
86 	TTUSB_DEC_INTERFACE_IN,
87 	TTUSB_DEC_INTERFACE_OUT
88 };
89 
90 typedef int (dvb_filter_pes2ts_cb_t) (void *, unsigned char *);
91 
92 struct dvb_filter_pes2ts {
93 	unsigned char buf[188];
94 	unsigned char cc;
95 	dvb_filter_pes2ts_cb_t *cb;
96 	void *priv;
97 };
98 
99 struct ttusb_dec {
100 	enum ttusb_dec_model		model;
101 	char				*model_name;
102 	char				*firmware_name;
103 	int				can_playback;
104 
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;
112 
113 	u16			pid[DMX_PES_OTHER];
114 
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;
125 
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;
133 
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;
140 
141 	int				pva_stream_count;
142 	int				filter_stream_count;
143 
144 	struct dvb_filter_pes2ts	a_pes2ts;
145 	struct dvb_filter_pes2ts	v_pes2ts;
146 
147 	u8			v_pes[16 + MAX_PVA_LENGTH];
148 	int			v_pes_length;
149 	int			v_pes_postbytes;
150 
151 	struct list_head	urb_frame_list;
152 	struct tasklet_struct	urb_tasklet;
153 	spinlock_t		urb_frame_list_lock;
154 
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;
159 
160 	struct input_dev	*rc_input_dev;
161 	char			rc_phys[64];
162 
163 	int			active; /* Loaded successfully */
164 };
165 
166 struct urb_frame {
167 	u8			data[ISO_FRAME_SIZE];
168 	int			length;
169 	struct list_head	urb_frame_list;
170 };
171 
172 struct filter_info {
173 	u8			stream_id;
174 	struct dvb_demux_filter	*filter;
175 	struct list_head	filter_info_list;
176 };
177 
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
205 };
206 
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)
210 {
211 	unsigned char *buf=p2ts->buf;
212 
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;
219 }
220 
221 static int dvb_filter_pes2ts(struct dvb_filter_pes2ts *p2ts,
222 			     unsigned char *pes, int len, int payload_start)
223 {
224 	unsigned char *buf=p2ts->buf;
225 	int ret=0, rest;
226 
227 	//len=6+((pes[4]<<8)|pes[5]);
228 
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;
240 	}
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);
249 	}
250 	buf[4]=rest;
251 	memcpy(buf+5+rest, pes, len);
252 	return p2ts->cb(p2ts->priv, buf);
253 }
254 
255 static void ttusb_dec_set_model(struct ttusb_dec *dec,
256 				enum ttusb_dec_model model);
257 
258 static void ttusb_dec_handle_irq( struct urb *urb)
259 {
260 	struct ttusb_dec *dec = urb->context;
261 	char *buffer = dec->irq_buffer;
262 	int retval;
263 
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;
279 	}
280 
281 	if ((buffer[0] == 0x1) && (buffer[2] == 0x15))  {
282 		/*
283 		 * IR - Event
284 		 *
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
293 		 */
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);
300 		}
301 	}
302 
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);
308 }
309 
310 static u16 crc16(u16 crc, const u8 *buf, size_t len)
311 {
312 	u16 tmp;
313 
314 	while (len--) {
315 		crc ^= *buf++;
316 		crc ^= (u8)crc >> 4;
317 		tmp = (u8)crc;
318 		crc ^= (tmp ^ (tmp << 1)) << 4;
319 	}
320 	return crc;
321 }
322 
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[])
326 {
327 	int result, actual_len;
328 	u8 *b;
329 
330 	dprintk("%s\n", __func__);
331 
332 	b = kmalloc(COMMAND_PACKET_SIZE + 4, GFP_KERNEL);
333 	if (!b)
334 		return -ENOMEM;
335 
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;
340 	}
341 
342 	b[0] = 0xaa;
343 	b[1] = ++dec->trans_count;
344 	b[2] = command;
345 	b[3] = param_length;
346 
347 	if (params)
348 		memcpy(&b[4], params, param_length);
349 
350 	if (debug) {
351 		printk(KERN_DEBUG "%s: command: %*ph\n",
352 		       __func__, param_length, b);
353 	}
354 
355 	result = usb_bulk_msg(dec->udev, dec->command_pipe, b,
356 			      COMMAND_PACKET_SIZE + 4, &actual_len, 1000);
357 
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;
364 	}
365 
366 	result = usb_bulk_msg(dec->udev, dec->result_pipe, b,
367 			      COMMAND_PACKET_SIZE + 4, &actual_len, 1000);
368 
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);
379 		}
380 
381 		if (result_length)
382 			*result_length = b[3];
383 		if (cmd_result && b[3] > 0)
384 			memcpy(cmd_result, &b[4], b[3]);
385 
386 		mutex_unlock(&dec->usb_mutex);
387 
388 		kfree(b);
389 		return 0;
390 	}
391 }
392 
393 static int ttusb_dec_get_stb_state (struct ttusb_dec *dec, unsigned int *mode,
394 				    unsigned int *model, unsigned int *version)
395 {
396 	u8 c[COMMAND_PACKET_SIZE];
397 	int c_length;
398 	int result;
399 	__be32 tmp;
400 
401 	dprintk("%s\n", __func__);
402 
403 	result = ttusb_dec_send_command(dec, 0x08, 0, NULL, &c_length, c);
404 	if (result)
405 		return result;
406 
407 	if (c_length >= 0x0c) {
408 		if (mode != NULL) {
409 			memcpy(&tmp, c, 4);
410 			*mode = ntohl(tmp);
411 		}
412 		if (model != NULL) {
413 			memcpy(&tmp, &c[4], 4);
414 			*model = ntohl(tmp);
415 		}
416 		if (version != NULL) {
417 			memcpy(&tmp, &c[8], 4);
418 			*version = ntohl(tmp);
419 		}
420 		return 0;
421 	} else {
422 		return -ENOENT;
423 	}
424 }
425 
426 static int ttusb_dec_audio_pes2ts_cb(void *priv, unsigned char *data)
427 {
428 	struct ttusb_dec *dec = priv;
429 
430 	dec->audio_filter->feed->cb.ts(data, 188, NULL, 0,
431 				       &dec->audio_filter->feed->feed.ts, NULL);
432 
433 	return 0;
434 }
435 
436 static int ttusb_dec_video_pes2ts_cb(void *priv, unsigned char *data)
437 {
438 	struct ttusb_dec *dec = priv;
439 
440 	dec->video_filter->feed->cb.ts(data, 188, NULL, 0,
441 				       &dec->video_filter->feed->feed.ts, NULL);
442 
443 	return 0;
444 }
445 
446 static void ttusb_dec_set_pids(struct ttusb_dec *dec)
447 {
448 	u8 b[] = { 0x00, 0x00, 0x00, 0x00,
449 		   0x00, 0x00, 0xff, 0xff,
450 		   0xff, 0xff, 0xff, 0xff };
451 
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]);
455 
456 	dprintk("%s\n", __func__);
457 
458 	memcpy(&b[0], &pcr, 2);
459 	memcpy(&b[2], &audio, 2);
460 	memcpy(&b[4], &video, 2);
461 
462 	ttusb_dec_send_command(dec, 0x50, sizeof(b), b, NULL, NULL);
463 
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;
470 }
471 
472 static void ttusb_dec_process_pva(struct ttusb_dec *dec, u8 *pva, int length)
473 {
474 	if (length < 8) {
475 		printk("%s: packet too short - discarding\n", __func__);
476 		return;
477 	}
478 
479 	if (length > 8 + MAX_PVA_LENGTH) {
480 		printk("%s: packet too long - discarding\n", __func__);
481 		return;
482 	}
483 
484 	switch (pva[2]) {
485 
486 	case 0x01: {		/* VideoStream */
487 		int prebytes = pva[5] & 0x03;
488 		int postbytes = (pva[5] & 0x0c) >> 2;
489 		__be16 v_pes_payload_length;
490 
491 		if (output_pva) {
492 			dec->video_filter->feed->cb.ts(pva, length, NULL, 0,
493 				&dec->video_filter->feed->feed.ts, NULL);
494 			return;
495 		}
496 
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);
501 
502 			dvb_filter_pes2ts(&dec->v_pes2ts, dec->v_pes,
503 					  dec->v_pes_length + prebytes, 1);
504 		}
505 
506 		if (pva[5] & 0x10) {
507 			dec->v_pes[7] = 0x80;
508 			dec->v_pes[8] = 0x05;
509 
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);
519 
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;
526 
527 			memcpy(&dec->v_pes[9], &pva[8], length - 8);
528 			dec->v_pes_length = 9 + length - 8;
529 		}
530 
531 		dec->v_pes_postbytes = postbytes;
532 
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;
539 
540 		v_pes_payload_length = htons(dec->v_pes_length - 6 +
541 					     postbytes);
542 		memcpy(&dec->v_pes[4], &v_pes_payload_length, 2);
543 
544 		if (postbytes == 0)
545 			dvb_filter_pes2ts(&dec->v_pes2ts, dec->v_pes,
546 					  dec->v_pes_length, 1);
547 
548 		break;
549 	}
550 
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, NULL);
555 			return;
556 		}
557 
558 		dvb_filter_pes2ts(&dec->a_pes2ts, &pva[8], length - 8,
559 				  pva[5] & 0x10);
560 		break;
561 
562 	default:
563 		printk("%s: unknown PVA type: %02x.\n", __func__,
564 		       pva[2]);
565 		break;
566 	}
567 }
568 
569 static void ttusb_dec_process_filter(struct ttusb_dec *dec, u8 *packet,
570 				     int length)
571 {
572 	struct list_head *item;
573 	struct filter_info *finfo;
574 	struct dvb_demux_filter *filter = NULL;
575 	unsigned long flags;
576 	u8 sid;
577 
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;
586 		}
587 	}
588 	spin_unlock_irqrestore(&dec->filter_info_list_lock, flags);
589 
590 	if (filter)
591 		filter->feed->cb.sec(&packet[2], length - 2, NULL, 0,
592 				     &filter->filter, NULL);
593 }
594 
595 static void ttusb_dec_process_packet(struct ttusb_dec *dec)
596 {
597 	int i;
598 	u16 csum = 0;
599 	u16 packet_id;
600 
601 	if (dec->packet_length % 2) {
602 		printk("%s: odd sized packet - discarding\n", __func__);
603 		return;
604 	}
605 
606 	for (i = 0; i < dec->packet_length; i += 2)
607 		csum ^= ((dec->packet[i] << 8) + dec->packet[i + 1]);
608 
609 	if (csum) {
610 		printk("%s: checksum failed - discarding\n", __func__);
611 		return;
612 	}
613 
614 	packet_id = dec->packet[dec->packet_length - 4] << 8;
615 	packet_id += dec->packet[dec->packet_length - 3];
616 
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);
620 	}
621 
622 	if (packet_id == 0xffff)
623 		dec->next_packet_id = 0x8000;
624 	else
625 		dec->next_packet_id = packet_id + 1;
626 
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;
633 
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;
639 
640 	case TTUSB_DEC_PACKET_EMPTY:
641 		break;
642 	}
643 }
644 
645 static void swap_bytes(u8 *b, int length)
646 {
647 	length -= length % 2;
648 	for (; length; b += 2, length -= 2)
649 		swap(*b, *(b + 1));
650 }
651 
652 static void ttusb_dec_process_urb_frame(struct ttusb_dec *dec, u8 *b,
653 					int length)
654 {
655 	swap_bytes(b, length);
656 
657 	while (length) {
658 		switch (dec->packet_state) {
659 
660 		case 0:
661 		case 1:
662 		case 2:
663 			if (*b++ == 0xaa)
664 				dec->packet_state++;
665 			else
666 				dec->packet_state = 0;
667 
668 			length--;
669 			break;
670 
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;
677 			}
678 
679 			b++;
680 			length--;
681 			break;
682 
683 		case 4:
684 			dec->packet[dec->packet_length++] = *b++;
685 
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;
706 				}
707 			}
708 
709 			length--;
710 			break;
711 
712 		case 5:
713 			dec->packet[dec->packet_length++] = *b++;
714 
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];
728 			}
729 
730 			length--;
731 			break;
732 
733 		case 6: {
734 			int remainder = dec->packet_payload_length -
735 					dec->packet_length;
736 
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;
749 			}
750 
751 			break;
752 		}
753 
754 		case 7: {
755 			int tail = 4;
756 
757 			dec->packet[dec->packet_length++] = *b++;
758 
759 			if (dec->packet_type == TTUSB_DEC_PACKET_SECTION &&
760 			    dec->packet_payload_length % 2)
761 				tail++;
762 
763 			if (dec->packet_length ==
764 			    dec->packet_payload_length + tail) {
765 				ttusb_dec_process_packet(dec);
766 				dec->packet_state = 0;
767 			}
768 
769 			length--;
770 			break;
771 		}
772 
773 		default:
774 			printk("%s: illegal packet state encountered.\n",
775 			       __func__);
776 			dec->packet_state = 0;
777 		}
778 	}
779 }
780 
781 static void ttusb_dec_process_urb_frame_list(unsigned long data)
782 {
783 	struct ttusb_dec *dec = (struct ttusb_dec *)data;
784 	struct list_head *item;
785 	struct urb_frame *frame;
786 	unsigned long flags;
787 
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;
798 		}
799 		spin_unlock_irqrestore(&dec->urb_frame_list_lock, flags);
800 
801 		ttusb_dec_process_urb_frame(dec, frame->data, frame->length);
802 		kfree(frame);
803 	}
804 }
805 
806 static void ttusb_dec_process_urb(struct urb *urb)
807 {
808 	struct ttusb_dec *dec = urb->context;
809 
810 	if (!urb->status) {
811 		int i;
812 
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;
818 
819 			d = &urb->iso_frame_desc[i];
820 			b = urb->transfer_buffer + d->offset;
821 			length = d->actual_length;
822 
823 			if ((frame = kmalloc(sizeof(struct urb_frame),
824 					     GFP_ATOMIC))) {
825 				unsigned long flags;
826 
827 				memcpy(frame->data, b, length);
828 				frame->length = length;
829 
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);
836 
837 				tasklet_schedule(&dec->urb_tasklet);
838 			}
839 		}
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);
845 	}
846 
847 	if (dec->iso_stream_count)
848 		usb_submit_urb(urb, GFP_ATOMIC);
849 }
850 
851 static void ttusb_dec_setup_urbs(struct ttusb_dec *dec)
852 {
853 	int i, j, buffer_offset = 0;
854 
855 	dprintk("%s\n", __func__);
856 
857 	for (i = 0; i < ISO_BUF_COUNT; i++) {
858 		int frame_offset = 0;
859 		struct urb *urb = dec->iso_urb[i];
860 
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;
872 
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;
877 		}
878 	}
879 }
880 
881 static void ttusb_dec_stop_iso_xfer(struct ttusb_dec *dec)
882 {
883 	int i;
884 
885 	dprintk("%s\n", __func__);
886 
887 	if (mutex_lock_interruptible(&dec->iso_mutex))
888 		return;
889 
890 	dec->iso_stream_count--;
891 
892 	if (!dec->iso_stream_count) {
893 		for (i = 0; i < ISO_BUF_COUNT; i++)
894 			usb_kill_urb(dec->iso_urb[i]);
895 	}
896 
897 	mutex_unlock(&dec->iso_mutex);
898 }
899 
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.
903  */
904 static int ttusb_dec_set_interface(struct ttusb_dec *dec,
905 				   enum ttusb_dec_interface interface)
906 {
907 	int result = 0;
908 	u8 b[] = { 0x05 };
909 
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;
925 		}
926 
927 		if (result)
928 			return result;
929 
930 		dec->interface = interface;
931 	}
932 
933 	return 0;
934 }
935 
936 static int ttusb_dec_start_iso_xfer(struct ttusb_dec *dec)
937 {
938 	int i, result;
939 
940 	dprintk("%s\n", __func__);
941 
942 	if (mutex_lock_interruptible(&dec->iso_mutex))
943 		return -EAGAIN;
944 
945 	if (!dec->iso_stream_count) {
946 		ttusb_dec_setup_urbs(dec);
947 
948 		dec->packet_state = 0;
949 		dec->v_pes_postbytes = 0;
950 		dec->next_packet_id = 0;
951 
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);
957 
958 				while (i) {
959 					usb_kill_urb(dec->iso_urb[i - 1]);
960 					i--;
961 				}
962 
963 				mutex_unlock(&dec->iso_mutex);
964 				return result;
965 			}
966 		}
967 	}
968 
969 	dec->iso_stream_count++;
970 
971 	mutex_unlock(&dec->iso_mutex);
972 
973 	return 0;
974 }
975 
976 static int ttusb_dec_start_ts_feed(struct dvb_demux_feed *dvbdmxfeed)
977 {
978 	struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
979 	struct ttusb_dec *dec = dvbdmx->priv;
980 	u8 b0[] = { 0x05 };
981 	int result = 0;
982 
983 	dprintk("%s\n", __func__);
984 
985 	dprintk("  ts_type:");
986 
987 	if (dvbdmxfeed->ts_type & TS_DECODER)
988 		dprintk(" TS_DECODER");
989 
990 	if (dvbdmxfeed->ts_type & TS_PACKET)
991 		dprintk(" TS_PACKET");
992 
993 	if (dvbdmxfeed->ts_type & TS_PAYLOAD_ONLY)
994 		dprintk(" TS_PAYLOAD_ONLY");
995 
996 	dprintk("\n");
997 
998 	switch (dvbdmxfeed->pes_type) {
999 
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;
1007 
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;
1014 
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;
1019 
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;
1025 
1026 	case DMX_PES_OTHER:
1027 		dprintk("  pes_type: DMX_PES_OTHER(not supported)\n");
1028 		return -ENOSYS;
1029 
1030 	default:
1031 		dprintk("  pes_type: unknown (%d)\n", dvbdmxfeed->pes_type);
1032 		return -EINVAL;
1033 
1034 	}
1035 
1036 	result = ttusb_dec_send_command(dec, 0x80, sizeof(b0), b0, NULL, NULL);
1037 	if (result)
1038 		return result;
1039 
1040 	dec->pva_stream_count++;
1041 	return ttusb_dec_start_iso_xfer(dec);
1042 }
1043 
1044 static int ttusb_dec_start_sec_feed(struct dvb_demux_feed *dvbdmxfeed)
1045 {
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;
1062 
1063 	dprintk("%s\n", __func__);
1064 
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);
1069 
1070 	result = ttusb_dec_send_command(dec, 0x60, sizeof(b0), b0,
1071 					&c_length, c);
1072 
1073 	if (!result) {
1074 		if (c_length == 2) {
1075 			if (!(finfo = kmalloc(sizeof(struct filter_info),
1076 					      GFP_ATOMIC)))
1077 				return -ENOMEM;
1078 
1079 			finfo->stream_id = c[1];
1080 			finfo->filter = dvbdmxfeed->filter;
1081 
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);
1087 
1088 			dvbdmxfeed->priv = finfo;
1089 
1090 			dec->filter_stream_count++;
1091 			return ttusb_dec_start_iso_xfer(dec);
1092 		}
1093 
1094 		return -EAGAIN;
1095 	} else
1096 		return result;
1097 }
1098 
1099 static int ttusb_dec_start_feed(struct dvb_demux_feed *dvbdmxfeed)
1100 {
1101 	struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
1102 
1103 	dprintk("%s\n", __func__);
1104 
1105 	if (!dvbdmx->dmx.frontend)
1106 		return -EINVAL;
1107 
1108 	dprintk("  pid: 0x%04X\n", dvbdmxfeed->pid);
1109 
1110 	switch (dvbdmxfeed->type) {
1111 
1112 	case DMX_TYPE_TS:
1113 		return ttusb_dec_start_ts_feed(dvbdmxfeed);
1114 		break;
1115 
1116 	case DMX_TYPE_SEC:
1117 		return ttusb_dec_start_sec_feed(dvbdmxfeed);
1118 		break;
1119 
1120 	default:
1121 		dprintk("  type: unknown (%d)\n", dvbdmxfeed->type);
1122 		return -EINVAL;
1123 
1124 	}
1125 }
1126 
1127 static int ttusb_dec_stop_ts_feed(struct dvb_demux_feed *dvbdmxfeed)
1128 {
1129 	struct ttusb_dec *dec = dvbdmxfeed->demux->priv;
1130 	u8 b0[] = { 0x00 };
1131 
1132 	ttusb_dec_send_command(dec, 0x81, sizeof(b0), b0, NULL, NULL);
1133 
1134 	dec->pva_stream_count--;
1135 
1136 	ttusb_dec_stop_iso_xfer(dec);
1137 
1138 	return 0;
1139 }
1140 
1141 static int ttusb_dec_stop_sec_feed(struct dvb_demux_feed *dvbdmxfeed)
1142 {
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;
1147 
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);
1154 
1155 	dec->filter_stream_count--;
1156 
1157 	ttusb_dec_stop_iso_xfer(dec);
1158 
1159 	return 0;
1160 }
1161 
1162 static int ttusb_dec_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
1163 {
1164 	dprintk("%s\n", __func__);
1165 
1166 	switch (dvbdmxfeed->type) {
1167 	case DMX_TYPE_TS:
1168 		return ttusb_dec_stop_ts_feed(dvbdmxfeed);
1169 		break;
1170 
1171 	case DMX_TYPE_SEC:
1172 		return ttusb_dec_stop_sec_feed(dvbdmxfeed);
1173 		break;
1174 	}
1175 
1176 	return 0;
1177 }
1178 
1179 static void ttusb_dec_free_iso_urbs(struct ttusb_dec *dec)
1180 {
1181 	int i;
1182 
1183 	dprintk("%s\n", __func__);
1184 
1185 	for (i = 0; i < ISO_BUF_COUNT; i++)
1186 		usb_free_urb(dec->iso_urb[i]);
1187 	kfree(dec->iso_buffer);
1188 }
1189 
1190 static int ttusb_dec_alloc_iso_urbs(struct ttusb_dec *dec)
1191 {
1192 	int i;
1193 
1194 	dprintk("%s\n", __func__);
1195 
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;
1200 
1201 	for (i = 0; i < ISO_BUF_COUNT; i++) {
1202 		struct urb *urb;
1203 
1204 		if (!(urb = usb_alloc_urb(FRAMES_PER_ISO_BUF, GFP_ATOMIC))) {
1205 			ttusb_dec_free_iso_urbs(dec);
1206 			return -ENOMEM;
1207 		}
1208 
1209 		dec->iso_urb[i] = urb;
1210 	}
1211 
1212 	ttusb_dec_setup_urbs(dec);
1213 
1214 	return 0;
1215 }
1216 
1217 static void ttusb_dec_init_tasklet(struct ttusb_dec *dec)
1218 {
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);
1223 }
1224 
1225 static int ttusb_init_rc( struct ttusb_dec *dec)
1226 {
1227 	struct input_dev *input_dev;
1228 	u8 b[] = { 0x00, 0x01 };
1229 	int i;
1230 	int err;
1231 
1232 	usb_make_path(dec->udev, dec->rc_phys, sizeof(dec->rc_phys));
1233 	strlcat(dec->rc_phys, "/input0", sizeof(dec->rc_phys));
1234 
1235 	input_dev = input_allocate_device();
1236 	if (!input_dev)
1237 		return -ENOMEM;
1238 
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;
1245 
1246 	for (i = 0; i < ARRAY_SIZE(rc_keys); i++)
1247 		  set_bit(rc_keys[i], input_dev->keybit);
1248 
1249 	err = input_register_device(input_dev);
1250 	if (err) {
1251 		input_free_device(input_dev);
1252 		return err;
1253 	}
1254 
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);
1260 
1261 	return 0;
1262 }
1263 
1264 static void ttusb_dec_init_v_pes(struct ttusb_dec *dec)
1265 {
1266 	dprintk("%s\n", __func__);
1267 
1268 	dec->v_pes[0] = 0x00;
1269 	dec->v_pes[1] = 0x00;
1270 	dec->v_pes[2] = 0x01;
1271 	dec->v_pes[3] = 0xe0;
1272 }
1273 
1274 static int ttusb_dec_init_usb(struct ttusb_dec *dec)
1275 {
1276 	int result;
1277 
1278 	dprintk("%s\n", __func__);
1279 
1280 	mutex_init(&dec->usb_mutex);
1281 	mutex_init(&dec->iso_mutex);
1282 
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);
1288 
1289 	if(enable_rc) {
1290 		dec->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
1291 		if(!dec->irq_urb) {
1292 			return -ENOMEM;
1293 		}
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;
1299 		}
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;
1305 	}
1306 
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);
1312 	}
1313 	return result;
1314 }
1315 
1316 static int ttusb_dec_boot_dsp(struct ttusb_dec *dec)
1317 {
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;
1333 
1334 	dprintk("%s\n", __func__);
1335 
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;
1341 	}
1342 
1343 	firmware = fw_entry->data;
1344 	firmware_size = fw_entry->size;
1345 
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;
1351 	}
1352 
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;
1364 	}
1365 	memcpy(idstring, &firmware[36], 20);
1366 	idstring[20] = '\0';
1367 	printk(KERN_INFO "ttusb_dec: found DSP code \"%s\".\n", idstring);
1368 
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);
1374 
1375 	result = ttusb_dec_send_command(dec, 0x41, sizeof(b0), b0, NULL, NULL);
1376 
1377 	if (result) {
1378 		release_firmware(fw_entry);
1379 		return result;
1380 	}
1381 
1382 	trans_count = 0;
1383 	j = 0;
1384 
1385 	b = kmalloc(ARM_PACKET_SIZE, GFP_KERNEL);
1386 	if (b == NULL) {
1387 		release_firmware(fw_entry);
1388 		return -ENOMEM;
1389 	}
1390 
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;
1395 
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);
1401 
1402 		j += COMMAND_PACKET_SIZE + 4;
1403 
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);
1413 		}
1414 	}
1415 
1416 	result = ttusb_dec_send_command(dec, 0x43, sizeof(b1), b1, NULL, NULL);
1417 
1418 	release_firmware(fw_entry);
1419 	kfree(b);
1420 
1421 	return result;
1422 }
1423 
1424 static int ttusb_dec_init_stb(struct ttusb_dec *dec)
1425 {
1426 	int result;
1427 	unsigned int mode = 0, model = 0, version = 0;
1428 
1429 	dprintk("%s\n", __func__);
1430 
1431 	result = ttusb_dec_get_stb_state(dec, &mode, &model, &version);
1432 	if (result)
1433 		return result;
1434 
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);
1442 
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;
1466 		}
1467 		if (version >= 0x01770000)
1468 			dec->can_playback = 1;
1469 	}
1470 	return 0;
1471 }
1472 
1473 static int ttusb_dec_init_dvb(struct ttusb_dec *dec)
1474 {
1475 	int result;
1476 
1477 	dprintk("%s\n", __func__);
1478 
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);
1485 
1486 		return result;
1487 	}
1488 
1489 	dec->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING;
1490 
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;
1497 
1498 	if ((result = dvb_dmx_init(&dec->demux)) < 0) {
1499 		printk("%s: dvb_dmx_init failed: error %d\n", __func__,
1500 		       result);
1501 
1502 		dvb_unregister_adapter(&dec->adapter);
1503 
1504 		return result;
1505 	}
1506 
1507 	dec->dmxdev.filternum = 32;
1508 	dec->dmxdev.demux = &dec->demux.dmx;
1509 	dec->dmxdev.capabilities = 0;
1510 
1511 	if ((result = dvb_dmxdev_init(&dec->dmxdev, &dec->adapter)) < 0) {
1512 		printk("%s: dvb_dmxdev_init failed: error %d\n",
1513 		       __func__, result);
1514 
1515 		dvb_dmx_release(&dec->demux);
1516 		dvb_unregister_adapter(&dec->adapter);
1517 
1518 		return result;
1519 	}
1520 
1521 	dec->frontend.source = DMX_FRONTEND_0;
1522 
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);
1527 
1528 		dvb_dmxdev_release(&dec->dmxdev);
1529 		dvb_dmx_release(&dec->demux);
1530 		dvb_unregister_adapter(&dec->adapter);
1531 
1532 		return result;
1533 	}
1534 
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);
1539 
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);
1544 
1545 		return result;
1546 	}
1547 
1548 	dvb_net_init(&dec->adapter, &dec->dvb_net, &dec->demux.dmx);
1549 
1550 	return 0;
1551 }
1552 
1553 static void ttusb_dec_exit_dvb(struct ttusb_dec *dec)
1554 {
1555 	dprintk("%s\n", __func__);
1556 
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);
1566 	}
1567 	dvb_unregister_adapter(&dec->adapter);
1568 }
1569 
1570 static void ttusb_dec_exit_rc(struct ttusb_dec *dec)
1571 {
1572 	dprintk("%s\n", __func__);
1573 
1574 	if (dec->rc_input_dev) {
1575 		input_unregister_device(dec->rc_input_dev);
1576 		dec->rc_input_dev = NULL;
1577 	}
1578 }
1579 
1580 
1581 static void ttusb_dec_exit_usb(struct ttusb_dec *dec)
1582 {
1583 	int i;
1584 
1585 	dprintk("%s\n", __func__);
1586 
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);
1594 
1595 		usb_free_urb(dec->irq_urb);
1596 
1597 		usb_free_coherent(dec->udev, IRQ_PACKET_SIZE,
1598 				  dec->irq_buffer, dec->irq_dma_handle);
1599 	}
1600 
1601 	dec->iso_stream_count = 0;
1602 
1603 	for (i = 0; i < ISO_BUF_COUNT; i++)
1604 		usb_kill_urb(dec->iso_urb[i]);
1605 
1606 	ttusb_dec_free_iso_urbs(dec);
1607 }
1608 
1609 static void ttusb_dec_exit_tasklet(struct ttusb_dec *dec)
1610 {
1611 	struct list_head *item;
1612 	struct urb_frame *frame;
1613 
1614 	tasklet_kill(&dec->urb_tasklet);
1615 
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);
1620 	}
1621 }
1622 
1623 static void ttusb_dec_init_filters(struct ttusb_dec *dec)
1624 {
1625 	INIT_LIST_HEAD(&dec->filter_info_list);
1626 	spin_lock_init(&dec->filter_info_list_lock);
1627 }
1628 
1629 static void ttusb_dec_exit_filters(struct ttusb_dec *dec)
1630 {
1631 	struct list_head *item;
1632 	struct filter_info *finfo;
1633 
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);
1638 	}
1639 }
1640 
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[])
1644 {
1645 	struct ttusb_dec* dec = fe->dvb->priv;
1646 	return ttusb_dec_send_command(dec, command, param_length, params, result_length, cmd_result);
1647 }
1648 
1649 static const struct ttusbdecfe_config fe_config = {
1650 	.send_command = fe_send_command
1651 };
1652 
1653 static int ttusb_dec_probe(struct usb_interface *intf,
1654 			   const struct usb_device_id *id)
1655 {
1656 	struct usb_device *udev;
1657 	struct ttusb_dec *dec;
1658 	int result;
1659 
1660 	dprintk("%s\n", __func__);
1661 
1662 	udev = interface_to_usbdev(intf);
1663 
1664 	if (!(dec = kzalloc(sizeof(struct ttusb_dec), GFP_KERNEL))) {
1665 		printk("%s: couldn't allocate memory.\n", __func__);
1666 		return -ENOMEM;
1667 	}
1668 
1669 	usb_set_intfdata(intf, (void *)dec);
1670 
1671 	switch (id->idProduct) {
1672 	case 0x1006:
1673 		ttusb_dec_set_model(dec, TTUSB_DEC3000S);
1674 		break;
1675 
1676 	case 0x1008:
1677 		ttusb_dec_set_model(dec, TTUSB_DEC2000T);
1678 		break;
1679 
1680 	case 0x1009:
1681 		ttusb_dec_set_model(dec, TTUSB_DEC2540T);
1682 		break;
1683 	}
1684 
1685 	dec->udev = udev;
1686 
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;
1696 
1697 	dec->adapter.priv = dec;
1698 	switch (id->idProduct) {
1699 	case 0x1006:
1700 		dec->fe = ttusbdecfe_dvbs_attach(&fe_config);
1701 		break;
1702 
1703 	case 0x1008:
1704 	case 0x1009:
1705 		dec->fe = ttusbdecfe_dvbt_attach(&fe_config);
1706 		break;
1707 	}
1708 
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;
1719 		}
1720 	}
1721 
1722 	ttusb_dec_init_v_pes(dec);
1723 	ttusb_dec_init_filters(dec);
1724 	ttusb_dec_init_tasklet(dec);
1725 
1726 	dec->active = 1;
1727 
1728 	ttusb_dec_set_interface(dec, TTUSB_DEC_INTERFACE_IN);
1729 
1730 	if (enable_rc)
1731 		ttusb_init_rc(dec);
1732 
1733 	return 0;
1734 err_stb:
1735 	ttusb_dec_exit_usb(dec);
1736 err_usb:
1737 	kfree(dec);
1738 	return result;
1739 }
1740 
1741 static void ttusb_dec_disconnect(struct usb_interface *intf)
1742 {
1743 	struct ttusb_dec *dec = usb_get_intfdata(intf);
1744 
1745 	usb_set_intfdata(intf, NULL);
1746 
1747 	dprintk("%s\n", __func__);
1748 
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);
1756 	}
1757 
1758 	kfree(dec);
1759 }
1760 
1761 static void ttusb_dec_set_model(struct ttusb_dec *dec,
1762 				enum ttusb_dec_model model)
1763 {
1764 	dec->model = model;
1765 
1766 	switch (model) {
1767 	case TTUSB_DEC2000T:
1768 		dec->model_name = "DEC2000-t";
1769 		dec->firmware_name = "dvb-ttusb-dec-2000t.fw";
1770 		break;
1771 
1772 	case TTUSB_DEC2540T:
1773 		dec->model_name = "DEC2540-t";
1774 		dec->firmware_name = "dvb-ttusb-dec-2540t.fw";
1775 		break;
1776 
1777 	case TTUSB_DEC3000S:
1778 		dec->model_name = "DEC3000-s";
1779 		dec->firmware_name = "dvb-ttusb-dec-3000s.fw";
1780 		break;
1781 	}
1782 }
1783 
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 */
1789 	{}
1790 };
1791 
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,
1797 };
1798 
1799 module_usb_driver(ttusb_dec_driver);
1800 
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);
1805