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