[media] uvcvideo: Implement vb2 queue start and stop stream operations
[deliverable/linux.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)) {
b83bba24 534 uvc_queue_enable(&stream->queue, 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
d5e90b7a
LP
554static int uvc_ioctl_querycap(struct file *file, void *fh,
555 struct v4l2_capability *cap)
c0efd232
LP
556{
557 struct video_device *vdev = video_devdata(file);
abf84383 558 struct uvc_fh *handle = file->private_data;
8e113595 559 struct uvc_video_chain *chain = handle->chain;
35f02a68 560 struct uvc_streaming *stream = handle->stream;
c0efd232 561
d5e90b7a
LP
562 strlcpy(cap->driver, "uvcvideo", sizeof(cap->driver));
563 strlcpy(cap->card, vdev->name, sizeof(cap->card));
564 usb_make_path(stream->dev->udev, cap->bus_info, sizeof(cap->bus_info));
565 cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
566 | chain->caps;
567 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
568 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
569 else
570 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
c0efd232 571
d5e90b7a
LP
572 return 0;
573}
0550513c 574
d5e90b7a
LP
575static int uvc_ioctl_enum_fmt(struct uvc_streaming *stream,
576 struct v4l2_fmtdesc *fmt)
577{
578 struct uvc_format *format;
579 enum v4l2_buf_type type = fmt->type;
580 __u32 index = fmt->index;
0550513c 581
d5e90b7a
LP
582 if (fmt->type != stream->type || fmt->index >= stream->nformats)
583 return -EINVAL;
0550513c 584
d5e90b7a
LP
585 memset(fmt, 0, sizeof(*fmt));
586 fmt->index = index;
587 fmt->type = type;
588
589 format = &stream->format[fmt->index];
590 fmt->flags = 0;
591 if (format->flags & UVC_FMT_FLAG_COMPRESSED)
592 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
593 strlcpy(fmt->description, format->name, sizeof(fmt->description));
594 fmt->description[sizeof(fmt->description) - 1] = 0;
595 fmt->pixelformat = format->fcc;
596 return 0;
597}
c0efd232 598
d5e90b7a
LP
599static int uvc_ioctl_enum_fmt_vid_cap(struct file *file, void *fh,
600 struct v4l2_fmtdesc *fmt)
601{
602 struct uvc_fh *handle = fh;
603 struct uvc_streaming *stream = handle->stream;
c0efd232 604
d5e90b7a
LP
605 return uvc_ioctl_enum_fmt(stream, fmt);
606}
c0efd232 607
d5e90b7a
LP
608static int uvc_ioctl_enum_fmt_vid_out(struct file *file, void *fh,
609 struct v4l2_fmtdesc *fmt)
610{
611 struct uvc_fh *handle = fh;
612 struct uvc_streaming *stream = handle->stream;
1fcbcc47 613
d5e90b7a
LP
614 return uvc_ioctl_enum_fmt(stream, fmt);
615}
c0efd232 616
d5e90b7a
LP
617static int uvc_ioctl_g_fmt_vid_cap(struct file *file, void *fh,
618 struct v4l2_format *fmt)
619{
620 struct uvc_fh *handle = fh;
621 struct uvc_streaming *stream = handle->stream;
c0efd232 622
d5e90b7a
LP
623 return uvc_v4l2_get_format(stream, fmt);
624}
0550513c 625
d5e90b7a
LP
626static int uvc_ioctl_g_fmt_vid_out(struct file *file, void *fh,
627 struct v4l2_format *fmt)
628{
629 struct uvc_fh *handle = fh;
630 struct uvc_streaming *stream = handle->stream;
c0efd232 631
d5e90b7a
LP
632 return uvc_v4l2_get_format(stream, fmt);
633}
1fcbcc47 634
d5e90b7a
LP
635static int uvc_ioctl_s_fmt_vid_cap(struct file *file, void *fh,
636 struct v4l2_format *fmt)
637{
638 struct uvc_fh *handle = fh;
639 struct uvc_streaming *stream = handle->stream;
640 int ret;
c0efd232 641
d5e90b7a
LP
642 ret = uvc_acquire_privileges(handle);
643 if (ret < 0)
644 return ret;
c0efd232 645
d5e90b7a
LP
646 return uvc_v4l2_set_format(stream, fmt);
647}
c0efd232 648
d5e90b7a
LP
649static int uvc_ioctl_s_fmt_vid_out(struct file *file, void *fh,
650 struct v4l2_format *fmt)
651{
652 struct uvc_fh *handle = fh;
653 struct uvc_streaming *stream = handle->stream;
654 int ret;
1fcbcc47 655
d5e90b7a
LP
656 ret = uvc_acquire_privileges(handle);
657 if (ret < 0)
658 return ret;
c0efd232 659
d5e90b7a
LP
660 return uvc_v4l2_set_format(stream, fmt);
661}
c0efd232 662
d5e90b7a
LP
663static int uvc_ioctl_try_fmt_vid_cap(struct file *file, void *fh,
664 struct v4l2_format *fmt)
665{
666 struct uvc_fh *handle = fh;
667 struct uvc_streaming *stream = handle->stream;
668 struct uvc_streaming_control probe;
c0efd232 669
d5e90b7a
LP
670 return uvc_v4l2_try_format(stream, fmt, &probe, NULL, NULL);
671}
c0efd232 672
d5e90b7a
LP
673static int uvc_ioctl_try_fmt_vid_out(struct file *file, void *fh,
674 struct v4l2_format *fmt)
675{
676 struct uvc_fh *handle = fh;
677 struct uvc_streaming *stream = handle->stream;
678 struct uvc_streaming_control probe;
c0efd232 679
d5e90b7a
LP
680 return uvc_v4l2_try_format(stream, fmt, &probe, NULL, NULL);
681}
c0efd232 682
d5e90b7a
LP
683static int uvc_ioctl_reqbufs(struct file *file, void *fh,
684 struct v4l2_requestbuffers *rb)
685{
686 struct uvc_fh *handle = fh;
687 struct uvc_streaming *stream = handle->stream;
688 int ret;
c0efd232 689
d5e90b7a
LP
690 ret = uvc_acquire_privileges(handle);
691 if (ret < 0)
692 return ret;
c0efd232 693
d5e90b7a
LP
694 mutex_lock(&stream->mutex);
695 ret = uvc_alloc_buffers(&stream->queue, rb);
696 mutex_unlock(&stream->mutex);
697 if (ret < 0)
698 return ret;
c0efd232 699
d5e90b7a
LP
700 if (ret == 0)
701 uvc_dismiss_privileges(handle);
c0efd232 702
d5e90b7a
LP
703 return 0;
704}
c0efd232 705
d5e90b7a
LP
706static int uvc_ioctl_querybuf(struct file *file, void *fh,
707 struct v4l2_buffer *buf)
708{
709 struct uvc_fh *handle = fh;
710 struct uvc_streaming *stream = handle->stream;
c0efd232 711
d5e90b7a
LP
712 if (!uvc_has_privileges(handle))
713 return -EBUSY;
c0efd232 714
d5e90b7a
LP
715 return uvc_query_buffer(&stream->queue, buf);
716}
0550513c 717
d5e90b7a
LP
718static int uvc_ioctl_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
719{
720 struct uvc_fh *handle = fh;
721 struct uvc_streaming *stream = handle->stream;
c0efd232 722
d5e90b7a
LP
723 if (!uvc_has_privileges(handle))
724 return -EBUSY;
c0efd232 725
d5e90b7a
LP
726 return uvc_queue_buffer(&stream->queue, buf);
727}
c0efd232 728
d5e90b7a
LP
729static int uvc_ioctl_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
730{
731 struct uvc_fh *handle = fh;
732 struct uvc_streaming *stream = handle->stream;
c0efd232 733
d5e90b7a
LP
734 if (!uvc_has_privileges(handle))
735 return -EBUSY;
c0efd232 736
d5e90b7a
LP
737 return uvc_dequeue_buffer(&stream->queue, buf,
738 file->f_flags & O_NONBLOCK);
739}
c0efd232 740
d5e90b7a
LP
741static int uvc_ioctl_create_bufs(struct file *file, void *fh,
742 struct v4l2_create_buffers *cb)
743{
744 struct uvc_fh *handle = fh;
745 struct uvc_streaming *stream = handle->stream;
746 int ret;
c0efd232 747
d5e90b7a
LP
748 ret = uvc_acquire_privileges(handle);
749 if (ret < 0)
750 return ret;
c0efd232 751
d5e90b7a
LP
752 return uvc_create_buffers(&stream->queue, cb);
753}
c0efd232 754
d5e90b7a
LP
755static int uvc_ioctl_streamon(struct file *file, void *fh,
756 enum v4l2_buf_type type)
757{
758 struct uvc_fh *handle = fh;
759 struct uvc_streaming *stream = handle->stream;
760 int ret;
0550513c 761
d5e90b7a
LP
762 if (type != stream->type)
763 return -EINVAL;
764
765 if (!uvc_has_privileges(handle))
766 return -EBUSY;
767
768 mutex_lock(&stream->mutex);
b83bba24 769 ret = uvc_queue_enable(&stream->queue, 1);
d5e90b7a 770 mutex_unlock(&stream->mutex);
c0efd232 771
d5e90b7a
LP
772 return ret;
773}
c0efd232 774
d5e90b7a
LP
775static int uvc_ioctl_streamoff(struct file *file, void *fh,
776 enum v4l2_buf_type type)
777{
778 struct uvc_fh *handle = fh;
779 struct uvc_streaming *stream = handle->stream;
c0efd232 780
d5e90b7a
LP
781 if (type != stream->type)
782 return -EINVAL;
c0efd232 783
d5e90b7a
LP
784 if (!uvc_has_privileges(handle))
785 return -EBUSY;
c0efd232 786
d5e90b7a 787 mutex_lock(&stream->mutex);
b83bba24 788 uvc_queue_enable(&stream->queue, 0);
d5e90b7a 789 mutex_unlock(&stream->mutex);
c0efd232 790
b83bba24 791 return 0;
d5e90b7a 792}
c0efd232 793
d5e90b7a
LP
794static int uvc_ioctl_enum_input(struct file *file, void *fh,
795 struct v4l2_input *input)
796{
797 struct uvc_fh *handle = fh;
798 struct uvc_video_chain *chain = handle->chain;
799 const struct uvc_entity *selector = chain->selector;
800 struct uvc_entity *iterm = NULL;
801 u32 index = input->index;
802 int pin = 0;
803
804 if (selector == NULL ||
805 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
806 if (index != 0)
c0efd232 807 return -EINVAL;
d5e90b7a
LP
808 list_for_each_entry(iterm, &chain->entities, chain) {
809 if (UVC_ENTITY_IS_ITERM(iterm))
c0efd232 810 break;
c0efd232 811 }
d5e90b7a
LP
812 pin = iterm->id;
813 } else if (index < selector->bNrInPins) {
814 pin = selector->baSourceID[index];
815 list_for_each_entry(iterm, &chain->entities, chain) {
816 if (!UVC_ENTITY_IS_ITERM(iterm))
817 continue;
818 if (iterm->id == pin)
819 break;
c0efd232 820 }
c0efd232
LP
821 }
822
d5e90b7a
LP
823 if (iterm == NULL || iterm->id != pin)
824 return -EINVAL;
c0efd232 825
d5e90b7a
LP
826 memset(input, 0, sizeof(*input));
827 input->index = index;
828 strlcpy(input->name, iterm->name, sizeof(input->name));
829 if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
830 input->type = V4L2_INPUT_TYPE_CAMERA;
0550513c 831
d5e90b7a
LP
832 return 0;
833}
c0efd232 834
d5e90b7a
LP
835static int uvc_ioctl_g_input(struct file *file, void *fh, unsigned int *input)
836{
837 struct uvc_fh *handle = fh;
838 struct uvc_video_chain *chain = handle->chain;
839 int ret;
840 u8 i;
c0efd232 841
d5e90b7a
LP
842 if (chain->selector == NULL ||
843 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
844 *input = 0;
845 return 0;
846 }
c0efd232 847
d5e90b7a
LP
848 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, chain->selector->id,
849 chain->dev->intfnum, UVC_SU_INPUT_SELECT_CONTROL,
850 &i, 1);
851 if (ret < 0)
852 return ret;
c0efd232 853
d5e90b7a
LP
854 *input = i - 1;
855 return 0;
856}
6947756d 857
d5e90b7a
LP
858static int uvc_ioctl_s_input(struct file *file, void *fh, unsigned int input)
859{
860 struct uvc_fh *handle = fh;
861 struct uvc_video_chain *chain = handle->chain;
862 int ret;
863 u32 i;
c0efd232 864
d5e90b7a
LP
865 ret = uvc_acquire_privileges(handle);
866 if (ret < 0)
867 return ret;
c0efd232 868
d5e90b7a
LP
869 if (chain->selector == NULL ||
870 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
871 if (input)
872 return -EINVAL;
873 return 0;
c0efd232
LP
874 }
875
d5e90b7a
LP
876 if (input >= chain->selector->bNrInPins)
877 return -EINVAL;
c0efd232 878
d5e90b7a
LP
879 i = input + 1;
880 return uvc_query_ctrl(chain->dev, UVC_SET_CUR, chain->selector->id,
881 chain->dev->intfnum, UVC_SU_INPUT_SELECT_CONTROL,
882 &i, 1);
883}
0550513c 884
d5e90b7a
LP
885static int uvc_ioctl_queryctrl(struct file *file, void *fh,
886 struct v4l2_queryctrl *qc)
887{
888 struct uvc_fh *handle = fh;
889 struct uvc_video_chain *chain = handle->chain;
c0efd232 890
d5e90b7a
LP
891 return uvc_query_v4l2_ctrl(chain, qc);
892}
c0efd232 893
d5e90b7a
LP
894static int uvc_ioctl_g_ctrl(struct file *file, void *fh,
895 struct v4l2_control *ctrl)
896{
897 struct uvc_fh *handle = fh;
898 struct uvc_video_chain *chain = handle->chain;
899 struct v4l2_ext_control xctrl;
900 int ret;
1a969d98 901
d5e90b7a
LP
902 memset(&xctrl, 0, sizeof(xctrl));
903 xctrl.id = ctrl->id;
c0efd232 904
d5e90b7a
LP
905 ret = uvc_ctrl_begin(chain);
906 if (ret < 0)
907 return ret;
c0efd232 908
d5e90b7a
LP
909 ret = uvc_ctrl_get(chain, &xctrl);
910 uvc_ctrl_rollback(handle);
911 if (ret < 0)
912 return ret;
c0efd232 913
d5e90b7a
LP
914 ctrl->value = xctrl.value;
915 return 0;
916}
c0efd232 917
d5e90b7a
LP
918static int uvc_ioctl_s_ctrl(struct file *file, void *fh,
919 struct v4l2_control *ctrl)
920{
921 struct uvc_fh *handle = fh;
922 struct uvc_video_chain *chain = handle->chain;
923 struct v4l2_ext_control xctrl;
924 int ret;
6e9179e2 925
d5e90b7a
LP
926 memset(&xctrl, 0, sizeof(xctrl));
927 xctrl.id = ctrl->id;
928 xctrl.value = ctrl->value;
6e9179e2 929
d5e90b7a
LP
930 ret = uvc_ctrl_begin(chain);
931 if (ret < 0)
932 return ret;
933
934 ret = uvc_ctrl_set(chain, &xctrl);
935 if (ret < 0) {
936 uvc_ctrl_rollback(handle);
937 return ret;
6e9179e2
PZ
938 }
939
d5e90b7a
LP
940 ret = uvc_ctrl_commit(handle, &xctrl, 1);
941 if (ret < 0)
942 return ret;
c0efd232 943
d5e90b7a
LP
944 ctrl->value = xctrl.value;
945 return 0;
946}
c0efd232 947
d5e90b7a
LP
948static int uvc_ioctl_g_ext_ctrls(struct file *file, void *fh,
949 struct v4l2_ext_controls *ctrls)
950{
951 struct uvc_fh *handle = fh;
952 struct uvc_video_chain *chain = handle->chain;
953 struct v4l2_ext_control *ctrl = ctrls->controls;
954 unsigned int i;
955 int ret;
c0efd232 956
d5e90b7a
LP
957 ret = uvc_ctrl_begin(chain);
958 if (ret < 0)
959 return ret;
c0efd232 960
d5e90b7a
LP
961 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
962 ret = uvc_ctrl_get(chain, ctrl);
963 if (ret < 0) {
964 uvc_ctrl_rollback(handle);
965 ctrls->error_idx = i;
966 return ret;
967 }
968 }
c0efd232 969
d5e90b7a 970 ctrls->error_idx = 0;
c0efd232 971
d5e90b7a
LP
972 return uvc_ctrl_rollback(handle);
973}
0550513c 974
d5e90b7a
LP
975static int uvc_ioctl_s_try_ext_ctrls(struct uvc_fh *handle,
976 struct v4l2_ext_controls *ctrls,
977 bool commit)
978{
979 struct v4l2_ext_control *ctrl = ctrls->controls;
980 struct uvc_video_chain *chain = handle->chain;
981 unsigned int i;
982 int ret;
c0efd232 983
d5e90b7a
LP
984 ret = uvc_ctrl_begin(chain);
985 if (ret < 0)
986 return ret;
987
988 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
989 ret = uvc_ctrl_set(chain, ctrl);
990 if (ret < 0) {
991 uvc_ctrl_rollback(handle);
992 ctrls->error_idx = commit ? ctrls->count : i;
c0efd232 993 return ret;
d5e90b7a 994 }
c0efd232
LP
995 }
996
d5e90b7a 997 ctrls->error_idx = 0;
c0efd232 998
d5e90b7a
LP
999 if (commit)
1000 return uvc_ctrl_commit(handle, ctrls->controls, ctrls->count);
1001 else
1002 return uvc_ctrl_rollback(handle);
1003}
c0efd232 1004
d5e90b7a
LP
1005static int uvc_ioctl_s_ext_ctrls(struct file *file, void *fh,
1006 struct v4l2_ext_controls *ctrls)
1007{
1008 struct uvc_fh *handle = fh;
0550513c 1009
d5e90b7a
LP
1010 return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, true);
1011}
1012
1013static int uvc_ioctl_try_ext_ctrls(struct file *file, void *fh,
1014 struct v4l2_ext_controls *ctrls)
1015{
1016 struct uvc_fh *handle = fh;
1017
1018 return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, false);
1019}
c0efd232 1020
d5e90b7a
LP
1021static int uvc_ioctl_querymenu(struct file *file, void *fh,
1022 struct v4l2_querymenu *qm)
1023{
1024 struct uvc_fh *handle = fh;
1025 struct uvc_video_chain *chain = handle->chain;
1026
1027 return uvc_query_v4l2_menu(chain, qm);
1028}
1029
1030static int uvc_ioctl_cropcap(struct file *file, void *fh,
1031 struct v4l2_cropcap *ccap)
1032{
1033 struct uvc_fh *handle = fh;
1034 struct uvc_streaming *stream = handle->stream;
1035
1036 if (ccap->type != stream->type)
1037 return -EINVAL;
1038
1039 ccap->bounds.left = 0;
1040 ccap->bounds.top = 0;
1041 mutex_lock(&stream->mutex);
1042 ccap->bounds.width = stream->cur_frame->wWidth;
1043 ccap->bounds.height = stream->cur_frame->wHeight;
1044 mutex_unlock(&stream->mutex);
1045
1046 ccap->defrect = ccap->bounds;
1047
1048 ccap->pixelaspect.numerator = 1;
1049 ccap->pixelaspect.denominator = 1;
1050 return 0;
1051}
1052
1053static int uvc_ioctl_g_parm(struct file *file, void *fh,
1054 struct v4l2_streamparm *parm)
1055{
1056 struct uvc_fh *handle = fh;
1057 struct uvc_streaming *stream = handle->stream;
1058
1059 return uvc_v4l2_get_streamparm(stream, parm);
1060}
1061
1062static int uvc_ioctl_s_parm(struct file *file, void *fh,
1063 struct v4l2_streamparm *parm)
1064{
1065 struct uvc_fh *handle = fh;
1066 struct uvc_streaming *stream = handle->stream;
1067 int ret;
1068
1069 ret = uvc_acquire_privileges(handle);
1070 if (ret < 0)
1071 return ret;
1072
1073 return uvc_v4l2_set_streamparm(stream, parm);
1074}
1075
1076static int uvc_ioctl_enum_framesizes(struct file *file, void *fh,
1077 struct v4l2_frmsizeenum *fsize)
1078{
1079 struct uvc_fh *handle = fh;
1080 struct uvc_streaming *stream = handle->stream;
1081 struct uvc_format *format = NULL;
1082 struct uvc_frame *frame;
1083 int i;
1084
1085 /* Look for the given pixel format */
1086 for (i = 0; i < stream->nformats; i++) {
1087 if (stream->format[i].fcc == fsize->pixel_format) {
1088 format = &stream->format[i];
1089 break;
1090 }
c0efd232 1091 }
d5e90b7a
LP
1092 if (format == NULL)
1093 return -EINVAL;
c0efd232 1094
d5e90b7a
LP
1095 if (fsize->index >= format->nframes)
1096 return -EINVAL;
b4012002 1097
d5e90b7a
LP
1098 frame = &format->frame[fsize->index];
1099 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1100 fsize->discrete.width = frame->wWidth;
1101 fsize->discrete.height = frame->wHeight;
1102 return 0;
1103}
1104
1105static int uvc_ioctl_enum_frameintervals(struct file *file, void *fh,
1106 struct v4l2_frmivalenum *fival)
1107{
1108 struct uvc_fh *handle = fh;
1109 struct uvc_streaming *stream = handle->stream;
1110 struct uvc_format *format = NULL;
1111 struct uvc_frame *frame = NULL;
1112 int i;
1113
1114 /* Look for the given pixel format and frame size */
1115 for (i = 0; i < stream->nformats; i++) {
1116 if (stream->format[i].fcc == fival->pixel_format) {
1117 format = &stream->format[i];
1118 break;
b4012002
HG
1119 }
1120 }
d5e90b7a
LP
1121 if (format == NULL)
1122 return -EINVAL;
b4012002 1123
d5e90b7a
LP
1124 for (i = 0; i < format->nframes; i++) {
1125 if (format->frame[i].wWidth == fival->width &&
1126 format->frame[i].wHeight == fival->height) {
1127 frame = &format->frame[i];
1128 break;
1129 }
1130 }
1131 if (frame == NULL)
1132 return -EINVAL;
b4012002 1133
d5e90b7a
LP
1134 if (frame->bFrameIntervalType) {
1135 if (fival->index >= frame->bFrameIntervalType)
1136 return -EINVAL;
b4012002 1137
d5e90b7a
LP
1138 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1139 fival->discrete.numerator =
1140 frame->dwFrameInterval[fival->index];
1141 fival->discrete.denominator = 10000000;
1142 uvc_simplify_fraction(&fival->discrete.numerator,
1143 &fival->discrete.denominator, 8, 333);
1144 } else {
1145 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
1146 fival->stepwise.min.numerator = frame->dwFrameInterval[0];
1147 fival->stepwise.min.denominator = 10000000;
1148 fival->stepwise.max.numerator = frame->dwFrameInterval[1];
1149 fival->stepwise.max.denominator = 10000000;
1150 fival->stepwise.step.numerator = frame->dwFrameInterval[2];
1151 fival->stepwise.step.denominator = 10000000;
1152 uvc_simplify_fraction(&fival->stepwise.min.numerator,
1153 &fival->stepwise.min.denominator, 8, 333);
1154 uvc_simplify_fraction(&fival->stepwise.max.numerator,
1155 &fival->stepwise.max.denominator, 8, 333);
1156 uvc_simplify_fraction(&fival->stepwise.step.numerator,
1157 &fival->stepwise.step.denominator, 8, 333);
1158 }
c0efd232 1159
d5e90b7a
LP
1160 return 0;
1161}
c0efd232 1162
d5e90b7a
LP
1163static int uvc_ioctl_subscribe_event(struct v4l2_fh *fh,
1164 const struct v4l2_event_subscription *sub)
1165{
1166 switch (sub->type) {
1167 case V4L2_EVENT_CTRL:
1168 return v4l2_event_subscribe(fh, sub, 0, &uvc_ctrl_sub_ev_ops);
1169 default:
1170 return -EINVAL;
1171 }
1172}
c0efd232 1173
d5e90b7a
LP
1174static long uvc_ioctl_default(struct file *file, void *fh, bool valid_prio,
1175 unsigned int cmd, void *arg)
1176{
1177 struct uvc_fh *handle = fh;
1178 struct uvc_video_chain *chain = handle->chain;
c0efd232 1179
d5e90b7a
LP
1180 switch (cmd) {
1181 /* Dynamic controls. */
c0efd232 1182 case UVCIOC_CTRL_MAP:
227bd5b3 1183 return uvc_ioctl_ctrl_map(chain, arg);
fe78d187
MR
1184
1185 case UVCIOC_CTRL_QUERY:
1186 return uvc_xu_ctrl_query(chain, arg);
c0efd232
LP
1187
1188 default:
a3ec69b7 1189 return -ENOTTY;
c0efd232 1190 }
c0efd232
LP
1191}
1192
1a5e4c86
LP
1193#ifdef CONFIG_COMPAT
1194struct uvc_xu_control_mapping32 {
1195 __u32 id;
1196 __u8 name[32];
1197 __u8 entity[16];
1198 __u8 selector;
1199
1200 __u8 size;
1201 __u8 offset;
1202 __u32 v4l2_type;
1203 __u32 data_type;
1204
1205 compat_caddr_t menu_info;
1206 __u32 menu_count;
1207
1208 __u32 reserved[4];
1209};
1210
1211static int uvc_v4l2_get_xu_mapping(struct uvc_xu_control_mapping *kp,
1212 const struct uvc_xu_control_mapping32 __user *up)
1213{
1214 struct uvc_menu_info __user *umenus;
1215 struct uvc_menu_info __user *kmenus;
1216 compat_caddr_t p;
1217
1218 if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
1219 __copy_from_user(kp, up, offsetof(typeof(*up), menu_info)) ||
1220 __get_user(kp->menu_count, &up->menu_count))
1221 return -EFAULT;
1222
1223 memset(kp->reserved, 0, sizeof(kp->reserved));
1224
1225 if (kp->menu_count == 0) {
1226 kp->menu_info = NULL;
1227 return 0;
1228 }
1229
1230 if (__get_user(p, &up->menu_info))
1231 return -EFAULT;
1232 umenus = compat_ptr(p);
1233 if (!access_ok(VERIFY_READ, umenus, kp->menu_count * sizeof(*umenus)))
1234 return -EFAULT;
1235
1236 kmenus = compat_alloc_user_space(kp->menu_count * sizeof(*kmenus));
1237 if (kmenus == NULL)
1238 return -EFAULT;
1239 kp->menu_info = kmenus;
1240
1241 if (copy_in_user(kmenus, umenus, kp->menu_count * sizeof(*umenus)))
1242 return -EFAULT;
1243
1244 return 0;
1245}
1246
1247static int uvc_v4l2_put_xu_mapping(const struct uvc_xu_control_mapping *kp,
1248 struct uvc_xu_control_mapping32 __user *up)
1249{
1250 struct uvc_menu_info __user *umenus;
1251 struct uvc_menu_info __user *kmenus = kp->menu_info;
1252 compat_caddr_t p;
1253
1254 if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
1255 __copy_to_user(up, kp, offsetof(typeof(*up), menu_info)) ||
1256 __put_user(kp->menu_count, &up->menu_count))
1257 return -EFAULT;
1258
57fb4a48
HG
1259 if (__clear_user(up->reserved, sizeof(up->reserved)))
1260 return -EFAULT;
1a5e4c86
LP
1261
1262 if (kp->menu_count == 0)
1263 return 0;
1264
1265 if (get_user(p, &up->menu_info))
1266 return -EFAULT;
1267 umenus = compat_ptr(p);
1a5e4c86
LP
1268
1269 if (copy_in_user(umenus, kmenus, kp->menu_count * sizeof(*umenus)))
1270 return -EFAULT;
1271
1272 return 0;
1273}
1274
1275struct uvc_xu_control_query32 {
1276 __u8 unit;
1277 __u8 selector;
1278 __u8 query;
1279 __u16 size;
1280 compat_caddr_t data;
1281};
1282
1283static int uvc_v4l2_get_xu_query(struct uvc_xu_control_query *kp,
1284 const struct uvc_xu_control_query32 __user *up)
1285{
1286 u8 __user *udata;
1287 u8 __user *kdata;
1288 compat_caddr_t p;
1289
1290 if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
1291 __copy_from_user(kp, up, offsetof(typeof(*up), data)))
1292 return -EFAULT;
1293
1294 if (kp->size == 0) {
1295 kp->data = NULL;
1296 return 0;
1297 }
1298
1299 if (__get_user(p, &up->data))
1300 return -EFAULT;
1301 udata = compat_ptr(p);
1302 if (!access_ok(VERIFY_READ, udata, kp->size))
1303 return -EFAULT;
1304
1305 kdata = compat_alloc_user_space(kp->size);
1306 if (kdata == NULL)
1307 return -EFAULT;
1308 kp->data = kdata;
1309
1310 if (copy_in_user(kdata, udata, kp->size))
1311 return -EFAULT;
1312
1313 return 0;
1314}
1315
1316static int uvc_v4l2_put_xu_query(const struct uvc_xu_control_query *kp,
1317 struct uvc_xu_control_query32 __user *up)
1318{
1319 u8 __user *udata;
1320 u8 __user *kdata = kp->data;
1321 compat_caddr_t p;
1322
1323 if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
1324 __copy_to_user(up, kp, offsetof(typeof(*up), data)))
1325 return -EFAULT;
1326
1327 if (kp->size == 0)
1328 return 0;
1329
1330 if (get_user(p, &up->data))
1331 return -EFAULT;
1332 udata = compat_ptr(p);
1333 if (!access_ok(VERIFY_READ, udata, kp->size))
1334 return -EFAULT;
1335
1336 if (copy_in_user(udata, kdata, kp->size))
1337 return -EFAULT;
1338
1339 return 0;
1340}
1341
1342#define UVCIOC_CTRL_MAP32 _IOWR('u', 0x20, struct uvc_xu_control_mapping32)
1343#define UVCIOC_CTRL_QUERY32 _IOWR('u', 0x21, struct uvc_xu_control_query32)
1344
1345static long uvc_v4l2_compat_ioctl32(struct file *file,
1346 unsigned int cmd, unsigned long arg)
1347{
1348 union {
1349 struct uvc_xu_control_mapping xmap;
1350 struct uvc_xu_control_query xqry;
1351 } karg;
1352 void __user *up = compat_ptr(arg);
1353 mm_segment_t old_fs;
1354 long ret;
1355
1356 switch (cmd) {
1357 case UVCIOC_CTRL_MAP32:
1358 cmd = UVCIOC_CTRL_MAP;
1359 ret = uvc_v4l2_get_xu_mapping(&karg.xmap, up);
1360 break;
1361
1362 case UVCIOC_CTRL_QUERY32:
1363 cmd = UVCIOC_CTRL_QUERY;
1364 ret = uvc_v4l2_get_xu_query(&karg.xqry, up);
1365 break;
1366
1367 default:
1368 return -ENOIOCTLCMD;
1369 }
1370
1371 old_fs = get_fs();
1372 set_fs(KERNEL_DS);
d5e90b7a 1373 ret = video_ioctl2(file, cmd, (unsigned long)&karg);
1a5e4c86
LP
1374 set_fs(old_fs);
1375
1376 if (ret < 0)
1377 return ret;
1378
1379 switch (cmd) {
1380 case UVCIOC_CTRL_MAP:
1381 ret = uvc_v4l2_put_xu_mapping(&karg.xmap, up);
1382 break;
1383
1384 case UVCIOC_CTRL_QUERY:
1385 ret = uvc_v4l2_put_xu_query(&karg.xqry, up);
1386 break;
1387 }
1388
1389 return ret;
1390}
1391#endif
1392
c0efd232
LP
1393static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1394 size_t count, loff_t *ppos)
1395{
1396 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
350d6407 1397 return -EINVAL;
c0efd232
LP
1398}
1399
c0efd232
LP
1400static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1401{
abf84383 1402 struct uvc_fh *handle = file->private_data;
35f02a68 1403 struct uvc_streaming *stream = handle->stream;
c0efd232
LP
1404
1405 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
1406
4aa27597 1407 return uvc_queue_mmap(&stream->queue, vma);
c0efd232
LP
1408}
1409
1410static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
1411{
abf84383 1412 struct uvc_fh *handle = file->private_data;
35f02a68 1413 struct uvc_streaming *stream = handle->stream;
c0efd232
LP
1414
1415 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
1416
35f02a68 1417 return uvc_queue_poll(&stream->queue, file, wait);
c0efd232
LP
1418}
1419
72969447
BL
1420#ifndef CONFIG_MMU
1421static unsigned long uvc_v4l2_get_unmapped_area(struct file *file,
1422 unsigned long addr, unsigned long len, unsigned long pgoff,
1423 unsigned long flags)
1424{
1425 struct uvc_fh *handle = file->private_data;
1426 struct uvc_streaming *stream = handle->stream;
1427
1428 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_get_unmapped_area\n");
1429
1430 return uvc_queue_get_unmapped_area(&stream->queue, pgoff);
1431}
1432#endif
1433
d5e90b7a
LP
1434const struct v4l2_ioctl_ops uvc_ioctl_ops = {
1435 .vidioc_querycap = uvc_ioctl_querycap,
1436 .vidioc_enum_fmt_vid_cap = uvc_ioctl_enum_fmt_vid_cap,
1437 .vidioc_enum_fmt_vid_out = uvc_ioctl_enum_fmt_vid_out,
1438 .vidioc_g_fmt_vid_cap = uvc_ioctl_g_fmt_vid_cap,
1439 .vidioc_g_fmt_vid_out = uvc_ioctl_g_fmt_vid_out,
1440 .vidioc_s_fmt_vid_cap = uvc_ioctl_s_fmt_vid_cap,
1441 .vidioc_s_fmt_vid_out = uvc_ioctl_s_fmt_vid_out,
1442 .vidioc_try_fmt_vid_cap = uvc_ioctl_try_fmt_vid_cap,
1443 .vidioc_try_fmt_vid_out = uvc_ioctl_try_fmt_vid_out,
1444 .vidioc_reqbufs = uvc_ioctl_reqbufs,
1445 .vidioc_querybuf = uvc_ioctl_querybuf,
1446 .vidioc_qbuf = uvc_ioctl_qbuf,
1447 .vidioc_dqbuf = uvc_ioctl_dqbuf,
1448 .vidioc_create_bufs = uvc_ioctl_create_bufs,
1449 .vidioc_streamon = uvc_ioctl_streamon,
1450 .vidioc_streamoff = uvc_ioctl_streamoff,
1451 .vidioc_enum_input = uvc_ioctl_enum_input,
1452 .vidioc_g_input = uvc_ioctl_g_input,
1453 .vidioc_s_input = uvc_ioctl_s_input,
1454 .vidioc_queryctrl = uvc_ioctl_queryctrl,
1455 .vidioc_g_ctrl = uvc_ioctl_g_ctrl,
1456 .vidioc_s_ctrl = uvc_ioctl_s_ctrl,
1457 .vidioc_g_ext_ctrls = uvc_ioctl_g_ext_ctrls,
1458 .vidioc_s_ext_ctrls = uvc_ioctl_s_ext_ctrls,
1459 .vidioc_try_ext_ctrls = uvc_ioctl_try_ext_ctrls,
1460 .vidioc_querymenu = uvc_ioctl_querymenu,
1461 .vidioc_cropcap = uvc_ioctl_cropcap,
1462 .vidioc_g_parm = uvc_ioctl_g_parm,
1463 .vidioc_s_parm = uvc_ioctl_s_parm,
1464 .vidioc_enum_framesizes = uvc_ioctl_enum_framesizes,
1465 .vidioc_enum_frameintervals = uvc_ioctl_enum_frameintervals,
1466 .vidioc_subscribe_event = uvc_ioctl_subscribe_event,
1467 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1468 .vidioc_default = uvc_ioctl_default,
1469};
1470
bec43661 1471const struct v4l2_file_operations uvc_fops = {
c0efd232
LP
1472 .owner = THIS_MODULE,
1473 .open = uvc_v4l2_open,
1474 .release = uvc_v4l2_release,
d5e90b7a 1475 .unlocked_ioctl = video_ioctl2,
1a5e4c86
LP
1476#ifdef CONFIG_COMPAT
1477 .compat_ioctl32 = uvc_v4l2_compat_ioctl32,
1478#endif
c0efd232
LP
1479 .read = uvc_v4l2_read,
1480 .mmap = uvc_v4l2_mmap,
1481 .poll = uvc_v4l2_poll,
72969447
BL
1482#ifndef CONFIG_MMU
1483 .get_unmapped_area = uvc_v4l2_get_unmapped_area,
1484#endif
c0efd232 1485};
f87086e3 1486
This page took 0.980443 seconds and 5 git commands to generate.