1 // SPDX-License-Identifier: GPL-2.0+
3 * uvc_v4l2.c -- USB Video Class Gadget driver
5 * Copyright (C) 2009-2010
6 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
9 #include <linux/device.h>
10 #include <linux/errno.h>
11 #include <linux/kernel.h>
12 #include <linux/list.h>
13 #include <linux/usb/g_uvc.h>
14 #include <linux/videodev2.h>
15 #include <linux/vmalloc.h>
16 #include <linux/wait.h>
18 #include <media/v4l2-dev.h>
19 #include <media/v4l2-event.h>
20 #include <media/v4l2-ioctl.h>
24 #include "uvc_queue.h"
25 #include "uvc_video.h"
28 /* --------------------------------------------------------------------------
33 uvc_send_response(struct uvc_device *uvc, struct uvc_request_data *data)
35 struct usb_composite_dev *cdev = uvc->func.config->cdev;
36 struct usb_request *req = uvc->control_req;
39 return usb_ep_set_halt(cdev->gadget->ep0);
41 req->length = min_t(unsigned int, uvc->event_length, data->length);
42 req->zero = data->length < uvc->event_length;
44 memcpy(req->buf, data->data, req->length);
46 return usb_ep_queue(cdev->gadget->ep0, req, GFP_KERNEL);
49 /* --------------------------------------------------------------------------
58 static struct uvc_format uvc_formats[] = {
59 { 16, V4L2_PIX_FMT_YUYV },
60 { 0, V4L2_PIX_FMT_MJPEG },
64 uvc_v4l2_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
66 struct video_device *vdev = video_devdata(file);
67 struct uvc_device *uvc = video_get_drvdata(vdev);
68 struct usb_composite_dev *cdev = uvc->func.config->cdev;
70 strlcpy(cap->driver, "g_uvc", sizeof(cap->driver));
71 strlcpy(cap->card, cdev->gadget->name, sizeof(cap->card));
72 strlcpy(cap->bus_info, dev_name(&cdev->gadget->dev),
73 sizeof(cap->bus_info));
78 uvc_v4l2_get_format(struct file *file, void *fh, struct v4l2_format *fmt)
80 struct video_device *vdev = video_devdata(file);
81 struct uvc_device *uvc = video_get_drvdata(vdev);
82 struct uvc_video *video = &uvc->video;
84 fmt->fmt.pix.pixelformat = video->fcc;
85 fmt->fmt.pix.width = video->width;
86 fmt->fmt.pix.height = video->height;
87 fmt->fmt.pix.field = V4L2_FIELD_NONE;
88 fmt->fmt.pix.bytesperline = video->bpp * video->width / 8;
89 fmt->fmt.pix.sizeimage = video->imagesize;
90 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
91 fmt->fmt.pix.priv = 0;
97 uvc_v4l2_set_format(struct file *file, void *fh, struct v4l2_format *fmt)
99 struct video_device *vdev = video_devdata(file);
100 struct uvc_device *uvc = video_get_drvdata(vdev);
101 struct uvc_video *video = &uvc->video;
102 struct uvc_format *format;
103 unsigned int imagesize;
107 for (i = 0; i < ARRAY_SIZE(uvc_formats); ++i) {
108 format = &uvc_formats[i];
109 if (format->fcc == fmt->fmt.pix.pixelformat)
113 if (i == ARRAY_SIZE(uvc_formats)) {
114 uvcg_info(&uvc->func, "Unsupported format 0x%08x.\n",
115 fmt->fmt.pix.pixelformat);
119 bpl = format->bpp * fmt->fmt.pix.width / 8;
120 imagesize = bpl ? bpl * fmt->fmt.pix.height : fmt->fmt.pix.sizeimage;
122 video->fcc = format->fcc;
123 video->bpp = format->bpp;
124 video->width = fmt->fmt.pix.width;
125 video->height = fmt->fmt.pix.height;
126 video->imagesize = imagesize;
128 fmt->fmt.pix.field = V4L2_FIELD_NONE;
129 fmt->fmt.pix.bytesperline = bpl;
130 fmt->fmt.pix.sizeimage = imagesize;
131 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
132 fmt->fmt.pix.priv = 0;
138 uvc_v4l2_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *b)
140 struct video_device *vdev = video_devdata(file);
141 struct uvc_device *uvc = video_get_drvdata(vdev);
142 struct uvc_video *video = &uvc->video;
144 if (b->type != video->queue.queue.type)
147 return uvcg_alloc_buffers(&video->queue, b);
151 uvc_v4l2_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
153 struct video_device *vdev = video_devdata(file);
154 struct uvc_device *uvc = video_get_drvdata(vdev);
155 struct uvc_video *video = &uvc->video;
157 return uvcg_query_buffer(&video->queue, b);
161 uvc_v4l2_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
163 struct video_device *vdev = video_devdata(file);
164 struct uvc_device *uvc = video_get_drvdata(vdev);
165 struct uvc_video *video = &uvc->video;
168 ret = uvcg_queue_buffer(&video->queue, b);
172 if (uvc->state == UVC_STATE_STREAMING)
173 schedule_work(&video->pump);
179 uvc_v4l2_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
181 struct video_device *vdev = video_devdata(file);
182 struct uvc_device *uvc = video_get_drvdata(vdev);
183 struct uvc_video *video = &uvc->video;
185 return uvcg_dequeue_buffer(&video->queue, b, file->f_flags & O_NONBLOCK);
189 uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
191 struct video_device *vdev = video_devdata(file);
192 struct uvc_device *uvc = video_get_drvdata(vdev);
193 struct uvc_video *video = &uvc->video;
196 if (type != video->queue.queue.type)
199 /* Enable UVC video. */
200 ret = uvcg_video_enable(video, 1);
205 * Complete the alternate setting selection setup phase now that
206 * userspace is ready to provide video frames.
208 uvc_function_setup_continue(uvc);
209 uvc->state = UVC_STATE_STREAMING;
215 uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
217 struct video_device *vdev = video_devdata(file);
218 struct uvc_device *uvc = video_get_drvdata(vdev);
219 struct uvc_video *video = &uvc->video;
221 if (type != video->queue.queue.type)
224 return uvcg_video_enable(video, 0);
228 uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
229 const struct v4l2_event_subscription *sub)
231 struct uvc_device *uvc = video_get_drvdata(fh->vdev);
232 struct uvc_file_handle *handle = to_uvc_file_handle(fh);
235 if (sub->type < UVC_EVENT_FIRST || sub->type > UVC_EVENT_LAST)
238 if (sub->type == UVC_EVENT_SETUP && uvc->func_connected)
241 ret = v4l2_event_subscribe(fh, sub, 2, NULL);
245 if (sub->type == UVC_EVENT_SETUP) {
246 uvc->func_connected = true;
247 handle->is_uvc_app_handle = true;
248 uvc_function_connect(uvc);
254 static void uvc_v4l2_disable(struct uvc_device *uvc)
256 uvc->func_connected = false;
257 uvc_function_disconnect(uvc);
258 uvcg_video_enable(&uvc->video, 0);
259 uvcg_free_buffers(&uvc->video.queue);
263 uvc_v4l2_unsubscribe_event(struct v4l2_fh *fh,
264 const struct v4l2_event_subscription *sub)
266 struct uvc_device *uvc = video_get_drvdata(fh->vdev);
267 struct uvc_file_handle *handle = to_uvc_file_handle(fh);
270 ret = v4l2_event_unsubscribe(fh, sub);
274 if (sub->type == UVC_EVENT_SETUP && handle->is_uvc_app_handle) {
275 uvc_v4l2_disable(uvc);
276 handle->is_uvc_app_handle = false;
283 uvc_v4l2_ioctl_default(struct file *file, void *fh, bool valid_prio,
284 unsigned int cmd, void *arg)
286 struct video_device *vdev = video_devdata(file);
287 struct uvc_device *uvc = video_get_drvdata(vdev);
290 case UVCIOC_SEND_RESPONSE:
291 return uvc_send_response(uvc, arg);
298 const struct v4l2_ioctl_ops uvc_v4l2_ioctl_ops = {
299 .vidioc_querycap = uvc_v4l2_querycap,
300 .vidioc_g_fmt_vid_out = uvc_v4l2_get_format,
301 .vidioc_s_fmt_vid_out = uvc_v4l2_set_format,
302 .vidioc_reqbufs = uvc_v4l2_reqbufs,
303 .vidioc_querybuf = uvc_v4l2_querybuf,
304 .vidioc_qbuf = uvc_v4l2_qbuf,
305 .vidioc_dqbuf = uvc_v4l2_dqbuf,
306 .vidioc_streamon = uvc_v4l2_streamon,
307 .vidioc_streamoff = uvc_v4l2_streamoff,
308 .vidioc_subscribe_event = uvc_v4l2_subscribe_event,
309 .vidioc_unsubscribe_event = uvc_v4l2_unsubscribe_event,
310 .vidioc_default = uvc_v4l2_ioctl_default,
313 /* --------------------------------------------------------------------------
318 uvc_v4l2_open(struct file *file)
320 struct video_device *vdev = video_devdata(file);
321 struct uvc_device *uvc = video_get_drvdata(vdev);
322 struct uvc_file_handle *handle;
324 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
328 v4l2_fh_init(&handle->vfh, vdev);
329 v4l2_fh_add(&handle->vfh);
331 handle->device = &uvc->video;
332 file->private_data = &handle->vfh;
338 uvc_v4l2_release(struct file *file)
340 struct video_device *vdev = video_devdata(file);
341 struct uvc_device *uvc = video_get_drvdata(vdev);
342 struct uvc_file_handle *handle = to_uvc_file_handle(file->private_data);
343 struct uvc_video *video = handle->device;
345 mutex_lock(&video->mutex);
346 if (handle->is_uvc_app_handle)
347 uvc_v4l2_disable(uvc);
348 mutex_unlock(&video->mutex);
350 file->private_data = NULL;
351 v4l2_fh_del(&handle->vfh);
352 v4l2_fh_exit(&handle->vfh);
359 uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
361 struct video_device *vdev = video_devdata(file);
362 struct uvc_device *uvc = video_get_drvdata(vdev);
364 return uvcg_queue_mmap(&uvc->video.queue, vma);
368 uvc_v4l2_poll(struct file *file, poll_table *wait)
370 struct video_device *vdev = video_devdata(file);
371 struct uvc_device *uvc = video_get_drvdata(vdev);
373 return uvcg_queue_poll(&uvc->video.queue, file, wait);
377 static unsigned long uvcg_v4l2_get_unmapped_area(struct file *file,
378 unsigned long addr, unsigned long len, unsigned long pgoff,
381 struct video_device *vdev = video_devdata(file);
382 struct uvc_device *uvc = video_get_drvdata(vdev);
384 return uvcg_queue_get_unmapped_area(&uvc->video.queue, pgoff);
388 const struct v4l2_file_operations uvc_v4l2_fops = {
389 .owner = THIS_MODULE,
390 .open = uvc_v4l2_open,
391 .release = uvc_v4l2_release,
392 .unlocked_ioctl = video_ioctl2,
393 .mmap = uvc_v4l2_mmap,
394 .poll = uvc_v4l2_poll,
396 .get_unmapped_area = uvcg_v4l2_get_unmapped_area,