xref: /OK3568_Linux_fs/kernel/Documentation/driver-api/media/v4l2-device.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun.. SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun
3*4882a593SmuzhiyunV4L2 device instance
4*4882a593Smuzhiyun--------------------
5*4882a593Smuzhiyun
6*4882a593SmuzhiyunEach device instance is represented by a struct v4l2_device.
7*4882a593SmuzhiyunVery simple devices can just allocate this struct, but most of the time you
8*4882a593Smuzhiyunwould embed this struct inside a larger struct.
9*4882a593Smuzhiyun
10*4882a593SmuzhiyunYou must register the device instance by calling:
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun	:c:func:`v4l2_device_register <v4l2_device_register>`
13*4882a593Smuzhiyun	(dev, :c:type:`v4l2_dev <v4l2_device>`).
14*4882a593Smuzhiyun
15*4882a593SmuzhiyunRegistration will initialize the :c:type:`v4l2_device` struct. If the
16*4882a593Smuzhiyundev->driver_data field is ``NULL``, it will be linked to
17*4882a593Smuzhiyun:c:type:`v4l2_dev <v4l2_device>` argument.
18*4882a593Smuzhiyun
19*4882a593SmuzhiyunDrivers that want integration with the media device framework need to set
20*4882a593Smuzhiyundev->driver_data manually to point to the driver-specific device structure
21*4882a593Smuzhiyunthat embed the struct v4l2_device instance. This is achieved by a
22*4882a593Smuzhiyun``dev_set_drvdata()`` call before registering the V4L2 device instance.
23*4882a593SmuzhiyunThey must also set the struct v4l2_device mdev field to point to a
24*4882a593Smuzhiyunproperly initialized and registered :c:type:`media_device` instance.
25*4882a593Smuzhiyun
26*4882a593SmuzhiyunIf :c:type:`v4l2_dev <v4l2_device>`\ ->name is empty then it will be set to a
27*4882a593Smuzhiyunvalue derived from dev (driver name followed by the bus_id, to be precise).
28*4882a593SmuzhiyunIf you set it up before  calling :c:func:`v4l2_device_register` then it will
29*4882a593Smuzhiyunbe untouched. If dev is ``NULL``, then you **must** setup
30*4882a593Smuzhiyun:c:type:`v4l2_dev <v4l2_device>`\ ->name before calling
31*4882a593Smuzhiyun:c:func:`v4l2_device_register`.
32*4882a593Smuzhiyun
33*4882a593SmuzhiyunYou can use :c:func:`v4l2_device_set_name` to set the name based on a driver
34*4882a593Smuzhiyunname and a driver-global atomic_t instance. This will generate names like
35*4882a593Smuzhiyun``ivtv0``, ``ivtv1``, etc. If the name ends with a digit, then it will insert
36*4882a593Smuzhiyuna dash: ``cx18-0``, ``cx18-1``, etc. This function returns the instance number.
37*4882a593Smuzhiyun
38*4882a593SmuzhiyunThe first ``dev`` argument is normally the ``struct device`` pointer of a
39*4882a593Smuzhiyun``pci_dev``, ``usb_interface`` or ``platform_device``. It is rare for dev to
40*4882a593Smuzhiyunbe ``NULL``, but it happens with ISA devices or when one device creates
41*4882a593Smuzhiyunmultiple PCI devices, thus making it impossible to associate
42*4882a593Smuzhiyun:c:type:`v4l2_dev <v4l2_device>` with a particular parent.
43*4882a593Smuzhiyun
44*4882a593SmuzhiyunYou can also supply a ``notify()`` callback that can be called by sub-devices
45*4882a593Smuzhiyunto notify you of events. Whether you need to set this depends on the
46*4882a593Smuzhiyunsub-device. Any notifications a sub-device supports must be defined in a header
47*4882a593Smuzhiyunin ``include/media/subdevice.h``.
48*4882a593Smuzhiyun
49*4882a593SmuzhiyunV4L2 devices are unregistered by calling:
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun	:c:func:`v4l2_device_unregister`
52*4882a593Smuzhiyun	(:c:type:`v4l2_dev <v4l2_device>`).
53*4882a593Smuzhiyun
54*4882a593SmuzhiyunIf the dev->driver_data field points to :c:type:`v4l2_dev <v4l2_device>`,
55*4882a593Smuzhiyunit will be reset to ``NULL``. Unregistering will also automatically unregister
56*4882a593Smuzhiyunall subdevs from the device.
57*4882a593Smuzhiyun
58*4882a593SmuzhiyunIf you have a hotpluggable device (e.g. a USB device), then when a disconnect
59*4882a593Smuzhiyunhappens the parent device becomes invalid. Since :c:type:`v4l2_device` has a
60*4882a593Smuzhiyunpointer to that parent device it has to be cleared as well to mark that the
61*4882a593Smuzhiyunparent is gone. To do this call:
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun	:c:func:`v4l2_device_disconnect`
64*4882a593Smuzhiyun	(:c:type:`v4l2_dev <v4l2_device>`).
65*4882a593Smuzhiyun
66*4882a593SmuzhiyunThis does *not* unregister the subdevs, so you still need to call the
67*4882a593Smuzhiyun:c:func:`v4l2_device_unregister` function for that. If your driver is not
68*4882a593Smuzhiyunhotpluggable, then there is no need to call :c:func:`v4l2_device_disconnect`.
69*4882a593Smuzhiyun
70*4882a593SmuzhiyunSometimes you need to iterate over all devices registered by a specific
71*4882a593Smuzhiyundriver. This is usually the case if multiple device drivers use the same
72*4882a593Smuzhiyunhardware. E.g. the ivtvfb driver is a framebuffer driver that uses the ivtv
73*4882a593Smuzhiyunhardware. The same is true for alsa drivers for example.
74*4882a593Smuzhiyun
75*4882a593SmuzhiyunYou can iterate over all registered devices as follows:
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun.. code-block:: c
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun	static int callback(struct device *dev, void *p)
80*4882a593Smuzhiyun	{
81*4882a593Smuzhiyun		struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun		/* test if this device was inited */
84*4882a593Smuzhiyun		if (v4l2_dev == NULL)
85*4882a593Smuzhiyun			return 0;
86*4882a593Smuzhiyun		...
87*4882a593Smuzhiyun		return 0;
88*4882a593Smuzhiyun	}
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun	int iterate(void *p)
91*4882a593Smuzhiyun	{
92*4882a593Smuzhiyun		struct device_driver *drv;
93*4882a593Smuzhiyun		int err;
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun		/* Find driver 'ivtv' on the PCI bus.
96*4882a593Smuzhiyun		pci_bus_type is a global. For USB buses use usb_bus_type. */
97*4882a593Smuzhiyun		drv = driver_find("ivtv", &pci_bus_type);
98*4882a593Smuzhiyun		/* iterate over all ivtv device instances */
99*4882a593Smuzhiyun		err = driver_for_each_device(drv, NULL, p, callback);
100*4882a593Smuzhiyun		put_driver(drv);
101*4882a593Smuzhiyun		return err;
102*4882a593Smuzhiyun	}
103*4882a593Smuzhiyun
104*4882a593SmuzhiyunSometimes you need to keep a running counter of the device instance. This is
105*4882a593Smuzhiyuncommonly used to map a device instance to an index of a module option array.
106*4882a593Smuzhiyun
107*4882a593SmuzhiyunThe recommended approach is as follows:
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun.. code-block:: c
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun	static atomic_t drv_instance = ATOMIC_INIT(0);
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun	static int drv_probe(struct pci_dev *pdev, const struct pci_device_id *pci_id)
114*4882a593Smuzhiyun	{
115*4882a593Smuzhiyun		...
116*4882a593Smuzhiyun		state->instance = atomic_inc_return(&drv_instance) - 1;
117*4882a593Smuzhiyun	}
118*4882a593Smuzhiyun
119*4882a593SmuzhiyunIf you have multiple device nodes then it can be difficult to know when it is
120*4882a593Smuzhiyunsafe to unregister :c:type:`v4l2_device` for hotpluggable devices. For this
121*4882a593Smuzhiyunpurpose :c:type:`v4l2_device` has refcounting support. The refcount is
122*4882a593Smuzhiyunincreased whenever :c:func:`video_register_device` is called and it is
123*4882a593Smuzhiyundecreased whenever that device node is released. When the refcount reaches
124*4882a593Smuzhiyunzero, then the :c:type:`v4l2_device` release() callback is called. You can
125*4882a593Smuzhiyundo your final cleanup there.
126*4882a593Smuzhiyun
127*4882a593SmuzhiyunIf other device nodes (e.g. ALSA) are created, then you can increase and
128*4882a593Smuzhiyundecrease the refcount manually as well by calling:
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun	:c:func:`v4l2_device_get`
131*4882a593Smuzhiyun	(:c:type:`v4l2_dev <v4l2_device>`).
132*4882a593Smuzhiyun
133*4882a593Smuzhiyunor:
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun	:c:func:`v4l2_device_put`
136*4882a593Smuzhiyun	(:c:type:`v4l2_dev <v4l2_device>`).
137*4882a593Smuzhiyun
138*4882a593SmuzhiyunSince the initial refcount is 1 you also need to call
139*4882a593Smuzhiyun:c:func:`v4l2_device_put` in the ``disconnect()`` callback (for USB devices)
140*4882a593Smuzhiyunor in the ``remove()`` callback (for e.g. PCI devices), otherwise the refcount
141*4882a593Smuzhiyunwill never reach 0.
142*4882a593Smuzhiyun
143*4882a593Smuzhiyunv4l2_device functions and data structures
144*4882a593Smuzhiyun^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun.. kernel-doc:: include/media/v4l2-device.h
147