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