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