2 * uvc_v4l2.c -- USB Video Class driver - V4L2 API
4 * Copyright (C) 2005-2010
5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
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 = kmalloc(size, GFP_KERNEL);
74 if (map->menu_info == NULL) {
79 if (copy_from_user(map->menu_info, xmap->menu_info, size)) {
84 map->menu_count = xmap->menu_count;
88 uvc_trace(UVC_TRACE_CONTROL, "Unsupported V4L2 control type "
89 "%u.\n", xmap->v4l2_type);
94 ret = uvc_ctrl_add_mapping(chain, map);
97 kfree(map->menu_info);
103 /* ------------------------------------------------------------------------
108 * Find the frame interval closest to the requested frame interval for the
109 * given frame format and size. This should be done by the device as part of
110 * the Video Probe and Commit negotiation, but some hardware don't implement
113 static __u32 uvc_try_frame_interval(struct uvc_frame *frame, __u32 interval)
117 if (frame->bFrameIntervalType) {
118 __u32 best = -1, dist;
120 for (i = 0; i < frame->bFrameIntervalType; ++i) {
121 dist = interval > frame->dwFrameInterval[i]
122 ? interval - frame->dwFrameInterval[i]
123 : frame->dwFrameInterval[i] - interval;
131 interval = frame->dwFrameInterval[i-1];
133 const __u32 min = frame->dwFrameInterval[0];
134 const __u32 max = frame->dwFrameInterval[1];
135 const __u32 step = frame->dwFrameInterval[2];
137 interval = min + (interval - min + step/2) / step * step;
145 static __u32 uvc_v4l2_get_bytesperline(const struct uvc_format *format,
146 const struct uvc_frame *frame)
148 switch (format->fcc) {
149 case V4L2_PIX_FMT_NV12:
150 case V4L2_PIX_FMT_YVU420:
151 case V4L2_PIX_FMT_YUV420:
152 case V4L2_PIX_FMT_M420:
153 return frame->wWidth;
156 return format->bpp * frame->wWidth / 8;
160 static int uvc_v4l2_try_format(struct uvc_streaming *stream,
161 struct v4l2_format *fmt, struct uvc_streaming_control *probe,
162 struct uvc_format **uvc_format, struct uvc_frame **uvc_frame)
164 struct uvc_format *format = NULL;
165 struct uvc_frame *frame = NULL;
167 unsigned int d, maxd;
173 if (fmt->type != stream->type)
176 fcc = (__u8 *)&fmt->fmt.pix.pixelformat;
177 uvc_trace(UVC_TRACE_FORMAT, "Trying format 0x%08x (%c%c%c%c): %ux%u.\n",
178 fmt->fmt.pix.pixelformat,
179 fcc[0], fcc[1], fcc[2], fcc[3],
180 fmt->fmt.pix.width, fmt->fmt.pix.height);
182 /* Check if the hardware supports the requested format, use the default
185 for (i = 0; i < stream->nformats; ++i) {
186 format = &stream->format[i];
187 if (format->fcc == fmt->fmt.pix.pixelformat)
191 if (i == stream->nformats) {
192 format = stream->def_format;
193 fmt->fmt.pix.pixelformat = format->fcc;
196 /* Find the closest image size. The distance between image sizes is
197 * the size in pixels of the non-overlapping regions between the
198 * requested size and the frame-specified size.
200 rw = fmt->fmt.pix.width;
201 rh = fmt->fmt.pix.height;
202 maxd = (unsigned int)-1;
204 for (i = 0; i < format->nframes; ++i) {
205 __u16 w = format->frame[i].wWidth;
206 __u16 h = format->frame[i].wHeight;
208 d = min(w, rw) * min(h, rh);
209 d = w*h + rw*rh - 2*d;
212 frame = &format->frame[i];
220 uvc_trace(UVC_TRACE_FORMAT, "Unsupported size %ux%u.\n",
221 fmt->fmt.pix.width, fmt->fmt.pix.height);
225 /* Use the default frame interval. */
226 interval = frame->dwDefaultFrameInterval;
227 uvc_trace(UVC_TRACE_FORMAT, "Using default frame interval %u.%u us "
228 "(%u.%u fps).\n", interval/10, interval%10, 10000000/interval,
229 (100000000/interval)%10);
231 /* Set the format index, frame index and frame interval. */
232 memset(probe, 0, sizeof *probe);
233 probe->bmHint = 1; /* dwFrameInterval */
234 probe->bFormatIndex = format->index;
235 probe->bFrameIndex = frame->bFrameIndex;
236 probe->dwFrameInterval = uvc_try_frame_interval(frame, interval);
237 /* Some webcams stall the probe control set request when the
238 * dwMaxVideoFrameSize field is set to zero. The UVC specification
239 * clearly states that the field is read-only from the host, so this
240 * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by
241 * the webcam to work around the problem.
243 * The workaround could probably be enabled for all webcams, so the
244 * quirk can be removed if needed. It's currently useful to detect
245 * webcam bugs and fix them before they hit the market (providing
246 * developers test their webcams with the Linux driver as well as with
247 * the Windows driver).
249 mutex_lock(&stream->mutex);
250 if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS)
251 probe->dwMaxVideoFrameSize =
252 stream->ctrl.dwMaxVideoFrameSize;
254 /* Probe the device. */
255 ret = uvc_probe_video(stream, probe);
256 mutex_unlock(&stream->mutex);
260 fmt->fmt.pix.width = frame->wWidth;
261 fmt->fmt.pix.height = frame->wHeight;
262 fmt->fmt.pix.field = V4L2_FIELD_NONE;
263 fmt->fmt.pix.bytesperline = uvc_v4l2_get_bytesperline(format, frame);
264 fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize;
265 fmt->fmt.pix.colorspace = format->colorspace;
266 fmt->fmt.pix.priv = 0;
268 if (uvc_format != NULL)
269 *uvc_format = format;
270 if (uvc_frame != NULL)
277 static int uvc_v4l2_get_format(struct uvc_streaming *stream,
278 struct v4l2_format *fmt)
280 struct uvc_format *format;
281 struct uvc_frame *frame;
284 if (fmt->type != stream->type)
287 mutex_lock(&stream->mutex);
288 format = stream->cur_format;
289 frame = stream->cur_frame;
291 if (format == NULL || frame == NULL) {
296 fmt->fmt.pix.pixelformat = format->fcc;
297 fmt->fmt.pix.width = frame->wWidth;
298 fmt->fmt.pix.height = frame->wHeight;
299 fmt->fmt.pix.field = V4L2_FIELD_NONE;
300 fmt->fmt.pix.bytesperline = uvc_v4l2_get_bytesperline(format, frame);
301 fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize;
302 fmt->fmt.pix.colorspace = format->colorspace;
303 fmt->fmt.pix.priv = 0;
306 mutex_unlock(&stream->mutex);
310 static int uvc_v4l2_set_format(struct uvc_streaming *stream,
311 struct v4l2_format *fmt)
313 struct uvc_streaming_control probe;
314 struct uvc_format *format;
315 struct uvc_frame *frame;
318 if (fmt->type != stream->type)
321 ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame);
325 mutex_lock(&stream->mutex);
327 if (uvc_queue_allocated(&stream->queue)) {
332 stream->ctrl = probe;
333 stream->cur_format = format;
334 stream->cur_frame = frame;
337 mutex_unlock(&stream->mutex);
341 static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream,
342 struct v4l2_streamparm *parm)
344 uint32_t numerator, denominator;
346 if (parm->type != stream->type)
349 mutex_lock(&stream->mutex);
350 numerator = stream->ctrl.dwFrameInterval;
351 mutex_unlock(&stream->mutex);
353 denominator = 10000000;
354 uvc_simplify_fraction(&numerator, &denominator, 8, 333);
356 memset(parm, 0, sizeof *parm);
357 parm->type = stream->type;
359 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
360 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
361 parm->parm.capture.capturemode = 0;
362 parm->parm.capture.timeperframe.numerator = numerator;
363 parm->parm.capture.timeperframe.denominator = denominator;
364 parm->parm.capture.extendedmode = 0;
365 parm->parm.capture.readbuffers = 0;
367 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
368 parm->parm.output.outputmode = 0;
369 parm->parm.output.timeperframe.numerator = numerator;
370 parm->parm.output.timeperframe.denominator = denominator;
376 static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
377 struct v4l2_streamparm *parm)
379 struct uvc_streaming_control probe;
380 struct v4l2_fract timeperframe;
384 if (parm->type != stream->type)
387 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
388 timeperframe = parm->parm.capture.timeperframe;
390 timeperframe = parm->parm.output.timeperframe;
392 interval = uvc_fraction_to_interval(timeperframe.numerator,
393 timeperframe.denominator);
394 uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
395 timeperframe.numerator, timeperframe.denominator, interval);
397 mutex_lock(&stream->mutex);
399 if (uvc_queue_streaming(&stream->queue)) {
400 mutex_unlock(&stream->mutex);
404 probe = stream->ctrl;
405 probe.dwFrameInterval =
406 uvc_try_frame_interval(stream->cur_frame, interval);
408 /* Probe the device with the new settings. */
409 ret = uvc_probe_video(stream, &probe);
411 mutex_unlock(&stream->mutex);
415 stream->ctrl = probe;
416 mutex_unlock(&stream->mutex);
418 /* Return the actual frame period. */
419 timeperframe.numerator = probe.dwFrameInterval;
420 timeperframe.denominator = 10000000;
421 uvc_simplify_fraction(&timeperframe.numerator,
422 &timeperframe.denominator, 8, 333);
424 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
425 parm->parm.capture.timeperframe = timeperframe;
427 parm->parm.output.timeperframe = timeperframe;
432 /* ------------------------------------------------------------------------
433 * Privilege management
437 * Privilege management is the multiple-open implementation basis. The current
438 * implementation is completely transparent for the end-user and doesn't
439 * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls.
440 * Those ioctls enable finer control on the device (by making possible for a
441 * user to request exclusive access to a device), but are not mature yet.
442 * Switching to the V4L2 priority mechanism might be considered in the future
443 * if this situation changes.
445 * Each open instance of a UVC device can either be in a privileged or
446 * unprivileged state. Only a single instance can be in a privileged state at
447 * a given time. Trying to perform an operation that requires privileges will
448 * automatically acquire the required privileges if possible, or return -EBUSY
449 * otherwise. Privileges are dismissed when closing the instance or when
450 * freeing the video buffers using VIDIOC_REQBUFS.
452 * Operations that require privileges are:
459 static int uvc_acquire_privileges(struct uvc_fh *handle)
461 /* Always succeed if the handle is already privileged. */
462 if (handle->state == UVC_HANDLE_ACTIVE)
465 /* Check if the device already has a privileged handle. */
466 if (atomic_inc_return(&handle->stream->active) != 1) {
467 atomic_dec(&handle->stream->active);
471 handle->state = UVC_HANDLE_ACTIVE;
475 static void uvc_dismiss_privileges(struct uvc_fh *handle)
477 if (handle->state == UVC_HANDLE_ACTIVE)
478 atomic_dec(&handle->stream->active);
480 handle->state = UVC_HANDLE_PASSIVE;
483 static int uvc_has_privileges(struct uvc_fh *handle)
485 return handle->state == UVC_HANDLE_ACTIVE;
488 /* ------------------------------------------------------------------------
489 * V4L2 file operations
492 static int uvc_v4l2_open(struct file *file)
494 struct uvc_streaming *stream;
495 struct uvc_fh *handle;
498 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
499 stream = video_drvdata(file);
501 ret = usb_autopm_get_interface(stream->dev->intf);
505 /* Create the device handle. */
506 handle = kzalloc(sizeof *handle, GFP_KERNEL);
507 if (handle == NULL) {
508 usb_autopm_put_interface(stream->dev->intf);
512 mutex_lock(&stream->dev->lock);
513 if (stream->dev->users == 0) {
514 ret = uvc_status_start(stream->dev, GFP_KERNEL);
516 mutex_unlock(&stream->dev->lock);
517 usb_autopm_put_interface(stream->dev->intf);
523 stream->dev->users++;
524 mutex_unlock(&stream->dev->lock);
526 v4l2_fh_init(&handle->vfh, &stream->vdev);
527 v4l2_fh_add(&handle->vfh);
528 handle->chain = stream->chain;
529 handle->stream = stream;
530 handle->state = UVC_HANDLE_PASSIVE;
531 file->private_data = handle;
536 static int uvc_v4l2_release(struct file *file)
538 struct uvc_fh *handle = file->private_data;
539 struct uvc_streaming *stream = handle->stream;
541 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");
543 /* Only free resources if this is a privileged handle. */
544 if (uvc_has_privileges(handle))
545 uvc_queue_release(&stream->queue);
547 /* Release the file handle. */
548 uvc_dismiss_privileges(handle);
549 v4l2_fh_del(&handle->vfh);
550 v4l2_fh_exit(&handle->vfh);
552 file->private_data = NULL;
554 mutex_lock(&stream->dev->lock);
555 if (--stream->dev->users == 0)
556 uvc_status_stop(stream->dev);
557 mutex_unlock(&stream->dev->lock);
559 usb_autopm_put_interface(stream->dev->intf);
563 static int uvc_ioctl_querycap(struct file *file, void *fh,
564 struct v4l2_capability *cap)
566 struct video_device *vdev = video_devdata(file);
567 struct uvc_fh *handle = file->private_data;
568 struct uvc_video_chain *chain = handle->chain;
569 struct uvc_streaming *stream = handle->stream;
571 strlcpy(cap->driver, "uvcvideo", sizeof(cap->driver));
572 strlcpy(cap->card, vdev->name, sizeof(cap->card));
573 usb_make_path(stream->dev->udev, cap->bus_info, sizeof(cap->bus_info));
574 cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
576 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
577 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
579 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
584 static int uvc_ioctl_enum_fmt(struct uvc_streaming *stream,
585 struct v4l2_fmtdesc *fmt)
587 struct uvc_format *format;
588 enum v4l2_buf_type type = fmt->type;
589 __u32 index = fmt->index;
591 if (fmt->type != stream->type || fmt->index >= stream->nformats)
594 memset(fmt, 0, sizeof(*fmt));
598 format = &stream->format[fmt->index];
600 if (format->flags & UVC_FMT_FLAG_COMPRESSED)
601 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
602 strlcpy(fmt->description, format->name, sizeof(fmt->description));
603 fmt->description[sizeof(fmt->description) - 1] = 0;
604 fmt->pixelformat = format->fcc;
608 static int uvc_ioctl_enum_fmt_vid_cap(struct file *file, void *fh,
609 struct v4l2_fmtdesc *fmt)
611 struct uvc_fh *handle = fh;
612 struct uvc_streaming *stream = handle->stream;
614 return uvc_ioctl_enum_fmt(stream, fmt);
617 static int uvc_ioctl_enum_fmt_vid_out(struct file *file, void *fh,
618 struct v4l2_fmtdesc *fmt)
620 struct uvc_fh *handle = fh;
621 struct uvc_streaming *stream = handle->stream;
623 return uvc_ioctl_enum_fmt(stream, fmt);
626 static int uvc_ioctl_g_fmt_vid_cap(struct file *file, void *fh,
627 struct v4l2_format *fmt)
629 struct uvc_fh *handle = fh;
630 struct uvc_streaming *stream = handle->stream;
632 return uvc_v4l2_get_format(stream, fmt);
635 static int uvc_ioctl_g_fmt_vid_out(struct file *file, void *fh,
636 struct v4l2_format *fmt)
638 struct uvc_fh *handle = fh;
639 struct uvc_streaming *stream = handle->stream;
641 return uvc_v4l2_get_format(stream, fmt);
644 static int uvc_ioctl_s_fmt_vid_cap(struct file *file, void *fh,
645 struct v4l2_format *fmt)
647 struct uvc_fh *handle = fh;
648 struct uvc_streaming *stream = handle->stream;
651 ret = uvc_acquire_privileges(handle);
655 return uvc_v4l2_set_format(stream, fmt);
658 static int uvc_ioctl_s_fmt_vid_out(struct file *file, void *fh,
659 struct v4l2_format *fmt)
661 struct uvc_fh *handle = fh;
662 struct uvc_streaming *stream = handle->stream;
665 ret = uvc_acquire_privileges(handle);
669 return uvc_v4l2_set_format(stream, fmt);
672 static int uvc_ioctl_try_fmt_vid_cap(struct file *file, void *fh,
673 struct v4l2_format *fmt)
675 struct uvc_fh *handle = fh;
676 struct uvc_streaming *stream = handle->stream;
677 struct uvc_streaming_control probe;
679 return uvc_v4l2_try_format(stream, fmt, &probe, NULL, NULL);
682 static int uvc_ioctl_try_fmt_vid_out(struct file *file, void *fh,
683 struct v4l2_format *fmt)
685 struct uvc_fh *handle = fh;
686 struct uvc_streaming *stream = handle->stream;
687 struct uvc_streaming_control probe;
689 return uvc_v4l2_try_format(stream, fmt, &probe, NULL, NULL);
692 static int uvc_ioctl_reqbufs(struct file *file, void *fh,
693 struct v4l2_requestbuffers *rb)
695 struct uvc_fh *handle = fh;
696 struct uvc_streaming *stream = handle->stream;
699 ret = uvc_acquire_privileges(handle);
703 mutex_lock(&stream->mutex);
704 ret = uvc_request_buffers(&stream->queue, rb);
705 mutex_unlock(&stream->mutex);
710 uvc_dismiss_privileges(handle);
715 static int uvc_ioctl_querybuf(struct file *file, void *fh,
716 struct v4l2_buffer *buf)
718 struct uvc_fh *handle = fh;
719 struct uvc_streaming *stream = handle->stream;
721 if (!uvc_has_privileges(handle))
724 return uvc_query_buffer(&stream->queue, buf);
727 static int uvc_ioctl_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
729 struct uvc_fh *handle = fh;
730 struct uvc_streaming *stream = handle->stream;
732 if (!uvc_has_privileges(handle))
735 return uvc_queue_buffer(&stream->queue, buf);
738 static int uvc_ioctl_expbuf(struct file *file, void *fh,
739 struct v4l2_exportbuffer *exp)
741 struct uvc_fh *handle = fh;
742 struct uvc_streaming *stream = handle->stream;
744 if (!uvc_has_privileges(handle))
747 return uvc_export_buffer(&stream->queue, exp);
750 static int uvc_ioctl_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
752 struct uvc_fh *handle = fh;
753 struct uvc_streaming *stream = handle->stream;
755 if (!uvc_has_privileges(handle))
758 return uvc_dequeue_buffer(&stream->queue, buf,
759 file->f_flags & O_NONBLOCK);
762 static int uvc_ioctl_create_bufs(struct file *file, void *fh,
763 struct v4l2_create_buffers *cb)
765 struct uvc_fh *handle = fh;
766 struct uvc_streaming *stream = handle->stream;
769 ret = uvc_acquire_privileges(handle);
773 return uvc_create_buffers(&stream->queue, cb);
776 static int uvc_ioctl_streamon(struct file *file, void *fh,
777 enum v4l2_buf_type type)
779 struct uvc_fh *handle = fh;
780 struct uvc_streaming *stream = handle->stream;
783 if (!uvc_has_privileges(handle))
786 mutex_lock(&stream->mutex);
787 ret = uvc_queue_streamon(&stream->queue, type);
788 mutex_unlock(&stream->mutex);
793 static int uvc_ioctl_streamoff(struct file *file, void *fh,
794 enum v4l2_buf_type type)
796 struct uvc_fh *handle = fh;
797 struct uvc_streaming *stream = handle->stream;
799 if (!uvc_has_privileges(handle))
802 mutex_lock(&stream->mutex);
803 uvc_queue_streamoff(&stream->queue, type);
804 mutex_unlock(&stream->mutex);
809 static int uvc_ioctl_enum_input(struct file *file, void *fh,
810 struct v4l2_input *input)
812 struct uvc_fh *handle = fh;
813 struct uvc_video_chain *chain = handle->chain;
814 const struct uvc_entity *selector = chain->selector;
815 struct uvc_entity *iterm = NULL;
816 u32 index = input->index;
819 if (selector == NULL ||
820 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
823 list_for_each_entry(iterm, &chain->entities, chain) {
824 if (UVC_ENTITY_IS_ITERM(iterm))
828 } else if (index < selector->bNrInPins) {
829 pin = selector->baSourceID[index];
830 list_for_each_entry(iterm, &chain->entities, chain) {
831 if (!UVC_ENTITY_IS_ITERM(iterm))
833 if (iterm->id == pin)
838 if (iterm == NULL || iterm->id != pin)
841 memset(input, 0, sizeof(*input));
842 input->index = index;
843 strlcpy(input->name, iterm->name, sizeof(input->name));
844 if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
845 input->type = V4L2_INPUT_TYPE_CAMERA;
850 static int uvc_ioctl_g_input(struct file *file, void *fh, unsigned int *input)
852 struct uvc_fh *handle = fh;
853 struct uvc_video_chain *chain = handle->chain;
857 if (chain->selector == NULL ||
858 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
863 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, chain->selector->id,
864 chain->dev->intfnum, UVC_SU_INPUT_SELECT_CONTROL,
873 static int uvc_ioctl_s_input(struct file *file, void *fh, unsigned int input)
875 struct uvc_fh *handle = fh;
876 struct uvc_video_chain *chain = handle->chain;
880 ret = uvc_acquire_privileges(handle);
884 if (chain->selector == NULL ||
885 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
891 if (input >= chain->selector->bNrInPins)
895 return uvc_query_ctrl(chain->dev, UVC_SET_CUR, chain->selector->id,
896 chain->dev->intfnum, UVC_SU_INPUT_SELECT_CONTROL,
900 static int uvc_ioctl_queryctrl(struct file *file, void *fh,
901 struct v4l2_queryctrl *qc)
903 struct uvc_fh *handle = fh;
904 struct uvc_video_chain *chain = handle->chain;
906 return uvc_query_v4l2_ctrl(chain, qc);
909 static int uvc_ioctl_query_ext_ctrl(struct file *file, void *fh,
910 struct v4l2_query_ext_ctrl *qec)
912 struct uvc_fh *handle = fh;
913 struct uvc_video_chain *chain = handle->chain;
914 struct v4l2_queryctrl qc = { qec->id };
917 ret = uvc_query_v4l2_ctrl(chain, &qc);
923 strlcpy(qec->name, qc.name, sizeof(qec->name));
924 qec->minimum = qc.minimum;
925 qec->maximum = qc.maximum;
927 qec->default_value = qc.default_value;
928 qec->flags = qc.flags;
932 memset(qec->dims, 0, sizeof(qec->dims));
933 memset(qec->reserved, 0, sizeof(qec->reserved));
938 static int uvc_ioctl_g_ctrl(struct file *file, void *fh,
939 struct v4l2_control *ctrl)
941 struct uvc_fh *handle = fh;
942 struct uvc_video_chain *chain = handle->chain;
943 struct v4l2_ext_control xctrl;
946 memset(&xctrl, 0, sizeof(xctrl));
949 ret = uvc_ctrl_begin(chain);
953 ret = uvc_ctrl_get(chain, &xctrl);
954 uvc_ctrl_rollback(handle);
958 ctrl->value = xctrl.value;
962 static int uvc_ioctl_s_ctrl(struct file *file, void *fh,
963 struct v4l2_control *ctrl)
965 struct uvc_fh *handle = fh;
966 struct uvc_video_chain *chain = handle->chain;
967 struct v4l2_ext_control xctrl;
970 memset(&xctrl, 0, sizeof(xctrl));
972 xctrl.value = ctrl->value;
974 ret = uvc_ctrl_begin(chain);
978 ret = uvc_ctrl_set(chain, &xctrl);
980 uvc_ctrl_rollback(handle);
984 ret = uvc_ctrl_commit(handle, &xctrl, 1);
988 ctrl->value = xctrl.value;
992 static int uvc_ioctl_g_ext_ctrls(struct file *file, void *fh,
993 struct v4l2_ext_controls *ctrls)
995 struct uvc_fh *handle = fh;
996 struct uvc_video_chain *chain = handle->chain;
997 struct v4l2_ext_control *ctrl = ctrls->controls;
1001 if (ctrls->which == V4L2_CTRL_WHICH_DEF_VAL) {
1002 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
1003 struct v4l2_queryctrl qc = { .id = ctrl->id };
1005 ret = uvc_query_v4l2_ctrl(chain, &qc);
1007 ctrls->error_idx = i;
1011 ctrl->value = qc.default_value;
1017 ret = uvc_ctrl_begin(chain);
1021 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
1022 ret = uvc_ctrl_get(chain, ctrl);
1024 uvc_ctrl_rollback(handle);
1025 ctrls->error_idx = i;
1030 ctrls->error_idx = 0;
1032 return uvc_ctrl_rollback(handle);
1035 static int uvc_ioctl_s_try_ext_ctrls(struct uvc_fh *handle,
1036 struct v4l2_ext_controls *ctrls,
1039 struct v4l2_ext_control *ctrl = ctrls->controls;
1040 struct uvc_video_chain *chain = handle->chain;
1044 /* Default value cannot be changed */
1045 if (ctrls->which == V4L2_CTRL_WHICH_DEF_VAL)
1048 ret = uvc_ctrl_begin(chain);
1052 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
1053 ret = uvc_ctrl_set(chain, ctrl);
1055 uvc_ctrl_rollback(handle);
1056 ctrls->error_idx = commit ? ctrls->count : i;
1061 ctrls->error_idx = 0;
1064 return uvc_ctrl_commit(handle, ctrls->controls, ctrls->count);
1066 return uvc_ctrl_rollback(handle);
1069 static int uvc_ioctl_s_ext_ctrls(struct file *file, void *fh,
1070 struct v4l2_ext_controls *ctrls)
1072 struct uvc_fh *handle = fh;
1074 return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, true);
1077 static int uvc_ioctl_try_ext_ctrls(struct file *file, void *fh,
1078 struct v4l2_ext_controls *ctrls)
1080 struct uvc_fh *handle = fh;
1082 return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, false);
1085 static int uvc_ioctl_querymenu(struct file *file, void *fh,
1086 struct v4l2_querymenu *qm)
1088 struct uvc_fh *handle = fh;
1089 struct uvc_video_chain *chain = handle->chain;
1091 return uvc_query_v4l2_menu(chain, qm);
1094 static int uvc_ioctl_g_selection(struct file *file, void *fh,
1095 struct v4l2_selection *sel)
1097 struct uvc_fh *handle = fh;
1098 struct uvc_streaming *stream = handle->stream;
1100 if (sel->type != stream->type)
1103 switch (sel->target) {
1104 case V4L2_SEL_TGT_CROP_DEFAULT:
1105 case V4L2_SEL_TGT_CROP_BOUNDS:
1106 if (stream->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1109 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1110 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1111 if (stream->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1120 mutex_lock(&stream->mutex);
1121 sel->r.width = stream->cur_frame->wWidth;
1122 sel->r.height = stream->cur_frame->wHeight;
1123 mutex_unlock(&stream->mutex);
1128 static int uvc_ioctl_g_parm(struct file *file, void *fh,
1129 struct v4l2_streamparm *parm)
1131 struct uvc_fh *handle = fh;
1132 struct uvc_streaming *stream = handle->stream;
1134 return uvc_v4l2_get_streamparm(stream, parm);
1137 static int uvc_ioctl_s_parm(struct file *file, void *fh,
1138 struct v4l2_streamparm *parm)
1140 struct uvc_fh *handle = fh;
1141 struct uvc_streaming *stream = handle->stream;
1144 ret = uvc_acquire_privileges(handle);
1148 return uvc_v4l2_set_streamparm(stream, parm);
1151 static int uvc_ioctl_enum_framesizes(struct file *file, void *fh,
1152 struct v4l2_frmsizeenum *fsize)
1154 struct uvc_fh *handle = fh;
1155 struct uvc_streaming *stream = handle->stream;
1156 struct uvc_format *format = NULL;
1157 struct uvc_frame *frame;
1160 /* Look for the given pixel format */
1161 for (i = 0; i < stream->nformats; i++) {
1162 if (stream->format[i].fcc == fsize->pixel_format) {
1163 format = &stream->format[i];
1170 if (fsize->index >= format->nframes)
1173 frame = &format->frame[fsize->index];
1174 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1175 fsize->discrete.width = frame->wWidth;
1176 fsize->discrete.height = frame->wHeight;
1180 static int uvc_ioctl_enum_frameintervals(struct file *file, void *fh,
1181 struct v4l2_frmivalenum *fival)
1183 struct uvc_fh *handle = fh;
1184 struct uvc_streaming *stream = handle->stream;
1185 struct uvc_format *format = NULL;
1186 struct uvc_frame *frame = NULL;
1189 /* Look for the given pixel format and frame size */
1190 for (i = 0; i < stream->nformats; i++) {
1191 if (stream->format[i].fcc == fival->pixel_format) {
1192 format = &stream->format[i];
1199 for (i = 0; i < format->nframes; i++) {
1200 if (format->frame[i].wWidth == fival->width &&
1201 format->frame[i].wHeight == fival->height) {
1202 frame = &format->frame[i];
1209 if (frame->bFrameIntervalType) {
1210 if (fival->index >= frame->bFrameIntervalType)
1213 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1214 fival->discrete.numerator =
1215 frame->dwFrameInterval[fival->index];
1216 fival->discrete.denominator = 10000000;
1217 uvc_simplify_fraction(&fival->discrete.numerator,
1218 &fival->discrete.denominator, 8, 333);
1223 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
1224 fival->stepwise.min.numerator = frame->dwFrameInterval[0];
1225 fival->stepwise.min.denominator = 10000000;
1226 fival->stepwise.max.numerator = frame->dwFrameInterval[1];
1227 fival->stepwise.max.denominator = 10000000;
1228 fival->stepwise.step.numerator = frame->dwFrameInterval[2];
1229 fival->stepwise.step.denominator = 10000000;
1230 uvc_simplify_fraction(&fival->stepwise.min.numerator,
1231 &fival->stepwise.min.denominator, 8, 333);
1232 uvc_simplify_fraction(&fival->stepwise.max.numerator,
1233 &fival->stepwise.max.denominator, 8, 333);
1234 uvc_simplify_fraction(&fival->stepwise.step.numerator,
1235 &fival->stepwise.step.denominator, 8, 333);
1241 static int uvc_ioctl_subscribe_event(struct v4l2_fh *fh,
1242 const struct v4l2_event_subscription *sub)
1244 switch (sub->type) {
1245 case V4L2_EVENT_CTRL:
1246 return v4l2_event_subscribe(fh, sub, 0, &uvc_ctrl_sub_ev_ops);
1252 static long uvc_ioctl_default(struct file *file, void *fh, bool valid_prio,
1253 unsigned int cmd, void *arg)
1255 struct uvc_fh *handle = fh;
1256 struct uvc_video_chain *chain = handle->chain;
1259 /* Dynamic controls. */
1260 case UVCIOC_CTRL_MAP:
1261 return uvc_ioctl_ctrl_map(chain, arg);
1263 case UVCIOC_CTRL_QUERY:
1264 return uvc_xu_ctrl_query(chain, arg);
1271 #ifdef CONFIG_COMPAT
1272 struct uvc_xu_control_mapping32 {
1283 compat_caddr_t menu_info;
1289 static int uvc_v4l2_get_xu_mapping(struct uvc_xu_control_mapping *kp,
1290 const struct uvc_xu_control_mapping32 __user *up)
1294 if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
1295 __copy_from_user(kp, up, offsetof(typeof(*up), menu_info)) ||
1296 __get_user(kp->menu_count, &up->menu_count))
1299 memset(kp->reserved, 0, sizeof(kp->reserved));
1301 if (kp->menu_count == 0) {
1302 kp->menu_info = NULL;
1306 if (__get_user(p, &up->menu_info))
1308 kp->menu_info = compat_ptr(p);
1313 static int uvc_v4l2_put_xu_mapping(const struct uvc_xu_control_mapping *kp,
1314 struct uvc_xu_control_mapping32 __user *up)
1316 if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
1317 __copy_to_user(up, kp, offsetof(typeof(*up), menu_info)) ||
1318 __put_user(kp->menu_count, &up->menu_count))
1321 if (__clear_user(up->reserved, sizeof(up->reserved)))
1327 struct uvc_xu_control_query32 {
1332 compat_caddr_t data;
1335 static int uvc_v4l2_get_xu_query(struct uvc_xu_control_query *kp,
1336 const struct uvc_xu_control_query32 __user *up)
1340 if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
1341 __copy_from_user(kp, up, offsetof(typeof(*up), data)))
1344 if (kp->size == 0) {
1349 if (__get_user(p, &up->data))
1351 kp->data = compat_ptr(p);
1356 static int uvc_v4l2_put_xu_query(const struct uvc_xu_control_query *kp,
1357 struct uvc_xu_control_query32 __user *up)
1359 if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
1360 __copy_to_user(up, kp, offsetof(typeof(*up), data)))
1366 #define UVCIOC_CTRL_MAP32 _IOWR('u', 0x20, struct uvc_xu_control_mapping32)
1367 #define UVCIOC_CTRL_QUERY32 _IOWR('u', 0x21, struct uvc_xu_control_query32)
1369 static long uvc_v4l2_compat_ioctl32(struct file *file,
1370 unsigned int cmd, unsigned long arg)
1372 struct uvc_fh *handle = file->private_data;
1374 struct uvc_xu_control_mapping xmap;
1375 struct uvc_xu_control_query xqry;
1377 void __user *up = compat_ptr(arg);
1381 case UVCIOC_CTRL_MAP32:
1382 ret = uvc_v4l2_get_xu_mapping(&karg.xmap, up);
1385 ret = uvc_ioctl_ctrl_map(handle->chain, &karg.xmap);
1388 ret = uvc_v4l2_put_xu_mapping(&karg.xmap, up);
1394 case UVCIOC_CTRL_QUERY32:
1395 ret = uvc_v4l2_get_xu_query(&karg.xqry, up);
1398 ret = uvc_xu_ctrl_query(handle->chain, &karg.xqry);
1401 ret = uvc_v4l2_put_xu_query(&karg.xqry, up);
1407 return -ENOIOCTLCMD;
1414 static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1415 size_t count, loff_t *ppos)
1417 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
1421 static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1423 struct uvc_fh *handle = file->private_data;
1424 struct uvc_streaming *stream = handle->stream;
1426 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
1428 return uvc_queue_mmap(&stream->queue, vma);
1431 static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
1433 struct uvc_fh *handle = file->private_data;
1434 struct uvc_streaming *stream = handle->stream;
1436 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
1438 return uvc_queue_poll(&stream->queue, file, wait);
1442 static unsigned long uvc_v4l2_get_unmapped_area(struct file *file,
1443 unsigned long addr, unsigned long len, unsigned long pgoff,
1444 unsigned long flags)
1446 struct uvc_fh *handle = file->private_data;
1447 struct uvc_streaming *stream = handle->stream;
1449 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_get_unmapped_area\n");
1451 return uvc_queue_get_unmapped_area(&stream->queue, pgoff);
1455 const struct v4l2_ioctl_ops uvc_ioctl_ops = {
1456 .vidioc_querycap = uvc_ioctl_querycap,
1457 .vidioc_enum_fmt_vid_cap = uvc_ioctl_enum_fmt_vid_cap,
1458 .vidioc_enum_fmt_vid_out = uvc_ioctl_enum_fmt_vid_out,
1459 .vidioc_g_fmt_vid_cap = uvc_ioctl_g_fmt_vid_cap,
1460 .vidioc_g_fmt_vid_out = uvc_ioctl_g_fmt_vid_out,
1461 .vidioc_s_fmt_vid_cap = uvc_ioctl_s_fmt_vid_cap,
1462 .vidioc_s_fmt_vid_out = uvc_ioctl_s_fmt_vid_out,
1463 .vidioc_try_fmt_vid_cap = uvc_ioctl_try_fmt_vid_cap,
1464 .vidioc_try_fmt_vid_out = uvc_ioctl_try_fmt_vid_out,
1465 .vidioc_reqbufs = uvc_ioctl_reqbufs,
1466 .vidioc_querybuf = uvc_ioctl_querybuf,
1467 .vidioc_qbuf = uvc_ioctl_qbuf,
1468 .vidioc_expbuf = uvc_ioctl_expbuf,
1469 .vidioc_dqbuf = uvc_ioctl_dqbuf,
1470 .vidioc_create_bufs = uvc_ioctl_create_bufs,
1471 .vidioc_streamon = uvc_ioctl_streamon,
1472 .vidioc_streamoff = uvc_ioctl_streamoff,
1473 .vidioc_enum_input = uvc_ioctl_enum_input,
1474 .vidioc_g_input = uvc_ioctl_g_input,
1475 .vidioc_s_input = uvc_ioctl_s_input,
1476 .vidioc_queryctrl = uvc_ioctl_queryctrl,
1477 .vidioc_query_ext_ctrl = uvc_ioctl_query_ext_ctrl,
1478 .vidioc_g_ctrl = uvc_ioctl_g_ctrl,
1479 .vidioc_s_ctrl = uvc_ioctl_s_ctrl,
1480 .vidioc_g_ext_ctrls = uvc_ioctl_g_ext_ctrls,
1481 .vidioc_s_ext_ctrls = uvc_ioctl_s_ext_ctrls,
1482 .vidioc_try_ext_ctrls = uvc_ioctl_try_ext_ctrls,
1483 .vidioc_querymenu = uvc_ioctl_querymenu,
1484 .vidioc_g_selection = uvc_ioctl_g_selection,
1485 .vidioc_g_parm = uvc_ioctl_g_parm,
1486 .vidioc_s_parm = uvc_ioctl_s_parm,
1487 .vidioc_enum_framesizes = uvc_ioctl_enum_framesizes,
1488 .vidioc_enum_frameintervals = uvc_ioctl_enum_frameintervals,
1489 .vidioc_subscribe_event = uvc_ioctl_subscribe_event,
1490 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1491 .vidioc_default = uvc_ioctl_default,
1494 const struct v4l2_file_operations uvc_fops = {
1495 .owner = THIS_MODULE,
1496 .open = uvc_v4l2_open,
1497 .release = uvc_v4l2_release,
1498 .unlocked_ioctl = video_ioctl2,
1499 #ifdef CONFIG_COMPAT
1500 .compat_ioctl32 = uvc_v4l2_compat_ioctl32,
1502 .read = uvc_v4l2_read,
1503 .mmap = uvc_v4l2_mmap,
1504 .poll = uvc_v4l2_poll,
1506 .get_unmapped_area = uvc_v4l2_get_unmapped_area,