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