Mauro Carvalho Chehab | 840b14d | 2016-07-20 15:27:04 -0300 | [diff] [blame^] | 1 | V4L2 sub-devices |
| 2 | ---------------- |
| 3 | |
| 4 | Many drivers need to communicate with sub-devices. These devices can do all |
| 5 | sort of tasks, but most commonly they handle audio and/or video muxing, |
| 6 | encoding or decoding. For webcams common sub-devices are sensors and camera |
| 7 | controllers. |
| 8 | |
| 9 | Usually these are I2C devices, but not necessarily. In order to provide the |
| 10 | driver with a consistent interface to these sub-devices the v4l2_subdev struct |
| 11 | (v4l2-subdev.h) was created. |
| 12 | |
| 13 | Each sub-device driver must have a v4l2_subdev struct. This struct can be |
| 14 | stand-alone for simple sub-devices or it might be embedded in a larger struct |
| 15 | if more state information needs to be stored. Usually there is a low-level |
| 16 | device struct (e.g. i2c_client) that contains the device data as setup |
| 17 | by the kernel. It is recommended to store that pointer in the private |
| 18 | data of v4l2_subdev using v4l2_set_subdevdata(). That makes it easy to go |
| 19 | from a v4l2_subdev to the actual low-level bus-specific device data. |
| 20 | |
| 21 | You also need a way to go from the low-level struct to v4l2_subdev. For the |
| 22 | common i2c_client struct the i2c_set_clientdata() call is used to store a |
| 23 | v4l2_subdev pointer, for other busses you may have to use other methods. |
| 24 | |
| 25 | Bridges might also need to store per-subdev private data, such as a pointer to |
| 26 | bridge-specific per-subdev private data. The v4l2_subdev structure provides |
| 27 | host private data for that purpose that can be accessed with |
| 28 | v4l2_get_subdev_hostdata() and v4l2_set_subdev_hostdata(). |
| 29 | |
| 30 | From the bridge driver perspective you load the sub-device module and somehow |
| 31 | obtain the v4l2_subdev pointer. For i2c devices this is easy: you call |
| 32 | i2c_get_clientdata(). For other busses something similar needs to be done. |
| 33 | Helper functions exists for sub-devices on an I2C bus that do most of this |
| 34 | tricky work for you. |
| 35 | |
| 36 | Each v4l2_subdev contains function pointers that sub-device drivers can |
| 37 | implement (or leave NULL if it is not applicable). Since sub-devices can do |
| 38 | so many different things and you do not want to end up with a huge ops struct |
| 39 | of which only a handful of ops are commonly implemented, the function pointers |
| 40 | are sorted according to category and each category has its own ops struct. |
| 41 | |
| 42 | The top-level ops struct contains pointers to the category ops structs, which |
| 43 | may be NULL if the subdev driver does not support anything from that category. |
| 44 | |
| 45 | It looks like this: |
| 46 | |
| 47 | .. code-block:: none |
| 48 | |
| 49 | struct v4l2_subdev_core_ops { |
| 50 | int (*log_status)(struct v4l2_subdev *sd); |
| 51 | int (*init)(struct v4l2_subdev *sd, u32 val); |
| 52 | ... |
| 53 | }; |
| 54 | |
| 55 | struct v4l2_subdev_tuner_ops { |
| 56 | ... |
| 57 | }; |
| 58 | |
| 59 | struct v4l2_subdev_audio_ops { |
| 60 | ... |
| 61 | }; |
| 62 | |
| 63 | struct v4l2_subdev_video_ops { |
| 64 | ... |
| 65 | }; |
| 66 | |
| 67 | struct v4l2_subdev_pad_ops { |
| 68 | ... |
| 69 | }; |
| 70 | |
| 71 | struct v4l2_subdev_ops { |
| 72 | const struct v4l2_subdev_core_ops *core; |
| 73 | const struct v4l2_subdev_tuner_ops *tuner; |
| 74 | const struct v4l2_subdev_audio_ops *audio; |
| 75 | const struct v4l2_subdev_video_ops *video; |
| 76 | const struct v4l2_subdev_pad_ops *video; |
| 77 | }; |
| 78 | |
| 79 | The core ops are common to all subdevs, the other categories are implemented |
| 80 | depending on the sub-device. E.g. a video device is unlikely to support the |
| 81 | audio ops and vice versa. |
| 82 | |
| 83 | This setup limits the number of function pointers while still making it easy |
| 84 | to add new ops and categories. |
| 85 | |
| 86 | A sub-device driver initializes the v4l2_subdev struct using: |
| 87 | |
| 88 | .. code-block:: none |
| 89 | |
| 90 | v4l2_subdev_init(sd, &ops); |
| 91 | |
| 92 | Afterwards you need to initialize subdev->name with a unique name and set the |
| 93 | module owner. This is done for you if you use the i2c helper functions. |
| 94 | |
| 95 | If integration with the media framework is needed, you must initialize the |
| 96 | media_entity struct embedded in the v4l2_subdev struct (entity field) by |
| 97 | calling media_entity_pads_init(), if the entity has pads: |
| 98 | |
| 99 | .. code-block:: none |
| 100 | |
| 101 | struct media_pad *pads = &my_sd->pads; |
| 102 | int err; |
| 103 | |
| 104 | err = media_entity_pads_init(&sd->entity, npads, pads); |
| 105 | |
| 106 | The pads array must have been previously initialized. There is no need to |
| 107 | manually set the struct media_entity function and name fields, but the |
| 108 | revision field must be initialized if needed. |
| 109 | |
| 110 | A reference to the entity will be automatically acquired/released when the |
| 111 | subdev device node (if any) is opened/closed. |
| 112 | |
| 113 | Don't forget to cleanup the media entity before the sub-device is destroyed: |
| 114 | |
| 115 | .. code-block:: none |
| 116 | |
| 117 | media_entity_cleanup(&sd->entity); |
| 118 | |
| 119 | If the subdev driver intends to process video and integrate with the media |
| 120 | framework, it must implement format related functionality using |
| 121 | v4l2_subdev_pad_ops instead of v4l2_subdev_video_ops. |
| 122 | |
| 123 | In that case, the subdev driver may set the link_validate field to provide |
| 124 | its own link validation function. The link validation function is called for |
| 125 | every link in the pipeline where both of the ends of the links are V4L2 |
| 126 | sub-devices. The driver is still responsible for validating the correctness |
| 127 | of the format configuration between sub-devices and video nodes. |
| 128 | |
| 129 | If link_validate op is not set, the default function |
| 130 | v4l2_subdev_link_validate_default() is used instead. This function ensures |
| 131 | that width, height and the media bus pixel code are equal on both source and |
| 132 | sink of the link. Subdev drivers are also free to use this function to |
| 133 | perform the checks mentioned above in addition to their own checks. |
| 134 | |
| 135 | There are currently two ways to register subdevices with the V4L2 core. The |
| 136 | first (traditional) possibility is to have subdevices registered by bridge |
| 137 | drivers. This can be done when the bridge driver has the complete information |
| 138 | about subdevices connected to it and knows exactly when to register them. This |
| 139 | is typically the case for internal subdevices, like video data processing units |
| 140 | within SoCs or complex PCI(e) boards, camera sensors in USB cameras or connected |
| 141 | to SoCs, which pass information about them to bridge drivers, usually in their |
| 142 | platform data. |
| 143 | |
| 144 | There are however also situations where subdevices have to be registered |
| 145 | asynchronously to bridge devices. An example of such a configuration is a Device |
| 146 | Tree based system where information about subdevices is made available to the |
| 147 | system independently from the bridge devices, e.g. when subdevices are defined |
| 148 | in DT as I2C device nodes. The API used in this second case is described further |
| 149 | below. |
| 150 | |
| 151 | Using one or the other registration method only affects the probing process, the |
| 152 | run-time bridge-subdevice interaction is in both cases the same. |
| 153 | |
| 154 | In the synchronous case a device (bridge) driver needs to register the |
| 155 | v4l2_subdev with the v4l2_device: |
| 156 | |
| 157 | .. code-block:: none |
| 158 | |
| 159 | int err = v4l2_device_register_subdev(v4l2_dev, sd); |
| 160 | |
| 161 | This can fail if the subdev module disappeared before it could be registered. |
| 162 | After this function was called successfully the subdev->dev field points to |
| 163 | the v4l2_device. |
| 164 | |
| 165 | If the v4l2_device parent device has a non-NULL mdev field, the sub-device |
| 166 | entity will be automatically registered with the media device. |
| 167 | |
| 168 | You can unregister a sub-device using: |
| 169 | |
| 170 | .. code-block:: none |
| 171 | |
| 172 | v4l2_device_unregister_subdev(sd); |
| 173 | |
| 174 | Afterwards the subdev module can be unloaded and sd->dev == NULL. |
| 175 | |
| 176 | You can call an ops function either directly: |
| 177 | |
| 178 | .. code-block:: none |
| 179 | |
| 180 | err = sd->ops->core->g_std(sd, &norm); |
| 181 | |
| 182 | but it is better and easier to use this macro: |
| 183 | |
| 184 | .. code-block:: none |
| 185 | |
| 186 | err = v4l2_subdev_call(sd, core, g_std, &norm); |
| 187 | |
| 188 | The macro will to the right NULL pointer checks and returns -ENODEV if subdev |
| 189 | is NULL, -ENOIOCTLCMD if either subdev->core or subdev->core->g_std is |
| 190 | NULL, or the actual result of the subdev->ops->core->g_std ops. |
| 191 | |
| 192 | It is also possible to call all or a subset of the sub-devices: |
| 193 | |
| 194 | .. code-block:: none |
| 195 | |
| 196 | v4l2_device_call_all(v4l2_dev, 0, core, g_std, &norm); |
| 197 | |
| 198 | Any subdev that does not support this ops is skipped and error results are |
| 199 | ignored. If you want to check for errors use this: |
| 200 | |
| 201 | .. code-block:: none |
| 202 | |
| 203 | err = v4l2_device_call_until_err(v4l2_dev, 0, core, g_std, &norm); |
| 204 | |
| 205 | Any error except -ENOIOCTLCMD will exit the loop with that error. If no |
| 206 | errors (except -ENOIOCTLCMD) occurred, then 0 is returned. |
| 207 | |
| 208 | The second argument to both calls is a group ID. If 0, then all subdevs are |
| 209 | called. If non-zero, then only those whose group ID match that value will |
| 210 | be called. Before a bridge driver registers a subdev it can set sd->grp_id |
| 211 | to whatever value it wants (it's 0 by default). This value is owned by the |
| 212 | bridge driver and the sub-device driver will never modify or use it. |
| 213 | |
| 214 | The group ID gives the bridge driver more control how callbacks are called. |
| 215 | For example, there may be multiple audio chips on a board, each capable of |
| 216 | changing the volume. But usually only one will actually be used when the |
| 217 | user want to change the volume. You can set the group ID for that subdev to |
| 218 | e.g. AUDIO_CONTROLLER and specify that as the group ID value when calling |
| 219 | v4l2_device_call_all(). That ensures that it will only go to the subdev |
| 220 | that needs it. |
| 221 | |
| 222 | If the sub-device needs to notify its v4l2_device parent of an event, then |
| 223 | it can call v4l2_subdev_notify(sd, notification, arg). This macro checks |
| 224 | whether there is a notify() callback defined and returns -ENODEV if not. |
| 225 | Otherwise the result of the notify() call is returned. |
| 226 | |
| 227 | The advantage of using v4l2_subdev is that it is a generic struct and does |
| 228 | not contain any knowledge about the underlying hardware. So a driver might |
| 229 | contain several subdevs that use an I2C bus, but also a subdev that is |
| 230 | controlled through GPIO pins. This distinction is only relevant when setting |
| 231 | up the device, but once the subdev is registered it is completely transparent. |
| 232 | |
| 233 | |
| 234 | In the asynchronous case subdevice probing can be invoked independently of the |
| 235 | bridge driver availability. The subdevice driver then has to verify whether all |
| 236 | the requirements for a successful probing are satisfied. This can include a |
| 237 | check for a master clock availability. If any of the conditions aren't satisfied |
| 238 | the driver might decide to return -EPROBE_DEFER to request further reprobing |
| 239 | attempts. Once all conditions are met the subdevice shall be registered using |
| 240 | the v4l2_async_register_subdev() function. Unregistration is performed using |
| 241 | the v4l2_async_unregister_subdev() call. Subdevices registered this way are |
| 242 | stored in a global list of subdevices, ready to be picked up by bridge drivers. |
| 243 | |
| 244 | Bridge drivers in turn have to register a notifier object with an array of |
| 245 | subdevice descriptors that the bridge device needs for its operation. This is |
| 246 | performed using the v4l2_async_notifier_register() call. To unregister the |
| 247 | notifier the driver has to call v4l2_async_notifier_unregister(). The former of |
| 248 | the two functions takes two arguments: a pointer to struct v4l2_device and a |
| 249 | pointer to struct v4l2_async_notifier. The latter contains a pointer to an array |
| 250 | of pointers to subdevice descriptors of type struct v4l2_async_subdev type. The |
| 251 | V4L2 core will then use these descriptors to match asynchronously registered |
| 252 | subdevices to them. If a match is detected the .bound() notifier callback is |
| 253 | called. After all subdevices have been located the .complete() callback is |
| 254 | called. When a subdevice is removed from the system the .unbind() method is |
| 255 | called. All three callbacks are optional. |
| 256 | |
Mauro Carvalho Chehab | 5875987 | 2016-07-20 14:14:37 -0300 | [diff] [blame] | 257 | V4L2 subdev kAPI |
| 258 | ^^^^^^^^^^^^^^^^ |
| 259 | |
| 260 | .. kernel-doc:: include/media/v4l2-subdev.h |