vimc-capture.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * vimc-capture.c Virtual Media Controller Driver
  4. *
  5. * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
  6. */
  7. #include <media/v4l2-ioctl.h>
  8. #include <media/videobuf2-core.h>
  9. #include <media/videobuf2-dma-contig.h>
  10. #include <media/videobuf2-vmalloc.h>
  11. #include "vimc-common.h"
  12. #include "vimc-streamer.h"
  13. struct vimc_capture_device {
  14. struct vimc_ent_device ved;
  15. struct video_device vdev;
  16. struct v4l2_pix_format format;
  17. struct vb2_queue queue;
  18. struct list_head buf_list;
  19. /*
  20. * NOTE: in a real driver, a spin lock must be used to access the
  21. * queue because the frames are generated from a hardware interruption
  22. * and the isr is not allowed to sleep.
  23. * Even if it is not necessary a spinlock in the vimc driver, we
  24. * use it here as a code reference
  25. */
  26. spinlock_t qlock;
  27. struct mutex lock;
  28. u32 sequence;
  29. struct vimc_stream stream;
  30. struct media_pad pad;
  31. };
  32. static const struct v4l2_pix_format fmt_default = {
  33. .width = 640,
  34. .height = 480,
  35. .pixelformat = V4L2_PIX_FMT_RGB24,
  36. .field = V4L2_FIELD_NONE,
  37. .colorspace = V4L2_COLORSPACE_SRGB,
  38. };
  39. struct vimc_capture_buffer {
  40. /*
  41. * struct vb2_v4l2_buffer must be the first element
  42. * the videobuf2 framework will allocate this struct based on
  43. * buf_struct_size and use the first sizeof(struct vb2_buffer) bytes of
  44. * memory as a vb2_buffer
  45. */
  46. struct vb2_v4l2_buffer vb2;
  47. struct list_head list;
  48. };
  49. static int vimc_capture_querycap(struct file *file, void *priv,
  50. struct v4l2_capability *cap)
  51. {
  52. strscpy(cap->driver, VIMC_PDEV_NAME, sizeof(cap->driver));
  53. strscpy(cap->card, KBUILD_MODNAME, sizeof(cap->card));
  54. snprintf(cap->bus_info, sizeof(cap->bus_info),
  55. "platform:%s", VIMC_PDEV_NAME);
  56. return 0;
  57. }
  58. static void vimc_capture_get_format(struct vimc_ent_device *ved,
  59. struct v4l2_pix_format *fmt)
  60. {
  61. struct vimc_capture_device *vcapture = container_of(ved, struct vimc_capture_device,
  62. ved);
  63. *fmt = vcapture->format;
  64. }
  65. static int vimc_capture_g_fmt_vid_cap(struct file *file, void *priv,
  66. struct v4l2_format *f)
  67. {
  68. struct vimc_capture_device *vcapture = video_drvdata(file);
  69. f->fmt.pix = vcapture->format;
  70. return 0;
  71. }
  72. static int vimc_capture_try_fmt_vid_cap(struct file *file, void *priv,
  73. struct v4l2_format *f)
  74. {
  75. struct v4l2_pix_format *format = &f->fmt.pix;
  76. const struct vimc_pix_map *vpix;
  77. format->width = clamp_t(u32, format->width, VIMC_FRAME_MIN_WIDTH,
  78. VIMC_FRAME_MAX_WIDTH) & ~1;
  79. format->height = clamp_t(u32, format->height, VIMC_FRAME_MIN_HEIGHT,
  80. VIMC_FRAME_MAX_HEIGHT) & ~1;
  81. /* Don't accept a pixelformat that is not on the table */
  82. vpix = vimc_pix_map_by_pixelformat(format->pixelformat);
  83. if (!vpix) {
  84. format->pixelformat = fmt_default.pixelformat;
  85. vpix = vimc_pix_map_by_pixelformat(format->pixelformat);
  86. }
  87. /* TODO: Add support for custom bytesperline values */
  88. format->bytesperline = format->width * vpix->bpp;
  89. format->sizeimage = format->bytesperline * format->height;
  90. if (format->field == V4L2_FIELD_ANY)
  91. format->field = fmt_default.field;
  92. vimc_colorimetry_clamp(format);
  93. if (format->colorspace == V4L2_COLORSPACE_DEFAULT)
  94. format->colorspace = fmt_default.colorspace;
  95. return 0;
  96. }
  97. static int vimc_capture_s_fmt_vid_cap(struct file *file, void *priv,
  98. struct v4l2_format *f)
  99. {
  100. struct vimc_capture_device *vcapture = video_drvdata(file);
  101. int ret;
  102. /* Do not change the format while stream is on */
  103. if (vb2_is_busy(&vcapture->queue))
  104. return -EBUSY;
  105. ret = vimc_capture_try_fmt_vid_cap(file, priv, f);
  106. if (ret)
  107. return ret;
  108. dev_dbg(vcapture->ved.dev, "%s: format update: "
  109. "old:%dx%d (0x%x, %d, %d, %d, %d) "
  110. "new:%dx%d (0x%x, %d, %d, %d, %d)\n", vcapture->vdev.name,
  111. /* old */
  112. vcapture->format.width, vcapture->format.height,
  113. vcapture->format.pixelformat, vcapture->format.colorspace,
  114. vcapture->format.quantization, vcapture->format.xfer_func,
  115. vcapture->format.ycbcr_enc,
  116. /* new */
  117. f->fmt.pix.width, f->fmt.pix.height,
  118. f->fmt.pix.pixelformat, f->fmt.pix.colorspace,
  119. f->fmt.pix.quantization, f->fmt.pix.xfer_func,
  120. f->fmt.pix.ycbcr_enc);
  121. vcapture->format = f->fmt.pix;
  122. return 0;
  123. }
  124. static int vimc_capture_enum_fmt_vid_cap(struct file *file, void *priv,
  125. struct v4l2_fmtdesc *f)
  126. {
  127. const struct vimc_pix_map *vpix;
  128. if (f->mbus_code) {
  129. if (f->index > 0)
  130. return -EINVAL;
  131. vpix = vimc_pix_map_by_code(f->mbus_code);
  132. } else {
  133. vpix = vimc_pix_map_by_index(f->index);
  134. }
  135. if (!vpix)
  136. return -EINVAL;
  137. f->pixelformat = vpix->pixelformat;
  138. return 0;
  139. }
  140. static int vimc_capture_enum_framesizes(struct file *file, void *fh,
  141. struct v4l2_frmsizeenum *fsize)
  142. {
  143. const struct vimc_pix_map *vpix;
  144. if (fsize->index)
  145. return -EINVAL;
  146. /* Only accept code in the pix map table */
  147. vpix = vimc_pix_map_by_code(fsize->pixel_format);
  148. if (!vpix)
  149. return -EINVAL;
  150. fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
  151. fsize->stepwise.min_width = VIMC_FRAME_MIN_WIDTH;
  152. fsize->stepwise.max_width = VIMC_FRAME_MAX_WIDTH;
  153. fsize->stepwise.min_height = VIMC_FRAME_MIN_HEIGHT;
  154. fsize->stepwise.max_height = VIMC_FRAME_MAX_HEIGHT;
  155. fsize->stepwise.step_width = 1;
  156. fsize->stepwise.step_height = 1;
  157. return 0;
  158. }
  159. static const struct v4l2_file_operations vimc_capture_fops = {
  160. .owner = THIS_MODULE,
  161. .open = v4l2_fh_open,
  162. .release = vb2_fop_release,
  163. .read = vb2_fop_read,
  164. .poll = vb2_fop_poll,
  165. .unlocked_ioctl = video_ioctl2,
  166. .mmap = vb2_fop_mmap,
  167. };
  168. static const struct v4l2_ioctl_ops vimc_capture_ioctl_ops = {
  169. .vidioc_querycap = vimc_capture_querycap,
  170. .vidioc_g_fmt_vid_cap = vimc_capture_g_fmt_vid_cap,
  171. .vidioc_s_fmt_vid_cap = vimc_capture_s_fmt_vid_cap,
  172. .vidioc_try_fmt_vid_cap = vimc_capture_try_fmt_vid_cap,
  173. .vidioc_enum_fmt_vid_cap = vimc_capture_enum_fmt_vid_cap,
  174. .vidioc_enum_framesizes = vimc_capture_enum_framesizes,
  175. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  176. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  177. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  178. .vidioc_querybuf = vb2_ioctl_querybuf,
  179. .vidioc_qbuf = vb2_ioctl_qbuf,
  180. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  181. .vidioc_expbuf = vb2_ioctl_expbuf,
  182. .vidioc_streamon = vb2_ioctl_streamon,
  183. .vidioc_streamoff = vb2_ioctl_streamoff,
  184. .vidioc_remove_bufs = vb2_ioctl_remove_bufs,
  185. };
  186. static void vimc_capture_return_all_buffers(struct vimc_capture_device *vcapture,
  187. enum vb2_buffer_state state)
  188. {
  189. struct vimc_capture_buffer *vbuf, *node;
  190. spin_lock(&vcapture->qlock);
  191. list_for_each_entry_safe(vbuf, node, &vcapture->buf_list, list) {
  192. list_del(&vbuf->list);
  193. vb2_buffer_done(&vbuf->vb2.vb2_buf, state);
  194. }
  195. spin_unlock(&vcapture->qlock);
  196. }
  197. static int vimc_capture_start_streaming(struct vb2_queue *vq, unsigned int count)
  198. {
  199. struct vimc_capture_device *vcapture = vb2_get_drv_priv(vq);
  200. int ret;
  201. vcapture->sequence = 0;
  202. /* Start the media pipeline */
  203. ret = video_device_pipeline_start(&vcapture->vdev, &vcapture->stream.pipe);
  204. if (ret) {
  205. vimc_capture_return_all_buffers(vcapture, VB2_BUF_STATE_QUEUED);
  206. return ret;
  207. }
  208. ret = vimc_streamer_s_stream(&vcapture->stream, &vcapture->ved, 1);
  209. if (ret) {
  210. video_device_pipeline_stop(&vcapture->vdev);
  211. vimc_capture_return_all_buffers(vcapture, VB2_BUF_STATE_QUEUED);
  212. return ret;
  213. }
  214. return 0;
  215. }
  216. /*
  217. * Stop the stream engine. Any remaining buffers in the stream queue are
  218. * dequeued and passed on to the vb2 framework marked as STATE_ERROR.
  219. */
  220. static void vimc_capture_stop_streaming(struct vb2_queue *vq)
  221. {
  222. struct vimc_capture_device *vcapture = vb2_get_drv_priv(vq);
  223. vimc_streamer_s_stream(&vcapture->stream, &vcapture->ved, 0);
  224. /* Stop the media pipeline */
  225. video_device_pipeline_stop(&vcapture->vdev);
  226. /* Release all active buffers */
  227. vimc_capture_return_all_buffers(vcapture, VB2_BUF_STATE_ERROR);
  228. }
  229. static void vimc_capture_buf_queue(struct vb2_buffer *vb2_buf)
  230. {
  231. struct vimc_capture_device *vcapture = vb2_get_drv_priv(vb2_buf->vb2_queue);
  232. struct vimc_capture_buffer *buf = container_of(vb2_buf,
  233. struct vimc_capture_buffer,
  234. vb2.vb2_buf);
  235. spin_lock(&vcapture->qlock);
  236. list_add_tail(&buf->list, &vcapture->buf_list);
  237. spin_unlock(&vcapture->qlock);
  238. }
  239. static int vimc_capture_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers,
  240. unsigned int *nplanes, unsigned int sizes[],
  241. struct device *alloc_devs[])
  242. {
  243. struct vimc_capture_device *vcapture = vb2_get_drv_priv(vq);
  244. if (*nplanes)
  245. return sizes[0] < vcapture->format.sizeimage ? -EINVAL : 0;
  246. /* We don't support multiplanes for now */
  247. *nplanes = 1;
  248. sizes[0] = vcapture->format.sizeimage;
  249. return 0;
  250. }
  251. static int vimc_capture_buffer_prepare(struct vb2_buffer *vb)
  252. {
  253. struct vimc_capture_device *vcapture = vb2_get_drv_priv(vb->vb2_queue);
  254. unsigned long size = vcapture->format.sizeimage;
  255. if (vb2_plane_size(vb, 0) < size) {
  256. dev_err(vcapture->ved.dev, "%s: buffer too small (%lu < %lu)\n",
  257. vcapture->vdev.name, vb2_plane_size(vb, 0), size);
  258. return -EINVAL;
  259. }
  260. return 0;
  261. }
  262. static const struct vb2_ops vimc_capture_qops = {
  263. .start_streaming = vimc_capture_start_streaming,
  264. .stop_streaming = vimc_capture_stop_streaming,
  265. .buf_queue = vimc_capture_buf_queue,
  266. .queue_setup = vimc_capture_queue_setup,
  267. .buf_prepare = vimc_capture_buffer_prepare,
  268. /*
  269. * Since q->lock is set we can use the standard
  270. * vb2_ops_wait_prepare/finish helper functions.
  271. */
  272. .wait_prepare = vb2_ops_wait_prepare,
  273. .wait_finish = vb2_ops_wait_finish,
  274. };
  275. static const struct media_entity_operations vimc_capture_mops = {
  276. .link_validate = vimc_vdev_link_validate,
  277. };
  278. static void vimc_capture_release(struct vimc_ent_device *ved)
  279. {
  280. struct vimc_capture_device *vcapture =
  281. container_of(ved, struct vimc_capture_device, ved);
  282. media_entity_cleanup(vcapture->ved.ent);
  283. kfree(vcapture);
  284. }
  285. static void vimc_capture_unregister(struct vimc_ent_device *ved)
  286. {
  287. struct vimc_capture_device *vcapture =
  288. container_of(ved, struct vimc_capture_device, ved);
  289. vb2_video_unregister_device(&vcapture->vdev);
  290. }
  291. static void *vimc_capture_process_frame(struct vimc_ent_device *ved,
  292. const void *frame)
  293. {
  294. struct vimc_capture_device *vcapture = container_of(ved, struct vimc_capture_device,
  295. ved);
  296. struct vimc_capture_buffer *vimc_buf;
  297. void *vbuf;
  298. spin_lock(&vcapture->qlock);
  299. /* Get the first entry of the list */
  300. vimc_buf = list_first_entry_or_null(&vcapture->buf_list,
  301. typeof(*vimc_buf), list);
  302. if (!vimc_buf) {
  303. spin_unlock(&vcapture->qlock);
  304. return ERR_PTR(-EAGAIN);
  305. }
  306. /* Remove this entry from the list */
  307. list_del(&vimc_buf->list);
  308. spin_unlock(&vcapture->qlock);
  309. /* Fill the buffer */
  310. vimc_buf->vb2.vb2_buf.timestamp = ktime_get_ns();
  311. vimc_buf->vb2.sequence = vcapture->sequence++;
  312. vimc_buf->vb2.field = vcapture->format.field;
  313. vbuf = vb2_plane_vaddr(&vimc_buf->vb2.vb2_buf, 0);
  314. memcpy(vbuf, frame, vcapture->format.sizeimage);
  315. /* Set it as ready */
  316. vb2_set_plane_payload(&vimc_buf->vb2.vb2_buf, 0,
  317. vcapture->format.sizeimage);
  318. vb2_buffer_done(&vimc_buf->vb2.vb2_buf, VB2_BUF_STATE_DONE);
  319. return NULL;
  320. }
  321. static struct vimc_ent_device *vimc_capture_add(struct vimc_device *vimc,
  322. const char *vcfg_name)
  323. {
  324. struct v4l2_device *v4l2_dev = &vimc->v4l2_dev;
  325. const struct vimc_pix_map *vpix;
  326. struct vimc_capture_device *vcapture;
  327. struct video_device *vdev;
  328. struct vb2_queue *q;
  329. int ret;
  330. /* Allocate the vimc_capture_device struct */
  331. vcapture = kzalloc(sizeof(*vcapture), GFP_KERNEL);
  332. if (!vcapture)
  333. return ERR_PTR(-ENOMEM);
  334. /* Initialize the media entity */
  335. vcapture->vdev.entity.name = vcfg_name;
  336. vcapture->vdev.entity.function = MEDIA_ENT_F_IO_V4L;
  337. vcapture->pad.flags = MEDIA_PAD_FL_SINK;
  338. ret = media_entity_pads_init(&vcapture->vdev.entity,
  339. 1, &vcapture->pad);
  340. if (ret)
  341. goto err_free_vcapture;
  342. /* Initialize the lock */
  343. mutex_init(&vcapture->lock);
  344. /* Initialize the vb2 queue */
  345. q = &vcapture->queue;
  346. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  347. q->io_modes = VB2_MMAP | VB2_DMABUF;
  348. if (vimc_allocator == VIMC_ALLOCATOR_VMALLOC)
  349. q->io_modes |= VB2_USERPTR;
  350. q->drv_priv = vcapture;
  351. q->buf_struct_size = sizeof(struct vimc_capture_buffer);
  352. q->ops = &vimc_capture_qops;
  353. q->mem_ops = vimc_allocator == VIMC_ALLOCATOR_DMA_CONTIG
  354. ? &vb2_dma_contig_memops : &vb2_vmalloc_memops;
  355. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  356. q->min_reqbufs_allocation = 2;
  357. q->lock = &vcapture->lock;
  358. q->dev = v4l2_dev->dev;
  359. ret = vb2_queue_init(q);
  360. if (ret) {
  361. dev_err(vimc->mdev.dev, "%s: vb2 queue init failed (err=%d)\n",
  362. vcfg_name, ret);
  363. goto err_clean_m_ent;
  364. }
  365. /* Initialize buffer list and its lock */
  366. INIT_LIST_HEAD(&vcapture->buf_list);
  367. spin_lock_init(&vcapture->qlock);
  368. /* Set default frame format */
  369. vcapture->format = fmt_default;
  370. vpix = vimc_pix_map_by_pixelformat(vcapture->format.pixelformat);
  371. vcapture->format.bytesperline = vcapture->format.width * vpix->bpp;
  372. vcapture->format.sizeimage = vcapture->format.bytesperline *
  373. vcapture->format.height;
  374. /* Fill the vimc_ent_device struct */
  375. vcapture->ved.ent = &vcapture->vdev.entity;
  376. vcapture->ved.process_frame = vimc_capture_process_frame;
  377. vcapture->ved.vdev_get_format = vimc_capture_get_format;
  378. vcapture->ved.dev = vimc->mdev.dev;
  379. /* Initialize the video_device struct */
  380. vdev = &vcapture->vdev;
  381. vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING
  382. | V4L2_CAP_IO_MC;
  383. vdev->entity.ops = &vimc_capture_mops;
  384. vdev->release = video_device_release_empty;
  385. vdev->fops = &vimc_capture_fops;
  386. vdev->ioctl_ops = &vimc_capture_ioctl_ops;
  387. vdev->lock = &vcapture->lock;
  388. vdev->queue = q;
  389. vdev->v4l2_dev = v4l2_dev;
  390. vdev->vfl_dir = VFL_DIR_RX;
  391. strscpy(vdev->name, vcfg_name, sizeof(vdev->name));
  392. video_set_drvdata(vdev, &vcapture->ved);
  393. /* Register the video_device with the v4l2 and the media framework */
  394. ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
  395. if (ret) {
  396. dev_err(vimc->mdev.dev, "%s: video register failed (err=%d)\n",
  397. vcapture->vdev.name, ret);
  398. goto err_clean_m_ent;
  399. }
  400. return &vcapture->ved;
  401. err_clean_m_ent:
  402. media_entity_cleanup(&vcapture->vdev.entity);
  403. err_free_vcapture:
  404. kfree(vcapture);
  405. return ERR_PTR(ret);
  406. }
  407. const struct vimc_ent_type vimc_capture_type = {
  408. .add = vimc_capture_add,
  409. .unregister = vimc_capture_unregister,
  410. .release = vimc_capture_release
  411. };