1 /*
2  * TTUSB DVB driver
3  *
4  * Copyright (c) 2002 Holger Waechtler <holger@convergence.de>
5  * Copyright (c) 2003 Felix Domke <tmbinc@elitedvb.net>
6  *
7  *	This program is free software; you can redistribute it and/or
8  *	modify it under the terms of the GNU General Public License as
9  *	published by the Free Software Foundation; either version 2 of
10  *	the License, or (at your option) any later version.
11  */
12 #include <linux/init.h>
13 #include <linux/slab.h>
14 #include <linux/wait.h>
15 #include <linux/fs.h>
16 #include <linux/module.h>
17 #include <linux/usb.h>
18 #include <linux/delay.h>
19 #include <linux/time.h>
20 #include <linux/errno.h>
21 #include <linux/jiffies.h>
22 #include <linux/mutex.h>
23 #include <linux/firmware.h>
24 
25 #include "dvb_frontend.h"
26 #include "dmxdev.h"
27 #include "dvb_demux.h"
28 #include "dvb_net.h"
29 #include "ves1820.h"
30 #include "cx22700.h"
31 #include "tda1004x.h"
32 #include "stv0299.h"
33 #include "tda8083.h"
34 #include "stv0297.h"
35 #include "lnbp21.h"
36 
37 #include <linux/dvb/frontend.h>
38 #include <linux/dvb/dmx.h>
39 #include <linux/pci.h>
40 
41 /*
42   TTUSB_HWSECTIONS:
43     the DSP supports filtering in hardware, however, since the "muxstream"
44     is a bit braindead (no matching channel masks or no matching filter mask),
45     we won't support this - yet. it doesn't event support negative filters,
46     so the best way is maybe to keep TTUSB_HWSECTIONS undef'd and just
47     parse TS data. USB bandwidth will be a problem when having large
48     datastreams, especially for dvb-net, but hey, that's not my problem.
49 
50   TTUSB_DISEQC, TTUSB_TONE:
51     let the STC do the diseqc/tone stuff. this isn't supported at least with
52     my TTUSB, so let it undef'd unless you want to implement another
53     frontend. never tested.
54 
55   debug:
56     define it to > 3 for really hardcore debugging. you probably don't want
57     this unless the device doesn't load at all. > 2 for bandwidth statistics.
58 */
59 
60 static int debug;
61 module_param(debug, int, 0644);
62 MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
63 
64 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
65 
66 #define dprintk(x...) do { if (debug) printk(KERN_DEBUG x); } while (0)
67 
68 #define ISO_BUF_COUNT      4
69 #define FRAMES_PER_ISO_BUF 4
70 #define ISO_FRAME_SIZE     912
71 #define TTUSB_MAXCHANNEL   32
72 #ifdef TTUSB_HWSECTIONS
73 #define TTUSB_MAXFILTER    16	/* ??? */
74 #endif
75 
76 #define TTUSB_REV_2_2	0x22
77 #define TTUSB_BUDGET_NAME "ttusb_stc_fw"
78 
79 /**
80  *  since we're casting (struct ttusb*) <-> (struct dvb_demux*) around
81  *  the dvb_demux field must be the first in struct!!
82  */
83 struct ttusb {
84 	struct dvb_demux dvb_demux;
85 	struct dmxdev dmxdev;
86 	struct dvb_net dvbnet;
87 
88 	/* and one for USB access. */
89 	struct mutex semi2c;
90 	struct mutex semusb;
91 
92 	struct dvb_adapter adapter;
93 	struct usb_device *dev;
94 
95 	struct i2c_adapter i2c_adap;
96 
97 	int disconnecting;
98 	int iso_streaming;
99 
100 	unsigned int bulk_out_pipe;
101 	unsigned int bulk_in_pipe;
102 	unsigned int isoc_in_pipe;
103 
104 	void *iso_buffer;
105 	dma_addr_t iso_dma_handle;
106 
107 	struct urb *iso_urb[ISO_BUF_COUNT];
108 
109 	int running_feed_count;
110 	int last_channel;
111 	int last_filter;
112 
113 	u8 c;			/* transaction counter, wraps around...  */
114 	enum fe_sec_tone_mode tone;
115 	enum fe_sec_voltage voltage;
116 
117 	int mux_state;		// 0..2 - MuxSyncWord, 3 - nMuxPacks,    4 - muxpack
118 	u8 mux_npacks;
119 	u8 muxpack[256 + 8];
120 	int muxpack_ptr, muxpack_len;
121 
122 	int insync;
123 
124 	int cc;			/* MuxCounter - will increment on EVERY MUX PACKET */
125 	/* (including stuffing. yes. really.) */
126 
127 	u8 last_result[32];
128 
129 	int revision;
130 
131 	struct dvb_frontend* fe;
132 };
133 
134 /* ugly workaround ... don't know why it's necessary to read */
135 /* all result codes. */
136 
137 static int ttusb_cmd(struct ttusb *ttusb,
138 	      const u8 * data, int len, int needresult)
139 {
140 	int actual_len;
141 	int err;
142 	int i;
143 
144 	if (debug >= 3) {
145 		printk(KERN_DEBUG ">");
146 		for (i = 0; i < len; ++i)
147 			printk(KERN_CONT " %02x", data[i]);
148 		printk(KERN_CONT "\n");
149 	}
150 
151 	if (mutex_lock_interruptible(&ttusb->semusb) < 0)
152 		return -EAGAIN;
153 
154 	err = usb_bulk_msg(ttusb->dev, ttusb->bulk_out_pipe,
155 			   (u8 *) data, len, &actual_len, 1000);
156 	if (err != 0) {
157 		dprintk("%s: usb_bulk_msg(send) failed, err == %i!\n",
158 			__func__, err);
159 		mutex_unlock(&ttusb->semusb);
160 		return err;
161 	}
162 	if (actual_len != len) {
163 		dprintk("%s: only wrote %d of %d bytes\n", __func__,
164 			actual_len, len);
165 		mutex_unlock(&ttusb->semusb);
166 		return -1;
167 	}
168 
169 	err = usb_bulk_msg(ttusb->dev, ttusb->bulk_in_pipe,
170 			   ttusb->last_result, 32, &actual_len, 1000);
171 
172 	if (err != 0) {
173 		printk("%s: failed, receive error %d\n", __func__,
174 		       err);
175 		mutex_unlock(&ttusb->semusb);
176 		return err;
177 	}
178 
179 	if (debug >= 3) {
180 		actual_len = ttusb->last_result[3] + 4;
181 		printk(KERN_DEBUG "<");
182 		for (i = 0; i < actual_len; ++i)
183 			printk(KERN_CONT " %02x", ttusb->last_result[i]);
184 		printk(KERN_CONT "\n");
185 	}
186 
187 	if (!needresult)
188 		mutex_unlock(&ttusb->semusb);
189 	return 0;
190 }
191 
192 static int ttusb_result(struct ttusb *ttusb, u8 * data, int len)
193 {
194 	memcpy(data, ttusb->last_result, len);
195 	mutex_unlock(&ttusb->semusb);
196 	return 0;
197 }
198 
199 static int ttusb_i2c_msg(struct ttusb *ttusb,
200 		  u8 addr, u8 * snd_buf, u8 snd_len, u8 * rcv_buf,
201 		  u8 rcv_len)
202 {
203 	u8 b[0x28];
204 	u8 id = ++ttusb->c;
205 	int i, err;
206 
207 	if (snd_len > 0x28 - 7 || rcv_len > 0x20 - 7)
208 		return -EINVAL;
209 
210 	b[0] = 0xaa;
211 	b[1] = id;
212 	b[2] = 0x31;
213 	b[3] = snd_len + 3;
214 	b[4] = addr << 1;
215 	b[5] = snd_len;
216 	b[6] = rcv_len;
217 
218 	for (i = 0; i < snd_len; i++)
219 		b[7 + i] = snd_buf[i];
220 
221 	err = ttusb_cmd(ttusb, b, snd_len + 7, 1);
222 
223 	if (err)
224 		return -EREMOTEIO;
225 
226 	err = ttusb_result(ttusb, b, 0x20);
227 
228 	/* check if the i2c transaction was successful */
229 	if ((snd_len != b[5]) || (rcv_len != b[6])) return -EREMOTEIO;
230 
231 	if (rcv_len > 0) {
232 
233 		if (err || b[0] != 0x55 || b[1] != id) {
234 			dprintk
235 			    ("%s: usb_bulk_msg(recv) failed, err == %i, id == %02x, b == ",
236 			     __func__, err, id);
237 			return -EREMOTEIO;
238 		}
239 
240 		for (i = 0; i < rcv_len; i++)
241 			rcv_buf[i] = b[7 + i];
242 	}
243 
244 	return rcv_len;
245 }
246 
247 static int master_xfer(struct i2c_adapter* adapter, struct i2c_msg *msg, int num)
248 {
249 	struct ttusb *ttusb = i2c_get_adapdata(adapter);
250 	int i = 0;
251 	int inc;
252 
253 	if (mutex_lock_interruptible(&ttusb->semi2c) < 0)
254 		return -EAGAIN;
255 
256 	while (i < num) {
257 		u8 addr, snd_len, rcv_len, *snd_buf, *rcv_buf;
258 		int err;
259 
260 		if (num > i + 1 && (msg[i + 1].flags & I2C_M_RD)) {
261 			addr = msg[i].addr;
262 			snd_buf = msg[i].buf;
263 			snd_len = msg[i].len;
264 			rcv_buf = msg[i + 1].buf;
265 			rcv_len = msg[i + 1].len;
266 			inc = 2;
267 		} else {
268 			addr = msg[i].addr;
269 			snd_buf = msg[i].buf;
270 			snd_len = msg[i].len;
271 			rcv_buf = NULL;
272 			rcv_len = 0;
273 			inc = 1;
274 		}
275 
276 		err = ttusb_i2c_msg(ttusb, addr,
277 				    snd_buf, snd_len, rcv_buf, rcv_len);
278 
279 		if (err < rcv_len) {
280 			dprintk("%s: i == %i\n", __func__, i);
281 			break;
282 		}
283 
284 		i += inc;
285 	}
286 
287 	mutex_unlock(&ttusb->semi2c);
288 	return i;
289 }
290 
291 static int ttusb_boot_dsp(struct ttusb *ttusb)
292 {
293 	const struct firmware *fw;
294 	int i, err;
295 	u8 b[40];
296 
297 	err = request_firmware(&fw, "ttusb-budget/dspbootcode.bin",
298 			       &ttusb->dev->dev);
299 	if (err) {
300 		printk(KERN_ERR "ttusb-budget: failed to request firmware\n");
301 		return err;
302 	}
303 
304 	/* BootBlock */
305 	b[0] = 0xaa;
306 	b[2] = 0x13;
307 	b[3] = 28;
308 
309 	/* upload dsp code in 32 byte steps (36 didn't work for me ...) */
310 	/* 32 is max packet size, no messages should be splitted. */
311 	for (i = 0; i < fw->size; i += 28) {
312 		memcpy(&b[4], &fw->data[i], 28);
313 
314 		b[1] = ++ttusb->c;
315 
316 		err = ttusb_cmd(ttusb, b, 32, 0);
317 		if (err)
318 			goto done;
319 	}
320 
321 	/* last block ... */
322 	b[1] = ++ttusb->c;
323 	b[2] = 0x13;
324 	b[3] = 0;
325 
326 	err = ttusb_cmd(ttusb, b, 4, 0);
327 	if (err)
328 		goto done;
329 
330 	/* BootEnd */
331 	b[1] = ++ttusb->c;
332 	b[2] = 0x14;
333 	b[3] = 0;
334 
335 	err = ttusb_cmd(ttusb, b, 4, 0);
336 
337       done:
338 	release_firmware(fw);
339 	if (err) {
340 		dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
341 			__func__, err);
342 	}
343 
344 	return err;
345 }
346 
347 static int ttusb_set_channel(struct ttusb *ttusb, int chan_id, int filter_type,
348 		      int pid)
349 {
350 	int err;
351 	/* SetChannel */
352 	u8 b[] = { 0xaa, ++ttusb->c, 0x22, 4, chan_id, filter_type,
353 		(pid >> 8) & 0xff, pid & 0xff
354 	};
355 
356 	err = ttusb_cmd(ttusb, b, sizeof(b), 0);
357 	return err;
358 }
359 
360 static int ttusb_del_channel(struct ttusb *ttusb, int channel_id)
361 {
362 	int err;
363 	/* DelChannel */
364 	u8 b[] = { 0xaa, ++ttusb->c, 0x23, 1, channel_id };
365 
366 	err = ttusb_cmd(ttusb, b, sizeof(b), 0);
367 	return err;
368 }
369 
370 #ifdef TTUSB_HWSECTIONS
371 static int ttusb_set_filter(struct ttusb *ttusb, int filter_id,
372 		     int associated_chan, u8 filter[8], u8 mask[8])
373 {
374 	int err;
375 	/* SetFilter */
376 	u8 b[] = { 0xaa, 0, 0x24, 0x1a, filter_id, associated_chan,
377 		filter[0], filter[1], filter[2], filter[3],
378 		filter[4], filter[5], filter[6], filter[7],
379 		filter[8], filter[9], filter[10], filter[11],
380 		mask[0], mask[1], mask[2], mask[3],
381 		mask[4], mask[5], mask[6], mask[7],
382 		mask[8], mask[9], mask[10], mask[11]
383 	};
384 
385 	err = ttusb_cmd(ttusb, b, sizeof(b), 0);
386 	return err;
387 }
388 
389 static int ttusb_del_filter(struct ttusb *ttusb, int filter_id)
390 {
391 	int err;
392 	/* DelFilter */
393 	u8 b[] = { 0xaa, ++ttusb->c, 0x25, 1, filter_id };
394 
395 	err = ttusb_cmd(ttusb, b, sizeof(b), 0);
396 	return err;
397 }
398 #endif
399 
400 static int ttusb_init_controller(struct ttusb *ttusb)
401 {
402 	u8 b0[] = { 0xaa, ++ttusb->c, 0x15, 1, 0 };
403 	u8 b1[] = { 0xaa, ++ttusb->c, 0x15, 1, 1 };
404 	u8 b2[] = { 0xaa, ++ttusb->c, 0x32, 1, 0 };
405 	/* i2c write read: 5 bytes, addr 0x10, 0x02 bytes write, 1 bytes read. */
406 	u8 b3[] =
407 	    { 0xaa, ++ttusb->c, 0x31, 5, 0x10, 0x02, 0x01, 0x00, 0x1e };
408 	u8 b4[] =
409 	    { 0x55, ttusb->c, 0x31, 4, 0x10, 0x02, 0x01, 0x00, 0x1e };
410 
411 	u8 get_version[] = { 0xaa, ++ttusb->c, 0x17, 5, 0, 0, 0, 0, 0 };
412 	u8 get_dsp_version[0x20] =
413 	    { 0xaa, ++ttusb->c, 0x26, 28, 0, 0, 0, 0, 0 };
414 	int err;
415 
416 	/* reset board */
417 	if ((err = ttusb_cmd(ttusb, b0, sizeof(b0), 0)))
418 		return err;
419 
420 	/* reset board (again?) */
421 	if ((err = ttusb_cmd(ttusb, b1, sizeof(b1), 0)))
422 		return err;
423 
424 	ttusb_boot_dsp(ttusb);
425 
426 	/* set i2c bit rate */
427 	if ((err = ttusb_cmd(ttusb, b2, sizeof(b2), 0)))
428 		return err;
429 
430 	if ((err = ttusb_cmd(ttusb, b3, sizeof(b3), 1)))
431 		return err;
432 
433 	err = ttusb_result(ttusb, b4, sizeof(b4));
434 
435 	if ((err = ttusb_cmd(ttusb, get_version, sizeof(get_version), 1)))
436 		return err;
437 
438 	if ((err = ttusb_result(ttusb, get_version, sizeof(get_version))))
439 		return err;
440 
441 	dprintk("%s: stc-version: %c%c%c%c%c\n", __func__,
442 		get_version[4], get_version[5], get_version[6],
443 		get_version[7], get_version[8]);
444 
445 	if (memcmp(get_version + 4, "V 0.0", 5) &&
446 	    memcmp(get_version + 4, "V 1.1", 5) &&
447 	    memcmp(get_version + 4, "V 2.1", 5) &&
448 	    memcmp(get_version + 4, "V 2.2", 5)) {
449 		printk
450 		    ("%s: unknown STC version %c%c%c%c%c, please report!\n",
451 		     __func__, get_version[4], get_version[5],
452 		     get_version[6], get_version[7], get_version[8]);
453 	}
454 
455 	ttusb->revision = ((get_version[6] - '0') << 4) |
456 			   (get_version[8] - '0');
457 
458 	err =
459 	    ttusb_cmd(ttusb, get_dsp_version, sizeof(get_dsp_version), 1);
460 	if (err)
461 		return err;
462 
463 	err =
464 	    ttusb_result(ttusb, get_dsp_version, sizeof(get_dsp_version));
465 	if (err)
466 		return err;
467 	printk("%s: dsp-version: %c%c%c\n", __func__,
468 	       get_dsp_version[4], get_dsp_version[5], get_dsp_version[6]);
469 	return 0;
470 }
471 
472 #ifdef TTUSB_DISEQC
473 static int ttusb_send_diseqc(struct dvb_frontend* fe,
474 			     const struct dvb_diseqc_master_cmd *cmd)
475 {
476 	struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
477 	u8 b[12] = { 0xaa, ++ttusb->c, 0x18 };
478 
479 	int err;
480 
481 	b[3] = 4 + 2 + cmd->msg_len;
482 	b[4] = 0xFF;		/* send diseqc master, not burst */
483 	b[5] = cmd->msg_len;
484 
485 	memcpy(b + 5, cmd->msg, cmd->msg_len);
486 
487 	/* Diseqc */
488 	if ((err = ttusb_cmd(ttusb, b, 4 + b[3], 0))) {
489 		dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
490 			__func__, err);
491 	}
492 
493 	return err;
494 }
495 #endif
496 
497 static int ttusb_update_lnb(struct ttusb *ttusb)
498 {
499 	u8 b[] = { 0xaa, ++ttusb->c, 0x16, 5, /*power: */ 1,
500 		ttusb->voltage == SEC_VOLTAGE_18 ? 0 : 1,
501 		ttusb->tone == SEC_TONE_ON ? 1 : 0, 1, 1
502 	};
503 	int err;
504 
505 	/* SetLNB */
506 	if ((err = ttusb_cmd(ttusb, b, sizeof(b), 0))) {
507 		dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
508 			__func__, err);
509 	}
510 
511 	return err;
512 }
513 
514 static int ttusb_set_voltage(struct dvb_frontend *fe,
515 			     enum fe_sec_voltage voltage)
516 {
517 	struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
518 
519 	ttusb->voltage = voltage;
520 	return ttusb_update_lnb(ttusb);
521 }
522 
523 #ifdef TTUSB_TONE
524 static int ttusb_set_tone(struct dvb_frontend *fe, enum fe_sec_tone_mode tone)
525 {
526 	struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
527 
528 	ttusb->tone = tone;
529 	return ttusb_update_lnb(ttusb);
530 }
531 #endif
532 
533 
534 #if 0
535 static void ttusb_set_led_freq(struct ttusb *ttusb, u8 freq)
536 {
537 	u8 b[] = { 0xaa, ++ttusb->c, 0x19, 1, freq };
538 	int err, actual_len;
539 
540 	err = ttusb_cmd(ttusb, b, sizeof(b), 0);
541 	if (err) {
542 		dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
543 			__func__, err);
544 	}
545 }
546 #endif
547 
548 /*****************************************************************************/
549 
550 #ifdef TTUSB_HWSECTIONS
551 static void ttusb_handle_ts_data(struct ttusb_channel *channel,
552 				 const u8 * data, int len);
553 static void ttusb_handle_sec_data(struct ttusb_channel *channel,
554 				  const u8 * data, int len);
555 #endif
556 
557 static int numpkt, numts, numstuff, numsec, numinvalid;
558 static unsigned long lastj;
559 
560 static void ttusb_process_muxpack(struct ttusb *ttusb, const u8 * muxpack,
561 			   int len)
562 {
563 	u16 csum = 0, cc;
564 	int i;
565 
566 	if (len < 4 || len & 0x1) {
567 		pr_warn("%s: muxpack has invalid len %d\n", __func__, len);
568 		numinvalid++;
569 		return;
570 	}
571 
572 	for (i = 0; i < len; i += 2)
573 		csum ^= le16_to_cpup((__le16 *) (muxpack + i));
574 	if (csum) {
575 		printk("%s: muxpack with incorrect checksum, ignoring\n",
576 		       __func__);
577 		numinvalid++;
578 		return;
579 	}
580 
581 	cc = (muxpack[len - 4] << 8) | muxpack[len - 3];
582 	cc &= 0x7FFF;
583 	if ((cc != ttusb->cc) && (ttusb->cc != -1))
584 		printk("%s: cc discontinuity (%d frames missing)\n",
585 		       __func__, (cc - ttusb->cc) & 0x7FFF);
586 	ttusb->cc = (cc + 1) & 0x7FFF;
587 	if (muxpack[0] & 0x80) {
588 #ifdef TTUSB_HWSECTIONS
589 		/* section data */
590 		int pusi = muxpack[0] & 0x40;
591 		int channel = muxpack[0] & 0x1F;
592 		int payload = muxpack[1];
593 		const u8 *data = muxpack + 2;
594 		/* check offset flag */
595 		if (muxpack[0] & 0x20)
596 			data++;
597 
598 		ttusb_handle_sec_data(ttusb->channel + channel, data,
599 				      payload);
600 		data += payload;
601 
602 		if ((!!(ttusb->muxpack[0] & 0x20)) ^
603 		    !!(ttusb->muxpack[1] & 1))
604 			data++;
605 #warning TODO: pusi
606 		printk("cc: %04x\n", (data[0] << 8) | data[1]);
607 #endif
608 		numsec++;
609 	} else if (muxpack[0] == 0x47) {
610 #ifdef TTUSB_HWSECTIONS
611 		/* we have TS data here! */
612 		int pid = ((muxpack[1] & 0x0F) << 8) | muxpack[2];
613 		int channel;
614 		for (channel = 0; channel < TTUSB_MAXCHANNEL; ++channel)
615 			if (ttusb->channel[channel].active
616 			    && (pid == ttusb->channel[channel].pid))
617 				ttusb_handle_ts_data(ttusb->channel +
618 						     channel, muxpack,
619 						     188);
620 #endif
621 		numts++;
622 		dvb_dmx_swfilter_packets(&ttusb->dvb_demux, muxpack, 1);
623 	} else if (muxpack[0] != 0) {
624 		numinvalid++;
625 		printk("illegal muxpack type %02x\n", muxpack[0]);
626 	} else
627 		numstuff++;
628 }
629 
630 static void ttusb_process_frame(struct ttusb *ttusb, u8 * data, int len)
631 {
632 	int maxwork = 1024;
633 	while (len) {
634 		if (!(maxwork--)) {
635 			printk("%s: too much work\n", __func__);
636 			break;
637 		}
638 
639 		switch (ttusb->mux_state) {
640 		case 0:
641 		case 1:
642 		case 2:
643 			len--;
644 			if (*data++ == 0xAA)
645 				++ttusb->mux_state;
646 			else {
647 				ttusb->mux_state = 0;
648 				if (ttusb->insync) {
649 					dprintk("%s: %02x\n",
650 						__func__, data[-1]);
651 					printk(KERN_INFO "%s: lost sync.\n",
652 					       __func__);
653 					ttusb->insync = 0;
654 				}
655 			}
656 			break;
657 		case 3:
658 			ttusb->insync = 1;
659 			len--;
660 			ttusb->mux_npacks = *data++;
661 			++ttusb->mux_state;
662 			ttusb->muxpack_ptr = 0;
663 			/* maximum bytes, until we know the length */
664 			ttusb->muxpack_len = 2;
665 			break;
666 		case 4:
667 			{
668 				int avail;
669 				avail = len;
670 				if (avail >
671 				    (ttusb->muxpack_len -
672 				     ttusb->muxpack_ptr))
673 					avail =
674 					    ttusb->muxpack_len -
675 					    ttusb->muxpack_ptr;
676 				memcpy(ttusb->muxpack + ttusb->muxpack_ptr,
677 				       data, avail);
678 				ttusb->muxpack_ptr += avail;
679 				BUG_ON(ttusb->muxpack_ptr > 264);
680 				data += avail;
681 				len -= avail;
682 				/* determine length */
683 				if (ttusb->muxpack_ptr == 2) {
684 					if (ttusb->muxpack[0] & 0x80) {
685 						ttusb->muxpack_len =
686 						    ttusb->muxpack[1] + 2;
687 						if (ttusb->
688 						    muxpack[0] & 0x20)
689 							ttusb->
690 							    muxpack_len++;
691 						if ((!!
692 						     (ttusb->
693 						      muxpack[0] & 0x20)) ^
694 						    !!(ttusb->
695 						       muxpack[1] & 1))
696 							ttusb->
697 							    muxpack_len++;
698 						ttusb->muxpack_len += 4;
699 					} else if (ttusb->muxpack[0] ==
700 						   0x47)
701 						ttusb->muxpack_len =
702 						    188 + 4;
703 					else if (ttusb->muxpack[0] == 0x00)
704 						ttusb->muxpack_len =
705 						    ttusb->muxpack[1] + 2 +
706 						    4;
707 					else {
708 						dprintk
709 						    ("%s: invalid state: first byte is %x\n",
710 						     __func__,
711 						     ttusb->muxpack[0]);
712 						ttusb->mux_state = 0;
713 					}
714 				}
715 
716 			/**
717 			 * if length is valid and we reached the end:
718 			 * goto next muxpack
719 			 */
720 				if ((ttusb->muxpack_ptr >= 2) &&
721 				    (ttusb->muxpack_ptr ==
722 				     ttusb->muxpack_len)) {
723 					ttusb_process_muxpack(ttusb,
724 							      ttusb->
725 							      muxpack,
726 							      ttusb->
727 							      muxpack_ptr);
728 					ttusb->muxpack_ptr = 0;
729 					/* maximum bytes, until we know the length */
730 					ttusb->muxpack_len = 2;
731 
732 				/**
733 				 * no muxpacks left?
734 				 * return to search-sync state
735 				 */
736 					if (!ttusb->mux_npacks--) {
737 						ttusb->mux_state = 0;
738 						break;
739 					}
740 				}
741 				break;
742 			}
743 		default:
744 			BUG();
745 			break;
746 		}
747 	}
748 }
749 
750 static void ttusb_iso_irq(struct urb *urb)
751 {
752 	struct ttusb *ttusb = urb->context;
753 	struct usb_iso_packet_descriptor *d;
754 	u8 *data;
755 	int len, i;
756 
757 	if (!ttusb->iso_streaming)
758 		return;
759 
760 #if 0
761 	printk("%s: status %d, errcount == %d, length == %i\n",
762 	       __func__,
763 	       urb->status, urb->error_count, urb->actual_length);
764 #endif
765 
766 	if (!urb->status) {
767 		for (i = 0; i < urb->number_of_packets; ++i) {
768 			numpkt++;
769 			if (time_after_eq(jiffies, lastj + HZ)) {
770 				dprintk("frames/s: %lu (ts: %d, stuff %d, sec: %d, invalid: %d, all: %d)\n",
771 					numpkt * HZ / (jiffies - lastj),
772 					numts, numstuff, numsec, numinvalid,
773 					numts + numstuff + numsec + numinvalid);
774 				numts = numstuff = numsec = numinvalid = 0;
775 				lastj = jiffies;
776 				numpkt = 0;
777 			}
778 			d = &urb->iso_frame_desc[i];
779 			data = urb->transfer_buffer + d->offset;
780 			len = d->actual_length;
781 			d->actual_length = 0;
782 			d->status = 0;
783 			ttusb_process_frame(ttusb, data, len);
784 		}
785 	}
786 	usb_submit_urb(urb, GFP_ATOMIC);
787 }
788 
789 static void ttusb_free_iso_urbs(struct ttusb *ttusb)
790 {
791 	int i;
792 
793 	for (i = 0; i < ISO_BUF_COUNT; i++)
794 		usb_free_urb(ttusb->iso_urb[i]);
795 
796 	pci_free_consistent(NULL,
797 			    ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF *
798 			    ISO_BUF_COUNT, ttusb->iso_buffer,
799 			    ttusb->iso_dma_handle);
800 }
801 
802 static int ttusb_alloc_iso_urbs(struct ttusb *ttusb)
803 {
804 	int i;
805 
806 	ttusb->iso_buffer = pci_zalloc_consistent(NULL,
807 						  ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF * ISO_BUF_COUNT,
808 						  &ttusb->iso_dma_handle);
809 
810 	if (!ttusb->iso_buffer) {
811 		dprintk("%s: pci_alloc_consistent - not enough memory\n",
812 			__func__);
813 		return -ENOMEM;
814 	}
815 
816 	for (i = 0; i < ISO_BUF_COUNT; i++) {
817 		struct urb *urb;
818 
819 		if (!
820 		    (urb =
821 		     usb_alloc_urb(FRAMES_PER_ISO_BUF, GFP_ATOMIC))) {
822 			ttusb_free_iso_urbs(ttusb);
823 			return -ENOMEM;
824 		}
825 
826 		ttusb->iso_urb[i] = urb;
827 	}
828 
829 	return 0;
830 }
831 
832 static void ttusb_stop_iso_xfer(struct ttusb *ttusb)
833 {
834 	int i;
835 
836 	for (i = 0; i < ISO_BUF_COUNT; i++)
837 		usb_kill_urb(ttusb->iso_urb[i]);
838 
839 	ttusb->iso_streaming = 0;
840 }
841 
842 static int ttusb_start_iso_xfer(struct ttusb *ttusb)
843 {
844 	int i, j, err, buffer_offset = 0;
845 
846 	if (ttusb->iso_streaming) {
847 		printk("%s: iso xfer already running!\n", __func__);
848 		return 0;
849 	}
850 
851 	ttusb->cc = -1;
852 	ttusb->insync = 0;
853 	ttusb->mux_state = 0;
854 
855 	for (i = 0; i < ISO_BUF_COUNT; i++) {
856 		int frame_offset = 0;
857 		struct urb *urb = ttusb->iso_urb[i];
858 
859 		urb->dev = ttusb->dev;
860 		urb->context = ttusb;
861 		urb->complete = ttusb_iso_irq;
862 		urb->pipe = ttusb->isoc_in_pipe;
863 		urb->transfer_flags = URB_ISO_ASAP;
864 		urb->interval = 1;
865 		urb->number_of_packets = FRAMES_PER_ISO_BUF;
866 		urb->transfer_buffer_length =
867 		    ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF;
868 		urb->transfer_buffer = ttusb->iso_buffer + buffer_offset;
869 		buffer_offset += ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF;
870 
871 		for (j = 0; j < FRAMES_PER_ISO_BUF; j++) {
872 			urb->iso_frame_desc[j].offset = frame_offset;
873 			urb->iso_frame_desc[j].length = ISO_FRAME_SIZE;
874 			frame_offset += ISO_FRAME_SIZE;
875 		}
876 	}
877 
878 	for (i = 0; i < ISO_BUF_COUNT; i++) {
879 		if ((err = usb_submit_urb(ttusb->iso_urb[i], GFP_ATOMIC))) {
880 			ttusb_stop_iso_xfer(ttusb);
881 			printk
882 			    ("%s: failed urb submission (%i: err = %i)!\n",
883 			     __func__, i, err);
884 			return err;
885 		}
886 	}
887 
888 	ttusb->iso_streaming = 1;
889 
890 	return 0;
891 }
892 
893 #ifdef TTUSB_HWSECTIONS
894 static void ttusb_handle_ts_data(struct dvb_demux_feed *dvbdmxfeed, const u8 * data,
895 			  int len)
896 {
897 	dvbdmxfeed->cb.ts(data, len, 0, 0, &dvbdmxfeed->feed.ts, 0);
898 }
899 
900 static void ttusb_handle_sec_data(struct dvb_demux_feed *dvbdmxfeed, const u8 * data,
901 			   int len)
902 {
903 //      struct dvb_demux_feed *dvbdmxfeed = channel->dvbdmxfeed;
904 #error TODO: handle ugly stuff
905 //      dvbdmxfeed->cb.sec(data, len, 0, 0, &dvbdmxfeed->feed.sec, 0);
906 }
907 #endif
908 
909 static int ttusb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
910 {
911 	struct ttusb *ttusb = (struct ttusb *) dvbdmxfeed->demux;
912 	int feed_type = 1;
913 
914 	dprintk("ttusb_start_feed\n");
915 
916 	switch (dvbdmxfeed->type) {
917 	case DMX_TYPE_TS:
918 		break;
919 	case DMX_TYPE_SEC:
920 		break;
921 	default:
922 		return -EINVAL;
923 	}
924 
925 	if (dvbdmxfeed->type == DMX_TYPE_TS) {
926 		switch (dvbdmxfeed->pes_type) {
927 		case DMX_PES_VIDEO:
928 		case DMX_PES_AUDIO:
929 		case DMX_PES_TELETEXT:
930 		case DMX_PES_PCR:
931 		case DMX_PES_OTHER:
932 			break;
933 		default:
934 			return -EINVAL;
935 		}
936 	}
937 
938 #ifdef TTUSB_HWSECTIONS
939 #error TODO: allocate filters
940 	if (dvbdmxfeed->type == DMX_TYPE_TS) {
941 		feed_type = 1;
942 	} else if (dvbdmxfeed->type == DMX_TYPE_SEC) {
943 		feed_type = 2;
944 	}
945 #endif
946 
947 	ttusb_set_channel(ttusb, dvbdmxfeed->index, feed_type, dvbdmxfeed->pid);
948 
949 	if (0 == ttusb->running_feed_count++)
950 		ttusb_start_iso_xfer(ttusb);
951 
952 	return 0;
953 }
954 
955 static int ttusb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
956 {
957 	struct ttusb *ttusb = (struct ttusb *) dvbdmxfeed->demux;
958 
959 	ttusb_del_channel(ttusb, dvbdmxfeed->index);
960 
961 	if (--ttusb->running_feed_count == 0)
962 		ttusb_stop_iso_xfer(ttusb);
963 
964 	return 0;
965 }
966 
967 static int ttusb_setup_interfaces(struct ttusb *ttusb)
968 {
969 	usb_set_interface(ttusb->dev, 1, 1);
970 
971 	ttusb->bulk_out_pipe = usb_sndbulkpipe(ttusb->dev, 1);
972 	ttusb->bulk_in_pipe = usb_rcvbulkpipe(ttusb->dev, 1);
973 	ttusb->isoc_in_pipe = usb_rcvisocpipe(ttusb->dev, 2);
974 
975 	return 0;
976 }
977 
978 #if 0
979 static u8 stc_firmware[8192];
980 
981 static int stc_open(struct inode *inode, struct file *file)
982 {
983 	struct ttusb *ttusb = file->private_data;
984 	int addr;
985 
986 	for (addr = 0; addr < 8192; addr += 16) {
987 		u8 snd_buf[2] = { addr >> 8, addr & 0xFF };
988 		ttusb_i2c_msg(ttusb, 0x50, snd_buf, 2, stc_firmware + addr,
989 			      16);
990 	}
991 
992 	return 0;
993 }
994 
995 static ssize_t stc_read(struct file *file, char *buf, size_t count,
996 		 loff_t *offset)
997 {
998 	return simple_read_from_buffer(buf, count, offset, stc_firmware, 8192);
999 }
1000 
1001 static int stc_release(struct inode *inode, struct file *file)
1002 {
1003 	return 0;
1004 }
1005 
1006 static const struct file_operations stc_fops = {
1007 	.owner = THIS_MODULE,
1008 	.read = stc_read,
1009 	.open = stc_open,
1010 	.release = stc_release,
1011 };
1012 #endif
1013 
1014 static u32 functionality(struct i2c_adapter *adapter)
1015 {
1016 	return I2C_FUNC_I2C;
1017 }
1018 
1019 
1020 
1021 static int alps_tdmb7_tuner_set_params(struct dvb_frontend *fe)
1022 {
1023 	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1024 	struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1025 	u8 data[4];
1026 	struct i2c_msg msg = {.addr=0x61, .flags=0, .buf=data, .len=sizeof(data) };
1027 	u32 div;
1028 
1029 	div = (p->frequency + 36166667) / 166667;
1030 
1031 	data[0] = (div >> 8) & 0x7f;
1032 	data[1] = div & 0xff;
1033 	data[2] = ((div >> 10) & 0x60) | 0x85;
1034 	data[3] = p->frequency < 592000000 ? 0x40 : 0x80;
1035 
1036 	if (fe->ops.i2c_gate_ctrl)
1037 		fe->ops.i2c_gate_ctrl(fe, 1);
1038 	if (i2c_transfer(&ttusb->i2c_adap, &msg, 1) != 1) return -EIO;
1039 	return 0;
1040 }
1041 
1042 static struct cx22700_config alps_tdmb7_config = {
1043 	.demod_address = 0x43,
1044 };
1045 
1046 
1047 
1048 
1049 
1050 static int philips_tdm1316l_tuner_init(struct dvb_frontend* fe)
1051 {
1052 	struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1053 	static u8 td1316_init[] = { 0x0b, 0xf5, 0x85, 0xab };
1054 	static u8 disable_mc44BC374c[] = { 0x1d, 0x74, 0xa0, 0x68 };
1055 	struct i2c_msg tuner_msg = { .addr=0x60, .flags=0, .buf=td1316_init, .len=sizeof(td1316_init) };
1056 
1057 	// setup PLL configuration
1058 	if (fe->ops.i2c_gate_ctrl)
1059 		fe->ops.i2c_gate_ctrl(fe, 1);
1060 	if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1) return -EIO;
1061 	msleep(1);
1062 
1063 	// disable the mc44BC374c (do not check for errors)
1064 	tuner_msg.addr = 0x65;
1065 	tuner_msg.buf = disable_mc44BC374c;
1066 	tuner_msg.len = sizeof(disable_mc44BC374c);
1067 	if (fe->ops.i2c_gate_ctrl)
1068 		fe->ops.i2c_gate_ctrl(fe, 1);
1069 	if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1) {
1070 		i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1);
1071 	}
1072 
1073 	return 0;
1074 }
1075 
1076 static int philips_tdm1316l_tuner_set_params(struct dvb_frontend *fe)
1077 {
1078 	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1079 	struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1080 	u8 tuner_buf[4];
1081 	struct i2c_msg tuner_msg = {.addr=0x60, .flags=0, .buf=tuner_buf, .len=sizeof(tuner_buf) };
1082 	int tuner_frequency = 0;
1083 	u8 band, cp, filter;
1084 
1085 	// determine charge pump
1086 	tuner_frequency = p->frequency + 36130000;
1087 	if (tuner_frequency < 87000000) return -EINVAL;
1088 	else if (tuner_frequency < 130000000) cp = 3;
1089 	else if (tuner_frequency < 160000000) cp = 5;
1090 	else if (tuner_frequency < 200000000) cp = 6;
1091 	else if (tuner_frequency < 290000000) cp = 3;
1092 	else if (tuner_frequency < 420000000) cp = 5;
1093 	else if (tuner_frequency < 480000000) cp = 6;
1094 	else if (tuner_frequency < 620000000) cp = 3;
1095 	else if (tuner_frequency < 830000000) cp = 5;
1096 	else if (tuner_frequency < 895000000) cp = 7;
1097 	else return -EINVAL;
1098 
1099 	// determine band
1100 	if (p->frequency < 49000000)
1101 		return -EINVAL;
1102 	else if (p->frequency < 159000000)
1103 		band = 1;
1104 	else if (p->frequency < 444000000)
1105 		band = 2;
1106 	else if (p->frequency < 861000000)
1107 		band = 4;
1108 	else return -EINVAL;
1109 
1110 	// setup PLL filter
1111 	switch (p->bandwidth_hz) {
1112 	case 6000000:
1113 		tda1004x_writereg(fe, 0x0C, 0);
1114 		filter = 0;
1115 		break;
1116 
1117 	case 7000000:
1118 		tda1004x_writereg(fe, 0x0C, 0);
1119 		filter = 0;
1120 		break;
1121 
1122 	case 8000000:
1123 		tda1004x_writereg(fe, 0x0C, 0xFF);
1124 		filter = 1;
1125 		break;
1126 
1127 	default:
1128 		return -EINVAL;
1129 	}
1130 
1131 	// calculate divisor
1132 	// ((36130000+((1000000/6)/2)) + Finput)/(1000000/6)
1133 	tuner_frequency = (((p->frequency / 1000) * 6) + 217280) / 1000;
1134 
1135 	// setup tuner buffer
1136 	tuner_buf[0] = tuner_frequency >> 8;
1137 	tuner_buf[1] = tuner_frequency & 0xff;
1138 	tuner_buf[2] = 0xca;
1139 	tuner_buf[3] = (cp << 5) | (filter << 3) | band;
1140 
1141 	if (fe->ops.i2c_gate_ctrl)
1142 		fe->ops.i2c_gate_ctrl(fe, 1);
1143 	if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1)
1144 		return -EIO;
1145 
1146 	msleep(1);
1147 	return 0;
1148 }
1149 
1150 static int philips_tdm1316l_request_firmware(struct dvb_frontend* fe, const struct firmware **fw, char* name)
1151 {
1152 	struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1153 
1154 	return request_firmware(fw, name, &ttusb->dev->dev);
1155 }
1156 
1157 static struct tda1004x_config philips_tdm1316l_config = {
1158 
1159 	.demod_address = 0x8,
1160 	.invert = 1,
1161 	.invert_oclk = 0,
1162 	.request_firmware = philips_tdm1316l_request_firmware,
1163 };
1164 
1165 static u8 alps_bsbe1_inittab[] = {
1166 	0x01, 0x15,
1167 	0x02, 0x30,
1168 	0x03, 0x00,
1169 	0x04, 0x7d,             /* F22FR = 0x7d, F22 = f_VCO / 128 / 0x7d = 22 kHz */
1170 	0x05, 0x35,             /* I2CT = 0, SCLT = 1, SDAT = 1 */
1171 	0x06, 0x40,             /* DAC not used, set to high impendance mode */
1172 	0x07, 0x00,             /* DAC LSB */
1173 	0x08, 0x40,             /* DiSEqC off, LNB power on OP2/LOCK pin on */
1174 	0x09, 0x00,             /* FIFO */
1175 	0x0c, 0x51,             /* OP1 ctl = Normal, OP1 val = 1 (LNB Power ON) */
1176 	0x0d, 0x82,             /* DC offset compensation = ON, beta_agc1 = 2 */
1177 	0x0e, 0x23,             /* alpha_tmg = 2, beta_tmg = 3 */
1178 	0x10, 0x3f,             // AGC2  0x3d
1179 	0x11, 0x84,
1180 	0x12, 0xb9,
1181 	0x15, 0xc9,             // lock detector threshold
1182 	0x16, 0x00,
1183 	0x17, 0x00,
1184 	0x18, 0x00,
1185 	0x19, 0x00,
1186 	0x1a, 0x00,
1187 	0x1f, 0x50,
1188 	0x20, 0x00,
1189 	0x21, 0x00,
1190 	0x22, 0x00,
1191 	0x23, 0x00,
1192 	0x28, 0x00,             // out imp: normal  out type: parallel FEC mode:0
1193 	0x29, 0x1e,             // 1/2 threshold
1194 	0x2a, 0x14,             // 2/3 threshold
1195 	0x2b, 0x0f,             // 3/4 threshold
1196 	0x2c, 0x09,             // 5/6 threshold
1197 	0x2d, 0x05,             // 7/8 threshold
1198 	0x2e, 0x01,
1199 	0x31, 0x1f,             // test all FECs
1200 	0x32, 0x19,             // viterbi and synchro search
1201 	0x33, 0xfc,             // rs control
1202 	0x34, 0x93,             // error control
1203 	0x0f, 0x92,
1204 	0xff, 0xff
1205 };
1206 
1207 static u8 alps_bsru6_inittab[] = {
1208 	0x01, 0x15,
1209 	0x02, 0x30,
1210 	0x03, 0x00,
1211 	0x04, 0x7d,		/* F22FR = 0x7d, F22 = f_VCO / 128 / 0x7d = 22 kHz */
1212 	0x05, 0x35,		/* I2CT = 0, SCLT = 1, SDAT = 1 */
1213 	0x06, 0x40,		/* DAC not used, set to high impendance mode */
1214 	0x07, 0x00,		/* DAC LSB */
1215 	0x08, 0x40,		/* DiSEqC off, LNB power on OP2/LOCK pin on */
1216 	0x09, 0x00,		/* FIFO */
1217 	0x0c, 0x51,		/* OP1 ctl = Normal, OP1 val = 1 (LNB Power ON) */
1218 	0x0d, 0x82,		/* DC offset compensation = ON, beta_agc1 = 2 */
1219 	0x0e, 0x23,		/* alpha_tmg = 2, beta_tmg = 3 */
1220 	0x10, 0x3f,		// AGC2  0x3d
1221 	0x11, 0x84,
1222 	0x12, 0xb9,
1223 	0x15, 0xc9,		// lock detector threshold
1224 	0x16, 0x00,
1225 	0x17, 0x00,
1226 	0x18, 0x00,
1227 	0x19, 0x00,
1228 	0x1a, 0x00,
1229 	0x1f, 0x50,
1230 	0x20, 0x00,
1231 	0x21, 0x00,
1232 	0x22, 0x00,
1233 	0x23, 0x00,
1234 	0x28, 0x00,		// out imp: normal  out type: parallel FEC mode:0
1235 	0x29, 0x1e,		// 1/2 threshold
1236 	0x2a, 0x14,		// 2/3 threshold
1237 	0x2b, 0x0f,		// 3/4 threshold
1238 	0x2c, 0x09,		// 5/6 threshold
1239 	0x2d, 0x05,		// 7/8 threshold
1240 	0x2e, 0x01,
1241 	0x31, 0x1f,		// test all FECs
1242 	0x32, 0x19,		// viterbi and synchro search
1243 	0x33, 0xfc,		// rs control
1244 	0x34, 0x93,		// error control
1245 	0x0f, 0x52,
1246 	0xff, 0xff
1247 };
1248 
1249 static int alps_stv0299_set_symbol_rate(struct dvb_frontend *fe, u32 srate, u32 ratio)
1250 {
1251 	u8 aclk = 0;
1252 	u8 bclk = 0;
1253 
1254 	if (srate < 1500000) {
1255 		aclk = 0xb7;
1256 		bclk = 0x47;
1257 	} else if (srate < 3000000) {
1258 		aclk = 0xb7;
1259 		bclk = 0x4b;
1260 	} else if (srate < 7000000) {
1261 		aclk = 0xb7;
1262 		bclk = 0x4f;
1263 	} else if (srate < 14000000) {
1264 		aclk = 0xb7;
1265 		bclk = 0x53;
1266 	} else if (srate < 30000000) {
1267 		aclk = 0xb6;
1268 		bclk = 0x53;
1269 	} else if (srate < 45000000) {
1270 		aclk = 0xb4;
1271 		bclk = 0x51;
1272 	}
1273 
1274 	stv0299_writereg(fe, 0x13, aclk);
1275 	stv0299_writereg(fe, 0x14, bclk);
1276 	stv0299_writereg(fe, 0x1f, (ratio >> 16) & 0xff);
1277 	stv0299_writereg(fe, 0x20, (ratio >> 8) & 0xff);
1278 	stv0299_writereg(fe, 0x21, (ratio) & 0xf0);
1279 
1280 	return 0;
1281 }
1282 
1283 static int philips_tsa5059_tuner_set_params(struct dvb_frontend *fe)
1284 {
1285 	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1286 	struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1287 	u8 buf[4];
1288 	u32 div;
1289 	struct i2c_msg msg = {.addr = 0x61,.flags = 0,.buf = buf,.len = sizeof(buf) };
1290 
1291 	if ((p->frequency < 950000) || (p->frequency > 2150000))
1292 		return -EINVAL;
1293 
1294 	div = (p->frequency + (125 - 1)) / 125;	/* round correctly */
1295 	buf[0] = (div >> 8) & 0x7f;
1296 	buf[1] = div & 0xff;
1297 	buf[2] = 0x80 | ((div & 0x18000) >> 10) | 4;
1298 	buf[3] = 0xC4;
1299 
1300 	if (p->frequency > 1530000)
1301 		buf[3] = 0xC0;
1302 
1303 	/* BSBE1 wants XCE bit set */
1304 	if (ttusb->revision == TTUSB_REV_2_2)
1305 		buf[3] |= 0x20;
1306 
1307 	if (fe->ops.i2c_gate_ctrl)
1308 		fe->ops.i2c_gate_ctrl(fe, 1);
1309 	if (i2c_transfer(&ttusb->i2c_adap, &msg, 1) != 1)
1310 		return -EIO;
1311 
1312 	return 0;
1313 }
1314 
1315 static struct stv0299_config alps_stv0299_config = {
1316 	.demod_address = 0x68,
1317 	.inittab = alps_bsru6_inittab,
1318 	.mclk = 88000000UL,
1319 	.invert = 1,
1320 	.skip_reinit = 0,
1321 	.lock_output = STV0299_LOCKOUTPUT_1,
1322 	.volt13_op0_op1 = STV0299_VOLT13_OP1,
1323 	.min_delay_ms = 100,
1324 	.set_symbol_rate = alps_stv0299_set_symbol_rate,
1325 };
1326 
1327 static int ttusb_novas_grundig_29504_491_tuner_set_params(struct dvb_frontend *fe)
1328 {
1329 	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1330 	struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1331 	u8 buf[4];
1332 	u32 div;
1333 	struct i2c_msg msg = {.addr = 0x61,.flags = 0,.buf = buf,.len = sizeof(buf) };
1334 
1335 	div = p->frequency / 125;
1336 
1337 	buf[0] = (div >> 8) & 0x7f;
1338 	buf[1] = div & 0xff;
1339 	buf[2] = 0x8e;
1340 	buf[3] = 0x00;
1341 
1342 	if (fe->ops.i2c_gate_ctrl)
1343 		fe->ops.i2c_gate_ctrl(fe, 1);
1344 	if (i2c_transfer(&ttusb->i2c_adap, &msg, 1) != 1)
1345 		return -EIO;
1346 
1347 	return 0;
1348 }
1349 
1350 static struct tda8083_config ttusb_novas_grundig_29504_491_config = {
1351 
1352 	.demod_address = 0x68,
1353 };
1354 
1355 static int alps_tdbe2_tuner_set_params(struct dvb_frontend *fe)
1356 {
1357 	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1358 	struct ttusb* ttusb = fe->dvb->priv;
1359 	u32 div;
1360 	u8 data[4];
1361 	struct i2c_msg msg = { .addr = 0x62, .flags = 0, .buf = data, .len = sizeof(data) };
1362 
1363 	div = (p->frequency + 35937500 + 31250) / 62500;
1364 
1365 	data[0] = (div >> 8) & 0x7f;
1366 	data[1] = div & 0xff;
1367 	data[2] = 0x85 | ((div >> 10) & 0x60);
1368 	data[3] = (p->frequency < 174000000 ? 0x88 : p->frequency < 470000000 ? 0x84 : 0x81);
1369 
1370 	if (fe->ops.i2c_gate_ctrl)
1371 		fe->ops.i2c_gate_ctrl(fe, 1);
1372 	if (i2c_transfer (&ttusb->i2c_adap, &msg, 1) != 1)
1373 		return -EIO;
1374 
1375 	return 0;
1376 }
1377 
1378 
1379 static struct ves1820_config alps_tdbe2_config = {
1380 	.demod_address = 0x09,
1381 	.xin = 57840000UL,
1382 	.invert = 1,
1383 	.selagc = VES1820_SELAGC_SIGNAMPERR,
1384 };
1385 
1386 static u8 read_pwm(struct ttusb* ttusb)
1387 {
1388 	u8 b = 0xff;
1389 	u8 pwm;
1390 	struct i2c_msg msg[] = { { .addr = 0x50,.flags = 0,.buf = &b,.len = 1 },
1391 				{ .addr = 0x50,.flags = I2C_M_RD,.buf = &pwm,.len = 1} };
1392 
1393 	if ((i2c_transfer(&ttusb->i2c_adap, msg, 2) != 2) || (pwm == 0xff))
1394 		pwm = 0x48;
1395 
1396 	return pwm;
1397 }
1398 
1399 
1400 static int dvbc_philips_tdm1316l_tuner_set_params(struct dvb_frontend *fe)
1401 {
1402 	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1403 	struct ttusb *ttusb = (struct ttusb *) fe->dvb->priv;
1404 	u8 tuner_buf[5];
1405 	struct i2c_msg tuner_msg = {.addr = 0x60,
1406 				    .flags = 0,
1407 				    .buf = tuner_buf,
1408 				    .len = sizeof(tuner_buf) };
1409 	int tuner_frequency = 0;
1410 	u8 band, cp, filter;
1411 
1412 	// determine charge pump
1413 	tuner_frequency = p->frequency;
1414 	if      (tuner_frequency <  87000000) {return -EINVAL;}
1415 	else if (tuner_frequency < 130000000) {cp = 3; band = 1;}
1416 	else if (tuner_frequency < 160000000) {cp = 5; band = 1;}
1417 	else if (tuner_frequency < 200000000) {cp = 6; band = 1;}
1418 	else if (tuner_frequency < 290000000) {cp = 3; band = 2;}
1419 	else if (tuner_frequency < 420000000) {cp = 5; band = 2;}
1420 	else if (tuner_frequency < 480000000) {cp = 6; band = 2;}
1421 	else if (tuner_frequency < 620000000) {cp = 3; band = 4;}
1422 	else if (tuner_frequency < 830000000) {cp = 5; band = 4;}
1423 	else if (tuner_frequency < 895000000) {cp = 7; band = 4;}
1424 	else {return -EINVAL;}
1425 
1426 	// assume PLL filter should always be 8MHz for the moment.
1427 	filter = 1;
1428 
1429 	// calculate divisor
1430 	// (Finput + Fif)/Fref; Fif = 36125000 Hz, Fref = 62500 Hz
1431 	tuner_frequency = ((p->frequency + 36125000) / 62500);
1432 
1433 	// setup tuner buffer
1434 	tuner_buf[0] = tuner_frequency >> 8;
1435 	tuner_buf[1] = tuner_frequency & 0xff;
1436 	tuner_buf[2] = 0xc8;
1437 	tuner_buf[3] = (cp << 5) | (filter << 3) | band;
1438 	tuner_buf[4] = 0x80;
1439 
1440 	if (fe->ops.i2c_gate_ctrl)
1441 		fe->ops.i2c_gate_ctrl(fe, 1);
1442 	if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1) {
1443 		printk("dvb-ttusb-budget: dvbc_philips_tdm1316l_pll_set Error 1\n");
1444 		return -EIO;
1445 	}
1446 
1447 	msleep(50);
1448 
1449 	if (fe->ops.i2c_gate_ctrl)
1450 		fe->ops.i2c_gate_ctrl(fe, 1);
1451 	if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1) {
1452 		printk("dvb-ttusb-budget: dvbc_philips_tdm1316l_pll_set Error 2\n");
1453 		return -EIO;
1454 	}
1455 
1456 	msleep(1);
1457 
1458 	return 0;
1459 }
1460 
1461 static u8 dvbc_philips_tdm1316l_inittab[] = {
1462 	0x80, 0x21,
1463 	0x80, 0x20,
1464 	0x81, 0x01,
1465 	0x81, 0x00,
1466 	0x00, 0x09,
1467 	0x01, 0x69,
1468 	0x03, 0x00,
1469 	0x04, 0x00,
1470 	0x07, 0x00,
1471 	0x08, 0x00,
1472 	0x20, 0x00,
1473 	0x21, 0x40,
1474 	0x22, 0x00,
1475 	0x23, 0x00,
1476 	0x24, 0x40,
1477 	0x25, 0x88,
1478 	0x30, 0xff,
1479 	0x31, 0x00,
1480 	0x32, 0xff,
1481 	0x33, 0x00,
1482 	0x34, 0x50,
1483 	0x35, 0x7f,
1484 	0x36, 0x00,
1485 	0x37, 0x20,
1486 	0x38, 0x00,
1487 	0x40, 0x1c,
1488 	0x41, 0xff,
1489 	0x42, 0x29,
1490 	0x43, 0x20,
1491 	0x44, 0xff,
1492 	0x45, 0x00,
1493 	0x46, 0x00,
1494 	0x49, 0x04,
1495 	0x4a, 0xff,
1496 	0x4b, 0x7f,
1497 	0x52, 0x30,
1498 	0x55, 0xae,
1499 	0x56, 0x47,
1500 	0x57, 0xe1,
1501 	0x58, 0x3a,
1502 	0x5a, 0x1e,
1503 	0x5b, 0x34,
1504 	0x60, 0x00,
1505 	0x63, 0x00,
1506 	0x64, 0x00,
1507 	0x65, 0x00,
1508 	0x66, 0x00,
1509 	0x67, 0x00,
1510 	0x68, 0x00,
1511 	0x69, 0x00,
1512 	0x6a, 0x02,
1513 	0x6b, 0x00,
1514 	0x70, 0xff,
1515 	0x71, 0x00,
1516 	0x72, 0x00,
1517 	0x73, 0x00,
1518 	0x74, 0x0c,
1519 	0x80, 0x00,
1520 	0x81, 0x00,
1521 	0x82, 0x00,
1522 	0x83, 0x00,
1523 	0x84, 0x04,
1524 	0x85, 0x80,
1525 	0x86, 0x24,
1526 	0x87, 0x78,
1527 	0x88, 0x00,
1528 	0x89, 0x00,
1529 	0x90, 0x01,
1530 	0x91, 0x01,
1531 	0xa0, 0x00,
1532 	0xa1, 0x00,
1533 	0xa2, 0x00,
1534 	0xb0, 0x91,
1535 	0xb1, 0x0b,
1536 	0xc0, 0x4b,
1537 	0xc1, 0x00,
1538 	0xc2, 0x00,
1539 	0xd0, 0x00,
1540 	0xd1, 0x00,
1541 	0xd2, 0x00,
1542 	0xd3, 0x00,
1543 	0xd4, 0x00,
1544 	0xd5, 0x00,
1545 	0xde, 0x00,
1546 	0xdf, 0x00,
1547 	0x61, 0x38,
1548 	0x62, 0x0a,
1549 	0x53, 0x13,
1550 	0x59, 0x08,
1551 	0x55, 0x00,
1552 	0x56, 0x40,
1553 	0x57, 0x08,
1554 	0x58, 0x3d,
1555 	0x88, 0x10,
1556 	0xa0, 0x00,
1557 	0xa0, 0x00,
1558 	0xa0, 0x00,
1559 	0xa0, 0x04,
1560 	0xff, 0xff,
1561 };
1562 
1563 static struct stv0297_config dvbc_philips_tdm1316l_config = {
1564 	.demod_address = 0x1c,
1565 	.inittab = dvbc_philips_tdm1316l_inittab,
1566 	.invert = 0,
1567 };
1568 
1569 static void frontend_init(struct ttusb* ttusb)
1570 {
1571 	switch(le16_to_cpu(ttusb->dev->descriptor.idProduct)) {
1572 	case 0x1003: // Hauppauge/TT Nova-USB-S budget (stv0299/ALPS BSRU6|BSBE1(tsa5059))
1573 		// try the stv0299 based first
1574 		ttusb->fe = dvb_attach(stv0299_attach, &alps_stv0299_config, &ttusb->i2c_adap);
1575 		if (ttusb->fe != NULL) {
1576 			ttusb->fe->ops.tuner_ops.set_params = philips_tsa5059_tuner_set_params;
1577 
1578 			if(ttusb->revision == TTUSB_REV_2_2) { // ALPS BSBE1
1579 				alps_stv0299_config.inittab = alps_bsbe1_inittab;
1580 				dvb_attach(lnbp21_attach, ttusb->fe, &ttusb->i2c_adap, 0, 0);
1581 			} else { // ALPS BSRU6
1582 				ttusb->fe->ops.set_voltage = ttusb_set_voltage;
1583 			}
1584 			break;
1585 		}
1586 
1587 		// Grundig 29504-491
1588 		ttusb->fe = dvb_attach(tda8083_attach, &ttusb_novas_grundig_29504_491_config, &ttusb->i2c_adap);
1589 		if (ttusb->fe != NULL) {
1590 			ttusb->fe->ops.tuner_ops.set_params = ttusb_novas_grundig_29504_491_tuner_set_params;
1591 			ttusb->fe->ops.set_voltage = ttusb_set_voltage;
1592 			break;
1593 		}
1594 		break;
1595 
1596 	case 0x1004: // Hauppauge/TT DVB-C budget (ves1820/ALPS TDBE2(sp5659))
1597 		ttusb->fe = dvb_attach(ves1820_attach, &alps_tdbe2_config, &ttusb->i2c_adap, read_pwm(ttusb));
1598 		if (ttusb->fe != NULL) {
1599 			ttusb->fe->ops.tuner_ops.set_params = alps_tdbe2_tuner_set_params;
1600 			break;
1601 		}
1602 
1603 		ttusb->fe = dvb_attach(stv0297_attach, &dvbc_philips_tdm1316l_config, &ttusb->i2c_adap);
1604 		if (ttusb->fe != NULL) {
1605 			ttusb->fe->ops.tuner_ops.set_params = dvbc_philips_tdm1316l_tuner_set_params;
1606 			break;
1607 		}
1608 		break;
1609 
1610 	case 0x1005: // Hauppauge/TT Nova-USB-t budget (tda10046/Philips td1316(tda6651tt) OR cx22700/ALPS TDMB7(??))
1611 		// try the ALPS TDMB7 first
1612 		ttusb->fe = dvb_attach(cx22700_attach, &alps_tdmb7_config, &ttusb->i2c_adap);
1613 		if (ttusb->fe != NULL) {
1614 			ttusb->fe->ops.tuner_ops.set_params = alps_tdmb7_tuner_set_params;
1615 			break;
1616 		}
1617 
1618 		// Philips td1316
1619 		ttusb->fe = dvb_attach(tda10046_attach, &philips_tdm1316l_config, &ttusb->i2c_adap);
1620 		if (ttusb->fe != NULL) {
1621 			ttusb->fe->ops.tuner_ops.init = philips_tdm1316l_tuner_init;
1622 			ttusb->fe->ops.tuner_ops.set_params = philips_tdm1316l_tuner_set_params;
1623 			break;
1624 		}
1625 		break;
1626 	}
1627 
1628 	if (ttusb->fe == NULL) {
1629 		printk("dvb-ttusb-budget: A frontend driver was not found for device [%04x:%04x]\n",
1630 		       le16_to_cpu(ttusb->dev->descriptor.idVendor),
1631 		       le16_to_cpu(ttusb->dev->descriptor.idProduct));
1632 	} else {
1633 		if (dvb_register_frontend(&ttusb->adapter, ttusb->fe)) {
1634 			printk("dvb-ttusb-budget: Frontend registration failed!\n");
1635 			dvb_frontend_detach(ttusb->fe);
1636 			ttusb->fe = NULL;
1637 		}
1638 	}
1639 }
1640 
1641 
1642 
1643 static const struct i2c_algorithm ttusb_dec_algo = {
1644 	.master_xfer	= master_xfer,
1645 	.functionality	= functionality,
1646 };
1647 
1648 static int ttusb_probe(struct usb_interface *intf, const struct usb_device_id *id)
1649 {
1650 	struct usb_device *udev;
1651 	struct ttusb *ttusb;
1652 	int result;
1653 
1654 	dprintk("%s: TTUSB DVB connected\n", __func__);
1655 
1656 	udev = interface_to_usbdev(intf);
1657 
1658 	if (intf->altsetting->desc.bInterfaceNumber != 1) return -ENODEV;
1659 
1660 	if (!(ttusb = kzalloc(sizeof(struct ttusb), GFP_KERNEL)))
1661 		return -ENOMEM;
1662 
1663 	ttusb->dev = udev;
1664 	ttusb->c = 0;
1665 	ttusb->mux_state = 0;
1666 	mutex_init(&ttusb->semi2c);
1667 
1668 	mutex_lock(&ttusb->semi2c);
1669 
1670 	mutex_init(&ttusb->semusb);
1671 
1672 	ttusb_setup_interfaces(ttusb);
1673 
1674 	result = ttusb_alloc_iso_urbs(ttusb);
1675 	if (result < 0) {
1676 		dprintk("%s: ttusb_alloc_iso_urbs - failed\n", __func__);
1677 		mutex_unlock(&ttusb->semi2c);
1678 		kfree(ttusb);
1679 		return result;
1680 	}
1681 
1682 	if (ttusb_init_controller(ttusb))
1683 		printk("ttusb_init_controller: error\n");
1684 
1685 	mutex_unlock(&ttusb->semi2c);
1686 
1687 	result = dvb_register_adapter(&ttusb->adapter,
1688 				      "Technotrend/Hauppauge Nova-USB",
1689 				      THIS_MODULE, &udev->dev, adapter_nr);
1690 	if (result < 0) {
1691 		ttusb_free_iso_urbs(ttusb);
1692 		kfree(ttusb);
1693 		return result;
1694 	}
1695 	ttusb->adapter.priv = ttusb;
1696 
1697 	/* i2c */
1698 	memset(&ttusb->i2c_adap, 0, sizeof(struct i2c_adapter));
1699 	strcpy(ttusb->i2c_adap.name, "TTUSB DEC");
1700 
1701 	i2c_set_adapdata(&ttusb->i2c_adap, ttusb);
1702 
1703 	ttusb->i2c_adap.algo              = &ttusb_dec_algo;
1704 	ttusb->i2c_adap.algo_data         = NULL;
1705 	ttusb->i2c_adap.dev.parent	  = &udev->dev;
1706 
1707 	result = i2c_add_adapter(&ttusb->i2c_adap);
1708 	if (result)
1709 		goto err_unregister_adapter;
1710 
1711 	memset(&ttusb->dvb_demux, 0, sizeof(ttusb->dvb_demux));
1712 
1713 	ttusb->dvb_demux.dmx.capabilities =
1714 	    DMX_TS_FILTERING | DMX_SECTION_FILTERING;
1715 	ttusb->dvb_demux.priv = NULL;
1716 #ifdef TTUSB_HWSECTIONS
1717 	ttusb->dvb_demux.filternum = TTUSB_MAXFILTER;
1718 #else
1719 	ttusb->dvb_demux.filternum = 32;
1720 #endif
1721 	ttusb->dvb_demux.feednum = TTUSB_MAXCHANNEL;
1722 	ttusb->dvb_demux.start_feed = ttusb_start_feed;
1723 	ttusb->dvb_demux.stop_feed = ttusb_stop_feed;
1724 	ttusb->dvb_demux.write_to_decoder = NULL;
1725 
1726 	result = dvb_dmx_init(&ttusb->dvb_demux);
1727 	if (result < 0) {
1728 		printk("ttusb_dvb: dvb_dmx_init failed (errno = %d)\n", result);
1729 		result = -ENODEV;
1730 		goto err_i2c_del_adapter;
1731 	}
1732 //FIXME dmxdev (nur WAS?)
1733 	ttusb->dmxdev.filternum = ttusb->dvb_demux.filternum;
1734 	ttusb->dmxdev.demux = &ttusb->dvb_demux.dmx;
1735 	ttusb->dmxdev.capabilities = 0;
1736 
1737 	result = dvb_dmxdev_init(&ttusb->dmxdev, &ttusb->adapter);
1738 	if (result < 0) {
1739 		printk("ttusb_dvb: dvb_dmxdev_init failed (errno = %d)\n",
1740 		       result);
1741 		result = -ENODEV;
1742 		goto err_release_dmx;
1743 	}
1744 
1745 	if (dvb_net_init(&ttusb->adapter, &ttusb->dvbnet, &ttusb->dvb_demux.dmx)) {
1746 		printk("ttusb_dvb: dvb_net_init failed!\n");
1747 		result = -ENODEV;
1748 		goto err_release_dmxdev;
1749 	}
1750 
1751 	usb_set_intfdata(intf, (void *) ttusb);
1752 
1753 	frontend_init(ttusb);
1754 
1755 	return 0;
1756 
1757 err_release_dmxdev:
1758 	dvb_dmxdev_release(&ttusb->dmxdev);
1759 err_release_dmx:
1760 	dvb_dmx_release(&ttusb->dvb_demux);
1761 err_i2c_del_adapter:
1762 	i2c_del_adapter(&ttusb->i2c_adap);
1763 err_unregister_adapter:
1764 	dvb_unregister_adapter (&ttusb->adapter);
1765 	ttusb_free_iso_urbs(ttusb);
1766 	kfree(ttusb);
1767 	return result;
1768 }
1769 
1770 static void ttusb_disconnect(struct usb_interface *intf)
1771 {
1772 	struct ttusb *ttusb = usb_get_intfdata(intf);
1773 
1774 	usb_set_intfdata(intf, NULL);
1775 
1776 	ttusb->disconnecting = 1;
1777 
1778 	ttusb_stop_iso_xfer(ttusb);
1779 
1780 	ttusb->dvb_demux.dmx.close(&ttusb->dvb_demux.dmx);
1781 	dvb_net_release(&ttusb->dvbnet);
1782 	dvb_dmxdev_release(&ttusb->dmxdev);
1783 	dvb_dmx_release(&ttusb->dvb_demux);
1784 	if (ttusb->fe != NULL) {
1785 		dvb_unregister_frontend(ttusb->fe);
1786 		dvb_frontend_detach(ttusb->fe);
1787 	}
1788 	i2c_del_adapter(&ttusb->i2c_adap);
1789 	dvb_unregister_adapter(&ttusb->adapter);
1790 
1791 	ttusb_free_iso_urbs(ttusb);
1792 
1793 	kfree(ttusb);
1794 
1795 	dprintk("%s: TTUSB DVB disconnected\n", __func__);
1796 }
1797 
1798 static const struct usb_device_id ttusb_table[] = {
1799 	{USB_DEVICE(0xb48, 0x1003)},
1800 	{USB_DEVICE(0xb48, 0x1004)},
1801 	{USB_DEVICE(0xb48, 0x1005)},
1802 	{}
1803 };
1804 
1805 MODULE_DEVICE_TABLE(usb, ttusb_table);
1806 
1807 static struct usb_driver ttusb_driver = {
1808       .name		= "ttusb",
1809       .probe		= ttusb_probe,
1810       .disconnect	= ttusb_disconnect,
1811       .id_table		= ttusb_table,
1812 };
1813 
1814 module_usb_driver(ttusb_driver);
1815 
1816 MODULE_AUTHOR("Holger Waechtler <holger@convergence.de>");
1817 MODULE_DESCRIPTION("TTUSB DVB Driver");
1818 MODULE_LICENSE("GPL");
1819 MODULE_FIRMWARE("ttusb-budget/dspbootcode.bin");
1820