vimc-capture.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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 <media/v4l2-ioctl.h>
  18. #include <media/videobuf2-core.h>
  19. #include <media/videobuf2-vmalloc.h>
  20. #include "vimc-capture.h"
  21. struct vimc_cap_device {
  22. struct vimc_ent_device ved;
  23. struct video_device vdev;
  24. struct v4l2_pix_format format;
  25. struct vb2_queue queue;
  26. struct list_head buf_list;
  27. /*
  28. * NOTE: in a real driver, a spin lock must be used to access the
  29. * queue because the frames are generated from a hardware interruption
  30. * and the isr is not allowed to sleep.
  31. * Even if it is not necessary a spinlock in the vimc driver, we
  32. * use it here as a code reference
  33. */
  34. spinlock_t qlock;
  35. struct mutex lock;
  36. u32 sequence;
  37. struct media_pipeline pipe;
  38. };
  39. struct vimc_cap_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_cap_querycap(struct file *file, void *priv,
  50. struct v4l2_capability *cap)
  51. {
  52. struct vimc_cap_device *vcap = video_drvdata(file);
  53. strlcpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
  54. strlcpy(cap->card, KBUILD_MODNAME, sizeof(cap->card));
  55. snprintf(cap->bus_info, sizeof(cap->bus_info),
  56. "platform:%s", vcap->vdev.v4l2_dev->name);
  57. return 0;
  58. }
  59. static int vimc_cap_fmt_vid_cap(struct file *file, void *priv,
  60. struct v4l2_format *f)
  61. {
  62. struct vimc_cap_device *vcap = video_drvdata(file);
  63. f->fmt.pix = vcap->format;
  64. return 0;
  65. }
  66. static int vimc_cap_enum_fmt_vid_cap(struct file *file, void *priv,
  67. struct v4l2_fmtdesc *f)
  68. {
  69. struct vimc_cap_device *vcap = video_drvdata(file);
  70. if (f->index > 0)
  71. return -EINVAL;
  72. /* We only support one format for now */
  73. f->pixelformat = vcap->format.pixelformat;
  74. return 0;
  75. }
  76. static const struct v4l2_file_operations vimc_cap_fops = {
  77. .owner = THIS_MODULE,
  78. .open = v4l2_fh_open,
  79. .release = vb2_fop_release,
  80. .read = vb2_fop_read,
  81. .poll = vb2_fop_poll,
  82. .unlocked_ioctl = video_ioctl2,
  83. .mmap = vb2_fop_mmap,
  84. };
  85. static const struct v4l2_ioctl_ops vimc_cap_ioctl_ops = {
  86. .vidioc_querycap = vimc_cap_querycap,
  87. .vidioc_g_fmt_vid_cap = vimc_cap_fmt_vid_cap,
  88. .vidioc_s_fmt_vid_cap = vimc_cap_fmt_vid_cap,
  89. .vidioc_try_fmt_vid_cap = vimc_cap_fmt_vid_cap,
  90. .vidioc_enum_fmt_vid_cap = vimc_cap_enum_fmt_vid_cap,
  91. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  92. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  93. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  94. .vidioc_querybuf = vb2_ioctl_querybuf,
  95. .vidioc_qbuf = vb2_ioctl_qbuf,
  96. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  97. .vidioc_expbuf = vb2_ioctl_expbuf,
  98. .vidioc_streamon = vb2_ioctl_streamon,
  99. .vidioc_streamoff = vb2_ioctl_streamoff,
  100. };
  101. static void vimc_cap_return_all_buffers(struct vimc_cap_device *vcap,
  102. enum vb2_buffer_state state)
  103. {
  104. struct vimc_cap_buffer *vbuf, *node;
  105. spin_lock(&vcap->qlock);
  106. list_for_each_entry_safe(vbuf, node, &vcap->buf_list, list) {
  107. list_del(&vbuf->list);
  108. vb2_buffer_done(&vbuf->vb2.vb2_buf, state);
  109. }
  110. spin_unlock(&vcap->qlock);
  111. }
  112. static int vimc_cap_pipeline_s_stream(struct vimc_cap_device *vcap, int enable)
  113. {
  114. struct v4l2_subdev *sd;
  115. struct media_pad *pad;
  116. int ret;
  117. /* Start the stream in the subdevice direct connected */
  118. pad = media_entity_remote_pad(&vcap->vdev.entity.pads[0]);
  119. /*
  120. * if it is a raw node from vimc-core, there is nothing to activate
  121. * TODO: remove this when there are no more raw nodes in the
  122. * core and return error instead
  123. */
  124. if (pad->entity->obj_type == MEDIA_ENTITY_TYPE_BASE)
  125. return 0;
  126. sd = media_entity_to_v4l2_subdev(pad->entity);
  127. ret = v4l2_subdev_call(sd, video, s_stream, enable);
  128. if (ret && ret != -ENOIOCTLCMD)
  129. return ret;
  130. return 0;
  131. }
  132. static int vimc_cap_start_streaming(struct vb2_queue *vq, unsigned int count)
  133. {
  134. struct vimc_cap_device *vcap = vb2_get_drv_priv(vq);
  135. struct media_entity *entity = &vcap->vdev.entity;
  136. int ret;
  137. vcap->sequence = 0;
  138. /* Start the media pipeline */
  139. ret = media_pipeline_start(entity, &vcap->pipe);
  140. if (ret) {
  141. vimc_cap_return_all_buffers(vcap, VB2_BUF_STATE_QUEUED);
  142. return ret;
  143. }
  144. /* Enable streaming from the pipe */
  145. ret = vimc_cap_pipeline_s_stream(vcap, 1);
  146. if (ret) {
  147. media_pipeline_stop(entity);
  148. vimc_cap_return_all_buffers(vcap, VB2_BUF_STATE_QUEUED);
  149. return ret;
  150. }
  151. return 0;
  152. }
  153. /*
  154. * Stop the stream engine. Any remaining buffers in the stream queue are
  155. * dequeued and passed on to the vb2 framework marked as STATE_ERROR.
  156. */
  157. static void vimc_cap_stop_streaming(struct vb2_queue *vq)
  158. {
  159. struct vimc_cap_device *vcap = vb2_get_drv_priv(vq);
  160. /* Disable streaming from the pipe */
  161. vimc_cap_pipeline_s_stream(vcap, 0);
  162. /* Stop the media pipeline */
  163. media_pipeline_stop(&vcap->vdev.entity);
  164. /* Release all active buffers */
  165. vimc_cap_return_all_buffers(vcap, VB2_BUF_STATE_ERROR);
  166. }
  167. static void vimc_cap_buf_queue(struct vb2_buffer *vb2_buf)
  168. {
  169. struct vimc_cap_device *vcap = vb2_get_drv_priv(vb2_buf->vb2_queue);
  170. struct vimc_cap_buffer *buf = container_of(vb2_buf,
  171. struct vimc_cap_buffer,
  172. vb2.vb2_buf);
  173. spin_lock(&vcap->qlock);
  174. list_add_tail(&buf->list, &vcap->buf_list);
  175. spin_unlock(&vcap->qlock);
  176. }
  177. static int vimc_cap_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers,
  178. unsigned int *nplanes, unsigned int sizes[],
  179. struct device *alloc_devs[])
  180. {
  181. struct vimc_cap_device *vcap = vb2_get_drv_priv(vq);
  182. if (*nplanes)
  183. return sizes[0] < vcap->format.sizeimage ? -EINVAL : 0;
  184. /* We don't support multiplanes for now */
  185. *nplanes = 1;
  186. sizes[0] = vcap->format.sizeimage;
  187. return 0;
  188. }
  189. static int vimc_cap_buffer_prepare(struct vb2_buffer *vb)
  190. {
  191. struct vimc_cap_device *vcap = vb2_get_drv_priv(vb->vb2_queue);
  192. unsigned long size = vcap->format.sizeimage;
  193. if (vb2_plane_size(vb, 0) < size) {
  194. dev_err(vcap->vdev.v4l2_dev->dev,
  195. "%s: buffer too small (%lu < %lu)\n",
  196. vcap->vdev.name, vb2_plane_size(vb, 0), size);
  197. return -EINVAL;
  198. }
  199. return 0;
  200. }
  201. static const struct vb2_ops vimc_cap_qops = {
  202. .start_streaming = vimc_cap_start_streaming,
  203. .stop_streaming = vimc_cap_stop_streaming,
  204. .buf_queue = vimc_cap_buf_queue,
  205. .queue_setup = vimc_cap_queue_setup,
  206. .buf_prepare = vimc_cap_buffer_prepare,
  207. /*
  208. * Since q->lock is set we can use the standard
  209. * vb2_ops_wait_prepare/finish helper functions.
  210. */
  211. .wait_prepare = vb2_ops_wait_prepare,
  212. .wait_finish = vb2_ops_wait_finish,
  213. };
  214. /*
  215. * NOTE: this function is a copy of v4l2_subdev_link_validate_get_format
  216. * maybe the v4l2 function should be public
  217. */
  218. static int vimc_cap_v4l2_subdev_link_validate_get_format(struct media_pad *pad,
  219. struct v4l2_subdev_format *fmt)
  220. {
  221. struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(pad->entity);
  222. fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
  223. fmt->pad = pad->index;
  224. return v4l2_subdev_call(sd, pad, get_fmt, NULL, fmt);
  225. }
  226. static int vimc_cap_link_validate(struct media_link *link)
  227. {
  228. struct v4l2_subdev_format source_fmt;
  229. const struct vimc_pix_map *vpix;
  230. struct vimc_cap_device *vcap = container_of(link->sink->entity,
  231. struct vimc_cap_device,
  232. vdev.entity);
  233. struct v4l2_pix_format *sink_fmt = &vcap->format;
  234. int ret;
  235. /*
  236. * if it is a raw node from vimc-core, ignore the link for now
  237. * TODO: remove this when there are no more raw nodes in the
  238. * core and return error instead
  239. */
  240. if (link->source->entity->obj_type == MEDIA_ENTITY_TYPE_BASE)
  241. return 0;
  242. /* Get the the format of the subdev */
  243. ret = vimc_cap_v4l2_subdev_link_validate_get_format(link->source,
  244. &source_fmt);
  245. if (ret)
  246. return ret;
  247. dev_dbg(vcap->vdev.v4l2_dev->dev,
  248. "%s: link validate formats src:%dx%d %d sink:%dx%d %d\n",
  249. vcap->vdev.name,
  250. source_fmt.format.width, source_fmt.format.height,
  251. source_fmt.format.code,
  252. sink_fmt->width, sink_fmt->height,
  253. sink_fmt->pixelformat);
  254. /* The width, height and code must match. */
  255. vpix = vimc_pix_map_by_pixelformat(sink_fmt->pixelformat);
  256. if (source_fmt.format.width != sink_fmt->width
  257. || source_fmt.format.height != sink_fmt->height
  258. || vpix->code != source_fmt.format.code)
  259. return -EPIPE;
  260. /*
  261. * The field order must match, or the sink field order must be NONE
  262. * to support interlaced hardware connected to bridges that support
  263. * progressive formats only.
  264. */
  265. if (source_fmt.format.field != sink_fmt->field &&
  266. sink_fmt->field != V4L2_FIELD_NONE)
  267. return -EPIPE;
  268. return 0;
  269. }
  270. static const struct media_entity_operations vimc_cap_mops = {
  271. .link_validate = vimc_cap_link_validate,
  272. };
  273. static void vimc_cap_destroy(struct vimc_ent_device *ved)
  274. {
  275. struct vimc_cap_device *vcap = container_of(ved, struct vimc_cap_device,
  276. ved);
  277. vb2_queue_release(&vcap->queue);
  278. media_entity_cleanup(ved->ent);
  279. video_unregister_device(&vcap->vdev);
  280. vimc_pads_cleanup(vcap->ved.pads);
  281. kfree(vcap);
  282. }
  283. static void vimc_cap_process_frame(struct vimc_ent_device *ved,
  284. struct media_pad *sink, const void *frame)
  285. {
  286. struct vimc_cap_device *vcap = container_of(ved, struct vimc_cap_device,
  287. ved);
  288. struct vimc_cap_buffer *vimc_buf;
  289. void *vbuf;
  290. spin_lock(&vcap->qlock);
  291. /* Get the first entry of the list */
  292. vimc_buf = list_first_entry_or_null(&vcap->buf_list,
  293. typeof(*vimc_buf), list);
  294. if (!vimc_buf) {
  295. spin_unlock(&vcap->qlock);
  296. return;
  297. }
  298. /* Remove this entry from the list */
  299. list_del(&vimc_buf->list);
  300. spin_unlock(&vcap->qlock);
  301. /* Fill the buffer */
  302. vimc_buf->vb2.vb2_buf.timestamp = ktime_get_ns();
  303. vimc_buf->vb2.sequence = vcap->sequence++;
  304. vimc_buf->vb2.field = vcap->format.field;
  305. vbuf = vb2_plane_vaddr(&vimc_buf->vb2.vb2_buf, 0);
  306. memcpy(vbuf, frame, vcap->format.sizeimage);
  307. /* Set it as ready */
  308. vb2_set_plane_payload(&vimc_buf->vb2.vb2_buf, 0,
  309. vcap->format.sizeimage);
  310. vb2_buffer_done(&vimc_buf->vb2.vb2_buf, VB2_BUF_STATE_DONE);
  311. }
  312. struct vimc_ent_device *vimc_cap_create(struct v4l2_device *v4l2_dev,
  313. const char *const name,
  314. u16 num_pads,
  315. const unsigned long *pads_flag)
  316. {
  317. const struct vimc_pix_map *vpix;
  318. struct vimc_cap_device *vcap;
  319. struct video_device *vdev;
  320. struct vb2_queue *q;
  321. int ret;
  322. /*
  323. * Check entity configuration params
  324. * NOTE: we only support a single sink pad
  325. */
  326. if (!name || num_pads != 1 || !pads_flag ||
  327. !(pads_flag[0] & MEDIA_PAD_FL_SINK))
  328. return ERR_PTR(-EINVAL);
  329. /* Allocate the vimc_cap_device struct */
  330. vcap = kzalloc(sizeof(*vcap), GFP_KERNEL);
  331. if (!vcap)
  332. return ERR_PTR(-ENOMEM);
  333. /* Allocate the pads */
  334. vcap->ved.pads = vimc_pads_init(num_pads, pads_flag);
  335. if (IS_ERR(vcap->ved.pads)) {
  336. ret = PTR_ERR(vcap->ved.pads);
  337. goto err_free_vcap;
  338. }
  339. /* Initialize the media entity */
  340. vcap->vdev.entity.name = name;
  341. vcap->vdev.entity.function = MEDIA_ENT_F_IO_V4L;
  342. ret = media_entity_pads_init(&vcap->vdev.entity,
  343. num_pads, vcap->ved.pads);
  344. if (ret)
  345. goto err_clean_pads;
  346. /* Initialize the lock */
  347. mutex_init(&vcap->lock);
  348. /* Initialize the vb2 queue */
  349. q = &vcap->queue;
  350. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  351. q->io_modes = VB2_MMAP | VB2_DMABUF;
  352. q->drv_priv = vcap;
  353. q->buf_struct_size = sizeof(struct vimc_cap_buffer);
  354. q->ops = &vimc_cap_qops;
  355. q->mem_ops = &vb2_vmalloc_memops;
  356. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  357. q->min_buffers_needed = 2;
  358. q->lock = &vcap->lock;
  359. ret = vb2_queue_init(q);
  360. if (ret) {
  361. dev_err(vcap->vdev.v4l2_dev->dev,
  362. "%s: vb2 queue init failed (err=%d)\n",
  363. vcap->vdev.name, ret);
  364. goto err_clean_m_ent;
  365. }
  366. /* Initialize buffer list and its lock */
  367. INIT_LIST_HEAD(&vcap->buf_list);
  368. spin_lock_init(&vcap->qlock);
  369. /* Set the frame format (this is hardcoded for now) */
  370. vcap->format.width = 640;
  371. vcap->format.height = 480;
  372. vcap->format.pixelformat = V4L2_PIX_FMT_RGB24;
  373. vcap->format.field = V4L2_FIELD_NONE;
  374. vcap->format.colorspace = V4L2_COLORSPACE_SRGB;
  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.destroy = vimc_cap_destroy;
  381. vcap->ved.ent = &vcap->vdev.entity;
  382. vcap->ved.process_frame = vimc_cap_process_frame;
  383. /* Initialize the video_device struct */
  384. vdev = &vcap->vdev;
  385. vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  386. vdev->entity.ops = &vimc_cap_mops;
  387. vdev->release = video_device_release_empty;
  388. vdev->fops = &vimc_cap_fops;
  389. vdev->ioctl_ops = &vimc_cap_ioctl_ops;
  390. vdev->lock = &vcap->lock;
  391. vdev->queue = q;
  392. vdev->v4l2_dev = v4l2_dev;
  393. vdev->vfl_dir = VFL_DIR_RX;
  394. strlcpy(vdev->name, name, sizeof(vdev->name));
  395. video_set_drvdata(vdev, &vcap->ved);
  396. /* Register the video_device with the v4l2 and the media framework */
  397. ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
  398. if (ret) {
  399. dev_err(vcap->vdev.v4l2_dev->dev,
  400. "%s: video register failed (err=%d)\n",
  401. vcap->vdev.name, ret);
  402. goto err_release_queue;
  403. }
  404. return &vcap->ved;
  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 ERR_PTR(ret);
  414. }