net: qmi_wwan: add Olivetti Olicard 500
[linux/fpc-iii.git] / drivers / media / usb / dvb-usb-v2 / dvb_usb_core.c
blobde02db802acea6bc47caacab100d6481eb639f8f
1 /*
2 * DVB USB framework
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,
30 int, 0444);
31 MODULE_PARM_DESC(force_pid_filter_usage,
32 "force all DVB USB devices to use a PID filter, if any (default: 0)");
34 static int dvb_usbv2_download_firmware(struct dvb_usb_device *d,
35 const char *name)
37 int ret;
38 const struct firmware *fw;
39 dev_dbg(&d->udev->dev, "%s:\n", __func__);
41 if (!d->props->download_firmware) {
42 ret = -EINVAL;
43 goto err;
46 ret = request_firmware(&fw, name, &d->udev->dev);
47 if (ret < 0) {
48 dev_err(&d->udev->dev,
49 "%s: Did not find the firmware file '%s'. Please see linux/Documentation/dvb/ for more details on firmware-problems. Status %d\n",
50 KBUILD_MODNAME, name, ret);
51 goto err;
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);
58 release_firmware(fw);
59 if (ret < 0)
60 goto err;
62 return ret;
63 err:
64 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
65 return ret;
68 static int dvb_usbv2_i2c_init(struct dvb_usb_device *d)
70 int ret;
71 dev_dbg(&d->udev->dev, "%s:\n", __func__);
73 if (!d->props->i2c_algo)
74 return 0;
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);
82 if (ret < 0) {
83 d->i2c_adap.algo = NULL;
84 dev_err(&d->udev->dev, "%s: i2c_add_adapter() failed=%d\n",
85 KBUILD_MODNAME, ret);
86 goto err;
89 return 0;
90 err:
91 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
92 return ret;
95 static int dvb_usbv2_i2c_exit(struct dvb_usb_device *d)
97 dev_dbg(&d->udev->dev, "%s:\n", __func__);
99 if (d->i2c_adap.algo)
100 i2c_del_adapter(&d->i2c_adap);
102 return 0;
105 #if IS_ENABLED(CONFIG_RC_CORE)
106 static void dvb_usb_read_remote_control(struct work_struct *work)
108 struct dvb_usb_device *d = container_of(work,
109 struct dvb_usb_device, rc_query_work.work);
110 int ret;
113 * When the parameter has been set to 1 via sysfs while the
114 * driver was running, or when bulk mode is enabled after IR init.
116 if (dvb_usbv2_disable_rc_polling || d->rc.bulk_mode) {
117 d->rc_polling_active = false;
118 return;
121 ret = d->rc.query(d);
122 if (ret < 0) {
123 dev_err(&d->udev->dev, "%s: rc.query() failed=%d\n",
124 KBUILD_MODNAME, ret);
125 d->rc_polling_active = false;
126 return; /* stop polling */
129 schedule_delayed_work(&d->rc_query_work,
130 msecs_to_jiffies(d->rc.interval));
133 static int dvb_usbv2_remote_init(struct dvb_usb_device *d)
135 int ret;
136 struct rc_dev *dev;
137 dev_dbg(&d->udev->dev, "%s:\n", __func__);
139 if (dvb_usbv2_disable_rc_polling || !d->props->get_rc_config)
140 return 0;
142 d->rc.map_name = d->rc_map;
143 ret = d->props->get_rc_config(d, &d->rc);
144 if (ret < 0)
145 goto err;
147 /* disable rc when there is no keymap defined */
148 if (!d->rc.map_name)
149 return 0;
151 dev = rc_allocate_device();
152 if (!dev) {
153 ret = -ENOMEM;
154 goto err;
157 dev->dev.parent = &d->udev->dev;
158 dev->input_name = d->name;
159 usb_make_path(d->udev, d->rc_phys, sizeof(d->rc_phys));
160 strlcat(d->rc_phys, "/ir0", sizeof(d->rc_phys));
161 dev->input_phys = d->rc_phys;
162 usb_to_input_id(d->udev, &dev->input_id);
163 /* TODO: likely RC-core should took const char * */
164 dev->driver_name = (char *) d->props->driver_name;
165 dev->map_name = d->rc.map_name;
166 dev->driver_type = d->rc.driver_type;
167 rc_set_allowed_protocols(dev, d->rc.allowed_protos);
168 dev->change_protocol = d->rc.change_protocol;
169 dev->priv = d;
171 ret = rc_register_device(dev);
172 if (ret < 0) {
173 rc_free_device(dev);
174 goto err;
177 d->rc_dev = dev;
179 /* start polling if needed */
180 if (d->rc.query && !d->rc.bulk_mode) {
181 /* initialize a work queue for handling polling */
182 INIT_DELAYED_WORK(&d->rc_query_work,
183 dvb_usb_read_remote_control);
184 dev_info(&d->udev->dev,
185 "%s: schedule remote query interval to %d msecs\n",
186 KBUILD_MODNAME, d->rc.interval);
187 schedule_delayed_work(&d->rc_query_work,
188 msecs_to_jiffies(d->rc.interval));
189 d->rc_polling_active = true;
192 return 0;
193 err:
194 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
195 return ret;
198 static int dvb_usbv2_remote_exit(struct dvb_usb_device *d)
200 dev_dbg(&d->udev->dev, "%s:\n", __func__);
202 if (d->rc_dev) {
203 cancel_delayed_work_sync(&d->rc_query_work);
204 rc_unregister_device(d->rc_dev);
205 d->rc_dev = NULL;
208 return 0;
210 #else
211 #define dvb_usbv2_remote_init(args...) 0
212 #define dvb_usbv2_remote_exit(args...)
213 #endif
215 static void dvb_usb_data_complete(struct usb_data_stream *stream, u8 *buf,
216 size_t len)
218 struct dvb_usb_adapter *adap = stream->user_priv;
219 dvb_dmx_swfilter(&adap->demux, buf, len);
222 static void dvb_usb_data_complete_204(struct usb_data_stream *stream, u8 *buf,
223 size_t len)
225 struct dvb_usb_adapter *adap = stream->user_priv;
226 dvb_dmx_swfilter_204(&adap->demux, buf, len);
229 static void dvb_usb_data_complete_raw(struct usb_data_stream *stream, u8 *buf,
230 size_t len)
232 struct dvb_usb_adapter *adap = stream->user_priv;
233 dvb_dmx_swfilter_raw(&adap->demux, buf, len);
236 static int dvb_usbv2_adapter_stream_init(struct dvb_usb_adapter *adap)
238 dev_dbg(&adap_to_d(adap)->udev->dev, "%s: adap=%d\n", __func__,
239 adap->id);
241 adap->stream.udev = adap_to_d(adap)->udev;
242 adap->stream.user_priv = adap;
243 adap->stream.complete = dvb_usb_data_complete;
245 return usb_urb_initv2(&adap->stream, &adap->props->stream);
248 static int dvb_usbv2_adapter_stream_exit(struct dvb_usb_adapter *adap)
250 dev_dbg(&adap_to_d(adap)->udev->dev, "%s: adap=%d\n", __func__,
251 adap->id);
253 return usb_urb_exitv2(&adap->stream);
256 static int wait_schedule(void *ptr)
258 schedule();
260 return 0;
263 static int dvb_usb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
265 struct dvb_usb_adapter *adap = dvbdmxfeed->demux->priv;
266 struct dvb_usb_device *d = adap_to_d(adap);
267 int ret = 0;
268 struct usb_data_stream_properties stream_props;
269 dev_dbg(&d->udev->dev,
270 "%s: adap=%d active_fe=%d feed_type=%d setting pid [%s]: %04x (%04d) at index %d\n",
271 __func__, adap->id, adap->active_fe, dvbdmxfeed->type,
272 adap->pid_filtering ? "yes" : "no", dvbdmxfeed->pid,
273 dvbdmxfeed->pid, dvbdmxfeed->index);
275 /* wait init is done */
276 wait_on_bit(&adap->state_bits, ADAP_INIT, wait_schedule,
277 TASK_UNINTERRUPTIBLE);
279 if (adap->active_fe == -1)
280 return -EINVAL;
282 /* skip feed setup if we are already feeding */
283 if (adap->feed_count++ > 0)
284 goto skip_feed_start;
286 /* set 'streaming' status bit */
287 set_bit(ADAP_STREAMING, &adap->state_bits);
289 /* resolve input and output streaming parameters */
290 if (d->props->get_stream_config) {
291 memcpy(&stream_props, &adap->props->stream,
292 sizeof(struct usb_data_stream_properties));
293 ret = d->props->get_stream_config(adap->fe[adap->active_fe],
294 &adap->ts_type, &stream_props);
295 if (ret)
296 dev_err(&d->udev->dev,
297 "%s: get_stream_config() failed=%d\n",
298 KBUILD_MODNAME, ret);
299 } else {
300 stream_props = adap->props->stream;
303 switch (adap->ts_type) {
304 case DVB_USB_FE_TS_TYPE_204:
305 adap->stream.complete = dvb_usb_data_complete_204;
306 break;
307 case DVB_USB_FE_TS_TYPE_RAW:
308 adap->stream.complete = dvb_usb_data_complete_raw;
309 break;
310 case DVB_USB_FE_TS_TYPE_188:
311 default:
312 adap->stream.complete = dvb_usb_data_complete;
313 break;
316 /* submit USB streaming packets */
317 usb_urb_submitv2(&adap->stream, &stream_props);
319 /* enable HW PID filter */
320 if (adap->pid_filtering && adap->props->pid_filter_ctrl) {
321 ret = adap->props->pid_filter_ctrl(adap, 1);
322 if (ret)
323 dev_err(&d->udev->dev,
324 "%s: pid_filter_ctrl() failed=%d\n",
325 KBUILD_MODNAME, ret);
328 /* ask device to start streaming */
329 if (d->props->streaming_ctrl) {
330 ret = d->props->streaming_ctrl(adap->fe[adap->active_fe], 1);
331 if (ret)
332 dev_err(&d->udev->dev,
333 "%s: streaming_ctrl() failed=%d\n",
334 KBUILD_MODNAME, ret);
336 skip_feed_start:
338 /* add PID to device HW PID filter */
339 if (adap->pid_filtering && adap->props->pid_filter) {
340 ret = adap->props->pid_filter(adap, dvbdmxfeed->index,
341 dvbdmxfeed->pid, 1);
342 if (ret)
343 dev_err(&d->udev->dev, "%s: pid_filter() failed=%d\n",
344 KBUILD_MODNAME, ret);
347 if (ret)
348 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
349 return ret;
352 static int dvb_usb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
354 struct dvb_usb_adapter *adap = dvbdmxfeed->demux->priv;
355 struct dvb_usb_device *d = adap_to_d(adap);
356 int ret = 0;
357 dev_dbg(&d->udev->dev,
358 "%s: adap=%d active_fe=%d feed_type=%d setting pid [%s]: %04x (%04d) at index %d\n",
359 __func__, adap->id, adap->active_fe, dvbdmxfeed->type,
360 adap->pid_filtering ? "yes" : "no", dvbdmxfeed->pid,
361 dvbdmxfeed->pid, dvbdmxfeed->index);
363 if (adap->active_fe == -1)
364 return -EINVAL;
366 /* remove PID from device HW PID filter */
367 if (adap->pid_filtering && adap->props->pid_filter) {
368 ret = adap->props->pid_filter(adap, dvbdmxfeed->index,
369 dvbdmxfeed->pid, 0);
370 if (ret)
371 dev_err(&d->udev->dev, "%s: pid_filter() failed=%d\n",
372 KBUILD_MODNAME, ret);
375 /* we cannot stop streaming until last PID is removed */
376 if (--adap->feed_count > 0)
377 goto skip_feed_stop;
379 /* ask device to stop streaming */
380 if (d->props->streaming_ctrl) {
381 ret = d->props->streaming_ctrl(adap->fe[adap->active_fe], 0);
382 if (ret)
383 dev_err(&d->udev->dev,
384 "%s: streaming_ctrl() failed=%d\n",
385 KBUILD_MODNAME, ret);
388 /* disable HW PID filter */
389 if (adap->pid_filtering && adap->props->pid_filter_ctrl) {
390 ret = adap->props->pid_filter_ctrl(adap, 0);
391 if (ret)
392 dev_err(&d->udev->dev,
393 "%s: pid_filter_ctrl() failed=%d\n",
394 KBUILD_MODNAME, ret);
397 /* kill USB streaming packets */
398 usb_urb_killv2(&adap->stream);
400 /* clear 'streaming' status bit */
401 clear_bit(ADAP_STREAMING, &adap->state_bits);
402 smp_mb__after_clear_bit();
403 wake_up_bit(&adap->state_bits, ADAP_STREAMING);
404 skip_feed_stop:
406 if (ret)
407 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
408 return ret;
411 static int dvb_usbv2_adapter_dvb_init(struct dvb_usb_adapter *adap)
413 int ret;
414 struct dvb_usb_device *d = adap_to_d(adap);
415 dev_dbg(&d->udev->dev, "%s: adap=%d\n", __func__, adap->id);
417 ret = dvb_register_adapter(&adap->dvb_adap, d->name, d->props->owner,
418 &d->udev->dev, d->props->adapter_nr);
419 if (ret < 0) {
420 dev_dbg(&d->udev->dev, "%s: dvb_register_adapter() failed=%d\n",
421 __func__, ret);
422 goto err_dvb_register_adapter;
425 adap->dvb_adap.priv = adap;
427 if (d->props->read_mac_address) {
428 ret = d->props->read_mac_address(adap,
429 adap->dvb_adap.proposed_mac);
430 if (ret < 0)
431 goto err_dvb_dmx_init;
433 dev_info(&d->udev->dev, "%s: MAC address: %pM\n",
434 KBUILD_MODNAME, adap->dvb_adap.proposed_mac);
437 adap->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING;
438 adap->demux.priv = adap;
439 adap->demux.filternum = 0;
440 adap->demux.filternum = adap->max_feed_count;
441 adap->demux.feednum = adap->demux.filternum;
442 adap->demux.start_feed = dvb_usb_start_feed;
443 adap->demux.stop_feed = dvb_usb_stop_feed;
444 adap->demux.write_to_decoder = NULL;
445 ret = dvb_dmx_init(&adap->demux);
446 if (ret < 0) {
447 dev_err(&d->udev->dev, "%s: dvb_dmx_init() failed=%d\n",
448 KBUILD_MODNAME, ret);
449 goto err_dvb_dmx_init;
452 adap->dmxdev.filternum = adap->demux.filternum;
453 adap->dmxdev.demux = &adap->demux.dmx;
454 adap->dmxdev.capabilities = 0;
455 ret = dvb_dmxdev_init(&adap->dmxdev, &adap->dvb_adap);
456 if (ret < 0) {
457 dev_err(&d->udev->dev, "%s: dvb_dmxdev_init() failed=%d\n",
458 KBUILD_MODNAME, ret);
459 goto err_dvb_dmxdev_init;
462 ret = dvb_net_init(&adap->dvb_adap, &adap->dvb_net, &adap->demux.dmx);
463 if (ret < 0) {
464 dev_err(&d->udev->dev, "%s: dvb_net_init() failed=%d\n",
465 KBUILD_MODNAME, ret);
466 goto err_dvb_net_init;
469 return 0;
470 err_dvb_net_init:
471 dvb_dmxdev_release(&adap->dmxdev);
472 err_dvb_dmxdev_init:
473 dvb_dmx_release(&adap->demux);
474 err_dvb_dmx_init:
475 dvb_unregister_adapter(&adap->dvb_adap);
476 err_dvb_register_adapter:
477 adap->dvb_adap.priv = NULL;
478 return ret;
481 static int dvb_usbv2_adapter_dvb_exit(struct dvb_usb_adapter *adap)
483 dev_dbg(&adap_to_d(adap)->udev->dev, "%s: adap=%d\n", __func__,
484 adap->id);
486 if (adap->dvb_adap.priv) {
487 dvb_net_release(&adap->dvb_net);
488 adap->demux.dmx.close(&adap->demux.dmx);
489 dvb_dmxdev_release(&adap->dmxdev);
490 dvb_dmx_release(&adap->demux);
491 dvb_unregister_adapter(&adap->dvb_adap);
494 return 0;
497 static int dvb_usbv2_device_power_ctrl(struct dvb_usb_device *d, int onoff)
499 int ret;
501 if (onoff)
502 d->powered++;
503 else
504 d->powered--;
506 if (d->powered == 0 || (onoff && d->powered == 1)) {
507 /* when switching from 1 to 0 or from 0 to 1 */
508 dev_dbg(&d->udev->dev, "%s: power=%d\n", __func__, onoff);
509 if (d->props->power_ctrl) {
510 ret = d->props->power_ctrl(d, onoff);
511 if (ret < 0)
512 goto err;
516 return 0;
517 err:
518 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
519 return ret;
522 static int dvb_usb_fe_init(struct dvb_frontend *fe)
524 int ret;
525 struct dvb_usb_adapter *adap = fe->dvb->priv;
526 struct dvb_usb_device *d = adap_to_d(adap);
527 dev_dbg(&d->udev->dev, "%s: adap=%d fe=%d\n", __func__, adap->id,
528 fe->id);
530 if (!adap->suspend_resume_active) {
531 adap->active_fe = fe->id;
532 set_bit(ADAP_INIT, &adap->state_bits);
535 ret = dvb_usbv2_device_power_ctrl(d, 1);
536 if (ret < 0)
537 goto err;
539 if (d->props->frontend_ctrl) {
540 ret = d->props->frontend_ctrl(fe, 1);
541 if (ret < 0)
542 goto err;
545 if (adap->fe_init[fe->id]) {
546 ret = adap->fe_init[fe->id](fe);
547 if (ret < 0)
548 goto err;
550 err:
551 if (!adap->suspend_resume_active) {
552 clear_bit(ADAP_INIT, &adap->state_bits);
553 smp_mb__after_clear_bit();
554 wake_up_bit(&adap->state_bits, ADAP_INIT);
557 dev_dbg(&d->udev->dev, "%s: ret=%d\n", __func__, ret);
558 return ret;
561 static int dvb_usb_fe_sleep(struct dvb_frontend *fe)
563 int ret;
564 struct dvb_usb_adapter *adap = fe->dvb->priv;
565 struct dvb_usb_device *d = adap_to_d(adap);
566 dev_dbg(&d->udev->dev, "%s: adap=%d fe=%d\n", __func__, adap->id,
567 fe->id);
569 if (!adap->suspend_resume_active) {
570 set_bit(ADAP_SLEEP, &adap->state_bits);
571 wait_on_bit(&adap->state_bits, ADAP_STREAMING, wait_schedule,
572 TASK_UNINTERRUPTIBLE);
575 if (adap->fe_sleep[fe->id]) {
576 ret = adap->fe_sleep[fe->id](fe);
577 if (ret < 0)
578 goto err;
581 if (d->props->frontend_ctrl) {
582 ret = d->props->frontend_ctrl(fe, 0);
583 if (ret < 0)
584 goto err;
587 ret = dvb_usbv2_device_power_ctrl(d, 0);
588 if (ret < 0)
589 goto err;
590 err:
591 if (!adap->suspend_resume_active) {
592 adap->active_fe = -1;
593 clear_bit(ADAP_SLEEP, &adap->state_bits);
594 smp_mb__after_clear_bit();
595 wake_up_bit(&adap->state_bits, ADAP_SLEEP);
598 dev_dbg(&d->udev->dev, "%s: ret=%d\n", __func__, ret);
599 return ret;
602 static int dvb_usbv2_adapter_frontend_init(struct dvb_usb_adapter *adap)
604 int ret, i, count_registered = 0;
605 struct dvb_usb_device *d = adap_to_d(adap);
606 dev_dbg(&d->udev->dev, "%s: adap=%d\n", __func__, adap->id);
608 memset(adap->fe, 0, sizeof(adap->fe));
609 adap->active_fe = -1;
611 if (d->props->frontend_attach) {
612 ret = d->props->frontend_attach(adap);
613 if (ret < 0) {
614 dev_dbg(&d->udev->dev,
615 "%s: frontend_attach() failed=%d\n",
616 __func__, ret);
617 goto err_dvb_frontend_detach;
619 } else {
620 dev_dbg(&d->udev->dev, "%s: frontend_attach() do not exists\n",
621 __func__);
622 ret = 0;
623 goto err;
626 for (i = 0; i < MAX_NO_OF_FE_PER_ADAP && adap->fe[i]; i++) {
627 adap->fe[i]->id = i;
628 /* re-assign sleep and wakeup functions */
629 adap->fe_init[i] = adap->fe[i]->ops.init;
630 adap->fe[i]->ops.init = dvb_usb_fe_init;
631 adap->fe_sleep[i] = adap->fe[i]->ops.sleep;
632 adap->fe[i]->ops.sleep = dvb_usb_fe_sleep;
634 ret = dvb_register_frontend(&adap->dvb_adap, adap->fe[i]);
635 if (ret < 0) {
636 dev_err(&d->udev->dev,
637 "%s: frontend%d registration failed\n",
638 KBUILD_MODNAME, i);
639 goto err_dvb_unregister_frontend;
642 count_registered++;
645 if (d->props->tuner_attach) {
646 ret = d->props->tuner_attach(adap);
647 if (ret < 0) {
648 dev_dbg(&d->udev->dev, "%s: tuner_attach() failed=%d\n",
649 __func__, ret);
650 goto err_dvb_unregister_frontend;
654 return 0;
656 err_dvb_unregister_frontend:
657 for (i = count_registered - 1; i >= 0; i--)
658 dvb_unregister_frontend(adap->fe[i]);
660 err_dvb_frontend_detach:
661 for (i = MAX_NO_OF_FE_PER_ADAP - 1; i >= 0; i--) {
662 if (adap->fe[i]) {
663 dvb_frontend_detach(adap->fe[i]);
664 adap->fe[i] = NULL;
668 err:
669 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
670 return ret;
673 static int dvb_usbv2_adapter_frontend_exit(struct dvb_usb_adapter *adap)
675 int i;
676 dev_dbg(&adap_to_d(adap)->udev->dev, "%s: adap=%d\n", __func__,
677 adap->id);
679 for (i = MAX_NO_OF_FE_PER_ADAP - 1; i >= 0; i--) {
680 if (adap->fe[i]) {
681 dvb_unregister_frontend(adap->fe[i]);
682 dvb_frontend_detach(adap->fe[i]);
686 return 0;
689 static int dvb_usbv2_adapter_init(struct dvb_usb_device *d)
691 struct dvb_usb_adapter *adap;
692 int ret, i, adapter_count;
694 /* resolve adapter count */
695 adapter_count = d->props->num_adapters;
696 if (d->props->get_adapter_count) {
697 ret = d->props->get_adapter_count(d);
698 if (ret < 0)
699 goto err;
701 adapter_count = ret;
704 for (i = 0; i < adapter_count; i++) {
705 adap = &d->adapter[i];
706 adap->id = i;
707 adap->props = &d->props->adapter[i];
709 /* speed - when running at FULL speed we need a HW PID filter */
710 if (d->udev->speed == USB_SPEED_FULL &&
711 !(adap->props->caps & DVB_USB_ADAP_HAS_PID_FILTER)) {
712 dev_err(&d->udev->dev,
713 "%s: this USB2.0 device cannot be run on a USB1.1 port (it lacks a hardware PID filter)\n",
714 KBUILD_MODNAME);
715 ret = -ENODEV;
716 goto err;
717 } else if ((d->udev->speed == USB_SPEED_FULL &&
718 adap->props->caps & DVB_USB_ADAP_HAS_PID_FILTER) ||
719 (adap->props->caps & DVB_USB_ADAP_NEED_PID_FILTERING)) {
720 dev_info(&d->udev->dev,
721 "%s: will use the device's hardware PID filter (table count: %d)\n",
722 KBUILD_MODNAME,
723 adap->props->pid_filter_count);
724 adap->pid_filtering = 1;
725 adap->max_feed_count = adap->props->pid_filter_count;
726 } else {
727 dev_info(&d->udev->dev,
728 "%s: will pass the complete MPEG2 transport stream to the software demuxer\n",
729 KBUILD_MODNAME);
730 adap->pid_filtering = 0;
731 adap->max_feed_count = 255;
734 if (!adap->pid_filtering && dvb_usb_force_pid_filter_usage &&
735 adap->props->caps & DVB_USB_ADAP_HAS_PID_FILTER) {
736 dev_info(&d->udev->dev,
737 "%s: PID filter enabled by module option\n",
738 KBUILD_MODNAME);
739 adap->pid_filtering = 1;
740 adap->max_feed_count = adap->props->pid_filter_count;
743 ret = dvb_usbv2_adapter_stream_init(adap);
744 if (ret)
745 goto err;
747 ret = dvb_usbv2_adapter_dvb_init(adap);
748 if (ret)
749 goto err;
751 ret = dvb_usbv2_adapter_frontend_init(adap);
752 if (ret)
753 goto err;
755 /* use exclusive FE lock if there is multiple shared FEs */
756 if (adap->fe[1])
757 adap->dvb_adap.mfe_shared = 1;
760 return 0;
761 err:
762 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
763 return ret;
766 static int dvb_usbv2_adapter_exit(struct dvb_usb_device *d)
768 int i;
769 dev_dbg(&d->udev->dev, "%s:\n", __func__);
771 for (i = MAX_NO_OF_ADAPTER_PER_DEVICE - 1; i >= 0; i--) {
772 if (d->adapter[i].props) {
773 dvb_usbv2_adapter_frontend_exit(&d->adapter[i]);
774 dvb_usbv2_adapter_dvb_exit(&d->adapter[i]);
775 dvb_usbv2_adapter_stream_exit(&d->adapter[i]);
779 return 0;
782 /* general initialization functions */
783 static int dvb_usbv2_exit(struct dvb_usb_device *d)
785 dev_dbg(&d->udev->dev, "%s:\n", __func__);
787 dvb_usbv2_remote_exit(d);
788 dvb_usbv2_adapter_exit(d);
789 dvb_usbv2_i2c_exit(d);
790 kfree(d->priv);
791 kfree(d);
793 return 0;
796 static int dvb_usbv2_init(struct dvb_usb_device *d)
798 int ret;
799 dev_dbg(&d->udev->dev, "%s:\n", __func__);
801 dvb_usbv2_device_power_ctrl(d, 1);
803 if (d->props->read_config) {
804 ret = d->props->read_config(d);
805 if (ret < 0)
806 goto err;
809 ret = dvb_usbv2_i2c_init(d);
810 if (ret < 0)
811 goto err;
813 ret = dvb_usbv2_adapter_init(d);
814 if (ret < 0)
815 goto err;
817 if (d->props->init) {
818 ret = d->props->init(d);
819 if (ret < 0)
820 goto err;
823 ret = dvb_usbv2_remote_init(d);
824 if (ret < 0)
825 goto err;
827 dvb_usbv2_device_power_ctrl(d, 0);
829 return 0;
830 err:
831 dvb_usbv2_device_power_ctrl(d, 0);
832 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
833 return ret;
836 int dvb_usbv2_probe(struct usb_interface *intf,
837 const struct usb_device_id *id)
839 int ret;
840 struct dvb_usb_device *d;
841 struct usb_device *udev = interface_to_usbdev(intf);
842 struct dvb_usb_driver_info *driver_info =
843 (struct dvb_usb_driver_info *) id->driver_info;
845 dev_dbg(&udev->dev, "%s: bInterfaceNumber=%d\n", __func__,
846 intf->cur_altsetting->desc.bInterfaceNumber);
848 if (!id->driver_info) {
849 dev_err(&udev->dev, "%s: driver_info failed\n", KBUILD_MODNAME);
850 ret = -ENODEV;
851 goto err;
854 d = kzalloc(sizeof(struct dvb_usb_device), GFP_KERNEL);
855 if (!d) {
856 dev_err(&udev->dev, "%s: kzalloc() failed\n", KBUILD_MODNAME);
857 ret = -ENOMEM;
858 goto err;
861 d->name = driver_info->name;
862 d->rc_map = driver_info->rc_map;
863 d->udev = udev;
864 d->props = driver_info->props;
866 if (intf->cur_altsetting->desc.bInterfaceNumber !=
867 d->props->bInterfaceNumber) {
868 ret = -ENODEV;
869 goto err_free_all;
872 mutex_init(&d->usb_mutex);
873 mutex_init(&d->i2c_mutex);
875 if (d->props->size_of_priv) {
876 d->priv = kzalloc(d->props->size_of_priv, GFP_KERNEL);
877 if (!d->priv) {
878 dev_err(&d->udev->dev, "%s: kzalloc() failed\n",
879 KBUILD_MODNAME);
880 ret = -ENOMEM;
881 goto err_free_all;
885 if (d->props->identify_state) {
886 const char *name = NULL;
887 ret = d->props->identify_state(d, &name);
888 if (ret == 0) {
890 } else if (ret == COLD) {
891 dev_info(&d->udev->dev,
892 "%s: found a '%s' in cold state\n",
893 KBUILD_MODNAME, d->name);
895 if (!name)
896 name = d->props->firmware;
898 ret = dvb_usbv2_download_firmware(d, name);
899 if (ret == 0) {
900 /* device is warm, continue initialization */
902 } else if (ret == RECONNECTS_USB) {
904 * USB core will call disconnect() and then
905 * probe() as device reconnects itself from the
906 * USB bus. disconnect() will release all driver
907 * resources and probe() is called for 'new'
908 * device. As 'new' device is warm we should
909 * never go here again.
911 goto exit;
912 } else {
913 goto err_free_all;
915 } else {
916 goto err_free_all;
920 dev_info(&d->udev->dev, "%s: found a '%s' in warm state\n",
921 KBUILD_MODNAME, d->name);
923 ret = dvb_usbv2_init(d);
924 if (ret < 0)
925 goto err_free_all;
927 dev_info(&d->udev->dev,
928 "%s: '%s' successfully initialized and connected\n",
929 KBUILD_MODNAME, d->name);
930 exit:
931 usb_set_intfdata(intf, d);
933 return 0;
934 err_free_all:
935 dvb_usbv2_exit(d);
936 err:
937 dev_dbg(&udev->dev, "%s: failed=%d\n", __func__, ret);
938 return ret;
940 EXPORT_SYMBOL(dvb_usbv2_probe);
942 void dvb_usbv2_disconnect(struct usb_interface *intf)
944 struct dvb_usb_device *d = usb_get_intfdata(intf);
945 const char *name = d->name;
946 struct device dev = d->udev->dev;
947 dev_dbg(&d->udev->dev, "%s: bInterfaceNumber=%d\n", __func__,
948 intf->cur_altsetting->desc.bInterfaceNumber);
950 if (d->props->exit)
951 d->props->exit(d);
953 dvb_usbv2_exit(d);
955 dev_info(&dev, "%s: '%s' successfully deinitialized and disconnected\n",
956 KBUILD_MODNAME, name);
958 EXPORT_SYMBOL(dvb_usbv2_disconnect);
960 int dvb_usbv2_suspend(struct usb_interface *intf, pm_message_t msg)
962 struct dvb_usb_device *d = usb_get_intfdata(intf);
963 int ret = 0, i, active_fe;
964 struct dvb_frontend *fe;
965 dev_dbg(&d->udev->dev, "%s:\n", __func__);
967 /* stop remote controller poll */
968 if (d->rc_polling_active)
969 cancel_delayed_work_sync(&d->rc_query_work);
971 for (i = MAX_NO_OF_ADAPTER_PER_DEVICE - 1; i >= 0; i--) {
972 active_fe = d->adapter[i].active_fe;
973 if (d->adapter[i].dvb_adap.priv && active_fe != -1) {
974 fe = d->adapter[i].fe[active_fe];
975 d->adapter[i].suspend_resume_active = true;
977 if (d->props->streaming_ctrl)
978 d->props->streaming_ctrl(fe, 0);
980 /* stop usb streaming */
981 usb_urb_killv2(&d->adapter[i].stream);
983 ret = dvb_frontend_suspend(fe);
987 return ret;
989 EXPORT_SYMBOL(dvb_usbv2_suspend);
991 static int dvb_usbv2_resume_common(struct dvb_usb_device *d)
993 int ret = 0, i, active_fe;
994 struct dvb_frontend *fe;
995 dev_dbg(&d->udev->dev, "%s:\n", __func__);
997 for (i = 0; i < MAX_NO_OF_ADAPTER_PER_DEVICE; i++) {
998 active_fe = d->adapter[i].active_fe;
999 if (d->adapter[i].dvb_adap.priv && active_fe != -1) {
1000 fe = d->adapter[i].fe[active_fe];
1002 ret = dvb_frontend_resume(fe);
1004 /* resume usb streaming */
1005 usb_urb_submitv2(&d->adapter[i].stream, NULL);
1007 if (d->props->streaming_ctrl)
1008 d->props->streaming_ctrl(fe, 1);
1010 d->adapter[i].suspend_resume_active = false;
1014 /* start remote controller poll */
1015 if (d->rc_polling_active)
1016 schedule_delayed_work(&d->rc_query_work,
1017 msecs_to_jiffies(d->rc.interval));
1019 return ret;
1022 int dvb_usbv2_resume(struct usb_interface *intf)
1024 struct dvb_usb_device *d = usb_get_intfdata(intf);
1025 dev_dbg(&d->udev->dev, "%s:\n", __func__);
1027 return dvb_usbv2_resume_common(d);
1029 EXPORT_SYMBOL(dvb_usbv2_resume);
1031 int dvb_usbv2_reset_resume(struct usb_interface *intf)
1033 struct dvb_usb_device *d = usb_get_intfdata(intf);
1034 int ret;
1035 dev_dbg(&d->udev->dev, "%s:\n", __func__);
1037 dvb_usbv2_device_power_ctrl(d, 1);
1039 if (d->props->init)
1040 d->props->init(d);
1042 ret = dvb_usbv2_resume_common(d);
1044 dvb_usbv2_device_power_ctrl(d, 0);
1046 return ret;
1048 EXPORT_SYMBOL(dvb_usbv2_reset_resume);
1050 MODULE_VERSION("2.0");
1051 MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@desy.de>");
1052 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1053 MODULE_DESCRIPTION("DVB USB common");
1054 MODULE_LICENSE("GPL");