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