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/kernel.h>
15 #include <linux/version.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 <asm/atomic.h>
26 #include <media/v4l2-common.h>
27 #include <media/v4l2-ioctl.h>
31 /* ------------------------------------------------------------------------
34 static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain,
35 struct uvc_xu_control_mapping *xmap, int old)
37 struct uvc_control_mapping *map;
41 map = kzalloc(sizeof *map, GFP_KERNEL);
46 memcpy(map->name, xmap->name, sizeof map->name);
47 memcpy(map->entity, xmap->entity, sizeof map->entity);
48 map->selector = xmap->selector;
49 map->size = xmap->size;
50 map->offset = xmap->offset;
51 map->v4l2_type = xmap->v4l2_type;
52 map->data_type = xmap->data_type;
54 switch (xmap->v4l2_type) {
55 case V4L2_CTRL_TYPE_INTEGER:
56 case V4L2_CTRL_TYPE_BOOLEAN:
57 case V4L2_CTRL_TYPE_BUTTON:
60 case V4L2_CTRL_TYPE_MENU:
62 uvc_trace(UVC_TRACE_CONTROL, "V4L2_CTRL_TYPE_MENU not "
63 "supported for UVCIOC_CTRL_MAP_OLD.\n");
68 /* Prevent excessive memory consumption, as well as integer
71 if (xmap->menu_count == 0 ||
72 xmap->menu_count > UVC_MAX_CONTROL_MENU_ENTRIES) {
77 size = xmap->menu_count * sizeof(*map->menu_info);
78 map->menu_info = kmalloc(size, GFP_KERNEL);
79 if (map->menu_info == NULL) {
84 if (copy_from_user(map->menu_info, xmap->menu_info, size)) {
89 map->menu_count = xmap->menu_count;
93 uvc_trace(UVC_TRACE_CONTROL, "Unsupported V4L2 control type "
94 "%u.\n", xmap->v4l2_type);
99 ret = uvc_ctrl_add_mapping(chain, map);
102 kfree(map->menu_info);
108 /* ------------------------------------------------------------------------
113 * Find the frame interval closest to the requested frame interval for the
114 * given frame format and size. This should be done by the device as part of
115 * the Video Probe and Commit negotiation, but some hardware don't implement
118 static __u32 uvc_try_frame_interval(struct uvc_frame *frame, __u32 interval)
122 if (frame->bFrameIntervalType) {
123 __u32 best = -1, dist;
125 for (i = 0; i < frame->bFrameIntervalType; ++i) {
126 dist = interval > frame->dwFrameInterval[i]
127 ? interval - frame->dwFrameInterval[i]
128 : frame->dwFrameInterval[i] - interval;
136 interval = frame->dwFrameInterval[i-1];
138 const __u32 min = frame->dwFrameInterval[0];
139 const __u32 max = frame->dwFrameInterval[1];
140 const __u32 step = frame->dwFrameInterval[2];
142 interval = min + (interval - min + step/2) / step * step;
150 static int uvc_v4l2_try_format(struct uvc_streaming *stream,
151 struct v4l2_format *fmt, struct uvc_streaming_control *probe,
152 struct uvc_format **uvc_format, struct uvc_frame **uvc_frame)
154 struct uvc_format *format = NULL;
155 struct uvc_frame *frame = NULL;
157 unsigned int d, maxd;
163 if (fmt->type != stream->type)
166 fcc = (__u8 *)&fmt->fmt.pix.pixelformat;
167 uvc_trace(UVC_TRACE_FORMAT, "Trying format 0x%08x (%c%c%c%c): %ux%u.\n",
168 fmt->fmt.pix.pixelformat,
169 fcc[0], fcc[1], fcc[2], fcc[3],
170 fmt->fmt.pix.width, fmt->fmt.pix.height);
172 /* Check if the hardware supports the requested format. */
173 for (i = 0; i < stream->nformats; ++i) {
174 format = &stream->format[i];
175 if (format->fcc == fmt->fmt.pix.pixelformat)
179 if (format == NULL || format->fcc != fmt->fmt.pix.pixelformat) {
180 uvc_trace(UVC_TRACE_FORMAT, "Unsupported format 0x%08x.\n",
181 fmt->fmt.pix.pixelformat);
185 /* Find the closest image size. The distance between image sizes is
186 * the size in pixels of the non-overlapping regions between the
187 * requested size and the frame-specified size.
189 rw = fmt->fmt.pix.width;
190 rh = fmt->fmt.pix.height;
191 maxd = (unsigned int)-1;
193 for (i = 0; i < format->nframes; ++i) {
194 __u16 w = format->frame[i].wWidth;
195 __u16 h = format->frame[i].wHeight;
197 d = min(w, rw) * min(h, rh);
198 d = w*h + rw*rh - 2*d;
201 frame = &format->frame[i];
209 uvc_trace(UVC_TRACE_FORMAT, "Unsupported size %ux%u.\n",
210 fmt->fmt.pix.width, fmt->fmt.pix.height);
214 /* Use the default frame interval. */
215 interval = frame->dwDefaultFrameInterval;
216 uvc_trace(UVC_TRACE_FORMAT, "Using default frame interval %u.%u us "
217 "(%u.%u fps).\n", interval/10, interval%10, 10000000/interval,
218 (100000000/interval)%10);
220 /* Set the format index, frame index and frame interval. */
221 memset(probe, 0, sizeof *probe);
222 probe->bmHint = 1; /* dwFrameInterval */
223 probe->bFormatIndex = format->index;
224 probe->bFrameIndex = frame->bFrameIndex;
225 probe->dwFrameInterval = uvc_try_frame_interval(frame, interval);
226 /* Some webcams stall the probe control set request when the
227 * dwMaxVideoFrameSize field is set to zero. The UVC specification
228 * clearly states that the field is read-only from the host, so this
229 * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by
230 * the webcam to work around the problem.
232 * The workaround could probably be enabled for all webcams, so the
233 * quirk can be removed if needed. It's currently useful to detect
234 * webcam bugs and fix them before they hit the market (providing
235 * developers test their webcams with the Linux driver as well as with
236 * the Windows driver).
238 mutex_lock(&stream->mutex);
239 if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS)
240 probe->dwMaxVideoFrameSize =
241 stream->ctrl.dwMaxVideoFrameSize;
243 /* Probe the device. */
244 ret = uvc_probe_video(stream, probe);
245 mutex_unlock(&stream->mutex);
249 fmt->fmt.pix.width = frame->wWidth;
250 fmt->fmt.pix.height = frame->wHeight;
251 fmt->fmt.pix.field = V4L2_FIELD_NONE;
252 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
253 fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize;
254 fmt->fmt.pix.colorspace = format->colorspace;
255 fmt->fmt.pix.priv = 0;
257 if (uvc_format != NULL)
258 *uvc_format = format;
259 if (uvc_frame != NULL)
266 static int uvc_v4l2_get_format(struct uvc_streaming *stream,
267 struct v4l2_format *fmt)
269 struct uvc_format *format;
270 struct uvc_frame *frame;
273 if (fmt->type != stream->type)
276 mutex_lock(&stream->mutex);
277 format = stream->cur_format;
278 frame = stream->cur_frame;
280 if (format == NULL || frame == NULL) {
285 fmt->fmt.pix.pixelformat = format->fcc;
286 fmt->fmt.pix.width = frame->wWidth;
287 fmt->fmt.pix.height = frame->wHeight;
288 fmt->fmt.pix.field = V4L2_FIELD_NONE;
289 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
290 fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize;
291 fmt->fmt.pix.colorspace = format->colorspace;
292 fmt->fmt.pix.priv = 0;
295 mutex_unlock(&stream->mutex);
299 static int uvc_v4l2_set_format(struct uvc_streaming *stream,
300 struct v4l2_format *fmt)
302 struct uvc_streaming_control probe;
303 struct uvc_format *format;
304 struct uvc_frame *frame;
307 if (fmt->type != stream->type)
310 ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame);
314 mutex_lock(&stream->mutex);
316 if (uvc_queue_allocated(&stream->queue)) {
321 memcpy(&stream->ctrl, &probe, sizeof probe);
322 stream->cur_format = format;
323 stream->cur_frame = frame;
326 mutex_unlock(&stream->mutex);
330 static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream,
331 struct v4l2_streamparm *parm)
333 uint32_t numerator, denominator;
335 if (parm->type != stream->type)
338 mutex_lock(&stream->mutex);
339 numerator = stream->ctrl.dwFrameInterval;
340 mutex_unlock(&stream->mutex);
342 denominator = 10000000;
343 uvc_simplify_fraction(&numerator, &denominator, 8, 333);
345 memset(parm, 0, sizeof *parm);
346 parm->type = stream->type;
348 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
349 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
350 parm->parm.capture.capturemode = 0;
351 parm->parm.capture.timeperframe.numerator = numerator;
352 parm->parm.capture.timeperframe.denominator = denominator;
353 parm->parm.capture.extendedmode = 0;
354 parm->parm.capture.readbuffers = 0;
356 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
357 parm->parm.output.outputmode = 0;
358 parm->parm.output.timeperframe.numerator = numerator;
359 parm->parm.output.timeperframe.denominator = denominator;
365 static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
366 struct v4l2_streamparm *parm)
368 struct uvc_streaming_control probe;
369 struct v4l2_fract timeperframe;
373 if (parm->type != stream->type)
376 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
377 timeperframe = parm->parm.capture.timeperframe;
379 timeperframe = parm->parm.output.timeperframe;
381 interval = uvc_fraction_to_interval(timeperframe.numerator,
382 timeperframe.denominator);
383 uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
384 timeperframe.numerator, timeperframe.denominator, interval);
386 mutex_lock(&stream->mutex);
388 if (uvc_queue_streaming(&stream->queue)) {
389 mutex_unlock(&stream->mutex);
393 memcpy(&probe, &stream->ctrl, sizeof probe);
394 probe.dwFrameInterval =
395 uvc_try_frame_interval(stream->cur_frame, interval);
397 /* Probe the device with the new settings. */
398 ret = uvc_probe_video(stream, &probe);
400 mutex_unlock(&stream->mutex);
404 memcpy(&stream->ctrl, &probe, sizeof probe);
405 mutex_unlock(&stream->mutex);
407 /* Return the actual frame period. */
408 timeperframe.numerator = probe.dwFrameInterval;
409 timeperframe.denominator = 10000000;
410 uvc_simplify_fraction(&timeperframe.numerator,
411 &timeperframe.denominator, 8, 333);
413 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
414 parm->parm.capture.timeperframe = timeperframe;
416 parm->parm.output.timeperframe = timeperframe;
421 /* ------------------------------------------------------------------------
422 * Privilege management
426 * Privilege management is the multiple-open implementation basis. The current
427 * implementation is completely transparent for the end-user and doesn't
428 * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls.
429 * Those ioctls enable finer control on the device (by making possible for a
430 * user to request exclusive access to a device), but are not mature yet.
431 * Switching to the V4L2 priority mechanism might be considered in the future
432 * if this situation changes.
434 * Each open instance of a UVC device can either be in a privileged or
435 * unprivileged state. Only a single instance can be in a privileged state at
436 * a given time. Trying to perform an operation that requires privileges will
437 * automatically acquire the required privileges if possible, or return -EBUSY
438 * otherwise. Privileges are dismissed when closing the instance or when
439 * freeing the video buffers using VIDIOC_REQBUFS.
441 * Operations that require privileges are:
448 static int uvc_acquire_privileges(struct uvc_fh *handle)
450 /* Always succeed if the handle is already privileged. */
451 if (handle->state == UVC_HANDLE_ACTIVE)
454 /* Check if the device already has a privileged handle. */
455 if (atomic_inc_return(&handle->stream->active) != 1) {
456 atomic_dec(&handle->stream->active);
460 handle->state = UVC_HANDLE_ACTIVE;
464 static void uvc_dismiss_privileges(struct uvc_fh *handle)
466 if (handle->state == UVC_HANDLE_ACTIVE)
467 atomic_dec(&handle->stream->active);
469 handle->state = UVC_HANDLE_PASSIVE;
472 static int uvc_has_privileges(struct uvc_fh *handle)
474 return handle->state == UVC_HANDLE_ACTIVE;
477 /* ------------------------------------------------------------------------
478 * V4L2 file operations
481 static int uvc_v4l2_open(struct file *file)
483 struct uvc_streaming *stream;
484 struct uvc_fh *handle;
487 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
488 stream = video_drvdata(file);
490 if (stream->dev->state & UVC_DEV_DISCONNECTED)
493 ret = usb_autopm_get_interface(stream->dev->intf);
497 /* Create the device handle. */
498 handle = kzalloc(sizeof *handle, GFP_KERNEL);
499 if (handle == NULL) {
500 usb_autopm_put_interface(stream->dev->intf);
504 if (atomic_inc_return(&stream->dev->users) == 1) {
505 ret = uvc_status_start(stream->dev);
507 usb_autopm_put_interface(stream->dev->intf);
508 atomic_dec(&stream->dev->users);
514 handle->chain = stream->chain;
515 handle->stream = stream;
516 handle->state = UVC_HANDLE_PASSIVE;
517 file->private_data = handle;
522 static int uvc_v4l2_release(struct file *file)
524 struct uvc_fh *handle = file->private_data;
525 struct uvc_streaming *stream = handle->stream;
527 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");
529 /* Only free resources if this is a privileged handle. */
530 if (uvc_has_privileges(handle)) {
531 uvc_video_enable(stream, 0);
533 if (uvc_free_buffers(&stream->queue) < 0)
534 uvc_printk(KERN_ERR, "uvc_v4l2_release: Unable to "
538 /* Release the file handle. */
539 uvc_dismiss_privileges(handle);
541 file->private_data = NULL;
543 if (atomic_dec_return(&stream->dev->users) == 0)
544 uvc_status_stop(stream->dev);
546 usb_autopm_put_interface(stream->dev->intf);
550 static void uvc_v4l2_ioctl_warn(void)
557 uvc_printk(KERN_INFO, "Deprecated UVCIOC_CTRL_{ADD,MAP_OLD,GET,SET} "
558 "ioctls will be removed in 2.6.42.\n");
559 uvc_printk(KERN_INFO, "See http://www.ideasonboard.org/uvc/upgrade/ "
560 "for upgrade instructions.\n");
564 static long uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
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;
573 /* Query capabilities */
574 case VIDIOC_QUERYCAP:
576 struct v4l2_capability *cap = arg;
578 memset(cap, 0, sizeof *cap);
579 strlcpy(cap->driver, "uvcvideo", sizeof cap->driver);
580 strlcpy(cap->card, vdev->name, sizeof cap->card);
581 usb_make_path(stream->dev->udev,
582 cap->bus_info, sizeof(cap->bus_info));
583 cap->version = DRIVER_VERSION_NUMBER;
584 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
585 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
586 | V4L2_CAP_STREAMING;
588 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT
589 | V4L2_CAP_STREAMING;
593 /* Get, Set & Query control */
594 case VIDIOC_QUERYCTRL:
595 return uvc_query_v4l2_ctrl(chain, arg);
599 struct v4l2_control *ctrl = arg;
600 struct v4l2_ext_control xctrl;
602 memset(&xctrl, 0, sizeof xctrl);
605 ret = uvc_ctrl_begin(chain);
609 ret = uvc_ctrl_get(chain, &xctrl);
610 uvc_ctrl_rollback(chain);
612 ctrl->value = xctrl.value;
618 struct v4l2_control *ctrl = arg;
619 struct v4l2_ext_control xctrl;
621 memset(&xctrl, 0, sizeof xctrl);
623 xctrl.value = ctrl->value;
625 ret = uvc_ctrl_begin(chain);
629 ret = uvc_ctrl_set(chain, &xctrl);
631 uvc_ctrl_rollback(chain);
634 ret = uvc_ctrl_commit(chain);
636 ctrl->value = xctrl.value;
640 case VIDIOC_QUERYMENU:
641 return uvc_query_v4l2_menu(chain, arg);
643 case VIDIOC_G_EXT_CTRLS:
645 struct v4l2_ext_controls *ctrls = arg;
646 struct v4l2_ext_control *ctrl = ctrls->controls;
649 ret = uvc_ctrl_begin(chain);
653 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
654 ret = uvc_ctrl_get(chain, ctrl);
656 uvc_ctrl_rollback(chain);
657 ctrls->error_idx = i;
661 ctrls->error_idx = 0;
662 ret = uvc_ctrl_rollback(chain);
666 case VIDIOC_S_EXT_CTRLS:
667 case VIDIOC_TRY_EXT_CTRLS:
669 struct v4l2_ext_controls *ctrls = arg;
670 struct v4l2_ext_control *ctrl = ctrls->controls;
673 ret = uvc_ctrl_begin(chain);
677 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
678 ret = uvc_ctrl_set(chain, ctrl);
680 uvc_ctrl_rollback(chain);
681 ctrls->error_idx = i;
686 ctrls->error_idx = 0;
688 if (cmd == VIDIOC_S_EXT_CTRLS)
689 ret = uvc_ctrl_commit(chain);
691 ret = uvc_ctrl_rollback(chain);
695 /* Get, Set & Enum input */
696 case VIDIOC_ENUMINPUT:
698 const struct uvc_entity *selector = chain->selector;
699 struct v4l2_input *input = arg;
700 struct uvc_entity *iterm = NULL;
701 u32 index = input->index;
704 if (selector == NULL ||
705 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
708 list_for_each_entry(iterm, &chain->entities, chain) {
709 if (UVC_ENTITY_IS_ITERM(iterm))
713 } else if (index < selector->bNrInPins) {
714 pin = selector->baSourceID[index];
715 list_for_each_entry(iterm, &chain->entities, chain) {
716 if (!UVC_ENTITY_IS_ITERM(iterm))
718 if (iterm->id == pin)
723 if (iterm == NULL || iterm->id != pin)
726 memset(input, 0, sizeof *input);
727 input->index = index;
728 strlcpy(input->name, iterm->name, sizeof input->name);
729 if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
730 input->type = V4L2_INPUT_TYPE_CAMERA;
738 if (chain->selector == NULL ||
739 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
744 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
745 chain->selector->id, chain->dev->intfnum,
746 UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
750 *(int *)arg = input - 1;
756 u32 input = *(u32 *)arg + 1;
758 if ((ret = uvc_acquire_privileges(handle)) < 0)
761 if (chain->selector == NULL ||
762 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
768 if (input == 0 || input > chain->selector->bNrInPins)
771 return uvc_query_ctrl(chain->dev, UVC_SET_CUR,
772 chain->selector->id, chain->dev->intfnum,
773 UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
776 /* Try, Get, Set & Enum format */
777 case VIDIOC_ENUM_FMT:
779 struct v4l2_fmtdesc *fmt = arg;
780 struct uvc_format *format;
781 enum v4l2_buf_type type = fmt->type;
782 __u32 index = fmt->index;
784 if (fmt->type != stream->type ||
785 fmt->index >= stream->nformats)
788 memset(fmt, 0, sizeof(*fmt));
792 format = &stream->format[fmt->index];
794 if (format->flags & UVC_FMT_FLAG_COMPRESSED)
795 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
796 strlcpy(fmt->description, format->name,
797 sizeof fmt->description);
798 fmt->description[sizeof fmt->description - 1] = 0;
799 fmt->pixelformat = format->fcc;
805 struct uvc_streaming_control probe;
807 return uvc_v4l2_try_format(stream, arg, &probe, NULL, NULL);
811 if ((ret = uvc_acquire_privileges(handle)) < 0)
814 return uvc_v4l2_set_format(stream, arg);
817 return uvc_v4l2_get_format(stream, arg);
819 /* Frame size enumeration */
820 case VIDIOC_ENUM_FRAMESIZES:
822 struct v4l2_frmsizeenum *fsize = arg;
823 struct uvc_format *format = NULL;
824 struct uvc_frame *frame;
827 /* Look for the given pixel format */
828 for (i = 0; i < stream->nformats; i++) {
829 if (stream->format[i].fcc ==
830 fsize->pixel_format) {
831 format = &stream->format[i];
838 if (fsize->index >= format->nframes)
841 frame = &format->frame[fsize->index];
842 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
843 fsize->discrete.width = frame->wWidth;
844 fsize->discrete.height = frame->wHeight;
848 /* Frame interval enumeration */
849 case VIDIOC_ENUM_FRAMEINTERVALS:
851 struct v4l2_frmivalenum *fival = arg;
852 struct uvc_format *format = NULL;
853 struct uvc_frame *frame = NULL;
856 /* Look for the given pixel format and frame size */
857 for (i = 0; i < stream->nformats; i++) {
858 if (stream->format[i].fcc ==
859 fival->pixel_format) {
860 format = &stream->format[i];
867 for (i = 0; i < format->nframes; i++) {
868 if (format->frame[i].wWidth == fival->width &&
869 format->frame[i].wHeight == fival->height) {
870 frame = &format->frame[i];
877 if (frame->bFrameIntervalType) {
878 if (fival->index >= frame->bFrameIntervalType)
881 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
882 fival->discrete.numerator =
883 frame->dwFrameInterval[fival->index];
884 fival->discrete.denominator = 10000000;
885 uvc_simplify_fraction(&fival->discrete.numerator,
886 &fival->discrete.denominator, 8, 333);
888 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
889 fival->stepwise.min.numerator =
890 frame->dwFrameInterval[0];
891 fival->stepwise.min.denominator = 10000000;
892 fival->stepwise.max.numerator =
893 frame->dwFrameInterval[1];
894 fival->stepwise.max.denominator = 10000000;
895 fival->stepwise.step.numerator =
896 frame->dwFrameInterval[2];
897 fival->stepwise.step.denominator = 10000000;
898 uvc_simplify_fraction(&fival->stepwise.min.numerator,
899 &fival->stepwise.min.denominator, 8, 333);
900 uvc_simplify_fraction(&fival->stepwise.max.numerator,
901 &fival->stepwise.max.denominator, 8, 333);
902 uvc_simplify_fraction(&fival->stepwise.step.numerator,
903 &fival->stepwise.step.denominator, 8, 333);
908 /* Get & Set streaming parameters */
910 return uvc_v4l2_get_streamparm(stream, arg);
913 if ((ret = uvc_acquire_privileges(handle)) < 0)
916 return uvc_v4l2_set_streamparm(stream, arg);
918 /* Cropping and scaling */
921 struct v4l2_cropcap *ccap = arg;
923 if (ccap->type != stream->type)
926 ccap->bounds.left = 0;
927 ccap->bounds.top = 0;
929 mutex_lock(&stream->mutex);
930 ccap->bounds.width = stream->cur_frame->wWidth;
931 ccap->bounds.height = stream->cur_frame->wHeight;
932 mutex_unlock(&stream->mutex);
934 ccap->defrect = ccap->bounds;
936 ccap->pixelaspect.numerator = 1;
937 ccap->pixelaspect.denominator = 1;
945 /* Buffers & streaming */
948 struct v4l2_requestbuffers *rb = arg;
950 if (rb->type != stream->type ||
951 rb->memory != V4L2_MEMORY_MMAP)
954 if ((ret = uvc_acquire_privileges(handle)) < 0)
957 mutex_lock(&stream->mutex);
958 ret = uvc_alloc_buffers(&stream->queue, rb->count,
959 stream->ctrl.dwMaxVideoFrameSize);
960 mutex_unlock(&stream->mutex);
965 uvc_dismiss_privileges(handle);
972 case VIDIOC_QUERYBUF:
974 struct v4l2_buffer *buf = arg;
976 if (buf->type != stream->type)
979 if (!uvc_has_privileges(handle))
982 return uvc_query_buffer(&stream->queue, buf);
986 if (!uvc_has_privileges(handle))
989 return uvc_queue_buffer(&stream->queue, arg);
992 if (!uvc_has_privileges(handle))
995 return uvc_dequeue_buffer(&stream->queue, arg,
996 file->f_flags & O_NONBLOCK);
998 case VIDIOC_STREAMON:
1002 if (*type != stream->type)
1005 if (!uvc_has_privileges(handle))
1008 mutex_lock(&stream->mutex);
1009 ret = uvc_video_enable(stream, 1);
1010 mutex_unlock(&stream->mutex);
1016 case VIDIOC_STREAMOFF:
1020 if (*type != stream->type)
1023 if (!uvc_has_privileges(handle))
1026 return uvc_video_enable(stream, 0);
1029 /* Analog video standards make no sense for digital cameras. */
1030 case VIDIOC_ENUMSTD:
1031 case VIDIOC_QUERYSTD:
1035 case VIDIOC_OVERLAY:
1037 case VIDIOC_ENUMAUDIO:
1038 case VIDIOC_ENUMAUDOUT:
1040 case VIDIOC_ENUMOUTPUT:
1041 uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd);
1044 /* Dynamic controls. UVCIOC_CTRL_ADD, UVCIOC_CTRL_MAP_OLD,
1045 * UVCIOC_CTRL_GET and UVCIOC_CTRL_SET are deprecated and scheduled for
1046 * removal in 2.6.42.
1048 case __UVCIOC_CTRL_ADD:
1049 uvc_v4l2_ioctl_warn();
1052 case __UVCIOC_CTRL_MAP_OLD:
1053 uvc_v4l2_ioctl_warn();
1054 case __UVCIOC_CTRL_MAP:
1055 case UVCIOC_CTRL_MAP:
1056 return uvc_ioctl_ctrl_map(chain, arg,
1057 cmd == __UVCIOC_CTRL_MAP_OLD);
1059 case __UVCIOC_CTRL_GET:
1060 case __UVCIOC_CTRL_SET:
1062 struct uvc_xu_control *xctrl = arg;
1063 struct uvc_xu_control_query xqry = {
1064 .unit = xctrl->unit,
1065 .selector = xctrl->selector,
1066 .query = cmd == __UVCIOC_CTRL_GET
1067 ? UVC_GET_CUR : UVC_SET_CUR,
1068 .size = xctrl->size,
1069 .data = xctrl->data,
1072 uvc_v4l2_ioctl_warn();
1073 return uvc_xu_ctrl_query(chain, &xqry);
1076 case UVCIOC_CTRL_QUERY:
1077 return uvc_xu_ctrl_query(chain, arg);
1080 uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n", cmd);
1087 static long uvc_v4l2_ioctl(struct file *file,
1088 unsigned int cmd, unsigned long arg)
1090 if (uvc_trace_param & UVC_TRACE_IOCTL) {
1091 uvc_printk(KERN_DEBUG, "uvc_v4l2_ioctl(");
1092 v4l_printk_ioctl(cmd);
1096 return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl);
1099 static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1100 size_t count, loff_t *ppos)
1102 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
1106 static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1108 struct uvc_fh *handle = file->private_data;
1109 struct uvc_streaming *stream = handle->stream;
1111 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
1113 return uvc_queue_mmap(&stream->queue, vma);
1116 static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
1118 struct uvc_fh *handle = file->private_data;
1119 struct uvc_streaming *stream = handle->stream;
1121 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
1123 return uvc_queue_poll(&stream->queue, file, wait);
1127 static unsigned long uvc_v4l2_get_unmapped_area(struct file *file,
1128 unsigned long addr, unsigned long len, unsigned long pgoff,
1129 unsigned long flags)
1131 struct uvc_fh *handle = file->private_data;
1132 struct uvc_streaming *stream = handle->stream;
1134 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_get_unmapped_area\n");
1136 return uvc_queue_get_unmapped_area(&stream->queue, pgoff);
1140 const struct v4l2_file_operations uvc_fops = {
1141 .owner = THIS_MODULE,
1142 .open = uvc_v4l2_open,
1143 .release = uvc_v4l2_release,
1144 .unlocked_ioctl = uvc_v4l2_ioctl,
1145 .read = uvc_v4l2_read,
1146 .mmap = uvc_v4l2_mmap,
1147 .poll = uvc_v4l2_poll,
1149 .get_unmapped_area = uvc_v4l2_get_unmapped_area,