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