mediactl.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * Media controller interface library
  3. *
  4. * Copyright (C) 2010-2014 Ideas on board SPRL
  5. *
  6. * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published
  10. * by the Free Software Foundation; either version 2.1 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #ifndef __MEDIA_H__
  22. #define __MEDIA_H__
  23. #include <linux/media.h>
  24. struct media_link {
  25. struct media_pad *source;
  26. struct media_pad *sink;
  27. struct media_link *twin;
  28. __u32 flags;
  29. __u32 padding[3];
  30. };
  31. struct media_pad {
  32. struct media_entity *entity;
  33. __u32 index;
  34. __u32 flags;
  35. __u32 padding[3];
  36. };
  37. struct media_device;
  38. struct media_entity;
  39. /**
  40. * @brief Create a new media device.
  41. * @param devnode - device node path.
  42. *
  43. * Create a media device instance for the given device node and return it. The
  44. * device node is not accessed by this function, device node access errors will
  45. * not be caught and reported here. The media device needs to be enumerated
  46. * before it can be accessed, see media_device_enumerate().
  47. *
  48. * Media devices are reference-counted, see media_device_ref() and
  49. * media_device_unref() for more information.
  50. *
  51. * @return A pointer to the new media device or NULL if memory cannot be
  52. * allocated.
  53. */
  54. struct media_device *media_device_new(const char *devnode);
  55. /**
  56. * @brief Create a new emulated media device.
  57. * @param info - device information.
  58. *
  59. * Emulated media devices are userspace-only objects not backed by a kernel
  60. * media device. They are created for ALSA and V4L2 devices that are not
  61. * associated with a media controller device.
  62. *
  63. * Only device query functions are available for media devices. Enumerating or
  64. * setting up links is invalid.
  65. *
  66. * @return A pointer to the new media device or NULL if memory cannot be
  67. * allocated.
  68. */
  69. struct media_device *media_device_new_emulated(struct media_device_info *info);
  70. /**
  71. * @brief Take a reference to the device.
  72. * @param media - device instance.
  73. *
  74. * Media devices are reference-counted. Taking a reference to a device prevents
  75. * it from being freed until all references are released. The reference count is
  76. * initialized to 1 when the device is created.
  77. *
  78. * @return A pointer to @a media.
  79. */
  80. struct media_device *media_device_ref(struct media_device *media);
  81. /**
  82. * @brief Release a reference to the device.
  83. * @param media - device instance.
  84. *
  85. * Release a reference to the media device. When the reference count reaches 0
  86. * this function frees the device.
  87. */
  88. void media_device_unref(struct media_device *media);
  89. /**
  90. * @brief Add an entity to an existing media device
  91. * @param media - device instance.
  92. * @param desc - description of the entity to be added
  93. * @param devnode - device node corresponding to the entity
  94. *
  95. * Entities are usually created and added to media devices automatically when
  96. * the media device is enumerated through the media controller API. However,
  97. * when an emulated media device (thus not backed with a kernel-side media
  98. * controller device) is created, entities need to be manually added.
  99. *
  100. * Entities can also be manually added to a successfully enumerated media device
  101. * to group several functions provided by separate kernel devices. The most
  102. * common use case is to group the audio and video functions of a USB webcam in
  103. * a single media device. Those functions are exposed through separate USB
  104. * interfaces and handled through unrelated kernel drivers, they must thus be
  105. * manually added to the same media device.
  106. *
  107. * This function adds a new entity to the given media device and initializes it
  108. * from the given entity description and device node name. Only the following
  109. * fields of the description are copied over to the new entity:
  110. *
  111. * - type
  112. * - flags (MEDIA_ENT_FL_DEFAULT only)
  113. * - name
  114. * - v4l, fb, alsa or dvb (depending on the device type)
  115. *
  116. * All other fields of the newly created entity id are initialized to 0,
  117. * including the entity ID.
  118. *
  119. * @return Zero on success or -ENOMEM if memory cannot be allocated.
  120. */
  121. int media_device_add_entity(struct media_device *media,
  122. const struct media_entity_desc *desc,
  123. const char *devnode);
  124. /**
  125. * @brief Set a handler for debug messages.
  126. * @param media - device instance.
  127. * @param debug_handler - debug message handler
  128. * @param debug_priv - first argument to debug message handler
  129. *
  130. * Set a handler for debug messages that will be called whenever
  131. * debugging information is to be printed. The handler expects an
  132. * fprintf-like function.
  133. */
  134. void media_debug_set_handler(
  135. struct media_device *media, void (*debug_handler)(void *, ...),
  136. void *debug_priv);
  137. /**
  138. * @brief Enumerate the device topology
  139. * @param media - device instance.
  140. *
  141. * Enumerate the media device entities, pads and links. Calling this function is
  142. * mandatory before accessing the media device contents.
  143. *
  144. * @return Zero on success or a negative error code on failure.
  145. */
  146. int media_device_enumerate(struct media_device *media);
  147. /**
  148. * @brief Locate the pad at the other end of a link.
  149. * @param pad - sink pad at one end of the link.
  150. *
  151. * Locate the source pad connected to @a pad through an enabled link. As only one
  152. * link connected to a sink pad can be enabled at a time, the connected source
  153. * pad is guaranteed to be unique.
  154. *
  155. * @return A pointer to the connected source pad, or NULL if all links connected
  156. * to @a pad are disabled. Return NULL also if @a pad is not a sink pad.
  157. */
  158. struct media_pad *media_entity_remote_source(struct media_pad *pad);
  159. /**
  160. * @brief Get information about a media entity
  161. * @param entity - media entity.
  162. *
  163. * The information structure is owned by the media entity object and will be
  164. * freed when the object is destroyed.
  165. *
  166. * @return A pointer to the media entity information
  167. */
  168. const struct media_entity_desc *media_entity_get_info(struct media_entity *entity);
  169. /**
  170. * @brief Get an entity pad
  171. * @param entity - media entity.
  172. * @param index - pad index.
  173. *
  174. * This function returns a pointer to the pad object identified by its index
  175. * for the given entity. If the pad index is out of bounds it will return NULL.
  176. *
  177. * @return A pointer to the pad
  178. */
  179. const struct media_pad *media_entity_get_pad(struct media_entity *entity,
  180. unsigned int index);
  181. /**
  182. * @brief Get the number of links
  183. * @param entity - media entity.
  184. *
  185. * This function returns the total number of links that originate from or arrive
  186. * at the the media entity.
  187. *
  188. * @return The number of links for the entity
  189. */
  190. unsigned int media_entity_get_links_count(struct media_entity *entity);
  191. /**
  192. * @brief Get an entity link
  193. * @param entity - media entity.
  194. * @param index - link index.
  195. *
  196. * This function returns a pointer to the link object identified by its index
  197. * for the given entity. If the link index is out of bounds it will return NULL.
  198. *
  199. * @return A pointer to the link
  200. */
  201. const struct media_link *media_entity_get_link(struct media_entity *entity,
  202. unsigned int index);
  203. /**
  204. * @brief Get the device node name for an entity
  205. * @param entity - media entity.
  206. *
  207. * This function returns the full path and name to the device node corresponding
  208. * to the given entity.
  209. *
  210. * @return A pointer to the device node name or NULL if the entity has no
  211. * associated device node
  212. */
  213. const char *media_entity_get_devname(struct media_entity *entity);
  214. /**
  215. * @brief Get the type of an entity.
  216. * @param entity - the entity.
  217. *
  218. * @return The type of @a entity.
  219. */
  220. static inline unsigned int media_entity_type(struct media_entity *entity)
  221. {
  222. return media_entity_get_info(entity)->type & MEDIA_ENT_TYPE_MASK;
  223. }
  224. /**
  225. * @brief Find an entity by its name.
  226. * @param media - media device.
  227. * @param name - entity name.
  228. * @param length - size of @a name.
  229. *
  230. * Search for an entity with a name equal to @a name.
  231. *
  232. * @return A pointer to the entity if found, or NULL otherwise.
  233. */
  234. struct media_entity *media_get_entity_by_name(struct media_device *media,
  235. const char *name, size_t length);
  236. /**
  237. * @brief Find an entity by its ID.
  238. * @param media - media device.
  239. * @param id - entity ID.
  240. *
  241. * This function searches for an entity based on its ID using an exact match or
  242. * next ID method based on the given @a id. If @a id is ORed with
  243. * MEDIA_ENT_ID_FLAG_NEXT, the function will return the entity with the smallest
  244. * ID larger than @a id. Otherwise it will return the entity with an ID equal to
  245. * @a id.
  246. *
  247. * @return A pointer to the entity if found, or NULL otherwise.
  248. */
  249. struct media_entity *media_get_entity_by_id(struct media_device *media,
  250. __u32 id);
  251. /**
  252. * @brief Get the number of entities
  253. * @param media - media device.
  254. *
  255. * This function returns the total number of entities in the media device. If
  256. * entities haven't been enumerated yet it will return 0.
  257. *
  258. * @return The number of entities in the media device
  259. */
  260. unsigned int media_get_entities_count(struct media_device *media);
  261. /**
  262. * @brief Get the entities
  263. * @param media - media device.
  264. *
  265. * This function returns a pointer to the array of entities for the media
  266. * device. If entities haven't been enumerated yet it will return NULL.
  267. *
  268. * The array of entities is owned by the media device object and will be freed
  269. * when the media object is destroyed.
  270. *
  271. * @return A pointer to an array of entities
  272. */
  273. struct media_entity *media_get_entity(struct media_device *media, unsigned int index);
  274. /**
  275. * @brief Get the default entity for a given type
  276. * @param media - media device.
  277. * @param type - entity type.
  278. *
  279. * This function returns the default entity of the requested type. @a type must
  280. * be one of
  281. *
  282. * MEDIA_ENT_T_DEVNODE_V4L
  283. * MEDIA_ENT_T_DEVNODE_FB
  284. * MEDIA_ENT_T_DEVNODE_ALSA
  285. * MEDIA_ENT_T_DEVNODE_DVB
  286. *
  287. * @return A pointer to the default entity for the type if it exists, or NULL
  288. * otherwise.
  289. */
  290. struct media_entity *media_get_default_entity(struct media_device *media,
  291. unsigned int type);
  292. /**
  293. * @brief Get the media device information
  294. * @param media - media device.
  295. *
  296. * The information structure is owned by the media device object and will be freed
  297. * when the media object is destroyed.
  298. *
  299. * @return A pointer to the media device information
  300. */
  301. const struct media_device_info *media_get_info(struct media_device *media);
  302. /**
  303. * @brief Get the media device node name
  304. * @param media - media device.
  305. *
  306. * The device node name string is owned by the media device object and will be
  307. * freed when the media object is destroyed.
  308. *
  309. * @return A pointer to the media device node name
  310. */
  311. const char *media_get_devnode(struct media_device *media);
  312. /**
  313. * @brief Configure a link.
  314. * @param media - media device.
  315. * @param source - source pad at the link origin.
  316. * @param sink - sink pad at the link target.
  317. * @param flags - configuration flags.
  318. *
  319. * Locate the link between @a source and @a sink, and configure it by applying
  320. * the new @a flags.
  321. *
  322. * Only the MEDIA_LINK_FLAG_ENABLED flag is writable.
  323. *
  324. * @return 0 on success, -1 on failure:
  325. * -ENOENT: link not found
  326. * - other error codes returned by MEDIA_IOC_SETUP_LINK
  327. */
  328. int media_setup_link(struct media_device *media,
  329. struct media_pad *source, struct media_pad *sink,
  330. __u32 flags);
  331. /**
  332. * @brief Reset all links to the disabled state.
  333. * @param media - media device.
  334. *
  335. * Disable all links in the media device. This function is usually used after
  336. * opening a media device to reset all links to a known state.
  337. *
  338. * @return 0 on success, or a negative error code on failure.
  339. */
  340. int media_reset_links(struct media_device *media);
  341. /**
  342. * @brief Parse string to a pad on the media device.
  343. * @param media - media device.
  344. * @param p - input string
  345. * @param endp - pointer to string where parsing ended
  346. *
  347. * Parse NULL terminated string describing a pad and return its struct
  348. * media_pad instance.
  349. *
  350. * @return Pointer to struct media_pad on success, NULL on failure.
  351. */
  352. struct media_pad *media_parse_pad(struct media_device *media,
  353. const char *p, char **endp);
  354. /**
  355. * @brief Parse string to a link on the media device.
  356. * @param media - media device.
  357. * @param p - input string
  358. * @param endp - pointer to p where parsing ended
  359. *
  360. * Parse NULL terminated string p describing a link and return its struct
  361. * media_link instance.
  362. *
  363. * @return Pointer to struct media_link on success, NULL on failure.
  364. */
  365. struct media_link *media_parse_link(struct media_device *media,
  366. const char *p, char **endp);
  367. /**
  368. * @brief Parse string to a link on the media device and set it up.
  369. * @param media - media device.
  370. * @param p - input string
  371. *
  372. * Parse NULL terminated string p describing a link and its configuration
  373. * and configure the link.
  374. *
  375. * @return 0 on success, or a negative error code on failure.
  376. */
  377. int media_parse_setup_link(struct media_device *media,
  378. const char *p, char **endp);
  379. /**
  380. * @brief Parse string to link(s) on the media device and set it up.
  381. * @param media - media device.
  382. * @param p - input string
  383. *
  384. * Parse NULL terminated string p describing link(s) separated by
  385. * commas (,) and configure the link(s).
  386. *
  387. * @return 0 on success, or a negative error code on failure.
  388. */
  389. int media_parse_setup_links(struct media_device *media, const char *p);
  390. #endif