media-entity.c 25 KB

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