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 = 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, 336 const char *opname, 337 struct media_request **p_req) 338 { 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 (!(b->flags & V4L2_BUF_FLAG_REQUEST_FD)) { 381 if (q->uses_requests) { 382 dprintk(1, "%s: queue uses requests\n", opname); 383 return -EBUSY; 384 } 385 return 0; 386 } else if (!q->supports_requests) { 387 dprintk(1, "%s: queue does not support requests\n", opname); 388 return -EACCES; 389 } else if (q->uses_qbuf) { 390 dprintk(1, "%s: queue does not use requests\n", opname); 391 return -EBUSY; 392 } 393 394 /* 395 * For proper locking when queueing a request you need to be able 396 * to lock access to the vb2 queue, so check that there is a lock 397 * that we can use. In addition p_req must be non-NULL. 398 */ 399 if (WARN_ON(!q->lock || !p_req)) 400 return -EINVAL; 401 402 /* 403 * Make sure this op is implemented by the driver. It's easy to forget 404 * this callback, but is it important when canceling a buffer in a 405 * queued request. 406 */ 407 if (WARN_ON(!q->ops->buf_request_complete)) 408 return -EINVAL; 409 410 if (vb->state != VB2_BUF_STATE_DEQUEUED) { 411 dprintk(1, "%s: buffer is not in dequeued state\n", opname); 412 return -EINVAL; 413 } 414 415 if (b->request_fd < 0) { 416 dprintk(1, "%s: request_fd < 0\n", opname); 417 return -EINVAL; 418 } 419 420 req = media_request_get_by_fd(mdev, b->request_fd); 421 if (IS_ERR(req)) { 422 dprintk(1, "%s: invalid request_fd\n", opname); 423 return PTR_ERR(req); 424 } 425 426 /* 427 * Early sanity check. This is checked again when the buffer 428 * is bound to the request in vb2_core_qbuf(). 429 */ 430 if (req->state != MEDIA_REQUEST_STATE_IDLE && 431 req->state != MEDIA_REQUEST_STATE_UPDATING) { 432 dprintk(1, "%s: request is not idle\n", opname); 433 media_request_put(req); 434 return -EBUSY; 435 } 436 437 *p_req = req; 438 vbuf->request_fd = b->request_fd; 439 440 return 0; 441 } 442 443 /* 444 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be 445 * returned to userspace 446 */ 447 static void __fill_v4l2_buffer(struct vb2_buffer *vb, void *pb) 448 { 449 struct v4l2_buffer *b = pb; 450 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 451 struct vb2_queue *q = vb->vb2_queue; 452 unsigned int plane; 453 454 /* Copy back data such as timestamp, flags, etc. */ 455 b->index = vb->index; 456 b->type = vb->type; 457 b->memory = vb->memory; 458 b->bytesused = 0; 459 460 b->flags = vbuf->flags; 461 b->field = vbuf->field; 462 b->timestamp = ns_to_timeval(vb->timestamp); 463 b->timecode = vbuf->timecode; 464 b->sequence = vbuf->sequence; 465 b->reserved2 = 0; 466 b->request_fd = 0; 467 468 if (q->is_multiplanar) { 469 /* 470 * Fill in plane-related data if userspace provided an array 471 * for it. The caller has already verified memory and size. 472 */ 473 b->length = vb->num_planes; 474 for (plane = 0; plane < vb->num_planes; ++plane) { 475 struct v4l2_plane *pdst = &b->m.planes[plane]; 476 struct vb2_plane *psrc = &vb->planes[plane]; 477 478 pdst->bytesused = psrc->bytesused; 479 pdst->length = psrc->length; 480 if (q->memory == VB2_MEMORY_MMAP) 481 pdst->m.mem_offset = psrc->m.offset; 482 else if (q->memory == VB2_MEMORY_USERPTR) 483 pdst->m.userptr = psrc->m.userptr; 484 else if (q->memory == VB2_MEMORY_DMABUF) 485 pdst->m.fd = psrc->m.fd; 486 pdst->data_offset = psrc->data_offset; 487 memset(pdst->reserved, 0, sizeof(pdst->reserved)); 488 } 489 } else { 490 /* 491 * We use length and offset in v4l2_planes array even for 492 * single-planar buffers, but userspace does not. 493 */ 494 b->length = vb->planes[0].length; 495 b->bytesused = vb->planes[0].bytesused; 496 if (q->memory == VB2_MEMORY_MMAP) 497 b->m.offset = vb->planes[0].m.offset; 498 else if (q->memory == VB2_MEMORY_USERPTR) 499 b->m.userptr = vb->planes[0].m.userptr; 500 else if (q->memory == VB2_MEMORY_DMABUF) 501 b->m.fd = vb->planes[0].m.fd; 502 } 503 504 /* 505 * Clear any buffer state related flags. 506 */ 507 b->flags &= ~V4L2_BUFFER_MASK_FLAGS; 508 b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK; 509 if (!q->copy_timestamp) { 510 /* 511 * For non-COPY timestamps, drop timestamp source bits 512 * and obtain the timestamp source from the queue. 513 */ 514 b->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK; 515 b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK; 516 } 517 518 switch (vb->state) { 519 case VB2_BUF_STATE_QUEUED: 520 case VB2_BUF_STATE_ACTIVE: 521 b->flags |= V4L2_BUF_FLAG_QUEUED; 522 break; 523 case VB2_BUF_STATE_IN_REQUEST: 524 b->flags |= V4L2_BUF_FLAG_IN_REQUEST; 525 break; 526 case VB2_BUF_STATE_ERROR: 527 b->flags |= V4L2_BUF_FLAG_ERROR; 528 /* fall through */ 529 case VB2_BUF_STATE_DONE: 530 b->flags |= V4L2_BUF_FLAG_DONE; 531 break; 532 case VB2_BUF_STATE_PREPARING: 533 case VB2_BUF_STATE_DEQUEUED: 534 case VB2_BUF_STATE_REQUEUEING: 535 /* nothing */ 536 break; 537 } 538 539 if ((vb->state == VB2_BUF_STATE_DEQUEUED || 540 vb->state == VB2_BUF_STATE_IN_REQUEST) && 541 vb->synced && vb->prepared) 542 b->flags |= V4L2_BUF_FLAG_PREPARED; 543 544 if (vb2_buffer_in_use(q, vb)) 545 b->flags |= V4L2_BUF_FLAG_MAPPED; 546 if (vbuf->request_fd >= 0) { 547 b->flags |= V4L2_BUF_FLAG_REQUEST_FD; 548 b->request_fd = vbuf->request_fd; 549 } 550 551 if (!q->is_output && 552 b->flags & V4L2_BUF_FLAG_DONE && 553 b->flags & V4L2_BUF_FLAG_LAST) 554 q->last_buffer_dequeued = true; 555 } 556 557 /* 558 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a 559 * v4l2_buffer by the userspace. It also verifies that struct 560 * v4l2_buffer has a valid number of planes. 561 */ 562 static int __fill_vb2_buffer(struct vb2_buffer *vb, struct vb2_plane *planes) 563 { 564 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 565 unsigned int plane; 566 567 if (!vb->vb2_queue->is_output || !vb->vb2_queue->copy_timestamp) 568 vb->timestamp = 0; 569 570 for (plane = 0; plane < vb->num_planes; ++plane) { 571 if (vb->vb2_queue->memory != VB2_MEMORY_MMAP) { 572 planes[plane].m = vbuf->planes[plane].m; 573 planes[plane].length = vbuf->planes[plane].length; 574 } 575 planes[plane].bytesused = vbuf->planes[plane].bytesused; 576 planes[plane].data_offset = vbuf->planes[plane].data_offset; 577 } 578 return 0; 579 } 580 581 static const struct vb2_buf_ops v4l2_buf_ops = { 582 .verify_planes_array = __verify_planes_array_core, 583 .init_buffer = __init_v4l2_vb2_buffer, 584 .fill_user_buffer = __fill_v4l2_buffer, 585 .fill_vb2_buffer = __fill_vb2_buffer, 586 .copy_timestamp = __copy_timestamp, 587 }; 588 589 /* 590 * vb2_querybuf() - query video buffer information 591 * @q: videobuf queue 592 * @b: buffer struct passed from userspace to vidioc_querybuf handler 593 * in driver 594 * 595 * Should be called from vidioc_querybuf ioctl handler in driver. 596 * This function will verify the passed v4l2_buffer structure and fill the 597 * relevant information for the userspace. 598 * 599 * The return values from this function are intended to be directly returned 600 * from vidioc_querybuf handler in driver. 601 */ 602 int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b) 603 { 604 struct vb2_buffer *vb; 605 int ret; 606 607 if (b->type != q->type) { 608 dprintk(1, "wrong buffer type\n"); 609 return -EINVAL; 610 } 611 612 if (b->index >= q->num_buffers) { 613 dprintk(1, "buffer index out of range\n"); 614 return -EINVAL; 615 } 616 vb = q->bufs[b->index]; 617 ret = __verify_planes_array(vb, b); 618 if (!ret) 619 vb2_core_querybuf(q, b->index, b); 620 return ret; 621 } 622 EXPORT_SYMBOL(vb2_querybuf); 623 624 static void fill_buf_caps(struct vb2_queue *q, u32 *caps) 625 { 626 *caps = V4L2_BUF_CAP_SUPPORTS_ORPHANED_BUFS; 627 if (q->io_modes & VB2_MMAP) 628 *caps |= V4L2_BUF_CAP_SUPPORTS_MMAP; 629 if (q->io_modes & VB2_USERPTR) 630 *caps |= V4L2_BUF_CAP_SUPPORTS_USERPTR; 631 if (q->io_modes & VB2_DMABUF) 632 *caps |= V4L2_BUF_CAP_SUPPORTS_DMABUF; 633 if (q->supports_requests) 634 *caps |= V4L2_BUF_CAP_SUPPORTS_REQUESTS; 635 } 636 637 int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req) 638 { 639 int ret = vb2_verify_memory_type(q, req->memory, req->type); 640 641 fill_buf_caps(q, &req->capabilities); 642 return ret ? ret : vb2_core_reqbufs(q, req->memory, &req->count); 643 } 644 EXPORT_SYMBOL_GPL(vb2_reqbufs); 645 646 int vb2_prepare_buf(struct vb2_queue *q, struct media_device *mdev, 647 struct v4l2_buffer *b) 648 { 649 int ret; 650 651 if (vb2_fileio_is_active(q)) { 652 dprintk(1, "file io in progress\n"); 653 return -EBUSY; 654 } 655 656 if (b->flags & V4L2_BUF_FLAG_REQUEST_FD) 657 return -EINVAL; 658 659 ret = vb2_queue_or_prepare_buf(q, mdev, b, "prepare_buf", NULL); 660 661 return ret ? ret : vb2_core_prepare_buf(q, b->index, b); 662 } 663 EXPORT_SYMBOL_GPL(vb2_prepare_buf); 664 665 int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create) 666 { 667 unsigned requested_planes = 1; 668 unsigned requested_sizes[VIDEO_MAX_PLANES]; 669 struct v4l2_format *f = &create->format; 670 int ret = vb2_verify_memory_type(q, create->memory, f->type); 671 unsigned i; 672 673 fill_buf_caps(q, &create->capabilities); 674 create->index = q->num_buffers; 675 if (create->count == 0) 676 return ret != -EBUSY ? ret : 0; 677 678 switch (f->type) { 679 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE: 680 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE: 681 requested_planes = f->fmt.pix_mp.num_planes; 682 if (requested_planes == 0 || 683 requested_planes > VIDEO_MAX_PLANES) 684 return -EINVAL; 685 for (i = 0; i < requested_planes; i++) 686 requested_sizes[i] = 687 f->fmt.pix_mp.plane_fmt[i].sizeimage; 688 break; 689 case V4L2_BUF_TYPE_VIDEO_CAPTURE: 690 case V4L2_BUF_TYPE_VIDEO_OUTPUT: 691 requested_sizes[0] = f->fmt.pix.sizeimage; 692 break; 693 case V4L2_BUF_TYPE_VBI_CAPTURE: 694 case V4L2_BUF_TYPE_VBI_OUTPUT: 695 requested_sizes[0] = f->fmt.vbi.samples_per_line * 696 (f->fmt.vbi.count[0] + f->fmt.vbi.count[1]); 697 break; 698 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE: 699 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT: 700 requested_sizes[0] = f->fmt.sliced.io_size; 701 break; 702 case V4L2_BUF_TYPE_SDR_CAPTURE: 703 case V4L2_BUF_TYPE_SDR_OUTPUT: 704 requested_sizes[0] = f->fmt.sdr.buffersize; 705 break; 706 case V4L2_BUF_TYPE_META_CAPTURE: 707 case V4L2_BUF_TYPE_META_OUTPUT: 708 requested_sizes[0] = f->fmt.meta.buffersize; 709 break; 710 default: 711 return -EINVAL; 712 } 713 for (i = 0; i < requested_planes; i++) 714 if (requested_sizes[i] == 0) 715 return -EINVAL; 716 return ret ? ret : vb2_core_create_bufs(q, create->memory, 717 &create->count, requested_planes, requested_sizes); 718 } 719 EXPORT_SYMBOL_GPL(vb2_create_bufs); 720 721 int vb2_qbuf(struct vb2_queue *q, struct media_device *mdev, 722 struct v4l2_buffer *b) 723 { 724 struct media_request *req = NULL; 725 int ret; 726 727 if (vb2_fileio_is_active(q)) { 728 dprintk(1, "file io in progress\n"); 729 return -EBUSY; 730 } 731 732 ret = vb2_queue_or_prepare_buf(q, mdev, b, "qbuf", &req); 733 if (ret) 734 return ret; 735 ret = vb2_core_qbuf(q, b->index, b, req); 736 if (req) 737 media_request_put(req); 738 return ret; 739 } 740 EXPORT_SYMBOL_GPL(vb2_qbuf); 741 742 int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking) 743 { 744 int ret; 745 746 if (vb2_fileio_is_active(q)) { 747 dprintk(1, "file io in progress\n"); 748 return -EBUSY; 749 } 750 751 if (b->type != q->type) { 752 dprintk(1, "invalid buffer type\n"); 753 return -EINVAL; 754 } 755 756 ret = vb2_core_dqbuf(q, NULL, b, nonblocking); 757 758 /* 759 * After calling the VIDIOC_DQBUF V4L2_BUF_FLAG_DONE must be 760 * cleared. 761 */ 762 b->flags &= ~V4L2_BUF_FLAG_DONE; 763 764 return ret; 765 } 766 EXPORT_SYMBOL_GPL(vb2_dqbuf); 767 768 int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type) 769 { 770 if (vb2_fileio_is_active(q)) { 771 dprintk(1, "file io in progress\n"); 772 return -EBUSY; 773 } 774 return vb2_core_streamon(q, type); 775 } 776 EXPORT_SYMBOL_GPL(vb2_streamon); 777 778 int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type) 779 { 780 if (vb2_fileio_is_active(q)) { 781 dprintk(1, "file io in progress\n"); 782 return -EBUSY; 783 } 784 return vb2_core_streamoff(q, type); 785 } 786 EXPORT_SYMBOL_GPL(vb2_streamoff); 787 788 int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb) 789 { 790 return vb2_core_expbuf(q, &eb->fd, eb->type, eb->index, 791 eb->plane, eb->flags); 792 } 793 EXPORT_SYMBOL_GPL(vb2_expbuf); 794 795 int vb2_queue_init(struct vb2_queue *q) 796 { 797 /* 798 * Sanity check 799 */ 800 if (WARN_ON(!q) || 801 WARN_ON(q->timestamp_flags & 802 ~(V4L2_BUF_FLAG_TIMESTAMP_MASK | 803 V4L2_BUF_FLAG_TSTAMP_SRC_MASK))) 804 return -EINVAL; 805 806 /* Warn that the driver should choose an appropriate timestamp type */ 807 WARN_ON((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) == 808 V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN); 809 810 /* Warn that vb2_memory should match with v4l2_memory */ 811 if (WARN_ON(VB2_MEMORY_MMAP != (int)V4L2_MEMORY_MMAP) 812 || WARN_ON(VB2_MEMORY_USERPTR != (int)V4L2_MEMORY_USERPTR) 813 || WARN_ON(VB2_MEMORY_DMABUF != (int)V4L2_MEMORY_DMABUF)) 814 return -EINVAL; 815 816 if (q->buf_struct_size == 0) 817 q->buf_struct_size = sizeof(struct vb2_v4l2_buffer); 818 819 q->buf_ops = &v4l2_buf_ops; 820 q->is_multiplanar = V4L2_TYPE_IS_MULTIPLANAR(q->type); 821 q->is_output = V4L2_TYPE_IS_OUTPUT(q->type); 822 q->copy_timestamp = (q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) 823 == V4L2_BUF_FLAG_TIMESTAMP_COPY; 824 /* 825 * For compatibility with vb1: if QBUF hasn't been called yet, then 826 * return EPOLLERR as well. This only affects capture queues, output 827 * queues will always initialize waiting_for_buffers to false. 828 */ 829 q->quirk_poll_must_check_waiting_for_buffers = true; 830 831 return vb2_core_queue_init(q); 832 } 833 EXPORT_SYMBOL_GPL(vb2_queue_init); 834 835 void vb2_queue_release(struct vb2_queue *q) 836 { 837 vb2_core_queue_release(q); 838 } 839 EXPORT_SYMBOL_GPL(vb2_queue_release); 840 841 __poll_t vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait) 842 { 843 struct video_device *vfd = video_devdata(file); 844 __poll_t req_events = poll_requested_events(wait); 845 __poll_t res = 0; 846 847 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) { 848 struct v4l2_fh *fh = file->private_data; 849 850 if (v4l2_event_pending(fh)) 851 res = EPOLLPRI; 852 else if (req_events & EPOLLPRI) 853 poll_wait(file, &fh->wait, wait); 854 } 855 856 return res | vb2_core_poll(q, file, wait); 857 } 858 EXPORT_SYMBOL_GPL(vb2_poll); 859 860 /* 861 * The following functions are not part of the vb2 core API, but are helper 862 * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations 863 * and struct vb2_ops. 864 * They contain boilerplate code that most if not all drivers have to do 865 * and so they simplify the driver code. 866 */ 867 868 /* The queue is busy if there is a owner and you are not that owner. */ 869 static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file) 870 { 871 return vdev->queue->owner && vdev->queue->owner != file->private_data; 872 } 873 874 /* vb2 ioctl helpers */ 875 876 int vb2_ioctl_reqbufs(struct file *file, void *priv, 877 struct v4l2_requestbuffers *p) 878 { 879 struct video_device *vdev = video_devdata(file); 880 int res = vb2_verify_memory_type(vdev->queue, p->memory, p->type); 881 882 fill_buf_caps(vdev->queue, &p->capabilities); 883 if (res) 884 return res; 885 if (vb2_queue_is_busy(vdev, file)) 886 return -EBUSY; 887 res = vb2_core_reqbufs(vdev->queue, p->memory, &p->count); 888 /* If count == 0, then the owner has released all buffers and he 889 is no longer owner of the queue. Otherwise we have a new owner. */ 890 if (res == 0) 891 vdev->queue->owner = p->count ? file->private_data : NULL; 892 return res; 893 } 894 EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs); 895 896 int vb2_ioctl_create_bufs(struct file *file, void *priv, 897 struct v4l2_create_buffers *p) 898 { 899 struct video_device *vdev = video_devdata(file); 900 int res = vb2_verify_memory_type(vdev->queue, p->memory, 901 p->format.type); 902 903 p->index = vdev->queue->num_buffers; 904 fill_buf_caps(vdev->queue, &p->capabilities); 905 /* 906 * If count == 0, then just check if memory and type are valid. 907 * Any -EBUSY result from vb2_verify_memory_type can be mapped to 0. 908 */ 909 if (p->count == 0) 910 return res != -EBUSY ? res : 0; 911 if (res) 912 return res; 913 if (vb2_queue_is_busy(vdev, file)) 914 return -EBUSY; 915 916 res = vb2_create_bufs(vdev->queue, p); 917 if (res == 0) 918 vdev->queue->owner = file->private_data; 919 return res; 920 } 921 EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs); 922 923 int vb2_ioctl_prepare_buf(struct file *file, void *priv, 924 struct v4l2_buffer *p) 925 { 926 struct video_device *vdev = video_devdata(file); 927 928 if (vb2_queue_is_busy(vdev, file)) 929 return -EBUSY; 930 return vb2_prepare_buf(vdev->queue, vdev->v4l2_dev->mdev, p); 931 } 932 EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf); 933 934 int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p) 935 { 936 struct video_device *vdev = video_devdata(file); 937 938 /* No need to call vb2_queue_is_busy(), anyone can query buffers. */ 939 return vb2_querybuf(vdev->queue, p); 940 } 941 EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf); 942 943 int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p) 944 { 945 struct video_device *vdev = video_devdata(file); 946 947 if (vb2_queue_is_busy(vdev, file)) 948 return -EBUSY; 949 return vb2_qbuf(vdev->queue, vdev->v4l2_dev->mdev, p); 950 } 951 EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf); 952 953 int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p) 954 { 955 struct video_device *vdev = video_devdata(file); 956 957 if (vb2_queue_is_busy(vdev, file)) 958 return -EBUSY; 959 return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK); 960 } 961 EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf); 962 963 int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i) 964 { 965 struct video_device *vdev = video_devdata(file); 966 967 if (vb2_queue_is_busy(vdev, file)) 968 return -EBUSY; 969 return vb2_streamon(vdev->queue, i); 970 } 971 EXPORT_SYMBOL_GPL(vb2_ioctl_streamon); 972 973 int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i) 974 { 975 struct video_device *vdev = video_devdata(file); 976 977 if (vb2_queue_is_busy(vdev, file)) 978 return -EBUSY; 979 return vb2_streamoff(vdev->queue, i); 980 } 981 EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff); 982 983 int vb2_ioctl_expbuf(struct file *file, void *priv, struct v4l2_exportbuffer *p) 984 { 985 struct video_device *vdev = video_devdata(file); 986 987 if (vb2_queue_is_busy(vdev, file)) 988 return -EBUSY; 989 return vb2_expbuf(vdev->queue, p); 990 } 991 EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf); 992 993 /* v4l2_file_operations helpers */ 994 995 int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma) 996 { 997 struct video_device *vdev = video_devdata(file); 998 999 return vb2_mmap(vdev->queue, vma); 1000 } 1001 EXPORT_SYMBOL_GPL(vb2_fop_mmap); 1002 1003 int _vb2_fop_release(struct file *file, struct mutex *lock) 1004 { 1005 struct video_device *vdev = video_devdata(file); 1006 1007 if (lock) 1008 mutex_lock(lock); 1009 if (file->private_data == vdev->queue->owner) { 1010 vb2_queue_release(vdev->queue); 1011 vdev->queue->owner = NULL; 1012 } 1013 if (lock) 1014 mutex_unlock(lock); 1015 return v4l2_fh_release(file); 1016 } 1017 EXPORT_SYMBOL_GPL(_vb2_fop_release); 1018 1019 int vb2_fop_release(struct file *file) 1020 { 1021 struct video_device *vdev = video_devdata(file); 1022 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock; 1023 1024 return _vb2_fop_release(file, lock); 1025 } 1026 EXPORT_SYMBOL_GPL(vb2_fop_release); 1027 1028 ssize_t vb2_fop_write(struct file *file, const char __user *buf, 1029 size_t count, loff_t *ppos) 1030 { 1031 struct video_device *vdev = video_devdata(file); 1032 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock; 1033 int err = -EBUSY; 1034 1035 if (!(vdev->queue->io_modes & VB2_WRITE)) 1036 return -EINVAL; 1037 if (lock && mutex_lock_interruptible(lock)) 1038 return -ERESTARTSYS; 1039 if (vb2_queue_is_busy(vdev, file)) 1040 goto exit; 1041 err = vb2_write(vdev->queue, buf, count, ppos, 1042 file->f_flags & O_NONBLOCK); 1043 if (vdev->queue->fileio) 1044 vdev->queue->owner = file->private_data; 1045 exit: 1046 if (lock) 1047 mutex_unlock(lock); 1048 return err; 1049 } 1050 EXPORT_SYMBOL_GPL(vb2_fop_write); 1051 1052 ssize_t vb2_fop_read(struct file *file, char __user *buf, 1053 size_t count, loff_t *ppos) 1054 { 1055 struct video_device *vdev = video_devdata(file); 1056 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock; 1057 int err = -EBUSY; 1058 1059 if (!(vdev->queue->io_modes & VB2_READ)) 1060 return -EINVAL; 1061 if (lock && mutex_lock_interruptible(lock)) 1062 return -ERESTARTSYS; 1063 if (vb2_queue_is_busy(vdev, file)) 1064 goto exit; 1065 err = vb2_read(vdev->queue, buf, count, ppos, 1066 file->f_flags & O_NONBLOCK); 1067 if (vdev->queue->fileio) 1068 vdev->queue->owner = file->private_data; 1069 exit: 1070 if (lock) 1071 mutex_unlock(lock); 1072 return err; 1073 } 1074 EXPORT_SYMBOL_GPL(vb2_fop_read); 1075 1076 __poll_t vb2_fop_poll(struct file *file, poll_table *wait) 1077 { 1078 struct video_device *vdev = video_devdata(file); 1079 struct vb2_queue *q = vdev->queue; 1080 struct mutex *lock = q->lock ? q->lock : vdev->lock; 1081 __poll_t res; 1082 void *fileio; 1083 1084 /* 1085 * If this helper doesn't know how to lock, then you shouldn't be using 1086 * it but you should write your own. 1087 */ 1088 WARN_ON(!lock); 1089 1090 if (lock && mutex_lock_interruptible(lock)) 1091 return EPOLLERR; 1092 1093 fileio = q->fileio; 1094 1095 res = vb2_poll(vdev->queue, file, wait); 1096 1097 /* If fileio was started, then we have a new queue owner. */ 1098 if (!fileio && q->fileio) 1099 q->owner = file->private_data; 1100 if (lock) 1101 mutex_unlock(lock); 1102 return res; 1103 } 1104 EXPORT_SYMBOL_GPL(vb2_fop_poll); 1105 1106 #ifndef CONFIG_MMU 1107 unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr, 1108 unsigned long len, unsigned long pgoff, unsigned long flags) 1109 { 1110 struct video_device *vdev = video_devdata(file); 1111 1112 return vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags); 1113 } 1114 EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area); 1115 #endif 1116 1117 /* vb2_ops helpers. Only use if vq->lock is non-NULL. */ 1118 1119 void vb2_ops_wait_prepare(struct vb2_queue *vq) 1120 { 1121 mutex_unlock(vq->lock); 1122 } 1123 EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare); 1124 1125 void vb2_ops_wait_finish(struct vb2_queue *vq) 1126 { 1127 mutex_lock(vq->lock); 1128 } 1129 EXPORT_SYMBOL_GPL(vb2_ops_wait_finish); 1130 1131 /* 1132 * Note that this function is called during validation time and 1133 * thus the req_queue_mutex is held to ensure no request objects 1134 * can be added or deleted while validating. So there is no need 1135 * to protect the objects list. 1136 */ 1137 int vb2_request_validate(struct media_request *req) 1138 { 1139 struct media_request_object *obj; 1140 int ret = 0; 1141 1142 if (!vb2_request_buffer_cnt(req)) 1143 return -ENOENT; 1144 1145 list_for_each_entry(obj, &req->objects, list) { 1146 if (!obj->ops->prepare) 1147 continue; 1148 1149 ret = obj->ops->prepare(obj); 1150 if (ret) 1151 break; 1152 } 1153 1154 if (ret) { 1155 list_for_each_entry_continue_reverse(obj, &req->objects, list) 1156 if (obj->ops->unprepare) 1157 obj->ops->unprepare(obj); 1158 return ret; 1159 } 1160 return 0; 1161 } 1162 EXPORT_SYMBOL_GPL(vb2_request_validate); 1163 1164 void vb2_request_queue(struct media_request *req) 1165 { 1166 struct media_request_object *obj, *obj_safe; 1167 1168 /* 1169 * Queue all objects. Note that buffer objects are at the end of the 1170 * objects list, after all other object types. Once buffer objects 1171 * are queued, the driver might delete them immediately (if the driver 1172 * processes the buffer at once), so we have to use 1173 * list_for_each_entry_safe() to handle the case where the object we 1174 * queue is deleted. 1175 */ 1176 list_for_each_entry_safe(obj, obj_safe, &req->objects, list) 1177 if (obj->ops->queue) 1178 obj->ops->queue(obj); 1179 } 1180 EXPORT_SYMBOL_GPL(vb2_request_queue); 1181 1182 MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2"); 1183 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski"); 1184 MODULE_LICENSE("GPL"); 1185