0001 .. SPDX-License-Identifier: GPL-2.0
0002
0003 V4L2 device instance
0004 --------------------
0005
0006 Each device instance is represented by a struct v4l2_device.
0007 Very simple devices can just allocate this struct, but most of the time you
0008 would embed this struct inside a larger struct.
0009
0010 You must register the device instance by calling:
0011
0012 :c:func:`v4l2_device_register <v4l2_device_register>`
0013 (dev, :c:type:`v4l2_dev <v4l2_device>`).
0014
0015 Registration will initialize the :c:type:`v4l2_device` struct. If the
0016 dev->driver_data field is ``NULL``, it will be linked to
0017 :c:type:`v4l2_dev <v4l2_device>` argument.
0018
0019 Drivers that want integration with the media device framework need to set
0020 dev->driver_data manually to point to the driver-specific device structure
0021 that embed the struct v4l2_device instance. This is achieved by a
0022 ``dev_set_drvdata()`` call before registering the V4L2 device instance.
0023 They must also set the struct v4l2_device mdev field to point to a
0024 properly initialized and registered :c:type:`media_device` instance.
0025
0026 If :c:type:`v4l2_dev <v4l2_device>`\ ->name is empty then it will be set to a
0027 value derived from dev (driver name followed by the bus_id, to be precise).
0028 If you set it up before calling :c:func:`v4l2_device_register` then it will
0029 be untouched. If dev is ``NULL``, then you **must** setup
0030 :c:type:`v4l2_dev <v4l2_device>`\ ->name before calling
0031 :c:func:`v4l2_device_register`.
0032
0033 You can use :c:func:`v4l2_device_set_name` to set the name based on a driver
0034 name and a driver-global atomic_t instance. This will generate names like
0035 ``ivtv0``, ``ivtv1``, etc. If the name ends with a digit, then it will insert
0036 a dash: ``cx18-0``, ``cx18-1``, etc. This function returns the instance number.
0037
0038 The first ``dev`` argument is normally the ``struct device`` pointer of a
0039 ``pci_dev``, ``usb_interface`` or ``platform_device``. It is rare for dev to
0040 be ``NULL``, but it happens with ISA devices or when one device creates
0041 multiple PCI devices, thus making it impossible to associate
0042 :c:type:`v4l2_dev <v4l2_device>` with a particular parent.
0043
0044 You can also supply a ``notify()`` callback that can be called by sub-devices
0045 to notify you of events. Whether you need to set this depends on the
0046 sub-device. Any notifications a sub-device supports must be defined in a header
0047 in ``include/media/subdevice.h``.
0048
0049 V4L2 devices are unregistered by calling:
0050
0051 :c:func:`v4l2_device_unregister`
0052 (:c:type:`v4l2_dev <v4l2_device>`).
0053
0054 If the dev->driver_data field points to :c:type:`v4l2_dev <v4l2_device>`,
0055 it will be reset to ``NULL``. Unregistering will also automatically unregister
0056 all subdevs from the device.
0057
0058 If you have a hotpluggable device (e.g. a USB device), then when a disconnect
0059 happens the parent device becomes invalid. Since :c:type:`v4l2_device` has a
0060 pointer to that parent device it has to be cleared as well to mark that the
0061 parent is gone. To do this call:
0062
0063 :c:func:`v4l2_device_disconnect`
0064 (:c:type:`v4l2_dev <v4l2_device>`).
0065
0066 This does *not* unregister the subdevs, so you still need to call the
0067 :c:func:`v4l2_device_unregister` function for that. If your driver is not
0068 hotpluggable, then there is no need to call :c:func:`v4l2_device_disconnect`.
0069
0070 Sometimes you need to iterate over all devices registered by a specific
0071 driver. This is usually the case if multiple device drivers use the same
0072 hardware. E.g. the ivtvfb driver is a framebuffer driver that uses the ivtv
0073 hardware. The same is true for alsa drivers for example.
0074
0075 You can iterate over all registered devices as follows:
0076
0077 .. code-block:: c
0078
0079 static int callback(struct device *dev, void *p)
0080 {
0081 struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
0082
0083 /* test if this device was inited */
0084 if (v4l2_dev == NULL)
0085 return 0;
0086 ...
0087 return 0;
0088 }
0089
0090 int iterate(void *p)
0091 {
0092 struct device_driver *drv;
0093 int err;
0094
0095 /* Find driver 'ivtv' on the PCI bus.
0096 pci_bus_type is a global. For USB buses use usb_bus_type. */
0097 drv = driver_find("ivtv", &pci_bus_type);
0098 /* iterate over all ivtv device instances */
0099 err = driver_for_each_device(drv, NULL, p, callback);
0100 put_driver(drv);
0101 return err;
0102 }
0103
0104 Sometimes you need to keep a running counter of the device instance. This is
0105 commonly used to map a device instance to an index of a module option array.
0106
0107 The recommended approach is as follows:
0108
0109 .. code-block:: c
0110
0111 static atomic_t drv_instance = ATOMIC_INIT(0);
0112
0113 static int drv_probe(struct pci_dev *pdev, const struct pci_device_id *pci_id)
0114 {
0115 ...
0116 state->instance = atomic_inc_return(&drv_instance) - 1;
0117 }
0118
0119 If you have multiple device nodes then it can be difficult to know when it is
0120 safe to unregister :c:type:`v4l2_device` for hotpluggable devices. For this
0121 purpose :c:type:`v4l2_device` has refcounting support. The refcount is
0122 increased whenever :c:func:`video_register_device` is called and it is
0123 decreased whenever that device node is released. When the refcount reaches
0124 zero, then the :c:type:`v4l2_device` release() callback is called. You can
0125 do your final cleanup there.
0126
0127 If other device nodes (e.g. ALSA) are created, then you can increase and
0128 decrease the refcount manually as well by calling:
0129
0130 :c:func:`v4l2_device_get`
0131 (:c:type:`v4l2_dev <v4l2_device>`).
0132
0133 or:
0134
0135 :c:func:`v4l2_device_put`
0136 (:c:type:`v4l2_dev <v4l2_device>`).
0137
0138 Since the initial refcount is 1 you also need to call
0139 :c:func:`v4l2_device_put` in the ``disconnect()`` callback (for USB devices)
0140 or in the ``remove()`` callback (for e.g. PCI devices), otherwise the refcount
0141 will never reach 0.
0142
0143 v4l2_device functions and data structures
0144 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
0145
0146 .. kernel-doc:: include/media/v4l2-device.h