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