Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * generic helper functions for handling video4linux capture buffers
0004  *
0005  * (c) 2007 Mauro Carvalho Chehab, <mchehab@kernel.org>
0006  *
0007  * Highly based on video-buf written originally by:
0008  * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org>
0009  * (c) 2006 Mauro Carvalho Chehab, <mchehab@kernel.org>
0010  * (c) 2006 Ted Walther and John Sokol
0011  */
0012 
0013 #include <linux/init.h>
0014 #include <linux/module.h>
0015 #include <linux/moduleparam.h>
0016 #include <linux/mm.h>
0017 #include <linux/sched.h>
0018 #include <linux/slab.h>
0019 #include <linux/interrupt.h>
0020 
0021 #include <media/videobuf-core.h>
0022 #include <media/v4l2-common.h>
0023 
0024 #define MAGIC_BUFFER 0x20070728
0025 #define MAGIC_CHECK(is, should)                     \
0026     do {                                \
0027         if (unlikely((is) != (should))) {           \
0028             printk(KERN_ERR                 \
0029                 "magic mismatch: %x (expected %x)\n",   \
0030                     is, should);            \
0031             BUG();                      \
0032         }                           \
0033     } while (0)
0034 
0035 static int debug;
0036 module_param(debug, int, 0644);
0037 
0038 MODULE_DESCRIPTION("helper module to manage video4linux buffers");
0039 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@kernel.org>");
0040 MODULE_LICENSE("GPL");
0041 
0042 #define dprintk(level, fmt, arg...)                 \
0043     do {                                \
0044         if (debug >= level)                 \
0045             printk(KERN_DEBUG "vbuf: " fmt, ## arg);    \
0046     } while (0)
0047 
0048 /* --------------------------------------------------------------------- */
0049 
0050 #define CALL(q, f, arg...)                      \
0051     ((q->int_ops->f) ? q->int_ops->f(arg) : 0)
0052 #define CALLPTR(q, f, arg...)                       \
0053     ((q->int_ops->f) ? q->int_ops->f(arg) : NULL)
0054 
0055 struct videobuf_buffer *videobuf_alloc_vb(struct videobuf_queue *q)
0056 {
0057     struct videobuf_buffer *vb;
0058 
0059     BUG_ON(q->msize < sizeof(*vb));
0060 
0061     if (!q->int_ops || !q->int_ops->alloc_vb) {
0062         printk(KERN_ERR "No specific ops defined!\n");
0063         BUG();
0064     }
0065 
0066     vb = q->int_ops->alloc_vb(q->msize);
0067     if (NULL != vb) {
0068         init_waitqueue_head(&vb->done);
0069         vb->magic = MAGIC_BUFFER;
0070     }
0071 
0072     return vb;
0073 }
0074 EXPORT_SYMBOL_GPL(videobuf_alloc_vb);
0075 
0076 static int state_neither_active_nor_queued(struct videobuf_queue *q,
0077                        struct videobuf_buffer *vb)
0078 {
0079     unsigned long flags;
0080     bool rc;
0081 
0082     spin_lock_irqsave(q->irqlock, flags);
0083     rc = vb->state != VIDEOBUF_ACTIVE && vb->state != VIDEOBUF_QUEUED;
0084     spin_unlock_irqrestore(q->irqlock, flags);
0085     return rc;
0086 };
0087 
0088 int videobuf_waiton(struct videobuf_queue *q, struct videobuf_buffer *vb,
0089         int non_blocking, int intr)
0090 {
0091     bool is_ext_locked;
0092     int ret = 0;
0093 
0094     MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
0095 
0096     if (non_blocking) {
0097         if (state_neither_active_nor_queued(q, vb))
0098             return 0;
0099         return -EAGAIN;
0100     }
0101 
0102     is_ext_locked = q->ext_lock && mutex_is_locked(q->ext_lock);
0103 
0104     /* Release vdev lock to prevent this wait from blocking outside access to
0105        the device. */
0106     if (is_ext_locked)
0107         mutex_unlock(q->ext_lock);
0108     if (intr)
0109         ret = wait_event_interruptible(vb->done,
0110                     state_neither_active_nor_queued(q, vb));
0111     else
0112         wait_event(vb->done, state_neither_active_nor_queued(q, vb));
0113     /* Relock */
0114     if (is_ext_locked)
0115         mutex_lock(q->ext_lock);
0116 
0117     return ret;
0118 }
0119 EXPORT_SYMBOL_GPL(videobuf_waiton);
0120 
0121 int videobuf_iolock(struct videobuf_queue *q, struct videobuf_buffer *vb,
0122             struct v4l2_framebuffer *fbuf)
0123 {
0124     MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
0125     MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
0126 
0127     return CALL(q, iolock, q, vb, fbuf);
0128 }
0129 EXPORT_SYMBOL_GPL(videobuf_iolock);
0130 
0131 void *videobuf_queue_to_vaddr(struct videobuf_queue *q,
0132                   struct videobuf_buffer *buf)
0133 {
0134     if (q->int_ops->vaddr)
0135         return q->int_ops->vaddr(buf);
0136     return NULL;
0137 }
0138 EXPORT_SYMBOL_GPL(videobuf_queue_to_vaddr);
0139 
0140 /* --------------------------------------------------------------------- */
0141 
0142 
0143 void videobuf_queue_core_init(struct videobuf_queue *q,
0144              const struct videobuf_queue_ops *ops,
0145              struct device *dev,
0146              spinlock_t *irqlock,
0147              enum v4l2_buf_type type,
0148              enum v4l2_field field,
0149              unsigned int msize,
0150              void *priv,
0151              struct videobuf_qtype_ops *int_ops,
0152              struct mutex *ext_lock)
0153 {
0154     BUG_ON(!q);
0155     memset(q, 0, sizeof(*q));
0156     q->irqlock   = irqlock;
0157     q->ext_lock  = ext_lock;
0158     q->dev       = dev;
0159     q->type      = type;
0160     q->field     = field;
0161     q->msize     = msize;
0162     q->ops       = ops;
0163     q->priv_data = priv;
0164     q->int_ops   = int_ops;
0165 
0166     /* All buffer operations are mandatory */
0167     BUG_ON(!q->ops->buf_setup);
0168     BUG_ON(!q->ops->buf_prepare);
0169     BUG_ON(!q->ops->buf_queue);
0170     BUG_ON(!q->ops->buf_release);
0171 
0172     /* Lock is mandatory for queue_cancel to work */
0173     BUG_ON(!irqlock);
0174 
0175     /* Having implementations for abstract methods are mandatory */
0176     BUG_ON(!q->int_ops);
0177 
0178     mutex_init(&q->vb_lock);
0179     init_waitqueue_head(&q->wait);
0180     INIT_LIST_HEAD(&q->stream);
0181 }
0182 EXPORT_SYMBOL_GPL(videobuf_queue_core_init);
0183 
0184 /* Locking: Only usage in bttv unsafe find way to remove */
0185 int videobuf_queue_is_busy(struct videobuf_queue *q)
0186 {
0187     int i;
0188 
0189     MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
0190 
0191     if (q->streaming) {
0192         dprintk(1, "busy: streaming active\n");
0193         return 1;
0194     }
0195     if (q->reading) {
0196         dprintk(1, "busy: pending read #1\n");
0197         return 1;
0198     }
0199     if (q->read_buf) {
0200         dprintk(1, "busy: pending read #2\n");
0201         return 1;
0202     }
0203     for (i = 0; i < VIDEO_MAX_FRAME; i++) {
0204         if (NULL == q->bufs[i])
0205             continue;
0206         if (q->bufs[i]->map) {
0207             dprintk(1, "busy: buffer #%d mapped\n", i);
0208             return 1;
0209         }
0210         if (q->bufs[i]->state == VIDEOBUF_QUEUED) {
0211             dprintk(1, "busy: buffer #%d queued\n", i);
0212             return 1;
0213         }
0214         if (q->bufs[i]->state == VIDEOBUF_ACTIVE) {
0215             dprintk(1, "busy: buffer #%d active\n", i);
0216             return 1;
0217         }
0218     }
0219     return 0;
0220 }
0221 EXPORT_SYMBOL_GPL(videobuf_queue_is_busy);
0222 
0223 /*
0224  * __videobuf_free() - free all the buffers and their control structures
0225  *
0226  * This function can only be called if streaming/reading is off, i.e. no buffers
0227  * are under control of the driver.
0228  */
0229 /* Locking: Caller holds q->vb_lock */
0230 static int __videobuf_free(struct videobuf_queue *q)
0231 {
0232     int i;
0233 
0234     dprintk(1, "%s\n", __func__);
0235     if (!q)
0236         return 0;
0237 
0238     if (q->streaming || q->reading) {
0239         dprintk(1, "Cannot free buffers when streaming or reading\n");
0240         return -EBUSY;
0241     }
0242 
0243     MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
0244 
0245     for (i = 0; i < VIDEO_MAX_FRAME; i++)
0246         if (q->bufs[i] && q->bufs[i]->map) {
0247             dprintk(1, "Cannot free mmapped buffers\n");
0248             return -EBUSY;
0249         }
0250 
0251     for (i = 0; i < VIDEO_MAX_FRAME; i++) {
0252         if (NULL == q->bufs[i])
0253             continue;
0254         q->ops->buf_release(q, q->bufs[i]);
0255         kfree(q->bufs[i]);
0256         q->bufs[i] = NULL;
0257     }
0258 
0259     return 0;
0260 }
0261 
0262 /* Locking: Caller holds q->vb_lock */
0263 void videobuf_queue_cancel(struct videobuf_queue *q)
0264 {
0265     unsigned long flags = 0;
0266     int i;
0267 
0268     q->streaming = 0;
0269     q->reading  = 0;
0270     wake_up_interruptible_sync(&q->wait);
0271 
0272     /* remove queued buffers from list */
0273     spin_lock_irqsave(q->irqlock, flags);
0274     for (i = 0; i < VIDEO_MAX_FRAME; i++) {
0275         if (NULL == q->bufs[i])
0276             continue;
0277         if (q->bufs[i]->state == VIDEOBUF_QUEUED) {
0278             list_del(&q->bufs[i]->queue);
0279             q->bufs[i]->state = VIDEOBUF_ERROR;
0280             wake_up_all(&q->bufs[i]->done);
0281         }
0282     }
0283     spin_unlock_irqrestore(q->irqlock, flags);
0284 
0285     /* free all buffers + clear queue */
0286     for (i = 0; i < VIDEO_MAX_FRAME; i++) {
0287         if (NULL == q->bufs[i])
0288             continue;
0289         q->ops->buf_release(q, q->bufs[i]);
0290     }
0291     INIT_LIST_HEAD(&q->stream);
0292 }
0293 EXPORT_SYMBOL_GPL(videobuf_queue_cancel);
0294 
0295 /* --------------------------------------------------------------------- */
0296 
0297 /* Locking: Caller holds q->vb_lock */
0298 enum v4l2_field videobuf_next_field(struct videobuf_queue *q)
0299 {
0300     enum v4l2_field field = q->field;
0301 
0302     BUG_ON(V4L2_FIELD_ANY == field);
0303 
0304     if (V4L2_FIELD_ALTERNATE == field) {
0305         if (V4L2_FIELD_TOP == q->last) {
0306             field   = V4L2_FIELD_BOTTOM;
0307             q->last = V4L2_FIELD_BOTTOM;
0308         } else {
0309             field   = V4L2_FIELD_TOP;
0310             q->last = V4L2_FIELD_TOP;
0311         }
0312     }
0313     return field;
0314 }
0315 EXPORT_SYMBOL_GPL(videobuf_next_field);
0316 
0317 /* Locking: Caller holds q->vb_lock */
0318 static void videobuf_status(struct videobuf_queue *q, struct v4l2_buffer *b,
0319                 struct videobuf_buffer *vb, enum v4l2_buf_type type)
0320 {
0321     MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
0322     MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
0323 
0324     b->index    = vb->i;
0325     b->type     = type;
0326 
0327     b->memory   = vb->memory;
0328     switch (b->memory) {
0329     case V4L2_MEMORY_MMAP:
0330         b->m.offset  = vb->boff;
0331         b->length    = vb->bsize;
0332         break;
0333     case V4L2_MEMORY_USERPTR:
0334         b->m.userptr = vb->baddr;
0335         b->length    = vb->bsize;
0336         break;
0337     case V4L2_MEMORY_OVERLAY:
0338         b->m.offset  = vb->boff;
0339         break;
0340     case V4L2_MEMORY_DMABUF:
0341         /* DMABUF is not handled in videobuf framework */
0342         break;
0343     }
0344 
0345     b->flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
0346     if (vb->map)
0347         b->flags |= V4L2_BUF_FLAG_MAPPED;
0348 
0349     switch (vb->state) {
0350     case VIDEOBUF_PREPARED:
0351     case VIDEOBUF_QUEUED:
0352     case VIDEOBUF_ACTIVE:
0353         b->flags |= V4L2_BUF_FLAG_QUEUED;
0354         break;
0355     case VIDEOBUF_ERROR:
0356         b->flags |= V4L2_BUF_FLAG_ERROR;
0357         fallthrough;
0358     case VIDEOBUF_DONE:
0359         b->flags |= V4L2_BUF_FLAG_DONE;
0360         break;
0361     case VIDEOBUF_NEEDS_INIT:
0362     case VIDEOBUF_IDLE:
0363         /* nothing */
0364         break;
0365     }
0366 
0367     b->field     = vb->field;
0368     v4l2_buffer_set_timestamp(b, vb->ts);
0369     b->bytesused = vb->size;
0370     b->sequence  = vb->field_count >> 1;
0371 }
0372 
0373 int videobuf_mmap_free(struct videobuf_queue *q)
0374 {
0375     int ret;
0376     videobuf_queue_lock(q);
0377     ret = __videobuf_free(q);
0378     videobuf_queue_unlock(q);
0379     return ret;
0380 }
0381 EXPORT_SYMBOL_GPL(videobuf_mmap_free);
0382 
0383 /* Locking: Caller holds q->vb_lock */
0384 int __videobuf_mmap_setup(struct videobuf_queue *q,
0385             unsigned int bcount, unsigned int bsize,
0386             enum v4l2_memory memory)
0387 {
0388     unsigned int i;
0389     int err;
0390 
0391     MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
0392 
0393     err = __videobuf_free(q);
0394     if (0 != err)
0395         return err;
0396 
0397     /* Allocate and initialize buffers */
0398     for (i = 0; i < bcount; i++) {
0399         q->bufs[i] = videobuf_alloc_vb(q);
0400 
0401         if (NULL == q->bufs[i])
0402             break;
0403 
0404         q->bufs[i]->i      = i;
0405         q->bufs[i]->memory = memory;
0406         q->bufs[i]->bsize  = bsize;
0407         switch (memory) {
0408         case V4L2_MEMORY_MMAP:
0409             q->bufs[i]->boff = PAGE_ALIGN(bsize) * i;
0410             break;
0411         case V4L2_MEMORY_USERPTR:
0412         case V4L2_MEMORY_OVERLAY:
0413         case V4L2_MEMORY_DMABUF:
0414             /* nothing */
0415             break;
0416         }
0417     }
0418 
0419     if (!i)
0420         return -ENOMEM;
0421 
0422     dprintk(1, "mmap setup: %d buffers, %d bytes each\n", i, bsize);
0423 
0424     return i;
0425 }
0426 EXPORT_SYMBOL_GPL(__videobuf_mmap_setup);
0427 
0428 int videobuf_mmap_setup(struct videobuf_queue *q,
0429             unsigned int bcount, unsigned int bsize,
0430             enum v4l2_memory memory)
0431 {
0432     int ret;
0433     videobuf_queue_lock(q);
0434     ret = __videobuf_mmap_setup(q, bcount, bsize, memory);
0435     videobuf_queue_unlock(q);
0436     return ret;
0437 }
0438 EXPORT_SYMBOL_GPL(videobuf_mmap_setup);
0439 
0440 int videobuf_reqbufs(struct videobuf_queue *q,
0441          struct v4l2_requestbuffers *req)
0442 {
0443     unsigned int size, count;
0444     int retval;
0445 
0446     if (req->memory != V4L2_MEMORY_MMAP     &&
0447         req->memory != V4L2_MEMORY_USERPTR  &&
0448         req->memory != V4L2_MEMORY_OVERLAY) {
0449         dprintk(1, "reqbufs: memory type invalid\n");
0450         return -EINVAL;
0451     }
0452 
0453     videobuf_queue_lock(q);
0454     if (req->type != q->type) {
0455         dprintk(1, "reqbufs: queue type invalid\n");
0456         retval = -EINVAL;
0457         goto done;
0458     }
0459 
0460     if (q->streaming) {
0461         dprintk(1, "reqbufs: streaming already exists\n");
0462         retval = -EBUSY;
0463         goto done;
0464     }
0465     if (!list_empty(&q->stream)) {
0466         dprintk(1, "reqbufs: stream running\n");
0467         retval = -EBUSY;
0468         goto done;
0469     }
0470 
0471     if (req->count == 0) {
0472         dprintk(1, "reqbufs: count invalid (%d)\n", req->count);
0473         retval = __videobuf_free(q);
0474         goto done;
0475     }
0476 
0477     count = req->count;
0478     if (count > VIDEO_MAX_FRAME)
0479         count = VIDEO_MAX_FRAME;
0480     size = 0;
0481     q->ops->buf_setup(q, &count, &size);
0482     dprintk(1, "reqbufs: bufs=%d, size=0x%x [%u pages total]\n",
0483         count, size,
0484         (unsigned int)((count * PAGE_ALIGN(size)) >> PAGE_SHIFT));
0485 
0486     retval = __videobuf_mmap_setup(q, count, size, req->memory);
0487     if (retval < 0) {
0488         dprintk(1, "reqbufs: mmap setup returned %d\n", retval);
0489         goto done;
0490     }
0491 
0492     req->count = retval;
0493     retval = 0;
0494 
0495  done:
0496     videobuf_queue_unlock(q);
0497     return retval;
0498 }
0499 EXPORT_SYMBOL_GPL(videobuf_reqbufs);
0500 
0501 int videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b)
0502 {
0503     int ret = -EINVAL;
0504 
0505     videobuf_queue_lock(q);
0506     if (unlikely(b->type != q->type)) {
0507         dprintk(1, "querybuf: Wrong type.\n");
0508         goto done;
0509     }
0510     if (unlikely(b->index >= VIDEO_MAX_FRAME)) {
0511         dprintk(1, "querybuf: index out of range.\n");
0512         goto done;
0513     }
0514     if (unlikely(NULL == q->bufs[b->index])) {
0515         dprintk(1, "querybuf: buffer is null.\n");
0516         goto done;
0517     }
0518 
0519     videobuf_status(q, b, q->bufs[b->index], q->type);
0520 
0521     ret = 0;
0522 done:
0523     videobuf_queue_unlock(q);
0524     return ret;
0525 }
0526 EXPORT_SYMBOL_GPL(videobuf_querybuf);
0527 
0528 int videobuf_qbuf(struct videobuf_queue *q, struct v4l2_buffer *b)
0529 {
0530     struct videobuf_buffer *buf;
0531     enum v4l2_field field;
0532     unsigned long flags = 0;
0533     int retval;
0534 
0535     MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
0536 
0537     if (b->memory == V4L2_MEMORY_MMAP)
0538         mmap_read_lock(current->mm);
0539 
0540     videobuf_queue_lock(q);
0541     retval = -EBUSY;
0542     if (q->reading) {
0543         dprintk(1, "qbuf: Reading running...\n");
0544         goto done;
0545     }
0546     retval = -EINVAL;
0547     if (b->type != q->type) {
0548         dprintk(1, "qbuf: Wrong type.\n");
0549         goto done;
0550     }
0551     if (b->index >= VIDEO_MAX_FRAME) {
0552         dprintk(1, "qbuf: index out of range.\n");
0553         goto done;
0554     }
0555     buf = q->bufs[b->index];
0556     if (NULL == buf) {
0557         dprintk(1, "qbuf: buffer is null.\n");
0558         goto done;
0559     }
0560     MAGIC_CHECK(buf->magic, MAGIC_BUFFER);
0561     if (buf->memory != b->memory) {
0562         dprintk(1, "qbuf: memory type is wrong.\n");
0563         goto done;
0564     }
0565     if (buf->state != VIDEOBUF_NEEDS_INIT && buf->state != VIDEOBUF_IDLE) {
0566         dprintk(1, "qbuf: buffer is already queued or active.\n");
0567         goto done;
0568     }
0569 
0570     switch (b->memory) {
0571     case V4L2_MEMORY_MMAP:
0572         if (0 == buf->baddr) {
0573             dprintk(1, "qbuf: mmap requested but buffer addr is zero!\n");
0574             goto done;
0575         }
0576         if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT
0577             || q->type == V4L2_BUF_TYPE_VBI_OUTPUT
0578             || q->type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT
0579             || q->type == V4L2_BUF_TYPE_SDR_OUTPUT) {
0580             buf->size = b->bytesused;
0581             buf->field = b->field;
0582             buf->ts = v4l2_buffer_get_timestamp(b);
0583         }
0584         break;
0585     case V4L2_MEMORY_USERPTR:
0586         if (b->length < buf->bsize) {
0587             dprintk(1, "qbuf: buffer length is not enough\n");
0588             goto done;
0589         }
0590         if (VIDEOBUF_NEEDS_INIT != buf->state &&
0591             buf->baddr != b->m.userptr)
0592             q->ops->buf_release(q, buf);
0593         buf->baddr = b->m.userptr;
0594         break;
0595     case V4L2_MEMORY_OVERLAY:
0596         buf->boff = b->m.offset;
0597         break;
0598     default:
0599         dprintk(1, "qbuf: wrong memory type\n");
0600         goto done;
0601     }
0602 
0603     dprintk(1, "qbuf: requesting next field\n");
0604     field = videobuf_next_field(q);
0605     retval = q->ops->buf_prepare(q, buf, field);
0606     if (0 != retval) {
0607         dprintk(1, "qbuf: buffer_prepare returned %d\n", retval);
0608         goto done;
0609     }
0610 
0611     list_add_tail(&buf->stream, &q->stream);
0612     if (q->streaming) {
0613         spin_lock_irqsave(q->irqlock, flags);
0614         q->ops->buf_queue(q, buf);
0615         spin_unlock_irqrestore(q->irqlock, flags);
0616     }
0617     dprintk(1, "qbuf: succeeded\n");
0618     retval = 0;
0619     wake_up_interruptible_sync(&q->wait);
0620 
0621 done:
0622     videobuf_queue_unlock(q);
0623 
0624     if (b->memory == V4L2_MEMORY_MMAP)
0625         mmap_read_unlock(current->mm);
0626 
0627     return retval;
0628 }
0629 EXPORT_SYMBOL_GPL(videobuf_qbuf);
0630 
0631 /* Locking: Caller holds q->vb_lock */
0632 static int stream_next_buffer_check_queue(struct videobuf_queue *q, int noblock)
0633 {
0634     int retval;
0635 
0636 checks:
0637     if (!q->streaming) {
0638         dprintk(1, "next_buffer: Not streaming\n");
0639         retval = -EINVAL;
0640         goto done;
0641     }
0642 
0643     if (list_empty(&q->stream)) {
0644         if (noblock) {
0645             retval = -EAGAIN;
0646             dprintk(2, "next_buffer: no buffers to dequeue\n");
0647             goto done;
0648         } else {
0649             dprintk(2, "next_buffer: waiting on buffer\n");
0650 
0651             /* Drop lock to avoid deadlock with qbuf */
0652             videobuf_queue_unlock(q);
0653 
0654             /* Checking list_empty and streaming is safe without
0655              * locks because we goto checks to validate while
0656              * holding locks before proceeding */
0657             retval = wait_event_interruptible(q->wait,
0658                 !list_empty(&q->stream) || !q->streaming);
0659             videobuf_queue_lock(q);
0660 
0661             if (retval)
0662                 goto done;
0663 
0664             goto checks;
0665         }
0666     }
0667 
0668     retval = 0;
0669 
0670 done:
0671     return retval;
0672 }
0673 
0674 /* Locking: Caller holds q->vb_lock */
0675 static int stream_next_buffer(struct videobuf_queue *q,
0676             struct videobuf_buffer **vb, int nonblocking)
0677 {
0678     int retval;
0679     struct videobuf_buffer *buf = NULL;
0680 
0681     retval = stream_next_buffer_check_queue(q, nonblocking);
0682     if (retval)
0683         goto done;
0684 
0685     buf = list_entry(q->stream.next, struct videobuf_buffer, stream);
0686     retval = videobuf_waiton(q, buf, nonblocking, 1);
0687     if (retval < 0)
0688         goto done;
0689 
0690     *vb = buf;
0691 done:
0692     return retval;
0693 }
0694 
0695 int videobuf_dqbuf(struct videobuf_queue *q,
0696            struct v4l2_buffer *b, int nonblocking)
0697 {
0698     struct videobuf_buffer *buf = NULL;
0699     int retval;
0700 
0701     MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
0702 
0703     memset(b, 0, sizeof(*b));
0704     videobuf_queue_lock(q);
0705 
0706     retval = stream_next_buffer(q, &buf, nonblocking);
0707     if (retval < 0) {
0708         dprintk(1, "dqbuf: next_buffer error: %i\n", retval);
0709         goto done;
0710     }
0711 
0712     switch (buf->state) {
0713     case VIDEOBUF_ERROR:
0714         dprintk(1, "dqbuf: state is error\n");
0715         break;
0716     case VIDEOBUF_DONE:
0717         dprintk(1, "dqbuf: state is done\n");
0718         break;
0719     default:
0720         dprintk(1, "dqbuf: state invalid\n");
0721         retval = -EINVAL;
0722         goto done;
0723     }
0724     CALL(q, sync, q, buf);
0725     videobuf_status(q, b, buf, q->type);
0726     list_del(&buf->stream);
0727     buf->state = VIDEOBUF_IDLE;
0728     b->flags &= ~V4L2_BUF_FLAG_DONE;
0729 done:
0730     videobuf_queue_unlock(q);
0731     return retval;
0732 }
0733 EXPORT_SYMBOL_GPL(videobuf_dqbuf);
0734 
0735 int videobuf_streamon(struct videobuf_queue *q)
0736 {
0737     struct videobuf_buffer *buf;
0738     unsigned long flags = 0;
0739     int retval;
0740 
0741     videobuf_queue_lock(q);
0742     retval = -EBUSY;
0743     if (q->reading)
0744         goto done;
0745     retval = 0;
0746     if (q->streaming)
0747         goto done;
0748     q->streaming = 1;
0749     spin_lock_irqsave(q->irqlock, flags);
0750     list_for_each_entry(buf, &q->stream, stream)
0751         if (buf->state == VIDEOBUF_PREPARED)
0752             q->ops->buf_queue(q, buf);
0753     spin_unlock_irqrestore(q->irqlock, flags);
0754 
0755     wake_up_interruptible_sync(&q->wait);
0756 done:
0757     videobuf_queue_unlock(q);
0758     return retval;
0759 }
0760 EXPORT_SYMBOL_GPL(videobuf_streamon);
0761 
0762 /* Locking: Caller holds q->vb_lock */
0763 static int __videobuf_streamoff(struct videobuf_queue *q)
0764 {
0765     if (!q->streaming)
0766         return -EINVAL;
0767 
0768     videobuf_queue_cancel(q);
0769 
0770     return 0;
0771 }
0772 
0773 int videobuf_streamoff(struct videobuf_queue *q)
0774 {
0775     int retval;
0776 
0777     videobuf_queue_lock(q);
0778     retval = __videobuf_streamoff(q);
0779     videobuf_queue_unlock(q);
0780 
0781     return retval;
0782 }
0783 EXPORT_SYMBOL_GPL(videobuf_streamoff);
0784 
0785 /* Locking: Caller holds q->vb_lock */
0786 static ssize_t videobuf_read_zerocopy(struct videobuf_queue *q,
0787                       char __user *data,
0788                       size_t count, loff_t *ppos)
0789 {
0790     enum v4l2_field field;
0791     unsigned long flags = 0;
0792     int retval;
0793 
0794     MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
0795 
0796     /* setup stuff */
0797     q->read_buf = videobuf_alloc_vb(q);
0798     if (NULL == q->read_buf)
0799         return -ENOMEM;
0800 
0801     q->read_buf->memory = V4L2_MEMORY_USERPTR;
0802     q->read_buf->baddr  = (unsigned long)data;
0803     q->read_buf->bsize  = count;
0804 
0805     field = videobuf_next_field(q);
0806     retval = q->ops->buf_prepare(q, q->read_buf, field);
0807     if (0 != retval)
0808         goto done;
0809 
0810     /* start capture & wait */
0811     spin_lock_irqsave(q->irqlock, flags);
0812     q->ops->buf_queue(q, q->read_buf);
0813     spin_unlock_irqrestore(q->irqlock, flags);
0814     retval = videobuf_waiton(q, q->read_buf, 0, 0);
0815     if (0 == retval) {
0816         CALL(q, sync, q, q->read_buf);
0817         if (VIDEOBUF_ERROR == q->read_buf->state)
0818             retval = -EIO;
0819         else
0820             retval = q->read_buf->size;
0821     }
0822 
0823 done:
0824     /* cleanup */
0825     q->ops->buf_release(q, q->read_buf);
0826     kfree(q->read_buf);
0827     q->read_buf = NULL;
0828     return retval;
0829 }
0830 
0831 static int __videobuf_copy_to_user(struct videobuf_queue *q,
0832                    struct videobuf_buffer *buf,
0833                    char __user *data, size_t count,
0834                    int nonblocking)
0835 {
0836     void *vaddr = CALLPTR(q, vaddr, buf);
0837 
0838     /* copy to userspace */
0839     if (count > buf->size - q->read_off)
0840         count = buf->size - q->read_off;
0841 
0842     if (copy_to_user(data, vaddr + q->read_off, count))
0843         return -EFAULT;
0844 
0845     return count;
0846 }
0847 
0848 static int __videobuf_copy_stream(struct videobuf_queue *q,
0849                   struct videobuf_buffer *buf,
0850                   char __user *data, size_t count, size_t pos,
0851                   int vbihack, int nonblocking)
0852 {
0853     unsigned int *fc = CALLPTR(q, vaddr, buf);
0854 
0855     if (vbihack) {
0856         /* dirty, undocumented hack -- pass the frame counter
0857             * within the last four bytes of each vbi data block.
0858             * We need that one to maintain backward compatibility
0859             * to all vbi decoding software out there ... */
0860         fc += (buf->size >> 2) - 1;
0861         *fc = buf->field_count >> 1;
0862         dprintk(1, "vbihack: %d\n", *fc);
0863     }
0864 
0865     /* copy stuff using the common method */
0866     count = __videobuf_copy_to_user(q, buf, data, count, nonblocking);
0867 
0868     if ((count == -EFAULT) && (pos == 0))
0869         return -EFAULT;
0870 
0871     return count;
0872 }
0873 
0874 ssize_t videobuf_read_one(struct videobuf_queue *q,
0875               char __user *data, size_t count, loff_t *ppos,
0876               int nonblocking)
0877 {
0878     enum v4l2_field field;
0879     unsigned long flags = 0;
0880     unsigned size = 0, nbufs = 1;
0881     int retval;
0882 
0883     MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
0884 
0885     videobuf_queue_lock(q);
0886 
0887     q->ops->buf_setup(q, &nbufs, &size);
0888 
0889     if (NULL == q->read_buf  &&
0890         count >= size        &&
0891         !nonblocking) {
0892         retval = videobuf_read_zerocopy(q, data, count, ppos);
0893         if (retval >= 0  ||  retval == -EIO)
0894             /* ok, all done */
0895             goto done;
0896         /* fallback to kernel bounce buffer on failures */
0897     }
0898 
0899     if (NULL == q->read_buf) {
0900         /* need to capture a new frame */
0901         retval = -ENOMEM;
0902         q->read_buf = videobuf_alloc_vb(q);
0903 
0904         dprintk(1, "video alloc=0x%p\n", q->read_buf);
0905         if (NULL == q->read_buf)
0906             goto done;
0907         q->read_buf->memory = V4L2_MEMORY_USERPTR;
0908         q->read_buf->bsize = count; /* preferred size */
0909         field = videobuf_next_field(q);
0910         retval = q->ops->buf_prepare(q, q->read_buf, field);
0911 
0912         if (0 != retval) {
0913             kfree(q->read_buf);
0914             q->read_buf = NULL;
0915             goto done;
0916         }
0917 
0918         spin_lock_irqsave(q->irqlock, flags);
0919         q->ops->buf_queue(q, q->read_buf);
0920         spin_unlock_irqrestore(q->irqlock, flags);
0921 
0922         q->read_off = 0;
0923     }
0924 
0925     /* wait until capture is done */
0926     retval = videobuf_waiton(q, q->read_buf, nonblocking, 1);
0927     if (0 != retval)
0928         goto done;
0929 
0930     CALL(q, sync, q, q->read_buf);
0931 
0932     if (VIDEOBUF_ERROR == q->read_buf->state) {
0933         /* catch I/O errors */
0934         q->ops->buf_release(q, q->read_buf);
0935         kfree(q->read_buf);
0936         q->read_buf = NULL;
0937         retval = -EIO;
0938         goto done;
0939     }
0940 
0941     /* Copy to userspace */
0942     retval = __videobuf_copy_to_user(q, q->read_buf, data, count, nonblocking);
0943     if (retval < 0)
0944         goto done;
0945 
0946     q->read_off += retval;
0947     if (q->read_off == q->read_buf->size) {
0948         /* all data copied, cleanup */
0949         q->ops->buf_release(q, q->read_buf);
0950         kfree(q->read_buf);
0951         q->read_buf = NULL;
0952     }
0953 
0954 done:
0955     videobuf_queue_unlock(q);
0956     return retval;
0957 }
0958 EXPORT_SYMBOL_GPL(videobuf_read_one);
0959 
0960 /* Locking: Caller holds q->vb_lock */
0961 static int __videobuf_read_start(struct videobuf_queue *q)
0962 {
0963     enum v4l2_field field;
0964     unsigned long flags = 0;
0965     unsigned int count = 0, size = 0;
0966     int err, i;
0967 
0968     q->ops->buf_setup(q, &count, &size);
0969     if (count < 2)
0970         count = 2;
0971     if (count > VIDEO_MAX_FRAME)
0972         count = VIDEO_MAX_FRAME;
0973     size = PAGE_ALIGN(size);
0974 
0975     err = __videobuf_mmap_setup(q, count, size, V4L2_MEMORY_USERPTR);
0976     if (err < 0)
0977         return err;
0978 
0979     count = err;
0980 
0981     for (i = 0; i < count; i++) {
0982         field = videobuf_next_field(q);
0983         err = q->ops->buf_prepare(q, q->bufs[i], field);
0984         if (err)
0985             return err;
0986         list_add_tail(&q->bufs[i]->stream, &q->stream);
0987     }
0988     spin_lock_irqsave(q->irqlock, flags);
0989     for (i = 0; i < count; i++)
0990         q->ops->buf_queue(q, q->bufs[i]);
0991     spin_unlock_irqrestore(q->irqlock, flags);
0992     q->reading = 1;
0993     return 0;
0994 }
0995 
0996 static void __videobuf_read_stop(struct videobuf_queue *q)
0997 {
0998     int i;
0999 
1000     videobuf_queue_cancel(q);
1001     __videobuf_free(q);
1002     INIT_LIST_HEAD(&q->stream);
1003     for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1004         if (NULL == q->bufs[i])
1005             continue;
1006         kfree(q->bufs[i]);
1007         q->bufs[i] = NULL;
1008     }
1009     q->read_buf = NULL;
1010 }
1011 
1012 int videobuf_read_start(struct videobuf_queue *q)
1013 {
1014     int rc;
1015 
1016     videobuf_queue_lock(q);
1017     rc = __videobuf_read_start(q);
1018     videobuf_queue_unlock(q);
1019 
1020     return rc;
1021 }
1022 EXPORT_SYMBOL_GPL(videobuf_read_start);
1023 
1024 void videobuf_read_stop(struct videobuf_queue *q)
1025 {
1026     videobuf_queue_lock(q);
1027     __videobuf_read_stop(q);
1028     videobuf_queue_unlock(q);
1029 }
1030 EXPORT_SYMBOL_GPL(videobuf_read_stop);
1031 
1032 void videobuf_stop(struct videobuf_queue *q)
1033 {
1034     videobuf_queue_lock(q);
1035 
1036     if (q->streaming)
1037         __videobuf_streamoff(q);
1038 
1039     if (q->reading)
1040         __videobuf_read_stop(q);
1041 
1042     videobuf_queue_unlock(q);
1043 }
1044 EXPORT_SYMBOL_GPL(videobuf_stop);
1045 
1046 ssize_t videobuf_read_stream(struct videobuf_queue *q,
1047                  char __user *data, size_t count, loff_t *ppos,
1048                  int vbihack, int nonblocking)
1049 {
1050     int rc, retval;
1051     unsigned long flags = 0;
1052 
1053     MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
1054 
1055     dprintk(2, "%s\n", __func__);
1056     videobuf_queue_lock(q);
1057     retval = -EBUSY;
1058     if (q->streaming)
1059         goto done;
1060     if (!q->reading) {
1061         retval = __videobuf_read_start(q);
1062         if (retval < 0)
1063             goto done;
1064     }
1065 
1066     retval = 0;
1067     while (count > 0) {
1068         /* get / wait for data */
1069         if (NULL == q->read_buf) {
1070             q->read_buf = list_entry(q->stream.next,
1071                          struct videobuf_buffer,
1072                          stream);
1073             list_del(&q->read_buf->stream);
1074             q->read_off = 0;
1075         }
1076         rc = videobuf_waiton(q, q->read_buf, nonblocking, 1);
1077         if (rc < 0) {
1078             if (0 == retval)
1079                 retval = rc;
1080             break;
1081         }
1082 
1083         if (q->read_buf->state == VIDEOBUF_DONE) {
1084             rc = __videobuf_copy_stream(q, q->read_buf, data + retval, count,
1085                     retval, vbihack, nonblocking);
1086             if (rc < 0) {
1087                 retval = rc;
1088                 break;
1089             }
1090             retval      += rc;
1091             count       -= rc;
1092             q->read_off += rc;
1093         } else {
1094             /* some error */
1095             q->read_off = q->read_buf->size;
1096             if (0 == retval)
1097                 retval = -EIO;
1098         }
1099 
1100         /* requeue buffer when done with copying */
1101         if (q->read_off == q->read_buf->size) {
1102             list_add_tail(&q->read_buf->stream,
1103                       &q->stream);
1104             spin_lock_irqsave(q->irqlock, flags);
1105             q->ops->buf_queue(q, q->read_buf);
1106             spin_unlock_irqrestore(q->irqlock, flags);
1107             q->read_buf = NULL;
1108         }
1109         if (retval < 0)
1110             break;
1111     }
1112 
1113 done:
1114     videobuf_queue_unlock(q);
1115     return retval;
1116 }
1117 EXPORT_SYMBOL_GPL(videobuf_read_stream);
1118 
1119 __poll_t videobuf_poll_stream(struct file *file,
1120                   struct videobuf_queue *q,
1121                   poll_table *wait)
1122 {
1123     __poll_t req_events = poll_requested_events(wait);
1124     struct videobuf_buffer *buf = NULL;
1125     __poll_t rc = 0;
1126 
1127     videobuf_queue_lock(q);
1128     if (q->streaming) {
1129         if (!list_empty(&q->stream))
1130             buf = list_entry(q->stream.next,
1131                      struct videobuf_buffer, stream);
1132     } else if (req_events & (EPOLLIN | EPOLLRDNORM)) {
1133         if (!q->reading)
1134             __videobuf_read_start(q);
1135         if (!q->reading) {
1136             rc = EPOLLERR;
1137         } else if (NULL == q->read_buf) {
1138             q->read_buf = list_entry(q->stream.next,
1139                          struct videobuf_buffer,
1140                          stream);
1141             list_del(&q->read_buf->stream);
1142             q->read_off = 0;
1143         }
1144         buf = q->read_buf;
1145     }
1146     if (buf)
1147         poll_wait(file, &buf->done, wait);
1148     else
1149         rc = EPOLLERR;
1150 
1151     if (0 == rc) {
1152         if (buf->state == VIDEOBUF_DONE ||
1153             buf->state == VIDEOBUF_ERROR) {
1154             switch (q->type) {
1155             case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1156             case V4L2_BUF_TYPE_VBI_OUTPUT:
1157             case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1158             case V4L2_BUF_TYPE_SDR_OUTPUT:
1159                 rc = EPOLLOUT | EPOLLWRNORM;
1160                 break;
1161             default:
1162                 rc = EPOLLIN | EPOLLRDNORM;
1163                 break;
1164             }
1165         }
1166     }
1167     videobuf_queue_unlock(q);
1168     return rc;
1169 }
1170 EXPORT_SYMBOL_GPL(videobuf_poll_stream);
1171 
1172 int videobuf_mmap_mapper(struct videobuf_queue *q, struct vm_area_struct *vma)
1173 {
1174     int rc = -EINVAL;
1175     int i;
1176 
1177     MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
1178 
1179     if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED)) {
1180         dprintk(1, "mmap appl bug: PROT_WRITE and MAP_SHARED are required\n");
1181         return -EINVAL;
1182     }
1183 
1184     videobuf_queue_lock(q);
1185     for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1186         struct videobuf_buffer *buf = q->bufs[i];
1187 
1188         if (buf && buf->memory == V4L2_MEMORY_MMAP &&
1189                 buf->boff == (vma->vm_pgoff << PAGE_SHIFT)) {
1190             rc = CALL(q, mmap_mapper, q, buf, vma);
1191             break;
1192         }
1193     }
1194     videobuf_queue_unlock(q);
1195 
1196     return rc;
1197 }
1198 EXPORT_SYMBOL_GPL(videobuf_mmap_mapper);