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