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