Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 1 | /* |
| 2 | * uvc_queue.c -- USB Video Class driver - Buffers management |
| 3 | * |
| 4 | * Copyright (C) 2005-2010 |
| 5 | * Laurent Pinchart (laurent.pinchart@ideasonboard.com) |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 11 | */ |
| 12 | |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 13 | #include <linux/atomic.h> |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 14 | #include <linux/kernel.h> |
| 15 | #include <linux/mm.h> |
| 16 | #include <linux/list.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/usb.h> |
| 19 | #include <linux/videodev2.h> |
| 20 | #include <linux/vmalloc.h> |
| 21 | #include <linux/wait.h> |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 22 | |
Laurent Pinchart | 16bf900 | 2014-03-23 16:25:09 +0100 | [diff] [blame] | 23 | #include <media/v4l2-common.h> |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 24 | #include <media/videobuf2-vmalloc.h> |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 25 | |
| 26 | #include "uvc.h" |
| 27 | |
| 28 | /* ------------------------------------------------------------------------ |
| 29 | * Video buffers queue management. |
| 30 | * |
| 31 | * Video queues is initialized by uvc_queue_init(). The function performs |
| 32 | * basic initialization of the uvc_video_queue struct and never fails. |
| 33 | * |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 34 | * Video buffers are managed by videobuf2. The driver uses a mutex to protect |
| 35 | * the videobuf2 queue operations by serializing calls to videobuf2 and a |
| 36 | * spinlock to protect the IRQ queue that holds the buffers to be processed by |
| 37 | * the driver. |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 38 | */ |
| 39 | |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 40 | /* ----------------------------------------------------------------------------- |
| 41 | * videobuf2 queue operations |
| 42 | */ |
| 43 | |
| 44 | static int uvc_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt, |
| 45 | unsigned int *nbuffers, unsigned int *nplanes, |
| 46 | unsigned int sizes[], void *alloc_ctxs[]) |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 47 | { |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 48 | struct uvc_video_queue *queue = vb2_get_drv_priv(vq); |
| 49 | struct uvc_video *video = container_of(queue, struct uvc_video, queue); |
| 50 | |
| 51 | if (*nbuffers > UVC_MAX_VIDEO_BUFFERS) |
| 52 | *nbuffers = UVC_MAX_VIDEO_BUFFERS; |
| 53 | |
| 54 | *nplanes = 1; |
| 55 | |
| 56 | sizes[0] = video->imagesize; |
| 57 | |
| 58 | return 0; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 59 | } |
| 60 | |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 61 | static int uvc_buffer_prepare(struct vb2_buffer *vb) |
Laurent Pinchart | 5d9955f | 2010-07-10 16:13:05 -0300 | [diff] [blame] | 62 | { |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 63 | struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue); |
| 64 | struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf); |
Laurent Pinchart | 5d9955f | 2010-07-10 16:13:05 -0300 | [diff] [blame] | 65 | |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 66 | if (vb->v4l2_buf.type == V4L2_BUF_TYPE_VIDEO_OUTPUT && |
| 67 | vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) { |
| 68 | uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n"); |
| 69 | return -EINVAL; |
Laurent Pinchart | 5d9955f | 2010-07-10 16:13:05 -0300 | [diff] [blame] | 70 | } |
| 71 | |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 72 | if (unlikely(queue->flags & UVC_QUEUE_DISCONNECTED)) |
| 73 | return -ENODEV; |
| 74 | |
| 75 | buf->state = UVC_BUF_STATE_QUEUED; |
| 76 | buf->mem = vb2_plane_vaddr(vb, 0); |
| 77 | buf->length = vb2_plane_size(vb, 0); |
| 78 | if (vb->v4l2_buf.type == V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 79 | buf->bytesused = 0; |
| 80 | else |
| 81 | buf->bytesused = vb2_get_plane_payload(vb, 0); |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | static void uvc_buffer_queue(struct vb2_buffer *vb) |
| 87 | { |
| 88 | struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue); |
| 89 | struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf); |
| 90 | unsigned long flags; |
| 91 | |
| 92 | spin_lock_irqsave(&queue->irqlock, flags); |
| 93 | |
| 94 | if (likely(!(queue->flags & UVC_QUEUE_DISCONNECTED))) { |
| 95 | list_add_tail(&buf->queue, &queue->irqqueue); |
| 96 | } else { |
| 97 | /* If the device is disconnected return the buffer to userspace |
| 98 | * directly. The next QBUF call will fail with -ENODEV. |
| 99 | */ |
| 100 | buf->state = UVC_BUF_STATE_ERROR; |
| 101 | vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR); |
Laurent Pinchart | 5d9955f | 2010-07-10 16:13:05 -0300 | [diff] [blame] | 102 | } |
| 103 | |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 104 | spin_unlock_irqrestore(&queue->irqlock, flags); |
| 105 | } |
| 106 | |
Michael Grzeschik | a2cc81d | 2013-02-09 00:54:54 +0100 | [diff] [blame] | 107 | static void uvc_wait_prepare(struct vb2_queue *vq) |
| 108 | { |
| 109 | struct uvc_video_queue *queue = vb2_get_drv_priv(vq); |
| 110 | |
| 111 | mutex_unlock(&queue->mutex); |
| 112 | } |
| 113 | |
| 114 | static void uvc_wait_finish(struct vb2_queue *vq) |
| 115 | { |
| 116 | struct uvc_video_queue *queue = vb2_get_drv_priv(vq); |
| 117 | |
| 118 | mutex_lock(&queue->mutex); |
| 119 | } |
| 120 | |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 121 | static struct vb2_ops uvc_queue_qops = { |
| 122 | .queue_setup = uvc_queue_setup, |
| 123 | .buf_prepare = uvc_buffer_prepare, |
| 124 | .buf_queue = uvc_buffer_queue, |
Michael Grzeschik | a2cc81d | 2013-02-09 00:54:54 +0100 | [diff] [blame] | 125 | .wait_prepare = uvc_wait_prepare, |
| 126 | .wait_finish = uvc_wait_finish, |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | static int uvc_queue_init(struct uvc_video_queue *queue, |
| 130 | enum v4l2_buf_type type) |
| 131 | { |
| 132 | int ret; |
| 133 | |
| 134 | queue->queue.type = type; |
Philipp Zabel | ee7ec7f | 2014-08-21 16:54:44 +0200 | [diff] [blame^] | 135 | queue->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF; |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 136 | queue->queue.drv_priv = queue; |
| 137 | queue->queue.buf_struct_size = sizeof(struct uvc_buffer); |
| 138 | queue->queue.ops = &uvc_queue_qops; |
| 139 | queue->queue.mem_ops = &vb2_vmalloc_memops; |
Laurent Pinchart | c9e44b5 | 2014-03-23 16:19:50 +0100 | [diff] [blame] | 140 | queue->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC |
| 141 | | V4L2_BUF_FLAG_TSTAMP_SRC_EOF; |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 142 | ret = vb2_queue_init(&queue->queue); |
| 143 | if (ret) |
| 144 | return ret; |
| 145 | |
| 146 | mutex_init(&queue->mutex); |
| 147 | spin_lock_init(&queue->irqlock); |
| 148 | INIT_LIST_HEAD(&queue->irqqueue); |
| 149 | queue->flags = 0; |
| 150 | |
Laurent Pinchart | 5d9955f | 2010-07-10 16:13:05 -0300 | [diff] [blame] | 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | /* |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 155 | * Free the video buffers. |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 156 | */ |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 157 | static void uvc_free_buffers(struct uvc_video_queue *queue) |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 158 | { |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 159 | mutex_lock(&queue->mutex); |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 160 | vb2_queue_release(&queue->queue); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 161 | mutex_unlock(&queue->mutex); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /* |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 165 | * Allocate the video buffers. |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 166 | */ |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 167 | static int uvc_alloc_buffers(struct uvc_video_queue *queue, |
| 168 | struct v4l2_requestbuffers *rb) |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 169 | { |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 170 | int ret; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 171 | |
| 172 | mutex_lock(&queue->mutex); |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 173 | ret = vb2_reqbufs(&queue->queue, rb); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 174 | mutex_unlock(&queue->mutex); |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 175 | |
| 176 | return ret ? ret : rb->count; |
| 177 | } |
| 178 | |
| 179 | static int uvc_query_buffer(struct uvc_video_queue *queue, |
| 180 | struct v4l2_buffer *buf) |
| 181 | { |
| 182 | int ret; |
| 183 | |
| 184 | mutex_lock(&queue->mutex); |
| 185 | ret = vb2_querybuf(&queue->queue, buf); |
| 186 | mutex_unlock(&queue->mutex); |
| 187 | |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 188 | return ret; |
| 189 | } |
| 190 | |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 191 | static int uvc_queue_buffer(struct uvc_video_queue *queue, |
| 192 | struct v4l2_buffer *buf) |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 193 | { |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 194 | unsigned long flags; |
| 195 | int ret; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 196 | |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 197 | mutex_lock(&queue->mutex); |
| 198 | ret = vb2_qbuf(&queue->queue, buf); |
Laurent Pinchart | ebe864a | 2013-04-29 22:18:01 +0200 | [diff] [blame] | 199 | if (ret < 0) |
| 200 | goto done; |
| 201 | |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 202 | spin_lock_irqsave(&queue->irqlock, flags); |
| 203 | ret = (queue->flags & UVC_QUEUE_PAUSED) != 0; |
| 204 | queue->flags &= ~UVC_QUEUE_PAUSED; |
| 205 | spin_unlock_irqrestore(&queue->irqlock, flags); |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 206 | |
Laurent Pinchart | ebe864a | 2013-04-29 22:18:01 +0200 | [diff] [blame] | 207 | done: |
| 208 | mutex_unlock(&queue->mutex); |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 209 | return ret; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /* |
| 213 | * Dequeue a video buffer. If nonblocking is false, block until a buffer is |
| 214 | * available. |
| 215 | */ |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 216 | static int uvc_dequeue_buffer(struct uvc_video_queue *queue, |
| 217 | struct v4l2_buffer *buf, int nonblocking) |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 218 | { |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 219 | int ret; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 220 | |
| 221 | mutex_lock(&queue->mutex); |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 222 | ret = vb2_dqbuf(&queue->queue, buf, nonblocking); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 223 | mutex_unlock(&queue->mutex); |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 224 | |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 225 | return ret; |
| 226 | } |
| 227 | |
| 228 | /* |
| 229 | * Poll the video queue. |
| 230 | * |
| 231 | * This function implements video queue polling and is intended to be used by |
| 232 | * the device poll handler. |
| 233 | */ |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 234 | static unsigned int uvc_queue_poll(struct uvc_video_queue *queue, |
| 235 | struct file *file, poll_table *wait) |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 236 | { |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 237 | unsigned int ret; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 238 | |
| 239 | mutex_lock(&queue->mutex); |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 240 | ret = vb2_poll(&queue->queue, file, wait); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 241 | mutex_unlock(&queue->mutex); |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 242 | |
| 243 | return ret; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 244 | } |
| 245 | |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 246 | static int uvc_queue_mmap(struct uvc_video_queue *queue, |
| 247 | struct vm_area_struct *vma) |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 248 | { |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 249 | int ret; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 250 | |
| 251 | mutex_lock(&queue->mutex); |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 252 | ret = vb2_mmap(&queue->queue, vma); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 253 | mutex_unlock(&queue->mutex); |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 254 | |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 255 | return ret; |
| 256 | } |
| 257 | |
Bhupesh Sharma | 2f1d570 | 2013-03-28 15:11:53 +0530 | [diff] [blame] | 258 | #ifndef CONFIG_MMU |
| 259 | /* |
| 260 | * Get unmapped area. |
| 261 | * |
| 262 | * NO-MMU arch need this function to make mmap() work correctly. |
| 263 | */ |
| 264 | static unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue, |
| 265 | unsigned long pgoff) |
| 266 | { |
| 267 | unsigned long ret; |
| 268 | |
| 269 | mutex_lock(&queue->mutex); |
| 270 | ret = vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0); |
| 271 | mutex_unlock(&queue->mutex); |
| 272 | return ret; |
| 273 | } |
| 274 | #endif |
| 275 | |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 276 | /* |
Laurent Pinchart | 5d9955f | 2010-07-10 16:13:05 -0300 | [diff] [blame] | 277 | * Cancel the video buffers queue. |
| 278 | * |
| 279 | * Cancelling the queue marks all buffers on the irq queue as erroneous, |
| 280 | * wakes them up and removes them from the queue. |
| 281 | * |
| 282 | * If the disconnect parameter is set, further calls to uvc_queue_buffer will |
| 283 | * fail with -ENODEV. |
| 284 | * |
| 285 | * This function acquires the irq spinlock and can be called from interrupt |
| 286 | * context. |
| 287 | */ |
| 288 | static void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect) |
| 289 | { |
| 290 | struct uvc_buffer *buf; |
| 291 | unsigned long flags; |
| 292 | |
| 293 | spin_lock_irqsave(&queue->irqlock, flags); |
| 294 | while (!list_empty(&queue->irqqueue)) { |
| 295 | buf = list_first_entry(&queue->irqqueue, struct uvc_buffer, |
| 296 | queue); |
| 297 | list_del(&buf->queue); |
| 298 | buf->state = UVC_BUF_STATE_ERROR; |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 299 | vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR); |
Laurent Pinchart | 5d9955f | 2010-07-10 16:13:05 -0300 | [diff] [blame] | 300 | } |
| 301 | /* This must be protected by the irqlock spinlock to avoid race |
| 302 | * conditions between uvc_queue_buffer and the disconnection event that |
| 303 | * could result in an interruptible wait in uvc_dequeue_buffer. Do not |
| 304 | * blindly replace this logic by checking for the UVC_DEV_DISCONNECTED |
| 305 | * state outside the queue code. |
| 306 | */ |
| 307 | if (disconnect) |
| 308 | queue->flags |= UVC_QUEUE_DISCONNECTED; |
| 309 | spin_unlock_irqrestore(&queue->irqlock, flags); |
| 310 | } |
| 311 | |
| 312 | /* |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 313 | * Enable or disable the video buffers queue. |
| 314 | * |
| 315 | * The queue must be enabled before starting video acquisition and must be |
| 316 | * disabled after stopping it. This ensures that the video buffers queue |
| 317 | * state can be properly initialized before buffers are accessed from the |
| 318 | * interrupt handler. |
| 319 | * |
| 320 | * Enabling the video queue initializes parameters (such as sequence number, |
| 321 | * sync pattern, ...). If the queue is already enabled, return -EBUSY. |
| 322 | * |
| 323 | * Disabling the video queue cancels the queue and removes all buffers from |
| 324 | * the main queue. |
| 325 | * |
| 326 | * This function can't be called from interrupt context. Use |
| 327 | * uvc_queue_cancel() instead. |
| 328 | */ |
Laurent Pinchart | 5d9955f | 2010-07-10 16:13:05 -0300 | [diff] [blame] | 329 | static int uvc_queue_enable(struct uvc_video_queue *queue, int enable) |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 330 | { |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 331 | unsigned long flags; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 332 | int ret = 0; |
| 333 | |
| 334 | mutex_lock(&queue->mutex); |
| 335 | if (enable) { |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 336 | ret = vb2_streamon(&queue->queue, queue->queue.type); |
| 337 | if (ret < 0) |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 338 | goto done; |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 339 | |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 340 | queue->sequence = 0; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 341 | queue->buf_used = 0; |
| 342 | } else { |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 343 | ret = vb2_streamoff(&queue->queue, queue->queue.type); |
| 344 | if (ret < 0) |
| 345 | goto done; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 346 | |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 347 | spin_lock_irqsave(&queue->irqlock, flags); |
| 348 | INIT_LIST_HEAD(&queue->irqqueue); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 349 | |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 350 | /* |
| 351 | * FIXME: We need to clear the DISCONNECTED flag to ensure that |
| 352 | * applications will be able to queue buffers for the next |
| 353 | * streaming run. However, clearing it here doesn't guarantee |
| 354 | * that the device will be reconnected in the meantime. |
| 355 | */ |
| 356 | queue->flags &= ~UVC_QUEUE_DISCONNECTED; |
| 357 | spin_unlock_irqrestore(&queue->irqlock, flags); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | done: |
| 361 | mutex_unlock(&queue->mutex); |
| 362 | return ret; |
| 363 | } |
| 364 | |
Bhupesh Sharma | 95faf82 | 2012-03-22 00:50:37 -0300 | [diff] [blame] | 365 | /* called with &queue_irqlock held.. */ |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 366 | static struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue, |
| 367 | struct uvc_buffer *buf) |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 368 | { |
| 369 | struct uvc_buffer *nextbuf; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 370 | |
| 371 | if ((queue->flags & UVC_QUEUE_DROP_INCOMPLETE) && |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 372 | buf->length != buf->bytesused) { |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 373 | buf->state = UVC_BUF_STATE_QUEUED; |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 374 | vb2_set_plane_payload(&buf->buf, 0, 0); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 375 | return buf; |
| 376 | } |
| 377 | |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 378 | list_del(&buf->queue); |
| 379 | if (!list_empty(&queue->irqqueue)) |
| 380 | nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer, |
| 381 | queue); |
| 382 | else |
| 383 | nextbuf = NULL; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 384 | |
Laurent Pinchart | f17388c | 2014-03-23 16:25:09 +0100 | [diff] [blame] | 385 | buf->buf.v4l2_buf.field = V4L2_FIELD_NONE; |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 386 | buf->buf.v4l2_buf.sequence = queue->sequence++; |
Laurent Pinchart | 16bf900 | 2014-03-23 16:25:09 +0100 | [diff] [blame] | 387 | v4l2_get_timestamp(&buf->buf.v4l2_buf.timestamp); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 388 | |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 389 | vb2_set_plane_payload(&buf->buf, 0, buf->bytesused); |
| 390 | vb2_buffer_done(&buf->buf, VB2_BUF_STATE_DONE); |
| 391 | |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 392 | return nextbuf; |
| 393 | } |
| 394 | |
Laurent Pinchart | 5d9955f | 2010-07-10 16:13:05 -0300 | [diff] [blame] | 395 | static struct uvc_buffer *uvc_queue_head(struct uvc_video_queue *queue) |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 396 | { |
| 397 | struct uvc_buffer *buf = NULL; |
| 398 | |
| 399 | if (!list_empty(&queue->irqqueue)) |
| 400 | buf = list_first_entry(&queue->irqqueue, struct uvc_buffer, |
| 401 | queue); |
| 402 | else |
| 403 | queue->flags |= UVC_QUEUE_PAUSED; |
| 404 | |
| 405 | return buf; |
| 406 | } |
| 407 | |