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;
49 u32 index = query_menu->index;
50 u32 id = query_menu->id;
52 ctrl = uvc_find_control(video, query_menu->id, &mapping);
53 if (ctrl == NULL || mapping->v4l2_type != V4L2_CTRL_TYPE_MENU)
56 if (query_menu->index >= mapping->menu_count)
59 memset(query_menu, 0, sizeof(*query_menu));
61 query_menu->index = index;
63 menu_info = &mapping->menu_info[query_menu->index];
64 strlcpy(query_menu->name, menu_info->name, sizeof query_menu->name);
69 * Find the frame interval closest to the requested frame interval for the
70 * given frame format and size. This should be done by the device as part of
71 * the Video Probe and Commit negotiation, but some hardware don't implement
74 static __u32 uvc_try_frame_interval(struct uvc_frame *frame, __u32 interval)
78 if (frame->bFrameIntervalType) {
79 __u32 best = -1, dist;
81 for (i = 0; i < frame->bFrameIntervalType; ++i) {
82 dist = interval > frame->dwFrameInterval[i]
83 ? interval - frame->dwFrameInterval[i]
84 : frame->dwFrameInterval[i] - interval;
92 interval = frame->dwFrameInterval[i-1];
94 const __u32 min = frame->dwFrameInterval[0];
95 const __u32 max = frame->dwFrameInterval[1];
96 const __u32 step = frame->dwFrameInterval[2];
98 interval = min + (interval - min + step/2) / step * step;
106 static int uvc_v4l2_try_format(struct uvc_video_device *video,
107 struct v4l2_format *fmt, struct uvc_streaming_control *probe,
108 struct uvc_format **uvc_format, struct uvc_frame **uvc_frame)
110 struct uvc_format *format = NULL;
111 struct uvc_frame *frame = NULL;
113 unsigned int d, maxd;
119 if (fmt->type != video->streaming->type)
122 fcc = (__u8 *)&fmt->fmt.pix.pixelformat;
123 uvc_trace(UVC_TRACE_FORMAT, "Trying format 0x%08x (%c%c%c%c): %ux%u.\n",
124 fmt->fmt.pix.pixelformat,
125 fcc[0], fcc[1], fcc[2], fcc[3],
126 fmt->fmt.pix.width, fmt->fmt.pix.height);
128 /* Check if the hardware supports the requested format. */
129 for (i = 0; i < video->streaming->nformats; ++i) {
130 format = &video->streaming->format[i];
131 if (format->fcc == fmt->fmt.pix.pixelformat)
135 if (format == NULL || format->fcc != fmt->fmt.pix.pixelformat) {
136 uvc_trace(UVC_TRACE_FORMAT, "Unsupported format 0x%08x.\n",
137 fmt->fmt.pix.pixelformat);
141 /* Find the closest image size. The distance between image sizes is
142 * the size in pixels of the non-overlapping regions between the
143 * requested size and the frame-specified size.
145 rw = fmt->fmt.pix.width;
146 rh = fmt->fmt.pix.height;
147 maxd = (unsigned int)-1;
149 for (i = 0; i < format->nframes; ++i) {
150 __u16 w = format->frame[i].wWidth;
151 __u16 h = format->frame[i].wHeight;
153 d = min(w, rw) * min(h, rh);
154 d = w*h + rw*rh - 2*d;
157 frame = &format->frame[i];
165 uvc_trace(UVC_TRACE_FORMAT, "Unsupported size %ux%u.\n",
166 fmt->fmt.pix.width, fmt->fmt.pix.height);
170 /* Use the default frame interval. */
171 interval = frame->dwDefaultFrameInterval;
172 uvc_trace(UVC_TRACE_FORMAT, "Using default frame interval %u.%u us "
173 "(%u.%u fps).\n", interval/10, interval%10, 10000000/interval,
174 (100000000/interval)%10);
176 /* Set the format index, frame index and frame interval. */
177 memset(probe, 0, sizeof *probe);
178 probe->bmHint = 1; /* dwFrameInterval */
179 probe->bFormatIndex = format->index;
180 probe->bFrameIndex = frame->bFrameIndex;
181 probe->dwFrameInterval = uvc_try_frame_interval(frame, interval);
182 /* Some webcams stall the probe control set request when the
183 * dwMaxVideoFrameSize field is set to zero. The UVC specification
184 * clearly states that the field is read-only from the host, so this
185 * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by
186 * the webcam to work around the problem.
188 * The workaround could probably be enabled for all webcams, so the
189 * quirk can be removed if needed. It's currently useful to detect
190 * webcam bugs and fix them before they hit the market (providing
191 * developers test their webcams with the Linux driver as well as with
192 * the Windows driver).
194 if (video->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS)
195 probe->dwMaxVideoFrameSize =
196 video->streaming->ctrl.dwMaxVideoFrameSize;
198 /* Probe the device. */
199 if ((ret = uvc_probe_video(video, probe)) < 0)
202 fmt->fmt.pix.width = frame->wWidth;
203 fmt->fmt.pix.height = frame->wHeight;
204 fmt->fmt.pix.field = V4L2_FIELD_NONE;
205 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
206 fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize;
207 fmt->fmt.pix.colorspace = format->colorspace;
208 fmt->fmt.pix.priv = 0;
210 if (uvc_format != NULL)
211 *uvc_format = format;
212 if (uvc_frame != NULL)
219 static int uvc_v4l2_get_format(struct uvc_video_device *video,
220 struct v4l2_format *fmt)
222 struct uvc_format *format = video->streaming->cur_format;
223 struct uvc_frame *frame = video->streaming->cur_frame;
225 if (fmt->type != video->streaming->type)
228 if (format == NULL || frame == NULL)
231 fmt->fmt.pix.pixelformat = format->fcc;
232 fmt->fmt.pix.width = frame->wWidth;
233 fmt->fmt.pix.height = frame->wHeight;
234 fmt->fmt.pix.field = V4L2_FIELD_NONE;
235 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
236 fmt->fmt.pix.sizeimage = video->streaming->ctrl.dwMaxVideoFrameSize;
237 fmt->fmt.pix.colorspace = format->colorspace;
238 fmt->fmt.pix.priv = 0;
243 static int uvc_v4l2_set_format(struct uvc_video_device *video,
244 struct v4l2_format *fmt)
246 struct uvc_streaming_control probe;
247 struct uvc_format *format;
248 struct uvc_frame *frame;
251 if (fmt->type != video->streaming->type)
254 if (uvc_queue_allocated(&video->queue))
257 ret = uvc_v4l2_try_format(video, fmt, &probe, &format, &frame);
261 memcpy(&video->streaming->ctrl, &probe, sizeof probe);
262 video->streaming->cur_format = format;
263 video->streaming->cur_frame = frame;
268 static int uvc_v4l2_get_streamparm(struct uvc_video_device *video,
269 struct v4l2_streamparm *parm)
271 uint32_t numerator, denominator;
273 if (parm->type != video->streaming->type)
276 numerator = video->streaming->ctrl.dwFrameInterval;
277 denominator = 10000000;
278 uvc_simplify_fraction(&numerator, &denominator, 8, 333);
280 memset(parm, 0, sizeof *parm);
281 parm->type = video->streaming->type;
283 if (video->streaming->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
284 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
285 parm->parm.capture.capturemode = 0;
286 parm->parm.capture.timeperframe.numerator = numerator;
287 parm->parm.capture.timeperframe.denominator = denominator;
288 parm->parm.capture.extendedmode = 0;
289 parm->parm.capture.readbuffers = 0;
291 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
292 parm->parm.output.outputmode = 0;
293 parm->parm.output.timeperframe.numerator = numerator;
294 parm->parm.output.timeperframe.denominator = denominator;
300 static int uvc_v4l2_set_streamparm(struct uvc_video_device *video,
301 struct v4l2_streamparm *parm)
303 struct uvc_frame *frame = video->streaming->cur_frame;
304 struct uvc_streaming_control probe;
305 struct v4l2_fract timeperframe;
309 if (parm->type != video->streaming->type)
312 if (uvc_queue_streaming(&video->queue))
315 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
316 timeperframe = parm->parm.capture.timeperframe;
318 timeperframe = parm->parm.output.timeperframe;
320 memcpy(&probe, &video->streaming->ctrl, sizeof probe);
321 interval = uvc_fraction_to_interval(timeperframe.numerator,
322 timeperframe.denominator);
324 uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
325 timeperframe.numerator, timeperframe.denominator, interval);
326 probe.dwFrameInterval = uvc_try_frame_interval(frame, interval);
328 /* Probe the device with the new settings. */
329 if ((ret = uvc_probe_video(video, &probe)) < 0)
332 memcpy(&video->streaming->ctrl, &probe, sizeof probe);
334 /* Return the actual frame period. */
335 timeperframe.numerator = probe.dwFrameInterval;
336 timeperframe.denominator = 10000000;
337 uvc_simplify_fraction(&timeperframe.numerator,
338 &timeperframe.denominator, 8, 333);
340 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
341 parm->parm.capture.timeperframe = timeperframe;
343 parm->parm.output.timeperframe = timeperframe;
348 /* ------------------------------------------------------------------------
349 * Privilege management
353 * Privilege management is the multiple-open implementation basis. The current
354 * implementation is completely transparent for the end-user and doesn't
355 * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls.
356 * Those ioctls enable finer control on the device (by making possible for a
357 * user to request exclusive access to a device), but are not mature yet.
358 * Switching to the V4L2 priority mechanism might be considered in the future
359 * if this situation changes.
361 * Each open instance of a UVC device can either be in a privileged or
362 * unprivileged state. Only a single instance can be in a privileged state at
363 * a given time. Trying to perform an operation that requires privileges will
364 * automatically acquire the required privileges if possible, or return -EBUSY
365 * otherwise. Privileges are dismissed when closing the instance.
367 * Operations that require privileges are:
375 static int uvc_acquire_privileges(struct uvc_fh *handle)
379 /* Always succeed if the handle is already privileged. */
380 if (handle->state == UVC_HANDLE_ACTIVE)
383 /* Check if the device already has a privileged handle. */
384 mutex_lock(&uvc_driver.open_mutex);
385 if (atomic_inc_return(&handle->device->active) != 1) {
386 atomic_dec(&handle->device->active);
391 handle->state = UVC_HANDLE_ACTIVE;
394 mutex_unlock(&uvc_driver.open_mutex);
398 static void uvc_dismiss_privileges(struct uvc_fh *handle)
400 if (handle->state == UVC_HANDLE_ACTIVE)
401 atomic_dec(&handle->device->active);
403 handle->state = UVC_HANDLE_PASSIVE;
406 static int uvc_has_privileges(struct uvc_fh *handle)
408 return handle->state == UVC_HANDLE_ACTIVE;
411 /* ------------------------------------------------------------------------
412 * V4L2 file operations
415 static int uvc_v4l2_open(struct file *file)
417 struct uvc_video_device *video;
418 struct uvc_fh *handle;
421 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
422 mutex_lock(&uvc_driver.open_mutex);
423 video = video_drvdata(file);
425 if (video->dev->state & UVC_DEV_DISCONNECTED) {
430 ret = usb_autopm_get_interface(video->dev->intf);
434 /* Create the device handle. */
435 handle = kzalloc(sizeof *handle, GFP_KERNEL);
436 if (handle == NULL) {
437 usb_autopm_put_interface(video->dev->intf);
442 if (atomic_inc_return(&video->dev->users) == 1) {
443 if ((ret = uvc_status_start(video->dev)) < 0) {
444 usb_autopm_put_interface(video->dev->intf);
445 atomic_dec(&video->dev->users);
451 handle->device = video;
452 handle->state = UVC_HANDLE_PASSIVE;
453 file->private_data = handle;
455 kref_get(&video->dev->kref);
458 mutex_unlock(&uvc_driver.open_mutex);
462 static int uvc_v4l2_release(struct file *file)
464 struct uvc_video_device *video = video_drvdata(file);
465 struct uvc_fh *handle = (struct uvc_fh *)file->private_data;
467 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");
469 /* Only free resources if this is a privileged handle. */
470 if (uvc_has_privileges(handle)) {
471 uvc_video_enable(video, 0);
473 mutex_lock(&video->queue.mutex);
474 if (uvc_free_buffers(&video->queue) < 0)
475 uvc_printk(KERN_ERR, "uvc_v4l2_release: Unable to "
477 mutex_unlock(&video->queue.mutex);
480 /* Release the file handle. */
481 uvc_dismiss_privileges(handle);
483 file->private_data = NULL;
485 if (atomic_dec_return(&video->dev->users) == 0)
486 uvc_status_stop(video->dev);
488 usb_autopm_put_interface(video->dev->intf);
489 kref_put(&video->dev->kref, uvc_delete);
493 static long uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
495 struct video_device *vdev = video_devdata(file);
496 struct uvc_video_device *video = video_get_drvdata(vdev);
497 struct uvc_fh *handle = (struct uvc_fh *)file->private_data;
501 /* Query capabilities */
502 case VIDIOC_QUERYCAP:
504 struct v4l2_capability *cap = arg;
506 memset(cap, 0, sizeof *cap);
507 strlcpy(cap->driver, "uvcvideo", sizeof cap->driver);
508 strlcpy(cap->card, vdev->name, sizeof cap->card);
509 usb_make_path(video->dev->udev,
510 cap->bus_info, sizeof(cap->bus_info));
511 cap->version = DRIVER_VERSION_NUMBER;
512 if (video->streaming->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
513 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
514 | V4L2_CAP_STREAMING;
516 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT
517 | V4L2_CAP_STREAMING;
521 /* Get, Set & Query control */
522 case VIDIOC_QUERYCTRL:
523 return uvc_query_v4l2_ctrl(video, arg);
527 struct v4l2_control *ctrl = arg;
528 struct v4l2_ext_control xctrl;
530 memset(&xctrl, 0, sizeof xctrl);
533 ret = uvc_ctrl_begin(video);
537 ret = uvc_ctrl_get(video, &xctrl);
538 uvc_ctrl_rollback(video);
540 ctrl->value = xctrl.value;
546 struct v4l2_control *ctrl = arg;
547 struct v4l2_ext_control xctrl;
549 memset(&xctrl, 0, sizeof xctrl);
551 xctrl.value = ctrl->value;
553 ret = uvc_ctrl_begin(video);
557 ret = uvc_ctrl_set(video, &xctrl);
559 uvc_ctrl_rollback(video);
562 ret = uvc_ctrl_commit(video);
566 case VIDIOC_QUERYMENU:
567 return uvc_v4l2_query_menu(video, arg);
569 case VIDIOC_G_EXT_CTRLS:
571 struct v4l2_ext_controls *ctrls = arg;
572 struct v4l2_ext_control *ctrl = ctrls->controls;
575 ret = uvc_ctrl_begin(video);
579 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
580 ret = uvc_ctrl_get(video, ctrl);
582 uvc_ctrl_rollback(video);
583 ctrls->error_idx = i;
587 ctrls->error_idx = 0;
588 ret = uvc_ctrl_rollback(video);
592 case VIDIOC_S_EXT_CTRLS:
593 case VIDIOC_TRY_EXT_CTRLS:
595 struct v4l2_ext_controls *ctrls = arg;
596 struct v4l2_ext_control *ctrl = ctrls->controls;
599 ret = uvc_ctrl_begin(video);
603 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
604 ret = uvc_ctrl_set(video, ctrl);
606 uvc_ctrl_rollback(video);
607 ctrls->error_idx = i;
612 ctrls->error_idx = 0;
614 if (cmd == VIDIOC_S_EXT_CTRLS)
615 ret = uvc_ctrl_commit(video);
617 ret = uvc_ctrl_rollback(video);
621 /* Get, Set & Enum input */
622 case VIDIOC_ENUMINPUT:
624 const struct uvc_entity *selector = video->selector;
625 struct v4l2_input *input = arg;
626 struct uvc_entity *iterm = NULL;
627 u32 index = input->index;
630 if (selector == NULL ||
631 (video->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
634 iterm = list_first_entry(&video->iterms,
635 struct uvc_entity, chain);
637 } else if (pin < selector->selector.bNrInPins) {
638 pin = selector->selector.baSourceID[index];
639 list_for_each_entry(iterm, video->iterms.next, chain) {
640 if (iterm->id == pin)
645 if (iterm == NULL || iterm->id != pin)
648 memset(input, 0, sizeof *input);
649 input->index = index;
650 strlcpy(input->name, iterm->name, sizeof input->name);
651 if (UVC_ENTITY_TYPE(iterm) == ITT_CAMERA)
652 input->type = V4L2_INPUT_TYPE_CAMERA;
660 if (video->selector == NULL ||
661 (video->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
666 ret = uvc_query_ctrl(video->dev, GET_CUR, video->selector->id,
667 video->dev->intfnum, SU_INPUT_SELECT_CONTROL,
672 *(int *)arg = input - 1;
678 u32 input = *(u32 *)arg + 1;
680 if ((ret = uvc_acquire_privileges(handle)) < 0)
683 if (video->selector == NULL ||
684 (video->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
690 if (input == 0 || input > video->selector->selector.bNrInPins)
693 return uvc_query_ctrl(video->dev, SET_CUR, video->selector->id,
694 video->dev->intfnum, SU_INPUT_SELECT_CONTROL,
698 /* Try, Get, Set & Enum format */
699 case VIDIOC_ENUM_FMT:
701 struct v4l2_fmtdesc *fmt = arg;
702 struct uvc_format *format;
703 enum v4l2_buf_type type = fmt->type;
704 __u32 index = fmt->index;
706 if (fmt->type != video->streaming->type ||
707 fmt->index >= video->streaming->nformats)
710 memset(fmt, 0, sizeof(*fmt));
714 format = &video->streaming->format[fmt->index];
716 if (format->flags & UVC_FMT_FLAG_COMPRESSED)
717 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
718 strlcpy(fmt->description, format->name,
719 sizeof fmt->description);
720 fmt->description[sizeof fmt->description - 1] = 0;
721 fmt->pixelformat = format->fcc;
727 struct uvc_streaming_control probe;
729 if ((ret = uvc_acquire_privileges(handle)) < 0)
732 return uvc_v4l2_try_format(video, arg, &probe, NULL, NULL);
736 if ((ret = uvc_acquire_privileges(handle)) < 0)
739 return uvc_v4l2_set_format(video, arg);
742 return uvc_v4l2_get_format(video, arg);
744 /* Frame size enumeration */
745 case VIDIOC_ENUM_FRAMESIZES:
747 struct v4l2_frmsizeenum *fsize = arg;
748 struct uvc_format *format = NULL;
749 struct uvc_frame *frame;
752 /* Look for the given pixel format */
753 for (i = 0; i < video->streaming->nformats; i++) {
754 if (video->streaming->format[i].fcc ==
755 fsize->pixel_format) {
756 format = &video->streaming->format[i];
763 if (fsize->index >= format->nframes)
766 frame = &format->frame[fsize->index];
767 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
768 fsize->discrete.width = frame->wWidth;
769 fsize->discrete.height = frame->wHeight;
773 /* Frame interval enumeration */
774 case VIDIOC_ENUM_FRAMEINTERVALS:
776 struct v4l2_frmivalenum *fival = arg;
777 struct uvc_format *format = NULL;
778 struct uvc_frame *frame = NULL;
781 /* Look for the given pixel format and frame size */
782 for (i = 0; i < video->streaming->nformats; i++) {
783 if (video->streaming->format[i].fcc ==
784 fival->pixel_format) {
785 format = &video->streaming->format[i];
792 for (i = 0; i < format->nframes; i++) {
793 if (format->frame[i].wWidth == fival->width &&
794 format->frame[i].wHeight == fival->height) {
795 frame = &format->frame[i];
802 if (frame->bFrameIntervalType) {
803 if (fival->index >= frame->bFrameIntervalType)
806 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
807 fival->discrete.numerator =
808 frame->dwFrameInterval[fival->index];
809 fival->discrete.denominator = 10000000;
810 uvc_simplify_fraction(&fival->discrete.numerator,
811 &fival->discrete.denominator, 8, 333);
813 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
814 fival->stepwise.min.numerator =
815 frame->dwFrameInterval[0];
816 fival->stepwise.min.denominator = 10000000;
817 fival->stepwise.max.numerator =
818 frame->dwFrameInterval[1];
819 fival->stepwise.max.denominator = 10000000;
820 fival->stepwise.step.numerator =
821 frame->dwFrameInterval[2];
822 fival->stepwise.step.denominator = 10000000;
823 uvc_simplify_fraction(&fival->stepwise.min.numerator,
824 &fival->stepwise.min.denominator, 8, 333);
825 uvc_simplify_fraction(&fival->stepwise.max.numerator,
826 &fival->stepwise.max.denominator, 8, 333);
827 uvc_simplify_fraction(&fival->stepwise.step.numerator,
828 &fival->stepwise.step.denominator, 8, 333);
833 /* Get & Set streaming parameters */
835 return uvc_v4l2_get_streamparm(video, arg);
838 if ((ret = uvc_acquire_privileges(handle)) < 0)
841 return uvc_v4l2_set_streamparm(video, arg);
843 /* Cropping and scaling */
846 struct v4l2_cropcap *ccap = arg;
847 struct uvc_frame *frame = video->streaming->cur_frame;
849 if (ccap->type != video->streaming->type)
852 ccap->bounds.left = 0;
853 ccap->bounds.top = 0;
854 ccap->bounds.width = frame->wWidth;
855 ccap->bounds.height = frame->wHeight;
857 ccap->defrect = ccap->bounds;
859 ccap->pixelaspect.numerator = 1;
860 ccap->pixelaspect.denominator = 1;
868 /* Buffers & streaming */
871 struct v4l2_requestbuffers *rb = arg;
872 unsigned int bufsize =
873 video->streaming->ctrl.dwMaxVideoFrameSize;
875 if (rb->type != video->streaming->type ||
876 rb->memory != V4L2_MEMORY_MMAP)
879 if ((ret = uvc_acquire_privileges(handle)) < 0)
882 ret = uvc_alloc_buffers(&video->queue, rb->count, bufsize);
891 case VIDIOC_QUERYBUF:
893 struct v4l2_buffer *buf = arg;
895 if (buf->type != video->streaming->type)
898 if (!uvc_has_privileges(handle))
901 return uvc_query_buffer(&video->queue, buf);
905 if (!uvc_has_privileges(handle))
908 return uvc_queue_buffer(&video->queue, arg);
911 if (!uvc_has_privileges(handle))
914 return uvc_dequeue_buffer(&video->queue, arg,
915 file->f_flags & O_NONBLOCK);
917 case VIDIOC_STREAMON:
921 if (*type != video->streaming->type)
924 if (!uvc_has_privileges(handle))
927 if ((ret = uvc_video_enable(video, 1)) < 0)
932 case VIDIOC_STREAMOFF:
936 if (*type != video->streaming->type)
939 if (!uvc_has_privileges(handle))
942 return uvc_video_enable(video, 0);
945 /* Analog video standards make no sense for digital cameras. */
947 case VIDIOC_QUERYSTD:
953 case VIDIOC_ENUMAUDIO:
954 case VIDIOC_ENUMAUDOUT:
956 case VIDIOC_ENUMOUTPUT:
957 uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd);
960 /* Dynamic controls. */
961 case UVCIOC_CTRL_ADD:
963 struct uvc_xu_control_info *xinfo = arg;
964 struct uvc_control_info *info;
966 if (!capable(CAP_SYS_ADMIN))
969 info = kzalloc(sizeof *info, GFP_KERNEL);
973 memcpy(info->entity, xinfo->entity, sizeof info->entity);
974 info->index = xinfo->index;
975 info->selector = xinfo->selector;
976 info->size = xinfo->size;
977 info->flags = xinfo->flags;
979 info->flags |= UVC_CONTROL_GET_MIN | UVC_CONTROL_GET_MAX |
980 UVC_CONTROL_GET_RES | UVC_CONTROL_GET_DEF;
982 ret = uvc_ctrl_add_info(info);
988 case UVCIOC_CTRL_MAP:
990 struct uvc_xu_control_mapping *xmap = arg;
991 struct uvc_control_mapping *map;
993 if (!capable(CAP_SYS_ADMIN))
996 map = kzalloc(sizeof *map, GFP_KERNEL);
1001 memcpy(map->name, xmap->name, sizeof map->name);
1002 memcpy(map->entity, xmap->entity, sizeof map->entity);
1003 map->selector = xmap->selector;
1004 map->size = xmap->size;
1005 map->offset = xmap->offset;
1006 map->v4l2_type = xmap->v4l2_type;
1007 map->data_type = xmap->data_type;
1009 ret = uvc_ctrl_add_mapping(map);
1015 case UVCIOC_CTRL_GET:
1016 return uvc_xu_ctrl_query(video, arg, 0);
1018 case UVCIOC_CTRL_SET:
1019 return uvc_xu_ctrl_query(video, arg, 1);
1022 if ((ret = v4l_compat_translate_ioctl(file, cmd, arg,
1023 uvc_v4l2_do_ioctl)) == -ENOIOCTLCMD)
1024 uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n",
1032 static long uvc_v4l2_ioctl(struct file *file,
1033 unsigned int cmd, unsigned long arg)
1035 if (uvc_trace_param & UVC_TRACE_IOCTL) {
1036 uvc_printk(KERN_DEBUG, "uvc_v4l2_ioctl(");
1037 v4l_printk_ioctl(cmd);
1041 return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl);
1044 static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1045 size_t count, loff_t *ppos)
1047 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
1054 static void uvc_vm_open(struct vm_area_struct *vma)
1056 struct uvc_buffer *buffer = vma->vm_private_data;
1057 buffer->vma_use_count++;
1060 static void uvc_vm_close(struct vm_area_struct *vma)
1062 struct uvc_buffer *buffer = vma->vm_private_data;
1063 buffer->vma_use_count--;
1066 static struct vm_operations_struct uvc_vm_ops = {
1067 .open = uvc_vm_open,
1068 .close = uvc_vm_close,
1071 static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1073 struct uvc_video_device *video = video_drvdata(file);
1074 struct uvc_buffer *uninitialized_var(buffer);
1076 unsigned long addr, start, size;
1080 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
1082 start = vma->vm_start;
1083 size = vma->vm_end - vma->vm_start;
1085 mutex_lock(&video->queue.mutex);
1087 for (i = 0; i < video->queue.count; ++i) {
1088 buffer = &video->queue.buffer[i];
1089 if ((buffer->buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
1093 if (i == video->queue.count || size != video->queue.buf_size) {
1099 * VM_IO marks the area as being an mmaped region for I/O to a
1100 * device. It also prevents the region from being core dumped.
1102 vma->vm_flags |= VM_IO;
1104 addr = (unsigned long)video->queue.mem + buffer->buf.m.offset;
1106 page = vmalloc_to_page((void *)addr);
1107 if ((ret = vm_insert_page(vma, start, page)) < 0)
1115 vma->vm_ops = &uvc_vm_ops;
1116 vma->vm_private_data = buffer;
1120 mutex_unlock(&video->queue.mutex);
1124 static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
1126 struct uvc_video_device *video = video_drvdata(file);
1128 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
1130 return uvc_queue_poll(&video->queue, file, wait);
1133 const struct v4l2_file_operations uvc_fops = {
1134 .owner = THIS_MODULE,
1135 .open = uvc_v4l2_open,
1136 .release = uvc_v4l2_release,
1137 .ioctl = uvc_v4l2_ioctl,
1138 .read = uvc_v4l2_read,
1139 .mmap = uvc_v4l2_mmap,
1140 .poll = uvc_v4l2_poll,