RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / drivers / media / video / uvc / uvc_v4l2.c
blobec582e15af563ad99075acfd4c2cfc73520934df
1 /*
2 * uvc_v4l2.c -- USB Video Class driver - V4L2 API
4 * Copyright (C) 2005-2009
5 * Laurent Pinchart (laurent.pinchart@skynet.be)
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/kernel.h>
15 #include <linux/version.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>
22 #include <linux/mm.h>
23 #include <linux/wait.h>
24 #include <asm/atomic.h>
26 #include <media/v4l2-common.h>
27 #include <media/v4l2-ioctl.h>
29 #include "uvcvideo.h"
31 /* ------------------------------------------------------------------------
32 * UVC ioctls
34 static int uvc_ioctl_ctrl_map(struct uvc_xu_control_mapping *xmap, int old)
36 struct uvc_control_mapping *map;
37 unsigned int size;
38 int ret;
40 map = kzalloc(sizeof *map, GFP_KERNEL);
41 if (map == NULL)
42 return -ENOMEM;
44 map->id = xmap->id;
45 memcpy(map->name, xmap->name, sizeof map->name);
46 memcpy(map->entity, xmap->entity, sizeof map->entity);
47 map->selector = xmap->selector;
48 map->size = xmap->size;
49 map->offset = xmap->offset;
50 map->v4l2_type = xmap->v4l2_type;
51 map->data_type = xmap->data_type;
53 switch (xmap->v4l2_type) {
54 case V4L2_CTRL_TYPE_INTEGER:
55 case V4L2_CTRL_TYPE_BOOLEAN:
56 case V4L2_CTRL_TYPE_BUTTON:
57 break;
59 case V4L2_CTRL_TYPE_MENU:
60 if (old) {
61 ret = -EINVAL;
62 goto done;
65 size = xmap->menu_count * sizeof(*map->menu_info);
66 map->menu_info = kmalloc(size, GFP_KERNEL);
67 if (map->menu_info == NULL) {
68 ret = -ENOMEM;
69 goto done;
72 if (copy_from_user(map->menu_info, xmap->menu_info, size)) {
73 ret = -EFAULT;
74 goto done;
77 map->menu_count = xmap->menu_count;
78 break;
80 default:
81 ret = -EINVAL;
82 goto done;
85 ret = uvc_ctrl_add_mapping(map);
87 done:
88 if (ret < 0) {
89 kfree(map->menu_info);
90 kfree(map);
93 return ret;
96 /* ------------------------------------------------------------------------
97 * V4L2 interface
101 * Mapping V4L2 controls to UVC controls can be straighforward if done well.
102 * Most of the UVC controls exist in V4L2, and can be mapped directly. Some
103 * must be grouped (for instance the Red Balance, Blue Balance and Do White
104 * Balance V4L2 controls use the White Balance Component UVC control) or
105 * otherwise translated. The approach we take here is to use a translation
106 * table for the controls that can be mapped directly, and handle the others
107 * manually.
109 static int uvc_v4l2_query_menu(struct uvc_video_chain *chain,
110 struct v4l2_querymenu *query_menu)
112 struct uvc_menu_info *menu_info;
113 struct uvc_control_mapping *mapping;
114 struct uvc_control *ctrl;
115 u32 index = query_menu->index;
116 u32 id = query_menu->id;
118 ctrl = uvc_find_control(chain, query_menu->id, &mapping);
119 if (ctrl == NULL || mapping->v4l2_type != V4L2_CTRL_TYPE_MENU)
120 return -EINVAL;
122 if (query_menu->index >= mapping->menu_count)
123 return -EINVAL;
125 memset(query_menu, 0, sizeof(*query_menu));
126 query_menu->id = id;
127 query_menu->index = index;
129 menu_info = &mapping->menu_info[query_menu->index];
130 strlcpy(query_menu->name, menu_info->name, sizeof query_menu->name);
131 return 0;
135 * Find the frame interval closest to the requested frame interval for the
136 * given frame format and size. This should be done by the device as part of
137 * the Video Probe and Commit negotiation, but some hardware don't implement
138 * that feature.
140 static __u32 uvc_try_frame_interval(struct uvc_frame *frame, __u32 interval)
142 unsigned int i;
144 if (frame->bFrameIntervalType) {
145 __u32 best = -1, dist;
147 for (i = 0; i < frame->bFrameIntervalType; ++i) {
148 dist = interval > frame->dwFrameInterval[i]
149 ? interval - frame->dwFrameInterval[i]
150 : frame->dwFrameInterval[i] - interval;
152 if (dist > best)
153 break;
155 best = dist;
158 interval = frame->dwFrameInterval[i-1];
159 } else {
160 const __u32 min = frame->dwFrameInterval[0];
161 const __u32 max = frame->dwFrameInterval[1];
162 const __u32 step = frame->dwFrameInterval[2];
164 interval = min + (interval - min + step/2) / step * step;
165 if (interval > max)
166 interval = max;
169 return interval;
172 static int uvc_v4l2_try_format(struct uvc_streaming *stream,
173 struct v4l2_format *fmt, struct uvc_streaming_control *probe,
174 struct uvc_format **uvc_format, struct uvc_frame **uvc_frame)
176 struct uvc_format *format = NULL;
177 struct uvc_frame *frame = NULL;
178 __u16 rw, rh;
179 unsigned int d, maxd;
180 unsigned int i;
181 __u32 interval;
182 int ret = 0;
183 __u8 *fcc;
185 if (fmt->type != stream->type)
186 return -EINVAL;
188 fcc = (__u8 *)&fmt->fmt.pix.pixelformat;
189 uvc_trace(UVC_TRACE_FORMAT, "Trying format 0x%08x (%c%c%c%c): %ux%u.\n",
190 fmt->fmt.pix.pixelformat,
191 fcc[0], fcc[1], fcc[2], fcc[3],
192 fmt->fmt.pix.width, fmt->fmt.pix.height);
194 /* Check if the hardware supports the requested format. */
195 for (i = 0; i < stream->nformats; ++i) {
196 format = &stream->format[i];
197 if (format->fcc == fmt->fmt.pix.pixelformat)
198 break;
201 if (format == NULL || format->fcc != fmt->fmt.pix.pixelformat) {
202 uvc_trace(UVC_TRACE_FORMAT, "Unsupported format 0x%08x.\n",
203 fmt->fmt.pix.pixelformat);
204 return -EINVAL;
207 /* Find the closest image size. The distance between image sizes is
208 * the size in pixels of the non-overlapping regions between the
209 * requested size and the frame-specified size.
211 rw = fmt->fmt.pix.width;
212 rh = fmt->fmt.pix.height;
213 maxd = (unsigned int)-1;
215 for (i = 0; i < format->nframes; ++i) {
216 __u16 w = format->frame[i].wWidth;
217 __u16 h = format->frame[i].wHeight;
219 d = min(w, rw) * min(h, rh);
220 d = w*h + rw*rh - 2*d;
221 if (d < maxd) {
222 maxd = d;
223 frame = &format->frame[i];
226 if (maxd == 0)
227 break;
230 if (frame == NULL) {
231 uvc_trace(UVC_TRACE_FORMAT, "Unsupported size %ux%u.\n",
232 fmt->fmt.pix.width, fmt->fmt.pix.height);
233 return -EINVAL;
236 /* Use the default frame interval. */
237 interval = frame->dwDefaultFrameInterval;
238 uvc_trace(UVC_TRACE_FORMAT, "Using default frame interval %u.%u us "
239 "(%u.%u fps).\n", interval/10, interval%10, 10000000/interval,
240 (100000000/interval)%10);
242 /* Set the format index, frame index and frame interval. */
243 memset(probe, 0, sizeof *probe);
244 probe->bmHint = 1; /* dwFrameInterval */
245 probe->bFormatIndex = format->index;
246 probe->bFrameIndex = frame->bFrameIndex;
247 probe->dwFrameInterval = uvc_try_frame_interval(frame, interval);
248 /* Some webcams stall the probe control set request when the
249 * dwMaxVideoFrameSize field is set to zero. The UVC specification
250 * clearly states that the field is read-only from the host, so this
251 * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by
252 * the webcam to work around the problem.
254 * The workaround could probably be enabled for all webcams, so the
255 * quirk can be removed if needed. It's currently useful to detect
256 * webcam bugs and fix them before they hit the market (providing
257 * developers test their webcams with the Linux driver as well as with
258 * the Windows driver).
260 if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS)
261 probe->dwMaxVideoFrameSize =
262 stream->ctrl.dwMaxVideoFrameSize;
264 /* Probe the device. */
265 ret = uvc_probe_video(stream, probe);
266 if (ret < 0)
267 goto done;
269 fmt->fmt.pix.width = frame->wWidth;
270 fmt->fmt.pix.height = frame->wHeight;
271 fmt->fmt.pix.field = V4L2_FIELD_NONE;
272 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
273 fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize;
274 fmt->fmt.pix.colorspace = format->colorspace;
275 fmt->fmt.pix.priv = 0;
277 if (uvc_format != NULL)
278 *uvc_format = format;
279 if (uvc_frame != NULL)
280 *uvc_frame = frame;
282 done:
283 return ret;
286 static int uvc_v4l2_get_format(struct uvc_streaming *stream,
287 struct v4l2_format *fmt)
289 struct uvc_format *format = stream->cur_format;
290 struct uvc_frame *frame = stream->cur_frame;
292 if (fmt->type != stream->type)
293 return -EINVAL;
295 if (format == NULL || frame == NULL)
296 return -EINVAL;
298 fmt->fmt.pix.pixelformat = format->fcc;
299 fmt->fmt.pix.width = frame->wWidth;
300 fmt->fmt.pix.height = frame->wHeight;
301 fmt->fmt.pix.field = V4L2_FIELD_NONE;
302 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
303 fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize;
304 fmt->fmt.pix.colorspace = format->colorspace;
305 fmt->fmt.pix.priv = 0;
307 return 0;
310 static int uvc_v4l2_set_format(struct uvc_streaming *stream,
311 struct v4l2_format *fmt)
313 struct uvc_streaming_control probe;
314 struct uvc_format *format;
315 struct uvc_frame *frame;
316 int ret;
318 if (fmt->type != stream->type)
319 return -EINVAL;
321 if (uvc_queue_allocated(&stream->queue))
322 return -EBUSY;
324 ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame);
325 if (ret < 0)
326 return ret;
328 memcpy(&stream->ctrl, &probe, sizeof probe);
329 stream->cur_format = format;
330 stream->cur_frame = frame;
332 return 0;
335 static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream,
336 struct v4l2_streamparm *parm)
338 uint32_t numerator, denominator;
340 if (parm->type != stream->type)
341 return -EINVAL;
343 numerator = stream->ctrl.dwFrameInterval;
344 denominator = 10000000;
345 uvc_simplify_fraction(&numerator, &denominator, 8, 333);
347 memset(parm, 0, sizeof *parm);
348 parm->type = stream->type;
350 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
351 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
352 parm->parm.capture.capturemode = 0;
353 parm->parm.capture.timeperframe.numerator = numerator;
354 parm->parm.capture.timeperframe.denominator = denominator;
355 parm->parm.capture.extendedmode = 0;
356 parm->parm.capture.readbuffers = 0;
357 } else {
358 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
359 parm->parm.output.outputmode = 0;
360 parm->parm.output.timeperframe.numerator = numerator;
361 parm->parm.output.timeperframe.denominator = denominator;
364 return 0;
367 static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
368 struct v4l2_streamparm *parm)
370 struct uvc_frame *frame = stream->cur_frame;
371 struct uvc_streaming_control probe;
372 struct v4l2_fract timeperframe;
373 uint32_t interval;
374 int ret;
376 if (parm->type != stream->type)
377 return -EINVAL;
379 if (uvc_queue_streaming(&stream->queue))
380 return -EBUSY;
382 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
383 timeperframe = parm->parm.capture.timeperframe;
384 else
385 timeperframe = parm->parm.output.timeperframe;
387 memcpy(&probe, &stream->ctrl, sizeof probe);
388 interval = uvc_fraction_to_interval(timeperframe.numerator,
389 timeperframe.denominator);
391 uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
392 timeperframe.numerator, timeperframe.denominator, interval);
393 probe.dwFrameInterval = uvc_try_frame_interval(frame, interval);
395 /* Probe the device with the new settings. */
396 ret = uvc_probe_video(stream, &probe);
397 if (ret < 0)
398 return ret;
400 memcpy(&stream->ctrl, &probe, sizeof probe);
402 /* Return the actual frame period. */
403 timeperframe.numerator = probe.dwFrameInterval;
404 timeperframe.denominator = 10000000;
405 uvc_simplify_fraction(&timeperframe.numerator,
406 &timeperframe.denominator, 8, 333);
408 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
409 parm->parm.capture.timeperframe = timeperframe;
410 else
411 parm->parm.output.timeperframe = timeperframe;
413 return 0;
416 /* ------------------------------------------------------------------------
417 * Privilege management
421 * Privilege management is the multiple-open implementation basis. The current
422 * implementation is completely transparent for the end-user and doesn't
423 * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls.
424 * Those ioctls enable finer control on the device (by making possible for a
425 * user to request exclusive access to a device), but are not mature yet.
426 * Switching to the V4L2 priority mechanism might be considered in the future
427 * if this situation changes.
429 * Each open instance of a UVC device can either be in a privileged or
430 * unprivileged state. Only a single instance can be in a privileged state at
431 * a given time. Trying to perform an operation that requires privileges will
432 * automatically acquire the required privileges if possible, or return -EBUSY
433 * otherwise. Privileges are dismissed when closing the instance or when
434 * freeing the video buffers using VIDIOC_REQBUFS.
436 * Operations that require privileges are:
438 * - VIDIOC_S_INPUT
439 * - VIDIOC_S_PARM
440 * - VIDIOC_S_FMT
441 * - VIDIOC_REQBUFS
443 static int uvc_acquire_privileges(struct uvc_fh *handle)
445 /* Always succeed if the handle is already privileged. */
446 if (handle->state == UVC_HANDLE_ACTIVE)
447 return 0;
449 /* Check if the device already has a privileged handle. */
450 if (atomic_inc_return(&handle->stream->active) != 1) {
451 atomic_dec(&handle->stream->active);
452 return -EBUSY;
455 handle->state = UVC_HANDLE_ACTIVE;
456 return 0;
459 static void uvc_dismiss_privileges(struct uvc_fh *handle)
461 if (handle->state == UVC_HANDLE_ACTIVE)
462 atomic_dec(&handle->stream->active);
464 handle->state = UVC_HANDLE_PASSIVE;
467 static int uvc_has_privileges(struct uvc_fh *handle)
469 return handle->state == UVC_HANDLE_ACTIVE;
472 /* ------------------------------------------------------------------------
473 * V4L2 file operations
476 static int uvc_v4l2_open(struct file *file)
478 struct uvc_streaming *stream;
479 struct uvc_fh *handle;
480 int ret = 0;
482 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
483 stream = video_drvdata(file);
485 if (stream->dev->state & UVC_DEV_DISCONNECTED)
486 return -ENODEV;
488 ret = usb_autopm_get_interface(stream->dev->intf);
489 if (ret < 0)
490 return ret;
492 /* Create the device handle. */
493 handle = kzalloc(sizeof *handle, GFP_KERNEL);
494 if (handle == NULL) {
495 usb_autopm_put_interface(stream->dev->intf);
496 return -ENOMEM;
499 if (atomic_inc_return(&stream->dev->users) == 1) {
500 ret = uvc_status_start(stream->dev);
501 if (ret < 0) {
502 usb_autopm_put_interface(stream->dev->intf);
503 atomic_dec(&stream->dev->users);
504 kfree(handle);
505 return ret;
509 handle->chain = stream->chain;
510 handle->stream = stream;
511 handle->state = UVC_HANDLE_PASSIVE;
512 file->private_data = handle;
514 return 0;
517 static int uvc_v4l2_release(struct file *file)
519 struct uvc_fh *handle = file->private_data;
520 struct uvc_streaming *stream = handle->stream;
522 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");
524 /* Only free resources if this is a privileged handle. */
525 if (uvc_has_privileges(handle)) {
526 uvc_video_enable(stream, 0);
528 mutex_lock(&stream->queue.mutex);
529 if (uvc_free_buffers(&stream->queue) < 0)
530 uvc_printk(KERN_ERR, "uvc_v4l2_release: Unable to "
531 "free buffers.\n");
532 mutex_unlock(&stream->queue.mutex);
535 /* Release the file handle. */
536 uvc_dismiss_privileges(handle);
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 = DRIVER_VERSION_NUMBER;
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(chain);
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(chain);
615 return ret;
617 ret = uvc_ctrl_commit(chain);
618 if (ret == 0)
619 ctrl->value = xctrl.value;
620 break;
623 case VIDIOC_QUERYMENU:
624 return uvc_v4l2_query_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(chain);
640 ctrls->error_idx = i;
641 return ret;
644 ctrls->error_idx = 0;
645 ret = uvc_ctrl_rollback(chain);
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(chain);
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(chain);
673 else
674 ret = uvc_ctrl_rollback(chain);
675 break;
678 /* Get, Set & Enum input */
679 case VIDIOC_ENUMINPUT:
681 const struct uvc_entity *selector = chain->selector;
682 struct v4l2_input *input = arg;
683 struct uvc_entity *iterm = NULL;
684 u32 index = input->index;
685 int pin = 0;
687 if (selector == NULL ||
688 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
689 if (index != 0)
690 return -EINVAL;
691 list_for_each_entry(iterm, &chain->entities, chain) {
692 if (UVC_ENTITY_IS_ITERM(iterm))
693 break;
695 pin = iterm->id;
696 } else if (pin < selector->bNrInPins) {
697 pin = selector->baSourceID[index];
698 list_for_each_entry(iterm, &chain->entities, chain) {
699 if (!UVC_ENTITY_IS_ITERM(iterm))
700 continue;
701 if (iterm->id == pin)
702 break;
706 if (iterm == NULL || iterm->id != pin)
707 return -EINVAL;
709 memset(input, 0, sizeof *input);
710 input->index = index;
711 strlcpy(input->name, iterm->name, sizeof input->name);
712 if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
713 input->type = V4L2_INPUT_TYPE_CAMERA;
714 break;
717 case VIDIOC_G_INPUT:
719 u8 input;
721 if (chain->selector == NULL ||
722 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
723 *(int *)arg = 0;
724 break;
727 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
728 chain->selector->id, chain->dev->intfnum,
729 UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
730 if (ret < 0)
731 return ret;
733 *(int *)arg = input - 1;
734 break;
737 case VIDIOC_S_INPUT:
739 u32 input = *(u32 *)arg + 1;
741 if ((ret = uvc_acquire_privileges(handle)) < 0)
742 return ret;
744 if (chain->selector == NULL ||
745 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
746 if (input != 1)
747 return -EINVAL;
748 break;
751 if (input == 0 || input > chain->selector->bNrInPins)
752 return -EINVAL;
754 return uvc_query_ctrl(chain->dev, UVC_SET_CUR,
755 chain->selector->id, chain->dev->intfnum,
756 UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
759 /* Try, Get, Set & Enum format */
760 case VIDIOC_ENUM_FMT:
762 struct v4l2_fmtdesc *fmt = arg;
763 struct uvc_format *format;
764 enum v4l2_buf_type type = fmt->type;
765 __u32 index = fmt->index;
767 if (fmt->type != stream->type ||
768 fmt->index >= stream->nformats)
769 return -EINVAL;
771 memset(fmt, 0, sizeof(*fmt));
772 fmt->index = index;
773 fmt->type = type;
775 format = &stream->format[fmt->index];
776 fmt->flags = 0;
777 if (format->flags & UVC_FMT_FLAG_COMPRESSED)
778 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
779 strlcpy(fmt->description, format->name,
780 sizeof fmt->description);
781 fmt->description[sizeof fmt->description - 1] = 0;
782 fmt->pixelformat = format->fcc;
783 break;
786 case VIDIOC_TRY_FMT:
788 struct uvc_streaming_control probe;
790 return uvc_v4l2_try_format(stream, arg, &probe, NULL, NULL);
793 case VIDIOC_S_FMT:
794 if ((ret = uvc_acquire_privileges(handle)) < 0)
795 return ret;
797 return uvc_v4l2_set_format(stream, arg);
799 case VIDIOC_G_FMT:
800 return uvc_v4l2_get_format(stream, arg);
802 /* Frame size enumeration */
803 case VIDIOC_ENUM_FRAMESIZES:
805 struct v4l2_frmsizeenum *fsize = arg;
806 struct uvc_format *format = NULL;
807 struct uvc_frame *frame;
808 int i;
810 /* Look for the given pixel format */
811 for (i = 0; i < stream->nformats; i++) {
812 if (stream->format[i].fcc ==
813 fsize->pixel_format) {
814 format = &stream->format[i];
815 break;
818 if (format == NULL)
819 return -EINVAL;
821 if (fsize->index >= format->nframes)
822 return -EINVAL;
824 frame = &format->frame[fsize->index];
825 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
826 fsize->discrete.width = frame->wWidth;
827 fsize->discrete.height = frame->wHeight;
828 break;
831 /* Frame interval enumeration */
832 case VIDIOC_ENUM_FRAMEINTERVALS:
834 struct v4l2_frmivalenum *fival = arg;
835 struct uvc_format *format = NULL;
836 struct uvc_frame *frame = NULL;
837 int i;
839 /* Look for the given pixel format and frame size */
840 for (i = 0; i < stream->nformats; i++) {
841 if (stream->format[i].fcc ==
842 fival->pixel_format) {
843 format = &stream->format[i];
844 break;
847 if (format == NULL)
848 return -EINVAL;
850 for (i = 0; i < format->nframes; i++) {
851 if (format->frame[i].wWidth == fival->width &&
852 format->frame[i].wHeight == fival->height) {
853 frame = &format->frame[i];
854 break;
857 if (frame == NULL)
858 return -EINVAL;
860 if (frame->bFrameIntervalType) {
861 if (fival->index >= frame->bFrameIntervalType)
862 return -EINVAL;
864 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
865 fival->discrete.numerator =
866 frame->dwFrameInterval[fival->index];
867 fival->discrete.denominator = 10000000;
868 uvc_simplify_fraction(&fival->discrete.numerator,
869 &fival->discrete.denominator, 8, 333);
870 } else {
871 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
872 fival->stepwise.min.numerator =
873 frame->dwFrameInterval[0];
874 fival->stepwise.min.denominator = 10000000;
875 fival->stepwise.max.numerator =
876 frame->dwFrameInterval[1];
877 fival->stepwise.max.denominator = 10000000;
878 fival->stepwise.step.numerator =
879 frame->dwFrameInterval[2];
880 fival->stepwise.step.denominator = 10000000;
881 uvc_simplify_fraction(&fival->stepwise.min.numerator,
882 &fival->stepwise.min.denominator, 8, 333);
883 uvc_simplify_fraction(&fival->stepwise.max.numerator,
884 &fival->stepwise.max.denominator, 8, 333);
885 uvc_simplify_fraction(&fival->stepwise.step.numerator,
886 &fival->stepwise.step.denominator, 8, 333);
888 break;
891 /* Get & Set streaming parameters */
892 case VIDIOC_G_PARM:
893 return uvc_v4l2_get_streamparm(stream, arg);
895 case VIDIOC_S_PARM:
896 if ((ret = uvc_acquire_privileges(handle)) < 0)
897 return ret;
899 return uvc_v4l2_set_streamparm(stream, arg);
901 /* Cropping and scaling */
902 case VIDIOC_CROPCAP:
904 struct v4l2_cropcap *ccap = arg;
905 struct uvc_frame *frame = stream->cur_frame;
907 if (ccap->type != stream->type)
908 return -EINVAL;
910 ccap->bounds.left = 0;
911 ccap->bounds.top = 0;
912 ccap->bounds.width = frame->wWidth;
913 ccap->bounds.height = frame->wHeight;
915 ccap->defrect = ccap->bounds;
917 ccap->pixelaspect.numerator = 1;
918 ccap->pixelaspect.denominator = 1;
919 break;
922 case VIDIOC_G_CROP:
923 case VIDIOC_S_CROP:
924 return -EINVAL;
926 /* Buffers & streaming */
927 case VIDIOC_REQBUFS:
929 struct v4l2_requestbuffers *rb = arg;
930 unsigned int bufsize =
931 stream->ctrl.dwMaxVideoFrameSize;
933 if (rb->type != stream->type ||
934 rb->memory != V4L2_MEMORY_MMAP)
935 return -EINVAL;
937 if ((ret = uvc_acquire_privileges(handle)) < 0)
938 return ret;
940 ret = uvc_alloc_buffers(&stream->queue, rb->count, bufsize);
941 if (ret < 0)
942 return ret;
944 if (ret == 0)
945 uvc_dismiss_privileges(handle);
947 rb->count = ret;
948 ret = 0;
949 break;
952 case VIDIOC_QUERYBUF:
954 struct v4l2_buffer *buf = arg;
956 if (buf->type != stream->type)
957 return -EINVAL;
959 if (!uvc_has_privileges(handle))
960 return -EBUSY;
962 return uvc_query_buffer(&stream->queue, buf);
965 case VIDIOC_QBUF:
966 if (!uvc_has_privileges(handle))
967 return -EBUSY;
969 return uvc_queue_buffer(&stream->queue, arg);
971 case VIDIOC_DQBUF:
972 if (!uvc_has_privileges(handle))
973 return -EBUSY;
975 return uvc_dequeue_buffer(&stream->queue, arg,
976 file->f_flags & O_NONBLOCK);
978 case VIDIOC_STREAMON:
980 int *type = arg;
982 if (*type != stream->type)
983 return -EINVAL;
985 if (!uvc_has_privileges(handle))
986 return -EBUSY;
988 ret = uvc_video_enable(stream, 1);
989 if (ret < 0)
990 return ret;
991 break;
994 case VIDIOC_STREAMOFF:
996 int *type = arg;
998 if (*type != stream->type)
999 return -EINVAL;
1001 if (!uvc_has_privileges(handle))
1002 return -EBUSY;
1004 return uvc_video_enable(stream, 0);
1007 /* Analog video standards make no sense for digital cameras. */
1008 case VIDIOC_ENUMSTD:
1009 case VIDIOC_QUERYSTD:
1010 case VIDIOC_G_STD:
1011 case VIDIOC_S_STD:
1013 case VIDIOC_OVERLAY:
1015 case VIDIOC_ENUMAUDIO:
1016 case VIDIOC_ENUMAUDOUT:
1018 case VIDIOC_ENUMOUTPUT:
1019 uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd);
1020 return -EINVAL;
1022 /* Dynamic controls. */
1023 case UVCIOC_CTRL_ADD:
1025 struct uvc_xu_control_info *xinfo = arg;
1026 struct uvc_control_info *info;
1028 if (!capable(CAP_SYS_ADMIN))
1029 return -EPERM;
1031 if (xinfo->size == 0)
1032 return -EINVAL;
1034 info = kzalloc(sizeof *info, GFP_KERNEL);
1035 if (info == NULL)
1036 return -ENOMEM;
1038 memcpy(info->entity, xinfo->entity, sizeof info->entity);
1039 info->index = xinfo->index;
1040 info->selector = xinfo->selector;
1041 info->size = xinfo->size;
1042 info->flags = xinfo->flags;
1044 info->flags |= UVC_CONTROL_GET_MIN | UVC_CONTROL_GET_MAX |
1045 UVC_CONTROL_GET_RES | UVC_CONTROL_GET_DEF |
1046 UVC_CONTROL_EXTENSION;
1048 ret = uvc_ctrl_add_info(info);
1049 if (ret < 0)
1050 kfree(info);
1051 break;
1054 case UVCIOC_CTRL_MAP_OLD:
1055 case UVCIOC_CTRL_MAP:
1056 if (!capable(CAP_SYS_ADMIN))
1057 return -EPERM;
1059 return uvc_ioctl_ctrl_map(arg, cmd == UVCIOC_CTRL_MAP_OLD);
1061 case UVCIOC_CTRL_GET:
1062 return uvc_xu_ctrl_query(chain, arg, 0);
1064 case UVCIOC_CTRL_SET:
1065 return uvc_xu_ctrl_query(chain, arg, 1);
1067 default:
1068 if ((ret = v4l_compat_translate_ioctl(file, cmd, arg,
1069 uvc_v4l2_do_ioctl)) == -ENOIOCTLCMD)
1070 uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n",
1071 cmd);
1072 return ret;
1075 return ret;
1078 static long uvc_v4l2_ioctl(struct file *file,
1079 unsigned int cmd, unsigned long arg)
1081 if (uvc_trace_param & UVC_TRACE_IOCTL) {
1082 uvc_printk(KERN_DEBUG, "uvc_v4l2_ioctl(");
1083 v4l_printk_ioctl(cmd);
1084 printk(")\n");
1087 return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl);
1090 static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1091 size_t count, loff_t *ppos)
1093 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
1094 return -EINVAL;
1098 * VMA operations.
1100 static void uvc_vm_open(struct vm_area_struct *vma)
1102 struct uvc_buffer *buffer = vma->vm_private_data;
1103 buffer->vma_use_count++;
1106 static void uvc_vm_close(struct vm_area_struct *vma)
1108 struct uvc_buffer *buffer = vma->vm_private_data;
1109 buffer->vma_use_count--;
1112 static struct vm_operations_struct uvc_vm_ops = {
1113 .open = uvc_vm_open,
1114 .close = uvc_vm_close,
1117 static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1119 struct uvc_fh *handle = file->private_data;
1120 struct uvc_streaming *stream = handle->stream;
1121 struct uvc_video_queue *queue = &stream->queue;
1122 struct uvc_buffer *uninitialized_var(buffer);
1123 struct page *page;
1124 unsigned long addr, start, size;
1125 unsigned int i;
1126 int ret = 0;
1128 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
1130 start = vma->vm_start;
1131 size = vma->vm_end - vma->vm_start;
1133 mutex_lock(&queue->mutex);
1135 for (i = 0; i < queue->count; ++i) {
1136 buffer = &queue->buffer[i];
1137 if ((buffer->buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
1138 break;
1141 if (i == queue->count || size != queue->buf_size) {
1142 ret = -EINVAL;
1143 goto done;
1147 * VM_IO marks the area as being an mmaped region for I/O to a
1148 * device. It also prevents the region from being core dumped.
1150 vma->vm_flags |= VM_IO;
1152 addr = (unsigned long)queue->mem + buffer->buf.m.offset;
1153 while (size > 0) {
1154 page = vmalloc_to_page((void *)addr);
1155 if ((ret = vm_insert_page(vma, start, page)) < 0)
1156 goto done;
1158 start += PAGE_SIZE;
1159 addr += PAGE_SIZE;
1160 size -= PAGE_SIZE;
1163 vma->vm_ops = &uvc_vm_ops;
1164 vma->vm_private_data = buffer;
1165 uvc_vm_open(vma);
1167 done:
1168 mutex_unlock(&queue->mutex);
1169 return ret;
1172 static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
1174 struct uvc_fh *handle = file->private_data;
1175 struct uvc_streaming *stream = handle->stream;
1177 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
1179 return uvc_queue_poll(&stream->queue, file, wait);
1182 const struct v4l2_file_operations uvc_fops = {
1183 .owner = THIS_MODULE,
1184 .open = uvc_v4l2_open,
1185 .release = uvc_v4l2_release,
1186 .ioctl = uvc_v4l2_ioctl,
1187 .read = uvc_v4l2_read,
1188 .mmap = uvc_v4l2_mmap,
1189 .poll = uvc_v4l2_poll,