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