media-entity.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  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. /**
  28. * dev_dbg_obj - Prints in debug mode a change on some object
  29. *
  30. * @event_name: Name of the event to report. Could be __func__
  31. * @gobj: Pointer to the object
  32. *
  33. * Enabled only if DEBUG or CONFIG_DYNAMIC_DEBUG. Otherwise, it
  34. * won't produce any code.
  35. */
  36. static inline const char *gobj_type(enum media_gobj_type type)
  37. {
  38. switch (type) {
  39. case MEDIA_GRAPH_ENTITY:
  40. return "entity";
  41. case MEDIA_GRAPH_PAD:
  42. return "pad";
  43. case MEDIA_GRAPH_LINK:
  44. return "link";
  45. case MEDIA_GRAPH_INTF_DEVNODE:
  46. return "intf-devnode";
  47. default:
  48. return "unknown";
  49. }
  50. }
  51. static inline const char *intf_type(struct media_interface *intf)
  52. {
  53. switch (intf->type) {
  54. case MEDIA_INTF_T_DVB_FE:
  55. return "frontend";
  56. case MEDIA_INTF_T_DVB_DEMUX:
  57. return "demux";
  58. case MEDIA_INTF_T_DVB_DVR:
  59. return "DVR";
  60. case MEDIA_INTF_T_DVB_CA:
  61. return "CA";
  62. case MEDIA_INTF_T_DVB_NET:
  63. return "dvbnet";
  64. case MEDIA_INTF_T_V4L_VIDEO:
  65. return "video";
  66. case MEDIA_INTF_T_V4L_VBI:
  67. return "vbi";
  68. case MEDIA_INTF_T_V4L_RADIO:
  69. return "radio";
  70. case MEDIA_INTF_T_V4L_SUBDEV:
  71. return "v4l2-subdev";
  72. case MEDIA_INTF_T_V4L_SWRADIO:
  73. return "swradio";
  74. default:
  75. return "unknown-intf";
  76. }
  77. };
  78. static void dev_dbg_obj(const char *event_name, struct media_gobj *gobj)
  79. {
  80. #if defined(DEBUG) || defined (CONFIG_DYNAMIC_DEBUG)
  81. switch (media_type(gobj)) {
  82. case MEDIA_GRAPH_ENTITY:
  83. dev_dbg(gobj->mdev->dev,
  84. "%s: id 0x%08x entity#%d: '%s'\n",
  85. event_name, gobj->id, media_localid(gobj),
  86. gobj_to_entity(gobj)->name);
  87. break;
  88. case MEDIA_GRAPH_LINK:
  89. {
  90. struct media_link *link = gobj_to_link(gobj);
  91. dev_dbg(gobj->mdev->dev,
  92. "%s: id 0x%08x link#%d: %s#%d ==> %s#%d\n",
  93. event_name, gobj->id, media_localid(gobj),
  94. gobj_type(media_type(link->gobj0)),
  95. media_localid(link->gobj0),
  96. gobj_type(media_type(link->gobj1)),
  97. media_localid(link->gobj1));
  98. break;
  99. }
  100. case MEDIA_GRAPH_PAD:
  101. {
  102. struct media_pad *pad = gobj_to_pad(gobj);
  103. dev_dbg(gobj->mdev->dev,
  104. "%s: id 0x%08x pad#%d: '%s':%d\n",
  105. event_name, gobj->id, media_localid(gobj),
  106. pad->entity->name, pad->index);
  107. break;
  108. }
  109. case MEDIA_GRAPH_INTF_DEVNODE:
  110. {
  111. struct media_interface *intf = gobj_to_intf(gobj);
  112. struct media_intf_devnode *devnode = intf_to_devnode(intf);
  113. dev_dbg(gobj->mdev->dev,
  114. "%s: id 0x%08x intf_devnode#%d: %s - major: %d, minor: %d\n",
  115. event_name, gobj->id, media_localid(gobj),
  116. intf_type(intf),
  117. devnode->major, devnode->minor);
  118. break;
  119. }
  120. }
  121. #endif
  122. }
  123. /**
  124. * media_gobj_init - Initialize a graph object
  125. *
  126. * @mdev: Pointer to the media_device that contains the object
  127. * @type: Type of the object
  128. * @gobj: Pointer to the object
  129. *
  130. * This routine initializes the embedded struct media_gobj inside a
  131. * media graph object. It is called automatically if media_*_create()
  132. * calls are used. However, if the object (entity, link, pad, interface)
  133. * is embedded on some other object, this function should be called before
  134. * registering the object at the media controller.
  135. */
  136. void media_gobj_init(struct media_device *mdev,
  137. enum media_gobj_type type,
  138. struct media_gobj *gobj)
  139. {
  140. BUG_ON(!mdev);
  141. gobj->mdev = mdev;
  142. /* Create a per-type unique object ID */
  143. switch (type) {
  144. case MEDIA_GRAPH_ENTITY:
  145. gobj->id = media_gobj_gen_id(type, ++mdev->entity_id);
  146. break;
  147. case MEDIA_GRAPH_PAD:
  148. gobj->id = media_gobj_gen_id(type, ++mdev->pad_id);
  149. break;
  150. case MEDIA_GRAPH_LINK:
  151. gobj->id = media_gobj_gen_id(type, ++mdev->link_id);
  152. break;
  153. case MEDIA_GRAPH_INTF_DEVNODE:
  154. gobj->id = media_gobj_gen_id(type, ++mdev->intf_devnode_id);
  155. break;
  156. }
  157. dev_dbg_obj(__func__, gobj);
  158. }
  159. /**
  160. * media_gobj_remove - Stop using a graph object on a media device
  161. *
  162. * @graph_obj: Pointer to the object
  163. *
  164. * This should be called at media_device_unregister_*() routines
  165. */
  166. void media_gobj_remove(struct media_gobj *gobj)
  167. {
  168. dev_dbg_obj(__func__, gobj);
  169. }
  170. /**
  171. * media_entity_init - Initialize a media entity
  172. *
  173. * @num_pads: Total number of sink and source pads.
  174. * @pads: Array of 'num_pads' pads.
  175. *
  176. * The total number of pads is an intrinsic property of entities known by the
  177. * entity driver, while the total number of links depends on hardware design
  178. * and is an extrinsic property unknown to the entity driver. However, in most
  179. * use cases the number of links can safely be assumed to be equal to or
  180. * larger than the number of pads.
  181. *
  182. * For those reasons the links array can be preallocated based on the number
  183. * of pads and will be reallocated later if extra links need to be created.
  184. *
  185. * This function allocates a links array with enough space to hold at least
  186. * 'num_pads' elements. The media_entity::max_links field will be set to the
  187. * number of allocated elements.
  188. *
  189. * The pads array is managed by the entity driver and passed to
  190. * media_entity_init() where its pointer will be stored in the entity structure.
  191. */
  192. int
  193. media_entity_init(struct media_entity *entity, u16 num_pads,
  194. struct media_pad *pads)
  195. {
  196. unsigned int i;
  197. entity->group_id = 0;
  198. entity->num_links = 0;
  199. entity->num_backlinks = 0;
  200. entity->num_pads = num_pads;
  201. entity->pads = pads;
  202. for (i = 0; i < num_pads; i++) {
  203. pads[i].entity = entity;
  204. pads[i].index = i;
  205. }
  206. return 0;
  207. }
  208. EXPORT_SYMBOL_GPL(media_entity_init);
  209. void
  210. media_entity_cleanup(struct media_entity *entity)
  211. {
  212. struct media_link *link, *tmp;
  213. list_for_each_entry_safe(link, tmp, &entity->links, list) {
  214. media_gobj_remove(&link->graph_obj);
  215. list_del(&link->list);
  216. kfree(link);
  217. }
  218. }
  219. EXPORT_SYMBOL_GPL(media_entity_cleanup);
  220. /* -----------------------------------------------------------------------------
  221. * Graph traversal
  222. */
  223. static struct media_entity *
  224. media_entity_other(struct media_entity *entity, struct media_link *link)
  225. {
  226. if (link->source->entity == entity)
  227. return link->sink->entity;
  228. else
  229. return link->source->entity;
  230. }
  231. /* push an entity to traversal stack */
  232. static void stack_push(struct media_entity_graph *graph,
  233. struct media_entity *entity)
  234. {
  235. if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) {
  236. WARN_ON(1);
  237. return;
  238. }
  239. graph->top++;
  240. graph->stack[graph->top].link = (&entity->links)->next;
  241. graph->stack[graph->top].entity = entity;
  242. }
  243. static struct media_entity *stack_pop(struct media_entity_graph *graph)
  244. {
  245. struct media_entity *entity;
  246. entity = graph->stack[graph->top].entity;
  247. graph->top--;
  248. return entity;
  249. }
  250. #define link_top(en) ((en)->stack[(en)->top].link)
  251. #define stack_top(en) ((en)->stack[(en)->top].entity)
  252. /**
  253. * media_entity_graph_walk_start - Start walking the media graph at a given entity
  254. * @graph: Media graph structure that will be used to walk the graph
  255. * @entity: Starting entity
  256. *
  257. * This function initializes the graph traversal structure to walk the entities
  258. * graph starting at the given entity. The traversal structure must not be
  259. * modified by the caller during graph traversal. When done the structure can
  260. * safely be freed.
  261. */
  262. void media_entity_graph_walk_start(struct media_entity_graph *graph,
  263. struct media_entity *entity)
  264. {
  265. graph->top = 0;
  266. graph->stack[graph->top].entity = NULL;
  267. bitmap_zero(graph->entities, MEDIA_ENTITY_ENUM_MAX_ID);
  268. if (WARN_ON(media_entity_id(entity) >= MEDIA_ENTITY_ENUM_MAX_ID))
  269. return;
  270. __set_bit(media_entity_id(entity), graph->entities);
  271. stack_push(graph, entity);
  272. }
  273. EXPORT_SYMBOL_GPL(media_entity_graph_walk_start);
  274. /**
  275. * media_entity_graph_walk_next - Get the next entity in the graph
  276. * @graph: Media graph structure
  277. *
  278. * Perform a depth-first traversal of the given media entities graph.
  279. *
  280. * The graph structure must have been previously initialized with a call to
  281. * media_entity_graph_walk_start().
  282. *
  283. * Return the next entity in the graph or NULL if the whole graph have been
  284. * traversed.
  285. */
  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. if (WARN_ON(media_entity_id(next) >= MEDIA_ENTITY_ENUM_MAX_ID))
  309. return NULL;
  310. /* Has the entity already been visited? */
  311. if (__test_and_set_bit(media_entity_id(next), graph->entities)) {
  312. link_top(graph) = link_top(graph)->next;
  313. continue;
  314. }
  315. /* Push the new entity to stack and start over. */
  316. link_top(graph) = link_top(graph)->next;
  317. stack_push(graph, next);
  318. }
  319. return stack_pop(graph);
  320. }
  321. EXPORT_SYMBOL_GPL(media_entity_graph_walk_next);
  322. /* -----------------------------------------------------------------------------
  323. * Pipeline management
  324. */
  325. /**
  326. * media_entity_pipeline_start - Mark a pipeline as streaming
  327. * @entity: Starting entity
  328. * @pipe: Media pipeline to be assigned to all entities in the pipeline.
  329. *
  330. * Mark all entities connected to a given entity through enabled links, either
  331. * directly or indirectly, as streaming. The given pipeline object is assigned to
  332. * every entity in the pipeline and stored in the media_entity pipe field.
  333. *
  334. * Calls to this function can be nested, in which case the same number of
  335. * media_entity_pipeline_stop() calls will be required to stop streaming. The
  336. * pipeline pointer must be identical for all nested calls to
  337. * media_entity_pipeline_start().
  338. */
  339. __must_check int media_entity_pipeline_start(struct media_entity *entity,
  340. struct media_pipeline *pipe)
  341. {
  342. struct media_device *mdev = entity->graph_obj.mdev;
  343. struct media_entity_graph graph;
  344. struct media_entity *entity_err = entity;
  345. struct media_link *link;
  346. int ret;
  347. mutex_lock(&mdev->graph_mutex);
  348. media_entity_graph_walk_start(&graph, entity);
  349. while ((entity = media_entity_graph_walk_next(&graph))) {
  350. DECLARE_BITMAP(active, MEDIA_ENTITY_MAX_PADS);
  351. DECLARE_BITMAP(has_no_links, MEDIA_ENTITY_MAX_PADS);
  352. entity->stream_count++;
  353. WARN_ON(entity->pipe && entity->pipe != pipe);
  354. entity->pipe = pipe;
  355. /* Already streaming --- no need to check. */
  356. if (entity->stream_count > 1)
  357. continue;
  358. if (!entity->ops || !entity->ops->link_validate)
  359. continue;
  360. bitmap_zero(active, entity->num_pads);
  361. bitmap_fill(has_no_links, entity->num_pads);
  362. list_for_each_entry(link, &entity->links, list) {
  363. struct media_pad *pad = link->sink->entity == entity
  364. ? link->sink : link->source;
  365. /* Mark that a pad is connected by a link. */
  366. bitmap_clear(has_no_links, pad->index, 1);
  367. /*
  368. * Pads that either do not need to connect or
  369. * are connected through an enabled link are
  370. * fine.
  371. */
  372. if (!(pad->flags & MEDIA_PAD_FL_MUST_CONNECT) ||
  373. link->flags & MEDIA_LNK_FL_ENABLED)
  374. bitmap_set(active, pad->index, 1);
  375. /*
  376. * Link validation will only take place for
  377. * sink ends of the link that are enabled.
  378. */
  379. if (link->sink != pad ||
  380. !(link->flags & MEDIA_LNK_FL_ENABLED))
  381. continue;
  382. ret = entity->ops->link_validate(link);
  383. if (ret < 0 && ret != -ENOIOCTLCMD) {
  384. dev_dbg(entity->graph_obj.mdev->dev,
  385. "link validation failed for \"%s\":%u -> \"%s\":%u, error %d\n",
  386. link->source->entity->name,
  387. link->source->index,
  388. entity->name, link->sink->index, ret);
  389. goto error;
  390. }
  391. }
  392. /* Either no links or validated links are fine. */
  393. bitmap_or(active, active, has_no_links, entity->num_pads);
  394. if (!bitmap_full(active, entity->num_pads)) {
  395. ret = -EPIPE;
  396. dev_dbg(entity->graph_obj.mdev->dev,
  397. "\"%s\":%u must be connected by an enabled link\n",
  398. entity->name,
  399. (unsigned)find_first_zero_bit(
  400. active, entity->num_pads));
  401. goto error;
  402. }
  403. }
  404. mutex_unlock(&mdev->graph_mutex);
  405. return 0;
  406. error:
  407. /*
  408. * Link validation on graph failed. We revert what we did and
  409. * return the error.
  410. */
  411. media_entity_graph_walk_start(&graph, entity_err);
  412. while ((entity_err = media_entity_graph_walk_next(&graph))) {
  413. entity_err->stream_count--;
  414. if (entity_err->stream_count == 0)
  415. entity_err->pipe = NULL;
  416. /*
  417. * We haven't increased stream_count further than this
  418. * so we quit here.
  419. */
  420. if (entity_err == entity)
  421. break;
  422. }
  423. mutex_unlock(&mdev->graph_mutex);
  424. return ret;
  425. }
  426. EXPORT_SYMBOL_GPL(media_entity_pipeline_start);
  427. /**
  428. * media_entity_pipeline_stop - Mark a pipeline as not streaming
  429. * @entity: Starting entity
  430. *
  431. * Mark all entities connected to a given entity through enabled links, either
  432. * directly or indirectly, as not streaming. The media_entity pipe field is
  433. * reset to NULL.
  434. *
  435. * If multiple calls to media_entity_pipeline_start() have been made, the same
  436. * number of calls to this function are required to mark the pipeline as not
  437. * streaming.
  438. */
  439. void media_entity_pipeline_stop(struct media_entity *entity)
  440. {
  441. struct media_device *mdev = entity->graph_obj.mdev;
  442. struct media_entity_graph graph;
  443. mutex_lock(&mdev->graph_mutex);
  444. media_entity_graph_walk_start(&graph, entity);
  445. while ((entity = media_entity_graph_walk_next(&graph))) {
  446. entity->stream_count--;
  447. if (entity->stream_count == 0)
  448. entity->pipe = NULL;
  449. }
  450. mutex_unlock(&mdev->graph_mutex);
  451. }
  452. EXPORT_SYMBOL_GPL(media_entity_pipeline_stop);
  453. /* -----------------------------------------------------------------------------
  454. * Module use count
  455. */
  456. /*
  457. * media_entity_get - Get a reference to the parent module
  458. * @entity: The entity
  459. *
  460. * Get a reference to the parent media device module.
  461. *
  462. * The function will return immediately if @entity is NULL.
  463. *
  464. * Return a pointer to the entity on success or NULL on failure.
  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. /*
  477. * media_entity_put - Release the reference to the parent module
  478. * @entity: The entity
  479. *
  480. * Release the reference count acquired by media_entity_get().
  481. *
  482. * The function will return immediately if @entity is NULL.
  483. */
  484. void media_entity_put(struct media_entity *entity)
  485. {
  486. if (entity == NULL)
  487. return;
  488. if (entity->graph_obj.mdev->dev)
  489. module_put(entity->graph_obj.mdev->dev->driver->owner);
  490. }
  491. EXPORT_SYMBOL_GPL(media_entity_put);
  492. /* -----------------------------------------------------------------------------
  493. * Links management
  494. */
  495. static struct media_link *media_add_link(struct list_head *head)
  496. {
  497. struct media_link *link;
  498. link = kzalloc(sizeof(*link), GFP_KERNEL);
  499. if (link == NULL)
  500. return NULL;
  501. list_add_tail(&link->list, head);
  502. return link;
  503. }
  504. static void __media_entity_remove_link(struct media_entity *entity,
  505. struct media_link *link);
  506. int
  507. media_create_pad_link(struct media_entity *source, u16 source_pad,
  508. struct media_entity *sink, u16 sink_pad, u32 flags)
  509. {
  510. struct media_link *link;
  511. struct media_link *backlink;
  512. BUG_ON(source == NULL || sink == NULL);
  513. BUG_ON(source_pad >= source->num_pads);
  514. BUG_ON(sink_pad >= sink->num_pads);
  515. link = media_add_link(&source->links);
  516. if (link == NULL)
  517. return -ENOMEM;
  518. link->source = &source->pads[source_pad];
  519. link->sink = &sink->pads[sink_pad];
  520. link->flags = flags;
  521. /* Initialize graph object embedded at the new link */
  522. media_gobj_init(source->graph_obj.mdev, MEDIA_GRAPH_LINK,
  523. &link->graph_obj);
  524. /* Create the backlink. Backlinks are used to help graph traversal and
  525. * are not reported to userspace.
  526. */
  527. backlink = media_add_link(&sink->links);
  528. if (backlink == NULL) {
  529. __media_entity_remove_link(source, link);
  530. return -ENOMEM;
  531. }
  532. backlink->source = &source->pads[source_pad];
  533. backlink->sink = &sink->pads[sink_pad];
  534. backlink->flags = flags;
  535. /* Initialize graph object embedded at the new link */
  536. media_gobj_init(sink->graph_obj.mdev, MEDIA_GRAPH_LINK,
  537. &backlink->graph_obj);
  538. link->reverse = backlink;
  539. backlink->reverse = link;
  540. sink->num_backlinks++;
  541. sink->num_links++;
  542. source->num_links++;
  543. return 0;
  544. }
  545. EXPORT_SYMBOL_GPL(media_create_pad_link);
  546. static void __media_entity_remove_link(struct media_entity *entity,
  547. struct media_link *link)
  548. {
  549. struct media_link *rlink, *tmp;
  550. struct media_entity *remote;
  551. unsigned int r = 0;
  552. if (link->source->entity == entity)
  553. remote = link->sink->entity;
  554. else
  555. remote = link->source->entity;
  556. list_for_each_entry_safe(rlink, tmp, &remote->links, list) {
  557. if (rlink != link->reverse) {
  558. r++;
  559. continue;
  560. }
  561. if (link->source->entity == entity)
  562. remote->num_backlinks--;
  563. if (--remote->num_links == 0)
  564. break;
  565. /* Remove the remote link */
  566. list_del(&rlink->list);
  567. kfree(rlink);
  568. }
  569. list_del(&link->list);
  570. kfree(link);
  571. }
  572. void __media_entity_remove_links(struct media_entity *entity)
  573. {
  574. struct media_link *link, *tmp;
  575. list_for_each_entry_safe(link, tmp, &entity->links, list)
  576. __media_entity_remove_link(entity, link);
  577. entity->num_links = 0;
  578. entity->num_backlinks = 0;
  579. }
  580. EXPORT_SYMBOL_GPL(__media_entity_remove_links);
  581. void media_entity_remove_links(struct media_entity *entity)
  582. {
  583. /* Do nothing if the entity is not registered. */
  584. if (entity->graph_obj.mdev == NULL)
  585. return;
  586. mutex_lock(&entity->graph_obj.mdev->graph_mutex);
  587. __media_entity_remove_links(entity);
  588. mutex_unlock(&entity->graph_obj.mdev->graph_mutex);
  589. }
  590. EXPORT_SYMBOL_GPL(media_entity_remove_links);
  591. static int __media_entity_setup_link_notify(struct media_link *link, u32 flags)
  592. {
  593. int ret;
  594. /* Notify both entities. */
  595. ret = media_entity_call(link->source->entity, link_setup,
  596. link->source, link->sink, flags);
  597. if (ret < 0 && ret != -ENOIOCTLCMD)
  598. return ret;
  599. ret = media_entity_call(link->sink->entity, link_setup,
  600. link->sink, link->source, flags);
  601. if (ret < 0 && ret != -ENOIOCTLCMD) {
  602. media_entity_call(link->source->entity, link_setup,
  603. link->source, link->sink, link->flags);
  604. return ret;
  605. }
  606. link->flags = flags;
  607. link->reverse->flags = link->flags;
  608. return 0;
  609. }
  610. /**
  611. * __media_entity_setup_link - Configure a media link
  612. * @link: The link being configured
  613. * @flags: Link configuration flags
  614. *
  615. * The bulk of link setup is handled by the two entities connected through the
  616. * link. This function notifies both entities of the link configuration change.
  617. *
  618. * If the link is immutable or if the current and new configuration are
  619. * identical, return immediately.
  620. *
  621. * The user is expected to hold link->source->parent->mutex. If not,
  622. * media_entity_setup_link() should be used instead.
  623. */
  624. int __media_entity_setup_link(struct media_link *link, u32 flags)
  625. {
  626. const u32 mask = MEDIA_LNK_FL_ENABLED;
  627. struct media_device *mdev;
  628. struct media_entity *source, *sink;
  629. int ret = -EBUSY;
  630. if (link == NULL)
  631. return -EINVAL;
  632. /* The non-modifiable link flags must not be modified. */
  633. if ((link->flags & ~mask) != (flags & ~mask))
  634. return -EINVAL;
  635. if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
  636. return link->flags == flags ? 0 : -EINVAL;
  637. if (link->flags == flags)
  638. return 0;
  639. source = link->source->entity;
  640. sink = link->sink->entity;
  641. if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) &&
  642. (source->stream_count || sink->stream_count))
  643. return -EBUSY;
  644. mdev = source->graph_obj.mdev;
  645. if (mdev->link_notify) {
  646. ret = mdev->link_notify(link, flags,
  647. MEDIA_DEV_NOTIFY_PRE_LINK_CH);
  648. if (ret < 0)
  649. return ret;
  650. }
  651. ret = __media_entity_setup_link_notify(link, flags);
  652. if (mdev->link_notify)
  653. mdev->link_notify(link, flags, MEDIA_DEV_NOTIFY_POST_LINK_CH);
  654. return ret;
  655. }
  656. int media_entity_setup_link(struct media_link *link, u32 flags)
  657. {
  658. int ret;
  659. mutex_lock(&link->source->entity->graph_obj.mdev->graph_mutex);
  660. ret = __media_entity_setup_link(link, flags);
  661. mutex_unlock(&link->source->entity->graph_obj.mdev->graph_mutex);
  662. return ret;
  663. }
  664. EXPORT_SYMBOL_GPL(media_entity_setup_link);
  665. /**
  666. * media_entity_find_link - Find a link between two pads
  667. * @source: Source pad
  668. * @sink: Sink pad
  669. *
  670. * Return a pointer to the link between the two entities. If no such link
  671. * exists, return NULL.
  672. */
  673. struct media_link *
  674. media_entity_find_link(struct media_pad *source, struct media_pad *sink)
  675. {
  676. struct media_link *link;
  677. list_for_each_entry(link, &source->entity->links, list) {
  678. if (link->source->entity == source->entity &&
  679. link->source->index == source->index &&
  680. link->sink->entity == sink->entity &&
  681. link->sink->index == sink->index)
  682. return link;
  683. }
  684. return NULL;
  685. }
  686. EXPORT_SYMBOL_GPL(media_entity_find_link);
  687. /**
  688. * media_entity_remote_pad - Find the pad at the remote end of a link
  689. * @pad: Pad at the local end of the link
  690. *
  691. * Search for a remote pad connected to the given pad by iterating over all
  692. * links originating or terminating at that pad until an enabled link is found.
  693. *
  694. * Return a pointer to the pad at the remote end of the first found enabled
  695. * link, or NULL if no enabled link has been found.
  696. */
  697. struct media_pad *media_entity_remote_pad(struct media_pad *pad)
  698. {
  699. struct media_link *link;
  700. list_for_each_entry(link, &pad->entity->links, list) {
  701. if (!(link->flags & MEDIA_LNK_FL_ENABLED))
  702. continue;
  703. if (link->source == pad)
  704. return link->sink;
  705. if (link->sink == pad)
  706. return link->source;
  707. }
  708. return NULL;
  709. }
  710. EXPORT_SYMBOL_GPL(media_entity_remote_pad);
  711. static void media_interface_init(struct media_device *mdev,
  712. struct media_interface *intf,
  713. u32 gobj_type,
  714. u32 intf_type, u32 flags)
  715. {
  716. intf->type = intf_type;
  717. intf->flags = flags;
  718. INIT_LIST_HEAD(&intf->links);
  719. media_gobj_init(mdev, gobj_type, &intf->graph_obj);
  720. list_add_tail(&intf->list, &mdev->interfaces);
  721. }
  722. /* Functions related to the media interface via device nodes */
  723. struct media_intf_devnode *media_devnode_create(struct media_device *mdev,
  724. u32 type, u32 flags,
  725. u32 major, u32 minor,
  726. gfp_t gfp_flags)
  727. {
  728. struct media_intf_devnode *devnode;
  729. devnode = kzalloc(sizeof(*devnode), gfp_flags);
  730. if (!devnode)
  731. return NULL;
  732. devnode->major = major;
  733. devnode->minor = minor;
  734. media_interface_init(mdev, &devnode->intf, MEDIA_GRAPH_INTF_DEVNODE,
  735. type, flags);
  736. return devnode;
  737. }
  738. EXPORT_SYMBOL_GPL(media_devnode_create);
  739. void media_devnode_remove(struct media_intf_devnode *devnode)
  740. {
  741. media_gobj_remove(&devnode->intf.graph_obj);
  742. list_del(&devnode->intf.list);
  743. kfree(devnode);
  744. }
  745. EXPORT_SYMBOL_GPL(media_devnode_remove);
  746. struct media_link *media_create_intf_link(struct media_entity *entity,
  747. struct media_interface *intf,
  748. u32 flags)
  749. {
  750. struct media_link *link;
  751. link = media_add_link(&intf->links);
  752. if (link == NULL)
  753. return NULL;
  754. link->intf = intf;
  755. link->entity = entity;
  756. link->flags = flags;
  757. /* Initialize graph object embedded at the new link */
  758. media_gobj_init(intf->graph_obj.mdev, MEDIA_GRAPH_LINK,
  759. &link->graph_obj);
  760. return link;
  761. }
  762. EXPORT_SYMBOL_GPL(media_create_intf_link);
  763. static void __media_remove_intf_link(struct media_link *link)
  764. {
  765. media_gobj_remove(&link->graph_obj);
  766. kfree(link);
  767. }
  768. void media_remove_intf_link(struct media_link *link)
  769. {
  770. mutex_lock(&link->graph_obj.mdev->graph_mutex);
  771. __media_remove_intf_link(link);
  772. mutex_unlock(&link->graph_obj.mdev->graph_mutex);
  773. }
  774. EXPORT_SYMBOL_GPL(media_remove_intf_link);