mc-entity.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Media entity
  4. *
  5. * Copyright (C) 2010 Nokia Corporation
  6. *
  7. * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  8. * Sakari Ailus <sakari.ailus@iki.fi>
  9. */
  10. #include <linux/bitmap.h>
  11. #include <linux/property.h>
  12. #include <linux/slab.h>
  13. #include <media/media-entity.h>
  14. #include <media/media-device.h>
  15. static inline const char *gobj_type(enum media_gobj_type type)
  16. {
  17. switch (type) {
  18. case MEDIA_GRAPH_ENTITY:
  19. return "entity";
  20. case MEDIA_GRAPH_PAD:
  21. return "pad";
  22. case MEDIA_GRAPH_LINK:
  23. return "link";
  24. case MEDIA_GRAPH_INTF_DEVNODE:
  25. return "intf-devnode";
  26. default:
  27. return "unknown";
  28. }
  29. }
  30. static inline const char *intf_type(struct media_interface *intf)
  31. {
  32. switch (intf->type) {
  33. case MEDIA_INTF_T_DVB_FE:
  34. return "dvb-frontend";
  35. case MEDIA_INTF_T_DVB_DEMUX:
  36. return "dvb-demux";
  37. case MEDIA_INTF_T_DVB_DVR:
  38. return "dvb-dvr";
  39. case MEDIA_INTF_T_DVB_CA:
  40. return "dvb-ca";
  41. case MEDIA_INTF_T_DVB_NET:
  42. return "dvb-net";
  43. case MEDIA_INTF_T_V4L_VIDEO:
  44. return "v4l-video";
  45. case MEDIA_INTF_T_V4L_VBI:
  46. return "v4l-vbi";
  47. case MEDIA_INTF_T_V4L_RADIO:
  48. return "v4l-radio";
  49. case MEDIA_INTF_T_V4L_SUBDEV:
  50. return "v4l-subdev";
  51. case MEDIA_INTF_T_V4L_SWRADIO:
  52. return "v4l-swradio";
  53. case MEDIA_INTF_T_V4L_TOUCH:
  54. return "v4l-touch";
  55. default:
  56. return "unknown-intf";
  57. }
  58. };
  59. __must_check int __media_entity_enum_init(struct media_entity_enum *ent_enum,
  60. int idx_max)
  61. {
  62. idx_max = ALIGN(idx_max, BITS_PER_LONG);
  63. ent_enum->bmap = kcalloc(idx_max / BITS_PER_LONG, sizeof(long),
  64. GFP_KERNEL);
  65. if (!ent_enum->bmap)
  66. return -ENOMEM;
  67. bitmap_zero(ent_enum->bmap, idx_max);
  68. ent_enum->idx_max = idx_max;
  69. return 0;
  70. }
  71. EXPORT_SYMBOL_GPL(__media_entity_enum_init);
  72. void media_entity_enum_cleanup(struct media_entity_enum *ent_enum)
  73. {
  74. kfree(ent_enum->bmap);
  75. }
  76. EXPORT_SYMBOL_GPL(media_entity_enum_cleanup);
  77. /**
  78. * dev_dbg_obj - Prints in debug mode a change on some object
  79. *
  80. * @event_name: Name of the event to report. Could be __func__
  81. * @gobj: Pointer to the object
  82. *
  83. * Enabled only if DEBUG or CONFIG_DYNAMIC_DEBUG. Otherwise, it
  84. * won't produce any code.
  85. */
  86. static void dev_dbg_obj(const char *event_name, struct media_gobj *gobj)
  87. {
  88. #if defined(DEBUG) || defined (CONFIG_DYNAMIC_DEBUG)
  89. switch (media_type(gobj)) {
  90. case MEDIA_GRAPH_ENTITY:
  91. dev_dbg(gobj->mdev->dev,
  92. "%s id %u: entity '%s'\n",
  93. event_name, media_id(gobj),
  94. gobj_to_entity(gobj)->name);
  95. break;
  96. case MEDIA_GRAPH_LINK:
  97. {
  98. struct media_link *link = gobj_to_link(gobj);
  99. dev_dbg(gobj->mdev->dev,
  100. "%s id %u: %s link id %u ==> id %u\n",
  101. event_name, media_id(gobj),
  102. media_type(link->gobj0) == MEDIA_GRAPH_PAD ?
  103. "data" : "interface",
  104. media_id(link->gobj0),
  105. media_id(link->gobj1));
  106. break;
  107. }
  108. case MEDIA_GRAPH_PAD:
  109. {
  110. struct media_pad *pad = gobj_to_pad(gobj);
  111. dev_dbg(gobj->mdev->dev,
  112. "%s id %u: %s%spad '%s':%d\n",
  113. event_name, media_id(gobj),
  114. pad->flags & MEDIA_PAD_FL_SINK ? "sink " : "",
  115. pad->flags & MEDIA_PAD_FL_SOURCE ? "source " : "",
  116. pad->entity->name, pad->index);
  117. break;
  118. }
  119. case MEDIA_GRAPH_INTF_DEVNODE:
  120. {
  121. struct media_interface *intf = gobj_to_intf(gobj);
  122. struct media_intf_devnode *devnode = intf_to_devnode(intf);
  123. dev_dbg(gobj->mdev->dev,
  124. "%s id %u: intf_devnode %s - major: %d, minor: %d\n",
  125. event_name, media_id(gobj),
  126. intf_type(intf),
  127. devnode->major, devnode->minor);
  128. break;
  129. }
  130. }
  131. #endif
  132. }
  133. void media_gobj_create(struct media_device *mdev,
  134. enum media_gobj_type type,
  135. struct media_gobj *gobj)
  136. {
  137. BUG_ON(!mdev);
  138. gobj->mdev = mdev;
  139. /* Create a per-type unique object ID */
  140. gobj->id = media_gobj_gen_id(type, ++mdev->id);
  141. switch (type) {
  142. case MEDIA_GRAPH_ENTITY:
  143. list_add_tail(&gobj->list, &mdev->entities);
  144. break;
  145. case MEDIA_GRAPH_PAD:
  146. list_add_tail(&gobj->list, &mdev->pads);
  147. break;
  148. case MEDIA_GRAPH_LINK:
  149. list_add_tail(&gobj->list, &mdev->links);
  150. break;
  151. case MEDIA_GRAPH_INTF_DEVNODE:
  152. list_add_tail(&gobj->list, &mdev->interfaces);
  153. break;
  154. }
  155. mdev->topology_version++;
  156. dev_dbg_obj(__func__, gobj);
  157. }
  158. void media_gobj_destroy(struct media_gobj *gobj)
  159. {
  160. /* Do nothing if the object is not linked. */
  161. if (gobj->mdev == NULL)
  162. return;
  163. dev_dbg_obj(__func__, gobj);
  164. gobj->mdev->topology_version++;
  165. /* Remove the object from mdev list */
  166. list_del(&gobj->list);
  167. gobj->mdev = NULL;
  168. }
  169. /*
  170. * TODO: Get rid of this.
  171. */
  172. #define MEDIA_ENTITY_MAX_PADS 512
  173. int media_entity_pads_init(struct media_entity *entity, u16 num_pads,
  174. struct media_pad *pads)
  175. {
  176. struct media_device *mdev = entity->graph_obj.mdev;
  177. unsigned int i;
  178. if (num_pads >= MEDIA_ENTITY_MAX_PADS)
  179. return -E2BIG;
  180. entity->num_pads = num_pads;
  181. entity->pads = pads;
  182. if (mdev)
  183. mutex_lock(&mdev->graph_mutex);
  184. for (i = 0; i < num_pads; i++) {
  185. pads[i].entity = entity;
  186. pads[i].index = i;
  187. if (mdev)
  188. media_gobj_create(mdev, MEDIA_GRAPH_PAD,
  189. &entity->pads[i].graph_obj);
  190. }
  191. if (mdev)
  192. mutex_unlock(&mdev->graph_mutex);
  193. return 0;
  194. }
  195. EXPORT_SYMBOL_GPL(media_entity_pads_init);
  196. /* -----------------------------------------------------------------------------
  197. * Graph traversal
  198. */
  199. static struct media_entity *
  200. media_entity_other(struct media_entity *entity, struct media_link *link)
  201. {
  202. if (link->source->entity == entity)
  203. return link->sink->entity;
  204. else
  205. return link->source->entity;
  206. }
  207. /* push an entity to traversal stack */
  208. static void stack_push(struct media_graph *graph,
  209. struct media_entity *entity)
  210. {
  211. if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) {
  212. WARN_ON(1);
  213. return;
  214. }
  215. graph->top++;
  216. graph->stack[graph->top].link = entity->links.next;
  217. graph->stack[graph->top].entity = entity;
  218. }
  219. static struct media_entity *stack_pop(struct media_graph *graph)
  220. {
  221. struct media_entity *entity;
  222. entity = graph->stack[graph->top].entity;
  223. graph->top--;
  224. return entity;
  225. }
  226. #define link_top(en) ((en)->stack[(en)->top].link)
  227. #define stack_top(en) ((en)->stack[(en)->top].entity)
  228. /**
  229. * media_graph_walk_init - Allocate resources for graph walk
  230. * @graph: Media graph structure that will be used to walk the graph
  231. * @mdev: Media device
  232. *
  233. * Reserve resources for graph walk in media device's current
  234. * state. The memory must be released using
  235. * media_graph_walk_free().
  236. *
  237. * Returns error on failure, zero on success.
  238. */
  239. __must_check int media_graph_walk_init(
  240. struct media_graph *graph, struct media_device *mdev)
  241. {
  242. return media_entity_enum_init(&graph->ent_enum, mdev);
  243. }
  244. EXPORT_SYMBOL_GPL(media_graph_walk_init);
  245. /**
  246. * media_graph_walk_cleanup - Release resources related to graph walking
  247. * @graph: Media graph structure that was used to walk the graph
  248. */
  249. void media_graph_walk_cleanup(struct media_graph *graph)
  250. {
  251. media_entity_enum_cleanup(&graph->ent_enum);
  252. }
  253. EXPORT_SYMBOL_GPL(media_graph_walk_cleanup);
  254. void media_graph_walk_start(struct media_graph *graph,
  255. struct media_entity *entity)
  256. {
  257. media_entity_enum_zero(&graph->ent_enum);
  258. media_entity_enum_set(&graph->ent_enum, entity);
  259. graph->top = 0;
  260. graph->stack[graph->top].entity = NULL;
  261. stack_push(graph, entity);
  262. dev_dbg(entity->graph_obj.mdev->dev,
  263. "begin graph walk at '%s'\n", entity->name);
  264. }
  265. EXPORT_SYMBOL_GPL(media_graph_walk_start);
  266. static void media_graph_walk_iter(struct media_graph *graph)
  267. {
  268. struct media_entity *entity = stack_top(graph);
  269. struct media_link *link;
  270. struct media_entity *next;
  271. link = list_entry(link_top(graph), typeof(*link), list);
  272. /* The link is not enabled so we do not follow. */
  273. if (!(link->flags & MEDIA_LNK_FL_ENABLED)) {
  274. link_top(graph) = link_top(graph)->next;
  275. dev_dbg(entity->graph_obj.mdev->dev,
  276. "walk: skipping disabled link '%s':%u -> '%s':%u\n",
  277. link->source->entity->name, link->source->index,
  278. link->sink->entity->name, link->sink->index);
  279. return;
  280. }
  281. /* Get the entity in the other end of the link . */
  282. next = media_entity_other(entity, link);
  283. /* Has the entity already been visited? */
  284. if (media_entity_enum_test_and_set(&graph->ent_enum, next)) {
  285. link_top(graph) = link_top(graph)->next;
  286. dev_dbg(entity->graph_obj.mdev->dev,
  287. "walk: skipping entity '%s' (already seen)\n",
  288. next->name);
  289. return;
  290. }
  291. /* Push the new entity to stack and start over. */
  292. link_top(graph) = link_top(graph)->next;
  293. stack_push(graph, next);
  294. dev_dbg(entity->graph_obj.mdev->dev, "walk: pushing '%s' on stack\n",
  295. next->name);
  296. }
  297. struct media_entity *media_graph_walk_next(struct media_graph *graph)
  298. {
  299. struct media_entity *entity;
  300. if (stack_top(graph) == NULL)
  301. return NULL;
  302. /*
  303. * Depth first search. Push entity to stack and continue from
  304. * top of the stack until no more entities on the level can be
  305. * found.
  306. */
  307. while (link_top(graph) != &stack_top(graph)->links)
  308. media_graph_walk_iter(graph);
  309. entity = stack_pop(graph);
  310. dev_dbg(entity->graph_obj.mdev->dev,
  311. "walk: returning entity '%s'\n", entity->name);
  312. return entity;
  313. }
  314. EXPORT_SYMBOL_GPL(media_graph_walk_next);
  315. int media_entity_get_fwnode_pad(struct media_entity *entity,
  316. struct fwnode_handle *fwnode,
  317. unsigned long direction_flags)
  318. {
  319. struct fwnode_endpoint endpoint;
  320. unsigned int i;
  321. int ret;
  322. if (!entity->ops || !entity->ops->get_fwnode_pad) {
  323. for (i = 0; i < entity->num_pads; i++) {
  324. if (entity->pads[i].flags & direction_flags)
  325. return i;
  326. }
  327. return -ENXIO;
  328. }
  329. ret = fwnode_graph_parse_endpoint(fwnode, &endpoint);
  330. if (ret)
  331. return ret;
  332. ret = entity->ops->get_fwnode_pad(entity, &endpoint);
  333. if (ret < 0)
  334. return ret;
  335. if (ret >= entity->num_pads)
  336. return -ENXIO;
  337. if (!(entity->pads[ret].flags & direction_flags))
  338. return -ENXIO;
  339. return ret;
  340. }
  341. EXPORT_SYMBOL_GPL(media_entity_get_fwnode_pad);
  342. /* -----------------------------------------------------------------------------
  343. * Pipeline management
  344. */
  345. __must_check int __media_pipeline_start(struct media_entity *entity,
  346. struct media_pipeline *pipe)
  347. {
  348. struct media_device *mdev = entity->graph_obj.mdev;
  349. struct media_graph *graph = &pipe->graph;
  350. struct media_entity *entity_err = entity;
  351. struct media_link *link;
  352. int ret;
  353. if (!pipe->streaming_count++) {
  354. ret = media_graph_walk_init(&pipe->graph, mdev);
  355. if (ret)
  356. goto error_graph_walk_start;
  357. }
  358. media_graph_walk_start(&pipe->graph, entity);
  359. while ((entity = media_graph_walk_next(graph))) {
  360. DECLARE_BITMAP(active, MEDIA_ENTITY_MAX_PADS);
  361. DECLARE_BITMAP(has_no_links, MEDIA_ENTITY_MAX_PADS);
  362. entity->stream_count++;
  363. if (entity->pipe && entity->pipe != pipe) {
  364. pr_err("Pipe active for %s. Can't start for %s\n",
  365. entity->name,
  366. entity_err->name);
  367. ret = -EBUSY;
  368. goto error;
  369. }
  370. entity->pipe = pipe;
  371. /* Already streaming --- no need to check. */
  372. if (entity->stream_count > 1)
  373. continue;
  374. if (!entity->ops || !entity->ops->link_validate)
  375. continue;
  376. bitmap_zero(active, entity->num_pads);
  377. bitmap_fill(has_no_links, entity->num_pads);
  378. list_for_each_entry(link, &entity->links, list) {
  379. struct media_pad *pad = link->sink->entity == entity
  380. ? link->sink : link->source;
  381. /* Mark that a pad is connected by a link. */
  382. bitmap_clear(has_no_links, pad->index, 1);
  383. /*
  384. * Pads that either do not need to connect or
  385. * are connected through an enabled link are
  386. * fine.
  387. */
  388. if (!(pad->flags & MEDIA_PAD_FL_MUST_CONNECT) ||
  389. link->flags & MEDIA_LNK_FL_ENABLED)
  390. bitmap_set(active, pad->index, 1);
  391. /*
  392. * Link validation will only take place for
  393. * sink ends of the link that are enabled.
  394. */
  395. if (link->sink != pad ||
  396. !(link->flags & MEDIA_LNK_FL_ENABLED))
  397. continue;
  398. ret = entity->ops->link_validate(link);
  399. if (ret < 0 && ret != -ENOIOCTLCMD) {
  400. dev_dbg(entity->graph_obj.mdev->dev,
  401. "link validation failed for '%s':%u -> '%s':%u, error %d\n",
  402. link->source->entity->name,
  403. link->source->index,
  404. entity->name, link->sink->index, ret);
  405. goto error;
  406. }
  407. }
  408. /* Either no links or validated links are fine. */
  409. bitmap_or(active, active, has_no_links, entity->num_pads);
  410. if (!bitmap_full(active, entity->num_pads)) {
  411. ret = -ENOLINK;
  412. dev_dbg(entity->graph_obj.mdev->dev,
  413. "'%s':%u must be connected by an enabled link\n",
  414. entity->name,
  415. (unsigned)find_first_zero_bit(
  416. active, entity->num_pads));
  417. goto error;
  418. }
  419. }
  420. return 0;
  421. error:
  422. /*
  423. * Link validation on graph failed. We revert what we did and
  424. * return the error.
  425. */
  426. media_graph_walk_start(graph, entity_err);
  427. while ((entity_err = media_graph_walk_next(graph))) {
  428. /* Sanity check for negative stream_count */
  429. if (!WARN_ON_ONCE(entity_err->stream_count <= 0)) {
  430. entity_err->stream_count--;
  431. if (entity_err->stream_count == 0)
  432. entity_err->pipe = NULL;
  433. }
  434. /*
  435. * We haven't increased stream_count further than this
  436. * so we quit here.
  437. */
  438. if (entity_err == entity)
  439. break;
  440. }
  441. error_graph_walk_start:
  442. if (!--pipe->streaming_count)
  443. media_graph_walk_cleanup(graph);
  444. return ret;
  445. }
  446. EXPORT_SYMBOL_GPL(__media_pipeline_start);
  447. __must_check int media_pipeline_start(struct media_entity *entity,
  448. struct media_pipeline *pipe)
  449. {
  450. struct media_device *mdev = entity->graph_obj.mdev;
  451. int ret;
  452. mutex_lock(&mdev->graph_mutex);
  453. ret = __media_pipeline_start(entity, pipe);
  454. mutex_unlock(&mdev->graph_mutex);
  455. return ret;
  456. }
  457. EXPORT_SYMBOL_GPL(media_pipeline_start);
  458. void __media_pipeline_stop(struct media_entity *entity)
  459. {
  460. struct media_graph *graph = &entity->pipe->graph;
  461. struct media_pipeline *pipe = entity->pipe;
  462. /*
  463. * If the following check fails, the driver has performed an
  464. * unbalanced call to media_pipeline_stop()
  465. */
  466. if (WARN_ON(!pipe))
  467. return;
  468. media_graph_walk_start(graph, entity);
  469. while ((entity = media_graph_walk_next(graph))) {
  470. /* Sanity check for negative stream_count */
  471. if (!WARN_ON_ONCE(entity->stream_count <= 0)) {
  472. entity->stream_count--;
  473. if (entity->stream_count == 0)
  474. entity->pipe = NULL;
  475. }
  476. }
  477. if (!--pipe->streaming_count)
  478. media_graph_walk_cleanup(graph);
  479. }
  480. EXPORT_SYMBOL_GPL(__media_pipeline_stop);
  481. void media_pipeline_stop(struct media_entity *entity)
  482. {
  483. struct media_device *mdev = entity->graph_obj.mdev;
  484. mutex_lock(&mdev->graph_mutex);
  485. __media_pipeline_stop(entity);
  486. mutex_unlock(&mdev->graph_mutex);
  487. }
  488. EXPORT_SYMBOL_GPL(media_pipeline_stop);
  489. /* -----------------------------------------------------------------------------
  490. * Links management
  491. */
  492. static struct media_link *media_add_link(struct list_head *head)
  493. {
  494. struct media_link *link;
  495. link = kzalloc(sizeof(*link), GFP_KERNEL);
  496. if (link == NULL)
  497. return NULL;
  498. list_add_tail(&link->list, head);
  499. return link;
  500. }
  501. static void __media_entity_remove_link(struct media_entity *entity,
  502. struct media_link *link)
  503. {
  504. struct media_link *rlink, *tmp;
  505. struct media_entity *remote;
  506. if (link->source->entity == entity)
  507. remote = link->sink->entity;
  508. else
  509. remote = link->source->entity;
  510. list_for_each_entry_safe(rlink, tmp, &remote->links, list) {
  511. if (rlink != link->reverse)
  512. continue;
  513. if (link->source->entity == entity)
  514. remote->num_backlinks--;
  515. /* Remove the remote link */
  516. list_del(&rlink->list);
  517. media_gobj_destroy(&rlink->graph_obj);
  518. kfree(rlink);
  519. if (--remote->num_links == 0)
  520. break;
  521. }
  522. list_del(&link->list);
  523. media_gobj_destroy(&link->graph_obj);
  524. kfree(link);
  525. }
  526. int media_get_pad_index(struct media_entity *entity, bool is_sink,
  527. enum media_pad_signal_type sig_type)
  528. {
  529. int i;
  530. bool pad_is_sink;
  531. if (!entity)
  532. return -EINVAL;
  533. for (i = 0; i < entity->num_pads; i++) {
  534. if (entity->pads[i].flags & MEDIA_PAD_FL_SINK)
  535. pad_is_sink = true;
  536. else if (entity->pads[i].flags & MEDIA_PAD_FL_SOURCE)
  537. pad_is_sink = false;
  538. else
  539. continue; /* This is an error! */
  540. if (pad_is_sink != is_sink)
  541. continue;
  542. if (entity->pads[i].sig_type == sig_type)
  543. return i;
  544. }
  545. return -EINVAL;
  546. }
  547. EXPORT_SYMBOL_GPL(media_get_pad_index);
  548. int
  549. media_create_pad_link(struct media_entity *source, u16 source_pad,
  550. struct media_entity *sink, u16 sink_pad, u32 flags)
  551. {
  552. struct media_link *link;
  553. struct media_link *backlink;
  554. if (WARN_ON(!source || !sink) ||
  555. WARN_ON(source_pad >= source->num_pads) ||
  556. WARN_ON(sink_pad >= sink->num_pads))
  557. return -EINVAL;
  558. if (WARN_ON(!(source->pads[source_pad].flags & MEDIA_PAD_FL_SOURCE)))
  559. return -EINVAL;
  560. if (WARN_ON(!(sink->pads[sink_pad].flags & MEDIA_PAD_FL_SINK)))
  561. return -EINVAL;
  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);