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