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