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