media-entity.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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. static inline const char *gobj_type(enum media_gobj_type type)
  28. {
  29. switch (type) {
  30. case MEDIA_GRAPH_ENTITY:
  31. return "entity";
  32. case MEDIA_GRAPH_PAD:
  33. return "pad";
  34. case MEDIA_GRAPH_LINK:
  35. return "link";
  36. case MEDIA_GRAPH_INTF_DEVNODE:
  37. return "intf-devnode";
  38. default:
  39. return "unknown";
  40. }
  41. }
  42. static inline const char *intf_type(struct media_interface *intf)
  43. {
  44. switch (intf->type) {
  45. case MEDIA_INTF_T_DVB_FE:
  46. return "frontend";
  47. case MEDIA_INTF_T_DVB_DEMUX:
  48. return "demux";
  49. case MEDIA_INTF_T_DVB_DVR:
  50. return "DVR";
  51. case MEDIA_INTF_T_DVB_CA:
  52. return "CA";
  53. case MEDIA_INTF_T_DVB_NET:
  54. return "dvbnet";
  55. case MEDIA_INTF_T_V4L_VIDEO:
  56. return "video";
  57. case MEDIA_INTF_T_V4L_VBI:
  58. return "vbi";
  59. case MEDIA_INTF_T_V4L_RADIO:
  60. return "radio";
  61. case MEDIA_INTF_T_V4L_SUBDEV:
  62. return "v4l2-subdev";
  63. case MEDIA_INTF_T_V4L_SWRADIO:
  64. return "swradio";
  65. default:
  66. return "unknown-intf";
  67. }
  68. };
  69. /**
  70. * dev_dbg_obj - Prints in debug mode a change on some object
  71. *
  72. * @event_name: Name of the event to report. Could be __func__
  73. * @gobj: Pointer to the object
  74. *
  75. * Enabled only if DEBUG or CONFIG_DYNAMIC_DEBUG. Otherwise, it
  76. * won't produce any code.
  77. */
  78. static void dev_dbg_obj(const char *event_name, struct media_gobj *gobj)
  79. {
  80. #if defined(DEBUG) || defined (CONFIG_DYNAMIC_DEBUG)
  81. switch (media_type(gobj)) {
  82. case MEDIA_GRAPH_ENTITY:
  83. dev_dbg(gobj->mdev->dev,
  84. "%s: id 0x%08x entity#%d: '%s'\n",
  85. event_name, gobj->id, media_localid(gobj),
  86. gobj_to_entity(gobj)->name);
  87. break;
  88. case MEDIA_GRAPH_LINK:
  89. {
  90. struct media_link *link = gobj_to_link(gobj);
  91. dev_dbg(gobj->mdev->dev,
  92. "%s: id 0x%08x link#%d: %s#%d ==> %s#%d\n",
  93. event_name, gobj->id, media_localid(gobj),
  94. gobj_type(media_type(link->gobj0)),
  95. media_localid(link->gobj0),
  96. gobj_type(media_type(link->gobj1)),
  97. media_localid(link->gobj1));
  98. break;
  99. }
  100. case MEDIA_GRAPH_PAD:
  101. {
  102. struct media_pad *pad = gobj_to_pad(gobj);
  103. dev_dbg(gobj->mdev->dev,
  104. "%s: id 0x%08x %s%spad#%d: '%s':%d\n",
  105. event_name, gobj->id,
  106. pad->flags & MEDIA_PAD_FL_SINK ? " sink " : "",
  107. pad->flags & MEDIA_PAD_FL_SOURCE ? "source " : "",
  108. media_localid(gobj),
  109. pad->entity->name, pad->index);
  110. break;
  111. }
  112. case MEDIA_GRAPH_INTF_DEVNODE:
  113. {
  114. struct media_interface *intf = gobj_to_intf(gobj);
  115. struct media_intf_devnode *devnode = intf_to_devnode(intf);
  116. dev_dbg(gobj->mdev->dev,
  117. "%s: id 0x%08x intf_devnode#%d: %s - major: %d, minor: %d\n",
  118. event_name, gobj->id, media_localid(gobj),
  119. intf_type(intf),
  120. devnode->major, devnode->minor);
  121. break;
  122. }
  123. }
  124. #endif
  125. }
  126. void media_gobj_create(struct media_device *mdev,
  127. enum media_gobj_type type,
  128. struct media_gobj *gobj)
  129. {
  130. BUG_ON(!mdev);
  131. gobj->mdev = mdev;
  132. /* Create a per-type unique object ID */
  133. switch (type) {
  134. case MEDIA_GRAPH_ENTITY:
  135. gobj->id = media_gobj_gen_id(type, ++mdev->entity_id);
  136. list_add_tail(&gobj->list, &mdev->entities);
  137. break;
  138. case MEDIA_GRAPH_PAD:
  139. gobj->id = media_gobj_gen_id(type, ++mdev->pad_id);
  140. list_add_tail(&gobj->list, &mdev->pads);
  141. break;
  142. case MEDIA_GRAPH_LINK:
  143. gobj->id = media_gobj_gen_id(type, ++mdev->link_id);
  144. list_add_tail(&gobj->list, &mdev->links);
  145. break;
  146. case MEDIA_GRAPH_INTF_DEVNODE:
  147. gobj->id = media_gobj_gen_id(type, ++mdev->intf_devnode_id);
  148. list_add_tail(&gobj->list, &mdev->interfaces);
  149. break;
  150. }
  151. mdev->topology_version++;
  152. dev_dbg_obj(__func__, gobj);
  153. }
  154. void media_gobj_destroy(struct media_gobj *gobj)
  155. {
  156. dev_dbg_obj(__func__, gobj);
  157. gobj->mdev->topology_version++;
  158. /* Remove the object from mdev list */
  159. list_del(&gobj->list);
  160. }
  161. int media_entity_pads_init(struct media_entity *entity, u16 num_pads,
  162. struct media_pad *pads)
  163. {
  164. struct media_device *mdev = entity->graph_obj.mdev;
  165. unsigned int i;
  166. entity->num_pads = num_pads;
  167. entity->pads = pads;
  168. if (mdev)
  169. spin_lock(&mdev->lock);
  170. for (i = 0; i < num_pads; i++) {
  171. pads[i].entity = entity;
  172. pads[i].index = i;
  173. if (mdev)
  174. media_gobj_create(mdev, MEDIA_GRAPH_PAD,
  175. &entity->pads[i].graph_obj);
  176. }
  177. if (mdev)
  178. spin_unlock(&mdev->lock);
  179. return 0;
  180. }
  181. EXPORT_SYMBOL_GPL(media_entity_pads_init);
  182. /* -----------------------------------------------------------------------------
  183. * Graph traversal
  184. */
  185. static struct media_entity *
  186. media_entity_other(struct media_entity *entity, struct media_link *link)
  187. {
  188. if (link->source->entity == entity)
  189. return link->sink->entity;
  190. else
  191. return link->source->entity;
  192. }
  193. /* push an entity to traversal stack */
  194. static void stack_push(struct media_entity_graph *graph,
  195. struct media_entity *entity)
  196. {
  197. if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) {
  198. WARN_ON(1);
  199. return;
  200. }
  201. graph->top++;
  202. graph->stack[graph->top].link = (&entity->links)->next;
  203. graph->stack[graph->top].entity = entity;
  204. }
  205. static struct media_entity *stack_pop(struct media_entity_graph *graph)
  206. {
  207. struct media_entity *entity;
  208. entity = graph->stack[graph->top].entity;
  209. graph->top--;
  210. return entity;
  211. }
  212. #define link_top(en) ((en)->stack[(en)->top].link)
  213. #define stack_top(en) ((en)->stack[(en)->top].entity)
  214. void media_entity_graph_walk_start(struct media_entity_graph *graph,
  215. struct media_entity *entity)
  216. {
  217. graph->top = 0;
  218. graph->stack[graph->top].entity = NULL;
  219. bitmap_zero(graph->entities, MEDIA_ENTITY_ENUM_MAX_ID);
  220. if (WARN_ON(media_entity_id(entity) >= MEDIA_ENTITY_ENUM_MAX_ID))
  221. return;
  222. __set_bit(media_entity_id(entity), graph->entities);
  223. stack_push(graph, entity);
  224. }
  225. EXPORT_SYMBOL_GPL(media_entity_graph_walk_start);
  226. struct media_entity *
  227. media_entity_graph_walk_next(struct media_entity_graph *graph)
  228. {
  229. if (stack_top(graph) == NULL)
  230. return NULL;
  231. /*
  232. * Depth first search. Push entity to stack and continue from
  233. * top of the stack until no more entities on the level can be
  234. * found.
  235. */
  236. while (link_top(graph) != &(stack_top(graph)->links)) {
  237. struct media_entity *entity = stack_top(graph);
  238. struct media_link *link;
  239. struct media_entity *next;
  240. link = list_entry(link_top(graph), typeof(*link), list);
  241. /* The link is not enabled so we do not follow. */
  242. if (!(link->flags & MEDIA_LNK_FL_ENABLED)) {
  243. link_top(graph) = link_top(graph)->next;
  244. continue;
  245. }
  246. /* Get the entity in the other end of the link . */
  247. next = media_entity_other(entity, link);
  248. if (WARN_ON(media_entity_id(next) >= MEDIA_ENTITY_ENUM_MAX_ID))
  249. return NULL;
  250. /* Has the entity already been visited? */
  251. if (__test_and_set_bit(media_entity_id(next), graph->entities)) {
  252. link_top(graph) = link_top(graph)->next;
  253. continue;
  254. }
  255. /* Push the new entity to stack and start over. */
  256. link_top(graph) = link_top(graph)->next;
  257. stack_push(graph, next);
  258. }
  259. return stack_pop(graph);
  260. }
  261. EXPORT_SYMBOL_GPL(media_entity_graph_walk_next);
  262. /* -----------------------------------------------------------------------------
  263. * Pipeline management
  264. */
  265. __must_check int media_entity_pipeline_start(struct media_entity *entity,
  266. struct media_pipeline *pipe)
  267. {
  268. struct media_device *mdev = entity->graph_obj.mdev;
  269. struct media_entity_graph graph;
  270. struct media_entity *entity_err = entity;
  271. struct media_link *link;
  272. int ret;
  273. mutex_lock(&mdev->graph_mutex);
  274. media_entity_graph_walk_start(&graph, entity);
  275. while ((entity = media_entity_graph_walk_next(&graph))) {
  276. DECLARE_BITMAP(active, MEDIA_ENTITY_MAX_PADS);
  277. DECLARE_BITMAP(has_no_links, MEDIA_ENTITY_MAX_PADS);
  278. entity->stream_count++;
  279. WARN_ON(entity->pipe && entity->pipe != pipe);
  280. entity->pipe = pipe;
  281. /* Already streaming --- no need to check. */
  282. if (entity->stream_count > 1)
  283. continue;
  284. if (!entity->ops || !entity->ops->link_validate)
  285. continue;
  286. bitmap_zero(active, entity->num_pads);
  287. bitmap_fill(has_no_links, entity->num_pads);
  288. list_for_each_entry(link, &entity->links, list) {
  289. struct media_pad *pad = link->sink->entity == entity
  290. ? link->sink : link->source;
  291. /* Mark that a pad is connected by a link. */
  292. bitmap_clear(has_no_links, pad->index, 1);
  293. /*
  294. * Pads that either do not need to connect or
  295. * are connected through an enabled link are
  296. * fine.
  297. */
  298. if (!(pad->flags & MEDIA_PAD_FL_MUST_CONNECT) ||
  299. link->flags & MEDIA_LNK_FL_ENABLED)
  300. bitmap_set(active, pad->index, 1);
  301. /*
  302. * Link validation will only take place for
  303. * sink ends of the link that are enabled.
  304. */
  305. if (link->sink != pad ||
  306. !(link->flags & MEDIA_LNK_FL_ENABLED))
  307. continue;
  308. ret = entity->ops->link_validate(link);
  309. if (ret < 0 && ret != -ENOIOCTLCMD) {
  310. dev_dbg(entity->graph_obj.mdev->dev,
  311. "link validation failed for \"%s\":%u -> \"%s\":%u, error %d\n",
  312. link->source->entity->name,
  313. link->source->index,
  314. entity->name, link->sink->index, ret);
  315. goto error;
  316. }
  317. }
  318. /* Either no links or validated links are fine. */
  319. bitmap_or(active, active, has_no_links, entity->num_pads);
  320. if (!bitmap_full(active, entity->num_pads)) {
  321. ret = -EPIPE;
  322. dev_dbg(entity->graph_obj.mdev->dev,
  323. "\"%s\":%u must be connected by an enabled link\n",
  324. entity->name,
  325. (unsigned)find_first_zero_bit(
  326. active, entity->num_pads));
  327. goto error;
  328. }
  329. }
  330. mutex_unlock(&mdev->graph_mutex);
  331. return 0;
  332. error:
  333. /*
  334. * Link validation on graph failed. We revert what we did and
  335. * return the error.
  336. */
  337. media_entity_graph_walk_start(&graph, entity_err);
  338. while ((entity_err = media_entity_graph_walk_next(&graph))) {
  339. entity_err->stream_count--;
  340. if (entity_err->stream_count == 0)
  341. entity_err->pipe = NULL;
  342. /*
  343. * We haven't increased stream_count further than this
  344. * so we quit here.
  345. */
  346. if (entity_err == entity)
  347. break;
  348. }
  349. mutex_unlock(&mdev->graph_mutex);
  350. return ret;
  351. }
  352. EXPORT_SYMBOL_GPL(media_entity_pipeline_start);
  353. void media_entity_pipeline_stop(struct media_entity *entity)
  354. {
  355. struct media_device *mdev = entity->graph_obj.mdev;
  356. struct media_entity_graph graph;
  357. mutex_lock(&mdev->graph_mutex);
  358. media_entity_graph_walk_start(&graph, entity);
  359. while ((entity = media_entity_graph_walk_next(&graph))) {
  360. entity->stream_count--;
  361. if (entity->stream_count == 0)
  362. entity->pipe = NULL;
  363. }
  364. mutex_unlock(&mdev->graph_mutex);
  365. }
  366. EXPORT_SYMBOL_GPL(media_entity_pipeline_stop);
  367. /* -----------------------------------------------------------------------------
  368. * Module use count
  369. */
  370. struct media_entity *media_entity_get(struct media_entity *entity)
  371. {
  372. if (entity == NULL)
  373. return NULL;
  374. if (entity->graph_obj.mdev->dev &&
  375. !try_module_get(entity->graph_obj.mdev->dev->driver->owner))
  376. return NULL;
  377. return entity;
  378. }
  379. EXPORT_SYMBOL_GPL(media_entity_get);
  380. void media_entity_put(struct media_entity *entity)
  381. {
  382. if (entity == NULL)
  383. return;
  384. if (entity->graph_obj.mdev->dev)
  385. module_put(entity->graph_obj.mdev->dev->driver->owner);
  386. }
  387. EXPORT_SYMBOL_GPL(media_entity_put);
  388. /* -----------------------------------------------------------------------------
  389. * Links management
  390. */
  391. static struct media_link *media_add_link(struct list_head *head)
  392. {
  393. struct media_link *link;
  394. link = kzalloc(sizeof(*link), GFP_KERNEL);
  395. if (link == NULL)
  396. return NULL;
  397. list_add_tail(&link->list, head);
  398. return link;
  399. }
  400. static void __media_entity_remove_link(struct media_entity *entity,
  401. struct media_link *link)
  402. {
  403. struct media_link *rlink, *tmp;
  404. struct media_entity *remote;
  405. if (link->source->entity == entity)
  406. remote = link->sink->entity;
  407. else
  408. remote = link->source->entity;
  409. list_for_each_entry_safe(rlink, tmp, &remote->links, list) {
  410. if (rlink != link->reverse)
  411. continue;
  412. if (link->source->entity == entity)
  413. remote->num_backlinks--;
  414. /* Remove the remote link */
  415. list_del(&rlink->list);
  416. media_gobj_destroy(&rlink->graph_obj);
  417. kfree(rlink);
  418. if (--remote->num_links == 0)
  419. break;
  420. }
  421. list_del(&link->list);
  422. media_gobj_destroy(&link->graph_obj);
  423. kfree(link);
  424. }
  425. int
  426. media_create_pad_link(struct media_entity *source, u16 source_pad,
  427. struct media_entity *sink, u16 sink_pad, u32 flags)
  428. {
  429. struct media_link *link;
  430. struct media_link *backlink;
  431. BUG_ON(source == NULL || sink == NULL);
  432. BUG_ON(source_pad >= source->num_pads);
  433. BUG_ON(sink_pad >= sink->num_pads);
  434. link = media_add_link(&source->links);
  435. if (link == NULL)
  436. return -ENOMEM;
  437. link->source = &source->pads[source_pad];
  438. link->sink = &sink->pads[sink_pad];
  439. link->flags = flags;
  440. /* Initialize graph object embedded at the new link */
  441. media_gobj_create(source->graph_obj.mdev, MEDIA_GRAPH_LINK,
  442. &link->graph_obj);
  443. /* Create the backlink. Backlinks are used to help graph traversal and
  444. * are not reported to userspace.
  445. */
  446. backlink = media_add_link(&sink->links);
  447. if (backlink == NULL) {
  448. __media_entity_remove_link(source, link);
  449. return -ENOMEM;
  450. }
  451. backlink->source = &source->pads[source_pad];
  452. backlink->sink = &sink->pads[sink_pad];
  453. backlink->flags = flags;
  454. backlink->is_backlink = true;
  455. /* Initialize graph object embedded at the new link */
  456. media_gobj_create(sink->graph_obj.mdev, MEDIA_GRAPH_LINK,
  457. &backlink->graph_obj);
  458. link->reverse = backlink;
  459. backlink->reverse = link;
  460. sink->num_backlinks++;
  461. sink->num_links++;
  462. source->num_links++;
  463. return 0;
  464. }
  465. EXPORT_SYMBOL_GPL(media_create_pad_link);
  466. void __media_entity_remove_links(struct media_entity *entity)
  467. {
  468. struct media_link *link, *tmp;
  469. list_for_each_entry_safe(link, tmp, &entity->links, list)
  470. __media_entity_remove_link(entity, link);
  471. entity->num_links = 0;
  472. entity->num_backlinks = 0;
  473. }
  474. EXPORT_SYMBOL_GPL(__media_entity_remove_links);
  475. void media_entity_remove_links(struct media_entity *entity)
  476. {
  477. /* Do nothing if the entity is not registered. */
  478. if (entity->graph_obj.mdev == NULL)
  479. return;
  480. spin_lock(&entity->graph_obj.mdev->lock);
  481. __media_entity_remove_links(entity);
  482. spin_unlock(&entity->graph_obj.mdev->lock);
  483. }
  484. EXPORT_SYMBOL_GPL(media_entity_remove_links);
  485. static int __media_entity_setup_link_notify(struct media_link *link, u32 flags)
  486. {
  487. int ret;
  488. /* Notify both entities. */
  489. ret = media_entity_call(link->source->entity, link_setup,
  490. link->source, link->sink, flags);
  491. if (ret < 0 && ret != -ENOIOCTLCMD)
  492. return ret;
  493. ret = media_entity_call(link->sink->entity, link_setup,
  494. link->sink, link->source, flags);
  495. if (ret < 0 && ret != -ENOIOCTLCMD) {
  496. media_entity_call(link->source->entity, link_setup,
  497. link->source, link->sink, link->flags);
  498. return ret;
  499. }
  500. link->flags = flags;
  501. link->reverse->flags = link->flags;
  502. return 0;
  503. }
  504. int __media_entity_setup_link(struct media_link *link, u32 flags)
  505. {
  506. const u32 mask = MEDIA_LNK_FL_ENABLED;
  507. struct media_device *mdev;
  508. struct media_entity *source, *sink;
  509. int ret = -EBUSY;
  510. if (link == NULL)
  511. return -EINVAL;
  512. /* The non-modifiable link flags must not be modified. */
  513. if ((link->flags & ~mask) != (flags & ~mask))
  514. return -EINVAL;
  515. if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
  516. return link->flags == flags ? 0 : -EINVAL;
  517. if (link->flags == flags)
  518. return 0;
  519. source = link->source->entity;
  520. sink = link->sink->entity;
  521. if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) &&
  522. (source->stream_count || sink->stream_count))
  523. return -EBUSY;
  524. mdev = source->graph_obj.mdev;
  525. if (mdev->link_notify) {
  526. ret = mdev->link_notify(link, flags,
  527. MEDIA_DEV_NOTIFY_PRE_LINK_CH);
  528. if (ret < 0)
  529. return ret;
  530. }
  531. ret = __media_entity_setup_link_notify(link, flags);
  532. if (mdev->link_notify)
  533. mdev->link_notify(link, flags, MEDIA_DEV_NOTIFY_POST_LINK_CH);
  534. return ret;
  535. }
  536. int media_entity_setup_link(struct media_link *link, u32 flags)
  537. {
  538. int ret;
  539. spin_lock(&link->source->entity->graph_obj.mdev->lock);
  540. ret = __media_entity_setup_link(link, flags);
  541. spin_unlock(&link->source->entity->graph_obj.mdev->lock);
  542. return ret;
  543. }
  544. EXPORT_SYMBOL_GPL(media_entity_setup_link);
  545. struct media_link *
  546. media_entity_find_link(struct media_pad *source, struct media_pad *sink)
  547. {
  548. struct media_link *link;
  549. list_for_each_entry(link, &source->entity->links, list) {
  550. if (link->source->entity == source->entity &&
  551. link->source->index == source->index &&
  552. link->sink->entity == sink->entity &&
  553. link->sink->index == sink->index)
  554. return link;
  555. }
  556. return NULL;
  557. }
  558. EXPORT_SYMBOL_GPL(media_entity_find_link);
  559. struct media_pad *media_entity_remote_pad(struct media_pad *pad)
  560. {
  561. struct media_link *link;
  562. list_for_each_entry(link, &pad->entity->links, list) {
  563. if (!(link->flags & MEDIA_LNK_FL_ENABLED))
  564. continue;
  565. if (link->source == pad)
  566. return link->sink;
  567. if (link->sink == pad)
  568. return link->source;
  569. }
  570. return NULL;
  571. }
  572. EXPORT_SYMBOL_GPL(media_entity_remote_pad);
  573. static void media_interface_init(struct media_device *mdev,
  574. struct media_interface *intf,
  575. u32 gobj_type,
  576. u32 intf_type, u32 flags)
  577. {
  578. intf->type = intf_type;
  579. intf->flags = flags;
  580. INIT_LIST_HEAD(&intf->links);
  581. media_gobj_create(mdev, gobj_type, &intf->graph_obj);
  582. }
  583. /* Functions related to the media interface via device nodes */
  584. struct media_intf_devnode *media_devnode_create(struct media_device *mdev,
  585. u32 type, u32 flags,
  586. u32 major, u32 minor)
  587. {
  588. struct media_intf_devnode *devnode;
  589. devnode = kzalloc(sizeof(*devnode), GFP_KERNEL);
  590. if (!devnode)
  591. return NULL;
  592. devnode->major = major;
  593. devnode->minor = minor;
  594. media_interface_init(mdev, &devnode->intf, MEDIA_GRAPH_INTF_DEVNODE,
  595. type, flags);
  596. return devnode;
  597. }
  598. EXPORT_SYMBOL_GPL(media_devnode_create);
  599. void media_devnode_remove(struct media_intf_devnode *devnode)
  600. {
  601. media_remove_intf_links(&devnode->intf);
  602. media_gobj_destroy(&devnode->intf.graph_obj);
  603. kfree(devnode);
  604. }
  605. EXPORT_SYMBOL_GPL(media_devnode_remove);
  606. struct media_link *media_create_intf_link(struct media_entity *entity,
  607. struct media_interface *intf,
  608. u32 flags)
  609. {
  610. struct media_link *link;
  611. link = media_add_link(&intf->links);
  612. if (link == NULL)
  613. return NULL;
  614. link->intf = intf;
  615. link->entity = entity;
  616. link->flags = flags;
  617. /* Initialize graph object embedded at the new link */
  618. media_gobj_create(intf->graph_obj.mdev, MEDIA_GRAPH_LINK,
  619. &link->graph_obj);
  620. return link;
  621. }
  622. EXPORT_SYMBOL_GPL(media_create_intf_link);
  623. void __media_remove_intf_link(struct media_link *link)
  624. {
  625. list_del(&link->list);
  626. media_gobj_destroy(&link->graph_obj);
  627. kfree(link);
  628. }
  629. EXPORT_SYMBOL_GPL(__media_remove_intf_link);
  630. void media_remove_intf_link(struct media_link *link)
  631. {
  632. spin_lock(&link->graph_obj.mdev->lock);
  633. __media_remove_intf_link(link);
  634. spin_unlock(&link->graph_obj.mdev->lock);
  635. }
  636. EXPORT_SYMBOL_GPL(media_remove_intf_link);
  637. void __media_remove_intf_links(struct media_interface *intf)
  638. {
  639. struct media_link *link, *tmp;
  640. list_for_each_entry_safe(link, tmp, &intf->links, list)
  641. __media_remove_intf_link(link);
  642. }
  643. EXPORT_SYMBOL_GPL(__media_remove_intf_links);
  644. void media_remove_intf_links(struct media_interface *intf)
  645. {
  646. /* Do nothing if the intf is not registered. */
  647. if (intf->graph_obj.mdev == NULL)
  648. return;
  649. spin_lock(&intf->graph_obj.mdev->lock);
  650. __media_remove_intf_links(intf);
  651. spin_unlock(&intf->graph_obj.mdev->lock);
  652. }
  653. EXPORT_SYMBOL_GPL(media_remove_intf_links);