Merge remote-tracking branch 'kernel-2.6.32/develop' into develop-2.6.36
[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-2009
5  *          Laurent Pinchart (laurent.pinchart@skynet.be)
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_xu_control_mapping *xmap, int old)
35 {
36         struct uvc_control_mapping *map;
37         unsigned int size;
38         int ret;
39
40         map = kzalloc(sizeof *map, GFP_KERNEL);
41         if (map == NULL)
42                 return -ENOMEM;
43
44         map->id = xmap->id;
45         memcpy(map->name, xmap->name, sizeof map->name);
46         memcpy(map->entity, xmap->entity, sizeof map->entity);
47         map->selector = xmap->selector;
48         map->size = xmap->size;
49         map->offset = xmap->offset;
50         map->v4l2_type = xmap->v4l2_type;
51         map->data_type = xmap->data_type;
52
53         switch (xmap->v4l2_type) {
54         case V4L2_CTRL_TYPE_INTEGER:
55         case V4L2_CTRL_TYPE_BOOLEAN:
56         case V4L2_CTRL_TYPE_BUTTON:
57                 break;
58
59         case V4L2_CTRL_TYPE_MENU:
60                 if (old) {
61                         ret = -EINVAL;
62                         goto done;
63                 }
64
65                 size = xmap->menu_count * sizeof(*map->menu_info);
66                 map->menu_info = kmalloc(size, GFP_KERNEL);
67                 if (map->menu_info == NULL) {
68                         ret = -ENOMEM;
69                         goto done;
70                 }
71
72                 if (copy_from_user(map->menu_info, xmap->menu_info, size)) {
73                         ret = -EFAULT;
74                         goto done;
75                 }
76
77                 map->menu_count = xmap->menu_count;
78                 break;
79
80         default:
81                 ret = -EINVAL;
82                 goto done;
83         }
84
85         ret = uvc_ctrl_add_mapping(map);
86
87 done:
88         if (ret < 0) {
89                 kfree(map->menu_info);
90                 kfree(map);
91         }
92
93         return ret;
94 }
95
96 /* ------------------------------------------------------------------------
97  * V4L2 interface
98  */
99
100 /*
101  * Mapping V4L2 controls to UVC controls can be straighforward if done well.
102  * Most of the UVC controls exist in V4L2, and can be mapped directly. Some
103  * must be grouped (for instance the Red Balance, Blue Balance and Do White
104  * Balance V4L2 controls use the White Balance Component UVC control) or
105  * otherwise translated. The approach we take here is to use a translation
106  * table for the controls that can be mapped directly, and handle the others
107  * manually.
108  */
109 static int uvc_v4l2_query_menu(struct uvc_video_chain *chain,
110         struct v4l2_querymenu *query_menu)
111 {
112         struct uvc_menu_info *menu_info;
113         struct uvc_control_mapping *mapping;
114         struct uvc_control *ctrl;
115         u32 index = query_menu->index;
116         u32 id = query_menu->id;
117
118         ctrl = uvc_find_control(chain, query_menu->id, &mapping);
119         if (ctrl == NULL || mapping->v4l2_type != V4L2_CTRL_TYPE_MENU)
120                 return -EINVAL;
121
122         if (query_menu->index >= mapping->menu_count)
123                 return -EINVAL;
124
125         memset(query_menu, 0, sizeof(*query_menu));
126         query_menu->id = id;
127         query_menu->index = index;
128
129         menu_info = &mapping->menu_info[query_menu->index];
130         strlcpy(query_menu->name, menu_info->name, sizeof query_menu->name);
131         return 0;
132 }
133
134 /*
135  * Find the frame interval closest to the requested frame interval for the
136  * given frame format and size. This should be done by the device as part of
137  * the Video Probe and Commit negotiation, but some hardware don't implement
138  * that feature.
139  */
140 static __u32 uvc_try_frame_interval(struct uvc_frame *frame, __u32 interval)
141 {
142         unsigned int i;
143
144         if (frame->bFrameIntervalType) {
145                 __u32 best = -1, dist;
146
147                 for (i = 0; i < frame->bFrameIntervalType; ++i) {
148                         dist = interval > frame->dwFrameInterval[i]
149                              ? interval - frame->dwFrameInterval[i]
150                              : frame->dwFrameInterval[i] - interval;
151
152                         if (dist > best)
153                                 break;
154
155                         best = dist;
156                 }
157
158                 interval = frame->dwFrameInterval[i-1];
159         } else {
160                 const __u32 min = frame->dwFrameInterval[0];
161                 const __u32 max = frame->dwFrameInterval[1];
162                 const __u32 step = frame->dwFrameInterval[2];
163
164                 interval = min + (interval - min + step/2) / step * step;
165                 if (interval > max)
166                         interval = max;
167         }
168
169         return interval;
170 }
171
172 static int uvc_v4l2_try_format(struct uvc_streaming *stream,
173         struct v4l2_format *fmt, struct uvc_streaming_control *probe,
174         struct uvc_format **uvc_format, struct uvc_frame **uvc_frame)
175 {
176         struct uvc_format *format = NULL;
177         struct uvc_frame *frame = NULL;
178         __u16 rw, rh;
179         unsigned int d, maxd;
180         unsigned int i;
181         __u32 interval;
182         int ret = 0;
183         __u8 *fcc;
184
185         if (fmt->type != stream->type)
186                 return -EINVAL;
187
188         fcc = (__u8 *)&fmt->fmt.pix.pixelformat;
189         uvc_trace(UVC_TRACE_FORMAT, "Trying format 0x%08x (%c%c%c%c): %ux%u.\n",
190                         fmt->fmt.pix.pixelformat,
191                         fcc[0], fcc[1], fcc[2], fcc[3],
192                         fmt->fmt.pix.width, fmt->fmt.pix.height);
193
194         /* Check if the hardware supports the requested format. */
195         for (i = 0; i < stream->nformats; ++i) {
196                 format = &stream->format[i];
197                 if (format->fcc == fmt->fmt.pix.pixelformat)
198                         break;
199         }
200
201         if (format == NULL || format->fcc != fmt->fmt.pix.pixelformat) {
202                 uvc_trace(UVC_TRACE_FORMAT, "Unsupported format 0x%08x.\n",
203                                 fmt->fmt.pix.pixelformat);
204                 return -EINVAL;
205         }
206
207         /* Find the closest image size. The distance between image sizes is
208          * the size in pixels of the non-overlapping regions between the
209          * requested size and the frame-specified size.
210          */
211         rw = fmt->fmt.pix.width;
212         rh = fmt->fmt.pix.height;
213         maxd = (unsigned int)-1;
214
215         for (i = 0; i < format->nframes; ++i) {
216                 __u16 w = format->frame[i].wWidth;
217                 __u16 h = format->frame[i].wHeight;
218
219                 d = min(w, rw) * min(h, rh);
220                 d = w*h + rw*rh - 2*d;
221                 if (d < maxd) {
222                         maxd = d;
223                         frame = &format->frame[i];
224                 }
225
226                 if (maxd == 0)
227                         break;
228         }
229
230         if (frame == NULL) {
231                 uvc_trace(UVC_TRACE_FORMAT, "Unsupported size %ux%u.\n",
232                                 fmt->fmt.pix.width, fmt->fmt.pix.height);
233                 return -EINVAL;
234         }
235
236         /* Use the default frame interval. */
237         interval = frame->dwDefaultFrameInterval;
238         uvc_trace(UVC_TRACE_FORMAT, "Using default frame interval %u.%u us "
239                 "(%u.%u fps).\n", interval/10, interval%10, 10000000/interval,
240                 (100000000/interval)%10);
241
242         /* Set the format index, frame index and frame interval. */
243         memset(probe, 0, sizeof *probe);
244         probe->bmHint = 1;      /* dwFrameInterval */
245         probe->bFormatIndex = format->index;
246         probe->bFrameIndex = frame->bFrameIndex;
247         probe->dwFrameInterval = uvc_try_frame_interval(frame, interval);
248         /* Some webcams stall the probe control set request when the
249          * dwMaxVideoFrameSize field is set to zero. The UVC specification
250          * clearly states that the field is read-only from the host, so this
251          * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by
252          * the webcam to work around the problem.
253          *
254          * The workaround could probably be enabled for all webcams, so the
255          * quirk can be removed if needed. It's currently useful to detect
256          * webcam bugs and fix them before they hit the market (providing
257          * developers test their webcams with the Linux driver as well as with
258          * the Windows driver).
259          */
260         if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS)
261                 probe->dwMaxVideoFrameSize =
262                         stream->ctrl.dwMaxVideoFrameSize;
263
264         /* Probe the device. */
265         ret = uvc_probe_video(stream, probe);
266         if (ret < 0)
267                 goto done;
268
269         fmt->fmt.pix.width = frame->wWidth;
270         fmt->fmt.pix.height = frame->wHeight;
271         fmt->fmt.pix.field = V4L2_FIELD_NONE;
272         fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
273         fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize;
274         fmt->fmt.pix.colorspace = format->colorspace;
275         fmt->fmt.pix.priv = 0;
276
277         if (uvc_format != NULL)
278                 *uvc_format = format;
279         if (uvc_frame != NULL)
280                 *uvc_frame = frame;
281
282 done:
283         return ret;
284 }
285
286 static int uvc_v4l2_get_format(struct uvc_streaming *stream,
287         struct v4l2_format *fmt)
288 {
289         struct uvc_format *format = stream->cur_format;
290         struct uvc_frame *frame = stream->cur_frame;
291
292         if (fmt->type != stream->type)
293                 return -EINVAL;
294
295         if (format == NULL || frame == NULL)
296                 return -EINVAL;
297
298         fmt->fmt.pix.pixelformat = format->fcc;
299         fmt->fmt.pix.width = frame->wWidth;
300         fmt->fmt.pix.height = frame->wHeight;
301         fmt->fmt.pix.field = V4L2_FIELD_NONE;
302         fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
303         fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize;
304         fmt->fmt.pix.colorspace = format->colorspace;
305         fmt->fmt.pix.priv = 0;
306
307         return 0;
308 }
309
310 static int uvc_v4l2_set_format(struct uvc_streaming *stream,
311         struct v4l2_format *fmt)
312 {
313         struct uvc_streaming_control probe;
314         struct uvc_format *format;
315         struct uvc_frame *frame;
316         int ret;
317
318         if (fmt->type != stream->type) {
319         printk("uvc_v4l2_set_format, fmt->type(%d) != stream->type(%d)\n",fmt->type,stream->type);
320                 return -EINVAL;
321         }
322
323         if (uvc_queue_allocated(&stream->queue)) {
324         printk("uvc_queue_allocated failed\n");
325                 return -EBUSY;
326
327         }
328
329         ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame);
330         if (ret < 0)
331                 return ret;
332
333         memcpy(&stream->ctrl, &probe, sizeof probe);
334         stream->cur_format = format;
335         stream->cur_frame = frame;
336
337         return 0;
338 }
339
340 static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream,
341                 struct v4l2_streamparm *parm)
342 {
343         uint32_t numerator, denominator;
344
345         if (parm->type != stream->type)
346                 return -EINVAL;
347
348         numerator = stream->ctrl.dwFrameInterval;
349         denominator = 10000000;
350         uvc_simplify_fraction(&numerator, &denominator, 8, 333);
351
352         memset(parm, 0, sizeof *parm);
353         parm->type = stream->type;
354
355         if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
356                 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
357                 parm->parm.capture.capturemode = 0;
358                 parm->parm.capture.timeperframe.numerator = numerator;
359                 parm->parm.capture.timeperframe.denominator = denominator;
360                 parm->parm.capture.extendedmode = 0;
361                 parm->parm.capture.readbuffers = 0;
362         } else {
363                 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
364                 parm->parm.output.outputmode = 0;
365                 parm->parm.output.timeperframe.numerator = numerator;
366                 parm->parm.output.timeperframe.denominator = denominator;
367         }
368
369         return 0;
370 }
371
372 static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
373                 struct v4l2_streamparm *parm)
374 {
375         struct uvc_frame *frame = stream->cur_frame;
376         struct uvc_streaming_control probe;
377         struct v4l2_fract timeperframe;
378         uint32_t interval;
379         int ret;
380
381         if (parm->type != stream->type)
382                 return -EINVAL;
383
384         if (uvc_queue_streaming(&stream->queue))
385                 return -EBUSY;
386
387         if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
388                 timeperframe = parm->parm.capture.timeperframe;
389         else
390                 timeperframe = parm->parm.output.timeperframe;
391
392         memcpy(&probe, &stream->ctrl, sizeof probe);
393         interval = uvc_fraction_to_interval(timeperframe.numerator,
394                 timeperframe.denominator);
395
396         uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
397                 timeperframe.numerator, timeperframe.denominator, interval);
398         probe.dwFrameInterval = uvc_try_frame_interval(frame, interval);
399
400         /* Probe the device with the new settings. */
401         ret = uvc_probe_video(stream, &probe);
402         if (ret < 0)
403                 return ret;
404
405         memcpy(&stream->ctrl, &probe, sizeof probe);
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                 mutex_lock(&stream->queue.mutex);
534                 if (uvc_free_buffers(&stream->queue) < 0)
535                         uvc_printk(KERN_ERR, "uvc_v4l2_release: Unable to "
536                                         "free buffers.\n");
537                 mutex_unlock(&stream->queue.mutex);
538         }
539
540         /* Release the file handle. */
541         uvc_dismiss_privileges(handle);
542         kfree(handle);
543         file->private_data = NULL;
544
545         if (atomic_dec_return(&stream->dev->users) == 0)
546                 uvc_status_stop(stream->dev);
547
548         usb_autopm_put_interface(stream->dev->intf);
549         return 0;
550 }
551
552 static long uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
553 {
554         struct video_device *vdev = video_devdata(file);
555         struct uvc_fh *handle = file->private_data;
556         struct uvc_video_chain *chain = handle->chain;
557         struct uvc_streaming *stream = handle->stream;
558         long ret = 0;
559
560         switch (cmd) {
561         /* Query capabilities */
562         case VIDIOC_QUERYCAP:
563         {
564                 struct v4l2_capability *cap = arg;
565
566                 memset(cap, 0, sizeof *cap);
567                 strlcpy(cap->driver, "uvcvideo", sizeof cap->driver);
568                 strlcpy(cap->card, vdev->name, sizeof cap->card);
569                 usb_make_path(stream->dev->udev,
570                               cap->bus_info, sizeof(cap->bus_info));
571                 cap->version = DRIVER_VERSION_NUMBER;
572                 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
573                         cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
574                                           | V4L2_CAP_STREAMING;
575                 else
576                         cap->capabilities = V4L2_CAP_VIDEO_OUTPUT
577                                           | V4L2_CAP_STREAMING;
578                 break;
579         }
580
581         /* Get, Set & Query control */
582         case VIDIOC_QUERYCTRL:
583                 return uvc_query_v4l2_ctrl(chain, arg);
584
585         case VIDIOC_G_CTRL:
586         {
587                 struct v4l2_control *ctrl = arg;
588                 struct v4l2_ext_control xctrl;
589
590                 memset(&xctrl, 0, sizeof xctrl);
591                 xctrl.id = ctrl->id;
592
593                 ret = uvc_ctrl_begin(chain);
594                 if (ret < 0)
595                         return ret;
596
597                 ret = uvc_ctrl_get(chain, &xctrl);
598                 uvc_ctrl_rollback(chain);
599                 if (ret >= 0)
600                         ctrl->value = xctrl.value;
601                 break;
602         }
603
604         case VIDIOC_S_CTRL:
605         {
606                 struct v4l2_control *ctrl = arg;
607                 struct v4l2_ext_control xctrl;
608
609                 memset(&xctrl, 0, sizeof xctrl);
610                 xctrl.id = ctrl->id;
611                 xctrl.value = ctrl->value;
612
613                 ret = uvc_ctrl_begin(chain);
614                 if (ret < 0)
615                         return ret;
616
617                 ret = uvc_ctrl_set(chain, &xctrl);
618                 if (ret < 0) {
619                         uvc_ctrl_rollback(chain);
620                         return ret;
621                 }
622                 ret = uvc_ctrl_commit(chain);
623                 if (ret == 0)
624                         ctrl->value = xctrl.value;
625                 break;
626         }
627
628         case VIDIOC_QUERYMENU:
629                 return uvc_v4l2_query_menu(chain, arg);
630
631         case VIDIOC_G_EXT_CTRLS:
632         {
633                 struct v4l2_ext_controls *ctrls = arg;
634                 struct v4l2_ext_control *ctrl = ctrls->controls;
635                 unsigned int i;
636
637                 ret = uvc_ctrl_begin(chain);
638                 if (ret < 0)
639                         return ret;
640
641                 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
642                         ret = uvc_ctrl_get(chain, ctrl);
643                         if (ret < 0) {
644                                 uvc_ctrl_rollback(chain);
645                                 ctrls->error_idx = i;
646                                 return ret;
647                         }
648                 }
649                 ctrls->error_idx = 0;
650                 ret = uvc_ctrl_rollback(chain);
651                 break;
652         }
653
654         case VIDIOC_S_EXT_CTRLS:
655         case VIDIOC_TRY_EXT_CTRLS:
656         {
657                 struct v4l2_ext_controls *ctrls = arg;
658                 struct v4l2_ext_control *ctrl = ctrls->controls;
659                 unsigned int i;
660
661                 ret = uvc_ctrl_begin(chain);
662                 if (ret < 0)
663                         return ret;
664
665                 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
666                         ret = uvc_ctrl_set(chain, ctrl);
667                         if (ret < 0) {
668                                 uvc_ctrl_rollback(chain);
669                                 ctrls->error_idx = i;
670                                 return ret;
671                         }
672                 }
673
674                 ctrls->error_idx = 0;
675
676                 if (cmd == VIDIOC_S_EXT_CTRLS)
677                         ret = uvc_ctrl_commit(chain);
678                 else
679                         ret = uvc_ctrl_rollback(chain);
680                 break;
681         }
682
683         /* Get, Set & Enum input */
684         case VIDIOC_ENUMINPUT:
685         {
686                 const struct uvc_entity *selector = chain->selector;
687                 struct v4l2_input *input = arg;
688                 struct uvc_entity *iterm = NULL;
689                 u32 index = input->index;
690                 int pin = 0;
691
692                 if (selector == NULL ||
693                     (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
694                         if (index != 0)
695                                 return -EINVAL;
696                         list_for_each_entry(iterm, &chain->entities, chain) {
697                                 if (UVC_ENTITY_IS_ITERM(iterm))
698                                         break;
699                         }
700                         pin = iterm->id;
701                 } else if (pin < selector->bNrInPins) {
702                         pin = selector->baSourceID[index];
703                         list_for_each_entry(iterm, &chain->entities, chain) {
704                                 if (!UVC_ENTITY_IS_ITERM(iterm))
705                                         continue;
706                                 if (iterm->id == pin)
707                                         break;
708                         }
709                 }
710
711                 if (iterm == NULL || iterm->id != pin)
712                         return -EINVAL;
713
714                 memset(input, 0, sizeof *input);
715                 input->index = index;
716                 strlcpy(input->name, iterm->name, sizeof input->name);
717                 if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
718                         input->type = V4L2_INPUT_TYPE_CAMERA;
719                 break;
720         }
721
722         case VIDIOC_G_INPUT:
723         {
724                 u8 input;
725
726                 if (chain->selector == NULL ||
727                     (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
728                         *(int *)arg = 0;
729                         break;
730                 }
731
732                 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
733                         chain->selector->id, chain->dev->intfnum,
734                         UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
735                 if (ret < 0)
736                         return ret;
737
738                 *(int *)arg = input - 1;
739                 break;
740         }
741
742         case VIDIOC_S_INPUT:
743         {
744                 u32 input = *(u32 *)arg + 1;
745
746                 if ((ret = uvc_acquire_privileges(handle)) < 0)
747                         return ret;
748
749                 if (chain->selector == NULL ||
750                     (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
751                         if (input != 1)
752                                 return -EINVAL;
753                         break;
754                 }
755
756                 if (input == 0 || input > chain->selector->bNrInPins)
757                         return -EINVAL;
758
759                 return uvc_query_ctrl(chain->dev, UVC_SET_CUR,
760                         chain->selector->id, chain->dev->intfnum,
761                         UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
762         }
763
764         /* Try, Get, Set & Enum format */
765         case VIDIOC_ENUM_FMT:
766         {
767                 struct v4l2_fmtdesc *fmt = arg;
768                 struct uvc_format *format;
769                 enum v4l2_buf_type type = fmt->type;
770                 __u32 index = fmt->index;
771
772                 if (fmt->type != stream->type ||
773                     fmt->index >= stream->nformats)
774                         return -EINVAL;
775
776                 memset(fmt, 0, sizeof(*fmt));
777                 fmt->index = index;
778                 fmt->type = type;
779
780                 format = &stream->format[fmt->index];
781                 fmt->flags = 0;
782                 if (format->flags & UVC_FMT_FLAG_COMPRESSED)
783                         fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
784                 strlcpy(fmt->description, format->name,
785                         sizeof fmt->description);
786                 fmt->description[sizeof fmt->description - 1] = 0;
787                 fmt->pixelformat = format->fcc;
788                 break;
789         }
790
791         case VIDIOC_TRY_FMT:
792         {
793                 struct uvc_streaming_control probe;
794
795                 return uvc_v4l2_try_format(stream, arg, &probe, NULL, NULL);
796         }
797
798         case VIDIOC_S_FMT:
799                 if ((ret = uvc_acquire_privileges(handle)) < 0) {
800             printk("uvc_acquire_privileges error.");
801                         return ret;
802                 }
803
804                 return uvc_v4l2_set_format(stream, arg);
805
806         case VIDIOC_G_FMT:
807                 return uvc_v4l2_get_format(stream, arg);
808
809         /* Frame size enumeration */
810         case VIDIOC_ENUM_FRAMESIZES:
811         {
812                 struct v4l2_frmsizeenum *fsize = arg;
813                 struct uvc_format *format = NULL;
814                 struct uvc_frame *frame;
815                 int i;
816
817                 /* Look for the given pixel format */
818                 for (i = 0; i < stream->nformats; i++) {
819                         if (stream->format[i].fcc ==
820                                         fsize->pixel_format) {
821                                 format = &stream->format[i];
822                                 break;
823                         }
824                 }
825                 if (format == NULL)
826                         return -EINVAL;
827
828                 if (fsize->index >= format->nframes)
829                         return -EINVAL;
830
831                 frame = &format->frame[fsize->index];
832                 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
833                 fsize->discrete.width = frame->wWidth;
834                 fsize->discrete.height = frame->wHeight;
835                 break;
836         }
837
838         /* Frame interval enumeration */
839         case VIDIOC_ENUM_FRAMEINTERVALS:
840         {
841                 struct v4l2_frmivalenum *fival = arg;
842                 struct uvc_format *format = NULL;
843                 struct uvc_frame *frame = NULL;
844                 int i;
845
846                 /* Look for the given pixel format and frame size */
847                 for (i = 0; i < stream->nformats; i++) {
848                         if (stream->format[i].fcc ==
849                                         fival->pixel_format) {
850                                 format = &stream->format[i];
851                                 break;
852                         }
853                 }
854                 if (format == NULL)
855                         return -EINVAL;
856
857                 for (i = 0; i < format->nframes; i++) {
858                         if (format->frame[i].wWidth == fival->width &&
859                             format->frame[i].wHeight == fival->height) {
860                                 frame = &format->frame[i];
861                                 break;
862                         }
863                 }
864                 if (frame == NULL)
865                         return -EINVAL;
866
867                 if (frame->bFrameIntervalType) {
868                         if (fival->index >= frame->bFrameIntervalType)
869                                 return -EINVAL;
870
871                         fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
872                         fival->discrete.numerator =
873                                 frame->dwFrameInterval[fival->index];
874                         fival->discrete.denominator = 10000000;
875                         uvc_simplify_fraction(&fival->discrete.numerator,
876                                 &fival->discrete.denominator, 8, 333);
877                 } else {
878                         fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
879                         fival->stepwise.min.numerator =
880                                 frame->dwFrameInterval[0];
881                         fival->stepwise.min.denominator = 10000000;
882                         fival->stepwise.max.numerator =
883                                 frame->dwFrameInterval[1];
884                         fival->stepwise.max.denominator = 10000000;
885                         fival->stepwise.step.numerator =
886                                 frame->dwFrameInterval[2];
887                         fival->stepwise.step.denominator = 10000000;
888                         uvc_simplify_fraction(&fival->stepwise.min.numerator,
889                                 &fival->stepwise.min.denominator, 8, 333);
890                         uvc_simplify_fraction(&fival->stepwise.max.numerator,
891                                 &fival->stepwise.max.denominator, 8, 333);
892                         uvc_simplify_fraction(&fival->stepwise.step.numerator,
893                                 &fival->stepwise.step.denominator, 8, 333);
894                 }
895                 break;
896         }
897
898         /* Get & Set streaming parameters */
899         case VIDIOC_G_PARM:
900                 return uvc_v4l2_get_streamparm(stream, arg);
901
902         case VIDIOC_S_PARM:
903                 if ((ret = uvc_acquire_privileges(handle)) < 0)
904                         return ret;
905
906                 return uvc_v4l2_set_streamparm(stream, arg);
907
908         /* Cropping and scaling */
909         case VIDIOC_CROPCAP:
910         {
911                 struct v4l2_cropcap *ccap = arg;
912                 struct uvc_frame *frame = stream->cur_frame;
913
914                 if (ccap->type != stream->type)
915                         return -EINVAL;
916
917                 ccap->bounds.left = 0;
918                 ccap->bounds.top = 0;
919                 ccap->bounds.width = frame->wWidth;
920                 ccap->bounds.height = frame->wHeight;
921
922                 ccap->defrect = ccap->bounds;
923
924                 ccap->pixelaspect.numerator = 1;
925                 ccap->pixelaspect.denominator = 1;
926                 break;
927         }
928
929         case VIDIOC_G_CROP:
930         case VIDIOC_S_CROP:
931                 return -EINVAL;
932
933         /* Buffers & streaming */
934         case VIDIOC_REQBUFS:
935         {
936                 struct v4l2_requestbuffers *rb = arg;
937                 unsigned int bufsize =
938                         stream->ctrl.dwMaxVideoFrameSize;
939
940                 if (rb->type != stream->type ||
941                     rb->memory != V4L2_MEMORY_MMAP)
942                         return -EINVAL;
943
944                 if ((ret = uvc_acquire_privileges(handle)) < 0)
945                         return ret;
946
947                 ret = uvc_alloc_buffers(&stream->queue, rb->count, bufsize);
948                 if (ret < 0)
949                         return ret;
950
951                 if (ret == 0)
952                         uvc_dismiss_privileges(handle);
953
954                 rb->count = ret;
955                 ret = 0;
956                 break;
957         }
958
959         case VIDIOC_QUERYBUF:
960         {
961                 struct v4l2_buffer *buf = arg;
962
963                 if (buf->type != stream->type)
964                         return -EINVAL;
965
966                 if (!uvc_has_privileges(handle))
967                         return -EBUSY;
968
969                 return uvc_query_buffer(&stream->queue, buf);
970         }
971
972         case VIDIOC_QBUF:
973                 if (!uvc_has_privileges(handle)) {
974             printk("uvcvideo: VIDIOC_QBUF uvc_has_privileges failed\n");
975                         return -EBUSY;
976                 }
977
978                 return uvc_queue_buffer(&stream->queue, arg);
979
980         case VIDIOC_DQBUF:
981                 if (!uvc_has_privileges(handle)) {
982             printk("uvcvideo: VIDIOC_DQBUF uvc_has_privileges failed\n");
983                         return -EBUSY;
984                 }
985
986                 return uvc_dequeue_buffer(&stream->queue, arg,
987                         file->f_flags & O_NONBLOCK);
988
989         case VIDIOC_STREAMON:
990         {
991                 int *type = arg;
992
993                 if (*type != stream->type)
994                         return -EINVAL;
995
996                 if (!uvc_has_privileges(handle))
997                         return -EBUSY;
998
999                 ret = uvc_video_enable(stream, 1);
1000                 if (ret < 0)
1001                         return ret;
1002                 break;
1003         }
1004
1005         case VIDIOC_STREAMOFF:
1006         {
1007                 int *type = arg;
1008
1009                 if (*type != stream->type)
1010                         return -EINVAL;
1011
1012                 if (!uvc_has_privileges(handle))
1013                         return -EBUSY;
1014
1015                 return uvc_video_enable(stream, 0);
1016         }
1017
1018         /* Analog video standards make no sense for digital cameras. */
1019         case VIDIOC_ENUMSTD:
1020         case VIDIOC_QUERYSTD:
1021         case VIDIOC_G_STD:
1022         case VIDIOC_S_STD:
1023
1024         case VIDIOC_OVERLAY:
1025
1026         case VIDIOC_ENUMAUDIO:
1027         case VIDIOC_ENUMAUDOUT:
1028
1029         case VIDIOC_ENUMOUTPUT:
1030                 uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd);
1031                 return -EINVAL;
1032
1033         /* Dynamic controls. */
1034         case UVCIOC_CTRL_ADD:
1035         {
1036                 struct uvc_xu_control_info *xinfo = arg;
1037                 struct uvc_control_info *info;
1038
1039                 if (!capable(CAP_SYS_ADMIN))
1040                         return -EPERM;
1041
1042                 if (xinfo->size == 0)
1043                         return -EINVAL;
1044
1045                 info = kzalloc(sizeof *info, GFP_KERNEL);
1046                 if (info == NULL)
1047                         return -ENOMEM;
1048
1049                 memcpy(info->entity, xinfo->entity, sizeof info->entity);
1050                 info->index = xinfo->index;
1051                 info->selector = xinfo->selector;
1052                 info->size = xinfo->size;
1053                 info->flags = xinfo->flags;
1054
1055                 info->flags |= UVC_CONTROL_GET_MIN | UVC_CONTROL_GET_MAX |
1056                                UVC_CONTROL_GET_RES | UVC_CONTROL_GET_DEF |
1057                                UVC_CONTROL_EXTENSION;
1058
1059                 ret = uvc_ctrl_add_info(info);
1060                 if (ret < 0)
1061                         kfree(info);
1062                 break;
1063         }
1064
1065         case UVCIOC_CTRL_MAP_OLD:
1066         case UVCIOC_CTRL_MAP:
1067                 if (!capable(CAP_SYS_ADMIN))
1068                         return -EPERM;
1069
1070                 return uvc_ioctl_ctrl_map(arg, cmd == UVCIOC_CTRL_MAP_OLD);
1071
1072         case UVCIOC_CTRL_GET:
1073                 return uvc_xu_ctrl_query(chain, arg, 0);
1074
1075         case UVCIOC_CTRL_SET:
1076                 return uvc_xu_ctrl_query(chain, arg, 1);
1077
1078         default:
1079                 if ((ret = v4l_compat_translate_ioctl(file, cmd, arg,
1080                         uvc_v4l2_do_ioctl)) == -ENOIOCTLCMD)
1081                         uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n",
1082                                   cmd);
1083                 return ret;
1084         }
1085
1086         return ret;
1087 }
1088
1089 static long uvc_v4l2_ioctl(struct file *file,
1090                      unsigned int cmd, unsigned long arg)
1091 {
1092         if (uvc_trace_param & UVC_TRACE_IOCTL) {
1093                 uvc_printk(KERN_DEBUG, "uvc_v4l2_ioctl(");
1094                 v4l_printk_ioctl(cmd);
1095                 printk(")\n");
1096         }
1097
1098         return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl);
1099 }
1100
1101 static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1102                     size_t count, loff_t *ppos)
1103 {
1104         uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
1105         return -EINVAL;
1106 }
1107
1108 /*
1109  * VMA operations.
1110  */
1111 static void uvc_vm_open(struct vm_area_struct *vma)
1112 {
1113         struct uvc_buffer *buffer = vma->vm_private_data;
1114         buffer->vma_use_count++;
1115 }
1116
1117 static void uvc_vm_close(struct vm_area_struct *vma)
1118 {
1119         struct uvc_buffer *buffer = vma->vm_private_data;
1120         buffer->vma_use_count--;
1121 }
1122
1123 static const struct vm_operations_struct uvc_vm_ops = {
1124         .open           = uvc_vm_open,
1125         .close          = uvc_vm_close,
1126 };
1127
1128 static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1129 {
1130         struct uvc_fh *handle = file->private_data;
1131         struct uvc_streaming *stream = handle->stream;
1132         struct uvc_video_queue *queue = &stream->queue;
1133         struct uvc_buffer *uninitialized_var(buffer);
1134         struct page *page;
1135         unsigned long addr, start, size;
1136         unsigned int i;
1137         int ret = 0;
1138
1139         uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
1140
1141         start = vma->vm_start;
1142         size = vma->vm_end - vma->vm_start;
1143
1144         mutex_lock(&queue->mutex);
1145
1146         for (i = 0; i < queue->count; ++i) {
1147                 buffer = &queue->buffer[i];
1148                 if ((buffer->buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
1149                         break;
1150         }
1151
1152         if (i == queue->count || size != queue->buf_size) {
1153                 ret = -EINVAL;
1154                 goto done;
1155         }
1156
1157         /*
1158          * VM_IO marks the area as being an mmaped region for I/O to a
1159          * device. It also prevents the region from being core dumped.
1160          */
1161         vma->vm_flags |= VM_IO;
1162
1163         addr = (unsigned long)queue->mem + buffer->buf.m.offset;
1164         while (size > 0) {
1165                 page = vmalloc_to_page((void *)addr);
1166                 if ((ret = vm_insert_page(vma, start, page)) < 0)
1167                         goto done;
1168
1169                 start += PAGE_SIZE;
1170                 addr += PAGE_SIZE;
1171                 size -= PAGE_SIZE;
1172         }
1173
1174         vma->vm_ops = &uvc_vm_ops;
1175         vma->vm_private_data = buffer;
1176         uvc_vm_open(vma);
1177
1178 done:
1179         mutex_unlock(&queue->mutex);
1180         return ret;
1181 }
1182
1183 static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
1184 {
1185         struct uvc_fh *handle = file->private_data;
1186         struct uvc_streaming *stream = handle->stream;
1187
1188         uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
1189
1190         return uvc_queue_poll(&stream->queue, file, wait);
1191 }
1192
1193 const struct v4l2_file_operations uvc_fops = {
1194         .owner          = THIS_MODULE,
1195         .open           = uvc_v4l2_open,
1196         .release        = uvc_v4l2_release,
1197         .ioctl          = uvc_v4l2_ioctl,
1198         .read           = uvc_v4l2_read,
1199         .mmap           = uvc_v4l2_mmap,
1200         .poll           = uvc_v4l2_poll,
1201 };
1202