vimc-capture.c 15 KB

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