cx231xx-dvb.c 23.5 KB
Newer Older
1 2 3 4
/*
 DVB device driver for cx231xx

 Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
5
		Based on em28xx driver
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

 This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

22
#include "cx231xx.h"
23
#include <linux/kernel.h>
24
#include <linux/slab.h>
25 26 27 28 29

#include <media/v4l2-common.h>
#include <media/videobuf-vmalloc.h>

#include "xc5000.h"
30 31 32
#include "s5h1432.h"
#include "tda18271.h"
#include "s5h1411.h"
33
#include "lgdt3305.h"
34
#include "si2165.h"
35
#include "mb86a20s.h"
36
#include "si2157.h"
37
#include "lgdt3306a.h"
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

MODULE_DESCRIPTION("driver for cx231xx based DVB cards");
MODULE_AUTHOR("Srinivasa Deevi <srinivasa.deevi@conexant.com>");
MODULE_LICENSE("GPL");

static unsigned int debug;
module_param(debug, int, 0644);
MODULE_PARM_DESC(debug, "enable debug messages [dvb]");

DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);

#define CX231XX_DVB_NUM_BUFS 5
#define CX231XX_DVB_MAX_PACKETSIZE 564
#define CX231XX_DVB_MAX_PACKETS 64

struct cx231xx_dvb {
54
	struct dvb_frontend *frontend;
55 56

	/* feed count management */
57 58
	struct mutex lock;
	int nfeeds;
59 60

	/* general boilerplate stuff */
61 62 63 64 65 66
	struct dvb_adapter adapter;
	struct dvb_demux demux;
	struct dmxdev dmxdev;
	struct dmx_frontend fe_hw;
	struct dmx_frontend fe_mem;
	struct dvb_net net;
67
	struct i2c_client *i2c_client_tuner;
68 69
};

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
static struct s5h1432_config dvico_s5h1432_config = {
	.output_mode   = S5H1432_SERIAL_OUTPUT,
	.gpio          = S5H1432_GPIO_ON,
	.qam_if        = S5H1432_IF_4000,
	.vsb_if        = S5H1432_IF_4000,
	.inversion     = S5H1432_INVERSION_OFF,
	.status_mode   = S5H1432_DEMODLOCKING,
	.mpeg_timing   = S5H1432_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
};

static struct tda18271_std_map cnxt_rde253s_tda18271_std_map = {
	.dvbt_6   = { .if_freq = 4000, .agc_mode = 3, .std = 4,
		      .if_lvl = 1, .rfagc_top = 0x37, },
	.dvbt_7   = { .if_freq = 4000, .agc_mode = 3, .std = 5,
		      .if_lvl = 1, .rfagc_top = 0x37, },
	.dvbt_8   = { .if_freq = 4000, .agc_mode = 3, .std = 6,
		      .if_lvl = 1, .rfagc_top = 0x37, },
};

89
static struct tda18271_std_map mb86a20s_tda18271_config = {
90 91
	.dvbt_6   = { .if_freq = 4000, .agc_mode = 3, .std = 4,
		      .if_lvl = 0, .rfagc_top = 0x37, },
92 93
};

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
static struct tda18271_config cnxt_rde253s_tunerconfig = {
	.std_map = &cnxt_rde253s_tda18271_std_map,
	.gate    = TDA18271_GATE_ANALOG,
};

static struct s5h1411_config tda18271_s5h1411_config = {
	.output_mode   = S5H1411_SERIAL_OUTPUT,
	.gpio          = S5H1411_GPIO_OFF,
	.vsb_if        = S5H1411_IF_3250,
	.qam_if        = S5H1411_IF_4000,
	.inversion     = S5H1411_INVERSION_ON,
	.status_mode   = S5H1411_DEMODLOCKING,
	.mpeg_timing   = S5H1411_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
};
static struct s5h1411_config xc5000_s5h1411_config = {
	.output_mode   = S5H1411_SERIAL_OUTPUT,
	.gpio          = S5H1411_GPIO_OFF,
	.vsb_if        = S5H1411_IF_3250,
	.qam_if        = S5H1411_IF_3250,
	.inversion     = S5H1411_INVERSION_OFF,
	.status_mode   = S5H1411_DEMODLOCKING,
	.mpeg_timing   = S5H1411_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
};
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140

