au0828-dvb.c 15.3 KB
Newer Older
1 2 3
/*
 *  Driver for the Auvitek USB bridge
 *
4
 *  Copyright (c) 2008 Steven Toth <stoth@linuxtv.org>
5 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 23
#include "au0828.h"

24
#include <linux/module.h>
25
#include <linux/slab.h>
26 27 28
#include <linux/init.h>
#include <linux/device.h>
#include <media/v4l2-common.h>
29
#include <media/tuner.h>
30 31 32

#include "au8522.h"
#include "xc5000.h"
33
#include "mxl5007t.h"
34
#include "tda18271.h"
35

36
static int preallocate_big_buffers;
37 38 39
module_param_named(preallocate_big_buffers, preallocate_big_buffers, int, 0644);
MODULE_PARM_DESC(preallocate_big_buffers, "Preallocate the larger transfer buffers at module load time");

40 41
DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);

S
Steven Toth 已提交
42 43 44
#define _AU0828_BULKPIPE 0x83
#define _BULKPIPESIZE 0xe522

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
static u8 hauppauge_hvr950q_led_states[] = {
	0x00, /* off */
	0x02, /* yellow */
	0x04, /* green */
};

static struct au8522_led_config hauppauge_hvr950q_led_cfg = {
	.gpio_output = 0x00e0,
	.gpio_output_enable  = 0x6006,
	.gpio_output_disable = 0x0660,

	.gpio_leds = 0x00e2,
	.led_states  = hauppauge_hvr950q_led_states,
	.num_led_states = sizeof(hauppauge_hvr950q_led_states),

	.vsb8_strong   = 20 /* dB */ * 10,
	.qam64_strong  = 25 /* dB */ * 10,
	.qam256_strong = 32 /* dB */ * 10,
};

65 66 67
static struct au8522_config hauppauge_hvr950q_config = {
	.demod_address = 0x8e >> 1,
	.status_mode   = AU8522_DEMODLOCKING,
68 69
	.qam_if        = AU8522_IF_6MHZ,
	.vsb_if        = AU8522_IF_6MHZ,
70 71 72 73 74 75 76 77
	.led_cfg       = &hauppauge_hvr950q_led_cfg,
};

static struct au8522_config fusionhdtv7usb_config = {
	.demod_address = 0x8e >> 1,
	.status_mode   = AU8522_DEMODLOCKING,
	.qam_if        = AU8522_IF_6MHZ,
	.vsb_if        = AU8522_IF_6MHZ,
78 79
};

80 81 82 83 84 85 86
static struct au8522_config hauppauge_woodbury_config = {
	.demod_address = 0x8e >> 1,
	.status_mode   = AU8522_DEMODLOCKING,
	.qam_if        = AU8522_IF_4MHZ,
	.vsb_if        = AU8522_IF_3_25MHZ,
};

87
static struct xc5000_config hauppauge_xc5000a_config = {
88 89
	.i2c_address      = 0x61,
	.if_khz           = 6000,
90 91 92 93 94 95 96
	.chip_id          = XC5000A,
};

static struct xc5000_config hauppauge_xc5000c_config = {
	.i2c_address      = 0x61,
	.if_khz           = 6000,
	.chip_id          = XC5000C,
97 98
};

99 100 101 102 103
static struct mxl5007t_config mxl5007t_hvr950q_config = {
	.xtal_freq_hz = MxL_XTAL_24_MHZ,
	.if_freq_hz = MxL_IF_6_MHZ,
};

104 105 106 107
static struct tda18271_config hauppauge_woodbury_tunerconfig = {
	.gate    = TDA18271_GATE_DIGITAL,
};

108 109
static void au0828_restart_dvb_streaming(struct work_struct *work);

