PageRenderTime 32ms CodeModel.GetById 13ms app.highlight 17ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/media/dvb/dvb-usb/dvb-usb-init.c

https://bitbucket.org/ndreys/linux-sunxi
C | 289 lines | 212 code | 52 blank | 25 comment | 65 complexity | 48f693862a65436bd2acd1de9888595f MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1/*
  2 * DVB USB library - provides a generic interface for a DVB USB device driver.
  3 *
  4 * dvb-usb-init.c
  5 *
  6 * Copyright (C) 2004-6 Patrick Boettcher (patrick.boettcher@desy.de)
  7 *
  8 *	This program is free software; you can redistribute it and/or modify it
  9 *	under the terms of the GNU General Public License as published by the Free
 10 *	Software Foundation, version 2.
 11 *
 12 * see Documentation/dvb/README.dvb-usb for more information
 13 */
 14#include "dvb-usb-common.h"
 15
 16/* debug */
 17int dvb_usb_debug;
 18module_param_named(debug, dvb_usb_debug, int, 0644);
 19MODULE_PARM_DESC(debug, "set debugging level (1=info,xfer=2,pll=4,ts=8,err=16,rc=32,fw=64,mem=128,uxfer=256  (or-able))." DVB_USB_DEBUG_STATUS);
 20
 21int dvb_usb_disable_rc_polling;
 22module_param_named(disable_rc_polling, dvb_usb_disable_rc_polling, int, 0644);
 23MODULE_PARM_DESC(disable_rc_polling, "disable remote control polling (default: 0).");
 24
 25static int dvb_usb_force_pid_filter_usage;
 26module_param_named(force_pid_filter_usage, dvb_usb_force_pid_filter_usage, int, 0444);
 27MODULE_PARM_DESC(force_pid_filter_usage, "force all dvb-usb-devices to use a PID filter, if any (default: 0).");
 28
 29static int dvb_usb_adapter_init(struct dvb_usb_device *d, short *adapter_nrs)
 30{
 31	struct dvb_usb_adapter *adap;
 32	int ret, n;
 33
 34	for (n = 0; n < d->props.num_adapters; n++) {
 35		adap = &d->adapter[n];
 36		adap->dev = d;
 37		adap->id  = n;
 38
 39		memcpy(&adap->props, &d->props.adapter[n], sizeof(struct dvb_usb_adapter_properties));
 40
 41		/* speed - when running at FULL speed we need a HW PID filter */
 42		if (d->udev->speed == USB_SPEED_FULL && !(adap->props.caps & DVB_USB_ADAP_HAS_PID_FILTER)) {
 43			err("This USB2.0 device cannot be run on a USB1.1 port. (it lacks a hardware PID filter)");
 44			return -ENODEV;
 45		}
 46
 47		if ((d->udev->speed == USB_SPEED_FULL && adap->props.caps & DVB_USB_ADAP_HAS_PID_FILTER) ||
 48			(adap->props.caps & DVB_USB_ADAP_NEED_PID_FILTERING)) {
 49			info("will use the device's hardware PID filter (table count: %d).", adap->props.pid_filter_count);
 50			adap->pid_filtering  = 1;
 51			adap->max_feed_count = adap->props.pid_filter_count;
 52		} else {
 53			info("will pass the complete MPEG2 transport stream to the software demuxer.");
 54			adap->pid_filtering  = 0;
 55			adap->max_feed_count = 255;
 56		}
 57
 58		if (!adap->pid_filtering &&
 59			dvb_usb_force_pid_filter_usage &&
 60			adap->props.caps & DVB_USB_ADAP_HAS_PID_FILTER) {
 61			info("pid filter enabled by module option.");
 62			adap->pid_filtering  = 1;
 63			adap->max_feed_count = adap->props.pid_filter_count;
 64		}
 65
 66		if (adap->props.size_of_priv > 0) {
 67			adap->priv = kzalloc(adap->props.size_of_priv, GFP_KERNEL);
 68			if (adap->priv == NULL) {
 69				err("no memory for priv for adapter %d.", n);
 70				return -ENOMEM;
 71			}
 72		}
 73
 74		if ((ret = dvb_usb_adapter_stream_init(adap)) ||
 75			(ret = dvb_usb_adapter_dvb_init(adap, adapter_nrs)) ||
 76			(ret = dvb_usb_adapter_frontend_init(adap))) {
 77			return ret;
 78		}
 79
 80		d->num_adapters_initialized++;
 81		d->state |= DVB_USB_STATE_DVB;
 82	}
 83
 84	/*
 85	 * when reloading the driver w/o replugging the device
 86	 * sometimes a timeout occures, this helps
 87	 */
 88	if (d->props.generic_bulk_ctrl_endpoint != 0) {
 89		usb_clear_halt(d->udev, usb_sndbulkpipe(d->udev, d->props.generic_bulk_ctrl_endpoint));
 90		usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, d->props.generic_bulk_ctrl_endpoint));
 91	}
 92
 93	return 0;
 94}
 95
 96static int dvb_usb_adapter_exit(struct dvb_usb_device *d)
 97{
 98	int n;
 99
100	for (n = 0; n < d->num_adapters_initialized; n++) {
101		dvb_usb_adapter_frontend_exit(&d->adapter[n]);
102		dvb_usb_adapter_dvb_exit(&d->adapter[n]);
103		dvb_usb_adapter_stream_exit(&d->adapter[n]);
104		kfree(d->adapter[n].priv);
105	}
106	d->num_adapters_initialized = 0;
107	d->state &= ~DVB_USB_STATE_DVB;
108	return 0;
109}
110
111
112/* general initialization functions */
113static int dvb_usb_exit(struct dvb_usb_device *d)
114{
115	deb_info("state before exiting everything: %x\n", d->state);
116	dvb_usb_remote_exit(d);
117	dvb_usb_adapter_exit(d);
118	dvb_usb_i2c_exit(d);
119	deb_info("state should be zero now: %x\n", d->state);
120	d->state = DVB_USB_STATE_INIT;
121	kfree(d->priv);
122	kfree(d);
123	return 0;
124}
125
126static int dvb_usb_init(struct dvb_usb_device *d, short *adapter_nums)
127{
128	int ret = 0;
129
130	mutex_init(&d->usb_mutex);
131	mutex_init(&d->i2c_mutex);
132
133	d->state = DVB_USB_STATE_INIT;
134
135	if (d->props.size_of_priv > 0) {
136		d->priv = kzalloc(d->props.size_of_priv, GFP_KERNEL);
137		if (d->priv == NULL) {
138			err("no memory for priv in 'struct dvb_usb_device'");
139			return -ENOMEM;
140		}
141	}
142
143	/* check the capabilities and set appropriate variables */
144	dvb_usb_device_power_ctrl(d, 1);
145
146	if ((ret = dvb_usb_i2c_init(d)) ||
147		(ret = dvb_usb_adapter_init(d, adapter_nums))) {
148		dvb_usb_exit(d);
149		return ret;
150	}
151
152	if ((ret = dvb_usb_remote_init(d)))
153		err("could not initialize remote control.");
154
155	dvb_usb_device_power_ctrl(d, 0);
156
157	return 0;
158}
159
160/* determine the name and the state of the just found USB device */
161static struct dvb_usb_device_description *dvb_usb_find_device(struct usb_device *udev, struct dvb_usb_device_properties *props, int *cold)
162{
163	int i, j;
164	struct dvb_usb_device_description *desc = NULL;
165
166	*cold = -1;
167
168	for (i = 0; i < props->num_device_descs; i++) {
169
170		for (j = 0; j < DVB_USB_ID_MAX_NUM && props->devices[i].cold_ids[j] != NULL; j++) {
171			deb_info("check for cold %x %x\n", props->devices[i].cold_ids[j]->idVendor, props->devices[i].cold_ids[j]->idProduct);
172			if (props->devices[i].cold_ids[j]->idVendor  == le16_to_cpu(udev->descriptor.idVendor) &&
173				props->devices[i].cold_ids[j]->idProduct == le16_to_cpu(udev->descriptor.idProduct)) {
174				*cold = 1;
175				desc = &props->devices[i];
176				break;
177			}
178		}
179
180		if (desc != NULL)
181			break;
182
183		for (j = 0; j < DVB_USB_ID_MAX_NUM && props->devices[i].warm_ids[j] != NULL; j++) {
184			deb_info("check for warm %x %x\n", props->devices[i].warm_ids[j]->idVendor, props->devices[i].warm_ids[j]->idProduct);
185			if (props->devices[i].warm_ids[j]->idVendor == le16_to_cpu(udev->descriptor.idVendor) &&
186				props->devices[i].warm_ids[j]->idProduct == le16_to_cpu(udev->descriptor.idProduct)) {
187				*cold = 0;
188				desc = &props->devices[i];
189				break;
190			}
191		}
192	}
193
194	if (desc != NULL && props->identify_state != NULL)
195		props->identify_state(udev, props, &desc, cold);
196
197	return desc;
198}
199
200int dvb_usb_device_power_ctrl(struct dvb_usb_device *d, int onoff)
201{
202	if (onoff)
203		d->powered++;
204	else
205		d->powered--;
206
207	if (d->powered == 0 || (onoff && d->powered == 1)) { /* when switching from 1 to 0 or from 0 to 1 */
208		deb_info("power control: %d\n", onoff);
209		if (d->props.power_ctrl)
210			return d->props.power_ctrl(d, onoff);
211	}
212	return 0;
213}
214
215/*
216 * USB
217 */
218int dvb_usb_device_init(struct usb_interface *intf,
219			struct dvb_usb_device_properties *props,
220			struct module *owner, struct dvb_usb_device **du,
221			short *adapter_nums)
222{
223	struct usb_device *udev = interface_to_usbdev(intf);
224	struct dvb_usb_device *d = NULL;
225	struct dvb_usb_device_description *desc = NULL;
226
227	int ret = -ENOMEM, cold = 0;
228
229	if (du != NULL)
230		*du = NULL;
231
232	if ((desc = dvb_usb_find_device(udev, props, &cold)) == NULL) {
233		deb_err("something went very wrong, device was not found in current device list - let's see what comes next.\n");
234		return -ENODEV;
235	}
236
237	if (cold) {
238		info("found a '%s' in cold state, will try to load a firmware", desc->name);
239		ret = dvb_usb_download_firmware(udev, props);
240		if (!props->no_reconnect || ret != 0)
241			return ret;
242	}
243
244	info("found a '%s' in warm state.", desc->name);
245	d = kzalloc(sizeof(struct dvb_usb_device), GFP_KERNEL);
246	if (d == NULL) {
247		err("no memory for 'struct dvb_usb_device'");
248		return -ENOMEM;
249	}
250
251	d->udev = udev;
252	memcpy(&d->props, props, sizeof(struct dvb_usb_device_properties));
253	d->desc = desc;
254	d->owner = owner;
255
256	usb_set_intfdata(intf, d);
257
258	if (du != NULL)
259		*du = d;
260
261	ret = dvb_usb_init(d, adapter_nums);
262
263	if (ret == 0)
264		info("%s successfully initialized and connected.", desc->name);
265	else
266		info("%s error while loading driver (%d)", desc->name, ret);
267	return ret;
268}
269EXPORT_SYMBOL(dvb_usb_device_init);
270
271void dvb_usb_device_exit(struct usb_interface *intf)
272{
273	struct dvb_usb_device *d = usb_get_intfdata(intf);
274	const char *name = "generic DVB-USB module";
275
276	usb_set_intfdata(intf, NULL);
277	if (d != NULL && d->desc != NULL) {
278		name = d->desc->name;
279		dvb_usb_exit(d);
280	}
281	info("%s successfully deinitialized and disconnected.", name);
282
283}
284EXPORT_SYMBOL(dvb_usb_device_exit);
285
286MODULE_VERSION("1.0");
287MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@desy.de>");
288MODULE_DESCRIPTION("A library module containing commonly used USB and DVB function USB DVB devices");
289MODULE_LICENSE("GPL");