2 * uvc_v4l2.c -- USB Video Class driver - V4L2 API
4 * Copyright (C) 2005-2009
5 * Laurent Pinchart (laurent.pinchart@skynet.be)
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_xu_control_mapping *xmap, int old)
36 struct uvc_control_mapping *map;
40 map = kzalloc(sizeof *map, GFP_KERNEL);
45 memcpy(map->name, xmap->name, sizeof map->name);
46 memcpy(map->entity, xmap->entity, sizeof map->entity);
47 map->selector = xmap->selector;
48 map->size = xmap->size;
49 map->offset = xmap->offset;
50 map->v4l2_type = xmap->v4l2_type;
51 map->data_type = xmap->data_type;
53 switch (xmap->v4l2_type) {
54 case V4L2_CTRL_TYPE_INTEGER:
55 case V4L2_CTRL_TYPE_BOOLEAN:
56 case V4L2_CTRL_TYPE_BUTTON:
59 case V4L2_CTRL_TYPE_MENU:
65 size = xmap->menu_count * sizeof(*map->menu_info);
66 map->menu_info = kmalloc(size, GFP_KERNEL);
67 if (map->menu_info == NULL) {
72 if (copy_from_user(map->menu_info, xmap->menu_info, size)) {
77 map->menu_count = xmap->menu_count;
85 ret = uvc_ctrl_add_mapping(map);
89 kfree(map->menu_info);
96 /* ------------------------------------------------------------------------
101 * Mapping V4L2 controls to UVC controls can be straighforward if done well.
102 * Most of the UVC controls exist in V4L2, and can be mapped directly. Some
103 * must be grouped (for instance the Red Balance, Blue Balance and Do White
104 * Balance V4L2 controls use the White Balance Component UVC control) or
105 * otherwise translated. The approach we take here is to use a translation
106 * table for the controls that can be mapped directly, and handle the others
109 static int uvc_v4l2_query_menu(struct uvc_video_chain *chain,
110 struct v4l2_querymenu *query_menu)
112 struct uvc_menu_info *menu_info;
113 struct uvc_control_mapping *mapping;
114 struct uvc_control *ctrl;
115 u32 index = query_menu->index;
116 u32 id = query_menu->id;
118 ctrl = uvc_find_control(chain, query_menu->id, &mapping);
119 if (ctrl == NULL || mapping->v4l2_type != V4L2_CTRL_TYPE_MENU)
122 if (query_menu->index >= mapping->menu_count)
125 memset(query_menu, 0, sizeof(*query_menu));
127 query_menu->index = index;
129 menu_info = &mapping->menu_info[query_menu->index];
130 strlcpy(query_menu->name, menu_info->name, sizeof query_menu->name);
135 * Find the frame interval closest to the requested frame interval for the
136 * given frame format and size. This should be done by the device as part of
137 * the Video Probe and Commit negotiation, but some hardware don't implement
140 static __u32 uvc_try_frame_interval(struct uvc_frame *frame, __u32 interval)
144 if (frame->bFrameIntervalType) {
145 __u32 best = -1, dist;
147 for (i = 0; i < frame->bFrameIntervalType; ++i) {
148 dist = interval > frame->dwFrameInterval[i]
149 ? interval - frame->dwFrameInterval[i]
150 : frame->dwFrameInterval[i] - interval;
158 interval = frame->dwFrameInterval[i-1];
160 const __u32 min = frame->dwFrameInterval[0];
161 const __u32 max = frame->dwFrameInterval[1];
162 const __u32 step = frame->dwFrameInterval[2];
164 interval = min + (interval - min + step/2) / step * step;
172 static int uvc_v4l2_try_format(struct uvc_streaming *stream,
173 struct v4l2_format *fmt, struct uvc_streaming_control *probe,
174 struct uvc_format **uvc_format, struct uvc_frame **uvc_frame)
176 struct uvc_format *format = NULL;
177 struct uvc_frame *frame = NULL;
179 unsigned int d, maxd;
185 if (fmt->type != stream->type)
188 fcc = (__u8 *)&fmt->fmt.pix.pixelformat;
189 uvc_trace(UVC_TRACE_FORMAT, "Trying format 0x%08x (%c%c%c%c): %ux%u.\n",
190 fmt->fmt.pix.pixelformat,
191 fcc[0], fcc[1], fcc[2], fcc[3],
192 fmt->fmt.pix.width, fmt->fmt.pix.height);
194 /* Check if the hardware supports the requested format. */
195 for (i = 0; i < stream->nformats; ++i) {
196 format = &stream->format[i];
197 if (format->fcc == fmt->fmt.pix.pixelformat)
201 if (format == NULL || format->fcc != fmt->fmt.pix.pixelformat) {
202 uvc_trace(UVC_TRACE_FORMAT, "Unsupported format 0x%08x.\n",
203 fmt->fmt.pix.pixelformat);
207 /* Find the closest image size. The distance between image sizes is
208 * the size in pixels of the non-overlapping regions between the
209 * requested size and the frame-specified size.
211 rw = fmt->fmt.pix.width;
212 rh = fmt->fmt.pix.height;
213 maxd = (unsigned int)-1;
215 for (i = 0; i < format->nframes; ++i) {
216 __u16 w = format->frame[i].wWidth;
217 __u16 h = format->frame[i].wHeight;
219 d = min(w, rw) * min(h, rh);
220 d = w*h + rw*rh - 2*d;
223 frame = &format->frame[i];
231 uvc_trace(UVC_TRACE_FORMAT, "Unsupported size %ux%u.\n",
232 fmt->fmt.pix.width, fmt->fmt.pix.height);
236 /* Use the default frame interval. */
237 interval = frame->dwDefaultFrameInterval;
238 uvc_trace(UVC_TRACE_FORMAT, "Using default frame interval %u.%u us "
239 "(%u.%u fps).\n", interval/10, interval%10, 10000000/interval,
240 (100000000/interval)%10);
242 /* Set the format index, frame index and frame interval. */
243 memset(probe, 0, sizeof *probe);
244 probe->bmHint = 1; /* dwFrameInterval */
245 probe->bFormatIndex = format->index;
246 probe->bFrameIndex = frame->bFrameIndex;
247 probe->dwFrameInterval = uvc_try_frame_interval(frame, interval);
248 /* Some webcams stall the probe control set request when the
249 * dwMaxVideoFrameSize field is set to zero. The UVC specification
250 * clearly states that the field is read-only from the host, so this
251 * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by
252 * the webcam to work around the problem.
254 * The workaround could probably be enabled for all webcams, so the
255 * quirk can be removed if needed. It's currently useful to detect
256 * webcam bugs and fix them before they hit the market (providing
257 * developers test their webcams with the Linux driver as well as with
258 * the Windows driver).
260 if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS)
261 probe->dwMaxVideoFrameSize =
262 stream->ctrl.dwMaxVideoFrameSize;
264 /* Probe the device. */
265 ret = uvc_probe_video(stream, probe);
269 fmt->fmt.pix.width = frame->wWidth;
270 fmt->fmt.pix.height = frame->wHeight;
271 fmt->fmt.pix.field = V4L2_FIELD_NONE;
272 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
273 fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize;
274 fmt->fmt.pix.colorspace = format->colorspace;
275 fmt->fmt.pix.priv = 0;
277 if (uvc_format != NULL)
278 *uvc_format = format;
279 if (uvc_frame != NULL)
286 static int uvc_v4l2_get_format(struct uvc_streaming *stream,
287 struct v4l2_format *fmt)
289 struct uvc_format *format = stream->cur_format;
290 struct uvc_frame *frame = stream->cur_frame;
292 if (fmt->type != stream->type)
295 if (format == NULL || frame == NULL)
298 fmt->fmt.pix.pixelformat = format->fcc;
299 fmt->fmt.pix.width = frame->wWidth;
300 fmt->fmt.pix.height = frame->wHeight;
301 fmt->fmt.pix.field = V4L2_FIELD_NONE;
302 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
303 fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize;
304 fmt->fmt.pix.colorspace = format->colorspace;
305 fmt->fmt.pix.priv = 0;
310 static int uvc_v4l2_set_format(struct uvc_streaming *stream,
311 struct v4l2_format *fmt)
313 struct uvc_streaming_control probe;
314 struct uvc_format *format;
315 struct uvc_frame *frame;
318 if (fmt->type != stream->type) {
319 printk("uvc_v4l2_set_format, fmt->type(%d) != stream->type(%d)\n",fmt->type,stream->type);
323 if (uvc_queue_allocated(&stream->queue)) {
324 printk("uvc_queue_allocated failed\n");
329 ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame);
333 memcpy(&stream->ctrl, &probe, sizeof probe);
334 stream->cur_format = format;
335 stream->cur_frame = frame;
340 static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream,
341 struct v4l2_streamparm *parm)
343 uint32_t numerator, denominator;
345 if (parm->type != stream->type)
348 numerator = stream->ctrl.dwFrameInterval;
349 denominator = 10000000;
350 uvc_simplify_fraction(&numerator, &denominator, 8, 333);
352 memset(parm, 0, sizeof *parm);
353 parm->type = stream->type;
355 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
356 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
357 parm->parm.capture.capturemode = 0;
358 parm->parm.capture.timeperframe.numerator = numerator;
359 parm->parm.capture.timeperframe.denominator = denominator;
360 parm->parm.capture.extendedmode = 0;
361 parm->parm.capture.readbuffers = 0;
363 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
364 parm->parm.output.outputmode = 0;
365 parm->parm.output.timeperframe.numerator = numerator;
366 parm->parm.output.timeperframe.denominator = denominator;
372 static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
373 struct v4l2_streamparm *parm)
375 struct uvc_frame *frame = stream->cur_frame;
376 struct uvc_streaming_control probe;
377 struct v4l2_fract timeperframe;
381 if (parm->type != stream->type)
384 if (uvc_queue_streaming(&stream->queue))
387 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
388 timeperframe = parm->parm.capture.timeperframe;
390 timeperframe = parm->parm.output.timeperframe;
392 memcpy(&probe, &stream->ctrl, sizeof probe);
393 interval = uvc_fraction_to_interval(timeperframe.numerator,
394 timeperframe.denominator);
396 uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
397 timeperframe.numerator, timeperframe.denominator, interval);
398 probe.dwFrameInterval = uvc_try_frame_interval(frame, interval);
400 /* Probe the device with the new settings. */
401 ret = uvc_probe_video(stream, &probe);
405 memcpy(&stream->ctrl, &probe, sizeof probe);
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 mutex_lock(&stream->queue.mutex);
534 if (uvc_free_buffers(&stream->queue) < 0)
535 uvc_printk(KERN_ERR, "uvc_v4l2_release: Unable to "
537 mutex_unlock(&stream->queue.mutex);
540 /* Release the file handle. */
541 uvc_dismiss_privileges(handle);
543 file->private_data = NULL;
545 if (atomic_dec_return(&stream->dev->users) == 0)
546 uvc_status_stop(stream->dev);
548 usb_autopm_put_interface(stream->dev->intf);
552 static long uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
554 struct video_device *vdev = video_devdata(file);
555 struct uvc_fh *handle = file->private_data;
556 struct uvc_video_chain *chain = handle->chain;
557 struct uvc_streaming *stream = handle->stream;
561 /* Query capabilities */
562 case VIDIOC_QUERYCAP:
564 struct v4l2_capability *cap = arg;
566 memset(cap, 0, sizeof *cap);
567 strlcpy(cap->driver, "uvcvideo", sizeof cap->driver);
568 strlcpy(cap->card, vdev->name, sizeof cap->card);
569 usb_make_path(stream->dev->udev,
570 cap->bus_info, sizeof(cap->bus_info));
571 cap->version = DRIVER_VERSION_NUMBER;
572 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
573 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
574 | V4L2_CAP_STREAMING;
576 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT
577 | V4L2_CAP_STREAMING;
581 /* Get, Set & Query control */
582 case VIDIOC_QUERYCTRL:
583 return uvc_query_v4l2_ctrl(chain, arg);
587 struct v4l2_control *ctrl = arg;
588 struct v4l2_ext_control xctrl;
590 memset(&xctrl, 0, sizeof xctrl);
593 ret = uvc_ctrl_begin(chain);
597 ret = uvc_ctrl_get(chain, &xctrl);
598 uvc_ctrl_rollback(chain);
600 ctrl->value = xctrl.value;
606 struct v4l2_control *ctrl = arg;
607 struct v4l2_ext_control xctrl;
609 memset(&xctrl, 0, sizeof xctrl);
611 xctrl.value = ctrl->value;
613 ret = uvc_ctrl_begin(chain);
617 ret = uvc_ctrl_set(chain, &xctrl);
619 uvc_ctrl_rollback(chain);
622 ret = uvc_ctrl_commit(chain);
624 ctrl->value = xctrl.value;
628 case VIDIOC_QUERYMENU:
629 return uvc_v4l2_query_menu(chain, arg);
631 case VIDIOC_G_EXT_CTRLS:
633 struct v4l2_ext_controls *ctrls = arg;
634 struct v4l2_ext_control *ctrl = ctrls->controls;
637 ret = uvc_ctrl_begin(chain);
641 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
642 ret = uvc_ctrl_get(chain, ctrl);
644 uvc_ctrl_rollback(chain);
645 ctrls->error_idx = i;
649 ctrls->error_idx = 0;
650 ret = uvc_ctrl_rollback(chain);
654 case VIDIOC_S_EXT_CTRLS:
655 case VIDIOC_TRY_EXT_CTRLS:
657 struct v4l2_ext_controls *ctrls = arg;
658 struct v4l2_ext_control *ctrl = ctrls->controls;
661 ret = uvc_ctrl_begin(chain);
665 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
666 ret = uvc_ctrl_set(chain, ctrl);
668 uvc_ctrl_rollback(chain);
669 ctrls->error_idx = i;
674 ctrls->error_idx = 0;
676 if (cmd == VIDIOC_S_EXT_CTRLS)
677 ret = uvc_ctrl_commit(chain);
679 ret = uvc_ctrl_rollback(chain);
683 /* Get, Set & Enum input */
684 case VIDIOC_ENUMINPUT:
686 const struct uvc_entity *selector = chain->selector;
687 struct v4l2_input *input = arg;
688 struct uvc_entity *iterm = NULL;
689 u32 index = input->index;
692 if (selector == NULL ||
693 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
696 list_for_each_entry(iterm, &chain->entities, chain) {
697 if (UVC_ENTITY_IS_ITERM(iterm))
701 } else if (pin < selector->bNrInPins) {
702 pin = selector->baSourceID[index];
703 list_for_each_entry(iterm, &chain->entities, chain) {
704 if (!UVC_ENTITY_IS_ITERM(iterm))
706 if (iterm->id == pin)
711 if (iterm == NULL || iterm->id != pin)
714 memset(input, 0, sizeof *input);
715 input->index = index;
716 strlcpy(input->name, iterm->name, sizeof input->name);
717 if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
718 input->type = V4L2_INPUT_TYPE_CAMERA;
726 if (chain->selector == NULL ||
727 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
732 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
733 chain->selector->id, chain->dev->intfnum,
734 UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
738 *(int *)arg = input - 1;
744 u32 input = *(u32 *)arg + 1;
746 if ((ret = uvc_acquire_privileges(handle)) < 0)
749 if (chain->selector == NULL ||
750 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
756 if (input == 0 || input > chain->selector->bNrInPins)
759 return uvc_query_ctrl(chain->dev, UVC_SET_CUR,
760 chain->selector->id, chain->dev->intfnum,
761 UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
764 /* Try, Get, Set & Enum format */
765 case VIDIOC_ENUM_FMT:
767 struct v4l2_fmtdesc *fmt = arg;
768 struct uvc_format *format;
769 enum v4l2_buf_type type = fmt->type;
770 __u32 index = fmt->index;
772 if (fmt->type != stream->type ||
773 fmt->index >= stream->nformats)
776 memset(fmt, 0, sizeof(*fmt));
780 format = &stream->format[fmt->index];
782 if (format->flags & UVC_FMT_FLAG_COMPRESSED)
783 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
784 strlcpy(fmt->description, format->name,
785 sizeof fmt->description);
786 fmt->description[sizeof fmt->description - 1] = 0;
787 fmt->pixelformat = format->fcc;
793 struct uvc_streaming_control probe;
795 return uvc_v4l2_try_format(stream, arg, &probe, NULL, NULL);
799 if ((ret = uvc_acquire_privileges(handle)) < 0) {
800 printk("uvc_acquire_privileges error.");
804 return uvc_v4l2_set_format(stream, arg);
807 return uvc_v4l2_get_format(stream, arg);
809 /* Frame size enumeration */
810 case VIDIOC_ENUM_FRAMESIZES:
812 struct v4l2_frmsizeenum *fsize = arg;
813 struct uvc_format *format = NULL;
814 struct uvc_frame *frame;
817 /* Look for the given pixel format */
818 for (i = 0; i < stream->nformats; i++) {
819 if (stream->format[i].fcc ==
820 fsize->pixel_format) {
821 format = &stream->format[i];
828 if (fsize->index >= format->nframes)
831 frame = &format->frame[fsize->index];
832 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
833 fsize->discrete.width = frame->wWidth;
834 fsize->discrete.height = frame->wHeight;
838 /* Frame interval enumeration */
839 case VIDIOC_ENUM_FRAMEINTERVALS:
841 struct v4l2_frmivalenum *fival = arg;
842 struct uvc_format *format = NULL;
843 struct uvc_frame *frame = NULL;
846 /* Look for the given pixel format and frame size */
847 for (i = 0; i < stream->nformats; i++) {
848 if (stream->format[i].fcc ==
849 fival->pixel_format) {
850 format = &stream->format[i];
857 for (i = 0; i < format->nframes; i++) {
858 if (format->frame[i].wWidth == fival->width &&
859 format->frame[i].wHeight == fival->height) {
860 frame = &format->frame[i];
867 if (frame->bFrameIntervalType) {
868 if (fival->index >= frame->bFrameIntervalType)
871 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
872 fival->discrete.numerator =
873 frame->dwFrameInterval[fival->index];
874 fival->discrete.denominator = 10000000;
875 uvc_simplify_fraction(&fival->discrete.numerator,
876 &fival->discrete.denominator, 8, 333);
878 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
879 fival->stepwise.min.numerator =
880 frame->dwFrameInterval[0];
881 fival->stepwise.min.denominator = 10000000;
882 fival->stepwise.max.numerator =
883 frame->dwFrameInterval[1];
884 fival->stepwise.max.denominator = 10000000;
885 fival->stepwise.step.numerator =
886 frame->dwFrameInterval[2];
887 fival->stepwise.step.denominator = 10000000;
888 uvc_simplify_fraction(&fival->stepwise.min.numerator,
889 &fival->stepwise.min.denominator, 8, 333);
890 uvc_simplify_fraction(&fival->stepwise.max.numerator,
891 &fival->stepwise.max.denominator, 8, 333);
892 uvc_simplify_fraction(&fival->stepwise.step.numerator,
893 &fival->stepwise.step.denominator, 8, 333);
898 /* Get & Set streaming parameters */
900 return uvc_v4l2_get_streamparm(stream, arg);
903 if ((ret = uvc_acquire_privileges(handle)) < 0)
906 return uvc_v4l2_set_streamparm(stream, arg);
908 /* Cropping and scaling */
911 struct v4l2_cropcap *ccap = arg;
912 struct uvc_frame *frame = stream->cur_frame;
914 if (ccap->type != stream->type)
917 ccap->bounds.left = 0;
918 ccap->bounds.top = 0;
919 ccap->bounds.width = frame->wWidth;
920 ccap->bounds.height = frame->wHeight;
922 ccap->defrect = ccap->bounds;
924 ccap->pixelaspect.numerator = 1;
925 ccap->pixelaspect.denominator = 1;
933 /* Buffers & streaming */
936 struct v4l2_requestbuffers *rb = arg;
937 unsigned int bufsize =
938 stream->ctrl.dwMaxVideoFrameSize;
940 if (rb->type != stream->type ||
941 rb->memory != V4L2_MEMORY_MMAP)
944 if ((ret = uvc_acquire_privileges(handle)) < 0)
947 ret = uvc_alloc_buffers(&stream->queue, rb->count, bufsize);
952 uvc_dismiss_privileges(handle);
959 case VIDIOC_QUERYBUF:
961 struct v4l2_buffer *buf = arg;
963 if (buf->type != stream->type)
966 if (!uvc_has_privileges(handle))
969 return uvc_query_buffer(&stream->queue, buf);
973 if (!uvc_has_privileges(handle)) {
974 printk("uvcvideo: VIDIOC_QBUF uvc_has_privileges failed\n");
978 return uvc_queue_buffer(&stream->queue, arg);
981 if (!uvc_has_privileges(handle)) {
982 printk("uvcvideo: VIDIOC_DQBUF uvc_has_privileges failed\n");
986 return uvc_dequeue_buffer(&stream->queue, arg,
987 file->f_flags & O_NONBLOCK);
989 case VIDIOC_STREAMON:
993 if (*type != stream->type)
996 if (!uvc_has_privileges(handle))
999 ret = uvc_video_enable(stream, 1);
1005 case VIDIOC_STREAMOFF:
1009 if (*type != stream->type)
1012 if (!uvc_has_privileges(handle))
1015 return uvc_video_enable(stream, 0);
1018 /* Analog video standards make no sense for digital cameras. */
1019 case VIDIOC_ENUMSTD:
1020 case VIDIOC_QUERYSTD:
1024 case VIDIOC_OVERLAY:
1026 case VIDIOC_ENUMAUDIO:
1027 case VIDIOC_ENUMAUDOUT:
1029 case VIDIOC_ENUMOUTPUT:
1030 uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd);
1033 /* Dynamic controls. */
1034 case UVCIOC_CTRL_ADD:
1036 struct uvc_xu_control_info *xinfo = arg;
1037 struct uvc_control_info *info;
1039 if (!capable(CAP_SYS_ADMIN))
1042 if (xinfo->size == 0)
1045 info = kzalloc(sizeof *info, GFP_KERNEL);
1049 memcpy(info->entity, xinfo->entity, sizeof info->entity);
1050 info->index = xinfo->index;
1051 info->selector = xinfo->selector;
1052 info->size = xinfo->size;
1053 info->flags = xinfo->flags;
1055 info->flags |= UVC_CONTROL_GET_MIN | UVC_CONTROL_GET_MAX |
1056 UVC_CONTROL_GET_RES | UVC_CONTROL_GET_DEF |
1057 UVC_CONTROL_EXTENSION;
1059 ret = uvc_ctrl_add_info(info);
1065 case UVCIOC_CTRL_MAP_OLD:
1066 case UVCIOC_CTRL_MAP:
1067 if (!capable(CAP_SYS_ADMIN))
1070 return uvc_ioctl_ctrl_map(arg, cmd == UVCIOC_CTRL_MAP_OLD);
1072 case UVCIOC_CTRL_GET:
1073 return uvc_xu_ctrl_query(chain, arg, 0);
1075 case UVCIOC_CTRL_SET:
1076 return uvc_xu_ctrl_query(chain, arg, 1);
1079 if ((ret = v4l_compat_translate_ioctl(file, cmd, arg,
1080 uvc_v4l2_do_ioctl)) == -ENOIOCTLCMD)
1081 uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n",
1089 static long uvc_v4l2_ioctl(struct file *file,
1090 unsigned int cmd, unsigned long arg)
1092 if (uvc_trace_param & UVC_TRACE_IOCTL) {
1093 uvc_printk(KERN_DEBUG, "uvc_v4l2_ioctl(");
1094 v4l_printk_ioctl(cmd);
1098 return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl);
1101 static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1102 size_t count, loff_t *ppos)
1104 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
1111 static void uvc_vm_open(struct vm_area_struct *vma)
1113 struct uvc_buffer *buffer = vma->vm_private_data;
1114 buffer->vma_use_count++;
1117 static void uvc_vm_close(struct vm_area_struct *vma)
1119 struct uvc_buffer *buffer = vma->vm_private_data;
1120 buffer->vma_use_count--;
1123 static const struct vm_operations_struct uvc_vm_ops = {
1124 .open = uvc_vm_open,
1125 .close = uvc_vm_close,
1128 static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1130 struct uvc_fh *handle = file->private_data;
1131 struct uvc_streaming *stream = handle->stream;
1132 struct uvc_video_queue *queue = &stream->queue;
1133 struct uvc_buffer *uninitialized_var(buffer);
1135 unsigned long addr, start, size;
1139 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
1141 start = vma->vm_start;
1142 size = vma->vm_end - vma->vm_start;
1144 mutex_lock(&queue->mutex);
1146 for (i = 0; i < queue->count; ++i) {
1147 buffer = &queue->buffer[i];
1148 if ((buffer->buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
1152 if (i == queue->count || size != queue->buf_size) {
1158 * VM_IO marks the area as being an mmaped region for I/O to a
1159 * device. It also prevents the region from being core dumped.
1161 vma->vm_flags |= VM_IO;
1163 addr = (unsigned long)queue->mem + buffer->buf.m.offset;
1165 page = vmalloc_to_page((void *)addr);
1166 if ((ret = vm_insert_page(vma, start, page)) < 0)
1174 vma->vm_ops = &uvc_vm_ops;
1175 vma->vm_private_data = buffer;
1179 mutex_unlock(&queue->mutex);
1183 static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
1185 struct uvc_fh *handle = file->private_data;
1186 struct uvc_streaming *stream = handle->stream;
1188 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
1190 return uvc_queue_poll(&stream->queue, file, wait);
1193 const struct v4l2_file_operations uvc_fops = {
1194 .owner = THIS_MODULE,
1195 .open = uvc_v4l2_open,
1196 .release = uvc_v4l2_release,
1197 .ioctl = uvc_v4l2_ioctl,
1198 .read = uvc_v4l2_read,
1199 .mmap = uvc_v4l2_mmap,
1200 .poll = uvc_v4l2_poll,