tm6000-dvb.c 10.8 KB
Newer Older
1
/*
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
 *  tm6000-dvb.c - dvb-t support for TM5600/TM6000/TM6010 USB video capture devices
 *
 *  Copyright (C) 2007 Michel Ludwig <michel.ludwig@gmail.com>
 *
 *  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 version 2
 *
 *  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.
18 19
 */

20
#include <linux/kernel.h>
T
Tejun Heo 已提交
21
#include <linux/slab.h>
22 23 24 25 26 27 28 29 30
#include <linux/usb.h>

#include "tm6000.h"
#include "tm6000-regs.h"

#include "zl10353.h"

#include <media/tuner.h>

31
#include "tuner-xc2028.h"
32
#include "xc5000.h"
33

34
MODULE_DESCRIPTION("DVB driver extension module for tm5600/6000/6010 based TV cards");
35
MODULE_AUTHOR("Mauro Carvalho Chehab");
36 37 38 39 40 41 42 43 44 45 46
MODULE_LICENSE("GPL");

MODULE_SUPPORTED_DEVICE("{{Trident, tm5600},"
			"{{Trident, tm6000},"
			"{{Trident, tm6010}");

static int debug;

module_param(debug, int, 0644);
MODULE_PARM_DESC(debug, "enable debug message");

47 48
static inline void print_err_status(struct tm6000_core *dev,
				    int packet, int status)
49 50 51
{
	char *errmsg = "Unknown";

52
	switch (status) {
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
	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;
	}
78
	if (packet < 0) {
79 80 81 82 83 84 85 86
		dprintk(dev, 1, "URB status %d [%s].\n",
			status, errmsg);
	} else {
		dprintk(dev, 1, "URB packet %d, status %d [%s].\n",
			packet, status, errmsg);
	}
}

87 88 89
static void tm6000_urb_received(struct urb *urb)
{
	int ret;
90
	struct tm6000_core *dev = urb->context;
91

92 93 94 95 96 97 98 99 100
	switch (urb->status) {
	case 0:
	case -ETIMEDOUT:
		break;
	case -ENOENT:
	case -ECONNRESET:
	case -ESHUTDOWN:
		return;
	default:
101
		print_err_status(dev, 0, urb->status);
102 103 104
	}

	if (urb->actual_length > 0)
105 106 107
		dvb_dmx_swfilter(&dev->dvb->demux, urb->transfer_buffer,
						   urb->actual_length);

108
	if (dev->dvb->streams > 0) {
109
		ret = usb_submit_urb(urb, GFP_ATOMIC);
110
		if (ret < 0) {
111
			printk(KERN_ERR "tm6000:  error %s\n", __func__);
112 113 114 115 116 117
			kfree(urb->transfer_buffer);
			usb_free_urb(urb);
		}
	}
}

118
static int tm6000_start_stream(struct tm6000_core *dev)
119 120
{
	int ret;
121
	unsigned int pipe, size;
122 123
	struct tm6000_dvb *dvb = dev->dvb;

124
	printk(KERN_INFO "tm6000: got start stream request %s\n", __func__);
125

126 127 128 129
	if (dev->mode != TM6000_MODE_DIGITAL) {
		tm6000_init_digital_mode(dev);
		dev->mode = TM6000_MODE_DIGITAL;
	}
130 131

	dvb->bulk_urb = usb_alloc_urb(0, GFP_KERNEL);
132
	if (dvb->bulk_urb == NULL) {
133 134 135 136
		printk(KERN_ERR "tm6000: couldn't allocate urb\n");
		return -ENOMEM;
	}

137
	pipe = usb_rcvbulkpipe(dev->udev, dev->bulk_in.endp->desc.bEndpointAddress
138 139 140 141
							  & USB_ENDPOINT_NUMBER_MASK);

	size = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe));
	size = size * 15; /* 512 x 8 or 12 or 15 */
142

143
	dvb->bulk_urb->transfer_buffer = kzalloc(size, GFP_KERNEL);
144
	if (dvb->bulk_urb->transfer_buffer == NULL) {
145 146 147 148 149 150 151
		usb_free_urb(dvb->bulk_urb);
		printk(KERN_ERR "tm6000: couldn't allocate transfer buffer!\n");
		return -ENOMEM;
	}

	usb_fill_bulk_urb(dvb->bulk_urb, dev->udev, pipe,
						 dvb->bulk_urb->transfer_buffer,
152
						 size,
153
						 tm6000_urb_received, dev);
154

155
	ret = usb_clear_halt(dev->udev, pipe);
156 157
	if (ret < 0) {
		printk(KERN_ERR "tm6000: error %i in %s during pipe reset\n",
158
							ret, __func__);
159
		return ret;
160
	} else
161 162
		printk(KERN_ERR "tm6000: pipe resetted\n");

163
/*	mutex_lock(&tm6000_driver.open_close_mutex); */
164
	ret = usb_submit_urb(dvb->bulk_urb, GFP_ATOMIC);
165

