media-entity.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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. void __media_entity_remove_links(struct media_entity *entity)
  365. {
  366. unsigned int i;
  367. for (i = 0; i < entity->num_links; i++) {
  368. struct media_link *link = &entity->links[i];
  369. struct media_entity *remote;
  370. unsigned int r = 0;
  371. if (link->source->entity == entity)
  372. remote = link->sink->entity;
  373. else
  374. remote = link->source->entity;
  375. while (r < remote->num_links) {
  376. struct media_link *rlink = &remote->links[r];
  377. if (rlink != link->reverse) {
  378. r++;
  379. continue;
  380. }
  381. if (link->source->entity == entity)
  382. remote->num_backlinks--;
  383. if (--remote->num_links == 0)
  384. break;
  385. /* Insert last entry in place of the dropped link. */
  386. *rlink = remote->links[remote->num_links];
  387. }
  388. }
  389. entity->num_links = 0;
  390. entity->num_backlinks = 0;
  391. }
  392. EXPORT_SYMBOL_GPL(__media_entity_remove_links);
  393. void media_entity_remove_links(struct media_entity *entity)
  394. {
  395. /* Do nothing if the entity is not registered. */
  396. if (entity->parent == NULL)
  397. return;
  398. mutex_lock(&entity->parent->graph_mutex);
  399. __media_entity_remove_links(entity);
  400. mutex_unlock(&entity->parent->graph_mutex);
  401. }
  402. EXPORT_SYMBOL_GPL(media_entity_remove_links);
  403. static int __media_entity_setup_link_notify(struct media_link *link, u32 flags)
  404. {
  405. int ret;
  406. /* Notify both entities. */
  407. ret = media_entity_call(link->source->entity, link_setup,
  408. link->source, link->sink, flags);
  409. if (ret < 0 && ret != -ENOIOCTLCMD)
  410. return ret;
  411. ret = media_entity_call(link->sink->entity, link_setup,
  412. link->sink, link->source, flags);
  413. if (ret < 0 && ret != -ENOIOCTLCMD) {
  414. media_entity_call(link->source->entity, link_setup,
  415. link->source, link->sink, link->flags);
  416. return ret;
  417. }
  418. link->flags = flags;
  419. link->reverse->flags = link->flags;
  420. return 0;
  421. }
  422. /**
  423. * __media_entity_setup_link - Configure a media link
  424. * @link: The link being configured
  425. * @flags: Link configuration flags
  426. *
  427. * The bulk of link setup is handled by the two entities connected through the
  428. * link. This function notifies both entities of the link configuration change.
  429. *
  430. * If the link is immutable or if the current and new configuration are
  431. * identical, return immediately.
  432. *
  433. * The user is expected to hold link->source->parent->mutex. If not,
  434. * media_entity_setup_link() should be used instead.
  435. */
  436. int __media_entity_setup_link(struct media_link *link, u32 flags)
  437. {
  438. const u32 mask = MEDIA_LNK_FL_ENABLED;
  439. struct media_device *mdev;
  440. struct media_entity *source, *sink;
  441. int ret = -EBUSY;
  442. if (link == NULL)
  443. return -EINVAL;
  444. /* The non-modifiable link flags must not be modified. */
  445. if ((link->flags & ~mask) != (flags & ~mask))
  446. return -EINVAL;
  447. if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
  448. return link->flags == flags ? 0 : -EINVAL;
  449. if (link->flags == flags)
  450. return 0;
  451. source = link->source->entity;
  452. sink = link->sink->entity;
  453. if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) &&
  454. (source->stream_count || sink->stream_count))
  455. return -EBUSY;
  456. mdev = source->parent;
  457. if (mdev->link_notify) {
  458. ret = mdev->link_notify(link, flags,
  459. MEDIA_DEV_NOTIFY_PRE_LINK_CH);
  460. if (ret < 0)
  461. return ret;
  462. }
  463. ret = __media_entity_setup_link_notify(link, flags);
  464. if (mdev->link_notify)
  465. mdev->link_notify(link, flags, MEDIA_DEV_NOTIFY_POST_LINK_CH);
  466. return ret;
  467. }
  468. int media_entity_setup_link(struct media_link *link, u32 flags)
  469. {
  470. int ret;
  471. mutex_lock(&link->source->entity->parent->graph_mutex);
  472. ret = __media_entity_setup_link(link, flags);
  473. mutex_unlock(&link->source->entity->parent->graph_mutex);
  474. return ret;
  475. }
  476. EXPORT_SYMBOL_GPL(media_entity_setup_link);
  477. /**
  478. * media_entity_find_link - Find a link between two pads
  479. * @source: Source pad
  480. * @sink: Sink pad
  481. *
  482. * Return a pointer to the link between the two entities. If no such link
  483. * exists, return NULL.
  484. */
  485. struct media_link *
  486. media_entity_find_link(struct media_pad *source, struct media_pad *sink)
  487. {
  488. struct media_link *link;
  489. unsigned int i;
  490. for (i = 0; i < source->entity->num_links; ++i) {
  491. link = &source->entity->links[i];
  492. if (link->source->entity == source->entity &&
  493. link->source->index == source->index &&
  494. link->sink->entity == sink->entity &&
  495. link->sink->index == sink->index)
  496. return link;
  497. }
  498. return NULL;
  499. }
  500. EXPORT_SYMBOL_GPL(media_entity_find_link);
  501. /**
  502. * media_entity_remote_pad - Find the pad at the remote end of a link
  503. * @pad: Pad at the local end of the link
  504. *
  505. * Search for a remote pad connected to the given pad by iterating over all
  506. * links originating or terminating at that pad until an enabled link is found.
  507. *
  508. * Return a pointer to the pad at the remote end of the first found enabled
  509. * link, or NULL if no enabled link has been found.
  510. */
  511. struct media_pad *media_entity_remote_pad(struct media_pad *pad)
  512. {
  513. unsigned int i;
  514. for (i = 0; i < pad->entity->num_links; i++) {
  515. struct media_link *link = &pad->entity->links[i];
  516. if (!(link->flags & MEDIA_LNK_FL_ENABLED))
  517. continue;
  518. if (link->source == pad)
  519. return link->sink;
  520. if (link->sink == pad)
  521. return link->source;
  522. }
  523. return NULL;
  524. }
  525. EXPORT_SYMBOL_GPL(media_entity_remote_pad);