Directly use vb2_buffer pointer instead of index inside queue array.
Signed-off-by: Benjamin Gaignard <benjamin.gaignard@collabora.com>
Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
Signed-off-by: Mauro Carvalho Chehab <mchehab@kernel.org>
        return false;
 }
 
-void vb2_core_querybuf(struct vb2_queue *q, unsigned int index, void *pb)
+void vb2_core_querybuf(struct vb2_queue *q, struct vb2_buffer *vb, void *pb)
 {
-       call_void_bufop(q, fill_user_buffer, q->bufs[index], pb);
+       call_void_bufop(q, fill_user_buffer, vb, pb);
 }
 EXPORT_SYMBOL_GPL(vb2_core_querybuf);
 
        WARN_ON(!vb->req_obj.req);
 }
 
-int vb2_core_qbuf(struct vb2_queue *q, unsigned int index, void *pb,
-                 struct media_request *req);
-
 static void vb2_req_queue(struct media_request_object *obj)
 {
        struct vb2_buffer *vb = container_of(obj, struct vb2_buffer, req_obj);
         * set. We just ignore that, and expect this will be caught the
         * next time vb2_req_prepare() is called.
         */
-       err = vb2_core_qbuf(vb->vb2_queue, vb->index, NULL, NULL);
+       err = vb2_core_qbuf(vb->vb2_queue, vb, NULL, NULL);
        WARN_ON_ONCE(err && err != -EIO);
        mutex_unlock(vb->vb2_queue->lock);
 }
 }
 EXPORT_SYMBOL_GPL(vb2_request_buffer_cnt);
 
