em28xx-dvb.c 10.9 KB
Newer Older
1 2 3 4 5
/*
 DVB device driver for em28xx

 (c) 2008 Mauro Carvalho Chehab <mchehab@infradead.org>

6 7 8
 (c) 2008 Devin Heitmueller <devin.heitmueller@gmail.com>
	- Fixes for the driver to properly work with HVR-950

9 10 11
 (c) 2008 Aidan Thornton <makosoft@googlemail.com>

 Based on cx88-dvb, saa7134-dvb and videobuf-dvb originally written by:
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
	(c) 2004, 2005 Chris Pascoe <c.pascoe@itee.uq.edu.au>
	(c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]

 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.
 */

#include <linux/kernel.h>
#include <linux/usb.h>

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

#include "lgdt330x.h"
28
#include "zl10353.h"
29 30 31 32 33 34 35 36 37 38 39 40 41

MODULE_DESCRIPTION("driver for em28xx based DVB cards");
MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
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 dprintk(level, fmt, arg...) do {			\
if (debug >= level) 						\
42
	printk(KERN_DEBUG "%s/2-dvb: " fmt, dev->name, ## arg);	\
43 44
} while (0)

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
#define EM28XX_DVB_NUM_BUFS 5
#define EM28XX_DVB_MAX_PACKETSIZE 564
#define EM28XX_DVB_MAX_PACKETS 64

struct em28xx_dvb {
	struct dvb_frontend        *frontend;

	/* feed count management */
	struct mutex               lock;
	int                        nfeeds;

	/* general boilerplate stuff */
	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;
};


static inline void print_err_status(struct em28xx *dev,
				     int packet, int status)
68
{
69
	char *errmsg = "Unknown";
70

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
	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) {
		dprintk(1, "URB status %d [%s].\n", status, errmsg);
	} else {
100 101
		dprintk(1, "URB packet %d, status %d [%s].\n",
			packet, status, errmsg);
102 103
	}
}
104

105 106 107
static inline int dvb_isoc_copy(struct em28xx *dev, struct urb *urb)
{
	int i;
108

109 110
	if (!dev)
		return 0;
111

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
	if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
		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;
		}

		dvb_dmx_swfilter(&dev->dvb->demux, urb->transfer_buffer +
				 urb->iso_frame_desc[i].offset,
				 urb->iso_frame_desc[i].actual_length);
	}

	return 0;
}

138 139
static int start_streaming(struct em28xx_dvb *dvb)
{
140
	int rc;
141 142 143
	struct em28xx *dev = dvb->adapter.priv;

	usb_set_interface(dev->udev, 0, 1);
144 145 146
	rc = em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
	if (rc < 0)
		return rc;
147 148 149

	return em28xx_init_isoc(dev, EM28XX_DVB_MAX_PACKETS,
				EM28XX_DVB_NUM_BUFS, EM28XX_DVB_MAX_PACKETSIZE,
150
				dvb_isoc_copy);
151 152
}

153 154
static int stop_streaming(struct em28xx_dvb *dvb)
{
155 156 157
	struct em28xx *dev = dvb->adapter.priv;

	em28xx_uninit_isoc(dev);
158 159 160

	em28xx_set_mode(dev, EM28XX_MODE_UNDEFINED);

161 162 163
	return 0;
}

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
static int start_feed(struct dvb_demux_feed *feed)
{
	struct dvb_demux *demux  = feed->demux;
	struct em28xx_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);
179 180
		if (ret < 0)
			rc = ret;
181 182 183 184 185 186 187 188 189 190 191 192 193 194
	}

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

static int stop_feed(struct dvb_demux_feed *feed)
{
	struct dvb_demux *demux  = feed->demux;
	struct em28xx_dvb *dvb = demux->priv;
	int err = 0;

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

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

199 200 201 202 203
	mutex_unlock(&dvb->lock);
	return err;
}


204 205 206 207 208 209 210 211 212 213 214 215

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

	if (acquire)
		return em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
	else
		return em28xx_set_mode(dev, EM28XX_MODE_UNDEFINED);
}

216 217
/* ------------------------------------------------------------------ */

218 219 220 221
static struct lgdt330x_config em2880_lgdt3303_dev = {
	.demod_address = 0x0e,
	.demod_chip = LGDT3303,
};
222

223 224 225 226 227 228 229
static struct zl10353_config em28xx_zl10353_with_xc3028 = {
	.demod_address = (0x1e >> 1),
	.no_tuner = 1,
	.parallel_ts = 1,
	.if2 = 45600,
};

230 231 232 233 234
/* ------------------------------------------------------------------ */

static int attach_xc3028(u8 addr, struct em28xx *dev)
{
	struct dvb_frontend *fe;
235 236
	struct xc2028_config cfg;

237
	memset(&cfg, 0, sizeof(cfg));
238 239 240 241
	cfg.i2c_adap  = &dev->i2c_adap;
	cfg.i2c_addr  = addr;
	cfg.callback  = em28xx_tuner_callback;

242
	if (!dev->dvb->frontend) {
243 244 245 246 247 248
		printk(KERN_ERR "%s/2: dvb frontend not attached. "
				"Can't attach xc3028\n",
		       dev->name);
		return -EINVAL;
	}

249
	fe = dvb_attach(xc2028_attach, dev->dvb->frontend, &cfg);
250 251 252
	if (!fe) {
		printk(KERN_ERR "%s/2: xc3028 attach failed\n",
		       dev->name);
253 254
		dvb_frontend_detach(dev->dvb->frontend);
		dev->dvb->frontend = NULL;
255 256 257 258 259 260 261 262
		return -EINVAL;
	}

	printk(KERN_INFO "%s/2: xc3028 attached\n", dev->name);

	return 0;
}

