vimc-capture.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /*
  2. * vimc-capture.c Virtual Media Controller Driver
  3. *
  4. * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. #include <linux/component.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <media/v4l2-ioctl.h>
  21. #include <media/videobuf2-core.h>
  22. #include <media/videobuf2-vmalloc.h>
  23. #include "vimc-common.h"
  24. #define VIMC_CAP_DRV_NAME "vimc-capture"
  25. struct vimc_cap_device {
  26. struct vimc_ent_device ved;
  27. struct video_device vdev;
  28. struct device *dev;
  29. struct v4l2_pix_format format;
  30. struct vb2_queue queue;
  31. struct list_head buf_list;
  32. /*
  33. * NOTE: in a real driver, a spin lock must be used to access the
  34. * queue because the frames are generated from a hardware interruption
  35. * and the isr is not allowed to sleep.
  36. * Even if it is not necessary a spinlock in the vimc driver, we
  37. * use it here as a code reference
  38. */
  39. spinlock_t qlock;
  40. struct mutex lock;
  41. u32 sequence;
  42. struct media_pipeline pipe;
  43. };
  44. static const struct v4l2_pix_format fmt_default = {
  45. .width = 640,
  46. .height = 480,
  47. .pixelformat = V4L2_PIX_FMT_RGB24,
  48. .field = V4L2_FIELD_NONE,
  49. .colorspace = V4L2_COLORSPACE_DEFAULT,
  50. };
  51. struct vimc_cap_buffer {
  52. /*
  53. * struct vb2_v4l2_buffer must be the first element
  54. * the videobuf2 framework will allocate this struct based on
  55. * buf_struct_size and use the first sizeof(struct vb2_buffer) bytes of
  56. * memory as a vb2_buffer
  57. */
  58. struct vb2_v4l2_buffer vb2;
  59. struct list_head list;
  60. };
  61. static int vimc_cap_querycap(struct file *file, void *priv,
  62. struct v4l2_capability *cap)
  63. {
  64. struct vimc_cap_device *vcap = video_drvdata(file);
  65. strlcpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
  66. strlcpy(cap->card, KBUILD_MODNAME, sizeof(cap->card));
  67. snprintf(cap->bus_info, sizeof(cap->bus_info),
  68. "platform:%s", vcap->vdev.v4l2_dev->name);
  69. return 0;
  70. }
  71. static void vimc_cap_get_format(struct vimc_ent_device *ved,
  72. struct v4l2_pix_format *fmt)
  73. {
  74. struct vimc_cap_device *vcap = container_of(ved, struct vimc_cap_device,
  75. ved);
  76. *fmt = vcap->format;
  77. }
  78. static int vimc_cap_g_fmt_vid_cap(struct file *file, void *priv,
  79. struct v4l2_format *f)
  80. {
  81. struct vimc_cap_device *vcap = video_drvdata(file);
  82. f->fmt.pix = vcap->format;
  83. return 0;
  84. }
  85. static int vimc_cap_try_fmt_vid_cap(struct file *file, void *priv,
  86. struct v4l2_format *f)
  87. {
  88. struct v4l2_pix_format *format = &f->fmt.pix;
  89. const struct vimc_pix_map *vpix;
  90. format->width = clamp_t(u32, format->width, VIMC_FRAME_MIN_WIDTH,
  91. VIMC_FRAME_MAX_WIDTH) & ~1;
  92. format->height = clamp_t(u32, format->height, VIMC_FRAME_MIN_HEIGHT,
  93. VIMC_FRAME_MAX_HEIGHT) & ~1;
  94. /* Don't accept a pixelformat that is not on the table */
  95. vpix = vimc_pix_map_by_pixelformat(format->pixelformat);
  96. if (!vpix) {
  97. format->pixelformat = fmt_default.pixelformat;
  98. vpix = vimc_pix_map_by_pixelformat(format->pixelformat);
  99. }
  100. /* TODO: Add support for custom bytesperline values */
  101. format->bytesperline = format->width * vpix->bpp;
  102. format->sizeimage = format->bytesperline * format->height;
  103. if (format->field == V4L2_FIELD_ANY)
  104. format->field = fmt_default.field;
  105. vimc_colorimetry_clamp(format);
  106. return 0;
  107. }
  108. static int vimc_cap_s_fmt_vid_cap(struct file *file, void *priv,
  109. struct v4l2_format *f)
  110. {
  111. struct vimc_cap_device *vcap = video_drvdata(file);
  112. /* Do not change the format while stream is on */
  113. if (vb2_is_busy(&vcap->queue))
  114. return -EBUSY;
  115. vimc_cap_try_fmt_vid_cap(file, priv, f);
  116. dev_dbg(vcap->dev, "%s: format update: "
  117. "old:%dx%d (0x%x, %d, %d, %d, %d) "
  118. "new:%dx%d (0x%x, %d, %d, %d, %d)\n", vcap->vdev.name,
  119. /* old */
  120. vcap->format.width, vcap->format.height,
  121. vcap->format.pixelformat, vcap->format.colorspace,
  122. vcap->format.quantization, vcap->format.xfer_func,
  123. vcap->format.ycbcr_enc,
  124. /* new */
  125. f->fmt.pix.width, f->fmt.pix.height,
  126. f->fmt.pix.pixelformat, f->fmt.pix.colorspace,
  127. f->fmt.pix.quantization, f->fmt.pix.xfer_func,
  128. f->fmt.pix.ycbcr_enc);
  129. vcap->format = f->fmt.pix;
  130. return 0;
  131. }
  132. static int vimc_cap_enum_fmt_vid_cap(struct file *file, void *priv,
  133. struct v4l2_fmtdesc *f)
  134. {
  135. const struct vimc_pix_map *vpix = vimc_pix_map_by_index(f->index);
  136. if (!vpix)
  137. return -EINVAL;
  138. f->pixelformat = vpix->pixelformat;
  139. return 0;
  140. }
  141. static int vimc_cap_enum_framesizes(struct file *file, void *fh,
  142. struct v4l2_frmsizeenum *fsize)
  143. {
  144. const struct vimc_pix_map *vpix;
  145. if (fsize->index)
  146. return -EINVAL;
  147. /* Only accept code in the pix map table */
  148. vpix = vimc_pix_map_by_code(fsize->pixel_format);
  149. if (!vpix)
  150. return -EINVAL;
  151. fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
  152. fsize->stepwise.min_width = VIMC_FRAME_MIN_WIDTH;
  153. fsize->stepwise.max_width = VIMC_FRAME_MAX_WIDTH;
  154. fsize->stepwise.min_height = VIMC_FRAME_MIN_HEIGHT;
  155. fsize->stepwise.max_height = VIMC_FRAME_MAX_HEIGHT;
  156. fsize->stepwise.step_width = 2;
  157. fsize->stepwise.step_height = 2;
  158. return 0;
  159. }
  160. static const struct v4l2_file_operations vimc_cap_fops = {
  161. .owner = THIS_MODULE,
  162. .open = v4l2_fh_open,
  163. .release = vb2_fop_release,
  164. .read = vb2_fop_read,
  165. .poll = vb2_fop_poll,
  166. .unlocked_ioctl = video_ioctl2,
  167. .mmap = vb2_fop_mmap,
  168. };
  169. static const struct v4l2_ioctl_ops vimc_cap_ioctl_ops = {
  170. .vidioc_querycap = vimc_cap_querycap,
  171. .vidioc_g_fmt_vid_cap = vimc_cap_g_fmt_vid_cap,
  172. .vidioc_s_fmt_vid_cap = vimc_cap_s_fmt_vid_cap,
  173. .vidioc_try_fmt_vid_cap = vimc_cap_try_fmt_vid_cap,
  174. .vidioc_enum_fmt_vid_cap = vimc_cap_enum_fmt_vid_cap,
  175. .vidioc_enum_framesizes = vimc_cap_enum_framesizes,
  176. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  177. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  178. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  179. .vidioc_querybuf = vb2_ioctl_querybuf,
  180. .vidioc_qbuf = vb2_ioctl_qbuf,
  181. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  182. .vidioc_expbuf = vb2_ioctl_expbuf,
  183. .vidioc_streamon = vb2_ioctl_streamon,
  184. .vidioc_streamoff = vb2_ioctl_streamoff,
  185. };
  186. static void vimc_cap_return_all_buffers(struct vimc_cap_device *vcap,
  187. enum vb2_buffer_state state)
  188. {
  189. struct vimc_cap_buffer *vbuf, *node;
  190. spin_lock(&vcap->qlock);
  191. list_for_each_entry_safe(vbuf, node, &vcap->buf_list, list) {
  192. list_del(&vbuf->list);
  193. vb2_buffer_done(&vbuf->vb2.vb2_buf, state);
  194. }
  195. spin_unlock(&vcap->qlock);
  196. }
  197. static int vimc_cap_start_streaming(struct vb2_queue *vq, unsigned int count)
  198. {
  199. struct vimc_cap_device *vcap = vb2_get_drv_priv(vq);
  200. struct media_entity *entity = &vcap->vdev.entity;
  201. int ret;
  202. vcap->sequence = 0;
  203. /* Start the media pipeline */
  204. ret = media_pipeline_start(entity, &vcap->pipe);
  205. if (ret) {
  206. vimc_cap_return_all_buffers(vcap, VB2_BUF_STATE_QUEUED);
  207. return ret;
  208. }
  209. /* Enable streaming from the pipe */
  210. ret = vimc_pipeline_s_stream(&vcap->vdev.entity, 1);
  211. if (ret) {
  212. media_pipeline_stop(entity);
  213. vimc_cap_return_all_buffers(vcap, VB2_BUF_STATE_QUEUED);
  214. return ret;
  215. }
  216. return 0;
  217. }
  218. /*
  219. * Stop the stream engine. Any remaining buffers in the stream queue are
  220. * dequeued and passed on to the vb2 framework marked as STATE_ERROR.
  221. */
  222. static void vimc_cap_stop_streaming(struct vb2_queue *vq)
  223. {
  224. struct vimc_cap_device *vcap = vb2_get_drv_priv(vq);
  225. /* Disable streaming from the pipe */
  226. vimc_pipeline_s_stream(&vcap->vdev.entity, 0);
  227. /* Stop the media pipeline */
  228. media_pipeline_stop(&vcap->vdev.entity);
  229. /* Release all active buffers */
  230. vimc_cap_return_all_buffers(vcap, VB2_BUF_STATE_ERROR);
  231. }
  232. static void vimc_cap_buf_queue(struct vb2_buffer *vb2_buf)
  233. {
  234. struct vimc_cap_device *vcap = vb2_get_drv_priv(vb2_buf->vb2_queue);
  235. struct vimc_cap_buffer *buf = container_of(vb2_buf,
  236. struct vimc_cap_buffer,
  237. vb2.vb2_buf);
  238. spin_lock(&vcap->qlock);
  239. list_add_tail(&buf->list, &vcap->buf_list);
  240. spin_unlock(&vcap->qlock);
  241. }
  242. static int vimc_cap_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers,
  243. unsigned int *nplanes, unsigned int sizes[],
  244. struct device *alloc_devs[])
  245. {
  246. struct vimc_cap_device *vcap = vb2_get_drv_priv(vq);
  247. if (*nplanes)
  248. return sizes[0] < vcap->format.sizeimage ? -EINVAL : 0;
  249. /* We don't support multiplanes for now */
  250. *nplanes = 1;
  251. sizes[0] = vcap->format.sizeimage;
  252. return 0;
  253. }
  254. static int vimc_cap_buffer_prepare(struct vb2_buffer *vb)
  255. {
  256. struct vimc_cap_device *vcap = vb2_get_drv_priv(vb->vb2_queue);
  257. unsigned long size = vcap->format.sizeimage;
  258. if (vb2_plane_size(vb, 0) < size) {
  259. dev_err(vcap->dev, "%s: buffer too small (%lu < %lu)\n",
  260. vcap->vdev.name, vb2_plane_size(vb, 0), size);
  261. return -EINVAL;
  262. }
  263. return 0;
  264. }
  265. static const struct vb2_ops vimc_cap_qops = {
  266. .start_streaming = vimc_cap_start_streaming,
  267. .stop_streaming = vimc_cap_stop_streaming,
  268. .buf_queue = vimc_cap_buf_queue,
  269. .queue_setup = vimc_cap_queue_setup,
  270. .buf_prepare = vimc_cap_buffer_prepare,
  271. /*
  272. * Since q->lock is set we can use the standard
  273. * vb2_ops_wait_prepare/finish helper functions.
  274. */
  275. .wait_prepare = vb2_ops_wait_prepare,
  276. .wait_finish = vb2_ops_wait_finish,
  277. };
  278. static const struct media_entity_operations vimc_cap_mops = {
  279. .link_validate = vimc_link_validate,
  280. };
  281. static void vimc_cap_comp_unbind(struct device *comp, struct device *master,
  282. void *master_data)
  283. {
  284. struct vimc_ent_device *ved = dev_get_drvdata(comp);
  285. struct vimc_cap_device *vcap = container_of(ved, struct vimc_cap_device,
  286. ved);
  287. vb2_queue_release(&vcap->queue);
  288. media_entity_cleanup(ved->ent);
  289. video_unregister_device(&vcap->vdev);
  290. vimc_pads_cleanup(vcap->ved.pads);
  291. kfree(vcap);
  292. }
  293. static void vimc_cap_process_frame(struct vimc_ent_device *ved,
  294. struct media_pad *sink, const void *frame)
  295. {
  296. struct vimc_cap_device *vcap = container_of(ved, struct vimc_cap_device,
  297. ved);
  298. struct vimc_cap_buffer *vimc_buf;
  299. void *vbuf;
  300. spin_lock(&vcap->qlock);
  301. /* Get the first entry of the list */
  302. vimc_buf = list_first_entry_or_null(&vcap->buf_list,
  303. typeof(*vimc_buf), list);
  304. if (!vimc_buf) {
  305. spin_unlock(&vcap->qlock);
  306. return;
  307. }
  308. /* Remove this entry from the list */
  309. list_del(&vimc_buf->list);
  310. spin_unlock(&vcap->qlock);
  311. /* Fill the buffer */
  312. vimc_buf->vb2.vb2_buf.timestamp = ktime_get_ns();
  313. vimc_buf->vb2.sequence = vcap->sequence++;
  314. vimc_buf->vb2.field = vcap->format.field;
  315. vbuf = vb2_plane_vaddr(&vimc_buf->vb2.vb2_buf, 0);
  316. memcpy(vbuf, frame, vcap->format.sizeimage);
  317. /* Set it as ready */
  318. vb2_set_plane_payload(&vimc_buf->vb2.vb2_buf, 0,
  319. vcap->format.sizeimage);
  320. vb2_buffer_done(&vimc_buf->vb2.vb2_buf, VB2_BUF_STATE_DONE);
  321. }
  322. static int vimc_cap_comp_bind(struct device *comp, struct device *master,
  323. void *master_data)
  324. {
  325. struct v4l2_device *v4l2_dev = master_data;
  326. struct vimc_platform_data *pdata = comp->platform_data;
  327. const struct vimc_pix_map *vpix;
  328. struct vimc_cap_device *vcap;
  329. struct video_device *vdev;
  330. struct vb2_queue *q;
  331. int ret;
  332. /* Allocate the vimc_cap_device struct */
  333. vcap = kzalloc(sizeof(*vcap), GFP_KERNEL);
  334. if (!vcap)
  335. return -ENOMEM;
  336. /* Allocate the pads */
  337. vcap->ved.pads =
  338. vimc_pads_init(1, (const unsigned long[1]) {MEDIA_PAD_FL_SINK});
  339. if (IS_ERR(vcap->ved.pads)) {
  340. ret = PTR_ERR(vcap->ved.pads);
  341. goto err_free_vcap;
  342. }
  343. /* Initialize the media entity */
  344. vcap->vdev.entity.name = pdata->entity_name;
  345. vcap->vdev.entity.function = MEDIA_ENT_F_IO_V4L;
  346. ret = media_entity_pads_init(&vcap->vdev.entity,
  347. 1, vcap->ved.pads);
  348. if (ret)
  349. goto err_clean_pads;
  350. /* Initialize the lock */
  351. mutex_init(&vcap->lock);
  352. /* Initialize the vb2 queue */
  353. q = &vcap->queue;
  354. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  355. q->io_modes = VB2_MMAP | VB2_DMABUF;
  356. q->drv_priv = vcap;
  357. q->buf_struct_size = sizeof(struct vimc_cap_buffer);
  358. q->ops = &vimc_cap_qops;
  359. q->mem_ops = &vb2_vmalloc_memops;
  360. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  361. q->min_buffers_needed = 2;
  362. q->lock = &vcap->lock;
  363. ret = vb2_queue_init(q);
  364. if (ret) {
  365. dev_err(comp, "%s: vb2 queue init failed (err=%d)\n",
  366. pdata->entity_name, ret);
  367. goto err_clean_m_ent;
  368. }
  369. /* Initialize buffer list and its lock */
  370. INIT_LIST_HEAD(&vcap->buf_list);
  371. spin_lock_init(&vcap->qlock);
  372. /* Set default frame format */
  373. vcap->format = fmt_default;
  374. vpix = vimc_pix_map_by_pixelformat(vcap->format.pixelformat);
  375. vcap->format.bytesperline = vcap->format.width * vpix->bpp;
  376. vcap->format.sizeimage = vcap->format.bytesperline *
  377. vcap->format.height;
  378. /* Fill the vimc_ent_device struct */
  379. vcap->ved.ent = &vcap->vdev.entity;
  380. vcap->ved.process_frame = vimc_cap_process_frame;
  381. vcap->ved.vdev_get_format = vimc_cap_get_format;
  382. dev_set_drvdata(comp, &vcap->ved);
  383. vcap->dev = comp;
  384. /* Initialize the video_device struct */
  385. vdev = &vcap->vdev;
  386. vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  387. vdev->entity.ops = &vimc_cap_mops;
  388. vdev->release = video_device_release_empty;
  389. vdev->fops = &vimc_cap_fops;
  390. vdev->ioctl_ops = &vimc_cap_ioctl_ops;
  391. vdev->lock = &vcap->lock;
  392. vdev->queue = q;
  393. vdev->v4l2_dev = v4l2_dev;
  394. vdev->vfl_dir = VFL_DIR_RX;
  395. strlcpy(vdev->name, pdata->entity_name, sizeof(vdev->name));
  396. video_set_drvdata(vdev, &vcap->ved);
  397. /* Register the video_device with the v4l2 and the media framework */
  398. ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
  399. if (ret) {
  400. dev_err(comp, "%s: video register failed (err=%d)\n",
  401. vcap->vdev.name, ret);
  402. goto err_release_queue;
  403. }
  404. return 0;
  405. err_release_queue:
  406. vb2_queue_release(q);
  407. err_clean_m_ent:
  408. media_entity_cleanup(&vcap->vdev.entity);
  409. err_clean_pads:
  410. vimc_pads_cleanup(vcap->ved.pads);
  411. err_free_vcap:
  412. kfree(vcap);
  413. return ret;
  414. }
  415. static const struct component_ops vimc_cap_comp_ops = {
  416. .bind = vimc_cap_comp_bind,
  417. .unbind = vimc_cap_comp_unbind,
  418. };
  419. static int vimc_cap_probe(struct platform_device *pdev)
  420. {
  421. return component_add(&pdev->dev, &vimc_cap_comp_ops);
  422. }
  423. static int vimc_cap_remove(struct platform_device *pdev)
  424. {
  425. component_del(&pdev->dev, &vimc_cap_comp_ops);
  426. return 0;
  427. }
  428. static struct platform_driver vimc_cap_pdrv = {
  429. .probe = vimc_cap_probe,
  430. .remove = vimc_cap_remove,
  431. .driver = {
  432. .name = VIMC_CAP_DRV_NAME,
  433. },
  434. };
  435. static const struct platform_device_id vimc_cap_driver_ids[] = {
  436. {
  437. .name = VIMC_CAP_DRV_NAME,
  438. },
  439. { }
  440. };
  441. module_platform_driver(vimc_cap_pdrv);
  442. MODULE_DEVICE_TABLE(platform, vimc_cap_driver_ids);
  443. MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC) Capture");
  444. MODULE_AUTHOR("Helen Mae Koike Fornazier <helen.fornazier@gmail.com>");
  445. MODULE_LICENSE("GPL");