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/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;
34 struct uvcg_uncompressed *unc;
36 if (uformat->type == UVCG_UNCOMPRESSED) {
37 unc = to_uvcg_uncompressed(&uformat->group.cg_item);
39 return ERR_PTR(-EINVAL);
41 memcpy(guid, unc->desc.guidFormat, sizeof(guid));
44 format = uvc_format_by_guid(guid);
46 return ERR_PTR(-EINVAL);
51 static int uvc_v4l2_get_bytesperline(struct uvcg_format *uformat,
52 struct uvcg_frame *uframe)
54 struct uvcg_uncompressed *u;
56 if (uformat->type == UVCG_UNCOMPRESSED) {
57 u = to_uvcg_uncompressed(&uformat->group.cg_item);
61 return u->desc.bBitsPerPixel * uframe->frame.w_width / 8;
67 static int uvc_get_frame_size(struct uvcg_format *uformat,
68 struct uvcg_frame *uframe)
70 unsigned int bpl = uvc_v4l2_get_bytesperline(uformat, uframe);
72 return bpl ? bpl * uframe->frame.w_height :
73 uframe->frame.dw_max_video_frame_buffer_size;
76 static struct uvcg_format *find_format_by_index(struct uvc_device *uvc, int index)
78 struct uvcg_format_ptr *format;
79 struct uvcg_format *uformat = NULL;
82 list_for_each_entry(format, &uvc->header->formats, entry) {
84 uformat = format->fmt;
93 static struct uvcg_frame *find_frame_by_index(struct uvc_device *uvc,
94 struct uvcg_format *uformat,
97 struct uvcg_format_ptr *format;
98 struct uvcg_frame_ptr *frame;
99 struct uvcg_frame *uframe = NULL;
101 list_for_each_entry(format, &uvc->header->formats, entry) {
102 if (format->fmt->type != uformat->type)
104 list_for_each_entry(frame, &format->fmt->frames, entry) {
105 if (index == frame->frm->frame.b_frame_index) {
115 static struct uvcg_format *find_format_by_pix(struct uvc_device *uvc,
118 struct uvcg_format_ptr *format;
119 struct uvcg_format *uformat = NULL;
121 list_for_each_entry(format, &uvc->header->formats, entry) {
122 const struct uvc_format_desc *fmtdesc = to_uvc_format(format->fmt);
124 if (fmtdesc->fcc == pixelformat) {
125 uformat = format->fmt;
133 static struct uvcg_frame *find_closest_frame_by_size(struct uvc_device *uvc,
134 struct uvcg_format *uformat,
137 struct uvc_video *video = &uvc->video;
138 struct uvcg_format_ptr *format;
139 struct uvcg_frame_ptr *frame;
140 struct uvcg_frame *uframe = NULL;
141 unsigned int d, maxd;
143 /* Find the closest image size. The distance between image sizes is
144 * the size in pixels of the non-overlapping regions between the
145 * requested size and the frame-specified size.
147 maxd = (unsigned int)-1;
149 list_for_each_entry(format, &uvc->header->formats, entry) {
150 if (format->fmt->type != uformat->type)
153 list_for_each_entry(frame, &format->fmt->frames, entry) {
156 w = frame->frm->frame.w_width;
157 h = frame->frm->frame.w_height;
159 d = min(w, rw) * min(h, rh);
160 d = w*h + rw*rh - 2*d;
172 uvcg_dbg(&video->uvc->func, "Unsupported size %ux%u\n", rw, rh);
177 /* --------------------------------------------------------------------------
182 uvc_send_response(struct uvc_device *uvc, struct uvc_request_data *data)
184 struct usb_composite_dev *cdev = uvc->func.config->cdev;
185 struct usb_request *req = uvc->control_req;
187 if (data->length < 0)
188 return usb_ep_set_halt(cdev->gadget->ep0);
190 req->length = min_t(unsigned int, uvc->event_length, data->length);
191 req->zero = data->length < uvc->event_length;
193 memcpy(req->buf, data->data, req->length);
195 return usb_ep_queue(cdev->gadget->ep0, req, GFP_KERNEL);
198 /* --------------------------------------------------------------------------
203 uvc_v4l2_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
205 struct video_device *vdev = video_devdata(file);
206 struct uvc_device *uvc = video_get_drvdata(vdev);
207 struct usb_composite_dev *cdev = uvc->func.config->cdev;
209 strscpy(cap->driver, "g_uvc", sizeof(cap->driver));
210 strscpy(cap->card, cdev->gadget->name, sizeof(cap->card));
211 strscpy(cap->bus_info, dev_name(&cdev->gadget->dev),
212 sizeof(cap->bus_info));
217 uvc_v4l2_get_format(struct file *file, void *fh, struct v4l2_format *fmt)
219 struct video_device *vdev = video_devdata(file);
220 struct uvc_device *uvc = video_get_drvdata(vdev);
221 struct uvc_video *video = &uvc->video;
223 fmt->fmt.pix.pixelformat = video->fcc;
224 fmt->fmt.pix.width = video->width;
225 fmt->fmt.pix.height = video->height;
226 fmt->fmt.pix.field = V4L2_FIELD_NONE;
227 fmt->fmt.pix.bytesperline = video->bpp * video->width / 8;
228 fmt->fmt.pix.sizeimage = video->imagesize;
229 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
230 fmt->fmt.pix.priv = 0;
236 uvc_v4l2_try_format(struct file *file, void *fh, struct v4l2_format *fmt)
238 struct video_device *vdev = video_devdata(file);
239 struct uvc_device *uvc = video_get_drvdata(vdev);
240 struct uvc_video *video = &uvc->video;
241 struct uvcg_format *uformat;
242 struct uvcg_frame *uframe;
245 if (fmt->type != video->queue.queue.type)
248 fcc = (u8 *)&fmt->fmt.pix.pixelformat;
249 uvcg_dbg(&uvc->func, "Trying format 0x%08x (%c%c%c%c): %ux%u\n",
250 fmt->fmt.pix.pixelformat,
251 fcc[0], fcc[1], fcc[2], fcc[3],
252 fmt->fmt.pix.width, fmt->fmt.pix.height);
254 uformat = find_format_by_pix(uvc, fmt->fmt.pix.pixelformat);
258 uframe = find_closest_frame_by_size(uvc, uformat,
259 fmt->fmt.pix.width, fmt->fmt.pix.height);
263 fmt->fmt.pix.width = uframe->frame.w_width;
264 fmt->fmt.pix.height = uframe->frame.w_height;
265 fmt->fmt.pix.field = V4L2_FIELD_NONE;
266 fmt->fmt.pix.bytesperline = uvc_v4l2_get_bytesperline(uformat, uframe);
267 fmt->fmt.pix.sizeimage = uvc_get_frame_size(uformat, uframe);
268 fmt->fmt.pix.pixelformat = to_uvc_format(uformat)->fcc;
269 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
270 fmt->fmt.pix.priv = 0;
276 uvc_v4l2_set_format(struct file *file, void *fh, struct v4l2_format *fmt)
278 struct video_device *vdev = video_devdata(file);
279 struct uvc_device *uvc = video_get_drvdata(vdev);
280 struct uvc_video *video = &uvc->video;
283 ret = uvc_v4l2_try_format(file, fh, fmt);
287 video->fcc = fmt->fmt.pix.pixelformat;
288 video->bpp = fmt->fmt.pix.bytesperline * 8 / video->width;
289 video->width = fmt->fmt.pix.width;
290 video->height = fmt->fmt.pix.height;
291 video->imagesize = fmt->fmt.pix.sizeimage;
297 uvc_v4l2_enum_frameintervals(struct file *file, void *fh,
298 struct v4l2_frmivalenum *fival)
300 struct video_device *vdev = video_devdata(file);
301 struct uvc_device *uvc = video_get_drvdata(vdev);
302 struct uvcg_format *uformat = NULL;
303 struct uvcg_frame *uframe = NULL;
304 struct uvcg_frame_ptr *frame;
306 uformat = find_format_by_pix(uvc, fival->pixel_format);
310 list_for_each_entry(frame, &uformat->frames, entry) {
311 if (frame->frm->frame.w_width == fival->width &&
312 frame->frm->frame.w_height == fival->height) {
320 if (fival->index >= uframe->frame.b_frame_interval_type)
323 fival->discrete.numerator =
324 uframe->dw_frame_interval[fival->index];
326 /* TODO: handle V4L2_FRMIVAL_TYPE_STEPWISE */
327 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
328 fival->discrete.denominator = 10000000;
329 v4l2_simplify_fraction(&fival->discrete.numerator,
330 &fival->discrete.denominator, 8, 333);
336 uvc_v4l2_enum_framesizes(struct file *file, void *fh,
337 struct v4l2_frmsizeenum *fsize)
339 struct video_device *vdev = video_devdata(file);
340 struct uvc_device *uvc = video_get_drvdata(vdev);
341 struct uvcg_format *uformat = NULL;
342 struct uvcg_frame *uframe = NULL;
344 uformat = find_format_by_pix(uvc, fsize->pixel_format);
348 if (fsize->index >= uformat->num_frames)
351 uframe = find_frame_by_index(uvc, uformat, fsize->index + 1);
355 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
356 fsize->discrete.width = uframe->frame.w_width;
357 fsize->discrete.height = uframe->frame.w_height;
363 uvc_v4l2_enum_format(struct file *file, void *fh, struct v4l2_fmtdesc *f)
365 struct video_device *vdev = video_devdata(file);
366 struct uvc_device *uvc = video_get_drvdata(vdev);
367 const struct uvc_format_desc *fmtdesc;
368 struct uvcg_format *uformat;
370 if (f->index >= uvc->header->num_fmt)
373 uformat = find_format_by_index(uvc, f->index + 1);
377 fmtdesc = to_uvc_format(uformat);
378 f->pixelformat = fmtdesc->fcc;
384 uvc_v4l2_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *b)
386 struct video_device *vdev = video_devdata(file);
387 struct uvc_device *uvc = video_get_drvdata(vdev);
388 struct uvc_video *video = &uvc->video;
390 if (b->type != video->queue.queue.type)
393 return uvcg_alloc_buffers(&video->queue, b);
397 uvc_v4l2_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
399 struct video_device *vdev = video_devdata(file);
400 struct uvc_device *uvc = video_get_drvdata(vdev);
401 struct uvc_video *video = &uvc->video;
403 return uvcg_query_buffer(&video->queue, b);
407 uvc_v4l2_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
409 struct video_device *vdev = video_devdata(file);
410 struct uvc_device *uvc = video_get_drvdata(vdev);
411 struct uvc_video *video = &uvc->video;
414 ret = uvcg_queue_buffer(&video->queue, b);
418 if (uvc->state == UVC_STATE_STREAMING)
419 queue_work(video->async_wq, &video->pump);
425 uvc_v4l2_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
427 struct video_device *vdev = video_devdata(file);
428 struct uvc_device *uvc = video_get_drvdata(vdev);
429 struct uvc_video *video = &uvc->video;
431 return uvcg_dequeue_buffer(&video->queue, b, file->f_flags & O_NONBLOCK);
435 uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
437 struct video_device *vdev = video_devdata(file);
438 struct uvc_device *uvc = video_get_drvdata(vdev);
439 struct uvc_video *video = &uvc->video;
442 if (type != video->queue.queue.type)
445 /* Enable UVC video. */
446 ret = uvcg_video_enable(video, 1);
451 * Complete the alternate setting selection setup phase now that
452 * userspace is ready to provide video frames.
454 uvc_function_setup_continue(uvc);
455 uvc->state = UVC_STATE_STREAMING;
461 uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
463 struct video_device *vdev = video_devdata(file);
464 struct uvc_device *uvc = video_get_drvdata(vdev);
465 struct uvc_video *video = &uvc->video;
467 if (type != video->queue.queue.type)
470 return uvcg_video_enable(video, 0);
474 uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
475 const struct v4l2_event_subscription *sub)
477 struct uvc_device *uvc = video_get_drvdata(fh->vdev);
478 struct uvc_file_handle *handle = to_uvc_file_handle(fh);
481 if (sub->type < UVC_EVENT_FIRST || sub->type > UVC_EVENT_LAST)
484 if (sub->type == UVC_EVENT_SETUP && uvc->func_connected)
487 ret = v4l2_event_subscribe(fh, sub, 2, NULL);
491 if (sub->type == UVC_EVENT_SETUP) {
492 uvc->func_connected = true;
493 handle->is_uvc_app_handle = true;
494 uvc_function_connect(uvc);
500 static void uvc_v4l2_disable(struct uvc_device *uvc)
502 uvc_function_disconnect(uvc);
503 uvcg_video_enable(&uvc->video, 0);
504 uvcg_free_buffers(&uvc->video.queue);
505 uvc->func_connected = false;
506 wake_up_interruptible(&uvc->func_connected_queue);
510 uvc_v4l2_unsubscribe_event(struct v4l2_fh *fh,
511 const struct v4l2_event_subscription *sub)
513 struct uvc_device *uvc = video_get_drvdata(fh->vdev);
514 struct uvc_file_handle *handle = to_uvc_file_handle(fh);
517 ret = v4l2_event_unsubscribe(fh, sub);
521 if (sub->type == UVC_EVENT_SETUP && handle->is_uvc_app_handle) {
522 uvc_v4l2_disable(uvc);
523 handle->is_uvc_app_handle = false;
530 uvc_v4l2_ioctl_default(struct file *file, void *fh, bool valid_prio,
531 unsigned int cmd, void *arg)
533 struct video_device *vdev = video_devdata(file);
534 struct uvc_device *uvc = video_get_drvdata(vdev);
537 case UVCIOC_SEND_RESPONSE:
538 return uvc_send_response(uvc, arg);
545 const struct v4l2_ioctl_ops uvc_v4l2_ioctl_ops = {
546 .vidioc_querycap = uvc_v4l2_querycap,
547 .vidioc_try_fmt_vid_out = uvc_v4l2_try_format,
548 .vidioc_g_fmt_vid_out = uvc_v4l2_get_format,
549 .vidioc_s_fmt_vid_out = uvc_v4l2_set_format,
550 .vidioc_enum_frameintervals = uvc_v4l2_enum_frameintervals,
551 .vidioc_enum_framesizes = uvc_v4l2_enum_framesizes,
552 .vidioc_enum_fmt_vid_out = uvc_v4l2_enum_format,
553 .vidioc_reqbufs = uvc_v4l2_reqbufs,
554 .vidioc_querybuf = uvc_v4l2_querybuf,
555 .vidioc_qbuf = uvc_v4l2_qbuf,
556 .vidioc_dqbuf = uvc_v4l2_dqbuf,
557 .vidioc_streamon = uvc_v4l2_streamon,
558 .vidioc_streamoff = uvc_v4l2_streamoff,
559 .vidioc_subscribe_event = uvc_v4l2_subscribe_event,
560 .vidioc_unsubscribe_event = uvc_v4l2_unsubscribe_event,
561 .vidioc_default = uvc_v4l2_ioctl_default,
564 /* --------------------------------------------------------------------------
569 uvc_v4l2_open(struct file *file)
571 struct video_device *vdev = video_devdata(file);
572 struct uvc_device *uvc = video_get_drvdata(vdev);
573 struct uvc_file_handle *handle;
575 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
579 v4l2_fh_init(&handle->vfh, vdev);
580 v4l2_fh_add(&handle->vfh);
582 handle->device = &uvc->video;
583 file->private_data = &handle->vfh;
589 uvc_v4l2_release(struct file *file)
591 struct video_device *vdev = video_devdata(file);
592 struct uvc_device *uvc = video_get_drvdata(vdev);
593 struct uvc_file_handle *handle = to_uvc_file_handle(file->private_data);
594 struct uvc_video *video = handle->device;
596 mutex_lock(&video->mutex);
597 if (handle->is_uvc_app_handle)
598 uvc_v4l2_disable(uvc);
599 mutex_unlock(&video->mutex);
601 file->private_data = NULL;
602 v4l2_fh_del(&handle->vfh);
603 v4l2_fh_exit(&handle->vfh);
610 uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
612 struct video_device *vdev = video_devdata(file);
613 struct uvc_device *uvc = video_get_drvdata(vdev);
615 return uvcg_queue_mmap(&uvc->video.queue, vma);
619 uvc_v4l2_poll(struct file *file, poll_table *wait)
621 struct video_device *vdev = video_devdata(file);
622 struct uvc_device *uvc = video_get_drvdata(vdev);
624 return uvcg_queue_poll(&uvc->video.queue, file, wait);
628 static unsigned long uvcg_v4l2_get_unmapped_area(struct file *file,
629 unsigned long addr, unsigned long len, unsigned long pgoff,
632 struct video_device *vdev = video_devdata(file);
633 struct uvc_device *uvc = video_get_drvdata(vdev);
635 return uvcg_queue_get_unmapped_area(&uvc->video.queue, pgoff);
639 const struct v4l2_file_operations uvc_v4l2_fops = {
640 .owner = THIS_MODULE,
641 .open = uvc_v4l2_open,
642 .release = uvc_v4l2_release,
643 .unlocked_ioctl = video_ioctl2,
644 .mmap = uvc_v4l2_mmap,
645 .poll = uvc_v4l2_poll,
647 .get_unmapped_area = uvcg_v4l2_get_unmapped_area,