Commit d6925225 authored by Bhupesh Sharma's avatar Bhupesh Sharma Committed by Felipe Balbi

usb: gadget/uvc: Port UVC webcam gadget to use videobuf2 framework

This patch reworks the videobuffer management logic present in the UVC
webcam gadget and ports it to use the "more apt" videobuf2 framework for
video buffer management.

To support routing video data captured from a real V4L2 video capture
device with a "zero copy" operation on videobuffers (as they pass from
the V4L2 domain to UVC domain via a user-space application), we need to
support USER_PTR IO method at the UVC gadget side.

So the V4L2 capture device driver can still continue to use MMAP IO
method and now the user-space application can just pass a pointer to the
video buffers being dequeued from the V4L2 device side while queueing
them at the UVC gadget end. This ensures that we have a "zero-copy"
design as the videobuffers pass from the V4L2 capture device to the UVC
gadget.

Note that there will still be a need to apply UVC specific payload
headers on top of each UVC payload data, which will still require a copy
operation to be performed in the 'encode' routines of the UVC gadget.

This patch also addresses one issue found out while porting the UVC
gadget to videobuf2 framework:
	- In case the usb requests queued by the gadget get completed
	  with a status of -ESHUTDOWN (disconnected from host),
	  the queue of videobuf2 should be cancelled to ensure that the
	  application space daemon is not left in a state waiting for
	  a vb2 to be successfully absorbed at the USB side.
