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