v4l2-mc.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Media Controller ancillary functions
  3. *
  4. * Copyright (c) 2016 Mauro Carvalho Chehab <mchehab@osg.samsung.com>
  5. * Copyright (C) 2016 Shuah Khan <shuahkh@osg.samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/module.h>
  18. #include <media/media-entity.h>
  19. #include <media/media-device.h>
  20. #include <media/v4l2-fh.h>
  21. #include <media/v4l2-mc.h>
  22. #include <media/videobuf2-core.h>
  23. int v4l2_mc_create_media_graph(struct media_device *mdev)
  24. {
  25. struct media_entity *entity;
  26. struct media_entity *if_vid = NULL, *if_aud = NULL;
  27. struct media_entity *tuner = NULL, *decoder = NULL;
  28. struct media_entity *io_v4l = NULL, *io_vbi = NULL, *io_swradio = NULL;
  29. bool is_webcam = false;
  30. u32 flags;
  31. int ret;
  32. if (!mdev)
  33. return 0;
  34. media_device_for_each_entity(entity, mdev) {
  35. switch (entity->function) {
  36. case MEDIA_ENT_F_IF_VID_DECODER:
  37. if_vid = entity;
  38. break;
  39. case MEDIA_ENT_F_IF_AUD_DECODER:
  40. if_aud = entity;
  41. break;
  42. case MEDIA_ENT_F_TUNER:
  43. tuner = entity;
  44. break;
  45. case MEDIA_ENT_F_ATV_DECODER:
  46. decoder = entity;
  47. break;
  48. case MEDIA_ENT_F_IO_V4L:
  49. io_v4l = entity;
  50. break;
  51. case MEDIA_ENT_F_IO_VBI:
  52. io_vbi = entity;
  53. break;
  54. case MEDIA_ENT_F_IO_SWRADIO:
  55. io_swradio = entity;
  56. break;
  57. case MEDIA_ENT_F_CAM_SENSOR:
  58. is_webcam = true;
  59. break;
  60. }
  61. }
  62. /* It should have at least one I/O entity */
  63. if (!io_v4l && !io_vbi && !io_swradio)
  64. return -EINVAL;
  65. /*
  66. * Here, webcams are modelled on a very simple way: the sensor is
  67. * connected directly to the I/O entity. All dirty details, like
  68. * scaler and crop HW are hidden. While such mapping is not enough
  69. * for mc-centric hardware, it is enough for v4l2 interface centric
  70. * PC-consumer's hardware.
  71. */
  72. if (is_webcam) {
  73. if (!io_v4l)
  74. return -EINVAL;
  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. return ret;
  83. }
  84. if (!decoder)
  85. return 0;
  86. }
  87. /* The device isn't a webcam. So, it should have a decoder */
  88. if (!decoder)
  89. return -EINVAL;
  90. /* Link the tuner and IF video output pads */
  91. if (tuner) {
  92. if (if_vid) {
  93. ret = media_create_pad_link(tuner, TUNER_PAD_OUTPUT,
  94. if_vid,
  95. IF_VID_DEC_PAD_IF_INPUT,
  96. MEDIA_LNK_FL_ENABLED);
  97. if (ret)
  98. return ret;
  99. ret = media_create_pad_link(if_vid, IF_VID_DEC_PAD_OUT,
  100. decoder, DEMOD_PAD_IF_INPUT,
  101. MEDIA_LNK_FL_ENABLED);
  102. if (ret)
  103. return ret;
  104. } else {
  105. ret = media_create_pad_link(tuner, TUNER_PAD_OUTPUT,
  106. decoder, DEMOD_PAD_IF_INPUT,
  107. MEDIA_LNK_FL_ENABLED);
  108. if (ret)
  109. return ret;
  110. }
  111. if (if_aud) {
  112. ret = media_create_pad_link(tuner, TUNER_PAD_AUD_OUT,
  113. if_aud,
  114. IF_AUD_DEC_PAD_IF_INPUT,
  115. MEDIA_LNK_FL_ENABLED);
  116. if (ret)
  117. return ret;
  118. } else {
  119. if_aud = tuner;
  120. }
  121. }
  122. /* Create demod to V4L, VBI and SDR radio links */
  123. if (io_v4l) {
  124. ret = media_create_pad_link(decoder, DEMOD_PAD_VID_OUT,
  125. io_v4l, 0,
  126. MEDIA_LNK_FL_ENABLED);
  127. if (ret)
  128. return ret;
  129. }
  130. if (io_swradio) {
  131. ret = media_create_pad_link(decoder, DEMOD_PAD_VID_OUT,
  132. io_swradio, 0,
  133. MEDIA_LNK_FL_ENABLED);
  134. if (ret)
  135. return ret;
  136. }
  137. if (io_vbi) {
  138. ret = media_create_pad_link(decoder, DEMOD_PAD_VBI_OUT,
  139. io_vbi, 0,
  140. MEDIA_LNK_FL_ENABLED);
  141. if (ret)
  142. return ret;
  143. }
  144. /* Create links for the media connectors */
  145. flags = MEDIA_LNK_FL_ENABLED;
  146. media_device_for_each_entity(entity, mdev) {
  147. switch (entity->function) {
  148. case MEDIA_ENT_F_CONN_RF:
  149. if (!tuner)
  150. continue;
  151. ret = media_create_pad_link(entity, 0, tuner,
  152. TUNER_PAD_RF_INPUT,
  153. flags);
  154. break;
  155. case MEDIA_ENT_F_CONN_SVIDEO:
  156. case MEDIA_ENT_F_CONN_COMPOSITE:
  157. ret = media_create_pad_link(entity, 0, decoder,
  158. DEMOD_PAD_IF_INPUT,
  159. flags);
  160. break;
  161. default:
  162. continue;
  163. }
  164. if (ret)
  165. return ret;
  166. flags = 0;
  167. }
  168. return 0;
  169. }
  170. EXPORT_SYMBOL_GPL(v4l2_mc_create_media_graph);
  171. int v4l_enable_media_source(struct video_device *vdev)
  172. {
  173. struct media_device *mdev = vdev->entity.graph_obj.mdev;
  174. int ret;
  175. if (!mdev || !mdev->enable_source)
  176. return 0;
  177. ret = mdev->enable_source(&vdev->entity, &vdev->pipe);
  178. if (ret)
  179. return -EBUSY;
  180. return 0;
  181. }
  182. EXPORT_SYMBOL_GPL(v4l_enable_media_source);
  183. void v4l_disable_media_source(struct video_device *vdev)
  184. {
  185. struct media_device *mdev = vdev->entity.graph_obj.mdev;
  186. if (mdev && mdev->disable_source)
  187. mdev->disable_source(&vdev->entity);
  188. }
  189. EXPORT_SYMBOL_GPL(v4l_disable_media_source);
  190. int v4l_vb2q_enable_media_source(struct vb2_queue *q)
  191. {
  192. struct v4l2_fh *fh = q->owner;
  193. return v4l_enable_media_source(fh->vdev);
  194. }
  195. EXPORT_SYMBOL_GPL(v4l_vb2q_enable_media_source);