media-entity.h 34 KB

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