263 264 265 266 267 268
/* ------------------------------------------------------------------ */

int register_dvb(struct em28xx_dvb *dvb,
		 struct module *module,
		 struct em28xx *dev,
		 struct device *device)
269
{
270
	int result;
271

272
	mutex_init(&dvb->lock);
273

274 275 276 277 278 279 280 281
	/* register adapter */
	result = dvb_register_adapter(&dvb->adapter, dev->name, module, device,
				      adapter_nr);
	if (result < 0) {
		printk(KERN_WARNING "%s: dvb_register_adapter failed (errno = %d)\n",
		       dev->name, result);
		goto fail_adapter;
	}
282 283 284 285

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

286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
	dvb->adapter.priv = dev;

	/* register frontend */
	result = dvb_register_frontend(&dvb->adapter, dvb->frontend);
	if (result < 0) {
		printk(KERN_WARNING "%s: dvb_register_frontend failed (errno = %d)\n",
		       dev->name, result);
		goto fail_frontend;
	}

	/* register demux stuff */
	dvb->demux.dmx.capabilities =
		DMX_TS_FILTERING | DMX_SECTION_FILTERING |
		DMX_MEMORY_BASED_FILTERING;
	dvb->demux.priv       = dvb;
	dvb->demux.filternum  = 256;
	dvb->demux.feednum    = 256;
	dvb->demux.start_feed = start_feed;
	dvb->demux.stop_feed  = stop_feed;
305

306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
	result = dvb_dmx_init(&dvb->demux);
	if (result < 0) {
		printk(KERN_WARNING "%s: dvb_dmx_init failed (errno = %d)\n",
		       dev->name, result);
		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) {
		printk(KERN_WARNING "%s: dvb_dmxdev_init failed (errno = %d)\n",
		       dev->name, result);
		goto fail_dmxdev;
	}
322

323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 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 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
	dvb->fe_hw.source = DMX_FRONTEND_0;
	result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
	if (result < 0) {
		printk(KERN_WARNING "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
		       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) {
		printk(KERN_WARNING "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
		       dev->name, result);
		goto fail_fe_mem;
	}

	result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
	if (result < 0) {
		printk(KERN_WARNING "%s: connect_frontend failed (errno = %d)\n",
		       dev->name, result);
		goto fail_fe_conn;
	}

	/* register network adapter */
	dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
	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:
	return result;
}

static void unregister_dvb(struct em28xx_dvb *dvb)
{
	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);
}


static int dvb_init(struct em28xx *dev)
{
	int result = 0;
	struct em28xx_dvb *dvb;

	dvb = kzalloc(sizeof(struct em28xx_dvb), GFP_KERNEL);
386 387 388

	if (dvb == NULL) {
		printk(KERN_INFO "em28xx_dvb: memory allocation failed\n");
389 390 391
		return -ENOMEM;
	}
	dev->dvb = dvb;
392

393
	em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
394 395
	/* init frontend */
	switch (dev->model) {
396
	case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_950:
397 398 399 400 401 402 403
		dvb->frontend = dvb_attach(lgdt330x_attach,
					   &em2880_lgdt3303_dev,
					   &dev->i2c_adap);
		if (attach_xc3028(0x61, dev) < 0) {
			result = -EINVAL;
			goto out_free;
		}
404
		break;
405
	case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900:
406 407 408 409 410 411 412
		dvb->frontend = dvb_attach(zl10353_attach,
					   &em28xx_zl10353_with_xc3028,
					   &dev->i2c_adap);
		if (attach_xc3028(0x61, dev) < 0) {
			result = -EINVAL;
			goto out_free;
		}
413
		break;
414 415 416 417 418 419
	default:
		printk(KERN_ERR "%s/2: The frontend of your DVB/ATSC card"
				" isn't supported yet\n",
		       dev->name);
		break;
	}
420
	if (NULL == dvb->frontend) {
421 422 423
		printk(KERN_ERR
		       "%s/2: frontend initialization failed\n",
		       dev->name);
424 425
		result = -EINVAL;
		goto out_free;
426 427 428
	}

	/* register everything */
429 430
	result = register_dvb(dvb, THIS_MODULE, dev, &dev->udev->dev);

431
	if (result < 0)
432 433
		goto out_free;

434
	em28xx_set_mode(dev, EM28XX_MODE_UNDEFINED);
435
	printk(KERN_INFO "Successfully loaded em28xx-dvb\n");
436 437 438
	return 0;

out_free:
439
	em28xx_set_mode(dev, EM28XX_MODE_UNDEFINED);
440 441 442
	kfree(dvb);
	dev->dvb = NULL;
	return result;
443 444 445 446
}

static int dvb_fini(struct em28xx *dev)
{
447 448 449 450
	if (dev->dvb) {
		unregister_dvb(dev->dvb);
		dev->dvb = NULL;
	}
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473

	return 0;
}

static struct em28xx_ops dvb_ops = {
	.id   = EM28XX_DVB,
	.name = "Em28xx dvb Extension",
	.init = dvb_init,
	.fini = dvb_fini,
};

static int __init em28xx_dvb_register(void)
{
	return em28xx_register_extension(&dvb_ops);
}

static void __exit em28xx_dvb_unregister(void)
{
	em28xx_unregister_extension(&dvb_ops);
}

module_init(em28xx_dvb_register);
module_exit(em28xx_dvb_unregister);