media-entity.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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/bitmap.h>
  23. #include <linux/module.h>
  24. #include <linux/slab.h>
  25. #include <media/media-entity.h>
  26. #include <media/media-device.h>
  27. /**
  28. * media_entity_init - Initialize a media entity
  29. *
  30. * @num_pads: Total number of sink and source pads.
  31. * @extra_links: Initial estimate of the number of extra links.
  32. * @pads: Array of 'num_pads' pads.
  33. *
  34. * The total number of pads is an intrinsic property of entities known by the
  35. * entity driver, while the total number of links depends on hardware design
  36. * and is an extrinsic property unknown to the entity driver. However, in most
  37. * use cases the entity driver can guess the number of links which can safely
  38. * be assumed to be equal to or larger than the number of pads.
  39. *
  40. * For those reasons the links array can be preallocated based on the entity
  41. * driver guess and will be reallocated later if extra links need to be
  42. * created.
  43. *
  44. * This function allocates a links array with enough space to hold at least
  45. * 'num_pads' + 'extra_links' elements. The media_entity::max_links field will
  46. * be set to the number of allocated elements.
  47. *
  48. * The pads array is managed by the entity driver and passed to
  49. * media_entity_init() where its pointer will be stored in the entity structure.
  50. */
  51. int
  52. media_entity_init(struct media_entity *entity, u16 num_pads,
  53. struct media_pad *pads, u16 extra_links)
  54. {
  55. struct media_link *links;
  56. unsigned int max_links = num_pads + extra_links;
  57. unsigned int i;
  58. links = kzalloc(max_links * sizeof(links[0]), GFP_KERNEL);
  59. if (links == NULL)
  60. return -ENOMEM;
  61. entity->group_id = 0;
  62. entity->max_links = max_links;
  63. entity->num_links = 0;
  64. entity->num_backlinks = 0;
  65. entity->num_pads = num_pads;
  66. entity->pads = pads;
  67. entity->links = links;
  68. for (i = 0; i < num_pads; i++) {
  69. pads[i].entity = entity;
  70. pads[i].index = i;
  71. }
  72. return 0;
  73. }
  74. EXPORT_SYMBOL_GPL(media_entity_init);
  75. void
  76. media_entity_cleanup(struct media_entity *entity)
  77. {
  78. kfree(entity->links);
  79. }
  80. EXPORT_SYMBOL_GPL(media_entity_cleanup);
  81. /* -----------------------------------------------------------------------------
  82. * Graph traversal
  83. */
  84. static struct media_entity *
  85. media_entity_other(struct media_entity *entity, struct media_link *link)
  86. {
  87. if (link->source->entity == entity)
  88. return link->sink->entity;
  89. else
  90. return link->source->entity;
  91. }
  92. /* push an entity to traversal stack */
  93. static void stack_push(struct media_entity_graph *graph,
  94. struct media_entity *entity)
  95. {
  96. if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) {
  97. WARN_ON(1);
  98. return;
  99. }
  100. graph->top++;
  101. graph->stack[graph->top].link = 0;
  102. graph->stack[graph->top].entity = entity;
  103. }
  104. static struct media_entity *stack_pop(struct media_entity_graph *graph)
  105. {
  106. struct media_entity *entity;
  107. entity = graph->stack[graph->top].entity;
  108. graph->top--;
  109. return entity;
  110. }
  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. bitmap_zero(graph->entities, MEDIA_ENTITY_ENUM_MAX_ID);
  129. if (WARN_ON(entity->id >= MEDIA_ENTITY_ENUM_MAX_ID))
  130. return;
  131. __set_bit(entity->id, graph->entities);
  132. stack_push(graph, entity);
  133. }
  134. EXPORT_SYMBOL_GPL(media_entity_graph_walk_start);
  135. /**
  136. * media_entity_graph_walk_next - Get the next entity in the graph
  137. * @graph: Media graph structure
  138. *
  139. * Perform a depth-first traversal of the given media entities graph.
  140. *
  141. * The graph structure must have been previously initialized with a call to
  142. * media_entity_graph_walk_start().
  143. *
  144. * Return the next entity in the graph or NULL if the whole graph have been
  145. * traversed.
  146. */
  147. struct media_entity *
  148. media_entity_graph_walk_next(struct media_entity_graph *graph)
  149. {
  150. if (stack_top(graph) == NULL)
  151. return NULL;
  152. /*
  153. * Depth first search. Push entity to stack and continue from
  154. * top of the stack until no more entities on the level can be
  155. * found.
  156. */
  157. while (link_top(graph) < stack_top(graph)->num_links) {
  158. struct media_entity *entity = stack_top(graph);
  159. struct media_link *link = &entity->links[link_top(graph)];
  160. struct media_entity *next;
  161. /* The link is not enabled so we do not follow. */
  162. if (!(link->flags & MEDIA_LNK_FL_ENABLED)) {
  163. link_top(graph)++;
  164. continue;
  165. }
  166. /* Get the entity in the other end of the link . */
  167. next = media_entity_other(entity, link);
  168. if (WARN_ON(next->id >= MEDIA_ENTITY_ENUM_MAX_ID))
  169. return NULL;
  170. /* Has the entity already been visited? */
  171. if (__test_and_set_bit(next->id, graph->entities)) {
  172. link_top(graph)++;
  173. continue;
  174. }
  175. /* Push the new entity to stack and start over. */
  176. link_top(graph)++;
  177. stack_push(graph, next);
  178. }
  179. return stack_pop(graph);
  180. }
  181. EXPORT_SYMBOL_GPL(media_entity_graph_walk_next);
  182. /* -----------------------------------------------------------------------------
  183. * Pipeline management
  184. */
  185. /**
  186. * media_entity_pipeline_start - Mark a pipeline as streaming
  187. * @entity: Starting entity
  188. * @pipe: Media pipeline to be assigned to all entities in the pipeline.
  189. *
  190. * Mark all entities connected to a given entity through enabled links, either
  191. * directly or indirectly, as streaming. The given pipeline object is assigned to
  192. * every entity in the pipeline and stored in the media_entity pipe field.
  193. *
  194. * Calls to this function can be nested, in which case the same number of
  195. * media_entity_pipeline_stop() calls will be required to stop streaming. The
  196. * pipeline pointer must be identical for all nested calls to
  197. * media_entity_pipeline_start().
  198. */
  199. __must_check int media_entity_pipeline_start(struct media_entity *entity,
  200. struct media_pipeline *pipe)
  201. {
  202. struct media_device *mdev = entity->parent;
  203. struct media_entity_graph graph;
  204. struct media_entity *entity_err = entity;
  205. int ret;
  206. mutex_lock(&mdev->graph_mutex);
  207. media_entity_graph_walk_start(&graph, entity);
  208. while ((entity = media_entity_graph_walk_next(&graph))) {
  209. unsigned int i;
  210. entity->stream_count++;
  211. WARN_ON(entity->pipe && entity->pipe != pipe);
  212. entity->pipe = pipe;
  213. /* Already streaming --- no need to check. */
  214. if (entity->stream_count > 1)
  215. continue;
  216. if (!entity->ops || !entity->ops->link_validate)
  217. continue;
  218. for (i = 0; i < entity->num_links; i++) {
  219. struct media_link *link = &entity->links[i];
  220. /* Is this pad part of an enabled link? */
  221. if (!(link->flags & MEDIA_LNK_FL_ENABLED))
  222. continue;
  223. /* Are we the sink or not? */
  224. if (link->sink->entity != entity)
  225. continue;
  226. ret = entity->ops->link_validate(link);
  227. if (ret < 0 && ret != -ENOIOCTLCMD)
  228. goto error;
  229. }
  230. }
  231. mutex_unlock(&mdev->graph_mutex);
  232. return 0;
  233. error:
  234. /*
  235. * Link validation on graph failed. We revert what we did and
  236. * return the error.
  237. */
  238. media_entity_graph_walk_start(&graph, entity_err);
  239. while ((entity_err = media_entity_graph_walk_next(&graph))) {
  240. entity_err->stream_count--;
  241. if (entity_err->stream_count == 0)
  242. entity_err->pipe = NULL;
  243. /*
  244. * We haven't increased stream_count further than this
  245. * so we quit here.
  246. */
  247. if (entity_err == entity)
  248. break;
  249. }
  250. mutex_unlock(&mdev->graph_mutex);
  251. return ret;
  252. }
  253. EXPORT_SYMBOL_GPL(media_entity_pipeline_start);
  254. /**
  255. * media_entity_pipeline_stop - Mark a pipeline as not streaming
  256. * @entity: Starting entity
  257. *
  258. * Mark all entities connected to a given entity through enabled links, either
  259. * directly or indirectly, as not streaming. The media_entity pipe field is
  260. * reset to NULL.
  261. *
  262. * If multiple calls to media_entity_pipeline_start() have been made, the same
  263. * number of calls to this function are required to mark the pipeline as not
  264. * streaming.
  265. */
  266. void media_entity_pipeline_stop(struct media_entity *entity)
  267. {
  268. struct media_device *mdev = entity->parent;
  269. struct media_entity_graph graph;
  270. mutex_lock(&mdev->graph_mutex);
  271. media_entity_graph_walk_start(&graph, entity);
  272. while ((entity = media_entity_graph_walk_next(&graph))) {
  273. entity->stream_count--;
  274. if (entity->stream_count == 0)
  275. entity->pipe = NULL;
  276. }
  277. mutex_unlock(&mdev->graph_mutex);
  278. }
  279. EXPORT_SYMBOL_GPL(media_entity_pipeline_stop);
  280. /* -----------------------------------------------------------------------------
  281. * Module use count
  282. */
  283. /*
  284. * media_entity_get - Get a reference to the parent module
  285. * @entity: The entity
  286. *
  287. * Get a reference to the parent media device module.
  288. *
  289. * The function will return immediately if @entity is NULL.
  290. *
  291. * Return a pointer to the entity on success or NULL on failure.
  292. */
  293. struct media_entity *media_entity_get(struct media_entity *entity)
  294. {
  295. if (entity == NULL)
  296. return NULL;
  297. if (entity->parent->dev &&
  298. !try_module_get(entity->parent->dev->driver->owner))
  299. return NULL;
  300. return entity;
  301. }
  302. EXPORT_SYMBOL_GPL(media_entity_get);
  303. /*
  304. * media_entity_put - Release the reference to the parent module
  305. * @entity: The entity
  306. *
  307. * Release the reference count acquired by media_entity_get().
  308. *
  309. * The function will return immediately if @entity is NULL.
  310. */
  311. void media_entity_put(struct media_entity *entity)
  312. {
  313. if (entity == NULL)
  314. return;
  315. if (entity->parent->dev)
  316. module_put(entity->parent->dev->driver->owner);
  317. }
  318. EXPORT_SYMBOL_GPL(media_entity_put);
  319. /* -----------------------------------------------------------------------------
  320. * Links management
  321. */
  322. static struct media_link *media_entity_add_link(struct media_entity *entity)
  323. {
  324. if (entity->num_links >= entity->max_links) {
  325. struct media_link *links = entity->links;
  326. unsigned int max_links = entity->max_links + 2;
  327. unsigned int i;
  328. links = krealloc(links, max_links * sizeof(*links), GFP_KERNEL);
  329. if (links == NULL)
  330. return NULL;
  331. for (i = 0; i < entity->num_links; i++)
  332. links[i].reverse->reverse = &links[i];
  333. entity->max_links = max_links;
  334. entity->links = links;
  335. }
  336. return &entity->links[entity->num_links++];
  337. }
  338. int
  339. media_entity_create_link(struct media_entity *source, u16 source_pad,
  340. struct media_entity *sink, u16 sink_pad, u32 flags)
  341. {
  342. struct media_link *link;
  343. struct media_link *backlink;
  344. BUG_ON(source == NULL || sink == NULL);
  345. BUG_ON(source_pad >= source->num_pads);
  346. BUG_ON(sink_pad >= sink->num_pads);
  347. link = media_entity_add_link(source);
  348. if (link == NULL)
  349. return -ENOMEM;
  350. link->source = &source->pads[source_pad];
  351. link->sink = &sink->pads[sink_pad];
  352. link->flags = flags;
  353. /* Create the backlink. Backlinks are used to help graph traversal and
  354. * are not reported to userspace.
  355. */
  356. backlink = media_entity_add_link(sink);
  357. if (backlink == NULL) {
  358. source->num_links--;
  359. return -ENOMEM;
  360. }
  361. backlink->source = &source->pads[source_pad];
  362. backlink->sink = &sink->pads[sink_pad];
  363. backlink->flags = flags;
  364. link->reverse = backlink;
  365. backlink->reverse = link;
  366. sink->num_backlinks++;
  367. return 0;
  368. }
  369. EXPORT_SYMBOL_GPL(media_entity_create_link);
  370. void __media_entity_remove_links(struct media_entity *entity)
  371. {
  372. unsigned int i;
  373. for (i = 0; i < entity->num_links; i++) {
  374. struct media_link *link = &entity->links[i];
  375. struct media_entity *remote;
  376. unsigned int r = 0;
  377. if (link->source->entity == entity)
  378. remote = link->sink->entity;
  379. else
  380. remote = link->source->entity;
  381. while (r < remote->num_links) {
  382. struct media_link *rlink = &remote->links[r];
  383. if (rlink != link->reverse) {
  384. r++;
  385. continue;
  386. }
  387. if (link->source->entity == entity)
  388. remote->num_backlinks--;
  389. if (--remote->num_links == 0)
  390. break;
  391. /* Insert last entry in place of the dropped link. */
  392. *rlink = remote->links[remote->num_links];
  393. }
  394. }
  395. entity->num_links = 0;
  396. entity->num_backlinks = 0;
  397. }
  398. EXPORT_SYMBOL_GPL(__media_entity_remove_links);
  399. void media_entity_remove_links(struct media_entity *entity)
  400. {
  401. /* Do nothing if the entity is not registered. */
  402. if (entity->parent == NULL)
  403. return;
  404. mutex_lock(&entity->parent->graph_mutex);
  405. __media_entity_remove_links(entity);
  406. mutex_unlock(&entity->parent->graph_mutex);
  407. }
  408. EXPORT_SYMBOL_GPL(media_entity_remove_links);
  409. static int __media_entity_setup_link_notify(struct media_link *link, u32 flags)
  410. {
  411. int ret;
  412. /* Notify both entities. */
  413. ret = media_entity_call(link->source->entity, link_setup,
  414. link->source, link->sink, flags);
  415. if (ret < 0 && ret != -ENOIOCTLCMD)
  416. return ret;
  417. ret = media_entity_call(link->sink->entity, link_setup,
  418. link->sink, link->source, flags);
  419. if (ret < 0 && ret != -ENOIOCTLCMD) {
  420. media_entity_call(link->source->entity, link_setup,
  421. link->source, link->sink, link->flags);
  422. return ret;
  423. }
  424. link->flags = flags;
  425. link->reverse->flags = link->flags;
  426. return 0;
  427. }
  428. /**
  429. * __media_entity_setup_link - Configure a media link
  430. * @link: The link being configured
  431. * @flags: Link configuration flags
  432. *
  433. * The bulk of link setup is handled by the two entities connected through the
  434. * link. This function notifies both entities of the link configuration change.
  435. *
  436. * If the link is immutable or if the current and new configuration are
  437. * identical, return immediately.
  438. *
  439. * The user is expected to hold link->source->parent->mutex. If not,
  440. * media_entity_setup_link() should be used instead.
  441. */
  442. int __media_entity_setup_link(struct media_link *link, u32 flags)
  443. {
  444. const u32 mask = MEDIA_LNK_FL_ENABLED;
  445. struct media_device *mdev;
  446. struct media_entity *source, *sink;
  447. int ret = -EBUSY;
  448. if (link == NULL)
  449. return -EINVAL;
  450. /* The non-modifiable link flags must not be modified. */
  451. if ((link->flags & ~mask) != (flags & ~mask))
  452. return -EINVAL;
  453. if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
  454. return link->flags == flags ? 0 : -EINVAL;
  455. if (link->flags == flags)
  456. return 0;
  457. source = link->source->entity;
  458. sink = link->sink->entity;
  459. if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) &&
  460. (source->stream_count || sink->stream_count))
  461. return -EBUSY;
  462. mdev = source->parent;
  463. if (mdev->link_notify) {
  464. ret = mdev->link_notify(link, flags,
  465. MEDIA_DEV_NOTIFY_PRE_LINK_CH);
  466. if (ret < 0)
  467. return ret;
  468. }
  469. ret = __media_entity_setup_link_notify(link, flags);
  470. if (mdev->link_notify)
  471. mdev->link_notify(link, flags, MEDIA_DEV_NOTIFY_POST_LINK_CH);
  472. return ret;
  473. }
  474. int media_entity_setup_link(struct media_link *link, u32 flags)
  475. {
  476. int ret;
  477. mutex_lock(&link->source->entity->parent->graph_mutex);
  478. ret = __media_entity_setup_link(link, flags);
  479. mutex_unlock(&link->source->entity->parent->graph_mutex);
  480. return ret;
  481. }
  482. EXPORT_SYMBOL_GPL(media_entity_setup_link);
  483. /**
  484. * media_entity_find_link - Find a link between two pads
  485. * @source: Source pad
  486. * @sink: Sink pad
  487. *
  488. * Return a pointer to the link between the two entities. If no such link
  489. * exists, return NULL.
  490. */
  491. struct media_link *
  492. media_entity_find_link(struct media_pad *source, struct media_pad *sink)
  493. {
  494. struct media_link *link;
  495. unsigned int i;
  496. for (i = 0; i < source->entity->num_links; ++i) {
  497. link = &source->entity->links[i];
  498. if (link->source->entity == source->entity &&
  499. link->source->index == source->index &&
  500. link->sink->entity == sink->entity &&
  501. link->sink->index == sink->index)
  502. return link;
  503. }
  504. return NULL;
  505. }
  506. EXPORT_SYMBOL_GPL(media_entity_find_link);
  507. /**
  508. * media_entity_remote_pad - Find the pad at the remote end of a link
  509. * @pad: Pad at the local end of the link
  510. *
  511. * Search for a remote pad connected to the given pad by iterating over all
  512. * links originating or terminating at that pad until an enabled link is found.
  513. *
  514. * Return a pointer to the pad at the remote end of the first found enabled
  515. * link, or NULL if no enabled link has been found.
  516. */
  517. struct media_pad *media_entity_remote_pad(struct media_pad *pad)
  518. {
  519. unsigned int i;
  520. for (i = 0; i < pad->entity->num_links; i++) {
  521. struct media_link *link = &pad->entity->links[i];
  522. if (!(link->flags & MEDIA_LNK_FL_ENABLED))
  523. continue;
  524. if (link->source == pad)
  525. return link->sink;
  526. if (link->sink == pad)
  527. return link->source;
  528. }
  529. return NULL;
  530. }
  531. EXPORT_SYMBOL_GPL(media_entity_remote_pad);