media-entity.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. /**
  26. * media_entity_init - Initialize a media entity
  27. *
  28. * @num_pads: Total number of sink and source pads.
  29. * @extra_links: Initial estimate of the number of extra links.
  30. * @pads: Array of 'num_pads' pads.
  31. *
  32. * The total number of pads is an intrinsic property of entities known by the
  33. * entity driver, while the total number of links depends on hardware design
  34. * and is an extrinsic property unknown to the entity driver. However, in most
  35. * use cases the entity driver can guess the number of links which can safely
  36. * be assumed to be equal to or larger than the number of pads.
  37. *
  38. * For those reasons the links array can be preallocated based on the entity
  39. * driver guess and will be reallocated later if extra links need to be
  40. * created.
  41. *
  42. * This function allocates a links array with enough space to hold at least
  43. * 'num_pads' + 'extra_links' elements. The media_entity::max_links field will
  44. * be set to the number of allocated elements.
  45. *
  46. * The pads array is managed by the entity driver and passed to
  47. * media_entity_init() where its pointer will be stored in the entity structure.
  48. */
  49. int
  50. media_entity_init(struct media_entity *entity, u16 num_pads,
  51. struct media_pad *pads, u16 extra_links)
  52. {
  53. struct media_link *links;
  54. unsigned int max_links = num_pads + extra_links;
  55. unsigned int i;
  56. links = kzalloc(max_links * sizeof(links[0]), GFP_KERNEL);
  57. if (links == NULL)
  58. return -ENOMEM;
  59. entity->group_id = 0;
  60. entity->max_links = max_links;
  61. entity->num_links = 0;
  62. entity->num_backlinks = 0;
  63. entity->num_pads = num_pads;
  64. entity->pads = pads;
  65. entity->links = links;
  66. for (i = 0; i < num_pads; i++) {
  67. pads[i].entity = entity;
  68. pads[i].index = i;
  69. }
  70. return 0;
  71. }
  72. EXPORT_SYMBOL_GPL(media_entity_init);
  73. void
  74. media_entity_cleanup(struct media_entity *entity)
  75. {
  76. kfree(entity->links);
  77. }
  78. EXPORT_SYMBOL_GPL(media_entity_cleanup);
  79. /* -----------------------------------------------------------------------------
  80. * Graph traversal
  81. */
  82. static struct media_entity *
  83. media_entity_other(struct media_entity *entity, struct media_link *link)
  84. {
  85. if (link->source->entity == entity)
  86. return link->sink->entity;
  87. else
  88. return link->source->entity;
  89. }
  90. /* push an entity to traversal stack */
  91. static void stack_push(struct media_entity_graph *graph,
  92. struct media_entity *entity)
  93. {
  94. if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) {
  95. WARN_ON(1);
  96. return;
  97. }
  98. graph->top++;
  99. graph->stack[graph->top].link = 0;
  100. graph->stack[graph->top].entity = entity;
  101. }
  102. static struct media_entity *stack_pop(struct media_entity_graph *graph)
  103. {
  104. struct media_entity *entity;
  105. entity = graph->stack[graph->top].entity;
  106. graph->top--;
  107. return entity;
  108. }
  109. #define stack_peek(en) ((en)->stack[(en)->top - 1].entity)
  110. #define link_top(en) ((en)->stack[(en)->top].link)
  111. #define stack_top(en) ((en)->stack[(en)->top].entity)
  112. /**
  113. * media_entity_graph_walk_start - Start walking the media graph at a given entity
  114. * @graph: Media graph structure that will be used to walk the graph
  115. * @entity: Starting entity
  116. *
  117. * This function initializes the graph traversal structure to walk the entities
  118. * graph starting at the given entity. The traversal structure must not be
  119. * modified by the caller during graph traversal. When done the structure can
  120. * safely be freed.
  121. */
  122. void media_entity_graph_walk_start(struct media_entity_graph *graph,
  123. struct media_entity *entity)
  124. {
  125. graph->top = 0;
  126. graph->stack[graph->top].entity = NULL;
  127. stack_push(graph, entity);
  128. }
  129. EXPORT_SYMBOL_GPL(media_entity_graph_walk_start);
  130. /**
  131. * media_entity_graph_walk_next - Get the next entity in the graph
  132. * @graph: Media graph structure
  133. *
  134. * Perform a depth-first traversal of the given media entities graph.
  135. *
  136. * The graph structure must have been previously initialized with a call to
  137. * media_entity_graph_walk_start().
  138. *
  139. * Return the next entity in the graph or NULL if the whole graph have been
  140. * traversed.
  141. */
  142. struct media_entity *
  143. media_entity_graph_walk_next(struct media_entity_graph *graph)
  144. {
  145. if (stack_top(graph) == NULL)
  146. return NULL;
  147. /*
  148. * Depth first search. Push entity to stack and continue from
  149. * top of the stack until no more entities on the level can be
  150. * found.
  151. */
  152. while (link_top(graph) < stack_top(graph)->num_links) {
  153. struct media_entity *entity = stack_top(graph);
  154. struct media_link *link = &entity->links[link_top(graph)];
  155. struct media_entity *next;
  156. /* The link is not enabled so we do not follow. */
  157. if (!(link->flags & MEDIA_LNK_FL_ENABLED)) {
  158. link_top(graph)++;
  159. continue;
  160. }
  161. /* Get the entity in the other end of the link . */
  162. next = media_entity_other(entity, link);
  163. /* Was it the entity we came here from? */
  164. if (next == stack_peek(graph)) {
  165. link_top(graph)++;
  166. continue;
  167. }
  168. /* Push the new entity to stack and start over. */
  169. link_top(graph)++;
  170. stack_push(graph, next);
  171. }
  172. return stack_pop(graph);
  173. }
  174. EXPORT_SYMBOL_GPL(media_entity_graph_walk_next);
  175. /* -----------------------------------------------------------------------------
  176. * Links management
  177. */
  178. static struct media_link *media_entity_add_link(struct media_entity *entity)
  179. {
  180. if (entity->num_links >= entity->max_links) {
  181. struct media_link *links = entity->links;
  182. unsigned int max_links = entity->max_links + 2;
  183. unsigned int i;
  184. links = krealloc(links, max_links * sizeof(*links), GFP_KERNEL);
  185. if (links == NULL)
  186. return NULL;
  187. for (i = 0; i < entity->num_links; i++)
  188. links[i].reverse->reverse = &links[i];
  189. entity->max_links = max_links;
  190. entity->links = links;
  191. }
  192. return &entity->links[entity->num_links++];
  193. }
  194. int
  195. media_entity_create_link(struct media_entity *source, u16 source_pad,
  196. struct media_entity *sink, u16 sink_pad, u32 flags)
  197. {
  198. struct media_link *link;
  199. struct media_link *backlink;
  200. BUG_ON(source == NULL || sink == NULL);
  201. BUG_ON(source_pad >= source->num_pads);
  202. BUG_ON(sink_pad >= sink->num_pads);
  203. link = media_entity_add_link(source);
  204. if (link == NULL)
  205. return -ENOMEM;
  206. link->source = &source->pads[source_pad];
  207. link->sink = &sink->pads[sink_pad];
  208. link->flags = flags;
  209. /* Create the backlink. Backlinks are used to help graph traversal and
  210. * are not reported to userspace.
  211. */
  212. backlink = media_entity_add_link(sink);
  213. if (backlink == NULL) {
  214. source->num_links--;
  215. return -ENOMEM;
  216. }
  217. backlink->source = &source->pads[source_pad];
  218. backlink->sink = &sink->pads[sink_pad];
  219. backlink->flags = flags;
  220. link->reverse = backlink;
  221. backlink->reverse = link;
  222. sink->num_backlinks++;
  223. return 0;
  224. }
  225. EXPORT_SYMBOL_GPL(media_entity_create_link);