Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
at v3.11 405 lines 11 kB view raw
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#include <linux/atomic.h> 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> 22 23#include <media/videobuf2-vmalloc.h> 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 * 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/* ----------------------------------------------------------------------------- 40 * videobuf2 queue operations 41 */ 42 43static 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[]) 46{ 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; 58} 59 60static int uvc_buffer_prepare(struct vb2_buffer *vb) 61{ 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); 64 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; 69 } 70 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 85static 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); 101 } 102 103 spin_unlock_irqrestore(&queue->irqlock, flags); 104} 105 106static 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 113static 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 120static struct vb2_ops uvc_queue_qops = { 121 .queue_setup = uvc_queue_setup, 122 .buf_prepare = uvc_buffer_prepare, 123 .buf_queue = uvc_buffer_queue, 124 .wait_prepare = uvc_wait_prepare, 125 .wait_finish = uvc_wait_finish, 126}; 127 128static 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 148 return 0; 149} 150 151/* 152 * Free the video buffers. 153 */ 154static void uvc_free_buffers(struct uvc_video_queue *queue) 155{ 156 mutex_lock(&queue->mutex); 157 vb2_queue_release(&queue->queue); 158 mutex_unlock(&queue->mutex); 159} 160 161/* 162 * Allocate the video buffers. 163 */ 164static int uvc_alloc_buffers(struct uvc_video_queue *queue, 165 struct v4l2_requestbuffers *rb) 166{ 167 int ret; 168 169 mutex_lock(&queue->mutex); 170 ret = vb2_reqbufs(&queue->queue, rb); 171 mutex_unlock(&queue->mutex); 172 173 return ret ? ret : rb->count; 174} 175 176static 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 185 return ret; 186} 187 188static int uvc_queue_buffer(struct uvc_video_queue *queue, 189 struct v4l2_buffer *buf) 190{ 191 unsigned long flags; 192 int ret; 193 194 mutex_lock(&queue->mutex); 195 ret = vb2_qbuf(&queue->queue, buf); 196 spin_lock_irqsave(&queue->irqlock, flags); 197 ret = (queue->flags & UVC_QUEUE_PAUSED) != 0; 198 queue->flags &= ~UVC_QUEUE_PAUSED; 199 spin_unlock_irqrestore(&queue->irqlock, flags); 200 mutex_unlock(&queue->mutex); 201 202 return ret; 203} 204 205/* 206 * Dequeue a video buffer. If nonblocking is false, block until a buffer is 207 * available. 208 */ 209static int uvc_dequeue_buffer(struct uvc_video_queue *queue, 210 struct v4l2_buffer *buf, int nonblocking) 211{ 212 int ret; 213 214 mutex_lock(&queue->mutex); 215 ret = vb2_dqbuf(&queue->queue, buf, nonblocking); 216 mutex_unlock(&queue->mutex); 217 218 return ret; 219} 220 221/* 222 * Poll the video queue. 223 * 224 * This function implements video queue polling and is intended to be used by 225 * the device poll handler. 226 */ 227static unsigned int uvc_queue_poll(struct uvc_video_queue *queue, 228 struct file *file, poll_table *wait) 229{ 230 unsigned int ret; 231 232 mutex_lock(&queue->mutex); 233 ret = vb2_poll(&queue->queue, file, wait); 234 mutex_unlock(&queue->mutex); 235 236 return ret; 237} 238 239static int uvc_queue_mmap(struct uvc_video_queue *queue, 240 struct vm_area_struct *vma) 241{ 242 int ret; 243 244 mutex_lock(&queue->mutex); 245 ret = vb2_mmap(&queue->queue, vma); 246 mutex_unlock(&queue->mutex); 247 248 return ret; 249} 250 251#ifndef CONFIG_MMU 252/* 253 * Get unmapped area. 254 * 255 * NO-MMU arch need this function to make mmap() work correctly. 256 */ 257static unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue, 258 unsigned long pgoff) 259{ 260 unsigned long ret; 261 262 mutex_lock(&queue->mutex); 263 ret = vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0); 264 mutex_unlock(&queue->mutex); 265 return ret; 266} 267#endif 268 269/* 270 * Cancel the video buffers queue. 271 * 272 * Cancelling the queue marks all buffers on the irq queue as erroneous, 273 * wakes them up and removes them from the queue. 274 * 275 * If the disconnect parameter is set, further calls to uvc_queue_buffer will 276 * fail with -ENODEV. 277 * 278 * This function acquires the irq spinlock and can be called from interrupt 279 * context. 280 */ 281static void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect) 282{ 283 struct uvc_buffer *buf; 284 unsigned long flags; 285 286 spin_lock_irqsave(&queue->irqlock, flags); 287 while (!list_empty(&queue->irqqueue)) { 288 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer, 289 queue); 290 list_del(&buf->queue); 291 buf->state = UVC_BUF_STATE_ERROR; 292 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR); 293 } 294 /* This must be protected by the irqlock spinlock to avoid race 295 * conditions between uvc_queue_buffer and the disconnection event that 296 * could result in an interruptible wait in uvc_dequeue_buffer. Do not 297 * blindly replace this logic by checking for the UVC_DEV_DISCONNECTED 298 * state outside the queue code. 299 */ 300 if (disconnect) 301 queue->flags |= UVC_QUEUE_DISCONNECTED; 302 spin_unlock_irqrestore(&queue->irqlock, flags); 303} 304 305/* 306 * Enable or disable the video buffers queue. 307 * 308 * The queue must be enabled before starting video acquisition and must be 309 * disabled after stopping it. This ensures that the video buffers queue 310 * state can be properly initialized before buffers are accessed from the 311 * interrupt handler. 312 * 313 * Enabling the video queue initializes parameters (such as sequence number, 314 * sync pattern, ...). If the queue is already enabled, return -EBUSY. 315 * 316 * Disabling the video queue cancels the queue and removes all buffers from 317 * the main queue. 318 * 319 * This function can't be called from interrupt context. Use 320 * uvc_queue_cancel() instead. 321 */ 322static int uvc_queue_enable(struct uvc_video_queue *queue, int enable) 323{ 324 unsigned long flags; 325 int ret = 0; 326 327 mutex_lock(&queue->mutex); 328 if (enable) { 329 ret = vb2_streamon(&queue->queue, queue->queue.type); 330 if (ret < 0) 331 goto done; 332 333 queue->sequence = 0; 334 queue->buf_used = 0; 335 } else { 336 ret = vb2_streamoff(&queue->queue, queue->queue.type); 337 if (ret < 0) 338 goto done; 339 340 spin_lock_irqsave(&queue->irqlock, flags); 341 INIT_LIST_HEAD(&queue->irqqueue); 342 343 /* 344 * FIXME: We need to clear the DISCONNECTED flag to ensure that 345 * applications will be able to queue buffers for the next 346 * streaming run. However, clearing it here doesn't guarantee 347 * that the device will be reconnected in the meantime. 348 */ 349 queue->flags &= ~UVC_QUEUE_DISCONNECTED; 350 spin_unlock_irqrestore(&queue->irqlock, flags); 351 } 352 353done: 354 mutex_unlock(&queue->mutex); 355 return ret; 356} 357 358/* called with &queue_irqlock held.. */ 359static struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue, 360 struct uvc_buffer *buf) 361{ 362 struct uvc_buffer *nextbuf; 363 364 if ((queue->flags & UVC_QUEUE_DROP_INCOMPLETE) && 365 buf->length != buf->bytesused) { 366 buf->state = UVC_BUF_STATE_QUEUED; 367 vb2_set_plane_payload(&buf->buf, 0, 0); 368 return buf; 369 } 370 371 list_del(&buf->queue); 372 if (!list_empty(&queue->irqqueue)) 373 nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer, 374 queue); 375 else 376 nextbuf = NULL; 377 378 /* 379 * FIXME: with videobuf2, the sequence number or timestamp fields 380 * are valid only for video capture devices and the UVC gadget usually 381 * is a video output device. Keeping these until the specs are clear on 382 * this aspect. 383 */ 384 buf->buf.v4l2_buf.sequence = queue->sequence++; 385 do_gettimeofday(&buf->buf.v4l2_buf.timestamp); 386 387 vb2_set_plane_payload(&buf->buf, 0, buf->bytesused); 388 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_DONE); 389 390 return nextbuf; 391} 392 393static struct uvc_buffer *uvc_queue_head(struct uvc_video_queue *queue) 394{ 395 struct uvc_buffer *buf = NULL; 396 397 if (!list_empty(&queue->irqqueue)) 398 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer, 399 queue); 400 else 401 queue->flags |= UVC_QUEUE_PAUSED; 402 403 return buf; 404} 405