Signed-off-by: default avatarBhupesh Sharma <bhupesh.sharma@st.com>
Signed-off-by: default avatarLaurent Pinchart <laurent.pinchart@ideasonboard.com>
parent 225da3e3
...@@ -949,6 +949,7 @@ config USB_G_WEBCAM ...@@ -949,6 +949,7 @@ config USB_G_WEBCAM
tristate "USB Webcam Gadget" tristate "USB Webcam Gadget"
depends on VIDEO_DEV depends on VIDEO_DEV
select USB_LIBCOMPOSITE select USB_LIBCOMPOSITE
select VIDEOBUF2_VMALLOC
help help
The Webcam Gadget acts as a composite USB Audio and Video Class The Webcam Gadget acts as a composite USB Audio and Video Class
device. It provides a userspace API to process UVC control requests device. It provides a userspace API to process UVC control requests
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
* (at your option) any later version. * (at your option) any later version.
*/ */
#include <linux/atomic.h>
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/mm.h> #include <linux/mm.h>
#include <linux/list.h> #include <linux/list.h>
...@@ -18,7 +19,8 @@ ...@@ -18,7 +19,8 @@
#include <linux/videodev2.h> #include <linux/videodev2.h>
#include <linux/vmalloc.h> #include <linux/vmalloc.h>
#include <linux/wait.h> #include <linux/wait.h>
#include <linux/atomic.h>
#include <media/videobuf2-vmalloc.h>
#include "uvc.h" #include "uvc.h"
...@@ -28,330 +30,175 @@ ...@@ -28,330 +30,175 @@
* Video queues is initialized by uvc_queue_init(). The function performs * Video queues is initialized by uvc_queue_init(). The function performs
* basic initialization of the uvc_video_queue struct and never fails. * basic initialization of the uvc_video_queue struct and never fails.
* *
* Video buffer allocation and freeing are performed by uvc_alloc_buffers and * Video buffers are managed by videobuf2. The driver uses a mutex to protect
* uvc_free_buffers respectively. The former acquires the video queue lock, * the videobuf2 queue operations by serializing calls to videobuf2 and a
* while the later must be called with the lock held (so that allocation can * spinlock to protect the IRQ queue that holds the buffers to be processed by
* free previously allocated buffers). Trying to free buffers that are mapped * the driver.
* to user space will return -EBUSY.
*
* Video buffers are managed using two queues. However, unlike most USB video
* drivers that use an in queue and an out queue, we use a main queue to hold
* all queued buffers (both 'empty' and 'done' buffers), and an irq queue to
* hold empty buffers. This design (copied from video-buf) minimizes locking
* in interrupt, as only one queue is shared between interrupt and user
* contexts.
*
* Use cases
* ---------
*
* Unless stated otherwise, all operations that modify the irq buffers queue
* are protected by the irq spinlock.
*
* 1. The user queues the buffers, starts streaming and dequeues a buffer.
*
* The buffers are added to the main and irq queues. Both operations are
* protected by the queue lock, and the later is protected by the irq
* spinlock as well.
*
* The completion handler fetches a buffer from the irq queue and fills it
* with video data. If no buffer is available (irq queue empty), the handler
* returns immediately.
*
* When the buffer is full, the completion handler removes it from the irq
* queue, marks it as ready (UVC_BUF_STATE_DONE) and wakes its wait queue.
* At that point, any process waiting on the buffer will be woken up. If a
* process tries to dequeue a buffer after it has been marked ready, the
* dequeing will succeed immediately.
*
* 2. Buffers are queued, user is waiting on a buffer and the device gets
* disconnected.
*
* When the device is disconnected, the kernel calls the completion handler
* with an appropriate status code. The handler marks all buffers in the
* irq queue as being erroneous (UVC_BUF_STATE_ERROR) and wakes them up so
* that any process waiting on a buffer gets woken up.
*
* Waking up up the first buffer on the irq list is not enough, as the
* process waiting on the buffer might restart the dequeue operation
* immediately.
*
*/ */
static void /* -----------------------------------------------------------------------------
uvc_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type) * videobuf2 queue operations
{
mutex_init(&queue->mutex);
spin_lock_init(&queue->irqlock);
INIT_LIST_HEAD(&queue->mainqueue);
INIT_LIST_HEAD(&queue->irqqueue);
queue->type = type;
}
/*
* Free the video buffers.
*
* This function must be called with the queue lock held.
*/ */
static int uvc_free_buffers(struct uvc_video_queue *queue)
static int uvc_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
unsigned int *nbuffers, unsigned int *nplanes,
unsigned int sizes[], void *alloc_ctxs[])
{ {
unsigned int i; struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
struct uvc_video *video = container_of(queue, struct uvc_video, queue);
for (i = 0; i < queue->count; ++i) { if (*nbuffers > UVC_MAX_VIDEO_BUFFERS)
if (queue->buffer[i].vma_use_count != 0) *nbuffers = UVC_MAX_VIDEO_BUFFERS;
return -EBUSY;
}
if (queue->count) { *nplanes = 1;
vfree(queue->mem);
queue->count = 0; sizes[0] = video->imagesize;
}
return 0; return 0;
} }
/* static int uvc_buffer_prepare(struct vb2_buffer *vb)
* Allocate the video buffers.
*
* Pages are reserved to make sure they will not be swapped, as they will be
* filled in the URB completion handler.
*
* Buffers will be individually mapped, so they must all be page aligned.
*/
static int
uvc_alloc_buffers(struct uvc_video_queue *queue, unsigned int nbuffers,
unsigned int buflength)
{ {
unsigned int bufsize = PAGE_ALIGN(buflength); struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
unsigned int i; struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf);
void *mem = NULL;
int ret;
if (nbuffers > UVC_MAX_VIDEO_BUFFERS) if (vb->v4l2_buf.type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
nbuffers = UVC_MAX_VIDEO_BUFFERS; vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) {
uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n");
return -EINVAL;
}
mutex_lock(&queue->mutex); if (unlikely(queue->flags & UVC_QUEUE_DISCONNECTED))
return -ENODEV;
if ((ret = uvc_free_buffers(queue)) < 0) buf->state = UVC_BUF_STATE_QUEUED;
goto done; buf->mem = vb2_plane_vaddr(vb, 0);
buf->length = vb2_plane_size(vb, 0);
if (vb->v4l2_buf.type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
buf->bytesused = 0;
else
buf->bytesused = vb2_get_plane_payload(vb, 0);
/* Bail out if no buffers should be allocated. */ return 0;
if (nbuffers == 0) }
goto done;
/* Decrement the number of buffers until allocation succeeds. */ static void uvc_buffer_queue(struct vb2_buffer *vb)
for (; nbuffers > 0; --nbuffers) { {
mem = vmalloc_32(nbuffers * bufsize); struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
if (mem != NULL) struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf);
break; unsigned long flags;
}
if (mem == NULL) { spin_lock_irqsave(&queue->irqlock, flags);
ret = -ENOMEM;
goto done;
}
for (i = 0; i < nbuffers; ++i) { if (likely(!(queue->flags & UVC_QUEUE_DISCONNECTED))) {
memset(&queue->buffer[i], 0, sizeof queue->buffer[i]); list_add_tail(&buf->queue, &queue->irqqueue);
queue->buffer[i].buf.index = i; } else {
queue->buffer[i].buf.m.offset = i * bufsize; /* If the device is disconnected return the buffer to userspace
queue->buffer[i].buf.length = buflength; * directly. The next QBUF call will fail with -ENODEV.
queue->buffer[i].buf.type = queue->type; */
queue->buffer[i].buf.sequence = 0; buf->state = UVC_BUF_STATE_ERROR;
queue->buffer[i].buf.field = V4L2_FIELD_NONE; vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR);
queue->buffer[i].buf.memory = V4L2_MEMORY_MMAP;
queue->buffer[i].buf.flags = 0;
init_waitqueue_head(&queue->buffer[i].wait);
} }
queue->mem = mem; spin_unlock_irqrestore(&queue->irqlock, flags);
queue->count = nbuffers;
queue->buf_size = bufsize;
ret = nbuffers;
done:
mutex_unlock(&queue->mutex);
return ret;
} }
static void __uvc_query_buffer(struct uvc_buffer *buf, static struct vb2_ops uvc_queue_qops = {
struct v4l2_buffer *v4l2_buf) .queue_setup = uvc_queue_setup,
{ .buf_prepare = uvc_buffer_prepare,
memcpy(v4l2_buf, &buf->buf, sizeof *v4l2_buf); .buf_queue = uvc_buffer_queue,
};
if (buf->vma_use_count)
v4l2_buf->flags |= V4L2_BUF_FLAG_MAPPED;
switch (buf->state) {
case UVC_BUF_STATE_ERROR:
case UVC_BUF_STATE_DONE:
v4l2_buf->flags |= V4L2_BUF_FLAG_DONE;
break;
case UVC_BUF_STATE_QUEUED:
case UVC_BUF_STATE_ACTIVE:
v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
break;
case UVC_BUF_STATE_IDLE:
default:
break;
}
}
static int static int uvc_queue_init(struct uvc_video_queue *queue,
uvc_query_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *v4l2_buf) enum v4l2_buf_type type)
{ {
int ret = 0; int ret;
mutex_lock(&queue->mutex); queue->queue.type = type;
if (v4l2_buf->index >= queue->count) { queue->queue.io_modes = VB2_MMAP | VB2_USERPTR;
ret = -EINVAL; queue->queue.drv_priv = queue;
goto done; queue->queue.buf_struct_size = sizeof(struct uvc_buffer);
} queue->queue.ops = &uvc_queue_qops;
queue->queue.mem_ops = &vb2_vmalloc_memops;
ret = vb2_queue_init(&queue->queue);
if (ret)
return ret;
__uvc_query_buffer(&queue->buffer[v4l2_buf->index], v4l2_buf); mutex_init(&queue->mutex);
spin_lock_init(&queue->irqlock);
INIT_LIST_HEAD(&queue->irqqueue);
queue->flags = 0;
done: return 0;
}
/*
* Free the video buffers.
*/
static void uvc_free_buffers(struct uvc_video_queue *queue)
{
mutex_lock(&queue->mutex);
vb2_queue_release(&queue->queue);
mutex_unlock(&queue->mutex); mutex_unlock(&queue->mutex);
return ret;
} }
/* /*
* Queue a video buffer. Attempting to queue a buffer that has already been * Allocate the video buffers.
* queued will return -EINVAL.
*/ */
static int static int uvc_alloc_buffers(struct uvc_video_queue *queue,
uvc_queue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *v4l2_buf) struct v4l2_requestbuffers *rb)
{ {
struct uvc_buffer *buf; int ret;
unsigned long flags;
int ret = 0;
uvc_trace(UVC_TRACE_CAPTURE, "Queuing buffer %u.\n", v4l2_buf->index); mutex_lock(&queue->mutex);
ret = vb2_reqbufs(&queue->queue, rb);
mutex_unlock(&queue->mutex);
if (v4l2_buf->type != queue->type || return ret ? ret : rb->count;
v4l2_buf->memory != V4L2_MEMORY_MMAP) { }
uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer type (%u) "
"and/or memory (%u).\n", v4l2_buf->type,
v4l2_buf->memory);
return -EINVAL;
}
mutex_lock(&queue->mutex); static int uvc_query_buffer(struct uvc_video_queue *queue,
if (v4l2_buf->index >= queue->count) { struct v4l2_buffer *buf)
uvc_trace(UVC_TRACE_CAPTURE, "[E] Out of range index.\n"); {
ret = -EINVAL; int ret;
goto done;
}
buf = &queue->buffer[v4l2_buf->index]; mutex_lock(&queue->mutex);
if (buf->state != UVC_BUF_STATE_IDLE) { ret = vb2_querybuf(&queue->queue, buf);
uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer state " mutex_unlock(&queue->mutex);
"(%u).\n", buf->state);
ret = -EINVAL;
goto done;
}
if (v4l2_buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT && return ret;
v4l2_buf->bytesused > buf->buf.length) { }
uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n");
ret = -EINVAL;
goto done;
}
if (v4l2_buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) static int uvc_queue_buffer(struct uvc_video_queue *queue,
buf->buf.bytesused = 0; struct v4l2_buffer *buf)
else {
buf->buf.bytesused = v4l2_buf->bytesused; unsigned long flags;
int ret;
mutex_lock(&queue->mutex);
ret = vb2_qbuf(&queue->queue, buf);
spin_lock_irqsave(&queue->irqlock, flags); spin_lock_irqsave(&queue->irqlock, flags);
if (queue->flags & UVC_QUEUE_DISCONNECTED) {
spin_unlock_irqrestore(&queue->irqlock, flags);
ret = -ENODEV;
goto done;
}
buf->state = UVC_BUF_STATE_QUEUED;
ret = (queue->flags & UVC_QUEUE_PAUSED) != 0; ret = (queue->flags & UVC_QUEUE_PAUSED) != 0;
queue->flags &= ~UVC_QUEUE_PAUSED; queue->flags &= ~UVC_QUEUE_PAUSED;
list_add_tail(&buf->stream, &queue->mainqueue);
list_add_tail(&buf->queue, &queue->irqqueue);
spin_unlock_irqrestore(&queue->irqlock, flags); spin_unlock_irqrestore(&queue->irqlock, flags);
done:
mutex_unlock(&queue->mutex); mutex_unlock(&queue->mutex);
return ret;
}
static int uvc_queue_waiton(struct uvc_buffer *buf, int nonblocking)
{
if (nonblocking) {
return (buf->state != UVC_BUF_STATE_QUEUED &&
buf->state != UVC_BUF_STATE_ACTIVE)
? 0 : -EAGAIN;
}
return wait_event_interruptible(buf->wait, return ret;
buf->state != UVC_BUF_STATE_QUEUED &&
buf->state != UVC_BUF_STATE_ACTIVE);
} }
/* /*
* Dequeue a video buffer. If nonblocking is false, block until a buffer is * Dequeue a video buffer. If nonblocking is false, block until a buffer is
* available. * available.
*/ */
static int static int uvc_dequeue_buffer(struct uvc_video_queue *queue,
uvc_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *v4l2_buf, struct v4l2_buffer *buf, int nonblocking)
int nonblocking)
{ {
struct uvc_buffer *buf; int ret;
int ret = 0;
if (v4l2_buf->type != queue->type ||
v4l2_buf->memory != V4L2_MEMORY_MMAP) {
uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer type (%u) "
"and/or memory (%u).\n", v4l2_buf->type,
v4l2_buf->memory);
return -EINVAL;
}
mutex_lock(&queue->mutex); mutex_lock(&queue->mutex);
if (list_empty(&queue->mainqueue)) { ret = vb2_dqbuf(&queue->queue, buf, nonblocking);
uvc_trace(UVC_TRACE_CAPTURE, "[E] Empty buffer queue.\n");
ret = -EINVAL;
goto done;
}
buf = list_first_entry(&queue->mainqueue, struct uvc_buffer, stream);
if ((ret = uvc_queue_waiton(buf, nonblocking)) < 0)
goto done;
uvc_trace(UVC_TRACE_CAPTURE, "Dequeuing buffer %u (%u, %u bytes).\n",
buf->buf.index, buf->state, buf->buf.bytesused);
switch (buf->state) {
case UVC_BUF_STATE_ERROR:
uvc_trace(UVC_TRACE_CAPTURE, "[W] Corrupted data "
"(transmission error).\n");
ret = -EIO;
case UVC_BUF_STATE_DONE:
buf->state = UVC_BUF_STATE_IDLE;
break;
case UVC_BUF_STATE_IDLE:
case UVC_BUF_STATE_QUEUED:
case UVC_BUF_STATE_ACTIVE:
default:
uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer state %u "
"(driver bug?).\n", buf->state);
ret = -EINVAL;
goto done;
}
list_del(&buf->stream);
__uvc_query_buffer(buf, v4l2_buf);
done:
mutex_unlock(&queue->mutex); mutex_unlock(&queue->mutex);
return ret; return ret;
} }
...@@ -361,103 +208,27 @@ uvc_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *v4l2_buf, ...@@ -361,103 +208,27 @@ uvc_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *v4l2_buf,
* This function implements video queue polling and is intended to be used by * This function implements video queue polling and is intended to be used by
* the device poll handler. * the device poll handler.
*/ */
static unsigned int static unsigned int uvc_queue_poll(struct uvc_video_queue *queue,
uvc_queue_poll(struct uvc_video_queue *queue, struct file *file, struct file *file, poll_table *wait)
poll_table *wait)
{ {
struct uvc_buffer *buf; unsigned int ret;
unsigned int mask = 0;
mutex_lock(&queue->mutex); mutex_lock(&queue->mutex);
if (list_empty(&queue->mainqueue)) ret = vb2_poll(&queue->queue, file, wait);
goto done;
buf = list_first_entry(&queue->mainqueue, struct uvc_buffer, stream);
poll_wait(file, &buf->wait, wait);
if (buf->state == UVC_BUF_STATE_DONE ||
buf->state == UVC_BUF_STATE_ERROR)
mask |= POLLOUT | POLLWRNORM;
done:
mutex_unlock(&queue->mutex); mutex_unlock(&queue->mutex);
return mask;
}
/*
* VMA operations.
*/
static void uvc_vm_open(struct vm_area_struct *vma)
{
struct uvc_buffer *buffer = vma->vm_private_data;
buffer->vma_use_count++;
}
static void uvc_vm_close(struct vm_area_struct *vma) return ret;
{
struct uvc_buffer *buffer = vma->vm_private_data;
buffer->vma_use_count--;
} }
static struct vm_operations_struct uvc_vm_ops = { static int uvc_queue_mmap(struct uvc_video_queue *queue,
.open = uvc_vm_open, struct vm_area_struct *vma)
.close = uvc_vm_close,
};
/*
* Memory-map a buffer.
*
* This function implements video buffer memory mapping and is intended to be
* used by the device mmap handler.
*/
static int
uvc_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma)
{ {
struct uvc_buffer *uninitialized_var(buffer); int ret;
struct page *page;
unsigned long addr, start, size;
unsigned int i;
int ret = 0;
start = vma->vm_start;
size = vma->vm_end - vma->vm_start;
mutex_lock(&queue->mutex); mutex_lock(&queue->mutex);
ret = vb2_mmap(&queue->queue, vma);
for (i = 0; i < queue->count; ++i) {
buffer = &queue->buffer[i];
if ((buffer->buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
break;
}
if (i == queue->count || size != queue->buf_size) {
ret = -EINVAL;
goto done;
}
/*
* VM_IO marks the area as being an mmaped region for I/O to a
* device. It also prevents the region from being core dumped.
*/
vma->vm_flags |= VM_IO;
addr = (unsigned long)queue->mem + buffer->buf.m.offset;
while (size > 0) {
page = vmalloc_to_page((void *)addr);
if ((ret = vm_insert_page(vma, start, page)) < 0)
goto done;
start += PAGE_SIZE;
addr += PAGE_SIZE;
size -= PAGE_SIZE;
}
vma->vm_ops = &uvc_vm_ops;
vma->vm_private_data = buffer;
uvc_vm_open(vma);
done:
mutex_unlock(&queue->mutex); mutex_unlock(&queue->mutex);
return ret; return ret;
} }
...@@ -484,7 +255,7 @@ static void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect) ...@@ -484,7 +255,7 @@ static void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
queue); queue);
list_del(&buf->queue); list_del(&buf->queue);
buf->state = UVC_BUF_STATE_ERROR; buf->state = UVC_BUF_STATE_ERROR;
wake_up(&buf->wait); vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR);
} }
/* This must be protected by the irqlock spinlock to avoid race /* This must be protected by the irqlock spinlock to avoid race
* conditions between uvc_queue_buffer and the disconnection event that * conditions between uvc_queue_buffer and the disconnection event that
...@@ -516,26 +287,33 @@ static void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect) ...@@ -516,26 +287,33 @@ static void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
*/ */
static int uvc_queue_enable(struct uvc_video_queue *queue, int enable) static int uvc_queue_enable(struct uvc_video_queue *queue, int enable)
{ {
unsigned int i; unsigned long flags;
int ret = 0; int ret = 0;
mutex_lock(&queue->mutex); mutex_lock(&queue->mutex);
if (enable) { if (enable) {
if (uvc_queue_streaming(queue)) { ret = vb2_streamon(&queue->queue, queue->queue.type);
ret = -EBUSY; if (ret < 0)
goto done; goto done;
}
queue->sequence = 0; queue->sequence = 0;
queue->flags |= UVC_QUEUE_STREAMING;
queue->buf_used = 0; queue->buf_used = 0;
} else { } else {
uvc_queue_cancel(queue, 0); ret = vb2_streamoff(&queue->queue, queue->queue.type);
INIT_LIST_HEAD(&queue->mainqueue); if (ret < 0)
goto done;
for (i = 0; i < queue->count; ++i) spin_lock_irqsave(&queue->irqlock, flags);
queue->buffer[i].state = UVC_BUF_STATE_IDLE; INIT_LIST_HEAD(&queue->irqqueue);
queue->flags &= ~UVC_QUEUE_STREAMING; /*
* FIXME: We need to clear the DISCONNECTED flag to ensure that
* applications will be able to queue buffers for the next
* streaming run. However, clearing it here doesn't guarantee
* that the device will be reconnected in the meantime.
*/
queue->flags &= ~UVC_QUEUE_DISCONNECTED;
spin_unlock_irqrestore(&queue->irqlock, flags);
} }
done: done:
...@@ -544,15 +322,15 @@ static int uvc_queue_enable(struct uvc_video_queue *queue, int enable) ...@@ -544,15 +322,15 @@ static int uvc_queue_enable(struct uvc_video_queue *queue, int enable)
} }
/* called with &queue_irqlock held.. */ /* called with &queue_irqlock held.. */
static struct uvc_buffer * static struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
uvc_queue_next_buffer(struct uvc_video_queue *queue, struct uvc_buffer *buf) struct uvc_buffer *buf)
{ {
struct uvc_buffer *nextbuf; struct uvc_buffer *nextbuf;
if ((queue->flags & UVC_QUEUE_DROP_INCOMPLETE) && if ((queue->flags & UVC_QUEUE_DROP_INCOMPLETE) &&
buf->buf.length != buf->buf.bytesused) { buf->length != buf->bytesused) {
buf->state = UVC_BUF_STATE_QUEUED; buf->state = UVC_BUF_STATE_QUEUED;
buf->buf.bytesused = 0; vb2_set_plane_payload(&buf->buf, 0, 0);
return buf; return buf;
} }
...@@ -563,10 +341,18 @@ uvc_queue_next_buffer(struct uvc_video_queue *queue, struct uvc_buffer *buf) ...@@ -563,10 +341,18 @@ uvc_queue_next_buffer(struct uvc_video_queue *queue, struct uvc_buffer *buf)
else else
nextbuf = NULL; nextbuf = NULL;
buf->buf.sequence = queue->sequence++; /*
do_gettimeofday(&buf->buf.timestamp); * FIXME: with videobuf2, the sequence number or timestamp fields
* are valid only for video capture devices and the UVC gadget usually
* is a video output device. Keeping these until the specs are clear on
* this aspect.
*/
buf->buf.v4l2_buf.sequence = queue->sequence++;
do_gettimeofday(&buf->buf.v4l2_buf.timestamp);
vb2_set_plane_payload(&buf->buf, 0, buf->bytesused);
vb2_buffer_done(&buf->buf, VB2_BUF_STATE_DONE);
wake_up(&buf->wait);
return nextbuf; return nextbuf;
} }
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/poll.h> #include <linux/poll.h>
#include <linux/videodev2.h> #include <linux/videodev2.h>
#include <media/videobuf2-core.h>
/* Maximum frame size in bytes, for sanity checking. */ /* Maximum frame size in bytes, for sanity checking. */
#define UVC_MAX_FRAME_SIZE (16*1024*1024) #define UVC_MAX_FRAME_SIZE (16*1024*1024)
...@@ -25,42 +26,35 @@ enum uvc_buffer_state { ...@@ -25,42 +26,35 @@ enum uvc_buffer_state {
}; };
struct uvc_buffer { struct uvc_buffer {
unsigned long vma_use_count; struct vb2_buffer buf;
struct list_head stream;
/* Touched by interrupt handler. */
struct v4l2_buffer buf;
struct list_head queue; struct list_head queue;
wait_queue_head_t wait;
enum uvc_buffer_state state; enum uvc_buffer_state state;
void *mem;
unsigned int length;
unsigned int bytesused;
}; };
#define UVC_QUEUE_STREAMING (1 << 0) #define UVC_QUEUE_DISCONNECTED (1 << 0)
#define UVC_QUEUE_DISCONNECTED (1 << 1) #define UVC_QUEUE_DROP_INCOMPLETE (1 << 1)
#define UVC_QUEUE_DROP_INCOMPLETE (1 << 2) #define UVC_QUEUE_PAUSED (1 << 2)
#define UVC_QUEUE_PAUSED (1 << 3)
struct uvc_video_queue { struct uvc_video_queue {
enum v4l2_buf_type type; struct vb2_queue queue;
struct mutex mutex; /* Protects queue */
void *mem;
unsigned int flags; unsigned int flags;
__u32 sequence; __u32 sequence;
unsigned int count;
unsigned int buf_size;
unsigned int buf_used; unsigned int buf_used;
struct uvc_buffer buffer[UVC_MAX_VIDEO_BUFFERS];
struct mutex mutex; /* protects buffers and mainqueue */
spinlock_t irqlock; /* protects irqqueue */
struct list_head mainqueue; spinlock_t irqlock; /* Protects flags and irqqueue */
struct list_head irqqueue; struct list_head irqqueue;
}; };
static inline int uvc_queue_streaming(struct uvc_video_queue *queue) static inline int uvc_queue_streaming(struct uvc_video_queue *queue)
{ {
return queue->flags & UVC_QUEUE_STREAMING; return vb2_is_streaming(&queue->queue);
} }
#endif /* __KERNEL__ */ #endif /* __KERNEL__ */
......
...@@ -147,16 +147,13 @@ uvc_v4l2_release(struct file *file) ...@@ -147,16 +147,13 @@ uvc_v4l2_release(struct file *file)
uvc_function_disconnect(uvc); uvc_function_disconnect(uvc);
uvc_video_enable(video, 0); uvc_video_enable(video, 0);
mutex_lock(&video->queue.mutex); uvc_free_buffers(&video->queue);
if (uvc_free_buffers(&video->queue) < 0)
printk(KERN_ERR "uvc_v4l2_release: Unable to free "
"buffers.\n");
mutex_unlock(&video->queue.mutex);
file->private_data = NULL; file->private_data = NULL;
v4l2_fh_del(&handle->vfh); v4l2_fh_del(&handle->vfh);
v4l2_fh_exit(&handle->vfh); v4l2_fh_exit(&handle->vfh);
kfree(handle); kfree(handle);
return 0; return 0;
} }
...@@ -191,7 +188,7 @@ uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg) ...@@ -191,7 +188,7 @@ uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
{ {
struct v4l2_format *fmt = arg; struct v4l2_format *fmt = arg;
if (fmt->type != video->queue.type) if (fmt->type != video->queue.queue.type)
return -EINVAL; return -EINVAL;
return uvc_v4l2_get_format(video, fmt); return uvc_v4l2_get_format(video, fmt);
...@@ -201,7 +198,7 @@ uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg) ...@@ -201,7 +198,7 @@ uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
{ {
struct v4l2_format *fmt = arg; struct v4l2_format *fmt = arg;
if (fmt->type != video->queue.type) if (fmt->type != video->queue.queue.type)
return -EINVAL; return -EINVAL;
return uvc_v4l2_set_format(video, fmt); return uvc_v4l2_set_format(video, fmt);
...@@ -212,16 +209,13 @@ uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg) ...@@ -212,16 +209,13 @@ uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
{ {
struct v4l2_requestbuffers *rb = arg; struct v4l2_requestbuffers *rb = arg;
if (rb->type != video->queue.type || if (rb->type != video->queue.queue.type)
rb->memory != V4L2_MEMORY_MMAP)
return -EINVAL; return -EINVAL;
ret = uvc_alloc_buffers(&video->queue, rb->count, ret = uvc_alloc_buffers(&video->queue, rb);
video->imagesize);
if (ret < 0) if (ret < 0)
return ret; return ret;
rb->count = ret;
ret = 0; ret = 0;
break; break;
} }
...@@ -230,9 +224,6 @@ uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg) ...@@ -230,9 +224,6 @@ uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
{ {
struct v4l2_buffer *buf = arg; struct v4l2_buffer *buf = arg;
if (buf->type != video->queue.type)
return -EINVAL;
return uvc_query_buffer(&video->queue, buf); return uvc_query_buffer(&video->queue, buf);
} }
...@@ -250,7 +241,7 @@ uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg) ...@@ -250,7 +241,7 @@ uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
{ {
int *type = arg; int *type = arg;
if (*type != video->queue.type) if (*type != video->queue.queue.type)
return -EINVAL; return -EINVAL;
/* Enable UVC video. */ /* Enable UVC video. */
...@@ -272,7 +263,7 @@ uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg) ...@@ -272,7 +263,7 @@ uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
{ {
int *type = arg; int *type = arg;
if (*type != video->queue.type) if (*type != video->queue.queue.type)
return -EINVAL; return -EINVAL;
return uvc_video_enable(video, 0); return uvc_video_enable(video, 0);
...@@ -344,16 +335,8 @@ uvc_v4l2_poll(struct file *file, poll_table *wait) ...@@ -344,16 +335,8 @@ uvc_v4l2_poll(struct file *file, poll_table *wait)
{ {
struct video_device *vdev = video_devdata(file); struct video_device *vdev = video_devdata(file);
struct uvc_device *uvc = video_get_drvdata(vdev); struct uvc_device *uvc = video_get_drvdata(vdev);
struct uvc_file_handle *handle = to_uvc_file_handle(file->private_data);
unsigned int mask = 0;
poll_wait(file, &handle->vfh.wait, wait);
if (v4l2_event_pending(&handle->vfh))
mask |= POLLPRI;
mask |= uvc_queue_poll(&uvc->video.queue, file, wait);
return mask; return uvc_queue_poll(&uvc->video.queue, file, wait);
} }
static struct v4l2_file_operations uvc_v4l2_fops = { static struct v4l2_file_operations uvc_v4l2_fops = {
......
...@@ -32,7 +32,7 @@ uvc_video_encode_header(struct uvc_video *video, struct uvc_buffer *buf, ...@@ -32,7 +32,7 @@ uvc_video_encode_header(struct uvc_video *video, struct uvc_buffer *buf,
data[0] = 2; data[0] = 2;
data[1] = UVC_STREAM_EOH | video->fid; data[1] = UVC_STREAM_EOH | video->fid;
if (buf->buf.bytesused - video->queue.buf_used <= len - 2) if (buf->bytesused - video->queue.buf_used <= len - 2)
data[1] |= UVC_STREAM_EOF; data[1] |= UVC_STREAM_EOF;
return 2; return 2;
...@@ -47,8 +47,8 @@ uvc_video_encode_data(struct uvc_video *video, struct uvc_buffer *buf, ...@@ -47,8 +47,8 @@ uvc_video_encode_data(struct uvc_video *video, struct uvc_buffer *buf,
void *mem; void *mem;
/* Copy video data to the USB buffer. */ /* Copy video data to the USB buffer. */
mem = queue->mem + buf->buf.m.offset + queue->buf_used; mem = buf->mem + queue->buf_used;
nbytes = min((unsigned int)len, buf->buf.bytesused - queue->buf_used); nbytes = min((unsigned int)len, buf->bytesused - queue->buf_used);
memcpy(data, mem, nbytes); memcpy(data, mem, nbytes);
queue->buf_used += nbytes; queue->buf_used += nbytes;
...@@ -82,7 +82,7 @@ uvc_video_encode_bulk(struct usb_request *req, struct uvc_video *video, ...@@ -82,7 +82,7 @@ uvc_video_encode_bulk(struct usb_request *req, struct uvc_video *video,
req->length = video->req_size - len; req->length = video->req_size - len;
req->zero = video->payload_size == video->max_payload_size; req->zero = video->payload_size == video->max_payload_size;
if (buf->buf.bytesused == video->queue.buf_used) { if (buf->bytesused == video->queue.buf_used) {
video->queue.buf_used = 0; video->queue.buf_used = 0;
buf->state = UVC_BUF_STATE_DONE; buf->state = UVC_BUF_STATE_DONE;
uvc_queue_next_buffer(&video->queue, buf); uvc_queue_next_buffer(&video->queue, buf);
...@@ -92,7 +92,7 @@ uvc_video_encode_bulk(struct usb_request *req, struct uvc_video *video, ...@@ -92,7 +92,7 @@ uvc_video_encode_bulk(struct usb_request *req, struct uvc_video *video,
} }
if (video->payload_size == video->max_payload_size || if (video->payload_size == video->max_payload_size ||
buf->buf.bytesused == video->queue.buf_used) buf->bytesused == video->queue.buf_used)
video->payload_size = 0; video->payload_size = 0;
} }
...@@ -115,7 +115,7 @@ uvc_video_encode_isoc(struct usb_request *req, struct uvc_video *video, ...@@ -115,7 +115,7 @@ uvc_video_encode_isoc(struct usb_request *req, struct uvc_video *video,
req->length = video->req_size - len; req->length = video->req_size - len;
if (buf->buf.bytesused == video->queue.buf_used) { if (buf->bytesused == video->queue.buf_used) {
video->queue.buf_used = 0; video->queue.buf_used = 0;
buf->state = UVC_BUF_STATE_DONE; buf->state = UVC_BUF_STATE_DONE;
uvc_queue_next_buffer(&video->queue, buf); uvc_queue_next_buffer(&video->queue, buf);
...@@ -161,6 +161,7 @@ static void ...@@ -161,6 +161,7 @@ static void
uvc_video_complete(struct usb_ep *ep, struct usb_request *req) uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
{ {
struct uvc_video *video = req->context; struct uvc_video *video = req->context;
struct uvc_video_queue *queue = &video->queue;
struct uvc_buffer *buf; struct uvc_buffer *buf;
unsigned long flags; unsigned long flags;
int ret; int ret;
...@@ -169,13 +170,15 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req) ...@@ -169,13 +170,15 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
case 0: case 0:
break; break;
case -ESHUTDOWN: case -ESHUTDOWN: /* disconnect from host. */
printk(KERN_INFO "VS request cancelled.\n"); printk(KERN_INFO "VS request cancelled.\n");
uvc_queue_cancel(queue, 1);
goto requeue; goto requeue;
default: default:
printk(KERN_INFO "VS request completed with status %d.\n", printk(KERN_INFO "VS request completed with status %d.\n",
req->status); req->status);
uvc_queue_cancel(queue, 0);
goto requeue; goto requeue;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment