v4l2-device.rst 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. V4L2 device instance
  2. --------------------
  3. Each device instance is represented by a struct :c:type:`v4l2_device`.
  4. Very simple devices can just allocate this struct, but most of the time you
  5. would embed this struct inside a larger struct.
  6. You must register the device instance by calling:
  7. :c:func:`v4l2_device_register <v4l2_device_register>`
  8. (dev, :c:type:`v4l2_dev <v4l2_device>`).
  9. Registration will initialize the :c:type:`v4l2_device` struct. If the
  10. dev->driver_data field is ``NULL``, it will be linked to
  11. :c:type:`v4l2_dev <v4l2_device>` argument.
  12. Drivers that want integration with the media device framework need to set
  13. dev->driver_data manually to point to the driver-specific device structure
  14. that embed the struct :c:type:`v4l2_device` instance. This is achieved by a
  15. ``dev_set_drvdata()`` call before registering the V4L2 device instance.
  16. They must also set the struct :c:type:`v4l2_device` mdev field to point to a
  17. properly initialized and registered :c:type:`media_device` instance.
  18. If :c:type:`v4l2_dev <v4l2_device>`\ ->name is empty then it will be set to a
  19. value derived from dev (driver name followed by the bus_id, to be precise).
  20. If you set it up before calling :c:func:`v4l2_device_register` then it will
  21. be untouched. If dev is ``NULL``, then you **must** setup
  22. :c:type:`v4l2_dev <v4l2_device>`\ ->name before calling
  23. :c:func:`v4l2_device_register`.
  24. You can use :c:func:`v4l2_device_set_name` to set the name based on a driver
  25. name and a driver-global atomic_t instance. This will generate names like
  26. ``ivtv0``, ``ivtv1``, etc. If the name ends with a digit, then it will insert
  27. a dash: ``cx18-0``, ``cx18-1``, etc. This function returns the instance number.
  28. The first ``dev`` argument is normally the ``struct device`` pointer of a
  29. ``pci_dev``, ``usb_interface`` or ``platform_device``. It is rare for dev to
  30. be ``NULL``, but it happens with ISA devices or when one device creates
  31. multiple PCI devices, thus making it impossible to associate
  32. :c:type:`v4l2_dev <v4l2_device>` with a particular parent.
  33. You can also supply a ``notify()`` callback that can be called by sub-devices
  34. to notify you of events. Whether you need to set this depends on the
  35. sub-device. Any notifications a sub-device supports must be defined in a header
  36. in ``include/media/subdevice.h``.
  37. V4L2 devices are unregistered by calling:
  38. :c:func:`v4l2_device_unregister`
  39. (:c:type:`v4l2_dev <v4l2_device>`).
  40. If the dev->driver_data field points to :c:type:`v4l2_dev <v4l2_device>`,
  41. it will be reset to ``NULL``. Unregistering will also automatically unregister
  42. all subdevs from the device.
  43. If you have a hotpluggable device (e.g. a USB device), then when a disconnect
  44. happens the parent device becomes invalid. Since :c:type:`v4l2_device` has a
  45. pointer to that parent device it has to be cleared as well to mark that the
  46. parent is gone. To do this call:
  47. :c:func:`v4l2_device_disconnect`
  48. (:c:type:`v4l2_dev <v4l2_device>`).
  49. This does *not* unregister the subdevs, so you still need to call the
  50. :c:func:`v4l2_device_unregister` function for that. If your driver is not
  51. hotpluggable, then there is no need to call :c:func:`v4l2_device_disconnect`.
  52. Sometimes you need to iterate over all devices registered by a specific
  53. driver. This is usually the case if multiple device drivers use the same
  54. hardware. E.g. the ivtvfb driver is a framebuffer driver that uses the ivtv
  55. hardware. The same is true for alsa drivers for example.
  56. You can iterate over all registered devices as follows:
  57. .. code-block:: c
  58. static int callback(struct device *dev, void *p)
  59. {
  60. struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
  61. /* test if this device was inited */
  62. if (v4l2_dev == NULL)
  63. return 0;
  64. ...
  65. return 0;
  66. }
  67. int iterate(void *p)
  68. {
  69. struct device_driver *drv;
  70. int err;
  71. /* Find driver 'ivtv' on the PCI bus.
  72. pci_bus_type is a global. For USB busses use usb_bus_type. */
  73. drv = driver_find("ivtv", &pci_bus_type);
  74. /* iterate over all ivtv device instances */
  75. err = driver_for_each_device(drv, NULL, p, callback);
  76. put_driver(drv);
  77. return err;
  78. }
  79. Sometimes you need to keep a running counter of the device instance. This is
  80. commonly used to map a device instance to an index of a module option array.
  81. The recommended approach is as follows:
  82. .. code-block:: c
  83. static atomic_t drv_instance = ATOMIC_INIT(0);
  84. static int drv_probe(struct pci_dev *pdev, const struct pci_device_id *pci_id)
  85. {
  86. ...
  87. state->instance = atomic_inc_return(&drv_instance) - 1;
  88. }
  89. If you have multiple device nodes then it can be difficult to know when it is
  90. safe to unregister :c:type:`v4l2_device` for hotpluggable devices. For this
  91. purpose :c:type:`v4l2_device` has refcounting support. The refcount is
  92. increased whenever :c:func:`video_register_device` is called and it is
  93. decreased whenever that device node is released. When the refcount reaches
  94. zero, then the :c:type:`v4l2_device` release() callback is called. You can
  95. do your final cleanup there.
  96. If other device nodes (e.g. ALSA) are created, then you can increase and
  97. decrease the refcount manually as well by calling:
  98. :c:func:`v4l2_device_get`
  99. (:c:type:`v4l2_dev <v4l2_device>`).
  100. or:
  101. :c:func:`v4l2_device_put`
  102. (:c:type:`v4l2_dev <v4l2_device>`).
  103. Since the initial refcount is 1 you also need to call
  104. :c:func:`v4l2_device_put` in the ``disconnect()`` callback (for USB devices)
  105. or in the ``remove()`` callback (for e.g. PCI devices), otherwise the refcount
  106. will never reach 0.
  107. v4l2_device functions and data structures
  108. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  109. .. kernel-doc:: include/media/v4l2-device.h