GNU Linux-libre 4.19.264-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         u32 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         struct uvc_format *format;
407         struct uvc_frame *frame;
408         u32 interval, maxd;
409         unsigned int i;
410         int ret;
411
412         if (parm->type != stream->type)
413                 return -EINVAL;
414
415         if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
416                 timeperframe = parm->parm.capture.timeperframe;
417         else
418                 timeperframe = parm->parm.output.timeperframe;
419
420         interval = uvc_fraction_to_interval(timeperframe.numerator,
421                 timeperframe.denominator);
422         uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
423                 timeperframe.numerator, timeperframe.denominator, interval);
424
425         mutex_lock(&stream->mutex);
426
427         if (uvc_queue_streaming(&stream->queue)) {
428                 mutex_unlock(&stream->mutex);
429                 return -EBUSY;
430         }
431
432         format = stream->cur_format;
433         frame = stream->cur_frame;
434         probe = stream->ctrl;
435         probe.dwFrameInterval = uvc_try_frame_interval(frame, interval);
436         maxd = abs((s32)probe.dwFrameInterval - interval);
437
438         /* Try frames with matching size to find the best frame interval. */
439         for (i = 0; i < format->nframes && maxd != 0; i++) {
440                 u32 d, ival;
441
442                 if (&format->frame[i] == stream->cur_frame)
443                         continue;
444
445                 if (format->frame[i].wWidth != stream->cur_frame->wWidth ||
446                     format->frame[i].wHeight != stream->cur_frame->wHeight)
447                         continue;
448
449                 ival = uvc_try_frame_interval(&format->frame[i], interval);
450                 d = abs((s32)ival - interval);
451                 if (d >= maxd)
452                         continue;
453
454                 frame = &format->frame[i];
455                 probe.bFrameIndex = frame->bFrameIndex;
456                 probe.dwFrameInterval = ival;
457                 maxd = d;
458         }
459
460         /* Probe the device with the new settings. */
461         ret = uvc_probe_video(stream, &probe);
462         if (ret < 0) {
463                 mutex_unlock(&stream->mutex);
464                 return ret;
465         }
466
467         stream->ctrl = probe;
468         stream->cur_frame = frame;
469         mutex_unlock(&stream->mutex);
470
471         /* Return the actual frame period. */
472         timeperframe.numerator = probe.dwFrameInterval;
473         timeperframe.denominator = 10000000;
474         uvc_simplify_fraction(&timeperframe.numerator,
475                 &timeperframe.denominator, 8, 333);
476
477         if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
478                 parm->parm.capture.timeperframe = timeperframe;
479                 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
480         } else {
481                 parm->parm.output.timeperframe = timeperframe;
482                 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
483         }
484
485         return 0;
486 }
487
488 /* ------------------------------------------------------------------------
489  * Privilege management
490  */
491
492 /*
493  * Privilege management is the multiple-open implementation basis. The current
494  * implementation is completely transparent for the end-user and doesn't
495  * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls.
496  * Those ioctls enable finer control on the device (by making possible for a
497  * user to request exclusive access to a device), but are not mature yet.
498  * Switching to the V4L2 priority mechanism might be considered in the future
499  * if this situation changes.
500  *
501  * Each open instance of a UVC device can either be in a privileged or
502  * unprivileged state. Only a single instance can be in a privileged state at
503  * a given time. Trying to perform an operation that requires privileges will
504  * automatically acquire the required privileges if possible, or return -EBUSY
505  * otherwise. Privileges are dismissed when closing the instance or when
506  * freeing the video buffers using VIDIOC_REQBUFS.
507  *
508  * Operations that require privileges are:
509  *
510  * - VIDIOC_S_INPUT
511  * - VIDIOC_S_PARM
512  * - VIDIOC_S_FMT
513  * - VIDIOC_REQBUFS
514  */
515 static int uvc_acquire_privileges(struct uvc_fh *handle)
516 {
517         /* Always succeed if the handle is already privileged. */
518         if (handle->state == UVC_HANDLE_ACTIVE)
519                 return 0;
520
521         /* Check if the device already has a privileged handle. */
522         if (atomic_inc_return(&handle->stream->active) != 1) {
523                 atomic_dec(&handle->stream->active);
524                 return -EBUSY;
525         }
526
527         handle->state = UVC_HANDLE_ACTIVE;
528         return 0;
529 }
530
531 static void uvc_dismiss_privileges(struct uvc_fh *handle)
532 {
533         if (handle->state == UVC_HANDLE_ACTIVE)
534                 atomic_dec(&handle->stream->active);
535
536         handle->state = UVC_HANDLE_PASSIVE;
537 }
538
539 static int uvc_has_privileges(struct uvc_fh *handle)
540 {
541         return handle->state == UVC_HANDLE_ACTIVE;
542 }
543
544 /* ------------------------------------------------------------------------
545  * V4L2 file operations
546  */
547
548 static int uvc_v4l2_open(struct file *file)
549 {
550         struct uvc_streaming *stream;
551         struct uvc_fh *handle;
552         int ret = 0;
553
554         uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
555         stream = video_drvdata(file);
556
557         ret = usb_autopm_get_interface(stream->dev->intf);
558         if (ret < 0)
559                 return ret;
560
561         /* Create the device handle. */
562         handle = kzalloc(sizeof(*handle), GFP_KERNEL);
563         if (handle == NULL) {
564                 usb_autopm_put_interface(stream->dev->intf);
565                 return -ENOMEM;
566         }
567
568         mutex_lock(&stream->dev->lock);
569         if (stream->dev->users == 0) {
570                 ret = uvc_status_start(stream->dev, GFP_KERNEL);
571                 if (ret < 0) {
572                         mutex_unlock(&stream->dev->lock);
573                         usb_autopm_put_interface(stream->dev->intf);
574                         kfree(handle);
575                         return ret;
576                 }
577         }
578
579         stream->dev->users++;
580         mutex_unlock(&stream->dev->lock);
581
582         v4l2_fh_init(&handle->vfh, &stream->vdev);
583         v4l2_fh_add(&handle->vfh);
584         handle->chain = stream->chain;
585         handle->stream = stream;
586         handle->state = UVC_HANDLE_PASSIVE;
587         file->private_data = handle;
588
589         return 0;
590 }
591
592 static int uvc_v4l2_release(struct file *file)
593 {
594         struct uvc_fh *handle = file->private_data;
595         struct uvc_streaming *stream = handle->stream;
596
597         uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");
598
599         /* Only free resources if this is a privileged handle. */
600         if (uvc_has_privileges(handle))
601                 uvc_queue_release(&stream->queue);
602
603         /* Release the file handle. */
604         uvc_dismiss_privileges(handle);
605         v4l2_fh_del(&handle->vfh);
606         v4l2_fh_exit(&handle->vfh);
607         kfree(handle);
608         file->private_data = NULL;
609
610         mutex_lock(&stream->dev->lock);
611         if (--stream->dev->users == 0)
612                 uvc_status_stop(stream->dev);
613         mutex_unlock(&stream->dev->lock);
614
615         usb_autopm_put_interface(stream->dev->intf);
616         return 0;
617 }
618
619 static int uvc_ioctl_querycap(struct file *file, void *fh,
620                               struct v4l2_capability *cap)
621 {
622         struct video_device *vdev = video_devdata(file);
623         struct uvc_fh *handle = file->private_data;
624         struct uvc_video_chain *chain = handle->chain;
625         struct uvc_streaming *stream = handle->stream;
626
627         strlcpy(cap->driver, "uvcvideo", sizeof(cap->driver));
628         strlcpy(cap->card, vdev->name, sizeof(cap->card));
629         usb_make_path(stream->dev->udev, cap->bus_info, sizeof(cap->bus_info));
630         cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
631                           | chain->caps;
632
633         return 0;
634 }
635
636 static int uvc_ioctl_enum_fmt(struct uvc_streaming *stream,
637                               struct v4l2_fmtdesc *fmt)
638 {
639         struct uvc_format *format;
640         enum v4l2_buf_type type = fmt->type;
641         u32 index = fmt->index;
642
643         if (fmt->type != stream->type || fmt->index >= stream->nformats)
644                 return -EINVAL;
645
646         memset(fmt, 0, sizeof(*fmt));
647         fmt->index = index;
648         fmt->type = type;
649
650         format = &stream->format[fmt->index];
651         fmt->flags = 0;
652         if (format->flags & UVC_FMT_FLAG_COMPRESSED)
653                 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
654         strlcpy(fmt->description, format->name, sizeof(fmt->description));
655         fmt->description[sizeof(fmt->description) - 1] = 0;
656         fmt->pixelformat = format->fcc;
657         return 0;
658 }
659
660 static int uvc_ioctl_enum_fmt_vid_cap(struct file *file, void *fh,
661                                       struct v4l2_fmtdesc *fmt)
662 {
663         struct uvc_fh *handle = fh;
664         struct uvc_streaming *stream = handle->stream;
665
666         return uvc_ioctl_enum_fmt(stream, fmt);
667 }
668
669 static int uvc_ioctl_enum_fmt_vid_out(struct file *file, void *fh,
670                                       struct v4l2_fmtdesc *fmt)
671 {
672         struct uvc_fh *handle = fh;
673         struct uvc_streaming *stream = handle->stream;
674
675         return uvc_ioctl_enum_fmt(stream, fmt);
676 }
677
678 static int uvc_ioctl_g_fmt_vid_cap(struct file *file, void *fh,
679                                    struct v4l2_format *fmt)
680 {
681         struct uvc_fh *handle = fh;
682         struct uvc_streaming *stream = handle->stream;
683
684         return uvc_v4l2_get_format(stream, fmt);
685 }
686
687 static int uvc_ioctl_g_fmt_vid_out(struct file *file, void *fh,
688                                    struct v4l2_format *fmt)
689 {
690         struct uvc_fh *handle = fh;
691         struct uvc_streaming *stream = handle->stream;
692
693         return uvc_v4l2_get_format(stream, fmt);
694 }
695
696 static int uvc_ioctl_s_fmt_vid_cap(struct file *file, void *fh,
697                                    struct v4l2_format *fmt)
698 {
699         struct uvc_fh *handle = fh;
700         struct uvc_streaming *stream = handle->stream;
701         int ret;
702
703         ret = uvc_acquire_privileges(handle);
704         if (ret < 0)
705                 return ret;
706
707         return uvc_v4l2_set_format(stream, fmt);
708 }
709
710 static int uvc_ioctl_s_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         int ret;
716
717         ret = uvc_acquire_privileges(handle);
718         if (ret < 0)
719                 return ret;
720
721         return uvc_v4l2_set_format(stream, fmt);
722 }
723
724 static int uvc_ioctl_try_fmt_vid_cap(struct file *file, void *fh,
725                                      struct v4l2_format *fmt)
726 {
727         struct uvc_fh *handle = fh;
728         struct uvc_streaming *stream = handle->stream;
729         struct uvc_streaming_control probe;
730
731         return uvc_v4l2_try_format(stream, fmt, &probe, NULL, NULL);
732 }
733
734 static int uvc_ioctl_try_fmt_vid_out(struct file *file, void *fh,
735                                      struct v4l2_format *fmt)
736 {
737         struct uvc_fh *handle = fh;
738         struct uvc_streaming *stream = handle->stream;
739         struct uvc_streaming_control probe;
740
741         return uvc_v4l2_try_format(stream, fmt, &probe, NULL, NULL);
742 }
743
744 static int uvc_ioctl_reqbufs(struct file *file, void *fh,
745                              struct v4l2_requestbuffers *rb)
746 {
747         struct uvc_fh *handle = fh;
748         struct uvc_streaming *stream = handle->stream;
749         int ret;
750
751         ret = uvc_acquire_privileges(handle);
752         if (ret < 0)
753                 return ret;
754
755         mutex_lock(&stream->mutex);
756         ret = uvc_request_buffers(&stream->queue, rb);
757         mutex_unlock(&stream->mutex);
758         if (ret < 0)
759                 return ret;
760
761         if (ret == 0)
762                 uvc_dismiss_privileges(handle);
763
764         return 0;
765 }
766
767 static int uvc_ioctl_querybuf(struct file *file, void *fh,
768                               struct v4l2_buffer *buf)
769 {
770         struct uvc_fh *handle = fh;
771         struct uvc_streaming *stream = handle->stream;
772
773         if (!uvc_has_privileges(handle))
774                 return -EBUSY;
775
776         return uvc_query_buffer(&stream->queue, buf);
777 }
778
779 static int uvc_ioctl_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
780 {
781         struct uvc_fh *handle = fh;
782         struct uvc_streaming *stream = handle->stream;
783
784         if (!uvc_has_privileges(handle))
785                 return -EBUSY;
786
787         return uvc_queue_buffer(&stream->queue, buf);
788 }
789
790 static int uvc_ioctl_expbuf(struct file *file, void *fh,
791                             struct v4l2_exportbuffer *exp)
792 {
793         struct uvc_fh *handle = fh;
794         struct uvc_streaming *stream = handle->stream;
795
796         if (!uvc_has_privileges(handle))
797                 return -EBUSY;
798
799         return uvc_export_buffer(&stream->queue, exp);
800 }
801
802 static int uvc_ioctl_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
803 {
804         struct uvc_fh *handle = fh;
805         struct uvc_streaming *stream = handle->stream;
806
807         if (!uvc_has_privileges(handle))
808                 return -EBUSY;
809
810         return uvc_dequeue_buffer(&stream->queue, buf,
811                                   file->f_flags & O_NONBLOCK);
812 }
813
814 static int uvc_ioctl_create_bufs(struct file *file, void *fh,
815                                   struct v4l2_create_buffers *cb)
816 {
817         struct uvc_fh *handle = fh;
818         struct uvc_streaming *stream = handle->stream;
819         int ret;
820
821         ret = uvc_acquire_privileges(handle);
822         if (ret < 0)
823                 return ret;
824
825         return uvc_create_buffers(&stream->queue, cb);
826 }
827
828 static int uvc_ioctl_streamon(struct file *file, void *fh,
829                               enum v4l2_buf_type type)
830 {
831         struct uvc_fh *handle = fh;
832         struct uvc_streaming *stream = handle->stream;
833         int ret;
834
835         if (!uvc_has_privileges(handle))
836                 return -EBUSY;
837
838         mutex_lock(&stream->mutex);
839         ret = uvc_queue_streamon(&stream->queue, type);
840         mutex_unlock(&stream->mutex);
841
842         return ret;
843 }
844
845 static int uvc_ioctl_streamoff(struct file *file, void *fh,
846                                enum v4l2_buf_type type)
847 {
848         struct uvc_fh *handle = fh;
849         struct uvc_streaming *stream = handle->stream;
850
851         if (!uvc_has_privileges(handle))
852                 return -EBUSY;
853
854         mutex_lock(&stream->mutex);
855         uvc_queue_streamoff(&stream->queue, type);
856         mutex_unlock(&stream->mutex);
857
858         return 0;
859 }
860
861 static int uvc_ioctl_enum_input(struct file *file, void *fh,
862                                 struct v4l2_input *input)
863 {
864         struct uvc_fh *handle = fh;
865         struct uvc_video_chain *chain = handle->chain;
866         const struct uvc_entity *selector = chain->selector;
867         struct uvc_entity *iterm = NULL;
868         struct uvc_entity *it;
869         u32 index = input->index;
870
871         if (selector == NULL ||
872             (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
873                 if (index != 0)
874                         return -EINVAL;
875                 list_for_each_entry(it, &chain->entities, chain) {
876                         if (UVC_ENTITY_IS_ITERM(it)) {
877                                 iterm = it;
878                                 break;
879                         }
880                 }
881         } else if (index < selector->bNrInPins) {
882                 list_for_each_entry(it, &chain->entities, chain) {
883                         if (!UVC_ENTITY_IS_ITERM(it))
884                                 continue;
885                         if (it->id == selector->baSourceID[index]) {
886                                 iterm = it;
887                                 break;
888                         }
889                 }
890         }
891
892         if (iterm == NULL)
893                 return -EINVAL;
894
895         memset(input, 0, sizeof(*input));
896         input->index = index;
897         strlcpy(input->name, iterm->name, sizeof(input->name));
898         if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
899                 input->type = V4L2_INPUT_TYPE_CAMERA;
900
901         return 0;
902 }
903
904 static int uvc_ioctl_g_input(struct file *file, void *fh, unsigned int *input)
905 {
906         struct uvc_fh *handle = fh;
907         struct uvc_video_chain *chain = handle->chain;
908         u8 *buf;
909         int ret;
910
911         if (chain->selector == NULL ||
912             (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
913                 *input = 0;
914                 return 0;
915         }
916
917         buf = kmalloc(1, GFP_KERNEL);
918         if (!buf)
919                 return -ENOMEM;
920
921         ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, chain->selector->id,
922                              chain->dev->intfnum,  UVC_SU_INPUT_SELECT_CONTROL,
923                              buf, 1);
924         if (!ret)
925                 *input = *buf - 1;
926
927         kfree(buf);
928
929         return ret;
930 }
931
932 static int uvc_ioctl_s_input(struct file *file, void *fh, unsigned int input)
933 {
934         struct uvc_fh *handle = fh;
935         struct uvc_video_chain *chain = handle->chain;
936         u8 *buf;
937         int ret;
938
939         ret = uvc_acquire_privileges(handle);
940         if (ret < 0)
941                 return ret;
942
943         if (chain->selector == NULL ||
944             (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
945                 if (input)
946                         return -EINVAL;
947                 return 0;
948         }
949
950         if (input >= chain->selector->bNrInPins)
951                 return -EINVAL;
952
953         buf = kmalloc(1, GFP_KERNEL);
954         if (!buf)
955                 return -ENOMEM;
956
957         *buf = input + 1;
958         ret = uvc_query_ctrl(chain->dev, UVC_SET_CUR, chain->selector->id,
959                              chain->dev->intfnum, UVC_SU_INPUT_SELECT_CONTROL,
960                              buf, 1);
961         kfree(buf);
962
963         return ret;
964 }
965
966 static int uvc_ioctl_queryctrl(struct file *file, void *fh,
967                                struct v4l2_queryctrl *qc)
968 {
969         struct uvc_fh *handle = fh;
970         struct uvc_video_chain *chain = handle->chain;
971
972         return uvc_query_v4l2_ctrl(chain, qc);
973 }
974
975 static int uvc_ioctl_query_ext_ctrl(struct file *file, void *fh,
976                                     struct v4l2_query_ext_ctrl *qec)
977 {
978         struct uvc_fh *handle = fh;
979         struct uvc_video_chain *chain = handle->chain;
980         struct v4l2_queryctrl qc = { qec->id };
981         int ret;
982
983         ret = uvc_query_v4l2_ctrl(chain, &qc);
984         if (ret)
985                 return ret;
986
987         qec->id = qc.id;
988         qec->type = qc.type;
989         strlcpy(qec->name, qc.name, sizeof(qec->name));
990         qec->minimum = qc.minimum;
991         qec->maximum = qc.maximum;
992         qec->step = qc.step;
993         qec->default_value = qc.default_value;
994         qec->flags = qc.flags;
995         qec->elem_size = 4;
996         qec->elems = 1;
997         qec->nr_of_dims = 0;
998         memset(qec->dims, 0, sizeof(qec->dims));
999         memset(qec->reserved, 0, sizeof(qec->reserved));
1000
1001         return 0;
1002 }
1003
1004 static int uvc_ioctl_g_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
1015         ret = uvc_ctrl_begin(chain);
1016         if (ret < 0)
1017                 return ret;
1018
1019         ret = uvc_ctrl_get(chain, &xctrl);
1020         uvc_ctrl_rollback(handle);
1021         if (ret < 0)
1022                 return ret;
1023
1024         ctrl->value = xctrl.value;
1025         return 0;
1026 }
1027
1028 static int uvc_ioctl_s_ctrl(struct file *file, void *fh,
1029                             struct v4l2_control *ctrl)
1030 {
1031         struct uvc_fh *handle = fh;
1032         struct uvc_video_chain *chain = handle->chain;
1033         struct v4l2_ext_control xctrl;
1034         int ret;
1035
1036         memset(&xctrl, 0, sizeof(xctrl));
1037         xctrl.id = ctrl->id;
1038         xctrl.value = ctrl->value;
1039
1040         ret = uvc_ctrl_begin(chain);
1041         if (ret < 0)
1042                 return ret;
1043
1044         ret = uvc_ctrl_set(handle, &xctrl);
1045         if (ret < 0) {
1046                 uvc_ctrl_rollback(handle);
1047                 return ret;
1048         }
1049
1050         ret = uvc_ctrl_commit(handle, &xctrl, 1);
1051         if (ret < 0)
1052                 return ret;
1053
1054         ctrl->value = xctrl.value;
1055         return 0;
1056 }
1057
1058 static int uvc_ioctl_g_ext_ctrls(struct file *file, void *fh,
1059                                  struct v4l2_ext_controls *ctrls)
1060 {
1061         struct uvc_fh *handle = fh;
1062         struct uvc_video_chain *chain = handle->chain;
1063         struct v4l2_ext_control *ctrl = ctrls->controls;
1064         unsigned int i;
1065         int ret;
1066
1067         if (ctrls->which == V4L2_CTRL_WHICH_DEF_VAL) {
1068                 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
1069                         struct v4l2_queryctrl qc = { .id = ctrl->id };
1070
1071                         ret = uvc_query_v4l2_ctrl(chain, &qc);
1072                         if (ret < 0) {
1073                                 ctrls->error_idx = i;
1074                                 return ret;
1075                         }
1076
1077                         ctrl->value = qc.default_value;
1078                 }
1079
1080                 return 0;
1081         }
1082
1083         ret = uvc_ctrl_begin(chain);
1084         if (ret < 0)
1085                 return ret;
1086
1087         for (i = 0; i < ctrls->count; ++ctrl, ++i) {
1088                 ret = uvc_ctrl_get(chain, ctrl);
1089                 if (ret < 0) {
1090                         uvc_ctrl_rollback(handle);
1091                         ctrls->error_idx = i;
1092                         return ret;
1093                 }
1094         }
1095
1096         ctrls->error_idx = 0;
1097
1098         return uvc_ctrl_rollback(handle);
1099 }
1100
1101 static int uvc_ioctl_s_try_ext_ctrls(struct uvc_fh *handle,
1102                                      struct v4l2_ext_controls *ctrls,
1103                                      bool commit)
1104 {
1105         struct v4l2_ext_control *ctrl = ctrls->controls;
1106         struct uvc_video_chain *chain = handle->chain;
1107         unsigned int i;
1108         int ret;
1109
1110         /* Default value cannot be changed */
1111         if (ctrls->which == V4L2_CTRL_WHICH_DEF_VAL)
1112                 return -EINVAL;
1113
1114         ret = uvc_ctrl_begin(chain);
1115         if (ret < 0)
1116                 return ret;
1117
1118         for (i = 0; i < ctrls->count; ++ctrl, ++i) {
1119                 ret = uvc_ctrl_set(handle, ctrl);
1120                 if (ret < 0) {
1121                         uvc_ctrl_rollback(handle);
1122                         ctrls->error_idx = commit ? ctrls->count : i;
1123                         return ret;
1124                 }
1125         }
1126
1127         ctrls->error_idx = 0;
1128
1129         if (commit)
1130                 return uvc_ctrl_commit(handle, ctrls->controls, ctrls->count);
1131         else
1132                 return uvc_ctrl_rollback(handle);
1133 }
1134
1135 static int uvc_ioctl_s_ext_ctrls(struct file *file, void *fh,
1136                                  struct v4l2_ext_controls *ctrls)
1137 {
1138         struct uvc_fh *handle = fh;
1139
1140         return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, true);
1141 }
1142
1143 static int uvc_ioctl_try_ext_ctrls(struct file *file, void *fh,
1144                                    struct v4l2_ext_controls *ctrls)
1145 {
1146         struct uvc_fh *handle = fh;
1147
1148         return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, false);
1149 }
1150
1151 static int uvc_ioctl_querymenu(struct file *file, void *fh,
1152                                struct v4l2_querymenu *qm)
1153 {
1154         struct uvc_fh *handle = fh;
1155         struct uvc_video_chain *chain = handle->chain;
1156
1157         return uvc_query_v4l2_menu(chain, qm);
1158 }
1159
1160 static int uvc_ioctl_g_selection(struct file *file, void *fh,
1161                                  struct v4l2_selection *sel)
1162 {
1163         struct uvc_fh *handle = fh;
1164         struct uvc_streaming *stream = handle->stream;
1165
1166         if (sel->type != stream->type)
1167                 return -EINVAL;
1168
1169         switch (sel->target) {
1170         case V4L2_SEL_TGT_CROP_DEFAULT:
1171         case V4L2_SEL_TGT_CROP_BOUNDS:
1172                 if (stream->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1173                         return -EINVAL;
1174                 break;
1175         case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1176         case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1177                 if (stream->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1178                         return -EINVAL;
1179                 break;
1180         default:
1181                 return -EINVAL;
1182         }
1183
1184         sel->r.left = 0;
1185         sel->r.top = 0;
1186         mutex_lock(&stream->mutex);
1187         sel->r.width = stream->cur_frame->wWidth;
1188         sel->r.height = stream->cur_frame->wHeight;
1189         mutex_unlock(&stream->mutex);
1190
1191         return 0;
1192 }
1193
1194 static int uvc_ioctl_g_parm(struct file *file, void *fh,
1195                             struct v4l2_streamparm *parm)
1196 {
1197         struct uvc_fh *handle = fh;
1198         struct uvc_streaming *stream = handle->stream;
1199
1200         return uvc_v4l2_get_streamparm(stream, parm);
1201 }
1202
1203 static int uvc_ioctl_s_parm(struct file *file, void *fh,
1204                             struct v4l2_streamparm *parm)
1205 {
1206         struct uvc_fh *handle = fh;
1207         struct uvc_streaming *stream = handle->stream;
1208         int ret;
1209
1210         ret = uvc_acquire_privileges(handle);
1211         if (ret < 0)
1212                 return ret;
1213
1214         return uvc_v4l2_set_streamparm(stream, parm);
1215 }
1216
1217 static int uvc_ioctl_enum_framesizes(struct file *file, void *fh,
1218                                      struct v4l2_frmsizeenum *fsize)
1219 {
1220         struct uvc_fh *handle = fh;
1221         struct uvc_streaming *stream = handle->stream;
1222         struct uvc_format *format = NULL;
1223         struct uvc_frame *frame = NULL;
1224         unsigned int index;
1225         unsigned int i;
1226
1227         /* Look for the given pixel format */
1228         for (i = 0; i < stream->nformats; i++) {
1229                 if (stream->format[i].fcc == fsize->pixel_format) {
1230                         format = &stream->format[i];
1231                         break;
1232                 }
1233         }
1234         if (format == NULL)
1235                 return -EINVAL;
1236
1237         /* Skip duplicate frame sizes */
1238         for (i = 0, index = 0; i < format->nframes; i++) {
1239                 if (frame && frame->wWidth == format->frame[i].wWidth &&
1240                     frame->wHeight == format->frame[i].wHeight)
1241                         continue;
1242                 frame = &format->frame[i];
1243                 if (index == fsize->index)
1244                         break;
1245                 index++;
1246         }
1247
1248         if (i == format->nframes)
1249                 return -EINVAL;
1250
1251         fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1252         fsize->discrete.width = frame->wWidth;
1253         fsize->discrete.height = frame->wHeight;
1254         return 0;
1255 }
1256
1257 static int uvc_ioctl_enum_frameintervals(struct file *file, void *fh,
1258                                          struct v4l2_frmivalenum *fival)
1259 {
1260         struct uvc_fh *handle = fh;
1261         struct uvc_streaming *stream = handle->stream;
1262         struct uvc_format *format = NULL;
1263         struct uvc_frame *frame = NULL;
1264         unsigned int nintervals;
1265         unsigned int index;
1266         unsigned int i;
1267
1268         /* Look for the given pixel format and frame size */
1269         for (i = 0; i < stream->nformats; i++) {
1270                 if (stream->format[i].fcc == fival->pixel_format) {
1271                         format = &stream->format[i];
1272                         break;
1273                 }
1274         }
1275         if (format == NULL)
1276                 return -EINVAL;
1277
1278         index = fival->index;
1279         for (i = 0; i < format->nframes; i++) {
1280                 if (format->frame[i].wWidth == fival->width &&
1281                     format->frame[i].wHeight == fival->height) {
1282                         frame = &format->frame[i];
1283                         nintervals = frame->bFrameIntervalType ?: 1;
1284                         if (index < nintervals)
1285                                 break;
1286                         index -= nintervals;
1287                 }
1288         }
1289         if (i == format->nframes)
1290                 return -EINVAL;
1291
1292         if (frame->bFrameIntervalType) {
1293                 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1294                 fival->discrete.numerator =
1295                         frame->dwFrameInterval[index];
1296                 fival->discrete.denominator = 10000000;
1297                 uvc_simplify_fraction(&fival->discrete.numerator,
1298                         &fival->discrete.denominator, 8, 333);
1299         } else {
1300                 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
1301                 fival->stepwise.min.numerator = frame->dwFrameInterval[0];
1302                 fival->stepwise.min.denominator = 10000000;
1303                 fival->stepwise.max.numerator = frame->dwFrameInterval[1];
1304                 fival->stepwise.max.denominator = 10000000;
1305                 fival->stepwise.step.numerator = frame->dwFrameInterval[2];
1306                 fival->stepwise.step.denominator = 10000000;
1307                 uvc_simplify_fraction(&fival->stepwise.min.numerator,
1308                         &fival->stepwise.min.denominator, 8, 333);
1309                 uvc_simplify_fraction(&fival->stepwise.max.numerator,
1310                         &fival->stepwise.max.denominator, 8, 333);
1311                 uvc_simplify_fraction(&fival->stepwise.step.numerator,
1312                         &fival->stepwise.step.denominator, 8, 333);
1313         }
1314
1315         return 0;
1316 }
1317
1318 static int uvc_ioctl_subscribe_event(struct v4l2_fh *fh,
1319                                      const struct v4l2_event_subscription *sub)
1320 {
1321         switch (sub->type) {
1322         case V4L2_EVENT_CTRL:
1323                 return v4l2_event_subscribe(fh, sub, 0, &uvc_ctrl_sub_ev_ops);
1324         default:
1325                 return -EINVAL;
1326         }
1327 }
1328
1329 static long uvc_ioctl_default(struct file *file, void *fh, bool valid_prio,
1330                               unsigned int cmd, void *arg)
1331 {
1332         struct uvc_fh *handle = fh;
1333         struct uvc_video_chain *chain = handle->chain;
1334
1335         switch (cmd) {
1336         /* Dynamic controls. */
1337         case UVCIOC_CTRL_MAP:
1338                 return uvc_ioctl_ctrl_map(chain, arg);
1339
1340         case UVCIOC_CTRL_QUERY:
1341                 return uvc_xu_ctrl_query(chain, arg);
1342
1343         default:
1344                 return -ENOTTY;
1345         }
1346 }
1347
1348 #ifdef CONFIG_COMPAT
1349 struct uvc_xu_control_mapping32 {
1350         u32 id;
1351         u8 name[32];
1352         u8 entity[16];
1353         u8 selector;
1354
1355         u8 size;
1356         u8 offset;
1357         u32 v4l2_type;
1358         u32 data_type;
1359
1360         compat_caddr_t menu_info;
1361         u32 menu_count;
1362
1363         u32 reserved[4];
1364 };
1365
1366 static int uvc_v4l2_get_xu_mapping(struct uvc_xu_control_mapping *kp,
1367                         const struct uvc_xu_control_mapping32 __user *up)
1368 {
1369         struct uvc_xu_control_mapping32 *p = (void *)kp;
1370         compat_caddr_t info;
1371         u32 count;
1372
1373         if (copy_from_user(p, up, sizeof(*p)))
1374                 return -EFAULT;
1375
1376         count = p->menu_count;
1377         info = p->menu_info;
1378
1379         memset(kp->reserved, 0, sizeof(kp->reserved));
1380         kp->menu_info = count ? compat_ptr(info) : NULL;
1381         kp->menu_count = count;
1382         return 0;
1383 }
1384
1385 static int uvc_v4l2_put_xu_mapping(const struct uvc_xu_control_mapping *kp,
1386                         struct uvc_xu_control_mapping32 __user *up)
1387 {
1388         if (copy_to_user(up, kp, offsetof(typeof(*up), menu_info)) ||
1389             put_user(kp->menu_count, &up->menu_count))
1390                 return -EFAULT;
1391
1392         if (clear_user(up->reserved, sizeof(up->reserved)))
1393                 return -EFAULT;
1394
1395         return 0;
1396 }
1397
1398 struct uvc_xu_control_query32 {
1399         u8 unit;
1400         u8 selector;
1401         u8 query;
1402         u16 size;
1403         compat_caddr_t data;
1404 };
1405
1406 static int uvc_v4l2_get_xu_query(struct uvc_xu_control_query *kp,
1407                         const struct uvc_xu_control_query32 __user *up)
1408 {
1409         struct uvc_xu_control_query32 v;
1410
1411         if (copy_from_user(&v, up, sizeof(v)))
1412                 return -EFAULT;
1413
1414         *kp = (struct uvc_xu_control_query){
1415                 .unit = v.unit,
1416                 .selector = v.selector,
1417                 .query = v.query,
1418                 .size = v.size,
1419                 .data = v.size ? compat_ptr(v.data) : NULL
1420         };
1421         return 0;
1422 }
1423
1424 static int uvc_v4l2_put_xu_query(const struct uvc_xu_control_query *kp,
1425                         struct uvc_xu_control_query32 __user *up)
1426 {
1427         if (copy_to_user(up, kp, offsetof(typeof(*up), data)))
1428                 return -EFAULT;
1429         return 0;
1430 }
1431
1432 #define UVCIOC_CTRL_MAP32       _IOWR('u', 0x20, struct uvc_xu_control_mapping32)
1433 #define UVCIOC_CTRL_QUERY32     _IOWR('u', 0x21, struct uvc_xu_control_query32)
1434
1435 static long uvc_v4l2_compat_ioctl32(struct file *file,
1436                      unsigned int cmd, unsigned long arg)
1437 {
1438         struct uvc_fh *handle = file->private_data;
1439         union {
1440                 struct uvc_xu_control_mapping xmap;
1441                 struct uvc_xu_control_query xqry;
1442         } karg;
1443         void __user *up = compat_ptr(arg);
1444         long ret;
1445
1446         switch (cmd) {
1447         case UVCIOC_CTRL_MAP32:
1448                 ret = uvc_v4l2_get_xu_mapping(&karg.xmap, up);
1449                 if (ret)
1450                         return ret;
1451                 ret = uvc_ioctl_ctrl_map(handle->chain, &karg.xmap);
1452                 if (ret)
1453                         return ret;
1454                 ret = uvc_v4l2_put_xu_mapping(&karg.xmap, up);
1455                 if (ret)
1456                         return ret;
1457
1458                 break;
1459
1460         case UVCIOC_CTRL_QUERY32:
1461                 ret = uvc_v4l2_get_xu_query(&karg.xqry, up);
1462                 if (ret)
1463                         return ret;
1464                 ret = uvc_xu_ctrl_query(handle->chain, &karg.xqry);
1465                 if (ret)
1466                         return ret;
1467                 ret = uvc_v4l2_put_xu_query(&karg.xqry, up);
1468                 if (ret)
1469                         return ret;
1470                 break;
1471
1472         default:
1473                 return -ENOIOCTLCMD;
1474         }
1475
1476         return ret;
1477 }
1478 #endif
1479
1480 static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1481                     size_t count, loff_t *ppos)
1482 {
1483         uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
1484         return -EINVAL;
1485 }
1486
1487 static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1488 {
1489         struct uvc_fh *handle = file->private_data;
1490         struct uvc_streaming *stream = handle->stream;
1491
1492         uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
1493
1494         return uvc_queue_mmap(&stream->queue, vma);
1495 }
1496
1497 static __poll_t uvc_v4l2_poll(struct file *file, poll_table *wait)
1498 {
1499         struct uvc_fh *handle = file->private_data;
1500         struct uvc_streaming *stream = handle->stream;
1501
1502         uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
1503
1504         return uvc_queue_poll(&stream->queue, file, wait);
1505 }
1506
1507 #ifndef CONFIG_MMU
1508 static unsigned long uvc_v4l2_get_unmapped_area(struct file *file,
1509                 unsigned long addr, unsigned long len, unsigned long pgoff,
1510                 unsigned long flags)
1511 {
1512         struct uvc_fh *handle = file->private_data;
1513         struct uvc_streaming *stream = handle->stream;
1514
1515         uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_get_unmapped_area\n");
1516
1517         return uvc_queue_get_unmapped_area(&stream->queue, pgoff);
1518 }
1519 #endif
1520
1521 const struct v4l2_ioctl_ops uvc_ioctl_ops = {
1522         .vidioc_querycap = uvc_ioctl_querycap,
1523         .vidioc_enum_fmt_vid_cap = uvc_ioctl_enum_fmt_vid_cap,
1524         .vidioc_enum_fmt_vid_out = uvc_ioctl_enum_fmt_vid_out,
1525         .vidioc_g_fmt_vid_cap = uvc_ioctl_g_fmt_vid_cap,
1526         .vidioc_g_fmt_vid_out = uvc_ioctl_g_fmt_vid_out,
1527         .vidioc_s_fmt_vid_cap = uvc_ioctl_s_fmt_vid_cap,
1528         .vidioc_s_fmt_vid_out = uvc_ioctl_s_fmt_vid_out,
1529         .vidioc_try_fmt_vid_cap = uvc_ioctl_try_fmt_vid_cap,
1530         .vidioc_try_fmt_vid_out = uvc_ioctl_try_fmt_vid_out,
1531         .vidioc_reqbufs = uvc_ioctl_reqbufs,
1532         .vidioc_querybuf = uvc_ioctl_querybuf,
1533         .vidioc_qbuf = uvc_ioctl_qbuf,
1534         .vidioc_expbuf = uvc_ioctl_expbuf,
1535         .vidioc_dqbuf = uvc_ioctl_dqbuf,
1536         .vidioc_create_bufs = uvc_ioctl_create_bufs,
1537         .vidioc_streamon = uvc_ioctl_streamon,
1538         .vidioc_streamoff = uvc_ioctl_streamoff,
1539         .vidioc_enum_input = uvc_ioctl_enum_input,
1540         .vidioc_g_input = uvc_ioctl_g_input,
1541         .vidioc_s_input = uvc_ioctl_s_input,
1542         .vidioc_queryctrl = uvc_ioctl_queryctrl,
1543         .vidioc_query_ext_ctrl = uvc_ioctl_query_ext_ctrl,
1544         .vidioc_g_ctrl = uvc_ioctl_g_ctrl,
1545         .vidioc_s_ctrl = uvc_ioctl_s_ctrl,
1546         .vidioc_g_ext_ctrls = uvc_ioctl_g_ext_ctrls,
1547         .vidioc_s_ext_ctrls = uvc_ioctl_s_ext_ctrls,
1548         .vidioc_try_ext_ctrls = uvc_ioctl_try_ext_ctrls,
1549         .vidioc_querymenu = uvc_ioctl_querymenu,
1550         .vidioc_g_selection = uvc_ioctl_g_selection,
1551         .vidioc_g_parm = uvc_ioctl_g_parm,
1552         .vidioc_s_parm = uvc_ioctl_s_parm,
1553         .vidioc_enum_framesizes = uvc_ioctl_enum_framesizes,
1554         .vidioc_enum_frameintervals = uvc_ioctl_enum_frameintervals,
1555         .vidioc_subscribe_event = uvc_ioctl_subscribe_event,
1556         .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1557         .vidioc_default = uvc_ioctl_default,
1558 };
1559
1560 const struct v4l2_file_operations uvc_fops = {
1561         .owner          = THIS_MODULE,
1562         .open           = uvc_v4l2_open,
1563         .release        = uvc_v4l2_release,
1564         .unlocked_ioctl = video_ioctl2,
1565 #ifdef CONFIG_COMPAT
1566         .compat_ioctl32 = uvc_v4l2_compat_ioctl32,
1567 #endif
1568         .read           = uvc_v4l2_read,
1569         .mmap           = uvc_v4l2_mmap,
1570         .poll           = uvc_v4l2_poll,
1571 #ifndef CONFIG_MMU
1572         .get_unmapped_area = uvc_v4l2_get_unmapped_area,
1573 #endif
1574 };
1575