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