media-entity.h 30 KB

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