media-entity.h 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  1. /*
  2. * Media entity
  3. *
  4. * Copyright (C) 2010 Nokia Corporation
  5. *
  6. * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  7. * Sakari Ailus <sakari.ailus@iki.fi>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  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 General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #ifndef _MEDIA_ENTITY_H
  23. #define _MEDIA_ENTITY_H
  24. #include <linux/bitmap.h>
  25. #include <linux/bug.h>
  26. #include <linux/kernel.h>
  27. #include <linux/list.h>
  28. #include <linux/media.h>
  29. /* Enums used internally at the media controller to represent graphs */
  30. /**
  31. * enum media_gobj_type - type of a graph object
  32. *
  33. * @MEDIA_GRAPH_ENTITY: Identify a media entity
  34. * @MEDIA_GRAPH_PAD: Identify a media pad
  35. * @MEDIA_GRAPH_LINK: Identify a media link
  36. * @MEDIA_GRAPH_INTF_DEVNODE: Identify a media Kernel API interface via
  37. * a device node
  38. */
  39. enum media_gobj_type {
  40. MEDIA_GRAPH_ENTITY,
  41. MEDIA_GRAPH_PAD,
  42. MEDIA_GRAPH_LINK,
  43. MEDIA_GRAPH_INTF_DEVNODE,
  44. };
  45. #define MEDIA_BITS_PER_TYPE 8
  46. #define MEDIA_BITS_PER_ID (32 - MEDIA_BITS_PER_TYPE)
  47. #define MEDIA_ID_MASK GENMASK_ULL(MEDIA_BITS_PER_ID - 1, 0)
  48. /* Structs to represent the objects that belong to a media graph */
  49. /**
  50. * struct media_gobj - Define a graph object.
  51. *
  52. * @mdev: Pointer to the struct &media_device that owns the object
  53. * @id: Non-zero object ID identifier. The ID should be unique
  54. * inside a media_device, as it is composed by
  55. * %MEDIA_BITS_PER_TYPE to store the type plus
  56. * %MEDIA_BITS_PER_ID to store the ID
  57. * @list: List entry stored in one of the per-type mdev object lists
  58. *
  59. * All objects on the media graph should have this struct embedded
  60. */
  61. struct media_gobj {
  62. struct media_device *mdev;
  63. u32 id;
  64. struct list_head list;
  65. };
  66. #define MEDIA_ENTITY_ENUM_MAX_DEPTH 16
  67. /**
  68. * struct media_entity_enum - An enumeration of media entities.
  69. *
  70. * @bmap: Bit map in which each bit represents one entity at struct
  71. * media_entity->internal_idx.
  72. * @idx_max: Number of bits in bmap
  73. */
  74. struct media_entity_enum {
  75. unsigned long *bmap;
  76. int idx_max;
  77. };
  78. /**
  79. * struct media_entity_graph - Media graph traversal state
  80. *
  81. * @stack: Graph traversal stack; the stack contains information
  82. * on the path the media entities to be walked and the
  83. * links through which they were reached.
  84. * @ent_enum: Visited entities
  85. * @top: The top of the stack
  86. */
  87. struct media_entity_graph {
  88. struct {
  89. struct media_entity *entity;
  90. struct list_head *link;
  91. } stack[MEDIA_ENTITY_ENUM_MAX_DEPTH];
  92. struct media_entity_enum ent_enum;
  93. int top;
  94. };
  95. /**
  96. * struct media_pipeline - Media pipeline related information
  97. *
  98. * @streaming_count: Streaming start count - streaming stop count
  99. * @graph: Media graph walk during pipeline start / stop
  100. */
  101. struct media_pipeline {
  102. int streaming_count;
  103. struct media_entity_graph graph;
  104. };
  105. /**
  106. * struct media_link - A link object part of a media graph.
  107. *
  108. * @graph_obj: Embedded structure containing the media object common data
  109. * @list: Linked list associated with an entity or an interface that
  110. * owns the link.
  111. * @gobj0: Part of a union. Used to get the pointer for the first
  112. * graph_object of the link.
  113. * @source: Part of a union. Used only if the first object (gobj0) is
  114. * a pad. In that case, it represents the source pad.
  115. * @intf: Part of a union. Used only if the first object (gobj0) is
  116. * an interface.
  117. * @gobj1: Part of a union. Used to get the pointer for the second
  118. * graph_object of the link.
  119. * @sink: Part of a union. Used only if the second object (gobj1) is
  120. * a pad. In that case, it represents the sink pad.
  121. * @entity: Part of a union. Used only if the second object (gobj1) is
  122. * an entity.
  123. * @reverse: Pointer to the link for the reverse direction of a pad to pad
  124. * link.
  125. * @flags: Link flags, as defined in uapi/media.h (MEDIA_LNK_FL_*)
  126. * @is_backlink: Indicate if the link is a backlink.
  127. */
  128. struct media_link {
  129. struct media_gobj graph_obj;
  130. struct list_head list;
  131. union {
  132. struct media_gobj *gobj0;
  133. struct media_pad *source;
  134. struct media_interface *intf;
  135. };
  136. union {
  137. struct media_gobj *gobj1;
  138. struct media_pad *sink;
  139. struct media_entity *entity;
  140. };
  141. struct media_link *reverse;
  142. unsigned long flags;
  143. bool is_backlink;
  144. };
  145. /**
  146. * struct media_pad - A media pad graph object.
  147. *
  148. * @graph_obj: Embedded structure containing the media object common data
  149. * @entity: Entity this pad belongs to
  150. * @index: Pad index in the entity pads array, numbered from 0 to n
  151. * @flags: Pad flags, as defined in
  152. * :ref:`include/uapi/linux/media.h <media_header>`
  153. * (seek for ``MEDIA_PAD_FL_*``)
  154. */
  155. struct media_pad {
  156. struct media_gobj graph_obj; /* must be first field in struct */
  157. struct media_entity *entity;
  158. u16 index;
  159. unsigned long flags;
  160. };
  161. /**
  162. * struct media_entity_operations - Media entity operations
  163. * @link_setup: Notify the entity of link changes. The operation can
  164. * return an error, in which case link setup will be
  165. * cancelled. Optional.
  166. * @link_validate: Return whether a link is valid from the entity point of
  167. * view. The media_entity_pipeline_start() function
  168. * validates all links by calling this operation. Optional.
  169. *
  170. * .. note::
  171. *
  172. * Those these callbacks are called with struct &media_device.graph_mutex
  173. * mutex held.
  174. */
  175. struct media_entity_operations {
  176. int (*link_setup)(struct media_entity *entity,
  177. const struct media_pad *local,
  178. const struct media_pad *remote, u32 flags);
  179. int (*link_validate)(struct media_link *link);
  180. };
  181. /**
  182. * enum media_entity_type - Media entity type
  183. *
  184. * @MEDIA_ENTITY_TYPE_BASE:
  185. * The entity isn't embedded in another subsystem structure.
  186. * @MEDIA_ENTITY_TYPE_VIDEO_DEVICE:
  187. * The entity is embedded in a struct video_device instance.
  188. * @MEDIA_ENTITY_TYPE_V4L2_SUBDEV:
  189. * The entity is embedded in a struct v4l2_subdev instance.
  190. *
  191. * Media entity objects are often not instantiated directly, but the media
  192. * entity structure is inherited by (through embedding) other subsystem-specific
  193. * structures. The media entity type identifies the type of the subclass
  194. * structure that implements a media entity instance.
  195. *
  196. * This allows runtime type identification of media entities and safe casting to
  197. * the correct object type. For instance, a media entity structure instance
  198. * embedded in a v4l2_subdev structure instance will have the type
  199. * %MEDIA_ENTITY_TYPE_V4L2_SUBDEV and can safely be cast to a &v4l2_subdev
  200. * structure using the container_of() macro.
  201. */
  202. enum media_entity_type {
  203. MEDIA_ENTITY_TYPE_BASE,
  204. MEDIA_ENTITY_TYPE_VIDEO_DEVICE,
  205. MEDIA_ENTITY_TYPE_V4L2_SUBDEV,
  206. };
  207. /**
  208. * struct media_entity - A media entity graph object.
  209. *
  210. * @graph_obj: Embedded structure containing the media object common data.
  211. * @name: Entity name.
  212. * @obj_type: Type of the object that implements the media_entity.
  213. * @function: Entity main function, as defined in
  214. * :ref:`include/uapi/linux/media.h <media_header>`
  215. * (seek for ``MEDIA_ENT_F_*``)
  216. * @flags: Entity flags, as defined in
  217. * :ref:`include/uapi/linux/media.h <media_header>`
  218. * (seek for ``MEDIA_ENT_FL_*``)
  219. * @num_pads: Number of sink and source pads.
  220. * @num_links: Total number of links, forward and back, enabled and disabled.
  221. * @num_backlinks: Number of backlinks
  222. * @internal_idx: An unique internal entity specific number. The numbers are
  223. * re-used if entities are unregistered or registered again.
  224. * @pads: Pads array with the size defined by @num_pads.
  225. * @links: List of data links.
  226. * @ops: Entity operations.
  227. * @stream_count: Stream count for the entity.
  228. * @use_count: Use count for the entity.
  229. * @pipe: Pipeline this entity belongs to.
  230. * @info: Union with devnode information. Kept just for backward
  231. * compatibility.
  232. * @major: Devnode major number (zero if not applicable). Kept just
  233. * for backward compatibility.
  234. * @minor: Devnode minor number (zero if not applicable). Kept just
  235. * for backward compatibility.
  236. *
  237. * .. note::
  238. *
  239. * @stream_count and @use_count reference counts must never be
  240. * negative, but are signed integers on purpose: a simple ``WARN_ON(<0)``
  241. * check can be used to detect reference count bugs that would make them
  242. * negative.
  243. */
  244. struct media_entity {
  245. struct media_gobj graph_obj; /* must be first field in struct */
  246. const char *name;
  247. enum media_entity_type obj_type;
  248. u32 function;
  249. unsigned long flags;
  250. u16 num_pads;
  251. u16 num_links;
  252. u16 num_backlinks;
  253. int internal_idx;
  254. struct media_pad *pads;
  255. struct list_head links;
  256. const struct media_entity_operations *ops;
  257. int stream_count;
  258. int use_count;
  259. struct media_pipeline *pipe;
  260. union {
  261. struct {
  262. u32 major;
  263. u32 minor;
  264. } dev;
  265. } info;
  266. };
  267. /**
  268. * struct media_interface - A media interface graph object.
  269. *
  270. * @graph_obj: embedded graph object
  271. * @links: List of links pointing to graph entities
  272. * @type: Type of the interface as defined in
  273. * :ref:`include/uapi/linux/media.h <media_header>`
  274. * (seek for ``MEDIA_INTF_T_*``)
  275. * @flags: Interface flags as defined in
  276. * :ref:`include/uapi/linux/media.h <media_header>`
  277. * (seek for ``MEDIA_INTF_FL_*``)
  278. *
  279. * .. note::
  280. *
  281. * Currently, no flags for &media_interface is defined.
  282. */
  283. struct media_interface {
  284. struct media_gobj graph_obj;
  285. struct list_head links;
  286. u32 type;
  287. u32 flags;
  288. };
  289. /**
  290. * struct media_intf_devnode - A media interface via a device node.
  291. *
  292. * @intf: embedded interface object
  293. * @major: Major number of a device node
  294. * @minor: Minor number of a device node
  295. */
  296. struct media_intf_devnode {
  297. struct media_interface intf;
  298. /* Should match the fields at media_v2_intf_devnode */
  299. u32 major;
  300. u32 minor;
  301. };
  302. /**
  303. * media_entity_id() - return the media entity graph object id
  304. *
  305. * @entity: pointer to &media_entity
  306. */
  307. static inline u32 media_entity_id(struct media_entity *entity)
  308. {
  309. return entity->graph_obj.id;
  310. }
  311. /**
  312. * media_type() - return the media object type
  313. *
  314. * @gobj: Pointer to the struct &media_gobj graph object
  315. */
  316. static inline enum media_gobj_type media_type(struct media_gobj *gobj)
  317. {
  318. return gobj->id >> MEDIA_BITS_PER_ID;
  319. }
  320. /**
  321. * media_id() - return the media object ID
  322. *
  323. * @gobj: Pointer to the struct &media_gobj graph object
  324. */
  325. static inline u32 media_id(struct media_gobj *gobj)
  326. {
  327. return gobj->id & MEDIA_ID_MASK;
  328. }
  329. /**
  330. * media_gobj_gen_id() - encapsulates type and ID on at the object ID
  331. *
  332. * @type: object type as define at enum &media_gobj_type.
  333. * @local_id: next ID, from struct &media_device.id.
  334. */
  335. static inline u32 media_gobj_gen_id(enum media_gobj_type type, u64 local_id)
  336. {
  337. u32 id;
  338. id = type << MEDIA_BITS_PER_ID;
  339. id |= local_id & MEDIA_ID_MASK;
  340. return id;
  341. }
  342. /**
  343. * is_media_entity_v4l2_video_device() - Check if the entity is a video_device
  344. * @entity: pointer to entity
  345. *
  346. * Return: %true if the entity is an instance of a video_device object and can
  347. * safely be cast to a struct video_device using the container_of() macro, or
  348. * %false otherwise.
  349. */
  350. static inline bool is_media_entity_v4l2_video_device(struct media_entity *entity)
  351. {
  352. return entity && entity->obj_type == MEDIA_ENTITY_TYPE_VIDEO_DEVICE;
  353. }
  354. /**
  355. * is_media_entity_v4l2_subdev() - Check if the entity is a v4l2_subdev
  356. * @entity: pointer to entity
  357. *
  358. * Return: %true if the entity is an instance of a &v4l2_subdev object and can
  359. * safely be cast to a struct &v4l2_subdev using the container_of() macro, or
  360. * %false otherwise.
  361. */
  362. static inline bool is_media_entity_v4l2_subdev(struct media_entity *entity)
  363. {
  364. return entity && entity->obj_type == MEDIA_ENTITY_TYPE_V4L2_SUBDEV;
  365. }
  366. /**
  367. * __media_entity_enum_init - Initialise an entity enumeration
  368. *
  369. * @ent_enum: Entity enumeration to be initialised
  370. * @idx_max: Maximum number of entities in the enumeration
  371. *
  372. * Return: Returns zero on success or a negative error code.
  373. */
  374. __must_check int __media_entity_enum_init(struct media_entity_enum *ent_enum,
  375. int idx_max);
  376. /**
  377. * media_entity_enum_cleanup - Release resources of an entity enumeration
  378. *
  379. * @ent_enum: Entity enumeration to be released
  380. */
  381. void media_entity_enum_cleanup(struct media_entity_enum *ent_enum);
  382. /**
  383. * media_entity_enum_zero - Clear the entire enum
  384. *
  385. * @ent_enum: Entity enumeration to be cleared
  386. */
  387. static inline void media_entity_enum_zero(struct media_entity_enum *ent_enum)
  388. {
  389. bitmap_zero(ent_enum->bmap, ent_enum->idx_max);
  390. }
  391. /**
  392. * media_entity_enum_set - Mark a single entity in the enum
  393. *
  394. * @ent_enum: Entity enumeration
  395. * @entity: Entity to be marked
  396. */
  397. static inline void media_entity_enum_set(struct media_entity_enum *ent_enum,
  398. struct media_entity *entity)
  399. {
  400. if (WARN_ON(entity->internal_idx >= ent_enum->idx_max))
  401. return;
  402. __set_bit(entity->internal_idx, ent_enum->bmap);
  403. }
  404. /**
  405. * media_entity_enum_clear - Unmark a single entity in the enum
  406. *
  407. * @ent_enum: Entity enumeration
  408. * @entity: Entity to be unmarked
  409. */
  410. static inline void media_entity_enum_clear(struct media_entity_enum *ent_enum,
  411. struct media_entity *entity)
  412. {
  413. if (WARN_ON(entity->internal_idx >= ent_enum->idx_max))
  414. return;
  415. __clear_bit(entity->internal_idx, ent_enum->bmap);
  416. }
  417. /**
  418. * media_entity_enum_test - Test whether the entity is marked
  419. *
  420. * @ent_enum: Entity enumeration
  421. * @entity: Entity to be tested
  422. *
  423. * Returns %true if the entity was marked.
  424. */
  425. static inline bool media_entity_enum_test(struct media_entity_enum *ent_enum,
  426. struct media_entity *entity)
  427. {
  428. if (WARN_ON(entity->internal_idx >= ent_enum->idx_max))
  429. return true;
  430. return test_bit(entity->internal_idx, ent_enum->bmap);
  431. }
  432. /**
  433. * media_entity_enum_test_and_set - Test whether the entity is marked,
  434. * and mark it
  435. *
  436. * @ent_enum: Entity enumeration
  437. * @entity: Entity to be tested
  438. *
  439. * Returns %true if the entity was marked, and mark it before doing so.
  440. */
  441. static inline bool
  442. media_entity_enum_test_and_set(struct media_entity_enum *ent_enum,
  443. struct media_entity *entity)
  444. {
  445. if (WARN_ON(entity->internal_idx >= ent_enum->idx_max))
  446. return true;
  447. return __test_and_set_bit(entity->internal_idx, ent_enum->bmap);
  448. }
  449. /**
  450. * media_entity_enum_empty - Test whether the entire enum is empty
  451. *
  452. * @ent_enum: Entity enumeration
  453. *
  454. * Return: %true if the entity was empty.
  455. */
  456. static inline bool media_entity_enum_empty(struct media_entity_enum *ent_enum)
  457. {
  458. return bitmap_empty(ent_enum->bmap, ent_enum->idx_max);
  459. }
  460. /**
  461. * media_entity_enum_intersects - Test whether two enums intersect
  462. *
  463. * @ent_enum1: First entity enumeration
  464. * @ent_enum2: Second entity enumeration
  465. *
  466. * Return: %true if entity enumerations @ent_enum1 and @ent_enum2 intersect,
  467. * otherwise %false.
  468. */
  469. static inline bool media_entity_enum_intersects(
  470. struct media_entity_enum *ent_enum1,
  471. struct media_entity_enum *ent_enum2)
  472. {
  473. WARN_ON(ent_enum1->idx_max != ent_enum2->idx_max);
  474. return bitmap_intersects(ent_enum1->bmap, ent_enum2->bmap,
  475. min(ent_enum1->idx_max, ent_enum2->idx_max));
  476. }
  477. /**
  478. * gobj_to_entity - returns the struct &media_entity pointer from the
  479. * @gobj contained on it.
  480. *
  481. * @gobj: Pointer to the struct &media_gobj graph object
  482. */
  483. #define gobj_to_entity(gobj) \
  484. container_of(gobj, struct media_entity, graph_obj)
  485. /**
  486. * gobj_to_pad - returns the struct &media_pad pointer from the
  487. * @gobj contained on it.
  488. *
  489. * @gobj: Pointer to the struct &media_gobj graph object
  490. */
  491. #define gobj_to_pad(gobj) \
  492. container_of(gobj, struct media_pad, graph_obj)
  493. /**
  494. * gobj_to_link - returns the struct &media_link pointer from the
  495. * @gobj contained on it.
  496. *
  497. * @gobj: Pointer to the struct &media_gobj graph object
  498. */
  499. #define gobj_to_link(gobj) \
  500. container_of(gobj, struct media_link, graph_obj)
  501. /**
  502. * gobj_to_intf - returns the struct &media_interface pointer from the
  503. * @gobj contained on it.
  504. *
  505. * @gobj: Pointer to the struct &media_gobj graph object
  506. */
  507. #define gobj_to_intf(gobj) \
  508. container_of(gobj, struct media_interface, graph_obj)
  509. /**
  510. * intf_to_devnode - returns the struct media_intf_devnode pointer from the
  511. * @intf contained on it.
  512. *
  513. * @intf: Pointer to struct &media_intf_devnode
  514. */
  515. #define intf_to_devnode(intf) \
  516. container_of(intf, struct media_intf_devnode, intf)
  517. /**
  518. * media_gobj_create - Initialize a graph object
  519. *
  520. * @mdev: Pointer to the &media_device that contains the object
  521. * @type: Type of the object
  522. * @gobj: Pointer to the struct &media_gobj graph object
  523. *
  524. * This routine initializes the embedded struct &media_gobj inside a
  525. * media graph object. It is called automatically if ``media_*_create``
  526. * function calls are used. However, if the object (entity, link, pad,
  527. * interface) is embedded on some other object, this function should be
  528. * called before registering the object at the media controller.
  529. */
  530. void media_gobj_create(struct media_device *mdev,
  531. enum media_gobj_type type,
  532. struct media_gobj *gobj);
  533. /**
  534. * media_gobj_destroy - Stop using a graph object on a media device
  535. *
  536. * @gobj: Pointer to the struct &media_gobj graph object
  537. *
  538. * This should be called by all routines like media_device_unregister()
  539. * that remove/destroy media graph objects.
  540. */
  541. void media_gobj_destroy(struct media_gobj *gobj);
  542. /**
  543. * media_entity_pads_init() - Initialize the entity pads
  544. *
  545. * @entity: entity where the pads belong
  546. * @num_pads: total number of sink and source pads
  547. * @pads: Array of @num_pads pads.
  548. *
  549. * The pads array is managed by the entity driver and passed to
  550. * media_entity_pads_init() where its pointer will be stored in the
  551. * &media_entity structure.
  552. *
  553. * If no pads are needed, drivers could either directly fill
  554. * &media_entity->num_pads with 0 and &media_entity->pads with %NULL or call
  555. * this function that will do the same.
  556. *
  557. * As the number of pads is known in advance, the pads array is not allocated
  558. * dynamically but is managed by the entity driver. Most drivers will embed the
  559. * pads array in a driver-specific structure, avoiding dynamic allocation.
  560. *
  561. * Drivers must set the direction of every pad in the pads array before calling
  562. * media_entity_pads_init(). The function will initialize the other pads fields.
  563. */
  564. int media_entity_pads_init(struct media_entity *entity, u16 num_pads,
  565. struct media_pad *pads);
  566. /**
  567. * media_entity_cleanup() - free resources associated with an entity
  568. *
  569. * @entity: entity where the pads belong
  570. *
  571. * This function must be called during the cleanup phase after unregistering
  572. * the entity (currently, it does nothing).
  573. */
  574. static inline void media_entity_cleanup(struct media_entity *entity) {};
  575. /**
  576. * media_create_pad_link() - creates a link between two entities.
  577. *
  578. * @source: pointer to &media_entity of the source pad.
  579. * @source_pad: number of the source pad in the pads array
  580. * @sink: pointer to &media_entity of the sink pad.
  581. * @sink_pad: number of the sink pad in the pads array.
  582. * @flags: Link flags, as defined in
  583. * :ref:`include/uapi/linux/media.h <media_header>`
  584. * ( seek for ``MEDIA_LNK_FL_*``)
  585. *
  586. * Valid values for flags:
  587. *
  588. * %MEDIA_LNK_FL_ENABLED
  589. * Indicates that the link is enabled and can be used to transfer media data.
  590. * When two or more links target a sink pad, only one of them can be
  591. * enabled at a time.
  592. *
  593. * %MEDIA_LNK_FL_IMMUTABLE
  594. * Indicates that the link enabled state can't be modified at runtime. If
  595. * %MEDIA_LNK_FL_IMMUTABLE is set, then %MEDIA_LNK_FL_ENABLED must also be
  596. * set, since an immutable link is always enabled.
  597. *
  598. * .. note::
  599. *
  600. * Before calling this function, media_entity_pads_init() and
  601. * media_device_register_entity() should be called previously for both ends.
  602. */
  603. __must_check int media_create_pad_link(struct media_entity *source,
  604. u16 source_pad, struct media_entity *sink,
  605. u16 sink_pad, u32 flags);
  606. /**
  607. * media_create_pad_links() - creates a link between two entities.
  608. *
  609. * @mdev: Pointer to the media_device that contains the object
  610. * @source_function: Function of the source entities. Used only if @source is
  611. * NULL.
  612. * @source: pointer to &media_entity of the source pad. If NULL, it will use
  613. * all entities that matches the @sink_function.
  614. * @source_pad: number of the source pad in the pads array
  615. * @sink_function: Function of the sink entities. Used only if @sink is NULL.
  616. * @sink: pointer to &media_entity of the sink pad. If NULL, it will use
  617. * all entities that matches the @sink_function.
  618. * @sink_pad: number of the sink pad in the pads array.
  619. * @flags: Link flags, as defined in include/uapi/linux/media.h.
  620. * @allow_both_undefined: if %true, then both @source and @sink can be NULL.
  621. * In such case, it will create a crossbar between all entities that
  622. * matches @source_function to all entities that matches @sink_function.
  623. * If %false, it will return 0 and won't create any link if both @source
  624. * and @sink are NULL.
  625. *
  626. * Valid values for flags:
  627. *
  628. * A %MEDIA_LNK_FL_ENABLED flag indicates that the link is enabled and can be
  629. * used to transfer media data. If multiple links are created and this
  630. * flag is passed as an argument, only the first created link will have
  631. * this flag.
  632. *
  633. * A %MEDIA_LNK_FL_IMMUTABLE flag indicates that the link enabled state can't
  634. * be modified at runtime. If %MEDIA_LNK_FL_IMMUTABLE is set, then
  635. * %MEDIA_LNK_FL_ENABLED must also be set since an immutable link is
  636. * always enabled.
  637. *
  638. * It is common for some devices to have multiple source and/or sink entities
  639. * of the same type that should be linked. While media_create_pad_link()
  640. * creates link by link, this function is meant to allow 1:n, n:1 and even
  641. * cross-bar (n:n) links.
  642. *
  643. * .. note::
  644. *
  645. * Before calling this function, media_entity_pads_init() and
  646. * media_device_register_entity() should be called previously for the
  647. * entities to be linked.
  648. */
  649. int media_create_pad_links(const struct media_device *mdev,
  650. const u32 source_function,
  651. struct media_entity *source,
  652. const u16 source_pad,
  653. const u32 sink_function,
  654. struct media_entity *sink,
  655. const u16 sink_pad,
  656. u32 flags,
  657. const bool allow_both_undefined);
  658. void __media_entity_remove_links(struct media_entity *entity);
  659. /**
  660. * media_entity_remove_links() - remove all links associated with an entity
  661. *
  662. * @entity: pointer to &media_entity
  663. *
  664. * .. note::
  665. *
  666. * This is called automatically when an entity is unregistered via
  667. * media_device_register_entity().
  668. */
  669. void media_entity_remove_links(struct media_entity *entity);
  670. /**
  671. * __media_entity_setup_link - Configure a media link without locking
  672. * @link: The link being configured
  673. * @flags: Link configuration flags
  674. *
  675. * The bulk of link setup is handled by the two entities connected through the
  676. * link. This function notifies both entities of the link configuration change.
  677. *
  678. * If the link is immutable or if the current and new configuration are
  679. * identical, return immediately.
  680. *
  681. * The user is expected to hold link->source->parent->mutex. If not,
  682. * media_entity_setup_link() should be used instead.
  683. */
  684. int __media_entity_setup_link(struct media_link *link, u32 flags);
  685. /**
  686. * media_entity_setup_link() - changes the link flags properties in runtime
  687. *
  688. * @link: pointer to &media_link
  689. * @flags: the requested new link flags
  690. *
  691. * The only configurable property is the %MEDIA_LNK_FL_ENABLED link flag
  692. * flag to enable/disable a link. Links marked with the
  693. * %MEDIA_LNK_FL_IMMUTABLE link flag can not be enabled or disabled.
  694. *
  695. * When a link is enabled or disabled, the media framework calls the
  696. * link_setup operation for the two entities at the source and sink of the
  697. * link, in that order. If the second link_setup call fails, another
  698. * link_setup call is made on the first entity to restore the original link
  699. * flags.
  700. *
  701. * Media device drivers can be notified of link setup operations by setting the
  702. * &media_device.link_notify pointer to a callback function. If provided, the
  703. * notification callback will be called before enabling and after disabling
  704. * links.
  705. *
  706. * Entity drivers must implement the link_setup operation if any of their links
  707. * is non-immutable. The operation must either configure the hardware or store
  708. * the configuration information to be applied later.
  709. *
  710. * Link configuration must not have any side effect on other links. If an
  711. * enabled link at a sink pad prevents another link at the same pad from
  712. * being enabled, the link_setup operation must return %-EBUSY and can't
  713. * implicitly disable the first enabled link.
  714. *
  715. * .. note::
  716. *
  717. * The valid values of the flags for the link is the same as described
  718. * on media_create_pad_link(), for pad to pad links or the same as described
  719. * on media_create_intf_link(), for interface to entity links.
  720. */
  721. int media_entity_setup_link(struct media_link *link, u32 flags);
  722. /**
  723. * media_entity_find_link - Find a link between two pads
  724. * @source: Source pad
  725. * @sink: Sink pad
  726. *
  727. * Return: returns a pointer to the link between the two entities. If no
  728. * such link exists, return %NULL.
  729. */
  730. struct media_link *media_entity_find_link(struct media_pad *source,
  731. struct media_pad *sink);
  732. /**
  733. * media_entity_remote_pad - Find the pad at the remote end of a link
  734. * @pad: Pad at the local end of the link
  735. *
  736. * Search for a remote pad connected to the given pad by iterating over all
  737. * links originating or terminating at that pad until an enabled link is found.
  738. *
  739. * Return: returns a pointer to the pad at the remote end of the first found
  740. * enabled link, or %NULL if no enabled link has been found.
  741. */
  742. struct media_pad *media_entity_remote_pad(struct media_pad *pad);
  743. /**
  744. * media_entity_get - Get a reference to the parent module
  745. *
  746. * @entity: The entity
  747. *
  748. * Get a reference to the parent media device module.
  749. *
  750. * The function will return immediately if @entity is %NULL.
  751. *
  752. * Return: returns a pointer to the entity on success or %NULL on failure.
  753. */
  754. struct media_entity *media_entity_get(struct media_entity *entity);
  755. /**
  756. * media_entity_graph_walk_init - Allocate resources used by graph walk.
  757. *
  758. * @graph: Media graph structure that will be used to walk the graph
  759. * @mdev: Pointer to the &media_device that contains the object
  760. */
  761. __must_check int media_entity_graph_walk_init(
  762. struct media_entity_graph *graph, struct media_device *mdev);
  763. /**
  764. * media_entity_graph_walk_cleanup - Release resources used by graph walk.
  765. *
  766. * @graph: Media graph structure that will be used to walk the graph
  767. */
  768. void media_entity_graph_walk_cleanup(struct media_entity_graph *graph);
  769. /**
  770. * media_entity_put - Release the reference to the parent module
  771. *
  772. * @entity: The entity
  773. *
  774. * Release the reference count acquired by media_entity_get().
  775. *
  776. * The function will return immediately if @entity is %NULL.
  777. */
  778. void media_entity_put(struct media_entity *entity);
  779. /**
  780. * media_entity_graph_walk_start - Start walking the media graph at a
  781. * given entity
  782. *
  783. * @graph: Media graph structure that will be used to walk the graph
  784. * @entity: Starting entity
  785. *
  786. * Before using this function, media_entity_graph_walk_init() must be
  787. * used to allocate resources used for walking the graph. This
  788. * function initializes the graph traversal structure to walk the
  789. * entities graph starting at the given entity. The traversal
  790. * structure must not be modified by the caller during graph
  791. * traversal. After the graph walk, the resources must be released
  792. * using media_entity_graph_walk_cleanup().
  793. */
  794. void media_entity_graph_walk_start(struct media_entity_graph *graph,
  795. struct media_entity *entity);
  796. /**
  797. * media_entity_graph_walk_next - Get the next entity in the graph
  798. * @graph: Media graph structure
  799. *
  800. * Perform a depth-first traversal of the given media entities graph.
  801. *
  802. * The graph structure must have been previously initialized with a call to
  803. * media_entity_graph_walk_start().
  804. *
  805. * Return: returns the next entity in the graph or %NULL if the whole graph
  806. * have been traversed.
  807. */
  808. struct media_entity *
  809. media_entity_graph_walk_next(struct media_entity_graph *graph);
  810. /**
  811. * media_entity_pipeline_start - Mark a pipeline as streaming
  812. * @entity: Starting entity
  813. * @pipe: Media pipeline to be assigned to all entities in the pipeline.
  814. *
  815. * Mark all entities connected to a given entity through enabled links, either
  816. * directly or indirectly, as streaming. The given pipeline object is assigned
  817. * to every entity in the pipeline and stored in the media_entity pipe field.
  818. *
  819. * Calls to this function can be nested, in which case the same number of
  820. * media_entity_pipeline_stop() calls will be required to stop streaming. The
  821. * pipeline pointer must be identical for all nested calls to
  822. * media_entity_pipeline_start().
  823. */
  824. __must_check int media_entity_pipeline_start(struct media_entity *entity,
  825. struct media_pipeline *pipe);
  826. /**
  827. * __media_entity_pipeline_start - Mark a pipeline as streaming
  828. *
  829. * @entity: Starting entity
  830. * @pipe: Media pipeline to be assigned to all entities in the pipeline.
  831. *
  832. * ..note:: This is the non-locking version of media_entity_pipeline_start()
  833. */
  834. __must_check int __media_entity_pipeline_start(struct media_entity *entity,
  835. struct media_pipeline *pipe);
  836. /**
  837. * media_entity_pipeline_stop - Mark a pipeline as not streaming
  838. * @entity: Starting entity
  839. *
  840. * Mark all entities connected to a given entity through enabled links, either
  841. * directly or indirectly, as not streaming. The media_entity pipe field is
  842. * reset to %NULL.
  843. *
  844. * If multiple calls to media_entity_pipeline_start() have been made, the same
  845. * number of calls to this function are required to mark the pipeline as not
  846. * streaming.
  847. */
  848. void media_entity_pipeline_stop(struct media_entity *entity);
  849. /**
  850. * __media_entity_pipeline_stop - Mark a pipeline as not streaming
  851. *
  852. * @entity: Starting entity
  853. *
  854. * .. note:: This is the non-locking version of media_entity_pipeline_stop()
  855. */
  856. void __media_entity_pipeline_stop(struct media_entity *entity);
  857. /**
  858. * media_devnode_create() - creates and initializes a device node interface
  859. *
  860. * @mdev: pointer to struct &media_device
  861. * @type: type of the interface, as given by
  862. * :ref:`include/uapi/linux/media.h <media_header>`
  863. * ( seek for ``MEDIA_INTF_T_*``) macros.
  864. * @flags: Interface flags, as defined in
  865. * :ref:`include/uapi/linux/media.h <media_header>`
  866. * ( seek for ``MEDIA_INTF_FL_*``)
  867. * @major: Device node major number.
  868. * @minor: Device node minor number.
  869. *
  870. * Return: if succeeded, returns a pointer to the newly allocated
  871. * &media_intf_devnode pointer.
  872. *
  873. * .. note::
  874. *
  875. * Currently, no flags for &media_interface is defined.
  876. */
  877. struct media_intf_devnode *
  878. __must_check media_devnode_create(struct media_device *mdev,
  879. u32 type, u32 flags,
  880. u32 major, u32 minor);
  881. /**
  882. * media_devnode_remove() - removes a device node interface
  883. *
  884. * @devnode: pointer to &media_intf_devnode to be freed.
  885. *
  886. * When a device node interface is removed, all links to it are automatically
  887. * removed.
  888. */
  889. void media_devnode_remove(struct media_intf_devnode *devnode);
  890. struct media_link *
  891. /**
  892. * media_create_intf_link() - creates a link between an entity and an interface
  893. *
  894. * @entity: pointer to %media_entity
  895. * @intf: pointer to %media_interface
  896. * @flags: Link flags, as defined in
  897. * :ref:`include/uapi/linux/media.h <media_header>`
  898. * ( seek for ``MEDIA_LNK_FL_*``)
  899. *
  900. *
  901. * Valid values for flags:
  902. *
  903. * %MEDIA_LNK_FL_ENABLED
  904. * Indicates that the interface is connected to the entity hardware.
  905. * That's the default value for interfaces. An interface may be disabled if
  906. * the hardware is busy due to the usage of some other interface that it is
  907. * currently controlling the hardware.
  908. *
  909. * A typical example is an hybrid TV device that handle only one type of
  910. * stream on a given time. So, when the digital TV is streaming,
  911. * the V4L2 interfaces won't be enabled, as such device is not able to
  912. * also stream analog TV or radio.
  913. *
  914. * .. note::
  915. *
  916. * Before calling this function, media_devnode_create() should be called for
  917. * the interface and media_device_register_entity() should be called for the
  918. * interface that will be part of the link.
  919. */
  920. __must_check media_create_intf_link(struct media_entity *entity,
  921. struct media_interface *intf,
  922. u32 flags);
  923. /**
  924. * __media_remove_intf_link() - remove a single interface link
  925. *
  926. * @link: pointer to &media_link.
  927. *
  928. * .. note:: This is an unlocked version of media_remove_intf_link()
  929. */
  930. void __media_remove_intf_link(struct media_link *link);
  931. /**
  932. * media_remove_intf_link() - remove a single interface link
  933. *
  934. * @link: pointer to &media_link.
  935. *
  936. * .. note:: Prefer to use this one, instead of __media_remove_intf_link()
  937. */
  938. void media_remove_intf_link(struct media_link *link);
  939. /**
  940. * __media_remove_intf_links() - remove all links associated with an interface
  941. *
  942. * @intf: pointer to &media_interface
  943. *
  944. * .. note:: This is an unlocked version of media_remove_intf_links().
  945. */
  946. void __media_remove_intf_links(struct media_interface *intf);
  947. /**
  948. * media_remove_intf_links() - remove all links associated with an interface
  949. *
  950. * @intf: pointer to &media_interface
  951. *
  952. * .. note::
  953. *
  954. * #) This is called automatically when an entity is unregistered via
  955. * media_device_register_entity() and by media_devnode_remove().
  956. *
  957. * #) Prefer to use this one, instead of __media_remove_intf_links().
  958. */
  959. void media_remove_intf_links(struct media_interface *intf);
  960. /**
  961. * media_entity_call - Calls a struct media_entity_operations operation on
  962. * an entity
  963. *
  964. * @entity: entity where the @operation will be called
  965. * @operation: type of the operation. Should be the name of a member of
  966. * struct &media_entity_operations.
  967. *
  968. * This helper function will check if @operation is not %NULL. On such case,
  969. * it will issue a call to @operation\(@entity, @args\).
  970. */
  971. #define media_entity_call(entity, operation, args...) \
  972. (((entity)->ops && (entity)->ops->operation) ? \
  973. (entity)->ops->operation((entity) , ##args) : -ENOIOCTLCMD)
  974. #endif