xref: /linux/drivers/media/usb/cx231xx/cx231xx-dvb.c (revision e58e871becec2d3b04ed91c0c16fe8deac9c9dfa)
1 /*
2  DVB device driver for cx231xx
3 
4  Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
5 		Based on em28xx driver
6 
7  This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21 
22 #include "cx231xx.h"
23 #include <linux/kernel.h>
24 #include <linux/slab.h>
25 
26 #include <media/v4l2-common.h>
27 #include <media/videobuf-vmalloc.h>
28 #include <media/tuner.h>
29 
30 #include "xc5000.h"
31 #include "s5h1432.h"
32 #include "tda18271.h"
33 #include "s5h1411.h"
34 #include "lgdt3305.h"
35 #include "si2165.h"
36 #include "si2168.h"
37 #include "mb86a20s.h"
38 #include "si2157.h"
39 #include "lgdt3306a.h"
40 
41 MODULE_DESCRIPTION("driver for cx231xx based DVB cards");
42 MODULE_AUTHOR("Srinivasa Deevi <srinivasa.deevi@conexant.com>");
43 MODULE_LICENSE("GPL");
44 
45 static unsigned int debug;
46 module_param(debug, int, 0644);
47 MODULE_PARM_DESC(debug, "enable debug messages [dvb]");
48 
49 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
50 
51 #define CX231XX_DVB_NUM_BUFS 5
52 #define CX231XX_DVB_MAX_PACKETSIZE 564
53 #define CX231XX_DVB_MAX_PACKETS 64
54 
55 struct cx231xx_dvb {
56 	struct dvb_frontend *frontend;
57 
58 	/* feed count management */
59 	struct mutex lock;
60 	int nfeeds;
61 
62 	/* general boilerplate stuff */
63 	struct dvb_adapter adapter;
64 	struct dvb_demux demux;
65 	struct dmxdev dmxdev;
66 	struct dmx_frontend fe_hw;
67 	struct dmx_frontend fe_mem;
68 	struct dvb_net net;
69 	struct i2c_client *i2c_client_demod;
70 	struct i2c_client *i2c_client_tuner;
71 };
72 
73 static struct s5h1432_config dvico_s5h1432_config = {
74 	.output_mode   = S5H1432_SERIAL_OUTPUT,
75 	.gpio          = S5H1432_GPIO_ON,
76 	.qam_if        = S5H1432_IF_4000,
77 	.vsb_if        = S5H1432_IF_4000,
78 	.inversion     = S5H1432_INVERSION_OFF,
79 	.status_mode   = S5H1432_DEMODLOCKING,
80 	.mpeg_timing   = S5H1432_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
81 };
82 
83 static struct tda18271_std_map cnxt_rde253s_tda18271_std_map = {
84 	.dvbt_6   = { .if_freq = 4000, .agc_mode = 3, .std = 4,
85 		      .if_lvl = 1, .rfagc_top = 0x37, },
86 	.dvbt_7   = { .if_freq = 4000, .agc_mode = 3, .std = 5,
87 		      .if_lvl = 1, .rfagc_top = 0x37, },
88 	.dvbt_8   = { .if_freq = 4000, .agc_mode = 3, .std = 6,
89 		      .if_lvl = 1, .rfagc_top = 0x37, },
90 };
91 
92 static struct tda18271_std_map mb86a20s_tda18271_config = {
93 	.dvbt_6   = { .if_freq = 4000, .agc_mode = 3, .std = 4,
94 		      .if_lvl = 0, .rfagc_top = 0x37, },
95 };
96 
97 static struct tda18271_config cnxt_rde253s_tunerconfig = {
98 	.std_map = &cnxt_rde253s_tda18271_std_map,
99 	.gate    = TDA18271_GATE_ANALOG,
100 };
101 
102 static struct s5h1411_config tda18271_s5h1411_config = {
103 	.output_mode   = S5H1411_SERIAL_OUTPUT,
104 	.gpio          = S5H1411_GPIO_OFF,
105 	.vsb_if        = S5H1411_IF_3250,
106 	.qam_if        = S5H1411_IF_4000,
107 	.inversion     = S5H1411_INVERSION_ON,
108 	.status_mode   = S5H1411_DEMODLOCKING,
109 	.mpeg_timing   = S5H1411_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
110 };
111 static struct s5h1411_config xc5000_s5h1411_config = {
112 	.output_mode   = S5H1411_SERIAL_OUTPUT,
113 	.gpio          = S5H1411_GPIO_OFF,
114 	.vsb_if        = S5H1411_IF_3250,
115 	.qam_if        = S5H1411_IF_3250,
116 	.inversion     = S5H1411_INVERSION_OFF,
117 	.status_mode   = S5H1411_DEMODLOCKING,
118 	.mpeg_timing   = S5H1411_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
119 };
120 
121 static struct lgdt3305_config hcw_lgdt3305_config = {
122 	.i2c_addr           = 0x0e,
123 	.mpeg_mode          = LGDT3305_MPEG_SERIAL,
124 	.tpclk_edge         = LGDT3305_TPCLK_FALLING_EDGE,
125 	.tpvalid_polarity   = LGDT3305_TP_VALID_HIGH,
126 	.deny_i2c_rptr      = 1,
127 	.spectral_inversion = 1,
128 	.qam_if_khz         = 4000,
129 	.vsb_if_khz         = 3250,
130 };
131 
132 static struct tda18271_std_map hauppauge_tda18271_std_map = {
133 	.atsc_6   = { .if_freq = 3250, .agc_mode = 3, .std = 4,
134 		      .if_lvl = 1, .rfagc_top = 0x58, },
135 	.qam_6    = { .if_freq = 4000, .agc_mode = 3, .std = 5,
136 		      .if_lvl = 1, .rfagc_top = 0x58, },
137 };
138 
139 static struct tda18271_config hcw_tda18271_config = {
140 	.std_map = &hauppauge_tda18271_std_map,
141 	.gate    = TDA18271_GATE_DIGITAL,
142 };
143 
144 static const struct mb86a20s_config pv_mb86a20s_config = {
145 	.demod_address = 0x10,
146 	.is_serial = true,
147 };
148 
149 static struct tda18271_config pv_tda18271_config = {
150 	.std_map = &mb86a20s_tda18271_config,
151 	.gate    = TDA18271_GATE_DIGITAL,
152 	.small_i2c = TDA18271_03_BYTE_CHUNK_INIT,
153 };
154 
155 static struct lgdt3306a_config hauppauge_955q_lgdt3306a_config = {
156 	.i2c_addr           = 0x59,
157 	.qam_if_khz         = 4000,
158 	.vsb_if_khz         = 3250,
159 	.deny_i2c_rptr      = 1,
160 	.spectral_inversion = 1,
161 	.mpeg_mode          = LGDT3306A_MPEG_SERIAL,
162 	.tpclk_edge         = LGDT3306A_TPCLK_RISING_EDGE,
163 	.tpvalid_polarity   = LGDT3306A_TP_VALID_HIGH,
164 	.xtalMHz            = 25,
165 };
166 
167 static inline void print_err_status(struct cx231xx *dev, int packet, int status)
168 {
169 	char *errmsg = "Unknown";
170 
171 	switch (status) {
172 	case -ENOENT:
173 		errmsg = "unlinked synchronuously";
174 		break;
175 	case -ECONNRESET:
176 		errmsg = "unlinked asynchronuously";
177 		break;
178 	case -ENOSR:
179 		errmsg = "Buffer error (overrun)";
180 		break;
181 	case -EPIPE:
182 		errmsg = "Stalled (device not responding)";
183 		break;
184 	case -EOVERFLOW:
185 		errmsg = "Babble (bad cable?)";
186 		break;
187 	case -EPROTO:
188 		errmsg = "Bit-stuff error (bad cable?)";
189 		break;
190 	case -EILSEQ:
191 		errmsg = "CRC/Timeout (could be anything)";
192 		break;
193 	case -ETIME:
194 		errmsg = "Device does not respond";
195 		break;
196 	}
197 	if (packet < 0) {
198 		dev_dbg(dev->dev,
199 			"URB status %d [%s].\n", status, errmsg);
200 	} else {
201 		dev_dbg(dev->dev,
202 			"URB packet %d, status %d [%s].\n",
203 			packet, status, errmsg);
204 	}
205 }
206 
207 static inline int dvb_isoc_copy(struct cx231xx *dev, struct urb *urb)
208 {
209 	int i;
210 
211 	if (!dev)
212 		return 0;
213 
214 	if (dev->state & DEV_DISCONNECTED)
215 		return 0;
216 
217 	if (urb->status < 0) {
218 		print_err_status(dev, -1, urb->status);
219 		if (urb->status == -ENOENT)
220 			return 0;
221 	}
222 
223 	for (i = 0; i < urb->number_of_packets; i++) {
224 		int status = urb->iso_frame_desc[i].status;
225 
226 		if (status < 0) {
227 			print_err_status(dev, i, status);
228 			if (urb->iso_frame_desc[i].status != -EPROTO)
229 				continue;
230 		}
231 
232 		dvb_dmx_swfilter(&dev->dvb->demux,
233 				 urb->transfer_buffer +
234 				urb->iso_frame_desc[i].offset,
235 				urb->iso_frame_desc[i].actual_length);
236 	}
237 
238 	return 0;
239 }
240 
241 static inline int dvb_bulk_copy(struct cx231xx *dev, struct urb *urb)
242 {
243 	if (!dev)
244 		return 0;
245 
246 	if (dev->state & DEV_DISCONNECTED)
247 		return 0;
248 
249 	if (urb->status < 0) {
250 		print_err_status(dev, -1, urb->status);
251 		if (urb->status == -ENOENT)
252 			return 0;
253 	}
254 
255 	/* Feed the transport payload into the kernel demux */
256 	dvb_dmx_swfilter(&dev->dvb->demux,
257 		urb->transfer_buffer, urb->actual_length);
258 
259 	return 0;
260 }
261 
262 static int start_streaming(struct cx231xx_dvb *dvb)
263 {
264 	int rc;
265 	struct cx231xx *dev = dvb->adapter.priv;
266 
267 	if (dev->USE_ISO) {
268 		dev_dbg(dev->dev, "DVB transfer mode is ISO.\n");
269 		cx231xx_set_alt_setting(dev, INDEX_TS1, 4);
270 		rc = cx231xx_set_mode(dev, CX231XX_DIGITAL_MODE);
271 		if (rc < 0)
272 			return rc;
273 		dev->mode_tv = 1;
274 		return cx231xx_init_isoc(dev, CX231XX_DVB_MAX_PACKETS,
275 					CX231XX_DVB_NUM_BUFS,
276 					dev->ts1_mode.max_pkt_size,
277 					dvb_isoc_copy);
278 	} else {
279 		dev_dbg(dev->dev, "DVB transfer mode is BULK.\n");
280 		cx231xx_set_alt_setting(dev, INDEX_TS1, 0);
281 		rc = cx231xx_set_mode(dev, CX231XX_DIGITAL_MODE);
282 		if (rc < 0)
283 			return rc;
284 		dev->mode_tv = 1;
285 		return cx231xx_init_bulk(dev, CX231XX_DVB_MAX_PACKETS,
286 					CX231XX_DVB_NUM_BUFS,
287 					dev->ts1_mode.max_pkt_size,
288 					dvb_bulk_copy);
289 	}
290 
291 }
292 
293 static int stop_streaming(struct cx231xx_dvb *dvb)
294 {
295 	struct cx231xx *dev = dvb->adapter.priv;
296 
297 	if (dev->USE_ISO)
298 		cx231xx_uninit_isoc(dev);
299 	else
300 		cx231xx_uninit_bulk(dev);
301 
302 	cx231xx_set_mode(dev, CX231XX_SUSPEND);
303 
304 	return 0;
305 }
306 
307 static int start_feed(struct dvb_demux_feed *feed)
308 {
309 	struct dvb_demux *demux = feed->demux;
310 	struct cx231xx_dvb *dvb = demux->priv;
311 	int rc, ret;
312 
313 	if (!demux->dmx.frontend)
314 		return -EINVAL;
315 
316 	mutex_lock(&dvb->lock);
317 	dvb->nfeeds++;
318 	rc = dvb->nfeeds;
319 
320 	if (dvb->nfeeds == 1) {
321 		ret = start_streaming(dvb);
322 		if (ret < 0)
323 			rc = ret;
324 	}
325 
326 	mutex_unlock(&dvb->lock);
327 	return rc;
328 }
329 
330 static int stop_feed(struct dvb_demux_feed *feed)
331 {
332 	struct dvb_demux *demux = feed->demux;
333 	struct cx231xx_dvb *dvb = demux->priv;
334 	int err = 0;
335 
336 	mutex_lock(&dvb->lock);
337 	dvb->nfeeds--;
338 
339 	if (0 == dvb->nfeeds)
340 		err = stop_streaming(dvb);
341 
342 	mutex_unlock(&dvb->lock);
343 	return err;
344 }
345 
346 /* ------------------------------------------------------------------ */
347 static int cx231xx_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
348 {
349 	struct cx231xx *dev = fe->dvb->priv;
350 
351 	if (acquire)
352 		return cx231xx_set_mode(dev, CX231XX_DIGITAL_MODE);
353 	else
354 		return cx231xx_set_mode(dev, CX231XX_SUSPEND);
355 }
356 
357 /* ------------------------------------------------------------------ */
358 
359 static struct xc5000_config cnxt_rde250_tunerconfig = {
360 	.i2c_address = 0x61,
361 	.if_khz = 4000,
362 };
363 static struct xc5000_config cnxt_rdu250_tunerconfig = {
364 	.i2c_address = 0x61,
365 	.if_khz = 3250,
366 };
367 
368 /* ------------------------------------------------------------------ */
369 #if 0
370 static int attach_xc5000(u8 addr, struct cx231xx *dev)
371 {
372 
373 	struct dvb_frontend *fe;
374 	struct xc5000_config cfg;
375 
376 	memset(&cfg, 0, sizeof(cfg));
377 	cfg.i2c_adap = cx231xx_get_i2c_adap(dev, dev->board.tuner_i2c_master);
378 	cfg.i2c_addr = addr;
379 
380 	if (!dev->dvb->frontend) {
381 		dev_err(dev->dev, "%s/2: dvb frontend not attached. Can't attach xc5000\n",
382 			dev->name);
383 		return -EINVAL;
384 	}
385 
386 	fe = dvb_attach(xc5000_attach, dev->dvb->frontend, &cfg);
387 	if (!fe) {
388 		dev_err(dev->dev, "%s/2: xc5000 attach failed\n", dev->name);
389 		dvb_frontend_detach(dev->dvb->frontend);
390 		dev->dvb->frontend = NULL;
391 		return -EINVAL;
392 	}
393 
394 	dev_info(dev->dev, "%s/2: xc5000 attached\n", dev->name);
395 
396 	return 0;
397 }
398 #endif
399 
400 int cx231xx_set_analog_freq(struct cx231xx *dev, u32 freq)
401 {
402 	if ((dev->dvb != NULL) && (dev->dvb->frontend != NULL)) {
403 
404 		struct dvb_tuner_ops *dops = &dev->dvb->frontend->ops.tuner_ops;
405 
406 		if (dops->set_analog_params != NULL) {
407 			struct analog_parameters params;
408 
409 			params.frequency = freq;
410 			params.std = dev->norm;
411 			params.mode = 0;	/* 0- Air; 1 - cable */
412 			/*params.audmode = ;       */
413 
414 			/* Set the analog parameters to set the frequency */
415 			dops->set_analog_params(dev->dvb->frontend, &params);
416 		}
417 
418 	}
419 
420 	return 0;
421 }
422 
423 int cx231xx_reset_analog_tuner(struct cx231xx *dev)
424 {
425 	int status = 0;
426 
427 	if ((dev->dvb != NULL) && (dev->dvb->frontend != NULL)) {
428 
429 		struct dvb_tuner_ops *dops = &dev->dvb->frontend->ops.tuner_ops;
430 
431 		if (dops->init != NULL && !dev->xc_fw_load_done) {
432 
433 			dev_dbg(dev->dev,
434 				"Reloading firmware for XC5000\n");
435 			status = dops->init(dev->dvb->frontend);
436 			if (status == 0) {
437 				dev->xc_fw_load_done = 1;
438 				dev_dbg(dev->dev,
439 					"XC5000 firmware download completed\n");
440 			} else {
441 				dev->xc_fw_load_done = 0;
442 				dev_dbg(dev->dev,
443 					"XC5000 firmware download failed !!!\n");
444 			}
445 		}
446 
447 	}
448 
449 	return status;
450 }
451 
452 /* ------------------------------------------------------------------ */
453 
454 static int register_dvb(struct cx231xx_dvb *dvb,
455 			struct module *module,
456 			struct cx231xx *dev, struct device *device)
457 {
458 	int result;
459 
460 	mutex_init(&dvb->lock);
461 
462 
463 	/* register adapter */
464 	result = dvb_register_adapter(&dvb->adapter, dev->name, module, device,
465 				      adapter_nr);
466 	if (result < 0) {
467 		dev_warn(dev->dev,
468 		       "%s: dvb_register_adapter failed (errno = %d)\n",
469 		       dev->name, result);
470 		goto fail_adapter;
471 	}
472 	dvb_register_media_controller(&dvb->adapter, dev->media_dev);
473 
474 	/* Ensure all frontends negotiate bus access */
475 	dvb->frontend->ops.ts_bus_ctrl = cx231xx_dvb_bus_ctrl;
476 
477 	dvb->adapter.priv = dev;
478 
479 	/* register frontend */
480 	result = dvb_register_frontend(&dvb->adapter, dvb->frontend);
481 	if (result < 0) {
482 		dev_warn(dev->dev,
483 		       "%s: dvb_register_frontend failed (errno = %d)\n",
484 		       dev->name, result);
485 		goto fail_frontend;
486 	}
487 
488 	/* register demux stuff */
489 	dvb->demux.dmx.capabilities =
490 	    DMX_TS_FILTERING | DMX_SECTION_FILTERING |
491 	    DMX_MEMORY_BASED_FILTERING;
492 	dvb->demux.priv = dvb;
493 	dvb->demux.filternum = 256;
494 	dvb->demux.feednum = 256;
495 	dvb->demux.start_feed = start_feed;
496 	dvb->demux.stop_feed = stop_feed;
497 
498 	result = dvb_dmx_init(&dvb->demux);
499 	if (result < 0) {
500 		dev_warn(dev->dev,
501 			 "%s: dvb_dmx_init failed (errno = %d)\n",
502 		       dev->name, result);
503 		goto fail_dmx;
504 	}
505 
506 	dvb->dmxdev.filternum = 256;
507 	dvb->dmxdev.demux = &dvb->demux.dmx;
508 	dvb->dmxdev.capabilities = 0;
509 	result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
510 	if (result < 0) {
511 		dev_warn(dev->dev,
512 			 "%s: dvb_dmxdev_init failed (errno = %d)\n",
513 			 dev->name, result);
514 		goto fail_dmxdev;
515 	}
516 
517 	dvb->fe_hw.source = DMX_FRONTEND_0;
518 	result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
519 	if (result < 0) {
520 		dev_warn(dev->dev,
521 		       "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
522 		       dev->name, result);
523 		goto fail_fe_hw;
524 	}
525 
526 	dvb->fe_mem.source = DMX_MEMORY_FE;
527 	result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
528 	if (result < 0) {
529 		dev_warn(dev->dev,
530 			 "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
531 			 dev->name, result);
532 		goto fail_fe_mem;
533 	}
534 
535 	result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
536 	if (result < 0) {
537 		dev_warn(dev->dev,
538 			 "%s: connect_frontend failed (errno = %d)\n",
539 			 dev->name, result);
540 		goto fail_fe_conn;
541 	}
542 
543 	/* register network adapter */
544 	dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
545 	result = dvb_create_media_graph(&dvb->adapter,
546 					dev->tuner_type == TUNER_ABSENT);
547 	if (result < 0)
548 		goto fail_create_graph;
549 
550 	return 0;
551 
552 fail_create_graph:
553 	dvb_net_release(&dvb->net);
554 fail_fe_conn:
555 	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
556 fail_fe_mem:
557 	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
558 fail_fe_hw:
559 	dvb_dmxdev_release(&dvb->dmxdev);
560 fail_dmxdev:
561 	dvb_dmx_release(&dvb->demux);
562 fail_dmx:
563 	dvb_unregister_frontend(dvb->frontend);
564 fail_frontend:
565 	dvb_frontend_detach(dvb->frontend);
566 	dvb_unregister_adapter(&dvb->adapter);
567 fail_adapter:
568 	return result;
569 }
570 
571 static void unregister_dvb(struct cx231xx_dvb *dvb)
572 {
573 	struct i2c_client *client;
574 	dvb_net_release(&dvb->net);
575 	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
576 	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
577 	dvb_dmxdev_release(&dvb->dmxdev);
578 	dvb_dmx_release(&dvb->demux);
579 	/* remove I2C tuner */
580 	client = dvb->i2c_client_tuner;
581 	if (client) {
582 		module_put(client->dev.driver->owner);
583 		i2c_unregister_device(client);
584 	}
585 	/* remove I2C demod */
586 	client = dvb->i2c_client_demod;
587 	if (client) {
588 		module_put(client->dev.driver->owner);
589 		i2c_unregister_device(client);
590 	}
591 	dvb_unregister_frontend(dvb->frontend);
592 	dvb_frontend_detach(dvb->frontend);
593 	dvb_unregister_adapter(&dvb->adapter);
594 }
595 
596 static int dvb_init(struct cx231xx *dev)
597 {
598 	int result = 0;
599 	struct cx231xx_dvb *dvb;
600 	struct i2c_adapter *tuner_i2c;
601 	struct i2c_adapter *demod_i2c;
602 
603 	if (!dev->board.has_dvb) {
604 		/* This device does not support the extension */
605 		return 0;
606 	}
607 
608 	dvb = kzalloc(sizeof(struct cx231xx_dvb), GFP_KERNEL);
609 
610 	if (dvb == NULL) {
611 		dev_info(dev->dev,
612 			 "cx231xx_dvb: memory allocation failed\n");
613 		return -ENOMEM;
614 	}
615 	dev->dvb = dvb;
616 	dev->cx231xx_set_analog_freq = cx231xx_set_analog_freq;
617 	dev->cx231xx_reset_analog_tuner = cx231xx_reset_analog_tuner;
618 
619 	tuner_i2c = cx231xx_get_i2c_adap(dev, dev->board.tuner_i2c_master);
620 	demod_i2c = cx231xx_get_i2c_adap(dev, dev->board.demod_i2c_master);
621 	mutex_lock(&dev->lock);
622 	cx231xx_set_mode(dev, CX231XX_DIGITAL_MODE);
623 	cx231xx_demod_reset(dev);
624 	/* init frontend */
625 	switch (dev->model) {
626 	case CX231XX_BOARD_CNXT_CARRAERA:
627 	case CX231XX_BOARD_CNXT_RDE_250:
628 
629 		dev->dvb->frontend = dvb_attach(s5h1432_attach,
630 					&dvico_s5h1432_config,
631 					demod_i2c);
632 
633 		if (dev->dvb->frontend == NULL) {
634 			dev_err(dev->dev,
635 				"Failed to attach s5h1432 front end\n");
636 			result = -EINVAL;
637 			goto out_free;
638 		}
639 
640 		/* define general-purpose callback pointer */
641 		dvb->frontend->callback = cx231xx_tuner_callback;
642 
643 		if (!dvb_attach(xc5000_attach, dev->dvb->frontend,
644 			       tuner_i2c,
645 			       &cnxt_rde250_tunerconfig)) {
646 			result = -EINVAL;
647 			goto out_free;
648 		}
649 
650 		break;
651 	case CX231XX_BOARD_CNXT_SHELBY:
652 	case CX231XX_BOARD_CNXT_RDU_250:
653 
654 		dev->dvb->frontend = dvb_attach(s5h1411_attach,
655 					       &xc5000_s5h1411_config,
656 					       demod_i2c);
657 
658 		if (dev->dvb->frontend == NULL) {
659 			dev_err(dev->dev,
660 				"Failed to attach s5h1411 front end\n");
661 			result = -EINVAL;
662 			goto out_free;
663 		}
664 
665 		/* define general-purpose callback pointer */
666 		dvb->frontend->callback = cx231xx_tuner_callback;
667 
668 		if (!dvb_attach(xc5000_attach, dev->dvb->frontend,
669 			       tuner_i2c,
670 			       &cnxt_rdu250_tunerconfig)) {
671 			result = -EINVAL;
672 			goto out_free;
673 		}
674 		break;
675 	case CX231XX_BOARD_CNXT_RDE_253S:
676 
677 		dev->dvb->frontend = dvb_attach(s5h1432_attach,
678 					&dvico_s5h1432_config,
679 					demod_i2c);
680 
681 		if (dev->dvb->frontend == NULL) {
682 			dev_err(dev->dev,
683 				"Failed to attach s5h1432 front end\n");
684 			result = -EINVAL;
685 			goto out_free;
686 		}
687 
688 		/* define general-purpose callback pointer */
689 		dvb->frontend->callback = cx231xx_tuner_callback;
690 
691 		if (!dvb_attach(tda18271_attach, dev->dvb->frontend,
692 			       0x60, tuner_i2c,
693 			       &cnxt_rde253s_tunerconfig)) {
694 			result = -EINVAL;
695 			goto out_free;
696 		}
697 		break;
698 	case CX231XX_BOARD_CNXT_RDU_253S:
699 	case CX231XX_BOARD_KWORLD_UB445_USB_HYBRID:
700 
701 		dev->dvb->frontend = dvb_attach(s5h1411_attach,
702 					       &tda18271_s5h1411_config,
703 					       demod_i2c);
704 
705 		if (dev->dvb->frontend == NULL) {
706 			dev_err(dev->dev,
707 				"Failed to attach s5h1411 front end\n");
708 			result = -EINVAL;
709 			goto out_free;
710 		}
711 
712 		/* define general-purpose callback pointer */
713 		dvb->frontend->callback = cx231xx_tuner_callback;
714 
715 		if (!dvb_attach(tda18271_attach, dev->dvb->frontend,
716 			       0x60, tuner_i2c,
717 			       &cnxt_rde253s_tunerconfig)) {
718 			result = -EINVAL;
719 			goto out_free;
720 		}
721 		break;
722 	case CX231XX_BOARD_HAUPPAUGE_EXETER:
723 
724 		dev_info(dev->dev,
725 			 "%s: looking for tuner / demod on i2c bus: %d\n",
726 		       __func__, i2c_adapter_id(tuner_i2c));
727 
728 		dev->dvb->frontend = dvb_attach(lgdt3305_attach,
729 						&hcw_lgdt3305_config,
730 						demod_i2c);
731 
732 		if (dev->dvb->frontend == NULL) {
733 			dev_err(dev->dev,
734 				"Failed to attach LG3305 front end\n");
735 			result = -EINVAL;
736 			goto out_free;
737 		}
738 
739 		/* define general-purpose callback pointer */
740 		dvb->frontend->callback = cx231xx_tuner_callback;
741 
742 		dvb_attach(tda18271_attach, dev->dvb->frontend,
743 			   0x60, tuner_i2c,
744 			   &hcw_tda18271_config);
745 		break;
746 
747 	case CX231XX_BOARD_HAUPPAUGE_930C_HD_1113xx:
748 	{
749 		struct i2c_client *client;
750 		struct i2c_board_info info;
751 		struct si2165_platform_data si2165_pdata;
752 
753 		/* attach demod */
754 		memset(&si2165_pdata, 0, sizeof(si2165_pdata));
755 		si2165_pdata.fe = &dev->dvb->frontend;
756 		si2165_pdata.chip_mode = SI2165_MODE_PLL_XTAL,
757 		si2165_pdata.ref_freq_Hz = 16000000,
758 
759 		memset(&info, 0, sizeof(struct i2c_board_info));
760 		strlcpy(info.type, "si2165", I2C_NAME_SIZE);
761 		info.addr = 0x64;
762 		info.platform_data = &si2165_pdata;
763 		request_module(info.type);
764 		client = i2c_new_device(demod_i2c, &info);
765 		if (client == NULL || client->dev.driver == NULL || dev->dvb->frontend == NULL) {
766 			dev_err(dev->dev,
767 				"Failed to attach SI2165 front end\n");
768 			result = -EINVAL;
769 			goto out_free;
770 		}
771 
772 		if (!try_module_get(client->dev.driver->owner)) {
773 			i2c_unregister_device(client);
774 			result = -ENODEV;
775 			goto out_free;
776 		}
777 
778 		dvb->i2c_client_demod = client;
779 
780 		dev->dvb->frontend->ops.i2c_gate_ctrl = NULL;
781 
782 		/* define general-purpose callback pointer */
783 		dvb->frontend->callback = cx231xx_tuner_callback;
784 
785 		dvb_attach(tda18271_attach, dev->dvb->frontend,
786 			0x60,
787 			tuner_i2c,
788 			&hcw_tda18271_config);
789 
790 		dev->cx231xx_reset_analog_tuner = NULL;
791 		break;
792 	}
793 	case CX231XX_BOARD_HAUPPAUGE_930C_HD_1114xx:
794 	{
795 		struct i2c_client *client;
796 		struct i2c_board_info info;
797 		struct si2165_platform_data si2165_pdata;
798 		struct si2157_config si2157_config;
799 
800 		/* attach demod */
801 		memset(&si2165_pdata, 0, sizeof(si2165_pdata));
802 		si2165_pdata.fe = &dev->dvb->frontend;
803 		si2165_pdata.chip_mode = SI2165_MODE_PLL_EXT,
804 		si2165_pdata.ref_freq_Hz = 24000000,
805 
806 		memset(&info, 0, sizeof(struct i2c_board_info));
807 		strlcpy(info.type, "si2165", I2C_NAME_SIZE);
808 		info.addr = 0x64;
809 		info.platform_data = &si2165_pdata;
810 		request_module(info.type);
811 		client = i2c_new_device(demod_i2c, &info);
812 		if (client == NULL || client->dev.driver == NULL || dev->dvb->frontend == NULL) {
813 			dev_err(dev->dev,
814 				"Failed to attach SI2165 front end\n");
815 			result = -EINVAL;
816 			goto out_free;
817 		}
818 
819 		if (!try_module_get(client->dev.driver->owner)) {
820 			i2c_unregister_device(client);
821 			result = -ENODEV;
822 			goto out_free;
823 		}
824 
825 		dvb->i2c_client_demod = client;
826 
827 		memset(&info, 0, sizeof(struct i2c_board_info));
828 
829 		dev->dvb->frontend->ops.i2c_gate_ctrl = NULL;
830 
831 		/* define general-purpose callback pointer */
832 		dvb->frontend->callback = cx231xx_tuner_callback;
833 
834 		/* attach tuner */
835 		memset(&si2157_config, 0, sizeof(si2157_config));
836 		si2157_config.fe = dev->dvb->frontend;
837 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
838 		si2157_config.mdev = dev->media_dev;
839 #endif
840 		si2157_config.if_port = 1;
841 		si2157_config.inversion = true;
842 		strlcpy(info.type, "si2157", I2C_NAME_SIZE);
843 		info.addr = 0x60;
844 		info.platform_data = &si2157_config;
845 		request_module("si2157");
846 
847 		client = i2c_new_device(
848 			tuner_i2c,
849 			&info);
850 		if (client == NULL || client->dev.driver == NULL) {
851 			dvb_frontend_detach(dev->dvb->frontend);
852 			result = -ENODEV;
853 			goto out_free;
854 		}
855 
856 		if (!try_module_get(client->dev.driver->owner)) {
857 			i2c_unregister_device(client);
858 			dvb_frontend_detach(dev->dvb->frontend);
859 			result = -ENODEV;
860 			goto out_free;
861 		}
862 
863 		dev->cx231xx_reset_analog_tuner = NULL;
864 
865 		dev->dvb->i2c_client_tuner = client;
866 		break;
867 	}
868 	case CX231XX_BOARD_HAUPPAUGE_955Q:
869 	{
870 		struct i2c_client *client;
871 		struct i2c_board_info info;
872 		struct si2157_config si2157_config;
873 
874 		memset(&info, 0, sizeof(struct i2c_board_info));
875 
876 		dev->dvb->frontend = dvb_attach(lgdt3306a_attach,
877 			&hauppauge_955q_lgdt3306a_config,
878 			demod_i2c
879 			);
880 
881 		if (dev->dvb->frontend == NULL) {
882 			dev_err(dev->dev,
883 				"Failed to attach LGDT3306A frontend.\n");
884 			result = -EINVAL;
885 			goto out_free;
886 		}
887 
888 		dev->dvb->frontend->ops.i2c_gate_ctrl = NULL;
889 
890 		/* define general-purpose callback pointer */
891 		dvb->frontend->callback = cx231xx_tuner_callback;
892 
893 		/* attach tuner */
894 		memset(&si2157_config, 0, sizeof(si2157_config));
895 		si2157_config.fe = dev->dvb->frontend;
896 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
897 		si2157_config.mdev = dev->media_dev;
898 #endif
899 		si2157_config.if_port = 1;
900 		si2157_config.inversion = true;
901 		strlcpy(info.type, "si2157", I2C_NAME_SIZE);
902 		info.addr = 0x60;
903 		info.platform_data = &si2157_config;
904 		request_module("si2157");
905 
906 		client = i2c_new_device(
907 			tuner_i2c,
908 			&info);
909 		if (client == NULL || client->dev.driver == NULL) {
910 			dvb_frontend_detach(dev->dvb->frontend);
911 			result = -ENODEV;
912 			goto out_free;
913 		}
914 
915 		if (!try_module_get(client->dev.driver->owner)) {
916 			i2c_unregister_device(client);
917 			dvb_frontend_detach(dev->dvb->frontend);
918 			result = -ENODEV;
919 			goto out_free;
920 		}
921 
922 		dev->cx231xx_reset_analog_tuner = NULL;
923 
924 		dev->dvb->i2c_client_tuner = client;
925 		break;
926 	}
927 	case CX231XX_BOARD_PV_PLAYTV_USB_HYBRID:
928 	case CX231XX_BOARD_KWORLD_UB430_USB_HYBRID:
929 
930 		dev_info(dev->dev,
931 			 "%s: looking for demod on i2c bus: %d\n",
932 			 __func__, i2c_adapter_id(tuner_i2c));
933 
934 		dev->dvb->frontend = dvb_attach(mb86a20s_attach,
935 						&pv_mb86a20s_config,
936 						demod_i2c);
937 
938 		if (dev->dvb->frontend == NULL) {
939 			dev_err(dev->dev,
940 				"Failed to attach mb86a20s demod\n");
941 			result = -EINVAL;
942 			goto out_free;
943 		}
944 
945 		/* define general-purpose callback pointer */
946 		dvb->frontend->callback = cx231xx_tuner_callback;
947 
948 		dvb_attach(tda18271_attach, dev->dvb->frontend,
949 			   0x60, tuner_i2c,
950 			   &pv_tda18271_config);
951 		break;
952 
953 	case CX231XX_BOARD_EVROMEDIA_FULL_HYBRID_FULLHD:
954 	{
955 		struct si2157_config si2157_config = {};
956 		struct si2168_config si2168_config = {};
957 		struct i2c_board_info info = {};
958 		struct i2c_client *client;
959 		struct i2c_adapter *adapter;
960 
961 		/* attach demodulator chip */
962 		si2168_config.ts_mode = SI2168_TS_SERIAL; /* from *.inf file */
963 		si2168_config.fe = &dev->dvb->frontend;
964 		si2168_config.i2c_adapter = &adapter;
965 		si2168_config.ts_clock_inv = true;
966 
967 		strlcpy(info.type, "si2168", sizeof(info.type));
968 		info.addr = dev->board.demod_addr;
969 		info.platform_data = &si2168_config;
970 
971 		request_module(info.type);
972 		client = i2c_new_device(demod_i2c, &info);
973 
974 		if (client == NULL || client->dev.driver == NULL) {
975 			result = -ENODEV;
976 			goto out_free;
977 		}
978 
979 		if (!try_module_get(client->dev.driver->owner)) {
980 			i2c_unregister_device(client);
981 			result = -ENODEV;
982 			goto out_free;
983 		}
984 
985 		dvb->i2c_client_demod = client;
986 
987 		/* attach tuner chip */
988 		si2157_config.fe = dev->dvb->frontend;
989 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
990 		si2157_config.mdev = dev->media_dev;
991 #endif
992 		si2157_config.if_port = 1;
993 		si2157_config.inversion = false;
994 
995 		memset(&info, 0, sizeof(info));
996 		strlcpy(info.type, "si2157", sizeof(info.type));
997 		info.addr = dev->board.tuner_addr;
998 		info.platform_data = &si2157_config;
999 
1000 		request_module(info.type);
1001 		client = i2c_new_device(tuner_i2c, &info);
1002 
1003 		if (client == NULL || client->dev.driver == NULL) {
1004 			module_put(dvb->i2c_client_demod->dev.driver->owner);
1005 			i2c_unregister_device(dvb->i2c_client_demod);
1006 			result = -ENODEV;
1007 			goto out_free;
1008 		}
1009 
1010 		if (!try_module_get(client->dev.driver->owner)) {
1011 			i2c_unregister_device(client);
1012 			module_put(dvb->i2c_client_demod->dev.driver->owner);
1013 			i2c_unregister_device(dvb->i2c_client_demod);
1014 			result = -ENODEV;
1015 			goto out_free;
1016 		}
1017 
1018 		dev->cx231xx_reset_analog_tuner = NULL;
1019 		dev->dvb->i2c_client_tuner = client;
1020 		break;
1021 	}
1022 	default:
1023 		dev_err(dev->dev,
1024 			"%s/2: The frontend of your DVB/ATSC card isn't supported yet\n",
1025 			dev->name);
1026 		break;
1027 	}
1028 	if (NULL == dvb->frontend) {
1029 		dev_err(dev->dev,
1030 		       "%s/2: frontend initialization failed\n", dev->name);
1031 		result = -EINVAL;
1032 		goto out_free;
1033 	}
1034 
1035 	/* register everything */
1036 	result = register_dvb(dvb, THIS_MODULE, dev, dev->dev);
1037 
1038 	if (result < 0)
1039 		goto out_free;
1040 
1041 
1042 	dev_info(dev->dev, "Successfully loaded cx231xx-dvb\n");
1043 
1044 ret:
1045 	cx231xx_set_mode(dev, CX231XX_SUSPEND);
1046 	mutex_unlock(&dev->lock);
1047 	return result;
1048 
1049 out_free:
1050 	kfree(dvb);
1051 	dev->dvb = NULL;
1052 	goto ret;
1053 }
1054 
1055 static int dvb_fini(struct cx231xx *dev)
1056 {
1057 	if (!dev->board.has_dvb) {
1058 		/* This device does not support the extension */
1059 		return 0;
1060 	}
1061 
1062 	if (dev->dvb) {
1063 		unregister_dvb(dev->dvb);
1064 		dev->dvb = NULL;
1065 	}
1066 
1067 	return 0;
1068 }
1069 
1070 static struct cx231xx_ops dvb_ops = {
1071 	.id = CX231XX_DVB,
1072 	.name = "Cx231xx dvb Extension",
1073 	.init = dvb_init,
1074 	.fini = dvb_fini,
1075 };
1076 
1077 static int __init cx231xx_dvb_register(void)
1078 {
1079 	return cx231xx_register_extension(&dvb_ops);
1080 }
1081 
1082 static void __exit cx231xx_dvb_unregister(void)
1083 {
1084 	cx231xx_unregister_extension(&dvb_ops);
1085 }
1086 
1087 module_init(cx231xx_dvb_register);
1088 module_exit(cx231xx_dvb_unregister);
1089