1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * uvc_v4l2.c -- USB Video Class driver - V4L2 API
5 * Copyright (C) 2005-2010
6 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
9 #include <linux/compat.h>
10 #include <linux/kernel.h>
11 #include <linux/list.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/usb.h>
15 #include <linux/videodev2.h>
16 #include <linux/vmalloc.h>
18 #include <linux/wait.h>
19 #include <linux/atomic.h>
21 #include <media/v4l2-common.h>
22 #include <media/v4l2-ctrls.h>
23 #include <media/v4l2-event.h>
24 #include <media/v4l2-ioctl.h>
28 /* ------------------------------------------------------------------------
31 static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain,
32 struct uvc_xu_control_mapping *xmap)
34 struct uvc_control_mapping *map;
38 map = kzalloc(sizeof(*map), GFP_KERNEL);
43 memcpy(map->name, xmap->name, sizeof(map->name));
44 memcpy(map->entity, xmap->entity, sizeof(map->entity));
45 map->selector = xmap->selector;
46 map->size = xmap->size;
47 map->offset = xmap->offset;
48 map->v4l2_type = xmap->v4l2_type;
49 map->data_type = xmap->data_type;
51 switch (xmap->v4l2_type) {
52 case V4L2_CTRL_TYPE_INTEGER:
53 case V4L2_CTRL_TYPE_BOOLEAN:
54 case V4L2_CTRL_TYPE_BUTTON:
57 case V4L2_CTRL_TYPE_MENU:
58 /* Prevent excessive memory consumption, as well as integer
61 if (xmap->menu_count == 0 ||
62 xmap->menu_count > UVC_MAX_CONTROL_MENU_ENTRIES) {
67 size = xmap->menu_count * sizeof(*map->menu_info);
68 map->menu_info = memdup_user(xmap->menu_info, size);
69 if (IS_ERR(map->menu_info)) {
70 ret = PTR_ERR(map->menu_info);
74 map->menu_count = xmap->menu_count;
78 uvc_trace(UVC_TRACE_CONTROL, "Unsupported V4L2 control type "
79 "%u.\n", xmap->v4l2_type);
84 ret = uvc_ctrl_add_mapping(chain, map);
86 kfree(map->menu_info);
93 /* ------------------------------------------------------------------------
98 * Find the frame interval closest to the requested frame interval for the
99 * given frame format and size. This should be done by the device as part of
100 * the Video Probe and Commit negotiation, but some hardware don't implement
103 static u32 uvc_try_frame_interval(struct uvc_frame *frame, u32 interval)
107 if (frame->bFrameIntervalType) {
110 for (i = 0; i < frame->bFrameIntervalType; ++i) {
111 dist = interval > frame->dwFrameInterval[i]
112 ? interval - frame->dwFrameInterval[i]
113 : frame->dwFrameInterval[i] - interval;
121 interval = frame->dwFrameInterval[i-1];
123 const u32 min = frame->dwFrameInterval[0];
124 const u32 max = frame->dwFrameInterval[1];
125 const u32 step = frame->dwFrameInterval[2];
127 interval = min + (interval - min + step/2) / step * step;
135 static u32 uvc_v4l2_get_bytesperline(const struct uvc_format *format,
136 const struct uvc_frame *frame)
138 switch (format->fcc) {
139 case V4L2_PIX_FMT_NV12:
140 case V4L2_PIX_FMT_YVU420:
141 case V4L2_PIX_FMT_YUV420:
142 case V4L2_PIX_FMT_M420:
143 return frame->wWidth;
146 return format->bpp * frame->wWidth / 8;
150 static int uvc_v4l2_try_format(struct uvc_streaming *stream,
151 struct v4l2_format *fmt, struct uvc_streaming_control *probe,
152 struct uvc_format **uvc_format, struct uvc_frame **uvc_frame)
154 struct uvc_format *format = NULL;
155 struct uvc_frame *frame = NULL;
157 unsigned int d, maxd;
163 if (fmt->type != stream->type)
166 fcc = (u8 *)&fmt->fmt.pix.pixelformat;
167 uvc_trace(UVC_TRACE_FORMAT, "Trying format 0x%08x (%c%c%c%c): %ux%u.\n",
168 fmt->fmt.pix.pixelformat,
169 fcc[0], fcc[1], fcc[2], fcc[3],
170 fmt->fmt.pix.width, fmt->fmt.pix.height);
172 /* Check if the hardware supports the requested format, use the default
175 for (i = 0; i < stream->nformats; ++i) {
176 format = &stream->format[i];
177 if (format->fcc == fmt->fmt.pix.pixelformat)
181 if (i == stream->nformats) {
182 format = stream->def_format;
183 fmt->fmt.pix.pixelformat = format->fcc;
186 /* Find the closest image size. The distance between image sizes is
187 * the size in pixels of the non-overlapping regions between the
188 * requested size and the frame-specified size.
190 rw = fmt->fmt.pix.width;
191 rh = fmt->fmt.pix.height;
192 maxd = (unsigned int)-1;
194 for (i = 0; i < format->nframes; ++i) {
195 u16 w = format->frame[i].wWidth;
196 u16 h = format->frame[i].wHeight;
198 d = min(w, rw) * min(h, rh);
199 d = w*h + rw*rh - 2*d;
202 frame = &format->frame[i];
210 uvc_trace(UVC_TRACE_FORMAT, "Unsupported size %ux%u.\n",
211 fmt->fmt.pix.width, fmt->fmt.pix.height);
215 /* Use the default frame interval. */
216 interval = frame->dwDefaultFrameInterval;
217 uvc_trace(UVC_TRACE_FORMAT, "Using default frame interval %u.%u us "
218 "(%u.%u fps).\n", interval/10, interval%10, 10000000/interval,
219 (100000000/interval)%10);
221 /* Set the format index, frame index and frame interval. */
222 memset(probe, 0, sizeof(*probe));
223 probe->bmHint = 1; /* dwFrameInterval */
224 probe->bFormatIndex = format->index;
225 probe->bFrameIndex = frame->bFrameIndex;
226 probe->dwFrameInterval = uvc_try_frame_interval(frame, interval);
227 /* Some webcams stall the probe control set request when the
228 * dwMaxVideoFrameSize field is set to zero. The UVC specification
229 * clearly states that the field is read-only from the host, so this
230 * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by
231 * the webcam to work around the problem.
233 * The workaround could probably be enabled for all webcams, so the
234 * quirk can be removed if needed. It's currently useful to detect
235 * webcam bugs and fix them before they hit the market (providing
236 * developers test their webcams with the Linux driver as well as with
237 * the Windows driver).
239 mutex_lock(&stream->mutex);
240 if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS)
241 probe->dwMaxVideoFrameSize =
242 stream->ctrl.dwMaxVideoFrameSize;
244 /* Probe the device. */
245 ret = uvc_probe_video(stream, probe);
246 mutex_unlock(&stream->mutex);
250 fmt->fmt.pix.width = frame->wWidth;
251 fmt->fmt.pix.height = frame->wHeight;
252 fmt->fmt.pix.field = V4L2_FIELD_NONE;
253 fmt->fmt.pix.bytesperline = uvc_v4l2_get_bytesperline(format, frame);
254 fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize;
255 fmt->fmt.pix.colorspace = format->colorspace;
257 if (uvc_format != NULL)
258 *uvc_format = format;
259 if (uvc_frame != NULL)
266 static int uvc_v4l2_get_format(struct uvc_streaming *stream,
267 struct v4l2_format *fmt)
269 struct uvc_format *format;
270 struct uvc_frame *frame;
273 if (fmt->type != stream->type)
276 mutex_lock(&stream->mutex);
277 format = stream->cur_format;
278 frame = stream->cur_frame;
280 if (format == NULL || frame == NULL) {
285 fmt->fmt.pix.pixelformat = format->fcc;
286 fmt->fmt.pix.width = frame->wWidth;
287 fmt->fmt.pix.height = frame->wHeight;
288 fmt->fmt.pix.field = V4L2_FIELD_NONE;
289 fmt->fmt.pix.bytesperline = uvc_v4l2_get_bytesperline(format, frame);
290 fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize;
291 fmt->fmt.pix.colorspace = format->colorspace;
294 mutex_unlock(&stream->mutex);
298 static int uvc_v4l2_set_format(struct uvc_streaming *stream,
299 struct v4l2_format *fmt)
301 struct uvc_streaming_control probe;
302 struct uvc_format *format;
303 struct uvc_frame *frame;
306 if (fmt->type != stream->type)
309 ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame);
313 mutex_lock(&stream->mutex);
315 if (uvc_queue_allocated(&stream->queue)) {
320 stream->ctrl = probe;
321 stream->cur_format = format;
322 stream->cur_frame = frame;
325 mutex_unlock(&stream->mutex);
329 static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream,
330 struct v4l2_streamparm *parm)
332 u32 numerator, denominator;
334 if (parm->type != stream->type)
337 mutex_lock(&stream->mutex);
338 numerator = stream->ctrl.dwFrameInterval;
339 mutex_unlock(&stream->mutex);
341 denominator = 10000000;
342 uvc_simplify_fraction(&numerator, &denominator, 8, 333);
344 memset(parm, 0, sizeof(*parm));
345 parm->type = stream->type;
347 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
348 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
349 parm->parm.capture.capturemode = 0;
350 parm->parm.capture.timeperframe.numerator = numerator;
351 parm->parm.capture.timeperframe.denominator = denominator;
352 parm->parm.capture.extendedmode = 0;
353 parm->parm.capture.readbuffers = 0;
355 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
356 parm->parm.output.outputmode = 0;
357 parm->parm.output.timeperframe.numerator = numerator;
358 parm->parm.output.timeperframe.denominator = denominator;
364 static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
365 struct v4l2_streamparm *parm)
367 struct uvc_streaming_control probe;
368 struct v4l2_fract timeperframe;
369 struct uvc_format *format;
370 struct uvc_frame *frame;
375 if (parm->type != stream->type)
378 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
379 timeperframe = parm->parm.capture.timeperframe;
381 timeperframe = parm->parm.output.timeperframe;
383 interval = uvc_fraction_to_interval(timeperframe.numerator,
384 timeperframe.denominator);
385 uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
386 timeperframe.numerator, timeperframe.denominator, interval);
388 mutex_lock(&stream->mutex);
390 if (uvc_queue_streaming(&stream->queue)) {
391 mutex_unlock(&stream->mutex);
395 format = stream->cur_format;
396 frame = stream->cur_frame;
397 probe = stream->ctrl;
398 probe.dwFrameInterval = uvc_try_frame_interval(frame, interval);
399 maxd = abs((s32)probe.dwFrameInterval - interval);
401 /* Try frames with matching size to find the best frame interval. */
402 for (i = 0; i < format->nframes && maxd != 0; i++) {
405 if (&format->frame[i] == stream->cur_frame)
408 if (format->frame[i].wWidth != stream->cur_frame->wWidth ||
409 format->frame[i].wHeight != stream->cur_frame->wHeight)
412 ival = uvc_try_frame_interval(&format->frame[i], interval);
413 d = abs((s32)ival - interval);
417 frame = &format->frame[i];
418 probe.bFrameIndex = frame->bFrameIndex;
419 probe.dwFrameInterval = ival;
423 /* Probe the device with the new settings. */
424 ret = uvc_probe_video(stream, &probe);
426 mutex_unlock(&stream->mutex);
430 stream->ctrl = probe;
431 stream->cur_frame = frame;
432 mutex_unlock(&stream->mutex);
434 /* Return the actual frame period. */
435 timeperframe.numerator = probe.dwFrameInterval;
436 timeperframe.denominator = 10000000;
437 uvc_simplify_fraction(&timeperframe.numerator,
438 &timeperframe.denominator, 8, 333);
440 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
441 parm->parm.capture.timeperframe = timeperframe;
443 parm->parm.output.timeperframe = timeperframe;
448 /* ------------------------------------------------------------------------
449 * Privilege management
453 * Privilege management is the multiple-open implementation basis. The current
454 * implementation is completely transparent for the end-user and doesn't
455 * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls.
456 * Those ioctls enable finer control on the device (by making possible for a
457 * user to request exclusive access to a device), but are not mature yet.
458 * Switching to the V4L2 priority mechanism might be considered in the future
459 * if this situation changes.
461 * Each open instance of a UVC device can either be in a privileged or
462 * unprivileged state. Only a single instance can be in a privileged state at
463 * a given time. Trying to perform an operation that requires privileges will
464 * automatically acquire the required privileges if possible, or return -EBUSY
465 * otherwise. Privileges are dismissed when closing the instance or when
466 * freeing the video buffers using VIDIOC_REQBUFS.
468 * Operations that require privileges are:
475 static int uvc_acquire_privileges(struct uvc_fh *handle)
477 /* Always succeed if the handle is already privileged. */
478 if (handle->state == UVC_HANDLE_ACTIVE)
481 /* Check if the device already has a privileged handle. */
482 if (atomic_inc_return(&handle->stream->active) != 1) {
483 atomic_dec(&handle->stream->active);
487 handle->state = UVC_HANDLE_ACTIVE;
491 static void uvc_dismiss_privileges(struct uvc_fh *handle)
493 if (handle->state == UVC_HANDLE_ACTIVE)
494 atomic_dec(&handle->stream->active);
496 handle->state = UVC_HANDLE_PASSIVE;
499 static int uvc_has_privileges(struct uvc_fh *handle)
501 return handle->state == UVC_HANDLE_ACTIVE;
504 /* ------------------------------------------------------------------------
505 * V4L2 file operations
508 static int uvc_v4l2_open(struct file *file)
510 struct uvc_streaming *stream;
511 struct uvc_fh *handle;
514 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
515 stream = video_drvdata(file);
517 ret = usb_autopm_get_interface(stream->dev->intf);
521 /* Create the device handle. */
522 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
523 if (handle == NULL) {
524 usb_autopm_put_interface(stream->dev->intf);
528 mutex_lock(&stream->dev->lock);
529 if (stream->dev->users == 0) {
530 ret = uvc_status_start(stream->dev, GFP_KERNEL);
532 mutex_unlock(&stream->dev->lock);
533 usb_autopm_put_interface(stream->dev->intf);
539 stream->dev->users++;
540 mutex_unlock(&stream->dev->lock);
542 v4l2_fh_init(&handle->vfh, &stream->vdev);
543 v4l2_fh_add(&handle->vfh);
544 handle->chain = stream->chain;
545 handle->stream = stream;
546 handle->state = UVC_HANDLE_PASSIVE;
547 file->private_data = handle;
552 static int uvc_v4l2_release(struct file *file)
554 struct uvc_fh *handle = file->private_data;
555 struct uvc_streaming *stream = handle->stream;
557 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");
559 /* Only free resources if this is a privileged handle. */
560 if (uvc_has_privileges(handle))
561 uvc_queue_release(&stream->queue);
563 /* Release the file handle. */
564 uvc_dismiss_privileges(handle);
565 v4l2_fh_del(&handle->vfh);
566 v4l2_fh_exit(&handle->vfh);
568 file->private_data = NULL;
570 mutex_lock(&stream->dev->lock);
571 if (--stream->dev->users == 0)
572 uvc_status_stop(stream->dev);
573 mutex_unlock(&stream->dev->lock);
575 usb_autopm_put_interface(stream->dev->intf);
579 static int uvc_ioctl_querycap(struct file *file, void *fh,
580 struct v4l2_capability *cap)
582 struct video_device *vdev = video_devdata(file);
583 struct uvc_fh *handle = file->private_data;
584 struct uvc_video_chain *chain = handle->chain;
585 struct uvc_streaming *stream = handle->stream;
587 strscpy(cap->driver, "uvcvideo", sizeof(cap->driver));
588 strscpy(cap->card, vdev->name, sizeof(cap->card));
589 usb_make_path(stream->dev->udev, cap->bus_info, sizeof(cap->bus_info));
590 cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
596 static int uvc_ioctl_enum_fmt(struct uvc_streaming *stream,
597 struct v4l2_fmtdesc *fmt)
599 struct uvc_format *format;
600 enum v4l2_buf_type type = fmt->type;
601 u32 index = fmt->index;
603 if (fmt->type != stream->type || fmt->index >= stream->nformats)
606 memset(fmt, 0, sizeof(*fmt));
610 format = &stream->format[fmt->index];
612 if (format->flags & UVC_FMT_FLAG_COMPRESSED)
613 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
614 strscpy(fmt->description, format->name, sizeof(fmt->description));
615 fmt->description[sizeof(fmt->description) - 1] = 0;
616 fmt->pixelformat = format->fcc;
620 static int uvc_ioctl_enum_fmt_vid_cap(struct file *file, void *fh,
621 struct v4l2_fmtdesc *fmt)
623 struct uvc_fh *handle = fh;
624 struct uvc_streaming *stream = handle->stream;
626 return uvc_ioctl_enum_fmt(stream, fmt);
629 static int uvc_ioctl_enum_fmt_vid_out(struct file *file, void *fh,
630 struct v4l2_fmtdesc *fmt)
632 struct uvc_fh *handle = fh;
633 struct uvc_streaming *stream = handle->stream;
635 return uvc_ioctl_enum_fmt(stream, fmt);
638 static int uvc_ioctl_g_fmt_vid_cap(struct file *file, void *fh,
639 struct v4l2_format *fmt)
641 struct uvc_fh *handle = fh;
642 struct uvc_streaming *stream = handle->stream;
644 return uvc_v4l2_get_format(stream, fmt);
647 static int uvc_ioctl_g_fmt_vid_out(struct file *file, void *fh,
648 struct v4l2_format *fmt)
650 struct uvc_fh *handle = fh;
651 struct uvc_streaming *stream = handle->stream;
653 return uvc_v4l2_get_format(stream, fmt);
656 static int uvc_ioctl_s_fmt_vid_cap(struct file *file, void *fh,
657 struct v4l2_format *fmt)
659 struct uvc_fh *handle = fh;
660 struct uvc_streaming *stream = handle->stream;
663 ret = uvc_acquire_privileges(handle);
667 return uvc_v4l2_set_format(stream, fmt);
670 static int uvc_ioctl_s_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;
677 ret = uvc_acquire_privileges(handle);
681 return uvc_v4l2_set_format(stream, fmt);
684 static int uvc_ioctl_try_fmt_vid_cap(struct file *file, void *fh,
685 struct v4l2_format *fmt)
687 struct uvc_fh *handle = fh;
688 struct uvc_streaming *stream = handle->stream;
689 struct uvc_streaming_control probe;
691 return uvc_v4l2_try_format(stream, fmt, &probe, NULL, NULL);
694 static int uvc_ioctl_try_fmt_vid_out(struct file *file, void *fh,
695 struct v4l2_format *fmt)
697 struct uvc_fh *handle = fh;
698 struct uvc_streaming *stream = handle->stream;
699 struct uvc_streaming_control probe;
701 return uvc_v4l2_try_format(stream, fmt, &probe, NULL, NULL);
704 static int uvc_ioctl_reqbufs(struct file *file, void *fh,
705 struct v4l2_requestbuffers *rb)
707 struct uvc_fh *handle = fh;
708 struct uvc_streaming *stream = handle->stream;
711 ret = uvc_acquire_privileges(handle);
715 mutex_lock(&stream->mutex);
716 ret = uvc_request_buffers(&stream->queue, rb);
717 mutex_unlock(&stream->mutex);
722 uvc_dismiss_privileges(handle);
727 static int uvc_ioctl_querybuf(struct file *file, void *fh,
728 struct v4l2_buffer *buf)
730 struct uvc_fh *handle = fh;
731 struct uvc_streaming *stream = handle->stream;
733 if (!uvc_has_privileges(handle))
736 return uvc_query_buffer(&stream->queue, buf);
739 static int uvc_ioctl_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
741 struct uvc_fh *handle = fh;
742 struct uvc_streaming *stream = handle->stream;
744 if (!uvc_has_privileges(handle))
747 return uvc_queue_buffer(&stream->queue,
748 stream->vdev.v4l2_dev->mdev, buf);
751 static int uvc_ioctl_expbuf(struct file *file, void *fh,
752 struct v4l2_exportbuffer *exp)
754 struct uvc_fh *handle = fh;
755 struct uvc_streaming *stream = handle->stream;
757 if (!uvc_has_privileges(handle))
760 return uvc_export_buffer(&stream->queue, exp);
763 static int uvc_ioctl_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
765 struct uvc_fh *handle = fh;
766 struct uvc_streaming *stream = handle->stream;
768 if (!uvc_has_privileges(handle))
771 return uvc_dequeue_buffer(&stream->queue, buf,
772 file->f_flags & O_NONBLOCK);
775 static int uvc_ioctl_create_bufs(struct file *file, void *fh,
776 struct v4l2_create_buffers *cb)
778 struct uvc_fh *handle = fh;
779 struct uvc_streaming *stream = handle->stream;
782 ret = uvc_acquire_privileges(handle);
786 return uvc_create_buffers(&stream->queue, cb);
789 static int uvc_ioctl_streamon(struct file *file, void *fh,
790 enum v4l2_buf_type type)
792 struct uvc_fh *handle = fh;
793 struct uvc_streaming *stream = handle->stream;
796 if (!uvc_has_privileges(handle))
799 mutex_lock(&stream->mutex);
800 ret = uvc_queue_streamon(&stream->queue, type);
801 mutex_unlock(&stream->mutex);
806 static int uvc_ioctl_streamoff(struct file *file, void *fh,
807 enum v4l2_buf_type type)
809 struct uvc_fh *handle = fh;
810 struct uvc_streaming *stream = handle->stream;
812 if (!uvc_has_privileges(handle))
815 mutex_lock(&stream->mutex);
816 uvc_queue_streamoff(&stream->queue, type);
817 mutex_unlock(&stream->mutex);
822 static int uvc_ioctl_enum_input(struct file *file, void *fh,
823 struct v4l2_input *input)
825 struct uvc_fh *handle = fh;
826 struct uvc_video_chain *chain = handle->chain;
827 const struct uvc_entity *selector = chain->selector;
828 struct uvc_entity *iterm = NULL;
829 u32 index = input->index;
832 if (selector == NULL ||
833 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
836 list_for_each_entry(iterm, &chain->entities, chain) {
837 if (UVC_ENTITY_IS_ITERM(iterm))
841 } else if (index < selector->bNrInPins) {
842 pin = selector->baSourceID[index];
843 list_for_each_entry(iterm, &chain->entities, chain) {
844 if (!UVC_ENTITY_IS_ITERM(iterm))
846 if (iterm->id == pin)
851 if (iterm == NULL || iterm->id != pin)
854 memset(input, 0, sizeof(*input));
855 input->index = index;
856 strscpy(input->name, iterm->name, sizeof(input->name));
857 if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
858 input->type = V4L2_INPUT_TYPE_CAMERA;
863 static int uvc_ioctl_g_input(struct file *file, void *fh, unsigned int *input)
865 struct uvc_fh *handle = fh;
866 struct uvc_video_chain *chain = handle->chain;
870 if (chain->selector == NULL ||
871 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
876 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, chain->selector->id,
877 chain->dev->intfnum, UVC_SU_INPUT_SELECT_CONTROL,
886 static int uvc_ioctl_s_input(struct file *file, void *fh, unsigned int input)
888 struct uvc_fh *handle = fh;
889 struct uvc_video_chain *chain = handle->chain;
893 ret = uvc_acquire_privileges(handle);
897 if (chain->selector == NULL ||
898 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
904 if (input >= chain->selector->bNrInPins)
908 return uvc_query_ctrl(chain->dev, UVC_SET_CUR, chain->selector->id,
909 chain->dev->intfnum, UVC_SU_INPUT_SELECT_CONTROL,
913 static int uvc_ioctl_queryctrl(struct file *file, void *fh,
914 struct v4l2_queryctrl *qc)
916 struct uvc_fh *handle = fh;
917 struct uvc_video_chain *chain = handle->chain;
919 return uvc_query_v4l2_ctrl(chain, qc);
922 static int uvc_ioctl_query_ext_ctrl(struct file *file, void *fh,
923 struct v4l2_query_ext_ctrl *qec)
925 struct uvc_fh *handle = fh;
926 struct uvc_video_chain *chain = handle->chain;
927 struct v4l2_queryctrl qc = { qec->id };
930 ret = uvc_query_v4l2_ctrl(chain, &qc);
936 strscpy(qec->name, qc.name, sizeof(qec->name));
937 qec->minimum = qc.minimum;
938 qec->maximum = qc.maximum;
940 qec->default_value = qc.default_value;
941 qec->flags = qc.flags;
945 memset(qec->dims, 0, sizeof(qec->dims));
946 memset(qec->reserved, 0, sizeof(qec->reserved));
951 static int uvc_ioctl_g_ctrl(struct file *file, void *fh,
952 struct v4l2_control *ctrl)
954 struct uvc_fh *handle = fh;
955 struct uvc_video_chain *chain = handle->chain;
956 struct v4l2_ext_control xctrl;
959 memset(&xctrl, 0, sizeof(xctrl));
962 ret = uvc_ctrl_begin(chain);
966 ret = uvc_ctrl_get(chain, &xctrl);
967 uvc_ctrl_rollback(handle);
971 ctrl->value = xctrl.value;
975 static int uvc_ioctl_s_ctrl(struct file *file, void *fh,
976 struct v4l2_control *ctrl)
978 struct uvc_fh *handle = fh;
979 struct uvc_video_chain *chain = handle->chain;
980 struct v4l2_ext_control xctrl;
983 memset(&xctrl, 0, sizeof(xctrl));
985 xctrl.value = ctrl->value;
987 ret = uvc_ctrl_begin(chain);
991 ret = uvc_ctrl_set(handle, &xctrl);
993 uvc_ctrl_rollback(handle);
997 ret = uvc_ctrl_commit(handle, &xctrl, 1);
1001 ctrl->value = xctrl.value;
1005 static int uvc_ioctl_g_ext_ctrls(struct file *file, void *fh,
1006 struct v4l2_ext_controls *ctrls)
1008 struct uvc_fh *handle = fh;
1009 struct uvc_video_chain *chain = handle->chain;
1010 struct v4l2_ext_control *ctrl = ctrls->controls;
1014 if (ctrls->which == V4L2_CTRL_WHICH_DEF_VAL) {
1015 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
1016 struct v4l2_queryctrl qc = { .id = ctrl->id };
1018 ret = uvc_query_v4l2_ctrl(chain, &qc);
1020 ctrls->error_idx = i;
1024 ctrl->value = qc.default_value;
1030 ret = uvc_ctrl_begin(chain);
1034 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
1035 ret = uvc_ctrl_get(chain, ctrl);
1037 uvc_ctrl_rollback(handle);
1038 ctrls->error_idx = i;
1043 ctrls->error_idx = 0;
1045 return uvc_ctrl_rollback(handle);
1048 static int uvc_ioctl_s_try_ext_ctrls(struct uvc_fh *handle,
1049 struct v4l2_ext_controls *ctrls,
1052 struct v4l2_ext_control *ctrl = ctrls->controls;
1053 struct uvc_video_chain *chain = handle->chain;
1057 /* Default value cannot be changed */
1058 if (ctrls->which == V4L2_CTRL_WHICH_DEF_VAL)
1061 ret = uvc_ctrl_begin(chain);
1065 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
1066 ret = uvc_ctrl_set(handle, ctrl);
1068 uvc_ctrl_rollback(handle);
1069 ctrls->error_idx = commit ? ctrls->count : i;
1074 ctrls->error_idx = 0;
1077 return uvc_ctrl_commit(handle, ctrls->controls, ctrls->count);
1079 return uvc_ctrl_rollback(handle);
1082 static int uvc_ioctl_s_ext_ctrls(struct file *file, void *fh,
1083 struct v4l2_ext_controls *ctrls)
1085 struct uvc_fh *handle = fh;
1087 return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, true);
1090 static int uvc_ioctl_try_ext_ctrls(struct file *file, void *fh,
1091 struct v4l2_ext_controls *ctrls)
1093 struct uvc_fh *handle = fh;
1095 return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, false);
1098 static int uvc_ioctl_querymenu(struct file *file, void *fh,
1099 struct v4l2_querymenu *qm)
1101 struct uvc_fh *handle = fh;
1102 struct uvc_video_chain *chain = handle->chain;
1104 return uvc_query_v4l2_menu(chain, qm);
1107 static int uvc_ioctl_g_selection(struct file *file, void *fh,
1108 struct v4l2_selection *sel)
1110 struct uvc_fh *handle = fh;
1111 struct uvc_streaming *stream = handle->stream;
1113 if (sel->type != stream->type)
1116 switch (sel->target) {
1117 case V4L2_SEL_TGT_CROP_DEFAULT:
1118 case V4L2_SEL_TGT_CROP_BOUNDS:
1119 if (stream->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1122 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1123 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1124 if (stream->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1133 mutex_lock(&stream->mutex);
1134 sel->r.width = stream->cur_frame->wWidth;
1135 sel->r.height = stream->cur_frame->wHeight;
1136 mutex_unlock(&stream->mutex);
1141 static int uvc_ioctl_g_parm(struct file *file, void *fh,
1142 struct v4l2_streamparm *parm)
1144 struct uvc_fh *handle = fh;
1145 struct uvc_streaming *stream = handle->stream;
1147 return uvc_v4l2_get_streamparm(stream, parm);
1150 static int uvc_ioctl_s_parm(struct file *file, void *fh,
1151 struct v4l2_streamparm *parm)
1153 struct uvc_fh *handle = fh;
1154 struct uvc_streaming *stream = handle->stream;
1157 ret = uvc_acquire_privileges(handle);
1161 return uvc_v4l2_set_streamparm(stream, parm);
1164 static int uvc_ioctl_enum_framesizes(struct file *file, void *fh,
1165 struct v4l2_frmsizeenum *fsize)
1167 struct uvc_fh *handle = fh;
1168 struct uvc_streaming *stream = handle->stream;
1169 struct uvc_format *format = NULL;
1170 struct uvc_frame *frame = NULL;
1174 /* Look for the given pixel format */
1175 for (i = 0; i < stream->nformats; i++) {
1176 if (stream->format[i].fcc == fsize->pixel_format) {
1177 format = &stream->format[i];
1184 /* Skip duplicate frame sizes */
1185 for (i = 0, index = 0; i < format->nframes; i++) {
1186 if (frame && frame->wWidth == format->frame[i].wWidth &&
1187 frame->wHeight == format->frame[i].wHeight)
1189 frame = &format->frame[i];
1190 if (index == fsize->index)
1195 if (i == format->nframes)
1198 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1199 fsize->discrete.width = frame->wWidth;
1200 fsize->discrete.height = frame->wHeight;
1204 static int uvc_ioctl_enum_frameintervals(struct file *file, void *fh,
1205 struct v4l2_frmivalenum *fival)
1207 struct uvc_fh *handle = fh;
1208 struct uvc_streaming *stream = handle->stream;
1209 struct uvc_format *format = NULL;
1210 struct uvc_frame *frame = NULL;
1211 unsigned int nintervals;
1215 /* Look for the given pixel format and frame size */
1216 for (i = 0; i < stream->nformats; i++) {
1217 if (stream->format[i].fcc == fival->pixel_format) {
1218 format = &stream->format[i];
1225 index = fival->index;
1226 for (i = 0; i < format->nframes; i++) {
1227 if (format->frame[i].wWidth == fival->width &&
1228 format->frame[i].wHeight == fival->height) {
1229 frame = &format->frame[i];
1230 nintervals = frame->bFrameIntervalType ?: 1;
1231 if (index < nintervals)
1233 index -= nintervals;
1236 if (i == format->nframes)
1239 if (frame->bFrameIntervalType) {
1240 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1241 fival->discrete.numerator =
1242 frame->dwFrameInterval[index];
1243 fival->discrete.denominator = 10000000;
1244 uvc_simplify_fraction(&fival->discrete.numerator,
1245 &fival->discrete.denominator, 8, 333);
1247 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
1248 fival->stepwise.min.numerator = frame->dwFrameInterval[0];
1249 fival->stepwise.min.denominator = 10000000;
1250 fival->stepwise.max.numerator = frame->dwFrameInterval[1];
1251 fival->stepwise.max.denominator = 10000000;
1252 fival->stepwise.step.numerator = frame->dwFrameInterval[2];
1253 fival->stepwise.step.denominator = 10000000;
1254 uvc_simplify_fraction(&fival->stepwise.min.numerator,
1255 &fival->stepwise.min.denominator, 8, 333);
1256 uvc_simplify_fraction(&fival->stepwise.max.numerator,
1257 &fival->stepwise.max.denominator, 8, 333);
1258 uvc_simplify_fraction(&fival->stepwise.step.numerator,
1259 &fival->stepwise.step.denominator, 8, 333);
1265 static int uvc_ioctl_subscribe_event(struct v4l2_fh *fh,
1266 const struct v4l2_event_subscription *sub)
1268 switch (sub->type) {
1269 case V4L2_EVENT_CTRL:
1270 return v4l2_event_subscribe(fh, sub, 0, &uvc_ctrl_sub_ev_ops);
1276 static long uvc_ioctl_default(struct file *file, void *fh, bool valid_prio,
1277 unsigned int cmd, void *arg)
1279 struct uvc_fh *handle = fh;
1280 struct uvc_video_chain *chain = handle->chain;
1283 /* Dynamic controls. */
1284 case UVCIOC_CTRL_MAP:
1285 return uvc_ioctl_ctrl_map(chain, arg);
1287 case UVCIOC_CTRL_QUERY:
1288 return uvc_xu_ctrl_query(chain, arg);
1295 #ifdef CONFIG_COMPAT
1296 struct uvc_xu_control_mapping32 {
1307 compat_caddr_t menu_info;
1313 static int uvc_v4l2_get_xu_mapping(struct uvc_xu_control_mapping *kp,
1314 const struct uvc_xu_control_mapping32 __user *up)
1316 struct uvc_xu_control_mapping32 *p = (void *)kp;
1317 compat_caddr_t info;
1320 if (copy_from_user(p, up, sizeof(*p)))
1323 count = p->menu_count;
1324 info = p->menu_info;
1326 memset(kp->reserved, 0, sizeof(kp->reserved));
1327 kp->menu_info = count ? compat_ptr(info) : NULL;
1328 kp->menu_count = count;
1332 static int uvc_v4l2_put_xu_mapping(const struct uvc_xu_control_mapping *kp,
1333 struct uvc_xu_control_mapping32 __user *up)
1335 if (copy_to_user(up, kp, offsetof(typeof(*up), menu_info)) ||
1336 put_user(kp->menu_count, &up->menu_count))
1339 if (clear_user(up->reserved, sizeof(up->reserved)))
1345 struct uvc_xu_control_query32 {
1350 compat_caddr_t data;
1353 static int uvc_v4l2_get_xu_query(struct uvc_xu_control_query *kp,
1354 const struct uvc_xu_control_query32 __user *up)
1356 struct uvc_xu_control_query32 v;
1358 if (copy_from_user(&v, up, sizeof(v)))
1361 *kp = (struct uvc_xu_control_query){
1363 .selector = v.selector,
1366 .data = v.size ? compat_ptr(v.data) : NULL
1371 static int uvc_v4l2_put_xu_query(const struct uvc_xu_control_query *kp,
1372 struct uvc_xu_control_query32 __user *up)
1374 if (copy_to_user(up, kp, offsetof(typeof(*up), data)))
1379 #define UVCIOC_CTRL_MAP32 _IOWR('u', 0x20, struct uvc_xu_control_mapping32)
1380 #define UVCIOC_CTRL_QUERY32 _IOWR('u', 0x21, struct uvc_xu_control_query32)
1382 static long uvc_v4l2_compat_ioctl32(struct file *file,
1383 unsigned int cmd, unsigned long arg)
1385 struct uvc_fh *handle = file->private_data;
1387 struct uvc_xu_control_mapping xmap;
1388 struct uvc_xu_control_query xqry;
1390 void __user *up = compat_ptr(arg);
1394 case UVCIOC_CTRL_MAP32:
1395 ret = uvc_v4l2_get_xu_mapping(&karg.xmap, up);
1398 ret = uvc_ioctl_ctrl_map(handle->chain, &karg.xmap);
1401 ret = uvc_v4l2_put_xu_mapping(&karg.xmap, up);
1407 case UVCIOC_CTRL_QUERY32:
1408 ret = uvc_v4l2_get_xu_query(&karg.xqry, up);
1411 ret = uvc_xu_ctrl_query(handle->chain, &karg.xqry);
1414 ret = uvc_v4l2_put_xu_query(&karg.xqry, up);
1420 return -ENOIOCTLCMD;
1427 static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1428 size_t count, loff_t *ppos)
1430 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
1434 static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1436 struct uvc_fh *handle = file->private_data;
1437 struct uvc_streaming *stream = handle->stream;
1439 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
1441 return uvc_queue_mmap(&stream->queue, vma);
1444 static __poll_t uvc_v4l2_poll(struct file *file, poll_table *wait)
1446 struct uvc_fh *handle = file->private_data;
1447 struct uvc_streaming *stream = handle->stream;
1449 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
1451 return uvc_queue_poll(&stream->queue, file, wait);
1455 static unsigned long uvc_v4l2_get_unmapped_area(struct file *file,
1456 unsigned long addr, unsigned long len, unsigned long pgoff,
1457 unsigned long flags)
1459 struct uvc_fh *handle = file->private_data;
1460 struct uvc_streaming *stream = handle->stream;
1462 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_get_unmapped_area\n");
1464 return uvc_queue_get_unmapped_area(&stream->queue, pgoff);
1468 const struct v4l2_ioctl_ops uvc_ioctl_ops = {
1469 .vidioc_querycap = uvc_ioctl_querycap,
1470 .vidioc_enum_fmt_vid_cap = uvc_ioctl_enum_fmt_vid_cap,
1471 .vidioc_enum_fmt_vid_out = uvc_ioctl_enum_fmt_vid_out,
1472 .vidioc_g_fmt_vid_cap = uvc_ioctl_g_fmt_vid_cap,
1473 .vidioc_g_fmt_vid_out = uvc_ioctl_g_fmt_vid_out,
1474 .vidioc_s_fmt_vid_cap = uvc_ioctl_s_fmt_vid_cap,
1475 .vidioc_s_fmt_vid_out = uvc_ioctl_s_fmt_vid_out,
1476 .vidioc_try_fmt_vid_cap = uvc_ioctl_try_fmt_vid_cap,
1477 .vidioc_try_fmt_vid_out = uvc_ioctl_try_fmt_vid_out,
1478 .vidioc_reqbufs = uvc_ioctl_reqbufs,
1479 .vidioc_querybuf = uvc_ioctl_querybuf,
1480 .vidioc_qbuf = uvc_ioctl_qbuf,
1481 .vidioc_expbuf = uvc_ioctl_expbuf,
1482 .vidioc_dqbuf = uvc_ioctl_dqbuf,
1483 .vidioc_create_bufs = uvc_ioctl_create_bufs,
1484 .vidioc_streamon = uvc_ioctl_streamon,
1485 .vidioc_streamoff = uvc_ioctl_streamoff,
1486 .vidioc_enum_input = uvc_ioctl_enum_input,
1487 .vidioc_g_input = uvc_ioctl_g_input,
1488 .vidioc_s_input = uvc_ioctl_s_input,
1489 .vidioc_queryctrl = uvc_ioctl_queryctrl,
1490 .vidioc_query_ext_ctrl = uvc_ioctl_query_ext_ctrl,
1491 .vidioc_g_ctrl = uvc_ioctl_g_ctrl,
1492 .vidioc_s_ctrl = uvc_ioctl_s_ctrl,
1493 .vidioc_g_ext_ctrls = uvc_ioctl_g_ext_ctrls,
1494 .vidioc_s_ext_ctrls = uvc_ioctl_s_ext_ctrls,
1495 .vidioc_try_ext_ctrls = uvc_ioctl_try_ext_ctrls,
1496 .vidioc_querymenu = uvc_ioctl_querymenu,
1497 .vidioc_g_selection = uvc_ioctl_g_selection,
1498 .vidioc_g_parm = uvc_ioctl_g_parm,
1499 .vidioc_s_parm = uvc_ioctl_s_parm,
1500 .vidioc_enum_framesizes = uvc_ioctl_enum_framesizes,
1501 .vidioc_enum_frameintervals = uvc_ioctl_enum_frameintervals,
1502 .vidioc_subscribe_event = uvc_ioctl_subscribe_event,
1503 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1504 .vidioc_default = uvc_ioctl_default,
1507 const struct v4l2_file_operations uvc_fops = {
1508 .owner = THIS_MODULE,
1509 .open = uvc_v4l2_open,
1510 .release = uvc_v4l2_release,
1511 .unlocked_ioctl = video_ioctl2,
1512 #ifdef CONFIG_COMPAT
1513 .compat_ioctl32 = uvc_v4l2_compat_ioctl32,
1515 .read = uvc_v4l2_read,
1516 .mmap = uvc_v4l2_mmap,
1517 .poll = uvc_v4l2_poll,
1519 .get_unmapped_area = uvc_v4l2_get_unmapped_area,