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