v4l2-mc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * Media Controller ancillary functions
  3. *
  4. * Copyright (c) 2016 Mauro Carvalho Chehab <mchehab@kernel.org>
  5. * Copyright (C) 2016 Shuah Khan <shuahkh@osg.samsung.com>
  6. * Copyright (C) 2006-2010 Nokia Corporation
  7. * Copyright (c) 2016 Intel Corporation.
  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 as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/pci.h>
  21. #include <linux/usb.h>
  22. #include <media/media-device.h>
  23. #include <media/media-entity.h>
  24. #include <media/v4l2-fh.h>
  25. #include <media/v4l2-mc.h>
  26. #include <media/v4l2-subdev.h>
  27. #include <media/media-device.h>
  28. #include <media/v4l2-mc.h>
  29. #include <media/videobuf2-core.h>
  30. int v4l2_mc_create_media_graph(struct media_device *mdev)
  31. {
  32. struct media_entity *entity;
  33. struct media_entity *if_vid = NULL, *if_aud = NULL;
  34. struct media_entity *tuner = NULL, *decoder = NULL;
  35. struct media_entity *io_v4l = NULL, *io_vbi = NULL, *io_swradio = NULL;
  36. bool is_webcam = false;
  37. u32 flags;
  38. int ret;
  39. if (!mdev)
  40. return 0;
  41. media_device_for_each_entity(entity, mdev) {
  42. switch (entity->function) {
  43. case MEDIA_ENT_F_IF_VID_DECODER:
  44. if_vid = entity;
  45. break;
  46. case MEDIA_ENT_F_IF_AUD_DECODER:
  47. if_aud = entity;
  48. break;
  49. case MEDIA_ENT_F_TUNER:
  50. tuner = entity;
  51. break;
  52. case MEDIA_ENT_F_ATV_DECODER:
  53. decoder = entity;
  54. break;
  55. case MEDIA_ENT_F_IO_V4L:
  56. io_v4l = entity;
  57. break;
  58. case MEDIA_ENT_F_IO_VBI:
  59. io_vbi = entity;
  60. break;
  61. case MEDIA_ENT_F_IO_SWRADIO:
  62. io_swradio = entity;
  63. break;
  64. case MEDIA_ENT_F_CAM_SENSOR:
  65. is_webcam = true;
  66. break;
  67. }
  68. }
  69. /* It should have at least one I/O entity */
  70. if (!io_v4l && !io_vbi && !io_swradio)
  71. return -EINVAL;
  72. /*
  73. * Here, webcams are modelled on a very simple way: the sensor is
  74. * connected directly to the I/O entity. All dirty details, like
  75. * scaler and crop HW are hidden. While such mapping is not enough
  76. * for mc-centric hardware, it is enough for v4l2 interface centric
  77. * PC-consumer's hardware.
  78. */
  79. if (is_webcam) {
  80. if (!io_v4l)
  81. return -EINVAL;
  82. media_device_for_each_entity(entity, mdev) {
  83. if (entity->function != MEDIA_ENT_F_CAM_SENSOR)
  84. continue;
  85. ret = media_create_pad_link(entity, 0,
  86. io_v4l, 0,
  87. MEDIA_LNK_FL_ENABLED);
  88. if (ret)
  89. return ret;
  90. }
  91. if (!decoder)
  92. return 0;
  93. }
  94. /* The device isn't a webcam. So, it should have a decoder */
  95. if (!decoder)
  96. return -EINVAL;
  97. /* Link the tuner and IF video output pads */
  98. if (tuner) {
  99. if (if_vid) {
  100. ret = media_create_pad_link(tuner, TUNER_PAD_OUTPUT,
  101. if_vid,
  102. IF_VID_DEC_PAD_IF_INPUT,
  103. MEDIA_LNK_FL_ENABLED);
  104. if (ret)
  105. return ret;
  106. ret = media_create_pad_link(if_vid, IF_VID_DEC_PAD_OUT,
  107. decoder, DEMOD_PAD_IF_INPUT,
  108. MEDIA_LNK_FL_ENABLED);
  109. if (ret)
  110. return ret;
  111. } else {
  112. ret = media_create_pad_link(tuner, TUNER_PAD_OUTPUT,
  113. decoder, DEMOD_PAD_IF_INPUT,
  114. MEDIA_LNK_FL_ENABLED);
  115. if (ret)
  116. return ret;
  117. }
  118. if (if_aud) {
  119. ret = media_create_pad_link(tuner, TUNER_PAD_AUD_OUT,
  120. if_aud,
  121. IF_AUD_DEC_PAD_IF_INPUT,
  122. MEDIA_LNK_FL_ENABLED);
  123. if (ret)
  124. return ret;
  125. } else {
  126. if_aud = tuner;
  127. }
  128. }
  129. /* Create demod to V4L, VBI and SDR radio links */
  130. if (io_v4l) {
  131. ret = media_create_pad_link(decoder, DEMOD_PAD_VID_OUT,
  132. io_v4l, 0,
  133. MEDIA_LNK_FL_ENABLED);
  134. if (ret)
  135. return ret;
  136. }
  137. if (io_swradio) {
  138. ret = media_create_pad_link(decoder, DEMOD_PAD_VID_OUT,
  139. io_swradio, 0,
  140. MEDIA_LNK_FL_ENABLED);
  141. if (ret)
  142. return ret;
  143. }
  144. if (io_vbi) {
  145. ret = media_create_pad_link(decoder, DEMOD_PAD_VBI_OUT,
  146. io_vbi, 0,
  147. MEDIA_LNK_FL_ENABLED);
  148. if (ret)
  149. return ret;
  150. }
  151. /* Create links for the media connectors */
  152. flags = MEDIA_LNK_FL_ENABLED;
  153. media_device_for_each_entity(entity, mdev) {
  154. switch (entity->function) {
  155. case MEDIA_ENT_F_CONN_RF:
  156. if (!tuner)
  157. continue;
  158. ret = media_create_pad_link(entity, 0, tuner,
  159. TUNER_PAD_RF_INPUT,
  160. flags);
  161. break;
  162. case MEDIA_ENT_F_CONN_SVIDEO:
  163. case MEDIA_ENT_F_CONN_COMPOSITE:
  164. ret = media_create_pad_link(entity, 0, decoder,
  165. DEMOD_PAD_IF_INPUT,
  166. flags);
  167. break;
  168. default:
  169. continue;
  170. }
  171. if (ret)
  172. return ret;
  173. flags = 0;
  174. }
  175. return 0;
  176. }
  177. EXPORT_SYMBOL_GPL(v4l2_mc_create_media_graph);
  178. int v4l_enable_media_source(struct video_device *vdev)
  179. {
  180. struct media_device *mdev = vdev->entity.graph_obj.mdev;
  181. int ret;
  182. if (!mdev || !mdev->enable_source)
  183. return 0;
  184. ret = mdev->enable_source(&vdev->entity, &vdev->pipe);
  185. if (ret)
  186. return -EBUSY;
  187. return 0;
  188. }
  189. EXPORT_SYMBOL_GPL(v4l_enable_media_source);
  190. void v4l_disable_media_source(struct video_device *vdev)
  191. {
  192. struct media_device *mdev = vdev->entity.graph_obj.mdev;
  193. if (mdev && mdev->disable_source)
  194. mdev->disable_source(&vdev->entity);
  195. }
  196. EXPORT_SYMBOL_GPL(v4l_disable_media_source);
  197. int v4l_vb2q_enable_media_source(struct vb2_queue *q)
  198. {
  199. struct v4l2_fh *fh = q->owner;
  200. if (fh && fh->vdev)
  201. return v4l_enable_media_source(fh->vdev);
  202. return 0;
  203. }
  204. EXPORT_SYMBOL_GPL(v4l_vb2q_enable_media_source);
  205. /* -----------------------------------------------------------------------------
  206. * Pipeline power management
  207. *
  208. * Entities must be powered up when part of a pipeline that contains at least
  209. * one open video device node.
  210. *
  211. * To achieve this use the entity use_count field to track the number of users.
  212. * For entities corresponding to video device nodes the use_count field stores
  213. * the users count of the node. For entities corresponding to subdevs the
  214. * use_count field stores the total number of users of all video device nodes
  215. * in the pipeline.
  216. *
  217. * The v4l2_pipeline_pm_use() function must be called in the open() and
  218. * close() handlers of video device nodes. It increments or decrements the use
  219. * count of all subdev entities in the pipeline.
  220. *
  221. * To react to link management on powered pipelines, the link setup notification
  222. * callback updates the use count of all entities in the source and sink sides
  223. * of the link.
  224. */
  225. /*
  226. * pipeline_pm_use_count - Count the number of users of a pipeline
  227. * @entity: The entity
  228. *
  229. * Return the total number of users of all video device nodes in the pipeline.
  230. */
  231. static int pipeline_pm_use_count(struct media_entity *entity,
  232. struct media_entity_graph *graph)
  233. {
  234. int use = 0;
  235. media_entity_graph_walk_start(graph, entity);
  236. while ((entity = media_entity_graph_walk_next(graph))) {
  237. if (is_media_entity_v4l2_video_device(entity))
  238. use += entity->use_count;
  239. }
  240. return use;
  241. }
  242. /*
  243. * pipeline_pm_power_one - Apply power change to an entity
  244. * @entity: The entity
  245. * @change: Use count change
  246. *
  247. * Change the entity use count by @change. If the entity is a subdev update its
  248. * power state by calling the core::s_power operation when the use count goes
  249. * from 0 to != 0 or from != 0 to 0.
  250. *
  251. * Return 0 on success or a negative error code on failure.
  252. */
  253. static int pipeline_pm_power_one(struct media_entity *entity, int change)
  254. {
  255. struct v4l2_subdev *subdev;
  256. int ret;
  257. subdev = is_media_entity_v4l2_subdev(entity)
  258. ? media_entity_to_v4l2_subdev(entity) : NULL;
  259. if (entity->use_count == 0 && change > 0 && subdev != NULL) {
  260. ret = v4l2_subdev_call(subdev, core, s_power, 1);
  261. if (ret < 0 && ret != -ENOIOCTLCMD)
  262. return ret;
  263. }
  264. entity->use_count += change;
  265. WARN_ON(entity->use_count < 0);
  266. if (entity->use_count == 0 && change < 0 && subdev != NULL)
  267. v4l2_subdev_call(subdev, core, s_power, 0);
  268. return 0;
  269. }
  270. /*
  271. * pipeline_pm_power - Apply power change to all entities in a pipeline
  272. * @entity: The entity
  273. * @change: Use count change
  274. *
  275. * Walk the pipeline to update the use count and the power state of all non-node
  276. * entities.
  277. *
  278. * Return 0 on success or a negative error code on failure.
  279. */
  280. static int pipeline_pm_power(struct media_entity *entity, int change,
  281. struct media_entity_graph *graph)
  282. {
  283. struct media_entity *first = entity;
  284. int ret = 0;
  285. if (!change)
  286. return 0;
  287. media_entity_graph_walk_start(graph, entity);
  288. while (!ret && (entity = media_entity_graph_walk_next(graph)))
  289. if (is_media_entity_v4l2_subdev(entity))
  290. ret = pipeline_pm_power_one(entity, change);
  291. if (!ret)
  292. return ret;
  293. media_entity_graph_walk_start(graph, first);
  294. while ((first = media_entity_graph_walk_next(graph))
  295. && first != entity)
  296. if (is_media_entity_v4l2_subdev(first))
  297. pipeline_pm_power_one(first, -change);
  298. return ret;
  299. }
  300. int v4l2_pipeline_pm_use(struct media_entity *entity, int use)
  301. {
  302. struct media_device *mdev = entity->graph_obj.mdev;
  303. int change = use ? 1 : -1;
  304. int ret;
  305. mutex_lock(&mdev->graph_mutex);
  306. /* Apply use count to node. */
  307. entity->use_count += change;
  308. WARN_ON(entity->use_count < 0);
  309. /* Apply power change to connected non-nodes. */
  310. ret = pipeline_pm_power(entity, change, &mdev->pm_count_walk);
  311. if (ret < 0)
  312. entity->use_count -= change;
  313. mutex_unlock(&mdev->graph_mutex);
  314. return ret;
  315. }
  316. EXPORT_SYMBOL_GPL(v4l2_pipeline_pm_use);
  317. int v4l2_pipeline_link_notify(struct media_link *link, u32 flags,
  318. unsigned int notification)
  319. {
  320. struct media_entity_graph *graph = &link->graph_obj.mdev->pm_count_walk;
  321. struct media_entity *source = link->source->entity;
  322. struct media_entity *sink = link->sink->entity;
  323. int source_use;
  324. int sink_use;
  325. int ret = 0;
  326. source_use = pipeline_pm_use_count(source, graph);
  327. sink_use = pipeline_pm_use_count(sink, graph);
  328. if (notification == MEDIA_DEV_NOTIFY_POST_LINK_CH &&
  329. !(flags & MEDIA_LNK_FL_ENABLED)) {
  330. /* Powering off entities is assumed to never fail. */
  331. pipeline_pm_power(source, -sink_use, graph);
  332. pipeline_pm_power(sink, -source_use, graph);
  333. return 0;
  334. }
  335. if (notification == MEDIA_DEV_NOTIFY_PRE_LINK_CH &&
  336. (flags & MEDIA_LNK_FL_ENABLED)) {
  337. ret = pipeline_pm_power(source, sink_use, graph);
  338. if (ret < 0)
  339. return ret;
  340. ret = pipeline_pm_power(sink, source_use, graph);
  341. if (ret < 0)
  342. pipeline_pm_power(source, -sink_use, graph);
  343. }
  344. return ret;
  345. }
  346. EXPORT_SYMBOL_GPL(v4l2_pipeline_link_notify);