dvb_usb_init.c 14.4 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * DVB USB library - provides a generic interface for a DVB USB device driver.
 *
 * dvb-usb-init.c
 *
 * Copyright (C) 2004-6 Patrick Boettcher (patrick.boettcher@desy.de)
 *
 *	This program is free software; you can redistribute it and/or modify it
9 10
 *	under the terms of the GNU General Public License as published by the
 *	Free Software Foundation, version 2.
11 12 13 14
 *
 * see Documentation/dvb/README.dvb-usb for more information
 */
#include "dvb_usb_common.h"
15
#include <linux/usb/input.h>
16

17 18
int dvb_usbv2_disable_rc_polling;
module_param_named(disable_rc_polling, dvb_usbv2_disable_rc_polling, int, 0644);
19 20
MODULE_PARM_DESC(disable_rc_polling,
		"disable remote control polling (default: 0).");
21
static int dvb_usb_force_pid_filter_usage;
22 23 24 25
module_param_named(force_pid_filter_usage, dvb_usb_force_pid_filter_usage,
		int, 0444);
MODULE_PARM_DESC(force_pid_filter_usage, "force all dvb-usb-devices to use a" \
		" PID filter, if any (default: 0).");
26

27
static int dvb_usbv2_download_firmware(struct dvb_usb_device *d)
28 29 30 31 32 33
{
	int ret;
	const struct firmware *fw = NULL;
	const char *name;

	/* resolve firmware name */
34 35 36
	name = d->props->firmware;
	if (d->props->get_firmware_name) {
		ret = d->props->get_firmware_name(d, &name);
37
		if (ret < 0)
38
			goto err;
39 40
	}

41
	if (!d->props->download_firmware) {
42 43 44 45 46 47
		ret = -EINVAL;
		goto err;
	}

	ret = request_firmware(&fw, name, &d->udev->dev);
	if (ret < 0) {
48 49
		pr_err("%s: did not find the firmware file. (%s) " \
				"Please see linux/Documentation/dvb/ for " \
50
				"more details on firmware-problems. (%d)\n",
51
				KBUILD_MODNAME, name, ret);
52 53 54
		goto err;
	}

55
	pr_info("%s: downloading firmware from file '%s'\n", KBUILD_MODNAME,
56
			name);
57

58
	ret = d->props->download_firmware(d, fw);
59 60 61 62 63 64

	release_firmware(fw);

	if (ret < 0)
		goto err;

65
	return ret;
66 67 68 69 70
err:
	pr_debug("%s: failed=%d\n", __func__, ret);
	return ret;
}

71
static int dvb_usbv2_i2c_init(struct dvb_usb_device *d)
72
{
73
	int ret;
74

75
	if (!d->props->i2c_algo) {
76 77 78
		ret = 0;
		goto err;
	}
79 80

	strlcpy(d->i2c_adap.name, d->name, sizeof(d->i2c_adap.name));
81
	d->i2c_adap.algo = d->props->i2c_algo;
82 83 84 85 86 87
	d->i2c_adap.algo_data = NULL;
	d->i2c_adap.dev.parent = &d->udev->dev;

	i2c_set_adapdata(&d->i2c_adap, d);

	ret = i2c_add_adapter(&d->i2c_adap);
88 89 90 91
	if (ret < 0) {
		pr_err("%s: i2c_add_adapter() failed\n", KBUILD_MODNAME);
		goto err;
	}
92 93 94

	d->state |= DVB_USB_STATE_I2C;

95 96 97
	return 0;
err:
	pr_debug("%s: failed=%d\n", __func__, ret);
98 99 100
	return ret;
}

