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