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