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