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