static struct lgdt3305_config hcw_lgdt3305_config = {
	.i2c_addr           = 0x0e,
	.mpeg_mode          = LGDT3305_MPEG_SERIAL,
	.tpclk_edge         = LGDT3305_TPCLK_FALLING_EDGE,
	.tpvalid_polarity   = LGDT3305_TP_VALID_HIGH,
	.deny_i2c_rptr      = 1,
	.spectral_inversion = 1,
	.qam_if_khz         = 4000,
	.vsb_if_khz         = 3250,
};

static struct tda18271_std_map hauppauge_tda18271_std_map = {
	.atsc_6   = { .if_freq = 3250, .agc_mode = 3, .std = 4,
		      .if_lvl = 1, .rfagc_top = 0x58, },
	.qam_6    = { .if_freq = 4000, .agc_mode = 3, .std = 5,
		      .if_lvl = 1, .rfagc_top = 0x58, },
};

static struct tda18271_config hcw_tda18271_config = {
	.std_map = &hauppauge_tda18271_std_map,
	.gate    = TDA18271_GATE_DIGITAL,
};

141 142
static const struct mb86a20s_config pv_mb86a20s_config = {
	.demod_address = 0x10,
143
	.is_serial = true,
144 145 146 147 148 149 150 151
};

static struct tda18271_config pv_tda18271_config = {
	.std_map = &mb86a20s_tda18271_config,
	.gate    = TDA18271_GATE_DIGITAL,
	.small_i2c = TDA18271_03_BYTE_CHUNK_INIT,
};

152 153 154 155 156 157
static const struct si2165_config hauppauge_930C_HD_1113xx_si2165_config = {
	.i2c_addr	= 0x64,
	.chip_mode	= SI2165_MODE_PLL_XTAL,
	.ref_freq_Hz	= 16000000,
};

158 159 160 161 162 163
static const struct si2165_config pctv_quatro_stick_1114xx_si2165_config = {
	.i2c_addr	= 0x64,
	.chip_mode	= SI2165_MODE_PLL_EXT,
	.ref_freq_Hz	= 24000000,
};

164 165 166 167 168 169 170 171 172 173 174 175
static struct lgdt3306a_config hauppauge_955q_lgdt3306a_config = {
	.i2c_addr           = 0x59,
	.qam_if_khz         = 4000,
	.vsb_if_khz         = 3250,
	.deny_i2c_rptr      = 1,
	.spectral_inversion = 1,
	.mpeg_mode          = LGDT3306A_MPEG_SERIAL,
	.tpclk_edge         = LGDT3306A_TPCLK_RISING_EDGE,
	.tpvalid_polarity   = LGDT3306A_TP_VALID_HIGH,
	.xtalMHz            = 25,
};

176
static inline void print_err_status(struct cx231xx *dev, int packet, int status)
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
{
	char *errmsg = "Unknown";

	switch (status) {
	case -ENOENT:
		errmsg = "unlinked synchronuously";
		break;
	case -ECONNRESET:
		errmsg = "unlinked asynchronuously";
		break;
	case -ENOSR:
		errmsg = "Buffer error (overrun)";
		break;
	case -EPIPE:
		errmsg = "Stalled (device not responding)";
		break;
	case -EOVERFLOW:
		errmsg = "Babble (bad cable?)";
		break;
	case -EPROTO:
		errmsg = "Bit-stuff error (bad cable?)";
		break;
	case -EILSEQ:
		errmsg = "CRC/Timeout (could be anything)";
		break;
	case -ETIME:
		errmsg = "Device does not respond";
		break;
	}
	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 213 214 215 216 217 218 219 220 221 222
			packet, status, errmsg);
	}
}

