blob: bb0a1c8de414b57735e0e45418cc2d29c1831ca5 [file] [log] [blame]
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001/*
2 * generic helper functions for handling video4linux capture buffers
3 *
4 * (c) 2007 Mauro Carvalho Chehab, <mchehab@infradead.org>
5 *
6 * Highly based on video-buf written originally by:
7 * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org>
8 * (c) 2006 Mauro Carvalho Chehab, <mchehab@infradead.org>
9 * (c) 2006 Ted Walther and John Sokol
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2
14 */
15
16#include <linux/init.h>
17#include <linux/module.h>
18#include <linux/moduleparam.h>
Andrea Righi27ac7922008-07-23 21:28:13 -070019#include <linux/mm.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040020#include <linux/sched.h>
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030021#include <linux/slab.h>
22#include <linux/interrupt.h>
23
24#include <media/videobuf-core.h>
25
26#define MAGIC_BUFFER 0x20070728
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -030027#define MAGIC_CHECK(is, should) do { \
28 if (unlikely((is) != (should))) { \
29 printk(KERN_ERR "magic mismatch: %x (expected %x)\n", is, should); \
30 BUG(); } } while (0)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030031
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -030032static int debug;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030033module_param(debug, int, 0644);
34
35MODULE_DESCRIPTION("helper module to manage video4linux buffers");
36MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
37MODULE_LICENSE("GPL");
38
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -030039#define dprintk(level, fmt, arg...) do { \
40 if (debug >= level) \
41 printk(KERN_DEBUG "vbuf: " fmt , ## arg); } while (0)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030042
43/* --------------------------------------------------------------------- */
44
45#define CALL(q, f, arg...) \
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -030046 ((q->int_ops->f) ? q->int_ops->f(arg) : 0)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030047
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -030048void *videobuf_alloc(struct videobuf_queue *q)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030049{
50 struct videobuf_buffer *vb;
51
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -030052 BUG_ON(q->msize < sizeof(*vb));
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030053
54 if (!q->int_ops || !q->int_ops->alloc) {
55 printk(KERN_ERR "No specific ops defined!\n");
56 BUG();
57 }
58
59 vb = q->int_ops->alloc(q->msize);
60
61 if (NULL != vb) {
62 init_waitqueue_head(&vb->done);
63 vb->magic = MAGIC_BUFFER;
64 }
65
66 return vb;
67}
68
Brandon Philips009a9052008-04-02 18:10:59 -030069#define WAITON_CONDITION (vb->state != VIDEOBUF_ACTIVE &&\
70 vb->state != VIDEOBUF_QUEUED)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030071int videobuf_waiton(struct videobuf_buffer *vb, int non_blocking, int intr)
72{
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -030073 MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
Brandon Philips009a9052008-04-02 18:10:59 -030074
75 if (non_blocking) {
76 if (WAITON_CONDITION)
77 return 0;
78 else
79 return -EAGAIN;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030080 }
Brandon Philips009a9052008-04-02 18:10:59 -030081
82 if (intr)
83 return wait_event_interruptible(vb->done, WAITON_CONDITION);
84 else
85 wait_event(vb->done, WAITON_CONDITION);
86
87 return 0;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030088}
89
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -030090int videobuf_iolock(struct videobuf_queue *q, struct videobuf_buffer *vb,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030091 struct v4l2_framebuffer *fbuf)
92{
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -030093 MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
94 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030095
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -030096 return CALL(q, iolock, q, vb, fbuf);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030097}
98
Mauro Carvalho Chehab59d34482008-04-13 15:10:00 -030099void *videobuf_queue_to_vmalloc (struct videobuf_queue *q,
100 struct videobuf_buffer *buf)
101{
Harvey Harrisona6a3a172008-04-28 16:50:03 -0700102 if (q->int_ops->vmalloc)
103 return q->int_ops->vmalloc(buf);
104 else
105 return NULL;
Mauro Carvalho Chehab59d34482008-04-13 15:10:00 -0300106}
107EXPORT_SYMBOL_GPL(videobuf_queue_to_vmalloc);
108
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300109/* --------------------------------------------------------------------- */
110
111
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300112void videobuf_queue_core_init(struct videobuf_queue *q,
Jonathan Corbet38a54f32009-11-17 19:43:41 -0300113 const struct videobuf_queue_ops *ops,
Guennadi Liakhovetskie9bcf662008-04-22 14:46:02 -0300114 struct device *dev,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300115 spinlock_t *irqlock,
116 enum v4l2_buf_type type,
117 enum v4l2_field field,
118 unsigned int msize,
Mauro Carvalho Chehabd4cae5a2007-10-08 12:20:02 -0300119 void *priv,
120 struct videobuf_qtype_ops *int_ops)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300121{
Figo.zhang96ceea22009-06-02 23:01:04 -0300122 BUG_ON(!q);
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300123 memset(q, 0, sizeof(*q));
Mauro Carvalho Chehabd4cae5a2007-10-08 12:20:02 -0300124 q->irqlock = irqlock;
125 q->dev = dev;
126 q->type = type;
127 q->field = field;
128 q->msize = msize;
129 q->ops = ops;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300130 q->priv_data = priv;
Mauro Carvalho Chehabd4cae5a2007-10-08 12:20:02 -0300131 q->int_ops = int_ops;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300132
133 /* All buffer operations are mandatory */
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300134 BUG_ON(!q->ops->buf_setup);
135 BUG_ON(!q->ops->buf_prepare);
136 BUG_ON(!q->ops->buf_queue);
137 BUG_ON(!q->ops->buf_release);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300138
Brandon Philips0cf4dae2008-03-28 10:18:33 -0700139 /* Lock is mandatory for queue_cancel to work */
140 BUG_ON(!irqlock);
141
Mauro Carvalho Chehabd4cae5a2007-10-08 12:20:02 -0300142 /* Having implementations for abstract methods are mandatory */
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300143 BUG_ON(!q->int_ops);
Mauro Carvalho Chehabd4cae5a2007-10-08 12:20:02 -0300144
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300145 mutex_init(&q->vb_lock);
Brandon Philips137d1cb2008-04-02 18:10:59 -0300146 init_waitqueue_head(&q->wait);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300147 INIT_LIST_HEAD(&q->stream);
148}
149
Brandon Philips19bc5132007-11-13 20:05:38 -0300150/* Locking: Only usage in bttv unsafe find way to remove */
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300151int videobuf_queue_is_busy(struct videobuf_queue *q)
152{
153 int i;
154
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300155 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300156
157 if (q->streaming) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300158 dprintk(1, "busy: streaming active\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300159 return 1;
160 }
161 if (q->reading) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300162 dprintk(1, "busy: pending read #1\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300163 return 1;
164 }
165 if (q->read_buf) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300166 dprintk(1, "busy: pending read #2\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300167 return 1;
168 }
169 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
170 if (NULL == q->bufs[i])
171 continue;
Mauro Carvalho Chehab851c0c92007-09-27 18:25:44 -0300172 if (q->bufs[i]->map) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300173 dprintk(1, "busy: buffer #%d mapped\n", i);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300174 return 1;
175 }
Brandon Philips0fc06862007-11-06 20:02:36 -0300176 if (q->bufs[i]->state == VIDEOBUF_QUEUED) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300177 dprintk(1, "busy: buffer #%d queued\n", i);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300178 return 1;
179 }
Brandon Philips0fc06862007-11-06 20:02:36 -0300180 if (q->bufs[i]->state == VIDEOBUF_ACTIVE) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300181 dprintk(1, "busy: buffer #%d avtive\n", i);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300182 return 1;
183 }
184 }
185 return 0;
186}
187
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300188/* Locking: Caller holds q->vb_lock */
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300189void videobuf_queue_cancel(struct videobuf_queue *q)
190{
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300191 unsigned long flags = 0;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300192 int i;
193
Brandon Philips137d1cb2008-04-02 18:10:59 -0300194 q->streaming = 0;
195 q->reading = 0;
196 wake_up_interruptible_sync(&q->wait);
197
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300198 /* remove queued buffers from list */
Brandon Philips0cf4dae2008-03-28 10:18:33 -0700199 spin_lock_irqsave(q->irqlock, flags);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300200 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
201 if (NULL == q->bufs[i])
202 continue;
Brandon Philips0fc06862007-11-06 20:02:36 -0300203 if (q->bufs[i]->state == VIDEOBUF_QUEUED) {
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300204 list_del(&q->bufs[i]->queue);
Brandon Philips0fc06862007-11-06 20:02:36 -0300205 q->bufs[i]->state = VIDEOBUF_ERROR;
Brandon Philipsb608f432008-04-02 18:10:57 -0300206 wake_up_all(&q->bufs[i]->done);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300207 }
208 }
Brandon Philips0cf4dae2008-03-28 10:18:33 -0700209 spin_unlock_irqrestore(q->irqlock, flags);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300210
211 /* free all buffers + clear queue */
212 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
213 if (NULL == q->bufs[i])
214 continue;
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300215 q->ops->buf_release(q, q->bufs[i]);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300216 }
217 INIT_LIST_HEAD(&q->stream);
218}
219
220/* --------------------------------------------------------------------- */
221
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300222/* Locking: Caller holds q->vb_lock */
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300223enum v4l2_field videobuf_next_field(struct videobuf_queue *q)
224{
225 enum v4l2_field field = q->field;
226
227 BUG_ON(V4L2_FIELD_ANY == field);
228
229 if (V4L2_FIELD_ALTERNATE == field) {
230 if (V4L2_FIELD_TOP == q->last) {
231 field = V4L2_FIELD_BOTTOM;
232 q->last = V4L2_FIELD_BOTTOM;
233 } else {
234 field = V4L2_FIELD_TOP;
235 q->last = V4L2_FIELD_TOP;
236 }
237 }
238 return field;
239}
240
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300241/* Locking: Caller holds q->vb_lock */
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300242static void videobuf_status(struct videobuf_queue *q, struct v4l2_buffer *b,
243 struct videobuf_buffer *vb, enum v4l2_buf_type type)
244{
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300245 MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
246 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300247
248 b->index = vb->i;
249 b->type = type;
250
251 b->memory = vb->memory;
252 switch (b->memory) {
253 case V4L2_MEMORY_MMAP:
254 b->m.offset = vb->boff;
255 b->length = vb->bsize;
256 break;
257 case V4L2_MEMORY_USERPTR:
258 b->m.userptr = vb->baddr;
259 b->length = vb->bsize;
260 break;
261 case V4L2_MEMORY_OVERLAY:
262 b->m.offset = vb->boff;
263 break;
264 }
265
266 b->flags = 0;
Mauro Carvalho Chehab851c0c92007-09-27 18:25:44 -0300267 if (vb->map)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300268 b->flags |= V4L2_BUF_FLAG_MAPPED;
269
270 switch (vb->state) {
Brandon Philips0fc06862007-11-06 20:02:36 -0300271 case VIDEOBUF_PREPARED:
272 case VIDEOBUF_QUEUED:
273 case VIDEOBUF_ACTIVE:
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300274 b->flags |= V4L2_BUF_FLAG_QUEUED;
275 break;
Brandon Philips0fc06862007-11-06 20:02:36 -0300276 case VIDEOBUF_DONE:
277 case VIDEOBUF_ERROR:
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300278 b->flags |= V4L2_BUF_FLAG_DONE;
279 break;
Brandon Philips0fc06862007-11-06 20:02:36 -0300280 case VIDEOBUF_NEEDS_INIT:
281 case VIDEOBUF_IDLE:
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300282 /* nothing */
283 break;
284 }
285
286 if (vb->input != UNSET) {
287 b->flags |= V4L2_BUF_FLAG_INPUT;
288 b->input = vb->input;
289 }
290
291 b->field = vb->field;
292 b->timestamp = vb->ts;
293 b->bytesused = vb->size;
294 b->sequence = vb->field_count >> 1;
295}
296
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300297/* Locking: Caller holds q->vb_lock */
Brandon Philips19bc5132007-11-13 20:05:38 -0300298static int __videobuf_mmap_free(struct videobuf_queue *q)
299{
300 int i;
301 int rc;
302
303 if (!q)
304 return 0;
305
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300306 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
Brandon Philips19bc5132007-11-13 20:05:38 -0300307
Mauro Carvalho Chehabd05051c2008-01-10 07:33:03 -0300308
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300309 rc = CALL(q, mmap_free, q);
Mauro Carvalho Chehabd05051c2008-01-10 07:33:03 -0300310
311 q->is_mmapped = 0;
312
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300313 if (rc < 0)
Brandon Philips19bc5132007-11-13 20:05:38 -0300314 return rc;
315
316 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
317 if (NULL == q->bufs[i])
318 continue;
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300319 q->ops->buf_release(q, q->bufs[i]);
Brandon Philips19bc5132007-11-13 20:05:38 -0300320 kfree(q->bufs[i]);
321 q->bufs[i] = NULL;
322 }
323
324 return rc;
325}
326
327int videobuf_mmap_free(struct videobuf_queue *q)
328{
329 int ret;
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300330 mutex_lock(&q->vb_lock);
Brandon Philips19bc5132007-11-13 20:05:38 -0300331 ret = __videobuf_mmap_free(q);
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300332 mutex_unlock(&q->vb_lock);
Brandon Philips19bc5132007-11-13 20:05:38 -0300333 return ret;
334}
335
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300336/* Locking: Caller holds q->vb_lock */
Arjan van de Ven81b2dbc2008-05-20 09:53:52 -0700337int __videobuf_mmap_setup(struct videobuf_queue *q,
Brandon Philips19bc5132007-11-13 20:05:38 -0300338 unsigned int bcount, unsigned int bsize,
339 enum v4l2_memory memory)
340{
341 unsigned int i;
342 int err;
343
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300344 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
Brandon Philips19bc5132007-11-13 20:05:38 -0300345
346 err = __videobuf_mmap_free(q);
347 if (0 != err)
348 return err;
349
350 /* Allocate and initialize buffers */
351 for (i = 0; i < bcount; i++) {
352 q->bufs[i] = videobuf_alloc(q);
353
354 if (q->bufs[i] == NULL)
355 break;
356
357 q->bufs[i]->i = i;
358 q->bufs[i]->input = UNSET;
359 q->bufs[i]->memory = memory;
360 q->bufs[i]->bsize = bsize;
361 switch (memory) {
362 case V4L2_MEMORY_MMAP:
Tuukka Toivonen7cbefad2009-07-23 10:56:25 -0300363 q->bufs[i]->boff = PAGE_ALIGN(bsize) * i;
Brandon Philips19bc5132007-11-13 20:05:38 -0300364 break;
365 case V4L2_MEMORY_USERPTR:
366 case V4L2_MEMORY_OVERLAY:
367 /* nothing */
368 break;
369 }
370 }
371
372 if (!i)
373 return -ENOMEM;
374
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300375 dprintk(1, "mmap setup: %d buffers, %d bytes each\n",
Brandon Philips19bc5132007-11-13 20:05:38 -0300376 i, bsize);
377
378 return i;
379}
380
381int videobuf_mmap_setup(struct videobuf_queue *q,
382 unsigned int bcount, unsigned int bsize,
383 enum v4l2_memory memory)
384{
385 int ret;
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300386 mutex_lock(&q->vb_lock);
Brandon Philips19bc5132007-11-13 20:05:38 -0300387 ret = __videobuf_mmap_setup(q, bcount, bsize, memory);
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300388 mutex_unlock(&q->vb_lock);
Brandon Philips19bc5132007-11-13 20:05:38 -0300389 return ret;
390}
391
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300392int videobuf_reqbufs(struct videobuf_queue *q,
393 struct v4l2_requestbuffers *req)
394{
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300395 unsigned int size, count;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300396 int retval;
397
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300398 if (req->count < 1) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300399 dprintk(1, "reqbufs: count invalid (%d)\n", req->count);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300400 return -EINVAL;
401 }
Brandon Philips19bc5132007-11-13 20:05:38 -0300402
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300403 if (req->memory != V4L2_MEMORY_MMAP &&
404 req->memory != V4L2_MEMORY_USERPTR &&
405 req->memory != V4L2_MEMORY_OVERLAY) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300406 dprintk(1, "reqbufs: memory type invalid\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300407 return -EINVAL;
408 }
409
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300410 mutex_lock(&q->vb_lock);
Brandon Philips19bc5132007-11-13 20:05:38 -0300411 if (req->type != q->type) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300412 dprintk(1, "reqbufs: queue type invalid\n");
Brandon Philips19bc5132007-11-13 20:05:38 -0300413 retval = -EINVAL;
414 goto done;
415 }
416
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300417 if (q->streaming) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300418 dprintk(1, "reqbufs: streaming already exists\n");
Brandon Philips00f98d02007-09-27 20:55:28 -0300419 retval = -EBUSY;
420 goto done;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300421 }
422 if (!list_empty(&q->stream)) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300423 dprintk(1, "reqbufs: stream running\n");
Brandon Philips00f98d02007-09-27 20:55:28 -0300424 retval = -EBUSY;
425 goto done;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300426 }
427
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300428 count = req->count;
429 if (count > VIDEO_MAX_FRAME)
430 count = VIDEO_MAX_FRAME;
431 size = 0;
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300432 q->ops->buf_setup(q, &count, &size);
Márton Németh296372e2009-11-22 18:03:05 -0300433 dprintk(1, "reqbufs: bufs=%d, size=0x%x [%u pages total]\n",
434 count, size,
435 (unsigned int)((count*PAGE_ALIGN(size))>>PAGE_SHIFT) );
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300436
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300437 retval = __videobuf_mmap_setup(q, count, size, req->memory);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300438 if (retval < 0) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300439 dprintk(1, "reqbufs: mmap setup returned %d\n", retval);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300440 goto done;
441 }
442
Brandon Philips49ee7182007-10-05 16:26:27 -0300443 req->count = retval;
Németh Márton925d74a2009-04-29 15:57:24 -0300444 retval = 0;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300445
446 done:
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300447 mutex_unlock(&q->vb_lock);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300448 return retval;
449}
450
451int videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b)
452{
Brandon Philips19bc5132007-11-13 20:05:38 -0300453 int ret = -EINVAL;
454
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300455 mutex_lock(&q->vb_lock);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300456 if (unlikely(b->type != q->type)) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300457 dprintk(1, "querybuf: Wrong type.\n");
Brandon Philips19bc5132007-11-13 20:05:38 -0300458 goto done;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300459 }
Roel Kluin223ffe52009-05-02 16:38:47 -0300460 if (unlikely(b->index >= VIDEO_MAX_FRAME)) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300461 dprintk(1, "querybuf: index out of range.\n");
Brandon Philips19bc5132007-11-13 20:05:38 -0300462 goto done;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300463 }
464 if (unlikely(NULL == q->bufs[b->index])) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300465 dprintk(1, "querybuf: buffer is null.\n");
Brandon Philips19bc5132007-11-13 20:05:38 -0300466 goto done;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300467 }
Brandon Philips19bc5132007-11-13 20:05:38 -0300468
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300469 videobuf_status(q, b, q->bufs[b->index], q->type);
Brandon Philips19bc5132007-11-13 20:05:38 -0300470
471 ret = 0;
472done:
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300473 mutex_unlock(&q->vb_lock);
Brandon Philips19bc5132007-11-13 20:05:38 -0300474 return ret;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300475}
476
477int videobuf_qbuf(struct videobuf_queue *q,
478 struct v4l2_buffer *b)
479{
480 struct videobuf_buffer *buf;
481 enum v4l2_field field;
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300482 unsigned long flags = 0;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300483 int retval;
484
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300485 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300486
Maxim Levitsky99001322007-09-27 20:34:09 -0300487 if (b->memory == V4L2_MEMORY_MMAP)
488 down_read(&current->mm->mmap_sem);
489
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300490 mutex_lock(&q->vb_lock);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300491 retval = -EBUSY;
492 if (q->reading) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300493 dprintk(1, "qbuf: Reading running...\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300494 goto done;
495 }
496 retval = -EINVAL;
497 if (b->type != q->type) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300498 dprintk(1, "qbuf: Wrong type.\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300499 goto done;
500 }
Roel Kluin223ffe52009-05-02 16:38:47 -0300501 if (b->index >= VIDEO_MAX_FRAME) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300502 dprintk(1, "qbuf: index out of range.\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300503 goto done;
504 }
505 buf = q->bufs[b->index];
506 if (NULL == buf) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300507 dprintk(1, "qbuf: buffer is null.\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300508 goto done;
509 }
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300510 MAGIC_CHECK(buf->magic, MAGIC_BUFFER);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300511 if (buf->memory != b->memory) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300512 dprintk(1, "qbuf: memory type is wrong.\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300513 goto done;
514 }
Brandon Philips0fc06862007-11-06 20:02:36 -0300515 if (buf->state != VIDEOBUF_NEEDS_INIT && buf->state != VIDEOBUF_IDLE) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300516 dprintk(1, "qbuf: buffer is already queued or active.\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300517 goto done;
518 }
519
520 if (b->flags & V4L2_BUF_FLAG_INPUT) {
521 if (b->input >= q->inputs) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300522 dprintk(1, "qbuf: wrong input.\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300523 goto done;
524 }
525 buf->input = b->input;
526 } else {
527 buf->input = UNSET;
528 }
529
530 switch (b->memory) {
531 case V4L2_MEMORY_MMAP:
532 if (0 == buf->baddr) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300533 dprintk(1, "qbuf: mmap requested "
534 "but buffer addr is zero!\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300535 goto done;
536 }
537 break;
538 case V4L2_MEMORY_USERPTR:
539 if (b->length < buf->bsize) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300540 dprintk(1, "qbuf: buffer length is not enough\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300541 goto done;
542 }
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300543 if (VIDEOBUF_NEEDS_INIT != buf->state &&
544 buf->baddr != b->m.userptr)
545 q->ops->buf_release(q, buf);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300546 buf->baddr = b->m.userptr;
547 break;
548 case V4L2_MEMORY_OVERLAY:
549 buf->boff = b->m.offset;
550 break;
551 default:
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300552 dprintk(1, "qbuf: wrong memory type\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300553 goto done;
554 }
555
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300556 dprintk(1, "qbuf: requesting next field\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300557 field = videobuf_next_field(q);
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300558 retval = q->ops->buf_prepare(q, buf, field);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300559 if (0 != retval) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300560 dprintk(1, "qbuf: buffer_prepare returned %d\n", retval);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300561 goto done;
562 }
563
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300564 list_add_tail(&buf->stream, &q->stream);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300565 if (q->streaming) {
Brandon Philips0cf4dae2008-03-28 10:18:33 -0700566 spin_lock_irqsave(q->irqlock, flags);
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300567 q->ops->buf_queue(q, buf);
Brandon Philips0cf4dae2008-03-28 10:18:33 -0700568 spin_unlock_irqrestore(q->irqlock, flags);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300569 }
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300570 dprintk(1, "qbuf: succeded\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300571 retval = 0;
Brandon Philips137d1cb2008-04-02 18:10:59 -0300572 wake_up_interruptible_sync(&q->wait);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300573
574 done:
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300575 mutex_unlock(&q->vb_lock);
Maxim Levitsky99001322007-09-27 20:34:09 -0300576
577 if (b->memory == V4L2_MEMORY_MMAP)
578 up_read(&current->mm->mmap_sem);
579
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300580 return retval;
581}
582
Brandon Philips137d1cb2008-04-02 18:10:59 -0300583
584/* Locking: Caller holds q->vb_lock */
585static int stream_next_buffer_check_queue(struct videobuf_queue *q, int noblock)
586{
587 int retval;
588
589checks:
590 if (!q->streaming) {
591 dprintk(1, "next_buffer: Not streaming\n");
592 retval = -EINVAL;
593 goto done;
594 }
595
596 if (list_empty(&q->stream)) {
597 if (noblock) {
598 retval = -EAGAIN;
599 dprintk(2, "next_buffer: no buffers to dequeue\n");
600 goto done;
601 } else {
602 dprintk(2, "next_buffer: waiting on buffer\n");
603
604 /* Drop lock to avoid deadlock with qbuf */
605 mutex_unlock(&q->vb_lock);
606
607 /* Checking list_empty and streaming is safe without
608 * locks because we goto checks to validate while
609 * holding locks before proceeding */
610 retval = wait_event_interruptible(q->wait,
611 !list_empty(&q->stream) || !q->streaming);
612 mutex_lock(&q->vb_lock);
613
614 if (retval)
615 goto done;
616
617 goto checks;
618 }
619 }
620
621 retval = 0;
622
623done:
624 return retval;
625}
626
627
628/* Locking: Caller holds q->vb_lock */
629static int stream_next_buffer(struct videobuf_queue *q,
630 struct videobuf_buffer **vb, int nonblocking)
631{
632 int retval;
633 struct videobuf_buffer *buf = NULL;
634
635 retval = stream_next_buffer_check_queue(q, nonblocking);
636 if (retval)
637 goto done;
638
639 buf = list_entry(q->stream.next, struct videobuf_buffer, stream);
640 retval = videobuf_waiton(buf, nonblocking, 1);
641 if (retval < 0)
642 goto done;
643
644 *vb = buf;
645done:
646 return retval;
647}
648
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300649int videobuf_dqbuf(struct videobuf_queue *q,
650 struct v4l2_buffer *b, int nonblocking)
651{
Brandon Philips137d1cb2008-04-02 18:10:59 -0300652 struct videobuf_buffer *buf = NULL;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300653 int retval;
654
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300655 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300656
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300657 mutex_lock(&q->vb_lock);
Brandon Philips137d1cb2008-04-02 18:10:59 -0300658
659 retval = stream_next_buffer(q, &buf, nonblocking);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300660 if (retval < 0) {
Brandon Philips137d1cb2008-04-02 18:10:59 -0300661 dprintk(1, "dqbuf: next_buffer error: %i\n", retval);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300662 goto done;
663 }
Brandon Philips137d1cb2008-04-02 18:10:59 -0300664
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300665 switch (buf->state) {
Brandon Philips0fc06862007-11-06 20:02:36 -0300666 case VIDEOBUF_ERROR:
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300667 dprintk(1, "dqbuf: state is error\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300668 retval = -EIO;
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300669 CALL(q, sync, q, buf);
Brandon Philips0fc06862007-11-06 20:02:36 -0300670 buf->state = VIDEOBUF_IDLE;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300671 break;
Brandon Philips0fc06862007-11-06 20:02:36 -0300672 case VIDEOBUF_DONE:
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300673 dprintk(1, "dqbuf: state is done\n");
674 CALL(q, sync, q, buf);
Brandon Philips0fc06862007-11-06 20:02:36 -0300675 buf->state = VIDEOBUF_IDLE;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300676 break;
677 default:
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300678 dprintk(1, "dqbuf: state invalid\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300679 retval = -EINVAL;
680 goto done;
681 }
682 list_del(&buf->stream);
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300683 memset(b, 0, sizeof(*b));
684 videobuf_status(q, b, buf, q->type);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300685
686 done:
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300687 mutex_unlock(&q->vb_lock);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300688 return retval;
689}
690
691int videobuf_streamon(struct videobuf_queue *q)
692{
693 struct videobuf_buffer *buf;
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300694 unsigned long flags = 0;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300695 int retval;
696
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300697 mutex_lock(&q->vb_lock);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300698 retval = -EBUSY;
699 if (q->reading)
700 goto done;
701 retval = 0;
702 if (q->streaming)
703 goto done;
704 q->streaming = 1;
Brandon Philips0cf4dae2008-03-28 10:18:33 -0700705 spin_lock_irqsave(q->irqlock, flags);
Trent Piephoa991f442007-10-10 05:37:43 -0300706 list_for_each_entry(buf, &q->stream, stream)
Brandon Philips0fc06862007-11-06 20:02:36 -0300707 if (buf->state == VIDEOBUF_PREPARED)
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300708 q->ops->buf_queue(q, buf);
Brandon Philips0cf4dae2008-03-28 10:18:33 -0700709 spin_unlock_irqrestore(q->irqlock, flags);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300710
Brandon Philips137d1cb2008-04-02 18:10:59 -0300711 wake_up_interruptible_sync(&q->wait);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300712 done:
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300713 mutex_unlock(&q->vb_lock);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300714 return retval;
715}
716
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300717/* Locking: Caller holds q->vb_lock */
Brandon Philips19bc5132007-11-13 20:05:38 -0300718static int __videobuf_streamoff(struct videobuf_queue *q)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300719{
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300720 if (!q->streaming)
Brandon Philips19bc5132007-11-13 20:05:38 -0300721 return -EINVAL;
722
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300723 videobuf_queue_cancel(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300724
Brandon Philips19bc5132007-11-13 20:05:38 -0300725 return 0;
726}
727
728int videobuf_streamoff(struct videobuf_queue *q)
729{
730 int retval;
731
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300732 mutex_lock(&q->vb_lock);
Brandon Philips19bc5132007-11-13 20:05:38 -0300733 retval = __videobuf_streamoff(q);
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300734 mutex_unlock(&q->vb_lock);
Brandon Philips19bc5132007-11-13 20:05:38 -0300735
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300736 return retval;
737}
738
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300739/* Locking: Caller holds q->vb_lock */
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300740static ssize_t videobuf_read_zerocopy(struct videobuf_queue *q,
741 char __user *data,
742 size_t count, loff_t *ppos)
743{
744 enum v4l2_field field;
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300745 unsigned long flags = 0;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300746 int retval;
747
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300748 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300749
750 /* setup stuff */
751 q->read_buf = videobuf_alloc(q);
752 if (NULL == q->read_buf)
753 return -ENOMEM;
754
755 q->read_buf->memory = V4L2_MEMORY_USERPTR;
756 q->read_buf->baddr = (unsigned long)data;
757 q->read_buf->bsize = count;
758
759 field = videobuf_next_field(q);
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300760 retval = q->ops->buf_prepare(q, q->read_buf, field);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300761 if (0 != retval)
762 goto done;
763
764 /* start capture & wait */
Brandon Philips0cf4dae2008-03-28 10:18:33 -0700765 spin_lock_irqsave(q->irqlock, flags);
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300766 q->ops->buf_queue(q, q->read_buf);
Brandon Philips0cf4dae2008-03-28 10:18:33 -0700767 spin_unlock_irqrestore(q->irqlock, flags);
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300768 retval = videobuf_waiton(q->read_buf, 0, 0);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300769 if (0 == retval) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300770 CALL(q, sync, q, q->read_buf);
Brandon Philips0fc06862007-11-06 20:02:36 -0300771 if (VIDEOBUF_ERROR == q->read_buf->state)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300772 retval = -EIO;
773 else
774 retval = q->read_buf->size;
775 }
776
777 done:
778 /* cleanup */
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300779 q->ops->buf_release(q, q->read_buf);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300780 kfree(q->read_buf);
781 q->read_buf = NULL;
782 return retval;
783}
784
785ssize_t videobuf_read_one(struct videobuf_queue *q,
786 char __user *data, size_t count, loff_t *ppos,
787 int nonblocking)
788{
789 enum v4l2_field field;
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300790 unsigned long flags = 0;
Guennadi Liakhovetski7daa4a82008-04-22 14:46:03 -0300791 unsigned size = 0, nbufs = 1;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300792 int retval;
793
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300794 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300795
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300796 mutex_lock(&q->vb_lock);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300797
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300798 q->ops->buf_setup(q, &nbufs, &size);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300799
800 if (NULL == q->read_buf &&
801 count >= size &&
802 !nonblocking) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300803 retval = videobuf_read_zerocopy(q, data, count, ppos);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300804 if (retval >= 0 || retval == -EIO)
805 /* ok, all done */
806 goto done;
807 /* fallback to kernel bounce buffer on failures */
808 }
809
810 if (NULL == q->read_buf) {
811 /* need to capture a new frame */
812 retval = -ENOMEM;
813 q->read_buf = videobuf_alloc(q);
814
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300815 dprintk(1, "video alloc=0x%p\n", q->read_buf);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300816 if (NULL == q->read_buf)
817 goto done;
818 q->read_buf->memory = V4L2_MEMORY_USERPTR;
819 q->read_buf->bsize = count; /* preferred size */
820 field = videobuf_next_field(q);
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300821 retval = q->ops->buf_prepare(q, q->read_buf, field);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300822
823 if (0 != retval) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300824 kfree(q->read_buf);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300825 q->read_buf = NULL;
826 goto done;
827 }
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300828
Brandon Philips0cf4dae2008-03-28 10:18:33 -0700829 spin_lock_irqsave(q->irqlock, flags);
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300830 q->ops->buf_queue(q, q->read_buf);
Brandon Philips0cf4dae2008-03-28 10:18:33 -0700831 spin_unlock_irqrestore(q->irqlock, flags);
832
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300833 q->read_off = 0;
834 }
835
836 /* wait until capture is done */
837 retval = videobuf_waiton(q->read_buf, nonblocking, 1);
838 if (0 != retval)
839 goto done;
840
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300841 CALL(q, sync, q, q->read_buf);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300842
Brandon Philips0fc06862007-11-06 20:02:36 -0300843 if (VIDEOBUF_ERROR == q->read_buf->state) {
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300844 /* catch I/O errors */
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300845 q->ops->buf_release(q, q->read_buf);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300846 kfree(q->read_buf);
847 q->read_buf = NULL;
848 retval = -EIO;
849 goto done;
850 }
851
852 /* Copy to userspace */
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300853 retval = CALL(q, video_copy_to_user, q, data, count, nonblocking);
854 if (retval < 0)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300855 goto done;
856
857 q->read_off += retval;
858 if (q->read_off == q->read_buf->size) {
859 /* all data copied, cleanup */
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300860 q->ops->buf_release(q, q->read_buf);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300861 kfree(q->read_buf);
862 q->read_buf = NULL;
863 }
864
865 done:
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300866 mutex_unlock(&q->vb_lock);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300867 return retval;
868}
869
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300870/* Locking: Caller holds q->vb_lock */
Mauro Carvalho Chehab225ba902007-12-12 21:46:26 -0300871static int __videobuf_read_start(struct videobuf_queue *q)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300872{
873 enum v4l2_field field;
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300874 unsigned long flags = 0;
Brandon Philips49ee7182007-10-05 16:26:27 -0300875 unsigned int count = 0, size = 0;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300876 int err, i;
877
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300878 q->ops->buf_setup(q, &count, &size);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300879 if (count < 2)
880 count = 2;
881 if (count > VIDEO_MAX_FRAME)
882 count = VIDEO_MAX_FRAME;
883 size = PAGE_ALIGN(size);
884
Brandon Philips19bc5132007-11-13 20:05:38 -0300885 err = __videobuf_mmap_setup(q, count, size, V4L2_MEMORY_USERPTR);
Brandon Philips49ee7182007-10-05 16:26:27 -0300886 if (err < 0)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300887 return err;
888
Brandon Philips49ee7182007-10-05 16:26:27 -0300889 count = err;
890
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300891 for (i = 0; i < count; i++) {
892 field = videobuf_next_field(q);
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300893 err = q->ops->buf_prepare(q, q->bufs[i], field);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300894 if (err)
895 return err;
896 list_add_tail(&q->bufs[i]->stream, &q->stream);
897 }
Brandon Philips0cf4dae2008-03-28 10:18:33 -0700898 spin_lock_irqsave(q->irqlock, flags);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300899 for (i = 0; i < count; i++)
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300900 q->ops->buf_queue(q, q->bufs[i]);
Brandon Philips0cf4dae2008-03-28 10:18:33 -0700901 spin_unlock_irqrestore(q->irqlock, flags);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300902 q->reading = 1;
903 return 0;
904}
905
Brandon Philips19bc5132007-11-13 20:05:38 -0300906static void __videobuf_read_stop(struct videobuf_queue *q)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300907{
908 int i;
909
910 videobuf_queue_cancel(q);
Brandon Philips19bc5132007-11-13 20:05:38 -0300911 __videobuf_mmap_free(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300912 INIT_LIST_HEAD(&q->stream);
913 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
914 if (NULL == q->bufs[i])
915 continue;
916 kfree(q->bufs[i]);
917 q->bufs[i] = NULL;
918 }
919 q->read_buf = NULL;
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300920
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300921}
922
Mauro Carvalho Chehab19fb1452007-11-15 23:09:30 -0300923int videobuf_read_start(struct videobuf_queue *q)
924{
925 int rc;
926
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300927 mutex_lock(&q->vb_lock);
Mauro Carvalho Chehab19fb1452007-11-15 23:09:30 -0300928 rc = __videobuf_read_start(q);
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300929 mutex_unlock(&q->vb_lock);
Mauro Carvalho Chehab19fb1452007-11-15 23:09:30 -0300930
931 return rc;
932}
933
Brandon Philips19bc5132007-11-13 20:05:38 -0300934void videobuf_read_stop(struct videobuf_queue *q)
935{
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300936 mutex_lock(&q->vb_lock);
Brandon Philips19bc5132007-11-13 20:05:38 -0300937 __videobuf_read_stop(q);
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300938 mutex_unlock(&q->vb_lock);
Brandon Philips19bc5132007-11-13 20:05:38 -0300939}
940
941void videobuf_stop(struct videobuf_queue *q)
942{
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300943 mutex_lock(&q->vb_lock);
Brandon Philips19bc5132007-11-13 20:05:38 -0300944
945 if (q->streaming)
946 __videobuf_streamoff(q);
947
948 if (q->reading)
949 __videobuf_read_stop(q);
950
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300951 mutex_unlock(&q->vb_lock);
Brandon Philips19bc5132007-11-13 20:05:38 -0300952}
953
954
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300955ssize_t videobuf_read_stream(struct videobuf_queue *q,
956 char __user *data, size_t count, loff_t *ppos,
957 int vbihack, int nonblocking)
958{
959 int rc, retval;
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300960 unsigned long flags = 0;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300961
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300962 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300963
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300964 dprintk(2, "%s\n", __func__);
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300965 mutex_lock(&q->vb_lock);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300966 retval = -EBUSY;
967 if (q->streaming)
968 goto done;
969 if (!q->reading) {
Adrian Bunk3f843072007-12-12 16:44:54 -0300970 retval = __videobuf_read_start(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300971 if (retval < 0)
972 goto done;
973 }
974
975 retval = 0;
976 while (count > 0) {
977 /* get / wait for data */
978 if (NULL == q->read_buf) {
979 q->read_buf = list_entry(q->stream.next,
980 struct videobuf_buffer,
981 stream);
982 list_del(&q->read_buf->stream);
983 q->read_off = 0;
984 }
985 rc = videobuf_waiton(q->read_buf, nonblocking, 1);
986 if (rc < 0) {
987 if (0 == retval)
988 retval = rc;
989 break;
990 }
991
Brandon Philips0fc06862007-11-06 20:02:36 -0300992 if (q->read_buf->state == VIDEOBUF_DONE) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300993 rc = CALL(q, copy_stream, q, data + retval, count,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300994 retval, vbihack, nonblocking);
995 if (rc < 0) {
996 retval = rc;
997 break;
998 }
999 retval += rc;
1000 count -= rc;
1001 q->read_off += rc;
1002 } else {
1003 /* some error */
1004 q->read_off = q->read_buf->size;
1005 if (0 == retval)
1006 retval = -EIO;
1007 }
1008
1009 /* requeue buffer when done with copying */
1010 if (q->read_off == q->read_buf->size) {
1011 list_add_tail(&q->read_buf->stream,
1012 &q->stream);
Brandon Philips0cf4dae2008-03-28 10:18:33 -07001013 spin_lock_irqsave(q->irqlock, flags);
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -03001014 q->ops->buf_queue(q, q->read_buf);
Brandon Philips0cf4dae2008-03-28 10:18:33 -07001015 spin_unlock_irqrestore(q->irqlock, flags);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001016 q->read_buf = NULL;
1017 }
1018 if (retval < 0)
1019 break;
1020 }
1021
1022 done:
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03001023 mutex_unlock(&q->vb_lock);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001024 return retval;
1025}
1026
1027unsigned int videobuf_poll_stream(struct file *file,
1028 struct videobuf_queue *q,
1029 poll_table *wait)
1030{
1031 struct videobuf_buffer *buf = NULL;
1032 unsigned int rc = 0;
1033
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03001034 mutex_lock(&q->vb_lock);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001035 if (q->streaming) {
1036 if (!list_empty(&q->stream))
1037 buf = list_entry(q->stream.next,
1038 struct videobuf_buffer, stream);
1039 } else {
1040 if (!q->reading)
Adrian Bunk3f843072007-12-12 16:44:54 -03001041 __videobuf_read_start(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001042 if (!q->reading) {
1043 rc = POLLERR;
1044 } else if (NULL == q->read_buf) {
1045 q->read_buf = list_entry(q->stream.next,
1046 struct videobuf_buffer,
1047 stream);
1048 list_del(&q->read_buf->stream);
1049 q->read_off = 0;
1050 }
1051 buf = q->read_buf;
1052 }
1053 if (!buf)
1054 rc = POLLERR;
1055
1056 if (0 == rc) {
1057 poll_wait(file, &buf->done, wait);
Brandon Philips0fc06862007-11-06 20:02:36 -03001058 if (buf->state == VIDEOBUF_DONE ||
1059 buf->state == VIDEOBUF_ERROR)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001060 rc = POLLIN|POLLRDNORM;
1061 }
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03001062 mutex_unlock(&q->vb_lock);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001063 return rc;
1064}
1065
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001066int videobuf_mmap_mapper(struct videobuf_queue *q,
1067 struct vm_area_struct *vma)
1068{
1069 int retval;
1070
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -03001071 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001072
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03001073 mutex_lock(&q->vb_lock);
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -03001074 retval = CALL(q, mmap_mapper, q, vma);
Mauro Carvalho Chehabd05051c2008-01-10 07:33:03 -03001075 q->is_mmapped = 1;
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -03001076 mutex_unlock(&q->vb_lock);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001077
1078 return retval;
1079}
1080
1081#ifdef CONFIG_VIDEO_V4L1_COMPAT
1082int videobuf_cgmbuf(struct videobuf_queue *q,
1083 struct video_mbuf *mbuf, int count)
1084{
1085 struct v4l2_requestbuffers req;
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -03001086 int rc, i;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001087
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -03001088 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001089
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -03001090 memset(&req, 0, sizeof(req));
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001091 req.type = q->type;
1092 req.count = count;
1093 req.memory = V4L2_MEMORY_MMAP;
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -03001094 rc = videobuf_reqbufs(q, &req);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001095 if (rc < 0)
1096 return rc;
1097
1098 mbuf->frames = req.count;
1099 mbuf->size = 0;
1100 for (i = 0; i < mbuf->frames; i++) {
1101 mbuf->offsets[i] = q->bufs[i]->boff;
Tuukka Toivonen7cbefad2009-07-23 10:56:25 -03001102 mbuf->size += PAGE_ALIGN(q->bufs[i]->bsize);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001103 }
1104
1105 return 0;
1106}
Pekka Enberga13625c2007-10-13 05:54:54 -03001107EXPORT_SYMBOL_GPL(videobuf_cgmbuf);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001108#endif
1109
1110/* --------------------------------------------------------------------- */
1111
1112EXPORT_SYMBOL_GPL(videobuf_waiton);
1113EXPORT_SYMBOL_GPL(videobuf_iolock);
1114
1115EXPORT_SYMBOL_GPL(videobuf_alloc);
1116
Mauro Carvalho Chehabd4cae5a2007-10-08 12:20:02 -03001117EXPORT_SYMBOL_GPL(videobuf_queue_core_init);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001118EXPORT_SYMBOL_GPL(videobuf_queue_cancel);
1119EXPORT_SYMBOL_GPL(videobuf_queue_is_busy);
1120
1121EXPORT_SYMBOL_GPL(videobuf_next_field);
1122EXPORT_SYMBOL_GPL(videobuf_reqbufs);
1123EXPORT_SYMBOL_GPL(videobuf_querybuf);
1124EXPORT_SYMBOL_GPL(videobuf_qbuf);
1125EXPORT_SYMBOL_GPL(videobuf_dqbuf);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001126EXPORT_SYMBOL_GPL(videobuf_streamon);
1127EXPORT_SYMBOL_GPL(videobuf_streamoff);
1128
Mauro Carvalho Chehab19fb1452007-11-15 23:09:30 -03001129EXPORT_SYMBOL_GPL(videobuf_read_start);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001130EXPORT_SYMBOL_GPL(videobuf_read_stop);
Brandon Philips19bc5132007-11-13 20:05:38 -03001131EXPORT_SYMBOL_GPL(videobuf_stop);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001132EXPORT_SYMBOL_GPL(videobuf_read_stream);
1133EXPORT_SYMBOL_GPL(videobuf_read_one);
1134EXPORT_SYMBOL_GPL(videobuf_poll_stream);
1135
Arjan van de Ven81b2dbc2008-05-20 09:53:52 -07001136EXPORT_SYMBOL_GPL(__videobuf_mmap_setup);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001137EXPORT_SYMBOL_GPL(videobuf_mmap_setup);
1138EXPORT_SYMBOL_GPL(videobuf_mmap_free);
1139EXPORT_SYMBOL_GPL(videobuf_mmap_mapper);