1 /*
2 * DVB USB framework
3 *
4 * Copyright (C) 2004-6 Patrick Boettcher <patrick.boettcher@posteo.de>
5 * Copyright (C) 2012 Antti Palosaari <crope@iki.fi>
6 *
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.
11 *
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.
16 *
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.
20 */
21
22 #include "dvb_usb_common.h"
23 #include <media/media-device.h>
24
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,
31 int, 0444);
32 MODULE_PARM_DESC(force_pid_filter_usage,
33 "force all DVB USB devices to use a PID filter, if any (default: 0)");
34
dvb_usbv2_download_firmware(struct dvb_usb_device * d,const char * name)35 static 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'. Please see linux/Documentation/dvb/ for more details on firmware-problems. Status %d\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;
64 err:
65 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
66 return ret;
67 }
68
dvb_usbv2_i2c_init(struct dvb_usb_device * d)69 static 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 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);
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;
89 err:
90 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
91 return ret;
92 }
93
dvb_usbv2_i2c_exit(struct dvb_usb_device * d)94 static 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)
dvb_usb_read_remote_control(struct work_struct * work)105 static 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 * 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.
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; /* stop polling */
126 }
127
128 schedule_delayed_work(&d->rc_query_work,
129 msecs_to_jiffies(d->rc.interval));
130 }
131
dvb_usbv2_remote_init(struct dvb_usb_device * d)132 static 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 /* disable rc when there is no keymap defined */
147 if (!d->rc.map_name)
148 return 0;
149
150 dev = rc_allocate_device();
151 if (!dev) {
152 ret = -ENOMEM;
153 goto err;
154 }
155
156 dev->dev.parent = &d->udev->dev;
157 dev->input_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 /* TODO: likely RC-core should took const char * */
163 dev->driver_name = (char *) d->props->driver_name;
164 dev->map_name = d->rc.map_name;
165 dev->driver_type = d->rc.driver_type;
166 dev->allowed_protocols = d->rc.allowed_protos;
167 dev->change_protocol = d->rc.change_protocol;
168 dev->priv = d;
169
170 ret = rc_register_device(dev);
171 if (ret < 0) {
172 rc_free_device(dev);
173 goto err;
174 }
175
176 d->rc_dev = dev;
177
178 /* start polling if needed */
179 if (d->rc.query && !d->rc.bulk_mode) {
180 /* initialize a work queue for handling polling */
181 INIT_DELAYED_WORK(&d->rc_query_work,
182 dvb_usb_read_remote_control);
183 dev_info(&d->udev->dev,
184 "%s: schedule remote query interval to %d msecs\n",
185 KBUILD_MODNAME, d->rc.interval);
186 schedule_delayed_work(&d->rc_query_work,
187 msecs_to_jiffies(d->rc.interval));
188 d->rc_polling_active = true;
189 }
190
191 return 0;
192 err:
193 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
194 return ret;
195 }
196
dvb_usbv2_remote_exit(struct dvb_usb_device * d)197 static int dvb_usbv2_remote_exit(struct dvb_usb_device *d)
198 {
199 dev_dbg(&d->udev->dev, "%s:\n", __func__);
200
201 if (d->rc_dev) {
202 cancel_delayed_work_sync(&d->rc_query_work);
203 rc_unregister_device(d->rc_dev);
204 d->rc_dev = NULL;
205 }
206
207 return 0;
208 }
209 #else
210 #define dvb_usbv2_remote_init(args...) 0
211 #define dvb_usbv2_remote_exit(args...)
212 #endif
213
dvb_usb_data_complete(struct usb_data_stream * stream,u8 * buf,size_t len)214 static void dvb_usb_data_complete(struct usb_data_stream *stream, u8 *buf,
215 size_t len)
216 {
217 struct dvb_usb_adapter *adap = stream->user_priv;
218 dvb_dmx_swfilter(&adap->demux, buf, len);
219 }
220
dvb_usb_data_complete_204(struct usb_data_stream * stream,u8 * buf,size_t len)221 static void dvb_usb_data_complete_204(struct usb_data_stream *stream, u8 *buf,
222 size_t len)
223 {
224 struct dvb_usb_adapter *adap = stream->user_priv;
225 dvb_dmx_swfilter_204(&adap->demux, buf, len);
226 }
227
dvb_usb_data_complete_raw(struct usb_data_stream * stream,u8 * buf,size_t len)228 static void dvb_usb_data_complete_raw(struct usb_data_stream *stream, u8 *buf,
229 size_t len)
230 {
231 struct dvb_usb_adapter *adap = stream->user_priv;
232 dvb_dmx_swfilter_raw(&adap->demux, buf, len);
233 }
234
dvb_usbv2_adapter_stream_init(struct dvb_usb_adapter * adap)235 static int dvb_usbv2_adapter_stream_init(struct dvb_usb_adapter *adap)
236 {
237 dev_dbg(&adap_to_d(adap)->udev->dev, "%s: adap=%d\n", __func__,
238 adap->id);
239
240 adap->stream.udev = adap_to_d(adap)->udev;
241 adap->stream.user_priv = adap;
242 adap->stream.complete = dvb_usb_data_complete;
243
244 return usb_urb_initv2(&adap->stream, &adap->props->stream);
245 }
246
dvb_usbv2_adapter_stream_exit(struct dvb_usb_adapter * adap)247 static int dvb_usbv2_adapter_stream_exit(struct dvb_usb_adapter *adap)
248 {
249 dev_dbg(&adap_to_d(adap)->udev->dev, "%s: adap=%d\n", __func__,
250 adap->id);
251
252 return usb_urb_exitv2(&adap->stream);
253 }
254
dvb_usb_start_feed(struct dvb_demux_feed * dvbdmxfeed)255 static int dvb_usb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
256 {
257 struct dvb_usb_adapter *adap = dvbdmxfeed->demux->priv;
258 struct dvb_usb_device *d = adap_to_d(adap);
259 int ret = 0;
260 struct usb_data_stream_properties stream_props;
261 dev_dbg(&d->udev->dev,
262 "%s: adap=%d active_fe=%d feed_type=%d setting pid [%s]: %04x (%04d) at index %d\n",
263 __func__, adap->id, adap->active_fe, dvbdmxfeed->type,
264 adap->pid_filtering ? "yes" : "no", dvbdmxfeed->pid,
265 dvbdmxfeed->pid, dvbdmxfeed->index);
266
267 /* wait init is done */
268 wait_on_bit(&adap->state_bits, ADAP_INIT, TASK_UNINTERRUPTIBLE);
269
270 if (adap->active_fe == -1)
271 return -EINVAL;
272
273 /* skip feed setup if we are already feeding */
274 if (adap->feed_count++ > 0)
275 goto skip_feed_start;
276
277 /* set 'streaming' status bit */
278 set_bit(ADAP_STREAMING, &adap->state_bits);
279
280 /* resolve input and output streaming parameters */
281 if (d->props->get_stream_config) {
282 memcpy(&stream_props, &adap->props->stream,
283 sizeof(struct usb_data_stream_properties));
284 ret = d->props->get_stream_config(adap->fe[adap->active_fe],
285 &adap->ts_type, &stream_props);
286 if (ret)
287 dev_err(&d->udev->dev,
288 "%s: get_stream_config() failed=%d\n",
289 KBUILD_MODNAME, ret);
290 } else {
291 stream_props = adap->props->stream;
292 }
293
294 switch (adap->ts_type) {
295 case DVB_USB_FE_TS_TYPE_204:
296 adap->stream.complete = dvb_usb_data_complete_204;
297 break;
298 case DVB_USB_FE_TS_TYPE_RAW:
299 adap->stream.complete = dvb_usb_data_complete_raw;
300 break;
301 case DVB_USB_FE_TS_TYPE_188:
302 default:
303 adap->stream.complete = dvb_usb_data_complete;
304 break;
305 }
306
307 /* submit USB streaming packets */
308 usb_urb_submitv2(&adap->stream, &stream_props);
309
310 /* enable HW PID filter */
311 if (adap->pid_filtering && adap->props->pid_filter_ctrl) {
312 ret = adap->props->pid_filter_ctrl(adap, 1);
313 if (ret)
314 dev_err(&d->udev->dev,
315 "%s: pid_filter_ctrl() failed=%d\n",
316 KBUILD_MODNAME, ret);
317 }
318
319 /* ask device to start streaming */
320 if (d->props->streaming_ctrl) {
321 ret = d->props->streaming_ctrl(adap->fe[adap->active_fe], 1);
322 if (ret)
323 dev_err(&d->udev->dev,
324 "%s: streaming_ctrl() failed=%d\n",
325 KBUILD_MODNAME, ret);
326 }
327 skip_feed_start:
328
329 /* add PID to device HW PID filter */
330 if (adap->pid_filtering && adap->props->pid_filter) {
331 ret = adap->props->pid_filter(adap, dvbdmxfeed->index,
332 dvbdmxfeed->pid, 1);
333 if (ret)
334 dev_err(&d->udev->dev, "%s: pid_filter() failed=%d\n",
335 KBUILD_MODNAME, ret);
336 }
337
338 if (ret)
339 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
340 return ret;
341 }
342
dvb_usb_stop_feed(struct dvb_demux_feed * dvbdmxfeed)343 static int dvb_usb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
344 {
345 struct dvb_usb_adapter *adap = dvbdmxfeed->demux->priv;
346 struct dvb_usb_device *d = adap_to_d(adap);
347 int ret = 0;
348 dev_dbg(&d->udev->dev,
349 "%s: adap=%d active_fe=%d feed_type=%d setting pid [%s]: %04x (%04d) at index %d\n",
350 __func__, adap->id, adap->active_fe, dvbdmxfeed->type,
351 adap->pid_filtering ? "yes" : "no", dvbdmxfeed->pid,
352 dvbdmxfeed->pid, dvbdmxfeed->index);
353
354 if (adap->active_fe == -1)
355 return -EINVAL;
356
357 /* remove PID from device HW PID filter */
358 if (adap->pid_filtering && adap->props->pid_filter) {
359 ret = adap->props->pid_filter(adap, dvbdmxfeed->index,
360 dvbdmxfeed->pid, 0);
361 if (ret)
362 dev_err(&d->udev->dev, "%s: pid_filter() failed=%d\n",
363 KBUILD_MODNAME, ret);
364 }
365
366 /* we cannot stop streaming until last PID is removed */
367 if (--adap->feed_count > 0)
368 goto skip_feed_stop;
369
370 /* ask device to stop streaming */
371 if (d->props->streaming_ctrl) {
372 ret = d->props->streaming_ctrl(adap->fe[adap->active_fe], 0);
373 if (ret)
374 dev_err(&d->udev->dev,
375 "%s: streaming_ctrl() failed=%d\n",
376 KBUILD_MODNAME, ret);
377 }
378
379 /* disable HW PID filter */
380 if (adap->pid_filtering && adap->props->pid_filter_ctrl) {
381 ret = adap->props->pid_filter_ctrl(adap, 0);
382 if (ret)
383 dev_err(&d->udev->dev,
384 "%s: pid_filter_ctrl() failed=%d\n",
385 KBUILD_MODNAME, ret);
386 }
387
388 /* kill USB streaming packets */
389 usb_urb_killv2(&adap->stream);
390
391 /* clear 'streaming' status bit */
392 clear_bit(ADAP_STREAMING, &adap->state_bits);
393 smp_mb__after_atomic();
394 wake_up_bit(&adap->state_bits, ADAP_STREAMING);
395 skip_feed_stop:
396
397 if (ret)
398 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
399 return ret;
400 }
401
dvb_usbv2_media_device_init(struct dvb_usb_adapter * adap)402 static int dvb_usbv2_media_device_init(struct dvb_usb_adapter *adap)
403 {
404 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
405 struct media_device *mdev;
406 struct dvb_usb_device *d = adap_to_d(adap);
407 struct usb_device *udev = d->udev;
408
409 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
410 if (!mdev)
411 return -ENOMEM;
412
413 media_device_usb_init(mdev, udev, d->name);
414
415 dvb_register_media_controller(&adap->dvb_adap, mdev);
416
417 dev_info(&d->udev->dev, "media controller created\n");
418 #endif
419 return 0;
420 }
421
dvb_usbv2_media_device_register(struct dvb_usb_adapter * adap)422 static int dvb_usbv2_media_device_register(struct dvb_usb_adapter *adap)
423 {
424 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
425 return media_device_register(adap->dvb_adap.mdev);
426 #else
427 return 0;
428 #endif
429 }
430
dvb_usbv2_media_device_unregister(struct dvb_usb_adapter * adap)431 static void dvb_usbv2_media_device_unregister(struct dvb_usb_adapter *adap)
432 {
433 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
434
435 if (!adap->dvb_adap.mdev)
436 return;
437
438 media_device_unregister(adap->dvb_adap.mdev);
439 media_device_cleanup(adap->dvb_adap.mdev);
440 kfree(adap->dvb_adap.mdev);
441 adap->dvb_adap.mdev = NULL;
442
443 #endif
444 }
445
dvb_usbv2_adapter_dvb_init(struct dvb_usb_adapter * adap)446 static int dvb_usbv2_adapter_dvb_init(struct dvb_usb_adapter *adap)
447 {
448 int ret;
449 struct dvb_usb_device *d = adap_to_d(adap);
450
451 dev_dbg(&d->udev->dev, "%s: adap=%d\n", __func__, adap->id);
452
453 ret = dvb_register_adapter(&adap->dvb_adap, d->name, d->props->owner,
454 &d->udev->dev, d->props->adapter_nr);
455 if (ret < 0) {
456 dev_dbg(&d->udev->dev, "%s: dvb_register_adapter() failed=%d\n",
457 __func__, ret);
458 goto err_dvb_register_adapter;
459 }
460
461 adap->dvb_adap.priv = adap;
462
463 ret = dvb_usbv2_media_device_init(adap);
464 if (ret < 0) {
465 dev_dbg(&d->udev->dev, "%s: dvb_usbv2_media_device_init() failed=%d\n",
466 __func__, ret);
467 goto err_dvb_register_mc;
468 }
469
470 if (d->props->read_mac_address) {
471 ret = d->props->read_mac_address(adap,
472 adap->dvb_adap.proposed_mac);
473 if (ret < 0)
474 goto err_dvb_dmx_init;
475
476 dev_info(&d->udev->dev, "%s: MAC address: %pM\n",
477 KBUILD_MODNAME, adap->dvb_adap.proposed_mac);
478 }
479
480 adap->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING;
481 adap->demux.priv = adap;
482 adap->demux.filternum = 0;
483 adap->demux.filternum = adap->max_feed_count;
484 adap->demux.feednum = adap->demux.filternum;
485 adap->demux.start_feed = dvb_usb_start_feed;
486 adap->demux.stop_feed = dvb_usb_stop_feed;
487 adap->demux.write_to_decoder = NULL;
488 ret = dvb_dmx_init(&adap->demux);
489 if (ret < 0) {
490 dev_err(&d->udev->dev, "%s: dvb_dmx_init() failed=%d\n",
491 KBUILD_MODNAME, ret);
492 goto err_dvb_dmx_init;
493 }
494
495 adap->dmxdev.filternum = adap->demux.filternum;
496 adap->dmxdev.demux = &adap->demux.dmx;
497 adap->dmxdev.capabilities = 0;
498 ret = dvb_dmxdev_init(&adap->dmxdev, &adap->dvb_adap);
499 if (ret < 0) {
500 dev_err(&d->udev->dev, "%s: dvb_dmxdev_init() failed=%d\n",
501 KBUILD_MODNAME, ret);
502 goto err_dvb_dmxdev_init;
503 }
504
505 ret = dvb_net_init(&adap->dvb_adap, &adap->dvb_net, &adap->demux.dmx);
506 if (ret < 0) {
507 dev_err(&d->udev->dev, "%s: dvb_net_init() failed=%d\n",
508 KBUILD_MODNAME, ret);
509 goto err_dvb_net_init;
510 }
511
512 return 0;
513 err_dvb_net_init:
514 dvb_dmxdev_release(&adap->dmxdev);
515 err_dvb_dmxdev_init:
516 dvb_dmx_release(&adap->demux);
517 err_dvb_dmx_init:
518 dvb_usbv2_media_device_unregister(adap);
519 err_dvb_register_mc:
520 dvb_unregister_adapter(&adap->dvb_adap);
521 err_dvb_register_adapter:
522 adap->dvb_adap.priv = NULL;
523 return ret;
524 }
525
dvb_usbv2_adapter_dvb_exit(struct dvb_usb_adapter * adap)526 static int dvb_usbv2_adapter_dvb_exit(struct dvb_usb_adapter *adap)
527 {
528 dev_dbg(&adap_to_d(adap)->udev->dev, "%s: adap=%d\n", __func__,
529 adap->id);
530
531 if (adap->dvb_adap.priv) {
532 dvb_net_release(&adap->dvb_net);
533 adap->demux.dmx.close(&adap->demux.dmx);
534 dvb_dmxdev_release(&adap->dmxdev);
535 dvb_dmx_release(&adap->demux);
536 dvb_unregister_adapter(&adap->dvb_adap);
537 }
538
539 return 0;
540 }
541
dvb_usbv2_device_power_ctrl(struct dvb_usb_device * d,int onoff)542 static int dvb_usbv2_device_power_ctrl(struct dvb_usb_device *d, int onoff)
543 {
544 int ret;
545
546 if (onoff)
547 d->powered++;
548 else
549 d->powered--;
550
551 if (d->powered == 0 || (onoff && d->powered == 1)) {
552 /* when switching from 1 to 0 or from 0 to 1 */
553 dev_dbg(&d->udev->dev, "%s: power=%d\n", __func__, onoff);
554 if (d->props->power_ctrl) {
555 ret = d->props->power_ctrl(d, onoff);
556 if (ret < 0)
557 goto err;
558 }
559 }
560
561 return 0;
562 err:
563 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
564 return ret;
565 }
566
dvb_usb_fe_init(struct dvb_frontend * fe)567 static int dvb_usb_fe_init(struct dvb_frontend *fe)
568 {
569 int ret;
570 struct dvb_usb_adapter *adap = fe->dvb->priv;
571 struct dvb_usb_device *d = adap_to_d(adap);
572 dev_dbg(&d->udev->dev, "%s: adap=%d fe=%d\n", __func__, adap->id,
573 fe->id);
574
575 if (!adap->suspend_resume_active) {
576 adap->active_fe = fe->id;
577 set_bit(ADAP_INIT, &adap->state_bits);
578 }
579
580 ret = dvb_usbv2_device_power_ctrl(d, 1);
581 if (ret < 0)
582 goto err;
583
584 if (d->props->frontend_ctrl) {
585 ret = d->props->frontend_ctrl(fe, 1);
586 if (ret < 0)
587 goto err;
588 }
589
590 if (adap->fe_init[fe->id]) {
591 ret = adap->fe_init[fe->id](fe);
592 if (ret < 0)
593 goto err;
594 }
595 err:
596 if (!adap->suspend_resume_active) {
597 clear_bit(ADAP_INIT, &adap->state_bits);
598 smp_mb__after_atomic();
599 wake_up_bit(&adap->state_bits, ADAP_INIT);
600 }
601
602 dev_dbg(&d->udev->dev, "%s: ret=%d\n", __func__, ret);
603 return ret;
604 }
605
dvb_usb_fe_sleep(struct dvb_frontend * fe)606 static int dvb_usb_fe_sleep(struct dvb_frontend *fe)
607 {
608 int ret;
609 struct dvb_usb_adapter *adap = fe->dvb->priv;
610 struct dvb_usb_device *d = adap_to_d(adap);
611 dev_dbg(&d->udev->dev, "%s: adap=%d fe=%d\n", __func__, adap->id,
612 fe->id);
613
614 if (!adap->suspend_resume_active) {
615 set_bit(ADAP_SLEEP, &adap->state_bits);
616 wait_on_bit(&adap->state_bits, ADAP_STREAMING,
617 TASK_UNINTERRUPTIBLE);
618 }
619
620 if (adap->fe_sleep[fe->id]) {
621 ret = adap->fe_sleep[fe->id](fe);
622 if (ret < 0)
623 goto err;
624 }
625
626 if (d->props->frontend_ctrl) {
627 ret = d->props->frontend_ctrl(fe, 0);
628 if (ret < 0)
629 goto err;
630 }
631
632 ret = dvb_usbv2_device_power_ctrl(d, 0);
633 if (ret < 0)
634 goto err;
635 err:
636 if (!adap->suspend_resume_active) {
637 adap->active_fe = -1;
638 clear_bit(ADAP_SLEEP, &adap->state_bits);
639 smp_mb__after_atomic();
640 wake_up_bit(&adap->state_bits, ADAP_SLEEP);
641 }
642
643 dev_dbg(&d->udev->dev, "%s: ret=%d\n", __func__, ret);
644 return ret;
645 }
646
dvb_usbv2_adapter_frontend_init(struct dvb_usb_adapter * adap)647 static int dvb_usbv2_adapter_frontend_init(struct dvb_usb_adapter *adap)
648 {
649 int ret, i, count_registered = 0;
650 struct dvb_usb_device *d = adap_to_d(adap);
651 dev_dbg(&d->udev->dev, "%s: adap=%d\n", __func__, adap->id);
652
653 memset(adap->fe, 0, sizeof(adap->fe));
654 adap->active_fe = -1;
655
656 if (d->props->frontend_attach) {
657 ret = d->props->frontend_attach(adap);
658 if (ret < 0) {
659 dev_dbg(&d->udev->dev,
660 "%s: frontend_attach() failed=%d\n",
661 __func__, ret);
662 goto err_dvb_frontend_detach;
663 }
664 } else {
665 dev_dbg(&d->udev->dev, "%s: frontend_attach() do not exists\n",
666 __func__);
667 ret = 0;
668 goto err;
669 }
670
671 for (i = 0; i < MAX_NO_OF_FE_PER_ADAP && adap->fe[i]; i++) {
672 adap->fe[i]->id = i;
673 /* re-assign sleep and wakeup functions */
674 adap->fe_init[i] = adap->fe[i]->ops.init;
675 adap->fe[i]->ops.init = dvb_usb_fe_init;
676 adap->fe_sleep[i] = adap->fe[i]->ops.sleep;
677 adap->fe[i]->ops.sleep = dvb_usb_fe_sleep;
678
679 ret = dvb_register_frontend(&adap->dvb_adap, adap->fe[i]);
680 if (ret < 0) {
681 dev_err(&d->udev->dev,
682 "%s: frontend%d registration failed\n",
683 KBUILD_MODNAME, i);
684 goto err_dvb_unregister_frontend;
685 }
686
687 count_registered++;
688 }
689
690 if (d->props->tuner_attach) {
691 ret = d->props->tuner_attach(adap);
692 if (ret < 0) {
693 dev_dbg(&d->udev->dev, "%s: tuner_attach() failed=%d\n",
694 __func__, ret);
695 goto err_dvb_unregister_frontend;
696 }
697 }
698
699 ret = dvb_create_media_graph(&adap->dvb_adap, true);
700 if (ret < 0)
701 goto err_dvb_unregister_frontend;
702
703 ret = dvb_usbv2_media_device_register(adap);
704
705 return ret;
706
707 err_dvb_unregister_frontend:
708 for (i = count_registered - 1; i >= 0; i--)
709 dvb_unregister_frontend(adap->fe[i]);
710
711 err_dvb_frontend_detach:
712 for (i = MAX_NO_OF_FE_PER_ADAP - 1; i >= 0; i--) {
713 if (adap->fe[i]) {
714 dvb_frontend_detach(adap->fe[i]);
715 adap->fe[i] = NULL;
716 }
717 }
718
719 err:
720 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
721 return ret;
722 }
723
dvb_usbv2_adapter_frontend_exit(struct dvb_usb_adapter * adap)724 static int dvb_usbv2_adapter_frontend_exit(struct dvb_usb_adapter *adap)
725 {
726 int ret, i;
727 struct dvb_usb_device *d = adap_to_d(adap);
728
729 dev_dbg(&d->udev->dev, "%s: adap=%d\n", __func__, adap->id);
730
731 for (i = MAX_NO_OF_FE_PER_ADAP - 1; i >= 0; i--) {
732 if (adap->fe[i]) {
733 dvb_unregister_frontend(adap->fe[i]);
734 dvb_frontend_detach(adap->fe[i]);
735 }
736 }
737
738 if (d->props->tuner_detach) {
739 ret = d->props->tuner_detach(adap);
740 if (ret < 0) {
741 dev_dbg(&d->udev->dev, "%s: tuner_detach() failed=%d\n",
742 __func__, ret);
743 }
744 }
745
746 if (d->props->frontend_detach) {
747 ret = d->props->frontend_detach(adap);
748 if (ret < 0) {
749 dev_dbg(&d->udev->dev,
750 "%s: frontend_detach() failed=%d\n",
751 __func__, ret);
752 }
753 }
754
755 return 0;
756 }
757
dvb_usbv2_adapter_init(struct dvb_usb_device * d)758 static int dvb_usbv2_adapter_init(struct dvb_usb_device *d)
759 {
760 struct dvb_usb_adapter *adap;
761 int ret, i, adapter_count;
762
763 /* resolve adapter count */
764 adapter_count = d->props->num_adapters;
765 if (d->props->get_adapter_count) {
766 ret = d->props->get_adapter_count(d);
767 if (ret < 0)
768 goto err;
769
770 adapter_count = ret;
771 }
772
773 for (i = 0; i < adapter_count; i++) {
774 adap = &d->adapter[i];
775 adap->id = i;
776 adap->props = &d->props->adapter[i];
777
778 /* speed - when running at FULL speed we need a HW PID filter */
779 if (d->udev->speed == USB_SPEED_FULL &&
780 !(adap->props->caps & DVB_USB_ADAP_HAS_PID_FILTER)) {
781 dev_err(&d->udev->dev,
782 "%s: this USB2.0 device cannot be run on a USB1.1 port (it lacks a hardware PID filter)\n",
783 KBUILD_MODNAME);
784 ret = -ENODEV;
785 goto err;
786 } else if ((d->udev->speed == USB_SPEED_FULL &&
787 adap->props->caps & DVB_USB_ADAP_HAS_PID_FILTER) ||
788 (adap->props->caps & DVB_USB_ADAP_NEED_PID_FILTERING)) {
789 dev_info(&d->udev->dev,
790 "%s: will use the device's hardware PID filter (table count: %d)\n",
791 KBUILD_MODNAME,
792 adap->props->pid_filter_count);
793 adap->pid_filtering = 1;
794 adap->max_feed_count = adap->props->pid_filter_count;
795 } else {
796 dev_info(&d->udev->dev,
797 "%s: will pass the complete MPEG2 transport stream to the software demuxer\n",
798 KBUILD_MODNAME);
799 adap->pid_filtering = 0;
800 adap->max_feed_count = 255;
801 }
802
803 if (!adap->pid_filtering && dvb_usb_force_pid_filter_usage &&
804 adap->props->caps & DVB_USB_ADAP_HAS_PID_FILTER) {
805 dev_info(&d->udev->dev,
806 "%s: PID filter enabled by module option\n",
807 KBUILD_MODNAME);
808 adap->pid_filtering = 1;
809 adap->max_feed_count = adap->props->pid_filter_count;
810 }
811
812 ret = dvb_usbv2_adapter_stream_init(adap);
813 if (ret)
814 goto err;
815
816 ret = dvb_usbv2_adapter_dvb_init(adap);
817 if (ret)
818 goto err;
819
820 ret = dvb_usbv2_adapter_frontend_init(adap);
821 if (ret)
822 goto err;
823
824 /* use exclusive FE lock if there is multiple shared FEs */
825 if (adap->fe[1])
826 adap->dvb_adap.mfe_shared = 1;
827 }
828
829 return 0;
830 err:
831 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
832 return ret;
833 }
834
dvb_usbv2_adapter_exit(struct dvb_usb_device * d)835 static int dvb_usbv2_adapter_exit(struct dvb_usb_device *d)
836 {
837 int i;
838 dev_dbg(&d->udev->dev, "%s:\n", __func__);
839
840 for (i = MAX_NO_OF_ADAPTER_PER_DEVICE - 1; i >= 0; i--) {
841 if (d->adapter[i].props) {
842 dvb_usbv2_adapter_dvb_exit(&d->adapter[i]);
843 dvb_usbv2_adapter_stream_exit(&d->adapter[i]);
844 dvb_usbv2_adapter_frontend_exit(&d->adapter[i]);
845 dvb_usbv2_media_device_unregister(&d->adapter[i]);
846 }
847 }
848
849 return 0;
850 }
851
852 /* general initialization functions */
dvb_usbv2_exit(struct dvb_usb_device * d)853 static int dvb_usbv2_exit(struct dvb_usb_device *d)
854 {
855 dev_dbg(&d->udev->dev, "%s:\n", __func__);
856
857 dvb_usbv2_remote_exit(d);
858 dvb_usbv2_adapter_exit(d);
859 dvb_usbv2_i2c_exit(d);
860 kfree(d->priv);
861 kfree(d);
862
863 return 0;
864 }
865
dvb_usbv2_init(struct dvb_usb_device * d)866 static int dvb_usbv2_init(struct dvb_usb_device *d)
867 {
868 int ret;
869 dev_dbg(&d->udev->dev, "%s:\n", __func__);
870
871 dvb_usbv2_device_power_ctrl(d, 1);
872
873 if (d->props->read_config) {
874 ret = d->props->read_config(d);
875 if (ret < 0)
876 goto err;
877 }
878
879 ret = dvb_usbv2_i2c_init(d);
880 if (ret < 0)
881 goto err;
882
883 ret = dvb_usbv2_adapter_init(d);
884 if (ret < 0)
885 goto err;
886
887 if (d->props->init) {
888 ret = d->props->init(d);
889 if (ret < 0)
890 goto err;
891 }
892
893 ret = dvb_usbv2_remote_init(d);
894 if (ret < 0)
895 goto err;
896
897 dvb_usbv2_device_power_ctrl(d, 0);
898
899 return 0;
900 err:
901 dvb_usbv2_device_power_ctrl(d, 0);
902 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
903 return ret;
904 }
905
dvb_usbv2_probe(struct usb_interface * intf,const struct usb_device_id * id)906 int dvb_usbv2_probe(struct usb_interface *intf,
907 const struct usb_device_id *id)
908 {
909 int ret;
910 struct dvb_usb_device *d;
911 struct usb_device *udev = interface_to_usbdev(intf);
912 struct dvb_usb_driver_info *driver_info =
913 (struct dvb_usb_driver_info *) id->driver_info;
914
915 dev_dbg(&udev->dev, "%s: bInterfaceNumber=%d\n", __func__,
916 intf->cur_altsetting->desc.bInterfaceNumber);
917
918 if (!id->driver_info) {
919 dev_err(&udev->dev, "%s: driver_info failed\n", KBUILD_MODNAME);
920 ret = -ENODEV;
921 goto err;
922 }
923
924 d = kzalloc(sizeof(struct dvb_usb_device), GFP_KERNEL);
925 if (!d) {
926 dev_err(&udev->dev, "%s: kzalloc() failed\n", KBUILD_MODNAME);
927 ret = -ENOMEM;
928 goto err;
929 }
930
931 d->intf = intf;
932 d->name = driver_info->name;
933 d->rc_map = driver_info->rc_map;
934 d->udev = udev;
935 d->props = driver_info->props;
936
937 if (intf->cur_altsetting->desc.bInterfaceNumber !=
938 d->props->bInterfaceNumber) {
939 ret = -ENODEV;
940 goto err_free_all;
941 }
942
943 mutex_init(&d->usb_mutex);
944 mutex_init(&d->i2c_mutex);
945
946 if (d->props->size_of_priv) {
947 d->priv = kzalloc(d->props->size_of_priv, GFP_KERNEL);
948 if (!d->priv) {
949 dev_err(&d->udev->dev, "%s: kzalloc() failed\n",
950 KBUILD_MODNAME);
951 ret = -ENOMEM;
952 goto err_free_all;
953 }
954 }
955
956 if (d->props->identify_state) {
957 const char *name = NULL;
958 ret = d->props->identify_state(d, &name);
959 if (ret == 0) {
960 ;
961 } else if (ret == COLD) {
962 dev_info(&d->udev->dev,
963 "%s: found a '%s' in cold state\n",
964 KBUILD_MODNAME, d->name);
965
966 if (!name)
967 name = d->props->firmware;
968
969 ret = dvb_usbv2_download_firmware(d, name);
970 if (ret == 0) {
971 /* device is warm, continue initialization */
972 ;
973 } else if (ret == RECONNECTS_USB) {
974 /*
975 * USB core will call disconnect() and then
976 * probe() as device reconnects itself from the
977 * USB bus. disconnect() will release all driver
978 * resources and probe() is called for 'new'
979 * device. As 'new' device is warm we should
980 * never go here again.
981 */
982 goto exit;
983 } else {
984 goto err_free_all;
985 }
986 } else {
987 goto err_free_all;
988 }
989 }
990
991 dev_info(&d->udev->dev, "%s: found a '%s' in warm state\n",
992 KBUILD_MODNAME, d->name);
993
994 ret = dvb_usbv2_init(d);
995 if (ret < 0)
996 goto err_free_all;
997
998 dev_info(&d->udev->dev,
999 "%s: '%s' successfully initialized and connected\n",
1000 KBUILD_MODNAME, d->name);
1001 exit:
1002 usb_set_intfdata(intf, d);
1003
1004 return 0;
1005 err_free_all:
1006 dvb_usbv2_exit(d);
1007 err:
1008 dev_dbg(&udev->dev, "%s: failed=%d\n", __func__, ret);
1009 return ret;
1010 }
1011 EXPORT_SYMBOL(dvb_usbv2_probe);
1012
dvb_usbv2_disconnect(struct usb_interface * intf)1013 void dvb_usbv2_disconnect(struct usb_interface *intf)
1014 {
1015 struct dvb_usb_device *d = usb_get_intfdata(intf);
1016 const char *devname = kstrdup(dev_name(&d->udev->dev), GFP_KERNEL);
1017 const char *drvname = d->name;
1018
1019 dev_dbg(&d->udev->dev, "%s: bInterfaceNumber=%d\n", __func__,
1020 intf->cur_altsetting->desc.bInterfaceNumber);
1021
1022 if (d->props->exit)
1023 d->props->exit(d);
1024
1025 dvb_usbv2_exit(d);
1026
1027 pr_info("%s: '%s:%s' successfully deinitialized and disconnected\n",
1028 KBUILD_MODNAME, drvname, devname);
1029 kfree(devname);
1030 }
1031 EXPORT_SYMBOL(dvb_usbv2_disconnect);
1032
dvb_usbv2_suspend(struct usb_interface * intf,pm_message_t msg)1033 int dvb_usbv2_suspend(struct usb_interface *intf, pm_message_t msg)
1034 {
1035 struct dvb_usb_device *d = usb_get_intfdata(intf);
1036 int ret = 0, i, active_fe;
1037 struct dvb_frontend *fe;
1038 dev_dbg(&d->udev->dev, "%s:\n", __func__);
1039
1040 /* stop remote controller poll */
1041 if (d->rc_polling_active)
1042 cancel_delayed_work_sync(&d->rc_query_work);
1043
1044 for (i = MAX_NO_OF_ADAPTER_PER_DEVICE - 1; i >= 0; i--) {
1045 active_fe = d->adapter[i].active_fe;
1046 if (d->adapter[i].dvb_adap.priv && active_fe != -1) {
1047 fe = d->adapter[i].fe[active_fe];
1048 d->adapter[i].suspend_resume_active = true;
1049
1050 if (d->props->streaming_ctrl)
1051 d->props->streaming_ctrl(fe, 0);
1052
1053 /* stop usb streaming */
1054 usb_urb_killv2(&d->adapter[i].stream);
1055
1056 ret = dvb_frontend_suspend(fe);
1057 }
1058 }
1059
1060 return ret;
1061 }
1062 EXPORT_SYMBOL(dvb_usbv2_suspend);
1063
dvb_usbv2_resume_common(struct dvb_usb_device * d)1064 static int dvb_usbv2_resume_common(struct dvb_usb_device *d)
1065 {
1066 int ret = 0, i, active_fe;
1067 struct dvb_frontend *fe;
1068 dev_dbg(&d->udev->dev, "%s:\n", __func__);
1069
1070 for (i = 0; i < MAX_NO_OF_ADAPTER_PER_DEVICE; i++) {
1071 active_fe = d->adapter[i].active_fe;
1072 if (d->adapter[i].dvb_adap.priv && active_fe != -1) {
1073 fe = d->adapter[i].fe[active_fe];
1074
1075 ret = dvb_frontend_resume(fe);
1076
1077 /* resume usb streaming */
1078 usb_urb_submitv2(&d->adapter[i].stream, NULL);
1079
1080 if (d->props->streaming_ctrl)
1081 d->props->streaming_ctrl(fe, 1);
1082
1083 d->adapter[i].suspend_resume_active = false;
1084 }
1085 }
1086
1087 /* start remote controller poll */
1088 if (d->rc_polling_active)
1089 schedule_delayed_work(&d->rc_query_work,
1090 msecs_to_jiffies(d->rc.interval));
1091
1092 return ret;
1093 }
1094
dvb_usbv2_resume(struct usb_interface * intf)1095 int dvb_usbv2_resume(struct usb_interface *intf)
1096 {
1097 struct dvb_usb_device *d = usb_get_intfdata(intf);
1098 dev_dbg(&d->udev->dev, "%s:\n", __func__);
1099
1100 return dvb_usbv2_resume_common(d);
1101 }
1102 EXPORT_SYMBOL(dvb_usbv2_resume);
1103
dvb_usbv2_reset_resume(struct usb_interface * intf)1104 int dvb_usbv2_reset_resume(struct usb_interface *intf)
1105 {
1106 struct dvb_usb_device *d = usb_get_intfdata(intf);
1107 int ret;
1108 dev_dbg(&d->udev->dev, "%s:\n", __func__);
1109
1110 dvb_usbv2_device_power_ctrl(d, 1);
1111
1112 if (d->props->init)
1113 d->props->init(d);
1114
1115 ret = dvb_usbv2_resume_common(d);
1116
1117 dvb_usbv2_device_power_ctrl(d, 0);
1118
1119 return ret;
1120 }
1121 EXPORT_SYMBOL(dvb_usbv2_reset_resume);
1122
1123 MODULE_VERSION("2.0");
1124 MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@posteo.de>");
1125 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1126 MODULE_DESCRIPTION("DVB USB common");
1127 MODULE_LICENSE("GPL");
1128