1 /*
2  DVB device driver for em28xx
3 
4  (c) 2008-2011 Mauro Carvalho Chehab <mchehab@infradead.org>
5 
6  (c) 2008 Devin Heitmueller <devin.heitmueller@gmail.com>
7 	- Fixes for the driver to properly work with HVR-950
8 	- Fixes for the driver to properly work with Pinnacle PCTV HD Pro Stick
9 	- Fixes for the driver to properly work with AMD ATI TV Wonder HD 600
10 
11  (c) 2008 Aidan Thornton <makosoft@googlemail.com>
12 
13  (c) 2012 Frank Schäfer <fschaefer.oss@googlemail.com>
14 
15  Based on cx88-dvb, saa7134-dvb and videobuf-dvb originally written by:
16 	(c) 2004, 2005 Chris Pascoe <c.pascoe@itee.uq.edu.au>
17 	(c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
18 
19  This program is free software; you can redistribute it and/or modify
20  it under the terms of the GNU General Public License as published by
21  the Free Software Foundation; either version 2 of the License.
22  */
23 
24 #include "em28xx.h"
25 
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/usb.h>
29 
30 #include <media/v4l2-common.h>
31 #include <dvb_demux.h>
32 #include <dvb_net.h>
33 #include <dmxdev.h>
34 #include <media/tuner.h>
35 #include "tuner-simple.h"
36 #include <linux/gpio.h>
37 
38 #include "lgdt330x.h"
39 #include "lgdt3305.h"
40 #include "zl10353.h"
41 #include "s5h1409.h"
42 #include "mt2060.h"
43 #include "mt352.h"
44 #include "mt352_priv.h" /* FIXME */
45 #include "tda1002x.h"
46 #include "drx39xyj/drx39xxj.h"
47 #include "tda18271.h"
48 #include "s921.h"
49 #include "drxd.h"
50 #include "cxd2820r.h"
51 #include "tda18271c2dd.h"
52 #include "drxk.h"
53 #include "tda10071.h"
54 #include "tda18212.h"
55 #include "a8293.h"
56 #include "qt1010.h"
57 #include "mb86a20s.h"
58 #include "m88ds3103.h"
59 #include "ts2020.h"
60 #include "si2168.h"
61 #include "si2157.h"
62 #include "tc90522.h"
63 #include "qm1d1c0042.h"
64 
65 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
66 MODULE_LICENSE("GPL");
67 MODULE_DESCRIPTION(DRIVER_DESC " - digital TV interface");
68 MODULE_VERSION(EM28XX_VERSION);
69 
70 static unsigned int debug;
71 module_param(debug, int, 0644);
72 MODULE_PARM_DESC(debug, "enable debug messages [dvb]");
73 
74 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
75 
76 #define dprintk(level, fmt, arg...) do {				\
77 	if (debug >= level)						\
78 		dev_printk(KERN_DEBUG, &dev->intf->dev,			\
79 			   "dvb: " fmt, ## arg);			\
80 } while (0)
81 
82 struct em28xx_dvb {
83 	struct dvb_frontend        *fe[2];
84 
85 	/* feed count management */
86 	struct mutex               lock;
87 	int                        nfeeds;
88 
89 	/* general boilerplate stuff */
90 	struct dvb_adapter         adapter;
91 	struct dvb_demux           demux;
92 	struct dmxdev              dmxdev;
93 	struct dmx_frontend        fe_hw;
94 	struct dmx_frontend        fe_mem;
95 	struct dvb_net             net;
96 
97 	/* Due to DRX-K - probably need changes */
98 	int (*gate_ctrl)(struct dvb_frontend *, int);
99 	struct semaphore      pll_mutex;
100 	bool			dont_attach_fe1;
101 	int			lna_gpio;
102 	struct i2c_client	*i2c_client_demod;
103 	struct i2c_client	*i2c_client_tuner;
104 	struct i2c_client	*i2c_client_sec;
105 };
106 
107 static inline void print_err_status(struct em28xx *dev,
108 				    int packet, int status)
109 {
110 	char *errmsg = "Unknown";
111 
112 	switch (status) {
113 	case -ENOENT:
114 		errmsg = "unlinked synchronuously";
115 		break;
116 	case -ECONNRESET:
117 		errmsg = "unlinked asynchronuously";
118 		break;
119 	case -ENOSR:
120 		errmsg = "Buffer error (overrun)";
121 		break;
122 	case -EPIPE:
123 		errmsg = "Stalled (device not responding)";
124 		break;
125 	case -EOVERFLOW:
126 		errmsg = "Babble (bad cable?)";
127 		break;
128 	case -EPROTO:
129 		errmsg = "Bit-stuff error (bad cable?)";
130 		break;
131 	case -EILSEQ:
132 		errmsg = "CRC/Timeout (could be anything)";
133 		break;
134 	case -ETIME:
135 		errmsg = "Device does not respond";
136 		break;
137 	}
138 	if (packet < 0) {
139 		dprintk(1, "URB status %d [%s].\n", status, errmsg);
140 	} else {
141 		dprintk(1, "URB packet %d, status %d [%s].\n",
142 			packet, status, errmsg);
143 	}
144 }
145 
146 static inline int em28xx_dvb_urb_data_copy(struct em28xx *dev, struct urb *urb)
147 {
148 	int xfer_bulk, num_packets, i;
149 
150 	if (!dev)
151 		return 0;
152 
153 	if (dev->disconnected)
154 		return 0;
155 
156 	if (urb->status < 0)
157 		print_err_status(dev, -1, urb->status);
158 
159 	xfer_bulk = usb_pipebulk(urb->pipe);
160 
161 	if (xfer_bulk) /* bulk */
162 		num_packets = 1;
163 	else /* isoc */
164 		num_packets = urb->number_of_packets;
165 
166 	for (i = 0; i < num_packets; i++) {
167 		if (xfer_bulk) {
168 			if (urb->status < 0) {
169 				print_err_status(dev, i, urb->status);
170 				if (urb->status != -EPROTO)
171 					continue;
172 			}
173 			if (!urb->actual_length)
174 				continue;
175 			dvb_dmx_swfilter(&dev->dvb->demux, urb->transfer_buffer,
176 					 urb->actual_length);
177 		} else {
178 			if (urb->iso_frame_desc[i].status < 0) {
179 				print_err_status(dev, i,
180 						 urb->iso_frame_desc[i].status);
181 				if (urb->iso_frame_desc[i].status != -EPROTO)
182 					continue;
183 			}
184 			if (!urb->iso_frame_desc[i].actual_length)
185 				continue;
186 			dvb_dmx_swfilter(&dev->dvb->demux,
187 					 urb->transfer_buffer +
188 					 urb->iso_frame_desc[i].offset,
189 					 urb->iso_frame_desc[i].actual_length);
190 		}
191 	}
192 
193 	return 0;
194 }
195 
196 static int em28xx_start_streaming(struct em28xx_dvb *dvb)
197 {
198 	int rc;
199 	struct em28xx_i2c_bus *i2c_bus = dvb->adapter.priv;
200 	struct em28xx *dev = i2c_bus->dev;
201 	struct usb_device *udev = interface_to_usbdev(dev->intf);
202 	int dvb_max_packet_size, packet_multiplier, dvb_alt;
203 
204 	if (dev->dvb_xfer_bulk) {
205 		if (!dev->dvb_ep_bulk)
206 			return -ENODEV;
207 		dvb_max_packet_size = 512; /* USB 2.0 spec */
208 		packet_multiplier = EM28XX_DVB_BULK_PACKET_MULTIPLIER;
209 		dvb_alt = 0;
210 	} else { /* isoc */
211 		if (!dev->dvb_ep_isoc)
212 			return -ENODEV;
213 		dvb_max_packet_size = dev->dvb_max_pkt_size_isoc;
214 		if (dvb_max_packet_size < 0)
215 			return dvb_max_packet_size;
216 		packet_multiplier = EM28XX_DVB_NUM_ISOC_PACKETS;
217 		dvb_alt = dev->dvb_alt_isoc;
218 	}
219 
220 	usb_set_interface(udev, dev->ifnum, dvb_alt);
221 	rc = em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
222 	if (rc < 0)
223 		return rc;
224 
225 	dprintk(1, "Using %d buffers each with %d x %d bytes, alternate %d\n",
226 		EM28XX_DVB_NUM_BUFS,
227 		packet_multiplier,
228 		dvb_max_packet_size, dvb_alt);
229 
230 	return em28xx_init_usb_xfer(dev, EM28XX_DIGITAL_MODE,
231 				    dev->dvb_xfer_bulk,
232 				    EM28XX_DVB_NUM_BUFS,
233 				    dvb_max_packet_size,
234 				    packet_multiplier,
235 				    em28xx_dvb_urb_data_copy);
236 }
237 
238 static int em28xx_stop_streaming(struct em28xx_dvb *dvb)
239 {
240 	struct em28xx_i2c_bus *i2c_bus = dvb->adapter.priv;
241 	struct em28xx *dev = i2c_bus->dev;
242 
243 	em28xx_stop_urbs(dev);
244 
245 	return 0;
246 }
247 
248 static int em28xx_start_feed(struct dvb_demux_feed *feed)
249 {
250 	struct dvb_demux *demux  = feed->demux;
251 	struct em28xx_dvb *dvb = demux->priv;
252 	int rc, ret;
253 
254 	if (!demux->dmx.frontend)
255 		return -EINVAL;
256 
257 	mutex_lock(&dvb->lock);
258 	dvb->nfeeds++;
259 	rc = dvb->nfeeds;
260 
261 	if (dvb->nfeeds == 1) {
262 		ret = em28xx_start_streaming(dvb);
263 		if (ret < 0)
264 			rc = ret;
265 	}
266 
267 	mutex_unlock(&dvb->lock);
268 	return rc;
269 }
270 
271 static int em28xx_stop_feed(struct dvb_demux_feed *feed)
272 {
273 	struct dvb_demux *demux  = feed->demux;
274 	struct em28xx_dvb *dvb = demux->priv;
275 	int err = 0;
276 
277 	mutex_lock(&dvb->lock);
278 	dvb->nfeeds--;
279 
280 	if (0 == dvb->nfeeds)
281 		err = em28xx_stop_streaming(dvb);
282 
283 	mutex_unlock(&dvb->lock);
284 	return err;
285 }
286 
287 
288 /* ------------------------------------------------------------------ */
289 static int em28xx_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
290 {
291 	struct em28xx_i2c_bus *i2c_bus = fe->dvb->priv;
292 	struct em28xx *dev = i2c_bus->dev;
293 
294 	if (acquire)
295 		return em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
296 	else
297 		return em28xx_set_mode(dev, EM28XX_SUSPEND);
298 }
299 
300 /* ------------------------------------------------------------------ */
301 
302 static struct lgdt330x_config em2880_lgdt3303_dev = {
303 	.demod_address = 0x0e,
304 	.demod_chip = LGDT3303,
305 };
306 
307 static struct lgdt3305_config em2870_lgdt3304_dev = {
308 	.i2c_addr           = 0x0e,
309 	.demod_chip         = LGDT3304,
310 	.spectral_inversion = 1,
311 	.deny_i2c_rptr      = 1,
312 	.mpeg_mode          = LGDT3305_MPEG_PARALLEL,
313 	.tpclk_edge         = LGDT3305_TPCLK_FALLING_EDGE,
314 	.tpvalid_polarity   = LGDT3305_TP_VALID_HIGH,
315 	.vsb_if_khz         = 3250,
316 	.qam_if_khz         = 4000,
317 };
318 
319 static struct lgdt3305_config em2874_lgdt3305_dev = {
320 	.i2c_addr           = 0x0e,
321 	.demod_chip         = LGDT3305,
322 	.spectral_inversion = 1,
323 	.deny_i2c_rptr      = 0,
324 	.mpeg_mode          = LGDT3305_MPEG_SERIAL,
325 	.tpclk_edge         = LGDT3305_TPCLK_FALLING_EDGE,
326 	.tpvalid_polarity   = LGDT3305_TP_VALID_HIGH,
327 	.vsb_if_khz         = 3250,
328 	.qam_if_khz         = 4000,
329 };
330 
331 static struct lgdt3305_config em2874_lgdt3305_nogate_dev = {
332 	.i2c_addr           = 0x0e,
333 	.demod_chip         = LGDT3305,
334 	.spectral_inversion = 1,
335 	.deny_i2c_rptr      = 1,
336 	.mpeg_mode          = LGDT3305_MPEG_SERIAL,
337 	.tpclk_edge         = LGDT3305_TPCLK_FALLING_EDGE,
338 	.tpvalid_polarity   = LGDT3305_TP_VALID_HIGH,
339 	.vsb_if_khz         = 3600,
340 	.qam_if_khz         = 3600,
341 };
342 
343 static struct s921_config sharp_isdbt = {
344 	.demod_address = 0x30 >> 1
345 };
346 
347 static struct zl10353_config em28xx_zl10353_with_xc3028 = {
348 	.demod_address = (0x1e >> 1),
349 	.no_tuner = 1,
350 	.parallel_ts = 1,
351 	.if2 = 45600,
352 };
353 
354 static struct s5h1409_config em28xx_s5h1409_with_xc3028 = {
355 	.demod_address = 0x32 >> 1,
356 	.output_mode   = S5H1409_PARALLEL_OUTPUT,
357 	.gpio          = S5H1409_GPIO_OFF,
358 	.inversion     = S5H1409_INVERSION_OFF,
359 	.status_mode   = S5H1409_DEMODLOCKING,
360 	.mpeg_timing   = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK
361 };
362 
363 static struct tda18271_std_map kworld_a340_std_map = {
364 	.atsc_6   = { .if_freq = 3250, .agc_mode = 3, .std = 0,
365 		      .if_lvl = 1, .rfagc_top = 0x37, },
366 	.qam_6    = { .if_freq = 4000, .agc_mode = 3, .std = 1,
367 		      .if_lvl = 1, .rfagc_top = 0x37, },
368 };
369 
370 static struct tda18271_config kworld_a340_config = {
371 	.std_map           = &kworld_a340_std_map,
372 };
373 
374 static struct tda18271_config kworld_ub435q_v2_config = {
375 	.std_map	= &kworld_a340_std_map,
376 	.gate		= TDA18271_GATE_DIGITAL,
377 };
378 
379 static struct tda18212_config kworld_ub435q_v3_config = {
380 	.if_atsc_vsb	= 3600,
381 	.if_atsc_qam	= 3600,
382 };
383 
384 static struct zl10353_config em28xx_zl10353_xc3028_no_i2c_gate = {
385 	.demod_address = (0x1e >> 1),
386 	.no_tuner = 1,
387 	.disable_i2c_gate_ctrl = 1,
388 	.parallel_ts = 1,
389 	.if2 = 45600,
390 };
391 
392 static struct drxd_config em28xx_drxd = {
393 	.demod_address = 0x70,
394 	.demod_revision = 0xa2,
395 	.pll_type = DRXD_PLL_NONE,
396 	.clock = 12000,
397 	.insert_rs_byte = 1,
398 	.IF = 42800000,
399 	.disable_i2c_gate_ctrl = 1,
400 };
401 
402 static struct drxk_config terratec_h5_drxk = {
403 	.adr = 0x29,
404 	.single_master = 1,
405 	.no_i2c_bridge = 1,
406 	.microcode_name = "dvb-usb-terratec-h5-drxk.fw",
407 	.qam_demod_parameter_count = 2,
408 };
409 
410 static struct drxk_config hauppauge_930c_drxk = {
411 	.adr = 0x29,
412 	.single_master = 1,
413 	.no_i2c_bridge = 1,
414 	.microcode_name = "dvb-usb-hauppauge-hvr930c-drxk.fw",
415 	.chunk_size = 56,
416 	.qam_demod_parameter_count = 2,
417 };
418 
419 static struct drxk_config terratec_htc_stick_drxk = {
420 	.adr = 0x29,
421 	.single_master = 1,
422 	.no_i2c_bridge = 1,
423 	.microcode_name = "dvb-usb-terratec-htc-stick-drxk.fw",
424 	.chunk_size = 54,
425 	.qam_demod_parameter_count = 2,
426 	/* Required for the antenna_gpio to disable LNA. */
427 	.antenna_dvbt = true,
428 	/* The windows driver uses the same. This will disable LNA. */
429 	.antenna_gpio = 0x6,
430 };
431 
432 static struct drxk_config maxmedia_ub425_tc_drxk = {
433 	.adr = 0x29,
434 	.single_master = 1,
435 	.no_i2c_bridge = 1,
436 	.microcode_name = "dvb-demod-drxk-01.fw",
437 	.chunk_size = 62,
438 	.qam_demod_parameter_count = 2,
439 };
440 
441 static struct drxk_config pctv_520e_drxk = {
442 	.adr = 0x29,
443 	.single_master = 1,
444 	.microcode_name = "dvb-demod-drxk-pctv.fw",
445 	.qam_demod_parameter_count = 2,
446 	.chunk_size = 58,
447 	.antenna_dvbt = true, /* disable LNA */
448 	.antenna_gpio = (1 << 2), /* disable LNA */
449 };
450 
451 static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
452 {
453 	struct em28xx_dvb *dvb = fe->sec_priv;
454 	int status;
455 
456 	if (!dvb)
457 		return -EINVAL;
458 
459 	if (enable) {
460 		down(&dvb->pll_mutex);
461 		status = dvb->gate_ctrl(fe, 1);
462 	} else {
463 		status = dvb->gate_ctrl(fe, 0);
464 		up(&dvb->pll_mutex);
465 	}
466 	return status;
467 }
468 
469 static void hauppauge_hvr930c_init(struct em28xx *dev)
470 {
471 	int i;
472 
473 	struct em28xx_reg_seq hauppauge_hvr930c_init[] = {
474 		{EM2874_R80_GPIO_P0_CTRL,	0xff,	0xff,	0x65},
475 		{EM2874_R80_GPIO_P0_CTRL,	0xfb,	0xff,	0x32},
476 		{EM2874_R80_GPIO_P0_CTRL,	0xff,	0xff,	0xb8},
477 		{	-1,			-1,	-1,	-1},
478 	};
479 	struct em28xx_reg_seq hauppauge_hvr930c_end[] = {
480 		{EM2874_R80_GPIO_P0_CTRL,	0xef,	0xff,	0x01},
481 		{EM2874_R80_GPIO_P0_CTRL,	0xaf,	0xff,	0x65},
482 		{EM2874_R80_GPIO_P0_CTRL,	0xef,	0xff,	0x76},
483 		{EM2874_R80_GPIO_P0_CTRL,	0xef,	0xff,	0x01},
484 		{EM2874_R80_GPIO_P0_CTRL,	0xcf,	0xff,	0x0b},
485 		{EM2874_R80_GPIO_P0_CTRL,	0xef,	0xff,	0x40},
486 
487 		{EM2874_R80_GPIO_P0_CTRL,	0xcf,	0xff,	0x65},
488 		{EM2874_R80_GPIO_P0_CTRL,	0xef,	0xff,	0x65},
489 		{EM2874_R80_GPIO_P0_CTRL,	0xcf,	0xff,	0x0b},
490 		{EM2874_R80_GPIO_P0_CTRL,	0xef,	0xff,	0x65},
491 
492 		{	-1,			-1,	-1,	-1},
493 	};
494 
495 	struct {
496 		unsigned char r[4];
497 		int len;
498 	} regs[] = {
499 		{{ 0x06, 0x02, 0x00, 0x31 }, 4},
500 		{{ 0x01, 0x02 }, 2},
501 		{{ 0x01, 0x02, 0x00, 0xc6 }, 4},
502 		{{ 0x01, 0x00 }, 2},
503 		{{ 0x01, 0x00, 0xff, 0xaf }, 4},
504 		{{ 0x01, 0x00, 0x03, 0xa0 }, 4},
505 		{{ 0x01, 0x00 }, 2},
506 		{{ 0x01, 0x00, 0x73, 0xaf }, 4},
507 		{{ 0x04, 0x00 }, 2},
508 		{{ 0x00, 0x04 }, 2},
509 		{{ 0x00, 0x04, 0x00, 0x0a }, 4},
510 		{{ 0x04, 0x14 }, 2},
511 		{{ 0x04, 0x14, 0x00, 0x00 }, 4},
512 	};
513 
514 	em28xx_gpio_set(dev, hauppauge_hvr930c_init);
515 	em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
516 	msleep(10);
517 	em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
518 	msleep(10);
519 
520 	dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1;
521 
522 	for (i = 0; i < ARRAY_SIZE(regs); i++)
523 		i2c_master_send(&dev->i2c_client[dev->def_i2c_bus], regs[i].r, regs[i].len);
524 	em28xx_gpio_set(dev, hauppauge_hvr930c_end);
525 
526 	msleep(100);
527 
528 	em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
529 	msleep(30);
530 
531 	em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x45);
532 	msleep(10);
533 
534 }
535 
536 static void terratec_h5_init(struct em28xx *dev)
537 {
538 	int i;
539 	struct em28xx_reg_seq terratec_h5_init[] = {
540 		{EM2820_R08_GPIO_CTRL,		0xff,	0xff,	10},
541 		{EM2874_R80_GPIO_P0_CTRL,	0xf6,	0xff,	100},
542 		{EM2874_R80_GPIO_P0_CTRL,	0xf2,	0xff,	50},
543 		{EM2874_R80_GPIO_P0_CTRL,	0xf6,	0xff,	100},
544 		{	-1,			-1,	-1,	-1},
545 	};
546 	struct em28xx_reg_seq terratec_h5_end[] = {
547 		{EM2874_R80_GPIO_P0_CTRL,	0xe6,	0xff,	100},
548 		{EM2874_R80_GPIO_P0_CTRL,	0xa6,	0xff,	50},
549 		{EM2874_R80_GPIO_P0_CTRL,	0xe6,	0xff,	100},
550 		{	-1,			-1,	-1,	-1},
551 	};
552 	struct {
553 		unsigned char r[4];
554 		int len;
555 	} regs[] = {
556 		{{ 0x06, 0x02, 0x00, 0x31 }, 4},
557 		{{ 0x01, 0x02 }, 2},
558 		{{ 0x01, 0x02, 0x00, 0xc6 }, 4},
559 		{{ 0x01, 0x00 }, 2},
560 		{{ 0x01, 0x00, 0xff, 0xaf }, 4},
561 		{{ 0x01, 0x00, 0x03, 0xa0 }, 4},
562 		{{ 0x01, 0x00 }, 2},
563 		{{ 0x01, 0x00, 0x73, 0xaf }, 4},
564 		{{ 0x04, 0x00 }, 2},
565 		{{ 0x00, 0x04 }, 2},
566 		{{ 0x00, 0x04, 0x00, 0x0a }, 4},
567 		{{ 0x04, 0x14 }, 2},
568 		{{ 0x04, 0x14, 0x00, 0x00 }, 4},
569 	};
570 
571 	em28xx_gpio_set(dev, terratec_h5_init);
572 	em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
573 	msleep(10);
574 	em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x45);
575 	msleep(10);
576 
577 	dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1;
578 
579 	for (i = 0; i < ARRAY_SIZE(regs); i++)
580 		i2c_master_send(&dev->i2c_client[dev->def_i2c_bus], regs[i].r, regs[i].len);
581 	em28xx_gpio_set(dev, terratec_h5_end);
582 };
583 
584 static void terratec_htc_stick_init(struct em28xx *dev)
585 {
586 	int i;
587 
588 	/*
589 	 * GPIO configuration:
590 	 * 0xff: unknown (does not affect DVB-T).
591 	 * 0xf6: DRX-K (demodulator).
592 	 * 0xe6: unknown (does not affect DVB-T).
593 	 * 0xb6: unknown (does not affect DVB-T).
594 	 */
595 	struct em28xx_reg_seq terratec_htc_stick_init[] = {
596 		{EM2820_R08_GPIO_CTRL,		0xff,	0xff,	10},
597 		{EM2874_R80_GPIO_P0_CTRL,	0xf6,	0xff,	100},
598 		{EM2874_R80_GPIO_P0_CTRL,	0xe6,	0xff,	50},
599 		{EM2874_R80_GPIO_P0_CTRL,	0xf6,	0xff,	100},
600 		{	-1,			-1,	-1,	-1},
601 	};
602 	struct em28xx_reg_seq terratec_htc_stick_end[] = {
603 		{EM2874_R80_GPIO_P0_CTRL,	0xb6,	0xff,	100},
604 		{EM2874_R80_GPIO_P0_CTRL,	0xf6,	0xff,	50},
605 		{	-1,			-1,	-1,	-1},
606 	};
607 
608 	/*
609 	 * Init the analog decoder (not yet supported), but
610 	 * it's probably still a good idea.
611 	 */
612 	struct {
613 		unsigned char r[4];
614 		int len;
615 	} regs[] = {
616 		{{ 0x06, 0x02, 0x00, 0x31 }, 4},
617 		{{ 0x01, 0x02 }, 2},
618 		{{ 0x01, 0x02, 0x00, 0xc6 }, 4},
619 		{{ 0x01, 0x00 }, 2},
620 		{{ 0x01, 0x00, 0xff, 0xaf }, 4},
621 	};
622 
623 	em28xx_gpio_set(dev, terratec_htc_stick_init);
624 
625 	em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
626 	msleep(10);
627 	em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
628 	msleep(10);
629 
630 	dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1;
631 
632 	for (i = 0; i < ARRAY_SIZE(regs); i++)
633 		i2c_master_send(&dev->i2c_client[dev->def_i2c_bus], regs[i].r, regs[i].len);
634 
635 	em28xx_gpio_set(dev, terratec_htc_stick_end);
636 };
637 
638 static void terratec_htc_usb_xs_init(struct em28xx *dev)
639 {
640 	int i;
641 
642 	struct em28xx_reg_seq terratec_htc_usb_xs_init[] = {
643 		{EM2820_R08_GPIO_CTRL,		0xff,	0xff,	10},
644 		{EM2874_R80_GPIO_P0_CTRL,	0xb2,	0xff,	100},
645 		{EM2874_R80_GPIO_P0_CTRL,	0xb2,	0xff,	50},
646 		{EM2874_R80_GPIO_P0_CTRL,	0xb6,	0xff,	100},
647 		{	-1,			-1,	-1,	-1},
648 	};
649 	struct em28xx_reg_seq terratec_htc_usb_xs_end[] = {
650 		{EM2874_R80_GPIO_P0_CTRL,	0xa6,	0xff,	100},
651 		{EM2874_R80_GPIO_P0_CTRL,	0xa6,	0xff,	50},
652 		{EM2874_R80_GPIO_P0_CTRL,	0xe6,	0xff,	100},
653 		{	-1,			-1,	-1,	-1},
654 	};
655 
656 	/*
657 	 * Init the analog decoder (not yet supported), but
658 	 * it's probably still a good idea.
659 	 */
660 	struct {
661 		unsigned char r[4];
662 		int len;
663 	} regs[] = {
664 		{{ 0x06, 0x02, 0x00, 0x31 }, 4},
665 		{{ 0x01, 0x02 }, 2},
666 		{{ 0x01, 0x02, 0x00, 0xc6 }, 4},
667 		{{ 0x01, 0x00 }, 2},
668 		{{ 0x01, 0x00, 0xff, 0xaf }, 4},
669 		{{ 0x01, 0x00, 0x03, 0xa0 }, 4},
670 		{{ 0x01, 0x00 }, 2},
671 		{{ 0x01, 0x00, 0x73, 0xaf }, 4},
672 		{{ 0x04, 0x00 }, 2},
673 		{{ 0x00, 0x04 }, 2},
674 		{{ 0x00, 0x04, 0x00, 0x0a }, 4},
675 		{{ 0x04, 0x14 }, 2},
676 		{{ 0x04, 0x14, 0x00, 0x00 }, 4},
677 	};
678 
679 	em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
680 
681 	em28xx_gpio_set(dev, terratec_htc_usb_xs_init);
682 
683 	em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
684 	msleep(10);
685 	em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
686 	msleep(10);
687 
688 	dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1;
689 
690 	for (i = 0; i < ARRAY_SIZE(regs); i++)
691 		i2c_master_send(&dev->i2c_client[dev->def_i2c_bus], regs[i].r, regs[i].len);
692 
693 	em28xx_gpio_set(dev, terratec_htc_usb_xs_end);
694 };
695 
696 static void pctv_520e_init(struct em28xx *dev)
697 {
698 	/*
699 	 * Init AVF4910B analog decoder. Looks like I2C traffic to
700 	 * digital demodulator and tuner are routed via AVF4910B.
701 	 */
702 	int i;
703 	struct {
704 		unsigned char r[4];
705 		int len;
706 	} regs[] = {
707 		{{ 0x06, 0x02, 0x00, 0x31 }, 4},
708 		{{ 0x01, 0x02 }, 2},
709 		{{ 0x01, 0x02, 0x00, 0xc6 }, 4},
710 		{{ 0x01, 0x00 }, 2},
711 		{{ 0x01, 0x00, 0xff, 0xaf }, 4},
712 		{{ 0x01, 0x00, 0x03, 0xa0 }, 4},
713 		{{ 0x01, 0x00 }, 2},
714 		{{ 0x01, 0x00, 0x73, 0xaf }, 4},
715 	};
716 
717 	dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1; /* 0x41 */
718 
719 	for (i = 0; i < ARRAY_SIZE(regs); i++)
720 		i2c_master_send(&dev->i2c_client[dev->def_i2c_bus], regs[i].r, regs[i].len);
721 };
722 
723 static int em28xx_pctv_290e_set_lna(struct dvb_frontend *fe)
724 {
725 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
726 	struct em28xx_i2c_bus *i2c_bus = fe->dvb->priv;
727 	struct em28xx *dev = i2c_bus->dev;
728 #ifdef CONFIG_GPIOLIB
729 	struct em28xx_dvb *dvb = dev->dvb;
730 	int ret;
731 	unsigned long flags;
732 
733 	if (c->lna == 1)
734 		flags = GPIOF_OUT_INIT_HIGH; /* enable LNA */
735 	else
736 		flags = GPIOF_OUT_INIT_LOW; /* disable LNA */
737 
738 	ret = gpio_request_one(dvb->lna_gpio, flags, NULL);
739 	if (ret)
740 		dev_err(&dev->intf->dev, "gpio request failed %d\n", ret);
741 	else
742 		gpio_free(dvb->lna_gpio);
743 
744 	return ret;
745 #else
746 	dev_warn(&dev->intf->dev, "%s: LNA control is disabled (lna=%u)\n",
747 		 KBUILD_MODNAME, c->lna);
748 	return 0;
749 #endif
750 }
751 
752 static int em28xx_pctv_292e_set_lna(struct dvb_frontend *fe)
753 {
754 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
755 	struct em28xx_i2c_bus *i2c_bus = fe->dvb->priv;
756 	struct em28xx *dev = i2c_bus->dev;
757 	u8 lna;
758 
759 	if (c->lna == 1)
760 		lna = 0x01;
761 	else
762 		lna = 0x00;
763 
764 	return em28xx_write_reg_bits(dev, EM2874_R80_GPIO_P0_CTRL, lna, 0x01);
765 }
766 
767 static int em28xx_mt352_terratec_xs_init(struct dvb_frontend *fe)
768 {
769 	/* Values extracted from a USB trace of the Terratec Windows driver */
770 	static u8 clock_config[]   = { CLOCK_CTL,  0x38, 0x2c };
771 	static u8 reset[]          = { RESET,      0x80 };
772 	static u8 adc_ctl_1_cfg[]  = { ADC_CTL_1,  0x40 };
773 	static u8 agc_cfg[]        = { AGC_TARGET, 0x28, 0xa0 };
774 	static u8 input_freq_cfg[] = { INPUT_FREQ_1, 0x31, 0xb8 };
775 	static u8 rs_err_cfg[]     = { RS_ERR_PER_1, 0x00, 0x4d };
776 	static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
777 	static u8 trl_nom_cfg[]    = { TRL_NOMINAL_RATE_1, 0x64, 0x00 };
778 	static u8 tps_given_cfg[]  = { TPS_GIVEN_1, 0x40, 0x80, 0x50 };
779 	static u8 tuner_go[]       = { TUNER_GO, 0x01};
780 
781 	mt352_write(fe, clock_config,   sizeof(clock_config));
782 	udelay(200);
783 	mt352_write(fe, reset,          sizeof(reset));
784 	mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
785 	mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
786 	mt352_write(fe, input_freq_cfg, sizeof(input_freq_cfg));
787 	mt352_write(fe, rs_err_cfg,     sizeof(rs_err_cfg));
788 	mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
789 	mt352_write(fe, trl_nom_cfg,    sizeof(trl_nom_cfg));
790 	mt352_write(fe, tps_given_cfg,  sizeof(tps_given_cfg));
791 	mt352_write(fe, tuner_go,       sizeof(tuner_go));
792 	return 0;
793 }
794 
795 static void px_bcud_init(struct em28xx *dev)
796 {
797 	int i;
798 	struct {
799 		unsigned char r[4];
800 		int len;
801 	} regs1[] = {
802 		{{ 0x0e, 0x77 }, 2},
803 		{{ 0x0f, 0x77 }, 2},
804 		{{ 0x03, 0x90 }, 2},
805 	}, regs2[] = {
806 		{{ 0x07, 0x01 }, 2},
807 		{{ 0x08, 0x10 }, 2},
808 		{{ 0x13, 0x00 }, 2},
809 		{{ 0x17, 0x00 }, 2},
810 		{{ 0x03, 0x01 }, 2},
811 		{{ 0x10, 0xb1 }, 2},
812 		{{ 0x11, 0x40 }, 2},
813 		{{ 0x85, 0x7a }, 2},
814 		{{ 0x87, 0x04 }, 2},
815 	};
816 	static struct em28xx_reg_seq gpio[] = {
817 		{EM28XX_R06_I2C_CLK,		0x40,	0xff,	300},
818 		{EM2874_R80_GPIO_P0_CTRL,	0xfd,	0xff,	60},
819 		{EM28XX_R15_RGAIN,		0x20,	0xff,	0},
820 		{EM28XX_R16_GGAIN,		0x20,	0xff,	0},
821 		{EM28XX_R17_BGAIN,		0x20,	0xff,	0},
822 		{EM28XX_R18_ROFFSET,		0x00,	0xff,	0},
823 		{EM28XX_R19_GOFFSET,		0x00,	0xff,	0},
824 		{EM28XX_R1A_BOFFSET,		0x00,	0xff,	0},
825 		{EM28XX_R23_UOFFSET,		0x00,	0xff,	0},
826 		{EM28XX_R24_VOFFSET,		0x00,	0xff,	0},
827 		{EM28XX_R26_COMPR,		0x00,	0xff,	0},
828 		{0x13,				0x08,	0xff,	0},
829 		{EM28XX_R12_VINENABLE,		0x27,	0xff,	0},
830 		{EM28XX_R0C_USBSUSP,		0x10,	0xff,	0},
831 		{EM28XX_R27_OUTFMT,		0x00,	0xff,	0},
832 		{EM28XX_R10_VINMODE,		0x00,	0xff,	0},
833 		{EM28XX_R11_VINCTRL,		0x11,	0xff,	0},
834 		{EM2874_R50_IR_CONFIG,		0x01,	0xff,	0},
835 		{EM2874_R5F_TS_ENABLE,		0x80,	0xff,	0},
836 		{EM28XX_R06_I2C_CLK,		0x46,	0xff,	0},
837 	};
838 	em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x46);
839 	/* sleeping ISDB-T */
840 	dev->dvb->i2c_client_demod->addr = 0x14;
841 	for (i = 0; i < ARRAY_SIZE(regs1); i++)
842 		i2c_master_send(dev->dvb->i2c_client_demod, regs1[i].r,
843 				regs1[i].len);
844 	/* sleeping ISDB-S */
845 	dev->dvb->i2c_client_demod->addr = 0x15;
846 	for (i = 0; i < ARRAY_SIZE(regs2); i++)
847 		i2c_master_send(dev->dvb->i2c_client_demod, regs2[i].r,
848 				regs2[i].len);
849 	for (i = 0; i < ARRAY_SIZE(gpio); i++) {
850 		em28xx_write_reg_bits(dev, gpio[i].reg, gpio[i].val,
851 				      gpio[i].mask);
852 		if (gpio[i].sleep > 0)
853 			msleep(gpio[i].sleep);
854 	}
855 };
856 
857 static struct mt352_config terratec_xs_mt352_cfg = {
858 	.demod_address = (0x1e >> 1),
859 	.no_tuner = 1,
860 	.if2 = 45600,
861 	.demod_init = em28xx_mt352_terratec_xs_init,
862 };
863 
864 static struct tda10023_config em28xx_tda10023_config = {
865 	.demod_address = 0x0c,
866 	.invert = 1,
867 };
868 
869 static struct cxd2820r_config em28xx_cxd2820r_config = {
870 	.i2c_address = (0xd8 >> 1),
871 	.ts_mode = CXD2820R_TS_SERIAL,
872 };
873 
874 static struct tda18271_config em28xx_cxd2820r_tda18271_config = {
875 	.output_opt = TDA18271_OUTPUT_LT_OFF,
876 	.gate = TDA18271_GATE_DIGITAL,
877 };
878 
879 static struct zl10353_config em28xx_zl10353_no_i2c_gate_dev = {
880 	.demod_address = (0x1e >> 1),
881 	.disable_i2c_gate_ctrl = 1,
882 	.no_tuner = 1,
883 	.parallel_ts = 1,
884 };
885 
886 static struct mt2060_config em28xx_mt2060_config = {
887 	.i2c_address = 0x60,
888 };
889 
890 static struct qt1010_config em28xx_qt1010_config = {
891 	.i2c_address = 0x62
892 };
893 
894 static const struct mb86a20s_config c3tech_duo_mb86a20s_config = {
895 	.demod_address = 0x10,
896 	.is_serial = true,
897 };
898 
899 static struct tda18271_std_map mb86a20s_tda18271_config = {
900 	.dvbt_6   = { .if_freq = 4000, .agc_mode = 3, .std = 4,
901 		      .if_lvl = 1, .rfagc_top = 0x37, },
902 };
903 
904 static struct tda18271_config c3tech_duo_tda18271_config = {
905 	.std_map = &mb86a20s_tda18271_config,
906 	.gate    = TDA18271_GATE_DIGITAL,
907 	.small_i2c = TDA18271_03_BYTE_CHUNK_INIT,
908 };
909 
910 static struct tda18271_std_map drx_j_std_map = {
911 	.atsc_6   = { .if_freq = 5000, .agc_mode = 3, .std = 0, .if_lvl = 1,
912 		      .rfagc_top = 0x37, },
913 	.qam_6    = { .if_freq = 5380, .agc_mode = 3, .std = 3, .if_lvl = 1,
914 		      .rfagc_top = 0x37, },
915 };
916 
917 static struct tda18271_config pinnacle_80e_dvb_config = {
918 	.std_map = &drx_j_std_map,
919 	.gate    = TDA18271_GATE_DIGITAL,
920 	.role    = TDA18271_MASTER,
921 };
922 
923 /* ------------------------------------------------------------------ */
924 
925 static int em28xx_attach_xc3028(u8 addr, struct em28xx *dev)
926 {
927 	struct dvb_frontend *fe;
928 	struct xc2028_config cfg;
929 	struct xc2028_ctrl ctl;
930 
931 	memset(&cfg, 0, sizeof(cfg));
932 	cfg.i2c_adap  = &dev->i2c_adap[dev->def_i2c_bus];
933 	cfg.i2c_addr  = addr;
934 
935 	memset(&ctl, 0, sizeof(ctl));
936 	em28xx_setup_xc3028(dev, &ctl);
937 	cfg.ctrl  = &ctl;
938 
939 	if (!dev->dvb->fe[0]) {
940 		dev_err(&dev->intf->dev,
941 			"dvb frontend not attached. Can't attach xc3028\n");
942 		return -EINVAL;
943 	}
944 
945 	fe = dvb_attach(xc2028_attach, dev->dvb->fe[0], &cfg);
946 	if (!fe) {
947 		dev_err(&dev->intf->dev, "xc3028 attach failed\n");
948 		dvb_frontend_detach(dev->dvb->fe[0]);
949 		dev->dvb->fe[0] = NULL;
950 		return -EINVAL;
951 	}
952 
953 	dev_info(&dev->intf->dev, "xc3028 attached\n");
954 
955 	return 0;
956 }
957 
958 /* ------------------------------------------------------------------ */
959 
960 static int em28xx_register_dvb(struct em28xx_dvb *dvb, struct module *module,
961 			       struct em28xx *dev, struct device *device)
962 {
963 	int result;
964 	bool create_rf_connector = false;
965 
966 	mutex_init(&dvb->lock);
967 
968 	/* register adapter */
969 	result = dvb_register_adapter(&dvb->adapter,
970 				      dev_name(&dev->intf->dev), module,
971 				      device, adapter_nr);
972 	if (result < 0) {
973 		dev_warn(&dev->intf->dev,
974 			 "dvb_register_adapter failed (errno = %d)\n",
975 			 result);
976 		goto fail_adapter;
977 	}
978 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
979 	dvb->adapter.mdev = dev->media_dev;
980 #endif
981 
982 	/* Ensure all frontends negotiate bus access */
983 	dvb->fe[0]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
984 	if (dvb->fe[1])
985 		dvb->fe[1]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
986 
987 	dvb->adapter.priv = &dev->i2c_bus[dev->def_i2c_bus];
988 
989 	/* register frontend */
990 	result = dvb_register_frontend(&dvb->adapter, dvb->fe[0]);
991 	if (result < 0) {
992 		dev_warn(&dev->intf->dev,
993 			 "dvb_register_frontend failed (errno = %d)\n",
994 			 result);
995 		goto fail_frontend0;
996 	}
997 
998 	/* register 2nd frontend */
999 	if (dvb->fe[1]) {
1000 		result = dvb_register_frontend(&dvb->adapter, dvb->fe[1]);
1001 		if (result < 0) {
1002 			dev_warn(&dev->intf->dev,
1003 				 "2nd dvb_register_frontend failed (errno = %d)\n",
1004 				 result);
1005 			goto fail_frontend1;
1006 		}
1007 	}
1008 
1009 	/* register demux stuff */
1010 	dvb->demux.dmx.capabilities =
1011 		DMX_TS_FILTERING | DMX_SECTION_FILTERING |
1012 		DMX_MEMORY_BASED_FILTERING;
1013 	dvb->demux.priv       = dvb;
1014 	dvb->demux.filternum  = 256;
1015 	dvb->demux.feednum    = 256;
1016 	dvb->demux.start_feed = em28xx_start_feed;
1017 	dvb->demux.stop_feed  = em28xx_stop_feed;
1018 
1019 	result = dvb_dmx_init(&dvb->demux);
1020 	if (result < 0) {
1021 		dev_warn(&dev->intf->dev,
1022 			 "dvb_dmx_init failed (errno = %d)\n",
1023 			 result);
1024 		goto fail_dmx;
1025 	}
1026 
1027 	dvb->dmxdev.filternum    = 256;
1028 	dvb->dmxdev.demux        = &dvb->demux.dmx;
1029 	dvb->dmxdev.capabilities = 0;
1030 	result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
1031 	if (result < 0) {
1032 		dev_warn(&dev->intf->dev,
1033 			 "dvb_dmxdev_init failed (errno = %d)\n",
1034 			 result);
1035 		goto fail_dmxdev;
1036 	}
1037 
1038 	dvb->fe_hw.source = DMX_FRONTEND_0;
1039 	result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
1040 	if (result < 0) {
1041 		dev_warn(&dev->intf->dev,
1042 			 "add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
1043 			 result);
1044 		goto fail_fe_hw;
1045 	}
1046 
1047 	dvb->fe_mem.source = DMX_MEMORY_FE;
1048 	result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
1049 	if (result < 0) {
1050 		dev_warn(&dev->intf->dev,
1051 			 "add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
1052 			 result);
1053 		goto fail_fe_mem;
1054 	}
1055 
1056 	result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
1057 	if (result < 0) {
1058 		dev_warn(&dev->intf->dev,
1059 			 "connect_frontend failed (errno = %d)\n",
1060 			 result);
1061 		goto fail_fe_conn;
1062 	}
1063 
1064 	/* register network adapter */
1065 	dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
1066 
1067 	/* If the analog part won't create RF connectors, DVB will do it */
1068 	if (!dev->has_video || (dev->tuner_type == TUNER_ABSENT))
1069 		create_rf_connector = true;
1070 
1071 	result = dvb_create_media_graph(&dvb->adapter, create_rf_connector);
1072 	if (result < 0)
1073 		goto fail_create_graph;
1074 
1075 	return 0;
1076 
1077 fail_create_graph:
1078 	dvb_net_release(&dvb->net);
1079 fail_fe_conn:
1080 	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
1081 fail_fe_mem:
1082 	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
1083 fail_fe_hw:
1084 	dvb_dmxdev_release(&dvb->dmxdev);
1085 fail_dmxdev:
1086 	dvb_dmx_release(&dvb->demux);
1087 fail_dmx:
1088 	if (dvb->fe[1])
1089 		dvb_unregister_frontend(dvb->fe[1]);
1090 	dvb_unregister_frontend(dvb->fe[0]);
1091 fail_frontend1:
1092 	if (dvb->fe[1])
1093 		dvb_frontend_detach(dvb->fe[1]);
1094 fail_frontend0:
1095 	dvb_frontend_detach(dvb->fe[0]);
1096 	dvb_unregister_adapter(&dvb->adapter);
1097 fail_adapter:
1098 	return result;
1099 }
1100 
1101 static void em28xx_unregister_dvb(struct em28xx_dvb *dvb)
1102 {
1103 	dvb_net_release(&dvb->net);
1104 	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
1105 	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
1106 	dvb_dmxdev_release(&dvb->dmxdev);
1107 	dvb_dmx_release(&dvb->demux);
1108 	if (dvb->fe[1])
1109 		dvb_unregister_frontend(dvb->fe[1]);
1110 	dvb_unregister_frontend(dvb->fe[0]);
1111 	if (dvb->fe[1] && !dvb->dont_attach_fe1)
1112 		dvb_frontend_detach(dvb->fe[1]);
1113 	dvb_frontend_detach(dvb->fe[0]);
1114 	dvb_unregister_adapter(&dvb->adapter);
1115 }
1116 
1117 static int em28xx_dvb_init(struct em28xx *dev)
1118 {
1119 	int result = 0;
1120 	struct em28xx_dvb *dvb;
1121 
1122 	if (dev->is_audio_only) {
1123 		/* Shouldn't initialize IR for this interface */
1124 		return 0;
1125 	}
1126 
1127 	if (!dev->board.has_dvb) {
1128 		/* This device does not support the extension */
1129 		return 0;
1130 	}
1131 
1132 	dev_info(&dev->intf->dev, "Binding DVB extension\n");
1133 
1134 	dvb = kzalloc(sizeof(struct em28xx_dvb), GFP_KERNEL);
1135 	if (!dvb)
1136 		return -ENOMEM;
1137 
1138 	dev->dvb = dvb;
1139 	dvb->fe[0] = dvb->fe[1] = NULL;
1140 
1141 	/* pre-allocate DVB usb transfer buffers */
1142 	if (dev->dvb_xfer_bulk) {
1143 		result = em28xx_alloc_urbs(dev, EM28XX_DIGITAL_MODE,
1144 					   dev->dvb_xfer_bulk,
1145 					   EM28XX_DVB_NUM_BUFS,
1146 					   512,
1147 					   EM28XX_DVB_BULK_PACKET_MULTIPLIER);
1148 	} else {
1149 		result = em28xx_alloc_urbs(dev, EM28XX_DIGITAL_MODE,
1150 					   dev->dvb_xfer_bulk,
1151 					   EM28XX_DVB_NUM_BUFS,
1152 					   dev->dvb_max_pkt_size_isoc,
1153 					   EM28XX_DVB_NUM_ISOC_PACKETS);
1154 	}
1155 	if (result) {
1156 		dev_err(&dev->intf->dev,
1157 			"failed to pre-allocate USB transfer buffers for DVB.\n");
1158 		kfree(dvb);
1159 		dev->dvb = NULL;
1160 		return result;
1161 	}
1162 
1163 	mutex_lock(&dev->lock);
1164 	em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
1165 	/* init frontend */
1166 	switch (dev->model) {
1167 	case EM2874_BOARD_LEADERSHIP_ISDBT:
1168 		dvb->fe[0] = dvb_attach(s921_attach,
1169 				&sharp_isdbt, &dev->i2c_adap[dev->def_i2c_bus]);
1170 
1171 		if (!dvb->fe[0]) {
1172 			result = -EINVAL;
1173 			goto out_free;
1174 		}
1175 
1176 		break;
1177 	case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_850:
1178 	case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_950:
1179 	case EM2880_BOARD_PINNACLE_PCTV_HD_PRO:
1180 	case EM2880_BOARD_AMD_ATI_TV_WONDER_HD_600:
1181 		dvb->fe[0] = dvb_attach(lgdt330x_attach,
1182 					   &em2880_lgdt3303_dev,
1183 					   &dev->i2c_adap[dev->def_i2c_bus]);
1184 		if (em28xx_attach_xc3028(0x61, dev) < 0) {
1185 			result = -EINVAL;
1186 			goto out_free;
1187 		}
1188 		break;
1189 	case EM2880_BOARD_KWORLD_DVB_310U:
1190 		dvb->fe[0] = dvb_attach(zl10353_attach,
1191 					   &em28xx_zl10353_with_xc3028,
1192 					   &dev->i2c_adap[dev->def_i2c_bus]);
1193 		if (em28xx_attach_xc3028(0x61, dev) < 0) {
1194 			result = -EINVAL;
1195 			goto out_free;
1196 		}
1197 		break;
1198 	case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900:
1199 	case EM2882_BOARD_TERRATEC_HYBRID_XS:
1200 	case EM2880_BOARD_EMPIRE_DUAL_TV:
1201 		dvb->fe[0] = dvb_attach(zl10353_attach,
1202 					   &em28xx_zl10353_xc3028_no_i2c_gate,
1203 					   &dev->i2c_adap[dev->def_i2c_bus]);
1204 		if (em28xx_attach_xc3028(0x61, dev) < 0) {
1205 			result = -EINVAL;
1206 			goto out_free;
1207 		}
1208 		break;
1209 	case EM2880_BOARD_TERRATEC_HYBRID_XS:
1210 	case EM2880_BOARD_TERRATEC_HYBRID_XS_FR:
1211 	case EM2881_BOARD_PINNACLE_HYBRID_PRO:
1212 	case EM2882_BOARD_DIKOM_DK300:
1213 	case EM2882_BOARD_KWORLD_VS_DVBT:
1214 		dvb->fe[0] = dvb_attach(zl10353_attach,
1215 					   &em28xx_zl10353_xc3028_no_i2c_gate,
1216 					   &dev->i2c_adap[dev->def_i2c_bus]);
1217 		if (dvb->fe[0] == NULL) {
1218 			/* This board could have either a zl10353 or a mt352.
1219 			   If the chip id isn't for zl10353, try mt352 */
1220 			dvb->fe[0] = dvb_attach(mt352_attach,
1221 						   &terratec_xs_mt352_cfg,
1222 						   &dev->i2c_adap[dev->def_i2c_bus]);
1223 		}
1224 
1225 		if (em28xx_attach_xc3028(0x61, dev) < 0) {
1226 			result = -EINVAL;
1227 			goto out_free;
1228 		}
1229 		break;
1230 	case EM2870_BOARD_TERRATEC_XS_MT2060:
1231 		dvb->fe[0] = dvb_attach(zl10353_attach,
1232 						&em28xx_zl10353_no_i2c_gate_dev,
1233 						&dev->i2c_adap[dev->def_i2c_bus]);
1234 		if (dvb->fe[0] != NULL) {
1235 			dvb_attach(mt2060_attach, dvb->fe[0],
1236 					&dev->i2c_adap[dev->def_i2c_bus],
1237 					&em28xx_mt2060_config, 1220);
1238 		}
1239 		break;
1240 	case EM2870_BOARD_KWORLD_355U:
1241 		dvb->fe[0] = dvb_attach(zl10353_attach,
1242 					   &em28xx_zl10353_no_i2c_gate_dev,
1243 					   &dev->i2c_adap[dev->def_i2c_bus]);
1244 		if (dvb->fe[0] != NULL)
1245 			dvb_attach(qt1010_attach, dvb->fe[0],
1246 				   &dev->i2c_adap[dev->def_i2c_bus], &em28xx_qt1010_config);
1247 		break;
1248 	case EM2883_BOARD_KWORLD_HYBRID_330U:
1249 	case EM2882_BOARD_EVGA_INDTUBE:
1250 		dvb->fe[0] = dvb_attach(s5h1409_attach,
1251 					   &em28xx_s5h1409_with_xc3028,
1252 					   &dev->i2c_adap[dev->def_i2c_bus]);
1253 		if (em28xx_attach_xc3028(0x61, dev) < 0) {
1254 			result = -EINVAL;
1255 			goto out_free;
1256 		}
1257 		break;
1258 	case EM2882_BOARD_KWORLD_ATSC_315U:
1259 		dvb->fe[0] = dvb_attach(lgdt330x_attach,
1260 					   &em2880_lgdt3303_dev,
1261 					   &dev->i2c_adap[dev->def_i2c_bus]);
1262 		if (dvb->fe[0] != NULL) {
1263 			if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
1264 					&dev->i2c_adap[dev->def_i2c_bus],
1265 					0x61, TUNER_THOMSON_DTT761X)) {
1266 				result = -EINVAL;
1267 				goto out_free;
1268 			}
1269 		}
1270 		break;
1271 	case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900_R2:
1272 	case EM2882_BOARD_PINNACLE_HYBRID_PRO_330E:
1273 		dvb->fe[0] = dvb_attach(drxd_attach, &em28xx_drxd, NULL,
1274 					&dev->i2c_adap[dev->def_i2c_bus],
1275 					&dev->intf->dev);
1276 		if (em28xx_attach_xc3028(0x61, dev) < 0) {
1277 			result = -EINVAL;
1278 			goto out_free;
1279 		}
1280 		break;
1281 	case EM2870_BOARD_REDDO_DVB_C_USB_BOX:
1282 		/* Philips CU1216L NIM (Philips TDA10023 + Infineon TUA6034) */
1283 		dvb->fe[0] = dvb_attach(tda10023_attach,
1284 			&em28xx_tda10023_config,
1285 			&dev->i2c_adap[dev->def_i2c_bus], 0x48);
1286 		if (dvb->fe[0]) {
1287 			if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
1288 					&dev->i2c_adap[dev->def_i2c_bus],
1289 					0x60, TUNER_PHILIPS_CU1216L)) {
1290 				result = -EINVAL;
1291 				goto out_free;
1292 			}
1293 		}
1294 		break;
1295 	case EM2870_BOARD_KWORLD_A340:
1296 		dvb->fe[0] = dvb_attach(lgdt3305_attach,
1297 					   &em2870_lgdt3304_dev,
1298 					   &dev->i2c_adap[dev->def_i2c_bus]);
1299 		if (!dvb->fe[0]) {
1300 			result = -EINVAL;
1301 			goto out_free;
1302 		}
1303 		if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1304 				&dev->i2c_adap[dev->def_i2c_bus],
1305 			&kworld_a340_config)) {
1306 				dvb_frontend_detach(dvb->fe[0]);
1307 				result = -EINVAL;
1308 				goto out_free;
1309 		}
1310 		break;
1311 	case EM28174_BOARD_PCTV_290E:
1312 		/* set default GPIO0 for LNA, used if GPIOLIB is undefined */
1313 		dvb->lna_gpio = CXD2820R_GPIO_E | CXD2820R_GPIO_O |
1314 				CXD2820R_GPIO_L;
1315 		dvb->fe[0] = dvb_attach(cxd2820r_attach,
1316 					&em28xx_cxd2820r_config,
1317 					&dev->i2c_adap[dev->def_i2c_bus],
1318 					&dvb->lna_gpio);
1319 		if (dvb->fe[0]) {
1320 			/* FE 0 attach tuner */
1321 			if (!dvb_attach(tda18271_attach,
1322 					dvb->fe[0],
1323 					0x60,
1324 					&dev->i2c_adap[dev->def_i2c_bus],
1325 					&em28xx_cxd2820r_tda18271_config)) {
1326 
1327 				dvb_frontend_detach(dvb->fe[0]);
1328 				result = -EINVAL;
1329 				goto out_free;
1330 			}
1331 
1332 #ifdef CONFIG_GPIOLIB
1333 			/* enable LNA for DVB-T, DVB-T2 and DVB-C */
1334 			result = gpio_request_one(dvb->lna_gpio,
1335 						  GPIOF_OUT_INIT_LOW, NULL);
1336 			if (result)
1337 				dev_err(&dev->intf->dev,
1338 					"gpio request failed %d\n",
1339 					result);
1340 			else
1341 				gpio_free(dvb->lna_gpio);
1342 
1343 			result = 0; /* continue even set LNA fails */
1344 #endif
1345 			dvb->fe[0]->ops.set_lna = em28xx_pctv_290e_set_lna;
1346 		}
1347 
1348 		break;
1349 	case EM2884_BOARD_HAUPPAUGE_WINTV_HVR_930C:
1350 	{
1351 		struct xc5000_config cfg;
1352 
1353 		hauppauge_hvr930c_init(dev);
1354 
1355 		dvb->fe[0] = dvb_attach(drxk_attach,
1356 					&hauppauge_930c_drxk, &dev->i2c_adap[dev->def_i2c_bus]);
1357 		if (!dvb->fe[0]) {
1358 			result = -EINVAL;
1359 			goto out_free;
1360 		}
1361 		/* FIXME: do we need a pll semaphore? */
1362 		dvb->fe[0]->sec_priv = dvb;
1363 		sema_init(&dvb->pll_mutex, 1);
1364 		dvb->gate_ctrl = dvb->fe[0]->ops.i2c_gate_ctrl;
1365 		dvb->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
1366 
1367 		/* Attach xc5000 */
1368 		memset(&cfg, 0, sizeof(cfg));
1369 		cfg.i2c_address  = 0x61;
1370 		cfg.if_khz = 4000;
1371 
1372 		if (dvb->fe[0]->ops.i2c_gate_ctrl)
1373 			dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 1);
1374 		if (!dvb_attach(xc5000_attach, dvb->fe[0], &dev->i2c_adap[dev->def_i2c_bus],
1375 				&cfg)) {
1376 			result = -EINVAL;
1377 			goto out_free;
1378 		}
1379 		if (dvb->fe[0]->ops.i2c_gate_ctrl)
1380 			dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 0);
1381 
1382 		break;
1383 	}
1384 	case EM2884_BOARD_TERRATEC_H5:
1385 		terratec_h5_init(dev);
1386 
1387 		dvb->fe[0] = dvb_attach(drxk_attach, &terratec_h5_drxk, &dev->i2c_adap[dev->def_i2c_bus]);
1388 		if (!dvb->fe[0]) {
1389 			result = -EINVAL;
1390 			goto out_free;
1391 		}
1392 		/* FIXME: do we need a pll semaphore? */
1393 		dvb->fe[0]->sec_priv = dvb;
1394 		sema_init(&dvb->pll_mutex, 1);
1395 		dvb->gate_ctrl = dvb->fe[0]->ops.i2c_gate_ctrl;
1396 		dvb->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
1397 
1398 		/* Attach tda18271 to DVB-C frontend */
1399 		if (dvb->fe[0]->ops.i2c_gate_ctrl)
1400 			dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 1);
1401 		if (!dvb_attach(tda18271c2dd_attach, dvb->fe[0], &dev->i2c_adap[dev->def_i2c_bus], 0x60)) {
1402 			result = -EINVAL;
1403 			goto out_free;
1404 		}
1405 		if (dvb->fe[0]->ops.i2c_gate_ctrl)
1406 			dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 0);
1407 
1408 		break;
1409 	case EM2884_BOARD_C3TECH_DIGITAL_DUO:
1410 		dvb->fe[0] = dvb_attach(mb86a20s_attach,
1411 					   &c3tech_duo_mb86a20s_config,
1412 					   &dev->i2c_adap[dev->def_i2c_bus]);
1413 		if (dvb->fe[0] != NULL)
1414 			dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1415 				   &dev->i2c_adap[dev->def_i2c_bus],
1416 				   &c3tech_duo_tda18271_config);
1417 		break;
1418 	case EM28174_BOARD_PCTV_460E: {
1419 		struct i2c_client *client;
1420 		struct i2c_board_info board_info;
1421 		struct tda10071_platform_data tda10071_pdata = {};
1422 		struct a8293_platform_data a8293_pdata = {};
1423 
1424 		/* attach demod + tuner combo */
1425 		tda10071_pdata.clk = 40444000, /* 40.444 MHz */
1426 		tda10071_pdata.i2c_wr_max = 64,
1427 		tda10071_pdata.ts_mode = TDA10071_TS_SERIAL,
1428 		tda10071_pdata.pll_multiplier = 20,
1429 		tda10071_pdata.tuner_i2c_addr = 0x14,
1430 		memset(&board_info, 0, sizeof(board_info));
1431 		strlcpy(board_info.type, "tda10071_cx24118", I2C_NAME_SIZE);
1432 		board_info.addr = 0x55;
1433 		board_info.platform_data = &tda10071_pdata;
1434 		request_module("tda10071");
1435 		client = i2c_new_device(&dev->i2c_adap[dev->def_i2c_bus], &board_info);
1436 		if (client == NULL || client->dev.driver == NULL) {
1437 			result = -ENODEV;
1438 			goto out_free;
1439 		}
1440 		if (!try_module_get(client->dev.driver->owner)) {
1441 			i2c_unregister_device(client);
1442 			result = -ENODEV;
1443 			goto out_free;
1444 		}
1445 		dvb->fe[0] = tda10071_pdata.get_dvb_frontend(client);
1446 		dvb->i2c_client_demod = client;
1447 
1448 		/* attach SEC */
1449 		a8293_pdata.dvb_frontend = dvb->fe[0];
1450 		memset(&board_info, 0, sizeof(board_info));
1451 		strlcpy(board_info.type, "a8293", I2C_NAME_SIZE);
1452 		board_info.addr = 0x08;
1453 		board_info.platform_data = &a8293_pdata;
1454 		request_module("a8293");
1455 		client = i2c_new_device(&dev->i2c_adap[dev->def_i2c_bus], &board_info);
1456 		if (client == NULL || client->dev.driver == NULL) {
1457 			module_put(dvb->i2c_client_demod->dev.driver->owner);
1458 			i2c_unregister_device(dvb->i2c_client_demod);
1459 			result = -ENODEV;
1460 			goto out_free;
1461 		}
1462 		if (!try_module_get(client->dev.driver->owner)) {
1463 			i2c_unregister_device(client);
1464 			module_put(dvb->i2c_client_demod->dev.driver->owner);
1465 			i2c_unregister_device(dvb->i2c_client_demod);
1466 			result = -ENODEV;
1467 			goto out_free;
1468 		}
1469 		dvb->i2c_client_sec = client;
1470 		break;
1471 	}
1472 	case EM2874_BOARD_DELOCK_61959:
1473 	case EM2874_BOARD_MAXMEDIA_UB425_TC:
1474 		/* attach demodulator */
1475 		dvb->fe[0] = dvb_attach(drxk_attach, &maxmedia_ub425_tc_drxk,
1476 				&dev->i2c_adap[dev->def_i2c_bus]);
1477 
1478 		if (dvb->fe[0]) {
1479 			/* disable I2C-gate */
1480 			dvb->fe[0]->ops.i2c_gate_ctrl = NULL;
1481 
1482 			/* attach tuner */
1483 			if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1484 					&dev->i2c_adap[dev->def_i2c_bus],
1485 					&em28xx_cxd2820r_tda18271_config)) {
1486 				dvb_frontend_detach(dvb->fe[0]);
1487 				result = -EINVAL;
1488 				goto out_free;
1489 			}
1490 		}
1491 		break;
1492 	case EM2884_BOARD_PCTV_510E:
1493 	case EM2884_BOARD_PCTV_520E:
1494 		pctv_520e_init(dev);
1495 
1496 		/* attach demodulator */
1497 		dvb->fe[0] = dvb_attach(drxk_attach, &pctv_520e_drxk,
1498 				&dev->i2c_adap[dev->def_i2c_bus]);
1499 
1500 		if (dvb->fe[0]) {
1501 			/* attach tuner */
1502 			if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1503 					&dev->i2c_adap[dev->def_i2c_bus],
1504 					&em28xx_cxd2820r_tda18271_config)) {
1505 				dvb_frontend_detach(dvb->fe[0]);
1506 				result = -EINVAL;
1507 				goto out_free;
1508 			}
1509 		}
1510 		break;
1511 	case EM2884_BOARD_ELGATO_EYETV_HYBRID_2008:
1512 	case EM2884_BOARD_CINERGY_HTC_STICK:
1513 		terratec_htc_stick_init(dev);
1514 
1515 		/* attach demodulator */
1516 		dvb->fe[0] = dvb_attach(drxk_attach, &terratec_htc_stick_drxk,
1517 					&dev->i2c_adap[dev->def_i2c_bus]);
1518 		if (!dvb->fe[0]) {
1519 			result = -EINVAL;
1520 			goto out_free;
1521 		}
1522 
1523 		/* Attach the demodulator. */
1524 		if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1525 				&dev->i2c_adap[dev->def_i2c_bus],
1526 				&em28xx_cxd2820r_tda18271_config)) {
1527 			result = -EINVAL;
1528 			goto out_free;
1529 		}
1530 		break;
1531 	case EM2884_BOARD_TERRATEC_HTC_USB_XS:
1532 		terratec_htc_usb_xs_init(dev);
1533 
1534 		/* attach demodulator */
1535 		dvb->fe[0] = dvb_attach(drxk_attach, &terratec_htc_stick_drxk,
1536 					&dev->i2c_adap[dev->def_i2c_bus]);
1537 		if (!dvb->fe[0]) {
1538 			result = -EINVAL;
1539 			goto out_free;
1540 		}
1541 
1542 		/* Attach the demodulator. */
1543 		if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1544 				&dev->i2c_adap[dev->def_i2c_bus],
1545 				&em28xx_cxd2820r_tda18271_config)) {
1546 			result = -EINVAL;
1547 			goto out_free;
1548 		}
1549 		break;
1550 	case EM2874_BOARD_KWORLD_UB435Q_V2:
1551 		dvb->fe[0] = dvb_attach(lgdt3305_attach,
1552 					&em2874_lgdt3305_dev,
1553 					&dev->i2c_adap[dev->def_i2c_bus]);
1554 		if (!dvb->fe[0]) {
1555 			result = -EINVAL;
1556 			goto out_free;
1557 		}
1558 
1559 		/* Attach the demodulator. */
1560 		if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1561 				&dev->i2c_adap[dev->def_i2c_bus],
1562 				&kworld_ub435q_v2_config)) {
1563 			result = -EINVAL;
1564 			goto out_free;
1565 		}
1566 		break;
1567 	case EM2874_BOARD_KWORLD_UB435Q_V3:
1568 	{
1569 		struct i2c_client *client;
1570 		struct i2c_adapter *adapter = &dev->i2c_adap[dev->def_i2c_bus];
1571 		struct i2c_board_info board_info = {
1572 			.type = "tda18212",
1573 			.addr = 0x60,
1574 			.platform_data = &kworld_ub435q_v3_config,
1575 		};
1576 
1577 		dvb->fe[0] = dvb_attach(lgdt3305_attach,
1578 					&em2874_lgdt3305_nogate_dev,
1579 					&dev->i2c_adap[dev->def_i2c_bus]);
1580 		if (!dvb->fe[0]) {
1581 			result = -EINVAL;
1582 			goto out_free;
1583 		}
1584 
1585 		/* attach tuner */
1586 		kworld_ub435q_v3_config.fe = dvb->fe[0];
1587 		request_module("tda18212");
1588 		client = i2c_new_device(adapter, &board_info);
1589 		if (client == NULL || client->dev.driver == NULL) {
1590 			dvb_frontend_detach(dvb->fe[0]);
1591 			result = -ENODEV;
1592 			goto out_free;
1593 		}
1594 
1595 		if (!try_module_get(client->dev.driver->owner)) {
1596 			i2c_unregister_device(client);
1597 			dvb_frontend_detach(dvb->fe[0]);
1598 			result = -ENODEV;
1599 			goto out_free;
1600 		}
1601 
1602 		dvb->i2c_client_tuner = client;
1603 		break;
1604 	}
1605 	case EM2874_BOARD_PCTV_HD_MINI_80E:
1606 		dvb->fe[0] = dvb_attach(drx39xxj_attach, &dev->i2c_adap[dev->def_i2c_bus]);
1607 		if (dvb->fe[0] != NULL) {
1608 			dvb->fe[0] = dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1609 						&dev->i2c_adap[dev->def_i2c_bus],
1610 						&pinnacle_80e_dvb_config);
1611 			if (!dvb->fe[0]) {
1612 				result = -EINVAL;
1613 				goto out_free;
1614 			}
1615 		}
1616 		break;
1617 	case EM28178_BOARD_PCTV_461E: {
1618 		struct i2c_client *client;
1619 		struct i2c_adapter *i2c_adapter;
1620 		struct i2c_board_info board_info;
1621 		struct m88ds3103_platform_data m88ds3103_pdata = {};
1622 		struct ts2020_config ts2020_config = {};
1623 		struct a8293_platform_data a8293_pdata = {};
1624 
1625 		/* attach demod */
1626 		m88ds3103_pdata.clk = 27000000;
1627 		m88ds3103_pdata.i2c_wr_max = 33;
1628 		m88ds3103_pdata.ts_mode = M88DS3103_TS_PARALLEL;
1629 		m88ds3103_pdata.ts_clk = 16000;
1630 		m88ds3103_pdata.ts_clk_pol = 1;
1631 		m88ds3103_pdata.agc = 0x99;
1632 		memset(&board_info, 0, sizeof(board_info));
1633 		strlcpy(board_info.type, "m88ds3103", I2C_NAME_SIZE);
1634 		board_info.addr = 0x68;
1635 		board_info.platform_data = &m88ds3103_pdata;
1636 		request_module("m88ds3103");
1637 		client = i2c_new_device(&dev->i2c_adap[dev->def_i2c_bus], &board_info);
1638 		if (client == NULL || client->dev.driver == NULL) {
1639 			result = -ENODEV;
1640 			goto out_free;
1641 		}
1642 		if (!try_module_get(client->dev.driver->owner)) {
1643 			i2c_unregister_device(client);
1644 			result = -ENODEV;
1645 			goto out_free;
1646 		}
1647 		dvb->fe[0] = m88ds3103_pdata.get_dvb_frontend(client);
1648 		i2c_adapter = m88ds3103_pdata.get_i2c_adapter(client);
1649 		dvb->i2c_client_demod = client;
1650 
1651 		/* attach tuner */
1652 		ts2020_config.fe = dvb->fe[0];
1653 		memset(&board_info, 0, sizeof(board_info));
1654 		strlcpy(board_info.type, "ts2022", I2C_NAME_SIZE);
1655 		board_info.addr = 0x60;
1656 		board_info.platform_data = &ts2020_config;
1657 		request_module("ts2020");
1658 		client = i2c_new_device(i2c_adapter, &board_info);
1659 		if (client == NULL || client->dev.driver == NULL) {
1660 			module_put(dvb->i2c_client_demod->dev.driver->owner);
1661 			i2c_unregister_device(dvb->i2c_client_demod);
1662 			result = -ENODEV;
1663 			goto out_free;
1664 		}
1665 		if (!try_module_get(client->dev.driver->owner)) {
1666 			i2c_unregister_device(client);
1667 			module_put(dvb->i2c_client_demod->dev.driver->owner);
1668 			i2c_unregister_device(dvb->i2c_client_demod);
1669 			result = -ENODEV;
1670 			goto out_free;
1671 		}
1672 		dvb->i2c_client_tuner = client;
1673 		/* delegate signal strength measurement to tuner */
1674 		dvb->fe[0]->ops.read_signal_strength =
1675 				dvb->fe[0]->ops.tuner_ops.get_rf_strength;
1676 
1677 		/* attach SEC */
1678 		a8293_pdata.dvb_frontend = dvb->fe[0];
1679 		memset(&board_info, 0, sizeof(board_info));
1680 		strlcpy(board_info.type, "a8293", I2C_NAME_SIZE);
1681 		board_info.addr = 0x08;
1682 		board_info.platform_data = &a8293_pdata;
1683 		request_module("a8293");
1684 		client = i2c_new_device(&dev->i2c_adap[dev->def_i2c_bus], &board_info);
1685 		if (client == NULL || client->dev.driver == NULL) {
1686 			module_put(dvb->i2c_client_tuner->dev.driver->owner);
1687 			i2c_unregister_device(dvb->i2c_client_tuner);
1688 			module_put(dvb->i2c_client_demod->dev.driver->owner);
1689 			i2c_unregister_device(dvb->i2c_client_demod);
1690 			result = -ENODEV;
1691 			goto out_free;
1692 		}
1693 		if (!try_module_get(client->dev.driver->owner)) {
1694 			i2c_unregister_device(client);
1695 			module_put(dvb->i2c_client_tuner->dev.driver->owner);
1696 			i2c_unregister_device(dvb->i2c_client_tuner);
1697 			module_put(dvb->i2c_client_demod->dev.driver->owner);
1698 			i2c_unregister_device(dvb->i2c_client_demod);
1699 			result = -ENODEV;
1700 			goto out_free;
1701 		}
1702 		dvb->i2c_client_sec = client;
1703 		break;
1704 	}
1705 	case EM28178_BOARD_PCTV_292E:
1706 		{
1707 			struct i2c_adapter *adapter;
1708 			struct i2c_client *client;
1709 			struct i2c_board_info info;
1710 			struct si2168_config si2168_config;
1711 			struct si2157_config si2157_config;
1712 
1713 			/* attach demod */
1714 			memset(&si2168_config, 0, sizeof(si2168_config));
1715 			si2168_config.i2c_adapter = &adapter;
1716 			si2168_config.fe = &dvb->fe[0];
1717 			si2168_config.ts_mode = SI2168_TS_PARALLEL;
1718 			memset(&info, 0, sizeof(struct i2c_board_info));
1719 			strlcpy(info.type, "si2168", I2C_NAME_SIZE);
1720 			info.addr = 0x64;
1721 			info.platform_data = &si2168_config;
1722 			request_module(info.type);
1723 			client = i2c_new_device(&dev->i2c_adap[dev->def_i2c_bus], &info);
1724 			if (client == NULL || client->dev.driver == NULL) {
1725 				result = -ENODEV;
1726 				goto out_free;
1727 			}
1728 
1729 			if (!try_module_get(client->dev.driver->owner)) {
1730 				i2c_unregister_device(client);
1731 				result = -ENODEV;
1732 				goto out_free;
1733 			}
1734 
1735 			dvb->i2c_client_demod = client;
1736 
1737 			/* attach tuner */
1738 			memset(&si2157_config, 0, sizeof(si2157_config));
1739 			si2157_config.fe = dvb->fe[0];
1740 			si2157_config.if_port = 1;
1741 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
1742 			si2157_config.mdev = dev->media_dev;
1743 #endif
1744 			memset(&info, 0, sizeof(struct i2c_board_info));
1745 			strlcpy(info.type, "si2157", I2C_NAME_SIZE);
1746 			info.addr = 0x60;
1747 			info.platform_data = &si2157_config;
1748 			request_module(info.type);
1749 			client = i2c_new_device(adapter, &info);
1750 			if (client == NULL || client->dev.driver == NULL) {
1751 				module_put(dvb->i2c_client_demod->dev.driver->owner);
1752 				i2c_unregister_device(dvb->i2c_client_demod);
1753 				result = -ENODEV;
1754 				goto out_free;
1755 			}
1756 
1757 			if (!try_module_get(client->dev.driver->owner)) {
1758 				i2c_unregister_device(client);
1759 				module_put(dvb->i2c_client_demod->dev.driver->owner);
1760 				i2c_unregister_device(dvb->i2c_client_demod);
1761 				result = -ENODEV;
1762 				goto out_free;
1763 			}
1764 
1765 			dvb->i2c_client_tuner = client;
1766 			dvb->fe[0]->ops.set_lna = em28xx_pctv_292e_set_lna;
1767 		}
1768 		break;
1769 	case EM28178_BOARD_TERRATEC_T2_STICK_HD:
1770 		{
1771 			struct i2c_adapter *adapter;
1772 			struct i2c_client *client;
1773 			struct i2c_board_info info;
1774 			struct si2168_config si2168_config;
1775 			struct si2157_config si2157_config;
1776 
1777 			/* attach demod */
1778 			memset(&si2168_config, 0, sizeof(si2168_config));
1779 			si2168_config.i2c_adapter = &adapter;
1780 			si2168_config.fe = &dvb->fe[0];
1781 			si2168_config.ts_mode = SI2168_TS_PARALLEL;
1782 			memset(&info, 0, sizeof(struct i2c_board_info));
1783 			strlcpy(info.type, "si2168", I2C_NAME_SIZE);
1784 			info.addr = 0x64;
1785 			info.platform_data = &si2168_config;
1786 			request_module(info.type);
1787 			client = i2c_new_device(&dev->i2c_adap[dev->def_i2c_bus], &info);
1788 			if (client == NULL || client->dev.driver == NULL) {
1789 				result = -ENODEV;
1790 				goto out_free;
1791 			}
1792 
1793 			if (!try_module_get(client->dev.driver->owner)) {
1794 				i2c_unregister_device(client);
1795 				result = -ENODEV;
1796 				goto out_free;
1797 			}
1798 
1799 			dvb->i2c_client_demod = client;
1800 
1801 			/* attach tuner */
1802 			memset(&si2157_config, 0, sizeof(si2157_config));
1803 			si2157_config.fe = dvb->fe[0];
1804 			si2157_config.if_port = 0;
1805 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
1806 			si2157_config.mdev = dev->media_dev;
1807 #endif
1808 			memset(&info, 0, sizeof(struct i2c_board_info));
1809 			strlcpy(info.type, "si2146", I2C_NAME_SIZE);
1810 			info.addr = 0x60;
1811 			info.platform_data = &si2157_config;
1812 			request_module("si2157");
1813 			client = i2c_new_device(adapter, &info);
1814 			if (client == NULL || client->dev.driver == NULL) {
1815 				module_put(dvb->i2c_client_demod->dev.driver->owner);
1816 				i2c_unregister_device(dvb->i2c_client_demod);
1817 				result = -ENODEV;
1818 				goto out_free;
1819 			}
1820 
1821 			if (!try_module_get(client->dev.driver->owner)) {
1822 				i2c_unregister_device(client);
1823 				module_put(dvb->i2c_client_demod->dev.driver->owner);
1824 				i2c_unregister_device(dvb->i2c_client_demod);
1825 				result = -ENODEV;
1826 				goto out_free;
1827 			}
1828 
1829 			dvb->i2c_client_tuner = client;
1830 		}
1831 		break;
1832 
1833 	case EM28178_BOARD_PLEX_PX_BCUD:
1834 		{
1835 			struct i2c_client *client;
1836 			struct i2c_board_info info;
1837 			struct tc90522_config tc90522_config;
1838 			struct qm1d1c0042_config qm1d1c0042_config;
1839 
1840 			/* attach demod */
1841 			memset(&tc90522_config, 0, sizeof(tc90522_config));
1842 			memset(&info, 0, sizeof(struct i2c_board_info));
1843 			strlcpy(info.type, "tc90522sat", I2C_NAME_SIZE);
1844 			info.addr = 0x15;
1845 			info.platform_data = &tc90522_config;
1846 			request_module("tc90522");
1847 			client = i2c_new_device(&dev->i2c_adap[dev->def_i2c_bus], &info);
1848 			if (client == NULL || client->dev.driver == NULL) {
1849 				result = -ENODEV;
1850 				goto out_free;
1851 			}
1852 			dvb->i2c_client_demod = client;
1853 			if (!try_module_get(client->dev.driver->owner)) {
1854 				i2c_unregister_device(client);
1855 				result = -ENODEV;
1856 				goto out_free;
1857 			}
1858 
1859 			/* attach tuner */
1860 			memset(&qm1d1c0042_config, 0,
1861 			       sizeof(qm1d1c0042_config));
1862 			qm1d1c0042_config.fe = tc90522_config.fe;
1863 			qm1d1c0042_config.lpf = 1;
1864 			memset(&info, 0, sizeof(struct i2c_board_info));
1865 			strlcpy(info.type, "qm1d1c0042", I2C_NAME_SIZE);
1866 			info.addr = 0x61;
1867 			info.platform_data = &qm1d1c0042_config;
1868 			request_module(info.type);
1869 			client = i2c_new_device(tc90522_config.tuner_i2c,
1870 						&info);
1871 			if (client == NULL || client->dev.driver == NULL) {
1872 				module_put(dvb->i2c_client_demod->dev.driver->owner);
1873 				i2c_unregister_device(dvb->i2c_client_demod);
1874 				result = -ENODEV;
1875 				goto out_free;
1876 			}
1877 			dvb->i2c_client_tuner = client;
1878 			if (!try_module_get(client->dev.driver->owner)) {
1879 				i2c_unregister_device(client);
1880 				module_put(dvb->i2c_client_demod->dev.driver->owner);
1881 				i2c_unregister_device(dvb->i2c_client_demod);
1882 				result = -ENODEV;
1883 				goto out_free;
1884 			}
1885 			dvb->fe[0] = tc90522_config.fe;
1886 			px_bcud_init(dev);
1887 		}
1888 		break;
1889 	case EM28174_BOARD_HAUPPAUGE_WINTV_DUALHD_DVB:
1890 		{
1891 			struct i2c_adapter *adapter;
1892 			struct i2c_client *client;
1893 			struct i2c_board_info info;
1894 			struct si2168_config si2168_config;
1895 			struct si2157_config si2157_config;
1896 
1897 			/* attach demod */
1898 			memset(&si2168_config, 0, sizeof(si2168_config));
1899 			si2168_config.i2c_adapter = &adapter;
1900 			si2168_config.fe = &dvb->fe[0];
1901 			si2168_config.ts_mode = SI2168_TS_SERIAL;
1902 			memset(&info, 0, sizeof(struct i2c_board_info));
1903 			strlcpy(info.type, "si2168", I2C_NAME_SIZE);
1904 			info.addr = 0x64;
1905 			info.platform_data = &si2168_config;
1906 			request_module(info.type);
1907 			client = i2c_new_device(&dev->i2c_adap[dev->def_i2c_bus], &info);
1908 			if (client == NULL || client->dev.driver == NULL) {
1909 				result = -ENODEV;
1910 				goto out_free;
1911 			}
1912 
1913 			if (!try_module_get(client->dev.driver->owner)) {
1914 				i2c_unregister_device(client);
1915 				result = -ENODEV;
1916 				goto out_free;
1917 			}
1918 
1919 			dvb->i2c_client_demod = client;
1920 
1921 			/* attach tuner */
1922 			memset(&si2157_config, 0, sizeof(si2157_config));
1923 			si2157_config.fe = dvb->fe[0];
1924 			si2157_config.if_port = 1;
1925 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
1926 			si2157_config.mdev = dev->media_dev;
1927 #endif
1928 			memset(&info, 0, sizeof(struct i2c_board_info));
1929 			strlcpy(info.type, "si2157", I2C_NAME_SIZE);
1930 			info.addr = 0x60;
1931 			info.platform_data = &si2157_config;
1932 			request_module(info.type);
1933 			client = i2c_new_device(adapter, &info);
1934 			if (client == NULL || client->dev.driver == NULL) {
1935 				module_put(dvb->i2c_client_demod->dev.driver->owner);
1936 				i2c_unregister_device(dvb->i2c_client_demod);
1937 				result = -ENODEV;
1938 				goto out_free;
1939 			}
1940 
1941 			if (!try_module_get(client->dev.driver->owner)) {
1942 				i2c_unregister_device(client);
1943 				module_put(dvb->i2c_client_demod->dev.driver->owner);
1944 				i2c_unregister_device(dvb->i2c_client_demod);
1945 				result = -ENODEV;
1946 				goto out_free;
1947 			}
1948 
1949 			dvb->i2c_client_tuner = client;
1950 
1951 		}
1952 		break;
1953 	default:
1954 		dev_err(&dev->intf->dev,
1955 			"The frontend of your DVB/ATSC card isn't supported yet\n");
1956 		break;
1957 	}
1958 	if (NULL == dvb->fe[0]) {
1959 		dev_err(&dev->intf->dev, "frontend initialization failed\n");
1960 		result = -EINVAL;
1961 		goto out_free;
1962 	}
1963 	/* define general-purpose callback pointer */
1964 	dvb->fe[0]->callback = em28xx_tuner_callback;
1965 	if (dvb->fe[1])
1966 		dvb->fe[1]->callback = em28xx_tuner_callback;
1967 
1968 	/* register everything */
1969 	result = em28xx_register_dvb(dvb, THIS_MODULE, dev, &dev->intf->dev);
1970 
1971 	if (result < 0)
1972 		goto out_free;
1973 
1974 	dev_info(&dev->intf->dev, "DVB extension successfully initialized\n");
1975 
1976 	kref_get(&dev->ref);
1977 
1978 ret:
1979 	em28xx_set_mode(dev, EM28XX_SUSPEND);
1980 	mutex_unlock(&dev->lock);
1981 	return result;
1982 
1983 out_free:
1984 	kfree(dvb);
1985 	dev->dvb = NULL;
1986 	goto ret;
1987 }
1988 
1989 static inline void prevent_sleep(struct dvb_frontend_ops *ops)
1990 {
1991 	ops->set_voltage = NULL;
1992 	ops->sleep = NULL;
1993 	ops->tuner_ops.sleep = NULL;
1994 }
1995 
1996 static int em28xx_dvb_fini(struct em28xx *dev)
1997 {
1998 	struct em28xx_dvb *dvb;
1999 	struct i2c_client *client;
2000 
2001 	if (dev->is_audio_only) {
2002 		/* Shouldn't initialize IR for this interface */
2003 		return 0;
2004 	}
2005 
2006 	if (!dev->board.has_dvb) {
2007 		/* This device does not support the extension */
2008 		return 0;
2009 	}
2010 
2011 	if (!dev->dvb)
2012 		return 0;
2013 
2014 	dev_info(&dev->intf->dev, "Closing DVB extension\n");
2015 
2016 	dvb = dev->dvb;
2017 
2018 	em28xx_uninit_usb_xfer(dev, EM28XX_DIGITAL_MODE);
2019 
2020 	if (dev->disconnected) {
2021 		/* We cannot tell the device to sleep
2022 		 * once it has been unplugged. */
2023 		if (dvb->fe[0]) {
2024 			prevent_sleep(&dvb->fe[0]->ops);
2025 			dvb->fe[0]->exit = DVB_FE_DEVICE_REMOVED;
2026 		}
2027 		if (dvb->fe[1]) {
2028 			prevent_sleep(&dvb->fe[1]->ops);
2029 			dvb->fe[1]->exit = DVB_FE_DEVICE_REMOVED;
2030 		}
2031 	}
2032 
2033 	/* remove I2C SEC */
2034 	client = dvb->i2c_client_sec;
2035 	if (client) {
2036 		module_put(client->dev.driver->owner);
2037 		i2c_unregister_device(client);
2038 	}
2039 
2040 	/* remove I2C tuner */
2041 	client = dvb->i2c_client_tuner;
2042 	if (client) {
2043 		module_put(client->dev.driver->owner);
2044 		i2c_unregister_device(client);
2045 	}
2046 
2047 	/* remove I2C demod */
2048 	client = dvb->i2c_client_demod;
2049 	if (client) {
2050 		module_put(client->dev.driver->owner);
2051 		i2c_unregister_device(client);
2052 	}
2053 
2054 	em28xx_unregister_dvb(dvb);
2055 	kfree(dvb);
2056 	dev->dvb = NULL;
2057 	kref_put(&dev->ref, em28xx_free_device);
2058 
2059 	return 0;
2060 }
2061 
2062 static int em28xx_dvb_suspend(struct em28xx *dev)
2063 {
2064 	int ret = 0;
2065 
2066 	if (dev->is_audio_only)
2067 		return 0;
2068 
2069 	if (!dev->board.has_dvb)
2070 		return 0;
2071 
2072 	dev_info(&dev->intf->dev, "Suspending DVB extension\n");
2073 	if (dev->dvb) {
2074 		struct em28xx_dvb *dvb = dev->dvb;
2075 
2076 		if (dvb->fe[0]) {
2077 			ret = dvb_frontend_suspend(dvb->fe[0]);
2078 			dev_info(&dev->intf->dev, "fe0 suspend %d\n", ret);
2079 		}
2080 		if (dvb->fe[1]) {
2081 			dvb_frontend_suspend(dvb->fe[1]);
2082 			dev_info(&dev->intf->dev, "fe1 suspend %d\n", ret);
2083 		}
2084 	}
2085 
2086 	return 0;
2087 }
2088 
2089 static int em28xx_dvb_resume(struct em28xx *dev)
2090 {
2091 	int ret = 0;
2092 
2093 	if (dev->is_audio_only)
2094 		return 0;
2095 
2096 	if (!dev->board.has_dvb)
2097 		return 0;
2098 
2099 	dev_info(&dev->intf->dev, "Resuming DVB extension\n");
2100 	if (dev->dvb) {
2101 		struct em28xx_dvb *dvb = dev->dvb;
2102 
2103 		if (dvb->fe[0]) {
2104 			ret = dvb_frontend_resume(dvb->fe[0]);
2105 			dev_info(&dev->intf->dev, "fe0 resume %d\n", ret);
2106 		}
2107 
2108 		if (dvb->fe[1]) {
2109 			ret = dvb_frontend_resume(dvb->fe[1]);
2110 			dev_info(&dev->intf->dev, "fe1 resume %d\n", ret);
2111 		}
2112 	}
2113 
2114 	return 0;
2115 }
2116 
2117 static struct em28xx_ops dvb_ops = {
2118 	.id   = EM28XX_DVB,
2119 	.name = "Em28xx dvb Extension",
2120 	.init = em28xx_dvb_init,
2121 	.fini = em28xx_dvb_fini,
2122 	.suspend = em28xx_dvb_suspend,
2123 	.resume = em28xx_dvb_resume,
2124 };
2125 
2126 static int __init em28xx_dvb_register(void)
2127 {
2128 	return em28xx_register_extension(&dvb_ops);
2129 }
2130 
2131 static void __exit em28xx_dvb_unregister(void)
2132 {
2133 	em28xx_unregister_extension(&dvb_ops);
2134 }
2135 
2136 module_init(em28xx_dvb_register);
2137 module_exit(em28xx_dvb_unregister);
2138