blob: 69d01803e0aae628dcc0d05f46891101b1025880 [file] [log] [blame]
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001/*
2 * uvc_v4l2.c -- USB Video Class driver - V4L2 API
3 *
Laurent Pinchart11fc5ba2010-09-20 06:10:10 -03004 * Copyright (C) 2005-2010
5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03006 *
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
Laurent Pinchart1a5e4c82011-12-18 20:45:39 -030014#include <linux/compat.h>
Laurent Pinchartc0efd232008-06-30 15:04:50 -030015#include <linux/kernel.h>
Laurent Pinchartc0efd232008-06-30 15:04:50 -030016#include <linux/list.h>
17#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Laurent Pinchartc0efd232008-06-30 15:04:50 -030019#include <linux/usb.h>
20#include <linux/videodev2.h>
21#include <linux/vmalloc.h>
22#include <linux/mm.h>
23#include <linux/wait.h>
Arun Sharma600634972011-07-26 16:09:06 -070024#include <linux/atomic.h>
Laurent Pinchartc0efd232008-06-30 15:04:50 -030025
26#include <media/v4l2-common.h>
Hans de Goedeb4012002012-04-08 12:59:51 -030027#include <media/v4l2-ctrls.h>
28#include <media/v4l2-event.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030029#include <media/v4l2-ioctl.h>
Laurent Pinchartc0efd232008-06-30 15:04:50 -030030
31#include "uvcvideo.h"
32
33/* ------------------------------------------------------------------------
Laurent Pinchart561474c22010-02-18 16:38:52 -030034 * UVC ioctls
35 */
Laurent Pinchartba2fa992010-09-20 05:53:21 -030036static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain,
Laurent Pinchart227bd5b2011-07-30 16:19:49 -030037 struct uvc_xu_control_mapping *xmap)
Laurent Pinchart561474c22010-02-18 16:38:52 -030038{
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:
Haogang Chen806e23e2011-11-29 18:32:25 -030063 /* 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
Laurent Pinchart561474c22010-02-18 16:38:52 -030072 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:
Laurent Pinchartba2fa992010-09-20 05:53:21 -030088 uvc_trace(UVC_TRACE_CONTROL, "Unsupported V4L2 control type "
89 "%u.\n", xmap->v4l2_type);
Mauro Carvalho Chehab7a286cc2011-06-26 10:18:03 -030090 ret = -ENOTTY;
Laurent Pinchart561474c22010-02-18 16:38:52 -030091 goto done;
92 }
93
Laurent Pinchartba2fa992010-09-20 05:53:21 -030094 ret = uvc_ctrl_add_mapping(chain, map);
Laurent Pinchart561474c22010-02-18 16:38:52 -030095
96done:
Laurent Pinchartba2fa992010-09-20 05:53:21 -030097 kfree(map->menu_info);
98 kfree(map);
Laurent Pinchart561474c22010-02-18 16:38:52 -030099
100 return ret;
101}
102
103/* ------------------------------------------------------------------------
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300104 * V4L2 interface
105 */
106
107/*
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300108 * 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
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300145static int uvc_v4l2_try_format(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300146 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
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300158 if (fmt->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300159 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
Laurent Pinchart815adc42012-08-28 18:38:58 -0300167 /* Check if the hardware supports the requested format, use the default
168 * format otherwise.
169 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300170 for (i = 0; i < stream->nformats; ++i) {
171 format = &stream->format[i];
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300172 if (format->fcc == fmt->fmt.pix.pixelformat)
173 break;
174 }
175
Laurent Pinchart815adc42012-08-28 18:38:58 -0300176 if (i == stream->nformats) {
177 format = stream->def_format;
178 fmt->fmt.pix.pixelformat = format->fcc;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300179 }
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 */
Laurent Pinchart69477562010-11-21 13:36:34 -0300234 mutex_lock(&stream->mutex);
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300235 if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300236 probe->dwMaxVideoFrameSize =
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300237 stream->ctrl.dwMaxVideoFrameSize;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300238
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300239 /* Probe the device. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300240 ret = uvc_probe_video(stream, probe);
Laurent Pinchart69477562010-11-21 13:36:34 -0300241 mutex_unlock(&stream->mutex);
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300242 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300243 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
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300262static int uvc_v4l2_get_format(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300263 struct v4l2_format *fmt)
264{
Laurent Pinchart69477562010-11-21 13:36:34 -0300265 struct uvc_format *format;
266 struct uvc_frame *frame;
267 int ret = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300268
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300269 if (fmt->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300270 return -EINVAL;
271
Laurent Pinchart69477562010-11-21 13:36:34 -0300272 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 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300280
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;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300286 fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300287 fmt->fmt.pix.colorspace = format->colorspace;
288 fmt->fmt.pix.priv = 0;
289
Laurent Pinchart69477562010-11-21 13:36:34 -0300290done:
291 mutex_unlock(&stream->mutex);
292 return ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300293}
294
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300295static int uvc_v4l2_set_format(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300296 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
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300303 if (fmt->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300304 return -EINVAL;
305
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300306 ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300307 if (ret < 0)
308 return ret;
309
Laurent Pinchart69477562010-11-21 13:36:34 -0300310 mutex_lock(&stream->mutex);
311
312 if (uvc_queue_allocated(&stream->queue)) {
313 ret = -EBUSY;
314 goto done;
315 }
316
Ezequiel Garcia8c0d44e2013-01-14 15:22:55 -0300317 stream->ctrl = probe;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300318 stream->cur_format = format;
319 stream->cur_frame = frame;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300320
Laurent Pinchart69477562010-11-21 13:36:34 -0300321done:
322 mutex_unlock(&stream->mutex);
323 return ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300324}
325
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300326static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300327 struct v4l2_streamparm *parm)
328{
329 uint32_t numerator, denominator;
330
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300331 if (parm->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300332 return -EINVAL;
333
Laurent Pinchart69477562010-11-21 13:36:34 -0300334 mutex_lock(&stream->mutex);
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300335 numerator = stream->ctrl.dwFrameInterval;
Laurent Pinchart69477562010-11-21 13:36:34 -0300336 mutex_unlock(&stream->mutex);
337
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300338 denominator = 10000000;
339 uvc_simplify_fraction(&numerator, &denominator, 8, 333);
340
341 memset(parm, 0, sizeof *parm);
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300342 parm->type = stream->type;
Laurent Pinchartff924202008-12-28 22:32:29 -0300343
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300344 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Laurent Pinchartff924202008-12-28 22:32:29 -0300345 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 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300357
358 return 0;
359}
360
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300361static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300362 struct v4l2_streamparm *parm)
363{
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300364 struct uvc_streaming_control probe;
Laurent Pinchartff924202008-12-28 22:32:29 -0300365 struct v4l2_fract timeperframe;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300366 uint32_t interval;
367 int ret;
368
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300369 if (parm->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300370 return -EINVAL;
371
Laurent Pinchartff924202008-12-28 22:32:29 -0300372 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
373 timeperframe = parm->parm.capture.timeperframe;
374 else
375 timeperframe = parm->parm.output.timeperframe;
376
Laurent Pinchartff924202008-12-28 22:32:29 -0300377 interval = uvc_fraction_to_interval(timeperframe.numerator,
378 timeperframe.denominator);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300379 uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
Laurent Pinchartff924202008-12-28 22:32:29 -0300380 timeperframe.numerator, timeperframe.denominator, interval);
Laurent Pinchart69477562010-11-21 13:36:34 -0300381
382 mutex_lock(&stream->mutex);
383
384 if (uvc_queue_streaming(&stream->queue)) {
385 mutex_unlock(&stream->mutex);
386 return -EBUSY;
387 }
388
Ezequiel Garcia8c0d44e2013-01-14 15:22:55 -0300389 probe = stream->ctrl;
Laurent Pinchart69477562010-11-21 13:36:34 -0300390 probe.dwFrameInterval =
391 uvc_try_frame_interval(stream->cur_frame, interval);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300392
393 /* Probe the device with the new settings. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300394 ret = uvc_probe_video(stream, &probe);
Laurent Pinchart69477562010-11-21 13:36:34 -0300395 if (ret < 0) {
396 mutex_unlock(&stream->mutex);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300397 return ret;
Laurent Pinchart69477562010-11-21 13:36:34 -0300398 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300399
Ezequiel Garcia8c0d44e2013-01-14 15:22:55 -0300400 stream->ctrl = probe;
Laurent Pinchart69477562010-11-21 13:36:34 -0300401 mutex_unlock(&stream->mutex);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300402
403 /* Return the actual frame period. */
Laurent Pinchartff924202008-12-28 22:32:29 -0300404 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;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300413
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
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300432 * a given time. Trying to perform an operation that requires privileges will
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300433 * automatically acquire the required privileges if possible, or return -EBUSY
Laurent Pinchart1a969d92009-09-02 03:12:26 -0300434 * otherwise. Privileges are dismissed when closing the instance or when
435 * freeing the video buffers using VIDIOC_REQBUFS.
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300436 *
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300437 * Operations that require privileges are:
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300438 *
439 * - VIDIOC_S_INPUT
440 * - VIDIOC_S_PARM
441 * - VIDIOC_S_FMT
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300442 * - VIDIOC_REQBUFS
443 */
444static int uvc_acquire_privileges(struct uvc_fh *handle)
445{
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300446 /* 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. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300451 if (atomic_inc_return(&handle->stream->active) != 1) {
452 atomic_dec(&handle->stream->active);
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300453 return -EBUSY;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300454 }
455
456 handle->state = UVC_HANDLE_ACTIVE;
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300457 return 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300458}
459
460static void uvc_dismiss_privileges(struct uvc_fh *handle)
461{
462 if (handle->state == UVC_HANDLE_ACTIVE)
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300463 atomic_dec(&handle->stream->active);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300464
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
Hans Verkuilbec43662008-12-30 06:58:20 -0300477static int uvc_v4l2_open(struct file *file)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300478{
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300479 struct uvc_streaming *stream;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300480 struct uvc_fh *handle;
481 int ret = 0;
482
483 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300484 stream = video_drvdata(file);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300485
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300486 if (stream->dev->state & UVC_DEV_DISCONNECTED)
487 return -ENODEV;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300488
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300489 ret = usb_autopm_get_interface(stream->dev->intf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300490 if (ret < 0)
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300491 return ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300492
493 /* Create the device handle. */
494 handle = kzalloc(sizeof *handle, GFP_KERNEL);
495 if (handle == NULL) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300496 usb_autopm_put_interface(stream->dev->intf);
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300497 return -ENOMEM;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300498 }
499
Laurent Pinchart17706f52013-04-25 22:28:51 -0300500 mutex_lock(&stream->dev->lock);
501 if (stream->dev->users == 0) {
502 ret = uvc_status_start(stream->dev, GFP_KERNEL);
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300503 if (ret < 0) {
Laurent Pinchart17706f52013-04-25 22:28:51 -0300504 mutex_unlock(&stream->dev->lock);
Oliver Neukuma82a45f2013-01-10 07:04:55 -0300505 usb_autopm_put_interface(stream->dev->intf);
Laurent Pinchart04a37e02009-05-19 10:08:03 -0300506 kfree(handle);
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300507 return ret;
Laurent Pinchart04a37e02009-05-19 10:08:03 -0300508 }
509 }
510
Laurent Pinchart17706f52013-04-25 22:28:51 -0300511 stream->dev->users++;
512 mutex_unlock(&stream->dev->lock);
513
Hans Verkuild8da7512015-03-09 13:34:11 -0300514 v4l2_fh_init(&handle->vfh, &stream->vdev);
Hans de Goedeb4012002012-04-08 12:59:51 -0300515 v4l2_fh_add(&handle->vfh);
Laurent Pinchart8e113592009-07-01 20:24:47 -0300516 handle->chain = stream->chain;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300517 handle->stream = stream;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300518 handle->state = UVC_HANDLE_PASSIVE;
519 file->private_data = handle;
520
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300521 return 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300522}
523
Hans Verkuilbec43662008-12-30 06:58:20 -0300524static int uvc_v4l2_release(struct file *file)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300525{
Joe Perchesabf84382010-07-12 17:50:03 -0300526 struct uvc_fh *handle = file->private_data;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300527 struct uvc_streaming *stream = handle->stream;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300528
529 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");
530
531 /* Only free resources if this is a privileged handle. */
Laurent Pinchart3f02de22014-10-21 16:07:15 -0300532 if (uvc_has_privileges(handle))
533 uvc_queue_release(&stream->queue);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300534
535 /* Release the file handle. */
536 uvc_dismiss_privileges(handle);
Hans de Goedeb4012002012-04-08 12:59:51 -0300537 v4l2_fh_del(&handle->vfh);
538 v4l2_fh_exit(&handle->vfh);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300539 kfree(handle);
540 file->private_data = NULL;
541
Laurent Pinchart17706f52013-04-25 22:28:51 -0300542 mutex_lock(&stream->dev->lock);
543 if (--stream->dev->users == 0)
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300544 uvc_status_stop(stream->dev);
Laurent Pinchart17706f52013-04-25 22:28:51 -0300545 mutex_unlock(&stream->dev->lock);
Laurent Pinchart04a37e02009-05-19 10:08:03 -0300546
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300547 usb_autopm_put_interface(stream->dev->intf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300548 return 0;
549}
550
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300551static int uvc_ioctl_querycap(struct file *file, void *fh,
552 struct v4l2_capability *cap)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300553{
554 struct video_device *vdev = video_devdata(file);
Joe Perchesabf84382010-07-12 17:50:03 -0300555 struct uvc_fh *handle = file->private_data;
Laurent Pinchart8e113592009-07-01 20:24:47 -0300556 struct uvc_video_chain *chain = handle->chain;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300557 struct uvc_streaming *stream = handle->stream;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300558
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300559 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;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300568
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300569 return 0;
570}
571
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;
578
579 if (fmt->type != stream->type || fmt->index >= stream->nformats)
580 return -EINVAL;
581
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}
595
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;
601
602 return uvc_ioctl_enum_fmt(stream, fmt);
603}
604
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;
610
611 return uvc_ioctl_enum_fmt(stream, fmt);
612}
613
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;
619
620 return uvc_v4l2_get_format(stream, fmt);
621}
622
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;
628
629 return uvc_v4l2_get_format(stream, fmt);
630}
631
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;
638
639 ret = uvc_acquire_privileges(handle);
640 if (ret < 0)
641 return ret;
642
643 return uvc_v4l2_set_format(stream, fmt);
644}
645
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;
652
653 ret = uvc_acquire_privileges(handle);
654 if (ret < 0)
655 return ret;
656
657 return uvc_v4l2_set_format(stream, fmt);
658}
659
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;
666
667 return uvc_v4l2_try_format(stream, fmt, &probe, NULL, NULL);
668}
669
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;
676
677 return uvc_v4l2_try_format(stream, fmt, &probe, NULL, NULL);
678}
679
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;
686
687 ret = uvc_acquire_privileges(handle);
688 if (ret < 0)
689 return ret;
690
691 mutex_lock(&stream->mutex);
Laurent Pinchart1b7f9c92014-10-21 16:02:00 -0300692 ret = uvc_request_buffers(&stream->queue, rb);
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300693 mutex_unlock(&stream->mutex);
694 if (ret < 0)
695 return ret;
696
697 if (ret == 0)
698 uvc_dismiss_privileges(handle);
699
700 return 0;
701}
702
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;
708
709 if (!uvc_has_privileges(handle))
710 return -EBUSY;
711
712 return uvc_query_buffer(&stream->queue, buf);
713}
714
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;
719
720 if (!uvc_has_privileges(handle))
721 return -EBUSY;
722
723 return uvc_queue_buffer(&stream->queue, buf);
724}
725
Laurent Pinchart7195f612015-04-14 04:19:51 -0300726static int uvc_ioctl_expbuf(struct file *file, void *fh,
727 struct v4l2_exportbuffer *exp)
728{
729 struct uvc_fh *handle = fh;
730 struct uvc_streaming *stream = handle->stream;
731
732 if (!uvc_has_privileges(handle))
733 return -EBUSY;
734
735 return uvc_export_buffer(&stream->queue, exp);
736}
737
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300738static int uvc_ioctl_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
739{
740 struct uvc_fh *handle = fh;
741 struct uvc_streaming *stream = handle->stream;
742
743 if (!uvc_has_privileges(handle))
744 return -EBUSY;
745
746 return uvc_dequeue_buffer(&stream->queue, buf,
747 file->f_flags & O_NONBLOCK);
748}
749
750static int uvc_ioctl_create_bufs(struct file *file, void *fh,
751 struct v4l2_create_buffers *cb)
752{
753 struct uvc_fh *handle = fh;
754 struct uvc_streaming *stream = handle->stream;
755 int ret;
756
757 ret = uvc_acquire_privileges(handle);
758 if (ret < 0)
759 return ret;
760
761 return uvc_create_buffers(&stream->queue, cb);
762}
763
764static int uvc_ioctl_streamon(struct file *file, void *fh,
765 enum v4l2_buf_type type)
766{
767 struct uvc_fh *handle = fh;
768 struct uvc_streaming *stream = handle->stream;
769 int ret;
770
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300771 if (!uvc_has_privileges(handle))
772 return -EBUSY;
773
774 mutex_lock(&stream->mutex);
Laurent Pinchart0da4ab92014-10-21 16:19:04 -0300775 ret = uvc_queue_streamon(&stream->queue, type);
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300776 mutex_unlock(&stream->mutex);
777
778 return ret;
779}
780
781static int uvc_ioctl_streamoff(struct file *file, void *fh,
782 enum v4l2_buf_type type)
783{
784 struct uvc_fh *handle = fh;
785 struct uvc_streaming *stream = handle->stream;
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300786
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300787 if (!uvc_has_privileges(handle))
788 return -EBUSY;
789
790 mutex_lock(&stream->mutex);
Laurent Pinchart0da4ab92014-10-21 16:19:04 -0300791 uvc_queue_streamoff(&stream->queue, type);
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300792 mutex_unlock(&stream->mutex);
793
Laurent Pinchartb83bba22014-10-13 10:11:35 -0300794 return 0;
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300795}
796
797static int uvc_ioctl_enum_input(struct file *file, void *fh,
798 struct v4l2_input *input)
799{
800 struct uvc_fh *handle = fh;
801 struct uvc_video_chain *chain = handle->chain;
802 const struct uvc_entity *selector = chain->selector;
803 struct uvc_entity *iterm = NULL;
804 u32 index = input->index;
805 int pin = 0;
806
807 if (selector == NULL ||
808 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
809 if (index != 0)
810 return -EINVAL;
811 list_for_each_entry(iterm, &chain->entities, chain) {
812 if (UVC_ENTITY_IS_ITERM(iterm))
813 break;
814 }
815 pin = iterm->id;
816 } else if (index < selector->bNrInPins) {
817 pin = selector->baSourceID[index];
818 list_for_each_entry(iterm, &chain->entities, chain) {
819 if (!UVC_ENTITY_IS_ITERM(iterm))
820 continue;
821 if (iterm->id == pin)
822 break;
823 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300824 }
825
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300826 if (iterm == NULL || iterm->id != pin)
827 return -EINVAL;
Laurent Pinchart05505132012-08-28 20:29:56 -0300828
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300829 memset(input, 0, sizeof(*input));
830 input->index = index;
831 strlcpy(input->name, iterm->name, sizeof(input->name));
832 if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
833 input->type = V4L2_INPUT_TYPE_CAMERA;
Laurent Pinchart05505132012-08-28 20:29:56 -0300834
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300835 return 0;
836}
Laurent Pinchart05505132012-08-28 20:29:56 -0300837
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300838static int uvc_ioctl_g_input(struct file *file, void *fh, unsigned int *input)
839{
840 struct uvc_fh *handle = fh;
841 struct uvc_video_chain *chain = handle->chain;
842 int ret;
843 u8 i;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300844
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300845 if (chain->selector == NULL ||
846 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
847 *input = 0;
848 return 0;
849 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300850
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300851 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, chain->selector->id,
852 chain->dev->intfnum, UVC_SU_INPUT_SELECT_CONTROL,
853 &i, 1);
854 if (ret < 0)
855 return ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300856
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300857 *input = i - 1;
858 return 0;
859}
Robert Krakora1fcbcc42009-06-12 13:51:03 -0300860
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300861static int uvc_ioctl_s_input(struct file *file, void *fh, unsigned int input)
862{
863 struct uvc_fh *handle = fh;
864 struct uvc_video_chain *chain = handle->chain;
865 int ret;
866 u32 i;
867
868 ret = uvc_acquire_privileges(handle);
869 if (ret < 0)
870 return ret;
871
872 if (chain->selector == NULL ||
873 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
874 if (input)
875 return -EINVAL;
876 return 0;
877 }
878
879 if (input >= chain->selector->bNrInPins)
880 return -EINVAL;
881
882 i = input + 1;
883 return uvc_query_ctrl(chain->dev, UVC_SET_CUR, chain->selector->id,
884 chain->dev->intfnum, UVC_SU_INPUT_SELECT_CONTROL,
885 &i, 1);
886}
887
888static int uvc_ioctl_queryctrl(struct file *file, void *fh,
889 struct v4l2_queryctrl *qc)
890{
891 struct uvc_fh *handle = fh;
892 struct uvc_video_chain *chain = handle->chain;
893
894 return uvc_query_v4l2_ctrl(chain, qc);
895}
896
Hans Verkuile1832012015-03-14 14:04:00 -0300897static int uvc_ioctl_query_ext_ctrl(struct file *file, void *fh,
898 struct v4l2_query_ext_ctrl *qec)
899{
900 struct uvc_fh *handle = fh;
901 struct uvc_video_chain *chain = handle->chain;
902 struct v4l2_queryctrl qc = { qec->id };
903 int ret;
904
905 ret = uvc_query_v4l2_ctrl(chain, &qc);
906 if (ret)
907 return ret;
908
909 qec->id = qc.id;
910 qec->type = qc.type;
911 strlcpy(qec->name, qc.name, sizeof(qec->name));
912 qec->minimum = qc.minimum;
913 qec->maximum = qc.maximum;
914 qec->step = qc.step;
915 qec->default_value = qc.default_value;
916 qec->flags = qc.flags;
917 qec->elem_size = 4;
918 qec->elems = 1;
919 qec->nr_of_dims = 0;
920 memset(qec->dims, 0, sizeof(qec->dims));
921 memset(qec->reserved, 0, sizeof(qec->reserved));
922
923 return 0;
924}
925
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300926static int uvc_ioctl_g_ctrl(struct file *file, void *fh,
927 struct v4l2_control *ctrl)
928{
929 struct uvc_fh *handle = fh;
930 struct uvc_video_chain *chain = handle->chain;
931 struct v4l2_ext_control xctrl;
932 int ret;
933
934 memset(&xctrl, 0, sizeof(xctrl));
935 xctrl.id = ctrl->id;
936
937 ret = uvc_ctrl_begin(chain);
938 if (ret < 0)
939 return ret;
940
941 ret = uvc_ctrl_get(chain, &xctrl);
942 uvc_ctrl_rollback(handle);
943 if (ret < 0)
944 return ret;
945
946 ctrl->value = xctrl.value;
947 return 0;
948}
949
950static int uvc_ioctl_s_ctrl(struct file *file, void *fh,
951 struct v4l2_control *ctrl)
952{
953 struct uvc_fh *handle = fh;
954 struct uvc_video_chain *chain = handle->chain;
955 struct v4l2_ext_control xctrl;
956 int ret;
957
958 memset(&xctrl, 0, sizeof(xctrl));
959 xctrl.id = ctrl->id;
960 xctrl.value = ctrl->value;
961
962 ret = uvc_ctrl_begin(chain);
963 if (ret < 0)
964 return ret;
965
966 ret = uvc_ctrl_set(chain, &xctrl);
967 if (ret < 0) {
Hans de Goedeb4012002012-04-08 12:59:51 -0300968 uvc_ctrl_rollback(handle);
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300969 return ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300970 }
971
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300972 ret = uvc_ctrl_commit(handle, &xctrl, 1);
973 if (ret < 0)
974 return ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300975
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300976 ctrl->value = xctrl.value;
977 return 0;
978}
Laurent Pinchart05505132012-08-28 20:29:56 -0300979
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300980static int uvc_ioctl_g_ext_ctrls(struct file *file, void *fh,
981 struct v4l2_ext_controls *ctrls)
982{
983 struct uvc_fh *handle = fh;
984 struct uvc_video_chain *chain = handle->chain;
985 struct v4l2_ext_control *ctrl = ctrls->controls;
986 unsigned int i;
987 int ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300988
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300989 ret = uvc_ctrl_begin(chain);
990 if (ret < 0)
991 return ret;
Robert Krakora1fcbcc42009-06-12 13:51:03 -0300992
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300993 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
994 ret = uvc_ctrl_get(chain, ctrl);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300995 if (ret < 0) {
Hans de Goedeb4012002012-04-08 12:59:51 -0300996 uvc_ctrl_rollback(handle);
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300997 ctrls->error_idx = i;
Rafael J. Wysocki9c016d62012-12-23 14:39:32 +0100998 return ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300999 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001000 }
1001
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001002 ctrls->error_idx = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001003
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001004 return uvc_ctrl_rollback(handle);
1005}
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001006
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001007static int uvc_ioctl_s_try_ext_ctrls(struct uvc_fh *handle,
1008 struct v4l2_ext_controls *ctrls,
1009 bool commit)
1010{
1011 struct v4l2_ext_control *ctrl = ctrls->controls;
1012 struct uvc_video_chain *chain = handle->chain;
1013 unsigned int i;
1014 int ret;
1015
1016 ret = uvc_ctrl_begin(chain);
1017 if (ret < 0)
1018 return ret;
1019
1020 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
1021 ret = uvc_ctrl_set(chain, ctrl);
1022 if (ret < 0) {
1023 uvc_ctrl_rollback(handle);
1024 ctrls->error_idx = commit ? ctrls->count : i;
Robert Krakora1fcbcc42009-06-12 13:51:03 -03001025 return ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001026 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001027 }
1028
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001029 ctrls->error_idx = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001030
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001031 if (commit)
1032 return uvc_ctrl_commit(handle, ctrls->controls, ctrls->count);
1033 else
1034 return uvc_ctrl_rollback(handle);
1035}
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001036
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001037static int uvc_ioctl_s_ext_ctrls(struct file *file, void *fh,
1038 struct v4l2_ext_controls *ctrls)
1039{
1040 struct uvc_fh *handle = fh;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001041
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001042 return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, true);
1043}
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001044
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001045static int uvc_ioctl_try_ext_ctrls(struct file *file, void *fh,
1046 struct v4l2_ext_controls *ctrls)
1047{
1048 struct uvc_fh *handle = fh;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001049
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001050 return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, false);
1051}
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001052
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001053static int uvc_ioctl_querymenu(struct file *file, void *fh,
1054 struct v4l2_querymenu *qm)
1055{
1056 struct uvc_fh *handle = fh;
1057 struct uvc_video_chain *chain = handle->chain;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001058
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001059 return uvc_query_v4l2_menu(chain, qm);
1060}
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001061
Hans Verkuil1461fe72015-04-03 06:53:42 -03001062static int uvc_ioctl_g_selection(struct file *file, void *fh,
1063 struct v4l2_selection *sel)
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001064{
1065 struct uvc_fh *handle = fh;
1066 struct uvc_streaming *stream = handle->stream;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001067
Hans Verkuil1461fe72015-04-03 06:53:42 -03001068 if (sel->type != stream->type)
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001069 return -EINVAL;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001070
Hans Verkuil1461fe72015-04-03 06:53:42 -03001071 switch (sel->target) {
1072 case V4L2_SEL_TGT_CROP_DEFAULT:
1073 case V4L2_SEL_TGT_CROP_BOUNDS:
1074 if (stream->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1075 return -EINVAL;
1076 break;
1077 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1078 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1079 if (stream->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1080 return -EINVAL;
1081 break;
1082 default:
1083 return -EINVAL;
1084 }
1085
1086 sel->r.left = 0;
1087 sel->r.top = 0;
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001088 mutex_lock(&stream->mutex);
Hans Verkuil1461fe72015-04-03 06:53:42 -03001089 sel->r.width = stream->cur_frame->wWidth;
1090 sel->r.height = stream->cur_frame->wHeight;
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001091 mutex_unlock(&stream->mutex);
1092
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001093 return 0;
1094}
1095
1096static int uvc_ioctl_g_parm(struct file *file, void *fh,
1097 struct v4l2_streamparm *parm)
1098{
1099 struct uvc_fh *handle = fh;
1100 struct uvc_streaming *stream = handle->stream;
1101
1102 return uvc_v4l2_get_streamparm(stream, parm);
1103}
1104
1105static int uvc_ioctl_s_parm(struct file *file, void *fh,
1106 struct v4l2_streamparm *parm)
1107{
1108 struct uvc_fh *handle = fh;
1109 struct uvc_streaming *stream = handle->stream;
1110 int ret;
1111
1112 ret = uvc_acquire_privileges(handle);
1113 if (ret < 0)
1114 return ret;
1115
1116 return uvc_v4l2_set_streamparm(stream, parm);
1117}
1118
1119static int uvc_ioctl_enum_framesizes(struct file *file, void *fh,
1120 struct v4l2_frmsizeenum *fsize)
1121{
1122 struct uvc_fh *handle = fh;
1123 struct uvc_streaming *stream = handle->stream;
1124 struct uvc_format *format = NULL;
1125 struct uvc_frame *frame;
1126 int i;
1127
1128 /* Look for the given pixel format */
1129 for (i = 0; i < stream->nformats; i++) {
1130 if (stream->format[i].fcc == fsize->pixel_format) {
1131 format = &stream->format[i];
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001132 break;
1133 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001134 }
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001135 if (format == NULL)
1136 return -EINVAL;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001137
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001138 if (fsize->index >= format->nframes)
1139 return -EINVAL;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001140
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001141 frame = &format->frame[fsize->index];
1142 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1143 fsize->discrete.width = frame->wWidth;
1144 fsize->discrete.height = frame->wHeight;
1145 return 0;
1146}
Laurent Pinchart05505132012-08-28 20:29:56 -03001147
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001148static int uvc_ioctl_enum_frameintervals(struct file *file, void *fh,
1149 struct v4l2_frmivalenum *fival)
1150{
1151 struct uvc_fh *handle = fh;
1152 struct uvc_streaming *stream = handle->stream;
1153 struct uvc_format *format = NULL;
1154 struct uvc_frame *frame = NULL;
1155 int i;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001156
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001157 /* Look for the given pixel format and frame size */
1158 for (i = 0; i < stream->nformats; i++) {
1159 if (stream->format[i].fcc == fival->pixel_format) {
1160 format = &stream->format[i];
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001161 break;
1162 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001163 }
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001164 if (format == NULL)
1165 return -EINVAL;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001166
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001167 for (i = 0; i < format->nframes; i++) {
1168 if (format->frame[i].wWidth == fival->width &&
1169 format->frame[i].wHeight == fival->height) {
1170 frame = &format->frame[i];
1171 break;
Hans de Goedeb4012002012-04-08 12:59:51 -03001172 }
1173 }
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001174 if (frame == NULL)
1175 return -EINVAL;
Hans de Goedeb4012002012-04-08 12:59:51 -03001176
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001177 if (frame->bFrameIntervalType) {
1178 if (fival->index >= frame->bFrameIntervalType)
1179 return -EINVAL;
Hans de Goedeb4012002012-04-08 12:59:51 -03001180
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001181 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1182 fival->discrete.numerator =
1183 frame->dwFrameInterval[fival->index];
1184 fival->discrete.denominator = 10000000;
1185 uvc_simplify_fraction(&fival->discrete.numerator,
1186 &fival->discrete.denominator, 8, 333);
1187 } else {
Laurent Pinchart1cdf60c2015-03-06 09:54:47 -03001188 if (fival->index)
1189 return -EINVAL;
1190
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001191 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
1192 fival->stepwise.min.numerator = frame->dwFrameInterval[0];
1193 fival->stepwise.min.denominator = 10000000;
1194 fival->stepwise.max.numerator = frame->dwFrameInterval[1];
1195 fival->stepwise.max.denominator = 10000000;
1196 fival->stepwise.step.numerator = frame->dwFrameInterval[2];
1197 fival->stepwise.step.denominator = 10000000;
1198 uvc_simplify_fraction(&fival->stepwise.min.numerator,
1199 &fival->stepwise.min.denominator, 8, 333);
1200 uvc_simplify_fraction(&fival->stepwise.max.numerator,
1201 &fival->stepwise.max.denominator, 8, 333);
1202 uvc_simplify_fraction(&fival->stepwise.step.numerator,
1203 &fival->stepwise.step.denominator, 8, 333);
1204 }
Hans de Goedeb4012002012-04-08 12:59:51 -03001205
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001206 return 0;
1207}
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001208
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001209static int uvc_ioctl_subscribe_event(struct v4l2_fh *fh,
1210 const struct v4l2_event_subscription *sub)
1211{
1212 switch (sub->type) {
1213 case V4L2_EVENT_CTRL:
1214 return v4l2_event_subscribe(fh, sub, 0, &uvc_ctrl_sub_ev_ops);
1215 default:
1216 return -EINVAL;
1217 }
1218}
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001219
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001220static long uvc_ioctl_default(struct file *file, void *fh, bool valid_prio,
1221 unsigned int cmd, void *arg)
1222{
1223 struct uvc_fh *handle = fh;
1224 struct uvc_video_chain *chain = handle->chain;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001225
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001226 switch (cmd) {
1227 /* Dynamic controls. */
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001228 case UVCIOC_CTRL_MAP:
Laurent Pinchart227bd5b2011-07-30 16:19:49 -03001229 return uvc_ioctl_ctrl_map(chain, arg);
Martin Rublife78d182010-10-02 19:10:16 -03001230
1231 case UVCIOC_CTRL_QUERY:
1232 return uvc_xu_ctrl_query(chain, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001233
1234 default:
Laurent Pincharta3ec69b2011-12-18 20:22:50 -03001235 return -ENOTTY;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001236 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001237}
1238
Laurent Pinchart1a5e4c82011-12-18 20:45:39 -03001239#ifdef CONFIG_COMPAT
1240struct uvc_xu_control_mapping32 {
1241 __u32 id;
1242 __u8 name[32];
1243 __u8 entity[16];
1244 __u8 selector;
1245
1246 __u8 size;
1247 __u8 offset;
1248 __u32 v4l2_type;
1249 __u32 data_type;
1250
1251 compat_caddr_t menu_info;
1252 __u32 menu_count;
1253
1254 __u32 reserved[4];
1255};
1256
1257static int uvc_v4l2_get_xu_mapping(struct uvc_xu_control_mapping *kp,
1258 const struct uvc_xu_control_mapping32 __user *up)
1259{
1260 struct uvc_menu_info __user *umenus;
1261 struct uvc_menu_info __user *kmenus;
1262 compat_caddr_t p;
1263
1264 if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
1265 __copy_from_user(kp, up, offsetof(typeof(*up), menu_info)) ||
1266 __get_user(kp->menu_count, &up->menu_count))
1267 return -EFAULT;
1268
1269 memset(kp->reserved, 0, sizeof(kp->reserved));
1270
1271 if (kp->menu_count == 0) {
1272 kp->menu_info = NULL;
1273 return 0;
1274 }
1275
1276 if (__get_user(p, &up->menu_info))
1277 return -EFAULT;
1278 umenus = compat_ptr(p);
1279 if (!access_ok(VERIFY_READ, umenus, kp->menu_count * sizeof(*umenus)))
1280 return -EFAULT;
1281
1282 kmenus = compat_alloc_user_space(kp->menu_count * sizeof(*kmenus));
1283 if (kmenus == NULL)
1284 return -EFAULT;
1285 kp->menu_info = kmenus;
1286
1287 if (copy_in_user(kmenus, umenus, kp->menu_count * sizeof(*umenus)))
1288 return -EFAULT;
1289
1290 return 0;
1291}
1292
1293static int uvc_v4l2_put_xu_mapping(const struct uvc_xu_control_mapping *kp,
1294 struct uvc_xu_control_mapping32 __user *up)
1295{
1296 struct uvc_menu_info __user *umenus;
1297 struct uvc_menu_info __user *kmenus = kp->menu_info;
1298 compat_caddr_t p;
1299
1300 if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
1301 __copy_to_user(up, kp, offsetof(typeof(*up), menu_info)) ||
1302 __put_user(kp->menu_count, &up->menu_count))
1303 return -EFAULT;
1304
Hans de Goede57fb4a42012-04-08 12:59:48 -03001305 if (__clear_user(up->reserved, sizeof(up->reserved)))
1306 return -EFAULT;
Laurent Pinchart1a5e4c82011-12-18 20:45:39 -03001307
1308 if (kp->menu_count == 0)
1309 return 0;
1310
1311 if (get_user(p, &up->menu_info))
1312 return -EFAULT;
1313 umenus = compat_ptr(p);
Laurent Pinchart1a5e4c82011-12-18 20:45:39 -03001314
1315 if (copy_in_user(umenus, kmenus, kp->menu_count * sizeof(*umenus)))
1316 return -EFAULT;
1317
1318 return 0;
1319}
1320
1321struct uvc_xu_control_query32 {
1322 __u8 unit;
1323 __u8 selector;
1324 __u8 query;
1325 __u16 size;
1326 compat_caddr_t data;
1327};
1328
1329static int uvc_v4l2_get_xu_query(struct uvc_xu_control_query *kp,
1330 const struct uvc_xu_control_query32 __user *up)
1331{
1332 u8 __user *udata;
1333 u8 __user *kdata;
1334 compat_caddr_t p;
1335
1336 if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
1337 __copy_from_user(kp, up, offsetof(typeof(*up), data)))
1338 return -EFAULT;
1339
1340 if (kp->size == 0) {
1341 kp->data = NULL;
1342 return 0;
1343 }
1344
1345 if (__get_user(p, &up->data))
1346 return -EFAULT;
1347 udata = compat_ptr(p);
1348 if (!access_ok(VERIFY_READ, udata, kp->size))
1349 return -EFAULT;
1350
1351 kdata = compat_alloc_user_space(kp->size);
1352 if (kdata == NULL)
1353 return -EFAULT;
1354 kp->data = kdata;
1355
1356 if (copy_in_user(kdata, udata, kp->size))
1357 return -EFAULT;
1358
1359 return 0;
1360}
1361
1362static int uvc_v4l2_put_xu_query(const struct uvc_xu_control_query *kp,
1363 struct uvc_xu_control_query32 __user *up)
1364{
1365 u8 __user *udata;
1366 u8 __user *kdata = kp->data;
1367 compat_caddr_t p;
1368
1369 if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
1370 __copy_to_user(up, kp, offsetof(typeof(*up), data)))
1371 return -EFAULT;
1372
1373 if (kp->size == 0)
1374 return 0;
1375
1376 if (get_user(p, &up->data))
1377 return -EFAULT;
1378 udata = compat_ptr(p);
1379 if (!access_ok(VERIFY_READ, udata, kp->size))
1380 return -EFAULT;
1381
1382 if (copy_in_user(udata, kdata, kp->size))
1383 return -EFAULT;
1384
1385 return 0;
1386}
1387
1388#define UVCIOC_CTRL_MAP32 _IOWR('u', 0x20, struct uvc_xu_control_mapping32)
1389#define UVCIOC_CTRL_QUERY32 _IOWR('u', 0x21, struct uvc_xu_control_query32)
1390
1391static long uvc_v4l2_compat_ioctl32(struct file *file,
1392 unsigned int cmd, unsigned long arg)
1393{
1394 union {
1395 struct uvc_xu_control_mapping xmap;
1396 struct uvc_xu_control_query xqry;
1397 } karg;
1398 void __user *up = compat_ptr(arg);
1399 mm_segment_t old_fs;
1400 long ret;
1401
1402 switch (cmd) {
1403 case UVCIOC_CTRL_MAP32:
1404 cmd = UVCIOC_CTRL_MAP;
1405 ret = uvc_v4l2_get_xu_mapping(&karg.xmap, up);
1406 break;
1407
1408 case UVCIOC_CTRL_QUERY32:
1409 cmd = UVCIOC_CTRL_QUERY;
1410 ret = uvc_v4l2_get_xu_query(&karg.xqry, up);
1411 break;
1412
1413 default:
1414 return -ENOIOCTLCMD;
1415 }
1416
1417 old_fs = get_fs();
1418 set_fs(KERNEL_DS);
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001419 ret = video_ioctl2(file, cmd, (unsigned long)&karg);
Laurent Pinchart1a5e4c82011-12-18 20:45:39 -03001420 set_fs(old_fs);
1421
1422 if (ret < 0)
1423 return ret;
1424
1425 switch (cmd) {
1426 case UVCIOC_CTRL_MAP:
1427 ret = uvc_v4l2_put_xu_mapping(&karg.xmap, up);
1428 break;
1429
1430 case UVCIOC_CTRL_QUERY:
1431 ret = uvc_v4l2_put_xu_query(&karg.xqry, up);
1432 break;
1433 }
1434
1435 return ret;
1436}
1437#endif
1438
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001439static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1440 size_t count, loff_t *ppos)
1441{
1442 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
Laurent Pinchart350d6402009-11-04 11:53:49 -03001443 return -EINVAL;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001444}
1445
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001446static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1447{
Joe Perchesabf84382010-07-12 17:50:03 -03001448 struct uvc_fh *handle = file->private_data;
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001449 struct uvc_streaming *stream = handle->stream;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001450
1451 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
1452
Laurent Pinchart4aa27592010-11-21 15:18:08 -03001453 return uvc_queue_mmap(&stream->queue, vma);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001454}
1455
1456static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
1457{
Joe Perchesabf84382010-07-12 17:50:03 -03001458 struct uvc_fh *handle = file->private_data;
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001459 struct uvc_streaming *stream = handle->stream;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001460
1461 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
1462
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001463 return uvc_queue_poll(&stream->queue, file, wait);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001464}
1465
Bob Liu72969442011-04-29 07:11:35 -03001466#ifndef CONFIG_MMU
1467static unsigned long uvc_v4l2_get_unmapped_area(struct file *file,
1468 unsigned long addr, unsigned long len, unsigned long pgoff,
1469 unsigned long flags)
1470{
1471 struct uvc_fh *handle = file->private_data;
1472 struct uvc_streaming *stream = handle->stream;
1473
1474 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_get_unmapped_area\n");
1475
1476 return uvc_queue_get_unmapped_area(&stream->queue, pgoff);
1477}
1478#endif
1479
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001480const struct v4l2_ioctl_ops uvc_ioctl_ops = {
1481 .vidioc_querycap = uvc_ioctl_querycap,
1482 .vidioc_enum_fmt_vid_cap = uvc_ioctl_enum_fmt_vid_cap,
1483 .vidioc_enum_fmt_vid_out = uvc_ioctl_enum_fmt_vid_out,
1484 .vidioc_g_fmt_vid_cap = uvc_ioctl_g_fmt_vid_cap,
1485 .vidioc_g_fmt_vid_out = uvc_ioctl_g_fmt_vid_out,
1486 .vidioc_s_fmt_vid_cap = uvc_ioctl_s_fmt_vid_cap,
1487 .vidioc_s_fmt_vid_out = uvc_ioctl_s_fmt_vid_out,
1488 .vidioc_try_fmt_vid_cap = uvc_ioctl_try_fmt_vid_cap,
1489 .vidioc_try_fmt_vid_out = uvc_ioctl_try_fmt_vid_out,
1490 .vidioc_reqbufs = uvc_ioctl_reqbufs,
1491 .vidioc_querybuf = uvc_ioctl_querybuf,
1492 .vidioc_qbuf = uvc_ioctl_qbuf,
Laurent Pinchart7195f612015-04-14 04:19:51 -03001493 .vidioc_expbuf = uvc_ioctl_expbuf,
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001494 .vidioc_dqbuf = uvc_ioctl_dqbuf,
1495 .vidioc_create_bufs = uvc_ioctl_create_bufs,
1496 .vidioc_streamon = uvc_ioctl_streamon,
1497 .vidioc_streamoff = uvc_ioctl_streamoff,
1498 .vidioc_enum_input = uvc_ioctl_enum_input,
1499 .vidioc_g_input = uvc_ioctl_g_input,
1500 .vidioc_s_input = uvc_ioctl_s_input,
1501 .vidioc_queryctrl = uvc_ioctl_queryctrl,
Hans Verkuile1832012015-03-14 14:04:00 -03001502 .vidioc_query_ext_ctrl = uvc_ioctl_query_ext_ctrl,
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001503 .vidioc_g_ctrl = uvc_ioctl_g_ctrl,
1504 .vidioc_s_ctrl = uvc_ioctl_s_ctrl,
1505 .vidioc_g_ext_ctrls = uvc_ioctl_g_ext_ctrls,
1506 .vidioc_s_ext_ctrls = uvc_ioctl_s_ext_ctrls,
1507 .vidioc_try_ext_ctrls = uvc_ioctl_try_ext_ctrls,
1508 .vidioc_querymenu = uvc_ioctl_querymenu,
Hans Verkuil1461fe72015-04-03 06:53:42 -03001509 .vidioc_g_selection = uvc_ioctl_g_selection,
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001510 .vidioc_g_parm = uvc_ioctl_g_parm,
1511 .vidioc_s_parm = uvc_ioctl_s_parm,
1512 .vidioc_enum_framesizes = uvc_ioctl_enum_framesizes,
1513 .vidioc_enum_frameintervals = uvc_ioctl_enum_frameintervals,
1514 .vidioc_subscribe_event = uvc_ioctl_subscribe_event,
1515 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1516 .vidioc_default = uvc_ioctl_default,
1517};
1518
Hans Verkuilbec43662008-12-30 06:58:20 -03001519const struct v4l2_file_operations uvc_fops = {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001520 .owner = THIS_MODULE,
1521 .open = uvc_v4l2_open,
1522 .release = uvc_v4l2_release,
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001523 .unlocked_ioctl = video_ioctl2,
Laurent Pinchart1a5e4c82011-12-18 20:45:39 -03001524#ifdef CONFIG_COMPAT
1525 .compat_ioctl32 = uvc_v4l2_compat_ioctl32,
1526#endif
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001527 .read = uvc_v4l2_read,
1528 .mmap = uvc_v4l2_mmap,
1529 .poll = uvc_v4l2_poll,
Bob Liu72969442011-04-29 07:11:35 -03001530#ifndef CONFIG_MMU
1531 .get_unmapped_area = uvc_v4l2_get_unmapped_area,
1532#endif
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001533};
Hans Verkuilf87086e2008-07-18 00:50:58 -03001534