xref: /linux/drivers/media/usb/em28xx/em28xx-dvb.c (revision ec573362afa202acca640629b321ac09b6911448)
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 <linux/kernel.h>
25 #include <linux/slab.h>
26 #include <linux/usb.h>
27 
28 #include "em28xx.h"
29 #include <media/v4l2-common.h>
30 #include <dvb_demux.h>
31 #include <dvb_net.h>
32 #include <dmxdev.h>
33 #include <media/tuner.h>
34 #include "tuner-simple.h"
35 #include <linux/gpio.h>
36 
37 #include "lgdt330x.h"
38 #include "lgdt3305.h"
39 #include "zl10353.h"
40 #include "s5h1409.h"
41 #include "mt352.h"
42 #include "mt352_priv.h" /* FIXME */
43 #include "tda1002x.h"
44 #include "tda18271.h"
45 #include "s921.h"
46 #include "drxd.h"
47 #include "cxd2820r.h"
48 #include "tda18271c2dd.h"
49 #include "drxk.h"
50 #include "tda10071.h"
51 #include "a8293.h"
52 #include "qt1010.h"
53 #include "mb86a20s.h"
54 #include "m88ds3103.h"
55 #include "m88ts2022.h"
56 
57 MODULE_DESCRIPTION("driver for em28xx based DVB cards");
58 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
59 MODULE_LICENSE("GPL");
60 
61 static unsigned int debug;
62 module_param(debug, int, 0644);
63 MODULE_PARM_DESC(debug, "enable debug messages [dvb]");
64 
65 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
66 
67 #define dprintk(level, fmt, arg...) do {			\
68 if (debug >= level) 						\
69 	printk(KERN_DEBUG "%s/2-dvb: " fmt, dev->name, ## arg);	\
70 } while (0)
71 
72 struct em28xx_dvb {
73 	struct dvb_frontend        *fe[2];
74 
75 	/* feed count management */
76 	struct mutex               lock;
77 	int                        nfeeds;
78 
79 	/* general boilerplate stuff */
80 	struct dvb_adapter         adapter;
81 	struct dvb_demux           demux;
82 	struct dmxdev              dmxdev;
83 	struct dmx_frontend        fe_hw;
84 	struct dmx_frontend        fe_mem;
85 	struct dvb_net             net;
86 
87 	/* Due to DRX-K - probably need changes */
88 	int (*gate_ctrl)(struct dvb_frontend *, int);
89 	struct semaphore      pll_mutex;
90 	bool			dont_attach_fe1;
91 	int			lna_gpio;
92 };
93 
94 
95 static inline void print_err_status(struct em28xx *dev,
96 				     int packet, int status)
97 {
98 	char *errmsg = "Unknown";
99 
100 	switch (status) {
101 	case -ENOENT:
102 		errmsg = "unlinked synchronuously";
103 		break;
104 	case -ECONNRESET:
105 		errmsg = "unlinked asynchronuously";
106 		break;
107 	case -ENOSR:
108 		errmsg = "Buffer error (overrun)";
109 		break;
110 	case -EPIPE:
111 		errmsg = "Stalled (device not responding)";
112 		break;
113 	case -EOVERFLOW:
114 		errmsg = "Babble (bad cable?)";
115 		break;
116 	case -EPROTO:
117 		errmsg = "Bit-stuff error (bad cable?)";
118 		break;
119 	case -EILSEQ:
120 		errmsg = "CRC/Timeout (could be anything)";
121 		break;
122 	case -ETIME:
123 		errmsg = "Device does not respond";
124 		break;
125 	}
126 	if (packet < 0) {
127 		dprintk(1, "URB status %d [%s].\n", status, errmsg);
128 	} else {
129 		dprintk(1, "URB packet %d, status %d [%s].\n",
130 			packet, status, errmsg);
131 	}
132 }
133 
134 static inline int em28xx_dvb_urb_data_copy(struct em28xx *dev, struct urb *urb)
135 {
136 	int xfer_bulk, num_packets, i;
137 
138 	if (!dev)
139 		return 0;
140 
141 	if (dev->disconnected)
142 		return 0;
143 
144 	if (urb->status < 0)
145 		print_err_status(dev, -1, urb->status);
146 
147 	xfer_bulk = usb_pipebulk(urb->pipe);
148 
149 	if (xfer_bulk) /* bulk */
150 		num_packets = 1;
151 	else /* isoc */
152 		num_packets = urb->number_of_packets;
153 
154 	for (i = 0; i < num_packets; i++) {
155 		if (xfer_bulk) {
156 			if (urb->status < 0) {
157 				print_err_status(dev, i, urb->status);
158 				if (urb->status != -EPROTO)
159 					continue;
160 			}
161 			dvb_dmx_swfilter(&dev->dvb->demux, urb->transfer_buffer,
162 					urb->actual_length);
163 		} else {
164 			if (urb->iso_frame_desc[i].status < 0) {
165 				print_err_status(dev, i,
166 						 urb->iso_frame_desc[i].status);
167 				if (urb->iso_frame_desc[i].status != -EPROTO)
168 					continue;
169 			}
170 			dvb_dmx_swfilter(&dev->dvb->demux,
171 					 urb->transfer_buffer +
172 					 urb->iso_frame_desc[i].offset,
173 					 urb->iso_frame_desc[i].actual_length);
174 		}
175 	}
176 
177 	return 0;
178 }
179 
180 static int em28xx_start_streaming(struct em28xx_dvb *dvb)
181 {
182 	int rc;
183 	struct em28xx_i2c_bus *i2c_bus = dvb->adapter.priv;
184 	struct em28xx *dev = i2c_bus->dev;
185 	int dvb_max_packet_size, packet_multiplier, dvb_alt;
186 
187 	if (dev->dvb_xfer_bulk) {
188 		if (!dev->dvb_ep_bulk)
189 			return -ENODEV;
190 		dvb_max_packet_size = 512; /* USB 2.0 spec */
191 		packet_multiplier = EM28XX_DVB_BULK_PACKET_MULTIPLIER;
192 		dvb_alt = 0;
193 	} else { /* isoc */
194 		if (!dev->dvb_ep_isoc)
195 			return -ENODEV;
196 		dvb_max_packet_size = dev->dvb_max_pkt_size_isoc;
197 		if (dvb_max_packet_size < 0)
198 			return dvb_max_packet_size;
199 		packet_multiplier = EM28XX_DVB_NUM_ISOC_PACKETS;
200 		dvb_alt = dev->dvb_alt_isoc;
201 	}
202 
203 	usb_set_interface(dev->udev, 0, dvb_alt);
204 	rc = em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
205 	if (rc < 0)
206 		return rc;
207 
208 	dprintk(1, "Using %d buffers each with %d x %d bytes\n",
209 		EM28XX_DVB_NUM_BUFS,
210 		packet_multiplier,
211 		dvb_max_packet_size);
212 
213 	return em28xx_init_usb_xfer(dev, EM28XX_DIGITAL_MODE,
214 				    dev->dvb_xfer_bulk,
215 				    EM28XX_DVB_NUM_BUFS,
216 				    dvb_max_packet_size,
217 				    packet_multiplier,
218 				    em28xx_dvb_urb_data_copy);
219 }
220 
221 static int em28xx_stop_streaming(struct em28xx_dvb *dvb)
222 {
223 	struct em28xx_i2c_bus *i2c_bus = dvb->adapter.priv;
224 	struct em28xx *dev = i2c_bus->dev;
225 
226 	em28xx_stop_urbs(dev);
227 
228 	return 0;
229 }
230 
231 static int em28xx_start_feed(struct dvb_demux_feed *feed)
232 {
233 	struct dvb_demux *demux  = feed->demux;
234 	struct em28xx_dvb *dvb = demux->priv;
235 	int rc, ret;
236 
237 	if (!demux->dmx.frontend)
238 		return -EINVAL;
239 
240 	mutex_lock(&dvb->lock);
241 	dvb->nfeeds++;
242 	rc = dvb->nfeeds;
243 
244 	if (dvb->nfeeds == 1) {
245 		ret = em28xx_start_streaming(dvb);
246 		if (ret < 0)
247 			rc = ret;
248 	}
249 
250 	mutex_unlock(&dvb->lock);
251 	return rc;
252 }
253 
254 static int em28xx_stop_feed(struct dvb_demux_feed *feed)
255 {
256 	struct dvb_demux *demux  = feed->demux;
257 	struct em28xx_dvb *dvb = demux->priv;
258 	int err = 0;
259 
260 	mutex_lock(&dvb->lock);
261 	dvb->nfeeds--;
262 
263 	if (0 == dvb->nfeeds)
264 		err = em28xx_stop_streaming(dvb);
265 
266 	mutex_unlock(&dvb->lock);
267 	return err;
268 }
269 
270 
271 
272 /* ------------------------------------------------------------------ */
273 static int em28xx_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
274 {
275 	struct em28xx_i2c_bus *i2c_bus = fe->dvb->priv;
276         struct em28xx *dev = i2c_bus->dev;
277 
278 	if (acquire)
279 		return em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
280 	else
281 		return em28xx_set_mode(dev, EM28XX_SUSPEND);
282 }
283 
284 /* ------------------------------------------------------------------ */
285 
286 static struct lgdt330x_config em2880_lgdt3303_dev = {
287 	.demod_address = 0x0e,
288 	.demod_chip = LGDT3303,
289 };
290 
291 static struct lgdt3305_config em2870_lgdt3304_dev = {
292 	.i2c_addr           = 0x0e,
293 	.demod_chip         = LGDT3304,
294 	.spectral_inversion = 1,
295 	.deny_i2c_rptr      = 1,
296 	.mpeg_mode          = LGDT3305_MPEG_PARALLEL,
297 	.tpclk_edge         = LGDT3305_TPCLK_FALLING_EDGE,
298 	.tpvalid_polarity   = LGDT3305_TP_VALID_HIGH,
299 	.vsb_if_khz         = 3250,
300 	.qam_if_khz         = 4000,
301 };
302 
303 static struct lgdt3305_config em2874_lgdt3305_dev = {
304 	.i2c_addr           = 0x0e,
305 	.demod_chip         = LGDT3305,
306 	.spectral_inversion = 1,
307 	.deny_i2c_rptr      = 0,
308 	.mpeg_mode          = LGDT3305_MPEG_SERIAL,
309 	.tpclk_edge         = LGDT3305_TPCLK_FALLING_EDGE,
310 	.tpvalid_polarity   = LGDT3305_TP_VALID_HIGH,
311 	.vsb_if_khz         = 3250,
312 	.qam_if_khz         = 4000,
313 };
314 
315 static struct s921_config sharp_isdbt = {
316 	.demod_address = 0x30 >> 1
317 };
318 
319 static struct zl10353_config em28xx_zl10353_with_xc3028 = {
320 	.demod_address = (0x1e >> 1),
321 	.no_tuner = 1,
322 	.parallel_ts = 1,
323 	.if2 = 45600,
324 };
325 
326 static struct s5h1409_config em28xx_s5h1409_with_xc3028 = {
327 	.demod_address = 0x32 >> 1,
328 	.output_mode   = S5H1409_PARALLEL_OUTPUT,
329 	.gpio          = S5H1409_GPIO_OFF,
330 	.inversion     = S5H1409_INVERSION_OFF,
331 	.status_mode   = S5H1409_DEMODLOCKING,
332 	.mpeg_timing   = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK
333 };
334 
335 static struct tda18271_std_map kworld_a340_std_map = {
336 	.atsc_6   = { .if_freq = 3250, .agc_mode = 3, .std = 0,
337 		      .if_lvl = 1, .rfagc_top = 0x37, },
338 	.qam_6    = { .if_freq = 4000, .agc_mode = 3, .std = 1,
339 		      .if_lvl = 1, .rfagc_top = 0x37, },
340 };
341 
342 static struct tda18271_config kworld_a340_config = {
343 	.std_map           = &kworld_a340_std_map,
344 };
345 
346 static struct tda18271_config kworld_ub435q_v2_config = {
347 	.std_map	= &kworld_a340_std_map,
348 	.gate		= TDA18271_GATE_DIGITAL,
349 };
350 
351 static struct zl10353_config em28xx_zl10353_xc3028_no_i2c_gate = {
352 	.demod_address = (0x1e >> 1),
353 	.no_tuner = 1,
354 	.disable_i2c_gate_ctrl = 1,
355 	.parallel_ts = 1,
356 	.if2 = 45600,
357 };
358 
359 static struct drxd_config em28xx_drxd = {
360 	.demod_address = 0x70,
361 	.demod_revision = 0xa2,
362 	.pll_type = DRXD_PLL_NONE,
363 	.clock = 12000,
364 	.insert_rs_byte = 1,
365 	.IF = 42800000,
366 	.disable_i2c_gate_ctrl = 1,
367 };
368 
369 static struct drxk_config terratec_h5_drxk = {
370 	.adr = 0x29,
371 	.single_master = 1,
372 	.no_i2c_bridge = 1,
373 	.microcode_name = "dvb-usb-terratec-h5-drxk.fw",
374 	.qam_demod_parameter_count = 2,
375 	.load_firmware_sync = true,
376 };
377 
378 static struct drxk_config hauppauge_930c_drxk = {
379 	.adr = 0x29,
380 	.single_master = 1,
381 	.no_i2c_bridge = 1,
382 	.microcode_name = "dvb-usb-hauppauge-hvr930c-drxk.fw",
383 	.chunk_size = 56,
384 	.qam_demod_parameter_count = 2,
385 	.load_firmware_sync = true,
386 };
387 
388 static struct drxk_config terratec_htc_stick_drxk = {
389 	.adr = 0x29,
390 	.single_master = 1,
391 	.no_i2c_bridge = 1,
392 	.microcode_name = "dvb-usb-terratec-htc-stick-drxk.fw",
393 	.chunk_size = 54,
394 	.qam_demod_parameter_count = 2,
395 	/* Required for the antenna_gpio to disable LNA. */
396 	.antenna_dvbt = true,
397 	/* The windows driver uses the same. This will disable LNA. */
398 	.antenna_gpio = 0x6,
399 	.load_firmware_sync = true,
400 };
401 
402 static struct drxk_config maxmedia_ub425_tc_drxk = {
403 	.adr = 0x29,
404 	.single_master = 1,
405 	.no_i2c_bridge = 1,
406 	.microcode_name = "dvb-demod-drxk-01.fw",
407 	.chunk_size = 62,
408 	.load_firmware_sync = true,
409 	.qam_demod_parameter_count = 2,
410 };
411 
412 static struct drxk_config pctv_520e_drxk = {
413 	.adr = 0x29,
414 	.single_master = 1,
415 	.microcode_name = "dvb-demod-drxk-pctv.fw",
416 	.qam_demod_parameter_count = 2,
417 	.chunk_size = 58,
418 	.antenna_dvbt = true, /* disable LNA */
419 	.antenna_gpio = (1 << 2), /* disable LNA */
420 	.load_firmware_sync = true,
421 };
422 
423 static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
424 {
425 	struct em28xx_dvb *dvb = fe->sec_priv;
426 	int status;
427 
428 	if (!dvb)
429 		return -EINVAL;
430 
431 	if (enable) {
432 		down(&dvb->pll_mutex);
433 		status = dvb->gate_ctrl(fe, 1);
434 	} else {
435 		status = dvb->gate_ctrl(fe, 0);
436 		up(&dvb->pll_mutex);
437 	}
438 	return status;
439 }
440 
441 static void hauppauge_hvr930c_init(struct em28xx *dev)
442 {
443 	int i;
444 
445 	struct em28xx_reg_seq hauppauge_hvr930c_init[] = {
446 		{EM2874_R80_GPIO_P0_CTRL,	0xff,	0xff,	0x65},
447 		{EM2874_R80_GPIO_P0_CTRL,	0xfb,	0xff,	0x32},
448 		{EM2874_R80_GPIO_P0_CTRL,	0xff,	0xff,	0xb8},
449 		{	-1,			-1,	-1,	-1},
450 	};
451 	struct em28xx_reg_seq hauppauge_hvr930c_end[] = {
452 		{EM2874_R80_GPIO_P0_CTRL,	0xef,	0xff,	0x01},
453 		{EM2874_R80_GPIO_P0_CTRL,	0xaf,	0xff,	0x65},
454 		{EM2874_R80_GPIO_P0_CTRL,	0xef,	0xff,	0x76},
455 		{EM2874_R80_GPIO_P0_CTRL,	0xef,	0xff,	0x01},
456 		{EM2874_R80_GPIO_P0_CTRL,	0xcf,	0xff,	0x0b},
457 		{EM2874_R80_GPIO_P0_CTRL,	0xef,	0xff,	0x40},
458 
459 		{EM2874_R80_GPIO_P0_CTRL,	0xcf,	0xff,	0x65},
460 		{EM2874_R80_GPIO_P0_CTRL,	0xef,	0xff,	0x65},
461 		{EM2874_R80_GPIO_P0_CTRL,	0xcf,	0xff,	0x0b},
462 		{EM2874_R80_GPIO_P0_CTRL,	0xef,	0xff,	0x65},
463 
464 		{	-1,			-1,	-1,	-1},
465 	};
466 
467 	struct {
468 		unsigned char r[4];
469 		int len;
470 	} regs[] = {
471 		{{ 0x06, 0x02, 0x00, 0x31 }, 4},
472 		{{ 0x01, 0x02 }, 2},
473 		{{ 0x01, 0x02, 0x00, 0xc6 }, 4},
474 		{{ 0x01, 0x00 }, 2},
475 		{{ 0x01, 0x00, 0xff, 0xaf }, 4},
476 		{{ 0x01, 0x00, 0x03, 0xa0 }, 4},
477 		{{ 0x01, 0x00 }, 2},
478 		{{ 0x01, 0x00, 0x73, 0xaf }, 4},
479 		{{ 0x04, 0x00 }, 2},
480 		{{ 0x00, 0x04 }, 2},
481 		{{ 0x00, 0x04, 0x00, 0x0a }, 4},
482 		{{ 0x04, 0x14 }, 2},
483 		{{ 0x04, 0x14, 0x00, 0x00 }, 4},
484 	};
485 
486 	em28xx_gpio_set(dev, hauppauge_hvr930c_init);
487 	em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
488 	msleep(10);
489 	em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
490 	msleep(10);
491 
492 	dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1;
493 
494 	for (i = 0; i < ARRAY_SIZE(regs); i++)
495 		i2c_master_send(&dev->i2c_client[dev->def_i2c_bus], regs[i].r, regs[i].len);
496 	em28xx_gpio_set(dev, hauppauge_hvr930c_end);
497 
498 	msleep(100);
499 
500 	em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
501 	msleep(30);
502 
503 	em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x45);
504 	msleep(10);
505 
506 }
507 
508 static void terratec_h5_init(struct em28xx *dev)
509 {
510 	int i;
511 	struct em28xx_reg_seq terratec_h5_init[] = {
512 		{EM2820_R08_GPIO_CTRL,		0xff,	0xff,	10},
513 		{EM2874_R80_GPIO_P0_CTRL,	0xf6,	0xff,	100},
514 		{EM2874_R80_GPIO_P0_CTRL,	0xf2,	0xff,	50},
515 		{EM2874_R80_GPIO_P0_CTRL,	0xf6,	0xff,	100},
516 		{	-1,			-1,	-1,	-1},
517 	};
518 	struct em28xx_reg_seq terratec_h5_end[] = {
519 		{EM2874_R80_GPIO_P0_CTRL,	0xe6,	0xff,	100},
520 		{EM2874_R80_GPIO_P0_CTRL,	0xa6,	0xff,	50},
521 		{EM2874_R80_GPIO_P0_CTRL,	0xe6,	0xff,	100},
522 		{	-1,			-1,	-1,	-1},
523 	};
524 	struct {
525 		unsigned char r[4];
526 		int len;
527 	} regs[] = {
528 		{{ 0x06, 0x02, 0x00, 0x31 }, 4},
529 		{{ 0x01, 0x02 }, 2},
530 		{{ 0x01, 0x02, 0x00, 0xc6 }, 4},
531 		{{ 0x01, 0x00 }, 2},
532 		{{ 0x01, 0x00, 0xff, 0xaf }, 4},
533 		{{ 0x01, 0x00, 0x03, 0xa0 }, 4},
534 		{{ 0x01, 0x00 }, 2},
535 		{{ 0x01, 0x00, 0x73, 0xaf }, 4},
536 		{{ 0x04, 0x00 }, 2},
537 		{{ 0x00, 0x04 }, 2},
538 		{{ 0x00, 0x04, 0x00, 0x0a }, 4},
539 		{{ 0x04, 0x14 }, 2},
540 		{{ 0x04, 0x14, 0x00, 0x00 }, 4},
541 	};
542 
543 	em28xx_gpio_set(dev, terratec_h5_init);
544 	em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
545 	msleep(10);
546 	em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x45);
547 	msleep(10);
548 
549 	dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1;
550 
551 	for (i = 0; i < ARRAY_SIZE(regs); i++)
552 		i2c_master_send(&dev->i2c_client[dev->def_i2c_bus], regs[i].r, regs[i].len);
553 	em28xx_gpio_set(dev, terratec_h5_end);
554 };
555 
556 static void terratec_htc_stick_init(struct em28xx *dev)
557 {
558 	int i;
559 
560 	/*
561 	 * GPIO configuration:
562 	 * 0xff: unknown (does not affect DVB-T).
563 	 * 0xf6: DRX-K (demodulator).
564 	 * 0xe6: unknown (does not affect DVB-T).
565 	 * 0xb6: unknown (does not affect DVB-T).
566 	 */
567 	struct em28xx_reg_seq terratec_htc_stick_init[] = {
568 		{EM2820_R08_GPIO_CTRL,		0xff,	0xff,	10},
569 		{EM2874_R80_GPIO_P0_CTRL,	0xf6,	0xff,	100},
570 		{EM2874_R80_GPIO_P0_CTRL,	0xe6,	0xff,	50},
571 		{EM2874_R80_GPIO_P0_CTRL,	0xf6,	0xff,	100},
572 		{	-1,			-1,	-1,	-1},
573 	};
574 	struct em28xx_reg_seq terratec_htc_stick_end[] = {
575 		{EM2874_R80_GPIO_P0_CTRL,	0xb6,	0xff,	100},
576 		{EM2874_R80_GPIO_P0_CTRL,	0xf6,	0xff,	50},
577 		{	-1,			-1,	-1,	-1},
578 	};
579 
580 	/*
581 	 * Init the analog decoder (not yet supported), but
582 	 * it's probably still a good idea.
583 	 */
584 	struct {
585 		unsigned char r[4];
586 		int len;
587 	} regs[] = {
588 		{{ 0x06, 0x02, 0x00, 0x31 }, 4},
589 		{{ 0x01, 0x02 }, 2},
590 		{{ 0x01, 0x02, 0x00, 0xc6 }, 4},
591 		{{ 0x01, 0x00 }, 2},
592 		{{ 0x01, 0x00, 0xff, 0xaf }, 4},
593 	};
594 
595 	em28xx_gpio_set(dev, terratec_htc_stick_init);
596 
597 	em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
598 	msleep(10);
599 	em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
600 	msleep(10);
601 
602 	dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1;
603 
604 	for (i = 0; i < ARRAY_SIZE(regs); i++)
605 		i2c_master_send(&dev->i2c_client[dev->def_i2c_bus], regs[i].r, regs[i].len);
606 
607 	em28xx_gpio_set(dev, terratec_htc_stick_end);
608 };
609 
610 static void terratec_htc_usb_xs_init(struct em28xx *dev)
611 {
612 	int i;
613 
614 	struct em28xx_reg_seq terratec_htc_usb_xs_init[] = {
615 		{EM2820_R08_GPIO_CTRL,		0xff,	0xff,	10},
616 		{EM2874_R80_GPIO_P0_CTRL,	0xb2,	0xff,	100},
617 		{EM2874_R80_GPIO_P0_CTRL,	0xb2,	0xff,	50},
618 		{EM2874_R80_GPIO_P0_CTRL,	0xb6,	0xff,	100},
619 		{	-1,			-1,	-1,	-1},
620 	};
621 	struct em28xx_reg_seq terratec_htc_usb_xs_end[] = {
622 		{EM2874_R80_GPIO_P0_CTRL,	0xa6,	0xff,	100},
623 		{EM2874_R80_GPIO_P0_CTRL,	0xa6,	0xff,	50},
624 		{EM2874_R80_GPIO_P0_CTRL,	0xe6,	0xff,	100},
625 		{	-1,			-1,	-1,	-1},
626 	};
627 
628 	/*
629 	 * Init the analog decoder (not yet supported), but
630 	 * it's probably still a good idea.
631 	 */
632 	struct {
633 		unsigned char r[4];
634 		int len;
635 	} regs[] = {
636 		{{ 0x06, 0x02, 0x00, 0x31 }, 4},
637 		{{ 0x01, 0x02 }, 2},
638 		{{ 0x01, 0x02, 0x00, 0xc6 }, 4},
639 		{{ 0x01, 0x00 }, 2},
640 		{{ 0x01, 0x00, 0xff, 0xaf }, 4},
641 		{{ 0x01, 0x00, 0x03, 0xa0 }, 4},
642 		{{ 0x01, 0x00 }, 2},
643 		{{ 0x01, 0x00, 0x73, 0xaf }, 4},
644 		{{ 0x04, 0x00 }, 2},
645 		{{ 0x00, 0x04 }, 2},
646 		{{ 0x00, 0x04, 0x00, 0x0a }, 4},
647 		{{ 0x04, 0x14 }, 2},
648 		{{ 0x04, 0x14, 0x00, 0x00 }, 4},
649 	};
650 
651 	em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
652 
653 	em28xx_gpio_set(dev, terratec_htc_usb_xs_init);
654 
655 	em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
656 	msleep(10);
657 	em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
658 	msleep(10);
659 
660 	dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1;
661 
662 	for (i = 0; i < ARRAY_SIZE(regs); i++)
663 		i2c_master_send(&dev->i2c_client[dev->def_i2c_bus], regs[i].r, regs[i].len);
664 
665 	em28xx_gpio_set(dev, terratec_htc_usb_xs_end);
666 };
667 
668 static void pctv_520e_init(struct em28xx *dev)
669 {
670 	/*
671 	 * Init AVF4910B analog decoder. Looks like I2C traffic to
672 	 * digital demodulator and tuner are routed via AVF4910B.
673 	 */
674 	int i;
675 	struct {
676 		unsigned char r[4];
677 		int len;
678 	} regs[] = {
679 		{{ 0x06, 0x02, 0x00, 0x31 }, 4},
680 		{{ 0x01, 0x02 }, 2},
681 		{{ 0x01, 0x02, 0x00, 0xc6 }, 4},
682 		{{ 0x01, 0x00 }, 2},
683 		{{ 0x01, 0x00, 0xff, 0xaf }, 4},
684 		{{ 0x01, 0x00, 0x03, 0xa0 }, 4},
685 		{{ 0x01, 0x00 }, 2},
686 		{{ 0x01, 0x00, 0x73, 0xaf }, 4},
687 	};
688 
689 	dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1; /* 0x41 */
690 
691 	for (i = 0; i < ARRAY_SIZE(regs); i++)
692 		i2c_master_send(&dev->i2c_client[dev->def_i2c_bus], regs[i].r, regs[i].len);
693 };
694 
695 static int em28xx_pctv_290e_set_lna(struct dvb_frontend *fe)
696 {
697 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
698 	struct em28xx *dev = fe->dvb->priv;
699 #ifdef CONFIG_GPIOLIB
700 	struct em28xx_dvb *dvb = dev->dvb;
701 	int ret;
702 	unsigned long flags;
703 
704 	if (c->lna == 1)
705 		flags = GPIOF_OUT_INIT_HIGH; /* enable LNA */
706 	else
707 		flags = GPIOF_OUT_INIT_LOW; /* disable LNA */
708 
709 	ret = gpio_request_one(dvb->lna_gpio, flags, NULL);
710 	if (ret)
711 		em28xx_errdev("gpio request failed %d\n", ret);
712 	else
713 		gpio_free(dvb->lna_gpio);
714 
715 	return ret;
716 #else
717 	dev_warn(&dev->udev->dev, "%s: LNA control is disabled (lna=%u)\n",
718 			KBUILD_MODNAME, c->lna);
719 	return 0;
720 #endif
721 }
722 
723 static int em28xx_mt352_terratec_xs_init(struct dvb_frontend *fe)
724 {
725 	/* Values extracted from a USB trace of the Terratec Windows driver */
726 	static u8 clock_config[]   = { CLOCK_CTL,  0x38, 0x2c };
727 	static u8 reset[]          = { RESET,      0x80 };
728 	static u8 adc_ctl_1_cfg[]  = { ADC_CTL_1,  0x40 };
729 	static u8 agc_cfg[]        = { AGC_TARGET, 0x28, 0xa0 };
730 	static u8 input_freq_cfg[] = { INPUT_FREQ_1, 0x31, 0xb8 };
731 	static u8 rs_err_cfg[]     = { RS_ERR_PER_1, 0x00, 0x4d };
732 	static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
733 	static u8 trl_nom_cfg[]    = { TRL_NOMINAL_RATE_1, 0x64, 0x00 };
734 	static u8 tps_given_cfg[]  = { TPS_GIVEN_1, 0x40, 0x80, 0x50 };
735 	static u8 tuner_go[]       = { TUNER_GO, 0x01};
736 
737 	mt352_write(fe, clock_config,   sizeof(clock_config));
738 	udelay(200);
739 	mt352_write(fe, reset,          sizeof(reset));
740 	mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
741 	mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
742 	mt352_write(fe, input_freq_cfg, sizeof(input_freq_cfg));
743 	mt352_write(fe, rs_err_cfg,     sizeof(rs_err_cfg));
744 	mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
745 	mt352_write(fe, trl_nom_cfg,    sizeof(trl_nom_cfg));
746 	mt352_write(fe, tps_given_cfg,  sizeof(tps_given_cfg));
747 	mt352_write(fe, tuner_go,       sizeof(tuner_go));
748 	return 0;
749 }
750 
751 static struct mt352_config terratec_xs_mt352_cfg = {
752 	.demod_address = (0x1e >> 1),
753 	.no_tuner = 1,
754 	.if2 = 45600,
755 	.demod_init = em28xx_mt352_terratec_xs_init,
756 };
757 
758 static struct tda10023_config em28xx_tda10023_config = {
759 	.demod_address = 0x0c,
760 	.invert = 1,
761 };
762 
763 static struct cxd2820r_config em28xx_cxd2820r_config = {
764 	.i2c_address = (0xd8 >> 1),
765 	.ts_mode = CXD2820R_TS_SERIAL,
766 };
767 
768 static struct tda18271_config em28xx_cxd2820r_tda18271_config = {
769 	.output_opt = TDA18271_OUTPUT_LT_OFF,
770 	.gate = TDA18271_GATE_DIGITAL,
771 };
772 
773 static const struct tda10071_config em28xx_tda10071_config = {
774 	.demod_i2c_addr = 0x55, /* (0xaa >> 1) */
775 	.tuner_i2c_addr = 0x14,
776 	.i2c_wr_max = 64,
777 	.ts_mode = TDA10071_TS_SERIAL,
778 	.spec_inv = 0,
779 	.xtal = 40444000, /* 40.444 MHz */
780 	.pll_multiplier = 20,
781 };
782 
783 static const struct a8293_config em28xx_a8293_config = {
784 	.i2c_addr = 0x08, /* (0x10 >> 1) */
785 };
786 
787 static struct zl10353_config em28xx_zl10353_no_i2c_gate_dev = {
788 	.demod_address = (0x1e >> 1),
789 	.disable_i2c_gate_ctrl = 1,
790 	.no_tuner = 1,
791 	.parallel_ts = 1,
792 };
793 static struct qt1010_config em28xx_qt1010_config = {
794 	.i2c_address = 0x62
795 };
796 
797 static const struct mb86a20s_config c3tech_duo_mb86a20s_config = {
798 	.demod_address = 0x10,
799 	.is_serial = true,
800 };
801 
802 static struct tda18271_std_map mb86a20s_tda18271_config = {
803 	.dvbt_6   = { .if_freq = 4000, .agc_mode = 3, .std = 4,
804 		      .if_lvl = 1, .rfagc_top = 0x37, },
805 };
806 
807 static struct tda18271_config c3tech_duo_tda18271_config = {
808 	.std_map = &mb86a20s_tda18271_config,
809 	.gate    = TDA18271_GATE_DIGITAL,
810 	.small_i2c = TDA18271_03_BYTE_CHUNK_INIT,
811 };
812 
813 static const struct m88ds3103_config pctv_461e_m88ds3103_config = {
814 	.i2c_addr = 0x68,
815 	.clock = 27000000,
816 	.i2c_wr_max = 33,
817 	.clock_out = 0,
818 	.ts_mode = M88DS3103_TS_PARALLEL_16,
819 	.agc = 0x99,
820 };
821 
822 static const struct m88ts2022_config em28xx_m88ts2022_config = {
823 	.i2c_addr = 0x60,
824 	.clock = 27000000,
825 };
826 
827 /* ------------------------------------------------------------------ */
828 
829 static int em28xx_attach_xc3028(u8 addr, struct em28xx *dev)
830 {
831 	struct dvb_frontend *fe;
832 	struct xc2028_config cfg;
833 
834 	memset(&cfg, 0, sizeof(cfg));
835 	cfg.i2c_adap  = &dev->i2c_adap[dev->def_i2c_bus];
836 	cfg.i2c_addr  = addr;
837 
838 	if (!dev->dvb->fe[0]) {
839 		em28xx_errdev("/2: dvb frontend not attached. "
840 				"Can't attach xc3028\n");
841 		return -EINVAL;
842 	}
843 
844 	fe = dvb_attach(xc2028_attach, dev->dvb->fe[0], &cfg);
845 	if (!fe) {
846 		em28xx_errdev("/2: xc3028 attach failed\n");
847 		dvb_frontend_detach(dev->dvb->fe[0]);
848 		dev->dvb->fe[0] = NULL;
849 		return -EINVAL;
850 	}
851 
852 	em28xx_info("%s/2: xc3028 attached\n", dev->name);
853 
854 	return 0;
855 }
856 
857 /* ------------------------------------------------------------------ */
858 
859 static int em28xx_register_dvb(struct em28xx_dvb *dvb, struct module *module,
860 			       struct em28xx *dev, struct device *device)
861 {
862 	int result;
863 
864 	mutex_init(&dvb->lock);
865 
866 	/* register adapter */
867 	result = dvb_register_adapter(&dvb->adapter, dev->name, module, device,
868 				      adapter_nr);
869 	if (result < 0) {
870 		printk(KERN_WARNING "%s: dvb_register_adapter failed (errno = %d)\n",
871 		       dev->name, result);
872 		goto fail_adapter;
873 	}
874 
875 	/* Ensure all frontends negotiate bus access */
876 	dvb->fe[0]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
877 	if (dvb->fe[1])
878 		dvb->fe[1]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
879 
880 	dvb->adapter.priv = &dev->i2c_bus[dev->def_i2c_bus];
881 
882 	/* register frontend */
883 	result = dvb_register_frontend(&dvb->adapter, dvb->fe[0]);
884 	if (result < 0) {
885 		printk(KERN_WARNING "%s: dvb_register_frontend failed (errno = %d)\n",
886 		       dev->name, result);
887 		goto fail_frontend0;
888 	}
889 
890 	/* register 2nd frontend */
891 	if (dvb->fe[1]) {
892 		result = dvb_register_frontend(&dvb->adapter, dvb->fe[1]);
893 		if (result < 0) {
894 			printk(KERN_WARNING "%s: 2nd dvb_register_frontend failed (errno = %d)\n",
895 				dev->name, result);
896 			goto fail_frontend1;
897 		}
898 	}
899 
900 	/* register demux stuff */
901 	dvb->demux.dmx.capabilities =
902 		DMX_TS_FILTERING | DMX_SECTION_FILTERING |
903 		DMX_MEMORY_BASED_FILTERING;
904 	dvb->demux.priv       = dvb;
905 	dvb->demux.filternum  = 256;
906 	dvb->demux.feednum    = 256;
907 	dvb->demux.start_feed = em28xx_start_feed;
908 	dvb->demux.stop_feed  = em28xx_stop_feed;
909 
910 	result = dvb_dmx_init(&dvb->demux);
911 	if (result < 0) {
912 		printk(KERN_WARNING "%s: dvb_dmx_init failed (errno = %d)\n",
913 		       dev->name, result);
914 		goto fail_dmx;
915 	}
916 
917 	dvb->dmxdev.filternum    = 256;
918 	dvb->dmxdev.demux        = &dvb->demux.dmx;
919 	dvb->dmxdev.capabilities = 0;
920 	result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
921 	if (result < 0) {
922 		printk(KERN_WARNING "%s: dvb_dmxdev_init failed (errno = %d)\n",
923 		       dev->name, result);
924 		goto fail_dmxdev;
925 	}
926 
927 	dvb->fe_hw.source = DMX_FRONTEND_0;
928 	result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
929 	if (result < 0) {
930 		printk(KERN_WARNING "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
931 		       dev->name, result);
932 		goto fail_fe_hw;
933 	}
934 
935 	dvb->fe_mem.source = DMX_MEMORY_FE;
936 	result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
937 	if (result < 0) {
938 		printk(KERN_WARNING "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
939 		       dev->name, result);
940 		goto fail_fe_mem;
941 	}
942 
943 	result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
944 	if (result < 0) {
945 		printk(KERN_WARNING "%s: connect_frontend failed (errno = %d)\n",
946 		       dev->name, result);
947 		goto fail_fe_conn;
948 	}
949 
950 	/* register network adapter */
951 	dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
952 	return 0;
953 
954 fail_fe_conn:
955 	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
956 fail_fe_mem:
957 	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
958 fail_fe_hw:
959 	dvb_dmxdev_release(&dvb->dmxdev);
960 fail_dmxdev:
961 	dvb_dmx_release(&dvb->demux);
962 fail_dmx:
963 	if (dvb->fe[1])
964 		dvb_unregister_frontend(dvb->fe[1]);
965 	dvb_unregister_frontend(dvb->fe[0]);
966 fail_frontend1:
967 	if (dvb->fe[1])
968 		dvb_frontend_detach(dvb->fe[1]);
969 fail_frontend0:
970 	dvb_frontend_detach(dvb->fe[0]);
971 	dvb_unregister_adapter(&dvb->adapter);
972 fail_adapter:
973 	return result;
974 }
975 
976 static void em28xx_unregister_dvb(struct em28xx_dvb *dvb)
977 {
978 	dvb_net_release(&dvb->net);
979 	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
980 	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
981 	dvb_dmxdev_release(&dvb->dmxdev);
982 	dvb_dmx_release(&dvb->demux);
983 	if (dvb->fe[1])
984 		dvb_unregister_frontend(dvb->fe[1]);
985 	dvb_unregister_frontend(dvb->fe[0]);
986 	if (dvb->fe[1] && !dvb->dont_attach_fe1)
987 		dvb_frontend_detach(dvb->fe[1]);
988 	dvb_frontend_detach(dvb->fe[0]);
989 	dvb_unregister_adapter(&dvb->adapter);
990 }
991 
992 static int em28xx_dvb_init(struct em28xx *dev)
993 {
994 	int result = 0, mfe_shared = 0;
995 	struct em28xx_dvb *dvb;
996 
997 	if (!dev->board.has_dvb) {
998 		/* This device does not support the extension */
999 		printk(KERN_INFO "em28xx_dvb: This device does not support the extension\n");
1000 		return 0;
1001 	}
1002 
1003 	dvb = kzalloc(sizeof(struct em28xx_dvb), GFP_KERNEL);
1004 
1005 	if (dvb == NULL) {
1006 		em28xx_info("em28xx_dvb: memory allocation failed\n");
1007 		return -ENOMEM;
1008 	}
1009 	dev->dvb = dvb;
1010 	dvb->fe[0] = dvb->fe[1] = NULL;
1011 
1012 	mutex_lock(&dev->lock);
1013 	em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
1014 	/* init frontend */
1015 	switch (dev->model) {
1016 	case EM2874_BOARD_LEADERSHIP_ISDBT:
1017 		dvb->fe[0] = dvb_attach(s921_attach,
1018 				&sharp_isdbt, &dev->i2c_adap[dev->def_i2c_bus]);
1019 
1020 		if (!dvb->fe[0]) {
1021 			result = -EINVAL;
1022 			goto out_free;
1023 		}
1024 
1025 		break;
1026 	case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_850:
1027 	case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_950:
1028 	case EM2880_BOARD_PINNACLE_PCTV_HD_PRO:
1029 	case EM2880_BOARD_AMD_ATI_TV_WONDER_HD_600:
1030 		dvb->fe[0] = dvb_attach(lgdt330x_attach,
1031 					   &em2880_lgdt3303_dev,
1032 					   &dev->i2c_adap[dev->def_i2c_bus]);
1033 		if (em28xx_attach_xc3028(0x61, dev) < 0) {
1034 			result = -EINVAL;
1035 			goto out_free;
1036 		}
1037 		break;
1038 	case EM2880_BOARD_KWORLD_DVB_310U:
1039 		dvb->fe[0] = dvb_attach(zl10353_attach,
1040 					   &em28xx_zl10353_with_xc3028,
1041 					   &dev->i2c_adap[dev->def_i2c_bus]);
1042 		if (em28xx_attach_xc3028(0x61, dev) < 0) {
1043 			result = -EINVAL;
1044 			goto out_free;
1045 		}
1046 		break;
1047 	case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900:
1048 	case EM2882_BOARD_TERRATEC_HYBRID_XS:
1049 	case EM2880_BOARD_EMPIRE_DUAL_TV:
1050 		dvb->fe[0] = dvb_attach(zl10353_attach,
1051 					   &em28xx_zl10353_xc3028_no_i2c_gate,
1052 					   &dev->i2c_adap[dev->def_i2c_bus]);
1053 		if (em28xx_attach_xc3028(0x61, dev) < 0) {
1054 			result = -EINVAL;
1055 			goto out_free;
1056 		}
1057 		break;
1058 	case EM2880_BOARD_TERRATEC_HYBRID_XS:
1059 	case EM2880_BOARD_TERRATEC_HYBRID_XS_FR:
1060 	case EM2881_BOARD_PINNACLE_HYBRID_PRO:
1061 	case EM2882_BOARD_DIKOM_DK300:
1062 	case EM2882_BOARD_KWORLD_VS_DVBT:
1063 		dvb->fe[0] = dvb_attach(zl10353_attach,
1064 					   &em28xx_zl10353_xc3028_no_i2c_gate,
1065 					   &dev->i2c_adap[dev->def_i2c_bus]);
1066 		if (dvb->fe[0] == NULL) {
1067 			/* This board could have either a zl10353 or a mt352.
1068 			   If the chip id isn't for zl10353, try mt352 */
1069 			dvb->fe[0] = dvb_attach(mt352_attach,
1070 						   &terratec_xs_mt352_cfg,
1071 						   &dev->i2c_adap[dev->def_i2c_bus]);
1072 		}
1073 
1074 		if (em28xx_attach_xc3028(0x61, dev) < 0) {
1075 			result = -EINVAL;
1076 			goto out_free;
1077 		}
1078 		break;
1079 	case EM2870_BOARD_KWORLD_355U:
1080 		dvb->fe[0] = dvb_attach(zl10353_attach,
1081 					   &em28xx_zl10353_no_i2c_gate_dev,
1082 					   &dev->i2c_adap[dev->def_i2c_bus]);
1083 		if (dvb->fe[0] != NULL)
1084 			dvb_attach(qt1010_attach, dvb->fe[0],
1085 				   &dev->i2c_adap[dev->def_i2c_bus], &em28xx_qt1010_config);
1086 		break;
1087 	case EM2883_BOARD_KWORLD_HYBRID_330U:
1088 	case EM2882_BOARD_EVGA_INDTUBE:
1089 		dvb->fe[0] = dvb_attach(s5h1409_attach,
1090 					   &em28xx_s5h1409_with_xc3028,
1091 					   &dev->i2c_adap[dev->def_i2c_bus]);
1092 		if (em28xx_attach_xc3028(0x61, dev) < 0) {
1093 			result = -EINVAL;
1094 			goto out_free;
1095 		}
1096 		break;
1097 	case EM2882_BOARD_KWORLD_ATSC_315U:
1098 		dvb->fe[0] = dvb_attach(lgdt330x_attach,
1099 					   &em2880_lgdt3303_dev,
1100 					   &dev->i2c_adap[dev->def_i2c_bus]);
1101 		if (dvb->fe[0] != NULL) {
1102 			if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
1103 				&dev->i2c_adap[dev->def_i2c_bus], 0x61, TUNER_THOMSON_DTT761X)) {
1104 				result = -EINVAL;
1105 				goto out_free;
1106 			}
1107 		}
1108 		break;
1109 	case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900_R2:
1110 	case EM2882_BOARD_PINNACLE_HYBRID_PRO_330E:
1111 		dvb->fe[0] = dvb_attach(drxd_attach, &em28xx_drxd, NULL,
1112 					   &dev->i2c_adap[dev->def_i2c_bus], &dev->udev->dev);
1113 		if (em28xx_attach_xc3028(0x61, dev) < 0) {
1114 			result = -EINVAL;
1115 			goto out_free;
1116 		}
1117 		break;
1118 	case EM2870_BOARD_REDDO_DVB_C_USB_BOX:
1119 		/* Philips CU1216L NIM (Philips TDA10023 + Infineon TUA6034) */
1120 		dvb->fe[0] = dvb_attach(tda10023_attach,
1121 			&em28xx_tda10023_config,
1122 			&dev->i2c_adap[dev->def_i2c_bus], 0x48);
1123 		if (dvb->fe[0]) {
1124 			if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
1125 				&dev->i2c_adap[dev->def_i2c_bus], 0x60, TUNER_PHILIPS_CU1216L)) {
1126 				result = -EINVAL;
1127 				goto out_free;
1128 			}
1129 		}
1130 		break;
1131 	case EM2870_BOARD_KWORLD_A340:
1132 		dvb->fe[0] = dvb_attach(lgdt3305_attach,
1133 					   &em2870_lgdt3304_dev,
1134 					   &dev->i2c_adap[dev->def_i2c_bus]);
1135 		if (dvb->fe[0] != NULL)
1136 			dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1137 				   &dev->i2c_adap[dev->def_i2c_bus], &kworld_a340_config);
1138 		break;
1139 	case EM28174_BOARD_PCTV_290E:
1140 		/* set default GPIO0 for LNA, used if GPIOLIB is undefined */
1141 		dvb->lna_gpio = CXD2820R_GPIO_E | CXD2820R_GPIO_O |
1142 				CXD2820R_GPIO_L;
1143 		dvb->fe[0] = dvb_attach(cxd2820r_attach,
1144 					&em28xx_cxd2820r_config,
1145 					&dev->i2c_adap[dev->def_i2c_bus],
1146 					&dvb->lna_gpio);
1147 		if (dvb->fe[0]) {
1148 			/* FE 0 attach tuner */
1149 			if (!dvb_attach(tda18271_attach,
1150 					dvb->fe[0],
1151 					0x60,
1152 					&dev->i2c_adap[dev->def_i2c_bus],
1153 					&em28xx_cxd2820r_tda18271_config)) {
1154 
1155 				dvb_frontend_detach(dvb->fe[0]);
1156 				result = -EINVAL;
1157 				goto out_free;
1158 			}
1159 
1160 #ifdef CONFIG_GPIOLIB
1161 			/* enable LNA for DVB-T, DVB-T2 and DVB-C */
1162 			result = gpio_request_one(dvb->lna_gpio,
1163 					GPIOF_OUT_INIT_LOW, NULL);
1164 			if (result)
1165 				em28xx_errdev("gpio request failed %d\n",
1166 						result);
1167 			else
1168 				gpio_free(dvb->lna_gpio);
1169 
1170 			result = 0; /* continue even set LNA fails */
1171 #endif
1172 			dvb->fe[0]->ops.set_lna = em28xx_pctv_290e_set_lna;
1173 		}
1174 
1175 		break;
1176 	case EM2884_BOARD_HAUPPAUGE_WINTV_HVR_930C:
1177 	{
1178 		struct xc5000_config cfg;
1179 		hauppauge_hvr930c_init(dev);
1180 
1181 		dvb->fe[0] = dvb_attach(drxk_attach,
1182 					&hauppauge_930c_drxk, &dev->i2c_adap[dev->def_i2c_bus]);
1183 		if (!dvb->fe[0]) {
1184 			result = -EINVAL;
1185 			goto out_free;
1186 		}
1187 		/* FIXME: do we need a pll semaphore? */
1188 		dvb->fe[0]->sec_priv = dvb;
1189 		sema_init(&dvb->pll_mutex, 1);
1190 		dvb->gate_ctrl = dvb->fe[0]->ops.i2c_gate_ctrl;
1191 		dvb->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
1192 
1193 		/* Attach xc5000 */
1194 		memset(&cfg, 0, sizeof(cfg));
1195 		cfg.i2c_address  = 0x61;
1196 		cfg.if_khz = 4000;
1197 
1198 		if (dvb->fe[0]->ops.i2c_gate_ctrl)
1199 			dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 1);
1200 		if (!dvb_attach(xc5000_attach, dvb->fe[0], &dev->i2c_adap[dev->def_i2c_bus],
1201 				&cfg)) {
1202 			result = -EINVAL;
1203 			goto out_free;
1204 		}
1205 		if (dvb->fe[0]->ops.i2c_gate_ctrl)
1206 			dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 0);
1207 
1208 		break;
1209 	}
1210 	case EM2884_BOARD_TERRATEC_H5:
1211 		terratec_h5_init(dev);
1212 
1213 		dvb->fe[0] = dvb_attach(drxk_attach, &terratec_h5_drxk, &dev->i2c_adap[dev->def_i2c_bus]);
1214 		if (!dvb->fe[0]) {
1215 			result = -EINVAL;
1216 			goto out_free;
1217 		}
1218 		/* FIXME: do we need a pll semaphore? */
1219 		dvb->fe[0]->sec_priv = dvb;
1220 		sema_init(&dvb->pll_mutex, 1);
1221 		dvb->gate_ctrl = dvb->fe[0]->ops.i2c_gate_ctrl;
1222 		dvb->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
1223 
1224 		/* Attach tda18271 to DVB-C frontend */
1225 		if (dvb->fe[0]->ops.i2c_gate_ctrl)
1226 			dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 1);
1227 		if (!dvb_attach(tda18271c2dd_attach, dvb->fe[0], &dev->i2c_adap[dev->def_i2c_bus], 0x60)) {
1228 			result = -EINVAL;
1229 			goto out_free;
1230 		}
1231 		if (dvb->fe[0]->ops.i2c_gate_ctrl)
1232 			dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 0);
1233 
1234 		break;
1235 	case EM2884_BOARD_C3TECH_DIGITAL_DUO:
1236 		dvb->fe[0] = dvb_attach(mb86a20s_attach,
1237 					   &c3tech_duo_mb86a20s_config,
1238 					   &dev->i2c_adap[dev->def_i2c_bus]);
1239 		if (dvb->fe[0] != NULL)
1240 			dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1241 				   &dev->i2c_adap[dev->def_i2c_bus],
1242 				   &c3tech_duo_tda18271_config);
1243 		break;
1244 	case EM28174_BOARD_PCTV_460E:
1245 		/* attach demod */
1246 		dvb->fe[0] = dvb_attach(tda10071_attach,
1247 			&em28xx_tda10071_config, &dev->i2c_adap[dev->def_i2c_bus]);
1248 
1249 		/* attach SEC */
1250 		if (dvb->fe[0])
1251 			dvb_attach(a8293_attach, dvb->fe[0], &dev->i2c_adap[dev->def_i2c_bus],
1252 				&em28xx_a8293_config);
1253 		break;
1254 	case EM2874_BOARD_DELOCK_61959:
1255 	case EM2874_BOARD_MAXMEDIA_UB425_TC:
1256 		/* attach demodulator */
1257 		dvb->fe[0] = dvb_attach(drxk_attach, &maxmedia_ub425_tc_drxk,
1258 				&dev->i2c_adap[dev->def_i2c_bus]);
1259 
1260 		if (dvb->fe[0]) {
1261 			/* disable I2C-gate */
1262 			dvb->fe[0]->ops.i2c_gate_ctrl = NULL;
1263 
1264 			/* attach tuner */
1265 			if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1266 					&dev->i2c_adap[dev->def_i2c_bus],
1267 					&em28xx_cxd2820r_tda18271_config)) {
1268 				dvb_frontend_detach(dvb->fe[0]);
1269 				result = -EINVAL;
1270 				goto out_free;
1271 			}
1272 		}
1273 		break;
1274 	case EM2884_BOARD_PCTV_510E:
1275 	case EM2884_BOARD_PCTV_520E:
1276 		pctv_520e_init(dev);
1277 
1278 		/* attach demodulator */
1279 		dvb->fe[0] = dvb_attach(drxk_attach, &pctv_520e_drxk,
1280 				&dev->i2c_adap[dev->def_i2c_bus]);
1281 
1282 		if (dvb->fe[0]) {
1283 			/* attach tuner */
1284 			if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1285 					&dev->i2c_adap[dev->def_i2c_bus],
1286 					&em28xx_cxd2820r_tda18271_config)) {
1287 				dvb_frontend_detach(dvb->fe[0]);
1288 				result = -EINVAL;
1289 				goto out_free;
1290 			}
1291 		}
1292 		break;
1293 	case EM2884_BOARD_CINERGY_HTC_STICK:
1294 		terratec_htc_stick_init(dev);
1295 
1296 		/* attach demodulator */
1297 		dvb->fe[0] = dvb_attach(drxk_attach, &terratec_htc_stick_drxk,
1298 					&dev->i2c_adap[dev->def_i2c_bus]);
1299 		if (!dvb->fe[0]) {
1300 			result = -EINVAL;
1301 			goto out_free;
1302 		}
1303 
1304 		/* Attach the demodulator. */
1305 		if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1306 				&dev->i2c_adap[dev->def_i2c_bus],
1307 				&em28xx_cxd2820r_tda18271_config)) {
1308 			result = -EINVAL;
1309 			goto out_free;
1310 		}
1311 		break;
1312 	case EM2884_BOARD_TERRATEC_HTC_USB_XS:
1313 		terratec_htc_usb_xs_init(dev);
1314 
1315 		/* attach demodulator */
1316 		dvb->fe[0] = dvb_attach(drxk_attach, &terratec_htc_stick_drxk,
1317 					&dev->i2c_adap[dev->def_i2c_bus]);
1318 		if (!dvb->fe[0]) {
1319 			result = -EINVAL;
1320 			goto out_free;
1321 		}
1322 
1323 		/* Attach the demodulator. */
1324 		if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1325 				&dev->i2c_adap[dev->def_i2c_bus],
1326 				&em28xx_cxd2820r_tda18271_config)) {
1327 			result = -EINVAL;
1328 			goto out_free;
1329 		}
1330 		break;
1331 	case EM2874_BOARD_KWORLD_UB435Q_V2:
1332 		dvb->fe[0] = dvb_attach(lgdt3305_attach,
1333 					&em2874_lgdt3305_dev,
1334 					&dev->i2c_adap[dev->def_i2c_bus]);
1335 		if (!dvb->fe[0]) {
1336 			result = -EINVAL;
1337 			goto out_free;
1338 		}
1339 
1340 		/* Attach the demodulator. */
1341 		if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1342 				&dev->i2c_adap[dev->def_i2c_bus],
1343 				&kworld_ub435q_v2_config)) {
1344 			result = -EINVAL;
1345 			goto out_free;
1346 		}
1347 		break;
1348 	case EM28178_BOARD_PCTV_461E:
1349 		{
1350 			/* demod I2C adapter */
1351 			struct i2c_adapter *i2c_adapter;
1352 
1353 			/* attach demod */
1354 			dvb->fe[0] = dvb_attach(m88ds3103_attach,
1355 					&pctv_461e_m88ds3103_config,
1356 					&dev->i2c_adap[dev->def_i2c_bus],
1357 					&i2c_adapter);
1358 			if (dvb->fe[0] == NULL) {
1359 				result = -ENODEV;
1360 				goto out_free;
1361 			}
1362 
1363 			/* attach tuner */
1364 			if (!dvb_attach(m88ts2022_attach, dvb->fe[0],
1365 					i2c_adapter,
1366 					&em28xx_m88ts2022_config)) {
1367 				dvb_frontend_detach(dvb->fe[0]);
1368 				result = -ENODEV;
1369 				goto out_free;
1370 			}
1371 
1372 			/* delegate signal strength measurement to tuner */
1373 			dvb->fe[0]->ops.read_signal_strength =
1374 					dvb->fe[0]->ops.tuner_ops.get_rf_strength;
1375 
1376 			/* attach SEC */
1377 			if (!dvb_attach(a8293_attach, dvb->fe[0],
1378 					&dev->i2c_adap[dev->def_i2c_bus],
1379 					&em28xx_a8293_config)) {
1380 				dvb_frontend_detach(dvb->fe[0]);
1381 				result = -ENODEV;
1382 				goto out_free;
1383 			}
1384 		}
1385 		break;
1386 	default:
1387 		em28xx_errdev("/2: The frontend of your DVB/ATSC card"
1388 				" isn't supported yet\n");
1389 		break;
1390 	}
1391 	if (NULL == dvb->fe[0]) {
1392 		em28xx_errdev("/2: frontend initialization failed\n");
1393 		result = -EINVAL;
1394 		goto out_free;
1395 	}
1396 	/* define general-purpose callback pointer */
1397 	dvb->fe[0]->callback = em28xx_tuner_callback;
1398 	if (dvb->fe[1])
1399 		dvb->fe[1]->callback = em28xx_tuner_callback;
1400 
1401 	/* register everything */
1402 	result = em28xx_register_dvb(dvb, THIS_MODULE, dev, &dev->udev->dev);
1403 
1404 	if (result < 0)
1405 		goto out_free;
1406 
1407 	/* MFE lock */
1408 	dvb->adapter.mfe_shared = mfe_shared;
1409 
1410 	em28xx_info("Successfully loaded em28xx-dvb\n");
1411 ret:
1412 	em28xx_set_mode(dev, EM28XX_SUSPEND);
1413 	mutex_unlock(&dev->lock);
1414 	return result;
1415 
1416 out_free:
1417 	kfree(dvb);
1418 	dev->dvb = NULL;
1419 	goto ret;
1420 }
1421 
1422 static inline void prevent_sleep(struct dvb_frontend_ops *ops)
1423 {
1424 	ops->set_voltage = NULL;
1425 	ops->sleep = NULL;
1426 	ops->tuner_ops.sleep = NULL;
1427 }
1428 
1429 static int em28xx_dvb_fini(struct em28xx *dev)
1430 {
1431 	if (!dev->board.has_dvb) {
1432 		/* This device does not support the extension */
1433 		return 0;
1434 	}
1435 
1436 	if (dev->dvb) {
1437 		struct em28xx_dvb *dvb = dev->dvb;
1438 
1439 		if (dev->disconnected) {
1440 			/* We cannot tell the device to sleep
1441 			 * once it has been unplugged. */
1442 			if (dvb->fe[0])
1443 				prevent_sleep(&dvb->fe[0]->ops);
1444 			if (dvb->fe[1])
1445 				prevent_sleep(&dvb->fe[1]->ops);
1446 		}
1447 
1448 		em28xx_unregister_dvb(dvb);
1449 		kfree(dvb);
1450 		dev->dvb = NULL;
1451 	}
1452 
1453 	return 0;
1454 }
1455 
1456 static struct em28xx_ops dvb_ops = {
1457 	.id   = EM28XX_DVB,
1458 	.name = "Em28xx dvb Extension",
1459 	.init = em28xx_dvb_init,
1460 	.fini = em28xx_dvb_fini,
1461 };
1462 
1463 static int __init em28xx_dvb_register(void)
1464 {
1465 	return em28xx_register_extension(&dvb_ops);
1466 }
1467 
1468 static void __exit em28xx_dvb_unregister(void)
1469 {
1470 	em28xx_unregister_extension(&dvb_ops);
1471 }
1472 
1473 module_init(em28xx_dvb_register);
1474 module_exit(em28xx_dvb_unregister);
1475