-int vb2_core_prepare_buf(struct vb2_queue *q, unsigned int index, void *pb)
+int vb2_core_prepare_buf(struct vb2_queue *q, struct vb2_buffer *vb, void *pb)
 {
-       struct vb2_buffer *vb;
        int ret;
 
-       vb = q->bufs[index];
        if (vb->state != VB2_BUF_STATE_DEQUEUED) {
                dprintk(q, 1, "invalid buffer state %s\n",
                        vb2_state_name(vb->state));
        return ret;
 }
 
-int vb2_core_qbuf(struct vb2_queue *q, unsigned int index, void *pb,
+int vb2_core_qbuf(struct vb2_queue *q, struct vb2_buffer *vb, void *pb,
                  struct media_request *req)
 {
-       struct vb2_buffer *vb;
        enum vb2_buffer_state orig_state;
        int ret;
 
                return -EIO;
        }
 
-       vb = q->bufs[index];
-
        if (!req && vb->state != VB2_BUF_STATE_IN_REQUEST &&
            q->requires_requests) {
                dprintk(q, 1, "qbuf requires a request\n");
 }
 
 int vb2_core_expbuf(struct vb2_queue *q, int *fd, unsigned int type,
-               unsigned int index, unsigned int plane, unsigned int flags)
+                   struct vb2_buffer *vb, unsigned int plane, unsigned int flags)
 {
-       struct vb2_buffer *vb = NULL;
        struct vb2_plane *vb_plane;
        int ret;
        struct dma_buf *dbuf;
                return -EINVAL;
        }
 
-       if (index >= q->num_buffers) {
-               dprintk(q, 1, "buffer index out of range\n");
-               return -EINVAL;
-       }
-
-       vb = q->bufs[index];
-
        if (plane >= vb->num_planes) {
                dprintk(q, 1, "buffer plane out of range\n");
                return -EINVAL;
                              flags & O_ACCMODE);
        if (IS_ERR_OR_NULL(dbuf)) {
                dprintk(q, 1, "failed to export buffer %d, plane %d\n",
-                       index, plane);
+                       vb->index, plane);
                return -EINVAL;
        }
 
        ret = dma_buf_fd(dbuf, flags & ~O_ACCMODE);
        if (ret < 0) {
                dprintk(q, 3, "buffer %d, plane %d failed to export (%d)\n",
-                       index, plane, ret);
+                       vb->index, plane, ret);
                dma_buf_put(dbuf);
                return ret;
        }
 
        dprintk(q, 3, "buffer %d, plane %d exported as %d descriptor\n",
-               index, plane, ret);
+               vb->index, plane, ret);
        *fd = ret;
 
        return 0;
                 * Queue all buffers.
                 */
                for (i = 0; i < q->num_buffers; i++) {
-                       ret = vb2_core_qbuf(q, i, NULL, NULL);
+                       ret = vb2_core_qbuf(q, q->bufs[i], NULL, NULL);
                        if (ret)
                                goto err_reqbufs;
                        fileio->bufs[i].queued = 1;
 
                if (copy_timestamp)
                        b->timestamp = ktime_get_ns();
-               ret = vb2_core_qbuf(q, index, NULL, NULL);
+               ret = vb2_core_qbuf(q, b, NULL, NULL);
                dprintk(q, 5, "vb2_qbuf result: %d\n", ret);
                if (ret)
                        return ret;
                if (copy_timestamp)
                        vb->timestamp = ktime_get_ns();
                if (!threadio->stop)
-                       ret = vb2_core_qbuf(q, vb->index, NULL, NULL);
+                       ret = vb2_core_qbuf(q, vb, NULL, NULL);
                call_void_qop(q, wait_prepare, q);
                if (ret || threadio->stop)
                        break;
 
 }
 
 static int vb2_queue_or_prepare_buf(struct vb2_queue *q, struct media_device *mdev,
-                                   struct v4l2_buffer *b, bool is_prepare,
-                                   struct media_request **p_req)
+                                   struct vb2_buffer *vb, struct v4l2_buffer *b,
+                                   bool is_prepare, struct media_request **p_req)
 {
        const char *opname = is_prepare ? "prepare_buf" : "qbuf";
        struct media_request *req;
        struct vb2_v4l2_buffer *vbuf;
-       struct vb2_buffer *vb;
        int ret;
 
        if (b->type != q->type) {
                return -EINVAL;
        }
 
-       if (b->index >= q->num_buffers) {
-               dprintk(q, 1, "%s: buffer index out of range\n", opname);
-               return -EINVAL;
-       }
-
-       if (q->bufs[b->index] == NULL) {
-               /* Should never happen */
-               dprintk(q, 1, "%s: buffer is NULL\n", opname);
-               return -EINVAL;
-       }
-
        if (b->memory != q->memory) {
                dprintk(q, 1, "%s: invalid memory type\n", opname);
                return -EINVAL;
        }
 
-       vb = q->bufs[b->index];
        vbuf = to_vb2_v4l2_buffer(vb);
        ret = __verify_planes_array(vb, b);
        if (ret)
        vb = q->bufs[b->index];
        ret = __verify_planes_array(vb, b);
        if (!ret)
-               vb2_core_querybuf(q, b->index, b);
+               vb2_core_querybuf(q, vb, b);
        return ret;
 }
 EXPORT_SYMBOL(vb2_querybuf);
 int vb2_prepare_buf(struct vb2_queue *q, struct media_device *mdev,
                    struct v4l2_buffer *b)
 {
+       struct vb2_buffer *vb;
        int ret;
 
        if (vb2_fileio_is_active(q)) {
        if (b->flags & V4L2_BUF_FLAG_REQUEST_FD)
                return -EINVAL;
 
-       ret = vb2_queue_or_prepare_buf(q, mdev, b, true, NULL);
+       if (b->index >= q->num_buffers) {
+               dprintk(q, 1, "buffer index out of range\n");
+               return -EINVAL;
+       }
+       vb = q->bufs[b->index];
 
-       return ret ? ret : vb2_core_prepare_buf(q, b->index, b);
+       ret = vb2_queue_or_prepare_buf(q, mdev, vb, b, true, NULL);
+
+       return ret ? ret : vb2_core_prepare_buf(q, vb, b);
 }
 EXPORT_SYMBOL_GPL(vb2_prepare_buf);
 
             struct v4l2_buffer *b)
 {
        struct media_request *req = NULL;
+       struct vb2_buffer *vb;
        int ret;
 
        if (vb2_fileio_is_active(q)) {
                return -EBUSY;
        }
 
-       ret = vb2_queue_or_prepare_buf(q, mdev, b, false, &req);
+       if (b->index >= q->num_buffers) {
+               dprintk(q, 1, "buffer index out of range\n");
+               return -EINVAL;
+       }
+       vb = q->bufs[b->index];
+
+       ret = vb2_queue_or_prepare_buf(q, mdev, vb, b, false, &req);
        if (ret)
                return ret;
-       ret = vb2_core_qbuf(q, b->index, b, req);
+       ret = vb2_core_qbuf(q, vb, b, req);
        if (req)
                media_request_put(req);
        return ret;
 
 int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb)
 {
-       return vb2_core_expbuf(q, &eb->fd, eb->type, eb->index,
+       struct vb2_buffer *vb;
+
+       if (eb->index >= q->num_buffers) {
+               dprintk(q, 1, "buffer index out of range\n");
+               return -EINVAL;
+       }
+       vb = q->bufs[eb->index];
+
+       return vb2_core_expbuf(q, &eb->fd, eb->type, vb,
                                eb->plane, eb->flags);
 }
 EXPORT_SYMBOL_GPL(vb2_expbuf);
 
                dprintk(1, "[%s] buffer index out of range\n", ctx->name);
                return -EINVAL;
        }
