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