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