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