110 111 112 113 114
/*-------------------------------------------------------------------*/
static void urb_completion(struct urb *purb)
{
	struct au0828_dev *dev = purb->context;
	int ptype = usb_pipetype(purb->pipe);
115
	unsigned char *ptr;
116

117
	dprintk(2, "%s: %d\n", __func__, purb->actual_length);
S
Steven Toth 已提交
118

119 120
	if (!dev) {
		dprintk(2, "%s: no dev!\n", __func__);
121
		return;
122
	}
123

124 125
	if (dev->urb_streaming == 0) {
		dprintk(2, "%s: not streaming!\n", __func__);
126
		return;
127
	}
128 129

	if (ptype != PIPE_BULK) {
130
		pr_err("%s: Unsupported URB type %d\n",
131
		       __func__, ptype);
132 133 134
		return;
	}

135 136 137 138 139 140 141 142 143 144
	/* See if the stream is corrupted (to work around a hardware
	   bug where the stream gets misaligned */
	ptr = purb->transfer_buffer;
	if (purb->actual_length > 0 && ptr[0] != 0x47) {
		dprintk(1, "Need to restart streaming %02x len=%d!\n",
			ptr[0], purb->actual_length);
		schedule_work(&dev->restart_streaming);
		return;
	}

145
	/* Feed the transport payload into the kernel demux */
S
Steven Toth 已提交
146 147
	dvb_dmx_swfilter_packets(&dev->dvb.demux,
		purb->transfer_buffer, purb->actual_length / 188);
148 149 150 151 152 153 154 155 156 157 158 159

	/* Clean the buffer before we requeue */
	memset(purb->transfer_buffer, 0, URB_BUFSIZE);

	/* Requeue URB */
	usb_submit_urb(purb, GFP_ATOMIC);
}

static int stop_urb_transfer(struct au0828_dev *dev)
{
	int i;

160
	dprintk(2, "%s()\n", __func__);
161

162
	dev->urb_streaming = 0;
163
	for (i = 0; i < URB_COUNT; i++) {
164 165
		if (dev->urbs[i]) {
			usb_kill_urb(dev->urbs[i]);
166 167 168
			if (!preallocate_big_buffers)
				kfree(dev->urbs[i]->transfer_buffer);

169 170
			usb_free_urb(dev->urbs[i]);
		}
171 172 173 174 175 176 177 178 179 180
	}

	return 0;
}

static int start_urb_transfer(struct au0828_dev *dev)
{
	struct urb *purb;
	int i, ret = -ENOMEM;

181
	dprintk(2, "%s()\n", __func__);
182 183

	if (dev->urb_streaming) {
184
		dprintk(2, "%s: bulk xfer already running!\n", __func__);
185 186 187 188 189 190
		return 0;
	}

	for (i = 0; i < URB_COUNT; i++) {

		dev->urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
191
		if (!dev->urbs[i])
192 193 194 195
			goto err;

		purb = dev->urbs[i];

196 197 198 199 200 201
		if (preallocate_big_buffers)
			purb->transfer_buffer = dev->dig_transfer_buffer[i];
		else
			purb->transfer_buffer = kzalloc(URB_BUFSIZE,
					GFP_KERNEL);

202 203
		if (!purb->transfer_buffer) {
			usb_free_urb(purb);
204
			dev->urbs[i] = NULL;
205
			pr_err("%s: failed big buffer allocation, err = %d\n",
206
			       __func__, ret);
207 208 209 210 211 212
			goto err;
		}

		purb->status = -EINPROGRESS;
		usb_fill_bulk_urb(purb,
				  dev->usbdev,
213 214
				  usb_rcvbulkpipe(dev->usbdev,
					_AU0828_BULKPIPE),
215 216 217 218 219 220 221 222 223 224 225
				  purb->transfer_buffer,
				  URB_BUFSIZE,
				  urb_completion,
				  dev);

	}

	for (i = 0; i < URB_COUNT; i++) {
		ret = usb_submit_urb(dev->urbs[i], GFP_ATOMIC);
		if (ret != 0) {
			stop_urb_transfer(dev);
226 227
			pr_err("%s: failed urb submission, err = %d\n",
			       __func__, ret);
228 229 230 231 232 233 234 235 236 237 238
			return ret;
		}
	}

	dev->urb_streaming = 1;
	ret = 0;

err:
	return ret;
}

239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
static void au0828_start_transport(struct au0828_dev *dev)
{
	au0828_write(dev, 0x608, 0x90);
	au0828_write(dev, 0x609, 0x72);
	au0828_write(dev, 0x60a, 0x71);
	au0828_write(dev, 0x60b, 0x01);

}

static void au0828_stop_transport(struct au0828_dev *dev, int full_stop)
{
	if (full_stop) {
		au0828_write(dev, 0x608, 0x00);
		au0828_write(dev, 0x609, 0x00);
		au0828_write(dev, 0x60a, 0x00);
	}
	au0828_write(dev, 0x60b, 0x00);
}

