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. 11 * 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 #include <media/videobuf2-vmalloc.h> 24 25 #include "uvcvideo.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 * 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. 37 */ 38 39 static inline struct uvc_streaming * 40 uvc_queue_to_stream(struct uvc_video_queue *queue) 41 { 42 return container_of(queue, struct uvc_streaming, queue); 43 } 44 45 /* 46 * Return all queued buffers to videobuf2 in the requested state. 47 * 48 * This function must be called with the queue spinlock held. 49 */ 50 static void uvc_queue_return_buffers(struct uvc_video_queue *queue, 51 enum uvc_buffer_state state) 52 { 53 enum vb2_buffer_state vb2_state = state == UVC_BUF_STATE_ERROR 54 ? VB2_BUF_STATE_ERROR 55 : VB2_BUF_STATE_QUEUED; 56 57 while (!list_empty(&queue->irqqueue)) { 58 struct uvc_buffer *buf = list_first_entry(&queue->irqqueue, 59 struct uvc_buffer, 60 queue); 61 list_del(&buf->queue); 62 buf->state = state; 63 vb2_buffer_done(&buf->buf, vb2_state); 64 } 65 } 66 67 /* ----------------------------------------------------------------------------- 68 * videobuf2 queue operations 69 */ 70 71 static int uvc_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt, 72 unsigned int *nbuffers, unsigned int *nplanes, 73 unsigned int sizes[], void *alloc_ctxs[]) 74 { 75 struct uvc_video_queue *queue = vb2_get_drv_priv(vq); 76 struct uvc_streaming *stream = uvc_queue_to_stream(queue); 77 78 /* Make sure the image size is large enough. */ 79 if (fmt && fmt->fmt.pix.sizeimage < stream->ctrl.dwMaxVideoFrameSize) 80 return -EINVAL; 81 82 *nplanes = 1; 83 84 sizes[0] = fmt ? fmt->fmt.pix.sizeimage 85 : stream->ctrl.dwMaxVideoFrameSize; 86 87 return 0; 88 } 89 90 static int uvc_buffer_prepare(struct vb2_buffer *vb) 91 { 92 struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue); 93 struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf); 94 95 if (vb->v4l2_buf.type == V4L2_BUF_TYPE_VIDEO_OUTPUT && 96 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) { 97 uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n"); 98 return -EINVAL; 99 } 100 101 if (unlikely(queue->flags & UVC_QUEUE_DISCONNECTED)) 102 return -ENODEV; 103 104 buf->state = UVC_BUF_STATE_QUEUED; 105 buf->error = 0; 106 buf->mem = vb2_plane_vaddr(vb, 0); 107 buf->length = vb2_plane_size(vb, 0); 108 if (vb->v4l2_buf.type == V4L2_BUF_TYPE_VIDEO_CAPTURE) 109 buf->bytesused = 0; 110 else 111 buf->bytesused = vb2_get_plane_payload(vb, 0); 112 113 return 0; 114 } 115 116 static void uvc_buffer_queue(struct vb2_buffer *vb) 117 { 118 struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue); 119 struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf); 120 unsigned long flags; 121 122 spin_lock_irqsave(&queue->irqlock, flags); 123 if (likely(!(queue->flags & UVC_QUEUE_DISCONNECTED))) { 124 list_add_tail(&buf->queue, &queue->irqqueue); 125 } else { 126 /* If the device is disconnected return the buffer to userspace 127 * directly. The next QBUF call will fail with -ENODEV. 128 */ 129 buf->state = UVC_BUF_STATE_ERROR; 130 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR); 131 } 132 133 spin_unlock_irqrestore(&queue->irqlock, flags); 134 } 135 136 static void uvc_buffer_finish(struct vb2_buffer *vb) 137 { 138 struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue); 139 struct uvc_streaming *stream = uvc_queue_to_stream(queue); 140 struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf); 141 142 if (vb->state == VB2_BUF_STATE_DONE) 143 uvc_video_clock_update(stream, &vb->v4l2_buf, buf); 144 } 145 146 static void uvc_wait_prepare(struct vb2_queue *vq) 147 { 148 struct uvc_video_queue *queue = vb2_get_drv_priv(vq); 149 150 mutex_unlock(&queue->mutex); 151 } 152 153 static void uvc_wait_finish(struct vb2_queue *vq) 154 { 155 struct uvc_video_queue *queue = vb2_get_drv_priv(vq); 156 157 mutex_lock(&queue->mutex); 158 } 159 160 static int uvc_start_streaming(struct vb2_queue *vq, unsigned int count) 161 { 162 struct uvc_video_queue *queue = vb2_get_drv_priv(vq); 163 struct uvc_streaming *stream = uvc_queue_to_stream(queue); 164 unsigned long flags; 165 int ret; 166 167 queue->buf_used = 0; 168 169 ret = uvc_video_enable(stream, 1); 170 if (ret == 0) 171 return 0; 172 173 spin_lock_irqsave(&queue->irqlock, flags); 174 uvc_queue_return_buffers(queue, UVC_BUF_STATE_QUEUED); 175 spin_unlock_irqrestore(&queue->irqlock, flags); 176 177 return ret; 178 } 179 180 static void uvc_stop_streaming(struct vb2_queue *vq) 181 { 182 struct uvc_video_queue *queue = vb2_get_drv_priv(vq); 183 struct uvc_streaming *stream = uvc_queue_to_stream(queue); 184 unsigned long flags; 185 186 uvc_video_enable(stream, 0); 187 188 spin_lock_irqsave(&queue->irqlock, flags); 189 uvc_queue_return_buffers(queue, UVC_BUF_STATE_ERROR); 190 spin_unlock_irqrestore(&queue->irqlock, flags); 191 } 192 193 static struct vb2_ops uvc_queue_qops = { 194 .queue_setup = uvc_queue_setup, 195 .buf_prepare = uvc_buffer_prepare, 196 .buf_queue = uvc_buffer_queue, 197 .buf_finish = uvc_buffer_finish, 198 .wait_prepare = uvc_wait_prepare, 199 .wait_finish = uvc_wait_finish, 200 .start_streaming = uvc_start_streaming, 201 .stop_streaming = uvc_stop_streaming, 202 }; 203 204 int uvc_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type, 205 int drop_corrupted) 206 { 207 int ret; 208 209 queue->queue.type = type; 210 queue->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF; 211 queue->queue.drv_priv = queue; 212 queue->queue.buf_struct_size = sizeof(struct uvc_buffer); 213 queue->queue.ops = &uvc_queue_qops; 214 queue->queue.mem_ops = &vb2_vmalloc_memops; 215 queue->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC 216 | V4L2_BUF_FLAG_TSTAMP_SRC_SOE; 217 ret = vb2_queue_init(&queue->queue); 218 if (ret) 219 return ret; 220 221 mutex_init(&queue->mutex); 222 spin_lock_init(&queue->irqlock); 223 INIT_LIST_HEAD(&queue->irqqueue); 224 queue->flags = drop_corrupted ? UVC_QUEUE_DROP_CORRUPTED : 0; 225 226 return 0; 227 } 228 229 void uvc_queue_release(struct uvc_video_queue *queue) 230 { 231 mutex_lock(&queue->mutex); 232 vb2_queue_release(&queue->queue); 233 mutex_unlock(&queue->mutex); 234 } 235 236 /* ----------------------------------------------------------------------------- 237 * V4L2 queue operations 238 */ 239 240 int uvc_request_buffers(struct uvc_video_queue *queue, 241 struct v4l2_requestbuffers *rb) 242 { 243 int ret; 244 245 mutex_lock(&queue->mutex); 246 ret = vb2_reqbufs(&queue->queue, rb); 247 mutex_unlock(&queue->mutex); 248 249 return ret ? ret : rb->count; 250 } 251 252 int uvc_query_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf) 253 { 254 int ret; 255 256 mutex_lock(&queue->mutex); 257 ret = vb2_querybuf(&queue->queue, buf); 258 mutex_unlock(&queue->mutex); 259 260 return ret; 261 } 262 263 int uvc_create_buffers(struct uvc_video_queue *queue, 264 struct v4l2_create_buffers *cb) 265 { 266 int ret; 267 268 mutex_lock(&queue->mutex); 269 ret = vb2_create_bufs(&queue->queue, cb); 270 mutex_unlock(&queue->mutex); 271 272 return ret; 273 } 274 275 int uvc_queue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf) 276 { 277 int ret; 278 279 mutex_lock(&queue->mutex); 280 ret = vb2_qbuf(&queue->queue, buf); 281 mutex_unlock(&queue->mutex); 282 283 return ret; 284 } 285 286 int uvc_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf, 287 int nonblocking) 288 { 289 int ret; 290 291 mutex_lock(&queue->mutex); 292 ret = vb2_dqbuf(&queue->queue, buf, nonblocking); 293 mutex_unlock(&queue->mutex); 294 295 return ret; 296 } 297 298 int uvc_queue_streamon(struct uvc_video_queue *queue, enum v4l2_buf_type type) 299 { 300 int ret; 301 302 mutex_lock(&queue->mutex); 303 ret = vb2_streamon(&queue->queue, type); 304 mutex_unlock(&queue->mutex); 305 306 return ret; 307 } 308 309 int uvc_queue_streamoff(struct uvc_video_queue *queue, enum v4l2_buf_type type) 310 { 311 int ret; 312 313 mutex_lock(&queue->mutex); 314 ret = vb2_streamoff(&queue->queue, type); 315 mutex_unlock(&queue->mutex); 316 317 return ret; 318 } 319 320 int uvc_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma) 321 { 322 int ret; 323 324 mutex_lock(&queue->mutex); 325 ret = vb2_mmap(&queue->queue, vma); 326 mutex_unlock(&queue->mutex); 327 328 return ret; 329 } 330 331 #ifndef CONFIG_MMU 332 unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue, 333 unsigned long pgoff) 334 { 335 unsigned long ret; 336 337 mutex_lock(&queue->mutex); 338 ret = vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0); 339 mutex_unlock(&queue->mutex); 340 return ret; 341 } 342 #endif 343 344 unsigned int uvc_queue_poll(struct uvc_video_queue *queue, struct file *file, 345 poll_table *wait) 346 { 347 unsigned int ret; 348 349 mutex_lock(&queue->mutex); 350 ret = vb2_poll(&queue->queue, file, wait); 351 mutex_unlock(&queue->mutex); 352 353 return ret; 354 } 355 356 /* ----------------------------------------------------------------------------- 357 * 358 */ 359 360 /* 361 * Check if buffers have been allocated. 362 */ 363 int uvc_queue_allocated(struct uvc_video_queue *queue) 364 { 365 int allocated; 366 367 mutex_lock(&queue->mutex); 368 allocated = vb2_is_busy(&queue->queue); 369 mutex_unlock(&queue->mutex); 370 371 return allocated; 372 } 373 374 /* 375 * Cancel the video buffers queue. 376 * 377 * Cancelling the queue marks all buffers on the irq queue as erroneous, 378 * wakes them up and removes them from the queue. 379 * 380 * If the disconnect parameter is set, further calls to uvc_queue_buffer will 381 * fail with -ENODEV. 382 * 383 * This function acquires the irq spinlock and can be called from interrupt 384 * context. 385 */ 386 void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect) 387 { 388 unsigned long flags; 389 390 spin_lock_irqsave(&queue->irqlock, flags); 391 uvc_queue_return_buffers(queue, UVC_BUF_STATE_ERROR); 392 /* This must be protected by the irqlock spinlock to avoid race 393 * conditions between uvc_buffer_queue and the disconnection event that 394 * could result in an interruptible wait in uvc_dequeue_buffer. Do not 395 * blindly replace this logic by checking for the UVC_QUEUE_DISCONNECTED 396 * state outside the queue code. 397 */ 398 if (disconnect) 399 queue->flags |= UVC_QUEUE_DISCONNECTED; 400 spin_unlock_irqrestore(&queue->irqlock, flags); 401 } 402 403 struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue, 404 struct uvc_buffer *buf) 405 { 406 struct uvc_buffer *nextbuf; 407 unsigned long flags; 408 409 if ((queue->flags & UVC_QUEUE_DROP_CORRUPTED) && buf->error) { 410 buf->error = 0; 411 buf->state = UVC_BUF_STATE_QUEUED; 412 buf->bytesused = 0; 413 vb2_set_plane_payload(&buf->buf, 0, 0); 414 return buf; 415 } 416 417 spin_lock_irqsave(&queue->irqlock, flags); 418 list_del(&buf->queue); 419 if (!list_empty(&queue->irqqueue)) 420 nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer, 421 queue); 422 else 423 nextbuf = NULL; 424 spin_unlock_irqrestore(&queue->irqlock, flags); 425 426 buf->state = buf->error ? VB2_BUF_STATE_ERROR : UVC_BUF_STATE_DONE; 427 vb2_set_plane_payload(&buf->buf, 0, buf->bytesused); 428 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_DONE); 429 430 return nextbuf; 431 } 432