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/compat.h>
15 #include <linux/kernel.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-ctrls.h>
28 #include <media/v4l2-event.h>
29 #include <media/v4l2-ioctl.h>
33 /* ------------------------------------------------------------------------
36 static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain,
37 struct uvc_xu_control_mapping *xmap)
39 struct uvc_control_mapping *map;
43 map = kzalloc(sizeof *map, GFP_KERNEL);
48 memcpy(map->name, xmap->name, sizeof map->name);
49 memcpy(map->entity, xmap->entity, sizeof map->entity);
50 map->selector = xmap->selector;
51 map->size = xmap->size;
52 map->offset = xmap->offset;
53 map->v4l2_type = xmap->v4l2_type;
54 map->data_type = xmap->data_type;
56 switch (xmap->v4l2_type) {
57 case V4L2_CTRL_TYPE_INTEGER:
58 case V4L2_CTRL_TYPE_BOOLEAN:
59 case V4L2_CTRL_TYPE_BUTTON:
62 case V4L2_CTRL_TYPE_MENU:
63 /* Prevent excessive memory consumption, as well as integer
66 if (xmap->menu_count == 0 ||
67 xmap->menu_count > UVC_MAX_CONTROL_MENU_ENTRIES) {
72 size = xmap->menu_count * sizeof(*map->menu_info);
73 map->menu_info = kmalloc(size, GFP_KERNEL);
74 if (map->menu_info == NULL) {
79 if (copy_from_user(map->menu_info, xmap->menu_info, size)) {
84 map->menu_count = xmap->menu_count;
88 uvc_trace(UVC_TRACE_CONTROL, "Unsupported V4L2 control type "
89 "%u.\n", xmap->v4l2_type);
94 ret = uvc_ctrl_add_mapping(chain, map);
97 kfree(map->menu_info);
103 /* ------------------------------------------------------------------------
108 * Find the frame interval closest to the requested frame interval for the
109 * given frame format and size. This should be done by the device as part of
110 * the Video Probe and Commit negotiation, but some hardware don't implement
113 static __u32 uvc_try_frame_interval(struct uvc_frame *frame, __u32 interval)
117 if (frame->bFrameIntervalType) {
118 __u32 best = -1, dist;
120 for (i = 0; i < frame->bFrameIntervalType; ++i) {
121 dist = interval > frame->dwFrameInterval[i]
122 ? interval - frame->dwFrameInterval[i]
123 : frame->dwFrameInterval[i] - interval;
131 interval = frame->dwFrameInterval[i-1];
133 const __u32 min = frame->dwFrameInterval[0];
134 const __u32 max = frame->dwFrameInterval[1];
135 const __u32 step = frame->dwFrameInterval[2];
137 interval = min + (interval - min + step/2) / step * step;
145 static int uvc_v4l2_try_format(struct uvc_streaming *stream,
146 struct v4l2_format *fmt, struct uvc_streaming_control *probe,
147 struct uvc_format **uvc_format, struct uvc_frame **uvc_frame)
149 struct uvc_format *format = NULL;
150 struct uvc_frame *frame = NULL;
152 unsigned int d, maxd;
158 if (fmt->type != stream->type)
161 fcc = (__u8 *)&fmt->fmt.pix.pixelformat;
162 uvc_trace(UVC_TRACE_FORMAT, "Trying format 0x%08x (%c%c%c%c): %ux%u.\n",
163 fmt->fmt.pix.pixelformat,
164 fcc[0], fcc[1], fcc[2], fcc[3],
165 fmt->fmt.pix.width, fmt->fmt.pix.height);
167 /* Check if the hardware supports the requested format, use the default
170 for (i = 0; i < stream->nformats; ++i) {
171 format = &stream->format[i];
172 if (format->fcc == fmt->fmt.pix.pixelformat)
176 if (i == stream->nformats) {
177 format = stream->def_format;
178 fmt->fmt.pix.pixelformat = format->fcc;
181 /* Find the closest image size. The distance between image sizes is
182 * the size in pixels of the non-overlapping regions between the
183 * requested size and the frame-specified size.
185 rw = fmt->fmt.pix.width;
186 rh = fmt->fmt.pix.height;
187 maxd = (unsigned int)-1;
189 for (i = 0; i < format->nframes; ++i) {
190 __u16 w = format->frame[i].wWidth;
191 __u16 h = format->frame[i].wHeight;
193 d = min(w, rw) * min(h, rh);
194 d = w*h + rw*rh - 2*d;
197 frame = &format->frame[i];
205 uvc_trace(UVC_TRACE_FORMAT, "Unsupported size %ux%u.\n",
206 fmt->fmt.pix.width, fmt->fmt.pix.height);
210 /* Use the default frame interval. */
211 interval = frame->dwDefaultFrameInterval;
212 uvc_trace(UVC_TRACE_FORMAT, "Using default frame interval %u.%u us "
213 "(%u.%u fps).\n", interval/10, interval%10, 10000000/interval,
214 (100000000/interval)%10);
216 /* Set the format index, frame index and frame interval. */
217 memset(probe, 0, sizeof *probe);
218 probe->bmHint = 1; /* dwFrameInterval */
219 probe->bFormatIndex = format->index;
220 probe->bFrameIndex = frame->bFrameIndex;
221 probe->dwFrameInterval = uvc_try_frame_interval(frame, interval);
222 /* Some webcams stall the probe control set request when the
223 * dwMaxVideoFrameSize field is set to zero. The UVC specification
224 * clearly states that the field is read-only from the host, so this
225 * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by
226 * the webcam to work around the problem.
228 * The workaround could probably be enabled for all webcams, so the
229 * quirk can be removed if needed. It's currently useful to detect
230 * webcam bugs and fix them before they hit the market (providing
231 * developers test their webcams with the Linux driver as well as with
232 * the Windows driver).
234 mutex_lock(&stream->mutex);
235 if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS)
236 probe->dwMaxVideoFrameSize =
237 stream->ctrl.dwMaxVideoFrameSize;
239 /* Probe the device. */
240 ret = uvc_probe_video(stream, probe);
241 mutex_unlock(&stream->mutex);
245 fmt->fmt.pix.width = frame->wWidth;
246 fmt->fmt.pix.height = frame->wHeight;
247 fmt->fmt.pix.field = V4L2_FIELD_NONE;
248 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
249 fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize;
250 fmt->fmt.pix.colorspace = format->colorspace;
251 fmt->fmt.pix.priv = 0;
253 if (uvc_format != NULL)
254 *uvc_format = format;
255 if (uvc_frame != NULL)
262 static int uvc_v4l2_get_format(struct uvc_streaming *stream,
263 struct v4l2_format *fmt)
265 struct uvc_format *format;
266 struct uvc_frame *frame;
269 if (fmt->type != stream->type)
272 mutex_lock(&stream->mutex);
273 format = stream->cur_format;
274 frame = stream->cur_frame;
276 if (format == NULL || frame == NULL) {
281 fmt->fmt.pix.pixelformat = format->fcc;
282 fmt->fmt.pix.width = frame->wWidth;
283 fmt->fmt.pix.height = frame->wHeight;
284 fmt->fmt.pix.field = V4L2_FIELD_NONE;
285 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
286 fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize;
287 fmt->fmt.pix.colorspace = format->colorspace;
288 fmt->fmt.pix.priv = 0;
291 mutex_unlock(&stream->mutex);
295 static int uvc_v4l2_set_format(struct uvc_streaming *stream,
296 struct v4l2_format *fmt)
298 struct uvc_streaming_control probe;
299 struct uvc_format *format;
300 struct uvc_frame *frame;
303 if (fmt->type != stream->type)
306 ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame);
310 mutex_lock(&stream->mutex);
312 if (uvc_queue_allocated(&stream->queue)) {
317 stream->ctrl = probe;
318 stream->cur_format = format;
319 stream->cur_frame = frame;
322 mutex_unlock(&stream->mutex);
326 static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream,
327 struct v4l2_streamparm *parm)
329 uint32_t numerator, denominator;
331 if (parm->type != stream->type)
334 mutex_lock(&stream->mutex);
335 numerator = stream->ctrl.dwFrameInterval;
336 mutex_unlock(&stream->mutex);
338 denominator = 10000000;
339 uvc_simplify_fraction(&numerator, &denominator, 8, 333);
341 memset(parm, 0, sizeof *parm);
342 parm->type = stream->type;
344 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
345 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
346 parm->parm.capture.capturemode = 0;
347 parm->parm.capture.timeperframe.numerator = numerator;
348 parm->parm.capture.timeperframe.denominator = denominator;
349 parm->parm.capture.extendedmode = 0;
350 parm->parm.capture.readbuffers = 0;
352 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
353 parm->parm.output.outputmode = 0;
354 parm->parm.output.timeperframe.numerator = numerator;
355 parm->parm.output.timeperframe.denominator = denominator;
361 static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
362 struct v4l2_streamparm *parm)
364 struct uvc_streaming_control probe;
365 struct v4l2_fract timeperframe;
369 if (parm->type != stream->type)
372 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
373 timeperframe = parm->parm.capture.timeperframe;
375 timeperframe = parm->parm.output.timeperframe;
377 interval = uvc_fraction_to_interval(timeperframe.numerator,
378 timeperframe.denominator);
379 uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
380 timeperframe.numerator, timeperframe.denominator, interval);
382 mutex_lock(&stream->mutex);
384 if (uvc_queue_streaming(&stream->queue)) {
385 mutex_unlock(&stream->mutex);
389 probe = stream->ctrl;
390 probe.dwFrameInterval =
391 uvc_try_frame_interval(stream->cur_frame, interval);
393 /* Probe the device with the new settings. */
394 ret = uvc_probe_video(stream, &probe);
396 mutex_unlock(&stream->mutex);
400 stream->ctrl = probe;
401 mutex_unlock(&stream->mutex);
403 /* Return the actual frame period. */
404 timeperframe.numerator = probe.dwFrameInterval;
405 timeperframe.denominator = 10000000;
406 uvc_simplify_fraction(&timeperframe.numerator,
407 &timeperframe.denominator, 8, 333);
409 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
410 parm->parm.capture.timeperframe = timeperframe;
412 parm->parm.output.timeperframe = timeperframe;
417 /* ------------------------------------------------------------------------
418 * Privilege management
422 * Privilege management is the multiple-open implementation basis. The current
423 * implementation is completely transparent for the end-user and doesn't
424 * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls.
425 * Those ioctls enable finer control on the device (by making possible for a
426 * user to request exclusive access to a device), but are not mature yet.
427 * Switching to the V4L2 priority mechanism might be considered in the future
428 * if this situation changes.
430 * Each open instance of a UVC device can either be in a privileged or
431 * unprivileged state. Only a single instance can be in a privileged state at
432 * a given time. Trying to perform an operation that requires privileges will
433 * automatically acquire the required privileges if possible, or return -EBUSY
434 * otherwise. Privileges are dismissed when closing the instance or when
435 * freeing the video buffers using VIDIOC_REQBUFS.
437 * Operations that require privileges are:
444 static int uvc_acquire_privileges(struct uvc_fh *handle)
446 /* Always succeed if the handle is already privileged. */
447 if (handle->state == UVC_HANDLE_ACTIVE)
450 /* Check if the device already has a privileged handle. */
451 if (atomic_inc_return(&handle->stream->active) != 1) {
452 atomic_dec(&handle->stream->active);
456 handle->state = UVC_HANDLE_ACTIVE;
460 static void uvc_dismiss_privileges(struct uvc_fh *handle)
462 if (handle->state == UVC_HANDLE_ACTIVE)
463 atomic_dec(&handle->stream->active);
465 handle->state = UVC_HANDLE_PASSIVE;
468 static int uvc_has_privileges(struct uvc_fh *handle)
470 return handle->state == UVC_HANDLE_ACTIVE;
473 /* ------------------------------------------------------------------------
474 * V4L2 file operations
477 static int uvc_v4l2_open(struct file *file)
479 struct uvc_streaming *stream;
480 struct uvc_fh *handle;
483 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
484 stream = video_drvdata(file);
486 if (stream->dev->state & UVC_DEV_DISCONNECTED)
489 ret = usb_autopm_get_interface(stream->dev->intf);
493 /* Create the device handle. */
494 handle = kzalloc(sizeof *handle, GFP_KERNEL);
495 if (handle == NULL) {
496 usb_autopm_put_interface(stream->dev->intf);
500 mutex_lock(&stream->dev->lock);
501 if (stream->dev->users == 0) {
502 ret = uvc_status_start(stream->dev, GFP_KERNEL);
504 mutex_unlock(&stream->dev->lock);
505 usb_autopm_put_interface(stream->dev->intf);
511 stream->dev->users++;
512 mutex_unlock(&stream->dev->lock);
514 v4l2_fh_init(&handle->vfh, stream->vdev);
515 v4l2_fh_add(&handle->vfh);
516 handle->chain = stream->chain;
517 handle->stream = stream;
518 handle->state = UVC_HANDLE_PASSIVE;
519 file->private_data = handle;
524 static int uvc_v4l2_release(struct file *file)
526 struct uvc_fh *handle = file->private_data;
527 struct uvc_streaming *stream = handle->stream;
529 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");
531 /* Only free resources if this is a privileged handle. */
532 if (uvc_has_privileges(handle))
533 uvc_queue_release(&stream->queue);
535 /* Release the file handle. */
536 uvc_dismiss_privileges(handle);
537 v4l2_fh_del(&handle->vfh);
538 v4l2_fh_exit(&handle->vfh);
540 file->private_data = NULL;
542 mutex_lock(&stream->dev->lock);
543 if (--stream->dev->users == 0)
544 uvc_status_stop(stream->dev);
545 mutex_unlock(&stream->dev->lock);
547 usb_autopm_put_interface(stream->dev->intf);
551 static int uvc_ioctl_querycap(struct file *file, void *fh,
552 struct v4l2_capability *cap)
554 struct video_device *vdev = video_devdata(file);
555 struct uvc_fh *handle = file->private_data;
556 struct uvc_video_chain *chain = handle->chain;
557 struct uvc_streaming *stream = handle->stream;
559 strlcpy(cap->driver, "uvcvideo", sizeof(cap->driver));
560 strlcpy(cap->card, vdev->name, sizeof(cap->card));
561 usb_make_path(stream->dev->udev, cap->bus_info, sizeof(cap->bus_info));
562 cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
564 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
565 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
567 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
572 static int uvc_ioctl_enum_fmt(struct uvc_streaming *stream,
573 struct v4l2_fmtdesc *fmt)
575 struct uvc_format *format;
576 enum v4l2_buf_type type = fmt->type;
577 __u32 index = fmt->index;
579 if (fmt->type != stream->type || fmt->index >= stream->nformats)
582 memset(fmt, 0, sizeof(*fmt));
586 format = &stream->format[fmt->index];
588 if (format->flags & UVC_FMT_FLAG_COMPRESSED)
589 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
590 strlcpy(fmt->description, format->name, sizeof(fmt->description));
591 fmt->description[sizeof(fmt->description) - 1] = 0;
592 fmt->pixelformat = format->fcc;
596 static int uvc_ioctl_enum_fmt_vid_cap(struct file *file, void *fh,
597 struct v4l2_fmtdesc *fmt)
599 struct uvc_fh *handle = fh;
600 struct uvc_streaming *stream = handle->stream;
602 return uvc_ioctl_enum_fmt(stream, fmt);
605 static int uvc_ioctl_enum_fmt_vid_out(struct file *file, void *fh,
606 struct v4l2_fmtdesc *fmt)
608 struct uvc_fh *handle = fh;
609 struct uvc_streaming *stream = handle->stream;
611 return uvc_ioctl_enum_fmt(stream, fmt);
614 static int uvc_ioctl_g_fmt_vid_cap(struct file *file, void *fh,
615 struct v4l2_format *fmt)
617 struct uvc_fh *handle = fh;
618 struct uvc_streaming *stream = handle->stream;
620 return uvc_v4l2_get_format(stream, fmt);
623 static int uvc_ioctl_g_fmt_vid_out(struct file *file, void *fh,
624 struct v4l2_format *fmt)
626 struct uvc_fh *handle = fh;
627 struct uvc_streaming *stream = handle->stream;
629 return uvc_v4l2_get_format(stream, fmt);
632 static int uvc_ioctl_s_fmt_vid_cap(struct file *file, void *fh,
633 struct v4l2_format *fmt)
635 struct uvc_fh *handle = fh;
636 struct uvc_streaming *stream = handle->stream;
639 ret = uvc_acquire_privileges(handle);
643 return uvc_v4l2_set_format(stream, fmt);
646 static int uvc_ioctl_s_fmt_vid_out(struct file *file, void *fh,
647 struct v4l2_format *fmt)
649 struct uvc_fh *handle = fh;
650 struct uvc_streaming *stream = handle->stream;
653 ret = uvc_acquire_privileges(handle);
657 return uvc_v4l2_set_format(stream, fmt);
660 static int uvc_ioctl_try_fmt_vid_cap(struct file *file, void *fh,
661 struct v4l2_format *fmt)
663 struct uvc_fh *handle = fh;
664 struct uvc_streaming *stream = handle->stream;
665 struct uvc_streaming_control probe;
667 return uvc_v4l2_try_format(stream, fmt, &probe, NULL, NULL);
670 static int uvc_ioctl_try_fmt_vid_out(struct file *file, void *fh,
671 struct v4l2_format *fmt)
673 struct uvc_fh *handle = fh;
674 struct uvc_streaming *stream = handle->stream;
675 struct uvc_streaming_control probe;
677 return uvc_v4l2_try_format(stream, fmt, &probe, NULL, NULL);
680 static int uvc_ioctl_reqbufs(struct file *file, void *fh,
681 struct v4l2_requestbuffers *rb)
683 struct uvc_fh *handle = fh;
684 struct uvc_streaming *stream = handle->stream;
687 ret = uvc_acquire_privileges(handle);
691 mutex_lock(&stream->mutex);
692 ret = uvc_request_buffers(&stream->queue, rb);
693 mutex_unlock(&stream->mutex);
698 uvc_dismiss_privileges(handle);
703 static int uvc_ioctl_querybuf(struct file *file, void *fh,
704 struct v4l2_buffer *buf)
706 struct uvc_fh *handle = fh;
707 struct uvc_streaming *stream = handle->stream;
709 if (!uvc_has_privileges(handle))
712 return uvc_query_buffer(&stream->queue, buf);
715 static int uvc_ioctl_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
717 struct uvc_fh *handle = fh;
718 struct uvc_streaming *stream = handle->stream;
720 if (!uvc_has_privileges(handle))
723 return uvc_queue_buffer(&stream->queue, buf);
726 static int uvc_ioctl_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
728 struct uvc_fh *handle = fh;
729 struct uvc_streaming *stream = handle->stream;
731 if (!uvc_has_privileges(handle))
734 return uvc_dequeue_buffer(&stream->queue, buf,
735 file->f_flags & O_NONBLOCK);
738 static int uvc_ioctl_create_bufs(struct file *file, void *fh,
739 struct v4l2_create_buffers *cb)
741 struct uvc_fh *handle = fh;
742 struct uvc_streaming *stream = handle->stream;
745 ret = uvc_acquire_privileges(handle);
749 return uvc_create_buffers(&stream->queue, cb);
752 static int uvc_ioctl_streamon(struct file *file, void *fh,
753 enum v4l2_buf_type type)
755 struct uvc_fh *handle = fh;
756 struct uvc_streaming *stream = handle->stream;
759 if (!uvc_has_privileges(handle))
762 mutex_lock(&stream->mutex);
763 ret = uvc_queue_streamon(&stream->queue, type);
764 mutex_unlock(&stream->mutex);
769 static int uvc_ioctl_streamoff(struct file *file, void *fh,
770 enum v4l2_buf_type type)
772 struct uvc_fh *handle = fh;
773 struct uvc_streaming *stream = handle->stream;
775 if (!uvc_has_privileges(handle))
778 mutex_lock(&stream->mutex);
779 uvc_queue_streamoff(&stream->queue, type);
780 mutex_unlock(&stream->mutex);
785 static int uvc_ioctl_enum_input(struct file *file, void *fh,
786 struct v4l2_input *input)
788 struct uvc_fh *handle = fh;
789 struct uvc_video_chain *chain = handle->chain;
790 const struct uvc_entity *selector = chain->selector;
791 struct uvc_entity *iterm = NULL;
792 u32 index = input->index;
795 if (selector == NULL ||
796 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
799 list_for_each_entry(iterm, &chain->entities, chain) {
800 if (UVC_ENTITY_IS_ITERM(iterm))
804 } else if (index < selector->bNrInPins) {
805 pin = selector->baSourceID[index];
806 list_for_each_entry(iterm, &chain->entities, chain) {
807 if (!UVC_ENTITY_IS_ITERM(iterm))
809 if (iterm->id == pin)
814 if (iterm == NULL || iterm->id != pin)
817 memset(input, 0, sizeof(*input));
818 input->index = index;
819 strlcpy(input->name, iterm->name, sizeof(input->name));
820 if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
821 input->type = V4L2_INPUT_TYPE_CAMERA;
826 static int uvc_ioctl_g_input(struct file *file, void *fh, unsigned int *input)
828 struct uvc_fh *handle = fh;
829 struct uvc_video_chain *chain = handle->chain;
833 if (chain->selector == NULL ||
834 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
839 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, chain->selector->id,
840 chain->dev->intfnum, UVC_SU_INPUT_SELECT_CONTROL,
849 static int uvc_ioctl_s_input(struct file *file, void *fh, unsigned int input)
851 struct uvc_fh *handle = fh;
852 struct uvc_video_chain *chain = handle->chain;
856 ret = uvc_acquire_privileges(handle);
860 if (chain->selector == NULL ||
861 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
867 if (input >= chain->selector->bNrInPins)
871 return uvc_query_ctrl(chain->dev, UVC_SET_CUR, chain->selector->id,
872 chain->dev->intfnum, UVC_SU_INPUT_SELECT_CONTROL,
876 static int uvc_ioctl_queryctrl(struct file *file, void *fh,
877 struct v4l2_queryctrl *qc)
879 struct uvc_fh *handle = fh;
880 struct uvc_video_chain *chain = handle->chain;
882 return uvc_query_v4l2_ctrl(chain, qc);
885 static int uvc_ioctl_g_ctrl(struct file *file, void *fh,
886 struct v4l2_control *ctrl)
888 struct uvc_fh *handle = fh;
889 struct uvc_video_chain *chain = handle->chain;
890 struct v4l2_ext_control xctrl;
893 memset(&xctrl, 0, sizeof(xctrl));
896 ret = uvc_ctrl_begin(chain);
900 ret = uvc_ctrl_get(chain, &xctrl);
901 uvc_ctrl_rollback(handle);
905 ctrl->value = xctrl.value;
909 static int uvc_ioctl_s_ctrl(struct file *file, void *fh,
910 struct v4l2_control *ctrl)
912 struct uvc_fh *handle = fh;
913 struct uvc_video_chain *chain = handle->chain;
914 struct v4l2_ext_control xctrl;
917 memset(&xctrl, 0, sizeof(xctrl));
919 xctrl.value = ctrl->value;
921 ret = uvc_ctrl_begin(chain);
925 ret = uvc_ctrl_set(chain, &xctrl);
927 uvc_ctrl_rollback(handle);
931 ret = uvc_ctrl_commit(handle, &xctrl, 1);
935 ctrl->value = xctrl.value;
939 static int uvc_ioctl_g_ext_ctrls(struct file *file, void *fh,
940 struct v4l2_ext_controls *ctrls)
942 struct uvc_fh *handle = fh;
943 struct uvc_video_chain *chain = handle->chain;
944 struct v4l2_ext_control *ctrl = ctrls->controls;
948 ret = uvc_ctrl_begin(chain);
952 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
953 ret = uvc_ctrl_get(chain, ctrl);
955 uvc_ctrl_rollback(handle);
956 ctrls->error_idx = i;
961 ctrls->error_idx = 0;
963 return uvc_ctrl_rollback(handle);
966 static int uvc_ioctl_s_try_ext_ctrls(struct uvc_fh *handle,
967 struct v4l2_ext_controls *ctrls,
970 struct v4l2_ext_control *ctrl = ctrls->controls;
971 struct uvc_video_chain *chain = handle->chain;
975 ret = uvc_ctrl_begin(chain);
979 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
980 ret = uvc_ctrl_set(chain, ctrl);
982 uvc_ctrl_rollback(handle);
983 ctrls->error_idx = commit ? ctrls->count : i;
988 ctrls->error_idx = 0;
991 return uvc_ctrl_commit(handle, ctrls->controls, ctrls->count);
993 return uvc_ctrl_rollback(handle);
996 static int uvc_ioctl_s_ext_ctrls(struct file *file, void *fh,
997 struct v4l2_ext_controls *ctrls)
999 struct uvc_fh *handle = fh;
1001 return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, true);
1004 static int uvc_ioctl_try_ext_ctrls(struct file *file, void *fh,
1005 struct v4l2_ext_controls *ctrls)
1007 struct uvc_fh *handle = fh;
1009 return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, false);
1012 static int uvc_ioctl_querymenu(struct file *file, void *fh,
1013 struct v4l2_querymenu *qm)
1015 struct uvc_fh *handle = fh;
1016 struct uvc_video_chain *chain = handle->chain;
1018 return uvc_query_v4l2_menu(chain, qm);
1021 static int uvc_ioctl_cropcap(struct file *file, void *fh,
1022 struct v4l2_cropcap *ccap)
1024 struct uvc_fh *handle = fh;
1025 struct uvc_streaming *stream = handle->stream;
1027 if (ccap->type != stream->type)
1030 ccap->bounds.left = 0;
1031 ccap->bounds.top = 0;
1032 mutex_lock(&stream->mutex);
1033 ccap->bounds.width = stream->cur_frame->wWidth;
1034 ccap->bounds.height = stream->cur_frame->wHeight;
1035 mutex_unlock(&stream->mutex);
1037 ccap->defrect = ccap->bounds;
1039 ccap->pixelaspect.numerator = 1;
1040 ccap->pixelaspect.denominator = 1;
1044 static int uvc_ioctl_g_parm(struct file *file, void *fh,
1045 struct v4l2_streamparm *parm)
1047 struct uvc_fh *handle = fh;
1048 struct uvc_streaming *stream = handle->stream;
1050 return uvc_v4l2_get_streamparm(stream, parm);
1053 static int uvc_ioctl_s_parm(struct file *file, void *fh,
1054 struct v4l2_streamparm *parm)
1056 struct uvc_fh *handle = fh;
1057 struct uvc_streaming *stream = handle->stream;
1060 ret = uvc_acquire_privileges(handle);
1064 return uvc_v4l2_set_streamparm(stream, parm);
1067 static int uvc_ioctl_enum_framesizes(struct file *file, void *fh,
1068 struct v4l2_frmsizeenum *fsize)
1070 struct uvc_fh *handle = fh;
1071 struct uvc_streaming *stream = handle->stream;
1072 struct uvc_format *format = NULL;
1073 struct uvc_frame *frame;
1076 /* Look for the given pixel format */
1077 for (i = 0; i < stream->nformats; i++) {
1078 if (stream->format[i].fcc == fsize->pixel_format) {
1079 format = &stream->format[i];
1086 if (fsize->index >= format->nframes)
1089 frame = &format->frame[fsize->index];
1090 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1091 fsize->discrete.width = frame->wWidth;
1092 fsize->discrete.height = frame->wHeight;
1096 static int uvc_ioctl_enum_frameintervals(struct file *file, void *fh,
1097 struct v4l2_frmivalenum *fival)
1099 struct uvc_fh *handle = fh;
1100 struct uvc_streaming *stream = handle->stream;
1101 struct uvc_format *format = NULL;
1102 struct uvc_frame *frame = NULL;
1105 /* Look for the given pixel format and frame size */
1106 for (i = 0; i < stream->nformats; i++) {
1107 if (stream->format[i].fcc == fival->pixel_format) {
1108 format = &stream->format[i];
1115 for (i = 0; i < format->nframes; i++) {
1116 if (format->frame[i].wWidth == fival->width &&
1117 format->frame[i].wHeight == fival->height) {
1118 frame = &format->frame[i];
1125 if (frame->bFrameIntervalType) {
1126 if (fival->index >= frame->bFrameIntervalType)
1129 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1130 fival->discrete.numerator =
1131 frame->dwFrameInterval[fival->index];
1132 fival->discrete.denominator = 10000000;
1133 uvc_simplify_fraction(&fival->discrete.numerator,
1134 &fival->discrete.denominator, 8, 333);
1136 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
1137 fival->stepwise.min.numerator = frame->dwFrameInterval[0];
1138 fival->stepwise.min.denominator = 10000000;
1139 fival->stepwise.max.numerator = frame->dwFrameInterval[1];
1140 fival->stepwise.max.denominator = 10000000;
1141 fival->stepwise.step.numerator = frame->dwFrameInterval[2];
1142 fival->stepwise.step.denominator = 10000000;
1143 uvc_simplify_fraction(&fival->stepwise.min.numerator,
1144 &fival->stepwise.min.denominator, 8, 333);
1145 uvc_simplify_fraction(&fival->stepwise.max.numerator,
1146 &fival->stepwise.max.denominator, 8, 333);
1147 uvc_simplify_fraction(&fival->stepwise.step.numerator,
1148 &fival->stepwise.step.denominator, 8, 333);
1154 static int uvc_ioctl_subscribe_event(struct v4l2_fh *fh,
1155 const struct v4l2_event_subscription *sub)
1157 switch (sub->type) {
1158 case V4L2_EVENT_CTRL:
1159 return v4l2_event_subscribe(fh, sub, 0, &uvc_ctrl_sub_ev_ops);
1165 static long uvc_ioctl_default(struct file *file, void *fh, bool valid_prio,
1166 unsigned int cmd, void *arg)
1168 struct uvc_fh *handle = fh;
1169 struct uvc_video_chain *chain = handle->chain;
1172 /* Dynamic controls. */
1173 case UVCIOC_CTRL_MAP:
1174 return uvc_ioctl_ctrl_map(chain, arg);
1176 case UVCIOC_CTRL_QUERY:
1177 return uvc_xu_ctrl_query(chain, arg);
1184 #ifdef CONFIG_COMPAT
1185 struct uvc_xu_control_mapping32 {
1196 compat_caddr_t menu_info;
1202 static int uvc_v4l2_get_xu_mapping(struct uvc_xu_control_mapping *kp,
1203 const struct uvc_xu_control_mapping32 __user *up)
1205 struct uvc_menu_info __user *umenus;
1206 struct uvc_menu_info __user *kmenus;
1209 if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
1210 __copy_from_user(kp, up, offsetof(typeof(*up), menu_info)) ||
1211 __get_user(kp->menu_count, &up->menu_count))
1214 memset(kp->reserved, 0, sizeof(kp->reserved));
1216 if (kp->menu_count == 0) {
1217 kp->menu_info = NULL;
1221 if (__get_user(p, &up->menu_info))
1223 umenus = compat_ptr(p);
1224 if (!access_ok(VERIFY_READ, umenus, kp->menu_count * sizeof(*umenus)))
1227 kmenus = compat_alloc_user_space(kp->menu_count * sizeof(*kmenus));
1230 kp->menu_info = kmenus;
1232 if (copy_in_user(kmenus, umenus, kp->menu_count * sizeof(*umenus)))
1238 static int uvc_v4l2_put_xu_mapping(const struct uvc_xu_control_mapping *kp,
1239 struct uvc_xu_control_mapping32 __user *up)
1241 struct uvc_menu_info __user *umenus;
1242 struct uvc_menu_info __user *kmenus = kp->menu_info;
1245 if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
1246 __copy_to_user(up, kp, offsetof(typeof(*up), menu_info)) ||
1247 __put_user(kp->menu_count, &up->menu_count))
1250 if (__clear_user(up->reserved, sizeof(up->reserved)))
1253 if (kp->menu_count == 0)
1256 if (get_user(p, &up->menu_info))
1258 umenus = compat_ptr(p);
1260 if (copy_in_user(umenus, kmenus, kp->menu_count * sizeof(*umenus)))
1266 struct uvc_xu_control_query32 {
1271 compat_caddr_t data;
1274 static int uvc_v4l2_get_xu_query(struct uvc_xu_control_query *kp,
1275 const struct uvc_xu_control_query32 __user *up)
1281 if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
1282 __copy_from_user(kp, up, offsetof(typeof(*up), data)))
1285 if (kp->size == 0) {
1290 if (__get_user(p, &up->data))
1292 udata = compat_ptr(p);
1293 if (!access_ok(VERIFY_READ, udata, kp->size))
1296 kdata = compat_alloc_user_space(kp->size);
1301 if (copy_in_user(kdata, udata, kp->size))
1307 static int uvc_v4l2_put_xu_query(const struct uvc_xu_control_query *kp,
1308 struct uvc_xu_control_query32 __user *up)
1311 u8 __user *kdata = kp->data;
1314 if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
1315 __copy_to_user(up, kp, offsetof(typeof(*up), data)))
1321 if (get_user(p, &up->data))
1323 udata = compat_ptr(p);
1324 if (!access_ok(VERIFY_READ, udata, kp->size))
1327 if (copy_in_user(udata, kdata, kp->size))
1333 #define UVCIOC_CTRL_MAP32 _IOWR('u', 0x20, struct uvc_xu_control_mapping32)
1334 #define UVCIOC_CTRL_QUERY32 _IOWR('u', 0x21, struct uvc_xu_control_query32)
1336 static long uvc_v4l2_compat_ioctl32(struct file *file,
1337 unsigned int cmd, unsigned long arg)
1340 struct uvc_xu_control_mapping xmap;
1341 struct uvc_xu_control_query xqry;
1343 void __user *up = compat_ptr(arg);
1344 mm_segment_t old_fs;
1348 case UVCIOC_CTRL_MAP32:
1349 cmd = UVCIOC_CTRL_MAP;
1350 ret = uvc_v4l2_get_xu_mapping(&karg.xmap, up);
1353 case UVCIOC_CTRL_QUERY32:
1354 cmd = UVCIOC_CTRL_QUERY;
1355 ret = uvc_v4l2_get_xu_query(&karg.xqry, up);
1359 return -ENOIOCTLCMD;
1364 ret = video_ioctl2(file, cmd, (unsigned long)&karg);
1371 case UVCIOC_CTRL_MAP:
1372 ret = uvc_v4l2_put_xu_mapping(&karg.xmap, up);
1375 case UVCIOC_CTRL_QUERY:
1376 ret = uvc_v4l2_put_xu_query(&karg.xqry, up);
1384 static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1385 size_t count, loff_t *ppos)
1387 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
1391 static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1393 struct uvc_fh *handle = file->private_data;
1394 struct uvc_streaming *stream = handle->stream;
1396 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
1398 return uvc_queue_mmap(&stream->queue, vma);
1401 static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
1403 struct uvc_fh *handle = file->private_data;
1404 struct uvc_streaming *stream = handle->stream;
1406 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
1408 return uvc_queue_poll(&stream->queue, file, wait);
1412 static unsigned long uvc_v4l2_get_unmapped_area(struct file *file,
1413 unsigned long addr, unsigned long len, unsigned long pgoff,
1414 unsigned long flags)
1416 struct uvc_fh *handle = file->private_data;
1417 struct uvc_streaming *stream = handle->stream;
1419 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_get_unmapped_area\n");
1421 return uvc_queue_get_unmapped_area(&stream->queue, pgoff);
1425 const struct v4l2_ioctl_ops uvc_ioctl_ops = {
1426 .vidioc_querycap = uvc_ioctl_querycap,
1427 .vidioc_enum_fmt_vid_cap = uvc_ioctl_enum_fmt_vid_cap,
1428 .vidioc_enum_fmt_vid_out = uvc_ioctl_enum_fmt_vid_out,
1429 .vidioc_g_fmt_vid_cap = uvc_ioctl_g_fmt_vid_cap,
1430 .vidioc_g_fmt_vid_out = uvc_ioctl_g_fmt_vid_out,
1431 .vidioc_s_fmt_vid_cap = uvc_ioctl_s_fmt_vid_cap,
1432 .vidioc_s_fmt_vid_out = uvc_ioctl_s_fmt_vid_out,
1433 .vidioc_try_fmt_vid_cap = uvc_ioctl_try_fmt_vid_cap,
1434 .vidioc_try_fmt_vid_out = uvc_ioctl_try_fmt_vid_out,
1435 .vidioc_reqbufs = uvc_ioctl_reqbufs,
1436 .vidioc_querybuf = uvc_ioctl_querybuf,
1437 .vidioc_qbuf = uvc_ioctl_qbuf,
1438 .vidioc_dqbuf = uvc_ioctl_dqbuf,
1439 .vidioc_create_bufs = uvc_ioctl_create_bufs,
1440 .vidioc_streamon = uvc_ioctl_streamon,
1441 .vidioc_streamoff = uvc_ioctl_streamoff,
1442 .vidioc_enum_input = uvc_ioctl_enum_input,
1443 .vidioc_g_input = uvc_ioctl_g_input,
1444 .vidioc_s_input = uvc_ioctl_s_input,
1445 .vidioc_queryctrl = uvc_ioctl_queryctrl,
1446 .vidioc_g_ctrl = uvc_ioctl_g_ctrl,
1447 .vidioc_s_ctrl = uvc_ioctl_s_ctrl,
1448 .vidioc_g_ext_ctrls = uvc_ioctl_g_ext_ctrls,
1449 .vidioc_s_ext_ctrls = uvc_ioctl_s_ext_ctrls,
1450 .vidioc_try_ext_ctrls = uvc_ioctl_try_ext_ctrls,
1451 .vidioc_querymenu = uvc_ioctl_querymenu,
1452 .vidioc_cropcap = uvc_ioctl_cropcap,
1453 .vidioc_g_parm = uvc_ioctl_g_parm,
1454 .vidioc_s_parm = uvc_ioctl_s_parm,
1455 .vidioc_enum_framesizes = uvc_ioctl_enum_framesizes,
1456 .vidioc_enum_frameintervals = uvc_ioctl_enum_frameintervals,
1457 .vidioc_subscribe_event = uvc_ioctl_subscribe_event,
1458 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1459 .vidioc_default = uvc_ioctl_default,
1462 const struct v4l2_file_operations uvc_fops = {
1463 .owner = THIS_MODULE,
1464 .open = uvc_v4l2_open,
1465 .release = uvc_v4l2_release,
1466 .unlocked_ioctl = video_ioctl2,
1467 #ifdef CONFIG_COMPAT
1468 .compat_ioctl32 = uvc_v4l2_compat_ioctl32,
1470 .read = uvc_v4l2_read,
1471 .mmap = uvc_v4l2_mmap,
1472 .poll = uvc_v4l2_poll,
1474 .get_unmapped_area = uvc_v4l2_get_unmapped_area,