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-v4l2.h> 24 #include <media/videobuf2-vmalloc.h> 25 26 #include "uvcvideo.h" 27 28 /* ------------------------------------------------------------------------ 29 * Video buffers queue management. 30 * 31 * Video queues is initialized by uvc_queue_init(). The function performs 32 * basic initialization of the uvc_video_queue struct and never fails. 33 * 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. 38 */ 39 40 static inline struct uvc_streaming * 41 uvc_queue_to_stream(struct uvc_video_queue *queue) 42 { 43 return container_of(queue, struct uvc_streaming, queue); 44 } 45 46 static inline struct uvc_buffer *uvc_vbuf_to_buffer(struct vb2_v4l2_buffer *buf) 47 { 48 return container_of(buf, struct uvc_buffer, buf); 49 } 50 51 /* 52 * Return all queued buffers to videobuf2 in the requested state. 53 * 54 * This function must be called with the queue spinlock held. 55 */ 56 static void uvc_queue_return_buffers(struct uvc_video_queue *queue, 57 enum uvc_buffer_state state) 58 { 59 enum vb2_buffer_state vb2_state = state == UVC_BUF_STATE_ERROR 60 ? VB2_BUF_STATE_ERROR 61 : VB2_BUF_STATE_QUEUED; 62 63 while (!list_empty(&queue->irqqueue)) { 64 struct uvc_buffer *buf = list_first_entry(&queue->irqqueue, 65 struct uvc_buffer, 66 queue); 67 list_del(&buf->queue); 68 buf->state = state; 69 vb2_buffer_done(&buf->buf.vb2_buf, vb2_state); 70 } 71 } 72 73 /* ----------------------------------------------------------------------------- 74 * videobuf2 queue operations 75 */ 76 77 static int uvc_queue_setup(struct vb2_queue *vq, 78 unsigned int *nbuffers, unsigned int *nplanes, 79 unsigned int sizes[], struct device *alloc_devs[]) 80 { 81 struct uvc_video_queue *queue = vb2_get_drv_priv(vq); 82 struct uvc_streaming *stream; 83 unsigned int size; 84 85 switch (vq->type) { 86 case V4L2_BUF_TYPE_META_CAPTURE: 87 size = UVC_METATADA_BUF_SIZE; 88 break; 89 90 default: 91 stream = uvc_queue_to_stream(queue); 92 size = stream->ctrl.dwMaxVideoFrameSize; 93 break; 94 } 95 96 /* 97 * When called with plane sizes, validate them. The driver supports 98 * single planar formats only, and requires buffers to be large enough 99 * to store a complete frame. 100 */ 101 if (*nplanes) 102 return *nplanes != 1 || sizes[0] < size ? -EINVAL : 0; 103 104 *nplanes = 1; 105 sizes[0] = size; 106 return 0; 107 } 108 109 static int uvc_buffer_prepare(struct vb2_buffer *vb) 110 { 111 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 112 struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue); 113 struct uvc_buffer *buf = uvc_vbuf_to_buffer(vbuf); 114 115 if (vb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT && 116 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) { 117 uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n"); 118 return -EINVAL; 119 } 120 121 if (unlikely(queue->flags & UVC_QUEUE_DISCONNECTED)) 122 return -ENODEV; 123 124 buf->state = UVC_BUF_STATE_QUEUED; 125 buf->error = 0; 126 buf->mem = vb2_plane_vaddr(vb, 0); 127 buf->length = vb2_plane_size(vb, 0); 128 if (vb->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) 129 buf->bytesused = 0; 130 else 131 buf->bytesused = vb2_get_plane_payload(vb, 0); 132 133 return 0; 134 } 135 136 static void uvc_buffer_queue(struct vb2_buffer *vb) 137 { 138 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 139 struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue); 140 struct uvc_buffer *buf = uvc_vbuf_to_buffer(vbuf); 141 unsigned long flags; 142 143 spin_lock_irqsave(&queue->irqlock, flags); 144 if (likely(!(queue->flags & UVC_QUEUE_DISCONNECTED))) { 145 kref_init(&buf->ref); 146 list_add_tail(&buf->queue, &queue->irqqueue); 147 } else { 148 /* If the device is disconnected return the buffer to userspace 149 * directly. The next QBUF call will fail with -ENODEV. 150 */ 151 buf->state = UVC_BUF_STATE_ERROR; 152 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR); 153 } 154 155 spin_unlock_irqrestore(&queue->irqlock, flags); 156 } 157 158 static void uvc_buffer_finish(struct vb2_buffer *vb) 159 { 160 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 161 struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue); 162 struct uvc_streaming *stream = uvc_queue_to_stream(queue); 163 struct uvc_buffer *buf = uvc_vbuf_to_buffer(vbuf); 164 165 if (vb->state == VB2_BUF_STATE_DONE) 166 uvc_video_clock_update(stream, vbuf, buf); 167 } 168 169 static int uvc_start_streaming(struct vb2_queue *vq, unsigned int count) 170 { 171 struct uvc_video_queue *queue = vb2_get_drv_priv(vq); 172 struct uvc_streaming *stream = uvc_queue_to_stream(queue); 173 int ret; 174 175 lockdep_assert_irqs_enabled(); 176 177 queue->buf_used = 0; 178 179 ret = uvc_video_start_streaming(stream); 180 if (ret == 0) 181 return 0; 182 183 spin_lock_irq(&queue->irqlock); 184 uvc_queue_return_buffers(queue, UVC_BUF_STATE_QUEUED); 185 spin_unlock_irq(&queue->irqlock); 186 187 return ret; 188 } 189 190 static void uvc_stop_streaming(struct vb2_queue *vq) 191 { 192 struct uvc_video_queue *queue = vb2_get_drv_priv(vq); 193 194 lockdep_assert_irqs_enabled(); 195 196 if (vq->type != V4L2_BUF_TYPE_META_CAPTURE) 197 uvc_video_stop_streaming(uvc_queue_to_stream(queue)); 198 199 spin_lock_irq(&queue->irqlock); 200 uvc_queue_return_buffers(queue, UVC_BUF_STATE_ERROR); 201 spin_unlock_irq(&queue->irqlock); 202 } 203 204 static const struct vb2_ops uvc_queue_qops = { 205 .queue_setup = uvc_queue_setup, 206 .buf_prepare = uvc_buffer_prepare, 207 .buf_queue = uvc_buffer_queue, 208 .buf_finish = uvc_buffer_finish, 209 .wait_prepare = vb2_ops_wait_prepare, 210 .wait_finish = vb2_ops_wait_finish, 211 .start_streaming = uvc_start_streaming, 212 .stop_streaming = uvc_stop_streaming, 213 }; 214 215 static const struct vb2_ops uvc_meta_queue_qops = { 216 .queue_setup = uvc_queue_setup, 217 .buf_prepare = uvc_buffer_prepare, 218 .buf_queue = uvc_buffer_queue, 219 .wait_prepare = vb2_ops_wait_prepare, 220 .wait_finish = vb2_ops_wait_finish, 221 .stop_streaming = uvc_stop_streaming, 222 }; 223 224 int uvc_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type, 225 int drop_corrupted) 226 { 227 int ret; 228 229 queue->queue.type = type; 230 queue->queue.io_modes = VB2_MMAP | VB2_USERPTR; 231 queue->queue.drv_priv = queue; 232 queue->queue.buf_struct_size = sizeof(struct uvc_buffer); 233 queue->queue.mem_ops = &vb2_vmalloc_memops; 234 queue->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC 235 | V4L2_BUF_FLAG_TSTAMP_SRC_SOE; 236 queue->queue.lock = &queue->mutex; 237 238 switch (type) { 239 case V4L2_BUF_TYPE_META_CAPTURE: 240 queue->queue.ops = &uvc_meta_queue_qops; 241 break; 242 default: 243 queue->queue.io_modes |= VB2_DMABUF; 244 queue->queue.ops = &uvc_queue_qops; 245 break; 246 } 247 248 ret = vb2_queue_init(&queue->queue); 249 if (ret) 250 return ret; 251 252 mutex_init(&queue->mutex); 253 spin_lock_init(&queue->irqlock); 254 INIT_LIST_HEAD(&queue->irqqueue); 255 queue->flags = drop_corrupted ? UVC_QUEUE_DROP_CORRUPTED : 0; 256 257 return 0; 258 } 259 260 void uvc_queue_release(struct uvc_video_queue *queue) 261 { 262 mutex_lock(&queue->mutex); 263 vb2_queue_release(&queue->queue); 264 mutex_unlock(&queue->mutex); 265 } 266 267 /* ----------------------------------------------------------------------------- 268 * V4L2 queue operations 269 */ 270 271 int uvc_request_buffers(struct uvc_video_queue *queue, 272 struct v4l2_requestbuffers *rb) 273 { 274 int ret; 275 276 mutex_lock(&queue->mutex); 277 ret = vb2_reqbufs(&queue->queue, rb); 278 mutex_unlock(&queue->mutex); 279 280 return ret ? ret : rb->count; 281 } 282 283 int uvc_query_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf) 284 { 285 int ret; 286 287 mutex_lock(&queue->mutex); 288 ret = vb2_querybuf(&queue->queue, buf); 289 mutex_unlock(&queue->mutex); 290 291 return ret; 292 } 293 294 int uvc_create_buffers(struct uvc_video_queue *queue, 295 struct v4l2_create_buffers *cb) 296 { 297 int ret; 298 299 mutex_lock(&queue->mutex); 300 ret = vb2_create_bufs(&queue->queue, cb); 301 mutex_unlock(&queue->mutex); 302 303 return ret; 304 } 305 306 int uvc_queue_buffer(struct uvc_video_queue *queue, 307 struct media_device *mdev, struct v4l2_buffer *buf) 308 { 309 int ret; 310 311 mutex_lock(&queue->mutex); 312 ret = vb2_qbuf(&queue->queue, mdev, buf); 313 mutex_unlock(&queue->mutex); 314 315 return ret; 316 } 317 318 int uvc_export_buffer(struct uvc_video_queue *queue, 319 struct v4l2_exportbuffer *exp) 320 { 321 int ret; 322 323 mutex_lock(&queue->mutex); 324 ret = vb2_expbuf(&queue->queue, exp); 325 mutex_unlock(&queue->mutex); 326 327 return ret; 328 } 329 330 int uvc_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf, 331 int nonblocking) 332 { 333 int ret; 334 335 mutex_lock(&queue->mutex); 336 ret = vb2_dqbuf(&queue->queue, buf, nonblocking); 337 mutex_unlock(&queue->mutex); 338 339 return ret; 340 } 341 342 int uvc_queue_streamon(struct uvc_video_queue *queue, enum v4l2_buf_type type) 343 { 344 int ret; 345 346 mutex_lock(&queue->mutex); 347 ret = vb2_streamon(&queue->queue, type); 348 mutex_unlock(&queue->mutex); 349 350 return ret; 351 } 352 353 int uvc_queue_streamoff(struct uvc_video_queue *queue, enum v4l2_buf_type type) 354 { 355 int ret; 356 357 mutex_lock(&queue->mutex); 358 ret = vb2_streamoff(&queue->queue, type); 359 mutex_unlock(&queue->mutex); 360 361 return ret; 362 } 363 364 int uvc_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma) 365 { 366 return vb2_mmap(&queue->queue, vma); 367 } 368 369 #ifndef CONFIG_MMU 370 unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue, 371 unsigned long pgoff) 372 { 373 return vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0); 374 } 375 #endif 376 377 __poll_t uvc_queue_poll(struct uvc_video_queue *queue, struct file *file, 378 poll_table *wait) 379 { 380 __poll_t ret; 381 382 mutex_lock(&queue->mutex); 383 ret = vb2_poll(&queue->queue, file, wait); 384 mutex_unlock(&queue->mutex); 385 386 return ret; 387 } 388 389 /* ----------------------------------------------------------------------------- 390 * 391 */ 392 393 /* 394 * Check if buffers have been allocated. 395 */ 396 int uvc_queue_allocated(struct uvc_video_queue *queue) 397 { 398 int allocated; 399 400 mutex_lock(&queue->mutex); 401 allocated = vb2_is_busy(&queue->queue); 402 mutex_unlock(&queue->mutex); 403 404 return allocated; 405 } 406 407 /* 408 * Cancel the video buffers queue. 409 * 410 * Cancelling the queue marks all buffers on the irq queue as erroneous, 411 * wakes them up and removes them from the queue. 412 * 413 * If the disconnect parameter is set, further calls to uvc_queue_buffer will 414 * fail with -ENODEV. 415 * 416 * This function acquires the irq spinlock and can be called from interrupt 417 * context. 418 */ 419 void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect) 420 { 421 unsigned long flags; 422 423 spin_lock_irqsave(&queue->irqlock, flags); 424 uvc_queue_return_buffers(queue, UVC_BUF_STATE_ERROR); 425 /* This must be protected by the irqlock spinlock to avoid race 426 * conditions between uvc_buffer_queue and the disconnection event that 427 * could result in an interruptible wait in uvc_dequeue_buffer. Do not 428 * blindly replace this logic by checking for the UVC_QUEUE_DISCONNECTED 429 * state outside the queue code. 430 */ 431 if (disconnect) 432 queue->flags |= UVC_QUEUE_DISCONNECTED; 433 spin_unlock_irqrestore(&queue->irqlock, flags); 434 } 435 436 /* 437 * uvc_queue_get_current_buffer: Obtain the current working output buffer 438 * 439 * Buffers may span multiple packets, and even URBs, therefore the active buffer 440 * remains on the queue until the EOF marker. 441 */ 442 static struct uvc_buffer * 443 __uvc_queue_get_current_buffer(struct uvc_video_queue *queue) 444 { 445 if (list_empty(&queue->irqqueue)) 446 return NULL; 447 448 return list_first_entry(&queue->irqqueue, struct uvc_buffer, queue); 449 } 450 451 struct uvc_buffer *uvc_queue_get_current_buffer(struct uvc_video_queue *queue) 452 { 453 struct uvc_buffer *nextbuf; 454 unsigned long flags; 455 456 spin_lock_irqsave(&queue->irqlock, flags); 457 nextbuf = __uvc_queue_get_current_buffer(queue); 458 spin_unlock_irqrestore(&queue->irqlock, flags); 459 460 return nextbuf; 461 } 462 463 /* 464 * uvc_queue_buffer_requeue: Requeue a buffer on our internal irqqueue 465 * 466 * Reuse a buffer through our internal queue without the need to 'prepare'. 467 * The buffer will be returned to userspace through the uvc_buffer_queue call if 468 * the device has been disconnected. 469 */ 470 static void uvc_queue_buffer_requeue(struct uvc_video_queue *queue, 471 struct uvc_buffer *buf) 472 { 473 buf->error = 0; 474 buf->state = UVC_BUF_STATE_QUEUED; 475 buf->bytesused = 0; 476 vb2_set_plane_payload(&buf->buf.vb2_buf, 0, 0); 477 478 uvc_buffer_queue(&buf->buf.vb2_buf); 479 } 480 481 static void uvc_queue_buffer_complete(struct kref *ref) 482 { 483 struct uvc_buffer *buf = container_of(ref, struct uvc_buffer, ref); 484 struct vb2_buffer *vb = &buf->buf.vb2_buf; 485 struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue); 486 487 if ((queue->flags & UVC_QUEUE_DROP_CORRUPTED) && buf->error) { 488 uvc_queue_buffer_requeue(queue, buf); 489 return; 490 } 491 492 buf->state = buf->error ? UVC_BUF_STATE_ERROR : UVC_BUF_STATE_DONE; 493 vb2_set_plane_payload(&buf->buf.vb2_buf, 0, buf->bytesused); 494 vb2_buffer_done(&buf->buf.vb2_buf, VB2_BUF_STATE_DONE); 495 } 496 497 /* 498 * Release a reference on the buffer. Complete the buffer when the last 499 * reference is released. 500 */ 501 void uvc_queue_buffer_release(struct uvc_buffer *buf) 502 { 503 kref_put(&buf->ref, uvc_queue_buffer_complete); 504 } 505 506 /* 507 * Remove this buffer from the queue. Lifetime will persist while async actions 508 * are still running (if any), and uvc_queue_buffer_release will give the buffer 509 * back to VB2 when all users have completed. 510 */ 511 struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue, 512 struct uvc_buffer *buf) 513 { 514 struct uvc_buffer *nextbuf; 515 unsigned long flags; 516 517 spin_lock_irqsave(&queue->irqlock, flags); 518 list_del(&buf->queue); 519 nextbuf = __uvc_queue_get_current_buffer(queue); 520 spin_unlock_irqrestore(&queue->irqlock, flags); 521 522 uvc_queue_buffer_release(buf); 523 524 return nextbuf; 525 } 526