166
/*	mutex_unlock(&tm6000_driver.open_close_mutex); */
167
	if (ret) {
168 169
		printk(KERN_ERR "tm6000: submit of urb failed (error=%i)\n",
									ret);
170 171 172 173 174 175 176 177 178

		kfree(dvb->bulk_urb->transfer_buffer);
		usb_free_urb(dvb->bulk_urb);
		return ret;
	}

	return 0;
}

179
static void tm6000_stop_stream(struct tm6000_core *dev)
180 181 182
{
	struct tm6000_dvb *dvb = dev->dvb;

183 184
	if (dvb->bulk_urb) {
		printk(KERN_INFO "urb killing\n");
185
		usb_kill_urb(dvb->bulk_urb);
186
		printk(KERN_INFO "urb buffer free\n");
187 188 189 190 191 192
		kfree(dvb->bulk_urb->transfer_buffer);
		usb_free_urb(dvb->bulk_urb);
		dvb->bulk_urb = NULL;
	}
}

193
static int tm6000_start_feed(struct dvb_demux_feed *feed)
194 195 196 197
{
	struct dvb_demux *demux = feed->demux;
	struct tm6000_core *dev = demux->priv;
	struct tm6000_dvb *dvb = dev->dvb;
198
	printk(KERN_INFO "tm6000: got start feed request %s\n", __func__);
199 200

	mutex_lock(&dvb->mutex);
201
	if (dvb->streams == 0) {
202
		dvb->streams = 1;
203
/*		mutex_init(&tm6000_dev->streming_mutex); */
204
		tm6000_start_stream(dev);
205
	} else
206 207 208 209 210 211
		++(dvb->streams);
	mutex_unlock(&dvb->mutex);

	return 0;
}

212
static int tm6000_stop_feed(struct dvb_demux_feed *feed)
213
{
214 215 216 217
	struct dvb_demux *demux = feed->demux;
	struct tm6000_core *dev = demux->priv;
	struct tm6000_dvb *dvb = dev->dvb;

218
	printk(KERN_INFO "tm6000: got stop feed request %s\n", __func__);
219 220 221

	mutex_lock(&dvb->mutex);

222
	printk(KERN_INFO "stream %#x\n", dvb->streams);
223
	--(dvb->streams);
224 225
	if (dvb->streams == 0) {
		printk(KERN_INFO "stop stream\n");
226
		tm6000_stop_stream(dev);
227
/*		mutex_destroy(&tm6000_dev->streaming_mutex); */
228 229
	}
	mutex_unlock(&dvb->mutex);
230
/*	mutex_destroy(&tm6000_dev->streaming_mutex); */
231 232 233 234

	return 0;
}

235
static int tm6000_dvb_attach_frontend(struct tm6000_core *dev)
236 237 238
{
	struct tm6000_dvb *dvb = dev->dvb;

239 240 241
	if (dev->caps.has_zl10353) {
		struct zl10353_config config = {
				     .demod_address = dev->demod_addr,
242
				     .no_tuner = 1,
243 244 245
				     .parallel_ts = 1,
				     .if2 = 45700,
				     .disable_i2c_gate_ctrl = 1,
246 247
				    };

248
		dvb->frontend = dvb_attach(zl10353_attach, &config,
249
							   &dev->i2c_adap);
250
	} else {
251 252 253 254
		printk(KERN_ERR "tm6000: no frontend defined for the device!\n");
		return -1;
	}

255
	return (!dvb->frontend) ? -1 : 0;
256 257
}

258 259
DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);

