media-entity.h 33 KB

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