media-entity.c 16 KB

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