1/* DVB USB compliant linux driver for Conexant USB reference design.
2 *
3 * The Conexant reference design I saw on their website was only for analogue
4 * capturing (using the cx25842). The box I took to write this driver (reverse
5 * engineered) is the one labeled Medion MD95700. In addition to the cx25842
6 * for analogue capturing it also has a cx22702 DVB-T demodulator on the main
7 * board. Besides it has a atiremote (X10) and a USB2.0 hub onboard.
8 *
9 * Maybe it is a little bit premature to call this driver cxusb, but I assume
10 * the USB protocol is identical or at least inherited from the reference
11 * design, so it can be reused for the "analogue-only" device (if it will
12 * appear at all).
13 *
14 * TODO: Use the cx25840-driver for the analogue part
15 *
16 * Copyright (C) 2005 Patrick Boettcher (patrick.boettcher@desy.de)
17 * Copyright (C) 2006 Michael Krufky (mkrufky@linuxtv.org)
18 * Copyright (C) 2006, 2007 Chris Pascoe (c.pascoe@itee.uq.edu.au)
19 *
20 *   This program is free software; you can redistribute it and/or modify it
21 *   under the terms of the GNU General Public License as published by the Free
22 *   Software Foundation, version 2.
23 *
24 * see Documentation/dvb/README.dvb-usb for more information
25 */
26#include <media/tuner.h>
27#include <linux/vmalloc.h>
28#include <linux/slab.h>
29
30#include "cxusb.h"
31
32#include "cx22702.h"
33#include "lgdt330x.h"
34#include "mt352.h"
35#include "mt352_priv.h"
36#include "zl10353.h"
37#include "tuner-xc2028.h"
38#include "tuner-simple.h"
39#include "mxl5005s.h"
40#include "max2165.h"
41#include "dib7000p.h"
42#include "dib0070.h"
43#include "lgs8gxx.h"
44#include "atbm8830.h"
45#include "si2168.h"
46#include "si2157.h"
47#include "sp2.h"
48
49/* Max transfer size done by I2C transfer functions */
50#define MAX_XFER_SIZE  80
51
52/* debug */
53static int dvb_usb_cxusb_debug;
54module_param_named(debug, dvb_usb_cxusb_debug, int, 0644);
55MODULE_PARM_DESC(debug, "set debugging level (1=rc (or-able))." DVB_USB_DEBUG_STATUS);
56
57DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
58
59#define deb_info(args...)   dprintk(dvb_usb_cxusb_debug, 0x03, args)
60#define deb_i2c(args...)    dprintk(dvb_usb_cxusb_debug, 0x02, args)
61
62static int cxusb_ctrl_msg(struct dvb_usb_device *d,
63			  u8 cmd, u8 *wbuf, int wlen, u8 *rbuf, int rlen)
64{
65	int wo = (rbuf == NULL || rlen == 0); /* write-only */
66	u8 sndbuf[MAX_XFER_SIZE];
67
68	if (1 + wlen > sizeof(sndbuf)) {
69		warn("i2c wr: len=%d is too big!\n",
70		     wlen);
71		return -EOPNOTSUPP;
72	}
73
74	memset(sndbuf, 0, 1+wlen);
75
76	sndbuf[0] = cmd;
77	memcpy(&sndbuf[1], wbuf, wlen);
78	if (wo)
79		return dvb_usb_generic_write(d, sndbuf, 1+wlen);
80	else
81		return dvb_usb_generic_rw(d, sndbuf, 1+wlen, rbuf, rlen, 0);
82}
83
84/* GPIO */
85static void cxusb_gpio_tuner(struct dvb_usb_device *d, int onoff)
86{
87	struct cxusb_state *st = d->priv;
88	u8 o[2], i;
89
90	if (st->gpio_write_state[GPIO_TUNER] == onoff)
91		return;
92
93	o[0] = GPIO_TUNER;
94	o[1] = onoff;
95	cxusb_ctrl_msg(d, CMD_GPIO_WRITE, o, 2, &i, 1);
96
97	if (i != 0x01)
98		deb_info("gpio_write failed.\n");
99
100	st->gpio_write_state[GPIO_TUNER] = onoff;
101}
102
103static int cxusb_bluebird_gpio_rw(struct dvb_usb_device *d, u8 changemask,
104				 u8 newval)
105{
106	u8 o[2], gpio_state;
107	int rc;
108
109	o[0] = 0xff & ~changemask;	/* mask of bits to keep */
110	o[1] = newval & changemask;	/* new values for bits  */
111
112	rc = cxusb_ctrl_msg(d, CMD_BLUEBIRD_GPIO_RW, o, 2, &gpio_state, 1);
113	if (rc < 0 || (gpio_state & changemask) != (newval & changemask))
114		deb_info("bluebird_gpio_write failed.\n");
115
116	return rc < 0 ? rc : gpio_state;
117}
118
119static void cxusb_bluebird_gpio_pulse(struct dvb_usb_device *d, u8 pin, int low)
120{
121	cxusb_bluebird_gpio_rw(d, pin, low ? 0 : pin);
122	msleep(5);
123	cxusb_bluebird_gpio_rw(d, pin, low ? pin : 0);
124}
125
126static void cxusb_nano2_led(struct dvb_usb_device *d, int onoff)
127{
128	cxusb_bluebird_gpio_rw(d, 0x40, onoff ? 0 : 0x40);
129}
130
131static int cxusb_d680_dmb_gpio_tuner(struct dvb_usb_device *d,
132		u8 addr, int onoff)
133{
134	u8  o[2] = {addr, onoff};
135	u8  i;
136	int rc;
137
138	rc = cxusb_ctrl_msg(d, CMD_GPIO_WRITE, o, 2, &i, 1);
139
140	if (rc < 0)
141		return rc;
142	if (i == 0x01)
143		return 0;
144	else {
145		deb_info("gpio_write failed.\n");
146		return -EIO;
147	}
148}
149
150static int cxusb_tt_ct2_4400_gpio_tuner(struct dvb_usb_device *d, int onoff)
151{
152	u8 o[2], i;
153	int rc;
154
155	o[0] = 0x83;
156	o[1] = onoff;
157	rc = cxusb_ctrl_msg(d, CMD_GPIO_WRITE, o, 2, &i, 1);
158
159	if (rc) {
160		deb_info("gpio_write failed.\n");
161		return -EIO;
162	}
163	return 0;
164}
165
166/* I2C */
167static int cxusb_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
168			  int num)
169{
170	struct dvb_usb_device *d = i2c_get_adapdata(adap);
171	int ret;
172	int i;
173
174	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
175		return -EAGAIN;
176
177	for (i = 0; i < num; i++) {
178
179		if (le16_to_cpu(d->udev->descriptor.idVendor) == USB_VID_MEDION)
180			switch (msg[i].addr) {
181			case 0x63:
182				cxusb_gpio_tuner(d, 0);
183				break;
184			default:
185				cxusb_gpio_tuner(d, 1);
186				break;
187			}
188
189		if (msg[i].flags & I2C_M_RD) {
190			/* read only */
191			u8 obuf[3], ibuf[MAX_XFER_SIZE];
192
193			if (1 + msg[i].len > sizeof(ibuf)) {
194				warn("i2c rd: len=%d is too big!\n",
195				     msg[i].len);
196				ret = -EOPNOTSUPP;
197				goto unlock;
198			}
199			obuf[0] = 0;
200			obuf[1] = msg[i].len;
201			obuf[2] = msg[i].addr;
202			if (cxusb_ctrl_msg(d, CMD_I2C_READ,
203					   obuf, 3,
204					   ibuf, 1+msg[i].len) < 0) {
205				warn("i2c read failed");
206				break;
207			}
208			memcpy(msg[i].buf, &ibuf[1], msg[i].len);
209		} else if (i+1 < num && (msg[i+1].flags & I2C_M_RD) &&
210			   msg[i].addr == msg[i+1].addr) {
211			/* write to then read from same address */
212			u8 obuf[MAX_XFER_SIZE], ibuf[MAX_XFER_SIZE];
213
214			if (3 + msg[i].len > sizeof(obuf)) {
215				warn("i2c wr: len=%d is too big!\n",
216				     msg[i].len);
217				ret = -EOPNOTSUPP;
218				goto unlock;
219			}
220			if (1 + msg[i + 1].len > sizeof(ibuf)) {
221				warn("i2c rd: len=%d is too big!\n",
222				     msg[i + 1].len);
223				ret = -EOPNOTSUPP;
224				goto unlock;
225			}
226			obuf[0] = msg[i].len;
227			obuf[1] = msg[i+1].len;
228			obuf[2] = msg[i].addr;
229			memcpy(&obuf[3], msg[i].buf, msg[i].len);
230
231			if (cxusb_ctrl_msg(d, CMD_I2C_READ,
232					   obuf, 3+msg[i].len,
233					   ibuf, 1+msg[i+1].len) < 0)
234				break;
235
236			if (ibuf[0] != 0x08)
237				deb_i2c("i2c read may have failed\n");
238
239			memcpy(msg[i+1].buf, &ibuf[1], msg[i+1].len);
240
241			i++;
242		} else {
243			/* write only */
244			u8 obuf[MAX_XFER_SIZE], ibuf;
245
246			if (2 + msg[i].len > sizeof(obuf)) {
247				warn("i2c wr: len=%d is too big!\n",
248				     msg[i].len);
249				ret = -EOPNOTSUPP;
250				goto unlock;
251			}
252			obuf[0] = msg[i].addr;
253			obuf[1] = msg[i].len;
254			memcpy(&obuf[2], msg[i].buf, msg[i].len);
255
256			if (cxusb_ctrl_msg(d, CMD_I2C_WRITE, obuf,
257					   2+msg[i].len, &ibuf,1) < 0)
258				break;
259			if (ibuf != 0x08)
260				deb_i2c("i2c write may have failed\n");
261		}
262	}
263
264	if (i == num)
265		ret = num;
266	else
267		ret = -EREMOTEIO;
268
269unlock:
270	mutex_unlock(&d->i2c_mutex);
271	return ret;
272}
273
274static u32 cxusb_i2c_func(struct i2c_adapter *adapter)
275{
276	return I2C_FUNC_I2C;
277}
278
279static struct i2c_algorithm cxusb_i2c_algo = {
280	.master_xfer   = cxusb_i2c_xfer,
281	.functionality = cxusb_i2c_func,
282};
283
284static int cxusb_power_ctrl(struct dvb_usb_device *d, int onoff)
285{
286	u8 b = 0;
287	if (onoff)
288		return cxusb_ctrl_msg(d, CMD_POWER_ON, &b, 1, NULL, 0);
289	else
290		return cxusb_ctrl_msg(d, CMD_POWER_OFF, &b, 1, NULL, 0);
291}
292
293static int cxusb_aver_power_ctrl(struct dvb_usb_device *d, int onoff)
294{
295	int ret;
296	if (!onoff)
297		return cxusb_ctrl_msg(d, CMD_POWER_OFF, NULL, 0, NULL, 0);
298	if (d->state == DVB_USB_STATE_INIT &&
299	    usb_set_interface(d->udev, 0, 0) < 0)
300		err("set interface failed");
301	do {} while (!(ret = cxusb_ctrl_msg(d, CMD_POWER_ON, NULL, 0, NULL, 0)) &&
302		   !(ret = cxusb_ctrl_msg(d, 0x15, NULL, 0, NULL, 0)) &&
303		   !(ret = cxusb_ctrl_msg(d, 0x17, NULL, 0, NULL, 0)) && 0);
304	if (!ret) {
305		/* FIXME: We don't know why, but we need to configure the
306		 * lgdt3303 with the register settings below on resume */
307		int i;
308		u8 buf, bufs[] = {
309			0x0e, 0x2, 0x00, 0x7f,
310			0x0e, 0x2, 0x02, 0xfe,
311			0x0e, 0x2, 0x02, 0x01,
312			0x0e, 0x2, 0x00, 0x03,
313			0x0e, 0x2, 0x0d, 0x40,
314			0x0e, 0x2, 0x0e, 0x87,
315			0x0e, 0x2, 0x0f, 0x8e,
316			0x0e, 0x2, 0x10, 0x01,
317			0x0e, 0x2, 0x14, 0xd7,
318			0x0e, 0x2, 0x47, 0x88,
319		};
320		msleep(20);
321		for (i = 0; i < sizeof(bufs)/sizeof(u8); i += 4/sizeof(u8)) {
322			ret = cxusb_ctrl_msg(d, CMD_I2C_WRITE,
323					     bufs+i, 4, &buf, 1);
324			if (ret)
325				break;
326			if (buf != 0x8)
327				return -EREMOTEIO;
328		}
329	}
330	return ret;
331}
332
333static int cxusb_bluebird_power_ctrl(struct dvb_usb_device *d, int onoff)
334{
335	u8 b = 0;
336	if (onoff)
337		return cxusb_ctrl_msg(d, CMD_POWER_ON, &b, 1, NULL, 0);
338	else
339		return 0;
340}
341
342static int cxusb_nano2_power_ctrl(struct dvb_usb_device *d, int onoff)
343{
344	int rc = 0;
345
346	rc = cxusb_power_ctrl(d, onoff);
347	if (!onoff)
348		cxusb_nano2_led(d, 0);
349
350	return rc;
351}
352
353static int cxusb_d680_dmb_power_ctrl(struct dvb_usb_device *d, int onoff)
354{
355	int ret;
356	u8  b;
357	ret = cxusb_power_ctrl(d, onoff);
358	if (!onoff)
359		return ret;
360
361	msleep(128);
362	cxusb_ctrl_msg(d, CMD_DIGITAL, NULL, 0, &b, 1);
363	msleep(100);
364	return ret;
365}
366
367static int cxusb_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
368{
369	u8 buf[2] = { 0x03, 0x00 };
370	if (onoff)
371		cxusb_ctrl_msg(adap->dev, CMD_STREAMING_ON, buf, 2, NULL, 0);
372	else
373		cxusb_ctrl_msg(adap->dev, CMD_STREAMING_OFF, NULL, 0, NULL, 0);
374
375	return 0;
376}
377
378static int cxusb_aver_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
379{
380	if (onoff)
381		cxusb_ctrl_msg(adap->dev, CMD_AVER_STREAM_ON, NULL, 0, NULL, 0);
382	else
383		cxusb_ctrl_msg(adap->dev, CMD_AVER_STREAM_OFF,
384			       NULL, 0, NULL, 0);
385	return 0;
386}
387
388static void cxusb_d680_dmb_drain_message(struct dvb_usb_device *d)
389{
390	int       ep = d->props.generic_bulk_ctrl_endpoint;
391	const int timeout = 100;
392	const int junk_len = 32;
393	u8        *junk;
394	int       rd_count;
395
396	/* Discard remaining data in video pipe */
397	junk = kmalloc(junk_len, GFP_KERNEL);
398	if (!junk)
399		return;
400	while (1) {
401		if (usb_bulk_msg(d->udev,
402			usb_rcvbulkpipe(d->udev, ep),
403			junk, junk_len, &rd_count, timeout) < 0)
404			break;
405		if (!rd_count)
406			break;
407	}
408	kfree(junk);
409}
410
411static void cxusb_d680_dmb_drain_video(struct dvb_usb_device *d)
412{
413	struct usb_data_stream_properties *p = &d->props.adapter[0].fe[0].stream;
414	const int timeout = 100;
415	const int junk_len = p->u.bulk.buffersize;
416	u8        *junk;
417	int       rd_count;
418
419	/* Discard remaining data in video pipe */
420	junk = kmalloc(junk_len, GFP_KERNEL);
421	if (!junk)
422		return;
423	while (1) {
424		if (usb_bulk_msg(d->udev,
425			usb_rcvbulkpipe(d->udev, p->endpoint),
426			junk, junk_len, &rd_count, timeout) < 0)
427			break;
428		if (!rd_count)
429			break;
430	}
431	kfree(junk);
432}
433
434static int cxusb_d680_dmb_streaming_ctrl(
435		struct dvb_usb_adapter *adap, int onoff)
436{
437	if (onoff) {
438		u8 buf[2] = { 0x03, 0x00 };
439		cxusb_d680_dmb_drain_video(adap->dev);
440		return cxusb_ctrl_msg(adap->dev, CMD_STREAMING_ON,
441			buf, sizeof(buf), NULL, 0);
442	} else {
443		int ret = cxusb_ctrl_msg(adap->dev,
444			CMD_STREAMING_OFF, NULL, 0, NULL, 0);
445		return ret;
446	}
447}
448
449static int cxusb_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
450{
451	struct rc_map_table *keymap = d->props.rc.legacy.rc_map_table;
452	u8 ircode[4];
453	int i;
454
455	cxusb_ctrl_msg(d, CMD_GET_IR_CODE, NULL, 0, ircode, 4);
456
457	*event = 0;
458	*state = REMOTE_NO_KEY_PRESSED;
459
460	for (i = 0; i < d->props.rc.legacy.rc_map_size; i++) {
461		if (rc5_custom(&keymap[i]) == ircode[2] &&
462		    rc5_data(&keymap[i]) == ircode[3]) {
463			*event = keymap[i].keycode;
464			*state = REMOTE_KEY_PRESSED;
465
466			return 0;
467		}
468	}
469
470	return 0;
471}
472
473static int cxusb_bluebird2_rc_query(struct dvb_usb_device *d, u32 *event,
474				    int *state)
475{
476	struct rc_map_table *keymap = d->props.rc.legacy.rc_map_table;
477	u8 ircode[4];
478	int i;
479	struct i2c_msg msg = { .addr = 0x6b, .flags = I2C_M_RD,
480			       .buf = ircode, .len = 4 };
481
482	*event = 0;
483	*state = REMOTE_NO_KEY_PRESSED;
484
485	if (cxusb_i2c_xfer(&d->i2c_adap, &msg, 1) != 1)
486		return 0;
487
488	for (i = 0; i < d->props.rc.legacy.rc_map_size; i++) {
489		if (rc5_custom(&keymap[i]) == ircode[1] &&
490		    rc5_data(&keymap[i]) == ircode[2]) {
491			*event = keymap[i].keycode;
492			*state = REMOTE_KEY_PRESSED;
493
494			return 0;
495		}
496	}
497
498	return 0;
499}
500
501static int cxusb_d680_dmb_rc_query(struct dvb_usb_device *d, u32 *event,
502		int *state)
503{
504	struct rc_map_table *keymap = d->props.rc.legacy.rc_map_table;
505	u8 ircode[2];
506	int i;
507
508	*event = 0;
509	*state = REMOTE_NO_KEY_PRESSED;
510
511	if (cxusb_ctrl_msg(d, 0x10, NULL, 0, ircode, 2) < 0)
512		return 0;
513
514	for (i = 0; i < d->props.rc.legacy.rc_map_size; i++) {
515		if (rc5_custom(&keymap[i]) == ircode[0] &&
516		    rc5_data(&keymap[i]) == ircode[1]) {
517			*event = keymap[i].keycode;
518			*state = REMOTE_KEY_PRESSED;
519
520			return 0;
521		}
522	}
523
524	return 0;
525}
526
527static int cxusb_tt_ct2_4400_rc_query(struct dvb_usb_device *d)
528{
529	u8 i[2];
530	int ret;
531	u32 cmd, keycode;
532	u8 rc5_cmd, rc5_addr, rc5_toggle;
533
534	ret = cxusb_ctrl_msg(d, 0x10, NULL, 0, i, 2);
535	if (ret)
536		return ret;
537
538	cmd = (i[0] << 8) | i[1];
539
540	if (cmd != 0xffff) {
541		rc5_cmd = cmd & 0x3F; /* bits 1-6 for command */
542		rc5_addr = (cmd & 0x07C0) >> 6; /* bits 7-11 for address */
543		rc5_toggle = (cmd & 0x0800) >> 11; /* bit 12 for toggle */
544		keycode = (rc5_addr << 8) | rc5_cmd;
545		rc_keydown(d->rc_dev, RC_BIT_RC5, keycode, rc5_toggle);
546	}
547
548	return 0;
549}
550
551static struct rc_map_table rc_map_dvico_mce_table[] = {
552	{ 0xfe02, KEY_TV },
553	{ 0xfe0e, KEY_MP3 },
554	{ 0xfe1a, KEY_DVD },
555	{ 0xfe1e, KEY_FAVORITES },
556	{ 0xfe16, KEY_SETUP },
557	{ 0xfe46, KEY_POWER2 },
558	{ 0xfe0a, KEY_EPG },
559	{ 0xfe49, KEY_BACK },
560	{ 0xfe4d, KEY_MENU },
561	{ 0xfe51, KEY_UP },
562	{ 0xfe5b, KEY_LEFT },
563	{ 0xfe5f, KEY_RIGHT },
564	{ 0xfe53, KEY_DOWN },
565	{ 0xfe5e, KEY_OK },
566	{ 0xfe59, KEY_INFO },
567	{ 0xfe55, KEY_TAB },
568	{ 0xfe0f, KEY_PREVIOUSSONG },/* Replay */
569	{ 0xfe12, KEY_NEXTSONG },	/* Skip */
570	{ 0xfe42, KEY_ENTER	 },	/* Windows/Start */
571	{ 0xfe15, KEY_VOLUMEUP },
572	{ 0xfe05, KEY_VOLUMEDOWN },
573	{ 0xfe11, KEY_CHANNELUP },
574	{ 0xfe09, KEY_CHANNELDOWN },
575	{ 0xfe52, KEY_CAMERA },
576	{ 0xfe5a, KEY_TUNER },	/* Live */
577	{ 0xfe19, KEY_OPEN },
578	{ 0xfe0b, KEY_1 },
579	{ 0xfe17, KEY_2 },
580	{ 0xfe1b, KEY_3 },
581	{ 0xfe07, KEY_4 },
582	{ 0xfe50, KEY_5 },
583	{ 0xfe54, KEY_6 },
584	{ 0xfe48, KEY_7 },
585	{ 0xfe4c, KEY_8 },
586	{ 0xfe58, KEY_9 },
587	{ 0xfe13, KEY_ANGLE },	/* Aspect */
588	{ 0xfe03, KEY_0 },
589	{ 0xfe1f, KEY_ZOOM },
590	{ 0xfe43, KEY_REWIND },
591	{ 0xfe47, KEY_PLAYPAUSE },
592	{ 0xfe4f, KEY_FASTFORWARD },
593	{ 0xfe57, KEY_MUTE },
594	{ 0xfe0d, KEY_STOP },
595	{ 0xfe01, KEY_RECORD },
596	{ 0xfe4e, KEY_POWER },
597};
598
599static struct rc_map_table rc_map_dvico_portable_table[] = {
600	{ 0xfc02, KEY_SETUP },       /* Profile */
601	{ 0xfc43, KEY_POWER2 },
602	{ 0xfc06, KEY_EPG },
603	{ 0xfc5a, KEY_BACK },
604	{ 0xfc05, KEY_MENU },
605	{ 0xfc47, KEY_INFO },
606	{ 0xfc01, KEY_TAB },
607	{ 0xfc42, KEY_PREVIOUSSONG },/* Replay */
608	{ 0xfc49, KEY_VOLUMEUP },
609	{ 0xfc09, KEY_VOLUMEDOWN },
610	{ 0xfc54, KEY_CHANNELUP },
611	{ 0xfc0b, KEY_CHANNELDOWN },
612	{ 0xfc16, KEY_CAMERA },
613	{ 0xfc40, KEY_TUNER },	/* ATV/DTV */
614	{ 0xfc45, KEY_OPEN },
615	{ 0xfc19, KEY_1 },
616	{ 0xfc18, KEY_2 },
617	{ 0xfc1b, KEY_3 },
618	{ 0xfc1a, KEY_4 },
619	{ 0xfc58, KEY_5 },
620	{ 0xfc59, KEY_6 },
621	{ 0xfc15, KEY_7 },
622	{ 0xfc14, KEY_8 },
623	{ 0xfc17, KEY_9 },
624	{ 0xfc44, KEY_ANGLE },	/* Aspect */
625	{ 0xfc55, KEY_0 },
626	{ 0xfc07, KEY_ZOOM },
627	{ 0xfc0a, KEY_REWIND },
628	{ 0xfc08, KEY_PLAYPAUSE },
629	{ 0xfc4b, KEY_FASTFORWARD },
630	{ 0xfc5b, KEY_MUTE },
631	{ 0xfc04, KEY_STOP },
632	{ 0xfc56, KEY_RECORD },
633	{ 0xfc57, KEY_POWER },
634	{ 0xfc41, KEY_UNKNOWN },    /* INPUT */
635	{ 0xfc00, KEY_UNKNOWN },    /* HD */
636};
637
638static struct rc_map_table rc_map_d680_dmb_table[] = {
639	{ 0x0038, KEY_UNKNOWN },	/* TV/AV */
640	{ 0x080c, KEY_ZOOM },
641	{ 0x0800, KEY_0 },
642	{ 0x0001, KEY_1 },
643	{ 0x0802, KEY_2 },
644	{ 0x0003, KEY_3 },
645	{ 0x0804, KEY_4 },
646	{ 0x0005, KEY_5 },
647	{ 0x0806, KEY_6 },
648	{ 0x0007, KEY_7 },
649	{ 0x0808, KEY_8 },
650	{ 0x0009, KEY_9 },
651	{ 0x000a, KEY_MUTE },
652	{ 0x0829, KEY_BACK },
653	{ 0x0012, KEY_CHANNELUP },
654	{ 0x0813, KEY_CHANNELDOWN },
655	{ 0x002b, KEY_VOLUMEUP },
656	{ 0x082c, KEY_VOLUMEDOWN },
657	{ 0x0020, KEY_UP },
658	{ 0x0821, KEY_DOWN },
659	{ 0x0011, KEY_LEFT },
660	{ 0x0810, KEY_RIGHT },
661	{ 0x000d, KEY_OK },
662	{ 0x081f, KEY_RECORD },
663	{ 0x0017, KEY_PLAYPAUSE },
664	{ 0x0816, KEY_PLAYPAUSE },
665	{ 0x000b, KEY_STOP },
666	{ 0x0827, KEY_FASTFORWARD },
667	{ 0x0026, KEY_REWIND },
668	{ 0x081e, KEY_UNKNOWN },    /* Time Shift */
669	{ 0x000e, KEY_UNKNOWN },    /* Snapshot */
670	{ 0x082d, KEY_UNKNOWN },    /* Mouse Cursor */
671	{ 0x000f, KEY_UNKNOWN },    /* Minimize/Maximize */
672	{ 0x0814, KEY_UNKNOWN },    /* Shuffle */
673	{ 0x0025, KEY_POWER },
674};
675
676static int cxusb_tt_ct2_4400_read_mac_address(struct dvb_usb_device *d, u8 mac[6])
677{
678	u8 wbuf[2];
679	u8 rbuf[6];
680	int ret;
681	struct i2c_msg msg[] = {
682		{
683			.addr = 0x51,
684			.flags = 0,
685			.buf = wbuf,
686			.len = 2,
687		}, {
688			.addr = 0x51,
689			.flags = I2C_M_RD,
690			.buf = rbuf,
691			.len = 6,
692		}
693	};
694
695	wbuf[0] = 0x1e;
696	wbuf[1] = 0x00;
697	ret = cxusb_i2c_xfer(&d->i2c_adap, msg, 2);
698
699	if (ret == 2) {
700		memcpy(mac, rbuf, 6);
701		return 0;
702	} else {
703		if (ret < 0)
704			return ret;
705		return -EIO;
706	}
707}
708
709static int cxusb_tt_ct2_4650_ci_ctrl(void *priv, u8 read, int addr,
710					u8 data, int *mem)
711{
712	struct dvb_usb_device *d = priv;
713	u8 wbuf[3];
714	u8 rbuf[2];
715	int ret;
716
717	wbuf[0] = (addr >> 8) & 0xff;
718	wbuf[1] = addr & 0xff;
719
720	if (read) {
721		ret = cxusb_ctrl_msg(d, CMD_SP2_CI_READ, wbuf, 2, rbuf, 2);
722	} else {
723		wbuf[2] = data;
724		ret = cxusb_ctrl_msg(d, CMD_SP2_CI_WRITE, wbuf, 3, rbuf, 1);
725	}
726
727	if (ret)
728		goto err;
729
730	if (read)
731		*mem = rbuf[1];
732
733	return 0;
734err:
735	deb_info("%s: ci usb write returned %d\n", __func__, ret);
736	return ret;
737
738}
739
740static int cxusb_dee1601_demod_init(struct dvb_frontend* fe)
741{
742	static u8 clock_config []  = { CLOCK_CTL,  0x38, 0x28 };
743	static u8 reset []         = { RESET,      0x80 };
744	static u8 adc_ctl_1_cfg [] = { ADC_CTL_1,  0x40 };
745	static u8 agc_cfg []       = { AGC_TARGET, 0x28, 0x20 };
746	static u8 gpp_ctl_cfg []   = { GPP_CTL,    0x33 };
747	static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
748
749	mt352_write(fe, clock_config,   sizeof(clock_config));
750	udelay(200);
751	mt352_write(fe, reset,          sizeof(reset));
752	mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
753
754	mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
755	mt352_write(fe, gpp_ctl_cfg,    sizeof(gpp_ctl_cfg));
756	mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
757
758	return 0;
759}
760
761static int cxusb_mt352_demod_init(struct dvb_frontend* fe)
762{	/* used in both lgz201 and th7579 */
763	static u8 clock_config []  = { CLOCK_CTL,  0x38, 0x29 };
764	static u8 reset []         = { RESET,      0x80 };
765	static u8 adc_ctl_1_cfg [] = { ADC_CTL_1,  0x40 };
766	static u8 agc_cfg []       = { AGC_TARGET, 0x24, 0x20 };
767	static u8 gpp_ctl_cfg []   = { GPP_CTL,    0x33 };
768	static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
769
770	mt352_write(fe, clock_config,   sizeof(clock_config));
771	udelay(200);
772	mt352_write(fe, reset,          sizeof(reset));
773	mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
774
775	mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
776	mt352_write(fe, gpp_ctl_cfg,    sizeof(gpp_ctl_cfg));
777	mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
778	return 0;
779}
780
781static struct cx22702_config cxusb_cx22702_config = {
782	.demod_address = 0x63,
783	.output_mode = CX22702_PARALLEL_OUTPUT,
784};
785
786static struct lgdt330x_config cxusb_lgdt3303_config = {
787	.demod_address = 0x0e,
788	.demod_chip    = LGDT3303,
789};
790
791static struct lgdt330x_config cxusb_aver_lgdt3303_config = {
792	.demod_address       = 0x0e,
793	.demod_chip          = LGDT3303,
794	.clock_polarity_flip = 2,
795};
796
797static struct mt352_config cxusb_dee1601_config = {
798	.demod_address = 0x0f,
799	.demod_init    = cxusb_dee1601_demod_init,
800};
801
802static struct zl10353_config cxusb_zl10353_dee1601_config = {
803	.demod_address = 0x0f,
804	.parallel_ts = 1,
805};
806
807static struct mt352_config cxusb_mt352_config = {
808	/* used in both lgz201 and th7579 */
809	.demod_address = 0x0f,
810	.demod_init    = cxusb_mt352_demod_init,
811};
812
813static struct zl10353_config cxusb_zl10353_xc3028_config = {
814	.demod_address = 0x0f,
815	.if2 = 45600,
816	.no_tuner = 1,
817	.parallel_ts = 1,
818};
819
820static struct zl10353_config cxusb_zl10353_xc3028_config_no_i2c_gate = {
821	.demod_address = 0x0f,
822	.if2 = 45600,
823	.no_tuner = 1,
824	.parallel_ts = 1,
825	.disable_i2c_gate_ctrl = 1,
826};
827
828static struct mt352_config cxusb_mt352_xc3028_config = {
829	.demod_address = 0x0f,
830	.if2 = 4560,
831	.no_tuner = 1,
832	.demod_init = cxusb_mt352_demod_init,
833};
834
835/* FIXME: needs tweaking */
836static struct mxl5005s_config aver_a868r_tuner = {
837	.i2c_address     = 0x63,
838	.if_freq         = 6000000UL,
839	.xtal_freq       = CRYSTAL_FREQ_16000000HZ,
840	.agc_mode        = MXL_SINGLE_AGC,
841	.tracking_filter = MXL_TF_C,
842	.rssi_enable     = MXL_RSSI_ENABLE,
843	.cap_select      = MXL_CAP_SEL_ENABLE,
844	.div_out         = MXL_DIV_OUT_4,
845	.clock_out       = MXL_CLOCK_OUT_DISABLE,
846	.output_load     = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
847	.top		 = MXL5005S_TOP_25P2,
848	.mod_mode        = MXL_DIGITAL_MODE,
849	.if_mode         = MXL_ZERO_IF,
850	.AgcMasterByte   = 0x00,
851};
852
853/* FIXME: needs tweaking */
854static struct mxl5005s_config d680_dmb_tuner = {
855	.i2c_address     = 0x63,
856	.if_freq         = 36125000UL,
857	.xtal_freq       = CRYSTAL_FREQ_16000000HZ,
858	.agc_mode        = MXL_SINGLE_AGC,
859	.tracking_filter = MXL_TF_C,
860	.rssi_enable     = MXL_RSSI_ENABLE,
861	.cap_select      = MXL_CAP_SEL_ENABLE,
862	.div_out         = MXL_DIV_OUT_4,
863	.clock_out       = MXL_CLOCK_OUT_DISABLE,
864	.output_load     = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
865	.top		 = MXL5005S_TOP_25P2,
866	.mod_mode        = MXL_DIGITAL_MODE,
867	.if_mode         = MXL_ZERO_IF,
868	.AgcMasterByte   = 0x00,
869};
870
871static struct max2165_config mygica_d689_max2165_cfg = {
872	.i2c_address = 0x60,
873	.osc_clk = 20
874};
875
876/* Callbacks for DVB USB */
877static int cxusb_fmd1216me_tuner_attach(struct dvb_usb_adapter *adap)
878{
879	dvb_attach(simple_tuner_attach, adap->fe_adap[0].fe,
880		   &adap->dev->i2c_adap, 0x61,
881		   TUNER_PHILIPS_FMD1216ME_MK3);
882	return 0;
883}
884
885static int cxusb_dee1601_tuner_attach(struct dvb_usb_adapter *adap)
886{
887	dvb_attach(dvb_pll_attach, adap->fe_adap[0].fe, 0x61,
888		   NULL, DVB_PLL_THOMSON_DTT7579);
889	return 0;
890}
891
892static int cxusb_lgz201_tuner_attach(struct dvb_usb_adapter *adap)
893{
894	dvb_attach(dvb_pll_attach, adap->fe_adap[0].fe, 0x61, NULL, DVB_PLL_LG_Z201);
895	return 0;
896}
897
898static int cxusb_dtt7579_tuner_attach(struct dvb_usb_adapter *adap)
899{
900	dvb_attach(dvb_pll_attach, adap->fe_adap[0].fe, 0x60,
901		   NULL, DVB_PLL_THOMSON_DTT7579);
902	return 0;
903}
904
905static int cxusb_lgh064f_tuner_attach(struct dvb_usb_adapter *adap)
906{
907	dvb_attach(simple_tuner_attach, adap->fe_adap[0].fe,
908		   &adap->dev->i2c_adap, 0x61, TUNER_LG_TDVS_H06XF);
909	return 0;
910}
911
912static int dvico_bluebird_xc2028_callback(void *ptr, int component,
913					  int command, int arg)
914{
915	struct dvb_usb_adapter *adap = ptr;
916	struct dvb_usb_device *d = adap->dev;
917
918	switch (command) {
919	case XC2028_TUNER_RESET:
920		deb_info("%s: XC2028_TUNER_RESET %d\n", __func__, arg);
921		cxusb_bluebird_gpio_pulse(d, 0x01, 1);
922		break;
923	case XC2028_RESET_CLK:
924		deb_info("%s: XC2028_RESET_CLK %d\n", __func__, arg);
925		break;
926	default:
927		deb_info("%s: unknown command %d, arg %d\n", __func__,
928			 command, arg);
929		return -EINVAL;
930	}
931
932	return 0;
933}
934
935static int cxusb_dvico_xc3028_tuner_attach(struct dvb_usb_adapter *adap)
936{
937	struct dvb_frontend	 *fe;
938	struct xc2028_config	  cfg = {
939		.i2c_adap  = &adap->dev->i2c_adap,
940		.i2c_addr  = 0x61,
941	};
942	static struct xc2028_ctrl ctl = {
943		.fname       = XC2028_DEFAULT_FIRMWARE,
944		.max_len     = 64,
945		.demod       = XC3028_FE_ZARLINK456,
946	};
947
948	/* FIXME: generalize & move to common area */
949	adap->fe_adap[0].fe->callback = dvico_bluebird_xc2028_callback;
950
951	fe = dvb_attach(xc2028_attach, adap->fe_adap[0].fe, &cfg);
952	if (fe == NULL || fe->ops.tuner_ops.set_config == NULL)
953		return -EIO;
954
955	fe->ops.tuner_ops.set_config(fe, &ctl);
956
957	return 0;
958}
959
960static int cxusb_mxl5003s_tuner_attach(struct dvb_usb_adapter *adap)
961{
962	dvb_attach(mxl5005s_attach, adap->fe_adap[0].fe,
963		   &adap->dev->i2c_adap, &aver_a868r_tuner);
964	return 0;
965}
966
967static int cxusb_d680_dmb_tuner_attach(struct dvb_usb_adapter *adap)
968{
969	struct dvb_frontend *fe;
970	fe = dvb_attach(mxl5005s_attach, adap->fe_adap[0].fe,
971			&adap->dev->i2c_adap, &d680_dmb_tuner);
972	return (fe == NULL) ? -EIO : 0;
973}
974
975static int cxusb_mygica_d689_tuner_attach(struct dvb_usb_adapter *adap)
976{
977	struct dvb_frontend *fe;
978	fe = dvb_attach(max2165_attach, adap->fe_adap[0].fe,
979			&adap->dev->i2c_adap, &mygica_d689_max2165_cfg);
980	return (fe == NULL) ? -EIO : 0;
981}
982
983static int cxusb_cx22702_frontend_attach(struct dvb_usb_adapter *adap)
984{
985	u8 b;
986	if (usb_set_interface(adap->dev->udev, 0, 6) < 0)
987		err("set interface failed");
988
989	cxusb_ctrl_msg(adap->dev, CMD_DIGITAL, NULL, 0, &b, 1);
990
991	adap->fe_adap[0].fe = dvb_attach(cx22702_attach, &cxusb_cx22702_config,
992					 &adap->dev->i2c_adap);
993	if ((adap->fe_adap[0].fe) != NULL)
994		return 0;
995
996	return -EIO;
997}
998
999static int cxusb_lgdt3303_frontend_attach(struct dvb_usb_adapter *adap)
1000{
1001	if (usb_set_interface(adap->dev->udev, 0, 7) < 0)
1002		err("set interface failed");
1003
1004	cxusb_ctrl_msg(adap->dev, CMD_DIGITAL, NULL, 0, NULL, 0);
1005
1006	adap->fe_adap[0].fe = dvb_attach(lgdt330x_attach,
1007					 &cxusb_lgdt3303_config,
1008					 &adap->dev->i2c_adap);
1009	if ((adap->fe_adap[0].fe) != NULL)
1010		return 0;
1011
1012	return -EIO;
1013}
1014
1015static int cxusb_aver_lgdt3303_frontend_attach(struct dvb_usb_adapter *adap)
1016{
1017	adap->fe_adap[0].fe = dvb_attach(lgdt330x_attach, &cxusb_aver_lgdt3303_config,
1018			      &adap->dev->i2c_adap);
1019	if (adap->fe_adap[0].fe != NULL)
1020		return 0;
1021
1022	return -EIO;
1023}
1024
1025static int cxusb_mt352_frontend_attach(struct dvb_usb_adapter *adap)
1026{
1027	/* used in both lgz201 and th7579 */
1028	if (usb_set_interface(adap->dev->udev, 0, 0) < 0)
1029		err("set interface failed");
1030
1031	cxusb_ctrl_msg(adap->dev, CMD_DIGITAL, NULL, 0, NULL, 0);
1032
1033	adap->fe_adap[0].fe = dvb_attach(mt352_attach, &cxusb_mt352_config,
1034					 &adap->dev->i2c_adap);
1035	if ((adap->fe_adap[0].fe) != NULL)
1036		return 0;
1037
1038	return -EIO;
1039}
1040
1041static int cxusb_dee1601_frontend_attach(struct dvb_usb_adapter *adap)
1042{
1043	if (usb_set_interface(adap->dev->udev, 0, 0) < 0)
1044		err("set interface failed");
1045
1046	cxusb_ctrl_msg(adap->dev, CMD_DIGITAL, NULL, 0, NULL, 0);
1047
1048	adap->fe_adap[0].fe = dvb_attach(mt352_attach, &cxusb_dee1601_config,
1049					 &adap->dev->i2c_adap);
1050	if ((adap->fe_adap[0].fe) != NULL)
1051		return 0;
1052
1053	adap->fe_adap[0].fe = dvb_attach(zl10353_attach,
1054					 &cxusb_zl10353_dee1601_config,
1055					 &adap->dev->i2c_adap);
1056	if ((adap->fe_adap[0].fe) != NULL)
1057		return 0;
1058
1059	return -EIO;
1060}
1061
1062static int cxusb_dualdig4_frontend_attach(struct dvb_usb_adapter *adap)
1063{
1064	u8 ircode[4];
1065	int i;
1066	struct i2c_msg msg = { .addr = 0x6b, .flags = I2C_M_RD,
1067			       .buf = ircode, .len = 4 };
1068
1069	if (usb_set_interface(adap->dev->udev, 0, 1) < 0)
1070		err("set interface failed");
1071
1072	cxusb_ctrl_msg(adap->dev, CMD_DIGITAL, NULL, 0, NULL, 0);
1073
1074	/* reset the tuner and demodulator */
1075	cxusb_bluebird_gpio_rw(adap->dev, 0x04, 0);
1076	cxusb_bluebird_gpio_pulse(adap->dev, 0x01, 1);
1077	cxusb_bluebird_gpio_pulse(adap->dev, 0x02, 1);
1078
1079	adap->fe_adap[0].fe =
1080		dvb_attach(zl10353_attach,
1081			   &cxusb_zl10353_xc3028_config_no_i2c_gate,
1082			   &adap->dev->i2c_adap);
1083	if ((adap->fe_adap[0].fe) == NULL)
1084		return -EIO;
1085
1086	/* try to determine if there is no IR decoder on the I2C bus */
1087	for (i = 0; adap->dev->props.rc.legacy.rc_map_table != NULL && i < 5; i++) {
1088		msleep(20);
1089		if (cxusb_i2c_xfer(&adap->dev->i2c_adap, &msg, 1) != 1)
1090			goto no_IR;
1091		if (ircode[0] == 0 && ircode[1] == 0)
1092			continue;
1093		if (ircode[2] + ircode[3] != 0xff) {
1094no_IR:
1095			adap->dev->props.rc.legacy.rc_map_table = NULL;
1096			info("No IR receiver detected on this device.");
1097			break;
1098		}
1099	}
1100
1101	return 0;
1102}
1103
1104static struct dibx000_agc_config dib7070_agc_config = {
1105	.band_caps = BAND_UHF | BAND_VHF | BAND_LBAND | BAND_SBAND,
1106
1107	/*
1108	 * P_agc_use_sd_mod1=0, P_agc_use_sd_mod2=0, P_agc_freq_pwm_div=5,
1109	 * P_agc_inv_pwm1=0, P_agc_inv_pwm2=0, P_agc_inh_dc_rv_est=0,
1110	 * P_agc_time_est=3, P_agc_freeze=0, P_agc_nb_est=5, P_agc_write=0
1111	 */
1112	.setup = (0 << 15) | (0 << 14) | (5 << 11) | (0 << 10) | (0 << 9) |
1113		 (0 << 8) | (3 << 5) | (0 << 4) | (5 << 1) | (0 << 0),
1114	.inv_gain = 600,
1115	.time_stabiliz = 10,
1116	.alpha_level = 0,
1117	.thlock = 118,
1118	.wbd_inv = 0,
1119	.wbd_ref = 3530,
1120	.wbd_sel = 1,
1121	.wbd_alpha = 5,
1122	.agc1_max = 65535,
1123	.agc1_min = 0,
1124	.agc2_max = 65535,
1125	.agc2_min = 0,
1126	.agc1_pt1 = 0,
1127	.agc1_pt2 = 40,
1128	.agc1_pt3 = 183,
1129	.agc1_slope1 = 206,
1130	.agc1_slope2 = 255,
1131	.agc2_pt1 = 72,
1132	.agc2_pt2 = 152,
1133	.agc2_slope1 = 88,
1134	.agc2_slope2 = 90,
1135	.alpha_mant = 17,
1136	.alpha_exp = 27,
1137	.beta_mant = 23,
1138	.beta_exp = 51,
1139	.perform_agc_softsplit = 0,
1140};
1141
1142static struct dibx000_bandwidth_config dib7070_bw_config_12_mhz = {
1143	.internal = 60000,
1144	.sampling = 15000,
1145	.pll_prediv = 1,
1146	.pll_ratio = 20,
1147	.pll_range = 3,
1148	.pll_reset = 1,
1149	.pll_bypass = 0,
1150	.enable_refdiv = 0,
1151	.bypclk_div = 0,
1152	.IO_CLK_en_core = 1,
1153	.ADClkSrc = 1,
1154	.modulo = 2,
1155	/* refsel, sel, freq_15k */
1156	.sad_cfg = (3 << 14) | (1 << 12) | (524 << 0),
1157	.ifreq = (0 << 25) | 0,
1158	.timf = 20452225,
1159	.xtal_hz = 12000000,
1160};
1161
1162static struct dib7000p_config cxusb_dualdig4_rev2_config = {
1163	.output_mode = OUTMODE_MPEG2_PAR_GATED_CLK,
1164	.output_mpeg2_in_188_bytes = 1,
1165
1166	.agc_config_count = 1,
1167	.agc = &dib7070_agc_config,
1168	.bw  = &dib7070_bw_config_12_mhz,
1169	.tuner_is_baseband = 1,
1170	.spur_protect = 1,
1171
1172	.gpio_dir = 0xfcef,
1173	.gpio_val = 0x0110,
1174
1175	.gpio_pwm_pos = DIB7000P_GPIO_DEFAULT_PWM_POS,
1176
1177	.hostbus_diversity = 1,
1178};
1179
1180struct dib0700_adapter_state {
1181	int (*set_param_save)(struct dvb_frontend *);
1182	struct dib7000p_ops dib7000p_ops;
1183};
1184
1185static int cxusb_dualdig4_rev2_frontend_attach(struct dvb_usb_adapter *adap)
1186{
1187	struct dib0700_adapter_state *state = adap->priv;
1188
1189	if (usb_set_interface(adap->dev->udev, 0, 1) < 0)
1190		err("set interface failed");
1191
1192	cxusb_ctrl_msg(adap->dev, CMD_DIGITAL, NULL, 0, NULL, 0);
1193
1194	cxusb_bluebird_gpio_pulse(adap->dev, 0x02, 1);
1195
1196	if (!dvb_attach(dib7000p_attach, &state->dib7000p_ops))
1197		return -ENODEV;
1198
1199	if (state->dib7000p_ops.i2c_enumeration(&adap->dev->i2c_adap, 1, 18,
1200				       &cxusb_dualdig4_rev2_config) < 0) {
1201		printk(KERN_WARNING "Unable to enumerate dib7000p\n");
1202		return -ENODEV;
1203	}
1204
1205	adap->fe_adap[0].fe = state->dib7000p_ops.init(&adap->dev->i2c_adap, 0x80,
1206					      &cxusb_dualdig4_rev2_config);
1207	if (adap->fe_adap[0].fe == NULL)
1208		return -EIO;
1209
1210	return 0;
1211}
1212
1213static int dib7070_tuner_reset(struct dvb_frontend *fe, int onoff)
1214{
1215	struct dvb_usb_adapter *adap = fe->dvb->priv;
1216	struct dib0700_adapter_state *state = adap->priv;
1217
1218	return state->dib7000p_ops.set_gpio(fe, 8, 0, !onoff);
1219}
1220
1221static int dib7070_tuner_sleep(struct dvb_frontend *fe, int onoff)
1222{
1223	return 0;
1224}
1225
1226static struct dib0070_config dib7070p_dib0070_config = {
1227	.i2c_address = DEFAULT_DIB0070_I2C_ADDRESS,
1228	.reset = dib7070_tuner_reset,
1229	.sleep = dib7070_tuner_sleep,
1230	.clock_khz = 12000,
1231};
1232
1233static int dib7070_set_param_override(struct dvb_frontend *fe)
1234{
1235	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1236	struct dvb_usb_adapter *adap = fe->dvb->priv;
1237	struct dib0700_adapter_state *state = adap->priv;
1238
1239	u16 offset;
1240	u8 band = BAND_OF_FREQUENCY(p->frequency/1000);
1241	switch (band) {
1242	case BAND_VHF: offset = 950; break;
1243	default:
1244	case BAND_UHF: offset = 550; break;
1245	}
1246
1247	state->dib7000p_ops.set_wbd_ref(fe, offset + dib0070_wbd_offset(fe));
1248
1249	return state->set_param_save(fe);
1250}
1251
1252static int cxusb_dualdig4_rev2_tuner_attach(struct dvb_usb_adapter *adap)
1253{
1254	struct dib0700_adapter_state *st = adap->priv;
1255	struct i2c_adapter *tun_i2c;
1256
1257	/*
1258	 * No need to call dvb7000p_attach here, as it was called
1259	 * already, as frontend_attach method is called first, and
1260	 * tuner_attach is only called on sucess.
1261	 */
1262	tun_i2c = st->dib7000p_ops.get_i2c_master(adap->fe_adap[0].fe,
1263					DIBX000_I2C_INTERFACE_TUNER, 1);
1264
1265	if (dvb_attach(dib0070_attach, adap->fe_adap[0].fe, tun_i2c,
1266	    &dib7070p_dib0070_config) == NULL)
1267		return -ENODEV;
1268
1269	st->set_param_save = adap->fe_adap[0].fe->ops.tuner_ops.set_params;
1270	adap->fe_adap[0].fe->ops.tuner_ops.set_params = dib7070_set_param_override;
1271	return 0;
1272}
1273
1274static int cxusb_nano2_frontend_attach(struct dvb_usb_adapter *adap)
1275{
1276	if (usb_set_interface(adap->dev->udev, 0, 1) < 0)
1277		err("set interface failed");
1278
1279	cxusb_ctrl_msg(adap->dev, CMD_DIGITAL, NULL, 0, NULL, 0);
1280
1281	/* reset the tuner and demodulator */
1282	cxusb_bluebird_gpio_rw(adap->dev, 0x04, 0);
1283	cxusb_bluebird_gpio_pulse(adap->dev, 0x01, 1);
1284	cxusb_bluebird_gpio_pulse(adap->dev, 0x02, 1);
1285
1286	adap->fe_adap[0].fe = dvb_attach(zl10353_attach,
1287					 &cxusb_zl10353_xc3028_config,
1288					 &adap->dev->i2c_adap);
1289	if ((adap->fe_adap[0].fe) != NULL)
1290		return 0;
1291
1292	adap->fe_adap[0].fe = dvb_attach(mt352_attach,
1293					 &cxusb_mt352_xc3028_config,
1294					 &adap->dev->i2c_adap);
1295	if ((adap->fe_adap[0].fe) != NULL)
1296		return 0;
1297
1298	return -EIO;
1299}
1300
1301static struct lgs8gxx_config d680_lgs8gl5_cfg = {
1302	.prod = LGS8GXX_PROD_LGS8GL5,
1303	.demod_address = 0x19,
1304	.serial_ts = 0,
1305	.ts_clk_pol = 0,
1306	.ts_clk_gated = 1,
1307	.if_clk_freq = 30400, /* 30.4 MHz */
1308	.if_freq = 5725, /* 5.725 MHz */
1309	.if_neg_center = 0,
1310	.ext_adc = 0,
1311	.adc_signed = 0,
1312	.if_neg_edge = 0,
1313};
1314
1315static int cxusb_d680_dmb_frontend_attach(struct dvb_usb_adapter *adap)
1316{
1317	struct dvb_usb_device *d = adap->dev;
1318	int n;
1319
1320	/* Select required USB configuration */
1321	if (usb_set_interface(d->udev, 0, 0) < 0)
1322		err("set interface failed");
1323
1324	/* Unblock all USB pipes */
1325	usb_clear_halt(d->udev,
1326		usb_sndbulkpipe(d->udev, d->props.generic_bulk_ctrl_endpoint));
1327	usb_clear_halt(d->udev,
1328		usb_rcvbulkpipe(d->udev, d->props.generic_bulk_ctrl_endpoint));
1329	usb_clear_halt(d->udev,
1330		usb_rcvbulkpipe(d->udev, d->props.adapter[0].fe[0].stream.endpoint));
1331
1332	/* Drain USB pipes to avoid hang after reboot */
1333	for (n = 0;  n < 5;  n++) {
1334		cxusb_d680_dmb_drain_message(d);
1335		cxusb_d680_dmb_drain_video(d);
1336		msleep(200);
1337	}
1338
1339	/* Reset the tuner */
1340	if (cxusb_d680_dmb_gpio_tuner(d, 0x07, 0) < 0) {
1341		err("clear tuner gpio failed");
1342		return -EIO;
1343	}
1344	msleep(100);
1345	if (cxusb_d680_dmb_gpio_tuner(d, 0x07, 1) < 0) {
1346		err("set tuner gpio failed");
1347		return -EIO;
1348	}
1349	msleep(100);
1350
1351	/* Attach frontend */
1352	adap->fe_adap[0].fe = dvb_attach(lgs8gxx_attach, &d680_lgs8gl5_cfg, &d->i2c_adap);
1353	if (adap->fe_adap[0].fe == NULL)
1354		return -EIO;
1355
1356	return 0;
1357}
1358
1359static struct atbm8830_config mygica_d689_atbm8830_cfg = {
1360	.prod = ATBM8830_PROD_8830,
1361	.demod_address = 0x40,
1362	.serial_ts = 0,
1363	.ts_sampling_edge = 1,
1364	.ts_clk_gated = 0,
1365	.osc_clk_freq = 30400, /* in kHz */
1366	.if_freq = 0, /* zero IF */
1367	.zif_swap_iq = 1,
1368	.agc_min = 0x2E,
1369	.agc_max = 0x90,
1370	.agc_hold_loop = 0,
1371};
1372
1373static int cxusb_mygica_d689_frontend_attach(struct dvb_usb_adapter *adap)
1374{
1375	struct dvb_usb_device *d = adap->dev;
1376
1377	/* Select required USB configuration */
1378	if (usb_set_interface(d->udev, 0, 0) < 0)
1379		err("set interface failed");
1380
1381	/* Unblock all USB pipes */
1382	usb_clear_halt(d->udev,
1383		usb_sndbulkpipe(d->udev, d->props.generic_bulk_ctrl_endpoint));
1384	usb_clear_halt(d->udev,
1385		usb_rcvbulkpipe(d->udev, d->props.generic_bulk_ctrl_endpoint));
1386	usb_clear_halt(d->udev,
1387		usb_rcvbulkpipe(d->udev, d->props.adapter[0].fe[0].stream.endpoint));
1388
1389
1390	/* Reset the tuner */
1391	if (cxusb_d680_dmb_gpio_tuner(d, 0x07, 0) < 0) {
1392		err("clear tuner gpio failed");
1393		return -EIO;
1394	}
1395	msleep(100);
1396	if (cxusb_d680_dmb_gpio_tuner(d, 0x07, 1) < 0) {
1397		err("set tuner gpio failed");
1398		return -EIO;
1399	}
1400	msleep(100);
1401
1402	/* Attach frontend */
1403	adap->fe_adap[0].fe = dvb_attach(atbm8830_attach, &mygica_d689_atbm8830_cfg,
1404		&d->i2c_adap);
1405	if (adap->fe_adap[0].fe == NULL)
1406		return -EIO;
1407
1408	return 0;
1409}
1410
1411static int cxusb_tt_ct2_4400_attach(struct dvb_usb_adapter *adap)
1412{
1413	struct dvb_usb_device *d = adap->dev;
1414	struct cxusb_state *st = d->priv;
1415	struct i2c_adapter *adapter;
1416	struct i2c_client *client_demod;
1417	struct i2c_client *client_tuner;
1418	struct i2c_client *client_ci;
1419	struct i2c_board_info info;
1420	struct si2168_config si2168_config;
1421	struct si2157_config si2157_config;
1422	struct sp2_config sp2_config;
1423	u8 o[2], i;
1424
1425	/* reset the tuner */
1426	if (cxusb_tt_ct2_4400_gpio_tuner(d, 0) < 0) {
1427		err("clear tuner gpio failed");
1428		return -EIO;
1429	}
1430	msleep(100);
1431	if (cxusb_tt_ct2_4400_gpio_tuner(d, 1) < 0) {
1432		err("set tuner gpio failed");
1433		return -EIO;
1434	}
1435	msleep(100);
1436
1437	/* attach frontend */
1438	si2168_config.i2c_adapter = &adapter;
1439	si2168_config.fe = &adap->fe_adap[0].fe;
1440	si2168_config.ts_mode = SI2168_TS_PARALLEL;
1441	memset(&info, 0, sizeof(struct i2c_board_info));
1442	strlcpy(info.type, "si2168", I2C_NAME_SIZE);
1443	info.addr = 0x64;
1444	info.platform_data = &si2168_config;
1445	request_module(info.type);
1446	client_demod = i2c_new_device(&d->i2c_adap, &info);
1447	if (client_demod == NULL || client_demod->dev.driver == NULL)
1448		return -ENODEV;
1449
1450	if (!try_module_get(client_demod->dev.driver->owner)) {
1451		i2c_unregister_device(client_demod);
1452		return -ENODEV;
1453	}
1454
1455	st->i2c_client_demod = client_demod;
1456
1457	/* attach tuner */
1458	memset(&si2157_config, 0, sizeof(si2157_config));
1459	si2157_config.fe = adap->fe_adap[0].fe;
1460	memset(&info, 0, sizeof(struct i2c_board_info));
1461	strlcpy(info.type, "si2157", I2C_NAME_SIZE);
1462	info.addr = 0x60;
1463	info.platform_data = &si2157_config;
1464	request_module(info.type);
1465	client_tuner = i2c_new_device(adapter, &info);
1466	if (client_tuner == NULL || client_tuner->dev.driver == NULL) {
1467		module_put(client_demod->dev.driver->owner);
1468		i2c_unregister_device(client_demod);
1469		return -ENODEV;
1470	}
1471	if (!try_module_get(client_tuner->dev.driver->owner)) {
1472		i2c_unregister_device(client_tuner);
1473		module_put(client_demod->dev.driver->owner);
1474		i2c_unregister_device(client_demod);
1475		return -ENODEV;
1476	}
1477
1478	st->i2c_client_tuner = client_tuner;
1479
1480	/* initialize CI */
1481	if (d->udev->descriptor.idProduct ==
1482		USB_PID_TECHNOTREND_CONNECT_CT2_4650_CI) {
1483
1484		memcpy(o, "\xc0\x01", 2);
1485		cxusb_ctrl_msg(d, CMD_GPIO_WRITE, o, 2, &i, 1);
1486		msleep(100);
1487
1488		memcpy(o, "\xc0\x00", 2);
1489		cxusb_ctrl_msg(d, CMD_GPIO_WRITE, o, 2, &i, 1);
1490		msleep(100);
1491
1492		memset(&sp2_config, 0, sizeof(sp2_config));
1493		sp2_config.dvb_adap = &adap->dvb_adap;
1494		sp2_config.priv = d;
1495		sp2_config.ci_control = cxusb_tt_ct2_4650_ci_ctrl;
1496		memset(&info, 0, sizeof(struct i2c_board_info));
1497		strlcpy(info.type, "sp2", I2C_NAME_SIZE);
1498		info.addr = 0x40;
1499		info.platform_data = &sp2_config;
1500		request_module(info.type);
1501		client_ci = i2c_new_device(&d->i2c_adap, &info);
1502		if (client_ci == NULL || client_ci->dev.driver == NULL) {
1503			module_put(client_tuner->dev.driver->owner);
1504			i2c_unregister_device(client_tuner);
1505			module_put(client_demod->dev.driver->owner);
1506			i2c_unregister_device(client_demod);
1507			return -ENODEV;
1508		}
1509		if (!try_module_get(client_ci->dev.driver->owner)) {
1510			i2c_unregister_device(client_ci);
1511			module_put(client_tuner->dev.driver->owner);
1512			i2c_unregister_device(client_tuner);
1513			module_put(client_demod->dev.driver->owner);
1514			i2c_unregister_device(client_demod);
1515			return -ENODEV;
1516		}
1517
1518		st->i2c_client_ci = client_ci;
1519
1520	}
1521
1522	return 0;
1523}
1524
1525/*
1526 * DViCO has shipped two devices with the same USB ID, but only one of them
1527 * needs a firmware download.  Check the device class details to see if they
1528 * have non-default values to decide whether the device is actually cold or
1529 * not, and forget a match if it turns out we selected the wrong device.
1530 */
1531static int bluebird_fx2_identify_state(struct usb_device *udev,
1532				       struct dvb_usb_device_properties *props,
1533				       struct dvb_usb_device_description **desc,
1534				       int *cold)
1535{
1536	int wascold = *cold;
1537
1538	*cold = udev->descriptor.bDeviceClass == 0xff &&
1539		udev->descriptor.bDeviceSubClass == 0xff &&
1540		udev->descriptor.bDeviceProtocol == 0xff;
1541
1542	if (*cold && !wascold)
1543		*desc = NULL;
1544
1545	return 0;
1546}
1547
1548/*
1549 * DViCO bluebird firmware needs the "warm" product ID to be patched into the
1550 * firmware file before download.
1551 */
1552
1553static const int dvico_firmware_id_offsets[] = { 6638, 3204 };
1554static int bluebird_patch_dvico_firmware_download(struct usb_device *udev,
1555						  const struct firmware *fw)
1556{
1557	int pos;
1558
1559	for (pos = 0; pos < ARRAY_SIZE(dvico_firmware_id_offsets); pos++) {
1560		int idoff = dvico_firmware_id_offsets[pos];
1561
1562		if (fw->size < idoff + 4)
1563			continue;
1564
1565		if (fw->data[idoff] == (USB_VID_DVICO & 0xff) &&
1566		    fw->data[idoff + 1] == USB_VID_DVICO >> 8) {
1567			struct firmware new_fw;
1568			u8 *new_fw_data = vmalloc(fw->size);
1569			int ret;
1570
1571			if (!new_fw_data)
1572				return -ENOMEM;
1573
1574			memcpy(new_fw_data, fw->data, fw->size);
1575			new_fw.size = fw->size;
1576			new_fw.data = new_fw_data;
1577
1578			new_fw_data[idoff + 2] =
1579				le16_to_cpu(udev->descriptor.idProduct) + 1;
1580			new_fw_data[idoff + 3] =
1581				le16_to_cpu(udev->descriptor.idProduct) >> 8;
1582
1583			ret = usb_cypress_load_firmware(udev, &new_fw,
1584							CYPRESS_FX2);
1585			vfree(new_fw_data);
1586			return ret;
1587		}
1588	}
1589
1590	return -EINVAL;
1591}
1592
1593/* DVB USB Driver stuff */
1594static struct dvb_usb_device_properties cxusb_medion_properties;
1595static struct dvb_usb_device_properties cxusb_bluebird_lgh064f_properties;
1596static struct dvb_usb_device_properties cxusb_bluebird_dee1601_properties;
1597static struct dvb_usb_device_properties cxusb_bluebird_lgz201_properties;
1598static struct dvb_usb_device_properties cxusb_bluebird_dtt7579_properties;
1599static struct dvb_usb_device_properties cxusb_bluebird_dualdig4_properties;
1600static struct dvb_usb_device_properties cxusb_bluebird_dualdig4_rev2_properties;
1601static struct dvb_usb_device_properties cxusb_bluebird_nano2_properties;
1602static struct dvb_usb_device_properties cxusb_bluebird_nano2_needsfirmware_properties;
1603static struct dvb_usb_device_properties cxusb_aver_a868r_properties;
1604static struct dvb_usb_device_properties cxusb_d680_dmb_properties;
1605static struct dvb_usb_device_properties cxusb_mygica_d689_properties;
1606static struct dvb_usb_device_properties cxusb_tt_ct2_4400_properties;
1607
1608static int cxusb_probe(struct usb_interface *intf,
1609		       const struct usb_device_id *id)
1610{
1611	if (0 == dvb_usb_device_init(intf, &cxusb_medion_properties,
1612				     THIS_MODULE, NULL, adapter_nr) ||
1613	    0 == dvb_usb_device_init(intf, &cxusb_bluebird_lgh064f_properties,
1614				     THIS_MODULE, NULL, adapter_nr) ||
1615	    0 == dvb_usb_device_init(intf, &cxusb_bluebird_dee1601_properties,
1616				     THIS_MODULE, NULL, adapter_nr) ||
1617	    0 == dvb_usb_device_init(intf, &cxusb_bluebird_lgz201_properties,
1618				     THIS_MODULE, NULL, adapter_nr) ||
1619	    0 == dvb_usb_device_init(intf, &cxusb_bluebird_dtt7579_properties,
1620				     THIS_MODULE, NULL, adapter_nr) ||
1621	    0 == dvb_usb_device_init(intf, &cxusb_bluebird_dualdig4_properties,
1622				     THIS_MODULE, NULL, adapter_nr) ||
1623	    0 == dvb_usb_device_init(intf, &cxusb_bluebird_nano2_properties,
1624				     THIS_MODULE, NULL, adapter_nr) ||
1625	    0 == dvb_usb_device_init(intf,
1626				&cxusb_bluebird_nano2_needsfirmware_properties,
1627				     THIS_MODULE, NULL, adapter_nr) ||
1628	    0 == dvb_usb_device_init(intf, &cxusb_aver_a868r_properties,
1629				     THIS_MODULE, NULL, adapter_nr) ||
1630	    0 == dvb_usb_device_init(intf,
1631				     &cxusb_bluebird_dualdig4_rev2_properties,
1632				     THIS_MODULE, NULL, adapter_nr) ||
1633	    0 == dvb_usb_device_init(intf, &cxusb_d680_dmb_properties,
1634				     THIS_MODULE, NULL, adapter_nr) ||
1635	    0 == dvb_usb_device_init(intf, &cxusb_mygica_d689_properties,
1636				     THIS_MODULE, NULL, adapter_nr) ||
1637	    0 == dvb_usb_device_init(intf, &cxusb_tt_ct2_4400_properties,
1638				     THIS_MODULE, NULL, adapter_nr) ||
1639	    0)
1640		return 0;
1641
1642	return -EINVAL;
1643}
1644
1645static void cxusb_disconnect(struct usb_interface *intf)
1646{
1647	struct dvb_usb_device *d = usb_get_intfdata(intf);
1648	struct cxusb_state *st = d->priv;
1649	struct i2c_client *client;
1650
1651	/* remove I2C client for CI */
1652	client = st->i2c_client_ci;
1653	if (client) {
1654		module_put(client->dev.driver->owner);
1655		i2c_unregister_device(client);
1656	}
1657
1658	/* remove I2C client for tuner */
1659	client = st->i2c_client_tuner;
1660	if (client) {
1661		module_put(client->dev.driver->owner);
1662		i2c_unregister_device(client);
1663	}
1664
1665	/* remove I2C client for demodulator */
1666	client = st->i2c_client_demod;
1667	if (client) {
1668		module_put(client->dev.driver->owner);
1669		i2c_unregister_device(client);
1670	}
1671
1672	dvb_usb_device_exit(intf);
1673}
1674
1675static struct usb_device_id cxusb_table [] = {
1676	{ USB_DEVICE(USB_VID_MEDION, USB_PID_MEDION_MD95700) },
1677	{ USB_DEVICE(USB_VID_DVICO, USB_PID_DVICO_BLUEBIRD_LG064F_COLD) },
1678	{ USB_DEVICE(USB_VID_DVICO, USB_PID_DVICO_BLUEBIRD_LG064F_WARM) },
1679	{ USB_DEVICE(USB_VID_DVICO, USB_PID_DVICO_BLUEBIRD_DUAL_1_COLD) },
1680	{ USB_DEVICE(USB_VID_DVICO, USB_PID_DVICO_BLUEBIRD_DUAL_1_WARM) },
1681	{ USB_DEVICE(USB_VID_DVICO, USB_PID_DVICO_BLUEBIRD_LGZ201_COLD) },
1682	{ USB_DEVICE(USB_VID_DVICO, USB_PID_DVICO_BLUEBIRD_LGZ201_WARM) },
1683	{ USB_DEVICE(USB_VID_DVICO, USB_PID_DVICO_BLUEBIRD_TH7579_COLD) },
1684	{ USB_DEVICE(USB_VID_DVICO, USB_PID_DVICO_BLUEBIRD_TH7579_WARM) },
1685	{ USB_DEVICE(USB_VID_DVICO, USB_PID_DIGITALNOW_BLUEBIRD_DUAL_1_COLD) },
1686	{ USB_DEVICE(USB_VID_DVICO, USB_PID_DIGITALNOW_BLUEBIRD_DUAL_1_WARM) },
1687	{ USB_DEVICE(USB_VID_DVICO, USB_PID_DVICO_BLUEBIRD_DUAL_2_COLD) },
1688	{ USB_DEVICE(USB_VID_DVICO, USB_PID_DVICO_BLUEBIRD_DUAL_2_WARM) },
1689	{ USB_DEVICE(USB_VID_DVICO, USB_PID_DVICO_BLUEBIRD_DUAL_4) },
1690	{ USB_DEVICE(USB_VID_DVICO, USB_PID_DVICO_BLUEBIRD_DVB_T_NANO_2) },
1691	{ USB_DEVICE(USB_VID_DVICO, USB_PID_DVICO_BLUEBIRD_DVB_T_NANO_2_NFW_WARM) },
1692	{ USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_A868R) },
1693	{ USB_DEVICE(USB_VID_DVICO, USB_PID_DVICO_BLUEBIRD_DUAL_4_REV_2) },
1694	{ USB_DEVICE(USB_VID_CONEXANT, USB_PID_CONEXANT_D680_DMB) },
1695	{ USB_DEVICE(USB_VID_CONEXANT, USB_PID_MYGICA_D689) },
1696	{ USB_DEVICE(USB_VID_TECHNOTREND, USB_PID_TECHNOTREND_TVSTICK_CT2_4400) },
1697	{ USB_DEVICE(USB_VID_TECHNOTREND, USB_PID_TECHNOTREND_CONNECT_CT2_4650_CI) },
1698	{}		/* Terminating entry */
1699};
1700MODULE_DEVICE_TABLE (usb, cxusb_table);
1701
1702static struct dvb_usb_device_properties cxusb_medion_properties = {
1703	.caps = DVB_USB_IS_AN_I2C_ADAPTER,
1704
1705	.usb_ctrl = CYPRESS_FX2,
1706
1707	.size_of_priv     = sizeof(struct cxusb_state),
1708
1709	.num_adapters = 1,
1710	.adapter = {
1711		{
1712		.num_frontends = 1,
1713		.fe = {{
1714			.streaming_ctrl   = cxusb_streaming_ctrl,
1715			.frontend_attach  = cxusb_cx22702_frontend_attach,
1716			.tuner_attach     = cxusb_fmd1216me_tuner_attach,
1717			/* parameter for the MPEG2-data transfer */
1718					.stream = {
1719						.type = USB_BULK,
1720				.count = 5,
1721				.endpoint = 0x02,
1722				.u = {
1723					.bulk = {
1724						.buffersize = 8192,
1725					}
1726				}
1727			},
1728		}},
1729		},
1730	},
1731	.power_ctrl       = cxusb_power_ctrl,
1732
1733	.i2c_algo         = &cxusb_i2c_algo,
1734
1735	.generic_bulk_ctrl_endpoint = 0x01,
1736
1737	.num_device_descs = 1,
1738	.devices = {
1739		{   "Medion MD95700 (MDUSBTV-HYBRID)",
1740			{ NULL },
1741			{ &cxusb_table[0], NULL },
1742		},
1743	}
1744};
1745
1746static struct dvb_usb_device_properties cxusb_bluebird_lgh064f_properties = {
1747	.caps = DVB_USB_IS_AN_I2C_ADAPTER,
1748
1749	.usb_ctrl          = DEVICE_SPECIFIC,
1750	.firmware          = "dvb-usb-bluebird-01.fw",
1751	.download_firmware = bluebird_patch_dvico_firmware_download,
1752	/* use usb alt setting 0 for EP4 transfer (dvb-t),
1753	   use usb alt setting 7 for EP2 transfer (atsc) */
1754
1755	.size_of_priv     = sizeof(struct cxusb_state),
1756
1757	.num_adapters = 1,
1758	.adapter = {
1759		{
1760		.num_frontends = 1,
1761		.fe = {{
1762			.streaming_ctrl   = cxusb_streaming_ctrl,
1763			.frontend_attach  = cxusb_lgdt3303_frontend_attach,
1764			.tuner_attach     = cxusb_lgh064f_tuner_attach,
1765
1766			/* parameter for the MPEG2-data transfer */
1767					.stream = {
1768						.type = USB_BULK,
1769				.count = 5,
1770				.endpoint = 0x02,
1771				.u = {
1772					.bulk = {
1773						.buffersize = 8192,
1774					}
1775				}
1776			},
1777		}},
1778		},
1779	},
1780
1781	.power_ctrl       = cxusb_bluebird_power_ctrl,
1782
1783	.i2c_algo         = &cxusb_i2c_algo,
1784
1785	.rc.legacy = {
1786		.rc_interval      = 100,
1787		.rc_map_table     = rc_map_dvico_portable_table,
1788		.rc_map_size      = ARRAY_SIZE(rc_map_dvico_portable_table),
1789		.rc_query         = cxusb_rc_query,
1790	},
1791
1792	.generic_bulk_ctrl_endpoint = 0x01,
1793
1794	.num_device_descs = 1,
1795	.devices = {
1796		{   "DViCO FusionHDTV5 USB Gold",
1797			{ &cxusb_table[1], NULL },
1798			{ &cxusb_table[2], NULL },
1799		},
1800	}
1801};
1802
1803static struct dvb_usb_device_properties cxusb_bluebird_dee1601_properties = {
1804	.caps = DVB_USB_IS_AN_I2C_ADAPTER,
1805
1806	.usb_ctrl          = DEVICE_SPECIFIC,
1807	.firmware          = "dvb-usb-bluebird-01.fw",
1808	.download_firmware = bluebird_patch_dvico_firmware_download,
1809	/* use usb alt setting 0 for EP4 transfer (dvb-t),
1810	   use usb alt setting 7 for EP2 transfer (atsc) */
1811
1812	.size_of_priv     = sizeof(struct cxusb_state),
1813
1814	.num_adapters = 1,
1815	.adapter = {
1816		{
1817		.num_frontends = 1,
1818		.fe = {{
1819			.streaming_ctrl   = cxusb_streaming_ctrl,
1820			.frontend_attach  = cxusb_dee1601_frontend_attach,
1821			.tuner_attach     = cxusb_dee1601_tuner_attach,
1822			/* parameter for the MPEG2-data transfer */
1823			.stream = {
1824				.type = USB_BULK,
1825				.count = 5,
1826				.endpoint = 0x04,
1827				.u = {
1828					.bulk = {
1829						.buffersize = 8192,
1830					}
1831				}
1832			},
1833		}},
1834		},
1835	},
1836
1837	.power_ctrl       = cxusb_bluebird_power_ctrl,
1838
1839	.i2c_algo         = &cxusb_i2c_algo,
1840
1841	.rc.legacy = {
1842		.rc_interval      = 150,
1843		.rc_map_table     = rc_map_dvico_mce_table,
1844		.rc_map_size      = ARRAY_SIZE(rc_map_dvico_mce_table),
1845		.rc_query         = cxusb_rc_query,
1846	},
1847
1848	.generic_bulk_ctrl_endpoint = 0x01,
1849
1850	.num_device_descs = 3,
1851	.devices = {
1852		{   "DViCO FusionHDTV DVB-T Dual USB",
1853			{ &cxusb_table[3], NULL },
1854			{ &cxusb_table[4], NULL },
1855		},
1856		{   "DigitalNow DVB-T Dual USB",
1857			{ &cxusb_table[9],  NULL },
1858			{ &cxusb_table[10], NULL },
1859		},
1860		{   "DViCO FusionHDTV DVB-T Dual Digital 2",
1861			{ &cxusb_table[11], NULL },
1862			{ &cxusb_table[12], NULL },
1863		},
1864	}
1865};
1866
1867static struct dvb_usb_device_properties cxusb_bluebird_lgz201_properties = {
1868	.caps = DVB_USB_IS_AN_I2C_ADAPTER,
1869
1870	.usb_ctrl          = DEVICE_SPECIFIC,
1871	.firmware          = "dvb-usb-bluebird-01.fw",
1872	.download_firmware = bluebird_patch_dvico_firmware_download,
1873	/* use usb alt setting 0 for EP4 transfer (dvb-t),
1874	   use usb alt setting 7 for EP2 transfer (atsc) */
1875
1876	.size_of_priv     = sizeof(struct cxusb_state),
1877
1878	.num_adapters = 2,
1879	.adapter = {
1880		{
1881		.num_frontends = 1,
1882		.fe = {{
1883			.streaming_ctrl   = cxusb_streaming_ctrl,
1884			.frontend_attach  = cxusb_mt352_frontend_attach,
1885			.tuner_attach     = cxusb_lgz201_tuner_attach,
1886
1887			/* parameter for the MPEG2-data transfer */
1888			.stream = {
1889				.type = USB_BULK,
1890				.count = 5,
1891				.endpoint = 0x04,
1892				.u = {
1893					.bulk = {
1894						.buffersize = 8192,
1895					}
1896				}
1897			},
1898		}},
1899		},
1900	},
1901	.power_ctrl       = cxusb_bluebird_power_ctrl,
1902
1903	.i2c_algo         = &cxusb_i2c_algo,
1904
1905	.rc.legacy = {
1906		.rc_interval      = 100,
1907		.rc_map_table     = rc_map_dvico_portable_table,
1908		.rc_map_size      = ARRAY_SIZE(rc_map_dvico_portable_table),
1909		.rc_query         = cxusb_rc_query,
1910	},
1911
1912	.generic_bulk_ctrl_endpoint = 0x01,
1913	.num_device_descs = 1,
1914	.devices = {
1915		{   "DViCO FusionHDTV DVB-T USB (LGZ201)",
1916			{ &cxusb_table[5], NULL },
1917			{ &cxusb_table[6], NULL },
1918		},
1919	}
1920};
1921
1922static struct dvb_usb_device_properties cxusb_bluebird_dtt7579_properties = {
1923	.caps = DVB_USB_IS_AN_I2C_ADAPTER,
1924
1925	.usb_ctrl          = DEVICE_SPECIFIC,
1926	.firmware          = "dvb-usb-bluebird-01.fw",
1927	.download_firmware = bluebird_patch_dvico_firmware_download,
1928	/* use usb alt setting 0 for EP4 transfer (dvb-t),
1929	   use usb alt setting 7 for EP2 transfer (atsc) */
1930
1931	.size_of_priv     = sizeof(struct cxusb_state),
1932
1933	.num_adapters = 1,
1934	.adapter = {
1935		{
1936		.num_frontends = 1,
1937		.fe = {{
1938			.streaming_ctrl   = cxusb_streaming_ctrl,
1939			.frontend_attach  = cxusb_mt352_frontend_attach,
1940			.tuner_attach     = cxusb_dtt7579_tuner_attach,
1941
1942			/* parameter for the MPEG2-data transfer */
1943			.stream = {
1944				.type = USB_BULK,
1945				.count = 5,
1946				.endpoint = 0x04,
1947				.u = {
1948					.bulk = {
1949						.buffersize = 8192,
1950					}
1951				}
1952			},
1953		}},
1954		},
1955	},
1956	.power_ctrl       = cxusb_bluebird_power_ctrl,
1957
1958	.i2c_algo         = &cxusb_i2c_algo,
1959
1960	.rc.legacy = {
1961		.rc_interval      = 100,
1962		.rc_map_table     = rc_map_dvico_portable_table,
1963		.rc_map_size      = ARRAY_SIZE(rc_map_dvico_portable_table),
1964		.rc_query         = cxusb_rc_query,
1965	},
1966
1967	.generic_bulk_ctrl_endpoint = 0x01,
1968
1969	.num_device_descs = 1,
1970	.devices = {
1971		{   "DViCO FusionHDTV DVB-T USB (TH7579)",
1972			{ &cxusb_table[7], NULL },
1973			{ &cxusb_table[8], NULL },
1974		},
1975	}
1976};
1977
1978static struct dvb_usb_device_properties cxusb_bluebird_dualdig4_properties = {
1979	.caps = DVB_USB_IS_AN_I2C_ADAPTER,
1980
1981	.usb_ctrl         = CYPRESS_FX2,
1982
1983	.size_of_priv     = sizeof(struct cxusb_state),
1984
1985	.num_adapters = 1,
1986	.adapter = {
1987		{
1988		.num_frontends = 1,
1989		.fe = {{
1990			.streaming_ctrl   = cxusb_streaming_ctrl,
1991			.frontend_attach  = cxusb_dualdig4_frontend_attach,
1992			.tuner_attach     = cxusb_dvico_xc3028_tuner_attach,
1993			/* parameter for the MPEG2-data transfer */
1994			.stream = {
1995				.type = USB_BULK,
1996				.count = 5,
1997				.endpoint = 0x02,
1998				.u = {
1999					.bulk = {
2000						.buffersize = 8192,
2001					}
2002				}
2003			},
2004		}},
2005		},
2006	},
2007
2008	.power_ctrl       = cxusb_power_ctrl,
2009
2010	.i2c_algo         = &cxusb_i2c_algo,
2011
2012	.generic_bulk_ctrl_endpoint = 0x01,
2013
2014	.rc.legacy = {
2015		.rc_interval      = 100,
2016		.rc_map_table     = rc_map_dvico_mce_table,
2017		.rc_map_size      = ARRAY_SIZE(rc_map_dvico_mce_table),
2018		.rc_query         = cxusb_bluebird2_rc_query,
2019	},
2020
2021	.num_device_descs = 1,
2022	.devices = {
2023		{   "DViCO FusionHDTV DVB-T Dual Digital 4",
2024			{ NULL },
2025			{ &cxusb_table[13], NULL },
2026		},
2027	}
2028};
2029
2030static struct dvb_usb_device_properties cxusb_bluebird_nano2_properties = {
2031	.caps = DVB_USB_IS_AN_I2C_ADAPTER,
2032
2033	.usb_ctrl         = CYPRESS_FX2,
2034	.identify_state   = bluebird_fx2_identify_state,
2035
2036	.size_of_priv     = sizeof(struct cxusb_state),
2037
2038	.num_adapters = 1,
2039	.adapter = {
2040		{
2041		.num_frontends = 1,
2042		.fe = {{
2043			.streaming_ctrl   = cxusb_streaming_ctrl,
2044			.frontend_attach  = cxusb_nano2_frontend_attach,
2045			.tuner_attach     = cxusb_dvico_xc3028_tuner_attach,
2046			/* parameter for the MPEG2-data transfer */
2047			.stream = {
2048				.type = USB_BULK,
2049				.count = 5,
2050				.endpoint = 0x02,
2051				.u = {
2052					.bulk = {
2053						.buffersize = 8192,
2054					}
2055				}
2056			},
2057		}},
2058		},
2059	},
2060
2061	.power_ctrl       = cxusb_nano2_power_ctrl,
2062
2063	.i2c_algo         = &cxusb_i2c_algo,
2064
2065	.generic_bulk_ctrl_endpoint = 0x01,
2066
2067	.rc.legacy = {
2068		.rc_interval      = 100,
2069		.rc_map_table     = rc_map_dvico_portable_table,
2070		.rc_map_size      = ARRAY_SIZE(rc_map_dvico_portable_table),
2071		.rc_query         = cxusb_bluebird2_rc_query,
2072	},
2073
2074	.num_device_descs = 1,
2075	.devices = {
2076		{   "DViCO FusionHDTV DVB-T NANO2",
2077			{ NULL },
2078			{ &cxusb_table[14], NULL },
2079		},
2080	}
2081};
2082
2083static struct dvb_usb_device_properties cxusb_bluebird_nano2_needsfirmware_properties = {
2084	.caps = DVB_USB_IS_AN_I2C_ADAPTER,
2085
2086	.usb_ctrl          = DEVICE_SPECIFIC,
2087	.firmware          = "dvb-usb-bluebird-02.fw",
2088	.download_firmware = bluebird_patch_dvico_firmware_download,
2089	.identify_state    = bluebird_fx2_identify_state,
2090
2091	.size_of_priv      = sizeof(struct cxusb_state),
2092
2093	.num_adapters = 1,
2094	.adapter = {
2095		{
2096		.num_frontends = 1,
2097		.fe = {{
2098			.streaming_ctrl   = cxusb_streaming_ctrl,
2099			.frontend_attach  = cxusb_nano2_frontend_attach,
2100			.tuner_attach     = cxusb_dvico_xc3028_tuner_attach,
2101			/* parameter for the MPEG2-data transfer */
2102			.stream = {
2103				.type = USB_BULK,
2104				.count = 5,
2105				.endpoint = 0x02,
2106				.u = {
2107					.bulk = {
2108						.buffersize = 8192,
2109					}
2110				}
2111			},
2112		}},
2113		},
2114	},
2115
2116	.power_ctrl       = cxusb_nano2_power_ctrl,
2117
2118	.i2c_algo         = &cxusb_i2c_algo,
2119
2120	.generic_bulk_ctrl_endpoint = 0x01,
2121
2122	.rc.legacy = {
2123		.rc_interval      = 100,
2124		.rc_map_table     = rc_map_dvico_portable_table,
2125		.rc_map_size      = ARRAY_SIZE(rc_map_dvico_portable_table),
2126		.rc_query         = cxusb_rc_query,
2127	},
2128
2129	.num_device_descs = 1,
2130	.devices = {
2131		{   "DViCO FusionHDTV DVB-T NANO2 w/o firmware",
2132			{ &cxusb_table[14], NULL },
2133			{ &cxusb_table[15], NULL },
2134		},
2135	}
2136};
2137
2138static struct dvb_usb_device_properties cxusb_aver_a868r_properties = {
2139	.caps = DVB_USB_IS_AN_I2C_ADAPTER,
2140
2141	.usb_ctrl         = CYPRESS_FX2,
2142
2143	.size_of_priv     = sizeof(struct cxusb_state),
2144
2145	.num_adapters = 1,
2146	.adapter = {
2147		{
2148		.num_frontends = 1,
2149		.fe = {{
2150			.streaming_ctrl   = cxusb_aver_streaming_ctrl,
2151			.frontend_attach  = cxusb_aver_lgdt3303_frontend_attach,
2152			.tuner_attach     = cxusb_mxl5003s_tuner_attach,
2153			/* parameter for the MPEG2-data transfer */
2154			.stream = {
2155				.type = USB_BULK,
2156				.count = 5,
2157				.endpoint = 0x04,
2158				.u = {
2159					.bulk = {
2160						.buffersize = 8192,
2161					}
2162				}
2163			},
2164		}},
2165		},
2166	},
2167	.power_ctrl       = cxusb_aver_power_ctrl,
2168
2169	.i2c_algo         = &cxusb_i2c_algo,
2170
2171	.generic_bulk_ctrl_endpoint = 0x01,
2172
2173	.num_device_descs = 1,
2174	.devices = {
2175		{   "AVerMedia AVerTVHD Volar (A868R)",
2176			{ NULL },
2177			{ &cxusb_table[16], NULL },
2178		},
2179	}
2180};
2181
2182static
2183struct dvb_usb_device_properties cxusb_bluebird_dualdig4_rev2_properties = {
2184	.caps = DVB_USB_IS_AN_I2C_ADAPTER,
2185
2186	.usb_ctrl         = CYPRESS_FX2,
2187
2188	.size_of_priv     = sizeof(struct cxusb_state),
2189
2190	.num_adapters = 1,
2191	.adapter = {
2192		{
2193		.size_of_priv    = sizeof(struct dib0700_adapter_state),
2194		.num_frontends = 1,
2195		.fe = {{
2196			.streaming_ctrl  = cxusb_streaming_ctrl,
2197			.frontend_attach = cxusb_dualdig4_rev2_frontend_attach,
2198			.tuner_attach    = cxusb_dualdig4_rev2_tuner_attach,
2199			/* parameter for the MPEG2-data transfer */
2200			.stream = {
2201				.type = USB_BULK,
2202				.count = 7,
2203				.endpoint = 0x02,
2204				.u = {
2205					.bulk = {
2206						.buffersize = 4096,
2207					}
2208				}
2209			},
2210		}},
2211		},
2212	},
2213
2214	.power_ctrl       = cxusb_bluebird_power_ctrl,
2215
2216	.i2c_algo         = &cxusb_i2c_algo,
2217
2218	.generic_bulk_ctrl_endpoint = 0x01,
2219
2220	.rc.legacy = {
2221		.rc_interval      = 100,
2222		.rc_map_table     = rc_map_dvico_mce_table,
2223		.rc_map_size      = ARRAY_SIZE(rc_map_dvico_mce_table),
2224		.rc_query         = cxusb_rc_query,
2225	},
2226
2227	.num_device_descs = 1,
2228	.devices = {
2229		{   "DViCO FusionHDTV DVB-T Dual Digital 4 (rev 2)",
2230			{ NULL },
2231			{ &cxusb_table[17], NULL },
2232		},
2233	}
2234};
2235
2236static struct dvb_usb_device_properties cxusb_d680_dmb_properties = {
2237	.caps = DVB_USB_IS_AN_I2C_ADAPTER,
2238
2239	.usb_ctrl         = CYPRESS_FX2,
2240
2241	.size_of_priv     = sizeof(struct cxusb_state),
2242
2243	.num_adapters = 1,
2244	.adapter = {
2245		{
2246		.num_frontends = 1,
2247		.fe = {{
2248			.streaming_ctrl   = cxusb_d680_dmb_streaming_ctrl,
2249			.frontend_attach  = cxusb_d680_dmb_frontend_attach,
2250			.tuner_attach     = cxusb_d680_dmb_tuner_attach,
2251
2252			/* parameter for the MPEG2-data transfer */
2253			.stream = {
2254				.type = USB_BULK,
2255				.count = 5,
2256				.endpoint = 0x02,
2257				.u = {
2258					.bulk = {
2259						.buffersize = 8192,
2260					}
2261				}
2262			},
2263		}},
2264		},
2265	},
2266
2267	.power_ctrl       = cxusb_d680_dmb_power_ctrl,
2268
2269	.i2c_algo         = &cxusb_i2c_algo,
2270
2271	.generic_bulk_ctrl_endpoint = 0x01,
2272
2273	.rc.legacy = {
2274		.rc_interval      = 100,
2275		.rc_map_table     = rc_map_d680_dmb_table,
2276		.rc_map_size      = ARRAY_SIZE(rc_map_d680_dmb_table),
2277		.rc_query         = cxusb_d680_dmb_rc_query,
2278	},
2279
2280	.num_device_descs = 1,
2281	.devices = {
2282		{
2283			"Conexant DMB-TH Stick",
2284			{ NULL },
2285			{ &cxusb_table[18], NULL },
2286		},
2287	}
2288};
2289
2290static struct dvb_usb_device_properties cxusb_mygica_d689_properties = {
2291	.caps = DVB_USB_IS_AN_I2C_ADAPTER,
2292
2293	.usb_ctrl         = CYPRESS_FX2,
2294
2295	.size_of_priv     = sizeof(struct cxusb_state),
2296
2297	.num_adapters = 1,
2298	.adapter = {
2299		{
2300		.num_frontends = 1,
2301		.fe = {{
2302			.streaming_ctrl   = cxusb_d680_dmb_streaming_ctrl,
2303			.frontend_attach  = cxusb_mygica_d689_frontend_attach,
2304			.tuner_attach     = cxusb_mygica_d689_tuner_attach,
2305
2306			/* parameter for the MPEG2-data transfer */
2307			.stream = {
2308				.type = USB_BULK,
2309				.count = 5,
2310				.endpoint = 0x02,
2311				.u = {
2312					.bulk = {
2313						.buffersize = 8192,
2314					}
2315				}
2316			},
2317		}},
2318		},
2319	},
2320
2321	.power_ctrl       = cxusb_d680_dmb_power_ctrl,
2322
2323	.i2c_algo         = &cxusb_i2c_algo,
2324
2325	.generic_bulk_ctrl_endpoint = 0x01,
2326
2327	.rc.legacy = {
2328		.rc_interval      = 100,
2329		.rc_map_table     = rc_map_d680_dmb_table,
2330		.rc_map_size      = ARRAY_SIZE(rc_map_d680_dmb_table),
2331		.rc_query         = cxusb_d680_dmb_rc_query,
2332	},
2333
2334	.num_device_descs = 1,
2335	.devices = {
2336		{
2337			"Mygica D689 DMB-TH",
2338			{ NULL },
2339			{ &cxusb_table[19], NULL },
2340		},
2341	}
2342};
2343
2344static struct dvb_usb_device_properties cxusb_tt_ct2_4400_properties = {
2345	.caps = DVB_USB_IS_AN_I2C_ADAPTER,
2346
2347	.usb_ctrl         = CYPRESS_FX2,
2348
2349	.size_of_priv     = sizeof(struct cxusb_state),
2350
2351	.num_adapters = 1,
2352	.read_mac_address = cxusb_tt_ct2_4400_read_mac_address,
2353
2354	.adapter = {
2355		{
2356		.num_frontends = 1,
2357		.fe = {{
2358			.streaming_ctrl   = cxusb_streaming_ctrl,
2359			/* both frontend and tuner attached in the
2360			   same function */
2361			.frontend_attach  = cxusb_tt_ct2_4400_attach,
2362
2363			/* parameter for the MPEG2-data transfer */
2364			.stream = {
2365				.type = USB_BULK,
2366				.count = 8,
2367				.endpoint = 0x82,
2368				.u = {
2369					.bulk = {
2370						.buffersize = 4096,
2371					}
2372				}
2373			},
2374		} },
2375		},
2376	},
2377
2378	.i2c_algo = &cxusb_i2c_algo,
2379	.generic_bulk_ctrl_endpoint = 0x01,
2380	.generic_bulk_ctrl_endpoint_response = 0x81,
2381
2382	.rc.core = {
2383		.rc_codes       = RC_MAP_TT_1500,
2384		.allowed_protos = RC_BIT_RC5,
2385		.rc_query       = cxusb_tt_ct2_4400_rc_query,
2386		.rc_interval    = 150,
2387	},
2388
2389	.num_device_descs = 2,
2390	.devices = {
2391		{
2392			"TechnoTrend TVStick CT2-4400",
2393			{ NULL },
2394			{ &cxusb_table[20], NULL },
2395		},
2396		{
2397			"TechnoTrend TT-connect CT2-4650 CI",
2398			{ NULL },
2399			{ &cxusb_table[21], NULL },
2400		},
2401	}
2402};
2403
2404static struct usb_driver cxusb_driver = {
2405	.name		= "dvb_usb_cxusb",
2406	.probe		= cxusb_probe,
2407	.disconnect     = cxusb_disconnect,
2408	.id_table	= cxusb_table,
2409};
2410
2411module_usb_driver(cxusb_driver);
2412
2413MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@desy.de>");
2414MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
2415MODULE_AUTHOR("Chris Pascoe <c.pascoe@itee.uq.edu.au>");
2416MODULE_DESCRIPTION("Driver for Conexant USB2.0 hybrid reference design");
2417MODULE_VERSION("1.0-alpha");
2418MODULE_LICENSE("GPL");
2419