xref: /linux/drivers/media/usb/ttusb-dec/ttusb_dec.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * TTUSB DEC Driver
4  *
5  * Copyright (C) 2003-2004 Alex Woods <linux-dvb@giblets.org>
6  * IR support by Peter Beutner <p.beutner@gmx.net>
7  */
8 
9 #include <linux/list.h>
10 #include <linux/module.h>
11 #include <linux/pci.h>
12 #include <linux/slab.h>
13 #include <linux/spinlock.h>
14 #include <linux/usb.h>
15 #include <linux/interrupt.h>
16 #include <linux/firmware.h>
17 #include <linux/crc32.h>
18 #include <linux/init.h>
19 #include <linux/input.h>
20 
21 #include <linux/mutex.h>
22 #include <linux/workqueue.h>
23 
24 #include <media/dmxdev.h>
25 #include <media/dvb_demux.h>
26 #include <media/dvb_frontend.h>
27 #include <media/dvb_net.h>
28 #include "ttusbdecfe.h"
29 
30 static int debug;
31 static int output_pva;
32 static int enable_rc;
33 
34 module_param(debug, int, 0644);
35 MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
36 module_param(output_pva, int, 0444);
37 MODULE_PARM_DESC(output_pva, "Output PVA from dvr device (default:off)");
38 module_param(enable_rc, int, 0644);
39 MODULE_PARM_DESC(enable_rc, "Turn on/off IR remote control(default: off)");
40 
41 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
42 
43 #define dprintk	if (debug) printk
44 
45 #define DRIVER_NAME		"TechnoTrend/Hauppauge DEC USB"
46 
47 #define COMMAND_PIPE		0x03
48 #define RESULT_PIPE		0x04
49 #define IN_PIPE			0x08
50 #define OUT_PIPE		0x07
51 #define IRQ_PIPE		0x0A
52 
53 #define COMMAND_PACKET_SIZE	0x3c
54 #define ARM_PACKET_SIZE		0x1000
55 #define IRQ_PACKET_SIZE		0x8
56 
57 #define ISO_BUF_COUNT		0x04
58 #define FRAMES_PER_ISO_BUF	0x04
59 #define ISO_FRAME_SIZE		0x0380
60 
61 #define	MAX_PVA_LENGTH		6144
62 
63 enum ttusb_dec_model {
64 	TTUSB_DEC2000T,
65 	TTUSB_DEC2540T,
66 	TTUSB_DEC3000S
67 };
68 
69 enum ttusb_dec_packet_type {
70 	TTUSB_DEC_PACKET_PVA,
71 	TTUSB_DEC_PACKET_SECTION,
72 	TTUSB_DEC_PACKET_EMPTY
73 };
74 
75 enum ttusb_dec_interface {
76 	TTUSB_DEC_INTERFACE_INITIAL,
77 	TTUSB_DEC_INTERFACE_IN,
78 	TTUSB_DEC_INTERFACE_OUT
79 };
80 
81 typedef int (dvb_filter_pes2ts_cb_t) (void *, unsigned char *);
82 
83 struct dvb_filter_pes2ts {
84 	unsigned char buf[188];
85 	unsigned char cc;
86 	dvb_filter_pes2ts_cb_t *cb;
87 	void *priv;
88 };
89 
90 struct ttusb_dec {
91 	enum ttusb_dec_model		model;
92 	char				*model_name;
93 	char				*firmware_name;
94 	int				can_playback;
95 
96 	/* DVB bits */
97 	struct dvb_adapter		adapter;
98 	struct dmxdev			dmxdev;
99 	struct dvb_demux		demux;
100 	struct dmx_frontend		frontend;
101 	struct dvb_net			dvb_net;
102 	struct dvb_frontend*		fe;
103 
104 	u16			pid[DMX_PES_OTHER];
105 
106 	/* USB bits */
107 	struct usb_device		*udev;
108 	u8				trans_count;
109 	unsigned int			command_pipe;
110 	unsigned int			result_pipe;
111 	unsigned int			in_pipe;
112 	unsigned int			out_pipe;
113 	unsigned int			irq_pipe;
114 	enum ttusb_dec_interface	interface;
115 	struct mutex			usb_mutex;
116 
117 	void			*irq_buffer;
118 	struct urb		*irq_urb;
119 	dma_addr_t		irq_dma_handle;
120 	void			*iso_buffer;
121 	struct urb		*iso_urb[ISO_BUF_COUNT];
122 	int			iso_stream_count;
123 	struct mutex		iso_mutex;
124 
125 	u8				packet[MAX_PVA_LENGTH + 4];
126 	enum ttusb_dec_packet_type	packet_type;
127 	int				packet_state;
128 	int				packet_length;
129 	int				packet_payload_length;
130 	u16				next_packet_id;
131 
132 	int				pva_stream_count;
133 	int				filter_stream_count;
134 
135 	struct dvb_filter_pes2ts	a_pes2ts;
136 	struct dvb_filter_pes2ts	v_pes2ts;
137 
138 	u8			v_pes[16 + MAX_PVA_LENGTH];
139 	int			v_pes_length;
140 	int			v_pes_postbytes;
141 
142 	struct list_head	urb_frame_list;
143 	struct work_struct	urb_bh_work;
144 	spinlock_t		urb_frame_list_lock;
145 
146 	struct dvb_demux_filter	*audio_filter;
147 	struct dvb_demux_filter	*video_filter;
148 	struct list_head	filter_info_list;
149 	spinlock_t		filter_info_list_lock;
150 
151 	struct input_dev	*rc_input_dev;
152 	char			rc_phys[64];
153 
154 	int			active; /* Loaded successfully */
155 };
156 
157 struct urb_frame {
158 	u8			data[ISO_FRAME_SIZE];
159 	int			length;
160 	struct list_head	urb_frame_list;
161 };
162 
163 struct filter_info {
164 	u8			stream_id;
165 	struct dvb_demux_filter	*filter;
166 	struct list_head	filter_info_list;
167 };
168 
169 static u16 rc_keys[] = {
170 	KEY_POWER,
171 	KEY_MUTE,
172 	KEY_1,
173 	KEY_2,
174 	KEY_3,
175 	KEY_4,
176 	KEY_5,
177 	KEY_6,
178 	KEY_7,
179 	KEY_8,
180 	KEY_9,
181 	KEY_0,
182 	KEY_CHANNELUP,
183 	KEY_VOLUMEDOWN,
184 	KEY_OK,
185 	KEY_VOLUMEUP,
186 	KEY_CHANNELDOWN,
187 	KEY_PREVIOUS,
188 	KEY_ESC,
189 	KEY_RED,
190 	KEY_GREEN,
191 	KEY_YELLOW,
192 	KEY_BLUE,
193 	KEY_OPTION,
194 	KEY_M,
195 	KEY_RADIO
196 };
197 
dvb_filter_pes2ts_init(struct dvb_filter_pes2ts * p2ts,unsigned short pid,dvb_filter_pes2ts_cb_t * cb,void * priv)198 static void dvb_filter_pes2ts_init(struct dvb_filter_pes2ts *p2ts,
199 				   unsigned short pid,
200 				   dvb_filter_pes2ts_cb_t *cb, void *priv)
201 {
202 	unsigned char *buf=p2ts->buf;
203 
204 	buf[0]=0x47;
205 	buf[1]=(pid>>8);
206 	buf[2]=pid&0xff;
207 	p2ts->cc=0;
208 	p2ts->cb=cb;
209 	p2ts->priv=priv;
210 }
211 
dvb_filter_pes2ts(struct dvb_filter_pes2ts * p2ts,unsigned char * pes,int len,int payload_start)212 static int dvb_filter_pes2ts(struct dvb_filter_pes2ts *p2ts,
213 			     unsigned char *pes, int len, int payload_start)
214 {
215 	unsigned char *buf=p2ts->buf;
216 	int ret=0, rest;
217 
218 	//len=6+((pes[4]<<8)|pes[5]);
219 
220 	if (payload_start)
221 		buf[1]|=0x40;
222 	else
223 		buf[1]&=~0x40;
224 	while (len>=184) {
225 		buf[3]=0x10|((p2ts->cc++)&0x0f);
226 		memcpy(buf+4, pes, 184);
227 		if ((ret=p2ts->cb(p2ts->priv, buf)))
228 			return ret;
229 		len-=184; pes+=184;
230 		buf[1]&=~0x40;
231 	}
232 	if (!len)
233 		return 0;
234 	buf[3]=0x30|((p2ts->cc++)&0x0f);
235 	rest=183-len;
236 	if (rest) {
237 		buf[5]=0x00;
238 		if (rest-1)
239 			memset(buf+6, 0xff, rest-1);
240 	}
241 	buf[4]=rest;
242 	memcpy(buf+5+rest, pes, len);
243 	return p2ts->cb(p2ts->priv, buf);
244 }
245 
246 static void ttusb_dec_set_model(struct ttusb_dec *dec,
247 				enum ttusb_dec_model model);
248 
ttusb_dec_handle_irq(struct urb * urb)249 static void ttusb_dec_handle_irq( struct urb *urb)
250 {
251 	struct ttusb_dec *dec = urb->context;
252 	char *buffer = dec->irq_buffer;
253 	int retval;
254 	int index = buffer[4];
255 
256 	switch(urb->status) {
257 		case 0: /*success*/
258 			break;
259 		case -ECONNRESET:
260 		case -ENOENT:
261 		case -ESHUTDOWN:
262 		case -ETIME:
263 			/* this urb is dead, cleanup */
264 			dprintk("%s:urb shutting down with status: %d\n",
265 					__func__, urb->status);
266 			return;
267 		default:
268 			dprintk("%s:nonzero status received: %d\n",
269 					__func__,urb->status);
270 			goto exit;
271 	}
272 
273 	if ((buffer[0] == 0x1) && (buffer[2] == 0x15))  {
274 		/*
275 		 * IR - Event
276 		 *
277 		 * this is an fact a bit too simple implementation;
278 		 * the box also reports a keyrepeat signal
279 		 * (with buffer[3] == 0x40) in an interval of ~100ms.
280 		 * But to handle this correctly we had to imlemenent some
281 		 * kind of timer which signals a 'key up' event if no
282 		 * keyrepeat signal is received for lets say 200ms.
283 		 * this should/could be added later ...
284 		 * for now lets report each signal as a key down and up
285 		 */
286 		if (index - 1 < ARRAY_SIZE(rc_keys)) {
287 			dprintk("%s:rc signal:%d\n", __func__, index);
288 			input_report_key(dec->rc_input_dev, rc_keys[index - 1], 1);
289 			input_sync(dec->rc_input_dev);
290 			input_report_key(dec->rc_input_dev, rc_keys[index - 1], 0);
291 			input_sync(dec->rc_input_dev);
292 		}
293 	}
294 
295 exit:
296 	retval = usb_submit_urb(urb, GFP_ATOMIC);
297 	if (retval)
298 		printk("%s - usb_commit_urb failed with result: %d\n",
299 			__func__, retval);
300 }
301 
crc16(u16 crc,const u8 * buf,size_t len)302 static u16 crc16(u16 crc, const u8 *buf, size_t len)
303 {
304 	u16 tmp;
305 
306 	while (len--) {
307 		crc ^= *buf++;
308 		crc ^= (u8)crc >> 4;
309 		tmp = (u8)crc;
310 		crc ^= (tmp ^ (tmp << 1)) << 4;
311 	}
312 	return crc;
313 }
314 
ttusb_dec_send_command(struct ttusb_dec * dec,const u8 command,int param_length,const u8 params[],int * result_length,u8 cmd_result[])315 static int ttusb_dec_send_command(struct ttusb_dec *dec, const u8 command,
316 				  int param_length, const u8 params[],
317 				  int *result_length, u8 cmd_result[])
318 {
319 	int result, actual_len;
320 	u8 *b;
321 
322 	dprintk("%s\n", __func__);
323 
324 	b = kzalloc(COMMAND_PACKET_SIZE + 4, GFP_KERNEL);
325 	if (!b)
326 		return -ENOMEM;
327 
328 	result = mutex_lock_interruptible(&dec->usb_mutex);
329 	if (result) {
330 		printk("%s: Failed to lock usb mutex.\n", __func__);
331 		goto err_free;
332 	}
333 
334 	b[0] = 0xaa;
335 	b[1] = ++dec->trans_count;
336 	b[2] = command;
337 	b[3] = param_length;
338 
339 	if (params)
340 		memcpy(&b[4], params, param_length);
341 
342 	if (debug) {
343 		printk(KERN_DEBUG "%s: command: %*ph\n",
344 		       __func__, param_length, b);
345 	}
346 
347 	result = usb_bulk_msg(dec->udev, dec->command_pipe, b,
348 			      COMMAND_PACKET_SIZE + 4, &actual_len, 1000);
349 
350 	if (result) {
351 		printk("%s: command bulk message failed: error %d\n",
352 		       __func__, result);
353 		goto err_mutex_unlock;
354 	}
355 
356 	result = usb_bulk_msg(dec->udev, dec->result_pipe, b,
357 			      COMMAND_PACKET_SIZE + 4, &actual_len, 1000);
358 
359 	if (result) {
360 		printk("%s: result bulk message failed: error %d\n",
361 		       __func__, result);
362 		goto err_mutex_unlock;
363 	} else {
364 		if (debug) {
365 			printk(KERN_DEBUG "%s: result: %*ph\n",
366 			       __func__, actual_len, b);
367 		}
368 
369 		if (result_length)
370 			*result_length = b[3];
371 		if (cmd_result && b[3] > 0)
372 			memcpy(cmd_result, &b[4], b[3]);
373 	}
374 
375 err_mutex_unlock:
376 	mutex_unlock(&dec->usb_mutex);
377 err_free:
378 	kfree(b);
379 	return result;
380 }
381 
ttusb_dec_get_stb_state(struct ttusb_dec * dec,unsigned int * mode,unsigned int * model,unsigned int * version)382 static int ttusb_dec_get_stb_state (struct ttusb_dec *dec, unsigned int *mode,
383 				    unsigned int *model, unsigned int *version)
384 {
385 	u8 c[COMMAND_PACKET_SIZE];
386 	int c_length;
387 	int result;
388 	__be32 tmp;
389 
390 	dprintk("%s\n", __func__);
391 
392 	result = ttusb_dec_send_command(dec, 0x08, 0, NULL, &c_length, c);
393 	if (result)
394 		return result;
395 
396 	if (c_length >= 0x0c) {
397 		if (mode != NULL) {
398 			memcpy(&tmp, c, 4);
399 			*mode = ntohl(tmp);
400 		}
401 		if (model != NULL) {
402 			memcpy(&tmp, &c[4], 4);
403 			*model = ntohl(tmp);
404 		}
405 		if (version != NULL) {
406 			memcpy(&tmp, &c[8], 4);
407 			*version = ntohl(tmp);
408 		}
409 		return 0;
410 	} else {
411 		return -ENOENT;
412 	}
413 }
414 
ttusb_dec_audio_pes2ts_cb(void * priv,unsigned char * data)415 static int ttusb_dec_audio_pes2ts_cb(void *priv, unsigned char *data)
416 {
417 	struct ttusb_dec *dec = priv;
418 
419 	dec->audio_filter->feed->cb.ts(data, 188, NULL, 0,
420 				       &dec->audio_filter->feed->feed.ts, NULL);
421 
422 	return 0;
423 }
424 
ttusb_dec_video_pes2ts_cb(void * priv,unsigned char * data)425 static int ttusb_dec_video_pes2ts_cb(void *priv, unsigned char *data)
426 {
427 	struct ttusb_dec *dec = priv;
428 
429 	dec->video_filter->feed->cb.ts(data, 188, NULL, 0,
430 				       &dec->video_filter->feed->feed.ts, NULL);
431 
432 	return 0;
433 }
434 
ttusb_dec_set_pids(struct ttusb_dec * dec)435 static void ttusb_dec_set_pids(struct ttusb_dec *dec)
436 {
437 	u8 b[] = { 0x00, 0x00, 0x00, 0x00,
438 		   0x00, 0x00, 0xff, 0xff,
439 		   0xff, 0xff, 0xff, 0xff };
440 
441 	__be16 pcr = htons(dec->pid[DMX_PES_PCR]);
442 	__be16 audio = htons(dec->pid[DMX_PES_AUDIO]);
443 	__be16 video = htons(dec->pid[DMX_PES_VIDEO]);
444 
445 	dprintk("%s\n", __func__);
446 
447 	memcpy(&b[0], &pcr, 2);
448 	memcpy(&b[2], &audio, 2);
449 	memcpy(&b[4], &video, 2);
450 
451 	ttusb_dec_send_command(dec, 0x50, sizeof(b), b, NULL, NULL);
452 
453 	dvb_filter_pes2ts_init(&dec->a_pes2ts, dec->pid[DMX_PES_AUDIO],
454 			       ttusb_dec_audio_pes2ts_cb, dec);
455 	dvb_filter_pes2ts_init(&dec->v_pes2ts, dec->pid[DMX_PES_VIDEO],
456 			       ttusb_dec_video_pes2ts_cb, dec);
457 	dec->v_pes_length = 0;
458 	dec->v_pes_postbytes = 0;
459 }
460 
ttusb_dec_process_pva(struct ttusb_dec * dec,u8 * pva,int length)461 static void ttusb_dec_process_pva(struct ttusb_dec *dec, u8 *pva, int length)
462 {
463 	if (length < 8) {
464 		printk("%s: packet too short - discarding\n", __func__);
465 		return;
466 	}
467 
468 	if (length > 8 + MAX_PVA_LENGTH) {
469 		printk("%s: packet too long - discarding\n", __func__);
470 		return;
471 	}
472 
473 	switch (pva[2]) {
474 
475 	case 0x01: {		/* VideoStream */
476 		int prebytes = pva[5] & 0x03;
477 		int postbytes = (pva[5] & 0x0c) >> 2;
478 		__be16 v_pes_payload_length;
479 
480 		if (output_pva) {
481 			dec->video_filter->feed->cb.ts(pva, length, NULL, 0,
482 				&dec->video_filter->feed->feed.ts, NULL);
483 			return;
484 		}
485 
486 		if (dec->v_pes_postbytes > 0 &&
487 		    dec->v_pes_postbytes == prebytes) {
488 			memcpy(&dec->v_pes[dec->v_pes_length],
489 			       &pva[12], prebytes);
490 
491 			dvb_filter_pes2ts(&dec->v_pes2ts, dec->v_pes,
492 					  dec->v_pes_length + prebytes, 1);
493 		}
494 
495 		if (pva[5] & 0x10) {
496 			dec->v_pes[7] = 0x80;
497 			dec->v_pes[8] = 0x05;
498 
499 			dec->v_pes[9] = 0x21 | ((pva[8] & 0xc0) >> 5);
500 			dec->v_pes[10] = ((pva[8] & 0x3f) << 2) |
501 					 ((pva[9] & 0xc0) >> 6);
502 			dec->v_pes[11] = 0x01 |
503 					 ((pva[9] & 0x3f) << 2) |
504 					 ((pva[10] & 0x80) >> 6);
505 			dec->v_pes[12] = ((pva[10] & 0x7f) << 1) |
506 					 ((pva[11] & 0xc0) >> 7);
507 			dec->v_pes[13] = 0x01 | ((pva[11] & 0x7f) << 1);
508 
509 			memcpy(&dec->v_pes[14], &pva[12 + prebytes],
510 			       length - 12 - prebytes);
511 			dec->v_pes_length = 14 + length - 12 - prebytes;
512 		} else {
513 			dec->v_pes[7] = 0x00;
514 			dec->v_pes[8] = 0x00;
515 
516 			memcpy(&dec->v_pes[9], &pva[8], length - 8);
517 			dec->v_pes_length = 9 + length - 8;
518 		}
519 
520 		dec->v_pes_postbytes = postbytes;
521 
522 		if (dec->v_pes[9 + dec->v_pes[8]] == 0x00 &&
523 		    dec->v_pes[10 + dec->v_pes[8]] == 0x00 &&
524 		    dec->v_pes[11 + dec->v_pes[8]] == 0x01)
525 			dec->v_pes[6] = 0x84;
526 		else
527 			dec->v_pes[6] = 0x80;
528 
529 		v_pes_payload_length = htons(dec->v_pes_length - 6 +
530 					     postbytes);
531 		memcpy(&dec->v_pes[4], &v_pes_payload_length, 2);
532 
533 		if (postbytes == 0)
534 			dvb_filter_pes2ts(&dec->v_pes2ts, dec->v_pes,
535 					  dec->v_pes_length, 1);
536 
537 		break;
538 	}
539 
540 	case 0x02:		/* MainAudioStream */
541 		if (output_pva) {
542 			dec->audio_filter->feed->cb.ts(pva, length, NULL, 0,
543 				&dec->audio_filter->feed->feed.ts, NULL);
544 			return;
545 		}
546 
547 		dvb_filter_pes2ts(&dec->a_pes2ts, &pva[8], length - 8,
548 				  pva[5] & 0x10);
549 		break;
550 
551 	default:
552 		printk("%s: unknown PVA type: %02x.\n", __func__,
553 		       pva[2]);
554 		break;
555 	}
556 }
557 
ttusb_dec_process_filter(struct ttusb_dec * dec,u8 * packet,int length)558 static void ttusb_dec_process_filter(struct ttusb_dec *dec, u8 *packet,
559 				     int length)
560 {
561 	struct list_head *item;
562 	struct filter_info *finfo;
563 	struct dvb_demux_filter *filter = NULL;
564 	unsigned long flags;
565 	u8 sid;
566 
567 	sid = packet[1];
568 	spin_lock_irqsave(&dec->filter_info_list_lock, flags);
569 	for (item = dec->filter_info_list.next; item != &dec->filter_info_list;
570 	     item = item->next) {
571 		finfo = list_entry(item, struct filter_info, filter_info_list);
572 		if (finfo->stream_id == sid) {
573 			filter = finfo->filter;
574 			break;
575 		}
576 	}
577 	spin_unlock_irqrestore(&dec->filter_info_list_lock, flags);
578 
579 	if (filter)
580 		filter->feed->cb.sec(&packet[2], length - 2, NULL, 0,
581 				     &filter->filter, NULL);
582 }
583 
ttusb_dec_process_packet(struct ttusb_dec * dec)584 static void ttusb_dec_process_packet(struct ttusb_dec *dec)
585 {
586 	int i;
587 	u16 csum = 0;
588 	u16 packet_id;
589 
590 	if (dec->packet_length % 2) {
591 		printk("%s: odd sized packet - discarding\n", __func__);
592 		return;
593 	}
594 
595 	for (i = 0; i < dec->packet_length; i += 2)
596 		csum ^= ((dec->packet[i] << 8) + dec->packet[i + 1]);
597 
598 	if (csum) {
599 		printk("%s: checksum failed - discarding\n", __func__);
600 		return;
601 	}
602 
603 	packet_id = dec->packet[dec->packet_length - 4] << 8;
604 	packet_id += dec->packet[dec->packet_length - 3];
605 
606 	if ((packet_id != dec->next_packet_id) && dec->next_packet_id) {
607 		printk("%s: warning: lost packets between %u and %u\n",
608 		       __func__, dec->next_packet_id - 1, packet_id);
609 	}
610 
611 	if (packet_id == 0xffff)
612 		dec->next_packet_id = 0x8000;
613 	else
614 		dec->next_packet_id = packet_id + 1;
615 
616 	switch (dec->packet_type) {
617 	case TTUSB_DEC_PACKET_PVA:
618 		if (dec->pva_stream_count)
619 			ttusb_dec_process_pva(dec, dec->packet,
620 					      dec->packet_payload_length);
621 		break;
622 
623 	case TTUSB_DEC_PACKET_SECTION:
624 		if (dec->filter_stream_count)
625 			ttusb_dec_process_filter(dec, dec->packet,
626 						 dec->packet_payload_length);
627 		break;
628 
629 	case TTUSB_DEC_PACKET_EMPTY:
630 		break;
631 	}
632 }
633 
swap_bytes(u8 * b,int length)634 static void swap_bytes(u8 *b, int length)
635 {
636 	length -= length % 2;
637 	for (; length; b += 2, length -= 2)
638 		swap(*b, *(b + 1));
639 }
640 
ttusb_dec_process_urb_frame(struct ttusb_dec * dec,u8 * b,int length)641 static void ttusb_dec_process_urb_frame(struct ttusb_dec *dec, u8 *b,
642 					int length)
643 {
644 	swap_bytes(b, length);
645 
646 	while (length) {
647 		switch (dec->packet_state) {
648 
649 		case 0:
650 		case 1:
651 		case 2:
652 			if (*b++ == 0xaa)
653 				dec->packet_state++;
654 			else
655 				dec->packet_state = 0;
656 
657 			length--;
658 			break;
659 
660 		case 3:
661 			if (*b == 0x00) {
662 				dec->packet_state++;
663 				dec->packet_length = 0;
664 			} else if (*b != 0xaa) {
665 				dec->packet_state = 0;
666 			}
667 
668 			b++;
669 			length--;
670 			break;
671 
672 		case 4:
673 			dec->packet[dec->packet_length++] = *b++;
674 
675 			if (dec->packet_length == 2) {
676 				if (dec->packet[0] == 'A' &&
677 				    dec->packet[1] == 'V') {
678 					dec->packet_type =
679 						TTUSB_DEC_PACKET_PVA;
680 					dec->packet_state++;
681 				} else if (dec->packet[0] == 'S') {
682 					dec->packet_type =
683 						TTUSB_DEC_PACKET_SECTION;
684 					dec->packet_state++;
685 				} else if (dec->packet[0] == 0x00) {
686 					dec->packet_type =
687 						TTUSB_DEC_PACKET_EMPTY;
688 					dec->packet_payload_length = 2;
689 					dec->packet_state = 7;
690 				} else {
691 					printk("%s: unknown packet type: %02x%02x\n",
692 					       __func__,
693 					       dec->packet[0], dec->packet[1]);
694 					dec->packet_state = 0;
695 				}
696 			}
697 
698 			length--;
699 			break;
700 
701 		case 5:
702 			dec->packet[dec->packet_length++] = *b++;
703 
704 			if (dec->packet_type == TTUSB_DEC_PACKET_PVA &&
705 			    dec->packet_length == 8) {
706 				dec->packet_state++;
707 				dec->packet_payload_length = 8 +
708 					(dec->packet[6] << 8) +
709 					dec->packet[7];
710 			} else if (dec->packet_type ==
711 					TTUSB_DEC_PACKET_SECTION &&
712 				   dec->packet_length == 5) {
713 				dec->packet_state++;
714 				dec->packet_payload_length = 5 +
715 					((dec->packet[3] & 0x0f) << 8) +
716 					dec->packet[4];
717 			}
718 
719 			length--;
720 			break;
721 
722 		case 6: {
723 			int remainder = dec->packet_payload_length -
724 					dec->packet_length;
725 
726 			if (length >= remainder) {
727 				memcpy(dec->packet + dec->packet_length,
728 				       b, remainder);
729 				dec->packet_length += remainder;
730 				b += remainder;
731 				length -= remainder;
732 				dec->packet_state++;
733 			} else {
734 				memcpy(&dec->packet[dec->packet_length],
735 				       b, length);
736 				dec->packet_length += length;
737 				length = 0;
738 			}
739 
740 			break;
741 		}
742 
743 		case 7: {
744 			int tail = 4;
745 
746 			dec->packet[dec->packet_length++] = *b++;
747 
748 			if (dec->packet_type == TTUSB_DEC_PACKET_SECTION &&
749 			    dec->packet_payload_length % 2)
750 				tail++;
751 
752 			if (dec->packet_length ==
753 			    dec->packet_payload_length + tail) {
754 				ttusb_dec_process_packet(dec);
755 				dec->packet_state = 0;
756 			}
757 
758 			length--;
759 			break;
760 		}
761 
762 		default:
763 			printk("%s: illegal packet state encountered.\n",
764 			       __func__);
765 			dec->packet_state = 0;
766 		}
767 	}
768 }
769 
ttusb_dec_process_urb_frame_list(struct work_struct * t)770 static void ttusb_dec_process_urb_frame_list(struct work_struct *t)
771 {
772 	struct ttusb_dec *dec = from_work(dec, t, urb_bh_work);
773 	struct list_head *item;
774 	struct urb_frame *frame;
775 	unsigned long flags;
776 
777 	while (1) {
778 		spin_lock_irqsave(&dec->urb_frame_list_lock, flags);
779 		if ((item = dec->urb_frame_list.next) != &dec->urb_frame_list) {
780 			frame = list_entry(item, struct urb_frame,
781 					   urb_frame_list);
782 			list_del(&frame->urb_frame_list);
783 		} else {
784 			spin_unlock_irqrestore(&dec->urb_frame_list_lock,
785 					       flags);
786 			return;
787 		}
788 		spin_unlock_irqrestore(&dec->urb_frame_list_lock, flags);
789 
790 		ttusb_dec_process_urb_frame(dec, frame->data, frame->length);
791 		kfree(frame);
792 	}
793 }
794 
ttusb_dec_process_urb(struct urb * urb)795 static void ttusb_dec_process_urb(struct urb *urb)
796 {
797 	struct ttusb_dec *dec = urb->context;
798 
799 	if (!urb->status) {
800 		int i;
801 
802 		for (i = 0; i < FRAMES_PER_ISO_BUF; i++) {
803 			struct usb_iso_packet_descriptor *d;
804 			u8 *b;
805 			int length;
806 			struct urb_frame *frame;
807 
808 			d = &urb->iso_frame_desc[i];
809 			b = urb->transfer_buffer + d->offset;
810 			length = d->actual_length;
811 
812 			if ((frame = kmalloc(sizeof(struct urb_frame),
813 					     GFP_ATOMIC))) {
814 				unsigned long flags;
815 
816 				memcpy(frame->data, b, length);
817 				frame->length = length;
818 
819 				spin_lock_irqsave(&dec->urb_frame_list_lock,
820 						     flags);
821 				list_add_tail(&frame->urb_frame_list,
822 					      &dec->urb_frame_list);
823 				spin_unlock_irqrestore(&dec->urb_frame_list_lock,
824 						       flags);
825 
826 				queue_work(system_bh_wq, &dec->urb_bh_work);
827 			}
828 		}
829 	} else {
830 		 /* -ENOENT is expected when unlinking urbs */
831 		if (urb->status != -ENOENT)
832 			dprintk("%s: urb error: %d\n", __func__,
833 				urb->status);
834 	}
835 
836 	if (dec->iso_stream_count)
837 		usb_submit_urb(urb, GFP_ATOMIC);
838 }
839 
ttusb_dec_setup_urbs(struct ttusb_dec * dec)840 static void ttusb_dec_setup_urbs(struct ttusb_dec *dec)
841 {
842 	int i, j, buffer_offset = 0;
843 
844 	dprintk("%s\n", __func__);
845 
846 	for (i = 0; i < ISO_BUF_COUNT; i++) {
847 		int frame_offset = 0;
848 		struct urb *urb = dec->iso_urb[i];
849 
850 		urb->dev = dec->udev;
851 		urb->context = dec;
852 		urb->complete = ttusb_dec_process_urb;
853 		urb->pipe = dec->in_pipe;
854 		urb->transfer_flags = URB_ISO_ASAP;
855 		urb->interval = 1;
856 		urb->number_of_packets = FRAMES_PER_ISO_BUF;
857 		urb->transfer_buffer_length = ISO_FRAME_SIZE *
858 					      FRAMES_PER_ISO_BUF;
859 		urb->transfer_buffer = dec->iso_buffer + buffer_offset;
860 		buffer_offset += ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF;
861 
862 		for (j = 0; j < FRAMES_PER_ISO_BUF; j++) {
863 			urb->iso_frame_desc[j].offset = frame_offset;
864 			urb->iso_frame_desc[j].length = ISO_FRAME_SIZE;
865 			frame_offset += ISO_FRAME_SIZE;
866 		}
867 	}
868 }
869 
ttusb_dec_stop_iso_xfer(struct ttusb_dec * dec)870 static void ttusb_dec_stop_iso_xfer(struct ttusb_dec *dec)
871 {
872 	int i;
873 
874 	dprintk("%s\n", __func__);
875 
876 	if (mutex_lock_interruptible(&dec->iso_mutex))
877 		return;
878 
879 	dec->iso_stream_count--;
880 
881 	if (!dec->iso_stream_count) {
882 		for (i = 0; i < ISO_BUF_COUNT; i++)
883 			usb_kill_urb(dec->iso_urb[i]);
884 	}
885 
886 	mutex_unlock(&dec->iso_mutex);
887 }
888 
889 /* Setting the interface of the DEC tends to take down the USB communications
890  * for a short period, so it's important not to call this function just before
891  * trying to talk to it.
892  */
ttusb_dec_set_interface(struct ttusb_dec * dec,enum ttusb_dec_interface interface)893 static int ttusb_dec_set_interface(struct ttusb_dec *dec,
894 				   enum ttusb_dec_interface interface)
895 {
896 	int result = 0;
897 	u8 b[] = { 0x05 };
898 
899 	if (interface != dec->interface) {
900 		switch (interface) {
901 		case TTUSB_DEC_INTERFACE_INITIAL:
902 			result = usb_set_interface(dec->udev, 0, 0);
903 			break;
904 		case TTUSB_DEC_INTERFACE_IN:
905 			result = ttusb_dec_send_command(dec, 0x80, sizeof(b),
906 							b, NULL, NULL);
907 			if (result)
908 				return result;
909 			result = usb_set_interface(dec->udev, 0, 8);
910 			break;
911 		case TTUSB_DEC_INTERFACE_OUT:
912 			result = usb_set_interface(dec->udev, 0, 1);
913 			break;
914 		}
915 
916 		if (result)
917 			return result;
918 
919 		dec->interface = interface;
920 	}
921 
922 	return 0;
923 }
924 
ttusb_dec_start_iso_xfer(struct ttusb_dec * dec)925 static int ttusb_dec_start_iso_xfer(struct ttusb_dec *dec)
926 {
927 	int i, result;
928 
929 	dprintk("%s\n", __func__);
930 
931 	if (mutex_lock_interruptible(&dec->iso_mutex))
932 		return -EAGAIN;
933 
934 	if (!dec->iso_stream_count) {
935 		ttusb_dec_setup_urbs(dec);
936 
937 		dec->packet_state = 0;
938 		dec->v_pes_postbytes = 0;
939 		dec->next_packet_id = 0;
940 
941 		for (i = 0; i < ISO_BUF_COUNT; i++) {
942 			if ((result = usb_submit_urb(dec->iso_urb[i],
943 						     GFP_ATOMIC))) {
944 				printk("%s: failed urb submission %d: error %d\n",
945 				       __func__, i, result);
946 
947 				while (i) {
948 					usb_kill_urb(dec->iso_urb[i - 1]);
949 					i--;
950 				}
951 
952 				mutex_unlock(&dec->iso_mutex);
953 				return result;
954 			}
955 		}
956 	}
957 
958 	dec->iso_stream_count++;
959 
960 	mutex_unlock(&dec->iso_mutex);
961 
962 	return 0;
963 }
964 
ttusb_dec_start_ts_feed(struct dvb_demux_feed * dvbdmxfeed)965 static int ttusb_dec_start_ts_feed(struct dvb_demux_feed *dvbdmxfeed)
966 {
967 	struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
968 	struct ttusb_dec *dec = dvbdmx->priv;
969 	u8 b0[] = { 0x05 };
970 	int result = 0;
971 
972 	dprintk("%s\n", __func__);
973 
974 	dprintk("  ts_type:");
975 
976 	if (dvbdmxfeed->ts_type & TS_DECODER)
977 		dprintk(" TS_DECODER");
978 
979 	if (dvbdmxfeed->ts_type & TS_PACKET)
980 		dprintk(" TS_PACKET");
981 
982 	if (dvbdmxfeed->ts_type & TS_PAYLOAD_ONLY)
983 		dprintk(" TS_PAYLOAD_ONLY");
984 
985 	dprintk("\n");
986 
987 	switch (dvbdmxfeed->pes_type) {
988 
989 	case DMX_PES_VIDEO:
990 		dprintk("  pes_type: DMX_PES_VIDEO\n");
991 		dec->pid[DMX_PES_PCR] = dvbdmxfeed->pid;
992 		dec->pid[DMX_PES_VIDEO] = dvbdmxfeed->pid;
993 		dec->video_filter = dvbdmxfeed->filter;
994 		ttusb_dec_set_pids(dec);
995 		break;
996 
997 	case DMX_PES_AUDIO:
998 		dprintk("  pes_type: DMX_PES_AUDIO\n");
999 		dec->pid[DMX_PES_AUDIO] = dvbdmxfeed->pid;
1000 		dec->audio_filter = dvbdmxfeed->filter;
1001 		ttusb_dec_set_pids(dec);
1002 		break;
1003 
1004 	case DMX_PES_TELETEXT:
1005 		dec->pid[DMX_PES_TELETEXT] = dvbdmxfeed->pid;
1006 		dprintk("  pes_type: DMX_PES_TELETEXT(not supported)\n");
1007 		return -ENOSYS;
1008 
1009 	case DMX_PES_PCR:
1010 		dprintk("  pes_type: DMX_PES_PCR\n");
1011 		dec->pid[DMX_PES_PCR] = dvbdmxfeed->pid;
1012 		ttusb_dec_set_pids(dec);
1013 		break;
1014 
1015 	case DMX_PES_OTHER:
1016 		dprintk("  pes_type: DMX_PES_OTHER(not supported)\n");
1017 		return -ENOSYS;
1018 
1019 	default:
1020 		dprintk("  pes_type: unknown (%d)\n", dvbdmxfeed->pes_type);
1021 		return -EINVAL;
1022 
1023 	}
1024 
1025 	result = ttusb_dec_send_command(dec, 0x80, sizeof(b0), b0, NULL, NULL);
1026 	if (result)
1027 		return result;
1028 
1029 	dec->pva_stream_count++;
1030 	return ttusb_dec_start_iso_xfer(dec);
1031 }
1032 
ttusb_dec_start_sec_feed(struct dvb_demux_feed * dvbdmxfeed)1033 static int ttusb_dec_start_sec_feed(struct dvb_demux_feed *dvbdmxfeed)
1034 {
1035 	struct ttusb_dec *dec = dvbdmxfeed->demux->priv;
1036 	u8 b0[] = { 0x00, 0x00, 0x00, 0x01,
1037 		    0x00, 0x00, 0x00, 0x00,
1038 		    0x00, 0x00, 0x00, 0x00,
1039 		    0x00, 0x00, 0x00, 0x00,
1040 		    0x00, 0xff, 0x00, 0x00,
1041 		    0x00, 0x00, 0x00, 0x00,
1042 		    0x00, 0x00, 0x00, 0x00,
1043 		    0x00 };
1044 	__be16 pid;
1045 	u8 c[COMMAND_PACKET_SIZE];
1046 	int c_length;
1047 	int result;
1048 	struct filter_info *finfo;
1049 	unsigned long flags;
1050 	u8 x = 1;
1051 
1052 	dprintk("%s\n", __func__);
1053 
1054 	pid = htons(dvbdmxfeed->pid);
1055 	memcpy(&b0[0], &pid, 2);
1056 	memcpy(&b0[4], &x, 1);
1057 	memcpy(&b0[5], &dvbdmxfeed->filter->filter.filter_value[0], 1);
1058 
1059 	result = ttusb_dec_send_command(dec, 0x60, sizeof(b0), b0,
1060 					&c_length, c);
1061 
1062 	if (!result) {
1063 		if (c_length == 2) {
1064 			if (!(finfo = kmalloc(sizeof(struct filter_info),
1065 					      GFP_ATOMIC)))
1066 				return -ENOMEM;
1067 
1068 			finfo->stream_id = c[1];
1069 			finfo->filter = dvbdmxfeed->filter;
1070 
1071 			spin_lock_irqsave(&dec->filter_info_list_lock, flags);
1072 			list_add_tail(&finfo->filter_info_list,
1073 				      &dec->filter_info_list);
1074 			spin_unlock_irqrestore(&dec->filter_info_list_lock,
1075 					       flags);
1076 
1077 			dvbdmxfeed->priv = finfo;
1078 
1079 			dec->filter_stream_count++;
1080 			return ttusb_dec_start_iso_xfer(dec);
1081 		}
1082 
1083 		return -EAGAIN;
1084 	} else
1085 		return result;
1086 }
1087 
ttusb_dec_start_feed(struct dvb_demux_feed * dvbdmxfeed)1088 static int ttusb_dec_start_feed(struct dvb_demux_feed *dvbdmxfeed)
1089 {
1090 	struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
1091 
1092 	dprintk("%s\n", __func__);
1093 
1094 	if (!dvbdmx->dmx.frontend)
1095 		return -EINVAL;
1096 
1097 	dprintk("  pid: 0x%04X\n", dvbdmxfeed->pid);
1098 
1099 	switch (dvbdmxfeed->type) {
1100 
1101 	case DMX_TYPE_TS:
1102 		return ttusb_dec_start_ts_feed(dvbdmxfeed);
1103 
1104 	case DMX_TYPE_SEC:
1105 		return ttusb_dec_start_sec_feed(dvbdmxfeed);
1106 
1107 	default:
1108 		dprintk("  type: unknown (%d)\n", dvbdmxfeed->type);
1109 		return -EINVAL;
1110 
1111 	}
1112 }
1113 
ttusb_dec_stop_ts_feed(struct dvb_demux_feed * dvbdmxfeed)1114 static int ttusb_dec_stop_ts_feed(struct dvb_demux_feed *dvbdmxfeed)
1115 {
1116 	struct ttusb_dec *dec = dvbdmxfeed->demux->priv;
1117 	u8 b0[] = { 0x00 };
1118 
1119 	ttusb_dec_send_command(dec, 0x81, sizeof(b0), b0, NULL, NULL);
1120 
1121 	dec->pva_stream_count--;
1122 
1123 	ttusb_dec_stop_iso_xfer(dec);
1124 
1125 	return 0;
1126 }
1127 
ttusb_dec_stop_sec_feed(struct dvb_demux_feed * dvbdmxfeed)1128 static int ttusb_dec_stop_sec_feed(struct dvb_demux_feed *dvbdmxfeed)
1129 {
1130 	struct ttusb_dec *dec = dvbdmxfeed->demux->priv;
1131 	u8 b0[] = { 0x00, 0x00 };
1132 	struct filter_info *finfo = dvbdmxfeed->priv;
1133 	unsigned long flags;
1134 
1135 	b0[1] = finfo->stream_id;
1136 	spin_lock_irqsave(&dec->filter_info_list_lock, flags);
1137 	list_del(&finfo->filter_info_list);
1138 	spin_unlock_irqrestore(&dec->filter_info_list_lock, flags);
1139 	kfree(finfo);
1140 	ttusb_dec_send_command(dec, 0x62, sizeof(b0), b0, NULL, NULL);
1141 
1142 	dec->filter_stream_count--;
1143 
1144 	ttusb_dec_stop_iso_xfer(dec);
1145 
1146 	return 0;
1147 }
1148 
ttusb_dec_stop_feed(struct dvb_demux_feed * dvbdmxfeed)1149 static int ttusb_dec_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
1150 {
1151 	dprintk("%s\n", __func__);
1152 
1153 	switch (dvbdmxfeed->type) {
1154 	case DMX_TYPE_TS:
1155 		return ttusb_dec_stop_ts_feed(dvbdmxfeed);
1156 
1157 	case DMX_TYPE_SEC:
1158 		return ttusb_dec_stop_sec_feed(dvbdmxfeed);
1159 	}
1160 
1161 	return 0;
1162 }
1163 
ttusb_dec_free_iso_urbs(struct ttusb_dec * dec)1164 static void ttusb_dec_free_iso_urbs(struct ttusb_dec *dec)
1165 {
1166 	int i;
1167 
1168 	dprintk("%s\n", __func__);
1169 
1170 	for (i = 0; i < ISO_BUF_COUNT; i++)
1171 		usb_free_urb(dec->iso_urb[i]);
1172 	kfree(dec->iso_buffer);
1173 }
1174 
ttusb_dec_alloc_iso_urbs(struct ttusb_dec * dec)1175 static int ttusb_dec_alloc_iso_urbs(struct ttusb_dec *dec)
1176 {
1177 	int i;
1178 
1179 	dprintk("%s\n", __func__);
1180 
1181 	dec->iso_buffer = kcalloc(FRAMES_PER_ISO_BUF * ISO_BUF_COUNT,
1182 			ISO_FRAME_SIZE, GFP_KERNEL);
1183 	if (!dec->iso_buffer)
1184 		return -ENOMEM;
1185 
1186 	for (i = 0; i < ISO_BUF_COUNT; i++) {
1187 		struct urb *urb;
1188 
1189 		if (!(urb = usb_alloc_urb(FRAMES_PER_ISO_BUF, GFP_ATOMIC))) {
1190 			ttusb_dec_free_iso_urbs(dec);
1191 			return -ENOMEM;
1192 		}
1193 
1194 		dec->iso_urb[i] = urb;
1195 	}
1196 
1197 	ttusb_dec_setup_urbs(dec);
1198 
1199 	return 0;
1200 }
1201 
ttusb_dec_init_bh_work(struct ttusb_dec * dec)1202 static void ttusb_dec_init_bh_work(struct ttusb_dec *dec)
1203 {
1204 	spin_lock_init(&dec->urb_frame_list_lock);
1205 	INIT_LIST_HEAD(&dec->urb_frame_list);
1206 	INIT_WORK(&dec->urb_bh_work, ttusb_dec_process_urb_frame_list);
1207 }
1208 
ttusb_init_rc(struct ttusb_dec * dec)1209 static int ttusb_init_rc( struct ttusb_dec *dec)
1210 {
1211 	struct input_dev *input_dev;
1212 	u8 b[] = { 0x00, 0x01 };
1213 	int i;
1214 	int err;
1215 
1216 	usb_make_path(dec->udev, dec->rc_phys, sizeof(dec->rc_phys));
1217 	strlcat(dec->rc_phys, "/input0", sizeof(dec->rc_phys));
1218 
1219 	input_dev = input_allocate_device();
1220 	if (!input_dev)
1221 		return -ENOMEM;
1222 
1223 	input_dev->name = "ttusb_dec remote control";
1224 	input_dev->phys = dec->rc_phys;
1225 	input_dev->evbit[0] = BIT_MASK(EV_KEY);
1226 	input_dev->keycodesize = sizeof(u16);
1227 	input_dev->keycodemax = 0x1a;
1228 	input_dev->keycode = rc_keys;
1229 
1230 	for (i = 0; i < ARRAY_SIZE(rc_keys); i++)
1231 		  set_bit(rc_keys[i], input_dev->keybit);
1232 
1233 	err = input_register_device(input_dev);
1234 	if (err) {
1235 		input_free_device(input_dev);
1236 		return err;
1237 	}
1238 
1239 	dec->rc_input_dev = input_dev;
1240 	if (usb_submit_urb(dec->irq_urb, GFP_KERNEL))
1241 		printk("%s: usb_submit_urb failed\n",__func__);
1242 	/* enable irq pipe */
1243 	ttusb_dec_send_command(dec,0xb0,sizeof(b),b,NULL,NULL);
1244 
1245 	return 0;
1246 }
1247 
ttusb_dec_init_v_pes(struct ttusb_dec * dec)1248 static void ttusb_dec_init_v_pes(struct ttusb_dec *dec)
1249 {
1250 	dprintk("%s\n", __func__);
1251 
1252 	dec->v_pes[0] = 0x00;
1253 	dec->v_pes[1] = 0x00;
1254 	dec->v_pes[2] = 0x01;
1255 	dec->v_pes[3] = 0xe0;
1256 }
1257 
ttusb_dec_init_usb(struct ttusb_dec * dec)1258 static int ttusb_dec_init_usb(struct ttusb_dec *dec)
1259 {
1260 	int result;
1261 
1262 	dprintk("%s\n", __func__);
1263 
1264 	mutex_init(&dec->usb_mutex);
1265 	mutex_init(&dec->iso_mutex);
1266 
1267 	dec->command_pipe = usb_sndbulkpipe(dec->udev, COMMAND_PIPE);
1268 	dec->result_pipe = usb_rcvbulkpipe(dec->udev, RESULT_PIPE);
1269 	dec->in_pipe = usb_rcvisocpipe(dec->udev, IN_PIPE);
1270 	dec->out_pipe = usb_sndisocpipe(dec->udev, OUT_PIPE);
1271 	dec->irq_pipe = usb_rcvintpipe(dec->udev, IRQ_PIPE);
1272 
1273 	if(enable_rc) {
1274 		dec->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
1275 		if(!dec->irq_urb) {
1276 			return -ENOMEM;
1277 		}
1278 		dec->irq_buffer = usb_alloc_coherent(dec->udev,IRQ_PACKET_SIZE,
1279 					GFP_KERNEL, &dec->irq_dma_handle);
1280 		if(!dec->irq_buffer) {
1281 			usb_free_urb(dec->irq_urb);
1282 			return -ENOMEM;
1283 		}
1284 		usb_fill_int_urb(dec->irq_urb, dec->udev,dec->irq_pipe,
1285 				 dec->irq_buffer, IRQ_PACKET_SIZE,
1286 				 ttusb_dec_handle_irq, dec, 1);
1287 		dec->irq_urb->transfer_dma = dec->irq_dma_handle;
1288 		dec->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1289 	}
1290 
1291 	result = ttusb_dec_alloc_iso_urbs(dec);
1292 	if (result) {
1293 		usb_free_urb(dec->irq_urb);
1294 		usb_free_coherent(dec->udev, IRQ_PACKET_SIZE,
1295 				  dec->irq_buffer, dec->irq_dma_handle);
1296 	}
1297 	return result;
1298 }
1299 
ttusb_dec_boot_dsp(struct ttusb_dec * dec)1300 static int ttusb_dec_boot_dsp(struct ttusb_dec *dec)
1301 {
1302 	int i, j, actual_len, result, size, trans_count;
1303 	u8 b0[] = { 0x00, 0x00, 0x00, 0x00,
1304 		    0x00, 0x00, 0x00, 0x00,
1305 		    0x61, 0x00 };
1306 	u8 b1[] = { 0x61 };
1307 	u8 *b;
1308 	char idstring[21];
1309 	const u8 *firmware = NULL;
1310 	size_t firmware_size = 0;
1311 	u16 firmware_csum = 0;
1312 	__be16 firmware_csum_ns;
1313 	__be32 firmware_size_nl;
1314 	u32 crc32_csum, crc32_check;
1315 	__be32 tmp;
1316 	const struct firmware *fw_entry = NULL;
1317 
1318 	dprintk("%s\n", __func__);
1319 
1320 	result = request_firmware(&fw_entry, dec->firmware_name, &dec->udev->dev);
1321 	if (result) {
1322 		printk(KERN_ERR "%s: Firmware (%s) unavailable.\n",
1323 		       __func__, dec->firmware_name);
1324 		return result;
1325 	}
1326 
1327 	firmware = fw_entry->data;
1328 	firmware_size = fw_entry->size;
1329 
1330 	if (firmware_size < 60) {
1331 		printk("%s: firmware size too small for DSP code (%zu < 60).\n",
1332 			__func__, firmware_size);
1333 		release_firmware(fw_entry);
1334 		return -ENOENT;
1335 	}
1336 
1337 	/* a 32 bit checksum over the first 56 bytes of the DSP Code is stored
1338 	   at offset 56 of file, so use it to check if the firmware file is
1339 	   valid. */
1340 	crc32_csum = crc32(~0L, firmware, 56) ^ ~0L;
1341 	memcpy(&tmp, &firmware[56], 4);
1342 	crc32_check = ntohl(tmp);
1343 	if (crc32_csum != crc32_check) {
1344 		printk("%s: crc32 check of DSP code failed (calculated 0x%08x != 0x%08x in file), file invalid.\n",
1345 			__func__, crc32_csum, crc32_check);
1346 		release_firmware(fw_entry);
1347 		return -ENOENT;
1348 	}
1349 	memcpy(idstring, &firmware[36], 20);
1350 	idstring[20] = '\0';
1351 	printk(KERN_INFO "ttusb_dec: found DSP code \"%s\".\n", idstring);
1352 
1353 	firmware_size_nl = htonl(firmware_size);
1354 	memcpy(b0, &firmware_size_nl, 4);
1355 	firmware_csum = crc16(~0, firmware, firmware_size) ^ ~0;
1356 	firmware_csum_ns = htons(firmware_csum);
1357 	memcpy(&b0[6], &firmware_csum_ns, 2);
1358 
1359 	result = ttusb_dec_send_command(dec, 0x41, sizeof(b0), b0, NULL, NULL);
1360 
1361 	if (result) {
1362 		release_firmware(fw_entry);
1363 		return result;
1364 	}
1365 
1366 	trans_count = 0;
1367 	j = 0;
1368 
1369 	b = kmalloc(ARM_PACKET_SIZE, GFP_KERNEL);
1370 	if (b == NULL) {
1371 		release_firmware(fw_entry);
1372 		return -ENOMEM;
1373 	}
1374 
1375 	for (i = 0; i < firmware_size; i += COMMAND_PACKET_SIZE) {
1376 		size = firmware_size - i;
1377 		if (size > COMMAND_PACKET_SIZE)
1378 			size = COMMAND_PACKET_SIZE;
1379 
1380 		b[j + 0] = 0xaa;
1381 		b[j + 1] = trans_count++;
1382 		b[j + 2] = 0xf0;
1383 		b[j + 3] = size;
1384 		memcpy(&b[j + 4], &firmware[i], size);
1385 
1386 		j += COMMAND_PACKET_SIZE + 4;
1387 
1388 		if (j >= ARM_PACKET_SIZE) {
1389 			result = usb_bulk_msg(dec->udev, dec->command_pipe, b,
1390 					      ARM_PACKET_SIZE, &actual_len,
1391 					      100);
1392 			j = 0;
1393 		} else if (size < COMMAND_PACKET_SIZE) {
1394 			result = usb_bulk_msg(dec->udev, dec->command_pipe, b,
1395 					      j - COMMAND_PACKET_SIZE + size,
1396 					      &actual_len, 100);
1397 		}
1398 	}
1399 
1400 	result = ttusb_dec_send_command(dec, 0x43, sizeof(b1), b1, NULL, NULL);
1401 
1402 	release_firmware(fw_entry);
1403 	kfree(b);
1404 
1405 	return result;
1406 }
1407 
ttusb_dec_init_stb(struct ttusb_dec * dec)1408 static int ttusb_dec_init_stb(struct ttusb_dec *dec)
1409 {
1410 	int result;
1411 	unsigned int mode = 0, model = 0, version = 0;
1412 
1413 	dprintk("%s\n", __func__);
1414 
1415 	result = ttusb_dec_get_stb_state(dec, &mode, &model, &version);
1416 	if (result)
1417 		return result;
1418 
1419 	if (!mode) {
1420 		if (version == 0xABCDEFAB)
1421 			printk(KERN_INFO "ttusb_dec: no version info in Firmware\n");
1422 		else
1423 			printk(KERN_INFO "ttusb_dec: Firmware %x.%02x%c%c\n",
1424 			       version >> 24, (version >> 16) & 0xff,
1425 			       (version >> 8) & 0xff, version & 0xff);
1426 
1427 		result = ttusb_dec_boot_dsp(dec);
1428 		if (result)
1429 			return result;
1430 	} else {
1431 		/* We can't trust the USB IDs that some firmwares
1432 		   give the box */
1433 		switch (model) {
1434 		case 0x00070001:
1435 		case 0x00070008:
1436 		case 0x0007000c:
1437 			ttusb_dec_set_model(dec, TTUSB_DEC3000S);
1438 			break;
1439 		case 0x00070009:
1440 		case 0x00070013:
1441 			ttusb_dec_set_model(dec, TTUSB_DEC2000T);
1442 			break;
1443 		case 0x00070011:
1444 			ttusb_dec_set_model(dec, TTUSB_DEC2540T);
1445 			break;
1446 		default:
1447 			printk(KERN_ERR "%s: unknown model returned by firmware (%08x) - please report\n",
1448 			       __func__, model);
1449 			return -ENOENT;
1450 		}
1451 		if (version >= 0x01770000)
1452 			dec->can_playback = 1;
1453 	}
1454 	return 0;
1455 }
1456 
ttusb_dec_init_dvb(struct ttusb_dec * dec)1457 static int ttusb_dec_init_dvb(struct ttusb_dec *dec)
1458 {
1459 	int result;
1460 
1461 	dprintk("%s\n", __func__);
1462 
1463 	if ((result = dvb_register_adapter(&dec->adapter,
1464 					   dec->model_name, THIS_MODULE,
1465 					   &dec->udev->dev,
1466 					   adapter_nr)) < 0) {
1467 		printk("%s: dvb_register_adapter failed: error %d\n",
1468 		       __func__, result);
1469 
1470 		return result;
1471 	}
1472 
1473 	dec->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING;
1474 
1475 	dec->demux.priv = (void *)dec;
1476 	dec->demux.filternum = 31;
1477 	dec->demux.feednum = 31;
1478 	dec->demux.start_feed = ttusb_dec_start_feed;
1479 	dec->demux.stop_feed = ttusb_dec_stop_feed;
1480 	dec->demux.write_to_decoder = NULL;
1481 
1482 	if ((result = dvb_dmx_init(&dec->demux)) < 0) {
1483 		printk("%s: dvb_dmx_init failed: error %d\n", __func__,
1484 		       result);
1485 
1486 		dvb_unregister_adapter(&dec->adapter);
1487 
1488 		return result;
1489 	}
1490 
1491 	dec->dmxdev.filternum = 32;
1492 	dec->dmxdev.demux = &dec->demux.dmx;
1493 	dec->dmxdev.capabilities = 0;
1494 
1495 	if ((result = dvb_dmxdev_init(&dec->dmxdev, &dec->adapter)) < 0) {
1496 		printk("%s: dvb_dmxdev_init failed: error %d\n",
1497 		       __func__, result);
1498 
1499 		dvb_dmx_release(&dec->demux);
1500 		dvb_unregister_adapter(&dec->adapter);
1501 
1502 		return result;
1503 	}
1504 
1505 	dec->frontend.source = DMX_FRONTEND_0;
1506 
1507 	if ((result = dec->demux.dmx.add_frontend(&dec->demux.dmx,
1508 						  &dec->frontend)) < 0) {
1509 		printk("%s: dvb_dmx_init failed: error %d\n", __func__,
1510 		       result);
1511 
1512 		dvb_dmxdev_release(&dec->dmxdev);
1513 		dvb_dmx_release(&dec->demux);
1514 		dvb_unregister_adapter(&dec->adapter);
1515 
1516 		return result;
1517 	}
1518 
1519 	if ((result = dec->demux.dmx.connect_frontend(&dec->demux.dmx,
1520 						      &dec->frontend)) < 0) {
1521 		printk("%s: dvb_dmx_init failed: error %d\n", __func__,
1522 		       result);
1523 
1524 		dec->demux.dmx.remove_frontend(&dec->demux.dmx, &dec->frontend);
1525 		dvb_dmxdev_release(&dec->dmxdev);
1526 		dvb_dmx_release(&dec->demux);
1527 		dvb_unregister_adapter(&dec->adapter);
1528 
1529 		return result;
1530 	}
1531 
1532 	dvb_net_init(&dec->adapter, &dec->dvb_net, &dec->demux.dmx);
1533 
1534 	return 0;
1535 }
1536 
ttusb_dec_exit_dvb(struct ttusb_dec * dec)1537 static void ttusb_dec_exit_dvb(struct ttusb_dec *dec)
1538 {
1539 	dprintk("%s\n", __func__);
1540 
1541 	dvb_net_release(&dec->dvb_net);
1542 	dec->demux.dmx.close(&dec->demux.dmx);
1543 	dec->demux.dmx.remove_frontend(&dec->demux.dmx, &dec->frontend);
1544 	dvb_dmxdev_release(&dec->dmxdev);
1545 	dvb_dmx_release(&dec->demux);
1546 	if (dec->fe) {
1547 		dvb_unregister_frontend(dec->fe);
1548 		dvb_frontend_detach(dec->fe);
1549 	}
1550 	dvb_unregister_adapter(&dec->adapter);
1551 }
1552 
ttusb_dec_exit_rc(struct ttusb_dec * dec)1553 static void ttusb_dec_exit_rc(struct ttusb_dec *dec)
1554 {
1555 	dprintk("%s\n", __func__);
1556 
1557 	if (dec->rc_input_dev) {
1558 		input_unregister_device(dec->rc_input_dev);
1559 		dec->rc_input_dev = NULL;
1560 	}
1561 }
1562 
1563 
ttusb_dec_exit_usb(struct ttusb_dec * dec)1564 static void ttusb_dec_exit_usb(struct ttusb_dec *dec)
1565 {
1566 	int i;
1567 
1568 	dprintk("%s\n", __func__);
1569 
1570 	if (enable_rc) {
1571 		/* we have to check whether the irq URB is already submitted.
1572 		 * As the irq is submitted after the interface is changed,
1573 		 * this is the best method i figured out.
1574 		 * Any others?*/
1575 		if (dec->interface == TTUSB_DEC_INTERFACE_IN)
1576 			usb_kill_urb(dec->irq_urb);
1577 
1578 		usb_free_urb(dec->irq_urb);
1579 
1580 		usb_free_coherent(dec->udev, IRQ_PACKET_SIZE,
1581 				  dec->irq_buffer, dec->irq_dma_handle);
1582 	}
1583 
1584 	dec->iso_stream_count = 0;
1585 
1586 	for (i = 0; i < ISO_BUF_COUNT; i++)
1587 		usb_kill_urb(dec->iso_urb[i]);
1588 
1589 	ttusb_dec_free_iso_urbs(dec);
1590 }
1591 
ttusb_dec_exit_bh_work(struct ttusb_dec * dec)1592 static void ttusb_dec_exit_bh_work(struct ttusb_dec *dec)
1593 {
1594 	struct list_head *item;
1595 	struct urb_frame *frame;
1596 
1597 	cancel_work_sync(&dec->urb_bh_work);
1598 
1599 	while ((item = dec->urb_frame_list.next) != &dec->urb_frame_list) {
1600 		frame = list_entry(item, struct urb_frame, urb_frame_list);
1601 		list_del(&frame->urb_frame_list);
1602 		kfree(frame);
1603 	}
1604 }
1605 
ttusb_dec_init_filters(struct ttusb_dec * dec)1606 static void ttusb_dec_init_filters(struct ttusb_dec *dec)
1607 {
1608 	INIT_LIST_HEAD(&dec->filter_info_list);
1609 	spin_lock_init(&dec->filter_info_list_lock);
1610 }
1611 
ttusb_dec_exit_filters(struct ttusb_dec * dec)1612 static void ttusb_dec_exit_filters(struct ttusb_dec *dec)
1613 {
1614 	struct list_head *item;
1615 	struct filter_info *finfo;
1616 
1617 	while ((item = dec->filter_info_list.next) != &dec->filter_info_list) {
1618 		finfo = list_entry(item, struct filter_info, filter_info_list);
1619 		list_del(&finfo->filter_info_list);
1620 		kfree(finfo);
1621 	}
1622 }
1623 
fe_send_command(struct dvb_frontend * fe,const u8 command,int param_length,const u8 params[],int * result_length,u8 cmd_result[])1624 static int fe_send_command(struct dvb_frontend* fe, const u8 command,
1625 			   int param_length, const u8 params[],
1626 			   int *result_length, u8 cmd_result[])
1627 {
1628 	struct ttusb_dec* dec = fe->dvb->priv;
1629 	return ttusb_dec_send_command(dec, command, param_length, params, result_length, cmd_result);
1630 }
1631 
1632 static const struct ttusbdecfe_config fe_config = {
1633 	.send_command = fe_send_command
1634 };
1635 
ttusb_dec_probe(struct usb_interface * intf,const struct usb_device_id * id)1636 static int ttusb_dec_probe(struct usb_interface *intf,
1637 			   const struct usb_device_id *id)
1638 {
1639 	struct usb_device *udev;
1640 	struct ttusb_dec *dec;
1641 	int result;
1642 
1643 	dprintk("%s\n", __func__);
1644 
1645 	udev = interface_to_usbdev(intf);
1646 
1647 	if (!(dec = kzalloc(sizeof(struct ttusb_dec), GFP_KERNEL))) {
1648 		printk("%s: couldn't allocate memory.\n", __func__);
1649 		return -ENOMEM;
1650 	}
1651 
1652 	usb_set_intfdata(intf, (void *)dec);
1653 
1654 	switch (id->idProduct) {
1655 	case 0x1006:
1656 		ttusb_dec_set_model(dec, TTUSB_DEC3000S);
1657 		break;
1658 
1659 	case 0x1008:
1660 		ttusb_dec_set_model(dec, TTUSB_DEC2000T);
1661 		break;
1662 
1663 	case 0x1009:
1664 		ttusb_dec_set_model(dec, TTUSB_DEC2540T);
1665 		break;
1666 	}
1667 
1668 	dec->udev = udev;
1669 
1670 	result = ttusb_dec_init_usb(dec);
1671 	if (result)
1672 		goto err_usb;
1673 	result = ttusb_dec_init_stb(dec);
1674 	if (result)
1675 		goto err_stb;
1676 	result = ttusb_dec_init_dvb(dec);
1677 	if (result)
1678 		goto err_stb;
1679 
1680 	dec->adapter.priv = dec;
1681 	switch (id->idProduct) {
1682 	case 0x1006:
1683 		dec->fe = ttusbdecfe_dvbs_attach(&fe_config);
1684 		break;
1685 
1686 	case 0x1008:
1687 	case 0x1009:
1688 		dec->fe = ttusbdecfe_dvbt_attach(&fe_config);
1689 		break;
1690 	}
1691 
1692 	if (dec->fe == NULL) {
1693 		printk("dvb-ttusb-dec: A frontend driver was not found for device [%04x:%04x]\n",
1694 		       le16_to_cpu(dec->udev->descriptor.idVendor),
1695 		       le16_to_cpu(dec->udev->descriptor.idProduct));
1696 	} else {
1697 		if (dvb_register_frontend(&dec->adapter, dec->fe)) {
1698 			printk("budget-ci: Frontend registration failed!\n");
1699 			if (dec->fe->ops.release)
1700 				dec->fe->ops.release(dec->fe);
1701 			dec->fe = NULL;
1702 		}
1703 	}
1704 
1705 	ttusb_dec_init_v_pes(dec);
1706 	ttusb_dec_init_filters(dec);
1707 	ttusb_dec_init_bh_work(dec);
1708 
1709 	dec->active = 1;
1710 
1711 	ttusb_dec_set_interface(dec, TTUSB_DEC_INTERFACE_IN);
1712 
1713 	if (enable_rc)
1714 		ttusb_init_rc(dec);
1715 
1716 	return 0;
1717 err_stb:
1718 	ttusb_dec_exit_usb(dec);
1719 err_usb:
1720 	kfree(dec);
1721 	return result;
1722 }
1723 
ttusb_dec_disconnect(struct usb_interface * intf)1724 static void ttusb_dec_disconnect(struct usb_interface *intf)
1725 {
1726 	struct ttusb_dec *dec = usb_get_intfdata(intf);
1727 
1728 	usb_set_intfdata(intf, NULL);
1729 
1730 	dprintk("%s\n", __func__);
1731 
1732 	if (dec->active) {
1733 		ttusb_dec_exit_bh_work(dec);
1734 		ttusb_dec_exit_filters(dec);
1735 		if(enable_rc)
1736 			ttusb_dec_exit_rc(dec);
1737 		ttusb_dec_exit_usb(dec);
1738 		ttusb_dec_exit_dvb(dec);
1739 	}
1740 
1741 	kfree(dec);
1742 }
1743 
ttusb_dec_set_model(struct ttusb_dec * dec,enum ttusb_dec_model model)1744 static void ttusb_dec_set_model(struct ttusb_dec *dec,
1745 				enum ttusb_dec_model model)
1746 {
1747 	dec->model = model;
1748 
1749 	switch (model) {
1750 	case TTUSB_DEC2000T:
1751 		dec->model_name = "DEC2000-t";
1752 		dec->firmware_name = "dvb-ttusb-dec-2000t.fw";
1753 		break;
1754 
1755 	case TTUSB_DEC2540T:
1756 		dec->model_name = "DEC2540-t";
1757 		dec->firmware_name = "dvb-ttusb-dec-2540t.fw";
1758 		break;
1759 
1760 	case TTUSB_DEC3000S:
1761 		dec->model_name = "DEC3000-s";
1762 		dec->firmware_name = "dvb-ttusb-dec-3000s.fw";
1763 		break;
1764 	}
1765 }
1766 
1767 static const struct usb_device_id ttusb_dec_table[] = {
1768 	{USB_DEVICE(0x0b48, 0x1006)},	/* DEC3000-s */
1769 	/*{USB_DEVICE(0x0b48, 0x1007)},	   Unconfirmed */
1770 	{USB_DEVICE(0x0b48, 0x1008)},	/* DEC2000-t */
1771 	{USB_DEVICE(0x0b48, 0x1009)},	/* DEC2540-t */
1772 	{}
1773 };
1774 
1775 static struct usb_driver ttusb_dec_driver = {
1776 	.name		= "ttusb-dec",
1777 	.probe		= ttusb_dec_probe,
1778 	.disconnect	= ttusb_dec_disconnect,
1779 	.id_table	= ttusb_dec_table,
1780 };
1781 
1782 module_usb_driver(ttusb_dec_driver);
1783 
1784 MODULE_AUTHOR("Alex Woods <linux-dvb@giblets.org>");
1785 MODULE_DESCRIPTION(DRIVER_NAME);
1786 MODULE_LICENSE("GPL");
1787 MODULE_DEVICE_TABLE(usb, ttusb_dec_table);
1788