• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * DVB USB framework
4  *
5  * Copyright (C) 2004-6 Patrick Boettcher <patrick.boettcher@posteo.de>
6  * Copyright (C) 2012 Antti Palosaari <crope@iki.fi>
7  */
8 
9 #include "dvb_usb_common.h"
10 #include <media/media-device.h>
11 
12 static int dvb_usbv2_disable_rc_polling;
13 module_param_named(disable_rc_polling, dvb_usbv2_disable_rc_polling, int, 0644);
14 MODULE_PARM_DESC(disable_rc_polling,
15 		"disable remote control polling (default: 0)");
16 static int dvb_usb_force_pid_filter_usage;
17 module_param_named(force_pid_filter_usage, dvb_usb_force_pid_filter_usage,
18 		int, 0444);
19 MODULE_PARM_DESC(force_pid_filter_usage,
20 		"force all DVB USB devices to use a PID filter, if any (default: 0)");
21 
dvb_usbv2_download_firmware(struct dvb_usb_device * d,const char * name)22 static int dvb_usbv2_download_firmware(struct dvb_usb_device *d,
23 		const char *name)
24 {
25 	int ret;
26 	const struct firmware *fw;
27 	dev_dbg(&d->udev->dev, "%s:\n", __func__);
28 
29 	if (!d->props->download_firmware) {
30 		ret = -EINVAL;
31 		goto err;
32 	}
33 
34 	ret = request_firmware(&fw, name, &d->udev->dev);
35 	if (ret < 0) {
36 		dev_err(&d->udev->dev,
37 				"%s: Did not find the firmware file '%s' (status %d). You can use <kernel_dir>/scripts/get_dvb_firmware to get the firmware\n",
38 				KBUILD_MODNAME, name, ret);
39 		goto err;
40 	}
41 
42 	dev_info(&d->udev->dev, "%s: downloading firmware from file '%s'\n",
43 			KBUILD_MODNAME, name);
44 
45 	ret = d->props->download_firmware(d, fw);
46 	release_firmware(fw);
47 	if (ret < 0)
48 		goto err;
49 
50 	return ret;
51 err:
52 	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
53 	return ret;
54 }
55 
dvb_usbv2_i2c_init(struct dvb_usb_device * d)56 static int dvb_usbv2_i2c_init(struct dvb_usb_device *d)
57 {
58 	int ret;
59 	dev_dbg(&d->udev->dev, "%s:\n", __func__);
60 
61 	if (!d->props->i2c_algo)
62 		return 0;
63 
64 	strscpy(d->i2c_adap.name, d->name, sizeof(d->i2c_adap.name));
65 	d->i2c_adap.algo = d->props->i2c_algo;
66 	d->i2c_adap.dev.parent = &d->udev->dev;
67 	i2c_set_adapdata(&d->i2c_adap, d);
68 
69 	ret = i2c_add_adapter(&d->i2c_adap);
70 	if (ret < 0) {
71 		d->i2c_adap.algo = NULL;
72 		goto err;
73 	}
74 
75 	return 0;
76 err:
77 	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
78 	return ret;
79 }
80 
dvb_usbv2_i2c_exit(struct dvb_usb_device * d)81 static int dvb_usbv2_i2c_exit(struct dvb_usb_device *d)
82 {
83 	dev_dbg(&d->udev->dev, "%s:\n", __func__);
84 
85 	if (d->i2c_adap.algo)
86 		i2c_del_adapter(&d->i2c_adap);
87 
88 	return 0;
89 }
90 
91 #if IS_ENABLED(CONFIG_RC_CORE)
dvb_usb_read_remote_control(struct work_struct * work)92 static void dvb_usb_read_remote_control(struct work_struct *work)
93 {
94 	struct dvb_usb_device *d = container_of(work,
95 			struct dvb_usb_device, rc_query_work.work);
96 	int ret;
97 
98 	/*
99 	 * When the parameter has been set to 1 via sysfs while the
100 	 * driver was running, or when bulk mode is enabled after IR init.
101 	 */
102 	if (dvb_usbv2_disable_rc_polling || d->rc.bulk_mode) {
103 		d->rc_polling_active = false;
104 		return;
105 	}
106 
107 	ret = d->rc.query(d);
108 	if (ret < 0) {
109 		dev_err(&d->udev->dev, "%s: rc.query() failed=%d\n",
110 				KBUILD_MODNAME, ret);
111 		d->rc_polling_active = false;
112 		return; /* stop polling */
113 	}
114 
115 	schedule_delayed_work(&d->rc_query_work,
116 			msecs_to_jiffies(d->rc.interval));
117 }
118 
dvb_usbv2_remote_init(struct dvb_usb_device * d)119 static int dvb_usbv2_remote_init(struct dvb_usb_device *d)
120 {
121 	int ret;
122 	struct rc_dev *dev;
123 	dev_dbg(&d->udev->dev, "%s:\n", __func__);
124 
125 	if (dvb_usbv2_disable_rc_polling || !d->props->get_rc_config)
126 		return 0;
127 
128 	d->rc.map_name = d->rc_map;
129 	ret = d->props->get_rc_config(d, &d->rc);
130 	if (ret < 0)
131 		goto err;
132 
133 	/* disable rc when there is no keymap defined */
134 	if (!d->rc.map_name)
135 		return 0;
136 
137 	dev = rc_allocate_device(d->rc.driver_type);
138 	if (!dev) {
139 		ret = -ENOMEM;
140 		goto err;
141 	}
142 
143 	dev->dev.parent = &d->udev->dev;
144 	dev->device_name = d->name;
145 	usb_make_path(d->udev, d->rc_phys, sizeof(d->rc_phys));
146 	strlcat(d->rc_phys, "/ir0", sizeof(d->rc_phys));
147 	dev->input_phys = d->rc_phys;
148 	usb_to_input_id(d->udev, &dev->input_id);
149 	dev->driver_name = d->props->driver_name;
150 	dev->map_name = d->rc.map_name;
151 	dev->allowed_protocols = d->rc.allowed_protos;
152 	dev->change_protocol = d->rc.change_protocol;
153 	dev->timeout = d->rc.timeout;
154 	dev->priv = d;
155 
156 	ret = rc_register_device(dev);
157 	if (ret < 0) {
158 		rc_free_device(dev);
159 		goto err;
160 	}
161 
162 	d->rc_dev = dev;
163 
164 	/* start polling if needed */
165 	if (d->rc.query && !d->rc.bulk_mode) {
166 		/* initialize a work queue for handling polling */
167 		INIT_DELAYED_WORK(&d->rc_query_work,
168 				dvb_usb_read_remote_control);
169 		dev_info(&d->udev->dev,
170 				"%s: schedule remote query interval to %d msecs\n",
171 				KBUILD_MODNAME, d->rc.interval);
172 		schedule_delayed_work(&d->rc_query_work,
173 				msecs_to_jiffies(d->rc.interval));
174 		d->rc_polling_active = true;
175 	}
176 
177 	return 0;
178 err:
179 	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
180 	return ret;
181 }
182 
dvb_usbv2_remote_exit(struct dvb_usb_device * d)183 static int dvb_usbv2_remote_exit(struct dvb_usb_device *d)
184 {
185 	dev_dbg(&d->udev->dev, "%s:\n", __func__);
186 
187 	if (d->rc_dev) {
188 		cancel_delayed_work_sync(&d->rc_query_work);
189 		rc_unregister_device(d->rc_dev);
190 		d->rc_dev = NULL;
191 	}
192 
193 	return 0;
194 }
195 #else
196 	#define dvb_usbv2_remote_init(args...) 0
197 	#define dvb_usbv2_remote_exit(args...)
198 #endif
199 
dvb_usb_data_complete(struct usb_data_stream * stream,u8 * buf,size_t len)200 static void dvb_usb_data_complete(struct usb_data_stream *stream, u8 *buf,
201 		size_t len)
202 {
203 	struct dvb_usb_adapter *adap = stream->user_priv;
204 	dvb_dmx_swfilter(&adap->demux, buf, len);
205 }
206 
dvb_usb_data_complete_204(struct usb_data_stream * stream,u8 * buf,size_t len)207 static void dvb_usb_data_complete_204(struct usb_data_stream *stream, u8 *buf,
208 		size_t len)
209 {
210 	struct dvb_usb_adapter *adap = stream->user_priv;
211 	dvb_dmx_swfilter_204(&adap->demux, buf, len);
212 }
213 
dvb_usb_data_complete_raw(struct usb_data_stream * stream,u8 * buf,size_t len)214 static void dvb_usb_data_complete_raw(struct usb_data_stream *stream, u8 *buf,
215 		size_t len)
216 {
217 	struct dvb_usb_adapter *adap = stream->user_priv;
218 	dvb_dmx_swfilter_raw(&adap->demux, buf, len);
219 }
220 
dvb_usbv2_adapter_stream_init(struct dvb_usb_adapter * adap)221 static int dvb_usbv2_adapter_stream_init(struct dvb_usb_adapter *adap)
222 {
223 	dev_dbg(&adap_to_d(adap)->udev->dev, "%s: adap=%d\n", __func__,
224 			adap->id);
225 
226 	adap->stream.udev = adap_to_d(adap)->udev;
227 	adap->stream.user_priv = adap;
228 	adap->stream.complete = dvb_usb_data_complete;
229 
230 	return usb_urb_initv2(&adap->stream, &adap->props->stream);
231 }
232 
dvb_usbv2_adapter_stream_exit(struct dvb_usb_adapter * adap)233 static int dvb_usbv2_adapter_stream_exit(struct dvb_usb_adapter *adap)
234 {
235 	dev_dbg(&adap_to_d(adap)->udev->dev, "%s: adap=%d\n", __func__,
236 			adap->id);
237 
238 	return usb_urb_exitv2(&adap->stream);
239 }
240 
dvb_usb_start_feed(struct dvb_demux_feed * dvbdmxfeed)241 static int dvb_usb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
242 {
243 	struct dvb_usb_adapter *adap = dvbdmxfeed->demux->priv;
244 	struct dvb_usb_device *d = adap_to_d(adap);
245 	int ret = 0;
246 	struct usb_data_stream_properties stream_props;
247 	dev_dbg(&d->udev->dev,
248 			"%s: adap=%d active_fe=%d feed_type=%d setting pid [%s]: %04x (%04d) at index %d\n",
249 			__func__, adap->id, adap->active_fe, dvbdmxfeed->type,
250 			adap->pid_filtering ? "yes" : "no", dvbdmxfeed->pid,
251 			dvbdmxfeed->pid, dvbdmxfeed->index);
252 
253 	/* wait init is done */
254 	wait_on_bit(&adap->state_bits, ADAP_INIT, TASK_UNINTERRUPTIBLE);
255 
256 	if (adap->active_fe == -1)
257 		return -EINVAL;
258 
259 	/* skip feed setup if we are already feeding */
260 	if (adap->feed_count++ > 0)
261 		goto skip_feed_start;
262 
263 	/* set 'streaming' status bit */
264 	set_bit(ADAP_STREAMING, &adap->state_bits);
265 
266 	/* resolve input and output streaming parameters */
267 	if (d->props->get_stream_config) {
268 		memcpy(&stream_props, &adap->props->stream,
269 				sizeof(struct usb_data_stream_properties));
270 		ret = d->props->get_stream_config(adap->fe[adap->active_fe],
271 				&adap->ts_type, &stream_props);
272 		if (ret)
273 			dev_err(&d->udev->dev,
274 					"%s: get_stream_config() failed=%d\n",
275 					KBUILD_MODNAME, ret);
276 	} else {
277 		stream_props = adap->props->stream;
278 	}
279 
280 	switch (adap->ts_type) {
281 	case DVB_USB_FE_TS_TYPE_204:
282 		adap->stream.complete = dvb_usb_data_complete_204;
283 		break;
284 	case DVB_USB_FE_TS_TYPE_RAW:
285 		adap->stream.complete = dvb_usb_data_complete_raw;
286 		break;
287 	case DVB_USB_FE_TS_TYPE_188:
288 	default:
289 		adap->stream.complete = dvb_usb_data_complete;
290 		break;
291 	}
292 
293 	/* submit USB streaming packets */
294 	usb_urb_submitv2(&adap->stream, &stream_props);
295 
296 	/* enable HW PID filter */
297 	if (adap->pid_filtering && adap->props->pid_filter_ctrl) {
298 		ret = adap->props->pid_filter_ctrl(adap, 1);
299 		if (ret)
300 			dev_err(&d->udev->dev,
301 					"%s: pid_filter_ctrl() failed=%d\n",
302 					KBUILD_MODNAME, ret);
303 	}
304 
305 	/* ask device to start streaming */
306 	if (d->props->streaming_ctrl) {
307 		ret = d->props->streaming_ctrl(adap->fe[adap->active_fe], 1);
308 		if (ret)
309 			dev_err(&d->udev->dev,
310 					"%s: streaming_ctrl() failed=%d\n",
311 					KBUILD_MODNAME, ret);
312 	}
313 skip_feed_start:
314 
315 	/* add PID to device HW PID filter */
316 	if (adap->pid_filtering && adap->props->pid_filter) {
317 		ret = adap->props->pid_filter(adap, dvbdmxfeed->index,
318 				dvbdmxfeed->pid, 1);
319 		if (ret)
320 			dev_err(&d->udev->dev, "%s: pid_filter() failed=%d\n",
321 					KBUILD_MODNAME, ret);
322 	}
323 
324 	if (ret)
325 		dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
326 	return ret;
327 }
328 
dvb_usb_stop_feed(struct dvb_demux_feed * dvbdmxfeed)329 static int dvb_usb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
330 {
331 	struct dvb_usb_adapter *adap = dvbdmxfeed->demux->priv;
332 	struct dvb_usb_device *d = adap_to_d(adap);
333 	int ret = 0;
334 	dev_dbg(&d->udev->dev,
335 			"%s: adap=%d active_fe=%d feed_type=%d setting pid [%s]: %04x (%04d) at index %d\n",
336 			__func__, adap->id, adap->active_fe, dvbdmxfeed->type,
337 			adap->pid_filtering ? "yes" : "no", dvbdmxfeed->pid,
338 			dvbdmxfeed->pid, dvbdmxfeed->index);
339 
340 	if (adap->active_fe == -1)
341 		return -EINVAL;
342 
343 	/* remove PID from device HW PID filter */
344 	if (adap->pid_filtering && adap->props->pid_filter) {
345 		ret = adap->props->pid_filter(adap, dvbdmxfeed->index,
346 				dvbdmxfeed->pid, 0);
347 		if (ret)
348 			dev_err(&d->udev->dev, "%s: pid_filter() failed=%d\n",
349 					KBUILD_MODNAME, ret);
350 	}
351 
352 	/* we cannot stop streaming until last PID is removed */
353 	if (--adap->feed_count > 0)
354 		goto skip_feed_stop;
355 
356 	/* ask device to stop streaming */
357 	if (d->props->streaming_ctrl) {
358 		ret = d->props->streaming_ctrl(adap->fe[adap->active_fe], 0);
359 		if (ret)
360 			dev_err(&d->udev->dev,
361 					"%s: streaming_ctrl() failed=%d\n",
362 					KBUILD_MODNAME, ret);
363 	}
364 
365 	/* disable HW PID filter */
366 	if (adap->pid_filtering && adap->props->pid_filter_ctrl) {
367 		ret = adap->props->pid_filter_ctrl(adap, 0);
368 		if (ret)
369 			dev_err(&d->udev->dev,
370 					"%s: pid_filter_ctrl() failed=%d\n",
371 					KBUILD_MODNAME, ret);
372 	}
373 
374 	/* kill USB streaming packets */
375 	usb_urb_killv2(&adap->stream);
376 
377 	/* clear 'streaming' status bit */
378 	clear_bit(ADAP_STREAMING, &adap->state_bits);
379 	smp_mb__after_atomic();
380 	wake_up_bit(&adap->state_bits, ADAP_STREAMING);
381 skip_feed_stop:
382 
383 	if (ret)
384 		dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
385 	return ret;
386 }
387 
dvb_usbv2_media_device_init(struct dvb_usb_adapter * adap)388 static int dvb_usbv2_media_device_init(struct dvb_usb_adapter *adap)
389 {
390 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
391 	struct media_device *mdev;
392 	struct dvb_usb_device *d = adap_to_d(adap);
393 	struct usb_device *udev = d->udev;
394 
395 	mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
396 	if (!mdev)
397 		return -ENOMEM;
398 
399 	media_device_usb_init(mdev, udev, d->name);
400 
401 	dvb_register_media_controller(&adap->dvb_adap, mdev);
402 
403 	dev_info(&d->udev->dev, "media controller created\n");
404 #endif
405 	return 0;
406 }
407 
dvb_usbv2_media_device_register(struct dvb_usb_adapter * adap)408 static int dvb_usbv2_media_device_register(struct dvb_usb_adapter *adap)
409 {
410 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
411 	return media_device_register(adap->dvb_adap.mdev);
412 #else
413 	return 0;
414 #endif
415 }
416 
dvb_usbv2_media_device_unregister(struct dvb_usb_adapter * adap)417 static void dvb_usbv2_media_device_unregister(struct dvb_usb_adapter *adap)
418 {
419 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
420 
421 	if (!adap->dvb_adap.mdev)
422 		return;
423 
424 	media_device_unregister(adap->dvb_adap.mdev);
425 	media_device_cleanup(adap->dvb_adap.mdev);
426 	kfree(adap->dvb_adap.mdev);
427 	adap->dvb_adap.mdev = NULL;
428 
429 #endif
430 }
431 
dvb_usbv2_adapter_dvb_init(struct dvb_usb_adapter * adap)432 static int dvb_usbv2_adapter_dvb_init(struct dvb_usb_adapter *adap)
433 {
434 	int ret;
435 	struct dvb_usb_device *d = adap_to_d(adap);
436 
437 	dev_dbg(&d->udev->dev, "%s: adap=%d\n", __func__, adap->id);
438 
439 	ret = dvb_register_adapter(&adap->dvb_adap, d->name, d->props->owner,
440 			&d->udev->dev, d->props->adapter_nr);
441 	if (ret < 0) {
442 		dev_dbg(&d->udev->dev, "%s: dvb_register_adapter() failed=%d\n",
443 				__func__, ret);
444 		goto err_dvb_register_adapter;
445 	}
446 
447 	adap->dvb_adap.priv = adap;
448 
449 	ret = dvb_usbv2_media_device_init(adap);
450 	if (ret < 0) {
451 		dev_dbg(&d->udev->dev, "%s: dvb_usbv2_media_device_init() failed=%d\n",
452 				__func__, ret);
453 		goto err_dvb_register_mc;
454 	}
455 
456 	if (d->props->read_mac_address) {
457 		ret = d->props->read_mac_address(adap,
458 				adap->dvb_adap.proposed_mac);
459 		if (ret < 0)
460 			goto err_dvb_dmx_init;
461 
462 		dev_info(&d->udev->dev, "%s: MAC address: %pM\n",
463 				KBUILD_MODNAME, adap->dvb_adap.proposed_mac);
464 	}
465 
466 	adap->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING;
467 	adap->demux.priv             = adap;
468 	adap->demux.filternum        = 0;
469 	adap->demux.filternum        = adap->max_feed_count;
470 	adap->demux.feednum          = adap->demux.filternum;
471 	adap->demux.start_feed       = dvb_usb_start_feed;
472 	adap->demux.stop_feed        = dvb_usb_stop_feed;
473 	adap->demux.write_to_decoder = NULL;
474 	ret = dvb_dmx_init(&adap->demux);
475 	if (ret < 0) {
476 		dev_err(&d->udev->dev, "%s: dvb_dmx_init() failed=%d\n",
477 				KBUILD_MODNAME, ret);
478 		goto err_dvb_dmx_init;
479 	}
480 
481 	adap->dmxdev.filternum       = adap->demux.filternum;
482 	adap->dmxdev.demux           = &adap->demux.dmx;
483 	adap->dmxdev.capabilities    = 0;
484 	ret = dvb_dmxdev_init(&adap->dmxdev, &adap->dvb_adap);
485 	if (ret < 0) {
486 		dev_err(&d->udev->dev, "%s: dvb_dmxdev_init() failed=%d\n",
487 				KBUILD_MODNAME, ret);
488 		goto err_dvb_dmxdev_init;
489 	}
490 
491 	ret = dvb_net_init(&adap->dvb_adap, &adap->dvb_net, &adap->demux.dmx);
492 	if (ret < 0) {
493 		dev_err(&d->udev->dev, "%s: dvb_net_init() failed=%d\n",
494 				KBUILD_MODNAME, ret);
495 		goto err_dvb_net_init;
496 	}
497 
498 	return 0;
499 err_dvb_net_init:
500 	dvb_dmxdev_release(&adap->dmxdev);
501 err_dvb_dmxdev_init:
502 	dvb_dmx_release(&adap->demux);
503 err_dvb_dmx_init:
504 	dvb_usbv2_media_device_unregister(adap);
505 err_dvb_register_mc:
506 	dvb_unregister_adapter(&adap->dvb_adap);
507 err_dvb_register_adapter:
508 	adap->dvb_adap.priv = NULL;
509 	return ret;
510 }
511 
dvb_usbv2_adapter_dvb_exit(struct dvb_usb_adapter * adap)512 static int dvb_usbv2_adapter_dvb_exit(struct dvb_usb_adapter *adap)
513 {
514 	dev_dbg(&adap_to_d(adap)->udev->dev, "%s: adap=%d\n", __func__,
515 			adap->id);
516 
517 	if (adap->dvb_adap.priv) {
518 		dvb_net_release(&adap->dvb_net);
519 		adap->demux.dmx.close(&adap->demux.dmx);
520 		dvb_dmxdev_release(&adap->dmxdev);
521 		dvb_dmx_release(&adap->demux);
522 		dvb_unregister_adapter(&adap->dvb_adap);
523 	}
524 
525 	return 0;
526 }
527 
dvb_usbv2_device_power_ctrl(struct dvb_usb_device * d,int onoff)528 static int dvb_usbv2_device_power_ctrl(struct dvb_usb_device *d, int onoff)
529 {
530 	int ret;
531 
532 	if (onoff)
533 		d->powered++;
534 	else
535 		d->powered--;
536 
537 	if (d->powered == 0 || (onoff && d->powered == 1)) {
538 		/* when switching from 1 to 0 or from 0 to 1 */
539 		dev_dbg(&d->udev->dev, "%s: power=%d\n", __func__, onoff);
540 		if (d->props->power_ctrl) {
541 			ret = d->props->power_ctrl(d, onoff);
542 			if (ret < 0)
543 				goto err;
544 		}
545 	}
546 
547 	return 0;
548 err:
549 	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
550 	return ret;
551 }
552 
dvb_usb_fe_init(struct dvb_frontend * fe)553 static int dvb_usb_fe_init(struct dvb_frontend *fe)
554 {
555 	int ret;
556 	struct dvb_usb_adapter *adap = fe->dvb->priv;
557 	struct dvb_usb_device *d = adap_to_d(adap);
558 	dev_dbg(&d->udev->dev, "%s: adap=%d fe=%d\n", __func__, adap->id,
559 			fe->id);
560 
561 	if (!adap->suspend_resume_active) {
562 		adap->active_fe = fe->id;
563 		set_bit(ADAP_INIT, &adap->state_bits);
564 	}
565 
566 	ret = dvb_usbv2_device_power_ctrl(d, 1);
567 	if (ret < 0)
568 		goto err;
569 
570 	if (d->props->frontend_ctrl) {
571 		ret = d->props->frontend_ctrl(fe, 1);
572 		if (ret < 0)
573 			goto err;
574 	}
575 
576 	if (adap->fe_init[fe->id]) {
577 		ret = adap->fe_init[fe->id](fe);
578 		if (ret < 0)
579 			goto err;
580 	}
581 err:
582 	if (!adap->suspend_resume_active) {
583 		clear_bit(ADAP_INIT, &adap->state_bits);
584 		smp_mb__after_atomic();
585 		wake_up_bit(&adap->state_bits, ADAP_INIT);
586 	}
587 
588 	dev_dbg(&d->udev->dev, "%s: ret=%d\n", __func__, ret);
589 	return ret;
590 }
591 
dvb_usb_fe_sleep(struct dvb_frontend * fe)592 static int dvb_usb_fe_sleep(struct dvb_frontend *fe)
593 {
594 	int ret;
595 	struct dvb_usb_adapter *adap = fe->dvb->priv;
596 	struct dvb_usb_device *d = adap_to_d(adap);
597 	dev_dbg(&d->udev->dev, "%s: adap=%d fe=%d\n", __func__, adap->id,
598 			fe->id);
599 
600 	if (!adap->suspend_resume_active) {
601 		set_bit(ADAP_SLEEP, &adap->state_bits);
602 		wait_on_bit(&adap->state_bits, ADAP_STREAMING,
603 				TASK_UNINTERRUPTIBLE);
604 	}
605 
606 	if (adap->fe_sleep[fe->id]) {
607 		ret = adap->fe_sleep[fe->id](fe);
608 		if (ret < 0)
609 			goto err;
610 	}
611 
612 	if (d->props->frontend_ctrl) {
613 		ret = d->props->frontend_ctrl(fe, 0);
614 		if (ret < 0)
615 			goto err;
616 	}
617 
618 	ret = dvb_usbv2_device_power_ctrl(d, 0);
619 
620 err:
621 	if (!adap->suspend_resume_active) {
622 		adap->active_fe = -1;
623 		clear_bit(ADAP_SLEEP, &adap->state_bits);
624 		smp_mb__after_atomic();
625 		wake_up_bit(&adap->state_bits, ADAP_SLEEP);
626 	}
627 
628 	dev_dbg(&d->udev->dev, "%s: ret=%d\n", __func__, ret);
629 	return ret;
630 }
631 
dvb_usbv2_adapter_frontend_init(struct dvb_usb_adapter * adap)632 static int dvb_usbv2_adapter_frontend_init(struct dvb_usb_adapter *adap)
633 {
634 	int ret, i, count_registered = 0;
635 	struct dvb_usb_device *d = adap_to_d(adap);
636 	dev_dbg(&d->udev->dev, "%s: adap=%d\n", __func__, adap->id);
637 
638 	memset(adap->fe, 0, sizeof(adap->fe));
639 	adap->active_fe = -1;
640 
641 	if (d->props->frontend_attach) {
642 		ret = d->props->frontend_attach(adap);
643 		if (ret < 0) {
644 			dev_dbg(&d->udev->dev,
645 					"%s: frontend_attach() failed=%d\n",
646 					__func__, ret);
647 			goto err_dvb_frontend_detach;
648 		}
649 	} else {
650 		dev_dbg(&d->udev->dev, "%s: frontend_attach() do not exists\n",
651 				__func__);
652 		ret = 0;
653 		goto err;
654 	}
655 
656 	for (i = 0; i < MAX_NO_OF_FE_PER_ADAP && adap->fe[i]; i++) {
657 		adap->fe[i]->id = i;
658 		/* re-assign sleep and wakeup functions */
659 		adap->fe_init[i] = adap->fe[i]->ops.init;
660 		adap->fe[i]->ops.init = dvb_usb_fe_init;
661 		adap->fe_sleep[i] = adap->fe[i]->ops.sleep;
662 		adap->fe[i]->ops.sleep = dvb_usb_fe_sleep;
663 
664 		ret = dvb_register_frontend(&adap->dvb_adap, adap->fe[i]);
665 		if (ret < 0) {
666 			dev_err(&d->udev->dev,
667 					"%s: frontend%d registration failed\n",
668 					KBUILD_MODNAME, i);
669 			goto err_dvb_unregister_frontend;
670 		}
671 
672 		count_registered++;
673 	}
674 
675 	if (d->props->tuner_attach) {
676 		ret = d->props->tuner_attach(adap);
677 		if (ret < 0) {
678 			dev_dbg(&d->udev->dev, "%s: tuner_attach() failed=%d\n",
679 					__func__, ret);
680 			goto err_dvb_unregister_frontend;
681 		}
682 	}
683 
684 	ret = dvb_create_media_graph(&adap->dvb_adap, true);
685 	if (ret < 0)
686 		goto err_dvb_unregister_frontend;
687 
688 	ret = dvb_usbv2_media_device_register(adap);
689 
690 	return ret;
691 
692 err_dvb_unregister_frontend:
693 	for (i = count_registered - 1; i >= 0; i--)
694 		dvb_unregister_frontend(adap->fe[i]);
695 
696 err_dvb_frontend_detach:
697 	for (i = MAX_NO_OF_FE_PER_ADAP - 1; i >= 0; i--) {
698 		if (adap->fe[i]) {
699 			dvb_frontend_detach(adap->fe[i]);
700 			adap->fe[i] = NULL;
701 		}
702 	}
703 
704 err:
705 	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
706 	return ret;
707 }
708 
dvb_usbv2_adapter_frontend_exit(struct dvb_usb_adapter * adap)709 static int dvb_usbv2_adapter_frontend_exit(struct dvb_usb_adapter *adap)
710 {
711 	int ret, i;
712 	struct dvb_usb_device *d = adap_to_d(adap);
713 
714 	dev_dbg(&d->udev->dev, "%s: adap=%d\n", __func__, adap->id);
715 
716 	for (i = MAX_NO_OF_FE_PER_ADAP - 1; i >= 0; i--) {
717 		if (adap->fe[i]) {
718 			dvb_unregister_frontend(adap->fe[i]);
719 			dvb_frontend_detach(adap->fe[i]);
720 		}
721 	}
722 
723 	if (d->props->tuner_detach) {
724 		ret = d->props->tuner_detach(adap);
725 		if (ret < 0) {
726 			dev_dbg(&d->udev->dev, "%s: tuner_detach() failed=%d\n",
727 					__func__, ret);
728 		}
729 	}
730 
731 	if (d->props->frontend_detach) {
732 		ret = d->props->frontend_detach(adap);
733 		if (ret < 0) {
734 			dev_dbg(&d->udev->dev,
735 					"%s: frontend_detach() failed=%d\n",
736 					__func__, ret);
737 		}
738 	}
739 
740 	return 0;
741 }
742 
dvb_usbv2_adapter_init(struct dvb_usb_device * d)743 static int dvb_usbv2_adapter_init(struct dvb_usb_device *d)
744 {
745 	struct dvb_usb_adapter *adap;
746 	int ret, i, adapter_count;
747 
748 	/* resolve adapter count */
749 	adapter_count = d->props->num_adapters;
750 	if (d->props->get_adapter_count) {
751 		ret = d->props->get_adapter_count(d);
752 		if (ret < 0)
753 			goto err;
754 
755 		adapter_count = ret;
756 	}
757 
758 	for (i = 0; i < adapter_count; i++) {
759 		adap = &d->adapter[i];
760 		adap->id = i;
761 		adap->props = &d->props->adapter[i];
762 
763 		/* speed - when running at FULL speed we need a HW PID filter */
764 		if (d->udev->speed == USB_SPEED_FULL &&
765 				!(adap->props->caps & DVB_USB_ADAP_HAS_PID_FILTER)) {
766 			dev_err(&d->udev->dev,
767 					"%s: this USB2.0 device cannot be run on a USB1.1 port (it lacks a hardware PID filter)\n",
768 					KBUILD_MODNAME);
769 			ret = -ENODEV;
770 			goto err;
771 		} else if ((d->udev->speed == USB_SPEED_FULL &&
772 				adap->props->caps & DVB_USB_ADAP_HAS_PID_FILTER) ||
773 				(adap->props->caps & DVB_USB_ADAP_NEED_PID_FILTERING)) {
774 			dev_info(&d->udev->dev,
775 					"%s: will use the device's hardware PID filter (table count: %d)\n",
776 					KBUILD_MODNAME,
777 					adap->props->pid_filter_count);
778 			adap->pid_filtering  = 1;
779 			adap->max_feed_count = adap->props->pid_filter_count;
780 		} else {
781 			dev_info(&d->udev->dev,
782 					"%s: will pass the complete MPEG2 transport stream to the software demuxer\n",
783 					KBUILD_MODNAME);
784 			adap->pid_filtering  = 0;
785 			adap->max_feed_count = 255;
786 		}
787 
788 		if (!adap->pid_filtering && dvb_usb_force_pid_filter_usage &&
789 				adap->props->caps & DVB_USB_ADAP_HAS_PID_FILTER) {
790 			dev_info(&d->udev->dev,
791 					"%s: PID filter enabled by module option\n",
792 					KBUILD_MODNAME);
793 			adap->pid_filtering  = 1;
794 			adap->max_feed_count = adap->props->pid_filter_count;
795 		}
796 
797 		ret = dvb_usbv2_adapter_stream_init(adap);
798 		if (ret)
799 			goto err;
800 
801 		ret = dvb_usbv2_adapter_dvb_init(adap);
802 		if (ret)
803 			goto err;
804 
805 		ret = dvb_usbv2_adapter_frontend_init(adap);
806 		if (ret)
807 			goto err;
808 
809 		/* use exclusive FE lock if there is multiple shared FEs */
810 		if (adap->fe[1])
811 			adap->dvb_adap.mfe_shared = 1;
812 	}
813 
814 	return 0;
815 err:
816 	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
817 	return ret;
818 }
819 
dvb_usbv2_adapter_exit(struct dvb_usb_device * d)820 static int dvb_usbv2_adapter_exit(struct dvb_usb_device *d)
821 {
822 	int i;
823 	dev_dbg(&d->udev->dev, "%s:\n", __func__);
824 
825 	for (i = MAX_NO_OF_ADAPTER_PER_DEVICE - 1; i >= 0; i--) {
826 		if (d->adapter[i].props) {
827 			dvb_usbv2_adapter_dvb_exit(&d->adapter[i]);
828 			dvb_usbv2_adapter_stream_exit(&d->adapter[i]);
829 			dvb_usbv2_adapter_frontend_exit(&d->adapter[i]);
830 			dvb_usbv2_media_device_unregister(&d->adapter[i]);
831 		}
832 	}
833 
834 	return 0;
835 }
836 
837 /* general initialization functions */
dvb_usbv2_exit(struct dvb_usb_device * d)838 static int dvb_usbv2_exit(struct dvb_usb_device *d)
839 {
840 	dev_dbg(&d->udev->dev, "%s:\n", __func__);
841 
842 	dvb_usbv2_remote_exit(d);
843 	dvb_usbv2_adapter_exit(d);
844 	dvb_usbv2_i2c_exit(d);
845 
846 	return 0;
847 }
848 
dvb_usbv2_init(struct dvb_usb_device * d)849 static int dvb_usbv2_init(struct dvb_usb_device *d)
850 {
851 	int ret;
852 	dev_dbg(&d->udev->dev, "%s:\n", __func__);
853 
854 	dvb_usbv2_device_power_ctrl(d, 1);
855 
856 	if (d->props->read_config) {
857 		ret = d->props->read_config(d);
858 		if (ret < 0)
859 			goto err;
860 	}
861 
862 	ret = dvb_usbv2_i2c_init(d);
863 	if (ret < 0)
864 		goto err;
865 
866 	ret = dvb_usbv2_adapter_init(d);
867 	if (ret < 0)
868 		goto err;
869 
870 	if (d->props->init) {
871 		ret = d->props->init(d);
872 		if (ret < 0)
873 			goto err;
874 	}
875 
876 	ret = dvb_usbv2_remote_init(d);
877 	if (ret < 0)
878 		goto err;
879 
880 	dvb_usbv2_device_power_ctrl(d, 0);
881 
882 	return 0;
883 err:
884 	dvb_usbv2_device_power_ctrl(d, 0);
885 	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
886 	return ret;
887 }
888 
dvb_usbv2_probe(struct usb_interface * intf,const struct usb_device_id * id)889 int dvb_usbv2_probe(struct usb_interface *intf,
890 		const struct usb_device_id *id)
891 {
892 	int ret;
893 	struct dvb_usb_device *d;
894 	struct usb_device *udev = interface_to_usbdev(intf);
895 	struct dvb_usb_driver_info *driver_info =
896 			(struct dvb_usb_driver_info *) id->driver_info;
897 
898 	dev_dbg(&udev->dev, "%s: bInterfaceNumber=%d\n", __func__,
899 			intf->cur_altsetting->desc.bInterfaceNumber);
900 
901 	if (!id->driver_info) {
902 		dev_err(&udev->dev, "%s: driver_info failed\n", KBUILD_MODNAME);
903 		ret = -ENODEV;
904 		goto err;
905 	}
906 
907 	d = kzalloc(sizeof(struct dvb_usb_device), GFP_KERNEL);
908 	if (!d) {
909 		dev_err(&udev->dev, "%s: kzalloc() failed\n", KBUILD_MODNAME);
910 		ret = -ENOMEM;
911 		goto err;
912 	}
913 
914 	d->intf = intf;
915 	d->name = driver_info->name;
916 	d->rc_map = driver_info->rc_map;
917 	d->udev = udev;
918 	d->props = driver_info->props;
919 
920 	if (intf->cur_altsetting->desc.bInterfaceNumber !=
921 			d->props->bInterfaceNumber) {
922 		ret = -ENODEV;
923 		goto err_kfree_d;
924 	}
925 
926 	mutex_init(&d->usb_mutex);
927 	mutex_init(&d->i2c_mutex);
928 
929 	if (d->props->size_of_priv) {
930 		d->priv = kzalloc(d->props->size_of_priv, GFP_KERNEL);
931 		if (!d->priv) {
932 			dev_err(&d->udev->dev, "%s: kzalloc() failed\n",
933 					KBUILD_MODNAME);
934 			ret = -ENOMEM;
935 			goto err_kfree_d;
936 		}
937 	}
938 
939 	if (d->props->probe) {
940 		ret = d->props->probe(d);
941 		if (ret)
942 			goto err_kfree_priv;
943 	}
944 
945 	if (d->props->identify_state) {
946 		const char *name = NULL;
947 		ret = d->props->identify_state(d, &name);
948 		if (ret == COLD) {
949 			dev_info(&d->udev->dev,
950 					"%s: found a '%s' in cold state\n",
951 					KBUILD_MODNAME, d->name);
952 
953 			if (!name)
954 				name = d->props->firmware;
955 
956 			ret = dvb_usbv2_download_firmware(d, name);
957 			if (ret == 0) {
958 				/* device is warm, continue initialization */
959 				;
960 			} else if (ret == RECONNECTS_USB) {
961 				/*
962 				 * USB core will call disconnect() and then
963 				 * probe() as device reconnects itself from the
964 				 * USB bus. disconnect() will release all driver
965 				 * resources and probe() is called for 'new'
966 				 * device. As 'new' device is warm we should
967 				 * never go here again.
968 				 */
969 				goto exit;
970 			} else {
971 				goto err_free_all;
972 			}
973 		} else if (ret != WARM) {
974 			goto err_free_all;
975 		}
976 	}
977 
978 	dev_info(&d->udev->dev, "%s: found a '%s' in warm state\n",
979 			KBUILD_MODNAME, d->name);
980 
981 	ret = dvb_usbv2_init(d);
982 	if (ret < 0)
983 		goto err_free_all;
984 
985 	dev_info(&d->udev->dev,
986 			"%s: '%s' successfully initialized and connected\n",
987 			KBUILD_MODNAME, d->name);
988 exit:
989 	usb_set_intfdata(intf, d);
990 
991 	return 0;
992 err_free_all:
993 	dvb_usbv2_exit(d);
994 	if (d->props->disconnect)
995 		d->props->disconnect(d);
996 err_kfree_priv:
997 	kfree(d->priv);
998 err_kfree_d:
999 	kfree(d);
1000 err:
1001 	dev_dbg(&udev->dev, "%s: failed=%d\n", __func__, ret);
1002 	return ret;
1003 }
1004 EXPORT_SYMBOL(dvb_usbv2_probe);
1005 
dvb_usbv2_disconnect(struct usb_interface * intf)1006 void dvb_usbv2_disconnect(struct usb_interface *intf)
1007 {
1008 	struct dvb_usb_device *d = usb_get_intfdata(intf);
1009 	const char *devname = kstrdup(dev_name(&d->udev->dev), GFP_KERNEL);
1010 	const char *drvname = d->name;
1011 
1012 	dev_dbg(&d->udev->dev, "%s: bInterfaceNumber=%d\n", __func__,
1013 			intf->cur_altsetting->desc.bInterfaceNumber);
1014 
1015 	if (d->props->exit)
1016 		d->props->exit(d);
1017 
1018 	dvb_usbv2_exit(d);
1019 
1020 	if (d->props->disconnect)
1021 		d->props->disconnect(d);
1022 
1023 	kfree(d->priv);
1024 	kfree(d);
1025 
1026 	pr_info("%s: '%s:%s' successfully deinitialized and disconnected\n",
1027 		KBUILD_MODNAME, drvname, devname);
1028 	kfree(devname);
1029 }
1030 EXPORT_SYMBOL(dvb_usbv2_disconnect);
1031 
dvb_usbv2_suspend(struct usb_interface * intf,pm_message_t msg)1032 int dvb_usbv2_suspend(struct usb_interface *intf, pm_message_t msg)
1033 {
1034 	struct dvb_usb_device *d = usb_get_intfdata(intf);
1035 	int ret = 0, i, active_fe;
1036 	struct dvb_frontend *fe;
1037 	dev_dbg(&d->udev->dev, "%s:\n", __func__);
1038 
1039 	/* stop remote controller poll */
1040 	if (d->rc_polling_active)
1041 		cancel_delayed_work_sync(&d->rc_query_work);
1042 
1043 	for (i = MAX_NO_OF_ADAPTER_PER_DEVICE - 1; i >= 0; i--) {
1044 		active_fe = d->adapter[i].active_fe;
1045 		if (d->adapter[i].dvb_adap.priv && active_fe != -1) {
1046 			fe = d->adapter[i].fe[active_fe];
1047 			d->adapter[i].suspend_resume_active = true;
1048 
1049 			if (d->props->streaming_ctrl)
1050 				d->props->streaming_ctrl(fe, 0);
1051 
1052 			/* stop usb streaming */
1053 			usb_urb_killv2(&d->adapter[i].stream);
1054 
1055 			ret = dvb_frontend_suspend(fe);
1056 		}
1057 	}
1058 
1059 	return ret;
1060 }
1061 EXPORT_SYMBOL(dvb_usbv2_suspend);
1062 
dvb_usbv2_resume_common(struct dvb_usb_device * d)1063 static int dvb_usbv2_resume_common(struct dvb_usb_device *d)
1064 {
1065 	int ret = 0, i, active_fe;
1066 	struct dvb_frontend *fe;
1067 	dev_dbg(&d->udev->dev, "%s:\n", __func__);
1068 
1069 	for (i = 0; i < MAX_NO_OF_ADAPTER_PER_DEVICE; i++) {
1070 		active_fe = d->adapter[i].active_fe;
1071 		if (d->adapter[i].dvb_adap.priv && active_fe != -1) {
1072 			fe = d->adapter[i].fe[active_fe];
1073 
1074 			ret = dvb_frontend_resume(fe);
1075 
1076 			/* resume usb streaming */
1077 			usb_urb_submitv2(&d->adapter[i].stream, NULL);
1078 
1079 			if (d->props->streaming_ctrl)
1080 				d->props->streaming_ctrl(fe, 1);
1081 
1082 			d->adapter[i].suspend_resume_active = false;
1083 		}
1084 	}
1085 
1086 	/* start remote controller poll */
1087 	if (d->rc_polling_active)
1088 		schedule_delayed_work(&d->rc_query_work,
1089 				msecs_to_jiffies(d->rc.interval));
1090 
1091 	return ret;
1092 }
1093 
dvb_usbv2_resume(struct usb_interface * intf)1094 int dvb_usbv2_resume(struct usb_interface *intf)
1095 {
1096 	struct dvb_usb_device *d = usb_get_intfdata(intf);
1097 	dev_dbg(&d->udev->dev, "%s:\n", __func__);
1098 
1099 	return dvb_usbv2_resume_common(d);
1100 }
1101 EXPORT_SYMBOL(dvb_usbv2_resume);
1102 
dvb_usbv2_reset_resume(struct usb_interface * intf)1103 int dvb_usbv2_reset_resume(struct usb_interface *intf)
1104 {
1105 	struct dvb_usb_device *d = usb_get_intfdata(intf);
1106 	int ret;
1107 	dev_dbg(&d->udev->dev, "%s:\n", __func__);
1108 
1109 	dvb_usbv2_device_power_ctrl(d, 1);
1110 
1111 	if (d->props->init)
1112 		d->props->init(d);
1113 
1114 	ret = dvb_usbv2_resume_common(d);
1115 
1116 	dvb_usbv2_device_power_ctrl(d, 0);
1117 
1118 	return ret;
1119 }
1120 EXPORT_SYMBOL(dvb_usbv2_reset_resume);
1121 
1122 MODULE_VERSION("2.0");
1123 MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@posteo.de>");
1124 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1125 MODULE_DESCRIPTION("DVB USB common");
1126 MODULE_LICENSE("GPL");
1127