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;
321 stream->frame_size = fmt->fmt.pix.sizeimage;
324 mutex_unlock(&stream->mutex);
328 static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream,
329 struct v4l2_streamparm *parm)
331 uint32_t numerator, denominator;
333 if (parm->type != stream->type)
336 mutex_lock(&stream->mutex);
337 numerator = stream->ctrl.dwFrameInterval;
338 mutex_unlock(&stream->mutex);
340 denominator = 10000000;
341 uvc_simplify_fraction(&numerator, &denominator, 8, 333);
343 memset(parm, 0, sizeof *parm);
344 parm->type = stream->type;
346 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
347 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
348 parm->parm.capture.capturemode = 0;
349 parm->parm.capture.timeperframe.numerator = numerator;
350 parm->parm.capture.timeperframe.denominator = denominator;
351 parm->parm.capture.extendedmode = 0;
352 parm->parm.capture.readbuffers = 0;
354 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
355 parm->parm.output.outputmode = 0;
356 parm->parm.output.timeperframe.numerator = numerator;
357 parm->parm.output.timeperframe.denominator = denominator;
363 static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
364 struct v4l2_streamparm *parm)
366 struct uvc_streaming_control probe;
367 struct v4l2_fract timeperframe;
371 if (parm->type != stream->type)
374 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
375 timeperframe = parm->parm.capture.timeperframe;
377 timeperframe = parm->parm.output.timeperframe;
379 interval = uvc_fraction_to_interval(timeperframe.numerator,
380 timeperframe.denominator);
381 uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
382 timeperframe.numerator, timeperframe.denominator, interval);
384 mutex_lock(&stream->mutex);
386 if (uvc_queue_streaming(&stream->queue)) {
387 mutex_unlock(&stream->mutex);
391 probe = stream->ctrl;
392 probe.dwFrameInterval =
393 uvc_try_frame_interval(stream->cur_frame, interval);
395 /* Probe the device with the new settings. */
396 ret = uvc_probe_video(stream, &probe);
398 mutex_unlock(&stream->mutex);
402 stream->ctrl = probe;
403 mutex_unlock(&stream->mutex);
405 /* Return the actual frame period. */
406 timeperframe.numerator = probe.dwFrameInterval;
407 timeperframe.denominator = 10000000;
408 uvc_simplify_fraction(&timeperframe.numerator,
409 &timeperframe.denominator, 8, 333);
411 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
412 parm->parm.capture.timeperframe = timeperframe;
414 parm->parm.output.timeperframe = timeperframe;
419 /* ------------------------------------------------------------------------
420 * Privilege management
424 * Privilege management is the multiple-open implementation basis. The current
425 * implementation is completely transparent for the end-user and doesn't
426 * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls.
427 * Those ioctls enable finer control on the device (by making possible for a
428 * user to request exclusive access to a device), but are not mature yet.
429 * Switching to the V4L2 priority mechanism might be considered in the future
430 * if this situation changes.
432 * Each open instance of a UVC device can either be in a privileged or
433 * unprivileged state. Only a single instance can be in a privileged state at
434 * a given time. Trying to perform an operation that requires privileges will
435 * automatically acquire the required privileges if possible, or return -EBUSY
436 * otherwise. Privileges are dismissed when closing the instance or when
437 * freeing the video buffers using VIDIOC_REQBUFS.
439 * Operations that require privileges are:
446 static int uvc_acquire_privileges(struct uvc_fh *handle)
448 /* Always succeed if the handle is already privileged. */
449 if (handle->state == UVC_HANDLE_ACTIVE)
452 /* Check if the device already has a privileged handle. */
453 if (atomic_inc_return(&handle->stream->active) != 1) {
454 atomic_dec(&handle->stream->active);
458 handle->state = UVC_HANDLE_ACTIVE;
462 static void uvc_dismiss_privileges(struct uvc_fh *handle)
464 if (handle->state == UVC_HANDLE_ACTIVE)
465 atomic_dec(&handle->stream->active);
467 handle->state = UVC_HANDLE_PASSIVE;
470 static int uvc_has_privileges(struct uvc_fh *handle)
472 return handle->state == UVC_HANDLE_ACTIVE;
475 /* ------------------------------------------------------------------------
476 * V4L2 file operations
479 static int uvc_v4l2_open(struct file *file)
481 struct uvc_streaming *stream;
482 struct uvc_fh *handle;
485 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
486 stream = video_drvdata(file);
488 if (stream->dev->state & UVC_DEV_DISCONNECTED)
491 ret = usb_autopm_get_interface(stream->dev->intf);
495 /* Create the device handle. */
496 handle = kzalloc(sizeof *handle, GFP_KERNEL);
497 if (handle == NULL) {
498 usb_autopm_put_interface(stream->dev->intf);
502 mutex_lock(&stream->dev->lock);
503 if (stream->dev->users == 0) {
504 ret = uvc_status_start(stream->dev, GFP_KERNEL);
506 mutex_unlock(&stream->dev->lock);
507 usb_autopm_put_interface(stream->dev->intf);
513 stream->dev->users++;
514 mutex_unlock(&stream->dev->lock);
516 v4l2_fh_init(&handle->vfh, stream->vdev);
517 v4l2_fh_add(&handle->vfh);
518 handle->chain = stream->chain;
519 handle->stream = stream;
520 handle->state = UVC_HANDLE_PASSIVE;
521 file->private_data = handle;
526 static int uvc_v4l2_release(struct file *file)
528 struct uvc_fh *handle = file->private_data;
529 struct uvc_streaming *stream = handle->stream;
531 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");
533 /* Only free resources if this is a privileged handle. */
534 if (uvc_has_privileges(handle)) {
535 uvc_video_enable(stream, 0);
536 uvc_free_buffers(&stream->queue);
539 /* Release the file handle. */
540 uvc_dismiss_privileges(handle);
541 v4l2_fh_del(&handle->vfh);
542 v4l2_fh_exit(&handle->vfh);
544 file->private_data = NULL;
546 mutex_lock(&stream->dev->lock);
547 if (--stream->dev->users == 0)
548 uvc_status_stop(stream->dev);
549 mutex_unlock(&stream->dev->lock);
551 usb_autopm_put_interface(stream->dev->intf);
555 static long uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
557 struct video_device *vdev = video_devdata(file);
558 struct uvc_fh *handle = file->private_data;
559 struct uvc_video_chain *chain = handle->chain;
560 struct uvc_streaming *stream = handle->stream;
564 /* Query capabilities */
565 case VIDIOC_QUERYCAP:
567 struct v4l2_capability *cap = arg;
569 memset(cap, 0, sizeof *cap);
570 strlcpy(cap->driver, "uvcvideo", sizeof cap->driver);
571 strlcpy(cap->card, vdev->name, sizeof cap->card);
572 usb_make_path(stream->dev->udev,
573 cap->bus_info, sizeof(cap->bus_info));
574 cap->version = LINUX_VERSION_CODE;
575 cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
577 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
578 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE
579 | V4L2_CAP_STREAMING;
581 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT
582 | V4L2_CAP_STREAMING;
587 case VIDIOC_G_PRIORITY:
588 *(u32 *)arg = v4l2_prio_max(vdev->prio);
591 case VIDIOC_S_PRIORITY:
592 ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
596 return v4l2_prio_change(vdev->prio, &handle->vfh.prio,
599 /* Get, Set & Query control */
600 case VIDIOC_QUERYCTRL:
601 return uvc_query_v4l2_ctrl(chain, arg);
605 struct v4l2_control *ctrl = arg;
606 struct v4l2_ext_control xctrl;
608 memset(&xctrl, 0, sizeof xctrl);
611 ret = uvc_ctrl_begin(chain);
615 ret = uvc_ctrl_get(chain, &xctrl);
616 uvc_ctrl_rollback(handle);
618 ctrl->value = xctrl.value;
624 struct v4l2_control *ctrl = arg;
625 struct v4l2_ext_control xctrl;
627 ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
631 memset(&xctrl, 0, sizeof xctrl);
633 xctrl.value = ctrl->value;
635 ret = uvc_ctrl_begin(chain);
639 ret = uvc_ctrl_set(chain, &xctrl);
641 uvc_ctrl_rollback(handle);
644 ret = uvc_ctrl_commit(handle, &xctrl, 1);
646 ctrl->value = xctrl.value;
650 case VIDIOC_QUERYMENU:
651 return uvc_query_v4l2_menu(chain, arg);
653 case VIDIOC_G_EXT_CTRLS:
655 struct v4l2_ext_controls *ctrls = arg;
656 struct v4l2_ext_control *ctrl = ctrls->controls;
659 ret = uvc_ctrl_begin(chain);
663 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
664 ret = uvc_ctrl_get(chain, ctrl);
666 uvc_ctrl_rollback(handle);
667 ctrls->error_idx = i;
671 ctrls->error_idx = 0;
672 ret = uvc_ctrl_rollback(handle);
676 case VIDIOC_S_EXT_CTRLS:
677 ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
681 case VIDIOC_TRY_EXT_CTRLS:
683 struct v4l2_ext_controls *ctrls = arg;
684 struct v4l2_ext_control *ctrl = ctrls->controls;
687 ret = uvc_ctrl_begin(chain);
691 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
692 ret = uvc_ctrl_set(chain, ctrl);
694 uvc_ctrl_rollback(handle);
695 ctrls->error_idx = cmd == VIDIOC_S_EXT_CTRLS
701 ctrls->error_idx = 0;
703 if (cmd == VIDIOC_S_EXT_CTRLS)
704 ret = uvc_ctrl_commit(handle,
705 ctrls->controls, ctrls->count);
707 ret = uvc_ctrl_rollback(handle);
711 /* Get, Set & Enum input */
712 case VIDIOC_ENUMINPUT:
714 const struct uvc_entity *selector = chain->selector;
715 struct v4l2_input *input = arg;
716 struct uvc_entity *iterm = NULL;
717 u32 index = input->index;
720 if (selector == NULL ||
721 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
724 list_for_each_entry(iterm, &chain->entities, chain) {
725 if (UVC_ENTITY_IS_ITERM(iterm))
729 } else if (index < selector->bNrInPins) {
730 pin = selector->baSourceID[index];
731 list_for_each_entry(iterm, &chain->entities, chain) {
732 if (!UVC_ENTITY_IS_ITERM(iterm))
734 if (iterm->id == pin)
739 if (iterm == NULL || iterm->id != pin)
742 memset(input, 0, sizeof *input);
743 input->index = index;
744 strlcpy(input->name, iterm->name, sizeof input->name);
745 if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
746 input->type = V4L2_INPUT_TYPE_CAMERA;
754 if (chain->selector == NULL ||
755 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
760 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
761 chain->selector->id, chain->dev->intfnum,
762 UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
766 *(int *)arg = input - 1;
772 u32 input = *(u32 *)arg + 1;
774 ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
778 if ((ret = uvc_acquire_privileges(handle)) < 0)
781 if (chain->selector == NULL ||
782 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
788 if (input == 0 || input > chain->selector->bNrInPins)
791 return uvc_query_ctrl(chain->dev, UVC_SET_CUR,
792 chain->selector->id, chain->dev->intfnum,
793 UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
796 /* Try, Get, Set & Enum format */
797 case VIDIOC_ENUM_FMT:
799 struct v4l2_fmtdesc *fmt = arg;
800 struct uvc_format *format;
801 enum v4l2_buf_type type = fmt->type;
802 __u32 index = fmt->index;
804 if (fmt->type != stream->type ||
805 fmt->index >= stream->nformats)
808 memset(fmt, 0, sizeof(*fmt));
812 format = &stream->format[fmt->index];
814 if (format->flags & UVC_FMT_FLAG_COMPRESSED)
815 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
816 strlcpy(fmt->description, format->name,
817 sizeof fmt->description);
818 fmt->description[sizeof fmt->description - 1] = 0;
819 fmt->pixelformat = format->fcc;
825 struct uvc_streaming_control probe;
827 return uvc_v4l2_try_format(stream, arg, &probe, NULL, NULL);
831 ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
835 if ((ret = uvc_acquire_privileges(handle)) < 0)
838 return uvc_v4l2_set_format(stream, arg);
841 return uvc_v4l2_get_format(stream, arg);
843 /* Frame size enumeration */
844 case VIDIOC_ENUM_FRAMESIZES:
846 struct v4l2_frmsizeenum *fsize = arg;
847 struct uvc_format *format = NULL;
848 struct uvc_frame *frame;
851 /* Look for the given pixel format */
852 for (i = 0; i < stream->nformats; i++) {
853 if (stream->format[i].fcc ==
854 fsize->pixel_format) {
855 format = &stream->format[i];
862 if (fsize->index >= format->nframes)
865 frame = &format->frame[fsize->index];
866 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
867 fsize->discrete.width = frame->wWidth;
868 fsize->discrete.height = frame->wHeight;
872 /* Frame interval enumeration */
873 case VIDIOC_ENUM_FRAMEINTERVALS:
875 struct v4l2_frmivalenum *fival = arg;
876 struct uvc_format *format = NULL;
877 struct uvc_frame *frame = NULL;
880 /* Look for the given pixel format and frame size */
881 for (i = 0; i < stream->nformats; i++) {
882 if (stream->format[i].fcc ==
883 fival->pixel_format) {
884 format = &stream->format[i];
891 for (i = 0; i < format->nframes; i++) {
892 if (format->frame[i].wWidth == fival->width &&
893 format->frame[i].wHeight == fival->height) {
894 frame = &format->frame[i];
901 if (frame->bFrameIntervalType) {
902 if (fival->index >= frame->bFrameIntervalType)
905 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
906 fival->discrete.numerator =
907 frame->dwFrameInterval[fival->index];
908 fival->discrete.denominator = 10000000;
909 uvc_simplify_fraction(&fival->discrete.numerator,
910 &fival->discrete.denominator, 8, 333);
912 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
913 fival->stepwise.min.numerator =
914 frame->dwFrameInterval[0];
915 fival->stepwise.min.denominator = 10000000;
916 fival->stepwise.max.numerator =
917 frame->dwFrameInterval[1];
918 fival->stepwise.max.denominator = 10000000;
919 fival->stepwise.step.numerator =
920 frame->dwFrameInterval[2];
921 fival->stepwise.step.denominator = 10000000;
922 uvc_simplify_fraction(&fival->stepwise.min.numerator,
923 &fival->stepwise.min.denominator, 8, 333);
924 uvc_simplify_fraction(&fival->stepwise.max.numerator,
925 &fival->stepwise.max.denominator, 8, 333);
926 uvc_simplify_fraction(&fival->stepwise.step.numerator,
927 &fival->stepwise.step.denominator, 8, 333);
932 /* Get & Set streaming parameters */
934 return uvc_v4l2_get_streamparm(stream, arg);
937 ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
941 if ((ret = uvc_acquire_privileges(handle)) < 0)
944 return uvc_v4l2_set_streamparm(stream, arg);
946 /* Cropping and scaling */
949 struct v4l2_cropcap *ccap = arg;
951 if (ccap->type != stream->type)
954 ccap->bounds.left = 0;
955 ccap->bounds.top = 0;
957 mutex_lock(&stream->mutex);
958 ccap->bounds.width = stream->cur_frame->wWidth;
959 ccap->bounds.height = stream->cur_frame->wHeight;
960 mutex_unlock(&stream->mutex);
962 ccap->defrect = ccap->bounds;
964 ccap->pixelaspect.numerator = 1;
965 ccap->pixelaspect.denominator = 1;
973 /* Buffers & streaming */
975 ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
979 if ((ret = uvc_acquire_privileges(handle)) < 0)
982 mutex_lock(&stream->mutex);
983 ret = uvc_alloc_buffers(&stream->queue, arg);
984 mutex_unlock(&stream->mutex);
989 uvc_dismiss_privileges(handle);
994 case VIDIOC_QUERYBUF:
996 struct v4l2_buffer *buf = arg;
998 if (!uvc_has_privileges(handle))
1001 return uvc_query_buffer(&stream->queue, buf);
1004 case VIDIOC_CREATE_BUFS:
1006 struct v4l2_create_buffers *cb = arg;
1008 ret = uvc_acquire_privileges(handle);
1012 return uvc_create_buffers(&stream->queue, cb);
1016 if (!uvc_has_privileges(handle))
1019 return uvc_queue_buffer(&stream->queue, arg);
1022 if (!uvc_has_privileges(handle))
1025 return uvc_dequeue_buffer(&stream->queue, arg,
1026 file->f_flags & O_NONBLOCK);
1028 case VIDIOC_STREAMON:
1032 if (*type != stream->type)
1035 ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
1039 if (!uvc_has_privileges(handle))
1042 mutex_lock(&stream->mutex);
1043 ret = uvc_video_enable(stream, 1);
1044 mutex_unlock(&stream->mutex);
1050 case VIDIOC_STREAMOFF:
1054 if (*type != stream->type)
1057 ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
1061 if (!uvc_has_privileges(handle))
1064 return uvc_video_enable(stream, 0);
1067 case VIDIOC_SUBSCRIBE_EVENT:
1069 struct v4l2_event_subscription *sub = arg;
1071 switch (sub->type) {
1072 case V4L2_EVENT_CTRL:
1073 return v4l2_event_subscribe(&handle->vfh, sub, 0,
1074 &uvc_ctrl_sub_ev_ops);
1080 case VIDIOC_UNSUBSCRIBE_EVENT:
1081 return v4l2_event_unsubscribe(&handle->vfh, arg);
1083 case VIDIOC_DQEVENT:
1084 return v4l2_event_dequeue(&handle->vfh, arg,
1085 file->f_flags & O_NONBLOCK);
1087 /* Analog video standards make no sense for digital cameras. */
1088 case VIDIOC_ENUMSTD:
1089 case VIDIOC_QUERYSTD:
1093 case VIDIOC_OVERLAY:
1095 case VIDIOC_ENUMAUDIO:
1096 case VIDIOC_ENUMAUDOUT:
1098 case VIDIOC_ENUMOUTPUT:
1099 uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd);
1102 case UVCIOC_CTRL_MAP:
1103 return uvc_ioctl_ctrl_map(chain, arg);
1105 case UVCIOC_CTRL_QUERY:
1106 return uvc_xu_ctrl_query(chain, arg);
1109 uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n", cmd);
1116 static long uvc_v4l2_ioctl(struct file *file,
1117 unsigned int cmd, unsigned long arg)
1119 if (uvc_trace_param & UVC_TRACE_IOCTL) {
1120 uvc_printk(KERN_DEBUG, "uvc_v4l2_ioctl(");
1121 v4l_printk_ioctl(NULL, cmd);
1125 return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl);
1128 #ifdef CONFIG_COMPAT
1129 struct uvc_xu_control_mapping32 {
1140 compat_caddr_t menu_info;
1146 static int uvc_v4l2_get_xu_mapping(struct uvc_xu_control_mapping *kp,
1147 const struct uvc_xu_control_mapping32 __user *up)
1149 struct uvc_menu_info __user *umenus;
1150 struct uvc_menu_info __user *kmenus;
1153 if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
1154 __copy_from_user(kp, up, offsetof(typeof(*up), menu_info)) ||
1155 __get_user(kp->menu_count, &up->menu_count))
1158 memset(kp->reserved, 0, sizeof(kp->reserved));
1160 if (kp->menu_count == 0) {
1161 kp->menu_info = NULL;
1165 if (__get_user(p, &up->menu_info))
1167 umenus = compat_ptr(p);
1168 if (!access_ok(VERIFY_READ, umenus, kp->menu_count * sizeof(*umenus)))
1171 kmenus = compat_alloc_user_space(kp->menu_count * sizeof(*kmenus));
1174 kp->menu_info = kmenus;
1176 if (copy_in_user(kmenus, umenus, kp->menu_count * sizeof(*umenus)))
1182 static int uvc_v4l2_put_xu_mapping(const struct uvc_xu_control_mapping *kp,
1183 struct uvc_xu_control_mapping32 __user *up)
1185 struct uvc_menu_info __user *umenus;
1186 struct uvc_menu_info __user *kmenus = kp->menu_info;
1189 if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
1190 __copy_to_user(up, kp, offsetof(typeof(*up), menu_info)) ||
1191 __put_user(kp->menu_count, &up->menu_count))
1194 if (__clear_user(up->reserved, sizeof(up->reserved)))
1197 if (kp->menu_count == 0)
1200 if (get_user(p, &up->menu_info))
1202 umenus = compat_ptr(p);
1204 if (copy_in_user(umenus, kmenus, kp->menu_count * sizeof(*umenus)))
1210 struct uvc_xu_control_query32 {
1215 compat_caddr_t data;
1218 static int uvc_v4l2_get_xu_query(struct uvc_xu_control_query *kp,
1219 const struct uvc_xu_control_query32 __user *up)
1225 if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
1226 __copy_from_user(kp, up, offsetof(typeof(*up), data)))
1229 if (kp->size == 0) {
1234 if (__get_user(p, &up->data))
1236 udata = compat_ptr(p);
1237 if (!access_ok(VERIFY_READ, udata, kp->size))
1240 kdata = compat_alloc_user_space(kp->size);
1245 if (copy_in_user(kdata, udata, kp->size))
1251 static int uvc_v4l2_put_xu_query(const struct uvc_xu_control_query *kp,
1252 struct uvc_xu_control_query32 __user *up)
1255 u8 __user *kdata = kp->data;
1258 if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
1259 __copy_to_user(up, kp, offsetof(typeof(*up), data)))
1265 if (get_user(p, &up->data))
1267 udata = compat_ptr(p);
1268 if (!access_ok(VERIFY_READ, udata, kp->size))
1271 if (copy_in_user(udata, kdata, kp->size))
1277 #define UVCIOC_CTRL_MAP32 _IOWR('u', 0x20, struct uvc_xu_control_mapping32)
1278 #define UVCIOC_CTRL_QUERY32 _IOWR('u', 0x21, struct uvc_xu_control_query32)
1280 static long uvc_v4l2_compat_ioctl32(struct file *file,
1281 unsigned int cmd, unsigned long arg)
1284 struct uvc_xu_control_mapping xmap;
1285 struct uvc_xu_control_query xqry;
1287 void __user *up = compat_ptr(arg);
1288 mm_segment_t old_fs;
1292 case UVCIOC_CTRL_MAP32:
1293 cmd = UVCIOC_CTRL_MAP;
1294 ret = uvc_v4l2_get_xu_mapping(&karg.xmap, up);
1297 case UVCIOC_CTRL_QUERY32:
1298 cmd = UVCIOC_CTRL_QUERY;
1299 ret = uvc_v4l2_get_xu_query(&karg.xqry, up);
1303 return -ENOIOCTLCMD;
1308 ret = uvc_v4l2_ioctl(file, cmd, (unsigned long)&karg);
1315 case UVCIOC_CTRL_MAP:
1316 ret = uvc_v4l2_put_xu_mapping(&karg.xmap, up);
1319 case UVCIOC_CTRL_QUERY:
1320 ret = uvc_v4l2_put_xu_query(&karg.xqry, up);
1328 static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1329 size_t count, loff_t *ppos)
1331 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
1335 static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1337 struct uvc_fh *handle = file->private_data;
1338 struct uvc_streaming *stream = handle->stream;
1340 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
1342 return uvc_queue_mmap(&stream->queue, vma);
1345 static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
1347 struct uvc_fh *handle = file->private_data;
1348 struct uvc_streaming *stream = handle->stream;
1350 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
1352 return uvc_queue_poll(&stream->queue, file, wait);
1356 static unsigned long uvc_v4l2_get_unmapped_area(struct file *file,
1357 unsigned long addr, unsigned long len, unsigned long pgoff,
1358 unsigned long flags)
1360 struct uvc_fh *handle = file->private_data;
1361 struct uvc_streaming *stream = handle->stream;
1363 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_get_unmapped_area\n");
1365 return uvc_queue_get_unmapped_area(&stream->queue, pgoff);
1369 const struct v4l2_file_operations uvc_fops = {
1370 .owner = THIS_MODULE,
1371 .open = uvc_v4l2_open,
1372 .release = uvc_v4l2_release,
1373 .unlocked_ioctl = uvc_v4l2_ioctl,
1374 #ifdef CONFIG_COMPAT
1375 .compat_ioctl32 = uvc_v4l2_compat_ioctl32,
1377 .read = uvc_v4l2_read,
1378 .mmap = uvc_v4l2_mmap,
1379 .poll = uvc_v4l2_poll,
1381 .get_unmapped_area = uvc_v4l2_get_unmapped_area,