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