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