101
static int dvb_usbv2_i2c_exit(struct dvb_usb_device *d)
102 103 104
{
	if (d->state & DVB_USB_STATE_I2C)
		i2c_del_adapter(&d->i2c_adap);
105

106
	d->state &= ~DVB_USB_STATE_I2C;
107

108 109 110
	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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 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 207 208 209 210 211 212
static void dvb_usb_read_remote_control(struct work_struct *work)
{
	struct dvb_usb_device *d = container_of(work,
			struct dvb_usb_device, rc_query_work.work);
	int ret;

	/* TODO: need a lock here.  We can simply skip checking for the remote
	   control if we're busy. */

	/* when the parameter has been set to 1 via sysfs while the
	 * driver was running, or when bulk mode is enabled after IR init
	 */
	if (dvb_usbv2_disable_rc_polling || d->rc.bulk_mode)
		return;

	ret = d->rc.query(d);
	if (ret < 0)
		pr_err("%s: error %d while querying for an remote control " \
				"event\n", KBUILD_MODNAME, ret);

	schedule_delayed_work(&d->rc_query_work,
			      msecs_to_jiffies(d->rc.interval));
}

static int dvb_usbv2_remote_init(struct dvb_usb_device *d)
{
	int ret;
	struct rc_dev *dev;

	if (dvb_usbv2_disable_rc_polling || !d->props->get_rc_config)
		return 0;

	ret = d->props->get_rc_config(d, &d->rc);
	if (ret < 0)
		goto err;

	dev = rc_allocate_device();
	if (!dev) {
		ret = -ENOMEM;
		goto err;
	}

	dev->dev.parent = &d->udev->dev;
	dev->input_name = "IR-receiver inside an USB DVB receiver";
	usb_make_path(d->udev, d->rc_phys, sizeof(d->rc_phys));
	strlcat(d->rc_phys, "/ir0", sizeof(d->rc_phys));
	dev->input_phys = d->rc_phys;
	usb_to_input_id(d->udev, &dev->input_id);
	/* TODO: likely RC-core should took const char * */
	dev->driver_name = (char *) d->props->driver_name;
	dev->driver_type = d->rc.driver_type;
	dev->allowed_protos = d->rc.allowed_protos;
	dev->change_protocol = d->rc.change_protocol;
	dev->priv = d;
	/* select used keymap */
	if (d->rc.map_name)
		dev->map_name = d->rc.map_name;
	else if (d->rc_map)
		dev->map_name = d->rc_map;
	else
		dev->map_name = RC_MAP_EMPTY; /* keep rc enabled */

	ret = rc_register_device(dev);
	if (ret < 0) {
		rc_free_device(dev);
		goto err;
	}

	d->input_dev = NULL;
	d->rc_dev = dev;

	/* start polling if needed */
	if (d->rc.query && !d->rc.bulk_mode) {
		/* initialize a work queue for handling polling */
		INIT_DELAYED_WORK(&d->rc_query_work,
				dvb_usb_read_remote_control);
		pr_info("%s: schedule remote query interval to %d msecs\n",
				KBUILD_MODNAME, d->rc.interval);
		schedule_delayed_work(&d->rc_query_work,
				msecs_to_jiffies(d->rc.interval));
	}

	d->state |= DVB_USB_STATE_REMOTE;

	return 0;
err:
	pr_debug("%s: failed=%d\n", __func__, ret);
	return ret;
}

static int dvb_usbv2_remote_exit(struct dvb_usb_device *d)
{
	if (d->state & DVB_USB_STATE_REMOTE) {
		cancel_delayed_work_sync(&d->rc_query_work);
		rc_unregister_device(d->rc_dev);
	}

	d->state &= ~DVB_USB_STATE_REMOTE;

	return 0;
}

213
static int dvb_usbv2_adapter_init(struct dvb_usb_device *d)
214 215
{
	struct dvb_usb_adapter *adap;
216
	int ret, i, adapter_count;
217

218
	/* resolve adapter count */
219 220 221
	adapter_count = d->props->num_adapters;
	if (d->props->get_adapter_count) {
		ret = d->props->get_adapter_count(d);
222 223 224 225 226 227
		if (ret < 0)
			goto err;

		adapter_count = ret;
	}

228 229
	for (i = 0; i < adapter_count; i++) {
		adap = &d->adapter[i];
230
		adap->dev = d;
231 232
		adap->id = i;
		adap->props = &d->props->adapter[i];
233

234 235
		/* speed - when running at FULL speed we need a HW PID filter */
		if (d->udev->speed == USB_SPEED_FULL &&
236
				!(adap->props->caps & DVB_USB_ADAP_HAS_PID_FILTER)) {
237 238
			pr_err("%s: this USB2.0 device cannot be run on a " \
					"USB1.1 port (it lacks a hardware " \
239
					"PID filter)\n", KBUILD_MODNAME);
240 241
			ret = -ENODEV;
			goto err;
242
		} else if ((d->udev->speed == USB_SPEED_FULL &&
243 244
				adap->props->caps & DVB_USB_ADAP_HAS_PID_FILTER) ||
				(adap->props->caps & DVB_USB_ADAP_NEED_PID_FILTERING)) {
245
			pr_info("%s: will use the device's hardware PID " \
246
					"filter (table count: %d)\n",
247
					KBUILD_MODNAME,
248
					adap->props->pid_filter_count);
249
			adap->pid_filtering  = 1;
250
			adap->max_feed_count = adap->props->pid_filter_count;
251
		} else {
252
			pr_info("%s: will pass the complete MPEG2 transport " \
253
					"stream to the software demuxer\n",
254
					KBUILD_MODNAME);
255 256 257
			adap->pid_filtering  = 0;
			adap->max_feed_count = 255;
		}
258

259
		if (!adap->pid_filtering && dvb_usb_force_pid_filter_usage &&
260
				adap->props->caps & DVB_USB_ADAP_HAS_PID_FILTER) {
261
			pr_info("%s: pid filter enabled by module option\n",
262
					KBUILD_MODNAME);
263
			adap->pid_filtering  = 1;
264
			adap->max_feed_count = adap->props->pid_filter_count;
265 266
		}

267
		ret = dvb_usbv2_adapter_stream_init(adap);
268
		if (ret)
269
			goto err;
270

271
		ret = dvb_usbv2_adapter_dvb_init(adap);
272
		if (ret)
273
			goto err;
274

275
		ret = dvb_usbv2_adapter_frontend_init(adap);
276
		if (ret)
277
			goto err;
278 279

		/* use exclusive FE lock if there is multiple shared FEs */
280
		if (adap->fe[1])
281 282 283 284 285 286 287
			adap->dvb_adap.mfe_shared = 1;

		d->num_adapters_initialized++;
		d->state |= DVB_USB_STATE_DVB;
	}

	return 0;
288 289 290
err:
	pr_debug("%s: failed=%d\n", __func__, ret);
	return ret;
291 292
}

293
static int dvb_usbv2_adapter_exit(struct dvb_usb_device *d)
294
{
295
	int i;
296

297 298 299
	pr_debug("%s:\n", __func__);

	for (i = MAX_NO_OF_ADAPTER_PER_DEVICE - 1; i >= 0; i--) {
300 301 302
		dvb_usbv2_adapter_frontend_exit(&d->adapter[i]);
		dvb_usbv2_adapter_dvb_exit(&d->adapter[i]);
		dvb_usbv2_adapter_stream_exit(&d->adapter[i]);
303
	}
304

305 306
	d->num_adapters_initialized = 0;
	d->state &= ~DVB_USB_STATE_DVB;
307

308 309 310 311
	return 0;
}

/* general initialization functions */
312
static int dvb_usbv2_exit(struct dvb_usb_device *d)
313
{
314
	pr_debug("%s: state before exiting everything: %x\n", __func__, d->state);
315 316 317
	dvb_usbv2_remote_exit(d);
	dvb_usbv2_adapter_exit(d);
	dvb_usbv2_i2c_exit(d);
318
	pr_debug("%s: state should be zero now: %x\n", __func__, d->state);
319 320 321
	d->state = DVB_USB_STATE_INIT;
	kfree(d->priv);
	kfree(d);
322

323 324 325
	return 0;
}

326
static int dvb_usbv2_init(struct dvb_usb_device *d)
327 328 329 330 331 332
{
	int ret = 0;

	d->state = DVB_USB_STATE_INIT;

	/* check the capabilities and set appropriate variables */
333
	dvb_usbv2_device_power_ctrl(d, 1);
334

335 336
	if (d->props->read_config) {
		ret = d->props->read_config(d);
337 338 339 340
		if (ret < 0)
			goto err;
	}

341
	ret = dvb_usbv2_i2c_init(d);
342 343
	if (ret < 0)
		goto err;
344

345 346 347
	ret = dvb_usbv2_adapter_init(d);
	if (ret < 0)
		goto err;
348

349 350
	if (d->props->init) {
		ret = d->props->init(d);
351 352 353
		if (ret < 0)
			goto err;
	}
354

355
	ret = dvb_usbv2_remote_init(d);
356 357
	if (ret < 0)
		goto err;
358

359
	dvb_usbv2_device_power_ctrl(d, 0);
360 361

	return 0;
362
err:
363
	dvb_usbv2_device_power_ctrl(d, 0);
364 365
	pr_debug("%s: failed=%d\n", __func__, ret);
	return ret;
366 367
}

368
int dvb_usbv2_device_power_ctrl(struct dvb_usb_device *d, int onoff)
369
{
370 371
	int ret;

372 373 374 375 376
	if (onoff)
		d->powered++;
	else
		d->powered--;

377 378
	if (d->powered == 0 || (onoff && d->powered == 1)) {
		/* when switching from 1 to 0 or from 0 to 1 */
379
		pr_debug("%s: power control=%d\n", __func__, onoff);
380 381
		if (d->props->power_ctrl) {
			ret = d->props->power_ctrl(d, onoff);
382 383
			goto err;
		}
384
	}
385

386
	return 0;
387 388 389
err:
	pr_debug("%s: failed=%d\n", __func__, ret);
	return ret;
390 391
}

392 393 394 395 396 397
/*
 * udev, which is used for the firmware downloading, requires we cannot
 * block during module_init(). module_init() calls USB probe() which
 * is this routine. Due to that we delay actual operation using workqueue
 * and return always success here.
 */
398

399 400 401 402 403 404
static void dvb_usbv2_init_work(struct work_struct *work)
{
	int ret;
	struct dvb_usb_device *d =
			container_of(work, struct dvb_usb_device, probe_work);
	bool cold = false;
405

406 407 408
	d->work_pid = current->pid;

	pr_debug("%s: work_pid=%d\n", __func__, d->work_pid);
409

410 411
	if (d->props->size_of_priv) {
		d->priv = kzalloc(d->props->size_of_priv, GFP_KERNEL);
412 413
		if (!d->priv) {
			pr_err("%s: kzalloc() failed\n", KBUILD_MODNAME);
414
			ret = -ENOMEM;
415
			goto err_usb_driver_release_interface;
416 417
		}
	}
418

419 420
	if (d->props->identify_state) {
		ret = d->props->identify_state(d);
421 422 423 424 425 426
		if (ret == 0) {
			;
		} else if (ret == COLD) {
			cold = true;
			ret = 0;
		} else {
427
			goto err_usb_driver_release_interface;
428
		}
429 430 431
	}

	if (cold) {
432 433
		pr_info("%s: found a '%s' in cold state\n",
				KBUILD_MODNAME, d->name);
434
		ret = dvb_usbv2_download_firmware(d);
435
		if (ret == 0) {
436
			/* device is warm, continue initialization */
437 438
			;
		} else if (ret == RECONNECTS_USB) {
439 440 441 442 443 444 445 446
			/*
			 * USB core will call disconnect() and then probe()
			 * as device reconnects itself from the USB bus.
			 * disconnect() will release all driver resources
			 * and probe() is called for 'new' device. As 'new'
			 * device is warm we should never go here again.
			 */
			return;
447
		} else {
448 449 450 451 452 453
			/* Unexpected fatal error. We must unregister driver
			 * manually from the device, because device is already
			 * register by returning from probe() with success.
			 * usb_driver_release_interface() finally calls
			 * disconnect() in order to free resources.
			 */
454
			goto err_usb_driver_release_interface;
455
		}
456 457
	}

458
	pr_info("%s: found a '%s' in warm state\n", KBUILD_MODNAME, d->name);
459

460
	ret = dvb_usbv2_init(d);
461 462
	if (ret < 0)
		goto err_usb_driver_release_interface;
463

464 465
	pr_info("%s: '%s' successfully initialized and connected\n",
			KBUILD_MODNAME, d->name);
466 467

	return;
468 469 470
err_usb_driver_release_interface:
	pr_info("%s: '%s' error while loading driver (%d)\n", KBUILD_MODNAME,
			d->name, ret);
471
	/* it finally calls disconnect() which frees mem */
472 473
	usb_driver_release_interface(to_usb_driver(d->intf->dev.driver),
			d->intf);
474 475 476 477
	pr_debug("%s: failed=%d\n", __func__, ret);
	return;
}

478
int dvb_usbv2_probe(struct usb_interface *intf,
479 480 481
		const struct usb_device_id *id)
{
	int ret;
482 483 484
	struct dvb_usb_device *d;
	struct dvb_usb_driver_info *driver_info =
			(struct dvb_usb_driver_info *) id->driver_info;
485

486 487
	pr_debug("%s: bInterfaceNumber=%d\n", __func__,
			intf->cur_altsetting->desc.bInterfaceNumber);
488 489 490 491

	if (!id->driver_info) {
		pr_err("%s: driver_info failed\n", KBUILD_MODNAME);
		ret = -ENODEV;
492 493 494
		goto err;
	}

495 496 497 498 499 500
	d = kzalloc(sizeof(struct dvb_usb_device), GFP_KERNEL);
	if (!d) {
		pr_err("%s: kzalloc() failed\n", KBUILD_MODNAME);
		ret = -ENOMEM;
		goto err;
	}
501

502 503 504 505
	d->name = driver_info->name;
	d->rc_map = driver_info->rc_map;
	d->udev = interface_to_usbdev(intf);
	d->intf = intf;
506
	d->props = driver_info->props;
507 508

	if (d->intf->cur_altsetting->desc.bInterfaceNumber !=
509
			d->props->bInterfaceNumber) {
510
		ret = -ENODEV;
511
		goto err_kfree;
512 513
	}

514 515 516 517 518
	mutex_init(&d->usb_mutex);
	mutex_init(&d->i2c_mutex);
	INIT_WORK(&d->probe_work, dvb_usbv2_init_work);
	usb_set_intfdata(intf, d);
	ret = schedule_work(&d->probe_work);
519
	if (ret < 0) {
520
		pr_err("%s: schedule_work() failed\n", KBUILD_MODNAME);
521 522 523 524 525
		goto err_kfree;
	}

	return 0;
err_kfree:
526
	kfree(d);
527 528 529 530
err:
	pr_debug("%s: failed=%d\n", __func__, ret);
	return ret;
}
531
EXPORT_SYMBOL(dvb_usbv2_probe);
532

533
void dvb_usbv2_disconnect(struct usb_interface *intf)
534 535
{
	struct dvb_usb_device *d = usb_get_intfdata(intf);
536
	const char *name;
537

538 539
	pr_debug("%s: pid=%d work_pid=%d\n", __func__, current->pid,
			d->work_pid);
540

541 542 543
	/* ensure initialization work is finished until release resources */
	if (d->work_pid != current->pid)
		cancel_work_sync(&d->probe_work);
544

545 546
	if (d->props->disconnect)
		d->props->disconnect(d);
547

548 549
	name = d->name;
	dvb_usbv2_exit(d);
550 551 552

	pr_info("%s: '%s' successfully deinitialized and disconnected\n",
			KBUILD_MODNAME, name);
553
}
554
EXPORT_SYMBOL(dvb_usbv2_disconnect);
555

556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
int dvb_usbv2_suspend(struct usb_interface *intf, pm_message_t msg)
{
	struct dvb_usb_device *d = usb_get_intfdata(intf);
	int i;

	pr_debug("%s:\n", __func__);

	/* stop remote controller poll */
	if (d->rc.query && !d->rc.bulk_mode)
		cancel_delayed_work_sync(&d->rc_query_work);

	/* stop streaming */
	for (i = d->num_adapters_initialized - 1; i >= 0; i--) {
		if (d->adapter[i].active_fe != -1)
			usb_urb_killv2(&d->adapter[i].stream);
	}

	return 0;
}
EXPORT_SYMBOL(dvb_usbv2_suspend);

int dvb_usbv2_resume(struct usb_interface *intf)
{
	struct dvb_usb_device *d = usb_get_intfdata(intf);
	int i;

	pr_debug("%s:\n", __func__);

	/* start streaming */
	for (i = 0; i < d->num_adapters_initialized; i++) {
		if (d->adapter[i].active_fe != -1)
			usb_urb_submitv2(&d->adapter[i].stream, NULL);
	}

	/* start remote controller poll */
	if (d->rc.query && !d->rc.bulk_mode)
		schedule_delayed_work(&d->rc_query_work,
				msecs_to_jiffies(d->rc.interval));

	return 0;
}
EXPORT_SYMBOL(dvb_usbv2_resume);

599 600 601 602
MODULE_VERSION("1.0");
MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@desy.de>");
MODULE_DESCRIPTION("A library module containing commonly used USB and DVB function USB DVB devices");
MODULE_LICENSE("GPL");