static inline int dvb_isoc_copy(struct cx231xx *dev, struct urb *urb)
{
	int i;

	if (!dev)
		return 0;

223
	if (dev->state & DEV_DISCONNECTED)
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
		return 0;

	if (urb->status < 0) {
		print_err_status(dev, -1, urb->status);
		if (urb->status == -ENOENT)
			return 0;
	}

	for (i = 0; i < urb->number_of_packets; i++) {
		int status = urb->iso_frame_desc[i].status;

		if (status < 0) {
			print_err_status(dev, i, status);
			if (urb->iso_frame_desc[i].status != -EPROTO)
				continue;
		}

241 242 243 244
		dvb_dmx_swfilter(&dev->dvb->demux,
				 urb->transfer_buffer +
				urb->iso_frame_desc[i].offset,
				urb->iso_frame_desc[i].actual_length);
245 246 247 248 249
	}

	return 0;
}

250 251 252 253 254
static inline int dvb_bulk_copy(struct cx231xx *dev, struct urb *urb)
{
	if (!dev)
		return 0;

255
	if (dev->state & DEV_DISCONNECTED)
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
		return 0;

	if (urb->status < 0) {
		print_err_status(dev, -1, urb->status);
		if (urb->status == -ENOENT)
			return 0;
	}

	/* Feed the transport payload into the kernel demux */
	dvb_dmx_swfilter(&dev->dvb->demux,
		urb->transfer_buffer, urb->actual_length);

	return 0;
}

271 272 273 274 275
static int start_streaming(struct cx231xx_dvb *dvb)
{
	int rc;
	struct cx231xx *dev = dvb->adapter.priv;

276
	if (dev->USE_ISO) {
277
		dev_dbg(dev->dev, "DVB transfer mode is ISO.\n");
278 279 280 281 282 283 284 285 286 287
		cx231xx_set_alt_setting(dev, INDEX_TS1, 4);
		rc = cx231xx_set_mode(dev, CX231XX_DIGITAL_MODE);
		if (rc < 0)
			return rc;
		dev->mode_tv = 1;
		return cx231xx_init_isoc(dev, CX231XX_DVB_MAX_PACKETS,
					CX231XX_DVB_NUM_BUFS,
					dev->ts1_mode.max_pkt_size,
					dvb_isoc_copy);
	} else {
288
		dev_dbg(dev->dev, "DVB transfer mode is BULK.\n");
289 290 291 292 293 294 295 296 297 298
		cx231xx_set_alt_setting(dev, INDEX_TS1, 0);
		rc = cx231xx_set_mode(dev, CX231XX_DIGITAL_MODE);
		if (rc < 0)
			return rc;
		dev->mode_tv = 1;
		return cx231xx_init_bulk(dev, CX231XX_DVB_MAX_PACKETS,
					CX231XX_DVB_NUM_BUFS,
					dev->ts1_mode.max_pkt_size,
					dvb_bulk_copy);
	}
299 300 301 302 303 304 305

}

static int stop_streaming(struct cx231xx_dvb *dvb)
{
	struct cx231xx *dev = dvb->adapter.priv;

306 307 308 309
	if (dev->USE_ISO)
		cx231xx_uninit_isoc(dev);
	else
		cx231xx_uninit_bulk(dev);
310 311 312 313 314 315 316 317

	cx231xx_set_mode(dev, CX231XX_SUSPEND);

	return 0;
}

static int start_feed(struct dvb_demux_feed *feed)
{
318
	struct dvb_demux *demux = feed->demux;
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
	struct cx231xx_dvb *dvb = demux->priv;
	int rc, ret;

	if (!demux->dmx.frontend)
		return -EINVAL;

	mutex_lock(&dvb->lock);
	dvb->nfeeds++;
	rc = dvb->nfeeds;

	if (dvb->nfeeds == 1) {
		ret = start_streaming(dvb);
		if (ret < 0)
			rc = ret;
	}

	mutex_unlock(&dvb->lock);
	return rc;
}

