diff options
Diffstat (limited to 'drivers')
-rw-r--r-- | drivers/media/video/Kconfig | 3 | ||||
-rw-r--r-- | drivers/media/video/Makefile | 2 | ||||
-rw-r--r-- | drivers/media/video/videobuf2-core.c | 1405 |
3 files changed, 1410 insertions, 0 deletions
diff --git a/drivers/media/video/Kconfig b/drivers/media/video/Kconfig index aa021600e9df..a4a6aa703c26 100644 --- a/drivers/media/video/Kconfig +++ b/drivers/media/video/Kconfig | |||
@@ -44,6 +44,9 @@ config V4L2_MEM2MEM_DEV | |||
44 | tristate | 44 | tristate |
45 | depends on VIDEOBUF_GEN | 45 | depends on VIDEOBUF_GEN |
46 | 46 | ||
47 | config VIDEOBUF2_CORE | ||
48 | tristate | ||
49 | |||
47 | # | 50 | # |
48 | # Multimedia Video device configuration | 51 | # Multimedia Video device configuration |
49 | # | 52 | # |
diff --git a/drivers/media/video/Makefile b/drivers/media/video/Makefile index a509d317e258..6a1feba87869 100644 --- a/drivers/media/video/Makefile +++ b/drivers/media/video/Makefile | |||
@@ -111,6 +111,8 @@ obj-$(CONFIG_VIDEOBUF_VMALLOC) += videobuf-vmalloc.o | |||
111 | obj-$(CONFIG_VIDEOBUF_DVB) += videobuf-dvb.o | 111 | obj-$(CONFIG_VIDEOBUF_DVB) += videobuf-dvb.o |
112 | obj-$(CONFIG_VIDEO_BTCX) += btcx-risc.o | 112 | obj-$(CONFIG_VIDEO_BTCX) += btcx-risc.o |
113 | 113 | ||
114 | obj-$(CONFIG_VIDEOBUF2_CORE) += videobuf2-core.o | ||
115 | |||
114 | obj-$(CONFIG_V4L2_MEM2MEM_DEV) += v4l2-mem2mem.o | 116 | obj-$(CONFIG_V4L2_MEM2MEM_DEV) += v4l2-mem2mem.o |
115 | 117 | ||
116 | obj-$(CONFIG_VIDEO_M32R_AR_M64278) += arv.o | 118 | obj-$(CONFIG_VIDEO_M32R_AR_M64278) += arv.o |
diff --git a/drivers/media/video/videobuf2-core.c b/drivers/media/video/videobuf2-core.c new file mode 100644 index 000000000000..b856bd105d77 --- /dev/null +++ b/drivers/media/video/videobuf2-core.c | |||
@@ -0,0 +1,1405 @@ | |||
1 | /* | ||
2 | * videobuf2-core.c - V4L2 driver helper framework | ||
3 | * | ||
4 | * Copyright (C) 2010 Samsung Electronics | ||
5 | * | ||
6 | * Author: Pawel Osciak <p.osciak@samsung.com> | ||
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 | |||
22 | #include <media/videobuf2-core.h> | ||
23 | |||
24 | static int debug; | ||
25 | module_param(debug, int, 0644); | ||
26 | |||
27 | #define dprintk(level, fmt, arg...) \ | ||
28 | do { \ | ||
29 | if (debug >= level) \ | ||
30 | printk(KERN_DEBUG "vb2: " fmt, ## arg); \ | ||
31 | } while (0) | ||
32 | |||
33 | #define call_memop(q, plane, op, args...) \ | ||
34 | (((q)->mem_ops->op) ? \ | ||
35 | ((q)->mem_ops->op(args)) : 0) | ||
36 | |||
37 | #define call_qop(q, op, args...) \ | ||
38 | (((q)->ops->op) ? ((q)->ops->op(args)) : 0) | ||
39 | |||
40 | /** | ||
41 | * __vb2_buf_mem_alloc() - allocate video memory for the given buffer | ||
42 | */ | ||
43 | static int __vb2_buf_mem_alloc(struct vb2_buffer *vb, | ||
44 | unsigned long *plane_sizes) | ||
45 | { | ||
46 | struct vb2_queue *q = vb->vb2_queue; | ||
47 | void *mem_priv; | ||
48 | int plane; | ||
49 | |||
50 | /* Allocate memory for all planes in this buffer */ | ||
51 | for (plane = 0; plane < vb->num_planes; ++plane) { | ||
52 | mem_priv = call_memop(q, plane, alloc, q->alloc_ctx[plane], | ||
53 | plane_sizes[plane]); | ||
54 | if (!mem_priv) | ||
55 | goto free; | ||
56 | |||
57 | /* Associate allocator private data with this plane */ | ||
58 | vb->planes[plane].mem_priv = mem_priv; | ||
59 | vb->v4l2_planes[plane].length = plane_sizes[plane]; | ||
60 | } | ||
61 | |||
62 | return 0; | ||
63 | free: | ||
64 | /* Free already allocated memory if one of the allocations failed */ | ||
65 | for (; plane > 0; --plane) | ||
66 | call_memop(q, plane, put, vb->planes[plane - 1].mem_priv); | ||
67 | |||
68 | return -ENOMEM; | ||
69 | } | ||
70 | |||
71 | /** | ||
72 | * __vb2_buf_mem_free() - free memory of the given buffer | ||
73 | */ | ||
74 | static void __vb2_buf_mem_free(struct vb2_buffer *vb) | ||
75 | { | ||
76 | struct vb2_queue *q = vb->vb2_queue; | ||
77 | unsigned int plane; | ||
78 | |||
79 | for (plane = 0; plane < vb->num_planes; ++plane) { | ||
80 | call_memop(q, plane, put, vb->planes[plane].mem_priv); | ||
81 | vb->planes[plane].mem_priv = NULL; | ||
82 | dprintk(3, "Freed plane %d of buffer %d\n", | ||
83 | plane, vb->v4l2_buf.index); | ||
84 | } | ||
85 | } | ||
86 | |||
87 | /** | ||
88 | * __vb2_buf_userptr_put() - release userspace memory associated with | ||
89 | * a USERPTR buffer | ||
90 | */ | ||
91 | static void __vb2_buf_userptr_put(struct vb2_buffer *vb) | ||
92 | { | ||
93 | struct vb2_queue *q = vb->vb2_queue; | ||
94 | unsigned int plane; | ||
95 | |||
96 | for (plane = 0; plane < vb->num_planes; ++plane) { | ||
97 | void *mem_priv = vb->planes[plane].mem_priv; | ||
98 | |||
99 | if (mem_priv) { | ||
100 | call_memop(q, plane, put_userptr, mem_priv); | ||
101 | vb->planes[plane].mem_priv = NULL; | ||
102 | } | ||
103 | } | ||
104 | } | ||
105 | |||
106 | /** | ||
107 | * __setup_offsets() - setup unique offsets ("cookies") for every plane in | ||
108 | * every buffer on the queue | ||
109 | */ | ||
110 | static void __setup_offsets(struct vb2_queue *q) | ||
111 | { | ||
112 | unsigned int buffer, plane; | ||
113 | struct vb2_buffer *vb; | ||
114 | unsigned long off = 0; | ||
115 | |||
116 | for (buffer = 0; buffer < q->num_buffers; ++buffer) { | ||
117 | vb = q->bufs[buffer]; | ||
118 | if (!vb) | ||
119 | continue; | ||
120 | |||
121 | for (plane = 0; plane < vb->num_planes; ++plane) { | ||
122 | vb->v4l2_planes[plane].m.mem_offset = off; | ||
123 | |||
124 | dprintk(3, "Buffer %d, plane %d offset 0x%08lx\n", | ||
125 | buffer, plane, off); | ||
126 | |||
127 | off += vb->v4l2_planes[plane].length; | ||
128 | off = PAGE_ALIGN(off); | ||
129 | } | ||
130 | } | ||
131 | } | ||
132 | |||
133 | /** | ||
134 | * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type) | ||
135 | * video buffer memory for all buffers/planes on the queue and initializes the | ||
136 | * queue | ||
137 | * | ||
138 | * Returns the number of buffers successfully allocated. | ||
139 | */ | ||
140 | static int __vb2_queue_alloc(struct vb2_queue *q, enum v4l2_memory memory, | ||
141 | unsigned int num_buffers, unsigned int num_planes, | ||
142 | unsigned long plane_sizes[]) | ||
143 | { | ||
144 | unsigned int buffer; | ||
145 | struct vb2_buffer *vb; | ||
146 | int ret; | ||
147 | |||
148 | for (buffer = 0; buffer < num_buffers; ++buffer) { | ||
149 | /* Allocate videobuf buffer structures */ | ||
150 | vb = kzalloc(q->buf_struct_size, GFP_KERNEL); | ||
151 | if (!vb) { | ||
152 | dprintk(1, "Memory alloc for buffer struct failed\n"); | ||
153 | break; | ||
154 | } | ||
155 | |||
156 | /* Length stores number of planes for multiplanar buffers */ | ||
157 | if (V4L2_TYPE_IS_MULTIPLANAR(q->type)) | ||
158 | vb->v4l2_buf.length = num_planes; | ||
159 | |||
160 | vb->state = VB2_BUF_STATE_DEQUEUED; | ||
161 | vb->vb2_queue = q; | ||
162 | vb->num_planes = num_planes; | ||
163 | vb->v4l2_buf.index = buffer; | ||
164 | vb->v4l2_buf.type = q->type; | ||
165 | vb->v4l2_buf.memory = memory; | ||
166 | |||
167 | /* Allocate video buffer memory for the MMAP type */ | ||
168 | if (memory == V4L2_MEMORY_MMAP) { | ||
169 | ret = __vb2_buf_mem_alloc(vb, plane_sizes); | ||
170 | if (ret) { | ||
171 | dprintk(1, "Failed allocating memory for " | ||
172 | "buffer %d\n", buffer); | ||
173 | kfree(vb); | ||
174 | break; | ||
175 | } | ||
176 | /* | ||
177 | * Call the driver-provided buffer initialization | ||
178 | * callback, if given. An error in initialization | ||
179 | * results in queue setup failure. | ||
180 | */ | ||
181 | ret = call_qop(q, buf_init, vb); | ||
182 | if (ret) { | ||
183 | dprintk(1, "Buffer %d %p initialization" | ||
184 | " failed\n", buffer, vb); | ||
185 | __vb2_buf_mem_free(vb); | ||
186 | kfree(vb); | ||
187 | break; | ||
188 | } | ||
189 | } | ||
190 | |||
191 | q->bufs[buffer] = vb; | ||
192 | } | ||
193 | |||
194 | q->num_buffers = buffer; | ||
195 | |||
196 | __setup_offsets(q); | ||
197 | |||
198 | dprintk(1, "Allocated %d buffers, %d plane(s) each\n", | ||
199 | q->num_buffers, num_planes); | ||
200 | |||
201 | return buffer; | ||
202 | } | ||
203 | |||
204 | /** | ||
205 | * __vb2_free_mem() - release all video buffer memory for a given queue | ||
206 | */ | ||
207 | static void __vb2_free_mem(struct vb2_queue *q) | ||
208 | { | ||
209 | unsigned int buffer; | ||
210 | struct vb2_buffer *vb; | ||
211 | |||
212 | for (buffer = 0; buffer < q->num_buffers; ++buffer) { | ||
213 | vb = q->bufs[buffer]; | ||
214 | if (!vb) | ||
215 | continue; | ||
216 | |||
217 | /* Free MMAP buffers or release USERPTR buffers */ | ||
218 | if (q->memory == V4L2_MEMORY_MMAP) | ||
219 | __vb2_buf_mem_free(vb); | ||
220 | else | ||
221 | __vb2_buf_userptr_put(vb); | ||
222 | } | ||
223 | } | ||
224 | |||
225 | /** | ||
226 | * __vb2_queue_free() - free the queue - video memory and related information | ||
227 | * and return the queue to an uninitialized state. Might be called even if the | ||
228 | * queue has already been freed. | ||
229 | */ | ||
230 | static int __vb2_queue_free(struct vb2_queue *q) | ||
231 | { | ||
232 | unsigned int buffer; | ||
233 | |||
234 | /* Call driver-provided cleanup function for each buffer, if provided */ | ||
235 | if (q->ops->buf_cleanup) { | ||
236 | for (buffer = 0; buffer < q->num_buffers; ++buffer) { | ||
237 | if (NULL == q->bufs[buffer]) | ||
238 | continue; | ||
239 | q->ops->buf_cleanup(q->bufs[buffer]); | ||
240 | } | ||
241 | } | ||
242 | |||
243 | /* Release video buffer memory */ | ||
244 | __vb2_free_mem(q); | ||
245 | |||
246 | /* Free videobuf buffers */ | ||
247 | for (buffer = 0; buffer < q->num_buffers; ++buffer) { | ||
248 | kfree(q->bufs[buffer]); | ||
249 | q->bufs[buffer] = NULL; | ||
250 | } | ||
251 | |||
252 | q->num_buffers = 0; | ||
253 | q->memory = 0; | ||
254 | |||
255 | return 0; | ||
256 | } | ||
257 | |||
258 | /** | ||
259 | * __verify_planes_array() - verify that the planes array passed in struct | ||
260 | * v4l2_buffer from userspace can be safely used | ||
261 | */ | ||
262 | static int __verify_planes_array(struct vb2_buffer *vb, struct v4l2_buffer *b) | ||
263 | { | ||
264 | /* Is memory for copying plane information present? */ | ||
265 | if (NULL == b->m.planes) { | ||
266 | dprintk(1, "Multi-planar buffer passed but " | ||
267 | "planes array not provided\n"); | ||
268 | return -EINVAL; | ||
269 | } | ||
270 | |||
271 | if (b->length < vb->num_planes || b->length > VIDEO_MAX_PLANES) { | ||
272 | dprintk(1, "Incorrect planes array length, " | ||
273 | "expected %d, got %d\n", vb->num_planes, b->length); | ||
274 | return -EINVAL; | ||
275 | } | ||
276 | |||
277 | return 0; | ||
278 | } | ||
279 | |||
280 | /** | ||
281 | * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be | ||
282 | * returned to userspace | ||
283 | */ | ||
284 | static int __fill_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b) | ||
285 | { | ||
286 | struct vb2_queue *q = vb->vb2_queue; | ||
287 | int ret = 0; | ||
288 | |||
289 | /* Copy back data such as timestamp, input, etc. */ | ||
290 | memcpy(b, &vb->v4l2_buf, offsetof(struct v4l2_buffer, m)); | ||
291 | b->input = vb->v4l2_buf.input; | ||
292 | b->reserved = vb->v4l2_buf.reserved; | ||
293 | |||
294 | if (V4L2_TYPE_IS_MULTIPLANAR(q->type)) { | ||
295 | ret = __verify_planes_array(vb, b); | ||
296 | if (ret) | ||
297 | return ret; | ||
298 | |||
299 | /* | ||
300 | * Fill in plane-related data if userspace provided an array | ||
301 | * for it. The memory and size is verified above. | ||
302 | */ | ||
303 | memcpy(b->m.planes, vb->v4l2_planes, | ||
304 | b->length * sizeof(struct v4l2_plane)); | ||
305 | } else { | ||
306 | /* | ||
307 | * We use length and offset in v4l2_planes array even for | ||
308 | * single-planar buffers, but userspace does not. | ||
309 | */ | ||
310 | b->length = vb->v4l2_planes[0].length; | ||
311 | b->bytesused = vb->v4l2_planes[0].bytesused; | ||
312 | if (q->memory == V4L2_MEMORY_MMAP) | ||
313 | b->m.offset = vb->v4l2_planes[0].m.mem_offset; | ||
314 | else if (q->memory == V4L2_MEMORY_USERPTR) | ||
315 | b->m.userptr = vb->v4l2_planes[0].m.userptr; | ||
316 | } | ||
317 | |||
318 | b->flags = 0; | ||
319 | |||
320 | switch (vb->state) { | ||
321 | case VB2_BUF_STATE_QUEUED: | ||
322 | case VB2_BUF_STATE_ACTIVE: | ||
323 | b->flags |= V4L2_BUF_FLAG_QUEUED; | ||
324 | break; | ||
325 | case VB2_BUF_STATE_ERROR: | ||
326 | b->flags |= V4L2_BUF_FLAG_ERROR; | ||
327 | /* fall through */ | ||
328 | case VB2_BUF_STATE_DONE: | ||
329 | b->flags |= V4L2_BUF_FLAG_DONE; | ||
330 | break; | ||
331 | case VB2_BUF_STATE_DEQUEUED: | ||
332 | /* nothing */ | ||
333 | break; | ||
334 | } | ||
335 | |||
336 | if (vb->num_planes_mapped == vb->num_planes) | ||
337 | b->flags |= V4L2_BUF_FLAG_MAPPED; | ||
338 | |||
339 | return ret; | ||
340 | } | ||
341 | |||
342 | /** | ||
343 | * vb2_querybuf() - query video buffer information | ||
344 | * @q: videobuf queue | ||
345 | * @b: buffer struct passed from userspace to vidioc_querybuf handler | ||
346 | * in driver | ||
347 | * | ||
348 | * Should be called from vidioc_querybuf ioctl handler in driver. | ||
349 | * This function will verify the passed v4l2_buffer structure and fill the | ||
350 | * relevant information for the userspace. | ||
351 | * | ||
352 | * The return values from this function are intended to be directly returned | ||
353 | * from vidioc_querybuf handler in driver. | ||
354 | */ | ||
355 | int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b) | ||
356 | { | ||
357 | struct vb2_buffer *vb; | ||
358 | |||
359 | if (b->type != q->type) { | ||
360 | dprintk(1, "querybuf: wrong buffer type\n"); | ||
361 | return -EINVAL; | ||
362 | } | ||
363 | |||
364 | if (b->index >= q->num_buffers) { | ||
365 | dprintk(1, "querybuf: buffer index out of range\n"); | ||
366 | return -EINVAL; | ||
367 | } | ||
368 | vb = q->bufs[b->index]; | ||
369 | |||
370 | return __fill_v4l2_buffer(vb, b); | ||
371 | } | ||
372 | EXPORT_SYMBOL(vb2_querybuf); | ||
373 | |||
374 | /** | ||
375 | * __verify_userptr_ops() - verify that all memory operations required for | ||
376 | * USERPTR queue type have been provided | ||
377 | */ | ||
378 | static int __verify_userptr_ops(struct vb2_queue *q) | ||
379 | { | ||
380 | if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr || | ||
381 | !q->mem_ops->put_userptr) | ||
382 | return -EINVAL; | ||
383 | |||
384 | return 0; | ||
385 | } | ||
386 | |||
387 | /** | ||
388 | * __verify_mmap_ops() - verify that all memory operations required for | ||
389 | * MMAP queue type have been provided | ||
390 | */ | ||
391 | static int __verify_mmap_ops(struct vb2_queue *q) | ||
392 | { | ||
393 | if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc || | ||
394 | !q->mem_ops->put || !q->mem_ops->mmap) | ||
395 | return -EINVAL; | ||
396 | |||
397 | return 0; | ||
398 | } | ||
399 | |||
400 | /** | ||
401 | * __buffers_in_use() - return true if any buffers on the queue are in use and | ||
402 | * the queue cannot be freed (by the means of REQBUFS(0)) call | ||
403 | */ | ||
404 | static bool __buffers_in_use(struct vb2_queue *q) | ||
405 | { | ||
406 | unsigned int buffer, plane; | ||
407 | struct vb2_buffer *vb; | ||
408 | |||
409 | for (buffer = 0; buffer < q->num_buffers; ++buffer) { | ||
410 | vb = q->bufs[buffer]; | ||
411 | for (plane = 0; plane < vb->num_planes; ++plane) { | ||
412 | /* | ||
413 | * If num_users() has not been provided, call_memop | ||
414 | * will return 0, apparently nobody cares about this | ||
415 | * case anyway. If num_users() returns more than 1, | ||
416 | * we are not the only user of the plane's memory. | ||
417 | */ | ||
418 | if (call_memop(q, plane, num_users, | ||
419 | vb->planes[plane].mem_priv) > 1) | ||
420 | return true; | ||
421 | } | ||
422 | } | ||
423 | |||
424 | return false; | ||
425 | } | ||
426 | |||
427 | /** | ||
428 | * vb2_reqbufs() - Initiate streaming | ||
429 | * @q: videobuf2 queue | ||
430 | * @req: struct passed from userspace to vidioc_reqbufs handler in driver | ||
431 | * | ||
432 | * Should be called from vidioc_reqbufs ioctl handler of a driver. | ||
433 | * This function: | ||
434 | * 1) verifies streaming parameters passed from the userspace, | ||
435 | * 2) sets up the queue, | ||
436 | * 3) negotiates number of buffers and planes per buffer with the driver | ||
437 | * to be used during streaming, | ||
438 | * 4) allocates internal buffer structures (struct vb2_buffer), according to | ||
439 | * the agreed parameters, | ||
440 | * 5) for MMAP memory type, allocates actual video memory, using the | ||
441 | * memory handling/allocation routines provided during queue initialization | ||
442 | * | ||
443 | * If req->count is 0, all the memory will be freed instead. | ||
444 | * If the queue has been allocated previously (by a previous vb2_reqbufs) call | ||
445 | * and the queue is not busy, memory will be reallocated. | ||
446 | * | ||
447 | * The return values from this function are intended to be directly returned | ||
448 | * from vidioc_reqbufs handler in driver. | ||
449 | */ | ||
450 | int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req) | ||
451 | { | ||
452 | unsigned int num_buffers, num_planes; | ||
453 | unsigned long plane_sizes[VIDEO_MAX_PLANES]; | ||
454 | int ret = 0; | ||
455 | |||
456 | if (req->memory != V4L2_MEMORY_MMAP | ||
457 | && req->memory != V4L2_MEMORY_USERPTR) { | ||
458 | dprintk(1, "reqbufs: unsupported memory type\n"); | ||
459 | return -EINVAL; | ||
460 | } | ||
461 | |||
462 | if (req->type != q->type) { | ||
463 | dprintk(1, "reqbufs: requested type is incorrect\n"); | ||
464 | return -EINVAL; | ||
465 | } | ||
466 | |||
467 | if (q->streaming) { | ||
468 | dprintk(1, "reqbufs: streaming active\n"); | ||
469 | return -EBUSY; | ||
470 | } | ||
471 | |||
472 | /* | ||
473 | * Make sure all the required memory ops for given memory type | ||
474 | * are available. | ||
475 | */ | ||
476 | if (req->memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) { | ||
477 | dprintk(1, "reqbufs: MMAP for current setup unsupported\n"); | ||
478 | return -EINVAL; | ||
479 | } | ||
480 | |||
481 | if (req->memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) { | ||
482 | dprintk(1, "reqbufs: USERPTR for current setup unsupported\n"); | ||
483 | return -EINVAL; | ||
484 | } | ||
485 | |||
486 | if (req->count == 0 || q->num_buffers != 0) { | ||
487 | /* | ||
488 | * We already have buffers allocated, so first check if they | ||
489 | * are not in use and can be freed. | ||
490 | */ | ||
491 | if (q->memory == V4L2_MEMORY_MMAP && __buffers_in_use(q)) { | ||
492 | dprintk(1, "reqbufs: memory in use, cannot free\n"); | ||
493 | return -EBUSY; | ||
494 | } | ||
495 | |||
496 | ret = __vb2_queue_free(q); | ||
497 | if (ret != 0) | ||
498 | return ret; | ||
499 | } | ||
500 | |||
501 | /* | ||
502 | * Make sure the requested values and current defaults are sane. | ||
503 | */ | ||
504 | num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME); | ||
505 | memset(plane_sizes, 0, sizeof(plane_sizes)); | ||
506 | memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx)); | ||
507 | |||
508 | /* | ||
509 | * Ask the driver how many buffers and planes per buffer it requires. | ||
510 | * Driver also sets the size and allocator context for each plane. | ||
511 | */ | ||
512 | ret = call_qop(q, queue_setup, q, &num_buffers, &num_planes, | ||
513 | plane_sizes, q->alloc_ctx); | ||
514 | if (ret) | ||
515 | return ret; | ||
516 | |||
517 | /* Finally, allocate buffers and video memory */ | ||
518 | ret = __vb2_queue_alloc(q, req->memory, num_buffers, num_planes, | ||
519 | plane_sizes); | ||
520 | if (ret < 0) { | ||
521 | dprintk(1, "Memory allocation failed with error: %d\n", ret); | ||
522 | return ret; | ||
523 | } | ||
524 | |||
525 | /* | ||
526 | * Check if driver can handle the allocated number of buffers. | ||
527 | */ | ||
528 | if (ret < num_buffers) { | ||
529 | unsigned int orig_num_buffers; | ||
530 | |||
531 | orig_num_buffers = num_buffers = ret; | ||
532 | ret = call_qop(q, queue_setup, q, &num_buffers, &num_planes, | ||
533 | plane_sizes, q->alloc_ctx); | ||
534 | if (ret) | ||
535 | goto free_mem; | ||
536 | |||
537 | if (orig_num_buffers < num_buffers) { | ||
538 | ret = -ENOMEM; | ||
539 | goto free_mem; | ||
540 | } | ||
541 | |||
542 | /* | ||
543 | * Ok, driver accepted smaller number of buffers. | ||
544 | */ | ||
545 | ret = num_buffers; | ||
546 | } | ||
547 | |||
548 | q->memory = req->memory; | ||
549 | |||
550 | /* | ||
551 | * Return the number of successfully allocated buffers | ||
552 | * to the userspace. | ||
553 | */ | ||
554 | req->count = ret; | ||
555 | |||
556 | return 0; | ||
557 | |||
558 | free_mem: | ||
559 | __vb2_queue_free(q); | ||
560 | return ret; | ||
561 | } | ||
562 | EXPORT_SYMBOL_GPL(vb2_reqbufs); | ||
563 | |||
564 | /** | ||
565 | * vb2_plane_vaddr() - Return a kernel virtual address of a given plane | ||
566 | * @vb: vb2_buffer to which the plane in question belongs to | ||
567 | * @plane_no: plane number for which the address is to be returned | ||
568 | * | ||
569 | * This function returns a kernel virtual address of a given plane if | ||
570 | * such a mapping exist, NULL otherwise. | ||
571 | */ | ||
572 | void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no) | ||
573 | { | ||
574 | struct vb2_queue *q = vb->vb2_queue; | ||
575 | |||
576 | if (plane_no > vb->num_planes) | ||
577 | return NULL; | ||
578 | |||
579 | return call_memop(q, plane_no, vaddr, vb->planes[plane_no].mem_priv); | ||
580 | |||
581 | } | ||
582 | EXPORT_SYMBOL_GPL(vb2_plane_vaddr); | ||
583 | |||
584 | /** | ||
585 | * vb2_plane_cookie() - Return allocator specific cookie for the given plane | ||
586 | * @vb: vb2_buffer to which the plane in question belongs to | ||
587 | * @plane_no: plane number for which the cookie is to be returned | ||
588 | * | ||
589 | * This function returns an allocator specific cookie for a given plane if | ||
590 | * available, NULL otherwise. The allocator should provide some simple static | ||
591 | * inline function, which would convert this cookie to the allocator specific | ||
592 | * type that can be used directly by the driver to access the buffer. This can | ||
593 | * be for example physical address, pointer to scatter list or IOMMU mapping. | ||
594 | */ | ||
595 | void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no) | ||
596 | { | ||
597 | struct vb2_queue *q = vb->vb2_queue; | ||
598 | |||
599 | if (plane_no > vb->num_planes) | ||
600 | return NULL; | ||
601 | |||
602 | return call_memop(q, plane_no, cookie, vb->planes[plane_no].mem_priv); | ||
603 | } | ||
604 | EXPORT_SYMBOL_GPL(vb2_plane_cookie); | ||
605 | |||
606 | /** | ||
607 | * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished | ||
608 | * @vb: vb2_buffer returned from the driver | ||
609 | * @state: either VB2_BUF_STATE_DONE if the operation finished successfully | ||
610 | * or VB2_BUF_STATE_ERROR if the operation finished with an error | ||
611 | * | ||
612 | * This function should be called by the driver after a hardware operation on | ||
613 | * a buffer is finished and the buffer may be returned to userspace. The driver | ||
614 | * cannot use this buffer anymore until it is queued back to it by videobuf | ||
615 | * by the means of buf_queue callback. Only buffers previously queued to the | ||
616 | * driver by buf_queue can be passed to this function. | ||
617 | */ | ||
618 | void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state) | ||
619 | { | ||
620 | struct vb2_queue *q = vb->vb2_queue; | ||
621 | unsigned long flags; | ||
622 | |||
623 | if (vb->state != VB2_BUF_STATE_ACTIVE) | ||
624 | return; | ||
625 | |||
626 | if (state != VB2_BUF_STATE_DONE && state != VB2_BUF_STATE_ERROR) | ||
627 | return; | ||
628 | |||
629 | dprintk(4, "Done processing on buffer %d, state: %d\n", | ||
630 | vb->v4l2_buf.index, vb->state); | ||
631 | |||
632 | /* Add the buffer to the done buffers list */ | ||
633 | spin_lock_irqsave(&q->done_lock, flags); | ||
634 | vb->state = state; | ||
635 | list_add_tail(&vb->done_entry, &q->done_list); | ||
636 | atomic_dec(&q->queued_count); | ||
637 | spin_unlock_irqrestore(&q->done_lock, flags); | ||
638 | |||
639 | /* Inform any processes that may be waiting for buffers */ | ||
640 | wake_up(&q->done_wq); | ||
641 | } | ||
642 | EXPORT_SYMBOL_GPL(vb2_buffer_done); | ||
643 | |||
644 | /** | ||
645 | * __fill_vb2_buffer() - fill a vb2_buffer with information provided in | ||
646 | * a v4l2_buffer by the userspace | ||
647 | */ | ||
648 | static int __fill_vb2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b, | ||
649 | struct v4l2_plane *v4l2_planes) | ||
650 | { | ||
651 | unsigned int plane; | ||
652 | int ret; | ||
653 | |||
654 | if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) { | ||
655 | /* | ||
656 | * Verify that the userspace gave us a valid array for | ||
657 | * plane information. | ||
658 | */ | ||
659 | ret = __verify_planes_array(vb, b); | ||
660 | if (ret) | ||
661 | return ret; | ||
662 | |||
663 | /* Fill in driver-provided information for OUTPUT types */ | ||
664 | if (V4L2_TYPE_IS_OUTPUT(b->type)) { | ||
665 | /* | ||
666 | * Will have to go up to b->length when API starts | ||
667 | * accepting variable number of planes. | ||
668 | */ | ||
669 | for (plane = 0; plane < vb->num_planes; ++plane) { | ||
670 | v4l2_planes[plane].bytesused = | ||
671 | b->m.planes[plane].bytesused; | ||
672 | v4l2_planes[plane].data_offset = | ||
673 | b->m.planes[plane].data_offset; | ||
674 | } | ||
675 | } | ||
676 | |||
677 | if (b->memory == V4L2_MEMORY_USERPTR) { | ||
678 | for (plane = 0; plane < vb->num_planes; ++plane) { | ||
679 | v4l2_planes[plane].m.userptr = | ||
680 | b->m.planes[plane].m.userptr; | ||
681 | v4l2_planes[plane].length = | ||
682 | b->m.planes[plane].length; | ||
683 | } | ||
684 | } | ||
685 | } else { | ||
686 | /* | ||
687 | * Single-planar buffers do not use planes array, | ||
688 | * so fill in relevant v4l2_buffer struct fields instead. | ||
689 | * In videobuf we use our internal V4l2_planes struct for | ||
690 | * single-planar buffers as well, for simplicity. | ||
691 | */ | ||
692 | if (V4L2_TYPE_IS_OUTPUT(b->type)) | ||
693 | v4l2_planes[0].bytesused = b->bytesused; | ||
694 | |||
695 | if (b->memory == V4L2_MEMORY_USERPTR) { | ||
696 | v4l2_planes[0].m.userptr = b->m.userptr; | ||
697 | v4l2_planes[0].length = b->length; | ||
698 | } | ||
699 | } | ||
700 | |||
701 | vb->v4l2_buf.field = b->field; | ||
702 | vb->v4l2_buf.timestamp = b->timestamp; | ||
703 | |||
704 | return 0; | ||
705 | } | ||
706 | |||
707 | /** | ||
708 | * __qbuf_userptr() - handle qbuf of a USERPTR buffer | ||
709 | */ | ||
710 | static int __qbuf_userptr(struct vb2_buffer *vb, struct v4l2_buffer *b) | ||
711 | { | ||
712 | struct v4l2_plane planes[VIDEO_MAX_PLANES]; | ||
713 | struct vb2_queue *q = vb->vb2_queue; | ||
714 | void *mem_priv; | ||
715 | unsigned int plane; | ||
716 | int ret; | ||
717 | int write = !V4L2_TYPE_IS_OUTPUT(q->type); | ||
718 | |||
719 | /* Verify and copy relevant information provided by the userspace */ | ||
720 | ret = __fill_vb2_buffer(vb, b, planes); | ||
721 | if (ret) | ||
722 | return ret; | ||
723 | |||
724 | for (plane = 0; plane < vb->num_planes; ++plane) { | ||
725 | /* Skip the plane if already verified */ | ||
726 | if (vb->v4l2_planes[plane].m.userptr == planes[plane].m.userptr | ||
727 | && vb->v4l2_planes[plane].length == planes[plane].length) | ||
728 | continue; | ||
729 | |||
730 | dprintk(3, "qbuf: userspace address for plane %d changed, " | ||
731 | "reacquiring memory\n", plane); | ||
732 | |||
733 | /* Release previously acquired memory if present */ | ||
734 | if (vb->planes[plane].mem_priv) | ||
735 | call_memop(q, plane, put_userptr, | ||
736 | vb->planes[plane].mem_priv); | ||
737 | |||
738 | vb->planes[plane].mem_priv = NULL; | ||
739 | |||
740 | /* Acquire each plane's memory */ | ||
741 | if (q->mem_ops->get_userptr) { | ||
742 | mem_priv = q->mem_ops->get_userptr(q->alloc_ctx[plane], | ||
743 | planes[plane].m.userptr, | ||
744 | planes[plane].length, | ||
745 | write); | ||
746 | if (IS_ERR(mem_priv)) { | ||
747 | dprintk(1, "qbuf: failed acquiring userspace " | ||
748 | "memory for plane %d\n", plane); | ||
749 | ret = PTR_ERR(mem_priv); | ||
750 | goto err; | ||
751 | } | ||
752 | vb->planes[plane].mem_priv = mem_priv; | ||
753 | } | ||
754 | } | ||
755 | |||
756 | /* | ||
757 | * Call driver-specific initialization on the newly acquired buffer, | ||
758 | * if provided. | ||
759 | */ | ||
760 | ret = call_qop(q, buf_init, vb); | ||
761 | if (ret) { | ||
762 | dprintk(1, "qbuf: buffer initialization failed\n"); | ||
763 | goto err; | ||
764 | } | ||
765 | |||
766 | /* | ||
767 | * Now that everything is in order, copy relevant information | ||
768 | * provided by userspace. | ||
769 | */ | ||
770 | for (plane = 0; plane < vb->num_planes; ++plane) | ||
771 | vb->v4l2_planes[plane] = planes[plane]; | ||
772 | |||
773 | return 0; | ||
774 | err: | ||
775 | /* In case of errors, release planes that were already acquired */ | ||
776 | for (; plane > 0; --plane) { | ||
777 | call_memop(q, plane, put_userptr, | ||
778 | vb->planes[plane - 1].mem_priv); | ||
779 | vb->planes[plane - 1].mem_priv = NULL; | ||
780 | } | ||
781 | |||
782 | return ret; | ||
783 | } | ||
784 | |||
785 | /** | ||
786 | * __qbuf_mmap() - handle qbuf of an MMAP buffer | ||
787 | */ | ||
788 | static int __qbuf_mmap(struct vb2_buffer *vb, struct v4l2_buffer *b) | ||
789 | { | ||
790 | return __fill_vb2_buffer(vb, b, vb->v4l2_planes); | ||
791 | } | ||
792 | |||
793 | /** | ||
794 | * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing | ||
795 | */ | ||
796 | static void __enqueue_in_driver(struct vb2_buffer *vb) | ||
797 | { | ||
798 | struct vb2_queue *q = vb->vb2_queue; | ||
799 | |||
800 | vb->state = VB2_BUF_STATE_ACTIVE; | ||
801 | atomic_inc(&q->queued_count); | ||
802 | q->ops->buf_queue(vb); | ||
803 | } | ||
804 | |||
805 | /** | ||
806 | * vb2_qbuf() - Queue a buffer from userspace | ||
807 | * @q: videobuf2 queue | ||
808 | * @b: buffer structure passed from userspace to vidioc_qbuf handler | ||
809 | * in driver | ||
810 | * | ||
811 | * Should be called from vidioc_qbuf ioctl handler of a driver. | ||
812 | * This function: | ||
813 | * 1) verifies the passed buffer, | ||
814 | * 2) calls buf_prepare callback in the driver (if provided), in which | ||
815 | * driver-specific buffer initialization can be performed, | ||
816 | * 3) if streaming is on, queues the buffer in driver by the means of buf_queue | ||
817 | * callback for processing. | ||
818 | * | ||
819 | * The return values from this function are intended to be directly returned | ||
820 | * from vidioc_qbuf handler in driver. | ||
821 | */ | ||
822 | int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b) | ||
823 | { | ||
824 | struct vb2_buffer *vb; | ||
825 | int ret = 0; | ||
826 | |||
827 | if (b->type != q->type) { | ||
828 | dprintk(1, "qbuf: invalid buffer type\n"); | ||
829 | return -EINVAL; | ||
830 | } | ||
831 | |||
832 | if (b->index >= q->num_buffers) { | ||
833 | dprintk(1, "qbuf: buffer index out of range\n"); | ||
834 | return -EINVAL; | ||
835 | } | ||
836 | |||
837 | vb = q->bufs[b->index]; | ||
838 | if (NULL == vb) { | ||
839 | /* Should never happen */ | ||
840 | dprintk(1, "qbuf: buffer is NULL\n"); | ||
841 | return -EINVAL; | ||
842 | } | ||
843 | |||
844 | if (b->memory != q->memory) { | ||
845 | dprintk(1, "qbuf: invalid memory type\n"); | ||
846 | return -EINVAL; | ||
847 | } | ||
848 | |||
849 | if (vb->state != VB2_BUF_STATE_DEQUEUED) { | ||
850 | dprintk(1, "qbuf: buffer already in use\n"); | ||
851 | return -EINVAL; | ||
852 | } | ||
853 | |||
854 | if (q->memory == V4L2_MEMORY_MMAP) | ||
855 | ret = __qbuf_mmap(vb, b); | ||
856 | else if (q->memory == V4L2_MEMORY_USERPTR) | ||
857 | ret = __qbuf_userptr(vb, b); | ||
858 | else { | ||
859 | WARN(1, "Invalid queue type\n"); | ||
860 | return -EINVAL; | ||
861 | } | ||
862 | |||
863 | if (ret) | ||
864 | return ret; | ||
865 | |||
866 | ret = call_qop(q, buf_prepare, vb); | ||
867 | if (ret) { | ||
868 | dprintk(1, "qbuf: buffer preparation failed\n"); | ||
869 | return ret; | ||
870 | } | ||
871 | |||
872 | /* | ||
873 | * Add to the queued buffers list, a buffer will stay on it until | ||
874 | * dequeued in dqbuf. | ||
875 | */ | ||
876 | list_add_tail(&vb->queued_entry, &q->queued_list); | ||
877 | vb->state = VB2_BUF_STATE_QUEUED; | ||
878 | |||
879 | /* | ||
880 | * If already streaming, give the buffer to driver for processing. | ||
881 | * If not, the buffer will be given to driver on next streamon. | ||
882 | */ | ||
883 | if (q->streaming) | ||
884 | __enqueue_in_driver(vb); | ||
885 | |||
886 | dprintk(1, "qbuf of buffer %d succeeded\n", vb->v4l2_buf.index); | ||
887 | return 0; | ||
888 | } | ||
889 | EXPORT_SYMBOL_GPL(vb2_qbuf); | ||
890 | |||
891 | /** | ||
892 | * __vb2_wait_for_done_vb() - wait for a buffer to become available | ||
893 | * for dequeuing | ||
894 | * | ||
895 | * Will sleep if required for nonblocking == false. | ||
896 | */ | ||
897 | static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking) | ||
898 | { | ||
899 | /* | ||
900 | * All operations on vb_done_list are performed under done_lock | ||
901 | * spinlock protection. However, buffers may be removed from | ||
902 | * it and returned to userspace only while holding both driver's | ||
903 | * lock and the done_lock spinlock. Thus we can be sure that as | ||
904 | * long as we hold the driver's lock, the list will remain not | ||
905 | * empty if list_empty() check succeeds. | ||
906 | */ | ||
907 | |||
908 | for (;;) { | ||
909 | int ret; | ||
910 | |||
911 | if (!q->streaming) { | ||
912 | dprintk(1, "Streaming off, will not wait for buffers\n"); | ||
913 | return -EINVAL; | ||
914 | } | ||
915 | |||
916 | if (!list_empty(&q->done_list)) { | ||
917 | /* | ||
918 | * Found a buffer that we were waiting for. | ||
919 | */ | ||
920 | break; | ||
921 | } | ||
922 | |||
923 | if (nonblocking) { | ||
924 | dprintk(1, "Nonblocking and no buffers to dequeue, " | ||
925 | "will not wait\n"); | ||
926 | return -EAGAIN; | ||
927 | } | ||
928 | |||
929 | /* | ||
930 | * We are streaming and blocking, wait for another buffer to | ||
931 | * become ready or for streamoff. Driver's lock is released to | ||
932 | * allow streamoff or qbuf to be called while waiting. | ||
933 | */ | ||
934 | call_qop(q, wait_prepare, q); | ||
935 | |||
936 | /* | ||
937 | * All locks have been released, it is safe to sleep now. | ||
938 | */ | ||
939 | dprintk(3, "Will sleep waiting for buffers\n"); | ||
940 | ret = wait_event_interruptible(q->done_wq, | ||
941 | !list_empty(&q->done_list) || !q->streaming); | ||
942 | |||
943 | /* | ||
944 | * We need to reevaluate both conditions again after reacquiring | ||
945 | * the locks or return an error if one occurred. | ||
946 | */ | ||
947 | call_qop(q, wait_finish, q); | ||
948 | if (ret) | ||
949 | return ret; | ||
950 | } | ||
951 | return 0; | ||
952 | } | ||
953 | |||
954 | /** | ||
955 | * __vb2_get_done_vb() - get a buffer ready for dequeuing | ||
956 | * | ||
957 | * Will sleep if required for nonblocking == false. | ||
958 | */ | ||
959 | static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb, | ||
960 | int nonblocking) | ||
961 | { | ||
962 | unsigned long flags; | ||
963 | int ret; | ||
964 | |||
965 | /* | ||
966 | * Wait for at least one buffer to become available on the done_list. | ||
967 | */ | ||
968 | ret = __vb2_wait_for_done_vb(q, nonblocking); | ||
969 | if (ret) | ||
970 | return ret; | ||
971 | |||
972 | /* | ||
973 | * Driver's lock has been held since we last verified that done_list | ||
974 | * is not empty, so no need for another list_empty(done_list) check. | ||
975 | */ | ||
976 | spin_lock_irqsave(&q->done_lock, flags); | ||
977 | *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry); | ||
978 | list_del(&(*vb)->done_entry); | ||
979 | spin_unlock_irqrestore(&q->done_lock, flags); | ||
980 | |||
981 | return 0; | ||
982 | } | ||
983 | |||
984 | /** | ||
985 | * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2 | ||
986 | * @q: videobuf2 queue | ||
987 | * | ||
988 | * This function will wait until all buffers that have been given to the driver | ||
989 | * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call | ||
990 | * wait_prepare, wait_finish pair. It is intended to be called with all locks | ||
991 | * taken, for example from stop_streaming() callback. | ||
992 | */ | ||
993 | int vb2_wait_for_all_buffers(struct vb2_queue *q) | ||
994 | { | ||
995 | if (!q->streaming) { | ||
996 | dprintk(1, "Streaming off, will not wait for buffers\n"); | ||
997 | return -EINVAL; | ||
998 | } | ||
999 | |||
1000 | wait_event(q->done_wq, !atomic_read(&q->queued_count)); | ||
1001 | return 0; | ||
1002 | } | ||
1003 | EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers); | ||
1004 | |||
1005 | /** | ||
1006 | * vb2_dqbuf() - Dequeue a buffer to the userspace | ||
1007 | * @q: videobuf2 queue | ||
1008 | * @b: buffer structure passed from userspace to vidioc_dqbuf handler | ||
1009 | * in driver | ||
1010 | * @nonblocking: if true, this call will not sleep waiting for a buffer if no | ||
1011 | * buffers ready for dequeuing are present. Normally the driver | ||
1012 | * would be passing (file->f_flags & O_NONBLOCK) here | ||
1013 | * | ||
1014 | * Should be called from vidioc_dqbuf ioctl handler of a driver. | ||
1015 | * This function: | ||
1016 | * 1) verifies the passed buffer, | ||
1017 | * 2) calls buf_finish callback in the driver (if provided), in which | ||
1018 | * driver can perform any additional operations that may be required before | ||
1019 | * returning the buffer to userspace, such as cache sync, | ||
1020 | * 3) the buffer struct members are filled with relevant information for | ||
1021 | * the userspace. | ||
1022 | * | ||
1023 | * The return values from this function are intended to be directly returned | ||
1024 | * from vidioc_dqbuf handler in driver. | ||
1025 | */ | ||
1026 | int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking) | ||
1027 | { | ||
1028 | struct vb2_buffer *vb = NULL; | ||
1029 | int ret; | ||
1030 | |||
1031 | if (b->type != q->type) { | ||
1032 | dprintk(1, "dqbuf: invalid buffer type\n"); | ||
1033 | return -EINVAL; | ||
1034 | } | ||
1035 | |||
1036 | ret = __vb2_get_done_vb(q, &vb, nonblocking); | ||
1037 | if (ret < 0) { | ||
1038 | dprintk(1, "dqbuf: error getting next done buffer\n"); | ||
1039 | return ret; | ||
1040 | } | ||
1041 | |||
1042 | ret = call_qop(q, buf_finish, vb); | ||
1043 | if (ret) { | ||
1044 | dprintk(1, "dqbuf: buffer finish failed\n"); | ||
1045 | return ret; | ||
1046 | } | ||
1047 | |||
1048 | switch (vb->state) { | ||
1049 | case VB2_BUF_STATE_DONE: | ||
1050 | dprintk(3, "dqbuf: Returning done buffer\n"); | ||
1051 | break; | ||
1052 | case VB2_BUF_STATE_ERROR: | ||
1053 | dprintk(3, "dqbuf: Returning done buffer with errors\n"); | ||
1054 | break; | ||
1055 | default: | ||
1056 | dprintk(1, "dqbuf: Invalid buffer state\n"); | ||
1057 | return -EINVAL; | ||
1058 | } | ||
1059 | |||
1060 | /* Fill buffer information for the userspace */ | ||
1061 | __fill_v4l2_buffer(vb, b); | ||
1062 | /* Remove from videobuf queue */ | ||
1063 | list_del(&vb->queued_entry); | ||
1064 | |||
1065 | dprintk(1, "dqbuf of buffer %d, with state %d\n", | ||
1066 | vb->v4l2_buf.index, vb->state); | ||
1067 | |||
1068 | vb->state = VB2_BUF_STATE_DEQUEUED; | ||
1069 | return 0; | ||
1070 | } | ||
1071 | EXPORT_SYMBOL_GPL(vb2_dqbuf); | ||
1072 | |||
1073 | /** | ||
1074 | * vb2_streamon - start streaming | ||
1075 | * @q: videobuf2 queue | ||
1076 | * @type: type argument passed from userspace to vidioc_streamon handler | ||
1077 | * | ||
1078 | * Should be called from vidioc_streamon handler of a driver. | ||
1079 | * This function: | ||
1080 | * 1) verifies current state | ||
1081 | * 2) starts streaming and passes any previously queued buffers to the driver | ||
1082 | * | ||
1083 | * The return values from this function are intended to be directly returned | ||
1084 | * from vidioc_streamon handler in the driver. | ||
1085 | */ | ||
1086 | int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type) | ||
1087 | { | ||
1088 | struct vb2_buffer *vb; | ||
1089 | |||
1090 | if (type != q->type) { | ||
1091 | dprintk(1, "streamon: invalid stream type\n"); | ||
1092 | return -EINVAL; | ||
1093 | } | ||
1094 | |||
1095 | if (q->streaming) { | ||
1096 | dprintk(1, "streamon: already streaming\n"); | ||
1097 | return -EBUSY; | ||
1098 | } | ||
1099 | |||
1100 | /* | ||
1101 | * Cannot start streaming on an OUTPUT device if no buffers have | ||
1102 | * been queued yet. | ||
1103 | */ | ||
1104 | if (V4L2_TYPE_IS_OUTPUT(q->type)) { | ||
1105 | if (list_empty(&q->queued_list)) { | ||
1106 | dprintk(1, "streamon: no output buffers queued\n"); | ||
1107 | return -EINVAL; | ||
1108 | } | ||
1109 | } | ||
1110 | |||
1111 | q->streaming = 1; | ||
1112 | |||
1113 | /* | ||
1114 | * Let driver notice that streaming state has been enabled. | ||
1115 | */ | ||
1116 | call_qop(q, start_streaming, q); | ||
1117 | |||
1118 | /* | ||
1119 | * If any buffers were queued before streamon, | ||
1120 | * we can now pass them to driver for processing. | ||
1121 | */ | ||
1122 | list_for_each_entry(vb, &q->queued_list, queued_entry) | ||
1123 | __enqueue_in_driver(vb); | ||
1124 | |||
1125 | dprintk(3, "Streamon successful\n"); | ||
1126 | return 0; | ||
1127 | } | ||
1128 | EXPORT_SYMBOL_GPL(vb2_streamon); | ||
1129 | |||
1130 | /** | ||
1131 | * __vb2_queue_cancel() - cancel and stop (pause) streaming | ||
1132 | * | ||
1133 | * Removes all queued buffers from driver's queue and all buffers queued by | ||
1134 | * userspace from videobuf's queue. Returns to state after reqbufs. | ||
1135 | */ | ||
1136 | static void __vb2_queue_cancel(struct vb2_queue *q) | ||
1137 | { | ||
1138 | unsigned int i; | ||
1139 | |||
1140 | /* | ||
1141 | * Tell driver to stop all transactions and release all queued | ||
1142 | * buffers. | ||
1143 | */ | ||
1144 | if (q->streaming) | ||
1145 | call_qop(q, stop_streaming, q); | ||
1146 | q->streaming = 0; | ||
1147 | |||
1148 | /* | ||
1149 | * Remove all buffers from videobuf's list... | ||
1150 | */ | ||
1151 | INIT_LIST_HEAD(&q->queued_list); | ||
1152 | /* | ||
1153 | * ...and done list; userspace will not receive any buffers it | ||
1154 | * has not already dequeued before initiating cancel. | ||
1155 | */ | ||
1156 | INIT_LIST_HEAD(&q->done_list); | ||
1157 | wake_up_all(&q->done_wq); | ||
1158 | |||
1159 | /* | ||
1160 | * Reinitialize all buffers for next use. | ||
1161 | */ | ||
1162 | for (i = 0; i < q->num_buffers; ++i) | ||
1163 | q->bufs[i]->state = VB2_BUF_STATE_DEQUEUED; | ||
1164 | } | ||
1165 | |||
1166 | /** | ||
1167 | * vb2_streamoff - stop streaming | ||
1168 | * @q: videobuf2 queue | ||
1169 | * @type: type argument passed from userspace to vidioc_streamoff handler | ||
1170 | * | ||
1171 | * Should be called from vidioc_streamoff handler of a driver. | ||
1172 | * This function: | ||
1173 | * 1) verifies current state, | ||
1174 | * 2) stop streaming and dequeues any queued buffers, including those previously | ||
1175 | * passed to the driver (after waiting for the driver to finish). | ||
1176 | * | ||
1177 | * This call can be used for pausing playback. | ||
1178 | * The return values from this function are intended to be directly returned | ||
1179 | * from vidioc_streamoff handler in the driver | ||
1180 | */ | ||
1181 | int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type) | ||
1182 | { | ||
1183 | if (type != q->type) { | ||
1184 | dprintk(1, "streamoff: invalid stream type\n"); | ||
1185 | return -EINVAL; | ||
1186 | } | ||
1187 | |||
1188 | if (!q->streaming) { | ||
1189 | dprintk(1, "streamoff: not streaming\n"); | ||
1190 | return -EINVAL; | ||
1191 | } | ||
1192 | |||
1193 | /* | ||
1194 | * Cancel will pause streaming and remove all buffers from the driver | ||
1195 | * and videobuf, effectively returning control over them to userspace. | ||
1196 | */ | ||
1197 | __vb2_queue_cancel(q); | ||
1198 | |||
1199 | dprintk(3, "Streamoff successful\n"); | ||
1200 | return 0; | ||
1201 | } | ||
1202 | EXPORT_SYMBOL_GPL(vb2_streamoff); | ||
1203 | |||
1204 | /** | ||
1205 | * __find_plane_by_offset() - find plane associated with the given offset off | ||
1206 | */ | ||
1207 | static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off, | ||
1208 | unsigned int *_buffer, unsigned int *_plane) | ||
1209 | { | ||
1210 | struct vb2_buffer *vb; | ||
1211 | unsigned int buffer, plane; | ||
1212 | |||
1213 | /* | ||
1214 | * Go over all buffers and their planes, comparing the given offset | ||
1215 | * with an offset assigned to each plane. If a match is found, | ||
1216 | * return its buffer and plane numbers. | ||
1217 | */ | ||
1218 | for (buffer = 0; buffer < q->num_buffers; ++buffer) { | ||
1219 | vb = q->bufs[buffer]; | ||
1220 | |||
1221 | for (plane = 0; plane < vb->num_planes; ++plane) { | ||
1222 | if (vb->v4l2_planes[plane].m.mem_offset == off) { | ||
1223 | *_buffer = buffer; | ||
1224 | *_plane = plane; | ||
1225 | return 0; | ||
1226 | } | ||
1227 | } | ||
1228 | } | ||
1229 | |||
1230 | return -EINVAL; | ||
1231 | } | ||
1232 | |||
1233 | /** | ||
1234 | * vb2_mmap() - map video buffers into application address space | ||
1235 | * @q: videobuf2 queue | ||
1236 | * @vma: vma passed to the mmap file operation handler in the driver | ||
1237 | * | ||
1238 | * Should be called from mmap file operation handler of a driver. | ||
1239 | * This function maps one plane of one of the available video buffers to | ||
1240 | * userspace. To map whole video memory allocated on reqbufs, this function | ||
1241 | * has to be called once per each plane per each buffer previously allocated. | ||
1242 | * | ||
1243 | * When the userspace application calls mmap, it passes to it an offset returned | ||
1244 | * to it earlier by the means of vidioc_querybuf handler. That offset acts as | ||
1245 | * a "cookie", which is then used to identify the plane to be mapped. | ||
1246 | * This function finds a plane with a matching offset and a mapping is performed | ||
1247 | * by the means of a provided memory operation. | ||
1248 | * | ||
1249 | * The return values from this function are intended to be directly returned | ||
1250 | * from the mmap handler in driver. | ||
1251 | */ | ||
1252 | int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma) | ||
1253 | { | ||
1254 | unsigned long off = vma->vm_pgoff << PAGE_SHIFT; | ||
1255 | struct vb2_plane *vb_plane; | ||
1256 | struct vb2_buffer *vb; | ||
1257 | unsigned int buffer, plane; | ||
1258 | int ret; | ||
1259 | |||
1260 | if (q->memory != V4L2_MEMORY_MMAP) { | ||
1261 | dprintk(1, "Queue is not currently set up for mmap\n"); | ||
1262 | return -EINVAL; | ||
1263 | } | ||
1264 | |||
1265 | /* | ||
1266 | * Check memory area access mode. | ||
1267 | */ | ||
1268 | if (!(vma->vm_flags & VM_SHARED)) { | ||
1269 | dprintk(1, "Invalid vma flags, VM_SHARED needed\n"); | ||
1270 | return -EINVAL; | ||
1271 | } | ||
1272 | if (V4L2_TYPE_IS_OUTPUT(q->type)) { | ||
1273 | if (!(vma->vm_flags & VM_WRITE)) { | ||
1274 | dprintk(1, "Invalid vma flags, VM_WRITE needed\n"); | ||
1275 | return -EINVAL; | ||
1276 | } | ||
1277 | } else { | ||
1278 | if (!(vma->vm_flags & VM_READ)) { | ||
1279 | dprintk(1, "Invalid vma flags, VM_READ needed\n"); | ||
1280 | return -EINVAL; | ||
1281 | } | ||
1282 | } | ||
1283 | |||
1284 | /* | ||
1285 | * Find the plane corresponding to the offset passed by userspace. | ||
1286 | */ | ||
1287 | ret = __find_plane_by_offset(q, off, &buffer, &plane); | ||
1288 | if (ret) | ||
1289 | return ret; | ||
1290 | |||
1291 | vb = q->bufs[buffer]; | ||
1292 | vb_plane = &vb->planes[plane]; | ||
1293 | |||
1294 | ret = q->mem_ops->mmap(vb_plane->mem_priv, vma); | ||
1295 | if (ret) | ||
1296 | return ret; | ||
1297 | |||
1298 | vb_plane->mapped = 1; | ||
1299 | vb->num_planes_mapped++; | ||
1300 | |||
1301 | dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer, plane); | ||
1302 | return 0; | ||
1303 | } | ||
1304 | EXPORT_SYMBOL_GPL(vb2_mmap); | ||
1305 | |||
1306 | |||
1307 | /** | ||
1308 | * vb2_poll() - implements poll userspace operation | ||
1309 | * @q: videobuf2 queue | ||
1310 | * @file: file argument passed to the poll file operation handler | ||
1311 | * @wait: wait argument passed to the poll file operation handler | ||
1312 | * | ||
1313 | * This function implements poll file operation handler for a driver. | ||
1314 | * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will | ||
1315 | * be informed that the file descriptor of a video device is available for | ||
1316 | * reading. | ||
1317 | * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor | ||
1318 | * will be reported as available for writing. | ||
1319 | * | ||
1320 | * The return values from this function are intended to be directly returned | ||
1321 | * from poll handler in driver. | ||
1322 | */ | ||
1323 | unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait) | ||
1324 | { | ||
1325 | unsigned long flags; | ||
1326 | struct vb2_buffer *vb = NULL; | ||
1327 | |||
1328 | /* | ||
1329 | * There is nothing to wait for if no buffers have already been queued. | ||
1330 | */ | ||
1331 | if (list_empty(&q->queued_list)) | ||
1332 | return POLLERR; | ||
1333 | |||
1334 | poll_wait(file, &q->done_wq, wait); | ||
1335 | |||
1336 | /* | ||
1337 | * Take first buffer available for dequeuing. | ||
1338 | */ | ||
1339 | spin_lock_irqsave(&q->done_lock, flags); | ||
1340 | if (!list_empty(&q->done_list)) | ||
1341 | vb = list_first_entry(&q->done_list, struct vb2_buffer, | ||
1342 | done_entry); | ||
1343 | spin_unlock_irqrestore(&q->done_lock, flags); | ||
1344 | |||
1345 | if (vb && (vb->state == VB2_BUF_STATE_DONE | ||
1346 | || vb->state == VB2_BUF_STATE_ERROR)) { | ||
1347 | return (V4L2_TYPE_IS_OUTPUT(q->type)) ? POLLOUT | POLLWRNORM : | ||
1348 | POLLIN | POLLRDNORM; | ||
1349 | } | ||
1350 | return 0; | ||
1351 | } | ||
1352 | EXPORT_SYMBOL_GPL(vb2_poll); | ||
1353 | |||
1354 | /** | ||
1355 | * vb2_queue_init() - initialize a videobuf2 queue | ||
1356 | * @q: videobuf2 queue; this structure should be allocated in driver | ||
1357 | * | ||
1358 | * The vb2_queue structure should be allocated by the driver. The driver is | ||
1359 | * responsible of clearing it's content and setting initial values for some | ||
1360 | * required entries before calling this function. | ||
1361 | * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer | ||
1362 | * to the struct vb2_queue description in include/media/videobuf2-core.h | ||
1363 | * for more information. | ||
1364 | */ | ||
1365 | int vb2_queue_init(struct vb2_queue *q) | ||
1366 | { | ||
1367 | BUG_ON(!q); | ||
1368 | BUG_ON(!q->ops); | ||
1369 | BUG_ON(!q->mem_ops); | ||
1370 | BUG_ON(!q->type); | ||
1371 | BUG_ON(!q->io_modes); | ||
1372 | |||
1373 | BUG_ON(!q->ops->queue_setup); | ||
1374 | BUG_ON(!q->ops->buf_queue); | ||
1375 | |||
1376 | INIT_LIST_HEAD(&q->queued_list); | ||
1377 | INIT_LIST_HEAD(&q->done_list); | ||
1378 | spin_lock_init(&q->done_lock); | ||
1379 | init_waitqueue_head(&q->done_wq); | ||
1380 | |||
1381 | if (q->buf_struct_size == 0) | ||
1382 | q->buf_struct_size = sizeof(struct vb2_buffer); | ||
1383 | |||
1384 | return 0; | ||
1385 | } | ||
1386 | EXPORT_SYMBOL_GPL(vb2_queue_init); | ||
1387 | |||
1388 | /** | ||
1389 | * vb2_queue_release() - stop streaming, release the queue and free memory | ||
1390 | * @q: videobuf2 queue | ||
1391 | * | ||
1392 | * This function stops streaming and performs necessary clean ups, including | ||
1393 | * freeing video buffer memory. The driver is responsible for freeing | ||
1394 | * the vb2_queue structure itself. | ||
1395 | */ | ||
1396 | void vb2_queue_release(struct vb2_queue *q) | ||
1397 | { | ||
1398 | __vb2_queue_cancel(q); | ||
1399 | __vb2_queue_free(q); | ||
1400 | } | ||
1401 | EXPORT_SYMBOL_GPL(vb2_queue_release); | ||
1402 | |||
1403 | MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2"); | ||
1404 | MODULE_AUTHOR("Pawel Osciak, Marek Szyprowski"); | ||
1405 | MODULE_LICENSE("GPL"); | ||