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/bits.h>
10 #include <linux/compat.h>
11 #include <linux/kernel.h>
12 #include <linux/list.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/usb.h>
16 #include <linux/videodev2.h>
17 #include <linux/vmalloc.h>
19 #include <linux/wait.h>
20 #include <linux/atomic.h>
22 #include <media/v4l2-common.h>
23 #include <media/v4l2-ctrls.h>
24 #include <media/v4l2-event.h>
25 #include <media/v4l2-ioctl.h>
29 static int uvc_control_add_xu_mapping(struct uvc_video_chain *chain,
30 struct uvc_control_mapping *map,
31 const struct uvc_xu_control_mapping *xmap)
38 * Prevent excessive memory consumption, as well as integer
41 if (xmap->menu_count == 0 ||
42 xmap->menu_count > UVC_MAX_CONTROL_MENU_ENTRIES)
45 map->menu_names = NULL;
46 map->menu_mapping = NULL;
48 map->menu_mask = GENMASK(xmap->menu_count - 1, 0);
50 size = xmap->menu_count * sizeof(*map->menu_mapping);
51 map->menu_mapping = kzalloc(size, GFP_KERNEL);
52 if (!map->menu_mapping) {
57 for (i = 0; i < xmap->menu_count ; i++) {
58 if (copy_from_user((u32 *)&map->menu_mapping[i],
59 &xmap->menu_info[i].value,
60 sizeof(map->menu_mapping[i]))) {
67 * Always use the standard naming if available, otherwise copy the
68 * names supplied by userspace.
70 if (!v4l2_ctrl_get_menu(map->id)) {
71 size = xmap->menu_count * sizeof(map->menu_names[0]);
72 map->menu_names = kzalloc(size, GFP_KERNEL);
73 if (!map->menu_names) {
78 for (i = 0; i < xmap->menu_count ; i++) {
79 /* sizeof(names[i]) - 1: to take care of \0 */
80 if (copy_from_user((char *)map->menu_names[i],
81 xmap->menu_info[i].name,
82 sizeof(map->menu_names[i]) - 1)) {
89 ret = uvc_ctrl_add_mapping(chain, map);
92 kfree(map->menu_names);
93 map->menu_names = NULL;
94 kfree(map->menu_mapping);
95 map->menu_mapping = NULL;
100 /* ------------------------------------------------------------------------
103 static int uvc_ioctl_xu_ctrl_map(struct uvc_video_chain *chain,
104 struct uvc_xu_control_mapping *xmap)
106 struct uvc_control_mapping *map;
109 map = kzalloc(sizeof(*map), GFP_KERNEL);
114 /* Non standard control id. */
115 if (v4l2_ctrl_get_name(map->id) == NULL) {
116 if (xmap->name[0] == '\0') {
120 xmap->name[sizeof(xmap->name) - 1] = '\0';
121 map->name = xmap->name;
123 memcpy(map->entity, xmap->entity, sizeof(map->entity));
124 map->selector = xmap->selector;
125 map->size = xmap->size;
126 map->offset = xmap->offset;
127 map->v4l2_type = xmap->v4l2_type;
128 map->data_type = xmap->data_type;
130 switch (xmap->v4l2_type) {
131 case V4L2_CTRL_TYPE_INTEGER:
132 case V4L2_CTRL_TYPE_BOOLEAN:
133 case V4L2_CTRL_TYPE_BUTTON:
134 ret = uvc_ctrl_add_mapping(chain, map);
137 case V4L2_CTRL_TYPE_MENU:
138 ret = uvc_control_add_xu_mapping(chain, map, xmap);
142 uvc_dbg(chain->dev, CONTROL,
143 "Unsupported V4L2 control type %u\n", xmap->v4l2_type);
154 /* ------------------------------------------------------------------------
159 * Find the frame interval closest to the requested frame interval for the
160 * given frame format and size. This should be done by the device as part of
161 * the Video Probe and Commit negotiation, but some hardware don't implement
164 static u32 uvc_try_frame_interval(const struct uvc_frame *frame, u32 interval)
168 if (frame->bFrameIntervalType) {
171 for (i = 0; i < frame->bFrameIntervalType; ++i) {
172 dist = interval > frame->dwFrameInterval[i]
173 ? interval - frame->dwFrameInterval[i]
174 : frame->dwFrameInterval[i] - interval;
182 interval = frame->dwFrameInterval[i-1];
184 const u32 min = frame->dwFrameInterval[0];
185 const u32 max = frame->dwFrameInterval[1];
186 const u32 step = frame->dwFrameInterval[2];
188 interval = min + (interval - min + step/2) / step * step;
196 static u32 uvc_v4l2_get_bytesperline(const struct uvc_format *format,
197 const struct uvc_frame *frame)
199 switch (format->fcc) {
200 case V4L2_PIX_FMT_NV12:
201 case V4L2_PIX_FMT_YVU420:
202 case V4L2_PIX_FMT_YUV420:
203 case V4L2_PIX_FMT_M420:
204 return frame->wWidth;
207 return format->bpp * frame->wWidth / 8;
211 static int uvc_v4l2_try_format(struct uvc_streaming *stream,
212 struct v4l2_format *fmt, struct uvc_streaming_control *probe,
213 const struct uvc_format **uvc_format,
214 const struct uvc_frame **uvc_frame)
216 const struct uvc_format *format = NULL;
217 const struct uvc_frame *frame = NULL;
219 unsigned int d, maxd;
225 if (fmt->type != stream->type)
228 fcc = (u8 *)&fmt->fmt.pix.pixelformat;
229 uvc_dbg(stream->dev, FORMAT, "Trying format 0x%08x (%c%c%c%c): %ux%u\n",
230 fmt->fmt.pix.pixelformat,
231 fcc[0], fcc[1], fcc[2], fcc[3],
232 fmt->fmt.pix.width, fmt->fmt.pix.height);
235 * Check if the hardware supports the requested format, use the default
238 for (i = 0; i < stream->nformats; ++i) {
239 format = &stream->formats[i];
240 if (format->fcc == fmt->fmt.pix.pixelformat)
244 if (i == stream->nformats) {
245 format = stream->def_format;
246 fmt->fmt.pix.pixelformat = format->fcc;
250 * Find the closest image size. The distance between image sizes is
251 * the size in pixels of the non-overlapping regions between the
252 * requested size and the frame-specified size.
254 rw = fmt->fmt.pix.width;
255 rh = fmt->fmt.pix.height;
256 maxd = (unsigned int)-1;
258 for (i = 0; i < format->nframes; ++i) {
259 u16 w = format->frames[i].wWidth;
260 u16 h = format->frames[i].wHeight;
262 d = min(w, rw) * min(h, rh);
263 d = w*h + rw*rh - 2*d;
266 frame = &format->frames[i];
274 uvc_dbg(stream->dev, FORMAT, "Unsupported size %ux%u\n",
275 fmt->fmt.pix.width, fmt->fmt.pix.height);
279 /* Use the default frame interval. */
280 interval = frame->dwDefaultFrameInterval;
281 uvc_dbg(stream->dev, FORMAT,
282 "Using default frame interval %u.%u us (%u.%u fps)\n",
283 interval / 10, interval % 10, 10000000 / interval,
284 (100000000 / interval) % 10);
286 /* Set the format index, frame index and frame interval. */
287 memset(probe, 0, sizeof(*probe));
288 probe->bmHint = 1; /* dwFrameInterval */
289 probe->bFormatIndex = format->index;
290 probe->bFrameIndex = frame->bFrameIndex;
291 probe->dwFrameInterval = uvc_try_frame_interval(frame, interval);
293 * Some webcams stall the probe control set request when the
294 * dwMaxVideoFrameSize field is set to zero. The UVC specification
295 * clearly states that the field is read-only from the host, so this
296 * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by
297 * the webcam to work around the problem.
299 * The workaround could probably be enabled for all webcams, so the
300 * quirk can be removed if needed. It's currently useful to detect
301 * webcam bugs and fix them before they hit the market (providing
302 * developers test their webcams with the Linux driver as well as with
303 * the Windows driver).
305 mutex_lock(&stream->mutex);
306 if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS)
307 probe->dwMaxVideoFrameSize =
308 stream->ctrl.dwMaxVideoFrameSize;
310 /* Probe the device. */
311 ret = uvc_probe_video(stream, probe);
312 mutex_unlock(&stream->mutex);
317 * After the probe, update fmt with the values returned from
318 * negotiation with the device. Some devices return invalid bFormatIndex
319 * and bFrameIndex values, in which case we can only assume they have
320 * accepted the requested format as-is.
322 for (i = 0; i < stream->nformats; ++i) {
323 if (probe->bFormatIndex == stream->formats[i].index) {
324 format = &stream->formats[i];
329 if (i == stream->nformats)
330 uvc_dbg(stream->dev, FORMAT,
331 "Unknown bFormatIndex %u, using default\n",
332 probe->bFormatIndex);
334 for (i = 0; i < format->nframes; ++i) {
335 if (probe->bFrameIndex == format->frames[i].bFrameIndex) {
336 frame = &format->frames[i];
341 if (i == format->nframes)
342 uvc_dbg(stream->dev, FORMAT,
343 "Unknown bFrameIndex %u, using default\n",
346 fmt->fmt.pix.width = frame->wWidth;
347 fmt->fmt.pix.height = frame->wHeight;
348 fmt->fmt.pix.field = V4L2_FIELD_NONE;
349 fmt->fmt.pix.bytesperline = uvc_v4l2_get_bytesperline(format, frame);
350 fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize;
351 fmt->fmt.pix.pixelformat = format->fcc;
352 fmt->fmt.pix.colorspace = format->colorspace;
353 fmt->fmt.pix.xfer_func = format->xfer_func;
354 fmt->fmt.pix.ycbcr_enc = format->ycbcr_enc;
356 if (uvc_format != NULL)
357 *uvc_format = format;
358 if (uvc_frame != NULL)
364 static int uvc_v4l2_get_format(struct uvc_streaming *stream,
365 struct v4l2_format *fmt)
367 const struct uvc_format *format;
368 const struct uvc_frame *frame;
371 if (fmt->type != stream->type)
374 mutex_lock(&stream->mutex);
375 format = stream->cur_format;
376 frame = stream->cur_frame;
378 if (format == NULL || frame == NULL) {
383 fmt->fmt.pix.pixelformat = format->fcc;
384 fmt->fmt.pix.width = frame->wWidth;
385 fmt->fmt.pix.height = frame->wHeight;
386 fmt->fmt.pix.field = V4L2_FIELD_NONE;
387 fmt->fmt.pix.bytesperline = uvc_v4l2_get_bytesperline(format, frame);
388 fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize;
389 fmt->fmt.pix.colorspace = format->colorspace;
390 fmt->fmt.pix.xfer_func = format->xfer_func;
391 fmt->fmt.pix.ycbcr_enc = format->ycbcr_enc;
394 mutex_unlock(&stream->mutex);
398 static int uvc_v4l2_set_format(struct uvc_streaming *stream,
399 struct v4l2_format *fmt)
401 struct uvc_streaming_control probe;
402 const struct uvc_format *format;
403 const struct uvc_frame *frame;
406 if (fmt->type != stream->type)
409 ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame);
413 mutex_lock(&stream->mutex);
415 if (uvc_queue_allocated(&stream->queue)) {
420 stream->ctrl = probe;
421 stream->cur_format = format;
422 stream->cur_frame = frame;
425 mutex_unlock(&stream->mutex);
429 static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream,
430 struct v4l2_streamparm *parm)
432 u32 numerator, denominator;
434 if (parm->type != stream->type)
437 mutex_lock(&stream->mutex);
438 numerator = stream->ctrl.dwFrameInterval;
439 mutex_unlock(&stream->mutex);
441 denominator = 10000000;
442 v4l2_simplify_fraction(&numerator, &denominator, 8, 333);
444 memset(parm, 0, sizeof(*parm));
445 parm->type = stream->type;
447 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
448 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
449 parm->parm.capture.capturemode = 0;
450 parm->parm.capture.timeperframe.numerator = numerator;
451 parm->parm.capture.timeperframe.denominator = denominator;
452 parm->parm.capture.extendedmode = 0;
453 parm->parm.capture.readbuffers = 0;
455 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
456 parm->parm.output.outputmode = 0;
457 parm->parm.output.timeperframe.numerator = numerator;
458 parm->parm.output.timeperframe.denominator = denominator;
464 static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
465 struct v4l2_streamparm *parm)
467 struct uvc_streaming_control probe;
468 struct v4l2_fract timeperframe;
469 const struct uvc_format *format;
470 const struct uvc_frame *frame;
475 if (parm->type != stream->type)
478 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
479 timeperframe = parm->parm.capture.timeperframe;
481 timeperframe = parm->parm.output.timeperframe;
483 interval = v4l2_fraction_to_interval(timeperframe.numerator,
484 timeperframe.denominator);
485 uvc_dbg(stream->dev, FORMAT, "Setting frame interval to %u/%u (%u)\n",
486 timeperframe.numerator, timeperframe.denominator, interval);
488 mutex_lock(&stream->mutex);
490 if (uvc_queue_streaming(&stream->queue)) {
491 mutex_unlock(&stream->mutex);
495 format = stream->cur_format;
496 frame = stream->cur_frame;
497 probe = stream->ctrl;
498 probe.dwFrameInterval = uvc_try_frame_interval(frame, interval);
499 maxd = abs((s32)probe.dwFrameInterval - interval);
501 /* Try frames with matching size to find the best frame interval. */
502 for (i = 0; i < format->nframes && maxd != 0; i++) {
505 if (&format->frames[i] == stream->cur_frame)
508 if (format->frames[i].wWidth != stream->cur_frame->wWidth ||
509 format->frames[i].wHeight != stream->cur_frame->wHeight)
512 ival = uvc_try_frame_interval(&format->frames[i], interval);
513 d = abs((s32)ival - interval);
517 frame = &format->frames[i];
518 probe.bFrameIndex = frame->bFrameIndex;
519 probe.dwFrameInterval = ival;
523 /* Probe the device with the new settings. */
524 ret = uvc_probe_video(stream, &probe);
526 mutex_unlock(&stream->mutex);
530 stream->ctrl = probe;
531 stream->cur_frame = frame;
532 mutex_unlock(&stream->mutex);
534 /* Return the actual frame period. */
535 timeperframe.numerator = probe.dwFrameInterval;
536 timeperframe.denominator = 10000000;
537 v4l2_simplify_fraction(&timeperframe.numerator,
538 &timeperframe.denominator, 8, 333);
540 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
541 parm->parm.capture.timeperframe = timeperframe;
542 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
544 parm->parm.output.timeperframe = timeperframe;
545 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
551 /* ------------------------------------------------------------------------
552 * Privilege management
556 * Privilege management is the multiple-open implementation basis. The current
557 * implementation is completely transparent for the end-user and doesn't
558 * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls.
559 * Those ioctls enable finer control on the device (by making possible for a
560 * user to request exclusive access to a device), but are not mature yet.
561 * Switching to the V4L2 priority mechanism might be considered in the future
562 * if this situation changes.
564 * Each open instance of a UVC device can either be in a privileged or
565 * unprivileged state. Only a single instance can be in a privileged state at
566 * a given time. Trying to perform an operation that requires privileges will
567 * automatically acquire the required privileges if possible, or return -EBUSY
568 * otherwise. Privileges are dismissed when closing the instance or when
569 * freeing the video buffers using VIDIOC_REQBUFS.
571 * Operations that require privileges are:
578 static int uvc_acquire_privileges(struct uvc_fh *handle)
580 /* Always succeed if the handle is already privileged. */
581 if (handle->state == UVC_HANDLE_ACTIVE)
584 /* Check if the device already has a privileged handle. */
585 if (atomic_inc_return(&handle->stream->active) != 1) {
586 atomic_dec(&handle->stream->active);
590 handle->state = UVC_HANDLE_ACTIVE;
594 static void uvc_dismiss_privileges(struct uvc_fh *handle)
596 if (handle->state == UVC_HANDLE_ACTIVE)
597 atomic_dec(&handle->stream->active);
599 handle->state = UVC_HANDLE_PASSIVE;
602 static int uvc_has_privileges(struct uvc_fh *handle)
604 return handle->state == UVC_HANDLE_ACTIVE;
607 /* ------------------------------------------------------------------------
608 * V4L2 file operations
611 static int uvc_v4l2_open(struct file *file)
613 struct uvc_streaming *stream;
614 struct uvc_fh *handle;
617 stream = video_drvdata(file);
618 uvc_dbg(stream->dev, CALLS, "%s\n", __func__);
620 ret = usb_autopm_get_interface(stream->dev->intf);
624 /* Create the device handle. */
625 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
626 if (handle == NULL) {
627 usb_autopm_put_interface(stream->dev->intf);
631 mutex_lock(&stream->dev->lock);
632 if (stream->dev->users == 0) {
633 ret = uvc_status_start(stream->dev, GFP_KERNEL);
635 mutex_unlock(&stream->dev->lock);
636 usb_autopm_put_interface(stream->dev->intf);
642 stream->dev->users++;
643 mutex_unlock(&stream->dev->lock);
645 v4l2_fh_init(&handle->vfh, &stream->vdev);
646 v4l2_fh_add(&handle->vfh);
647 handle->chain = stream->chain;
648 handle->stream = stream;
649 handle->state = UVC_HANDLE_PASSIVE;
650 file->private_data = handle;
655 static int uvc_v4l2_release(struct file *file)
657 struct uvc_fh *handle = file->private_data;
658 struct uvc_streaming *stream = handle->stream;
660 uvc_dbg(stream->dev, CALLS, "%s\n", __func__);
662 /* Only free resources if this is a privileged handle. */
663 if (uvc_has_privileges(handle))
664 uvc_queue_release(&stream->queue);
666 /* Release the file handle. */
667 uvc_dismiss_privileges(handle);
668 v4l2_fh_del(&handle->vfh);
669 v4l2_fh_exit(&handle->vfh);
671 file->private_data = NULL;
673 mutex_lock(&stream->dev->lock);
674 if (--stream->dev->users == 0)
675 uvc_status_stop(stream->dev);
676 mutex_unlock(&stream->dev->lock);
678 usb_autopm_put_interface(stream->dev->intf);
682 static int uvc_ioctl_querycap(struct file *file, void *fh,
683 struct v4l2_capability *cap)
685 struct uvc_fh *handle = file->private_data;
686 struct uvc_video_chain *chain = handle->chain;
687 struct uvc_streaming *stream = handle->stream;
689 strscpy(cap->driver, "uvcvideo", sizeof(cap->driver));
690 strscpy(cap->card, handle->stream->dev->name, sizeof(cap->card));
691 usb_make_path(stream->dev->udev, cap->bus_info, sizeof(cap->bus_info));
692 cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
698 static int uvc_ioctl_enum_fmt(struct uvc_streaming *stream,
699 struct v4l2_fmtdesc *fmt)
701 const struct uvc_format *format;
702 enum v4l2_buf_type type = fmt->type;
703 u32 index = fmt->index;
705 if (fmt->type != stream->type || fmt->index >= stream->nformats)
708 memset(fmt, 0, sizeof(*fmt));
712 format = &stream->formats[fmt->index];
714 if (format->flags & UVC_FMT_FLAG_COMPRESSED)
715 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
716 fmt->pixelformat = format->fcc;
720 static int uvc_ioctl_enum_fmt_vid_cap(struct file *file, void *fh,
721 struct v4l2_fmtdesc *fmt)
723 struct uvc_fh *handle = fh;
724 struct uvc_streaming *stream = handle->stream;
726 return uvc_ioctl_enum_fmt(stream, fmt);
729 static int uvc_ioctl_enum_fmt_vid_out(struct file *file, void *fh,
730 struct v4l2_fmtdesc *fmt)
732 struct uvc_fh *handle = fh;
733 struct uvc_streaming *stream = handle->stream;
735 return uvc_ioctl_enum_fmt(stream, fmt);
738 static int uvc_ioctl_g_fmt_vid_cap(struct file *file, void *fh,
739 struct v4l2_format *fmt)
741 struct uvc_fh *handle = fh;
742 struct uvc_streaming *stream = handle->stream;
744 return uvc_v4l2_get_format(stream, fmt);
747 static int uvc_ioctl_g_fmt_vid_out(struct file *file, void *fh,
748 struct v4l2_format *fmt)
750 struct uvc_fh *handle = fh;
751 struct uvc_streaming *stream = handle->stream;
753 return uvc_v4l2_get_format(stream, fmt);
756 static int uvc_ioctl_s_fmt_vid_cap(struct file *file, void *fh,
757 struct v4l2_format *fmt)
759 struct uvc_fh *handle = fh;
760 struct uvc_streaming *stream = handle->stream;
763 ret = uvc_acquire_privileges(handle);
767 return uvc_v4l2_set_format(stream, fmt);
770 static int uvc_ioctl_s_fmt_vid_out(struct file *file, void *fh,
771 struct v4l2_format *fmt)
773 struct uvc_fh *handle = fh;
774 struct uvc_streaming *stream = handle->stream;
777 ret = uvc_acquire_privileges(handle);
781 return uvc_v4l2_set_format(stream, fmt);
784 static int uvc_ioctl_try_fmt_vid_cap(struct file *file, void *fh,
785 struct v4l2_format *fmt)
787 struct uvc_fh *handle = fh;
788 struct uvc_streaming *stream = handle->stream;
789 struct uvc_streaming_control probe;
791 return uvc_v4l2_try_format(stream, fmt, &probe, NULL, NULL);
794 static int uvc_ioctl_try_fmt_vid_out(struct file *file, void *fh,
795 struct v4l2_format *fmt)
797 struct uvc_fh *handle = fh;
798 struct uvc_streaming *stream = handle->stream;
799 struct uvc_streaming_control probe;
801 return uvc_v4l2_try_format(stream, fmt, &probe, NULL, NULL);
804 static int uvc_ioctl_reqbufs(struct file *file, void *fh,
805 struct v4l2_requestbuffers *rb)
807 struct uvc_fh *handle = fh;
808 struct uvc_streaming *stream = handle->stream;
811 ret = uvc_acquire_privileges(handle);
815 mutex_lock(&stream->mutex);
816 ret = uvc_request_buffers(&stream->queue, rb);
817 mutex_unlock(&stream->mutex);
822 uvc_dismiss_privileges(handle);
827 static int uvc_ioctl_querybuf(struct file *file, void *fh,
828 struct v4l2_buffer *buf)
830 struct uvc_fh *handle = fh;
831 struct uvc_streaming *stream = handle->stream;
833 if (!uvc_has_privileges(handle))
836 return uvc_query_buffer(&stream->queue, buf);
839 static int uvc_ioctl_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
841 struct uvc_fh *handle = fh;
842 struct uvc_streaming *stream = handle->stream;
844 if (!uvc_has_privileges(handle))
847 return uvc_queue_buffer(&stream->queue,
848 stream->vdev.v4l2_dev->mdev, buf);
851 static int uvc_ioctl_expbuf(struct file *file, void *fh,
852 struct v4l2_exportbuffer *exp)
854 struct uvc_fh *handle = fh;
855 struct uvc_streaming *stream = handle->stream;
857 if (!uvc_has_privileges(handle))
860 return uvc_export_buffer(&stream->queue, exp);
863 static int uvc_ioctl_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
865 struct uvc_fh *handle = fh;
866 struct uvc_streaming *stream = handle->stream;
868 if (!uvc_has_privileges(handle))
871 return uvc_dequeue_buffer(&stream->queue, buf,
872 file->f_flags & O_NONBLOCK);
875 static int uvc_ioctl_create_bufs(struct file *file, void *fh,
876 struct v4l2_create_buffers *cb)
878 struct uvc_fh *handle = fh;
879 struct uvc_streaming *stream = handle->stream;
882 ret = uvc_acquire_privileges(handle);
886 return uvc_create_buffers(&stream->queue, cb);
889 static int uvc_ioctl_streamon(struct file *file, void *fh,
890 enum v4l2_buf_type type)
892 struct uvc_fh *handle = fh;
893 struct uvc_streaming *stream = handle->stream;
896 if (!uvc_has_privileges(handle))
899 mutex_lock(&stream->mutex);
900 ret = uvc_queue_streamon(&stream->queue, type);
901 mutex_unlock(&stream->mutex);
906 static int uvc_ioctl_streamoff(struct file *file, void *fh,
907 enum v4l2_buf_type type)
909 struct uvc_fh *handle = fh;
910 struct uvc_streaming *stream = handle->stream;
912 if (!uvc_has_privileges(handle))
915 mutex_lock(&stream->mutex);
916 uvc_queue_streamoff(&stream->queue, type);
917 mutex_unlock(&stream->mutex);
922 static int uvc_ioctl_enum_input(struct file *file, void *fh,
923 struct v4l2_input *input)
925 struct uvc_fh *handle = fh;
926 struct uvc_video_chain *chain = handle->chain;
927 const struct uvc_entity *selector = chain->selector;
928 struct uvc_entity *iterm = NULL;
929 struct uvc_entity *it;
930 u32 index = input->index;
932 if (selector == NULL ||
933 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
936 list_for_each_entry(it, &chain->entities, chain) {
937 if (UVC_ENTITY_IS_ITERM(it)) {
942 } else if (index < selector->bNrInPins) {
943 list_for_each_entry(it, &chain->entities, chain) {
944 if (!UVC_ENTITY_IS_ITERM(it))
946 if (it->id == selector->baSourceID[index]) {
956 memset(input, 0, sizeof(*input));
957 input->index = index;
958 strscpy(input->name, iterm->name, sizeof(input->name));
959 if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
960 input->type = V4L2_INPUT_TYPE_CAMERA;
965 static int uvc_ioctl_g_input(struct file *file, void *fh, unsigned int *input)
967 struct uvc_fh *handle = fh;
968 struct uvc_video_chain *chain = handle->chain;
972 if (chain->selector == NULL ||
973 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
978 buf = kmalloc(1, GFP_KERNEL);
982 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, chain->selector->id,
983 chain->dev->intfnum, UVC_SU_INPUT_SELECT_CONTROL,
993 static int uvc_ioctl_s_input(struct file *file, void *fh, unsigned int input)
995 struct uvc_fh *handle = fh;
996 struct uvc_video_chain *chain = handle->chain;
1000 ret = uvc_acquire_privileges(handle);
1004 if (chain->selector == NULL ||
1005 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
1011 if (input >= chain->selector->bNrInPins)
1014 buf = kmalloc(1, GFP_KERNEL);
1019 ret = uvc_query_ctrl(chain->dev, UVC_SET_CUR, chain->selector->id,
1020 chain->dev->intfnum, UVC_SU_INPUT_SELECT_CONTROL,
1027 static int uvc_ioctl_queryctrl(struct file *file, void *fh,
1028 struct v4l2_queryctrl *qc)
1030 struct uvc_fh *handle = fh;
1031 struct uvc_video_chain *chain = handle->chain;
1033 return uvc_query_v4l2_ctrl(chain, qc);
1036 static int uvc_ioctl_query_ext_ctrl(struct file *file, void *fh,
1037 struct v4l2_query_ext_ctrl *qec)
1039 struct uvc_fh *handle = fh;
1040 struct uvc_video_chain *chain = handle->chain;
1041 struct v4l2_queryctrl qc = { qec->id };
1044 ret = uvc_query_v4l2_ctrl(chain, &qc);
1049 qec->type = qc.type;
1050 strscpy(qec->name, qc.name, sizeof(qec->name));
1051 qec->minimum = qc.minimum;
1052 qec->maximum = qc.maximum;
1053 qec->step = qc.step;
1054 qec->default_value = qc.default_value;
1055 qec->flags = qc.flags;
1058 qec->nr_of_dims = 0;
1059 memset(qec->dims, 0, sizeof(qec->dims));
1060 memset(qec->reserved, 0, sizeof(qec->reserved));
1065 static int uvc_ctrl_check_access(struct uvc_video_chain *chain,
1066 struct v4l2_ext_controls *ctrls,
1067 unsigned long ioctl)
1069 struct v4l2_ext_control *ctrl = ctrls->controls;
1073 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
1074 ret = uvc_ctrl_is_accessible(chain, ctrl->id, ctrls, ioctl);
1079 ctrls->error_idx = ioctl == VIDIOC_TRY_EXT_CTRLS ? i : ctrls->count;
1084 static int uvc_ioctl_g_ext_ctrls(struct file *file, void *fh,
1085 struct v4l2_ext_controls *ctrls)
1087 struct uvc_fh *handle = fh;
1088 struct uvc_video_chain *chain = handle->chain;
1089 struct v4l2_ext_control *ctrl = ctrls->controls;
1093 ret = uvc_ctrl_check_access(chain, ctrls, VIDIOC_G_EXT_CTRLS);
1097 if (ctrls->which == V4L2_CTRL_WHICH_DEF_VAL) {
1098 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
1099 struct v4l2_queryctrl qc = { .id = ctrl->id };
1101 ret = uvc_query_v4l2_ctrl(chain, &qc);
1103 ctrls->error_idx = i;
1107 ctrl->value = qc.default_value;
1113 ret = uvc_ctrl_begin(chain);
1117 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
1118 ret = uvc_ctrl_get(chain, ctrl);
1120 uvc_ctrl_rollback(handle);
1121 ctrls->error_idx = i;
1126 ctrls->error_idx = 0;
1128 return uvc_ctrl_rollback(handle);
1131 static int uvc_ioctl_s_try_ext_ctrls(struct uvc_fh *handle,
1132 struct v4l2_ext_controls *ctrls,
1133 unsigned long ioctl)
1135 struct v4l2_ext_control *ctrl = ctrls->controls;
1136 struct uvc_video_chain *chain = handle->chain;
1140 ret = uvc_ctrl_check_access(chain, ctrls, ioctl);
1144 ret = uvc_ctrl_begin(chain);
1148 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
1149 ret = uvc_ctrl_set(handle, ctrl);
1151 uvc_ctrl_rollback(handle);
1152 ctrls->error_idx = ioctl == VIDIOC_S_EXT_CTRLS ?
1158 ctrls->error_idx = 0;
1160 if (ioctl == VIDIOC_S_EXT_CTRLS)
1161 return uvc_ctrl_commit(handle, ctrls);
1163 return uvc_ctrl_rollback(handle);
1166 static int uvc_ioctl_s_ext_ctrls(struct file *file, void *fh,
1167 struct v4l2_ext_controls *ctrls)
1169 struct uvc_fh *handle = fh;
1171 return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, VIDIOC_S_EXT_CTRLS);
1174 static int uvc_ioctl_try_ext_ctrls(struct file *file, void *fh,
1175 struct v4l2_ext_controls *ctrls)
1177 struct uvc_fh *handle = fh;
1179 return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, VIDIOC_TRY_EXT_CTRLS);
1182 static int uvc_ioctl_querymenu(struct file *file, void *fh,
1183 struct v4l2_querymenu *qm)
1185 struct uvc_fh *handle = fh;
1186 struct uvc_video_chain *chain = handle->chain;
1188 return uvc_query_v4l2_menu(chain, qm);
1191 static int uvc_ioctl_g_selection(struct file *file, void *fh,
1192 struct v4l2_selection *sel)
1194 struct uvc_fh *handle = fh;
1195 struct uvc_streaming *stream = handle->stream;
1197 if (sel->type != stream->type)
1200 switch (sel->target) {
1201 case V4L2_SEL_TGT_CROP_DEFAULT:
1202 case V4L2_SEL_TGT_CROP_BOUNDS:
1203 if (stream->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1206 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1207 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1208 if (stream->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1217 mutex_lock(&stream->mutex);
1218 sel->r.width = stream->cur_frame->wWidth;
1219 sel->r.height = stream->cur_frame->wHeight;
1220 mutex_unlock(&stream->mutex);
1225 static int uvc_ioctl_g_parm(struct file *file, void *fh,
1226 struct v4l2_streamparm *parm)
1228 struct uvc_fh *handle = fh;
1229 struct uvc_streaming *stream = handle->stream;
1231 return uvc_v4l2_get_streamparm(stream, parm);
1234 static int uvc_ioctl_s_parm(struct file *file, void *fh,
1235 struct v4l2_streamparm *parm)
1237 struct uvc_fh *handle = fh;
1238 struct uvc_streaming *stream = handle->stream;
1241 ret = uvc_acquire_privileges(handle);
1245 return uvc_v4l2_set_streamparm(stream, parm);
1248 static int uvc_ioctl_enum_framesizes(struct file *file, void *fh,
1249 struct v4l2_frmsizeenum *fsize)
1251 struct uvc_fh *handle = fh;
1252 struct uvc_streaming *stream = handle->stream;
1253 const struct uvc_format *format = NULL;
1254 const struct uvc_frame *frame = NULL;
1258 /* Look for the given pixel format */
1259 for (i = 0; i < stream->nformats; i++) {
1260 if (stream->formats[i].fcc == fsize->pixel_format) {
1261 format = &stream->formats[i];
1268 /* Skip duplicate frame sizes */
1269 for (i = 0, index = 0; i < format->nframes; i++) {
1270 if (frame && frame->wWidth == format->frames[i].wWidth &&
1271 frame->wHeight == format->frames[i].wHeight)
1273 frame = &format->frames[i];
1274 if (index == fsize->index)
1279 if (i == format->nframes)
1282 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1283 fsize->discrete.width = frame->wWidth;
1284 fsize->discrete.height = frame->wHeight;
1288 static int uvc_ioctl_enum_frameintervals(struct file *file, void *fh,
1289 struct v4l2_frmivalenum *fival)
1291 struct uvc_fh *handle = fh;
1292 struct uvc_streaming *stream = handle->stream;
1293 const struct uvc_format *format = NULL;
1294 const struct uvc_frame *frame = NULL;
1295 unsigned int nintervals;
1299 /* Look for the given pixel format and frame size */
1300 for (i = 0; i < stream->nformats; i++) {
1301 if (stream->formats[i].fcc == fival->pixel_format) {
1302 format = &stream->formats[i];
1309 index = fival->index;
1310 for (i = 0; i < format->nframes; i++) {
1311 if (format->frames[i].wWidth == fival->width &&
1312 format->frames[i].wHeight == fival->height) {
1313 frame = &format->frames[i];
1314 nintervals = frame->bFrameIntervalType ?: 1;
1315 if (index < nintervals)
1317 index -= nintervals;
1320 if (i == format->nframes)
1323 if (frame->bFrameIntervalType) {
1324 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1325 fival->discrete.numerator =
1326 frame->dwFrameInterval[index];
1327 fival->discrete.denominator = 10000000;
1328 v4l2_simplify_fraction(&fival->discrete.numerator,
1329 &fival->discrete.denominator, 8, 333);
1331 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
1332 fival->stepwise.min.numerator = frame->dwFrameInterval[0];
1333 fival->stepwise.min.denominator = 10000000;
1334 fival->stepwise.max.numerator = frame->dwFrameInterval[1];
1335 fival->stepwise.max.denominator = 10000000;
1336 fival->stepwise.step.numerator = frame->dwFrameInterval[2];
1337 fival->stepwise.step.denominator = 10000000;
1338 v4l2_simplify_fraction(&fival->stepwise.min.numerator,
1339 &fival->stepwise.min.denominator, 8, 333);
1340 v4l2_simplify_fraction(&fival->stepwise.max.numerator,
1341 &fival->stepwise.max.denominator, 8, 333);
1342 v4l2_simplify_fraction(&fival->stepwise.step.numerator,
1343 &fival->stepwise.step.denominator, 8, 333);
1349 static int uvc_ioctl_subscribe_event(struct v4l2_fh *fh,
1350 const struct v4l2_event_subscription *sub)
1352 switch (sub->type) {
1353 case V4L2_EVENT_CTRL:
1354 return v4l2_event_subscribe(fh, sub, 0, &uvc_ctrl_sub_ev_ops);
1360 static long uvc_ioctl_default(struct file *file, void *fh, bool valid_prio,
1361 unsigned int cmd, void *arg)
1363 struct uvc_fh *handle = fh;
1364 struct uvc_video_chain *chain = handle->chain;
1367 /* Dynamic controls. */
1368 case UVCIOC_CTRL_MAP:
1369 return uvc_ioctl_xu_ctrl_map(chain, arg);
1371 case UVCIOC_CTRL_QUERY:
1372 return uvc_xu_ctrl_query(chain, arg);
1379 #ifdef CONFIG_COMPAT
1380 struct uvc_xu_control_mapping32 {
1391 compat_caddr_t menu_info;
1397 static int uvc_v4l2_get_xu_mapping(struct uvc_xu_control_mapping *kp,
1398 const struct uvc_xu_control_mapping32 __user *up)
1400 struct uvc_xu_control_mapping32 *p = (void *)kp;
1401 compat_caddr_t info;
1404 if (copy_from_user(p, up, sizeof(*p)))
1407 count = p->menu_count;
1408 info = p->menu_info;
1410 memset(kp->reserved, 0, sizeof(kp->reserved));
1411 kp->menu_info = count ? compat_ptr(info) : NULL;
1412 kp->menu_count = count;
1416 static int uvc_v4l2_put_xu_mapping(const struct uvc_xu_control_mapping *kp,
1417 struct uvc_xu_control_mapping32 __user *up)
1419 if (copy_to_user(up, kp, offsetof(typeof(*up), menu_info)) ||
1420 put_user(kp->menu_count, &up->menu_count))
1423 if (clear_user(up->reserved, sizeof(up->reserved)))
1429 struct uvc_xu_control_query32 {
1434 compat_caddr_t data;
1437 static int uvc_v4l2_get_xu_query(struct uvc_xu_control_query *kp,
1438 const struct uvc_xu_control_query32 __user *up)
1440 struct uvc_xu_control_query32 v;
1442 if (copy_from_user(&v, up, sizeof(v)))
1445 *kp = (struct uvc_xu_control_query){
1447 .selector = v.selector,
1450 .data = v.size ? compat_ptr(v.data) : NULL
1455 static int uvc_v4l2_put_xu_query(const struct uvc_xu_control_query *kp,
1456 struct uvc_xu_control_query32 __user *up)
1458 if (copy_to_user(up, kp, offsetof(typeof(*up), data)))
1463 #define UVCIOC_CTRL_MAP32 _IOWR('u', 0x20, struct uvc_xu_control_mapping32)
1464 #define UVCIOC_CTRL_QUERY32 _IOWR('u', 0x21, struct uvc_xu_control_query32)
1466 static long uvc_v4l2_compat_ioctl32(struct file *file,
1467 unsigned int cmd, unsigned long arg)
1469 struct uvc_fh *handle = file->private_data;
1471 struct uvc_xu_control_mapping xmap;
1472 struct uvc_xu_control_query xqry;
1474 void __user *up = compat_ptr(arg);
1478 case UVCIOC_CTRL_MAP32:
1479 ret = uvc_v4l2_get_xu_mapping(&karg.xmap, up);
1482 ret = uvc_ioctl_xu_ctrl_map(handle->chain, &karg.xmap);
1485 ret = uvc_v4l2_put_xu_mapping(&karg.xmap, up);
1491 case UVCIOC_CTRL_QUERY32:
1492 ret = uvc_v4l2_get_xu_query(&karg.xqry, up);
1495 ret = uvc_xu_ctrl_query(handle->chain, &karg.xqry);
1498 ret = uvc_v4l2_put_xu_query(&karg.xqry, up);
1504 return -ENOIOCTLCMD;
1511 static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1512 size_t count, loff_t *ppos)
1514 struct uvc_fh *handle = file->private_data;
1515 struct uvc_streaming *stream = handle->stream;
1517 uvc_dbg(stream->dev, CALLS, "%s: not implemented\n", __func__);
1521 static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1523 struct uvc_fh *handle = file->private_data;
1524 struct uvc_streaming *stream = handle->stream;
1526 uvc_dbg(stream->dev, CALLS, "%s\n", __func__);
1528 return uvc_queue_mmap(&stream->queue, vma);
1531 static __poll_t uvc_v4l2_poll(struct file *file, poll_table *wait)
1533 struct uvc_fh *handle = file->private_data;
1534 struct uvc_streaming *stream = handle->stream;
1536 uvc_dbg(stream->dev, CALLS, "%s\n", __func__);
1538 return uvc_queue_poll(&stream->queue, file, wait);
1542 static unsigned long uvc_v4l2_get_unmapped_area(struct file *file,
1543 unsigned long addr, unsigned long len, unsigned long pgoff,
1544 unsigned long flags)
1546 struct uvc_fh *handle = file->private_data;
1547 struct uvc_streaming *stream = handle->stream;
1549 uvc_dbg(stream->dev, CALLS, "%s\n", __func__);
1551 return uvc_queue_get_unmapped_area(&stream->queue, pgoff);
1555 const struct v4l2_ioctl_ops uvc_ioctl_ops = {
1556 .vidioc_querycap = uvc_ioctl_querycap,
1557 .vidioc_enum_fmt_vid_cap = uvc_ioctl_enum_fmt_vid_cap,
1558 .vidioc_enum_fmt_vid_out = uvc_ioctl_enum_fmt_vid_out,
1559 .vidioc_g_fmt_vid_cap = uvc_ioctl_g_fmt_vid_cap,
1560 .vidioc_g_fmt_vid_out = uvc_ioctl_g_fmt_vid_out,
1561 .vidioc_s_fmt_vid_cap = uvc_ioctl_s_fmt_vid_cap,
1562 .vidioc_s_fmt_vid_out = uvc_ioctl_s_fmt_vid_out,
1563 .vidioc_try_fmt_vid_cap = uvc_ioctl_try_fmt_vid_cap,
1564 .vidioc_try_fmt_vid_out = uvc_ioctl_try_fmt_vid_out,
1565 .vidioc_reqbufs = uvc_ioctl_reqbufs,
1566 .vidioc_querybuf = uvc_ioctl_querybuf,
1567 .vidioc_qbuf = uvc_ioctl_qbuf,
1568 .vidioc_expbuf = uvc_ioctl_expbuf,
1569 .vidioc_dqbuf = uvc_ioctl_dqbuf,
1570 .vidioc_create_bufs = uvc_ioctl_create_bufs,
1571 .vidioc_streamon = uvc_ioctl_streamon,
1572 .vidioc_streamoff = uvc_ioctl_streamoff,
1573 .vidioc_enum_input = uvc_ioctl_enum_input,
1574 .vidioc_g_input = uvc_ioctl_g_input,
1575 .vidioc_s_input = uvc_ioctl_s_input,
1576 .vidioc_queryctrl = uvc_ioctl_queryctrl,
1577 .vidioc_query_ext_ctrl = uvc_ioctl_query_ext_ctrl,
1578 .vidioc_g_ext_ctrls = uvc_ioctl_g_ext_ctrls,
1579 .vidioc_s_ext_ctrls = uvc_ioctl_s_ext_ctrls,
1580 .vidioc_try_ext_ctrls = uvc_ioctl_try_ext_ctrls,
1581 .vidioc_querymenu = uvc_ioctl_querymenu,
1582 .vidioc_g_selection = uvc_ioctl_g_selection,
1583 .vidioc_g_parm = uvc_ioctl_g_parm,
1584 .vidioc_s_parm = uvc_ioctl_s_parm,
1585 .vidioc_enum_framesizes = uvc_ioctl_enum_framesizes,
1586 .vidioc_enum_frameintervals = uvc_ioctl_enum_frameintervals,
1587 .vidioc_subscribe_event = uvc_ioctl_subscribe_event,
1588 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1589 .vidioc_default = uvc_ioctl_default,
1592 const struct v4l2_file_operations uvc_fops = {
1593 .owner = THIS_MODULE,
1594 .open = uvc_v4l2_open,
1595 .release = uvc_v4l2_release,
1596 .unlocked_ioctl = video_ioctl2,
1597 #ifdef CONFIG_COMPAT
1598 .compat_ioctl32 = uvc_v4l2_compat_ioctl32,
1600 .read = uvc_v4l2_read,
1601 .mmap = uvc_v4l2_mmap,
1602 .poll = uvc_v4l2_poll,
1604 .get_unmapped_area = uvc_v4l2_get_unmapped_area,