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