media-entity.h 36 KB

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