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/version.h>
17 #include <linux/list.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/usb.h>
21 #include <linux/videodev2.h>
22 #include <linux/vmalloc.h>
24 #include <linux/wait.h>
25 #include <linux/atomic.h>
27 #include <media/v4l2-common.h>
28 #include <media/v4l2-ctrls.h>
29 #include <media/v4l2-event.h>
30 #include <media/v4l2-ioctl.h>
34 /* ------------------------------------------------------------------------
37 static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain,
38 struct uvc_xu_control_mapping *xmap)
40 struct uvc_control_mapping *map;
44 map = kzalloc(sizeof *map, GFP_KERNEL);
49 memcpy(map->name, xmap->name, sizeof map->name);
50 memcpy(map->entity, xmap->entity, sizeof map->entity);
51 map->selector = xmap->selector;
52 map->size = xmap->size;
53 map->offset = xmap->offset;
54 map->v4l2_type = xmap->v4l2_type;
55 map->data_type = xmap->data_type;
57 switch (xmap->v4l2_type) {
58 case V4L2_CTRL_TYPE_INTEGER:
59 case V4L2_CTRL_TYPE_BOOLEAN:
60 case V4L2_CTRL_TYPE_BUTTON:
63 case V4L2_CTRL_TYPE_MENU:
64 /* Prevent excessive memory consumption, as well as integer
67 if (xmap->menu_count == 0 ||
68 xmap->menu_count > UVC_MAX_CONTROL_MENU_ENTRIES) {
73 size = xmap->menu_count * sizeof(*map->menu_info);
74 map->menu_info = kmalloc(size, GFP_KERNEL);
75 if (map->menu_info == NULL) {
80 if (copy_from_user(map->menu_info, xmap->menu_info, size)) {
85 map->menu_count = xmap->menu_count;
89 uvc_trace(UVC_TRACE_CONTROL, "Unsupported V4L2 control type "
90 "%u.\n", xmap->v4l2_type);
95 ret = uvc_ctrl_add_mapping(chain, map);
98 kfree(map->menu_info);
104 /* ------------------------------------------------------------------------
109 * Find the frame interval closest to the requested frame interval for the
110 * given frame format and size. This should be done by the device as part of
111 * the Video Probe and Commit negotiation, but some hardware don't implement
114 static __u32 uvc_try_frame_interval(struct uvc_frame *frame, __u32 interval)
118 if (frame->bFrameIntervalType) {
119 __u32 best = -1, dist;
121 for (i = 0; i < frame->bFrameIntervalType; ++i) {
122 dist = interval > frame->dwFrameInterval[i]
123 ? interval - frame->dwFrameInterval[i]
124 : frame->dwFrameInterval[i] - interval;
132 interval = frame->dwFrameInterval[i-1];
134 const __u32 min = frame->dwFrameInterval[0];
135 const __u32 max = frame->dwFrameInterval[1];
136 const __u32 step = frame->dwFrameInterval[2];
138 interval = min + (interval - min + step/2) / step * step;
146 static int uvc_v4l2_try_format(struct uvc_streaming *stream,
147 struct v4l2_format *fmt, struct uvc_streaming_control *probe,
148 struct uvc_format **uvc_format, struct uvc_frame **uvc_frame)
150 struct uvc_format *format = NULL;
151 struct uvc_frame *frame = NULL;
153 unsigned int d, maxd;
159 if (fmt->type != stream->type)
162 fcc = (__u8 *)&fmt->fmt.pix.pixelformat;
163 uvc_trace(UVC_TRACE_FORMAT, "Trying format 0x%08x (%c%c%c%c): %ux%u.\n",
164 fmt->fmt.pix.pixelformat,
165 fcc[0], fcc[1], fcc[2], fcc[3],
166 fmt->fmt.pix.width, fmt->fmt.pix.height);
168 /* Check if the hardware supports the requested format, use the default
171 for (i = 0; i < stream->nformats; ++i) {
172 format = &stream->format[i];
173 if (format->fcc == fmt->fmt.pix.pixelformat)
177 if (i == stream->nformats) {
178 format = stream->def_format;
179 fmt->fmt.pix.pixelformat = format->fcc;
182 /* Find the closest image size. The distance between image sizes is
183 * the size in pixels of the non-overlapping regions between the
184 * requested size and the frame-specified size.
186 rw = fmt->fmt.pix.width;
187 rh = fmt->fmt.pix.height;
188 maxd = (unsigned int)-1;
190 for (i = 0; i < format->nframes; ++i) {
191 __u16 w = format->frame[i].wWidth;
192 __u16 h = format->frame[i].wHeight;
194 d = min(w, rw) * min(h, rh);
195 d = w*h + rw*rh - 2*d;
198 frame = &format->frame[i];
206 uvc_trace(UVC_TRACE_FORMAT, "Unsupported size %ux%u.\n",
207 fmt->fmt.pix.width, fmt->fmt.pix.height);
211 /* Use the default frame interval. */
212 interval = frame->dwDefaultFrameInterval;
213 uvc_trace(UVC_TRACE_FORMAT, "Using default frame interval %u.%u us "
214 "(%u.%u fps).\n", interval/10, interval%10, 10000000/interval,
215 (100000000/interval)%10);
217 /* Set the format index, frame index and frame interval. */
218 memset(probe, 0, sizeof *probe);
219 probe->bmHint = 1; /* dwFrameInterval */
220 probe->bFormatIndex = format->index;
221 probe->bFrameIndex = frame->bFrameIndex;
222 probe->dwFrameInterval = uvc_try_frame_interval(frame, interval);
223 /* Some webcams stall the probe control set request when the
224 * dwMaxVideoFrameSize field is set to zero. The UVC specification
225 * clearly states that the field is read-only from the host, so this
226 * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by
227 * the webcam to work around the problem.
229 * The workaround could probably be enabled for all webcams, so the
230 * quirk can be removed if needed. It's currently useful to detect
231 * webcam bugs and fix them before they hit the market (providing
232 * developers test their webcams with the Linux driver as well as with
233 * the Windows driver).
235 mutex_lock(&stream->mutex);
236 if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS)
237 probe->dwMaxVideoFrameSize =
238 stream->ctrl.dwMaxVideoFrameSize;
240 /* Probe the device. */
241 ret = uvc_probe_video(stream, probe);
242 mutex_unlock(&stream->mutex);
246 fmt->fmt.pix.width = frame->wWidth;
247 fmt->fmt.pix.height = frame->wHeight;
248 fmt->fmt.pix.field = V4L2_FIELD_NONE;
249 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
250 fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize;
251 fmt->fmt.pix.colorspace = format->colorspace;
252 fmt->fmt.pix.priv = 0;
254 if (uvc_format != NULL)
255 *uvc_format = format;
256 if (uvc_frame != NULL)
263 static int uvc_v4l2_get_format(struct uvc_streaming *stream,
264 struct v4l2_format *fmt)
266 struct uvc_format *format;
267 struct uvc_frame *frame;
270 if (fmt->type != stream->type)
273 mutex_lock(&stream->mutex);
274 format = stream->cur_format;
275 frame = stream->cur_frame;
277 if (format == NULL || frame == NULL) {
282 fmt->fmt.pix.pixelformat = format->fcc;
283 fmt->fmt.pix.width = frame->wWidth;
284 fmt->fmt.pix.height = frame->wHeight;
285 fmt->fmt.pix.field = V4L2_FIELD_NONE;
286 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
287 fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize;
288 fmt->fmt.pix.colorspace = format->colorspace;
289 fmt->fmt.pix.priv = 0;
292 mutex_unlock(&stream->mutex);
296 static int uvc_v4l2_set_format(struct uvc_streaming *stream,
297 struct v4l2_format *fmt)
299 struct uvc_streaming_control probe;
300 struct uvc_format *format;
301 struct uvc_frame *frame;
304 if (fmt->type != stream->type)
307 ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame);
311 mutex_lock(&stream->mutex);
313 if (uvc_queue_allocated(&stream->queue)) {
318 stream->ctrl = probe;
319 stream->cur_format = format;
320 stream->cur_frame = frame;
323 mutex_unlock(&stream->mutex);
327 static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream,
328 struct v4l2_streamparm *parm)
330 uint32_t numerator, denominator;
332 if (parm->type != stream->type)
335 mutex_lock(&stream->mutex);
336 numerator = stream->ctrl.dwFrameInterval;
337 mutex_unlock(&stream->mutex);
339 denominator = 10000000;
340 uvc_simplify_fraction(&numerator, &denominator, 8, 333);
342 memset(parm, 0, sizeof *parm);
343 parm->type = stream->type;
345 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
346 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
347 parm->parm.capture.capturemode = 0;
348 parm->parm.capture.timeperframe.numerator = numerator;
349 parm->parm.capture.timeperframe.denominator = denominator;
350 parm->parm.capture.extendedmode = 0;
351 parm->parm.capture.readbuffers = 0;
353 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
354 parm->parm.output.outputmode = 0;
355 parm->parm.output.timeperframe.numerator = numerator;
356 parm->parm.output.timeperframe.denominator = denominator;
362 static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
363 struct v4l2_streamparm *parm)
365 struct uvc_streaming_control probe;
366 struct v4l2_fract timeperframe;
370 if (parm->type != stream->type)
373 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
374 timeperframe = parm->parm.capture.timeperframe;
376 timeperframe = parm->parm.output.timeperframe;
378 interval = uvc_fraction_to_interval(timeperframe.numerator,
379 timeperframe.denominator);
380 uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
381 timeperframe.numerator, timeperframe.denominator, interval);
383 mutex_lock(&stream->mutex);
385 if (uvc_queue_streaming(&stream->queue)) {
386 mutex_unlock(&stream->mutex);
390 probe = stream->ctrl;
391 probe.dwFrameInterval =
392 uvc_try_frame_interval(stream->cur_frame, interval);
394 /* Probe the device with the new settings. */
395 ret = uvc_probe_video(stream, &probe);
397 mutex_unlock(&stream->mutex);
401 stream->ctrl = probe;
402 mutex_unlock(&stream->mutex);
404 /* Return the actual frame period. */
405 timeperframe.numerator = probe.dwFrameInterval;
406 timeperframe.denominator = 10000000;
407 uvc_simplify_fraction(&timeperframe.numerator,
408 &timeperframe.denominator, 8, 333);
410 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
411 parm->parm.capture.timeperframe = timeperframe;
413 parm->parm.output.timeperframe = timeperframe;
418 /* ------------------------------------------------------------------------
419 * Privilege management
423 * Privilege management is the multiple-open implementation basis. The current
424 * implementation is completely transparent for the end-user and doesn't
425 * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls.
426 * Those ioctls enable finer control on the device (by making possible for a
427 * user to request exclusive access to a device), but are not mature yet.
428 * Switching to the V4L2 priority mechanism might be considered in the future
429 * if this situation changes.
431 * Each open instance of a UVC device can either be in a privileged or
432 * unprivileged state. Only a single instance can be in a privileged state at
433 * a given time. Trying to perform an operation that requires privileges will
434 * automatically acquire the required privileges if possible, or return -EBUSY
435 * otherwise. Privileges are dismissed when closing the instance or when
436 * freeing the video buffers using VIDIOC_REQBUFS.
438 * Operations that require privileges are:
445 static int uvc_acquire_privileges(struct uvc_fh *handle)
447 /* Always succeed if the handle is already privileged. */
448 if (handle->state == UVC_HANDLE_ACTIVE)
451 /* Check if the device already has a privileged handle. */
452 if (atomic_inc_return(&handle->stream->active) != 1) {
453 atomic_dec(&handle->stream->active);
457 handle->state = UVC_HANDLE_ACTIVE;
461 static void uvc_dismiss_privileges(struct uvc_fh *handle)
463 if (handle->state == UVC_HANDLE_ACTIVE)
464 atomic_dec(&handle->stream->active);
466 handle->state = UVC_HANDLE_PASSIVE;
469 static int uvc_has_privileges(struct uvc_fh *handle)
471 return handle->state == UVC_HANDLE_ACTIVE;
474 /* ------------------------------------------------------------------------
475 * V4L2 file operations
478 static int uvc_v4l2_open(struct file *file)
480 struct uvc_streaming *stream;
481 struct uvc_fh *handle;
484 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
485 stream = video_drvdata(file);
487 if (stream->dev->state & UVC_DEV_DISCONNECTED)
490 ret = usb_autopm_get_interface(stream->dev->intf);
494 /* Create the device handle. */
495 handle = kzalloc(sizeof *handle, GFP_KERNEL);
496 if (handle == NULL) {
497 usb_autopm_put_interface(stream->dev->intf);
501 mutex_lock(&stream->dev->lock);
502 if (stream->dev->users == 0) {
503 ret = uvc_status_start(stream->dev, GFP_KERNEL);
505 mutex_unlock(&stream->dev->lock);
506 usb_autopm_put_interface(stream->dev->intf);
512 stream->dev->users++;
513 mutex_unlock(&stream->dev->lock);
515 v4l2_fh_init(&handle->vfh, stream->vdev);
516 v4l2_fh_add(&handle->vfh);
517 handle->chain = stream->chain;
518 handle->stream = stream;
519 handle->state = UVC_HANDLE_PASSIVE;
520 file->private_data = handle;
525 static int uvc_v4l2_release(struct file *file)
527 struct uvc_fh *handle = file->private_data;
528 struct uvc_streaming *stream = handle->stream;
530 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");
532 /* Only free resources if this is a privileged handle. */
533 if (uvc_has_privileges(handle))
534 uvc_queue_release(&stream->queue);
536 /* Release the file handle. */
537 uvc_dismiss_privileges(handle);
538 v4l2_fh_del(&handle->vfh);
539 v4l2_fh_exit(&handle->vfh);
541 file->private_data = NULL;
543 mutex_lock(&stream->dev->lock);
544 if (--stream->dev->users == 0)
545 uvc_status_stop(stream->dev);
546 mutex_unlock(&stream->dev->lock);
548 usb_autopm_put_interface(stream->dev->intf);
552 static int uvc_ioctl_querycap(struct file *file, void *fh,
553 struct v4l2_capability *cap)
555 struct video_device *vdev = video_devdata(file);
556 struct uvc_fh *handle = file->private_data;
557 struct uvc_video_chain *chain = handle->chain;
558 struct uvc_streaming *stream = handle->stream;
560 strlcpy(cap->driver, "uvcvideo", sizeof(cap->driver));
561 strlcpy(cap->card, vdev->name, sizeof(cap->card));
562 usb_make_path(stream->dev->udev, cap->bus_info, sizeof(cap->bus_info));
563 cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
565 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
566 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
568 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
573 static int uvc_ioctl_enum_fmt(struct uvc_streaming *stream,
574 struct v4l2_fmtdesc *fmt)
576 struct uvc_format *format;
577 enum v4l2_buf_type type = fmt->type;
578 __u32 index = fmt->index;
580 if (fmt->type != stream->type || fmt->index >= stream->nformats)
583 memset(fmt, 0, sizeof(*fmt));
587 format = &stream->format[fmt->index];
589 if (format->flags & UVC_FMT_FLAG_COMPRESSED)
590 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
591 strlcpy(fmt->description, format->name, sizeof(fmt->description));
592 fmt->description[sizeof(fmt->description) - 1] = 0;
593 fmt->pixelformat = format->fcc;
597 static int uvc_ioctl_enum_fmt_vid_cap(struct file *file, void *fh,
598 struct v4l2_fmtdesc *fmt)
600 struct uvc_fh *handle = fh;
601 struct uvc_streaming *stream = handle->stream;
603 return uvc_ioctl_enum_fmt(stream, fmt);
606 static int uvc_ioctl_enum_fmt_vid_out(struct file *file, void *fh,
607 struct v4l2_fmtdesc *fmt)
609 struct uvc_fh *handle = fh;
610 struct uvc_streaming *stream = handle->stream;
612 return uvc_ioctl_enum_fmt(stream, fmt);
615 static int uvc_ioctl_g_fmt_vid_cap(struct file *file, void *fh,
616 struct v4l2_format *fmt)
618 struct uvc_fh *handle = fh;
619 struct uvc_streaming *stream = handle->stream;
621 return uvc_v4l2_get_format(stream, fmt);
624 static int uvc_ioctl_g_fmt_vid_out(struct file *file, void *fh,
625 struct v4l2_format *fmt)
627 struct uvc_fh *handle = fh;
628 struct uvc_streaming *stream = handle->stream;
630 return uvc_v4l2_get_format(stream, fmt);
633 static int uvc_ioctl_s_fmt_vid_cap(struct file *file, void *fh,
634 struct v4l2_format *fmt)
636 struct uvc_fh *handle = fh;
637 struct uvc_streaming *stream = handle->stream;
640 ret = uvc_acquire_privileges(handle);
644 return uvc_v4l2_set_format(stream, fmt);
647 static int uvc_ioctl_s_fmt_vid_out(struct file *file, void *fh,
648 struct v4l2_format *fmt)
650 struct uvc_fh *handle = fh;
651 struct uvc_streaming *stream = handle->stream;
654 ret = uvc_acquire_privileges(handle);
658 return uvc_v4l2_set_format(stream, fmt);
661 static int uvc_ioctl_try_fmt_vid_cap(struct file *file, void *fh,
662 struct v4l2_format *fmt)
664 struct uvc_fh *handle = fh;
665 struct uvc_streaming *stream = handle->stream;
666 struct uvc_streaming_control probe;
668 return uvc_v4l2_try_format(stream, fmt, &probe, NULL, NULL);
671 static int uvc_ioctl_try_fmt_vid_out(struct file *file, void *fh,
672 struct v4l2_format *fmt)
674 struct uvc_fh *handle = fh;
675 struct uvc_streaming *stream = handle->stream;
676 struct uvc_streaming_control probe;
678 return uvc_v4l2_try_format(stream, fmt, &probe, NULL, NULL);
681 static int uvc_ioctl_reqbufs(struct file *file, void *fh,
682 struct v4l2_requestbuffers *rb)
684 struct uvc_fh *handle = fh;
685 struct uvc_streaming *stream = handle->stream;
688 ret = uvc_acquire_privileges(handle);
692 mutex_lock(&stream->mutex);
693 ret = uvc_alloc_buffers(&stream->queue, rb);
694 mutex_unlock(&stream->mutex);
699 uvc_dismiss_privileges(handle);
704 static int uvc_ioctl_querybuf(struct file *file, void *fh,
705 struct v4l2_buffer *buf)
707 struct uvc_fh *handle = fh;
708 struct uvc_streaming *stream = handle->stream;
710 if (!uvc_has_privileges(handle))
713 return uvc_query_buffer(&stream->queue, buf);
716 static int uvc_ioctl_qbuf(struct file *file, void *fh, 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_queue_buffer(&stream->queue, buf);
727 static int uvc_ioctl_dqbuf(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_dequeue_buffer(&stream->queue, buf,
736 file->f_flags & O_NONBLOCK);
739 static int uvc_ioctl_create_bufs(struct file *file, void *fh,
740 struct v4l2_create_buffers *cb)
742 struct uvc_fh *handle = fh;
743 struct uvc_streaming *stream = handle->stream;
746 ret = uvc_acquire_privileges(handle);
750 return uvc_create_buffers(&stream->queue, cb);
753 static int uvc_ioctl_streamon(struct file *file, void *fh,
754 enum v4l2_buf_type type)
756 struct uvc_fh *handle = fh;
757 struct uvc_streaming *stream = handle->stream;
760 if (type != stream->type)
763 if (!uvc_has_privileges(handle))
766 mutex_lock(&stream->mutex);
767 ret = uvc_queue_enable(&stream->queue, 1);
768 mutex_unlock(&stream->mutex);
773 static int uvc_ioctl_streamoff(struct file *file, void *fh,
774 enum v4l2_buf_type type)
776 struct uvc_fh *handle = fh;
777 struct uvc_streaming *stream = handle->stream;
779 if (type != stream->type)
782 if (!uvc_has_privileges(handle))
785 mutex_lock(&stream->mutex);
786 uvc_queue_enable(&stream->queue, 0);
787 mutex_unlock(&stream->mutex);
792 static int uvc_ioctl_enum_input(struct file *file, void *fh,
793 struct v4l2_input *input)
795 struct uvc_fh *handle = fh;
796 struct uvc_video_chain *chain = handle->chain;
797 const struct uvc_entity *selector = chain->selector;
798 struct uvc_entity *iterm = NULL;
799 u32 index = input->index;
802 if (selector == NULL ||
803 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
806 list_for_each_entry(iterm, &chain->entities, chain) {
807 if (UVC_ENTITY_IS_ITERM(iterm))
811 } else if (index < selector->bNrInPins) {
812 pin = selector->baSourceID[index];
813 list_for_each_entry(iterm, &chain->entities, chain) {
814 if (!UVC_ENTITY_IS_ITERM(iterm))
816 if (iterm->id == pin)
821 if (iterm == NULL || iterm->id != pin)
824 memset(input, 0, sizeof(*input));
825 input->index = index;
826 strlcpy(input->name, iterm->name, sizeof(input->name));
827 if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
828 input->type = V4L2_INPUT_TYPE_CAMERA;
833 static int uvc_ioctl_g_input(struct file *file, void *fh, unsigned int *input)
835 struct uvc_fh *handle = fh;
836 struct uvc_video_chain *chain = handle->chain;
840 if (chain->selector == NULL ||
841 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
846 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, chain->selector->id,
847 chain->dev->intfnum, UVC_SU_INPUT_SELECT_CONTROL,
856 static int uvc_ioctl_s_input(struct file *file, void *fh, unsigned int input)
858 struct uvc_fh *handle = fh;
859 struct uvc_video_chain *chain = handle->chain;
863 ret = uvc_acquire_privileges(handle);
867 if (chain->selector == NULL ||
868 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
874 if (input >= chain->selector->bNrInPins)
878 return uvc_query_ctrl(chain->dev, UVC_SET_CUR, chain->selector->id,
879 chain->dev->intfnum, UVC_SU_INPUT_SELECT_CONTROL,
883 static int uvc_ioctl_queryctrl(struct file *file, void *fh,
884 struct v4l2_queryctrl *qc)
886 struct uvc_fh *handle = fh;
887 struct uvc_video_chain *chain = handle->chain;
889 return uvc_query_v4l2_ctrl(chain, qc);
892 static int uvc_ioctl_g_ctrl(struct file *file, void *fh,
893 struct v4l2_control *ctrl)
895 struct uvc_fh *handle = fh;
896 struct uvc_video_chain *chain = handle->chain;
897 struct v4l2_ext_control xctrl;
900 memset(&xctrl, 0, sizeof(xctrl));
903 ret = uvc_ctrl_begin(chain);
907 ret = uvc_ctrl_get(chain, &xctrl);
908 uvc_ctrl_rollback(handle);
912 ctrl->value = xctrl.value;
916 static int uvc_ioctl_s_ctrl(struct file *file, void *fh,
917 struct v4l2_control *ctrl)
919 struct uvc_fh *handle = fh;
920 struct uvc_video_chain *chain = handle->chain;
921 struct v4l2_ext_control xctrl;
924 memset(&xctrl, 0, sizeof(xctrl));
926 xctrl.value = ctrl->value;
928 ret = uvc_ctrl_begin(chain);
932 ret = uvc_ctrl_set(chain, &xctrl);
934 uvc_ctrl_rollback(handle);
938 ret = uvc_ctrl_commit(handle, &xctrl, 1);
942 ctrl->value = xctrl.value;
946 static int uvc_ioctl_g_ext_ctrls(struct file *file, void *fh,
947 struct v4l2_ext_controls *ctrls)
949 struct uvc_fh *handle = fh;
950 struct uvc_video_chain *chain = handle->chain;
951 struct v4l2_ext_control *ctrl = ctrls->controls;
955 ret = uvc_ctrl_begin(chain);
959 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
960 ret = uvc_ctrl_get(chain, ctrl);
962 uvc_ctrl_rollback(handle);
963 ctrls->error_idx = i;
968 ctrls->error_idx = 0;
970 return uvc_ctrl_rollback(handle);
973 static int uvc_ioctl_s_try_ext_ctrls(struct uvc_fh *handle,
974 struct v4l2_ext_controls *ctrls,
977 struct v4l2_ext_control *ctrl = ctrls->controls;
978 struct uvc_video_chain *chain = handle->chain;
982 ret = uvc_ctrl_begin(chain);
986 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
987 ret = uvc_ctrl_set(chain, ctrl);
989 uvc_ctrl_rollback(handle);
990 ctrls->error_idx = commit ? ctrls->count : i;
995 ctrls->error_idx = 0;
998 return uvc_ctrl_commit(handle, ctrls->controls, ctrls->count);
1000 return uvc_ctrl_rollback(handle);
1003 static int uvc_ioctl_s_ext_ctrls(struct file *file, void *fh,
1004 struct v4l2_ext_controls *ctrls)
1006 struct uvc_fh *handle = fh;
1008 return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, true);
1011 static int uvc_ioctl_try_ext_ctrls(struct file *file, void *fh,
1012 struct v4l2_ext_controls *ctrls)
1014 struct uvc_fh *handle = fh;
1016 return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, false);
1019 static int uvc_ioctl_querymenu(struct file *file, void *fh,
1020 struct v4l2_querymenu *qm)
1022 struct uvc_fh *handle = fh;
1023 struct uvc_video_chain *chain = handle->chain;
1025 return uvc_query_v4l2_menu(chain, qm);
1028 static int uvc_ioctl_cropcap(struct file *file, void *fh,
1029 struct v4l2_cropcap *ccap)
1031 struct uvc_fh *handle = fh;
1032 struct uvc_streaming *stream = handle->stream;
1034 if (ccap->type != stream->type)
1037 ccap->bounds.left = 0;
1038 ccap->bounds.top = 0;
1039 mutex_lock(&stream->mutex);
1040 ccap->bounds.width = stream->cur_frame->wWidth;
1041 ccap->bounds.height = stream->cur_frame->wHeight;
1042 mutex_unlock(&stream->mutex);
1044 ccap->defrect = ccap->bounds;
1046 ccap->pixelaspect.numerator = 1;
1047 ccap->pixelaspect.denominator = 1;
1051 static int uvc_ioctl_g_parm(struct file *file, void *fh,
1052 struct v4l2_streamparm *parm)
1054 struct uvc_fh *handle = fh;
1055 struct uvc_streaming *stream = handle->stream;
1057 return uvc_v4l2_get_streamparm(stream, parm);
1060 static int uvc_ioctl_s_parm(struct file *file, void *fh,
1061 struct v4l2_streamparm *parm)
1063 struct uvc_fh *handle = fh;
1064 struct uvc_streaming *stream = handle->stream;
1067 ret = uvc_acquire_privileges(handle);
1071 return uvc_v4l2_set_streamparm(stream, parm);
1074 static int uvc_ioctl_enum_framesizes(struct file *file, void *fh,
1075 struct v4l2_frmsizeenum *fsize)
1077 struct uvc_fh *handle = fh;
1078 struct uvc_streaming *stream = handle->stream;
1079 struct uvc_format *format = NULL;
1080 struct uvc_frame *frame;
1083 /* Look for the given pixel format */
1084 for (i = 0; i < stream->nformats; i++) {
1085 if (stream->format[i].fcc == fsize->pixel_format) {
1086 format = &stream->format[i];
1093 if (fsize->index >= format->nframes)
1096 frame = &format->frame[fsize->index];
1097 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1098 fsize->discrete.width = frame->wWidth;
1099 fsize->discrete.height = frame->wHeight;
1103 static int uvc_ioctl_enum_frameintervals(struct file *file, void *fh,
1104 struct v4l2_frmivalenum *fival)
1106 struct uvc_fh *handle = fh;
1107 struct uvc_streaming *stream = handle->stream;
1108 struct uvc_format *format = NULL;
1109 struct uvc_frame *frame = NULL;
1112 /* Look for the given pixel format and frame size */
1113 for (i = 0; i < stream->nformats; i++) {
1114 if (stream->format[i].fcc == fival->pixel_format) {
1115 format = &stream->format[i];
1122 for (i = 0; i < format->nframes; i++) {
1123 if (format->frame[i].wWidth == fival->width &&
1124 format->frame[i].wHeight == fival->height) {
1125 frame = &format->frame[i];
1132 if (frame->bFrameIntervalType) {
1133 if (fival->index >= frame->bFrameIntervalType)
1136 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1137 fival->discrete.numerator =
1138 frame->dwFrameInterval[fival->index];
1139 fival->discrete.denominator = 10000000;
1140 uvc_simplify_fraction(&fival->discrete.numerator,
1141 &fival->discrete.denominator, 8, 333);
1143 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
1144 fival->stepwise.min.numerator = frame->dwFrameInterval[0];
1145 fival->stepwise.min.denominator = 10000000;
1146 fival->stepwise.max.numerator = frame->dwFrameInterval[1];
1147 fival->stepwise.max.denominator = 10000000;
1148 fival->stepwise.step.numerator = frame->dwFrameInterval[2];
1149 fival->stepwise.step.denominator = 10000000;
1150 uvc_simplify_fraction(&fival->stepwise.min.numerator,
1151 &fival->stepwise.min.denominator, 8, 333);
1152 uvc_simplify_fraction(&fival->stepwise.max.numerator,
1153 &fival->stepwise.max.denominator, 8, 333);
1154 uvc_simplify_fraction(&fival->stepwise.step.numerator,
1155 &fival->stepwise.step.denominator, 8, 333);
1161 static int uvc_ioctl_subscribe_event(struct v4l2_fh *fh,
1162 const struct v4l2_event_subscription *sub)
1164 switch (sub->type) {
1165 case V4L2_EVENT_CTRL:
1166 return v4l2_event_subscribe(fh, sub, 0, &uvc_ctrl_sub_ev_ops);
1172 static long uvc_ioctl_default(struct file *file, void *fh, bool valid_prio,
1173 unsigned int cmd, void *arg)
1175 struct uvc_fh *handle = fh;
1176 struct uvc_video_chain *chain = handle->chain;
1179 /* Dynamic controls. */
1180 case UVCIOC_CTRL_MAP:
1181 return uvc_ioctl_ctrl_map(chain, arg);
1183 case UVCIOC_CTRL_QUERY:
1184 return uvc_xu_ctrl_query(chain, arg);
1191 #ifdef CONFIG_COMPAT
1192 struct uvc_xu_control_mapping32 {
1203 compat_caddr_t menu_info;
1209 static int uvc_v4l2_get_xu_mapping(struct uvc_xu_control_mapping *kp,
1210 const struct uvc_xu_control_mapping32 __user *up)
1212 struct uvc_menu_info __user *umenus;
1213 struct uvc_menu_info __user *kmenus;
1216 if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
1217 __copy_from_user(kp, up, offsetof(typeof(*up), menu_info)) ||
1218 __get_user(kp->menu_count, &up->menu_count))
1221 memset(kp->reserved, 0, sizeof(kp->reserved));
1223 if (kp->menu_count == 0) {
1224 kp->menu_info = NULL;
1228 if (__get_user(p, &up->menu_info))
1230 umenus = compat_ptr(p);
1231 if (!access_ok(VERIFY_READ, umenus, kp->menu_count * sizeof(*umenus)))
1234 kmenus = compat_alloc_user_space(kp->menu_count * sizeof(*kmenus));
1237 kp->menu_info = kmenus;
1239 if (copy_in_user(kmenus, umenus, kp->menu_count * sizeof(*umenus)))
1245 static int uvc_v4l2_put_xu_mapping(const struct uvc_xu_control_mapping *kp,
1246 struct uvc_xu_control_mapping32 __user *up)
1248 struct uvc_menu_info __user *umenus;
1249 struct uvc_menu_info __user *kmenus = kp->menu_info;
1252 if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
1253 __copy_to_user(up, kp, offsetof(typeof(*up), menu_info)) ||
1254 __put_user(kp->menu_count, &up->menu_count))
1257 if (__clear_user(up->reserved, sizeof(up->reserved)))
1260 if (kp->menu_count == 0)
1263 if (get_user(p, &up->menu_info))
1265 umenus = compat_ptr(p);
1267 if (copy_in_user(umenus, kmenus, kp->menu_count * sizeof(*umenus)))
1273 struct uvc_xu_control_query32 {
1278 compat_caddr_t data;
1281 static int uvc_v4l2_get_xu_query(struct uvc_xu_control_query *kp,
1282 const struct uvc_xu_control_query32 __user *up)
1288 if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
1289 __copy_from_user(kp, up, offsetof(typeof(*up), data)))
1292 if (kp->size == 0) {
1297 if (__get_user(p, &up->data))
1299 udata = compat_ptr(p);
1300 if (!access_ok(VERIFY_READ, udata, kp->size))
1303 kdata = compat_alloc_user_space(kp->size);
1308 if (copy_in_user(kdata, udata, kp->size))
1314 static int uvc_v4l2_put_xu_query(const struct uvc_xu_control_query *kp,
1315 struct uvc_xu_control_query32 __user *up)
1318 u8 __user *kdata = kp->data;
1321 if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
1322 __copy_to_user(up, kp, offsetof(typeof(*up), data)))
1328 if (get_user(p, &up->data))
1330 udata = compat_ptr(p);
1331 if (!access_ok(VERIFY_READ, udata, kp->size))
1334 if (copy_in_user(udata, kdata, kp->size))
1340 #define UVCIOC_CTRL_MAP32 _IOWR('u', 0x20, struct uvc_xu_control_mapping32)
1341 #define UVCIOC_CTRL_QUERY32 _IOWR('u', 0x21, struct uvc_xu_control_query32)
1343 static long uvc_v4l2_compat_ioctl32(struct file *file,
1344 unsigned int cmd, unsigned long arg)
1347 struct uvc_xu_control_mapping xmap;
1348 struct uvc_xu_control_query xqry;
1350 void __user *up = compat_ptr(arg);
1351 mm_segment_t old_fs;
1355 case UVCIOC_CTRL_MAP32:
1356 cmd = UVCIOC_CTRL_MAP;
1357 ret = uvc_v4l2_get_xu_mapping(&karg.xmap, up);
1360 case UVCIOC_CTRL_QUERY32:
1361 cmd = UVCIOC_CTRL_QUERY;
1362 ret = uvc_v4l2_get_xu_query(&karg.xqry, up);
1366 return -ENOIOCTLCMD;
1371 ret = video_ioctl2(file, cmd, (unsigned long)&karg);
1378 case UVCIOC_CTRL_MAP:
1379 ret = uvc_v4l2_put_xu_mapping(&karg.xmap, up);
1382 case UVCIOC_CTRL_QUERY:
1383 ret = uvc_v4l2_put_xu_query(&karg.xqry, up);
1391 static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1392 size_t count, loff_t *ppos)
1394 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
1398 static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1400 struct uvc_fh *handle = file->private_data;
1401 struct uvc_streaming *stream = handle->stream;
1403 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
1405 return uvc_queue_mmap(&stream->queue, vma);
1408 static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
1410 struct uvc_fh *handle = file->private_data;
1411 struct uvc_streaming *stream = handle->stream;
1413 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
1415 return uvc_queue_poll(&stream->queue, file, wait);
1419 static unsigned long uvc_v4l2_get_unmapped_area(struct file *file,
1420 unsigned long addr, unsigned long len, unsigned long pgoff,
1421 unsigned long flags)
1423 struct uvc_fh *handle = file->private_data;
1424 struct uvc_streaming *stream = handle->stream;
1426 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_get_unmapped_area\n");
1428 return uvc_queue_get_unmapped_area(&stream->queue, pgoff);
1432 const struct v4l2_ioctl_ops uvc_ioctl_ops = {
1433 .vidioc_querycap = uvc_ioctl_querycap,
1434 .vidioc_enum_fmt_vid_cap = uvc_ioctl_enum_fmt_vid_cap,
1435 .vidioc_enum_fmt_vid_out = uvc_ioctl_enum_fmt_vid_out,
1436 .vidioc_g_fmt_vid_cap = uvc_ioctl_g_fmt_vid_cap,
1437 .vidioc_g_fmt_vid_out = uvc_ioctl_g_fmt_vid_out,
1438 .vidioc_s_fmt_vid_cap = uvc_ioctl_s_fmt_vid_cap,
1439 .vidioc_s_fmt_vid_out = uvc_ioctl_s_fmt_vid_out,
1440 .vidioc_try_fmt_vid_cap = uvc_ioctl_try_fmt_vid_cap,
1441 .vidioc_try_fmt_vid_out = uvc_ioctl_try_fmt_vid_out,
1442 .vidioc_reqbufs = uvc_ioctl_reqbufs,
1443 .vidioc_querybuf = uvc_ioctl_querybuf,
1444 .vidioc_qbuf = uvc_ioctl_qbuf,
1445 .vidioc_dqbuf = uvc_ioctl_dqbuf,
1446 .vidioc_create_bufs = uvc_ioctl_create_bufs,
1447 .vidioc_streamon = uvc_ioctl_streamon,
1448 .vidioc_streamoff = uvc_ioctl_streamoff,
1449 .vidioc_enum_input = uvc_ioctl_enum_input,
1450 .vidioc_g_input = uvc_ioctl_g_input,
1451 .vidioc_s_input = uvc_ioctl_s_input,
1452 .vidioc_queryctrl = uvc_ioctl_queryctrl,
1453 .vidioc_g_ctrl = uvc_ioctl_g_ctrl,
1454 .vidioc_s_ctrl = uvc_ioctl_s_ctrl,
1455 .vidioc_g_ext_ctrls = uvc_ioctl_g_ext_ctrls,
1456 .vidioc_s_ext_ctrls = uvc_ioctl_s_ext_ctrls,
1457 .vidioc_try_ext_ctrls = uvc_ioctl_try_ext_ctrls,
1458 .vidioc_querymenu = uvc_ioctl_querymenu,
1459 .vidioc_cropcap = uvc_ioctl_cropcap,
1460 .vidioc_g_parm = uvc_ioctl_g_parm,
1461 .vidioc_s_parm = uvc_ioctl_s_parm,
1462 .vidioc_enum_framesizes = uvc_ioctl_enum_framesizes,
1463 .vidioc_enum_frameintervals = uvc_ioctl_enum_frameintervals,
1464 .vidioc_subscribe_event = uvc_ioctl_subscribe_event,
1465 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1466 .vidioc_default = uvc_ioctl_default,
1469 const struct v4l2_file_operations uvc_fops = {
1470 .owner = THIS_MODULE,
1471 .open = uvc_v4l2_open,
1472 .release = uvc_v4l2_release,
1473 .unlocked_ioctl = video_ioctl2,
1474 #ifdef CONFIG_COMPAT
1475 .compat_ioctl32 = uvc_v4l2_compat_ioctl32,
1477 .read = uvc_v4l2_read,
1478 .mmap = uvc_v4l2_mmap,
1479 .poll = uvc_v4l2_poll,
1481 .get_unmapped_area = uvc_v4l2_get_unmapped_area,