mc-core.rst 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. Media Controller devices
  2. ------------------------
  3. Media Controller
  4. ~~~~~~~~~~~~~~~~
  5. The media controller userspace API is documented in
  6. :ref:`the Media Controller uAPI book <media_controller>`. This document focus
  7. on the kernel-side implementation of the media framework.
  8. Abstract media device model
  9. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  10. Discovering a device internal topology, and configuring it at runtime, is one
  11. of the goals of the media framework. To achieve this, hardware devices are
  12. modelled as an oriented graph of building blocks called entities connected
  13. through pads.
  14. An entity is a basic media hardware building block. It can correspond to
  15. a large variety of logical blocks such as physical hardware devices
  16. (CMOS sensor for instance), logical hardware devices (a building block
  17. in a System-on-Chip image processing pipeline), DMA channels or physical
  18. connectors.
  19. A pad is a connection endpoint through which an entity can interact with
  20. other entities. Data (not restricted to video) produced by an entity
  21. flows from the entity's output to one or more entity inputs. Pads should
  22. not be confused with physical pins at chip boundaries.
  23. A link is a point-to-point oriented connection between two pads, either
  24. on the same entity or on different entities. Data flows from a source
  25. pad to a sink pad.
  26. Media device
  27. ^^^^^^^^^^^^
  28. A media device is represented by a struct :c:type:`media_device`
  29. instance, defined in ``include/media/media-device.h``.
  30. Allocation of the structure is handled by the media device driver, usually by
  31. embedding the :c:type:`media_device` instance in a larger driver-specific
  32. structure.
  33. Drivers register media device instances by calling
  34. :c:func:`__media_device_register()` via the macro ``media_device_register()``
  35. and unregistered by calling :c:func:`media_device_unregister()`.
  36. Entities
  37. ^^^^^^^^
  38. Entities are represented by a struct :c:type:`media_entity`
  39. instance, defined in ``include/media/media-entity.h``. The structure is usually
  40. embedded into a higher-level structure, such as
  41. :c:type:`v4l2_subdev` or :c:type:`video_device`
  42. instances, although drivers can allocate entities directly.
  43. Drivers initialize entity pads by calling
  44. :c:func:`media_entity_pads_init()`.
  45. Drivers register entities with a media device by calling
  46. :c:func:`media_device_register_entity()`
  47. and unregistred by calling
  48. :c:func:`media_device_unregister_entity()`.
  49. Interfaces
  50. ^^^^^^^^^^
  51. Interfaces are represented by a
  52. struct :c:type:`media_interface` instance, defined in
  53. ``include/media/media-entity.h``. Currently, only one type of interface is
  54. defined: a device node. Such interfaces are represented by a
  55. struct :c:type:`media_intf_devnode`.
  56. Drivers initialize and create device node interfaces by calling
  57. :c:func:`media_devnode_create()`
  58. and remove them by calling:
  59. :c:func:`media_devnode_remove()`.
  60. Pads
  61. ^^^^
  62. Pads are represented by a struct :c:type:`media_pad` instance,
  63. defined in ``include/media/media-entity.h``. Each entity stores its pads in
  64. a pads array managed by the entity driver. Drivers usually embed the array in
  65. a driver-specific structure.
  66. Pads are identified by their entity and their 0-based index in the pads
  67. array.
  68. Both information are stored in the struct :c:type:`media_pad`,
  69. making the struct :c:type:`media_pad` pointer the canonical way
  70. to store and pass link references.
  71. Pads have flags that describe the pad capabilities and state.
  72. ``MEDIA_PAD_FL_SINK`` indicates that the pad supports sinking data.
  73. ``MEDIA_PAD_FL_SOURCE`` indicates that the pad supports sourcing data.
  74. .. note::
  75. One and only one of ``MEDIA_PAD_FL_SINK`` or ``MEDIA_PAD_FL_SOURCE`` must
  76. be set for each pad.
  77. Links
  78. ^^^^^
  79. Links are represented by a struct :c:type:`media_link` instance,
  80. defined in ``include/media/media-entity.h``. There are two types of links:
  81. **1. pad to pad links**:
  82. Associate two entities via their PADs. Each entity has a list that points
  83. to all links originating at or targeting any of its pads.
  84. A given link is thus stored twice, once in the source entity and once in
  85. the target entity.
  86. Drivers create pad to pad links by calling:
  87. :c:func:`media_create_pad_link()` and remove with
  88. :c:func:`media_entity_remove_links()`.
  89. **2. interface to entity links**:
  90. Associate one interface to a Link.
  91. Drivers create interface to entity links by calling:
  92. :c:func:`media_create_intf_link()` and remove with
  93. :c:func:`media_remove_intf_links()`.
  94. .. note::
  95. Links can only be created after having both ends already created.
  96. Links have flags that describe the link capabilities and state. The
  97. valid values are described at :c:func:`media_create_pad_link()` and
  98. :c:func:`media_create_intf_link()`.
  99. Graph traversal
  100. ^^^^^^^^^^^^^^^
  101. The media framework provides APIs to iterate over entities in a graph.
  102. To iterate over all entities belonging to a media device, drivers can use
  103. the media_device_for_each_entity macro, defined in
  104. ``include/media/media-device.h``.
  105. .. code-block:: c
  106. struct media_entity *entity;
  107. media_device_for_each_entity(entity, mdev) {
  108. // entity will point to each entity in turn
  109. ...
  110. }
  111. Drivers might also need to iterate over all entities in a graph that can be
  112. reached only through enabled links starting at a given entity. The media
  113. framework provides a depth-first graph traversal API for that purpose.
  114. .. note::
  115. Graphs with cycles (whether directed or undirected) are **NOT**
  116. supported by the graph traversal API. To prevent infinite loops, the graph
  117. traversal code limits the maximum depth to ``MEDIA_ENTITY_ENUM_MAX_DEPTH``,
  118. currently defined as 16.
  119. Drivers initiate a graph traversal by calling
  120. :c:func:`media_entity_graph_walk_start()`
  121. The graph structure, provided by the caller, is initialized to start graph
  122. traversal at the given entity.
  123. Drivers can then retrieve the next entity by calling
  124. :c:func:`media_entity_graph_walk_next()`
  125. When the graph traversal is complete the function will return ``NULL``.
  126. Graph traversal can be interrupted at any moment. No cleanup function call
  127. is required and the graph structure can be freed normally.
  128. Helper functions can be used to find a link between two given pads, or a pad
  129. connected to another pad through an enabled link
  130. :c:func:`media_entity_find_link()` and
  131. :c:func:`media_entity_remote_pad()`.
  132. Use count and power handling
  133. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  134. Due to the wide differences between drivers regarding power management
  135. needs, the media controller does not implement power management. However,
  136. the struct :c:type:`media_entity` includes a ``use_count``
  137. field that media drivers
  138. can use to track the number of users of every entity for power management
  139. needs.
  140. The :c:type:`media_entity<media_entity>`.\ ``use_count`` field is owned by
  141. media drivers and must not be
  142. touched by entity drivers. Access to the field must be protected by the
  143. :c:type:`media_device`.\ ``graph_mutex`` lock.
  144. Links setup
  145. ^^^^^^^^^^^
  146. Link properties can be modified at runtime by calling
  147. :c:func:`media_entity_setup_link()`.
  148. Pipelines and media streams
  149. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  150. When starting streaming, drivers must notify all entities in the pipeline to
  151. prevent link states from being modified during streaming by calling
  152. :c:func:`media_entity_pipeline_start()`.
  153. The function will mark all entities connected to the given entity through
  154. enabled links, either directly or indirectly, as streaming.
  155. The struct :c:type:`media_pipeline` instance pointed to by
  156. the pipe argument will be stored in every entity in the pipeline.
  157. Drivers should embed the struct :c:type:`media_pipeline`
  158. in higher-level pipeline structures and can then access the
  159. pipeline through the struct :c:type:`media_entity`
  160. pipe field.
  161. Calls to :c:func:`media_entity_pipeline_start()` can be nested.
  162. The pipeline pointer must be identical for all nested calls to the function.
  163. :c:func:`media_entity_pipeline_start()` may return an error. In that case,
  164. it will clean up any of the changes it did by itself.
  165. When stopping the stream, drivers must notify the entities with
  166. :c:func:`media_entity_pipeline_stop()`.
  167. If multiple calls to :c:func:`media_entity_pipeline_start()` have been
  168. made the same number of :c:func:`media_entity_pipeline_stop()` calls
  169. are required to stop streaming.
  170. The :c:type:`media_entity`.\ ``pipe`` field is reset to ``NULL`` on the last
  171. nested stop call.
  172. Link configuration will fail with ``-EBUSY`` by default if either end of the
  173. link is a streaming entity. Links that can be modified while streaming must
  174. be marked with the ``MEDIA_LNK_FL_DYNAMIC`` flag.
  175. If other operations need to be disallowed on streaming entities (such as
  176. changing entities configuration parameters) drivers can explicitly check the
  177. media_entity stream_count field to find out if an entity is streaming. This
  178. operation must be done with the media_device graph_mutex held.
  179. Link validation
  180. ^^^^^^^^^^^^^^^
  181. Link validation is performed by :c:func:`media_entity_pipeline_start()`
  182. for any entity which has sink pads in the pipeline. The
  183. :c:type:`media_entity`.\ ``link_validate()`` callback is used for that
  184. purpose. In ``link_validate()`` callback, entity driver should check
  185. that the properties of the source pad of the connected entity and its own
  186. sink pad match. It is up to the type of the entity (and in the end, the
  187. properties of the hardware) what matching actually means.
  188. Subsystems should facilitate link validation by providing subsystem specific
  189. helper functions to provide easy access for commonly needed information, and
  190. in the end provide a way to use driver-specific callbacks.
  191. .. kernel-doc:: include/media/media-device.h
  192. .. kernel-doc:: include/media/media-devnode.h
  193. .. kernel-doc:: include/media/media-entity.h