initial commit with v3.6.7
[linux-3.6.7-moxart.git] / drivers / media / video / uvc / uvc_v4l2.c
blobf00db3060e0e36e005b306a53f393cc55071b101
1 /*
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/version.h>
17 #include <linux/list.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/usb.h>
21 #include <linux/videodev2.h>
22 #include <linux/vmalloc.h>
23 #include <linux/mm.h>
24 #include <linux/wait.h>
25 #include <linux/atomic.h>
27 #include <media/v4l2-common.h>
28 #include <media/v4l2-ctrls.h>
29 #include <media/v4l2-event.h>
30 #include <media/v4l2-ioctl.h>
32 #include "uvcvideo.h"
34 /* ------------------------------------------------------------------------
35 * UVC ioctls
37 static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain,
38 struct uvc_xu_control_mapping *xmap)
40 struct uvc_control_mapping *map;
41 unsigned int size;
42 int ret;
44 map = kzalloc(sizeof *map, GFP_KERNEL);
45 if (map == NULL)
46 return -ENOMEM;
48 map->id = xmap->id;
49 memcpy(map->name, xmap->name, sizeof map->name);
50 memcpy(map->entity, xmap->entity, sizeof map->entity);
51 map->selector = xmap->selector;
52 map->size = xmap->size;
53 map->offset = xmap->offset;
54 map->v4l2_type = xmap->v4l2_type;
55 map->data_type = xmap->data_type;
57 switch (xmap->v4l2_type) {
58 case V4L2_CTRL_TYPE_INTEGER:
59 case V4L2_CTRL_TYPE_BOOLEAN:
60 case V4L2_CTRL_TYPE_BUTTON:
61 break;
63 case V4L2_CTRL_TYPE_MENU:
64 /* Prevent excessive memory consumption, as well as integer
65 * overflows.
67 if (xmap->menu_count == 0 ||
68 xmap->menu_count > UVC_MAX_CONTROL_MENU_ENTRIES) {
69 ret = -EINVAL;
70 goto done;
73 size = xmap->menu_count * sizeof(*map->menu_info);
74 map->menu_info = kmalloc(size, GFP_KERNEL);
75 if (map->menu_info == NULL) {
76 ret = -ENOMEM;
77 goto done;
80 if (copy_from_user(map->menu_info, xmap->menu_info, size)) {
81 ret = -EFAULT;
82 goto done;
85 map->menu_count = xmap->menu_count;
86 break;
88 default:
89 uvc_trace(UVC_TRACE_CONTROL, "Unsupported V4L2 control type "
90 "%u.\n", xmap->v4l2_type);
91 ret = -ENOTTY;
92 goto done;
95 ret = uvc_ctrl_add_mapping(chain, map);
97 done:
98 kfree(map->menu_info);
99 kfree(map);
101 return ret;
104 /* ------------------------------------------------------------------------
105 * V4L2 interface
109 * Find the frame interval closest to the requested frame interval for the
110 * given frame format and size. This should be done by the device as part of
111 * the Video Probe and Commit negotiation, but some hardware don't implement
112 * that feature.
114 static __u32 uvc_try_frame_interval(struct uvc_frame *frame, __u32 interval)
116 unsigned int i;
118 if (frame->bFrameIntervalType) {
119 __u32 best = -1, dist;
121 for (i = 0; i < frame->bFrameIntervalType; ++i) {
122 dist = interval > frame->dwFrameInterval[i]
123 ? interval - frame->dwFrameInterval[i]
124 : frame->dwFrameInterval[i] - interval;
126 if (dist > best)
127 break;
129 best = dist;
132 interval = frame->dwFrameInterval[i-1];
133 } else {
134 const __u32 min = frame->dwFrameInterval[0];
135 const __u32 max = frame->dwFrameInterval[1];
136 const __u32 step = frame->dwFrameInterval[2];
138 interval = min + (interval - min + step/2) / step * step;
139 if (interval > max)
140 interval = max;
143 return interval;
146 static int uvc_v4l2_try_format(struct uvc_streaming *stream,
147 struct v4l2_format *fmt, struct uvc_streaming_control *probe,
148 struct uvc_format **uvc_format, struct uvc_frame **uvc_frame)
150 struct uvc_format *format = NULL;
151 struct uvc_frame *frame = NULL;
152 __u16 rw, rh;
153 unsigned int d, maxd;
154 unsigned int i;
155 __u32 interval;
156 int ret = 0;
157 __u8 *fcc;
159 if (fmt->type != stream->type)
160 return -EINVAL;
162 fcc = (__u8 *)&fmt->fmt.pix.pixelformat;
163 uvc_trace(UVC_TRACE_FORMAT, "Trying format 0x%08x (%c%c%c%c): %ux%u.\n",
164 fmt->fmt.pix.pixelformat,
165 fcc[0], fcc[1], fcc[2], fcc[3],
166 fmt->fmt.pix.width, fmt->fmt.pix.height);
168 /* Check if the hardware supports the requested format. */
169 for (i = 0; i < stream->nformats; ++i) {
170 format = &stream->format[i];
171 if (format->fcc == fmt->fmt.pix.pixelformat)
172 break;
175 if (format == NULL || format->fcc != fmt->fmt.pix.pixelformat) {
176 uvc_trace(UVC_TRACE_FORMAT, "Unsupported format 0x%08x.\n",
177 fmt->fmt.pix.pixelformat);
178 return -EINVAL;
181 /* Find the closest image size. The distance between image sizes is
182 * the size in pixels of the non-overlapping regions between the
183 * requested size and the frame-specified size.
185 rw = fmt->fmt.pix.width;
186 rh = fmt->fmt.pix.height;
187 maxd = (unsigned int)-1;
189 for (i = 0; i < format->nframes; ++i) {
190 __u16 w = format->frame[i].wWidth;
191 __u16 h = format->frame[i].wHeight;
193 d = min(w, rw) * min(h, rh);
194 d = w*h + rw*rh - 2*d;
195 if (d < maxd) {
196 maxd = d;
197 frame = &format->frame[i];
200 if (maxd == 0)
201 break;
204 if (frame == NULL) {
205 uvc_trace(UVC_TRACE_FORMAT, "Unsupported size %ux%u.\n",
206 fmt->fmt.pix.width, fmt->fmt.pix.height);
207 return -EINVAL;
210 /* Use the default frame interval. */
211 interval = frame->dwDefaultFrameInterval;
212 uvc_trace(UVC_TRACE_FORMAT, "Using default frame interval %u.%u us "
213 "(%u.%u fps).\n", interval/10, interval%10, 10000000/interval,
214 (100000000/interval)%10);
216 /* Set the format index, frame index and frame interval. */
217 memset(probe, 0, sizeof *probe);
218 probe->bmHint = 1; /* dwFrameInterval */
219 probe->bFormatIndex = format->index;
220 probe->bFrameIndex = frame->bFrameIndex;
221 probe->dwFrameInterval = uvc_try_frame_interval(frame, interval);
222 /* Some webcams stall the probe control set request when the
223 * dwMaxVideoFrameSize field is set to zero. The UVC specification
224 * clearly states that the field is read-only from the host, so this
225 * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by
226 * the webcam to work around the problem.
228 * The workaround could probably be enabled for all webcams, so the
229 * quirk can be removed if needed. It's currently useful to detect
230 * webcam bugs and fix them before they hit the market (providing
231 * developers test their webcams with the Linux driver as well as with
232 * the Windows driver).
234 mutex_lock(&stream->mutex);
235 if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS)
236 probe->dwMaxVideoFrameSize =
237 stream->ctrl.dwMaxVideoFrameSize;
239 /* Probe the device. */
240 ret = uvc_probe_video(stream, probe);
241 mutex_unlock(&stream->mutex);
242 if (ret < 0)
243 goto done;
245 fmt->fmt.pix.width = frame->wWidth;
246 fmt->fmt.pix.height = frame->wHeight;
247 fmt->fmt.pix.field = V4L2_FIELD_NONE;
248 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
249 fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize;
250 fmt->fmt.pix.colorspace = format->colorspace;
251 fmt->fmt.pix.priv = 0;
253 if (uvc_format != NULL)
254 *uvc_format = format;
255 if (uvc_frame != NULL)
256 *uvc_frame = frame;
258 done:
259 return ret;
262 static int uvc_v4l2_get_format(struct uvc_streaming *stream,
263 struct v4l2_format *fmt)
265 struct uvc_format *format;
266 struct uvc_frame *frame;
267 int ret = 0;
269 if (fmt->type != stream->type)
270 return -EINVAL;
272 mutex_lock(&stream->mutex);
273 format = stream->cur_format;
274 frame = stream->cur_frame;
276 if (format == NULL || frame == NULL) {
277 ret = -EINVAL;
278 goto done;
281 fmt->fmt.pix.pixelformat = format->fcc;
282 fmt->fmt.pix.width = frame->wWidth;
283 fmt->fmt.pix.height = frame->wHeight;
284 fmt->fmt.pix.field = V4L2_FIELD_NONE;
285 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
286 fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize;
287 fmt->fmt.pix.colorspace = format->colorspace;
288 fmt->fmt.pix.priv = 0;
290 done:
291 mutex_unlock(&stream->mutex);
292 return ret;
295 static int uvc_v4l2_set_format(struct uvc_streaming *stream,
296 struct v4l2_format *fmt)
298 struct uvc_streaming_control probe;
299 struct uvc_format *format;
300 struct uvc_frame *frame;
301 int ret;
303 if (fmt->type != stream->type)
304 return -EINVAL;
306 ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame);
307 if (ret < 0)
308 return ret;
310 mutex_lock(&stream->mutex);
312 if (uvc_queue_allocated(&stream->queue)) {
313 ret = -EBUSY;
314 goto done;
317 memcpy(&stream->ctrl, &probe, sizeof probe);
318 stream->cur_format = format;
319 stream->cur_frame = frame;
321 done:
322 mutex_unlock(&stream->mutex);
323 return ret;
326 static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream,
327 struct v4l2_streamparm *parm)
329 uint32_t numerator, denominator;
331 if (parm->type != stream->type)
332 return -EINVAL;
334 mutex_lock(&stream->mutex);
335 numerator = stream->ctrl.dwFrameInterval;
336 mutex_unlock(&stream->mutex);
338 denominator = 10000000;
339 uvc_simplify_fraction(&numerator, &denominator, 8, 333);
341 memset(parm, 0, sizeof *parm);
342 parm->type = stream->type;
344 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
345 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
346 parm->parm.capture.capturemode = 0;
347 parm->parm.capture.timeperframe.numerator = numerator;
348 parm->parm.capture.timeperframe.denominator = denominator;
349 parm->parm.capture.extendedmode = 0;
350 parm->parm.capture.readbuffers = 0;
351 } else {
352 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
353 parm->parm.output.outputmode = 0;
354 parm->parm.output.timeperframe.numerator = numerator;
355 parm->parm.output.timeperframe.denominator = denominator;
358 return 0;
361 static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
362 struct v4l2_streamparm *parm)
364 struct uvc_streaming_control probe;
365 struct v4l2_fract timeperframe;
366 uint32_t interval;
367 int ret;
369 if (parm->type != stream->type)
370 return -EINVAL;
372 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
373 timeperframe = parm->parm.capture.timeperframe;
374 else
375 timeperframe = parm->parm.output.timeperframe;
377 interval = uvc_fraction_to_interval(timeperframe.numerator,
378 timeperframe.denominator);
379 uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
380 timeperframe.numerator, timeperframe.denominator, interval);
382 mutex_lock(&stream->mutex);
384 if (uvc_queue_streaming(&stream->queue)) {
385 mutex_unlock(&stream->mutex);
386 return -EBUSY;
389 memcpy(&probe, &stream->ctrl, sizeof probe);
390 probe.dwFrameInterval =
391 uvc_try_frame_interval(stream->cur_frame, interval);
393 /* Probe the device with the new settings. */
394 ret = uvc_probe_video(stream, &probe);
395 if (ret < 0) {
396 mutex_unlock(&stream->mutex);
397 return ret;
400 memcpy(&stream->ctrl, &probe, sizeof probe);
401 mutex_unlock(&stream->mutex);
403 /* Return the actual frame period. */
404 timeperframe.numerator = probe.dwFrameInterval;
405 timeperframe.denominator = 10000000;
406 uvc_simplify_fraction(&timeperframe.numerator,
407 &timeperframe.denominator, 8, 333);
409 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
410 parm->parm.capture.timeperframe = timeperframe;
411 else
412 parm->parm.output.timeperframe = timeperframe;
414 return 0;
417 /* ------------------------------------------------------------------------
418 * Privilege management
422 * Privilege management is the multiple-open implementation basis. The current
423 * implementation is completely transparent for the end-user and doesn't
424 * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls.
425 * Those ioctls enable finer control on the device (by making possible for a
426 * user to request exclusive access to a device), but are not mature yet.
427 * Switching to the V4L2 priority mechanism might be considered in the future
428 * if this situation changes.
430 * Each open instance of a UVC device can either be in a privileged or
431 * unprivileged state. Only a single instance can be in a privileged state at
432 * a given time. Trying to perform an operation that requires privileges will
433 * automatically acquire the required privileges if possible, or return -EBUSY
434 * otherwise. Privileges are dismissed when closing the instance or when
435 * freeing the video buffers using VIDIOC_REQBUFS.
437 * Operations that require privileges are:
439 * - VIDIOC_S_INPUT
440 * - VIDIOC_S_PARM
441 * - VIDIOC_S_FMT
442 * - VIDIOC_REQBUFS
444 static int uvc_acquire_privileges(struct uvc_fh *handle)
446 /* Always succeed if the handle is already privileged. */
447 if (handle->state == UVC_HANDLE_ACTIVE)
448 return 0;
450 /* Check if the device already has a privileged handle. */
451 if (atomic_inc_return(&handle->stream->active) != 1) {
452 atomic_dec(&handle->stream->active);
453 return -EBUSY;
456 handle->state = UVC_HANDLE_ACTIVE;
457 return 0;
460 static void uvc_dismiss_privileges(struct uvc_fh *handle)
462 if (handle->state == UVC_HANDLE_ACTIVE)
463 atomic_dec(&handle->stream->active);
465 handle->state = UVC_HANDLE_PASSIVE;
468 static int uvc_has_privileges(struct uvc_fh *handle)
470 return handle->state == UVC_HANDLE_ACTIVE;
473 /* ------------------------------------------------------------------------
474 * V4L2 file operations
477 static int uvc_v4l2_open(struct file *file)
479 struct uvc_streaming *stream;
480 struct uvc_fh *handle;
481 int ret = 0;
483 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
484 stream = video_drvdata(file);
486 if (stream->dev->state & UVC_DEV_DISCONNECTED)
487 return -ENODEV;
489 ret = usb_autopm_get_interface(stream->dev->intf);
490 if (ret < 0)
491 return ret;
493 /* Create the device handle. */
494 handle = kzalloc(sizeof *handle, GFP_KERNEL);
495 if (handle == NULL) {
496 usb_autopm_put_interface(stream->dev->intf);
497 return -ENOMEM;
500 if (atomic_inc_return(&stream->dev->users) == 1) {
501 ret = uvc_status_start(stream->dev);
502 if (ret < 0) {
503 usb_autopm_put_interface(stream->dev->intf);
504 atomic_dec(&stream->dev->users);
505 kfree(handle);
506 return ret;
510 v4l2_fh_init(&handle->vfh, stream->vdev);
511 v4l2_fh_add(&handle->vfh);
512 handle->chain = stream->chain;
513 handle->stream = stream;
514 handle->state = UVC_HANDLE_PASSIVE;
515 file->private_data = handle;
517 return 0;
520 static int uvc_v4l2_release(struct file *file)
522 struct uvc_fh *handle = file->private_data;
523 struct uvc_streaming *stream = handle->stream;
525 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");
527 /* Only free resources if this is a privileged handle. */
528 if (uvc_has_privileges(handle)) {
529 uvc_video_enable(stream, 0);
530 uvc_free_buffers(&stream->queue);
533 /* Release the file handle. */
534 uvc_dismiss_privileges(handle);
535 v4l2_fh_del(&handle->vfh);
536 v4l2_fh_exit(&handle->vfh);
537 kfree(handle);
538 file->private_data = NULL;
540 if (atomic_dec_return(&stream->dev->users) == 0)
541 uvc_status_stop(stream->dev);
543 usb_autopm_put_interface(stream->dev->intf);
544 return 0;
547 static long uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
549 struct video_device *vdev = video_devdata(file);
550 struct uvc_fh *handle = file->private_data;
551 struct uvc_video_chain *chain = handle->chain;
552 struct uvc_streaming *stream = handle->stream;
553 long ret = 0;
555 switch (cmd) {
556 /* Query capabilities */
557 case VIDIOC_QUERYCAP:
559 struct v4l2_capability *cap = arg;
561 memset(cap, 0, sizeof *cap);
562 strlcpy(cap->driver, "uvcvideo", sizeof cap->driver);
563 strlcpy(cap->card, vdev->name, sizeof cap->card);
564 usb_make_path(stream->dev->udev,
565 cap->bus_info, sizeof(cap->bus_info));
566 cap->version = LINUX_VERSION_CODE;
567 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
568 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
569 | V4L2_CAP_STREAMING;
570 else
571 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT
572 | V4L2_CAP_STREAMING;
573 break;
576 /* Get, Set & Query control */
577 case VIDIOC_QUERYCTRL:
578 return uvc_query_v4l2_ctrl(chain, arg);
580 case VIDIOC_G_CTRL:
582 struct v4l2_control *ctrl = arg;
583 struct v4l2_ext_control xctrl;
585 memset(&xctrl, 0, sizeof xctrl);
586 xctrl.id = ctrl->id;
588 ret = uvc_ctrl_begin(chain);
589 if (ret < 0)
590 return ret;
592 ret = uvc_ctrl_get(chain, &xctrl);
593 uvc_ctrl_rollback(handle);
594 if (ret >= 0)
595 ctrl->value = xctrl.value;
596 break;
599 case VIDIOC_S_CTRL:
601 struct v4l2_control *ctrl = arg;
602 struct v4l2_ext_control xctrl;
604 memset(&xctrl, 0, sizeof xctrl);
605 xctrl.id = ctrl->id;
606 xctrl.value = ctrl->value;
608 ret = uvc_ctrl_begin(chain);
609 if (ret < 0)
610 return ret;
612 ret = uvc_ctrl_set(chain, &xctrl);
613 if (ret < 0) {
614 uvc_ctrl_rollback(handle);
615 return ret;
617 ret = uvc_ctrl_commit(handle, &xctrl, 1);
618 if (ret == 0)
619 ctrl->value = xctrl.value;
620 break;
623 case VIDIOC_QUERYMENU:
624 return uvc_query_v4l2_menu(chain, arg);
626 case VIDIOC_G_EXT_CTRLS:
628 struct v4l2_ext_controls *ctrls = arg;
629 struct v4l2_ext_control *ctrl = ctrls->controls;
630 unsigned int i;
632 ret = uvc_ctrl_begin(chain);
633 if (ret < 0)
634 return ret;
636 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
637 ret = uvc_ctrl_get(chain, ctrl);
638 if (ret < 0) {
639 uvc_ctrl_rollback(handle);
640 ctrls->error_idx = i;
641 return ret;
644 ctrls->error_idx = 0;
645 ret = uvc_ctrl_rollback(handle);
646 break;
649 case VIDIOC_S_EXT_CTRLS:
650 case VIDIOC_TRY_EXT_CTRLS:
652 struct v4l2_ext_controls *ctrls = arg;
653 struct v4l2_ext_control *ctrl = ctrls->controls;
654 unsigned int i;
656 ret = uvc_ctrl_begin(chain);
657 if (ret < 0)
658 return ret;
660 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
661 ret = uvc_ctrl_set(chain, ctrl);
662 if (ret < 0) {
663 uvc_ctrl_rollback(handle);
664 ctrls->error_idx = i;
665 return ret;
669 ctrls->error_idx = 0;
671 if (cmd == VIDIOC_S_EXT_CTRLS)
672 ret = uvc_ctrl_commit(handle,
673 ctrls->controls, ctrls->count);
674 else
675 ret = uvc_ctrl_rollback(handle);
676 break;
679 /* Get, Set & Enum input */
680 case VIDIOC_ENUMINPUT:
682 const struct uvc_entity *selector = chain->selector;
683 struct v4l2_input *input = arg;
684 struct uvc_entity *iterm = NULL;
685 u32 index = input->index;
686 int pin = 0;
688 if (selector == NULL ||
689 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
690 if (index != 0)
691 return -EINVAL;
692 list_for_each_entry(iterm, &chain->entities, chain) {
693 if (UVC_ENTITY_IS_ITERM(iterm))
694 break;
696 pin = iterm->id;
697 } else if (index < selector->bNrInPins) {
698 pin = selector->baSourceID[index];
699 list_for_each_entry(iterm, &chain->entities, chain) {
700 if (!UVC_ENTITY_IS_ITERM(iterm))
701 continue;
702 if (iterm->id == pin)
703 break;
707 if (iterm == NULL || iterm->id != pin)
708 return -EINVAL;
710 memset(input, 0, sizeof *input);
711 input->index = index;
712 strlcpy(input->name, iterm->name, sizeof input->name);
713 if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
714 input->type = V4L2_INPUT_TYPE_CAMERA;
715 break;
718 case VIDIOC_G_INPUT:
720 u8 input;
722 if (chain->selector == NULL ||
723 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
724 *(int *)arg = 0;
725 break;
728 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
729 chain->selector->id, chain->dev->intfnum,
730 UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
731 if (ret < 0)
732 return ret;
734 *(int *)arg = input - 1;
735 break;
738 case VIDIOC_S_INPUT:
740 u32 input = *(u32 *)arg + 1;
742 if ((ret = uvc_acquire_privileges(handle)) < 0)
743 return ret;
745 if (chain->selector == NULL ||
746 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
747 if (input != 1)
748 return -EINVAL;
749 break;
752 if (input == 0 || input > chain->selector->bNrInPins)
753 return -EINVAL;
755 return uvc_query_ctrl(chain->dev, UVC_SET_CUR,
756 chain->selector->id, chain->dev->intfnum,
757 UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
760 /* Try, Get, Set & Enum format */
761 case VIDIOC_ENUM_FMT:
763 struct v4l2_fmtdesc *fmt = arg;
764 struct uvc_format *format;
765 enum v4l2_buf_type type = fmt->type;
766 __u32 index = fmt->index;
768 if (fmt->type != stream->type ||
769 fmt->index >= stream->nformats)
770 return -EINVAL;
772 memset(fmt, 0, sizeof(*fmt));
773 fmt->index = index;
774 fmt->type = type;
776 format = &stream->format[fmt->index];
777 fmt->flags = 0;
778 if (format->flags & UVC_FMT_FLAG_COMPRESSED)
779 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
780 strlcpy(fmt->description, format->name,
781 sizeof fmt->description);
782 fmt->description[sizeof fmt->description - 1] = 0;
783 fmt->pixelformat = format->fcc;
784 break;
787 case VIDIOC_TRY_FMT:
789 struct uvc_streaming_control probe;
791 return uvc_v4l2_try_format(stream, arg, &probe, NULL, NULL);
794 case VIDIOC_S_FMT:
795 if ((ret = uvc_acquire_privileges(handle)) < 0)
796 return ret;
798 return uvc_v4l2_set_format(stream, arg);
800 case VIDIOC_G_FMT:
801 return uvc_v4l2_get_format(stream, arg);
803 /* Frame size enumeration */
804 case VIDIOC_ENUM_FRAMESIZES:
806 struct v4l2_frmsizeenum *fsize = arg;
807 struct uvc_format *format = NULL;
808 struct uvc_frame *frame;
809 int i;
811 /* Look for the given pixel format */
812 for (i = 0; i < stream->nformats; i++) {
813 if (stream->format[i].fcc ==
814 fsize->pixel_format) {
815 format = &stream->format[i];
816 break;
819 if (format == NULL)
820 return -EINVAL;
822 if (fsize->index >= format->nframes)
823 return -EINVAL;
825 frame = &format->frame[fsize->index];
826 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
827 fsize->discrete.width = frame->wWidth;
828 fsize->discrete.height = frame->wHeight;
829 break;
832 /* Frame interval enumeration */
833 case VIDIOC_ENUM_FRAMEINTERVALS:
835 struct v4l2_frmivalenum *fival = arg;
836 struct uvc_format *format = NULL;
837 struct uvc_frame *frame = NULL;
838 int i;
840 /* Look for the given pixel format and frame size */
841 for (i = 0; i < stream->nformats; i++) {
842 if (stream->format[i].fcc ==
843 fival->pixel_format) {
844 format = &stream->format[i];
845 break;
848 if (format == NULL)
849 return -EINVAL;
851 for (i = 0; i < format->nframes; i++) {
852 if (format->frame[i].wWidth == fival->width &&
853 format->frame[i].wHeight == fival->height) {
854 frame = &format->frame[i];
855 break;
858 if (frame == NULL)
859 return -EINVAL;
861 if (frame->bFrameIntervalType) {
862 if (fival->index >= frame->bFrameIntervalType)
863 return -EINVAL;
865 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
866 fival->discrete.numerator =
867 frame->dwFrameInterval[fival->index];
868 fival->discrete.denominator = 10000000;
869 uvc_simplify_fraction(&fival->discrete.numerator,
870 &fival->discrete.denominator, 8, 333);
871 } else {
872 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
873 fival->stepwise.min.numerator =
874 frame->dwFrameInterval[0];
875 fival->stepwise.min.denominator = 10000000;
876 fival->stepwise.max.numerator =
877 frame->dwFrameInterval[1];
878 fival->stepwise.max.denominator = 10000000;
879 fival->stepwise.step.numerator =
880 frame->dwFrameInterval[2];
881 fival->stepwise.step.denominator = 10000000;
882 uvc_simplify_fraction(&fival->stepwise.min.numerator,
883 &fival->stepwise.min.denominator, 8, 333);
884 uvc_simplify_fraction(&fival->stepwise.max.numerator,
885 &fival->stepwise.max.denominator, 8, 333);
886 uvc_simplify_fraction(&fival->stepwise.step.numerator,
887 &fival->stepwise.step.denominator, 8, 333);
889 break;
892 /* Get & Set streaming parameters */
893 case VIDIOC_G_PARM:
894 return uvc_v4l2_get_streamparm(stream, arg);
896 case VIDIOC_S_PARM:
897 if ((ret = uvc_acquire_privileges(handle)) < 0)
898 return ret;
900 return uvc_v4l2_set_streamparm(stream, arg);
902 /* Cropping and scaling */
903 case VIDIOC_CROPCAP:
905 struct v4l2_cropcap *ccap = arg;
907 if (ccap->type != stream->type)
908 return -EINVAL;
910 ccap->bounds.left = 0;
911 ccap->bounds.top = 0;
913 mutex_lock(&stream->mutex);
914 ccap->bounds.width = stream->cur_frame->wWidth;
915 ccap->bounds.height = stream->cur_frame->wHeight;
916 mutex_unlock(&stream->mutex);
918 ccap->defrect = ccap->bounds;
920 ccap->pixelaspect.numerator = 1;
921 ccap->pixelaspect.denominator = 1;
922 break;
925 case VIDIOC_G_CROP:
926 case VIDIOC_S_CROP:
927 return -EINVAL;
929 /* Buffers & streaming */
930 case VIDIOC_REQBUFS:
931 if ((ret = uvc_acquire_privileges(handle)) < 0)
932 return ret;
934 mutex_lock(&stream->mutex);
935 ret = uvc_alloc_buffers(&stream->queue, arg);
936 mutex_unlock(&stream->mutex);
937 if (ret < 0)
938 return ret;
940 if (ret == 0)
941 uvc_dismiss_privileges(handle);
943 ret = 0;
944 break;
946 case VIDIOC_QUERYBUF:
948 struct v4l2_buffer *buf = arg;
950 if (!uvc_has_privileges(handle))
951 return -EBUSY;
953 return uvc_query_buffer(&stream->queue, buf);
956 case VIDIOC_QBUF:
957 if (!uvc_has_privileges(handle))
958 return -EBUSY;
960 return uvc_queue_buffer(&stream->queue, arg);
962 case VIDIOC_DQBUF:
963 if (!uvc_has_privileges(handle))
964 return -EBUSY;
966 return uvc_dequeue_buffer(&stream->queue, arg,
967 file->f_flags & O_NONBLOCK);
969 case VIDIOC_STREAMON:
971 int *type = arg;
973 if (*type != stream->type)
974 return -EINVAL;
976 if (!uvc_has_privileges(handle))
977 return -EBUSY;
979 mutex_lock(&stream->mutex);
980 ret = uvc_video_enable(stream, 1);
981 mutex_unlock(&stream->mutex);
982 if (ret < 0)
983 return ret;
984 break;
987 case VIDIOC_STREAMOFF:
989 int *type = arg;
991 if (*type != stream->type)
992 return -EINVAL;
994 if (!uvc_has_privileges(handle))
995 return -EBUSY;
997 return uvc_video_enable(stream, 0);
1000 case VIDIOC_SUBSCRIBE_EVENT:
1002 struct v4l2_event_subscription *sub = arg;
1004 switch (sub->type) {
1005 case V4L2_EVENT_CTRL:
1006 return v4l2_event_subscribe(&handle->vfh, sub, 0,
1007 &uvc_ctrl_sub_ev_ops);
1008 default:
1009 return -EINVAL;
1013 case VIDIOC_UNSUBSCRIBE_EVENT:
1014 return v4l2_event_unsubscribe(&handle->vfh, arg);
1016 case VIDIOC_DQEVENT:
1017 return v4l2_event_dequeue(&handle->vfh, arg,
1018 file->f_flags & O_NONBLOCK);
1020 /* Analog video standards make no sense for digital cameras. */
1021 case VIDIOC_ENUMSTD:
1022 case VIDIOC_QUERYSTD:
1023 case VIDIOC_G_STD:
1024 case VIDIOC_S_STD:
1026 case VIDIOC_OVERLAY:
1028 case VIDIOC_ENUMAUDIO:
1029 case VIDIOC_ENUMAUDOUT:
1031 case VIDIOC_ENUMOUTPUT:
1032 uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd);
1033 return -EINVAL;
1035 case UVCIOC_CTRL_MAP:
1036 return uvc_ioctl_ctrl_map(chain, arg);
1038 case UVCIOC_CTRL_QUERY:
1039 return uvc_xu_ctrl_query(chain, arg);
1041 default:
1042 uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n", cmd);
1043 return -ENOTTY;
1046 return ret;
1049 static long uvc_v4l2_ioctl(struct file *file,
1050 unsigned int cmd, unsigned long arg)
1052 if (uvc_trace_param & UVC_TRACE_IOCTL) {
1053 uvc_printk(KERN_DEBUG, "uvc_v4l2_ioctl(");
1054 v4l_printk_ioctl(NULL, cmd);
1055 printk(")\n");
1058 return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl);
1061 #ifdef CONFIG_COMPAT
1062 struct uvc_xu_control_mapping32 {
1063 __u32 id;
1064 __u8 name[32];
1065 __u8 entity[16];
1066 __u8 selector;
1068 __u8 size;
1069 __u8 offset;
1070 __u32 v4l2_type;
1071 __u32 data_type;
1073 compat_caddr_t menu_info;
1074 __u32 menu_count;
1076 __u32 reserved[4];
1079 static int uvc_v4l2_get_xu_mapping(struct uvc_xu_control_mapping *kp,
1080 const struct uvc_xu_control_mapping32 __user *up)
1082 struct uvc_menu_info __user *umenus;
1083 struct uvc_menu_info __user *kmenus;
1084 compat_caddr_t p;
1086 if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
1087 __copy_from_user(kp, up, offsetof(typeof(*up), menu_info)) ||
1088 __get_user(kp->menu_count, &up->menu_count))
1089 return -EFAULT;
1091 memset(kp->reserved, 0, sizeof(kp->reserved));
1093 if (kp->menu_count == 0) {
1094 kp->menu_info = NULL;
1095 return 0;
1098 if (__get_user(p, &up->menu_info))
1099 return -EFAULT;
1100 umenus = compat_ptr(p);
1101 if (!access_ok(VERIFY_READ, umenus, kp->menu_count * sizeof(*umenus)))
1102 return -EFAULT;
1104 kmenus = compat_alloc_user_space(kp->menu_count * sizeof(*kmenus));
1105 if (kmenus == NULL)
1106 return -EFAULT;
1107 kp->menu_info = kmenus;
1109 if (copy_in_user(kmenus, umenus, kp->menu_count * sizeof(*umenus)))
1110 return -EFAULT;
1112 return 0;
1115 static int uvc_v4l2_put_xu_mapping(const struct uvc_xu_control_mapping *kp,
1116 struct uvc_xu_control_mapping32 __user *up)
1118 struct uvc_menu_info __user *umenus;
1119 struct uvc_menu_info __user *kmenus = kp->menu_info;
1120 compat_caddr_t p;
1122 if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
1123 __copy_to_user(up, kp, offsetof(typeof(*up), menu_info)) ||
1124 __put_user(kp->menu_count, &up->menu_count))
1125 return -EFAULT;
1127 if (__clear_user(up->reserved, sizeof(up->reserved)))
1128 return -EFAULT;
1130 if (kp->menu_count == 0)
1131 return 0;
1133 if (get_user(p, &up->menu_info))
1134 return -EFAULT;
1135 umenus = compat_ptr(p);
1137 if (copy_in_user(umenus, kmenus, kp->menu_count * sizeof(*umenus)))
1138 return -EFAULT;
1140 return 0;
1143 struct uvc_xu_control_query32 {
1144 __u8 unit;
1145 __u8 selector;
1146 __u8 query;
1147 __u16 size;
1148 compat_caddr_t data;
1151 static int uvc_v4l2_get_xu_query(struct uvc_xu_control_query *kp,
1152 const struct uvc_xu_control_query32 __user *up)
1154 u8 __user *udata;
1155 u8 __user *kdata;
1156 compat_caddr_t p;
1158 if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
1159 __copy_from_user(kp, up, offsetof(typeof(*up), data)))
1160 return -EFAULT;
1162 if (kp->size == 0) {
1163 kp->data = NULL;
1164 return 0;
1167 if (__get_user(p, &up->data))
1168 return -EFAULT;
1169 udata = compat_ptr(p);
1170 if (!access_ok(VERIFY_READ, udata, kp->size))
1171 return -EFAULT;
1173 kdata = compat_alloc_user_space(kp->size);
1174 if (kdata == NULL)
1175 return -EFAULT;
1176 kp->data = kdata;
1178 if (copy_in_user(kdata, udata, kp->size))
1179 return -EFAULT;
1181 return 0;
1184 static int uvc_v4l2_put_xu_query(const struct uvc_xu_control_query *kp,
1185 struct uvc_xu_control_query32 __user *up)
1187 u8 __user *udata;
1188 u8 __user *kdata = kp->data;
1189 compat_caddr_t p;
1191 if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
1192 __copy_to_user(up, kp, offsetof(typeof(*up), data)))
1193 return -EFAULT;
1195 if (kp->size == 0)
1196 return 0;
1198 if (get_user(p, &up->data))
1199 return -EFAULT;
1200 udata = compat_ptr(p);
1201 if (!access_ok(VERIFY_READ, udata, kp->size))
1202 return -EFAULT;
1204 if (copy_in_user(udata, kdata, kp->size))
1205 return -EFAULT;
1207 return 0;
1210 #define UVCIOC_CTRL_MAP32 _IOWR('u', 0x20, struct uvc_xu_control_mapping32)
1211 #define UVCIOC_CTRL_QUERY32 _IOWR('u', 0x21, struct uvc_xu_control_query32)
1213 static long uvc_v4l2_compat_ioctl32(struct file *file,
1214 unsigned int cmd, unsigned long arg)
1216 union {
1217 struct uvc_xu_control_mapping xmap;
1218 struct uvc_xu_control_query xqry;
1219 } karg;
1220 void __user *up = compat_ptr(arg);
1221 mm_segment_t old_fs;
1222 long ret;
1224 switch (cmd) {
1225 case UVCIOC_CTRL_MAP32:
1226 cmd = UVCIOC_CTRL_MAP;
1227 ret = uvc_v4l2_get_xu_mapping(&karg.xmap, up);
1228 break;
1230 case UVCIOC_CTRL_QUERY32:
1231 cmd = UVCIOC_CTRL_QUERY;
1232 ret = uvc_v4l2_get_xu_query(&karg.xqry, up);
1233 break;
1235 default:
1236 return -ENOIOCTLCMD;
1239 old_fs = get_fs();
1240 set_fs(KERNEL_DS);
1241 ret = uvc_v4l2_ioctl(file, cmd, (unsigned long)&karg);
1242 set_fs(old_fs);
1244 if (ret < 0)
1245 return ret;
1247 switch (cmd) {
1248 case UVCIOC_CTRL_MAP:
1249 ret = uvc_v4l2_put_xu_mapping(&karg.xmap, up);
1250 break;
1252 case UVCIOC_CTRL_QUERY:
1253 ret = uvc_v4l2_put_xu_query(&karg.xqry, up);
1254 break;
1257 return ret;
1259 #endif
1261 static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1262 size_t count, loff_t *ppos)
1264 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
1265 return -EINVAL;
1268 static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1270 struct uvc_fh *handle = file->private_data;
1271 struct uvc_streaming *stream = handle->stream;
1273 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
1275 return uvc_queue_mmap(&stream->queue, vma);
1278 static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
1280 struct uvc_fh *handle = file->private_data;
1281 struct uvc_streaming *stream = handle->stream;
1283 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
1285 return uvc_queue_poll(&stream->queue, file, wait);
1288 #ifndef CONFIG_MMU
1289 static unsigned long uvc_v4l2_get_unmapped_area(struct file *file,
1290 unsigned long addr, unsigned long len, unsigned long pgoff,
1291 unsigned long flags)
1293 struct uvc_fh *handle = file->private_data;
1294 struct uvc_streaming *stream = handle->stream;
1296 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_get_unmapped_area\n");
1298 return uvc_queue_get_unmapped_area(&stream->queue, pgoff);
1300 #endif
1302 const struct v4l2_file_operations uvc_fops = {
1303 .owner = THIS_MODULE,
1304 .open = uvc_v4l2_open,
1305 .release = uvc_v4l2_release,
1306 .unlocked_ioctl = uvc_v4l2_ioctl,
1307 #ifdef CONFIG_COMPAT
1308 .compat_ioctl32 = uvc_v4l2_compat_ioctl32,
1309 #endif
1310 .read = uvc_v4l2_read,
1311 .mmap = uvc_v4l2_mmap,
1312 .poll = uvc_v4l2_poll,
1313 #ifndef CONFIG_MMU
1314 .get_unmapped_area = uvc_v4l2_get_unmapped_area,
1315 #endif