media-entity.h 35 KB

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