media-entity.c 24 KB

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