// SPDX-License-Identifier: GPL-2.0+ /* * uvc_queue.c -- USB Video Class driver - Buffers management * * Copyright (C) 2005-2010 * Laurent Pinchart ([email protected]) */ #include <linux/atomic.h> #include <linux/kernel.h> #include <linux/mm.h> #include <linux/list.h> #include <linux/module.h> #include <linux/usb.h> #include <linux/videodev2.h> #include <linux/vmalloc.h> #include <linux/wait.h> #include <media/v4l2-common.h> #include <media/videobuf2-dma-sg.h> #include <media/videobuf2-vmalloc.h> #include "uvc.h" /* ------------------------------------------------------------------------ * Video buffers queue management. * * Video queues is initialized by uvcg_queue_init(). The function performs * basic initialization of the uvc_video_queue struct and never fails. * * Video buffers are managed by videobuf2. The driver uses a mutex to protect * the videobuf2 queue operations by serializing calls to videobuf2 and a * spinlock to protect the IRQ queue that holds the buffers to be processed by * the driver. */ /* ----------------------------------------------------------------------------- * videobuf2 queue operations */ static int uvc_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers, unsigned int *nplanes, unsigned int sizes[], struct device *alloc_devs[]) { … } static int uvc_buffer_prepare(struct vb2_buffer *vb) { … } static void uvc_buffer_queue(struct vb2_buffer *vb) { … } static const struct vb2_ops uvc_queue_qops = …; int uvcg_queue_init(struct uvc_video_queue *queue, struct device *dev, enum v4l2_buf_type type, struct mutex *lock) { … } /* * Free the video buffers. */ void uvcg_free_buffers(struct uvc_video_queue *queue) { … } /* * Allocate the video buffers. */ int uvcg_alloc_buffers(struct uvc_video_queue *queue, struct v4l2_requestbuffers *rb) { … } int uvcg_query_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf) { … } int uvcg_queue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf) { … } /* * Dequeue a video buffer. If nonblocking is false, block until a buffer is * available. */ int uvcg_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf, int nonblocking) { … } /* * Poll the video queue. * * This function implements video queue polling and is intended to be used by * the device poll handler. */ __poll_t uvcg_queue_poll(struct uvc_video_queue *queue, struct file *file, poll_table *wait) { … } int uvcg_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma) { … } #ifndef CONFIG_MMU /* * Get unmapped area. * * NO-MMU arch need this function to make mmap() work correctly. */ unsigned long uvcg_queue_get_unmapped_area(struct uvc_video_queue *queue, unsigned long pgoff) { return vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0); } #endif /* * Cancel the video buffers queue. * * Cancelling the queue marks all buffers on the irq queue as erroneous, * wakes them up and removes them from the queue. * * If the disconnect parameter is set, further calls to uvc_queue_buffer will * fail with -ENODEV. * * This function acquires the irq spinlock and can be called from interrupt * context. */ void uvcg_queue_cancel(struct uvc_video_queue *queue, int disconnect) { … } /* * Enable or disable the video buffers queue. * * The queue must be enabled before starting video acquisition and must be * disabled after stopping it. This ensures that the video buffers queue * state can be properly initialized before buffers are accessed from the * interrupt handler. * * Enabling the video queue initializes parameters (such as sequence number, * sync pattern, ...). If the queue is already enabled, return -EBUSY. * * Disabling the video queue cancels the queue and removes all buffers from * the main queue. * * This function can't be called from interrupt context. Use * uvcg_queue_cancel() instead. */ int uvcg_queue_enable(struct uvc_video_queue *queue, int enable) { … } /* called with &queue_irqlock held.. */ void uvcg_complete_buffer(struct uvc_video_queue *queue, struct uvc_buffer *buf) { … } struct uvc_buffer *uvcg_queue_head(struct uvc_video_queue *queue) { … }