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 size = xmap->menu_count * sizeof(*map->menu_info);
69 map->menu_info = kmalloc(size, GFP_KERNEL);
70 if (map->menu_info == NULL) {
75 if (copy_from_user(map->menu_info, xmap->menu_info, size)) {
80 map->menu_count = xmap->menu_count;
84 uvc_trace(UVC_TRACE_CONTROL, "Unsupported V4L2 control type "
85 "%u.\n", xmap->v4l2_type);
90 ret = uvc_ctrl_add_mapping(chain, map);
93 kfree(map->menu_info);
99 /* ------------------------------------------------------------------------
104 * Mapping V4L2 controls to UVC controls can be straighforward if done well.
105 * Most of the UVC controls exist in V4L2, and can be mapped directly. Some
106 * must be grouped (for instance the Red Balance, Blue Balance and Do White
107 * Balance V4L2 controls use the White Balance Component UVC control) or
108 * otherwise translated. The approach we take here is to use a translation
109 * table for the controls that can be mapped directly, and handle the others
112 static int uvc_v4l2_query_menu(struct uvc_video_chain *chain,
113 struct v4l2_querymenu *query_menu)
115 struct uvc_menu_info *menu_info;
116 struct uvc_control_mapping *mapping;
117 struct uvc_control *ctrl;
118 u32 index = query_menu->index;
119 u32 id = query_menu->id;
121 ctrl = uvc_find_control(chain, query_menu->id, &mapping);
122 if (ctrl == NULL || mapping->v4l2_type != V4L2_CTRL_TYPE_MENU)
125 if (query_menu->index >= mapping->menu_count)
128 memset(query_menu, 0, sizeof(*query_menu));
130 query_menu->index = index;
132 menu_info = &mapping->menu_info[query_menu->index];
133 strlcpy(query_menu->name, menu_info->name, sizeof query_menu->name);
138 * Find the frame interval closest to the requested frame interval for the
139 * given frame format and size. This should be done by the device as part of
140 * the Video Probe and Commit negotiation, but some hardware don't implement
143 static __u32 uvc_try_frame_interval(struct uvc_frame *frame, __u32 interval)
147 if (frame->bFrameIntervalType) {
148 __u32 best = -1, dist;
150 for (i = 0; i < frame->bFrameIntervalType; ++i) {
151 dist = interval > frame->dwFrameInterval[i]
152 ? interval - frame->dwFrameInterval[i]
153 : frame->dwFrameInterval[i] - interval;
161 interval = frame->dwFrameInterval[i-1];
163 const __u32 min = frame->dwFrameInterval[0];
164 const __u32 max = frame->dwFrameInterval[1];
165 const __u32 step = frame->dwFrameInterval[2];
167 interval = min + (interval - min + step/2) / step * step;
175 static int uvc_v4l2_try_format(struct uvc_streaming *stream,
176 struct v4l2_format *fmt, struct uvc_streaming_control *probe,
177 struct uvc_format **uvc_format, struct uvc_frame **uvc_frame)
179 struct uvc_format *format = NULL;
180 struct uvc_frame *frame = NULL;
182 unsigned int d, maxd;
188 if (fmt->type != stream->type)
191 fcc = (__u8 *)&fmt->fmt.pix.pixelformat;
192 uvc_trace(UVC_TRACE_FORMAT, "Trying format 0x%08x (%c%c%c%c): %ux%u.\n",
193 fmt->fmt.pix.pixelformat,
194 fcc[0], fcc[1], fcc[2], fcc[3],
195 fmt->fmt.pix.width, fmt->fmt.pix.height);
197 /* Check if the hardware supports the requested format. */
198 for (i = 0; i < stream->nformats; ++i) {
199 format = &stream->format[i];
200 if (format->fcc == fmt->fmt.pix.pixelformat)
204 if (format == NULL || format->fcc != fmt->fmt.pix.pixelformat) {
205 uvc_trace(UVC_TRACE_FORMAT, "Unsupported format 0x%08x.\n",
206 fmt->fmt.pix.pixelformat);
210 /* Find the closest image size. The distance between image sizes is
211 * the size in pixels of the non-overlapping regions between the
212 * requested size and the frame-specified size.
214 rw = fmt->fmt.pix.width;
215 rh = fmt->fmt.pix.height;
216 maxd = (unsigned int)-1;
218 for (i = 0; i < format->nframes; ++i) {
219 __u16 w = format->frame[i].wWidth;
220 __u16 h = format->frame[i].wHeight;
222 d = min(w, rw) * min(h, rh);
223 d = w*h + rw*rh - 2*d;
226 frame = &format->frame[i];
234 uvc_trace(UVC_TRACE_FORMAT, "Unsupported size %ux%u.\n",
235 fmt->fmt.pix.width, fmt->fmt.pix.height);
239 /* Use the default frame interval. */
240 interval = frame->dwDefaultFrameInterval;
241 uvc_trace(UVC_TRACE_FORMAT, "Using default frame interval %u.%u us "
242 "(%u.%u fps).\n", interval/10, interval%10, 10000000/interval,
243 (100000000/interval)%10);
245 /* Set the format index, frame index and frame interval. */
246 memset(probe, 0, sizeof *probe);
247 probe->bmHint = 1; /* dwFrameInterval */
248 probe->bFormatIndex = format->index;
249 probe->bFrameIndex = frame->bFrameIndex;
250 probe->dwFrameInterval = uvc_try_frame_interval(frame, interval);
251 /* Some webcams stall the probe control set request when the
252 * dwMaxVideoFrameSize field is set to zero. The UVC specification
253 * clearly states that the field is read-only from the host, so this
254 * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by
255 * the webcam to work around the problem.
257 * The workaround could probably be enabled for all webcams, so the
258 * quirk can be removed if needed. It's currently useful to detect
259 * webcam bugs and fix them before they hit the market (providing
260 * developers test their webcams with the Linux driver as well as with
261 * the Windows driver).
263 if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS)
264 probe->dwMaxVideoFrameSize =
265 stream->ctrl.dwMaxVideoFrameSize;
267 /* Probe the device. */
268 ret = uvc_probe_video(stream, probe);
272 fmt->fmt.pix.width = frame->wWidth;
273 fmt->fmt.pix.height = frame->wHeight;
274 fmt->fmt.pix.field = V4L2_FIELD_NONE;
275 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
276 fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize;
277 fmt->fmt.pix.colorspace = format->colorspace;
278 fmt->fmt.pix.priv = 0;
280 if (uvc_format != NULL)
281 *uvc_format = format;
282 if (uvc_frame != NULL)
289 static int uvc_v4l2_get_format(struct uvc_streaming *stream,
290 struct v4l2_format *fmt)
292 struct uvc_format *format = stream->cur_format;
293 struct uvc_frame *frame = stream->cur_frame;
295 if (fmt->type != stream->type)
298 if (format == NULL || frame == NULL)
301 fmt->fmt.pix.pixelformat = format->fcc;
302 fmt->fmt.pix.width = frame->wWidth;
303 fmt->fmt.pix.height = frame->wHeight;
304 fmt->fmt.pix.field = V4L2_FIELD_NONE;
305 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
306 fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize;
307 fmt->fmt.pix.colorspace = format->colorspace;
308 fmt->fmt.pix.priv = 0;
313 static int uvc_v4l2_set_format(struct uvc_streaming *stream,
314 struct v4l2_format *fmt)
316 struct uvc_streaming_control probe;
317 struct uvc_format *format;
318 struct uvc_frame *frame;
321 if (fmt->type != stream->type)
324 if (uvc_queue_allocated(&stream->queue))
327 ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame);
331 memcpy(&stream->ctrl, &probe, sizeof probe);
332 stream->cur_format = format;
333 stream->cur_frame = frame;
338 static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream,
339 struct v4l2_streamparm *parm)
341 uint32_t numerator, denominator;
343 if (parm->type != stream->type)
346 numerator = stream->ctrl.dwFrameInterval;
347 denominator = 10000000;
348 uvc_simplify_fraction(&numerator, &denominator, 8, 333);
350 memset(parm, 0, sizeof *parm);
351 parm->type = stream->type;
353 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
354 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
355 parm->parm.capture.capturemode = 0;
356 parm->parm.capture.timeperframe.numerator = numerator;
357 parm->parm.capture.timeperframe.denominator = denominator;
358 parm->parm.capture.extendedmode = 0;
359 parm->parm.capture.readbuffers = 0;
361 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
362 parm->parm.output.outputmode = 0;
363 parm->parm.output.timeperframe.numerator = numerator;
364 parm->parm.output.timeperframe.denominator = denominator;
370 static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
371 struct v4l2_streamparm *parm)
373 struct uvc_frame *frame = stream->cur_frame;
374 struct uvc_streaming_control probe;
375 struct v4l2_fract timeperframe;
379 if (parm->type != stream->type)
382 if (uvc_queue_streaming(&stream->queue))
385 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
386 timeperframe = parm->parm.capture.timeperframe;
388 timeperframe = parm->parm.output.timeperframe;
390 memcpy(&probe, &stream->ctrl, sizeof probe);
391 interval = uvc_fraction_to_interval(timeperframe.numerator,
392 timeperframe.denominator);
394 uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
395 timeperframe.numerator, timeperframe.denominator, interval);
396 probe.dwFrameInterval = uvc_try_frame_interval(frame, interval);
398 /* Probe the device with the new settings. */
399 ret = uvc_probe_video(stream, &probe);
403 memcpy(&stream->ctrl, &probe, sizeof probe);
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 if (atomic_inc_return(&stream->dev->users) == 1) {
503 ret = uvc_status_start(stream->dev);
505 usb_autopm_put_interface(stream->dev->intf);
506 atomic_dec(&stream->dev->users);
512 handle->chain = stream->chain;
513 handle->stream = stream;
514 handle->state = UVC_HANDLE_PASSIVE;
515 file->private_data = handle;
520 static int uvc_v4l2_release(struct file *file)
522 struct uvc_fh *handle = file->private_data;
523 struct uvc_streaming *stream = handle->stream;
525 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");
527 /* Only free resources if this is a privileged handle. */
528 if (uvc_has_privileges(handle)) {
529 uvc_video_enable(stream, 0);
531 mutex_lock(&stream->queue.mutex);
532 if (uvc_free_buffers(&stream->queue) < 0)
533 uvc_printk(KERN_ERR, "uvc_v4l2_release: Unable to "
535 mutex_unlock(&stream->queue.mutex);
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 long uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
552 struct video_device *vdev = video_devdata(file);
553 struct uvc_fh *handle = file->private_data;
554 struct uvc_video_chain *chain = handle->chain;
555 struct uvc_streaming *stream = handle->stream;
559 /* Query capabilities */
560 case VIDIOC_QUERYCAP:
562 struct v4l2_capability *cap = arg;
564 memset(cap, 0, sizeof *cap);
565 strlcpy(cap->driver, "uvcvideo", sizeof cap->driver);
566 strlcpy(cap->card, vdev->name, sizeof cap->card);
567 usb_make_path(stream->dev->udev,
568 cap->bus_info, sizeof(cap->bus_info));
569 cap->version = DRIVER_VERSION_NUMBER;
570 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
571 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
572 | V4L2_CAP_STREAMING;
574 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT
575 | V4L2_CAP_STREAMING;
579 /* Get, Set & Query control */
580 case VIDIOC_QUERYCTRL:
581 return uvc_query_v4l2_ctrl(chain, arg);
585 struct v4l2_control *ctrl = arg;
586 struct v4l2_ext_control xctrl;
588 memset(&xctrl, 0, sizeof xctrl);
591 ret = uvc_ctrl_begin(chain);
595 ret = uvc_ctrl_get(chain, &xctrl);
596 uvc_ctrl_rollback(chain);
598 ctrl->value = xctrl.value;
604 struct v4l2_control *ctrl = arg;
605 struct v4l2_ext_control xctrl;
607 memset(&xctrl, 0, sizeof xctrl);
609 xctrl.value = ctrl->value;
611 ret = uvc_ctrl_begin(chain);
615 ret = uvc_ctrl_set(chain, &xctrl);
617 uvc_ctrl_rollback(chain);
620 ret = uvc_ctrl_commit(chain);
622 ctrl->value = xctrl.value;
626 case VIDIOC_QUERYMENU:
627 return uvc_v4l2_query_menu(chain, arg);
629 case VIDIOC_G_EXT_CTRLS:
631 struct v4l2_ext_controls *ctrls = arg;
632 struct v4l2_ext_control *ctrl = ctrls->controls;
635 ret = uvc_ctrl_begin(chain);
639 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
640 ret = uvc_ctrl_get(chain, ctrl);
642 uvc_ctrl_rollback(chain);
643 ctrls->error_idx = i;
647 ctrls->error_idx = 0;
648 ret = uvc_ctrl_rollback(chain);
652 case VIDIOC_S_EXT_CTRLS:
653 case VIDIOC_TRY_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_set(chain, ctrl);
666 uvc_ctrl_rollback(chain);
667 ctrls->error_idx = i;
672 ctrls->error_idx = 0;
674 if (cmd == VIDIOC_S_EXT_CTRLS)
675 ret = uvc_ctrl_commit(chain);
677 ret = uvc_ctrl_rollback(chain);
681 /* Get, Set & Enum input */
682 case VIDIOC_ENUMINPUT:
684 const struct uvc_entity *selector = chain->selector;
685 struct v4l2_input *input = arg;
686 struct uvc_entity *iterm = NULL;
687 u32 index = input->index;
690 if (selector == NULL ||
691 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
694 list_for_each_entry(iterm, &chain->entities, chain) {
695 if (UVC_ENTITY_IS_ITERM(iterm))
699 } else if (pin < selector->bNrInPins) {
700 pin = selector->baSourceID[index];
701 list_for_each_entry(iterm, &chain->entities, chain) {
702 if (!UVC_ENTITY_IS_ITERM(iterm))
704 if (iterm->id == pin)
709 if (iterm == NULL || iterm->id != pin)
712 memset(input, 0, sizeof *input);
713 input->index = index;
714 strlcpy(input->name, iterm->name, sizeof input->name);
715 if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
716 input->type = V4L2_INPUT_TYPE_CAMERA;
724 if (chain->selector == NULL ||
725 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
730 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
731 chain->selector->id, chain->dev->intfnum,
732 UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
736 *(int *)arg = input - 1;
742 u32 input = *(u32 *)arg + 1;
744 if ((ret = uvc_acquire_privileges(handle)) < 0)
747 if (chain->selector == NULL ||
748 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
754 if (input == 0 || input > chain->selector->bNrInPins)
757 return uvc_query_ctrl(chain->dev, UVC_SET_CUR,
758 chain->selector->id, chain->dev->intfnum,
759 UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
762 /* Try, Get, Set & Enum format */
763 case VIDIOC_ENUM_FMT:
765 struct v4l2_fmtdesc *fmt = arg;
766 struct uvc_format *format;
767 enum v4l2_buf_type type = fmt->type;
768 __u32 index = fmt->index;
770 if (fmt->type != stream->type ||
771 fmt->index >= stream->nformats)
774 memset(fmt, 0, sizeof(*fmt));
778 format = &stream->format[fmt->index];
780 if (format->flags & UVC_FMT_FLAG_COMPRESSED)
781 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
782 strlcpy(fmt->description, format->name,
783 sizeof fmt->description);
784 fmt->description[sizeof fmt->description - 1] = 0;
785 fmt->pixelformat = format->fcc;
791 struct uvc_streaming_control probe;
793 return uvc_v4l2_try_format(stream, arg, &probe, NULL, NULL);
797 if ((ret = uvc_acquire_privileges(handle)) < 0)
800 return uvc_v4l2_set_format(stream, arg);
803 return uvc_v4l2_get_format(stream, arg);
805 /* Frame size enumeration */
806 case VIDIOC_ENUM_FRAMESIZES:
808 struct v4l2_frmsizeenum *fsize = arg;
809 struct uvc_format *format = NULL;
810 struct uvc_frame *frame;
813 /* Look for the given pixel format */
814 for (i = 0; i < stream->nformats; i++) {
815 if (stream->format[i].fcc ==
816 fsize->pixel_format) {
817 format = &stream->format[i];
824 if (fsize->index >= format->nframes)
827 frame = &format->frame[fsize->index];
828 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
829 fsize->discrete.width = frame->wWidth;
830 fsize->discrete.height = frame->wHeight;
834 /* Frame interval enumeration */
835 case VIDIOC_ENUM_FRAMEINTERVALS:
837 struct v4l2_frmivalenum *fival = arg;
838 struct uvc_format *format = NULL;
839 struct uvc_frame *frame = NULL;
842 /* Look for the given pixel format and frame size */
843 for (i = 0; i < stream->nformats; i++) {
844 if (stream->format[i].fcc ==
845 fival->pixel_format) {
846 format = &stream->format[i];
853 for (i = 0; i < format->nframes; i++) {
854 if (format->frame[i].wWidth == fival->width &&
855 format->frame[i].wHeight == fival->height) {
856 frame = &format->frame[i];
863 if (frame->bFrameIntervalType) {
864 if (fival->index >= frame->bFrameIntervalType)
867 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
868 fival->discrete.numerator =
869 frame->dwFrameInterval[fival->index];
870 fival->discrete.denominator = 10000000;
871 uvc_simplify_fraction(&fival->discrete.numerator,
872 &fival->discrete.denominator, 8, 333);
874 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
875 fival->stepwise.min.numerator =
876 frame->dwFrameInterval[0];
877 fival->stepwise.min.denominator = 10000000;
878 fival->stepwise.max.numerator =
879 frame->dwFrameInterval[1];
880 fival->stepwise.max.denominator = 10000000;
881 fival->stepwise.step.numerator =
882 frame->dwFrameInterval[2];
883 fival->stepwise.step.denominator = 10000000;
884 uvc_simplify_fraction(&fival->stepwise.min.numerator,
885 &fival->stepwise.min.denominator, 8, 333);
886 uvc_simplify_fraction(&fival->stepwise.max.numerator,
887 &fival->stepwise.max.denominator, 8, 333);
888 uvc_simplify_fraction(&fival->stepwise.step.numerator,
889 &fival->stepwise.step.denominator, 8, 333);
894 /* Get & Set streaming parameters */
896 return uvc_v4l2_get_streamparm(stream, arg);
899 if ((ret = uvc_acquire_privileges(handle)) < 0)
902 return uvc_v4l2_set_streamparm(stream, arg);
904 /* Cropping and scaling */
907 struct v4l2_cropcap *ccap = arg;
908 struct uvc_frame *frame = stream->cur_frame;
910 if (ccap->type != stream->type)
913 ccap->bounds.left = 0;
914 ccap->bounds.top = 0;
915 ccap->bounds.width = frame->wWidth;
916 ccap->bounds.height = frame->wHeight;
918 ccap->defrect = ccap->bounds;
920 ccap->pixelaspect.numerator = 1;
921 ccap->pixelaspect.denominator = 1;
929 /* Buffers & streaming */
932 struct v4l2_requestbuffers *rb = arg;
933 unsigned int bufsize =
934 stream->ctrl.dwMaxVideoFrameSize;
936 if (rb->type != stream->type ||
937 rb->memory != V4L2_MEMORY_MMAP)
940 if ((ret = uvc_acquire_privileges(handle)) < 0)
943 ret = uvc_alloc_buffers(&stream->queue, rb->count, bufsize);
948 uvc_dismiss_privileges(handle);
955 case VIDIOC_QUERYBUF:
957 struct v4l2_buffer *buf = arg;
959 if (buf->type != stream->type)
962 if (!uvc_has_privileges(handle))
965 return uvc_query_buffer(&stream->queue, buf);
969 if (!uvc_has_privileges(handle))
972 return uvc_queue_buffer(&stream->queue, arg);
975 if (!uvc_has_privileges(handle))
978 return uvc_dequeue_buffer(&stream->queue, arg,
979 file->f_flags & O_NONBLOCK);
981 case VIDIOC_STREAMON:
985 if (*type != stream->type)
988 if (!uvc_has_privileges(handle))
991 ret = uvc_video_enable(stream, 1);
997 case VIDIOC_STREAMOFF:
1001 if (*type != stream->type)
1004 if (!uvc_has_privileges(handle))
1007 return uvc_video_enable(stream, 0);
1010 /* Analog video standards make no sense for digital cameras. */
1011 case VIDIOC_ENUMSTD:
1012 case VIDIOC_QUERYSTD:
1016 case VIDIOC_OVERLAY:
1018 case VIDIOC_ENUMAUDIO:
1019 case VIDIOC_ENUMAUDOUT:
1021 case VIDIOC_ENUMOUTPUT:
1022 uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd);
1025 /* Dynamic controls. */
1026 case UVCIOC_CTRL_ADD:
1027 /* Legacy ioctl, kept for API compatibility reasons */
1030 case UVCIOC_CTRL_MAP_OLD:
1031 case UVCIOC_CTRL_MAP:
1032 return uvc_ioctl_ctrl_map(chain, arg,
1033 cmd == UVCIOC_CTRL_MAP_OLD);
1035 case UVCIOC_CTRL_GET:
1036 return uvc_xu_ctrl_query(chain, arg, 0);
1038 case UVCIOC_CTRL_SET:
1039 return uvc_xu_ctrl_query(chain, arg, 1);
1042 if ((ret = v4l_compat_translate_ioctl(file, cmd, arg,
1043 uvc_v4l2_do_ioctl)) == -ENOIOCTLCMD)
1044 uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n",
1052 static long uvc_v4l2_ioctl(struct file *file,
1053 unsigned int cmd, unsigned long arg)
1055 if (uvc_trace_param & UVC_TRACE_IOCTL) {
1056 uvc_printk(KERN_DEBUG, "uvc_v4l2_ioctl(");
1057 v4l_printk_ioctl(cmd);
1061 return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl);
1064 static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1065 size_t count, loff_t *ppos)
1067 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
1074 static void uvc_vm_open(struct vm_area_struct *vma)
1076 struct uvc_buffer *buffer = vma->vm_private_data;
1077 buffer->vma_use_count++;
1080 static void uvc_vm_close(struct vm_area_struct *vma)
1082 struct uvc_buffer *buffer = vma->vm_private_data;
1083 buffer->vma_use_count--;
1086 static const struct vm_operations_struct uvc_vm_ops = {
1087 .open = uvc_vm_open,
1088 .close = uvc_vm_close,
1091 static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1093 struct uvc_fh *handle = file->private_data;
1094 struct uvc_streaming *stream = handle->stream;
1095 struct uvc_video_queue *queue = &stream->queue;
1096 struct uvc_buffer *uninitialized_var(buffer);
1098 unsigned long addr, start, size;
1102 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
1104 start = vma->vm_start;
1105 size = vma->vm_end - vma->vm_start;
1107 mutex_lock(&queue->mutex);
1109 for (i = 0; i < queue->count; ++i) {
1110 buffer = &queue->buffer[i];
1111 if ((buffer->buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
1115 if (i == queue->count || size != queue->buf_size) {
1121 * VM_IO marks the area as being an mmaped region for I/O to a
1122 * device. It also prevents the region from being core dumped.
1124 vma->vm_flags |= VM_IO;
1126 addr = (unsigned long)queue->mem + buffer->buf.m.offset;
1128 page = vmalloc_to_page((void *)addr);
1129 if ((ret = vm_insert_page(vma, start, page)) < 0)
1137 vma->vm_ops = &uvc_vm_ops;
1138 vma->vm_private_data = buffer;
1142 mutex_unlock(&queue->mutex);
1146 static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
1148 struct uvc_fh *handle = file->private_data;
1149 struct uvc_streaming *stream = handle->stream;
1151 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
1153 return uvc_queue_poll(&stream->queue, file, wait);
1156 const struct v4l2_file_operations uvc_fops = {
1157 .owner = THIS_MODULE,
1158 .open = uvc_v4l2_open,
1159 .release = uvc_v4l2_release,
1160 .ioctl = uvc_v4l2_ioctl,
1161 .read = uvc_v4l2_read,
1162 .mmap = uvc_v4l2_mmap,
1163 .poll = uvc_v4l2_poll,