media-entity.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. #include <linux/module.h>
  23. #include <linux/slab.h>
  24. #include <media/media-entity.h>
  25. #include <media/media-device.h>
  26. /**
  27. * media_entity_init - Initialize a media entity
  28. *
  29. * @num_pads: Total number of sink and source pads.
  30. * @extra_links: Initial estimate of the number of extra links.
  31. * @pads: Array of 'num_pads' pads.
  32. *
  33. * The total number of pads is an intrinsic property of entities known by the
  34. * entity driver, while the total number of links depends on hardware design
  35. * and is an extrinsic property unknown to the entity driver. However, in most
  36. * use cases the entity driver can guess the number of links which can safely
  37. * be assumed to be equal to or larger than the number of pads.
  38. *
  39. * For those reasons the links array can be preallocated based on the entity
  40. * driver guess and will be reallocated later if extra links need to be
  41. * created.
  42. *
  43. * This function allocates a links array with enough space to hold at least
  44. * 'num_pads' + 'extra_links' elements. The media_entity::max_links field will
  45. * be set to the number of allocated elements.
  46. *
  47. * The pads array is managed by the entity driver and passed to
  48. * media_entity_init() where its pointer will be stored in the entity structure.
  49. */
  50. int
  51. media_entity_init(struct media_entity *entity, u16 num_pads,
  52. struct media_pad *pads, u16 extra_links)
  53. {
  54. struct media_link *links;
  55. unsigned int max_links = num_pads + extra_links;
  56. unsigned int i;
  57. links = kzalloc(max_links * sizeof(links[0]), GFP_KERNEL);
  58. if (links == NULL)
  59. return -ENOMEM;
  60. entity->group_id = 0;
  61. entity->max_links = max_links;
  62. entity->num_links = 0;
  63. entity->num_backlinks = 0;
  64. entity->num_pads = num_pads;
  65. entity->pads = pads;
  66. entity->links = links;
  67. for (i = 0; i < num_pads; i++) {
  68. pads[i].entity = entity;
  69. pads[i].index = i;
  70. }
  71. return 0;
  72. }
  73. EXPORT_SYMBOL_GPL(media_entity_init);
  74. void
  75. media_entity_cleanup(struct media_entity *entity)
  76. {
  77. kfree(entity->links);
  78. }
  79. EXPORT_SYMBOL_GPL(media_entity_cleanup);
  80. /* -----------------------------------------------------------------------------
  81. * Graph traversal
  82. */
  83. static struct media_entity *
  84. media_entity_other(struct media_entity *entity, struct media_link *link)
  85. {
  86. if (link->source->entity == entity)
  87. return link->sink->entity;
  88. else
  89. return link->source->entity;
  90. }
  91. /* push an entity to traversal stack */
  92. static void stack_push(struct media_entity_graph *graph,
  93. struct media_entity *entity)
  94. {
  95. if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) {
  96. WARN_ON(1);
  97. return;
  98. }
  99. graph->top++;
  100. graph->stack[graph->top].link = 0;
  101. graph->stack[graph->top].entity = entity;
  102. }
  103. static struct media_entity *stack_pop(struct media_entity_graph *graph)
  104. {
  105. struct media_entity *entity;
  106. entity = graph->stack[graph->top].entity;
  107. graph->top--;
  108. return entity;
  109. }
  110. #define stack_peek(en) ((en)->stack[(en)->top - 1].entity)
  111. #define link_top(en) ((en)->stack[(en)->top].link)
  112. #define stack_top(en) ((en)->stack[(en)->top].entity)
  113. /**
  114. * media_entity_graph_walk_start - Start walking the media graph at a given entity
  115. * @graph: Media graph structure that will be used to walk the graph
  116. * @entity: Starting entity
  117. *
  118. * This function initializes the graph traversal structure to walk the entities
  119. * graph starting at the given entity. The traversal structure must not be
  120. * modified by the caller during graph traversal. When done the structure can
  121. * safely be freed.
  122. */
  123. void media_entity_graph_walk_start(struct media_entity_graph *graph,
  124. struct media_entity *entity)
  125. {
  126. graph->top = 0;
  127. graph->stack[graph->top].entity = NULL;
  128. stack_push(graph, entity);
  129. }
  130. EXPORT_SYMBOL_GPL(media_entity_graph_walk_start);
  131. /**
  132. * media_entity_graph_walk_next - Get the next entity in the graph
  133. * @graph: Media graph structure
  134. *
  135. * Perform a depth-first traversal of the given media entities graph.
  136. *
  137. * The graph structure must have been previously initialized with a call to
  138. * media_entity_graph_walk_start().
  139. *
  140. * Return the next entity in the graph or NULL if the whole graph have been
  141. * traversed.
  142. */
  143. struct media_entity *
  144. media_entity_graph_walk_next(struct media_entity_graph *graph)
  145. {
  146. if (stack_top(graph) == NULL)
  147. return NULL;
  148. /*
  149. * Depth first search. Push entity to stack and continue from
  150. * top of the stack until no more entities on the level can be
  151. * found.
  152. */
  153. while (link_top(graph) < stack_top(graph)->num_links) {
  154. struct media_entity *entity = stack_top(graph);
  155. struct media_link *link = &entity->links[link_top(graph)];
  156. struct media_entity *next;
  157. /* The link is not enabled so we do not follow. */
  158. if (!(link->flags & MEDIA_LNK_FL_ENABLED)) {
  159. link_top(graph)++;
  160. continue;
  161. }
  162. /* Get the entity in the other end of the link . */
  163. next = media_entity_other(entity, link);
  164. /* Was it the entity we came here from? */
  165. if (next == stack_peek(graph)) {
  166. link_top(graph)++;
  167. continue;
  168. }
  169. /* Push the new entity to stack and start over. */
  170. link_top(graph)++;
  171. stack_push(graph, next);
  172. }
  173. return stack_pop(graph);
  174. }
  175. EXPORT_SYMBOL_GPL(media_entity_graph_walk_next);
  176. /* -----------------------------------------------------------------------------
  177. * Module use count
  178. */
  179. /*
  180. * media_entity_get - Get a reference to the parent module
  181. * @entity: The entity
  182. *
  183. * Get a reference to the parent media device module.
  184. *
  185. * The function will return immediately if @entity is NULL.
  186. *
  187. * Return a pointer to the entity on success or NULL on failure.
  188. */
  189. struct media_entity *media_entity_get(struct media_entity *entity)
  190. {
  191. if (entity == NULL)
  192. return NULL;
  193. if (entity->parent->dev &&
  194. !try_module_get(entity->parent->dev->driver->owner))
  195. return NULL;
  196. return entity;
  197. }
  198. EXPORT_SYMBOL_GPL(media_entity_get);
  199. /*
  200. * media_entity_put - Release the reference to the parent module
  201. * @entity: The entity
  202. *
  203. * Release the reference count acquired by media_entity_get().
  204. *
  205. * The function will return immediately if @entity is NULL.
  206. */
  207. void media_entity_put(struct media_entity *entity)
  208. {
  209. if (entity == NULL)
  210. return;
  211. if (entity->parent->dev)
  212. module_put(entity->parent->dev->driver->owner);
  213. }
  214. EXPORT_SYMBOL_GPL(media_entity_put);
  215. /* -----------------------------------------------------------------------------
  216. * Links management
  217. */
  218. static struct media_link *media_entity_add_link(struct media_entity *entity)
  219. {
  220. if (entity->num_links >= entity->max_links) {
  221. struct media_link *links = entity->links;
  222. unsigned int max_links = entity->max_links + 2;
  223. unsigned int i;
  224. links = krealloc(links, max_links * sizeof(*links), GFP_KERNEL);
  225. if (links == NULL)
  226. return NULL;
  227. for (i = 0; i < entity->num_links; i++)
  228. links[i].reverse->reverse = &links[i];
  229. entity->max_links = max_links;
  230. entity->links = links;
  231. }
  232. return &entity->links[entity->num_links++];
  233. }
  234. int
  235. media_entity_create_link(struct media_entity *source, u16 source_pad,
  236. struct media_entity *sink, u16 sink_pad, u32 flags)
  237. {
  238. struct media_link *link;
  239. struct media_link *backlink;
  240. BUG_ON(source == NULL || sink == NULL);
  241. BUG_ON(source_pad >= source->num_pads);
  242. BUG_ON(sink_pad >= sink->num_pads);
  243. link = media_entity_add_link(source);
  244. if (link == NULL)
  245. return -ENOMEM;
  246. link->source = &source->pads[source_pad];
  247. link->sink = &sink->pads[sink_pad];
  248. link->flags = flags;
  249. /* Create the backlink. Backlinks are used to help graph traversal and
  250. * are not reported to userspace.
  251. */
  252. backlink = media_entity_add_link(sink);
  253. if (backlink == NULL) {
  254. source->num_links--;
  255. return -ENOMEM;
  256. }
  257. backlink->source = &source->pads[source_pad];
  258. backlink->sink = &sink->pads[sink_pad];
  259. backlink->flags = flags;
  260. link->reverse = backlink;
  261. backlink->reverse = link;
  262. sink->num_backlinks++;
  263. return 0;
  264. }
  265. EXPORT_SYMBOL_GPL(media_entity_create_link);
  266. static int __media_entity_setup_link_notify(struct media_link *link, u32 flags)
  267. {
  268. const u32 mask = MEDIA_LNK_FL_ENABLED;
  269. int ret;
  270. /* Notify both entities. */
  271. ret = media_entity_call(link->source->entity, link_setup,
  272. link->source, link->sink, flags);
  273. if (ret < 0 && ret != -ENOIOCTLCMD)
  274. return ret;
  275. ret = media_entity_call(link->sink->entity, link_setup,
  276. link->sink, link->source, flags);
  277. if (ret < 0 && ret != -ENOIOCTLCMD) {
  278. media_entity_call(link->source->entity, link_setup,
  279. link->source, link->sink, link->flags);
  280. return ret;
  281. }
  282. link->flags = (link->flags & ~mask) | (flags & mask);
  283. link->reverse->flags = link->flags;
  284. return 0;
  285. }
  286. /**
  287. * __media_entity_setup_link - Configure a media link
  288. * @link: The link being configured
  289. * @flags: Link configuration flags
  290. *
  291. * The bulk of link setup is handled by the two entities connected through the
  292. * link. This function notifies both entities of the link configuration change.
  293. *
  294. * If the link is immutable or if the current and new configuration are
  295. * identical, return immediately.
  296. *
  297. * The user is expected to hold link->source->parent->mutex. If not,
  298. * media_entity_setup_link() should be used instead.
  299. */
  300. int __media_entity_setup_link(struct media_link *link, u32 flags)
  301. {
  302. struct media_device *mdev;
  303. struct media_entity *source, *sink;
  304. int ret = -EBUSY;
  305. if (link == NULL)
  306. return -EINVAL;
  307. if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
  308. return link->flags == flags ? 0 : -EINVAL;
  309. if (link->flags == flags)
  310. return 0;
  311. source = link->source->entity;
  312. sink = link->sink->entity;
  313. mdev = source->parent;
  314. if ((flags & MEDIA_LNK_FL_ENABLED) && mdev->link_notify) {
  315. ret = mdev->link_notify(link->source, link->sink,
  316. MEDIA_LNK_FL_ENABLED);
  317. if (ret < 0)
  318. return ret;
  319. }
  320. ret = __media_entity_setup_link_notify(link, flags);
  321. if (ret < 0)
  322. goto err;
  323. if (!(flags & MEDIA_LNK_FL_ENABLED) && mdev->link_notify)
  324. mdev->link_notify(link->source, link->sink, 0);
  325. return 0;
  326. err:
  327. if ((flags & MEDIA_LNK_FL_ENABLED) && mdev->link_notify)
  328. mdev->link_notify(link->source, link->sink, 0);
  329. return ret;
  330. }
  331. int media_entity_setup_link(struct media_link *link, u32 flags)
  332. {
  333. int ret;
  334. mutex_lock(&link->source->entity->parent->graph_mutex);
  335. ret = __media_entity_setup_link(link, flags);
  336. mutex_unlock(&link->source->entity->parent->graph_mutex);
  337. return ret;
  338. }
  339. EXPORT_SYMBOL_GPL(media_entity_setup_link);
  340. /**
  341. * media_entity_find_link - Find a link between two pads
  342. * @source: Source pad
  343. * @sink: Sink pad
  344. *
  345. * Return a pointer to the link between the two entities. If no such link
  346. * exists, return NULL.
  347. */
  348. struct media_link *
  349. media_entity_find_link(struct media_pad *source, struct media_pad *sink)
  350. {
  351. struct media_link *link;
  352. unsigned int i;
  353. for (i = 0; i < source->entity->num_links; ++i) {
  354. link = &source->entity->links[i];
  355. if (link->source->entity == source->entity &&
  356. link->source->index == source->index &&
  357. link->sink->entity == sink->entity &&
  358. link->sink->index == sink->index)
  359. return link;
  360. }
  361. return NULL;
  362. }
  363. EXPORT_SYMBOL_GPL(media_entity_find_link);
  364. /**
  365. * media_entity_remote_source - Find the source pad at the remote end of a link
  366. * @pad: Sink pad at the local end of the link
  367. *
  368. * Search for a remote source pad connected to the given sink pad by iterating
  369. * over all links originating or terminating at that pad until an enabled link
  370. * is found.
  371. *
  372. * Return a pointer to the pad at the remote end of the first found enabled
  373. * link, or NULL if no enabled link has been found.
  374. */
  375. struct media_pad *media_entity_remote_source(struct media_pad *pad)
  376. {
  377. unsigned int i;
  378. for (i = 0; i < pad->entity->num_links; i++) {
  379. struct media_link *link = &pad->entity->links[i];
  380. if (!(link->flags & MEDIA_LNK_FL_ENABLED))
  381. continue;
  382. if (link->source == pad)
  383. return link->sink;
  384. if (link->sink == pad)
  385. return link->source;
  386. }
  387. return NULL;
  388. }
  389. EXPORT_SYMBOL_GPL(media_entity_remote_source);