Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 1 | /* |
| 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 Righi | 27ac792 | 2008-07-23 21:28:13 -0700 | [diff] [blame] | 19 | #include <linux/mm.h> |
Alexey Dobriyan | d43c36d | 2009-10-07 17:09:06 +0400 | [diff] [blame] | 20 | #include <linux/sched.h> |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 21 | #include <linux/slab.h> |
| 22 | #include <linux/interrupt.h> |
| 23 | |
| 24 | #include <media/videobuf-core.h> |
| 25 | |
| 26 | #define MAGIC_BUFFER 0x20070728 |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 27 | #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 Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 36 | |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 37 | static int debug; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 38 | module_param(debug, int, 0644); |
| 39 | |
| 40 | MODULE_DESCRIPTION("helper module to manage video4linux buffers"); |
| 41 | MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>"); |
| 42 | MODULE_LICENSE("GPL"); |
| 43 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 44 | #define dprintk(level, fmt, arg...) \ |
| 45 | do { \ |
| 46 | if (debug >= level) \ |
| 47 | printk(KERN_DEBUG "vbuf: " fmt, ## arg); \ |
| 48 | } while (0) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 49 | |
| 50 | /* --------------------------------------------------------------------- */ |
| 51 | |
| 52 | #define CALL(q, f, arg...) \ |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 53 | ((q->int_ops->f) ? q->int_ops->f(arg) : 0) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 54 | |
Pawel Osciak | 33c3828 | 2010-05-11 10:36:28 -0300 | [diff] [blame] | 55 | struct videobuf_buffer *videobuf_alloc_vb(struct videobuf_queue *q) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 56 | { |
| 57 | struct videobuf_buffer *vb; |
| 58 | |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 59 | BUG_ON(q->msize < sizeof(*vb)); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 60 | |
Pawel Osciak | 33c3828 | 2010-05-11 10:36:28 -0300 | [diff] [blame] | 61 | if (!q->int_ops || !q->int_ops->alloc_vb) { |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 62 | printk(KERN_ERR "No specific ops defined!\n"); |
| 63 | BUG(); |
| 64 | } |
| 65 | |
Pawel Osciak | 33c3828 | 2010-05-11 10:36:28 -0300 | [diff] [blame] | 66 | vb = q->int_ops->alloc_vb(q->msize); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 67 | if (NULL != vb) { |
| 68 | init_waitqueue_head(&vb->done); |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 69 | vb->magic = MAGIC_BUFFER; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | return vb; |
| 73 | } |
Pawel Osciak | 33c3828 | 2010-05-11 10:36:28 -0300 | [diff] [blame] | 74 | EXPORT_SYMBOL_GPL(videobuf_alloc_vb); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 75 | |
Hans Verkuil | 0e0809a | 2010-09-26 09:01:26 -0300 | [diff] [blame] | 76 | static int is_state_active_or_queued(struct videobuf_queue *q, struct videobuf_buffer *vb) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 77 | { |
Hans Verkuil | 0e0809a | 2010-09-26 09:01:26 -0300 | [diff] [blame] | 78 | unsigned long flags; |
| 79 | bool rc; |
| 80 | |
| 81 | spin_lock_irqsave(q->irqlock, flags); |
| 82 | rc = vb->state != VIDEOBUF_ACTIVE && vb->state != VIDEOBUF_QUEUED; |
| 83 | spin_unlock_irqrestore(q->irqlock, flags); |
| 84 | return rc; |
| 85 | }; |
| 86 | |
| 87 | int videobuf_waiton(struct videobuf_queue *q, struct videobuf_buffer *vb, |
| 88 | int non_blocking, int intr) |
| 89 | { |
| 90 | bool is_ext_locked; |
| 91 | int ret = 0; |
| 92 | |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 93 | MAGIC_CHECK(vb->magic, MAGIC_BUFFER); |
Brandon Philips | 009a905 | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 94 | |
| 95 | if (non_blocking) { |
Hans Verkuil | 0e0809a | 2010-09-26 09:01:26 -0300 | [diff] [blame] | 96 | if (is_state_active_or_queued(q, vb)) |
Brandon Philips | 009a905 | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 97 | return 0; |
Hans Verkuil | 0e0809a | 2010-09-26 09:01:26 -0300 | [diff] [blame] | 98 | return -EAGAIN; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 99 | } |
Brandon Philips | 009a905 | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 100 | |
Hans Verkuil | 0e0809a | 2010-09-26 09:01:26 -0300 | [diff] [blame] | 101 | is_ext_locked = q->ext_lock && mutex_is_locked(q->ext_lock); |
Brandon Philips | 009a905 | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 102 | |
Hans Verkuil | 0e0809a | 2010-09-26 09:01:26 -0300 | [diff] [blame] | 103 | /* Release vdev lock to prevent this wait from blocking outside access to |
| 104 | the device. */ |
| 105 | if (is_ext_locked) |
| 106 | mutex_unlock(q->ext_lock); |
| 107 | if (intr) |
| 108 | ret = wait_event_interruptible(vb->done, is_state_active_or_queued(q, vb)); |
| 109 | else |
| 110 | wait_event(vb->done, is_state_active_or_queued(q, vb)); |
| 111 | /* Relock */ |
| 112 | if (is_ext_locked) |
| 113 | mutex_lock(q->ext_lock); |
| 114 | |
| 115 | return ret; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 116 | } |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 117 | EXPORT_SYMBOL_GPL(videobuf_waiton); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 118 | |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 119 | int videobuf_iolock(struct videobuf_queue *q, struct videobuf_buffer *vb, |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 120 | struct v4l2_framebuffer *fbuf) |
| 121 | { |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 122 | MAGIC_CHECK(vb->magic, MAGIC_BUFFER); |
| 123 | MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 124 | |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 125 | return CALL(q, iolock, q, vb, fbuf); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 126 | } |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 127 | EXPORT_SYMBOL_GPL(videobuf_iolock); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 128 | |
Hans Verkuil | f4fce60 | 2010-03-28 08:33:23 -0300 | [diff] [blame] | 129 | void *videobuf_queue_to_vaddr(struct videobuf_queue *q, |
| 130 | struct videobuf_buffer *buf) |
Mauro Carvalho Chehab | 59d3448 | 2008-04-13 15:10:00 -0300 | [diff] [blame] | 131 | { |
Hans Verkuil | 037c75e | 2010-03-28 08:18:37 -0300 | [diff] [blame] | 132 | if (q->int_ops->vaddr) |
| 133 | return q->int_ops->vaddr(buf); |
Hans Verkuil | f4fce60 | 2010-03-28 08:33:23 -0300 | [diff] [blame] | 134 | return NULL; |
Mauro Carvalho Chehab | 59d3448 | 2008-04-13 15:10:00 -0300 | [diff] [blame] | 135 | } |
Hans Verkuil | f4fce60 | 2010-03-28 08:33:23 -0300 | [diff] [blame] | 136 | EXPORT_SYMBOL_GPL(videobuf_queue_to_vaddr); |
Mauro Carvalho Chehab | 59d3448 | 2008-04-13 15:10:00 -0300 | [diff] [blame] | 137 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 138 | /* --------------------------------------------------------------------- */ |
| 139 | |
| 140 | |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 141 | void videobuf_queue_core_init(struct videobuf_queue *q, |
Jonathan Corbet | 38a54f3 | 2009-11-17 19:43:41 -0300 | [diff] [blame] | 142 | const struct videobuf_queue_ops *ops, |
Guennadi Liakhovetski | e9bcf66 | 2008-04-22 14:46:02 -0300 | [diff] [blame] | 143 | struct device *dev, |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 144 | spinlock_t *irqlock, |
| 145 | enum v4l2_buf_type type, |
| 146 | enum v4l2_field field, |
| 147 | unsigned int msize, |
Mauro Carvalho Chehab | d4cae5a | 2007-10-08 12:20:02 -0300 | [diff] [blame] | 148 | void *priv, |
Hans Verkuil | 08bff03 | 2010-09-20 17:39:46 -0300 | [diff] [blame] | 149 | struct videobuf_qtype_ops *int_ops, |
| 150 | struct mutex *ext_lock) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 151 | { |
Figo.zhang | 96ceea2 | 2009-06-02 23:01:04 -0300 | [diff] [blame] | 152 | BUG_ON(!q); |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 153 | memset(q, 0, sizeof(*q)); |
Mauro Carvalho Chehab | d4cae5a | 2007-10-08 12:20:02 -0300 | [diff] [blame] | 154 | q->irqlock = irqlock; |
Hans Verkuil | 08bff03 | 2010-09-20 17:39:46 -0300 | [diff] [blame] | 155 | q->ext_lock = ext_lock; |
Mauro Carvalho Chehab | d4cae5a | 2007-10-08 12:20:02 -0300 | [diff] [blame] | 156 | q->dev = dev; |
| 157 | q->type = type; |
| 158 | q->field = field; |
| 159 | q->msize = msize; |
| 160 | q->ops = ops; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 161 | q->priv_data = priv; |
Mauro Carvalho Chehab | d4cae5a | 2007-10-08 12:20:02 -0300 | [diff] [blame] | 162 | q->int_ops = int_ops; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 163 | |
| 164 | /* All buffer operations are mandatory */ |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 165 | BUG_ON(!q->ops->buf_setup); |
| 166 | BUG_ON(!q->ops->buf_prepare); |
| 167 | BUG_ON(!q->ops->buf_queue); |
| 168 | BUG_ON(!q->ops->buf_release); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 169 | |
Brandon Philips | 0cf4dae | 2008-03-28 10:18:33 -0700 | [diff] [blame] | 170 | /* Lock is mandatory for queue_cancel to work */ |
| 171 | BUG_ON(!irqlock); |
| 172 | |
Mauro Carvalho Chehab | d4cae5a | 2007-10-08 12:20:02 -0300 | [diff] [blame] | 173 | /* Having implementations for abstract methods are mandatory */ |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 174 | BUG_ON(!q->int_ops); |
Mauro Carvalho Chehab | d4cae5a | 2007-10-08 12:20:02 -0300 | [diff] [blame] | 175 | |
Mauro Carvalho Chehab | 64f9477 | 2008-01-31 13:57:53 -0300 | [diff] [blame] | 176 | mutex_init(&q->vb_lock); |
Brandon Philips | 137d1cb | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 177 | init_waitqueue_head(&q->wait); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 178 | INIT_LIST_HEAD(&q->stream); |
| 179 | } |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 180 | EXPORT_SYMBOL_GPL(videobuf_queue_core_init); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 181 | |
Brandon Philips | 19bc513 | 2007-11-13 20:05:38 -0300 | [diff] [blame] | 182 | /* Locking: Only usage in bttv unsafe find way to remove */ |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 183 | int videobuf_queue_is_busy(struct videobuf_queue *q) |
| 184 | { |
| 185 | int i; |
| 186 | |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 187 | MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 188 | |
| 189 | if (q->streaming) { |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 190 | dprintk(1, "busy: streaming active\n"); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 191 | return 1; |
| 192 | } |
| 193 | if (q->reading) { |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 194 | dprintk(1, "busy: pending read #1\n"); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 195 | return 1; |
| 196 | } |
| 197 | if (q->read_buf) { |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 198 | dprintk(1, "busy: pending read #2\n"); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 199 | return 1; |
| 200 | } |
| 201 | for (i = 0; i < VIDEO_MAX_FRAME; i++) { |
| 202 | if (NULL == q->bufs[i]) |
| 203 | continue; |
Mauro Carvalho Chehab | 851c0c9 | 2007-09-27 18:25:44 -0300 | [diff] [blame] | 204 | if (q->bufs[i]->map) { |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 205 | dprintk(1, "busy: buffer #%d mapped\n", i); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 206 | return 1; |
| 207 | } |
Brandon Philips | 0fc0686 | 2007-11-06 20:02:36 -0300 | [diff] [blame] | 208 | if (q->bufs[i]->state == VIDEOBUF_QUEUED) { |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 209 | dprintk(1, "busy: buffer #%d queued\n", i); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 210 | return 1; |
| 211 | } |
Brandon Philips | 0fc0686 | 2007-11-06 20:02:36 -0300 | [diff] [blame] | 212 | if (q->bufs[i]->state == VIDEOBUF_ACTIVE) { |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 213 | dprintk(1, "busy: buffer #%d avtive\n", i); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 214 | return 1; |
| 215 | } |
| 216 | } |
| 217 | return 0; |
| 218 | } |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 219 | EXPORT_SYMBOL_GPL(videobuf_queue_is_busy); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 220 | |
Pawel Osciak | a438d6d | 2010-05-11 10:36:29 -0300 | [diff] [blame] | 221 | /** |
| 222 | * __videobuf_free() - free all the buffers and their control structures |
| 223 | * |
| 224 | * This function can only be called if streaming/reading is off, i.e. no buffers |
| 225 | * are under control of the driver. |
| 226 | */ |
| 227 | /* Locking: Caller holds q->vb_lock */ |
| 228 | static int __videobuf_free(struct videobuf_queue *q) |
| 229 | { |
| 230 | int i; |
| 231 | |
| 232 | dprintk(1, "%s\n", __func__); |
| 233 | if (!q) |
| 234 | return 0; |
| 235 | |
| 236 | if (q->streaming || q->reading) { |
| 237 | dprintk(1, "Cannot free buffers when streaming or reading\n"); |
| 238 | return -EBUSY; |
| 239 | } |
| 240 | |
| 241 | MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS); |
| 242 | |
| 243 | for (i = 0; i < VIDEO_MAX_FRAME; i++) |
| 244 | if (q->bufs[i] && q->bufs[i]->map) { |
| 245 | dprintk(1, "Cannot free mmapped buffers\n"); |
| 246 | return -EBUSY; |
| 247 | } |
| 248 | |
| 249 | for (i = 0; i < VIDEO_MAX_FRAME; i++) { |
| 250 | if (NULL == q->bufs[i]) |
| 251 | continue; |
| 252 | q->ops->buf_release(q, q->bufs[i]); |
| 253 | kfree(q->bufs[i]); |
| 254 | q->bufs[i] = NULL; |
| 255 | } |
| 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
Mauro Carvalho Chehab | 64f9477 | 2008-01-31 13:57:53 -0300 | [diff] [blame] | 260 | /* Locking: Caller holds q->vb_lock */ |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 261 | void videobuf_queue_cancel(struct videobuf_queue *q) |
| 262 | { |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 263 | unsigned long flags = 0; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 264 | int i; |
| 265 | |
Brandon Philips | 137d1cb | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 266 | q->streaming = 0; |
| 267 | q->reading = 0; |
| 268 | wake_up_interruptible_sync(&q->wait); |
| 269 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 270 | /* remove queued buffers from list */ |
Brandon Philips | 0cf4dae | 2008-03-28 10:18:33 -0700 | [diff] [blame] | 271 | spin_lock_irqsave(q->irqlock, flags); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 272 | for (i = 0; i < VIDEO_MAX_FRAME; i++) { |
| 273 | if (NULL == q->bufs[i]) |
| 274 | continue; |
Brandon Philips | 0fc0686 | 2007-11-06 20:02:36 -0300 | [diff] [blame] | 275 | if (q->bufs[i]->state == VIDEOBUF_QUEUED) { |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 276 | list_del(&q->bufs[i]->queue); |
Brandon Philips | 0fc0686 | 2007-11-06 20:02:36 -0300 | [diff] [blame] | 277 | q->bufs[i]->state = VIDEOBUF_ERROR; |
Brandon Philips | b608f43 | 2008-04-02 18:10:57 -0300 | [diff] [blame] | 278 | wake_up_all(&q->bufs[i]->done); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 279 | } |
| 280 | } |
Brandon Philips | 0cf4dae | 2008-03-28 10:18:33 -0700 | [diff] [blame] | 281 | spin_unlock_irqrestore(q->irqlock, flags); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 282 | |
| 283 | /* free all buffers + clear queue */ |
| 284 | for (i = 0; i < VIDEO_MAX_FRAME; i++) { |
| 285 | if (NULL == q->bufs[i]) |
| 286 | continue; |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 287 | q->ops->buf_release(q, q->bufs[i]); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 288 | } |
| 289 | INIT_LIST_HEAD(&q->stream); |
| 290 | } |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 291 | EXPORT_SYMBOL_GPL(videobuf_queue_cancel); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 292 | |
| 293 | /* --------------------------------------------------------------------- */ |
| 294 | |
Mauro Carvalho Chehab | 64f9477 | 2008-01-31 13:57:53 -0300 | [diff] [blame] | 295 | /* Locking: Caller holds q->vb_lock */ |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 296 | enum v4l2_field videobuf_next_field(struct videobuf_queue *q) |
| 297 | { |
| 298 | enum v4l2_field field = q->field; |
| 299 | |
| 300 | BUG_ON(V4L2_FIELD_ANY == field); |
| 301 | |
| 302 | if (V4L2_FIELD_ALTERNATE == field) { |
| 303 | if (V4L2_FIELD_TOP == q->last) { |
| 304 | field = V4L2_FIELD_BOTTOM; |
| 305 | q->last = V4L2_FIELD_BOTTOM; |
| 306 | } else { |
| 307 | field = V4L2_FIELD_TOP; |
| 308 | q->last = V4L2_FIELD_TOP; |
| 309 | } |
| 310 | } |
| 311 | return field; |
| 312 | } |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 313 | EXPORT_SYMBOL_GPL(videobuf_next_field); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 314 | |
Mauro Carvalho Chehab | 64f9477 | 2008-01-31 13:57:53 -0300 | [diff] [blame] | 315 | /* Locking: Caller holds q->vb_lock */ |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 316 | static void videobuf_status(struct videobuf_queue *q, struct v4l2_buffer *b, |
| 317 | struct videobuf_buffer *vb, enum v4l2_buf_type type) |
| 318 | { |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 319 | MAGIC_CHECK(vb->magic, MAGIC_BUFFER); |
| 320 | MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 321 | |
| 322 | b->index = vb->i; |
| 323 | b->type = type; |
| 324 | |
| 325 | b->memory = vb->memory; |
| 326 | switch (b->memory) { |
| 327 | case V4L2_MEMORY_MMAP: |
| 328 | b->m.offset = vb->boff; |
| 329 | b->length = vb->bsize; |
| 330 | break; |
| 331 | case V4L2_MEMORY_USERPTR: |
| 332 | b->m.userptr = vb->baddr; |
| 333 | b->length = vb->bsize; |
| 334 | break; |
| 335 | case V4L2_MEMORY_OVERLAY: |
| 336 | b->m.offset = vb->boff; |
| 337 | break; |
| 338 | } |
| 339 | |
| 340 | b->flags = 0; |
Mauro Carvalho Chehab | 851c0c9 | 2007-09-27 18:25:44 -0300 | [diff] [blame] | 341 | if (vb->map) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 342 | b->flags |= V4L2_BUF_FLAG_MAPPED; |
| 343 | |
| 344 | switch (vb->state) { |
Brandon Philips | 0fc0686 | 2007-11-06 20:02:36 -0300 | [diff] [blame] | 345 | case VIDEOBUF_PREPARED: |
| 346 | case VIDEOBUF_QUEUED: |
| 347 | case VIDEOBUF_ACTIVE: |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 348 | b->flags |= V4L2_BUF_FLAG_QUEUED; |
| 349 | break; |
Brandon Philips | 0fc0686 | 2007-11-06 20:02:36 -0300 | [diff] [blame] | 350 | case VIDEOBUF_ERROR: |
Hans Verkuil | b2dfd1a | 2010-04-28 04:05:22 -0300 | [diff] [blame] | 351 | b->flags |= V4L2_BUF_FLAG_ERROR; |
| 352 | /* fall through */ |
| 353 | case VIDEOBUF_DONE: |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 354 | b->flags |= V4L2_BUF_FLAG_DONE; |
| 355 | break; |
Brandon Philips | 0fc0686 | 2007-11-06 20:02:36 -0300 | [diff] [blame] | 356 | case VIDEOBUF_NEEDS_INIT: |
| 357 | case VIDEOBUF_IDLE: |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 358 | /* nothing */ |
| 359 | break; |
| 360 | } |
| 361 | |
| 362 | if (vb->input != UNSET) { |
| 363 | b->flags |= V4L2_BUF_FLAG_INPUT; |
| 364 | b->input = vb->input; |
| 365 | } |
| 366 | |
| 367 | b->field = vb->field; |
| 368 | b->timestamp = vb->ts; |
| 369 | b->bytesused = vb->size; |
| 370 | b->sequence = vb->field_count >> 1; |
| 371 | } |
| 372 | |
Brandon Philips | 19bc513 | 2007-11-13 20:05:38 -0300 | [diff] [blame] | 373 | int videobuf_mmap_free(struct videobuf_queue *q) |
| 374 | { |
| 375 | int ret; |
Hans Verkuil | 9739768 | 2010-09-20 17:24:30 -0300 | [diff] [blame] | 376 | videobuf_queue_lock(q); |
Pawel Osciak | a438d6d | 2010-05-11 10:36:29 -0300 | [diff] [blame] | 377 | ret = __videobuf_free(q); |
Hans Verkuil | 9739768 | 2010-09-20 17:24:30 -0300 | [diff] [blame] | 378 | videobuf_queue_unlock(q); |
Brandon Philips | 19bc513 | 2007-11-13 20:05:38 -0300 | [diff] [blame] | 379 | return ret; |
| 380 | } |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 381 | EXPORT_SYMBOL_GPL(videobuf_mmap_free); |
Brandon Philips | 19bc513 | 2007-11-13 20:05:38 -0300 | [diff] [blame] | 382 | |
Mauro Carvalho Chehab | 64f9477 | 2008-01-31 13:57:53 -0300 | [diff] [blame] | 383 | /* Locking: Caller holds q->vb_lock */ |
Arjan van de Ven | 81b2dbc | 2008-05-20 09:53:52 -0700 | [diff] [blame] | 384 | int __videobuf_mmap_setup(struct videobuf_queue *q, |
Brandon Philips | 19bc513 | 2007-11-13 20:05:38 -0300 | [diff] [blame] | 385 | unsigned int bcount, unsigned int bsize, |
| 386 | enum v4l2_memory memory) |
| 387 | { |
| 388 | unsigned int i; |
| 389 | int err; |
| 390 | |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 391 | MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS); |
Brandon Philips | 19bc513 | 2007-11-13 20:05:38 -0300 | [diff] [blame] | 392 | |
Pawel Osciak | a438d6d | 2010-05-11 10:36:29 -0300 | [diff] [blame] | 393 | err = __videobuf_free(q); |
Brandon Philips | 19bc513 | 2007-11-13 20:05:38 -0300 | [diff] [blame] | 394 | if (0 != err) |
| 395 | return err; |
| 396 | |
| 397 | /* Allocate and initialize buffers */ |
| 398 | for (i = 0; i < bcount; i++) { |
Pawel Osciak | 33c3828 | 2010-05-11 10:36:28 -0300 | [diff] [blame] | 399 | q->bufs[i] = videobuf_alloc_vb(q); |
Brandon Philips | 19bc513 | 2007-11-13 20:05:38 -0300 | [diff] [blame] | 400 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 401 | if (NULL == q->bufs[i]) |
Brandon Philips | 19bc513 | 2007-11-13 20:05:38 -0300 | [diff] [blame] | 402 | break; |
| 403 | |
| 404 | q->bufs[i]->i = i; |
| 405 | q->bufs[i]->input = UNSET; |
| 406 | q->bufs[i]->memory = memory; |
| 407 | q->bufs[i]->bsize = bsize; |
| 408 | switch (memory) { |
| 409 | case V4L2_MEMORY_MMAP: |
Tuukka Toivonen | 7cbefad | 2009-07-23 10:56:25 -0300 | [diff] [blame] | 410 | q->bufs[i]->boff = PAGE_ALIGN(bsize) * i; |
Brandon Philips | 19bc513 | 2007-11-13 20:05:38 -0300 | [diff] [blame] | 411 | break; |
| 412 | case V4L2_MEMORY_USERPTR: |
| 413 | case V4L2_MEMORY_OVERLAY: |
| 414 | /* nothing */ |
| 415 | break; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | if (!i) |
| 420 | return -ENOMEM; |
| 421 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 422 | dprintk(1, "mmap setup: %d buffers, %d bytes each\n", i, bsize); |
Brandon Philips | 19bc513 | 2007-11-13 20:05:38 -0300 | [diff] [blame] | 423 | |
| 424 | return i; |
| 425 | } |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 426 | EXPORT_SYMBOL_GPL(__videobuf_mmap_setup); |
Brandon Philips | 19bc513 | 2007-11-13 20:05:38 -0300 | [diff] [blame] | 427 | |
| 428 | int videobuf_mmap_setup(struct videobuf_queue *q, |
| 429 | unsigned int bcount, unsigned int bsize, |
| 430 | enum v4l2_memory memory) |
| 431 | { |
| 432 | int ret; |
Hans Verkuil | 9739768 | 2010-09-20 17:24:30 -0300 | [diff] [blame] | 433 | videobuf_queue_lock(q); |
Brandon Philips | 19bc513 | 2007-11-13 20:05:38 -0300 | [diff] [blame] | 434 | ret = __videobuf_mmap_setup(q, bcount, bsize, memory); |
Hans Verkuil | 9739768 | 2010-09-20 17:24:30 -0300 | [diff] [blame] | 435 | videobuf_queue_unlock(q); |
Brandon Philips | 19bc513 | 2007-11-13 20:05:38 -0300 | [diff] [blame] | 436 | return ret; |
| 437 | } |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 438 | EXPORT_SYMBOL_GPL(videobuf_mmap_setup); |
Brandon Philips | 19bc513 | 2007-11-13 20:05:38 -0300 | [diff] [blame] | 439 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 440 | int videobuf_reqbufs(struct videobuf_queue *q, |
| 441 | struct v4l2_requestbuffers *req) |
| 442 | { |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 443 | unsigned int size, count; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 444 | int retval; |
| 445 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 446 | if (req->count < 1) { |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 447 | dprintk(1, "reqbufs: count invalid (%d)\n", req->count); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 448 | return -EINVAL; |
| 449 | } |
Brandon Philips | 19bc513 | 2007-11-13 20:05:38 -0300 | [diff] [blame] | 450 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 451 | if (req->memory != V4L2_MEMORY_MMAP && |
| 452 | req->memory != V4L2_MEMORY_USERPTR && |
| 453 | req->memory != V4L2_MEMORY_OVERLAY) { |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 454 | dprintk(1, "reqbufs: memory type invalid\n"); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 455 | return -EINVAL; |
| 456 | } |
| 457 | |
Hans Verkuil | 9739768 | 2010-09-20 17:24:30 -0300 | [diff] [blame] | 458 | videobuf_queue_lock(q); |
Brandon Philips | 19bc513 | 2007-11-13 20:05:38 -0300 | [diff] [blame] | 459 | if (req->type != q->type) { |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 460 | dprintk(1, "reqbufs: queue type invalid\n"); |
Brandon Philips | 19bc513 | 2007-11-13 20:05:38 -0300 | [diff] [blame] | 461 | retval = -EINVAL; |
| 462 | goto done; |
| 463 | } |
| 464 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 465 | if (q->streaming) { |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 466 | dprintk(1, "reqbufs: streaming already exists\n"); |
Brandon Philips | 00f98d0 | 2007-09-27 20:55:28 -0300 | [diff] [blame] | 467 | retval = -EBUSY; |
| 468 | goto done; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 469 | } |
| 470 | if (!list_empty(&q->stream)) { |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 471 | dprintk(1, "reqbufs: stream running\n"); |
Brandon Philips | 00f98d0 | 2007-09-27 20:55:28 -0300 | [diff] [blame] | 472 | retval = -EBUSY; |
| 473 | goto done; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 474 | } |
| 475 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 476 | count = req->count; |
| 477 | if (count > VIDEO_MAX_FRAME) |
| 478 | count = VIDEO_MAX_FRAME; |
| 479 | size = 0; |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 480 | q->ops->buf_setup(q, &count, &size); |
Márton Németh | 296372e | 2009-11-22 18:03:05 -0300 | [diff] [blame] | 481 | dprintk(1, "reqbufs: bufs=%d, size=0x%x [%u pages total]\n", |
| 482 | count, size, |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 483 | (unsigned int)((count * PAGE_ALIGN(size)) >> PAGE_SHIFT)); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 484 | |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 485 | retval = __videobuf_mmap_setup(q, count, size, req->memory); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 486 | if (retval < 0) { |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 487 | dprintk(1, "reqbufs: mmap setup returned %d\n", retval); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 488 | goto done; |
| 489 | } |
| 490 | |
Brandon Philips | 49ee718 | 2007-10-05 16:26:27 -0300 | [diff] [blame] | 491 | req->count = retval; |
Németh Márton | 925d74a | 2009-04-29 15:57:24 -0300 | [diff] [blame] | 492 | retval = 0; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 493 | |
| 494 | done: |
Hans Verkuil | 9739768 | 2010-09-20 17:24:30 -0300 | [diff] [blame] | 495 | videobuf_queue_unlock(q); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 496 | return retval; |
| 497 | } |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 498 | EXPORT_SYMBOL_GPL(videobuf_reqbufs); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 499 | |
| 500 | int videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b) |
| 501 | { |
Brandon Philips | 19bc513 | 2007-11-13 20:05:38 -0300 | [diff] [blame] | 502 | int ret = -EINVAL; |
| 503 | |
Hans Verkuil | 9739768 | 2010-09-20 17:24:30 -0300 | [diff] [blame] | 504 | videobuf_queue_lock(q); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 505 | if (unlikely(b->type != q->type)) { |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 506 | dprintk(1, "querybuf: Wrong type.\n"); |
Brandon Philips | 19bc513 | 2007-11-13 20:05:38 -0300 | [diff] [blame] | 507 | goto done; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 508 | } |
Roel Kluin | 223ffe5 | 2009-05-02 16:38:47 -0300 | [diff] [blame] | 509 | if (unlikely(b->index >= VIDEO_MAX_FRAME)) { |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 510 | dprintk(1, "querybuf: index out of range.\n"); |
Brandon Philips | 19bc513 | 2007-11-13 20:05:38 -0300 | [diff] [blame] | 511 | goto done; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 512 | } |
| 513 | if (unlikely(NULL == q->bufs[b->index])) { |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 514 | dprintk(1, "querybuf: buffer is null.\n"); |
Brandon Philips | 19bc513 | 2007-11-13 20:05:38 -0300 | [diff] [blame] | 515 | goto done; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 516 | } |
Brandon Philips | 19bc513 | 2007-11-13 20:05:38 -0300 | [diff] [blame] | 517 | |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 518 | videobuf_status(q, b, q->bufs[b->index], q->type); |
Brandon Philips | 19bc513 | 2007-11-13 20:05:38 -0300 | [diff] [blame] | 519 | |
| 520 | ret = 0; |
| 521 | done: |
Hans Verkuil | 9739768 | 2010-09-20 17:24:30 -0300 | [diff] [blame] | 522 | videobuf_queue_unlock(q); |
Brandon Philips | 19bc513 | 2007-11-13 20:05:38 -0300 | [diff] [blame] | 523 | return ret; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 524 | } |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 525 | EXPORT_SYMBOL_GPL(videobuf_querybuf); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 526 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 527 | int videobuf_qbuf(struct videobuf_queue *q, struct v4l2_buffer *b) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 528 | { |
| 529 | struct videobuf_buffer *buf; |
| 530 | enum v4l2_field field; |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 531 | unsigned long flags = 0; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 532 | int retval; |
| 533 | |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 534 | MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 535 | |
Maxim Levitsky | 9900132 | 2007-09-27 20:34:09 -0300 | [diff] [blame] | 536 | if (b->memory == V4L2_MEMORY_MMAP) |
| 537 | down_read(¤t->mm->mmap_sem); |
| 538 | |
Hans Verkuil | 9739768 | 2010-09-20 17:24:30 -0300 | [diff] [blame] | 539 | videobuf_queue_lock(q); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 540 | retval = -EBUSY; |
| 541 | if (q->reading) { |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 542 | dprintk(1, "qbuf: Reading running...\n"); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 543 | goto done; |
| 544 | } |
| 545 | retval = -EINVAL; |
| 546 | if (b->type != q->type) { |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 547 | dprintk(1, "qbuf: Wrong type.\n"); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 548 | goto done; |
| 549 | } |
Roel Kluin | 223ffe5 | 2009-05-02 16:38:47 -0300 | [diff] [blame] | 550 | if (b->index >= VIDEO_MAX_FRAME) { |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 551 | dprintk(1, "qbuf: index out of range.\n"); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 552 | goto done; |
| 553 | } |
| 554 | buf = q->bufs[b->index]; |
| 555 | if (NULL == buf) { |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 556 | dprintk(1, "qbuf: buffer is null.\n"); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 557 | goto done; |
| 558 | } |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 559 | MAGIC_CHECK(buf->magic, MAGIC_BUFFER); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 560 | if (buf->memory != b->memory) { |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 561 | dprintk(1, "qbuf: memory type is wrong.\n"); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 562 | goto done; |
| 563 | } |
Brandon Philips | 0fc0686 | 2007-11-06 20:02:36 -0300 | [diff] [blame] | 564 | if (buf->state != VIDEOBUF_NEEDS_INIT && buf->state != VIDEOBUF_IDLE) { |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 565 | dprintk(1, "qbuf: buffer is already queued or active.\n"); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 566 | goto done; |
| 567 | } |
| 568 | |
| 569 | if (b->flags & V4L2_BUF_FLAG_INPUT) { |
| 570 | if (b->input >= q->inputs) { |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 571 | dprintk(1, "qbuf: wrong input.\n"); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 572 | goto done; |
| 573 | } |
| 574 | buf->input = b->input; |
| 575 | } else { |
| 576 | buf->input = UNSET; |
| 577 | } |
| 578 | |
| 579 | switch (b->memory) { |
| 580 | case V4L2_MEMORY_MMAP: |
| 581 | if (0 == buf->baddr) { |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 582 | dprintk(1, "qbuf: mmap requested " |
| 583 | "but buffer addr is zero!\n"); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 584 | goto done; |
| 585 | } |
Pawel Osciak | 96f2ec6 | 2010-04-21 06:44:27 -0300 | [diff] [blame] | 586 | if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT |
| 587 | || q->type == V4L2_BUF_TYPE_VBI_OUTPUT |
| 588 | || q->type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) { |
| 589 | buf->size = b->bytesused; |
| 590 | buf->field = b->field; |
| 591 | buf->ts = b->timestamp; |
| 592 | } |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 593 | break; |
| 594 | case V4L2_MEMORY_USERPTR: |
| 595 | if (b->length < buf->bsize) { |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 596 | dprintk(1, "qbuf: buffer length is not enough\n"); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 597 | goto done; |
| 598 | } |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 599 | if (VIDEOBUF_NEEDS_INIT != buf->state && |
| 600 | buf->baddr != b->m.userptr) |
| 601 | q->ops->buf_release(q, buf); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 602 | buf->baddr = b->m.userptr; |
| 603 | break; |
| 604 | case V4L2_MEMORY_OVERLAY: |
| 605 | buf->boff = b->m.offset; |
| 606 | break; |
| 607 | default: |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 608 | dprintk(1, "qbuf: wrong memory type\n"); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 609 | goto done; |
| 610 | } |
| 611 | |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 612 | dprintk(1, "qbuf: requesting next field\n"); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 613 | field = videobuf_next_field(q); |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 614 | retval = q->ops->buf_prepare(q, buf, field); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 615 | if (0 != retval) { |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 616 | dprintk(1, "qbuf: buffer_prepare returned %d\n", retval); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 617 | goto done; |
| 618 | } |
| 619 | |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 620 | list_add_tail(&buf->stream, &q->stream); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 621 | if (q->streaming) { |
Brandon Philips | 0cf4dae | 2008-03-28 10:18:33 -0700 | [diff] [blame] | 622 | spin_lock_irqsave(q->irqlock, flags); |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 623 | q->ops->buf_queue(q, buf); |
Brandon Philips | 0cf4dae | 2008-03-28 10:18:33 -0700 | [diff] [blame] | 624 | spin_unlock_irqrestore(q->irqlock, flags); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 625 | } |
Hans Verkuil | 771075b | 2010-03-13 11:47:06 -0300 | [diff] [blame] | 626 | dprintk(1, "qbuf: succeeded\n"); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 627 | retval = 0; |
Brandon Philips | 137d1cb | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 628 | wake_up_interruptible_sync(&q->wait); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 629 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 630 | done: |
Hans Verkuil | 9739768 | 2010-09-20 17:24:30 -0300 | [diff] [blame] | 631 | videobuf_queue_unlock(q); |
Maxim Levitsky | 9900132 | 2007-09-27 20:34:09 -0300 | [diff] [blame] | 632 | |
| 633 | if (b->memory == V4L2_MEMORY_MMAP) |
| 634 | up_read(¤t->mm->mmap_sem); |
| 635 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 636 | return retval; |
| 637 | } |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 638 | EXPORT_SYMBOL_GPL(videobuf_qbuf); |
Brandon Philips | 137d1cb | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 639 | |
| 640 | /* Locking: Caller holds q->vb_lock */ |
| 641 | static int stream_next_buffer_check_queue(struct videobuf_queue *q, int noblock) |
| 642 | { |
| 643 | int retval; |
| 644 | |
| 645 | checks: |
| 646 | if (!q->streaming) { |
| 647 | dprintk(1, "next_buffer: Not streaming\n"); |
| 648 | retval = -EINVAL; |
| 649 | goto done; |
| 650 | } |
| 651 | |
| 652 | if (list_empty(&q->stream)) { |
| 653 | if (noblock) { |
| 654 | retval = -EAGAIN; |
| 655 | dprintk(2, "next_buffer: no buffers to dequeue\n"); |
| 656 | goto done; |
| 657 | } else { |
| 658 | dprintk(2, "next_buffer: waiting on buffer\n"); |
| 659 | |
| 660 | /* Drop lock to avoid deadlock with qbuf */ |
Hans Verkuil | 9739768 | 2010-09-20 17:24:30 -0300 | [diff] [blame] | 661 | videobuf_queue_unlock(q); |
Brandon Philips | 137d1cb | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 662 | |
| 663 | /* Checking list_empty and streaming is safe without |
| 664 | * locks because we goto checks to validate while |
| 665 | * holding locks before proceeding */ |
| 666 | retval = wait_event_interruptible(q->wait, |
| 667 | !list_empty(&q->stream) || !q->streaming); |
Hans Verkuil | 9739768 | 2010-09-20 17:24:30 -0300 | [diff] [blame] | 668 | videobuf_queue_lock(q); |
Brandon Philips | 137d1cb | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 669 | |
| 670 | if (retval) |
| 671 | goto done; |
| 672 | |
| 673 | goto checks; |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | retval = 0; |
| 678 | |
| 679 | done: |
| 680 | return retval; |
| 681 | } |
| 682 | |
Brandon Philips | 137d1cb | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 683 | /* Locking: Caller holds q->vb_lock */ |
| 684 | static int stream_next_buffer(struct videobuf_queue *q, |
| 685 | struct videobuf_buffer **vb, int nonblocking) |
| 686 | { |
| 687 | int retval; |
| 688 | struct videobuf_buffer *buf = NULL; |
| 689 | |
| 690 | retval = stream_next_buffer_check_queue(q, nonblocking); |
| 691 | if (retval) |
| 692 | goto done; |
| 693 | |
| 694 | buf = list_entry(q->stream.next, struct videobuf_buffer, stream); |
Hans Verkuil | 0e0809a | 2010-09-26 09:01:26 -0300 | [diff] [blame] | 695 | retval = videobuf_waiton(q, buf, nonblocking, 1); |
Brandon Philips | 137d1cb | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 696 | if (retval < 0) |
| 697 | goto done; |
| 698 | |
| 699 | *vb = buf; |
| 700 | done: |
| 701 | return retval; |
| 702 | } |
| 703 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 704 | int videobuf_dqbuf(struct videobuf_queue *q, |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 705 | struct v4l2_buffer *b, int nonblocking) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 706 | { |
Brandon Philips | 137d1cb | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 707 | struct videobuf_buffer *buf = NULL; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 708 | int retval; |
| 709 | |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 710 | MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 711 | |
Hans Verkuil | b2dfd1a | 2010-04-28 04:05:22 -0300 | [diff] [blame] | 712 | memset(b, 0, sizeof(*b)); |
Hans Verkuil | 9739768 | 2010-09-20 17:24:30 -0300 | [diff] [blame] | 713 | videobuf_queue_lock(q); |
Brandon Philips | 137d1cb | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 714 | |
| 715 | retval = stream_next_buffer(q, &buf, nonblocking); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 716 | if (retval < 0) { |
Brandon Philips | 137d1cb | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 717 | dprintk(1, "dqbuf: next_buffer error: %i\n", retval); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 718 | goto done; |
| 719 | } |
Brandon Philips | 137d1cb | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 720 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 721 | switch (buf->state) { |
Brandon Philips | 0fc0686 | 2007-11-06 20:02:36 -0300 | [diff] [blame] | 722 | case VIDEOBUF_ERROR: |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 723 | dprintk(1, "dqbuf: state is error\n"); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 724 | break; |
Brandon Philips | 0fc0686 | 2007-11-06 20:02:36 -0300 | [diff] [blame] | 725 | case VIDEOBUF_DONE: |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 726 | dprintk(1, "dqbuf: state is done\n"); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 727 | break; |
| 728 | default: |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 729 | dprintk(1, "dqbuf: state invalid\n"); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 730 | retval = -EINVAL; |
| 731 | goto done; |
| 732 | } |
Hans Verkuil | b2dfd1a | 2010-04-28 04:05:22 -0300 | [diff] [blame] | 733 | CALL(q, sync, q, buf); |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 734 | videobuf_status(q, b, buf, q->type); |
Hans Verkuil | b2dfd1a | 2010-04-28 04:05:22 -0300 | [diff] [blame] | 735 | list_del(&buf->stream); |
| 736 | buf->state = VIDEOBUF_IDLE; |
| 737 | b->flags &= ~V4L2_BUF_FLAG_DONE; |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 738 | done: |
Hans Verkuil | 9739768 | 2010-09-20 17:24:30 -0300 | [diff] [blame] | 739 | videobuf_queue_unlock(q); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 740 | return retval; |
| 741 | } |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 742 | EXPORT_SYMBOL_GPL(videobuf_dqbuf); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 743 | |
| 744 | int videobuf_streamon(struct videobuf_queue *q) |
| 745 | { |
| 746 | struct videobuf_buffer *buf; |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 747 | unsigned long flags = 0; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 748 | int retval; |
| 749 | |
Hans Verkuil | 9739768 | 2010-09-20 17:24:30 -0300 | [diff] [blame] | 750 | videobuf_queue_lock(q); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 751 | retval = -EBUSY; |
| 752 | if (q->reading) |
| 753 | goto done; |
| 754 | retval = 0; |
| 755 | if (q->streaming) |
| 756 | goto done; |
| 757 | q->streaming = 1; |
Brandon Philips | 0cf4dae | 2008-03-28 10:18:33 -0700 | [diff] [blame] | 758 | spin_lock_irqsave(q->irqlock, flags); |
Trent Piepho | a991f44 | 2007-10-10 05:37:43 -0300 | [diff] [blame] | 759 | list_for_each_entry(buf, &q->stream, stream) |
Brandon Philips | 0fc0686 | 2007-11-06 20:02:36 -0300 | [diff] [blame] | 760 | if (buf->state == VIDEOBUF_PREPARED) |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 761 | q->ops->buf_queue(q, buf); |
Brandon Philips | 0cf4dae | 2008-03-28 10:18:33 -0700 | [diff] [blame] | 762 | spin_unlock_irqrestore(q->irqlock, flags); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 763 | |
Brandon Philips | 137d1cb | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 764 | wake_up_interruptible_sync(&q->wait); |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 765 | done: |
Hans Verkuil | 9739768 | 2010-09-20 17:24:30 -0300 | [diff] [blame] | 766 | videobuf_queue_unlock(q); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 767 | return retval; |
| 768 | } |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 769 | EXPORT_SYMBOL_GPL(videobuf_streamon); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 770 | |
Mauro Carvalho Chehab | 64f9477 | 2008-01-31 13:57:53 -0300 | [diff] [blame] | 771 | /* Locking: Caller holds q->vb_lock */ |
Brandon Philips | 19bc513 | 2007-11-13 20:05:38 -0300 | [diff] [blame] | 772 | static int __videobuf_streamoff(struct videobuf_queue *q) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 773 | { |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 774 | if (!q->streaming) |
Brandon Philips | 19bc513 | 2007-11-13 20:05:38 -0300 | [diff] [blame] | 775 | return -EINVAL; |
| 776 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 777 | videobuf_queue_cancel(q); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 778 | |
Brandon Philips | 19bc513 | 2007-11-13 20:05:38 -0300 | [diff] [blame] | 779 | return 0; |
| 780 | } |
| 781 | |
| 782 | int videobuf_streamoff(struct videobuf_queue *q) |
| 783 | { |
| 784 | int retval; |
| 785 | |
Hans Verkuil | 9739768 | 2010-09-20 17:24:30 -0300 | [diff] [blame] | 786 | videobuf_queue_lock(q); |
Brandon Philips | 19bc513 | 2007-11-13 20:05:38 -0300 | [diff] [blame] | 787 | retval = __videobuf_streamoff(q); |
Hans Verkuil | 9739768 | 2010-09-20 17:24:30 -0300 | [diff] [blame] | 788 | videobuf_queue_unlock(q); |
Brandon Philips | 19bc513 | 2007-11-13 20:05:38 -0300 | [diff] [blame] | 789 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 790 | return retval; |
| 791 | } |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 792 | EXPORT_SYMBOL_GPL(videobuf_streamoff); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 793 | |
Mauro Carvalho Chehab | 64f9477 | 2008-01-31 13:57:53 -0300 | [diff] [blame] | 794 | /* Locking: Caller holds q->vb_lock */ |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 795 | static ssize_t videobuf_read_zerocopy(struct videobuf_queue *q, |
| 796 | char __user *data, |
| 797 | size_t count, loff_t *ppos) |
| 798 | { |
| 799 | enum v4l2_field field; |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 800 | unsigned long flags = 0; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 801 | int retval; |
| 802 | |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 803 | MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 804 | |
| 805 | /* setup stuff */ |
Pawel Osciak | 33c3828 | 2010-05-11 10:36:28 -0300 | [diff] [blame] | 806 | q->read_buf = videobuf_alloc_vb(q); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 807 | if (NULL == q->read_buf) |
| 808 | return -ENOMEM; |
| 809 | |
| 810 | q->read_buf->memory = V4L2_MEMORY_USERPTR; |
| 811 | q->read_buf->baddr = (unsigned long)data; |
| 812 | q->read_buf->bsize = count; |
| 813 | |
| 814 | field = videobuf_next_field(q); |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 815 | retval = q->ops->buf_prepare(q, q->read_buf, field); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 816 | if (0 != retval) |
| 817 | goto done; |
| 818 | |
| 819 | /* start capture & wait */ |
Brandon Philips | 0cf4dae | 2008-03-28 10:18:33 -0700 | [diff] [blame] | 820 | spin_lock_irqsave(q->irqlock, flags); |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 821 | q->ops->buf_queue(q, q->read_buf); |
Brandon Philips | 0cf4dae | 2008-03-28 10:18:33 -0700 | [diff] [blame] | 822 | spin_unlock_irqrestore(q->irqlock, flags); |
Hans Verkuil | 0e0809a | 2010-09-26 09:01:26 -0300 | [diff] [blame] | 823 | retval = videobuf_waiton(q, q->read_buf, 0, 0); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 824 | if (0 == retval) { |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 825 | CALL(q, sync, q, q->read_buf); |
Brandon Philips | 0fc0686 | 2007-11-06 20:02:36 -0300 | [diff] [blame] | 826 | if (VIDEOBUF_ERROR == q->read_buf->state) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 827 | retval = -EIO; |
| 828 | else |
| 829 | retval = q->read_buf->size; |
| 830 | } |
| 831 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 832 | done: |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 833 | /* cleanup */ |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 834 | q->ops->buf_release(q, q->read_buf); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 835 | kfree(q->read_buf); |
| 836 | q->read_buf = NULL; |
| 837 | return retval; |
| 838 | } |
| 839 | |
Hans Verkuil | 3711103 | 2010-03-28 09:22:53 -0300 | [diff] [blame] | 840 | static int __videobuf_copy_to_user(struct videobuf_queue *q, |
| 841 | struct videobuf_buffer *buf, |
| 842 | char __user *data, size_t count, |
| 843 | int nonblocking) |
| 844 | { |
| 845 | void *vaddr = CALL(q, vaddr, buf); |
| 846 | |
| 847 | /* copy to userspace */ |
| 848 | if (count > buf->size - q->read_off) |
| 849 | count = buf->size - q->read_off; |
| 850 | |
| 851 | if (copy_to_user(data, vaddr + q->read_off, count)) |
| 852 | return -EFAULT; |
| 853 | |
| 854 | return count; |
| 855 | } |
| 856 | |
| 857 | static int __videobuf_copy_stream(struct videobuf_queue *q, |
| 858 | struct videobuf_buffer *buf, |
| 859 | char __user *data, size_t count, size_t pos, |
| 860 | int vbihack, int nonblocking) |
| 861 | { |
| 862 | unsigned int *fc = CALL(q, vaddr, buf); |
| 863 | |
| 864 | if (vbihack) { |
| 865 | /* dirty, undocumented hack -- pass the frame counter |
| 866 | * within the last four bytes of each vbi data block. |
| 867 | * We need that one to maintain backward compatibility |
| 868 | * to all vbi decoding software out there ... */ |
| 869 | fc += (buf->size >> 2) - 1; |
| 870 | *fc = buf->field_count >> 1; |
| 871 | dprintk(1, "vbihack: %d\n", *fc); |
| 872 | } |
| 873 | |
| 874 | /* copy stuff using the common method */ |
| 875 | count = __videobuf_copy_to_user(q, buf, data, count, nonblocking); |
| 876 | |
| 877 | if ((count == -EFAULT) && (pos == 0)) |
| 878 | return -EFAULT; |
| 879 | |
| 880 | return count; |
| 881 | } |
| 882 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 883 | ssize_t videobuf_read_one(struct videobuf_queue *q, |
| 884 | char __user *data, size_t count, loff_t *ppos, |
| 885 | int nonblocking) |
| 886 | { |
| 887 | enum v4l2_field field; |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 888 | unsigned long flags = 0; |
Guennadi Liakhovetski | 7daa4a8 | 2008-04-22 14:46:03 -0300 | [diff] [blame] | 889 | unsigned size = 0, nbufs = 1; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 890 | int retval; |
| 891 | |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 892 | MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 893 | |
Hans Verkuil | 9739768 | 2010-09-20 17:24:30 -0300 | [diff] [blame] | 894 | videobuf_queue_lock(q); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 895 | |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 896 | q->ops->buf_setup(q, &nbufs, &size); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 897 | |
| 898 | if (NULL == q->read_buf && |
| 899 | count >= size && |
| 900 | !nonblocking) { |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 901 | retval = videobuf_read_zerocopy(q, data, count, ppos); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 902 | if (retval >= 0 || retval == -EIO) |
| 903 | /* ok, all done */ |
| 904 | goto done; |
| 905 | /* fallback to kernel bounce buffer on failures */ |
| 906 | } |
| 907 | |
| 908 | if (NULL == q->read_buf) { |
| 909 | /* need to capture a new frame */ |
| 910 | retval = -ENOMEM; |
Pawel Osciak | 33c3828 | 2010-05-11 10:36:28 -0300 | [diff] [blame] | 911 | q->read_buf = videobuf_alloc_vb(q); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 912 | |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 913 | dprintk(1, "video alloc=0x%p\n", q->read_buf); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 914 | if (NULL == q->read_buf) |
| 915 | goto done; |
| 916 | q->read_buf->memory = V4L2_MEMORY_USERPTR; |
| 917 | q->read_buf->bsize = count; /* preferred size */ |
| 918 | field = videobuf_next_field(q); |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 919 | retval = q->ops->buf_prepare(q, q->read_buf, field); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 920 | |
| 921 | if (0 != retval) { |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 922 | kfree(q->read_buf); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 923 | q->read_buf = NULL; |
| 924 | goto done; |
| 925 | } |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 926 | |
Brandon Philips | 0cf4dae | 2008-03-28 10:18:33 -0700 | [diff] [blame] | 927 | spin_lock_irqsave(q->irqlock, flags); |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 928 | q->ops->buf_queue(q, q->read_buf); |
Brandon Philips | 0cf4dae | 2008-03-28 10:18:33 -0700 | [diff] [blame] | 929 | spin_unlock_irqrestore(q->irqlock, flags); |
| 930 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 931 | q->read_off = 0; |
| 932 | } |
| 933 | |
| 934 | /* wait until capture is done */ |
Hans Verkuil | 0e0809a | 2010-09-26 09:01:26 -0300 | [diff] [blame] | 935 | retval = videobuf_waiton(q, q->read_buf, nonblocking, 1); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 936 | if (0 != retval) |
| 937 | goto done; |
| 938 | |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 939 | CALL(q, sync, q, q->read_buf); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 940 | |
Brandon Philips | 0fc0686 | 2007-11-06 20:02:36 -0300 | [diff] [blame] | 941 | if (VIDEOBUF_ERROR == q->read_buf->state) { |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 942 | /* catch I/O errors */ |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 943 | q->ops->buf_release(q, q->read_buf); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 944 | kfree(q->read_buf); |
| 945 | q->read_buf = NULL; |
| 946 | retval = -EIO; |
| 947 | goto done; |
| 948 | } |
| 949 | |
| 950 | /* Copy to userspace */ |
Hans Verkuil | 3711103 | 2010-03-28 09:22:53 -0300 | [diff] [blame] | 951 | retval = __videobuf_copy_to_user(q, q->read_buf, data, count, nonblocking); |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 952 | if (retval < 0) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 953 | goto done; |
| 954 | |
| 955 | q->read_off += retval; |
| 956 | if (q->read_off == q->read_buf->size) { |
| 957 | /* all data copied, cleanup */ |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 958 | q->ops->buf_release(q, q->read_buf); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 959 | kfree(q->read_buf); |
| 960 | q->read_buf = NULL; |
| 961 | } |
| 962 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 963 | done: |
Hans Verkuil | 9739768 | 2010-09-20 17:24:30 -0300 | [diff] [blame] | 964 | videobuf_queue_unlock(q); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 965 | return retval; |
| 966 | } |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 967 | EXPORT_SYMBOL_GPL(videobuf_read_one); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 968 | |
Mauro Carvalho Chehab | 64f9477 | 2008-01-31 13:57:53 -0300 | [diff] [blame] | 969 | /* Locking: Caller holds q->vb_lock */ |
Mauro Carvalho Chehab | 225ba90 | 2007-12-12 21:46:26 -0300 | [diff] [blame] | 970 | static int __videobuf_read_start(struct videobuf_queue *q) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 971 | { |
| 972 | enum v4l2_field field; |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 973 | unsigned long flags = 0; |
Brandon Philips | 49ee718 | 2007-10-05 16:26:27 -0300 | [diff] [blame] | 974 | unsigned int count = 0, size = 0; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 975 | int err, i; |
| 976 | |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 977 | q->ops->buf_setup(q, &count, &size); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 978 | if (count < 2) |
| 979 | count = 2; |
| 980 | if (count > VIDEO_MAX_FRAME) |
| 981 | count = VIDEO_MAX_FRAME; |
| 982 | size = PAGE_ALIGN(size); |
| 983 | |
Brandon Philips | 19bc513 | 2007-11-13 20:05:38 -0300 | [diff] [blame] | 984 | err = __videobuf_mmap_setup(q, count, size, V4L2_MEMORY_USERPTR); |
Brandon Philips | 49ee718 | 2007-10-05 16:26:27 -0300 | [diff] [blame] | 985 | if (err < 0) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 986 | return err; |
| 987 | |
Brandon Philips | 49ee718 | 2007-10-05 16:26:27 -0300 | [diff] [blame] | 988 | count = err; |
| 989 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 990 | for (i = 0; i < count; i++) { |
| 991 | field = videobuf_next_field(q); |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 992 | err = q->ops->buf_prepare(q, q->bufs[i], field); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 993 | if (err) |
| 994 | return err; |
| 995 | list_add_tail(&q->bufs[i]->stream, &q->stream); |
| 996 | } |
Brandon Philips | 0cf4dae | 2008-03-28 10:18:33 -0700 | [diff] [blame] | 997 | spin_lock_irqsave(q->irqlock, flags); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 998 | for (i = 0; i < count; i++) |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 999 | q->ops->buf_queue(q, q->bufs[i]); |
Brandon Philips | 0cf4dae | 2008-03-28 10:18:33 -0700 | [diff] [blame] | 1000 | spin_unlock_irqrestore(q->irqlock, flags); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 1001 | q->reading = 1; |
| 1002 | return 0; |
| 1003 | } |
| 1004 | |
Brandon Philips | 19bc513 | 2007-11-13 20:05:38 -0300 | [diff] [blame] | 1005 | static void __videobuf_read_stop(struct videobuf_queue *q) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 1006 | { |
| 1007 | int i; |
| 1008 | |
| 1009 | videobuf_queue_cancel(q); |
Pawel Osciak | a438d6d | 2010-05-11 10:36:29 -0300 | [diff] [blame] | 1010 | __videobuf_free(q); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 1011 | INIT_LIST_HEAD(&q->stream); |
| 1012 | for (i = 0; i < VIDEO_MAX_FRAME; i++) { |
| 1013 | if (NULL == q->bufs[i]) |
| 1014 | continue; |
| 1015 | kfree(q->bufs[i]); |
| 1016 | q->bufs[i] = NULL; |
| 1017 | } |
| 1018 | q->read_buf = NULL; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 1019 | } |
| 1020 | |
Mauro Carvalho Chehab | 19fb14579 | 2007-11-15 23:09:30 -0300 | [diff] [blame] | 1021 | int videobuf_read_start(struct videobuf_queue *q) |
| 1022 | { |
| 1023 | int rc; |
| 1024 | |
Hans Verkuil | 9739768 | 2010-09-20 17:24:30 -0300 | [diff] [blame] | 1025 | videobuf_queue_lock(q); |
Mauro Carvalho Chehab | 19fb14579 | 2007-11-15 23:09:30 -0300 | [diff] [blame] | 1026 | rc = __videobuf_read_start(q); |
Hans Verkuil | 9739768 | 2010-09-20 17:24:30 -0300 | [diff] [blame] | 1027 | videobuf_queue_unlock(q); |
Mauro Carvalho Chehab | 19fb14579 | 2007-11-15 23:09:30 -0300 | [diff] [blame] | 1028 | |
| 1029 | return rc; |
| 1030 | } |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 1031 | EXPORT_SYMBOL_GPL(videobuf_read_start); |
Mauro Carvalho Chehab | 19fb14579 | 2007-11-15 23:09:30 -0300 | [diff] [blame] | 1032 | |
Brandon Philips | 19bc513 | 2007-11-13 20:05:38 -0300 | [diff] [blame] | 1033 | void videobuf_read_stop(struct videobuf_queue *q) |
| 1034 | { |
Hans Verkuil | 9739768 | 2010-09-20 17:24:30 -0300 | [diff] [blame] | 1035 | videobuf_queue_lock(q); |
Brandon Philips | 19bc513 | 2007-11-13 20:05:38 -0300 | [diff] [blame] | 1036 | __videobuf_read_stop(q); |
Hans Verkuil | 9739768 | 2010-09-20 17:24:30 -0300 | [diff] [blame] | 1037 | videobuf_queue_unlock(q); |
Brandon Philips | 19bc513 | 2007-11-13 20:05:38 -0300 | [diff] [blame] | 1038 | } |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 1039 | EXPORT_SYMBOL_GPL(videobuf_read_stop); |
Brandon Philips | 19bc513 | 2007-11-13 20:05:38 -0300 | [diff] [blame] | 1040 | |
| 1041 | void videobuf_stop(struct videobuf_queue *q) |
| 1042 | { |
Hans Verkuil | 9739768 | 2010-09-20 17:24:30 -0300 | [diff] [blame] | 1043 | videobuf_queue_lock(q); |
Brandon Philips | 19bc513 | 2007-11-13 20:05:38 -0300 | [diff] [blame] | 1044 | |
| 1045 | if (q->streaming) |
| 1046 | __videobuf_streamoff(q); |
| 1047 | |
| 1048 | if (q->reading) |
| 1049 | __videobuf_read_stop(q); |
| 1050 | |
Hans Verkuil | 9739768 | 2010-09-20 17:24:30 -0300 | [diff] [blame] | 1051 | videobuf_queue_unlock(q); |
Brandon Philips | 19bc513 | 2007-11-13 20:05:38 -0300 | [diff] [blame] | 1052 | } |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 1053 | EXPORT_SYMBOL_GPL(videobuf_stop); |
Brandon Philips | 19bc513 | 2007-11-13 20:05:38 -0300 | [diff] [blame] | 1054 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 1055 | ssize_t videobuf_read_stream(struct videobuf_queue *q, |
| 1056 | char __user *data, size_t count, loff_t *ppos, |
| 1057 | int vbihack, int nonblocking) |
| 1058 | { |
| 1059 | int rc, retval; |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 1060 | unsigned long flags = 0; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 1061 | |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 1062 | MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 1063 | |
Harvey Harrison | 7e28adb | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 1064 | dprintk(2, "%s\n", __func__); |
Hans Verkuil | 9739768 | 2010-09-20 17:24:30 -0300 | [diff] [blame] | 1065 | videobuf_queue_lock(q); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 1066 | retval = -EBUSY; |
| 1067 | if (q->streaming) |
| 1068 | goto done; |
| 1069 | if (!q->reading) { |
Adrian Bunk | 3f84307 | 2007-12-12 16:44:54 -0300 | [diff] [blame] | 1070 | retval = __videobuf_read_start(q); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 1071 | if (retval < 0) |
| 1072 | goto done; |
| 1073 | } |
| 1074 | |
| 1075 | retval = 0; |
| 1076 | while (count > 0) { |
| 1077 | /* get / wait for data */ |
| 1078 | if (NULL == q->read_buf) { |
| 1079 | q->read_buf = list_entry(q->stream.next, |
| 1080 | struct videobuf_buffer, |
| 1081 | stream); |
| 1082 | list_del(&q->read_buf->stream); |
| 1083 | q->read_off = 0; |
| 1084 | } |
Hans Verkuil | 0e0809a | 2010-09-26 09:01:26 -0300 | [diff] [blame] | 1085 | rc = videobuf_waiton(q, q->read_buf, nonblocking, 1); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 1086 | if (rc < 0) { |
| 1087 | if (0 == retval) |
| 1088 | retval = rc; |
| 1089 | break; |
| 1090 | } |
| 1091 | |
Brandon Philips | 0fc0686 | 2007-11-06 20:02:36 -0300 | [diff] [blame] | 1092 | if (q->read_buf->state == VIDEOBUF_DONE) { |
Hans Verkuil | 3711103 | 2010-03-28 09:22:53 -0300 | [diff] [blame] | 1093 | rc = __videobuf_copy_stream(q, q->read_buf, data + retval, count, |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 1094 | retval, vbihack, nonblocking); |
| 1095 | if (rc < 0) { |
| 1096 | retval = rc; |
| 1097 | break; |
| 1098 | } |
| 1099 | retval += rc; |
| 1100 | count -= rc; |
| 1101 | q->read_off += rc; |
| 1102 | } else { |
| 1103 | /* some error */ |
| 1104 | q->read_off = q->read_buf->size; |
| 1105 | if (0 == retval) |
| 1106 | retval = -EIO; |
| 1107 | } |
| 1108 | |
| 1109 | /* requeue buffer when done with copying */ |
| 1110 | if (q->read_off == q->read_buf->size) { |
| 1111 | list_add_tail(&q->read_buf->stream, |
| 1112 | &q->stream); |
Brandon Philips | 0cf4dae | 2008-03-28 10:18:33 -0700 | [diff] [blame] | 1113 | spin_lock_irqsave(q->irqlock, flags); |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 1114 | q->ops->buf_queue(q, q->read_buf); |
Brandon Philips | 0cf4dae | 2008-03-28 10:18:33 -0700 | [diff] [blame] | 1115 | spin_unlock_irqrestore(q->irqlock, flags); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 1116 | q->read_buf = NULL; |
| 1117 | } |
| 1118 | if (retval < 0) |
| 1119 | break; |
| 1120 | } |
| 1121 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 1122 | done: |
Hans Verkuil | 9739768 | 2010-09-20 17:24:30 -0300 | [diff] [blame] | 1123 | videobuf_queue_unlock(q); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 1124 | return retval; |
| 1125 | } |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 1126 | EXPORT_SYMBOL_GPL(videobuf_read_stream); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 1127 | |
| 1128 | unsigned int videobuf_poll_stream(struct file *file, |
| 1129 | struct videobuf_queue *q, |
| 1130 | poll_table *wait) |
| 1131 | { |
| 1132 | struct videobuf_buffer *buf = NULL; |
| 1133 | unsigned int rc = 0; |
| 1134 | |
Hans Verkuil | 9739768 | 2010-09-20 17:24:30 -0300 | [diff] [blame] | 1135 | videobuf_queue_lock(q); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 1136 | if (q->streaming) { |
| 1137 | if (!list_empty(&q->stream)) |
| 1138 | buf = list_entry(q->stream.next, |
| 1139 | struct videobuf_buffer, stream); |
| 1140 | } else { |
| 1141 | if (!q->reading) |
Adrian Bunk | 3f84307 | 2007-12-12 16:44:54 -0300 | [diff] [blame] | 1142 | __videobuf_read_start(q); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 1143 | if (!q->reading) { |
| 1144 | rc = POLLERR; |
| 1145 | } else if (NULL == q->read_buf) { |
| 1146 | q->read_buf = list_entry(q->stream.next, |
| 1147 | struct videobuf_buffer, |
| 1148 | stream); |
| 1149 | list_del(&q->read_buf->stream); |
| 1150 | q->read_off = 0; |
| 1151 | } |
| 1152 | buf = q->read_buf; |
| 1153 | } |
| 1154 | if (!buf) |
| 1155 | rc = POLLERR; |
| 1156 | |
| 1157 | if (0 == rc) { |
| 1158 | poll_wait(file, &buf->done, wait); |
Brandon Philips | 0fc0686 | 2007-11-06 20:02:36 -0300 | [diff] [blame] | 1159 | if (buf->state == VIDEOBUF_DONE || |
Pawel Osciak | 9b55843 | 2010-03-29 05:16:31 -0300 | [diff] [blame] | 1160 | buf->state == VIDEOBUF_ERROR) { |
| 1161 | switch (q->type) { |
| 1162 | case V4L2_BUF_TYPE_VIDEO_OUTPUT: |
| 1163 | case V4L2_BUF_TYPE_VBI_OUTPUT: |
| 1164 | case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT: |
| 1165 | rc = POLLOUT | POLLWRNORM; |
| 1166 | break; |
| 1167 | default: |
| 1168 | rc = POLLIN | POLLRDNORM; |
| 1169 | break; |
| 1170 | } |
| 1171 | } |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 1172 | } |
Hans Verkuil | 9739768 | 2010-09-20 17:24:30 -0300 | [diff] [blame] | 1173 | videobuf_queue_unlock(q); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 1174 | return rc; |
| 1175 | } |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 1176 | EXPORT_SYMBOL_GPL(videobuf_poll_stream); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 1177 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 1178 | int videobuf_mmap_mapper(struct videobuf_queue *q, struct vm_area_struct *vma) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 1179 | { |
Hans Verkuil | 0b62b73 | 2010-03-28 09:09:05 -0300 | [diff] [blame] | 1180 | int rc = -EINVAL; |
| 1181 | int i; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 1182 | |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 1183 | MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 1184 | |
Hans Verkuil | 0b62b73 | 2010-03-28 09:09:05 -0300 | [diff] [blame] | 1185 | if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED)) { |
| 1186 | dprintk(1, "mmap appl bug: PROT_WRITE and MAP_SHARED are required\n"); |
| 1187 | return -EINVAL; |
| 1188 | } |
| 1189 | |
Hans Verkuil | 9739768 | 2010-09-20 17:24:30 -0300 | [diff] [blame] | 1190 | videobuf_queue_lock(q); |
Hans Verkuil | 0b62b73 | 2010-03-28 09:09:05 -0300 | [diff] [blame] | 1191 | for (i = 0; i < VIDEO_MAX_FRAME; i++) { |
| 1192 | struct videobuf_buffer *buf = q->bufs[i]; |
| 1193 | |
| 1194 | if (buf && buf->memory == V4L2_MEMORY_MMAP && |
| 1195 | buf->boff == (vma->vm_pgoff << PAGE_SHIFT)) { |
| 1196 | rc = CALL(q, mmap_mapper, q, buf, vma); |
| 1197 | break; |
| 1198 | } |
| 1199 | } |
Hans Verkuil | 9739768 | 2010-09-20 17:24:30 -0300 | [diff] [blame] | 1200 | videobuf_queue_unlock(q); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 1201 | |
Hans Verkuil | 0b62b73 | 2010-03-28 09:09:05 -0300 | [diff] [blame] | 1202 | return rc; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 1203 | } |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 1204 | EXPORT_SYMBOL_GPL(videobuf_mmap_mapper); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 1205 | |
| 1206 | #ifdef CONFIG_VIDEO_V4L1_COMPAT |
| 1207 | int videobuf_cgmbuf(struct videobuf_queue *q, |
| 1208 | struct video_mbuf *mbuf, int count) |
| 1209 | { |
| 1210 | struct v4l2_requestbuffers req; |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 1211 | int rc, i; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 1212 | |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 1213 | MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 1214 | |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 1215 | memset(&req, 0, sizeof(req)); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 1216 | req.type = q->type; |
| 1217 | req.count = count; |
| 1218 | req.memory = V4L2_MEMORY_MMAP; |
Mauro Carvalho Chehab | e2c7731 | 2007-12-10 10:53:20 -0300 | [diff] [blame] | 1219 | rc = videobuf_reqbufs(q, &req); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 1220 | if (rc < 0) |
| 1221 | return rc; |
| 1222 | |
| 1223 | mbuf->frames = req.count; |
| 1224 | mbuf->size = 0; |
| 1225 | for (i = 0; i < mbuf->frames; i++) { |
| 1226 | mbuf->offsets[i] = q->bufs[i]->boff; |
Tuukka Toivonen | 7cbefad | 2009-07-23 10:56:25 -0300 | [diff] [blame] | 1227 | mbuf->size += PAGE_ALIGN(q->bufs[i]->bsize); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 1228 | } |
| 1229 | |
| 1230 | return 0; |
| 1231 | } |
Pekka Enberg | a13625c | 2007-10-13 05:54:54 -0300 | [diff] [blame] | 1232 | EXPORT_SYMBOL_GPL(videobuf_cgmbuf); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 1233 | #endif |
| 1234 | |