1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include "dvb_usb_common.h"
23#include <media/media-device.h>
24
25static int dvb_usbv2_disable_rc_polling;
26module_param_named(disable_rc_polling, dvb_usbv2_disable_rc_polling, int, 0644);
27MODULE_PARM_DESC(disable_rc_polling,
28 "disable remote control polling (default: 0)");
29static int dvb_usb_force_pid_filter_usage;
30module_param_named(force_pid_filter_usage, dvb_usb_force_pid_filter_usage,
31 int, 0444);
32MODULE_PARM_DESC(force_pid_filter_usage,
33 "force all DVB USB devices to use a PID filter, if any (default: 0)");
34
35static int dvb_usbv2_download_firmware(struct dvb_usb_device *d,
36 const char *name)
37{
38 int ret;
39 const struct firmware *fw;
40 dev_dbg(&d->udev->dev, "%s:\n", __func__);
41
42 if (!d->props->download_firmware) {
43 ret = -EINVAL;
44 goto err;
45 }
46
47 ret = request_firmware(&fw, name, &d->udev->dev);
48 if (ret < 0) {
49 dev_err(&d->udev->dev,
50 "%s: Did not find the firmware file '%s' (status %d). You can use <kernel_dir>/scripts/get_dvb_firmware to get the firmware\n",
51 KBUILD_MODNAME, name, ret);
52 goto err;
53 }
54
55 dev_info(&d->udev->dev, "%s: downloading firmware from file '%s'\n",
56 KBUILD_MODNAME, name);
57
58 ret = d->props->download_firmware(d, fw);
59 release_firmware(fw);
60 if (ret < 0)
61 goto err;
62
63 return ret;
64err:
65 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
66 return ret;
67}
68
69static int dvb_usbv2_i2c_init(struct dvb_usb_device *d)
70{
71 int ret;
72 dev_dbg(&d->udev->dev, "%s:\n", __func__);
73
74 if (!d->props->i2c_algo)
75 return 0;
76
77 strscpy(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);
81
82 ret = i2c_add_adapter(&d->i2c_adap);
83 if (ret < 0) {
84 d->i2c_adap.algo = NULL;
85 goto err;
86 }
87
88 return 0;
89err:
90 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
91 return ret;
92}
93
94static int dvb_usbv2_i2c_exit(struct dvb_usb_device *d)
95{
96 dev_dbg(&d->udev->dev, "%s:\n", __func__);
97
98 if (d->i2c_adap.algo)
99 i2c_del_adapter(&d->i2c_adap);
100
101 return 0;
102}
103
104#if IS_ENABLED(CONFIG_RC_CORE)
105static void dvb_usb_read_remote_control(struct work_struct *work)
106{
107 struct dvb_usb_device *d = container_of(work,
108 struct dvb_usb_device, rc_query_work.work);
109 int ret;
110
111
112
113
114
115 if (dvb_usbv2_disable_rc_polling || d->rc.bulk_mode) {
116 d->rc_polling_active = false;
117 return;
118 }
119
120 ret = d->rc.query(d);
121 if (ret < 0) {
122 dev_err(&d->udev->dev, "%s: rc.query() failed=%d\n",
123 KBUILD_MODNAME, ret);
124 d->rc_polling_active = false;
125 return;
126 }
127
128 schedule_delayed_work(&d->rc_query_work,
129 msecs_to_jiffies(d->rc.interval));
130}
131
132static int dvb_usbv2_remote_init(struct dvb_usb_device *d)
133{
134 int ret;
135 struct rc_dev *dev;
136 dev_dbg(&d->udev->dev, "%s:\n", __func__);
137
138 if (dvb_usbv2_disable_rc_polling || !d->props->get_rc_config)
139 return 0;
140
141 d->rc.map_name = d->rc_map;
142 ret = d->props->get_rc_config(d, &d->rc);
143 if (ret < 0)
144 goto err;
145
146
147 if (!d->rc.map_name)
148 return 0;
149
150 dev = rc_allocate_device(d->rc.driver_type);
151 if (!dev) {
152 ret = -ENOMEM;
153 goto err;
154 }
155
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;
166 dev->priv = d;
167
168 ret = rc_register_device(dev);
169 if (ret < 0) {
170 rc_free_device(dev);
171 goto err;
172 }
173
174 d->rc_dev = dev;
175
176
177 if (d->rc.query && !d->rc.bulk_mode) {
178
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;
187 }
188
189 return 0;
190err:
191 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
192 return ret;
193}
194
195static int dvb_usbv2_remote_exit(struct dvb_usb_device *d)
196{
197 dev_dbg(&d->udev->dev, "%s:\n", __func__);
198
199 if (d->rc_dev) {
200 cancel_delayed_work_sync(&d->rc_query_work);
201 rc_unregister_device(d->rc_dev);
202 d->rc_dev = NULL;
203 }
204
205 return 0;
206}
207#else
208 #define dvb_usbv2_remote_init(args...) 0
209 #define dvb_usbv2_remote_exit(args...)
210#endif
211
212static void dvb_usb_data_complete(struct usb_data_stream *stream, u8 *buf,
213 size_t len)
214{
215 struct dvb_usb_adapter *adap = stream->user_priv;
216 dvb_dmx_swfilter(&adap->demux, buf, len);
217}
218
219static void dvb_usb_data_complete_204(struct usb_data_stream *stream, u8 *buf,
220 size_t len)
221{
222 struct dvb_usb_adapter *adap = stream->user_priv;
223 dvb_dmx_swfilter_204(&adap->demux, buf, len);
224}
225
226static void dvb_usb_data_complete_raw(struct usb_data_stream *stream, u8 *buf,
227 size_t len)
228{
229 struct dvb_usb_adapter *adap = stream->user_priv;
230 dvb_dmx_swfilter_raw(&adap->demux, buf, len);
231}
232
233static int dvb_usbv2_adapter_stream_init(struct dvb_usb_adapter *adap)
234{
235 dev_dbg(&adap_to_d(adap)->udev->dev, "%s: adap=%d\n", __func__,
236 adap->id);
237
238 adap->stream.udev = adap_to_d(adap)->udev;
239 adap->stream.user_priv = adap;
240 adap->stream.complete = dvb_usb_data_complete;
241
242 return usb_urb_initv2(&adap->stream, &adap->props->stream);
243}
244
245static int dvb_usbv2_adapter_stream_exit(struct dvb_usb_adapter *adap)
246{
247 dev_dbg(&adap_to_d(adap)->udev->dev, "%s: adap=%d\n", __func__,
248 adap->id);
249
250 return usb_urb_exitv2(&adap->stream);
251}
252
253static int dvb_usb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
254{
255 struct dvb_usb_adapter *adap = dvbdmxfeed->demux->priv;
256 struct dvb_usb_device *d = adap_to_d(adap);
257 int ret = 0;
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);
264
265
266 wait_on_bit(&adap->state_bits, ADAP_INIT, TASK_UNINTERRUPTIBLE);
267
268 if (adap->active_fe == -1)
269 return -EINVAL;
270
271
272 if (adap->feed_count++ > 0)
273 goto skip_feed_start;
274
275
276 set_bit(ADAP_STREAMING, &adap->state_bits);
277
278
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);
284 if (ret)
285 dev_err(&d->udev->dev,
286 "%s: get_stream_config() failed=%d\n",
287 KBUILD_MODNAME, ret);
288 } else {
289 stream_props = adap->props->stream;
290 }
291
292 switch (adap->ts_type) {
293 case DVB_USB_FE_TS_TYPE_204:
294 adap->stream.complete = dvb_usb_data_complete_204;
295 break;
296 case DVB_USB_FE_TS_TYPE_RAW:
297 adap->stream.complete = dvb_usb_data_complete_raw;
298 break;
299 case DVB_USB_FE_TS_TYPE_188:
300 default:
301 adap->stream.complete = dvb_usb_data_complete;
302 break;
303 }
304
305
306 usb_urb_submitv2(&adap->stream, &stream_props);
307
308
309 if (adap->pid_filtering && adap->props->pid_filter_ctrl) {
310 ret = adap->props->pid_filter_ctrl(adap, 1);
311 if (ret)
312 dev_err(&d->udev->dev,
313 "%s: pid_filter_ctrl() failed=%d\n",
314 KBUILD_MODNAME, ret);
315 }
316
317
318 if (d->props->streaming_ctrl) {
319 ret = d->props->streaming_ctrl(adap->fe[adap->active_fe], 1);
320 if (ret)
321 dev_err(&d->udev->dev,
322 "%s: streaming_ctrl() failed=%d\n",
323 KBUILD_MODNAME, ret);
324 }
325skip_feed_start:
326
327
328 if (adap->pid_filtering && adap->props->pid_filter) {
329 ret = adap->props->pid_filter(adap, dvbdmxfeed->index,
330 dvbdmxfeed->pid, 1);
331 if (ret)
332 dev_err(&d->udev->dev, "%s: pid_filter() failed=%d\n",
333 KBUILD_MODNAME, ret);
334 }
335
336 if (ret)
337 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
338 return ret;
339}
340
341static int dvb_usb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
342{
343 struct dvb_usb_adapter *adap = dvbdmxfeed->demux->priv;
344 struct dvb_usb_device *d = adap_to_d(adap);
345 int ret = 0;
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);
351
352 if (adap->active_fe == -1)
353 return -EINVAL;
354
355
356 if (adap->pid_filtering && adap->props->pid_filter) {
357 ret = adap->props->pid_filter(adap, dvbdmxfeed->index,
358 dvbdmxfeed->pid, 0);
359 if (ret)
360 dev_err(&d->udev->dev, "%s: pid_filter() failed=%d\n",
361 KBUILD_MODNAME, ret);
362 }
363
364
365 if (--adap->feed_count > 0)
366 goto skip_feed_stop;
367
368
369 if (d->props->streaming_ctrl) {
370 ret = d->props->streaming_ctrl(adap->fe[adap->active_fe], 0);
371 if (ret)
372 dev_err(&d->udev->dev,
373 "%s: streaming_ctrl() failed=%d\n",
374 KBUILD_MODNAME, ret);
375 }
376
377
378 if (adap->pid_filtering && adap->props->pid_filter_ctrl) {
379 ret = adap->props->pid_filter_ctrl(adap, 0);
380 if (ret)
381 dev_err(&d->udev->dev,
382 "%s: pid_filter_ctrl() failed=%d\n",
383 KBUILD_MODNAME, ret);
384 }
385
386
387 usb_urb_killv2(&adap->stream);
388
389
390 clear_bit(ADAP_STREAMING, &adap->state_bits);
391 smp_mb__after_atomic();
392 wake_up_bit(&adap->state_bits, ADAP_STREAMING);
393skip_feed_stop:
394
395 if (ret)
396 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
397 return ret;
398}
399
400static int dvb_usbv2_media_device_init(struct dvb_usb_adapter *adap)
401{
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;
406
407 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
408 if (!mdev)
409 return -ENOMEM;
410
411 media_device_usb_init(mdev, udev, d->name);
412
413 dvb_register_media_controller(&adap->dvb_adap, mdev);
414
415 dev_info(&d->udev->dev, "media controller created\n");
416#endif
417 return 0;
418}
419
420static int dvb_usbv2_media_device_register(struct dvb_usb_adapter *adap)
421{
422#ifdef CONFIG_MEDIA_CONTROLLER_DVB
423 return media_device_register(adap->dvb_adap.mdev);
424#else
425 return 0;
426#endif
427}
428
429static void dvb_usbv2_media_device_unregister(struct dvb_usb_adapter *adap)
430{
431#ifdef CONFIG_MEDIA_CONTROLLER_DVB
432
433 if (!adap->dvb_adap.mdev)
434 return;
435
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;
440
441#endif
442}
443
444static int dvb_usbv2_adapter_dvb_init(struct dvb_usb_adapter *adap)
445{
446 int ret;
447 struct dvb_usb_device *d = adap_to_d(adap);
448
449 dev_dbg(&d->udev->dev, "%s: adap=%d\n", __func__, adap->id);
450
451 ret = dvb_register_adapter(&adap->dvb_adap, d->name, d->props->owner,
452 &d->udev->dev, d->props->adapter_nr);
453 if (ret < 0) {
454 dev_dbg(&d->udev->dev, "%s: dvb_register_adapter() failed=%d\n",
455 __func__, ret);
456 goto err_dvb_register_adapter;
457 }
458
459 adap->dvb_adap.priv = adap;
460
461 ret = dvb_usbv2_media_device_init(adap);
462 if (ret < 0) {
463 dev_dbg(&d->udev->dev, "%s: dvb_usbv2_media_device_init() failed=%d\n",
464 __func__, ret);
465 goto err_dvb_register_mc;
466 }
467
468 if (d->props->read_mac_address) {
469 ret = d->props->read_mac_address(adap,
470 adap->dvb_adap.proposed_mac);
471 if (ret < 0)
472 goto err_dvb_dmx_init;
473
474 dev_info(&d->udev->dev, "%s: MAC address: %pM\n",
475 KBUILD_MODNAME, adap->dvb_adap.proposed_mac);
476 }
477
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);
487 if (ret < 0) {
488 dev_err(&d->udev->dev, "%s: dvb_dmx_init() failed=%d\n",
489 KBUILD_MODNAME, ret);
490 goto err_dvb_dmx_init;
491 }
492
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);
497 if (ret < 0) {
498 dev_err(&d->udev->dev, "%s: dvb_dmxdev_init() failed=%d\n",
499 KBUILD_MODNAME, ret);
500 goto err_dvb_dmxdev_init;
501 }
502
503 ret = dvb_net_init(&adap->dvb_adap, &adap->dvb_net, &adap->demux.dmx);
504 if (ret < 0) {
505 dev_err(&d->udev->dev, "%s: dvb_net_init() failed=%d\n",
506 KBUILD_MODNAME, ret);
507 goto err_dvb_net_init;
508 }
509
510 return 0;
511err_dvb_net_init:
512 dvb_dmxdev_release(&adap->dmxdev);
513err_dvb_dmxdev_init:
514 dvb_dmx_release(&adap->demux);
515err_dvb_dmx_init:
516 dvb_usbv2_media_device_unregister(adap);
517err_dvb_register_mc:
518 dvb_unregister_adapter(&adap->dvb_adap);
519err_dvb_register_adapter:
520 adap->dvb_adap.priv = NULL;
521 return ret;
522}
523
524static int dvb_usbv2_adapter_dvb_exit(struct dvb_usb_adapter *adap)
525{
526 dev_dbg(&adap_to_d(adap)->udev->dev, "%s: adap=%d\n", __func__,
527 adap->id);
528
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);
535 }
536
537 return 0;
538}
539
540static int dvb_usbv2_device_power_ctrl(struct dvb_usb_device *d, int onoff)
541{
542 int ret;
543
544 if (onoff)
545 d->powered++;
546 else
547 d->powered--;
548
549 if (d->powered == 0 || (onoff && d->powered == 1)) {
550
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);
554 if (ret < 0)
555 goto err;
556 }
557 }
558
559 return 0;
560err:
561 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
562 return ret;
563}
564
565static int dvb_usb_fe_init(struct dvb_frontend *fe)
566{
567 int ret;
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,
571 fe->id);
572
573 if (!adap->suspend_resume_active) {
574 adap->active_fe = fe->id;
575 set_bit(ADAP_INIT, &adap->state_bits);
576 }
577
578 ret = dvb_usbv2_device_power_ctrl(d, 1);
579 if (ret < 0)
580 goto err;
581
582 if (d->props->frontend_ctrl) {
583 ret = d->props->frontend_ctrl(fe, 1);
584 if (ret < 0)
585 goto err;
586 }
587
588 if (adap->fe_init[fe->id]) {
589 ret = adap->fe_init[fe->id](fe);
590 if (ret < 0)
591 goto err;
592 }
593err:
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);
598 }
599
600 dev_dbg(&d->udev->dev, "%s: ret=%d\n", __func__, ret);
601 return ret;
602}
603
604static int dvb_usb_fe_sleep(struct dvb_frontend *fe)
605{
606 int ret;
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,
610 fe->id);
611
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);
616 }
617
618 if (adap->fe_sleep[fe->id]) {
619 ret = adap->fe_sleep[fe->id](fe);
620 if (ret < 0)
621 goto err;
622 }
623
624 if (d->props->frontend_ctrl) {
625 ret = d->props->frontend_ctrl(fe, 0);
626 if (ret < 0)
627 goto err;
628 }
629
630 ret = dvb_usbv2_device_power_ctrl(d, 0);
631
632err:
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);
638 }
639
640 dev_dbg(&d->udev->dev, "%s: ret=%d\n", __func__, ret);
641 return ret;
642}
643
644static int dvb_usbv2_adapter_frontend_init(struct dvb_usb_adapter *adap)
645{
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);
649
650 memset(adap->fe, 0, sizeof(adap->fe));
651 adap->active_fe = -1;
652
653 if (d->props->frontend_attach) {
654 ret = d->props->frontend_attach(adap);
655 if (ret < 0) {
656 dev_dbg(&d->udev->dev,
657 "%s: frontend_attach() failed=%d\n",
658 __func__, ret);
659 goto err_dvb_frontend_detach;
660 }
661 } else {
662 dev_dbg(&d->udev->dev, "%s: frontend_attach() do not exists\n",
663 __func__);
664 ret = 0;
665 goto err;
666 }
667
668 for (i = 0; i < MAX_NO_OF_FE_PER_ADAP && adap->fe[i]; i++) {
669 adap->fe[i]->id = i;
670
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;
675
676 ret = dvb_register_frontend(&adap->dvb_adap, adap->fe[i]);
677 if (ret < 0) {
678 dev_err(&d->udev->dev,
679 "%s: frontend%d registration failed\n",
680 KBUILD_MODNAME, i);
681 goto err_dvb_unregister_frontend;
682 }
683
684 count_registered++;
685 }
686
687 if (d->props->tuner_attach) {
688 ret = d->props->tuner_attach(adap);
689 if (ret < 0) {
690 dev_dbg(&d->udev->dev, "%s: tuner_attach() failed=%d\n",
691 __func__, ret);
692 goto err_dvb_unregister_frontend;
693 }
694 }
695
696 ret = dvb_create_media_graph(&adap->dvb_adap, true);
697 if (ret < 0)
698 goto err_dvb_unregister_frontend;
699
700 ret = dvb_usbv2_media_device_register(adap);
701
702 return ret;
703
704err_dvb_unregister_frontend:
705 for (i = count_registered - 1; i >= 0; i--)
706 dvb_unregister_frontend(adap->fe[i]);
707
708err_dvb_frontend_detach:
709 for (i = MAX_NO_OF_FE_PER_ADAP - 1; i >= 0; i--) {
710 if (adap->fe[i]) {
711 dvb_frontend_detach(adap->fe[i]);
712 adap->fe[i] = NULL;
713 }
714 }
715
716err:
717 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
718 return ret;
719}
720
721static int dvb_usbv2_adapter_frontend_exit(struct dvb_usb_adapter *adap)
722{
723 int ret, i;
724 struct dvb_usb_device *d = adap_to_d(adap);
725
726 dev_dbg(&d->udev->dev, "%s: adap=%d\n", __func__, adap->id);
727
728 for (i = MAX_NO_OF_FE_PER_ADAP - 1; i >= 0; i--) {
729 if (adap->fe[i]) {
730 dvb_unregister_frontend(adap->fe[i]);
731 dvb_frontend_detach(adap->fe[i]);
732 }
733 }
734
735 if (d->props->tuner_detach) {
736 ret = d->props->tuner_detach(adap);
737 if (ret < 0) {
738 dev_dbg(&d->udev->dev, "%s: tuner_detach() failed=%d\n",
739 __func__, ret);
740 }
741 }
742
743 if (d->props->frontend_detach) {
744 ret = d->props->frontend_detach(adap);
745 if (ret < 0) {
746 dev_dbg(&d->udev->dev,
747 "%s: frontend_detach() failed=%d\n",
748 __func__, ret);
749 }
750 }
751
752 return 0;
753}
754
755static int dvb_usbv2_adapter_init(struct dvb_usb_device *d)
756{
757 struct dvb_usb_adapter *adap;
758 int ret, i, adapter_count;
759
760
761 adapter_count = d->props->num_adapters;
762 if (d->props->get_adapter_count) {
763 ret = d->props->get_adapter_count(d);
764 if (ret < 0)
765 goto err;
766
767 adapter_count = ret;
768 }
769
770 for (i = 0; i < adapter_count; i++) {
771 adap = &d->adapter[i];
772 adap->id = i;
773 adap->props = &d->props->adapter[i];
774
775
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",
780 KBUILD_MODNAME);
781 ret = -ENODEV;
782 goto err;
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",
788 KBUILD_MODNAME,
789 adap->props->pid_filter_count);
790 adap->pid_filtering = 1;
791 adap->max_feed_count = adap->props->pid_filter_count;
792 } else {
793 dev_info(&d->udev->dev,
794 "%s: will pass the complete MPEG2 transport stream to the software demuxer\n",
795 KBUILD_MODNAME);
796 adap->pid_filtering = 0;
797 adap->max_feed_count = 255;
798 }
799
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",
804 KBUILD_MODNAME);
805 adap->pid_filtering = 1;
806 adap->max_feed_count = adap->props->pid_filter_count;
807 }
808
809 ret = dvb_usbv2_adapter_stream_init(adap);
810 if (ret)
811 goto err;
812
813 ret = dvb_usbv2_adapter_dvb_init(adap);
814 if (ret)
815 goto err;
816
817 ret = dvb_usbv2_adapter_frontend_init(adap);
818 if (ret)
819 goto err;
820
821
822 if (adap->fe[1])
823 adap->dvb_adap.mfe_shared = 1;
824 }
825
826 return 0;
827err:
828 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
829 return ret;
830}
831
832static int dvb_usbv2_adapter_exit(struct dvb_usb_device *d)
833{
834 int i;
835 dev_dbg(&d->udev->dev, "%s:\n", __func__);
836
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]);
843 }
844 }
845
846 return 0;
847}
848
849
850static int dvb_usbv2_exit(struct dvb_usb_device *d)
851{
852 dev_dbg(&d->udev->dev, "%s:\n", __func__);
853
854 dvb_usbv2_remote_exit(d);
855 dvb_usbv2_adapter_exit(d);
856 dvb_usbv2_i2c_exit(d);
857
858 return 0;
859}
860
861static int dvb_usbv2_init(struct dvb_usb_device *d)
862{
863 int ret;
864 dev_dbg(&d->udev->dev, "%s:\n", __func__);
865
866 dvb_usbv2_device_power_ctrl(d, 1);
867
868 if (d->props->read_config) {
869 ret = d->props->read_config(d);
870 if (ret < 0)
871 goto err;
872 }
873
874 ret = dvb_usbv2_i2c_init(d);
875 if (ret < 0)
876 goto err;
877
878 ret = dvb_usbv2_adapter_init(d);
879 if (ret < 0)
880 goto err;
881
882 if (d->props->init) {
883 ret = d->props->init(d);
884 if (ret < 0)
885 goto err;
886 }
887
888 ret = dvb_usbv2_remote_init(d);
889 if (ret < 0)
890 goto err;
891
892 dvb_usbv2_device_power_ctrl(d, 0);
893
894 return 0;
895err:
896 dvb_usbv2_device_power_ctrl(d, 0);
897 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
898 return ret;
899}
900
901int dvb_usbv2_probe(struct usb_interface *intf,
902 const struct usb_device_id *id)
903{
904 int ret;
905 struct dvb_usb_device *d;
906 struct usb_device *udev = interface_to_usbdev(intf);
907 struct dvb_usb_driver_info *driver_info =
908 (struct dvb_usb_driver_info *) id->driver_info;
909
910 dev_dbg(&udev->dev, "%s: bInterfaceNumber=%d\n", __func__,
911 intf->cur_altsetting->desc.bInterfaceNumber);
912
913 if (!id->driver_info) {
914 dev_err(&udev->dev, "%s: driver_info failed\n", KBUILD_MODNAME);
915 ret = -ENODEV;
916 goto err;
917 }
918
919 d = kzalloc(sizeof(struct dvb_usb_device), GFP_KERNEL);
920 if (!d) {
921 dev_err(&udev->dev, "%s: kzalloc() failed\n", KBUILD_MODNAME);
922 ret = -ENOMEM;
923 goto err;
924 }
925
926 d->intf = intf;
927 d->name = driver_info->name;
928 d->rc_map = driver_info->rc_map;
929 d->udev = udev;
930 d->props = driver_info->props;
931
932 if (intf->cur_altsetting->desc.bInterfaceNumber !=
933 d->props->bInterfaceNumber) {
934 ret = -ENODEV;
935 goto err_kfree_d;
936 }
937
938 mutex_init(&d->usb_mutex);
939 mutex_init(&d->i2c_mutex);
940
941 if (d->props->size_of_priv) {
942 d->priv = kzalloc(d->props->size_of_priv, GFP_KERNEL);
943 if (!d->priv) {
944 dev_err(&d->udev->dev, "%s: kzalloc() failed\n",
945 KBUILD_MODNAME);
946 ret = -ENOMEM;
947 goto err_kfree_d;
948 }
949 }
950
951 if (d->props->probe) {
952 ret = d->props->probe(d);
953 if (ret)
954 goto err_kfree_priv;
955 }
956
957 if (d->props->identify_state) {
958 const char *name = NULL;
959 ret = d->props->identify_state(d, &name);
960 if (ret == COLD) {
961 dev_info(&d->udev->dev,
962 "%s: found a '%s' in cold state\n",
963 KBUILD_MODNAME, d->name);
964
965 if (!name)
966 name = d->props->firmware;
967
968 ret = dvb_usbv2_download_firmware(d, name);
969 if (ret == 0) {
970
971 ;
972 } else if (ret == RECONNECTS_USB) {
973
974
975
976
977
978
979
980
981 goto exit;
982 } else {
983 goto err_free_all;
984 }
985 } else if (ret != WARM) {
986 goto err_free_all;
987 }
988 }
989
990 dev_info(&d->udev->dev, "%s: found a '%s' in warm state\n",
991 KBUILD_MODNAME, d->name);
992
993 ret = dvb_usbv2_init(d);
994 if (ret < 0)
995 goto err_free_all;
996
997 dev_info(&d->udev->dev,
998 "%s: '%s' successfully initialized and connected\n",
999 KBUILD_MODNAME, d->name);
1000exit:
1001 usb_set_intfdata(intf, d);
1002
1003 return 0;
1004err_free_all:
1005 dvb_usbv2_exit(d);
1006 if (d->props->disconnect)
1007 d->props->disconnect(d);
1008err_kfree_priv:
1009 kfree(d->priv);
1010err_kfree_d:
1011 kfree(d);
1012err:
1013 dev_dbg(&udev->dev, "%s: failed=%d\n", __func__, ret);
1014 return ret;
1015}
1016EXPORT_SYMBOL(dvb_usbv2_probe);
1017
1018void dvb_usbv2_disconnect(struct usb_interface *intf)
1019{
1020 struct dvb_usb_device *d = usb_get_intfdata(intf);
1021 const char *devname = kstrdup(dev_name(&d->udev->dev), GFP_KERNEL);
1022 const char *drvname = d->name;
1023
1024 dev_dbg(&d->udev->dev, "%s: bInterfaceNumber=%d\n", __func__,
1025 intf->cur_altsetting->desc.bInterfaceNumber);
1026
1027 if (d->props->exit)
1028 d->props->exit(d);
1029
1030 dvb_usbv2_exit(d);
1031
1032 if (d->props->disconnect)
1033 d->props->disconnect(d);
1034
1035 kfree(d->priv);
1036 kfree(d);
1037
1038 pr_info("%s: '%s:%s' successfully deinitialized and disconnected\n",
1039 KBUILD_MODNAME, drvname, devname);
1040 kfree(devname);
1041}
1042EXPORT_SYMBOL(dvb_usbv2_disconnect);
1043
1044int dvb_usbv2_suspend(struct usb_interface *intf, pm_message_t msg)
1045{
1046 struct dvb_usb_device *d = usb_get_intfdata(intf);
1047 int ret = 0, i, active_fe;
1048 struct dvb_frontend *fe;
1049 dev_dbg(&d->udev->dev, "%s:\n", __func__);
1050
1051
1052 if (d->rc_polling_active)
1053 cancel_delayed_work_sync(&d->rc_query_work);
1054
1055 for (i = MAX_NO_OF_ADAPTER_PER_DEVICE - 1; i >= 0; i--) {
1056 active_fe = d->adapter[i].active_fe;
1057 if (d->adapter[i].dvb_adap.priv && active_fe != -1) {
1058 fe = d->adapter[i].fe[active_fe];
1059 d->adapter[i].suspend_resume_active = true;
1060
1061 if (d->props->streaming_ctrl)
1062 d->props->streaming_ctrl(fe, 0);
1063
1064
1065 usb_urb_killv2(&d->adapter[i].stream);
1066
1067 ret = dvb_frontend_suspend(fe);
1068 }
1069 }
1070
1071 return ret;
1072}
1073EXPORT_SYMBOL(dvb_usbv2_suspend);
1074
1075static int dvb_usbv2_resume_common(struct dvb_usb_device *d)
1076{
1077 int ret = 0, i, active_fe;
1078 struct dvb_frontend *fe;
1079 dev_dbg(&d->udev->dev, "%s:\n", __func__);
1080
1081 for (i = 0; i < MAX_NO_OF_ADAPTER_PER_DEVICE; i++) {
1082 active_fe = d->adapter[i].active_fe;
1083 if (d->adapter[i].dvb_adap.priv && active_fe != -1) {
1084 fe = d->adapter[i].fe[active_fe];
1085
1086 ret = dvb_frontend_resume(fe);
1087
1088
1089 usb_urb_submitv2(&d->adapter[i].stream, NULL);
1090
1091 if (d->props->streaming_ctrl)
1092 d->props->streaming_ctrl(fe, 1);
1093
1094 d->adapter[i].suspend_resume_active = false;
1095 }
1096 }
1097
1098
1099 if (d->rc_polling_active)
1100 schedule_delayed_work(&d->rc_query_work,
1101 msecs_to_jiffies(d->rc.interval));
1102
1103 return ret;
1104}
1105
1106int dvb_usbv2_resume(struct usb_interface *intf)
1107{
1108 struct dvb_usb_device *d = usb_get_intfdata(intf);
1109 dev_dbg(&d->udev->dev, "%s:\n", __func__);
1110
1111 return dvb_usbv2_resume_common(d);
1112}
1113EXPORT_SYMBOL(dvb_usbv2_resume);
1114
1115int dvb_usbv2_reset_resume(struct usb_interface *intf)
1116{
1117 struct dvb_usb_device *d = usb_get_intfdata(intf);
1118 int ret;
1119 dev_dbg(&d->udev->dev, "%s:\n", __func__);
1120
1121 dvb_usbv2_device_power_ctrl(d, 1);
1122
1123 if (d->props->init)
1124 d->props->init(d);
1125
1126 ret = dvb_usbv2_resume_common(d);
1127
1128 dvb_usbv2_device_power_ctrl(d, 0);
1129
1130 return ret;
1131}
1132EXPORT_SYMBOL(dvb_usbv2_reset_resume);
1133
1134MODULE_VERSION("2.0");
1135MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@posteo.de>");
1136MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1137MODULE_DESCRIPTION("DVB USB common");
1138MODULE_LICENSE("GPL");
1139