1 // SPDX-License-Identifier: GPL-2.0+
3 * uvc_v4l2.c -- USB Video Class Gadget driver
5 * Copyright (C) 2009-2010
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/usb/uvc.h>
15 #include <linux/videodev2.h>
16 #include <linux/vmalloc.h>
17 #include <linux/wait.h>
19 #include <media/v4l2-dev.h>
20 #include <media/v4l2-event.h>
21 #include <media/v4l2-ioctl.h>
25 #include "uvc_queue.h"
26 #include "uvc_video.h"
28 #include "uvc_configfs.h"
30 static const struct uvc_format_desc *to_uvc_format(struct uvcg_format *uformat)
32 char guid[16] = UVC_GUID_FORMAT_MJPEG;
33 const struct uvc_format_desc *format;
35 if (uformat->type == UVCG_UNCOMPRESSED) {
36 struct uvcg_uncompressed *unc;
38 unc = to_uvcg_uncompressed(&uformat->group.cg_item);
40 return ERR_PTR(-EINVAL);
42 memcpy(guid, unc->desc.guidFormat, sizeof(guid));
43 } else if (uformat->type == UVCG_FRAMEBASED) {
44 struct uvcg_framebased *unc;
46 unc = to_uvcg_framebased(&uformat->group.cg_item);
48 return ERR_PTR(-EINVAL);
50 memcpy(guid, unc->desc.guidFormat, sizeof(guid));
53 format = uvc_format_by_guid(guid);
55 return ERR_PTR(-EINVAL);
60 static int uvc_v4l2_get_bytesperline(struct uvcg_format *uformat,
61 struct uvcg_frame *uframe)
63 struct uvcg_uncompressed *u;
65 if (uformat->type == UVCG_UNCOMPRESSED) {
66 u = to_uvcg_uncompressed(&uformat->group.cg_item);
70 return u->desc.bBitsPerPixel * uframe->frame.w_width / 8;
76 static int uvc_get_frame_size(struct uvcg_format *uformat,
77 struct uvcg_frame *uframe)
79 unsigned int bpl = uvc_v4l2_get_bytesperline(uformat, uframe);
81 return bpl ? bpl * uframe->frame.w_height :
82 uframe->frame.dw_max_video_frame_buffer_size;
85 static struct uvcg_format *find_format_by_index(struct uvc_device *uvc, int index)
87 struct uvcg_format_ptr *format;
88 struct uvcg_format *uformat = NULL;
91 list_for_each_entry(format, &uvc->header->formats, entry) {
93 uformat = format->fmt;
102 static struct uvcg_frame *find_frame_by_index(struct uvc_device *uvc,
103 struct uvcg_format *uformat,
106 struct uvcg_format_ptr *format;
107 struct uvcg_frame_ptr *frame;
108 struct uvcg_frame *uframe = NULL;
110 list_for_each_entry(format, &uvc->header->formats, entry) {
111 if (format->fmt->type != uformat->type)
113 list_for_each_entry(frame, &format->fmt->frames, entry) {
114 if (index == frame->frm->frame.b_frame_index) {
124 static struct uvcg_format *find_format_by_pix(struct uvc_device *uvc,
127 struct uvcg_format_ptr *format;
128 struct uvcg_format *uformat = NULL;
130 list_for_each_entry(format, &uvc->header->formats, entry) {
131 const struct uvc_format_desc *fmtdesc = to_uvc_format(format->fmt);
136 if (fmtdesc->fcc == pixelformat) {
137 uformat = format->fmt;
145 static struct uvcg_frame *find_closest_frame_by_size(struct uvc_device *uvc,
146 struct uvcg_format *uformat,
149 struct uvc_video *video = &uvc->video;
150 struct uvcg_format_ptr *format;
151 struct uvcg_frame_ptr *frame;
152 struct uvcg_frame *uframe = NULL;
153 unsigned int d, maxd;
155 /* Find the closest image size. The distance between image sizes is
156 * the size in pixels of the non-overlapping regions between the
157 * requested size and the frame-specified size.
159 maxd = (unsigned int)-1;
161 list_for_each_entry(format, &uvc->header->formats, entry) {
162 if (format->fmt->type != uformat->type)
165 list_for_each_entry(frame, &format->fmt->frames, entry) {
168 w = frame->frm->frame.w_width;
169 h = frame->frm->frame.w_height;
171 d = min(w, rw) * min(h, rh);
172 d = w*h + rw*rh - 2*d;
184 uvcg_dbg(&video->uvc->func, "Unsupported size %ux%u\n", rw, rh);
189 /* --------------------------------------------------------------------------
194 uvc_send_response(struct uvc_device *uvc, struct uvc_request_data *data)
196 struct usb_composite_dev *cdev = uvc->func.config->cdev;
197 struct usb_request *req = uvc->control_req;
199 if (data->length < 0)
200 return usb_ep_set_halt(cdev->gadget->ep0);
202 req->length = min_t(unsigned int, uvc->event_length, data->length);
203 req->zero = data->length < uvc->event_length;
205 memcpy(req->buf, data->data, req->length);
207 return usb_ep_queue(cdev->gadget->ep0, req, GFP_KERNEL);
210 /* --------------------------------------------------------------------------
215 uvc_v4l2_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
217 struct video_device *vdev = video_devdata(file);
218 struct uvc_device *uvc = video_get_drvdata(vdev);
219 struct usb_composite_dev *cdev = uvc->func.config->cdev;
221 strscpy(cap->driver, "g_uvc", sizeof(cap->driver));
222 strscpy(cap->card, cdev->gadget->name, sizeof(cap->card));
223 strscpy(cap->bus_info, dev_name(&cdev->gadget->dev),
224 sizeof(cap->bus_info));
229 uvc_v4l2_get_format(struct file *file, void *fh, struct v4l2_format *fmt)
231 struct video_device *vdev = video_devdata(file);
232 struct uvc_device *uvc = video_get_drvdata(vdev);
233 struct uvc_video *video = &uvc->video;
235 fmt->fmt.pix.pixelformat = video->fcc;
236 fmt->fmt.pix.width = video->width;
237 fmt->fmt.pix.height = video->height;
238 fmt->fmt.pix.field = V4L2_FIELD_NONE;
239 fmt->fmt.pix.bytesperline = video->bpp * video->width / 8;
240 fmt->fmt.pix.sizeimage = video->imagesize;
241 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
242 fmt->fmt.pix.priv = 0;
248 uvc_v4l2_try_format(struct file *file, void *fh, struct v4l2_format *fmt)
250 struct video_device *vdev = video_devdata(file);
251 struct uvc_device *uvc = video_get_drvdata(vdev);
252 struct uvc_video *video = &uvc->video;
253 struct uvcg_format *uformat;
254 struct uvcg_frame *uframe;
255 const struct uvc_format_desc *fmtdesc;
258 if (fmt->type != video->queue.queue.type)
261 fcc = (u8 *)&fmt->fmt.pix.pixelformat;
262 uvcg_dbg(&uvc->func, "Trying format 0x%08x (%c%c%c%c): %ux%u\n",
263 fmt->fmt.pix.pixelformat,
264 fcc[0], fcc[1], fcc[2], fcc[3],
265 fmt->fmt.pix.width, fmt->fmt.pix.height);
267 uformat = find_format_by_pix(uvc, fmt->fmt.pix.pixelformat);
271 uframe = find_closest_frame_by_size(uvc, uformat,
272 fmt->fmt.pix.width, fmt->fmt.pix.height);
276 if (uformat->type == UVCG_UNCOMPRESSED) {
277 struct uvcg_uncompressed *u =
278 to_uvcg_uncompressed(&uformat->group.cg_item);
282 v4l2_fill_pixfmt(&fmt->fmt.pix, fmt->fmt.pix.pixelformat,
283 uframe->frame.w_width, uframe->frame.w_height);
285 if (fmt->fmt.pix.sizeimage != (uvc_v4l2_get_bytesperline(uformat, uframe) *
286 uframe->frame.w_height))
289 fmt->fmt.pix.width = uframe->frame.w_width;
290 fmt->fmt.pix.height = uframe->frame.w_height;
291 fmt->fmt.pix.bytesperline = uvc_v4l2_get_bytesperline(uformat, uframe);
292 fmt->fmt.pix.sizeimage = uvc_get_frame_size(uformat, uframe);
293 fmtdesc = to_uvc_format(uformat);
295 return PTR_ERR(fmtdesc);
296 fmt->fmt.pix.pixelformat = fmtdesc->fcc;
298 fmt->fmt.pix.field = V4L2_FIELD_NONE;
299 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
300 fmt->fmt.pix.priv = 0;
306 uvc_v4l2_set_format(struct file *file, void *fh, struct v4l2_format *fmt)
308 struct video_device *vdev = video_devdata(file);
309 struct uvc_device *uvc = video_get_drvdata(vdev);
310 struct uvc_video *video = &uvc->video;
313 ret = uvc_v4l2_try_format(file, fh, fmt);
317 video->fcc = fmt->fmt.pix.pixelformat;
318 video->bpp = fmt->fmt.pix.bytesperline * 8 / video->width;
319 video->width = fmt->fmt.pix.width;
320 video->height = fmt->fmt.pix.height;
321 video->imagesize = fmt->fmt.pix.sizeimage;
326 static int uvc_v4l2_g_parm(struct file *file, void *fh,
327 struct v4l2_streamparm *parm)
329 struct video_device *vdev = video_devdata(file);
330 struct uvc_device *uvc = video_get_drvdata(vdev);
331 struct uvc_video *video = &uvc->video;
332 struct v4l2_fract timeperframe;
334 if (!V4L2_TYPE_IS_OUTPUT(parm->type))
337 /* Return the actual frame period. */
338 timeperframe.numerator = video->interval;
339 timeperframe.denominator = 10000000;
340 v4l2_simplify_fraction(&timeperframe.numerator,
341 &timeperframe.denominator, 8, 333);
343 uvcg_dbg(&uvc->func, "Getting frame interval of %u/%u (%u)\n",
344 timeperframe.numerator, timeperframe.denominator,
347 parm->parm.output.timeperframe = timeperframe;
348 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
353 static int uvc_v4l2_s_parm(struct file *file, void *fh,
354 struct v4l2_streamparm *parm)
356 struct video_device *vdev = video_devdata(file);
357 struct uvc_device *uvc = video_get_drvdata(vdev);
358 struct uvc_video *video = &uvc->video;
359 struct v4l2_fract timeperframe;
361 if (!V4L2_TYPE_IS_OUTPUT(parm->type))
364 timeperframe = parm->parm.output.timeperframe;
366 video->interval = v4l2_fraction_to_interval(timeperframe.numerator,
367 timeperframe.denominator);
369 uvcg_dbg(&uvc->func, "Setting frame interval to %u/%u (%u)\n",
370 timeperframe.numerator, timeperframe.denominator,
377 uvc_v4l2_enum_frameintervals(struct file *file, void *fh,
378 struct v4l2_frmivalenum *fival)
380 struct video_device *vdev = video_devdata(file);
381 struct uvc_device *uvc = video_get_drvdata(vdev);
382 struct uvcg_format *uformat = NULL;
383 struct uvcg_frame *uframe = NULL;
384 struct uvcg_frame_ptr *frame;
386 uformat = find_format_by_pix(uvc, fival->pixel_format);
390 list_for_each_entry(frame, &uformat->frames, entry) {
391 if (frame->frm->frame.w_width == fival->width &&
392 frame->frm->frame.w_height == fival->height) {
400 if (fival->index >= uframe->frame.b_frame_interval_type)
403 fival->discrete.numerator =
404 uframe->dw_frame_interval[fival->index];
406 /* TODO: handle V4L2_FRMIVAL_TYPE_STEPWISE */
407 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
408 fival->discrete.denominator = 10000000;
409 v4l2_simplify_fraction(&fival->discrete.numerator,
410 &fival->discrete.denominator, 8, 333);
416 uvc_v4l2_enum_framesizes(struct file *file, void *fh,
417 struct v4l2_frmsizeenum *fsize)
419 struct video_device *vdev = video_devdata(file);
420 struct uvc_device *uvc = video_get_drvdata(vdev);
421 struct uvcg_format *uformat = NULL;
422 struct uvcg_frame *uframe = NULL;
424 uformat = find_format_by_pix(uvc, fsize->pixel_format);
428 if (fsize->index >= uformat->num_frames)
431 uframe = find_frame_by_index(uvc, uformat, fsize->index + 1);
435 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
436 fsize->discrete.width = uframe->frame.w_width;
437 fsize->discrete.height = uframe->frame.w_height;
443 uvc_v4l2_enum_format(struct file *file, void *fh, struct v4l2_fmtdesc *f)
445 struct video_device *vdev = video_devdata(file);
446 struct uvc_device *uvc = video_get_drvdata(vdev);
447 const struct uvc_format_desc *fmtdesc;
448 struct uvcg_format *uformat;
450 if (f->index >= uvc->header->num_fmt)
453 uformat = find_format_by_index(uvc, f->index + 1);
457 fmtdesc = to_uvc_format(uformat);
459 return PTR_ERR(fmtdesc);
461 f->pixelformat = fmtdesc->fcc;
467 uvc_v4l2_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *b)
469 struct video_device *vdev = video_devdata(file);
470 struct uvc_device *uvc = video_get_drvdata(vdev);
471 struct uvc_video *video = &uvc->video;
473 if (b->type != video->queue.queue.type)
476 return uvcg_alloc_buffers(&video->queue, b);
480 uvc_v4l2_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
482 struct video_device *vdev = video_devdata(file);
483 struct uvc_device *uvc = video_get_drvdata(vdev);
484 struct uvc_video *video = &uvc->video;
486 return uvcg_query_buffer(&video->queue, b);
490 uvc_v4l2_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
492 struct video_device *vdev = video_devdata(file);
493 struct uvc_device *uvc = video_get_drvdata(vdev);
494 struct uvc_video *video = &uvc->video;
497 ret = uvcg_queue_buffer(&video->queue, b);
501 if (uvc->state == UVC_STATE_STREAMING)
502 queue_work(video->async_wq, &video->pump);
508 uvc_v4l2_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
510 struct video_device *vdev = video_devdata(file);
511 struct uvc_device *uvc = video_get_drvdata(vdev);
512 struct uvc_video *video = &uvc->video;
514 return uvcg_dequeue_buffer(&video->queue, b, file->f_flags & O_NONBLOCK);
518 uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
520 struct video_device *vdev = video_devdata(file);
521 struct uvc_device *uvc = video_get_drvdata(vdev);
522 struct uvc_video *video = &uvc->video;
525 if (type != video->queue.queue.type)
528 /* Enable UVC video. */
529 ret = uvcg_video_enable(video);
534 * Complete the alternate setting selection setup phase now that
535 * userspace is ready to provide video frames.
537 uvc_function_setup_continue(uvc, 0);
538 uvc->state = UVC_STATE_STREAMING;
544 uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
546 struct video_device *vdev = video_devdata(file);
547 struct uvc_device *uvc = video_get_drvdata(vdev);
548 struct uvc_video *video = &uvc->video;
551 if (type != video->queue.queue.type)
554 ret = uvcg_video_disable(video);
558 if (uvc->state != UVC_STATE_STREAMING)
561 uvc->state = UVC_STATE_CONNECTED;
562 uvc_function_setup_continue(uvc, 1);
567 uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
568 const struct v4l2_event_subscription *sub)
570 struct uvc_device *uvc = video_get_drvdata(fh->vdev);
571 struct uvc_file_handle *handle = to_uvc_file_handle(fh);
574 if (sub->type < UVC_EVENT_FIRST || sub->type > UVC_EVENT_LAST)
577 if (sub->type == UVC_EVENT_SETUP && uvc->func_connected)
580 ret = v4l2_event_subscribe(fh, sub, 2, NULL);
584 if (sub->type == UVC_EVENT_SETUP) {
585 uvc->func_connected = true;
586 handle->is_uvc_app_handle = true;
587 uvc_function_connect(uvc);
593 static void uvc_v4l2_disable(struct uvc_device *uvc)
595 uvc_function_disconnect(uvc);
596 uvcg_video_disable(&uvc->video);
597 uvcg_free_buffers(&uvc->video.queue);
598 uvc->func_connected = false;
599 wake_up_interruptible(&uvc->func_connected_queue);
603 uvc_v4l2_unsubscribe_event(struct v4l2_fh *fh,
604 const struct v4l2_event_subscription *sub)
606 struct uvc_device *uvc = video_get_drvdata(fh->vdev);
607 struct uvc_file_handle *handle = to_uvc_file_handle(fh);
610 ret = v4l2_event_unsubscribe(fh, sub);
614 if (sub->type == UVC_EVENT_SETUP && handle->is_uvc_app_handle) {
615 uvc_v4l2_disable(uvc);
616 handle->is_uvc_app_handle = false;
623 uvc_v4l2_ioctl_default(struct file *file, void *fh, bool valid_prio,
624 unsigned int cmd, void *arg)
626 struct video_device *vdev = video_devdata(file);
627 struct uvc_device *uvc = video_get_drvdata(vdev);
630 case UVCIOC_SEND_RESPONSE:
631 return uvc_send_response(uvc, arg);
638 const struct v4l2_ioctl_ops uvc_v4l2_ioctl_ops = {
639 .vidioc_querycap = uvc_v4l2_querycap,
640 .vidioc_try_fmt_vid_out = uvc_v4l2_try_format,
641 .vidioc_g_fmt_vid_out = uvc_v4l2_get_format,
642 .vidioc_s_fmt_vid_out = uvc_v4l2_set_format,
643 .vidioc_enum_frameintervals = uvc_v4l2_enum_frameintervals,
644 .vidioc_enum_framesizes = uvc_v4l2_enum_framesizes,
645 .vidioc_enum_fmt_vid_out = uvc_v4l2_enum_format,
646 .vidioc_reqbufs = uvc_v4l2_reqbufs,
647 .vidioc_querybuf = uvc_v4l2_querybuf,
648 .vidioc_qbuf = uvc_v4l2_qbuf,
649 .vidioc_dqbuf = uvc_v4l2_dqbuf,
650 .vidioc_streamon = uvc_v4l2_streamon,
651 .vidioc_streamoff = uvc_v4l2_streamoff,
652 .vidioc_s_parm = uvc_v4l2_s_parm,
653 .vidioc_g_parm = uvc_v4l2_g_parm,
654 .vidioc_subscribe_event = uvc_v4l2_subscribe_event,
655 .vidioc_unsubscribe_event = uvc_v4l2_unsubscribe_event,
656 .vidioc_default = uvc_v4l2_ioctl_default,
659 /* --------------------------------------------------------------------------
664 uvc_v4l2_open(struct file *file)
666 struct video_device *vdev = video_devdata(file);
667 struct uvc_device *uvc = video_get_drvdata(vdev);
668 struct uvc_file_handle *handle;
670 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
674 v4l2_fh_init(&handle->vfh, vdev);
675 v4l2_fh_add(&handle->vfh);
677 handle->device = &uvc->video;
678 file->private_data = &handle->vfh;
684 uvc_v4l2_release(struct file *file)
686 struct video_device *vdev = video_devdata(file);
687 struct uvc_device *uvc = video_get_drvdata(vdev);
688 struct uvc_file_handle *handle = to_uvc_file_handle(file->private_data);
689 struct uvc_video *video = handle->device;
691 mutex_lock(&video->mutex);
692 if (handle->is_uvc_app_handle)
693 uvc_v4l2_disable(uvc);
694 mutex_unlock(&video->mutex);
696 file->private_data = NULL;
697 v4l2_fh_del(&handle->vfh);
698 v4l2_fh_exit(&handle->vfh);
705 uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
707 struct video_device *vdev = video_devdata(file);
708 struct uvc_device *uvc = video_get_drvdata(vdev);
710 return uvcg_queue_mmap(&uvc->video.queue, vma);
714 uvc_v4l2_poll(struct file *file, poll_table *wait)
716 struct video_device *vdev = video_devdata(file);
717 struct uvc_device *uvc = video_get_drvdata(vdev);
719 return uvcg_queue_poll(&uvc->video.queue, file, wait);
723 static unsigned long uvcg_v4l2_get_unmapped_area(struct file *file,
724 unsigned long addr, unsigned long len, unsigned long pgoff,
727 struct video_device *vdev = video_devdata(file);
728 struct uvc_device *uvc = video_get_drvdata(vdev);
730 return uvcg_queue_get_unmapped_area(&uvc->video.queue, pgoff);
734 const struct v4l2_file_operations uvc_v4l2_fops = {
735 .owner = THIS_MODULE,
736 .open = uvc_v4l2_open,
737 .release = uvc_v4l2_release,
738 .unlocked_ioctl = video_ioctl2,
739 .mmap = uvc_v4l2_mmap,
740 .poll = uvc_v4l2_poll,
742 .get_unmapped_area = uvcg_v4l2_get_unmapped_area,