258 259 260 261 262 263 264
static int au0828_dvb_start_feed(struct dvb_demux_feed *feed)
{
	struct dvb_demux *demux = feed->demux;
	struct au0828_dev *dev = (struct au0828_dev *) demux->priv;
	struct au0828_dvb *dvb = &dev->dvb;
	int ret = 0;

265
	dprintk(1, "%s()\n", __func__);
266 267 268 269 270 271

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

	if (dvb) {
		mutex_lock(&dvb->lock);
272 273 274
		dvb->start_count++;
		dprintk(1, "%s(), start_count: %d, stop_count: %d\n", __func__,
			dvb->start_count, dvb->stop_count);
275
		if (dvb->feeding++ == 0) {
S
Steven Toth 已提交
276
			/* Start transport */
277
			au0828_start_transport(dev);
278
			ret = start_urb_transfer(dev);
279 280 281 282
			if (ret < 0) {
				au0828_stop_transport(dev, 0);
				dvb->feeding--;	/* We ran out of memory... */
			}
283 284 285 286 287 288 289 290 291 292 293 294 295 296
		}
		mutex_unlock(&dvb->lock);
	}

	return ret;
}

static int au0828_dvb_stop_feed(struct dvb_demux_feed *feed)
{
	struct dvb_demux *demux = feed->demux;
	struct au0828_dev *dev = (struct au0828_dev *) demux->priv;
	struct au0828_dvb *dvb = &dev->dvb;
	int ret = 0;

297
	dprintk(1, "%s()\n", __func__);
298 299

	if (dvb) {
300 301
		cancel_work_sync(&dev->restart_streaming);

302
		mutex_lock(&dvb->lock);
303 304 305 306 307 308 309 310 311 312
		dvb->stop_count++;
		dprintk(1, "%s(), start_count: %d, stop_count: %d\n", __func__,
			dvb->start_count, dvb->stop_count);
		if (dvb->feeding > 0) {
			dvb->feeding--;
			if (dvb->feeding == 0) {
				/* Stop transport */
				ret = stop_urb_transfer(dev);
				au0828_stop_transport(dev, 0);
			}
313 314 315 316 317 318 319
		}
		mutex_unlock(&dvb->lock);
	}

	return ret;
}

320 321 322 323 324 325 326 327 328 329 330 331 332 333
static void au0828_restart_dvb_streaming(struct work_struct *work)
{
	struct au0828_dev *dev = container_of(work, struct au0828_dev,
					      restart_streaming);
	struct au0828_dvb *dvb = &dev->dvb;

	if (dev->urb_streaming == 0)
		return;

	dprintk(1, "Restarting streaming...!\n");

	mutex_lock(&dvb->lock);

	/* Stop transport */
334
	stop_urb_transfer(dev);
335
	au0828_stop_transport(dev, 1);
336 337

	/* Start transport */
338
	au0828_start_transport(dev);
339
	start_urb_transfer(dev);
340 341 342 343

	mutex_unlock(&dvb->lock);
}

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 369 370 371 372 373 374 375 376 377 378
static int au0828_set_frontend(struct dvb_frontend *fe)
{
	struct au0828_dev *dev = fe->dvb->priv;
	struct au0828_dvb *dvb = &dev->dvb;
	int ret, was_streaming;

	mutex_lock(&dvb->lock);
	was_streaming = dev->urb_streaming;
	if (was_streaming) {
		au0828_stop_transport(dev, 1);

		/*
		 * We can't hold a mutex here, as the restart_streaming
		 * kthread may also hold it.
		 */
		mutex_unlock(&dvb->lock);
		cancel_work_sync(&dev->restart_streaming);
		mutex_lock(&dvb->lock);

		stop_urb_transfer(dev);
	}
	mutex_unlock(&dvb->lock);

	ret = dvb->set_frontend(fe);

	if (was_streaming) {
		mutex_lock(&dvb->lock);
		au0828_start_transport(dev);
		start_urb_transfer(dev);
		mutex_unlock(&dvb->lock);
	}

	return ret;
}