static int stop_feed(struct dvb_demux_feed *feed)
{
341
	struct dvb_demux *demux = feed->demux;
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
	struct cx231xx_dvb *dvb = demux->priv;
	int err = 0;

	mutex_lock(&dvb->lock);
	dvb->nfeeds--;

	if (0 == dvb->nfeeds)
		err = stop_streaming(dvb);

	mutex_unlock(&dvb->lock);
	return err;
}

/* ------------------------------------------------------------------ */
static int cx231xx_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
{
	struct cx231xx *dev = fe->dvb->priv;

	if (acquire)
		return cx231xx_set_mode(dev, CX231XX_DIGITAL_MODE);
	else
		return cx231xx_set_mode(dev, CX231XX_SUSPEND);
}

/* ------------------------------------------------------------------ */

static struct xc5000_config cnxt_rde250_tunerconfig = {
369
	.i2c_address = 0x61,
370 371 372 373 374
	.if_khz = 4000,
};
static struct xc5000_config cnxt_rdu250_tunerconfig = {
	.i2c_address = 0x61,
	.if_khz = 3250,
375 376 377 378 379 380 381 382 383 384 385
};

/* ------------------------------------------------------------------ */
#if 0
static int attach_xc5000(u8 addr, struct cx231xx *dev)
{

	struct dvb_frontend *fe;
	struct xc5000_config cfg;

	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 393 394 395 396
		return -EINVAL;
	}

	fe = dvb_attach(xc5000_attach, dev->dvb->frontend, &cfg);
	if (!fe) {
397
		dev_err(dev->dev, "%s/2: xc5000 attach failed\n", dev->name);
398 399 400 401 402
		dvb_frontend_detach(dev->dvb->frontend);
		dev->dvb->frontend = NULL;
		return -EINVAL;
	}

403
	dev_info(dev->dev, "%s/2: xc5000 attached\n", dev->name);
404 405 406 407 408

	return 0;
}
#endif

409
int 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 416
		if (dops->set_analog_params != NULL) {
			struct analog_parameters params;
417

418 419 420 421
			params.frequency = freq;
			params.std = dev->norm;
			params.mode = 0;	/* 0- Air; 1 - cable */
			/*params.audmode = ;       */
422

423 424
			/* Set the analog parameters to set the frequency */
			dops->set_analog_params(dev->dvb->frontend, &params);
425 426
		}

427 428
	}

429
	return 0;
430 431 432 433
}

int cx231xx_reset_analog_tuner(struct cx231xx *dev)
{
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 445 446
			status = dops->init(dev->dvb->frontend);
			if (status == 0) {
				dev->xc_fw_load_done = 1;
447
				dev_dbg(dev->dev,
448
					"XC5000 firmware download completed\n");
449 450
			} else {
				dev->xc_fw_load_done = 0;
451
				dev_dbg(dev->dev,
452
					"XC5000 firmware download failed !!!\n");
453 454 455
			}
		}

456 457
	}

458 459 460 461 462 463
	return status;
}

/* ------------------------------------------------------------------ */

static int register_dvb(struct cx231xx_dvb *dvb,
464 465
			struct module *module,
			struct cx231xx *dev, struct device *device)
