• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 */
17 int dvb_usb_debug;
18 module_param_named(debug, dvb_usb_debug, int, 0644);
19 MODULE_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 
21 int dvb_usb_disable_rc_polling;
22 module_param_named(disable_rc_polling, dvb_usb_disable_rc_polling, int, 0644);
23 MODULE_PARM_DESC(disable_rc_polling, "disable remote control polling (default: 0).");
24 
25 static int dvb_usb_force_pid_filter_usage;
26 module_param_named(force_pid_filter_usage, dvb_usb_force_pid_filter_usage, int, 0444);
27 MODULE_PARM_DESC(force_pid_filter_usage, "force all dvb-usb-devices to use a PID filter, if any (default: 0).");
28 
dvb_usb_adapter_init(struct dvb_usb_device * d,short * adapter_nrs)29 static int dvb_usb_adapter_init(struct dvb_usb_device *d, short *adapter_nrs)
30 {
31 	struct dvb_usb_adapter *adap;
32 	int ret, n, o;
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 		for (o = 0; o < adap->props.num_frontends; o++) {
42 			struct dvb_usb_adapter_fe_properties *props = &adap->props.fe[o];
43 			/* speed - when running at FULL speed we need a HW PID filter */
44 			if (d->udev->speed == USB_SPEED_FULL && !(props->caps & DVB_USB_ADAP_HAS_PID_FILTER)) {
45 				err("This USB2.0 device cannot be run on a USB1.1 port. (it lacks a hardware PID filter)");
46 				return -ENODEV;
47 			}
48 
49 			if ((d->udev->speed == USB_SPEED_FULL && props->caps & DVB_USB_ADAP_HAS_PID_FILTER) ||
50 				(props->caps & DVB_USB_ADAP_NEED_PID_FILTERING)) {
51 				info("will use the device's hardware PID filter (table count: %d).", props->pid_filter_count);
52 				adap->fe_adap[o].pid_filtering  = 1;
53 				adap->fe_adap[o].max_feed_count = props->pid_filter_count;
54 			} else {
55 				info("will pass the complete MPEG2 transport stream to the software demuxer.");
56 				adap->fe_adap[o].pid_filtering  = 0;
57 				adap->fe_adap[o].max_feed_count = 255;
58 			}
59 
60 			if (!adap->fe_adap[o].pid_filtering &&
61 				dvb_usb_force_pid_filter_usage &&
62 				props->caps & DVB_USB_ADAP_HAS_PID_FILTER) {
63 				info("pid filter enabled by module option.");
64 				adap->fe_adap[o].pid_filtering  = 1;
65 				adap->fe_adap[o].max_feed_count = props->pid_filter_count;
66 			}
67 
68 			if (props->size_of_priv > 0) {
69 				adap->fe_adap[o].priv = kzalloc(props->size_of_priv, GFP_KERNEL);
70 				if (adap->fe_adap[o].priv == NULL) {
71 					err("no memory for priv for adapter %d fe %d.", n, o);
72 					return -ENOMEM;
73 				}
74 			}
75 		}
76 
77 		if (adap->props.size_of_priv > 0) {
78 			adap->priv = kzalloc(adap->props.size_of_priv, GFP_KERNEL);
79 			if (adap->priv == NULL) {
80 				err("no memory for priv for adapter %d.", n);
81 				return -ENOMEM;
82 			}
83 		}
84 
85 		ret = dvb_usb_adapter_stream_init(adap);
86 		if (ret)
87 			return ret;
88 
89 		ret = dvb_usb_adapter_dvb_init(adap, adapter_nrs);
90 		if (ret)
91 			goto dvb_init_err;
92 
93 		ret = dvb_usb_adapter_frontend_init(adap);
94 		if (ret)
95 			goto frontend_init_err;
96 
97 		/* use exclusive FE lock if there is multiple shared FEs */
98 		if (adap->fe_adap[1].fe)
99 			adap->dvb_adap.mfe_shared = 1;
100 
101 		d->num_adapters_initialized++;
102 		d->state |= DVB_USB_STATE_DVB;
103 	}
104 
105 	/*
106 	 * when reloading the driver w/o replugging the device
107 	 * sometimes a timeout occures, this helps
108 	 */
109 	if (d->props.generic_bulk_ctrl_endpoint != 0) {
110 		usb_clear_halt(d->udev, usb_sndbulkpipe(d->udev, d->props.generic_bulk_ctrl_endpoint));
111 		usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, d->props.generic_bulk_ctrl_endpoint));
112 	}
113 
114 	return 0;
115 
116 frontend_init_err:
117 	dvb_usb_adapter_dvb_exit(adap);
118 dvb_init_err:
119 	dvb_usb_adapter_stream_exit(adap);
120 	return ret;
121 }
122 
dvb_usb_adapter_exit(struct dvb_usb_device * d)123 static int dvb_usb_adapter_exit(struct dvb_usb_device *d)
124 {
125 	int n;
126 
127 	for (n = 0; n < d->num_adapters_initialized; n++) {
128 		dvb_usb_adapter_frontend_exit(&d->adapter[n]);
129 		dvb_usb_adapter_dvb_exit(&d->adapter[n]);
130 		dvb_usb_adapter_stream_exit(&d->adapter[n]);
131 		kfree(d->adapter[n].priv);
132 	}
133 	d->num_adapters_initialized = 0;
134 	d->state &= ~DVB_USB_STATE_DVB;
135 	return 0;
136 }
137 
138 
139 /* general initialization functions */
dvb_usb_exit(struct dvb_usb_device * d)140 static int dvb_usb_exit(struct dvb_usb_device *d)
141 {
142 	deb_info("state before exiting everything: %x\n", d->state);
143 	dvb_usb_remote_exit(d);
144 	dvb_usb_adapter_exit(d);
145 	dvb_usb_i2c_exit(d);
146 	deb_info("state should be zero now: %x\n", d->state);
147 	d->state = DVB_USB_STATE_INIT;
148 	kfree(d->priv);
149 	kfree(d);
150 	return 0;
151 }
152 
dvb_usb_init(struct dvb_usb_device * d,short * adapter_nums)153 static int dvb_usb_init(struct dvb_usb_device *d, short *adapter_nums)
154 {
155 	int ret = 0;
156 
157 	mutex_init(&d->usb_mutex);
158 	mutex_init(&d->i2c_mutex);
159 
160 	d->state = DVB_USB_STATE_INIT;
161 
162 	if (d->props.size_of_priv > 0) {
163 		d->priv = kzalloc(d->props.size_of_priv, GFP_KERNEL);
164 		if (d->priv == NULL) {
165 			err("no memory for priv in 'struct dvb_usb_device'");
166 			return -ENOMEM;
167 		}
168 	}
169 
170 	/* check the capabilities and set appropriate variables */
171 	dvb_usb_device_power_ctrl(d, 1);
172 
173 	if ((ret = dvb_usb_i2c_init(d)) ||
174 		(ret = dvb_usb_adapter_init(d, adapter_nums))) {
175 		dvb_usb_exit(d);
176 		return ret;
177 	}
178 
179 	if ((ret = dvb_usb_remote_init(d)))
180 		err("could not initialize remote control.");
181 
182 	dvb_usb_device_power_ctrl(d, 0);
183 
184 	return 0;
185 }
186 
187 /* determine the name and the state of the just found USB device */
dvb_usb_find_device(struct usb_device * udev,struct dvb_usb_device_properties * props,int * cold)188 static struct dvb_usb_device_description *dvb_usb_find_device(struct usb_device *udev, struct dvb_usb_device_properties *props, int *cold)
189 {
190 	int i, j;
191 	struct dvb_usb_device_description *desc = NULL;
192 
193 	*cold = -1;
194 
195 	for (i = 0; i < props->num_device_descs; i++) {
196 
197 		for (j = 0; j < DVB_USB_ID_MAX_NUM && props->devices[i].cold_ids[j] != NULL; j++) {
198 			deb_info("check for cold %x %x\n", props->devices[i].cold_ids[j]->idVendor, props->devices[i].cold_ids[j]->idProduct);
199 			if (props->devices[i].cold_ids[j]->idVendor  == le16_to_cpu(udev->descriptor.idVendor) &&
200 				props->devices[i].cold_ids[j]->idProduct == le16_to_cpu(udev->descriptor.idProduct)) {
201 				*cold = 1;
202 				desc = &props->devices[i];
203 				break;
204 			}
205 		}
206 
207 		if (desc != NULL)
208 			break;
209 
210 		for (j = 0; j < DVB_USB_ID_MAX_NUM && props->devices[i].warm_ids[j] != NULL; j++) {
211 			deb_info("check for warm %x %x\n", props->devices[i].warm_ids[j]->idVendor, props->devices[i].warm_ids[j]->idProduct);
212 			if (props->devices[i].warm_ids[j]->idVendor == le16_to_cpu(udev->descriptor.idVendor) &&
213 				props->devices[i].warm_ids[j]->idProduct == le16_to_cpu(udev->descriptor.idProduct)) {
214 				*cold = 0;
215 				desc = &props->devices[i];
216 				break;
217 			}
218 		}
219 	}
220 
221 	if (desc != NULL && props->identify_state != NULL)
222 		props->identify_state(udev, props, &desc, cold);
223 
224 	return desc;
225 }
226 
dvb_usb_device_power_ctrl(struct dvb_usb_device * d,int onoff)227 int dvb_usb_device_power_ctrl(struct dvb_usb_device *d, int onoff)
228 {
229 	if (onoff)
230 		d->powered++;
231 	else
232 		d->powered--;
233 
234 	if (d->powered == 0 || (onoff && d->powered == 1)) { /* when switching from 1 to 0 or from 0 to 1 */
235 		deb_info("power control: %d\n", onoff);
236 		if (d->props.power_ctrl)
237 			return d->props.power_ctrl(d, onoff);
238 	}
239 	return 0;
240 }
241 
242 /*
243  * USB
244  */
dvb_usb_device_init(struct usb_interface * intf,struct dvb_usb_device_properties * props,struct module * owner,struct dvb_usb_device ** du,short * adapter_nums)245 int dvb_usb_device_init(struct usb_interface *intf,
246 			struct dvb_usb_device_properties *props,
247 			struct module *owner, struct dvb_usb_device **du,
248 			short *adapter_nums)
249 {
250 	struct usb_device *udev = interface_to_usbdev(intf);
251 	struct dvb_usb_device *d = NULL;
252 	struct dvb_usb_device_description *desc = NULL;
253 
254 	int ret = -ENOMEM, cold = 0;
255 
256 	if (du != NULL)
257 		*du = NULL;
258 
259 	if ((desc = dvb_usb_find_device(udev, props, &cold)) == NULL) {
260 		deb_err("something went very wrong, device was not found in current device list - let's see what comes next.\n");
261 		return -ENODEV;
262 	}
263 
264 	if (cold) {
265 		info("found a '%s' in cold state, will try to load a firmware", desc->name);
266 		ret = dvb_usb_download_firmware(udev, props);
267 		if (!props->no_reconnect || ret != 0)
268 			return ret;
269 	}
270 
271 	info("found a '%s' in warm state.", desc->name);
272 	d = kzalloc(sizeof(struct dvb_usb_device), GFP_KERNEL);
273 	if (d == NULL) {
274 		err("no memory for 'struct dvb_usb_device'");
275 		return -ENOMEM;
276 	}
277 
278 	d->udev = udev;
279 	memcpy(&d->props, props, sizeof(struct dvb_usb_device_properties));
280 	d->desc = desc;
281 	d->owner = owner;
282 
283 	usb_set_intfdata(intf, d);
284 
285 	if (du != NULL)
286 		*du = d;
287 
288 	ret = dvb_usb_init(d, adapter_nums);
289 
290 	if (ret == 0)
291 		info("%s successfully initialized and connected.", desc->name);
292 	else
293 		info("%s error while loading driver (%d)", desc->name, ret);
294 	return ret;
295 }
296 EXPORT_SYMBOL(dvb_usb_device_init);
297 
dvb_usb_device_exit(struct usb_interface * intf)298 void dvb_usb_device_exit(struct usb_interface *intf)
299 {
300 	struct dvb_usb_device *d = usb_get_intfdata(intf);
301 	const char *default_name = "generic DVB-USB module";
302 	char name[40];
303 
304 	usb_set_intfdata(intf, NULL);
305 	if (d != NULL && d->desc != NULL) {
306 		strscpy(name, d->desc->name, sizeof(name));
307 		dvb_usb_exit(d);
308 	} else {
309 		strscpy(name, default_name, sizeof(name));
310 	}
311 	info("%s successfully deinitialized and disconnected.", name);
312 
313 }
314 EXPORT_SYMBOL(dvb_usb_device_exit);
315 
316 MODULE_VERSION("1.0");
317 MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@desy.de>");
318 MODULE_DESCRIPTION("A library module containing commonly used USB and DVB function USB DVB devices");
319 MODULE_LICENSE("GPL");
320