1*4882a593Smuzhiyun.. SPDX-License-Identifier: GPL-2.0 2*4882a593Smuzhiyun 3*4882a593SmuzhiyunV4L2 sub-devices 4*4882a593Smuzhiyun---------------- 5*4882a593Smuzhiyun 6*4882a593SmuzhiyunMany drivers need to communicate with sub-devices. These devices can do all 7*4882a593Smuzhiyunsort of tasks, but most commonly they handle audio and/or video muxing, 8*4882a593Smuzhiyunencoding or decoding. For webcams common sub-devices are sensors and camera 9*4882a593Smuzhiyuncontrollers. 10*4882a593Smuzhiyun 11*4882a593SmuzhiyunUsually these are I2C devices, but not necessarily. In order to provide the 12*4882a593Smuzhiyundriver with a consistent interface to these sub-devices the 13*4882a593Smuzhiyun:c:type:`v4l2_subdev` struct (v4l2-subdev.h) was created. 14*4882a593Smuzhiyun 15*4882a593SmuzhiyunEach sub-device driver must have a :c:type:`v4l2_subdev` struct. This struct 16*4882a593Smuzhiyuncan be stand-alone for simple sub-devices or it might be embedded in a larger 17*4882a593Smuzhiyunstruct if more state information needs to be stored. Usually there is a 18*4882a593Smuzhiyunlow-level device struct (e.g. ``i2c_client``) that contains the device data as 19*4882a593Smuzhiyunsetup by the kernel. It is recommended to store that pointer in the private 20*4882a593Smuzhiyundata of :c:type:`v4l2_subdev` using :c:func:`v4l2_set_subdevdata`. That makes 21*4882a593Smuzhiyunit easy to go from a :c:type:`v4l2_subdev` to the actual low-level bus-specific 22*4882a593Smuzhiyundevice data. 23*4882a593Smuzhiyun 24*4882a593SmuzhiyunYou also need a way to go from the low-level struct to :c:type:`v4l2_subdev`. 25*4882a593SmuzhiyunFor the common i2c_client struct the i2c_set_clientdata() call is used to store 26*4882a593Smuzhiyuna :c:type:`v4l2_subdev` pointer, for other buses you may have to use other 27*4882a593Smuzhiyunmethods. 28*4882a593Smuzhiyun 29*4882a593SmuzhiyunBridges might also need to store per-subdev private data, such as a pointer to 30*4882a593Smuzhiyunbridge-specific per-subdev private data. The :c:type:`v4l2_subdev` structure 31*4882a593Smuzhiyunprovides host private data for that purpose that can be accessed with 32*4882a593Smuzhiyun:c:func:`v4l2_get_subdev_hostdata` and :c:func:`v4l2_set_subdev_hostdata`. 33*4882a593Smuzhiyun 34*4882a593SmuzhiyunFrom the bridge driver perspective, you load the sub-device module and somehow 35*4882a593Smuzhiyunobtain the :c:type:`v4l2_subdev` pointer. For i2c devices this is easy: you call 36*4882a593Smuzhiyun``i2c_get_clientdata()``. For other buses something similar needs to be done. 37*4882a593SmuzhiyunHelper functions exist for sub-devices on an I2C bus that do most of this 38*4882a593Smuzhiyuntricky work for you. 39*4882a593Smuzhiyun 40*4882a593SmuzhiyunEach :c:type:`v4l2_subdev` contains function pointers that sub-device drivers 41*4882a593Smuzhiyuncan implement (or leave ``NULL`` if it is not applicable). Since sub-devices can 42*4882a593Smuzhiyundo so many different things and you do not want to end up with a huge ops struct 43*4882a593Smuzhiyunof which only a handful of ops are commonly implemented, the function pointers 44*4882a593Smuzhiyunare sorted according to category and each category has its own ops struct. 45*4882a593Smuzhiyun 46*4882a593SmuzhiyunThe top-level ops struct contains pointers to the category ops structs, which 47*4882a593Smuzhiyunmay be NULL if the subdev driver does not support anything from that category. 48*4882a593Smuzhiyun 49*4882a593SmuzhiyunIt looks like this: 50*4882a593Smuzhiyun 51*4882a593Smuzhiyun.. code-block:: c 52*4882a593Smuzhiyun 53*4882a593Smuzhiyun struct v4l2_subdev_core_ops { 54*4882a593Smuzhiyun int (*log_status)(struct v4l2_subdev *sd); 55*4882a593Smuzhiyun int (*init)(struct v4l2_subdev *sd, u32 val); 56*4882a593Smuzhiyun ... 57*4882a593Smuzhiyun }; 58*4882a593Smuzhiyun 59*4882a593Smuzhiyun struct v4l2_subdev_tuner_ops { 60*4882a593Smuzhiyun ... 61*4882a593Smuzhiyun }; 62*4882a593Smuzhiyun 63*4882a593Smuzhiyun struct v4l2_subdev_audio_ops { 64*4882a593Smuzhiyun ... 65*4882a593Smuzhiyun }; 66*4882a593Smuzhiyun 67*4882a593Smuzhiyun struct v4l2_subdev_video_ops { 68*4882a593Smuzhiyun ... 69*4882a593Smuzhiyun }; 70*4882a593Smuzhiyun 71*4882a593Smuzhiyun struct v4l2_subdev_pad_ops { 72*4882a593Smuzhiyun ... 73*4882a593Smuzhiyun }; 74*4882a593Smuzhiyun 75*4882a593Smuzhiyun struct v4l2_subdev_ops { 76*4882a593Smuzhiyun const struct v4l2_subdev_core_ops *core; 77*4882a593Smuzhiyun const struct v4l2_subdev_tuner_ops *tuner; 78*4882a593Smuzhiyun const struct v4l2_subdev_audio_ops *audio; 79*4882a593Smuzhiyun const struct v4l2_subdev_video_ops *video; 80*4882a593Smuzhiyun const struct v4l2_subdev_pad_ops *video; 81*4882a593Smuzhiyun }; 82*4882a593Smuzhiyun 83*4882a593SmuzhiyunThe core ops are common to all subdevs, the other categories are implemented 84*4882a593Smuzhiyundepending on the sub-device. E.g. a video device is unlikely to support the 85*4882a593Smuzhiyunaudio ops and vice versa. 86*4882a593Smuzhiyun 87*4882a593SmuzhiyunThis setup limits the number of function pointers while still making it easy 88*4882a593Smuzhiyunto add new ops and categories. 89*4882a593Smuzhiyun 90*4882a593SmuzhiyunA sub-device driver initializes the :c:type:`v4l2_subdev` struct using: 91*4882a593Smuzhiyun 92*4882a593Smuzhiyun :c:func:`v4l2_subdev_init <v4l2_subdev_init>` 93*4882a593Smuzhiyun (:c:type:`sd <v4l2_subdev>`, &\ :c:type:`ops <v4l2_subdev_ops>`). 94*4882a593Smuzhiyun 95*4882a593Smuzhiyun 96*4882a593SmuzhiyunAfterwards you need to initialize :c:type:`sd <v4l2_subdev>`->name with a 97*4882a593Smuzhiyununique name and set the module owner. This is done for you if you use the 98*4882a593Smuzhiyuni2c helper functions. 99*4882a593Smuzhiyun 100*4882a593SmuzhiyunIf integration with the media framework is needed, you must initialize the 101*4882a593Smuzhiyun:c:type:`media_entity` struct embedded in the :c:type:`v4l2_subdev` struct 102*4882a593Smuzhiyun(entity field) by calling :c:func:`media_entity_pads_init`, if the entity has 103*4882a593Smuzhiyunpads: 104*4882a593Smuzhiyun 105*4882a593Smuzhiyun.. code-block:: c 106*4882a593Smuzhiyun 107*4882a593Smuzhiyun struct media_pad *pads = &my_sd->pads; 108*4882a593Smuzhiyun int err; 109*4882a593Smuzhiyun 110*4882a593Smuzhiyun err = media_entity_pads_init(&sd->entity, npads, pads); 111*4882a593Smuzhiyun 112*4882a593SmuzhiyunThe pads array must have been previously initialized. There is no need to 113*4882a593Smuzhiyunmanually set the struct media_entity function and name fields, but the 114*4882a593Smuzhiyunrevision field must be initialized if needed. 115*4882a593Smuzhiyun 116*4882a593SmuzhiyunA reference to the entity will be automatically acquired/released when the 117*4882a593Smuzhiyunsubdev device node (if any) is opened/closed. 118*4882a593Smuzhiyun 119*4882a593SmuzhiyunDon't forget to cleanup the media entity before the sub-device is destroyed: 120*4882a593Smuzhiyun 121*4882a593Smuzhiyun.. code-block:: c 122*4882a593Smuzhiyun 123*4882a593Smuzhiyun media_entity_cleanup(&sd->entity); 124*4882a593Smuzhiyun 125*4882a593SmuzhiyunIf the subdev driver intends to process video and integrate with the media 126*4882a593Smuzhiyunframework, it must implement format related functionality using 127*4882a593Smuzhiyun:c:type:`v4l2_subdev_pad_ops` instead of :c:type:`v4l2_subdev_video_ops`. 128*4882a593Smuzhiyun 129*4882a593SmuzhiyunIn that case, the subdev driver may set the link_validate field to provide 130*4882a593Smuzhiyunits own link validation function. The link validation function is called for 131*4882a593Smuzhiyunevery link in the pipeline where both of the ends of the links are V4L2 132*4882a593Smuzhiyunsub-devices. The driver is still responsible for validating the correctness 133*4882a593Smuzhiyunof the format configuration between sub-devices and video nodes. 134*4882a593Smuzhiyun 135*4882a593SmuzhiyunIf link_validate op is not set, the default function 136*4882a593Smuzhiyun:c:func:`v4l2_subdev_link_validate_default` is used instead. This function 137*4882a593Smuzhiyunensures that width, height and the media bus pixel code are equal on both source 138*4882a593Smuzhiyunand sink of the link. Subdev drivers are also free to use this function to 139*4882a593Smuzhiyunperform the checks mentioned above in addition to their own checks. 140*4882a593Smuzhiyun 141*4882a593SmuzhiyunSubdev registration 142*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~ 143*4882a593Smuzhiyun 144*4882a593SmuzhiyunThere are currently two ways to register subdevices with the V4L2 core. The 145*4882a593Smuzhiyunfirst (traditional) possibility is to have subdevices registered by bridge 146*4882a593Smuzhiyundrivers. This can be done when the bridge driver has the complete information 147*4882a593Smuzhiyunabout subdevices connected to it and knows exactly when to register them. This 148*4882a593Smuzhiyunis typically the case for internal subdevices, like video data processing units 149*4882a593Smuzhiyunwithin SoCs or complex PCI(e) boards, camera sensors in USB cameras or connected 150*4882a593Smuzhiyunto SoCs, which pass information about them to bridge drivers, usually in their 151*4882a593Smuzhiyunplatform data. 152*4882a593Smuzhiyun 153*4882a593SmuzhiyunThere are however also situations where subdevices have to be registered 154*4882a593Smuzhiyunasynchronously to bridge devices. An example of such a configuration is a Device 155*4882a593SmuzhiyunTree based system where information about subdevices is made available to the 156*4882a593Smuzhiyunsystem independently from the bridge devices, e.g. when subdevices are defined 157*4882a593Smuzhiyunin DT as I2C device nodes. The API used in this second case is described further 158*4882a593Smuzhiyunbelow. 159*4882a593Smuzhiyun 160*4882a593SmuzhiyunUsing one or the other registration method only affects the probing process, the 161*4882a593Smuzhiyunrun-time bridge-subdevice interaction is in both cases the same. 162*4882a593Smuzhiyun 163*4882a593SmuzhiyunIn the **synchronous** case a device (bridge) driver needs to register the 164*4882a593Smuzhiyun:c:type:`v4l2_subdev` with the v4l2_device: 165*4882a593Smuzhiyun 166*4882a593Smuzhiyun :c:func:`v4l2_device_register_subdev <v4l2_device_register_subdev>` 167*4882a593Smuzhiyun (:c:type:`v4l2_dev <v4l2_device>`, :c:type:`sd <v4l2_subdev>`). 168*4882a593Smuzhiyun 169*4882a593SmuzhiyunThis can fail if the subdev module disappeared before it could be registered. 170*4882a593SmuzhiyunAfter this function was called successfully the subdev->dev field points to 171*4882a593Smuzhiyunthe :c:type:`v4l2_device`. 172*4882a593Smuzhiyun 173*4882a593SmuzhiyunIf the v4l2_device parent device has a non-NULL mdev field, the sub-device 174*4882a593Smuzhiyunentity will be automatically registered with the media device. 175*4882a593Smuzhiyun 176*4882a593SmuzhiyunYou can unregister a sub-device using: 177*4882a593Smuzhiyun 178*4882a593Smuzhiyun :c:func:`v4l2_device_unregister_subdev <v4l2_device_unregister_subdev>` 179*4882a593Smuzhiyun (:c:type:`sd <v4l2_subdev>`). 180*4882a593Smuzhiyun 181*4882a593Smuzhiyun 182*4882a593SmuzhiyunAfterwards the subdev module can be unloaded and 183*4882a593Smuzhiyun:c:type:`sd <v4l2_subdev>`->dev == ``NULL``. 184*4882a593Smuzhiyun 185*4882a593SmuzhiyunIn the **asynchronous** case subdevice probing can be invoked independently of 186*4882a593Smuzhiyunthe bridge driver availability. The subdevice driver then has to verify whether 187*4882a593Smuzhiyunall the requirements for a successful probing are satisfied. This can include a 188*4882a593Smuzhiyuncheck for a master clock availability. If any of the conditions aren't satisfied 189*4882a593Smuzhiyunthe driver might decide to return ``-EPROBE_DEFER`` to request further reprobing 190*4882a593Smuzhiyunattempts. Once all conditions are met the subdevice shall be registered using 191*4882a593Smuzhiyunthe :c:func:`v4l2_async_register_subdev` function. Unregistration is 192*4882a593Smuzhiyunperformed using the :c:func:`v4l2_async_unregister_subdev` call. Subdevices 193*4882a593Smuzhiyunregistered this way are stored in a global list of subdevices, ready to be 194*4882a593Smuzhiyunpicked up by bridge drivers. 195*4882a593Smuzhiyun 196*4882a593SmuzhiyunBridge drivers in turn have to register a notifier object. This is 197*4882a593Smuzhiyunperformed using the :c:func:`v4l2_async_notifier_register` call. To 198*4882a593Smuzhiyununregister the notifier the driver has to call 199*4882a593Smuzhiyun:c:func:`v4l2_async_notifier_unregister`. The former of the two functions 200*4882a593Smuzhiyuntakes two arguments: a pointer to struct :c:type:`v4l2_device` and a 201*4882a593Smuzhiyunpointer to struct :c:type:`v4l2_async_notifier`. 202*4882a593Smuzhiyun 203*4882a593SmuzhiyunBefore registering the notifier, bridge drivers must do two things: 204*4882a593Smuzhiyunfirst, the notifier must be initialized using the 205*4882a593Smuzhiyun:c:func:`v4l2_async_notifier_init`. Second, bridge drivers can then 206*4882a593Smuzhiyunbegin to form a list of subdevice descriptors that the bridge device 207*4882a593Smuzhiyunneeds for its operation. Subdevice descriptors are added to the notifier 208*4882a593Smuzhiyunusing the :c:func:`v4l2_async_notifier_add_subdev` call. This function 209*4882a593Smuzhiyuntakes two arguments: a pointer to struct :c:type:`v4l2_async_notifier`, 210*4882a593Smuzhiyunand a pointer to the subdevice descripter, which is of type struct 211*4882a593Smuzhiyun:c:type:`v4l2_async_subdev`. 212*4882a593Smuzhiyun 213*4882a593SmuzhiyunThe V4L2 core will then use these descriptors to match asynchronously 214*4882a593Smuzhiyunregistered subdevices to them. If a match is detected the ``.bound()`` 215*4882a593Smuzhiyunnotifier callback is called. After all subdevices have been located the 216*4882a593Smuzhiyun.complete() callback is called. When a subdevice is removed from the 217*4882a593Smuzhiyunsystem the .unbind() method is called. All three callbacks are optional. 218*4882a593Smuzhiyun 219*4882a593SmuzhiyunCalling subdev operations 220*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~ 221*4882a593Smuzhiyun 222*4882a593SmuzhiyunThe advantage of using :c:type:`v4l2_subdev` is that it is a generic struct and 223*4882a593Smuzhiyundoes not contain any knowledge about the underlying hardware. So a driver might 224*4882a593Smuzhiyuncontain several subdevs that use an I2C bus, but also a subdev that is 225*4882a593Smuzhiyuncontrolled through GPIO pins. This distinction is only relevant when setting 226*4882a593Smuzhiyunup the device, but once the subdev is registered it is completely transparent. 227*4882a593Smuzhiyun 228*4882a593SmuzhiyunOnce te subdev has been registered you can call an ops function either 229*4882a593Smuzhiyundirectly: 230*4882a593Smuzhiyun 231*4882a593Smuzhiyun.. code-block:: c 232*4882a593Smuzhiyun 233*4882a593Smuzhiyun err = sd->ops->core->g_std(sd, &norm); 234*4882a593Smuzhiyun 235*4882a593Smuzhiyunbut it is better and easier to use this macro: 236*4882a593Smuzhiyun 237*4882a593Smuzhiyun.. code-block:: c 238*4882a593Smuzhiyun 239*4882a593Smuzhiyun err = v4l2_subdev_call(sd, core, g_std, &norm); 240*4882a593Smuzhiyun 241*4882a593SmuzhiyunThe macro will do the right ``NULL`` pointer checks and returns ``-ENODEV`` 242*4882a593Smuzhiyunif :c:type:`sd <v4l2_subdev>` is ``NULL``, ``-ENOIOCTLCMD`` if either 243*4882a593Smuzhiyun:c:type:`sd <v4l2_subdev>`->core or :c:type:`sd <v4l2_subdev>`->core->g_std is ``NULL``, or the actual result of the 244*4882a593Smuzhiyun:c:type:`sd <v4l2_subdev>`->ops->core->g_std ops. 245*4882a593Smuzhiyun 246*4882a593SmuzhiyunIt is also possible to call all or a subset of the sub-devices: 247*4882a593Smuzhiyun 248*4882a593Smuzhiyun.. code-block:: c 249*4882a593Smuzhiyun 250*4882a593Smuzhiyun v4l2_device_call_all(v4l2_dev, 0, core, g_std, &norm); 251*4882a593Smuzhiyun 252*4882a593SmuzhiyunAny subdev that does not support this ops is skipped and error results are 253*4882a593Smuzhiyunignored. If you want to check for errors use this: 254*4882a593Smuzhiyun 255*4882a593Smuzhiyun.. code-block:: c 256*4882a593Smuzhiyun 257*4882a593Smuzhiyun err = v4l2_device_call_until_err(v4l2_dev, 0, core, g_std, &norm); 258*4882a593Smuzhiyun 259*4882a593SmuzhiyunAny error except ``-ENOIOCTLCMD`` will exit the loop with that error. If no 260*4882a593Smuzhiyunerrors (except ``-ENOIOCTLCMD``) occurred, then 0 is returned. 261*4882a593Smuzhiyun 262*4882a593SmuzhiyunThe second argument to both calls is a group ID. If 0, then all subdevs are 263*4882a593Smuzhiyuncalled. If non-zero, then only those whose group ID match that value will 264*4882a593Smuzhiyunbe called. Before a bridge driver registers a subdev it can set 265*4882a593Smuzhiyun:c:type:`sd <v4l2_subdev>`->grp_id to whatever value it wants (it's 0 by 266*4882a593Smuzhiyundefault). This value is owned by the bridge driver and the sub-device driver 267*4882a593Smuzhiyunwill never modify or use it. 268*4882a593Smuzhiyun 269*4882a593SmuzhiyunThe group ID gives the bridge driver more control how callbacks are called. 270*4882a593SmuzhiyunFor example, there may be multiple audio chips on a board, each capable of 271*4882a593Smuzhiyunchanging the volume. But usually only one will actually be used when the 272*4882a593Smuzhiyunuser want to change the volume. You can set the group ID for that subdev to 273*4882a593Smuzhiyune.g. AUDIO_CONTROLLER and specify that as the group ID value when calling 274*4882a593Smuzhiyun``v4l2_device_call_all()``. That ensures that it will only go to the subdev 275*4882a593Smuzhiyunthat needs it. 276*4882a593Smuzhiyun 277*4882a593SmuzhiyunIf the sub-device needs to notify its v4l2_device parent of an event, then 278*4882a593Smuzhiyunit can call ``v4l2_subdev_notify(sd, notification, arg)``. This macro checks 279*4882a593Smuzhiyunwhether there is a ``notify()`` callback defined and returns ``-ENODEV`` if not. 280*4882a593SmuzhiyunOtherwise the result of the ``notify()`` call is returned. 281*4882a593Smuzhiyun 282*4882a593SmuzhiyunV4L2 sub-device userspace API 283*4882a593Smuzhiyun----------------------------- 284*4882a593Smuzhiyun 285*4882a593SmuzhiyunBridge drivers traditionally expose one or multiple video nodes to userspace, 286*4882a593Smuzhiyunand control subdevices through the :c:type:`v4l2_subdev_ops` operations in 287*4882a593Smuzhiyunresponse to video node operations. This hides the complexity of the underlying 288*4882a593Smuzhiyunhardware from applications. For complex devices, finer-grained control of the 289*4882a593Smuzhiyundevice than what the video nodes offer may be required. In those cases, bridge 290*4882a593Smuzhiyundrivers that implement :ref:`the media controller API <media_controller>` may 291*4882a593Smuzhiyunopt for making the subdevice operations directly accessible from userpace. 292*4882a593Smuzhiyun 293*4882a593SmuzhiyunDevice nodes named ``v4l-subdev``\ *X* can be created in ``/dev`` to access 294*4882a593Smuzhiyunsub-devices directly. If a sub-device supports direct userspace configuration 295*4882a593Smuzhiyunit must set the ``V4L2_SUBDEV_FL_HAS_DEVNODE`` flag before being registered. 296*4882a593Smuzhiyun 297*4882a593SmuzhiyunAfter registering sub-devices, the :c:type:`v4l2_device` driver can create 298*4882a593Smuzhiyundevice nodes for all registered sub-devices marked with 299*4882a593Smuzhiyun``V4L2_SUBDEV_FL_HAS_DEVNODE`` by calling 300*4882a593Smuzhiyun:c:func:`v4l2_device_register_subdev_nodes`. Those device nodes will be 301*4882a593Smuzhiyunautomatically removed when sub-devices are unregistered. 302*4882a593Smuzhiyun 303*4882a593SmuzhiyunThe device node handles a subset of the V4L2 API. 304*4882a593Smuzhiyun 305*4882a593Smuzhiyun``VIDIOC_QUERYCTRL``, 306*4882a593Smuzhiyun``VIDIOC_QUERYMENU``, 307*4882a593Smuzhiyun``VIDIOC_G_CTRL``, 308*4882a593Smuzhiyun``VIDIOC_S_CTRL``, 309*4882a593Smuzhiyun``VIDIOC_G_EXT_CTRLS``, 310*4882a593Smuzhiyun``VIDIOC_S_EXT_CTRLS`` and 311*4882a593Smuzhiyun``VIDIOC_TRY_EXT_CTRLS``: 312*4882a593Smuzhiyun 313*4882a593Smuzhiyun The controls ioctls are identical to the ones defined in V4L2. They 314*4882a593Smuzhiyun behave identically, with the only exception that they deal only with 315*4882a593Smuzhiyun controls implemented in the sub-device. Depending on the driver, those 316*4882a593Smuzhiyun controls can be also be accessed through one (or several) V4L2 device 317*4882a593Smuzhiyun nodes. 318*4882a593Smuzhiyun 319*4882a593Smuzhiyun``VIDIOC_DQEVENT``, 320*4882a593Smuzhiyun``VIDIOC_SUBSCRIBE_EVENT`` and 321*4882a593Smuzhiyun``VIDIOC_UNSUBSCRIBE_EVENT`` 322*4882a593Smuzhiyun 323*4882a593Smuzhiyun The events ioctls are identical to the ones defined in V4L2. They 324*4882a593Smuzhiyun behave identically, with the only exception that they deal only with 325*4882a593Smuzhiyun events generated by the sub-device. Depending on the driver, those 326*4882a593Smuzhiyun events can also be reported by one (or several) V4L2 device nodes. 327*4882a593Smuzhiyun 328*4882a593Smuzhiyun Sub-device drivers that want to use events need to set the 329*4882a593Smuzhiyun ``V4L2_SUBDEV_FL_HAS_EVENTS`` :c:type:`v4l2_subdev`.flags before registering 330*4882a593Smuzhiyun the sub-device. After registration events can be queued as usual on the 331*4882a593Smuzhiyun :c:type:`v4l2_subdev`.devnode device node. 332*4882a593Smuzhiyun 333*4882a593Smuzhiyun To properly support events, the ``poll()`` file operation is also 334*4882a593Smuzhiyun implemented. 335*4882a593Smuzhiyun 336*4882a593SmuzhiyunPrivate ioctls 337*4882a593Smuzhiyun 338*4882a593Smuzhiyun All ioctls not in the above list are passed directly to the sub-device 339*4882a593Smuzhiyun driver through the core::ioctl operation. 340*4882a593Smuzhiyun 341*4882a593SmuzhiyunRead-only sub-device userspace API 342*4882a593Smuzhiyun---------------------------------- 343*4882a593Smuzhiyun 344*4882a593SmuzhiyunBridge drivers that control their connected subdevices through direct calls to 345*4882a593Smuzhiyunthe kernel API realized by :c:type:`v4l2_subdev_ops` structure do not usually 346*4882a593Smuzhiyunwant userspace to be able to change the same parameters through the subdevice 347*4882a593Smuzhiyundevice node and thus do not usually register any. 348*4882a593Smuzhiyun 349*4882a593SmuzhiyunIt is sometimes useful to report to userspace the current subdevice 350*4882a593Smuzhiyunconfiguration through a read-only API, that does not permit applications to 351*4882a593Smuzhiyunchange to the device parameters but allows interfacing to the subdevice device 352*4882a593Smuzhiyunnode to inspect them. 353*4882a593Smuzhiyun 354*4882a593SmuzhiyunFor instance, to implement cameras based on computational photography, userspace 355*4882a593Smuzhiyunneeds to know the detailed camera sensor configuration (in terms of skipping, 356*4882a593Smuzhiyunbinning, cropping and scaling) for each supported output resolution. To support 357*4882a593Smuzhiyunsuch use cases, bridge drivers may expose the subdevice operations to userspace 358*4882a593Smuzhiyunthrough a read-only API. 359*4882a593Smuzhiyun 360*4882a593SmuzhiyunTo create a read-only device node for all the subdevices registered with the 361*4882a593Smuzhiyun``V4L2_SUBDEV_FL_HAS_DEVNODE`` set, the :c:type:`v4l2_device` driver should call 362*4882a593Smuzhiyun:c:func:`v4l2_device_register_ro_subdev_nodes`. 363*4882a593Smuzhiyun 364*4882a593SmuzhiyunAccess to the following ioctls for userspace applications is restricted on 365*4882a593Smuzhiyunsub-device device nodes registered with 366*4882a593Smuzhiyun:c:func:`v4l2_device_register_ro_subdev_nodes`. 367*4882a593Smuzhiyun 368*4882a593Smuzhiyun``VIDIOC_SUBDEV_S_FMT``, 369*4882a593Smuzhiyun``VIDIOC_SUBDEV_S_CROP``, 370*4882a593Smuzhiyun``VIDIOC_SUBDEV_S_SELECTION``: 371*4882a593Smuzhiyun 372*4882a593Smuzhiyun These ioctls are only allowed on a read-only subdevice device node 373*4882a593Smuzhiyun for the :ref:`V4L2_SUBDEV_FORMAT_TRY <v4l2-subdev-format-whence>` 374*4882a593Smuzhiyun formats and selection rectangles. 375*4882a593Smuzhiyun 376*4882a593Smuzhiyun``VIDIOC_SUBDEV_S_FRAME_INTERVAL``, 377*4882a593Smuzhiyun``VIDIOC_SUBDEV_S_DV_TIMINGS``, 378*4882a593Smuzhiyun``VIDIOC_SUBDEV_S_STD``: 379*4882a593Smuzhiyun 380*4882a593Smuzhiyun These ioctls are not allowed on a read-only subdevice node. 381*4882a593Smuzhiyun 382*4882a593SmuzhiyunIn case the ioctl is not allowed, or the format to modify is set to 383*4882a593Smuzhiyun``V4L2_SUBDEV_FORMAT_ACTIVE``, the core returns a negative error code and 384*4882a593Smuzhiyunthe errno variable is set to ``-EPERM``. 385*4882a593Smuzhiyun 386*4882a593SmuzhiyunI2C sub-device drivers 387*4882a593Smuzhiyun---------------------- 388*4882a593Smuzhiyun 389*4882a593SmuzhiyunSince these drivers are so common, special helper functions are available to 390*4882a593Smuzhiyunease the use of these drivers (``v4l2-common.h``). 391*4882a593Smuzhiyun 392*4882a593SmuzhiyunThe recommended method of adding :c:type:`v4l2_subdev` support to an I2C driver 393*4882a593Smuzhiyunis to embed the :c:type:`v4l2_subdev` struct into the state struct that is 394*4882a593Smuzhiyuncreated for each I2C device instance. Very simple devices have no state 395*4882a593Smuzhiyunstruct and in that case you can just create a :c:type:`v4l2_subdev` directly. 396*4882a593Smuzhiyun 397*4882a593SmuzhiyunA typical state struct would look like this (where 'chipname' is replaced by 398*4882a593Smuzhiyunthe name of the chip): 399*4882a593Smuzhiyun 400*4882a593Smuzhiyun.. code-block:: c 401*4882a593Smuzhiyun 402*4882a593Smuzhiyun struct chipname_state { 403*4882a593Smuzhiyun struct v4l2_subdev sd; 404*4882a593Smuzhiyun ... /* additional state fields */ 405*4882a593Smuzhiyun }; 406*4882a593Smuzhiyun 407*4882a593SmuzhiyunInitialize the :c:type:`v4l2_subdev` struct as follows: 408*4882a593Smuzhiyun 409*4882a593Smuzhiyun.. code-block:: c 410*4882a593Smuzhiyun 411*4882a593Smuzhiyun v4l2_i2c_subdev_init(&state->sd, client, subdev_ops); 412*4882a593Smuzhiyun 413*4882a593SmuzhiyunThis function will fill in all the fields of :c:type:`v4l2_subdev` ensure that 414*4882a593Smuzhiyunthe :c:type:`v4l2_subdev` and i2c_client both point to one another. 415*4882a593Smuzhiyun 416*4882a593SmuzhiyunYou should also add a helper inline function to go from a :c:type:`v4l2_subdev` 417*4882a593Smuzhiyunpointer to a chipname_state struct: 418*4882a593Smuzhiyun 419*4882a593Smuzhiyun.. code-block:: c 420*4882a593Smuzhiyun 421*4882a593Smuzhiyun static inline struct chipname_state *to_state(struct v4l2_subdev *sd) 422*4882a593Smuzhiyun { 423*4882a593Smuzhiyun return container_of(sd, struct chipname_state, sd); 424*4882a593Smuzhiyun } 425*4882a593Smuzhiyun 426*4882a593SmuzhiyunUse this to go from the :c:type:`v4l2_subdev` struct to the ``i2c_client`` 427*4882a593Smuzhiyunstruct: 428*4882a593Smuzhiyun 429*4882a593Smuzhiyun.. code-block:: c 430*4882a593Smuzhiyun 431*4882a593Smuzhiyun struct i2c_client *client = v4l2_get_subdevdata(sd); 432*4882a593Smuzhiyun 433*4882a593SmuzhiyunAnd this to go from an ``i2c_client`` to a :c:type:`v4l2_subdev` struct: 434*4882a593Smuzhiyun 435*4882a593Smuzhiyun.. code-block:: c 436*4882a593Smuzhiyun 437*4882a593Smuzhiyun struct v4l2_subdev *sd = i2c_get_clientdata(client); 438*4882a593Smuzhiyun 439*4882a593SmuzhiyunMake sure to call 440*4882a593Smuzhiyun:c:func:`v4l2_device_unregister_subdev`\ (:c:type:`sd <v4l2_subdev>`) 441*4882a593Smuzhiyunwhen the ``remove()`` callback is called. This will unregister the sub-device 442*4882a593Smuzhiyunfrom the bridge driver. It is safe to call this even if the sub-device was 443*4882a593Smuzhiyunnever registered. 444*4882a593Smuzhiyun 445*4882a593SmuzhiyunYou need to do this because when the bridge driver destroys the i2c adapter 446*4882a593Smuzhiyunthe ``remove()`` callbacks are called of the i2c devices on that adapter. 447*4882a593SmuzhiyunAfter that the corresponding v4l2_subdev structures are invalid, so they 448*4882a593Smuzhiyunhave to be unregistered first. Calling 449*4882a593Smuzhiyun:c:func:`v4l2_device_unregister_subdev`\ (:c:type:`sd <v4l2_subdev>`) 450*4882a593Smuzhiyunfrom the ``remove()`` callback ensures that this is always done correctly. 451*4882a593Smuzhiyun 452*4882a593Smuzhiyun 453*4882a593SmuzhiyunThe bridge driver also has some helper functions it can use: 454*4882a593Smuzhiyun 455*4882a593Smuzhiyun.. code-block:: c 456*4882a593Smuzhiyun 457*4882a593Smuzhiyun struct v4l2_subdev *sd = v4l2_i2c_new_subdev(v4l2_dev, adapter, 458*4882a593Smuzhiyun "module_foo", "chipid", 0x36, NULL); 459*4882a593Smuzhiyun 460*4882a593SmuzhiyunThis loads the given module (can be ``NULL`` if no module needs to be loaded) 461*4882a593Smuzhiyunand calls :c:func:`i2c_new_client_device` with the given ``i2c_adapter`` and 462*4882a593Smuzhiyunchip/address arguments. If all goes well, then it registers the subdev with 463*4882a593Smuzhiyunthe v4l2_device. 464*4882a593Smuzhiyun 465*4882a593SmuzhiyunYou can also use the last argument of :c:func:`v4l2_i2c_new_subdev` to pass 466*4882a593Smuzhiyunan array of possible I2C addresses that it should probe. These probe addresses 467*4882a593Smuzhiyunare only used if the previous argument is 0. A non-zero argument means that you 468*4882a593Smuzhiyunknow the exact i2c address so in that case no probing will take place. 469*4882a593Smuzhiyun 470*4882a593SmuzhiyunBoth functions return ``NULL`` if something went wrong. 471*4882a593Smuzhiyun 472*4882a593SmuzhiyunNote that the chipid you pass to :c:func:`v4l2_i2c_new_subdev` is usually 473*4882a593Smuzhiyunthe same as the module name. It allows you to specify a chip variant, e.g. 474*4882a593Smuzhiyun"saa7114" or "saa7115". In general though the i2c driver autodetects this. 475*4882a593SmuzhiyunThe use of chipid is something that needs to be looked at more closely at a 476*4882a593Smuzhiyunlater date. It differs between i2c drivers and as such can be confusing. 477*4882a593SmuzhiyunTo see which chip variants are supported you can look in the i2c driver code 478*4882a593Smuzhiyunfor the i2c_device_id table. This lists all the possibilities. 479*4882a593Smuzhiyun 480*4882a593SmuzhiyunThere are one more helper function: 481*4882a593Smuzhiyun 482*4882a593Smuzhiyun:c:func:`v4l2_i2c_new_subdev_board` uses an :c:type:`i2c_board_info` struct 483*4882a593Smuzhiyunwhich is passed to the i2c driver and replaces the irq, platform_data and addr 484*4882a593Smuzhiyunarguments. 485*4882a593Smuzhiyun 486*4882a593SmuzhiyunIf the subdev supports the s_config core ops, then that op is called with 487*4882a593Smuzhiyunthe irq and platform_data arguments after the subdev was setup. 488*4882a593Smuzhiyun 489*4882a593SmuzhiyunThe :c:func:`v4l2_i2c_new_subdev` function will call 490*4882a593Smuzhiyun:c:func:`v4l2_i2c_new_subdev_board`, internally filling a 491*4882a593Smuzhiyun:c:type:`i2c_board_info` structure using the ``client_type`` and the 492*4882a593Smuzhiyun``addr`` to fill it. 493*4882a593Smuzhiyun 494*4882a593SmuzhiyunV4L2 sub-device functions and data structures 495*4882a593Smuzhiyun--------------------------------------------- 496*4882a593Smuzhiyun 497*4882a593Smuzhiyun.. kernel-doc:: include/media/v4l2-subdev.h 498