media: uvcvideo: Fix ENUMINPUT handling
[firefly-linux-kernel-4.4.55.git] / drivers / media / video / 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/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>
25
26 #include <media/v4l2-common.h>
27 #include <media/v4l2-ioctl.h>
28
29 #include "uvcvideo.h"
30
31 /* ------------------------------------------------------------------------
32  * UVC ioctls
33  */
34 static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain,
35         struct uvc_xu_control_mapping *xmap, int old)
36 {
37         struct uvc_control_mapping *map;
38         unsigned int size;
39         int ret;
40
41         map = kzalloc(sizeof *map, GFP_KERNEL);
42         if (map == NULL)
43                 return -ENOMEM;
44
45         map->id = xmap->id;
46         memcpy(map->name, xmap->name, sizeof map->name);
47         memcpy(map->entity, xmap->entity, sizeof map->entity);
48         map->selector = xmap->selector;
49         map->size = xmap->size;
50         map->offset = xmap->offset;
51         map->v4l2_type = xmap->v4l2_type;
52         map->data_type = xmap->data_type;
53
54         switch (xmap->v4l2_type) {
55         case V4L2_CTRL_TYPE_INTEGER:
56         case V4L2_CTRL_TYPE_BOOLEAN:
57         case V4L2_CTRL_TYPE_BUTTON:
58                 break;
59
60         case V4L2_CTRL_TYPE_MENU:
61                 if (old) {
62                         uvc_trace(UVC_TRACE_CONTROL, "V4L2_CTRL_TYPE_MENU not "
63                                   "supported for UVCIOC_CTRL_MAP_OLD.\n");
64                         ret = -EINVAL;
65                         goto done;
66                 }
67
68                 /* Prevent excessive memory consumption, as well as integer
69                  * overflows.
70                  */
71                 if (xmap->menu_count == 0 ||
72                     xmap->menu_count > UVC_MAX_CONTROL_MENU_ENTRIES) {
73                         ret = -EINVAL;
74                         goto done;
75                 }
76
77                 size = xmap->menu_count * sizeof(*map->menu_info);
78                 map->menu_info = kmalloc(size, GFP_KERNEL);
79                 if (map->menu_info == NULL) {
80                         ret = -ENOMEM;
81                         goto done;
82                 }
83
84                 if (copy_from_user(map->menu_info, xmap->menu_info, size)) {
85                         ret = -EFAULT;
86                         goto done;
87                 }
88
89                 map->menu_count = xmap->menu_count;
90                 break;
91
92         default:
93                 uvc_trace(UVC_TRACE_CONTROL, "Unsupported V4L2 control type "
94                           "%u.\n", xmap->v4l2_type);
95                 ret = -EINVAL;
96                 goto done;
97         }
98
99         ret = uvc_ctrl_add_mapping(chain, map);
100
101 done:
102         kfree(map->menu_info);
103         kfree(map);
104
105         return ret;
106 }
107
108 /* ------------------------------------------------------------------------
109  * V4L2 interface
110  */
111
112 /*
113  * Find the frame interval closest to the requested frame interval for the
114  * given frame format and size. This should be done by the device as part of
115  * the Video Probe and Commit negotiation, but some hardware don't implement
116  * that feature.
117  */
118 static __u32 uvc_try_frame_interval(struct uvc_frame *frame, __u32 interval)
119 {
120         unsigned int i;
121
122         if (frame->bFrameIntervalType) {
123                 __u32 best = -1, dist;
124
125                 for (i = 0; i < frame->bFrameIntervalType; ++i) {
126                         dist = interval > frame->dwFrameInterval[i]
127                              ? interval - frame->dwFrameInterval[i]
128                              : frame->dwFrameInterval[i] - interval;
129
130                         if (dist > best)
131                                 break;
132
133                         best = dist;
134                 }
135
136                 interval = frame->dwFrameInterval[i-1];
137         } else {
138                 const __u32 min = frame->dwFrameInterval[0];
139                 const __u32 max = frame->dwFrameInterval[1];
140                 const __u32 step = frame->dwFrameInterval[2];
141
142                 interval = min + (interval - min + step/2) / step * step;
143                 if (interval > max)
144                         interval = max;
145         }
146
147         return interval;
148 }
149
150 static int uvc_v4l2_try_format(struct uvc_streaming *stream,
151         struct v4l2_format *fmt, struct uvc_streaming_control *probe,
152         struct uvc_format **uvc_format, struct uvc_frame **uvc_frame)
153 {
154         struct uvc_format *format = NULL;
155         struct uvc_frame *frame = NULL;
156         __u16 rw, rh;
157         unsigned int d, maxd;
158         unsigned int i;
159         __u32 interval;
160         int ret = 0;
161         __u8 *fcc;
162
163         if (fmt->type != stream->type)
164                 return -EINVAL;
165
166         fcc = (__u8 *)&fmt->fmt.pix.pixelformat;
167         uvc_trace(UVC_TRACE_FORMAT, "Trying format 0x%08x (%c%c%c%c): %ux%u.\n",
168                         fmt->fmt.pix.pixelformat,
169                         fcc[0], fcc[1], fcc[2], fcc[3],
170                         fmt->fmt.pix.width, fmt->fmt.pix.height);
171
172         /* Check if the hardware supports the requested format. */
173         for (i = 0; i < stream->nformats; ++i) {
174                 format = &stream->format[i];
175                 if (format->fcc == fmt->fmt.pix.pixelformat)
176                         break;
177         }
178
179         if (format == NULL || format->fcc != fmt->fmt.pix.pixelformat) {
180                 uvc_trace(UVC_TRACE_FORMAT, "Unsupported format 0x%08x.\n",
181                                 fmt->fmt.pix.pixelformat);
182                 return -EINVAL;
183         }
184
185         /* Find the closest image size. The distance between image sizes is
186          * the size in pixels of the non-overlapping regions between the
187          * requested size and the frame-specified size.
188          */
189         rw = fmt->fmt.pix.width;
190         rh = fmt->fmt.pix.height;
191         maxd = (unsigned int)-1;
192
193         for (i = 0; i < format->nframes; ++i) {
194                 __u16 w = format->frame[i].wWidth;
195                 __u16 h = format->frame[i].wHeight;
196
197                 d = min(w, rw) * min(h, rh);
198                 d = w*h + rw*rh - 2*d;
199                 if (d < maxd) {
200                         maxd = d;
201                         frame = &format->frame[i];
202                 }
203
204                 if (maxd == 0)
205                         break;
206         }
207
208         if (frame == NULL) {
209                 uvc_trace(UVC_TRACE_FORMAT, "Unsupported size %ux%u.\n",
210                                 fmt->fmt.pix.width, fmt->fmt.pix.height);
211                 return -EINVAL;
212         }
213
214         /* Use the default frame interval. */
215         interval = frame->dwDefaultFrameInterval;
216         uvc_trace(UVC_TRACE_FORMAT, "Using default frame interval %u.%u us "
217                 "(%u.%u fps).\n", interval/10, interval%10, 10000000/interval,
218                 (100000000/interval)%10);
219
220         /* Set the format index, frame index and frame interval. */
221         memset(probe, 0, sizeof *probe);
222         probe->bmHint = 1;      /* dwFrameInterval */
223         probe->bFormatIndex = format->index;
224         probe->bFrameIndex = frame->bFrameIndex;
225         probe->dwFrameInterval = uvc_try_frame_interval(frame, interval);
226         /* Some webcams stall the probe control set request when the
227          * dwMaxVideoFrameSize field is set to zero. The UVC specification
228          * clearly states that the field is read-only from the host, so this
229          * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by
230          * the webcam to work around the problem.
231          *
232          * The workaround could probably be enabled for all webcams, so the
233          * quirk can be removed if needed. It's currently useful to detect
234          * webcam bugs and fix them before they hit the market (providing
235          * developers test their webcams with the Linux driver as well as with
236          * the Windows driver).
237          */
238         mutex_lock(&stream->mutex);
239         if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS)
240                 probe->dwMaxVideoFrameSize =
241                         stream->ctrl.dwMaxVideoFrameSize;
242
243         /* Probe the device. */
244         ret = uvc_probe_video(stream, probe);
245         mutex_unlock(&stream->mutex);
246         if (ret < 0)
247                 goto done;
248
249         fmt->fmt.pix.width = frame->wWidth;
250         fmt->fmt.pix.height = frame->wHeight;
251         fmt->fmt.pix.field = V4L2_FIELD_NONE;
252         fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
253         fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize;
254         fmt->fmt.pix.colorspace = format->colorspace;
255         fmt->fmt.pix.priv = 0;
256
257         if (uvc_format != NULL)
258                 *uvc_format = format;
259         if (uvc_frame != NULL)
260                 *uvc_frame = frame;
261
262 done:
263         return ret;
264 }
265
266 static int uvc_v4l2_get_format(struct uvc_streaming *stream,
267         struct v4l2_format *fmt)
268 {
269         struct uvc_format *format;
270         struct uvc_frame *frame;
271         int ret = 0;
272
273         if (fmt->type != stream->type)
274                 return -EINVAL;
275
276         mutex_lock(&stream->mutex);
277         format = stream->cur_format;
278         frame = stream->cur_frame;
279
280         if (format == NULL || frame == NULL) {
281                 ret = -EINVAL;
282                 goto done;
283         }
284
285         fmt->fmt.pix.pixelformat = format->fcc;
286         fmt->fmt.pix.width = frame->wWidth;
287         fmt->fmt.pix.height = frame->wHeight;
288         fmt->fmt.pix.field = V4L2_FIELD_NONE;
289         fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
290         fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize;
291         fmt->fmt.pix.colorspace = format->colorspace;
292         fmt->fmt.pix.priv = 0;
293
294 done:
295         mutex_unlock(&stream->mutex);
296         return ret;
297 }
298
299 static int uvc_v4l2_set_format(struct uvc_streaming *stream,
300         struct v4l2_format *fmt)
301 {
302         struct uvc_streaming_control probe;
303         struct uvc_format *format;
304         struct uvc_frame *frame;
305         int ret;
306
307         if (fmt->type != stream->type)
308                 return -EINVAL;
309
310         ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame);
311         if (ret < 0)
312                 return ret;
313
314         mutex_lock(&stream->mutex);
315
316         if (uvc_queue_allocated(&stream->queue)) {
317                 ret = -EBUSY;
318                 goto done;
319         }
320
321         memcpy(&stream->ctrl, &probe, sizeof probe);
322         stream->cur_format = format;
323         stream->cur_frame = frame;
324
325 done:
326         mutex_unlock(&stream->mutex);
327         return ret;
328 }
329
330 static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream,
331                 struct v4l2_streamparm *parm)
332 {
333         uint32_t numerator, denominator;
334
335         if (parm->type != stream->type)
336                 return -EINVAL;
337
338         mutex_lock(&stream->mutex);
339         numerator = stream->ctrl.dwFrameInterval;
340         mutex_unlock(&stream->mutex);
341
342         denominator = 10000000;
343         uvc_simplify_fraction(&numerator, &denominator, 8, 333);
344
345         memset(parm, 0, sizeof *parm);
346         parm->type = stream->type;
347
348         if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
349                 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
350                 parm->parm.capture.capturemode = 0;
351                 parm->parm.capture.timeperframe.numerator = numerator;
352                 parm->parm.capture.timeperframe.denominator = denominator;
353                 parm->parm.capture.extendedmode = 0;
354                 parm->parm.capture.readbuffers = 0;
355         } else {
356                 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
357                 parm->parm.output.outputmode = 0;
358                 parm->parm.output.timeperframe.numerator = numerator;
359                 parm->parm.output.timeperframe.denominator = denominator;
360         }
361
362         return 0;
363 }
364
365 static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
366                 struct v4l2_streamparm *parm)
367 {
368         struct uvc_streaming_control probe;
369         struct v4l2_fract timeperframe;
370         uint32_t interval;
371         int ret;
372
373         if (parm->type != stream->type)
374                 return -EINVAL;
375
376         if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
377                 timeperframe = parm->parm.capture.timeperframe;
378         else
379                 timeperframe = parm->parm.output.timeperframe;
380
381         interval = uvc_fraction_to_interval(timeperframe.numerator,
382                 timeperframe.denominator);
383         uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
384                 timeperframe.numerator, timeperframe.denominator, interval);
385
386         mutex_lock(&stream->mutex);
387
388         if (uvc_queue_streaming(&stream->queue)) {
389                 mutex_unlock(&stream->mutex);
390                 return -EBUSY;
391         }
392
393         memcpy(&probe, &stream->ctrl, sizeof probe);
394         probe.dwFrameInterval =
395                 uvc_try_frame_interval(stream->cur_frame, interval);
396
397         /* Probe the device with the new settings. */
398         ret = uvc_probe_video(stream, &probe);
399         if (ret < 0) {
400                 mutex_unlock(&stream->mutex);
401                 return ret;
402         }
403
404         memcpy(&stream->ctrl, &probe, sizeof probe);
405         mutex_unlock(&stream->mutex);
406
407         /* Return the actual frame period. */
408         timeperframe.numerator = probe.dwFrameInterval;
409         timeperframe.denominator = 10000000;
410         uvc_simplify_fraction(&timeperframe.numerator,
411                 &timeperframe.denominator, 8, 333);
412
413         if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
414                 parm->parm.capture.timeperframe = timeperframe;
415         else
416                 parm->parm.output.timeperframe = timeperframe;
417
418         return 0;
419 }
420
421 /* ------------------------------------------------------------------------
422  * Privilege management
423  */
424
425 /*
426  * Privilege management is the multiple-open implementation basis. The current
427  * implementation is completely transparent for the end-user and doesn't
428  * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls.
429  * Those ioctls enable finer control on the device (by making possible for a
430  * user to request exclusive access to a device), but are not mature yet.
431  * Switching to the V4L2 priority mechanism might be considered in the future
432  * if this situation changes.
433  *
434  * Each open instance of a UVC device can either be in a privileged or
435  * unprivileged state. Only a single instance can be in a privileged state at
436  * a given time. Trying to perform an operation that requires privileges will
437  * automatically acquire the required privileges if possible, or return -EBUSY
438  * otherwise. Privileges are dismissed when closing the instance or when
439  * freeing the video buffers using VIDIOC_REQBUFS.
440  *
441  * Operations that require privileges are:
442  *
443  * - VIDIOC_S_INPUT
444  * - VIDIOC_S_PARM
445  * - VIDIOC_S_FMT
446  * - VIDIOC_REQBUFS
447  */
448 static int uvc_acquire_privileges(struct uvc_fh *handle)
449 {
450         /* Always succeed if the handle is already privileged. */
451         if (handle->state == UVC_HANDLE_ACTIVE)
452                 return 0;
453
454         /* Check if the device already has a privileged handle. */
455         if (atomic_inc_return(&handle->stream->active) != 1) {
456                 atomic_dec(&handle->stream->active);
457                 return -EBUSY;
458         }
459
460         handle->state = UVC_HANDLE_ACTIVE;
461         return 0;
462 }
463
464 static void uvc_dismiss_privileges(struct uvc_fh *handle)
465 {
466         if (handle->state == UVC_HANDLE_ACTIVE)
467                 atomic_dec(&handle->stream->active);
468
469         handle->state = UVC_HANDLE_PASSIVE;
470 }
471
472 static int uvc_has_privileges(struct uvc_fh *handle)
473 {
474         return handle->state == UVC_HANDLE_ACTIVE;
475 }
476
477 /* ------------------------------------------------------------------------
478  * V4L2 file operations
479  */
480
481 static int uvc_v4l2_open(struct file *file)
482 {
483         struct uvc_streaming *stream;
484         struct uvc_fh *handle;
485         int ret = 0;
486
487         uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
488         stream = video_drvdata(file);
489
490         if (stream->dev->state & UVC_DEV_DISCONNECTED)
491                 return -ENODEV;
492
493         ret = usb_autopm_get_interface(stream->dev->intf);
494         if (ret < 0)
495                 return ret;
496
497         /* Create the device handle. */
498         handle = kzalloc(sizeof *handle, GFP_KERNEL);
499         if (handle == NULL) {
500                 usb_autopm_put_interface(stream->dev->intf);
501                 return -ENOMEM;
502         }
503
504         if (atomic_inc_return(&stream->dev->users) == 1) {
505                 ret = uvc_status_start(stream->dev);
506                 if (ret < 0) {
507                         usb_autopm_put_interface(stream->dev->intf);
508                         atomic_dec(&stream->dev->users);
509                         kfree(handle);
510                         return ret;
511                 }
512         }
513
514         handle->chain = stream->chain;
515         handle->stream = stream;
516         handle->state = UVC_HANDLE_PASSIVE;
517         file->private_data = handle;
518
519         return 0;
520 }
521
522 static int uvc_v4l2_release(struct file *file)
523 {
524         struct uvc_fh *handle = file->private_data;
525         struct uvc_streaming *stream = handle->stream;
526
527         uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");
528
529         /* Only free resources if this is a privileged handle. */
530         if (uvc_has_privileges(handle)) {
531                 uvc_video_enable(stream, 0);
532
533                 if (uvc_free_buffers(&stream->queue) < 0)
534                         uvc_printk(KERN_ERR, "uvc_v4l2_release: Unable to "
535                                         "free buffers.\n");
536         }
537
538         /* Release the file handle. */
539         uvc_dismiss_privileges(handle);
540         kfree(handle);
541         file->private_data = NULL;
542
543         if (atomic_dec_return(&stream->dev->users) == 0)
544                 uvc_status_stop(stream->dev);
545
546         usb_autopm_put_interface(stream->dev->intf);
547         return 0;
548 }
549
550 static void uvc_v4l2_ioctl_warn(void)
551 {
552         static int warned;
553
554         if (warned)
555                 return;
556
557         uvc_printk(KERN_INFO, "Deprecated UVCIOC_CTRL_{ADD,MAP_OLD,GET,SET} "
558                    "ioctls will be removed in 2.6.42.\n");
559         uvc_printk(KERN_INFO, "See http://www.ideasonboard.org/uvc/upgrade/ "
560                    "for upgrade instructions.\n");
561         warned = 1;
562 }
563
564 static long uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
565 {
566         struct video_device *vdev = video_devdata(file);
567         struct uvc_fh *handle = file->private_data;
568         struct uvc_video_chain *chain = handle->chain;
569         struct uvc_streaming *stream = handle->stream;
570         long ret = 0;
571
572         switch (cmd) {
573         /* Query capabilities */
574         case VIDIOC_QUERYCAP:
575         {
576                 struct v4l2_capability *cap = arg;
577
578                 memset(cap, 0, sizeof *cap);
579                 strlcpy(cap->driver, "uvcvideo", sizeof cap->driver);
580                 strlcpy(cap->card, vdev->name, sizeof cap->card);
581                 usb_make_path(stream->dev->udev,
582                               cap->bus_info, sizeof(cap->bus_info));
583                 cap->version = DRIVER_VERSION_NUMBER;
584                 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
585                         cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
586                                           | V4L2_CAP_STREAMING;
587                 else
588                         cap->capabilities = V4L2_CAP_VIDEO_OUTPUT
589                                           | V4L2_CAP_STREAMING;
590                 break;
591         }
592
593         /* Get, Set & Query control */
594         case VIDIOC_QUERYCTRL:
595                 return uvc_query_v4l2_ctrl(chain, arg);
596
597         case VIDIOC_G_CTRL:
598         {
599                 struct v4l2_control *ctrl = arg;
600                 struct v4l2_ext_control xctrl;
601
602                 memset(&xctrl, 0, sizeof xctrl);
603                 xctrl.id = ctrl->id;
604
605                 ret = uvc_ctrl_begin(chain);
606                 if (ret < 0)
607                         return ret;
608
609                 ret = uvc_ctrl_get(chain, &xctrl);
610                 uvc_ctrl_rollback(chain);
611                 if (ret >= 0)
612                         ctrl->value = xctrl.value;
613                 break;
614         }
615
616         case VIDIOC_S_CTRL:
617         {
618                 struct v4l2_control *ctrl = arg;
619                 struct v4l2_ext_control xctrl;
620
621                 memset(&xctrl, 0, sizeof xctrl);
622                 xctrl.id = ctrl->id;
623                 xctrl.value = ctrl->value;
624
625                 ret = uvc_ctrl_begin(chain);
626                 if (ret < 0)
627                         return ret;
628
629                 ret = uvc_ctrl_set(chain, &xctrl);
630                 if (ret < 0) {
631                         uvc_ctrl_rollback(chain);
632                         return ret;
633                 }
634                 ret = uvc_ctrl_commit(chain);
635                 if (ret == 0)
636                         ctrl->value = xctrl.value;
637                 break;
638         }
639
640         case VIDIOC_QUERYMENU:
641                 return uvc_query_v4l2_menu(chain, arg);
642
643         case VIDIOC_G_EXT_CTRLS:
644         {
645                 struct v4l2_ext_controls *ctrls = arg;
646                 struct v4l2_ext_control *ctrl = ctrls->controls;
647                 unsigned int i;
648
649                 ret = uvc_ctrl_begin(chain);
650                 if (ret < 0)
651                         return ret;
652
653                 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
654                         ret = uvc_ctrl_get(chain, ctrl);
655                         if (ret < 0) {
656                                 uvc_ctrl_rollback(chain);
657                                 ctrls->error_idx = i;
658                                 return ret;
659                         }
660                 }
661                 ctrls->error_idx = 0;
662                 ret = uvc_ctrl_rollback(chain);
663                 break;
664         }
665
666         case VIDIOC_S_EXT_CTRLS:
667         case VIDIOC_TRY_EXT_CTRLS:
668         {
669                 struct v4l2_ext_controls *ctrls = arg;
670                 struct v4l2_ext_control *ctrl = ctrls->controls;
671                 unsigned int i;
672
673                 ret = uvc_ctrl_begin(chain);
674                 if (ret < 0)
675                         return ret;
676
677                 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
678                         ret = uvc_ctrl_set(chain, ctrl);
679                         if (ret < 0) {
680                                 uvc_ctrl_rollback(chain);
681                                 ctrls->error_idx = i;
682                                 return ret;
683                         }
684                 }
685
686                 ctrls->error_idx = 0;
687
688                 if (cmd == VIDIOC_S_EXT_CTRLS)
689                         ret = uvc_ctrl_commit(chain);
690                 else
691                         ret = uvc_ctrl_rollback(chain);
692                 break;
693         }
694
695         /* Get, Set & Enum input */
696         case VIDIOC_ENUMINPUT:
697         {
698                 const struct uvc_entity *selector = chain->selector;
699                 struct v4l2_input *input = arg;
700                 struct uvc_entity *iterm = NULL;
701                 u32 index = input->index;
702                 int pin = 0;
703
704                 if (selector == NULL ||
705                     (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
706                         if (index != 0)
707                                 return -EINVAL;
708                         list_for_each_entry(iterm, &chain->entities, chain) {
709                                 if (UVC_ENTITY_IS_ITERM(iterm))
710                                         break;
711                         }
712                         pin = iterm->id;
713                 } else if (index < selector->bNrInPins) {
714                         pin = selector->baSourceID[index];
715                         list_for_each_entry(iterm, &chain->entities, chain) {
716                                 if (!UVC_ENTITY_IS_ITERM(iterm))
717                                         continue;
718                                 if (iterm->id == pin)
719                                         break;
720                         }
721                 }
722
723                 if (iterm == NULL || iterm->id != pin)
724                         return -EINVAL;
725
726                 memset(input, 0, sizeof *input);
727                 input->index = index;
728                 strlcpy(input->name, iterm->name, sizeof input->name);
729                 if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
730                         input->type = V4L2_INPUT_TYPE_CAMERA;
731                 break;
732         }
733
734         case VIDIOC_G_INPUT:
735         {
736                 u8 input;
737
738                 if (chain->selector == NULL ||
739                     (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
740                         *(int *)arg = 0;
741                         break;
742                 }
743
744                 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
745                         chain->selector->id, chain->dev->intfnum,
746                         UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
747                 if (ret < 0)
748                         return ret;
749
750                 *(int *)arg = input - 1;
751                 break;
752         }
753
754         case VIDIOC_S_INPUT:
755         {
756                 u32 input = *(u32 *)arg + 1;
757
758                 if ((ret = uvc_acquire_privileges(handle)) < 0)
759                         return ret;
760
761                 if (chain->selector == NULL ||
762                     (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
763                         if (input != 1)
764                                 return -EINVAL;
765                         break;
766                 }
767
768                 if (input == 0 || input > chain->selector->bNrInPins)
769                         return -EINVAL;
770
771                 return uvc_query_ctrl(chain->dev, UVC_SET_CUR,
772                         chain->selector->id, chain->dev->intfnum,
773                         UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
774         }
775
776         /* Try, Get, Set & Enum format */
777         case VIDIOC_ENUM_FMT:
778         {
779                 struct v4l2_fmtdesc *fmt = arg;
780                 struct uvc_format *format;
781                 enum v4l2_buf_type type = fmt->type;
782                 __u32 index = fmt->index;
783
784                 if (fmt->type != stream->type ||
785                     fmt->index >= stream->nformats)
786                         return -EINVAL;
787
788                 memset(fmt, 0, sizeof(*fmt));
789                 fmt->index = index;
790                 fmt->type = type;
791
792                 format = &stream->format[fmt->index];
793                 fmt->flags = 0;
794                 if (format->flags & UVC_FMT_FLAG_COMPRESSED)
795                         fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
796                 strlcpy(fmt->description, format->name,
797                         sizeof fmt->description);
798                 fmt->description[sizeof fmt->description - 1] = 0;
799                 fmt->pixelformat = format->fcc;
800                 break;
801         }
802
803         case VIDIOC_TRY_FMT:
804         {
805                 struct uvc_streaming_control probe;
806
807                 return uvc_v4l2_try_format(stream, arg, &probe, NULL, NULL);
808         }
809
810         case VIDIOC_S_FMT:
811                 if ((ret = uvc_acquire_privileges(handle)) < 0)
812                         return ret;
813
814                 return uvc_v4l2_set_format(stream, arg);
815
816         case VIDIOC_G_FMT:
817                 return uvc_v4l2_get_format(stream, arg);
818
819         /* Frame size enumeration */
820         case VIDIOC_ENUM_FRAMESIZES:
821         {
822                 struct v4l2_frmsizeenum *fsize = arg;
823                 struct uvc_format *format = NULL;
824                 struct uvc_frame *frame;
825                 int i;
826
827                 /* Look for the given pixel format */
828                 for (i = 0; i < stream->nformats; i++) {
829                         if (stream->format[i].fcc ==
830                                         fsize->pixel_format) {
831                                 format = &stream->format[i];
832                                 break;
833                         }
834                 }
835                 if (format == NULL)
836                         return -EINVAL;
837
838                 if (fsize->index >= format->nframes)
839                         return -EINVAL;
840
841                 frame = &format->frame[fsize->index];
842                 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
843                 fsize->discrete.width = frame->wWidth;
844                 fsize->discrete.height = frame->wHeight;
845                 break;
846         }
847
848         /* Frame interval enumeration */
849         case VIDIOC_ENUM_FRAMEINTERVALS:
850         {
851                 struct v4l2_frmivalenum *fival = arg;
852                 struct uvc_format *format = NULL;
853                 struct uvc_frame *frame = NULL;
854                 int i;
855
856                 /* Look for the given pixel format and frame size */
857                 for (i = 0; i < stream->nformats; i++) {
858                         if (stream->format[i].fcc ==
859                                         fival->pixel_format) {
860                                 format = &stream->format[i];
861                                 break;
862                         }
863                 }
864                 if (format == NULL)
865                         return -EINVAL;
866
867                 for (i = 0; i < format->nframes; i++) {
868                         if (format->frame[i].wWidth == fival->width &&
869                             format->frame[i].wHeight == fival->height) {
870                                 frame = &format->frame[i];
871                                 break;
872                         }
873                 }
874                 if (frame == NULL)
875                         return -EINVAL;
876
877                 if (frame->bFrameIntervalType) {
878                         if (fival->index >= frame->bFrameIntervalType)
879                                 return -EINVAL;
880
881                         fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
882                         fival->discrete.numerator =
883                                 frame->dwFrameInterval[fival->index];
884                         fival->discrete.denominator = 10000000;
885                         uvc_simplify_fraction(&fival->discrete.numerator,
886                                 &fival->discrete.denominator, 8, 333);
887                 } else {
888                         fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
889                         fival->stepwise.min.numerator =
890                                 frame->dwFrameInterval[0];
891                         fival->stepwise.min.denominator = 10000000;
892                         fival->stepwise.max.numerator =
893                                 frame->dwFrameInterval[1];
894                         fival->stepwise.max.denominator = 10000000;
895                         fival->stepwise.step.numerator =
896                                 frame->dwFrameInterval[2];
897                         fival->stepwise.step.denominator = 10000000;
898                         uvc_simplify_fraction(&fival->stepwise.min.numerator,
899                                 &fival->stepwise.min.denominator, 8, 333);
900                         uvc_simplify_fraction(&fival->stepwise.max.numerator,
901                                 &fival->stepwise.max.denominator, 8, 333);
902                         uvc_simplify_fraction(&fival->stepwise.step.numerator,
903                                 &fival->stepwise.step.denominator, 8, 333);
904                 }
905                 break;
906         }
907
908         /* Get & Set streaming parameters */
909         case VIDIOC_G_PARM:
910                 return uvc_v4l2_get_streamparm(stream, arg);
911
912         case VIDIOC_S_PARM:
913                 if ((ret = uvc_acquire_privileges(handle)) < 0)
914                         return ret;
915
916                 return uvc_v4l2_set_streamparm(stream, arg);
917
918         /* Cropping and scaling */
919         case VIDIOC_CROPCAP:
920         {
921                 struct v4l2_cropcap *ccap = arg;
922
923                 if (ccap->type != stream->type)
924                         return -EINVAL;
925
926                 ccap->bounds.left = 0;
927                 ccap->bounds.top = 0;
928
929                 mutex_lock(&stream->mutex);
930                 ccap->bounds.width = stream->cur_frame->wWidth;
931                 ccap->bounds.height = stream->cur_frame->wHeight;
932                 mutex_unlock(&stream->mutex);
933
934                 ccap->defrect = ccap->bounds;
935
936                 ccap->pixelaspect.numerator = 1;
937                 ccap->pixelaspect.denominator = 1;
938                 break;
939         }
940
941         case VIDIOC_G_CROP:
942         case VIDIOC_S_CROP:
943                 return -EINVAL;
944
945         /* Buffers & streaming */
946         case VIDIOC_REQBUFS:
947         {
948                 struct v4l2_requestbuffers *rb = arg;
949
950                 if (rb->type != stream->type ||
951                     rb->memory != V4L2_MEMORY_MMAP)
952                         return -EINVAL;
953
954                 if ((ret = uvc_acquire_privileges(handle)) < 0)
955                         return ret;
956
957                 mutex_lock(&stream->mutex);
958                 ret = uvc_alloc_buffers(&stream->queue, rb->count,
959                                         stream->ctrl.dwMaxVideoFrameSize);
960                 mutex_unlock(&stream->mutex);
961                 if (ret < 0)
962                         return ret;
963
964                 if (ret == 0)
965                         uvc_dismiss_privileges(handle);
966
967                 rb->count = ret;
968                 ret = 0;
969                 break;
970         }
971
972         case VIDIOC_QUERYBUF:
973         {
974                 struct v4l2_buffer *buf = arg;
975
976                 if (buf->type != stream->type)
977                         return -EINVAL;
978
979                 if (!uvc_has_privileges(handle))
980                         return -EBUSY;
981
982                 return uvc_query_buffer(&stream->queue, buf);
983         }
984
985         case VIDIOC_QBUF:
986                 if (!uvc_has_privileges(handle))
987                         return -EBUSY;
988
989                 return uvc_queue_buffer(&stream->queue, arg);
990
991         case VIDIOC_DQBUF:
992                 if (!uvc_has_privileges(handle))
993                         return -EBUSY;
994
995                 return uvc_dequeue_buffer(&stream->queue, arg,
996                         file->f_flags & O_NONBLOCK);
997
998         case VIDIOC_STREAMON:
999         {
1000                 int *type = arg;
1001
1002                 if (*type != stream->type)
1003                         return -EINVAL;
1004
1005                 if (!uvc_has_privileges(handle))
1006                         return -EBUSY;
1007
1008                 mutex_lock(&stream->mutex);
1009                 ret = uvc_video_enable(stream, 1);
1010                 mutex_unlock(&stream->mutex);
1011                 if (ret < 0)
1012                         return ret;
1013                 break;
1014         }
1015
1016         case VIDIOC_STREAMOFF:
1017         {
1018                 int *type = arg;
1019
1020                 if (*type != stream->type)
1021                         return -EINVAL;
1022
1023                 if (!uvc_has_privileges(handle))
1024                         return -EBUSY;
1025
1026                 return uvc_video_enable(stream, 0);
1027         }
1028
1029         /* Analog video standards make no sense for digital cameras. */
1030         case VIDIOC_ENUMSTD:
1031         case VIDIOC_QUERYSTD:
1032         case VIDIOC_G_STD:
1033         case VIDIOC_S_STD:
1034
1035         case VIDIOC_OVERLAY:
1036
1037         case VIDIOC_ENUMAUDIO:
1038         case VIDIOC_ENUMAUDOUT:
1039
1040         case VIDIOC_ENUMOUTPUT:
1041                 uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd);
1042                 return -EINVAL;
1043
1044         /* Dynamic controls. UVCIOC_CTRL_ADD, UVCIOC_CTRL_MAP_OLD,
1045          * UVCIOC_CTRL_GET and UVCIOC_CTRL_SET are deprecated and scheduled for
1046          * removal in 2.6.42.
1047          */
1048         case __UVCIOC_CTRL_ADD:
1049                 uvc_v4l2_ioctl_warn();
1050                 return -EEXIST;
1051
1052         case __UVCIOC_CTRL_MAP_OLD:
1053                 uvc_v4l2_ioctl_warn();
1054         case __UVCIOC_CTRL_MAP:
1055         case UVCIOC_CTRL_MAP:
1056                 return uvc_ioctl_ctrl_map(chain, arg,
1057                                           cmd == __UVCIOC_CTRL_MAP_OLD);
1058
1059         case __UVCIOC_CTRL_GET:
1060         case __UVCIOC_CTRL_SET:
1061         {
1062                 struct uvc_xu_control *xctrl = arg;
1063                 struct uvc_xu_control_query xqry = {
1064                         .unit           = xctrl->unit,
1065                         .selector       = xctrl->selector,
1066                         .query          = cmd == __UVCIOC_CTRL_GET
1067                                         ? UVC_GET_CUR : UVC_SET_CUR,
1068                         .size           = xctrl->size,
1069                         .data           = xctrl->data,
1070                 };
1071
1072                 uvc_v4l2_ioctl_warn();
1073                 return uvc_xu_ctrl_query(chain, &xqry);
1074         }
1075
1076         case UVCIOC_CTRL_QUERY:
1077                 return uvc_xu_ctrl_query(chain, arg);
1078
1079         default:
1080                 uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n", cmd);
1081                 return -EINVAL;
1082         }
1083
1084         return ret;
1085 }
1086
1087 static long uvc_v4l2_ioctl(struct file *file,
1088                      unsigned int cmd, unsigned long arg)
1089 {
1090         if (uvc_trace_param & UVC_TRACE_IOCTL) {
1091                 uvc_printk(KERN_DEBUG, "uvc_v4l2_ioctl(");
1092                 v4l_printk_ioctl(cmd);
1093                 printk(")\n");
1094         }
1095
1096         return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl);
1097 }
1098
1099 static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1100                     size_t count, loff_t *ppos)
1101 {
1102         uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
1103         return -EINVAL;
1104 }
1105
1106 static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1107 {
1108         struct uvc_fh *handle = file->private_data;
1109         struct uvc_streaming *stream = handle->stream;
1110
1111         uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
1112
1113         return uvc_queue_mmap(&stream->queue, vma);
1114 }
1115
1116 static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
1117 {
1118         struct uvc_fh *handle = file->private_data;
1119         struct uvc_streaming *stream = handle->stream;
1120
1121         uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
1122
1123         return uvc_queue_poll(&stream->queue, file, wait);
1124 }
1125
1126 #ifndef CONFIG_MMU
1127 static unsigned long uvc_v4l2_get_unmapped_area(struct file *file,
1128                 unsigned long addr, unsigned long len, unsigned long pgoff,
1129                 unsigned long flags)
1130 {
1131         struct uvc_fh *handle = file->private_data;
1132         struct uvc_streaming *stream = handle->stream;
1133
1134         uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_get_unmapped_area\n");
1135
1136         return uvc_queue_get_unmapped_area(&stream->queue, pgoff);
1137 }
1138 #endif
1139
1140 const struct v4l2_file_operations uvc_fops = {
1141         .owner          = THIS_MODULE,
1142         .open           = uvc_v4l2_open,
1143         .release        = uvc_v4l2_release,
1144         .unlocked_ioctl = uvc_v4l2_ioctl,
1145         .read           = uvc_v4l2_read,
1146         .mmap           = uvc_v4l2_mmap,
1147         .poll           = uvc_v4l2_poll,
1148 #ifndef CONFIG_MMU
1149         .get_unmapped_area = uvc_v4l2_get_unmapped_area,
1150 #endif
1151 };
1152