466 467 468 469 470
{
	int result;

	mutex_init(&dvb->lock);

471

472 473 474 475
	/* register adapter */
	result = dvb_register_adapter(&dvb->adapter, dev->name, module, device,
				      adapter_nr);
	if (result < 0) {
476
		dev_warn(dev->dev,
477
		       "%s: dvb_register_adapter failed (errno = %d)\n",
478 479 480
		       dev->name, result);
		goto fail_adapter;
	}
481
	dvb_register_media_controller(&dvb->adapter, dev->media_dev);
482 483 484 485 486 487 488 489 490

	/* Ensure all frontends negotiate bus access */
	dvb->frontend->ops.ts_bus_ctrl = cx231xx_dvb_bus_ctrl;

	dvb->adapter.priv = dev;

	/* register frontend */
	result = dvb_register_frontend(&dvb->adapter, dvb->frontend);
	if (result < 0) {
491
		dev_warn(dev->dev,
492
		       "%s: dvb_register_frontend failed (errno = %d)\n",
493 494 495 496 497 498
		       dev->name, result);
		goto fail_frontend;
	}

	/* register demux stuff */
	dvb->demux.dmx.capabilities =
499 500 501 502 503
	    DMX_TS_FILTERING | DMX_SECTION_FILTERING |
	    DMX_MEMORY_BASED_FILTERING;
	dvb->demux.priv = dvb;
	dvb->demux.filternum = 256;
	dvb->demux.feednum = 256;
504
	dvb->demux.start_feed = start_feed;
505
	dvb->demux.stop_feed = stop_feed;
506 507 508

	result = dvb_dmx_init(&dvb->demux);
	if (result < 0) {
509
		dev_warn(dev->dev,
510
			 "%s: dvb_dmx_init failed (errno = %d)\n",
511 512 513 514
		       dev->name, result);
		goto fail_dmx;
	}

515 516
	dvb->dmxdev.filternum = 256;
	dvb->dmxdev.demux = &dvb->demux.dmx;
517 518 519
	dvb->dmxdev.capabilities = 0;
	result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
	if (result < 0) {
520
		dev_warn(dev->dev,
521 522
			 "%s: dvb_dmxdev_init failed (errno = %d)\n",
			 dev->name, result);
523 524 525 526 527 528
		goto fail_dmxdev;
	}

	dvb->fe_hw.source = DMX_FRONTEND_0;
	result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
	if (result < 0) {
529
		dev_warn(dev->dev,
530
		       "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
531 532 533 534 535 536 537
		       dev->name, result);
		goto fail_fe_hw;
	}

	dvb->fe_mem.source = DMX_MEMORY_FE;
	result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
	if (result < 0) {
538
		dev_warn(dev->dev,
539 540
			 "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
			 dev->name, result);
541 542 543 544 545
		goto fail_fe_mem;
	}

	result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
	if (result < 0) {
546
		dev_warn(dev->dev,
547 548
			 "%s: connect_frontend failed (errno = %d)\n",
			 dev->name, result);
549 550 551 552 553
		goto fail_fe_conn;
	}

	/* register network adapter */
	dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
554 555
	dvb_create_media_graph(&dvb->adapter);

556 557
	return 0;

558
fail_fe_conn:
559
	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
560
fail_fe_mem:
561
	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
562
fail_fe_hw:
563
	dvb_dmxdev_release(&dvb->dmxdev);
564
fail_dmxdev:
565
	dvb_dmx_release(&dvb->demux);
566
fail_dmx:
567
	dvb_unregister_frontend(dvb->frontend);
568
fail_frontend:
569 570
	dvb_frontend_detach(dvb->frontend);
	dvb_unregister_adapter(&dvb->adapter);
571
fail_adapter:
572 573 574 575 576
	return result;
}

static void unregister_dvb(struct cx231xx_dvb *dvb)
{
577
	struct i2c_client *client;
578 579 580 581 582
	dvb_net_release(&dvb->net);
	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
	dvb_dmxdev_release(&dvb->dmxdev);
	dvb_dmx_release(&dvb->demux);
583 584 585 586 587 588
	client = dvb->i2c_client_tuner;
	/* remove I2C tuner */
	if (client) {
		module_put(client->dev.driver->owner);
		i2c_unregister_device(client);
	}
589 590 591 592 593 594 595 596 597
	dvb_unregister_frontend(dvb->frontend);
	dvb_frontend_detach(dvb->frontend);
	dvb_unregister_adapter(&dvb->adapter);
}

