2 * vimc-capture.c Virtual Media Controller Driver
4 * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
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.
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.
18 #include <media/v4l2-ioctl.h>
19 #include <media/videobuf2-core.h>
20 #include <media/videobuf2-vmalloc.h>
22 #include "vimc-capture.h"
24 struct vimc_cap_device
{
25 struct vimc_ent_device ved
;
26 struct video_device vdev
;
27 struct v4l2_pix_format format
;
28 struct vb2_queue queue
;
29 struct list_head buf_list
;
31 * NOTE: in a real driver, a spin lock must be used to access the
32 * queue because the frames are generated from a hardware interruption
33 * and the isr is not allowed to sleep.
34 * Even if it is not necessary a spinlock in the vimc driver, we
35 * use it here as a code reference
40 struct media_pipeline pipe
;
43 struct vimc_cap_buffer
{
45 * struct vb2_v4l2_buffer must be the first element
46 * the videobuf2 framework will allocate this struct based on
47 * buf_struct_size and use the first sizeof(struct vb2_buffer) bytes of
48 * memory as a vb2_buffer
50 struct vb2_v4l2_buffer vb2
;
51 struct list_head list
;
54 static int vimc_cap_querycap(struct file
*file
, void *priv
,
55 struct v4l2_capability
*cap
)
57 struct vimc_cap_device
*vcap
= video_drvdata(file
);
59 strlcpy(cap
->driver
, KBUILD_MODNAME
, sizeof(cap
->driver
));
60 strlcpy(cap
->card
, KBUILD_MODNAME
, sizeof(cap
->card
));
61 snprintf(cap
->bus_info
, sizeof(cap
->bus_info
),
62 "platform:%s", vcap
->vdev
.v4l2_dev
->name
);
67 static int vimc_cap_fmt_vid_cap(struct file
*file
, void *priv
,
68 struct v4l2_format
*f
)
70 struct vimc_cap_device
*vcap
= video_drvdata(file
);
72 f
->fmt
.pix
= vcap
->format
;
77 static int vimc_cap_enum_fmt_vid_cap(struct file
*file
, void *priv
,
78 struct v4l2_fmtdesc
*f
)
80 struct vimc_cap_device
*vcap
= video_drvdata(file
);
85 /* We only support one format for now */
86 f
->pixelformat
= vcap
->format
.pixelformat
;
91 static const struct v4l2_file_operations vimc_cap_fops
= {
94 .release
= vb2_fop_release
,
97 .unlocked_ioctl
= video_ioctl2
,
101 static const struct v4l2_ioctl_ops vimc_cap_ioctl_ops
= {
102 .vidioc_querycap
= vimc_cap_querycap
,
104 .vidioc_g_fmt_vid_cap
= vimc_cap_fmt_vid_cap
,
105 .vidioc_s_fmt_vid_cap
= vimc_cap_fmt_vid_cap
,
106 .vidioc_try_fmt_vid_cap
= vimc_cap_fmt_vid_cap
,
107 .vidioc_enum_fmt_vid_cap
= vimc_cap_enum_fmt_vid_cap
,
109 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
110 .vidioc_create_bufs
= vb2_ioctl_create_bufs
,
111 .vidioc_prepare_buf
= vb2_ioctl_prepare_buf
,
112 .vidioc_querybuf
= vb2_ioctl_querybuf
,
113 .vidioc_qbuf
= vb2_ioctl_qbuf
,
114 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
115 .vidioc_expbuf
= vb2_ioctl_expbuf
,
116 .vidioc_streamon
= vb2_ioctl_streamon
,
117 .vidioc_streamoff
= vb2_ioctl_streamoff
,
120 static void vimc_cap_return_all_buffers(struct vimc_cap_device
*vcap
,
121 enum vb2_buffer_state state
)
123 struct vimc_cap_buffer
*vbuf
, *node
;
125 spin_lock(&vcap
->qlock
);
127 list_for_each_entry_safe(vbuf
, node
, &vcap
->buf_list
, list
) {
128 list_del(&vbuf
->list
);
129 vb2_buffer_done(&vbuf
->vb2
.vb2_buf
, state
);
132 spin_unlock(&vcap
->qlock
);
135 static int vimc_cap_pipeline_s_stream(struct vimc_cap_device
*vcap
, int enable
)
137 struct v4l2_subdev
*sd
;
138 struct media_pad
*pad
;
141 /* Start the stream in the subdevice direct connected */
142 pad
= media_entity_remote_pad(&vcap
->vdev
.entity
.pads
[0]);
145 * if it is a raw node from vimc-core, there is nothing to activate
146 * TODO: remove this when there are no more raw nodes in the
147 * core and return error instead
149 if (pad
->entity
->obj_type
== MEDIA_ENTITY_TYPE_BASE
)
152 sd
= media_entity_to_v4l2_subdev(pad
->entity
);
153 ret
= v4l2_subdev_call(sd
, video
, s_stream
, enable
);
154 if (ret
&& ret
!= -ENOIOCTLCMD
)
160 static int vimc_cap_start_streaming(struct vb2_queue
*vq
, unsigned int count
)
162 struct vimc_cap_device
*vcap
= vb2_get_drv_priv(vq
);
163 struct media_entity
*entity
= &vcap
->vdev
.entity
;
168 /* Start the media pipeline */
169 ret
= media_pipeline_start(entity
, &vcap
->pipe
);
171 vimc_cap_return_all_buffers(vcap
, VB2_BUF_STATE_QUEUED
);
175 /* Enable streaming from the pipe */
176 ret
= vimc_cap_pipeline_s_stream(vcap
, 1);
178 media_pipeline_stop(entity
);
179 vimc_cap_return_all_buffers(vcap
, VB2_BUF_STATE_QUEUED
);
187 * Stop the stream engine. Any remaining buffers in the stream queue are
188 * dequeued and passed on to the vb2 framework marked as STATE_ERROR.
190 static void vimc_cap_stop_streaming(struct vb2_queue
*vq
)
192 struct vimc_cap_device
*vcap
= vb2_get_drv_priv(vq
);
194 /* Disable streaming from the pipe */
195 vimc_cap_pipeline_s_stream(vcap
, 0);
197 /* Stop the media pipeline */
198 media_pipeline_stop(&vcap
->vdev
.entity
);
200 /* Release all active buffers */
201 vimc_cap_return_all_buffers(vcap
, VB2_BUF_STATE_ERROR
);
204 static void vimc_cap_buf_queue(struct vb2_buffer
*vb2_buf
)
206 struct vimc_cap_device
*vcap
= vb2_get_drv_priv(vb2_buf
->vb2_queue
);
207 struct vimc_cap_buffer
*buf
= container_of(vb2_buf
,
208 struct vimc_cap_buffer
,
211 spin_lock(&vcap
->qlock
);
212 list_add_tail(&buf
->list
, &vcap
->buf_list
);
213 spin_unlock(&vcap
->qlock
);
216 static int vimc_cap_queue_setup(struct vb2_queue
*vq
, unsigned int *nbuffers
,
217 unsigned int *nplanes
, unsigned int sizes
[],
218 struct device
*alloc_devs
[])
220 struct vimc_cap_device
*vcap
= vb2_get_drv_priv(vq
);
223 return sizes
[0] < vcap
->format
.sizeimage
? -EINVAL
: 0;
224 /* We don't support multiplanes for now */
226 sizes
[0] = vcap
->format
.sizeimage
;
231 static int vimc_cap_buffer_prepare(struct vb2_buffer
*vb
)
233 struct vimc_cap_device
*vcap
= vb2_get_drv_priv(vb
->vb2_queue
);
234 unsigned long size
= vcap
->format
.sizeimage
;
236 if (vb2_plane_size(vb
, 0) < size
) {
237 dev_err(vcap
->vdev
.v4l2_dev
->dev
,
238 "%s: buffer too small (%lu < %lu)\n",
239 vcap
->vdev
.name
, vb2_plane_size(vb
, 0), size
);
245 static const struct vb2_ops vimc_cap_qops
= {
246 .start_streaming
= vimc_cap_start_streaming
,
247 .stop_streaming
= vimc_cap_stop_streaming
,
248 .buf_queue
= vimc_cap_buf_queue
,
249 .queue_setup
= vimc_cap_queue_setup
,
250 .buf_prepare
= vimc_cap_buffer_prepare
,
252 * Since q->lock is set we can use the standard
253 * vb2_ops_wait_prepare/finish helper functions.
255 .wait_prepare
= vb2_ops_wait_prepare
,
256 .wait_finish
= vb2_ops_wait_finish
,
260 * NOTE: this function is a copy of v4l2_subdev_link_validate_get_format
261 * maybe the v4l2 function should be public
263 static int vimc_cap_v4l2_subdev_link_validate_get_format(struct media_pad
*pad
,
264 struct v4l2_subdev_format
*fmt
)
266 struct v4l2_subdev
*sd
= media_entity_to_v4l2_subdev(pad
->entity
);
268 fmt
->which
= V4L2_SUBDEV_FORMAT_ACTIVE
;
269 fmt
->pad
= pad
->index
;
271 return v4l2_subdev_call(sd
, pad
, get_fmt
, NULL
, fmt
);
274 static int vimc_cap_link_validate(struct media_link
*link
)
276 struct v4l2_subdev_format source_fmt
;
277 const struct vimc_pix_map
*vpix
;
278 struct vimc_cap_device
*vcap
= container_of(link
->sink
->entity
,
279 struct vimc_cap_device
,
281 struct v4l2_pix_format
*sink_fmt
= &vcap
->format
;
285 * if it is a raw node from vimc-core, ignore the link for now
286 * TODO: remove this when there are no more raw nodes in the
287 * core and return error instead
289 if (link
->source
->entity
->obj_type
== MEDIA_ENTITY_TYPE_BASE
)
292 /* Get the the format of the subdev */
293 ret
= vimc_cap_v4l2_subdev_link_validate_get_format(link
->source
,
298 dev_dbg(vcap
->vdev
.v4l2_dev
->dev
,
299 "%s: link validate formats src:%dx%d %d sink:%dx%d %d\n",
301 source_fmt
.format
.width
, source_fmt
.format
.height
,
302 source_fmt
.format
.code
,
303 sink_fmt
->width
, sink_fmt
->height
,
304 sink_fmt
->pixelformat
);
306 /* The width, height and code must match. */
307 vpix
= vimc_pix_map_by_pixelformat(sink_fmt
->pixelformat
);
308 if (source_fmt
.format
.width
!= sink_fmt
->width
309 || source_fmt
.format
.height
!= sink_fmt
->height
310 || vpix
->code
!= source_fmt
.format
.code
)
314 * The field order must match, or the sink field order must be NONE
315 * to support interlaced hardware connected to bridges that support
316 * progressive formats only.
318 if (source_fmt
.format
.field
!= sink_fmt
->field
&&
319 sink_fmt
->field
!= V4L2_FIELD_NONE
)
325 static const struct media_entity_operations vimc_cap_mops
= {
326 .link_validate
= vimc_cap_link_validate
,
329 static void vimc_cap_destroy(struct vimc_ent_device
*ved
)
331 struct vimc_cap_device
*vcap
= container_of(ved
, struct vimc_cap_device
,
334 vb2_queue_release(&vcap
->queue
);
335 media_entity_cleanup(ved
->ent
);
336 video_unregister_device(&vcap
->vdev
);
337 vimc_pads_cleanup(vcap
->ved
.pads
);
341 static void vimc_cap_process_frame(struct vimc_ent_device
*ved
,
342 struct media_pad
*sink
, const void *frame
)
344 struct vimc_cap_device
*vcap
= container_of(ved
, struct vimc_cap_device
,
346 struct vimc_cap_buffer
*vimc_buf
;
349 spin_lock(&vcap
->qlock
);
351 /* Get the first entry of the list */
352 vimc_buf
= list_first_entry_or_null(&vcap
->buf_list
,
353 typeof(*vimc_buf
), list
);
355 spin_unlock(&vcap
->qlock
);
359 /* Remove this entry from the list */
360 list_del(&vimc_buf
->list
);
362 spin_unlock(&vcap
->qlock
);
364 /* Fill the buffer */
365 vimc_buf
->vb2
.vb2_buf
.timestamp
= ktime_get_ns();
366 vimc_buf
->vb2
.sequence
= vcap
->sequence
++;
367 vimc_buf
->vb2
.field
= vcap
->format
.field
;
369 vbuf
= vb2_plane_vaddr(&vimc_buf
->vb2
.vb2_buf
, 0);
371 memcpy(vbuf
, frame
, vcap
->format
.sizeimage
);
373 /* Set it as ready */
374 vb2_set_plane_payload(&vimc_buf
->vb2
.vb2_buf
, 0,
375 vcap
->format
.sizeimage
);
376 vb2_buffer_done(&vimc_buf
->vb2
.vb2_buf
, VB2_BUF_STATE_DONE
);
379 struct vimc_ent_device
*vimc_cap_create(struct v4l2_device
*v4l2_dev
,
380 const char *const name
,
382 const unsigned long *pads_flag
)
384 const struct vimc_pix_map
*vpix
;
385 struct vimc_cap_device
*vcap
;
386 struct video_device
*vdev
;
391 * Check entity configuration params
392 * NOTE: we only support a single sink pad
394 if (!name
|| num_pads
!= 1 || !pads_flag
||
395 !(pads_flag
[0] & MEDIA_PAD_FL_SINK
))
396 return ERR_PTR(-EINVAL
);
398 /* Allocate the vimc_cap_device struct */
399 vcap
= kzalloc(sizeof(*vcap
), GFP_KERNEL
);
401 return ERR_PTR(-ENOMEM
);
403 /* Allocate the pads */
404 vcap
->ved
.pads
= vimc_pads_init(num_pads
, pads_flag
);
405 if (IS_ERR(vcap
->ved
.pads
)) {
406 ret
= PTR_ERR(vcap
->ved
.pads
);
410 /* Initialize the media entity */
411 vcap
->vdev
.entity
.name
= name
;
412 vcap
->vdev
.entity
.function
= MEDIA_ENT_F_IO_V4L
;
413 ret
= media_entity_pads_init(&vcap
->vdev
.entity
,
414 num_pads
, vcap
->ved
.pads
);
418 /* Initialize the lock */
419 mutex_init(&vcap
->lock
);
421 /* Initialize the vb2 queue */
423 q
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
424 q
->io_modes
= VB2_MMAP
| VB2_DMABUF
;
426 q
->buf_struct_size
= sizeof(struct vimc_cap_buffer
);
427 q
->ops
= &vimc_cap_qops
;
428 q
->mem_ops
= &vb2_vmalloc_memops
;
429 q
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
430 q
->min_buffers_needed
= 2;
431 q
->lock
= &vcap
->lock
;
433 ret
= vb2_queue_init(q
);
435 dev_err(vcap
->vdev
.v4l2_dev
->dev
,
436 "%s: vb2 queue init failed (err=%d)\n",
437 vcap
->vdev
.name
, ret
);
438 goto err_clean_m_ent
;
441 /* Initialize buffer list and its lock */
442 INIT_LIST_HEAD(&vcap
->buf_list
);
443 spin_lock_init(&vcap
->qlock
);
445 /* Set the frame format (this is hardcoded for now) */
446 vcap
->format
.width
= 640;
447 vcap
->format
.height
= 480;
448 vcap
->format
.pixelformat
= V4L2_PIX_FMT_RGB24
;
449 vcap
->format
.field
= V4L2_FIELD_NONE
;
450 vcap
->format
.colorspace
= V4L2_COLORSPACE_SRGB
;
452 vpix
= vimc_pix_map_by_pixelformat(vcap
->format
.pixelformat
);
454 vcap
->format
.bytesperline
= vcap
->format
.width
* vpix
->bpp
;
455 vcap
->format
.sizeimage
= vcap
->format
.bytesperline
*
458 /* Fill the vimc_ent_device struct */
459 vcap
->ved
.destroy
= vimc_cap_destroy
;
460 vcap
->ved
.ent
= &vcap
->vdev
.entity
;
461 vcap
->ved
.process_frame
= vimc_cap_process_frame
;
463 /* Initialize the video_device struct */
465 vdev
->device_caps
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
;
466 vdev
->entity
.ops
= &vimc_cap_mops
;
467 vdev
->release
= video_device_release_empty
;
468 vdev
->fops
= &vimc_cap_fops
;
469 vdev
->ioctl_ops
= &vimc_cap_ioctl_ops
;
470 vdev
->lock
= &vcap
->lock
;
472 vdev
->v4l2_dev
= v4l2_dev
;
473 vdev
->vfl_dir
= VFL_DIR_RX
;
474 strlcpy(vdev
->name
, name
, sizeof(vdev
->name
));
475 video_set_drvdata(vdev
, &vcap
->ved
);
477 /* Register the video_device with the v4l2 and the media framework */
478 ret
= video_register_device(vdev
, VFL_TYPE_GRABBER
, -1);
480 dev_err(vcap
->vdev
.v4l2_dev
->dev
,
481 "%s: video register failed (err=%d)\n",
482 vcap
->vdev
.name
, ret
);
483 goto err_release_queue
;
489 vb2_queue_release(q
);
491 media_entity_cleanup(&vcap
->vdev
.entity
);
493 vimc_pads_cleanup(vcap
->ved
.pads
);