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