vimc-capture.c 15 KB

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