v4l2-framework.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. Overview of the V4L2 driver framework
  2. =====================================
  3. This text documents the various structures provided by the V4L2 framework and
  4. their relationships.
  5. Introduction
  6. ------------
  7. The V4L2 drivers tend to be very complex due to the complexity of the
  8. hardware: most devices have multiple ICs, export multiple device nodes in
  9. /dev, and create also non-V4L2 devices such as DVB, ALSA, FB, I2C and input
  10. (IR) devices.
  11. Especially the fact that V4L2 drivers have to setup supporting ICs to
  12. do audio/video muxing/encoding/decoding makes it more complex than most.
  13. Usually these ICs are connected to the main bridge driver through one or
  14. more I2C busses, but other busses can also be used. Such devices are
  15. called 'sub-devices'.
  16. For a long time the framework was limited to the video_device struct for
  17. creating V4L device nodes and video_buf for handling the video buffers
  18. (note that this document does not discuss the video_buf framework).
  19. This meant that all drivers had to do the setup of device instances and
  20. connecting to sub-devices themselves. Some of this is quite complicated
  21. to do right and many drivers never did do it correctly.
  22. There is also a lot of common code that could never be refactored due to
  23. the lack of a framework.
  24. So this framework sets up the basic building blocks that all drivers
  25. need and this same framework should make it much easier to refactor
  26. common code into utility functions shared by all drivers.
  27. Structure of a driver
  28. ---------------------
  29. All drivers have the following structure:
  30. 1) A struct for each device instance containing the device state.
  31. 2) A way of initializing and commanding sub-devices (if any).
  32. 3) Creating V4L2 device nodes (/dev/videoX, /dev/vbiX, /dev/radioX and
  33. /dev/vtxX) and keeping track of device-node specific data.
  34. 4) Filehandle-specific structs containing per-filehandle data.
  35. This is a rough schematic of how it all relates:
  36. device instances
  37. |
  38. +-sub-device instances
  39. |
  40. \-V4L2 device nodes
  41. |
  42. \-filehandle instances
  43. Structure of the framework
  44. --------------------------
  45. The framework closely resembles the driver structure: it has a v4l2_device
  46. struct for the device instance data, a v4l2_subdev struct to refer to
  47. sub-device instances, the video_device struct stores V4L2 device node data
  48. and in the future a v4l2_fh struct will keep track of filehandle instances
  49. (this is not yet implemented).
  50. struct v4l2_device
  51. ------------------
  52. Each device instance is represented by a struct v4l2_device (v4l2-device.h).
  53. Very simple devices can just allocate this struct, but most of the time you
  54. would embed this struct inside a larger struct.
  55. You must register the device instance:
  56. v4l2_device_register(struct device *dev, struct v4l2_device *v4l2_dev);
  57. Registration will initialize the v4l2_device struct and link dev->driver_data
  58. to v4l2_dev. Registration will also set v4l2_dev->name to a value derived from
  59. dev (driver name followed by the bus_id, to be precise). You may change the
  60. name after registration if you want.
  61. You unregister with:
  62. v4l2_device_unregister(struct v4l2_device *v4l2_dev);
  63. Unregistering will also automatically unregister all subdevs from the device.
  64. Sometimes you need to iterate over all devices registered by a specific
  65. driver. This is usually the case if multiple device drivers use the same
  66. hardware. E.g. the ivtvfb driver is a framebuffer driver that uses the ivtv
  67. hardware. The same is true for alsa drivers for example.
  68. You can iterate over all registered devices as follows:
  69. static int callback(struct device *dev, void *p)
  70. {
  71. struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
  72. /* test if this device was inited */
  73. if (v4l2_dev == NULL)
  74. return 0;
  75. ...
  76. return 0;
  77. }
  78. int iterate(void *p)
  79. {
  80. struct device_driver *drv;
  81. int err;
  82. /* Find driver 'ivtv' on the PCI bus.
  83. pci_bus_type is a global. For USB busses use usb_bus_type. */
  84. drv = driver_find("ivtv", &pci_bus_type);
  85. /* iterate over all ivtv device instances */
  86. err = driver_for_each_device(drv, NULL, p, callback);
  87. put_driver(drv);
  88. return err;
  89. }
  90. Sometimes you need to keep a running counter of the device instance. This is
  91. commonly used to map a device instance to an index of a module option array.
  92. The recommended approach is as follows:
  93. static atomic_t drv_instance = ATOMIC_INIT(0);
  94. static int __devinit drv_probe(struct pci_dev *dev,
  95. const struct pci_device_id *pci_id)
  96. {
  97. ...
  98. state->instance = atomic_inc_return(&drv_instance) - 1;
  99. }
  100. struct v4l2_subdev
  101. ------------------
  102. Many drivers need to communicate with sub-devices. These devices can do all
  103. sort of tasks, but most commonly they handle audio and/or video muxing,
  104. encoding or decoding. For webcams common sub-devices are sensors and camera
  105. controllers.
  106. Usually these are I2C devices, but not necessarily. In order to provide the
  107. driver with a consistent interface to these sub-devices the v4l2_subdev struct
  108. (v4l2-subdev.h) was created.
  109. Each sub-device driver must have a v4l2_subdev struct. This struct can be
  110. stand-alone for simple sub-devices or it might be embedded in a larger struct
  111. if more state information needs to be stored. Usually there is a low-level
  112. device struct (e.g. i2c_client) that contains the device data as setup
  113. by the kernel. It is recommended to store that pointer in the private
  114. data of v4l2_subdev using v4l2_set_subdevdata(). That makes it easy to go
  115. from a v4l2_subdev to the actual low-level bus-specific device data.
  116. You also need a way to go from the low-level struct to v4l2_subdev. For the
  117. common i2c_client struct the i2c_set_clientdata() call is used to store a
  118. v4l2_subdev pointer, for other busses you may have to use other methods.
  119. From the bridge driver perspective you load the sub-device module and somehow
  120. obtain the v4l2_subdev pointer. For i2c devices this is easy: you call
  121. i2c_get_clientdata(). For other busses something similar needs to be done.
  122. Helper functions exists for sub-devices on an I2C bus that do most of this
  123. tricky work for you.
  124. Each v4l2_subdev contains function pointers that sub-device drivers can
  125. implement (or leave NULL if it is not applicable). Since sub-devices can do
  126. so many different things and you do not want to end up with a huge ops struct
  127. of which only a handful of ops are commonly implemented, the function pointers
  128. are sorted according to category and each category has its own ops struct.
  129. The top-level ops struct contains pointers to the category ops structs, which
  130. may be NULL if the subdev driver does not support anything from that category.
  131. It looks like this:
  132. struct v4l2_subdev_core_ops {
  133. int (*g_chip_ident)(struct v4l2_subdev *sd, struct v4l2_chip_ident *chip);
  134. int (*log_status)(struct v4l2_subdev *sd);
  135. int (*init)(struct v4l2_subdev *sd, u32 val);
  136. ...
  137. };
  138. struct v4l2_subdev_tuner_ops {
  139. ...
  140. };
  141. struct v4l2_subdev_audio_ops {
  142. ...
  143. };
  144. struct v4l2_subdev_video_ops {
  145. ...
  146. };
  147. struct v4l2_subdev_ops {
  148. const struct v4l2_subdev_core_ops *core;
  149. const struct v4l2_subdev_tuner_ops *tuner;
  150. const struct v4l2_subdev_audio_ops *audio;
  151. const struct v4l2_subdev_video_ops *video;
  152. };
  153. The core ops are common to all subdevs, the other categories are implemented
  154. depending on the sub-device. E.g. a video device is unlikely to support the
  155. audio ops and vice versa.
  156. This setup limits the number of function pointers while still making it easy
  157. to add new ops and categories.
  158. A sub-device driver initializes the v4l2_subdev struct using:
  159. v4l2_subdev_init(subdev, &ops);
  160. Afterwards you need to initialize subdev->name with a unique name and set the
  161. module owner. This is done for you if you use the i2c helper functions.
  162. A device (bridge) driver needs to register the v4l2_subdev with the
  163. v4l2_device:
  164. int err = v4l2_device_register_subdev(device, subdev);
  165. This can fail if the subdev module disappeared before it could be registered.
  166. After this function was called successfully the subdev->dev field points to
  167. the v4l2_device.
  168. You can unregister a sub-device using:
  169. v4l2_device_unregister_subdev(subdev);
  170. Afterwards the subdev module can be unloaded and subdev->dev == NULL.
  171. You can call an ops function either directly:
  172. err = subdev->ops->core->g_chip_ident(subdev, &chip);
  173. but it is better and easier to use this macro:
  174. err = v4l2_subdev_call(subdev, core, g_chip_ident, &chip);
  175. The macro will to the right NULL pointer checks and returns -ENODEV if subdev
  176. is NULL, -ENOIOCTLCMD if either subdev->core or subdev->core->g_chip_ident is
  177. NULL, or the actual result of the subdev->ops->core->g_chip_ident ops.
  178. It is also possible to call all or a subset of the sub-devices:
  179. v4l2_device_call_all(dev, 0, core, g_chip_ident, &chip);
  180. Any subdev that does not support this ops is skipped and error results are
  181. ignored. If you want to check for errors use this:
  182. err = v4l2_device_call_until_err(dev, 0, core, g_chip_ident, &chip);
  183. Any error except -ENOIOCTLCMD will exit the loop with that error. If no
  184. errors (except -ENOIOCTLCMD) occured, then 0 is returned.
  185. The second argument to both calls is a group ID. If 0, then all subdevs are
  186. called. If non-zero, then only those whose group ID match that value will
  187. be called. Before a bridge driver registers a subdev it can set subdev->grp_id
  188. to whatever value it wants (it's 0 by default). This value is owned by the
  189. bridge driver and the sub-device driver will never modify or use it.
  190. The group ID gives the bridge driver more control how callbacks are called.
  191. For example, there may be multiple audio chips on a board, each capable of
  192. changing the volume. But usually only one will actually be used when the
  193. user want to change the volume. You can set the group ID for that subdev to
  194. e.g. AUDIO_CONTROLLER and specify that as the group ID value when calling
  195. v4l2_device_call_all(). That ensures that it will only go to the subdev
  196. that needs it.
  197. The advantage of using v4l2_subdev is that it is a generic struct and does
  198. not contain any knowledge about the underlying hardware. So a driver might
  199. contain several subdevs that use an I2C bus, but also a subdev that is
  200. controlled through GPIO pins. This distinction is only relevant when setting
  201. up the device, but once the subdev is registered it is completely transparent.
  202. I2C sub-device drivers
  203. ----------------------
  204. Since these drivers are so common, special helper functions are available to
  205. ease the use of these drivers (v4l2-common.h).
  206. The recommended method of adding v4l2_subdev support to an I2C driver is to
  207. embed the v4l2_subdev struct into the state struct that is created for each
  208. I2C device instance. Very simple devices have no state struct and in that case
  209. you can just create a v4l2_subdev directly.
  210. A typical state struct would look like this (where 'chipname' is replaced by
  211. the name of the chip):
  212. struct chipname_state {
  213. struct v4l2_subdev sd;
  214. ... /* additional state fields */
  215. };
  216. Initialize the v4l2_subdev struct as follows:
  217. v4l2_i2c_subdev_init(&state->sd, client, subdev_ops);
  218. This function will fill in all the fields of v4l2_subdev and ensure that the
  219. v4l2_subdev and i2c_client both point to one another.
  220. You should also add a helper inline function to go from a v4l2_subdev pointer
  221. to a chipname_state struct:
  222. static inline struct chipname_state *to_state(struct v4l2_subdev *sd)
  223. {
  224. return container_of(sd, struct chipname_state, sd);
  225. }
  226. Use this to go from the v4l2_subdev struct to the i2c_client struct:
  227. struct i2c_client *client = v4l2_get_subdevdata(sd);
  228. And this to go from an i2c_client to a v4l2_subdev struct:
  229. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  230. Finally you need to make a command function to make driver->command()
  231. call the right subdev_ops functions:
  232. static int subdev_command(struct i2c_client *client, unsigned cmd, void *arg)
  233. {
  234. return v4l2_subdev_command(i2c_get_clientdata(client), cmd, arg);
  235. }
  236. If driver->command is never used then you can leave this out. Eventually the
  237. driver->command usage should be removed from v4l.
  238. Make sure to call v4l2_device_unregister_subdev(sd) when the remove() callback
  239. is called. This will unregister the sub-device from the bridge driver. It is
  240. safe to call this even if the sub-device was never registered.
  241. The bridge driver also has some helper functions it can use:
  242. struct v4l2_subdev *sd = v4l2_i2c_new_subdev(adapter, "module_foo", "chipid", 0x36);
  243. This loads the given module (can be NULL if no module needs to be loaded) and
  244. calls i2c_new_device() with the given i2c_adapter and chip/address arguments.
  245. If all goes well, then it registers the subdev with the v4l2_device. It gets
  246. the v4l2_device by calling i2c_get_adapdata(adapter), so you should make sure
  247. that adapdata is set to v4l2_device when you setup the i2c_adapter in your
  248. driver.
  249. You can also use v4l2_i2c_new_probed_subdev() which is very similar to
  250. v4l2_i2c_new_subdev(), except that it has an array of possible I2C addresses
  251. that it should probe. Internally it calls i2c_new_probed_device().
  252. Both functions return NULL if something went wrong.
  253. struct video_device
  254. -------------------
  255. Not yet documented.