sched: Remove double_rq_lock() from __migrate_task()
[linux/fpc-iii.git] / drivers / media / usb / dvb-usb-v2 / dvb_usb_core.c
blobf296394bb7c50b8ce9a66c1626eb2e14f9419897
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 dvb_usb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
258 struct dvb_usb_adapter *adap = dvbdmxfeed->demux->priv;
259 struct dvb_usb_device *d = adap_to_d(adap);
260 int ret = 0;
261 struct usb_data_stream_properties stream_props;
262 dev_dbg(&d->udev->dev,
263 "%s: adap=%d active_fe=%d feed_type=%d setting pid [%s]: %04x (%04d) at index %d\n",
264 __func__, adap->id, adap->active_fe, dvbdmxfeed->type,
265 adap->pid_filtering ? "yes" : "no", dvbdmxfeed->pid,
266 dvbdmxfeed->pid, dvbdmxfeed->index);
268 /* wait init is done */
269 wait_on_bit(&adap->state_bits, ADAP_INIT, TASK_UNINTERRUPTIBLE);
271 if (adap->active_fe == -1)
272 return -EINVAL;
274 /* skip feed setup if we are already feeding */
275 if (adap->feed_count++ > 0)
276 goto skip_feed_start;
278 /* set 'streaming' status bit */
279 set_bit(ADAP_STREAMING, &adap->state_bits);
281 /* resolve input and output streaming parameters */
282 if (d->props->get_stream_config) {
283 memcpy(&stream_props, &adap->props->stream,
284 sizeof(struct usb_data_stream_properties));
285 ret = d->props->get_stream_config(adap->fe[adap->active_fe],
286 &adap->ts_type, &stream_props);
287 if (ret)
288 dev_err(&d->udev->dev,
289 "%s: get_stream_config() failed=%d\n",
290 KBUILD_MODNAME, ret);
291 } else {
292 stream_props = adap->props->stream;
295 switch (adap->ts_type) {
296 case DVB_USB_FE_TS_TYPE_204:
297 adap->stream.complete = dvb_usb_data_complete_204;
298 break;
299 case DVB_USB_FE_TS_TYPE_RAW:
300 adap->stream.complete = dvb_usb_data_complete_raw;
301 break;
302 case DVB_USB_FE_TS_TYPE_188:
303 default:
304 adap->stream.complete = dvb_usb_data_complete;
305 break;
308 /* submit USB streaming packets */
309 usb_urb_submitv2(&adap->stream, &stream_props);
311 /* enable HW PID filter */
312 if (adap->pid_filtering && adap->props->pid_filter_ctrl) {
313 ret = adap->props->pid_filter_ctrl(adap, 1);
314 if (ret)
315 dev_err(&d->udev->dev,
316 "%s: pid_filter_ctrl() failed=%d\n",
317 KBUILD_MODNAME, ret);
320 /* ask device to start streaming */
321 if (d->props->streaming_ctrl) {
322 ret = d->props->streaming_ctrl(adap->fe[adap->active_fe], 1);
323 if (ret)
324 dev_err(&d->udev->dev,
325 "%s: streaming_ctrl() failed=%d\n",
326 KBUILD_MODNAME, ret);
328 skip_feed_start:
330 /* add PID to device HW PID filter */
331 if (adap->pid_filtering && adap->props->pid_filter) {
332 ret = adap->props->pid_filter(adap, dvbdmxfeed->index,
333 dvbdmxfeed->pid, 1);
334 if (ret)
335 dev_err(&d->udev->dev, "%s: pid_filter() failed=%d\n",
336 KBUILD_MODNAME, ret);
339 if (ret)
340 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
341 return ret;
344 static int dvb_usb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
346 struct dvb_usb_adapter *adap = dvbdmxfeed->demux->priv;
347 struct dvb_usb_device *d = adap_to_d(adap);
348 int ret = 0;
349 dev_dbg(&d->udev->dev,
350 "%s: adap=%d active_fe=%d feed_type=%d setting pid [%s]: %04x (%04d) at index %d\n",
351 __func__, adap->id, adap->active_fe, dvbdmxfeed->type,
352 adap->pid_filtering ? "yes" : "no", dvbdmxfeed->pid,
353 dvbdmxfeed->pid, dvbdmxfeed->index);
355 if (adap->active_fe == -1)
356 return -EINVAL;
358 /* remove PID from device HW PID filter */
359 if (adap->pid_filtering && adap->props->pid_filter) {
360 ret = adap->props->pid_filter(adap, dvbdmxfeed->index,
361 dvbdmxfeed->pid, 0);
362 if (ret)
363 dev_err(&d->udev->dev, "%s: pid_filter() failed=%d\n",
364 KBUILD_MODNAME, ret);
367 /* we cannot stop streaming until last PID is removed */
368 if (--adap->feed_count > 0)
369 goto skip_feed_stop;
371 /* ask device to stop streaming */
372 if (d->props->streaming_ctrl) {
373 ret = d->props->streaming_ctrl(adap->fe[adap->active_fe], 0);
374 if (ret)
375 dev_err(&d->udev->dev,
376 "%s: streaming_ctrl() failed=%d\n",
377 KBUILD_MODNAME, ret);
380 /* disable HW PID filter */
381 if (adap->pid_filtering && adap->props->pid_filter_ctrl) {
382 ret = adap->props->pid_filter_ctrl(adap, 0);
383 if (ret)
384 dev_err(&d->udev->dev,
385 "%s: pid_filter_ctrl() failed=%d\n",
386 KBUILD_MODNAME, ret);
389 /* kill USB streaming packets */
390 usb_urb_killv2(&adap->stream);
392 /* clear 'streaming' status bit */
393 clear_bit(ADAP_STREAMING, &adap->state_bits);
394 smp_mb__after_atomic();
395 wake_up_bit(&adap->state_bits, ADAP_STREAMING);
396 skip_feed_stop:
398 if (ret)
399 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
400 return ret;
403 static int dvb_usbv2_adapter_dvb_init(struct dvb_usb_adapter *adap)
405 int ret;
406 struct dvb_usb_device *d = adap_to_d(adap);
407 dev_dbg(&d->udev->dev, "%s: adap=%d\n", __func__, adap->id);
409 ret = dvb_register_adapter(&adap->dvb_adap, d->name, d->props->owner,
410 &d->udev->dev, d->props->adapter_nr);
411 if (ret < 0) {
412 dev_dbg(&d->udev->dev, "%s: dvb_register_adapter() failed=%d\n",
413 __func__, ret);
414 goto err_dvb_register_adapter;
417 adap->dvb_adap.priv = adap;
419 if (d->props->read_mac_address) {
420 ret = d->props->read_mac_address(adap,
421 adap->dvb_adap.proposed_mac);
422 if (ret < 0)
423 goto err_dvb_dmx_init;
425 dev_info(&d->udev->dev, "%s: MAC address: %pM\n",
426 KBUILD_MODNAME, adap->dvb_adap.proposed_mac);
429 adap->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING;
430 adap->demux.priv = adap;
431 adap->demux.filternum = 0;
432 adap->demux.filternum = adap->max_feed_count;
433 adap->demux.feednum = adap->demux.filternum;
434 adap->demux.start_feed = dvb_usb_start_feed;
435 adap->demux.stop_feed = dvb_usb_stop_feed;
436 adap->demux.write_to_decoder = NULL;
437 ret = dvb_dmx_init(&adap->demux);
438 if (ret < 0) {
439 dev_err(&d->udev->dev, "%s: dvb_dmx_init() failed=%d\n",
440 KBUILD_MODNAME, ret);
441 goto err_dvb_dmx_init;
444 adap->dmxdev.filternum = adap->demux.filternum;
445 adap->dmxdev.demux = &adap->demux.dmx;
446 adap->dmxdev.capabilities = 0;
447 ret = dvb_dmxdev_init(&adap->dmxdev, &adap->dvb_adap);
448 if (ret < 0) {
449 dev_err(&d->udev->dev, "%s: dvb_dmxdev_init() failed=%d\n",
450 KBUILD_MODNAME, ret);
451 goto err_dvb_dmxdev_init;
454 ret = dvb_net_init(&adap->dvb_adap, &adap->dvb_net, &adap->demux.dmx);
455 if (ret < 0) {
456 dev_err(&d->udev->dev, "%s: dvb_net_init() failed=%d\n",
457 KBUILD_MODNAME, ret);
458 goto err_dvb_net_init;
461 return 0;
462 err_dvb_net_init:
463 dvb_dmxdev_release(&adap->dmxdev);
464 err_dvb_dmxdev_init:
465 dvb_dmx_release(&adap->demux);
466 err_dvb_dmx_init:
467 dvb_unregister_adapter(&adap->dvb_adap);
468 err_dvb_register_adapter:
469 adap->dvb_adap.priv = NULL;
470 return ret;
473 static int dvb_usbv2_adapter_dvb_exit(struct dvb_usb_adapter *adap)
475 dev_dbg(&adap_to_d(adap)->udev->dev, "%s: adap=%d\n", __func__,
476 adap->id);
478 if (adap->dvb_adap.priv) {
479 dvb_net_release(&adap->dvb_net);
480 adap->demux.dmx.close(&adap->demux.dmx);
481 dvb_dmxdev_release(&adap->dmxdev);
482 dvb_dmx_release(&adap->demux);
483 dvb_unregister_adapter(&adap->dvb_adap);
486 return 0;
489 static int dvb_usbv2_device_power_ctrl(struct dvb_usb_device *d, int onoff)
491 int ret;
493 if (onoff)
494 d->powered++;
495 else
496 d->powered--;
498 if (d->powered == 0 || (onoff && d->powered == 1)) {
499 /* when switching from 1 to 0 or from 0 to 1 */
500 dev_dbg(&d->udev->dev, "%s: power=%d\n", __func__, onoff);
501 if (d->props->power_ctrl) {
502 ret = d->props->power_ctrl(d, onoff);
503 if (ret < 0)
504 goto err;
508 return 0;
509 err:
510 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
511 return ret;
514 static int dvb_usb_fe_init(struct dvb_frontend *fe)
516 int ret;
517 struct dvb_usb_adapter *adap = fe->dvb->priv;
518 struct dvb_usb_device *d = adap_to_d(adap);
519 dev_dbg(&d->udev->dev, "%s: adap=%d fe=%d\n", __func__, adap->id,
520 fe->id);
522 if (!adap->suspend_resume_active) {
523 adap->active_fe = fe->id;
524 set_bit(ADAP_INIT, &adap->state_bits);
527 ret = dvb_usbv2_device_power_ctrl(d, 1);
528 if (ret < 0)
529 goto err;
531 if (d->props->frontend_ctrl) {
532 ret = d->props->frontend_ctrl(fe, 1);
533 if (ret < 0)
534 goto err;
537 if (adap->fe_init[fe->id]) {
538 ret = adap->fe_init[fe->id](fe);
539 if (ret < 0)
540 goto err;
542 err:
543 if (!adap->suspend_resume_active) {
544 clear_bit(ADAP_INIT, &adap->state_bits);
545 smp_mb__after_atomic();
546 wake_up_bit(&adap->state_bits, ADAP_INIT);
549 dev_dbg(&d->udev->dev, "%s: ret=%d\n", __func__, ret);
550 return ret;
553 static int dvb_usb_fe_sleep(struct dvb_frontend *fe)
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);
561 if (!adap->suspend_resume_active) {
562 set_bit(ADAP_SLEEP, &adap->state_bits);
563 wait_on_bit(&adap->state_bits, ADAP_STREAMING,
564 TASK_UNINTERRUPTIBLE);
567 if (adap->fe_sleep[fe->id]) {
568 ret = adap->fe_sleep[fe->id](fe);
569 if (ret < 0)
570 goto err;
573 if (d->props->frontend_ctrl) {
574 ret = d->props->frontend_ctrl(fe, 0);
575 if (ret < 0)
576 goto err;
579 ret = dvb_usbv2_device_power_ctrl(d, 0);
580 if (ret < 0)
581 goto err;
582 err:
583 if (!adap->suspend_resume_active) {
584 adap->active_fe = -1;
585 clear_bit(ADAP_SLEEP, &adap->state_bits);
586 smp_mb__after_atomic();
587 wake_up_bit(&adap->state_bits, ADAP_SLEEP);
590 dev_dbg(&d->udev->dev, "%s: ret=%d\n", __func__, ret);
591 return ret;
594 static int dvb_usbv2_adapter_frontend_init(struct dvb_usb_adapter *adap)
596 int ret, i, count_registered = 0;
597 struct dvb_usb_device *d = adap_to_d(adap);
598 dev_dbg(&d->udev->dev, "%s: adap=%d\n", __func__, adap->id);
600 memset(adap->fe, 0, sizeof(adap->fe));
601 adap->active_fe = -1;
603 if (d->props->frontend_attach) {
604 ret = d->props->frontend_attach(adap);
605 if (ret < 0) {
606 dev_dbg(&d->udev->dev,
607 "%s: frontend_attach() failed=%d\n",
608 __func__, ret);
609 goto err_dvb_frontend_detach;
611 } else {
612 dev_dbg(&d->udev->dev, "%s: frontend_attach() do not exists\n",
613 __func__);
614 ret = 0;
615 goto err;
618 for (i = 0; i < MAX_NO_OF_FE_PER_ADAP && adap->fe[i]; i++) {
619 adap->fe[i]->id = i;
620 /* re-assign sleep and wakeup functions */
621 adap->fe_init[i] = adap->fe[i]->ops.init;
622 adap->fe[i]->ops.init = dvb_usb_fe_init;
623 adap->fe_sleep[i] = adap->fe[i]->ops.sleep;
624 adap->fe[i]->ops.sleep = dvb_usb_fe_sleep;
626 ret = dvb_register_frontend(&adap->dvb_adap, adap->fe[i]);
627 if (ret < 0) {
628 dev_err(&d->udev->dev,
629 "%s: frontend%d registration failed\n",
630 KBUILD_MODNAME, i);
631 goto err_dvb_unregister_frontend;
634 count_registered++;
637 if (d->props->tuner_attach) {
638 ret = d->props->tuner_attach(adap);
639 if (ret < 0) {
640 dev_dbg(&d->udev->dev, "%s: tuner_attach() failed=%d\n",
641 __func__, ret);
642 goto err_dvb_unregister_frontend;
646 return 0;
648 err_dvb_unregister_frontend:
649 for (i = count_registered - 1; i >= 0; i--)
650 dvb_unregister_frontend(adap->fe[i]);
652 err_dvb_frontend_detach:
653 for (i = MAX_NO_OF_FE_PER_ADAP - 1; i >= 0; i--) {
654 if (adap->fe[i]) {
655 dvb_frontend_detach(adap->fe[i]);
656 adap->fe[i] = NULL;
660 err:
661 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
662 return ret;
665 static int dvb_usbv2_adapter_frontend_exit(struct dvb_usb_adapter *adap)
667 int i;
668 dev_dbg(&adap_to_d(adap)->udev->dev, "%s: adap=%d\n", __func__,
669 adap->id);
671 for (i = MAX_NO_OF_FE_PER_ADAP - 1; i >= 0; i--) {
672 if (adap->fe[i]) {
673 dvb_unregister_frontend(adap->fe[i]);
674 dvb_frontend_detach(adap->fe[i]);
678 return 0;
681 static int dvb_usbv2_adapter_init(struct dvb_usb_device *d)
683 struct dvb_usb_adapter *adap;
684 int ret, i, adapter_count;
686 /* resolve adapter count */
687 adapter_count = d->props->num_adapters;
688 if (d->props->get_adapter_count) {
689 ret = d->props->get_adapter_count(d);
690 if (ret < 0)
691 goto err;
693 adapter_count = ret;
696 for (i = 0; i < adapter_count; i++) {
697 adap = &d->adapter[i];
698 adap->id = i;
699 adap->props = &d->props->adapter[i];
701 /* speed - when running at FULL speed we need a HW PID filter */
702 if (d->udev->speed == USB_SPEED_FULL &&
703 !(adap->props->caps & DVB_USB_ADAP_HAS_PID_FILTER)) {
704 dev_err(&d->udev->dev,
705 "%s: this USB2.0 device cannot be run on a USB1.1 port (it lacks a hardware PID filter)\n",
706 KBUILD_MODNAME);
707 ret = -ENODEV;
708 goto err;
709 } else if ((d->udev->speed == USB_SPEED_FULL &&
710 adap->props->caps & DVB_USB_ADAP_HAS_PID_FILTER) ||
711 (adap->props->caps & DVB_USB_ADAP_NEED_PID_FILTERING)) {
712 dev_info(&d->udev->dev,
713 "%s: will use the device's hardware PID filter (table count: %d)\n",
714 KBUILD_MODNAME,
715 adap->props->pid_filter_count);
716 adap->pid_filtering = 1;
717 adap->max_feed_count = adap->props->pid_filter_count;
718 } else {
719 dev_info(&d->udev->dev,
720 "%s: will pass the complete MPEG2 transport stream to the software demuxer\n",
721 KBUILD_MODNAME);
722 adap->pid_filtering = 0;
723 adap->max_feed_count = 255;
726 if (!adap->pid_filtering && dvb_usb_force_pid_filter_usage &&
727 adap->props->caps & DVB_USB_ADAP_HAS_PID_FILTER) {
728 dev_info(&d->udev->dev,
729 "%s: PID filter enabled by module option\n",
730 KBUILD_MODNAME);
731 adap->pid_filtering = 1;
732 adap->max_feed_count = adap->props->pid_filter_count;
735 ret = dvb_usbv2_adapter_stream_init(adap);
736 if (ret)
737 goto err;
739 ret = dvb_usbv2_adapter_dvb_init(adap);
740 if (ret)
741 goto err;
743 ret = dvb_usbv2_adapter_frontend_init(adap);
744 if (ret)
745 goto err;
747 /* use exclusive FE lock if there is multiple shared FEs */
748 if (adap->fe[1])
749 adap->dvb_adap.mfe_shared = 1;
752 return 0;
753 err:
754 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
755 return ret;
758 static int dvb_usbv2_adapter_exit(struct dvb_usb_device *d)
760 int i;
761 dev_dbg(&d->udev->dev, "%s:\n", __func__);
763 for (i = MAX_NO_OF_ADAPTER_PER_DEVICE - 1; i >= 0; i--) {
764 if (d->adapter[i].props) {
765 dvb_usbv2_adapter_frontend_exit(&d->adapter[i]);
766 dvb_usbv2_adapter_dvb_exit(&d->adapter[i]);
767 dvb_usbv2_adapter_stream_exit(&d->adapter[i]);
771 return 0;
774 /* general initialization functions */
775 static int dvb_usbv2_exit(struct dvb_usb_device *d)
777 dev_dbg(&d->udev->dev, "%s:\n", __func__);
779 dvb_usbv2_remote_exit(d);
780 dvb_usbv2_adapter_exit(d);
781 dvb_usbv2_i2c_exit(d);
782 kfree(d->priv);
783 kfree(d);
785 return 0;
788 static int dvb_usbv2_init(struct dvb_usb_device *d)
790 int ret;
791 dev_dbg(&d->udev->dev, "%s:\n", __func__);
793 dvb_usbv2_device_power_ctrl(d, 1);
795 if (d->props->read_config) {
796 ret = d->props->read_config(d);
797 if (ret < 0)
798 goto err;
801 ret = dvb_usbv2_i2c_init(d);
802 if (ret < 0)
803 goto err;
805 ret = dvb_usbv2_adapter_init(d);
806 if (ret < 0)
807 goto err;
809 if (d->props->init) {
810 ret = d->props->init(d);
811 if (ret < 0)
812 goto err;
815 ret = dvb_usbv2_remote_init(d);
816 if (ret < 0)
817 goto err;
819 dvb_usbv2_device_power_ctrl(d, 0);
821 return 0;
822 err:
823 dvb_usbv2_device_power_ctrl(d, 0);
824 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
825 return ret;
828 int dvb_usbv2_probe(struct usb_interface *intf,
829 const struct usb_device_id *id)
831 int ret;
832 struct dvb_usb_device *d;
833 struct usb_device *udev = interface_to_usbdev(intf);
834 struct dvb_usb_driver_info *driver_info =
835 (struct dvb_usb_driver_info *) id->driver_info;
837 dev_dbg(&udev->dev, "%s: bInterfaceNumber=%d\n", __func__,
838 intf->cur_altsetting->desc.bInterfaceNumber);
840 if (!id->driver_info) {
841 dev_err(&udev->dev, "%s: driver_info failed\n", KBUILD_MODNAME);
842 ret = -ENODEV;
843 goto err;
846 d = kzalloc(sizeof(struct dvb_usb_device), GFP_KERNEL);
847 if (!d) {
848 dev_err(&udev->dev, "%s: kzalloc() failed\n", KBUILD_MODNAME);
849 ret = -ENOMEM;
850 goto err;
853 d->name = driver_info->name;
854 d->rc_map = driver_info->rc_map;
855 d->udev = udev;
856 d->props = driver_info->props;
858 if (intf->cur_altsetting->desc.bInterfaceNumber !=
859 d->props->bInterfaceNumber) {
860 ret = -ENODEV;
861 goto err_free_all;
864 mutex_init(&d->usb_mutex);
865 mutex_init(&d->i2c_mutex);
867 if (d->props->size_of_priv) {
868 d->priv = kzalloc(d->props->size_of_priv, GFP_KERNEL);
869 if (!d->priv) {
870 dev_err(&d->udev->dev, "%s: kzalloc() failed\n",
871 KBUILD_MODNAME);
872 ret = -ENOMEM;
873 goto err_free_all;
877 if (d->props->identify_state) {
878 const char *name = NULL;
879 ret = d->props->identify_state(d, &name);
880 if (ret == 0) {
882 } else if (ret == COLD) {
883 dev_info(&d->udev->dev,
884 "%s: found a '%s' in cold state\n",
885 KBUILD_MODNAME, d->name);
887 if (!name)
888 name = d->props->firmware;
890 ret = dvb_usbv2_download_firmware(d, name);
891 if (ret == 0) {
892 /* device is warm, continue initialization */
894 } else if (ret == RECONNECTS_USB) {
896 * USB core will call disconnect() and then
897 * probe() as device reconnects itself from the
898 * USB bus. disconnect() will release all driver
899 * resources and probe() is called for 'new'
900 * device. As 'new' device is warm we should
901 * never go here again.
903 goto exit;
904 } else {
905 goto err_free_all;
907 } else {
908 goto err_free_all;
912 dev_info(&d->udev->dev, "%s: found a '%s' in warm state\n",
913 KBUILD_MODNAME, d->name);
915 ret = dvb_usbv2_init(d);
916 if (ret < 0)
917 goto err_free_all;
919 dev_info(&d->udev->dev,
920 "%s: '%s' successfully initialized and connected\n",
921 KBUILD_MODNAME, d->name);
922 exit:
923 usb_set_intfdata(intf, d);
925 return 0;
926 err_free_all:
927 dvb_usbv2_exit(d);
928 err:
929 dev_dbg(&udev->dev, "%s: failed=%d\n", __func__, ret);
930 return ret;
932 EXPORT_SYMBOL(dvb_usbv2_probe);
934 void dvb_usbv2_disconnect(struct usb_interface *intf)
936 struct dvb_usb_device *d = usb_get_intfdata(intf);
937 const char *name = d->name;
938 struct device dev = d->udev->dev;
939 dev_dbg(&d->udev->dev, "%s: bInterfaceNumber=%d\n", __func__,
940 intf->cur_altsetting->desc.bInterfaceNumber);
942 if (d->props->exit)
943 d->props->exit(d);
945 dvb_usbv2_exit(d);
947 dev_info(&dev, "%s: '%s' successfully deinitialized and disconnected\n",
948 KBUILD_MODNAME, name);
950 EXPORT_SYMBOL(dvb_usbv2_disconnect);
952 int dvb_usbv2_suspend(struct usb_interface *intf, pm_message_t msg)
954 struct dvb_usb_device *d = usb_get_intfdata(intf);
955 int ret = 0, i, active_fe;
956 struct dvb_frontend *fe;
957 dev_dbg(&d->udev->dev, "%s:\n", __func__);
959 /* stop remote controller poll */
960 if (d->rc_polling_active)
961 cancel_delayed_work_sync(&d->rc_query_work);
963 for (i = MAX_NO_OF_ADAPTER_PER_DEVICE - 1; i >= 0; i--) {
964 active_fe = d->adapter[i].active_fe;
965 if (d->adapter[i].dvb_adap.priv && active_fe != -1) {
966 fe = d->adapter[i].fe[active_fe];
967 d->adapter[i].suspend_resume_active = true;
969 if (d->props->streaming_ctrl)
970 d->props->streaming_ctrl(fe, 0);
972 /* stop usb streaming */
973 usb_urb_killv2(&d->adapter[i].stream);
975 ret = dvb_frontend_suspend(fe);
979 return ret;
981 EXPORT_SYMBOL(dvb_usbv2_suspend);
983 static int dvb_usbv2_resume_common(struct dvb_usb_device *d)
985 int ret = 0, i, active_fe;
986 struct dvb_frontend *fe;
987 dev_dbg(&d->udev->dev, "%s:\n", __func__);
989 for (i = 0; i < MAX_NO_OF_ADAPTER_PER_DEVICE; i++) {
990 active_fe = d->adapter[i].active_fe;
991 if (d->adapter[i].dvb_adap.priv && active_fe != -1) {
992 fe = d->adapter[i].fe[active_fe];
994 ret = dvb_frontend_resume(fe);
996 /* resume usb streaming */
997 usb_urb_submitv2(&d->adapter[i].stream, NULL);
999 if (d->props->streaming_ctrl)
1000 d->props->streaming_ctrl(fe, 1);
1002 d->adapter[i].suspend_resume_active = false;
1006 /* start remote controller poll */
1007 if (d->rc_polling_active)
1008 schedule_delayed_work(&d->rc_query_work,
1009 msecs_to_jiffies(d->rc.interval));
1011 return ret;
1014 int dvb_usbv2_resume(struct usb_interface *intf)
1016 struct dvb_usb_device *d = usb_get_intfdata(intf);
1017 dev_dbg(&d->udev->dev, "%s:\n", __func__);
1019 return dvb_usbv2_resume_common(d);
1021 EXPORT_SYMBOL(dvb_usbv2_resume);
1023 int dvb_usbv2_reset_resume(struct usb_interface *intf)
1025 struct dvb_usb_device *d = usb_get_intfdata(intf);
1026 int ret;
1027 dev_dbg(&d->udev->dev, "%s:\n", __func__);
1029 dvb_usbv2_device_power_ctrl(d, 1);
1031 if (d->props->init)
1032 d->props->init(d);
1034 ret = dvb_usbv2_resume_common(d);
1036 dvb_usbv2_device_power_ctrl(d, 0);
1038 return ret;
1040 EXPORT_SYMBOL(dvb_usbv2_reset_resume);
1042 MODULE_VERSION("2.0");
1043 MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@desy.de>");
1044 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1045 MODULE_DESCRIPTION("DVB USB common");
1046 MODULE_LICENSE("GPL");