mc-core.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. .. SPDX-License-Identifier: GPL-2.0
  2. Media Controller devices
  3. ------------------------
  4. Media Controller
  5. ~~~~~~~~~~~~~~~~
  6. The media controller userspace API is documented in
  7. :ref:`the Media Controller uAPI book <media_controller>`. This document focus
  8. on the kernel-side implementation of the media framework.
  9. Abstract media device model
  10. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  11. Discovering a device internal topology, and configuring it at runtime, is one
  12. of the goals of the media framework. To achieve this, hardware devices are
  13. modelled as an oriented graph of building blocks called entities connected
  14. through pads.
  15. An entity is a basic media hardware building block. It can correspond to
  16. a large variety of logical blocks such as physical hardware devices
  17. (CMOS sensor for instance), logical hardware devices (a building block
  18. in a System-on-Chip image processing pipeline), DMA channels or physical
  19. connectors.
  20. A pad is a connection endpoint through which an entity can interact with
  21. other entities. Data (not restricted to video) produced by an entity
  22. flows from the entity's output to one or more entity inputs. Pads should
  23. not be confused with physical pins at chip boundaries.
  24. A link is a point-to-point oriented connection between two pads, either
  25. on the same entity or on different entities. Data flows from a source
  26. pad to a sink pad.
  27. Media device
  28. ^^^^^^^^^^^^
  29. A media device is represented by a struct media_device
  30. instance, defined in ``include/media/media-device.h``.
  31. Allocation of the structure is handled by the media device driver, usually by
  32. embedding the :c:type:`media_device` instance in a larger driver-specific
  33. structure.
  34. Drivers initialise media device instances by calling
  35. :c:func:`media_device_init()`. After initialising a media device instance, it is
  36. registered by calling :c:func:`__media_device_register()` via the macro
  37. ``media_device_register()`` and unregistered by calling
  38. :c:func:`media_device_unregister()`. An initialised media device must be
  39. eventually cleaned up by calling :c:func:`media_device_cleanup()`.
  40. Note that it is not allowed to unregister a media device instance that was not
  41. previously registered, or clean up a media device instance that was not
  42. previously initialised.
  43. Entities
  44. ^^^^^^^^
  45. Entities are represented by a struct media_entity
  46. instance, defined in ``include/media/media-entity.h``. The structure is usually
  47. embedded into a higher-level structure, such as
  48. :c:type:`v4l2_subdev` or :c:type:`video_device`
  49. instances, although drivers can allocate entities directly.
  50. Drivers initialize entity pads by calling
  51. :c:func:`media_entity_pads_init()`.
  52. Drivers register entities with a media device by calling
  53. :c:func:`media_device_register_entity()`
  54. and unregistered by calling
  55. :c:func:`media_device_unregister_entity()`.
  56. Interfaces
  57. ^^^^^^^^^^
  58. Interfaces are represented by a
  59. struct media_interface instance, defined in
  60. ``include/media/media-entity.h``. Currently, only one type of interface is
  61. defined: a device node. Such interfaces are represented by a
  62. struct media_intf_devnode.
  63. Drivers initialize and create device node interfaces by calling
  64. :c:func:`media_devnode_create()`
  65. and remove them by calling:
  66. :c:func:`media_devnode_remove()`.
  67. Pads
  68. ^^^^
  69. Pads are represented by a struct media_pad instance,
  70. defined in ``include/media/media-entity.h``. Each entity stores its pads in
  71. a pads array managed by the entity driver. Drivers usually embed the array in
  72. a driver-specific structure.
  73. Pads are identified by their entity and their 0-based index in the pads
  74. array.
  75. Both information are stored in the struct media_pad,
  76. making the struct media_pad pointer the canonical way
  77. to store and pass link references.
  78. Pads have flags that describe the pad capabilities and state.
  79. ``MEDIA_PAD_FL_SINK`` indicates that the pad supports sinking data.
  80. ``MEDIA_PAD_FL_SOURCE`` indicates that the pad supports sourcing data.
  81. .. note::
  82. One and only one of ``MEDIA_PAD_FL_SINK`` or ``MEDIA_PAD_FL_SOURCE`` must
  83. be set for each pad.
  84. Links
  85. ^^^^^
  86. Links are represented by a struct media_link instance,
  87. defined in ``include/media/media-entity.h``. There are two types of links:
  88. **1. pad to pad links**:
  89. Associate two entities via their PADs. Each entity has a list that points
  90. to all links originating at or targeting any of its pads.
  91. A given link is thus stored twice, once in the source entity and once in
  92. the target entity.
  93. Drivers create pad to pad links by calling:
  94. :c:func:`media_create_pad_link()` and remove with
  95. :c:func:`media_entity_remove_links()`.
  96. **2. interface to entity links**:
  97. Associate one interface to a Link.
  98. Drivers create interface to entity links by calling:
  99. :c:func:`media_create_intf_link()` and remove with
  100. :c:func:`media_remove_intf_links()`.
  101. .. note::
  102. Links can only be created after having both ends already created.
  103. Links have flags that describe the link capabilities and state. The
  104. valid values are described at :c:func:`media_create_pad_link()` and
  105. :c:func:`media_create_intf_link()`.
  106. Graph traversal
  107. ^^^^^^^^^^^^^^^
  108. The media framework provides APIs to traverse media graphs, locating connected
  109. entities and links.
  110. To iterate over all entities belonging to a media device, drivers can use
  111. the media_device_for_each_entity macro, defined in
  112. ``include/media/media-device.h``.
  113. .. code-block:: c
  114. struct media_entity *entity;
  115. media_device_for_each_entity(entity, mdev) {
  116. // entity will point to each entity in turn
  117. ...
  118. }
  119. Helper functions can be used to find a link between two given pads, or a pad
  120. connected to another pad through an enabled link
  121. (:c:func:`media_entity_find_link()`, :c:func:`media_pad_remote_pad_first()`,
  122. :c:func:`media_entity_remote_source_pad_unique()` and
  123. :c:func:`media_pad_remote_pad_unique()`).
  124. Use count and power handling
  125. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  126. Due to the wide differences between drivers regarding power management
  127. needs, the media controller does not implement power management. However,
  128. the struct media_entity includes a ``use_count``
  129. field that media drivers
  130. can use to track the number of users of every entity for power management
  131. needs.
  132. The :c:type:`media_entity<media_entity>`.\ ``use_count`` field is owned by
  133. media drivers and must not be
  134. touched by entity drivers. Access to the field must be protected by the
  135. :c:type:`media_device`.\ ``graph_mutex`` lock.
  136. Links setup
  137. ^^^^^^^^^^^
  138. Link properties can be modified at runtime by calling
  139. :c:func:`media_entity_setup_link()`.
  140. Pipelines and media streams
  141. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  142. A media stream is a stream of pixels or metadata originating from one or more
  143. source devices (such as a sensors) and flowing through media entity pads
  144. towards the final sinks. The stream can be modified on the route by the
  145. devices (e.g. scaling or pixel format conversions), or it can be split into
  146. multiple branches, or multiple branches can be merged.
  147. A media pipeline is a set of media streams which are interdependent. This
  148. interdependency can be caused by the hardware (e.g. configuration of a second
  149. stream cannot be changed if the first stream has been enabled) or by the driver
  150. due to the software design. Most commonly a media pipeline consists of a single
  151. stream which does not branch.
  152. When starting streaming, drivers must notify all entities in the pipeline to
  153. prevent link states from being modified during streaming by calling
  154. :c:func:`media_pipeline_start()`.
  155. The function will mark all the pads which are part of the pipeline as streaming.
  156. The struct media_pipeline instance pointed to by the pipe argument will be
  157. stored in every pad in the pipeline. Drivers should embed the struct
  158. media_pipeline in higher-level pipeline structures and can then access the
  159. pipeline through the struct media_pad pipe field.
  160. Calls to :c:func:`media_pipeline_start()` can be nested.
  161. The pipeline pointer must be identical for all nested calls to the function.
  162. :c:func:`media_pipeline_start()` may return an error. In that case,
  163. it will clean up any of the changes it did by itself.
  164. When stopping the stream, drivers must notify the entities with
  165. :c:func:`media_pipeline_stop()`.
  166. If multiple calls to :c:func:`media_pipeline_start()` have been
  167. made the same number of :c:func:`media_pipeline_stop()` calls
  168. are required to stop streaming.
  169. The :c:type:`media_entity`.\ ``pipe`` field is reset to ``NULL`` on the last
  170. nested stop call.
  171. Link configuration will fail with ``-EBUSY`` by default if either end of the
  172. link is a streaming entity. Links that can be modified while streaming must
  173. be marked with the ``MEDIA_LNK_FL_DYNAMIC`` flag.
  174. If other operations need to be disallowed on streaming entities (such as
  175. changing entities configuration parameters) drivers can explicitly check the
  176. media_entity stream_count field to find out if an entity is streaming. This
  177. operation must be done with the media_device graph_mutex held.
  178. Link validation
  179. ^^^^^^^^^^^^^^^
  180. Link validation is performed by :c:func:`media_pipeline_start()`
  181. for any entity which has sink pads in the pipeline. The
  182. :c:type:`media_entity`.\ ``link_validate()`` callback is used for that
  183. purpose. In ``link_validate()`` callback, entity driver should check
  184. that the properties of the source pad of the connected entity and its own
  185. sink pad match. It is up to the type of the entity (and in the end, the
  186. properties of the hardware) what matching actually means.
  187. Subsystems should facilitate link validation by providing subsystem specific
  188. helper functions to provide easy access for commonly needed information, and
  189. in the end provide a way to use driver-specific callbacks.
  190. Pipeline traversal
  191. ^^^^^^^^^^^^^^^^^^
  192. Once a pipeline has been constructed with :c:func:`media_pipeline_start()`,
  193. drivers can iterate over entities or pads in the pipeline with the
  194. :c:macro:´media_pipeline_for_each_entity` and
  195. :c:macro:´media_pipeline_for_each_pad` macros. Iterating over pads is
  196. straightforward:
  197. .. code-block:: c
  198. media_pipeline_pad_iter iter;
  199. struct media_pad *pad;
  200. media_pipeline_for_each_pad(pipe, &iter, pad) {
  201. /* 'pad' will point to each pad in turn */
  202. ...
  203. }
  204. To iterate over entities, the iterator needs to be initialized and cleaned up
  205. as an additional steps:
  206. .. code-block:: c
  207. media_pipeline_entity_iter iter;
  208. struct media_entity *entity;
  209. int ret;
  210. ret = media_pipeline_entity_iter_init(pipe, &iter);
  211. if (ret)
  212. ...;
  213. media_pipeline_for_each_entity(pipe, &iter, entity) {
  214. /* 'entity' will point to each entity in turn */
  215. ...
  216. }
  217. media_pipeline_entity_iter_cleanup(&iter);
  218. Media Controller Device Allocator API
  219. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  220. When the media device belongs to more than one driver, the shared media
  221. device is allocated with the shared struct device as the key for look ups.
  222. The shared media device should stay in registered state until the last
  223. driver unregisters it. In addition, the media device should be released when
  224. all the references are released. Each driver gets a reference to the media
  225. device during probe, when it allocates the media device. If media device is
  226. already allocated, the allocate API bumps up the refcount and returns the
  227. existing media device. The driver puts the reference back in its disconnect
  228. routine when it calls :c:func:`media_device_delete()`.
  229. The media device is unregistered and cleaned up from the kref put handler to
  230. ensure that the media device stays in registered state until the last driver
  231. unregisters the media device.
  232. **Driver Usage**
  233. Drivers should use the appropriate media-core routines to manage the shared
  234. media device life-time handling the two states:
  235. 1. allocate -> register -> delete
  236. 2. get reference to already registered device -> delete
  237. call :c:func:`media_device_delete()` routine to make sure the shared media
  238. device delete is handled correctly.
  239. **driver probe:**
  240. Call :c:func:`media_device_usb_allocate()` to allocate or get a reference
  241. Call :c:func:`media_device_register()`, if media devnode isn't registered
  242. **driver disconnect:**
  243. Call :c:func:`media_device_delete()` to free the media_device. Freeing is
  244. handled by the kref put handler.
  245. API Definitions
  246. ^^^^^^^^^^^^^^^
  247. .. kernel-doc:: include/media/media-device.h
  248. .. kernel-doc:: include/media/media-devnode.h
  249. .. kernel-doc:: include/media/media-entity.h
  250. .. kernel-doc:: include/media/media-request.h
  251. .. kernel-doc:: include/media/media-dev-allocator.h