media-framework.txt 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. Linux kernel media framework
  2. ============================
  3. This document describes the Linux kernel media framework, its data structures,
  4. functions and their usage.
  5. Introduction
  6. ------------
  7. The media controller API is documented in DocBook format in
  8. Documentation/DocBook/v4l/media-controller.xml. This document will focus on
  9. the kernel-side implementation of the media framework.
  10. Abstract media device model
  11. ---------------------------
  12. Discovering a device internal topology, and configuring it at runtime, is one
  13. of the goals of the media framework. To achieve this, hardware devices are
  14. modeled as an oriented graph of building blocks called entities connected
  15. through pads.
  16. An entity is a basic media hardware building block. It can correspond to
  17. a large variety of logical blocks such as physical hardware devices
  18. (CMOS sensor for instance), logical hardware devices (a building block
  19. in a System-on-Chip image processing pipeline), DMA channels or physical
  20. connectors.
  21. A pad is a connection endpoint through which an entity can interact with
  22. other entities. Data (not restricted to video) produced by an entity
  23. flows from the entity's output to one or more entity inputs. Pads should
  24. not be confused with physical pins at chip boundaries.
  25. A link is a point-to-point oriented connection between two pads, either
  26. on the same entity or on different entities. Data flows from a source
  27. pad to a sink pad.
  28. Media device
  29. ------------
  30. A media device is represented by a struct media_device instance, defined in
  31. include/media/media-device.h. Allocation of the structure is handled by the
  32. media device driver, usually by embedding the media_device instance in a
  33. larger driver-specific structure.
  34. Drivers register media device instances by calling
  35. media_device_register(struct media_device *mdev);
  36. The caller is responsible for initializing the media_device structure before
  37. registration. The following fields must be set:
  38. - dev must point to the parent device (usually a pci_dev, usb_interface or
  39. platform_device instance).
  40. - model must be filled with the device model name as a NUL-terminated UTF-8
  41. string. The device/model revision must not be stored in this field.
  42. The following fields are optional:
  43. - serial is a unique serial number stored as a NUL-terminated ASCII string.
  44. The field is big enough to store a GUID in text form. If the hardware
  45. doesn't provide a unique serial number this field must be left empty.
  46. - bus_info represents the location of the device in the system as a
  47. NUL-terminated ASCII string. For PCI/PCIe devices bus_info must be set to
  48. "PCI:" (or "PCIe:") followed by the value of pci_name(). For USB devices,
  49. the usb_make_path() function must be used. This field is used by
  50. applications to distinguish between otherwise identical devices that don't
  51. provide a serial number.
  52. - hw_revision is the hardware device revision in a driver-specific format.
  53. When possible the revision should be formatted with the KERNEL_VERSION
  54. macro.
  55. - driver_version is formatted with the KERNEL_VERSION macro. The version
  56. minor must be incremented when new features are added to the userspace API
  57. without breaking binary compatibility. The version major must be
  58. incremented when binary compatibility is broken.
  59. Upon successful registration a character device named media[0-9]+ is created.
  60. The device major and minor numbers are dynamic. The model name is exported as
  61. a sysfs attribute.
  62. Drivers unregister media device instances by calling
  63. media_device_unregister(struct media_device *mdev);
  64. Unregistering a media device that hasn't been registered is *NOT* safe.
  65. Entities, pads and links
  66. ------------------------
  67. - Entities
  68. Entities are represented by a struct media_entity instance, defined in
  69. include/media/media-entity.h. The structure is usually embedded into a
  70. higher-level structure, such as a v4l2_subdev or video_device instance,
  71. although drivers can allocate entities directly.
  72. Drivers initialize entities by calling
  73. media_entity_init(struct media_entity *entity, u16 num_pads,
  74. struct media_pad *pads, u16 extra_links);
  75. The media_entity name, type, flags, revision and group_id fields can be
  76. initialized before or after calling media_entity_init. Entities embedded in
  77. higher-level standard structures can have some of those fields set by the
  78. higher-level framework.
  79. As the number of pads is known in advance, the pads array is not allocated
  80. dynamically but is managed by the entity driver. Most drivers will embed the
  81. pads array in a driver-specific structure, avoiding dynamic allocation.
  82. Drivers must set the direction of every pad in the pads array before calling
  83. media_entity_init. The function will initialize the other pads fields.
  84. Unlike the number of pads, the total number of links isn't always known in
  85. advance by the entity driver. As an initial estimate, media_entity_init
  86. pre-allocates a number of links equal to the number of pads plus an optional
  87. number of extra links. The links array will be reallocated if it grows beyond
  88. the initial estimate.
  89. Drivers register entities with a media device by calling
  90. media_device_register_entity(struct media_device *mdev,
  91. struct media_entity *entity);
  92. Entities are identified by a unique positive integer ID. Drivers can provide an
  93. ID by filling the media_entity id field prior to registration, or request the
  94. media controller framework to assign an ID automatically. Drivers that provide
  95. IDs manually must ensure that all IDs are unique. IDs are not guaranteed to be
  96. contiguous even when they are all assigned automatically by the framework.
  97. Drivers unregister entities by calling
  98. media_device_unregister_entity(struct media_entity *entity);
  99. Unregistering an entity will not change the IDs of the other entities, and the
  100. ID will never be reused for a newly registered entity.
  101. When a media device is unregistered, all its entities are unregistered
  102. automatically. No manual entities unregistration is then required.
  103. Drivers free resources associated with an entity by calling
  104. media_entity_cleanup(struct media_entity *entity);
  105. This function must be called during the cleanup phase after unregistering the
  106. entity. Note that the media_entity instance itself must be freed explicitly by
  107. the driver if required.
  108. Entities have flags that describe the entity capabilities and state.
  109. MEDIA_ENT_FL_DEFAULT indicates the default entity for a given type.
  110. This can be used to report the default audio and video devices or the
  111. default camera sensor.
  112. Logical entity groups can be defined by setting the group ID of all member
  113. entities to the same non-zero value. An entity group serves no purpose in the
  114. kernel, but is reported to userspace during entities enumeration. The group_id
  115. field belongs to the media device driver and must not by touched by entity
  116. drivers.
  117. Media device drivers should define groups if several entities are logically
  118. bound together. Example usages include reporting
  119. - ALSA, VBI and video nodes that carry the same media stream
  120. - lens and flash controllers associated with a sensor
  121. - Pads
  122. Pads are represented by a struct media_pad instance, defined in
  123. include/media/media-entity.h. Each entity stores its pads in a pads array
  124. managed by the entity driver. Drivers usually embed the array in a
  125. driver-specific structure.
  126. Pads are identified by their entity and their 0-based index in the pads array.
  127. Both information are stored in the media_pad structure, making the media_pad
  128. pointer the canonical way to store and pass link references.
  129. Pads have flags that describe the pad capabilities and state.
  130. MEDIA_PAD_FL_SINK indicates that the pad supports sinking data.
  131. MEDIA_PAD_FL_SOURCE indicates that the pad supports sourcing data.
  132. One and only one of MEDIA_PAD_FL_SINK and MEDIA_PAD_FL_SOURCE must be set for
  133. each pad.
  134. - Links
  135. Links are represented by a struct media_link instance, defined in
  136. include/media/media-entity.h. Each entity stores all links originating at or
  137. targeting any of its pads in a links array. A given link is thus stored
  138. twice, once in the source entity and once in the target entity. The array is
  139. pre-allocated and grows dynamically as needed.
  140. Drivers create links by calling
  141. media_entity_create_link(struct media_entity *source, u16 source_pad,
  142. struct media_entity *sink, u16 sink_pad,
  143. u32 flags);
  144. An entry in the link array of each entity is allocated and stores pointers
  145. to source and sink pads.
  146. Links have flags that describe the link capabilities and state.
  147. MEDIA_LNK_FL_ENABLED indicates that the link is enabled and can be used
  148. to transfer media data. When two or more links target a sink pad, only
  149. one of them can be enabled at a time.
  150. MEDIA_LNK_FL_IMMUTABLE indicates that the link enabled state can't be
  151. modified at runtime. If MEDIA_LNK_FL_IMMUTABLE is set, then
  152. MEDIA_LNK_FL_ENABLED must also be set since an immutable link is always
  153. enabled.
  154. Graph traversal
  155. ---------------
  156. The media framework provides APIs to iterate over entities in a graph.
  157. To iterate over all entities belonging to a media device, drivers can use the
  158. media_device_for_each_entity macro, defined in include/media/media-device.h.
  159. struct media_entity *entity;
  160. media_device_for_each_entity(entity, mdev) {
  161. /* entity will point to each entity in turn */
  162. ...
  163. }
  164. Drivers might also need to iterate over all entities in a graph that can be
  165. reached only through enabled links starting at a given entity. The media
  166. framework provides a depth-first graph traversal API for that purpose.
  167. Note that graphs with cycles (whether directed or undirected) are *NOT*
  168. supported by the graph traversal API. To prevent infinite loops, the graph
  169. traversal code limits the maximum depth to MEDIA_ENTITY_ENUM_MAX_DEPTH,
  170. currently defined as 16.
  171. Drivers initiate a graph traversal by calling
  172. media_entity_graph_walk_start(struct media_entity_graph *graph,
  173. struct media_entity *entity);
  174. The graph structure, provided by the caller, is initialized to start graph
  175. traversal at the given entity.
  176. Drivers can then retrieve the next entity by calling
  177. media_entity_graph_walk_next(struct media_entity_graph *graph);
  178. When the graph traversal is complete the function will return NULL.
  179. Graph traversal can be interrupted at any moment. No cleanup function call is
  180. required and the graph structure can be freed normally.
  181. Helper functions can be used to find a link between two given pads, or a pad
  182. connected to another pad through an enabled link
  183. media_entity_find_link(struct media_pad *source,
  184. struct media_pad *sink);
  185. media_entity_remote_source(struct media_pad *pad);
  186. Refer to the kerneldoc documentation for more information.
  187. Use count and power handling
  188. ----------------------------
  189. Due to the wide differences between drivers regarding power management needs,
  190. the media controller does not implement power management. However, the
  191. media_entity structure includes a use_count field that media drivers can use to
  192. track the number of users of every entity for power management needs.
  193. The use_count field is owned by media drivers and must not be touched by entity
  194. drivers. Access to the field must be protected by the media device graph_mutex
  195. lock.
  196. Links setup
  197. -----------
  198. Link properties can be modified at runtime by calling
  199. media_entity_setup_link(struct media_link *link, u32 flags);
  200. The flags argument contains the requested new link flags.
  201. The only configurable property is the ENABLED link flag to enable/disable a
  202. link. Links marked with the IMMUTABLE link flag can not be enabled or disabled.
  203. When a link is enabled or disabled, the media framework calls the
  204. link_setup operation for the two entities at the source and sink of the link,
  205. in that order. If the second link_setup call fails, another link_setup call is
  206. made on the first entity to restore the original link flags.
  207. Media device drivers can be notified of link setup operations by setting the
  208. media_device::link_notify pointer to a callback function. If provided, the
  209. notification callback will be called before enabling and after disabling
  210. links.
  211. Entity drivers must implement the link_setup operation if any of their links
  212. is non-immutable. The operation must either configure the hardware or store
  213. the configuration information to be applied later.
  214. Link configuration must not have any side effect on other links. If an enabled
  215. link at a sink pad prevents another link at the same pad from being disabled,
  216. the link_setup operation must return -EBUSY and can't implicitly disable the
  217. first enabled link.
  218. Pipelines and media streams
  219. ---------------------------
  220. When starting streaming, drivers must notify all entities in the pipeline to
  221. prevent link states from being modified during streaming by calling
  222. media_entity_pipeline_start(struct media_entity *entity,
  223. struct media_pipeline *pipe);
  224. The function will mark all entities connected to the given entity through
  225. enabled links, either directly or indirectly, as streaming.
  226. The media_pipeline instance pointed to by the pipe argument will be stored in
  227. every entity in the pipeline. Drivers should embed the media_pipeline structure
  228. in higher-level pipeline structures and can then access the pipeline through
  229. the media_entity pipe field.
  230. Calls to media_entity_pipeline_start() can be nested. The pipeline pointer must
  231. be identical for all nested calls to the function.
  232. When stopping the stream, drivers must notify the entities with
  233. media_entity_pipeline_stop(struct media_entity *entity);
  234. If multiple calls to media_entity_pipeline_start() have been made the same
  235. number of media_entity_pipeline_stop() calls are required to stop streaming. The
  236. media_entity pipe field is reset to NULL on the last nested stop call.
  237. Link configuration will fail with -EBUSY by default if either end of the link is
  238. a streaming entity. Links that can be modified while streaming must be marked
  239. with the MEDIA_LNK_FL_DYNAMIC flag.
  240. If other operations need to be disallowed on streaming entities (such as
  241. changing entities configuration parameters) drivers can explicitly check the
  242. media_entity stream_count field to find out if an entity is streaming. This
  243. operation must be done with the media_device graph_mutex held.