media-entity.c 24 KB

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