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/media/dvb-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 
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 		if ((ret = dvb_usb_adapter_stream_init(adap)) ||
83 			(ret = dvb_usb_adapter_dvb_init(adap, adapter_nrs)) ||
84 			(ret = dvb_usb_adapter_frontend_init(adap))) {
85 			return ret;
86 		}
87 
88 		/* use exclusive FE lock if there is multiple shared FEs */
89 		if (adap->fe_adap[1].fe)
90 			adap->dvb_adap.mfe_shared = 1;
91 
92 		d->num_adapters_initialized++;
93 		d->state |= DVB_USB_STATE_DVB;
94 	}
95 
96 	/*
97 	 * when reloading the driver w/o replugging the device
98 	 * sometimes a timeout occurs, this helps
99 	 */
100 	if (d->props.generic_bulk_ctrl_endpoint != 0) {
101 		usb_clear_halt(d->udev, usb_sndbulkpipe(d->udev, d->props.generic_bulk_ctrl_endpoint));
102 		usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, d->props.generic_bulk_ctrl_endpoint));
103 	}
104 
105 	return 0;
106 }
107 
108 static int dvb_usb_adapter_exit(struct dvb_usb_device *d)
109 {
110 	int n;
111 
112 	for (n = 0; n < d->num_adapters_initialized; n++) {
113 		dvb_usb_adapter_frontend_exit(&d->adapter[n]);
114 		dvb_usb_adapter_dvb_exit(&d->adapter[n]);
115 		dvb_usb_adapter_stream_exit(&d->adapter[n]);
116 		kfree(d->adapter[n].priv);
117 	}
118 	d->num_adapters_initialized = 0;
119 	d->state &= ~DVB_USB_STATE_DVB;
120 	return 0;
121 }
122 
123 
124 /* general initialization functions */
125 static int dvb_usb_exit(struct dvb_usb_device *d)
126 {
127 	deb_info("state before exiting everything: %x\n", d->state);
128 	dvb_usb_remote_exit(d);
129 	dvb_usb_adapter_exit(d);
130 	dvb_usb_i2c_exit(d);
131 	deb_info("state should be zero now: %x\n", d->state);
132 	d->state = DVB_USB_STATE_INIT;
133 
134 	if (d->priv != NULL && d->props.priv_destroy != NULL)
135 		d->props.priv_destroy(d);
136 
137 	kfree(d->priv);
138 	kfree(d);
139 	return 0;
140 }
141 
142 static int dvb_usb_init(struct dvb_usb_device *d, short *adapter_nums)
143 {
144 	int ret = 0;
145 
146 	mutex_init(&d->data_mutex);
147 	mutex_init(&d->usb_mutex);
148 	mutex_init(&d->i2c_mutex);
149 
150 	d->state = DVB_USB_STATE_INIT;
151 
152 	if (d->props.size_of_priv > 0) {
153 		d->priv = kzalloc(d->props.size_of_priv, GFP_KERNEL);
154 		if (d->priv == NULL) {
155 			err("no memory for priv in 'struct dvb_usb_device'");
156 			return -ENOMEM;
157 		}
158 
159 		if (d->props.priv_init != NULL) {
160 			ret = d->props.priv_init(d);
161 			if (ret != 0) {
162 				kfree(d->priv);
163 				d->priv = NULL;
164 				return ret;
165 			}
166 		}
167 	}
168 
169 	/* check the capabilities and set appropriate variables */
170 	dvb_usb_device_power_ctrl(d, 1);
171 
172 	if ((ret = dvb_usb_i2c_init(d)) ||
173 		(ret = dvb_usb_adapter_init(d, adapter_nums))) {
174 		dvb_usb_exit(d);
175 		return ret;
176 	}
177 
178 	if ((ret = dvb_usb_remote_init(d)))
179 		err("could not initialize remote control.");
180 
181 	dvb_usb_device_power_ctrl(d, 0);
182 
183 	return 0;
184 }
185 
186 /* determine the name and the state of the just found USB device */
187 static struct dvb_usb_device_description *dvb_usb_find_device(struct usb_device *udev, struct dvb_usb_device_properties *props, int *cold)
188 {
189 	int i, j;
190 	struct dvb_usb_device_description *desc = NULL;
191 
192 	*cold = -1;
193 
194 	for (i = 0; i < props->num_device_descs; i++) {
195 
196 		for (j = 0; j < DVB_USB_ID_MAX_NUM && props->devices[i].cold_ids[j] != NULL; j++) {
197 			deb_info("check for cold %x %x\n", props->devices[i].cold_ids[j]->idVendor, props->devices[i].cold_ids[j]->idProduct);
198 			if (props->devices[i].cold_ids[j]->idVendor  == le16_to_cpu(udev->descriptor.idVendor) &&
199 				props->devices[i].cold_ids[j]->idProduct == le16_to_cpu(udev->descriptor.idProduct)) {
200 				*cold = 1;
201 				desc = &props->devices[i];
202 				break;
203 			}
204 		}
205 
206 		if (desc != NULL)
207 			break;
208 
209 		for (j = 0; j < DVB_USB_ID_MAX_NUM && props->devices[i].warm_ids[j] != NULL; j++) {
210 			deb_info("check for warm %x %x\n", props->devices[i].warm_ids[j]->idVendor, props->devices[i].warm_ids[j]->idProduct);
211 			if (props->devices[i].warm_ids[j]->idVendor == le16_to_cpu(udev->descriptor.idVendor) &&
212 				props->devices[i].warm_ids[j]->idProduct == le16_to_cpu(udev->descriptor.idProduct)) {
213 				*cold = 0;
214 				desc = &props->devices[i];
215 				break;
216 			}
217 		}
218 	}
219 
220 	if (desc != NULL && props->identify_state != NULL)
221 		props->identify_state(udev, props, &desc, cold);
222 
223 	return desc;
224 }
225 
226 int dvb_usb_device_power_ctrl(struct dvb_usb_device *d, int onoff)
227 {
228 	if (onoff)
229 		d->powered++;
230 	else
231 		d->powered--;
232 
233 	if (d->powered == 0 || (onoff && d->powered == 1)) { /* when switching from 1 to 0 or from 0 to 1 */
234 		deb_info("power control: %d\n", onoff);
235 		if (d->props.power_ctrl)
236 			return d->props.power_ctrl(d, onoff);
237 	}
238 	return 0;
239 }
240 
241 /*
242  * USB
243  */
244 int dvb_usb_device_init(struct usb_interface *intf,
245 			struct dvb_usb_device_properties *props,
246 			struct module *owner, struct dvb_usb_device **du,
247 			short *adapter_nums)
248 {
249 	struct usb_device *udev = interface_to_usbdev(intf);
250 	struct dvb_usb_device *d = NULL;
251 	struct dvb_usb_device_description *desc = NULL;
252 
253 	int ret = -ENOMEM, cold = 0;
254 
255 	if (du != NULL)
256 		*du = NULL;
257 
258 	if ((desc = dvb_usb_find_device(udev, props, &cold)) == NULL) {
259 		deb_err("something went very wrong, device was not found in current device list - let's see what comes next.\n");
260 		return -ENODEV;
261 	}
262 
263 	if (cold) {
264 		info("found a '%s' in cold state, will try to load a firmware", desc->name);
265 		ret = dvb_usb_download_firmware(udev, props);
266 		if (!props->no_reconnect || ret != 0)
267 			return ret;
268 	}
269 
270 	info("found a '%s' in warm state.", desc->name);
271 	d = kzalloc(sizeof(struct dvb_usb_device), GFP_KERNEL);
272 	if (d == NULL) {
273 		err("no memory for 'struct dvb_usb_device'");
274 		return -ENOMEM;
275 	}
276 
277 	d->udev = udev;
278 	memcpy(&d->props, props, sizeof(struct dvb_usb_device_properties));
279 	d->desc = desc;
280 	d->owner = owner;
281 
282 	usb_set_intfdata(intf, d);
283 
284 	if (du != NULL)
285 		*du = d;
286 
287 	ret = dvb_usb_init(d, adapter_nums);
288 
289 	if (ret == 0)
290 		info("%s successfully initialized and connected.", desc->name);
291 	else
292 		info("%s error while loading driver (%d)", desc->name, ret);
293 	return ret;
294 }
295 EXPORT_SYMBOL(dvb_usb_device_init);
296 
297 void dvb_usb_device_exit(struct usb_interface *intf)
298 {
299 	struct dvb_usb_device *d = usb_get_intfdata(intf);
300 	const char *default_name = "generic DVB-USB module";
301 	char name[40];
302 
303 	usb_set_intfdata(intf, NULL);
304 	if (d != NULL && d->desc != NULL) {
305 		strscpy(name, d->desc->name, sizeof(name));
306 		dvb_usb_exit(d);
307 	} else {
308 		strscpy(name, default_name, sizeof(name));
309 	}
310 	info("%s successfully deinitialized and disconnected.", name);
311 
312 }
313 EXPORT_SYMBOL(dvb_usb_device_exit);
314 
315 MODULE_VERSION("1.0");
316 MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@posteo.de>");
317 MODULE_DESCRIPTION("A library module containing commonly used USB and DVB function USB DVB devices");
318 MODULE_LICENSE("GPL");
319