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