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 /* After the probe, update fmt with the values returned from
251 * negotiation with the device. Some devices return invalid bFormatIndex
252 * and bFrameIndex values, in which case we can only assume they have
253 * accepted the requested format as-is.
255 for (i = 0; i < stream->nformats; ++i) {
256 if (probe->bFormatIndex == stream->format[i].index) {
257 format = &stream->format[i];
262 if (i == stream->nformats)
263 uvc_trace(UVC_TRACE_FORMAT,
264 "Unknown bFormatIndex %u, using default\n",
265 probe->bFormatIndex);
267 for (i = 0; i < format->nframes; ++i) {
268 if (probe->bFrameIndex == format->frame[i].bFrameIndex) {
269 frame = &format->frame[i];
274 if (i == format->nframes)
275 uvc_trace(UVC_TRACE_FORMAT,
276 "Unknown bFrameIndex %u, using default\n",
279 fmt->fmt.pix.width = frame->wWidth;
280 fmt->fmt.pix.height = frame->wHeight;
281 fmt->fmt.pix.field = V4L2_FIELD_NONE;
282 fmt->fmt.pix.bytesperline = uvc_v4l2_get_bytesperline(format, frame);
283 fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize;
284 fmt->fmt.pix.pixelformat = format->fcc;
285 fmt->fmt.pix.colorspace = format->colorspace;
286 fmt->fmt.pix.xfer_func = format->xfer_func;
287 fmt->fmt.pix.ycbcr_enc = format->ycbcr_enc;
289 if (uvc_format != NULL)
290 *uvc_format = format;
291 if (uvc_frame != NULL)
298 static int uvc_v4l2_get_format(struct uvc_streaming *stream,
299 struct v4l2_format *fmt)
301 struct uvc_format *format;
302 struct uvc_frame *frame;
305 if (fmt->type != stream->type)
308 mutex_lock(&stream->mutex);
309 format = stream->cur_format;
310 frame = stream->cur_frame;
312 if (format == NULL || frame == NULL) {
317 fmt->fmt.pix.pixelformat = format->fcc;
318 fmt->fmt.pix.width = frame->wWidth;
319 fmt->fmt.pix.height = frame->wHeight;
320 fmt->fmt.pix.field = V4L2_FIELD_NONE;
321 fmt->fmt.pix.bytesperline = uvc_v4l2_get_bytesperline(format, frame);
322 fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize;
323 fmt->fmt.pix.colorspace = format->colorspace;
324 fmt->fmt.pix.xfer_func = format->xfer_func;
325 fmt->fmt.pix.ycbcr_enc = format->ycbcr_enc;
328 mutex_unlock(&stream->mutex);
332 static int uvc_v4l2_set_format(struct uvc_streaming *stream,
333 struct v4l2_format *fmt)
335 struct uvc_streaming_control probe;
336 struct uvc_format *format;
337 struct uvc_frame *frame;
340 if (fmt->type != stream->type)
343 ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame);
347 mutex_lock(&stream->mutex);
349 if (uvc_queue_allocated(&stream->queue)) {
354 stream->ctrl = probe;
355 stream->cur_format = format;
356 stream->cur_frame = frame;
359 mutex_unlock(&stream->mutex);
363 static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream,
364 struct v4l2_streamparm *parm)
366 u32 numerator, denominator;
368 if (parm->type != stream->type)
371 mutex_lock(&stream->mutex);
372 numerator = stream->ctrl.dwFrameInterval;
373 mutex_unlock(&stream->mutex);
375 denominator = 10000000;
376 uvc_simplify_fraction(&numerator, &denominator, 8, 333);
378 memset(parm, 0, sizeof(*parm));
379 parm->type = stream->type;
381 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
382 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
383 parm->parm.capture.capturemode = 0;
384 parm->parm.capture.timeperframe.numerator = numerator;
385 parm->parm.capture.timeperframe.denominator = denominator;
386 parm->parm.capture.extendedmode = 0;
387 parm->parm.capture.readbuffers = 0;
389 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
390 parm->parm.output.outputmode = 0;
391 parm->parm.output.timeperframe.numerator = numerator;
392 parm->parm.output.timeperframe.denominator = denominator;
398 static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
399 struct v4l2_streamparm *parm)
401 struct uvc_streaming_control probe;
402 struct v4l2_fract timeperframe;
403 struct uvc_format *format;
404 struct uvc_frame *frame;
409 if (parm->type != stream->type)
412 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
413 timeperframe = parm->parm.capture.timeperframe;
415 timeperframe = parm->parm.output.timeperframe;
417 interval = uvc_fraction_to_interval(timeperframe.numerator,
418 timeperframe.denominator);
419 uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
420 timeperframe.numerator, timeperframe.denominator, interval);
422 mutex_lock(&stream->mutex);
424 if (uvc_queue_streaming(&stream->queue)) {
425 mutex_unlock(&stream->mutex);
429 format = stream->cur_format;
430 frame = stream->cur_frame;
431 probe = stream->ctrl;
432 probe.dwFrameInterval = uvc_try_frame_interval(frame, interval);
433 maxd = abs((s32)probe.dwFrameInterval - interval);
435 /* Try frames with matching size to find the best frame interval. */
436 for (i = 0; i < format->nframes && maxd != 0; i++) {
439 if (&format->frame[i] == stream->cur_frame)
442 if (format->frame[i].wWidth != stream->cur_frame->wWidth ||
443 format->frame[i].wHeight != stream->cur_frame->wHeight)
446 ival = uvc_try_frame_interval(&format->frame[i], interval);
447 d = abs((s32)ival - interval);
451 frame = &format->frame[i];
452 probe.bFrameIndex = frame->bFrameIndex;
453 probe.dwFrameInterval = ival;
457 /* Probe the device with the new settings. */
458 ret = uvc_probe_video(stream, &probe);
460 mutex_unlock(&stream->mutex);
464 stream->ctrl = probe;
465 stream->cur_frame = frame;
466 mutex_unlock(&stream->mutex);
468 /* Return the actual frame period. */
469 timeperframe.numerator = probe.dwFrameInterval;
470 timeperframe.denominator = 10000000;
471 uvc_simplify_fraction(&timeperframe.numerator,
472 &timeperframe.denominator, 8, 333);
474 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
475 parm->parm.capture.timeperframe = timeperframe;
476 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
478 parm->parm.output.timeperframe = timeperframe;
479 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
485 /* ------------------------------------------------------------------------
486 * Privilege management
490 * Privilege management is the multiple-open implementation basis. The current
491 * implementation is completely transparent for the end-user and doesn't
492 * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls.
493 * Those ioctls enable finer control on the device (by making possible for a
494 * user to request exclusive access to a device), but are not mature yet.
495 * Switching to the V4L2 priority mechanism might be considered in the future
496 * if this situation changes.
498 * Each open instance of a UVC device can either be in a privileged or
499 * unprivileged state. Only a single instance can be in a privileged state at
500 * a given time. Trying to perform an operation that requires privileges will
501 * automatically acquire the required privileges if possible, or return -EBUSY
502 * otherwise. Privileges are dismissed when closing the instance or when
503 * freeing the video buffers using VIDIOC_REQBUFS.
505 * Operations that require privileges are:
512 static int uvc_acquire_privileges(struct uvc_fh *handle)
514 /* Always succeed if the handle is already privileged. */
515 if (handle->state == UVC_HANDLE_ACTIVE)
518 /* Check if the device already has a privileged handle. */
519 if (atomic_inc_return(&handle->stream->active) != 1) {
520 atomic_dec(&handle->stream->active);
524 handle->state = UVC_HANDLE_ACTIVE;
528 static void uvc_dismiss_privileges(struct uvc_fh *handle)
530 if (handle->state == UVC_HANDLE_ACTIVE)
531 atomic_dec(&handle->stream->active);
533 handle->state = UVC_HANDLE_PASSIVE;
536 static int uvc_has_privileges(struct uvc_fh *handle)
538 return handle->state == UVC_HANDLE_ACTIVE;
541 /* ------------------------------------------------------------------------
542 * V4L2 file operations
545 static int uvc_v4l2_open(struct file *file)
547 struct uvc_streaming *stream;
548 struct uvc_fh *handle;
551 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
552 stream = video_drvdata(file);
554 ret = usb_autopm_get_interface(stream->dev->intf);
558 /* Create the device handle. */
559 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
560 if (handle == NULL) {
561 usb_autopm_put_interface(stream->dev->intf);
565 mutex_lock(&stream->dev->lock);
566 if (stream->dev->users == 0) {
567 ret = uvc_status_start(stream->dev, GFP_KERNEL);
569 mutex_unlock(&stream->dev->lock);
570 usb_autopm_put_interface(stream->dev->intf);
576 stream->dev->users++;
577 mutex_unlock(&stream->dev->lock);
579 v4l2_fh_init(&handle->vfh, &stream->vdev);
580 v4l2_fh_add(&handle->vfh);
581 handle->chain = stream->chain;
582 handle->stream = stream;
583 handle->state = UVC_HANDLE_PASSIVE;
584 file->private_data = handle;
589 static int uvc_v4l2_release(struct file *file)
591 struct uvc_fh *handle = file->private_data;
592 struct uvc_streaming *stream = handle->stream;
594 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");
596 /* Only free resources if this is a privileged handle. */
597 if (uvc_has_privileges(handle))
598 uvc_queue_release(&stream->queue);
600 /* Release the file handle. */
601 uvc_dismiss_privileges(handle);
602 v4l2_fh_del(&handle->vfh);
603 v4l2_fh_exit(&handle->vfh);
605 file->private_data = NULL;
607 mutex_lock(&stream->dev->lock);
608 if (--stream->dev->users == 0)
609 uvc_status_stop(stream->dev);
610 mutex_unlock(&stream->dev->lock);
612 usb_autopm_put_interface(stream->dev->intf);
616 static int uvc_ioctl_querycap(struct file *file, void *fh,
617 struct v4l2_capability *cap)
619 struct video_device *vdev = video_devdata(file);
620 struct uvc_fh *handle = file->private_data;
621 struct uvc_video_chain *chain = handle->chain;
622 struct uvc_streaming *stream = handle->stream;
624 strscpy(cap->driver, "uvcvideo", sizeof(cap->driver));
625 strscpy(cap->card, vdev->name, sizeof(cap->card));
626 usb_make_path(stream->dev->udev, cap->bus_info, sizeof(cap->bus_info));
627 cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
633 static int uvc_ioctl_enum_fmt(struct uvc_streaming *stream,
634 struct v4l2_fmtdesc *fmt)
636 struct uvc_format *format;
637 enum v4l2_buf_type type = fmt->type;
638 u32 index = fmt->index;
640 if (fmt->type != stream->type || fmt->index >= stream->nformats)
643 memset(fmt, 0, sizeof(*fmt));
647 format = &stream->format[fmt->index];
649 if (format->flags & UVC_FMT_FLAG_COMPRESSED)
650 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
651 strscpy(fmt->description, format->name, sizeof(fmt->description));
652 fmt->description[sizeof(fmt->description) - 1] = 0;
653 fmt->pixelformat = format->fcc;
657 static int uvc_ioctl_enum_fmt_vid_cap(struct file *file, void *fh,
658 struct v4l2_fmtdesc *fmt)
660 struct uvc_fh *handle = fh;
661 struct uvc_streaming *stream = handle->stream;
663 return uvc_ioctl_enum_fmt(stream, fmt);
666 static int uvc_ioctl_enum_fmt_vid_out(struct file *file, void *fh,
667 struct v4l2_fmtdesc *fmt)
669 struct uvc_fh *handle = fh;
670 struct uvc_streaming *stream = handle->stream;
672 return uvc_ioctl_enum_fmt(stream, fmt);
675 static int uvc_ioctl_g_fmt_vid_cap(struct file *file, void *fh,
676 struct v4l2_format *fmt)
678 struct uvc_fh *handle = fh;
679 struct uvc_streaming *stream = handle->stream;
681 return uvc_v4l2_get_format(stream, fmt);
684 static int uvc_ioctl_g_fmt_vid_out(struct file *file, void *fh,
685 struct v4l2_format *fmt)
687 struct uvc_fh *handle = fh;
688 struct uvc_streaming *stream = handle->stream;
690 return uvc_v4l2_get_format(stream, fmt);
693 static int uvc_ioctl_s_fmt_vid_cap(struct file *file, void *fh,
694 struct v4l2_format *fmt)
696 struct uvc_fh *handle = fh;
697 struct uvc_streaming *stream = handle->stream;
700 ret = uvc_acquire_privileges(handle);
704 return uvc_v4l2_set_format(stream, fmt);
707 static int uvc_ioctl_s_fmt_vid_out(struct file *file, void *fh,
708 struct v4l2_format *fmt)
710 struct uvc_fh *handle = fh;
711 struct uvc_streaming *stream = handle->stream;
714 ret = uvc_acquire_privileges(handle);
718 return uvc_v4l2_set_format(stream, fmt);
721 static int uvc_ioctl_try_fmt_vid_cap(struct file *file, void *fh,
722 struct v4l2_format *fmt)
724 struct uvc_fh *handle = fh;
725 struct uvc_streaming *stream = handle->stream;
726 struct uvc_streaming_control probe;
728 return uvc_v4l2_try_format(stream, fmt, &probe, NULL, NULL);
731 static int uvc_ioctl_try_fmt_vid_out(struct file *file, void *fh,
732 struct v4l2_format *fmt)
734 struct uvc_fh *handle = fh;
735 struct uvc_streaming *stream = handle->stream;
736 struct uvc_streaming_control probe;
738 return uvc_v4l2_try_format(stream, fmt, &probe, NULL, NULL);
741 static int uvc_ioctl_reqbufs(struct file *file, void *fh,
742 struct v4l2_requestbuffers *rb)
744 struct uvc_fh *handle = fh;
745 struct uvc_streaming *stream = handle->stream;
748 ret = uvc_acquire_privileges(handle);
752 mutex_lock(&stream->mutex);
753 ret = uvc_request_buffers(&stream->queue, rb);
754 mutex_unlock(&stream->mutex);
759 uvc_dismiss_privileges(handle);
764 static int uvc_ioctl_querybuf(struct file *file, void *fh,
765 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_query_buffer(&stream->queue, buf);
776 static int uvc_ioctl_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
778 struct uvc_fh *handle = fh;
779 struct uvc_streaming *stream = handle->stream;
781 if (!uvc_has_privileges(handle))
784 return uvc_queue_buffer(&stream->queue,
785 stream->vdev.v4l2_dev->mdev, buf);
788 static int uvc_ioctl_expbuf(struct file *file, void *fh,
789 struct v4l2_exportbuffer *exp)
791 struct uvc_fh *handle = fh;
792 struct uvc_streaming *stream = handle->stream;
794 if (!uvc_has_privileges(handle))
797 return uvc_export_buffer(&stream->queue, exp);
800 static int uvc_ioctl_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
802 struct uvc_fh *handle = fh;
803 struct uvc_streaming *stream = handle->stream;
805 if (!uvc_has_privileges(handle))
808 return uvc_dequeue_buffer(&stream->queue, buf,
809 file->f_flags & O_NONBLOCK);
812 static int uvc_ioctl_create_bufs(struct file *file, void *fh,
813 struct v4l2_create_buffers *cb)
815 struct uvc_fh *handle = fh;
816 struct uvc_streaming *stream = handle->stream;
819 ret = uvc_acquire_privileges(handle);
823 return uvc_create_buffers(&stream->queue, cb);
826 static int uvc_ioctl_streamon(struct file *file, void *fh,
827 enum v4l2_buf_type type)
829 struct uvc_fh *handle = fh;
830 struct uvc_streaming *stream = handle->stream;
833 if (!uvc_has_privileges(handle))
836 mutex_lock(&stream->mutex);
837 ret = uvc_queue_streamon(&stream->queue, type);
838 mutex_unlock(&stream->mutex);
843 static int uvc_ioctl_streamoff(struct file *file, void *fh,
844 enum v4l2_buf_type type)
846 struct uvc_fh *handle = fh;
847 struct uvc_streaming *stream = handle->stream;
849 if (!uvc_has_privileges(handle))
852 mutex_lock(&stream->mutex);
853 uvc_queue_streamoff(&stream->queue, type);
854 mutex_unlock(&stream->mutex);
859 static int uvc_ioctl_enum_input(struct file *file, void *fh,
860 struct v4l2_input *input)
862 struct uvc_fh *handle = fh;
863 struct uvc_video_chain *chain = handle->chain;
864 const struct uvc_entity *selector = chain->selector;
865 struct uvc_entity *iterm = NULL;
866 struct uvc_entity *it;
867 u32 index = input->index;
869 if (selector == NULL ||
870 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
873 list_for_each_entry(it, &chain->entities, chain) {
874 if (UVC_ENTITY_IS_ITERM(it)) {
879 } else if (index < selector->bNrInPins) {
880 list_for_each_entry(it, &chain->entities, chain) {
881 if (!UVC_ENTITY_IS_ITERM(it))
883 if (it->id == selector->baSourceID[index]) {
893 memset(input, 0, sizeof(*input));
894 input->index = index;
895 strscpy(input->name, iterm->name, sizeof(input->name));
896 if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
897 input->type = V4L2_INPUT_TYPE_CAMERA;
902 static int uvc_ioctl_g_input(struct file *file, void *fh, unsigned int *input)
904 struct uvc_fh *handle = fh;
905 struct uvc_video_chain *chain = handle->chain;
909 if (chain->selector == NULL ||
910 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
915 buf = kmalloc(1, GFP_KERNEL);
919 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, chain->selector->id,
920 chain->dev->intfnum, UVC_SU_INPUT_SELECT_CONTROL,
930 static int uvc_ioctl_s_input(struct file *file, void *fh, unsigned int input)
932 struct uvc_fh *handle = fh;
933 struct uvc_video_chain *chain = handle->chain;
937 ret = uvc_acquire_privileges(handle);
941 if (chain->selector == NULL ||
942 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
948 if (input >= chain->selector->bNrInPins)
951 buf = kmalloc(1, GFP_KERNEL);
956 ret = uvc_query_ctrl(chain->dev, UVC_SET_CUR, chain->selector->id,
957 chain->dev->intfnum, UVC_SU_INPUT_SELECT_CONTROL,
964 static int uvc_ioctl_queryctrl(struct file *file, void *fh,
965 struct v4l2_queryctrl *qc)
967 struct uvc_fh *handle = fh;
968 struct uvc_video_chain *chain = handle->chain;
970 return uvc_query_v4l2_ctrl(chain, qc);
973 static int uvc_ioctl_query_ext_ctrl(struct file *file, void *fh,
974 struct v4l2_query_ext_ctrl *qec)
976 struct uvc_fh *handle = fh;
977 struct uvc_video_chain *chain = handle->chain;
978 struct v4l2_queryctrl qc = { qec->id };
981 ret = uvc_query_v4l2_ctrl(chain, &qc);
987 strscpy(qec->name, qc.name, sizeof(qec->name));
988 qec->minimum = qc.minimum;
989 qec->maximum = qc.maximum;
991 qec->default_value = qc.default_value;
992 qec->flags = qc.flags;
996 memset(qec->dims, 0, sizeof(qec->dims));
997 memset(qec->reserved, 0, sizeof(qec->reserved));
1002 static int uvc_ioctl_g_ctrl(struct file *file, void *fh,
1003 struct v4l2_control *ctrl)
1005 struct uvc_fh *handle = fh;
1006 struct uvc_video_chain *chain = handle->chain;
1007 struct v4l2_ext_control xctrl;
1010 memset(&xctrl, 0, sizeof(xctrl));
1011 xctrl.id = ctrl->id;
1013 ret = uvc_ctrl_begin(chain);
1017 ret = uvc_ctrl_get(chain, &xctrl);
1018 uvc_ctrl_rollback(handle);
1022 ctrl->value = xctrl.value;
1026 static int uvc_ioctl_s_ctrl(struct file *file, void *fh,
1027 struct v4l2_control *ctrl)
1029 struct uvc_fh *handle = fh;
1030 struct uvc_video_chain *chain = handle->chain;
1031 struct v4l2_ext_control xctrl;
1034 memset(&xctrl, 0, sizeof(xctrl));
1035 xctrl.id = ctrl->id;
1036 xctrl.value = ctrl->value;
1038 ret = uvc_ctrl_begin(chain);
1042 ret = uvc_ctrl_set(handle, &xctrl);
1044 uvc_ctrl_rollback(handle);
1048 ret = uvc_ctrl_commit(handle, &xctrl, 1);
1052 ctrl->value = xctrl.value;
1056 static int uvc_ioctl_g_ext_ctrls(struct file *file, void *fh,
1057 struct v4l2_ext_controls *ctrls)
1059 struct uvc_fh *handle = fh;
1060 struct uvc_video_chain *chain = handle->chain;
1061 struct v4l2_ext_control *ctrl = ctrls->controls;
1065 if (ctrls->which == V4L2_CTRL_WHICH_DEF_VAL) {
1066 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
1067 struct v4l2_queryctrl qc = { .id = ctrl->id };
1069 ret = uvc_query_v4l2_ctrl(chain, &qc);
1071 ctrls->error_idx = i;
1075 ctrl->value = qc.default_value;
1081 ret = uvc_ctrl_begin(chain);
1085 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
1086 ret = uvc_ctrl_get(chain, ctrl);
1088 uvc_ctrl_rollback(handle);
1089 ctrls->error_idx = i;
1094 ctrls->error_idx = 0;
1096 return uvc_ctrl_rollback(handle);
1099 static int uvc_ioctl_s_try_ext_ctrls(struct uvc_fh *handle,
1100 struct v4l2_ext_controls *ctrls,
1103 struct v4l2_ext_control *ctrl = ctrls->controls;
1104 struct uvc_video_chain *chain = handle->chain;
1108 /* Default value cannot be changed */
1109 if (ctrls->which == V4L2_CTRL_WHICH_DEF_VAL)
1112 ret = uvc_ctrl_begin(chain);
1116 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
1117 ret = uvc_ctrl_set(handle, ctrl);
1119 uvc_ctrl_rollback(handle);
1120 ctrls->error_idx = commit ? ctrls->count : i;
1125 ctrls->error_idx = 0;
1128 return uvc_ctrl_commit(handle, ctrls->controls, ctrls->count);
1130 return uvc_ctrl_rollback(handle);
1133 static int uvc_ioctl_s_ext_ctrls(struct file *file, void *fh,
1134 struct v4l2_ext_controls *ctrls)
1136 struct uvc_fh *handle = fh;
1138 return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, true);
1141 static int uvc_ioctl_try_ext_ctrls(struct file *file, void *fh,
1142 struct v4l2_ext_controls *ctrls)
1144 struct uvc_fh *handle = fh;
1146 return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, false);
1149 static int uvc_ioctl_querymenu(struct file *file, void *fh,
1150 struct v4l2_querymenu *qm)
1152 struct uvc_fh *handle = fh;
1153 struct uvc_video_chain *chain = handle->chain;
1155 return uvc_query_v4l2_menu(chain, qm);
1158 static int uvc_ioctl_g_selection(struct file *file, void *fh,
1159 struct v4l2_selection *sel)
1161 struct uvc_fh *handle = fh;
1162 struct uvc_streaming *stream = handle->stream;
1164 if (sel->type != stream->type)
1167 switch (sel->target) {
1168 case V4L2_SEL_TGT_CROP_DEFAULT:
1169 case V4L2_SEL_TGT_CROP_BOUNDS:
1170 if (stream->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1173 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1174 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1175 if (stream->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1184 mutex_lock(&stream->mutex);
1185 sel->r.width = stream->cur_frame->wWidth;
1186 sel->r.height = stream->cur_frame->wHeight;
1187 mutex_unlock(&stream->mutex);
1192 static int uvc_ioctl_g_parm(struct file *file, void *fh,
1193 struct v4l2_streamparm *parm)
1195 struct uvc_fh *handle = fh;
1196 struct uvc_streaming *stream = handle->stream;
1198 return uvc_v4l2_get_streamparm(stream, parm);
1201 static int uvc_ioctl_s_parm(struct file *file, void *fh,
1202 struct v4l2_streamparm *parm)
1204 struct uvc_fh *handle = fh;
1205 struct uvc_streaming *stream = handle->stream;
1208 ret = uvc_acquire_privileges(handle);
1212 return uvc_v4l2_set_streamparm(stream, parm);
1215 static int uvc_ioctl_enum_framesizes(struct file *file, void *fh,
1216 struct v4l2_frmsizeenum *fsize)
1218 struct uvc_fh *handle = fh;
1219 struct uvc_streaming *stream = handle->stream;
1220 struct uvc_format *format = NULL;
1221 struct uvc_frame *frame = NULL;
1225 /* Look for the given pixel format */
1226 for (i = 0; i < stream->nformats; i++) {
1227 if (stream->format[i].fcc == fsize->pixel_format) {
1228 format = &stream->format[i];
1235 /* Skip duplicate frame sizes */
1236 for (i = 0, index = 0; i < format->nframes; i++) {
1237 if (frame && frame->wWidth == format->frame[i].wWidth &&
1238 frame->wHeight == format->frame[i].wHeight)
1240 frame = &format->frame[i];
1241 if (index == fsize->index)
1246 if (i == format->nframes)
1249 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1250 fsize->discrete.width = frame->wWidth;
1251 fsize->discrete.height = frame->wHeight;
1255 static int uvc_ioctl_enum_frameintervals(struct file *file, void *fh,
1256 struct v4l2_frmivalenum *fival)
1258 struct uvc_fh *handle = fh;
1259 struct uvc_streaming *stream = handle->stream;
1260 struct uvc_format *format = NULL;
1261 struct uvc_frame *frame = NULL;
1262 unsigned int nintervals;
1266 /* Look for the given pixel format and frame size */
1267 for (i = 0; i < stream->nformats; i++) {
1268 if (stream->format[i].fcc == fival->pixel_format) {
1269 format = &stream->format[i];
1276 index = fival->index;
1277 for (i = 0; i < format->nframes; i++) {
1278 if (format->frame[i].wWidth == fival->width &&
1279 format->frame[i].wHeight == fival->height) {
1280 frame = &format->frame[i];
1281 nintervals = frame->bFrameIntervalType ?: 1;
1282 if (index < nintervals)
1284 index -= nintervals;
1287 if (i == format->nframes)
1290 if (frame->bFrameIntervalType) {
1291 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1292 fival->discrete.numerator =
1293 frame->dwFrameInterval[index];
1294 fival->discrete.denominator = 10000000;
1295 uvc_simplify_fraction(&fival->discrete.numerator,
1296 &fival->discrete.denominator, 8, 333);
1298 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
1299 fival->stepwise.min.numerator = frame->dwFrameInterval[0];
1300 fival->stepwise.min.denominator = 10000000;
1301 fival->stepwise.max.numerator = frame->dwFrameInterval[1];
1302 fival->stepwise.max.denominator = 10000000;
1303 fival->stepwise.step.numerator = frame->dwFrameInterval[2];
1304 fival->stepwise.step.denominator = 10000000;
1305 uvc_simplify_fraction(&fival->stepwise.min.numerator,
1306 &fival->stepwise.min.denominator, 8, 333);
1307 uvc_simplify_fraction(&fival->stepwise.max.numerator,
1308 &fival->stepwise.max.denominator, 8, 333);
1309 uvc_simplify_fraction(&fival->stepwise.step.numerator,
1310 &fival->stepwise.step.denominator, 8, 333);
1316 static int uvc_ioctl_subscribe_event(struct v4l2_fh *fh,
1317 const struct v4l2_event_subscription *sub)
1319 switch (sub->type) {
1320 case V4L2_EVENT_CTRL:
1321 return v4l2_event_subscribe(fh, sub, 0, &uvc_ctrl_sub_ev_ops);
1327 static long uvc_ioctl_default(struct file *file, void *fh, bool valid_prio,
1328 unsigned int cmd, void *arg)
1330 struct uvc_fh *handle = fh;
1331 struct uvc_video_chain *chain = handle->chain;
1334 /* Dynamic controls. */
1335 case UVCIOC_CTRL_MAP:
1336 return uvc_ioctl_ctrl_map(chain, arg);
1338 case UVCIOC_CTRL_QUERY:
1339 return uvc_xu_ctrl_query(chain, arg);
1346 #ifdef CONFIG_COMPAT
1347 struct uvc_xu_control_mapping32 {
1358 compat_caddr_t menu_info;
1364 static int uvc_v4l2_get_xu_mapping(struct uvc_xu_control_mapping *kp,
1365 const struct uvc_xu_control_mapping32 __user *up)
1367 struct uvc_xu_control_mapping32 *p = (void *)kp;
1368 compat_caddr_t info;
1371 if (copy_from_user(p, up, sizeof(*p)))
1374 count = p->menu_count;
1375 info = p->menu_info;
1377 memset(kp->reserved, 0, sizeof(kp->reserved));
1378 kp->menu_info = count ? compat_ptr(info) : NULL;
1379 kp->menu_count = count;
1383 static int uvc_v4l2_put_xu_mapping(const struct uvc_xu_control_mapping *kp,
1384 struct uvc_xu_control_mapping32 __user *up)
1386 if (copy_to_user(up, kp, offsetof(typeof(*up), menu_info)) ||
1387 put_user(kp->menu_count, &up->menu_count))
1390 if (clear_user(up->reserved, sizeof(up->reserved)))
1396 struct uvc_xu_control_query32 {
1401 compat_caddr_t data;
1404 static int uvc_v4l2_get_xu_query(struct uvc_xu_control_query *kp,
1405 const struct uvc_xu_control_query32 __user *up)
1407 struct uvc_xu_control_query32 v;
1409 if (copy_from_user(&v, up, sizeof(v)))
1412 *kp = (struct uvc_xu_control_query){
1414 .selector = v.selector,
1417 .data = v.size ? compat_ptr(v.data) : NULL
1422 static int uvc_v4l2_put_xu_query(const struct uvc_xu_control_query *kp,
1423 struct uvc_xu_control_query32 __user *up)
1425 if (copy_to_user(up, kp, offsetof(typeof(*up), data)))
1430 #define UVCIOC_CTRL_MAP32 _IOWR('u', 0x20, struct uvc_xu_control_mapping32)
1431 #define UVCIOC_CTRL_QUERY32 _IOWR('u', 0x21, struct uvc_xu_control_query32)
1433 static long uvc_v4l2_compat_ioctl32(struct file *file,
1434 unsigned int cmd, unsigned long arg)
1436 struct uvc_fh *handle = file->private_data;
1438 struct uvc_xu_control_mapping xmap;
1439 struct uvc_xu_control_query xqry;
1441 void __user *up = compat_ptr(arg);
1445 case UVCIOC_CTRL_MAP32:
1446 ret = uvc_v4l2_get_xu_mapping(&karg.xmap, up);
1449 ret = uvc_ioctl_ctrl_map(handle->chain, &karg.xmap);
1452 ret = uvc_v4l2_put_xu_mapping(&karg.xmap, up);
1458 case UVCIOC_CTRL_QUERY32:
1459 ret = uvc_v4l2_get_xu_query(&karg.xqry, up);
1462 ret = uvc_xu_ctrl_query(handle->chain, &karg.xqry);
1465 ret = uvc_v4l2_put_xu_query(&karg.xqry, up);
1471 return -ENOIOCTLCMD;
1478 static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1479 size_t count, loff_t *ppos)
1481 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
1485 static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1487 struct uvc_fh *handle = file->private_data;
1488 struct uvc_streaming *stream = handle->stream;
1490 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
1492 return uvc_queue_mmap(&stream->queue, vma);
1495 static __poll_t uvc_v4l2_poll(struct file *file, poll_table *wait)
1497 struct uvc_fh *handle = file->private_data;
1498 struct uvc_streaming *stream = handle->stream;
1500 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
1502 return uvc_queue_poll(&stream->queue, file, wait);
1506 static unsigned long uvc_v4l2_get_unmapped_area(struct file *file,
1507 unsigned long addr, unsigned long len, unsigned long pgoff,
1508 unsigned long flags)
1510 struct uvc_fh *handle = file->private_data;
1511 struct uvc_streaming *stream = handle->stream;
1513 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_get_unmapped_area\n");
1515 return uvc_queue_get_unmapped_area(&stream->queue, pgoff);
1519 const struct v4l2_ioctl_ops uvc_ioctl_ops = {
1520 .vidioc_querycap = uvc_ioctl_querycap,
1521 .vidioc_enum_fmt_vid_cap = uvc_ioctl_enum_fmt_vid_cap,
1522 .vidioc_enum_fmt_vid_out = uvc_ioctl_enum_fmt_vid_out,
1523 .vidioc_g_fmt_vid_cap = uvc_ioctl_g_fmt_vid_cap,
1524 .vidioc_g_fmt_vid_out = uvc_ioctl_g_fmt_vid_out,
1525 .vidioc_s_fmt_vid_cap = uvc_ioctl_s_fmt_vid_cap,
1526 .vidioc_s_fmt_vid_out = uvc_ioctl_s_fmt_vid_out,
1527 .vidioc_try_fmt_vid_cap = uvc_ioctl_try_fmt_vid_cap,
1528 .vidioc_try_fmt_vid_out = uvc_ioctl_try_fmt_vid_out,
1529 .vidioc_reqbufs = uvc_ioctl_reqbufs,
1530 .vidioc_querybuf = uvc_ioctl_querybuf,
1531 .vidioc_qbuf = uvc_ioctl_qbuf,
1532 .vidioc_expbuf = uvc_ioctl_expbuf,
1533 .vidioc_dqbuf = uvc_ioctl_dqbuf,
1534 .vidioc_create_bufs = uvc_ioctl_create_bufs,
1535 .vidioc_streamon = uvc_ioctl_streamon,
1536 .vidioc_streamoff = uvc_ioctl_streamoff,
1537 .vidioc_enum_input = uvc_ioctl_enum_input,
1538 .vidioc_g_input = uvc_ioctl_g_input,
1539 .vidioc_s_input = uvc_ioctl_s_input,
1540 .vidioc_queryctrl = uvc_ioctl_queryctrl,
1541 .vidioc_query_ext_ctrl = uvc_ioctl_query_ext_ctrl,
1542 .vidioc_g_ctrl = uvc_ioctl_g_ctrl,
1543 .vidioc_s_ctrl = uvc_ioctl_s_ctrl,
1544 .vidioc_g_ext_ctrls = uvc_ioctl_g_ext_ctrls,
1545 .vidioc_s_ext_ctrls = uvc_ioctl_s_ext_ctrls,
1546 .vidioc_try_ext_ctrls = uvc_ioctl_try_ext_ctrls,
1547 .vidioc_querymenu = uvc_ioctl_querymenu,
1548 .vidioc_g_selection = uvc_ioctl_g_selection,
1549 .vidioc_g_parm = uvc_ioctl_g_parm,
1550 .vidioc_s_parm = uvc_ioctl_s_parm,
1551 .vidioc_enum_framesizes = uvc_ioctl_enum_framesizes,
1552 .vidioc_enum_frameintervals = uvc_ioctl_enum_frameintervals,
1553 .vidioc_subscribe_event = uvc_ioctl_subscribe_event,
1554 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1555 .vidioc_default = uvc_ioctl_default,
1558 const struct v4l2_file_operations uvc_fops = {
1559 .owner = THIS_MODULE,
1560 .open = uvc_v4l2_open,
1561 .release = uvc_v4l2_release,
1562 .unlocked_ioctl = video_ioctl2,
1563 #ifdef CONFIG_COMPAT
1564 .compat_ioctl32 = uvc_v4l2_compat_ioctl32,
1566 .read = uvc_v4l2_read,
1567 .mmap = uvc_v4l2_mmap,
1568 .poll = uvc_v4l2_poll,
1570 .get_unmapped_area = uvc_v4l2_get_unmapped_area,