2 * uvc_v4l2.c -- USB Video Class driver - V4L2 API
4 * Copyright (C) 2005-2010
5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
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/slab.h>
19 #include <linux/usb.h>
20 #include <linux/videodev2.h>
21 #include <linux/vmalloc.h>
23 #include <linux/wait.h>
24 #include <linux/atomic.h>
26 #include <media/v4l2-common.h>
27 #include <media/v4l2-ioctl.h>
31 /* ------------------------------------------------------------------------
34 static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain,
35 struct uvc_xu_control_mapping *xmap)
37 struct uvc_control_mapping *map;
41 map = kzalloc(sizeof *map, GFP_KERNEL);
46 memcpy(map->name, xmap->name, sizeof map->name);
47 memcpy(map->entity, xmap->entity, sizeof map->entity);
48 map->selector = xmap->selector;
49 map->size = xmap->size;
50 map->offset = xmap->offset;
51 map->v4l2_type = xmap->v4l2_type;
52 map->data_type = xmap->data_type;
54 switch (xmap->v4l2_type) {
55 case V4L2_CTRL_TYPE_INTEGER:
56 case V4L2_CTRL_TYPE_BOOLEAN:
57 case V4L2_CTRL_TYPE_BUTTON:
60 case V4L2_CTRL_TYPE_MENU:
61 size = xmap->menu_count * sizeof(*map->menu_info);
62 map->menu_info = kmalloc(size, GFP_KERNEL);
63 if (map->menu_info == NULL) {
68 if (copy_from_user(map->menu_info, xmap->menu_info, size)) {
73 map->menu_count = xmap->menu_count;
77 uvc_trace(UVC_TRACE_CONTROL, "Unsupported V4L2 control type "
78 "%u.\n", xmap->v4l2_type);
83 ret = uvc_ctrl_add_mapping(chain, map);
86 kfree(map->menu_info);
92 /* ------------------------------------------------------------------------
97 * Find the frame interval closest to the requested frame interval for the
98 * given frame format and size. This should be done by the device as part of
99 * the Video Probe and Commit negotiation, but some hardware don't implement
102 static __u32 uvc_try_frame_interval(struct uvc_frame *frame, __u32 interval)
106 if (frame->bFrameIntervalType) {
107 __u32 best = -1, dist;
109 for (i = 0; i < frame->bFrameIntervalType; ++i) {
110 dist = interval > frame->dwFrameInterval[i]
111 ? interval - frame->dwFrameInterval[i]
112 : frame->dwFrameInterval[i] - interval;
120 interval = frame->dwFrameInterval[i-1];
122 const __u32 min = frame->dwFrameInterval[0];
123 const __u32 max = frame->dwFrameInterval[1];
124 const __u32 step = frame->dwFrameInterval[2];
126 interval = min + (interval - min + step/2) / step * step;
134 static int uvc_v4l2_try_format(struct uvc_streaming *stream,
135 struct v4l2_format *fmt, struct uvc_streaming_control *probe,
136 struct uvc_format **uvc_format, struct uvc_frame **uvc_frame)
138 struct uvc_format *format = NULL;
139 struct uvc_frame *frame = NULL;
141 unsigned int d, maxd;
147 if (fmt->type != stream->type)
150 fcc = (__u8 *)&fmt->fmt.pix.pixelformat;
151 uvc_trace(UVC_TRACE_FORMAT, "Trying format 0x%08x (%c%c%c%c): %ux%u.\n",
152 fmt->fmt.pix.pixelformat,
153 fcc[0], fcc[1], fcc[2], fcc[3],
154 fmt->fmt.pix.width, fmt->fmt.pix.height);
156 /* Check if the hardware supports the requested format. */
157 for (i = 0; i < stream->nformats; ++i) {
158 format = &stream->format[i];
159 if (format->fcc == fmt->fmt.pix.pixelformat)
163 if (format == NULL || format->fcc != fmt->fmt.pix.pixelformat) {
164 uvc_trace(UVC_TRACE_FORMAT, "Unsupported format 0x%08x.\n",
165 fmt->fmt.pix.pixelformat);
169 /* Find the closest image size. The distance between image sizes is
170 * the size in pixels of the non-overlapping regions between the
171 * requested size and the frame-specified size.
173 rw = fmt->fmt.pix.width;
174 rh = fmt->fmt.pix.height;
175 maxd = (unsigned int)-1;
177 for (i = 0; i < format->nframes; ++i) {
178 __u16 w = format->frame[i].wWidth;
179 __u16 h = format->frame[i].wHeight;
181 d = min(w, rw) * min(h, rh);
182 d = w*h + rw*rh - 2*d;
185 frame = &format->frame[i];
193 uvc_trace(UVC_TRACE_FORMAT, "Unsupported size %ux%u.\n",
194 fmt->fmt.pix.width, fmt->fmt.pix.height);
198 /* Use the default frame interval. */
199 interval = frame->dwDefaultFrameInterval;
200 uvc_trace(UVC_TRACE_FORMAT, "Using default frame interval %u.%u us "
201 "(%u.%u fps).\n", interval/10, interval%10, 10000000/interval,
202 (100000000/interval)%10);
204 /* Set the format index, frame index and frame interval. */
205 memset(probe, 0, sizeof *probe);
206 probe->bmHint = 1; /* dwFrameInterval */
207 probe->bFormatIndex = format->index;
208 probe->bFrameIndex = frame->bFrameIndex;
209 probe->dwFrameInterval = uvc_try_frame_interval(frame, interval);
210 /* Some webcams stall the probe control set request when the
211 * dwMaxVideoFrameSize field is set to zero. The UVC specification
212 * clearly states that the field is read-only from the host, so this
213 * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by
214 * the webcam to work around the problem.
216 * The workaround could probably be enabled for all webcams, so the
217 * quirk can be removed if needed. It's currently useful to detect
218 * webcam bugs and fix them before they hit the market (providing
219 * developers test their webcams with the Linux driver as well as with
220 * the Windows driver).
222 mutex_lock(&stream->mutex);
223 if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS)
224 probe->dwMaxVideoFrameSize =
225 stream->ctrl.dwMaxVideoFrameSize;
227 /* Probe the device. */
228 ret = uvc_probe_video(stream, probe);
229 mutex_unlock(&stream->mutex);
233 fmt->fmt.pix.width = frame->wWidth;
234 fmt->fmt.pix.height = frame->wHeight;
235 fmt->fmt.pix.field = V4L2_FIELD_NONE;
236 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
237 fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize;
238 fmt->fmt.pix.colorspace = format->colorspace;
239 fmt->fmt.pix.priv = 0;
241 if (uvc_format != NULL)
242 *uvc_format = format;
243 if (uvc_frame != NULL)
250 static int uvc_v4l2_get_format(struct uvc_streaming *stream,
251 struct v4l2_format *fmt)
253 struct uvc_format *format;
254 struct uvc_frame *frame;
257 if (fmt->type != stream->type)
260 mutex_lock(&stream->mutex);
261 format = stream->cur_format;
262 frame = stream->cur_frame;
264 if (format == NULL || frame == NULL) {
269 fmt->fmt.pix.pixelformat = format->fcc;
270 fmt->fmt.pix.width = frame->wWidth;
271 fmt->fmt.pix.height = frame->wHeight;
272 fmt->fmt.pix.field = V4L2_FIELD_NONE;
273 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
274 fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize;
275 fmt->fmt.pix.colorspace = format->colorspace;
276 fmt->fmt.pix.priv = 0;
279 mutex_unlock(&stream->mutex);
283 static int uvc_v4l2_set_format(struct uvc_streaming *stream,
284 struct v4l2_format *fmt)
286 struct uvc_streaming_control probe;
287 struct uvc_format *format;
288 struct uvc_frame *frame;
291 if (fmt->type != stream->type)
294 ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame);
298 mutex_lock(&stream->mutex);
300 if (uvc_queue_allocated(&stream->queue)) {
305 memcpy(&stream->ctrl, &probe, sizeof probe);
306 stream->cur_format = format;
307 stream->cur_frame = frame;
310 mutex_unlock(&stream->mutex);
314 static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream,
315 struct v4l2_streamparm *parm)
317 uint32_t numerator, denominator;
319 if (parm->type != stream->type)
322 mutex_lock(&stream->mutex);
323 numerator = stream->ctrl.dwFrameInterval;
324 mutex_unlock(&stream->mutex);
326 denominator = 10000000;
327 uvc_simplify_fraction(&numerator, &denominator, 8, 333);
329 memset(parm, 0, sizeof *parm);
330 parm->type = stream->type;
332 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
333 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
334 parm->parm.capture.capturemode = 0;
335 parm->parm.capture.timeperframe.numerator = numerator;
336 parm->parm.capture.timeperframe.denominator = denominator;
337 parm->parm.capture.extendedmode = 0;
338 parm->parm.capture.readbuffers = 0;
340 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
341 parm->parm.output.outputmode = 0;
342 parm->parm.output.timeperframe.numerator = numerator;
343 parm->parm.output.timeperframe.denominator = denominator;
349 static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
350 struct v4l2_streamparm *parm)
352 struct uvc_streaming_control probe;
353 struct v4l2_fract timeperframe;
357 if (parm->type != stream->type)
360 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
361 timeperframe = parm->parm.capture.timeperframe;
363 timeperframe = parm->parm.output.timeperframe;
365 interval = uvc_fraction_to_interval(timeperframe.numerator,
366 timeperframe.denominator);
367 uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
368 timeperframe.numerator, timeperframe.denominator, interval);
370 mutex_lock(&stream->mutex);
372 if (uvc_queue_streaming(&stream->queue)) {
373 mutex_unlock(&stream->mutex);
377 memcpy(&probe, &stream->ctrl, sizeof probe);
378 probe.dwFrameInterval =
379 uvc_try_frame_interval(stream->cur_frame, interval);
381 /* Probe the device with the new settings. */
382 ret = uvc_probe_video(stream, &probe);
384 mutex_unlock(&stream->mutex);
388 memcpy(&stream->ctrl, &probe, sizeof probe);
389 mutex_unlock(&stream->mutex);
391 /* Return the actual frame period. */
392 timeperframe.numerator = probe.dwFrameInterval;
393 timeperframe.denominator = 10000000;
394 uvc_simplify_fraction(&timeperframe.numerator,
395 &timeperframe.denominator, 8, 333);
397 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
398 parm->parm.capture.timeperframe = timeperframe;
400 parm->parm.output.timeperframe = timeperframe;
405 /* ------------------------------------------------------------------------
406 * Privilege management
410 * Privilege management is the multiple-open implementation basis. The current
411 * implementation is completely transparent for the end-user and doesn't
412 * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls.
413 * Those ioctls enable finer control on the device (by making possible for a
414 * user to request exclusive access to a device), but are not mature yet.
415 * Switching to the V4L2 priority mechanism might be considered in the future
416 * if this situation changes.
418 * Each open instance of a UVC device can either be in a privileged or
419 * unprivileged state. Only a single instance can be in a privileged state at
420 * a given time. Trying to perform an operation that requires privileges will
421 * automatically acquire the required privileges if possible, or return -EBUSY
422 * otherwise. Privileges are dismissed when closing the instance or when
423 * freeing the video buffers using VIDIOC_REQBUFS.
425 * Operations that require privileges are:
432 static int uvc_acquire_privileges(struct uvc_fh *handle)
434 /* Always succeed if the handle is already privileged. */
435 if (handle->state == UVC_HANDLE_ACTIVE)
438 /* Check if the device already has a privileged handle. */
439 if (atomic_inc_return(&handle->stream->active) != 1) {
440 atomic_dec(&handle->stream->active);
444 handle->state = UVC_HANDLE_ACTIVE;
448 static void uvc_dismiss_privileges(struct uvc_fh *handle)
450 if (handle->state == UVC_HANDLE_ACTIVE)
451 atomic_dec(&handle->stream->active);
453 handle->state = UVC_HANDLE_PASSIVE;
456 static int uvc_has_privileges(struct uvc_fh *handle)
458 return handle->state == UVC_HANDLE_ACTIVE;
461 /* ------------------------------------------------------------------------
462 * V4L2 file operations
465 static int uvc_v4l2_open(struct file *file)
467 struct uvc_streaming *stream;
468 struct uvc_fh *handle;
471 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
472 stream = video_drvdata(file);
474 if (stream->dev->state & UVC_DEV_DISCONNECTED)
477 ret = usb_autopm_get_interface(stream->dev->intf);
481 /* Create the device handle. */
482 handle = kzalloc(sizeof *handle, GFP_KERNEL);
483 if (handle == NULL) {
484 usb_autopm_put_interface(stream->dev->intf);
488 if (atomic_inc_return(&stream->dev->users) == 1) {
489 ret = uvc_status_start(stream->dev);
491 usb_autopm_put_interface(stream->dev->intf);
492 atomic_dec(&stream->dev->users);
498 handle->chain = stream->chain;
499 handle->stream = stream;
500 handle->state = UVC_HANDLE_PASSIVE;
501 file->private_data = handle;
506 static int uvc_v4l2_release(struct file *file)
508 struct uvc_fh *handle = file->private_data;
509 struct uvc_streaming *stream = handle->stream;
511 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");
513 /* Only free resources if this is a privileged handle. */
514 if (uvc_has_privileges(handle)) {
515 uvc_video_enable(stream, 0);
517 if (uvc_free_buffers(&stream->queue) < 0)
518 uvc_printk(KERN_ERR, "uvc_v4l2_release: Unable to "
522 /* Release the file handle. */
523 uvc_dismiss_privileges(handle);
525 file->private_data = NULL;
527 if (atomic_dec_return(&stream->dev->users) == 0)
528 uvc_status_stop(stream->dev);
530 usb_autopm_put_interface(stream->dev->intf);
534 static long uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
536 struct video_device *vdev = video_devdata(file);
537 struct uvc_fh *handle = file->private_data;
538 struct uvc_video_chain *chain = handle->chain;
539 struct uvc_streaming *stream = handle->stream;
543 /* Query capabilities */
544 case VIDIOC_QUERYCAP:
546 struct v4l2_capability *cap = arg;
548 memset(cap, 0, sizeof *cap);
549 strlcpy(cap->driver, "uvcvideo", sizeof cap->driver);
550 strlcpy(cap->card, vdev->name, sizeof cap->card);
551 usb_make_path(stream->dev->udev,
552 cap->bus_info, sizeof(cap->bus_info));
553 cap->version = LINUX_VERSION_CODE;
554 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
555 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
556 | V4L2_CAP_STREAMING;
558 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT
559 | V4L2_CAP_STREAMING;
563 /* Get, Set & Query control */
564 case VIDIOC_QUERYCTRL:
565 return uvc_query_v4l2_ctrl(chain, arg);
569 struct v4l2_control *ctrl = arg;
570 struct v4l2_ext_control xctrl;
572 memset(&xctrl, 0, sizeof xctrl);
575 ret = uvc_ctrl_begin(chain);
579 ret = uvc_ctrl_get(chain, &xctrl);
580 uvc_ctrl_rollback(chain);
582 ctrl->value = xctrl.value;
588 struct v4l2_control *ctrl = arg;
589 struct v4l2_ext_control xctrl;
591 memset(&xctrl, 0, sizeof xctrl);
593 xctrl.value = ctrl->value;
595 ret = uvc_ctrl_begin(chain);
599 ret = uvc_ctrl_set(chain, &xctrl);
601 uvc_ctrl_rollback(chain);
604 ret = uvc_ctrl_commit(chain);
606 ctrl->value = xctrl.value;
610 case VIDIOC_QUERYMENU:
611 return uvc_query_v4l2_menu(chain, arg);
613 case VIDIOC_G_EXT_CTRLS:
615 struct v4l2_ext_controls *ctrls = arg;
616 struct v4l2_ext_control *ctrl = ctrls->controls;
619 ret = uvc_ctrl_begin(chain);
623 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
624 ret = uvc_ctrl_get(chain, ctrl);
626 uvc_ctrl_rollback(chain);
627 ctrls->error_idx = i;
631 ctrls->error_idx = 0;
632 ret = uvc_ctrl_rollback(chain);
636 case VIDIOC_S_EXT_CTRLS:
637 case VIDIOC_TRY_EXT_CTRLS:
639 struct v4l2_ext_controls *ctrls = arg;
640 struct v4l2_ext_control *ctrl = ctrls->controls;
643 ret = uvc_ctrl_begin(chain);
647 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
648 ret = uvc_ctrl_set(chain, ctrl);
650 uvc_ctrl_rollback(chain);
651 ctrls->error_idx = i;
656 ctrls->error_idx = 0;
658 if (cmd == VIDIOC_S_EXT_CTRLS)
659 ret = uvc_ctrl_commit(chain);
661 ret = uvc_ctrl_rollback(chain);
665 /* Get, Set & Enum input */
666 case VIDIOC_ENUMINPUT:
668 const struct uvc_entity *selector = chain->selector;
669 struct v4l2_input *input = arg;
670 struct uvc_entity *iterm = NULL;
671 u32 index = input->index;
674 if (selector == NULL ||
675 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
678 list_for_each_entry(iterm, &chain->entities, chain) {
679 if (UVC_ENTITY_IS_ITERM(iterm))
683 } else if (pin < selector->bNrInPins) {
684 pin = selector->baSourceID[index];
685 list_for_each_entry(iterm, &chain->entities, chain) {
686 if (!UVC_ENTITY_IS_ITERM(iterm))
688 if (iterm->id == pin)
693 if (iterm == NULL || iterm->id != pin)
696 memset(input, 0, sizeof *input);
697 input->index = index;
698 strlcpy(input->name, iterm->name, sizeof input->name);
699 if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
700 input->type = V4L2_INPUT_TYPE_CAMERA;
708 if (chain->selector == NULL ||
709 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
714 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
715 chain->selector->id, chain->dev->intfnum,
716 UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
720 *(int *)arg = input - 1;
726 u32 input = *(u32 *)arg + 1;
728 if ((ret = uvc_acquire_privileges(handle)) < 0)
731 if (chain->selector == NULL ||
732 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
738 if (input == 0 || input > chain->selector->bNrInPins)
741 return uvc_query_ctrl(chain->dev, UVC_SET_CUR,
742 chain->selector->id, chain->dev->intfnum,
743 UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
746 /* Try, Get, Set & Enum format */
747 case VIDIOC_ENUM_FMT:
749 struct v4l2_fmtdesc *fmt = arg;
750 struct uvc_format *format;
751 enum v4l2_buf_type type = fmt->type;
752 __u32 index = fmt->index;
754 if (fmt->type != stream->type ||
755 fmt->index >= stream->nformats)
758 memset(fmt, 0, sizeof(*fmt));
762 format = &stream->format[fmt->index];
764 if (format->flags & UVC_FMT_FLAG_COMPRESSED)
765 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
766 strlcpy(fmt->description, format->name,
767 sizeof fmt->description);
768 fmt->description[sizeof fmt->description - 1] = 0;
769 fmt->pixelformat = format->fcc;
775 struct uvc_streaming_control probe;
777 return uvc_v4l2_try_format(stream, arg, &probe, NULL, NULL);
781 if ((ret = uvc_acquire_privileges(handle)) < 0)
784 return uvc_v4l2_set_format(stream, arg);
787 return uvc_v4l2_get_format(stream, arg);
789 /* Frame size enumeration */
790 case VIDIOC_ENUM_FRAMESIZES:
792 struct v4l2_frmsizeenum *fsize = arg;
793 struct uvc_format *format = NULL;
794 struct uvc_frame *frame;
797 /* Look for the given pixel format */
798 for (i = 0; i < stream->nformats; i++) {
799 if (stream->format[i].fcc ==
800 fsize->pixel_format) {
801 format = &stream->format[i];
808 if (fsize->index >= format->nframes)
811 frame = &format->frame[fsize->index];
812 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
813 fsize->discrete.width = frame->wWidth;
814 fsize->discrete.height = frame->wHeight;
818 /* Frame interval enumeration */
819 case VIDIOC_ENUM_FRAMEINTERVALS:
821 struct v4l2_frmivalenum *fival = arg;
822 struct uvc_format *format = NULL;
823 struct uvc_frame *frame = NULL;
826 /* Look for the given pixel format and frame size */
827 for (i = 0; i < stream->nformats; i++) {
828 if (stream->format[i].fcc ==
829 fival->pixel_format) {
830 format = &stream->format[i];
837 for (i = 0; i < format->nframes; i++) {
838 if (format->frame[i].wWidth == fival->width &&
839 format->frame[i].wHeight == fival->height) {
840 frame = &format->frame[i];
847 if (frame->bFrameIntervalType) {
848 if (fival->index >= frame->bFrameIntervalType)
851 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
852 fival->discrete.numerator =
853 frame->dwFrameInterval[fival->index];
854 fival->discrete.denominator = 10000000;
855 uvc_simplify_fraction(&fival->discrete.numerator,
856 &fival->discrete.denominator, 8, 333);
858 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
859 fival->stepwise.min.numerator =
860 frame->dwFrameInterval[0];
861 fival->stepwise.min.denominator = 10000000;
862 fival->stepwise.max.numerator =
863 frame->dwFrameInterval[1];
864 fival->stepwise.max.denominator = 10000000;
865 fival->stepwise.step.numerator =
866 frame->dwFrameInterval[2];
867 fival->stepwise.step.denominator = 10000000;
868 uvc_simplify_fraction(&fival->stepwise.min.numerator,
869 &fival->stepwise.min.denominator, 8, 333);
870 uvc_simplify_fraction(&fival->stepwise.max.numerator,
871 &fival->stepwise.max.denominator, 8, 333);
872 uvc_simplify_fraction(&fival->stepwise.step.numerator,
873 &fival->stepwise.step.denominator, 8, 333);
878 /* Get & Set streaming parameters */
880 return uvc_v4l2_get_streamparm(stream, arg);
883 if ((ret = uvc_acquire_privileges(handle)) < 0)
886 return uvc_v4l2_set_streamparm(stream, arg);
888 /* Cropping and scaling */
891 struct v4l2_cropcap *ccap = arg;
893 if (ccap->type != stream->type)
896 ccap->bounds.left = 0;
897 ccap->bounds.top = 0;
899 mutex_lock(&stream->mutex);
900 ccap->bounds.width = stream->cur_frame->wWidth;
901 ccap->bounds.height = stream->cur_frame->wHeight;
902 mutex_unlock(&stream->mutex);
904 ccap->defrect = ccap->bounds;
906 ccap->pixelaspect.numerator = 1;
907 ccap->pixelaspect.denominator = 1;
915 /* Buffers & streaming */
918 struct v4l2_requestbuffers *rb = arg;
920 if (rb->type != stream->type ||
921 rb->memory != V4L2_MEMORY_MMAP)
924 if ((ret = uvc_acquire_privileges(handle)) < 0)
927 mutex_lock(&stream->mutex);
928 ret = uvc_alloc_buffers(&stream->queue, rb->count,
929 stream->ctrl.dwMaxVideoFrameSize);
930 mutex_unlock(&stream->mutex);
935 uvc_dismiss_privileges(handle);
942 case VIDIOC_QUERYBUF:
944 struct v4l2_buffer *buf = arg;
946 if (buf->type != stream->type)
949 if (!uvc_has_privileges(handle))
952 return uvc_query_buffer(&stream->queue, buf);
956 if (!uvc_has_privileges(handle))
959 return uvc_queue_buffer(&stream->queue, arg);
962 if (!uvc_has_privileges(handle))
965 return uvc_dequeue_buffer(&stream->queue, arg,
966 file->f_flags & O_NONBLOCK);
968 case VIDIOC_STREAMON:
972 if (*type != stream->type)
975 if (!uvc_has_privileges(handle))
978 mutex_lock(&stream->mutex);
979 ret = uvc_video_enable(stream, 1);
980 mutex_unlock(&stream->mutex);
986 case VIDIOC_STREAMOFF:
990 if (*type != stream->type)
993 if (!uvc_has_privileges(handle))
996 return uvc_video_enable(stream, 0);
999 /* Analog video standards make no sense for digital cameras. */
1000 case VIDIOC_ENUMSTD:
1001 case VIDIOC_QUERYSTD:
1005 case VIDIOC_OVERLAY:
1007 case VIDIOC_ENUMAUDIO:
1008 case VIDIOC_ENUMAUDOUT:
1010 case VIDIOC_ENUMOUTPUT:
1011 uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd);
1014 case UVCIOC_CTRL_MAP:
1015 return uvc_ioctl_ctrl_map(chain, arg);
1017 case UVCIOC_CTRL_QUERY:
1018 return uvc_xu_ctrl_query(chain, arg);
1021 uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n", cmd);
1028 static long uvc_v4l2_ioctl(struct file *file,
1029 unsigned int cmd, unsigned long arg)
1031 if (uvc_trace_param & UVC_TRACE_IOCTL) {
1032 uvc_printk(KERN_DEBUG, "uvc_v4l2_ioctl(");
1033 v4l_printk_ioctl(cmd);
1037 return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl);
1040 static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1041 size_t count, loff_t *ppos)
1043 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
1047 static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1049 struct uvc_fh *handle = file->private_data;
1050 struct uvc_streaming *stream = handle->stream;
1052 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
1054 return uvc_queue_mmap(&stream->queue, vma);
1057 static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
1059 struct uvc_fh *handle = file->private_data;
1060 struct uvc_streaming *stream = handle->stream;
1062 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
1064 return uvc_queue_poll(&stream->queue, file, wait);
1068 static unsigned long uvc_v4l2_get_unmapped_area(struct file *file,
1069 unsigned long addr, unsigned long len, unsigned long pgoff,
1070 unsigned long flags)
1072 struct uvc_fh *handle = file->private_data;
1073 struct uvc_streaming *stream = handle->stream;
1075 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_get_unmapped_area\n");
1077 return uvc_queue_get_unmapped_area(&stream->queue, pgoff);
1081 const struct v4l2_file_operations uvc_fops = {
1082 .owner = THIS_MODULE,
1083 .open = uvc_v4l2_open,
1084 .release = uvc_v4l2_release,
1085 .unlocked_ioctl = uvc_v4l2_ioctl,
1086 .read = uvc_v4l2_read,
1087 .mmap = uvc_v4l2_mmap,
1088 .poll = uvc_v4l2_poll,
1090 .get_unmapped_area = uvc_v4l2_get_unmapped_area,