379
static int dvb_register(struct au0828_dev *dev)
380 381 382 383
{
	struct au0828_dvb *dvb = &dev->dvb;
	int result;

384
	dprintk(1, "%s()\n", __func__);
S
Steven Toth 已提交
385

386 387 388 389 390 391 392 393 394
	if (preallocate_big_buffers) {
		int i;
		for (i = 0; i < URB_COUNT; i++) {
			dev->dig_transfer_buffer[i] = kzalloc(URB_BUFSIZE,
					GFP_KERNEL);

			if (!dev->dig_transfer_buffer[i]) {
				result = -ENOMEM;

395 396
				pr_err("failed buffer allocation (errno = %d)\n",
				       result);
397 398 399 400 401
				goto fail_adapter;
			}
		}
	}

402 403
	INIT_WORK(&dev->restart_streaming, au0828_restart_dvb_streaming);

404
	/* register adapter */
405 406
	result = dvb_register_adapter(&dvb->adapter,
				      KBUILD_MODNAME, THIS_MODULE,
407 408
				      &dev->usbdev->dev, adapter_nr);
	if (result < 0) {
409 410
		pr_err("dvb_register_adapter failed (errno = %d)\n",
		       result);
411 412 413 414 415 416 417
		goto fail_adapter;
	}
	dvb->adapter.priv = dev;

	/* register frontend */
	result = dvb_register_frontend(&dvb->adapter, dvb->frontend);
	if (result < 0) {
418 419
		pr_err("dvb_register_frontend failed (errno = %d)\n",
		       result);
420 421 422
		goto fail_frontend;
	}

423 424 425 426
	/* Hook dvb frontend */
	dvb->set_frontend = dvb->frontend->ops.set_frontend;
	dvb->frontend->ops.set_frontend = au0828_set_frontend;

427 428 429 430 431 432 433 434 435 436 437
	/* register demux stuff */
	dvb->demux.dmx.capabilities =
		DMX_TS_FILTERING | DMX_SECTION_FILTERING |
		DMX_MEMORY_BASED_FILTERING;
	dvb->demux.priv       = dev;
	dvb->demux.filternum  = 256;
	dvb->demux.feednum    = 256;
	dvb->demux.start_feed = au0828_dvb_start_feed;
	dvb->demux.stop_feed  = au0828_dvb_stop_feed;
	result = dvb_dmx_init(&dvb->demux);
	if (result < 0) {
438
		pr_err("dvb_dmx_init failed (errno = %d)\n", result);
439 440 441 442 443 444 445 446
		goto fail_dmx;
	}

	dvb->dmxdev.filternum    = 256;
	dvb->dmxdev.demux        = &dvb->demux.dmx;
	dvb->dmxdev.capabilities = 0;
	result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
	if (result < 0) {
447
		pr_err("dvb_dmxdev_init failed (errno = %d)\n", result);
448 449 450 451 452 453
		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) {
454 455
		pr_err("add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
		       result);
456 457 458 459 460 461
		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) {
462 463
		pr_err("add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
		       result);
464 465 466 467 468
		goto fail_fe_mem;
	}

	result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
	if (result < 0) {
469
		pr_err("connect_frontend failed (errno = %d)\n", result);
470 471 472 473 474
		goto fail_fe_conn;
	}

	/* register network adapter */
	dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
475 476 477

	dvb->start_count = 0;
	dvb->stop_count = 0;
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
	return 0;

fail_fe_conn:
	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
fail_fe_mem:
	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
fail_fe_hw:
	dvb_dmxdev_release(&dvb->dmxdev);
fail_dmxdev:
	dvb_dmx_release(&dvb->demux);
fail_dmx:
	dvb_unregister_frontend(dvb->frontend);
fail_frontend:
	dvb_frontend_detach(dvb->frontend);
	dvb_unregister_adapter(&dvb->adapter);
fail_adapter:
494 495 496 497 498 499 500

	if (preallocate_big_buffers) {
		int i;
		for (i = 0; i < URB_COUNT; i++)
			kfree(dev->dig_transfer_buffer[i]);
	}

501 502 503 504 505 506 507
	return result;
}

void au0828_dvb_unregister(struct au0828_dev *dev)
{
	struct au0828_dvb *dvb = &dev->dvb;

508
	dprintk(1, "%s()\n", __func__);
S
Steven Toth 已提交
509

S
Steven Toth 已提交
510
	if (dvb->frontend == NULL)
511 512
		return;

513 514
	cancel_work_sync(&dev->restart_streaming);

515 516 517 518 519 520 521 522
	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);
	dvb_unregister_frontend(dvb->frontend);
	dvb_frontend_detach(dvb->frontend);
	dvb_unregister_adapter(&dvb->adapter);
523 524 525 526 527 528 529 530

	if (preallocate_big_buffers) {
		int i;
		for (i = 0; i < URB_COUNT; i++)
			kfree(dev->dig_transfer_buffer[i]);
	}


