2 * uvc_v4l2.c -- USB Video Class driver - V4L2 API
4 * Copyright (C) 2005-2010
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
14 #include <linux/compat.h>
15 #include <linux/kernel.h>
16 #include <linux/list.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/usb.h>
20 #include <linux/videodev2.h>
21 #include <linux/vmalloc.h>
23 #include <linux/wait.h>
24 #include <linux/atomic.h>
26 #include <media/v4l2-common.h>
27 #include <media/v4l2-ctrls.h>
28 #include <media/v4l2-event.h>
29 #include <media/v4l2-ioctl.h>
33 /* ------------------------------------------------------------------------
36 static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain,
37 struct uvc_xu_control_mapping *xmap)
39 struct uvc_control_mapping *map;
43 map = kzalloc(sizeof(*map), GFP_KERNEL);
48 memcpy(map->name, xmap->name, sizeof(map->name));
49 memcpy(map->entity, xmap->entity, sizeof(map->entity));
50 map->selector = xmap->selector;
51 map->size = xmap->size;
52 map->offset = xmap->offset;
53 map->v4l2_type = xmap->v4l2_type;
54 map->data_type = xmap->data_type;
56 switch (xmap->v4l2_type) {
57 case V4L2_CTRL_TYPE_INTEGER:
58 case V4L2_CTRL_TYPE_BOOLEAN:
59 case V4L2_CTRL_TYPE_BUTTON:
62 case V4L2_CTRL_TYPE_MENU:
63 /* Prevent excessive memory consumption, as well as integer
66 if (xmap->menu_count == 0 ||
67 xmap->menu_count > UVC_MAX_CONTROL_MENU_ENTRIES) {
72 size = xmap->menu_count * sizeof(*map->menu_info);
73 map->menu_info = memdup_user(xmap->menu_info, size);
74 if (IS_ERR(map->menu_info)) {
75 ret = PTR_ERR(map->menu_info);
79 map->menu_count = xmap->menu_count;
83 uvc_trace(UVC_TRACE_CONTROL, "Unsupported V4L2 control type "
84 "%u.\n", xmap->v4l2_type);
89 ret = uvc_ctrl_add_mapping(chain, map);
91 kfree(map->menu_info);
98 /* ------------------------------------------------------------------------
103 * Find the frame interval closest to the requested frame interval for the
104 * given frame format and size. This should be done by the device as part of
105 * the Video Probe and Commit negotiation, but some hardware don't implement
108 static u32 uvc_try_frame_interval(struct uvc_frame *frame, u32 interval)
112 if (frame->bFrameIntervalType) {
115 for (i = 0; i < frame->bFrameIntervalType; ++i) {
116 dist = interval > frame->dwFrameInterval[i]
117 ? interval - frame->dwFrameInterval[i]
118 : frame->dwFrameInterval[i] - interval;
126 interval = frame->dwFrameInterval[i-1];
128 const u32 min = frame->dwFrameInterval[0];
129 const u32 max = frame->dwFrameInterval[1];
130 const u32 step = frame->dwFrameInterval[2];
132 interval = min + (interval - min + step/2) / step * step;
140 static u32 uvc_v4l2_get_bytesperline(const struct uvc_format *format,
141 const struct uvc_frame *frame)
143 switch (format->fcc) {
144 case V4L2_PIX_FMT_NV12:
145 case V4L2_PIX_FMT_YVU420:
146 case V4L2_PIX_FMT_YUV420:
147 case V4L2_PIX_FMT_M420:
148 return frame->wWidth;
151 return format->bpp * frame->wWidth / 8;
155 static int uvc_v4l2_try_format(struct uvc_streaming *stream,
156 struct v4l2_format *fmt, struct uvc_streaming_control *probe,
157 struct uvc_format **uvc_format, struct uvc_frame **uvc_frame)
159 struct uvc_format *format = NULL;
160 struct uvc_frame *frame = NULL;
162 unsigned int d, maxd;
168 if (fmt->type != stream->type)
171 fcc = (u8 *)&fmt->fmt.pix.pixelformat;
172 uvc_trace(UVC_TRACE_FORMAT, "Trying format 0x%08x (%c%c%c%c): %ux%u.\n",
173 fmt->fmt.pix.pixelformat,
174 fcc[0], fcc[1], fcc[2], fcc[3],
175 fmt->fmt.pix.width, fmt->fmt.pix.height);
177 /* Check if the hardware supports the requested format, use the default
180 for (i = 0; i < stream->nformats; ++i) {
181 format = &stream->format[i];
182 if (format->fcc == fmt->fmt.pix.pixelformat)
186 if (i == stream->nformats) {
187 format = stream->def_format;
188 fmt->fmt.pix.pixelformat = format->fcc;
191 /* Find the closest image size. The distance between image sizes is
192 * the size in pixels of the non-overlapping regions between the
193 * requested size and the frame-specified size.
195 rw = fmt->fmt.pix.width;
196 rh = fmt->fmt.pix.height;
197 maxd = (unsigned int)-1;
199 for (i = 0; i < format->nframes; ++i) {
200 u16 w = format->frame[i].wWidth;
201 u16 h = format->frame[i].wHeight;
203 d = min(w, rw) * min(h, rh);
204 d = w*h + rw*rh - 2*d;
207 frame = &format->frame[i];
215 uvc_trace(UVC_TRACE_FORMAT, "Unsupported size %ux%u.\n",
216 fmt->fmt.pix.width, fmt->fmt.pix.height);
220 /* Use the default frame interval. */
221 interval = frame->dwDefaultFrameInterval;
222 uvc_trace(UVC_TRACE_FORMAT, "Using default frame interval %u.%u us "
223 "(%u.%u fps).\n", interval/10, interval%10, 10000000/interval,
224 (100000000/interval)%10);
226 /* Set the format index, frame index and frame interval. */
227 memset(probe, 0, sizeof(*probe));
228 probe->bmHint = 1; /* dwFrameInterval */
229 probe->bFormatIndex = format->index;
230 probe->bFrameIndex = frame->bFrameIndex;
231 probe->dwFrameInterval = uvc_try_frame_interval(frame, interval);
232 /* Some webcams stall the probe control set request when the
233 * dwMaxVideoFrameSize field is set to zero. The UVC specification
234 * clearly states that the field is read-only from the host, so this
235 * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by
236 * the webcam to work around the problem.
238 * The workaround could probably be enabled for all webcams, so the
239 * quirk can be removed if needed. It's currently useful to detect
240 * webcam bugs and fix them before they hit the market (providing
241 * developers test their webcams with the Linux driver as well as with
242 * the Windows driver).
244 mutex_lock(&stream->mutex);
245 if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS)
246 probe->dwMaxVideoFrameSize =
247 stream->ctrl.dwMaxVideoFrameSize;
249 /* Probe the device. */
250 ret = uvc_probe_video(stream, probe);
251 mutex_unlock(&stream->mutex);
255 fmt->fmt.pix.width = frame->wWidth;
256 fmt->fmt.pix.height = frame->wHeight;
257 fmt->fmt.pix.field = V4L2_FIELD_NONE;
258 fmt->fmt.pix.bytesperline = uvc_v4l2_get_bytesperline(format, frame);
259 fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize;
260 fmt->fmt.pix.colorspace = format->colorspace;
261 fmt->fmt.pix.priv = 0;
263 if (uvc_format != NULL)
264 *uvc_format = format;
265 if (uvc_frame != NULL)
272 static int uvc_v4l2_get_format(struct uvc_streaming *stream,
273 struct v4l2_format *fmt)
275 struct uvc_format *format;
276 struct uvc_frame *frame;
279 if (fmt->type != stream->type)
282 mutex_lock(&stream->mutex);
283 format = stream->cur_format;
284 frame = stream->cur_frame;
286 if (format == NULL || frame == NULL) {
291 fmt->fmt.pix.pixelformat = format->fcc;
292 fmt->fmt.pix.width = frame->wWidth;
293 fmt->fmt.pix.height = frame->wHeight;
294 fmt->fmt.pix.field = V4L2_FIELD_NONE;
295 fmt->fmt.pix.bytesperline = uvc_v4l2_get_bytesperline(format, frame);
296 fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize;
297 fmt->fmt.pix.colorspace = format->colorspace;
298 fmt->fmt.pix.priv = 0;
301 mutex_unlock(&stream->mutex);
305 static int uvc_v4l2_set_format(struct uvc_streaming *stream,
306 struct v4l2_format *fmt)
308 struct uvc_streaming_control probe;
309 struct uvc_format *format;
310 struct uvc_frame *frame;
313 if (fmt->type != stream->type)
316 ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame);
320 mutex_lock(&stream->mutex);
322 if (uvc_queue_allocated(&stream->queue)) {
327 stream->ctrl = probe;
328 stream->cur_format = format;
329 stream->cur_frame = frame;
332 mutex_unlock(&stream->mutex);
336 static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream,
337 struct v4l2_streamparm *parm)
339 u32 numerator, denominator;
341 if (parm->type != stream->type)
344 mutex_lock(&stream->mutex);
345 numerator = stream->ctrl.dwFrameInterval;
346 mutex_unlock(&stream->mutex);
348 denominator = 10000000;
349 uvc_simplify_fraction(&numerator, &denominator, 8, 333);
351 memset(parm, 0, sizeof(*parm));
352 parm->type = stream->type;
354 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
355 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
356 parm->parm.capture.capturemode = 0;
357 parm->parm.capture.timeperframe.numerator = numerator;
358 parm->parm.capture.timeperframe.denominator = denominator;
359 parm->parm.capture.extendedmode = 0;
360 parm->parm.capture.readbuffers = 0;
362 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
363 parm->parm.output.outputmode = 0;
364 parm->parm.output.timeperframe.numerator = numerator;
365 parm->parm.output.timeperframe.denominator = denominator;
371 static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
372 struct v4l2_streamparm *parm)
374 struct uvc_streaming_control probe;
375 struct v4l2_fract timeperframe;
376 struct uvc_format *format;
377 struct uvc_frame *frame;
382 if (parm->type != stream->type)
385 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
386 timeperframe = parm->parm.capture.timeperframe;
388 timeperframe = parm->parm.output.timeperframe;
390 interval = uvc_fraction_to_interval(timeperframe.numerator,
391 timeperframe.denominator);
392 uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
393 timeperframe.numerator, timeperframe.denominator, interval);
395 mutex_lock(&stream->mutex);
397 if (uvc_queue_streaming(&stream->queue)) {
398 mutex_unlock(&stream->mutex);
402 format = stream->cur_format;
403 frame = stream->cur_frame;
404 probe = stream->ctrl;
405 probe.dwFrameInterval = uvc_try_frame_interval(frame, interval);
406 maxd = abs((s32)probe.dwFrameInterval - interval);
408 /* Try frames with matching size to find the best frame interval. */
409 for (i = 0; i < format->nframes && maxd != 0; i++) {
412 if (&format->frame[i] == stream->cur_frame)
415 if (format->frame[i].wWidth != stream->cur_frame->wWidth ||
416 format->frame[i].wHeight != stream->cur_frame->wHeight)
419 ival = uvc_try_frame_interval(&format->frame[i], interval);
420 d = abs((s32)ival - interval);
424 frame = &format->frame[i];
425 probe.bFrameIndex = frame->bFrameIndex;
426 probe.dwFrameInterval = ival;
430 /* Probe the device with the new settings. */
431 ret = uvc_probe_video(stream, &probe);
433 mutex_unlock(&stream->mutex);
437 stream->ctrl = probe;
438 stream->cur_frame = frame;
439 mutex_unlock(&stream->mutex);
441 /* Return the actual frame period. */
442 timeperframe.numerator = probe.dwFrameInterval;
443 timeperframe.denominator = 10000000;
444 uvc_simplify_fraction(&timeperframe.numerator,
445 &timeperframe.denominator, 8, 333);
447 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
448 parm->parm.capture.timeperframe = timeperframe;
450 parm->parm.output.timeperframe = timeperframe;
455 /* ------------------------------------------------------------------------
456 * Privilege management
460 * Privilege management is the multiple-open implementation basis. The current
461 * implementation is completely transparent for the end-user and doesn't
462 * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls.
463 * Those ioctls enable finer control on the device (by making possible for a
464 * user to request exclusive access to a device), but are not mature yet.
465 * Switching to the V4L2 priority mechanism might be considered in the future
466 * if this situation changes.
468 * Each open instance of a UVC device can either be in a privileged or
469 * unprivileged state. Only a single instance can be in a privileged state at
470 * a given time. Trying to perform an operation that requires privileges will
471 * automatically acquire the required privileges if possible, or return -EBUSY
472 * otherwise. Privileges are dismissed when closing the instance or when
473 * freeing the video buffers using VIDIOC_REQBUFS.
475 * Operations that require privileges are:
482 static int uvc_acquire_privileges(struct uvc_fh *handle)
484 /* Always succeed if the handle is already privileged. */
485 if (handle->state == UVC_HANDLE_ACTIVE)
488 /* Check if the device already has a privileged handle. */
489 if (atomic_inc_return(&handle->stream->active) != 1) {
490 atomic_dec(&handle->stream->active);
494 handle->state = UVC_HANDLE_ACTIVE;
498 static void uvc_dismiss_privileges(struct uvc_fh *handle)
500 if (handle->state == UVC_HANDLE_ACTIVE)
501 atomic_dec(&handle->stream->active);
503 handle->state = UVC_HANDLE_PASSIVE;
506 static int uvc_has_privileges(struct uvc_fh *handle)
508 return handle->state == UVC_HANDLE_ACTIVE;
511 /* ------------------------------------------------------------------------
512 * V4L2 file operations
515 static int uvc_v4l2_open(struct file *file)
517 struct uvc_streaming *stream;
518 struct uvc_fh *handle;
521 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
522 stream = video_drvdata(file);
524 ret = usb_autopm_get_interface(stream->dev->intf);
528 /* Create the device handle. */
529 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
530 if (handle == NULL) {
531 usb_autopm_put_interface(stream->dev->intf);
535 mutex_lock(&stream->dev->lock);
536 if (stream->dev->users == 0) {
537 ret = uvc_status_start(stream->dev, GFP_KERNEL);
539 mutex_unlock(&stream->dev->lock);
540 usb_autopm_put_interface(stream->dev->intf);
546 stream->dev->users++;
547 mutex_unlock(&stream->dev->lock);
549 v4l2_fh_init(&handle->vfh, &stream->vdev);
550 v4l2_fh_add(&handle->vfh);
551 handle->chain = stream->chain;
552 handle->stream = stream;
553 handle->state = UVC_HANDLE_PASSIVE;
554 file->private_data = handle;
559 static int uvc_v4l2_release(struct file *file)
561 struct uvc_fh *handle = file->private_data;
562 struct uvc_streaming *stream = handle->stream;
564 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");
566 /* Only free resources if this is a privileged handle. */
567 if (uvc_has_privileges(handle))
568 uvc_queue_release(&stream->queue);
570 /* Release the file handle. */
571 uvc_dismiss_privileges(handle);
572 v4l2_fh_del(&handle->vfh);
573 v4l2_fh_exit(&handle->vfh);
575 file->private_data = NULL;
577 mutex_lock(&stream->dev->lock);
578 if (--stream->dev->users == 0)
579 uvc_status_stop(stream->dev);
580 mutex_unlock(&stream->dev->lock);
582 usb_autopm_put_interface(stream->dev->intf);
586 static int uvc_ioctl_querycap(struct file *file, void *fh,
587 struct v4l2_capability *cap)
589 struct video_device *vdev = video_devdata(file);
590 struct uvc_fh *handle = file->private_data;
591 struct uvc_video_chain *chain = handle->chain;
592 struct uvc_streaming *stream = handle->stream;
594 strscpy(cap->driver, "uvcvideo", sizeof(cap->driver));
595 strscpy(cap->card, vdev->name, sizeof(cap->card));
596 usb_make_path(stream->dev->udev, cap->bus_info, sizeof(cap->bus_info));
597 cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
603 static int uvc_ioctl_enum_fmt(struct uvc_streaming *stream,
604 struct v4l2_fmtdesc *fmt)
606 struct uvc_format *format;
607 enum v4l2_buf_type type = fmt->type;
608 u32 index = fmt->index;
610 if (fmt->type != stream->type || fmt->index >= stream->nformats)
613 memset(fmt, 0, sizeof(*fmt));
617 format = &stream->format[fmt->index];
619 if (format->flags & UVC_FMT_FLAG_COMPRESSED)
620 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
621 strscpy(fmt->description, format->name, sizeof(fmt->description));
622 fmt->description[sizeof(fmt->description) - 1] = 0;
623 fmt->pixelformat = format->fcc;
627 static int uvc_ioctl_enum_fmt_vid_cap(struct file *file, void *fh,
628 struct v4l2_fmtdesc *fmt)
630 struct uvc_fh *handle = fh;
631 struct uvc_streaming *stream = handle->stream;
633 return uvc_ioctl_enum_fmt(stream, fmt);
636 static int uvc_ioctl_enum_fmt_vid_out(struct file *file, void *fh,
637 struct v4l2_fmtdesc *fmt)
639 struct uvc_fh *handle = fh;
640 struct uvc_streaming *stream = handle->stream;
642 return uvc_ioctl_enum_fmt(stream, fmt);
645 static int uvc_ioctl_g_fmt_vid_cap(struct file *file, void *fh,
646 struct v4l2_format *fmt)
648 struct uvc_fh *handle = fh;
649 struct uvc_streaming *stream = handle->stream;
651 return uvc_v4l2_get_format(stream, fmt);
654 static int uvc_ioctl_g_fmt_vid_out(struct file *file, void *fh,
655 struct v4l2_format *fmt)
657 struct uvc_fh *handle = fh;
658 struct uvc_streaming *stream = handle->stream;
660 return uvc_v4l2_get_format(stream, fmt);
663 static int uvc_ioctl_s_fmt_vid_cap(struct file *file, void *fh,
664 struct v4l2_format *fmt)
666 struct uvc_fh *handle = fh;
667 struct uvc_streaming *stream = handle->stream;
670 ret = uvc_acquire_privileges(handle);
674 return uvc_v4l2_set_format(stream, fmt);
677 static int uvc_ioctl_s_fmt_vid_out(struct file *file, void *fh,
678 struct v4l2_format *fmt)
680 struct uvc_fh *handle = fh;
681 struct uvc_streaming *stream = handle->stream;
684 ret = uvc_acquire_privileges(handle);
688 return uvc_v4l2_set_format(stream, fmt);
691 static int uvc_ioctl_try_fmt_vid_cap(struct file *file, void *fh,
692 struct v4l2_format *fmt)
694 struct uvc_fh *handle = fh;
695 struct uvc_streaming *stream = handle->stream;
696 struct uvc_streaming_control probe;
698 return uvc_v4l2_try_format(stream, fmt, &probe, NULL, NULL);
701 static int uvc_ioctl_try_fmt_vid_out(struct file *file, void *fh,
702 struct v4l2_format *fmt)
704 struct uvc_fh *handle = fh;
705 struct uvc_streaming *stream = handle->stream;
706 struct uvc_streaming_control probe;
708 return uvc_v4l2_try_format(stream, fmt, &probe, NULL, NULL);
711 static int uvc_ioctl_reqbufs(struct file *file, void *fh,
712 struct v4l2_requestbuffers *rb)
714 struct uvc_fh *handle = fh;
715 struct uvc_streaming *stream = handle->stream;
718 ret = uvc_acquire_privileges(handle);
722 mutex_lock(&stream->mutex);
723 ret = uvc_request_buffers(&stream->queue, rb);
724 mutex_unlock(&stream->mutex);
729 uvc_dismiss_privileges(handle);
734 static int uvc_ioctl_querybuf(struct file *file, void *fh,
735 struct v4l2_buffer *buf)
737 struct uvc_fh *handle = fh;
738 struct uvc_streaming *stream = handle->stream;
740 if (!uvc_has_privileges(handle))
743 return uvc_query_buffer(&stream->queue, buf);
746 static int uvc_ioctl_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
748 struct uvc_fh *handle = fh;
749 struct uvc_streaming *stream = handle->stream;
751 if (!uvc_has_privileges(handle))
754 return uvc_queue_buffer(&stream->queue,
755 stream->vdev.v4l2_dev->mdev, buf);
758 static int uvc_ioctl_expbuf(struct file *file, void *fh,
759 struct v4l2_exportbuffer *exp)
761 struct uvc_fh *handle = fh;
762 struct uvc_streaming *stream = handle->stream;
764 if (!uvc_has_privileges(handle))
767 return uvc_export_buffer(&stream->queue, exp);
770 static int uvc_ioctl_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
772 struct uvc_fh *handle = fh;
773 struct uvc_streaming *stream = handle->stream;
775 if (!uvc_has_privileges(handle))
778 return uvc_dequeue_buffer(&stream->queue, buf,
779 file->f_flags & O_NONBLOCK);
782 static int uvc_ioctl_create_bufs(struct file *file, void *fh,
783 struct v4l2_create_buffers *cb)
785 struct uvc_fh *handle = fh;
786 struct uvc_streaming *stream = handle->stream;
789 ret = uvc_acquire_privileges(handle);
793 return uvc_create_buffers(&stream->queue, cb);
796 static int uvc_ioctl_streamon(struct file *file, void *fh,
797 enum v4l2_buf_type type)
799 struct uvc_fh *handle = fh;
800 struct uvc_streaming *stream = handle->stream;
803 if (!uvc_has_privileges(handle))
806 mutex_lock(&stream->mutex);
807 ret = uvc_queue_streamon(&stream->queue, type);
808 mutex_unlock(&stream->mutex);
813 static int uvc_ioctl_streamoff(struct file *file, void *fh,
814 enum v4l2_buf_type type)
816 struct uvc_fh *handle = fh;
817 struct uvc_streaming *stream = handle->stream;
819 if (!uvc_has_privileges(handle))
822 mutex_lock(&stream->mutex);
823 uvc_queue_streamoff(&stream->queue, type);
824 mutex_unlock(&stream->mutex);
829 static int uvc_ioctl_enum_input(struct file *file, void *fh,
830 struct v4l2_input *input)
832 struct uvc_fh *handle = fh;
833 struct uvc_video_chain *chain = handle->chain;
834 const struct uvc_entity *selector = chain->selector;
835 struct uvc_entity *iterm = NULL;
836 u32 index = input->index;
839 if (selector == NULL ||
840 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
843 list_for_each_entry(iterm, &chain->entities, chain) {
844 if (UVC_ENTITY_IS_ITERM(iterm))
848 } else if (index < selector->bNrInPins) {
849 pin = selector->baSourceID[index];
850 list_for_each_entry(iterm, &chain->entities, chain) {
851 if (!UVC_ENTITY_IS_ITERM(iterm))
853 if (iterm->id == pin)
858 if (iterm == NULL || iterm->id != pin)
861 memset(input, 0, sizeof(*input));
862 input->index = index;
863 strscpy(input->name, iterm->name, sizeof(input->name));
864 if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
865 input->type = V4L2_INPUT_TYPE_CAMERA;
870 static int uvc_ioctl_g_input(struct file *file, void *fh, unsigned int *input)
872 struct uvc_fh *handle = fh;
873 struct uvc_video_chain *chain = handle->chain;
877 if (chain->selector == NULL ||
878 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
883 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, chain->selector->id,
884 chain->dev->intfnum, UVC_SU_INPUT_SELECT_CONTROL,
893 static int uvc_ioctl_s_input(struct file *file, void *fh, unsigned int input)
895 struct uvc_fh *handle = fh;
896 struct uvc_video_chain *chain = handle->chain;
900 ret = uvc_acquire_privileges(handle);
904 if (chain->selector == NULL ||
905 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
911 if (input >= chain->selector->bNrInPins)
915 return uvc_query_ctrl(chain->dev, UVC_SET_CUR, chain->selector->id,
916 chain->dev->intfnum, UVC_SU_INPUT_SELECT_CONTROL,
920 static int uvc_ioctl_queryctrl(struct file *file, void *fh,
921 struct v4l2_queryctrl *qc)
923 struct uvc_fh *handle = fh;
924 struct uvc_video_chain *chain = handle->chain;
926 return uvc_query_v4l2_ctrl(chain, qc);
929 static int uvc_ioctl_query_ext_ctrl(struct file *file, void *fh,
930 struct v4l2_query_ext_ctrl *qec)
932 struct uvc_fh *handle = fh;
933 struct uvc_video_chain *chain = handle->chain;
934 struct v4l2_queryctrl qc = { qec->id };
937 ret = uvc_query_v4l2_ctrl(chain, &qc);
943 strscpy(qec->name, qc.name, sizeof(qec->name));
944 qec->minimum = qc.minimum;
945 qec->maximum = qc.maximum;
947 qec->default_value = qc.default_value;
948 qec->flags = qc.flags;
952 memset(qec->dims, 0, sizeof(qec->dims));
953 memset(qec->reserved, 0, sizeof(qec->reserved));
958 static int uvc_ioctl_g_ctrl(struct file *file, void *fh,
959 struct v4l2_control *ctrl)
961 struct uvc_fh *handle = fh;
962 struct uvc_video_chain *chain = handle->chain;
963 struct v4l2_ext_control xctrl;
966 memset(&xctrl, 0, sizeof(xctrl));
969 ret = uvc_ctrl_begin(chain);
973 ret = uvc_ctrl_get(chain, &xctrl);
974 uvc_ctrl_rollback(handle);
978 ctrl->value = xctrl.value;
982 static int uvc_ioctl_s_ctrl(struct file *file, void *fh,
983 struct v4l2_control *ctrl)
985 struct uvc_fh *handle = fh;
986 struct uvc_video_chain *chain = handle->chain;
987 struct v4l2_ext_control xctrl;
990 memset(&xctrl, 0, sizeof(xctrl));
992 xctrl.value = ctrl->value;
994 ret = uvc_ctrl_begin(chain);
998 ret = uvc_ctrl_set(handle, &xctrl);
1000 uvc_ctrl_rollback(handle);
1004 ret = uvc_ctrl_commit(handle, &xctrl, 1);
1008 ctrl->value = xctrl.value;
1012 static int uvc_ioctl_g_ext_ctrls(struct file *file, void *fh,
1013 struct v4l2_ext_controls *ctrls)
1015 struct uvc_fh *handle = fh;
1016 struct uvc_video_chain *chain = handle->chain;
1017 struct v4l2_ext_control *ctrl = ctrls->controls;
1021 if (ctrls->which == V4L2_CTRL_WHICH_DEF_VAL) {
1022 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
1023 struct v4l2_queryctrl qc = { .id = ctrl->id };
1025 ret = uvc_query_v4l2_ctrl(chain, &qc);
1027 ctrls->error_idx = i;
1031 ctrl->value = qc.default_value;
1037 ret = uvc_ctrl_begin(chain);
1041 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
1042 ret = uvc_ctrl_get(chain, ctrl);
1044 uvc_ctrl_rollback(handle);
1045 ctrls->error_idx = i;
1050 ctrls->error_idx = 0;
1052 return uvc_ctrl_rollback(handle);
1055 static int uvc_ioctl_s_try_ext_ctrls(struct uvc_fh *handle,
1056 struct v4l2_ext_controls *ctrls,
1059 struct v4l2_ext_control *ctrl = ctrls->controls;
1060 struct uvc_video_chain *chain = handle->chain;
1064 /* Default value cannot be changed */
1065 if (ctrls->which == V4L2_CTRL_WHICH_DEF_VAL)
1068 ret = uvc_ctrl_begin(chain);
1072 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
1073 ret = uvc_ctrl_set(handle, ctrl);
1075 uvc_ctrl_rollback(handle);
1076 ctrls->error_idx = commit ? ctrls->count : i;
1081 ctrls->error_idx = 0;
1084 return uvc_ctrl_commit(handle, ctrls->controls, ctrls->count);
1086 return uvc_ctrl_rollback(handle);
1089 static int uvc_ioctl_s_ext_ctrls(struct file *file, void *fh,
1090 struct v4l2_ext_controls *ctrls)
1092 struct uvc_fh *handle = fh;
1094 return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, true);
1097 static int uvc_ioctl_try_ext_ctrls(struct file *file, void *fh,
1098 struct v4l2_ext_controls *ctrls)
1100 struct uvc_fh *handle = fh;
1102 return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, false);
1105 static int uvc_ioctl_querymenu(struct file *file, void *fh,
1106 struct v4l2_querymenu *qm)
1108 struct uvc_fh *handle = fh;
1109 struct uvc_video_chain *chain = handle->chain;
1111 return uvc_query_v4l2_menu(chain, qm);
1114 static int uvc_ioctl_g_selection(struct file *file, void *fh,
1115 struct v4l2_selection *sel)
1117 struct uvc_fh *handle = fh;
1118 struct uvc_streaming *stream = handle->stream;
1120 if (sel->type != stream->type)
1123 switch (sel->target) {
1124 case V4L2_SEL_TGT_CROP_DEFAULT:
1125 case V4L2_SEL_TGT_CROP_BOUNDS:
1126 if (stream->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1129 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1130 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1131 if (stream->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1140 mutex_lock(&stream->mutex);
1141 sel->r.width = stream->cur_frame->wWidth;
1142 sel->r.height = stream->cur_frame->wHeight;
1143 mutex_unlock(&stream->mutex);
1148 static int uvc_ioctl_g_parm(struct file *file, void *fh,
1149 struct v4l2_streamparm *parm)
1151 struct uvc_fh *handle = fh;
1152 struct uvc_streaming *stream = handle->stream;
1154 return uvc_v4l2_get_streamparm(stream, parm);
1157 static int uvc_ioctl_s_parm(struct file *file, void *fh,
1158 struct v4l2_streamparm *parm)
1160 struct uvc_fh *handle = fh;
1161 struct uvc_streaming *stream = handle->stream;
1164 ret = uvc_acquire_privileges(handle);
1168 return uvc_v4l2_set_streamparm(stream, parm);
1171 static int uvc_ioctl_enum_framesizes(struct file *file, void *fh,
1172 struct v4l2_frmsizeenum *fsize)
1174 struct uvc_fh *handle = fh;
1175 struct uvc_streaming *stream = handle->stream;
1176 struct uvc_format *format = NULL;
1177 struct uvc_frame *frame = NULL;
1181 /* Look for the given pixel format */
1182 for (i = 0; i < stream->nformats; i++) {
1183 if (stream->format[i].fcc == fsize->pixel_format) {
1184 format = &stream->format[i];
1191 /* Skip duplicate frame sizes */
1192 for (i = 0, index = 0; i < format->nframes; i++) {
1193 if (frame && frame->wWidth == format->frame[i].wWidth &&
1194 frame->wHeight == format->frame[i].wHeight)
1196 frame = &format->frame[i];
1197 if (index == fsize->index)
1202 if (i == format->nframes)
1205 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1206 fsize->discrete.width = frame->wWidth;
1207 fsize->discrete.height = frame->wHeight;
1211 static int uvc_ioctl_enum_frameintervals(struct file *file, void *fh,
1212 struct v4l2_frmivalenum *fival)
1214 struct uvc_fh *handle = fh;
1215 struct uvc_streaming *stream = handle->stream;
1216 struct uvc_format *format = NULL;
1217 struct uvc_frame *frame = NULL;
1218 unsigned int nintervals;
1222 /* Look for the given pixel format and frame size */
1223 for (i = 0; i < stream->nformats; i++) {
1224 if (stream->format[i].fcc == fival->pixel_format) {
1225 format = &stream->format[i];
1232 index = fival->index;
1233 for (i = 0; i < format->nframes; i++) {
1234 if (format->frame[i].wWidth == fival->width &&
1235 format->frame[i].wHeight == fival->height) {
1236 frame = &format->frame[i];
1237 nintervals = frame->bFrameIntervalType ?: 1;
1238 if (index < nintervals)
1240 index -= nintervals;
1243 if (i == format->nframes)
1246 if (frame->bFrameIntervalType) {
1247 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1248 fival->discrete.numerator =
1249 frame->dwFrameInterval[index];
1250 fival->discrete.denominator = 10000000;
1251 uvc_simplify_fraction(&fival->discrete.numerator,
1252 &fival->discrete.denominator, 8, 333);
1254 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
1255 fival->stepwise.min.numerator = frame->dwFrameInterval[0];
1256 fival->stepwise.min.denominator = 10000000;
1257 fival->stepwise.max.numerator = frame->dwFrameInterval[1];
1258 fival->stepwise.max.denominator = 10000000;
1259 fival->stepwise.step.numerator = frame->dwFrameInterval[2];
1260 fival->stepwise.step.denominator = 10000000;
1261 uvc_simplify_fraction(&fival->stepwise.min.numerator,
1262 &fival->stepwise.min.denominator, 8, 333);
1263 uvc_simplify_fraction(&fival->stepwise.max.numerator,
1264 &fival->stepwise.max.denominator, 8, 333);
1265 uvc_simplify_fraction(&fival->stepwise.step.numerator,
1266 &fival->stepwise.step.denominator, 8, 333);
1272 static int uvc_ioctl_subscribe_event(struct v4l2_fh *fh,
1273 const struct v4l2_event_subscription *sub)
1275 switch (sub->type) {
1276 case V4L2_EVENT_CTRL:
1277 return v4l2_event_subscribe(fh, sub, 0, &uvc_ctrl_sub_ev_ops);
1283 static long uvc_ioctl_default(struct file *file, void *fh, bool valid_prio,
1284 unsigned int cmd, void *arg)
1286 struct uvc_fh *handle = fh;
1287 struct uvc_video_chain *chain = handle->chain;
1290 /* Dynamic controls. */
1291 case UVCIOC_CTRL_MAP:
1292 return uvc_ioctl_ctrl_map(chain, arg);
1294 case UVCIOC_CTRL_QUERY:
1295 return uvc_xu_ctrl_query(chain, arg);
1302 #ifdef CONFIG_COMPAT
1303 struct uvc_xu_control_mapping32 {
1314 compat_caddr_t menu_info;
1320 static int uvc_v4l2_get_xu_mapping(struct uvc_xu_control_mapping *kp,
1321 const struct uvc_xu_control_mapping32 __user *up)
1323 struct uvc_xu_control_mapping32 *p = (void *)kp;
1324 compat_caddr_t info;
1327 if (copy_from_user(p, up, sizeof(*p)))
1330 count = p->menu_count;
1331 info = p->menu_info;
1333 memset(kp->reserved, 0, sizeof(kp->reserved));
1334 kp->menu_info = count ? compat_ptr(info) : NULL;
1335 kp->menu_count = count;
1339 static int uvc_v4l2_put_xu_mapping(const struct uvc_xu_control_mapping *kp,
1340 struct uvc_xu_control_mapping32 __user *up)
1342 if (copy_to_user(up, kp, offsetof(typeof(*up), menu_info)) ||
1343 put_user(kp->menu_count, &up->menu_count))
1346 if (clear_user(up->reserved, sizeof(up->reserved)))
1352 struct uvc_xu_control_query32 {
1357 compat_caddr_t data;
1360 static int uvc_v4l2_get_xu_query(struct uvc_xu_control_query *kp,
1361 const struct uvc_xu_control_query32 __user *up)
1363 struct uvc_xu_control_query32 v;
1365 if (copy_from_user(&v, up, sizeof(v)))
1368 *kp = (struct uvc_xu_control_query){
1370 .selector = v.selector,
1373 .data = v.size ? compat_ptr(v.data) : NULL
1378 static int uvc_v4l2_put_xu_query(const struct uvc_xu_control_query *kp,
1379 struct uvc_xu_control_query32 __user *up)
1381 if (copy_to_user(up, kp, offsetof(typeof(*up), data)))
1386 #define UVCIOC_CTRL_MAP32 _IOWR('u', 0x20, struct uvc_xu_control_mapping32)
1387 #define UVCIOC_CTRL_QUERY32 _IOWR('u', 0x21, struct uvc_xu_control_query32)
1389 static long uvc_v4l2_compat_ioctl32(struct file *file,
1390 unsigned int cmd, unsigned long arg)
1392 struct uvc_fh *handle = file->private_data;
1394 struct uvc_xu_control_mapping xmap;
1395 struct uvc_xu_control_query xqry;
1397 void __user *up = compat_ptr(arg);
1401 case UVCIOC_CTRL_MAP32:
1402 ret = uvc_v4l2_get_xu_mapping(&karg.xmap, up);
1405 ret = uvc_ioctl_ctrl_map(handle->chain, &karg.xmap);
1408 ret = uvc_v4l2_put_xu_mapping(&karg.xmap, up);
1414 case UVCIOC_CTRL_QUERY32:
1415 ret = uvc_v4l2_get_xu_query(&karg.xqry, up);
1418 ret = uvc_xu_ctrl_query(handle->chain, &karg.xqry);
1421 ret = uvc_v4l2_put_xu_query(&karg.xqry, up);
1427 return -ENOIOCTLCMD;
1434 static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1435 size_t count, loff_t *ppos)
1437 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
1441 static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1443 struct uvc_fh *handle = file->private_data;
1444 struct uvc_streaming *stream = handle->stream;
1446 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
1448 return uvc_queue_mmap(&stream->queue, vma);
1451 static __poll_t uvc_v4l2_poll(struct file *file, poll_table *wait)
1453 struct uvc_fh *handle = file->private_data;
1454 struct uvc_streaming *stream = handle->stream;
1456 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
1458 return uvc_queue_poll(&stream->queue, file, wait);
1462 static unsigned long uvc_v4l2_get_unmapped_area(struct file *file,
1463 unsigned long addr, unsigned long len, unsigned long pgoff,
1464 unsigned long flags)
1466 struct uvc_fh *handle = file->private_data;
1467 struct uvc_streaming *stream = handle->stream;
1469 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_get_unmapped_area\n");
1471 return uvc_queue_get_unmapped_area(&stream->queue, pgoff);
1475 const struct v4l2_ioctl_ops uvc_ioctl_ops = {
1476 .vidioc_querycap = uvc_ioctl_querycap,
1477 .vidioc_enum_fmt_vid_cap = uvc_ioctl_enum_fmt_vid_cap,
1478 .vidioc_enum_fmt_vid_out = uvc_ioctl_enum_fmt_vid_out,
1479 .vidioc_g_fmt_vid_cap = uvc_ioctl_g_fmt_vid_cap,
1480 .vidioc_g_fmt_vid_out = uvc_ioctl_g_fmt_vid_out,
1481 .vidioc_s_fmt_vid_cap = uvc_ioctl_s_fmt_vid_cap,
1482 .vidioc_s_fmt_vid_out = uvc_ioctl_s_fmt_vid_out,
1483 .vidioc_try_fmt_vid_cap = uvc_ioctl_try_fmt_vid_cap,
1484 .vidioc_try_fmt_vid_out = uvc_ioctl_try_fmt_vid_out,
1485 .vidioc_reqbufs = uvc_ioctl_reqbufs,
1486 .vidioc_querybuf = uvc_ioctl_querybuf,
1487 .vidioc_qbuf = uvc_ioctl_qbuf,
1488 .vidioc_expbuf = uvc_ioctl_expbuf,
1489 .vidioc_dqbuf = uvc_ioctl_dqbuf,
1490 .vidioc_create_bufs = uvc_ioctl_create_bufs,
1491 .vidioc_streamon = uvc_ioctl_streamon,
1492 .vidioc_streamoff = uvc_ioctl_streamoff,
1493 .vidioc_enum_input = uvc_ioctl_enum_input,
1494 .vidioc_g_input = uvc_ioctl_g_input,
1495 .vidioc_s_input = uvc_ioctl_s_input,
1496 .vidioc_queryctrl = uvc_ioctl_queryctrl,
1497 .vidioc_query_ext_ctrl = uvc_ioctl_query_ext_ctrl,
1498 .vidioc_g_ctrl = uvc_ioctl_g_ctrl,
1499 .vidioc_s_ctrl = uvc_ioctl_s_ctrl,
1500 .vidioc_g_ext_ctrls = uvc_ioctl_g_ext_ctrls,
1501 .vidioc_s_ext_ctrls = uvc_ioctl_s_ext_ctrls,
1502 .vidioc_try_ext_ctrls = uvc_ioctl_try_ext_ctrls,
1503 .vidioc_querymenu = uvc_ioctl_querymenu,
1504 .vidioc_g_selection = uvc_ioctl_g_selection,
1505 .vidioc_g_parm = uvc_ioctl_g_parm,
1506 .vidioc_s_parm = uvc_ioctl_s_parm,
1507 .vidioc_enum_framesizes = uvc_ioctl_enum_framesizes,
1508 .vidioc_enum_frameintervals = uvc_ioctl_enum_frameintervals,
1509 .vidioc_subscribe_event = uvc_ioctl_subscribe_event,
1510 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1511 .vidioc_default = uvc_ioctl_default,
1514 const struct v4l2_file_operations uvc_fops = {
1515 .owner = THIS_MODULE,
1516 .open = uvc_v4l2_open,
1517 .release = uvc_v4l2_release,
1518 .unlocked_ioctl = video_ioctl2,
1519 #ifdef CONFIG_COMPAT
1520 .compat_ioctl32 = uvc_v4l2_compat_ioctl32,
1522 .read = uvc_v4l2_read,
1523 .mmap = uvc_v4l2_mmap,
1524 .poll = uvc_v4l2_poll,
1526 .get_unmapped_area = uvc_v4l2_get_unmapped_area,