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