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