media-entity.h 32 KB

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