static int dvb_init(struct cx231xx *dev)
{
	int result = 0;
	struct cx231xx_dvb *dvb;
598 599
	struct i2c_adapter *tuner_i2c;
	struct i2c_adapter *demod_i2c;
600 601 602 603 604 605 606 607 608

	if (!dev->board.has_dvb) {
		/* This device does not support the extension */
		return 0;
	}

	dvb = kzalloc(sizeof(struct cx231xx_dvb), GFP_KERNEL);

	if (dvb == NULL) {
609
		dev_info(dev->dev,
610
			 "cx231xx_dvb: memory allocation failed\n");
611 612 613 614
		return -ENOMEM;
	}
	dev->dvb = dvb;
	dev->cx231xx_set_analog_freq = cx231xx_set_analog_freq;
615
	dev->cx231xx_reset_analog_tuner = cx231xx_reset_analog_tuner;
616

617 618
	tuner_i2c = cx231xx_get_i2c_adap(dev, dev->board.tuner_i2c_master);
	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 623
	/* init frontend */
	switch (dev->model) {
624
	case CX231XX_BOARD_CNXT_CARRAERA:
625 626
	case CX231XX_BOARD_CNXT_RDE_250:

627 628
		dev->dvb->frontend = dvb_attach(s5h1432_attach,
					&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 635 636 637 638 639 640
			result = -EINVAL;
			goto out_free;
		}

		/* define general-purpose callback pointer */
		dvb->frontend->callback = cx231xx_tuner_callback;

641
		if (!dvb_attach(xc5000_attach, dev->dvb->frontend,
642
			       tuner_i2c,
643
			       &cnxt_rde250_tunerconfig)) {
644 645 646 647 648
			result = -EINVAL;
			goto out_free;
		}

		break;
649
	case CX231XX_BOARD_CNXT_SHELBY:
650 651
	case CX231XX_BOARD_CNXT_RDU_250:

652 653
		dev->dvb->frontend = dvb_attach(s5h1411_attach,
					       &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 660 661 662 663 664 665
			result = -EINVAL;
			goto out_free;
		}

		/* define general-purpose callback pointer */
		dvb->frontend->callback = cx231xx_tuner_callback;

666
		if (!dvb_attach(xc5000_attach, dev->dvb->frontend,
667
			       tuner_i2c,
668 669 670 671 672 673 674 675 676
			       &cnxt_rdu250_tunerconfig)) {
			result = -EINVAL;
			goto out_free;
		}
		break;
	case CX231XX_BOARD_CNXT_RDE_253S:

		dev->dvb->frontend = dvb_attach(s5h1432_attach,
					&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 683 684 685 686 687 688 689
			result = -EINVAL;
			goto out_free;
		}

		/* define general-purpose callback pointer */
		dvb->frontend->callback = cx231xx_tuner_callback;

		if (!dvb_attach(tda18271_attach, dev->dvb->frontend,
690
			       0x60, tuner_i2c,
691 692 693 694 695 696
			       &cnxt_rde253s_tunerconfig)) {
			result = -EINVAL;
			goto out_free;
		}
		break;
	case CX231XX_BOARD_CNXT_RDU_253S:
697
	case CX231XX_BOARD_KWORLD_UB445_USB_HYBRID:
698 699 700

		dev->dvb->frontend = dvb_attach(s5h1411_attach,
					       &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 707 708 709 710 711 712 713
			result = -EINVAL;
			goto out_free;
		}

		/* define general-purpose callback pointer */
		dvb->frontend->callback = cx231xx_tuner_callback;

		if (!dvb_attach(tda18271_attach, dev->dvb->frontend,
714
			       0x60, tuner_i2c,
715
			       &cnxt_rde253s_tunerconfig)) {
716 717 718 719
			result = -EINVAL;
			goto out_free;
		}
		break;
720 721
	case CX231XX_BOARD_HAUPPAUGE_EXETER:

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 727

		dev->dvb->frontend = dvb_attach(lgdt3305_attach,
						&hcw_lgdt3305_config,
728
						demod_i2c);
729 730

		if (dev->dvb->frontend == NULL) {
731
			dev_err(dev->dev,
732
				"Failed to attach LG3305 front end\n");
733 734 735 736 737 738 739 740
			result = -EINVAL;
			goto out_free;
		}

		/* define general-purpose callback pointer */
		dvb->frontend->callback = cx231xx_tuner_callback;

		dvb_attach(tda18271_attach, dev->dvb->frontend,
741
			   0x60, tuner_i2c,
742 743 744
			   &hcw_tda18271_config);
		break;

745 746 747 748
	case CX231XX_BOARD_HAUPPAUGE_930C_HD_1113xx:

		dev->dvb->frontend = dvb_attach(si2165_attach,
			&hauppauge_930C_HD_1113xx_si2165_config,
749
			demod_i2c
750 751 752
			);

		if (dev->dvb->frontend == NULL) {
753
			dev_err(dev->dev,
754
				"Failed to attach SI2165 front end\n");
755 756 757 758
			result = -EINVAL;
			goto out_free;
		}

759
		dev->dvb->frontend->ops.i2c_gate_ctrl = NULL;
760 761 762 763 764 765

		/* define general-purpose callback pointer */
		dvb->frontend->callback = cx231xx_tuner_callback;

		dvb_attach(tda18271_attach, dev->dvb->frontend,
			0x60,
766
			tuner_i2c,
767 768 769 770 771
			&hcw_tda18271_config);

		dev->cx231xx_reset_analog_tuner = NULL;
		break;

772 773 774 775 776 777 778 779 780 781
	case CX231XX_BOARD_HAUPPAUGE_930C_HD_1114xx:
	{
		struct i2c_client *client;
		struct i2c_board_info info;
		struct si2157_config si2157_config;

		memset(&info, 0, sizeof(struct i2c_board_info));

		dev->dvb->frontend = dvb_attach(si2165_attach,
			&pctv_quatro_stick_1114xx_si2165_config,
782
			demod_i2c
783 784 785
			);

		if (dev->dvb->frontend == NULL) {
786
			dev_err(dev->dev,
787
				"Failed to attach SI2165 front end\n");
788 789 790 791
			result = -EINVAL;
			goto out_free;
		}

792
		dev->dvb->frontend->ops.i2c_gate_ctrl = NULL;
793 794 795 796 797 798 799

		/* define general-purpose callback pointer */
		dvb->frontend->callback = cx231xx_tuner_callback;

		/* attach tuner */
		memset(&si2157_config, 0, sizeof(si2157_config));
		si2157_config.fe = dev->dvb->frontend;
800
		si2157_config.if_port = 1;
801 802 803 804 805 806 807
		si2157_config.inversion = true;
		strlcpy(info.type, "si2157", I2C_NAME_SIZE);
		info.addr = 0x60;
		info.platform_data = &si2157_config;
		request_module("si2157");

		client = i2c_new_device(
808
			tuner_i2c,
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
			&info);
		if (client == NULL || client->dev.driver == NULL) {
			dvb_frontend_detach(dev->dvb->frontend);
			result = -ENODEV;
			goto out_free;
		}

		if (!try_module_get(client->dev.driver->owner)) {
			i2c_unregister_device(client);
			dvb_frontend_detach(dev->dvb->frontend);
			result = -ENODEV;
			goto out_free;
		}

		dev->cx231xx_reset_analog_tuner = NULL;

		dev->dvb->i2c_client_tuner = client;
		break;
	}
828 829 830 831 832 833 834 835 836 837
	case CX231XX_BOARD_HAUPPAUGE_955Q:
	{
		struct i2c_client *client;
		struct i2c_board_info info;
		struct si2157_config si2157_config;

		memset(&info, 0, sizeof(struct i2c_board_info));

		dev->dvb->frontend = dvb_attach(lgdt3306a_attach,
			&hauppauge_955q_lgdt3306a_config,
838
			demod_i2c
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
			);

		if (dev->dvb->frontend == NULL) {
			dev_err(dev->dev,
				"Failed to attach LGDT3306A frontend.\n");
			result = -EINVAL;
			goto out_free;
		}

		dev->dvb->frontend->ops.i2c_gate_ctrl = NULL;

		/* define general-purpose callback pointer */
		dvb->frontend->callback = cx231xx_tuner_callback;

		/* attach tuner */
		memset(&si2157_config, 0, sizeof(si2157_config));
		si2157_config.fe = dev->dvb->frontend;
856
		si2157_config.if_port = 1;
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
		si2157_config.inversion = true;
		strlcpy(info.type, "si2157", I2C_NAME_SIZE);
		info.addr = 0x60;
		info.platform_data = &si2157_config;
		request_module("si2157");

		client = i2c_new_device(
			tuner_i2c,
			&info);
		if (client == NULL || client->dev.driver == NULL) {
			dvb_frontend_detach(dev->dvb->frontend);
			result = -ENODEV;
			goto out_free;
		}

		if (!try_module_get(client->dev.driver->owner)) {
			i2c_unregister_device(client);
			dvb_frontend_detach(dev->dvb->frontend);
			result = -ENODEV;
			goto out_free;
		}

		dev->cx231xx_reset_analog_tuner = NULL;
880

881 882 883
		dev->dvb->i2c_client_tuner = client;
		break;
	}
884
	case CX231XX_BOARD_PV_PLAYTV_USB_HYBRID:
885
	case CX231XX_BOARD_KWORLD_UB430_USB_HYBRID:
886

887
		dev_info(dev->dev,
888 889
			 "%s: looking for demod on i2c bus: %d\n",
			 __func__, i2c_adapter_id(tuner_i2c));
890 891 892

		dev->dvb->frontend = dvb_attach(mb86a20s_attach,
						&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 899 900 901 902 903 904 905
			result = -EINVAL;
			goto out_free;
		}

		/* define general-purpose callback pointer */
		dvb->frontend->callback = cx231xx_tuner_callback;

		dvb_attach(tda18271_attach, dev->dvb->frontend,
906
			   0x60, tuner_i2c,
907 908
			   &pv_tda18271_config);
		break;
909 910

	default:
911
		dev_err(dev->dev,
912 913
			"%s/2: The frontend of your DVB/ATSC card isn't supported yet\n",
			dev->name);
914 915 916
		break;
	}
	if (NULL == dvb->frontend) {
917
		dev_err(dev->dev,
918
		       "%s/2: frontend initialization failed\n", dev->name);
919 920 921 922 923
		result = -EINVAL;
		goto out_free;
	}

	/* register everything */
924
	result = register_dvb(dvb, THIS_MODULE, dev, dev->dev);
925 926 927 928

	if (result < 0)
		goto out_free;

929

930
	dev_info(dev->dev, "Successfully loaded cx231xx-dvb\n");
931

932
ret:
933
	cx231xx_set_mode(dev, CX231XX_SUSPEND);
934 935 936 937
	mutex_unlock(&dev->lock);
	return result;

out_free:
938 939
	kfree(dvb);
	dev->dvb = NULL;
940
	goto ret;
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958
}

static int dvb_fini(struct cx231xx *dev)
{
	if (!dev->board.has_dvb) {
		/* This device does not support the extension */
		return 0;
	}

	if (dev->dvb) {
		unregister_dvb(dev->dvb);
		dev->dvb = NULL;
	}

	return 0;
}

static struct cx231xx_ops dvb_ops = {
959
	.id = CX231XX_DVB,
960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
	.name = "Cx231xx dvb Extension",
	.init = dvb_init,
	.fini = dvb_fini,
};

static int __init cx231xx_dvb_register(void)
{
	return cx231xx_register_extension(&dvb_ops);
}

static void __exit cx231xx_dvb_unregister(void)
{
	cx231xx_unregister_extension(&dvb_ops);
}

module_init(cx231xx_dvb_register);
module_exit(cx231xx_dvb_unregister);