summaryrefslogtreecommitdiffstats
path: root/drivers/media/dvb/dvb-usb/dvb_usb_init.c
blob: 9dcf4e5257952532c09cccb9c43f8eb14f6f0366 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
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
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
386
387
388
389
390
391
392
393
/*
 * 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
 *	under the terms of the GNU General Public License as published by the
 *	Free Software Foundation, version 2.
 *
 * see Documentation/dvb/README.dvb-usb for more information
 */
#include "dvb_usb_common.h"

int dvb_usb_disable_rc_polling;
module_param_named(disable_rc_polling, dvb_usb_disable_rc_polling, int, 0644);
MODULE_PARM_DESC(disable_rc_polling,
		"disable remote control polling (default: 0).");

static int dvb_usb_force_pid_filter_usage;
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).");

int dvb_usb_download_firmware(struct dvb_usb_device *d)
{
	int ret;
	const struct firmware *fw = NULL;
	const char *name;

	/* resolve firmware name */
	name = d->props.firmware;
	if (d->props.get_firmware_name) {
		ret = d->props.get_firmware_name(d, &name);
		if (ret < 0)
			return ret;
	}

	if (!d->props.download_firmware) {
		ret = -EINVAL;
		goto err;
	}

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

	pr_info("%s: downloading firmware from file '%s'", KBUILD_MODNAME,
			name);

	ret = d->props.download_firmware(d, fw);

	release_firmware(fw);

	if (ret < 0)
		goto err;

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

static int dvb_usb_adapter_init(struct dvb_usb_device *d)
{
	struct dvb_usb_adapter *adap;
	int ret, n, adapter_count;

	/* resolve adapter count */
	adapter_count = d->props.num_adapters;
	if (d->props.get_adapter_count) {
		ret = d->props.get_adapter_count(d);
		if (ret < 0)
			goto err;

		adapter_count = ret;
	}

	for (n = 0; n < adapter_count; n++) {
		adap = &d->adapter[n];
		adap->dev = d;
		adap->id  = n;

		memcpy(&adap->props, &d->props.adapter[n],
				sizeof(struct dvb_usb_adapter_properties));

		/* speed - when running at FULL speed we need a HW PID filter */
		if (d->udev->speed == USB_SPEED_FULL &&
				!(adap->props.caps & DVB_USB_ADAP_HAS_PID_FILTER)) {
			pr_err("%s: this USB2.0 device cannot be run on a " \
					"USB1.1 port (it lacks a hardware " \
					"PID filter)", KBUILD_MODNAME);
			return -ENODEV;
		} else if ((d->udev->speed == USB_SPEED_FULL &&
				adap->props.caps & DVB_USB_ADAP_HAS_PID_FILTER) ||
				(adap->props.caps & DVB_USB_ADAP_NEED_PID_FILTERING)) {
			pr_info("%s: will use the device's hardware PID " \
					"filter (table count: %d)",
					KBUILD_MODNAME,
					adap->props.pid_filter_count);
			adap->pid_filtering  = 1;
			adap->max_feed_count = adap->props.pid_filter_count;
		} else {
			pr_info("%s: will pass the complete MPEG2 transport " \
					"stream to the software demuxer",
					KBUILD_MODNAME);
			adap->pid_filtering  = 0;
			adap->max_feed_count = 255;
		}

		if (!adap->pid_filtering && dvb_usb_force_pid_filter_usage &&
				adap->props.caps & DVB_USB_ADAP_HAS_PID_FILTER) {
			pr_info("%s: pid filter enabled by module option",
					KBUILD_MODNAME);
			adap->pid_filtering  = 1;
			adap->max_feed_count = adap->props.pid_filter_count;
		}

		ret = dvb_usb_adapter_stream_init(adap);
		if (ret)
			return ret;

		ret = dvb_usb_adapter_dvb_init(adap);
		if (ret)
			return ret;

		ret = dvb_usb_adapter_frontend_init(adap);
		if (ret)
			return ret;

		/* use exclusive FE lock if there is multiple shared FEs */
		if (adap->fe[1])
			adap->dvb_adap.mfe_shared = 1;

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

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

static int dvb_usb_adapter_exit(struct dvb_usb_device *d)
{
	int n;

	for (n = 0; n < d->num_adapters_initialized; n++) {
		dvb_usb_adapter_frontend_exit(&d->adapter[n]);
		dvb_usb_adapter_dvb_exit(&d->adapter[n]);
		dvb_usb_adapter_stream_exit(&d->adapter[n]);

	}
	d->num_adapters_initialized = 0;
	d->state &= ~DVB_USB_STATE_DVB;
	return 0;
}

/* general initialization functions */
static int dvb_usb_exit(struct dvb_usb_device *d)
{
	pr_debug("%s: state before exiting everything: %x\n", __func__, d->state);
	dvb_usb_remote_exit(d);
	dvb_usb_adapter_exit(d);
	dvb_usb_i2c_exit(d);
	pr_debug("%s: state should be zero now: %x\n", __func__, d->state);
	d->state = DVB_USB_STATE_INIT;
	kfree(d->priv);
	kfree(d);
	return 0;
}

static int dvb_usb_init(struct dvb_usb_device *d)
{
	int ret = 0;

	d->state = DVB_USB_STATE_INIT;

	/* check the capabilities and set appropriate variables */
	dvb_usb_device_power_ctrl(d, 1);

	/* read config */
	if (d->props.read_config) {
		ret = d->props.read_config(d);
		if (ret < 0)
			goto err;
	}

	ret = dvb_usb_i2c_init(d);
	if (ret == 0)
		ret = dvb_usb_adapter_init(d);

	if (ret) {
		dvb_usb_exit(d);
		return ret;
	}

	if (d->props.init)
		d->props.init(d);

	ret = dvb_usb_remote_init(d);
	if (ret)
		pr_err("%s: could not initialize remote control\n",
				KBUILD_MODNAME);

	dvb_usb_device_power_ctrl(d, 0);

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

int dvb_usb_device_power_ctrl(struct dvb_usb_device *d, int onoff)
{
	if (onoff)
		d->powered++;
	else
		d->powered--;

	if (d->powered == 0 || (onoff && d->powered == 1)) {
		/* when switching from 1 to 0 or from 0 to 1 */
		pr_debug("%s: power control: %d\n", __func__, onoff);
		if (d->props.power_ctrl)
			return d->props.power_ctrl(d, onoff);
	}
	return 0;
}

/*
 * USB
 */

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

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;

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

	if (d->props.size_of_priv) {
		d->priv = kzalloc(d->props.size_of_priv, GFP_KERNEL);
		if (!d->priv) {
			pr_err("%s: kzalloc() failed\n", KBUILD_MODNAME);
			ret = -ENOMEM;
			goto err_usb_driver_release_interface;
		}
	}

	if (d->props.identify_state) {
		ret = d->props.identify_state(d);
		if (ret == 0) {
			;
		} else if (ret == COLD) {
			cold = true;
			ret = 0;
		} else {
			goto err_usb_driver_release_interface;
		}
	}

	if (cold) {
		pr_info("%s: found a '%s' in cold state\n",
				KBUILD_MODNAME, d->name);
		ret = dvb_usb_download_firmware(d);
		if (ret == 0) {
			;
		} else if (ret == RECONNECTS_USB) {
			ret = 0;
			goto exit_usb_driver_release_interface;
		} else {
			goto err_usb_driver_release_interface;
		}
	}

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

	ret = dvb_usb_init(d);
	if (ret < 0)
		goto err_usb_driver_release_interface;

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

	return;
err_usb_driver_release_interface:
	pr_info("%s: '%s' error while loading driver (%d)\n", KBUILD_MODNAME,
			d->name, ret);
exit_usb_driver_release_interface:
	/* it finally calls .disconnect() which frees mem */
	usb_driver_release_interface(to_usb_driver(d->intf->dev.driver),
			d->intf);
	pr_debug("%s: failed=%d\n", __func__, ret);
	return;
}

int dvb_usbv2_device_init(struct usb_interface *intf,
		const struct usb_device_id *id)
{
	int ret;
	struct dvb_usb_device *d;
	struct dvb_usb_driver_info *driver_info =
			(struct dvb_usb_driver_info *) id->driver_info;

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

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

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

	d->name = driver_info->name;
	d->rc_map = driver_info->rc_map;
	d->udev = interface_to_usbdev(intf);
	d->intf = intf;
	memcpy(&d->props, driver_info->props,
			sizeof(struct dvb_usb_device_properties));
	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);
	if (ret < 0) {
		pr_err("%s: schedule_work() failed\n", KBUILD_MODNAME);
		goto err_kfree;
	}

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

EXPORT_SYMBOL(dvb_usbv2_device_init);

void dvb_usbv2_device_exit(struct usb_interface *intf)
{
	struct dvb_usb_device *d = usb_get_intfdata(intf);
	const char *name = "generic DVB-USB module";

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

	/*
	 * FIXME: We should ensure initialization work is finished
	 * until exit from this routine (cancel_work_sync / flush_work).
	 * Unfortunately usb_driver_release_interface() call finally goes
	 * here too and in that case we endup deadlock. How to perform
	 * operation conditionally only on disconned / unload?
	 */

	usb_set_intfdata(intf, NULL);
	if (d) {
		name = d->name;
		dvb_usb_exit(d);
	}

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

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");