Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1 | /* |
| 2 | * videobuf2-core.c - V4L2 driver helper framework |
| 3 | * |
| 4 | * Copyright (C) 2010 Samsung Electronics |
| 5 | * |
| 6 | * Author: Pawel Osciak <p.osciak@samsung.com> |
| 7 | * Marek Szyprowski <m.szyprowski@samsung.com> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/err.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/mm.h> |
| 18 | #include <linux/poll.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/sched.h> |
| 21 | |
| 22 | #include <media/videobuf2-core.h> |
| 23 | |
| 24 | static int debug; |
| 25 | module_param(debug, int, 0644); |
| 26 | |
| 27 | #define dprintk(level, fmt, arg...) \ |
| 28 | do { \ |
| 29 | if (debug >= level) \ |
| 30 | printk(KERN_DEBUG "vb2: " fmt, ## arg); \ |
| 31 | } while (0) |
| 32 | |
| 33 | #define call_memop(q, plane, op, args...) \ |
| 34 | (((q)->mem_ops->op) ? \ |
| 35 | ((q)->mem_ops->op(args)) : 0) |
| 36 | |
| 37 | #define call_qop(q, op, args...) \ |
| 38 | (((q)->ops->op) ? ((q)->ops->op(args)) : 0) |
| 39 | |
| 40 | /** |
| 41 | * __vb2_buf_mem_alloc() - allocate video memory for the given buffer |
| 42 | */ |
| 43 | static int __vb2_buf_mem_alloc(struct vb2_buffer *vb, |
| 44 | unsigned long *plane_sizes) |
| 45 | { |
| 46 | struct vb2_queue *q = vb->vb2_queue; |
| 47 | void *mem_priv; |
| 48 | int plane; |
| 49 | |
| 50 | /* Allocate memory for all planes in this buffer */ |
| 51 | for (plane = 0; plane < vb->num_planes; ++plane) { |
| 52 | mem_priv = call_memop(q, plane, alloc, q->alloc_ctx[plane], |
| 53 | plane_sizes[plane]); |
| 54 | if (!mem_priv) |
| 55 | goto free; |
| 56 | |
| 57 | /* Associate allocator private data with this plane */ |
| 58 | vb->planes[plane].mem_priv = mem_priv; |
| 59 | vb->v4l2_planes[plane].length = plane_sizes[plane]; |
| 60 | } |
| 61 | |
| 62 | return 0; |
| 63 | free: |
| 64 | /* Free already allocated memory if one of the allocations failed */ |
| 65 | for (; plane > 0; --plane) |
| 66 | call_memop(q, plane, put, vb->planes[plane - 1].mem_priv); |
| 67 | |
| 68 | return -ENOMEM; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * __vb2_buf_mem_free() - free memory of the given buffer |
| 73 | */ |
| 74 | static void __vb2_buf_mem_free(struct vb2_buffer *vb) |
| 75 | { |
| 76 | struct vb2_queue *q = vb->vb2_queue; |
| 77 | unsigned int plane; |
| 78 | |
| 79 | for (plane = 0; plane < vb->num_planes; ++plane) { |
| 80 | call_memop(q, plane, put, vb->planes[plane].mem_priv); |
| 81 | vb->planes[plane].mem_priv = NULL; |
| 82 | dprintk(3, "Freed plane %d of buffer %d\n", |
| 83 | plane, vb->v4l2_buf.index); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * __vb2_buf_userptr_put() - release userspace memory associated with |
| 89 | * a USERPTR buffer |
| 90 | */ |
| 91 | static void __vb2_buf_userptr_put(struct vb2_buffer *vb) |
| 92 | { |
| 93 | struct vb2_queue *q = vb->vb2_queue; |
| 94 | unsigned int plane; |
| 95 | |
| 96 | for (plane = 0; plane < vb->num_planes; ++plane) { |
| 97 | void *mem_priv = vb->planes[plane].mem_priv; |
| 98 | |
| 99 | if (mem_priv) { |
| 100 | call_memop(q, plane, put_userptr, mem_priv); |
| 101 | vb->planes[plane].mem_priv = NULL; |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * __setup_offsets() - setup unique offsets ("cookies") for every plane in |
| 108 | * every buffer on the queue |
| 109 | */ |
| 110 | static void __setup_offsets(struct vb2_queue *q) |
| 111 | { |
| 112 | unsigned int buffer, plane; |
| 113 | struct vb2_buffer *vb; |
| 114 | unsigned long off = 0; |
| 115 | |
| 116 | for (buffer = 0; buffer < q->num_buffers; ++buffer) { |
| 117 | vb = q->bufs[buffer]; |
| 118 | if (!vb) |
| 119 | continue; |
| 120 | |
| 121 | for (plane = 0; plane < vb->num_planes; ++plane) { |
| 122 | vb->v4l2_planes[plane].m.mem_offset = off; |
| 123 | |
| 124 | dprintk(3, "Buffer %d, plane %d offset 0x%08lx\n", |
| 125 | buffer, plane, off); |
| 126 | |
| 127 | off += vb->v4l2_planes[plane].length; |
| 128 | off = PAGE_ALIGN(off); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type) |
| 135 | * video buffer memory for all buffers/planes on the queue and initializes the |
| 136 | * queue |
| 137 | * |
| 138 | * Returns the number of buffers successfully allocated. |
| 139 | */ |
| 140 | static int __vb2_queue_alloc(struct vb2_queue *q, enum v4l2_memory memory, |
| 141 | unsigned int num_buffers, unsigned int num_planes, |
| 142 | unsigned long plane_sizes[]) |
| 143 | { |
| 144 | unsigned int buffer; |
| 145 | struct vb2_buffer *vb; |
| 146 | int ret; |
| 147 | |
| 148 | for (buffer = 0; buffer < num_buffers; ++buffer) { |
| 149 | /* Allocate videobuf buffer structures */ |
| 150 | vb = kzalloc(q->buf_struct_size, GFP_KERNEL); |
| 151 | if (!vb) { |
| 152 | dprintk(1, "Memory alloc for buffer struct failed\n"); |
| 153 | break; |
| 154 | } |
| 155 | |
| 156 | /* Length stores number of planes for multiplanar buffers */ |
| 157 | if (V4L2_TYPE_IS_MULTIPLANAR(q->type)) |
| 158 | vb->v4l2_buf.length = num_planes; |
| 159 | |
| 160 | vb->state = VB2_BUF_STATE_DEQUEUED; |
| 161 | vb->vb2_queue = q; |
| 162 | vb->num_planes = num_planes; |
| 163 | vb->v4l2_buf.index = buffer; |
| 164 | vb->v4l2_buf.type = q->type; |
| 165 | vb->v4l2_buf.memory = memory; |
| 166 | |
| 167 | /* Allocate video buffer memory for the MMAP type */ |
| 168 | if (memory == V4L2_MEMORY_MMAP) { |
| 169 | ret = __vb2_buf_mem_alloc(vb, plane_sizes); |
| 170 | if (ret) { |
| 171 | dprintk(1, "Failed allocating memory for " |
| 172 | "buffer %d\n", buffer); |
| 173 | kfree(vb); |
| 174 | break; |
| 175 | } |
| 176 | /* |
| 177 | * Call the driver-provided buffer initialization |
| 178 | * callback, if given. An error in initialization |
| 179 | * results in queue setup failure. |
| 180 | */ |
| 181 | ret = call_qop(q, buf_init, vb); |
| 182 | if (ret) { |
| 183 | dprintk(1, "Buffer %d %p initialization" |
| 184 | " failed\n", buffer, vb); |
| 185 | __vb2_buf_mem_free(vb); |
| 186 | kfree(vb); |
| 187 | break; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | q->bufs[buffer] = vb; |
| 192 | } |
| 193 | |
| 194 | q->num_buffers = buffer; |
| 195 | |
| 196 | __setup_offsets(q); |
| 197 | |
| 198 | dprintk(1, "Allocated %d buffers, %d plane(s) each\n", |
| 199 | q->num_buffers, num_planes); |
| 200 | |
| 201 | return buffer; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * __vb2_free_mem() - release all video buffer memory for a given queue |
| 206 | */ |
| 207 | static void __vb2_free_mem(struct vb2_queue *q) |
| 208 | { |
| 209 | unsigned int buffer; |
| 210 | struct vb2_buffer *vb; |
| 211 | |
| 212 | for (buffer = 0; buffer < q->num_buffers; ++buffer) { |
| 213 | vb = q->bufs[buffer]; |
| 214 | if (!vb) |
| 215 | continue; |
| 216 | |
| 217 | /* Free MMAP buffers or release USERPTR buffers */ |
| 218 | if (q->memory == V4L2_MEMORY_MMAP) |
| 219 | __vb2_buf_mem_free(vb); |
| 220 | else |
| 221 | __vb2_buf_userptr_put(vb); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * __vb2_queue_free() - free the queue - video memory and related information |
| 227 | * and return the queue to an uninitialized state. Might be called even if the |
| 228 | * queue has already been freed. |
| 229 | */ |
| 230 | static int __vb2_queue_free(struct vb2_queue *q) |
| 231 | { |
| 232 | unsigned int buffer; |
| 233 | |
| 234 | /* Call driver-provided cleanup function for each buffer, if provided */ |
| 235 | if (q->ops->buf_cleanup) { |
| 236 | for (buffer = 0; buffer < q->num_buffers; ++buffer) { |
| 237 | if (NULL == q->bufs[buffer]) |
| 238 | continue; |
| 239 | q->ops->buf_cleanup(q->bufs[buffer]); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | /* Release video buffer memory */ |
| 244 | __vb2_free_mem(q); |
| 245 | |
| 246 | /* Free videobuf buffers */ |
| 247 | for (buffer = 0; buffer < q->num_buffers; ++buffer) { |
| 248 | kfree(q->bufs[buffer]); |
| 249 | q->bufs[buffer] = NULL; |
| 250 | } |
| 251 | |
| 252 | q->num_buffers = 0; |
| 253 | q->memory = 0; |
| 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * __verify_planes_array() - verify that the planes array passed in struct |
| 260 | * v4l2_buffer from userspace can be safely used |
| 261 | */ |
| 262 | static int __verify_planes_array(struct vb2_buffer *vb, struct v4l2_buffer *b) |
| 263 | { |
| 264 | /* Is memory for copying plane information present? */ |
| 265 | if (NULL == b->m.planes) { |
| 266 | dprintk(1, "Multi-planar buffer passed but " |
| 267 | "planes array not provided\n"); |
| 268 | return -EINVAL; |
| 269 | } |
| 270 | |
| 271 | if (b->length < vb->num_planes || b->length > VIDEO_MAX_PLANES) { |
| 272 | dprintk(1, "Incorrect planes array length, " |
| 273 | "expected %d, got %d\n", vb->num_planes, b->length); |
| 274 | return -EINVAL; |
| 275 | } |
| 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be |
| 282 | * returned to userspace |
| 283 | */ |
| 284 | static int __fill_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b) |
| 285 | { |
| 286 | struct vb2_queue *q = vb->vb2_queue; |
| 287 | int ret = 0; |
| 288 | |
| 289 | /* Copy back data such as timestamp, input, etc. */ |
| 290 | memcpy(b, &vb->v4l2_buf, offsetof(struct v4l2_buffer, m)); |
| 291 | b->input = vb->v4l2_buf.input; |
| 292 | b->reserved = vb->v4l2_buf.reserved; |
| 293 | |
| 294 | if (V4L2_TYPE_IS_MULTIPLANAR(q->type)) { |
| 295 | ret = __verify_planes_array(vb, b); |
| 296 | if (ret) |
| 297 | return ret; |
| 298 | |
| 299 | /* |
| 300 | * Fill in plane-related data if userspace provided an array |
| 301 | * for it. The memory and size is verified above. |
| 302 | */ |
| 303 | memcpy(b->m.planes, vb->v4l2_planes, |
| 304 | b->length * sizeof(struct v4l2_plane)); |
| 305 | } else { |
| 306 | /* |
| 307 | * We use length and offset in v4l2_planes array even for |
| 308 | * single-planar buffers, but userspace does not. |
| 309 | */ |
| 310 | b->length = vb->v4l2_planes[0].length; |
| 311 | b->bytesused = vb->v4l2_planes[0].bytesused; |
| 312 | if (q->memory == V4L2_MEMORY_MMAP) |
| 313 | b->m.offset = vb->v4l2_planes[0].m.mem_offset; |
| 314 | else if (q->memory == V4L2_MEMORY_USERPTR) |
| 315 | b->m.userptr = vb->v4l2_planes[0].m.userptr; |
| 316 | } |
| 317 | |
| 318 | b->flags = 0; |
| 319 | |
| 320 | switch (vb->state) { |
| 321 | case VB2_BUF_STATE_QUEUED: |
| 322 | case VB2_BUF_STATE_ACTIVE: |
| 323 | b->flags |= V4L2_BUF_FLAG_QUEUED; |
| 324 | break; |
| 325 | case VB2_BUF_STATE_ERROR: |
| 326 | b->flags |= V4L2_BUF_FLAG_ERROR; |
| 327 | /* fall through */ |
| 328 | case VB2_BUF_STATE_DONE: |
| 329 | b->flags |= V4L2_BUF_FLAG_DONE; |
| 330 | break; |
| 331 | case VB2_BUF_STATE_DEQUEUED: |
| 332 | /* nothing */ |
| 333 | break; |
| 334 | } |
| 335 | |
| 336 | if (vb->num_planes_mapped == vb->num_planes) |
| 337 | b->flags |= V4L2_BUF_FLAG_MAPPED; |
| 338 | |
| 339 | return ret; |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * vb2_querybuf() - query video buffer information |
| 344 | * @q: videobuf queue |
| 345 | * @b: buffer struct passed from userspace to vidioc_querybuf handler |
| 346 | * in driver |
| 347 | * |
| 348 | * Should be called from vidioc_querybuf ioctl handler in driver. |
| 349 | * This function will verify the passed v4l2_buffer structure and fill the |
| 350 | * relevant information for the userspace. |
| 351 | * |
| 352 | * The return values from this function are intended to be directly returned |
| 353 | * from vidioc_querybuf handler in driver. |
| 354 | */ |
| 355 | int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b) |
| 356 | { |
| 357 | struct vb2_buffer *vb; |
| 358 | |
| 359 | if (b->type != q->type) { |
| 360 | dprintk(1, "querybuf: wrong buffer type\n"); |
| 361 | return -EINVAL; |
| 362 | } |
| 363 | |
| 364 | if (b->index >= q->num_buffers) { |
| 365 | dprintk(1, "querybuf: buffer index out of range\n"); |
| 366 | return -EINVAL; |
| 367 | } |
| 368 | vb = q->bufs[b->index]; |
| 369 | |
| 370 | return __fill_v4l2_buffer(vb, b); |
| 371 | } |
| 372 | EXPORT_SYMBOL(vb2_querybuf); |
| 373 | |
| 374 | /** |
| 375 | * __verify_userptr_ops() - verify that all memory operations required for |
| 376 | * USERPTR queue type have been provided |
| 377 | */ |
| 378 | static int __verify_userptr_ops(struct vb2_queue *q) |
| 379 | { |
| 380 | if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr || |
| 381 | !q->mem_ops->put_userptr) |
| 382 | return -EINVAL; |
| 383 | |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * __verify_mmap_ops() - verify that all memory operations required for |
| 389 | * MMAP queue type have been provided |
| 390 | */ |
| 391 | static int __verify_mmap_ops(struct vb2_queue *q) |
| 392 | { |
| 393 | if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc || |
| 394 | !q->mem_ops->put || !q->mem_ops->mmap) |
| 395 | return -EINVAL; |
| 396 | |
| 397 | return 0; |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * __buffers_in_use() - return true if any buffers on the queue are in use and |
| 402 | * the queue cannot be freed (by the means of REQBUFS(0)) call |
| 403 | */ |
| 404 | static bool __buffers_in_use(struct vb2_queue *q) |
| 405 | { |
| 406 | unsigned int buffer, plane; |
| 407 | struct vb2_buffer *vb; |
| 408 | |
| 409 | for (buffer = 0; buffer < q->num_buffers; ++buffer) { |
| 410 | vb = q->bufs[buffer]; |
| 411 | for (plane = 0; plane < vb->num_planes; ++plane) { |
| 412 | /* |
| 413 | * If num_users() has not been provided, call_memop |
| 414 | * will return 0, apparently nobody cares about this |
| 415 | * case anyway. If num_users() returns more than 1, |
| 416 | * we are not the only user of the plane's memory. |
| 417 | */ |
| 418 | if (call_memop(q, plane, num_users, |
| 419 | vb->planes[plane].mem_priv) > 1) |
| 420 | return true; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | return false; |
| 425 | } |
| 426 | |
| 427 | /** |
| 428 | * vb2_reqbufs() - Initiate streaming |
| 429 | * @q: videobuf2 queue |
| 430 | * @req: struct passed from userspace to vidioc_reqbufs handler in driver |
| 431 | * |
| 432 | * Should be called from vidioc_reqbufs ioctl handler of a driver. |
| 433 | * This function: |
| 434 | * 1) verifies streaming parameters passed from the userspace, |
| 435 | * 2) sets up the queue, |
| 436 | * 3) negotiates number of buffers and planes per buffer with the driver |
| 437 | * to be used during streaming, |
| 438 | * 4) allocates internal buffer structures (struct vb2_buffer), according to |
| 439 | * the agreed parameters, |
| 440 | * 5) for MMAP memory type, allocates actual video memory, using the |
| 441 | * memory handling/allocation routines provided during queue initialization |
| 442 | * |
| 443 | * If req->count is 0, all the memory will be freed instead. |
| 444 | * If the queue has been allocated previously (by a previous vb2_reqbufs) call |
| 445 | * and the queue is not busy, memory will be reallocated. |
| 446 | * |
| 447 | * The return values from this function are intended to be directly returned |
| 448 | * from vidioc_reqbufs handler in driver. |
| 449 | */ |
| 450 | int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req) |
| 451 | { |
| 452 | unsigned int num_buffers, num_planes; |
| 453 | unsigned long plane_sizes[VIDEO_MAX_PLANES]; |
| 454 | int ret = 0; |
| 455 | |
Marek Szyprowski | b25748f | 2010-12-06 05:56:55 -0300 | [diff] [blame^] | 456 | if (q->fileio) { |
| 457 | dprintk(1, "reqbufs: file io in progress\n"); |
| 458 | return -EBUSY; |
| 459 | } |
| 460 | |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 461 | if (req->memory != V4L2_MEMORY_MMAP |
| 462 | && req->memory != V4L2_MEMORY_USERPTR) { |
| 463 | dprintk(1, "reqbufs: unsupported memory type\n"); |
| 464 | return -EINVAL; |
| 465 | } |
| 466 | |
| 467 | if (req->type != q->type) { |
| 468 | dprintk(1, "reqbufs: requested type is incorrect\n"); |
| 469 | return -EINVAL; |
| 470 | } |
| 471 | |
| 472 | if (q->streaming) { |
| 473 | dprintk(1, "reqbufs: streaming active\n"); |
| 474 | return -EBUSY; |
| 475 | } |
| 476 | |
| 477 | /* |
| 478 | * Make sure all the required memory ops for given memory type |
| 479 | * are available. |
| 480 | */ |
| 481 | if (req->memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) { |
| 482 | dprintk(1, "reqbufs: MMAP for current setup unsupported\n"); |
| 483 | return -EINVAL; |
| 484 | } |
| 485 | |
| 486 | if (req->memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) { |
| 487 | dprintk(1, "reqbufs: USERPTR for current setup unsupported\n"); |
| 488 | return -EINVAL; |
| 489 | } |
| 490 | |
| 491 | if (req->count == 0 || q->num_buffers != 0) { |
| 492 | /* |
| 493 | * We already have buffers allocated, so first check if they |
| 494 | * are not in use and can be freed. |
| 495 | */ |
| 496 | if (q->memory == V4L2_MEMORY_MMAP && __buffers_in_use(q)) { |
| 497 | dprintk(1, "reqbufs: memory in use, cannot free\n"); |
| 498 | return -EBUSY; |
| 499 | } |
| 500 | |
| 501 | ret = __vb2_queue_free(q); |
| 502 | if (ret != 0) |
| 503 | return ret; |
| 504 | } |
| 505 | |
| 506 | /* |
| 507 | * Make sure the requested values and current defaults are sane. |
| 508 | */ |
| 509 | num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME); |
| 510 | memset(plane_sizes, 0, sizeof(plane_sizes)); |
| 511 | memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx)); |
| 512 | |
| 513 | /* |
| 514 | * Ask the driver how many buffers and planes per buffer it requires. |
| 515 | * Driver also sets the size and allocator context for each plane. |
| 516 | */ |
| 517 | ret = call_qop(q, queue_setup, q, &num_buffers, &num_planes, |
| 518 | plane_sizes, q->alloc_ctx); |
| 519 | if (ret) |
| 520 | return ret; |
| 521 | |
| 522 | /* Finally, allocate buffers and video memory */ |
| 523 | ret = __vb2_queue_alloc(q, req->memory, num_buffers, num_planes, |
| 524 | plane_sizes); |
| 525 | if (ret < 0) { |
| 526 | dprintk(1, "Memory allocation failed with error: %d\n", ret); |
| 527 | return ret; |
| 528 | } |
| 529 | |
| 530 | /* |
| 531 | * Check if driver can handle the allocated number of buffers. |
| 532 | */ |
| 533 | if (ret < num_buffers) { |
| 534 | unsigned int orig_num_buffers; |
| 535 | |
| 536 | orig_num_buffers = num_buffers = ret; |
| 537 | ret = call_qop(q, queue_setup, q, &num_buffers, &num_planes, |
| 538 | plane_sizes, q->alloc_ctx); |
| 539 | if (ret) |
| 540 | goto free_mem; |
| 541 | |
| 542 | if (orig_num_buffers < num_buffers) { |
| 543 | ret = -ENOMEM; |
| 544 | goto free_mem; |
| 545 | } |
| 546 | |
| 547 | /* |
| 548 | * Ok, driver accepted smaller number of buffers. |
| 549 | */ |
| 550 | ret = num_buffers; |
| 551 | } |
| 552 | |
| 553 | q->memory = req->memory; |
| 554 | |
| 555 | /* |
| 556 | * Return the number of successfully allocated buffers |
| 557 | * to the userspace. |
| 558 | */ |
| 559 | req->count = ret; |
| 560 | |
| 561 | return 0; |
| 562 | |
| 563 | free_mem: |
| 564 | __vb2_queue_free(q); |
| 565 | return ret; |
| 566 | } |
| 567 | EXPORT_SYMBOL_GPL(vb2_reqbufs); |
| 568 | |
| 569 | /** |
| 570 | * vb2_plane_vaddr() - Return a kernel virtual address of a given plane |
| 571 | * @vb: vb2_buffer to which the plane in question belongs to |
| 572 | * @plane_no: plane number for which the address is to be returned |
| 573 | * |
| 574 | * This function returns a kernel virtual address of a given plane if |
| 575 | * such a mapping exist, NULL otherwise. |
| 576 | */ |
| 577 | void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no) |
| 578 | { |
| 579 | struct vb2_queue *q = vb->vb2_queue; |
| 580 | |
| 581 | if (plane_no > vb->num_planes) |
| 582 | return NULL; |
| 583 | |
| 584 | return call_memop(q, plane_no, vaddr, vb->planes[plane_no].mem_priv); |
| 585 | |
| 586 | } |
| 587 | EXPORT_SYMBOL_GPL(vb2_plane_vaddr); |
| 588 | |
| 589 | /** |
| 590 | * vb2_plane_cookie() - Return allocator specific cookie for the given plane |
| 591 | * @vb: vb2_buffer to which the plane in question belongs to |
| 592 | * @plane_no: plane number for which the cookie is to be returned |
| 593 | * |
| 594 | * This function returns an allocator specific cookie for a given plane if |
| 595 | * available, NULL otherwise. The allocator should provide some simple static |
| 596 | * inline function, which would convert this cookie to the allocator specific |
| 597 | * type that can be used directly by the driver to access the buffer. This can |
| 598 | * be for example physical address, pointer to scatter list or IOMMU mapping. |
| 599 | */ |
| 600 | void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no) |
| 601 | { |
| 602 | struct vb2_queue *q = vb->vb2_queue; |
| 603 | |
| 604 | if (plane_no > vb->num_planes) |
| 605 | return NULL; |
| 606 | |
| 607 | return call_memop(q, plane_no, cookie, vb->planes[plane_no].mem_priv); |
| 608 | } |
| 609 | EXPORT_SYMBOL_GPL(vb2_plane_cookie); |
| 610 | |
| 611 | /** |
| 612 | * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished |
| 613 | * @vb: vb2_buffer returned from the driver |
| 614 | * @state: either VB2_BUF_STATE_DONE if the operation finished successfully |
| 615 | * or VB2_BUF_STATE_ERROR if the operation finished with an error |
| 616 | * |
| 617 | * This function should be called by the driver after a hardware operation on |
| 618 | * a buffer is finished and the buffer may be returned to userspace. The driver |
| 619 | * cannot use this buffer anymore until it is queued back to it by videobuf |
| 620 | * by the means of buf_queue callback. Only buffers previously queued to the |
| 621 | * driver by buf_queue can be passed to this function. |
| 622 | */ |
| 623 | void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state) |
| 624 | { |
| 625 | struct vb2_queue *q = vb->vb2_queue; |
| 626 | unsigned long flags; |
| 627 | |
| 628 | if (vb->state != VB2_BUF_STATE_ACTIVE) |
| 629 | return; |
| 630 | |
| 631 | if (state != VB2_BUF_STATE_DONE && state != VB2_BUF_STATE_ERROR) |
| 632 | return; |
| 633 | |
| 634 | dprintk(4, "Done processing on buffer %d, state: %d\n", |
| 635 | vb->v4l2_buf.index, vb->state); |
| 636 | |
| 637 | /* Add the buffer to the done buffers list */ |
| 638 | spin_lock_irqsave(&q->done_lock, flags); |
| 639 | vb->state = state; |
| 640 | list_add_tail(&vb->done_entry, &q->done_list); |
| 641 | atomic_dec(&q->queued_count); |
| 642 | spin_unlock_irqrestore(&q->done_lock, flags); |
| 643 | |
| 644 | /* Inform any processes that may be waiting for buffers */ |
| 645 | wake_up(&q->done_wq); |
| 646 | } |
| 647 | EXPORT_SYMBOL_GPL(vb2_buffer_done); |
| 648 | |
| 649 | /** |
| 650 | * __fill_vb2_buffer() - fill a vb2_buffer with information provided in |
| 651 | * a v4l2_buffer by the userspace |
| 652 | */ |
| 653 | static int __fill_vb2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b, |
| 654 | struct v4l2_plane *v4l2_planes) |
| 655 | { |
| 656 | unsigned int plane; |
| 657 | int ret; |
| 658 | |
| 659 | if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) { |
| 660 | /* |
| 661 | * Verify that the userspace gave us a valid array for |
| 662 | * plane information. |
| 663 | */ |
| 664 | ret = __verify_planes_array(vb, b); |
| 665 | if (ret) |
| 666 | return ret; |
| 667 | |
| 668 | /* Fill in driver-provided information for OUTPUT types */ |
| 669 | if (V4L2_TYPE_IS_OUTPUT(b->type)) { |
| 670 | /* |
| 671 | * Will have to go up to b->length when API starts |
| 672 | * accepting variable number of planes. |
| 673 | */ |
| 674 | for (plane = 0; plane < vb->num_planes; ++plane) { |
| 675 | v4l2_planes[plane].bytesused = |
| 676 | b->m.planes[plane].bytesused; |
| 677 | v4l2_planes[plane].data_offset = |
| 678 | b->m.planes[plane].data_offset; |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | if (b->memory == V4L2_MEMORY_USERPTR) { |
| 683 | for (plane = 0; plane < vb->num_planes; ++plane) { |
| 684 | v4l2_planes[plane].m.userptr = |
| 685 | b->m.planes[plane].m.userptr; |
| 686 | v4l2_planes[plane].length = |
| 687 | b->m.planes[plane].length; |
| 688 | } |
| 689 | } |
| 690 | } else { |
| 691 | /* |
| 692 | * Single-planar buffers do not use planes array, |
| 693 | * so fill in relevant v4l2_buffer struct fields instead. |
| 694 | * In videobuf we use our internal V4l2_planes struct for |
| 695 | * single-planar buffers as well, for simplicity. |
| 696 | */ |
| 697 | if (V4L2_TYPE_IS_OUTPUT(b->type)) |
| 698 | v4l2_planes[0].bytesused = b->bytesused; |
| 699 | |
| 700 | if (b->memory == V4L2_MEMORY_USERPTR) { |
| 701 | v4l2_planes[0].m.userptr = b->m.userptr; |
| 702 | v4l2_planes[0].length = b->length; |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | vb->v4l2_buf.field = b->field; |
| 707 | vb->v4l2_buf.timestamp = b->timestamp; |
| 708 | |
| 709 | return 0; |
| 710 | } |
| 711 | |
| 712 | /** |
| 713 | * __qbuf_userptr() - handle qbuf of a USERPTR buffer |
| 714 | */ |
| 715 | static int __qbuf_userptr(struct vb2_buffer *vb, struct v4l2_buffer *b) |
| 716 | { |
| 717 | struct v4l2_plane planes[VIDEO_MAX_PLANES]; |
| 718 | struct vb2_queue *q = vb->vb2_queue; |
| 719 | void *mem_priv; |
| 720 | unsigned int plane; |
| 721 | int ret; |
| 722 | int write = !V4L2_TYPE_IS_OUTPUT(q->type); |
| 723 | |
| 724 | /* Verify and copy relevant information provided by the userspace */ |
| 725 | ret = __fill_vb2_buffer(vb, b, planes); |
| 726 | if (ret) |
| 727 | return ret; |
| 728 | |
| 729 | for (plane = 0; plane < vb->num_planes; ++plane) { |
| 730 | /* Skip the plane if already verified */ |
| 731 | if (vb->v4l2_planes[plane].m.userptr == planes[plane].m.userptr |
| 732 | && vb->v4l2_planes[plane].length == planes[plane].length) |
| 733 | continue; |
| 734 | |
| 735 | dprintk(3, "qbuf: userspace address for plane %d changed, " |
| 736 | "reacquiring memory\n", plane); |
| 737 | |
| 738 | /* Release previously acquired memory if present */ |
| 739 | if (vb->planes[plane].mem_priv) |
| 740 | call_memop(q, plane, put_userptr, |
| 741 | vb->planes[plane].mem_priv); |
| 742 | |
| 743 | vb->planes[plane].mem_priv = NULL; |
| 744 | |
| 745 | /* Acquire each plane's memory */ |
| 746 | if (q->mem_ops->get_userptr) { |
| 747 | mem_priv = q->mem_ops->get_userptr(q->alloc_ctx[plane], |
| 748 | planes[plane].m.userptr, |
| 749 | planes[plane].length, |
| 750 | write); |
| 751 | if (IS_ERR(mem_priv)) { |
| 752 | dprintk(1, "qbuf: failed acquiring userspace " |
| 753 | "memory for plane %d\n", plane); |
| 754 | ret = PTR_ERR(mem_priv); |
| 755 | goto err; |
| 756 | } |
| 757 | vb->planes[plane].mem_priv = mem_priv; |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | /* |
| 762 | * Call driver-specific initialization on the newly acquired buffer, |
| 763 | * if provided. |
| 764 | */ |
| 765 | ret = call_qop(q, buf_init, vb); |
| 766 | if (ret) { |
| 767 | dprintk(1, "qbuf: buffer initialization failed\n"); |
| 768 | goto err; |
| 769 | } |
| 770 | |
| 771 | /* |
| 772 | * Now that everything is in order, copy relevant information |
| 773 | * provided by userspace. |
| 774 | */ |
| 775 | for (plane = 0; plane < vb->num_planes; ++plane) |
| 776 | vb->v4l2_planes[plane] = planes[plane]; |
| 777 | |
| 778 | return 0; |
| 779 | err: |
| 780 | /* In case of errors, release planes that were already acquired */ |
| 781 | for (; plane > 0; --plane) { |
| 782 | call_memop(q, plane, put_userptr, |
| 783 | vb->planes[plane - 1].mem_priv); |
| 784 | vb->planes[plane - 1].mem_priv = NULL; |
| 785 | } |
| 786 | |
| 787 | return ret; |
| 788 | } |
| 789 | |
| 790 | /** |
| 791 | * __qbuf_mmap() - handle qbuf of an MMAP buffer |
| 792 | */ |
| 793 | static int __qbuf_mmap(struct vb2_buffer *vb, struct v4l2_buffer *b) |
| 794 | { |
| 795 | return __fill_vb2_buffer(vb, b, vb->v4l2_planes); |
| 796 | } |
| 797 | |
| 798 | /** |
| 799 | * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing |
| 800 | */ |
| 801 | static void __enqueue_in_driver(struct vb2_buffer *vb) |
| 802 | { |
| 803 | struct vb2_queue *q = vb->vb2_queue; |
| 804 | |
| 805 | vb->state = VB2_BUF_STATE_ACTIVE; |
| 806 | atomic_inc(&q->queued_count); |
| 807 | q->ops->buf_queue(vb); |
| 808 | } |
| 809 | |
| 810 | /** |
| 811 | * vb2_qbuf() - Queue a buffer from userspace |
| 812 | * @q: videobuf2 queue |
| 813 | * @b: buffer structure passed from userspace to vidioc_qbuf handler |
| 814 | * in driver |
| 815 | * |
| 816 | * Should be called from vidioc_qbuf ioctl handler of a driver. |
| 817 | * This function: |
| 818 | * 1) verifies the passed buffer, |
| 819 | * 2) calls buf_prepare callback in the driver (if provided), in which |
| 820 | * driver-specific buffer initialization can be performed, |
| 821 | * 3) if streaming is on, queues the buffer in driver by the means of buf_queue |
| 822 | * callback for processing. |
| 823 | * |
| 824 | * The return values from this function are intended to be directly returned |
| 825 | * from vidioc_qbuf handler in driver. |
| 826 | */ |
| 827 | int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b) |
| 828 | { |
| 829 | struct vb2_buffer *vb; |
| 830 | int ret = 0; |
| 831 | |
Marek Szyprowski | b25748f | 2010-12-06 05:56:55 -0300 | [diff] [blame^] | 832 | if (q->fileio) { |
| 833 | dprintk(1, "qbuf: file io in progress\n"); |
| 834 | return -EBUSY; |
| 835 | } |
| 836 | |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 837 | if (b->type != q->type) { |
| 838 | dprintk(1, "qbuf: invalid buffer type\n"); |
| 839 | return -EINVAL; |
| 840 | } |
| 841 | |
| 842 | if (b->index >= q->num_buffers) { |
| 843 | dprintk(1, "qbuf: buffer index out of range\n"); |
| 844 | return -EINVAL; |
| 845 | } |
| 846 | |
| 847 | vb = q->bufs[b->index]; |
| 848 | if (NULL == vb) { |
| 849 | /* Should never happen */ |
| 850 | dprintk(1, "qbuf: buffer is NULL\n"); |
| 851 | return -EINVAL; |
| 852 | } |
| 853 | |
| 854 | if (b->memory != q->memory) { |
| 855 | dprintk(1, "qbuf: invalid memory type\n"); |
| 856 | return -EINVAL; |
| 857 | } |
| 858 | |
| 859 | if (vb->state != VB2_BUF_STATE_DEQUEUED) { |
| 860 | dprintk(1, "qbuf: buffer already in use\n"); |
| 861 | return -EINVAL; |
| 862 | } |
| 863 | |
| 864 | if (q->memory == V4L2_MEMORY_MMAP) |
| 865 | ret = __qbuf_mmap(vb, b); |
| 866 | else if (q->memory == V4L2_MEMORY_USERPTR) |
| 867 | ret = __qbuf_userptr(vb, b); |
| 868 | else { |
| 869 | WARN(1, "Invalid queue type\n"); |
| 870 | return -EINVAL; |
| 871 | } |
| 872 | |
| 873 | if (ret) |
| 874 | return ret; |
| 875 | |
| 876 | ret = call_qop(q, buf_prepare, vb); |
| 877 | if (ret) { |
| 878 | dprintk(1, "qbuf: buffer preparation failed\n"); |
| 879 | return ret; |
| 880 | } |
| 881 | |
| 882 | /* |
| 883 | * Add to the queued buffers list, a buffer will stay on it until |
| 884 | * dequeued in dqbuf. |
| 885 | */ |
| 886 | list_add_tail(&vb->queued_entry, &q->queued_list); |
| 887 | vb->state = VB2_BUF_STATE_QUEUED; |
| 888 | |
| 889 | /* |
| 890 | * If already streaming, give the buffer to driver for processing. |
| 891 | * If not, the buffer will be given to driver on next streamon. |
| 892 | */ |
| 893 | if (q->streaming) |
| 894 | __enqueue_in_driver(vb); |
| 895 | |
| 896 | dprintk(1, "qbuf of buffer %d succeeded\n", vb->v4l2_buf.index); |
| 897 | return 0; |
| 898 | } |
| 899 | EXPORT_SYMBOL_GPL(vb2_qbuf); |
| 900 | |
| 901 | /** |
| 902 | * __vb2_wait_for_done_vb() - wait for a buffer to become available |
| 903 | * for dequeuing |
| 904 | * |
| 905 | * Will sleep if required for nonblocking == false. |
| 906 | */ |
| 907 | static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking) |
| 908 | { |
| 909 | /* |
| 910 | * All operations on vb_done_list are performed under done_lock |
| 911 | * spinlock protection. However, buffers may be removed from |
| 912 | * it and returned to userspace only while holding both driver's |
| 913 | * lock and the done_lock spinlock. Thus we can be sure that as |
| 914 | * long as we hold the driver's lock, the list will remain not |
| 915 | * empty if list_empty() check succeeds. |
| 916 | */ |
| 917 | |
| 918 | for (;;) { |
| 919 | int ret; |
| 920 | |
| 921 | if (!q->streaming) { |
| 922 | dprintk(1, "Streaming off, will not wait for buffers\n"); |
| 923 | return -EINVAL; |
| 924 | } |
| 925 | |
| 926 | if (!list_empty(&q->done_list)) { |
| 927 | /* |
| 928 | * Found a buffer that we were waiting for. |
| 929 | */ |
| 930 | break; |
| 931 | } |
| 932 | |
| 933 | if (nonblocking) { |
| 934 | dprintk(1, "Nonblocking and no buffers to dequeue, " |
| 935 | "will not wait\n"); |
| 936 | return -EAGAIN; |
| 937 | } |
| 938 | |
| 939 | /* |
| 940 | * We are streaming and blocking, wait for another buffer to |
| 941 | * become ready or for streamoff. Driver's lock is released to |
| 942 | * allow streamoff or qbuf to be called while waiting. |
| 943 | */ |
| 944 | call_qop(q, wait_prepare, q); |
| 945 | |
| 946 | /* |
| 947 | * All locks have been released, it is safe to sleep now. |
| 948 | */ |
| 949 | dprintk(3, "Will sleep waiting for buffers\n"); |
| 950 | ret = wait_event_interruptible(q->done_wq, |
| 951 | !list_empty(&q->done_list) || !q->streaming); |
| 952 | |
| 953 | /* |
| 954 | * We need to reevaluate both conditions again after reacquiring |
| 955 | * the locks or return an error if one occurred. |
| 956 | */ |
| 957 | call_qop(q, wait_finish, q); |
| 958 | if (ret) |
| 959 | return ret; |
| 960 | } |
| 961 | return 0; |
| 962 | } |
| 963 | |
| 964 | /** |
| 965 | * __vb2_get_done_vb() - get a buffer ready for dequeuing |
| 966 | * |
| 967 | * Will sleep if required for nonblocking == false. |
| 968 | */ |
| 969 | static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb, |
| 970 | int nonblocking) |
| 971 | { |
| 972 | unsigned long flags; |
| 973 | int ret; |
| 974 | |
| 975 | /* |
| 976 | * Wait for at least one buffer to become available on the done_list. |
| 977 | */ |
| 978 | ret = __vb2_wait_for_done_vb(q, nonblocking); |
| 979 | if (ret) |
| 980 | return ret; |
| 981 | |
| 982 | /* |
| 983 | * Driver's lock has been held since we last verified that done_list |
| 984 | * is not empty, so no need for another list_empty(done_list) check. |
| 985 | */ |
| 986 | spin_lock_irqsave(&q->done_lock, flags); |
| 987 | *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry); |
| 988 | list_del(&(*vb)->done_entry); |
| 989 | spin_unlock_irqrestore(&q->done_lock, flags); |
| 990 | |
| 991 | return 0; |
| 992 | } |
| 993 | |
| 994 | /** |
| 995 | * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2 |
| 996 | * @q: videobuf2 queue |
| 997 | * |
| 998 | * This function will wait until all buffers that have been given to the driver |
| 999 | * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call |
| 1000 | * wait_prepare, wait_finish pair. It is intended to be called with all locks |
| 1001 | * taken, for example from stop_streaming() callback. |
| 1002 | */ |
| 1003 | int vb2_wait_for_all_buffers(struct vb2_queue *q) |
| 1004 | { |
| 1005 | if (!q->streaming) { |
| 1006 | dprintk(1, "Streaming off, will not wait for buffers\n"); |
| 1007 | return -EINVAL; |
| 1008 | } |
| 1009 | |
| 1010 | wait_event(q->done_wq, !atomic_read(&q->queued_count)); |
| 1011 | return 0; |
| 1012 | } |
| 1013 | EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers); |
| 1014 | |
| 1015 | /** |
| 1016 | * vb2_dqbuf() - Dequeue a buffer to the userspace |
| 1017 | * @q: videobuf2 queue |
| 1018 | * @b: buffer structure passed from userspace to vidioc_dqbuf handler |
| 1019 | * in driver |
| 1020 | * @nonblocking: if true, this call will not sleep waiting for a buffer if no |
| 1021 | * buffers ready for dequeuing are present. Normally the driver |
| 1022 | * would be passing (file->f_flags & O_NONBLOCK) here |
| 1023 | * |
| 1024 | * Should be called from vidioc_dqbuf ioctl handler of a driver. |
| 1025 | * This function: |
| 1026 | * 1) verifies the passed buffer, |
| 1027 | * 2) calls buf_finish callback in the driver (if provided), in which |
| 1028 | * driver can perform any additional operations that may be required before |
| 1029 | * returning the buffer to userspace, such as cache sync, |
| 1030 | * 3) the buffer struct members are filled with relevant information for |
| 1031 | * the userspace. |
| 1032 | * |
| 1033 | * The return values from this function are intended to be directly returned |
| 1034 | * from vidioc_dqbuf handler in driver. |
| 1035 | */ |
| 1036 | int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking) |
| 1037 | { |
| 1038 | struct vb2_buffer *vb = NULL; |
| 1039 | int ret; |
| 1040 | |
Marek Szyprowski | b25748f | 2010-12-06 05:56:55 -0300 | [diff] [blame^] | 1041 | if (q->fileio) { |
| 1042 | dprintk(1, "dqbuf: file io in progress\n"); |
| 1043 | return -EBUSY; |
| 1044 | } |
| 1045 | |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1046 | if (b->type != q->type) { |
| 1047 | dprintk(1, "dqbuf: invalid buffer type\n"); |
| 1048 | return -EINVAL; |
| 1049 | } |
| 1050 | |
| 1051 | ret = __vb2_get_done_vb(q, &vb, nonblocking); |
| 1052 | if (ret < 0) { |
| 1053 | dprintk(1, "dqbuf: error getting next done buffer\n"); |
| 1054 | return ret; |
| 1055 | } |
| 1056 | |
| 1057 | ret = call_qop(q, buf_finish, vb); |
| 1058 | if (ret) { |
| 1059 | dprintk(1, "dqbuf: buffer finish failed\n"); |
| 1060 | return ret; |
| 1061 | } |
| 1062 | |
| 1063 | switch (vb->state) { |
| 1064 | case VB2_BUF_STATE_DONE: |
| 1065 | dprintk(3, "dqbuf: Returning done buffer\n"); |
| 1066 | break; |
| 1067 | case VB2_BUF_STATE_ERROR: |
| 1068 | dprintk(3, "dqbuf: Returning done buffer with errors\n"); |
| 1069 | break; |
| 1070 | default: |
| 1071 | dprintk(1, "dqbuf: Invalid buffer state\n"); |
| 1072 | return -EINVAL; |
| 1073 | } |
| 1074 | |
| 1075 | /* Fill buffer information for the userspace */ |
| 1076 | __fill_v4l2_buffer(vb, b); |
| 1077 | /* Remove from videobuf queue */ |
| 1078 | list_del(&vb->queued_entry); |
| 1079 | |
| 1080 | dprintk(1, "dqbuf of buffer %d, with state %d\n", |
| 1081 | vb->v4l2_buf.index, vb->state); |
| 1082 | |
| 1083 | vb->state = VB2_BUF_STATE_DEQUEUED; |
| 1084 | return 0; |
| 1085 | } |
| 1086 | EXPORT_SYMBOL_GPL(vb2_dqbuf); |
| 1087 | |
| 1088 | /** |
| 1089 | * vb2_streamon - start streaming |
| 1090 | * @q: videobuf2 queue |
| 1091 | * @type: type argument passed from userspace to vidioc_streamon handler |
| 1092 | * |
| 1093 | * Should be called from vidioc_streamon handler of a driver. |
| 1094 | * This function: |
| 1095 | * 1) verifies current state |
| 1096 | * 2) starts streaming and passes any previously queued buffers to the driver |
| 1097 | * |
| 1098 | * The return values from this function are intended to be directly returned |
| 1099 | * from vidioc_streamon handler in the driver. |
| 1100 | */ |
| 1101 | int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type) |
| 1102 | { |
| 1103 | struct vb2_buffer *vb; |
| 1104 | |
Marek Szyprowski | b25748f | 2010-12-06 05:56:55 -0300 | [diff] [blame^] | 1105 | if (q->fileio) { |
| 1106 | dprintk(1, "streamon: file io in progress\n"); |
| 1107 | return -EBUSY; |
| 1108 | } |
| 1109 | |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1110 | if (type != q->type) { |
| 1111 | dprintk(1, "streamon: invalid stream type\n"); |
| 1112 | return -EINVAL; |
| 1113 | } |
| 1114 | |
| 1115 | if (q->streaming) { |
| 1116 | dprintk(1, "streamon: already streaming\n"); |
| 1117 | return -EBUSY; |
| 1118 | } |
| 1119 | |
| 1120 | /* |
| 1121 | * Cannot start streaming on an OUTPUT device if no buffers have |
| 1122 | * been queued yet. |
| 1123 | */ |
| 1124 | if (V4L2_TYPE_IS_OUTPUT(q->type)) { |
| 1125 | if (list_empty(&q->queued_list)) { |
| 1126 | dprintk(1, "streamon: no output buffers queued\n"); |
| 1127 | return -EINVAL; |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | q->streaming = 1; |
| 1132 | |
| 1133 | /* |
| 1134 | * Let driver notice that streaming state has been enabled. |
| 1135 | */ |
| 1136 | call_qop(q, start_streaming, q); |
| 1137 | |
| 1138 | /* |
| 1139 | * If any buffers were queued before streamon, |
| 1140 | * we can now pass them to driver for processing. |
| 1141 | */ |
| 1142 | list_for_each_entry(vb, &q->queued_list, queued_entry) |
| 1143 | __enqueue_in_driver(vb); |
| 1144 | |
| 1145 | dprintk(3, "Streamon successful\n"); |
| 1146 | return 0; |
| 1147 | } |
| 1148 | EXPORT_SYMBOL_GPL(vb2_streamon); |
| 1149 | |
| 1150 | /** |
| 1151 | * __vb2_queue_cancel() - cancel and stop (pause) streaming |
| 1152 | * |
| 1153 | * Removes all queued buffers from driver's queue and all buffers queued by |
| 1154 | * userspace from videobuf's queue. Returns to state after reqbufs. |
| 1155 | */ |
| 1156 | static void __vb2_queue_cancel(struct vb2_queue *q) |
| 1157 | { |
| 1158 | unsigned int i; |
| 1159 | |
| 1160 | /* |
| 1161 | * Tell driver to stop all transactions and release all queued |
| 1162 | * buffers. |
| 1163 | */ |
| 1164 | if (q->streaming) |
| 1165 | call_qop(q, stop_streaming, q); |
| 1166 | q->streaming = 0; |
| 1167 | |
| 1168 | /* |
| 1169 | * Remove all buffers from videobuf's list... |
| 1170 | */ |
| 1171 | INIT_LIST_HEAD(&q->queued_list); |
| 1172 | /* |
| 1173 | * ...and done list; userspace will not receive any buffers it |
| 1174 | * has not already dequeued before initiating cancel. |
| 1175 | */ |
| 1176 | INIT_LIST_HEAD(&q->done_list); |
| 1177 | wake_up_all(&q->done_wq); |
| 1178 | |
| 1179 | /* |
| 1180 | * Reinitialize all buffers for next use. |
| 1181 | */ |
| 1182 | for (i = 0; i < q->num_buffers; ++i) |
| 1183 | q->bufs[i]->state = VB2_BUF_STATE_DEQUEUED; |
| 1184 | } |
| 1185 | |
| 1186 | /** |
| 1187 | * vb2_streamoff - stop streaming |
| 1188 | * @q: videobuf2 queue |
| 1189 | * @type: type argument passed from userspace to vidioc_streamoff handler |
| 1190 | * |
| 1191 | * Should be called from vidioc_streamoff handler of a driver. |
| 1192 | * This function: |
| 1193 | * 1) verifies current state, |
| 1194 | * 2) stop streaming and dequeues any queued buffers, including those previously |
| 1195 | * passed to the driver (after waiting for the driver to finish). |
| 1196 | * |
| 1197 | * This call can be used for pausing playback. |
| 1198 | * The return values from this function are intended to be directly returned |
| 1199 | * from vidioc_streamoff handler in the driver |
| 1200 | */ |
| 1201 | int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type) |
| 1202 | { |
Marek Szyprowski | b25748f | 2010-12-06 05:56:55 -0300 | [diff] [blame^] | 1203 | if (q->fileio) { |
| 1204 | dprintk(1, "streamoff: file io in progress\n"); |
| 1205 | return -EBUSY; |
| 1206 | } |
| 1207 | |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1208 | if (type != q->type) { |
| 1209 | dprintk(1, "streamoff: invalid stream type\n"); |
| 1210 | return -EINVAL; |
| 1211 | } |
| 1212 | |
| 1213 | if (!q->streaming) { |
| 1214 | dprintk(1, "streamoff: not streaming\n"); |
| 1215 | return -EINVAL; |
| 1216 | } |
| 1217 | |
| 1218 | /* |
| 1219 | * Cancel will pause streaming and remove all buffers from the driver |
| 1220 | * and videobuf, effectively returning control over them to userspace. |
| 1221 | */ |
| 1222 | __vb2_queue_cancel(q); |
| 1223 | |
| 1224 | dprintk(3, "Streamoff successful\n"); |
| 1225 | return 0; |
| 1226 | } |
| 1227 | EXPORT_SYMBOL_GPL(vb2_streamoff); |
| 1228 | |
| 1229 | /** |
| 1230 | * __find_plane_by_offset() - find plane associated with the given offset off |
| 1231 | */ |
| 1232 | static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off, |
| 1233 | unsigned int *_buffer, unsigned int *_plane) |
| 1234 | { |
| 1235 | struct vb2_buffer *vb; |
| 1236 | unsigned int buffer, plane; |
| 1237 | |
| 1238 | /* |
| 1239 | * Go over all buffers and their planes, comparing the given offset |
| 1240 | * with an offset assigned to each plane. If a match is found, |
| 1241 | * return its buffer and plane numbers. |
| 1242 | */ |
| 1243 | for (buffer = 0; buffer < q->num_buffers; ++buffer) { |
| 1244 | vb = q->bufs[buffer]; |
| 1245 | |
| 1246 | for (plane = 0; plane < vb->num_planes; ++plane) { |
| 1247 | if (vb->v4l2_planes[plane].m.mem_offset == off) { |
| 1248 | *_buffer = buffer; |
| 1249 | *_plane = plane; |
| 1250 | return 0; |
| 1251 | } |
| 1252 | } |
| 1253 | } |
| 1254 | |
| 1255 | return -EINVAL; |
| 1256 | } |
| 1257 | |
| 1258 | /** |
| 1259 | * vb2_mmap() - map video buffers into application address space |
| 1260 | * @q: videobuf2 queue |
| 1261 | * @vma: vma passed to the mmap file operation handler in the driver |
| 1262 | * |
| 1263 | * Should be called from mmap file operation handler of a driver. |
| 1264 | * This function maps one plane of one of the available video buffers to |
| 1265 | * userspace. To map whole video memory allocated on reqbufs, this function |
| 1266 | * has to be called once per each plane per each buffer previously allocated. |
| 1267 | * |
| 1268 | * When the userspace application calls mmap, it passes to it an offset returned |
| 1269 | * to it earlier by the means of vidioc_querybuf handler. That offset acts as |
| 1270 | * a "cookie", which is then used to identify the plane to be mapped. |
| 1271 | * This function finds a plane with a matching offset and a mapping is performed |
| 1272 | * by the means of a provided memory operation. |
| 1273 | * |
| 1274 | * The return values from this function are intended to be directly returned |
| 1275 | * from the mmap handler in driver. |
| 1276 | */ |
| 1277 | int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma) |
| 1278 | { |
| 1279 | unsigned long off = vma->vm_pgoff << PAGE_SHIFT; |
| 1280 | struct vb2_plane *vb_plane; |
| 1281 | struct vb2_buffer *vb; |
| 1282 | unsigned int buffer, plane; |
| 1283 | int ret; |
| 1284 | |
| 1285 | if (q->memory != V4L2_MEMORY_MMAP) { |
| 1286 | dprintk(1, "Queue is not currently set up for mmap\n"); |
| 1287 | return -EINVAL; |
| 1288 | } |
| 1289 | |
| 1290 | /* |
| 1291 | * Check memory area access mode. |
| 1292 | */ |
| 1293 | if (!(vma->vm_flags & VM_SHARED)) { |
| 1294 | dprintk(1, "Invalid vma flags, VM_SHARED needed\n"); |
| 1295 | return -EINVAL; |
| 1296 | } |
| 1297 | if (V4L2_TYPE_IS_OUTPUT(q->type)) { |
| 1298 | if (!(vma->vm_flags & VM_WRITE)) { |
| 1299 | dprintk(1, "Invalid vma flags, VM_WRITE needed\n"); |
| 1300 | return -EINVAL; |
| 1301 | } |
| 1302 | } else { |
| 1303 | if (!(vma->vm_flags & VM_READ)) { |
| 1304 | dprintk(1, "Invalid vma flags, VM_READ needed\n"); |
| 1305 | return -EINVAL; |
| 1306 | } |
| 1307 | } |
| 1308 | |
| 1309 | /* |
| 1310 | * Find the plane corresponding to the offset passed by userspace. |
| 1311 | */ |
| 1312 | ret = __find_plane_by_offset(q, off, &buffer, &plane); |
| 1313 | if (ret) |
| 1314 | return ret; |
| 1315 | |
| 1316 | vb = q->bufs[buffer]; |
| 1317 | vb_plane = &vb->planes[plane]; |
| 1318 | |
| 1319 | ret = q->mem_ops->mmap(vb_plane->mem_priv, vma); |
| 1320 | if (ret) |
| 1321 | return ret; |
| 1322 | |
| 1323 | vb_plane->mapped = 1; |
| 1324 | vb->num_planes_mapped++; |
| 1325 | |
| 1326 | dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer, plane); |
| 1327 | return 0; |
| 1328 | } |
| 1329 | EXPORT_SYMBOL_GPL(vb2_mmap); |
| 1330 | |
Marek Szyprowski | b25748f | 2010-12-06 05:56:55 -0300 | [diff] [blame^] | 1331 | static int __vb2_init_fileio(struct vb2_queue *q, int read); |
| 1332 | static int __vb2_cleanup_fileio(struct vb2_queue *q); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1333 | |
| 1334 | /** |
| 1335 | * vb2_poll() - implements poll userspace operation |
| 1336 | * @q: videobuf2 queue |
| 1337 | * @file: file argument passed to the poll file operation handler |
| 1338 | * @wait: wait argument passed to the poll file operation handler |
| 1339 | * |
| 1340 | * This function implements poll file operation handler for a driver. |
| 1341 | * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will |
| 1342 | * be informed that the file descriptor of a video device is available for |
| 1343 | * reading. |
| 1344 | * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor |
| 1345 | * will be reported as available for writing. |
| 1346 | * |
| 1347 | * The return values from this function are intended to be directly returned |
| 1348 | * from poll handler in driver. |
| 1349 | */ |
| 1350 | unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait) |
| 1351 | { |
| 1352 | unsigned long flags; |
Marek Szyprowski | b25748f | 2010-12-06 05:56:55 -0300 | [diff] [blame^] | 1353 | unsigned int ret; |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1354 | struct vb2_buffer *vb = NULL; |
| 1355 | |
| 1356 | /* |
Marek Szyprowski | b25748f | 2010-12-06 05:56:55 -0300 | [diff] [blame^] | 1357 | * Start file io emulator if streaming api has not been used yet. |
| 1358 | */ |
| 1359 | if (q->num_buffers == 0 && q->fileio == NULL) { |
| 1360 | if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ)) { |
| 1361 | ret = __vb2_init_fileio(q, 1); |
| 1362 | if (ret) |
| 1363 | return ret; |
| 1364 | } |
| 1365 | if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE)) { |
| 1366 | ret = __vb2_init_fileio(q, 0); |
| 1367 | if (ret) |
| 1368 | return ret; |
| 1369 | /* |
| 1370 | * Write to OUTPUT queue can be done immediately. |
| 1371 | */ |
| 1372 | return POLLOUT | POLLWRNORM; |
| 1373 | } |
| 1374 | } |
| 1375 | |
| 1376 | /* |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1377 | * There is nothing to wait for if no buffers have already been queued. |
| 1378 | */ |
| 1379 | if (list_empty(&q->queued_list)) |
| 1380 | return POLLERR; |
| 1381 | |
| 1382 | poll_wait(file, &q->done_wq, wait); |
| 1383 | |
| 1384 | /* |
| 1385 | * Take first buffer available for dequeuing. |
| 1386 | */ |
| 1387 | spin_lock_irqsave(&q->done_lock, flags); |
| 1388 | if (!list_empty(&q->done_list)) |
| 1389 | vb = list_first_entry(&q->done_list, struct vb2_buffer, |
| 1390 | done_entry); |
| 1391 | spin_unlock_irqrestore(&q->done_lock, flags); |
| 1392 | |
| 1393 | if (vb && (vb->state == VB2_BUF_STATE_DONE |
| 1394 | || vb->state == VB2_BUF_STATE_ERROR)) { |
| 1395 | return (V4L2_TYPE_IS_OUTPUT(q->type)) ? POLLOUT | POLLWRNORM : |
| 1396 | POLLIN | POLLRDNORM; |
| 1397 | } |
| 1398 | return 0; |
| 1399 | } |
| 1400 | EXPORT_SYMBOL_GPL(vb2_poll); |
| 1401 | |
| 1402 | /** |
| 1403 | * vb2_queue_init() - initialize a videobuf2 queue |
| 1404 | * @q: videobuf2 queue; this structure should be allocated in driver |
| 1405 | * |
| 1406 | * The vb2_queue structure should be allocated by the driver. The driver is |
| 1407 | * responsible of clearing it's content and setting initial values for some |
| 1408 | * required entries before calling this function. |
| 1409 | * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer |
| 1410 | * to the struct vb2_queue description in include/media/videobuf2-core.h |
| 1411 | * for more information. |
| 1412 | */ |
| 1413 | int vb2_queue_init(struct vb2_queue *q) |
| 1414 | { |
| 1415 | BUG_ON(!q); |
| 1416 | BUG_ON(!q->ops); |
| 1417 | BUG_ON(!q->mem_ops); |
| 1418 | BUG_ON(!q->type); |
| 1419 | BUG_ON(!q->io_modes); |
| 1420 | |
| 1421 | BUG_ON(!q->ops->queue_setup); |
| 1422 | BUG_ON(!q->ops->buf_queue); |
| 1423 | |
| 1424 | INIT_LIST_HEAD(&q->queued_list); |
| 1425 | INIT_LIST_HEAD(&q->done_list); |
| 1426 | spin_lock_init(&q->done_lock); |
| 1427 | init_waitqueue_head(&q->done_wq); |
| 1428 | |
| 1429 | if (q->buf_struct_size == 0) |
| 1430 | q->buf_struct_size = sizeof(struct vb2_buffer); |
| 1431 | |
| 1432 | return 0; |
| 1433 | } |
| 1434 | EXPORT_SYMBOL_GPL(vb2_queue_init); |
| 1435 | |
| 1436 | /** |
| 1437 | * vb2_queue_release() - stop streaming, release the queue and free memory |
| 1438 | * @q: videobuf2 queue |
| 1439 | * |
| 1440 | * This function stops streaming and performs necessary clean ups, including |
| 1441 | * freeing video buffer memory. The driver is responsible for freeing |
| 1442 | * the vb2_queue structure itself. |
| 1443 | */ |
| 1444 | void vb2_queue_release(struct vb2_queue *q) |
| 1445 | { |
Marek Szyprowski | b25748f | 2010-12-06 05:56:55 -0300 | [diff] [blame^] | 1446 | __vb2_cleanup_fileio(q); |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1447 | __vb2_queue_cancel(q); |
| 1448 | __vb2_queue_free(q); |
| 1449 | } |
| 1450 | EXPORT_SYMBOL_GPL(vb2_queue_release); |
| 1451 | |
Marek Szyprowski | b25748f | 2010-12-06 05:56:55 -0300 | [diff] [blame^] | 1452 | /** |
| 1453 | * struct vb2_fileio_buf - buffer context used by file io emulator |
| 1454 | * |
| 1455 | * vb2 provides a compatibility layer and emulator of file io (read and |
| 1456 | * write) calls on top of streaming API. This structure is used for |
| 1457 | * tracking context related to the buffers. |
| 1458 | */ |
| 1459 | struct vb2_fileio_buf { |
| 1460 | void *vaddr; |
| 1461 | unsigned int size; |
| 1462 | unsigned int pos; |
| 1463 | unsigned int queued:1; |
| 1464 | }; |
| 1465 | |
| 1466 | /** |
| 1467 | * struct vb2_fileio_data - queue context used by file io emulator |
| 1468 | * |
| 1469 | * vb2 provides a compatibility layer and emulator of file io (read and |
| 1470 | * write) calls on top of streaming API. For proper operation it required |
| 1471 | * this structure to save the driver state between each call of the read |
| 1472 | * or write function. |
| 1473 | */ |
| 1474 | struct vb2_fileio_data { |
| 1475 | struct v4l2_requestbuffers req; |
| 1476 | struct v4l2_buffer b; |
| 1477 | struct vb2_fileio_buf bufs[VIDEO_MAX_FRAME]; |
| 1478 | unsigned int index; |
| 1479 | unsigned int q_count; |
| 1480 | unsigned int dq_count; |
| 1481 | unsigned int flags; |
| 1482 | }; |
| 1483 | |
| 1484 | /** |
| 1485 | * __vb2_init_fileio() - initialize file io emulator |
| 1486 | * @q: videobuf2 queue |
| 1487 | * @read: mode selector (1 means read, 0 means write) |
| 1488 | */ |
| 1489 | static int __vb2_init_fileio(struct vb2_queue *q, int read) |
| 1490 | { |
| 1491 | struct vb2_fileio_data *fileio; |
| 1492 | int i, ret; |
| 1493 | unsigned int count = 0; |
| 1494 | |
| 1495 | /* |
| 1496 | * Sanity check |
| 1497 | */ |
| 1498 | if ((read && !(q->io_modes & VB2_READ)) || |
| 1499 | (!read && !(q->io_modes & VB2_WRITE))) |
| 1500 | BUG(); |
| 1501 | |
| 1502 | /* |
| 1503 | * Check if device supports mapping buffers to kernel virtual space. |
| 1504 | */ |
| 1505 | if (!q->mem_ops->vaddr) |
| 1506 | return -EBUSY; |
| 1507 | |
| 1508 | /* |
| 1509 | * Check if streaming api has not been already activated. |
| 1510 | */ |
| 1511 | if (q->streaming || q->num_buffers > 0) |
| 1512 | return -EBUSY; |
| 1513 | |
| 1514 | /* |
| 1515 | * Start with count 1, driver can increase it in queue_setup() |
| 1516 | */ |
| 1517 | count = 1; |
| 1518 | |
| 1519 | dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n", |
| 1520 | (read) ? "read" : "write", count, q->io_flags); |
| 1521 | |
| 1522 | fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL); |
| 1523 | if (fileio == NULL) |
| 1524 | return -ENOMEM; |
| 1525 | |
| 1526 | fileio->flags = q->io_flags; |
| 1527 | |
| 1528 | /* |
| 1529 | * Request buffers and use MMAP type to force driver |
| 1530 | * to allocate buffers by itself. |
| 1531 | */ |
| 1532 | fileio->req.count = count; |
| 1533 | fileio->req.memory = V4L2_MEMORY_MMAP; |
| 1534 | fileio->req.type = q->type; |
| 1535 | ret = vb2_reqbufs(q, &fileio->req); |
| 1536 | if (ret) |
| 1537 | goto err_kfree; |
| 1538 | |
| 1539 | /* |
| 1540 | * Check if plane_count is correct |
| 1541 | * (multiplane buffers are not supported). |
| 1542 | */ |
| 1543 | if (q->bufs[0]->num_planes != 1) { |
| 1544 | fileio->req.count = 0; |
| 1545 | ret = -EBUSY; |
| 1546 | goto err_reqbufs; |
| 1547 | } |
| 1548 | |
| 1549 | /* |
| 1550 | * Get kernel address of each buffer. |
| 1551 | */ |
| 1552 | for (i = 0; i < q->num_buffers; i++) { |
| 1553 | fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0); |
| 1554 | if (fileio->bufs[i].vaddr == NULL) |
| 1555 | goto err_reqbufs; |
| 1556 | fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0); |
| 1557 | } |
| 1558 | |
| 1559 | /* |
| 1560 | * Read mode requires pre queuing of all buffers. |
| 1561 | */ |
| 1562 | if (read) { |
| 1563 | /* |
| 1564 | * Queue all buffers. |
| 1565 | */ |
| 1566 | for (i = 0; i < q->num_buffers; i++) { |
| 1567 | struct v4l2_buffer *b = &fileio->b; |
| 1568 | memset(b, 0, sizeof(*b)); |
| 1569 | b->type = q->type; |
| 1570 | b->memory = q->memory; |
| 1571 | b->index = i; |
| 1572 | ret = vb2_qbuf(q, b); |
| 1573 | if (ret) |
| 1574 | goto err_reqbufs; |
| 1575 | fileio->bufs[i].queued = 1; |
| 1576 | } |
| 1577 | |
| 1578 | /* |
| 1579 | * Start streaming. |
| 1580 | */ |
| 1581 | ret = vb2_streamon(q, q->type); |
| 1582 | if (ret) |
| 1583 | goto err_reqbufs; |
| 1584 | } |
| 1585 | |
| 1586 | q->fileio = fileio; |
| 1587 | |
| 1588 | return ret; |
| 1589 | |
| 1590 | err_reqbufs: |
| 1591 | vb2_reqbufs(q, &fileio->req); |
| 1592 | |
| 1593 | err_kfree: |
| 1594 | kfree(fileio); |
| 1595 | return ret; |
| 1596 | } |
| 1597 | |
| 1598 | /** |
| 1599 | * __vb2_cleanup_fileio() - free resourced used by file io emulator |
| 1600 | * @q: videobuf2 queue |
| 1601 | */ |
| 1602 | static int __vb2_cleanup_fileio(struct vb2_queue *q) |
| 1603 | { |
| 1604 | struct vb2_fileio_data *fileio = q->fileio; |
| 1605 | |
| 1606 | if (fileio) { |
| 1607 | /* |
| 1608 | * Hack fileio context to enable direct calls to vb2 ioctl |
| 1609 | * interface. |
| 1610 | */ |
| 1611 | q->fileio = NULL; |
| 1612 | |
| 1613 | vb2_streamoff(q, q->type); |
| 1614 | fileio->req.count = 0; |
| 1615 | vb2_reqbufs(q, &fileio->req); |
| 1616 | kfree(fileio); |
| 1617 | dprintk(3, "file io emulator closed\n"); |
| 1618 | } |
| 1619 | return 0; |
| 1620 | } |
| 1621 | |
| 1622 | /** |
| 1623 | * __vb2_perform_fileio() - perform a single file io (read or write) operation |
| 1624 | * @q: videobuf2 queue |
| 1625 | * @data: pointed to target userspace buffer |
| 1626 | * @count: number of bytes to read or write |
| 1627 | * @ppos: file handle position tracking pointer |
| 1628 | * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking) |
| 1629 | * @read: access mode selector (1 means read, 0 means write) |
| 1630 | */ |
| 1631 | static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count, |
| 1632 | loff_t *ppos, int nonblock, int read) |
| 1633 | { |
| 1634 | struct vb2_fileio_data *fileio; |
| 1635 | struct vb2_fileio_buf *buf; |
| 1636 | int ret, index; |
| 1637 | |
| 1638 | dprintk(3, "file io: mode %s, offset %ld, count %ld, %sblocking\n", |
| 1639 | read ? "read" : "write", (long)*ppos, count, |
| 1640 | nonblock ? "non" : ""); |
| 1641 | |
| 1642 | if (!data) |
| 1643 | return -EINVAL; |
| 1644 | |
| 1645 | /* |
| 1646 | * Initialize emulator on first call. |
| 1647 | */ |
| 1648 | if (!q->fileio) { |
| 1649 | ret = __vb2_init_fileio(q, read); |
| 1650 | dprintk(3, "file io: vb2_init_fileio result: %d\n", ret); |
| 1651 | if (ret) |
| 1652 | return ret; |
| 1653 | } |
| 1654 | fileio = q->fileio; |
| 1655 | |
| 1656 | /* |
| 1657 | * Hack fileio context to enable direct calls to vb2 ioctl interface. |
| 1658 | * The pointer will be restored before returning from this function. |
| 1659 | */ |
| 1660 | q->fileio = NULL; |
| 1661 | |
| 1662 | index = fileio->index; |
| 1663 | buf = &fileio->bufs[index]; |
| 1664 | |
| 1665 | /* |
| 1666 | * Check if we need to dequeue the buffer. |
| 1667 | */ |
| 1668 | if (buf->queued) { |
| 1669 | struct vb2_buffer *vb; |
| 1670 | |
| 1671 | /* |
| 1672 | * Call vb2_dqbuf to get buffer back. |
| 1673 | */ |
| 1674 | memset(&fileio->b, 0, sizeof(fileio->b)); |
| 1675 | fileio->b.type = q->type; |
| 1676 | fileio->b.memory = q->memory; |
| 1677 | fileio->b.index = index; |
| 1678 | ret = vb2_dqbuf(q, &fileio->b, nonblock); |
| 1679 | dprintk(5, "file io: vb2_dqbuf result: %d\n", ret); |
| 1680 | if (ret) |
| 1681 | goto end; |
| 1682 | fileio->dq_count += 1; |
| 1683 | |
| 1684 | /* |
| 1685 | * Get number of bytes filled by the driver |
| 1686 | */ |
| 1687 | vb = q->bufs[index]; |
| 1688 | buf->size = vb2_get_plane_payload(vb, 0); |
| 1689 | buf->queued = 0; |
| 1690 | } |
| 1691 | |
| 1692 | /* |
| 1693 | * Limit count on last few bytes of the buffer. |
| 1694 | */ |
| 1695 | if (buf->pos + count > buf->size) { |
| 1696 | count = buf->size - buf->pos; |
| 1697 | dprintk(5, "reducing read count: %ld\n", count); |
| 1698 | } |
| 1699 | |
| 1700 | /* |
| 1701 | * Transfer data to userspace. |
| 1702 | */ |
| 1703 | dprintk(3, "file io: copying %ld bytes - buffer %d, offset %u\n", |
| 1704 | count, index, buf->pos); |
| 1705 | if (read) |
| 1706 | ret = copy_to_user(data, buf->vaddr + buf->pos, count); |
| 1707 | else |
| 1708 | ret = copy_from_user(buf->vaddr + buf->pos, data, count); |
| 1709 | if (ret) { |
| 1710 | dprintk(3, "file io: error copying data\n"); |
| 1711 | ret = -EFAULT; |
| 1712 | goto end; |
| 1713 | } |
| 1714 | |
| 1715 | /* |
| 1716 | * Update counters. |
| 1717 | */ |
| 1718 | buf->pos += count; |
| 1719 | *ppos += count; |
| 1720 | |
| 1721 | /* |
| 1722 | * Queue next buffer if required. |
| 1723 | */ |
| 1724 | if (buf->pos == buf->size || |
| 1725 | (!read && (fileio->flags & VB2_FILEIO_WRITE_IMMEDIATELY))) { |
| 1726 | /* |
| 1727 | * Check if this is the last buffer to read. |
| 1728 | */ |
| 1729 | if (read && (fileio->flags & VB2_FILEIO_READ_ONCE) && |
| 1730 | fileio->dq_count == 1) { |
| 1731 | dprintk(3, "file io: read limit reached\n"); |
| 1732 | /* |
| 1733 | * Restore fileio pointer and release the context. |
| 1734 | */ |
| 1735 | q->fileio = fileio; |
| 1736 | return __vb2_cleanup_fileio(q); |
| 1737 | } |
| 1738 | |
| 1739 | /* |
| 1740 | * Call vb2_qbuf and give buffer to the driver. |
| 1741 | */ |
| 1742 | memset(&fileio->b, 0, sizeof(fileio->b)); |
| 1743 | fileio->b.type = q->type; |
| 1744 | fileio->b.memory = q->memory; |
| 1745 | fileio->b.index = index; |
| 1746 | fileio->b.bytesused = buf->pos; |
| 1747 | ret = vb2_qbuf(q, &fileio->b); |
| 1748 | dprintk(5, "file io: vb2_dbuf result: %d\n", ret); |
| 1749 | if (ret) |
| 1750 | goto end; |
| 1751 | |
| 1752 | /* |
| 1753 | * Buffer has been queued, update the status |
| 1754 | */ |
| 1755 | buf->pos = 0; |
| 1756 | buf->queued = 1; |
| 1757 | buf->size = q->bufs[0]->v4l2_planes[0].length; |
| 1758 | fileio->q_count += 1; |
| 1759 | |
| 1760 | /* |
| 1761 | * Switch to the next buffer |
| 1762 | */ |
| 1763 | fileio->index = (index + 1) % q->num_buffers; |
| 1764 | |
| 1765 | /* |
| 1766 | * Start streaming if required. |
| 1767 | */ |
| 1768 | if (!read && !q->streaming) { |
| 1769 | ret = vb2_streamon(q, q->type); |
| 1770 | if (ret) |
| 1771 | goto end; |
| 1772 | } |
| 1773 | } |
| 1774 | |
| 1775 | /* |
| 1776 | * Return proper number of bytes processed. |
| 1777 | */ |
| 1778 | if (ret == 0) |
| 1779 | ret = count; |
| 1780 | end: |
| 1781 | /* |
| 1782 | * Restore the fileio context and block vb2 ioctl interface. |
| 1783 | */ |
| 1784 | q->fileio = fileio; |
| 1785 | return ret; |
| 1786 | } |
| 1787 | |
| 1788 | size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count, |
| 1789 | loff_t *ppos, int nonblocking) |
| 1790 | { |
| 1791 | return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1); |
| 1792 | } |
| 1793 | EXPORT_SYMBOL_GPL(vb2_read); |
| 1794 | |
| 1795 | size_t vb2_write(struct vb2_queue *q, char __user *data, size_t count, |
| 1796 | loff_t *ppos, int nonblocking) |
| 1797 | { |
| 1798 | return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 0); |
| 1799 | } |
| 1800 | EXPORT_SYMBOL_GPL(vb2_write); |
| 1801 | |
Pawel Osciak | e23ccc0 | 2010-10-11 10:56:41 -0300 | [diff] [blame] | 1802 | MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2"); |
| 1803 | MODULE_AUTHOR("Pawel Osciak, Marek Szyprowski"); |
| 1804 | MODULE_LICENSE("GPL"); |