Commit 896f38f582730a19eb49677105b4fe4c0270b82e

Authored by Ezequiel Garcia
Committed by Mauro Carvalho Chehab
1 parent 4195ec7a8f

[media] videobuf2-core: Replace BUG_ON and return an error at vb2_queue_init()

This replaces BUG_ON() calls with WARN_ON(), and returns
EINVAL if some parameter is NULL, as suggested by Jonathan and Mauro.
The BUG_ON() call is too drastic to be used in this case.
See the full discussion here:
http://www.spinics.net/lists/linux-media/msg52462.html

Signed-off-by: Ezequiel Garcia <elezegarcia@gmail.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>

Showing 2 changed files with 12 additions and 9 deletions Inline Diff

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