GNU Linux-libre 4.4.289-gnu1
[releases.git] / drivers / media / usb / uvc / uvc_v4l2.c
1 /*
2  *      uvc_v4l2.c  --  USB Video Class driver - V4L2 API
3  *
4  *      Copyright (C) 2005-2010
5  *          Laurent Pinchart (laurent.pinchart@ideasonboard.com)
6  *
7  *      This program is free software; you can redistribute it and/or modify
8  *      it under the terms of the GNU General Public License as published by
9  *      the Free Software Foundation; either version 2 of the License, or
10  *      (at your option) any later version.
11  *
12  */
13
14 #include <linux/compat.h>
15 #include <linux/kernel.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 <linux/atomic.h>
25
26 #include <media/v4l2-common.h>
27 #include <media/v4l2-ctrls.h>
28 #include <media/v4l2-event.h>
29 #include <media/v4l2-ioctl.h>
30
31 #include "uvcvideo.h"
32
33 /* ------------------------------------------------------------------------
34  * UVC ioctls
35  */
36 static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain,
37         struct uvc_xu_control_mapping *xmap)
38 {
39         struct uvc_control_mapping *map;
40         unsigned int size;
41         int ret;
42
43         map = kzalloc(sizeof *map, GFP_KERNEL);
44         if (map == NULL)
45                 return -ENOMEM;
46
47         map->id = xmap->id;
48         memcpy(map->name, xmap->name, sizeof map->name);
49         memcpy(map->entity, xmap->entity, sizeof map->entity);
50         map->selector = xmap->selector;
51         map->size = xmap->size;
52         map->offset = xmap->offset;
53         map->v4l2_type = xmap->v4l2_type;
54         map->data_type = xmap->data_type;
55
56         switch (xmap->v4l2_type) {
57         case V4L2_CTRL_TYPE_INTEGER:
58         case V4L2_CTRL_TYPE_BOOLEAN:
59         case V4L2_CTRL_TYPE_BUTTON:
60                 break;
61
62         case V4L2_CTRL_TYPE_MENU:
63                 /* Prevent excessive memory consumption, as well as integer
64                  * overflows.
65                  */
66                 if (xmap->menu_count == 0 ||
67                     xmap->menu_count > UVC_MAX_CONTROL_MENU_ENTRIES) {
68                         ret = -EINVAL;
69                         goto done;
70                 }
71
72                 size = xmap->menu_count * sizeof(*map->menu_info);
73                 map->menu_info = kmalloc(size, GFP_KERNEL);
74                 if (map->menu_info == NULL) {
75                         ret = -ENOMEM;
76                         goto done;
77                 }
78
79                 if (copy_from_user(map->menu_info, xmap->menu_info, size)) {
80                         ret = -EFAULT;
81                         goto done;
82                 }
83
84                 map->menu_count = xmap->menu_count;
85                 break;
86
87         default:
88                 uvc_trace(UVC_TRACE_CONTROL, "Unsupported V4L2 control type "
89                           "%u.\n", xmap->v4l2_type);
90                 ret = -ENOTTY;
91                 goto done;
92         }
93
94         ret = uvc_ctrl_add_mapping(chain, map);
95
96 done:
97         kfree(map->menu_info);
98         kfree(map);
99
100         return ret;
101 }
102
103 /* ------------------------------------------------------------------------
104  * V4L2 interface
105  */
106
107 /*
108  * Find the frame interval closest to the requested frame interval for the
109  * given frame format and size. This should be done by the device as part of
110  * the Video Probe and Commit negotiation, but some hardware don't implement
111  * that feature.
112  */
113 static __u32 uvc_try_frame_interval(struct uvc_frame *frame, __u32 interval)
114 {
115         unsigned int i;
116
117         if (frame->bFrameIntervalType) {
118                 __u32 best = -1, dist;
119
120                 for (i = 0; i < frame->bFrameIntervalType; ++i) {
121                         dist = interval > frame->dwFrameInterval[i]
122                              ? interval - frame->dwFrameInterval[i]
123                              : frame->dwFrameInterval[i] - interval;
124
125                         if (dist > best)
126                                 break;
127
128                         best = dist;
129                 }
130
131                 interval = frame->dwFrameInterval[i-1];
132         } else {
133                 const __u32 min = frame->dwFrameInterval[0];
134                 const __u32 max = frame->dwFrameInterval[1];
135                 const __u32 step = frame->dwFrameInterval[2];
136
137                 interval = min + (interval - min + step/2) / step * step;
138                 if (interval > max)
139                         interval = max;
140         }
141
142         return interval;
143 }
144
145 static int uvc_v4l2_try_format(struct uvc_streaming *stream,
146         struct v4l2_format *fmt, struct uvc_streaming_control *probe,
147         struct uvc_format **uvc_format, struct uvc_frame **uvc_frame)
148 {
149         struct uvc_format *format = NULL;
150         struct uvc_frame *frame = NULL;
151         __u16 rw, rh;
152         unsigned int d, maxd;
153         unsigned int i;
154         __u32 interval;
155         int ret = 0;
156         __u8 *fcc;
157
158         if (fmt->type != stream->type)
159                 return -EINVAL;
160
161         fcc = (__u8 *)&fmt->fmt.pix.pixelformat;
162         uvc_trace(UVC_TRACE_FORMAT, "Trying format 0x%08x (%c%c%c%c): %ux%u.\n",
163                         fmt->fmt.pix.pixelformat,
164                         fcc[0], fcc[1], fcc[2], fcc[3],
165                         fmt->fmt.pix.width, fmt->fmt.pix.height);
166
167         /* Check if the hardware supports the requested format, use the default
168          * format otherwise.
169          */
170         for (i = 0; i < stream->nformats; ++i) {
171                 format = &stream->format[i];
172                 if (format->fcc == fmt->fmt.pix.pixelformat)
173                         break;
174         }
175
176         if (i == stream->nformats) {
177                 format = stream->def_format;
178                 fmt->fmt.pix.pixelformat = format->fcc;
179         }
180
181         /* Find the closest image size. The distance between image sizes is
182          * the size in pixels of the non-overlapping regions between the
183          * requested size and the frame-specified size.
184          */
185         rw = fmt->fmt.pix.width;
186         rh = fmt->fmt.pix.height;
187         maxd = (unsigned int)-1;
188
189         for (i = 0; i < format->nframes; ++i) {
190                 __u16 w = format->frame[i].wWidth;
191                 __u16 h = format->frame[i].wHeight;
192
193                 d = min(w, rw) * min(h, rh);
194                 d = w*h + rw*rh - 2*d;
195                 if (d < maxd) {
196                         maxd = d;
197                         frame = &format->frame[i];
198                 }
199
200                 if (maxd == 0)
201                         break;
202         }
203
204         if (frame == NULL) {
205                 uvc_trace(UVC_TRACE_FORMAT, "Unsupported size %ux%u.\n",
206                                 fmt->fmt.pix.width, fmt->fmt.pix.height);
207                 return -EINVAL;
208         }
209
210         /* Use the default frame interval. */
211         interval = frame->dwDefaultFrameInterval;
212         uvc_trace(UVC_TRACE_FORMAT, "Using default frame interval %u.%u us "
213                 "(%u.%u fps).\n", interval/10, interval%10, 10000000/interval,
214                 (100000000/interval)%10);
215
216         /* Set the format index, frame index and frame interval. */
217         memset(probe, 0, sizeof *probe);
218         probe->bmHint = 1;      /* dwFrameInterval */
219         probe->bFormatIndex = format->index;
220         probe->bFrameIndex = frame->bFrameIndex;
221         probe->dwFrameInterval = uvc_try_frame_interval(frame, interval);
222         /* Some webcams stall the probe control set request when the
223          * dwMaxVideoFrameSize field is set to zero. The UVC specification
224          * clearly states that the field is read-only from the host, so this
225          * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by
226          * the webcam to work around the problem.
227          *
228          * The workaround could probably be enabled for all webcams, so the
229          * quirk can be removed if needed. It's currently useful to detect
230          * webcam bugs and fix them before they hit the market (providing
231          * developers test their webcams with the Linux driver as well as with
232          * the Windows driver).
233          */
234         mutex_lock(&stream->mutex);
235         if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS)
236                 probe->dwMaxVideoFrameSize =
237                         stream->ctrl.dwMaxVideoFrameSize;
238
239         /* Probe the device. */
240         ret = uvc_probe_video(stream, probe);
241         mutex_unlock(&stream->mutex);
242         if (ret < 0)
243                 goto done;
244
245         /* After the probe, update fmt with the values returned from
246          * negotiation with the device. Some devices return invalid bFormatIndex
247          * and bFrameIndex values, in which case we can only assume they have
248          * accepted the requested format as-is.
249          */
250         for (i = 0; i < stream->nformats; ++i) {
251                 if (probe->bFormatIndex == stream->format[i].index) {
252                         format = &stream->format[i];
253                         break;
254                 }
255         }
256
257         if (i == stream->nformats)
258                 uvc_trace(UVC_TRACE_FORMAT,
259                           "Unknown bFormatIndex %u, using default\n",
260                           probe->bFormatIndex);
261
262         for (i = 0; i < format->nframes; ++i) {
263                 if (probe->bFrameIndex == format->frame[i].bFrameIndex) {
264                         frame = &format->frame[i];
265                         break;
266                 }
267         }
268
269         if (i == format->nframes)
270                 uvc_trace(UVC_TRACE_FORMAT,
271                           "Unknown bFrameIndex %u, using default\n",
272                           probe->bFrameIndex);
273
274         fmt->fmt.pix.width = frame->wWidth;
275         fmt->fmt.pix.height = frame->wHeight;
276         fmt->fmt.pix.field = V4L2_FIELD_NONE;
277         fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
278         fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize;
279         fmt->fmt.pix.pixelformat = format->fcc;
280         fmt->fmt.pix.colorspace = format->colorspace;
281         fmt->fmt.pix.priv = 0;
282
283         if (uvc_format != NULL)
284                 *uvc_format = format;
285         if (uvc_frame != NULL)
286                 *uvc_frame = frame;
287
288 done:
289         return ret;
290 }
291
292 static int uvc_v4l2_get_format(struct uvc_streaming *stream,
293         struct v4l2_format *fmt)
294 {
295         struct uvc_format *format;
296         struct uvc_frame *frame;
297         int ret = 0;
298
299         if (fmt->type != stream->type)
300                 return -EINVAL;
301
302         mutex_lock(&stream->mutex);
303         format = stream->cur_format;
304         frame = stream->cur_frame;
305
306         if (format == NULL || frame == NULL) {
307                 ret = -EINVAL;
308                 goto done;
309         }
310
311         fmt->fmt.pix.pixelformat = format->fcc;
312         fmt->fmt.pix.width = frame->wWidth;
313         fmt->fmt.pix.height = frame->wHeight;
314         fmt->fmt.pix.field = V4L2_FIELD_NONE;
315         fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
316         fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize;
317         fmt->fmt.pix.colorspace = format->colorspace;
318         fmt->fmt.pix.priv = 0;
319
320 done:
321         mutex_unlock(&stream->mutex);
322         return ret;
323 }
324
325 static int uvc_v4l2_set_format(struct uvc_streaming *stream,
326         struct v4l2_format *fmt)
327 {
328         struct uvc_streaming_control probe;
329         struct uvc_format *format;
330         struct uvc_frame *frame;
331         int ret;
332
333         if (fmt->type != stream->type)
334                 return -EINVAL;
335
336         ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame);
337         if (ret < 0)
338                 return ret;
339
340         mutex_lock(&stream->mutex);
341
342         if (uvc_queue_allocated(&stream->queue)) {
343                 ret = -EBUSY;
344                 goto done;
345         }
346
347         stream->ctrl = probe;
348         stream->cur_format = format;
349         stream->cur_frame = frame;
350
351 done:
352         mutex_unlock(&stream->mutex);
353         return ret;
354 }
355
356 static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream,
357                 struct v4l2_streamparm *parm)
358 {
359         uint32_t numerator, denominator;
360
361         if (parm->type != stream->type)
362                 return -EINVAL;
363
364         mutex_lock(&stream->mutex);
365         numerator = stream->ctrl.dwFrameInterval;
366         mutex_unlock(&stream->mutex);
367
368         denominator = 10000000;
369         uvc_simplify_fraction(&numerator, &denominator, 8, 333);
370
371         memset(parm, 0, sizeof *parm);
372         parm->type = stream->type;
373
374         if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
375                 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
376                 parm->parm.capture.capturemode = 0;
377                 parm->parm.capture.timeperframe.numerator = numerator;
378                 parm->parm.capture.timeperframe.denominator = denominator;
379                 parm->parm.capture.extendedmode = 0;
380                 parm->parm.capture.readbuffers = 0;
381         } else {
382                 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
383                 parm->parm.output.outputmode = 0;
384                 parm->parm.output.timeperframe.numerator = numerator;
385                 parm->parm.output.timeperframe.denominator = denominator;
386         }
387
388         return 0;
389 }
390
391 static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
392                 struct v4l2_streamparm *parm)
393 {
394         struct uvc_streaming_control probe;
395         struct v4l2_fract timeperframe;
396         uint32_t interval;
397         int ret;
398
399         if (parm->type != stream->type)
400                 return -EINVAL;
401
402         if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
403                 timeperframe = parm->parm.capture.timeperframe;
404         else
405                 timeperframe = parm->parm.output.timeperframe;
406
407         interval = uvc_fraction_to_interval(timeperframe.numerator,
408                 timeperframe.denominator);
409         uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
410                 timeperframe.numerator, timeperframe.denominator, interval);
411
412         mutex_lock(&stream->mutex);
413
414         if (uvc_queue_streaming(&stream->queue)) {
415                 mutex_unlock(&stream->mutex);
416                 return -EBUSY;
417         }
418
419         probe = stream->ctrl;
420         probe.dwFrameInterval =
421                 uvc_try_frame_interval(stream->cur_frame, interval);
422
423         /* Probe the device with the new settings. */
424         ret = uvc_probe_video(stream, &probe);
425         if (ret < 0) {
426                 mutex_unlock(&stream->mutex);
427                 return ret;
428         }
429
430         stream->ctrl = probe;
431         mutex_unlock(&stream->mutex);
432
433         /* Return the actual frame period. */
434         timeperframe.numerator = probe.dwFrameInterval;
435         timeperframe.denominator = 10000000;
436         uvc_simplify_fraction(&timeperframe.numerator,
437                 &timeperframe.denominator, 8, 333);
438
439         if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
440                 parm->parm.capture.timeperframe = timeperframe;
441         else
442                 parm->parm.output.timeperframe = timeperframe;
443
444         return 0;
445 }
446
447 /* ------------------------------------------------------------------------
448  * Privilege management
449  */
450
451 /*
452  * Privilege management is the multiple-open implementation basis. The current
453  * implementation is completely transparent for the end-user and doesn't
454  * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls.
455  * Those ioctls enable finer control on the device (by making possible for a
456  * user to request exclusive access to a device), but are not mature yet.
457  * Switching to the V4L2 priority mechanism might be considered in the future
458  * if this situation changes.
459  *
460  * Each open instance of a UVC device can either be in a privileged or
461  * unprivileged state. Only a single instance can be in a privileged state at
462  * a given time. Trying to perform an operation that requires privileges will
463  * automatically acquire the required privileges if possible, or return -EBUSY
464  * otherwise. Privileges are dismissed when closing the instance or when
465  * freeing the video buffers using VIDIOC_REQBUFS.
466  *
467  * Operations that require privileges are:
468  *
469  * - VIDIOC_S_INPUT
470  * - VIDIOC_S_PARM
471  * - VIDIOC_S_FMT
472  * - VIDIOC_REQBUFS
473  */
474 static int uvc_acquire_privileges(struct uvc_fh *handle)
475 {
476         /* Always succeed if the handle is already privileged. */
477         if (handle->state == UVC_HANDLE_ACTIVE)
478                 return 0;
479
480         /* Check if the device already has a privileged handle. */
481         if (atomic_inc_return(&handle->stream->active) != 1) {
482                 atomic_dec(&handle->stream->active);
483                 return -EBUSY;
484         }
485
486         handle->state = UVC_HANDLE_ACTIVE;
487         return 0;
488 }
489
490 static void uvc_dismiss_privileges(struct uvc_fh *handle)
491 {
492         if (handle->state == UVC_HANDLE_ACTIVE)
493                 atomic_dec(&handle->stream->active);
494
495         handle->state = UVC_HANDLE_PASSIVE;
496 }
497
498 static int uvc_has_privileges(struct uvc_fh *handle)
499 {
500         return handle->state == UVC_HANDLE_ACTIVE;
501 }
502
503 /* ------------------------------------------------------------------------
504  * V4L2 file operations
505  */
506
507 static int uvc_v4l2_open(struct file *file)
508 {
509         struct uvc_streaming *stream;
510         struct uvc_fh *handle;
511         int ret = 0;
512
513         uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
514         stream = video_drvdata(file);
515
516         ret = usb_autopm_get_interface(stream->dev->intf);
517         if (ret < 0)
518                 return ret;
519
520         /* Create the device handle. */
521         handle = kzalloc(sizeof *handle, GFP_KERNEL);
522         if (handle == NULL) {
523                 usb_autopm_put_interface(stream->dev->intf);
524                 return -ENOMEM;
525         }
526
527         mutex_lock(&stream->dev->lock);
528         if (stream->dev->users == 0) {
529                 ret = uvc_status_start(stream->dev, GFP_KERNEL);
530                 if (ret < 0) {
531                         mutex_unlock(&stream->dev->lock);
532                         usb_autopm_put_interface(stream->dev->intf);
533                         kfree(handle);
534                         return ret;
535                 }
536         }
537
538         stream->dev->users++;
539         mutex_unlock(&stream->dev->lock);
540
541         v4l2_fh_init(&handle->vfh, &stream->vdev);
542         v4l2_fh_add(&handle->vfh);
543         handle->chain = stream->chain;
544         handle->stream = stream;
545         handle->state = UVC_HANDLE_PASSIVE;
546         file->private_data = handle;
547
548         return 0;
549 }
550
551 static int uvc_v4l2_release(struct file *file)
552 {
553         struct uvc_fh *handle = file->private_data;
554         struct uvc_streaming *stream = handle->stream;
555
556         uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");
557
558         /* Only free resources if this is a privileged handle. */
559         if (uvc_has_privileges(handle))
560                 uvc_queue_release(&stream->queue);
561
562         /* Release the file handle. */
563         uvc_dismiss_privileges(handle);
564         v4l2_fh_del(&handle->vfh);
565         v4l2_fh_exit(&handle->vfh);
566         kfree(handle);
567         file->private_data = NULL;
568
569         mutex_lock(&stream->dev->lock);
570         if (--stream->dev->users == 0)
571                 uvc_status_stop(stream->dev);
572         mutex_unlock(&stream->dev->lock);
573
574         usb_autopm_put_interface(stream->dev->intf);
575         return 0;
576 }
577
578 static int uvc_ioctl_querycap(struct file *file, void *fh,
579                               struct v4l2_capability *cap)
580 {
581         struct video_device *vdev = video_devdata(file);
582         struct uvc_fh *handle = file->private_data;
583         struct uvc_video_chain *chain = handle->chain;
584         struct uvc_streaming *stream = handle->stream;
585
586         strlcpy(cap->driver, "uvcvideo", sizeof(cap->driver));
587         strlcpy(cap->card, vdev->name, sizeof(cap->card));
588         usb_make_path(stream->dev->udev, cap->bus_info, sizeof(cap->bus_info));
589         cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
590                           | chain->caps;
591         if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
592                 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
593         else
594                 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
595
596         return 0;
597 }
598
599 static int uvc_ioctl_enum_fmt(struct uvc_streaming *stream,
600                               struct v4l2_fmtdesc *fmt)
601 {
602         struct uvc_format *format;
603         enum v4l2_buf_type type = fmt->type;
604         __u32 index = fmt->index;
605
606         if (fmt->type != stream->type || fmt->index >= stream->nformats)
607                 return -EINVAL;
608
609         memset(fmt, 0, sizeof(*fmt));
610         fmt->index = index;
611         fmt->type = type;
612
613         format = &stream->format[fmt->index];
614         fmt->flags = 0;
615         if (format->flags & UVC_FMT_FLAG_COMPRESSED)
616                 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
617         strlcpy(fmt->description, format->name, sizeof(fmt->description));
618         fmt->description[sizeof(fmt->description) - 1] = 0;
619         fmt->pixelformat = format->fcc;
620         return 0;
621 }
622
623 static int uvc_ioctl_enum_fmt_vid_cap(struct file *file, void *fh,
624                                       struct v4l2_fmtdesc *fmt)
625 {
626         struct uvc_fh *handle = fh;
627         struct uvc_streaming *stream = handle->stream;
628
629         return uvc_ioctl_enum_fmt(stream, fmt);
630 }
631
632 static int uvc_ioctl_enum_fmt_vid_out(struct file *file, void *fh,
633                                       struct v4l2_fmtdesc *fmt)
634 {
635         struct uvc_fh *handle = fh;
636         struct uvc_streaming *stream = handle->stream;
637
638         return uvc_ioctl_enum_fmt(stream, fmt);
639 }
640
641 static int uvc_ioctl_g_fmt_vid_cap(struct file *file, void *fh,
642                                    struct v4l2_format *fmt)
643 {
644         struct uvc_fh *handle = fh;
645         struct uvc_streaming *stream = handle->stream;
646
647         return uvc_v4l2_get_format(stream, fmt);
648 }
649
650 static int uvc_ioctl_g_fmt_vid_out(struct file *file, void *fh,
651                                    struct v4l2_format *fmt)
652 {
653         struct uvc_fh *handle = fh;
654         struct uvc_streaming *stream = handle->stream;
655
656         return uvc_v4l2_get_format(stream, fmt);
657 }
658
659 static int uvc_ioctl_s_fmt_vid_cap(struct file *file, void *fh,
660                                    struct v4l2_format *fmt)
661 {
662         struct uvc_fh *handle = fh;
663         struct uvc_streaming *stream = handle->stream;
664         int ret;
665
666         ret = uvc_acquire_privileges(handle);
667         if (ret < 0)
668                 return ret;
669
670         return uvc_v4l2_set_format(stream, fmt);
671 }
672
673 static int uvc_ioctl_s_fmt_vid_out(struct file *file, void *fh,
674                                    struct v4l2_format *fmt)
675 {
676         struct uvc_fh *handle = fh;
677         struct uvc_streaming *stream = handle->stream;
678         int ret;
679
680         ret = uvc_acquire_privileges(handle);
681         if (ret < 0)
682                 return ret;
683
684         return uvc_v4l2_set_format(stream, fmt);
685 }
686
687 static int uvc_ioctl_try_fmt_vid_cap(struct file *file, void *fh,
688                                      struct v4l2_format *fmt)
689 {
690         struct uvc_fh *handle = fh;
691         struct uvc_streaming *stream = handle->stream;
692         struct uvc_streaming_control probe;
693
694         return uvc_v4l2_try_format(stream, fmt, &probe, NULL, NULL);
695 }
696
697 static int uvc_ioctl_try_fmt_vid_out(struct file *file, void *fh,
698                                      struct v4l2_format *fmt)
699 {
700         struct uvc_fh *handle = fh;
701         struct uvc_streaming *stream = handle->stream;
702         struct uvc_streaming_control probe;
703
704         return uvc_v4l2_try_format(stream, fmt, &probe, NULL, NULL);
705 }
706
707 static int uvc_ioctl_reqbufs(struct file *file, void *fh,
708                              struct v4l2_requestbuffers *rb)
709 {
710         struct uvc_fh *handle = fh;
711         struct uvc_streaming *stream = handle->stream;
712         int ret;
713
714         ret = uvc_acquire_privileges(handle);
715         if (ret < 0)
716                 return ret;
717
718         mutex_lock(&stream->mutex);
719         ret = uvc_request_buffers(&stream->queue, rb);
720         mutex_unlock(&stream->mutex);
721         if (ret < 0)
722                 return ret;
723
724         if (ret == 0)
725                 uvc_dismiss_privileges(handle);
726
727         return 0;
728 }
729
730 static int uvc_ioctl_querybuf(struct file *file, void *fh,
731                               struct v4l2_buffer *buf)
732 {
733         struct uvc_fh *handle = fh;
734         struct uvc_streaming *stream = handle->stream;
735
736         if (!uvc_has_privileges(handle))
737                 return -EBUSY;
738
739         return uvc_query_buffer(&stream->queue, buf);
740 }
741
742 static int uvc_ioctl_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
743 {
744         struct uvc_fh *handle = fh;
745         struct uvc_streaming *stream = handle->stream;
746
747         if (!uvc_has_privileges(handle))
748                 return -EBUSY;
749
750         return uvc_queue_buffer(&stream->queue, buf);
751 }
752
753 static int uvc_ioctl_expbuf(struct file *file, void *fh,
754                             struct v4l2_exportbuffer *exp)
755 {
756         struct uvc_fh *handle = fh;
757         struct uvc_streaming *stream = handle->stream;
758
759         if (!uvc_has_privileges(handle))
760                 return -EBUSY;
761
762         return uvc_export_buffer(&stream->queue, exp);
763 }
764
765 static int uvc_ioctl_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
766 {
767         struct uvc_fh *handle = fh;
768         struct uvc_streaming *stream = handle->stream;
769
770         if (!uvc_has_privileges(handle))
771                 return -EBUSY;
772
773         return uvc_dequeue_buffer(&stream->queue, buf,
774                                   file->f_flags & O_NONBLOCK);
775 }
776
777 static int uvc_ioctl_create_bufs(struct file *file, void *fh,
778                                   struct v4l2_create_buffers *cb)
779 {
780         struct uvc_fh *handle = fh;
781         struct uvc_streaming *stream = handle->stream;
782         int ret;
783
784         ret = uvc_acquire_privileges(handle);
785         if (ret < 0)
786                 return ret;
787
788         return uvc_create_buffers(&stream->queue, cb);
789 }
790
791 static int uvc_ioctl_streamon(struct file *file, void *fh,
792                               enum v4l2_buf_type type)
793 {
794         struct uvc_fh *handle = fh;
795         struct uvc_streaming *stream = handle->stream;
796         int ret;
797
798         if (!uvc_has_privileges(handle))
799                 return -EBUSY;
800
801         mutex_lock(&stream->mutex);
802         ret = uvc_queue_streamon(&stream->queue, type);
803         mutex_unlock(&stream->mutex);
804
805         return ret;
806 }
807
808 static int uvc_ioctl_streamoff(struct file *file, void *fh,
809                                enum v4l2_buf_type type)
810 {
811         struct uvc_fh *handle = fh;
812         struct uvc_streaming *stream = handle->stream;
813
814         if (!uvc_has_privileges(handle))
815                 return -EBUSY;
816
817         mutex_lock(&stream->mutex);
818         uvc_queue_streamoff(&stream->queue, type);
819         mutex_unlock(&stream->mutex);
820
821         return 0;
822 }
823
824 static int uvc_ioctl_enum_input(struct file *file, void *fh,
825                                 struct v4l2_input *input)
826 {
827         struct uvc_fh *handle = fh;
828         struct uvc_video_chain *chain = handle->chain;
829         const struct uvc_entity *selector = chain->selector;
830         struct uvc_entity *iterm = NULL;
831         u32 index = input->index;
832         int pin = 0;
833
834         if (selector == NULL ||
835             (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
836                 if (index != 0)
837                         return -EINVAL;
838                 list_for_each_entry(iterm, &chain->entities, chain) {
839                         if (UVC_ENTITY_IS_ITERM(iterm))
840                                 break;
841                 }
842                 pin = iterm->id;
843         } else if (index < selector->bNrInPins) {
844                 pin = selector->baSourceID[index];
845                 list_for_each_entry(iterm, &chain->entities, chain) {
846                         if (!UVC_ENTITY_IS_ITERM(iterm))
847                                 continue;
848                         if (iterm->id == pin)
849                                 break;
850                 }
851         }
852
853         if (iterm == NULL || iterm->id != pin)
854                 return -EINVAL;
855
856         memset(input, 0, sizeof(*input));
857         input->index = index;
858         strlcpy(input->name, iterm->name, sizeof(input->name));
859         if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
860                 input->type = V4L2_INPUT_TYPE_CAMERA;
861
862         return 0;
863 }
864
865 static int uvc_ioctl_g_input(struct file *file, void *fh, unsigned int *input)
866 {
867         struct uvc_fh *handle = fh;
868         struct uvc_video_chain *chain = handle->chain;
869         u8 *buf;
870         int ret;
871
872         if (chain->selector == NULL ||
873             (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
874                 *input = 0;
875                 return 0;
876         }
877
878         buf = kmalloc(1, GFP_KERNEL);
879         if (!buf)
880                 return -ENOMEM;
881
882         ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, chain->selector->id,
883                              chain->dev->intfnum,  UVC_SU_INPUT_SELECT_CONTROL,
884                              buf, 1);
885         if (!ret)
886                 *input = *buf - 1;
887
888         kfree(buf);
889
890         return ret;
891 }
892
893 static int uvc_ioctl_s_input(struct file *file, void *fh, unsigned int input)
894 {
895         struct uvc_fh *handle = fh;
896         struct uvc_video_chain *chain = handle->chain;
897         u8 *buf;
898         int ret;
899
900         ret = uvc_acquire_privileges(handle);
901         if (ret < 0)
902                 return ret;
903
904         if (chain->selector == NULL ||
905             (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
906                 if (input)
907                         return -EINVAL;
908                 return 0;
909         }
910
911         if (input >= chain->selector->bNrInPins)
912                 return -EINVAL;
913
914         buf = kmalloc(1, GFP_KERNEL);
915         if (!buf)
916                 return -ENOMEM;
917
918         *buf = input + 1;
919         ret = uvc_query_ctrl(chain->dev, UVC_SET_CUR, chain->selector->id,
920                              chain->dev->intfnum, UVC_SU_INPUT_SELECT_CONTROL,
921                              buf, 1);
922         kfree(buf);
923
924         return ret;
925 }
926
927 static int uvc_ioctl_queryctrl(struct file *file, void *fh,
928                                struct v4l2_queryctrl *qc)
929 {
930         struct uvc_fh *handle = fh;
931         struct uvc_video_chain *chain = handle->chain;
932
933         return uvc_query_v4l2_ctrl(chain, qc);
934 }
935
936 static int uvc_ioctl_query_ext_ctrl(struct file *file, void *fh,
937                                     struct v4l2_query_ext_ctrl *qec)
938 {
939         struct uvc_fh *handle = fh;
940         struct uvc_video_chain *chain = handle->chain;
941         struct v4l2_queryctrl qc = { qec->id };
942         int ret;
943
944         ret = uvc_query_v4l2_ctrl(chain, &qc);
945         if (ret)
946                 return ret;
947
948         qec->id = qc.id;
949         qec->type = qc.type;
950         strlcpy(qec->name, qc.name, sizeof(qec->name));
951         qec->minimum = qc.minimum;
952         qec->maximum = qc.maximum;
953         qec->step = qc.step;
954         qec->default_value = qc.default_value;
955         qec->flags = qc.flags;
956         qec->elem_size = 4;
957         qec->elems = 1;
958         qec->nr_of_dims = 0;
959         memset(qec->dims, 0, sizeof(qec->dims));
960         memset(qec->reserved, 0, sizeof(qec->reserved));
961
962         return 0;
963 }
964
965 static int uvc_ioctl_g_ctrl(struct file *file, void *fh,
966                             struct v4l2_control *ctrl)
967 {
968         struct uvc_fh *handle = fh;
969         struct uvc_video_chain *chain = handle->chain;
970         struct v4l2_ext_control xctrl;
971         int ret;
972
973         memset(&xctrl, 0, sizeof(xctrl));
974         xctrl.id = ctrl->id;
975
976         ret = uvc_ctrl_begin(chain);
977         if (ret < 0)
978                 return ret;
979
980         ret = uvc_ctrl_get(chain, &xctrl);
981         uvc_ctrl_rollback(handle);
982         if (ret < 0)
983                 return ret;
984
985         ctrl->value = xctrl.value;
986         return 0;
987 }
988
989 static int uvc_ioctl_s_ctrl(struct file *file, void *fh,
990                             struct v4l2_control *ctrl)
991 {
992         struct uvc_fh *handle = fh;
993         struct uvc_video_chain *chain = handle->chain;
994         struct v4l2_ext_control xctrl;
995         int ret;
996
997         memset(&xctrl, 0, sizeof(xctrl));
998         xctrl.id = ctrl->id;
999         xctrl.value = ctrl->value;
1000
1001         ret = uvc_ctrl_begin(chain);
1002         if (ret < 0)
1003                 return ret;
1004
1005         ret = uvc_ctrl_set(chain, &xctrl);
1006         if (ret < 0) {
1007                 uvc_ctrl_rollback(handle);
1008                 return ret;
1009         }
1010
1011         ret = uvc_ctrl_commit(handle, &xctrl, 1);
1012         if (ret < 0)
1013                 return ret;
1014
1015         ctrl->value = xctrl.value;
1016         return 0;
1017 }
1018
1019 static int uvc_ioctl_g_ext_ctrls(struct file *file, void *fh,
1020                                  struct v4l2_ext_controls *ctrls)
1021 {
1022         struct uvc_fh *handle = fh;
1023         struct uvc_video_chain *chain = handle->chain;
1024         struct v4l2_ext_control *ctrl = ctrls->controls;
1025         unsigned int i;
1026         int ret;
1027
1028         ret = uvc_ctrl_begin(chain);
1029         if (ret < 0)
1030                 return ret;
1031
1032         for (i = 0; i < ctrls->count; ++ctrl, ++i) {
1033                 ret = uvc_ctrl_get(chain, ctrl);
1034                 if (ret < 0) {
1035                         uvc_ctrl_rollback(handle);
1036                         ctrls->error_idx = i;
1037                         return ret;
1038                 }
1039         }
1040
1041         ctrls->error_idx = 0;
1042
1043         return uvc_ctrl_rollback(handle);
1044 }
1045
1046 static int uvc_ioctl_s_try_ext_ctrls(struct uvc_fh *handle,
1047                                      struct v4l2_ext_controls *ctrls,
1048                                      bool commit)
1049 {
1050         struct v4l2_ext_control *ctrl = ctrls->controls;
1051         struct uvc_video_chain *chain = handle->chain;
1052         unsigned int i;
1053         int ret;
1054
1055         ret = uvc_ctrl_begin(chain);
1056         if (ret < 0)
1057                 return ret;
1058
1059         for (i = 0; i < ctrls->count; ++ctrl, ++i) {
1060                 ret = uvc_ctrl_set(chain, ctrl);
1061                 if (ret < 0) {
1062                         uvc_ctrl_rollback(handle);
1063                         ctrls->error_idx = commit ? ctrls->count : i;
1064                         return ret;
1065                 }
1066         }
1067
1068         ctrls->error_idx = 0;
1069
1070         if (commit)
1071                 return uvc_ctrl_commit(handle, ctrls->controls, ctrls->count);
1072         else
1073                 return uvc_ctrl_rollback(handle);
1074 }
1075
1076 static int uvc_ioctl_s_ext_ctrls(struct file *file, void *fh,
1077                                  struct v4l2_ext_controls *ctrls)
1078 {
1079         struct uvc_fh *handle = fh;
1080
1081         return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, true);
1082 }
1083
1084 static int uvc_ioctl_try_ext_ctrls(struct file *file, void *fh,
1085                                    struct v4l2_ext_controls *ctrls)
1086 {
1087         struct uvc_fh *handle = fh;
1088
1089         return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, false);
1090 }
1091
1092 static int uvc_ioctl_querymenu(struct file *file, void *fh,
1093                                struct v4l2_querymenu *qm)
1094 {
1095         struct uvc_fh *handle = fh;
1096         struct uvc_video_chain *chain = handle->chain;
1097
1098         return uvc_query_v4l2_menu(chain, qm);
1099 }
1100
1101 static int uvc_ioctl_g_selection(struct file *file, void *fh,
1102                                  struct v4l2_selection *sel)
1103 {
1104         struct uvc_fh *handle = fh;
1105         struct uvc_streaming *stream = handle->stream;
1106
1107         if (sel->type != stream->type)
1108                 return -EINVAL;
1109
1110         switch (sel->target) {
1111         case V4L2_SEL_TGT_CROP_DEFAULT:
1112         case V4L2_SEL_TGT_CROP_BOUNDS:
1113                 if (stream->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1114                         return -EINVAL;
1115                 break;
1116         case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1117         case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1118                 if (stream->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1119                         return -EINVAL;
1120                 break;
1121         default:
1122                 return -EINVAL;
1123         }
1124
1125         sel->r.left = 0;
1126         sel->r.top = 0;
1127         mutex_lock(&stream->mutex);
1128         sel->r.width = stream->cur_frame->wWidth;
1129         sel->r.height = stream->cur_frame->wHeight;
1130         mutex_unlock(&stream->mutex);
1131
1132         return 0;
1133 }
1134
1135 static int uvc_ioctl_g_parm(struct file *file, void *fh,
1136                             struct v4l2_streamparm *parm)
1137 {
1138         struct uvc_fh *handle = fh;
1139         struct uvc_streaming *stream = handle->stream;
1140
1141         return uvc_v4l2_get_streamparm(stream, parm);
1142 }
1143
1144 static int uvc_ioctl_s_parm(struct file *file, void *fh,
1145                             struct v4l2_streamparm *parm)
1146 {
1147         struct uvc_fh *handle = fh;
1148         struct uvc_streaming *stream = handle->stream;
1149         int ret;
1150
1151         ret = uvc_acquire_privileges(handle);
1152         if (ret < 0)
1153                 return ret;
1154
1155         return uvc_v4l2_set_streamparm(stream, parm);
1156 }
1157
1158 static int uvc_ioctl_enum_framesizes(struct file *file, void *fh,
1159                                      struct v4l2_frmsizeenum *fsize)
1160 {
1161         struct uvc_fh *handle = fh;
1162         struct uvc_streaming *stream = handle->stream;
1163         struct uvc_format *format = NULL;
1164         struct uvc_frame *frame;
1165         int i;
1166
1167         /* Look for the given pixel format */
1168         for (i = 0; i < stream->nformats; i++) {
1169                 if (stream->format[i].fcc == fsize->pixel_format) {
1170                         format = &stream->format[i];
1171                         break;
1172                 }
1173         }
1174         if (format == NULL)
1175                 return -EINVAL;
1176
1177         if (fsize->index >= format->nframes)
1178                 return -EINVAL;
1179
1180         frame = &format->frame[fsize->index];
1181         fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1182         fsize->discrete.width = frame->wWidth;
1183         fsize->discrete.height = frame->wHeight;
1184         return 0;
1185 }
1186
1187 static int uvc_ioctl_enum_frameintervals(struct file *file, void *fh,
1188                                          struct v4l2_frmivalenum *fival)
1189 {
1190         struct uvc_fh *handle = fh;
1191         struct uvc_streaming *stream = handle->stream;
1192         struct uvc_format *format = NULL;
1193         struct uvc_frame *frame = NULL;
1194         int i;
1195
1196         /* Look for the given pixel format and frame size */
1197         for (i = 0; i < stream->nformats; i++) {
1198                 if (stream->format[i].fcc == fival->pixel_format) {
1199                         format = &stream->format[i];
1200                         break;
1201                 }
1202         }
1203         if (format == NULL)
1204                 return -EINVAL;
1205
1206         for (i = 0; i < format->nframes; i++) {
1207                 if (format->frame[i].wWidth == fival->width &&
1208                     format->frame[i].wHeight == fival->height) {
1209                         frame = &format->frame[i];
1210                         break;
1211                 }
1212         }
1213         if (frame == NULL)
1214                 return -EINVAL;
1215
1216         if (frame->bFrameIntervalType) {
1217                 if (fival->index >= frame->bFrameIntervalType)
1218                         return -EINVAL;
1219
1220                 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1221                 fival->discrete.numerator =
1222                         frame->dwFrameInterval[fival->index];
1223                 fival->discrete.denominator = 10000000;
1224                 uvc_simplify_fraction(&fival->discrete.numerator,
1225                         &fival->discrete.denominator, 8, 333);
1226         } else {
1227                 if (fival->index)
1228                         return -EINVAL;
1229
1230                 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
1231                 fival->stepwise.min.numerator = frame->dwFrameInterval[0];
1232                 fival->stepwise.min.denominator = 10000000;
1233                 fival->stepwise.max.numerator = frame->dwFrameInterval[1];
1234                 fival->stepwise.max.denominator = 10000000;
1235                 fival->stepwise.step.numerator = frame->dwFrameInterval[2];
1236                 fival->stepwise.step.denominator = 10000000;
1237                 uvc_simplify_fraction(&fival->stepwise.min.numerator,
1238                         &fival->stepwise.min.denominator, 8, 333);
1239                 uvc_simplify_fraction(&fival->stepwise.max.numerator,
1240                         &fival->stepwise.max.denominator, 8, 333);
1241                 uvc_simplify_fraction(&fival->stepwise.step.numerator,
1242                         &fival->stepwise.step.denominator, 8, 333);
1243         }
1244
1245         return 0;
1246 }
1247
1248 static int uvc_ioctl_subscribe_event(struct v4l2_fh *fh,
1249                                      const struct v4l2_event_subscription *sub)
1250 {
1251         switch (sub->type) {
1252         case V4L2_EVENT_CTRL:
1253                 return v4l2_event_subscribe(fh, sub, 0, &uvc_ctrl_sub_ev_ops);
1254         default:
1255                 return -EINVAL;
1256         }
1257 }
1258
1259 static long uvc_ioctl_default(struct file *file, void *fh, bool valid_prio,
1260                               unsigned int cmd, void *arg)
1261 {
1262         struct uvc_fh *handle = fh;
1263         struct uvc_video_chain *chain = handle->chain;
1264
1265         switch (cmd) {
1266         /* Dynamic controls. */
1267         case UVCIOC_CTRL_MAP:
1268                 return uvc_ioctl_ctrl_map(chain, arg);
1269
1270         case UVCIOC_CTRL_QUERY:
1271                 return uvc_xu_ctrl_query(chain, arg);
1272
1273         default:
1274                 return -ENOTTY;
1275         }
1276 }
1277
1278 #ifdef CONFIG_COMPAT
1279 struct uvc_xu_control_mapping32 {
1280         __u32 id;
1281         __u8 name[32];
1282         __u8 entity[16];
1283         __u8 selector;
1284
1285         __u8 size;
1286         __u8 offset;
1287         __u32 v4l2_type;
1288         __u32 data_type;
1289
1290         compat_caddr_t menu_info;
1291         __u32 menu_count;
1292
1293         __u32 reserved[4];
1294 };
1295
1296 static int uvc_v4l2_get_xu_mapping(struct uvc_xu_control_mapping *kp,
1297                         const struct uvc_xu_control_mapping32 __user *up)
1298 {
1299         struct uvc_menu_info __user *umenus;
1300         struct uvc_menu_info __user *kmenus;
1301         compat_caddr_t p;
1302
1303         if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
1304             __copy_from_user(kp, up, offsetof(typeof(*up), menu_info)) ||
1305             __get_user(kp->menu_count, &up->menu_count))
1306                 return -EFAULT;
1307
1308         memset(kp->reserved, 0, sizeof(kp->reserved));
1309
1310         if (kp->menu_count == 0) {
1311                 kp->menu_info = NULL;
1312                 return 0;
1313         }
1314
1315         if (__get_user(p, &up->menu_info))
1316                 return -EFAULT;
1317         umenus = compat_ptr(p);
1318         if (!access_ok(VERIFY_READ, umenus, kp->menu_count * sizeof(*umenus)))
1319                 return -EFAULT;
1320
1321         kmenus = compat_alloc_user_space(kp->menu_count * sizeof(*kmenus));
1322         if (kmenus == NULL)
1323                 return -EFAULT;
1324         kp->menu_info = kmenus;
1325
1326         if (copy_in_user(kmenus, umenus, kp->menu_count * sizeof(*umenus)))
1327                 return -EFAULT;
1328
1329         return 0;
1330 }
1331
1332 static int uvc_v4l2_put_xu_mapping(const struct uvc_xu_control_mapping *kp,
1333                         struct uvc_xu_control_mapping32 __user *up)
1334 {
1335         struct uvc_menu_info __user *umenus;
1336         struct uvc_menu_info __user *kmenus = kp->menu_info;
1337         compat_caddr_t p;
1338
1339         if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
1340             __copy_to_user(up, kp, offsetof(typeof(*up), menu_info)) ||
1341             __put_user(kp->menu_count, &up->menu_count))
1342                 return -EFAULT;
1343
1344         if (__clear_user(up->reserved, sizeof(up->reserved)))
1345                 return -EFAULT;
1346
1347         if (kp->menu_count == 0)
1348                 return 0;
1349
1350         if (get_user(p, &up->menu_info))
1351                 return -EFAULT;
1352         umenus = compat_ptr(p);
1353
1354         if (copy_in_user(umenus, kmenus, kp->menu_count * sizeof(*umenus)))
1355                 return -EFAULT;
1356
1357         return 0;
1358 }
1359
1360 struct uvc_xu_control_query32 {
1361         __u8 unit;
1362         __u8 selector;
1363         __u8 query;
1364         __u16 size;
1365         compat_caddr_t data;
1366 };
1367
1368 static int uvc_v4l2_get_xu_query(struct uvc_xu_control_query *kp,
1369                         const struct uvc_xu_control_query32 __user *up)
1370 {
1371         u8 __user *udata;
1372         u8 __user *kdata;
1373         compat_caddr_t p;
1374
1375         if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
1376             __copy_from_user(kp, up, offsetof(typeof(*up), data)))
1377                 return -EFAULT;
1378
1379         if (kp->size == 0) {
1380                 kp->data = NULL;
1381                 return 0;
1382         }
1383
1384         if (__get_user(p, &up->data))
1385                 return -EFAULT;
1386         udata = compat_ptr(p);
1387         if (!access_ok(VERIFY_READ, udata, kp->size))
1388                 return -EFAULT;
1389
1390         kdata = compat_alloc_user_space(kp->size);
1391         if (kdata == NULL)
1392                 return -EFAULT;
1393         kp->data = kdata;
1394
1395         if (copy_in_user(kdata, udata, kp->size))
1396                 return -EFAULT;
1397
1398         return 0;
1399 }
1400
1401 static int uvc_v4l2_put_xu_query(const struct uvc_xu_control_query *kp,
1402                         struct uvc_xu_control_query32 __user *up)
1403 {
1404         u8 __user *udata;
1405         u8 __user *kdata = kp->data;
1406         compat_caddr_t p;
1407
1408         if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
1409             __copy_to_user(up, kp, offsetof(typeof(*up), data)))
1410                 return -EFAULT;
1411
1412         if (kp->size == 0)
1413                 return 0;
1414
1415         if (get_user(p, &up->data))
1416                 return -EFAULT;
1417         udata = compat_ptr(p);
1418         if (!access_ok(VERIFY_READ, udata, kp->size))
1419                 return -EFAULT;
1420
1421         if (copy_in_user(udata, kdata, kp->size))
1422                 return -EFAULT;
1423
1424         return 0;
1425 }
1426
1427 #define UVCIOC_CTRL_MAP32       _IOWR('u', 0x20, struct uvc_xu_control_mapping32)
1428 #define UVCIOC_CTRL_QUERY32     _IOWR('u', 0x21, struct uvc_xu_control_query32)
1429
1430 static long uvc_v4l2_compat_ioctl32(struct file *file,
1431                      unsigned int cmd, unsigned long arg)
1432 {
1433         struct uvc_fh *handle = file->private_data;
1434         union {
1435                 struct uvc_xu_control_mapping xmap;
1436                 struct uvc_xu_control_query xqry;
1437         } karg;
1438         void __user *up = compat_ptr(arg);
1439         long ret;
1440
1441         switch (cmd) {
1442         case UVCIOC_CTRL_MAP32:
1443                 ret = uvc_v4l2_get_xu_mapping(&karg.xmap, up);
1444                 if (ret)
1445                         return ret;
1446                 ret = uvc_ioctl_ctrl_map(handle->chain, &karg.xmap);
1447                 if (ret)
1448                         return ret;
1449                 ret = uvc_v4l2_put_xu_mapping(&karg.xmap, up);
1450                 if (ret)
1451                         return ret;
1452
1453                 break;
1454
1455         case UVCIOC_CTRL_QUERY32:
1456                 ret = uvc_v4l2_get_xu_query(&karg.xqry, up);
1457                 if (ret)
1458                         return ret;
1459                 ret = uvc_xu_ctrl_query(handle->chain, &karg.xqry);
1460                 if (ret)
1461                         return ret;
1462                 ret = uvc_v4l2_put_xu_query(&karg.xqry, up);
1463                 if (ret)
1464                         return ret;
1465                 break;
1466
1467         default:
1468                 return -ENOIOCTLCMD;
1469         }
1470
1471         return ret;
1472 }
1473 #endif
1474
1475 static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1476                     size_t count, loff_t *ppos)
1477 {
1478         uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
1479         return -EINVAL;
1480 }
1481
1482 static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1483 {
1484         struct uvc_fh *handle = file->private_data;
1485         struct uvc_streaming *stream = handle->stream;
1486
1487         uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
1488
1489         return uvc_queue_mmap(&stream->queue, vma);
1490 }
1491
1492 static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
1493 {
1494         struct uvc_fh *handle = file->private_data;
1495         struct uvc_streaming *stream = handle->stream;
1496
1497         uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
1498
1499         return uvc_queue_poll(&stream->queue, file, wait);
1500 }
1501
1502 #ifndef CONFIG_MMU
1503 static unsigned long uvc_v4l2_get_unmapped_area(struct file *file,
1504                 unsigned long addr, unsigned long len, unsigned long pgoff,
1505                 unsigned long flags)
1506 {
1507         struct uvc_fh *handle = file->private_data;
1508         struct uvc_streaming *stream = handle->stream;
1509
1510         uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_get_unmapped_area\n");
1511
1512         return uvc_queue_get_unmapped_area(&stream->queue, pgoff);
1513 }
1514 #endif
1515
1516 const struct v4l2_ioctl_ops uvc_ioctl_ops = {
1517         .vidioc_querycap = uvc_ioctl_querycap,
1518         .vidioc_enum_fmt_vid_cap = uvc_ioctl_enum_fmt_vid_cap,
1519         .vidioc_enum_fmt_vid_out = uvc_ioctl_enum_fmt_vid_out,
1520         .vidioc_g_fmt_vid_cap = uvc_ioctl_g_fmt_vid_cap,
1521         .vidioc_g_fmt_vid_out = uvc_ioctl_g_fmt_vid_out,
1522         .vidioc_s_fmt_vid_cap = uvc_ioctl_s_fmt_vid_cap,
1523         .vidioc_s_fmt_vid_out = uvc_ioctl_s_fmt_vid_out,
1524         .vidioc_try_fmt_vid_cap = uvc_ioctl_try_fmt_vid_cap,
1525         .vidioc_try_fmt_vid_out = uvc_ioctl_try_fmt_vid_out,
1526         .vidioc_reqbufs = uvc_ioctl_reqbufs,
1527         .vidioc_querybuf = uvc_ioctl_querybuf,
1528         .vidioc_qbuf = uvc_ioctl_qbuf,
1529         .vidioc_expbuf = uvc_ioctl_expbuf,
1530         .vidioc_dqbuf = uvc_ioctl_dqbuf,
1531         .vidioc_create_bufs = uvc_ioctl_create_bufs,
1532         .vidioc_streamon = uvc_ioctl_streamon,
1533         .vidioc_streamoff = uvc_ioctl_streamoff,
1534         .vidioc_enum_input = uvc_ioctl_enum_input,
1535         .vidioc_g_input = uvc_ioctl_g_input,
1536         .vidioc_s_input = uvc_ioctl_s_input,
1537         .vidioc_queryctrl = uvc_ioctl_queryctrl,
1538         .vidioc_query_ext_ctrl = uvc_ioctl_query_ext_ctrl,
1539         .vidioc_g_ctrl = uvc_ioctl_g_ctrl,
1540         .vidioc_s_ctrl = uvc_ioctl_s_ctrl,
1541         .vidioc_g_ext_ctrls = uvc_ioctl_g_ext_ctrls,
1542         .vidioc_s_ext_ctrls = uvc_ioctl_s_ext_ctrls,
1543         .vidioc_try_ext_ctrls = uvc_ioctl_try_ext_ctrls,
1544         .vidioc_querymenu = uvc_ioctl_querymenu,
1545         .vidioc_g_selection = uvc_ioctl_g_selection,
1546         .vidioc_g_parm = uvc_ioctl_g_parm,
1547         .vidioc_s_parm = uvc_ioctl_s_parm,
1548         .vidioc_enum_framesizes = uvc_ioctl_enum_framesizes,
1549         .vidioc_enum_frameintervals = uvc_ioctl_enum_frameintervals,
1550         .vidioc_subscribe_event = uvc_ioctl_subscribe_event,
1551         .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1552         .vidioc_default = uvc_ioctl_default,
1553 };
1554
1555 const struct v4l2_file_operations uvc_fops = {
1556         .owner          = THIS_MODULE,
1557         .open           = uvc_v4l2_open,
1558         .release        = uvc_v4l2_release,
1559         .unlocked_ioctl = video_ioctl2,
1560 #ifdef CONFIG_COMPAT
1561         .compat_ioctl32 = uvc_v4l2_compat_ioctl32,
1562 #endif
1563         .read           = uvc_v4l2_read,
1564         .mmap           = uvc_v4l2_mmap,
1565         .poll           = uvc_v4l2_poll,
1566 #ifndef CONFIG_MMU
1567         .get_unmapped_area = uvc_v4l2_get_unmapped_area,
1568 #endif
1569 };
1570