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