18c2ecf20Sopenharmony_ci.. SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci 38c2ecf20Sopenharmony_ciV4L2 device instance 48c2ecf20Sopenharmony_ci-------------------- 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ciEach device instance is represented by a struct v4l2_device. 78c2ecf20Sopenharmony_ciVery simple devices can just allocate this struct, but most of the time you 88c2ecf20Sopenharmony_ciwould embed this struct inside a larger struct. 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ciYou must register the device instance by calling: 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci :c:func:`v4l2_device_register <v4l2_device_register>` 138c2ecf20Sopenharmony_ci (dev, :c:type:`v4l2_dev <v4l2_device>`). 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ciRegistration will initialize the :c:type:`v4l2_device` struct. If the 168c2ecf20Sopenharmony_cidev->driver_data field is ``NULL``, it will be linked to 178c2ecf20Sopenharmony_ci:c:type:`v4l2_dev <v4l2_device>` argument. 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ciDrivers that want integration with the media device framework need to set 208c2ecf20Sopenharmony_cidev->driver_data manually to point to the driver-specific device structure 218c2ecf20Sopenharmony_cithat embed the struct v4l2_device instance. This is achieved by a 228c2ecf20Sopenharmony_ci``dev_set_drvdata()`` call before registering the V4L2 device instance. 238c2ecf20Sopenharmony_ciThey must also set the struct v4l2_device mdev field to point to a 248c2ecf20Sopenharmony_ciproperly initialized and registered :c:type:`media_device` instance. 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ciIf :c:type:`v4l2_dev <v4l2_device>`\ ->name is empty then it will be set to a 278c2ecf20Sopenharmony_civalue derived from dev (driver name followed by the bus_id, to be precise). 288c2ecf20Sopenharmony_ciIf you set it up before calling :c:func:`v4l2_device_register` then it will 298c2ecf20Sopenharmony_cibe untouched. If dev is ``NULL``, then you **must** setup 308c2ecf20Sopenharmony_ci:c:type:`v4l2_dev <v4l2_device>`\ ->name before calling 318c2ecf20Sopenharmony_ci:c:func:`v4l2_device_register`. 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ciYou can use :c:func:`v4l2_device_set_name` to set the name based on a driver 348c2ecf20Sopenharmony_ciname and a driver-global atomic_t instance. This will generate names like 358c2ecf20Sopenharmony_ci``ivtv0``, ``ivtv1``, etc. If the name ends with a digit, then it will insert 368c2ecf20Sopenharmony_cia dash: ``cx18-0``, ``cx18-1``, etc. This function returns the instance number. 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ciThe first ``dev`` argument is normally the ``struct device`` pointer of a 398c2ecf20Sopenharmony_ci``pci_dev``, ``usb_interface`` or ``platform_device``. It is rare for dev to 408c2ecf20Sopenharmony_cibe ``NULL``, but it happens with ISA devices or when one device creates 418c2ecf20Sopenharmony_cimultiple PCI devices, thus making it impossible to associate 428c2ecf20Sopenharmony_ci:c:type:`v4l2_dev <v4l2_device>` with a particular parent. 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ciYou can also supply a ``notify()`` callback that can be called by sub-devices 458c2ecf20Sopenharmony_cito notify you of events. Whether you need to set this depends on the 468c2ecf20Sopenharmony_cisub-device. Any notifications a sub-device supports must be defined in a header 478c2ecf20Sopenharmony_ciin ``include/media/subdevice.h``. 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ciV4L2 devices are unregistered by calling: 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ci :c:func:`v4l2_device_unregister` 528c2ecf20Sopenharmony_ci (:c:type:`v4l2_dev <v4l2_device>`). 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ciIf the dev->driver_data field points to :c:type:`v4l2_dev <v4l2_device>`, 558c2ecf20Sopenharmony_ciit will be reset to ``NULL``. Unregistering will also automatically unregister 568c2ecf20Sopenharmony_ciall subdevs from the device. 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ciIf you have a hotpluggable device (e.g. a USB device), then when a disconnect 598c2ecf20Sopenharmony_cihappens the parent device becomes invalid. Since :c:type:`v4l2_device` has a 608c2ecf20Sopenharmony_cipointer to that parent device it has to be cleared as well to mark that the 618c2ecf20Sopenharmony_ciparent is gone. To do this call: 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci :c:func:`v4l2_device_disconnect` 648c2ecf20Sopenharmony_ci (:c:type:`v4l2_dev <v4l2_device>`). 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ciThis does *not* unregister the subdevs, so you still need to call the 678c2ecf20Sopenharmony_ci:c:func:`v4l2_device_unregister` function for that. If your driver is not 688c2ecf20Sopenharmony_cihotpluggable, then there is no need to call :c:func:`v4l2_device_disconnect`. 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ciSometimes you need to iterate over all devices registered by a specific 718c2ecf20Sopenharmony_cidriver. This is usually the case if multiple device drivers use the same 728c2ecf20Sopenharmony_cihardware. E.g. the ivtvfb driver is a framebuffer driver that uses the ivtv 738c2ecf20Sopenharmony_cihardware. The same is true for alsa drivers for example. 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ciYou can iterate over all registered devices as follows: 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci.. code-block:: c 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci static int callback(struct device *dev, void *p) 808c2ecf20Sopenharmony_ci { 818c2ecf20Sopenharmony_ci struct v4l2_device *v4l2_dev = dev_get_drvdata(dev); 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci /* test if this device was inited */ 848c2ecf20Sopenharmony_ci if (v4l2_dev == NULL) 858c2ecf20Sopenharmony_ci return 0; 868c2ecf20Sopenharmony_ci ... 878c2ecf20Sopenharmony_ci return 0; 888c2ecf20Sopenharmony_ci } 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci int iterate(void *p) 918c2ecf20Sopenharmony_ci { 928c2ecf20Sopenharmony_ci struct device_driver *drv; 938c2ecf20Sopenharmony_ci int err; 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci /* Find driver 'ivtv' on the PCI bus. 968c2ecf20Sopenharmony_ci pci_bus_type is a global. For USB buses use usb_bus_type. */ 978c2ecf20Sopenharmony_ci drv = driver_find("ivtv", &pci_bus_type); 988c2ecf20Sopenharmony_ci /* iterate over all ivtv device instances */ 998c2ecf20Sopenharmony_ci err = driver_for_each_device(drv, NULL, p, callback); 1008c2ecf20Sopenharmony_ci put_driver(drv); 1018c2ecf20Sopenharmony_ci return err; 1028c2ecf20Sopenharmony_ci } 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ciSometimes you need to keep a running counter of the device instance. This is 1058c2ecf20Sopenharmony_cicommonly used to map a device instance to an index of a module option array. 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ciThe recommended approach is as follows: 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci.. code-block:: c 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci static atomic_t drv_instance = ATOMIC_INIT(0); 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci static int drv_probe(struct pci_dev *pdev, const struct pci_device_id *pci_id) 1148c2ecf20Sopenharmony_ci { 1158c2ecf20Sopenharmony_ci ... 1168c2ecf20Sopenharmony_ci state->instance = atomic_inc_return(&drv_instance) - 1; 1178c2ecf20Sopenharmony_ci } 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ciIf you have multiple device nodes then it can be difficult to know when it is 1208c2ecf20Sopenharmony_cisafe to unregister :c:type:`v4l2_device` for hotpluggable devices. For this 1218c2ecf20Sopenharmony_cipurpose :c:type:`v4l2_device` has refcounting support. The refcount is 1228c2ecf20Sopenharmony_ciincreased whenever :c:func:`video_register_device` is called and it is 1238c2ecf20Sopenharmony_cidecreased whenever that device node is released. When the refcount reaches 1248c2ecf20Sopenharmony_cizero, then the :c:type:`v4l2_device` release() callback is called. You can 1258c2ecf20Sopenharmony_cido your final cleanup there. 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ciIf other device nodes (e.g. ALSA) are created, then you can increase and 1288c2ecf20Sopenharmony_cidecrease the refcount manually as well by calling: 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ci :c:func:`v4l2_device_get` 1318c2ecf20Sopenharmony_ci (:c:type:`v4l2_dev <v4l2_device>`). 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_cior: 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci :c:func:`v4l2_device_put` 1368c2ecf20Sopenharmony_ci (:c:type:`v4l2_dev <v4l2_device>`). 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ciSince the initial refcount is 1 you also need to call 1398c2ecf20Sopenharmony_ci:c:func:`v4l2_device_put` in the ``disconnect()`` callback (for USB devices) 1408c2ecf20Sopenharmony_cior in the ``remove()`` callback (for e.g. PCI devices), otherwise the refcount 1418c2ecf20Sopenharmony_ciwill never reach 0. 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_civ4l2_device functions and data structures 1448c2ecf20Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ci.. kernel-doc:: include/media/v4l2-device.h 147