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 | * |
Andrzej Pietrasiewicz | 7ea95b1 | 2014-09-09 02:02:08 +0300 | [diff] [blame] | 31 | * Video queues is initialized by uvcg_queue_init(). The function performs |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 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 | |
Andrzej Pietrasiewicz | 3a83c16 | 2014-09-09 02:02:09 +0300 | [diff] [blame] | 129 | int uvcg_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type) |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 130 | { |
| 131 | int ret; |
| 132 | |
| 133 | queue->queue.type = type; |
Philipp Zabel | ee7ec7f | 2014-08-21 16:54:44 +0200 | [diff] [blame] | 134 | queue->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF; |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 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; |
Laurent Pinchart | c9e44b5 | 2014-03-23 16:19:50 +0100 | [diff] [blame] | 139 | queue->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC |
| 140 | | V4L2_BUF_FLAG_TSTAMP_SRC_EOF; |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 141 | ret = vb2_queue_init(&queue->queue); |
| 142 | if (ret) |
| 143 | return ret; |
| 144 | |
| 145 | mutex_init(&queue->mutex); |
| 146 | spin_lock_init(&queue->irqlock); |
| 147 | INIT_LIST_HEAD(&queue->irqqueue); |
| 148 | queue->flags = 0; |
| 149 | |
Laurent Pinchart | 5d9955f | 2010-07-10 16:13:05 -0300 | [diff] [blame] | 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | /* |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 154 | * Free the video buffers. |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 155 | */ |
Andrzej Pietrasiewicz | 3a83c16 | 2014-09-09 02:02:09 +0300 | [diff] [blame] | 156 | void uvcg_free_buffers(struct uvc_video_queue *queue) |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 157 | { |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 158 | mutex_lock(&queue->mutex); |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 159 | vb2_queue_release(&queue->queue); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 160 | mutex_unlock(&queue->mutex); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | /* |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 164 | * Allocate the video buffers. |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 165 | */ |
Andrzej Pietrasiewicz | 3a83c16 | 2014-09-09 02:02:09 +0300 | [diff] [blame] | 166 | int uvcg_alloc_buffers(struct uvc_video_queue *queue, |
Andrzej Pietrasiewicz | 7ea95b1 | 2014-09-09 02:02:08 +0300 | [diff] [blame] | 167 | struct v4l2_requestbuffers *rb) |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 168 | { |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 169 | int ret; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 170 | |
| 171 | mutex_lock(&queue->mutex); |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 172 | ret = vb2_reqbufs(&queue->queue, rb); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 173 | mutex_unlock(&queue->mutex); |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 174 | |
| 175 | return ret ? ret : rb->count; |
| 176 | } |
| 177 | |
Andrzej Pietrasiewicz | 3a83c16 | 2014-09-09 02:02:09 +0300 | [diff] [blame] | 178 | int uvcg_query_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf) |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 179 | { |
| 180 | int ret; |
| 181 | |
| 182 | mutex_lock(&queue->mutex); |
| 183 | ret = vb2_querybuf(&queue->queue, buf); |
| 184 | mutex_unlock(&queue->mutex); |
| 185 | |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 186 | return ret; |
| 187 | } |
| 188 | |
Andrzej Pietrasiewicz | 3a83c16 | 2014-09-09 02:02:09 +0300 | [diff] [blame] | 189 | int uvcg_queue_buffer(struct uvc_video_queue *queue, 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 | */ |
Andrzej Pietrasiewicz | 3a83c16 | 2014-09-09 02:02:09 +0300 | [diff] [blame] | 213 | int uvcg_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf, |
| 214 | 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 | */ |
Andrzej Pietrasiewicz | 3a83c16 | 2014-09-09 02:02:09 +0300 | [diff] [blame] | 231 | unsigned int uvcg_queue_poll(struct uvc_video_queue *queue, struct file *file, |
| 232 | 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 | |
Andrzej Pietrasiewicz | 3a83c16 | 2014-09-09 02:02:09 +0300 | [diff] [blame] | 243 | int uvcg_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma) |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 244 | { |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 245 | int ret; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 246 | |
| 247 | mutex_lock(&queue->mutex); |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 248 | ret = vb2_mmap(&queue->queue, vma); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 249 | mutex_unlock(&queue->mutex); |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 250 | |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 251 | return ret; |
| 252 | } |
| 253 | |
Bhupesh Sharma | 2f1d570 | 2013-03-28 15:11:53 +0530 | [diff] [blame] | 254 | #ifndef CONFIG_MMU |
| 255 | /* |
| 256 | * Get unmapped area. |
| 257 | * |
| 258 | * NO-MMU arch need this function to make mmap() work correctly. |
| 259 | */ |
Andrzej Pietrasiewicz | 3a83c16 | 2014-09-09 02:02:09 +0300 | [diff] [blame] | 260 | unsigned long uvcg_queue_get_unmapped_area(struct uvc_video_queue *queue, |
| 261 | unsigned long pgoff) |
Bhupesh Sharma | 2f1d570 | 2013-03-28 15:11:53 +0530 | [diff] [blame] | 262 | { |
| 263 | unsigned long ret; |
| 264 | |
| 265 | mutex_lock(&queue->mutex); |
| 266 | ret = vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0); |
| 267 | mutex_unlock(&queue->mutex); |
| 268 | return ret; |
| 269 | } |
| 270 | #endif |
| 271 | |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 272 | /* |
Laurent Pinchart | 5d9955f | 2010-07-10 16:13:05 -0300 | [diff] [blame] | 273 | * Cancel the video buffers queue. |
| 274 | * |
| 275 | * Cancelling the queue marks all buffers on the irq queue as erroneous, |
| 276 | * wakes them up and removes them from the queue. |
| 277 | * |
| 278 | * If the disconnect parameter is set, further calls to uvc_queue_buffer will |
| 279 | * fail with -ENODEV. |
| 280 | * |
| 281 | * This function acquires the irq spinlock and can be called from interrupt |
| 282 | * context. |
| 283 | */ |
Andrzej Pietrasiewicz | 3a83c16 | 2014-09-09 02:02:09 +0300 | [diff] [blame] | 284 | void uvcg_queue_cancel(struct uvc_video_queue *queue, int disconnect) |
Laurent Pinchart | 5d9955f | 2010-07-10 16:13:05 -0300 | [diff] [blame] | 285 | { |
| 286 | struct uvc_buffer *buf; |
| 287 | unsigned long flags; |
| 288 | |
| 289 | spin_lock_irqsave(&queue->irqlock, flags); |
| 290 | while (!list_empty(&queue->irqqueue)) { |
| 291 | buf = list_first_entry(&queue->irqqueue, struct uvc_buffer, |
| 292 | queue); |
| 293 | list_del(&buf->queue); |
| 294 | buf->state = UVC_BUF_STATE_ERROR; |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 295 | vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR); |
Laurent Pinchart | 5d9955f | 2010-07-10 16:13:05 -0300 | [diff] [blame] | 296 | } |
| 297 | /* This must be protected by the irqlock spinlock to avoid race |
| 298 | * conditions between uvc_queue_buffer and the disconnection event that |
| 299 | * could result in an interruptible wait in uvc_dequeue_buffer. Do not |
| 300 | * blindly replace this logic by checking for the UVC_DEV_DISCONNECTED |
| 301 | * state outside the queue code. |
| 302 | */ |
| 303 | if (disconnect) |
| 304 | queue->flags |= UVC_QUEUE_DISCONNECTED; |
| 305 | spin_unlock_irqrestore(&queue->irqlock, flags); |
| 306 | } |
| 307 | |
| 308 | /* |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 309 | * Enable or disable the video buffers queue. |
| 310 | * |
| 311 | * The queue must be enabled before starting video acquisition and must be |
| 312 | * disabled after stopping it. This ensures that the video buffers queue |
| 313 | * state can be properly initialized before buffers are accessed from the |
| 314 | * interrupt handler. |
| 315 | * |
| 316 | * Enabling the video queue initializes parameters (such as sequence number, |
| 317 | * sync pattern, ...). If the queue is already enabled, return -EBUSY. |
| 318 | * |
| 319 | * Disabling the video queue cancels the queue and removes all buffers from |
| 320 | * the main queue. |
| 321 | * |
| 322 | * This function can't be called from interrupt context. Use |
Andrzej Pietrasiewicz | 7ea95b1 | 2014-09-09 02:02:08 +0300 | [diff] [blame] | 323 | * uvcg_queue_cancel() instead. |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 324 | */ |
Andrzej Pietrasiewicz | 3a83c16 | 2014-09-09 02:02:09 +0300 | [diff] [blame] | 325 | int uvcg_queue_enable(struct uvc_video_queue *queue, int enable) |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 326 | { |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 327 | unsigned long flags; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 328 | int ret = 0; |
| 329 | |
| 330 | mutex_lock(&queue->mutex); |
| 331 | if (enable) { |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 332 | ret = vb2_streamon(&queue->queue, queue->queue.type); |
| 333 | if (ret < 0) |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 334 | goto done; |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 335 | |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 336 | queue->sequence = 0; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 337 | queue->buf_used = 0; |
| 338 | } else { |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 339 | ret = vb2_streamoff(&queue->queue, queue->queue.type); |
| 340 | if (ret < 0) |
| 341 | goto done; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 342 | |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 343 | spin_lock_irqsave(&queue->irqlock, flags); |
| 344 | INIT_LIST_HEAD(&queue->irqqueue); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 345 | |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 346 | /* |
| 347 | * FIXME: We need to clear the DISCONNECTED flag to ensure that |
| 348 | * applications will be able to queue buffers for the next |
| 349 | * streaming run. However, clearing it here doesn't guarantee |
| 350 | * that the device will be reconnected in the meantime. |
| 351 | */ |
| 352 | queue->flags &= ~UVC_QUEUE_DISCONNECTED; |
| 353 | spin_unlock_irqrestore(&queue->irqlock, flags); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | done: |
| 357 | mutex_unlock(&queue->mutex); |
| 358 | return ret; |
| 359 | } |
| 360 | |
Bhupesh Sharma | 95faf82 | 2012-03-22 00:50:37 -0300 | [diff] [blame] | 361 | /* called with &queue_irqlock held.. */ |
Andrzej Pietrasiewicz | 3a83c16 | 2014-09-09 02:02:09 +0300 | [diff] [blame] | 362 | struct uvc_buffer *uvcg_queue_next_buffer(struct uvc_video_queue *queue, |
| 363 | struct uvc_buffer *buf) |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 364 | { |
| 365 | struct uvc_buffer *nextbuf; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 366 | |
| 367 | if ((queue->flags & UVC_QUEUE_DROP_INCOMPLETE) && |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 368 | buf->length != buf->bytesused) { |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 369 | buf->state = UVC_BUF_STATE_QUEUED; |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 370 | vb2_set_plane_payload(&buf->buf, 0, 0); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 371 | return buf; |
| 372 | } |
| 373 | |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 374 | list_del(&buf->queue); |
| 375 | if (!list_empty(&queue->irqqueue)) |
| 376 | nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer, |
| 377 | queue); |
| 378 | else |
| 379 | nextbuf = NULL; |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 380 | |
Laurent Pinchart | f17388c | 2014-03-23 16:25:09 +0100 | [diff] [blame] | 381 | buf->buf.v4l2_buf.field = V4L2_FIELD_NONE; |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 382 | buf->buf.v4l2_buf.sequence = queue->sequence++; |
Laurent Pinchart | 16bf900 | 2014-03-23 16:25:09 +0100 | [diff] [blame] | 383 | v4l2_get_timestamp(&buf->buf.v4l2_buf.timestamp); |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 384 | |
Bhupesh Sharma | d692522 | 2013-03-28 15:11:52 +0530 | [diff] [blame] | 385 | vb2_set_plane_payload(&buf->buf, 0, buf->bytesused); |
| 386 | vb2_buffer_done(&buf->buf, VB2_BUF_STATE_DONE); |
| 387 | |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 388 | return nextbuf; |
| 389 | } |
| 390 | |
Andrzej Pietrasiewicz | 3a83c16 | 2014-09-09 02:02:09 +0300 | [diff] [blame] | 391 | struct uvc_buffer *uvcg_queue_head(struct uvc_video_queue *queue) |
Laurent Pinchart | cdda479 | 2010-05-02 20:57:41 +0200 | [diff] [blame] | 392 | { |
| 393 | struct uvc_buffer *buf = NULL; |
| 394 | |
| 395 | if (!list_empty(&queue->irqqueue)) |
| 396 | buf = list_first_entry(&queue->irqqueue, struct uvc_buffer, |
| 397 | queue); |
| 398 | else |
| 399 | queue->flags |= UVC_QUEUE_PAUSED; |
| 400 | |
| 401 | return buf; |
| 402 | } |
| 403 | |