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