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>
26 #include <media/v4l2-ioctl.h>
30 /* ------------------------------------------------------------------------
35 * Mapping V4L2 controls to UVC controls can be straighforward if done well.
36 * Most of the UVC controls exist in V4L2, and can be mapped directly. Some
37 * must be grouped (for instance the Red Balance, Blue Balance and Do White
38 * Balance V4L2 controls use the White Balance Component UVC control) or
39 * otherwise translated. The approach we take here is to use a translation
40 * table for the controls which can be mapped directly, and handle the others
43 static int uvc_v4l2_query_menu(struct uvc_video_device *video,
44 struct v4l2_querymenu *query_menu)
46 struct uvc_menu_info *menu_info;
47 struct uvc_control_mapping *mapping;
48 struct uvc_control *ctrl;
50 ctrl = uvc_find_control(video, query_menu->id, &mapping);
51 if (ctrl == NULL || mapping->v4l2_type != V4L2_CTRL_TYPE_MENU)
54 if (query_menu->index >= mapping->menu_count)
57 menu_info = &mapping->menu_info[query_menu->index];
58 strncpy(query_menu->name, menu_info->name, 32);
63 * Find the frame interval closest to the requested frame interval for the
64 * given frame format and size. This should be done by the device as part of
65 * the Video Probe and Commit negotiation, but some hardware don't implement
68 static __u32 uvc_try_frame_interval(struct uvc_frame *frame, __u32 interval)
72 if (frame->bFrameIntervalType) {
73 __u32 best = -1, dist;
75 for (i = 0; i < frame->bFrameIntervalType; ++i) {
76 dist = interval > frame->dwFrameInterval[i]
77 ? interval - frame->dwFrameInterval[i]
78 : frame->dwFrameInterval[i] - interval;
86 interval = frame->dwFrameInterval[i-1];
88 const __u32 min = frame->dwFrameInterval[0];
89 const __u32 max = frame->dwFrameInterval[1];
90 const __u32 step = frame->dwFrameInterval[2];
92 interval = min + (interval - min + step/2) / step * step;
100 static int uvc_v4l2_try_format(struct uvc_video_device *video,
101 struct v4l2_format *fmt, struct uvc_streaming_control *probe,
102 struct uvc_format **uvc_format, struct uvc_frame **uvc_frame)
104 struct uvc_format *format = NULL;
105 struct uvc_frame *frame = NULL;
107 unsigned int d, maxd;
113 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
116 fcc = (__u8 *)&fmt->fmt.pix.pixelformat;
117 uvc_trace(UVC_TRACE_FORMAT, "Trying format 0x%08x (%c%c%c%c): %ux%u.\n",
118 fmt->fmt.pix.pixelformat,
119 fcc[0], fcc[1], fcc[2], fcc[3],
120 fmt->fmt.pix.width, fmt->fmt.pix.height);
122 /* Check if the hardware supports the requested format. */
123 for (i = 0; i < video->streaming->nformats; ++i) {
124 format = &video->streaming->format[i];
125 if (format->fcc == fmt->fmt.pix.pixelformat)
129 if (format == NULL || format->fcc != fmt->fmt.pix.pixelformat) {
130 uvc_trace(UVC_TRACE_FORMAT, "Unsupported format 0x%08x.\n",
131 fmt->fmt.pix.pixelformat);
135 /* Find the closest image size. The distance between image sizes is
136 * the size in pixels of the non-overlapping regions between the
137 * requested size and the frame-specified size.
139 rw = fmt->fmt.pix.width;
140 rh = fmt->fmt.pix.height;
141 maxd = (unsigned int)-1;
143 for (i = 0; i < format->nframes; ++i) {
144 __u16 w = format->frame[i].wWidth;
145 __u16 h = format->frame[i].wHeight;
147 d = min(w, rw) * min(h, rh);
148 d = w*h + rw*rh - 2*d;
151 frame = &format->frame[i];
159 uvc_trace(UVC_TRACE_FORMAT, "Unsupported size %ux%u.\n",
160 fmt->fmt.pix.width, fmt->fmt.pix.height);
164 /* Use the default frame interval. */
165 interval = frame->dwDefaultFrameInterval;
166 uvc_trace(UVC_TRACE_FORMAT, "Using default frame interval %u.%u us "
167 "(%u.%u fps).\n", interval/10, interval%10, 10000000/interval,
168 (100000000/interval)%10);
170 /* Set the format index, frame index and frame interval. */
171 memset(probe, 0, sizeof *probe);
172 probe->bmHint = 1; /* dwFrameInterval */
173 probe->bFormatIndex = format->index;
174 probe->bFrameIndex = frame->bFrameIndex;
175 probe->dwFrameInterval = uvc_try_frame_interval(frame, interval);
176 /* Some webcams stall the probe control set request when the
177 * dwMaxVideoFrameSize field is set to zero. The UVC specification
178 * clearly states that the field is read-only from the host, so this
179 * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by
180 * the webcam to work around the problem.
182 * The workaround could probably be enabled for all webcams, so the
183 * quirk can be removed if needed. It's currently useful to detect
184 * webcam bugs and fix them before they hit the market (providing
185 * developers test their webcams with the Linux driver as well as with
186 * the Windows driver).
188 if (video->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS)
189 probe->dwMaxVideoFrameSize =
190 video->streaming->ctrl.dwMaxVideoFrameSize;
192 /* Probe the device */
193 if ((ret = uvc_probe_video(video, probe)) < 0)
196 fmt->fmt.pix.width = frame->wWidth;
197 fmt->fmt.pix.height = frame->wHeight;
198 fmt->fmt.pix.field = V4L2_FIELD_NONE;
199 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
200 fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize;
201 fmt->fmt.pix.colorspace = format->colorspace;
202 fmt->fmt.pix.priv = 0;
204 if (uvc_format != NULL)
205 *uvc_format = format;
206 if (uvc_frame != NULL)
213 static int uvc_v4l2_get_format(struct uvc_video_device *video,
214 struct v4l2_format *fmt)
216 struct uvc_format *format = video->streaming->cur_format;
217 struct uvc_frame *frame = video->streaming->cur_frame;
219 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
222 if (format == NULL || frame == NULL)
225 fmt->fmt.pix.pixelformat = format->fcc;
226 fmt->fmt.pix.width = frame->wWidth;
227 fmt->fmt.pix.height = frame->wHeight;
228 fmt->fmt.pix.field = V4L2_FIELD_NONE;
229 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
230 fmt->fmt.pix.sizeimage = video->streaming->ctrl.dwMaxVideoFrameSize;
231 fmt->fmt.pix.colorspace = format->colorspace;
232 fmt->fmt.pix.priv = 0;
237 static int uvc_v4l2_set_format(struct uvc_video_device *video,
238 struct v4l2_format *fmt)
240 struct uvc_streaming_control probe;
241 struct uvc_format *format;
242 struct uvc_frame *frame;
245 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
248 if (uvc_queue_streaming(&video->queue))
251 ret = uvc_v4l2_try_format(video, fmt, &probe, &format, &frame);
255 if ((ret = uvc_set_video_ctrl(video, &probe, 0)) < 0)
258 memcpy(&video->streaming->ctrl, &probe, sizeof probe);
259 video->streaming->cur_format = format;
260 video->streaming->cur_frame = frame;
265 static int uvc_v4l2_get_streamparm(struct uvc_video_device *video,
266 struct v4l2_streamparm *parm)
268 uint32_t numerator, denominator;
270 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
273 numerator = video->streaming->ctrl.dwFrameInterval;
274 denominator = 10000000;
275 uvc_simplify_fraction(&numerator, &denominator, 8, 333);
277 memset(parm, 0, sizeof *parm);
278 parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
279 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
280 parm->parm.capture.capturemode = 0;
281 parm->parm.capture.timeperframe.numerator = numerator;
282 parm->parm.capture.timeperframe.denominator = denominator;
283 parm->parm.capture.extendedmode = 0;
284 parm->parm.capture.readbuffers = 0;
289 static int uvc_v4l2_set_streamparm(struct uvc_video_device *video,
290 struct v4l2_streamparm *parm)
292 struct uvc_frame *frame = video->streaming->cur_frame;
293 struct uvc_streaming_control probe;
297 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
300 if (uvc_queue_streaming(&video->queue))
303 memcpy(&probe, &video->streaming->ctrl, sizeof probe);
304 interval = uvc_fraction_to_interval(
305 parm->parm.capture.timeperframe.numerator,
306 parm->parm.capture.timeperframe.denominator);
308 uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
309 parm->parm.capture.timeperframe.numerator,
310 parm->parm.capture.timeperframe.denominator,
312 probe.dwFrameInterval = uvc_try_frame_interval(frame, interval);
314 /* Probe the device with the new settings. */
315 if ((ret = uvc_probe_video(video, &probe)) < 0)
318 /* Commit the new settings. */
319 if ((ret = uvc_set_video_ctrl(video, &probe, 0)) < 0)
322 memcpy(&video->streaming->ctrl, &probe, sizeof probe);
324 /* Return the actual frame period. */
325 parm->parm.capture.timeperframe.numerator = probe.dwFrameInterval;
326 parm->parm.capture.timeperframe.denominator = 10000000;
327 uvc_simplify_fraction(&parm->parm.capture.timeperframe.numerator,
328 &parm->parm.capture.timeperframe.denominator,
334 /* ------------------------------------------------------------------------
335 * Privilege management
339 * Privilege management is the multiple-open implementation basis. The current
340 * implementation is completely transparent for the end-user and doesn't
341 * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls.
342 * Those ioctls enable finer control on the device (by making possible for a
343 * user to request exclusive access to a device), but are not mature yet.
344 * Switching to the V4L2 priority mechanism might be considered in the future
345 * if this situation changes.
347 * Each open instance of a UVC device can either be in a privileged or
348 * unprivileged state. Only a single instance can be in a privileged state at
349 * a given time. Trying to perform an operation which requires privileges will
350 * automatically acquire the required privileges if possible, or return -EBUSY
351 * otherwise. Privileges are dismissed when closing the instance.
353 * Operations which require privileges are:
361 static int uvc_acquire_privileges(struct uvc_fh *handle)
365 /* Always succeed if the handle is already privileged. */
366 if (handle->state == UVC_HANDLE_ACTIVE)
369 /* Check if the device already has a privileged handle. */
370 mutex_lock(&uvc_driver.open_mutex);
371 if (atomic_inc_return(&handle->device->active) != 1) {
372 atomic_dec(&handle->device->active);
377 handle->state = UVC_HANDLE_ACTIVE;
380 mutex_unlock(&uvc_driver.open_mutex);
384 static void uvc_dismiss_privileges(struct uvc_fh *handle)
386 if (handle->state == UVC_HANDLE_ACTIVE)
387 atomic_dec(&handle->device->active);
389 handle->state = UVC_HANDLE_PASSIVE;
392 static int uvc_has_privileges(struct uvc_fh *handle)
394 return handle->state == UVC_HANDLE_ACTIVE;
397 /* ------------------------------------------------------------------------
398 * V4L2 file operations
401 static int uvc_v4l2_open(struct inode *inode, struct file *file)
403 struct video_device *vdev;
404 struct uvc_video_device *video;
405 struct uvc_fh *handle;
408 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
409 mutex_lock(&uvc_driver.open_mutex);
410 vdev = video_devdata(file);
411 video = video_get_drvdata(vdev);
413 if (video->dev->state & UVC_DEV_DISCONNECTED) {
418 ret = usb_autopm_get_interface(video->dev->intf);
422 /* Create the device handle. */
423 handle = kzalloc(sizeof *handle, GFP_KERNEL);
424 if (handle == NULL) {
425 usb_autopm_put_interface(video->dev->intf);
430 handle->device = video;
431 handle->state = UVC_HANDLE_PASSIVE;
432 file->private_data = handle;
434 kref_get(&video->dev->kref);
437 mutex_unlock(&uvc_driver.open_mutex);
441 static int uvc_v4l2_release(struct inode *inode, struct file *file)
443 struct video_device *vdev = video_devdata(file);
444 struct uvc_video_device *video = video_get_drvdata(vdev);
445 struct uvc_fh *handle = (struct uvc_fh *)file->private_data;
447 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");
449 /* Only free resources if this is a privileged handle. */
450 if (uvc_has_privileges(handle)) {
451 uvc_video_enable(video, 0);
453 mutex_lock(&video->queue.mutex);
454 if (uvc_free_buffers(&video->queue) < 0)
455 uvc_printk(KERN_ERR, "uvc_v4l2_release: Unable to "
457 mutex_unlock(&video->queue.mutex);
460 /* Release the file handle. */
461 uvc_dismiss_privileges(handle);
463 file->private_data = NULL;
465 usb_autopm_put_interface(video->dev->intf);
466 kref_put(&video->dev->kref, uvc_delete);
470 static int uvc_v4l2_do_ioctl(struct inode *inode, struct file *file,
471 unsigned int cmd, void *arg)
473 struct video_device *vdev = video_devdata(file);
474 struct uvc_video_device *video = video_get_drvdata(vdev);
475 struct uvc_fh *handle = (struct uvc_fh *)file->private_data;
478 if (uvc_trace_param & UVC_TRACE_IOCTL)
479 v4l_printk_ioctl(cmd);
482 /* Query capabilities */
483 case VIDIOC_QUERYCAP:
485 struct v4l2_capability *cap = arg;
487 memset(cap, 0, sizeof *cap);
488 strncpy(cap->driver, "uvcvideo", sizeof cap->driver);
489 strncpy(cap->card, vdev->name, 32);
490 strncpy(cap->bus_info, video->dev->udev->bus->bus_name,
491 sizeof cap->bus_info);
492 cap->version = DRIVER_VERSION_NUMBER;
493 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
494 | V4L2_CAP_STREAMING;
498 /* Get, Set & Query control */
499 case VIDIOC_QUERYCTRL:
500 return uvc_query_v4l2_ctrl(video, arg);
504 struct v4l2_control *ctrl = arg;
505 struct v4l2_ext_control xctrl;
507 memset(&xctrl, 0, sizeof xctrl);
510 uvc_ctrl_begin(video);
511 ret = uvc_ctrl_get(video, &xctrl);
512 uvc_ctrl_rollback(video);
514 ctrl->value = xctrl.value;
520 struct v4l2_control *ctrl = arg;
521 struct v4l2_ext_control xctrl;
523 memset(&xctrl, 0, sizeof xctrl);
525 xctrl.value = ctrl->value;
527 uvc_ctrl_begin(video);
528 ret = uvc_ctrl_set(video, &xctrl);
530 uvc_ctrl_rollback(video);
533 ret = uvc_ctrl_commit(video);
537 case VIDIOC_QUERYMENU:
538 return uvc_v4l2_query_menu(video, arg);
540 case VIDIOC_G_EXT_CTRLS:
542 struct v4l2_ext_controls *ctrls = arg;
543 struct v4l2_ext_control *ctrl = ctrls->controls;
546 uvc_ctrl_begin(video);
547 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
548 ret = uvc_ctrl_get(video, ctrl);
550 uvc_ctrl_rollback(video);
551 ctrls->error_idx = i;
555 ctrls->error_idx = 0;
556 ret = uvc_ctrl_rollback(video);
560 case VIDIOC_S_EXT_CTRLS:
561 case VIDIOC_TRY_EXT_CTRLS:
563 struct v4l2_ext_controls *ctrls = arg;
564 struct v4l2_ext_control *ctrl = ctrls->controls;
567 ret = uvc_ctrl_begin(video);
571 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
572 ret = uvc_ctrl_set(video, ctrl);
574 uvc_ctrl_rollback(video);
575 ctrls->error_idx = i;
580 ctrls->error_idx = 0;
582 if (cmd == VIDIOC_S_EXT_CTRLS)
583 ret = uvc_ctrl_commit(video);
585 ret = uvc_ctrl_rollback(video);
589 /* Get, Set & Enum input */
590 case VIDIOC_ENUMINPUT:
592 const struct uvc_entity *selector = video->selector;
593 struct v4l2_input *input = arg;
594 struct uvc_entity *iterm = NULL;
595 u32 index = input->index;
598 if (selector == NULL ||
599 (video->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
602 iterm = list_first_entry(&video->iterms,
603 struct uvc_entity, chain);
605 } else if (pin < selector->selector.bNrInPins) {
606 pin = selector->selector.baSourceID[index];
607 list_for_each_entry(iterm, video->iterms.next, chain) {
608 if (iterm->id == pin)
613 if (iterm == NULL || iterm->id != pin)
616 memset(input, 0, sizeof *input);
617 input->index = index;
618 strncpy(input->name, iterm->name, sizeof input->name);
619 if (UVC_ENTITY_TYPE(iterm) == ITT_CAMERA)
620 input->type = V4L2_INPUT_TYPE_CAMERA;
628 if (video->selector == NULL ||
629 (video->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
634 ret = uvc_query_ctrl(video->dev, GET_CUR, video->selector->id,
635 video->dev->intfnum, SU_INPUT_SELECT_CONTROL,
640 *(int *)arg = input - 1;
646 u8 input = *(u32 *)arg + 1;
648 if ((ret = uvc_acquire_privileges(handle)) < 0)
651 if (video->selector == NULL ||
652 (video->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
658 if (input > video->selector->selector.bNrInPins)
661 return uvc_query_ctrl(video->dev, SET_CUR, video->selector->id,
662 video->dev->intfnum, SU_INPUT_SELECT_CONTROL,
666 /* Try, Get, Set & Enum format */
667 case VIDIOC_ENUM_FMT:
669 struct v4l2_fmtdesc *fmt = arg;
670 struct uvc_format *format;
672 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
673 fmt->index >= video->streaming->nformats)
676 format = &video->streaming->format[fmt->index];
678 if (format->flags & UVC_FMT_FLAG_COMPRESSED)
679 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
680 strncpy(fmt->description, format->name,
681 sizeof fmt->description);
682 fmt->description[sizeof fmt->description - 1] = 0;
683 fmt->pixelformat = format->fcc;
689 struct uvc_streaming_control probe;
691 if ((ret = uvc_acquire_privileges(handle)) < 0)
694 return uvc_v4l2_try_format(video, arg, &probe, NULL, NULL);
698 if ((ret = uvc_acquire_privileges(handle)) < 0)
701 return uvc_v4l2_set_format(video, arg);
704 return uvc_v4l2_get_format(video, arg);
706 /* Frame size enumeration */
707 case VIDIOC_ENUM_FRAMESIZES:
709 struct v4l2_frmsizeenum *fsize = arg;
710 struct uvc_format *format = NULL;
711 struct uvc_frame *frame;
714 /* Look for the given pixel format */
715 for (i = 0; i < video->streaming->nformats; i++) {
716 if (video->streaming->format[i].fcc ==
717 fsize->pixel_format) {
718 format = &video->streaming->format[i];
725 if (fsize->index >= format->nframes)
728 frame = &format->frame[fsize->index];
729 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
730 fsize->discrete.width = frame->wWidth;
731 fsize->discrete.height = frame->wHeight;
735 /* Frame interval enumeration */
736 case VIDIOC_ENUM_FRAMEINTERVALS:
738 struct v4l2_frmivalenum *fival = arg;
739 struct uvc_format *format = NULL;
740 struct uvc_frame *frame = NULL;
743 /* Look for the given pixel format and frame size */
744 for (i = 0; i < video->streaming->nformats; i++) {
745 if (video->streaming->format[i].fcc ==
746 fival->pixel_format) {
747 format = &video->streaming->format[i];
754 for (i = 0; i < format->nframes; i++) {
755 if (format->frame[i].wWidth == fival->width &&
756 format->frame[i].wHeight == fival->height) {
757 frame = &format->frame[i];
764 if (frame->bFrameIntervalType) {
765 if (fival->index >= frame->bFrameIntervalType)
768 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
769 fival->discrete.numerator =
770 frame->dwFrameInterval[fival->index];
771 fival->discrete.denominator = 10000000;
772 uvc_simplify_fraction(&fival->discrete.numerator,
773 &fival->discrete.denominator, 8, 333);
775 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
776 fival->stepwise.min.numerator =
777 frame->dwFrameInterval[0];
778 fival->stepwise.min.denominator = 10000000;
779 fival->stepwise.max.numerator =
780 frame->dwFrameInterval[1];
781 fival->stepwise.max.denominator = 10000000;
782 fival->stepwise.step.numerator =
783 frame->dwFrameInterval[2];
784 fival->stepwise.step.denominator = 10000000;
785 uvc_simplify_fraction(&fival->stepwise.min.numerator,
786 &fival->stepwise.min.denominator, 8, 333);
787 uvc_simplify_fraction(&fival->stepwise.max.numerator,
788 &fival->stepwise.max.denominator, 8, 333);
789 uvc_simplify_fraction(&fival->stepwise.step.numerator,
790 &fival->stepwise.step.denominator, 8, 333);
795 /* Get & Set streaming parameters */
797 return uvc_v4l2_get_streamparm(video, arg);
800 if ((ret = uvc_acquire_privileges(handle)) < 0)
803 return uvc_v4l2_set_streamparm(video, arg);
805 /* Cropping and scaling */
808 struct v4l2_cropcap *ccap = arg;
809 struct uvc_frame *frame = video->streaming->cur_frame;
811 if (ccap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
814 ccap->bounds.left = 0;
815 ccap->bounds.top = 0;
816 ccap->bounds.width = frame->wWidth;
817 ccap->bounds.height = frame->wHeight;
819 ccap->defrect = ccap->bounds;
821 ccap->pixelaspect.numerator = 1;
822 ccap->pixelaspect.denominator = 1;
830 /* Buffers & streaming */
833 struct v4l2_requestbuffers *rb = arg;
834 unsigned int bufsize =
835 video->streaming->ctrl.dwMaxVideoFrameSize;
837 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
838 rb->memory != V4L2_MEMORY_MMAP)
841 if ((ret = uvc_acquire_privileges(handle)) < 0)
844 ret = uvc_alloc_buffers(&video->queue, rb->count, bufsize);
848 if (!(video->streaming->cur_format->flags &
849 UVC_FMT_FLAG_COMPRESSED))
850 video->queue.flags |= UVC_QUEUE_DROP_INCOMPLETE;
857 case VIDIOC_QUERYBUF:
859 struct v4l2_buffer *buf = arg;
861 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
864 if (!uvc_has_privileges(handle))
867 return uvc_query_buffer(&video->queue, buf);
871 if (!uvc_has_privileges(handle))
874 return uvc_queue_buffer(&video->queue, arg);
877 if (!uvc_has_privileges(handle))
880 return uvc_dequeue_buffer(&video->queue, arg,
881 file->f_flags & O_NONBLOCK);
883 case VIDIOC_STREAMON:
887 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
890 if (!uvc_has_privileges(handle))
893 if ((ret = uvc_video_enable(video, 1)) < 0)
898 case VIDIOC_STREAMOFF:
902 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
905 if (!uvc_has_privileges(handle))
908 return uvc_video_enable(video, 0);
911 /* Analog video standards make no sense for digital cameras. */
913 case VIDIOC_QUERYSTD:
919 case VIDIOC_ENUMAUDIO:
920 case VIDIOC_ENUMAUDOUT:
922 case VIDIOC_ENUMOUTPUT:
923 uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd);
926 /* Dynamic controls. */
927 case UVCIOC_CTRL_ADD:
929 struct uvc_xu_control_info *xinfo = arg;
930 struct uvc_control_info *info;
932 if (!capable(CAP_SYS_ADMIN))
935 info = kmalloc(sizeof *info, GFP_KERNEL);
939 memcpy(info->entity, xinfo->entity, sizeof info->entity);
940 info->index = xinfo->index;
941 info->selector = xinfo->selector;
942 info->size = xinfo->size;
943 info->flags = xinfo->flags;
945 info->flags |= UVC_CONTROL_GET_MIN | UVC_CONTROL_GET_MAX |
946 UVC_CONTROL_GET_RES | UVC_CONTROL_GET_DEF;
948 ret = uvc_ctrl_add_info(info);
954 case UVCIOC_CTRL_MAP:
956 struct uvc_xu_control_mapping *xmap = arg;
957 struct uvc_control_mapping *map;
959 if (!capable(CAP_SYS_ADMIN))
962 map = kmalloc(sizeof *map, GFP_KERNEL);
967 memcpy(map->name, xmap->name, sizeof map->name);
968 memcpy(map->entity, xmap->entity, sizeof map->entity);
969 map->selector = xmap->selector;
970 map->size = xmap->size;
971 map->offset = xmap->offset;
972 map->v4l2_type = xmap->v4l2_type;
973 map->data_type = xmap->data_type;
975 ret = uvc_ctrl_add_mapping(map);
981 case UVCIOC_CTRL_GET:
982 return uvc_xu_ctrl_query(video, arg, 0);
984 case UVCIOC_CTRL_SET:
985 return uvc_xu_ctrl_query(video, arg, 1);
988 if ((ret = v4l_compat_translate_ioctl(inode, file, cmd, arg,
989 uvc_v4l2_do_ioctl)) == -ENOIOCTLCMD)
990 uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n",
998 static int uvc_v4l2_ioctl(struct inode *inode, struct file *file,
999 unsigned int cmd, unsigned long arg)
1001 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_ioctl\n");
1002 return video_usercopy(inode, file, cmd, arg, uvc_v4l2_do_ioctl);
1005 static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1006 size_t count, loff_t *ppos)
1008 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
1015 static void uvc_vm_open(struct vm_area_struct *vma)
1017 struct uvc_buffer *buffer = vma->vm_private_data;
1018 buffer->vma_use_count++;
1021 static void uvc_vm_close(struct vm_area_struct *vma)
1023 struct uvc_buffer *buffer = vma->vm_private_data;
1024 buffer->vma_use_count--;
1027 static struct vm_operations_struct uvc_vm_ops = {
1028 .open = uvc_vm_open,
1029 .close = uvc_vm_close,
1032 static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1034 struct video_device *vdev = video_devdata(file);
1035 struct uvc_video_device *video = video_get_drvdata(vdev);
1036 struct uvc_buffer *uninitialized_var(buffer);
1038 unsigned long addr, start, size;
1042 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
1044 start = vma->vm_start;
1045 size = vma->vm_end - vma->vm_start;
1047 mutex_lock(&video->queue.mutex);
1049 for (i = 0; i < video->queue.count; ++i) {
1050 buffer = &video->queue.buffer[i];
1051 if ((buffer->buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
1055 if (i == video->queue.count || size != video->queue.buf_size) {
1061 * VM_IO marks the area as being an mmaped region for I/O to a
1062 * device. It also prevents the region from being core dumped.
1064 vma->vm_flags |= VM_IO;
1066 addr = (unsigned long)video->queue.mem + buffer->buf.m.offset;
1068 page = vmalloc_to_page((void *)addr);
1069 if ((ret = vm_insert_page(vma, start, page)) < 0)
1077 vma->vm_ops = &uvc_vm_ops;
1078 vma->vm_private_data = buffer;
1082 mutex_unlock(&video->queue.mutex);
1086 static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
1088 struct video_device *vdev = video_devdata(file);
1089 struct uvc_video_device *video = video_get_drvdata(vdev);
1091 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
1093 return uvc_queue_poll(&video->queue, file, wait);
1096 struct file_operations uvc_fops = {
1097 .owner = THIS_MODULE,
1098 .open = uvc_v4l2_open,
1099 .release = uvc_v4l2_release,
1100 .ioctl = uvc_v4l2_ioctl,
1101 .compat_ioctl = v4l_compat_ioctl32,
1102 .llseek = no_llseek,
1103 .read = uvc_v4l2_read,
1104 .mmap = uvc_v4l2_mmap,
1105 .poll = uvc_v4l2_poll,