media-entity.h 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Media entity
  4. *
  5. * Copyright (C) 2010 Nokia Corporation
  6. *
  7. * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  8. * Sakari Ailus <sakari.ailus@iki.fi>
  9. */
  10. #ifndef _MEDIA_ENTITY_H
  11. #define _MEDIA_ENTITY_H
  12. #include <linux/bitmap.h>
  13. #include <linux/bug.h>
  14. #include <linux/container_of.h>
  15. #include <linux/fwnode.h>
  16. #include <linux/list.h>
  17. #include <linux/media.h>
  18. #include <linux/minmax.h>
  19. #include <linux/types.h>
  20. /* Enums used internally at the media controller to represent graphs */
  21. /**
  22. * enum media_gobj_type - type of a graph object
  23. *
  24. * @MEDIA_GRAPH_ENTITY: Identify a media entity
  25. * @MEDIA_GRAPH_PAD: Identify a media pad
  26. * @MEDIA_GRAPH_LINK: Identify a media link
  27. * @MEDIA_GRAPH_INTF_DEVNODE: Identify a media Kernel API interface via
  28. * a device node
  29. */
  30. enum media_gobj_type {
  31. MEDIA_GRAPH_ENTITY,
  32. MEDIA_GRAPH_PAD,
  33. MEDIA_GRAPH_LINK,
  34. MEDIA_GRAPH_INTF_DEVNODE,
  35. };
  36. #define MEDIA_BITS_PER_TYPE 8
  37. #define MEDIA_BITS_PER_ID (32 - MEDIA_BITS_PER_TYPE)
  38. #define MEDIA_ID_MASK GENMASK_ULL(MEDIA_BITS_PER_ID - 1, 0)
  39. /* Structs to represent the objects that belong to a media graph */
  40. /**
  41. * struct media_gobj - Define a graph object.
  42. *
  43. * @mdev: Pointer to the struct &media_device that owns the object
  44. * @id: Non-zero object ID identifier. The ID should be unique
  45. * inside a media_device, as it is composed by
  46. * %MEDIA_BITS_PER_TYPE to store the type plus
  47. * %MEDIA_BITS_PER_ID to store the ID
  48. * @list: List entry stored in one of the per-type mdev object lists
  49. *
  50. * All objects on the media graph should have this struct embedded
  51. */
  52. struct media_gobj {
  53. struct media_device *mdev;
  54. u32 id;
  55. struct list_head list;
  56. };
  57. #define MEDIA_ENTITY_ENUM_MAX_DEPTH 16
  58. /**
  59. * struct media_entity_enum - An enumeration of media entities.
  60. *
  61. * @bmap: Bit map in which each bit represents one entity at struct
  62. * media_entity->internal_idx.
  63. * @idx_max: Number of bits in bmap
  64. */
  65. struct media_entity_enum {
  66. unsigned long *bmap;
  67. int idx_max;
  68. };
  69. /**
  70. * struct media_graph - Media graph traversal state
  71. *
  72. * @stack: Graph traversal stack; the stack contains information
  73. * on the path the media entities to be walked and the
  74. * links through which they were reached.
  75. * @stack.entity: pointer to &struct media_entity at the graph.
  76. * @stack.link: pointer to &struct list_head.
  77. * @ent_enum: Visited entities
  78. * @top: The top of the stack
  79. */
  80. struct media_graph {
  81. struct {
  82. struct media_entity *entity;
  83. struct list_head *link;
  84. } stack[MEDIA_ENTITY_ENUM_MAX_DEPTH];
  85. struct media_entity_enum ent_enum;
  86. int top;
  87. };
  88. /**
  89. * struct media_pipeline - Media pipeline related information
  90. *
  91. * @allocated: Media pipeline allocated and freed by the framework
  92. * @mdev: The media device the pipeline is part of
  93. * @pads: List of media_pipeline_pad
  94. * @start_count: Media pipeline start - stop count
  95. */
  96. struct media_pipeline {
  97. bool allocated;
  98. struct media_device *mdev;
  99. struct list_head pads;
  100. int start_count;
  101. };
  102. /**
  103. * struct media_pipeline_pad - A pad part of a media pipeline
  104. *
  105. * @list: Entry in the media_pad pads list
  106. * @pipe: The media_pipeline that the pad is part of
  107. * @pad: The media pad
  108. *
  109. * This structure associate a pad with a media pipeline. Instances of
  110. * media_pipeline_pad are created by media_pipeline_start() when it builds the
  111. * pipeline, and stored in the &media_pad.pads list. media_pipeline_stop()
  112. * removes the entries from the list and deletes them.
  113. */
  114. struct media_pipeline_pad {
  115. struct list_head list;
  116. struct media_pipeline *pipe;
  117. struct media_pad *pad;
  118. };
  119. /**
  120. * struct media_pipeline_pad_iter - Iterator for media_pipeline_for_each_pad
  121. *
  122. * @cursor: The current element
  123. */
  124. struct media_pipeline_pad_iter {
  125. struct list_head *cursor;
  126. };
  127. /**
  128. * struct media_pipeline_entity_iter - Iterator for media_pipeline_for_each_entity
  129. *
  130. * @ent_enum: The entity enumeration tracker
  131. * @cursor: The current element
  132. */
  133. struct media_pipeline_entity_iter {
  134. struct media_entity_enum ent_enum;
  135. struct list_head *cursor;
  136. };
  137. /**
  138. * struct media_link - A link object part of a media graph.
  139. *
  140. * @graph_obj: Embedded structure containing the media object common data
  141. * @list: Linked list associated with an entity or an interface that
  142. * owns the link.
  143. * @gobj0: Part of a union. Used to get the pointer for the first
  144. * graph_object of the link.
  145. * @source: Part of a union. Used only if the first object (gobj0) is
  146. * a pad. In that case, it represents the source pad.
  147. * @intf: Part of a union. Used only if the first object (gobj0) is
  148. * an interface.
  149. * @gobj1: Part of a union. Used to get the pointer for the second
  150. * graph_object of the link.
  151. * @sink: Part of a union. Used only if the second object (gobj1) is
  152. * a pad. In that case, it represents the sink pad.
  153. * @entity: Part of a union. Used only if the second object (gobj1) is
  154. * an entity.
  155. * @reverse: Pointer to the link for the reverse direction of a pad to pad
  156. * link.
  157. * @flags: Link flags, as defined in uapi/media.h (MEDIA_LNK_FL_*)
  158. * @is_backlink: Indicate if the link is a backlink.
  159. */
  160. struct media_link {
  161. struct media_gobj graph_obj;
  162. struct list_head list;
  163. union {
  164. struct media_gobj *gobj0;
  165. struct media_pad *source;
  166. struct media_interface *intf;
  167. };
  168. union {
  169. struct media_gobj *gobj1;
  170. struct media_pad *sink;
  171. struct media_entity *entity;
  172. };
  173. struct media_link *reverse;
  174. unsigned long flags;
  175. bool is_backlink;
  176. };
  177. /**
  178. * enum media_pad_signal_type - type of the signal inside a media pad
  179. *
  180. * @PAD_SIGNAL_DEFAULT:
  181. * Default signal. Use this when all inputs or all outputs are
  182. * uniquely identified by the pad number.
  183. * @PAD_SIGNAL_ANALOG:
  184. * The pad contains an analog signal. It can be Radio Frequency,
  185. * Intermediate Frequency, a baseband signal or sub-carriers.
  186. * Tuner inputs, IF-PLL demodulators, composite and s-video signals
  187. * should use it.
  188. * @PAD_SIGNAL_DV:
  189. * Contains a digital video signal, with can be a bitstream of samples
  190. * taken from an analog TV video source. On such case, it usually
  191. * contains the VBI data on it.
  192. * @PAD_SIGNAL_AUDIO:
  193. * Contains an Intermediate Frequency analog signal from an audio
  194. * sub-carrier or an audio bitstream. IF signals are provided by tuners
  195. * and consumed by audio AM/FM decoders. Bitstream audio is provided by
  196. * an audio decoder.
  197. */
  198. enum media_pad_signal_type {
  199. PAD_SIGNAL_DEFAULT = 0,
  200. PAD_SIGNAL_ANALOG,
  201. PAD_SIGNAL_DV,
  202. PAD_SIGNAL_AUDIO,
  203. };
  204. /**
  205. * struct media_pad - A media pad graph object.
  206. *
  207. * @graph_obj: Embedded structure containing the media object common data
  208. * @entity: Entity this pad belongs to
  209. * @index: Pad index in the entity pads array, numbered from 0 to n
  210. * @num_links: Number of links connected to this pad
  211. * @sig_type: Type of the signal inside a media pad
  212. * @flags: Pad flags, as defined in
  213. * :ref:`include/uapi/linux/media.h <media_header>`
  214. * (seek for ``MEDIA_PAD_FL_*``)
  215. * @pipe: Pipeline this pad belongs to. Use media_entity_pipeline() to
  216. * access this field.
  217. */
  218. struct media_pad {
  219. struct media_gobj graph_obj; /* must be first field in struct */
  220. struct media_entity *entity;
  221. u16 index;
  222. u16 num_links;
  223. enum media_pad_signal_type sig_type;
  224. unsigned long flags;
  225. /*
  226. * The fields below are private, and should only be accessed via
  227. * appropriate functions.
  228. */
  229. struct media_pipeline *pipe;
  230. };
  231. /**
  232. * struct media_entity_operations - Media entity operations
  233. * @get_fwnode_pad: Return the pad number based on a fwnode endpoint or
  234. * a negative value on error. This operation can be used
  235. * to map a fwnode to a media pad number. Optional.
  236. * @link_setup: Notify the entity of link changes. The operation can
  237. * return an error, in which case link setup will be
  238. * cancelled. Optional.
  239. * @link_validate: Return whether a link is valid from the entity point of
  240. * view. The media_pipeline_start() function
  241. * validates all links by calling this operation. Optional.
  242. * @has_pad_interdep: Return whether two pads of the entity are
  243. * interdependent. If two pads are interdependent they are
  244. * part of the same pipeline and enabling one of the pads
  245. * means that the other pad will become "locked" and
  246. * doesn't allow configuration changes. pad0 and pad1 are
  247. * guaranteed to not both be sinks or sources. Never call
  248. * the .has_pad_interdep() operation directly, always use
  249. * media_entity_has_pad_interdep().
  250. * Optional: If the operation isn't implemented all pads
  251. * will be considered as interdependent.
  252. *
  253. * .. note::
  254. *
  255. * Those these callbacks are called with struct &media_device.graph_mutex
  256. * mutex held.
  257. */
  258. struct media_entity_operations {
  259. int (*get_fwnode_pad)(struct media_entity *entity,
  260. struct fwnode_endpoint *endpoint);
  261. int (*link_setup)(struct media_entity *entity,
  262. const struct media_pad *local,
  263. const struct media_pad *remote, u32 flags);
  264. int (*link_validate)(struct media_link *link);
  265. bool (*has_pad_interdep)(struct media_entity *entity, unsigned int pad0,
  266. unsigned int pad1);
  267. };
  268. /**
  269. * enum media_entity_type - Media entity type
  270. *
  271. * @MEDIA_ENTITY_TYPE_BASE:
  272. * The entity isn't embedded in another subsystem structure.
  273. * @MEDIA_ENTITY_TYPE_VIDEO_DEVICE:
  274. * The entity is embedded in a struct video_device instance.
  275. * @MEDIA_ENTITY_TYPE_V4L2_SUBDEV:
  276. * The entity is embedded in a struct v4l2_subdev instance.
  277. *
  278. * Media entity objects are often not instantiated directly, but the media
  279. * entity structure is inherited by (through embedding) other subsystem-specific
  280. * structures. The media entity type identifies the type of the subclass
  281. * structure that implements a media entity instance.
  282. *
  283. * This allows runtime type identification of media entities and safe casting to
  284. * the correct object type. For instance, a media entity structure instance
  285. * embedded in a v4l2_subdev structure instance will have the type
  286. * %MEDIA_ENTITY_TYPE_V4L2_SUBDEV and can safely be cast to a &v4l2_subdev
  287. * structure using the container_of() macro.
  288. */
  289. enum media_entity_type {
  290. MEDIA_ENTITY_TYPE_BASE,
  291. MEDIA_ENTITY_TYPE_VIDEO_DEVICE,
  292. MEDIA_ENTITY_TYPE_V4L2_SUBDEV,
  293. };
  294. /**
  295. * struct media_entity - A media entity graph object.
  296. *
  297. * @graph_obj: Embedded structure containing the media object common data.
  298. * @name: Entity name.
  299. * @obj_type: Type of the object that implements the media_entity.
  300. * @function: Entity main function, as defined in
  301. * :ref:`include/uapi/linux/media.h <media_header>`
  302. * (seek for ``MEDIA_ENT_F_*``)
  303. * @flags: Entity flags, as defined in
  304. * :ref:`include/uapi/linux/media.h <media_header>`
  305. * (seek for ``MEDIA_ENT_FL_*``)
  306. * @num_pads: Number of sink and source pads.
  307. * @num_links: Total number of links, forward and back, enabled and disabled.
  308. * @num_backlinks: Number of backlinks
  309. * @internal_idx: An unique internal entity specific number. The numbers are
  310. * re-used if entities are unregistered or registered again.
  311. * @pads: Pads array with the size defined by @num_pads.
  312. * @links: List of data links.
  313. * @ops: Entity operations.
  314. * @use_count: Use count for the entity.
  315. * @info: Union with devnode information. Kept just for backward
  316. * compatibility.
  317. * @info.dev: Contains device major and minor info.
  318. * @info.dev.major: device node major, if the device is a devnode.
  319. * @info.dev.minor: device node minor, if the device is a devnode.
  320. *
  321. * .. note::
  322. *
  323. * The @use_count reference count must never be negative, but is a signed
  324. * integer on purpose: a simple ``WARN_ON(<0)`` check can be used to detect
  325. * reference count bugs that would make it negative.
  326. */
  327. struct media_entity {
  328. struct media_gobj graph_obj; /* must be first field in struct */
  329. const char *name;
  330. enum media_entity_type obj_type;
  331. u32 function;
  332. unsigned long flags;
  333. u16 num_pads;
  334. u16 num_links;
  335. u16 num_backlinks;
  336. int internal_idx;
  337. struct media_pad *pads;
  338. struct list_head links;
  339. const struct media_entity_operations *ops;
  340. int use_count;
  341. union {
  342. struct {
  343. u32 major;
  344. u32 minor;
  345. } dev;
  346. } info;
  347. };
  348. /**
  349. * media_entity_for_each_pad - Iterate on all pads in an entity
  350. * @entity: The entity the pads belong to
  351. * @iter: The iterator pad
  352. *
  353. * Iterate on all pads in a media entity.
  354. */
  355. #define media_entity_for_each_pad(entity, iter) \
  356. for (iter = (entity)->pads; \
  357. iter < &(entity)->pads[(entity)->num_pads]; \
  358. ++iter)
  359. /**
  360. * struct media_interface - A media interface graph object.
  361. *
  362. * @graph_obj: embedded graph object
  363. * @links: List of links pointing to graph entities
  364. * @type: Type of the interface as defined in
  365. * :ref:`include/uapi/linux/media.h <media_header>`
  366. * (seek for ``MEDIA_INTF_T_*``)
  367. * @flags: Interface flags as defined in
  368. * :ref:`include/uapi/linux/media.h <media_header>`
  369. * (seek for ``MEDIA_INTF_FL_*``)
  370. *
  371. * .. note::
  372. *
  373. * Currently, no flags for &media_interface is defined.
  374. */
  375. struct media_interface {
  376. struct media_gobj graph_obj;
  377. struct list_head links;
  378. u32 type;
  379. u32 flags;
  380. };
  381. /**
  382. * struct media_intf_devnode - A media interface via a device node.
  383. *
  384. * @intf: embedded interface object
  385. * @major: Major number of a device node
  386. * @minor: Minor number of a device node
  387. */
  388. struct media_intf_devnode {
  389. struct media_interface intf;
  390. /* Should match the fields at media_v2_intf_devnode */
  391. u32 major;
  392. u32 minor;
  393. };
  394. /**
  395. * media_entity_id() - return the media entity graph object id
  396. *
  397. * @entity: pointer to &media_entity
  398. */
  399. static inline u32 media_entity_id(struct media_entity *entity)
  400. {
  401. return entity->graph_obj.id;
  402. }
  403. /**
  404. * media_type() - return the media object type
  405. *
  406. * @gobj: Pointer to the struct &media_gobj graph object
  407. */
  408. static inline enum media_gobj_type media_type(struct media_gobj *gobj)
  409. {
  410. return gobj->id >> MEDIA_BITS_PER_ID;
  411. }
  412. /**
  413. * media_id() - return the media object ID
  414. *
  415. * @gobj: Pointer to the struct &media_gobj graph object
  416. */
  417. static inline u32 media_id(struct media_gobj *gobj)
  418. {
  419. return gobj->id & MEDIA_ID_MASK;
  420. }
  421. /**
  422. * media_gobj_gen_id() - encapsulates type and ID on at the object ID
  423. *
  424. * @type: object type as define at enum &media_gobj_type.
  425. * @local_id: next ID, from struct &media_device.id.
  426. */
  427. static inline u32 media_gobj_gen_id(enum media_gobj_type type, u64 local_id)
  428. {
  429. u32 id;
  430. id = type << MEDIA_BITS_PER_ID;
  431. id |= local_id & MEDIA_ID_MASK;
  432. return id;
  433. }
  434. /**
  435. * is_media_entity_v4l2_video_device() - Check if the entity is a video_device
  436. * @entity: pointer to entity
  437. *
  438. * Return: %true if the entity is an instance of a video_device object and can
  439. * safely be cast to a struct video_device using the container_of() macro, or
  440. * %false otherwise.
  441. */
  442. static inline bool is_media_entity_v4l2_video_device(struct media_entity *entity)
  443. {
  444. return entity && entity->obj_type == MEDIA_ENTITY_TYPE_VIDEO_DEVICE;
  445. }
  446. /**
  447. * is_media_entity_v4l2_subdev() - Check if the entity is a v4l2_subdev
  448. * @entity: pointer to entity
  449. *
  450. * Return: %true if the entity is an instance of a &v4l2_subdev object and can
  451. * safely be cast to a struct &v4l2_subdev using the container_of() macro, or
  452. * %false otherwise.
  453. */
  454. static inline bool is_media_entity_v4l2_subdev(struct media_entity *entity)
  455. {
  456. return entity && entity->obj_type == MEDIA_ENTITY_TYPE_V4L2_SUBDEV;
  457. }
  458. /**
  459. * media_entity_enum_init - Initialise an entity enumeration
  460. *
  461. * @ent_enum: Entity enumeration to be initialised
  462. * @mdev: The related media device
  463. *
  464. * Return: zero on success or a negative error code.
  465. */
  466. __must_check int media_entity_enum_init(struct media_entity_enum *ent_enum,
  467. struct media_device *mdev);
  468. /**
  469. * media_entity_enum_cleanup - Release resources of an entity enumeration
  470. *
  471. * @ent_enum: Entity enumeration to be released
  472. */
  473. void media_entity_enum_cleanup(struct media_entity_enum *ent_enum);
  474. /**
  475. * media_entity_enum_zero - Clear the entire enum
  476. *
  477. * @ent_enum: Entity enumeration to be cleared
  478. */
  479. static inline void media_entity_enum_zero(struct media_entity_enum *ent_enum)
  480. {
  481. bitmap_zero(ent_enum->bmap, ent_enum->idx_max);
  482. }
  483. /**
  484. * media_entity_enum_set - Mark a single entity in the enum
  485. *
  486. * @ent_enum: Entity enumeration
  487. * @entity: Entity to be marked
  488. */
  489. static inline void media_entity_enum_set(struct media_entity_enum *ent_enum,
  490. struct media_entity *entity)
  491. {
  492. if (WARN_ON(entity->internal_idx >= ent_enum->idx_max))
  493. return;
  494. __set_bit(entity->internal_idx, ent_enum->bmap);
  495. }
  496. /**
  497. * media_entity_enum_clear - Unmark a single entity in the enum
  498. *
  499. * @ent_enum: Entity enumeration
  500. * @entity: Entity to be unmarked
  501. */
  502. static inline void media_entity_enum_clear(struct media_entity_enum *ent_enum,
  503. struct media_entity *entity)
  504. {
  505. if (WARN_ON(entity->internal_idx >= ent_enum->idx_max))
  506. return;
  507. __clear_bit(entity->internal_idx, ent_enum->bmap);
  508. }
  509. /**
  510. * media_entity_enum_test - Test whether the entity is marked
  511. *
  512. * @ent_enum: Entity enumeration
  513. * @entity: Entity to be tested
  514. *
  515. * Returns %true if the entity was marked.
  516. */
  517. static inline bool media_entity_enum_test(struct media_entity_enum *ent_enum,
  518. struct media_entity *entity)
  519. {
  520. if (WARN_ON(entity->internal_idx >= ent_enum->idx_max))
  521. return true;
  522. return test_bit(entity->internal_idx, ent_enum->bmap);
  523. }
  524. /**
  525. * media_entity_enum_test_and_set - Test whether the entity is marked,
  526. * and mark it
  527. *
  528. * @ent_enum: Entity enumeration
  529. * @entity: Entity to be tested
  530. *
  531. * Returns %true if the entity was marked, and mark it before doing so.
  532. */
  533. static inline bool
  534. media_entity_enum_test_and_set(struct media_entity_enum *ent_enum,
  535. struct media_entity *entity)
  536. {
  537. if (WARN_ON(entity->internal_idx >= ent_enum->idx_max))
  538. return true;
  539. return __test_and_set_bit(entity->internal_idx, ent_enum->bmap);
  540. }
  541. /**
  542. * media_entity_enum_empty - Test whether the entire enum is empty
  543. *
  544. * @ent_enum: Entity enumeration
  545. *
  546. * Return: %true if the entity was empty.
  547. */
  548. static inline bool media_entity_enum_empty(struct media_entity_enum *ent_enum)
  549. {
  550. return bitmap_empty(ent_enum->bmap, ent_enum->idx_max);
  551. }
  552. /**
  553. * media_entity_enum_intersects - Test whether two enums intersect
  554. *
  555. * @ent_enum1: First entity enumeration
  556. * @ent_enum2: Second entity enumeration
  557. *
  558. * Return: %true if entity enumerations @ent_enum1 and @ent_enum2 intersect,
  559. * otherwise %false.
  560. */
  561. static inline bool media_entity_enum_intersects(
  562. struct media_entity_enum *ent_enum1,
  563. struct media_entity_enum *ent_enum2)
  564. {
  565. WARN_ON(ent_enum1->idx_max != ent_enum2->idx_max);
  566. return bitmap_intersects(ent_enum1->bmap, ent_enum2->bmap,
  567. min(ent_enum1->idx_max, ent_enum2->idx_max));
  568. }
  569. /**
  570. * gobj_to_entity - returns the struct &media_entity pointer from the
  571. * @gobj contained on it.
  572. *
  573. * @gobj: Pointer to the struct &media_gobj graph object
  574. */
  575. #define gobj_to_entity(gobj) \
  576. container_of_const(gobj, struct media_entity, graph_obj)
  577. /**
  578. * gobj_to_pad - returns the struct &media_pad pointer from the
  579. * @gobj contained on it.
  580. *
  581. * @gobj: Pointer to the struct &media_gobj graph object
  582. */
  583. #define gobj_to_pad(gobj) \
  584. container_of_const(gobj, struct media_pad, graph_obj)
  585. /**
  586. * gobj_to_link - returns the struct &media_link pointer from the
  587. * @gobj contained on it.
  588. *
  589. * @gobj: Pointer to the struct &media_gobj graph object
  590. */
  591. #define gobj_to_link(gobj) \
  592. container_of_const(gobj, struct media_link, graph_obj)
  593. /**
  594. * gobj_to_intf - returns the struct &media_interface pointer from the
  595. * @gobj contained on it.
  596. *
  597. * @gobj: Pointer to the struct &media_gobj graph object
  598. */
  599. #define gobj_to_intf(gobj) \
  600. container_of_const(gobj, struct media_interface, graph_obj)
  601. /**
  602. * intf_to_devnode - returns the struct media_intf_devnode pointer from the
  603. * @intf contained on it.
  604. *
  605. * @intf: Pointer to struct &media_intf_devnode
  606. */
  607. #define intf_to_devnode(intf) \
  608. container_of_const(intf, struct media_intf_devnode, intf)
  609. /**
  610. * media_gobj_create - Initialize a graph object
  611. *
  612. * @mdev: Pointer to the &media_device that contains the object
  613. * @type: Type of the object
  614. * @gobj: Pointer to the struct &media_gobj graph object
  615. *
  616. * This routine initializes the embedded struct &media_gobj inside a
  617. * media graph object. It is called automatically if ``media_*_create``
  618. * function calls are used. However, if the object (entity, link, pad,
  619. * interface) is embedded on some other object, this function should be
  620. * called before registering the object at the media controller.
  621. */
  622. void media_gobj_create(struct media_device *mdev,
  623. enum media_gobj_type type,
  624. struct media_gobj *gobj);
  625. /**
  626. * media_gobj_destroy - Stop using a graph object on a media device
  627. *
  628. * @gobj: Pointer to the struct &media_gobj graph object
  629. *
  630. * This should be called by all routines like media_device_unregister()
  631. * that remove/destroy media graph objects.
  632. */
  633. void media_gobj_destroy(struct media_gobj *gobj);
  634. /**
  635. * media_entity_pads_init() - Initialize the entity pads
  636. *
  637. * @entity: entity where the pads belong
  638. * @num_pads: total number of sink and source pads
  639. * @pads: Array of @num_pads pads.
  640. *
  641. * The pads array is managed by the entity driver and passed to
  642. * media_entity_pads_init() where its pointer will be stored in the
  643. * &media_entity structure.
  644. *
  645. * If no pads are needed, drivers could either directly fill
  646. * &media_entity->num_pads with 0 and &media_entity->pads with %NULL or call
  647. * this function that will do the same.
  648. *
  649. * As the number of pads is known in advance, the pads array is not allocated
  650. * dynamically but is managed by the entity driver. Most drivers will embed the
  651. * pads array in a driver-specific structure, avoiding dynamic allocation.
  652. *
  653. * Drivers must set the direction of every pad in the pads array before calling
  654. * media_entity_pads_init(). The function will initialize the other pads fields.
  655. */
  656. int media_entity_pads_init(struct media_entity *entity, u16 num_pads,
  657. struct media_pad *pads);
  658. /**
  659. * media_entity_cleanup() - free resources associated with an entity
  660. *
  661. * @entity: entity where the pads belong
  662. *
  663. * This function must be called during the cleanup phase after unregistering
  664. * the entity (currently, it does nothing).
  665. *
  666. * Calling media_entity_cleanup() on a media_entity whose memory has been
  667. * zeroed but that has not been initialized with media_entity_pad_init() is
  668. * valid and is a no-op.
  669. */
  670. #if IS_ENABLED(CONFIG_MEDIA_CONTROLLER)
  671. static inline void media_entity_cleanup(struct media_entity *entity) {}
  672. #else
  673. #define media_entity_cleanup(entity) do { } while (false)
  674. #endif
  675. /**
  676. * media_get_pad_index() - retrieves a pad index from an entity
  677. *
  678. * @entity: entity where the pads belong
  679. * @pad_type: the type of the pad, one of MEDIA_PAD_FL_* pad types
  680. * @sig_type: type of signal of the pad to be search
  681. *
  682. * This helper function finds the first pad index inside an entity that
  683. * satisfies both @is_sink and @sig_type conditions.
  684. *
  685. * Return:
  686. *
  687. * On success, return the pad number. If the pad was not found or the media
  688. * entity is a NULL pointer, return -EINVAL.
  689. */
  690. int media_get_pad_index(struct media_entity *entity, u32 pad_type,
  691. enum media_pad_signal_type sig_type);
  692. /**
  693. * media_create_pad_link() - creates a link between two entities.
  694. *
  695. * @source: pointer to &media_entity of the source pad.
  696. * @source_pad: number of the source pad in the pads array
  697. * @sink: pointer to &media_entity of the sink pad.
  698. * @sink_pad: number of the sink pad in the pads array.
  699. * @flags: Link flags, as defined in
  700. * :ref:`include/uapi/linux/media.h <media_header>`
  701. * ( seek for ``MEDIA_LNK_FL_*``)
  702. *
  703. * Valid values for flags:
  704. *
  705. * %MEDIA_LNK_FL_ENABLED
  706. * Indicates that the link is enabled and can be used to transfer media data.
  707. * When two or more links target a sink pad, only one of them can be
  708. * enabled at a time.
  709. *
  710. * %MEDIA_LNK_FL_IMMUTABLE
  711. * Indicates that the link enabled state can't be modified at runtime. If
  712. * %MEDIA_LNK_FL_IMMUTABLE is set, then %MEDIA_LNK_FL_ENABLED must also be
  713. * set, since an immutable link is always enabled.
  714. *
  715. * .. note::
  716. *
  717. * Before calling this function, media_entity_pads_init() and
  718. * media_device_register_entity() should be called previously for both ends.
  719. */
  720. __must_check int media_create_pad_link(struct media_entity *source,
  721. u16 source_pad, struct media_entity *sink,
  722. u16 sink_pad, u32 flags);
  723. /**
  724. * media_create_pad_links() - creates a link between two entities.
  725. *
  726. * @mdev: Pointer to the media_device that contains the object
  727. * @source_function: Function of the source entities. Used only if @source is
  728. * NULL.
  729. * @source: pointer to &media_entity of the source pad. If NULL, it will use
  730. * all entities that matches the @sink_function.
  731. * @source_pad: number of the source pad in the pads array
  732. * @sink_function: Function of the sink entities. Used only if @sink is NULL.
  733. * @sink: pointer to &media_entity of the sink pad. If NULL, it will use
  734. * all entities that matches the @sink_function.
  735. * @sink_pad: number of the sink pad in the pads array.
  736. * @flags: Link flags, as defined in include/uapi/linux/media.h.
  737. * @allow_both_undefined: if %true, then both @source and @sink can be NULL.
  738. * In such case, it will create a crossbar between all entities that
  739. * matches @source_function to all entities that matches @sink_function.
  740. * If %false, it will return 0 and won't create any link if both @source
  741. * and @sink are NULL.
  742. *
  743. * Valid values for flags:
  744. *
  745. * A %MEDIA_LNK_FL_ENABLED flag indicates that the link is enabled and can be
  746. * used to transfer media data. If multiple links are created and this
  747. * flag is passed as an argument, only the first created link will have
  748. * this flag.
  749. *
  750. * A %MEDIA_LNK_FL_IMMUTABLE flag indicates that the link enabled state can't
  751. * be modified at runtime. If %MEDIA_LNK_FL_IMMUTABLE is set, then
  752. * %MEDIA_LNK_FL_ENABLED must also be set since an immutable link is
  753. * always enabled.
  754. *
  755. * It is common for some devices to have multiple source and/or sink entities
  756. * of the same type that should be linked. While media_create_pad_link()
  757. * creates link by link, this function is meant to allow 1:n, n:1 and even
  758. * cross-bar (n:n) links.
  759. *
  760. * .. note::
  761. *
  762. * Before calling this function, media_entity_pads_init() and
  763. * media_device_register_entity() should be called previously for the
  764. * entities to be linked.
  765. */
  766. int media_create_pad_links(const struct media_device *mdev,
  767. const u32 source_function,
  768. struct media_entity *source,
  769. const u16 source_pad,
  770. const u32 sink_function,
  771. struct media_entity *sink,
  772. const u16 sink_pad,
  773. u32 flags,
  774. const bool allow_both_undefined);
  775. void __media_entity_remove_links(struct media_entity *entity);
  776. /**
  777. * media_entity_remove_links() - remove all links associated with an entity
  778. *
  779. * @entity: pointer to &media_entity
  780. *
  781. * .. note::
  782. *
  783. * This is called automatically when an entity is unregistered via
  784. * media_device_register_entity().
  785. */
  786. void media_entity_remove_links(struct media_entity *entity);
  787. /**
  788. * __media_entity_setup_link - Configure a media link without locking
  789. * @link: The link being configured
  790. * @flags: Link configuration flags
  791. *
  792. * The bulk of link setup is handled by the two entities connected through the
  793. * link. This function notifies both entities of the link configuration change.
  794. *
  795. * If the link is immutable or if the current and new configuration are
  796. * identical, return immediately.
  797. *
  798. * The user is expected to hold link->source->parent->mutex. If not,
  799. * media_entity_setup_link() should be used instead.
  800. */
  801. int __media_entity_setup_link(struct media_link *link, u32 flags);
  802. /**
  803. * media_entity_setup_link() - changes the link flags properties in runtime
  804. *
  805. * @link: pointer to &media_link
  806. * @flags: the requested new link flags
  807. *
  808. * The only configurable property is the %MEDIA_LNK_FL_ENABLED link flag
  809. * to enable/disable a link. Links marked with the
  810. * %MEDIA_LNK_FL_IMMUTABLE link flag can not be enabled or disabled.
  811. *
  812. * When a link is enabled or disabled, the media framework calls the
  813. * link_setup operation for the two entities at the source and sink of the
  814. * link, in that order. If the second link_setup call fails, another
  815. * link_setup call is made on the first entity to restore the original link
  816. * flags.
  817. *
  818. * Media device drivers can be notified of link setup operations by setting the
  819. * &media_device.link_notify pointer to a callback function. If provided, the
  820. * notification callback will be called before enabling and after disabling
  821. * links.
  822. *
  823. * Entity drivers must implement the link_setup operation if any of their links
  824. * is non-immutable. The operation must either configure the hardware or store
  825. * the configuration information to be applied later.
  826. *
  827. * Link configuration must not have any side effect on other links. If an
  828. * enabled link at a sink pad prevents another link at the same pad from
  829. * being enabled, the link_setup operation must return %-EBUSY and can't
  830. * implicitly disable the first enabled link.
  831. *
  832. * .. note::
  833. *
  834. * The valid values of the flags for the link is the same as described
  835. * on media_create_pad_link(), for pad to pad links or the same as described
  836. * on media_create_intf_link(), for interface to entity links.
  837. */
  838. int media_entity_setup_link(struct media_link *link, u32 flags);
  839. /**
  840. * media_entity_find_link - Find a link between two pads
  841. * @source: Source pad
  842. * @sink: Sink pad
  843. *
  844. * Return: returns a pointer to the link between the two entities. If no
  845. * such link exists, return %NULL.
  846. */
  847. struct media_link *media_entity_find_link(struct media_pad *source,
  848. struct media_pad *sink);
  849. /**
  850. * media_pad_remote_pad_first - Find the first pad at the remote end of a link
  851. * @pad: Pad at the local end of the link
  852. *
  853. * Search for a remote pad connected to the given pad by iterating over all
  854. * links originating or terminating at that pad until an enabled link is found.
  855. *
  856. * Return: returns a pointer to the pad at the remote end of the first found
  857. * enabled link, or %NULL if no enabled link has been found.
  858. */
  859. struct media_pad *media_pad_remote_pad_first(const struct media_pad *pad);
  860. /**
  861. * media_pad_remote_pad_unique - Find a remote pad connected to a pad
  862. * @pad: The pad
  863. *
  864. * Search for and return a remote pad connected to @pad through an enabled
  865. * link. If multiple (or no) remote pads are found, an error is returned.
  866. *
  867. * The uniqueness constraint makes this helper function suitable for entities
  868. * that support a single active source at a time on a given pad.
  869. *
  870. * Return: A pointer to the remote pad, or one of the following error pointers
  871. * if an error occurs:
  872. *
  873. * * -ENOTUNIQ - Multiple links are enabled
  874. * * -ENOLINK - No connected pad found
  875. */
  876. struct media_pad *media_pad_remote_pad_unique(const struct media_pad *pad);
  877. /**
  878. * media_entity_remote_pad_unique - Find a remote pad connected to an entity
  879. * @entity: The entity
  880. * @type: The type of pad to find (MEDIA_PAD_FL_SINK or MEDIA_PAD_FL_SOURCE)
  881. *
  882. * Search for and return a remote pad of @type connected to @entity through an
  883. * enabled link. If multiple (or no) remote pads match these criteria, an error
  884. * is returned.
  885. *
  886. * The uniqueness constraint makes this helper function suitable for entities
  887. * that support a single active source or sink at a time.
  888. *
  889. * Return: A pointer to the remote pad, or one of the following error pointers
  890. * if an error occurs:
  891. *
  892. * * -ENOTUNIQ - Multiple links are enabled
  893. * * -ENOLINK - No connected pad found
  894. */
  895. struct media_pad *
  896. media_entity_remote_pad_unique(const struct media_entity *entity,
  897. unsigned int type);
  898. /**
  899. * media_entity_remote_source_pad_unique - Find a remote source pad connected to
  900. * an entity
  901. * @entity: The entity
  902. *
  903. * Search for and return a remote source pad connected to @entity through an
  904. * enabled link. If multiple (or no) remote pads match these criteria, an error
  905. * is returned.
  906. *
  907. * The uniqueness constraint makes this helper function suitable for entities
  908. * that support a single active source at a time.
  909. *
  910. * Return: A pointer to the remote pad, or one of the following error pointers
  911. * if an error occurs:
  912. *
  913. * * -ENOTUNIQ - Multiple links are enabled
  914. * * -ENOLINK - No connected pad found
  915. */
  916. static inline struct media_pad *
  917. media_entity_remote_source_pad_unique(const struct media_entity *entity)
  918. {
  919. return media_entity_remote_pad_unique(entity, MEDIA_PAD_FL_SOURCE);
  920. }
  921. /**
  922. * media_pad_is_streaming - Test if a pad is part of a streaming pipeline
  923. * @pad: The pad
  924. *
  925. * Return: True if the pad is part of a pipeline started with the
  926. * media_pipeline_start() function, false otherwise.
  927. */
  928. static inline bool media_pad_is_streaming(const struct media_pad *pad)
  929. {
  930. return pad->pipe;
  931. }
  932. /**
  933. * media_entity_is_streaming - Test if an entity is part of a streaming pipeline
  934. * @entity: The entity
  935. *
  936. * Return: True if the entity is part of a pipeline started with the
  937. * media_pipeline_start() function, false otherwise.
  938. */
  939. static inline bool media_entity_is_streaming(const struct media_entity *entity)
  940. {
  941. struct media_pad *pad;
  942. media_entity_for_each_pad(entity, pad) {
  943. if (media_pad_is_streaming(pad))
  944. return true;
  945. }
  946. return false;
  947. }
  948. /**
  949. * media_entity_pipeline - Get the media pipeline an entity is part of
  950. * @entity: The entity
  951. *
  952. * DEPRECATED: use media_pad_pipeline() instead.
  953. *
  954. * This function returns the media pipeline that an entity has been associated
  955. * with when constructing the pipeline with media_pipeline_start(). The pointer
  956. * remains valid until media_pipeline_stop() is called.
  957. *
  958. * In general, entities can be part of multiple pipelines, when carrying
  959. * multiple streams (either on different pads, or on the same pad using
  960. * multiplexed streams). This function is to be used only for entities that
  961. * do not support multiple pipelines.
  962. *
  963. * Return: The media_pipeline the entity is part of, or NULL if the entity is
  964. * not part of any pipeline.
  965. */
  966. struct media_pipeline *media_entity_pipeline(struct media_entity *entity);
  967. /**
  968. * media_pad_pipeline - Get the media pipeline a pad is part of
  969. * @pad: The pad
  970. *
  971. * This function returns the media pipeline that a pad has been associated
  972. * with when constructing the pipeline with media_pipeline_start(). The pointer
  973. * remains valid until media_pipeline_stop() is called.
  974. *
  975. * Return: The media_pipeline the pad is part of, or NULL if the pad is
  976. * not part of any pipeline.
  977. */
  978. struct media_pipeline *media_pad_pipeline(struct media_pad *pad);
  979. /**
  980. * media_entity_get_fwnode_pad - Get pad number from fwnode
  981. *
  982. * @entity: The entity
  983. * @fwnode: Pointer to the fwnode_handle which should be used to find the pad
  984. * @direction_flags: Expected direction of the pad, as defined in
  985. * :ref:`include/uapi/linux/media.h <media_header>`
  986. * (seek for ``MEDIA_PAD_FL_*``)
  987. *
  988. * This function can be used to resolve the media pad number from
  989. * a fwnode. This is useful for devices which use more complex
  990. * mappings of media pads.
  991. *
  992. * If the entity does not implement the get_fwnode_pad() operation
  993. * then this function searches the entity for the first pad that
  994. * matches the @direction_flags.
  995. *
  996. * Return: returns the pad number on success or a negative error code.
  997. */
  998. int media_entity_get_fwnode_pad(struct media_entity *entity,
  999. const struct fwnode_handle *fwnode,
  1000. unsigned long direction_flags);
  1001. /**
  1002. * media_graph_walk_init - Allocate resources used by graph walk.
  1003. *
  1004. * @graph: Media graph structure that will be used to walk the graph
  1005. * @mdev: Pointer to the &media_device that contains the object
  1006. *
  1007. * This function is deprecated, use media_pipeline_for_each_pad() instead.
  1008. *
  1009. * The caller is required to hold the media_device graph_mutex during the graph
  1010. * walk until the graph state is released.
  1011. *
  1012. * Returns zero on success or a negative error code otherwise.
  1013. */
  1014. __must_check int media_graph_walk_init(
  1015. struct media_graph *graph, struct media_device *mdev);
  1016. /**
  1017. * media_graph_walk_cleanup - Release resources used by graph walk.
  1018. *
  1019. * @graph: Media graph structure that will be used to walk the graph
  1020. *
  1021. * This function is deprecated, use media_pipeline_for_each_pad() instead.
  1022. */
  1023. void media_graph_walk_cleanup(struct media_graph *graph);
  1024. /**
  1025. * media_graph_walk_start - Start walking the media graph at a
  1026. * given entity
  1027. *
  1028. * @graph: Media graph structure that will be used to walk the graph
  1029. * @entity: Starting entity
  1030. *
  1031. * This function is deprecated, use media_pipeline_for_each_pad() instead.
  1032. *
  1033. * Before using this function, media_graph_walk_init() must be
  1034. * used to allocate resources used for walking the graph. This
  1035. * function initializes the graph traversal structure to walk the
  1036. * entities graph starting at the given entity. The traversal
  1037. * structure must not be modified by the caller during graph
  1038. * traversal. After the graph walk, the resources must be released
  1039. * using media_graph_walk_cleanup().
  1040. */
  1041. void media_graph_walk_start(struct media_graph *graph,
  1042. struct media_entity *entity);
  1043. /**
  1044. * media_graph_walk_next - Get the next entity in the graph
  1045. * @graph: Media graph structure
  1046. *
  1047. * This function is deprecated, use media_pipeline_for_each_pad() instead.
  1048. *
  1049. * Perform a depth-first traversal of the given media entities graph.
  1050. *
  1051. * The graph structure must have been previously initialized with a call to
  1052. * media_graph_walk_start().
  1053. *
  1054. * Return: returns the next entity in the graph or %NULL if the whole graph
  1055. * have been traversed.
  1056. */
  1057. struct media_entity *media_graph_walk_next(struct media_graph *graph);
  1058. /**
  1059. * media_pipeline_start - Mark a pipeline as streaming
  1060. * @origin: Starting pad
  1061. * @pipe: Media pipeline to be assigned to all pads in the pipeline.
  1062. *
  1063. * Mark all pads connected to pad @origin through enabled links, either
  1064. * directly or indirectly, as streaming. The given pipeline object is assigned
  1065. * to every pad in the pipeline and stored in the media_pad pipe field.
  1066. *
  1067. * Calls to this function can be nested, in which case the same number of
  1068. * media_pipeline_stop() calls will be required to stop streaming. The
  1069. * pipeline pointer must be identical for all nested calls to
  1070. * media_pipeline_start().
  1071. */
  1072. __must_check int media_pipeline_start(struct media_pad *origin,
  1073. struct media_pipeline *pipe);
  1074. /**
  1075. * __media_pipeline_start - Mark a pipeline as streaming
  1076. *
  1077. * @origin: Starting pad
  1078. * @pipe: Media pipeline to be assigned to all pads in the pipeline.
  1079. *
  1080. * ..note:: This is the non-locking version of media_pipeline_start()
  1081. */
  1082. __must_check int __media_pipeline_start(struct media_pad *origin,
  1083. struct media_pipeline *pipe);
  1084. /**
  1085. * media_pipeline_stop - Mark a pipeline as not streaming
  1086. * @pad: Starting pad
  1087. *
  1088. * Mark all pads connected to a given pad through enabled links, either
  1089. * directly or indirectly, as not streaming. The media_pad pipe field is
  1090. * reset to %NULL.
  1091. *
  1092. * If multiple calls to media_pipeline_start() have been made, the same
  1093. * number of calls to this function are required to mark the pipeline as not
  1094. * streaming.
  1095. */
  1096. void media_pipeline_stop(struct media_pad *pad);
  1097. /**
  1098. * __media_pipeline_stop - Mark a pipeline as not streaming
  1099. *
  1100. * @pad: Starting pad
  1101. *
  1102. * .. note:: This is the non-locking version of media_pipeline_stop()
  1103. */
  1104. void __media_pipeline_stop(struct media_pad *pad);
  1105. struct media_pad *
  1106. __media_pipeline_pad_iter_next(struct media_pipeline *pipe,
  1107. struct media_pipeline_pad_iter *iter,
  1108. struct media_pad *pad);
  1109. /**
  1110. * media_pipeline_for_each_pad - Iterate on all pads in a media pipeline
  1111. * @pipe: The pipeline
  1112. * @iter: The iterator (struct media_pipeline_pad_iter)
  1113. * @pad: The iterator pad
  1114. *
  1115. * Iterate on all pads in a media pipeline. This is only valid after the
  1116. * pipeline has been built with media_pipeline_start() and before it gets
  1117. * destroyed with media_pipeline_stop().
  1118. */
  1119. #define media_pipeline_for_each_pad(pipe, iter, pad) \
  1120. for (pad = __media_pipeline_pad_iter_next((pipe), iter, NULL); \
  1121. pad != NULL; \
  1122. pad = __media_pipeline_pad_iter_next((pipe), iter, pad))
  1123. /**
  1124. * media_pipeline_entity_iter_init - Initialize a pipeline entity iterator
  1125. * @pipe: The pipeline
  1126. * @iter: The iterator
  1127. *
  1128. * This function must be called to initialize the iterator before using it in a
  1129. * media_pipeline_for_each_entity() loop. The iterator must be destroyed by a
  1130. * call to media_pipeline_entity_iter_cleanup after the loop (including in code
  1131. * paths that break from the loop).
  1132. *
  1133. * The same iterator can be used in multiple consecutive loops without being
  1134. * destroyed and reinitialized.
  1135. *
  1136. * Return: 0 on success or a negative error code otherwise.
  1137. */
  1138. int media_pipeline_entity_iter_init(struct media_pipeline *pipe,
  1139. struct media_pipeline_entity_iter *iter);
  1140. /**
  1141. * media_pipeline_entity_iter_cleanup - Destroy a pipeline entity iterator
  1142. * @iter: The iterator
  1143. *
  1144. * This function must be called to destroy iterators initialized with
  1145. * media_pipeline_entity_iter_init().
  1146. */
  1147. void media_pipeline_entity_iter_cleanup(struct media_pipeline_entity_iter *iter);
  1148. struct media_entity *
  1149. __media_pipeline_entity_iter_next(struct media_pipeline *pipe,
  1150. struct media_pipeline_entity_iter *iter,
  1151. struct media_entity *entity);
  1152. /**
  1153. * media_pipeline_for_each_entity - Iterate on all entities in a media pipeline
  1154. * @pipe: The pipeline
  1155. * @iter: The iterator (struct media_pipeline_entity_iter)
  1156. * @entity: The iterator entity
  1157. *
  1158. * Iterate on all entities in a media pipeline. This is only valid after the
  1159. * pipeline has been built with media_pipeline_start() and before it gets
  1160. * destroyed with media_pipeline_stop(). The iterator must be initialized with
  1161. * media_pipeline_entity_iter_init() before iteration, and destroyed with
  1162. * media_pipeline_entity_iter_cleanup() after (including in code paths that
  1163. * break from the loop).
  1164. */
  1165. #define media_pipeline_for_each_entity(pipe, iter, entity) \
  1166. for (entity = __media_pipeline_entity_iter_next((pipe), iter, NULL); \
  1167. entity != NULL; \
  1168. entity = __media_pipeline_entity_iter_next((pipe), iter, entity))
  1169. /**
  1170. * media_pipeline_alloc_start - Mark a pipeline as streaming
  1171. * @pad: Starting pad
  1172. *
  1173. * media_pipeline_alloc_start() is similar to media_pipeline_start() but instead
  1174. * of working on a given pipeline the function will use an existing pipeline if
  1175. * the pad is already part of a pipeline, or allocate a new pipeline.
  1176. *
  1177. * Calls to media_pipeline_alloc_start() must be matched with
  1178. * media_pipeline_stop().
  1179. */
  1180. __must_check int media_pipeline_alloc_start(struct media_pad *pad);
  1181. /**
  1182. * media_devnode_create() - creates and initializes a device node interface
  1183. *
  1184. * @mdev: pointer to struct &media_device
  1185. * @type: type of the interface, as given by
  1186. * :ref:`include/uapi/linux/media.h <media_header>`
  1187. * ( seek for ``MEDIA_INTF_T_*``) macros.
  1188. * @flags: Interface flags, as defined in
  1189. * :ref:`include/uapi/linux/media.h <media_header>`
  1190. * ( seek for ``MEDIA_INTF_FL_*``)
  1191. * @major: Device node major number.
  1192. * @minor: Device node minor number.
  1193. *
  1194. * Return: if succeeded, returns a pointer to the newly allocated
  1195. * &media_intf_devnode pointer.
  1196. *
  1197. * .. note::
  1198. *
  1199. * Currently, no flags for &media_interface is defined.
  1200. */
  1201. struct media_intf_devnode *
  1202. __must_check media_devnode_create(struct media_device *mdev,
  1203. u32 type, u32 flags,
  1204. u32 major, u32 minor);
  1205. /**
  1206. * media_devnode_remove() - removes a device node interface
  1207. *
  1208. * @devnode: pointer to &media_intf_devnode to be freed.
  1209. *
  1210. * When a device node interface is removed, all links to it are automatically
  1211. * removed.
  1212. */
  1213. void media_devnode_remove(struct media_intf_devnode *devnode);
  1214. /**
  1215. * media_create_intf_link() - creates a link between an entity and an interface
  1216. *
  1217. * @entity: pointer to %media_entity
  1218. * @intf: pointer to %media_interface
  1219. * @flags: Link flags, as defined in
  1220. * :ref:`include/uapi/linux/media.h <media_header>`
  1221. * ( seek for ``MEDIA_LNK_FL_*``)
  1222. *
  1223. *
  1224. * Valid values for flags:
  1225. *
  1226. * %MEDIA_LNK_FL_ENABLED
  1227. * Indicates that the interface is connected to the entity hardware.
  1228. * That's the default value for interfaces. An interface may be disabled if
  1229. * the hardware is busy due to the usage of some other interface that it is
  1230. * currently controlling the hardware.
  1231. *
  1232. * A typical example is an hybrid TV device that handle only one type of
  1233. * stream on a given time. So, when the digital TV is streaming,
  1234. * the V4L2 interfaces won't be enabled, as such device is not able to
  1235. * also stream analog TV or radio.
  1236. *
  1237. * .. note::
  1238. *
  1239. * Before calling this function, media_devnode_create() should be called for
  1240. * the interface and media_device_register_entity() should be called for the
  1241. * interface that will be part of the link.
  1242. */
  1243. struct media_link *
  1244. __must_check media_create_intf_link(struct media_entity *entity,
  1245. struct media_interface *intf,
  1246. u32 flags);
  1247. /**
  1248. * __media_remove_intf_link() - remove a single interface link
  1249. *
  1250. * @link: pointer to &media_link.
  1251. *
  1252. * .. note:: This is an unlocked version of media_remove_intf_link()
  1253. */
  1254. void __media_remove_intf_link(struct media_link *link);
  1255. /**
  1256. * media_remove_intf_link() - remove a single interface link
  1257. *
  1258. * @link: pointer to &media_link.
  1259. *
  1260. * .. note:: Prefer to use this one, instead of __media_remove_intf_link()
  1261. */
  1262. void media_remove_intf_link(struct media_link *link);
  1263. /**
  1264. * __media_remove_intf_links() - remove all links associated with an interface
  1265. *
  1266. * @intf: pointer to &media_interface
  1267. *
  1268. * .. note:: This is an unlocked version of media_remove_intf_links().
  1269. */
  1270. void __media_remove_intf_links(struct media_interface *intf);
  1271. /**
  1272. * media_remove_intf_links() - remove all links associated with an interface
  1273. *
  1274. * @intf: pointer to &media_interface
  1275. *
  1276. * .. note::
  1277. *
  1278. * #) This is called automatically when an entity is unregistered via
  1279. * media_device_register_entity() and by media_devnode_remove().
  1280. *
  1281. * #) Prefer to use this one, instead of __media_remove_intf_links().
  1282. */
  1283. void media_remove_intf_links(struct media_interface *intf);
  1284. /**
  1285. * media_entity_call - Calls a struct media_entity_operations operation on
  1286. * an entity
  1287. *
  1288. * @entity: entity where the @operation will be called
  1289. * @operation: type of the operation. Should be the name of a member of
  1290. * struct &media_entity_operations.
  1291. *
  1292. * This helper function will check if @operation is not %NULL. On such case,
  1293. * it will issue a call to @operation\(@entity, @args\).
  1294. */
  1295. #define media_entity_call(entity, operation, args...) \
  1296. (((entity)->ops && (entity)->ops->operation) ? \
  1297. (entity)->ops->operation((entity) , ##args) : -ENOIOCTLCMD)
  1298. /**
  1299. * media_create_ancillary_link() - create an ancillary link between two
  1300. * instances of &media_entity
  1301. *
  1302. * @primary: pointer to the primary &media_entity
  1303. * @ancillary: pointer to the ancillary &media_entity
  1304. *
  1305. * Create an ancillary link between two entities, indicating that they
  1306. * represent two connected pieces of hardware that form a single logical unit.
  1307. * A typical example is a camera lens controller being linked to the sensor that
  1308. * it is supporting.
  1309. *
  1310. * The function sets both MEDIA_LNK_FL_ENABLED and MEDIA_LNK_FL_IMMUTABLE for
  1311. * the new link.
  1312. */
  1313. struct media_link *
  1314. media_create_ancillary_link(struct media_entity *primary,
  1315. struct media_entity *ancillary);
  1316. /**
  1317. * __media_entity_next_link() - Iterate through a &media_entity's links
  1318. *
  1319. * @entity: pointer to the &media_entity
  1320. * @link: pointer to a &media_link to hold the iterated values
  1321. * @link_type: one of the MEDIA_LNK_FL_LINK_TYPE flags
  1322. *
  1323. * Return the next link against an entity matching a specific link type. This
  1324. * allows iteration through an entity's links whilst guaranteeing all of the
  1325. * returned links are of the given type.
  1326. */
  1327. struct media_link *__media_entity_next_link(struct media_entity *entity,
  1328. struct media_link *link,
  1329. unsigned long link_type);
  1330. /**
  1331. * for_each_media_entity_data_link() - Iterate through an entity's data links
  1332. *
  1333. * @entity: pointer to the &media_entity
  1334. * @link: pointer to a &media_link to hold the iterated values
  1335. *
  1336. * Iterate over a &media_entity's data links
  1337. */
  1338. #define for_each_media_entity_data_link(entity, link) \
  1339. for (link = __media_entity_next_link(entity, NULL, \
  1340. MEDIA_LNK_FL_DATA_LINK); \
  1341. link; \
  1342. link = __media_entity_next_link(entity, link, \
  1343. MEDIA_LNK_FL_DATA_LINK))
  1344. #endif