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