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;
256 fmt->fmt.pix.priv = 0;
258 if (uvc_format != NULL)
259 *uvc_format = format;
260 if (uvc_frame != NULL)
267 static int uvc_v4l2_get_format(struct uvc_streaming *stream,
268 struct v4l2_format *fmt)
270 struct uvc_format *format;
271 struct uvc_frame *frame;
274 if (fmt->type != stream->type)
277 mutex_lock(&stream->mutex);
278 format = stream->cur_format;
279 frame = stream->cur_frame;
281 if (format == NULL || frame == NULL) {
286 fmt->fmt.pix.pixelformat = format->fcc;
287 fmt->fmt.pix.width = frame->wWidth;
288 fmt->fmt.pix.height = frame->wHeight;
289 fmt->fmt.pix.field = V4L2_FIELD_NONE;
290 fmt->fmt.pix.bytesperline = uvc_v4l2_get_bytesperline(format, frame);
291 fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize;
292 fmt->fmt.pix.colorspace = format->colorspace;
293 fmt->fmt.pix.priv = 0;
296 mutex_unlock(&stream->mutex);
300 static int uvc_v4l2_set_format(struct uvc_streaming *stream,
301 struct v4l2_format *fmt)
303 struct uvc_streaming_control probe;
304 struct uvc_format *format;
305 struct uvc_frame *frame;
308 if (fmt->type != stream->type)
311 ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame);
315 mutex_lock(&stream->mutex);
317 if (uvc_queue_allocated(&stream->queue)) {
322 stream->ctrl = probe;
323 stream->cur_format = format;
324 stream->cur_frame = frame;
327 mutex_unlock(&stream->mutex);
331 static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream,
332 struct v4l2_streamparm *parm)
334 u32 numerator, denominator;
336 if (parm->type != stream->type)
339 mutex_lock(&stream->mutex);
340 numerator = stream->ctrl.dwFrameInterval;
341 mutex_unlock(&stream->mutex);
343 denominator = 10000000;
344 uvc_simplify_fraction(&numerator, &denominator, 8, 333);
346 memset(parm, 0, sizeof(*parm));
347 parm->type = stream->type;
349 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
350 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
351 parm->parm.capture.capturemode = 0;
352 parm->parm.capture.timeperframe.numerator = numerator;
353 parm->parm.capture.timeperframe.denominator = denominator;
354 parm->parm.capture.extendedmode = 0;
355 parm->parm.capture.readbuffers = 0;
357 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
358 parm->parm.output.outputmode = 0;
359 parm->parm.output.timeperframe.numerator = numerator;
360 parm->parm.output.timeperframe.denominator = denominator;
366 static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
367 struct v4l2_streamparm *parm)
369 struct uvc_streaming_control probe;
370 struct v4l2_fract timeperframe;
371 struct uvc_format *format;
372 struct uvc_frame *frame;
377 if (parm->type != stream->type)
380 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
381 timeperframe = parm->parm.capture.timeperframe;
383 timeperframe = parm->parm.output.timeperframe;
385 interval = uvc_fraction_to_interval(timeperframe.numerator,
386 timeperframe.denominator);
387 uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
388 timeperframe.numerator, timeperframe.denominator, interval);
390 mutex_lock(&stream->mutex);
392 if (uvc_queue_streaming(&stream->queue)) {
393 mutex_unlock(&stream->mutex);
397 format = stream->cur_format;
398 frame = stream->cur_frame;
399 probe = stream->ctrl;
400 probe.dwFrameInterval = uvc_try_frame_interval(frame, interval);
401 maxd = abs((s32)probe.dwFrameInterval - interval);
403 /* Try frames with matching size to find the best frame interval. */
404 for (i = 0; i < format->nframes && maxd != 0; i++) {
407 if (&format->frame[i] == stream->cur_frame)
410 if (format->frame[i].wWidth != stream->cur_frame->wWidth ||
411 format->frame[i].wHeight != stream->cur_frame->wHeight)
414 ival = uvc_try_frame_interval(&format->frame[i], interval);
415 d = abs((s32)ival - interval);
419 frame = &format->frame[i];
420 probe.bFrameIndex = frame->bFrameIndex;
421 probe.dwFrameInterval = ival;
425 /* Probe the device with the new settings. */
426 ret = uvc_probe_video(stream, &probe);
428 mutex_unlock(&stream->mutex);
432 stream->ctrl = probe;
433 stream->cur_frame = frame;
434 mutex_unlock(&stream->mutex);
436 /* Return the actual frame period. */
437 timeperframe.numerator = probe.dwFrameInterval;
438 timeperframe.denominator = 10000000;
439 uvc_simplify_fraction(&timeperframe.numerator,
440 &timeperframe.denominator, 8, 333);
442 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
443 parm->parm.capture.timeperframe = timeperframe;
445 parm->parm.output.timeperframe = timeperframe;
450 /* ------------------------------------------------------------------------
451 * Privilege management
455 * Privilege management is the multiple-open implementation basis. The current
456 * implementation is completely transparent for the end-user and doesn't
457 * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls.
458 * Those ioctls enable finer control on the device (by making possible for a
459 * user to request exclusive access to a device), but are not mature yet.
460 * Switching to the V4L2 priority mechanism might be considered in the future
461 * if this situation changes.
463 * Each open instance of a UVC device can either be in a privileged or
464 * unprivileged state. Only a single instance can be in a privileged state at
465 * a given time. Trying to perform an operation that requires privileges will
466 * automatically acquire the required privileges if possible, or return -EBUSY
467 * otherwise. Privileges are dismissed when closing the instance or when
468 * freeing the video buffers using VIDIOC_REQBUFS.
470 * Operations that require privileges are:
477 static int uvc_acquire_privileges(struct uvc_fh *handle)
479 /* Always succeed if the handle is already privileged. */
480 if (handle->state == UVC_HANDLE_ACTIVE)
483 /* Check if the device already has a privileged handle. */
484 if (atomic_inc_return(&handle->stream->active) != 1) {
485 atomic_dec(&handle->stream->active);
489 handle->state = UVC_HANDLE_ACTIVE;
493 static void uvc_dismiss_privileges(struct uvc_fh *handle)
495 if (handle->state == UVC_HANDLE_ACTIVE)
496 atomic_dec(&handle->stream->active);
498 handle->state = UVC_HANDLE_PASSIVE;
501 static int uvc_has_privileges(struct uvc_fh *handle)
503 return handle->state == UVC_HANDLE_ACTIVE;
506 /* ------------------------------------------------------------------------
507 * V4L2 file operations
510 static int uvc_v4l2_open(struct file *file)
512 struct uvc_streaming *stream;
513 struct uvc_fh *handle;
516 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
517 stream = video_drvdata(file);
519 ret = usb_autopm_get_interface(stream->dev->intf);
523 /* Create the device handle. */
524 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
525 if (handle == NULL) {
526 usb_autopm_put_interface(stream->dev->intf);
530 mutex_lock(&stream->dev->lock);
531 if (stream->dev->users == 0) {
532 ret = uvc_status_start(stream->dev, GFP_KERNEL);
534 mutex_unlock(&stream->dev->lock);
535 usb_autopm_put_interface(stream->dev->intf);
541 stream->dev->users++;
542 mutex_unlock(&stream->dev->lock);
544 v4l2_fh_init(&handle->vfh, &stream->vdev);
545 v4l2_fh_add(&handle->vfh);
546 handle->chain = stream->chain;
547 handle->stream = stream;
548 handle->state = UVC_HANDLE_PASSIVE;
549 file->private_data = handle;
554 static int uvc_v4l2_release(struct file *file)
556 struct uvc_fh *handle = file->private_data;
557 struct uvc_streaming *stream = handle->stream;
559 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");
561 /* Only free resources if this is a privileged handle. */
562 if (uvc_has_privileges(handle))
563 uvc_queue_release(&stream->queue);
565 /* Release the file handle. */
566 uvc_dismiss_privileges(handle);
567 v4l2_fh_del(&handle->vfh);
568 v4l2_fh_exit(&handle->vfh);
570 file->private_data = NULL;
572 mutex_lock(&stream->dev->lock);
573 if (--stream->dev->users == 0)
574 uvc_status_stop(stream->dev);
575 mutex_unlock(&stream->dev->lock);
577 usb_autopm_put_interface(stream->dev->intf);
581 static int uvc_ioctl_querycap(struct file *file, void *fh,
582 struct v4l2_capability *cap)
584 struct video_device *vdev = video_devdata(file);
585 struct uvc_fh *handle = file->private_data;
586 struct uvc_video_chain *chain = handle->chain;
587 struct uvc_streaming *stream = handle->stream;
589 strscpy(cap->driver, "uvcvideo", sizeof(cap->driver));
590 strscpy(cap->card, vdev->name, sizeof(cap->card));
591 usb_make_path(stream->dev->udev, cap->bus_info, sizeof(cap->bus_info));
592 cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
598 static int uvc_ioctl_enum_fmt(struct uvc_streaming *stream,
599 struct v4l2_fmtdesc *fmt)
601 struct uvc_format *format;
602 enum v4l2_buf_type type = fmt->type;
603 u32 index = fmt->index;
605 if (fmt->type != stream->type || fmt->index >= stream->nformats)
608 memset(fmt, 0, sizeof(*fmt));
612 format = &stream->format[fmt->index];
614 if (format->flags & UVC_FMT_FLAG_COMPRESSED)
615 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
616 strscpy(fmt->description, format->name, sizeof(fmt->description));
617 fmt->description[sizeof(fmt->description) - 1] = 0;
618 fmt->pixelformat = format->fcc;
622 static int uvc_ioctl_enum_fmt_vid_cap(struct file *file, void *fh,
623 struct v4l2_fmtdesc *fmt)
625 struct uvc_fh *handle = fh;
626 struct uvc_streaming *stream = handle->stream;
628 return uvc_ioctl_enum_fmt(stream, fmt);
631 static int uvc_ioctl_enum_fmt_vid_out(struct file *file, void *fh,
632 struct v4l2_fmtdesc *fmt)
634 struct uvc_fh *handle = fh;
635 struct uvc_streaming *stream = handle->stream;
637 return uvc_ioctl_enum_fmt(stream, fmt);
640 static int uvc_ioctl_g_fmt_vid_cap(struct file *file, void *fh,
641 struct v4l2_format *fmt)
643 struct uvc_fh *handle = fh;
644 struct uvc_streaming *stream = handle->stream;
646 return uvc_v4l2_get_format(stream, fmt);
649 static int uvc_ioctl_g_fmt_vid_out(struct file *file, void *fh,
650 struct v4l2_format *fmt)
652 struct uvc_fh *handle = fh;
653 struct uvc_streaming *stream = handle->stream;
655 return uvc_v4l2_get_format(stream, fmt);
658 static int uvc_ioctl_s_fmt_vid_cap(struct file *file, void *fh,
659 struct v4l2_format *fmt)
661 struct uvc_fh *handle = fh;
662 struct uvc_streaming *stream = handle->stream;
665 ret = uvc_acquire_privileges(handle);
669 return uvc_v4l2_set_format(stream, fmt);
672 static int uvc_ioctl_s_fmt_vid_out(struct file *file, void *fh,
673 struct v4l2_format *fmt)
675 struct uvc_fh *handle = fh;
676 struct uvc_streaming *stream = handle->stream;
679 ret = uvc_acquire_privileges(handle);
683 return uvc_v4l2_set_format(stream, fmt);
686 static int uvc_ioctl_try_fmt_vid_cap(struct file *file, void *fh,
687 struct v4l2_format *fmt)
689 struct uvc_fh *handle = fh;
690 struct uvc_streaming *stream = handle->stream;
691 struct uvc_streaming_control probe;
693 return uvc_v4l2_try_format(stream, fmt, &probe, NULL, NULL);
696 static int uvc_ioctl_try_fmt_vid_out(struct file *file, void *fh,
697 struct v4l2_format *fmt)
699 struct uvc_fh *handle = fh;
700 struct uvc_streaming *stream = handle->stream;
701 struct uvc_streaming_control probe;
703 return uvc_v4l2_try_format(stream, fmt, &probe, NULL, NULL);
706 static int uvc_ioctl_reqbufs(struct file *file, void *fh,
707 struct v4l2_requestbuffers *rb)
709 struct uvc_fh *handle = fh;
710 struct uvc_streaming *stream = handle->stream;
713 ret = uvc_acquire_privileges(handle);
717 mutex_lock(&stream->mutex);
718 ret = uvc_request_buffers(&stream->queue, rb);
719 mutex_unlock(&stream->mutex);
724 uvc_dismiss_privileges(handle);
729 static int uvc_ioctl_querybuf(struct file *file, void *fh,
730 struct v4l2_buffer *buf)
732 struct uvc_fh *handle = fh;
733 struct uvc_streaming *stream = handle->stream;
735 if (!uvc_has_privileges(handle))
738 return uvc_query_buffer(&stream->queue, buf);
741 static int uvc_ioctl_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
743 struct uvc_fh *handle = fh;
744 struct uvc_streaming *stream = handle->stream;
746 if (!uvc_has_privileges(handle))
749 return uvc_queue_buffer(&stream->queue,
750 stream->vdev.v4l2_dev->mdev, buf);
753 static int uvc_ioctl_expbuf(struct file *file, void *fh,
754 struct v4l2_exportbuffer *exp)
756 struct uvc_fh *handle = fh;
757 struct uvc_streaming *stream = handle->stream;
759 if (!uvc_has_privileges(handle))
762 return uvc_export_buffer(&stream->queue, exp);
765 static int uvc_ioctl_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
767 struct uvc_fh *handle = fh;
768 struct uvc_streaming *stream = handle->stream;
770 if (!uvc_has_privileges(handle))
773 return uvc_dequeue_buffer(&stream->queue, buf,
774 file->f_flags & O_NONBLOCK);
777 static int uvc_ioctl_create_bufs(struct file *file, void *fh,
778 struct v4l2_create_buffers *cb)
780 struct uvc_fh *handle = fh;
781 struct uvc_streaming *stream = handle->stream;
784 ret = uvc_acquire_privileges(handle);
788 return uvc_create_buffers(&stream->queue, cb);
791 static int uvc_ioctl_streamon(struct file *file, void *fh,
792 enum v4l2_buf_type type)
794 struct uvc_fh *handle = fh;
795 struct uvc_streaming *stream = handle->stream;
798 if (!uvc_has_privileges(handle))
801 mutex_lock(&stream->mutex);
802 ret = uvc_queue_streamon(&stream->queue, type);
803 mutex_unlock(&stream->mutex);
808 static int uvc_ioctl_streamoff(struct file *file, void *fh,
809 enum v4l2_buf_type type)
811 struct uvc_fh *handle = fh;
812 struct uvc_streaming *stream = handle->stream;
814 if (!uvc_has_privileges(handle))
817 mutex_lock(&stream->mutex);
818 uvc_queue_streamoff(&stream->queue, type);
819 mutex_unlock(&stream->mutex);
824 static int uvc_ioctl_enum_input(struct file *file, void *fh,
825 struct v4l2_input *input)
827 struct uvc_fh *handle = fh;
828 struct uvc_video_chain *chain = handle->chain;
829 const struct uvc_entity *selector = chain->selector;
830 struct uvc_entity *iterm = NULL;
831 u32 index = input->index;
834 if (selector == NULL ||
835 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
838 list_for_each_entry(iterm, &chain->entities, chain) {
839 if (UVC_ENTITY_IS_ITERM(iterm))
843 } else if (index < selector->bNrInPins) {
844 pin = selector->baSourceID[index];
845 list_for_each_entry(iterm, &chain->entities, chain) {
846 if (!UVC_ENTITY_IS_ITERM(iterm))
848 if (iterm->id == pin)
853 if (iterm == NULL || iterm->id != pin)
856 memset(input, 0, sizeof(*input));
857 input->index = index;
858 strscpy(input->name, iterm->name, sizeof(input->name));
859 if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
860 input->type = V4L2_INPUT_TYPE_CAMERA;
865 static int uvc_ioctl_g_input(struct file *file, void *fh, unsigned int *input)
867 struct uvc_fh *handle = fh;
868 struct uvc_video_chain *chain = handle->chain;
872 if (chain->selector == NULL ||
873 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
878 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, chain->selector->id,
879 chain->dev->intfnum, UVC_SU_INPUT_SELECT_CONTROL,
888 static int uvc_ioctl_s_input(struct file *file, void *fh, unsigned int input)
890 struct uvc_fh *handle = fh;
891 struct uvc_video_chain *chain = handle->chain;
895 ret = uvc_acquire_privileges(handle);
899 if (chain->selector == NULL ||
900 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
906 if (input >= chain->selector->bNrInPins)
910 return uvc_query_ctrl(chain->dev, UVC_SET_CUR, chain->selector->id,
911 chain->dev->intfnum, UVC_SU_INPUT_SELECT_CONTROL,
915 static int uvc_ioctl_queryctrl(struct file *file, void *fh,
916 struct v4l2_queryctrl *qc)
918 struct uvc_fh *handle = fh;
919 struct uvc_video_chain *chain = handle->chain;
921 return uvc_query_v4l2_ctrl(chain, qc);
924 static int uvc_ioctl_query_ext_ctrl(struct file *file, void *fh,
925 struct v4l2_query_ext_ctrl *qec)
927 struct uvc_fh *handle = fh;
928 struct uvc_video_chain *chain = handle->chain;
929 struct v4l2_queryctrl qc = { qec->id };
932 ret = uvc_query_v4l2_ctrl(chain, &qc);
938 strscpy(qec->name, qc.name, sizeof(qec->name));
939 qec->minimum = qc.minimum;
940 qec->maximum = qc.maximum;
942 qec->default_value = qc.default_value;
943 qec->flags = qc.flags;
947 memset(qec->dims, 0, sizeof(qec->dims));
948 memset(qec->reserved, 0, sizeof(qec->reserved));
953 static int uvc_ioctl_g_ctrl(struct file *file, void *fh,
954 struct v4l2_control *ctrl)
956 struct uvc_fh *handle = fh;
957 struct uvc_video_chain *chain = handle->chain;
958 struct v4l2_ext_control xctrl;
961 memset(&xctrl, 0, sizeof(xctrl));
964 ret = uvc_ctrl_begin(chain);
968 ret = uvc_ctrl_get(chain, &xctrl);
969 uvc_ctrl_rollback(handle);
973 ctrl->value = xctrl.value;
977 static int uvc_ioctl_s_ctrl(struct file *file, void *fh,
978 struct v4l2_control *ctrl)
980 struct uvc_fh *handle = fh;
981 struct uvc_video_chain *chain = handle->chain;
982 struct v4l2_ext_control xctrl;
985 memset(&xctrl, 0, sizeof(xctrl));
987 xctrl.value = ctrl->value;
989 ret = uvc_ctrl_begin(chain);
993 ret = uvc_ctrl_set(handle, &xctrl);
995 uvc_ctrl_rollback(handle);
999 ret = uvc_ctrl_commit(handle, &xctrl, 1);
1003 ctrl->value = xctrl.value;
1007 static int uvc_ioctl_g_ext_ctrls(struct file *file, void *fh,
1008 struct v4l2_ext_controls *ctrls)
1010 struct uvc_fh *handle = fh;
1011 struct uvc_video_chain *chain = handle->chain;
1012 struct v4l2_ext_control *ctrl = ctrls->controls;
1016 if (ctrls->which == V4L2_CTRL_WHICH_DEF_VAL) {
1017 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
1018 struct v4l2_queryctrl qc = { .id = ctrl->id };
1020 ret = uvc_query_v4l2_ctrl(chain, &qc);
1022 ctrls->error_idx = i;
1026 ctrl->value = qc.default_value;
1032 ret = uvc_ctrl_begin(chain);
1036 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
1037 ret = uvc_ctrl_get(chain, ctrl);
1039 uvc_ctrl_rollback(handle);
1040 ctrls->error_idx = i;
1045 ctrls->error_idx = 0;
1047 return uvc_ctrl_rollback(handle);
1050 static int uvc_ioctl_s_try_ext_ctrls(struct uvc_fh *handle,
1051 struct v4l2_ext_controls *ctrls,
1054 struct v4l2_ext_control *ctrl = ctrls->controls;
1055 struct uvc_video_chain *chain = handle->chain;
1059 /* Default value cannot be changed */
1060 if (ctrls->which == V4L2_CTRL_WHICH_DEF_VAL)
1063 ret = uvc_ctrl_begin(chain);
1067 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
1068 ret = uvc_ctrl_set(handle, ctrl);
1070 uvc_ctrl_rollback(handle);
1071 ctrls->error_idx = commit ? ctrls->count : i;
1076 ctrls->error_idx = 0;
1079 return uvc_ctrl_commit(handle, ctrls->controls, ctrls->count);
1081 return uvc_ctrl_rollback(handle);
1084 static int uvc_ioctl_s_ext_ctrls(struct file *file, void *fh,
1085 struct v4l2_ext_controls *ctrls)
1087 struct uvc_fh *handle = fh;
1089 return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, true);
1092 static int uvc_ioctl_try_ext_ctrls(struct file *file, void *fh,
1093 struct v4l2_ext_controls *ctrls)
1095 struct uvc_fh *handle = fh;
1097 return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, false);
1100 static int uvc_ioctl_querymenu(struct file *file, void *fh,
1101 struct v4l2_querymenu *qm)
1103 struct uvc_fh *handle = fh;
1104 struct uvc_video_chain *chain = handle->chain;
1106 return uvc_query_v4l2_menu(chain, qm);
1109 static int uvc_ioctl_g_selection(struct file *file, void *fh,
1110 struct v4l2_selection *sel)
1112 struct uvc_fh *handle = fh;
1113 struct uvc_streaming *stream = handle->stream;
1115 if (sel->type != stream->type)
1118 switch (sel->target) {
1119 case V4L2_SEL_TGT_CROP_DEFAULT:
1120 case V4L2_SEL_TGT_CROP_BOUNDS:
1121 if (stream->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1124 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1125 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1126 if (stream->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1135 mutex_lock(&stream->mutex);
1136 sel->r.width = stream->cur_frame->wWidth;
1137 sel->r.height = stream->cur_frame->wHeight;
1138 mutex_unlock(&stream->mutex);
1143 static int uvc_ioctl_g_parm(struct file *file, void *fh,
1144 struct v4l2_streamparm *parm)
1146 struct uvc_fh *handle = fh;
1147 struct uvc_streaming *stream = handle->stream;
1149 return uvc_v4l2_get_streamparm(stream, parm);
1152 static int uvc_ioctl_s_parm(struct file *file, void *fh,
1153 struct v4l2_streamparm *parm)
1155 struct uvc_fh *handle = fh;
1156 struct uvc_streaming *stream = handle->stream;
1159 ret = uvc_acquire_privileges(handle);
1163 return uvc_v4l2_set_streamparm(stream, parm);
1166 static int uvc_ioctl_enum_framesizes(struct file *file, void *fh,
1167 struct v4l2_frmsizeenum *fsize)
1169 struct uvc_fh *handle = fh;
1170 struct uvc_streaming *stream = handle->stream;
1171 struct uvc_format *format = NULL;
1172 struct uvc_frame *frame = NULL;
1176 /* Look for the given pixel format */
1177 for (i = 0; i < stream->nformats; i++) {
1178 if (stream->format[i].fcc == fsize->pixel_format) {
1179 format = &stream->format[i];
1186 /* Skip duplicate frame sizes */
1187 for (i = 0, index = 0; i < format->nframes; i++) {
1188 if (frame && frame->wWidth == format->frame[i].wWidth &&
1189 frame->wHeight == format->frame[i].wHeight)
1191 frame = &format->frame[i];
1192 if (index == fsize->index)
1197 if (i == format->nframes)
1200 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1201 fsize->discrete.width = frame->wWidth;
1202 fsize->discrete.height = frame->wHeight;
1206 static int uvc_ioctl_enum_frameintervals(struct file *file, void *fh,
1207 struct v4l2_frmivalenum *fival)
1209 struct uvc_fh *handle = fh;
1210 struct uvc_streaming *stream = handle->stream;
1211 struct uvc_format *format = NULL;
1212 struct uvc_frame *frame = NULL;
1213 unsigned int nintervals;
1217 /* Look for the given pixel format and frame size */
1218 for (i = 0; i < stream->nformats; i++) {
1219 if (stream->format[i].fcc == fival->pixel_format) {
1220 format = &stream->format[i];
1227 index = fival->index;
1228 for (i = 0; i < format->nframes; i++) {
1229 if (format->frame[i].wWidth == fival->width &&
1230 format->frame[i].wHeight == fival->height) {
1231 frame = &format->frame[i];
1232 nintervals = frame->bFrameIntervalType ?: 1;
1233 if (index < nintervals)
1235 index -= nintervals;
1238 if (i == format->nframes)
1241 if (frame->bFrameIntervalType) {
1242 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1243 fival->discrete.numerator =
1244 frame->dwFrameInterval[index];
1245 fival->discrete.denominator = 10000000;
1246 uvc_simplify_fraction(&fival->discrete.numerator,
1247 &fival->discrete.denominator, 8, 333);
1249 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
1250 fival->stepwise.min.numerator = frame->dwFrameInterval[0];
1251 fival->stepwise.min.denominator = 10000000;
1252 fival->stepwise.max.numerator = frame->dwFrameInterval[1];
1253 fival->stepwise.max.denominator = 10000000;
1254 fival->stepwise.step.numerator = frame->dwFrameInterval[2];
1255 fival->stepwise.step.denominator = 10000000;
1256 uvc_simplify_fraction(&fival->stepwise.min.numerator,
1257 &fival->stepwise.min.denominator, 8, 333);
1258 uvc_simplify_fraction(&fival->stepwise.max.numerator,
1259 &fival->stepwise.max.denominator, 8, 333);
1260 uvc_simplify_fraction(&fival->stepwise.step.numerator,
1261 &fival->stepwise.step.denominator, 8, 333);
1267 static int uvc_ioctl_subscribe_event(struct v4l2_fh *fh,
1268 const struct v4l2_event_subscription *sub)
1270 switch (sub->type) {
1271 case V4L2_EVENT_CTRL:
1272 return v4l2_event_subscribe(fh, sub, 0, &uvc_ctrl_sub_ev_ops);
1278 static long uvc_ioctl_default(struct file *file, void *fh, bool valid_prio,
1279 unsigned int cmd, void *arg)
1281 struct uvc_fh *handle = fh;
1282 struct uvc_video_chain *chain = handle->chain;
1285 /* Dynamic controls. */
1286 case UVCIOC_CTRL_MAP:
1287 return uvc_ioctl_ctrl_map(chain, arg);
1289 case UVCIOC_CTRL_QUERY:
1290 return uvc_xu_ctrl_query(chain, arg);
1297 #ifdef CONFIG_COMPAT
1298 struct uvc_xu_control_mapping32 {
1309 compat_caddr_t menu_info;
1315 static int uvc_v4l2_get_xu_mapping(struct uvc_xu_control_mapping *kp,
1316 const struct uvc_xu_control_mapping32 __user *up)
1318 struct uvc_xu_control_mapping32 *p = (void *)kp;
1319 compat_caddr_t info;
1322 if (copy_from_user(p, up, sizeof(*p)))
1325 count = p->menu_count;
1326 info = p->menu_info;
1328 memset(kp->reserved, 0, sizeof(kp->reserved));
1329 kp->menu_info = count ? compat_ptr(info) : NULL;
1330 kp->menu_count = count;
1334 static int uvc_v4l2_put_xu_mapping(const struct uvc_xu_control_mapping *kp,
1335 struct uvc_xu_control_mapping32 __user *up)
1337 if (copy_to_user(up, kp, offsetof(typeof(*up), menu_info)) ||
1338 put_user(kp->menu_count, &up->menu_count))
1341 if (clear_user(up->reserved, sizeof(up->reserved)))
1347 struct uvc_xu_control_query32 {
1352 compat_caddr_t data;
1355 static int uvc_v4l2_get_xu_query(struct uvc_xu_control_query *kp,
1356 const struct uvc_xu_control_query32 __user *up)
1358 struct uvc_xu_control_query32 v;
1360 if (copy_from_user(&v, up, sizeof(v)))
1363 *kp = (struct uvc_xu_control_query){
1365 .selector = v.selector,
1368 .data = v.size ? compat_ptr(v.data) : NULL
1373 static int uvc_v4l2_put_xu_query(const struct uvc_xu_control_query *kp,
1374 struct uvc_xu_control_query32 __user *up)
1376 if (copy_to_user(up, kp, offsetof(typeof(*up), data)))
1381 #define UVCIOC_CTRL_MAP32 _IOWR('u', 0x20, struct uvc_xu_control_mapping32)
1382 #define UVCIOC_CTRL_QUERY32 _IOWR('u', 0x21, struct uvc_xu_control_query32)
1384 static long uvc_v4l2_compat_ioctl32(struct file *file,
1385 unsigned int cmd, unsigned long arg)
1387 struct uvc_fh *handle = file->private_data;
1389 struct uvc_xu_control_mapping xmap;
1390 struct uvc_xu_control_query xqry;
1392 void __user *up = compat_ptr(arg);
1396 case UVCIOC_CTRL_MAP32:
1397 ret = uvc_v4l2_get_xu_mapping(&karg.xmap, up);
1400 ret = uvc_ioctl_ctrl_map(handle->chain, &karg.xmap);
1403 ret = uvc_v4l2_put_xu_mapping(&karg.xmap, up);
1409 case UVCIOC_CTRL_QUERY32:
1410 ret = uvc_v4l2_get_xu_query(&karg.xqry, up);
1413 ret = uvc_xu_ctrl_query(handle->chain, &karg.xqry);
1416 ret = uvc_v4l2_put_xu_query(&karg.xqry, up);
1422 return -ENOIOCTLCMD;
1429 static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1430 size_t count, loff_t *ppos)
1432 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
1436 static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1438 struct uvc_fh *handle = file->private_data;
1439 struct uvc_streaming *stream = handle->stream;
1441 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
1443 return uvc_queue_mmap(&stream->queue, vma);
1446 static __poll_t uvc_v4l2_poll(struct file *file, poll_table *wait)
1448 struct uvc_fh *handle = file->private_data;
1449 struct uvc_streaming *stream = handle->stream;
1451 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
1453 return uvc_queue_poll(&stream->queue, file, wait);
1457 static unsigned long uvc_v4l2_get_unmapped_area(struct file *file,
1458 unsigned long addr, unsigned long len, unsigned long pgoff,
1459 unsigned long flags)
1461 struct uvc_fh *handle = file->private_data;
1462 struct uvc_streaming *stream = handle->stream;
1464 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_get_unmapped_area\n");
1466 return uvc_queue_get_unmapped_area(&stream->queue, pgoff);
1470 const struct v4l2_ioctl_ops uvc_ioctl_ops = {
1471 .vidioc_querycap = uvc_ioctl_querycap,
1472 .vidioc_enum_fmt_vid_cap = uvc_ioctl_enum_fmt_vid_cap,
1473 .vidioc_enum_fmt_vid_out = uvc_ioctl_enum_fmt_vid_out,
1474 .vidioc_g_fmt_vid_cap = uvc_ioctl_g_fmt_vid_cap,
1475 .vidioc_g_fmt_vid_out = uvc_ioctl_g_fmt_vid_out,
1476 .vidioc_s_fmt_vid_cap = uvc_ioctl_s_fmt_vid_cap,
1477 .vidioc_s_fmt_vid_out = uvc_ioctl_s_fmt_vid_out,
1478 .vidioc_try_fmt_vid_cap = uvc_ioctl_try_fmt_vid_cap,
1479 .vidioc_try_fmt_vid_out = uvc_ioctl_try_fmt_vid_out,
1480 .vidioc_reqbufs = uvc_ioctl_reqbufs,
1481 .vidioc_querybuf = uvc_ioctl_querybuf,
1482 .vidioc_qbuf = uvc_ioctl_qbuf,
1483 .vidioc_expbuf = uvc_ioctl_expbuf,
1484 .vidioc_dqbuf = uvc_ioctl_dqbuf,
1485 .vidioc_create_bufs = uvc_ioctl_create_bufs,
1486 .vidioc_streamon = uvc_ioctl_streamon,
1487 .vidioc_streamoff = uvc_ioctl_streamoff,
1488 .vidioc_enum_input = uvc_ioctl_enum_input,
1489 .vidioc_g_input = uvc_ioctl_g_input,
1490 .vidioc_s_input = uvc_ioctl_s_input,
1491 .vidioc_queryctrl = uvc_ioctl_queryctrl,
1492 .vidioc_query_ext_ctrl = uvc_ioctl_query_ext_ctrl,
1493 .vidioc_g_ctrl = uvc_ioctl_g_ctrl,
1494 .vidioc_s_ctrl = uvc_ioctl_s_ctrl,
1495 .vidioc_g_ext_ctrls = uvc_ioctl_g_ext_ctrls,
1496 .vidioc_s_ext_ctrls = uvc_ioctl_s_ext_ctrls,
1497 .vidioc_try_ext_ctrls = uvc_ioctl_try_ext_ctrls,
1498 .vidioc_querymenu = uvc_ioctl_querymenu,
1499 .vidioc_g_selection = uvc_ioctl_g_selection,
1500 .vidioc_g_parm = uvc_ioctl_g_parm,
1501 .vidioc_s_parm = uvc_ioctl_s_parm,
1502 .vidioc_enum_framesizes = uvc_ioctl_enum_framesizes,
1503 .vidioc_enum_frameintervals = uvc_ioctl_enum_frameintervals,
1504 .vidioc_subscribe_event = uvc_ioctl_subscribe_event,
1505 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1506 .vidioc_default = uvc_ioctl_default,
1509 const struct v4l2_file_operations uvc_fops = {
1510 .owner = THIS_MODULE,
1511 .open = uvc_v4l2_open,
1512 .release = uvc_v4l2_release,
1513 .unlocked_ioctl = video_ioctl2,
1514 #ifdef CONFIG_COMPAT
1515 .compat_ioctl32 = uvc_v4l2_compat_ioctl32,
1517 .read = uvc_v4l2_read,
1518 .mmap = uvc_v4l2_mmap,
1519 .poll = uvc_v4l2_poll,
1521 .get_unmapped_area = uvc_v4l2_get_unmapped_area,