1 /* 2 * videobuf2-v4l2.c - V4L2 driver helper framework 3 * 4 * Copyright (C) 2010 Samsung Electronics 5 * 6 * Author: Pawel Osciak <pawel@osciak.com> 7 * Marek Szyprowski <m.szyprowski@samsung.com> 8 * 9 * The vb2_thread implementation was based on code from videobuf-dvb.c: 10 * (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SUSE Labs] 11 * 12 * This program is free software; you can redistribute it and/or modify 13 * it under the terms of the GNU General Public License as published by 14 * the Free Software Foundation. 15 */ 16 17 #include <linux/err.h> 18 #include <linux/kernel.h> 19 #include <linux/module.h> 20 #include <linux/mm.h> 21 #include <linux/poll.h> 22 #include <linux/slab.h> 23 #include <linux/sched.h> 24 #include <linux/freezer.h> 25 #include <linux/kthread.h> 26 27 #include <media/v4l2-dev.h> 28 #include <media/v4l2-device.h> 29 #include <media/v4l2-fh.h> 30 #include <media/v4l2-event.h> 31 #include <media/v4l2-common.h> 32 33 #include <media/videobuf2-v4l2.h> 34 35 static int debug; 36 module_param(debug, int, 0644); 37 38 #define dprintk(level, fmt, arg...) \ 39 do { \ 40 if (debug >= level) \ 41 pr_info("vb2-v4l2: %s: " fmt, __func__, ## arg); \ 42 } while (0) 43 44 /* Flags that are set by us */ 45 #define V4L2_BUFFER_MASK_FLAGS (V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | \ 46 V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR | \ 47 V4L2_BUF_FLAG_PREPARED | \ 48 V4L2_BUF_FLAG_IN_REQUEST | \ 49 V4L2_BUF_FLAG_REQUEST_FD | \ 50 V4L2_BUF_FLAG_TIMESTAMP_MASK) 51 /* Output buffer flags that should be passed on to the driver */ 52 #define V4L2_BUFFER_OUT_FLAGS (V4L2_BUF_FLAG_PFRAME | \ 53 V4L2_BUF_FLAG_BFRAME | \ 54 V4L2_BUF_FLAG_KEYFRAME | \ 55 V4L2_BUF_FLAG_TIMECODE | \ 56 V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF) 57 58 /* 59 * __verify_planes_array() - verify that the planes array passed in struct 60 * v4l2_buffer from userspace can be safely used 61 */ 62 static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b) 63 { 64 if (!V4L2_TYPE_IS_MULTIPLANAR(b->type)) 65 return 0; 66 67 /* Is memory for copying plane information present? */ 68 if (b->m.planes == NULL) { 69 dprintk(1, "multi-planar buffer passed but planes array not provided\n"); 70 return -EINVAL; 71 } 72 73 if (b->length < vb->num_planes || b->length > VB2_MAX_PLANES) { 74 dprintk(1, "incorrect planes array length, expected %d, got %d\n", 75 vb->num_planes, b->length); 76 return -EINVAL; 77 } 78 79 return 0; 80 } 81 82 static int __verify_planes_array_core(struct vb2_buffer *vb, const void *pb) 83 { 84 return __verify_planes_array(vb, pb); 85 } 86 87 /* 88 * __verify_length() - Verify that the bytesused value for each plane fits in 89 * the plane length and that the data offset doesn't exceed the bytesused value. 90 */ 91 static int __verify_length(struct vb2_buffer *vb, const struct v4l2_buffer *b) 92 { 93 unsigned int length; 94 unsigned int bytesused; 95 unsigned int plane; 96 97 if (!V4L2_TYPE_IS_OUTPUT(b->type)) 98 return 0; 99 100 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) { 101 for (plane = 0; plane < vb->num_planes; ++plane) { 102 length = (b->memory == VB2_MEMORY_USERPTR || 103 b->memory == VB2_MEMORY_DMABUF) 104 ? b->m.planes[plane].length 105 : vb->planes[plane].length; 106 bytesused = b->m.planes[plane].bytesused 107 ? b->m.planes[plane].bytesused : length; 108 109 if (b->m.planes[plane].bytesused > length) 110 return -EINVAL; 111 112 if (b->m.planes[plane].data_offset > 0 && 113 b->m.planes[plane].data_offset >= bytesused) 114 return -EINVAL; 115 } 116 } else { 117 length = (b->memory == VB2_MEMORY_USERPTR) 118 ? b->length : vb->planes[0].length; 119 120 if (b->bytesused > length) 121 return -EINVAL; 122 } 123 124 return 0; 125 } 126 127 /* 128 * __init_vb2_v4l2_buffer() - initialize the vb2_v4l2_buffer struct 129 */ 130 static void __init_vb2_v4l2_buffer(struct vb2_buffer *vb) 131 { 132 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 133 134 vbuf->request_fd = -1; 135 } 136 137 static void __copy_timestamp(struct vb2_buffer *vb, const void *pb) 138 { 139 const struct v4l2_buffer *b = pb; 140 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 141 struct vb2_queue *q = vb->vb2_queue; 142 143 if (q->is_output) { 144 /* 145 * For output buffers copy the timestamp if needed, 146 * and the timecode field and flag if needed. 147 */ 148 if (q->copy_timestamp) 149 vb->timestamp = v4l2_buffer_get_timestamp(b); 150 vbuf->flags |= b->flags & V4L2_BUF_FLAG_TIMECODE; 151 if (b->flags & V4L2_BUF_FLAG_TIMECODE) 152 vbuf->timecode = b->timecode; 153 } 154 }; 155 156 static void vb2_warn_zero_bytesused(struct vb2_buffer *vb) 157 { 158 static bool check_once; 159 160 if (check_once) 161 return; 162 163 check_once = true; 164 165 pr_warn("use of bytesused == 0 is deprecated and will be removed in the future,\n"); 166 if (vb->vb2_queue->allow_zero_bytesused) 167 pr_warn("use VIDIOC_DECODER_CMD(V4L2_DEC_CMD_STOP) instead.\n"); 168 else 169 pr_warn("use the actual size instead.\n"); 170 } 171 172 static int vb2_fill_vb2_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b) 173 { 174 struct vb2_queue *q = vb->vb2_queue; 175 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 176 struct vb2_plane *planes = vbuf->planes; 177 unsigned int plane; 178 int ret; 179 180 ret = __verify_length(vb, b); 181 if (ret < 0) { 182 dprintk(1, "plane parameters verification failed: %d\n", ret); 183 return ret; 184 } 185 if (b->field == V4L2_FIELD_ALTERNATE && q->is_output) { 186 /* 187 * If the format's field is ALTERNATE, then the buffer's field 188 * should be either TOP or BOTTOM, not ALTERNATE since that 189 * makes no sense. The driver has to know whether the 190 * buffer represents a top or a bottom field in order to 191 * program any DMA correctly. Using ALTERNATE is wrong, since 192 * that just says that it is either a top or a bottom field, 193 * but not which of the two it is. 194 */ 195 dprintk(1, "the field is incorrectly set to ALTERNATE for an output buffer\n"); 196 return -EINVAL; 197 } 198 vbuf->sequence = 0; 199 vbuf->request_fd = -1; 200 vbuf->is_held = false; 201 202 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) { 203 switch (b->memory) { 204 case VB2_MEMORY_USERPTR: 205 for (plane = 0; plane < vb->num_planes; ++plane) { 206 planes[plane].m.userptr = 207 b->m.planes[plane].m.userptr; 208 planes[plane].length = 209 b->m.planes[plane].length; 210 } 211 break; 212 case VB2_MEMORY_DMABUF: 213 for (plane = 0; plane < vb->num_planes; ++plane) { 214 planes[plane].m.fd = 215 b->m.planes[plane].m.fd; 216 planes[plane].length = 217 b->m.planes[plane].length; 218 } 219 break; 220 default: 221 for (plane = 0; plane < vb->num_planes; ++plane) { 222 planes[plane].m.offset = 223 vb->planes[plane].m.offset; 224 planes[plane].length = 225 vb->planes[plane].length; 226 } 227 break; 228 } 229 230 /* Fill in driver-provided information for OUTPUT types */ 231 if (V4L2_TYPE_IS_OUTPUT(b->type)) { 232 /* 233 * Will have to go up to b->length when API starts 234 * accepting variable number of planes. 235 * 236 * If bytesused == 0 for the output buffer, then fall 237 * back to the full buffer size. In that case 238 * userspace clearly never bothered to set it and 239 * it's a safe assumption that they really meant to 240 * use the full plane sizes. 241 * 242 * Some drivers, e.g. old codec drivers, use bytesused == 0 243 * as a way to indicate that streaming is finished. 244 * In that case, the driver should use the 245 * allow_zero_bytesused flag to keep old userspace 246 * applications working. 247 */ 248 for (plane = 0; plane < vb->num_planes; ++plane) { 249 struct vb2_plane *pdst = &planes[plane]; 250 struct v4l2_plane *psrc = &b->m.planes[plane]; 251 252 if (psrc->bytesused == 0) 253 vb2_warn_zero_bytesused(vb); 254 255 if (vb->vb2_queue->allow_zero_bytesused) 256 pdst->bytesused = psrc->bytesused; 257 else 258 pdst->bytesused = psrc->bytesused ? 259 psrc->bytesused : pdst->length; 260 pdst->data_offset = psrc->data_offset; 261 } 262 } 263 } else { 264 /* 265 * Single-planar buffers do not use planes array, 266 * so fill in relevant v4l2_buffer struct fields instead. 267 * In videobuf we use our internal V4l2_planes struct for 268 * single-planar buffers as well, for simplicity. 269 * 270 * If bytesused == 0 for the output buffer, then fall back 271 * to the full buffer size as that's a sensible default. 272 * 273 * Some drivers, e.g. old codec drivers, use bytesused == 0 as 274 * a way to indicate that streaming is finished. In that case, 275 * the driver should use the allow_zero_bytesused flag to keep 276 * old userspace applications working. 277 */ 278 switch (b->memory) { 279 case VB2_MEMORY_USERPTR: 280 planes[0].m.userptr = b->m.userptr; 281 planes[0].length = b->length; 282 break; 283 case VB2_MEMORY_DMABUF: 284 planes[0].m.fd = b->m.fd; 285 planes[0].length = b->length; 286 break; 287 default: 288 planes[0].m.offset = vb->planes[0].m.offset; 289 planes[0].length = vb->planes[0].length; 290 break; 291 } 292 293 planes[0].data_offset = 0; 294 if (V4L2_TYPE_IS_OUTPUT(b->type)) { 295 if (b->bytesused == 0) 296 vb2_warn_zero_bytesused(vb); 297 298 if (vb->vb2_queue->allow_zero_bytesused) 299 planes[0].bytesused = b->bytesused; 300 else 301 planes[0].bytesused = b->bytesused ? 302 b->bytesused : planes[0].length; 303 } else 304 planes[0].bytesused = 0; 305 306 } 307 308 /* Zero flags that we handle */ 309 vbuf->flags = b->flags & ~V4L2_BUFFER_MASK_FLAGS; 310 if (!vb->vb2_queue->copy_timestamp || !V4L2_TYPE_IS_OUTPUT(b->type)) { 311 /* 312 * Non-COPY timestamps and non-OUTPUT queues will get 313 * their timestamp and timestamp source flags from the 314 * queue. 315 */ 316 vbuf->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK; 317 } 318 319 if (V4L2_TYPE_IS_OUTPUT(b->type)) { 320 /* 321 * For output buffers mask out the timecode flag: 322 * this will be handled later in vb2_qbuf(). 323 * The 'field' is valid metadata for this output buffer 324 * and so that needs to be copied here. 325 */ 326 vbuf->flags &= ~V4L2_BUF_FLAG_TIMECODE; 327 vbuf->field = b->field; 328 if (!(q->subsystem_flags & VB2_V4L2_FL_SUPPORTS_M2M_HOLD_CAPTURE_BUF)) 329 vbuf->flags &= ~V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF; 330 } else { 331 /* Zero any output buffer flags as this is a capture buffer */ 332 vbuf->flags &= ~V4L2_BUFFER_OUT_FLAGS; 333 /* Zero last flag, this is a signal from driver to userspace */ 334 vbuf->flags &= ~V4L2_BUF_FLAG_LAST; 335 } 336 337 return 0; 338 } 339 340 static int vb2_queue_or_prepare_buf(struct vb2_queue *q, struct media_device *mdev, 341 struct v4l2_buffer *b, bool is_prepare, 342 struct media_request **p_req) 343 { 344 const char *opname = is_prepare ? "prepare_buf" : "qbuf"; 345 struct media_request *req; 346 struct vb2_v4l2_buffer *vbuf; 347 struct vb2_buffer *vb; 348 int ret; 349 350 if (b->type != q->type) { 351 dprintk(1, "%s: invalid buffer type\n", opname); 352 return -EINVAL; 353 } 354 355 if (b->index >= q->num_buffers) { 356 dprintk(1, "%s: buffer index out of range\n", opname); 357 return -EINVAL; 358 } 359 360 if (q->bufs[b->index] == NULL) { 361 /* Should never happen */ 362 dprintk(1, "%s: buffer is NULL\n", opname); 363 return -EINVAL; 364 } 365 366 if (b->memory != q->memory) { 367 dprintk(1, "%s: invalid memory type\n", opname); 368 return -EINVAL; 369 } 370 371 vb = q->bufs[b->index]; 372 vbuf = to_vb2_v4l2_buffer(vb); 373 ret = __verify_planes_array(vb, b); 374 if (ret) 375 return ret; 376 377 if (!is_prepare && (b->flags & V4L2_BUF_FLAG_REQUEST_FD) && 378 vb->state != VB2_BUF_STATE_DEQUEUED) { 379 dprintk(1, "%s: buffer is not in dequeued state\n", opname); 380 return -EINVAL; 381 } 382 383 if (!vb->prepared) { 384 /* Copy relevant information provided by the userspace */ 385 memset(vbuf->planes, 0, 386 sizeof(vbuf->planes[0]) * vb->num_planes); 387 ret = vb2_fill_vb2_v4l2_buffer(vb, b); 388 if (ret) 389 return ret; 390 } 391 392 if (is_prepare) 393 return 0; 394 395 if (!(b->flags & V4L2_BUF_FLAG_REQUEST_FD)) { 396 if (q->requires_requests) { 397 dprintk(1, "%s: queue requires requests\n", opname); 398 return -EBADR; 399 } 400 if (q->uses_requests) { 401 dprintk(1, "%s: queue uses requests\n", opname); 402 return -EBUSY; 403 } 404 return 0; 405 } else if (!q->supports_requests) { 406 dprintk(1, "%s: queue does not support requests\n", opname); 407 return -EBADR; 408 } else if (q->uses_qbuf) { 409 dprintk(1, "%s: queue does not use requests\n", opname); 410 return -EBUSY; 411 } 412 413 /* 414 * For proper locking when queueing a request you need to be able 415 * to lock access to the vb2 queue, so check that there is a lock 416 * that we can use. In addition p_req must be non-NULL. 417 */ 418 if (WARN_ON(!q->lock || !p_req)) 419 return -EINVAL; 420 421 /* 422 * Make sure this op is implemented by the driver. It's easy to forget 423 * this callback, but is it important when canceling a buffer in a 424 * queued request. 425 */ 426 if (WARN_ON(!q->ops->buf_request_complete)) 427 return -EINVAL; 428 /* 429 * Make sure this op is implemented by the driver for the output queue. 430 * It's easy to forget this callback, but is it important to correctly 431 * validate the 'field' value at QBUF time. 432 */ 433 if (WARN_ON((q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT || 434 q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) && 435 !q->ops->buf_out_validate)) 436 return -EINVAL; 437 438 if (b->request_fd < 0) { 439 dprintk(1, "%s: request_fd < 0\n", opname); 440 return -EINVAL; 441 } 442 443 req = media_request_get_by_fd(mdev, b->request_fd); 444 if (IS_ERR(req)) { 445 dprintk(1, "%s: invalid request_fd\n", opname); 446 return PTR_ERR(req); 447 } 448 449 /* 450 * Early sanity check. This is checked again when the buffer 451 * is bound to the request in vb2_core_qbuf(). 452 */ 453 if (req->state != MEDIA_REQUEST_STATE_IDLE && 454 req->state != MEDIA_REQUEST_STATE_UPDATING) { 455 dprintk(1, "%s: request is not idle\n", opname); 456 media_request_put(req); 457 return -EBUSY; 458 } 459 460 *p_req = req; 461 vbuf->request_fd = b->request_fd; 462 463 return 0; 464 } 465 466 /* 467 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be 468 * returned to userspace 469 */ 470 static void __fill_v4l2_buffer(struct vb2_buffer *vb, void *pb) 471 { 472 struct v4l2_buffer *b = pb; 473 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 474 struct vb2_queue *q = vb->vb2_queue; 475 unsigned int plane; 476 477 /* Copy back data such as timestamp, flags, etc. */ 478 b->index = vb->index; 479 b->type = vb->type; 480 b->memory = vb->memory; 481 b->bytesused = 0; 482 483 b->flags = vbuf->flags; 484 b->field = vbuf->field; 485 v4l2_buffer_set_timestamp(b, vb->timestamp); 486 b->timecode = vbuf->timecode; 487 b->sequence = vbuf->sequence; 488 b->reserved2 = 0; 489 b->request_fd = 0; 490 491 if (q->is_multiplanar) { 492 /* 493 * Fill in plane-related data if userspace provided an array 494 * for it. The caller has already verified memory and size. 495 */ 496 b->length = vb->num_planes; 497 for (plane = 0; plane < vb->num_planes; ++plane) { 498 struct v4l2_plane *pdst = &b->m.planes[plane]; 499 struct vb2_plane *psrc = &vb->planes[plane]; 500 501 pdst->bytesused = psrc->bytesused; 502 pdst->length = psrc->length; 503 if (q->memory == VB2_MEMORY_MMAP) 504 pdst->m.mem_offset = psrc->m.offset; 505 else if (q->memory == VB2_MEMORY_USERPTR) 506 pdst->m.userptr = psrc->m.userptr; 507 else if (q->memory == VB2_MEMORY_DMABUF) 508 pdst->m.fd = psrc->m.fd; 509 pdst->data_offset = psrc->data_offset; 510 memset(pdst->reserved, 0, sizeof(pdst->reserved)); 511 } 512 } else { 513 /* 514 * We use length and offset in v4l2_planes array even for 515 * single-planar buffers, but userspace does not. 516 */ 517 b->length = vb->planes[0].length; 518 b->bytesused = vb->planes[0].bytesused; 519 if (q->memory == VB2_MEMORY_MMAP) 520 b->m.offset = vb->planes[0].m.offset; 521 else if (q->memory == VB2_MEMORY_USERPTR) 522 b->m.userptr = vb->planes[0].m.userptr; 523 else if (q->memory == VB2_MEMORY_DMABUF) 524 b->m.fd = vb->planes[0].m.fd; 525 } 526 527 /* 528 * Clear any buffer state related flags. 529 */ 530 b->flags &= ~V4L2_BUFFER_MASK_FLAGS; 531 b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK; 532 if (!q->copy_timestamp) { 533 /* 534 * For non-COPY timestamps, drop timestamp source bits 535 * and obtain the timestamp source from the queue. 536 */ 537 b->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK; 538 b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK; 539 } 540 541 switch (vb->state) { 542 case VB2_BUF_STATE_QUEUED: 543 case VB2_BUF_STATE_ACTIVE: 544 b->flags |= V4L2_BUF_FLAG_QUEUED; 545 break; 546 case VB2_BUF_STATE_IN_REQUEST: 547 b->flags |= V4L2_BUF_FLAG_IN_REQUEST; 548 break; 549 case VB2_BUF_STATE_ERROR: 550 b->flags |= V4L2_BUF_FLAG_ERROR; 551 /* fall through */ 552 case VB2_BUF_STATE_DONE: 553 b->flags |= V4L2_BUF_FLAG_DONE; 554 break; 555 case VB2_BUF_STATE_PREPARING: 556 case VB2_BUF_STATE_DEQUEUED: 557 /* nothing */ 558 break; 559 } 560 561 if ((vb->state == VB2_BUF_STATE_DEQUEUED || 562 vb->state == VB2_BUF_STATE_IN_REQUEST) && 563 vb->synced && vb->prepared) 564 b->flags |= V4L2_BUF_FLAG_PREPARED; 565 566 if (vb2_buffer_in_use(q, vb)) 567 b->flags |= V4L2_BUF_FLAG_MAPPED; 568 if (vbuf->request_fd >= 0) { 569 b->flags |= V4L2_BUF_FLAG_REQUEST_FD; 570 b->request_fd = vbuf->request_fd; 571 } 572 } 573 574 /* 575 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a 576 * v4l2_buffer by the userspace. It also verifies that struct 577 * v4l2_buffer has a valid number of planes. 578 */ 579 static int __fill_vb2_buffer(struct vb2_buffer *vb, struct vb2_plane *planes) 580 { 581 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 582 unsigned int plane; 583 584 if (!vb->vb2_queue->copy_timestamp) 585 vb->timestamp = 0; 586 587 for (plane = 0; plane < vb->num_planes; ++plane) { 588 if (vb->vb2_queue->memory != VB2_MEMORY_MMAP) { 589 planes[plane].m = vbuf->planes[plane].m; 590 planes[plane].length = vbuf->planes[plane].length; 591 } 592 planes[plane].bytesused = vbuf->planes[plane].bytesused; 593 planes[plane].data_offset = vbuf->planes[plane].data_offset; 594 } 595 return 0; 596 } 597 598 static const struct vb2_buf_ops v4l2_buf_ops = { 599 .verify_planes_array = __verify_planes_array_core, 600 .init_buffer = __init_vb2_v4l2_buffer, 601 .fill_user_buffer = __fill_v4l2_buffer, 602 .fill_vb2_buffer = __fill_vb2_buffer, 603 .copy_timestamp = __copy_timestamp, 604 }; 605 606 int vb2_find_timestamp(const struct vb2_queue *q, u64 timestamp, 607 unsigned int start_idx) 608 { 609 unsigned int i; 610 611 for (i = start_idx; i < q->num_buffers; i++) 612 if (q->bufs[i]->copied_timestamp && 613 q->bufs[i]->timestamp == timestamp) 614 return i; 615 return -1; 616 } 617 EXPORT_SYMBOL_GPL(vb2_find_timestamp); 618 619 /* 620 * vb2_querybuf() - query video buffer information 621 * @q: videobuf queue 622 * @b: buffer struct passed from userspace to vidioc_querybuf handler 623 * in driver 624 * 625 * Should be called from vidioc_querybuf ioctl handler in driver. 626 * This function will verify the passed v4l2_buffer structure and fill the 627 * relevant information for the userspace. 628 * 629 * The return values from this function are intended to be directly returned 630 * from vidioc_querybuf handler in driver. 631 */ 632 int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b) 633 { 634 struct vb2_buffer *vb; 635 int ret; 636 637 if (b->type != q->type) { 638 dprintk(1, "wrong buffer type\n"); 639 return -EINVAL; 640 } 641 642 if (b->index >= q->num_buffers) { 643 dprintk(1, "buffer index out of range\n"); 644 return -EINVAL; 645 } 646 vb = q->bufs[b->index]; 647 ret = __verify_planes_array(vb, b); 648 if (!ret) 649 vb2_core_querybuf(q, b->index, b); 650 return ret; 651 } 652 EXPORT_SYMBOL(vb2_querybuf); 653 654 static void fill_buf_caps(struct vb2_queue *q, u32 *caps) 655 { 656 *caps = V4L2_BUF_CAP_SUPPORTS_ORPHANED_BUFS; 657 if (q->io_modes & VB2_MMAP) 658 *caps |= V4L2_BUF_CAP_SUPPORTS_MMAP; 659 if (q->io_modes & VB2_USERPTR) 660 *caps |= V4L2_BUF_CAP_SUPPORTS_USERPTR; 661 if (q->io_modes & VB2_DMABUF) 662 *caps |= V4L2_BUF_CAP_SUPPORTS_DMABUF; 663 if (q->subsystem_flags & VB2_V4L2_FL_SUPPORTS_M2M_HOLD_CAPTURE_BUF) 664 *caps |= V4L2_BUF_CAP_SUPPORTS_M2M_HOLD_CAPTURE_BUF; 665 #ifdef CONFIG_MEDIA_CONTROLLER_REQUEST_API 666 if (q->supports_requests) 667 *caps |= V4L2_BUF_CAP_SUPPORTS_REQUESTS; 668 #endif 669 } 670 671 int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req) 672 { 673 int ret = vb2_verify_memory_type(q, req->memory, req->type); 674 675 fill_buf_caps(q, &req->capabilities); 676 return ret ? ret : vb2_core_reqbufs(q, req->memory, &req->count); 677 } 678 EXPORT_SYMBOL_GPL(vb2_reqbufs); 679 680 int vb2_prepare_buf(struct vb2_queue *q, struct media_device *mdev, 681 struct v4l2_buffer *b) 682 { 683 int ret; 684 685 if (vb2_fileio_is_active(q)) { 686 dprintk(1, "file io in progress\n"); 687 return -EBUSY; 688 } 689 690 if (b->flags & V4L2_BUF_FLAG_REQUEST_FD) 691 return -EINVAL; 692 693 ret = vb2_queue_or_prepare_buf(q, mdev, b, true, NULL); 694 695 return ret ? ret : vb2_core_prepare_buf(q, b->index, b); 696 } 697 EXPORT_SYMBOL_GPL(vb2_prepare_buf); 698 699 int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create) 700 { 701 unsigned requested_planes = 1; 702 unsigned requested_sizes[VIDEO_MAX_PLANES]; 703 struct v4l2_format *f = &create->format; 704 int ret = vb2_verify_memory_type(q, create->memory, f->type); 705 unsigned i; 706 707 fill_buf_caps(q, &create->capabilities); 708 create->index = q->num_buffers; 709 if (create->count == 0) 710 return ret != -EBUSY ? ret : 0; 711 712 switch (f->type) { 713 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE: 714 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE: 715 requested_planes = f->fmt.pix_mp.num_planes; 716 if (requested_planes == 0 || 717 requested_planes > VIDEO_MAX_PLANES) 718 return -EINVAL; 719 for (i = 0; i < requested_planes; i++) 720 requested_sizes[i] = 721 f->fmt.pix_mp.plane_fmt[i].sizeimage; 722 break; 723 case V4L2_BUF_TYPE_VIDEO_CAPTURE: 724 case V4L2_BUF_TYPE_VIDEO_OUTPUT: 725 requested_sizes[0] = f->fmt.pix.sizeimage; 726 break; 727 case V4L2_BUF_TYPE_VBI_CAPTURE: 728 case V4L2_BUF_TYPE_VBI_OUTPUT: 729 requested_sizes[0] = f->fmt.vbi.samples_per_line * 730 (f->fmt.vbi.count[0] + f->fmt.vbi.count[1]); 731 break; 732 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE: 733 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT: 734 requested_sizes[0] = f->fmt.sliced.io_size; 735 break; 736 case V4L2_BUF_TYPE_SDR_CAPTURE: 737 case V4L2_BUF_TYPE_SDR_OUTPUT: 738 requested_sizes[0] = f->fmt.sdr.buffersize; 739 break; 740 case V4L2_BUF_TYPE_META_CAPTURE: 741 case V4L2_BUF_TYPE_META_OUTPUT: 742 requested_sizes[0] = f->fmt.meta.buffersize; 743 break; 744 default: 745 return -EINVAL; 746 } 747 for (i = 0; i < requested_planes; i++) 748 if (requested_sizes[i] == 0) 749 return -EINVAL; 750 return ret ? ret : vb2_core_create_bufs(q, create->memory, 751 &create->count, requested_planes, requested_sizes); 752 } 753 EXPORT_SYMBOL_GPL(vb2_create_bufs); 754 755 int vb2_qbuf(struct vb2_queue *q, struct media_device *mdev, 756 struct v4l2_buffer *b) 757 { 758 struct media_request *req = NULL; 759 int ret; 760 761 if (vb2_fileio_is_active(q)) { 762 dprintk(1, "file io in progress\n"); 763 return -EBUSY; 764 } 765 766 ret = vb2_queue_or_prepare_buf(q, mdev, b, false, &req); 767 if (ret) 768 return ret; 769 ret = vb2_core_qbuf(q, b->index, b, req); 770 if (req) 771 media_request_put(req); 772 return ret; 773 } 774 EXPORT_SYMBOL_GPL(vb2_qbuf); 775 776 int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking) 777 { 778 int ret; 779 780 if (vb2_fileio_is_active(q)) { 781 dprintk(1, "file io in progress\n"); 782 return -EBUSY; 783 } 784 785 if (b->type != q->type) { 786 dprintk(1, "invalid buffer type\n"); 787 return -EINVAL; 788 } 789 790 ret = vb2_core_dqbuf(q, NULL, b, nonblocking); 791 792 if (!q->is_output && 793 b->flags & V4L2_BUF_FLAG_DONE && 794 b->flags & V4L2_BUF_FLAG_LAST) 795 q->last_buffer_dequeued = true; 796 797 /* 798 * After calling the VIDIOC_DQBUF V4L2_BUF_FLAG_DONE must be 799 * cleared. 800 */ 801 b->flags &= ~V4L2_BUF_FLAG_DONE; 802 803 return ret; 804 } 805 EXPORT_SYMBOL_GPL(vb2_dqbuf); 806 807 int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type) 808 { 809 if (vb2_fileio_is_active(q)) { 810 dprintk(1, "file io in progress\n"); 811 return -EBUSY; 812 } 813 return vb2_core_streamon(q, type); 814 } 815 EXPORT_SYMBOL_GPL(vb2_streamon); 816 817 int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type) 818 { 819 if (vb2_fileio_is_active(q)) { 820 dprintk(1, "file io in progress\n"); 821 return -EBUSY; 822 } 823 return vb2_core_streamoff(q, type); 824 } 825 EXPORT_SYMBOL_GPL(vb2_streamoff); 826 827 int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb) 828 { 829 return vb2_core_expbuf(q, &eb->fd, eb->type, eb->index, 830 eb->plane, eb->flags); 831 } 832 EXPORT_SYMBOL_GPL(vb2_expbuf); 833 834 int vb2_queue_init(struct vb2_queue *q) 835 { 836 /* 837 * Sanity check 838 */ 839 if (WARN_ON(!q) || 840 WARN_ON(q->timestamp_flags & 841 ~(V4L2_BUF_FLAG_TIMESTAMP_MASK | 842 V4L2_BUF_FLAG_TSTAMP_SRC_MASK))) 843 return -EINVAL; 844 845 /* Warn that the driver should choose an appropriate timestamp type */ 846 WARN_ON((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) == 847 V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN); 848 849 /* Warn that vb2_memory should match with v4l2_memory */ 850 if (WARN_ON(VB2_MEMORY_MMAP != (int)V4L2_MEMORY_MMAP) 851 || WARN_ON(VB2_MEMORY_USERPTR != (int)V4L2_MEMORY_USERPTR) 852 || WARN_ON(VB2_MEMORY_DMABUF != (int)V4L2_MEMORY_DMABUF)) 853 return -EINVAL; 854 855 if (q->buf_struct_size == 0) 856 q->buf_struct_size = sizeof(struct vb2_v4l2_buffer); 857 858 q->buf_ops = &v4l2_buf_ops; 859 q->is_multiplanar = V4L2_TYPE_IS_MULTIPLANAR(q->type); 860 q->is_output = V4L2_TYPE_IS_OUTPUT(q->type); 861 q->copy_timestamp = (q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) 862 == V4L2_BUF_FLAG_TIMESTAMP_COPY; 863 /* 864 * For compatibility with vb1: if QBUF hasn't been called yet, then 865 * return EPOLLERR as well. This only affects capture queues, output 866 * queues will always initialize waiting_for_buffers to false. 867 */ 868 q->quirk_poll_must_check_waiting_for_buffers = true; 869 870 return vb2_core_queue_init(q); 871 } 872 EXPORT_SYMBOL_GPL(vb2_queue_init); 873 874 void vb2_queue_release(struct vb2_queue *q) 875 { 876 vb2_core_queue_release(q); 877 } 878 EXPORT_SYMBOL_GPL(vb2_queue_release); 879 880 __poll_t vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait) 881 { 882 struct video_device *vfd = video_devdata(file); 883 __poll_t res; 884 885 res = vb2_core_poll(q, file, wait); 886 887 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) { 888 struct v4l2_fh *fh = file->private_data; 889 890 poll_wait(file, &fh->wait, wait); 891 if (v4l2_event_pending(fh)) 892 res |= EPOLLPRI; 893 } 894 895 return res; 896 } 897 EXPORT_SYMBOL_GPL(vb2_poll); 898 899 /* 900 * The following functions are not part of the vb2 core API, but are helper 901 * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations 902 * and struct vb2_ops. 903 * They contain boilerplate code that most if not all drivers have to do 904 * and so they simplify the driver code. 905 */ 906 907 /* The queue is busy if there is a owner and you are not that owner. */ 908 static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file) 909 { 910 return vdev->queue->owner && vdev->queue->owner != file->private_data; 911 } 912 913 /* vb2 ioctl helpers */ 914 915 int vb2_ioctl_reqbufs(struct file *file, void *priv, 916 struct v4l2_requestbuffers *p) 917 { 918 struct video_device *vdev = video_devdata(file); 919 int res = vb2_verify_memory_type(vdev->queue, p->memory, p->type); 920 921 fill_buf_caps(vdev->queue, &p->capabilities); 922 if (res) 923 return res; 924 if (vb2_queue_is_busy(vdev, file)) 925 return -EBUSY; 926 res = vb2_core_reqbufs(vdev->queue, p->memory, &p->count); 927 /* If count == 0, then the owner has released all buffers and he 928 is no longer owner of the queue. Otherwise we have a new owner. */ 929 if (res == 0) 930 vdev->queue->owner = p->count ? file->private_data : NULL; 931 return res; 932 } 933 EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs); 934 935 int vb2_ioctl_create_bufs(struct file *file, void *priv, 936 struct v4l2_create_buffers *p) 937 { 938 struct video_device *vdev = video_devdata(file); 939 int res = vb2_verify_memory_type(vdev->queue, p->memory, 940 p->format.type); 941 942 p->index = vdev->queue->num_buffers; 943 fill_buf_caps(vdev->queue, &p->capabilities); 944 /* 945 * If count == 0, then just check if memory and type are valid. 946 * Any -EBUSY result from vb2_verify_memory_type can be mapped to 0. 947 */ 948 if (p->count == 0) 949 return res != -EBUSY ? res : 0; 950 if (res) 951 return res; 952 if (vb2_queue_is_busy(vdev, file)) 953 return -EBUSY; 954 955 res = vb2_create_bufs(vdev->queue, p); 956 if (res == 0) 957 vdev->queue->owner = file->private_data; 958 return res; 959 } 960 EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs); 961 962 int vb2_ioctl_prepare_buf(struct file *file, void *priv, 963 struct v4l2_buffer *p) 964 { 965 struct video_device *vdev = video_devdata(file); 966 967 if (vb2_queue_is_busy(vdev, file)) 968 return -EBUSY; 969 return vb2_prepare_buf(vdev->queue, vdev->v4l2_dev->mdev, p); 970 } 971 EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf); 972 973 int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p) 974 { 975 struct video_device *vdev = video_devdata(file); 976 977 /* No need to call vb2_queue_is_busy(), anyone can query buffers. */ 978 return vb2_querybuf(vdev->queue, p); 979 } 980 EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf); 981 982 int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p) 983 { 984 struct video_device *vdev = video_devdata(file); 985 986 if (vb2_queue_is_busy(vdev, file)) 987 return -EBUSY; 988 return vb2_qbuf(vdev->queue, vdev->v4l2_dev->mdev, p); 989 } 990 EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf); 991 992 int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p) 993 { 994 struct video_device *vdev = video_devdata(file); 995 996 if (vb2_queue_is_busy(vdev, file)) 997 return -EBUSY; 998 return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK); 999 } 1000 EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf); 1001 1002 int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i) 1003 { 1004 struct video_device *vdev = video_devdata(file); 1005 1006 if (vb2_queue_is_busy(vdev, file)) 1007 return -EBUSY; 1008 return vb2_streamon(vdev->queue, i); 1009 } 1010 EXPORT_SYMBOL_GPL(vb2_ioctl_streamon); 1011 1012 int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i) 1013 { 1014 struct video_device *vdev = video_devdata(file); 1015 1016 if (vb2_queue_is_busy(vdev, file)) 1017 return -EBUSY; 1018 return vb2_streamoff(vdev->queue, i); 1019 } 1020 EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff); 1021 1022 int vb2_ioctl_expbuf(struct file *file, void *priv, struct v4l2_exportbuffer *p) 1023 { 1024 struct video_device *vdev = video_devdata(file); 1025 1026 if (vb2_queue_is_busy(vdev, file)) 1027 return -EBUSY; 1028 return vb2_expbuf(vdev->queue, p); 1029 } 1030 EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf); 1031 1032 /* v4l2_file_operations helpers */ 1033 1034 int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma) 1035 { 1036 struct video_device *vdev = video_devdata(file); 1037 1038 return vb2_mmap(vdev->queue, vma); 1039 } 1040 EXPORT_SYMBOL_GPL(vb2_fop_mmap); 1041 1042 int _vb2_fop_release(struct file *file, struct mutex *lock) 1043 { 1044 struct video_device *vdev = video_devdata(file); 1045 1046 if (lock) 1047 mutex_lock(lock); 1048 if (file->private_data == vdev->queue->owner) { 1049 vb2_queue_release(vdev->queue); 1050 vdev->queue->owner = NULL; 1051 } 1052 if (lock) 1053 mutex_unlock(lock); 1054 return v4l2_fh_release(file); 1055 } 1056 EXPORT_SYMBOL_GPL(_vb2_fop_release); 1057 1058 int vb2_fop_release(struct file *file) 1059 { 1060 struct video_device *vdev = video_devdata(file); 1061 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock; 1062 1063 return _vb2_fop_release(file, lock); 1064 } 1065 EXPORT_SYMBOL_GPL(vb2_fop_release); 1066 1067 ssize_t vb2_fop_write(struct file *file, const char __user *buf, 1068 size_t count, loff_t *ppos) 1069 { 1070 struct video_device *vdev = video_devdata(file); 1071 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock; 1072 int err = -EBUSY; 1073 1074 if (!(vdev->queue->io_modes & VB2_WRITE)) 1075 return -EINVAL; 1076 if (lock && mutex_lock_interruptible(lock)) 1077 return -ERESTARTSYS; 1078 if (vb2_queue_is_busy(vdev, file)) 1079 goto exit; 1080 err = vb2_write(vdev->queue, buf, count, ppos, 1081 file->f_flags & O_NONBLOCK); 1082 if (vdev->queue->fileio) 1083 vdev->queue->owner = file->private_data; 1084 exit: 1085 if (lock) 1086 mutex_unlock(lock); 1087 return err; 1088 } 1089 EXPORT_SYMBOL_GPL(vb2_fop_write); 1090 1091 ssize_t vb2_fop_read(struct file *file, char __user *buf, 1092 size_t count, loff_t *ppos) 1093 { 1094 struct video_device *vdev = video_devdata(file); 1095 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock; 1096 int err = -EBUSY; 1097 1098 if (!(vdev->queue->io_modes & VB2_READ)) 1099 return -EINVAL; 1100 if (lock && mutex_lock_interruptible(lock)) 1101 return -ERESTARTSYS; 1102 if (vb2_queue_is_busy(vdev, file)) 1103 goto exit; 1104 err = vb2_read(vdev->queue, buf, count, ppos, 1105 file->f_flags & O_NONBLOCK); 1106 if (vdev->queue->fileio) 1107 vdev->queue->owner = file->private_data; 1108 exit: 1109 if (lock) 1110 mutex_unlock(lock); 1111 return err; 1112 } 1113 EXPORT_SYMBOL_GPL(vb2_fop_read); 1114 1115 __poll_t vb2_fop_poll(struct file *file, poll_table *wait) 1116 { 1117 struct video_device *vdev = video_devdata(file); 1118 struct vb2_queue *q = vdev->queue; 1119 struct mutex *lock = q->lock ? q->lock : vdev->lock; 1120 __poll_t res; 1121 void *fileio; 1122 1123 /* 1124 * If this helper doesn't know how to lock, then you shouldn't be using 1125 * it but you should write your own. 1126 */ 1127 WARN_ON(!lock); 1128 1129 if (lock && mutex_lock_interruptible(lock)) 1130 return EPOLLERR; 1131 1132 fileio = q->fileio; 1133 1134 res = vb2_poll(vdev->queue, file, wait); 1135 1136 /* If fileio was started, then we have a new queue owner. */ 1137 if (!fileio && q->fileio) 1138 q->owner = file->private_data; 1139 if (lock) 1140 mutex_unlock(lock); 1141 return res; 1142 } 1143 EXPORT_SYMBOL_GPL(vb2_fop_poll); 1144 1145 #ifndef CONFIG_MMU 1146 unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr, 1147 unsigned long len, unsigned long pgoff, unsigned long flags) 1148 { 1149 struct video_device *vdev = video_devdata(file); 1150 1151 return vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags); 1152 } 1153 EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area); 1154 #endif 1155 1156 /* vb2_ops helpers. Only use if vq->lock is non-NULL. */ 1157 1158 void vb2_ops_wait_prepare(struct vb2_queue *vq) 1159 { 1160 mutex_unlock(vq->lock); 1161 } 1162 EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare); 1163 1164 void vb2_ops_wait_finish(struct vb2_queue *vq) 1165 { 1166 mutex_lock(vq->lock); 1167 } 1168 EXPORT_SYMBOL_GPL(vb2_ops_wait_finish); 1169 1170 /* 1171 * Note that this function is called during validation time and 1172 * thus the req_queue_mutex is held to ensure no request objects 1173 * can be added or deleted while validating. So there is no need 1174 * to protect the objects list. 1175 */ 1176 int vb2_request_validate(struct media_request *req) 1177 { 1178 struct media_request_object *obj; 1179 int ret = 0; 1180 1181 if (!vb2_request_buffer_cnt(req)) 1182 return -ENOENT; 1183 1184 list_for_each_entry(obj, &req->objects, list) { 1185 if (!obj->ops->prepare) 1186 continue; 1187 1188 ret = obj->ops->prepare(obj); 1189 if (ret) 1190 break; 1191 } 1192 1193 if (ret) { 1194 list_for_each_entry_continue_reverse(obj, &req->objects, list) 1195 if (obj->ops->unprepare) 1196 obj->ops->unprepare(obj); 1197 return ret; 1198 } 1199 return 0; 1200 } 1201 EXPORT_SYMBOL_GPL(vb2_request_validate); 1202 1203 void vb2_request_queue(struct media_request *req) 1204 { 1205 struct media_request_object *obj, *obj_safe; 1206 1207 /* 1208 * Queue all objects. Note that buffer objects are at the end of the 1209 * objects list, after all other object types. Once buffer objects 1210 * are queued, the driver might delete them immediately (if the driver 1211 * processes the buffer at once), so we have to use 1212 * list_for_each_entry_safe() to handle the case where the object we 1213 * queue is deleted. 1214 */ 1215 list_for_each_entry_safe(obj, obj_safe, &req->objects, list) 1216 if (obj->ops->queue) 1217 obj->ops->queue(obj); 1218 } 1219 EXPORT_SYMBOL_GPL(vb2_request_queue); 1220 1221 MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2"); 1222 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski"); 1223 MODULE_LICENSE("GPL"); 1224