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