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 memcpy(&video->streaming->ctrl, &probe, sizeof probe);
256 video->streaming->cur_format = format;
257 video->streaming->cur_frame = frame;
262 static int uvc_v4l2_get_streamparm(struct uvc_video_device *video,
263 struct v4l2_streamparm *parm)
265 uint32_t numerator, denominator;
267 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
270 numerator = video->streaming->ctrl.dwFrameInterval;
271 denominator = 10000000;
272 uvc_simplify_fraction(&numerator, &denominator, 8, 333);
274 memset(parm, 0, sizeof *parm);
275 parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
276 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
277 parm->parm.capture.capturemode = 0;
278 parm->parm.capture.timeperframe.numerator = numerator;
279 parm->parm.capture.timeperframe.denominator = denominator;
280 parm->parm.capture.extendedmode = 0;
281 parm->parm.capture.readbuffers = 0;
286 static int uvc_v4l2_set_streamparm(struct uvc_video_device *video,
287 struct v4l2_streamparm *parm)
289 struct uvc_frame *frame = video->streaming->cur_frame;
290 struct uvc_streaming_control probe;
294 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
297 if (uvc_queue_streaming(&video->queue))
300 memcpy(&probe, &video->streaming->ctrl, sizeof probe);
301 interval = uvc_fraction_to_interval(
302 parm->parm.capture.timeperframe.numerator,
303 parm->parm.capture.timeperframe.denominator);
305 uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
306 parm->parm.capture.timeperframe.numerator,
307 parm->parm.capture.timeperframe.denominator,
309 probe.dwFrameInterval = uvc_try_frame_interval(frame, interval);
311 /* Probe the device with the new settings. */
312 if ((ret = uvc_probe_video(video, &probe)) < 0)
315 memcpy(&video->streaming->ctrl, &probe, sizeof probe);
317 /* Return the actual frame period. */
318 parm->parm.capture.timeperframe.numerator = probe.dwFrameInterval;
319 parm->parm.capture.timeperframe.denominator = 10000000;
320 uvc_simplify_fraction(&parm->parm.capture.timeperframe.numerator,
321 &parm->parm.capture.timeperframe.denominator,
327 /* ------------------------------------------------------------------------
328 * Privilege management
332 * Privilege management is the multiple-open implementation basis. The current
333 * implementation is completely transparent for the end-user and doesn't
334 * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls.
335 * Those ioctls enable finer control on the device (by making possible for a
336 * user to request exclusive access to a device), but are not mature yet.
337 * Switching to the V4L2 priority mechanism might be considered in the future
338 * if this situation changes.
340 * Each open instance of a UVC device can either be in a privileged or
341 * unprivileged state. Only a single instance can be in a privileged state at
342 * a given time. Trying to perform an operation which requires privileges will
343 * automatically acquire the required privileges if possible, or return -EBUSY
344 * otherwise. Privileges are dismissed when closing the instance.
346 * Operations which require privileges are:
354 static int uvc_acquire_privileges(struct uvc_fh *handle)
358 /* Always succeed if the handle is already privileged. */
359 if (handle->state == UVC_HANDLE_ACTIVE)
362 /* Check if the device already has a privileged handle. */
363 mutex_lock(&uvc_driver.open_mutex);
364 if (atomic_inc_return(&handle->device->active) != 1) {
365 atomic_dec(&handle->device->active);
370 handle->state = UVC_HANDLE_ACTIVE;
373 mutex_unlock(&uvc_driver.open_mutex);
377 static void uvc_dismiss_privileges(struct uvc_fh *handle)
379 if (handle->state == UVC_HANDLE_ACTIVE)
380 atomic_dec(&handle->device->active);
382 handle->state = UVC_HANDLE_PASSIVE;
385 static int uvc_has_privileges(struct uvc_fh *handle)
387 return handle->state == UVC_HANDLE_ACTIVE;
390 /* ------------------------------------------------------------------------
391 * V4L2 file operations
394 static int uvc_v4l2_open(struct inode *inode, struct file *file)
396 struct uvc_video_device *video;
397 struct uvc_fh *handle;
400 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
401 mutex_lock(&uvc_driver.open_mutex);
402 video = video_drvdata(file);
404 if (video->dev->state & UVC_DEV_DISCONNECTED) {
409 ret = usb_autopm_get_interface(video->dev->intf);
413 /* Create the device handle. */
414 handle = kzalloc(sizeof *handle, GFP_KERNEL);
415 if (handle == NULL) {
416 usb_autopm_put_interface(video->dev->intf);
421 handle->device = video;
422 handle->state = UVC_HANDLE_PASSIVE;
423 file->private_data = handle;
425 kref_get(&video->dev->kref);
428 mutex_unlock(&uvc_driver.open_mutex);
432 static int uvc_v4l2_release(struct inode *inode, struct file *file)
434 struct uvc_video_device *video = video_drvdata(file);
435 struct uvc_fh *handle = (struct uvc_fh *)file->private_data;
437 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");
439 /* Only free resources if this is a privileged handle. */
440 if (uvc_has_privileges(handle)) {
441 uvc_video_enable(video, 0);
443 mutex_lock(&video->queue.mutex);
444 if (uvc_free_buffers(&video->queue) < 0)
445 uvc_printk(KERN_ERR, "uvc_v4l2_release: Unable to "
447 mutex_unlock(&video->queue.mutex);
450 /* Release the file handle. */
451 uvc_dismiss_privileges(handle);
453 file->private_data = NULL;
455 usb_autopm_put_interface(video->dev->intf);
456 kref_put(&video->dev->kref, uvc_delete);
460 static int uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
462 struct video_device *vdev = video_devdata(file);
463 struct uvc_video_device *video = video_get_drvdata(vdev);
464 struct uvc_fh *handle = (struct uvc_fh *)file->private_data;
467 if (uvc_trace_param & UVC_TRACE_IOCTL)
468 v4l_printk_ioctl(cmd);
471 /* Query capabilities */
472 case VIDIOC_QUERYCAP:
474 struct v4l2_capability *cap = arg;
476 memset(cap, 0, sizeof *cap);
477 strncpy(cap->driver, "uvcvideo", sizeof cap->driver);
478 strncpy(cap->card, vdev->name, 32);
479 strncpy(cap->bus_info, video->dev->udev->bus->bus_name,
480 sizeof cap->bus_info);
481 cap->version = DRIVER_VERSION_NUMBER;
482 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
483 | V4L2_CAP_STREAMING;
487 /* Get, Set & Query control */
488 case VIDIOC_QUERYCTRL:
489 return uvc_query_v4l2_ctrl(video, arg);
493 struct v4l2_control *ctrl = arg;
494 struct v4l2_ext_control xctrl;
496 memset(&xctrl, 0, sizeof xctrl);
499 uvc_ctrl_begin(video);
500 ret = uvc_ctrl_get(video, &xctrl);
501 uvc_ctrl_rollback(video);
503 ctrl->value = xctrl.value;
509 struct v4l2_control *ctrl = arg;
510 struct v4l2_ext_control xctrl;
512 memset(&xctrl, 0, sizeof xctrl);
514 xctrl.value = ctrl->value;
516 uvc_ctrl_begin(video);
517 ret = uvc_ctrl_set(video, &xctrl);
519 uvc_ctrl_rollback(video);
522 ret = uvc_ctrl_commit(video);
526 case VIDIOC_QUERYMENU:
527 return uvc_v4l2_query_menu(video, arg);
529 case VIDIOC_G_EXT_CTRLS:
531 struct v4l2_ext_controls *ctrls = arg;
532 struct v4l2_ext_control *ctrl = ctrls->controls;
535 uvc_ctrl_begin(video);
536 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
537 ret = uvc_ctrl_get(video, ctrl);
539 uvc_ctrl_rollback(video);
540 ctrls->error_idx = i;
544 ctrls->error_idx = 0;
545 ret = uvc_ctrl_rollback(video);
549 case VIDIOC_S_EXT_CTRLS:
550 case VIDIOC_TRY_EXT_CTRLS:
552 struct v4l2_ext_controls *ctrls = arg;
553 struct v4l2_ext_control *ctrl = ctrls->controls;
556 ret = uvc_ctrl_begin(video);
560 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
561 ret = uvc_ctrl_set(video, ctrl);
563 uvc_ctrl_rollback(video);
564 ctrls->error_idx = i;
569 ctrls->error_idx = 0;
571 if (cmd == VIDIOC_S_EXT_CTRLS)
572 ret = uvc_ctrl_commit(video);
574 ret = uvc_ctrl_rollback(video);
578 /* Get, Set & Enum input */
579 case VIDIOC_ENUMINPUT:
581 const struct uvc_entity *selector = video->selector;
582 struct v4l2_input *input = arg;
583 struct uvc_entity *iterm = NULL;
584 u32 index = input->index;
587 if (selector == NULL ||
588 (video->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
591 iterm = list_first_entry(&video->iterms,
592 struct uvc_entity, chain);
594 } else if (pin < selector->selector.bNrInPins) {
595 pin = selector->selector.baSourceID[index];
596 list_for_each_entry(iterm, video->iterms.next, chain) {
597 if (iterm->id == pin)
602 if (iterm == NULL || iterm->id != pin)
605 memset(input, 0, sizeof *input);
606 input->index = index;
607 strncpy(input->name, iterm->name, sizeof input->name);
608 if (UVC_ENTITY_TYPE(iterm) == ITT_CAMERA)
609 input->type = V4L2_INPUT_TYPE_CAMERA;
617 if (video->selector == NULL ||
618 (video->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
623 ret = uvc_query_ctrl(video->dev, GET_CUR, video->selector->id,
624 video->dev->intfnum, SU_INPUT_SELECT_CONTROL,
629 *(int *)arg = input - 1;
635 u8 input = *(u32 *)arg + 1;
637 if ((ret = uvc_acquire_privileges(handle)) < 0)
640 if (video->selector == NULL ||
641 (video->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
647 if (input > video->selector->selector.bNrInPins)
650 return uvc_query_ctrl(video->dev, SET_CUR, video->selector->id,
651 video->dev->intfnum, SU_INPUT_SELECT_CONTROL,
655 /* Try, Get, Set & Enum format */
656 case VIDIOC_ENUM_FMT:
658 struct v4l2_fmtdesc *fmt = arg;
659 struct uvc_format *format;
661 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
662 fmt->index >= video->streaming->nformats)
665 format = &video->streaming->format[fmt->index];
667 if (format->flags & UVC_FMT_FLAG_COMPRESSED)
668 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
669 strncpy(fmt->description, format->name,
670 sizeof fmt->description);
671 fmt->description[sizeof fmt->description - 1] = 0;
672 fmt->pixelformat = format->fcc;
678 struct uvc_streaming_control probe;
680 if ((ret = uvc_acquire_privileges(handle)) < 0)
683 return uvc_v4l2_try_format(video, arg, &probe, NULL, NULL);
687 if ((ret = uvc_acquire_privileges(handle)) < 0)
690 return uvc_v4l2_set_format(video, arg);
693 return uvc_v4l2_get_format(video, arg);
695 /* Frame size enumeration */
696 case VIDIOC_ENUM_FRAMESIZES:
698 struct v4l2_frmsizeenum *fsize = arg;
699 struct uvc_format *format = NULL;
700 struct uvc_frame *frame;
703 /* Look for the given pixel format */
704 for (i = 0; i < video->streaming->nformats; i++) {
705 if (video->streaming->format[i].fcc ==
706 fsize->pixel_format) {
707 format = &video->streaming->format[i];
714 if (fsize->index >= format->nframes)
717 frame = &format->frame[fsize->index];
718 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
719 fsize->discrete.width = frame->wWidth;
720 fsize->discrete.height = frame->wHeight;
724 /* Frame interval enumeration */
725 case VIDIOC_ENUM_FRAMEINTERVALS:
727 struct v4l2_frmivalenum *fival = arg;
728 struct uvc_format *format = NULL;
729 struct uvc_frame *frame = NULL;
732 /* Look for the given pixel format and frame size */
733 for (i = 0; i < video->streaming->nformats; i++) {
734 if (video->streaming->format[i].fcc ==
735 fival->pixel_format) {
736 format = &video->streaming->format[i];
743 for (i = 0; i < format->nframes; i++) {
744 if (format->frame[i].wWidth == fival->width &&
745 format->frame[i].wHeight == fival->height) {
746 frame = &format->frame[i];
753 if (frame->bFrameIntervalType) {
754 if (fival->index >= frame->bFrameIntervalType)
757 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
758 fival->discrete.numerator =
759 frame->dwFrameInterval[fival->index];
760 fival->discrete.denominator = 10000000;
761 uvc_simplify_fraction(&fival->discrete.numerator,
762 &fival->discrete.denominator, 8, 333);
764 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
765 fival->stepwise.min.numerator =
766 frame->dwFrameInterval[0];
767 fival->stepwise.min.denominator = 10000000;
768 fival->stepwise.max.numerator =
769 frame->dwFrameInterval[1];
770 fival->stepwise.max.denominator = 10000000;
771 fival->stepwise.step.numerator =
772 frame->dwFrameInterval[2];
773 fival->stepwise.step.denominator = 10000000;
774 uvc_simplify_fraction(&fival->stepwise.min.numerator,
775 &fival->stepwise.min.denominator, 8, 333);
776 uvc_simplify_fraction(&fival->stepwise.max.numerator,
777 &fival->stepwise.max.denominator, 8, 333);
778 uvc_simplify_fraction(&fival->stepwise.step.numerator,
779 &fival->stepwise.step.denominator, 8, 333);
784 /* Get & Set streaming parameters */
786 return uvc_v4l2_get_streamparm(video, arg);
789 if ((ret = uvc_acquire_privileges(handle)) < 0)
792 return uvc_v4l2_set_streamparm(video, arg);
794 /* Cropping and scaling */
797 struct v4l2_cropcap *ccap = arg;
798 struct uvc_frame *frame = video->streaming->cur_frame;
800 if (ccap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
803 ccap->bounds.left = 0;
804 ccap->bounds.top = 0;
805 ccap->bounds.width = frame->wWidth;
806 ccap->bounds.height = frame->wHeight;
808 ccap->defrect = ccap->bounds;
810 ccap->pixelaspect.numerator = 1;
811 ccap->pixelaspect.denominator = 1;
819 /* Buffers & streaming */
822 struct v4l2_requestbuffers *rb = arg;
823 unsigned int bufsize =
824 video->streaming->ctrl.dwMaxVideoFrameSize;
826 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
827 rb->memory != V4L2_MEMORY_MMAP)
830 if ((ret = uvc_acquire_privileges(handle)) < 0)
833 ret = uvc_alloc_buffers(&video->queue, rb->count, bufsize);
842 case VIDIOC_QUERYBUF:
844 struct v4l2_buffer *buf = arg;
846 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
849 if (!uvc_has_privileges(handle))
852 return uvc_query_buffer(&video->queue, buf);
856 if (!uvc_has_privileges(handle))
859 return uvc_queue_buffer(&video->queue, arg);
862 if (!uvc_has_privileges(handle))
865 return uvc_dequeue_buffer(&video->queue, arg,
866 file->f_flags & O_NONBLOCK);
868 case VIDIOC_STREAMON:
872 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
875 if (!uvc_has_privileges(handle))
878 if ((ret = uvc_video_enable(video, 1)) < 0)
883 case VIDIOC_STREAMOFF:
887 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
890 if (!uvc_has_privileges(handle))
893 return uvc_video_enable(video, 0);
896 /* Analog video standards make no sense for digital cameras. */
898 case VIDIOC_QUERYSTD:
904 case VIDIOC_ENUMAUDIO:
905 case VIDIOC_ENUMAUDOUT:
907 case VIDIOC_ENUMOUTPUT:
908 uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd);
911 /* Dynamic controls. */
912 case UVCIOC_CTRL_ADD:
914 struct uvc_xu_control_info *xinfo = arg;
915 struct uvc_control_info *info;
917 if (!capable(CAP_SYS_ADMIN))
920 info = kmalloc(sizeof *info, GFP_KERNEL);
924 memcpy(info->entity, xinfo->entity, sizeof info->entity);
925 info->index = xinfo->index;
926 info->selector = xinfo->selector;
927 info->size = xinfo->size;
928 info->flags = xinfo->flags;
930 info->flags |= UVC_CONTROL_GET_MIN | UVC_CONTROL_GET_MAX |
931 UVC_CONTROL_GET_RES | UVC_CONTROL_GET_DEF;
933 ret = uvc_ctrl_add_info(info);
939 case UVCIOC_CTRL_MAP:
941 struct uvc_xu_control_mapping *xmap = arg;
942 struct uvc_control_mapping *map;
944 if (!capable(CAP_SYS_ADMIN))
947 map = kmalloc(sizeof *map, GFP_KERNEL);
952 memcpy(map->name, xmap->name, sizeof map->name);
953 memcpy(map->entity, xmap->entity, sizeof map->entity);
954 map->selector = xmap->selector;
955 map->size = xmap->size;
956 map->offset = xmap->offset;
957 map->v4l2_type = xmap->v4l2_type;
958 map->data_type = xmap->data_type;
960 ret = uvc_ctrl_add_mapping(map);
966 case UVCIOC_CTRL_GET:
967 return uvc_xu_ctrl_query(video, arg, 0);
969 case UVCIOC_CTRL_SET:
970 return uvc_xu_ctrl_query(video, arg, 1);
973 if ((ret = v4l_compat_translate_ioctl(file, cmd, arg,
974 uvc_v4l2_do_ioctl)) == -ENOIOCTLCMD)
975 uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n",
983 static int uvc_v4l2_ioctl(struct inode *inode, struct file *file,
984 unsigned int cmd, unsigned long arg)
986 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_ioctl\n");
987 return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl);
990 static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
991 size_t count, loff_t *ppos)
993 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
1000 static void uvc_vm_open(struct vm_area_struct *vma)
1002 struct uvc_buffer *buffer = vma->vm_private_data;
1003 buffer->vma_use_count++;
1006 static void uvc_vm_close(struct vm_area_struct *vma)
1008 struct uvc_buffer *buffer = vma->vm_private_data;
1009 buffer->vma_use_count--;
1012 static struct vm_operations_struct uvc_vm_ops = {
1013 .open = uvc_vm_open,
1014 .close = uvc_vm_close,
1017 static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1019 struct uvc_video_device *video = video_drvdata(file);
1020 struct uvc_buffer *uninitialized_var(buffer);
1022 unsigned long addr, start, size;
1026 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
1028 start = vma->vm_start;
1029 size = vma->vm_end - vma->vm_start;
1031 mutex_lock(&video->queue.mutex);
1033 for (i = 0; i < video->queue.count; ++i) {
1034 buffer = &video->queue.buffer[i];
1035 if ((buffer->buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
1039 if (i == video->queue.count || size != video->queue.buf_size) {
1045 * VM_IO marks the area as being an mmaped region for I/O to a
1046 * device. It also prevents the region from being core dumped.
1048 vma->vm_flags |= VM_IO;
1050 addr = (unsigned long)video->queue.mem + buffer->buf.m.offset;
1052 page = vmalloc_to_page((void *)addr);
1053 if ((ret = vm_insert_page(vma, start, page)) < 0)
1061 vma->vm_ops = &uvc_vm_ops;
1062 vma->vm_private_data = buffer;
1066 mutex_unlock(&video->queue.mutex);
1070 static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
1072 struct uvc_video_device *video = video_drvdata(file);
1074 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
1076 return uvc_queue_poll(&video->queue, file, wait);
1079 struct file_operations uvc_fops = {
1080 .owner = THIS_MODULE,
1081 .open = uvc_v4l2_open,
1082 .release = uvc_v4l2_release,
1083 .ioctl = uvc_v4l2_ioctl,
1084 .compat_ioctl = v4l_compat_ioctl32,
1085 .llseek = no_llseek,
1086 .read = uvc_v4l2_read,
1087 .mmap = uvc_v4l2_mmap,
1088 .poll = uvc_v4l2_poll,