v4l2-mc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Media Controller ancillary functions
  4. *
  5. * Copyright (c) 2016 Mauro Carvalho Chehab <mchehab@kernel.org>
  6. * Copyright (C) 2016 Shuah Khan <shuahkh@osg.samsung.com>
  7. * Copyright (C) 2006-2010 Nokia Corporation
  8. * Copyright (c) 2016 Intel Corporation.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/pci.h>
  12. #include <linux/usb.h>
  13. #include <media/media-device.h>
  14. #include <media/media-entity.h>
  15. #include <media/v4l2-fh.h>
  16. #include <media/v4l2-mc.h>
  17. #include <media/v4l2-subdev.h>
  18. #include <media/videobuf2-core.h>
  19. int v4l2_mc_create_media_graph(struct media_device *mdev)
  20. {
  21. struct media_entity *entity;
  22. struct media_entity *if_vid = NULL, *if_aud = NULL;
  23. struct media_entity *tuner = NULL, *decoder = NULL;
  24. struct media_entity *io_v4l = NULL, *io_vbi = NULL, *io_swradio = NULL;
  25. bool is_webcam = false;
  26. u32 flags;
  27. int ret, pad_sink, pad_source;
  28. if (!mdev)
  29. return 0;
  30. media_device_for_each_entity(entity, mdev) {
  31. switch (entity->function) {
  32. case MEDIA_ENT_F_IF_VID_DECODER:
  33. if_vid = entity;
  34. break;
  35. case MEDIA_ENT_F_IF_AUD_DECODER:
  36. if_aud = entity;
  37. break;
  38. case MEDIA_ENT_F_TUNER:
  39. tuner = entity;
  40. break;
  41. case MEDIA_ENT_F_ATV_DECODER:
  42. decoder = entity;
  43. break;
  44. case MEDIA_ENT_F_IO_V4L:
  45. io_v4l = entity;
  46. break;
  47. case MEDIA_ENT_F_IO_VBI:
  48. io_vbi = entity;
  49. break;
  50. case MEDIA_ENT_F_IO_SWRADIO:
  51. io_swradio = entity;
  52. break;
  53. case MEDIA_ENT_F_CAM_SENSOR:
  54. is_webcam = true;
  55. break;
  56. }
  57. }
  58. /* It should have at least one I/O entity */
  59. if (!io_v4l && !io_vbi && !io_swradio) {
  60. dev_warn(mdev->dev, "Didn't find any I/O entity\n");
  61. return -EINVAL;
  62. }
  63. /*
  64. * Here, webcams are modelled on a very simple way: the sensor is
  65. * connected directly to the I/O entity. All dirty details, like
  66. * scaler and crop HW are hidden. While such mapping is not enough
  67. * for mc-centric hardware, it is enough for v4l2 interface centric
  68. * PC-consumer's hardware.
  69. */
  70. if (is_webcam) {
  71. if (!io_v4l) {
  72. dev_warn(mdev->dev, "Didn't find a MEDIA_ENT_F_IO_V4L\n");
  73. return -EINVAL;
  74. }
  75. media_device_for_each_entity(entity, mdev) {
  76. if (entity->function != MEDIA_ENT_F_CAM_SENSOR)
  77. continue;
  78. ret = media_create_pad_link(entity, 0,
  79. io_v4l, 0,
  80. MEDIA_LNK_FL_ENABLED);
  81. if (ret) {
  82. dev_warn(mdev->dev, "Failed to create a sensor link\n");
  83. return ret;
  84. }
  85. }
  86. if (!decoder)
  87. return 0;
  88. }
  89. /* The device isn't a webcam. So, it should have a decoder */
  90. if (!decoder) {
  91. dev_warn(mdev->dev, "Decoder not found\n");
  92. return -EINVAL;
  93. }
  94. /* Link the tuner and IF video output pads */
  95. if (tuner) {
  96. if (if_vid) {
  97. pad_source = media_get_pad_index(tuner,
  98. MEDIA_PAD_FL_SOURCE,
  99. PAD_SIGNAL_ANALOG);
  100. pad_sink = media_get_pad_index(if_vid,
  101. MEDIA_PAD_FL_SINK,
  102. PAD_SIGNAL_ANALOG);
  103. if (pad_source < 0 || pad_sink < 0) {
  104. dev_warn(mdev->dev, "Couldn't get tuner and/or PLL pad(s): (%d, %d)\n",
  105. pad_source, pad_sink);
  106. return -EINVAL;
  107. }
  108. ret = media_create_pad_link(tuner, pad_source,
  109. if_vid, pad_sink,
  110. MEDIA_LNK_FL_ENABLED);
  111. if (ret) {
  112. dev_warn(mdev->dev, "Couldn't create tuner->PLL link)\n");
  113. return ret;
  114. }
  115. pad_source = media_get_pad_index(if_vid,
  116. MEDIA_PAD_FL_SOURCE,
  117. PAD_SIGNAL_ANALOG);
  118. pad_sink = media_get_pad_index(decoder,
  119. MEDIA_PAD_FL_SINK,
  120. PAD_SIGNAL_ANALOG);
  121. if (pad_source < 0 || pad_sink < 0) {
  122. dev_warn(mdev->dev, "get decoder and/or PLL pad(s): (%d, %d)\n",
  123. pad_source, pad_sink);
  124. return -EINVAL;
  125. }
  126. ret = media_create_pad_link(if_vid, pad_source,
  127. decoder, pad_sink,
  128. MEDIA_LNK_FL_ENABLED);
  129. if (ret) {
  130. dev_warn(mdev->dev, "couldn't link PLL to decoder\n");
  131. return ret;
  132. }
  133. } else {
  134. pad_source = media_get_pad_index(tuner,
  135. MEDIA_PAD_FL_SOURCE,
  136. PAD_SIGNAL_ANALOG);
  137. pad_sink = media_get_pad_index(decoder,
  138. MEDIA_PAD_FL_SINK,
  139. PAD_SIGNAL_ANALOG);
  140. if (pad_source < 0 || pad_sink < 0) {
  141. dev_warn(mdev->dev, "couldn't get tuner and/or decoder pad(s): (%d, %d)\n",
  142. pad_source, pad_sink);
  143. return -EINVAL;
  144. }
  145. ret = media_create_pad_link(tuner, pad_source,
  146. decoder, pad_sink,
  147. MEDIA_LNK_FL_ENABLED);
  148. if (ret)
  149. return ret;
  150. }
  151. if (if_aud) {
  152. pad_source = media_get_pad_index(tuner,
  153. MEDIA_PAD_FL_SOURCE,
  154. PAD_SIGNAL_AUDIO);
  155. pad_sink = media_get_pad_index(if_aud,
  156. MEDIA_PAD_FL_SINK,
  157. PAD_SIGNAL_AUDIO);
  158. if (pad_source < 0 || pad_sink < 0) {
  159. dev_warn(mdev->dev, "couldn't get tuner and/or decoder pad(s) for audio: (%d, %d)\n",
  160. pad_source, pad_sink);
  161. return -EINVAL;
  162. }
  163. ret = media_create_pad_link(tuner, pad_source,
  164. if_aud, pad_sink,
  165. MEDIA_LNK_FL_ENABLED);
  166. if (ret) {
  167. dev_warn(mdev->dev, "couldn't link tuner->audio PLL\n");
  168. return ret;
  169. }
  170. } else {
  171. if_aud = tuner;
  172. }
  173. }
  174. /* Create demod to V4L, VBI and SDR radio links */
  175. if (io_v4l) {
  176. pad_source = media_get_pad_index(decoder, MEDIA_PAD_FL_SOURCE,
  177. PAD_SIGNAL_DV);
  178. if (pad_source < 0) {
  179. dev_warn(mdev->dev, "couldn't get decoder output pad for V4L I/O\n");
  180. return -EINVAL;
  181. }
  182. ret = media_create_pad_link(decoder, pad_source,
  183. io_v4l, 0,
  184. MEDIA_LNK_FL_ENABLED);
  185. if (ret) {
  186. dev_warn(mdev->dev, "couldn't link decoder output to V4L I/O\n");
  187. return ret;
  188. }
  189. }
  190. if (io_swradio) {
  191. pad_source = media_get_pad_index(decoder, MEDIA_PAD_FL_SOURCE,
  192. PAD_SIGNAL_DV);
  193. if (pad_source < 0) {
  194. dev_warn(mdev->dev, "couldn't get decoder output pad for SDR\n");
  195. return -EINVAL;
  196. }
  197. ret = media_create_pad_link(decoder, pad_source,
  198. io_swradio, 0,
  199. MEDIA_LNK_FL_ENABLED);
  200. if (ret) {
  201. dev_warn(mdev->dev, "couldn't link decoder output to SDR\n");
  202. return ret;
  203. }
  204. }
  205. if (io_vbi) {
  206. pad_source = media_get_pad_index(decoder, MEDIA_PAD_FL_SOURCE,
  207. PAD_SIGNAL_DV);
  208. if (pad_source < 0) {
  209. dev_warn(mdev->dev, "couldn't get decoder output pad for VBI\n");
  210. return -EINVAL;
  211. }
  212. ret = media_create_pad_link(decoder, pad_source,
  213. io_vbi, 0,
  214. MEDIA_LNK_FL_ENABLED);
  215. if (ret) {
  216. dev_warn(mdev->dev, "couldn't link decoder output to VBI\n");
  217. return ret;
  218. }
  219. }
  220. /* Create links for the media connectors */
  221. flags = MEDIA_LNK_FL_ENABLED;
  222. media_device_for_each_entity(entity, mdev) {
  223. switch (entity->function) {
  224. case MEDIA_ENT_F_CONN_RF:
  225. if (!tuner)
  226. continue;
  227. pad_sink = media_get_pad_index(tuner, MEDIA_PAD_FL_SINK,
  228. PAD_SIGNAL_ANALOG);
  229. if (pad_sink < 0) {
  230. dev_warn(mdev->dev, "couldn't get tuner analog pad sink\n");
  231. return -EINVAL;
  232. }
  233. ret = media_create_pad_link(entity, 0, tuner,
  234. pad_sink,
  235. flags);
  236. break;
  237. case MEDIA_ENT_F_CONN_SVIDEO:
  238. case MEDIA_ENT_F_CONN_COMPOSITE:
  239. pad_sink = media_get_pad_index(decoder,
  240. MEDIA_PAD_FL_SINK,
  241. PAD_SIGNAL_ANALOG);
  242. if (pad_sink < 0) {
  243. dev_warn(mdev->dev, "couldn't get decoder analog pad sink\n");
  244. return -EINVAL;
  245. }
  246. ret = media_create_pad_link(entity, 0, decoder,
  247. pad_sink,
  248. flags);
  249. break;
  250. default:
  251. continue;
  252. }
  253. if (ret)
  254. return ret;
  255. flags = 0;
  256. }
  257. return 0;
  258. }
  259. EXPORT_SYMBOL_GPL(v4l2_mc_create_media_graph);
  260. int v4l_enable_media_source(struct video_device *vdev)
  261. {
  262. struct media_device *mdev = vdev->entity.graph_obj.mdev;
  263. int ret = 0, err;
  264. if (!mdev)
  265. return 0;
  266. mutex_lock(&mdev->graph_mutex);
  267. if (!mdev->enable_source)
  268. goto end;
  269. err = mdev->enable_source(&vdev->entity, &vdev->pipe);
  270. if (err)
  271. ret = -EBUSY;
  272. end:
  273. mutex_unlock(&mdev->graph_mutex);
  274. return ret;
  275. }
  276. EXPORT_SYMBOL_GPL(v4l_enable_media_source);
  277. void v4l_disable_media_source(struct video_device *vdev)
  278. {
  279. struct media_device *mdev = vdev->entity.graph_obj.mdev;
  280. if (mdev) {
  281. mutex_lock(&mdev->graph_mutex);
  282. if (mdev->disable_source)
  283. mdev->disable_source(&vdev->entity);
  284. mutex_unlock(&mdev->graph_mutex);
  285. }
  286. }
  287. EXPORT_SYMBOL_GPL(v4l_disable_media_source);
  288. int v4l_vb2q_enable_media_source(struct vb2_queue *q)
  289. {
  290. struct v4l2_fh *fh = q->owner;
  291. if (fh && fh->vdev)
  292. return v4l_enable_media_source(fh->vdev);
  293. return 0;
  294. }
  295. EXPORT_SYMBOL_GPL(v4l_vb2q_enable_media_source);
  296. int v4l2_create_fwnode_links_to_pad(struct v4l2_subdev *src_sd,
  297. struct media_pad *sink, u32 flags)
  298. {
  299. struct fwnode_handle *endpoint;
  300. if (!(sink->flags & MEDIA_PAD_FL_SINK))
  301. return -EINVAL;
  302. fwnode_graph_for_each_endpoint(src_sd->fwnode, endpoint) {
  303. struct fwnode_handle *remote_ep;
  304. int src_idx, sink_idx, ret;
  305. struct media_pad *src;
  306. src_idx = media_entity_get_fwnode_pad(&src_sd->entity,
  307. endpoint,
  308. MEDIA_PAD_FL_SOURCE);
  309. if (src_idx < 0) {
  310. dev_dbg(src_sd->dev, "no source pad found for %pfw\n",
  311. endpoint);
  312. continue;
  313. }
  314. remote_ep = fwnode_graph_get_remote_endpoint(endpoint);
  315. if (!remote_ep) {
  316. dev_dbg(src_sd->dev, "no remote ep found for %pfw\n",
  317. endpoint);
  318. continue;
  319. }
  320. /*
  321. * ask the sink to verify it owns the remote endpoint,
  322. * and translate to a sink pad.
  323. */
  324. sink_idx = media_entity_get_fwnode_pad(sink->entity,
  325. remote_ep,
  326. MEDIA_PAD_FL_SINK);
  327. fwnode_handle_put(remote_ep);
  328. if (sink_idx < 0 || sink_idx != sink->index) {
  329. dev_dbg(src_sd->dev,
  330. "sink pad index mismatch or error (is %d, expected %u)\n",
  331. sink_idx, sink->index);
  332. continue;
  333. }
  334. /*
  335. * the source endpoint corresponds to one of its source pads,
  336. * the source endpoint connects to an endpoint at the sink
  337. * entity, and the sink endpoint corresponds to the sink
  338. * pad requested, so we have found an endpoint connection
  339. * that works, create the media link for it.
  340. */
  341. src = &src_sd->entity.pads[src_idx];
  342. /* skip if link already exists */
  343. if (media_entity_find_link(src, sink)) {
  344. dev_dbg(src_sd->dev,
  345. "link %s:%d -> %s:%d already exists\n",
  346. src_sd->entity.name, src_idx,
  347. sink->entity->name, sink_idx);
  348. continue;
  349. }
  350. dev_dbg(src_sd->dev, "creating link %s:%d -> %s:%d\n",
  351. src_sd->entity.name, src_idx,
  352. sink->entity->name, sink_idx);
  353. ret = media_create_pad_link(&src_sd->entity, src_idx,
  354. sink->entity, sink_idx, flags);
  355. if (ret) {
  356. dev_err(src_sd->dev,
  357. "link %s:%d -> %s:%d failed with %d\n",
  358. src_sd->entity.name, src_idx,
  359. sink->entity->name, sink_idx, ret);
  360. fwnode_handle_put(endpoint);
  361. return ret;
  362. }
  363. }
  364. return 0;
  365. }
  366. EXPORT_SYMBOL_GPL(v4l2_create_fwnode_links_to_pad);
  367. int v4l2_create_fwnode_links(struct v4l2_subdev *src_sd,
  368. struct v4l2_subdev *sink_sd)
  369. {
  370. unsigned int i;
  371. for (i = 0; i < sink_sd->entity.num_pads; i++) {
  372. struct media_pad *pad = &sink_sd->entity.pads[i];
  373. int ret;
  374. if (!(pad->flags & MEDIA_PAD_FL_SINK))
  375. continue;
  376. ret = v4l2_create_fwnode_links_to_pad(src_sd, pad, 0);
  377. if (ret)
  378. return ret;
  379. }
  380. return 0;
  381. }
  382. EXPORT_SYMBOL_GPL(v4l2_create_fwnode_links);
  383. /* -----------------------------------------------------------------------------
  384. * Pipeline power management
  385. *
  386. * Entities must be powered up when part of a pipeline that contains at least
  387. * one open video device node.
  388. *
  389. * To achieve this use the entity use_count field to track the number of users.
  390. * For entities corresponding to video device nodes the use_count field stores
  391. * the users count of the node. For entities corresponding to subdevs the
  392. * use_count field stores the total number of users of all video device nodes
  393. * in the pipeline.
  394. *
  395. * The v4l2_pipeline_pm_{get, put}() functions must be called in the open() and
  396. * close() handlers of video device nodes. It increments or decrements the use
  397. * count of all subdev entities in the pipeline.
  398. *
  399. * To react to link management on powered pipelines, the link setup notification
  400. * callback updates the use count of all entities in the source and sink sides
  401. * of the link.
  402. */
  403. /*
  404. * pipeline_pm_use_count - Count the number of users of a pipeline
  405. * @entity: The entity
  406. *
  407. * Return the total number of users of all video device nodes in the pipeline.
  408. */
  409. static int pipeline_pm_use_count(struct media_entity *entity,
  410. struct media_graph *graph)
  411. {
  412. int use = 0;
  413. media_graph_walk_start(graph, entity);
  414. while ((entity = media_graph_walk_next(graph))) {
  415. if (is_media_entity_v4l2_video_device(entity))
  416. use += entity->use_count;
  417. }
  418. return use;
  419. }
  420. /*
  421. * pipeline_pm_power_one - Apply power change to an entity
  422. * @entity: The entity
  423. * @change: Use count change
  424. *
  425. * Change the entity use count by @change. If the entity is a subdev update its
  426. * power state by calling the core::s_power operation when the use count goes
  427. * from 0 to != 0 or from != 0 to 0.
  428. *
  429. * Return 0 on success or a negative error code on failure.
  430. */
  431. static int pipeline_pm_power_one(struct media_entity *entity, int change)
  432. {
  433. struct v4l2_subdev *subdev;
  434. int ret;
  435. subdev = is_media_entity_v4l2_subdev(entity)
  436. ? media_entity_to_v4l2_subdev(entity) : NULL;
  437. if (entity->use_count == 0 && change > 0 && subdev != NULL) {
  438. ret = v4l2_subdev_call(subdev, core, s_power, 1);
  439. if (ret < 0 && ret != -ENOIOCTLCMD)
  440. return ret;
  441. }
  442. entity->use_count += change;
  443. WARN_ON(entity->use_count < 0);
  444. if (entity->use_count == 0 && change < 0 && subdev != NULL)
  445. v4l2_subdev_call(subdev, core, s_power, 0);
  446. return 0;
  447. }
  448. /*
  449. * pipeline_pm_power - Apply power change to all entities in a pipeline
  450. * @entity: The entity
  451. * @change: Use count change
  452. *
  453. * Walk the pipeline to update the use count and the power state of all non-node
  454. * entities.
  455. *
  456. * Return 0 on success or a negative error code on failure.
  457. */
  458. static int pipeline_pm_power(struct media_entity *entity, int change,
  459. struct media_graph *graph)
  460. {
  461. struct media_entity *first = entity;
  462. int ret = 0;
  463. if (!change)
  464. return 0;
  465. media_graph_walk_start(graph, entity);
  466. while (!ret && (entity = media_graph_walk_next(graph)))
  467. if (is_media_entity_v4l2_subdev(entity))
  468. ret = pipeline_pm_power_one(entity, change);
  469. if (!ret)
  470. return ret;
  471. media_graph_walk_start(graph, first);
  472. while ((first = media_graph_walk_next(graph))
  473. && first != entity)
  474. if (is_media_entity_v4l2_subdev(first))
  475. pipeline_pm_power_one(first, -change);
  476. return ret;
  477. }
  478. static int v4l2_pipeline_pm_use(struct media_entity *entity, unsigned int use)
  479. {
  480. struct media_device *mdev = entity->graph_obj.mdev;
  481. int change = use ? 1 : -1;
  482. int ret;
  483. mutex_lock(&mdev->graph_mutex);
  484. /* Apply use count to node. */
  485. entity->use_count += change;
  486. WARN_ON(entity->use_count < 0);
  487. /* Apply power change to connected non-nodes. */
  488. ret = pipeline_pm_power(entity, change, &mdev->pm_count_walk);
  489. if (ret < 0)
  490. entity->use_count -= change;
  491. mutex_unlock(&mdev->graph_mutex);
  492. return ret;
  493. }
  494. int v4l2_pipeline_pm_get(struct media_entity *entity)
  495. {
  496. return v4l2_pipeline_pm_use(entity, 1);
  497. }
  498. EXPORT_SYMBOL_GPL(v4l2_pipeline_pm_get);
  499. void v4l2_pipeline_pm_put(struct media_entity *entity)
  500. {
  501. /* Powering off entities shouldn't fail. */
  502. WARN_ON(v4l2_pipeline_pm_use(entity, 0));
  503. }
  504. EXPORT_SYMBOL_GPL(v4l2_pipeline_pm_put);
  505. int v4l2_pipeline_link_notify(struct media_link *link, u32 flags,
  506. unsigned int notification)
  507. {
  508. struct media_graph *graph = &link->graph_obj.mdev->pm_count_walk;
  509. struct media_entity *source = link->source->entity;
  510. struct media_entity *sink = link->sink->entity;
  511. int source_use;
  512. int sink_use;
  513. int ret = 0;
  514. source_use = pipeline_pm_use_count(source, graph);
  515. sink_use = pipeline_pm_use_count(sink, graph);
  516. if (notification == MEDIA_DEV_NOTIFY_POST_LINK_CH &&
  517. !(flags & MEDIA_LNK_FL_ENABLED)) {
  518. /* Powering off entities is assumed to never fail. */
  519. pipeline_pm_power(source, -sink_use, graph);
  520. pipeline_pm_power(sink, -source_use, graph);
  521. return 0;
  522. }
  523. if (notification == MEDIA_DEV_NOTIFY_PRE_LINK_CH &&
  524. (flags & MEDIA_LNK_FL_ENABLED)) {
  525. ret = pipeline_pm_power(source, sink_use, graph);
  526. if (ret < 0)
  527. return ret;
  528. ret = pipeline_pm_power(sink, source_use, graph);
  529. if (ret < 0)
  530. pipeline_pm_power(source, -sink_use, graph);
  531. }
  532. return ret;
  533. }
  534. EXPORT_SYMBOL_GPL(v4l2_pipeline_link_notify);