root/drivers/media/usb/dvb-usb-v2/dvb_usb_core.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. dvb_usbv2_download_firmware
  2. dvb_usbv2_i2c_init
  3. dvb_usbv2_i2c_exit
  4. dvb_usb_read_remote_control
  5. dvb_usbv2_remote_init
  6. dvb_usbv2_remote_exit
  7. dvb_usb_data_complete
  8. dvb_usb_data_complete_204
  9. dvb_usb_data_complete_raw
  10. dvb_usbv2_adapter_stream_init
  11. dvb_usbv2_adapter_stream_exit
  12. dvb_usb_start_feed
  13. dvb_usb_stop_feed
  14. dvb_usbv2_media_device_init
  15. dvb_usbv2_media_device_register
  16. dvb_usbv2_media_device_unregister
  17. dvb_usbv2_adapter_dvb_init
  18. dvb_usbv2_adapter_dvb_exit
  19. dvb_usbv2_device_power_ctrl
  20. dvb_usb_fe_init
  21. dvb_usb_fe_sleep
  22. dvb_usbv2_adapter_frontend_init
  23. dvb_usbv2_adapter_frontend_exit
  24. dvb_usbv2_adapter_init
  25. dvb_usbv2_adapter_exit
  26. dvb_usbv2_exit
  27. dvb_usbv2_init
  28. dvb_usbv2_probe
  29. dvb_usbv2_disconnect
  30. dvb_usbv2_suspend
  31. dvb_usbv2_resume_common
  32. dvb_usbv2_resume
  33. dvb_usbv2_reset_resume

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

/* [<][>][^][v][top][bottom][index][help] */