2 * uvc_v4l2.c -- USB Video Class driver - V4L2 API
4 * Copyright (C) 2005-2010
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 <asm/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, int old)
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:
62 uvc_trace(UVC_TRACE_CONTROL, "V4L2_CTRL_TYPE_MENU not "
63 "supported for UVCIOC_CTRL_MAP_OLD.\n");
68 size = xmap->menu_count * sizeof(*map->menu_info);
69 map->menu_info = kmalloc(size, GFP_KERNEL);
70 if (map->menu_info == NULL) {
75 if (copy_from_user(map->menu_info, xmap->menu_info, size)) {
80 map->menu_count = xmap->menu_count;
84 uvc_trace(UVC_TRACE_CONTROL, "Unsupported V4L2 control type "
85 "%u.\n", xmap->v4l2_type);
90 ret = uvc_ctrl_add_mapping(chain, map);
93 kfree(map->menu_info);
99 /* ------------------------------------------------------------------------
104 * Find the frame interval closest to the requested frame interval for the
105 * given frame format and size. This should be done by the device as part of
106 * the Video Probe and Commit negotiation, but some hardware don't implement
109 static __u32 uvc_try_frame_interval(struct uvc_frame *frame, __u32 interval)
113 if (frame->bFrameIntervalType) {
114 __u32 best = -1, dist;
116 for (i = 0; i < frame->bFrameIntervalType; ++i) {
117 dist = interval > frame->dwFrameInterval[i]
118 ? interval - frame->dwFrameInterval[i]
119 : frame->dwFrameInterval[i] - interval;
127 interval = frame->dwFrameInterval[i-1];
129 const __u32 min = frame->dwFrameInterval[0];
130 const __u32 max = frame->dwFrameInterval[1];
131 const __u32 step = frame->dwFrameInterval[2];
133 interval = min + (interval - min + step/2) / step * step;
141 static int uvc_v4l2_try_format(struct uvc_streaming *stream,
142 struct v4l2_format *fmt, struct uvc_streaming_control *probe,
143 struct uvc_format **uvc_format, struct uvc_frame **uvc_frame)
145 struct uvc_format *format = NULL;
146 struct uvc_frame *frame = NULL;
148 unsigned int d, maxd;
154 if (fmt->type != stream->type)
157 fcc = (__u8 *)&fmt->fmt.pix.pixelformat;
158 uvc_trace(UVC_TRACE_FORMAT, "Trying format 0x%08x (%c%c%c%c): %ux%u.\n",
159 fmt->fmt.pix.pixelformat,
160 fcc[0], fcc[1], fcc[2], fcc[3],
161 fmt->fmt.pix.width, fmt->fmt.pix.height);
163 /* Check if the hardware supports the requested format. */
164 for (i = 0; i < stream->nformats; ++i) {
165 format = &stream->format[i];
166 if (format->fcc == fmt->fmt.pix.pixelformat)
170 if (format == NULL || format->fcc != fmt->fmt.pix.pixelformat) {
171 uvc_trace(UVC_TRACE_FORMAT, "Unsupported format 0x%08x.\n",
172 fmt->fmt.pix.pixelformat);
176 /* Find the closest image size. The distance between image sizes is
177 * the size in pixels of the non-overlapping regions between the
178 * requested size and the frame-specified size.
180 rw = fmt->fmt.pix.width;
181 rh = fmt->fmt.pix.height;
182 maxd = (unsigned int)-1;
184 for (i = 0; i < format->nframes; ++i) {
185 __u16 w = format->frame[i].wWidth;
186 __u16 h = format->frame[i].wHeight;
188 d = min(w, rw) * min(h, rh);
189 d = w*h + rw*rh - 2*d;
192 frame = &format->frame[i];
200 uvc_trace(UVC_TRACE_FORMAT, "Unsupported size %ux%u.\n",
201 fmt->fmt.pix.width, fmt->fmt.pix.height);
205 /* Use the default frame interval. */
206 interval = frame->dwDefaultFrameInterval;
207 uvc_trace(UVC_TRACE_FORMAT, "Using default frame interval %u.%u us "
208 "(%u.%u fps).\n", interval/10, interval%10, 10000000/interval,
209 (100000000/interval)%10);
211 /* Set the format index, frame index and frame interval. */
212 memset(probe, 0, sizeof *probe);
213 probe->bmHint = 1; /* dwFrameInterval */
214 probe->bFormatIndex = format->index;
215 probe->bFrameIndex = frame->bFrameIndex;
216 probe->dwFrameInterval = uvc_try_frame_interval(frame, interval);
217 /* Some webcams stall the probe control set request when the
218 * dwMaxVideoFrameSize field is set to zero. The UVC specification
219 * clearly states that the field is read-only from the host, so this
220 * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by
221 * the webcam to work around the problem.
223 * The workaround could probably be enabled for all webcams, so the
224 * quirk can be removed if needed. It's currently useful to detect
225 * webcam bugs and fix them before they hit the market (providing
226 * developers test their webcams with the Linux driver as well as with
227 * the Windows driver).
229 mutex_lock(&stream->mutex);
230 if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS)
231 probe->dwMaxVideoFrameSize =
232 stream->ctrl.dwMaxVideoFrameSize;
234 /* Probe the device. */
235 ret = uvc_probe_video(stream, probe);
236 mutex_unlock(&stream->mutex);
240 fmt->fmt.pix.width = frame->wWidth;
241 fmt->fmt.pix.height = frame->wHeight;
242 fmt->fmt.pix.field = V4L2_FIELD_NONE;
243 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
244 fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize;
245 fmt->fmt.pix.colorspace = format->colorspace;
246 fmt->fmt.pix.priv = 0;
248 if (uvc_format != NULL)
249 *uvc_format = format;
250 if (uvc_frame != NULL)
257 static int uvc_v4l2_get_format(struct uvc_streaming *stream,
258 struct v4l2_format *fmt)
260 struct uvc_format *format;
261 struct uvc_frame *frame;
264 if (fmt->type != stream->type)
267 mutex_lock(&stream->mutex);
268 format = stream->cur_format;
269 frame = stream->cur_frame;
271 if (format == NULL || frame == NULL) {
276 fmt->fmt.pix.pixelformat = format->fcc;
277 fmt->fmt.pix.width = frame->wWidth;
278 fmt->fmt.pix.height = frame->wHeight;
279 fmt->fmt.pix.field = V4L2_FIELD_NONE;
280 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
281 fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize;
282 fmt->fmt.pix.colorspace = format->colorspace;
283 fmt->fmt.pix.priv = 0;
286 mutex_unlock(&stream->mutex);
290 static int uvc_v4l2_set_format(struct uvc_streaming *stream,
291 struct v4l2_format *fmt)
293 struct uvc_streaming_control probe;
294 struct uvc_format *format;
295 struct uvc_frame *frame;
298 if (fmt->type != stream->type)
301 ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame);
305 mutex_lock(&stream->mutex);
307 if (uvc_queue_allocated(&stream->queue)) {
312 memcpy(&stream->ctrl, &probe, sizeof probe);
313 stream->cur_format = format;
314 stream->cur_frame = frame;
317 mutex_unlock(&stream->mutex);
321 static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream,
322 struct v4l2_streamparm *parm)
324 uint32_t numerator, denominator;
326 if (parm->type != stream->type)
329 mutex_lock(&stream->mutex);
330 numerator = stream->ctrl.dwFrameInterval;
331 mutex_unlock(&stream->mutex);
333 denominator = 10000000;
334 uvc_simplify_fraction(&numerator, &denominator, 8, 333);
336 memset(parm, 0, sizeof *parm);
337 parm->type = stream->type;
339 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
340 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
341 parm->parm.capture.capturemode = 0;
342 parm->parm.capture.timeperframe.numerator = numerator;
343 parm->parm.capture.timeperframe.denominator = denominator;
344 parm->parm.capture.extendedmode = 0;
345 parm->parm.capture.readbuffers = 0;
347 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
348 parm->parm.output.outputmode = 0;
349 parm->parm.output.timeperframe.numerator = numerator;
350 parm->parm.output.timeperframe.denominator = denominator;
356 static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
357 struct v4l2_streamparm *parm)
359 struct uvc_streaming_control probe;
360 struct v4l2_fract timeperframe;
364 if (parm->type != stream->type)
367 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
368 timeperframe = parm->parm.capture.timeperframe;
370 timeperframe = parm->parm.output.timeperframe;
372 interval = uvc_fraction_to_interval(timeperframe.numerator,
373 timeperframe.denominator);
374 uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
375 timeperframe.numerator, timeperframe.denominator, interval);
377 mutex_lock(&stream->mutex);
379 if (uvc_queue_streaming(&stream->queue)) {
380 mutex_unlock(&stream->mutex);
384 memcpy(&probe, &stream->ctrl, sizeof probe);
385 probe.dwFrameInterval =
386 uvc_try_frame_interval(stream->cur_frame, interval);
388 /* Probe the device with the new settings. */
389 ret = uvc_probe_video(stream, &probe);
391 mutex_unlock(&stream->mutex);
395 memcpy(&stream->ctrl, &probe, sizeof probe);
396 mutex_unlock(&stream->mutex);
398 /* Return the actual frame period. */
399 timeperframe.numerator = probe.dwFrameInterval;
400 timeperframe.denominator = 10000000;
401 uvc_simplify_fraction(&timeperframe.numerator,
402 &timeperframe.denominator, 8, 333);
404 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
405 parm->parm.capture.timeperframe = timeperframe;
407 parm->parm.output.timeperframe = timeperframe;
412 /* ------------------------------------------------------------------------
413 * Privilege management
417 * Privilege management is the multiple-open implementation basis. The current
418 * implementation is completely transparent for the end-user and doesn't
419 * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls.
420 * Those ioctls enable finer control on the device (by making possible for a
421 * user to request exclusive access to a device), but are not mature yet.
422 * Switching to the V4L2 priority mechanism might be considered in the future
423 * if this situation changes.
425 * Each open instance of a UVC device can either be in a privileged or
426 * unprivileged state. Only a single instance can be in a privileged state at
427 * a given time. Trying to perform an operation that requires privileges will
428 * automatically acquire the required privileges if possible, or return -EBUSY
429 * otherwise. Privileges are dismissed when closing the instance or when
430 * freeing the video buffers using VIDIOC_REQBUFS.
432 * Operations that require privileges are:
439 static int uvc_acquire_privileges(struct uvc_fh *handle)
441 /* Always succeed if the handle is already privileged. */
442 if (handle->state == UVC_HANDLE_ACTIVE)
445 /* Check if the device already has a privileged handle. */
446 if (atomic_inc_return(&handle->stream->active) != 1) {
447 atomic_dec(&handle->stream->active);
451 handle->state = UVC_HANDLE_ACTIVE;
455 static void uvc_dismiss_privileges(struct uvc_fh *handle)
457 if (handle->state == UVC_HANDLE_ACTIVE)
458 atomic_dec(&handle->stream->active);
460 handle->state = UVC_HANDLE_PASSIVE;
463 static int uvc_has_privileges(struct uvc_fh *handle)
465 return handle->state == UVC_HANDLE_ACTIVE;
468 /* ------------------------------------------------------------------------
469 * V4L2 file operations
472 static int uvc_v4l2_open(struct file *file)
474 struct uvc_streaming *stream;
475 struct uvc_fh *handle;
478 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
479 stream = video_drvdata(file);
481 if (stream->dev->state & UVC_DEV_DISCONNECTED)
484 ret = usb_autopm_get_interface(stream->dev->intf);
488 /* Create the device handle. */
489 handle = kzalloc(sizeof *handle, GFP_KERNEL);
490 if (handle == NULL) {
491 usb_autopm_put_interface(stream->dev->intf);
495 if (atomic_inc_return(&stream->dev->users) == 1) {
496 ret = uvc_status_start(stream->dev);
498 usb_autopm_put_interface(stream->dev->intf);
499 atomic_dec(&stream->dev->users);
505 handle->chain = stream->chain;
506 handle->stream = stream;
507 handle->state = UVC_HANDLE_PASSIVE;
508 file->private_data = handle;
513 static int uvc_v4l2_release(struct file *file)
515 struct uvc_fh *handle = file->private_data;
516 struct uvc_streaming *stream = handle->stream;
518 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");
520 /* Only free resources if this is a privileged handle. */
521 if (uvc_has_privileges(handle)) {
522 uvc_video_enable(stream, 0);
524 if (uvc_free_buffers(&stream->queue) < 0)
525 uvc_printk(KERN_ERR, "uvc_v4l2_release: Unable to "
529 /* Release the file handle. */
530 uvc_dismiss_privileges(handle);
532 file->private_data = NULL;
534 if (atomic_dec_return(&stream->dev->users) == 0)
535 uvc_status_stop(stream->dev);
537 usb_autopm_put_interface(stream->dev->intf);
541 static long uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
543 struct video_device *vdev = video_devdata(file);
544 struct uvc_fh *handle = file->private_data;
545 struct uvc_video_chain *chain = handle->chain;
546 struct uvc_streaming *stream = handle->stream;
550 /* Query capabilities */
551 case VIDIOC_QUERYCAP:
553 struct v4l2_capability *cap = arg;
555 memset(cap, 0, sizeof *cap);
556 strlcpy(cap->driver, "uvcvideo", sizeof cap->driver);
557 strlcpy(cap->card, vdev->name, sizeof cap->card);
558 usb_make_path(stream->dev->udev,
559 cap->bus_info, sizeof(cap->bus_info));
560 cap->version = DRIVER_VERSION_NUMBER;
561 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
562 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
563 | V4L2_CAP_STREAMING;
565 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT
566 | V4L2_CAP_STREAMING;
570 /* Get, Set & Query control */
571 case VIDIOC_QUERYCTRL:
572 return uvc_query_v4l2_ctrl(chain, arg);
576 struct v4l2_control *ctrl = arg;
577 struct v4l2_ext_control xctrl;
579 memset(&xctrl, 0, sizeof xctrl);
582 ret = uvc_ctrl_begin(chain);
586 ret = uvc_ctrl_get(chain, &xctrl);
587 uvc_ctrl_rollback(chain);
589 ctrl->value = xctrl.value;
595 struct v4l2_control *ctrl = arg;
596 struct v4l2_ext_control xctrl;
598 memset(&xctrl, 0, sizeof xctrl);
600 xctrl.value = ctrl->value;
602 ret = uvc_ctrl_begin(chain);
606 ret = uvc_ctrl_set(chain, &xctrl);
608 uvc_ctrl_rollback(chain);
611 ret = uvc_ctrl_commit(chain);
613 ctrl->value = xctrl.value;
617 case VIDIOC_QUERYMENU:
618 return uvc_query_v4l2_menu(chain, arg);
620 case VIDIOC_G_EXT_CTRLS:
622 struct v4l2_ext_controls *ctrls = arg;
623 struct v4l2_ext_control *ctrl = ctrls->controls;
626 ret = uvc_ctrl_begin(chain);
630 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
631 ret = uvc_ctrl_get(chain, ctrl);
633 uvc_ctrl_rollback(chain);
634 ctrls->error_idx = i;
638 ctrls->error_idx = 0;
639 ret = uvc_ctrl_rollback(chain);
643 case VIDIOC_S_EXT_CTRLS:
644 case VIDIOC_TRY_EXT_CTRLS:
646 struct v4l2_ext_controls *ctrls = arg;
647 struct v4l2_ext_control *ctrl = ctrls->controls;
650 ret = uvc_ctrl_begin(chain);
654 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
655 ret = uvc_ctrl_set(chain, ctrl);
657 uvc_ctrl_rollback(chain);
658 ctrls->error_idx = i;
663 ctrls->error_idx = 0;
665 if (cmd == VIDIOC_S_EXT_CTRLS)
666 ret = uvc_ctrl_commit(chain);
668 ret = uvc_ctrl_rollback(chain);
672 /* Get, Set & Enum input */
673 case VIDIOC_ENUMINPUT:
675 const struct uvc_entity *selector = chain->selector;
676 struct v4l2_input *input = arg;
677 struct uvc_entity *iterm = NULL;
678 u32 index = input->index;
681 if (selector == NULL ||
682 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
685 list_for_each_entry(iterm, &chain->entities, chain) {
686 if (UVC_ENTITY_IS_ITERM(iterm))
690 } else if (pin < selector->bNrInPins) {
691 pin = selector->baSourceID[index];
692 list_for_each_entry(iterm, &chain->entities, chain) {
693 if (!UVC_ENTITY_IS_ITERM(iterm))
695 if (iterm->id == pin)
700 if (iterm == NULL || iterm->id != pin)
703 memset(input, 0, sizeof *input);
704 input->index = index;
705 strlcpy(input->name, iterm->name, sizeof input->name);
706 if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
707 input->type = V4L2_INPUT_TYPE_CAMERA;
715 if (chain->selector == NULL ||
716 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
721 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
722 chain->selector->id, chain->dev->intfnum,
723 UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
727 *(int *)arg = input - 1;
733 u32 input = *(u32 *)arg + 1;
735 if ((ret = uvc_acquire_privileges(handle)) < 0)
738 if (chain->selector == NULL ||
739 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
745 if (input == 0 || input > chain->selector->bNrInPins)
748 return uvc_query_ctrl(chain->dev, UVC_SET_CUR,
749 chain->selector->id, chain->dev->intfnum,
750 UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
753 /* Try, Get, Set & Enum format */
754 case VIDIOC_ENUM_FMT:
756 struct v4l2_fmtdesc *fmt = arg;
757 struct uvc_format *format;
758 enum v4l2_buf_type type = fmt->type;
759 __u32 index = fmt->index;
761 if (fmt->type != stream->type ||
762 fmt->index >= stream->nformats)
765 memset(fmt, 0, sizeof(*fmt));
769 format = &stream->format[fmt->index];
771 if (format->flags & UVC_FMT_FLAG_COMPRESSED)
772 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
773 strlcpy(fmt->description, format->name,
774 sizeof fmt->description);
775 fmt->description[sizeof fmt->description - 1] = 0;
776 fmt->pixelformat = format->fcc;
782 struct uvc_streaming_control probe;
784 return uvc_v4l2_try_format(stream, arg, &probe, NULL, NULL);
788 if ((ret = uvc_acquire_privileges(handle)) < 0)
791 return uvc_v4l2_set_format(stream, arg);
794 return uvc_v4l2_get_format(stream, arg);
796 /* Frame size enumeration */
797 case VIDIOC_ENUM_FRAMESIZES:
799 struct v4l2_frmsizeenum *fsize = arg;
800 struct uvc_format *format = NULL;
801 struct uvc_frame *frame;
804 /* Look for the given pixel format */
805 for (i = 0; i < stream->nformats; i++) {
806 if (stream->format[i].fcc ==
807 fsize->pixel_format) {
808 format = &stream->format[i];
815 if (fsize->index >= format->nframes)
818 frame = &format->frame[fsize->index];
819 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
820 fsize->discrete.width = frame->wWidth;
821 fsize->discrete.height = frame->wHeight;
825 /* Frame interval enumeration */
826 case VIDIOC_ENUM_FRAMEINTERVALS:
828 struct v4l2_frmivalenum *fival = arg;
829 struct uvc_format *format = NULL;
830 struct uvc_frame *frame = NULL;
833 /* Look for the given pixel format and frame size */
834 for (i = 0; i < stream->nformats; i++) {
835 if (stream->format[i].fcc ==
836 fival->pixel_format) {
837 format = &stream->format[i];
844 for (i = 0; i < format->nframes; i++) {
845 if (format->frame[i].wWidth == fival->width &&
846 format->frame[i].wHeight == fival->height) {
847 frame = &format->frame[i];
854 if (frame->bFrameIntervalType) {
855 if (fival->index >= frame->bFrameIntervalType)
858 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
859 fival->discrete.numerator =
860 frame->dwFrameInterval[fival->index];
861 fival->discrete.denominator = 10000000;
862 uvc_simplify_fraction(&fival->discrete.numerator,
863 &fival->discrete.denominator, 8, 333);
865 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
866 fival->stepwise.min.numerator =
867 frame->dwFrameInterval[0];
868 fival->stepwise.min.denominator = 10000000;
869 fival->stepwise.max.numerator =
870 frame->dwFrameInterval[1];
871 fival->stepwise.max.denominator = 10000000;
872 fival->stepwise.step.numerator =
873 frame->dwFrameInterval[2];
874 fival->stepwise.step.denominator = 10000000;
875 uvc_simplify_fraction(&fival->stepwise.min.numerator,
876 &fival->stepwise.min.denominator, 8, 333);
877 uvc_simplify_fraction(&fival->stepwise.max.numerator,
878 &fival->stepwise.max.denominator, 8, 333);
879 uvc_simplify_fraction(&fival->stepwise.step.numerator,
880 &fival->stepwise.step.denominator, 8, 333);
885 /* Get & Set streaming parameters */
887 return uvc_v4l2_get_streamparm(stream, arg);
890 if ((ret = uvc_acquire_privileges(handle)) < 0)
893 return uvc_v4l2_set_streamparm(stream, arg);
895 /* Cropping and scaling */
898 struct v4l2_cropcap *ccap = arg;
900 if (ccap->type != stream->type)
903 ccap->bounds.left = 0;
904 ccap->bounds.top = 0;
906 mutex_lock(&stream->mutex);
907 ccap->bounds.width = stream->cur_frame->wWidth;
908 ccap->bounds.height = stream->cur_frame->wHeight;
909 mutex_unlock(&stream->mutex);
911 ccap->defrect = ccap->bounds;
913 ccap->pixelaspect.numerator = 1;
914 ccap->pixelaspect.denominator = 1;
922 /* Buffers & streaming */
925 struct v4l2_requestbuffers *rb = arg;
927 if (rb->type != stream->type ||
928 rb->memory != V4L2_MEMORY_MMAP)
931 if ((ret = uvc_acquire_privileges(handle)) < 0)
934 mutex_lock(&stream->mutex);
935 ret = uvc_alloc_buffers(&stream->queue, rb->count,
936 stream->ctrl.dwMaxVideoFrameSize);
937 mutex_unlock(&stream->mutex);
942 uvc_dismiss_privileges(handle);
949 case VIDIOC_QUERYBUF:
951 struct v4l2_buffer *buf = arg;
953 if (buf->type != stream->type)
956 if (!uvc_has_privileges(handle))
959 return uvc_query_buffer(&stream->queue, buf);
963 if (!uvc_has_privileges(handle))
966 return uvc_queue_buffer(&stream->queue, arg);
969 if (!uvc_has_privileges(handle))
972 return uvc_dequeue_buffer(&stream->queue, arg,
973 file->f_flags & O_NONBLOCK);
975 case VIDIOC_STREAMON:
979 if (*type != stream->type)
982 if (!uvc_has_privileges(handle))
985 mutex_lock(&stream->mutex);
986 ret = uvc_video_enable(stream, 1);
987 mutex_unlock(&stream->mutex);
993 case VIDIOC_STREAMOFF:
997 if (*type != stream->type)
1000 if (!uvc_has_privileges(handle))
1003 return uvc_video_enable(stream, 0);
1006 /* Analog video standards make no sense for digital cameras. */
1007 case VIDIOC_ENUMSTD:
1008 case VIDIOC_QUERYSTD:
1012 case VIDIOC_OVERLAY:
1014 case VIDIOC_ENUMAUDIO:
1015 case VIDIOC_ENUMAUDOUT:
1017 case VIDIOC_ENUMOUTPUT:
1018 uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd);
1021 /* Dynamic controls. */
1022 case UVCIOC_CTRL_ADD:
1023 /* Legacy ioctl, kept for API compatibility reasons */
1026 case UVCIOC_CTRL_MAP_OLD:
1027 case UVCIOC_CTRL_MAP:
1028 return uvc_ioctl_ctrl_map(chain, arg,
1029 cmd == UVCIOC_CTRL_MAP_OLD);
1031 case UVCIOC_CTRL_GET:
1032 return uvc_xu_ctrl_query(chain, arg, 0);
1034 case UVCIOC_CTRL_SET:
1035 return uvc_xu_ctrl_query(chain, arg, 1);
1038 uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n", cmd);
1045 static long uvc_v4l2_ioctl(struct file *file,
1046 unsigned int cmd, unsigned long arg)
1048 if (uvc_trace_param & UVC_TRACE_IOCTL) {
1049 uvc_printk(KERN_DEBUG, "uvc_v4l2_ioctl(");
1050 v4l_printk_ioctl(cmd);
1054 return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl);
1057 static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1058 size_t count, loff_t *ppos)
1060 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
1064 static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1066 struct uvc_fh *handle = file->private_data;
1067 struct uvc_streaming *stream = handle->stream;
1069 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
1071 return uvc_queue_mmap(&stream->queue, vma);
1074 static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
1076 struct uvc_fh *handle = file->private_data;
1077 struct uvc_streaming *stream = handle->stream;
1079 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
1081 return uvc_queue_poll(&stream->queue, file, wait);
1084 const struct v4l2_file_operations uvc_fops = {
1085 .owner = THIS_MODULE,
1086 .open = uvc_v4l2_open,
1087 .release = uvc_v4l2_release,
1088 .unlocked_ioctl = uvc_v4l2_ioctl,
1089 .read = uvc_v4l2_read,
1090 .mmap = uvc_v4l2_mmap,
1091 .poll = uvc_v4l2_poll,