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-ioctl.h>
32 /* ------------------------------------------------------------------------
35 static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain,
36 struct uvc_xu_control_mapping *xmap)
38 struct uvc_control_mapping *map;
42 map = kzalloc(sizeof *map, GFP_KERNEL);
47 memcpy(map->name, xmap->name, sizeof map->name);
48 memcpy(map->entity, xmap->entity, sizeof map->entity);
49 map->selector = xmap->selector;
50 map->size = xmap->size;
51 map->offset = xmap->offset;
52 map->v4l2_type = xmap->v4l2_type;
53 map->data_type = xmap->data_type;
55 switch (xmap->v4l2_type) {
56 case V4L2_CTRL_TYPE_INTEGER:
57 case V4L2_CTRL_TYPE_BOOLEAN:
58 case V4L2_CTRL_TYPE_BUTTON:
61 case V4L2_CTRL_TYPE_MENU:
62 /* Prevent excessive memory consumption, as well as integer
65 if (xmap->menu_count == 0 ||
66 xmap->menu_count > UVC_MAX_CONTROL_MENU_ENTRIES) {
71 size = xmap->menu_count * sizeof(*map->menu_info);
72 map->menu_info = kmalloc(size, GFP_KERNEL);
73 if (map->menu_info == NULL) {
78 if (copy_from_user(map->menu_info, xmap->menu_info, size)) {
83 map->menu_count = xmap->menu_count;
87 uvc_trace(UVC_TRACE_CONTROL, "Unsupported V4L2 control type "
88 "%u.\n", xmap->v4l2_type);
93 ret = uvc_ctrl_add_mapping(chain, map);
96 kfree(map->menu_info);
102 /* ------------------------------------------------------------------------
107 * Find the frame interval closest to the requested frame interval for the
108 * given frame format and size. This should be done by the device as part of
109 * the Video Probe and Commit negotiation, but some hardware don't implement
112 static __u32 uvc_try_frame_interval(struct uvc_frame *frame, __u32 interval)
116 if (frame->bFrameIntervalType) {
117 __u32 best = -1, dist;
119 for (i = 0; i < frame->bFrameIntervalType; ++i) {
120 dist = interval > frame->dwFrameInterval[i]
121 ? interval - frame->dwFrameInterval[i]
122 : frame->dwFrameInterval[i] - interval;
130 interval = frame->dwFrameInterval[i-1];
132 const __u32 min = frame->dwFrameInterval[0];
133 const __u32 max = frame->dwFrameInterval[1];
134 const __u32 step = frame->dwFrameInterval[2];
136 interval = min + (interval - min + step/2) / step * step;
144 static int uvc_v4l2_try_format(struct uvc_streaming *stream,
145 struct v4l2_format *fmt, struct uvc_streaming_control *probe,
146 struct uvc_format **uvc_format, struct uvc_frame **uvc_frame)
148 struct uvc_format *format = NULL;
149 struct uvc_frame *frame = NULL;
151 unsigned int d, maxd;
157 if (fmt->type != stream->type)
160 fcc = (__u8 *)&fmt->fmt.pix.pixelformat;
161 uvc_trace(UVC_TRACE_FORMAT, "Trying format 0x%08x (%c%c%c%c): %ux%u.\n",
162 fmt->fmt.pix.pixelformat,
163 fcc[0], fcc[1], fcc[2], fcc[3],
164 fmt->fmt.pix.width, fmt->fmt.pix.height);
166 /* Check if the hardware supports the requested format. */
167 for (i = 0; i < stream->nformats; ++i) {
168 format = &stream->format[i];
169 if (format->fcc == fmt->fmt.pix.pixelformat)
173 if (format == NULL || format->fcc != fmt->fmt.pix.pixelformat) {
174 uvc_trace(UVC_TRACE_FORMAT, "Unsupported format 0x%08x.\n",
175 fmt->fmt.pix.pixelformat);
179 /* Find the closest image size. The distance between image sizes is
180 * the size in pixels of the non-overlapping regions between the
181 * requested size and the frame-specified size.
183 rw = fmt->fmt.pix.width;
184 rh = fmt->fmt.pix.height;
185 maxd = (unsigned int)-1;
187 for (i = 0; i < format->nframes; ++i) {
188 __u16 w = format->frame[i].wWidth;
189 __u16 h = format->frame[i].wHeight;
191 d = min(w, rw) * min(h, rh);
192 d = w*h + rw*rh - 2*d;
195 frame = &format->frame[i];
203 uvc_trace(UVC_TRACE_FORMAT, "Unsupported size %ux%u.\n",
204 fmt->fmt.pix.width, fmt->fmt.pix.height);
208 /* Use the default frame interval. */
209 interval = frame->dwDefaultFrameInterval;
210 uvc_trace(UVC_TRACE_FORMAT, "Using default frame interval %u.%u us "
211 "(%u.%u fps).\n", interval/10, interval%10, 10000000/interval,
212 (100000000/interval)%10);
214 /* Set the format index, frame index and frame interval. */
215 memset(probe, 0, sizeof *probe);
216 probe->bmHint = 1; /* dwFrameInterval */
217 probe->bFormatIndex = format->index;
218 probe->bFrameIndex = frame->bFrameIndex;
219 probe->dwFrameInterval = uvc_try_frame_interval(frame, interval);
220 /* Some webcams stall the probe control set request when the
221 * dwMaxVideoFrameSize field is set to zero. The UVC specification
222 * clearly states that the field is read-only from the host, so this
223 * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by
224 * the webcam to work around the problem.
226 * The workaround could probably be enabled for all webcams, so the
227 * quirk can be removed if needed. It's currently useful to detect
228 * webcam bugs and fix them before they hit the market (providing
229 * developers test their webcams with the Linux driver as well as with
230 * the Windows driver).
232 mutex_lock(&stream->mutex);
233 if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS)
234 probe->dwMaxVideoFrameSize =
235 stream->ctrl.dwMaxVideoFrameSize;
237 /* Probe the device. */
238 ret = uvc_probe_video(stream, probe);
239 mutex_unlock(&stream->mutex);
243 fmt->fmt.pix.width = frame->wWidth;
244 fmt->fmt.pix.height = frame->wHeight;
245 fmt->fmt.pix.field = V4L2_FIELD_NONE;
246 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
247 fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize;
248 fmt->fmt.pix.colorspace = format->colorspace;
249 fmt->fmt.pix.priv = 0;
251 if (uvc_format != NULL)
252 *uvc_format = format;
253 if (uvc_frame != NULL)
260 static int uvc_v4l2_get_format(struct uvc_streaming *stream,
261 struct v4l2_format *fmt)
263 struct uvc_format *format;
264 struct uvc_frame *frame;
267 if (fmt->type != stream->type)
270 mutex_lock(&stream->mutex);
271 format = stream->cur_format;
272 frame = stream->cur_frame;
274 if (format == NULL || frame == NULL) {
279 fmt->fmt.pix.pixelformat = format->fcc;
280 fmt->fmt.pix.width = frame->wWidth;
281 fmt->fmt.pix.height = frame->wHeight;
282 fmt->fmt.pix.field = V4L2_FIELD_NONE;
283 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
284 fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize;
285 fmt->fmt.pix.colorspace = format->colorspace;
286 fmt->fmt.pix.priv = 0;
289 mutex_unlock(&stream->mutex);
293 static int uvc_v4l2_set_format(struct uvc_streaming *stream,
294 struct v4l2_format *fmt)
296 struct uvc_streaming_control probe;
297 struct uvc_format *format;
298 struct uvc_frame *frame;
301 if (fmt->type != stream->type)
304 ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame);
308 mutex_lock(&stream->mutex);
310 if (uvc_queue_allocated(&stream->queue)) {
315 memcpy(&stream->ctrl, &probe, sizeof probe);
316 stream->cur_format = format;
317 stream->cur_frame = frame;
320 mutex_unlock(&stream->mutex);
324 static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream,
325 struct v4l2_streamparm *parm)
327 uint32_t numerator, denominator;
329 if (parm->type != stream->type)
332 mutex_lock(&stream->mutex);
333 numerator = stream->ctrl.dwFrameInterval;
334 mutex_unlock(&stream->mutex);
336 denominator = 10000000;
337 uvc_simplify_fraction(&numerator, &denominator, 8, 333);
339 memset(parm, 0, sizeof *parm);
340 parm->type = stream->type;
342 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
343 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
344 parm->parm.capture.capturemode = 0;
345 parm->parm.capture.timeperframe.numerator = numerator;
346 parm->parm.capture.timeperframe.denominator = denominator;
347 parm->parm.capture.extendedmode = 0;
348 parm->parm.capture.readbuffers = 0;
350 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
351 parm->parm.output.outputmode = 0;
352 parm->parm.output.timeperframe.numerator = numerator;
353 parm->parm.output.timeperframe.denominator = denominator;
359 static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
360 struct v4l2_streamparm *parm)
362 struct uvc_streaming_control probe;
363 struct v4l2_fract timeperframe;
367 if (parm->type != stream->type)
370 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
371 timeperframe = parm->parm.capture.timeperframe;
373 timeperframe = parm->parm.output.timeperframe;
375 interval = uvc_fraction_to_interval(timeperframe.numerator,
376 timeperframe.denominator);
377 uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
378 timeperframe.numerator, timeperframe.denominator, interval);
380 mutex_lock(&stream->mutex);
382 if (uvc_queue_streaming(&stream->queue)) {
383 mutex_unlock(&stream->mutex);
387 memcpy(&probe, &stream->ctrl, sizeof probe);
388 probe.dwFrameInterval =
389 uvc_try_frame_interval(stream->cur_frame, interval);
391 /* Probe the device with the new settings. */
392 ret = uvc_probe_video(stream, &probe);
394 mutex_unlock(&stream->mutex);
398 memcpy(&stream->ctrl, &probe, sizeof probe);
399 mutex_unlock(&stream->mutex);
401 /* Return the actual frame period. */
402 timeperframe.numerator = probe.dwFrameInterval;
403 timeperframe.denominator = 10000000;
404 uvc_simplify_fraction(&timeperframe.numerator,
405 &timeperframe.denominator, 8, 333);
407 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
408 parm->parm.capture.timeperframe = timeperframe;
410 parm->parm.output.timeperframe = timeperframe;
415 /* ------------------------------------------------------------------------
416 * Privilege management
420 * Privilege management is the multiple-open implementation basis. The current
421 * implementation is completely transparent for the end-user and doesn't
422 * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls.
423 * Those ioctls enable finer control on the device (by making possible for a
424 * user to request exclusive access to a device), but are not mature yet.
425 * Switching to the V4L2 priority mechanism might be considered in the future
426 * if this situation changes.
428 * Each open instance of a UVC device can either be in a privileged or
429 * unprivileged state. Only a single instance can be in a privileged state at
430 * a given time. Trying to perform an operation that requires privileges will
431 * automatically acquire the required privileges if possible, or return -EBUSY
432 * otherwise. Privileges are dismissed when closing the instance or when
433 * freeing the video buffers using VIDIOC_REQBUFS.
435 * Operations that require privileges are:
442 static int uvc_acquire_privileges(struct uvc_fh *handle)
444 /* Always succeed if the handle is already privileged. */
445 if (handle->state == UVC_HANDLE_ACTIVE)
448 /* Check if the device already has a privileged handle. */
449 if (atomic_inc_return(&handle->stream->active) != 1) {
450 atomic_dec(&handle->stream->active);
454 handle->state = UVC_HANDLE_ACTIVE;
458 static void uvc_dismiss_privileges(struct uvc_fh *handle)
460 if (handle->state == UVC_HANDLE_ACTIVE)
461 atomic_dec(&handle->stream->active);
463 handle->state = UVC_HANDLE_PASSIVE;
466 static int uvc_has_privileges(struct uvc_fh *handle)
468 return handle->state == UVC_HANDLE_ACTIVE;
471 /* ------------------------------------------------------------------------
472 * V4L2 file operations
475 static int uvc_v4l2_open(struct file *file)
477 struct uvc_streaming *stream;
478 struct uvc_fh *handle;
481 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
482 stream = video_drvdata(file);
484 if (stream->dev->state & UVC_DEV_DISCONNECTED)
487 ret = usb_autopm_get_interface(stream->dev->intf);
491 /* Create the device handle. */
492 handle = kzalloc(sizeof *handle, GFP_KERNEL);
493 if (handle == NULL) {
494 usb_autopm_put_interface(stream->dev->intf);
498 if (atomic_inc_return(&stream->dev->users) == 1) {
499 ret = uvc_status_start(stream->dev);
501 usb_autopm_put_interface(stream->dev->intf);
502 atomic_dec(&stream->dev->users);
508 handle->chain = stream->chain;
509 handle->stream = stream;
510 handle->state = UVC_HANDLE_PASSIVE;
511 file->private_data = handle;
516 static int uvc_v4l2_release(struct file *file)
518 struct uvc_fh *handle = file->private_data;
519 struct uvc_streaming *stream = handle->stream;
521 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");
523 /* Only free resources if this is a privileged handle. */
524 if (uvc_has_privileges(handle)) {
525 uvc_video_enable(stream, 0);
526 uvc_free_buffers(&stream->queue);
529 /* Release the file handle. */
530 uvc_dismiss_privileges(handle);
532 file->private_data = NULL;
534 if (atomic_dec_return(&stream->dev->users) == 0)
535 uvc_status_stop(stream->dev);
537 usb_autopm_put_interface(stream->dev->intf);
541 static long uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
543 struct video_device *vdev = video_devdata(file);
544 struct uvc_fh *handle = file->private_data;
545 struct uvc_video_chain *chain = handle->chain;
546 struct uvc_streaming *stream = handle->stream;
550 /* Query capabilities */
551 case VIDIOC_QUERYCAP:
553 struct v4l2_capability *cap = arg;
555 memset(cap, 0, sizeof *cap);
556 strlcpy(cap->driver, "uvcvideo", sizeof cap->driver);
557 strlcpy(cap->card, vdev->name, sizeof cap->card);
558 usb_make_path(stream->dev->udev,
559 cap->bus_info, sizeof(cap->bus_info));
560 cap->version = LINUX_VERSION_CODE;
561 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
562 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
563 | V4L2_CAP_STREAMING;
565 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT
566 | V4L2_CAP_STREAMING;
570 /* Get, Set & Query control */
571 case VIDIOC_QUERYCTRL:
572 return uvc_query_v4l2_ctrl(chain, arg);
576 struct v4l2_control *ctrl = arg;
577 struct v4l2_ext_control xctrl;
579 memset(&xctrl, 0, sizeof xctrl);
582 ret = uvc_ctrl_begin(chain);
586 ret = uvc_ctrl_get(chain, &xctrl);
587 uvc_ctrl_rollback(chain);
589 ctrl->value = xctrl.value;
595 struct v4l2_control *ctrl = arg;
596 struct v4l2_ext_control xctrl;
598 memset(&xctrl, 0, sizeof xctrl);
600 xctrl.value = ctrl->value;
602 ret = uvc_ctrl_begin(chain);
606 ret = uvc_ctrl_set(chain, &xctrl);
608 uvc_ctrl_rollback(chain);
611 ret = uvc_ctrl_commit(chain);
613 ctrl->value = xctrl.value;
617 case VIDIOC_QUERYMENU:
618 return uvc_query_v4l2_menu(chain, arg);
620 case VIDIOC_G_EXT_CTRLS:
622 struct v4l2_ext_controls *ctrls = arg;
623 struct v4l2_ext_control *ctrl = ctrls->controls;
626 ret = uvc_ctrl_begin(chain);
630 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
631 ret = uvc_ctrl_get(chain, ctrl);
633 uvc_ctrl_rollback(chain);
634 ctrls->error_idx = i;
638 ctrls->error_idx = 0;
639 ret = uvc_ctrl_rollback(chain);
643 case VIDIOC_S_EXT_CTRLS:
644 case VIDIOC_TRY_EXT_CTRLS:
646 struct v4l2_ext_controls *ctrls = arg;
647 struct v4l2_ext_control *ctrl = ctrls->controls;
650 ret = uvc_ctrl_begin(chain);
654 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
655 ret = uvc_ctrl_set(chain, ctrl);
657 uvc_ctrl_rollback(chain);
658 ctrls->error_idx = i;
663 ctrls->error_idx = 0;
665 if (cmd == VIDIOC_S_EXT_CTRLS)
666 ret = uvc_ctrl_commit(chain);
668 ret = uvc_ctrl_rollback(chain);
672 /* Get, Set & Enum input */
673 case VIDIOC_ENUMINPUT:
675 const struct uvc_entity *selector = chain->selector;
676 struct v4l2_input *input = arg;
677 struct uvc_entity *iterm = NULL;
678 u32 index = input->index;
681 if (selector == NULL ||
682 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
685 list_for_each_entry(iterm, &chain->entities, chain) {
686 if (UVC_ENTITY_IS_ITERM(iterm))
690 } else if (pin < selector->bNrInPins) {
691 pin = selector->baSourceID[index];
692 list_for_each_entry(iterm, &chain->entities, chain) {
693 if (!UVC_ENTITY_IS_ITERM(iterm))
695 if (iterm->id == pin)
700 if (iterm == NULL || iterm->id != pin)
703 memset(input, 0, sizeof *input);
704 input->index = index;
705 strlcpy(input->name, iterm->name, sizeof input->name);
706 if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
707 input->type = V4L2_INPUT_TYPE_CAMERA;
715 if (chain->selector == NULL ||
716 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
721 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
722 chain->selector->id, chain->dev->intfnum,
723 UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
727 *(int *)arg = input - 1;
733 u32 input = *(u32 *)arg + 1;
735 if ((ret = uvc_acquire_privileges(handle)) < 0)
738 if (chain->selector == NULL ||
739 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
745 if (input == 0 || input > chain->selector->bNrInPins)
748 return uvc_query_ctrl(chain->dev, UVC_SET_CUR,
749 chain->selector->id, chain->dev->intfnum,
750 UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
753 /* Try, Get, Set & Enum format */
754 case VIDIOC_ENUM_FMT:
756 struct v4l2_fmtdesc *fmt = arg;
757 struct uvc_format *format;
758 enum v4l2_buf_type type = fmt->type;
759 __u32 index = fmt->index;
761 if (fmt->type != stream->type ||
762 fmt->index >= stream->nformats)
765 memset(fmt, 0, sizeof(*fmt));
769 format = &stream->format[fmt->index];
771 if (format->flags & UVC_FMT_FLAG_COMPRESSED)
772 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
773 strlcpy(fmt->description, format->name,
774 sizeof fmt->description);
775 fmt->description[sizeof fmt->description - 1] = 0;
776 fmt->pixelformat = format->fcc;
782 struct uvc_streaming_control probe;
784 return uvc_v4l2_try_format(stream, arg, &probe, NULL, NULL);
788 if ((ret = uvc_acquire_privileges(handle)) < 0)
791 return uvc_v4l2_set_format(stream, arg);
794 return uvc_v4l2_get_format(stream, arg);
796 /* Frame size enumeration */
797 case VIDIOC_ENUM_FRAMESIZES:
799 struct v4l2_frmsizeenum *fsize = arg;
800 struct uvc_format *format = NULL;
801 struct uvc_frame *frame;
804 /* Look for the given pixel format */
805 for (i = 0; i < stream->nformats; i++) {
806 if (stream->format[i].fcc ==
807 fsize->pixel_format) {
808 format = &stream->format[i];
815 if (fsize->index >= format->nframes)
818 frame = &format->frame[fsize->index];
819 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
820 fsize->discrete.width = frame->wWidth;
821 fsize->discrete.height = frame->wHeight;
825 /* Frame interval enumeration */
826 case VIDIOC_ENUM_FRAMEINTERVALS:
828 struct v4l2_frmivalenum *fival = arg;
829 struct uvc_format *format = NULL;
830 struct uvc_frame *frame = NULL;
833 /* Look for the given pixel format and frame size */
834 for (i = 0; i < stream->nformats; i++) {
835 if (stream->format[i].fcc ==
836 fival->pixel_format) {
837 format = &stream->format[i];
844 for (i = 0; i < format->nframes; i++) {
845 if (format->frame[i].wWidth == fival->width &&
846 format->frame[i].wHeight == fival->height) {
847 frame = &format->frame[i];
854 if (frame->bFrameIntervalType) {
855 if (fival->index >= frame->bFrameIntervalType)
858 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
859 fival->discrete.numerator =
860 frame->dwFrameInterval[fival->index];
861 fival->discrete.denominator = 10000000;
862 uvc_simplify_fraction(&fival->discrete.numerator,
863 &fival->discrete.denominator, 8, 333);
865 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
866 fival->stepwise.min.numerator =
867 frame->dwFrameInterval[0];
868 fival->stepwise.min.denominator = 10000000;
869 fival->stepwise.max.numerator =
870 frame->dwFrameInterval[1];
871 fival->stepwise.max.denominator = 10000000;
872 fival->stepwise.step.numerator =
873 frame->dwFrameInterval[2];
874 fival->stepwise.step.denominator = 10000000;
875 uvc_simplify_fraction(&fival->stepwise.min.numerator,
876 &fival->stepwise.min.denominator, 8, 333);
877 uvc_simplify_fraction(&fival->stepwise.max.numerator,
878 &fival->stepwise.max.denominator, 8, 333);
879 uvc_simplify_fraction(&fival->stepwise.step.numerator,
880 &fival->stepwise.step.denominator, 8, 333);
885 /* Get & Set streaming parameters */
887 return uvc_v4l2_get_streamparm(stream, arg);
890 if ((ret = uvc_acquire_privileges(handle)) < 0)
893 return uvc_v4l2_set_streamparm(stream, arg);
895 /* Cropping and scaling */
898 struct v4l2_cropcap *ccap = arg;
900 if (ccap->type != stream->type)
903 ccap->bounds.left = 0;
904 ccap->bounds.top = 0;
906 mutex_lock(&stream->mutex);
907 ccap->bounds.width = stream->cur_frame->wWidth;
908 ccap->bounds.height = stream->cur_frame->wHeight;
909 mutex_unlock(&stream->mutex);
911 ccap->defrect = ccap->bounds;
913 ccap->pixelaspect.numerator = 1;
914 ccap->pixelaspect.denominator = 1;
922 /* Buffers & streaming */
924 if ((ret = uvc_acquire_privileges(handle)) < 0)
927 mutex_lock(&stream->mutex);
928 ret = uvc_alloc_buffers(&stream->queue, arg);
929 mutex_unlock(&stream->mutex);
934 uvc_dismiss_privileges(handle);
939 case VIDIOC_QUERYBUF:
941 struct v4l2_buffer *buf = arg;
943 if (!uvc_has_privileges(handle))
946 return uvc_query_buffer(&stream->queue, buf);
950 if (!uvc_has_privileges(handle))
953 return uvc_queue_buffer(&stream->queue, arg);
956 if (!uvc_has_privileges(handle))
959 return uvc_dequeue_buffer(&stream->queue, arg,
960 file->f_flags & O_NONBLOCK);
962 case VIDIOC_STREAMON:
966 if (*type != stream->type)
969 if (!uvc_has_privileges(handle))
972 mutex_lock(&stream->mutex);
973 ret = uvc_video_enable(stream, 1);
974 mutex_unlock(&stream->mutex);
980 case VIDIOC_STREAMOFF:
984 if (*type != stream->type)
987 if (!uvc_has_privileges(handle))
990 return uvc_video_enable(stream, 0);
993 /* Analog video standards make no sense for digital cameras. */
995 case VIDIOC_QUERYSTD:
1001 case VIDIOC_ENUMAUDIO:
1002 case VIDIOC_ENUMAUDOUT:
1004 case VIDIOC_ENUMOUTPUT:
1005 uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd);
1008 case UVCIOC_CTRL_MAP:
1009 return uvc_ioctl_ctrl_map(chain, arg);
1011 case UVCIOC_CTRL_QUERY:
1012 return uvc_xu_ctrl_query(chain, arg);
1015 uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n", cmd);
1022 static long uvc_v4l2_ioctl(struct file *file,
1023 unsigned int cmd, unsigned long arg)
1025 if (uvc_trace_param & UVC_TRACE_IOCTL) {
1026 uvc_printk(KERN_DEBUG, "uvc_v4l2_ioctl(");
1027 v4l_printk_ioctl(cmd);
1031 return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl);
1034 #ifdef CONFIG_COMPAT
1035 struct uvc_xu_control_mapping32 {
1046 compat_caddr_t menu_info;
1052 static int uvc_v4l2_get_xu_mapping(struct uvc_xu_control_mapping *kp,
1053 const struct uvc_xu_control_mapping32 __user *up)
1055 struct uvc_menu_info __user *umenus;
1056 struct uvc_menu_info __user *kmenus;
1059 if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
1060 __copy_from_user(kp, up, offsetof(typeof(*up), menu_info)) ||
1061 __get_user(kp->menu_count, &up->menu_count))
1064 memset(kp->reserved, 0, sizeof(kp->reserved));
1066 if (kp->menu_count == 0) {
1067 kp->menu_info = NULL;
1071 if (__get_user(p, &up->menu_info))
1073 umenus = compat_ptr(p);
1074 if (!access_ok(VERIFY_READ, umenus, kp->menu_count * sizeof(*umenus)))
1077 kmenus = compat_alloc_user_space(kp->menu_count * sizeof(*kmenus));
1080 kp->menu_info = kmenus;
1082 if (copy_in_user(kmenus, umenus, kp->menu_count * sizeof(*umenus)))
1088 static int uvc_v4l2_put_xu_mapping(const struct uvc_xu_control_mapping *kp,
1089 struct uvc_xu_control_mapping32 __user *up)
1091 struct uvc_menu_info __user *umenus;
1092 struct uvc_menu_info __user *kmenus = kp->menu_info;
1095 if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
1096 __copy_to_user(up, kp, offsetof(typeof(*up), menu_info)) ||
1097 __put_user(kp->menu_count, &up->menu_count))
1100 __clear_user(up->reserved, sizeof(up->reserved));
1102 if (kp->menu_count == 0)
1105 if (get_user(p, &up->menu_info))
1107 umenus = compat_ptr(p);
1108 if (!access_ok(VERIFY_WRITE, umenus, kp->menu_count * sizeof(*umenus)))
1111 if (copy_in_user(umenus, kmenus, kp->menu_count * sizeof(*umenus)))
1117 struct uvc_xu_control_query32 {
1122 compat_caddr_t data;
1125 static int uvc_v4l2_get_xu_query(struct uvc_xu_control_query *kp,
1126 const struct uvc_xu_control_query32 __user *up)
1132 if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
1133 __copy_from_user(kp, up, offsetof(typeof(*up), data)))
1136 if (kp->size == 0) {
1141 if (__get_user(p, &up->data))
1143 udata = compat_ptr(p);
1144 if (!access_ok(VERIFY_READ, udata, kp->size))
1147 kdata = compat_alloc_user_space(kp->size);
1152 if (copy_in_user(kdata, udata, kp->size))
1158 static int uvc_v4l2_put_xu_query(const struct uvc_xu_control_query *kp,
1159 struct uvc_xu_control_query32 __user *up)
1162 u8 __user *kdata = kp->data;
1165 if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
1166 __copy_to_user(up, kp, offsetof(typeof(*up), data)))
1172 if (get_user(p, &up->data))
1174 udata = compat_ptr(p);
1175 if (!access_ok(VERIFY_READ, udata, kp->size))
1178 if (copy_in_user(udata, kdata, kp->size))
1184 #define UVCIOC_CTRL_MAP32 _IOWR('u', 0x20, struct uvc_xu_control_mapping32)
1185 #define UVCIOC_CTRL_QUERY32 _IOWR('u', 0x21, struct uvc_xu_control_query32)
1187 static long uvc_v4l2_compat_ioctl32(struct file *file,
1188 unsigned int cmd, unsigned long arg)
1191 struct uvc_xu_control_mapping xmap;
1192 struct uvc_xu_control_query xqry;
1194 void __user *up = compat_ptr(arg);
1195 mm_segment_t old_fs;
1199 case UVCIOC_CTRL_MAP32:
1200 cmd = UVCIOC_CTRL_MAP;
1201 ret = uvc_v4l2_get_xu_mapping(&karg.xmap, up);
1204 case UVCIOC_CTRL_QUERY32:
1205 cmd = UVCIOC_CTRL_QUERY;
1206 ret = uvc_v4l2_get_xu_query(&karg.xqry, up);
1210 return -ENOIOCTLCMD;
1215 ret = uvc_v4l2_ioctl(file, cmd, (unsigned long)&karg);
1222 case UVCIOC_CTRL_MAP:
1223 ret = uvc_v4l2_put_xu_mapping(&karg.xmap, up);
1226 case UVCIOC_CTRL_QUERY:
1227 ret = uvc_v4l2_put_xu_query(&karg.xqry, up);
1235 static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1236 size_t count, loff_t *ppos)
1238 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
1242 static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1244 struct uvc_fh *handle = file->private_data;
1245 struct uvc_streaming *stream = handle->stream;
1247 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
1249 return uvc_queue_mmap(&stream->queue, vma);
1252 static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
1254 struct uvc_fh *handle = file->private_data;
1255 struct uvc_streaming *stream = handle->stream;
1257 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
1259 return uvc_queue_poll(&stream->queue, file, wait);
1263 static unsigned long uvc_v4l2_get_unmapped_area(struct file *file,
1264 unsigned long addr, unsigned long len, unsigned long pgoff,
1265 unsigned long flags)
1267 struct uvc_fh *handle = file->private_data;
1268 struct uvc_streaming *stream = handle->stream;
1270 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_get_unmapped_area\n");
1272 return uvc_queue_get_unmapped_area(&stream->queue, pgoff);
1276 const struct v4l2_file_operations uvc_fops = {
1277 .owner = THIS_MODULE,
1278 .open = uvc_v4l2_open,
1279 .release = uvc_v4l2_release,
1280 .unlocked_ioctl = uvc_v4l2_ioctl,
1281 #ifdef CONFIG_COMPAT
1282 .compat_ioctl32 = uvc_v4l2_compat_ioctl32,
1284 .read = uvc_v4l2_read,
1285 .mmap = uvc_v4l2_mmap,
1286 .poll = uvc_v4l2_poll,
1288 .get_unmapped_area = uvc_v4l2_get_unmapped_area,