media-entity.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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. * Pipeline management
  178. */
  179. /**
  180. * media_entity_pipeline_start - Mark a pipeline as streaming
  181. * @entity: Starting entity
  182. * @pipe: Media pipeline to be assigned to all entities in the pipeline.
  183. *
  184. * Mark all entities connected to a given entity through enabled links, either
  185. * directly or indirectly, as streaming. The given pipeline object is assigned to
  186. * every entity in the pipeline and stored in the media_entity pipe field.
  187. *
  188. * Calls to this function can be nested, in which case the same number of
  189. * media_entity_pipeline_stop() calls will be required to stop streaming. The
  190. * pipeline pointer must be identical for all nested calls to
  191. * media_entity_pipeline_start().
  192. */
  193. void media_entity_pipeline_start(struct media_entity *entity,
  194. struct media_pipeline *pipe)
  195. {
  196. struct media_device *mdev = entity->parent;
  197. struct media_entity_graph graph;
  198. mutex_lock(&mdev->graph_mutex);
  199. media_entity_graph_walk_start(&graph, entity);
  200. while ((entity = media_entity_graph_walk_next(&graph))) {
  201. entity->stream_count++;
  202. WARN_ON(entity->pipe && entity->pipe != pipe);
  203. entity->pipe = pipe;
  204. }
  205. mutex_unlock(&mdev->graph_mutex);
  206. }
  207. EXPORT_SYMBOL_GPL(media_entity_pipeline_start);
  208. /**
  209. * media_entity_pipeline_stop - Mark a pipeline as not streaming
  210. * @entity: Starting entity
  211. *
  212. * Mark all entities connected to a given entity through enabled links, either
  213. * directly or indirectly, as not streaming. The media_entity pipe field is
  214. * reset to NULL.
  215. *
  216. * If multiple calls to media_entity_pipeline_start() have been made, the same
  217. * number of calls to this function are required to mark the pipeline as not
  218. * streaming.
  219. */
  220. void media_entity_pipeline_stop(struct media_entity *entity)
  221. {
  222. struct media_device *mdev = entity->parent;
  223. struct media_entity_graph graph;
  224. mutex_lock(&mdev->graph_mutex);
  225. media_entity_graph_walk_start(&graph, entity);
  226. while ((entity = media_entity_graph_walk_next(&graph))) {
  227. entity->stream_count--;
  228. if (entity->stream_count == 0)
  229. entity->pipe = NULL;
  230. }
  231. mutex_unlock(&mdev->graph_mutex);
  232. }
  233. EXPORT_SYMBOL_GPL(media_entity_pipeline_stop);
  234. /* -----------------------------------------------------------------------------
  235. * Module use count
  236. */
  237. /*
  238. * media_entity_get - Get a reference to the parent module
  239. * @entity: The entity
  240. *
  241. * Get a reference to the parent media device module.
  242. *
  243. * The function will return immediately if @entity is NULL.
  244. *
  245. * Return a pointer to the entity on success or NULL on failure.
  246. */
  247. struct media_entity *media_entity_get(struct media_entity *entity)
  248. {
  249. if (entity == NULL)
  250. return NULL;
  251. if (entity->parent->dev &&
  252. !try_module_get(entity->parent->dev->driver->owner))
  253. return NULL;
  254. return entity;
  255. }
  256. EXPORT_SYMBOL_GPL(media_entity_get);
  257. /*
  258. * media_entity_put - Release the reference to the parent module
  259. * @entity: The entity
  260. *
  261. * Release the reference count acquired by media_entity_get().
  262. *
  263. * The function will return immediately if @entity is NULL.
  264. */
  265. void media_entity_put(struct media_entity *entity)
  266. {
  267. if (entity == NULL)
  268. return;
  269. if (entity->parent->dev)
  270. module_put(entity->parent->dev->driver->owner);
  271. }
  272. EXPORT_SYMBOL_GPL(media_entity_put);
  273. /* -----------------------------------------------------------------------------
  274. * Links management
  275. */
  276. static struct media_link *media_entity_add_link(struct media_entity *entity)
  277. {
  278. if (entity->num_links >= entity->max_links) {
  279. struct media_link *links = entity->links;
  280. unsigned int max_links = entity->max_links + 2;
  281. unsigned int i;
  282. links = krealloc(links, max_links * sizeof(*links), GFP_KERNEL);
  283. if (links == NULL)
  284. return NULL;
  285. for (i = 0; i < entity->num_links; i++)
  286. links[i].reverse->reverse = &links[i];
  287. entity->max_links = max_links;
  288. entity->links = links;
  289. }
  290. return &entity->links[entity->num_links++];
  291. }
  292. int
  293. media_entity_create_link(struct media_entity *source, u16 source_pad,
  294. struct media_entity *sink, u16 sink_pad, u32 flags)
  295. {
  296. struct media_link *link;
  297. struct media_link *backlink;
  298. BUG_ON(source == NULL || sink == NULL);
  299. BUG_ON(source_pad >= source->num_pads);
  300. BUG_ON(sink_pad >= sink->num_pads);
  301. link = media_entity_add_link(source);
  302. if (link == NULL)
  303. return -ENOMEM;
  304. link->source = &source->pads[source_pad];
  305. link->sink = &sink->pads[sink_pad];
  306. link->flags = flags;
  307. /* Create the backlink. Backlinks are used to help graph traversal and
  308. * are not reported to userspace.
  309. */
  310. backlink = media_entity_add_link(sink);
  311. if (backlink == NULL) {
  312. source->num_links--;
  313. return -ENOMEM;
  314. }
  315. backlink->source = &source->pads[source_pad];
  316. backlink->sink = &sink->pads[sink_pad];
  317. backlink->flags = flags;
  318. link->reverse = backlink;
  319. backlink->reverse = link;
  320. sink->num_backlinks++;
  321. return 0;
  322. }
  323. EXPORT_SYMBOL_GPL(media_entity_create_link);
  324. static int __media_entity_setup_link_notify(struct media_link *link, u32 flags)
  325. {
  326. int ret;
  327. /* Notify both entities. */
  328. ret = media_entity_call(link->source->entity, link_setup,
  329. link->source, link->sink, flags);
  330. if (ret < 0 && ret != -ENOIOCTLCMD)
  331. return ret;
  332. ret = media_entity_call(link->sink->entity, link_setup,
  333. link->sink, link->source, flags);
  334. if (ret < 0 && ret != -ENOIOCTLCMD) {
  335. media_entity_call(link->source->entity, link_setup,
  336. link->source, link->sink, link->flags);
  337. return ret;
  338. }
  339. link->flags = flags;
  340. link->reverse->flags = link->flags;
  341. return 0;
  342. }
  343. /**
  344. * __media_entity_setup_link - Configure a media link
  345. * @link: The link being configured
  346. * @flags: Link configuration flags
  347. *
  348. * The bulk of link setup is handled by the two entities connected through the
  349. * link. This function notifies both entities of the link configuration change.
  350. *
  351. * If the link is immutable or if the current and new configuration are
  352. * identical, return immediately.
  353. *
  354. * The user is expected to hold link->source->parent->mutex. If not,
  355. * media_entity_setup_link() should be used instead.
  356. */
  357. int __media_entity_setup_link(struct media_link *link, u32 flags)
  358. {
  359. const u32 mask = MEDIA_LNK_FL_ENABLED;
  360. struct media_device *mdev;
  361. struct media_entity *source, *sink;
  362. int ret = -EBUSY;
  363. if (link == NULL)
  364. return -EINVAL;
  365. /* The non-modifiable link flags must not be modified. */
  366. if ((link->flags & ~mask) != (flags & ~mask))
  367. return -EINVAL;
  368. if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
  369. return link->flags == flags ? 0 : -EINVAL;
  370. if (link->flags == flags)
  371. return 0;
  372. source = link->source->entity;
  373. sink = link->sink->entity;
  374. if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) &&
  375. (source->stream_count || sink->stream_count))
  376. return -EBUSY;
  377. mdev = source->parent;
  378. if ((flags & MEDIA_LNK_FL_ENABLED) && mdev->link_notify) {
  379. ret = mdev->link_notify(link->source, link->sink,
  380. MEDIA_LNK_FL_ENABLED);
  381. if (ret < 0)
  382. return ret;
  383. }
  384. ret = __media_entity_setup_link_notify(link, flags);
  385. if (ret < 0)
  386. goto err;
  387. if (!(flags & MEDIA_LNK_FL_ENABLED) && mdev->link_notify)
  388. mdev->link_notify(link->source, link->sink, 0);
  389. return 0;
  390. err:
  391. if ((flags & MEDIA_LNK_FL_ENABLED) && mdev->link_notify)
  392. mdev->link_notify(link->source, link->sink, 0);
  393. return ret;
  394. }
  395. int media_entity_setup_link(struct media_link *link, u32 flags)
  396. {
  397. int ret;
  398. mutex_lock(&link->source->entity->parent->graph_mutex);
  399. ret = __media_entity_setup_link(link, flags);
  400. mutex_unlock(&link->source->entity->parent->graph_mutex);
  401. return ret;
  402. }
  403. EXPORT_SYMBOL_GPL(media_entity_setup_link);
  404. /**
  405. * media_entity_find_link - Find a link between two pads
  406. * @source: Source pad
  407. * @sink: Sink pad
  408. *
  409. * Return a pointer to the link between the two entities. If no such link
  410. * exists, return NULL.
  411. */
  412. struct media_link *
  413. media_entity_find_link(struct media_pad *source, struct media_pad *sink)
  414. {
  415. struct media_link *link;
  416. unsigned int i;
  417. for (i = 0; i < source->entity->num_links; ++i) {
  418. link = &source->entity->links[i];
  419. if (link->source->entity == source->entity &&
  420. link->source->index == source->index &&
  421. link->sink->entity == sink->entity &&
  422. link->sink->index == sink->index)
  423. return link;
  424. }
  425. return NULL;
  426. }
  427. EXPORT_SYMBOL_GPL(media_entity_find_link);
  428. /**
  429. * media_entity_remote_source - Find the source pad at the remote end of a link
  430. * @pad: Sink pad at the local end of the link
  431. *
  432. * Search for a remote source pad connected to the given sink pad by iterating
  433. * over all links originating or terminating at that pad until an enabled link
  434. * is found.
  435. *
  436. * Return a pointer to the pad at the remote end of the first found enabled
  437. * link, or NULL if no enabled link has been found.
  438. */
  439. struct media_pad *media_entity_remote_source(struct media_pad *pad)
  440. {
  441. unsigned int i;
  442. for (i = 0; i < pad->entity->num_links; i++) {
  443. struct media_link *link = &pad->entity->links[i];
  444. if (!(link->flags & MEDIA_LNK_FL_ENABLED))
  445. continue;
  446. if (link->source == pad)
  447. return link->sink;
  448. if (link->sink == pad)
  449. return link->source;
  450. }
  451. return NULL;
  452. }
  453. EXPORT_SYMBOL_GPL(media_entity_remote_source);