v4l2-mc.c 10 KB

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