4 * Copyright (C) 2004-6 Patrick Boettcher <patrick.boettcher@desy.de>
5 * Copyright (C) 2012 Antti Palosaari <crope@iki.fi>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "dvb_usb_common.h"
24 int dvb_usbv2_disable_rc_polling;
25 module_param_named(disable_rc_polling, dvb_usbv2_disable_rc_polling, int, 0644);
26 MODULE_PARM_DESC(disable_rc_polling,
27 "disable remote control polling (default: 0)");
28 static int dvb_usb_force_pid_filter_usage;
29 module_param_named(force_pid_filter_usage, dvb_usb_force_pid_filter_usage,
31 MODULE_PARM_DESC(force_pid_filter_usage, "force all DVB USB devices to use a " \
32 "PID filter, if any (default: 0)");
34 static int dvb_usbv2_download_firmware(struct dvb_usb_device *d, const char *name)
37 const struct firmware *fw;
38 dev_dbg(&d->udev->dev, "%s:\n", __func__);
40 if (!d->props->download_firmware) {
45 ret = request_firmware(&fw, name, &d->udev->dev);
47 dev_err(&d->udev->dev, "%s: Did not find the firmware file "\
48 "'%s'. Please see linux/Documentation/dvb/ " \
49 "for more details on firmware-problems. " \
50 "Status %d\n", KBUILD_MODNAME, name, ret);
54 dev_info(&d->udev->dev, "%s: downloading firmware from file '%s'\n",
55 KBUILD_MODNAME, name);
57 ret = d->props->download_firmware(d, fw);
64 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
68 static int dvb_usbv2_i2c_init(struct dvb_usb_device *d)
71 dev_dbg(&d->udev->dev, "%s:\n", __func__);
73 if (!d->props->i2c_algo)
76 strlcpy(d->i2c_adap.name, d->name, sizeof(d->i2c_adap.name));
77 d->i2c_adap.algo = d->props->i2c_algo;
78 d->i2c_adap.dev.parent = &d->udev->dev;
79 i2c_set_adapdata(&d->i2c_adap, d);
81 ret = i2c_add_adapter(&d->i2c_adap);
83 d->i2c_adap.algo = NULL;
84 dev_err(&d->udev->dev, "%s: i2c_add_adapter() failed=%d\n",
91 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
95 static int dvb_usbv2_i2c_exit(struct dvb_usb_device *d)
97 dev_dbg(&d->udev->dev, "%s:\n", __func__);
100 i2c_del_adapter(&d->i2c_adap);
105 static void dvb_usb_read_remote_control(struct work_struct *work)
107 struct dvb_usb_device *d = container_of(work,
108 struct dvb_usb_device, rc_query_work.work);
112 * When the parameter has been set to 1 via sysfs while the
113 * driver was running, or when bulk mode is enabled after IR init.
115 if (dvb_usbv2_disable_rc_polling || d->rc.bulk_mode)
118 ret = d->rc.query(d);
120 dev_err(&d->udev->dev, "%s: rc.query() failed=%d\n",
121 KBUILD_MODNAME, ret);
122 return; /* stop polling */
125 schedule_delayed_work(&d->rc_query_work,
126 msecs_to_jiffies(d->rc.interval));
129 static int dvb_usbv2_remote_init(struct dvb_usb_device *d)
133 dev_dbg(&d->udev->dev, "%s:\n", __func__);
135 if (dvb_usbv2_disable_rc_polling || !d->props->get_rc_config)
138 d->rc.map_name = d->rc_map;
139 ret = d->props->get_rc_config(d, &d->rc);
143 /* disable rc when there is no keymap defined */
147 dev = rc_allocate_device();
153 dev->dev.parent = &d->udev->dev;
154 dev->input_name = d->name;
155 usb_make_path(d->udev, d->rc_phys, sizeof(d->rc_phys));
156 strlcat(d->rc_phys, "/ir0", sizeof(d->rc_phys));
157 dev->input_phys = d->rc_phys;
158 usb_to_input_id(d->udev, &dev->input_id);
159 /* TODO: likely RC-core should took const char * */
160 dev->driver_name = (char *) d->props->driver_name;
161 dev->map_name = d->rc.map_name;
162 dev->driver_type = d->rc.driver_type;
163 dev->allowed_protos = d->rc.allowed_protos;
164 dev->change_protocol = d->rc.change_protocol;
167 ret = rc_register_device(dev);
175 /* start polling if needed */
176 if (d->rc.query && !d->rc.bulk_mode) {
177 /* initialize a work queue for handling polling */
178 INIT_DELAYED_WORK(&d->rc_query_work,
179 dvb_usb_read_remote_control);
180 dev_info(&d->udev->dev, "%s: schedule remote query interval " \
181 "to %d msecs\n", KBUILD_MODNAME,
183 schedule_delayed_work(&d->rc_query_work,
184 msecs_to_jiffies(d->rc.interval));
189 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
193 static int dvb_usbv2_remote_exit(struct dvb_usb_device *d)
195 dev_dbg(&d->udev->dev, "%s:\n", __func__);
198 cancel_delayed_work_sync(&d->rc_query_work);
199 rc_unregister_device(d->rc_dev);
206 static void dvb_usb_data_complete(struct usb_data_stream *stream, u8 *buf,
209 struct dvb_usb_adapter *adap = stream->user_priv;
210 dvb_dmx_swfilter(&adap->demux, buf, len);
213 static void dvb_usb_data_complete_204(struct usb_data_stream *stream, u8 *buf,
216 struct dvb_usb_adapter *adap = stream->user_priv;
217 dvb_dmx_swfilter_204(&adap->demux, buf, len);
220 static void dvb_usb_data_complete_raw(struct usb_data_stream *stream, u8 *buf,
223 struct dvb_usb_adapter *adap = stream->user_priv;
224 dvb_dmx_swfilter_raw(&adap->demux, buf, len);
227 static int dvb_usbv2_adapter_stream_init(struct dvb_usb_adapter *adap)
229 dev_dbg(&adap_to_d(adap)->udev->dev, "%s: adap=%d\n", __func__,
232 adap->stream.udev = adap_to_d(adap)->udev;
233 adap->stream.user_priv = adap;
234 adap->stream.complete = dvb_usb_data_complete;
236 return usb_urb_initv2(&adap->stream, &adap->props->stream);
239 static int dvb_usbv2_adapter_stream_exit(struct dvb_usb_adapter *adap)
241 dev_dbg(&adap_to_d(adap)->udev->dev, "%s: adap=%d\n", __func__,
244 return usb_urb_exitv2(&adap->stream);
247 static inline int dvb_usb_ctrl_feed(struct dvb_demux_feed *dvbdmxfeed,
250 struct dvb_usb_adapter *adap = dvbdmxfeed->demux->priv;
251 struct dvb_usb_device *d = adap_to_d(adap);
253 dev_dbg(&d->udev->dev, "%s: adap=%d active_fe=%d feed_type=%d " \
254 "setting pid [%s]: %04x (%04d) at index %d '%s'\n",
255 __func__, adap->id, adap->active_fe, dvbdmxfeed->type,
256 adap->pid_filtering ? "yes" : "no", dvbdmxfeed->pid,
257 dvbdmxfeed->pid, dvbdmxfeed->index,
258 (count == 1) ? "on" : "off");
260 if (adap->active_fe == -1)
263 adap->feed_count += count;
265 /* stop feeding if it is last pid */
266 if (adap->feed_count == 0) {
267 dev_dbg(&d->udev->dev, "%s: stop feeding\n", __func__);
269 if (d->props->streaming_ctrl) {
270 ret = d->props->streaming_ctrl(
271 adap->fe[adap->active_fe], 0);
273 dev_err(&d->udev->dev, "%s: streaming_ctrl() " \
274 "failed=%d\n", KBUILD_MODNAME,
276 usb_urb_killv2(&adap->stream);
277 goto err_mutex_unlock;
280 usb_urb_killv2(&adap->stream);
281 mutex_unlock(&adap->sync_mutex);
284 /* activate the pid on the device pid filter */
285 if (adap->props->caps & DVB_USB_ADAP_HAS_PID_FILTER &&
286 adap->pid_filtering && adap->props->pid_filter) {
287 ret = adap->props->pid_filter(adap, dvbdmxfeed->index,
288 dvbdmxfeed->pid, (count == 1) ? 1 : 0);
290 dev_err(&d->udev->dev, "%s: pid_filter() failed=%d\n",
291 KBUILD_MODNAME, ret);
294 /* start feeding if it is first pid */
295 if (adap->feed_count == 1 && count == 1) {
296 struct usb_data_stream_properties stream_props;
297 mutex_lock(&adap->sync_mutex);
298 dev_dbg(&d->udev->dev, "%s: start feeding\n", __func__);
300 /* resolve input and output streaming paramters */
301 if (d->props->get_stream_config) {
302 memcpy(&stream_props, &adap->props->stream,
303 sizeof(struct usb_data_stream_properties));
304 ret = d->props->get_stream_config(
305 adap->fe[adap->active_fe],
306 &adap->ts_type, &stream_props);
308 goto err_mutex_unlock;
310 stream_props = adap->props->stream;
313 switch (adap->ts_type) {
314 case DVB_USB_FE_TS_TYPE_204:
315 adap->stream.complete = dvb_usb_data_complete_204;
317 case DVB_USB_FE_TS_TYPE_RAW:
318 adap->stream.complete = dvb_usb_data_complete_raw;
320 case DVB_USB_FE_TS_TYPE_188:
322 adap->stream.complete = dvb_usb_data_complete;
326 usb_urb_submitv2(&adap->stream, &stream_props);
328 if (adap->props->caps & DVB_USB_ADAP_HAS_PID_FILTER &&
330 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF &&
331 adap->props->pid_filter_ctrl) {
332 ret = adap->props->pid_filter_ctrl(adap,
333 adap->pid_filtering);
335 dev_err(&d->udev->dev, "%s: " \
336 "pid_filter_ctrl() failed=%d\n",
337 KBUILD_MODNAME, ret);
338 goto err_mutex_unlock;
342 if (d->props->streaming_ctrl) {
343 ret = d->props->streaming_ctrl(
344 adap->fe[adap->active_fe], 1);
346 dev_err(&d->udev->dev, "%s: streaming_ctrl() " \
347 "failed=%d\n", KBUILD_MODNAME,
349 goto err_mutex_unlock;
356 mutex_unlock(&adap->sync_mutex);
357 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
361 static int dvb_usb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
363 return dvb_usb_ctrl_feed(dvbdmxfeed, 1);
366 static int dvb_usb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
368 return dvb_usb_ctrl_feed(dvbdmxfeed, -1);
371 static int dvb_usbv2_adapter_dvb_init(struct dvb_usb_adapter *adap)
374 struct dvb_usb_device *d = adap_to_d(adap);
375 dev_dbg(&d->udev->dev, "%s: adap=%d\n", __func__, adap->id);
377 ret = dvb_register_adapter(&adap->dvb_adap, d->name, d->props->owner,
378 &d->udev->dev, d->props->adapter_nr);
380 dev_dbg(&d->udev->dev, "%s: dvb_register_adapter() failed=%d\n",
382 goto err_dvb_register_adapter;
385 adap->dvb_adap.priv = adap;
387 if (d->props->read_mac_address) {
388 ret = d->props->read_mac_address(adap,
389 adap->dvb_adap.proposed_mac);
391 goto err_dvb_dmx_init;
393 dev_info(&d->udev->dev, "%s: MAC address: %pM\n",
394 KBUILD_MODNAME, adap->dvb_adap.proposed_mac);
397 adap->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING;
398 adap->demux.priv = adap;
399 adap->demux.filternum = 0;
400 adap->demux.filternum = adap->max_feed_count;
401 adap->demux.feednum = adap->demux.filternum;
402 adap->demux.start_feed = dvb_usb_start_feed;
403 adap->demux.stop_feed = dvb_usb_stop_feed;
404 adap->demux.write_to_decoder = NULL;
405 ret = dvb_dmx_init(&adap->demux);
407 dev_err(&d->udev->dev, "%s: dvb_dmx_init() failed=%d\n",
408 KBUILD_MODNAME, ret);
409 goto err_dvb_dmx_init;
412 adap->dmxdev.filternum = adap->demux.filternum;
413 adap->dmxdev.demux = &adap->demux.dmx;
414 adap->dmxdev.capabilities = 0;
415 ret = dvb_dmxdev_init(&adap->dmxdev, &adap->dvb_adap);
417 dev_err(&d->udev->dev, "%s: dvb_dmxdev_init() failed=%d\n",
418 KBUILD_MODNAME, ret);
419 goto err_dvb_dmxdev_init;
422 ret = dvb_net_init(&adap->dvb_adap, &adap->dvb_net, &adap->demux.dmx);
424 dev_err(&d->udev->dev, "%s: dvb_net_init() failed=%d\n",
425 KBUILD_MODNAME, ret);
426 goto err_dvb_net_init;
429 mutex_init(&adap->sync_mutex);
433 dvb_dmxdev_release(&adap->dmxdev);
435 dvb_dmx_release(&adap->demux);
437 dvb_unregister_adapter(&adap->dvb_adap);
438 err_dvb_register_adapter:
439 adap->dvb_adap.priv = NULL;
443 static int dvb_usbv2_adapter_dvb_exit(struct dvb_usb_adapter *adap)
445 dev_dbg(&adap_to_d(adap)->udev->dev, "%s: adap=%d\n", __func__,
448 if (adap->dvb_adap.priv) {
449 dvb_net_release(&adap->dvb_net);
450 adap->demux.dmx.close(&adap->demux.dmx);
451 dvb_dmxdev_release(&adap->dmxdev);
452 dvb_dmx_release(&adap->demux);
453 dvb_unregister_adapter(&adap->dvb_adap);
459 static int dvb_usbv2_device_power_ctrl(struct dvb_usb_device *d, int onoff)
468 if (d->powered == 0 || (onoff && d->powered == 1)) {
469 /* when switching from 1 to 0 or from 0 to 1 */
470 dev_dbg(&d->udev->dev, "%s: power=%d\n", __func__, onoff);
471 if (d->props->power_ctrl) {
472 ret = d->props->power_ctrl(d, onoff);
480 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
484 static int dvb_usb_fe_init(struct dvb_frontend *fe)
487 struct dvb_usb_adapter *adap = fe->dvb->priv;
488 struct dvb_usb_device *d = adap_to_d(adap);
489 dev_dbg(&d->udev->dev, "%s: adap=%d fe=%d\n", __func__, adap->id,
492 if (!adap->suspend_resume_active) {
493 adap->active_fe = fe->id;
494 mutex_lock(&adap->sync_mutex);
497 ret = dvb_usbv2_device_power_ctrl(d, 1);
501 if (d->props->frontend_ctrl) {
502 ret = d->props->frontend_ctrl(fe, 1);
507 if (adap->fe_init[fe->id]) {
508 ret = adap->fe_init[fe->id](fe);
513 if (!adap->suspend_resume_active)
514 mutex_unlock(&adap->sync_mutex);
516 dev_dbg(&d->udev->dev, "%s: ret=%d\n", __func__, ret);
520 static int dvb_usb_fe_sleep(struct dvb_frontend *fe)
523 struct dvb_usb_adapter *adap = fe->dvb->priv;
524 struct dvb_usb_device *d = adap_to_d(adap);
525 dev_dbg(&d->udev->dev, "%s: adap=%d fe=%d\n", __func__, adap->id,
528 if (!adap->suspend_resume_active)
529 mutex_lock(&adap->sync_mutex);
531 if (adap->fe_sleep[fe->id]) {
532 ret = adap->fe_sleep[fe->id](fe);
537 if (d->props->frontend_ctrl) {
538 ret = d->props->frontend_ctrl(fe, 0);
543 ret = dvb_usbv2_device_power_ctrl(d, 0);
547 if (!adap->suspend_resume_active) {
548 adap->active_fe = -1;
549 mutex_unlock(&adap->sync_mutex);
552 dev_dbg(&d->udev->dev, "%s: ret=%d\n", __func__, ret);
556 static int dvb_usbv2_adapter_frontend_init(struct dvb_usb_adapter *adap)
558 int ret, i, count_registered = 0;
559 struct dvb_usb_device *d = adap_to_d(adap);
560 dev_dbg(&d->udev->dev, "%s: adap=%d\n", __func__, adap->id);
562 memset(adap->fe, 0, sizeof(adap->fe));
563 adap->active_fe = -1;
565 if (d->props->frontend_attach) {
566 ret = d->props->frontend_attach(adap);
568 dev_dbg(&d->udev->dev, "%s: frontend_attach() " \
569 "failed=%d\n", __func__, ret);
570 goto err_dvb_frontend_detach;
573 dev_dbg(&d->udev->dev, "%s: frontend_attach() do not exists\n",
579 for (i = 0; i < MAX_NO_OF_FE_PER_ADAP && adap->fe[i]; i++) {
581 /* re-assign sleep and wakeup functions */
582 adap->fe_init[i] = adap->fe[i]->ops.init;
583 adap->fe[i]->ops.init = dvb_usb_fe_init;
584 adap->fe_sleep[i] = adap->fe[i]->ops.sleep;
585 adap->fe[i]->ops.sleep = dvb_usb_fe_sleep;
587 ret = dvb_register_frontend(&adap->dvb_adap, adap->fe[i]);
589 dev_err(&d->udev->dev, "%s: frontend%d registration " \
590 "failed\n", KBUILD_MODNAME, i);
591 goto err_dvb_unregister_frontend;
597 if (d->props->tuner_attach) {
598 ret = d->props->tuner_attach(adap);
600 dev_dbg(&d->udev->dev, "%s: tuner_attach() failed=%d\n",
602 goto err_dvb_unregister_frontend;
608 err_dvb_unregister_frontend:
609 for (i = count_registered - 1; i >= 0; i--)
610 dvb_unregister_frontend(adap->fe[i]);
612 err_dvb_frontend_detach:
613 for (i = MAX_NO_OF_FE_PER_ADAP - 1; i >= 0; i--) {
615 dvb_frontend_detach(adap->fe[i]);
621 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
625 static int dvb_usbv2_adapter_frontend_exit(struct dvb_usb_adapter *adap)
628 dev_dbg(&adap_to_d(adap)->udev->dev, "%s: adap=%d\n", __func__,
631 for (i = MAX_NO_OF_FE_PER_ADAP - 1; i >= 0; i--) {
633 dvb_unregister_frontend(adap->fe[i]);
634 dvb_frontend_detach(adap->fe[i]);
641 static int dvb_usbv2_adapter_init(struct dvb_usb_device *d)
643 struct dvb_usb_adapter *adap;
644 int ret, i, adapter_count;
646 /* resolve adapter count */
647 adapter_count = d->props->num_adapters;
648 if (d->props->get_adapter_count) {
649 ret = d->props->get_adapter_count(d);
656 for (i = 0; i < adapter_count; i++) {
657 adap = &d->adapter[i];
659 adap->props = &d->props->adapter[i];
661 /* speed - when running at FULL speed we need a HW PID filter */
662 if (d->udev->speed == USB_SPEED_FULL &&
663 !(adap->props->caps & DVB_USB_ADAP_HAS_PID_FILTER)) {
664 dev_err(&d->udev->dev, "%s: this USB2.0 device " \
665 "cannot be run on a USB1.1 port (it " \
666 "lacks a hardware PID filter)\n",
670 } else if ((d->udev->speed == USB_SPEED_FULL &&
671 adap->props->caps & DVB_USB_ADAP_HAS_PID_FILTER) ||
672 (adap->props->caps & DVB_USB_ADAP_NEED_PID_FILTERING)) {
673 dev_info(&d->udev->dev, "%s: will use the device's " \
674 "hardware PID filter " \
675 "(table count: %d)\n", KBUILD_MODNAME,
676 adap->props->pid_filter_count);
677 adap->pid_filtering = 1;
678 adap->max_feed_count = adap->props->pid_filter_count;
680 dev_info(&d->udev->dev, "%s: will pass the complete " \
681 "MPEG2 transport stream to the " \
682 "software demuxer\n", KBUILD_MODNAME);
683 adap->pid_filtering = 0;
684 adap->max_feed_count = 255;
687 if (!adap->pid_filtering && dvb_usb_force_pid_filter_usage &&
688 adap->props->caps & DVB_USB_ADAP_HAS_PID_FILTER) {
689 dev_info(&d->udev->dev, "%s: PID filter enabled by " \
690 "module option\n", KBUILD_MODNAME);
691 adap->pid_filtering = 1;
692 adap->max_feed_count = adap->props->pid_filter_count;
695 ret = dvb_usbv2_adapter_stream_init(adap);
699 ret = dvb_usbv2_adapter_dvb_init(adap);
703 ret = dvb_usbv2_adapter_frontend_init(adap);
707 /* use exclusive FE lock if there is multiple shared FEs */
709 adap->dvb_adap.mfe_shared = 1;
714 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
718 static int dvb_usbv2_adapter_exit(struct dvb_usb_device *d)
721 dev_dbg(&d->udev->dev, "%s:\n", __func__);
723 for (i = MAX_NO_OF_ADAPTER_PER_DEVICE - 1; i >= 0; i--) {
724 if (d->adapter[i].props) {
725 dvb_usbv2_adapter_frontend_exit(&d->adapter[i]);
726 dvb_usbv2_adapter_dvb_exit(&d->adapter[i]);
727 dvb_usbv2_adapter_stream_exit(&d->adapter[i]);
734 /* general initialization functions */
735 static int dvb_usbv2_exit(struct dvb_usb_device *d)
737 dev_dbg(&d->udev->dev, "%s:\n", __func__);
739 dvb_usbv2_remote_exit(d);
740 dvb_usbv2_adapter_exit(d);
741 dvb_usbv2_i2c_exit(d);
748 static int dvb_usbv2_init(struct dvb_usb_device *d)
751 dev_dbg(&d->udev->dev, "%s:\n", __func__);
753 dvb_usbv2_device_power_ctrl(d, 1);
755 if (d->props->read_config) {
756 ret = d->props->read_config(d);
761 ret = dvb_usbv2_i2c_init(d);
765 ret = dvb_usbv2_adapter_init(d);
769 if (d->props->init) {
770 ret = d->props->init(d);
775 ret = dvb_usbv2_remote_init(d);
779 dvb_usbv2_device_power_ctrl(d, 0);
783 dvb_usbv2_device_power_ctrl(d, 0);
784 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
789 * udev, which is used for the firmware downloading, requires we cannot
790 * block during module_init(). module_init() calls USB probe() which
791 * is this routine. Due to that we delay actual operation using workqueue
792 * and return always success here.
794 static void dvb_usbv2_init_work(struct work_struct *work)
797 struct dvb_usb_device *d =
798 container_of(work, struct dvb_usb_device, probe_work);
800 d->work_pid = current->pid;
801 dev_dbg(&d->udev->dev, "%s: work_pid=%d\n", __func__, d->work_pid);
803 if (d->props->size_of_priv) {
804 d->priv = kzalloc(d->props->size_of_priv, GFP_KERNEL);
806 dev_err(&d->udev->dev, "%s: kzalloc() failed\n",
809 goto err_usb_driver_release_interface;
813 if (d->props->identify_state) {
814 const char *name = NULL;
815 ret = d->props->identify_state(d, &name);
818 } else if (ret == COLD) {
819 dev_info(&d->udev->dev, "%s: found a '%s' in cold " \
820 "state\n", KBUILD_MODNAME, d->name);
823 name = d->props->firmware;
825 ret = dvb_usbv2_download_firmware(d, name);
827 /* device is warm, continue initialization */
829 } else if (ret == RECONNECTS_USB) {
831 * USB core will call disconnect() and then
832 * probe() as device reconnects itself from the
833 * USB bus. disconnect() will release all driver
834 * resources and probe() is called for 'new'
835 * device. As 'new' device is warm we should
836 * never go here again.
841 * Unexpected error. We must unregister driver
842 * manually from the device, because device is
843 * already register by returning from probe()
844 * with success. usb_driver_release_interface()
845 * finally calls disconnect() in order to free
848 goto err_usb_driver_release_interface;
851 goto err_usb_driver_release_interface;
855 dev_info(&d->udev->dev, "%s: found a '%s' in warm state\n",
856 KBUILD_MODNAME, d->name);
858 ret = dvb_usbv2_init(d);
860 goto err_usb_driver_release_interface;
862 dev_info(&d->udev->dev, "%s: '%s' successfully initialized and " \
863 "connected\n", KBUILD_MODNAME, d->name);
866 err_usb_driver_release_interface:
867 dev_info(&d->udev->dev, "%s: '%s' error while loading driver (%d)\n",
868 KBUILD_MODNAME, d->name, ret);
869 usb_driver_release_interface(to_usb_driver(d->intf->dev.driver),
871 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
875 int dvb_usbv2_probe(struct usb_interface *intf,
876 const struct usb_device_id *id)
879 struct dvb_usb_device *d;
880 struct usb_device *udev = interface_to_usbdev(intf);
881 struct dvb_usb_driver_info *driver_info =
882 (struct dvb_usb_driver_info *) id->driver_info;
884 dev_dbg(&udev->dev, "%s: bInterfaceNumber=%d\n", __func__,
885 intf->cur_altsetting->desc.bInterfaceNumber);
887 if (!id->driver_info) {
888 dev_err(&udev->dev, "%s: driver_info failed\n", KBUILD_MODNAME);
893 d = kzalloc(sizeof(struct dvb_usb_device), GFP_KERNEL);
895 dev_err(&udev->dev, "%s: kzalloc() failed\n", KBUILD_MODNAME);
900 d->name = driver_info->name;
901 d->rc_map = driver_info->rc_map;
904 d->props = driver_info->props;
906 if (d->intf->cur_altsetting->desc.bInterfaceNumber !=
907 d->props->bInterfaceNumber) {
912 mutex_init(&d->usb_mutex);
913 mutex_init(&d->i2c_mutex);
914 INIT_WORK(&d->probe_work, dvb_usbv2_init_work);
915 usb_set_intfdata(intf, d);
916 ret = schedule_work(&d->probe_work);
918 dev_err(&d->udev->dev, "%s: schedule_work() failed\n",
927 dev_dbg(&udev->dev, "%s: failed=%d\n", __func__, ret);
930 EXPORT_SYMBOL(dvb_usbv2_probe);
932 void dvb_usbv2_disconnect(struct usb_interface *intf)
934 struct dvb_usb_device *d = usb_get_intfdata(intf);
935 const char *name = d->name;
936 struct device dev = d->udev->dev;
937 dev_dbg(&d->udev->dev, "%s: pid=%d work_pid=%d\n", __func__,
938 current->pid, d->work_pid);
940 /* ensure initialization work is finished until release resources */
941 if (d->work_pid != current->pid)
942 cancel_work_sync(&d->probe_work);
949 dev_info(&dev, "%s: '%s' successfully deinitialized and disconnected\n",
950 KBUILD_MODNAME, name);
952 EXPORT_SYMBOL(dvb_usbv2_disconnect);
954 int dvb_usbv2_suspend(struct usb_interface *intf, pm_message_t msg)
956 struct dvb_usb_device *d = usb_get_intfdata(intf);
957 int ret = 0, i, active_fe;
958 struct dvb_frontend *fe;
959 dev_dbg(&d->udev->dev, "%s:\n", __func__);
961 /* stop remote controller poll */
962 if (d->rc.query && !d->rc.bulk_mode)
963 cancel_delayed_work_sync(&d->rc_query_work);
965 for (i = MAX_NO_OF_ADAPTER_PER_DEVICE - 1; i >= 0; i--) {
966 active_fe = d->adapter[i].active_fe;
967 if (d->adapter[i].dvb_adap.priv && active_fe != -1) {
968 fe = d->adapter[i].fe[active_fe];
969 d->adapter[i].suspend_resume_active = true;
971 if (d->props->streaming_ctrl)
972 d->props->streaming_ctrl(fe, 0);
974 /* stop usb streaming */
975 usb_urb_killv2(&d->adapter[i].stream);
977 ret = dvb_frontend_suspend(fe);
983 EXPORT_SYMBOL(dvb_usbv2_suspend);
985 static int dvb_usbv2_resume_common(struct dvb_usb_device *d)
987 int ret = 0, i, active_fe;
988 struct dvb_frontend *fe;
989 dev_dbg(&d->udev->dev, "%s:\n", __func__);
991 for (i = 0; i < MAX_NO_OF_ADAPTER_PER_DEVICE; i++) {
992 active_fe = d->adapter[i].active_fe;
993 if (d->adapter[i].dvb_adap.priv && active_fe != -1) {
994 fe = d->adapter[i].fe[active_fe];
996 ret = dvb_frontend_resume(fe);
998 /* resume usb streaming */
999 usb_urb_submitv2(&d->adapter[i].stream, NULL);
1001 if (d->props->streaming_ctrl)
1002 d->props->streaming_ctrl(fe, 1);
1004 d->adapter[i].suspend_resume_active = false;
1008 /* start remote controller poll */
1009 if (d->rc.query && !d->rc.bulk_mode)
1010 schedule_delayed_work(&d->rc_query_work,
1011 msecs_to_jiffies(d->rc.interval));
1016 int dvb_usbv2_resume(struct usb_interface *intf)
1018 struct dvb_usb_device *d = usb_get_intfdata(intf);
1019 dev_dbg(&d->udev->dev, "%s:\n", __func__);
1021 return dvb_usbv2_resume_common(d);
1023 EXPORT_SYMBOL(dvb_usbv2_resume);
1025 int dvb_usbv2_reset_resume(struct usb_interface *intf)
1027 struct dvb_usb_device *d = usb_get_intfdata(intf);
1029 dev_dbg(&d->udev->dev, "%s:\n", __func__);
1031 dvb_usbv2_device_power_ctrl(d, 1);
1036 ret = dvb_usbv2_resume_common(d);
1038 dvb_usbv2_device_power_ctrl(d, 0);
1042 EXPORT_SYMBOL(dvb_usbv2_reset_resume);
1044 MODULE_VERSION("2.0");
1045 MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@desy.de>");
1046 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1047 MODULE_DESCRIPTION("DVB USB common");
1048 MODULE_LICENSE("GPL");