2 * uvc_v4l2.c -- USB Video Class driver - V4L2 API
4 * Copyright (C) 2005-2008
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/usb.h>
19 #include <linux/videodev2.h>
20 #include <linux/vmalloc.h>
22 #include <linux/wait.h>
23 #include <asm/atomic.h>
25 #include <media/v4l2-common.h>
29 /* ------------------------------------------------------------------------
34 * Mapping V4L2 controls to UVC controls can be straighforward if done well.
35 * Most of the UVC controls exist in V4L2, and can be mapped directly. Some
36 * must be grouped (for instance the Red Balance, Blue Balance and Do White
37 * Balance V4L2 controls use the White Balance Component UVC control) or
38 * otherwise translated. The approach we take here is to use a translation
39 * table for the controls which can be mapped directly, and handle the others
42 static int uvc_v4l2_query_menu(struct uvc_video_device *video,
43 struct v4l2_querymenu *query_menu)
45 struct uvc_menu_info *menu_info;
46 struct uvc_control_mapping *mapping;
47 struct uvc_control *ctrl;
49 ctrl = uvc_find_control(video, query_menu->id, &mapping);
50 if (ctrl == NULL || mapping->v4l2_type != V4L2_CTRL_TYPE_MENU)
53 if (query_menu->index >= mapping->menu_count)
56 menu_info = &mapping->menu_info[query_menu->index];
57 strncpy(query_menu->name, menu_info->name, 32);
62 * Find the frame interval closest to the requested frame interval for the
63 * given frame format and size. This should be done by the device as part of
64 * the Video Probe and Commit negotiation, but some hardware don't implement
67 static __u32 uvc_try_frame_interval(struct uvc_frame *frame, __u32 interval)
71 if (frame->bFrameIntervalType) {
72 __u32 best = -1, dist;
74 for (i = 0; i < frame->bFrameIntervalType; ++i) {
75 dist = interval > frame->dwFrameInterval[i]
76 ? interval - frame->dwFrameInterval[i]
77 : frame->dwFrameInterval[i] - interval;
85 interval = frame->dwFrameInterval[i-1];
87 const __u32 min = frame->dwFrameInterval[0];
88 const __u32 max = frame->dwFrameInterval[1];
89 const __u32 step = frame->dwFrameInterval[2];
91 interval = min + (interval - min + step/2) / step * step;
99 static int uvc_v4l2_try_format(struct uvc_video_device *video,
100 struct v4l2_format *fmt, struct uvc_streaming_control *probe,
101 struct uvc_format **uvc_format, struct uvc_frame **uvc_frame)
103 struct uvc_format *format = NULL;
104 struct uvc_frame *frame = NULL;
106 unsigned int d, maxd;
112 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
115 fcc = (__u8 *)&fmt->fmt.pix.pixelformat;
116 uvc_trace(UVC_TRACE_FORMAT, "Trying format 0x%08x (%c%c%c%c): %ux%u.\n",
117 fmt->fmt.pix.pixelformat,
118 fcc[0], fcc[1], fcc[2], fcc[3],
119 fmt->fmt.pix.width, fmt->fmt.pix.height);
121 /* Check if the hardware supports the requested format. */
122 for (i = 0; i < video->streaming->nformats; ++i) {
123 format = &video->streaming->format[i];
124 if (format->fcc == fmt->fmt.pix.pixelformat)
128 if (format == NULL || format->fcc != fmt->fmt.pix.pixelformat) {
129 uvc_trace(UVC_TRACE_FORMAT, "Unsupported format 0x%08x.\n",
130 fmt->fmt.pix.pixelformat);
134 /* Find the closest image size. The distance between image sizes is
135 * the size in pixels of the non-overlapping regions between the
136 * requested size and the frame-specified size.
138 rw = fmt->fmt.pix.width;
139 rh = fmt->fmt.pix.height;
140 maxd = (unsigned int)-1;
142 for (i = 0; i < format->nframes; ++i) {
143 __u16 w = format->frame[i].wWidth;
144 __u16 h = format->frame[i].wHeight;
146 d = min(w, rw) * min(h, rh);
147 d = w*h + rw*rh - 2*d;
150 frame = &format->frame[i];
158 uvc_trace(UVC_TRACE_FORMAT, "Unsupported size %ux%u.\n",
159 fmt->fmt.pix.width, fmt->fmt.pix.height);
163 /* Use the default frame interval. */
164 interval = frame->dwDefaultFrameInterval;
165 uvc_trace(UVC_TRACE_FORMAT, "Using default frame interval %u.%u us "
166 "(%u.%u fps).\n", interval/10, interval%10, 10000000/interval,
167 (100000000/interval)%10);
169 /* Set the format index, frame index and frame interval. */
170 memset(probe, 0, sizeof *probe);
171 probe->bmHint = 1; /* dwFrameInterval */
172 probe->bFormatIndex = format->index;
173 probe->bFrameIndex = frame->bFrameIndex;
174 probe->dwFrameInterval = uvc_try_frame_interval(frame, interval);
175 /* Some webcams stall the probe control set request when the
176 * dwMaxVideoFrameSize field is set to zero. The UVC specification
177 * clearly states that the field is read-only from the host, so this
178 * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by
179 * the webcam to work around the problem.
181 * The workaround could probably be enabled for all webcams, so the
182 * quirk can be removed if needed. It's currently useful to detect
183 * webcam bugs and fix them before they hit the market (providing
184 * developers test their webcams with the Linux driver as well as with
185 * the Windows driver).
187 if (video->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS)
188 probe->dwMaxVideoFrameSize =
189 video->streaming->ctrl.dwMaxVideoFrameSize;
191 /* Probe the device */
192 if ((ret = uvc_probe_video(video, probe)) < 0)
195 fmt->fmt.pix.width = frame->wWidth;
196 fmt->fmt.pix.height = frame->wHeight;
197 fmt->fmt.pix.field = V4L2_FIELD_NONE;
198 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
199 fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize;
200 fmt->fmt.pix.colorspace = format->colorspace;
201 fmt->fmt.pix.priv = 0;
203 if (uvc_format != NULL)
204 *uvc_format = format;
205 if (uvc_frame != NULL)
212 static int uvc_v4l2_get_format(struct uvc_video_device *video,
213 struct v4l2_format *fmt)
215 struct uvc_format *format = video->streaming->cur_format;
216 struct uvc_frame *frame = video->streaming->cur_frame;
218 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
221 if (format == NULL || frame == NULL)
224 fmt->fmt.pix.pixelformat = format->fcc;
225 fmt->fmt.pix.width = frame->wWidth;
226 fmt->fmt.pix.height = frame->wHeight;
227 fmt->fmt.pix.field = V4L2_FIELD_NONE;
228 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
229 fmt->fmt.pix.sizeimage = video->streaming->ctrl.dwMaxVideoFrameSize;
230 fmt->fmt.pix.colorspace = format->colorspace;
231 fmt->fmt.pix.priv = 0;
236 static int uvc_v4l2_set_format(struct uvc_video_device *video,
237 struct v4l2_format *fmt)
239 struct uvc_streaming_control probe;
240 struct uvc_format *format;
241 struct uvc_frame *frame;
244 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
247 if (uvc_queue_streaming(&video->queue))
250 ret = uvc_v4l2_try_format(video, fmt, &probe, &format, &frame);
254 if ((ret = uvc_set_video_ctrl(video, &probe, 0)) < 0)
257 memcpy(&video->streaming->ctrl, &probe, sizeof probe);
258 video->streaming->cur_format = format;
259 video->streaming->cur_frame = frame;
264 static int uvc_v4l2_get_streamparm(struct uvc_video_device *video,
265 struct v4l2_streamparm *parm)
267 uint32_t numerator, denominator;
269 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
272 numerator = video->streaming->ctrl.dwFrameInterval;
273 denominator = 10000000;
274 uvc_simplify_fraction(&numerator, &denominator, 8, 333);
276 memset(parm, 0, sizeof *parm);
277 parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
278 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
279 parm->parm.capture.capturemode = 0;
280 parm->parm.capture.timeperframe.numerator = numerator;
281 parm->parm.capture.timeperframe.denominator = denominator;
282 parm->parm.capture.extendedmode = 0;
283 parm->parm.capture.readbuffers = 0;
288 static int uvc_v4l2_set_streamparm(struct uvc_video_device *video,
289 struct v4l2_streamparm *parm)
291 struct uvc_frame *frame = video->streaming->cur_frame;
292 struct uvc_streaming_control probe;
296 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
299 if (uvc_queue_streaming(&video->queue))
302 memcpy(&probe, &video->streaming->ctrl, sizeof probe);
303 interval = uvc_fraction_to_interval(
304 parm->parm.capture.timeperframe.numerator,
305 parm->parm.capture.timeperframe.denominator);
307 uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
308 parm->parm.capture.timeperframe.numerator,
309 parm->parm.capture.timeperframe.denominator,
311 probe.dwFrameInterval = uvc_try_frame_interval(frame, interval);
313 /* Probe the device with the new settings. */
314 if ((ret = uvc_probe_video(video, &probe)) < 0)
317 /* Commit the new settings. */
318 if ((ret = uvc_set_video_ctrl(video, &probe, 0)) < 0)
321 memcpy(&video->streaming->ctrl, &probe, sizeof probe);
323 /* Return the actual frame period. */
324 parm->parm.capture.timeperframe.numerator = probe.dwFrameInterval;
325 parm->parm.capture.timeperframe.denominator = 10000000;
326 uvc_simplify_fraction(&parm->parm.capture.timeperframe.numerator,
327 &parm->parm.capture.timeperframe.denominator,
333 /* ------------------------------------------------------------------------
334 * Privilege management
338 * Privilege management is the multiple-open implementation basis. The current
339 * implementation is completely transparent for the end-user and doesn't
340 * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls.
341 * Those ioctls enable finer control on the device (by making possible for a
342 * user to request exclusive access to a device), but are not mature yet.
343 * Switching to the V4L2 priority mechanism might be considered in the future
344 * if this situation changes.
346 * Each open instance of a UVC device can either be in a privileged or
347 * unprivileged state. Only a single instance can be in a privileged state at
348 * a given time. Trying to perform an operation which requires privileges will
349 * automatically acquire the required privileges if possible, or return -EBUSY
350 * otherwise. Privileges are dismissed when closing the instance.
352 * Operations which require privileges are:
360 static int uvc_acquire_privileges(struct uvc_fh *handle)
364 /* Always succeed if the handle is already privileged. */
365 if (handle->state == UVC_HANDLE_ACTIVE)
368 /* Check if the device already has a privileged handle. */
369 mutex_lock(&uvc_driver.open_mutex);
370 if (atomic_inc_return(&handle->device->active) != 1) {
371 atomic_dec(&handle->device->active);
376 handle->state = UVC_HANDLE_ACTIVE;
379 mutex_unlock(&uvc_driver.open_mutex);
383 static void uvc_dismiss_privileges(struct uvc_fh *handle)
385 if (handle->state == UVC_HANDLE_ACTIVE)
386 atomic_dec(&handle->device->active);
388 handle->state = UVC_HANDLE_PASSIVE;
391 static int uvc_has_privileges(struct uvc_fh *handle)
393 return handle->state == UVC_HANDLE_ACTIVE;
396 /* ------------------------------------------------------------------------
397 * V4L2 file operations
400 static int uvc_v4l2_open(struct inode *inode, struct file *file)
402 struct video_device *vdev;
403 struct uvc_video_device *video;
404 struct uvc_fh *handle;
407 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
408 mutex_lock(&uvc_driver.open_mutex);
409 vdev = video_devdata(file);
410 video = video_get_drvdata(vdev);
412 if (video->dev->state & UVC_DEV_DISCONNECTED) {
417 ret = usb_autopm_get_interface(video->dev->intf);
421 /* Create the device handle. */
422 handle = kzalloc(sizeof *handle, GFP_KERNEL);
423 if (handle == NULL) {
424 usb_autopm_put_interface(video->dev->intf);
429 handle->device = video;
430 handle->state = UVC_HANDLE_PASSIVE;
431 file->private_data = handle;
433 kref_get(&video->dev->kref);
436 mutex_unlock(&uvc_driver.open_mutex);
440 static int uvc_v4l2_release(struct inode *inode, struct file *file)
442 struct video_device *vdev = video_devdata(file);
443 struct uvc_video_device *video = video_get_drvdata(vdev);
444 struct uvc_fh *handle = (struct uvc_fh *)file->private_data;
446 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");
448 /* Only free resources if this is a privileged handle. */
449 if (uvc_has_privileges(handle)) {
450 uvc_video_enable(video, 0);
452 mutex_lock(&video->queue.mutex);
453 if (uvc_free_buffers(&video->queue) < 0)
454 uvc_printk(KERN_ERR, "uvc_v4l2_release: Unable to "
456 mutex_unlock(&video->queue.mutex);
459 /* Release the file handle. */
460 uvc_dismiss_privileges(handle);
462 file->private_data = NULL;
464 usb_autopm_put_interface(video->dev->intf);
465 kref_put(&video->dev->kref, uvc_delete);
469 static int uvc_v4l2_do_ioctl(struct inode *inode, struct file *file,
470 unsigned int cmd, void *arg)
472 struct video_device *vdev = video_devdata(file);
473 struct uvc_video_device *video = video_get_drvdata(vdev);
474 struct uvc_fh *handle = (struct uvc_fh *)file->private_data;
477 if (uvc_trace_param & UVC_TRACE_IOCTL)
478 v4l_printk_ioctl(cmd);
481 /* Query capabilities */
482 case VIDIOC_QUERYCAP:
484 struct v4l2_capability *cap = arg;
486 memset(cap, 0, sizeof *cap);
487 strncpy(cap->driver, "uvcvideo", sizeof cap->driver);
488 strncpy(cap->card, vdev->name, 32);
489 strncpy(cap->bus_info, video->dev->udev->bus->bus_name,
490 sizeof cap->bus_info);
491 cap->version = DRIVER_VERSION_NUMBER;
492 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
493 | V4L2_CAP_STREAMING;
497 /* Get, Set & Query control */
498 case VIDIOC_QUERYCTRL:
499 return uvc_query_v4l2_ctrl(video, arg);
503 struct v4l2_control *ctrl = arg;
504 struct v4l2_ext_control xctrl;
506 memset(&xctrl, 0, sizeof xctrl);
509 uvc_ctrl_begin(video);
510 ret = uvc_ctrl_get(video, &xctrl);
511 uvc_ctrl_rollback(video);
513 ctrl->value = xctrl.value;
519 struct v4l2_control *ctrl = arg;
520 struct v4l2_ext_control xctrl;
522 memset(&xctrl, 0, sizeof xctrl);
524 xctrl.value = ctrl->value;
526 uvc_ctrl_begin(video);
527 ret = uvc_ctrl_set(video, &xctrl);
529 uvc_ctrl_rollback(video);
532 ret = uvc_ctrl_commit(video);
536 case VIDIOC_QUERYMENU:
537 return uvc_v4l2_query_menu(video, arg);
539 case VIDIOC_G_EXT_CTRLS:
541 struct v4l2_ext_controls *ctrls = arg;
542 struct v4l2_ext_control *ctrl = ctrls->controls;
545 uvc_ctrl_begin(video);
546 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
547 ret = uvc_ctrl_get(video, ctrl);
549 uvc_ctrl_rollback(video);
550 ctrls->error_idx = i;
554 ctrls->error_idx = 0;
555 ret = uvc_ctrl_rollback(video);
559 case VIDIOC_S_EXT_CTRLS:
560 case VIDIOC_TRY_EXT_CTRLS:
562 struct v4l2_ext_controls *ctrls = arg;
563 struct v4l2_ext_control *ctrl = ctrls->controls;
566 ret = uvc_ctrl_begin(video);
570 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
571 ret = uvc_ctrl_set(video, ctrl);
573 uvc_ctrl_rollback(video);
574 ctrls->error_idx = i;
579 ctrls->error_idx = 0;
581 if (cmd == VIDIOC_S_EXT_CTRLS)
582 ret = uvc_ctrl_commit(video);
584 ret = uvc_ctrl_rollback(video);
588 /* Get, Set & Enum input */
589 case VIDIOC_ENUMINPUT:
591 const struct uvc_entity *selector = video->selector;
592 struct v4l2_input *input = arg;
593 struct uvc_entity *iterm = NULL;
594 u32 index = input->index;
597 if (selector == NULL ||
598 (video->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
601 iterm = list_first_entry(&video->iterms,
602 struct uvc_entity, chain);
604 } else if (pin < selector->selector.bNrInPins) {
605 pin = selector->selector.baSourceID[index];
606 list_for_each_entry(iterm, video->iterms.next, chain) {
607 if (iterm->id == pin)
612 if (iterm == NULL || iterm->id != pin)
615 memset(input, 0, sizeof *input);
616 input->index = index;
617 strncpy(input->name, iterm->name, sizeof input->name);
618 if (UVC_ENTITY_TYPE(iterm) == ITT_CAMERA)
619 input->type = V4L2_INPUT_TYPE_CAMERA;
627 if (video->selector == NULL ||
628 (video->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
633 ret = uvc_query_ctrl(video->dev, GET_CUR, video->selector->id,
634 video->dev->intfnum, SU_INPUT_SELECT_CONTROL,
639 *(int *)arg = input - 1;
645 u8 input = *(u32 *)arg + 1;
647 if ((ret = uvc_acquire_privileges(handle)) < 0)
650 if (video->selector == NULL ||
651 (video->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
657 if (input > video->selector->selector.bNrInPins)
660 return uvc_query_ctrl(video->dev, SET_CUR, video->selector->id,
661 video->dev->intfnum, SU_INPUT_SELECT_CONTROL,
665 /* Try, Get, Set & Enum format */
666 case VIDIOC_ENUM_FMT:
668 struct v4l2_fmtdesc *fmt = arg;
669 struct uvc_format *format;
671 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
672 fmt->index >= video->streaming->nformats)
675 format = &video->streaming->format[fmt->index];
677 if (format->flags & UVC_FMT_FLAG_COMPRESSED)
678 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
679 strncpy(fmt->description, format->name,
680 sizeof fmt->description);
681 fmt->description[sizeof fmt->description - 1] = 0;
682 fmt->pixelformat = format->fcc;
688 struct uvc_streaming_control probe;
690 if ((ret = uvc_acquire_privileges(handle)) < 0)
693 return uvc_v4l2_try_format(video, arg, &probe, NULL, NULL);
697 if ((ret = uvc_acquire_privileges(handle)) < 0)
700 return uvc_v4l2_set_format(video, arg);
703 return uvc_v4l2_get_format(video, arg);
705 /* Frame size enumeration */
706 case VIDIOC_ENUM_FRAMESIZES:
708 struct v4l2_frmsizeenum *fsize = arg;
709 struct uvc_format *format = NULL;
710 struct uvc_frame *frame;
713 /* Look for the given pixel format */
714 for (i = 0; i < video->streaming->nformats; i++) {
715 if (video->streaming->format[i].fcc ==
716 fsize->pixel_format) {
717 format = &video->streaming->format[i];
724 if (fsize->index >= format->nframes)
727 frame = &format->frame[fsize->index];
728 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
729 fsize->discrete.width = frame->wWidth;
730 fsize->discrete.height = frame->wHeight;
734 /* Frame interval enumeration */
735 case VIDIOC_ENUM_FRAMEINTERVALS:
737 struct v4l2_frmivalenum *fival = arg;
738 struct uvc_format *format = NULL;
739 struct uvc_frame *frame = NULL;
742 /* Look for the given pixel format and frame size */
743 for (i = 0; i < video->streaming->nformats; i++) {
744 if (video->streaming->format[i].fcc ==
745 fival->pixel_format) {
746 format = &video->streaming->format[i];
753 for (i = 0; i < format->nframes; i++) {
754 if (format->frame[i].wWidth == fival->width &&
755 format->frame[i].wHeight == fival->height) {
756 frame = &format->frame[i];
763 if (frame->bFrameIntervalType) {
764 if (fival->index >= frame->bFrameIntervalType)
767 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
768 fival->discrete.numerator =
769 frame->dwFrameInterval[fival->index];
770 fival->discrete.denominator = 10000000;
771 uvc_simplify_fraction(&fival->discrete.numerator,
772 &fival->discrete.denominator, 8, 333);
774 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
775 fival->stepwise.min.numerator =
776 frame->dwFrameInterval[0];
777 fival->stepwise.min.denominator = 10000000;
778 fival->stepwise.max.numerator =
779 frame->dwFrameInterval[1];
780 fival->stepwise.max.denominator = 10000000;
781 fival->stepwise.step.numerator =
782 frame->dwFrameInterval[2];
783 fival->stepwise.step.denominator = 10000000;
784 uvc_simplify_fraction(&fival->stepwise.min.numerator,
785 &fival->stepwise.min.denominator, 8, 333);
786 uvc_simplify_fraction(&fival->stepwise.max.numerator,
787 &fival->stepwise.max.denominator, 8, 333);
788 uvc_simplify_fraction(&fival->stepwise.step.numerator,
789 &fival->stepwise.step.denominator, 8, 333);
794 /* Get & Set streaming parameters */
796 return uvc_v4l2_get_streamparm(video, arg);
799 if ((ret = uvc_acquire_privileges(handle)) < 0)
802 return uvc_v4l2_set_streamparm(video, arg);
804 /* Cropping and scaling */
807 struct v4l2_cropcap *ccap = arg;
808 struct uvc_frame *frame = video->streaming->cur_frame;
810 if (ccap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
813 ccap->bounds.left = 0;
814 ccap->bounds.top = 0;
815 ccap->bounds.width = frame->wWidth;
816 ccap->bounds.height = frame->wHeight;
818 ccap->defrect = ccap->bounds;
820 ccap->pixelaspect.numerator = 1;
821 ccap->pixelaspect.denominator = 1;
829 /* Buffers & streaming */
832 struct v4l2_requestbuffers *rb = arg;
833 unsigned int bufsize =
834 video->streaming->ctrl.dwMaxVideoFrameSize;
836 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
837 rb->memory != V4L2_MEMORY_MMAP)
840 if ((ret = uvc_acquire_privileges(handle)) < 0)
843 ret = uvc_alloc_buffers(&video->queue, rb->count, bufsize);
847 if (!(video->streaming->cur_format->flags &
848 UVC_FMT_FLAG_COMPRESSED))
849 video->queue.flags |= UVC_QUEUE_DROP_INCOMPLETE;
856 case VIDIOC_QUERYBUF:
858 struct v4l2_buffer *buf = arg;
860 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
863 if (!uvc_has_privileges(handle))
866 return uvc_query_buffer(&video->queue, buf);
870 if (!uvc_has_privileges(handle))
873 return uvc_queue_buffer(&video->queue, arg);
876 if (!uvc_has_privileges(handle))
879 return uvc_dequeue_buffer(&video->queue, arg,
880 file->f_flags & O_NONBLOCK);
882 case VIDIOC_STREAMON:
886 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
889 if (!uvc_has_privileges(handle))
892 if ((ret = uvc_video_enable(video, 1)) < 0)
897 case VIDIOC_STREAMOFF:
901 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
904 if (!uvc_has_privileges(handle))
907 return uvc_video_enable(video, 0);
910 /* Analog video standards make no sense for digital cameras. */
912 case VIDIOC_QUERYSTD:
918 case VIDIOC_ENUMAUDIO:
919 case VIDIOC_ENUMAUDOUT:
921 case VIDIOC_ENUMOUTPUT:
922 uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd);
925 /* Dynamic controls. */
926 case UVCIOC_CTRL_ADD:
928 struct uvc_xu_control_info *xinfo = arg;
929 struct uvc_control_info *info;
931 if (!capable(CAP_SYS_ADMIN))
934 info = kmalloc(sizeof *info, GFP_KERNEL);
938 memcpy(info->entity, xinfo->entity, sizeof info->entity);
939 info->index = xinfo->index;
940 info->selector = xinfo->selector;
941 info->size = xinfo->size;
942 info->flags = xinfo->flags;
944 info->flags |= UVC_CONTROL_GET_MIN | UVC_CONTROL_GET_MAX |
945 UVC_CONTROL_GET_RES | UVC_CONTROL_GET_DEF;
947 ret = uvc_ctrl_add_info(info);
953 case UVCIOC_CTRL_MAP:
955 struct uvc_xu_control_mapping *xmap = arg;
956 struct uvc_control_mapping *map;
958 if (!capable(CAP_SYS_ADMIN))
961 map = kmalloc(sizeof *map, GFP_KERNEL);
966 memcpy(map->name, xmap->name, sizeof map->name);
967 memcpy(map->entity, xmap->entity, sizeof map->entity);
968 map->selector = xmap->selector;
969 map->size = xmap->size;
970 map->offset = xmap->offset;
971 map->v4l2_type = xmap->v4l2_type;
972 map->data_type = xmap->data_type;
974 ret = uvc_ctrl_add_mapping(map);
980 case UVCIOC_CTRL_GET:
981 return uvc_xu_ctrl_query(video, arg, 0);
983 case UVCIOC_CTRL_SET:
984 return uvc_xu_ctrl_query(video, arg, 1);
987 if ((ret = v4l_compat_translate_ioctl(inode, file, cmd, arg,
988 uvc_v4l2_do_ioctl)) == -ENOIOCTLCMD)
989 uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n",
997 static int uvc_v4l2_ioctl(struct inode *inode, struct file *file,
998 unsigned int cmd, unsigned long arg)
1000 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_ioctl\n");
1001 return video_usercopy(inode, file, cmd, arg, uvc_v4l2_do_ioctl);
1004 static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1005 size_t count, loff_t *ppos)
1007 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
1014 static void uvc_vm_open(struct vm_area_struct *vma)
1016 struct uvc_buffer *buffer = vma->vm_private_data;
1017 buffer->vma_use_count++;
1020 static void uvc_vm_close(struct vm_area_struct *vma)
1022 struct uvc_buffer *buffer = vma->vm_private_data;
1023 buffer->vma_use_count--;
1026 static struct vm_operations_struct uvc_vm_ops = {
1027 .open = uvc_vm_open,
1028 .close = uvc_vm_close,
1031 static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1033 struct video_device *vdev = video_devdata(file);
1034 struct uvc_video_device *video = video_get_drvdata(vdev);
1035 struct uvc_buffer *uninitialized_var(buffer);
1037 unsigned long addr, start, size;
1041 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
1043 start = vma->vm_start;
1044 size = vma->vm_end - vma->vm_start;
1046 mutex_lock(&video->queue.mutex);
1048 for (i = 0; i < video->queue.count; ++i) {
1049 buffer = &video->queue.buffer[i];
1050 if ((buffer->buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
1054 if (i == video->queue.count || size != video->queue.buf_size) {
1060 * VM_IO marks the area as being an mmaped region for I/O to a
1061 * device. It also prevents the region from being core dumped.
1063 vma->vm_flags |= VM_IO;
1065 addr = (unsigned long)video->queue.mem + buffer->buf.m.offset;
1067 page = vmalloc_to_page((void *)addr);
1068 if ((ret = vm_insert_page(vma, start, page)) < 0)
1076 vma->vm_ops = &uvc_vm_ops;
1077 vma->vm_private_data = buffer;
1081 mutex_unlock(&video->queue.mutex);
1085 static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
1087 struct video_device *vdev = video_devdata(file);
1088 struct uvc_video_device *video = video_get_drvdata(vdev);
1090 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
1092 return uvc_queue_poll(&video->queue, file, wait);
1095 struct file_operations uvc_fops = {
1096 .owner = THIS_MODULE,
1097 .open = uvc_v4l2_open,
1098 .release = uvc_v4l2_release,
1099 .ioctl = uvc_v4l2_ioctl,
1100 .compat_ioctl = v4l_compat_ioctl32,
1101 .llseek = no_llseek,
1102 .read = uvc_v4l2_read,
1103 .mmap = uvc_v4l2_mmap,
1104 .poll = uvc_v4l2_poll,