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