blob: def84753c4c32a41230237aa686a89ccd03143f7 [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)
Hans Verkuila8d54e42014-11-05 04:51:26 -030054#define CALLPTR(q, f, arg...) \
55 ((q->int_ops->f) ? q->int_ops->f(arg) : NULL)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030056
Pawel Osciak33c38282010-05-11 10:36:28 -030057struct videobuf_buffer *videobuf_alloc_vb(struct videobuf_queue *q)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030058{
59 struct videobuf_buffer *vb;
60
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -030061 BUG_ON(q->msize < sizeof(*vb));
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030062
Pawel Osciak33c38282010-05-11 10:36:28 -030063 if (!q->int_ops || !q->int_ops->alloc_vb) {
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030064 printk(KERN_ERR "No specific ops defined!\n");
65 BUG();
66 }
67
Pawel Osciak33c38282010-05-11 10:36:28 -030068 vb = q->int_ops->alloc_vb(q->msize);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030069 if (NULL != vb) {
70 init_waitqueue_head(&vb->done);
Pawel Osciak7a022642010-03-17 04:01:04 -030071 vb->magic = MAGIC_BUFFER;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030072 }
73
74 return vb;
75}
Pawel Osciak33c38282010-05-11 10:36:28 -030076EXPORT_SYMBOL_GPL(videobuf_alloc_vb);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030077
Guennadi Liakhovetski5eba1726e2016-02-21 13:34:59 -030078static int state_neither_active_nor_queued(struct videobuf_queue *q,
79 struct videobuf_buffer *vb)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030080{
Hans Verkuil0e0809a2010-09-26 09:01:26 -030081 unsigned long flags;
82 bool rc;
83
84 spin_lock_irqsave(q->irqlock, flags);
85 rc = vb->state != VIDEOBUF_ACTIVE && vb->state != VIDEOBUF_QUEUED;
86 spin_unlock_irqrestore(q->irqlock, flags);
87 return rc;
88};
89
90int videobuf_waiton(struct videobuf_queue *q, struct videobuf_buffer *vb,
91 int non_blocking, int intr)
92{
93 bool is_ext_locked;
94 int ret = 0;
95
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -030096 MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
Brandon Philips009a9052008-04-02 18:10:59 -030097
98 if (non_blocking) {
Guennadi Liakhovetski5eba1726e2016-02-21 13:34:59 -030099 if (state_neither_active_nor_queued(q, vb))
Brandon Philips009a9052008-04-02 18:10:59 -0300100 return 0;
Hans Verkuil0e0809a2010-09-26 09:01:26 -0300101 return -EAGAIN;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300102 }
Brandon Philips009a9052008-04-02 18:10:59 -0300103
Hans Verkuil0e0809a2010-09-26 09:01:26 -0300104 is_ext_locked = q->ext_lock && mutex_is_locked(q->ext_lock);
Brandon Philips009a9052008-04-02 18:10:59 -0300105
Hans Verkuil0e0809a2010-09-26 09:01:26 -0300106 /* Release vdev lock to prevent this wait from blocking outside access to
107 the device. */
108 if (is_ext_locked)
109 mutex_unlock(q->ext_lock);
110 if (intr)
Guennadi Liakhovetski5eba1726e2016-02-21 13:34:59 -0300111 ret = wait_event_interruptible(vb->done,
112 state_neither_active_nor_queued(q, vb));
Hans Verkuil0e0809a2010-09-26 09:01:26 -0300113 else
Guennadi Liakhovetski5eba1726e2016-02-21 13:34:59 -0300114 wait_event(vb->done, state_neither_active_nor_queued(q, vb));
Hans Verkuil0e0809a2010-09-26 09:01:26 -0300115 /* Relock */
116 if (is_ext_locked)
117 mutex_lock(q->ext_lock);
118
119 return ret;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300120}
Pawel Osciak7a022642010-03-17 04:01:04 -0300121EXPORT_SYMBOL_GPL(videobuf_waiton);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300122
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300123int videobuf_iolock(struct videobuf_queue *q, struct videobuf_buffer *vb,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300124 struct v4l2_framebuffer *fbuf)
125{
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300126 MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
127 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300128
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300129 return CALL(q, iolock, q, vb, fbuf);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300130}
Pawel Osciak7a022642010-03-17 04:01:04 -0300131EXPORT_SYMBOL_GPL(videobuf_iolock);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300132
Hans Verkuilf4fce602010-03-28 08:33:23 -0300133void *videobuf_queue_to_vaddr(struct videobuf_queue *q,
134 struct videobuf_buffer *buf)
Mauro Carvalho Chehab59d34482008-04-13 15:10:00 -0300135{
Hans Verkuil037c75e2010-03-28 08:18:37 -0300136 if (q->int_ops->vaddr)
137 return q->int_ops->vaddr(buf);
Hans Verkuilf4fce602010-03-28 08:33:23 -0300138 return NULL;
Mauro Carvalho Chehab59d34482008-04-13 15:10:00 -0300139}
Hans Verkuilf4fce602010-03-28 08:33:23 -0300140EXPORT_SYMBOL_GPL(videobuf_queue_to_vaddr);
Mauro Carvalho Chehab59d34482008-04-13 15:10:00 -0300141
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300142/* --------------------------------------------------------------------- */
143
144
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300145void videobuf_queue_core_init(struct videobuf_queue *q,
Jonathan Corbet38a54f32009-11-17 19:43:41 -0300146 const struct videobuf_queue_ops *ops,
Guennadi Liakhovetskie9bcf662008-04-22 14:46:02 -0300147 struct device *dev,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300148 spinlock_t *irqlock,
149 enum v4l2_buf_type type,
150 enum v4l2_field field,
151 unsigned int msize,
Mauro Carvalho Chehabd4cae5a2007-10-08 12:20:02 -0300152 void *priv,
Hans Verkuil08bff032010-09-20 17:39:46 -0300153 struct videobuf_qtype_ops *int_ops,
154 struct mutex *ext_lock)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300155{
Figo.zhang96ceea22009-06-02 23:01:04 -0300156 BUG_ON(!q);
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300157 memset(q, 0, sizeof(*q));
Mauro Carvalho Chehabd4cae5a2007-10-08 12:20:02 -0300158 q->irqlock = irqlock;
Hans Verkuil08bff032010-09-20 17:39:46 -0300159 q->ext_lock = ext_lock;
Mauro Carvalho Chehabd4cae5a2007-10-08 12:20:02 -0300160 q->dev = dev;
161 q->type = type;
162 q->field = field;
163 q->msize = msize;
164 q->ops = ops;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300165 q->priv_data = priv;
Mauro Carvalho Chehabd4cae5a2007-10-08 12:20:02 -0300166 q->int_ops = int_ops;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300167
168 /* All buffer operations are mandatory */
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300169 BUG_ON(!q->ops->buf_setup);
170 BUG_ON(!q->ops->buf_prepare);
171 BUG_ON(!q->ops->buf_queue);
172 BUG_ON(!q->ops->buf_release);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300173
Brandon Philips0cf4dae2008-03-28 10:18:33 -0700174 /* Lock is mandatory for queue_cancel to work */
175 BUG_ON(!irqlock);
176
Mauro Carvalho Chehabd4cae5a2007-10-08 12:20:02 -0300177 /* Having implementations for abstract methods are mandatory */
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300178 BUG_ON(!q->int_ops);
Mauro Carvalho Chehabd4cae5a2007-10-08 12:20:02 -0300179
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300180 mutex_init(&q->vb_lock);
Brandon Philips137d1cb2008-04-02 18:10:59 -0300181 init_waitqueue_head(&q->wait);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300182 INIT_LIST_HEAD(&q->stream);
183}
Pawel Osciak7a022642010-03-17 04:01:04 -0300184EXPORT_SYMBOL_GPL(videobuf_queue_core_init);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300185
Brandon Philips19bc5132007-11-13 20:05:38 -0300186/* Locking: Only usage in bttv unsafe find way to remove */
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300187int videobuf_queue_is_busy(struct videobuf_queue *q)
188{
189 int i;
190
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300191 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300192
193 if (q->streaming) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300194 dprintk(1, "busy: streaming active\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300195 return 1;
196 }
197 if (q->reading) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300198 dprintk(1, "busy: pending read #1\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300199 return 1;
200 }
201 if (q->read_buf) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300202 dprintk(1, "busy: pending read #2\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300203 return 1;
204 }
205 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
206 if (NULL == q->bufs[i])
207 continue;
Mauro Carvalho Chehab851c0c92007-09-27 18:25:44 -0300208 if (q->bufs[i]->map) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300209 dprintk(1, "busy: buffer #%d mapped\n", i);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300210 return 1;
211 }
Brandon Philips0fc06862007-11-06 20:02:36 -0300212 if (q->bufs[i]->state == VIDEOBUF_QUEUED) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300213 dprintk(1, "busy: buffer #%d queued\n", i);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300214 return 1;
215 }
Brandon Philips0fc06862007-11-06 20:02:36 -0300216 if (q->bufs[i]->state == VIDEOBUF_ACTIVE) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300217 dprintk(1, "busy: buffer #%d avtive\n", i);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300218 return 1;
219 }
220 }
221 return 0;
222}
Pawel Osciak7a022642010-03-17 04:01:04 -0300223EXPORT_SYMBOL_GPL(videobuf_queue_is_busy);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300224
Pawel Osciaka438d6d2010-05-11 10:36:29 -0300225/**
226 * __videobuf_free() - free all the buffers and their control structures
227 *
228 * This function can only be called if streaming/reading is off, i.e. no buffers
229 * are under control of the driver.
230 */
231/* Locking: Caller holds q->vb_lock */
232static int __videobuf_free(struct videobuf_queue *q)
233{
234 int i;
235
236 dprintk(1, "%s\n", __func__);
237 if (!q)
238 return 0;
239
240 if (q->streaming || q->reading) {
241 dprintk(1, "Cannot free buffers when streaming or reading\n");
242 return -EBUSY;
243 }
244
245 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
246
247 for (i = 0; i < VIDEO_MAX_FRAME; i++)
248 if (q->bufs[i] && q->bufs[i]->map) {
249 dprintk(1, "Cannot free mmapped buffers\n");
250 return -EBUSY;
251 }
252
253 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
254 if (NULL == q->bufs[i])
255 continue;
256 q->ops->buf_release(q, q->bufs[i]);
257 kfree(q->bufs[i]);
258 q->bufs[i] = NULL;
259 }
260
261 return 0;
262}
263
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300264/* Locking: Caller holds q->vb_lock */
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300265void videobuf_queue_cancel(struct videobuf_queue *q)
266{
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300267 unsigned long flags = 0;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300268 int i;
269
Brandon Philips137d1cb2008-04-02 18:10:59 -0300270 q->streaming = 0;
271 q->reading = 0;
272 wake_up_interruptible_sync(&q->wait);
273
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300274 /* remove queued buffers from list */
Brandon Philips0cf4dae2008-03-28 10:18:33 -0700275 spin_lock_irqsave(q->irqlock, flags);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300276 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
277 if (NULL == q->bufs[i])
278 continue;
Brandon Philips0fc06862007-11-06 20:02:36 -0300279 if (q->bufs[i]->state == VIDEOBUF_QUEUED) {
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300280 list_del(&q->bufs[i]->queue);
Brandon Philips0fc06862007-11-06 20:02:36 -0300281 q->bufs[i]->state = VIDEOBUF_ERROR;
Brandon Philipsb608f432008-04-02 18:10:57 -0300282 wake_up_all(&q->bufs[i]->done);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300283 }
284 }
Brandon Philips0cf4dae2008-03-28 10:18:33 -0700285 spin_unlock_irqrestore(q->irqlock, flags);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300286
287 /* free all buffers + clear queue */
288 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
289 if (NULL == q->bufs[i])
290 continue;
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300291 q->ops->buf_release(q, q->bufs[i]);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300292 }
293 INIT_LIST_HEAD(&q->stream);
294}
Pawel Osciak7a022642010-03-17 04:01:04 -0300295EXPORT_SYMBOL_GPL(videobuf_queue_cancel);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300296
297/* --------------------------------------------------------------------- */
298
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300299/* Locking: Caller holds q->vb_lock */
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300300enum v4l2_field videobuf_next_field(struct videobuf_queue *q)
301{
302 enum v4l2_field field = q->field;
303
304 BUG_ON(V4L2_FIELD_ANY == field);
305
306 if (V4L2_FIELD_ALTERNATE == field) {
307 if (V4L2_FIELD_TOP == q->last) {
308 field = V4L2_FIELD_BOTTOM;
309 q->last = V4L2_FIELD_BOTTOM;
310 } else {
311 field = V4L2_FIELD_TOP;
312 q->last = V4L2_FIELD_TOP;
313 }
314 }
315 return field;
316}
Pawel Osciak7a022642010-03-17 04:01:04 -0300317EXPORT_SYMBOL_GPL(videobuf_next_field);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300318
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300319/* Locking: Caller holds q->vb_lock */
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300320static void videobuf_status(struct videobuf_queue *q, struct v4l2_buffer *b,
321 struct videobuf_buffer *vb, enum v4l2_buf_type type)
322{
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300323 MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
324 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300325
326 b->index = vb->i;
327 b->type = type;
328
329 b->memory = vb->memory;
330 switch (b->memory) {
331 case V4L2_MEMORY_MMAP:
332 b->m.offset = vb->boff;
333 b->length = vb->bsize;
334 break;
335 case V4L2_MEMORY_USERPTR:
336 b->m.userptr = vb->baddr;
337 b->length = vb->bsize;
338 break;
339 case V4L2_MEMORY_OVERLAY:
340 b->m.offset = vb->boff;
341 break;
Sumit Semwal3c3016b2012-06-14 10:37:38 -0300342 case V4L2_MEMORY_DMABUF:
343 /* DMABUF is not handled in videobuf framework */
344 break;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300345 }
346
Sakari Ailus1b18e7a2012-10-22 17:10:16 -0300347 b->flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
Mauro Carvalho Chehab851c0c92007-09-27 18:25:44 -0300348 if (vb->map)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300349 b->flags |= V4L2_BUF_FLAG_MAPPED;
350
351 switch (vb->state) {
Brandon Philips0fc06862007-11-06 20:02:36 -0300352 case VIDEOBUF_PREPARED:
353 case VIDEOBUF_QUEUED:
354 case VIDEOBUF_ACTIVE:
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300355 b->flags |= V4L2_BUF_FLAG_QUEUED;
356 break;
Brandon Philips0fc06862007-11-06 20:02:36 -0300357 case VIDEOBUF_ERROR:
Hans Verkuilb2dfd1a2010-04-28 04:05:22 -0300358 b->flags |= V4L2_BUF_FLAG_ERROR;
359 /* fall through */
360 case VIDEOBUF_DONE:
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300361 b->flags |= V4L2_BUF_FLAG_DONE;
362 break;
Brandon Philips0fc06862007-11-06 20:02:36 -0300363 case VIDEOBUF_NEEDS_INIT:
364 case VIDEOBUF_IDLE:
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300365 /* nothing */
366 break;
367 }
368
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300369 b->field = vb->field;
370 b->timestamp = vb->ts;
371 b->bytesused = vb->size;
372 b->sequence = vb->field_count >> 1;
373}
374
Brandon Philips19bc5132007-11-13 20:05:38 -0300375int videobuf_mmap_free(struct videobuf_queue *q)
376{
377 int ret;
Hans Verkuil97397682010-09-20 17:24:30 -0300378 videobuf_queue_lock(q);
Pawel Osciaka438d6d2010-05-11 10:36:29 -0300379 ret = __videobuf_free(q);
Hans Verkuil97397682010-09-20 17:24:30 -0300380 videobuf_queue_unlock(q);
Brandon Philips19bc5132007-11-13 20:05:38 -0300381 return ret;
382}
Pawel Osciak7a022642010-03-17 04:01:04 -0300383EXPORT_SYMBOL_GPL(videobuf_mmap_free);
Brandon Philips19bc5132007-11-13 20:05:38 -0300384
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300385/* Locking: Caller holds q->vb_lock */
Arjan van de Ven81b2dbc2008-05-20 09:53:52 -0700386int __videobuf_mmap_setup(struct videobuf_queue *q,
Brandon Philips19bc5132007-11-13 20:05:38 -0300387 unsigned int bcount, unsigned int bsize,
388 enum v4l2_memory memory)
389{
390 unsigned int i;
391 int err;
392
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300393 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
Brandon Philips19bc5132007-11-13 20:05:38 -0300394
Pawel Osciaka438d6d2010-05-11 10:36:29 -0300395 err = __videobuf_free(q);
Brandon Philips19bc5132007-11-13 20:05:38 -0300396 if (0 != err)
397 return err;
398
399 /* Allocate and initialize buffers */
400 for (i = 0; i < bcount; i++) {
Pawel Osciak33c38282010-05-11 10:36:28 -0300401 q->bufs[i] = videobuf_alloc_vb(q);
Brandon Philips19bc5132007-11-13 20:05:38 -0300402
Pawel Osciak7a022642010-03-17 04:01:04 -0300403 if (NULL == q->bufs[i])
Brandon Philips19bc5132007-11-13 20:05:38 -0300404 break;
405
406 q->bufs[i]->i = i;
Brandon Philips19bc5132007-11-13 20:05:38 -0300407 q->bufs[i]->memory = memory;
408 q->bufs[i]->bsize = bsize;
409 switch (memory) {
410 case V4L2_MEMORY_MMAP:
Tuukka Toivonen7cbefad2009-07-23 10:56:25 -0300411 q->bufs[i]->boff = PAGE_ALIGN(bsize) * i;
Brandon Philips19bc5132007-11-13 20:05:38 -0300412 break;
413 case V4L2_MEMORY_USERPTR:
414 case V4L2_MEMORY_OVERLAY:
Sumit Semwal3c3016b2012-06-14 10:37:38 -0300415 case V4L2_MEMORY_DMABUF:
Brandon Philips19bc5132007-11-13 20:05:38 -0300416 /* nothing */
417 break;
418 }
419 }
420
421 if (!i)
422 return -ENOMEM;
423
Pawel Osciak7a022642010-03-17 04:01:04 -0300424 dprintk(1, "mmap setup: %d buffers, %d bytes each\n", i, bsize);
Brandon Philips19bc5132007-11-13 20:05:38 -0300425
426 return i;
427}
Pawel Osciak7a022642010-03-17 04:01:04 -0300428EXPORT_SYMBOL_GPL(__videobuf_mmap_setup);
Brandon Philips19bc5132007-11-13 20:05:38 -0300429
430int videobuf_mmap_setup(struct videobuf_queue *q,
431 unsigned int bcount, unsigned int bsize,
432 enum v4l2_memory memory)
433{
434 int ret;
Hans Verkuil97397682010-09-20 17:24:30 -0300435 videobuf_queue_lock(q);
Brandon Philips19bc5132007-11-13 20:05:38 -0300436 ret = __videobuf_mmap_setup(q, bcount, bsize, memory);
Hans Verkuil97397682010-09-20 17:24:30 -0300437 videobuf_queue_unlock(q);
Brandon Philips19bc5132007-11-13 20:05:38 -0300438 return ret;
439}
Pawel Osciak7a022642010-03-17 04:01:04 -0300440EXPORT_SYMBOL_GPL(videobuf_mmap_setup);
Brandon Philips19bc5132007-11-13 20:05:38 -0300441
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300442int videobuf_reqbufs(struct videobuf_queue *q,
443 struct v4l2_requestbuffers *req)
444{
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300445 unsigned int size, count;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300446 int retval;
447
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300448 if (req->memory != V4L2_MEMORY_MMAP &&
449 req->memory != V4L2_MEMORY_USERPTR &&
450 req->memory != V4L2_MEMORY_OVERLAY) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300451 dprintk(1, "reqbufs: memory type invalid\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300452 return -EINVAL;
453 }
454
Hans Verkuil97397682010-09-20 17:24:30 -0300455 videobuf_queue_lock(q);
Brandon Philips19bc5132007-11-13 20:05:38 -0300456 if (req->type != q->type) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300457 dprintk(1, "reqbufs: queue type invalid\n");
Brandon Philips19bc5132007-11-13 20:05:38 -0300458 retval = -EINVAL;
459 goto done;
460 }
461
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300462 if (q->streaming) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300463 dprintk(1, "reqbufs: streaming already exists\n");
Brandon Philips00f98d02007-09-27 20:55:28 -0300464 retval = -EBUSY;
465 goto done;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300466 }
467 if (!list_empty(&q->stream)) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300468 dprintk(1, "reqbufs: stream running\n");
Brandon Philips00f98d02007-09-27 20:55:28 -0300469 retval = -EBUSY;
470 goto done;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300471 }
472
Hans de Goedeb7900ee2014-08-31 07:19:21 -0300473 if (req->count == 0) {
474 dprintk(1, "reqbufs: count invalid (%d)\n", req->count);
475 retval = __videobuf_free(q);
476 goto done;
477 }
478
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300479 count = req->count;
480 if (count > VIDEO_MAX_FRAME)
481 count = VIDEO_MAX_FRAME;
482 size = 0;
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300483 q->ops->buf_setup(q, &count, &size);
Márton Németh296372e2009-11-22 18:03:05 -0300484 dprintk(1, "reqbufs: bufs=%d, size=0x%x [%u pages total]\n",
485 count, size,
Pawel Osciak7a022642010-03-17 04:01:04 -0300486 (unsigned int)((count * PAGE_ALIGN(size)) >> PAGE_SHIFT));
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300487
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300488 retval = __videobuf_mmap_setup(q, count, size, req->memory);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300489 if (retval < 0) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300490 dprintk(1, "reqbufs: mmap setup returned %d\n", retval);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300491 goto done;
492 }
493
Brandon Philips49ee7182007-10-05 16:26:27 -0300494 req->count = retval;
Németh Márton925d74a2009-04-29 15:57:24 -0300495 retval = 0;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300496
497 done:
Hans Verkuil97397682010-09-20 17:24:30 -0300498 videobuf_queue_unlock(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300499 return retval;
500}
Pawel Osciak7a022642010-03-17 04:01:04 -0300501EXPORT_SYMBOL_GPL(videobuf_reqbufs);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300502
503int videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b)
504{
Brandon Philips19bc5132007-11-13 20:05:38 -0300505 int ret = -EINVAL;
506
Hans Verkuil97397682010-09-20 17:24:30 -0300507 videobuf_queue_lock(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300508 if (unlikely(b->type != q->type)) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300509 dprintk(1, "querybuf: Wrong type.\n");
Brandon Philips19bc5132007-11-13 20:05:38 -0300510 goto done;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300511 }
Roel Kluin223ffe52009-05-02 16:38:47 -0300512 if (unlikely(b->index >= VIDEO_MAX_FRAME)) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300513 dprintk(1, "querybuf: index out of range.\n");
Brandon Philips19bc5132007-11-13 20:05:38 -0300514 goto done;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300515 }
516 if (unlikely(NULL == q->bufs[b->index])) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300517 dprintk(1, "querybuf: buffer is null.\n");
Brandon Philips19bc5132007-11-13 20:05:38 -0300518 goto done;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300519 }
Brandon Philips19bc5132007-11-13 20:05:38 -0300520
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300521 videobuf_status(q, b, q->bufs[b->index], q->type);
Brandon Philips19bc5132007-11-13 20:05:38 -0300522
523 ret = 0;
524done:
Hans Verkuil97397682010-09-20 17:24:30 -0300525 videobuf_queue_unlock(q);
Brandon Philips19bc5132007-11-13 20:05:38 -0300526 return ret;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300527}
Pawel Osciak7a022642010-03-17 04:01:04 -0300528EXPORT_SYMBOL_GPL(videobuf_querybuf);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300529
Pawel Osciak7a022642010-03-17 04:01:04 -0300530int videobuf_qbuf(struct videobuf_queue *q, struct v4l2_buffer *b)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300531{
532 struct videobuf_buffer *buf;
533 enum v4l2_field field;
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300534 unsigned long flags = 0;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300535 int retval;
536
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300537 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300538
Maxim Levitsky99001322007-09-27 20:34:09 -0300539 if (b->memory == V4L2_MEMORY_MMAP)
540 down_read(&current->mm->mmap_sem);
541
Hans Verkuil97397682010-09-20 17:24:30 -0300542 videobuf_queue_lock(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300543 retval = -EBUSY;
544 if (q->reading) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300545 dprintk(1, "qbuf: Reading running...\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300546 goto done;
547 }
548 retval = -EINVAL;
549 if (b->type != q->type) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300550 dprintk(1, "qbuf: Wrong type.\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300551 goto done;
552 }
Roel Kluin223ffe52009-05-02 16:38:47 -0300553 if (b->index >= VIDEO_MAX_FRAME) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300554 dprintk(1, "qbuf: index out of range.\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300555 goto done;
556 }
557 buf = q->bufs[b->index];
558 if (NULL == buf) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300559 dprintk(1, "qbuf: buffer is null.\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300560 goto done;
561 }
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300562 MAGIC_CHECK(buf->magic, MAGIC_BUFFER);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300563 if (buf->memory != b->memory) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300564 dprintk(1, "qbuf: memory type is wrong.\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300565 goto done;
566 }
Brandon Philips0fc06862007-11-06 20:02:36 -0300567 if (buf->state != VIDEOBUF_NEEDS_INIT && buf->state != VIDEOBUF_IDLE) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300568 dprintk(1, "qbuf: buffer is already queued or active.\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300569 goto done;
570 }
571
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300572 switch (b->memory) {
573 case V4L2_MEMORY_MMAP:
574 if (0 == buf->baddr) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300575 dprintk(1, "qbuf: mmap requested "
576 "but buffer addr is zero!\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300577 goto done;
578 }
Pawel Osciak96f2ec62010-04-21 06:44:27 -0300579 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT
580 || q->type == V4L2_BUF_TYPE_VBI_OUTPUT
Antti Palosaari9effc722015-10-10 13:51:00 -0300581 || q->type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT
582 || q->type == V4L2_BUF_TYPE_SDR_OUTPUT) {
Pawel Osciak96f2ec62010-04-21 06:44:27 -0300583 buf->size = b->bytesused;
584 buf->field = b->field;
585 buf->ts = b->timestamp;
586 }
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300587 break;
588 case V4L2_MEMORY_USERPTR:
589 if (b->length < buf->bsize) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300590 dprintk(1, "qbuf: buffer length is not enough\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300591 goto done;
592 }
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300593 if (VIDEOBUF_NEEDS_INIT != buf->state &&
594 buf->baddr != b->m.userptr)
595 q->ops->buf_release(q, buf);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300596 buf->baddr = b->m.userptr;
597 break;
598 case V4L2_MEMORY_OVERLAY:
599 buf->boff = b->m.offset;
600 break;
601 default:
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300602 dprintk(1, "qbuf: wrong memory type\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300603 goto done;
604 }
605
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300606 dprintk(1, "qbuf: requesting next field\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300607 field = videobuf_next_field(q);
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300608 retval = q->ops->buf_prepare(q, buf, field);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300609 if (0 != retval) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300610 dprintk(1, "qbuf: buffer_prepare returned %d\n", retval);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300611 goto done;
612 }
613
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300614 list_add_tail(&buf->stream, &q->stream);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300615 if (q->streaming) {
Brandon Philips0cf4dae2008-03-28 10:18:33 -0700616 spin_lock_irqsave(q->irqlock, flags);
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300617 q->ops->buf_queue(q, buf);
Brandon Philips0cf4dae2008-03-28 10:18:33 -0700618 spin_unlock_irqrestore(q->irqlock, flags);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300619 }
Hans Verkuil771075b2010-03-13 11:47:06 -0300620 dprintk(1, "qbuf: succeeded\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300621 retval = 0;
Brandon Philips137d1cb2008-04-02 18:10:59 -0300622 wake_up_interruptible_sync(&q->wait);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300623
Pawel Osciak7a022642010-03-17 04:01:04 -0300624done:
Hans Verkuil97397682010-09-20 17:24:30 -0300625 videobuf_queue_unlock(q);
Maxim Levitsky99001322007-09-27 20:34:09 -0300626
627 if (b->memory == V4L2_MEMORY_MMAP)
628 up_read(&current->mm->mmap_sem);
629
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300630 return retval;
631}
Pawel Osciak7a022642010-03-17 04:01:04 -0300632EXPORT_SYMBOL_GPL(videobuf_qbuf);
Brandon Philips137d1cb2008-04-02 18:10:59 -0300633
634/* Locking: Caller holds q->vb_lock */
635static int stream_next_buffer_check_queue(struct videobuf_queue *q, int noblock)
636{
637 int retval;
638
639checks:
640 if (!q->streaming) {
641 dprintk(1, "next_buffer: Not streaming\n");
642 retval = -EINVAL;
643 goto done;
644 }
645
646 if (list_empty(&q->stream)) {
647 if (noblock) {
648 retval = -EAGAIN;
649 dprintk(2, "next_buffer: no buffers to dequeue\n");
650 goto done;
651 } else {
652 dprintk(2, "next_buffer: waiting on buffer\n");
653
654 /* Drop lock to avoid deadlock with qbuf */
Hans Verkuil97397682010-09-20 17:24:30 -0300655 videobuf_queue_unlock(q);
Brandon Philips137d1cb2008-04-02 18:10:59 -0300656
657 /* Checking list_empty and streaming is safe without
658 * locks because we goto checks to validate while
659 * holding locks before proceeding */
660 retval = wait_event_interruptible(q->wait,
661 !list_empty(&q->stream) || !q->streaming);
Hans Verkuil97397682010-09-20 17:24:30 -0300662 videobuf_queue_lock(q);
Brandon Philips137d1cb2008-04-02 18:10:59 -0300663
664 if (retval)
665 goto done;
666
667 goto checks;
668 }
669 }
670
671 retval = 0;
672
673done:
674 return retval;
675}
676
Brandon Philips137d1cb2008-04-02 18:10:59 -0300677/* Locking: Caller holds q->vb_lock */
678static int stream_next_buffer(struct videobuf_queue *q,
679 struct videobuf_buffer **vb, int nonblocking)
680{
681 int retval;
682 struct videobuf_buffer *buf = NULL;
683
684 retval = stream_next_buffer_check_queue(q, nonblocking);
685 if (retval)
686 goto done;
687
688 buf = list_entry(q->stream.next, struct videobuf_buffer, stream);
Hans Verkuil0e0809a2010-09-26 09:01:26 -0300689 retval = videobuf_waiton(q, buf, nonblocking, 1);
Brandon Philips137d1cb2008-04-02 18:10:59 -0300690 if (retval < 0)
691 goto done;
692
693 *vb = buf;
694done:
695 return retval;
696}
697
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300698int videobuf_dqbuf(struct videobuf_queue *q,
Pawel Osciak7a022642010-03-17 04:01:04 -0300699 struct v4l2_buffer *b, int nonblocking)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300700{
Brandon Philips137d1cb2008-04-02 18:10:59 -0300701 struct videobuf_buffer *buf = NULL;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300702 int retval;
703
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300704 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300705
Hans Verkuilb2dfd1a2010-04-28 04:05:22 -0300706 memset(b, 0, sizeof(*b));
Hans Verkuil97397682010-09-20 17:24:30 -0300707 videobuf_queue_lock(q);
Brandon Philips137d1cb2008-04-02 18:10:59 -0300708
709 retval = stream_next_buffer(q, &buf, nonblocking);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300710 if (retval < 0) {
Brandon Philips137d1cb2008-04-02 18:10:59 -0300711 dprintk(1, "dqbuf: next_buffer error: %i\n", retval);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300712 goto done;
713 }
Brandon Philips137d1cb2008-04-02 18:10:59 -0300714
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300715 switch (buf->state) {
Brandon Philips0fc06862007-11-06 20:02:36 -0300716 case VIDEOBUF_ERROR:
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300717 dprintk(1, "dqbuf: state is error\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300718 break;
Brandon Philips0fc06862007-11-06 20:02:36 -0300719 case VIDEOBUF_DONE:
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300720 dprintk(1, "dqbuf: state is done\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300721 break;
722 default:
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300723 dprintk(1, "dqbuf: state invalid\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300724 retval = -EINVAL;
725 goto done;
726 }
Hans Verkuilb2dfd1a2010-04-28 04:05:22 -0300727 CALL(q, sync, q, buf);
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300728 videobuf_status(q, b, buf, q->type);
Hans Verkuilb2dfd1a2010-04-28 04:05:22 -0300729 list_del(&buf->stream);
730 buf->state = VIDEOBUF_IDLE;
731 b->flags &= ~V4L2_BUF_FLAG_DONE;
Pawel Osciak7a022642010-03-17 04:01:04 -0300732done:
Hans Verkuil97397682010-09-20 17:24:30 -0300733 videobuf_queue_unlock(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300734 return retval;
735}
Pawel Osciak7a022642010-03-17 04:01:04 -0300736EXPORT_SYMBOL_GPL(videobuf_dqbuf);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300737
738int videobuf_streamon(struct videobuf_queue *q)
739{
740 struct videobuf_buffer *buf;
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300741 unsigned long flags = 0;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300742 int retval;
743
Hans Verkuil97397682010-09-20 17:24:30 -0300744 videobuf_queue_lock(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300745 retval = -EBUSY;
746 if (q->reading)
747 goto done;
748 retval = 0;
749 if (q->streaming)
750 goto done;
751 q->streaming = 1;
Brandon Philips0cf4dae2008-03-28 10:18:33 -0700752 spin_lock_irqsave(q->irqlock, flags);
Trent Piephoa991f442007-10-10 05:37:43 -0300753 list_for_each_entry(buf, &q->stream, stream)
Brandon Philips0fc06862007-11-06 20:02:36 -0300754 if (buf->state == VIDEOBUF_PREPARED)
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300755 q->ops->buf_queue(q, buf);
Brandon Philips0cf4dae2008-03-28 10:18:33 -0700756 spin_unlock_irqrestore(q->irqlock, flags);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300757
Brandon Philips137d1cb2008-04-02 18:10:59 -0300758 wake_up_interruptible_sync(&q->wait);
Pawel Osciak7a022642010-03-17 04:01:04 -0300759done:
Hans Verkuil97397682010-09-20 17:24:30 -0300760 videobuf_queue_unlock(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300761 return retval;
762}
Pawel Osciak7a022642010-03-17 04:01:04 -0300763EXPORT_SYMBOL_GPL(videobuf_streamon);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300764
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300765/* Locking: Caller holds q->vb_lock */
Brandon Philips19bc5132007-11-13 20:05:38 -0300766static int __videobuf_streamoff(struct videobuf_queue *q)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300767{
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300768 if (!q->streaming)
Brandon Philips19bc5132007-11-13 20:05:38 -0300769 return -EINVAL;
770
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300771 videobuf_queue_cancel(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300772
Brandon Philips19bc5132007-11-13 20:05:38 -0300773 return 0;
774}
775
776int videobuf_streamoff(struct videobuf_queue *q)
777{
778 int retval;
779
Hans Verkuil97397682010-09-20 17:24:30 -0300780 videobuf_queue_lock(q);
Brandon Philips19bc5132007-11-13 20:05:38 -0300781 retval = __videobuf_streamoff(q);
Hans Verkuil97397682010-09-20 17:24:30 -0300782 videobuf_queue_unlock(q);
Brandon Philips19bc5132007-11-13 20:05:38 -0300783
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300784 return retval;
785}
Pawel Osciak7a022642010-03-17 04:01:04 -0300786EXPORT_SYMBOL_GPL(videobuf_streamoff);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300787
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300788/* Locking: Caller holds q->vb_lock */
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300789static ssize_t videobuf_read_zerocopy(struct videobuf_queue *q,
790 char __user *data,
791 size_t count, loff_t *ppos)
792{
793 enum v4l2_field field;
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300794 unsigned long flags = 0;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300795 int retval;
796
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300797 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300798
799 /* setup stuff */
Pawel Osciak33c38282010-05-11 10:36:28 -0300800 q->read_buf = videobuf_alloc_vb(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300801 if (NULL == q->read_buf)
802 return -ENOMEM;
803
804 q->read_buf->memory = V4L2_MEMORY_USERPTR;
805 q->read_buf->baddr = (unsigned long)data;
806 q->read_buf->bsize = count;
807
808 field = videobuf_next_field(q);
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300809 retval = q->ops->buf_prepare(q, q->read_buf, field);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300810 if (0 != retval)
811 goto done;
812
813 /* start capture & wait */
Brandon Philips0cf4dae2008-03-28 10:18:33 -0700814 spin_lock_irqsave(q->irqlock, flags);
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300815 q->ops->buf_queue(q, q->read_buf);
Brandon Philips0cf4dae2008-03-28 10:18:33 -0700816 spin_unlock_irqrestore(q->irqlock, flags);
Hans Verkuil0e0809a2010-09-26 09:01:26 -0300817 retval = videobuf_waiton(q, q->read_buf, 0, 0);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300818 if (0 == retval) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300819 CALL(q, sync, q, q->read_buf);
Brandon Philips0fc06862007-11-06 20:02:36 -0300820 if (VIDEOBUF_ERROR == q->read_buf->state)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300821 retval = -EIO;
822 else
823 retval = q->read_buf->size;
824 }
825
Pawel Osciak7a022642010-03-17 04:01:04 -0300826done:
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300827 /* cleanup */
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300828 q->ops->buf_release(q, q->read_buf);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300829 kfree(q->read_buf);
830 q->read_buf = NULL;
831 return retval;
832}
833
Hans Verkuil37111032010-03-28 09:22:53 -0300834static int __videobuf_copy_to_user(struct videobuf_queue *q,
835 struct videobuf_buffer *buf,
836 char __user *data, size_t count,
837 int nonblocking)
838{
Hans Verkuila8d54e42014-11-05 04:51:26 -0300839 void *vaddr = CALLPTR(q, vaddr, buf);
Hans Verkuil37111032010-03-28 09:22:53 -0300840
841 /* copy to userspace */
842 if (count > buf->size - q->read_off)
843 count = buf->size - q->read_off;
844
845 if (copy_to_user(data, vaddr + q->read_off, count))
846 return -EFAULT;
847
848 return count;
849}
850
851static int __videobuf_copy_stream(struct videobuf_queue *q,
852 struct videobuf_buffer *buf,
853 char __user *data, size_t count, size_t pos,
854 int vbihack, int nonblocking)
855{
Hans Verkuila8d54e42014-11-05 04:51:26 -0300856 unsigned int *fc = CALLPTR(q, vaddr, buf);
Hans Verkuil37111032010-03-28 09:22:53 -0300857
858 if (vbihack) {
859 /* dirty, undocumented hack -- pass the frame counter
860 * within the last four bytes of each vbi data block.
861 * We need that one to maintain backward compatibility
862 * to all vbi decoding software out there ... */
863 fc += (buf->size >> 2) - 1;
864 *fc = buf->field_count >> 1;
865 dprintk(1, "vbihack: %d\n", *fc);
866 }
867
868 /* copy stuff using the common method */
869 count = __videobuf_copy_to_user(q, buf, data, count, nonblocking);
870
871 if ((count == -EFAULT) && (pos == 0))
872 return -EFAULT;
873
874 return count;
875}
876
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300877ssize_t videobuf_read_one(struct videobuf_queue *q,
878 char __user *data, size_t count, loff_t *ppos,
879 int nonblocking)
880{
881 enum v4l2_field field;
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300882 unsigned long flags = 0;
Guennadi Liakhovetski7daa4a82008-04-22 14:46:03 -0300883 unsigned size = 0, nbufs = 1;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300884 int retval;
885
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300886 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300887
Hans Verkuil97397682010-09-20 17:24:30 -0300888 videobuf_queue_lock(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300889
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300890 q->ops->buf_setup(q, &nbufs, &size);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300891
892 if (NULL == q->read_buf &&
893 count >= size &&
894 !nonblocking) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300895 retval = videobuf_read_zerocopy(q, data, count, ppos);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300896 if (retval >= 0 || retval == -EIO)
897 /* ok, all done */
898 goto done;
899 /* fallback to kernel bounce buffer on failures */
900 }
901
902 if (NULL == q->read_buf) {
903 /* need to capture a new frame */
904 retval = -ENOMEM;
Pawel Osciak33c38282010-05-11 10:36:28 -0300905 q->read_buf = videobuf_alloc_vb(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300906
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300907 dprintk(1, "video alloc=0x%p\n", q->read_buf);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300908 if (NULL == q->read_buf)
909 goto done;
910 q->read_buf->memory = V4L2_MEMORY_USERPTR;
911 q->read_buf->bsize = count; /* preferred size */
912 field = videobuf_next_field(q);
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300913 retval = q->ops->buf_prepare(q, q->read_buf, field);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300914
915 if (0 != retval) {
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300916 kfree(q->read_buf);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300917 q->read_buf = NULL;
918 goto done;
919 }
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300920
Brandon Philips0cf4dae2008-03-28 10:18:33 -0700921 spin_lock_irqsave(q->irqlock, flags);
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300922 q->ops->buf_queue(q, q->read_buf);
Brandon Philips0cf4dae2008-03-28 10:18:33 -0700923 spin_unlock_irqrestore(q->irqlock, flags);
924
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300925 q->read_off = 0;
926 }
927
928 /* wait until capture is done */
Hans Verkuil0e0809a2010-09-26 09:01:26 -0300929 retval = videobuf_waiton(q, q->read_buf, nonblocking, 1);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300930 if (0 != retval)
931 goto done;
932
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300933 CALL(q, sync, q, q->read_buf);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300934
Brandon Philips0fc06862007-11-06 20:02:36 -0300935 if (VIDEOBUF_ERROR == q->read_buf->state) {
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300936 /* catch I/O errors */
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 retval = -EIO;
941 goto done;
942 }
943
944 /* Copy to userspace */
Hans Verkuil37111032010-03-28 09:22:53 -0300945 retval = __videobuf_copy_to_user(q, q->read_buf, data, count, nonblocking);
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300946 if (retval < 0)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300947 goto done;
948
949 q->read_off += retval;
950 if (q->read_off == q->read_buf->size) {
951 /* all data copied, cleanup */
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300952 q->ops->buf_release(q, q->read_buf);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300953 kfree(q->read_buf);
954 q->read_buf = NULL;
955 }
956
Pawel Osciak7a022642010-03-17 04:01:04 -0300957done:
Hans Verkuil97397682010-09-20 17:24:30 -0300958 videobuf_queue_unlock(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300959 return retval;
960}
Pawel Osciak7a022642010-03-17 04:01:04 -0300961EXPORT_SYMBOL_GPL(videobuf_read_one);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300962
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300963/* Locking: Caller holds q->vb_lock */
Mauro Carvalho Chehab225ba902007-12-12 21:46:26 -0300964static int __videobuf_read_start(struct videobuf_queue *q)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300965{
966 enum v4l2_field field;
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300967 unsigned long flags = 0;
Brandon Philips49ee7182007-10-05 16:26:27 -0300968 unsigned int count = 0, size = 0;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300969 int err, i;
970
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300971 q->ops->buf_setup(q, &count, &size);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300972 if (count < 2)
973 count = 2;
974 if (count > VIDEO_MAX_FRAME)
975 count = VIDEO_MAX_FRAME;
976 size = PAGE_ALIGN(size);
977
Brandon Philips19bc5132007-11-13 20:05:38 -0300978 err = __videobuf_mmap_setup(q, count, size, V4L2_MEMORY_USERPTR);
Brandon Philips49ee7182007-10-05 16:26:27 -0300979 if (err < 0)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300980 return err;
981
Brandon Philips49ee7182007-10-05 16:26:27 -0300982 count = err;
983
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300984 for (i = 0; i < count; i++) {
985 field = videobuf_next_field(q);
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300986 err = q->ops->buf_prepare(q, q->bufs[i], field);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300987 if (err)
988 return err;
989 list_add_tail(&q->bufs[i]->stream, &q->stream);
990 }
Brandon Philips0cf4dae2008-03-28 10:18:33 -0700991 spin_lock_irqsave(q->irqlock, flags);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300992 for (i = 0; i < count; i++)
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -0300993 q->ops->buf_queue(q, q->bufs[i]);
Brandon Philips0cf4dae2008-03-28 10:18:33 -0700994 spin_unlock_irqrestore(q->irqlock, flags);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300995 q->reading = 1;
996 return 0;
997}
998
Brandon Philips19bc5132007-11-13 20:05:38 -0300999static void __videobuf_read_stop(struct videobuf_queue *q)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001000{
1001 int i;
1002
1003 videobuf_queue_cancel(q);
Pawel Osciaka438d6d2010-05-11 10:36:29 -03001004 __videobuf_free(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001005 INIT_LIST_HEAD(&q->stream);
1006 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1007 if (NULL == q->bufs[i])
1008 continue;
1009 kfree(q->bufs[i]);
1010 q->bufs[i] = NULL;
1011 }
1012 q->read_buf = NULL;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001013}
1014
Mauro Carvalho Chehab19fb1452007-11-15 23:09:30 -03001015int videobuf_read_start(struct videobuf_queue *q)
1016{
1017 int rc;
1018
Hans Verkuil97397682010-09-20 17:24:30 -03001019 videobuf_queue_lock(q);
Mauro Carvalho Chehab19fb1452007-11-15 23:09:30 -03001020 rc = __videobuf_read_start(q);
Hans Verkuil97397682010-09-20 17:24:30 -03001021 videobuf_queue_unlock(q);
Mauro Carvalho Chehab19fb1452007-11-15 23:09:30 -03001022
1023 return rc;
1024}
Pawel Osciak7a022642010-03-17 04:01:04 -03001025EXPORT_SYMBOL_GPL(videobuf_read_start);
Mauro Carvalho Chehab19fb1452007-11-15 23:09:30 -03001026
Brandon Philips19bc5132007-11-13 20:05:38 -03001027void videobuf_read_stop(struct videobuf_queue *q)
1028{
Hans Verkuil97397682010-09-20 17:24:30 -03001029 videobuf_queue_lock(q);
Brandon Philips19bc5132007-11-13 20:05:38 -03001030 __videobuf_read_stop(q);
Hans Verkuil97397682010-09-20 17:24:30 -03001031 videobuf_queue_unlock(q);
Brandon Philips19bc5132007-11-13 20:05:38 -03001032}
Pawel Osciak7a022642010-03-17 04:01:04 -03001033EXPORT_SYMBOL_GPL(videobuf_read_stop);
Brandon Philips19bc5132007-11-13 20:05:38 -03001034
1035void videobuf_stop(struct videobuf_queue *q)
1036{
Hans Verkuil97397682010-09-20 17:24:30 -03001037 videobuf_queue_lock(q);
Brandon Philips19bc5132007-11-13 20:05:38 -03001038
1039 if (q->streaming)
1040 __videobuf_streamoff(q);
1041
1042 if (q->reading)
1043 __videobuf_read_stop(q);
1044
Hans Verkuil97397682010-09-20 17:24:30 -03001045 videobuf_queue_unlock(q);
Brandon Philips19bc5132007-11-13 20:05:38 -03001046}
Pawel Osciak7a022642010-03-17 04:01:04 -03001047EXPORT_SYMBOL_GPL(videobuf_stop);
Brandon Philips19bc5132007-11-13 20:05:38 -03001048
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001049ssize_t videobuf_read_stream(struct videobuf_queue *q,
1050 char __user *data, size_t count, loff_t *ppos,
1051 int vbihack, int nonblocking)
1052{
1053 int rc, retval;
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -03001054 unsigned long flags = 0;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001055
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -03001056 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001057
Harvey Harrison7e28adb2008-04-08 23:20:00 -03001058 dprintk(2, "%s\n", __func__);
Hans Verkuil97397682010-09-20 17:24:30 -03001059 videobuf_queue_lock(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001060 retval = -EBUSY;
1061 if (q->streaming)
1062 goto done;
1063 if (!q->reading) {
Adrian Bunk3f843072007-12-12 16:44:54 -03001064 retval = __videobuf_read_start(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001065 if (retval < 0)
1066 goto done;
1067 }
1068
1069 retval = 0;
1070 while (count > 0) {
1071 /* get / wait for data */
1072 if (NULL == q->read_buf) {
1073 q->read_buf = list_entry(q->stream.next,
1074 struct videobuf_buffer,
1075 stream);
1076 list_del(&q->read_buf->stream);
1077 q->read_off = 0;
1078 }
Hans Verkuil0e0809a2010-09-26 09:01:26 -03001079 rc = videobuf_waiton(q, q->read_buf, nonblocking, 1);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001080 if (rc < 0) {
1081 if (0 == retval)
1082 retval = rc;
1083 break;
1084 }
1085
Brandon Philips0fc06862007-11-06 20:02:36 -03001086 if (q->read_buf->state == VIDEOBUF_DONE) {
Hans Verkuil37111032010-03-28 09:22:53 -03001087 rc = __videobuf_copy_stream(q, q->read_buf, data + retval, count,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001088 retval, vbihack, nonblocking);
1089 if (rc < 0) {
1090 retval = rc;
1091 break;
1092 }
1093 retval += rc;
1094 count -= rc;
1095 q->read_off += rc;
1096 } else {
1097 /* some error */
1098 q->read_off = q->read_buf->size;
1099 if (0 == retval)
1100 retval = -EIO;
1101 }
1102
1103 /* requeue buffer when done with copying */
1104 if (q->read_off == q->read_buf->size) {
1105 list_add_tail(&q->read_buf->stream,
1106 &q->stream);
Brandon Philips0cf4dae2008-03-28 10:18:33 -07001107 spin_lock_irqsave(q->irqlock, flags);
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -03001108 q->ops->buf_queue(q, q->read_buf);
Brandon Philips0cf4dae2008-03-28 10:18:33 -07001109 spin_unlock_irqrestore(q->irqlock, flags);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001110 q->read_buf = NULL;
1111 }
1112 if (retval < 0)
1113 break;
1114 }
1115
Pawel Osciak7a022642010-03-17 04:01:04 -03001116done:
Hans Verkuil97397682010-09-20 17:24:30 -03001117 videobuf_queue_unlock(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001118 return retval;
1119}
Pawel Osciak7a022642010-03-17 04:01:04 -03001120EXPORT_SYMBOL_GPL(videobuf_read_stream);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001121
1122unsigned int videobuf_poll_stream(struct file *file,
1123 struct videobuf_queue *q,
1124 poll_table *wait)
1125{
Hans Verkuil0e17e9a2011-07-13 04:03:52 -03001126 unsigned long req_events = poll_requested_events(wait);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001127 struct videobuf_buffer *buf = NULL;
1128 unsigned int rc = 0;
1129
Hans Verkuil97397682010-09-20 17:24:30 -03001130 videobuf_queue_lock(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001131 if (q->streaming) {
1132 if (!list_empty(&q->stream))
1133 buf = list_entry(q->stream.next,
1134 struct videobuf_buffer, stream);
Hans Verkuil0e17e9a2011-07-13 04:03:52 -03001135 } else if (req_events & (POLLIN | POLLRDNORM)) {
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001136 if (!q->reading)
Adrian Bunk3f843072007-12-12 16:44:54 -03001137 __videobuf_read_start(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001138 if (!q->reading) {
1139 rc = POLLERR;
1140 } else if (NULL == q->read_buf) {
1141 q->read_buf = list_entry(q->stream.next,
1142 struct videobuf_buffer,
1143 stream);
1144 list_del(&q->read_buf->stream);
1145 q->read_off = 0;
1146 }
1147 buf = q->read_buf;
1148 }
1149 if (!buf)
1150 rc = POLLERR;
1151
1152 if (0 == rc) {
1153 poll_wait(file, &buf->done, wait);
Brandon Philips0fc06862007-11-06 20:02:36 -03001154 if (buf->state == VIDEOBUF_DONE ||
Pawel Osciak9b558432010-03-29 05:16:31 -03001155 buf->state == VIDEOBUF_ERROR) {
1156 switch (q->type) {
1157 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1158 case V4L2_BUF_TYPE_VBI_OUTPUT:
1159 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
Antti Palosaari9effc722015-10-10 13:51:00 -03001160 case V4L2_BUF_TYPE_SDR_OUTPUT:
Pawel Osciak9b558432010-03-29 05:16:31 -03001161 rc = POLLOUT | POLLWRNORM;
1162 break;
1163 default:
1164 rc = POLLIN | POLLRDNORM;
1165 break;
1166 }
1167 }
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001168 }
Hans Verkuil97397682010-09-20 17:24:30 -03001169 videobuf_queue_unlock(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001170 return rc;
1171}
Pawel Osciak7a022642010-03-17 04:01:04 -03001172EXPORT_SYMBOL_GPL(videobuf_poll_stream);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001173
Pawel Osciak7a022642010-03-17 04:01:04 -03001174int videobuf_mmap_mapper(struct videobuf_queue *q, struct vm_area_struct *vma)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001175{
Hans Verkuil0b62b732010-03-28 09:09:05 -03001176 int rc = -EINVAL;
1177 int i;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001178
Mauro Carvalho Chehabe2c77312007-12-10 10:53:20 -03001179 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001180
Hans Verkuil0b62b732010-03-28 09:09:05 -03001181 if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED)) {
1182 dprintk(1, "mmap appl bug: PROT_WRITE and MAP_SHARED are required\n");
1183 return -EINVAL;
1184 }
1185
Hans Verkuil97397682010-09-20 17:24:30 -03001186 videobuf_queue_lock(q);
Hans Verkuil0b62b732010-03-28 09:09:05 -03001187 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1188 struct videobuf_buffer *buf = q->bufs[i];
1189
1190 if (buf && buf->memory == V4L2_MEMORY_MMAP &&
1191 buf->boff == (vma->vm_pgoff << PAGE_SHIFT)) {
1192 rc = CALL(q, mmap_mapper, q, buf, vma);
1193 break;
1194 }
1195 }
Hans Verkuil97397682010-09-20 17:24:30 -03001196 videobuf_queue_unlock(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001197
Hans Verkuil0b62b732010-03-28 09:09:05 -03001198 return rc;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001199}
Pawel Osciak7a022642010-03-17 04:01:04 -03001200EXPORT_SYMBOL_GPL(videobuf_mmap_mapper);