1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright (C) 2004-6 Patrick Boettcher <patrick.boettcher@posteo.de>
6 * Copyright (C) 2012 Antti Palosaari <crope@iki.fi>
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
;
153 dev
->timeout
= d
->rc
.timeout
;
156 ret
= rc_register_device(dev
);
164 /* start polling if needed */
165 if (d
->rc
.query
&& !d
->rc
.bulk_mode
) {
166 /* initialize a work queue for handling polling */
167 INIT_DELAYED_WORK(&d
->rc_query_work
,
168 dvb_usb_read_remote_control
);
169 dev_info(&d
->udev
->dev
,
170 "%s: schedule remote query interval to %d msecs\n",
171 KBUILD_MODNAME
, d
->rc
.interval
);
172 schedule_delayed_work(&d
->rc_query_work
,
173 msecs_to_jiffies(d
->rc
.interval
));
174 d
->rc_polling_active
= true;
179 dev_dbg(&d
->udev
->dev
, "%s: failed=%d\n", __func__
, ret
);
183 static int dvb_usbv2_remote_exit(struct dvb_usb_device
*d
)
185 dev_dbg(&d
->udev
->dev
, "%s:\n", __func__
);
188 cancel_delayed_work_sync(&d
->rc_query_work
);
189 rc_unregister_device(d
->rc_dev
);
196 #define dvb_usbv2_remote_init(args...) 0
197 #define dvb_usbv2_remote_exit(args...)
200 static void dvb_usb_data_complete(struct usb_data_stream
*stream
, u8
*buf
,
203 struct dvb_usb_adapter
*adap
= stream
->user_priv
;
204 dvb_dmx_swfilter(&adap
->demux
, buf
, len
);
207 static void dvb_usb_data_complete_204(struct usb_data_stream
*stream
, u8
*buf
,
210 struct dvb_usb_adapter
*adap
= stream
->user_priv
;
211 dvb_dmx_swfilter_204(&adap
->demux
, buf
, len
);
214 static void dvb_usb_data_complete_raw(struct usb_data_stream
*stream
, u8
*buf
,
217 struct dvb_usb_adapter
*adap
= stream
->user_priv
;
218 dvb_dmx_swfilter_raw(&adap
->demux
, buf
, len
);
221 static int dvb_usbv2_adapter_stream_init(struct dvb_usb_adapter
*adap
)
223 dev_dbg(&adap_to_d(adap
)->udev
->dev
, "%s: adap=%d\n", __func__
,
226 adap
->stream
.udev
= adap_to_d(adap
)->udev
;
227 adap
->stream
.user_priv
= adap
;
228 adap
->stream
.complete
= dvb_usb_data_complete
;
230 return usb_urb_initv2(&adap
->stream
, &adap
->props
->stream
);
233 static int dvb_usbv2_adapter_stream_exit(struct dvb_usb_adapter
*adap
)
235 dev_dbg(&adap_to_d(adap
)->udev
->dev
, "%s: adap=%d\n", __func__
,
238 return usb_urb_exitv2(&adap
->stream
);
241 static int dvb_usb_start_feed(struct dvb_demux_feed
*dvbdmxfeed
)
243 struct dvb_usb_adapter
*adap
= dvbdmxfeed
->demux
->priv
;
244 struct dvb_usb_device
*d
= adap_to_d(adap
);
246 struct usb_data_stream_properties stream_props
;
247 dev_dbg(&d
->udev
->dev
,
248 "%s: adap=%d active_fe=%d feed_type=%d setting pid [%s]: %04x (%04d) at index %d\n",
249 __func__
, adap
->id
, adap
->active_fe
, dvbdmxfeed
->type
,
250 adap
->pid_filtering
? "yes" : "no", dvbdmxfeed
->pid
,
251 dvbdmxfeed
->pid
, dvbdmxfeed
->index
);
253 /* wait init is done */
254 wait_on_bit(&adap
->state_bits
, ADAP_INIT
, TASK_UNINTERRUPTIBLE
);
256 if (adap
->active_fe
== -1)
259 /* skip feed setup if we are already feeding */
260 if (adap
->feed_count
++ > 0)
261 goto skip_feed_start
;
263 /* set 'streaming' status bit */
264 set_bit(ADAP_STREAMING
, &adap
->state_bits
);
266 /* resolve input and output streaming parameters */
267 if (d
->props
->get_stream_config
) {
268 memcpy(&stream_props
, &adap
->props
->stream
,
269 sizeof(struct usb_data_stream_properties
));
270 ret
= d
->props
->get_stream_config(adap
->fe
[adap
->active_fe
],
271 &adap
->ts_type
, &stream_props
);
273 dev_err(&d
->udev
->dev
,
274 "%s: get_stream_config() failed=%d\n",
275 KBUILD_MODNAME
, ret
);
277 stream_props
= adap
->props
->stream
;
280 switch (adap
->ts_type
) {
281 case DVB_USB_FE_TS_TYPE_204
:
282 adap
->stream
.complete
= dvb_usb_data_complete_204
;
284 case DVB_USB_FE_TS_TYPE_RAW
:
285 adap
->stream
.complete
= dvb_usb_data_complete_raw
;
287 case DVB_USB_FE_TS_TYPE_188
:
289 adap
->stream
.complete
= dvb_usb_data_complete
;
293 /* submit USB streaming packets */
294 usb_urb_submitv2(&adap
->stream
, &stream_props
);
296 /* enable HW PID filter */
297 if (adap
->pid_filtering
&& adap
->props
->pid_filter_ctrl
) {
298 ret
= adap
->props
->pid_filter_ctrl(adap
, 1);
300 dev_err(&d
->udev
->dev
,
301 "%s: pid_filter_ctrl() failed=%d\n",
302 KBUILD_MODNAME
, ret
);
305 /* ask device to start streaming */
306 if (d
->props
->streaming_ctrl
) {
307 ret
= d
->props
->streaming_ctrl(adap
->fe
[adap
->active_fe
], 1);
309 dev_err(&d
->udev
->dev
,
310 "%s: streaming_ctrl() failed=%d\n",
311 KBUILD_MODNAME
, ret
);
315 /* add PID to device HW PID filter */
316 if (adap
->pid_filtering
&& adap
->props
->pid_filter
) {
317 ret
= adap
->props
->pid_filter(adap
, dvbdmxfeed
->index
,
320 dev_err(&d
->udev
->dev
, "%s: pid_filter() failed=%d\n",
321 KBUILD_MODNAME
, ret
);
325 dev_dbg(&d
->udev
->dev
, "%s: failed=%d\n", __func__
, ret
);
329 static int dvb_usb_stop_feed(struct dvb_demux_feed
*dvbdmxfeed
)
331 struct dvb_usb_adapter
*adap
= dvbdmxfeed
->demux
->priv
;
332 struct dvb_usb_device
*d
= adap_to_d(adap
);
334 dev_dbg(&d
->udev
->dev
,
335 "%s: adap=%d active_fe=%d feed_type=%d setting pid [%s]: %04x (%04d) at index %d\n",
336 __func__
, adap
->id
, adap
->active_fe
, dvbdmxfeed
->type
,
337 adap
->pid_filtering
? "yes" : "no", dvbdmxfeed
->pid
,
338 dvbdmxfeed
->pid
, dvbdmxfeed
->index
);
340 if (adap
->active_fe
== -1)
343 /* remove PID from device HW PID filter */
344 if (adap
->pid_filtering
&& adap
->props
->pid_filter
) {
345 ret
= adap
->props
->pid_filter(adap
, dvbdmxfeed
->index
,
348 dev_err(&d
->udev
->dev
, "%s: pid_filter() failed=%d\n",
349 KBUILD_MODNAME
, ret
);
352 /* we cannot stop streaming until last PID is removed */
353 if (--adap
->feed_count
> 0)
356 /* ask device to stop streaming */
357 if (d
->props
->streaming_ctrl
) {
358 ret
= d
->props
->streaming_ctrl(adap
->fe
[adap
->active_fe
], 0);
360 dev_err(&d
->udev
->dev
,
361 "%s: streaming_ctrl() failed=%d\n",
362 KBUILD_MODNAME
, ret
);
365 /* disable HW PID filter */
366 if (adap
->pid_filtering
&& adap
->props
->pid_filter_ctrl
) {
367 ret
= adap
->props
->pid_filter_ctrl(adap
, 0);
369 dev_err(&d
->udev
->dev
,
370 "%s: pid_filter_ctrl() failed=%d\n",
371 KBUILD_MODNAME
, ret
);
374 /* kill USB streaming packets */
375 usb_urb_killv2(&adap
->stream
);
377 /* clear 'streaming' status bit */
378 clear_bit(ADAP_STREAMING
, &adap
->state_bits
);
379 smp_mb__after_atomic();
380 wake_up_bit(&adap
->state_bits
, ADAP_STREAMING
);
384 dev_dbg(&d
->udev
->dev
, "%s: failed=%d\n", __func__
, ret
);
388 static int dvb_usbv2_media_device_init(struct dvb_usb_adapter
*adap
)
390 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
391 struct media_device
*mdev
;
392 struct dvb_usb_device
*d
= adap_to_d(adap
);
393 struct usb_device
*udev
= d
->udev
;
395 mdev
= kzalloc(sizeof(*mdev
), GFP_KERNEL
);
399 media_device_usb_init(mdev
, udev
, d
->name
);
401 dvb_register_media_controller(&adap
->dvb_adap
, mdev
);
403 dev_info(&d
->udev
->dev
, "media controller created\n");
408 static int dvb_usbv2_media_device_register(struct dvb_usb_adapter
*adap
)
410 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
411 return media_device_register(adap
->dvb_adap
.mdev
);
417 static void dvb_usbv2_media_device_unregister(struct dvb_usb_adapter
*adap
)
419 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
421 if (!adap
->dvb_adap
.mdev
)
424 media_device_unregister(adap
->dvb_adap
.mdev
);
425 media_device_cleanup(adap
->dvb_adap
.mdev
);
426 kfree(adap
->dvb_adap
.mdev
);
427 adap
->dvb_adap
.mdev
= NULL
;
432 static int dvb_usbv2_adapter_dvb_init(struct dvb_usb_adapter
*adap
)
435 struct dvb_usb_device
*d
= adap_to_d(adap
);
437 dev_dbg(&d
->udev
->dev
, "%s: adap=%d\n", __func__
, adap
->id
);
439 ret
= dvb_register_adapter(&adap
->dvb_adap
, d
->name
, d
->props
->owner
,
440 &d
->udev
->dev
, d
->props
->adapter_nr
);
442 dev_dbg(&d
->udev
->dev
, "%s: dvb_register_adapter() failed=%d\n",
444 goto err_dvb_register_adapter
;
447 adap
->dvb_adap
.priv
= adap
;
449 ret
= dvb_usbv2_media_device_init(adap
);
451 dev_dbg(&d
->udev
->dev
, "%s: dvb_usbv2_media_device_init() failed=%d\n",
453 goto err_dvb_register_mc
;
456 if (d
->props
->read_mac_address
) {
457 ret
= d
->props
->read_mac_address(adap
,
458 adap
->dvb_adap
.proposed_mac
);
460 goto err_dvb_dmx_init
;
462 dev_info(&d
->udev
->dev
, "%s: MAC address: %pM\n",
463 KBUILD_MODNAME
, adap
->dvb_adap
.proposed_mac
);
466 adap
->demux
.dmx
.capabilities
= DMX_TS_FILTERING
| DMX_SECTION_FILTERING
;
467 adap
->demux
.priv
= adap
;
468 adap
->demux
.filternum
= 0;
469 adap
->demux
.filternum
= adap
->max_feed_count
;
470 adap
->demux
.feednum
= adap
->demux
.filternum
;
471 adap
->demux
.start_feed
= dvb_usb_start_feed
;
472 adap
->demux
.stop_feed
= dvb_usb_stop_feed
;
473 adap
->demux
.write_to_decoder
= NULL
;
474 ret
= dvb_dmx_init(&adap
->demux
);
476 dev_err(&d
->udev
->dev
, "%s: dvb_dmx_init() failed=%d\n",
477 KBUILD_MODNAME
, ret
);
478 goto err_dvb_dmx_init
;
481 adap
->dmxdev
.filternum
= adap
->demux
.filternum
;
482 adap
->dmxdev
.demux
= &adap
->demux
.dmx
;
483 adap
->dmxdev
.capabilities
= 0;
484 ret
= dvb_dmxdev_init(&adap
->dmxdev
, &adap
->dvb_adap
);
486 dev_err(&d
->udev
->dev
, "%s: dvb_dmxdev_init() failed=%d\n",
487 KBUILD_MODNAME
, ret
);
488 goto err_dvb_dmxdev_init
;
491 ret
= dvb_net_init(&adap
->dvb_adap
, &adap
->dvb_net
, &adap
->demux
.dmx
);
493 dev_err(&d
->udev
->dev
, "%s: dvb_net_init() failed=%d\n",
494 KBUILD_MODNAME
, ret
);
495 goto err_dvb_net_init
;
500 dvb_dmxdev_release(&adap
->dmxdev
);
502 dvb_dmx_release(&adap
->demux
);
504 dvb_usbv2_media_device_unregister(adap
);
506 dvb_unregister_adapter(&adap
->dvb_adap
);
507 err_dvb_register_adapter
:
508 adap
->dvb_adap
.priv
= NULL
;
512 static int dvb_usbv2_adapter_dvb_exit(struct dvb_usb_adapter
*adap
)
514 dev_dbg(&adap_to_d(adap
)->udev
->dev
, "%s: adap=%d\n", __func__
,
517 if (adap
->dvb_adap
.priv
) {
518 dvb_net_release(&adap
->dvb_net
);
519 adap
->demux
.dmx
.close(&adap
->demux
.dmx
);
520 dvb_dmxdev_release(&adap
->dmxdev
);
521 dvb_dmx_release(&adap
->demux
);
522 dvb_unregister_adapter(&adap
->dvb_adap
);
528 static int dvb_usbv2_device_power_ctrl(struct dvb_usb_device
*d
, int onoff
)
537 if (d
->powered
== 0 || (onoff
&& d
->powered
== 1)) {
538 /* when switching from 1 to 0 or from 0 to 1 */
539 dev_dbg(&d
->udev
->dev
, "%s: power=%d\n", __func__
, onoff
);
540 if (d
->props
->power_ctrl
) {
541 ret
= d
->props
->power_ctrl(d
, onoff
);
549 dev_dbg(&d
->udev
->dev
, "%s: failed=%d\n", __func__
, ret
);
553 static int dvb_usb_fe_init(struct dvb_frontend
*fe
)
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
,
561 if (!adap
->suspend_resume_active
) {
562 adap
->active_fe
= fe
->id
;
563 set_bit(ADAP_INIT
, &adap
->state_bits
);
566 ret
= dvb_usbv2_device_power_ctrl(d
, 1);
570 if (d
->props
->frontend_ctrl
) {
571 ret
= d
->props
->frontend_ctrl(fe
, 1);
576 if (adap
->fe_init
[fe
->id
]) {
577 ret
= adap
->fe_init
[fe
->id
](fe
);
582 if (!adap
->suspend_resume_active
) {
583 clear_bit(ADAP_INIT
, &adap
->state_bits
);
584 smp_mb__after_atomic();
585 wake_up_bit(&adap
->state_bits
, ADAP_INIT
);
588 dev_dbg(&d
->udev
->dev
, "%s: ret=%d\n", __func__
, ret
);
592 static int dvb_usb_fe_sleep(struct dvb_frontend
*fe
)
595 struct dvb_usb_adapter
*adap
= fe
->dvb
->priv
;
596 struct dvb_usb_device
*d
= adap_to_d(adap
);
597 dev_dbg(&d
->udev
->dev
, "%s: adap=%d fe=%d\n", __func__
, adap
->id
,
600 if (!adap
->suspend_resume_active
) {
601 set_bit(ADAP_SLEEP
, &adap
->state_bits
);
602 wait_on_bit(&adap
->state_bits
, ADAP_STREAMING
,
603 TASK_UNINTERRUPTIBLE
);
606 if (adap
->fe_sleep
[fe
->id
]) {
607 ret
= adap
->fe_sleep
[fe
->id
](fe
);
612 if (d
->props
->frontend_ctrl
) {
613 ret
= d
->props
->frontend_ctrl(fe
, 0);
618 ret
= dvb_usbv2_device_power_ctrl(d
, 0);
621 if (!adap
->suspend_resume_active
) {
622 adap
->active_fe
= -1;
623 clear_bit(ADAP_SLEEP
, &adap
->state_bits
);
624 smp_mb__after_atomic();
625 wake_up_bit(&adap
->state_bits
, ADAP_SLEEP
);
628 dev_dbg(&d
->udev
->dev
, "%s: ret=%d\n", __func__
, ret
);
632 static int dvb_usbv2_adapter_frontend_init(struct dvb_usb_adapter
*adap
)
634 int ret
, i
, count_registered
= 0;
635 struct dvb_usb_device
*d
= adap_to_d(adap
);
636 dev_dbg(&d
->udev
->dev
, "%s: adap=%d\n", __func__
, adap
->id
);
638 memset(adap
->fe
, 0, sizeof(adap
->fe
));
639 adap
->active_fe
= -1;
641 if (d
->props
->frontend_attach
) {
642 ret
= d
->props
->frontend_attach(adap
);
644 dev_dbg(&d
->udev
->dev
,
645 "%s: frontend_attach() failed=%d\n",
647 goto err_dvb_frontend_detach
;
650 dev_dbg(&d
->udev
->dev
, "%s: frontend_attach() do not exists\n",
656 for (i
= 0; i
< MAX_NO_OF_FE_PER_ADAP
&& adap
->fe
[i
]; i
++) {
658 /* re-assign sleep and wakeup functions */
659 adap
->fe_init
[i
] = adap
->fe
[i
]->ops
.init
;
660 adap
->fe
[i
]->ops
.init
= dvb_usb_fe_init
;
661 adap
->fe_sleep
[i
] = adap
->fe
[i
]->ops
.sleep
;
662 adap
->fe
[i
]->ops
.sleep
= dvb_usb_fe_sleep
;
664 ret
= dvb_register_frontend(&adap
->dvb_adap
, adap
->fe
[i
]);
666 dev_err(&d
->udev
->dev
,
667 "%s: frontend%d registration failed\n",
669 goto err_dvb_unregister_frontend
;
675 if (d
->props
->tuner_attach
) {
676 ret
= d
->props
->tuner_attach(adap
);
678 dev_dbg(&d
->udev
->dev
, "%s: tuner_attach() failed=%d\n",
680 goto err_dvb_unregister_frontend
;
684 ret
= dvb_create_media_graph(&adap
->dvb_adap
, true);
686 goto err_dvb_unregister_frontend
;
688 ret
= dvb_usbv2_media_device_register(adap
);
692 err_dvb_unregister_frontend
:
693 for (i
= count_registered
- 1; i
>= 0; i
--)
694 dvb_unregister_frontend(adap
->fe
[i
]);
696 err_dvb_frontend_detach
:
697 for (i
= MAX_NO_OF_FE_PER_ADAP
- 1; i
>= 0; i
--) {
699 dvb_frontend_detach(adap
->fe
[i
]);
705 dev_dbg(&d
->udev
->dev
, "%s: failed=%d\n", __func__
, ret
);
709 static int dvb_usbv2_adapter_frontend_exit(struct dvb_usb_adapter
*adap
)
712 struct dvb_usb_device
*d
= adap_to_d(adap
);
714 dev_dbg(&d
->udev
->dev
, "%s: adap=%d\n", __func__
, adap
->id
);
716 for (i
= MAX_NO_OF_FE_PER_ADAP
- 1; i
>= 0; i
--) {
718 dvb_unregister_frontend(adap
->fe
[i
]);
719 dvb_frontend_detach(adap
->fe
[i
]);
723 if (d
->props
->tuner_detach
) {
724 ret
= d
->props
->tuner_detach(adap
);
726 dev_dbg(&d
->udev
->dev
, "%s: tuner_detach() failed=%d\n",
731 if (d
->props
->frontend_detach
) {
732 ret
= d
->props
->frontend_detach(adap
);
734 dev_dbg(&d
->udev
->dev
,
735 "%s: frontend_detach() failed=%d\n",
743 static int dvb_usbv2_adapter_init(struct dvb_usb_device
*d
)
745 struct dvb_usb_adapter
*adap
;
746 int ret
, i
, adapter_count
;
748 /* resolve adapter count */
749 adapter_count
= d
->props
->num_adapters
;
750 if (d
->props
->get_adapter_count
) {
751 ret
= d
->props
->get_adapter_count(d
);
758 for (i
= 0; i
< adapter_count
; i
++) {
759 adap
= &d
->adapter
[i
];
761 adap
->props
= &d
->props
->adapter
[i
];
763 /* speed - when running at FULL speed we need a HW PID filter */
764 if (d
->udev
->speed
== USB_SPEED_FULL
&&
765 !(adap
->props
->caps
& DVB_USB_ADAP_HAS_PID_FILTER
)) {
766 dev_err(&d
->udev
->dev
,
767 "%s: this USB2.0 device cannot be run on a USB1.1 port (it lacks a hardware PID filter)\n",
771 } else if ((d
->udev
->speed
== USB_SPEED_FULL
&&
772 adap
->props
->caps
& DVB_USB_ADAP_HAS_PID_FILTER
) ||
773 (adap
->props
->caps
& DVB_USB_ADAP_NEED_PID_FILTERING
)) {
774 dev_info(&d
->udev
->dev
,
775 "%s: will use the device's hardware PID filter (table count: %d)\n",
777 adap
->props
->pid_filter_count
);
778 adap
->pid_filtering
= 1;
779 adap
->max_feed_count
= adap
->props
->pid_filter_count
;
781 dev_info(&d
->udev
->dev
,
782 "%s: will pass the complete MPEG2 transport stream to the software demuxer\n",
784 adap
->pid_filtering
= 0;
785 adap
->max_feed_count
= 255;
788 if (!adap
->pid_filtering
&& dvb_usb_force_pid_filter_usage
&&
789 adap
->props
->caps
& DVB_USB_ADAP_HAS_PID_FILTER
) {
790 dev_info(&d
->udev
->dev
,
791 "%s: PID filter enabled by module option\n",
793 adap
->pid_filtering
= 1;
794 adap
->max_feed_count
= adap
->props
->pid_filter_count
;
797 ret
= dvb_usbv2_adapter_stream_init(adap
);
801 ret
= dvb_usbv2_adapter_dvb_init(adap
);
805 ret
= dvb_usbv2_adapter_frontend_init(adap
);
809 /* use exclusive FE lock if there is multiple shared FEs */
811 adap
->dvb_adap
.mfe_shared
= 1;
816 dev_dbg(&d
->udev
->dev
, "%s: failed=%d\n", __func__
, ret
);
820 static int dvb_usbv2_adapter_exit(struct dvb_usb_device
*d
)
823 dev_dbg(&d
->udev
->dev
, "%s:\n", __func__
);
825 for (i
= MAX_NO_OF_ADAPTER_PER_DEVICE
- 1; i
>= 0; i
--) {
826 if (d
->adapter
[i
].props
) {
827 dvb_usbv2_adapter_dvb_exit(&d
->adapter
[i
]);
828 dvb_usbv2_adapter_stream_exit(&d
->adapter
[i
]);
829 dvb_usbv2_adapter_frontend_exit(&d
->adapter
[i
]);
830 dvb_usbv2_media_device_unregister(&d
->adapter
[i
]);
837 /* general initialization functions */
838 static int dvb_usbv2_exit(struct dvb_usb_device
*d
)
840 dev_dbg(&d
->udev
->dev
, "%s:\n", __func__
);
842 dvb_usbv2_remote_exit(d
);
843 dvb_usbv2_adapter_exit(d
);
844 dvb_usbv2_i2c_exit(d
);
849 static int dvb_usbv2_init(struct dvb_usb_device
*d
)
852 dev_dbg(&d
->udev
->dev
, "%s:\n", __func__
);
854 dvb_usbv2_device_power_ctrl(d
, 1);
856 if (d
->props
->read_config
) {
857 ret
= d
->props
->read_config(d
);
862 ret
= dvb_usbv2_i2c_init(d
);
866 ret
= dvb_usbv2_adapter_init(d
);
870 if (d
->props
->init
) {
871 ret
= d
->props
->init(d
);
876 ret
= dvb_usbv2_remote_init(d
);
880 dvb_usbv2_device_power_ctrl(d
, 0);
884 dvb_usbv2_device_power_ctrl(d
, 0);
885 dev_dbg(&d
->udev
->dev
, "%s: failed=%d\n", __func__
, ret
);
889 int dvb_usbv2_probe(struct usb_interface
*intf
,
890 const struct usb_device_id
*id
)
893 struct dvb_usb_device
*d
;
894 struct usb_device
*udev
= interface_to_usbdev(intf
);
895 struct dvb_usb_driver_info
*driver_info
=
896 (struct dvb_usb_driver_info
*) id
->driver_info
;
898 dev_dbg(&udev
->dev
, "%s: bInterfaceNumber=%d\n", __func__
,
899 intf
->cur_altsetting
->desc
.bInterfaceNumber
);
901 if (!id
->driver_info
) {
902 dev_err(&udev
->dev
, "%s: driver_info failed\n", KBUILD_MODNAME
);
907 d
= kzalloc(sizeof(struct dvb_usb_device
), GFP_KERNEL
);
909 dev_err(&udev
->dev
, "%s: kzalloc() failed\n", KBUILD_MODNAME
);
915 d
->name
= driver_info
->name
;
916 d
->rc_map
= driver_info
->rc_map
;
918 d
->props
= driver_info
->props
;
920 if (intf
->cur_altsetting
->desc
.bInterfaceNumber
!=
921 d
->props
->bInterfaceNumber
) {
926 mutex_init(&d
->usb_mutex
);
927 mutex_init(&d
->i2c_mutex
);
929 if (d
->props
->size_of_priv
) {
930 d
->priv
= kzalloc(d
->props
->size_of_priv
, GFP_KERNEL
);
932 dev_err(&d
->udev
->dev
, "%s: kzalloc() failed\n",
939 if (d
->props
->probe
) {
940 ret
= d
->props
->probe(d
);
945 if (d
->props
->identify_state
) {
946 const char *name
= NULL
;
947 ret
= d
->props
->identify_state(d
, &name
);
949 dev_info(&d
->udev
->dev
,
950 "%s: found a '%s' in cold state\n",
951 KBUILD_MODNAME
, d
->name
);
954 name
= d
->props
->firmware
;
956 ret
= dvb_usbv2_download_firmware(d
, name
);
958 /* device is warm, continue initialization */
960 } else if (ret
== RECONNECTS_USB
) {
962 * USB core will call disconnect() and then
963 * probe() as device reconnects itself from the
964 * USB bus. disconnect() will release all driver
965 * resources and probe() is called for 'new'
966 * device. As 'new' device is warm we should
967 * never go here again.
973 } else if (ret
!= WARM
) {
978 dev_info(&d
->udev
->dev
, "%s: found a '%s' in warm state\n",
979 KBUILD_MODNAME
, d
->name
);
981 ret
= dvb_usbv2_init(d
);
985 dev_info(&d
->udev
->dev
,
986 "%s: '%s' successfully initialized and connected\n",
987 KBUILD_MODNAME
, d
->name
);
989 usb_set_intfdata(intf
, d
);
994 if (d
->props
->disconnect
)
995 d
->props
->disconnect(d
);
1001 dev_dbg(&udev
->dev
, "%s: failed=%d\n", __func__
, ret
);
1004 EXPORT_SYMBOL(dvb_usbv2_probe
);
1006 void dvb_usbv2_disconnect(struct usb_interface
*intf
)
1008 struct dvb_usb_device
*d
= usb_get_intfdata(intf
);
1009 const char *devname
= kstrdup(dev_name(&d
->udev
->dev
), GFP_KERNEL
);
1010 const char *drvname
= d
->name
;
1012 dev_dbg(&d
->udev
->dev
, "%s: bInterfaceNumber=%d\n", __func__
,
1013 intf
->cur_altsetting
->desc
.bInterfaceNumber
);
1020 if (d
->props
->disconnect
)
1021 d
->props
->disconnect(d
);
1026 pr_info("%s: '%s:%s' successfully deinitialized and disconnected\n",
1027 KBUILD_MODNAME
, drvname
, devname
);
1030 EXPORT_SYMBOL(dvb_usbv2_disconnect
);
1032 int dvb_usbv2_suspend(struct usb_interface
*intf
, pm_message_t msg
)
1034 struct dvb_usb_device
*d
= usb_get_intfdata(intf
);
1035 int ret
= 0, i
, active_fe
;
1036 struct dvb_frontend
*fe
;
1037 dev_dbg(&d
->udev
->dev
, "%s:\n", __func__
);
1039 /* stop remote controller poll */
1040 if (d
->rc_polling_active
)
1041 cancel_delayed_work_sync(&d
->rc_query_work
);
1043 for (i
= MAX_NO_OF_ADAPTER_PER_DEVICE
- 1; i
>= 0; i
--) {
1044 active_fe
= d
->adapter
[i
].active_fe
;
1045 if (d
->adapter
[i
].dvb_adap
.priv
&& active_fe
!= -1) {
1046 fe
= d
->adapter
[i
].fe
[active_fe
];
1047 d
->adapter
[i
].suspend_resume_active
= true;
1049 if (d
->props
->streaming_ctrl
)
1050 d
->props
->streaming_ctrl(fe
, 0);
1052 /* stop usb streaming */
1053 usb_urb_killv2(&d
->adapter
[i
].stream
);
1055 ret
= dvb_frontend_suspend(fe
);
1061 EXPORT_SYMBOL(dvb_usbv2_suspend
);
1063 static int dvb_usbv2_resume_common(struct dvb_usb_device
*d
)
1065 int ret
= 0, i
, active_fe
;
1066 struct dvb_frontend
*fe
;
1067 dev_dbg(&d
->udev
->dev
, "%s:\n", __func__
);
1069 for (i
= 0; i
< MAX_NO_OF_ADAPTER_PER_DEVICE
; i
++) {
1070 active_fe
= d
->adapter
[i
].active_fe
;
1071 if (d
->adapter
[i
].dvb_adap
.priv
&& active_fe
!= -1) {
1072 fe
= d
->adapter
[i
].fe
[active_fe
];
1074 ret
= dvb_frontend_resume(fe
);
1076 /* resume usb streaming */
1077 usb_urb_submitv2(&d
->adapter
[i
].stream
, NULL
);
1079 if (d
->props
->streaming_ctrl
)
1080 d
->props
->streaming_ctrl(fe
, 1);
1082 d
->adapter
[i
].suspend_resume_active
= false;
1086 /* start remote controller poll */
1087 if (d
->rc_polling_active
)
1088 schedule_delayed_work(&d
->rc_query_work
,
1089 msecs_to_jiffies(d
->rc
.interval
));
1094 int dvb_usbv2_resume(struct usb_interface
*intf
)
1096 struct dvb_usb_device
*d
= usb_get_intfdata(intf
);
1097 dev_dbg(&d
->udev
->dev
, "%s:\n", __func__
);
1099 return dvb_usbv2_resume_common(d
);
1101 EXPORT_SYMBOL(dvb_usbv2_resume
);
1103 int dvb_usbv2_reset_resume(struct usb_interface
*intf
)
1105 struct dvb_usb_device
*d
= usb_get_intfdata(intf
);
1107 dev_dbg(&d
->udev
->dev
, "%s:\n", __func__
);
1109 dvb_usbv2_device_power_ctrl(d
, 1);
1114 ret
= dvb_usbv2_resume_common(d
);
1116 dvb_usbv2_device_power_ctrl(d
, 0);
1120 EXPORT_SYMBOL(dvb_usbv2_reset_resume
);
1122 MODULE_VERSION("2.0");
1123 MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@posteo.de>");
1124 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1125 MODULE_DESCRIPTION("DVB USB common");
1126 MODULE_LICENSE("GPL");