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