media-entity.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  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. #include <linux/bitmap.h>
  19. #include <linux/module.h>
  20. #include <linux/property.h>
  21. #include <linux/slab.h>
  22. #include <media/media-entity.h>
  23. #include <media/media-device.h>
  24. static inline const char *gobj_type(enum media_gobj_type type)
  25. {
  26. switch (type) {
  27. case MEDIA_GRAPH_ENTITY:
  28. return "entity";
  29. case MEDIA_GRAPH_PAD:
  30. return "pad";
  31. case MEDIA_GRAPH_LINK:
  32. return "link";
  33. case MEDIA_GRAPH_INTF_DEVNODE:
  34. return "intf-devnode";
  35. default:
  36. return "unknown";
  37. }
  38. }
  39. static inline const char *intf_type(struct media_interface *intf)
  40. {
  41. switch (intf->type) {
  42. case MEDIA_INTF_T_DVB_FE:
  43. return "dvb-frontend";
  44. case MEDIA_INTF_T_DVB_DEMUX:
  45. return "dvb-demux";
  46. case MEDIA_INTF_T_DVB_DVR:
  47. return "dvb-dvr";
  48. case MEDIA_INTF_T_DVB_CA:
  49. return "dvb-ca";
  50. case MEDIA_INTF_T_DVB_NET:
  51. return "dvb-net";
  52. case MEDIA_INTF_T_V4L_VIDEO:
  53. return "v4l-video";
  54. case MEDIA_INTF_T_V4L_VBI:
  55. return "v4l-vbi";
  56. case MEDIA_INTF_T_V4L_RADIO:
  57. return "v4l-radio";
  58. case MEDIA_INTF_T_V4L_SUBDEV:
  59. return "v4l-subdev";
  60. case MEDIA_INTF_T_V4L_SWRADIO:
  61. return "v4l-swradio";
  62. case MEDIA_INTF_T_V4L_TOUCH:
  63. return "v4l-touch";
  64. default:
  65. return "unknown-intf";
  66. }
  67. };
  68. __must_check int __media_entity_enum_init(struct media_entity_enum *ent_enum,
  69. int idx_max)
  70. {
  71. idx_max = ALIGN(idx_max, BITS_PER_LONG);
  72. ent_enum->bmap = kcalloc(idx_max / BITS_PER_LONG, sizeof(long),
  73. GFP_KERNEL);
  74. if (!ent_enum->bmap)
  75. return -ENOMEM;
  76. bitmap_zero(ent_enum->bmap, idx_max);
  77. ent_enum->idx_max = idx_max;
  78. return 0;
  79. }
  80. EXPORT_SYMBOL_GPL(__media_entity_enum_init);
  81. void media_entity_enum_cleanup(struct media_entity_enum *ent_enum)
  82. {
  83. kfree(ent_enum->bmap);
  84. }
  85. EXPORT_SYMBOL_GPL(media_entity_enum_cleanup);
  86. /**
  87. * dev_dbg_obj - Prints in debug mode a change on some object
  88. *
  89. * @event_name: Name of the event to report. Could be __func__
  90. * @gobj: Pointer to the object
  91. *
  92. * Enabled only if DEBUG or CONFIG_DYNAMIC_DEBUG. Otherwise, it
  93. * won't produce any code.
  94. */
  95. static void dev_dbg_obj(const char *event_name, struct media_gobj *gobj)
  96. {
  97. #if defined(DEBUG) || defined (CONFIG_DYNAMIC_DEBUG)
  98. switch (media_type(gobj)) {
  99. case MEDIA_GRAPH_ENTITY:
  100. dev_dbg(gobj->mdev->dev,
  101. "%s id %u: entity '%s'\n",
  102. event_name, media_id(gobj),
  103. gobj_to_entity(gobj)->name);
  104. break;
  105. case MEDIA_GRAPH_LINK:
  106. {
  107. struct media_link *link = gobj_to_link(gobj);
  108. dev_dbg(gobj->mdev->dev,
  109. "%s id %u: %s link id %u ==> id %u\n",
  110. event_name, media_id(gobj),
  111. media_type(link->gobj0) == MEDIA_GRAPH_PAD ?
  112. "data" : "interface",
  113. media_id(link->gobj0),
  114. media_id(link->gobj1));
  115. break;
  116. }
  117. case MEDIA_GRAPH_PAD:
  118. {
  119. struct media_pad *pad = gobj_to_pad(gobj);
  120. dev_dbg(gobj->mdev->dev,
  121. "%s id %u: %s%spad '%s':%d\n",
  122. event_name, media_id(gobj),
  123. pad->flags & MEDIA_PAD_FL_SINK ? "sink " : "",
  124. pad->flags & MEDIA_PAD_FL_SOURCE ? "source " : "",
  125. pad->entity->name, pad->index);
  126. break;
  127. }
  128. case MEDIA_GRAPH_INTF_DEVNODE:
  129. {
  130. struct media_interface *intf = gobj_to_intf(gobj);
  131. struct media_intf_devnode *devnode = intf_to_devnode(intf);
  132. dev_dbg(gobj->mdev->dev,
  133. "%s id %u: intf_devnode %s - major: %d, minor: %d\n",
  134. event_name, media_id(gobj),
  135. intf_type(intf),
  136. devnode->major, devnode->minor);
  137. break;
  138. }
  139. }
  140. #endif
  141. }
  142. void media_gobj_create(struct media_device *mdev,
  143. enum media_gobj_type type,
  144. struct media_gobj *gobj)
  145. {
  146. BUG_ON(!mdev);
  147. gobj->mdev = mdev;
  148. /* Create a per-type unique object ID */
  149. gobj->id = media_gobj_gen_id(type, ++mdev->id);
  150. switch (type) {
  151. case MEDIA_GRAPH_ENTITY:
  152. list_add_tail(&gobj->list, &mdev->entities);
  153. break;
  154. case MEDIA_GRAPH_PAD:
  155. list_add_tail(&gobj->list, &mdev->pads);
  156. break;
  157. case MEDIA_GRAPH_LINK:
  158. list_add_tail(&gobj->list, &mdev->links);
  159. break;
  160. case MEDIA_GRAPH_INTF_DEVNODE:
  161. list_add_tail(&gobj->list, &mdev->interfaces);
  162. break;
  163. }
  164. mdev->topology_version++;
  165. dev_dbg_obj(__func__, gobj);
  166. }
  167. void media_gobj_destroy(struct media_gobj *gobj)
  168. {
  169. /* Do nothing if the object is not linked. */
  170. if (gobj->mdev == NULL)
  171. return;
  172. dev_dbg_obj(__func__, gobj);
  173. gobj->mdev->topology_version++;
  174. /* Remove the object from mdev list */
  175. list_del(&gobj->list);
  176. gobj->mdev = NULL;
  177. }
  178. /*
  179. * TODO: Get rid of this.
  180. */
  181. #define MEDIA_ENTITY_MAX_PADS 512
  182. int media_entity_pads_init(struct media_entity *entity, u16 num_pads,
  183. struct media_pad *pads)
  184. {
  185. struct media_device *mdev = entity->graph_obj.mdev;
  186. unsigned int i;
  187. if (num_pads >= MEDIA_ENTITY_MAX_PADS)
  188. return -E2BIG;
  189. entity->num_pads = num_pads;
  190. entity->pads = pads;
  191. if (mdev)
  192. mutex_lock(&mdev->graph_mutex);
  193. for (i = 0; i < num_pads; i++) {
  194. pads[i].entity = entity;
  195. pads[i].index = i;
  196. if (mdev)
  197. media_gobj_create(mdev, MEDIA_GRAPH_PAD,
  198. &entity->pads[i].graph_obj);
  199. }
  200. if (mdev)
  201. mutex_unlock(&mdev->graph_mutex);
  202. return 0;
  203. }
  204. EXPORT_SYMBOL_GPL(media_entity_pads_init);
  205. /* -----------------------------------------------------------------------------
  206. * Graph traversal
  207. */
  208. static struct media_entity *
  209. media_entity_other(struct media_entity *entity, struct media_link *link)
  210. {
  211. if (link->source->entity == entity)
  212. return link->sink->entity;
  213. else
  214. return link->source->entity;
  215. }
  216. /* push an entity to traversal stack */
  217. static void stack_push(struct media_graph *graph,
  218. struct media_entity *entity)
  219. {
  220. if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) {
  221. WARN_ON(1);
  222. return;
  223. }
  224. graph->top++;
  225. graph->stack[graph->top].link = entity->links.next;
  226. graph->stack[graph->top].entity = entity;
  227. }
  228. static struct media_entity *stack_pop(struct media_graph *graph)
  229. {
  230. struct media_entity *entity;
  231. entity = graph->stack[graph->top].entity;
  232. graph->top--;
  233. return entity;
  234. }
  235. #define link_top(en) ((en)->stack[(en)->top].link)
  236. #define stack_top(en) ((en)->stack[(en)->top].entity)
  237. /**
  238. * media_graph_walk_init - Allocate resources for graph walk
  239. * @graph: Media graph structure that will be used to walk the graph
  240. * @mdev: Media device
  241. *
  242. * Reserve resources for graph walk in media device's current
  243. * state. The memory must be released using
  244. * media_graph_walk_free().
  245. *
  246. * Returns error on failure, zero on success.
  247. */
  248. __must_check int media_graph_walk_init(
  249. struct media_graph *graph, struct media_device *mdev)
  250. {
  251. return media_entity_enum_init(&graph->ent_enum, mdev);
  252. }
  253. EXPORT_SYMBOL_GPL(media_graph_walk_init);
  254. /**
  255. * media_graph_walk_cleanup - Release resources related to graph walking
  256. * @graph: Media graph structure that was used to walk the graph
  257. */
  258. void media_graph_walk_cleanup(struct media_graph *graph)
  259. {
  260. media_entity_enum_cleanup(&graph->ent_enum);
  261. }
  262. EXPORT_SYMBOL_GPL(media_graph_walk_cleanup);
  263. void media_graph_walk_start(struct media_graph *graph,
  264. struct media_entity *entity)
  265. {
  266. media_entity_enum_zero(&graph->ent_enum);
  267. media_entity_enum_set(&graph->ent_enum, entity);
  268. graph->top = 0;
  269. graph->stack[graph->top].entity = NULL;
  270. stack_push(graph, entity);
  271. dev_dbg(entity->graph_obj.mdev->dev,
  272. "begin graph walk at '%s'\n", entity->name);
  273. }
  274. EXPORT_SYMBOL_GPL(media_graph_walk_start);
  275. static void media_graph_walk_iter(struct media_graph *graph)
  276. {
  277. struct media_entity *entity = stack_top(graph);
  278. struct media_link *link;
  279. struct media_entity *next;
  280. link = list_entry(link_top(graph), typeof(*link), list);
  281. /* The link is not enabled so we do not follow. */
  282. if (!(link->flags & MEDIA_LNK_FL_ENABLED)) {
  283. link_top(graph) = link_top(graph)->next;
  284. dev_dbg(entity->graph_obj.mdev->dev,
  285. "walk: skipping disabled link '%s':%u -> '%s':%u\n",
  286. link->source->entity->name, link->source->index,
  287. link->sink->entity->name, link->sink->index);
  288. return;
  289. }
  290. /* Get the entity in the other end of the link . */
  291. next = media_entity_other(entity, link);
  292. /* Has the entity already been visited? */
  293. if (media_entity_enum_test_and_set(&graph->ent_enum, next)) {
  294. link_top(graph) = link_top(graph)->next;
  295. dev_dbg(entity->graph_obj.mdev->dev,
  296. "walk: skipping entity '%s' (already seen)\n",
  297. next->name);
  298. return;
  299. }
  300. /* Push the new entity to stack and start over. */
  301. link_top(graph) = link_top(graph)->next;
  302. stack_push(graph, next);
  303. dev_dbg(entity->graph_obj.mdev->dev, "walk: pushing '%s' on stack\n",
  304. next->name);
  305. }
  306. struct media_entity *media_graph_walk_next(struct media_graph *graph)
  307. {
  308. struct media_entity *entity;
  309. if (stack_top(graph) == NULL)
  310. return NULL;
  311. /*
  312. * Depth first search. Push entity to stack and continue from
  313. * top of the stack until no more entities on the level can be
  314. * found.
  315. */
  316. while (link_top(graph) != &stack_top(graph)->links)
  317. media_graph_walk_iter(graph);
  318. entity = stack_pop(graph);
  319. dev_dbg(entity->graph_obj.mdev->dev,
  320. "walk: returning entity '%s'\n", entity->name);
  321. return entity;
  322. }
  323. EXPORT_SYMBOL_GPL(media_graph_walk_next);
  324. int media_entity_get_fwnode_pad(struct media_entity *entity,
  325. struct fwnode_handle *fwnode,
  326. unsigned long direction_flags)
  327. {
  328. struct fwnode_endpoint endpoint;
  329. unsigned int i;
  330. int ret;
  331. if (!entity->ops || !entity->ops->get_fwnode_pad) {
  332. for (i = 0; i < entity->num_pads; i++) {
  333. if (entity->pads[i].flags & direction_flags)
  334. return i;
  335. }
  336. return -ENXIO;
  337. }
  338. ret = fwnode_graph_parse_endpoint(fwnode, &endpoint);
  339. if (ret)
  340. return ret;
  341. ret = entity->ops->get_fwnode_pad(&endpoint);
  342. if (ret < 0)
  343. return ret;
  344. if (ret >= entity->num_pads)
  345. return -ENXIO;
  346. if (!(entity->pads[ret].flags & direction_flags))
  347. return -ENXIO;
  348. return ret;
  349. }
  350. EXPORT_SYMBOL_GPL(media_entity_get_fwnode_pad);
  351. /* -----------------------------------------------------------------------------
  352. * Pipeline management
  353. */
  354. __must_check int __media_pipeline_start(struct media_entity *entity,
  355. struct media_pipeline *pipe)
  356. {
  357. struct media_device *mdev = entity->graph_obj.mdev;
  358. struct media_graph *graph = &pipe->graph;
  359. struct media_entity *entity_err = entity;
  360. struct media_link *link;
  361. int ret;
  362. if (!pipe->streaming_count++) {
  363. ret = media_graph_walk_init(&pipe->graph, mdev);
  364. if (ret)
  365. goto error_graph_walk_start;
  366. }
  367. media_graph_walk_start(&pipe->graph, entity);
  368. while ((entity = media_graph_walk_next(graph))) {
  369. DECLARE_BITMAP(active, MEDIA_ENTITY_MAX_PADS);
  370. DECLARE_BITMAP(has_no_links, MEDIA_ENTITY_MAX_PADS);
  371. entity->stream_count++;
  372. if (WARN_ON(entity->pipe && entity->pipe != pipe)) {
  373. ret = -EBUSY;
  374. goto error;
  375. }
  376. entity->pipe = pipe;
  377. /* Already streaming --- no need to check. */
  378. if (entity->stream_count > 1)
  379. continue;
  380. if (!entity->ops || !entity->ops->link_validate)
  381. continue;
  382. bitmap_zero(active, entity->num_pads);
  383. bitmap_fill(has_no_links, entity->num_pads);
  384. list_for_each_entry(link, &entity->links, list) {
  385. struct media_pad *pad = link->sink->entity == entity
  386. ? link->sink : link->source;
  387. /* Mark that a pad is connected by a link. */
  388. bitmap_clear(has_no_links, pad->index, 1);
  389. /*
  390. * Pads that either do not need to connect or
  391. * are connected through an enabled link are
  392. * fine.
  393. */
  394. if (!(pad->flags & MEDIA_PAD_FL_MUST_CONNECT) ||
  395. link->flags & MEDIA_LNK_FL_ENABLED)
  396. bitmap_set(active, pad->index, 1);
  397. /*
  398. * Link validation will only take place for
  399. * sink ends of the link that are enabled.
  400. */
  401. if (link->sink != pad ||
  402. !(link->flags & MEDIA_LNK_FL_ENABLED))
  403. continue;
  404. ret = entity->ops->link_validate(link);
  405. if (ret < 0 && ret != -ENOIOCTLCMD) {
  406. dev_dbg(entity->graph_obj.mdev->dev,
  407. "link validation failed for '%s':%u -> '%s':%u, error %d\n",
  408. link->source->entity->name,
  409. link->source->index,
  410. entity->name, link->sink->index, ret);
  411. goto error;
  412. }
  413. }
  414. /* Either no links or validated links are fine. */
  415. bitmap_or(active, active, has_no_links, entity->num_pads);
  416. if (!bitmap_full(active, entity->num_pads)) {
  417. ret = -ENOLINK;
  418. dev_dbg(entity->graph_obj.mdev->dev,
  419. "'%s':%u must be connected by an enabled link\n",
  420. entity->name,
  421. (unsigned)find_first_zero_bit(
  422. active, entity->num_pads));
  423. goto error;
  424. }
  425. }
  426. return 0;
  427. error:
  428. /*
  429. * Link validation on graph failed. We revert what we did and
  430. * return the error.
  431. */
  432. media_graph_walk_start(graph, entity_err);
  433. while ((entity_err = media_graph_walk_next(graph))) {
  434. /* Sanity check for negative stream_count */
  435. if (!WARN_ON_ONCE(entity_err->stream_count <= 0)) {
  436. entity_err->stream_count--;
  437. if (entity_err->stream_count == 0)
  438. entity_err->pipe = NULL;
  439. }
  440. /*
  441. * We haven't increased stream_count further than this
  442. * so we quit here.
  443. */
  444. if (entity_err == entity)
  445. break;
  446. }
  447. error_graph_walk_start:
  448. if (!--pipe->streaming_count)
  449. media_graph_walk_cleanup(graph);
  450. return ret;
  451. }
  452. EXPORT_SYMBOL_GPL(__media_pipeline_start);
  453. __must_check int media_pipeline_start(struct media_entity *entity,
  454. struct media_pipeline *pipe)
  455. {
  456. struct media_device *mdev = entity->graph_obj.mdev;
  457. int ret;
  458. mutex_lock(&mdev->graph_mutex);
  459. ret = __media_pipeline_start(entity, pipe);
  460. mutex_unlock(&mdev->graph_mutex);
  461. return ret;
  462. }
  463. EXPORT_SYMBOL_GPL(media_pipeline_start);
  464. void __media_pipeline_stop(struct media_entity *entity)
  465. {
  466. struct media_graph *graph = &entity->pipe->graph;
  467. struct media_pipeline *pipe = entity->pipe;
  468. /*
  469. * If the following check fails, the driver has performed an
  470. * unbalanced call to media_pipeline_stop()
  471. */
  472. if (WARN_ON(!pipe))
  473. return;
  474. media_graph_walk_start(graph, entity);
  475. while ((entity = media_graph_walk_next(graph))) {
  476. /* Sanity check for negative stream_count */
  477. if (!WARN_ON_ONCE(entity->stream_count <= 0)) {
  478. entity->stream_count--;
  479. if (entity->stream_count == 0)
  480. entity->pipe = NULL;
  481. }
  482. }
  483. if (!--pipe->streaming_count)
  484. media_graph_walk_cleanup(graph);
  485. }
  486. EXPORT_SYMBOL_GPL(__media_pipeline_stop);
  487. void media_pipeline_stop(struct media_entity *entity)
  488. {
  489. struct media_device *mdev = entity->graph_obj.mdev;
  490. mutex_lock(&mdev->graph_mutex);
  491. __media_pipeline_stop(entity);
  492. mutex_unlock(&mdev->graph_mutex);
  493. }
  494. EXPORT_SYMBOL_GPL(media_pipeline_stop);
  495. /* -----------------------------------------------------------------------------
  496. * Module use count
  497. */
  498. struct media_entity *media_entity_get(struct media_entity *entity)
  499. {
  500. if (entity == NULL)
  501. return NULL;
  502. if (entity->graph_obj.mdev->dev &&
  503. !try_module_get(entity->graph_obj.mdev->dev->driver->owner))
  504. return NULL;
  505. return entity;
  506. }
  507. EXPORT_SYMBOL_GPL(media_entity_get);
  508. void media_entity_put(struct media_entity *entity)
  509. {
  510. if (entity == NULL)
  511. return;
  512. if (entity->graph_obj.mdev->dev)
  513. module_put(entity->graph_obj.mdev->dev->driver->owner);
  514. }
  515. EXPORT_SYMBOL_GPL(media_entity_put);
  516. /* -----------------------------------------------------------------------------
  517. * Links management
  518. */
  519. static struct media_link *media_add_link(struct list_head *head)
  520. {
  521. struct media_link *link;
  522. link = kzalloc(sizeof(*link), GFP_KERNEL);
  523. if (link == NULL)
  524. return NULL;
  525. list_add_tail(&link->list, head);
  526. return link;
  527. }
  528. static void __media_entity_remove_link(struct media_entity *entity,
  529. struct media_link *link)
  530. {
  531. struct media_link *rlink, *tmp;
  532. struct media_entity *remote;
  533. if (link->source->entity == entity)
  534. remote = link->sink->entity;
  535. else
  536. remote = link->source->entity;
  537. list_for_each_entry_safe(rlink, tmp, &remote->links, list) {
  538. if (rlink != link->reverse)
  539. continue;
  540. if (link->source->entity == entity)
  541. remote->num_backlinks--;
  542. /* Remove the remote link */
  543. list_del(&rlink->list);
  544. media_gobj_destroy(&rlink->graph_obj);
  545. kfree(rlink);
  546. if (--remote->num_links == 0)
  547. break;
  548. }
  549. list_del(&link->list);
  550. media_gobj_destroy(&link->graph_obj);
  551. kfree(link);
  552. }
  553. int
  554. media_create_pad_link(struct media_entity *source, u16 source_pad,
  555. struct media_entity *sink, u16 sink_pad, u32 flags)
  556. {
  557. struct media_link *link;
  558. struct media_link *backlink;
  559. BUG_ON(source == NULL || sink == NULL);
  560. BUG_ON(source_pad >= source->num_pads);
  561. BUG_ON(sink_pad >= sink->num_pads);
  562. link = media_add_link(&source->links);
  563. if (link == NULL)
  564. return -ENOMEM;
  565. link->source = &source->pads[source_pad];
  566. link->sink = &sink->pads[sink_pad];
  567. link->flags = flags & ~MEDIA_LNK_FL_INTERFACE_LINK;
  568. /* Initialize graph object embedded at the new link */
  569. media_gobj_create(source->graph_obj.mdev, MEDIA_GRAPH_LINK,
  570. &link->graph_obj);
  571. /* Create the backlink. Backlinks are used to help graph traversal and
  572. * are not reported to userspace.
  573. */
  574. backlink = media_add_link(&sink->links);
  575. if (backlink == NULL) {
  576. __media_entity_remove_link(source, link);
  577. return -ENOMEM;
  578. }
  579. backlink->source = &source->pads[source_pad];
  580. backlink->sink = &sink->pads[sink_pad];
  581. backlink->flags = flags;
  582. backlink->is_backlink = true;
  583. /* Initialize graph object embedded at the new link */
  584. media_gobj_create(sink->graph_obj.mdev, MEDIA_GRAPH_LINK,
  585. &backlink->graph_obj);
  586. link->reverse = backlink;
  587. backlink->reverse = link;
  588. sink->num_backlinks++;
  589. sink->num_links++;
  590. source->num_links++;
  591. return 0;
  592. }
  593. EXPORT_SYMBOL_GPL(media_create_pad_link);
  594. int media_create_pad_links(const struct media_device *mdev,
  595. const u32 source_function,
  596. struct media_entity *source,
  597. const u16 source_pad,
  598. const u32 sink_function,
  599. struct media_entity *sink,
  600. const u16 sink_pad,
  601. u32 flags,
  602. const bool allow_both_undefined)
  603. {
  604. struct media_entity *entity;
  605. unsigned function;
  606. int ret;
  607. /* Trivial case: 1:1 relation */
  608. if (source && sink)
  609. return media_create_pad_link(source, source_pad,
  610. sink, sink_pad, flags);
  611. /* Worse case scenario: n:n relation */
  612. if (!source && !sink) {
  613. if (!allow_both_undefined)
  614. return 0;
  615. media_device_for_each_entity(source, mdev) {
  616. if (source->function != source_function)
  617. continue;
  618. media_device_for_each_entity(sink, mdev) {
  619. if (sink->function != sink_function)
  620. continue;
  621. ret = media_create_pad_link(source, source_pad,
  622. sink, sink_pad,
  623. flags);
  624. if (ret)
  625. return ret;
  626. flags &= ~(MEDIA_LNK_FL_ENABLED |
  627. MEDIA_LNK_FL_IMMUTABLE);
  628. }
  629. }
  630. return 0;
  631. }
  632. /* Handle 1:n and n:1 cases */
  633. if (source)
  634. function = sink_function;
  635. else
  636. function = source_function;
  637. media_device_for_each_entity(entity, mdev) {
  638. if (entity->function != function)
  639. continue;
  640. if (source)
  641. ret = media_create_pad_link(source, source_pad,
  642. entity, sink_pad, flags);
  643. else
  644. ret = media_create_pad_link(entity, source_pad,
  645. sink, sink_pad, flags);
  646. if (ret)
  647. return ret;
  648. flags &= ~(MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE);
  649. }
  650. return 0;
  651. }
  652. EXPORT_SYMBOL_GPL(media_create_pad_links);
  653. void __media_entity_remove_links(struct media_entity *entity)
  654. {
  655. struct media_link *link, *tmp;
  656. list_for_each_entry_safe(link, tmp, &entity->links, list)
  657. __media_entity_remove_link(entity, link);
  658. entity->num_links = 0;
  659. entity->num_backlinks = 0;
  660. }
  661. EXPORT_SYMBOL_GPL(__media_entity_remove_links);
  662. void media_entity_remove_links(struct media_entity *entity)
  663. {
  664. struct media_device *mdev = entity->graph_obj.mdev;
  665. /* Do nothing if the entity is not registered. */
  666. if (mdev == NULL)
  667. return;
  668. mutex_lock(&mdev->graph_mutex);
  669. __media_entity_remove_links(entity);
  670. mutex_unlock(&mdev->graph_mutex);
  671. }
  672. EXPORT_SYMBOL_GPL(media_entity_remove_links);
  673. static int __media_entity_setup_link_notify(struct media_link *link, u32 flags)
  674. {
  675. int ret;
  676. /* Notify both entities. */
  677. ret = media_entity_call(link->source->entity, link_setup,
  678. link->source, link->sink, flags);
  679. if (ret < 0 && ret != -ENOIOCTLCMD)
  680. return ret;
  681. ret = media_entity_call(link->sink->entity, link_setup,
  682. link->sink, link->source, flags);
  683. if (ret < 0 && ret != -ENOIOCTLCMD) {
  684. media_entity_call(link->source->entity, link_setup,
  685. link->source, link->sink, link->flags);
  686. return ret;
  687. }
  688. link->flags = flags;
  689. link->reverse->flags = link->flags;
  690. return 0;
  691. }
  692. int __media_entity_setup_link(struct media_link *link, u32 flags)
  693. {
  694. const u32 mask = MEDIA_LNK_FL_ENABLED;
  695. struct media_device *mdev;
  696. struct media_entity *source, *sink;
  697. int ret = -EBUSY;
  698. if (link == NULL)
  699. return -EINVAL;
  700. /* The non-modifiable link flags must not be modified. */
  701. if ((link->flags & ~mask) != (flags & ~mask))
  702. return -EINVAL;
  703. if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
  704. return link->flags == flags ? 0 : -EINVAL;
  705. if (link->flags == flags)
  706. return 0;
  707. source = link->source->entity;
  708. sink = link->sink->entity;
  709. if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) &&
  710. (source->stream_count || sink->stream_count))
  711. return -EBUSY;
  712. mdev = source->graph_obj.mdev;
  713. if (mdev->ops && mdev->ops->link_notify) {
  714. ret = mdev->ops->link_notify(link, flags,
  715. MEDIA_DEV_NOTIFY_PRE_LINK_CH);
  716. if (ret < 0)
  717. return ret;
  718. }
  719. ret = __media_entity_setup_link_notify(link, flags);
  720. if (mdev->ops && mdev->ops->link_notify)
  721. mdev->ops->link_notify(link, flags,
  722. MEDIA_DEV_NOTIFY_POST_LINK_CH);
  723. return ret;
  724. }
  725. EXPORT_SYMBOL_GPL(__media_entity_setup_link);
  726. int media_entity_setup_link(struct media_link *link, u32 flags)
  727. {
  728. int ret;
  729. mutex_lock(&link->graph_obj.mdev->graph_mutex);
  730. ret = __media_entity_setup_link(link, flags);
  731. mutex_unlock(&link->graph_obj.mdev->graph_mutex);
  732. return ret;
  733. }
  734. EXPORT_SYMBOL_GPL(media_entity_setup_link);
  735. struct media_link *
  736. media_entity_find_link(struct media_pad *source, struct media_pad *sink)
  737. {
  738. struct media_link *link;
  739. list_for_each_entry(link, &source->entity->links, list) {
  740. if (link->source->entity == source->entity &&
  741. link->source->index == source->index &&
  742. link->sink->entity == sink->entity &&
  743. link->sink->index == sink->index)
  744. return link;
  745. }
  746. return NULL;
  747. }
  748. EXPORT_SYMBOL_GPL(media_entity_find_link);
  749. struct media_pad *media_entity_remote_pad(const struct media_pad *pad)
  750. {
  751. struct media_link *link;
  752. list_for_each_entry(link, &pad->entity->links, list) {
  753. if (!(link->flags & MEDIA_LNK_FL_ENABLED))
  754. continue;
  755. if (link->source == pad)
  756. return link->sink;
  757. if (link->sink == pad)
  758. return link->source;
  759. }
  760. return NULL;
  761. }
  762. EXPORT_SYMBOL_GPL(media_entity_remote_pad);
  763. static void media_interface_init(struct media_device *mdev,
  764. struct media_interface *intf,
  765. u32 gobj_type,
  766. u32 intf_type, u32 flags)
  767. {
  768. intf->type = intf_type;
  769. intf->flags = flags;
  770. INIT_LIST_HEAD(&intf->links);
  771. media_gobj_create(mdev, gobj_type, &intf->graph_obj);
  772. }
  773. /* Functions related to the media interface via device nodes */
  774. struct media_intf_devnode *media_devnode_create(struct media_device *mdev,
  775. u32 type, u32 flags,
  776. u32 major, u32 minor)
  777. {
  778. struct media_intf_devnode *devnode;
  779. devnode = kzalloc(sizeof(*devnode), GFP_KERNEL);
  780. if (!devnode)
  781. return NULL;
  782. devnode->major = major;
  783. devnode->minor = minor;
  784. media_interface_init(mdev, &devnode->intf, MEDIA_GRAPH_INTF_DEVNODE,
  785. type, flags);
  786. return devnode;
  787. }
  788. EXPORT_SYMBOL_GPL(media_devnode_create);
  789. void media_devnode_remove(struct media_intf_devnode *devnode)
  790. {
  791. media_remove_intf_links(&devnode->intf);
  792. media_gobj_destroy(&devnode->intf.graph_obj);
  793. kfree(devnode);
  794. }
  795. EXPORT_SYMBOL_GPL(media_devnode_remove);
  796. struct media_link *media_create_intf_link(struct media_entity *entity,
  797. struct media_interface *intf,
  798. u32 flags)
  799. {
  800. struct media_link *link;
  801. link = media_add_link(&intf->links);
  802. if (link == NULL)
  803. return NULL;
  804. link->intf = intf;
  805. link->entity = entity;
  806. link->flags = flags | MEDIA_LNK_FL_INTERFACE_LINK;
  807. /* Initialize graph object embedded at the new link */
  808. media_gobj_create(intf->graph_obj.mdev, MEDIA_GRAPH_LINK,
  809. &link->graph_obj);
  810. return link;
  811. }
  812. EXPORT_SYMBOL_GPL(media_create_intf_link);
  813. void __media_remove_intf_link(struct media_link *link)
  814. {
  815. list_del(&link->list);
  816. media_gobj_destroy(&link->graph_obj);
  817. kfree(link);
  818. }
  819. EXPORT_SYMBOL_GPL(__media_remove_intf_link);
  820. void media_remove_intf_link(struct media_link *link)
  821. {
  822. struct media_device *mdev = link->graph_obj.mdev;
  823. /* Do nothing if the intf is not registered. */
  824. if (mdev == NULL)
  825. return;
  826. mutex_lock(&mdev->graph_mutex);
  827. __media_remove_intf_link(link);
  828. mutex_unlock(&mdev->graph_mutex);
  829. }
  830. EXPORT_SYMBOL_GPL(media_remove_intf_link);
  831. void __media_remove_intf_links(struct media_interface *intf)
  832. {
  833. struct media_link *link, *tmp;
  834. list_for_each_entry_safe(link, tmp, &intf->links, list)
  835. __media_remove_intf_link(link);
  836. }
  837. EXPORT_SYMBOL_GPL(__media_remove_intf_links);
  838. void media_remove_intf_links(struct media_interface *intf)
  839. {
  840. struct media_device *mdev = intf->graph_obj.mdev;
  841. /* Do nothing if the intf is not registered. */
  842. if (mdev == NULL)
  843. return;
  844. mutex_lock(&mdev->graph_mutex);
  845. __media_remove_intf_links(intf);
  846. mutex_unlock(&mdev->graph_mutex);
  847. }
  848. EXPORT_SYMBOL_GPL(media_remove_intf_links);