media-entity.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  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. DECLARE_BITMAP(active, MEDIA_ENTITY_MAX_PADS);
  210. DECLARE_BITMAP(has_no_links, MEDIA_ENTITY_MAX_PADS);
  211. unsigned int i;
  212. entity->stream_count++;
  213. WARN_ON(entity->pipe && entity->pipe != pipe);
  214. entity->pipe = pipe;
  215. /* Already streaming --- no need to check. */
  216. if (entity->stream_count > 1)
  217. continue;
  218. if (!entity->ops || !entity->ops->link_validate)
  219. continue;
  220. bitmap_zero(active, entity->num_pads);
  221. bitmap_fill(has_no_links, entity->num_pads);
  222. for (i = 0; i < entity->num_links; i++) {
  223. struct media_link *link = &entity->links[i];
  224. struct media_pad *pad = link->sink->entity == entity
  225. ? link->sink : link->source;
  226. /* Mark that a pad is connected by a link. */
  227. bitmap_clear(has_no_links, pad->index, 1);
  228. /*
  229. * Pads that either do not need to connect or
  230. * are connected through an enabled link are
  231. * fine.
  232. */
  233. if (!(pad->flags & MEDIA_PAD_FL_MUST_CONNECT) ||
  234. link->flags & MEDIA_LNK_FL_ENABLED)
  235. bitmap_set(active, pad->index, 1);
  236. /*
  237. * Link validation will only take place for
  238. * sink ends of the link that are enabled.
  239. */
  240. if (link->sink != pad ||
  241. !(link->flags & MEDIA_LNK_FL_ENABLED))
  242. continue;
  243. ret = entity->ops->link_validate(link);
  244. if (ret < 0 && ret != -ENOIOCTLCMD) {
  245. dev_dbg(entity->parent->dev,
  246. "link validation failed for \"%s\":%u -> \"%s\":%u, error %d\n",
  247. link->source->entity->name,
  248. link->source->index,
  249. entity->name, link->sink->index, ret);
  250. goto error;
  251. }
  252. }
  253. /* Either no links or validated links are fine. */
  254. bitmap_or(active, active, has_no_links, entity->num_pads);
  255. if (!bitmap_full(active, entity->num_pads)) {
  256. ret = -EPIPE;
  257. dev_dbg(entity->parent->dev,
  258. "\"%s\":%u must be connected by an enabled link\n",
  259. entity->name,
  260. (unsigned)find_first_zero_bit(
  261. active, entity->num_pads));
  262. goto error;
  263. }
  264. }
  265. mutex_unlock(&mdev->graph_mutex);
  266. return 0;
  267. error:
  268. /*
  269. * Link validation on graph failed. We revert what we did and
  270. * return the error.
  271. */
  272. media_entity_graph_walk_start(&graph, entity_err);
  273. while ((entity_err = media_entity_graph_walk_next(&graph))) {
  274. entity_err->stream_count--;
  275. if (entity_err->stream_count == 0)
  276. entity_err->pipe = NULL;
  277. /*
  278. * We haven't increased stream_count further than this
  279. * so we quit here.
  280. */
  281. if (entity_err == entity)
  282. break;
  283. }
  284. mutex_unlock(&mdev->graph_mutex);
  285. return ret;
  286. }
  287. EXPORT_SYMBOL_GPL(media_entity_pipeline_start);
  288. /**
  289. * media_entity_pipeline_stop - Mark a pipeline as not streaming
  290. * @entity: Starting entity
  291. *
  292. * Mark all entities connected to a given entity through enabled links, either
  293. * directly or indirectly, as not streaming. The media_entity pipe field is
  294. * reset to NULL.
  295. *
  296. * If multiple calls to media_entity_pipeline_start() have been made, the same
  297. * number of calls to this function are required to mark the pipeline as not
  298. * streaming.
  299. */
  300. void media_entity_pipeline_stop(struct media_entity *entity)
  301. {
  302. struct media_device *mdev = entity->parent;
  303. struct media_entity_graph graph;
  304. mutex_lock(&mdev->graph_mutex);
  305. media_entity_graph_walk_start(&graph, entity);
  306. while ((entity = media_entity_graph_walk_next(&graph))) {
  307. entity->stream_count--;
  308. if (entity->stream_count == 0)
  309. entity->pipe = NULL;
  310. }
  311. mutex_unlock(&mdev->graph_mutex);
  312. }
  313. EXPORT_SYMBOL_GPL(media_entity_pipeline_stop);
  314. /* -----------------------------------------------------------------------------
  315. * Module use count
  316. */
  317. /*
  318. * media_entity_get - Get a reference to the parent module
  319. * @entity: The entity
  320. *
  321. * Get a reference to the parent media device module.
  322. *
  323. * The function will return immediately if @entity is NULL.
  324. *
  325. * Return a pointer to the entity on success or NULL on failure.
  326. */
  327. struct media_entity *media_entity_get(struct media_entity *entity)
  328. {
  329. if (entity == NULL)
  330. return NULL;
  331. if (entity->parent->dev &&
  332. !try_module_get(entity->parent->dev->driver->owner))
  333. return NULL;
  334. return entity;
  335. }
  336. EXPORT_SYMBOL_GPL(media_entity_get);
  337. /*
  338. * media_entity_put - Release the reference to the parent module
  339. * @entity: The entity
  340. *
  341. * Release the reference count acquired by media_entity_get().
  342. *
  343. * The function will return immediately if @entity is NULL.
  344. */
  345. void media_entity_put(struct media_entity *entity)
  346. {
  347. if (entity == NULL)
  348. return;
  349. if (entity->parent->dev)
  350. module_put(entity->parent->dev->driver->owner);
  351. }
  352. EXPORT_SYMBOL_GPL(media_entity_put);
  353. /* -----------------------------------------------------------------------------
  354. * Links management
  355. */
  356. static struct media_link *media_entity_add_link(struct media_entity *entity)
  357. {
  358. if (entity->num_links >= entity->max_links) {
  359. struct media_link *links = entity->links;
  360. unsigned int max_links = entity->max_links + 2;
  361. unsigned int i;
  362. links = krealloc(links, max_links * sizeof(*links), GFP_KERNEL);
  363. if (links == NULL)
  364. return NULL;
  365. for (i = 0; i < entity->num_links; i++)
  366. links[i].reverse->reverse = &links[i];
  367. entity->max_links = max_links;
  368. entity->links = links;
  369. }
  370. return &entity->links[entity->num_links++];
  371. }
  372. int
  373. media_entity_create_link(struct media_entity *source, u16 source_pad,
  374. struct media_entity *sink, u16 sink_pad, u32 flags)
  375. {
  376. struct media_link *link;
  377. struct media_link *backlink;
  378. BUG_ON(source == NULL || sink == NULL);
  379. BUG_ON(source_pad >= source->num_pads);
  380. BUG_ON(sink_pad >= sink->num_pads);
  381. link = media_entity_add_link(source);
  382. if (link == NULL)
  383. return -ENOMEM;
  384. link->source = &source->pads[source_pad];
  385. link->sink = &sink->pads[sink_pad];
  386. link->flags = flags;
  387. /* Create the backlink. Backlinks are used to help graph traversal and
  388. * are not reported to userspace.
  389. */
  390. backlink = media_entity_add_link(sink);
  391. if (backlink == NULL) {
  392. source->num_links--;
  393. return -ENOMEM;
  394. }
  395. backlink->source = &source->pads[source_pad];
  396. backlink->sink = &sink->pads[sink_pad];
  397. backlink->flags = flags;
  398. link->reverse = backlink;
  399. backlink->reverse = link;
  400. sink->num_backlinks++;
  401. return 0;
  402. }
  403. EXPORT_SYMBOL_GPL(media_entity_create_link);
  404. void __media_entity_remove_links(struct media_entity *entity)
  405. {
  406. unsigned int i;
  407. for (i = 0; i < entity->num_links; i++) {
  408. struct media_link *link = &entity->links[i];
  409. struct media_entity *remote;
  410. unsigned int r = 0;
  411. if (link->source->entity == entity)
  412. remote = link->sink->entity;
  413. else
  414. remote = link->source->entity;
  415. while (r < remote->num_links) {
  416. struct media_link *rlink = &remote->links[r];
  417. if (rlink != link->reverse) {
  418. r++;
  419. continue;
  420. }
  421. if (link->source->entity == entity)
  422. remote->num_backlinks--;
  423. if (--remote->num_links == 0)
  424. break;
  425. /* Insert last entry in place of the dropped link. */
  426. *rlink = remote->links[remote->num_links];
  427. }
  428. }
  429. entity->num_links = 0;
  430. entity->num_backlinks = 0;
  431. }
  432. EXPORT_SYMBOL_GPL(__media_entity_remove_links);
  433. void media_entity_remove_links(struct media_entity *entity)
  434. {
  435. /* Do nothing if the entity is not registered. */
  436. if (entity->parent == NULL)
  437. return;
  438. mutex_lock(&entity->parent->graph_mutex);
  439. __media_entity_remove_links(entity);
  440. mutex_unlock(&entity->parent->graph_mutex);
  441. }
  442. EXPORT_SYMBOL_GPL(media_entity_remove_links);
  443. static int __media_entity_setup_link_notify(struct media_link *link, u32 flags)
  444. {
  445. int ret;
  446. /* Notify both entities. */
  447. ret = media_entity_call(link->source->entity, link_setup,
  448. link->source, link->sink, flags);
  449. if (ret < 0 && ret != -ENOIOCTLCMD)
  450. return ret;
  451. ret = media_entity_call(link->sink->entity, link_setup,
  452. link->sink, link->source, flags);
  453. if (ret < 0 && ret != -ENOIOCTLCMD) {
  454. media_entity_call(link->source->entity, link_setup,
  455. link->source, link->sink, link->flags);
  456. return ret;
  457. }
  458. link->flags = flags;
  459. link->reverse->flags = link->flags;
  460. return 0;
  461. }
  462. /**
  463. * __media_entity_setup_link - Configure a media link
  464. * @link: The link being configured
  465. * @flags: Link configuration flags
  466. *
  467. * The bulk of link setup is handled by the two entities connected through the
  468. * link. This function notifies both entities of the link configuration change.
  469. *
  470. * If the link is immutable or if the current and new configuration are
  471. * identical, return immediately.
  472. *
  473. * The user is expected to hold link->source->parent->mutex. If not,
  474. * media_entity_setup_link() should be used instead.
  475. */
  476. int __media_entity_setup_link(struct media_link *link, u32 flags)
  477. {
  478. const u32 mask = MEDIA_LNK_FL_ENABLED;
  479. struct media_device *mdev;
  480. struct media_entity *source, *sink;
  481. int ret = -EBUSY;
  482. if (link == NULL)
  483. return -EINVAL;
  484. /* The non-modifiable link flags must not be modified. */
  485. if ((link->flags & ~mask) != (flags & ~mask))
  486. return -EINVAL;
  487. if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
  488. return link->flags == flags ? 0 : -EINVAL;
  489. if (link->flags == flags)
  490. return 0;
  491. source = link->source->entity;
  492. sink = link->sink->entity;
  493. if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) &&
  494. (source->stream_count || sink->stream_count))
  495. return -EBUSY;
  496. mdev = source->parent;
  497. if (mdev->link_notify) {
  498. ret = mdev->link_notify(link, flags,
  499. MEDIA_DEV_NOTIFY_PRE_LINK_CH);
  500. if (ret < 0)
  501. return ret;
  502. }
  503. ret = __media_entity_setup_link_notify(link, flags);
  504. if (mdev->link_notify)
  505. mdev->link_notify(link, flags, MEDIA_DEV_NOTIFY_POST_LINK_CH);
  506. return ret;
  507. }
  508. int media_entity_setup_link(struct media_link *link, u32 flags)
  509. {
  510. int ret;
  511. mutex_lock(&link->source->entity->parent->graph_mutex);
  512. ret = __media_entity_setup_link(link, flags);
  513. mutex_unlock(&link->source->entity->parent->graph_mutex);
  514. return ret;
  515. }
  516. EXPORT_SYMBOL_GPL(media_entity_setup_link);
  517. /**
  518. * media_entity_find_link - Find a link between two pads
  519. * @source: Source pad
  520. * @sink: Sink pad
  521. *
  522. * Return a pointer to the link between the two entities. If no such link
  523. * exists, return NULL.
  524. */
  525. struct media_link *
  526. media_entity_find_link(struct media_pad *source, struct media_pad *sink)
  527. {
  528. struct media_link *link;
  529. unsigned int i;
  530. for (i = 0; i < source->entity->num_links; ++i) {
  531. link = &source->entity->links[i];
  532. if (link->source->entity == source->entity &&
  533. link->source->index == source->index &&
  534. link->sink->entity == sink->entity &&
  535. link->sink->index == sink->index)
  536. return link;
  537. }
  538. return NULL;
  539. }
  540. EXPORT_SYMBOL_GPL(media_entity_find_link);
  541. /**
  542. * media_entity_remote_pad - Find the pad at the remote end of a link
  543. * @pad: Pad at the local end of the link
  544. *
  545. * Search for a remote pad connected to the given pad by iterating over all
  546. * links originating or terminating at that pad until an enabled link is found.
  547. *
  548. * Return a pointer to the pad at the remote end of the first found enabled
  549. * link, or NULL if no enabled link has been found.
  550. */
  551. struct media_pad *media_entity_remote_pad(struct media_pad *pad)
  552. {
  553. unsigned int i;
  554. for (i = 0; i < pad->entity->num_links; i++) {
  555. struct media_link *link = &pad->entity->links[i];
  556. if (!(link->flags & MEDIA_LNK_FL_ENABLED))
  557. continue;
  558. if (link->source == pad)
  559. return link->sink;
  560. if (link->sink == pad)
  561. return link->source;
  562. }
  563. return NULL;
  564. }
  565. EXPORT_SYMBOL_GPL(media_entity_remote_pad);