]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/media/video/uvc/uvc_v4l2.c
23239a4adefe092d0ed1a2e026499215613595d5
[net-next-2.6.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                 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                 break;
553         }
554
555         case VIDIOC_QUERYMENU:
556                 return uvc_v4l2_query_menu(chain, arg);
557
558         case VIDIOC_G_EXT_CTRLS:
559         {
560                 struct v4l2_ext_controls *ctrls = arg;
561                 struct v4l2_ext_control *ctrl = ctrls->controls;
562                 unsigned int i;
563
564                 ret = uvc_ctrl_begin(chain);
565                 if (ret < 0)
566                         return ret;
567
568                 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
569                         ret = uvc_ctrl_get(chain, ctrl);
570                         if (ret < 0) {
571                                 uvc_ctrl_rollback(chain);
572                                 ctrls->error_idx = i;
573                                 return ret;
574                         }
575                 }
576                 ctrls->error_idx = 0;
577                 ret = uvc_ctrl_rollback(chain);
578                 break;
579         }
580
581         case VIDIOC_S_EXT_CTRLS:
582         case VIDIOC_TRY_EXT_CTRLS:
583         {
584                 struct v4l2_ext_controls *ctrls = arg;
585                 struct v4l2_ext_control *ctrl = ctrls->controls;
586                 unsigned int i;
587
588                 ret = uvc_ctrl_begin(chain);
589                 if (ret < 0)
590                         return ret;
591
592                 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
593                         ret = uvc_ctrl_set(chain, ctrl);
594                         if (ret < 0) {
595                                 uvc_ctrl_rollback(chain);
596                                 ctrls->error_idx = i;
597                                 return ret;
598                         }
599                 }
600
601                 ctrls->error_idx = 0;
602
603                 if (cmd == VIDIOC_S_EXT_CTRLS)
604                         ret = uvc_ctrl_commit(chain);
605                 else
606                         ret = uvc_ctrl_rollback(chain);
607                 break;
608         }
609
610         /* Get, Set & Enum input */
611         case VIDIOC_ENUMINPUT:
612         {
613                 const struct uvc_entity *selector = chain->selector;
614                 struct v4l2_input *input = arg;
615                 struct uvc_entity *iterm = NULL;
616                 u32 index = input->index;
617                 int pin = 0;
618
619                 if (selector == NULL ||
620                     (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
621                         if (index != 0)
622                                 return -EINVAL;
623                         list_for_each_entry(iterm, &chain->entities, chain) {
624                                 if (UVC_ENTITY_IS_ITERM(iterm))
625                                         break;
626                         }
627                         pin = iterm->id;
628                 } else if (pin < selector->bNrInPins) {
629                         pin = selector->baSourceID[index];
630                         list_for_each_entry(iterm, &chain->entities, chain) {
631                                 if (!UVC_ENTITY_IS_ITERM(iterm))
632                                         continue;
633                                 if (iterm->id == pin)
634                                         break;
635                         }
636                 }
637
638                 if (iterm == NULL || iterm->id != pin)
639                         return -EINVAL;
640
641                 memset(input, 0, sizeof *input);
642                 input->index = index;
643                 strlcpy(input->name, iterm->name, sizeof input->name);
644                 if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
645                         input->type = V4L2_INPUT_TYPE_CAMERA;
646                 break;
647         }
648
649         case VIDIOC_G_INPUT:
650         {
651                 u8 input;
652
653                 if (chain->selector == NULL ||
654                     (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
655                         *(int *)arg = 0;
656                         break;
657                 }
658
659                 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
660                         chain->selector->id, chain->dev->intfnum,
661                         UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
662                 if (ret < 0)
663                         return ret;
664
665                 *(int *)arg = input - 1;
666                 break;
667         }
668
669         case VIDIOC_S_INPUT:
670         {
671                 u32 input = *(u32 *)arg + 1;
672
673                 if ((ret = uvc_acquire_privileges(handle)) < 0)
674                         return ret;
675
676                 if (chain->selector == NULL ||
677                     (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
678                         if (input != 1)
679                                 return -EINVAL;
680                         break;
681                 }
682
683                 if (input == 0 || input > chain->selector->bNrInPins)
684                         return -EINVAL;
685
686                 return uvc_query_ctrl(chain->dev, UVC_SET_CUR,
687                         chain->selector->id, chain->dev->intfnum,
688                         UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
689         }
690
691         /* Try, Get, Set & Enum format */
692         case VIDIOC_ENUM_FMT:
693         {
694                 struct v4l2_fmtdesc *fmt = arg;
695                 struct uvc_format *format;
696                 enum v4l2_buf_type type = fmt->type;
697                 __u32 index = fmt->index;
698
699                 if (fmt->type != stream->type ||
700                     fmt->index >= stream->nformats)
701                         return -EINVAL;
702
703                 memset(fmt, 0, sizeof(*fmt));
704                 fmt->index = index;
705                 fmt->type = type;
706
707                 format = &stream->format[fmt->index];
708                 fmt->flags = 0;
709                 if (format->flags & UVC_FMT_FLAG_COMPRESSED)
710                         fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
711                 strlcpy(fmt->description, format->name,
712                         sizeof fmt->description);
713                 fmt->description[sizeof fmt->description - 1] = 0;
714                 fmt->pixelformat = format->fcc;
715                 break;
716         }
717
718         case VIDIOC_TRY_FMT:
719         {
720                 struct uvc_streaming_control probe;
721
722                 return uvc_v4l2_try_format(stream, arg, &probe, NULL, NULL);
723         }
724
725         case VIDIOC_S_FMT:
726                 if ((ret = uvc_acquire_privileges(handle)) < 0)
727                         return ret;
728
729                 return uvc_v4l2_set_format(stream, arg);
730
731         case VIDIOC_G_FMT:
732                 return uvc_v4l2_get_format(stream, arg);
733
734         /* Frame size enumeration */
735         case VIDIOC_ENUM_FRAMESIZES:
736         {
737                 struct v4l2_frmsizeenum *fsize = arg;
738                 struct uvc_format *format = NULL;
739                 struct uvc_frame *frame;
740                 int i;
741
742                 /* Look for the given pixel format */
743                 for (i = 0; i < stream->nformats; i++) {
744                         if (stream->format[i].fcc ==
745                                         fsize->pixel_format) {
746                                 format = &stream->format[i];
747                                 break;
748                         }
749                 }
750                 if (format == NULL)
751                         return -EINVAL;
752
753                 if (fsize->index >= format->nframes)
754                         return -EINVAL;
755
756                 frame = &format->frame[fsize->index];
757                 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
758                 fsize->discrete.width = frame->wWidth;
759                 fsize->discrete.height = frame->wHeight;
760                 break;
761         }
762
763         /* Frame interval enumeration */
764         case VIDIOC_ENUM_FRAMEINTERVALS:
765         {
766                 struct v4l2_frmivalenum *fival = arg;
767                 struct uvc_format *format = NULL;
768                 struct uvc_frame *frame = NULL;
769                 int i;
770
771                 /* Look for the given pixel format and frame size */
772                 for (i = 0; i < stream->nformats; i++) {
773                         if (stream->format[i].fcc ==
774                                         fival->pixel_format) {
775                                 format = &stream->format[i];
776                                 break;
777                         }
778                 }
779                 if (format == NULL)
780                         return -EINVAL;
781
782                 for (i = 0; i < format->nframes; i++) {
783                         if (format->frame[i].wWidth == fival->width &&
784                             format->frame[i].wHeight == fival->height) {
785                                 frame = &format->frame[i];
786                                 break;
787                         }
788                 }
789                 if (frame == NULL)
790                         return -EINVAL;
791
792                 if (frame->bFrameIntervalType) {
793                         if (fival->index >= frame->bFrameIntervalType)
794                                 return -EINVAL;
795
796                         fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
797                         fival->discrete.numerator =
798                                 frame->dwFrameInterval[fival->index];
799                         fival->discrete.denominator = 10000000;
800                         uvc_simplify_fraction(&fival->discrete.numerator,
801                                 &fival->discrete.denominator, 8, 333);
802                 } else {
803                         fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
804                         fival->stepwise.min.numerator =
805                                 frame->dwFrameInterval[0];
806                         fival->stepwise.min.denominator = 10000000;
807                         fival->stepwise.max.numerator =
808                                 frame->dwFrameInterval[1];
809                         fival->stepwise.max.denominator = 10000000;
810                         fival->stepwise.step.numerator =
811                                 frame->dwFrameInterval[2];
812                         fival->stepwise.step.denominator = 10000000;
813                         uvc_simplify_fraction(&fival->stepwise.min.numerator,
814                                 &fival->stepwise.min.denominator, 8, 333);
815                         uvc_simplify_fraction(&fival->stepwise.max.numerator,
816                                 &fival->stepwise.max.denominator, 8, 333);
817                         uvc_simplify_fraction(&fival->stepwise.step.numerator,
818                                 &fival->stepwise.step.denominator, 8, 333);
819                 }
820                 break;
821         }
822
823         /* Get & Set streaming parameters */
824         case VIDIOC_G_PARM:
825                 return uvc_v4l2_get_streamparm(stream, arg);
826
827         case VIDIOC_S_PARM:
828                 if ((ret = uvc_acquire_privileges(handle)) < 0)
829                         return ret;
830
831                 return uvc_v4l2_set_streamparm(stream, arg);
832
833         /* Cropping and scaling */
834         case VIDIOC_CROPCAP:
835         {
836                 struct v4l2_cropcap *ccap = arg;
837                 struct uvc_frame *frame = stream->cur_frame;
838
839                 if (ccap->type != stream->type)
840                         return -EINVAL;
841
842                 ccap->bounds.left = 0;
843                 ccap->bounds.top = 0;
844                 ccap->bounds.width = frame->wWidth;
845                 ccap->bounds.height = frame->wHeight;
846
847                 ccap->defrect = ccap->bounds;
848
849                 ccap->pixelaspect.numerator = 1;
850                 ccap->pixelaspect.denominator = 1;
851                 break;
852         }
853
854         case VIDIOC_G_CROP:
855         case VIDIOC_S_CROP:
856                 return -EINVAL;
857
858         /* Buffers & streaming */
859         case VIDIOC_REQBUFS:
860         {
861                 struct v4l2_requestbuffers *rb = arg;
862                 unsigned int bufsize =
863                         stream->ctrl.dwMaxVideoFrameSize;
864
865                 if (rb->type != stream->type ||
866                     rb->memory != V4L2_MEMORY_MMAP)
867                         return -EINVAL;
868
869                 if ((ret = uvc_acquire_privileges(handle)) < 0)
870                         return ret;
871
872                 ret = uvc_alloc_buffers(&stream->queue, rb->count, bufsize);
873                 if (ret < 0)
874                         return ret;
875
876                 if (ret == 0)
877                         uvc_dismiss_privileges(handle);
878
879                 rb->count = ret;
880                 ret = 0;
881                 break;
882         }
883
884         case VIDIOC_QUERYBUF:
885         {
886                 struct v4l2_buffer *buf = arg;
887
888                 if (buf->type != stream->type)
889                         return -EINVAL;
890
891                 if (!uvc_has_privileges(handle))
892                         return -EBUSY;
893
894                 return uvc_query_buffer(&stream->queue, buf);
895         }
896
897         case VIDIOC_QBUF:
898                 if (!uvc_has_privileges(handle))
899                         return -EBUSY;
900
901                 return uvc_queue_buffer(&stream->queue, arg);
902
903         case VIDIOC_DQBUF:
904                 if (!uvc_has_privileges(handle))
905                         return -EBUSY;
906
907                 return uvc_dequeue_buffer(&stream->queue, arg,
908                         file->f_flags & O_NONBLOCK);
909
910         case VIDIOC_STREAMON:
911         {
912                 int *type = arg;
913
914                 if (*type != stream->type)
915                         return -EINVAL;
916
917                 if (!uvc_has_privileges(handle))
918                         return -EBUSY;
919
920                 ret = uvc_video_enable(stream, 1);
921                 if (ret < 0)
922                         return ret;
923                 break;
924         }
925
926         case VIDIOC_STREAMOFF:
927         {
928                 int *type = arg;
929
930                 if (*type != stream->type)
931                         return -EINVAL;
932
933                 if (!uvc_has_privileges(handle))
934                         return -EBUSY;
935
936                 return uvc_video_enable(stream, 0);
937         }
938
939         /* Analog video standards make no sense for digital cameras. */
940         case VIDIOC_ENUMSTD:
941         case VIDIOC_QUERYSTD:
942         case VIDIOC_G_STD:
943         case VIDIOC_S_STD:
944
945         case VIDIOC_OVERLAY:
946
947         case VIDIOC_ENUMAUDIO:
948         case VIDIOC_ENUMAUDOUT:
949
950         case VIDIOC_ENUMOUTPUT:
951                 uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd);
952                 return -EINVAL;
953
954         /* Dynamic controls. */
955         case UVCIOC_CTRL_ADD:
956         {
957                 struct uvc_xu_control_info *xinfo = arg;
958                 struct uvc_control_info *info;
959
960                 if (!capable(CAP_SYS_ADMIN))
961                         return -EPERM;
962
963                 info = kzalloc(sizeof *info, GFP_KERNEL);
964                 if (info == NULL)
965                         return -ENOMEM;
966
967                 memcpy(info->entity, xinfo->entity, sizeof info->entity);
968                 info->index = xinfo->index;
969                 info->selector = xinfo->selector;
970                 info->size = xinfo->size;
971                 info->flags = xinfo->flags;
972
973                 info->flags |= UVC_CONTROL_GET_MIN | UVC_CONTROL_GET_MAX |
974                                 UVC_CONTROL_GET_RES | UVC_CONTROL_GET_DEF;
975
976                 ret = uvc_ctrl_add_info(info);
977                 if (ret < 0)
978                         kfree(info);
979                 break;
980         }
981
982         case UVCIOC_CTRL_MAP:
983         {
984                 struct uvc_xu_control_mapping *xmap = arg;
985                 struct uvc_control_mapping *map;
986
987                 if (!capable(CAP_SYS_ADMIN))
988                         return -EPERM;
989
990                 map = kzalloc(sizeof *map, GFP_KERNEL);
991                 if (map == NULL)
992                         return -ENOMEM;
993
994                 map->id = xmap->id;
995                 memcpy(map->name, xmap->name, sizeof map->name);
996                 memcpy(map->entity, xmap->entity, sizeof map->entity);
997                 map->selector = xmap->selector;
998                 map->size = xmap->size;
999                 map->offset = xmap->offset;
1000                 map->v4l2_type = xmap->v4l2_type;
1001                 map->data_type = xmap->data_type;
1002
1003                 ret = uvc_ctrl_add_mapping(map);
1004                 if (ret < 0)
1005                         kfree(map);
1006                 break;
1007         }
1008
1009         case UVCIOC_CTRL_GET:
1010                 return uvc_xu_ctrl_query(chain, arg, 0);
1011
1012         case UVCIOC_CTRL_SET:
1013                 return uvc_xu_ctrl_query(chain, arg, 1);
1014
1015         default:
1016                 if ((ret = v4l_compat_translate_ioctl(file, cmd, arg,
1017                         uvc_v4l2_do_ioctl)) == -ENOIOCTLCMD)
1018                         uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n",
1019                                   cmd);
1020                 return ret;
1021         }
1022
1023         return ret;
1024 }
1025
1026 static long uvc_v4l2_ioctl(struct file *file,
1027                      unsigned int cmd, unsigned long arg)
1028 {
1029         if (uvc_trace_param & UVC_TRACE_IOCTL) {
1030                 uvc_printk(KERN_DEBUG, "uvc_v4l2_ioctl(");
1031                 v4l_printk_ioctl(cmd);
1032                 printk(")\n");
1033         }
1034
1035         return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl);
1036 }
1037
1038 static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1039                     size_t count, loff_t *ppos)
1040 {
1041         uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
1042         return -EINVAL;
1043 }
1044
1045 /*
1046  * VMA operations.
1047  */
1048 static void uvc_vm_open(struct vm_area_struct *vma)
1049 {
1050         struct uvc_buffer *buffer = vma->vm_private_data;
1051         buffer->vma_use_count++;
1052 }
1053
1054 static void uvc_vm_close(struct vm_area_struct *vma)
1055 {
1056         struct uvc_buffer *buffer = vma->vm_private_data;
1057         buffer->vma_use_count--;
1058 }
1059
1060 static const struct vm_operations_struct uvc_vm_ops = {
1061         .open           = uvc_vm_open,
1062         .close          = uvc_vm_close,
1063 };
1064
1065 static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1066 {
1067         struct uvc_fh *handle = (struct uvc_fh *)file->private_data;
1068         struct uvc_streaming *stream = handle->stream;
1069         struct uvc_video_queue *queue = &stream->queue;
1070         struct uvc_buffer *uninitialized_var(buffer);
1071         struct page *page;
1072         unsigned long addr, start, size;
1073         unsigned int i;
1074         int ret = 0;
1075
1076         uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
1077
1078         start = vma->vm_start;
1079         size = vma->vm_end - vma->vm_start;
1080
1081         mutex_lock(&queue->mutex);
1082
1083         for (i = 0; i < queue->count; ++i) {
1084                 buffer = &queue->buffer[i];
1085                 if ((buffer->buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
1086                         break;
1087         }
1088
1089         if (i == queue->count || size != queue->buf_size) {
1090                 ret = -EINVAL;
1091                 goto done;
1092         }
1093
1094         /*
1095          * VM_IO marks the area as being an mmaped region for I/O to a
1096          * device. It also prevents the region from being core dumped.
1097          */
1098         vma->vm_flags |= VM_IO;
1099
1100         addr = (unsigned long)queue->mem + buffer->buf.m.offset;
1101         while (size > 0) {
1102                 page = vmalloc_to_page((void *)addr);
1103                 if ((ret = vm_insert_page(vma, start, page)) < 0)
1104                         goto done;
1105
1106                 start += PAGE_SIZE;
1107                 addr += PAGE_SIZE;
1108                 size -= PAGE_SIZE;
1109         }
1110
1111         vma->vm_ops = &uvc_vm_ops;
1112         vma->vm_private_data = buffer;
1113         uvc_vm_open(vma);
1114
1115 done:
1116         mutex_unlock(&queue->mutex);
1117         return ret;
1118 }
1119
1120 static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
1121 {
1122         struct uvc_fh *handle = (struct uvc_fh *)file->private_data;
1123         struct uvc_streaming *stream = handle->stream;
1124
1125         uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
1126
1127         return uvc_queue_poll(&stream->queue, file, wait);
1128 }
1129
1130 const struct v4l2_file_operations uvc_fops = {
1131         .owner          = THIS_MODULE,
1132         .open           = uvc_v4l2_open,
1133         .release        = uvc_v4l2_release,
1134         .ioctl          = uvc_v4l2_ioctl,
1135         .read           = uvc_v4l2_read,
1136         .mmap           = uvc_v4l2_mmap,
1137         .poll           = uvc_v4l2_poll,
1138 };
1139