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/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 that 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 strlcpy(query_menu->name, menu_info->name, sizeof query_menu->name);
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 != video->streaming->type)
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 != video->streaming->type)
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 != video->streaming->type)
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 != video->streaming->type)
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 = video->streaming->type;
277 if (video->streaming->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;
285 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
286 parm->parm.output.outputmode = 0;
287 parm->parm.output.timeperframe.numerator = numerator;
288 parm->parm.output.timeperframe.denominator = denominator;
294 static int uvc_v4l2_set_streamparm(struct uvc_video_device *video,
295 struct v4l2_streamparm *parm)
297 struct uvc_frame *frame = video->streaming->cur_frame;
298 struct uvc_streaming_control probe;
299 struct v4l2_fract timeperframe;
303 if (parm->type != video->streaming->type)
306 if (uvc_queue_streaming(&video->queue))
309 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
310 timeperframe = parm->parm.capture.timeperframe;
312 timeperframe = parm->parm.output.timeperframe;
314 memcpy(&probe, &video->streaming->ctrl, sizeof probe);
315 interval = uvc_fraction_to_interval(timeperframe.numerator,
316 timeperframe.denominator);
318 uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
319 timeperframe.numerator, timeperframe.denominator, interval);
320 probe.dwFrameInterval = uvc_try_frame_interval(frame, interval);
322 /* Probe the device with the new settings. */
323 if ((ret = uvc_probe_video(video, &probe)) < 0)
326 memcpy(&video->streaming->ctrl, &probe, sizeof probe);
328 /* Return the actual frame period. */
329 timeperframe.numerator = probe.dwFrameInterval;
330 timeperframe.denominator = 10000000;
331 uvc_simplify_fraction(&timeperframe.numerator,
332 &timeperframe.denominator, 8, 333);
334 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
335 parm->parm.capture.timeperframe = timeperframe;
337 parm->parm.output.timeperframe = timeperframe;
342 /* ------------------------------------------------------------------------
343 * Privilege management
347 * Privilege management is the multiple-open implementation basis. The current
348 * implementation is completely transparent for the end-user and doesn't
349 * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls.
350 * Those ioctls enable finer control on the device (by making possible for a
351 * user to request exclusive access to a device), but are not mature yet.
352 * Switching to the V4L2 priority mechanism might be considered in the future
353 * if this situation changes.
355 * Each open instance of a UVC device can either be in a privileged or
356 * unprivileged state. Only a single instance can be in a privileged state at
357 * a given time. Trying to perform an operation that requires privileges will
358 * automatically acquire the required privileges if possible, or return -EBUSY
359 * otherwise. Privileges are dismissed when closing the instance.
361 * Operations that require privileges are:
369 static int uvc_acquire_privileges(struct uvc_fh *handle)
373 /* Always succeed if the handle is already privileged. */
374 if (handle->state == UVC_HANDLE_ACTIVE)
377 /* Check if the device already has a privileged handle. */
378 mutex_lock(&uvc_driver.open_mutex);
379 if (atomic_inc_return(&handle->device->active) != 1) {
380 atomic_dec(&handle->device->active);
385 handle->state = UVC_HANDLE_ACTIVE;
388 mutex_unlock(&uvc_driver.open_mutex);
392 static void uvc_dismiss_privileges(struct uvc_fh *handle)
394 if (handle->state == UVC_HANDLE_ACTIVE)
395 atomic_dec(&handle->device->active);
397 handle->state = UVC_HANDLE_PASSIVE;
400 static int uvc_has_privileges(struct uvc_fh *handle)
402 return handle->state == UVC_HANDLE_ACTIVE;
405 /* ------------------------------------------------------------------------
406 * V4L2 file operations
409 static int uvc_v4l2_open(struct file *file)
411 struct uvc_video_device *video;
412 struct uvc_fh *handle;
415 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
416 mutex_lock(&uvc_driver.open_mutex);
417 video = video_drvdata(file);
419 if (video->dev->state & UVC_DEV_DISCONNECTED) {
424 ret = usb_autopm_get_interface(video->dev->intf);
428 /* Create the device handle. */
429 handle = kzalloc(sizeof *handle, GFP_KERNEL);
430 if (handle == NULL) {
431 usb_autopm_put_interface(video->dev->intf);
436 handle->device = video;
437 handle->state = UVC_HANDLE_PASSIVE;
438 file->private_data = handle;
440 kref_get(&video->dev->kref);
443 mutex_unlock(&uvc_driver.open_mutex);
447 static int uvc_v4l2_release(struct file *file)
449 struct uvc_video_device *video = video_drvdata(file);
450 struct uvc_fh *handle = (struct uvc_fh *)file->private_data;
452 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");
454 /* Only free resources if this is a privileged handle. */
455 if (uvc_has_privileges(handle)) {
456 uvc_video_enable(video, 0);
458 mutex_lock(&video->queue.mutex);
459 if (uvc_free_buffers(&video->queue) < 0)
460 uvc_printk(KERN_ERR, "uvc_v4l2_release: Unable to "
462 mutex_unlock(&video->queue.mutex);
465 /* Release the file handle. */
466 uvc_dismiss_privileges(handle);
468 file->private_data = NULL;
470 usb_autopm_put_interface(video->dev->intf);
471 kref_put(&video->dev->kref, uvc_delete);
475 static long uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
477 struct video_device *vdev = video_devdata(file);
478 struct uvc_video_device *video = video_get_drvdata(vdev);
479 struct uvc_fh *handle = (struct uvc_fh *)file->private_data;
483 /* Query capabilities */
484 case VIDIOC_QUERYCAP:
486 struct v4l2_capability *cap = arg;
488 memset(cap, 0, sizeof *cap);
489 strlcpy(cap->driver, "uvcvideo", sizeof cap->driver);
490 strlcpy(cap->card, vdev->name, sizeof cap->card);
491 usb_make_path(video->dev->udev,
492 cap->bus_info, sizeof(cap->bus_info));
493 cap->version = DRIVER_VERSION_NUMBER;
494 if (video->streaming->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
495 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
496 | V4L2_CAP_STREAMING;
498 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT
499 | V4L2_CAP_STREAMING;
503 /* Get, Set & Query control */
504 case VIDIOC_QUERYCTRL:
505 return uvc_query_v4l2_ctrl(video, arg);
509 struct v4l2_control *ctrl = arg;
510 struct v4l2_ext_control xctrl;
512 memset(&xctrl, 0, sizeof xctrl);
515 uvc_ctrl_begin(video);
516 ret = uvc_ctrl_get(video, &xctrl);
517 uvc_ctrl_rollback(video);
519 ctrl->value = xctrl.value;
525 struct v4l2_control *ctrl = arg;
526 struct v4l2_ext_control xctrl;
528 memset(&xctrl, 0, sizeof xctrl);
530 xctrl.value = ctrl->value;
532 uvc_ctrl_begin(video);
533 ret = uvc_ctrl_set(video, &xctrl);
535 uvc_ctrl_rollback(video);
538 ret = uvc_ctrl_commit(video);
542 case VIDIOC_QUERYMENU:
543 return uvc_v4l2_query_menu(video, arg);
545 case VIDIOC_G_EXT_CTRLS:
547 struct v4l2_ext_controls *ctrls = arg;
548 struct v4l2_ext_control *ctrl = ctrls->controls;
551 uvc_ctrl_begin(video);
552 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
553 ret = uvc_ctrl_get(video, ctrl);
555 uvc_ctrl_rollback(video);
556 ctrls->error_idx = i;
560 ctrls->error_idx = 0;
561 ret = uvc_ctrl_rollback(video);
565 case VIDIOC_S_EXT_CTRLS:
566 case VIDIOC_TRY_EXT_CTRLS:
568 struct v4l2_ext_controls *ctrls = arg;
569 struct v4l2_ext_control *ctrl = ctrls->controls;
572 ret = uvc_ctrl_begin(video);
576 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
577 ret = uvc_ctrl_set(video, ctrl);
579 uvc_ctrl_rollback(video);
580 ctrls->error_idx = i;
585 ctrls->error_idx = 0;
587 if (cmd == VIDIOC_S_EXT_CTRLS)
588 ret = uvc_ctrl_commit(video);
590 ret = uvc_ctrl_rollback(video);
594 /* Get, Set & Enum input */
595 case VIDIOC_ENUMINPUT:
597 const struct uvc_entity *selector = video->selector;
598 struct v4l2_input *input = arg;
599 struct uvc_entity *iterm = NULL;
600 u32 index = input->index;
603 if (selector == NULL ||
604 (video->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
607 iterm = list_first_entry(&video->iterms,
608 struct uvc_entity, chain);
610 } else if (pin < selector->selector.bNrInPins) {
611 pin = selector->selector.baSourceID[index];
612 list_for_each_entry(iterm, video->iterms.next, chain) {
613 if (iterm->id == pin)
618 if (iterm == NULL || iterm->id != pin)
621 memset(input, 0, sizeof *input);
622 input->index = index;
623 strlcpy(input->name, iterm->name, sizeof input->name);
624 if (UVC_ENTITY_TYPE(iterm) == ITT_CAMERA)
625 input->type = V4L2_INPUT_TYPE_CAMERA;
633 if (video->selector == NULL ||
634 (video->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
639 ret = uvc_query_ctrl(video->dev, GET_CUR, video->selector->id,
640 video->dev->intfnum, SU_INPUT_SELECT_CONTROL,
645 *(int *)arg = input - 1;
651 u8 input = *(u32 *)arg + 1;
653 if ((ret = uvc_acquire_privileges(handle)) < 0)
656 if (video->selector == NULL ||
657 (video->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
663 if (input > video->selector->selector.bNrInPins)
666 return uvc_query_ctrl(video->dev, SET_CUR, video->selector->id,
667 video->dev->intfnum, SU_INPUT_SELECT_CONTROL,
671 /* Try, Get, Set & Enum format */
672 case VIDIOC_ENUM_FMT:
674 struct v4l2_fmtdesc *fmt = arg;
675 struct uvc_format *format;
676 enum v4l2_buf_type type = fmt->type;
677 __u32 index = fmt->index;
679 if (fmt->type != video->streaming->type ||
680 fmt->index >= video->streaming->nformats)
683 memset(fmt, 0, sizeof(*fmt));
687 format = &video->streaming->format[fmt->index];
689 if (format->flags & UVC_FMT_FLAG_COMPRESSED)
690 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
691 strlcpy(fmt->description, format->name,
692 sizeof fmt->description);
693 fmt->description[sizeof fmt->description - 1] = 0;
694 fmt->pixelformat = format->fcc;
700 struct uvc_streaming_control probe;
702 if ((ret = uvc_acquire_privileges(handle)) < 0)
705 return uvc_v4l2_try_format(video, arg, &probe, NULL, NULL);
709 if ((ret = uvc_acquire_privileges(handle)) < 0)
712 return uvc_v4l2_set_format(video, arg);
715 return uvc_v4l2_get_format(video, arg);
717 /* Frame size enumeration */
718 case VIDIOC_ENUM_FRAMESIZES:
720 struct v4l2_frmsizeenum *fsize = arg;
721 struct uvc_format *format = NULL;
722 struct uvc_frame *frame;
725 /* Look for the given pixel format */
726 for (i = 0; i < video->streaming->nformats; i++) {
727 if (video->streaming->format[i].fcc ==
728 fsize->pixel_format) {
729 format = &video->streaming->format[i];
736 if (fsize->index >= format->nframes)
739 frame = &format->frame[fsize->index];
740 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
741 fsize->discrete.width = frame->wWidth;
742 fsize->discrete.height = frame->wHeight;
746 /* Frame interval enumeration */
747 case VIDIOC_ENUM_FRAMEINTERVALS:
749 struct v4l2_frmivalenum *fival = arg;
750 struct uvc_format *format = NULL;
751 struct uvc_frame *frame = NULL;
754 /* Look for the given pixel format and frame size */
755 for (i = 0; i < video->streaming->nformats; i++) {
756 if (video->streaming->format[i].fcc ==
757 fival->pixel_format) {
758 format = &video->streaming->format[i];
765 for (i = 0; i < format->nframes; i++) {
766 if (format->frame[i].wWidth == fival->width &&
767 format->frame[i].wHeight == fival->height) {
768 frame = &format->frame[i];
775 if (frame->bFrameIntervalType) {
776 if (fival->index >= frame->bFrameIntervalType)
779 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
780 fival->discrete.numerator =
781 frame->dwFrameInterval[fival->index];
782 fival->discrete.denominator = 10000000;
783 uvc_simplify_fraction(&fival->discrete.numerator,
784 &fival->discrete.denominator, 8, 333);
786 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
787 fival->stepwise.min.numerator =
788 frame->dwFrameInterval[0];
789 fival->stepwise.min.denominator = 10000000;
790 fival->stepwise.max.numerator =
791 frame->dwFrameInterval[1];
792 fival->stepwise.max.denominator = 10000000;
793 fival->stepwise.step.numerator =
794 frame->dwFrameInterval[2];
795 fival->stepwise.step.denominator = 10000000;
796 uvc_simplify_fraction(&fival->stepwise.min.numerator,
797 &fival->stepwise.min.denominator, 8, 333);
798 uvc_simplify_fraction(&fival->stepwise.max.numerator,
799 &fival->stepwise.max.denominator, 8, 333);
800 uvc_simplify_fraction(&fival->stepwise.step.numerator,
801 &fival->stepwise.step.denominator, 8, 333);
806 /* Get & Set streaming parameters */
808 return uvc_v4l2_get_streamparm(video, arg);
811 if ((ret = uvc_acquire_privileges(handle)) < 0)
814 return uvc_v4l2_set_streamparm(video, arg);
816 /* Cropping and scaling */
819 struct v4l2_cropcap *ccap = arg;
820 struct uvc_frame *frame = video->streaming->cur_frame;
822 if (ccap->type != video->streaming->type)
825 ccap->bounds.left = 0;
826 ccap->bounds.top = 0;
827 ccap->bounds.width = frame->wWidth;
828 ccap->bounds.height = frame->wHeight;
830 ccap->defrect = ccap->bounds;
832 ccap->pixelaspect.numerator = 1;
833 ccap->pixelaspect.denominator = 1;
841 /* Buffers & streaming */
844 struct v4l2_requestbuffers *rb = arg;
845 unsigned int bufsize =
846 video->streaming->ctrl.dwMaxVideoFrameSize;
848 if (rb->type != video->streaming->type ||
849 rb->memory != V4L2_MEMORY_MMAP)
852 if ((ret = uvc_acquire_privileges(handle)) < 0)
855 ret = uvc_alloc_buffers(&video->queue, rb->count, bufsize);
864 case VIDIOC_QUERYBUF:
866 struct v4l2_buffer *buf = arg;
868 if (buf->type != video->streaming->type)
871 if (!uvc_has_privileges(handle))
874 return uvc_query_buffer(&video->queue, buf);
878 if (!uvc_has_privileges(handle))
881 return uvc_queue_buffer(&video->queue, arg);
884 if (!uvc_has_privileges(handle))
887 return uvc_dequeue_buffer(&video->queue, arg,
888 file->f_flags & O_NONBLOCK);
890 case VIDIOC_STREAMON:
894 if (*type != video->streaming->type)
897 if (!uvc_has_privileges(handle))
900 if ((ret = uvc_video_enable(video, 1)) < 0)
905 case VIDIOC_STREAMOFF:
909 if (*type != video->streaming->type)
912 if (!uvc_has_privileges(handle))
915 return uvc_video_enable(video, 0);
918 /* Analog video standards make no sense for digital cameras. */
920 case VIDIOC_QUERYSTD:
926 case VIDIOC_ENUMAUDIO:
927 case VIDIOC_ENUMAUDOUT:
929 case VIDIOC_ENUMOUTPUT:
930 uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd);
933 /* Dynamic controls. */
934 case UVCIOC_CTRL_ADD:
936 struct uvc_xu_control_info *xinfo = arg;
937 struct uvc_control_info *info;
939 if (!capable(CAP_SYS_ADMIN))
942 info = kzalloc(sizeof *info, GFP_KERNEL);
946 memcpy(info->entity, xinfo->entity, sizeof info->entity);
947 info->index = xinfo->index;
948 info->selector = xinfo->selector;
949 info->size = xinfo->size;
950 info->flags = xinfo->flags;
952 info->flags |= UVC_CONTROL_GET_MIN | UVC_CONTROL_GET_MAX |
953 UVC_CONTROL_GET_RES | UVC_CONTROL_GET_DEF;
955 ret = uvc_ctrl_add_info(info);
961 case UVCIOC_CTRL_MAP:
963 struct uvc_xu_control_mapping *xmap = arg;
964 struct uvc_control_mapping *map;
966 if (!capable(CAP_SYS_ADMIN))
969 map = kzalloc(sizeof *map, GFP_KERNEL);
974 memcpy(map->name, xmap->name, sizeof map->name);
975 memcpy(map->entity, xmap->entity, sizeof map->entity);
976 map->selector = xmap->selector;
977 map->size = xmap->size;
978 map->offset = xmap->offset;
979 map->v4l2_type = xmap->v4l2_type;
980 map->data_type = xmap->data_type;
982 ret = uvc_ctrl_add_mapping(map);
988 case UVCIOC_CTRL_GET:
989 return uvc_xu_ctrl_query(video, arg, 0);
991 case UVCIOC_CTRL_SET:
992 return uvc_xu_ctrl_query(video, arg, 1);
995 if ((ret = v4l_compat_translate_ioctl(file, cmd, arg,
996 uvc_v4l2_do_ioctl)) == -ENOIOCTLCMD)
997 uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n",
1005 static long uvc_v4l2_ioctl(struct file *file,
1006 unsigned int cmd, unsigned long arg)
1008 if (uvc_trace_param & UVC_TRACE_IOCTL) {
1009 uvc_printk(KERN_DEBUG, "uvc_v4l2_ioctl(");
1010 v4l_printk_ioctl(cmd);
1014 return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl);
1017 static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1018 size_t count, loff_t *ppos)
1020 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
1027 static void uvc_vm_open(struct vm_area_struct *vma)
1029 struct uvc_buffer *buffer = vma->vm_private_data;
1030 buffer->vma_use_count++;
1033 static void uvc_vm_close(struct vm_area_struct *vma)
1035 struct uvc_buffer *buffer = vma->vm_private_data;
1036 buffer->vma_use_count--;
1039 static struct vm_operations_struct uvc_vm_ops = {
1040 .open = uvc_vm_open,
1041 .close = uvc_vm_close,
1044 static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1046 struct uvc_video_device *video = video_drvdata(file);
1047 struct uvc_buffer *uninitialized_var(buffer);
1049 unsigned long addr, start, size;
1053 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
1055 start = vma->vm_start;
1056 size = vma->vm_end - vma->vm_start;
1058 mutex_lock(&video->queue.mutex);
1060 for (i = 0; i < video->queue.count; ++i) {
1061 buffer = &video->queue.buffer[i];
1062 if ((buffer->buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
1066 if (i == video->queue.count || size != video->queue.buf_size) {
1072 * VM_IO marks the area as being an mmaped region for I/O to a
1073 * device. It also prevents the region from being core dumped.
1075 vma->vm_flags |= VM_IO;
1077 addr = (unsigned long)video->queue.mem + buffer->buf.m.offset;
1079 page = vmalloc_to_page((void *)addr);
1080 if ((ret = vm_insert_page(vma, start, page)) < 0)
1088 vma->vm_ops = &uvc_vm_ops;
1089 vma->vm_private_data = buffer;
1093 mutex_unlock(&video->queue.mutex);
1097 static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
1099 struct uvc_video_device *video = video_drvdata(file);
1101 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
1103 return uvc_queue_poll(&video->queue, file, wait);
1106 const struct v4l2_file_operations uvc_fops = {
1107 .owner = THIS_MODULE,
1108 .open = uvc_v4l2_open,
1109 .release = uvc_v4l2_release,
1110 .ioctl = uvc_v4l2_ioctl,
1111 .read = uvc_v4l2_read,
1112 .mmap = uvc_v4l2_mmap,
1113 .poll = uvc_v4l2_poll,