1 // SPDX-License-Identifier: GPL-2.0-or-later
9 #include "dvb_usb_common.h"
10 #include <media/media-device.h>
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,
19 MODULE_PARM_DESC(force_pid_filter_usage,
20 "force all DVB USB devices to use a PID filter, if any (default: 0)");
22 static int dvb_usbv2_download_firmware(struct dvb_usb_device *d,
26 const struct firmware *fw;
27 dev_dbg(&d->udev->dev, "%s:\n", __func__);
29 if (!d->props->download_firmware) {
34 ret = request_firmware(&fw, name, &d->udev->dev);
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);
42 dev_info(&d->udev->dev, "%s: downloading firmware from file '%s'\n",
43 KBUILD_MODNAME, name);
45 ret = d->props->download_firmware(d, fw);
52 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
56 static int dvb_usbv2_i2c_init(struct dvb_usb_device *d)
59 dev_dbg(&d->udev->dev, "%s:\n", __func__);
61 if (!d->props->i2c_algo)
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);
69 ret = i2c_add_adapter(&d->i2c_adap);
71 d->i2c_adap.algo = NULL;
77 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
81 static int dvb_usbv2_i2c_exit(struct dvb_usb_device *d)
83 dev_dbg(&d->udev->dev, "%s:\n", __func__);
86 i2c_del_adapter(&d->i2c_adap);
91 #if IS_ENABLED(CONFIG_RC_CORE)
92 static void dvb_usb_read_remote_control(struct work_struct *work)
94 struct dvb_usb_device *d = container_of(work,
95 struct dvb_usb_device, rc_query_work.work);
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.
102 if (dvb_usbv2_disable_rc_polling || d->rc.bulk_mode) {
103 d->rc_polling_active = false;
107 ret = d->rc.query(d);
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 */
115 schedule_delayed_work(&d->rc_query_work,
116 msecs_to_jiffies(d->rc.interval));
119 static int dvb_usbv2_remote_init(struct dvb_usb_device *d)
123 dev_dbg(&d->udev->dev, "%s:\n", __func__);
125 if (dvb_usbv2_disable_rc_polling || !d->props->get_rc_config)
128 d->rc.map_name = d->rc_map;
129 ret = d->props->get_rc_config(d, &d->rc);
133 /* disable rc when there is no keymap defined */
137 dev = rc_allocate_device(d->rc.driver_type);
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;
155 ret = rc_register_device(dev);
163 /* start polling if needed */
164 if (d->rc.query && !d->rc.bulk_mode) {
165 /* initialize a work queue for handling polling */
166 INIT_DELAYED_WORK(&d->rc_query_work,
167 dvb_usb_read_remote_control);
168 dev_info(&d->udev->dev,
169 "%s: schedule remote query interval to %d msecs\n",
170 KBUILD_MODNAME, d->rc.interval);
171 schedule_delayed_work(&d->rc_query_work,
172 msecs_to_jiffies(d->rc.interval));
173 d->rc_polling_active = true;
178 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
182 static int dvb_usbv2_remote_exit(struct dvb_usb_device *d)
184 dev_dbg(&d->udev->dev, "%s:\n", __func__);
187 cancel_delayed_work_sync(&d->rc_query_work);
188 rc_unregister_device(d->rc_dev);
195 #define dvb_usbv2_remote_init(args...) 0
196 #define dvb_usbv2_remote_exit(args...)
199 static void dvb_usb_data_complete(struct usb_data_stream *stream, u8 *buf,
202 struct dvb_usb_adapter *adap = stream->user_priv;
203 dvb_dmx_swfilter(&adap->demux, buf, len);
206 static void dvb_usb_data_complete_204(struct usb_data_stream *stream, u8 *buf,
209 struct dvb_usb_adapter *adap = stream->user_priv;
210 dvb_dmx_swfilter_204(&adap->demux, buf, len);
213 static void dvb_usb_data_complete_raw(struct usb_data_stream *stream, u8 *buf,
216 struct dvb_usb_adapter *adap = stream->user_priv;
217 dvb_dmx_swfilter_raw(&adap->demux, buf, len);
220 static int dvb_usbv2_adapter_stream_init(struct dvb_usb_adapter *adap)
222 dev_dbg(&adap_to_d(adap)->udev->dev, "%s: adap=%d\n", __func__,
225 adap->stream.udev = adap_to_d(adap)->udev;
226 adap->stream.user_priv = adap;
227 adap->stream.complete = dvb_usb_data_complete;
229 return usb_urb_initv2(&adap->stream, &adap->props->stream);
232 static int dvb_usbv2_adapter_stream_exit(struct dvb_usb_adapter *adap)
234 dev_dbg(&adap_to_d(adap)->udev->dev, "%s: adap=%d\n", __func__,
237 return usb_urb_exitv2(&adap->stream);
240 static int dvb_usb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
242 struct dvb_usb_adapter *adap = dvbdmxfeed->demux->priv;
243 struct dvb_usb_device *d = adap_to_d(adap);
245 struct usb_data_stream_properties stream_props;
246 dev_dbg(&d->udev->dev,
247 "%s: adap=%d active_fe=%d feed_type=%d setting pid [%s]: %04x (%04d) at index %d\n",
248 __func__, adap->id, adap->active_fe, dvbdmxfeed->type,
249 adap->pid_filtering ? "yes" : "no", dvbdmxfeed->pid,
250 dvbdmxfeed->pid, dvbdmxfeed->index);
252 /* wait init is done */
253 wait_on_bit(&adap->state_bits, ADAP_INIT, TASK_UNINTERRUPTIBLE);
255 if (adap->active_fe == -1)
258 /* skip feed setup if we are already feeding */
259 if (adap->feed_count++ > 0)
260 goto skip_feed_start;
262 /* set 'streaming' status bit */
263 set_bit(ADAP_STREAMING, &adap->state_bits);
265 /* resolve input and output streaming parameters */
266 if (d->props->get_stream_config) {
267 memcpy(&stream_props, &adap->props->stream,
268 sizeof(struct usb_data_stream_properties));
269 ret = d->props->get_stream_config(adap->fe[adap->active_fe],
270 &adap->ts_type, &stream_props);
272 dev_err(&d->udev->dev,
273 "%s: get_stream_config() failed=%d\n",
274 KBUILD_MODNAME, ret);
276 stream_props = adap->props->stream;
279 switch (adap->ts_type) {
280 case DVB_USB_FE_TS_TYPE_204:
281 adap->stream.complete = dvb_usb_data_complete_204;
283 case DVB_USB_FE_TS_TYPE_RAW:
284 adap->stream.complete = dvb_usb_data_complete_raw;
286 case DVB_USB_FE_TS_TYPE_188:
288 adap->stream.complete = dvb_usb_data_complete;
292 /* submit USB streaming packets */
293 usb_urb_submitv2(&adap->stream, &stream_props);
295 /* enable HW PID filter */
296 if (adap->pid_filtering && adap->props->pid_filter_ctrl) {
297 ret = adap->props->pid_filter_ctrl(adap, 1);
299 dev_err(&d->udev->dev,
300 "%s: pid_filter_ctrl() failed=%d\n",
301 KBUILD_MODNAME, ret);
304 /* ask device to start streaming */
305 if (d->props->streaming_ctrl) {
306 ret = d->props->streaming_ctrl(adap->fe[adap->active_fe], 1);
308 dev_err(&d->udev->dev,
309 "%s: streaming_ctrl() failed=%d\n",
310 KBUILD_MODNAME, ret);
314 /* add PID to device HW PID filter */
315 if (adap->pid_filtering && adap->props->pid_filter) {
316 ret = adap->props->pid_filter(adap, dvbdmxfeed->index,
319 dev_err(&d->udev->dev, "%s: pid_filter() failed=%d\n",
320 KBUILD_MODNAME, ret);
324 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
328 static int dvb_usb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
330 struct dvb_usb_adapter *adap = dvbdmxfeed->demux->priv;
331 struct dvb_usb_device *d = adap_to_d(adap);
333 dev_dbg(&d->udev->dev,
334 "%s: adap=%d active_fe=%d feed_type=%d setting pid [%s]: %04x (%04d) at index %d\n",
335 __func__, adap->id, adap->active_fe, dvbdmxfeed->type,
336 adap->pid_filtering ? "yes" : "no", dvbdmxfeed->pid,
337 dvbdmxfeed->pid, dvbdmxfeed->index);
339 if (adap->active_fe == -1)
342 /* remove PID from device HW PID filter */
343 if (adap->pid_filtering && adap->props->pid_filter) {
344 ret = adap->props->pid_filter(adap, dvbdmxfeed->index,
347 dev_err(&d->udev->dev, "%s: pid_filter() failed=%d\n",
348 KBUILD_MODNAME, ret);
351 /* we cannot stop streaming until last PID is removed */
352 if (--adap->feed_count > 0)
355 /* ask device to stop streaming */
356 if (d->props->streaming_ctrl) {
357 ret = d->props->streaming_ctrl(adap->fe[adap->active_fe], 0);
359 dev_err(&d->udev->dev,
360 "%s: streaming_ctrl() failed=%d\n",
361 KBUILD_MODNAME, ret);
364 /* disable HW PID filter */
365 if (adap->pid_filtering && adap->props->pid_filter_ctrl) {
366 ret = adap->props->pid_filter_ctrl(adap, 0);
368 dev_err(&d->udev->dev,
369 "%s: pid_filter_ctrl() failed=%d\n",
370 KBUILD_MODNAME, ret);
373 /* kill USB streaming packets */
374 usb_urb_killv2(&adap->stream);
376 /* clear 'streaming' status bit */
377 clear_bit(ADAP_STREAMING, &adap->state_bits);
378 smp_mb__after_atomic();
379 wake_up_bit(&adap->state_bits, ADAP_STREAMING);
383 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
387 static int dvb_usbv2_media_device_init(struct dvb_usb_adapter *adap)
389 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
390 struct media_device *mdev;
391 struct dvb_usb_device *d = adap_to_d(adap);
392 struct usb_device *udev = d->udev;
394 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
398 media_device_usb_init(mdev, udev, d->name);
400 dvb_register_media_controller(&adap->dvb_adap, mdev);
402 dev_info(&d->udev->dev, "media controller created\n");
407 static int dvb_usbv2_media_device_register(struct dvb_usb_adapter *adap)
409 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
410 return media_device_register(adap->dvb_adap.mdev);
416 static void dvb_usbv2_media_device_unregister(struct dvb_usb_adapter *adap)
418 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
420 if (!adap->dvb_adap.mdev)
423 media_device_unregister(adap->dvb_adap.mdev);
424 media_device_cleanup(adap->dvb_adap.mdev);
425 kfree(adap->dvb_adap.mdev);
426 adap->dvb_adap.mdev = NULL;
431 static int dvb_usbv2_adapter_dvb_init(struct dvb_usb_adapter *adap)
434 struct dvb_usb_device *d = adap_to_d(adap);
436 dev_dbg(&d->udev->dev, "%s: adap=%d\n", __func__, adap->id);
438 ret = dvb_register_adapter(&adap->dvb_adap, d->name, d->props->owner,
439 &d->udev->dev, d->props->adapter_nr);
441 dev_dbg(&d->udev->dev, "%s: dvb_register_adapter() failed=%d\n",
443 goto err_dvb_register_adapter;
446 adap->dvb_adap.priv = adap;
448 ret = dvb_usbv2_media_device_init(adap);
450 dev_dbg(&d->udev->dev, "%s: dvb_usbv2_media_device_init() failed=%d\n",
452 goto err_dvb_register_mc;
455 if (d->props->read_mac_address) {
456 ret = d->props->read_mac_address(adap,
457 adap->dvb_adap.proposed_mac);
459 goto err_dvb_dmx_init;
461 dev_info(&d->udev->dev, "%s: MAC address: %pM\n",
462 KBUILD_MODNAME, adap->dvb_adap.proposed_mac);
465 adap->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING;
466 adap->demux.priv = adap;
467 adap->demux.filternum = 0;
468 adap->demux.filternum = adap->max_feed_count;
469 adap->demux.feednum = adap->demux.filternum;
470 adap->demux.start_feed = dvb_usb_start_feed;
471 adap->demux.stop_feed = dvb_usb_stop_feed;
472 adap->demux.write_to_decoder = NULL;
473 ret = dvb_dmx_init(&adap->demux);
475 dev_err(&d->udev->dev, "%s: dvb_dmx_init() failed=%d\n",
476 KBUILD_MODNAME, ret);
477 goto err_dvb_dmx_init;
480 adap->dmxdev.filternum = adap->demux.filternum;
481 adap->dmxdev.demux = &adap->demux.dmx;
482 adap->dmxdev.capabilities = 0;
483 ret = dvb_dmxdev_init(&adap->dmxdev, &adap->dvb_adap);
485 dev_err(&d->udev->dev, "%s: dvb_dmxdev_init() failed=%d\n",
486 KBUILD_MODNAME, ret);
487 goto err_dvb_dmxdev_init;
490 ret = dvb_net_init(&adap->dvb_adap, &adap->dvb_net, &adap->demux.dmx);
492 dev_err(&d->udev->dev, "%s: dvb_net_init() failed=%d\n",
493 KBUILD_MODNAME, ret);
494 goto err_dvb_net_init;
499 dvb_dmxdev_release(&adap->dmxdev);
501 dvb_dmx_release(&adap->demux);
503 dvb_usbv2_media_device_unregister(adap);
505 dvb_unregister_adapter(&adap->dvb_adap);
506 err_dvb_register_adapter:
507 adap->dvb_adap.priv = NULL;
511 static int dvb_usbv2_adapter_dvb_exit(struct dvb_usb_adapter *adap)
513 dev_dbg(&adap_to_d(adap)->udev->dev, "%s: adap=%d\n", __func__,
516 if (adap->dvb_adap.priv) {
517 dvb_net_release(&adap->dvb_net);
518 adap->demux.dmx.close(&adap->demux.dmx);
519 dvb_dmxdev_release(&adap->dmxdev);
520 dvb_dmx_release(&adap->demux);
521 dvb_unregister_adapter(&adap->dvb_adap);
527 static int dvb_usbv2_device_power_ctrl(struct dvb_usb_device *d, int onoff)
536 if (d->powered == 0 || (onoff && d->powered == 1)) {
537 /* when switching from 1 to 0 or from 0 to 1 */
538 dev_dbg(&d->udev->dev, "%s: power=%d\n", __func__, onoff);
539 if (d->props->power_ctrl) {
540 ret = d->props->power_ctrl(d, onoff);
548 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
552 static int dvb_usb_fe_init(struct dvb_frontend *fe)
555 struct dvb_usb_adapter *adap = fe->dvb->priv;
556 struct dvb_usb_device *d = adap_to_d(adap);
557 dev_dbg(&d->udev->dev, "%s: adap=%d fe=%d\n", __func__, adap->id,
560 if (!adap->suspend_resume_active) {
561 adap->active_fe = fe->id;
562 set_bit(ADAP_INIT, &adap->state_bits);
565 ret = dvb_usbv2_device_power_ctrl(d, 1);
569 if (d->props->frontend_ctrl) {
570 ret = d->props->frontend_ctrl(fe, 1);
575 if (adap->fe_init[fe->id]) {
576 ret = adap->fe_init[fe->id](fe);
581 if (!adap->suspend_resume_active) {
582 clear_bit(ADAP_INIT, &adap->state_bits);
583 smp_mb__after_atomic();
584 wake_up_bit(&adap->state_bits, ADAP_INIT);
587 dev_dbg(&d->udev->dev, "%s: ret=%d\n", __func__, ret);
591 static int dvb_usb_fe_sleep(struct dvb_frontend *fe)
594 struct dvb_usb_adapter *adap = fe->dvb->priv;
595 struct dvb_usb_device *d = adap_to_d(adap);
596 dev_dbg(&d->udev->dev, "%s: adap=%d fe=%d\n", __func__, adap->id,
599 if (!adap->suspend_resume_active) {
600 set_bit(ADAP_SLEEP, &adap->state_bits);
601 wait_on_bit(&adap->state_bits, ADAP_STREAMING,
602 TASK_UNINTERRUPTIBLE);
605 if (adap->fe_sleep[fe->id]) {
606 ret = adap->fe_sleep[fe->id](fe);
611 if (d->props->frontend_ctrl) {
612 ret = d->props->frontend_ctrl(fe, 0);
617 ret = dvb_usbv2_device_power_ctrl(d, 0);
620 if (!adap->suspend_resume_active) {
621 adap->active_fe = -1;
622 clear_bit(ADAP_SLEEP, &adap->state_bits);
623 smp_mb__after_atomic();
624 wake_up_bit(&adap->state_bits, ADAP_SLEEP);
627 dev_dbg(&d->udev->dev, "%s: ret=%d\n", __func__, ret);
631 static int dvb_usbv2_adapter_frontend_init(struct dvb_usb_adapter *adap)
633 int ret, i, count_registered = 0;
634 struct dvb_usb_device *d = adap_to_d(adap);
635 dev_dbg(&d->udev->dev, "%s: adap=%d\n", __func__, adap->id);
637 memset(adap->fe, 0, sizeof(adap->fe));
638 adap->active_fe = -1;
640 if (d->props->frontend_attach) {
641 ret = d->props->frontend_attach(adap);
643 dev_dbg(&d->udev->dev,
644 "%s: frontend_attach() failed=%d\n",
646 goto err_dvb_frontend_detach;
649 dev_dbg(&d->udev->dev, "%s: frontend_attach() do not exists\n",
655 for (i = 0; i < MAX_NO_OF_FE_PER_ADAP && adap->fe[i]; i++) {
657 /* re-assign sleep and wakeup functions */
658 adap->fe_init[i] = adap->fe[i]->ops.init;
659 adap->fe[i]->ops.init = dvb_usb_fe_init;
660 adap->fe_sleep[i] = adap->fe[i]->ops.sleep;
661 adap->fe[i]->ops.sleep = dvb_usb_fe_sleep;
663 ret = dvb_register_frontend(&adap->dvb_adap, adap->fe[i]);
665 dev_err(&d->udev->dev,
666 "%s: frontend%d registration failed\n",
668 goto err_dvb_unregister_frontend;
674 if (d->props->tuner_attach) {
675 ret = d->props->tuner_attach(adap);
677 dev_dbg(&d->udev->dev, "%s: tuner_attach() failed=%d\n",
679 goto err_dvb_unregister_frontend;
683 ret = dvb_create_media_graph(&adap->dvb_adap, true);
685 goto err_dvb_unregister_frontend;
687 ret = dvb_usbv2_media_device_register(adap);
691 err_dvb_unregister_frontend:
692 for (i = count_registered - 1; i >= 0; i--)
693 dvb_unregister_frontend(adap->fe[i]);
695 err_dvb_frontend_detach:
696 for (i = MAX_NO_OF_FE_PER_ADAP - 1; i >= 0; i--) {
698 dvb_frontend_detach(adap->fe[i]);
704 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
708 static int dvb_usbv2_adapter_frontend_exit(struct dvb_usb_adapter *adap)
711 struct dvb_usb_device *d = adap_to_d(adap);
713 dev_dbg(&d->udev->dev, "%s: adap=%d\n", __func__, adap->id);
715 for (i = MAX_NO_OF_FE_PER_ADAP - 1; i >= 0; i--) {
717 dvb_unregister_frontend(adap->fe[i]);
718 dvb_frontend_detach(adap->fe[i]);
722 if (d->props->tuner_detach) {
723 ret = d->props->tuner_detach(adap);
725 dev_dbg(&d->udev->dev, "%s: tuner_detach() failed=%d\n",
730 if (d->props->frontend_detach) {
731 ret = d->props->frontend_detach(adap);
733 dev_dbg(&d->udev->dev,
734 "%s: frontend_detach() failed=%d\n",
742 static int dvb_usbv2_adapter_init(struct dvb_usb_device *d)
744 struct dvb_usb_adapter *adap;
745 int ret, i, adapter_count;
747 /* resolve adapter count */
748 adapter_count = d->props->num_adapters;
749 if (d->props->get_adapter_count) {
750 ret = d->props->get_adapter_count(d);
757 for (i = 0; i < adapter_count; i++) {
758 adap = &d->adapter[i];
760 adap->props = &d->props->adapter[i];
762 /* speed - when running at FULL speed we need a HW PID filter */
763 if (d->udev->speed == USB_SPEED_FULL &&
764 !(adap->props->caps & DVB_USB_ADAP_HAS_PID_FILTER)) {
765 dev_err(&d->udev->dev,
766 "%s: this USB2.0 device cannot be run on a USB1.1 port (it lacks a hardware PID filter)\n",
770 } else if ((d->udev->speed == USB_SPEED_FULL &&
771 adap->props->caps & DVB_USB_ADAP_HAS_PID_FILTER) ||
772 (adap->props->caps & DVB_USB_ADAP_NEED_PID_FILTERING)) {
773 dev_info(&d->udev->dev,
774 "%s: will use the device's hardware PID filter (table count: %d)\n",
776 adap->props->pid_filter_count);
777 adap->pid_filtering = 1;
778 adap->max_feed_count = adap->props->pid_filter_count;
780 dev_info(&d->udev->dev,
781 "%s: will pass the complete MPEG2 transport stream to the software demuxer\n",
783 adap->pid_filtering = 0;
784 adap->max_feed_count = 255;
787 if (!adap->pid_filtering && dvb_usb_force_pid_filter_usage &&
788 adap->props->caps & DVB_USB_ADAP_HAS_PID_FILTER) {
789 dev_info(&d->udev->dev,
790 "%s: PID filter enabled by module option\n",
792 adap->pid_filtering = 1;
793 adap->max_feed_count = adap->props->pid_filter_count;
796 ret = dvb_usbv2_adapter_stream_init(adap);
800 ret = dvb_usbv2_adapter_dvb_init(adap);
804 ret = dvb_usbv2_adapter_frontend_init(adap);
808 /* use exclusive FE lock if there is multiple shared FEs */
810 adap->dvb_adap.mfe_shared = 1;
815 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
819 static int dvb_usbv2_adapter_exit(struct dvb_usb_device *d)
822 dev_dbg(&d->udev->dev, "%s:\n", __func__);
824 for (i = MAX_NO_OF_ADAPTER_PER_DEVICE - 1; i >= 0; i--) {
825 if (d->adapter[i].props) {
826 dvb_usbv2_adapter_dvb_exit(&d->adapter[i]);
827 dvb_usbv2_adapter_stream_exit(&d->adapter[i]);
828 dvb_usbv2_adapter_frontend_exit(&d->adapter[i]);
829 dvb_usbv2_media_device_unregister(&d->adapter[i]);
836 /* general initialization functions */
837 static int dvb_usbv2_exit(struct dvb_usb_device *d)
839 dev_dbg(&d->udev->dev, "%s:\n", __func__);
841 dvb_usbv2_remote_exit(d);
842 dvb_usbv2_adapter_exit(d);
843 dvb_usbv2_i2c_exit(d);
848 static int dvb_usbv2_init(struct dvb_usb_device *d)
851 dev_dbg(&d->udev->dev, "%s:\n", __func__);
853 dvb_usbv2_device_power_ctrl(d, 1);
855 if (d->props->read_config) {
856 ret = d->props->read_config(d);
861 ret = dvb_usbv2_i2c_init(d);
865 ret = dvb_usbv2_adapter_init(d);
869 if (d->props->init) {
870 ret = d->props->init(d);
875 ret = dvb_usbv2_remote_init(d);
879 dvb_usbv2_device_power_ctrl(d, 0);
883 dvb_usbv2_device_power_ctrl(d, 0);
884 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
888 int dvb_usbv2_probe(struct usb_interface *intf,
889 const struct usb_device_id *id)
892 struct dvb_usb_device *d;
893 struct usb_device *udev = interface_to_usbdev(intf);
894 struct dvb_usb_driver_info *driver_info =
895 (struct dvb_usb_driver_info *) id->driver_info;
897 dev_dbg(&udev->dev, "%s: bInterfaceNumber=%d\n", __func__,
898 intf->cur_altsetting->desc.bInterfaceNumber);
900 if (!id->driver_info) {
901 dev_err(&udev->dev, "%s: driver_info failed\n", KBUILD_MODNAME);
906 d = kzalloc(sizeof(struct dvb_usb_device), GFP_KERNEL);
908 dev_err(&udev->dev, "%s: kzalloc() failed\n", KBUILD_MODNAME);
914 d->name = driver_info->name;
915 d->rc_map = driver_info->rc_map;
917 d->props = driver_info->props;
919 if (intf->cur_altsetting->desc.bInterfaceNumber !=
920 d->props->bInterfaceNumber) {
925 mutex_init(&d->usb_mutex);
926 mutex_init(&d->i2c_mutex);
928 if (d->props->size_of_priv) {
929 d->priv = kzalloc(d->props->size_of_priv, GFP_KERNEL);
931 dev_err(&d->udev->dev, "%s: kzalloc() failed\n",
938 if (d->props->probe) {
939 ret = d->props->probe(d);
944 if (d->props->identify_state) {
945 const char *name = NULL;
946 ret = d->props->identify_state(d, &name);
948 dev_info(&d->udev->dev,
949 "%s: found a '%s' in cold state\n",
950 KBUILD_MODNAME, d->name);
953 name = d->props->firmware;
955 ret = dvb_usbv2_download_firmware(d, name);
957 /* device is warm, continue initialization */
959 } else if (ret == RECONNECTS_USB) {
961 * USB core will call disconnect() and then
962 * probe() as device reconnects itself from the
963 * USB bus. disconnect() will release all driver
964 * resources and probe() is called for 'new'
965 * device. As 'new' device is warm we should
966 * never go here again.
972 } else if (ret != WARM) {
977 dev_info(&d->udev->dev, "%s: found a '%s' in warm state\n",
978 KBUILD_MODNAME, d->name);
980 ret = dvb_usbv2_init(d);
984 dev_info(&d->udev->dev,
985 "%s: '%s' successfully initialized and connected\n",
986 KBUILD_MODNAME, d->name);
988 usb_set_intfdata(intf, d);
993 if (d->props->disconnect)
994 d->props->disconnect(d);
1000 dev_dbg(&udev->dev, "%s: failed=%d\n", __func__, ret);
1003 EXPORT_SYMBOL(dvb_usbv2_probe);
1005 void dvb_usbv2_disconnect(struct usb_interface *intf)
1007 struct dvb_usb_device *d = usb_get_intfdata(intf);
1008 const char *devname = kstrdup(dev_name(&d->udev->dev), GFP_KERNEL);
1009 const char *drvname = d->name;
1011 dev_dbg(&d->udev->dev, "%s: bInterfaceNumber=%d\n", __func__,
1012 intf->cur_altsetting->desc.bInterfaceNumber);
1019 if (d->props->disconnect)
1020 d->props->disconnect(d);
1025 pr_info("%s: '%s:%s' successfully deinitialized and disconnected\n",
1026 KBUILD_MODNAME, drvname, devname);
1029 EXPORT_SYMBOL(dvb_usbv2_disconnect);
1031 int dvb_usbv2_suspend(struct usb_interface *intf, pm_message_t msg)
1033 struct dvb_usb_device *d = usb_get_intfdata(intf);
1034 int ret = 0, i, active_fe;
1035 struct dvb_frontend *fe;
1036 dev_dbg(&d->udev->dev, "%s:\n", __func__);
1038 /* stop remote controller poll */
1039 if (d->rc_polling_active)
1040 cancel_delayed_work_sync(&d->rc_query_work);
1042 for (i = MAX_NO_OF_ADAPTER_PER_DEVICE - 1; i >= 0; i--) {
1043 active_fe = d->adapter[i].active_fe;
1044 if (d->adapter[i].dvb_adap.priv && active_fe != -1) {
1045 fe = d->adapter[i].fe[active_fe];
1046 d->adapter[i].suspend_resume_active = true;
1048 if (d->props->streaming_ctrl)
1049 d->props->streaming_ctrl(fe, 0);
1051 /* stop usb streaming */
1052 usb_urb_killv2(&d->adapter[i].stream);
1054 ret = dvb_frontend_suspend(fe);
1060 EXPORT_SYMBOL(dvb_usbv2_suspend);
1062 static int dvb_usbv2_resume_common(struct dvb_usb_device *d)
1064 int ret = 0, i, active_fe;
1065 struct dvb_frontend *fe;
1066 dev_dbg(&d->udev->dev, "%s:\n", __func__);
1068 for (i = 0; i < MAX_NO_OF_ADAPTER_PER_DEVICE; i++) {
1069 active_fe = d->adapter[i].active_fe;
1070 if (d->adapter[i].dvb_adap.priv && active_fe != -1) {
1071 fe = d->adapter[i].fe[active_fe];
1073 ret = dvb_frontend_resume(fe);
1075 /* resume usb streaming */
1076 usb_urb_submitv2(&d->adapter[i].stream, NULL);
1078 if (d->props->streaming_ctrl)
1079 d->props->streaming_ctrl(fe, 1);
1081 d->adapter[i].suspend_resume_active = false;
1085 /* start remote controller poll */
1086 if (d->rc_polling_active)
1087 schedule_delayed_work(&d->rc_query_work,
1088 msecs_to_jiffies(d->rc.interval));
1093 int dvb_usbv2_resume(struct usb_interface *intf)
1095 struct dvb_usb_device *d = usb_get_intfdata(intf);
1096 dev_dbg(&d->udev->dev, "%s:\n", __func__);
1098 return dvb_usbv2_resume_common(d);
1100 EXPORT_SYMBOL(dvb_usbv2_resume);
1102 int dvb_usbv2_reset_resume(struct usb_interface *intf)
1104 struct dvb_usb_device *d = usb_get_intfdata(intf);
1106 dev_dbg(&d->udev->dev, "%s:\n", __func__);
1108 dvb_usbv2_device_power_ctrl(d, 1);
1113 ret = dvb_usbv2_resume_common(d);
1115 dvb_usbv2_device_power_ctrl(d, 0);
1119 EXPORT_SYMBOL(dvb_usbv2_reset_resume);
1121 MODULE_VERSION("2.0");
1124 MODULE_DESCRIPTION("DVB USB common");
1125 MODULE_LICENSE("GPL");