blob: a32ef8eafc0176669d1bd3f8644714b709bdbe7d [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
Pawel Osciak7a022642010-03-17 04:01:04 -030027#define MAGIC_CHECK(is, should) \
28 do { \
29 if (unlikely((is) != (should))) { \
30 printk(KERN_ERR \
31 "magic mismatch: %x (expected %x)\n", \
32 is, should); \
33 BUG(); \
34 } \
35 } while (0)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030036
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -030037static int debug;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030038module_param(debug, int, 0644);
39
40MODULE_DESCRIPTION("helper module to manage video4linux buffers");
41MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
42MODULE_LICENSE("GPL");
43
Pawel Osciak7a022642010-03-17 04:01:04 -030044#define dprintk(level, fmt, arg...) \
45 do { \
46 if (debug >= level) \
47 printk(KERN_DEBUG "vbuf: " fmt, ## arg); \
48 } while (0)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030049
50/* --------------------------------------------------------------------- */
51
52#define CALL(q, f, arg...) \
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -030053 ((q->int_ops->f) ? q->int_ops->f(arg) : 0)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030054
Pawel Osciak33c38282010-05-11 10:36:28 -030055struct videobuf_buffer *videobuf_alloc_vb(struct videobuf_queue *q)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030056{
57 struct videobuf_buffer *vb;
58
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -030059 BUG_ON(q->msize < sizeof(*vb));
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030060
Pawel Osciak33c38282010-05-11 10:36:28 -030061 if (!q->int_ops || !q->int_ops->alloc_vb) {
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030062 printk(KERN_ERR "No specific ops defined!\n");
63 BUG();
64 }
65
Pawel Osciak33c38282010-05-11 10:36:28 -030066 vb = q->int_ops->alloc_vb(q->msize);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030067 if (NULL != vb) {
68 init_waitqueue_head(&vb->done);
Pawel Osciak7a022642010-03-17 04:01:04 -030069 vb->magic = MAGIC_BUFFER;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030070 }
71
72 return vb;
73}
Pawel Osciak33c38282010-05-11 10:36:28 -030074EXPORT_SYMBOL_GPL(videobuf_alloc_vb);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030075
Brandon Philips009a9052008-04-02 18:10:59 -030076#define WAITON_CONDITION (vb->state != VIDEOBUF_ACTIVE &&\
77 vb->state != VIDEOBUF_QUEUED)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030078int videobuf_waiton(struct videobuf_buffer *vb, int non_blocking, int intr)
79{
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -030080 MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
Brandon Philips009a9052008-04-02 18:10:59 -030081
82 if (non_blocking) {
83 if (WAITON_CONDITION)
84 return 0;
85 else
86 return -EAGAIN;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030087 }
Brandon Philips009a9052008-04-02 18:10:59 -030088
89 if (intr)
90 return wait_event_interruptible(vb->done, WAITON_CONDITION);
91 else
92 wait_event(vb->done, WAITON_CONDITION);
93
94 return 0;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030095}
Pawel Osciak7a022642010-03-17 04:01:04 -030096EXPORT_SYMBOL_GPL(videobuf_waiton);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030097
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -030098int videobuf_iolock(struct videobuf_queue *q, struct videobuf_buffer *vb,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030099 struct v4l2_framebuffer *fbuf)
100{
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300101 MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
102 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300103
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300104 return CALL(q, iolock, q, vb, fbuf);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300105}
Pawel Osciak7a022642010-03-17 04:01:04 -0300106EXPORT_SYMBOL_GPL(videobuf_iolock);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300107
Hans Verkuilf4fce602010-03-28 08:33:23 -0300108void *videobuf_queue_to_vaddr(struct videobuf_queue *q,
109 struct videobuf_buffer *buf)
Mauro Carvalho Chehab59d34482008-04-13 15:10:00 -0300110{
Hans Verkuil037c75e2010-03-28 08:18:37 -0300111 if (q->int_ops->vaddr)
112 return q->int_ops->vaddr(buf);
Hans Verkuilf4fce602010-03-28 08:33:23 -0300113 return NULL;
Mauro Carvalho Chehab59d34482008-04-13 15:10:00 -0300114}
Hans Verkuilf4fce602010-03-28 08:33:23 -0300115EXPORT_SYMBOL_GPL(videobuf_queue_to_vaddr);
Mauro Carvalho Chehab59d34482008-04-13 15:10:00 -0300116
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300117/* --------------------------------------------------------------------- */
118
119
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300120void videobuf_queue_core_init(struct videobuf_queue *q,
Jonathan Corbet38a54f32009-11-17 19:43:41 -0300121 const struct videobuf_queue_ops *ops,
Guennadi Liakhovetskie9bcf662008-04-22 14:46:02 -0300122 struct device *dev,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300123 spinlock_t *irqlock,
124 enum v4l2_buf_type type,
125 enum v4l2_field field,
126 unsigned int msize,
Mauro Carvalho Chehabd4cae5a2007-10-08 12:20:02 -0300127 void *priv,
Hans Verkuil08bff032010-09-20 17:39:46 -0300128 struct videobuf_qtype_ops *int_ops,
129 struct mutex *ext_lock)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300130{
Figo.zhang96ceea22009-06-02 23:01:04 -0300131 BUG_ON(!q);
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300132 memset(q, 0, sizeof(*q));
Mauro Carvalho Chehabd4cae5a2007-10-08 12:20:02 -0300133 q->irqlock = irqlock;
Hans Verkuil08bff032010-09-20 17:39:46 -0300134 q->ext_lock = ext_lock;
Mauro Carvalho Chehabd4cae5a2007-10-08 12:20:02 -0300135 q->dev = dev;
136 q->type = type;
137 q->field = field;
138 q->msize = msize;
139 q->ops = ops;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300140 q->priv_data = priv;
Mauro Carvalho Chehabd4cae5a2007-10-08 12:20:02 -0300141 q->int_ops = int_ops;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300142
143 /* All buffer operations are mandatory */
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300144 BUG_ON(!q->ops->buf_setup);
145 BUG_ON(!q->ops->buf_prepare);
146 BUG_ON(!q->ops->buf_queue);
147 BUG_ON(!q->ops->buf_release);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300148
Brandon Philips0cf4dae2008-03-28 10:18:33 -0700149 /* Lock is mandatory for queue_cancel to work */
150 BUG_ON(!irqlock);
151
Mauro Carvalho Chehabd4cae5a2007-10-08 12:20:02 -0300152 /* Having implementations for abstract methods are mandatory */
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300153 BUG_ON(!q->int_ops);
Mauro Carvalho Chehabd4cae5a2007-10-08 12:20:02 -0300154
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300155 mutex_init(&q->vb_lock);
Brandon Philips137d1cb2008-04-02 18:10:59 -0300156 init_waitqueue_head(&q->wait);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300157 INIT_LIST_HEAD(&q->stream);
158}
Pawel Osciak7a022642010-03-17 04:01:04 -0300159EXPORT_SYMBOL_GPL(videobuf_queue_core_init);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300160
Brandon Philips19bc5132007-11-13 20:05:38 -0300161/* Locking: Only usage in bttv unsafe find way to remove */
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300162int videobuf_queue_is_busy(struct videobuf_queue *q)
163{
164 int i;
165
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300166 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300167
168 if (q->streaming) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300169 dprintk(1, "busy: streaming active\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300170 return 1;
171 }
172 if (q->reading) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300173 dprintk(1, "busy: pending read #1\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300174 return 1;
175 }
176 if (q->read_buf) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300177 dprintk(1, "busy: pending read #2\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300178 return 1;
179 }
180 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
181 if (NULL == q->bufs[i])
182 continue;
Mauro Carvalho Chehab851c0c92007-09-27 18:25:44 -0300183 if (q->bufs[i]->map) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300184 dprintk(1, "busy: buffer #%d mapped\n", i);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300185 return 1;
186 }
Brandon Philips0fc06862007-11-06 20:02:36 -0300187 if (q->bufs[i]->state == VIDEOBUF_QUEUED) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300188 dprintk(1, "busy: buffer #%d queued\n", i);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300189 return 1;
190 }
Brandon Philips0fc06862007-11-06 20:02:36 -0300191 if (q->bufs[i]->state == VIDEOBUF_ACTIVE) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300192 dprintk(1, "busy: buffer #%d avtive\n", i);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300193 return 1;
194 }
195 }
196 return 0;
197}
Pawel Osciak7a022642010-03-17 04:01:04 -0300198EXPORT_SYMBOL_GPL(videobuf_queue_is_busy);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300199
Pawel Osciaka438d6d2010-05-11 10:36:29 -0300200/**
201 * __videobuf_free() - free all the buffers and their control structures
202 *
203 * This function can only be called if streaming/reading is off, i.e. no buffers
204 * are under control of the driver.
205 */
206/* Locking: Caller holds q->vb_lock */
207static int __videobuf_free(struct videobuf_queue *q)
208{
209 int i;
210
211 dprintk(1, "%s\n", __func__);
212 if (!q)
213 return 0;
214
215 if (q->streaming || q->reading) {
216 dprintk(1, "Cannot free buffers when streaming or reading\n");
217 return -EBUSY;
218 }
219
220 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
221
222 for (i = 0; i < VIDEO_MAX_FRAME; i++)
223 if (q->bufs[i] && q->bufs[i]->map) {
224 dprintk(1, "Cannot free mmapped buffers\n");
225 return -EBUSY;
226 }
227
228 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
229 if (NULL == q->bufs[i])
230 continue;
231 q->ops->buf_release(q, q->bufs[i]);
232 kfree(q->bufs[i]);
233 q->bufs[i] = NULL;
234 }
235
236 return 0;
237}
238
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300239/* Locking: Caller holds q->vb_lock */
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300240void videobuf_queue_cancel(struct videobuf_queue *q)
241{
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300242 unsigned long flags = 0;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300243 int i;
244
Brandon Philips137d1cb2008-04-02 18:10:59 -0300245 q->streaming = 0;
246 q->reading = 0;
247 wake_up_interruptible_sync(&q->wait);
248
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300249 /* remove queued buffers from list */
Brandon Philips0cf4dae2008-03-28 10:18:33 -0700250 spin_lock_irqsave(q->irqlock, flags);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300251 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
252 if (NULL == q->bufs[i])
253 continue;
Brandon Philips0fc06862007-11-06 20:02:36 -0300254 if (q->bufs[i]->state == VIDEOBUF_QUEUED) {
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300255 list_del(&q->bufs[i]->queue);
Brandon Philips0fc06862007-11-06 20:02:36 -0300256 q->bufs[i]->state = VIDEOBUF_ERROR;
Brandon Philipsb608f432008-04-02 18:10:57 -0300257 wake_up_all(&q->bufs[i]->done);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300258 }
259 }
Brandon Philips0cf4dae2008-03-28 10:18:33 -0700260 spin_unlock_irqrestore(q->irqlock, flags);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300261
262 /* free all buffers + clear queue */
263 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
264 if (NULL == q->bufs[i])
265 continue;
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300266 q->ops->buf_release(q, q->bufs[i]);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300267 }
268 INIT_LIST_HEAD(&q->stream);
269}
Pawel Osciak7a022642010-03-17 04:01:04 -0300270EXPORT_SYMBOL_GPL(videobuf_queue_cancel);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300271
272/* --------------------------------------------------------------------- */
273
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300274/* Locking: Caller holds q->vb_lock */
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300275enum v4l2_field videobuf_next_field(struct videobuf_queue *q)
276{
277 enum v4l2_field field = q->field;
278
279 BUG_ON(V4L2_FIELD_ANY == field);
280
281 if (V4L2_FIELD_ALTERNATE == field) {
282 if (V4L2_FIELD_TOP == q->last) {
283 field = V4L2_FIELD_BOTTOM;
284 q->last = V4L2_FIELD_BOTTOM;
285 } else {
286 field = V4L2_FIELD_TOP;
287 q->last = V4L2_FIELD_TOP;
288 }
289 }
290 return field;
291}
Pawel Osciak7a022642010-03-17 04:01:04 -0300292EXPORT_SYMBOL_GPL(videobuf_next_field);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300293
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300294/* Locking: Caller holds q->vb_lock */
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300295static void videobuf_status(struct videobuf_queue *q, struct v4l2_buffer *b,
296 struct videobuf_buffer *vb, enum v4l2_buf_type type)
297{
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300298 MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
299 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300300
301 b->index = vb->i;
302 b->type = type;
303
304 b->memory = vb->memory;
305 switch (b->memory) {
306 case V4L2_MEMORY_MMAP:
307 b->m.offset = vb->boff;
308 b->length = vb->bsize;
309 break;
310 case V4L2_MEMORY_USERPTR:
311 b->m.userptr = vb->baddr;
312 b->length = vb->bsize;
313 break;
314 case V4L2_MEMORY_OVERLAY:
315 b->m.offset = vb->boff;
316 break;
317 }
318
319 b->flags = 0;
Mauro Carvalho Chehab851c0c92007-09-27 18:25:44 -0300320 if (vb->map)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300321 b->flags |= V4L2_BUF_FLAG_MAPPED;
322
323 switch (vb->state) {
Brandon Philips0fc06862007-11-06 20:02:36 -0300324 case VIDEOBUF_PREPARED:
325 case VIDEOBUF_QUEUED:
326 case VIDEOBUF_ACTIVE:
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300327 b->flags |= V4L2_BUF_FLAG_QUEUED;
328 break;
Brandon Philips0fc06862007-11-06 20:02:36 -0300329 case VIDEOBUF_ERROR:
Hans Verkuilb2dfd1a2010-04-28 04:05:22 -0300330 b->flags |= V4L2_BUF_FLAG_ERROR;
331 /* fall through */
332 case VIDEOBUF_DONE:
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300333 b->flags |= V4L2_BUF_FLAG_DONE;
334 break;
Brandon Philips0fc06862007-11-06 20:02:36 -0300335 case VIDEOBUF_NEEDS_INIT:
336 case VIDEOBUF_IDLE:
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300337 /* nothing */
338 break;
339 }
340
341 if (vb->input != UNSET) {
342 b->flags |= V4L2_BUF_FLAG_INPUT;
343 b->input = vb->input;
344 }
345
346 b->field = vb->field;
347 b->timestamp = vb->ts;
348 b->bytesused = vb->size;
349 b->sequence = vb->field_count >> 1;
350}
351
Brandon Philips19bc5132007-11-13 20:05:38 -0300352int videobuf_mmap_free(struct videobuf_queue *q)
353{
354 int ret;
Hans Verkuil97397682010-09-20 17:24:30 -0300355 videobuf_queue_lock(q);
Pawel Osciaka438d6d2010-05-11 10:36:29 -0300356 ret = __videobuf_free(q);
Hans Verkuil97397682010-09-20 17:24:30 -0300357 videobuf_queue_unlock(q);
Brandon Philips19bc5132007-11-13 20:05:38 -0300358 return ret;
359}
Pawel Osciak7a022642010-03-17 04:01:04 -0300360EXPORT_SYMBOL_GPL(videobuf_mmap_free);
Brandon Philips19bc5132007-11-13 20:05:38 -0300361
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300362/* Locking: Caller holds q->vb_lock */
Arjan van de Ven81b2dbc2008-05-20 09:53:52 -0700363int __videobuf_mmap_setup(struct videobuf_queue *q,
Brandon Philips19bc5132007-11-13 20:05:38 -0300364 unsigned int bcount, unsigned int bsize,
365 enum v4l2_memory memory)
366{
367 unsigned int i;
368 int err;
369
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300370 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
Brandon Philips19bc5132007-11-13 20:05:38 -0300371
Pawel Osciaka438d6d2010-05-11 10:36:29 -0300372 err = __videobuf_free(q);
Brandon Philips19bc5132007-11-13 20:05:38 -0300373 if (0 != err)
374 return err;
375
376 /* Allocate and initialize buffers */
377 for (i = 0; i < bcount; i++) {
Pawel Osciak33c38282010-05-11 10:36:28 -0300378 q->bufs[i] = videobuf_alloc_vb(q);
Brandon Philips19bc5132007-11-13 20:05:38 -0300379
Pawel Osciak7a022642010-03-17 04:01:04 -0300380 if (NULL == q->bufs[i])
Brandon Philips19bc5132007-11-13 20:05:38 -0300381 break;
382
383 q->bufs[i]->i = i;
384 q->bufs[i]->input = UNSET;
385 q->bufs[i]->memory = memory;
386 q->bufs[i]->bsize = bsize;
387 switch (memory) {
388 case V4L2_MEMORY_MMAP:
Tuukka Toivonen7cbefad2009-07-23 10:56:25 -0300389 q->bufs[i]->boff = PAGE_ALIGN(bsize) * i;
Brandon Philips19bc5132007-11-13 20:05:38 -0300390 break;
391 case V4L2_MEMORY_USERPTR:
392 case V4L2_MEMORY_OVERLAY:
393 /* nothing */
394 break;
395 }
396 }
397
398 if (!i)
399 return -ENOMEM;
400
Pawel Osciak7a022642010-03-17 04:01:04 -0300401 dprintk(1, "mmap setup: %d buffers, %d bytes each\n", i, bsize);
Brandon Philips19bc5132007-11-13 20:05:38 -0300402
403 return i;
404}
Pawel Osciak7a022642010-03-17 04:01:04 -0300405EXPORT_SYMBOL_GPL(__videobuf_mmap_setup);
Brandon Philips19bc5132007-11-13 20:05:38 -0300406
407int videobuf_mmap_setup(struct videobuf_queue *q,
408 unsigned int bcount, unsigned int bsize,
409 enum v4l2_memory memory)
410{
411 int ret;
Hans Verkuil97397682010-09-20 17:24:30 -0300412 videobuf_queue_lock(q);
Brandon Philips19bc5132007-11-13 20:05:38 -0300413 ret = __videobuf_mmap_setup(q, bcount, bsize, memory);
Hans Verkuil97397682010-09-20 17:24:30 -0300414 videobuf_queue_unlock(q);
Brandon Philips19bc5132007-11-13 20:05:38 -0300415 return ret;
416}
Pawel Osciak7a022642010-03-17 04:01:04 -0300417EXPORT_SYMBOL_GPL(videobuf_mmap_setup);
Brandon Philips19bc5132007-11-13 20:05:38 -0300418
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300419int videobuf_reqbufs(struct videobuf_queue *q,
420 struct v4l2_requestbuffers *req)
421{
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300422 unsigned int size, count;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300423 int retval;
424
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300425 if (req->count < 1) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300426 dprintk(1, "reqbufs: count invalid (%d)\n", req->count);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300427 return -EINVAL;
428 }
Brandon Philips19bc5132007-11-13 20:05:38 -0300429
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300430 if (req->memory != V4L2_MEMORY_MMAP &&
431 req->memory != V4L2_MEMORY_USERPTR &&
432 req->memory != V4L2_MEMORY_OVERLAY) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300433 dprintk(1, "reqbufs: memory type invalid\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300434 return -EINVAL;
435 }
436
Hans Verkuil97397682010-09-20 17:24:30 -0300437 videobuf_queue_lock(q);
Brandon Philips19bc5132007-11-13 20:05:38 -0300438 if (req->type != q->type) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300439 dprintk(1, "reqbufs: queue type invalid\n");
Brandon Philips19bc5132007-11-13 20:05:38 -0300440 retval = -EINVAL;
441 goto done;
442 }
443
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300444 if (q->streaming) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300445 dprintk(1, "reqbufs: streaming already exists\n");
Brandon Philips00f98d02007-09-27 20:55:28 -0300446 retval = -EBUSY;
447 goto done;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300448 }
449 if (!list_empty(&q->stream)) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300450 dprintk(1, "reqbufs: stream running\n");
Brandon Philips00f98d02007-09-27 20:55:28 -0300451 retval = -EBUSY;
452 goto done;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300453 }
454
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300455 count = req->count;
456 if (count > VIDEO_MAX_FRAME)
457 count = VIDEO_MAX_FRAME;
458 size = 0;
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300459 q->ops->buf_setup(q, &count, &size);
Márton Németh296372e2009-11-22 18:03:05 -0300460 dprintk(1, "reqbufs: bufs=%d, size=0x%x [%u pages total]\n",
461 count, size,
Pawel Osciak7a022642010-03-17 04:01:04 -0300462 (unsigned int)((count * PAGE_ALIGN(size)) >> PAGE_SHIFT));
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300463
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300464 retval = __videobuf_mmap_setup(q, count, size, req->memory);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300465 if (retval < 0) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300466 dprintk(1, "reqbufs: mmap setup returned %d\n", retval);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300467 goto done;
468 }
469
Brandon Philips49ee7182007-10-05 16:26:27 -0300470 req->count = retval;
Németh Márton925d74a2009-04-29 15:57:24 -0300471 retval = 0;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300472
473 done:
Hans Verkuil97397682010-09-20 17:24:30 -0300474 videobuf_queue_unlock(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300475 return retval;
476}
Pawel Osciak7a022642010-03-17 04:01:04 -0300477EXPORT_SYMBOL_GPL(videobuf_reqbufs);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300478
479int videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b)
480{
Brandon Philips19bc5132007-11-13 20:05:38 -0300481 int ret = -EINVAL;
482
Hans Verkuil97397682010-09-20 17:24:30 -0300483 videobuf_queue_lock(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300484 if (unlikely(b->type != q->type)) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300485 dprintk(1, "querybuf: Wrong type.\n");
Brandon Philips19bc5132007-11-13 20:05:38 -0300486 goto done;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300487 }
Roel Kluin223ffe52009-05-02 16:38:47 -0300488 if (unlikely(b->index >= VIDEO_MAX_FRAME)) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300489 dprintk(1, "querybuf: index out of range.\n");
Brandon Philips19bc5132007-11-13 20:05:38 -0300490 goto done;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300491 }
492 if (unlikely(NULL == q->bufs[b->index])) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300493 dprintk(1, "querybuf: buffer is null.\n");
Brandon Philips19bc5132007-11-13 20:05:38 -0300494 goto done;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300495 }
Brandon Philips19bc5132007-11-13 20:05:38 -0300496
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300497 videobuf_status(q, b, q->bufs[b->index], q->type);
Brandon Philips19bc5132007-11-13 20:05:38 -0300498
499 ret = 0;
500done:
Hans Verkuil97397682010-09-20 17:24:30 -0300501 videobuf_queue_unlock(q);
Brandon Philips19bc5132007-11-13 20:05:38 -0300502 return ret;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300503}
Pawel Osciak7a022642010-03-17 04:01:04 -0300504EXPORT_SYMBOL_GPL(videobuf_querybuf);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300505
Pawel Osciak7a022642010-03-17 04:01:04 -0300506int videobuf_qbuf(struct videobuf_queue *q, struct v4l2_buffer *b)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300507{
508 struct videobuf_buffer *buf;
509 enum v4l2_field field;
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300510 unsigned long flags = 0;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300511 int retval;
512
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300513 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300514
Maxim Levitsky99001322007-09-27 20:34:09 -0300515 if (b->memory == V4L2_MEMORY_MMAP)
516 down_read(&current->mm->mmap_sem);
517
Hans Verkuil97397682010-09-20 17:24:30 -0300518 videobuf_queue_lock(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300519 retval = -EBUSY;
520 if (q->reading) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300521 dprintk(1, "qbuf: Reading running...\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300522 goto done;
523 }
524 retval = -EINVAL;
525 if (b->type != q->type) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300526 dprintk(1, "qbuf: Wrong type.\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300527 goto done;
528 }
Roel Kluin223ffe52009-05-02 16:38:47 -0300529 if (b->index >= VIDEO_MAX_FRAME) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300530 dprintk(1, "qbuf: index out of range.\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300531 goto done;
532 }
533 buf = q->bufs[b->index];
534 if (NULL == buf) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300535 dprintk(1, "qbuf: buffer is null.\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300536 goto done;
537 }
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300538 MAGIC_CHECK(buf->magic, MAGIC_BUFFER);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300539 if (buf->memory != b->memory) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300540 dprintk(1, "qbuf: memory type is wrong.\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300541 goto done;
542 }
Brandon Philips0fc06862007-11-06 20:02:36 -0300543 if (buf->state != VIDEOBUF_NEEDS_INIT && buf->state != VIDEOBUF_IDLE) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300544 dprintk(1, "qbuf: buffer is already queued or active.\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300545 goto done;
546 }
547
548 if (b->flags & V4L2_BUF_FLAG_INPUT) {
549 if (b->input >= q->inputs) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300550 dprintk(1, "qbuf: wrong input.\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300551 goto done;
552 }
553 buf->input = b->input;
554 } else {
555 buf->input = UNSET;
556 }
557
558 switch (b->memory) {
559 case V4L2_MEMORY_MMAP:
560 if (0 == buf->baddr) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300561 dprintk(1, "qbuf: mmap requested "
562 "but buffer addr is zero!\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300563 goto done;
564 }
Pawel Osciak96f2ec62010-04-21 06:44:27 -0300565 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT
566 || q->type == V4L2_BUF_TYPE_VBI_OUTPUT
567 || q->type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
568 buf->size = b->bytesused;
569 buf->field = b->field;
570 buf->ts = b->timestamp;
571 }
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300572 break;
573 case V4L2_MEMORY_USERPTR:
574 if (b->length < buf->bsize) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300575 dprintk(1, "qbuf: buffer length is not enough\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300576 goto done;
577 }
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300578 if (VIDEOBUF_NEEDS_INIT != buf->state &&
579 buf->baddr != b->m.userptr)
580 q->ops->buf_release(q, buf);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300581 buf->baddr = b->m.userptr;
582 break;
583 case V4L2_MEMORY_OVERLAY:
584 buf->boff = b->m.offset;
585 break;
586 default:
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300587 dprintk(1, "qbuf: wrong memory type\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300588 goto done;
589 }
590
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300591 dprintk(1, "qbuf: requesting next field\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300592 field = videobuf_next_field(q);
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300593 retval = q->ops->buf_prepare(q, buf, field);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300594 if (0 != retval) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300595 dprintk(1, "qbuf: buffer_prepare returned %d\n", retval);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300596 goto done;
597 }
598
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300599 list_add_tail(&buf->stream, &q->stream);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300600 if (q->streaming) {
Brandon Philips0cf4dae2008-03-28 10:18:33 -0700601 spin_lock_irqsave(q->irqlock, flags);
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300602 q->ops->buf_queue(q, buf);
Brandon Philips0cf4dae2008-03-28 10:18:33 -0700603 spin_unlock_irqrestore(q->irqlock, flags);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300604 }
Hans Verkuil771075b2010-03-13 11:47:06 -0300605 dprintk(1, "qbuf: succeeded\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300606 retval = 0;
Brandon Philips137d1cb2008-04-02 18:10:59 -0300607 wake_up_interruptible_sync(&q->wait);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300608
Pawel Osciak7a022642010-03-17 04:01:04 -0300609done:
Hans Verkuil97397682010-09-20 17:24:30 -0300610 videobuf_queue_unlock(q);
Maxim Levitsky99001322007-09-27 20:34:09 -0300611
612 if (b->memory == V4L2_MEMORY_MMAP)
613 up_read(&current->mm->mmap_sem);
614
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300615 return retval;
616}
Pawel Osciak7a022642010-03-17 04:01:04 -0300617EXPORT_SYMBOL_GPL(videobuf_qbuf);
Brandon Philips137d1cb2008-04-02 18:10:59 -0300618
619/* Locking: Caller holds q->vb_lock */
620static int stream_next_buffer_check_queue(struct videobuf_queue *q, int noblock)
621{
622 int retval;
623
624checks:
625 if (!q->streaming) {
626 dprintk(1, "next_buffer: Not streaming\n");
627 retval = -EINVAL;
628 goto done;
629 }
630
631 if (list_empty(&q->stream)) {
632 if (noblock) {
633 retval = -EAGAIN;
634 dprintk(2, "next_buffer: no buffers to dequeue\n");
635 goto done;
636 } else {
637 dprintk(2, "next_buffer: waiting on buffer\n");
638
639 /* Drop lock to avoid deadlock with qbuf */
Hans Verkuil97397682010-09-20 17:24:30 -0300640 videobuf_queue_unlock(q);
Brandon Philips137d1cb2008-04-02 18:10:59 -0300641
642 /* Checking list_empty and streaming is safe without
643 * locks because we goto checks to validate while
644 * holding locks before proceeding */
645 retval = wait_event_interruptible(q->wait,
646 !list_empty(&q->stream) || !q->streaming);
Hans Verkuil97397682010-09-20 17:24:30 -0300647 videobuf_queue_lock(q);
Brandon Philips137d1cb2008-04-02 18:10:59 -0300648
649 if (retval)
650 goto done;
651
652 goto checks;
653 }
654 }
655
656 retval = 0;
657
658done:
659 return retval;
660}
661
Brandon Philips137d1cb2008-04-02 18:10:59 -0300662/* Locking: Caller holds q->vb_lock */
663static int stream_next_buffer(struct videobuf_queue *q,
664 struct videobuf_buffer **vb, int nonblocking)
665{
666 int retval;
667 struct videobuf_buffer *buf = NULL;
668
669 retval = stream_next_buffer_check_queue(q, nonblocking);
670 if (retval)
671 goto done;
672
673 buf = list_entry(q->stream.next, struct videobuf_buffer, stream);
674 retval = videobuf_waiton(buf, nonblocking, 1);
675 if (retval < 0)
676 goto done;
677
678 *vb = buf;
679done:
680 return retval;
681}
682
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300683int videobuf_dqbuf(struct videobuf_queue *q,
Pawel Osciak7a022642010-03-17 04:01:04 -0300684 struct v4l2_buffer *b, int nonblocking)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300685{
Brandon Philips137d1cb2008-04-02 18:10:59 -0300686 struct videobuf_buffer *buf = NULL;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300687 int retval;
688
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300689 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300690
Hans Verkuilb2dfd1a2010-04-28 04:05:22 -0300691 memset(b, 0, sizeof(*b));
Hans Verkuil97397682010-09-20 17:24:30 -0300692 videobuf_queue_lock(q);
Brandon Philips137d1cb2008-04-02 18:10:59 -0300693
694 retval = stream_next_buffer(q, &buf, nonblocking);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300695 if (retval < 0) {
Brandon Philips137d1cb2008-04-02 18:10:59 -0300696 dprintk(1, "dqbuf: next_buffer error: %i\n", retval);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300697 goto done;
698 }
Brandon Philips137d1cb2008-04-02 18:10:59 -0300699
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300700 switch (buf->state) {
Brandon Philips0fc06862007-11-06 20:02:36 -0300701 case VIDEOBUF_ERROR:
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300702 dprintk(1, "dqbuf: state is error\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300703 break;
Brandon Philips0fc06862007-11-06 20:02:36 -0300704 case VIDEOBUF_DONE:
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300705 dprintk(1, "dqbuf: state is done\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300706 break;
707 default:
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300708 dprintk(1, "dqbuf: state invalid\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300709 retval = -EINVAL;
710 goto done;
711 }
Hans Verkuilb2dfd1a2010-04-28 04:05:22 -0300712 CALL(q, sync, q, buf);
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300713 videobuf_status(q, b, buf, q->type);
Hans Verkuilb2dfd1a2010-04-28 04:05:22 -0300714 list_del(&buf->stream);
715 buf->state = VIDEOBUF_IDLE;
716 b->flags &= ~V4L2_BUF_FLAG_DONE;
Pawel Osciak7a022642010-03-17 04:01:04 -0300717done:
Hans Verkuil97397682010-09-20 17:24:30 -0300718 videobuf_queue_unlock(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300719 return retval;
720}
Pawel Osciak7a022642010-03-17 04:01:04 -0300721EXPORT_SYMBOL_GPL(videobuf_dqbuf);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300722
723int videobuf_streamon(struct videobuf_queue *q)
724{
725 struct videobuf_buffer *buf;
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300726 unsigned long flags = 0;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300727 int retval;
728
Hans Verkuil97397682010-09-20 17:24:30 -0300729 videobuf_queue_lock(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300730 retval = -EBUSY;
731 if (q->reading)
732 goto done;
733 retval = 0;
734 if (q->streaming)
735 goto done;
736 q->streaming = 1;
Brandon Philips0cf4dae2008-03-28 10:18:33 -0700737 spin_lock_irqsave(q->irqlock, flags);
Trent Piephoa991f442007-10-10 05:37:43 -0300738 list_for_each_entry(buf, &q->stream, stream)
Brandon Philips0fc06862007-11-06 20:02:36 -0300739 if (buf->state == VIDEOBUF_PREPARED)
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300740 q->ops->buf_queue(q, buf);
Brandon Philips0cf4dae2008-03-28 10:18:33 -0700741 spin_unlock_irqrestore(q->irqlock, flags);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300742
Brandon Philips137d1cb2008-04-02 18:10:59 -0300743 wake_up_interruptible_sync(&q->wait);
Pawel Osciak7a022642010-03-17 04:01:04 -0300744done:
Hans Verkuil97397682010-09-20 17:24:30 -0300745 videobuf_queue_unlock(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300746 return retval;
747}
Pawel Osciak7a022642010-03-17 04:01:04 -0300748EXPORT_SYMBOL_GPL(videobuf_streamon);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300749
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300750/* Locking: Caller holds q->vb_lock */
Brandon Philips19bc5132007-11-13 20:05:38 -0300751static int __videobuf_streamoff(struct videobuf_queue *q)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300752{
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300753 if (!q->streaming)
Brandon Philips19bc5132007-11-13 20:05:38 -0300754 return -EINVAL;
755
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300756 videobuf_queue_cancel(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300757
Brandon Philips19bc5132007-11-13 20:05:38 -0300758 return 0;
759}
760
761int videobuf_streamoff(struct videobuf_queue *q)
762{
763 int retval;
764
Hans Verkuil97397682010-09-20 17:24:30 -0300765 videobuf_queue_lock(q);
Brandon Philips19bc5132007-11-13 20:05:38 -0300766 retval = __videobuf_streamoff(q);
Hans Verkuil97397682010-09-20 17:24:30 -0300767 videobuf_queue_unlock(q);
Brandon Philips19bc5132007-11-13 20:05:38 -0300768
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300769 return retval;
770}
Pawel Osciak7a022642010-03-17 04:01:04 -0300771EXPORT_SYMBOL_GPL(videobuf_streamoff);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300772
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300773/* Locking: Caller holds q->vb_lock */
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300774static ssize_t videobuf_read_zerocopy(struct videobuf_queue *q,
775 char __user *data,
776 size_t count, loff_t *ppos)
777{
778 enum v4l2_field field;
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300779 unsigned long flags = 0;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300780 int retval;
781
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300782 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300783
784 /* setup stuff */
Pawel Osciak33c38282010-05-11 10:36:28 -0300785 q->read_buf = videobuf_alloc_vb(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300786 if (NULL == q->read_buf)
787 return -ENOMEM;
788
789 q->read_buf->memory = V4L2_MEMORY_USERPTR;
790 q->read_buf->baddr = (unsigned long)data;
791 q->read_buf->bsize = count;
792
793 field = videobuf_next_field(q);
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300794 retval = q->ops->buf_prepare(q, q->read_buf, field);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300795 if (0 != retval)
796 goto done;
797
798 /* start capture & wait */
Brandon Philips0cf4dae2008-03-28 10:18:33 -0700799 spin_lock_irqsave(q->irqlock, flags);
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300800 q->ops->buf_queue(q, q->read_buf);
Brandon Philips0cf4dae2008-03-28 10:18:33 -0700801 spin_unlock_irqrestore(q->irqlock, flags);
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300802 retval = videobuf_waiton(q->read_buf, 0, 0);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300803 if (0 == retval) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300804 CALL(q, sync, q, q->read_buf);
Brandon Philips0fc06862007-11-06 20:02:36 -0300805 if (VIDEOBUF_ERROR == q->read_buf->state)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300806 retval = -EIO;
807 else
808 retval = q->read_buf->size;
809 }
810
Pawel Osciak7a022642010-03-17 04:01:04 -0300811done:
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300812 /* cleanup */
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300813 q->ops->buf_release(q, q->read_buf);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300814 kfree(q->read_buf);
815 q->read_buf = NULL;
816 return retval;
817}
818
Hans Verkuil37111032010-03-28 09:22:53 -0300819static int __videobuf_copy_to_user(struct videobuf_queue *q,
820 struct videobuf_buffer *buf,
821 char __user *data, size_t count,
822 int nonblocking)
823{
824 void *vaddr = CALL(q, vaddr, buf);
825
826 /* copy to userspace */
827 if (count > buf->size - q->read_off)
828 count = buf->size - q->read_off;
829
830 if (copy_to_user(data, vaddr + q->read_off, count))
831 return -EFAULT;
832
833 return count;
834}
835
836static int __videobuf_copy_stream(struct videobuf_queue *q,
837 struct videobuf_buffer *buf,
838 char __user *data, size_t count, size_t pos,
839 int vbihack, int nonblocking)
840{
841 unsigned int *fc = CALL(q, vaddr, buf);
842
843 if (vbihack) {
844 /* dirty, undocumented hack -- pass the frame counter
845 * within the last four bytes of each vbi data block.
846 * We need that one to maintain backward compatibility
847 * to all vbi decoding software out there ... */
848 fc += (buf->size >> 2) - 1;
849 *fc = buf->field_count >> 1;
850 dprintk(1, "vbihack: %d\n", *fc);
851 }
852
853 /* copy stuff using the common method */
854 count = __videobuf_copy_to_user(q, buf, data, count, nonblocking);
855
856 if ((count == -EFAULT) && (pos == 0))
857 return -EFAULT;
858
859 return count;
860}
861
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300862ssize_t videobuf_read_one(struct videobuf_queue *q,
863 char __user *data, size_t count, loff_t *ppos,
864 int nonblocking)
865{
866 enum v4l2_field field;
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300867 unsigned long flags = 0;
Guennadi Liakhovetski7daa4a82008-04-22 14:46:03 -0300868 unsigned size = 0, nbufs = 1;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300869 int retval;
870
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300871 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300872
Hans Verkuil97397682010-09-20 17:24:30 -0300873 videobuf_queue_lock(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300874
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300875 q->ops->buf_setup(q, &nbufs, &size);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300876
877 if (NULL == q->read_buf &&
878 count >= size &&
879 !nonblocking) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300880 retval = videobuf_read_zerocopy(q, data, count, ppos);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300881 if (retval >= 0 || retval == -EIO)
882 /* ok, all done */
883 goto done;
884 /* fallback to kernel bounce buffer on failures */
885 }
886
887 if (NULL == q->read_buf) {
888 /* need to capture a new frame */
889 retval = -ENOMEM;
Pawel Osciak33c38282010-05-11 10:36:28 -0300890 q->read_buf = videobuf_alloc_vb(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300891
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300892 dprintk(1, "video alloc=0x%p\n", q->read_buf);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300893 if (NULL == q->read_buf)
894 goto done;
895 q->read_buf->memory = V4L2_MEMORY_USERPTR;
896 q->read_buf->bsize = count; /* preferred size */
897 field = videobuf_next_field(q);
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300898 retval = q->ops->buf_prepare(q, q->read_buf, field);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300899
900 if (0 != retval) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300901 kfree(q->read_buf);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300902 q->read_buf = NULL;
903 goto done;
904 }
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300905
Brandon Philips0cf4dae2008-03-28 10:18:33 -0700906 spin_lock_irqsave(q->irqlock, flags);
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300907 q->ops->buf_queue(q, q->read_buf);
Brandon Philips0cf4dae2008-03-28 10:18:33 -0700908 spin_unlock_irqrestore(q->irqlock, flags);
909
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300910 q->read_off = 0;
911 }
912
913 /* wait until capture is done */
914 retval = videobuf_waiton(q->read_buf, nonblocking, 1);
915 if (0 != retval)
916 goto done;
917
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300918 CALL(q, sync, q, q->read_buf);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300919
Brandon Philips0fc06862007-11-06 20:02:36 -0300920 if (VIDEOBUF_ERROR == q->read_buf->state) {
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300921 /* catch I/O errors */
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300922 q->ops->buf_release(q, q->read_buf);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300923 kfree(q->read_buf);
924 q->read_buf = NULL;
925 retval = -EIO;
926 goto done;
927 }
928
929 /* Copy to userspace */
Hans Verkuil37111032010-03-28 09:22:53 -0300930 retval = __videobuf_copy_to_user(q, q->read_buf, data, count, nonblocking);
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300931 if (retval < 0)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300932 goto done;
933
934 q->read_off += retval;
935 if (q->read_off == q->read_buf->size) {
936 /* all data copied, cleanup */
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300937 q->ops->buf_release(q, q->read_buf);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300938 kfree(q->read_buf);
939 q->read_buf = NULL;
940 }
941
Pawel Osciak7a022642010-03-17 04:01:04 -0300942done:
Hans Verkuil97397682010-09-20 17:24:30 -0300943 videobuf_queue_unlock(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300944 return retval;
945}
Pawel Osciak7a022642010-03-17 04:01:04 -0300946EXPORT_SYMBOL_GPL(videobuf_read_one);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300947
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300948/* Locking: Caller holds q->vb_lock */
Mauro Carvalho Chehab225ba902007-12-12 21:46:26 -0300949static int __videobuf_read_start(struct videobuf_queue *q)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300950{
951 enum v4l2_field field;
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300952 unsigned long flags = 0;
Brandon Philips49ee7182007-10-05 16:26:27 -0300953 unsigned int count = 0, size = 0;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300954 int err, i;
955
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300956 q->ops->buf_setup(q, &count, &size);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300957 if (count < 2)
958 count = 2;
959 if (count > VIDEO_MAX_FRAME)
960 count = VIDEO_MAX_FRAME;
961 size = PAGE_ALIGN(size);
962
Brandon Philips19bc5132007-11-13 20:05:38 -0300963 err = __videobuf_mmap_setup(q, count, size, V4L2_MEMORY_USERPTR);
Brandon Philips49ee7182007-10-05 16:26:27 -0300964 if (err < 0)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300965 return err;
966
Brandon Philips49ee7182007-10-05 16:26:27 -0300967 count = err;
968
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300969 for (i = 0; i < count; i++) {
970 field = videobuf_next_field(q);
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300971 err = q->ops->buf_prepare(q, q->bufs[i], field);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300972 if (err)
973 return err;
974 list_add_tail(&q->bufs[i]->stream, &q->stream);
975 }
Brandon Philips0cf4dae2008-03-28 10:18:33 -0700976 spin_lock_irqsave(q->irqlock, flags);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300977 for (i = 0; i < count; i++)
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300978 q->ops->buf_queue(q, q->bufs[i]);
Brandon Philips0cf4dae2008-03-28 10:18:33 -0700979 spin_unlock_irqrestore(q->irqlock, flags);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300980 q->reading = 1;
981 return 0;
982}
983
Brandon Philips19bc5132007-11-13 20:05:38 -0300984static void __videobuf_read_stop(struct videobuf_queue *q)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300985{
986 int i;
987
988 videobuf_queue_cancel(q);
Pawel Osciaka438d6d2010-05-11 10:36:29 -0300989 __videobuf_free(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300990 INIT_LIST_HEAD(&q->stream);
991 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
992 if (NULL == q->bufs[i])
993 continue;
994 kfree(q->bufs[i]);
995 q->bufs[i] = NULL;
996 }
997 q->read_buf = NULL;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300998}
999
Mauro Carvalho Chehab19fb1452007-11-15 23:09:30 -03001000int videobuf_read_start(struct videobuf_queue *q)
1001{
1002 int rc;
1003
Hans Verkuil97397682010-09-20 17:24:30 -03001004 videobuf_queue_lock(q);
Mauro Carvalho Chehab19fb1452007-11-15 23:09:30 -03001005 rc = __videobuf_read_start(q);
Hans Verkuil97397682010-09-20 17:24:30 -03001006 videobuf_queue_unlock(q);
Mauro Carvalho Chehab19fb1452007-11-15 23:09:30 -03001007
1008 return rc;
1009}
Pawel Osciak7a022642010-03-17 04:01:04 -03001010EXPORT_SYMBOL_GPL(videobuf_read_start);
Mauro Carvalho Chehab19fb1452007-11-15 23:09:30 -03001011
Brandon Philips19bc5132007-11-13 20:05:38 -03001012void videobuf_read_stop(struct videobuf_queue *q)
1013{
Hans Verkuil97397682010-09-20 17:24:30 -03001014 videobuf_queue_lock(q);
Brandon Philips19bc5132007-11-13 20:05:38 -03001015 __videobuf_read_stop(q);
Hans Verkuil97397682010-09-20 17:24:30 -03001016 videobuf_queue_unlock(q);
Brandon Philips19bc5132007-11-13 20:05:38 -03001017}
Pawel Osciak7a022642010-03-17 04:01:04 -03001018EXPORT_SYMBOL_GPL(videobuf_read_stop);
Brandon Philips19bc5132007-11-13 20:05:38 -03001019
1020void videobuf_stop(struct videobuf_queue *q)
1021{
Hans Verkuil97397682010-09-20 17:24:30 -03001022 videobuf_queue_lock(q);
Brandon Philips19bc5132007-11-13 20:05:38 -03001023
1024 if (q->streaming)
1025 __videobuf_streamoff(q);
1026
1027 if (q->reading)
1028 __videobuf_read_stop(q);
1029
Hans Verkuil97397682010-09-20 17:24:30 -03001030 videobuf_queue_unlock(q);
Brandon Philips19bc5132007-11-13 20:05:38 -03001031}
Pawel Osciak7a022642010-03-17 04:01:04 -03001032EXPORT_SYMBOL_GPL(videobuf_stop);
Brandon Philips19bc5132007-11-13 20:05:38 -03001033
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001034ssize_t videobuf_read_stream(struct videobuf_queue *q,
1035 char __user *data, size_t count, loff_t *ppos,
1036 int vbihack, int nonblocking)
1037{
1038 int rc, retval;
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -03001039 unsigned long flags = 0;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001040
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -03001041 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001042
Harvey Harrison7e28adb2008-04-08 23:20:00 -03001043 dprintk(2, "%s\n", __func__);
Hans Verkuil97397682010-09-20 17:24:30 -03001044 videobuf_queue_lock(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001045 retval = -EBUSY;
1046 if (q->streaming)
1047 goto done;
1048 if (!q->reading) {
Adrian Bunk3f843072007-12-12 16:44:54 -03001049 retval = __videobuf_read_start(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001050 if (retval < 0)
1051 goto done;
1052 }
1053
1054 retval = 0;
1055 while (count > 0) {
1056 /* get / wait for data */
1057 if (NULL == q->read_buf) {
1058 q->read_buf = list_entry(q->stream.next,
1059 struct videobuf_buffer,
1060 stream);
1061 list_del(&q->read_buf->stream);
1062 q->read_off = 0;
1063 }
1064 rc = videobuf_waiton(q->read_buf, nonblocking, 1);
1065 if (rc < 0) {
1066 if (0 == retval)
1067 retval = rc;
1068 break;
1069 }
1070
Brandon Philips0fc06862007-11-06 20:02:36 -03001071 if (q->read_buf->state == VIDEOBUF_DONE) {
Hans Verkuil37111032010-03-28 09:22:53 -03001072 rc = __videobuf_copy_stream(q, q->read_buf, data + retval, count,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001073 retval, vbihack, nonblocking);
1074 if (rc < 0) {
1075 retval = rc;
1076 break;
1077 }
1078 retval += rc;
1079 count -= rc;
1080 q->read_off += rc;
1081 } else {
1082 /* some error */
1083 q->read_off = q->read_buf->size;
1084 if (0 == retval)
1085 retval = -EIO;
1086 }
1087
1088 /* requeue buffer when done with copying */
1089 if (q->read_off == q->read_buf->size) {
1090 list_add_tail(&q->read_buf->stream,
1091 &q->stream);
Brandon Philips0cf4dae2008-03-28 10:18:33 -07001092 spin_lock_irqsave(q->irqlock, flags);
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -03001093 q->ops->buf_queue(q, q->read_buf);
Brandon Philips0cf4dae2008-03-28 10:18:33 -07001094 spin_unlock_irqrestore(q->irqlock, flags);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001095 q->read_buf = NULL;
1096 }
1097 if (retval < 0)
1098 break;
1099 }
1100
Pawel Osciak7a022642010-03-17 04:01:04 -03001101done:
Hans Verkuil97397682010-09-20 17:24:30 -03001102 videobuf_queue_unlock(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001103 return retval;
1104}
Pawel Osciak7a022642010-03-17 04:01:04 -03001105EXPORT_SYMBOL_GPL(videobuf_read_stream);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001106
1107unsigned int videobuf_poll_stream(struct file *file,
1108 struct videobuf_queue *q,
1109 poll_table *wait)
1110{
1111 struct videobuf_buffer *buf = NULL;
1112 unsigned int rc = 0;
1113
Hans Verkuil97397682010-09-20 17:24:30 -03001114 videobuf_queue_lock(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001115 if (q->streaming) {
1116 if (!list_empty(&q->stream))
1117 buf = list_entry(q->stream.next,
1118 struct videobuf_buffer, stream);
1119 } else {
1120 if (!q->reading)
Adrian Bunk3f843072007-12-12 16:44:54 -03001121 __videobuf_read_start(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001122 if (!q->reading) {
1123 rc = POLLERR;
1124 } else if (NULL == q->read_buf) {
1125 q->read_buf = list_entry(q->stream.next,
1126 struct videobuf_buffer,
1127 stream);
1128 list_del(&q->read_buf->stream);
1129 q->read_off = 0;
1130 }
1131 buf = q->read_buf;
1132 }
1133 if (!buf)
1134 rc = POLLERR;
1135
1136 if (0 == rc) {
1137 poll_wait(file, &buf->done, wait);
Brandon Philips0fc06862007-11-06 20:02:36 -03001138 if (buf->state == VIDEOBUF_DONE ||
Pawel Osciak9b558432010-03-29 05:16:31 -03001139 buf->state == VIDEOBUF_ERROR) {
1140 switch (q->type) {
1141 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1142 case V4L2_BUF_TYPE_VBI_OUTPUT:
1143 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1144 rc = POLLOUT | POLLWRNORM;
1145 break;
1146 default:
1147 rc = POLLIN | POLLRDNORM;
1148 break;
1149 }
1150 }
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001151 }
Hans Verkuil97397682010-09-20 17:24:30 -03001152 videobuf_queue_unlock(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001153 return rc;
1154}
Pawel Osciak7a022642010-03-17 04:01:04 -03001155EXPORT_SYMBOL_GPL(videobuf_poll_stream);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001156
Pawel Osciak7a022642010-03-17 04:01:04 -03001157int videobuf_mmap_mapper(struct videobuf_queue *q, struct vm_area_struct *vma)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001158{
Hans Verkuil0b62b732010-03-28 09:09:05 -03001159 int rc = -EINVAL;
1160 int i;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001161
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -03001162 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001163
Hans Verkuil0b62b732010-03-28 09:09:05 -03001164 if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED)) {
1165 dprintk(1, "mmap appl bug: PROT_WRITE and MAP_SHARED are required\n");
1166 return -EINVAL;
1167 }
1168
Hans Verkuil97397682010-09-20 17:24:30 -03001169 videobuf_queue_lock(q);
Hans Verkuil0b62b732010-03-28 09:09:05 -03001170 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1171 struct videobuf_buffer *buf = q->bufs[i];
1172
1173 if (buf && buf->memory == V4L2_MEMORY_MMAP &&
1174 buf->boff == (vma->vm_pgoff << PAGE_SHIFT)) {
1175 rc = CALL(q, mmap_mapper, q, buf, vma);
1176 break;
1177 }
1178 }
Hans Verkuil97397682010-09-20 17:24:30 -03001179 videobuf_queue_unlock(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001180
Hans Verkuil0b62b732010-03-28 09:09:05 -03001181 return rc;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001182}
Pawel Osciak7a022642010-03-17 04:01:04 -03001183EXPORT_SYMBOL_GPL(videobuf_mmap_mapper);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001184
1185#ifdef CONFIG_VIDEO_V4L1_COMPAT
1186int videobuf_cgmbuf(struct videobuf_queue *q,
1187 struct video_mbuf *mbuf, int count)
1188{
1189 struct v4l2_requestbuffers req;
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -03001190 int rc, i;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001191
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -03001192 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001193
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -03001194 memset(&req, 0, sizeof(req));
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001195 req.type = q->type;
1196 req.count = count;
1197 req.memory = V4L2_MEMORY_MMAP;
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -03001198 rc = videobuf_reqbufs(q, &req);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001199 if (rc < 0)
1200 return rc;
1201
1202 mbuf->frames = req.count;
1203 mbuf->size = 0;
1204 for (i = 0; i < mbuf->frames; i++) {
1205 mbuf->offsets[i] = q->bufs[i]->boff;
Tuukka Toivonen7cbefad2009-07-23 10:56:25 -03001206 mbuf->size += PAGE_ALIGN(q->bufs[i]->bsize);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001207 }
1208
1209 return 0;
1210}
Pekka Enberga13625c2007-10-13 05:54:54 -03001211EXPORT_SYMBOL_GPL(videobuf_cgmbuf);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001212#endif
1213