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 | ||
95213ceb HV |
22 | #include <media/v4l2-dev.h> |
23 | #include <media/v4l2-fh.h> | |
24 | #include <media/v4l2-event.h> | |
e23ccc0a PO |
25 | #include <media/videobuf2-core.h> |
26 | ||
27 | static int debug; | |
28 | module_param(debug, int, 0644); | |
29 | ||
30 | #define dprintk(level, fmt, arg...) \ | |
31 | do { \ | |
32 | if (debug >= level) \ | |
33 | printk(KERN_DEBUG "vb2: " fmt, ## arg); \ | |
34 | } while (0) | |
35 | ||
5931ffe3 | 36 | #define call_memop(q, op, args...) \ |
e23ccc0a PO |
37 | (((q)->mem_ops->op) ? \ |
38 | ((q)->mem_ops->op(args)) : 0) | |
39 | ||
40 | #define call_qop(q, op, args...) \ | |
41 | (((q)->ops->op) ? ((q)->ops->op(args)) : 0) | |
42 | ||
ea42c8ec | 43 | #define V4L2_BUFFER_STATE_FLAGS (V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | \ |
2d86401c GL |
44 | V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR | \ |
45 | V4L2_BUF_FLAG_PREPARED) | |
ea42c8ec | 46 | |
e23ccc0a PO |
47 | /** |
48 | * __vb2_buf_mem_alloc() - allocate video memory for the given buffer | |
49 | */ | |
c1426bc7 | 50 | static int __vb2_buf_mem_alloc(struct vb2_buffer *vb) |
e23ccc0a PO |
51 | { |
52 | struct vb2_queue *q = vb->vb2_queue; | |
53 | void *mem_priv; | |
54 | int plane; | |
55 | ||
56 | /* Allocate memory for all planes in this buffer */ | |
57 | for (plane = 0; plane < vb->num_planes; ++plane) { | |
5931ffe3 | 58 | mem_priv = call_memop(q, alloc, q->alloc_ctx[plane], |
c1426bc7 | 59 | q->plane_sizes[plane]); |
62a79436 | 60 | if (IS_ERR_OR_NULL(mem_priv)) |
e23ccc0a PO |
61 | goto free; |
62 | ||
63 | /* Associate allocator private data with this plane */ | |
64 | vb->planes[plane].mem_priv = mem_priv; | |
c1426bc7 | 65 | vb->v4l2_planes[plane].length = q->plane_sizes[plane]; |
e23ccc0a PO |
66 | } |
67 | ||
68 | return 0; | |
69 | free: | |
70 | /* Free already allocated memory if one of the allocations failed */ | |
a00d0266 | 71 | for (; plane > 0; --plane) { |
5931ffe3 | 72 | call_memop(q, put, vb->planes[plane - 1].mem_priv); |
a00d0266 MS |
73 | vb->planes[plane - 1].mem_priv = NULL; |
74 | } | |
e23ccc0a PO |
75 | |
76 | return -ENOMEM; | |
77 | } | |
78 | ||
79 | /** | |
80 | * __vb2_buf_mem_free() - free memory of the given buffer | |
81 | */ | |
82 | static void __vb2_buf_mem_free(struct vb2_buffer *vb) | |
83 | { | |
84 | struct vb2_queue *q = vb->vb2_queue; | |
85 | unsigned int plane; | |
86 | ||
87 | for (plane = 0; plane < vb->num_planes; ++plane) { | |
5931ffe3 | 88 | call_memop(q, put, vb->planes[plane].mem_priv); |
e23ccc0a | 89 | vb->planes[plane].mem_priv = NULL; |
a00d0266 MS |
90 | dprintk(3, "Freed plane %d of buffer %d\n", plane, |
91 | vb->v4l2_buf.index); | |
e23ccc0a PO |
92 | } |
93 | } | |
94 | ||
95 | /** | |
96 | * __vb2_buf_userptr_put() - release userspace memory associated with | |
97 | * a USERPTR buffer | |
98 | */ | |
99 | static void __vb2_buf_userptr_put(struct vb2_buffer *vb) | |
100 | { | |
101 | struct vb2_queue *q = vb->vb2_queue; | |
102 | unsigned int plane; | |
103 | ||
104 | for (plane = 0; plane < vb->num_planes; ++plane) { | |
a00d0266 MS |
105 | if (vb->planes[plane].mem_priv) |
106 | call_memop(q, put_userptr, vb->planes[plane].mem_priv); | |
107 | vb->planes[plane].mem_priv = NULL; | |
e23ccc0a PO |
108 | } |
109 | } | |
110 | ||
c5384048 SS |
111 | /** |
112 | * __vb2_plane_dmabuf_put() - release memory associated with | |
113 | * a DMABUF shared plane | |
114 | */ | |
115 | static void __vb2_plane_dmabuf_put(struct vb2_queue *q, struct vb2_plane *p) | |
116 | { | |
117 | if (!p->mem_priv) | |
118 | return; | |
119 | ||
120 | if (p->dbuf_mapped) | |
121 | call_memop(q, unmap_dmabuf, p->mem_priv); | |
122 | ||
123 | call_memop(q, detach_dmabuf, p->mem_priv); | |
124 | dma_buf_put(p->dbuf); | |
125 | memset(p, 0, sizeof(*p)); | |
126 | } | |
127 | ||
128 | /** | |
129 | * __vb2_buf_dmabuf_put() - release memory associated with | |
130 | * a DMABUF shared buffer | |
131 | */ | |
132 | static void __vb2_buf_dmabuf_put(struct vb2_buffer *vb) | |
133 | { | |
134 | struct vb2_queue *q = vb->vb2_queue; | |
135 | unsigned int plane; | |
136 | ||
137 | for (plane = 0; plane < vb->num_planes; ++plane) | |
138 | __vb2_plane_dmabuf_put(q, &vb->planes[plane]); | |
139 | } | |
140 | ||
e23ccc0a PO |
141 | /** |
142 | * __setup_offsets() - setup unique offsets ("cookies") for every plane in | |
143 | * every buffer on the queue | |
144 | */ | |
2d86401c | 145 | static void __setup_offsets(struct vb2_queue *q, unsigned int n) |
e23ccc0a PO |
146 | { |
147 | unsigned int buffer, plane; | |
148 | struct vb2_buffer *vb; | |
2d86401c | 149 | unsigned long off; |
e23ccc0a | 150 | |
2d86401c GL |
151 | if (q->num_buffers) { |
152 | struct v4l2_plane *p; | |
153 | vb = q->bufs[q->num_buffers - 1]; | |
154 | p = &vb->v4l2_planes[vb->num_planes - 1]; | |
155 | off = PAGE_ALIGN(p->m.mem_offset + p->length); | |
156 | } else { | |
157 | off = 0; | |
158 | } | |
159 | ||
160 | for (buffer = q->num_buffers; buffer < q->num_buffers + n; ++buffer) { | |
e23ccc0a PO |
161 | vb = q->bufs[buffer]; |
162 | if (!vb) | |
163 | continue; | |
164 | ||
165 | for (plane = 0; plane < vb->num_planes; ++plane) { | |
4907602f | 166 | vb->v4l2_planes[plane].length = q->plane_sizes[plane]; |
e23ccc0a PO |
167 | vb->v4l2_planes[plane].m.mem_offset = off; |
168 | ||
169 | dprintk(3, "Buffer %d, plane %d offset 0x%08lx\n", | |
170 | buffer, plane, off); | |
171 | ||
172 | off += vb->v4l2_planes[plane].length; | |
173 | off = PAGE_ALIGN(off); | |
174 | } | |
175 | } | |
176 | } | |
177 | ||
178 | /** | |
179 | * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type) | |
180 | * video buffer memory for all buffers/planes on the queue and initializes the | |
181 | * queue | |
182 | * | |
183 | * Returns the number of buffers successfully allocated. | |
184 | */ | |
185 | static int __vb2_queue_alloc(struct vb2_queue *q, enum v4l2_memory memory, | |
c1426bc7 | 186 | unsigned int num_buffers, unsigned int num_planes) |
e23ccc0a PO |
187 | { |
188 | unsigned int buffer; | |
189 | struct vb2_buffer *vb; | |
190 | int ret; | |
191 | ||
192 | for (buffer = 0; buffer < num_buffers; ++buffer) { | |
193 | /* Allocate videobuf buffer structures */ | |
194 | vb = kzalloc(q->buf_struct_size, GFP_KERNEL); | |
195 | if (!vb) { | |
196 | dprintk(1, "Memory alloc for buffer struct failed\n"); | |
197 | break; | |
198 | } | |
199 | ||
200 | /* Length stores number of planes for multiplanar buffers */ | |
201 | if (V4L2_TYPE_IS_MULTIPLANAR(q->type)) | |
202 | vb->v4l2_buf.length = num_planes; | |
203 | ||
204 | vb->state = VB2_BUF_STATE_DEQUEUED; | |
205 | vb->vb2_queue = q; | |
206 | vb->num_planes = num_planes; | |
2d86401c | 207 | vb->v4l2_buf.index = q->num_buffers + buffer; |
e23ccc0a PO |
208 | vb->v4l2_buf.type = q->type; |
209 | vb->v4l2_buf.memory = memory; | |
210 | ||
211 | /* Allocate video buffer memory for the MMAP type */ | |
212 | if (memory == V4L2_MEMORY_MMAP) { | |
c1426bc7 | 213 | ret = __vb2_buf_mem_alloc(vb); |
e23ccc0a PO |
214 | if (ret) { |
215 | dprintk(1, "Failed allocating memory for " | |
216 | "buffer %d\n", buffer); | |
217 | kfree(vb); | |
218 | break; | |
219 | } | |
220 | /* | |
221 | * Call the driver-provided buffer initialization | |
222 | * callback, if given. An error in initialization | |
223 | * results in queue setup failure. | |
224 | */ | |
225 | ret = call_qop(q, buf_init, vb); | |
226 | if (ret) { | |
227 | dprintk(1, "Buffer %d %p initialization" | |
228 | " failed\n", buffer, vb); | |
229 | __vb2_buf_mem_free(vb); | |
230 | kfree(vb); | |
231 | break; | |
232 | } | |
233 | } | |
234 | ||
2d86401c | 235 | q->bufs[q->num_buffers + buffer] = vb; |
e23ccc0a PO |
236 | } |
237 | ||
2d86401c | 238 | __setup_offsets(q, buffer); |
e23ccc0a PO |
239 | |
240 | dprintk(1, "Allocated %d buffers, %d plane(s) each\n", | |
2d86401c | 241 | buffer, num_planes); |
e23ccc0a PO |
242 | |
243 | return buffer; | |
244 | } | |
245 | ||
246 | /** | |
247 | * __vb2_free_mem() - release all video buffer memory for a given queue | |
248 | */ | |
2d86401c | 249 | static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers) |
e23ccc0a PO |
250 | { |
251 | unsigned int buffer; | |
252 | struct vb2_buffer *vb; | |
253 | ||
2d86401c GL |
254 | for (buffer = q->num_buffers - buffers; buffer < q->num_buffers; |
255 | ++buffer) { | |
e23ccc0a PO |
256 | vb = q->bufs[buffer]; |
257 | if (!vb) | |
258 | continue; | |
259 | ||
260 | /* Free MMAP buffers or release USERPTR buffers */ | |
261 | if (q->memory == V4L2_MEMORY_MMAP) | |
262 | __vb2_buf_mem_free(vb); | |
c5384048 SS |
263 | else if (q->memory == V4L2_MEMORY_DMABUF) |
264 | __vb2_buf_dmabuf_put(vb); | |
e23ccc0a PO |
265 | else |
266 | __vb2_buf_userptr_put(vb); | |
267 | } | |
268 | } | |
269 | ||
270 | /** | |
2d86401c GL |
271 | * __vb2_queue_free() - free buffers at the end of the queue - video memory and |
272 | * related information, if no buffers are left return the queue to an | |
273 | * uninitialized state. Might be called even if the queue has already been freed. | |
e23ccc0a | 274 | */ |
2d86401c | 275 | static void __vb2_queue_free(struct vb2_queue *q, unsigned int buffers) |
e23ccc0a PO |
276 | { |
277 | unsigned int buffer; | |
278 | ||
279 | /* Call driver-provided cleanup function for each buffer, if provided */ | |
280 | if (q->ops->buf_cleanup) { | |
2d86401c GL |
281 | for (buffer = q->num_buffers - buffers; buffer < q->num_buffers; |
282 | ++buffer) { | |
e23ccc0a PO |
283 | if (NULL == q->bufs[buffer]) |
284 | continue; | |
285 | q->ops->buf_cleanup(q->bufs[buffer]); | |
286 | } | |
287 | } | |
288 | ||
289 | /* Release video buffer memory */ | |
2d86401c | 290 | __vb2_free_mem(q, buffers); |
e23ccc0a PO |
291 | |
292 | /* Free videobuf buffers */ | |
2d86401c GL |
293 | for (buffer = q->num_buffers - buffers; buffer < q->num_buffers; |
294 | ++buffer) { | |
e23ccc0a PO |
295 | kfree(q->bufs[buffer]); |
296 | q->bufs[buffer] = NULL; | |
297 | } | |
298 | ||
2d86401c GL |
299 | q->num_buffers -= buffers; |
300 | if (!q->num_buffers) | |
301 | q->memory = 0; | |
bd50d999 | 302 | INIT_LIST_HEAD(&q->queued_list); |
e23ccc0a PO |
303 | } |
304 | ||
305 | /** | |
306 | * __verify_planes_array() - verify that the planes array passed in struct | |
307 | * v4l2_buffer from userspace can be safely used | |
308 | */ | |
2d86401c | 309 | static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b) |
e23ccc0a | 310 | { |
32a77260 HV |
311 | if (!V4L2_TYPE_IS_MULTIPLANAR(b->type)) |
312 | return 0; | |
313 | ||
e23ccc0a PO |
314 | /* Is memory for copying plane information present? */ |
315 | if (NULL == b->m.planes) { | |
316 | dprintk(1, "Multi-planar buffer passed but " | |
317 | "planes array not provided\n"); | |
318 | return -EINVAL; | |
319 | } | |
320 | ||
321 | if (b->length < vb->num_planes || b->length > VIDEO_MAX_PLANES) { | |
322 | dprintk(1, "Incorrect planes array length, " | |
323 | "expected %d, got %d\n", vb->num_planes, b->length); | |
324 | return -EINVAL; | |
325 | } | |
326 | ||
327 | return 0; | |
328 | } | |
329 | ||
25a27d91 MS |
330 | /** |
331 | * __buffer_in_use() - return true if the buffer is in use and | |
332 | * the queue cannot be freed (by the means of REQBUFS(0)) call | |
333 | */ | |
334 | static bool __buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb) | |
335 | { | |
336 | unsigned int plane; | |
337 | for (plane = 0; plane < vb->num_planes; ++plane) { | |
2c2dd6ac | 338 | void *mem_priv = vb->planes[plane].mem_priv; |
25a27d91 MS |
339 | /* |
340 | * If num_users() has not been provided, call_memop | |
341 | * will return 0, apparently nobody cares about this | |
342 | * case anyway. If num_users() returns more than 1, | |
343 | * we are not the only user of the plane's memory. | |
344 | */ | |
5931ffe3 | 345 | if (mem_priv && call_memop(q, num_users, mem_priv) > 1) |
25a27d91 MS |
346 | return true; |
347 | } | |
348 | return false; | |
349 | } | |
350 | ||
351 | /** | |
352 | * __buffers_in_use() - return true if any buffers on the queue are in use and | |
353 | * the queue cannot be freed (by the means of REQBUFS(0)) call | |
354 | */ | |
355 | static bool __buffers_in_use(struct vb2_queue *q) | |
356 | { | |
357 | unsigned int buffer; | |
358 | for (buffer = 0; buffer < q->num_buffers; ++buffer) { | |
359 | if (__buffer_in_use(q, q->bufs[buffer])) | |
360 | return true; | |
361 | } | |
362 | return false; | |
363 | } | |
364 | ||
e23ccc0a PO |
365 | /** |
366 | * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be | |
367 | * returned to userspace | |
368 | */ | |
32a77260 | 369 | static void __fill_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b) |
e23ccc0a PO |
370 | { |
371 | struct vb2_queue *q = vb->vb2_queue; | |
e23ccc0a | 372 | |
2b719d7b | 373 | /* Copy back data such as timestamp, flags, etc. */ |
e23ccc0a | 374 | memcpy(b, &vb->v4l2_buf, offsetof(struct v4l2_buffer, m)); |
2b719d7b | 375 | b->reserved2 = vb->v4l2_buf.reserved2; |
e23ccc0a PO |
376 | b->reserved = vb->v4l2_buf.reserved; |
377 | ||
378 | if (V4L2_TYPE_IS_MULTIPLANAR(q->type)) { | |
e23ccc0a PO |
379 | /* |
380 | * Fill in plane-related data if userspace provided an array | |
32a77260 | 381 | * for it. The caller has already verified memory and size. |
e23ccc0a | 382 | */ |
3c0b6061 | 383 | b->length = vb->num_planes; |
e23ccc0a PO |
384 | memcpy(b->m.planes, vb->v4l2_planes, |
385 | b->length * sizeof(struct v4l2_plane)); | |
386 | } else { | |
387 | /* | |
388 | * We use length and offset in v4l2_planes array even for | |
389 | * single-planar buffers, but userspace does not. | |
390 | */ | |
391 | b->length = vb->v4l2_planes[0].length; | |
392 | b->bytesused = vb->v4l2_planes[0].bytesused; | |
393 | if (q->memory == V4L2_MEMORY_MMAP) | |
394 | b->m.offset = vb->v4l2_planes[0].m.mem_offset; | |
395 | else if (q->memory == V4L2_MEMORY_USERPTR) | |
396 | b->m.userptr = vb->v4l2_planes[0].m.userptr; | |
c5384048 SS |
397 | else if (q->memory == V4L2_MEMORY_DMABUF) |
398 | b->m.fd = vb->v4l2_planes[0].m.fd; | |
e23ccc0a PO |
399 | } |
400 | ||
ea42c8ec MS |
401 | /* |
402 | * Clear any buffer state related flags. | |
403 | */ | |
404 | b->flags &= ~V4L2_BUFFER_STATE_FLAGS; | |
e23ccc0a PO |
405 | |
406 | switch (vb->state) { | |
407 | case VB2_BUF_STATE_QUEUED: | |
408 | case VB2_BUF_STATE_ACTIVE: | |
409 | b->flags |= V4L2_BUF_FLAG_QUEUED; | |
410 | break; | |
411 | case VB2_BUF_STATE_ERROR: | |
412 | b->flags |= V4L2_BUF_FLAG_ERROR; | |
413 | /* fall through */ | |
414 | case VB2_BUF_STATE_DONE: | |
415 | b->flags |= V4L2_BUF_FLAG_DONE; | |
416 | break; | |
ebc087d0 | 417 | case VB2_BUF_STATE_PREPARED: |
2d86401c GL |
418 | b->flags |= V4L2_BUF_FLAG_PREPARED; |
419 | break; | |
420 | case VB2_BUF_STATE_DEQUEUED: | |
e23ccc0a PO |
421 | /* nothing */ |
422 | break; | |
423 | } | |
424 | ||
25a27d91 | 425 | if (__buffer_in_use(q, vb)) |
e23ccc0a | 426 | b->flags |= V4L2_BUF_FLAG_MAPPED; |
e23ccc0a PO |
427 | } |
428 | ||
429 | /** | |
430 | * vb2_querybuf() - query video buffer information | |
431 | * @q: videobuf queue | |
432 | * @b: buffer struct passed from userspace to vidioc_querybuf handler | |
433 | * in driver | |
434 | * | |
435 | * Should be called from vidioc_querybuf ioctl handler in driver. | |
436 | * This function will verify the passed v4l2_buffer structure and fill the | |
437 | * relevant information for the userspace. | |
438 | * | |
439 | * The return values from this function are intended to be directly returned | |
440 | * from vidioc_querybuf handler in driver. | |
441 | */ | |
442 | int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b) | |
443 | { | |
444 | struct vb2_buffer *vb; | |
32a77260 | 445 | int ret; |
e23ccc0a PO |
446 | |
447 | if (b->type != q->type) { | |
448 | dprintk(1, "querybuf: wrong buffer type\n"); | |
449 | return -EINVAL; | |
450 | } | |
451 | ||
452 | if (b->index >= q->num_buffers) { | |
453 | dprintk(1, "querybuf: buffer index out of range\n"); | |
454 | return -EINVAL; | |
455 | } | |
456 | vb = q->bufs[b->index]; | |
32a77260 HV |
457 | ret = __verify_planes_array(vb, b); |
458 | if (!ret) | |
459 | __fill_v4l2_buffer(vb, b); | |
460 | return ret; | |
e23ccc0a PO |
461 | } |
462 | EXPORT_SYMBOL(vb2_querybuf); | |
463 | ||
464 | /** | |
465 | * __verify_userptr_ops() - verify that all memory operations required for | |
466 | * USERPTR queue type have been provided | |
467 | */ | |
468 | static int __verify_userptr_ops(struct vb2_queue *q) | |
469 | { | |
470 | if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr || | |
471 | !q->mem_ops->put_userptr) | |
472 | return -EINVAL; | |
473 | ||
474 | return 0; | |
475 | } | |
476 | ||
477 | /** | |
478 | * __verify_mmap_ops() - verify that all memory operations required for | |
479 | * MMAP queue type have been provided | |
480 | */ | |
481 | static int __verify_mmap_ops(struct vb2_queue *q) | |
482 | { | |
483 | if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc || | |
484 | !q->mem_ops->put || !q->mem_ops->mmap) | |
485 | return -EINVAL; | |
486 | ||
487 | return 0; | |
488 | } | |
489 | ||
c5384048 SS |
490 | /** |
491 | * __verify_dmabuf_ops() - verify that all memory operations required for | |
492 | * DMABUF queue type have been provided | |
493 | */ | |
494 | static int __verify_dmabuf_ops(struct vb2_queue *q) | |
495 | { | |
496 | if (!(q->io_modes & VB2_DMABUF) || !q->mem_ops->attach_dmabuf || | |
497 | !q->mem_ops->detach_dmabuf || !q->mem_ops->map_dmabuf || | |
498 | !q->mem_ops->unmap_dmabuf) | |
499 | return -EINVAL; | |
500 | ||
501 | return 0; | |
502 | } | |
503 | ||
e23ccc0a | 504 | /** |
37d9ed94 HV |
505 | * __verify_memory_type() - Check whether the memory type and buffer type |
506 | * passed to a buffer operation are compatible with the queue. | |
507 | */ | |
508 | static int __verify_memory_type(struct vb2_queue *q, | |
509 | enum v4l2_memory memory, enum v4l2_buf_type type) | |
510 | { | |
c5384048 SS |
511 | if (memory != V4L2_MEMORY_MMAP && memory != V4L2_MEMORY_USERPTR && |
512 | memory != V4L2_MEMORY_DMABUF) { | |
37d9ed94 HV |
513 | dprintk(1, "reqbufs: unsupported memory type\n"); |
514 | return -EINVAL; | |
515 | } | |
516 | ||
517 | if (type != q->type) { | |
518 | dprintk(1, "reqbufs: requested type is incorrect\n"); | |
519 | return -EINVAL; | |
520 | } | |
521 | ||
522 | /* | |
523 | * Make sure all the required memory ops for given memory type | |
524 | * are available. | |
525 | */ | |
526 | if (memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) { | |
527 | dprintk(1, "reqbufs: MMAP for current setup unsupported\n"); | |
528 | return -EINVAL; | |
529 | } | |
530 | ||
531 | if (memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) { | |
532 | dprintk(1, "reqbufs: USERPTR for current setup unsupported\n"); | |
533 | return -EINVAL; | |
534 | } | |
535 | ||
c5384048 SS |
536 | if (memory == V4L2_MEMORY_DMABUF && __verify_dmabuf_ops(q)) { |
537 | dprintk(1, "reqbufs: DMABUF for current setup unsupported\n"); | |
538 | return -EINVAL; | |
539 | } | |
540 | ||
37d9ed94 HV |
541 | /* |
542 | * Place the busy tests at the end: -EBUSY can be ignored when | |
543 | * create_bufs is called with count == 0, but count == 0 should still | |
544 | * do the memory and type validation. | |
545 | */ | |
546 | if (q->fileio) { | |
547 | dprintk(1, "reqbufs: file io in progress\n"); | |
548 | return -EBUSY; | |
549 | } | |
550 | return 0; | |
551 | } | |
552 | ||
553 | /** | |
554 | * __reqbufs() - Initiate streaming | |
e23ccc0a PO |
555 | * @q: videobuf2 queue |
556 | * @req: struct passed from userspace to vidioc_reqbufs handler in driver | |
557 | * | |
558 | * Should be called from vidioc_reqbufs ioctl handler of a driver. | |
559 | * This function: | |
560 | * 1) verifies streaming parameters passed from the userspace, | |
561 | * 2) sets up the queue, | |
562 | * 3) negotiates number of buffers and planes per buffer with the driver | |
563 | * to be used during streaming, | |
564 | * 4) allocates internal buffer structures (struct vb2_buffer), according to | |
565 | * the agreed parameters, | |
566 | * 5) for MMAP memory type, allocates actual video memory, using the | |
567 | * memory handling/allocation routines provided during queue initialization | |
568 | * | |
569 | * If req->count is 0, all the memory will be freed instead. | |
570 | * If the queue has been allocated previously (by a previous vb2_reqbufs) call | |
571 | * and the queue is not busy, memory will be reallocated. | |
572 | * | |
573 | * The return values from this function are intended to be directly returned | |
574 | * from vidioc_reqbufs handler in driver. | |
575 | */ | |
37d9ed94 | 576 | static int __reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req) |
e23ccc0a | 577 | { |
2d86401c | 578 | unsigned int num_buffers, allocated_buffers, num_planes = 0; |
37d9ed94 | 579 | int ret; |
e23ccc0a PO |
580 | |
581 | if (q->streaming) { | |
582 | dprintk(1, "reqbufs: streaming active\n"); | |
583 | return -EBUSY; | |
584 | } | |
585 | ||
29e3fbd8 | 586 | if (req->count == 0 || q->num_buffers != 0 || q->memory != req->memory) { |
e23ccc0a PO |
587 | /* |
588 | * We already have buffers allocated, so first check if they | |
589 | * are not in use and can be freed. | |
590 | */ | |
591 | if (q->memory == V4L2_MEMORY_MMAP && __buffers_in_use(q)) { | |
592 | dprintk(1, "reqbufs: memory in use, cannot free\n"); | |
593 | return -EBUSY; | |
594 | } | |
595 | ||
2d86401c | 596 | __vb2_queue_free(q, q->num_buffers); |
29e3fbd8 MS |
597 | |
598 | /* | |
599 | * In case of REQBUFS(0) return immediately without calling | |
600 | * driver's queue_setup() callback and allocating resources. | |
601 | */ | |
602 | if (req->count == 0) | |
603 | return 0; | |
e23ccc0a PO |
604 | } |
605 | ||
606 | /* | |
607 | * Make sure the requested values and current defaults are sane. | |
608 | */ | |
609 | num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME); | |
c1426bc7 | 610 | memset(q->plane_sizes, 0, sizeof(q->plane_sizes)); |
e23ccc0a | 611 | memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx)); |
13b14095 | 612 | q->memory = req->memory; |
e23ccc0a PO |
613 | |
614 | /* | |
615 | * Ask the driver how many buffers and planes per buffer it requires. | |
616 | * Driver also sets the size and allocator context for each plane. | |
617 | */ | |
fc714e70 | 618 | ret = call_qop(q, queue_setup, q, NULL, &num_buffers, &num_planes, |
c1426bc7 | 619 | q->plane_sizes, q->alloc_ctx); |
e23ccc0a PO |
620 | if (ret) |
621 | return ret; | |
622 | ||
623 | /* Finally, allocate buffers and video memory */ | |
c1426bc7 | 624 | ret = __vb2_queue_alloc(q, req->memory, num_buffers, num_planes); |
66072d4f MS |
625 | if (ret == 0) { |
626 | dprintk(1, "Memory allocation failed\n"); | |
627 | return -ENOMEM; | |
e23ccc0a PO |
628 | } |
629 | ||
2d86401c GL |
630 | allocated_buffers = ret; |
631 | ||
e23ccc0a PO |
632 | /* |
633 | * Check if driver can handle the allocated number of buffers. | |
634 | */ | |
2d86401c GL |
635 | if (allocated_buffers < num_buffers) { |
636 | num_buffers = allocated_buffers; | |
e23ccc0a | 637 | |
fc714e70 GL |
638 | ret = call_qop(q, queue_setup, q, NULL, &num_buffers, |
639 | &num_planes, q->plane_sizes, q->alloc_ctx); | |
e23ccc0a | 640 | |
2d86401c | 641 | if (!ret && allocated_buffers < num_buffers) |
e23ccc0a | 642 | ret = -ENOMEM; |
e23ccc0a PO |
643 | |
644 | /* | |
2d86401c GL |
645 | * Either the driver has accepted a smaller number of buffers, |
646 | * or .queue_setup() returned an error | |
e23ccc0a | 647 | */ |
2d86401c GL |
648 | } |
649 | ||
650 | q->num_buffers = allocated_buffers; | |
651 | ||
652 | if (ret < 0) { | |
653 | __vb2_queue_free(q, allocated_buffers); | |
654 | return ret; | |
e23ccc0a PO |
655 | } |
656 | ||
e23ccc0a PO |
657 | /* |
658 | * Return the number of successfully allocated buffers | |
659 | * to the userspace. | |
660 | */ | |
2d86401c | 661 | req->count = allocated_buffers; |
e23ccc0a PO |
662 | |
663 | return 0; | |
e23ccc0a | 664 | } |
37d9ed94 HV |
665 | |
666 | /** | |
667 | * vb2_reqbufs() - Wrapper for __reqbufs() that also verifies the memory and | |
668 | * type values. | |
669 | * @q: videobuf2 queue | |
670 | * @req: struct passed from userspace to vidioc_reqbufs handler in driver | |
671 | */ | |
672 | int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req) | |
673 | { | |
674 | int ret = __verify_memory_type(q, req->memory, req->type); | |
675 | ||
676 | return ret ? ret : __reqbufs(q, req); | |
677 | } | |
e23ccc0a PO |
678 | EXPORT_SYMBOL_GPL(vb2_reqbufs); |
679 | ||
2d86401c | 680 | /** |
37d9ed94 | 681 | * __create_bufs() - Allocate buffers and any required auxiliary structs |
2d86401c GL |
682 | * @q: videobuf2 queue |
683 | * @create: creation parameters, passed from userspace to vidioc_create_bufs | |
684 | * handler in driver | |
685 | * | |
686 | * Should be called from vidioc_create_bufs ioctl handler of a driver. | |
687 | * This function: | |
688 | * 1) verifies parameter sanity | |
689 | * 2) calls the .queue_setup() queue operation | |
690 | * 3) performs any necessary memory allocations | |
691 | * | |
692 | * The return values from this function are intended to be directly returned | |
693 | * from vidioc_create_bufs handler in driver. | |
694 | */ | |
37d9ed94 | 695 | static int __create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create) |
2d86401c GL |
696 | { |
697 | unsigned int num_planes = 0, num_buffers, allocated_buffers; | |
37d9ed94 | 698 | int ret; |
2d86401c GL |
699 | |
700 | if (q->num_buffers == VIDEO_MAX_FRAME) { | |
701 | dprintk(1, "%s(): maximum number of buffers already allocated\n", | |
702 | __func__); | |
703 | return -ENOBUFS; | |
704 | } | |
705 | ||
2d86401c GL |
706 | if (!q->num_buffers) { |
707 | memset(q->plane_sizes, 0, sizeof(q->plane_sizes)); | |
708 | memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx)); | |
709 | q->memory = create->memory; | |
710 | } | |
711 | ||
712 | num_buffers = min(create->count, VIDEO_MAX_FRAME - q->num_buffers); | |
713 | ||
714 | /* | |
715 | * Ask the driver, whether the requested number of buffers, planes per | |
716 | * buffer and their sizes are acceptable | |
717 | */ | |
718 | ret = call_qop(q, queue_setup, q, &create->format, &num_buffers, | |
719 | &num_planes, q->plane_sizes, q->alloc_ctx); | |
720 | if (ret) | |
721 | return ret; | |
722 | ||
723 | /* Finally, allocate buffers and video memory */ | |
724 | ret = __vb2_queue_alloc(q, create->memory, num_buffers, | |
725 | num_planes); | |
f05393d2 HV |
726 | if (ret == 0) { |
727 | dprintk(1, "Memory allocation failed\n"); | |
728 | return -ENOMEM; | |
2d86401c GL |
729 | } |
730 | ||
731 | allocated_buffers = ret; | |
732 | ||
733 | /* | |
734 | * Check if driver can handle the so far allocated number of buffers. | |
735 | */ | |
736 | if (ret < num_buffers) { | |
737 | num_buffers = ret; | |
738 | ||
739 | /* | |
740 | * q->num_buffers contains the total number of buffers, that the | |
741 | * queue driver has set up | |
742 | */ | |
743 | ret = call_qop(q, queue_setup, q, &create->format, &num_buffers, | |
744 | &num_planes, q->plane_sizes, q->alloc_ctx); | |
745 | ||
746 | if (!ret && allocated_buffers < num_buffers) | |
747 | ret = -ENOMEM; | |
748 | ||
749 | /* | |
750 | * Either the driver has accepted a smaller number of buffers, | |
751 | * or .queue_setup() returned an error | |
752 | */ | |
753 | } | |
754 | ||
755 | q->num_buffers += allocated_buffers; | |
756 | ||
757 | if (ret < 0) { | |
758 | __vb2_queue_free(q, allocated_buffers); | |
f05393d2 | 759 | return -ENOMEM; |
2d86401c GL |
760 | } |
761 | ||
762 | /* | |
763 | * Return the number of successfully allocated buffers | |
764 | * to the userspace. | |
765 | */ | |
766 | create->count = allocated_buffers; | |
767 | ||
768 | return 0; | |
769 | } | |
37d9ed94 HV |
770 | |
771 | /** | |
53aa3b19 NT |
772 | * vb2_create_bufs() - Wrapper for __create_bufs() that also verifies the |
773 | * memory and type values. | |
37d9ed94 HV |
774 | * @q: videobuf2 queue |
775 | * @create: creation parameters, passed from userspace to vidioc_create_bufs | |
776 | * handler in driver | |
777 | */ | |
778 | int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create) | |
779 | { | |
780 | int ret = __verify_memory_type(q, create->memory, create->format.type); | |
781 | ||
782 | create->index = q->num_buffers; | |
f05393d2 HV |
783 | if (create->count == 0) |
784 | return ret != -EBUSY ? ret : 0; | |
37d9ed94 HV |
785 | return ret ? ret : __create_bufs(q, create); |
786 | } | |
2d86401c GL |
787 | EXPORT_SYMBOL_GPL(vb2_create_bufs); |
788 | ||
e23ccc0a PO |
789 | /** |
790 | * vb2_plane_vaddr() - Return a kernel virtual address of a given plane | |
791 | * @vb: vb2_buffer to which the plane in question belongs to | |
792 | * @plane_no: plane number for which the address is to be returned | |
793 | * | |
794 | * This function returns a kernel virtual address of a given plane if | |
795 | * such a mapping exist, NULL otherwise. | |
796 | */ | |
797 | void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no) | |
798 | { | |
799 | struct vb2_queue *q = vb->vb2_queue; | |
800 | ||
a00d0266 | 801 | if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv) |
e23ccc0a PO |
802 | return NULL; |
803 | ||
5931ffe3 | 804 | return call_memop(q, vaddr, vb->planes[plane_no].mem_priv); |
e23ccc0a PO |
805 | |
806 | } | |
807 | EXPORT_SYMBOL_GPL(vb2_plane_vaddr); | |
808 | ||
809 | /** | |
810 | * vb2_plane_cookie() - Return allocator specific cookie for the given plane | |
811 | * @vb: vb2_buffer to which the plane in question belongs to | |
812 | * @plane_no: plane number for which the cookie is to be returned | |
813 | * | |
814 | * This function returns an allocator specific cookie for a given plane if | |
815 | * available, NULL otherwise. The allocator should provide some simple static | |
816 | * inline function, which would convert this cookie to the allocator specific | |
817 | * type that can be used directly by the driver to access the buffer. This can | |
818 | * be for example physical address, pointer to scatter list or IOMMU mapping. | |
819 | */ | |
820 | void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no) | |
821 | { | |
822 | struct vb2_queue *q = vb->vb2_queue; | |
823 | ||
a00d0266 | 824 | if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv) |
e23ccc0a PO |
825 | return NULL; |
826 | ||
5931ffe3 | 827 | return call_memop(q, cookie, vb->planes[plane_no].mem_priv); |
e23ccc0a PO |
828 | } |
829 | EXPORT_SYMBOL_GPL(vb2_plane_cookie); | |
830 | ||
831 | /** | |
832 | * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished | |
833 | * @vb: vb2_buffer returned from the driver | |
834 | * @state: either VB2_BUF_STATE_DONE if the operation finished successfully | |
835 | * or VB2_BUF_STATE_ERROR if the operation finished with an error | |
836 | * | |
837 | * This function should be called by the driver after a hardware operation on | |
838 | * a buffer is finished and the buffer may be returned to userspace. The driver | |
839 | * cannot use this buffer anymore until it is queued back to it by videobuf | |
840 | * by the means of buf_queue callback. Only buffers previously queued to the | |
841 | * driver by buf_queue can be passed to this function. | |
842 | */ | |
843 | void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state) | |
844 | { | |
845 | struct vb2_queue *q = vb->vb2_queue; | |
846 | unsigned long flags; | |
3e0c2f20 | 847 | unsigned int plane; |
e23ccc0a PO |
848 | |
849 | if (vb->state != VB2_BUF_STATE_ACTIVE) | |
850 | return; | |
851 | ||
852 | if (state != VB2_BUF_STATE_DONE && state != VB2_BUF_STATE_ERROR) | |
853 | return; | |
854 | ||
855 | dprintk(4, "Done processing on buffer %d, state: %d\n", | |
856 | vb->v4l2_buf.index, vb->state); | |
857 | ||
3e0c2f20 MS |
858 | /* sync buffers */ |
859 | for (plane = 0; plane < vb->num_planes; ++plane) | |
860 | call_memop(q, finish, vb->planes[plane].mem_priv); | |
861 | ||
e23ccc0a PO |
862 | /* Add the buffer to the done buffers list */ |
863 | spin_lock_irqsave(&q->done_lock, flags); | |
864 | vb->state = state; | |
865 | list_add_tail(&vb->done_entry, &q->done_list); | |
866 | atomic_dec(&q->queued_count); | |
867 | spin_unlock_irqrestore(&q->done_lock, flags); | |
868 | ||
869 | /* Inform any processes that may be waiting for buffers */ | |
870 | wake_up(&q->done_wq); | |
871 | } | |
872 | EXPORT_SYMBOL_GPL(vb2_buffer_done); | |
873 | ||
874 | /** | |
32a77260 HV |
875 | * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a |
876 | * v4l2_buffer by the userspace. The caller has already verified that struct | |
877 | * v4l2_buffer has a valid number of planes. | |
e23ccc0a | 878 | */ |
32a77260 | 879 | static void __fill_vb2_buffer(struct vb2_buffer *vb, const struct v4l2_buffer *b, |
e23ccc0a PO |
880 | struct v4l2_plane *v4l2_planes) |
881 | { | |
882 | unsigned int plane; | |
e23ccc0a PO |
883 | |
884 | if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) { | |
e23ccc0a PO |
885 | /* Fill in driver-provided information for OUTPUT types */ |
886 | if (V4L2_TYPE_IS_OUTPUT(b->type)) { | |
887 | /* | |
888 | * Will have to go up to b->length when API starts | |
889 | * accepting variable number of planes. | |
890 | */ | |
891 | for (plane = 0; plane < vb->num_planes; ++plane) { | |
892 | v4l2_planes[plane].bytesused = | |
893 | b->m.planes[plane].bytesused; | |
894 | v4l2_planes[plane].data_offset = | |
895 | b->m.planes[plane].data_offset; | |
896 | } | |
897 | } | |
898 | ||
899 | if (b->memory == V4L2_MEMORY_USERPTR) { | |
900 | for (plane = 0; plane < vb->num_planes; ++plane) { | |
901 | v4l2_planes[plane].m.userptr = | |
902 | b->m.planes[plane].m.userptr; | |
903 | v4l2_planes[plane].length = | |
904 | b->m.planes[plane].length; | |
905 | } | |
906 | } | |
c5384048 SS |
907 | if (b->memory == V4L2_MEMORY_DMABUF) { |
908 | for (plane = 0; plane < vb->num_planes; ++plane) { | |
909 | v4l2_planes[plane].m.fd = | |
910 | b->m.planes[plane].m.fd; | |
911 | v4l2_planes[plane].length = | |
912 | b->m.planes[plane].length; | |
913 | v4l2_planes[plane].data_offset = | |
914 | b->m.planes[plane].data_offset; | |
915 | } | |
916 | } | |
e23ccc0a PO |
917 | } else { |
918 | /* | |
919 | * Single-planar buffers do not use planes array, | |
920 | * so fill in relevant v4l2_buffer struct fields instead. | |
921 | * In videobuf we use our internal V4l2_planes struct for | |
922 | * single-planar buffers as well, for simplicity. | |
923 | */ | |
ac706bf7 | 924 | if (V4L2_TYPE_IS_OUTPUT(b->type)) { |
e23ccc0a | 925 | v4l2_planes[0].bytesused = b->bytesused; |
ac706bf7 LP |
926 | v4l2_planes[0].data_offset = 0; |
927 | } | |
e23ccc0a PO |
928 | |
929 | if (b->memory == V4L2_MEMORY_USERPTR) { | |
930 | v4l2_planes[0].m.userptr = b->m.userptr; | |
931 | v4l2_planes[0].length = b->length; | |
932 | } | |
c5384048 SS |
933 | |
934 | if (b->memory == V4L2_MEMORY_DMABUF) { | |
935 | v4l2_planes[0].m.fd = b->m.fd; | |
936 | v4l2_planes[0].length = b->length; | |
937 | v4l2_planes[0].data_offset = 0; | |
938 | } | |
939 | ||
e23ccc0a PO |
940 | } |
941 | ||
942 | vb->v4l2_buf.field = b->field; | |
943 | vb->v4l2_buf.timestamp = b->timestamp; | |
ea42c8ec | 944 | vb->v4l2_buf.flags = b->flags & ~V4L2_BUFFER_STATE_FLAGS; |
e23ccc0a PO |
945 | } |
946 | ||
947 | /** | |
948 | * __qbuf_userptr() - handle qbuf of a USERPTR buffer | |
949 | */ | |
2d86401c | 950 | static int __qbuf_userptr(struct vb2_buffer *vb, const struct v4l2_buffer *b) |
e23ccc0a PO |
951 | { |
952 | struct v4l2_plane planes[VIDEO_MAX_PLANES]; | |
953 | struct vb2_queue *q = vb->vb2_queue; | |
954 | void *mem_priv; | |
955 | unsigned int plane; | |
956 | int ret; | |
957 | int write = !V4L2_TYPE_IS_OUTPUT(q->type); | |
958 | ||
32a77260 HV |
959 | /* Copy relevant information provided by the userspace */ |
960 | __fill_vb2_buffer(vb, b, planes); | |
e23ccc0a PO |
961 | |
962 | for (plane = 0; plane < vb->num_planes; ++plane) { | |
963 | /* Skip the plane if already verified */ | |
f0b7c7fc MS |
964 | if (vb->v4l2_planes[plane].m.userptr && |
965 | vb->v4l2_planes[plane].m.userptr == planes[plane].m.userptr | |
e23ccc0a PO |
966 | && vb->v4l2_planes[plane].length == planes[plane].length) |
967 | continue; | |
968 | ||
969 | dprintk(3, "qbuf: userspace address for plane %d changed, " | |
970 | "reacquiring memory\n", plane); | |
971 | ||
c1426bc7 MS |
972 | /* Check if the provided plane buffer is large enough */ |
973 | if (planes[plane].length < q->plane_sizes[plane]) { | |
4c2625db | 974 | ret = -EINVAL; |
c1426bc7 MS |
975 | goto err; |
976 | } | |
977 | ||
e23ccc0a PO |
978 | /* Release previously acquired memory if present */ |
979 | if (vb->planes[plane].mem_priv) | |
5931ffe3 | 980 | call_memop(q, put_userptr, vb->planes[plane].mem_priv); |
e23ccc0a PO |
981 | |
982 | vb->planes[plane].mem_priv = NULL; | |
c1426bc7 MS |
983 | vb->v4l2_planes[plane].m.userptr = 0; |
984 | vb->v4l2_planes[plane].length = 0; | |
e23ccc0a PO |
985 | |
986 | /* Acquire each plane's memory */ | |
a00d0266 MS |
987 | mem_priv = call_memop(q, get_userptr, q->alloc_ctx[plane], |
988 | planes[plane].m.userptr, | |
989 | planes[plane].length, write); | |
990 | if (IS_ERR_OR_NULL(mem_priv)) { | |
991 | dprintk(1, "qbuf: failed acquiring userspace " | |
e23ccc0a | 992 | "memory for plane %d\n", plane); |
a00d0266 MS |
993 | ret = mem_priv ? PTR_ERR(mem_priv) : -EINVAL; |
994 | goto err; | |
e23ccc0a | 995 | } |
a00d0266 | 996 | vb->planes[plane].mem_priv = mem_priv; |
e23ccc0a PO |
997 | } |
998 | ||
999 | /* | |
1000 | * Call driver-specific initialization on the newly acquired buffer, | |
1001 | * if provided. | |
1002 | */ | |
1003 | ret = call_qop(q, buf_init, vb); | |
1004 | if (ret) { | |
1005 | dprintk(1, "qbuf: buffer initialization failed\n"); | |
1006 | goto err; | |
1007 | } | |
1008 | ||
1009 | /* | |
1010 | * Now that everything is in order, copy relevant information | |
1011 | * provided by userspace. | |
1012 | */ | |
1013 | for (plane = 0; plane < vb->num_planes; ++plane) | |
1014 | vb->v4l2_planes[plane] = planes[plane]; | |
1015 | ||
1016 | return 0; | |
1017 | err: | |
1018 | /* In case of errors, release planes that were already acquired */ | |
c1426bc7 MS |
1019 | for (plane = 0; plane < vb->num_planes; ++plane) { |
1020 | if (vb->planes[plane].mem_priv) | |
5931ffe3 | 1021 | call_memop(q, put_userptr, vb->planes[plane].mem_priv); |
c1426bc7 MS |
1022 | vb->planes[plane].mem_priv = NULL; |
1023 | vb->v4l2_planes[plane].m.userptr = 0; | |
1024 | vb->v4l2_planes[plane].length = 0; | |
e23ccc0a PO |
1025 | } |
1026 | ||
1027 | return ret; | |
1028 | } | |
1029 | ||
1030 | /** | |
1031 | * __qbuf_mmap() - handle qbuf of an MMAP buffer | |
1032 | */ | |
2d86401c | 1033 | static int __qbuf_mmap(struct vb2_buffer *vb, const struct v4l2_buffer *b) |
e23ccc0a | 1034 | { |
32a77260 HV |
1035 | __fill_vb2_buffer(vb, b, vb->v4l2_planes); |
1036 | return 0; | |
e23ccc0a PO |
1037 | } |
1038 | ||
c5384048 SS |
1039 | /** |
1040 | * __qbuf_dmabuf() - handle qbuf of a DMABUF buffer | |
1041 | */ | |
1042 | static int __qbuf_dmabuf(struct vb2_buffer *vb, const struct v4l2_buffer *b) | |
1043 | { | |
1044 | struct v4l2_plane planes[VIDEO_MAX_PLANES]; | |
1045 | struct vb2_queue *q = vb->vb2_queue; | |
1046 | void *mem_priv; | |
1047 | unsigned int plane; | |
1048 | int ret; | |
1049 | int write = !V4L2_TYPE_IS_OUTPUT(q->type); | |
1050 | ||
1051 | /* Verify and copy relevant information provided by the userspace */ | |
1052 | __fill_vb2_buffer(vb, b, planes); | |
1053 | ||
1054 | for (plane = 0; plane < vb->num_planes; ++plane) { | |
1055 | struct dma_buf *dbuf = dma_buf_get(planes[plane].m.fd); | |
1056 | ||
1057 | if (IS_ERR_OR_NULL(dbuf)) { | |
1058 | dprintk(1, "qbuf: invalid dmabuf fd for plane %d\n", | |
1059 | plane); | |
1060 | ret = -EINVAL; | |
1061 | goto err; | |
1062 | } | |
1063 | ||
1064 | /* use DMABUF size if length is not provided */ | |
1065 | if (planes[plane].length == 0) | |
1066 | planes[plane].length = dbuf->size; | |
1067 | ||
1068 | if (planes[plane].length < planes[plane].data_offset + | |
1069 | q->plane_sizes[plane]) { | |
1070 | ret = -EINVAL; | |
1071 | goto err; | |
1072 | } | |
1073 | ||
1074 | /* Skip the plane if already verified */ | |
1075 | if (dbuf == vb->planes[plane].dbuf && | |
1076 | vb->v4l2_planes[plane].length == planes[plane].length) { | |
1077 | dma_buf_put(dbuf); | |
1078 | continue; | |
1079 | } | |
1080 | ||
1081 | dprintk(1, "qbuf: buffer for plane %d changed\n", plane); | |
1082 | ||
1083 | /* Release previously acquired memory if present */ | |
1084 | __vb2_plane_dmabuf_put(q, &vb->planes[plane]); | |
1085 | memset(&vb->v4l2_planes[plane], 0, sizeof(struct v4l2_plane)); | |
1086 | ||
1087 | /* Acquire each plane's memory */ | |
1088 | mem_priv = call_memop(q, attach_dmabuf, q->alloc_ctx[plane], | |
1089 | dbuf, planes[plane].length, write); | |
1090 | if (IS_ERR(mem_priv)) { | |
1091 | dprintk(1, "qbuf: failed to attach dmabuf\n"); | |
1092 | ret = PTR_ERR(mem_priv); | |
1093 | dma_buf_put(dbuf); | |
1094 | goto err; | |
1095 | } | |
1096 | ||
1097 | vb->planes[plane].dbuf = dbuf; | |
1098 | vb->planes[plane].mem_priv = mem_priv; | |
1099 | } | |
1100 | ||
1101 | /* TODO: This pins the buffer(s) with dma_buf_map_attachment()).. but | |
1102 | * really we want to do this just before the DMA, not while queueing | |
1103 | * the buffer(s).. | |
1104 | */ | |
1105 | for (plane = 0; plane < vb->num_planes; ++plane) { | |
1106 | ret = call_memop(q, map_dmabuf, vb->planes[plane].mem_priv); | |
1107 | if (ret) { | |
1108 | dprintk(1, "qbuf: failed to map dmabuf for plane %d\n", | |
1109 | plane); | |
1110 | goto err; | |
1111 | } | |
1112 | vb->planes[plane].dbuf_mapped = 1; | |
1113 | } | |
1114 | ||
1115 | /* | |
1116 | * Call driver-specific initialization on the newly acquired buffer, | |
1117 | * if provided. | |
1118 | */ | |
1119 | ret = call_qop(q, buf_init, vb); | |
1120 | if (ret) { | |
1121 | dprintk(1, "qbuf: buffer initialization failed\n"); | |
1122 | goto err; | |
1123 | } | |
1124 | ||
1125 | /* | |
1126 | * Now that everything is in order, copy relevant information | |
1127 | * provided by userspace. | |
1128 | */ | |
1129 | for (plane = 0; plane < vb->num_planes; ++plane) | |
1130 | vb->v4l2_planes[plane] = planes[plane]; | |
1131 | ||
1132 | return 0; | |
1133 | err: | |
1134 | /* In case of errors, release planes that were already acquired */ | |
1135 | __vb2_buf_dmabuf_put(vb); | |
1136 | ||
1137 | return ret; | |
1138 | } | |
1139 | ||
e23ccc0a PO |
1140 | /** |
1141 | * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing | |
1142 | */ | |
1143 | static void __enqueue_in_driver(struct vb2_buffer *vb) | |
1144 | { | |
1145 | struct vb2_queue *q = vb->vb2_queue; | |
3e0c2f20 | 1146 | unsigned int plane; |
e23ccc0a PO |
1147 | |
1148 | vb->state = VB2_BUF_STATE_ACTIVE; | |
1149 | atomic_inc(&q->queued_count); | |
3e0c2f20 MS |
1150 | |
1151 | /* sync buffers */ | |
1152 | for (plane = 0; plane < vb->num_planes; ++plane) | |
1153 | call_memop(q, prepare, vb->planes[plane].mem_priv); | |
1154 | ||
e23ccc0a PO |
1155 | q->ops->buf_queue(vb); |
1156 | } | |
1157 | ||
2d86401c | 1158 | static int __buf_prepare(struct vb2_buffer *vb, const struct v4l2_buffer *b) |
ebc087d0 GL |
1159 | { |
1160 | struct vb2_queue *q = vb->vb2_queue; | |
1161 | int ret; | |
1162 | ||
1163 | switch (q->memory) { | |
1164 | case V4L2_MEMORY_MMAP: | |
1165 | ret = __qbuf_mmap(vb, b); | |
1166 | break; | |
1167 | case V4L2_MEMORY_USERPTR: | |
1168 | ret = __qbuf_userptr(vb, b); | |
1169 | break; | |
c5384048 SS |
1170 | case V4L2_MEMORY_DMABUF: |
1171 | ret = __qbuf_dmabuf(vb, b); | |
1172 | break; | |
ebc087d0 GL |
1173 | default: |
1174 | WARN(1, "Invalid queue type\n"); | |
1175 | ret = -EINVAL; | |
1176 | } | |
1177 | ||
1178 | if (!ret) | |
1179 | ret = call_qop(q, buf_prepare, vb); | |
1180 | if (ret) | |
1181 | dprintk(1, "qbuf: buffer preparation failed: %d\n", ret); | |
1182 | else | |
1183 | vb->state = VB2_BUF_STATE_PREPARED; | |
1184 | ||
1185 | return ret; | |
1186 | } | |
1187 | ||
2d86401c GL |
1188 | /** |
1189 | * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel | |
1190 | * @q: videobuf2 queue | |
1191 | * @b: buffer structure passed from userspace to vidioc_prepare_buf | |
1192 | * handler in driver | |
1193 | * | |
1194 | * Should be called from vidioc_prepare_buf ioctl handler of a driver. | |
1195 | * This function: | |
1196 | * 1) verifies the passed buffer, | |
1197 | * 2) calls buf_prepare callback in the driver (if provided), in which | |
1198 | * driver-specific buffer initialization can be performed, | |
1199 | * | |
1200 | * The return values from this function are intended to be directly returned | |
1201 | * from vidioc_prepare_buf handler in driver. | |
1202 | */ | |
1203 | int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b) | |
1204 | { | |
1205 | struct vb2_buffer *vb; | |
1206 | int ret; | |
1207 | ||
1208 | if (q->fileio) { | |
1209 | dprintk(1, "%s(): file io in progress\n", __func__); | |
1210 | return -EBUSY; | |
1211 | } | |
1212 | ||
1213 | if (b->type != q->type) { | |
1214 | dprintk(1, "%s(): invalid buffer type\n", __func__); | |
1215 | return -EINVAL; | |
1216 | } | |
1217 | ||
1218 | if (b->index >= q->num_buffers) { | |
1219 | dprintk(1, "%s(): buffer index out of range\n", __func__); | |
1220 | return -EINVAL; | |
1221 | } | |
1222 | ||
1223 | vb = q->bufs[b->index]; | |
1224 | if (NULL == vb) { | |
1225 | /* Should never happen */ | |
1226 | dprintk(1, "%s(): buffer is NULL\n", __func__); | |
1227 | return -EINVAL; | |
1228 | } | |
1229 | ||
1230 | if (b->memory != q->memory) { | |
1231 | dprintk(1, "%s(): invalid memory type\n", __func__); | |
1232 | return -EINVAL; | |
1233 | } | |
1234 | ||
1235 | if (vb->state != VB2_BUF_STATE_DEQUEUED) { | |
1236 | dprintk(1, "%s(): invalid buffer state %d\n", __func__, vb->state); | |
1237 | return -EINVAL; | |
1238 | } | |
32a77260 HV |
1239 | ret = __verify_planes_array(vb, b); |
1240 | if (ret < 0) | |
1241 | return ret; | |
2d86401c GL |
1242 | ret = __buf_prepare(vb, b); |
1243 | if (ret < 0) | |
1244 | return ret; | |
1245 | ||
1246 | __fill_v4l2_buffer(vb, b); | |
1247 | ||
1248 | return 0; | |
1249 | } | |
1250 | EXPORT_SYMBOL_GPL(vb2_prepare_buf); | |
1251 | ||
e23ccc0a PO |
1252 | /** |
1253 | * vb2_qbuf() - Queue a buffer from userspace | |
1254 | * @q: videobuf2 queue | |
1255 | * @b: buffer structure passed from userspace to vidioc_qbuf handler | |
1256 | * in driver | |
1257 | * | |
1258 | * Should be called from vidioc_qbuf ioctl handler of a driver. | |
1259 | * This function: | |
1260 | * 1) verifies the passed buffer, | |
ebc087d0 GL |
1261 | * 2) if necessary, calls buf_prepare callback in the driver (if provided), in |
1262 | * which driver-specific buffer initialization can be performed, | |
e23ccc0a PO |
1263 | * 3) if streaming is on, queues the buffer in driver by the means of buf_queue |
1264 | * callback for processing. | |
1265 | * | |
1266 | * The return values from this function are intended to be directly returned | |
1267 | * from vidioc_qbuf handler in driver. | |
1268 | */ | |
1269 | int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b) | |
1270 | { | |
b037c0fd | 1271 | struct rw_semaphore *mmap_sem = NULL; |
e23ccc0a | 1272 | struct vb2_buffer *vb; |
b037c0fd MS |
1273 | int ret = 0; |
1274 | ||
1275 | /* | |
1276 | * In case of user pointer buffers vb2 allocator needs to get direct | |
1277 | * access to userspace pages. This requires getting read access on | |
1278 | * mmap semaphore in the current process structure. The same | |
1279 | * semaphore is taken before calling mmap operation, while both mmap | |
1280 | * and qbuf are called by the driver or v4l2 core with driver's lock | |
1281 | * held. To avoid a AB-BA deadlock (mmap_sem then driver's lock in | |
1282 | * mmap and driver's lock then mmap_sem in qbuf) the videobuf2 core | |
1283 | * release driver's lock, takes mmap_sem and then takes again driver's | |
1284 | * lock. | |
1285 | * | |
1286 | * To avoid race with other vb2 calls, which might be called after | |
1287 | * releasing driver's lock, this operation is performed at the | |
1288 | * beggining of qbuf processing. This way the queue status is | |
1289 | * consistent after getting driver's lock back. | |
1290 | */ | |
57e43cfb | 1291 | if (q->memory == V4L2_MEMORY_USERPTR) { |
b037c0fd MS |
1292 | mmap_sem = ¤t->mm->mmap_sem; |
1293 | call_qop(q, wait_prepare, q); | |
1294 | down_read(mmap_sem); | |
1295 | call_qop(q, wait_finish, q); | |
1296 | } | |
e23ccc0a | 1297 | |
b25748fe MS |
1298 | if (q->fileio) { |
1299 | dprintk(1, "qbuf: file io in progress\n"); | |
b037c0fd MS |
1300 | ret = -EBUSY; |
1301 | goto unlock; | |
b25748fe MS |
1302 | } |
1303 | ||
e23ccc0a PO |
1304 | if (b->type != q->type) { |
1305 | dprintk(1, "qbuf: invalid buffer type\n"); | |
b037c0fd MS |
1306 | ret = -EINVAL; |
1307 | goto unlock; | |
e23ccc0a PO |
1308 | } |
1309 | ||
1310 | if (b->index >= q->num_buffers) { | |
1311 | dprintk(1, "qbuf: buffer index out of range\n"); | |
b037c0fd MS |
1312 | ret = -EINVAL; |
1313 | goto unlock; | |
e23ccc0a PO |
1314 | } |
1315 | ||
1316 | vb = q->bufs[b->index]; | |
1317 | if (NULL == vb) { | |
1318 | /* Should never happen */ | |
1319 | dprintk(1, "qbuf: buffer is NULL\n"); | |
b037c0fd MS |
1320 | ret = -EINVAL; |
1321 | goto unlock; | |
e23ccc0a PO |
1322 | } |
1323 | ||
1324 | if (b->memory != q->memory) { | |
1325 | dprintk(1, "qbuf: invalid memory type\n"); | |
b037c0fd MS |
1326 | ret = -EINVAL; |
1327 | goto unlock; | |
e23ccc0a | 1328 | } |
32a77260 HV |
1329 | ret = __verify_planes_array(vb, b); |
1330 | if (ret) | |
1331 | goto unlock; | |
e23ccc0a | 1332 | |
ebc087d0 GL |
1333 | switch (vb->state) { |
1334 | case VB2_BUF_STATE_DEQUEUED: | |
1335 | ret = __buf_prepare(vb, b); | |
1336 | if (ret) | |
b037c0fd | 1337 | goto unlock; |
ebc087d0 GL |
1338 | case VB2_BUF_STATE_PREPARED: |
1339 | break; | |
1340 | default: | |
e23ccc0a | 1341 | dprintk(1, "qbuf: buffer already in use\n"); |
b037c0fd MS |
1342 | ret = -EINVAL; |
1343 | goto unlock; | |
e23ccc0a PO |
1344 | } |
1345 | ||
e23ccc0a PO |
1346 | /* |
1347 | * Add to the queued buffers list, a buffer will stay on it until | |
1348 | * dequeued in dqbuf. | |
1349 | */ | |
1350 | list_add_tail(&vb->queued_entry, &q->queued_list); | |
1351 | vb->state = VB2_BUF_STATE_QUEUED; | |
1352 | ||
1353 | /* | |
1354 | * If already streaming, give the buffer to driver for processing. | |
1355 | * If not, the buffer will be given to driver on next streamon. | |
1356 | */ | |
1357 | if (q->streaming) | |
1358 | __enqueue_in_driver(vb); | |
1359 | ||
21db3e07 GL |
1360 | /* Fill buffer information for the userspace */ |
1361 | __fill_v4l2_buffer(vb, b); | |
1362 | ||
e23ccc0a | 1363 | dprintk(1, "qbuf of buffer %d succeeded\n", vb->v4l2_buf.index); |
b037c0fd MS |
1364 | unlock: |
1365 | if (mmap_sem) | |
1366 | up_read(mmap_sem); | |
1367 | return ret; | |
e23ccc0a PO |
1368 | } |
1369 | EXPORT_SYMBOL_GPL(vb2_qbuf); | |
1370 | ||
1371 | /** | |
1372 | * __vb2_wait_for_done_vb() - wait for a buffer to become available | |
1373 | * for dequeuing | |
1374 | * | |
1375 | * Will sleep if required for nonblocking == false. | |
1376 | */ | |
1377 | static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking) | |
1378 | { | |
1379 | /* | |
1380 | * All operations on vb_done_list are performed under done_lock | |
1381 | * spinlock protection. However, buffers may be removed from | |
1382 | * it and returned to userspace only while holding both driver's | |
1383 | * lock and the done_lock spinlock. Thus we can be sure that as | |
1384 | * long as we hold the driver's lock, the list will remain not | |
1385 | * empty if list_empty() check succeeds. | |
1386 | */ | |
1387 | ||
1388 | for (;;) { | |
1389 | int ret; | |
1390 | ||
1391 | if (!q->streaming) { | |
1392 | dprintk(1, "Streaming off, will not wait for buffers\n"); | |
1393 | return -EINVAL; | |
1394 | } | |
1395 | ||
1396 | if (!list_empty(&q->done_list)) { | |
1397 | /* | |
1398 | * Found a buffer that we were waiting for. | |
1399 | */ | |
1400 | break; | |
1401 | } | |
1402 | ||
1403 | if (nonblocking) { | |
1404 | dprintk(1, "Nonblocking and no buffers to dequeue, " | |
1405 | "will not wait\n"); | |
1406 | return -EAGAIN; | |
1407 | } | |
1408 | ||
1409 | /* | |
1410 | * We are streaming and blocking, wait for another buffer to | |
1411 | * become ready or for streamoff. Driver's lock is released to | |
1412 | * allow streamoff or qbuf to be called while waiting. | |
1413 | */ | |
1414 | call_qop(q, wait_prepare, q); | |
1415 | ||
1416 | /* | |
1417 | * All locks have been released, it is safe to sleep now. | |
1418 | */ | |
1419 | dprintk(3, "Will sleep waiting for buffers\n"); | |
1420 | ret = wait_event_interruptible(q->done_wq, | |
1421 | !list_empty(&q->done_list) || !q->streaming); | |
1422 | ||
1423 | /* | |
1424 | * We need to reevaluate both conditions again after reacquiring | |
1425 | * the locks or return an error if one occurred. | |
1426 | */ | |
1427 | call_qop(q, wait_finish, q); | |
32a77260 HV |
1428 | if (ret) { |
1429 | dprintk(1, "Sleep was interrupted\n"); | |
e23ccc0a | 1430 | return ret; |
32a77260 | 1431 | } |
e23ccc0a PO |
1432 | } |
1433 | return 0; | |
1434 | } | |
1435 | ||
1436 | /** | |
1437 | * __vb2_get_done_vb() - get a buffer ready for dequeuing | |
1438 | * | |
1439 | * Will sleep if required for nonblocking == false. | |
1440 | */ | |
1441 | static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb, | |
32a77260 | 1442 | struct v4l2_buffer *b, int nonblocking) |
e23ccc0a PO |
1443 | { |
1444 | unsigned long flags; | |
1445 | int ret; | |
1446 | ||
1447 | /* | |
1448 | * Wait for at least one buffer to become available on the done_list. | |
1449 | */ | |
1450 | ret = __vb2_wait_for_done_vb(q, nonblocking); | |
1451 | if (ret) | |
1452 | return ret; | |
1453 | ||
1454 | /* | |
1455 | * Driver's lock has been held since we last verified that done_list | |
1456 | * is not empty, so no need for another list_empty(done_list) check. | |
1457 | */ | |
1458 | spin_lock_irqsave(&q->done_lock, flags); | |
1459 | *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry); | |
32a77260 HV |
1460 | /* |
1461 | * Only remove the buffer from done_list if v4l2_buffer can handle all | |
1462 | * the planes. | |
1463 | */ | |
1464 | ret = __verify_planes_array(*vb, b); | |
1465 | if (!ret) | |
1466 | list_del(&(*vb)->done_entry); | |
e23ccc0a PO |
1467 | spin_unlock_irqrestore(&q->done_lock, flags); |
1468 | ||
32a77260 | 1469 | return ret; |
e23ccc0a PO |
1470 | } |
1471 | ||
1472 | /** | |
1473 | * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2 | |
1474 | * @q: videobuf2 queue | |
1475 | * | |
1476 | * This function will wait until all buffers that have been given to the driver | |
1477 | * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call | |
1478 | * wait_prepare, wait_finish pair. It is intended to be called with all locks | |
1479 | * taken, for example from stop_streaming() callback. | |
1480 | */ | |
1481 | int vb2_wait_for_all_buffers(struct vb2_queue *q) | |
1482 | { | |
1483 | if (!q->streaming) { | |
1484 | dprintk(1, "Streaming off, will not wait for buffers\n"); | |
1485 | return -EINVAL; | |
1486 | } | |
1487 | ||
1488 | wait_event(q->done_wq, !atomic_read(&q->queued_count)); | |
1489 | return 0; | |
1490 | } | |
1491 | EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers); | |
1492 | ||
c5384048 SS |
1493 | /** |
1494 | * __vb2_dqbuf() - bring back the buffer to the DEQUEUED state | |
1495 | */ | |
1496 | static void __vb2_dqbuf(struct vb2_buffer *vb) | |
1497 | { | |
1498 | struct vb2_queue *q = vb->vb2_queue; | |
1499 | unsigned int i; | |
1500 | ||
1501 | /* nothing to do if the buffer is already dequeued */ | |
1502 | if (vb->state == VB2_BUF_STATE_DEQUEUED) | |
1503 | return; | |
1504 | ||
1505 | vb->state = VB2_BUF_STATE_DEQUEUED; | |
1506 | ||
1507 | /* unmap DMABUF buffer */ | |
1508 | if (q->memory == V4L2_MEMORY_DMABUF) | |
1509 | for (i = 0; i < vb->num_planes; ++i) { | |
1510 | if (!vb->planes[i].dbuf_mapped) | |
1511 | continue; | |
1512 | call_memop(q, unmap_dmabuf, vb->planes[i].mem_priv); | |
1513 | vb->planes[i].dbuf_mapped = 0; | |
1514 | } | |
1515 | } | |
1516 | ||
e23ccc0a PO |
1517 | /** |
1518 | * vb2_dqbuf() - Dequeue a buffer to the userspace | |
1519 | * @q: videobuf2 queue | |
1520 | * @b: buffer structure passed from userspace to vidioc_dqbuf handler | |
1521 | * in driver | |
1522 | * @nonblocking: if true, this call will not sleep waiting for a buffer if no | |
1523 | * buffers ready for dequeuing are present. Normally the driver | |
1524 | * would be passing (file->f_flags & O_NONBLOCK) here | |
1525 | * | |
1526 | * Should be called from vidioc_dqbuf ioctl handler of a driver. | |
1527 | * This function: | |
1528 | * 1) verifies the passed buffer, | |
1529 | * 2) calls buf_finish callback in the driver (if provided), in which | |
1530 | * driver can perform any additional operations that may be required before | |
1531 | * returning the buffer to userspace, such as cache sync, | |
1532 | * 3) the buffer struct members are filled with relevant information for | |
1533 | * the userspace. | |
1534 | * | |
1535 | * The return values from this function are intended to be directly returned | |
1536 | * from vidioc_dqbuf handler in driver. | |
1537 | */ | |
1538 | int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking) | |
1539 | { | |
1540 | struct vb2_buffer *vb = NULL; | |
1541 | int ret; | |
1542 | ||
b25748fe MS |
1543 | if (q->fileio) { |
1544 | dprintk(1, "dqbuf: file io in progress\n"); | |
1545 | return -EBUSY; | |
1546 | } | |
1547 | ||
e23ccc0a PO |
1548 | if (b->type != q->type) { |
1549 | dprintk(1, "dqbuf: invalid buffer type\n"); | |
1550 | return -EINVAL; | |
1551 | } | |
32a77260 HV |
1552 | ret = __vb2_get_done_vb(q, &vb, b, nonblocking); |
1553 | if (ret < 0) | |
e23ccc0a | 1554 | return ret; |
e23ccc0a PO |
1555 | |
1556 | ret = call_qop(q, buf_finish, vb); | |
1557 | if (ret) { | |
1558 | dprintk(1, "dqbuf: buffer finish failed\n"); | |
1559 | return ret; | |
1560 | } | |
1561 | ||
1562 | switch (vb->state) { | |
1563 | case VB2_BUF_STATE_DONE: | |
1564 | dprintk(3, "dqbuf: Returning done buffer\n"); | |
1565 | break; | |
1566 | case VB2_BUF_STATE_ERROR: | |
1567 | dprintk(3, "dqbuf: Returning done buffer with errors\n"); | |
1568 | break; | |
1569 | default: | |
1570 | dprintk(1, "dqbuf: Invalid buffer state\n"); | |
1571 | return -EINVAL; | |
1572 | } | |
1573 | ||
1574 | /* Fill buffer information for the userspace */ | |
1575 | __fill_v4l2_buffer(vb, b); | |
1576 | /* Remove from videobuf queue */ | |
1577 | list_del(&vb->queued_entry); | |
c5384048 SS |
1578 | /* go back to dequeued state */ |
1579 | __vb2_dqbuf(vb); | |
e23ccc0a PO |
1580 | |
1581 | dprintk(1, "dqbuf of buffer %d, with state %d\n", | |
1582 | vb->v4l2_buf.index, vb->state); | |
1583 | ||
e23ccc0a PO |
1584 | return 0; |
1585 | } | |
1586 | EXPORT_SYMBOL_GPL(vb2_dqbuf); | |
1587 | ||
bd323e28 MS |
1588 | /** |
1589 | * __vb2_queue_cancel() - cancel and stop (pause) streaming | |
1590 | * | |
1591 | * Removes all queued buffers from driver's queue and all buffers queued by | |
1592 | * userspace from videobuf's queue. Returns to state after reqbufs. | |
1593 | */ | |
1594 | static void __vb2_queue_cancel(struct vb2_queue *q) | |
1595 | { | |
1596 | unsigned int i; | |
1597 | ||
1598 | /* | |
1599 | * Tell driver to stop all transactions and release all queued | |
1600 | * buffers. | |
1601 | */ | |
1602 | if (q->streaming) | |
1603 | call_qop(q, stop_streaming, q); | |
1604 | q->streaming = 0; | |
1605 | ||
1606 | /* | |
1607 | * Remove all buffers from videobuf's list... | |
1608 | */ | |
1609 | INIT_LIST_HEAD(&q->queued_list); | |
1610 | /* | |
1611 | * ...and done list; userspace will not receive any buffers it | |
1612 | * has not already dequeued before initiating cancel. | |
1613 | */ | |
1614 | INIT_LIST_HEAD(&q->done_list); | |
1615 | atomic_set(&q->queued_count, 0); | |
1616 | wake_up_all(&q->done_wq); | |
1617 | ||
1618 | /* | |
1619 | * Reinitialize all buffers for next use. | |
1620 | */ | |
1621 | for (i = 0; i < q->num_buffers; ++i) | |
c5384048 | 1622 | __vb2_dqbuf(q->bufs[i]); |
bd323e28 MS |
1623 | } |
1624 | ||
e23ccc0a PO |
1625 | /** |
1626 | * vb2_streamon - start streaming | |
1627 | * @q: videobuf2 queue | |
1628 | * @type: type argument passed from userspace to vidioc_streamon handler | |
1629 | * | |
1630 | * Should be called from vidioc_streamon handler of a driver. | |
1631 | * This function: | |
1632 | * 1) verifies current state | |
bd323e28 | 1633 | * 2) passes any previously queued buffers to the driver and starts streaming |
e23ccc0a PO |
1634 | * |
1635 | * The return values from this function are intended to be directly returned | |
1636 | * from vidioc_streamon handler in the driver. | |
1637 | */ | |
1638 | int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type) | |
1639 | { | |
1640 | struct vb2_buffer *vb; | |
5db2c3ba | 1641 | int ret; |
e23ccc0a | 1642 | |
b25748fe MS |
1643 | if (q->fileio) { |
1644 | dprintk(1, "streamon: file io in progress\n"); | |
1645 | return -EBUSY; | |
1646 | } | |
1647 | ||
e23ccc0a PO |
1648 | if (type != q->type) { |
1649 | dprintk(1, "streamon: invalid stream type\n"); | |
1650 | return -EINVAL; | |
1651 | } | |
1652 | ||
1653 | if (q->streaming) { | |
1654 | dprintk(1, "streamon: already streaming\n"); | |
1655 | return -EBUSY; | |
1656 | } | |
1657 | ||
1658 | /* | |
bd323e28 MS |
1659 | * If any buffers were queued before streamon, |
1660 | * we can now pass them to driver for processing. | |
e23ccc0a | 1661 | */ |
bd323e28 MS |
1662 | list_for_each_entry(vb, &q->queued_list, queued_entry) |
1663 | __enqueue_in_driver(vb); | |
e23ccc0a | 1664 | |
e23ccc0a PO |
1665 | /* |
1666 | * Let driver notice that streaming state has been enabled. | |
1667 | */ | |
bd323e28 | 1668 | ret = call_qop(q, start_streaming, q, atomic_read(&q->queued_count)); |
5db2c3ba PO |
1669 | if (ret) { |
1670 | dprintk(1, "streamon: driver refused to start streaming\n"); | |
bd323e28 | 1671 | __vb2_queue_cancel(q); |
5db2c3ba PO |
1672 | return ret; |
1673 | } | |
1674 | ||
1675 | q->streaming = 1; | |
e23ccc0a | 1676 | |
e23ccc0a PO |
1677 | dprintk(3, "Streamon successful\n"); |
1678 | return 0; | |
1679 | } | |
1680 | EXPORT_SYMBOL_GPL(vb2_streamon); | |
1681 | ||
e23ccc0a PO |
1682 | |
1683 | /** | |
1684 | * vb2_streamoff - stop streaming | |
1685 | * @q: videobuf2 queue | |
1686 | * @type: type argument passed from userspace to vidioc_streamoff handler | |
1687 | * | |
1688 | * Should be called from vidioc_streamoff handler of a driver. | |
1689 | * This function: | |
1690 | * 1) verifies current state, | |
1691 | * 2) stop streaming and dequeues any queued buffers, including those previously | |
1692 | * passed to the driver (after waiting for the driver to finish). | |
1693 | * | |
1694 | * This call can be used for pausing playback. | |
1695 | * The return values from this function are intended to be directly returned | |
1696 | * from vidioc_streamoff handler in the driver | |
1697 | */ | |
1698 | int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type) | |
1699 | { | |
b25748fe MS |
1700 | if (q->fileio) { |
1701 | dprintk(1, "streamoff: file io in progress\n"); | |
1702 | return -EBUSY; | |
1703 | } | |
1704 | ||
e23ccc0a PO |
1705 | if (type != q->type) { |
1706 | dprintk(1, "streamoff: invalid stream type\n"); | |
1707 | return -EINVAL; | |
1708 | } | |
1709 | ||
1710 | if (!q->streaming) { | |
1711 | dprintk(1, "streamoff: not streaming\n"); | |
1712 | return -EINVAL; | |
1713 | } | |
1714 | ||
1715 | /* | |
1716 | * Cancel will pause streaming and remove all buffers from the driver | |
1717 | * and videobuf, effectively returning control over them to userspace. | |
1718 | */ | |
1719 | __vb2_queue_cancel(q); | |
1720 | ||
1721 | dprintk(3, "Streamoff successful\n"); | |
1722 | return 0; | |
1723 | } | |
1724 | EXPORT_SYMBOL_GPL(vb2_streamoff); | |
1725 | ||
1726 | /** | |
1727 | * __find_plane_by_offset() - find plane associated with the given offset off | |
1728 | */ | |
1729 | static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off, | |
1730 | unsigned int *_buffer, unsigned int *_plane) | |
1731 | { | |
1732 | struct vb2_buffer *vb; | |
1733 | unsigned int buffer, plane; | |
1734 | ||
1735 | /* | |
1736 | * Go over all buffers and their planes, comparing the given offset | |
1737 | * with an offset assigned to each plane. If a match is found, | |
1738 | * return its buffer and plane numbers. | |
1739 | */ | |
1740 | for (buffer = 0; buffer < q->num_buffers; ++buffer) { | |
1741 | vb = q->bufs[buffer]; | |
1742 | ||
1743 | for (plane = 0; plane < vb->num_planes; ++plane) { | |
1744 | if (vb->v4l2_planes[plane].m.mem_offset == off) { | |
1745 | *_buffer = buffer; | |
1746 | *_plane = plane; | |
1747 | return 0; | |
1748 | } | |
1749 | } | |
1750 | } | |
1751 | ||
1752 | return -EINVAL; | |
1753 | } | |
1754 | ||
83ae7c5a TS |
1755 | /** |
1756 | * vb2_expbuf() - Export a buffer as a file descriptor | |
1757 | * @q: videobuf2 queue | |
1758 | * @eb: export buffer structure passed from userspace to vidioc_expbuf | |
1759 | * handler in driver | |
1760 | * | |
1761 | * The return values from this function are intended to be directly returned | |
1762 | * from vidioc_expbuf handler in driver. | |
1763 | */ | |
1764 | int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb) | |
1765 | { | |
1766 | struct vb2_buffer *vb = NULL; | |
1767 | struct vb2_plane *vb_plane; | |
1768 | int ret; | |
1769 | struct dma_buf *dbuf; | |
1770 | ||
1771 | if (q->memory != V4L2_MEMORY_MMAP) { | |
1772 | dprintk(1, "Queue is not currently set up for mmap\n"); | |
1773 | return -EINVAL; | |
1774 | } | |
1775 | ||
1776 | if (!q->mem_ops->get_dmabuf) { | |
1777 | dprintk(1, "Queue does not support DMA buffer exporting\n"); | |
1778 | return -EINVAL; | |
1779 | } | |
1780 | ||
1781 | if (eb->flags & ~O_CLOEXEC) { | |
1782 | dprintk(1, "Queue does support only O_CLOEXEC flag\n"); | |
1783 | return -EINVAL; | |
1784 | } | |
1785 | ||
1786 | if (eb->type != q->type) { | |
1787 | dprintk(1, "qbuf: invalid buffer type\n"); | |
1788 | return -EINVAL; | |
1789 | } | |
1790 | ||
1791 | if (eb->index >= q->num_buffers) { | |
1792 | dprintk(1, "buffer index out of range\n"); | |
1793 | return -EINVAL; | |
1794 | } | |
1795 | ||
1796 | vb = q->bufs[eb->index]; | |
1797 | ||
1798 | if (eb->plane >= vb->num_planes) { | |
1799 | dprintk(1, "buffer plane out of range\n"); | |
1800 | return -EINVAL; | |
1801 | } | |
1802 | ||
1803 | vb_plane = &vb->planes[eb->plane]; | |
1804 | ||
1805 | dbuf = call_memop(q, get_dmabuf, vb_plane->mem_priv); | |
1806 | if (IS_ERR_OR_NULL(dbuf)) { | |
1807 | dprintk(1, "Failed to export buffer %d, plane %d\n", | |
1808 | eb->index, eb->plane); | |
1809 | return -EINVAL; | |
1810 | } | |
1811 | ||
1812 | ret = dma_buf_fd(dbuf, eb->flags); | |
1813 | if (ret < 0) { | |
1814 | dprintk(3, "buffer %d, plane %d failed to export (%d)\n", | |
1815 | eb->index, eb->plane, ret); | |
1816 | dma_buf_put(dbuf); | |
1817 | return ret; | |
1818 | } | |
1819 | ||
1820 | dprintk(3, "buffer %d, plane %d exported as %d descriptor\n", | |
1821 | eb->index, eb->plane, ret); | |
1822 | eb->fd = ret; | |
1823 | ||
1824 | return 0; | |
1825 | } | |
1826 | EXPORT_SYMBOL_GPL(vb2_expbuf); | |
1827 | ||
e23ccc0a PO |
1828 | /** |
1829 | * vb2_mmap() - map video buffers into application address space | |
1830 | * @q: videobuf2 queue | |
1831 | * @vma: vma passed to the mmap file operation handler in the driver | |
1832 | * | |
1833 | * Should be called from mmap file operation handler of a driver. | |
1834 | * This function maps one plane of one of the available video buffers to | |
1835 | * userspace. To map whole video memory allocated on reqbufs, this function | |
1836 | * has to be called once per each plane per each buffer previously allocated. | |
1837 | * | |
1838 | * When the userspace application calls mmap, it passes to it an offset returned | |
1839 | * to it earlier by the means of vidioc_querybuf handler. That offset acts as | |
1840 | * a "cookie", which is then used to identify the plane to be mapped. | |
1841 | * This function finds a plane with a matching offset and a mapping is performed | |
1842 | * by the means of a provided memory operation. | |
1843 | * | |
1844 | * The return values from this function are intended to be directly returned | |
1845 | * from the mmap handler in driver. | |
1846 | */ | |
1847 | int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma) | |
1848 | { | |
1849 | unsigned long off = vma->vm_pgoff << PAGE_SHIFT; | |
e23ccc0a PO |
1850 | struct vb2_buffer *vb; |
1851 | unsigned int buffer, plane; | |
1852 | int ret; | |
1853 | ||
1854 | if (q->memory != V4L2_MEMORY_MMAP) { | |
1855 | dprintk(1, "Queue is not currently set up for mmap\n"); | |
1856 | return -EINVAL; | |
1857 | } | |
1858 | ||
1859 | /* | |
1860 | * Check memory area access mode. | |
1861 | */ | |
1862 | if (!(vma->vm_flags & VM_SHARED)) { | |
1863 | dprintk(1, "Invalid vma flags, VM_SHARED needed\n"); | |
1864 | return -EINVAL; | |
1865 | } | |
1866 | if (V4L2_TYPE_IS_OUTPUT(q->type)) { | |
1867 | if (!(vma->vm_flags & VM_WRITE)) { | |
1868 | dprintk(1, "Invalid vma flags, VM_WRITE needed\n"); | |
1869 | return -EINVAL; | |
1870 | } | |
1871 | } else { | |
1872 | if (!(vma->vm_flags & VM_READ)) { | |
1873 | dprintk(1, "Invalid vma flags, VM_READ needed\n"); | |
1874 | return -EINVAL; | |
1875 | } | |
1876 | } | |
1877 | ||
1878 | /* | |
1879 | * Find the plane corresponding to the offset passed by userspace. | |
1880 | */ | |
1881 | ret = __find_plane_by_offset(q, off, &buffer, &plane); | |
1882 | if (ret) | |
1883 | return ret; | |
1884 | ||
1885 | vb = q->bufs[buffer]; | |
e23ccc0a | 1886 | |
a00d0266 | 1887 | ret = call_memop(q, mmap, vb->planes[plane].mem_priv, vma); |
e23ccc0a PO |
1888 | if (ret) |
1889 | return ret; | |
1890 | ||
e23ccc0a PO |
1891 | dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer, plane); |
1892 | return 0; | |
1893 | } | |
1894 | EXPORT_SYMBOL_GPL(vb2_mmap); | |
1895 | ||
6f524ec1 SJ |
1896 | #ifndef CONFIG_MMU |
1897 | unsigned long vb2_get_unmapped_area(struct vb2_queue *q, | |
1898 | unsigned long addr, | |
1899 | unsigned long len, | |
1900 | unsigned long pgoff, | |
1901 | unsigned long flags) | |
1902 | { | |
1903 | unsigned long off = pgoff << PAGE_SHIFT; | |
1904 | struct vb2_buffer *vb; | |
1905 | unsigned int buffer, plane; | |
1906 | int ret; | |
1907 | ||
1908 | if (q->memory != V4L2_MEMORY_MMAP) { | |
1909 | dprintk(1, "Queue is not currently set up for mmap\n"); | |
1910 | return -EINVAL; | |
1911 | } | |
1912 | ||
1913 | /* | |
1914 | * Find the plane corresponding to the offset passed by userspace. | |
1915 | */ | |
1916 | ret = __find_plane_by_offset(q, off, &buffer, &plane); | |
1917 | if (ret) | |
1918 | return ret; | |
1919 | ||
1920 | vb = q->bufs[buffer]; | |
1921 | ||
1922 | return (unsigned long)vb2_plane_vaddr(vb, plane); | |
1923 | } | |
1924 | EXPORT_SYMBOL_GPL(vb2_get_unmapped_area); | |
1925 | #endif | |
1926 | ||
b25748fe MS |
1927 | static int __vb2_init_fileio(struct vb2_queue *q, int read); |
1928 | static int __vb2_cleanup_fileio(struct vb2_queue *q); | |
e23ccc0a PO |
1929 | |
1930 | /** | |
1931 | * vb2_poll() - implements poll userspace operation | |
1932 | * @q: videobuf2 queue | |
1933 | * @file: file argument passed to the poll file operation handler | |
1934 | * @wait: wait argument passed to the poll file operation handler | |
1935 | * | |
1936 | * This function implements poll file operation handler for a driver. | |
1937 | * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will | |
1938 | * be informed that the file descriptor of a video device is available for | |
1939 | * reading. | |
1940 | * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor | |
1941 | * will be reported as available for writing. | |
1942 | * | |
95213ceb HV |
1943 | * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any |
1944 | * pending events. | |
1945 | * | |
e23ccc0a PO |
1946 | * The return values from this function are intended to be directly returned |
1947 | * from poll handler in driver. | |
1948 | */ | |
1949 | unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait) | |
1950 | { | |
95213ceb | 1951 | struct video_device *vfd = video_devdata(file); |
bf5c7cbb | 1952 | unsigned long req_events = poll_requested_events(wait); |
e23ccc0a | 1953 | struct vb2_buffer *vb = NULL; |
95213ceb HV |
1954 | unsigned int res = 0; |
1955 | unsigned long flags; | |
1956 | ||
1957 | if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) { | |
1958 | struct v4l2_fh *fh = file->private_data; | |
1959 | ||
1960 | if (v4l2_event_pending(fh)) | |
1961 | res = POLLPRI; | |
1962 | else if (req_events & POLLPRI) | |
1963 | poll_wait(file, &fh->wait, wait); | |
1964 | } | |
e23ccc0a | 1965 | |
b25748fe | 1966 | /* |
4ffabdb3 | 1967 | * Start file I/O emulator only if streaming API has not been used yet. |
b25748fe MS |
1968 | */ |
1969 | if (q->num_buffers == 0 && q->fileio == NULL) { | |
bf5c7cbb HV |
1970 | if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) && |
1971 | (req_events & (POLLIN | POLLRDNORM))) { | |
95213ceb HV |
1972 | if (__vb2_init_fileio(q, 1)) |
1973 | return res | POLLERR; | |
b25748fe | 1974 | } |
bf5c7cbb HV |
1975 | if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) && |
1976 | (req_events & (POLLOUT | POLLWRNORM))) { | |
95213ceb HV |
1977 | if (__vb2_init_fileio(q, 0)) |
1978 | return res | POLLERR; | |
b25748fe MS |
1979 | /* |
1980 | * Write to OUTPUT queue can be done immediately. | |
1981 | */ | |
95213ceb | 1982 | return res | POLLOUT | POLLWRNORM; |
b25748fe MS |
1983 | } |
1984 | } | |
1985 | ||
e23ccc0a PO |
1986 | /* |
1987 | * There is nothing to wait for if no buffers have already been queued. | |
1988 | */ | |
1989 | if (list_empty(&q->queued_list)) | |
95213ceb | 1990 | return res | POLLERR; |
e23ccc0a PO |
1991 | |
1992 | poll_wait(file, &q->done_wq, wait); | |
1993 | ||
1994 | /* | |
1995 | * Take first buffer available for dequeuing. | |
1996 | */ | |
1997 | spin_lock_irqsave(&q->done_lock, flags); | |
1998 | if (!list_empty(&q->done_list)) | |
1999 | vb = list_first_entry(&q->done_list, struct vb2_buffer, | |
2000 | done_entry); | |
2001 | spin_unlock_irqrestore(&q->done_lock, flags); | |
2002 | ||
2003 | if (vb && (vb->state == VB2_BUF_STATE_DONE | |
2004 | || vb->state == VB2_BUF_STATE_ERROR)) { | |
95213ceb HV |
2005 | return (V4L2_TYPE_IS_OUTPUT(q->type)) ? |
2006 | res | POLLOUT | POLLWRNORM : | |
2007 | res | POLLIN | POLLRDNORM; | |
e23ccc0a | 2008 | } |
95213ceb | 2009 | return res; |
e23ccc0a PO |
2010 | } |
2011 | EXPORT_SYMBOL_GPL(vb2_poll); | |
2012 | ||
2013 | /** | |
2014 | * vb2_queue_init() - initialize a videobuf2 queue | |
2015 | * @q: videobuf2 queue; this structure should be allocated in driver | |
2016 | * | |
2017 | * The vb2_queue structure should be allocated by the driver. The driver is | |
2018 | * responsible of clearing it's content and setting initial values for some | |
2019 | * required entries before calling this function. | |
2020 | * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer | |
2021 | * to the struct vb2_queue description in include/media/videobuf2-core.h | |
2022 | * for more information. | |
2023 | */ | |
2024 | int vb2_queue_init(struct vb2_queue *q) | |
2025 | { | |
896f38f5 EG |
2026 | /* |
2027 | * Sanity check | |
2028 | */ | |
2029 | if (WARN_ON(!q) || | |
2030 | WARN_ON(!q->ops) || | |
2031 | WARN_ON(!q->mem_ops) || | |
2032 | WARN_ON(!q->type) || | |
2033 | WARN_ON(!q->io_modes) || | |
2034 | WARN_ON(!q->ops->queue_setup) || | |
2035 | WARN_ON(!q->ops->buf_queue)) | |
2036 | return -EINVAL; | |
e23ccc0a PO |
2037 | |
2038 | INIT_LIST_HEAD(&q->queued_list); | |
2039 | INIT_LIST_HEAD(&q->done_list); | |
2040 | spin_lock_init(&q->done_lock); | |
2041 | init_waitqueue_head(&q->done_wq); | |
2042 | ||
2043 | if (q->buf_struct_size == 0) | |
2044 | q->buf_struct_size = sizeof(struct vb2_buffer); | |
2045 | ||
2046 | return 0; | |
2047 | } | |
2048 | EXPORT_SYMBOL_GPL(vb2_queue_init); | |
2049 | ||
2050 | /** | |
2051 | * vb2_queue_release() - stop streaming, release the queue and free memory | |
2052 | * @q: videobuf2 queue | |
2053 | * | |
2054 | * This function stops streaming and performs necessary clean ups, including | |
2055 | * freeing video buffer memory. The driver is responsible for freeing | |
2056 | * the vb2_queue structure itself. | |
2057 | */ | |
2058 | void vb2_queue_release(struct vb2_queue *q) | |
2059 | { | |
b25748fe | 2060 | __vb2_cleanup_fileio(q); |
e23ccc0a | 2061 | __vb2_queue_cancel(q); |
2d86401c | 2062 | __vb2_queue_free(q, q->num_buffers); |
e23ccc0a PO |
2063 | } |
2064 | EXPORT_SYMBOL_GPL(vb2_queue_release); | |
2065 | ||
b25748fe MS |
2066 | /** |
2067 | * struct vb2_fileio_buf - buffer context used by file io emulator | |
2068 | * | |
2069 | * vb2 provides a compatibility layer and emulator of file io (read and | |
2070 | * write) calls on top of streaming API. This structure is used for | |
2071 | * tracking context related to the buffers. | |
2072 | */ | |
2073 | struct vb2_fileio_buf { | |
2074 | void *vaddr; | |
2075 | unsigned int size; | |
2076 | unsigned int pos; | |
2077 | unsigned int queued:1; | |
2078 | }; | |
2079 | ||
2080 | /** | |
2081 | * struct vb2_fileio_data - queue context used by file io emulator | |
2082 | * | |
2083 | * vb2 provides a compatibility layer and emulator of file io (read and | |
2084 | * write) calls on top of streaming API. For proper operation it required | |
2085 | * this structure to save the driver state between each call of the read | |
2086 | * or write function. | |
2087 | */ | |
2088 | struct vb2_fileio_data { | |
2089 | struct v4l2_requestbuffers req; | |
2090 | struct v4l2_buffer b; | |
2091 | struct vb2_fileio_buf bufs[VIDEO_MAX_FRAME]; | |
2092 | unsigned int index; | |
2093 | unsigned int q_count; | |
2094 | unsigned int dq_count; | |
2095 | unsigned int flags; | |
2096 | }; | |
2097 | ||
2098 | /** | |
2099 | * __vb2_init_fileio() - initialize file io emulator | |
2100 | * @q: videobuf2 queue | |
2101 | * @read: mode selector (1 means read, 0 means write) | |
2102 | */ | |
2103 | static int __vb2_init_fileio(struct vb2_queue *q, int read) | |
2104 | { | |
2105 | struct vb2_fileio_data *fileio; | |
2106 | int i, ret; | |
2107 | unsigned int count = 0; | |
2108 | ||
2109 | /* | |
2110 | * Sanity check | |
2111 | */ | |
2112 | if ((read && !(q->io_modes & VB2_READ)) || | |
2113 | (!read && !(q->io_modes & VB2_WRITE))) | |
2114 | BUG(); | |
2115 | ||
2116 | /* | |
2117 | * Check if device supports mapping buffers to kernel virtual space. | |
2118 | */ | |
2119 | if (!q->mem_ops->vaddr) | |
2120 | return -EBUSY; | |
2121 | ||
2122 | /* | |
2123 | * Check if streaming api has not been already activated. | |
2124 | */ | |
2125 | if (q->streaming || q->num_buffers > 0) | |
2126 | return -EBUSY; | |
2127 | ||
2128 | /* | |
2129 | * Start with count 1, driver can increase it in queue_setup() | |
2130 | */ | |
2131 | count = 1; | |
2132 | ||
2133 | dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n", | |
2134 | (read) ? "read" : "write", count, q->io_flags); | |
2135 | ||
2136 | fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL); | |
2137 | if (fileio == NULL) | |
2138 | return -ENOMEM; | |
2139 | ||
2140 | fileio->flags = q->io_flags; | |
2141 | ||
2142 | /* | |
2143 | * Request buffers and use MMAP type to force driver | |
2144 | * to allocate buffers by itself. | |
2145 | */ | |
2146 | fileio->req.count = count; | |
2147 | fileio->req.memory = V4L2_MEMORY_MMAP; | |
2148 | fileio->req.type = q->type; | |
2149 | ret = vb2_reqbufs(q, &fileio->req); | |
2150 | if (ret) | |
2151 | goto err_kfree; | |
2152 | ||
2153 | /* | |
2154 | * Check if plane_count is correct | |
2155 | * (multiplane buffers are not supported). | |
2156 | */ | |
2157 | if (q->bufs[0]->num_planes != 1) { | |
b25748fe MS |
2158 | ret = -EBUSY; |
2159 | goto err_reqbufs; | |
2160 | } | |
2161 | ||
2162 | /* | |
2163 | * Get kernel address of each buffer. | |
2164 | */ | |
2165 | for (i = 0; i < q->num_buffers; i++) { | |
2166 | fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0); | |
2167 | if (fileio->bufs[i].vaddr == NULL) | |
2168 | goto err_reqbufs; | |
2169 | fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0); | |
2170 | } | |
2171 | ||
2172 | /* | |
2173 | * Read mode requires pre queuing of all buffers. | |
2174 | */ | |
2175 | if (read) { | |
2176 | /* | |
2177 | * Queue all buffers. | |
2178 | */ | |
2179 | for (i = 0; i < q->num_buffers; i++) { | |
2180 | struct v4l2_buffer *b = &fileio->b; | |
2181 | memset(b, 0, sizeof(*b)); | |
2182 | b->type = q->type; | |
2183 | b->memory = q->memory; | |
2184 | b->index = i; | |
2185 | ret = vb2_qbuf(q, b); | |
2186 | if (ret) | |
2187 | goto err_reqbufs; | |
2188 | fileio->bufs[i].queued = 1; | |
2189 | } | |
2190 | ||
2191 | /* | |
2192 | * Start streaming. | |
2193 | */ | |
2194 | ret = vb2_streamon(q, q->type); | |
2195 | if (ret) | |
2196 | goto err_reqbufs; | |
2197 | } | |
2198 | ||
2199 | q->fileio = fileio; | |
2200 | ||
2201 | return ret; | |
2202 | ||
2203 | err_reqbufs: | |
a67e1722 | 2204 | fileio->req.count = 0; |
b25748fe MS |
2205 | vb2_reqbufs(q, &fileio->req); |
2206 | ||
2207 | err_kfree: | |
2208 | kfree(fileio); | |
2209 | return ret; | |
2210 | } | |
2211 | ||
2212 | /** | |
2213 | * __vb2_cleanup_fileio() - free resourced used by file io emulator | |
2214 | * @q: videobuf2 queue | |
2215 | */ | |
2216 | static int __vb2_cleanup_fileio(struct vb2_queue *q) | |
2217 | { | |
2218 | struct vb2_fileio_data *fileio = q->fileio; | |
2219 | ||
2220 | if (fileio) { | |
2221 | /* | |
2222 | * Hack fileio context to enable direct calls to vb2 ioctl | |
2223 | * interface. | |
2224 | */ | |
2225 | q->fileio = NULL; | |
2226 | ||
2227 | vb2_streamoff(q, q->type); | |
2228 | fileio->req.count = 0; | |
2229 | vb2_reqbufs(q, &fileio->req); | |
2230 | kfree(fileio); | |
2231 | dprintk(3, "file io emulator closed\n"); | |
2232 | } | |
2233 | return 0; | |
2234 | } | |
2235 | ||
2236 | /** | |
2237 | * __vb2_perform_fileio() - perform a single file io (read or write) operation | |
2238 | * @q: videobuf2 queue | |
2239 | * @data: pointed to target userspace buffer | |
2240 | * @count: number of bytes to read or write | |
2241 | * @ppos: file handle position tracking pointer | |
2242 | * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking) | |
2243 | * @read: access mode selector (1 means read, 0 means write) | |
2244 | */ | |
2245 | static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count, | |
2246 | loff_t *ppos, int nonblock, int read) | |
2247 | { | |
2248 | struct vb2_fileio_data *fileio; | |
2249 | struct vb2_fileio_buf *buf; | |
2250 | int ret, index; | |
2251 | ||
08b99e26 | 2252 | dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n", |
b25748fe MS |
2253 | read ? "read" : "write", (long)*ppos, count, |
2254 | nonblock ? "non" : ""); | |
2255 | ||
2256 | if (!data) | |
2257 | return -EINVAL; | |
2258 | ||
2259 | /* | |
2260 | * Initialize emulator on first call. | |
2261 | */ | |
2262 | if (!q->fileio) { | |
2263 | ret = __vb2_init_fileio(q, read); | |
2264 | dprintk(3, "file io: vb2_init_fileio result: %d\n", ret); | |
2265 | if (ret) | |
2266 | return ret; | |
2267 | } | |
2268 | fileio = q->fileio; | |
2269 | ||
2270 | /* | |
2271 | * Hack fileio context to enable direct calls to vb2 ioctl interface. | |
2272 | * The pointer will be restored before returning from this function. | |
2273 | */ | |
2274 | q->fileio = NULL; | |
2275 | ||
2276 | index = fileio->index; | |
2277 | buf = &fileio->bufs[index]; | |
2278 | ||
2279 | /* | |
2280 | * Check if we need to dequeue the buffer. | |
2281 | */ | |
2282 | if (buf->queued) { | |
2283 | struct vb2_buffer *vb; | |
2284 | ||
2285 | /* | |
2286 | * Call vb2_dqbuf to get buffer back. | |
2287 | */ | |
2288 | memset(&fileio->b, 0, sizeof(fileio->b)); | |
2289 | fileio->b.type = q->type; | |
2290 | fileio->b.memory = q->memory; | |
2291 | fileio->b.index = index; | |
2292 | ret = vb2_dqbuf(q, &fileio->b, nonblock); | |
2293 | dprintk(5, "file io: vb2_dqbuf result: %d\n", ret); | |
2294 | if (ret) | |
2295 | goto end; | |
2296 | fileio->dq_count += 1; | |
2297 | ||
2298 | /* | |
2299 | * Get number of bytes filled by the driver | |
2300 | */ | |
2301 | vb = q->bufs[index]; | |
2302 | buf->size = vb2_get_plane_payload(vb, 0); | |
2303 | buf->queued = 0; | |
2304 | } | |
2305 | ||
2306 | /* | |
2307 | * Limit count on last few bytes of the buffer. | |
2308 | */ | |
2309 | if (buf->pos + count > buf->size) { | |
2310 | count = buf->size - buf->pos; | |
08b99e26 | 2311 | dprintk(5, "reducing read count: %zd\n", count); |
b25748fe MS |
2312 | } |
2313 | ||
2314 | /* | |
2315 | * Transfer data to userspace. | |
2316 | */ | |
08b99e26 | 2317 | dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n", |
b25748fe MS |
2318 | count, index, buf->pos); |
2319 | if (read) | |
2320 | ret = copy_to_user(data, buf->vaddr + buf->pos, count); | |
2321 | else | |
2322 | ret = copy_from_user(buf->vaddr + buf->pos, data, count); | |
2323 | if (ret) { | |
2324 | dprintk(3, "file io: error copying data\n"); | |
2325 | ret = -EFAULT; | |
2326 | goto end; | |
2327 | } | |
2328 | ||
2329 | /* | |
2330 | * Update counters. | |
2331 | */ | |
2332 | buf->pos += count; | |
2333 | *ppos += count; | |
2334 | ||
2335 | /* | |
2336 | * Queue next buffer if required. | |
2337 | */ | |
2338 | if (buf->pos == buf->size || | |
2339 | (!read && (fileio->flags & VB2_FILEIO_WRITE_IMMEDIATELY))) { | |
2340 | /* | |
2341 | * Check if this is the last buffer to read. | |
2342 | */ | |
2343 | if (read && (fileio->flags & VB2_FILEIO_READ_ONCE) && | |
2344 | fileio->dq_count == 1) { | |
2345 | dprintk(3, "file io: read limit reached\n"); | |
2346 | /* | |
2347 | * Restore fileio pointer and release the context. | |
2348 | */ | |
2349 | q->fileio = fileio; | |
2350 | return __vb2_cleanup_fileio(q); | |
2351 | } | |
2352 | ||
2353 | /* | |
2354 | * Call vb2_qbuf and give buffer to the driver. | |
2355 | */ | |
2356 | memset(&fileio->b, 0, sizeof(fileio->b)); | |
2357 | fileio->b.type = q->type; | |
2358 | fileio->b.memory = q->memory; | |
2359 | fileio->b.index = index; | |
2360 | fileio->b.bytesused = buf->pos; | |
2361 | ret = vb2_qbuf(q, &fileio->b); | |
2362 | dprintk(5, "file io: vb2_dbuf result: %d\n", ret); | |
2363 | if (ret) | |
2364 | goto end; | |
2365 | ||
2366 | /* | |
2367 | * Buffer has been queued, update the status | |
2368 | */ | |
2369 | buf->pos = 0; | |
2370 | buf->queued = 1; | |
2371 | buf->size = q->bufs[0]->v4l2_planes[0].length; | |
2372 | fileio->q_count += 1; | |
2373 | ||
2374 | /* | |
2375 | * Switch to the next buffer | |
2376 | */ | |
2377 | fileio->index = (index + 1) % q->num_buffers; | |
2378 | ||
2379 | /* | |
2380 | * Start streaming if required. | |
2381 | */ | |
2382 | if (!read && !q->streaming) { | |
2383 | ret = vb2_streamon(q, q->type); | |
2384 | if (ret) | |
2385 | goto end; | |
2386 | } | |
2387 | } | |
2388 | ||
2389 | /* | |
2390 | * Return proper number of bytes processed. | |
2391 | */ | |
2392 | if (ret == 0) | |
2393 | ret = count; | |
2394 | end: | |
2395 | /* | |
2396 | * Restore the fileio context and block vb2 ioctl interface. | |
2397 | */ | |
2398 | q->fileio = fileio; | |
2399 | return ret; | |
2400 | } | |
2401 | ||
2402 | size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count, | |
2403 | loff_t *ppos, int nonblocking) | |
2404 | { | |
2405 | return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1); | |
2406 | } | |
2407 | EXPORT_SYMBOL_GPL(vb2_read); | |
2408 | ||
2409 | size_t vb2_write(struct vb2_queue *q, char __user *data, size_t count, | |
2410 | loff_t *ppos, int nonblocking) | |
2411 | { | |
2412 | return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 0); | |
2413 | } | |
2414 | EXPORT_SYMBOL_GPL(vb2_write); | |
2415 | ||
4c1ffcaa HV |
2416 | |
2417 | /* | |
2418 | * The following functions are not part of the vb2 core API, but are helper | |
2419 | * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations | |
2420 | * and struct vb2_ops. | |
2421 | * They contain boilerplate code that most if not all drivers have to do | |
2422 | * and so they simplify the driver code. | |
2423 | */ | |
2424 | ||
2425 | /* The queue is busy if there is a owner and you are not that owner. */ | |
2426 | static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file) | |
2427 | { | |
2428 | return vdev->queue->owner && vdev->queue->owner != file->private_data; | |
2429 | } | |
2430 | ||
2431 | /* vb2 ioctl helpers */ | |
2432 | ||
2433 | int vb2_ioctl_reqbufs(struct file *file, void *priv, | |
2434 | struct v4l2_requestbuffers *p) | |
2435 | { | |
2436 | struct video_device *vdev = video_devdata(file); | |
2437 | int res = __verify_memory_type(vdev->queue, p->memory, p->type); | |
2438 | ||
2439 | if (res) | |
2440 | return res; | |
2441 | if (vb2_queue_is_busy(vdev, file)) | |
2442 | return -EBUSY; | |
2443 | res = __reqbufs(vdev->queue, p); | |
2444 | /* If count == 0, then the owner has released all buffers and he | |
2445 | is no longer owner of the queue. Otherwise we have a new owner. */ | |
2446 | if (res == 0) | |
2447 | vdev->queue->owner = p->count ? file->private_data : NULL; | |
2448 | return res; | |
2449 | } | |
2450 | EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs); | |
2451 | ||
2452 | int vb2_ioctl_create_bufs(struct file *file, void *priv, | |
2453 | struct v4l2_create_buffers *p) | |
2454 | { | |
2455 | struct video_device *vdev = video_devdata(file); | |
2456 | int res = __verify_memory_type(vdev->queue, p->memory, p->format.type); | |
2457 | ||
2458 | p->index = vdev->queue->num_buffers; | |
2459 | /* If count == 0, then just check if memory and type are valid. | |
2460 | Any -EBUSY result from __verify_memory_type can be mapped to 0. */ | |
2461 | if (p->count == 0) | |
2462 | return res != -EBUSY ? res : 0; | |
2463 | if (res) | |
2464 | return res; | |
2465 | if (vb2_queue_is_busy(vdev, file)) | |
2466 | return -EBUSY; | |
2467 | res = __create_bufs(vdev->queue, p); | |
2468 | if (res == 0) | |
2469 | vdev->queue->owner = file->private_data; | |
2470 | return res; | |
2471 | } | |
2472 | EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs); | |
2473 | ||
2474 | int vb2_ioctl_prepare_buf(struct file *file, void *priv, | |
2475 | struct v4l2_buffer *p) | |
2476 | { | |
2477 | struct video_device *vdev = video_devdata(file); | |
2478 | ||
2479 | if (vb2_queue_is_busy(vdev, file)) | |
2480 | return -EBUSY; | |
2481 | return vb2_prepare_buf(vdev->queue, p); | |
2482 | } | |
2483 | EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf); | |
2484 | ||
2485 | int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p) | |
2486 | { | |
2487 | struct video_device *vdev = video_devdata(file); | |
2488 | ||
2489 | /* No need to call vb2_queue_is_busy(), anyone can query buffers. */ | |
2490 | return vb2_querybuf(vdev->queue, p); | |
2491 | } | |
2492 | EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf); | |
2493 | ||
2494 | int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p) | |
2495 | { | |
2496 | struct video_device *vdev = video_devdata(file); | |
2497 | ||
2498 | if (vb2_queue_is_busy(vdev, file)) | |
2499 | return -EBUSY; | |
2500 | return vb2_qbuf(vdev->queue, p); | |
2501 | } | |
2502 | EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf); | |
2503 | ||
2504 | int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p) | |
2505 | { | |
2506 | struct video_device *vdev = video_devdata(file); | |
2507 | ||
2508 | if (vb2_queue_is_busy(vdev, file)) | |
2509 | return -EBUSY; | |
2510 | return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK); | |
2511 | } | |
2512 | EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf); | |
2513 | ||
2514 | int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i) | |
2515 | { | |
2516 | struct video_device *vdev = video_devdata(file); | |
2517 | ||
2518 | if (vb2_queue_is_busy(vdev, file)) | |
2519 | return -EBUSY; | |
2520 | return vb2_streamon(vdev->queue, i); | |
2521 | } | |
2522 | EXPORT_SYMBOL_GPL(vb2_ioctl_streamon); | |
2523 | ||
2524 | int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i) | |
2525 | { | |
2526 | struct video_device *vdev = video_devdata(file); | |
2527 | ||
2528 | if (vb2_queue_is_busy(vdev, file)) | |
2529 | return -EBUSY; | |
2530 | return vb2_streamoff(vdev->queue, i); | |
2531 | } | |
2532 | EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff); | |
2533 | ||
83ae7c5a TS |
2534 | int vb2_ioctl_expbuf(struct file *file, void *priv, struct v4l2_exportbuffer *p) |
2535 | { | |
2536 | struct video_device *vdev = video_devdata(file); | |
2537 | ||
2538 | if (vb2_queue_is_busy(vdev, file)) | |
2539 | return -EBUSY; | |
2540 | return vb2_expbuf(vdev->queue, p); | |
2541 | } | |
2542 | EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf); | |
2543 | ||
4c1ffcaa HV |
2544 | /* v4l2_file_operations helpers */ |
2545 | ||
2546 | int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma) | |
2547 | { | |
2548 | struct video_device *vdev = video_devdata(file); | |
2549 | ||
2550 | return vb2_mmap(vdev->queue, vma); | |
2551 | } | |
2552 | EXPORT_SYMBOL_GPL(vb2_fop_mmap); | |
2553 | ||
2554 | int vb2_fop_release(struct file *file) | |
2555 | { | |
2556 | struct video_device *vdev = video_devdata(file); | |
2557 | ||
2558 | if (file->private_data == vdev->queue->owner) { | |
2559 | vb2_queue_release(vdev->queue); | |
2560 | vdev->queue->owner = NULL; | |
2561 | } | |
2562 | return v4l2_fh_release(file); | |
2563 | } | |
2564 | EXPORT_SYMBOL_GPL(vb2_fop_release); | |
2565 | ||
2566 | ssize_t vb2_fop_write(struct file *file, char __user *buf, | |
2567 | size_t count, loff_t *ppos) | |
2568 | { | |
2569 | struct video_device *vdev = video_devdata(file); | |
2570 | struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock; | |
4c1ffcaa HV |
2571 | int err = -EBUSY; |
2572 | ||
cf533735 | 2573 | if (lock && mutex_lock_interruptible(lock)) |
4c1ffcaa HV |
2574 | return -ERESTARTSYS; |
2575 | if (vb2_queue_is_busy(vdev, file)) | |
2576 | goto exit; | |
2577 | err = vb2_write(vdev->queue, buf, count, ppos, | |
2578 | file->f_flags & O_NONBLOCK); | |
8c82c75c | 2579 | if (vdev->queue->fileio) |
4c1ffcaa HV |
2580 | vdev->queue->owner = file->private_data; |
2581 | exit: | |
cf533735 | 2582 | if (lock) |
4c1ffcaa HV |
2583 | mutex_unlock(lock); |
2584 | return err; | |
2585 | } | |
2586 | EXPORT_SYMBOL_GPL(vb2_fop_write); | |
2587 | ||
2588 | ssize_t vb2_fop_read(struct file *file, char __user *buf, | |
2589 | size_t count, loff_t *ppos) | |
2590 | { | |
2591 | struct video_device *vdev = video_devdata(file); | |
2592 | struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock; | |
4c1ffcaa HV |
2593 | int err = -EBUSY; |
2594 | ||
cf533735 | 2595 | if (lock && mutex_lock_interruptible(lock)) |
4c1ffcaa HV |
2596 | return -ERESTARTSYS; |
2597 | if (vb2_queue_is_busy(vdev, file)) | |
2598 | goto exit; | |
2599 | err = vb2_read(vdev->queue, buf, count, ppos, | |
2600 | file->f_flags & O_NONBLOCK); | |
8c82c75c | 2601 | if (vdev->queue->fileio) |
4c1ffcaa HV |
2602 | vdev->queue->owner = file->private_data; |
2603 | exit: | |
cf533735 | 2604 | if (lock) |
4c1ffcaa HV |
2605 | mutex_unlock(lock); |
2606 | return err; | |
2607 | } | |
2608 | EXPORT_SYMBOL_GPL(vb2_fop_read); | |
2609 | ||
2610 | unsigned int vb2_fop_poll(struct file *file, poll_table *wait) | |
2611 | { | |
2612 | struct video_device *vdev = video_devdata(file); | |
2613 | struct vb2_queue *q = vdev->queue; | |
2614 | struct mutex *lock = q->lock ? q->lock : vdev->lock; | |
2615 | unsigned long req_events = poll_requested_events(wait); | |
2616 | unsigned res; | |
2617 | void *fileio; | |
4c1ffcaa HV |
2618 | bool must_lock = false; |
2619 | ||
2620 | /* Try to be smart: only lock if polling might start fileio, | |
2621 | otherwise locking will only introduce unwanted delays. */ | |
2622 | if (q->num_buffers == 0 && q->fileio == NULL) { | |
2623 | if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) && | |
2624 | (req_events & (POLLIN | POLLRDNORM))) | |
2625 | must_lock = true; | |
2626 | else if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) && | |
2627 | (req_events & (POLLOUT | POLLWRNORM))) | |
2628 | must_lock = true; | |
2629 | } | |
2630 | ||
2631 | /* If locking is needed, but this helper doesn't know how, then you | |
2632 | shouldn't be using this helper but you should write your own. */ | |
cf533735 | 2633 | WARN_ON(must_lock && !lock); |
4c1ffcaa | 2634 | |
cf533735 | 2635 | if (must_lock && lock && mutex_lock_interruptible(lock)) |
4c1ffcaa HV |
2636 | return POLLERR; |
2637 | ||
2638 | fileio = q->fileio; | |
2639 | ||
2640 | res = vb2_poll(vdev->queue, file, wait); | |
2641 | ||
2642 | /* If fileio was started, then we have a new queue owner. */ | |
2643 | if (must_lock && !fileio && q->fileio) | |
2644 | q->owner = file->private_data; | |
cf533735 | 2645 | if (must_lock && lock) |
4c1ffcaa HV |
2646 | mutex_unlock(lock); |
2647 | return res; | |
2648 | } | |
2649 | EXPORT_SYMBOL_GPL(vb2_fop_poll); | |
2650 | ||
2651 | #ifndef CONFIG_MMU | |
2652 | unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr, | |
2653 | unsigned long len, unsigned long pgoff, unsigned long flags) | |
2654 | { | |
2655 | struct video_device *vdev = video_devdata(file); | |
2656 | ||
2657 | return vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags); | |
2658 | } | |
2659 | EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area); | |
2660 | #endif | |
2661 | ||
2662 | /* vb2_ops helpers. Only use if vq->lock is non-NULL. */ | |
2663 | ||
2664 | void vb2_ops_wait_prepare(struct vb2_queue *vq) | |
2665 | { | |
2666 | mutex_unlock(vq->lock); | |
2667 | } | |
2668 | EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare); | |
2669 | ||
2670 | void vb2_ops_wait_finish(struct vb2_queue *vq) | |
2671 | { | |
2672 | mutex_lock(vq->lock); | |
2673 | } | |
2674 | EXPORT_SYMBOL_GPL(vb2_ops_wait_finish); | |
2675 | ||
e23ccc0a | 2676 | MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2"); |
95072084 | 2677 | MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski"); |
e23ccc0a | 2678 | MODULE_LICENSE("GPL"); |