vimc-capture.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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 <[email protected]>
  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. };
  185. static void vimc_capture_return_all_buffers(struct vimc_capture_device *vcapture,
  186. enum vb2_buffer_state state)
  187. {
  188. struct vimc_capture_buffer *vbuf, *node;
  189. spin_lock(&vcapture->qlock);
  190. list_for_each_entry_safe(vbuf, node, &vcapture->buf_list, list) {
  191. list_del(&vbuf->list);
  192. vb2_buffer_done(&vbuf->vb2.vb2_buf, state);
  193. }
  194. spin_unlock(&vcapture->qlock);
  195. }
  196. static int vimc_capture_start_streaming(struct vb2_queue *vq, unsigned int count)
  197. {
  198. struct vimc_capture_device *vcapture = vb2_get_drv_priv(vq);
  199. int ret;
  200. vcapture->sequence = 0;
  201. /* Start the media pipeline */
  202. ret = video_device_pipeline_start(&vcapture->vdev, &vcapture->stream.pipe);
  203. if (ret) {
  204. vimc_capture_return_all_buffers(vcapture, VB2_BUF_STATE_QUEUED);
  205. return ret;
  206. }
  207. ret = vimc_streamer_s_stream(&vcapture->stream, &vcapture->ved, 1);
  208. if (ret) {
  209. video_device_pipeline_stop(&vcapture->vdev);
  210. vimc_capture_return_all_buffers(vcapture, VB2_BUF_STATE_QUEUED);
  211. return ret;
  212. }
  213. return 0;
  214. }
  215. /*
  216. * Stop the stream engine. Any remaining buffers in the stream queue are
  217. * dequeued and passed on to the vb2 framework marked as STATE_ERROR.
  218. */
  219. static void vimc_capture_stop_streaming(struct vb2_queue *vq)
  220. {
  221. struct vimc_capture_device *vcapture = vb2_get_drv_priv(vq);
  222. vimc_streamer_s_stream(&vcapture->stream, &vcapture->ved, 0);
  223. /* Stop the media pipeline */
  224. video_device_pipeline_stop(&vcapture->vdev);
  225. /* Release all active buffers */
  226. vimc_capture_return_all_buffers(vcapture, VB2_BUF_STATE_ERROR);
  227. }
  228. static void vimc_capture_buf_queue(struct vb2_buffer *vb2_buf)
  229. {
  230. struct vimc_capture_device *vcapture = vb2_get_drv_priv(vb2_buf->vb2_queue);
  231. struct vimc_capture_buffer *buf = container_of(vb2_buf,
  232. struct vimc_capture_buffer,
  233. vb2.vb2_buf);
  234. spin_lock(&vcapture->qlock);
  235. list_add_tail(&buf->list, &vcapture->buf_list);
  236. spin_unlock(&vcapture->qlock);
  237. }
  238. static int vimc_capture_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers,
  239. unsigned int *nplanes, unsigned int sizes[],
  240. struct device *alloc_devs[])
  241. {
  242. struct vimc_capture_device *vcapture = vb2_get_drv_priv(vq);
  243. if (*nplanes)
  244. return sizes[0] < vcapture->format.sizeimage ? -EINVAL : 0;
  245. /* We don't support multiplanes for now */
  246. *nplanes = 1;
  247. sizes[0] = vcapture->format.sizeimage;
  248. return 0;
  249. }
  250. static int vimc_capture_buffer_prepare(struct vb2_buffer *vb)
  251. {
  252. struct vimc_capture_device *vcapture = vb2_get_drv_priv(vb->vb2_queue);
  253. unsigned long size = vcapture->format.sizeimage;
  254. if (vb2_plane_size(vb, 0) < size) {
  255. dev_err(vcapture->ved.dev, "%s: buffer too small (%lu < %lu)\n",
  256. vcapture->vdev.name, vb2_plane_size(vb, 0), size);
  257. return -EINVAL;
  258. }
  259. return 0;
  260. }
  261. static const struct vb2_ops vimc_capture_qops = {
  262. .start_streaming = vimc_capture_start_streaming,
  263. .stop_streaming = vimc_capture_stop_streaming,
  264. .buf_queue = vimc_capture_buf_queue,
  265. .queue_setup = vimc_capture_queue_setup,
  266. .buf_prepare = vimc_capture_buffer_prepare,
  267. /*
  268. * Since q->lock is set we can use the standard
  269. * vb2_ops_wait_prepare/finish helper functions.
  270. */
  271. .wait_prepare = vb2_ops_wait_prepare,
  272. .wait_finish = vb2_ops_wait_finish,
  273. };
  274. static const struct media_entity_operations vimc_capture_mops = {
  275. .link_validate = vimc_vdev_link_validate,
  276. };
  277. static void vimc_capture_release(struct vimc_ent_device *ved)
  278. {
  279. struct vimc_capture_device *vcapture =
  280. container_of(ved, struct vimc_capture_device, ved);
  281. media_entity_cleanup(vcapture->ved.ent);
  282. kfree(vcapture);
  283. }
  284. static void vimc_capture_unregister(struct vimc_ent_device *ved)
  285. {
  286. struct vimc_capture_device *vcapture =
  287. container_of(ved, struct vimc_capture_device, ved);
  288. vb2_video_unregister_device(&vcapture->vdev);
  289. }
  290. static void *vimc_capture_process_frame(struct vimc_ent_device *ved,
  291. const void *frame)
  292. {
  293. struct vimc_capture_device *vcapture = container_of(ved, struct vimc_capture_device,
  294. ved);
  295. struct vimc_capture_buffer *vimc_buf;
  296. void *vbuf;
  297. spin_lock(&vcapture->qlock);
  298. /* Get the first entry of the list */
  299. vimc_buf = list_first_entry_or_null(&vcapture->buf_list,
  300. typeof(*vimc_buf), list);
  301. if (!vimc_buf) {
  302. spin_unlock(&vcapture->qlock);
  303. return ERR_PTR(-EAGAIN);
  304. }
  305. /* Remove this entry from the list */
  306. list_del(&vimc_buf->list);
  307. spin_unlock(&vcapture->qlock);
  308. /* Fill the buffer */
  309. vimc_buf->vb2.vb2_buf.timestamp = ktime_get_ns();
  310. vimc_buf->vb2.sequence = vcapture->sequence++;
  311. vimc_buf->vb2.field = vcapture->format.field;
  312. vbuf = vb2_plane_vaddr(&vimc_buf->vb2.vb2_buf, 0);
  313. memcpy(vbuf, frame, vcapture->format.sizeimage);
  314. /* Set it as ready */
  315. vb2_set_plane_payload(&vimc_buf->vb2.vb2_buf, 0,
  316. vcapture->format.sizeimage);
  317. vb2_buffer_done(&vimc_buf->vb2.vb2_buf, VB2_BUF_STATE_DONE);
  318. return NULL;
  319. }
  320. static struct vimc_ent_device *vimc_capture_add(struct vimc_device *vimc,
  321. const char *vcfg_name)
  322. {
  323. struct v4l2_device *v4l2_dev = &vimc->v4l2_dev;
  324. const struct vimc_pix_map *vpix;
  325. struct vimc_capture_device *vcapture;
  326. struct video_device *vdev;
  327. struct vb2_queue *q;
  328. int ret;
  329. /* Allocate the vimc_capture_device struct */
  330. vcapture = kzalloc(sizeof(*vcapture), GFP_KERNEL);
  331. if (!vcapture)
  332. return ERR_PTR(-ENOMEM);
  333. /* Initialize the media entity */
  334. vcapture->vdev.entity.name = vcfg_name;
  335. vcapture->vdev.entity.function = MEDIA_ENT_F_IO_V4L;
  336. vcapture->pad.flags = MEDIA_PAD_FL_SINK;
  337. ret = media_entity_pads_init(&vcapture->vdev.entity,
  338. 1, &vcapture->pad);
  339. if (ret)
  340. goto err_free_vcapture;
  341. /* Initialize the lock */
  342. mutex_init(&vcapture->lock);
  343. /* Initialize the vb2 queue */
  344. q = &vcapture->queue;
  345. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  346. q->io_modes = VB2_MMAP | VB2_DMABUF;
  347. if (vimc_allocator == VIMC_ALLOCATOR_VMALLOC)
  348. q->io_modes |= VB2_USERPTR;
  349. q->drv_priv = vcapture;
  350. q->buf_struct_size = sizeof(struct vimc_capture_buffer);
  351. q->ops = &vimc_capture_qops;
  352. q->mem_ops = vimc_allocator == VIMC_ALLOCATOR_DMA_CONTIG
  353. ? &vb2_dma_contig_memops : &vb2_vmalloc_memops;
  354. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  355. q->min_buffers_needed = 2;
  356. q->lock = &vcapture->lock;
  357. q->dev = v4l2_dev->dev;
  358. ret = vb2_queue_init(q);
  359. if (ret) {
  360. dev_err(vimc->mdev.dev, "%s: vb2 queue init failed (err=%d)\n",
  361. vcfg_name, ret);
  362. goto err_clean_m_ent;
  363. }
  364. /* Initialize buffer list and its lock */
  365. INIT_LIST_HEAD(&vcapture->buf_list);
  366. spin_lock_init(&vcapture->qlock);
  367. /* Set default frame format */
  368. vcapture->format = fmt_default;
  369. vpix = vimc_pix_map_by_pixelformat(vcapture->format.pixelformat);
  370. vcapture->format.bytesperline = vcapture->format.width * vpix->bpp;
  371. vcapture->format.sizeimage = vcapture->format.bytesperline *
  372. vcapture->format.height;
  373. /* Fill the vimc_ent_device struct */
  374. vcapture->ved.ent = &vcapture->vdev.entity;
  375. vcapture->ved.process_frame = vimc_capture_process_frame;
  376. vcapture->ved.vdev_get_format = vimc_capture_get_format;
  377. vcapture->ved.dev = vimc->mdev.dev;
  378. /* Initialize the video_device struct */
  379. vdev = &vcapture->vdev;
  380. vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING
  381. | V4L2_CAP_IO_MC;
  382. vdev->entity.ops = &vimc_capture_mops;
  383. vdev->release = video_device_release_empty;
  384. vdev->fops = &vimc_capture_fops;
  385. vdev->ioctl_ops = &vimc_capture_ioctl_ops;
  386. vdev->lock = &vcapture->lock;
  387. vdev->queue = q;
  388. vdev->v4l2_dev = v4l2_dev;
  389. vdev->vfl_dir = VFL_DIR_RX;
  390. strscpy(vdev->name, vcfg_name, sizeof(vdev->name));
  391. video_set_drvdata(vdev, &vcapture->ved);
  392. /* Register the video_device with the v4l2 and the media framework */
  393. ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
  394. if (ret) {
  395. dev_err(vimc->mdev.dev, "%s: video register failed (err=%d)\n",
  396. vcapture->vdev.name, ret);
  397. goto err_clean_m_ent;
  398. }
  399. return &vcapture->ved;
  400. err_clean_m_ent:
  401. media_entity_cleanup(&vcapture->vdev.entity);
  402. err_free_vcapture:
  403. kfree(vcapture);
  404. return ERR_PTR(ret);
  405. }
  406. struct vimc_ent_type vimc_capture_type = {
  407. .add = vimc_capture_add,
  408. .unregister = vimc_capture_unregister,
  409. .release = vimc_capture_release
  410. };