2 * uvc_v4l2.c -- USB Video Class driver - V4L2 API
4 * Copyright (C) 2005-2010
5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
14 #include <linux/compat.h>
15 #include <linux/kernel.h>
16 #include <linux/list.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/usb.h>
20 #include <linux/videodev2.h>
21 #include <linux/vmalloc.h>
23 #include <linux/wait.h>
24 #include <linux/atomic.h>
26 #include <media/v4l2-common.h>
27 #include <media/v4l2-ctrls.h>
28 #include <media/v4l2-event.h>
29 #include <media/v4l2-ioctl.h>
33 /* ------------------------------------------------------------------------
36 static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain,
37 struct uvc_xu_control_mapping *xmap)
39 struct uvc_control_mapping *map;
43 map = kzalloc(sizeof(*map), GFP_KERNEL);
48 memcpy(map->name, xmap->name, sizeof(map->name));
49 memcpy(map->entity, xmap->entity, sizeof(map->entity));
50 map->selector = xmap->selector;
51 map->size = xmap->size;
52 map->offset = xmap->offset;
53 map->v4l2_type = xmap->v4l2_type;
54 map->data_type = xmap->data_type;
56 switch (xmap->v4l2_type) {
57 case V4L2_CTRL_TYPE_INTEGER:
58 case V4L2_CTRL_TYPE_BOOLEAN:
59 case V4L2_CTRL_TYPE_BUTTON:
62 case V4L2_CTRL_TYPE_MENU:
63 /* Prevent excessive memory consumption, as well as integer
66 if (xmap->menu_count == 0 ||
67 xmap->menu_count > UVC_MAX_CONTROL_MENU_ENTRIES) {
72 size = xmap->menu_count * sizeof(*map->menu_info);
73 map->menu_info = memdup_user(xmap->menu_info, size);
74 if (IS_ERR(map->menu_info)) {
75 ret = PTR_ERR(map->menu_info);
79 map->menu_count = xmap->menu_count;
83 uvc_trace(UVC_TRACE_CONTROL, "Unsupported V4L2 control type "
84 "%u.\n", xmap->v4l2_type);
89 ret = uvc_ctrl_add_mapping(chain, map);
91 kfree(map->menu_info);
98 /* ------------------------------------------------------------------------
103 * Find the frame interval closest to the requested frame interval for the
104 * given frame format and size. This should be done by the device as part of
105 * the Video Probe and Commit negotiation, but some hardware don't implement
108 static u32 uvc_try_frame_interval(struct uvc_frame *frame, u32 interval)
112 if (frame->bFrameIntervalType) {
115 for (i = 0; i < frame->bFrameIntervalType; ++i) {
116 dist = interval > frame->dwFrameInterval[i]
117 ? interval - frame->dwFrameInterval[i]
118 : frame->dwFrameInterval[i] - interval;
126 interval = frame->dwFrameInterval[i-1];
128 const u32 min = frame->dwFrameInterval[0];
129 const u32 max = frame->dwFrameInterval[1];
130 const u32 step = frame->dwFrameInterval[2];
132 interval = min + (interval - min + step/2) / step * step;
140 static u32 uvc_v4l2_get_bytesperline(const struct uvc_format *format,
141 const struct uvc_frame *frame)
143 switch (format->fcc) {
144 case V4L2_PIX_FMT_NV12:
145 case V4L2_PIX_FMT_YVU420:
146 case V4L2_PIX_FMT_YUV420:
147 case V4L2_PIX_FMT_M420:
148 return frame->wWidth;
151 return format->bpp * frame->wWidth / 8;
155 static int uvc_v4l2_try_format(struct uvc_streaming *stream,
156 struct v4l2_format *fmt, struct uvc_streaming_control *probe,
157 struct uvc_format **uvc_format, struct uvc_frame **uvc_frame)
159 struct uvc_format *format = NULL;
160 struct uvc_frame *frame = NULL;
162 unsigned int d, maxd;
168 if (fmt->type != stream->type)
171 fcc = (u8 *)&fmt->fmt.pix.pixelformat;
172 uvc_trace(UVC_TRACE_FORMAT, "Trying format 0x%08x (%c%c%c%c): %ux%u.\n",
173 fmt->fmt.pix.pixelformat,
174 fcc[0], fcc[1], fcc[2], fcc[3],
175 fmt->fmt.pix.width, fmt->fmt.pix.height);
177 /* Check if the hardware supports the requested format, use the default
180 for (i = 0; i < stream->nformats; ++i) {
181 format = &stream->format[i];
182 if (format->fcc == fmt->fmt.pix.pixelformat)
186 if (i == stream->nformats) {
187 format = stream->def_format;
188 fmt->fmt.pix.pixelformat = format->fcc;
191 /* Find the closest image size. The distance between image sizes is
192 * the size in pixels of the non-overlapping regions between the
193 * requested size and the frame-specified size.
195 rw = fmt->fmt.pix.width;
196 rh = fmt->fmt.pix.height;
197 maxd = (unsigned int)-1;
199 for (i = 0; i < format->nframes; ++i) {
200 u16 w = format->frame[i].wWidth;
201 u16 h = format->frame[i].wHeight;
203 d = min(w, rw) * min(h, rh);
204 d = w*h + rw*rh - 2*d;
207 frame = &format->frame[i];
215 uvc_trace(UVC_TRACE_FORMAT, "Unsupported size %ux%u.\n",
216 fmt->fmt.pix.width, fmt->fmt.pix.height);
220 /* Use the default frame interval. */
221 interval = frame->dwDefaultFrameInterval;
222 uvc_trace(UVC_TRACE_FORMAT, "Using default frame interval %u.%u us "
223 "(%u.%u fps).\n", interval/10, interval%10, 10000000/interval,
224 (100000000/interval)%10);
226 /* Set the format index, frame index and frame interval. */
227 memset(probe, 0, sizeof(*probe));
228 probe->bmHint = 1; /* dwFrameInterval */
229 probe->bFormatIndex = format->index;
230 probe->bFrameIndex = frame->bFrameIndex;
231 probe->dwFrameInterval = uvc_try_frame_interval(frame, interval);
232 /* Some webcams stall the probe control set request when the
233 * dwMaxVideoFrameSize field is set to zero. The UVC specification
234 * clearly states that the field is read-only from the host, so this
235 * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by
236 * the webcam to work around the problem.
238 * The workaround could probably be enabled for all webcams, so the
239 * quirk can be removed if needed. It's currently useful to detect
240 * webcam bugs and fix them before they hit the market (providing
241 * developers test their webcams with the Linux driver as well as with
242 * the Windows driver).
244 mutex_lock(&stream->mutex);
245 if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS)
246 probe->dwMaxVideoFrameSize =
247 stream->ctrl.dwMaxVideoFrameSize;
249 /* Probe the device. */
250 ret = uvc_probe_video(stream, probe);
251 mutex_unlock(&stream->mutex);
255 /* After the probe, update fmt with the values returned from
256 * negotiation with the device. Some devices return invalid bFormatIndex
257 * and bFrameIndex values, in which case we can only assume they have
258 * accepted the requested format as-is.
260 for (i = 0; i < stream->nformats; ++i) {
261 if (probe->bFormatIndex == stream->format[i].index) {
262 format = &stream->format[i];
267 if (i == stream->nformats)
268 uvc_trace(UVC_TRACE_FORMAT,
269 "Unknown bFormatIndex %u, using default\n",
270 probe->bFormatIndex);
272 for (i = 0; i < format->nframes; ++i) {
273 if (probe->bFrameIndex == format->frame[i].bFrameIndex) {
274 frame = &format->frame[i];
279 if (i == format->nframes)
280 uvc_trace(UVC_TRACE_FORMAT,
281 "Unknown bFrameIndex %u, using default\n",
284 fmt->fmt.pix.width = frame->wWidth;
285 fmt->fmt.pix.height = frame->wHeight;
286 fmt->fmt.pix.field = V4L2_FIELD_NONE;
287 fmt->fmt.pix.bytesperline = uvc_v4l2_get_bytesperline(format, frame);
288 fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize;
289 fmt->fmt.pix.pixelformat = format->fcc;
290 fmt->fmt.pix.colorspace = format->colorspace;
291 fmt->fmt.pix.priv = 0;
293 if (uvc_format != NULL)
294 *uvc_format = format;
295 if (uvc_frame != NULL)
302 static int uvc_v4l2_get_format(struct uvc_streaming *stream,
303 struct v4l2_format *fmt)
305 struct uvc_format *format;
306 struct uvc_frame *frame;
309 if (fmt->type != stream->type)
312 mutex_lock(&stream->mutex);
313 format = stream->cur_format;
314 frame = stream->cur_frame;
316 if (format == NULL || frame == NULL) {
321 fmt->fmt.pix.pixelformat = format->fcc;
322 fmt->fmt.pix.width = frame->wWidth;
323 fmt->fmt.pix.height = frame->wHeight;
324 fmt->fmt.pix.field = V4L2_FIELD_NONE;
325 fmt->fmt.pix.bytesperline = uvc_v4l2_get_bytesperline(format, frame);
326 fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize;
327 fmt->fmt.pix.colorspace = format->colorspace;
328 fmt->fmt.pix.priv = 0;
331 mutex_unlock(&stream->mutex);
335 static int uvc_v4l2_set_format(struct uvc_streaming *stream,
336 struct v4l2_format *fmt)
338 struct uvc_streaming_control probe;
339 struct uvc_format *format;
340 struct uvc_frame *frame;
343 if (fmt->type != stream->type)
346 ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame);
350 mutex_lock(&stream->mutex);
352 if (uvc_queue_allocated(&stream->queue)) {
357 stream->ctrl = probe;
358 stream->cur_format = format;
359 stream->cur_frame = frame;
362 mutex_unlock(&stream->mutex);
366 static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream,
367 struct v4l2_streamparm *parm)
369 u32 numerator, denominator;
371 if (parm->type != stream->type)
374 mutex_lock(&stream->mutex);
375 numerator = stream->ctrl.dwFrameInterval;
376 mutex_unlock(&stream->mutex);
378 denominator = 10000000;
379 uvc_simplify_fraction(&numerator, &denominator, 8, 333);
381 memset(parm, 0, sizeof(*parm));
382 parm->type = stream->type;
384 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
385 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
386 parm->parm.capture.capturemode = 0;
387 parm->parm.capture.timeperframe.numerator = numerator;
388 parm->parm.capture.timeperframe.denominator = denominator;
389 parm->parm.capture.extendedmode = 0;
390 parm->parm.capture.readbuffers = 0;
392 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
393 parm->parm.output.outputmode = 0;
394 parm->parm.output.timeperframe.numerator = numerator;
395 parm->parm.output.timeperframe.denominator = denominator;
401 static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
402 struct v4l2_streamparm *parm)
404 struct uvc_streaming_control probe;
405 struct v4l2_fract timeperframe;
406 struct uvc_format *format;
407 struct uvc_frame *frame;
412 if (parm->type != stream->type)
415 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
416 timeperframe = parm->parm.capture.timeperframe;
418 timeperframe = parm->parm.output.timeperframe;
420 interval = uvc_fraction_to_interval(timeperframe.numerator,
421 timeperframe.denominator);
422 uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
423 timeperframe.numerator, timeperframe.denominator, interval);
425 mutex_lock(&stream->mutex);
427 if (uvc_queue_streaming(&stream->queue)) {
428 mutex_unlock(&stream->mutex);
432 format = stream->cur_format;
433 frame = stream->cur_frame;
434 probe = stream->ctrl;
435 probe.dwFrameInterval = uvc_try_frame_interval(frame, interval);
436 maxd = abs((s32)probe.dwFrameInterval - interval);
438 /* Try frames with matching size to find the best frame interval. */
439 for (i = 0; i < format->nframes && maxd != 0; i++) {
442 if (&format->frame[i] == stream->cur_frame)
445 if (format->frame[i].wWidth != stream->cur_frame->wWidth ||
446 format->frame[i].wHeight != stream->cur_frame->wHeight)
449 ival = uvc_try_frame_interval(&format->frame[i], interval);
450 d = abs((s32)ival - interval);
454 frame = &format->frame[i];
455 probe.bFrameIndex = frame->bFrameIndex;
456 probe.dwFrameInterval = ival;
460 /* Probe the device with the new settings. */
461 ret = uvc_probe_video(stream, &probe);
463 mutex_unlock(&stream->mutex);
467 stream->ctrl = probe;
468 stream->cur_frame = frame;
469 mutex_unlock(&stream->mutex);
471 /* Return the actual frame period. */
472 timeperframe.numerator = probe.dwFrameInterval;
473 timeperframe.denominator = 10000000;
474 uvc_simplify_fraction(&timeperframe.numerator,
475 &timeperframe.denominator, 8, 333);
477 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
478 parm->parm.capture.timeperframe = timeperframe;
479 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
481 parm->parm.output.timeperframe = timeperframe;
482 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
488 /* ------------------------------------------------------------------------
489 * Privilege management
493 * Privilege management is the multiple-open implementation basis. The current
494 * implementation is completely transparent for the end-user and doesn't
495 * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls.
496 * Those ioctls enable finer control on the device (by making possible for a
497 * user to request exclusive access to a device), but are not mature yet.
498 * Switching to the V4L2 priority mechanism might be considered in the future
499 * if this situation changes.
501 * Each open instance of a UVC device can either be in a privileged or
502 * unprivileged state. Only a single instance can be in a privileged state at
503 * a given time. Trying to perform an operation that requires privileges will
504 * automatically acquire the required privileges if possible, or return -EBUSY
505 * otherwise. Privileges are dismissed when closing the instance or when
506 * freeing the video buffers using VIDIOC_REQBUFS.
508 * Operations that require privileges are:
515 static int uvc_acquire_privileges(struct uvc_fh *handle)
517 /* Always succeed if the handle is already privileged. */
518 if (handle->state == UVC_HANDLE_ACTIVE)
521 /* Check if the device already has a privileged handle. */
522 if (atomic_inc_return(&handle->stream->active) != 1) {
523 atomic_dec(&handle->stream->active);
527 handle->state = UVC_HANDLE_ACTIVE;
531 static void uvc_dismiss_privileges(struct uvc_fh *handle)
533 if (handle->state == UVC_HANDLE_ACTIVE)
534 atomic_dec(&handle->stream->active);
536 handle->state = UVC_HANDLE_PASSIVE;
539 static int uvc_has_privileges(struct uvc_fh *handle)
541 return handle->state == UVC_HANDLE_ACTIVE;
544 /* ------------------------------------------------------------------------
545 * V4L2 file operations
548 static int uvc_v4l2_open(struct file *file)
550 struct uvc_streaming *stream;
551 struct uvc_fh *handle;
554 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
555 stream = video_drvdata(file);
557 ret = usb_autopm_get_interface(stream->dev->intf);
561 /* Create the device handle. */
562 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
563 if (handle == NULL) {
564 usb_autopm_put_interface(stream->dev->intf);
568 mutex_lock(&stream->dev->lock);
569 if (stream->dev->users == 0) {
570 ret = uvc_status_start(stream->dev, GFP_KERNEL);
572 mutex_unlock(&stream->dev->lock);
573 usb_autopm_put_interface(stream->dev->intf);
579 stream->dev->users++;
580 mutex_unlock(&stream->dev->lock);
582 v4l2_fh_init(&handle->vfh, &stream->vdev);
583 v4l2_fh_add(&handle->vfh);
584 handle->chain = stream->chain;
585 handle->stream = stream;
586 handle->state = UVC_HANDLE_PASSIVE;
587 file->private_data = handle;
592 static int uvc_v4l2_release(struct file *file)
594 struct uvc_fh *handle = file->private_data;
595 struct uvc_streaming *stream = handle->stream;
597 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");
599 /* Only free resources if this is a privileged handle. */
600 if (uvc_has_privileges(handle))
601 uvc_queue_release(&stream->queue);
603 /* Release the file handle. */
604 uvc_dismiss_privileges(handle);
605 v4l2_fh_del(&handle->vfh);
606 v4l2_fh_exit(&handle->vfh);
608 file->private_data = NULL;
610 mutex_lock(&stream->dev->lock);
611 if (--stream->dev->users == 0)
612 uvc_status_stop(stream->dev);
613 mutex_unlock(&stream->dev->lock);
615 usb_autopm_put_interface(stream->dev->intf);
619 static int uvc_ioctl_querycap(struct file *file, void *fh,
620 struct v4l2_capability *cap)
622 struct video_device *vdev = video_devdata(file);
623 struct uvc_fh *handle = file->private_data;
624 struct uvc_video_chain *chain = handle->chain;
625 struct uvc_streaming *stream = handle->stream;
627 strlcpy(cap->driver, "uvcvideo", sizeof(cap->driver));
628 strlcpy(cap->card, vdev->name, sizeof(cap->card));
629 usb_make_path(stream->dev->udev, cap->bus_info, sizeof(cap->bus_info));
630 cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
636 static int uvc_ioctl_enum_fmt(struct uvc_streaming *stream,
637 struct v4l2_fmtdesc *fmt)
639 struct uvc_format *format;
640 enum v4l2_buf_type type = fmt->type;
641 u32 index = fmt->index;
643 if (fmt->type != stream->type || fmt->index >= stream->nformats)
646 memset(fmt, 0, sizeof(*fmt));
650 format = &stream->format[fmt->index];
652 if (format->flags & UVC_FMT_FLAG_COMPRESSED)
653 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
654 strlcpy(fmt->description, format->name, sizeof(fmt->description));
655 fmt->description[sizeof(fmt->description) - 1] = 0;
656 fmt->pixelformat = format->fcc;
660 static int uvc_ioctl_enum_fmt_vid_cap(struct file *file, void *fh,
661 struct v4l2_fmtdesc *fmt)
663 struct uvc_fh *handle = fh;
664 struct uvc_streaming *stream = handle->stream;
666 return uvc_ioctl_enum_fmt(stream, fmt);
669 static int uvc_ioctl_enum_fmt_vid_out(struct file *file, void *fh,
670 struct v4l2_fmtdesc *fmt)
672 struct uvc_fh *handle = fh;
673 struct uvc_streaming *stream = handle->stream;
675 return uvc_ioctl_enum_fmt(stream, fmt);
678 static int uvc_ioctl_g_fmt_vid_cap(struct file *file, void *fh,
679 struct v4l2_format *fmt)
681 struct uvc_fh *handle = fh;
682 struct uvc_streaming *stream = handle->stream;
684 return uvc_v4l2_get_format(stream, fmt);
687 static int uvc_ioctl_g_fmt_vid_out(struct file *file, void *fh,
688 struct v4l2_format *fmt)
690 struct uvc_fh *handle = fh;
691 struct uvc_streaming *stream = handle->stream;
693 return uvc_v4l2_get_format(stream, fmt);
696 static int uvc_ioctl_s_fmt_vid_cap(struct file *file, void *fh,
697 struct v4l2_format *fmt)
699 struct uvc_fh *handle = fh;
700 struct uvc_streaming *stream = handle->stream;
703 ret = uvc_acquire_privileges(handle);
707 return uvc_v4l2_set_format(stream, fmt);
710 static int uvc_ioctl_s_fmt_vid_out(struct file *file, void *fh,
711 struct v4l2_format *fmt)
713 struct uvc_fh *handle = fh;
714 struct uvc_streaming *stream = handle->stream;
717 ret = uvc_acquire_privileges(handle);
721 return uvc_v4l2_set_format(stream, fmt);
724 static int uvc_ioctl_try_fmt_vid_cap(struct file *file, void *fh,
725 struct v4l2_format *fmt)
727 struct uvc_fh *handle = fh;
728 struct uvc_streaming *stream = handle->stream;
729 struct uvc_streaming_control probe;
731 return uvc_v4l2_try_format(stream, fmt, &probe, NULL, NULL);
734 static int uvc_ioctl_try_fmt_vid_out(struct file *file, void *fh,
735 struct v4l2_format *fmt)
737 struct uvc_fh *handle = fh;
738 struct uvc_streaming *stream = handle->stream;
739 struct uvc_streaming_control probe;
741 return uvc_v4l2_try_format(stream, fmt, &probe, NULL, NULL);
744 static int uvc_ioctl_reqbufs(struct file *file, void *fh,
745 struct v4l2_requestbuffers *rb)
747 struct uvc_fh *handle = fh;
748 struct uvc_streaming *stream = handle->stream;
751 ret = uvc_acquire_privileges(handle);
755 mutex_lock(&stream->mutex);
756 ret = uvc_request_buffers(&stream->queue, rb);
757 mutex_unlock(&stream->mutex);
762 uvc_dismiss_privileges(handle);
767 static int uvc_ioctl_querybuf(struct file *file, void *fh,
768 struct v4l2_buffer *buf)
770 struct uvc_fh *handle = fh;
771 struct uvc_streaming *stream = handle->stream;
773 if (!uvc_has_privileges(handle))
776 return uvc_query_buffer(&stream->queue, buf);
779 static int uvc_ioctl_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
781 struct uvc_fh *handle = fh;
782 struct uvc_streaming *stream = handle->stream;
784 if (!uvc_has_privileges(handle))
787 return uvc_queue_buffer(&stream->queue, buf);
790 static int uvc_ioctl_expbuf(struct file *file, void *fh,
791 struct v4l2_exportbuffer *exp)
793 struct uvc_fh *handle = fh;
794 struct uvc_streaming *stream = handle->stream;
796 if (!uvc_has_privileges(handle))
799 return uvc_export_buffer(&stream->queue, exp);
802 static int uvc_ioctl_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
804 struct uvc_fh *handle = fh;
805 struct uvc_streaming *stream = handle->stream;
807 if (!uvc_has_privileges(handle))
810 return uvc_dequeue_buffer(&stream->queue, buf,
811 file->f_flags & O_NONBLOCK);
814 static int uvc_ioctl_create_bufs(struct file *file, void *fh,
815 struct v4l2_create_buffers *cb)
817 struct uvc_fh *handle = fh;
818 struct uvc_streaming *stream = handle->stream;
821 ret = uvc_acquire_privileges(handle);
825 return uvc_create_buffers(&stream->queue, cb);
828 static int uvc_ioctl_streamon(struct file *file, void *fh,
829 enum v4l2_buf_type type)
831 struct uvc_fh *handle = fh;
832 struct uvc_streaming *stream = handle->stream;
835 if (!uvc_has_privileges(handle))
838 mutex_lock(&stream->mutex);
839 ret = uvc_queue_streamon(&stream->queue, type);
840 mutex_unlock(&stream->mutex);
845 static int uvc_ioctl_streamoff(struct file *file, void *fh,
846 enum v4l2_buf_type type)
848 struct uvc_fh *handle = fh;
849 struct uvc_streaming *stream = handle->stream;
851 if (!uvc_has_privileges(handle))
854 mutex_lock(&stream->mutex);
855 uvc_queue_streamoff(&stream->queue, type);
856 mutex_unlock(&stream->mutex);
861 static int uvc_ioctl_enum_input(struct file *file, void *fh,
862 struct v4l2_input *input)
864 struct uvc_fh *handle = fh;
865 struct uvc_video_chain *chain = handle->chain;
866 const struct uvc_entity *selector = chain->selector;
867 struct uvc_entity *iterm = NULL;
868 struct uvc_entity *it;
869 u32 index = input->index;
871 if (selector == NULL ||
872 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
875 list_for_each_entry(it, &chain->entities, chain) {
876 if (UVC_ENTITY_IS_ITERM(it)) {
881 } else if (index < selector->bNrInPins) {
882 list_for_each_entry(it, &chain->entities, chain) {
883 if (!UVC_ENTITY_IS_ITERM(it))
885 if (it->id == selector->baSourceID[index]) {
895 memset(input, 0, sizeof(*input));
896 input->index = index;
897 strlcpy(input->name, iterm->name, sizeof(input->name));
898 if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
899 input->type = V4L2_INPUT_TYPE_CAMERA;
904 static int uvc_ioctl_g_input(struct file *file, void *fh, unsigned int *input)
906 struct uvc_fh *handle = fh;
907 struct uvc_video_chain *chain = handle->chain;
911 if (chain->selector == NULL ||
912 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
917 buf = kmalloc(1, GFP_KERNEL);
921 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, chain->selector->id,
922 chain->dev->intfnum, UVC_SU_INPUT_SELECT_CONTROL,
932 static int uvc_ioctl_s_input(struct file *file, void *fh, unsigned int input)
934 struct uvc_fh *handle = fh;
935 struct uvc_video_chain *chain = handle->chain;
939 ret = uvc_acquire_privileges(handle);
943 if (chain->selector == NULL ||
944 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
950 if (input >= chain->selector->bNrInPins)
953 buf = kmalloc(1, GFP_KERNEL);
958 ret = uvc_query_ctrl(chain->dev, UVC_SET_CUR, chain->selector->id,
959 chain->dev->intfnum, UVC_SU_INPUT_SELECT_CONTROL,
966 static int uvc_ioctl_queryctrl(struct file *file, void *fh,
967 struct v4l2_queryctrl *qc)
969 struct uvc_fh *handle = fh;
970 struct uvc_video_chain *chain = handle->chain;
972 return uvc_query_v4l2_ctrl(chain, qc);
975 static int uvc_ioctl_query_ext_ctrl(struct file *file, void *fh,
976 struct v4l2_query_ext_ctrl *qec)
978 struct uvc_fh *handle = fh;
979 struct uvc_video_chain *chain = handle->chain;
980 struct v4l2_queryctrl qc = { qec->id };
983 ret = uvc_query_v4l2_ctrl(chain, &qc);
989 strlcpy(qec->name, qc.name, sizeof(qec->name));
990 qec->minimum = qc.minimum;
991 qec->maximum = qc.maximum;
993 qec->default_value = qc.default_value;
994 qec->flags = qc.flags;
998 memset(qec->dims, 0, sizeof(qec->dims));
999 memset(qec->reserved, 0, sizeof(qec->reserved));
1004 static int uvc_ioctl_g_ctrl(struct file *file, void *fh,
1005 struct v4l2_control *ctrl)
1007 struct uvc_fh *handle = fh;
1008 struct uvc_video_chain *chain = handle->chain;
1009 struct v4l2_ext_control xctrl;
1012 memset(&xctrl, 0, sizeof(xctrl));
1013 xctrl.id = ctrl->id;
1015 ret = uvc_ctrl_begin(chain);
1019 ret = uvc_ctrl_get(chain, &xctrl);
1020 uvc_ctrl_rollback(handle);
1024 ctrl->value = xctrl.value;
1028 static int uvc_ioctl_s_ctrl(struct file *file, void *fh,
1029 struct v4l2_control *ctrl)
1031 struct uvc_fh *handle = fh;
1032 struct uvc_video_chain *chain = handle->chain;
1033 struct v4l2_ext_control xctrl;
1036 memset(&xctrl, 0, sizeof(xctrl));
1037 xctrl.id = ctrl->id;
1038 xctrl.value = ctrl->value;
1040 ret = uvc_ctrl_begin(chain);
1044 ret = uvc_ctrl_set(handle, &xctrl);
1046 uvc_ctrl_rollback(handle);
1050 ret = uvc_ctrl_commit(handle, &xctrl, 1);
1054 ctrl->value = xctrl.value;
1058 static int uvc_ioctl_g_ext_ctrls(struct file *file, void *fh,
1059 struct v4l2_ext_controls *ctrls)
1061 struct uvc_fh *handle = fh;
1062 struct uvc_video_chain *chain = handle->chain;
1063 struct v4l2_ext_control *ctrl = ctrls->controls;
1067 if (ctrls->which == V4L2_CTRL_WHICH_DEF_VAL) {
1068 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
1069 struct v4l2_queryctrl qc = { .id = ctrl->id };
1071 ret = uvc_query_v4l2_ctrl(chain, &qc);
1073 ctrls->error_idx = i;
1077 ctrl->value = qc.default_value;
1083 ret = uvc_ctrl_begin(chain);
1087 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
1088 ret = uvc_ctrl_get(chain, ctrl);
1090 uvc_ctrl_rollback(handle);
1091 ctrls->error_idx = i;
1096 ctrls->error_idx = 0;
1098 return uvc_ctrl_rollback(handle);
1101 static int uvc_ioctl_s_try_ext_ctrls(struct uvc_fh *handle,
1102 struct v4l2_ext_controls *ctrls,
1105 struct v4l2_ext_control *ctrl = ctrls->controls;
1106 struct uvc_video_chain *chain = handle->chain;
1110 /* Default value cannot be changed */
1111 if (ctrls->which == V4L2_CTRL_WHICH_DEF_VAL)
1114 ret = uvc_ctrl_begin(chain);
1118 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
1119 ret = uvc_ctrl_set(handle, ctrl);
1121 uvc_ctrl_rollback(handle);
1122 ctrls->error_idx = commit ? ctrls->count : i;
1127 ctrls->error_idx = 0;
1130 return uvc_ctrl_commit(handle, ctrls->controls, ctrls->count);
1132 return uvc_ctrl_rollback(handle);
1135 static int uvc_ioctl_s_ext_ctrls(struct file *file, void *fh,
1136 struct v4l2_ext_controls *ctrls)
1138 struct uvc_fh *handle = fh;
1140 return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, true);
1143 static int uvc_ioctl_try_ext_ctrls(struct file *file, void *fh,
1144 struct v4l2_ext_controls *ctrls)
1146 struct uvc_fh *handle = fh;
1148 return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, false);
1151 static int uvc_ioctl_querymenu(struct file *file, void *fh,
1152 struct v4l2_querymenu *qm)
1154 struct uvc_fh *handle = fh;
1155 struct uvc_video_chain *chain = handle->chain;
1157 return uvc_query_v4l2_menu(chain, qm);
1160 static int uvc_ioctl_g_selection(struct file *file, void *fh,
1161 struct v4l2_selection *sel)
1163 struct uvc_fh *handle = fh;
1164 struct uvc_streaming *stream = handle->stream;
1166 if (sel->type != stream->type)
1169 switch (sel->target) {
1170 case V4L2_SEL_TGT_CROP_DEFAULT:
1171 case V4L2_SEL_TGT_CROP_BOUNDS:
1172 if (stream->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1175 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1176 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1177 if (stream->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1186 mutex_lock(&stream->mutex);
1187 sel->r.width = stream->cur_frame->wWidth;
1188 sel->r.height = stream->cur_frame->wHeight;
1189 mutex_unlock(&stream->mutex);
1194 static int uvc_ioctl_g_parm(struct file *file, void *fh,
1195 struct v4l2_streamparm *parm)
1197 struct uvc_fh *handle = fh;
1198 struct uvc_streaming *stream = handle->stream;
1200 return uvc_v4l2_get_streamparm(stream, parm);
1203 static int uvc_ioctl_s_parm(struct file *file, void *fh,
1204 struct v4l2_streamparm *parm)
1206 struct uvc_fh *handle = fh;
1207 struct uvc_streaming *stream = handle->stream;
1210 ret = uvc_acquire_privileges(handle);
1214 return uvc_v4l2_set_streamparm(stream, parm);
1217 static int uvc_ioctl_enum_framesizes(struct file *file, void *fh,
1218 struct v4l2_frmsizeenum *fsize)
1220 struct uvc_fh *handle = fh;
1221 struct uvc_streaming *stream = handle->stream;
1222 struct uvc_format *format = NULL;
1223 struct uvc_frame *frame = NULL;
1227 /* Look for the given pixel format */
1228 for (i = 0; i < stream->nformats; i++) {
1229 if (stream->format[i].fcc == fsize->pixel_format) {
1230 format = &stream->format[i];
1237 /* Skip duplicate frame sizes */
1238 for (i = 0, index = 0; i < format->nframes; i++) {
1239 if (frame && frame->wWidth == format->frame[i].wWidth &&
1240 frame->wHeight == format->frame[i].wHeight)
1242 frame = &format->frame[i];
1243 if (index == fsize->index)
1248 if (i == format->nframes)
1251 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1252 fsize->discrete.width = frame->wWidth;
1253 fsize->discrete.height = frame->wHeight;
1257 static int uvc_ioctl_enum_frameintervals(struct file *file, void *fh,
1258 struct v4l2_frmivalenum *fival)
1260 struct uvc_fh *handle = fh;
1261 struct uvc_streaming *stream = handle->stream;
1262 struct uvc_format *format = NULL;
1263 struct uvc_frame *frame = NULL;
1264 unsigned int nintervals;
1268 /* Look for the given pixel format and frame size */
1269 for (i = 0; i < stream->nformats; i++) {
1270 if (stream->format[i].fcc == fival->pixel_format) {
1271 format = &stream->format[i];
1278 index = fival->index;
1279 for (i = 0; i < format->nframes; i++) {
1280 if (format->frame[i].wWidth == fival->width &&
1281 format->frame[i].wHeight == fival->height) {
1282 frame = &format->frame[i];
1283 nintervals = frame->bFrameIntervalType ?: 1;
1284 if (index < nintervals)
1286 index -= nintervals;
1289 if (i == format->nframes)
1292 if (frame->bFrameIntervalType) {
1293 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1294 fival->discrete.numerator =
1295 frame->dwFrameInterval[index];
1296 fival->discrete.denominator = 10000000;
1297 uvc_simplify_fraction(&fival->discrete.numerator,
1298 &fival->discrete.denominator, 8, 333);
1300 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
1301 fival->stepwise.min.numerator = frame->dwFrameInterval[0];
1302 fival->stepwise.min.denominator = 10000000;
1303 fival->stepwise.max.numerator = frame->dwFrameInterval[1];
1304 fival->stepwise.max.denominator = 10000000;
1305 fival->stepwise.step.numerator = frame->dwFrameInterval[2];
1306 fival->stepwise.step.denominator = 10000000;
1307 uvc_simplify_fraction(&fival->stepwise.min.numerator,
1308 &fival->stepwise.min.denominator, 8, 333);
1309 uvc_simplify_fraction(&fival->stepwise.max.numerator,
1310 &fival->stepwise.max.denominator, 8, 333);
1311 uvc_simplify_fraction(&fival->stepwise.step.numerator,
1312 &fival->stepwise.step.denominator, 8, 333);
1318 static int uvc_ioctl_subscribe_event(struct v4l2_fh *fh,
1319 const struct v4l2_event_subscription *sub)
1321 switch (sub->type) {
1322 case V4L2_EVENT_CTRL:
1323 return v4l2_event_subscribe(fh, sub, 0, &uvc_ctrl_sub_ev_ops);
1329 static long uvc_ioctl_default(struct file *file, void *fh, bool valid_prio,
1330 unsigned int cmd, void *arg)
1332 struct uvc_fh *handle = fh;
1333 struct uvc_video_chain *chain = handle->chain;
1336 /* Dynamic controls. */
1337 case UVCIOC_CTRL_MAP:
1338 return uvc_ioctl_ctrl_map(chain, arg);
1340 case UVCIOC_CTRL_QUERY:
1341 return uvc_xu_ctrl_query(chain, arg);
1348 #ifdef CONFIG_COMPAT
1349 struct uvc_xu_control_mapping32 {
1360 compat_caddr_t menu_info;
1366 static int uvc_v4l2_get_xu_mapping(struct uvc_xu_control_mapping *kp,
1367 const struct uvc_xu_control_mapping32 __user *up)
1369 struct uvc_xu_control_mapping32 *p = (void *)kp;
1370 compat_caddr_t info;
1373 if (copy_from_user(p, up, sizeof(*p)))
1376 count = p->menu_count;
1377 info = p->menu_info;
1379 memset(kp->reserved, 0, sizeof(kp->reserved));
1380 kp->menu_info = count ? compat_ptr(info) : NULL;
1381 kp->menu_count = count;
1385 static int uvc_v4l2_put_xu_mapping(const struct uvc_xu_control_mapping *kp,
1386 struct uvc_xu_control_mapping32 __user *up)
1388 if (copy_to_user(up, kp, offsetof(typeof(*up), menu_info)) ||
1389 put_user(kp->menu_count, &up->menu_count))
1392 if (clear_user(up->reserved, sizeof(up->reserved)))
1398 struct uvc_xu_control_query32 {
1403 compat_caddr_t data;
1406 static int uvc_v4l2_get_xu_query(struct uvc_xu_control_query *kp,
1407 const struct uvc_xu_control_query32 __user *up)
1409 struct uvc_xu_control_query32 v;
1411 if (copy_from_user(&v, up, sizeof(v)))
1414 *kp = (struct uvc_xu_control_query){
1416 .selector = v.selector,
1419 .data = v.size ? compat_ptr(v.data) : NULL
1424 static int uvc_v4l2_put_xu_query(const struct uvc_xu_control_query *kp,
1425 struct uvc_xu_control_query32 __user *up)
1427 if (copy_to_user(up, kp, offsetof(typeof(*up), data)))
1432 #define UVCIOC_CTRL_MAP32 _IOWR('u', 0x20, struct uvc_xu_control_mapping32)
1433 #define UVCIOC_CTRL_QUERY32 _IOWR('u', 0x21, struct uvc_xu_control_query32)
1435 static long uvc_v4l2_compat_ioctl32(struct file *file,
1436 unsigned int cmd, unsigned long arg)
1438 struct uvc_fh *handle = file->private_data;
1440 struct uvc_xu_control_mapping xmap;
1441 struct uvc_xu_control_query xqry;
1443 void __user *up = compat_ptr(arg);
1447 case UVCIOC_CTRL_MAP32:
1448 ret = uvc_v4l2_get_xu_mapping(&karg.xmap, up);
1451 ret = uvc_ioctl_ctrl_map(handle->chain, &karg.xmap);
1454 ret = uvc_v4l2_put_xu_mapping(&karg.xmap, up);
1460 case UVCIOC_CTRL_QUERY32:
1461 ret = uvc_v4l2_get_xu_query(&karg.xqry, up);
1464 ret = uvc_xu_ctrl_query(handle->chain, &karg.xqry);
1467 ret = uvc_v4l2_put_xu_query(&karg.xqry, up);
1473 return -ENOIOCTLCMD;
1480 static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1481 size_t count, loff_t *ppos)
1483 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
1487 static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1489 struct uvc_fh *handle = file->private_data;
1490 struct uvc_streaming *stream = handle->stream;
1492 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
1494 return uvc_queue_mmap(&stream->queue, vma);
1497 static __poll_t uvc_v4l2_poll(struct file *file, poll_table *wait)
1499 struct uvc_fh *handle = file->private_data;
1500 struct uvc_streaming *stream = handle->stream;
1502 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
1504 return uvc_queue_poll(&stream->queue, file, wait);
1508 static unsigned long uvc_v4l2_get_unmapped_area(struct file *file,
1509 unsigned long addr, unsigned long len, unsigned long pgoff,
1510 unsigned long flags)
1512 struct uvc_fh *handle = file->private_data;
1513 struct uvc_streaming *stream = handle->stream;
1515 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_get_unmapped_area\n");
1517 return uvc_queue_get_unmapped_area(&stream->queue, pgoff);
1521 const struct v4l2_ioctl_ops uvc_ioctl_ops = {
1522 .vidioc_querycap = uvc_ioctl_querycap,
1523 .vidioc_enum_fmt_vid_cap = uvc_ioctl_enum_fmt_vid_cap,
1524 .vidioc_enum_fmt_vid_out = uvc_ioctl_enum_fmt_vid_out,
1525 .vidioc_g_fmt_vid_cap = uvc_ioctl_g_fmt_vid_cap,
1526 .vidioc_g_fmt_vid_out = uvc_ioctl_g_fmt_vid_out,
1527 .vidioc_s_fmt_vid_cap = uvc_ioctl_s_fmt_vid_cap,
1528 .vidioc_s_fmt_vid_out = uvc_ioctl_s_fmt_vid_out,
1529 .vidioc_try_fmt_vid_cap = uvc_ioctl_try_fmt_vid_cap,
1530 .vidioc_try_fmt_vid_out = uvc_ioctl_try_fmt_vid_out,
1531 .vidioc_reqbufs = uvc_ioctl_reqbufs,
1532 .vidioc_querybuf = uvc_ioctl_querybuf,
1533 .vidioc_qbuf = uvc_ioctl_qbuf,
1534 .vidioc_expbuf = uvc_ioctl_expbuf,
1535 .vidioc_dqbuf = uvc_ioctl_dqbuf,
1536 .vidioc_create_bufs = uvc_ioctl_create_bufs,
1537 .vidioc_streamon = uvc_ioctl_streamon,
1538 .vidioc_streamoff = uvc_ioctl_streamoff,
1539 .vidioc_enum_input = uvc_ioctl_enum_input,
1540 .vidioc_g_input = uvc_ioctl_g_input,
1541 .vidioc_s_input = uvc_ioctl_s_input,
1542 .vidioc_queryctrl = uvc_ioctl_queryctrl,
1543 .vidioc_query_ext_ctrl = uvc_ioctl_query_ext_ctrl,
1544 .vidioc_g_ctrl = uvc_ioctl_g_ctrl,
1545 .vidioc_s_ctrl = uvc_ioctl_s_ctrl,
1546 .vidioc_g_ext_ctrls = uvc_ioctl_g_ext_ctrls,
1547 .vidioc_s_ext_ctrls = uvc_ioctl_s_ext_ctrls,
1548 .vidioc_try_ext_ctrls = uvc_ioctl_try_ext_ctrls,
1549 .vidioc_querymenu = uvc_ioctl_querymenu,
1550 .vidioc_g_selection = uvc_ioctl_g_selection,
1551 .vidioc_g_parm = uvc_ioctl_g_parm,
1552 .vidioc_s_parm = uvc_ioctl_s_parm,
1553 .vidioc_enum_framesizes = uvc_ioctl_enum_framesizes,
1554 .vidioc_enum_frameintervals = uvc_ioctl_enum_frameintervals,
1555 .vidioc_subscribe_event = uvc_ioctl_subscribe_event,
1556 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1557 .vidioc_default = uvc_ioctl_default,
1560 const struct v4l2_file_operations uvc_fops = {
1561 .owner = THIS_MODULE,
1562 .open = uvc_v4l2_open,
1563 .release = uvc_v4l2_release,
1564 .unlocked_ioctl = video_ioctl2,
1565 #ifdef CONFIG_COMPAT
1566 .compat_ioctl32 = uvc_v4l2_compat_ioctl32,
1568 .read = uvc_v4l2_read,
1569 .mmap = uvc_v4l2_mmap,
1570 .poll = uvc_v4l2_poll,
1572 .get_unmapped_area = uvc_v4l2_get_unmapped_area,