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
......
This diff is collapsed.
...@@ -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,14 +263,14 @@ uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg) ...@@ -272,14 +263,14 @@ 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);
} }
/* Events */ /* Events */
case VIDIOC_DQEVENT: case VIDIOC_DQEVENT:
{ {
struct v4l2_event *event = arg; struct v4l2_event *event = arg;
...@@ -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