media-entity.c 24 KB

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