-       vb2_core_querybuf(&ctx->vb_q, b->index, b);
+       vb2_core_querybuf(&ctx->vb_q, q->bufs[b->index], b);
        dprintk(3, "[%s] index=%d\n", ctx->name, b->index);
        return 0;
 }
        struct vb2_queue *q = &ctx->vb_q;
        int ret;
 
-       ret = vb2_core_expbuf(&ctx->vb_q, &exp->fd, q->type, exp->index,
+       ret = vb2_core_expbuf(&ctx->vb_q, &exp->fd, q->type, q->bufs[exp->index],
                              0, exp->flags);
        if (ret) {
                dprintk(1, "[%s] index=%d errno=%d\n", ctx->name,
                dprintk(1, "[%s] buffer index out of range\n", ctx->name);
                return -EINVAL;
        }
-       ret = vb2_core_qbuf(&ctx->vb_q, b->index, b, NULL);
+       ret = vb2_core_qbuf(&ctx->vb_q, q->bufs[b->index], b, NULL);
        if (ret) {
                dprintk(1, "[%s] index=%d errno=%d\n", ctx->name,
                        b->index, ret);
 
 /**
  * vb2_core_querybuf() - query video buffer information.
  * @q:         pointer to &struct vb2_queue with videobuf2 queue.
- * @index:     id number of the buffer.
+ * @vb:                pointer to struct &vb2_buffer.
  * @pb:                buffer struct passed from userspace.
  *
  * Videobuf2 core helper to implement VIDIOC_QUERYBUF() operation. It is called
  *
  * Return: returns zero on success; an error code otherwise.
  */
-void vb2_core_querybuf(struct vb2_queue *q, unsigned int index, void *pb);
+void vb2_core_querybuf(struct vb2_queue *q, struct vb2_buffer *vb, void *pb);
 
 /**
  * vb2_core_reqbufs() - Initiate streaming.
  * vb2_core_prepare_buf() - Pass ownership of a buffer from userspace
  *                     to the kernel.
  * @q:         pointer to &struct vb2_queue with videobuf2 queue.
- * @index:     id number of the buffer.
+ * @vb:                pointer to struct &vb2_buffer.
  * @pb:                buffer structure passed from userspace to
  *             &v4l2_ioctl_ops->vidioc_prepare_buf handler in driver.
  *
  *
  * Return: returns zero on success; an error code otherwise.
  */
-int vb2_core_prepare_buf(struct vb2_queue *q, unsigned int index, void *pb);
+int vb2_core_prepare_buf(struct vb2_queue *q, struct vb2_buffer *vb, void *pb);
 
 /**
  * vb2_core_qbuf() - Queue a buffer from userspace
  *
  * @q:         pointer to &struct vb2_queue with videobuf2 queue.
- * @index:     id number of the buffer
+ * @vb:                pointer to struct &vb2_buffer.
  * @pb:                buffer structure passed from userspace to
  *             v4l2_ioctl_ops->vidioc_qbuf handler in driver
  * @req:       pointer to &struct media_request, may be NULL.
  *
  * Return: returns zero on success; an error code otherwise.
  */
-int vb2_core_qbuf(struct vb2_queue *q, unsigned int index, void *pb,
+int vb2_core_qbuf(struct vb2_queue *q, struct vb2_buffer *vb, void *pb,
                  struct media_request *req);
 
 /**
  * @fd:                pointer to the file descriptor associated with DMABUF
  *             (set by driver).
  * @type:      buffer type.
- * @index:     id number of the buffer.
+ * @vb:                pointer to struct &vb2_buffer.
  * @plane:     index of the plane to be exported, 0 for single plane queues
  * @flags:     file flags for newly created file, as defined at
  *             include/uapi/asm-generic/fcntl.h.
  * Return: returns zero on success; an error code otherwise.
  */
 int vb2_core_expbuf(struct vb2_queue *q, int *fd, unsigned int type,
-               unsigned int index, unsigned int plane, unsigned int flags);
+                   struct vb2_buffer *vb, unsigned int plane, unsigned int flags);
 
 /**
  * vb2_core_queue_init() - initialize a videobuf2 queue