Merge tag 'pinctrl-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw...
[cascardo/linux.git] / drivers / media / usb / uvc / uvc_v4l2.c
1 /*
2  *      uvc_v4l2.c  --  USB Video Class driver - V4L2 API
3  *
4  *      Copyright (C) 2005-2010
5  *          Laurent Pinchart (laurent.pinchart@ideasonboard.com)
6  *
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.
11  *
12  */
13
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>
26
27 #include <media/v4l2-common.h>
28 #include <media/v4l2-ctrls.h>
29 #include <media/v4l2-event.h>
30 #include <media/v4l2-ioctl.h>
31
32 #include "uvcvideo.h"
33
34 /* ------------------------------------------------------------------------
35  * UVC ioctls
36  */
37 static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain,
38         struct uvc_xu_control_mapping *xmap)
39 {
40         struct uvc_control_mapping *map;
41         unsigned int size;
42         int ret;
43
44         map = kzalloc(sizeof *map, GFP_KERNEL);
45         if (map == NULL)
46                 return -ENOMEM;
47
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;
56
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;
62
63         case V4L2_CTRL_TYPE_MENU:
64                 /* Prevent excessive memory consumption, as well as integer
65                  * overflows.
66                  */
67                 if (xmap->menu_count == 0 ||
68                     xmap->menu_count > UVC_MAX_CONTROL_MENU_ENTRIES) {
69                         ret = -EINVAL;
70                         goto done;
71                 }
72
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;
78                 }
79
80                 if (copy_from_user(map->menu_info, xmap->menu_info, size)) {
81                         ret = -EFAULT;
82                         goto done;
83                 }
84
85                 map->menu_count = xmap->menu_count;
86                 break;
87
88         default:
89                 uvc_trace(UVC_TRACE_CONTROL, "Unsupported V4L2 control type "
90                           "%u.\n", xmap->v4l2_type);
91                 ret = -ENOTTY;
92                 goto done;
93         }
94
95         ret = uvc_ctrl_add_mapping(chain, map);
96
97 done:
98         kfree(map->menu_info);
99         kfree(map);
100
101         return ret;
102 }
103
104 /* ------------------------------------------------------------------------
105  * V4L2 interface
106  */
107
108 /*
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.
113  */
114 static __u32 uvc_try_frame_interval(struct uvc_frame *frame, __u32 interval)
115 {
116         unsigned int i;
117
118         if (frame->bFrameIntervalType) {
119                 __u32 best = -1, dist;
120
121                 for (i = 0; i < frame->bFrameIntervalType; ++i) {
122                         dist = interval > frame->dwFrameInterval[i]
123                              ? interval - frame->dwFrameInterval[i]
124                              : frame->dwFrameInterval[i] - interval;
125
126                         if (dist > best)
127                                 break;
128
129                         best = dist;
130                 }
131
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];
137
138                 interval = min + (interval - min + step/2) / step * step;
139                 if (interval > max)
140                         interval = max;
141         }
142
143         return interval;
144 }
145
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)
149 {
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;
158
159         if (fmt->type != stream->type)
160                 return -EINVAL;
161
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);
167
168         /* Check if the hardware supports the requested format, use the default
169          * format otherwise.
170          */
171         for (i = 0; i < stream->nformats; ++i) {
172                 format = &stream->format[i];
173                 if (format->fcc == fmt->fmt.pix.pixelformat)
174                         break;
175         }
176
177         if (i == stream->nformats) {
178                 format = stream->def_format;
179                 fmt->fmt.pix.pixelformat = format->fcc;
180         }
181
182         /* Find the closest image size. The distance between image sizes is
183          * the size in pixels of the non-overlapping regions between the
184          * requested size and the frame-specified size.
185          */
186         rw = fmt->fmt.pix.width;
187         rh = fmt->fmt.pix.height;
188         maxd = (unsigned int)-1;
189
190         for (i = 0; i < format->nframes; ++i) {
191                 __u16 w = format->frame[i].wWidth;
192                 __u16 h = format->frame[i].wHeight;
193
194                 d = min(w, rw) * min(h, rh);
195                 d = w*h + rw*rh - 2*d;
196                 if (d < maxd) {
197                         maxd = d;
198                         frame = &format->frame[i];
199                 }
200
201                 if (maxd == 0)
202                         break;
203         }
204
205         if (frame == NULL) {
206                 uvc_trace(UVC_TRACE_FORMAT, "Unsupported size %ux%u.\n",
207                                 fmt->fmt.pix.width, fmt->fmt.pix.height);
208                 return -EINVAL;
209         }
210
211         /* Use the default frame interval. */
212         interval = frame->dwDefaultFrameInterval;
213         uvc_trace(UVC_TRACE_FORMAT, "Using default frame interval %u.%u us "
214                 "(%u.%u fps).\n", interval/10, interval%10, 10000000/interval,
215                 (100000000/interval)%10);
216
217         /* Set the format index, frame index and frame interval. */
218         memset(probe, 0, sizeof *probe);
219         probe->bmHint = 1;      /* dwFrameInterval */
220         probe->bFormatIndex = format->index;
221         probe->bFrameIndex = frame->bFrameIndex;
222         probe->dwFrameInterval = uvc_try_frame_interval(frame, interval);
223         /* Some webcams stall the probe control set request when the
224          * dwMaxVideoFrameSize field is set to zero. The UVC specification
225          * clearly states that the field is read-only from the host, so this
226          * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by
227          * the webcam to work around the problem.
228          *
229          * The workaround could probably be enabled for all webcams, so the
230          * quirk can be removed if needed. It's currently useful to detect
231          * webcam bugs and fix them before they hit the market (providing
232          * developers test their webcams with the Linux driver as well as with
233          * the Windows driver).
234          */
235         mutex_lock(&stream->mutex);
236         if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS)
237                 probe->dwMaxVideoFrameSize =
238                         stream->ctrl.dwMaxVideoFrameSize;
239
240         /* Probe the device. */
241         ret = uvc_probe_video(stream, probe);
242         mutex_unlock(&stream->mutex);
243         if (ret < 0)
244                 goto done;
245
246         fmt->fmt.pix.width = frame->wWidth;
247         fmt->fmt.pix.height = frame->wHeight;
248         fmt->fmt.pix.field = V4L2_FIELD_NONE;
249         fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
250         fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize;
251         fmt->fmt.pix.colorspace = format->colorspace;
252         fmt->fmt.pix.priv = 0;
253
254         if (uvc_format != NULL)
255                 *uvc_format = format;
256         if (uvc_frame != NULL)
257                 *uvc_frame = frame;
258
259 done:
260         return ret;
261 }
262
263 static int uvc_v4l2_get_format(struct uvc_streaming *stream,
264         struct v4l2_format *fmt)
265 {
266         struct uvc_format *format;
267         struct uvc_frame *frame;
268         int ret = 0;
269
270         if (fmt->type != stream->type)
271                 return -EINVAL;
272
273         mutex_lock(&stream->mutex);
274         format = stream->cur_format;
275         frame = stream->cur_frame;
276
277         if (format == NULL || frame == NULL) {
278                 ret = -EINVAL;
279                 goto done;
280         }
281
282         fmt->fmt.pix.pixelformat = format->fcc;
283         fmt->fmt.pix.width = frame->wWidth;
284         fmt->fmt.pix.height = frame->wHeight;
285         fmt->fmt.pix.field = V4L2_FIELD_NONE;
286         fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
287         fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize;
288         fmt->fmt.pix.colorspace = format->colorspace;
289         fmt->fmt.pix.priv = 0;
290
291 done:
292         mutex_unlock(&stream->mutex);
293         return ret;
294 }
295
296 static int uvc_v4l2_set_format(struct uvc_streaming *stream,
297         struct v4l2_format *fmt)
298 {
299         struct uvc_streaming_control probe;
300         struct uvc_format *format;
301         struct uvc_frame *frame;
302         int ret;
303
304         if (fmt->type != stream->type)
305                 return -EINVAL;
306
307         ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame);
308         if (ret < 0)
309                 return ret;
310
311         mutex_lock(&stream->mutex);
312
313         if (uvc_queue_allocated(&stream->queue)) {
314                 ret = -EBUSY;
315                 goto done;
316         }
317
318         memcpy(&stream->ctrl, &probe, sizeof probe);
319         stream->cur_format = format;
320         stream->cur_frame = frame;
321
322 done:
323         mutex_unlock(&stream->mutex);
324         return ret;
325 }
326
327 static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream,
328                 struct v4l2_streamparm *parm)
329 {
330         uint32_t numerator, denominator;
331
332         if (parm->type != stream->type)
333                 return -EINVAL;
334
335         mutex_lock(&stream->mutex);
336         numerator = stream->ctrl.dwFrameInterval;
337         mutex_unlock(&stream->mutex);
338
339         denominator = 10000000;
340         uvc_simplify_fraction(&numerator, &denominator, 8, 333);
341
342         memset(parm, 0, sizeof *parm);
343         parm->type = stream->type;
344
345         if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
346                 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
347                 parm->parm.capture.capturemode = 0;
348                 parm->parm.capture.timeperframe.numerator = numerator;
349                 parm->parm.capture.timeperframe.denominator = denominator;
350                 parm->parm.capture.extendedmode = 0;
351                 parm->parm.capture.readbuffers = 0;
352         } else {
353                 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
354                 parm->parm.output.outputmode = 0;
355                 parm->parm.output.timeperframe.numerator = numerator;
356                 parm->parm.output.timeperframe.denominator = denominator;
357         }
358
359         return 0;
360 }
361
362 static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
363                 struct v4l2_streamparm *parm)
364 {
365         struct uvc_streaming_control probe;
366         struct v4l2_fract timeperframe;
367         uint32_t interval;
368         int ret;
369
370         if (parm->type != stream->type)
371                 return -EINVAL;
372
373         if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
374                 timeperframe = parm->parm.capture.timeperframe;
375         else
376                 timeperframe = parm->parm.output.timeperframe;
377
378         interval = uvc_fraction_to_interval(timeperframe.numerator,
379                 timeperframe.denominator);
380         uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
381                 timeperframe.numerator, timeperframe.denominator, interval);
382
383         mutex_lock(&stream->mutex);
384
385         if (uvc_queue_streaming(&stream->queue)) {
386                 mutex_unlock(&stream->mutex);
387                 return -EBUSY;
388         }
389
390         memcpy(&probe, &stream->ctrl, sizeof probe);
391         probe.dwFrameInterval =
392                 uvc_try_frame_interval(stream->cur_frame, interval);
393
394         /* Probe the device with the new settings. */
395         ret = uvc_probe_video(stream, &probe);
396         if (ret < 0) {
397                 mutex_unlock(&stream->mutex);
398                 return ret;
399         }
400
401         memcpy(&stream->ctrl, &probe, sizeof probe);
402         mutex_unlock(&stream->mutex);
403
404         /* Return the actual frame period. */
405         timeperframe.numerator = probe.dwFrameInterval;
406         timeperframe.denominator = 10000000;
407         uvc_simplify_fraction(&timeperframe.numerator,
408                 &timeperframe.denominator, 8, 333);
409
410         if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
411                 parm->parm.capture.timeperframe = timeperframe;
412         else
413                 parm->parm.output.timeperframe = timeperframe;
414
415         return 0;
416 }
417
418 /* ------------------------------------------------------------------------
419  * Privilege management
420  */
421
422 /*
423  * Privilege management is the multiple-open implementation basis. The current
424  * implementation is completely transparent for the end-user and doesn't
425  * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls.
426  * Those ioctls enable finer control on the device (by making possible for a
427  * user to request exclusive access to a device), but are not mature yet.
428  * Switching to the V4L2 priority mechanism might be considered in the future
429  * if this situation changes.
430  *
431  * Each open instance of a UVC device can either be in a privileged or
432  * unprivileged state. Only a single instance can be in a privileged state at
433  * a given time. Trying to perform an operation that requires privileges will
434  * automatically acquire the required privileges if possible, or return -EBUSY
435  * otherwise. Privileges are dismissed when closing the instance or when
436  * freeing the video buffers using VIDIOC_REQBUFS.
437  *
438  * Operations that require privileges are:
439  *
440  * - VIDIOC_S_INPUT
441  * - VIDIOC_S_PARM
442  * - VIDIOC_S_FMT
443  * - VIDIOC_REQBUFS
444  */
445 static int uvc_acquire_privileges(struct uvc_fh *handle)
446 {
447         /* Always succeed if the handle is already privileged. */
448         if (handle->state == UVC_HANDLE_ACTIVE)
449                 return 0;
450
451         /* Check if the device already has a privileged handle. */
452         if (atomic_inc_return(&handle->stream->active) != 1) {
453                 atomic_dec(&handle->stream->active);
454                 return -EBUSY;
455         }
456
457         handle->state = UVC_HANDLE_ACTIVE;
458         return 0;
459 }
460
461 static void uvc_dismiss_privileges(struct uvc_fh *handle)
462 {
463         if (handle->state == UVC_HANDLE_ACTIVE)
464                 atomic_dec(&handle->stream->active);
465
466         handle->state = UVC_HANDLE_PASSIVE;
467 }
468
469 static int uvc_has_privileges(struct uvc_fh *handle)
470 {
471         return handle->state == UVC_HANDLE_ACTIVE;
472 }
473
474 /* ------------------------------------------------------------------------
475  * V4L2 file operations
476  */
477
478 static int uvc_v4l2_open(struct file *file)
479 {
480         struct uvc_streaming *stream;
481         struct uvc_fh *handle;
482         int ret = 0;
483
484         uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
485         stream = video_drvdata(file);
486
487         if (stream->dev->state & UVC_DEV_DISCONNECTED)
488                 return -ENODEV;
489
490         ret = usb_autopm_get_interface(stream->dev->intf);
491         if (ret < 0)
492                 return ret;
493
494         /* Create the device handle. */
495         handle = kzalloc(sizeof *handle, GFP_KERNEL);
496         if (handle == NULL) {
497                 usb_autopm_put_interface(stream->dev->intf);
498                 return -ENOMEM;
499         }
500
501         if (atomic_inc_return(&stream->dev->users) == 1) {
502                 ret = uvc_status_start(stream->dev);
503                 if (ret < 0) {
504                         usb_autopm_put_interface(stream->dev->intf);
505                         atomic_dec(&stream->dev->users);
506                         kfree(handle);
507                         return ret;
508                 }
509         }
510
511         v4l2_fh_init(&handle->vfh, stream->vdev);
512         v4l2_fh_add(&handle->vfh);
513         handle->chain = stream->chain;
514         handle->stream = stream;
515         handle->state = UVC_HANDLE_PASSIVE;
516         file->private_data = handle;
517
518         return 0;
519 }
520
521 static int uvc_v4l2_release(struct file *file)
522 {
523         struct uvc_fh *handle = file->private_data;
524         struct uvc_streaming *stream = handle->stream;
525
526         uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");
527
528         /* Only free resources if this is a privileged handle. */
529         if (uvc_has_privileges(handle)) {
530                 uvc_video_enable(stream, 0);
531                 uvc_free_buffers(&stream->queue);
532         }
533
534         /* Release the file handle. */
535         uvc_dismiss_privileges(handle);
536         v4l2_fh_del(&handle->vfh);
537         v4l2_fh_exit(&handle->vfh);
538         kfree(handle);
539         file->private_data = NULL;
540
541         if (atomic_dec_return(&stream->dev->users) == 0)
542                 uvc_status_stop(stream->dev);
543
544         usb_autopm_put_interface(stream->dev->intf);
545         return 0;
546 }
547
548 static long uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
549 {
550         struct video_device *vdev = video_devdata(file);
551         struct uvc_fh *handle = file->private_data;
552         struct uvc_video_chain *chain = handle->chain;
553         struct uvc_streaming *stream = handle->stream;
554         long ret = 0;
555
556         switch (cmd) {
557         /* Query capabilities */
558         case VIDIOC_QUERYCAP:
559         {
560                 struct v4l2_capability *cap = arg;
561
562                 memset(cap, 0, sizeof *cap);
563                 strlcpy(cap->driver, "uvcvideo", sizeof cap->driver);
564                 strlcpy(cap->card, vdev->name, sizeof cap->card);
565                 usb_make_path(stream->dev->udev,
566                               cap->bus_info, sizeof(cap->bus_info));
567                 cap->version = LINUX_VERSION_CODE;
568                 cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
569                                   | chain->caps;
570                 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
571                         cap->device_caps = V4L2_CAP_VIDEO_CAPTURE
572                                          | V4L2_CAP_STREAMING;
573                 else
574                         cap->device_caps = V4L2_CAP_VIDEO_OUTPUT
575                                          | V4L2_CAP_STREAMING;
576                 break;
577         }
578
579         /* Priority */
580         case VIDIOC_G_PRIORITY:
581                 *(u32 *)arg = v4l2_prio_max(vdev->prio);
582                 break;
583
584         case VIDIOC_S_PRIORITY:
585                 ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
586                 if (ret < 0)
587                         return ret;
588
589                 return v4l2_prio_change(vdev->prio, &handle->vfh.prio,
590                                         *(u32 *)arg);
591
592         /* Get, Set & Query control */
593         case VIDIOC_QUERYCTRL:
594                 return uvc_query_v4l2_ctrl(chain, arg);
595
596         case VIDIOC_G_CTRL:
597         {
598                 struct v4l2_control *ctrl = arg;
599                 struct v4l2_ext_control xctrl;
600
601                 memset(&xctrl, 0, sizeof xctrl);
602                 xctrl.id = ctrl->id;
603
604                 ret = uvc_ctrl_begin(chain);
605                 if (ret < 0)
606                         return ret;
607
608                 ret = uvc_ctrl_get(chain, &xctrl);
609                 uvc_ctrl_rollback(handle);
610                 if (ret >= 0)
611                         ctrl->value = xctrl.value;
612                 break;
613         }
614
615         case VIDIOC_S_CTRL:
616         {
617                 struct v4l2_control *ctrl = arg;
618                 struct v4l2_ext_control xctrl;
619
620                 ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
621                 if (ret < 0)
622                         return ret;
623
624                 memset(&xctrl, 0, sizeof xctrl);
625                 xctrl.id = ctrl->id;
626                 xctrl.value = ctrl->value;
627
628                 ret = uvc_ctrl_begin(chain);
629                 if (ret < 0)
630                         return ret;
631
632                 ret = uvc_ctrl_set(chain, &xctrl);
633                 if (ret < 0) {
634                         uvc_ctrl_rollback(handle);
635                         return ret;
636                 }
637                 ret = uvc_ctrl_commit(handle, &xctrl, 1);
638                 if (ret == 0)
639                         ctrl->value = xctrl.value;
640                 break;
641         }
642
643         case VIDIOC_QUERYMENU:
644                 return uvc_query_v4l2_menu(chain, arg);
645
646         case VIDIOC_G_EXT_CTRLS:
647         {
648                 struct v4l2_ext_controls *ctrls = arg;
649                 struct v4l2_ext_control *ctrl = ctrls->controls;
650                 unsigned int i;
651
652                 ret = uvc_ctrl_begin(chain);
653                 if (ret < 0)
654                         return ret;
655
656                 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
657                         ret = uvc_ctrl_get(chain, ctrl);
658                         if (ret < 0) {
659                                 uvc_ctrl_rollback(handle);
660                                 ctrls->error_idx = ret == -ENOENT
661                                                  ? ctrls->count : i;
662                                 return ret;
663                         }
664                 }
665                 ctrls->error_idx = 0;
666                 ret = uvc_ctrl_rollback(handle);
667                 break;
668         }
669
670         case VIDIOC_S_EXT_CTRLS:
671                 ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
672                 if (ret < 0)
673                         return ret;
674                 /* Fall through */
675         case VIDIOC_TRY_EXT_CTRLS:
676         {
677                 struct v4l2_ext_controls *ctrls = arg;
678                 struct v4l2_ext_control *ctrl = ctrls->controls;
679                 unsigned int i;
680
681                 ret = uvc_ctrl_begin(chain);
682                 if (ret < 0)
683                         return ret;
684
685                 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
686                         ret = uvc_ctrl_set(chain, ctrl);
687                         if (ret < 0) {
688                                 uvc_ctrl_rollback(handle);
689                                 ctrls->error_idx = (ret == -ENOENT &&
690                                                     cmd == VIDIOC_S_EXT_CTRLS)
691                                                  ? ctrls->count : i;
692                                 return ret;
693                         }
694                 }
695
696                 ctrls->error_idx = 0;
697
698                 if (cmd == VIDIOC_S_EXT_CTRLS)
699                         ret = uvc_ctrl_commit(handle,
700                                               ctrls->controls, ctrls->count);
701                 else
702                         ret = uvc_ctrl_rollback(handle);
703                 break;
704         }
705
706         /* Get, Set & Enum input */
707         case VIDIOC_ENUMINPUT:
708         {
709                 const struct uvc_entity *selector = chain->selector;
710                 struct v4l2_input *input = arg;
711                 struct uvc_entity *iterm = NULL;
712                 u32 index = input->index;
713                 int pin = 0;
714
715                 if (selector == NULL ||
716                     (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
717                         if (index != 0)
718                                 return -EINVAL;
719                         list_for_each_entry(iterm, &chain->entities, chain) {
720                                 if (UVC_ENTITY_IS_ITERM(iterm))
721                                         break;
722                         }
723                         pin = iterm->id;
724                 } else if (index < selector->bNrInPins) {
725                         pin = selector->baSourceID[index];
726                         list_for_each_entry(iterm, &chain->entities, chain) {
727                                 if (!UVC_ENTITY_IS_ITERM(iterm))
728                                         continue;
729                                 if (iterm->id == pin)
730                                         break;
731                         }
732                 }
733
734                 if (iterm == NULL || iterm->id != pin)
735                         return -EINVAL;
736
737                 memset(input, 0, sizeof *input);
738                 input->index = index;
739                 strlcpy(input->name, iterm->name, sizeof input->name);
740                 if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
741                         input->type = V4L2_INPUT_TYPE_CAMERA;
742                 break;
743         }
744
745         case VIDIOC_G_INPUT:
746         {
747                 u8 input;
748
749                 if (chain->selector == NULL ||
750                     (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
751                         *(int *)arg = 0;
752                         break;
753                 }
754
755                 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
756                         chain->selector->id, chain->dev->intfnum,
757                         UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
758                 if (ret < 0)
759                         return ret;
760
761                 *(int *)arg = input - 1;
762                 break;
763         }
764
765         case VIDIOC_S_INPUT:
766         {
767                 u32 input = *(u32 *)arg + 1;
768
769                 ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
770                 if (ret < 0)
771                         return ret;
772
773                 if ((ret = uvc_acquire_privileges(handle)) < 0)
774                         return ret;
775
776                 if (chain->selector == NULL ||
777                     (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
778                         if (input != 1)
779                                 return -EINVAL;
780                         break;
781                 }
782
783                 if (input == 0 || input > chain->selector->bNrInPins)
784                         return -EINVAL;
785
786                 return uvc_query_ctrl(chain->dev, UVC_SET_CUR,
787                         chain->selector->id, chain->dev->intfnum,
788                         UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
789         }
790
791         /* Try, Get, Set & Enum format */
792         case VIDIOC_ENUM_FMT:
793         {
794                 struct v4l2_fmtdesc *fmt = arg;
795                 struct uvc_format *format;
796                 enum v4l2_buf_type type = fmt->type;
797                 __u32 index = fmt->index;
798
799                 if (fmt->type != stream->type ||
800                     fmt->index >= stream->nformats)
801                         return -EINVAL;
802
803                 memset(fmt, 0, sizeof(*fmt));
804                 fmt->index = index;
805                 fmt->type = type;
806
807                 format = &stream->format[fmt->index];
808                 fmt->flags = 0;
809                 if (format->flags & UVC_FMT_FLAG_COMPRESSED)
810                         fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
811                 strlcpy(fmt->description, format->name,
812                         sizeof fmt->description);
813                 fmt->description[sizeof fmt->description - 1] = 0;
814                 fmt->pixelformat = format->fcc;
815                 break;
816         }
817
818         case VIDIOC_TRY_FMT:
819         {
820                 struct uvc_streaming_control probe;
821
822                 return uvc_v4l2_try_format(stream, arg, &probe, NULL, NULL);
823         }
824
825         case VIDIOC_S_FMT:
826                 ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
827                 if (ret < 0)
828                         return ret;
829
830                 if ((ret = uvc_acquire_privileges(handle)) < 0)
831                         return ret;
832
833                 return uvc_v4l2_set_format(stream, arg);
834
835         case VIDIOC_G_FMT:
836                 return uvc_v4l2_get_format(stream, arg);
837
838         /* Frame size enumeration */
839         case VIDIOC_ENUM_FRAMESIZES:
840         {
841                 struct v4l2_frmsizeenum *fsize = arg;
842                 struct uvc_format *format = NULL;
843                 struct uvc_frame *frame;
844                 int i;
845
846                 /* Look for the given pixel format */
847                 for (i = 0; i < stream->nformats; i++) {
848                         if (stream->format[i].fcc ==
849                                         fsize->pixel_format) {
850                                 format = &stream->format[i];
851                                 break;
852                         }
853                 }
854                 if (format == NULL)
855                         return -EINVAL;
856
857                 if (fsize->index >= format->nframes)
858                         return -EINVAL;
859
860                 frame = &format->frame[fsize->index];
861                 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
862                 fsize->discrete.width = frame->wWidth;
863                 fsize->discrete.height = frame->wHeight;
864                 break;
865         }
866
867         /* Frame interval enumeration */
868         case VIDIOC_ENUM_FRAMEINTERVALS:
869         {
870                 struct v4l2_frmivalenum *fival = arg;
871                 struct uvc_format *format = NULL;
872                 struct uvc_frame *frame = NULL;
873                 int i;
874
875                 /* Look for the given pixel format and frame size */
876                 for (i = 0; i < stream->nformats; i++) {
877                         if (stream->format[i].fcc ==
878                                         fival->pixel_format) {
879                                 format = &stream->format[i];
880                                 break;
881                         }
882                 }
883                 if (format == NULL)
884                         return -EINVAL;
885
886                 for (i = 0; i < format->nframes; i++) {
887                         if (format->frame[i].wWidth == fival->width &&
888                             format->frame[i].wHeight == fival->height) {
889                                 frame = &format->frame[i];
890                                 break;
891                         }
892                 }
893                 if (frame == NULL)
894                         return -EINVAL;
895
896                 if (frame->bFrameIntervalType) {
897                         if (fival->index >= frame->bFrameIntervalType)
898                                 return -EINVAL;
899
900                         fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
901                         fival->discrete.numerator =
902                                 frame->dwFrameInterval[fival->index];
903                         fival->discrete.denominator = 10000000;
904                         uvc_simplify_fraction(&fival->discrete.numerator,
905                                 &fival->discrete.denominator, 8, 333);
906                 } else {
907                         fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
908                         fival->stepwise.min.numerator =
909                                 frame->dwFrameInterval[0];
910                         fival->stepwise.min.denominator = 10000000;
911                         fival->stepwise.max.numerator =
912                                 frame->dwFrameInterval[1];
913                         fival->stepwise.max.denominator = 10000000;
914                         fival->stepwise.step.numerator =
915                                 frame->dwFrameInterval[2];
916                         fival->stepwise.step.denominator = 10000000;
917                         uvc_simplify_fraction(&fival->stepwise.min.numerator,
918                                 &fival->stepwise.min.denominator, 8, 333);
919                         uvc_simplify_fraction(&fival->stepwise.max.numerator,
920                                 &fival->stepwise.max.denominator, 8, 333);
921                         uvc_simplify_fraction(&fival->stepwise.step.numerator,
922                                 &fival->stepwise.step.denominator, 8, 333);
923                 }
924                 break;
925         }
926
927         /* Get & Set streaming parameters */
928         case VIDIOC_G_PARM:
929                 return uvc_v4l2_get_streamparm(stream, arg);
930
931         case VIDIOC_S_PARM:
932                 ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
933                 if (ret < 0)
934                         return ret;
935
936                 if ((ret = uvc_acquire_privileges(handle)) < 0)
937                         return ret;
938
939                 return uvc_v4l2_set_streamparm(stream, arg);
940
941         /* Cropping and scaling */
942         case VIDIOC_CROPCAP:
943         {
944                 struct v4l2_cropcap *ccap = arg;
945
946                 if (ccap->type != stream->type)
947                         return -EINVAL;
948
949                 ccap->bounds.left = 0;
950                 ccap->bounds.top = 0;
951
952                 mutex_lock(&stream->mutex);
953                 ccap->bounds.width = stream->cur_frame->wWidth;
954                 ccap->bounds.height = stream->cur_frame->wHeight;
955                 mutex_unlock(&stream->mutex);
956
957                 ccap->defrect = ccap->bounds;
958
959                 ccap->pixelaspect.numerator = 1;
960                 ccap->pixelaspect.denominator = 1;
961                 break;
962         }
963
964         case VIDIOC_G_CROP:
965         case VIDIOC_S_CROP:
966                 return -ENOTTY;
967
968         /* Buffers & streaming */
969         case VIDIOC_REQBUFS:
970                 ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
971                 if (ret < 0)
972                         return ret;
973
974                 if ((ret = uvc_acquire_privileges(handle)) < 0)
975                         return ret;
976
977                 mutex_lock(&stream->mutex);
978                 ret = uvc_alloc_buffers(&stream->queue, arg);
979                 mutex_unlock(&stream->mutex);
980                 if (ret < 0)
981                         return ret;
982
983                 if (ret == 0)
984                         uvc_dismiss_privileges(handle);
985
986                 ret = 0;
987                 break;
988
989         case VIDIOC_QUERYBUF:
990         {
991                 struct v4l2_buffer *buf = arg;
992
993                 if (!uvc_has_privileges(handle))
994                         return -EBUSY;
995
996                 return uvc_query_buffer(&stream->queue, buf);
997         }
998
999         case VIDIOC_QBUF:
1000                 if (!uvc_has_privileges(handle))
1001                         return -EBUSY;
1002
1003                 return uvc_queue_buffer(&stream->queue, arg);
1004
1005         case VIDIOC_DQBUF:
1006                 if (!uvc_has_privileges(handle))
1007                         return -EBUSY;
1008
1009                 return uvc_dequeue_buffer(&stream->queue, arg,
1010                         file->f_flags & O_NONBLOCK);
1011
1012         case VIDIOC_STREAMON:
1013         {
1014                 int *type = arg;
1015
1016                 if (*type != stream->type)
1017                         return -EINVAL;
1018
1019                 ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
1020                 if (ret < 0)
1021                         return ret;
1022
1023                 if (!uvc_has_privileges(handle))
1024                         return -EBUSY;
1025
1026                 mutex_lock(&stream->mutex);
1027                 ret = uvc_video_enable(stream, 1);
1028                 mutex_unlock(&stream->mutex);
1029                 if (ret < 0)
1030                         return ret;
1031                 break;
1032         }
1033
1034         case VIDIOC_STREAMOFF:
1035         {
1036                 int *type = arg;
1037
1038                 if (*type != stream->type)
1039                         return -EINVAL;
1040
1041                 ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
1042                 if (ret < 0)
1043                         return ret;
1044
1045                 if (!uvc_has_privileges(handle))
1046                         return -EBUSY;
1047
1048                 return uvc_video_enable(stream, 0);
1049         }
1050
1051         case VIDIOC_SUBSCRIBE_EVENT:
1052         {
1053                 struct v4l2_event_subscription *sub = arg;
1054
1055                 switch (sub->type) {
1056                 case V4L2_EVENT_CTRL:
1057                         return v4l2_event_subscribe(&handle->vfh, sub, 0,
1058                                                     &uvc_ctrl_sub_ev_ops);
1059                 default:
1060                         return -EINVAL;
1061                 }
1062         }
1063
1064         case VIDIOC_UNSUBSCRIBE_EVENT:
1065                 return v4l2_event_unsubscribe(&handle->vfh, arg);
1066
1067         case VIDIOC_DQEVENT:
1068                 return v4l2_event_dequeue(&handle->vfh, arg,
1069                                           file->f_flags & O_NONBLOCK);
1070
1071         /* Analog video standards make no sense for digital cameras. */
1072         case VIDIOC_ENUMSTD:
1073         case VIDIOC_QUERYSTD:
1074         case VIDIOC_G_STD:
1075         case VIDIOC_S_STD:
1076
1077         case VIDIOC_OVERLAY:
1078
1079         case VIDIOC_ENUMAUDIO:
1080         case VIDIOC_ENUMAUDOUT:
1081
1082         case VIDIOC_ENUMOUTPUT:
1083                 uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd);
1084                 return -ENOTTY;
1085
1086         case UVCIOC_CTRL_MAP:
1087                 return uvc_ioctl_ctrl_map(chain, arg);
1088
1089         case UVCIOC_CTRL_QUERY:
1090                 return uvc_xu_ctrl_query(chain, arg);
1091
1092         default:
1093                 uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n", cmd);
1094                 return -ENOTTY;
1095         }
1096
1097         return ret;
1098 }
1099
1100 static long uvc_v4l2_ioctl(struct file *file,
1101                      unsigned int cmd, unsigned long arg)
1102 {
1103         if (uvc_trace_param & UVC_TRACE_IOCTL) {
1104                 uvc_printk(KERN_DEBUG, "uvc_v4l2_ioctl(");
1105                 v4l_printk_ioctl(NULL, cmd);
1106                 printk(")\n");
1107         }
1108
1109         return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl);
1110 }
1111
1112 #ifdef CONFIG_COMPAT
1113 struct uvc_xu_control_mapping32 {
1114         __u32 id;
1115         __u8 name[32];
1116         __u8 entity[16];
1117         __u8 selector;
1118
1119         __u8 size;
1120         __u8 offset;
1121         __u32 v4l2_type;
1122         __u32 data_type;
1123
1124         compat_caddr_t menu_info;
1125         __u32 menu_count;
1126
1127         __u32 reserved[4];
1128 };
1129
1130 static int uvc_v4l2_get_xu_mapping(struct uvc_xu_control_mapping *kp,
1131                         const struct uvc_xu_control_mapping32 __user *up)
1132 {
1133         struct uvc_menu_info __user *umenus;
1134         struct uvc_menu_info __user *kmenus;
1135         compat_caddr_t p;
1136
1137         if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
1138             __copy_from_user(kp, up, offsetof(typeof(*up), menu_info)) ||
1139             __get_user(kp->menu_count, &up->menu_count))
1140                 return -EFAULT;
1141
1142         memset(kp->reserved, 0, sizeof(kp->reserved));
1143
1144         if (kp->menu_count == 0) {
1145                 kp->menu_info = NULL;
1146                 return 0;
1147         }
1148
1149         if (__get_user(p, &up->menu_info))
1150                 return -EFAULT;
1151         umenus = compat_ptr(p);
1152         if (!access_ok(VERIFY_READ, umenus, kp->menu_count * sizeof(*umenus)))
1153                 return -EFAULT;
1154
1155         kmenus = compat_alloc_user_space(kp->menu_count * sizeof(*kmenus));
1156         if (kmenus == NULL)
1157                 return -EFAULT;
1158         kp->menu_info = kmenus;
1159
1160         if (copy_in_user(kmenus, umenus, kp->menu_count * sizeof(*umenus)))
1161                 return -EFAULT;
1162
1163         return 0;
1164 }
1165
1166 static int uvc_v4l2_put_xu_mapping(const struct uvc_xu_control_mapping *kp,
1167                         struct uvc_xu_control_mapping32 __user *up)
1168 {
1169         struct uvc_menu_info __user *umenus;
1170         struct uvc_menu_info __user *kmenus = kp->menu_info;
1171         compat_caddr_t p;
1172
1173         if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
1174             __copy_to_user(up, kp, offsetof(typeof(*up), menu_info)) ||
1175             __put_user(kp->menu_count, &up->menu_count))
1176                 return -EFAULT;
1177
1178         if (__clear_user(up->reserved, sizeof(up->reserved)))
1179                 return -EFAULT;
1180
1181         if (kp->menu_count == 0)
1182                 return 0;
1183
1184         if (get_user(p, &up->menu_info))
1185                 return -EFAULT;
1186         umenus = compat_ptr(p);
1187
1188         if (copy_in_user(umenus, kmenus, kp->menu_count * sizeof(*umenus)))
1189                 return -EFAULT;
1190
1191         return 0;
1192 }
1193
1194 struct uvc_xu_control_query32 {
1195         __u8 unit;
1196         __u8 selector;
1197         __u8 query;
1198         __u16 size;
1199         compat_caddr_t data;
1200 };
1201
1202 static int uvc_v4l2_get_xu_query(struct uvc_xu_control_query *kp,
1203                         const struct uvc_xu_control_query32 __user *up)
1204 {
1205         u8 __user *udata;
1206         u8 __user *kdata;
1207         compat_caddr_t p;
1208
1209         if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
1210             __copy_from_user(kp, up, offsetof(typeof(*up), data)))
1211                 return -EFAULT;
1212
1213         if (kp->size == 0) {
1214                 kp->data = NULL;
1215                 return 0;
1216         }
1217
1218         if (__get_user(p, &up->data))
1219                 return -EFAULT;
1220         udata = compat_ptr(p);
1221         if (!access_ok(VERIFY_READ, udata, kp->size))
1222                 return -EFAULT;
1223
1224         kdata = compat_alloc_user_space(kp->size);
1225         if (kdata == NULL)
1226                 return -EFAULT;
1227         kp->data = kdata;
1228
1229         if (copy_in_user(kdata, udata, kp->size))
1230                 return -EFAULT;
1231
1232         return 0;
1233 }
1234
1235 static int uvc_v4l2_put_xu_query(const struct uvc_xu_control_query *kp,
1236                         struct uvc_xu_control_query32 __user *up)
1237 {
1238         u8 __user *udata;
1239         u8 __user *kdata = kp->data;
1240         compat_caddr_t p;
1241
1242         if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
1243             __copy_to_user(up, kp, offsetof(typeof(*up), data)))
1244                 return -EFAULT;
1245
1246         if (kp->size == 0)
1247                 return 0;
1248
1249         if (get_user(p, &up->data))
1250                 return -EFAULT;
1251         udata = compat_ptr(p);
1252         if (!access_ok(VERIFY_READ, udata, kp->size))
1253                 return -EFAULT;
1254
1255         if (copy_in_user(udata, kdata, kp->size))
1256                 return -EFAULT;
1257
1258         return 0;
1259 }
1260
1261 #define UVCIOC_CTRL_MAP32       _IOWR('u', 0x20, struct uvc_xu_control_mapping32)
1262 #define UVCIOC_CTRL_QUERY32     _IOWR('u', 0x21, struct uvc_xu_control_query32)
1263
1264 static long uvc_v4l2_compat_ioctl32(struct file *file,
1265                      unsigned int cmd, unsigned long arg)
1266 {
1267         union {
1268                 struct uvc_xu_control_mapping xmap;
1269                 struct uvc_xu_control_query xqry;
1270         } karg;
1271         void __user *up = compat_ptr(arg);
1272         mm_segment_t old_fs;
1273         long ret;
1274
1275         switch (cmd) {
1276         case UVCIOC_CTRL_MAP32:
1277                 cmd = UVCIOC_CTRL_MAP;
1278                 ret = uvc_v4l2_get_xu_mapping(&karg.xmap, up);
1279                 break;
1280
1281         case UVCIOC_CTRL_QUERY32:
1282                 cmd = UVCIOC_CTRL_QUERY;
1283                 ret = uvc_v4l2_get_xu_query(&karg.xqry, up);
1284                 break;
1285
1286         default:
1287                 return -ENOIOCTLCMD;
1288         }
1289
1290         old_fs = get_fs();
1291         set_fs(KERNEL_DS);
1292         ret = uvc_v4l2_ioctl(file, cmd, (unsigned long)&karg);
1293         set_fs(old_fs);
1294
1295         if (ret < 0)
1296                 return ret;
1297
1298         switch (cmd) {
1299         case UVCIOC_CTRL_MAP:
1300                 ret = uvc_v4l2_put_xu_mapping(&karg.xmap, up);
1301                 break;
1302
1303         case UVCIOC_CTRL_QUERY:
1304                 ret = uvc_v4l2_put_xu_query(&karg.xqry, up);
1305                 break;
1306         }
1307
1308         return ret;
1309 }
1310 #endif
1311
1312 static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1313                     size_t count, loff_t *ppos)
1314 {
1315         uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
1316         return -EINVAL;
1317 }
1318
1319 static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1320 {
1321         struct uvc_fh *handle = file->private_data;
1322         struct uvc_streaming *stream = handle->stream;
1323
1324         uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
1325
1326         return uvc_queue_mmap(&stream->queue, vma);
1327 }
1328
1329 static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
1330 {
1331         struct uvc_fh *handle = file->private_data;
1332         struct uvc_streaming *stream = handle->stream;
1333
1334         uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
1335
1336         return uvc_queue_poll(&stream->queue, file, wait);
1337 }
1338
1339 #ifndef CONFIG_MMU
1340 static unsigned long uvc_v4l2_get_unmapped_area(struct file *file,
1341                 unsigned long addr, unsigned long len, unsigned long pgoff,
1342                 unsigned long flags)
1343 {
1344         struct uvc_fh *handle = file->private_data;
1345         struct uvc_streaming *stream = handle->stream;
1346
1347         uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_get_unmapped_area\n");
1348
1349         return uvc_queue_get_unmapped_area(&stream->queue, pgoff);
1350 }
1351 #endif
1352
1353 const struct v4l2_file_operations uvc_fops = {
1354         .owner          = THIS_MODULE,
1355         .open           = uvc_v4l2_open,
1356         .release        = uvc_v4l2_release,
1357         .unlocked_ioctl = uvc_v4l2_ioctl,
1358 #ifdef CONFIG_COMPAT
1359         .compat_ioctl32 = uvc_v4l2_compat_ioctl32,
1360 #endif
1361         .read           = uvc_v4l2_read,
1362         .mmap           = uvc_v4l2_mmap,
1363         .poll           = uvc_v4l2_poll,
1364 #ifndef CONFIG_MMU
1365         .get_unmapped_area = uvc_v4l2_get_unmapped_area,
1366 #endif
1367 };
1368