]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - drivers/media/usb/uvc/uvc_v4l2.c
Merge remote-tracking branches 'spi/topic/mxs', 'spi/topic/pxa', 'spi/topic/rockchip...
[mirror_ubuntu-eoan-kernel.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
LP
15#include <linux/kernel.h>
16#include <linux/version.h>
17#include <linux/list.h>
18#include <linux/module.h>
5a0e3ad6 19#include <linux/slab.h>
c0efd232
LP
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>
60063497 25#include <linux/atomic.h>
c0efd232
LP
26
27#include <media/v4l2-common.h>
b4012002
HG
28#include <media/v4l2-ctrls.h>
29#include <media/v4l2-event.h>
35ea11ff 30#include <media/v4l2-ioctl.h>
c0efd232
LP
31
32#include "uvcvideo.h"
33
561474c2
LP
34/* ------------------------------------------------------------------------
35 * UVC ioctls
36 */
ba2fa996 37static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain,
227bd5b3 38 struct uvc_xu_control_mapping *xmap)
561474c2
LP
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:
806e23e9
HC
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
561474c2
LP
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:
ba2fa996
LP
89 uvc_trace(UVC_TRACE_CONTROL, "Unsupported V4L2 control type "
90 "%u.\n", xmap->v4l2_type);
7a286cc1 91 ret = -ENOTTY;
561474c2
LP
92 goto done;
93 }
94
ba2fa996 95 ret = uvc_ctrl_add_mapping(chain, map);
561474c2
LP
96
97done:
ba2fa996
LP
98 kfree(map->menu_info);
99 kfree(map);
561474c2
LP
100
101 return ret;
102}
103
c0efd232
LP
104/* ------------------------------------------------------------------------
105 * V4L2 interface
106 */
107
c0efd232
LP
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 */
114static __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
35f02a68 146static int uvc_v4l2_try_format(struct uvc_streaming *stream,
c0efd232
LP
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
35f02a68 159 if (fmt->type != stream->type)
c0efd232
LP
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
815adc46
LP
168 /* Check if the hardware supports the requested format, use the default
169 * format otherwise.
170 */
35f02a68
LP
171 for (i = 0; i < stream->nformats; ++i) {
172 format = &stream->format[i];
c0efd232
LP
173 if (format->fcc == fmt->fmt.pix.pixelformat)
174 break;
175 }
176
815adc46
LP
177 if (i == stream->nformats) {
178 format = stream->def_format;
179 fmt->fmt.pix.pixelformat = format->fcc;
c0efd232
LP
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 */
6947756d 235 mutex_lock(&stream->mutex);
35f02a68 236 if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS)
c0efd232 237 probe->dwMaxVideoFrameSize =
35f02a68 238 stream->ctrl.dwMaxVideoFrameSize;
c0efd232 239
2c2d264b 240 /* Probe the device. */
35f02a68 241 ret = uvc_probe_video(stream, probe);
6947756d 242 mutex_unlock(&stream->mutex);
35f02a68 243 if (ret < 0)
c0efd232
LP
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
259done:
260 return ret;
261}
262
35f02a68 263static int uvc_v4l2_get_format(struct uvc_streaming *stream,
c0efd232
LP
264 struct v4l2_format *fmt)
265{
6947756d
LP
266 struct uvc_format *format;
267 struct uvc_frame *frame;
268 int ret = 0;
c0efd232 269
35f02a68 270 if (fmt->type != stream->type)
c0efd232
LP
271 return -EINVAL;
272
6947756d
LP
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 }
c0efd232
LP
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;
35f02a68 287 fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize;
c0efd232
LP
288 fmt->fmt.pix.colorspace = format->colorspace;
289 fmt->fmt.pix.priv = 0;
290
6947756d
LP
291done:
292 mutex_unlock(&stream->mutex);
293 return ret;
c0efd232
LP
294}
295
35f02a68 296static int uvc_v4l2_set_format(struct uvc_streaming *stream,
c0efd232
LP
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
35f02a68 304 if (fmt->type != stream->type)
c0efd232
LP
305 return -EINVAL;
306
35f02a68 307 ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame);
c0efd232
LP
308 if (ret < 0)
309 return ret;
310
6947756d
LP
311 mutex_lock(&stream->mutex);
312
313 if (uvc_queue_allocated(&stream->queue)) {
314 ret = -EBUSY;
315 goto done;
316 }
317
8c0d44e2 318 stream->ctrl = probe;
35f02a68
LP
319 stream->cur_format = format;
320 stream->cur_frame = frame;
c0efd232 321
6947756d
LP
322done:
323 mutex_unlock(&stream->mutex);
324 return ret;
c0efd232
LP
325}
326
35f02a68 327static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream,
c0efd232
LP
328 struct v4l2_streamparm *parm)
329{
330 uint32_t numerator, denominator;
331
35f02a68 332 if (parm->type != stream->type)
c0efd232
LP
333 return -EINVAL;
334
6947756d 335 mutex_lock(&stream->mutex);
35f02a68 336 numerator = stream->ctrl.dwFrameInterval;
6947756d
LP
337 mutex_unlock(&stream->mutex);
338
c0efd232
LP
339 denominator = 10000000;
340 uvc_simplify_fraction(&numerator, &denominator, 8, 333);
341
342 memset(parm, 0, sizeof *parm);
35f02a68 343 parm->type = stream->type;
ff924203 344
35f02a68 345 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
ff924203
LP
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 }
c0efd232
LP
358
359 return 0;
360}
361
35f02a68 362static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
c0efd232
LP
363 struct v4l2_streamparm *parm)
364{
c0efd232 365 struct uvc_streaming_control probe;
ff924203 366 struct v4l2_fract timeperframe;
c0efd232
LP
367 uint32_t interval;
368 int ret;
369
35f02a68 370 if (parm->type != stream->type)
c0efd232
LP
371 return -EINVAL;
372
ff924203
LP
373 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
374 timeperframe = parm->parm.capture.timeperframe;
375 else
376 timeperframe = parm->parm.output.timeperframe;
377
ff924203
LP
378 interval = uvc_fraction_to_interval(timeperframe.numerator,
379 timeperframe.denominator);
c0efd232 380 uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
ff924203 381 timeperframe.numerator, timeperframe.denominator, interval);
6947756d
LP
382
383 mutex_lock(&stream->mutex);
384
385 if (uvc_queue_streaming(&stream->queue)) {
386 mutex_unlock(&stream->mutex);
387 return -EBUSY;
388 }
389
8c0d44e2 390 probe = stream->ctrl;
6947756d
LP
391 probe.dwFrameInterval =
392 uvc_try_frame_interval(stream->cur_frame, interval);
c0efd232
LP
393
394 /* Probe the device with the new settings. */
35f02a68 395 ret = uvc_probe_video(stream, &probe);
6947756d
LP
396 if (ret < 0) {
397 mutex_unlock(&stream->mutex);
c0efd232 398 return ret;
6947756d 399 }
c0efd232 400
8c0d44e2 401 stream->ctrl = probe;
6947756d 402 mutex_unlock(&stream->mutex);
c0efd232
LP
403
404 /* Return the actual frame period. */
ff924203
LP
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;
c0efd232
LP
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
2c2d264b 433 * a given time. Trying to perform an operation that requires privileges will
c0efd232 434 * automatically acquire the required privileges if possible, or return -EBUSY
1a969d98
LP
435 * otherwise. Privileges are dismissed when closing the instance or when
436 * freeing the video buffers using VIDIOC_REQBUFS.
c0efd232 437 *
2c2d264b 438 * Operations that require privileges are:
c0efd232
LP
439 *
440 * - VIDIOC_S_INPUT
441 * - VIDIOC_S_PARM
442 * - VIDIOC_S_FMT
c0efd232
LP
443 * - VIDIOC_REQBUFS
444 */
445static int uvc_acquire_privileges(struct uvc_fh *handle)
446{
c0efd232
LP
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. */
35f02a68
LP
452 if (atomic_inc_return(&handle->stream->active) != 1) {
453 atomic_dec(&handle->stream->active);
716fdee1 454 return -EBUSY;
c0efd232
LP
455 }
456
457 handle->state = UVC_HANDLE_ACTIVE;
716fdee1 458 return 0;
c0efd232
LP
459}
460
461static void uvc_dismiss_privileges(struct uvc_fh *handle)
462{
463 if (handle->state == UVC_HANDLE_ACTIVE)
35f02a68 464 atomic_dec(&handle->stream->active);
c0efd232
LP
465
466 handle->state = UVC_HANDLE_PASSIVE;
467}
468
469static 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
bec43661 478static int uvc_v4l2_open(struct file *file)
c0efd232 479{
35f02a68 480 struct uvc_streaming *stream;
c0efd232
LP
481 struct uvc_fh *handle;
482 int ret = 0;
483
484 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
35f02a68 485 stream = video_drvdata(file);
c0efd232 486
716fdee1
LP
487 if (stream->dev->state & UVC_DEV_DISCONNECTED)
488 return -ENODEV;
c0efd232 489
35f02a68 490 ret = usb_autopm_get_interface(stream->dev->intf);
c0efd232 491 if (ret < 0)
716fdee1 492 return ret;
c0efd232
LP
493
494 /* Create the device handle. */
495 handle = kzalloc(sizeof *handle, GFP_KERNEL);
496 if (handle == NULL) {
35f02a68 497 usb_autopm_put_interface(stream->dev->intf);
716fdee1 498 return -ENOMEM;
c0efd232
LP
499 }
500
17706f56
LP
501 mutex_lock(&stream->dev->lock);
502 if (stream->dev->users == 0) {
503 ret = uvc_status_start(stream->dev, GFP_KERNEL);
35f02a68 504 if (ret < 0) {
17706f56 505 mutex_unlock(&stream->dev->lock);
a82a45f6 506 usb_autopm_put_interface(stream->dev->intf);
04a37e0f 507 kfree(handle);
716fdee1 508 return ret;
04a37e0f
LP
509 }
510 }
511
17706f56
LP
512 stream->dev->users++;
513 mutex_unlock(&stream->dev->lock);
514
b4012002
HG
515 v4l2_fh_init(&handle->vfh, stream->vdev);
516 v4l2_fh_add(&handle->vfh);
8e113595 517 handle->chain = stream->chain;
35f02a68 518 handle->stream = stream;
c0efd232
LP
519 handle->state = UVC_HANDLE_PASSIVE;
520 file->private_data = handle;
521
716fdee1 522 return 0;
c0efd232
LP
523}
524
bec43661 525static int uvc_v4l2_release(struct file *file)
c0efd232 526{
abf84383 527 struct uvc_fh *handle = file->private_data;
35f02a68 528 struct uvc_streaming *stream = handle->stream;
c0efd232
LP
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)) {
35f02a68 534 uvc_video_enable(stream, 0);
6998b6fb 535 uvc_free_buffers(&stream->queue);
c0efd232
LP
536 }
537
538 /* Release the file handle. */
539 uvc_dismiss_privileges(handle);
b4012002
HG
540 v4l2_fh_del(&handle->vfh);
541 v4l2_fh_exit(&handle->vfh);
c0efd232
LP
542 kfree(handle);
543 file->private_data = NULL;
544
17706f56
LP
545 mutex_lock(&stream->dev->lock);
546 if (--stream->dev->users == 0)
35f02a68 547 uvc_status_stop(stream->dev);
17706f56 548 mutex_unlock(&stream->dev->lock);
04a37e0f 549
35f02a68 550 usb_autopm_put_interface(stream->dev->intf);
c0efd232
LP
551 return 0;
552}
553
069b7479 554static long uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
c0efd232
LP
555{
556 struct video_device *vdev = video_devdata(file);
abf84383 557 struct uvc_fh *handle = file->private_data;
8e113595 558 struct uvc_video_chain *chain = handle->chain;
35f02a68 559 struct uvc_streaming *stream = handle->stream;
069b7479 560 long ret = 0;
c0efd232 561
c0efd232
LP
562 switch (cmd) {
563 /* Query capabilities */
564 case VIDIOC_QUERYCAP:
565 {
566 struct v4l2_capability *cap = arg;
567
568 memset(cap, 0, sizeof *cap);
d0ebf307
LP
569 strlcpy(cap->driver, "uvcvideo", sizeof cap->driver);
570 strlcpy(cap->card, vdev->name, sizeof cap->card);
35f02a68 571 usb_make_path(stream->dev->udev,
b12049a2 572 cap->bus_info, sizeof(cap->bus_info));
fd3e5824 573 cap->version = LINUX_VERSION_CODE;
f887e99a
LP
574 cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
575 | chain->caps;
35f02a68 576 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
f887e99a
LP
577 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE
578 | V4L2_CAP_STREAMING;
ff924203 579 else
f887e99a
LP
580 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT
581 | V4L2_CAP_STREAMING;
c0efd232
LP
582 break;
583 }
584
0550513c
LP
585 /* Priority */
586 case VIDIOC_G_PRIORITY:
587 *(u32 *)arg = v4l2_prio_max(vdev->prio);
588 break;
589
590 case VIDIOC_S_PRIORITY:
591 ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
592 if (ret < 0)
593 return ret;
594
595 return v4l2_prio_change(vdev->prio, &handle->vfh.prio,
596 *(u32 *)arg);
597
c0efd232
LP
598 /* Get, Set & Query control */
599 case VIDIOC_QUERYCTRL:
8e113595 600 return uvc_query_v4l2_ctrl(chain, arg);
c0efd232
LP
601
602 case VIDIOC_G_CTRL:
603 {
604 struct v4l2_control *ctrl = arg;
605 struct v4l2_ext_control xctrl;
606
607 memset(&xctrl, 0, sizeof xctrl);
608 xctrl.id = ctrl->id;
609
8e113595
LP
610 ret = uvc_ctrl_begin(chain);
611 if (ret < 0)
1fcbcc47
RK
612 return ret;
613
8e113595 614 ret = uvc_ctrl_get(chain, &xctrl);
b4012002 615 uvc_ctrl_rollback(handle);
9c016d61
RW
616 if (ret >= 0)
617 ctrl->value = xctrl.value;
c0efd232
LP
618 break;
619 }
620
621 case VIDIOC_S_CTRL:
622 {
623 struct v4l2_control *ctrl = arg;
624 struct v4l2_ext_control xctrl;
625
0550513c
LP
626 ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
627 if (ret < 0)
628 return ret;
629
c0efd232
LP
630 memset(&xctrl, 0, sizeof xctrl);
631 xctrl.id = ctrl->id;
632 xctrl.value = ctrl->value;
633
8d556625 634 ret = uvc_ctrl_begin(chain);
8e113595 635 if (ret < 0)
1fcbcc47
RK
636 return ret;
637
8e113595 638 ret = uvc_ctrl_set(chain, &xctrl);
c0efd232 639 if (ret < 0) {
b4012002 640 uvc_ctrl_rollback(handle);
9c016d61 641 return ret;
c0efd232 642 }
b4012002 643 ret = uvc_ctrl_commit(handle, &xctrl, 1);
e54532e5
LP
644 if (ret == 0)
645 ctrl->value = xctrl.value;
c0efd232
LP
646 break;
647 }
648
649 case VIDIOC_QUERYMENU:
23d9f3ef 650 return uvc_query_v4l2_menu(chain, arg);
c0efd232
LP
651
652 case VIDIOC_G_EXT_CTRLS:
653 {
654 struct v4l2_ext_controls *ctrls = arg;
655 struct v4l2_ext_control *ctrl = ctrls->controls;
656 unsigned int i;
657
8e113595
LP
658 ret = uvc_ctrl_begin(chain);
659 if (ret < 0)
1fcbcc47
RK
660 return ret;
661
c0efd232 662 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
8e113595 663 ret = uvc_ctrl_get(chain, ctrl);
c0efd232 664 if (ret < 0) {
b4012002 665 uvc_ctrl_rollback(handle);
29005c09 666 ctrls->error_idx = i;
9c016d61 667 return ret;
c0efd232
LP
668 }
669 }
670 ctrls->error_idx = 0;
b4012002 671 ret = uvc_ctrl_rollback(handle);
c0efd232
LP
672 break;
673 }
674
675 case VIDIOC_S_EXT_CTRLS:
0550513c
LP
676 ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
677 if (ret < 0)
678 return ret;
679 /* Fall through */
c0efd232
LP
680 case VIDIOC_TRY_EXT_CTRLS:
681 {
682 struct v4l2_ext_controls *ctrls = arg;
683 struct v4l2_ext_control *ctrl = ctrls->controls;
684 unsigned int i;
685
8e113595 686 ret = uvc_ctrl_begin(chain);
c0efd232
LP
687 if (ret < 0)
688 return ret;
689
690 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
8e113595 691 ret = uvc_ctrl_set(chain, ctrl);
c0efd232 692 if (ret < 0) {
b4012002 693 uvc_ctrl_rollback(handle);
68d6f84b
LP
694 ctrls->error_idx = cmd == VIDIOC_S_EXT_CTRLS
695 ? ctrls->count : i;
9c016d61 696 return ret;
c0efd232
LP
697 }
698 }
699
700 ctrls->error_idx = 0;
701
702 if (cmd == VIDIOC_S_EXT_CTRLS)
b4012002
HG
703 ret = uvc_ctrl_commit(handle,
704 ctrls->controls, ctrls->count);
c0efd232 705 else
b4012002 706 ret = uvc_ctrl_rollback(handle);
c0efd232
LP
707 break;
708 }
709
710 /* Get, Set & Enum input */
711 case VIDIOC_ENUMINPUT:
712 {
8e113595 713 const struct uvc_entity *selector = chain->selector;
c0efd232
LP
714 struct v4l2_input *input = arg;
715 struct uvc_entity *iterm = NULL;
716 u32 index = input->index;
717 int pin = 0;
718
719 if (selector == NULL ||
8e113595 720 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
c0efd232
LP
721 if (index != 0)
722 return -EINVAL;
6241d8ca
LP
723 list_for_each_entry(iterm, &chain->entities, chain) {
724 if (UVC_ENTITY_IS_ITERM(iterm))
725 break;
726 }
c0efd232 727 pin = iterm->id;
31c5f0c5 728 } else if (index < selector->bNrInPins) {
8ca5a639 729 pin = selector->baSourceID[index];
6241d8ca
LP
730 list_for_each_entry(iterm, &chain->entities, chain) {
731 if (!UVC_ENTITY_IS_ITERM(iterm))
732 continue;
c0efd232
LP
733 if (iterm->id == pin)
734 break;
735 }
736 }
737
738 if (iterm == NULL || iterm->id != pin)
739 return -EINVAL;
740
741 memset(input, 0, sizeof *input);
742 input->index = index;
d0ebf307 743 strlcpy(input->name, iterm->name, sizeof input->name);
b482d923 744 if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
c0efd232
LP
745 input->type = V4L2_INPUT_TYPE_CAMERA;
746 break;
747 }
748
749 case VIDIOC_G_INPUT:
750 {
751 u8 input;
752
8e113595
LP
753 if (chain->selector == NULL ||
754 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
c0efd232
LP
755 *(int *)arg = 0;
756 break;
757 }
758
8e113595
LP
759 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
760 chain->selector->id, chain->dev->intfnum,
b482d923 761 UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
c0efd232
LP
762 if (ret < 0)
763 return ret;
764
765 *(int *)arg = input - 1;
766 break;
767 }
768
769 case VIDIOC_S_INPUT:
770 {
9086c7b9 771 u32 input = *(u32 *)arg + 1;
c0efd232 772
0550513c
LP
773 ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
774 if (ret < 0)
775 return ret;
776
c0efd232
LP
777 if ((ret = uvc_acquire_privileges(handle)) < 0)
778 return ret;
779
8e113595
LP
780 if (chain->selector == NULL ||
781 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
c0efd232
LP
782 if (input != 1)
783 return -EINVAL;
784 break;
785 }
786
8ca5a639 787 if (input == 0 || input > chain->selector->bNrInPins)
c0efd232
LP
788 return -EINVAL;
789
8e113595
LP
790 return uvc_query_ctrl(chain->dev, UVC_SET_CUR,
791 chain->selector->id, chain->dev->intfnum,
b482d923 792 UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
c0efd232
LP
793 }
794
795 /* Try, Get, Set & Enum format */
796 case VIDIOC_ENUM_FMT:
797 {
798 struct v4l2_fmtdesc *fmt = arg;
799 struct uvc_format *format;
8bbd90ce
MN
800 enum v4l2_buf_type type = fmt->type;
801 __u32 index = fmt->index;
c0efd232 802
35f02a68
LP
803 if (fmt->type != stream->type ||
804 fmt->index >= stream->nformats)
c0efd232
LP
805 return -EINVAL;
806
8bbd90ce
MN
807 memset(fmt, 0, sizeof(*fmt));
808 fmt->index = index;
809 fmt->type = type;
810
35f02a68 811 format = &stream->format[fmt->index];
c0efd232
LP
812 fmt->flags = 0;
813 if (format->flags & UVC_FMT_FLAG_COMPRESSED)
814 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
d0ebf307 815 strlcpy(fmt->description, format->name,
c0efd232
LP
816 sizeof fmt->description);
817 fmt->description[sizeof fmt->description - 1] = 0;
818 fmt->pixelformat = format->fcc;
819 break;
820 }
821
822 case VIDIOC_TRY_FMT:
823 {
824 struct uvc_streaming_control probe;
825
35f02a68 826 return uvc_v4l2_try_format(stream, arg, &probe, NULL, NULL);
c0efd232
LP
827 }
828
829 case VIDIOC_S_FMT:
0550513c
LP
830 ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
831 if (ret < 0)
832 return ret;
833
c0efd232
LP
834 if ((ret = uvc_acquire_privileges(handle)) < 0)
835 return ret;
836
35f02a68 837 return uvc_v4l2_set_format(stream, arg);
c0efd232
LP
838
839 case VIDIOC_G_FMT:
35f02a68 840 return uvc_v4l2_get_format(stream, arg);
c0efd232
LP
841
842 /* Frame size enumeration */
843 case VIDIOC_ENUM_FRAMESIZES:
844 {
845 struct v4l2_frmsizeenum *fsize = arg;
846 struct uvc_format *format = NULL;
847 struct uvc_frame *frame;
848 int i;
849
850 /* Look for the given pixel format */
35f02a68
LP
851 for (i = 0; i < stream->nformats; i++) {
852 if (stream->format[i].fcc ==
c0efd232 853 fsize->pixel_format) {
35f02a68 854 format = &stream->format[i];
c0efd232
LP
855 break;
856 }
857 }
858 if (format == NULL)
859 return -EINVAL;
860
861 if (fsize->index >= format->nframes)
862 return -EINVAL;
863
864 frame = &format->frame[fsize->index];
865 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
866 fsize->discrete.width = frame->wWidth;
867 fsize->discrete.height = frame->wHeight;
868 break;
869 }
870
871 /* Frame interval enumeration */
872 case VIDIOC_ENUM_FRAMEINTERVALS:
873 {
874 struct v4l2_frmivalenum *fival = arg;
875 struct uvc_format *format = NULL;
876 struct uvc_frame *frame = NULL;
877 int i;
878
879 /* Look for the given pixel format and frame size */
35f02a68
LP
880 for (i = 0; i < stream->nformats; i++) {
881 if (stream->format[i].fcc ==
c0efd232 882 fival->pixel_format) {
35f02a68 883 format = &stream->format[i];
c0efd232
LP
884 break;
885 }
886 }
887 if (format == NULL)
888 return -EINVAL;
889
890 for (i = 0; i < format->nframes; i++) {
891 if (format->frame[i].wWidth == fival->width &&
892 format->frame[i].wHeight == fival->height) {
893 frame = &format->frame[i];
894 break;
895 }
896 }
897 if (frame == NULL)
898 return -EINVAL;
899
900 if (frame->bFrameIntervalType) {
901 if (fival->index >= frame->bFrameIntervalType)
902 return -EINVAL;
903
904 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
905 fival->discrete.numerator =
906 frame->dwFrameInterval[fival->index];
907 fival->discrete.denominator = 10000000;
908 uvc_simplify_fraction(&fival->discrete.numerator,
909 &fival->discrete.denominator, 8, 333);
910 } else {
911 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
912 fival->stepwise.min.numerator =
913 frame->dwFrameInterval[0];
914 fival->stepwise.min.denominator = 10000000;
915 fival->stepwise.max.numerator =
916 frame->dwFrameInterval[1];
917 fival->stepwise.max.denominator = 10000000;
918 fival->stepwise.step.numerator =
919 frame->dwFrameInterval[2];
920 fival->stepwise.step.denominator = 10000000;
921 uvc_simplify_fraction(&fival->stepwise.min.numerator,
922 &fival->stepwise.min.denominator, 8, 333);
923 uvc_simplify_fraction(&fival->stepwise.max.numerator,
924 &fival->stepwise.max.denominator, 8, 333);
925 uvc_simplify_fraction(&fival->stepwise.step.numerator,
926 &fival->stepwise.step.denominator, 8, 333);
927 }
928 break;
929 }
930
931 /* Get & Set streaming parameters */
932 case VIDIOC_G_PARM:
35f02a68 933 return uvc_v4l2_get_streamparm(stream, arg);
c0efd232
LP
934
935 case VIDIOC_S_PARM:
0550513c
LP
936 ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
937 if (ret < 0)
938 return ret;
939
c0efd232
LP
940 if ((ret = uvc_acquire_privileges(handle)) < 0)
941 return ret;
942
35f02a68 943 return uvc_v4l2_set_streamparm(stream, arg);
c0efd232
LP
944
945 /* Cropping and scaling */
946 case VIDIOC_CROPCAP:
947 {
948 struct v4l2_cropcap *ccap = arg;
c0efd232 949
35f02a68 950 if (ccap->type != stream->type)
c0efd232
LP
951 return -EINVAL;
952
953 ccap->bounds.left = 0;
954 ccap->bounds.top = 0;
6947756d
LP
955
956 mutex_lock(&stream->mutex);
957 ccap->bounds.width = stream->cur_frame->wWidth;
958 ccap->bounds.height = stream->cur_frame->wHeight;
959 mutex_unlock(&stream->mutex);
c0efd232
LP
960
961 ccap->defrect = ccap->bounds;
962
963 ccap->pixelaspect.numerator = 1;
964 ccap->pixelaspect.denominator = 1;
965 break;
966 }
967
968 case VIDIOC_G_CROP:
969 case VIDIOC_S_CROP:
69d11262 970 return -ENOTTY;
c0efd232
LP
971
972 /* Buffers & streaming */
973 case VIDIOC_REQBUFS:
0550513c
LP
974 ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
975 if (ret < 0)
976 return ret;
977
c0efd232
LP
978 if ((ret = uvc_acquire_privileges(handle)) < 0)
979 return ret;
980
6947756d 981 mutex_lock(&stream->mutex);
6998b6fb 982 ret = uvc_alloc_buffers(&stream->queue, arg);
6947756d 983 mutex_unlock(&stream->mutex);
c0efd232
LP
984 if (ret < 0)
985 return ret;
986
1a969d98
LP
987 if (ret == 0)
988 uvc_dismiss_privileges(handle);
989
c0efd232
LP
990 ret = 0;
991 break;
c0efd232
LP
992
993 case VIDIOC_QUERYBUF:
994 {
995 struct v4l2_buffer *buf = arg;
996
c0efd232
LP
997 if (!uvc_has_privileges(handle))
998 return -EBUSY;
999
35f02a68 1000 return uvc_query_buffer(&stream->queue, buf);
c0efd232
LP
1001 }
1002
6e9179e2
PZ
1003 case VIDIOC_CREATE_BUFS:
1004 {
1005 struct v4l2_create_buffers *cb = arg;
1006
1007 ret = uvc_acquire_privileges(handle);
1008 if (ret < 0)
1009 return ret;
1010
1011 return uvc_create_buffers(&stream->queue, cb);
1012 }
1013
c0efd232
LP
1014 case VIDIOC_QBUF:
1015 if (!uvc_has_privileges(handle))
1016 return -EBUSY;
1017
35f02a68 1018 return uvc_queue_buffer(&stream->queue, arg);
c0efd232
LP
1019
1020 case VIDIOC_DQBUF:
1021 if (!uvc_has_privileges(handle))
1022 return -EBUSY;
1023
35f02a68 1024 return uvc_dequeue_buffer(&stream->queue, arg,
c0efd232
LP
1025 file->f_flags & O_NONBLOCK);
1026
1027 case VIDIOC_STREAMON:
1028 {
1029 int *type = arg;
1030
35f02a68 1031 if (*type != stream->type)
c0efd232
LP
1032 return -EINVAL;
1033
0550513c
LP
1034 ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
1035 if (ret < 0)
1036 return ret;
1037
c0efd232
LP
1038 if (!uvc_has_privileges(handle))
1039 return -EBUSY;
1040
6947756d 1041 mutex_lock(&stream->mutex);
35f02a68 1042 ret = uvc_video_enable(stream, 1);
6947756d 1043 mutex_unlock(&stream->mutex);
35f02a68 1044 if (ret < 0)
c0efd232
LP
1045 return ret;
1046 break;
1047 }
1048
1049 case VIDIOC_STREAMOFF:
1050 {
1051 int *type = arg;
1052
35f02a68 1053 if (*type != stream->type)
c0efd232
LP
1054 return -EINVAL;
1055
0550513c
LP
1056 ret = v4l2_prio_check(vdev->prio, handle->vfh.prio);
1057 if (ret < 0)
1058 return ret;
1059
c0efd232
LP
1060 if (!uvc_has_privileges(handle))
1061 return -EBUSY;
1062
35f02a68 1063 return uvc_video_enable(stream, 0);
c0efd232
LP
1064 }
1065
b4012002
HG
1066 case VIDIOC_SUBSCRIBE_EVENT:
1067 {
1068 struct v4l2_event_subscription *sub = arg;
1069
1070 switch (sub->type) {
1071 case V4L2_EVENT_CTRL:
1072 return v4l2_event_subscribe(&handle->vfh, sub, 0,
1073 &uvc_ctrl_sub_ev_ops);
1074 default:
1075 return -EINVAL;
1076 }
1077 }
1078
1079 case VIDIOC_UNSUBSCRIBE_EVENT:
1080 return v4l2_event_unsubscribe(&handle->vfh, arg);
1081
1082 case VIDIOC_DQEVENT:
1083 return v4l2_event_dequeue(&handle->vfh, arg,
1084 file->f_flags & O_NONBLOCK);
1085
c0efd232
LP
1086 /* Analog video standards make no sense for digital cameras. */
1087 case VIDIOC_ENUMSTD:
1088 case VIDIOC_QUERYSTD:
1089 case VIDIOC_G_STD:
1090 case VIDIOC_S_STD:
1091
1092 case VIDIOC_OVERLAY:
1093
1094 case VIDIOC_ENUMAUDIO:
1095 case VIDIOC_ENUMAUDOUT:
1096
1097 case VIDIOC_ENUMOUTPUT:
1098 uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd);
69d11262 1099 return -ENOTTY;
c0efd232 1100
c0efd232 1101 case UVCIOC_CTRL_MAP:
227bd5b3 1102 return uvc_ioctl_ctrl_map(chain, arg);
fe78d187
MR
1103
1104 case UVCIOC_CTRL_QUERY:
1105 return uvc_xu_ctrl_query(chain, arg);
c0efd232
LP
1106
1107 default:
08af245d 1108 uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n", cmd);
a3ec69b7 1109 return -ENOTTY;
c0efd232
LP
1110 }
1111
1112 return ret;
1113}
1114
069b7479 1115static long uvc_v4l2_ioctl(struct file *file,
c0efd232
LP
1116 unsigned int cmd, unsigned long arg)
1117{
308bb52c
LP
1118 if (uvc_trace_param & UVC_TRACE_IOCTL) {
1119 uvc_printk(KERN_DEBUG, "uvc_v4l2_ioctl(");
4a085168 1120 v4l_printk_ioctl(NULL, cmd);
308bb52c
LP
1121 printk(")\n");
1122 }
1123
f473bf76 1124 return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl);
c0efd232
LP
1125}
1126
1a5e4c86
LP
1127#ifdef CONFIG_COMPAT
1128struct uvc_xu_control_mapping32 {
1129 __u32 id;
1130 __u8 name[32];
1131 __u8 entity[16];
1132 __u8 selector;
1133
1134 __u8 size;
1135 __u8 offset;
1136 __u32 v4l2_type;
1137 __u32 data_type;
1138
1139 compat_caddr_t menu_info;
1140 __u32 menu_count;
1141
1142 __u32 reserved[4];
1143};
1144
1145static int uvc_v4l2_get_xu_mapping(struct uvc_xu_control_mapping *kp,
1146 const struct uvc_xu_control_mapping32 __user *up)
1147{
1148 struct uvc_menu_info __user *umenus;
1149 struct uvc_menu_info __user *kmenus;
1150 compat_caddr_t p;
1151
1152 if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
1153 __copy_from_user(kp, up, offsetof(typeof(*up), menu_info)) ||
1154 __get_user(kp->menu_count, &up->menu_count))
1155 return -EFAULT;
1156
1157 memset(kp->reserved, 0, sizeof(kp->reserved));
1158
1159 if (kp->menu_count == 0) {
1160 kp->menu_info = NULL;
1161 return 0;
1162 }
1163
1164 if (__get_user(p, &up->menu_info))
1165 return -EFAULT;
1166 umenus = compat_ptr(p);
1167 if (!access_ok(VERIFY_READ, umenus, kp->menu_count * sizeof(*umenus)))
1168 return -EFAULT;
1169
1170 kmenus = compat_alloc_user_space(kp->menu_count * sizeof(*kmenus));
1171 if (kmenus == NULL)
1172 return -EFAULT;
1173 kp->menu_info = kmenus;
1174
1175 if (copy_in_user(kmenus, umenus, kp->menu_count * sizeof(*umenus)))
1176 return -EFAULT;
1177
1178 return 0;
1179}
1180
1181static int uvc_v4l2_put_xu_mapping(const struct uvc_xu_control_mapping *kp,
1182 struct uvc_xu_control_mapping32 __user *up)
1183{
1184 struct uvc_menu_info __user *umenus;
1185 struct uvc_menu_info __user *kmenus = kp->menu_info;
1186 compat_caddr_t p;
1187
1188 if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
1189 __copy_to_user(up, kp, offsetof(typeof(*up), menu_info)) ||
1190 __put_user(kp->menu_count, &up->menu_count))
1191 return -EFAULT;
1192
57fb4a48
HG
1193 if (__clear_user(up->reserved, sizeof(up->reserved)))
1194 return -EFAULT;
1a5e4c86
LP
1195
1196 if (kp->menu_count == 0)
1197 return 0;
1198
1199 if (get_user(p, &up->menu_info))
1200 return -EFAULT;
1201 umenus = compat_ptr(p);
1a5e4c86
LP
1202
1203 if (copy_in_user(umenus, kmenus, kp->menu_count * sizeof(*umenus)))
1204 return -EFAULT;
1205
1206 return 0;
1207}
1208
1209struct uvc_xu_control_query32 {
1210 __u8 unit;
1211 __u8 selector;
1212 __u8 query;
1213 __u16 size;
1214 compat_caddr_t data;
1215};
1216
1217static int uvc_v4l2_get_xu_query(struct uvc_xu_control_query *kp,
1218 const struct uvc_xu_control_query32 __user *up)
1219{
1220 u8 __user *udata;
1221 u8 __user *kdata;
1222 compat_caddr_t p;
1223
1224 if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
1225 __copy_from_user(kp, up, offsetof(typeof(*up), data)))
1226 return -EFAULT;
1227
1228 if (kp->size == 0) {
1229 kp->data = NULL;
1230 return 0;
1231 }
1232
1233 if (__get_user(p, &up->data))
1234 return -EFAULT;
1235 udata = compat_ptr(p);
1236 if (!access_ok(VERIFY_READ, udata, kp->size))
1237 return -EFAULT;
1238
1239 kdata = compat_alloc_user_space(kp->size);
1240 if (kdata == NULL)
1241 return -EFAULT;
1242 kp->data = kdata;
1243
1244 if (copy_in_user(kdata, udata, kp->size))
1245 return -EFAULT;
1246
1247 return 0;
1248}
1249
1250static int uvc_v4l2_put_xu_query(const struct uvc_xu_control_query *kp,
1251 struct uvc_xu_control_query32 __user *up)
1252{
1253 u8 __user *udata;
1254 u8 __user *kdata = kp->data;
1255 compat_caddr_t p;
1256
1257 if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
1258 __copy_to_user(up, kp, offsetof(typeof(*up), data)))
1259 return -EFAULT;
1260
1261 if (kp->size == 0)
1262 return 0;
1263
1264 if (get_user(p, &up->data))
1265 return -EFAULT;
1266 udata = compat_ptr(p);
1267 if (!access_ok(VERIFY_READ, udata, kp->size))
1268 return -EFAULT;
1269
1270 if (copy_in_user(udata, kdata, kp->size))
1271 return -EFAULT;
1272
1273 return 0;
1274}
1275
1276#define UVCIOC_CTRL_MAP32 _IOWR('u', 0x20, struct uvc_xu_control_mapping32)
1277#define UVCIOC_CTRL_QUERY32 _IOWR('u', 0x21, struct uvc_xu_control_query32)
1278
1279static long uvc_v4l2_compat_ioctl32(struct file *file,
1280 unsigned int cmd, unsigned long arg)
1281{
1282 union {
1283 struct uvc_xu_control_mapping xmap;
1284 struct uvc_xu_control_query xqry;
1285 } karg;
1286 void __user *up = compat_ptr(arg);
1287 mm_segment_t old_fs;
1288 long ret;
1289
1290 switch (cmd) {
1291 case UVCIOC_CTRL_MAP32:
1292 cmd = UVCIOC_CTRL_MAP;
1293 ret = uvc_v4l2_get_xu_mapping(&karg.xmap, up);
1294 break;
1295
1296 case UVCIOC_CTRL_QUERY32:
1297 cmd = UVCIOC_CTRL_QUERY;
1298 ret = uvc_v4l2_get_xu_query(&karg.xqry, up);
1299 break;
1300
1301 default:
1302 return -ENOIOCTLCMD;
1303 }
1304
1305 old_fs = get_fs();
1306 set_fs(KERNEL_DS);
1307 ret = uvc_v4l2_ioctl(file, cmd, (unsigned long)&karg);
1308 set_fs(old_fs);
1309
1310 if (ret < 0)
1311 return ret;
1312
1313 switch (cmd) {
1314 case UVCIOC_CTRL_MAP:
1315 ret = uvc_v4l2_put_xu_mapping(&karg.xmap, up);
1316 break;
1317
1318 case UVCIOC_CTRL_QUERY:
1319 ret = uvc_v4l2_put_xu_query(&karg.xqry, up);
1320 break;
1321 }
1322
1323 return ret;
1324}
1325#endif
1326
c0efd232
LP
1327static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1328 size_t count, loff_t *ppos)
1329{
1330 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
350d6407 1331 return -EINVAL;
c0efd232
LP
1332}
1333
c0efd232
LP
1334static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1335{
abf84383 1336 struct uvc_fh *handle = file->private_data;
35f02a68 1337 struct uvc_streaming *stream = handle->stream;
c0efd232
LP
1338
1339 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
1340
4aa27597 1341 return uvc_queue_mmap(&stream->queue, vma);
c0efd232
LP
1342}
1343
1344static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
1345{
abf84383 1346 struct uvc_fh *handle = file->private_data;
35f02a68 1347 struct uvc_streaming *stream = handle->stream;
c0efd232
LP
1348
1349 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
1350
35f02a68 1351 return uvc_queue_poll(&stream->queue, file, wait);
c0efd232
LP
1352}
1353
72969447
BL
1354#ifndef CONFIG_MMU
1355static unsigned long uvc_v4l2_get_unmapped_area(struct file *file,
1356 unsigned long addr, unsigned long len, unsigned long pgoff,
1357 unsigned long flags)
1358{
1359 struct uvc_fh *handle = file->private_data;
1360 struct uvc_streaming *stream = handle->stream;
1361
1362 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_get_unmapped_area\n");
1363
1364 return uvc_queue_get_unmapped_area(&stream->queue, pgoff);
1365}
1366#endif
1367
bec43661 1368const struct v4l2_file_operations uvc_fops = {
c0efd232
LP
1369 .owner = THIS_MODULE,
1370 .open = uvc_v4l2_open,
1371 .release = uvc_v4l2_release,
673eb9ff 1372 .unlocked_ioctl = uvc_v4l2_ioctl,
1a5e4c86
LP
1373#ifdef CONFIG_COMPAT
1374 .compat_ioctl32 = uvc_v4l2_compat_ioctl32,
1375#endif
c0efd232
LP
1376 .read = uvc_v4l2_read,
1377 .mmap = uvc_v4l2_mmap,
1378 .poll = uvc_v4l2_poll,
72969447
BL
1379#ifndef CONFIG_MMU
1380 .get_unmapped_area = uvc_v4l2_get_unmapped_area,
1381#endif
c0efd232 1382};
f87086e3 1383