531 532 533 534 535 536 537 538 539 540 541
}

/* All the DVB attach calls go here, this function get's modified
 * for each new card. No other function in this file needs
 * to change.
 */
int au0828_dvb_register(struct au0828_dev *dev)
{
	struct au0828_dvb *dvb = &dev->dvb;
	int ret;

542
	dprintk(1, "%s()\n", __func__);
S
Steven Toth 已提交
543

544
	/* init frontend */
545
	switch (dev->boardnr) {
546 547 548 549 550
	case AU0828_BOARD_HAUPPAUGE_HVR850:
	case AU0828_BOARD_HAUPPAUGE_HVR950Q:
		dvb->frontend = dvb_attach(au8522_attach,
				&hauppauge_hvr950q_config,
				&dev->i2c_adap);
551
		if (dvb->frontend != NULL)
552 553 554 555 556 557 558 559 560 561 562 563 564
			switch (dev->board.tuner_type) {
			default:
			case TUNER_XC5000:
				dvb_attach(xc5000_attach, dvb->frontend,
					   &dev->i2c_adap,
					   &hauppauge_xc5000a_config);
				break;
			case TUNER_XC5000C:
				dvb_attach(xc5000_attach, dvb->frontend,
					   &dev->i2c_adap,
					   &hauppauge_xc5000c_config);
				break;
			}
565
		break;
566 567 568 569 570 571 572 573 574
	case AU0828_BOARD_HAUPPAUGE_HVR950Q_MXL:
		dvb->frontend = dvb_attach(au8522_attach,
				&hauppauge_hvr950q_config,
				&dev->i2c_adap);
		if (dvb->frontend != NULL)
			dvb_attach(mxl5007t_attach, dvb->frontend,
				   &dev->i2c_adap, 0x60,
				   &mxl5007t_hvr950q_config);
		break;
575 576 577 578 579 580 581 582 583
	case AU0828_BOARD_HAUPPAUGE_WOODBURY:
		dvb->frontend = dvb_attach(au8522_attach,
				&hauppauge_woodbury_config,
				&dev->i2c_adap);
		if (dvb->frontend != NULL)
			dvb_attach(tda18271_attach, dvb->frontend,
				   0x60, &dev->i2c_adap,
				   &hauppauge_woodbury_tunerconfig);
		break;
584 585 586 587 588 589 590
	case AU0828_BOARD_DVICO_FUSIONHDTV7:
		dvb->frontend = dvb_attach(au8522_attach,
				&fusionhdtv7usb_config,
				&dev->i2c_adap);
		if (dvb->frontend != NULL) {
			dvb_attach(xc5000_attach, dvb->frontend,
				&dev->i2c_adap,
591
				&hauppauge_xc5000a_config);
592 593
		}
		break;
594
	default:
595
		pr_warn("The frontend of your DVB/ATSC card isn't supported yet\n");
596 597 598
		break;
	}
	if (NULL == dvb->frontend) {
599
		pr_err("%s() Frontend initialization failed\n",
600
		       __func__);
601 602
		return -1;
	}
603 604
	/* define general-purpose callback pointer */
	dvb->frontend->callback = au0828_tuner_callback;
605 606 607 608 609 610 611 612 613 614 615

	/* register everything */
	ret = dvb_register(dev);
	if (ret < 0) {
		if (dvb->frontend->ops.release)
			dvb->frontend->ops.release(dvb->frontend);
		return ret;
	}

	return 0;
}
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645

void au0828_dvb_suspend(struct au0828_dev *dev)
{
	struct au0828_dvb *dvb = &dev->dvb;

	if (dvb && dev->urb_streaming) {
		cancel_work_sync(&dev->restart_streaming);

		/* Stop transport */
		mutex_lock(&dvb->lock);
		stop_urb_transfer(dev);
		au0828_stop_transport(dev, 1);
		mutex_unlock(&dvb->lock);
	}
}

void au0828_dvb_resume(struct au0828_dev *dev)
{
	struct au0828_dvb *dvb = &dev->dvb;

	if (dvb && dev->urb_streaming) {
		au0828_set_frontend(dvb->frontend);

		/* Start transport */
		mutex_lock(&dvb->lock);
		au0828_start_transport(dev);
		start_urb_transfer(dev);
		mutex_unlock(&dvb->lock);
	}
}