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