4 * Copyright (C) 2004-6 Patrick Boettcher <patrick.boettcher@posteo.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"
23 #include <media/media-device.h>
25 static int dvb_usbv2_disable_rc_polling
;
26 module_param_named(disable_rc_polling
, dvb_usbv2_disable_rc_polling
, int, 0644);
27 MODULE_PARM_DESC(disable_rc_polling
,
28 "disable remote control polling (default: 0)");
29 static int dvb_usb_force_pid_filter_usage
;
30 module_param_named(force_pid_filter_usage
, dvb_usb_force_pid_filter_usage
,
32 MODULE_PARM_DESC(force_pid_filter_usage
,
33 "force all DVB USB devices to use a PID filter, if any (default: 0)");
35 static int dvb_usbv2_download_firmware(struct dvb_usb_device
*d
,
39 const struct firmware
*fw
;
40 dev_dbg(&d
->udev
->dev
, "%s:\n", __func__
);
42 if (!d
->props
->download_firmware
) {
47 ret
= request_firmware(&fw
, name
, &d
->udev
->dev
);
49 dev_err(&d
->udev
->dev
,
50 "%s: Did not find the firmware file '%s'. Please see linux/Documentation/dvb/ for more details on firmware-problems. Status %d\n",
51 KBUILD_MODNAME
, name
, ret
);
55 dev_info(&d
->udev
->dev
, "%s: downloading firmware from file '%s'\n",
56 KBUILD_MODNAME
, name
);
58 ret
= d
->props
->download_firmware(d
, fw
);
65 dev_dbg(&d
->udev
->dev
, "%s: failed=%d\n", __func__
, ret
);
69 static int dvb_usbv2_i2c_init(struct dvb_usb_device
*d
)
72 dev_dbg(&d
->udev
->dev
, "%s:\n", __func__
);
74 if (!d
->props
->i2c_algo
)
77 strlcpy(d
->i2c_adap
.name
, d
->name
, sizeof(d
->i2c_adap
.name
));
78 d
->i2c_adap
.algo
= d
->props
->i2c_algo
;
79 d
->i2c_adap
.dev
.parent
= &d
->udev
->dev
;
80 i2c_set_adapdata(&d
->i2c_adap
, d
);
82 ret
= i2c_add_adapter(&d
->i2c_adap
);
84 d
->i2c_adap
.algo
= NULL
;
90 dev_dbg(&d
->udev
->dev
, "%s: failed=%d\n", __func__
, ret
);
94 static int dvb_usbv2_i2c_exit(struct dvb_usb_device
*d
)
96 dev_dbg(&d
->udev
->dev
, "%s:\n", __func__
);
99 i2c_del_adapter(&d
->i2c_adap
);
104 #if IS_ENABLED(CONFIG_RC_CORE)
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
) {
116 d
->rc_polling_active
= false;
120 ret
= d
->rc
.query(d
);
122 dev_err(&d
->udev
->dev
, "%s: rc.query() failed=%d\n",
123 KBUILD_MODNAME
, ret
);
124 d
->rc_polling_active
= false;
125 return; /* stop polling */
128 schedule_delayed_work(&d
->rc_query_work
,
129 msecs_to_jiffies(d
->rc
.interval
));
132 static int dvb_usbv2_remote_init(struct dvb_usb_device
*d
)
136 dev_dbg(&d
->udev
->dev
, "%s:\n", __func__
);
138 if (dvb_usbv2_disable_rc_polling
|| !d
->props
->get_rc_config
)
141 d
->rc
.map_name
= d
->rc_map
;
142 ret
= d
->props
->get_rc_config(d
, &d
->rc
);
146 /* disable rc when there is no keymap defined */
150 dev
= rc_allocate_device(d
->rc
.driver_type
);
156 dev
->dev
.parent
= &d
->udev
->dev
;
157 dev
->device_name
= d
->name
;
158 usb_make_path(d
->udev
, d
->rc_phys
, sizeof(d
->rc_phys
));
159 strlcat(d
->rc_phys
, "/ir0", sizeof(d
->rc_phys
));
160 dev
->input_phys
= d
->rc_phys
;
161 usb_to_input_id(d
->udev
, &dev
->input_id
);
162 dev
->driver_name
= d
->props
->driver_name
;
163 dev
->map_name
= d
->rc
.map_name
;
164 dev
->allowed_protocols
= d
->rc
.allowed_protos
;
165 dev
->change_protocol
= d
->rc
.change_protocol
;
168 ret
= rc_register_device(dev
);
176 /* start polling if needed */
177 if (d
->rc
.query
&& !d
->rc
.bulk_mode
) {
178 /* initialize a work queue for handling polling */
179 INIT_DELAYED_WORK(&d
->rc_query_work
,
180 dvb_usb_read_remote_control
);
181 dev_info(&d
->udev
->dev
,
182 "%s: schedule remote query interval to %d msecs\n",
183 KBUILD_MODNAME
, d
->rc
.interval
);
184 schedule_delayed_work(&d
->rc_query_work
,
185 msecs_to_jiffies(d
->rc
.interval
));
186 d
->rc_polling_active
= true;
191 dev_dbg(&d
->udev
->dev
, "%s: failed=%d\n", __func__
, ret
);
195 static int dvb_usbv2_remote_exit(struct dvb_usb_device
*d
)
197 dev_dbg(&d
->udev
->dev
, "%s:\n", __func__
);
200 cancel_delayed_work_sync(&d
->rc_query_work
);
201 rc_unregister_device(d
->rc_dev
);
208 #define dvb_usbv2_remote_init(args...) 0
209 #define dvb_usbv2_remote_exit(args...)
212 static void dvb_usb_data_complete(struct usb_data_stream
*stream
, u8
*buf
,
215 struct dvb_usb_adapter
*adap
= stream
->user_priv
;
216 dvb_dmx_swfilter(&adap
->demux
, buf
, len
);
219 static void dvb_usb_data_complete_204(struct usb_data_stream
*stream
, u8
*buf
,
222 struct dvb_usb_adapter
*adap
= stream
->user_priv
;
223 dvb_dmx_swfilter_204(&adap
->demux
, buf
, len
);
226 static void dvb_usb_data_complete_raw(struct usb_data_stream
*stream
, u8
*buf
,
229 struct dvb_usb_adapter
*adap
= stream
->user_priv
;
230 dvb_dmx_swfilter_raw(&adap
->demux
, buf
, len
);
233 static int dvb_usbv2_adapter_stream_init(struct dvb_usb_adapter
*adap
)
235 dev_dbg(&adap_to_d(adap
)->udev
->dev
, "%s: adap=%d\n", __func__
,
238 adap
->stream
.udev
= adap_to_d(adap
)->udev
;
239 adap
->stream
.user_priv
= adap
;
240 adap
->stream
.complete
= dvb_usb_data_complete
;
242 return usb_urb_initv2(&adap
->stream
, &adap
->props
->stream
);
245 static int dvb_usbv2_adapter_stream_exit(struct dvb_usb_adapter
*adap
)
247 dev_dbg(&adap_to_d(adap
)->udev
->dev
, "%s: adap=%d\n", __func__
,
250 return usb_urb_exitv2(&adap
->stream
);
253 static int dvb_usb_start_feed(struct dvb_demux_feed
*dvbdmxfeed
)
255 struct dvb_usb_adapter
*adap
= dvbdmxfeed
->demux
->priv
;
256 struct dvb_usb_device
*d
= adap_to_d(adap
);
258 struct usb_data_stream_properties stream_props
;
259 dev_dbg(&d
->udev
->dev
,
260 "%s: adap=%d active_fe=%d feed_type=%d setting pid [%s]: %04x (%04d) at index %d\n",
261 __func__
, adap
->id
, adap
->active_fe
, dvbdmxfeed
->type
,
262 adap
->pid_filtering
? "yes" : "no", dvbdmxfeed
->pid
,
263 dvbdmxfeed
->pid
, dvbdmxfeed
->index
);
265 /* wait init is done */
266 wait_on_bit(&adap
->state_bits
, ADAP_INIT
, TASK_UNINTERRUPTIBLE
);
268 if (adap
->active_fe
== -1)
271 /* skip feed setup if we are already feeding */
272 if (adap
->feed_count
++ > 0)
273 goto skip_feed_start
;
275 /* set 'streaming' status bit */
276 set_bit(ADAP_STREAMING
, &adap
->state_bits
);
278 /* resolve input and output streaming parameters */
279 if (d
->props
->get_stream_config
) {
280 memcpy(&stream_props
, &adap
->props
->stream
,
281 sizeof(struct usb_data_stream_properties
));
282 ret
= d
->props
->get_stream_config(adap
->fe
[adap
->active_fe
],
283 &adap
->ts_type
, &stream_props
);
285 dev_err(&d
->udev
->dev
,
286 "%s: get_stream_config() failed=%d\n",
287 KBUILD_MODNAME
, ret
);
289 stream_props
= adap
->props
->stream
;
292 switch (adap
->ts_type
) {
293 case DVB_USB_FE_TS_TYPE_204
:
294 adap
->stream
.complete
= dvb_usb_data_complete_204
;
296 case DVB_USB_FE_TS_TYPE_RAW
:
297 adap
->stream
.complete
= dvb_usb_data_complete_raw
;
299 case DVB_USB_FE_TS_TYPE_188
:
301 adap
->stream
.complete
= dvb_usb_data_complete
;
305 /* submit USB streaming packets */
306 usb_urb_submitv2(&adap
->stream
, &stream_props
);
308 /* enable HW PID filter */
309 if (adap
->pid_filtering
&& adap
->props
->pid_filter_ctrl
) {
310 ret
= adap
->props
->pid_filter_ctrl(adap
, 1);
312 dev_err(&d
->udev
->dev
,
313 "%s: pid_filter_ctrl() failed=%d\n",
314 KBUILD_MODNAME
, ret
);
317 /* ask device to start streaming */
318 if (d
->props
->streaming_ctrl
) {
319 ret
= d
->props
->streaming_ctrl(adap
->fe
[adap
->active_fe
], 1);
321 dev_err(&d
->udev
->dev
,
322 "%s: streaming_ctrl() failed=%d\n",
323 KBUILD_MODNAME
, ret
);
327 /* add PID to device HW PID filter */
328 if (adap
->pid_filtering
&& adap
->props
->pid_filter
) {
329 ret
= adap
->props
->pid_filter(adap
, dvbdmxfeed
->index
,
332 dev_err(&d
->udev
->dev
, "%s: pid_filter() failed=%d\n",
333 KBUILD_MODNAME
, ret
);
337 dev_dbg(&d
->udev
->dev
, "%s: failed=%d\n", __func__
, ret
);
341 static int dvb_usb_stop_feed(struct dvb_demux_feed
*dvbdmxfeed
)
343 struct dvb_usb_adapter
*adap
= dvbdmxfeed
->demux
->priv
;
344 struct dvb_usb_device
*d
= adap_to_d(adap
);
346 dev_dbg(&d
->udev
->dev
,
347 "%s: adap=%d active_fe=%d feed_type=%d setting pid [%s]: %04x (%04d) at index %d\n",
348 __func__
, adap
->id
, adap
->active_fe
, dvbdmxfeed
->type
,
349 adap
->pid_filtering
? "yes" : "no", dvbdmxfeed
->pid
,
350 dvbdmxfeed
->pid
, dvbdmxfeed
->index
);
352 if (adap
->active_fe
== -1)
355 /* remove PID from device HW PID filter */
356 if (adap
->pid_filtering
&& adap
->props
->pid_filter
) {
357 ret
= adap
->props
->pid_filter(adap
, dvbdmxfeed
->index
,
360 dev_err(&d
->udev
->dev
, "%s: pid_filter() failed=%d\n",
361 KBUILD_MODNAME
, ret
);
364 /* we cannot stop streaming until last PID is removed */
365 if (--adap
->feed_count
> 0)
368 /* ask device to stop streaming */
369 if (d
->props
->streaming_ctrl
) {
370 ret
= d
->props
->streaming_ctrl(adap
->fe
[adap
->active_fe
], 0);
372 dev_err(&d
->udev
->dev
,
373 "%s: streaming_ctrl() failed=%d\n",
374 KBUILD_MODNAME
, ret
);
377 /* disable HW PID filter */
378 if (adap
->pid_filtering
&& adap
->props
->pid_filter_ctrl
) {
379 ret
= adap
->props
->pid_filter_ctrl(adap
, 0);
381 dev_err(&d
->udev
->dev
,
382 "%s: pid_filter_ctrl() failed=%d\n",
383 KBUILD_MODNAME
, ret
);
386 /* kill USB streaming packets */
387 usb_urb_killv2(&adap
->stream
);
389 /* clear 'streaming' status bit */
390 clear_bit(ADAP_STREAMING
, &adap
->state_bits
);
391 smp_mb__after_atomic();
392 wake_up_bit(&adap
->state_bits
, ADAP_STREAMING
);
396 dev_dbg(&d
->udev
->dev
, "%s: failed=%d\n", __func__
, ret
);
400 static int dvb_usbv2_media_device_init(struct dvb_usb_adapter
*adap
)
402 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
403 struct media_device
*mdev
;
404 struct dvb_usb_device
*d
= adap_to_d(adap
);
405 struct usb_device
*udev
= d
->udev
;
407 mdev
= kzalloc(sizeof(*mdev
), GFP_KERNEL
);
411 media_device_usb_init(mdev
, udev
, d
->name
);
413 dvb_register_media_controller(&adap
->dvb_adap
, mdev
);
415 dev_info(&d
->udev
->dev
, "media controller created\n");
420 static int dvb_usbv2_media_device_register(struct dvb_usb_adapter
*adap
)
422 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
423 return media_device_register(adap
->dvb_adap
.mdev
);
429 static void dvb_usbv2_media_device_unregister(struct dvb_usb_adapter
*adap
)
431 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
433 if (!adap
->dvb_adap
.mdev
)
436 media_device_unregister(adap
->dvb_adap
.mdev
);
437 media_device_cleanup(adap
->dvb_adap
.mdev
);
438 kfree(adap
->dvb_adap
.mdev
);
439 adap
->dvb_adap
.mdev
= NULL
;
444 static int dvb_usbv2_adapter_dvb_init(struct dvb_usb_adapter
*adap
)
447 struct dvb_usb_device
*d
= adap_to_d(adap
);
449 dev_dbg(&d
->udev
->dev
, "%s: adap=%d\n", __func__
, adap
->id
);
451 ret
= dvb_register_adapter(&adap
->dvb_adap
, d
->name
, d
->props
->owner
,
452 &d
->udev
->dev
, d
->props
->adapter_nr
);
454 dev_dbg(&d
->udev
->dev
, "%s: dvb_register_adapter() failed=%d\n",
456 goto err_dvb_register_adapter
;
459 adap
->dvb_adap
.priv
= adap
;
461 ret
= dvb_usbv2_media_device_init(adap
);
463 dev_dbg(&d
->udev
->dev
, "%s: dvb_usbv2_media_device_init() failed=%d\n",
465 goto err_dvb_register_mc
;
468 if (d
->props
->read_mac_address
) {
469 ret
= d
->props
->read_mac_address(adap
,
470 adap
->dvb_adap
.proposed_mac
);
472 goto err_dvb_dmx_init
;
474 dev_info(&d
->udev
->dev
, "%s: MAC address: %pM\n",
475 KBUILD_MODNAME
, adap
->dvb_adap
.proposed_mac
);
478 adap
->demux
.dmx
.capabilities
= DMX_TS_FILTERING
| DMX_SECTION_FILTERING
;
479 adap
->demux
.priv
= adap
;
480 adap
->demux
.filternum
= 0;
481 adap
->demux
.filternum
= adap
->max_feed_count
;
482 adap
->demux
.feednum
= adap
->demux
.filternum
;
483 adap
->demux
.start_feed
= dvb_usb_start_feed
;
484 adap
->demux
.stop_feed
= dvb_usb_stop_feed
;
485 adap
->demux
.write_to_decoder
= NULL
;
486 ret
= dvb_dmx_init(&adap
->demux
);
488 dev_err(&d
->udev
->dev
, "%s: dvb_dmx_init() failed=%d\n",
489 KBUILD_MODNAME
, ret
);
490 goto err_dvb_dmx_init
;
493 adap
->dmxdev
.filternum
= adap
->demux
.filternum
;
494 adap
->dmxdev
.demux
= &adap
->demux
.dmx
;
495 adap
->dmxdev
.capabilities
= 0;
496 ret
= dvb_dmxdev_init(&adap
->dmxdev
, &adap
->dvb_adap
);
498 dev_err(&d
->udev
->dev
, "%s: dvb_dmxdev_init() failed=%d\n",
499 KBUILD_MODNAME
, ret
);
500 goto err_dvb_dmxdev_init
;
503 ret
= dvb_net_init(&adap
->dvb_adap
, &adap
->dvb_net
, &adap
->demux
.dmx
);
505 dev_err(&d
->udev
->dev
, "%s: dvb_net_init() failed=%d\n",
506 KBUILD_MODNAME
, ret
);
507 goto err_dvb_net_init
;
512 dvb_dmxdev_release(&adap
->dmxdev
);
514 dvb_dmx_release(&adap
->demux
);
516 dvb_usbv2_media_device_unregister(adap
);
518 dvb_unregister_adapter(&adap
->dvb_adap
);
519 err_dvb_register_adapter
:
520 adap
->dvb_adap
.priv
= NULL
;
524 static int dvb_usbv2_adapter_dvb_exit(struct dvb_usb_adapter
*adap
)
526 dev_dbg(&adap_to_d(adap
)->udev
->dev
, "%s: adap=%d\n", __func__
,
529 if (adap
->dvb_adap
.priv
) {
530 dvb_net_release(&adap
->dvb_net
);
531 adap
->demux
.dmx
.close(&adap
->demux
.dmx
);
532 dvb_dmxdev_release(&adap
->dmxdev
);
533 dvb_dmx_release(&adap
->demux
);
534 dvb_unregister_adapter(&adap
->dvb_adap
);
540 static int dvb_usbv2_device_power_ctrl(struct dvb_usb_device
*d
, int onoff
)
549 if (d
->powered
== 0 || (onoff
&& d
->powered
== 1)) {
550 /* when switching from 1 to 0 or from 0 to 1 */
551 dev_dbg(&d
->udev
->dev
, "%s: power=%d\n", __func__
, onoff
);
552 if (d
->props
->power_ctrl
) {
553 ret
= d
->props
->power_ctrl(d
, onoff
);
561 dev_dbg(&d
->udev
->dev
, "%s: failed=%d\n", __func__
, ret
);
565 static int dvb_usb_fe_init(struct dvb_frontend
*fe
)
568 struct dvb_usb_adapter
*adap
= fe
->dvb
->priv
;
569 struct dvb_usb_device
*d
= adap_to_d(adap
);
570 dev_dbg(&d
->udev
->dev
, "%s: adap=%d fe=%d\n", __func__
, adap
->id
,
573 if (!adap
->suspend_resume_active
) {
574 adap
->active_fe
= fe
->id
;
575 set_bit(ADAP_INIT
, &adap
->state_bits
);
578 ret
= dvb_usbv2_device_power_ctrl(d
, 1);
582 if (d
->props
->frontend_ctrl
) {
583 ret
= d
->props
->frontend_ctrl(fe
, 1);
588 if (adap
->fe_init
[fe
->id
]) {
589 ret
= adap
->fe_init
[fe
->id
](fe
);
594 if (!adap
->suspend_resume_active
) {
595 clear_bit(ADAP_INIT
, &adap
->state_bits
);
596 smp_mb__after_atomic();
597 wake_up_bit(&adap
->state_bits
, ADAP_INIT
);
600 dev_dbg(&d
->udev
->dev
, "%s: ret=%d\n", __func__
, ret
);
604 static int dvb_usb_fe_sleep(struct dvb_frontend
*fe
)
607 struct dvb_usb_adapter
*adap
= fe
->dvb
->priv
;
608 struct dvb_usb_device
*d
= adap_to_d(adap
);
609 dev_dbg(&d
->udev
->dev
, "%s: adap=%d fe=%d\n", __func__
, adap
->id
,
612 if (!adap
->suspend_resume_active
) {
613 set_bit(ADAP_SLEEP
, &adap
->state_bits
);
614 wait_on_bit(&adap
->state_bits
, ADAP_STREAMING
,
615 TASK_UNINTERRUPTIBLE
);
618 if (adap
->fe_sleep
[fe
->id
]) {
619 ret
= adap
->fe_sleep
[fe
->id
](fe
);
624 if (d
->props
->frontend_ctrl
) {
625 ret
= d
->props
->frontend_ctrl(fe
, 0);
630 ret
= dvb_usbv2_device_power_ctrl(d
, 0);
633 if (!adap
->suspend_resume_active
) {
634 adap
->active_fe
= -1;
635 clear_bit(ADAP_SLEEP
, &adap
->state_bits
);
636 smp_mb__after_atomic();
637 wake_up_bit(&adap
->state_bits
, ADAP_SLEEP
);
640 dev_dbg(&d
->udev
->dev
, "%s: ret=%d\n", __func__
, ret
);
644 static int dvb_usbv2_adapter_frontend_init(struct dvb_usb_adapter
*adap
)
646 int ret
, i
, count_registered
= 0;
647 struct dvb_usb_device
*d
= adap_to_d(adap
);
648 dev_dbg(&d
->udev
->dev
, "%s: adap=%d\n", __func__
, adap
->id
);
650 memset(adap
->fe
, 0, sizeof(adap
->fe
));
651 adap
->active_fe
= -1;
653 if (d
->props
->frontend_attach
) {
654 ret
= d
->props
->frontend_attach(adap
);
656 dev_dbg(&d
->udev
->dev
,
657 "%s: frontend_attach() failed=%d\n",
659 goto err_dvb_frontend_detach
;
662 dev_dbg(&d
->udev
->dev
, "%s: frontend_attach() do not exists\n",
668 for (i
= 0; i
< MAX_NO_OF_FE_PER_ADAP
&& adap
->fe
[i
]; i
++) {
670 /* re-assign sleep and wakeup functions */
671 adap
->fe_init
[i
] = adap
->fe
[i
]->ops
.init
;
672 adap
->fe
[i
]->ops
.init
= dvb_usb_fe_init
;
673 adap
->fe_sleep
[i
] = adap
->fe
[i
]->ops
.sleep
;
674 adap
->fe
[i
]->ops
.sleep
= dvb_usb_fe_sleep
;
676 ret
= dvb_register_frontend(&adap
->dvb_adap
, adap
->fe
[i
]);
678 dev_err(&d
->udev
->dev
,
679 "%s: frontend%d registration failed\n",
681 goto err_dvb_unregister_frontend
;
687 if (d
->props
->tuner_attach
) {
688 ret
= d
->props
->tuner_attach(adap
);
690 dev_dbg(&d
->udev
->dev
, "%s: tuner_attach() failed=%d\n",
692 goto err_dvb_unregister_frontend
;
696 ret
= dvb_create_media_graph(&adap
->dvb_adap
, true);
698 goto err_dvb_unregister_frontend
;
700 ret
= dvb_usbv2_media_device_register(adap
);
704 err_dvb_unregister_frontend
:
705 for (i
= count_registered
- 1; i
>= 0; i
--)
706 dvb_unregister_frontend(adap
->fe
[i
]);
708 err_dvb_frontend_detach
:
709 for (i
= MAX_NO_OF_FE_PER_ADAP
- 1; i
>= 0; i
--) {
711 dvb_frontend_detach(adap
->fe
[i
]);
717 dev_dbg(&d
->udev
->dev
, "%s: failed=%d\n", __func__
, ret
);
721 static int dvb_usbv2_adapter_frontend_exit(struct dvb_usb_adapter
*adap
)
724 struct dvb_usb_device
*d
= adap_to_d(adap
);
726 dev_dbg(&d
->udev
->dev
, "%s: adap=%d\n", __func__
, adap
->id
);
728 for (i
= MAX_NO_OF_FE_PER_ADAP
- 1; i
>= 0; i
--) {
730 dvb_unregister_frontend(adap
->fe
[i
]);
731 dvb_frontend_detach(adap
->fe
[i
]);
735 if (d
->props
->tuner_detach
) {
736 ret
= d
->props
->tuner_detach(adap
);
738 dev_dbg(&d
->udev
->dev
, "%s: tuner_detach() failed=%d\n",
743 if (d
->props
->frontend_detach
) {
744 ret
= d
->props
->frontend_detach(adap
);
746 dev_dbg(&d
->udev
->dev
,
747 "%s: frontend_detach() failed=%d\n",
755 static int dvb_usbv2_adapter_init(struct dvb_usb_device
*d
)
757 struct dvb_usb_adapter
*adap
;
758 int ret
, i
, adapter_count
;
760 /* resolve adapter count */
761 adapter_count
= d
->props
->num_adapters
;
762 if (d
->props
->get_adapter_count
) {
763 ret
= d
->props
->get_adapter_count(d
);
770 for (i
= 0; i
< adapter_count
; i
++) {
771 adap
= &d
->adapter
[i
];
773 adap
->props
= &d
->props
->adapter
[i
];
775 /* speed - when running at FULL speed we need a HW PID filter */
776 if (d
->udev
->speed
== USB_SPEED_FULL
&&
777 !(adap
->props
->caps
& DVB_USB_ADAP_HAS_PID_FILTER
)) {
778 dev_err(&d
->udev
->dev
,
779 "%s: this USB2.0 device cannot be run on a USB1.1 port (it lacks a hardware PID filter)\n",
783 } else if ((d
->udev
->speed
== USB_SPEED_FULL
&&
784 adap
->props
->caps
& DVB_USB_ADAP_HAS_PID_FILTER
) ||
785 (adap
->props
->caps
& DVB_USB_ADAP_NEED_PID_FILTERING
)) {
786 dev_info(&d
->udev
->dev
,
787 "%s: will use the device's hardware PID filter (table count: %d)\n",
789 adap
->props
->pid_filter_count
);
790 adap
->pid_filtering
= 1;
791 adap
->max_feed_count
= adap
->props
->pid_filter_count
;
793 dev_info(&d
->udev
->dev
,
794 "%s: will pass the complete MPEG2 transport stream to the software demuxer\n",
796 adap
->pid_filtering
= 0;
797 adap
->max_feed_count
= 255;
800 if (!adap
->pid_filtering
&& dvb_usb_force_pid_filter_usage
&&
801 adap
->props
->caps
& DVB_USB_ADAP_HAS_PID_FILTER
) {
802 dev_info(&d
->udev
->dev
,
803 "%s: PID filter enabled by module option\n",
805 adap
->pid_filtering
= 1;
806 adap
->max_feed_count
= adap
->props
->pid_filter_count
;
809 ret
= dvb_usbv2_adapter_stream_init(adap
);
813 ret
= dvb_usbv2_adapter_dvb_init(adap
);
817 ret
= dvb_usbv2_adapter_frontend_init(adap
);
821 /* use exclusive FE lock if there is multiple shared FEs */
823 adap
->dvb_adap
.mfe_shared
= 1;
828 dev_dbg(&d
->udev
->dev
, "%s: failed=%d\n", __func__
, ret
);
832 static int dvb_usbv2_adapter_exit(struct dvb_usb_device
*d
)
835 dev_dbg(&d
->udev
->dev
, "%s:\n", __func__
);
837 for (i
= MAX_NO_OF_ADAPTER_PER_DEVICE
- 1; i
>= 0; i
--) {
838 if (d
->adapter
[i
].props
) {
839 dvb_usbv2_adapter_dvb_exit(&d
->adapter
[i
]);
840 dvb_usbv2_adapter_stream_exit(&d
->adapter
[i
]);
841 dvb_usbv2_adapter_frontend_exit(&d
->adapter
[i
]);
842 dvb_usbv2_media_device_unregister(&d
->adapter
[i
]);
849 /* general initialization functions */
850 static int dvb_usbv2_exit(struct dvb_usb_device
*d
)
852 dev_dbg(&d
->udev
->dev
, "%s:\n", __func__
);
854 dvb_usbv2_remote_exit(d
);
855 dvb_usbv2_adapter_exit(d
);
856 dvb_usbv2_i2c_exit(d
);
863 static int dvb_usbv2_init(struct dvb_usb_device
*d
)
866 dev_dbg(&d
->udev
->dev
, "%s:\n", __func__
);
868 dvb_usbv2_device_power_ctrl(d
, 1);
870 if (d
->props
->read_config
) {
871 ret
= d
->props
->read_config(d
);
876 ret
= dvb_usbv2_i2c_init(d
);
880 ret
= dvb_usbv2_adapter_init(d
);
884 if (d
->props
->init
) {
885 ret
= d
->props
->init(d
);
890 ret
= dvb_usbv2_remote_init(d
);
894 dvb_usbv2_device_power_ctrl(d
, 0);
898 dvb_usbv2_device_power_ctrl(d
, 0);
899 dev_dbg(&d
->udev
->dev
, "%s: failed=%d\n", __func__
, ret
);
903 int dvb_usbv2_probe(struct usb_interface
*intf
,
904 const struct usb_device_id
*id
)
907 struct dvb_usb_device
*d
;
908 struct usb_device
*udev
= interface_to_usbdev(intf
);
909 struct dvb_usb_driver_info
*driver_info
=
910 (struct dvb_usb_driver_info
*) id
->driver_info
;
912 dev_dbg(&udev
->dev
, "%s: bInterfaceNumber=%d\n", __func__
,
913 intf
->cur_altsetting
->desc
.bInterfaceNumber
);
915 if (!id
->driver_info
) {
916 dev_err(&udev
->dev
, "%s: driver_info failed\n", KBUILD_MODNAME
);
921 d
= kzalloc(sizeof(struct dvb_usb_device
), GFP_KERNEL
);
923 dev_err(&udev
->dev
, "%s: kzalloc() failed\n", KBUILD_MODNAME
);
929 d
->name
= driver_info
->name
;
930 d
->rc_map
= driver_info
->rc_map
;
932 d
->props
= driver_info
->props
;
934 if (intf
->cur_altsetting
->desc
.bInterfaceNumber
!=
935 d
->props
->bInterfaceNumber
) {
940 mutex_init(&d
->usb_mutex
);
941 mutex_init(&d
->i2c_mutex
);
943 if (d
->props
->size_of_priv
) {
944 d
->priv
= kzalloc(d
->props
->size_of_priv
, GFP_KERNEL
);
946 dev_err(&d
->udev
->dev
, "%s: kzalloc() failed\n",
953 if (d
->props
->identify_state
) {
954 const char *name
= NULL
;
955 ret
= d
->props
->identify_state(d
, &name
);
958 } else if (ret
== COLD
) {
959 dev_info(&d
->udev
->dev
,
960 "%s: found a '%s' in cold state\n",
961 KBUILD_MODNAME
, d
->name
);
964 name
= d
->props
->firmware
;
966 ret
= dvb_usbv2_download_firmware(d
, name
);
968 /* device is warm, continue initialization */
970 } else if (ret
== RECONNECTS_USB
) {
972 * USB core will call disconnect() and then
973 * probe() as device reconnects itself from the
974 * USB bus. disconnect() will release all driver
975 * resources and probe() is called for 'new'
976 * device. As 'new' device is warm we should
977 * never go here again.
988 dev_info(&d
->udev
->dev
, "%s: found a '%s' in warm state\n",
989 KBUILD_MODNAME
, d
->name
);
991 ret
= dvb_usbv2_init(d
);
995 dev_info(&d
->udev
->dev
,
996 "%s: '%s' successfully initialized and connected\n",
997 KBUILD_MODNAME
, d
->name
);
999 usb_set_intfdata(intf
, d
);
1005 dev_dbg(&udev
->dev
, "%s: failed=%d\n", __func__
, ret
);
1008 EXPORT_SYMBOL(dvb_usbv2_probe
);
1010 void dvb_usbv2_disconnect(struct usb_interface
*intf
)
1012 struct dvb_usb_device
*d
= usb_get_intfdata(intf
);
1013 const char *devname
= kstrdup(dev_name(&d
->udev
->dev
), GFP_KERNEL
);
1014 const char *drvname
= d
->name
;
1016 dev_dbg(&d
->udev
->dev
, "%s: bInterfaceNumber=%d\n", __func__
,
1017 intf
->cur_altsetting
->desc
.bInterfaceNumber
);
1024 pr_info("%s: '%s:%s' successfully deinitialized and disconnected\n",
1025 KBUILD_MODNAME
, drvname
, devname
);
1028 EXPORT_SYMBOL(dvb_usbv2_disconnect
);
1030 int dvb_usbv2_suspend(struct usb_interface
*intf
, pm_message_t msg
)
1032 struct dvb_usb_device
*d
= usb_get_intfdata(intf
);
1033 int ret
= 0, i
, active_fe
;
1034 struct dvb_frontend
*fe
;
1035 dev_dbg(&d
->udev
->dev
, "%s:\n", __func__
);
1037 /* stop remote controller poll */
1038 if (d
->rc_polling_active
)
1039 cancel_delayed_work_sync(&d
->rc_query_work
);
1041 for (i
= MAX_NO_OF_ADAPTER_PER_DEVICE
- 1; i
>= 0; i
--) {
1042 active_fe
= d
->adapter
[i
].active_fe
;
1043 if (d
->adapter
[i
].dvb_adap
.priv
&& active_fe
!= -1) {
1044 fe
= d
->adapter
[i
].fe
[active_fe
];
1045 d
->adapter
[i
].suspend_resume_active
= true;
1047 if (d
->props
->streaming_ctrl
)
1048 d
->props
->streaming_ctrl(fe
, 0);
1050 /* stop usb streaming */
1051 usb_urb_killv2(&d
->adapter
[i
].stream
);
1053 ret
= dvb_frontend_suspend(fe
);
1059 EXPORT_SYMBOL(dvb_usbv2_suspend
);
1061 static int dvb_usbv2_resume_common(struct dvb_usb_device
*d
)
1063 int ret
= 0, i
, active_fe
;
1064 struct dvb_frontend
*fe
;
1065 dev_dbg(&d
->udev
->dev
, "%s:\n", __func__
);
1067 for (i
= 0; i
< MAX_NO_OF_ADAPTER_PER_DEVICE
; i
++) {
1068 active_fe
= d
->adapter
[i
].active_fe
;
1069 if (d
->adapter
[i
].dvb_adap
.priv
&& active_fe
!= -1) {
1070 fe
= d
->adapter
[i
].fe
[active_fe
];
1072 ret
= dvb_frontend_resume(fe
);
1074 /* resume usb streaming */
1075 usb_urb_submitv2(&d
->adapter
[i
].stream
, NULL
);
1077 if (d
->props
->streaming_ctrl
)
1078 d
->props
->streaming_ctrl(fe
, 1);
1080 d
->adapter
[i
].suspend_resume_active
= false;
1084 /* start remote controller poll */
1085 if (d
->rc_polling_active
)
1086 schedule_delayed_work(&d
->rc_query_work
,
1087 msecs_to_jiffies(d
->rc
.interval
));
1092 int dvb_usbv2_resume(struct usb_interface
*intf
)
1094 struct dvb_usb_device
*d
= usb_get_intfdata(intf
);
1095 dev_dbg(&d
->udev
->dev
, "%s:\n", __func__
);
1097 return dvb_usbv2_resume_common(d
);
1099 EXPORT_SYMBOL(dvb_usbv2_resume
);
1101 int dvb_usbv2_reset_resume(struct usb_interface
*intf
)
1103 struct dvb_usb_device
*d
= usb_get_intfdata(intf
);
1105 dev_dbg(&d
->udev
->dev
, "%s:\n", __func__
);
1107 dvb_usbv2_device_power_ctrl(d
, 1);
1112 ret
= dvb_usbv2_resume_common(d
);
1114 dvb_usbv2_device_power_ctrl(d
, 0);
1118 EXPORT_SYMBOL(dvb_usbv2_reset_resume
);
1120 MODULE_VERSION("2.0");
1121 MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@posteo.de>");
1122 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1123 MODULE_DESCRIPTION("DVB USB common");
1124 MODULE_LICENSE("GPL");