260
static int register_dvb(struct tm6000_core *dev)
261 262 263 264 265 266 267 268 269 270
{
	int ret = -1;
	struct tm6000_dvb *dvb = dev->dvb;

	mutex_init(&dvb->mutex);

	dvb->streams = 0;

	/* attach the frontend */
	ret = tm6000_dvb_attach_frontend(dev);
271
	if (ret < 0) {
272
		printk(KERN_ERR "tm6000: couldn't attach the frontend!\n");
273
		goto err;
274 275 276
	}

	ret = dvb_register_adapter(&dvb->adapter, "Trident TVMaster 6000 DVB-T",
277
					THIS_MODULE, &dev->udev->dev, adapter_nr);
278 279
	dvb->adapter.priv = dev;

280
	if (dvb->frontend) {
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
		switch (dev->tuner_type) {
		case TUNER_XC2028: {
			struct xc2028_config cfg = {
				.i2c_adap = &dev->i2c_adap,
				.i2c_addr = dev->tuner_addr,
			};

			dvb->frontend->callback = tm6000_tuner_callback;
			ret = dvb_register_frontend(&dvb->adapter, dvb->frontend);
			if (ret < 0) {
				printk(KERN_ERR
					"tm6000: couldn't register frontend\n");
				goto adapter_err;
			}

			if (!dvb_attach(xc2028_attach, dvb->frontend, &cfg)) {
				printk(KERN_ERR "tm6000: couldn't register "
						"frontend (xc3028)\n");
				ret = -EINVAL;
				goto frontend_err;
			}
			printk(KERN_INFO "tm6000: XC2028/3028 asked to be "
					 "attached to frontend!\n");
			break;
			}
		case TUNER_XC5000: {
			struct xc5000_config cfg = {
				.i2c_address = dev->tuner_addr,
			};

			dvb->frontend->callback = tm6000_xc5000_callback;
			ret = dvb_register_frontend(&dvb->adapter, dvb->frontend);
			if (ret < 0) {
				printk(KERN_ERR
					"tm6000: couldn't register frontend\n");
				goto adapter_err;
			}

			if (!dvb_attach(xc5000_attach, dvb->frontend, &dev->i2c_adap, &cfg)) {
				printk(KERN_ERR "tm6000: couldn't register "
						"frontend (xc5000)\n");
				ret = -EINVAL;
				goto frontend_err;
			}
			printk(KERN_INFO "tm6000: XC5000 asked to be "
					 "attached to frontend!\n");
			break;
			}
329
		}
330
	} else
331
		printk(KERN_ERR "tm6000: no frontend found\n");
332 333 334 335

	dvb->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING
							    | DMX_MEMORY_BASED_FILTERING;
	dvb->demux.priv = dev;
336 337
	dvb->demux.filternum = 8;
	dvb->demux.feednum = 8;
338 339 340 341
	dvb->demux.start_feed = tm6000_start_feed;
	dvb->demux.stop_feed = tm6000_stop_feed;
	dvb->demux.write_to_decoder = NULL;
	ret = dvb_dmx_init(&dvb->demux);
342
	if (ret < 0) {
343
		printk(KERN_ERR "tm6000: dvb_dmx_init failed (errno = %d)\n", ret);
344 345 346 347 348 349 350 351
		goto frontend_err;
	}

	dvb->dmxdev.filternum = dev->dvb->demux.filternum;
	dvb->dmxdev.demux = &dev->dvb->demux.dmx;
	dvb->dmxdev.capabilities = 0;

	ret =  dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
352
	if (ret < 0) {
353
		printk(KERN_ERR "tm6000: dvb_dmxdev_init failed (errno = %d)\n", ret);
354 355 356 357 358 359 360 361
		goto dvb_dmx_err;
	}

	return 0;

dvb_dmx_err:
	dvb_dmx_release(&dvb->demux);
frontend_err:
362
	if (dvb->frontend) {
363
		dvb_unregister_frontend(dvb->frontend);
364
		dvb_frontend_detach(dvb->frontend);
365 366 367 368 369 370 371
	}
adapter_err:
	dvb_unregister_adapter(&dvb->adapter);
err:
	return ret;
}

372
static void unregister_dvb(struct tm6000_core *dev)
373 374 375
{
	struct tm6000_dvb *dvb = dev->dvb;

376
	if (dvb->bulk_urb != NULL) {
377 378 379 380 381 382 383 384
		struct urb *bulk_urb = dvb->bulk_urb;

		kfree(bulk_urb->transfer_buffer);
		bulk_urb->transfer_buffer = NULL;
		usb_unlink_urb(bulk_urb);
		usb_free_urb(bulk_urb);
	}

385
/*	mutex_lock(&tm6000_driver.open_close_mutex); */
386
	if (dvb->frontend) {
387
		dvb_unregister_frontend(dvb->frontend);
388
		dvb_frontend_detach(dvb->frontend);
389 390 391 392 393 394
	}

	dvb_dmxdev_release(&dvb->dmxdev);
	dvb_dmx_release(&dvb->demux);
	dvb_unregister_adapter(&dvb->adapter);
	mutex_destroy(&dvb->mutex);
395
/*	mutex_unlock(&tm6000_driver.open_close_mutex); */
396
}
397 398 399 400 401 402 403 404 405 406 407 408

static int dvb_init(struct tm6000_core *dev)
{
	struct tm6000_dvb *dvb;
	int rc;

	if (!dev)
		return 0;

	if (!dev->caps.has_dvb)
		return 0;

409 410 411 412 413
	if (dev->udev->speed == USB_SPEED_FULL) {
		printk(KERN_INFO "This USB2.0 device cannot be run on a USB1.1 port. (it lacks a hardware PID filter)\n");
		return 0;
	}

414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
	dvb = kzalloc(sizeof(struct tm6000_dvb), GFP_KERNEL);
	if (!dvb) {
		printk(KERN_INFO "Cannot allocate memory\n");
		return -ENOMEM;
	}

	dev->dvb = dvb;

	rc = register_dvb(dev);
	if (rc < 0) {
		kfree(dvb);
		dev->dvb = NULL;
		return 0;
	}

	return 0;
}

static int dvb_fini(struct tm6000_core *dev)
{
	if (!dev)
		return 0;

	if (!dev->caps.has_dvb)
		return 0;

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

	return 0;
}

static struct tm6000_ops dvb_ops = {
450
	.type	= TM6000_DVB,
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
	.name	= "TM6000 dvb Extension",
	.init	= dvb_init,
	.fini	= dvb_fini,
};

static int __init tm6000_dvb_register(void)
{
	return tm6000_register_extension(&dvb_ops);
}

static void __exit tm6000_dvb_unregister(void)
{
	tm6000_unregister_extension(&dvb_ops);
}

module_init(tm6000_dvb_register);
module_exit(tm6000_dvb_unregister);