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