1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * vimc-capture.c Virtual Media Controller Driver 4 * 5 * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com> 6 */ 7 8 #include <media/v4l2-ioctl.h> 9 #include <media/videobuf2-core.h> 10 #include <media/videobuf2-dma-contig.h> 11 #include <media/videobuf2-vmalloc.h> 12 13 #include "vimc-common.h" 14 #include "vimc-streamer.h" 15 16 struct vimc_capture_device { 17 struct vimc_ent_device ved; 18 struct video_device vdev; 19 struct v4l2_pix_format format; 20 struct vb2_queue queue; 21 struct list_head buf_list; 22 /* 23 * NOTE: in a real driver, a spin lock must be used to access the 24 * queue because the frames are generated from a hardware interruption 25 * and the isr is not allowed to sleep. 26 * Even if it is not necessary a spinlock in the vimc driver, we 27 * use it here as a code reference 28 */ 29 spinlock_t qlock; 30 struct mutex lock; 31 u32 sequence; 32 struct vimc_stream stream; 33 struct media_pad pad; 34 }; 35 36 static const struct v4l2_pix_format fmt_default = { 37 .width = 640, 38 .height = 480, 39 .pixelformat = V4L2_PIX_FMT_RGB24, 40 .field = V4L2_FIELD_NONE, 41 .colorspace = V4L2_COLORSPACE_SRGB, 42 }; 43 44 struct vimc_capture_buffer { 45 /* 46 * struct vb2_v4l2_buffer must be the first element 47 * the videobuf2 framework will allocate this struct based on 48 * buf_struct_size and use the first sizeof(struct vb2_buffer) bytes of 49 * memory as a vb2_buffer 50 */ 51 struct vb2_v4l2_buffer vb2; 52 struct list_head list; 53 }; 54 55 static int vimc_capture_querycap(struct file *file, void *priv, 56 struct v4l2_capability *cap) 57 { 58 strscpy(cap->driver, VIMC_PDEV_NAME, sizeof(cap->driver)); 59 strscpy(cap->card, KBUILD_MODNAME, sizeof(cap->card)); 60 61 return 0; 62 } 63 64 static void vimc_capture_get_format(struct vimc_ent_device *ved, 65 struct v4l2_pix_format *fmt) 66 { 67 struct vimc_capture_device *vcapture = container_of(ved, struct vimc_capture_device, 68 ved); 69 70 *fmt = vcapture->format; 71 } 72 73 static int vimc_capture_g_fmt_vid_cap(struct file *file, void *priv, 74 struct v4l2_format *f) 75 { 76 struct vimc_capture_device *vcapture = video_drvdata(file); 77 78 f->fmt.pix = vcapture->format; 79 80 return 0; 81 } 82 83 static int vimc_capture_try_fmt_vid_cap(struct file *file, void *priv, 84 struct v4l2_format *f) 85 { 86 struct v4l2_pix_format *format = &f->fmt.pix; 87 const struct vimc_pix_map *vpix; 88 89 format->width = clamp_t(u32, format->width, VIMC_FRAME_MIN_WIDTH, 90 VIMC_FRAME_MAX_WIDTH) & ~1; 91 format->height = clamp_t(u32, format->height, VIMC_FRAME_MIN_HEIGHT, 92 VIMC_FRAME_MAX_HEIGHT) & ~1; 93 94 /* Don't accept a pixelformat that is not on the table */ 95 vpix = vimc_pix_map_by_pixelformat(format->pixelformat); 96 if (!vpix) { 97 format->pixelformat = fmt_default.pixelformat; 98 vpix = vimc_pix_map_by_pixelformat(format->pixelformat); 99 } 100 /* TODO: Add support for custom bytesperline values */ 101 format->bytesperline = format->width * vpix->bpp; 102 format->sizeimage = format->bytesperline * format->height; 103 104 if (format->field == V4L2_FIELD_ANY) 105 format->field = fmt_default.field; 106 107 vimc_colorimetry_clamp(format); 108 109 if (format->colorspace == V4L2_COLORSPACE_DEFAULT) 110 format->colorspace = fmt_default.colorspace; 111 112 return 0; 113 } 114 115 static int vimc_capture_s_fmt_vid_cap(struct file *file, void *priv, 116 struct v4l2_format *f) 117 { 118 struct vimc_capture_device *vcapture = video_drvdata(file); 119 int ret; 120 121 /* Do not change the format while stream is on */ 122 if (vb2_is_busy(&vcapture->queue)) 123 return -EBUSY; 124 125 ret = vimc_capture_try_fmt_vid_cap(file, priv, f); 126 if (ret) 127 return ret; 128 129 dev_dbg(vcapture->ved.dev, "%s: format update: " 130 "old:%dx%d (0x%x, %d, %d, %d, %d) " 131 "new:%dx%d (0x%x, %d, %d, %d, %d)\n", vcapture->vdev.name, 132 /* old */ 133 vcapture->format.width, vcapture->format.height, 134 vcapture->format.pixelformat, vcapture->format.colorspace, 135 vcapture->format.quantization, vcapture->format.xfer_func, 136 vcapture->format.ycbcr_enc, 137 /* new */ 138 f->fmt.pix.width, f->fmt.pix.height, 139 f->fmt.pix.pixelformat, f->fmt.pix.colorspace, 140 f->fmt.pix.quantization, f->fmt.pix.xfer_func, 141 f->fmt.pix.ycbcr_enc); 142 143 vcapture->format = f->fmt.pix; 144 145 return 0; 146 } 147 148 static int vimc_capture_enum_fmt_vid_cap(struct file *file, void *priv, 149 struct v4l2_fmtdesc *f) 150 { 151 const struct vimc_pix_map *vpix; 152 153 if (f->mbus_code) { 154 if (f->index > 0) 155 return -EINVAL; 156 157 vpix = vimc_pix_map_by_code(f->mbus_code); 158 } else { 159 vpix = vimc_pix_map_by_index(f->index); 160 } 161 162 if (!vpix) 163 return -EINVAL; 164 165 f->pixelformat = vpix->pixelformat; 166 167 return 0; 168 } 169 170 static int vimc_capture_enum_framesizes(struct file *file, void *priv, 171 struct v4l2_frmsizeenum *fsize) 172 { 173 const struct vimc_pix_map *vpix; 174 175 if (fsize->index) 176 return -EINVAL; 177 178 /* Only accept code in the pix map table */ 179 vpix = vimc_pix_map_by_code(fsize->pixel_format); 180 if (!vpix) 181 return -EINVAL; 182 183 fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS; 184 fsize->stepwise.min_width = VIMC_FRAME_MIN_WIDTH; 185 fsize->stepwise.max_width = VIMC_FRAME_MAX_WIDTH; 186 fsize->stepwise.min_height = VIMC_FRAME_MIN_HEIGHT; 187 fsize->stepwise.max_height = VIMC_FRAME_MAX_HEIGHT; 188 fsize->stepwise.step_width = 1; 189 fsize->stepwise.step_height = 1; 190 191 return 0; 192 } 193 194 static const struct v4l2_file_operations vimc_capture_fops = { 195 .owner = THIS_MODULE, 196 .open = v4l2_fh_open, 197 .release = vb2_fop_release, 198 .read = vb2_fop_read, 199 .poll = vb2_fop_poll, 200 .unlocked_ioctl = video_ioctl2, 201 .mmap = vb2_fop_mmap, 202 }; 203 204 static const struct v4l2_ioctl_ops vimc_capture_ioctl_ops = { 205 .vidioc_querycap = vimc_capture_querycap, 206 207 .vidioc_g_fmt_vid_cap = vimc_capture_g_fmt_vid_cap, 208 .vidioc_s_fmt_vid_cap = vimc_capture_s_fmt_vid_cap, 209 .vidioc_try_fmt_vid_cap = vimc_capture_try_fmt_vid_cap, 210 .vidioc_enum_fmt_vid_cap = vimc_capture_enum_fmt_vid_cap, 211 .vidioc_enum_framesizes = vimc_capture_enum_framesizes, 212 213 .vidioc_reqbufs = vb2_ioctl_reqbufs, 214 .vidioc_create_bufs = vb2_ioctl_create_bufs, 215 .vidioc_prepare_buf = vb2_ioctl_prepare_buf, 216 .vidioc_querybuf = vb2_ioctl_querybuf, 217 .vidioc_qbuf = vb2_ioctl_qbuf, 218 .vidioc_dqbuf = vb2_ioctl_dqbuf, 219 .vidioc_expbuf = vb2_ioctl_expbuf, 220 .vidioc_streamon = vb2_ioctl_streamon, 221 .vidioc_streamoff = vb2_ioctl_streamoff, 222 .vidioc_remove_bufs = vb2_ioctl_remove_bufs, 223 }; 224 225 static void vimc_capture_return_all_buffers(struct vimc_capture_device *vcapture, 226 enum vb2_buffer_state state) 227 { 228 struct vimc_capture_buffer *vbuf, *node; 229 230 spin_lock(&vcapture->qlock); 231 232 list_for_each_entry_safe(vbuf, node, &vcapture->buf_list, list) { 233 list_del(&vbuf->list); 234 vb2_buffer_done(&vbuf->vb2.vb2_buf, state); 235 } 236 237 spin_unlock(&vcapture->qlock); 238 } 239 240 static int vimc_capture_start_streaming(struct vb2_queue *vq, unsigned int count) 241 { 242 struct vimc_capture_device *vcapture = vb2_get_drv_priv(vq); 243 int ret; 244 245 vcapture->sequence = 0; 246 247 /* Start the media pipeline */ 248 ret = video_device_pipeline_start(&vcapture->vdev, &vcapture->stream.pipe); 249 if (ret) { 250 vimc_capture_return_all_buffers(vcapture, VB2_BUF_STATE_QUEUED); 251 return ret; 252 } 253 254 ret = vimc_streamer_s_stream(&vcapture->stream, &vcapture->ved, 1); 255 if (ret) { 256 video_device_pipeline_stop(&vcapture->vdev); 257 vimc_capture_return_all_buffers(vcapture, VB2_BUF_STATE_QUEUED); 258 return ret; 259 } 260 261 return 0; 262 } 263 264 /* 265 * Stop the stream engine. Any remaining buffers in the stream queue are 266 * dequeued and passed on to the vb2 framework marked as STATE_ERROR. 267 */ 268 static void vimc_capture_stop_streaming(struct vb2_queue *vq) 269 { 270 struct vimc_capture_device *vcapture = vb2_get_drv_priv(vq); 271 272 vimc_streamer_s_stream(&vcapture->stream, &vcapture->ved, 0); 273 274 /* Stop the media pipeline */ 275 video_device_pipeline_stop(&vcapture->vdev); 276 277 /* Release all active buffers */ 278 vimc_capture_return_all_buffers(vcapture, VB2_BUF_STATE_ERROR); 279 } 280 281 static void vimc_capture_buf_queue(struct vb2_buffer *vb2_buf) 282 { 283 struct vimc_capture_device *vcapture = vb2_get_drv_priv(vb2_buf->vb2_queue); 284 struct vimc_capture_buffer *buf = container_of(vb2_buf, 285 struct vimc_capture_buffer, 286 vb2.vb2_buf); 287 288 spin_lock(&vcapture->qlock); 289 list_add_tail(&buf->list, &vcapture->buf_list); 290 spin_unlock(&vcapture->qlock); 291 } 292 293 static int vimc_capture_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers, 294 unsigned int *nplanes, unsigned int sizes[], 295 struct device *alloc_devs[]) 296 { 297 struct vimc_capture_device *vcapture = vb2_get_drv_priv(vq); 298 299 if (*nplanes) 300 return sizes[0] < vcapture->format.sizeimage ? -EINVAL : 0; 301 /* We don't support multiplanes for now */ 302 *nplanes = 1; 303 sizes[0] = vcapture->format.sizeimage; 304 305 return 0; 306 } 307 308 static int vimc_capture_buffer_prepare(struct vb2_buffer *vb) 309 { 310 struct vimc_capture_device *vcapture = vb2_get_drv_priv(vb->vb2_queue); 311 unsigned long size = vcapture->format.sizeimage; 312 313 if (vb2_plane_size(vb, 0) < size) { 314 dev_err(vcapture->ved.dev, "%s: buffer too small (%lu < %lu)\n", 315 vcapture->vdev.name, vb2_plane_size(vb, 0), size); 316 return -EINVAL; 317 } 318 return 0; 319 } 320 321 static const struct vb2_ops vimc_capture_qops = { 322 .start_streaming = vimc_capture_start_streaming, 323 .stop_streaming = vimc_capture_stop_streaming, 324 .buf_queue = vimc_capture_buf_queue, 325 .queue_setup = vimc_capture_queue_setup, 326 .buf_prepare = vimc_capture_buffer_prepare, 327 }; 328 329 static const struct media_entity_operations vimc_capture_mops = { 330 .link_validate = vimc_vdev_link_validate, 331 }; 332 333 static void vimc_capture_release(struct vimc_ent_device *ved) 334 { 335 struct vimc_capture_device *vcapture = 336 container_of(ved, struct vimc_capture_device, ved); 337 338 media_entity_cleanup(vcapture->ved.ent); 339 kfree(vcapture); 340 } 341 342 static void vimc_capture_unregister(struct vimc_ent_device *ved) 343 { 344 struct vimc_capture_device *vcapture = 345 container_of(ved, struct vimc_capture_device, ved); 346 347 vb2_video_unregister_device(&vcapture->vdev); 348 } 349 350 static void *vimc_capture_process_frame(struct vimc_ent_device *ved, 351 const void *frame) 352 { 353 struct vimc_capture_device *vcapture = container_of(ved, struct vimc_capture_device, 354 ved); 355 struct vimc_capture_buffer *vimc_buf; 356 void *vbuf; 357 358 spin_lock(&vcapture->qlock); 359 360 /* Get the first entry of the list */ 361 vimc_buf = list_first_entry_or_null(&vcapture->buf_list, 362 typeof(*vimc_buf), list); 363 if (!vimc_buf) { 364 spin_unlock(&vcapture->qlock); 365 return ERR_PTR(-EAGAIN); 366 } 367 368 /* Remove this entry from the list */ 369 list_del(&vimc_buf->list); 370 371 spin_unlock(&vcapture->qlock); 372 373 /* Fill the buffer */ 374 vimc_buf->vb2.vb2_buf.timestamp = ktime_get_ns(); 375 vimc_buf->vb2.sequence = vcapture->sequence++; 376 vimc_buf->vb2.field = vcapture->format.field; 377 378 vbuf = vb2_plane_vaddr(&vimc_buf->vb2.vb2_buf, 0); 379 380 memcpy(vbuf, frame, vcapture->format.sizeimage); 381 382 /* Set it as ready */ 383 vb2_set_plane_payload(&vimc_buf->vb2.vb2_buf, 0, 384 vcapture->format.sizeimage); 385 vb2_buffer_done(&vimc_buf->vb2.vb2_buf, VB2_BUF_STATE_DONE); 386 return NULL; 387 } 388 389 static struct vimc_ent_device *vimc_capture_add(struct vimc_device *vimc, 390 const char *vcfg_name) 391 { 392 struct v4l2_device *v4l2_dev = &vimc->v4l2_dev; 393 const struct vimc_pix_map *vpix; 394 struct vimc_capture_device *vcapture; 395 struct video_device *vdev; 396 struct vb2_queue *q; 397 int ret; 398 399 /* Allocate the vimc_capture_device struct */ 400 vcapture = kzalloc(sizeof(*vcapture), GFP_KERNEL); 401 if (!vcapture) 402 return ERR_PTR(-ENOMEM); 403 404 /* Initialize the media entity */ 405 vcapture->vdev.entity.name = vcfg_name; 406 vcapture->vdev.entity.function = MEDIA_ENT_F_IO_V4L; 407 vcapture->pad.flags = MEDIA_PAD_FL_SINK; 408 ret = media_entity_pads_init(&vcapture->vdev.entity, 409 1, &vcapture->pad); 410 if (ret) 411 goto err_free_vcapture; 412 413 /* Initialize the lock */ 414 mutex_init(&vcapture->lock); 415 416 /* Initialize the vb2 queue */ 417 q = &vcapture->queue; 418 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 419 q->io_modes = VB2_MMAP | VB2_DMABUF; 420 if (vimc_allocator == VIMC_ALLOCATOR_VMALLOC) 421 q->io_modes |= VB2_USERPTR; 422 q->drv_priv = vcapture; 423 q->buf_struct_size = sizeof(struct vimc_capture_buffer); 424 q->ops = &vimc_capture_qops; 425 q->mem_ops = vimc_allocator == VIMC_ALLOCATOR_DMA_CONTIG 426 ? &vb2_dma_contig_memops : &vb2_vmalloc_memops; 427 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 428 q->min_reqbufs_allocation = 2; 429 q->lock = &vcapture->lock; 430 q->dev = v4l2_dev->dev; 431 432 ret = vb2_queue_init(q); 433 if (ret) { 434 dev_err(vimc->mdev.dev, "%s: vb2 queue init failed (err=%d)\n", 435 vcfg_name, ret); 436 goto err_clean_m_ent; 437 } 438 439 /* Initialize buffer list and its lock */ 440 INIT_LIST_HEAD(&vcapture->buf_list); 441 spin_lock_init(&vcapture->qlock); 442 443 /* Set default frame format */ 444 vcapture->format = fmt_default; 445 vpix = vimc_pix_map_by_pixelformat(vcapture->format.pixelformat); 446 vcapture->format.bytesperline = vcapture->format.width * vpix->bpp; 447 vcapture->format.sizeimage = vcapture->format.bytesperline * 448 vcapture->format.height; 449 450 /* Fill the vimc_ent_device struct */ 451 vcapture->ved.ent = &vcapture->vdev.entity; 452 vcapture->ved.process_frame = vimc_capture_process_frame; 453 vcapture->ved.vdev_get_format = vimc_capture_get_format; 454 vcapture->ved.dev = vimc->mdev.dev; 455 456 /* Initialize the video_device struct */ 457 vdev = &vcapture->vdev; 458 vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING 459 | V4L2_CAP_IO_MC; 460 vdev->entity.ops = &vimc_capture_mops; 461 vdev->release = video_device_release_empty; 462 vdev->fops = &vimc_capture_fops; 463 vdev->ioctl_ops = &vimc_capture_ioctl_ops; 464 vdev->lock = &vcapture->lock; 465 vdev->queue = q; 466 vdev->v4l2_dev = v4l2_dev; 467 vdev->vfl_dir = VFL_DIR_RX; 468 strscpy(vdev->name, vcfg_name, sizeof(vdev->name)); 469 video_set_drvdata(vdev, &vcapture->ved); 470 471 /* Register the video_device with the v4l2 and the media framework */ 472 ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1); 473 if (ret) { 474 dev_err(vimc->mdev.dev, "%s: video register failed (err=%d)\n", 475 vcapture->vdev.name, ret); 476 goto err_clean_m_ent; 477 } 478 479 return &vcapture->ved; 480 481 err_clean_m_ent: 482 media_entity_cleanup(&vcapture->vdev.entity); 483 err_free_vcapture: 484 kfree(vcapture); 485 486 return ERR_PTR(ret); 487 } 488 489 const struct vimc_ent_type vimc_capture_type = { 490 .add = vimc_capture_add, 491 .unregister = vimc_capture_unregister, 492 .release = vimc_capture_release 493 }; 494