blob: ed3bf05e2462f67cbc25eeb453f510e81b45be34 [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;
Dan Carpentercf2113c2016-11-25 08:28:35 -020069 goto free_map;
Haogang Chen806e23e2011-11-29 18:32:25 -030070 }
71
Laurent Pinchart561474c22010-02-18 16:38:52 -030072 size = xmap->menu_count * sizeof(*map->menu_info);
Markus Elfring4cc5bed2016-08-19 05:50:05 -030073 map->menu_info = memdup_user(xmap->menu_info, size);
74 if (IS_ERR(map->menu_info)) {
75 ret = PTR_ERR(map->menu_info);
Dan Carpentercf2113c2016-11-25 08:28:35 -020076 goto free_map;
Laurent Pinchart561474c22010-02-18 16:38:52 -030077 }
78
79 map->menu_count = xmap->menu_count;
80 break;
81
82 default:
Laurent Pinchartba2fa992010-09-20 05:53:21 -030083 uvc_trace(UVC_TRACE_CONTROL, "Unsupported V4L2 control type "
84 "%u.\n", xmap->v4l2_type);
Mauro Carvalho Chehab7a286cc2011-06-26 10:18:03 -030085 ret = -ENOTTY;
Dan Carpentercf2113c2016-11-25 08:28:35 -020086 goto free_map;
Laurent Pinchart561474c22010-02-18 16:38:52 -030087 }
88
Laurent Pinchartba2fa992010-09-20 05:53:21 -030089 ret = uvc_ctrl_add_mapping(chain, map);
Laurent Pinchart561474c22010-02-18 16:38:52 -030090
Laurent Pinchartba2fa992010-09-20 05:53:21 -030091 kfree(map->menu_info);
Dan Carpentercf2113c2016-11-25 08:28:35 -020092free_map:
Laurent Pinchartba2fa992010-09-20 05:53:21 -030093 kfree(map);
Laurent Pinchart561474c22010-02-18 16:38:52 -030094
95 return ret;
96}
97
98/* ------------------------------------------------------------------------
Laurent Pinchartc0efd232008-06-30 15:04:50 -030099 * V4L2 interface
100 */
101
102/*
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300103 * Find the frame interval closest to the requested frame interval for the
104 * given frame format and size. This should be done by the device as part of
105 * the Video Probe and Commit negotiation, but some hardware don't implement
106 * that feature.
107 */
108static __u32 uvc_try_frame_interval(struct uvc_frame *frame, __u32 interval)
109{
110 unsigned int i;
111
112 if (frame->bFrameIntervalType) {
113 __u32 best = -1, dist;
114
115 for (i = 0; i < frame->bFrameIntervalType; ++i) {
116 dist = interval > frame->dwFrameInterval[i]
117 ? interval - frame->dwFrameInterval[i]
118 : frame->dwFrameInterval[i] - interval;
119
120 if (dist > best)
121 break;
122
123 best = dist;
124 }
125
126 interval = frame->dwFrameInterval[i-1];
127 } else {
128 const __u32 min = frame->dwFrameInterval[0];
129 const __u32 max = frame->dwFrameInterval[1];
130 const __u32 step = frame->dwFrameInterval[2];
131
132 interval = min + (interval - min + step/2) / step * step;
133 if (interval > max)
134 interval = max;
135 }
136
137 return interval;
138}
139
Nicolas Dufresnef2224672016-01-07 18:43:48 -0200140static __u32 uvc_v4l2_get_bytesperline(const struct uvc_format *format,
141 const struct uvc_frame *frame)
142{
143 switch (format->fcc) {
144 case V4L2_PIX_FMT_NV12:
145 case V4L2_PIX_FMT_YVU420:
146 case V4L2_PIX_FMT_YUV420:
147 case V4L2_PIX_FMT_M420:
148 return frame->wWidth;
149
150 default:
151 return format->bpp * frame->wWidth / 8;
152 }
153}
154
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300155static int uvc_v4l2_try_format(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300156 struct v4l2_format *fmt, struct uvc_streaming_control *probe,
157 struct uvc_format **uvc_format, struct uvc_frame **uvc_frame)
158{
159 struct uvc_format *format = NULL;
160 struct uvc_frame *frame = NULL;
161 __u16 rw, rh;
162 unsigned int d, maxd;
163 unsigned int i;
164 __u32 interval;
165 int ret = 0;
166 __u8 *fcc;
167
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300168 if (fmt->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300169 return -EINVAL;
170
171 fcc = (__u8 *)&fmt->fmt.pix.pixelformat;
172 uvc_trace(UVC_TRACE_FORMAT, "Trying format 0x%08x (%c%c%c%c): %ux%u.\n",
173 fmt->fmt.pix.pixelformat,
174 fcc[0], fcc[1], fcc[2], fcc[3],
175 fmt->fmt.pix.width, fmt->fmt.pix.height);
176
Laurent Pinchart815adc42012-08-28 18:38:58 -0300177 /* Check if the hardware supports the requested format, use the default
178 * format otherwise.
179 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300180 for (i = 0; i < stream->nformats; ++i) {
181 format = &stream->format[i];
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300182 if (format->fcc == fmt->fmt.pix.pixelformat)
183 break;
184 }
185
Laurent Pinchart815adc42012-08-28 18:38:58 -0300186 if (i == stream->nformats) {
187 format = stream->def_format;
188 fmt->fmt.pix.pixelformat = format->fcc;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300189 }
190
191 /* Find the closest image size. The distance between image sizes is
192 * the size in pixels of the non-overlapping regions between the
193 * requested size and the frame-specified size.
194 */
195 rw = fmt->fmt.pix.width;
196 rh = fmt->fmt.pix.height;
197 maxd = (unsigned int)-1;
198
199 for (i = 0; i < format->nframes; ++i) {
200 __u16 w = format->frame[i].wWidth;
201 __u16 h = format->frame[i].wHeight;
202
203 d = min(w, rw) * min(h, rh);
204 d = w*h + rw*rh - 2*d;
205 if (d < maxd) {
206 maxd = d;
207 frame = &format->frame[i];
208 }
209
210 if (maxd == 0)
211 break;
212 }
213
214 if (frame == NULL) {
215 uvc_trace(UVC_TRACE_FORMAT, "Unsupported size %ux%u.\n",
216 fmt->fmt.pix.width, fmt->fmt.pix.height);
217 return -EINVAL;
218 }
219
220 /* Use the default frame interval. */
221 interval = frame->dwDefaultFrameInterval;
222 uvc_trace(UVC_TRACE_FORMAT, "Using default frame interval %u.%u us "
223 "(%u.%u fps).\n", interval/10, interval%10, 10000000/interval,
224 (100000000/interval)%10);
225
226 /* Set the format index, frame index and frame interval. */
227 memset(probe, 0, sizeof *probe);
228 probe->bmHint = 1; /* dwFrameInterval */
229 probe->bFormatIndex = format->index;
230 probe->bFrameIndex = frame->bFrameIndex;
231 probe->dwFrameInterval = uvc_try_frame_interval(frame, interval);
232 /* Some webcams stall the probe control set request when the
233 * dwMaxVideoFrameSize field is set to zero. The UVC specification
234 * clearly states that the field is read-only from the host, so this
235 * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by
236 * the webcam to work around the problem.
237 *
238 * The workaround could probably be enabled for all webcams, so the
239 * quirk can be removed if needed. It's currently useful to detect
240 * webcam bugs and fix them before they hit the market (providing
241 * developers test their webcams with the Linux driver as well as with
242 * the Windows driver).
243 */
Laurent Pinchart69477562010-11-21 13:36:34 -0300244 mutex_lock(&stream->mutex);
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300245 if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300246 probe->dwMaxVideoFrameSize =
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300247 stream->ctrl.dwMaxVideoFrameSize;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300248
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300249 /* Probe the device. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300250 ret = uvc_probe_video(stream, probe);
Laurent Pinchart69477562010-11-21 13:36:34 -0300251 mutex_unlock(&stream->mutex);
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300252 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300253 goto done;
254
255 fmt->fmt.pix.width = frame->wWidth;
256 fmt->fmt.pix.height = frame->wHeight;
257 fmt->fmt.pix.field = V4L2_FIELD_NONE;
Nicolas Dufresnef2224672016-01-07 18:43:48 -0200258 fmt->fmt.pix.bytesperline = uvc_v4l2_get_bytesperline(format, frame);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300259 fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize;
260 fmt->fmt.pix.colorspace = format->colorspace;
261 fmt->fmt.pix.priv = 0;
262
263 if (uvc_format != NULL)
264 *uvc_format = format;
265 if (uvc_frame != NULL)
266 *uvc_frame = frame;
267
268done:
269 return ret;
270}
271
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300272static int uvc_v4l2_get_format(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300273 struct v4l2_format *fmt)
274{
Laurent Pinchart69477562010-11-21 13:36:34 -0300275 struct uvc_format *format;
276 struct uvc_frame *frame;
277 int ret = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300278
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300279 if (fmt->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300280 return -EINVAL;
281
Laurent Pinchart69477562010-11-21 13:36:34 -0300282 mutex_lock(&stream->mutex);
283 format = stream->cur_format;
284 frame = stream->cur_frame;
285
286 if (format == NULL || frame == NULL) {
287 ret = -EINVAL;
288 goto done;
289 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300290
291 fmt->fmt.pix.pixelformat = format->fcc;
292 fmt->fmt.pix.width = frame->wWidth;
293 fmt->fmt.pix.height = frame->wHeight;
294 fmt->fmt.pix.field = V4L2_FIELD_NONE;
Nicolas Dufresnef2224672016-01-07 18:43:48 -0200295 fmt->fmt.pix.bytesperline = uvc_v4l2_get_bytesperline(format, frame);
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300296 fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300297 fmt->fmt.pix.colorspace = format->colorspace;
298 fmt->fmt.pix.priv = 0;
299
Laurent Pinchart69477562010-11-21 13:36:34 -0300300done:
301 mutex_unlock(&stream->mutex);
302 return ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300303}
304
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300305static int uvc_v4l2_set_format(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300306 struct v4l2_format *fmt)
307{
308 struct uvc_streaming_control probe;
309 struct uvc_format *format;
310 struct uvc_frame *frame;
311 int ret;
312
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300313 if (fmt->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300314 return -EINVAL;
315
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300316 ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300317 if (ret < 0)
318 return ret;
319
Laurent Pinchart69477562010-11-21 13:36:34 -0300320 mutex_lock(&stream->mutex);
321
322 if (uvc_queue_allocated(&stream->queue)) {
323 ret = -EBUSY;
324 goto done;
325 }
326
Ezequiel Garcia8c0d44e2013-01-14 15:22:55 -0300327 stream->ctrl = probe;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300328 stream->cur_format = format;
329 stream->cur_frame = frame;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300330
Laurent Pinchart69477562010-11-21 13:36:34 -0300331done:
332 mutex_unlock(&stream->mutex);
333 return ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300334}
335
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300336static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300337 struct v4l2_streamparm *parm)
338{
339 uint32_t numerator, denominator;
340
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300341 if (parm->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300342 return -EINVAL;
343
Laurent Pinchart69477562010-11-21 13:36:34 -0300344 mutex_lock(&stream->mutex);
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300345 numerator = stream->ctrl.dwFrameInterval;
Laurent Pinchart69477562010-11-21 13:36:34 -0300346 mutex_unlock(&stream->mutex);
347
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300348 denominator = 10000000;
349 uvc_simplify_fraction(&numerator, &denominator, 8, 333);
350
351 memset(parm, 0, sizeof *parm);
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300352 parm->type = stream->type;
Laurent Pinchartff924202008-12-28 22:32:29 -0300353
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300354 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Laurent Pinchartff924202008-12-28 22:32:29 -0300355 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
356 parm->parm.capture.capturemode = 0;
357 parm->parm.capture.timeperframe.numerator = numerator;
358 parm->parm.capture.timeperframe.denominator = denominator;
359 parm->parm.capture.extendedmode = 0;
360 parm->parm.capture.readbuffers = 0;
361 } else {
362 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
363 parm->parm.output.outputmode = 0;
364 parm->parm.output.timeperframe.numerator = numerator;
365 parm->parm.output.timeperframe.denominator = denominator;
366 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300367
368 return 0;
369}
370
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300371static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300372 struct v4l2_streamparm *parm)
373{
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300374 struct uvc_streaming_control probe;
Laurent Pinchartff924202008-12-28 22:32:29 -0300375 struct v4l2_fract timeperframe;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300376 uint32_t interval;
377 int ret;
378
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300379 if (parm->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300380 return -EINVAL;
381
Laurent Pinchartff924202008-12-28 22:32:29 -0300382 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
383 timeperframe = parm->parm.capture.timeperframe;
384 else
385 timeperframe = parm->parm.output.timeperframe;
386
Laurent Pinchartff924202008-12-28 22:32:29 -0300387 interval = uvc_fraction_to_interval(timeperframe.numerator,
388 timeperframe.denominator);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300389 uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
Laurent Pinchartff924202008-12-28 22:32:29 -0300390 timeperframe.numerator, timeperframe.denominator, interval);
Laurent Pinchart69477562010-11-21 13:36:34 -0300391
392 mutex_lock(&stream->mutex);
393
394 if (uvc_queue_streaming(&stream->queue)) {
395 mutex_unlock(&stream->mutex);
396 return -EBUSY;
397 }
398
Ezequiel Garcia8c0d44e2013-01-14 15:22:55 -0300399 probe = stream->ctrl;
Laurent Pinchart69477562010-11-21 13:36:34 -0300400 probe.dwFrameInterval =
401 uvc_try_frame_interval(stream->cur_frame, interval);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300402
403 /* Probe the device with the new settings. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300404 ret = uvc_probe_video(stream, &probe);
Laurent Pinchart69477562010-11-21 13:36:34 -0300405 if (ret < 0) {
406 mutex_unlock(&stream->mutex);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300407 return ret;
Laurent Pinchart69477562010-11-21 13:36:34 -0300408 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300409
Ezequiel Garcia8c0d44e2013-01-14 15:22:55 -0300410 stream->ctrl = probe;
Laurent Pinchart69477562010-11-21 13:36:34 -0300411 mutex_unlock(&stream->mutex);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300412
413 /* Return the actual frame period. */
Laurent Pinchartff924202008-12-28 22:32:29 -0300414 timeperframe.numerator = probe.dwFrameInterval;
415 timeperframe.denominator = 10000000;
416 uvc_simplify_fraction(&timeperframe.numerator,
417 &timeperframe.denominator, 8, 333);
418
419 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
420 parm->parm.capture.timeperframe = timeperframe;
421 else
422 parm->parm.output.timeperframe = timeperframe;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300423
424 return 0;
425}
426
427/* ------------------------------------------------------------------------
428 * Privilege management
429 */
430
431/*
432 * Privilege management is the multiple-open implementation basis. The current
433 * implementation is completely transparent for the end-user and doesn't
434 * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls.
435 * Those ioctls enable finer control on the device (by making possible for a
436 * user to request exclusive access to a device), but are not mature yet.
437 * Switching to the V4L2 priority mechanism might be considered in the future
438 * if this situation changes.
439 *
440 * Each open instance of a UVC device can either be in a privileged or
441 * unprivileged state. Only a single instance can be in a privileged state at
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300442 * a given time. Trying to perform an operation that requires privileges will
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300443 * automatically acquire the required privileges if possible, or return -EBUSY
Laurent Pinchart1a969d92009-09-02 03:12:26 -0300444 * otherwise. Privileges are dismissed when closing the instance or when
445 * freeing the video buffers using VIDIOC_REQBUFS.
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300446 *
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300447 * Operations that require privileges are:
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300448 *
449 * - VIDIOC_S_INPUT
450 * - VIDIOC_S_PARM
451 * - VIDIOC_S_FMT
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300452 * - VIDIOC_REQBUFS
453 */
454static int uvc_acquire_privileges(struct uvc_fh *handle)
455{
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300456 /* Always succeed if the handle is already privileged. */
457 if (handle->state == UVC_HANDLE_ACTIVE)
458 return 0;
459
460 /* Check if the device already has a privileged handle. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300461 if (atomic_inc_return(&handle->stream->active) != 1) {
462 atomic_dec(&handle->stream->active);
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300463 return -EBUSY;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300464 }
465
466 handle->state = UVC_HANDLE_ACTIVE;
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300467 return 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300468}
469
470static void uvc_dismiss_privileges(struct uvc_fh *handle)
471{
472 if (handle->state == UVC_HANDLE_ACTIVE)
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300473 atomic_dec(&handle->stream->active);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300474
475 handle->state = UVC_HANDLE_PASSIVE;
476}
477
478static int uvc_has_privileges(struct uvc_fh *handle)
479{
480 return handle->state == UVC_HANDLE_ACTIVE;
481}
482
483/* ------------------------------------------------------------------------
484 * V4L2 file operations
485 */
486
Hans Verkuilbec43662008-12-30 06:58:20 -0300487static int uvc_v4l2_open(struct file *file)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300488{
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300489 struct uvc_streaming *stream;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300490 struct uvc_fh *handle;
491 int ret = 0;
492
493 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300494 stream = video_drvdata(file);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300495
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300496 ret = usb_autopm_get_interface(stream->dev->intf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300497 if (ret < 0)
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300498 return ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300499
500 /* Create the device handle. */
501 handle = kzalloc(sizeof *handle, GFP_KERNEL);
502 if (handle == NULL) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300503 usb_autopm_put_interface(stream->dev->intf);
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300504 return -ENOMEM;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300505 }
506
Laurent Pinchart17706f52013-04-25 22:28:51 -0300507 mutex_lock(&stream->dev->lock);
508 if (stream->dev->users == 0) {
509 ret = uvc_status_start(stream->dev, GFP_KERNEL);
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300510 if (ret < 0) {
Laurent Pinchart17706f52013-04-25 22:28:51 -0300511 mutex_unlock(&stream->dev->lock);
Oliver Neukuma82a45f2013-01-10 07:04:55 -0300512 usb_autopm_put_interface(stream->dev->intf);
Laurent Pinchart04a37e02009-05-19 10:08:03 -0300513 kfree(handle);
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300514 return ret;
Laurent Pinchart04a37e02009-05-19 10:08:03 -0300515 }
516 }
517
Laurent Pinchart17706f52013-04-25 22:28:51 -0300518 stream->dev->users++;
519 mutex_unlock(&stream->dev->lock);
520
Hans Verkuild8da7512015-03-09 13:34:11 -0300521 v4l2_fh_init(&handle->vfh, &stream->vdev);
Hans de Goedeb4012002012-04-08 12:59:51 -0300522 v4l2_fh_add(&handle->vfh);
Laurent Pinchart8e113592009-07-01 20:24:47 -0300523 handle->chain = stream->chain;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300524 handle->stream = stream;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300525 handle->state = UVC_HANDLE_PASSIVE;
526 file->private_data = handle;
527
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300528 return 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300529}
530
Hans Verkuilbec43662008-12-30 06:58:20 -0300531static int uvc_v4l2_release(struct file *file)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300532{
Joe Perchesabf84382010-07-12 17:50:03 -0300533 struct uvc_fh *handle = file->private_data;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300534 struct uvc_streaming *stream = handle->stream;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300535
536 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");
537
538 /* Only free resources if this is a privileged handle. */
Laurent Pinchart3f02de22014-10-21 16:07:15 -0300539 if (uvc_has_privileges(handle))
540 uvc_queue_release(&stream->queue);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300541
542 /* Release the file handle. */
543 uvc_dismiss_privileges(handle);
Hans de Goedeb4012002012-04-08 12:59:51 -0300544 v4l2_fh_del(&handle->vfh);
545 v4l2_fh_exit(&handle->vfh);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300546 kfree(handle);
547 file->private_data = NULL;
548
Laurent Pinchart17706f52013-04-25 22:28:51 -0300549 mutex_lock(&stream->dev->lock);
550 if (--stream->dev->users == 0)
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300551 uvc_status_stop(stream->dev);
Laurent Pinchart17706f52013-04-25 22:28:51 -0300552 mutex_unlock(&stream->dev->lock);
Laurent Pinchart04a37e02009-05-19 10:08:03 -0300553
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300554 usb_autopm_put_interface(stream->dev->intf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300555 return 0;
556}
557
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300558static int uvc_ioctl_querycap(struct file *file, void *fh,
559 struct v4l2_capability *cap)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300560{
561 struct video_device *vdev = video_devdata(file);
Joe Perchesabf84382010-07-12 17:50:03 -0300562 struct uvc_fh *handle = file->private_data;
Laurent Pinchart8e113592009-07-01 20:24:47 -0300563 struct uvc_video_chain *chain = handle->chain;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300564 struct uvc_streaming *stream = handle->stream;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300565
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300566 strlcpy(cap->driver, "uvcvideo", sizeof(cap->driver));
567 strlcpy(cap->card, vdev->name, sizeof(cap->card));
568 usb_make_path(stream->dev->udev, cap->bus_info, sizeof(cap->bus_info));
569 cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
570 | chain->caps;
571 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
572 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
573 else
574 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300575
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300576 return 0;
577}
578
579static int uvc_ioctl_enum_fmt(struct uvc_streaming *stream,
580 struct v4l2_fmtdesc *fmt)
581{
582 struct uvc_format *format;
583 enum v4l2_buf_type type = fmt->type;
584 __u32 index = fmt->index;
585
586 if (fmt->type != stream->type || fmt->index >= stream->nformats)
587 return -EINVAL;
588
589 memset(fmt, 0, sizeof(*fmt));
590 fmt->index = index;
591 fmt->type = type;
592
593 format = &stream->format[fmt->index];
594 fmt->flags = 0;
595 if (format->flags & UVC_FMT_FLAG_COMPRESSED)
596 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
597 strlcpy(fmt->description, format->name, sizeof(fmt->description));
598 fmt->description[sizeof(fmt->description) - 1] = 0;
599 fmt->pixelformat = format->fcc;
600 return 0;
601}
602
603static int uvc_ioctl_enum_fmt_vid_cap(struct file *file, void *fh,
604 struct v4l2_fmtdesc *fmt)
605{
606 struct uvc_fh *handle = fh;
607 struct uvc_streaming *stream = handle->stream;
608
609 return uvc_ioctl_enum_fmt(stream, fmt);
610}
611
612static int uvc_ioctl_enum_fmt_vid_out(struct file *file, void *fh,
613 struct v4l2_fmtdesc *fmt)
614{
615 struct uvc_fh *handle = fh;
616 struct uvc_streaming *stream = handle->stream;
617
618 return uvc_ioctl_enum_fmt(stream, fmt);
619}
620
621static int uvc_ioctl_g_fmt_vid_cap(struct file *file, void *fh,
622 struct v4l2_format *fmt)
623{
624 struct uvc_fh *handle = fh;
625 struct uvc_streaming *stream = handle->stream;
626
627 return uvc_v4l2_get_format(stream, fmt);
628}
629
630static int uvc_ioctl_g_fmt_vid_out(struct file *file, void *fh,
631 struct v4l2_format *fmt)
632{
633 struct uvc_fh *handle = fh;
634 struct uvc_streaming *stream = handle->stream;
635
636 return uvc_v4l2_get_format(stream, fmt);
637}
638
639static int uvc_ioctl_s_fmt_vid_cap(struct file *file, void *fh,
640 struct v4l2_format *fmt)
641{
642 struct uvc_fh *handle = fh;
643 struct uvc_streaming *stream = handle->stream;
644 int ret;
645
646 ret = uvc_acquire_privileges(handle);
647 if (ret < 0)
648 return ret;
649
650 return uvc_v4l2_set_format(stream, fmt);
651}
652
653static int uvc_ioctl_s_fmt_vid_out(struct file *file, void *fh,
654 struct v4l2_format *fmt)
655{
656 struct uvc_fh *handle = fh;
657 struct uvc_streaming *stream = handle->stream;
658 int ret;
659
660 ret = uvc_acquire_privileges(handle);
661 if (ret < 0)
662 return ret;
663
664 return uvc_v4l2_set_format(stream, fmt);
665}
666
667static int uvc_ioctl_try_fmt_vid_cap(struct file *file, void *fh,
668 struct v4l2_format *fmt)
669{
670 struct uvc_fh *handle = fh;
671 struct uvc_streaming *stream = handle->stream;
672 struct uvc_streaming_control probe;
673
674 return uvc_v4l2_try_format(stream, fmt, &probe, NULL, NULL);
675}
676
677static int uvc_ioctl_try_fmt_vid_out(struct file *file, void *fh,
678 struct v4l2_format *fmt)
679{
680 struct uvc_fh *handle = fh;
681 struct uvc_streaming *stream = handle->stream;
682 struct uvc_streaming_control probe;
683
684 return uvc_v4l2_try_format(stream, fmt, &probe, NULL, NULL);
685}
686
687static int uvc_ioctl_reqbufs(struct file *file, void *fh,
688 struct v4l2_requestbuffers *rb)
689{
690 struct uvc_fh *handle = fh;
691 struct uvc_streaming *stream = handle->stream;
692 int ret;
693
694 ret = uvc_acquire_privileges(handle);
695 if (ret < 0)
696 return ret;
697
698 mutex_lock(&stream->mutex);
Laurent Pinchart1b7f9c92014-10-21 16:02:00 -0300699 ret = uvc_request_buffers(&stream->queue, rb);
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300700 mutex_unlock(&stream->mutex);
701 if (ret < 0)
702 return ret;
703
704 if (ret == 0)
705 uvc_dismiss_privileges(handle);
706
707 return 0;
708}
709
710static int uvc_ioctl_querybuf(struct file *file, void *fh,
711 struct v4l2_buffer *buf)
712{
713 struct uvc_fh *handle = fh;
714 struct uvc_streaming *stream = handle->stream;
715
716 if (!uvc_has_privileges(handle))
717 return -EBUSY;
718
719 return uvc_query_buffer(&stream->queue, buf);
720}
721
722static int uvc_ioctl_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
723{
724 struct uvc_fh *handle = fh;
725 struct uvc_streaming *stream = handle->stream;
726
727 if (!uvc_has_privileges(handle))
728 return -EBUSY;
729
730 return uvc_queue_buffer(&stream->queue, buf);
731}
732
Laurent Pinchart7195f612015-04-14 04:19:51 -0300733static int uvc_ioctl_expbuf(struct file *file, void *fh,
734 struct v4l2_exportbuffer *exp)
735{
736 struct uvc_fh *handle = fh;
737 struct uvc_streaming *stream = handle->stream;
738
739 if (!uvc_has_privileges(handle))
740 return -EBUSY;
741
742 return uvc_export_buffer(&stream->queue, exp);
743}
744
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300745static int uvc_ioctl_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
746{
747 struct uvc_fh *handle = fh;
748 struct uvc_streaming *stream = handle->stream;
749
750 if (!uvc_has_privileges(handle))
751 return -EBUSY;
752
753 return uvc_dequeue_buffer(&stream->queue, buf,
754 file->f_flags & O_NONBLOCK);
755}
756
757static int uvc_ioctl_create_bufs(struct file *file, void *fh,
758 struct v4l2_create_buffers *cb)
759{
760 struct uvc_fh *handle = fh;
761 struct uvc_streaming *stream = handle->stream;
762 int ret;
763
764 ret = uvc_acquire_privileges(handle);
765 if (ret < 0)
766 return ret;
767
768 return uvc_create_buffers(&stream->queue, cb);
769}
770
771static int uvc_ioctl_streamon(struct file *file, void *fh,
772 enum v4l2_buf_type type)
773{
774 struct uvc_fh *handle = fh;
775 struct uvc_streaming *stream = handle->stream;
776 int ret;
777
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300778 if (!uvc_has_privileges(handle))
779 return -EBUSY;
780
781 mutex_lock(&stream->mutex);
Laurent Pinchart0da4ab92014-10-21 16:19:04 -0300782 ret = uvc_queue_streamon(&stream->queue, type);
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300783 mutex_unlock(&stream->mutex);
784
785 return ret;
786}
787
788static int uvc_ioctl_streamoff(struct file *file, void *fh,
789 enum v4l2_buf_type type)
790{
791 struct uvc_fh *handle = fh;
792 struct uvc_streaming *stream = handle->stream;
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300793
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300794 if (!uvc_has_privileges(handle))
795 return -EBUSY;
796
797 mutex_lock(&stream->mutex);
Laurent Pinchart0da4ab92014-10-21 16:19:04 -0300798 uvc_queue_streamoff(&stream->queue, type);
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300799 mutex_unlock(&stream->mutex);
800
Laurent Pinchartb83bba22014-10-13 10:11:35 -0300801 return 0;
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300802}
803
804static int uvc_ioctl_enum_input(struct file *file, void *fh,
805 struct v4l2_input *input)
806{
807 struct uvc_fh *handle = fh;
808 struct uvc_video_chain *chain = handle->chain;
809 const struct uvc_entity *selector = chain->selector;
810 struct uvc_entity *iterm = NULL;
811 u32 index = input->index;
812 int pin = 0;
813
814 if (selector == NULL ||
815 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
816 if (index != 0)
817 return -EINVAL;
818 list_for_each_entry(iterm, &chain->entities, chain) {
819 if (UVC_ENTITY_IS_ITERM(iterm))
820 break;
821 }
822 pin = iterm->id;
823 } else if (index < selector->bNrInPins) {
824 pin = selector->baSourceID[index];
825 list_for_each_entry(iterm, &chain->entities, chain) {
826 if (!UVC_ENTITY_IS_ITERM(iterm))
827 continue;
828 if (iterm->id == pin)
829 break;
830 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300831 }
832
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300833 if (iterm == NULL || iterm->id != pin)
834 return -EINVAL;
Laurent Pinchart05505132012-08-28 20:29:56 -0300835
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300836 memset(input, 0, sizeof(*input));
837 input->index = index;
838 strlcpy(input->name, iterm->name, sizeof(input->name));
839 if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
840 input->type = V4L2_INPUT_TYPE_CAMERA;
Laurent Pinchart05505132012-08-28 20:29:56 -0300841
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300842 return 0;
843}
Laurent Pinchart05505132012-08-28 20:29:56 -0300844
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300845static int uvc_ioctl_g_input(struct file *file, void *fh, unsigned int *input)
846{
847 struct uvc_fh *handle = fh;
848 struct uvc_video_chain *chain = handle->chain;
849 int ret;
850 u8 i;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300851
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300852 if (chain->selector == NULL ||
853 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
854 *input = 0;
855 return 0;
856 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300857
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300858 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, chain->selector->id,
859 chain->dev->intfnum, UVC_SU_INPUT_SELECT_CONTROL,
860 &i, 1);
861 if (ret < 0)
862 return ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300863
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300864 *input = i - 1;
865 return 0;
866}
Robert Krakora1fcbcc42009-06-12 13:51:03 -0300867
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300868static int uvc_ioctl_s_input(struct file *file, void *fh, unsigned int input)
869{
870 struct uvc_fh *handle = fh;
871 struct uvc_video_chain *chain = handle->chain;
872 int ret;
873 u32 i;
874
875 ret = uvc_acquire_privileges(handle);
876 if (ret < 0)
877 return ret;
878
879 if (chain->selector == NULL ||
880 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
881 if (input)
882 return -EINVAL;
883 return 0;
884 }
885
886 if (input >= chain->selector->bNrInPins)
887 return -EINVAL;
888
889 i = input + 1;
890 return uvc_query_ctrl(chain->dev, UVC_SET_CUR, chain->selector->id,
891 chain->dev->intfnum, UVC_SU_INPUT_SELECT_CONTROL,
892 &i, 1);
893}
894
895static int uvc_ioctl_queryctrl(struct file *file, void *fh,
896 struct v4l2_queryctrl *qc)
897{
898 struct uvc_fh *handle = fh;
899 struct uvc_video_chain *chain = handle->chain;
900
901 return uvc_query_v4l2_ctrl(chain, qc);
902}
903
Hans Verkuile1832012015-03-14 14:04:00 -0300904static int uvc_ioctl_query_ext_ctrl(struct file *file, void *fh,
905 struct v4l2_query_ext_ctrl *qec)
906{
907 struct uvc_fh *handle = fh;
908 struct uvc_video_chain *chain = handle->chain;
909 struct v4l2_queryctrl qc = { qec->id };
910 int ret;
911
912 ret = uvc_query_v4l2_ctrl(chain, &qc);
913 if (ret)
914 return ret;
915
916 qec->id = qc.id;
917 qec->type = qc.type;
918 strlcpy(qec->name, qc.name, sizeof(qec->name));
919 qec->minimum = qc.minimum;
920 qec->maximum = qc.maximum;
921 qec->step = qc.step;
922 qec->default_value = qc.default_value;
923 qec->flags = qc.flags;
924 qec->elem_size = 4;
925 qec->elems = 1;
926 qec->nr_of_dims = 0;
927 memset(qec->dims, 0, sizeof(qec->dims));
928 memset(qec->reserved, 0, sizeof(qec->reserved));
929
930 return 0;
931}
932
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300933static int uvc_ioctl_g_ctrl(struct file *file, void *fh,
934 struct v4l2_control *ctrl)
935{
936 struct uvc_fh *handle = fh;
937 struct uvc_video_chain *chain = handle->chain;
938 struct v4l2_ext_control xctrl;
939 int ret;
940
941 memset(&xctrl, 0, sizeof(xctrl));
942 xctrl.id = ctrl->id;
943
944 ret = uvc_ctrl_begin(chain);
945 if (ret < 0)
946 return ret;
947
948 ret = uvc_ctrl_get(chain, &xctrl);
949 uvc_ctrl_rollback(handle);
950 if (ret < 0)
951 return ret;
952
953 ctrl->value = xctrl.value;
954 return 0;
955}
956
957static int uvc_ioctl_s_ctrl(struct file *file, void *fh,
958 struct v4l2_control *ctrl)
959{
960 struct uvc_fh *handle = fh;
961 struct uvc_video_chain *chain = handle->chain;
962 struct v4l2_ext_control xctrl;
963 int ret;
964
965 memset(&xctrl, 0, sizeof(xctrl));
966 xctrl.id = ctrl->id;
967 xctrl.value = ctrl->value;
968
969 ret = uvc_ctrl_begin(chain);
970 if (ret < 0)
971 return ret;
972
973 ret = uvc_ctrl_set(chain, &xctrl);
974 if (ret < 0) {
Hans de Goedeb4012002012-04-08 12:59:51 -0300975 uvc_ctrl_rollback(handle);
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300976 return ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300977 }
978
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300979 ret = uvc_ctrl_commit(handle, &xctrl, 1);
980 if (ret < 0)
981 return ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300982
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300983 ctrl->value = xctrl.value;
984 return 0;
985}
Laurent Pinchart05505132012-08-28 20:29:56 -0300986
Laurent Pinchartd5e90b72010-09-30 10:17:54 -0300987static int uvc_ioctl_g_ext_ctrls(struct file *file, void *fh,
988 struct v4l2_ext_controls *ctrls)
989{
990 struct uvc_fh *handle = fh;
991 struct uvc_video_chain *chain = handle->chain;
992 struct v4l2_ext_control *ctrl = ctrls->controls;
993 unsigned int i;
994 int ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300995
Ricardo Ribalda91739832015-10-29 08:10:30 -0200996 if (ctrls->which == V4L2_CTRL_WHICH_DEF_VAL) {
997 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
998 struct v4l2_queryctrl qc = { .id = ctrl->id };
999
1000 ret = uvc_query_v4l2_ctrl(chain, &qc);
1001 if (ret < 0) {
1002 ctrls->error_idx = i;
1003 return ret;
1004 }
1005
1006 ctrl->value = qc.default_value;
1007 }
1008
1009 return 0;
1010 }
1011
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001012 ret = uvc_ctrl_begin(chain);
1013 if (ret < 0)
1014 return ret;
Robert Krakora1fcbcc42009-06-12 13:51:03 -03001015
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001016 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
1017 ret = uvc_ctrl_get(chain, ctrl);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001018 if (ret < 0) {
Hans de Goedeb4012002012-04-08 12:59:51 -03001019 uvc_ctrl_rollback(handle);
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001020 ctrls->error_idx = i;
Rafael J. Wysocki9c016d62012-12-23 14:39:32 +01001021 return ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001022 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001023 }
1024
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001025 ctrls->error_idx = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001026
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001027 return uvc_ctrl_rollback(handle);
1028}
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001029
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001030static int uvc_ioctl_s_try_ext_ctrls(struct uvc_fh *handle,
1031 struct v4l2_ext_controls *ctrls,
1032 bool commit)
1033{
1034 struct v4l2_ext_control *ctrl = ctrls->controls;
1035 struct uvc_video_chain *chain = handle->chain;
1036 unsigned int i;
1037 int ret;
1038
Ricardo Ribalda91739832015-10-29 08:10:30 -02001039 /* Default value cannot be changed */
1040 if (ctrls->which == V4L2_CTRL_WHICH_DEF_VAL)
1041 return -EINVAL;
1042
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001043 ret = uvc_ctrl_begin(chain);
1044 if (ret < 0)
1045 return ret;
1046
1047 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
1048 ret = uvc_ctrl_set(chain, ctrl);
1049 if (ret < 0) {
1050 uvc_ctrl_rollback(handle);
1051 ctrls->error_idx = commit ? ctrls->count : i;
Robert Krakora1fcbcc42009-06-12 13:51:03 -03001052 return ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001053 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001054 }
1055
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001056 ctrls->error_idx = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001057
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001058 if (commit)
1059 return uvc_ctrl_commit(handle, ctrls->controls, ctrls->count);
1060 else
1061 return uvc_ctrl_rollback(handle);
1062}
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001063
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001064static int uvc_ioctl_s_ext_ctrls(struct file *file, void *fh,
1065 struct v4l2_ext_controls *ctrls)
1066{
1067 struct uvc_fh *handle = fh;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001068
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001069 return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, true);
1070}
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001071
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001072static int uvc_ioctl_try_ext_ctrls(struct file *file, void *fh,
1073 struct v4l2_ext_controls *ctrls)
1074{
1075 struct uvc_fh *handle = fh;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001076
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001077 return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, false);
1078}
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001079
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001080static int uvc_ioctl_querymenu(struct file *file, void *fh,
1081 struct v4l2_querymenu *qm)
1082{
1083 struct uvc_fh *handle = fh;
1084 struct uvc_video_chain *chain = handle->chain;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001085
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001086 return uvc_query_v4l2_menu(chain, qm);
1087}
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001088
Hans Verkuil1461fe72015-04-03 06:53:42 -03001089static int uvc_ioctl_g_selection(struct file *file, void *fh,
1090 struct v4l2_selection *sel)
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001091{
1092 struct uvc_fh *handle = fh;
1093 struct uvc_streaming *stream = handle->stream;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001094
Hans Verkuil1461fe72015-04-03 06:53:42 -03001095 if (sel->type != stream->type)
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001096 return -EINVAL;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001097
Hans Verkuil1461fe72015-04-03 06:53:42 -03001098 switch (sel->target) {
1099 case V4L2_SEL_TGT_CROP_DEFAULT:
1100 case V4L2_SEL_TGT_CROP_BOUNDS:
1101 if (stream->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1102 return -EINVAL;
1103 break;
1104 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1105 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1106 if (stream->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1107 return -EINVAL;
1108 break;
1109 default:
1110 return -EINVAL;
1111 }
1112
1113 sel->r.left = 0;
1114 sel->r.top = 0;
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001115 mutex_lock(&stream->mutex);
Hans Verkuil1461fe72015-04-03 06:53:42 -03001116 sel->r.width = stream->cur_frame->wWidth;
1117 sel->r.height = stream->cur_frame->wHeight;
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001118 mutex_unlock(&stream->mutex);
1119
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001120 return 0;
1121}
1122
1123static int uvc_ioctl_g_parm(struct file *file, void *fh,
1124 struct v4l2_streamparm *parm)
1125{
1126 struct uvc_fh *handle = fh;
1127 struct uvc_streaming *stream = handle->stream;
1128
1129 return uvc_v4l2_get_streamparm(stream, parm);
1130}
1131
1132static int uvc_ioctl_s_parm(struct file *file, void *fh,
1133 struct v4l2_streamparm *parm)
1134{
1135 struct uvc_fh *handle = fh;
1136 struct uvc_streaming *stream = handle->stream;
1137 int ret;
1138
1139 ret = uvc_acquire_privileges(handle);
1140 if (ret < 0)
1141 return ret;
1142
1143 return uvc_v4l2_set_streamparm(stream, parm);
1144}
1145
1146static int uvc_ioctl_enum_framesizes(struct file *file, void *fh,
1147 struct v4l2_frmsizeenum *fsize)
1148{
1149 struct uvc_fh *handle = fh;
1150 struct uvc_streaming *stream = handle->stream;
1151 struct uvc_format *format = NULL;
1152 struct uvc_frame *frame;
1153 int i;
1154
1155 /* Look for the given pixel format */
1156 for (i = 0; i < stream->nformats; i++) {
1157 if (stream->format[i].fcc == fsize->pixel_format) {
1158 format = &stream->format[i];
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001159 break;
1160 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001161 }
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001162 if (format == NULL)
1163 return -EINVAL;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001164
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001165 if (fsize->index >= format->nframes)
1166 return -EINVAL;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001167
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001168 frame = &format->frame[fsize->index];
1169 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1170 fsize->discrete.width = frame->wWidth;
1171 fsize->discrete.height = frame->wHeight;
1172 return 0;
1173}
Laurent Pinchart05505132012-08-28 20:29:56 -03001174
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001175static int uvc_ioctl_enum_frameintervals(struct file *file, void *fh,
1176 struct v4l2_frmivalenum *fival)
1177{
1178 struct uvc_fh *handle = fh;
1179 struct uvc_streaming *stream = handle->stream;
1180 struct uvc_format *format = NULL;
1181 struct uvc_frame *frame = NULL;
1182 int i;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001183
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001184 /* Look for the given pixel format and frame size */
1185 for (i = 0; i < stream->nformats; i++) {
1186 if (stream->format[i].fcc == fival->pixel_format) {
1187 format = &stream->format[i];
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001188 break;
1189 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001190 }
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001191 if (format == NULL)
1192 return -EINVAL;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001193
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001194 for (i = 0; i < format->nframes; i++) {
1195 if (format->frame[i].wWidth == fival->width &&
1196 format->frame[i].wHeight == fival->height) {
1197 frame = &format->frame[i];
1198 break;
Hans de Goedeb4012002012-04-08 12:59:51 -03001199 }
1200 }
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001201 if (frame == NULL)
1202 return -EINVAL;
Hans de Goedeb4012002012-04-08 12:59:51 -03001203
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001204 if (frame->bFrameIntervalType) {
1205 if (fival->index >= frame->bFrameIntervalType)
1206 return -EINVAL;
Hans de Goedeb4012002012-04-08 12:59:51 -03001207
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001208 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1209 fival->discrete.numerator =
1210 frame->dwFrameInterval[fival->index];
1211 fival->discrete.denominator = 10000000;
1212 uvc_simplify_fraction(&fival->discrete.numerator,
1213 &fival->discrete.denominator, 8, 333);
1214 } else {
Laurent Pinchart1cdf60c2015-03-06 09:54:47 -03001215 if (fival->index)
1216 return -EINVAL;
1217
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001218 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
1219 fival->stepwise.min.numerator = frame->dwFrameInterval[0];
1220 fival->stepwise.min.denominator = 10000000;
1221 fival->stepwise.max.numerator = frame->dwFrameInterval[1];
1222 fival->stepwise.max.denominator = 10000000;
1223 fival->stepwise.step.numerator = frame->dwFrameInterval[2];
1224 fival->stepwise.step.denominator = 10000000;
1225 uvc_simplify_fraction(&fival->stepwise.min.numerator,
1226 &fival->stepwise.min.denominator, 8, 333);
1227 uvc_simplify_fraction(&fival->stepwise.max.numerator,
1228 &fival->stepwise.max.denominator, 8, 333);
1229 uvc_simplify_fraction(&fival->stepwise.step.numerator,
1230 &fival->stepwise.step.denominator, 8, 333);
1231 }
Hans de Goedeb4012002012-04-08 12:59:51 -03001232
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001233 return 0;
1234}
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001235
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001236static int uvc_ioctl_subscribe_event(struct v4l2_fh *fh,
1237 const struct v4l2_event_subscription *sub)
1238{
1239 switch (sub->type) {
1240 case V4L2_EVENT_CTRL:
1241 return v4l2_event_subscribe(fh, sub, 0, &uvc_ctrl_sub_ev_ops);
1242 default:
1243 return -EINVAL;
1244 }
1245}
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001246
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001247static long uvc_ioctl_default(struct file *file, void *fh, bool valid_prio,
1248 unsigned int cmd, void *arg)
1249{
1250 struct uvc_fh *handle = fh;
1251 struct uvc_video_chain *chain = handle->chain;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001252
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001253 switch (cmd) {
1254 /* Dynamic controls. */
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001255 case UVCIOC_CTRL_MAP:
Laurent Pinchart227bd5b2011-07-30 16:19:49 -03001256 return uvc_ioctl_ctrl_map(chain, arg);
Martin Rublife78d182010-10-02 19:10:16 -03001257
1258 case UVCIOC_CTRL_QUERY:
1259 return uvc_xu_ctrl_query(chain, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001260
1261 default:
Laurent Pincharta3ec69b2011-12-18 20:22:50 -03001262 return -ENOTTY;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001263 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001264}
1265
Laurent Pinchart1a5e4c82011-12-18 20:45:39 -03001266#ifdef CONFIG_COMPAT
1267struct uvc_xu_control_mapping32 {
1268 __u32 id;
1269 __u8 name[32];
1270 __u8 entity[16];
1271 __u8 selector;
1272
1273 __u8 size;
1274 __u8 offset;
1275 __u32 v4l2_type;
1276 __u32 data_type;
1277
1278 compat_caddr_t menu_info;
1279 __u32 menu_count;
1280
1281 __u32 reserved[4];
1282};
1283
1284static int uvc_v4l2_get_xu_mapping(struct uvc_xu_control_mapping *kp,
1285 const struct uvc_xu_control_mapping32 __user *up)
1286{
Laurent Pinchart1a5e4c82011-12-18 20:45:39 -03001287 compat_caddr_t p;
1288
1289 if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
1290 __copy_from_user(kp, up, offsetof(typeof(*up), menu_info)) ||
1291 __get_user(kp->menu_count, &up->menu_count))
1292 return -EFAULT;
1293
1294 memset(kp->reserved, 0, sizeof(kp->reserved));
1295
1296 if (kp->menu_count == 0) {
1297 kp->menu_info = NULL;
1298 return 0;
1299 }
1300
1301 if (__get_user(p, &up->menu_info))
1302 return -EFAULT;
Andy Lutomirskif89dec72016-05-11 17:41:27 -07001303 kp->menu_info = compat_ptr(p);
Laurent Pinchart1a5e4c82011-12-18 20:45:39 -03001304
1305 return 0;
1306}
1307
1308static int uvc_v4l2_put_xu_mapping(const struct uvc_xu_control_mapping *kp,
1309 struct uvc_xu_control_mapping32 __user *up)
1310{
Laurent Pinchart1a5e4c82011-12-18 20:45:39 -03001311 if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
1312 __copy_to_user(up, kp, offsetof(typeof(*up), menu_info)) ||
1313 __put_user(kp->menu_count, &up->menu_count))
1314 return -EFAULT;
1315
Hans de Goede57fb4a42012-04-08 12:59:48 -03001316 if (__clear_user(up->reserved, sizeof(up->reserved)))
1317 return -EFAULT;
Laurent Pinchart1a5e4c82011-12-18 20:45:39 -03001318
Laurent Pinchart1a5e4c82011-12-18 20:45:39 -03001319 return 0;
1320}
1321
1322struct uvc_xu_control_query32 {
1323 __u8 unit;
1324 __u8 selector;
1325 __u8 query;
1326 __u16 size;
1327 compat_caddr_t data;
1328};
1329
1330static int uvc_v4l2_get_xu_query(struct uvc_xu_control_query *kp,
1331 const struct uvc_xu_control_query32 __user *up)
1332{
Laurent Pinchart1a5e4c82011-12-18 20:45:39 -03001333 compat_caddr_t p;
1334
1335 if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
1336 __copy_from_user(kp, up, offsetof(typeof(*up), data)))
1337 return -EFAULT;
1338
1339 if (kp->size == 0) {
1340 kp->data = NULL;
1341 return 0;
1342 }
1343
1344 if (__get_user(p, &up->data))
1345 return -EFAULT;
Andy Lutomirskif89dec72016-05-11 17:41:27 -07001346 kp->data = compat_ptr(p);
Laurent Pinchart1a5e4c82011-12-18 20:45:39 -03001347
1348 return 0;
1349}
1350
1351static int uvc_v4l2_put_xu_query(const struct uvc_xu_control_query *kp,
1352 struct uvc_xu_control_query32 __user *up)
1353{
Laurent Pinchart1a5e4c82011-12-18 20:45:39 -03001354 if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
1355 __copy_to_user(up, kp, offsetof(typeof(*up), data)))
1356 return -EFAULT;
1357
Laurent Pinchart1a5e4c82011-12-18 20:45:39 -03001358 return 0;
1359}
1360
1361#define UVCIOC_CTRL_MAP32 _IOWR('u', 0x20, struct uvc_xu_control_mapping32)
1362#define UVCIOC_CTRL_QUERY32 _IOWR('u', 0x21, struct uvc_xu_control_query32)
1363
1364static long uvc_v4l2_compat_ioctl32(struct file *file,
1365 unsigned int cmd, unsigned long arg)
1366{
Andy Lutomirskia44323e2016-05-24 15:13:02 -07001367 struct uvc_fh *handle = file->private_data;
Laurent Pinchart1a5e4c82011-12-18 20:45:39 -03001368 union {
1369 struct uvc_xu_control_mapping xmap;
1370 struct uvc_xu_control_query xqry;
1371 } karg;
1372 void __user *up = compat_ptr(arg);
Laurent Pinchart1a5e4c82011-12-18 20:45:39 -03001373 long ret;
1374
1375 switch (cmd) {
1376 case UVCIOC_CTRL_MAP32:
Laurent Pinchart1a5e4c82011-12-18 20:45:39 -03001377 ret = uvc_v4l2_get_xu_mapping(&karg.xmap, up);
Andy Lutomirskia44323e2016-05-24 15:13:02 -07001378 if (ret)
1379 return ret;
1380 ret = uvc_ioctl_ctrl_map(handle->chain, &karg.xmap);
1381 if (ret)
1382 return ret;
1383 ret = uvc_v4l2_put_xu_mapping(&karg.xmap, up);
1384 if (ret)
1385 return ret;
1386
Laurent Pinchart1a5e4c82011-12-18 20:45:39 -03001387 break;
1388
1389 case UVCIOC_CTRL_QUERY32:
Laurent Pinchart1a5e4c82011-12-18 20:45:39 -03001390 ret = uvc_v4l2_get_xu_query(&karg.xqry, up);
Andy Lutomirskia44323e2016-05-24 15:13:02 -07001391 if (ret)
1392 return ret;
1393 ret = uvc_xu_ctrl_query(handle->chain, &karg.xqry);
1394 if (ret)
1395 return ret;
1396 ret = uvc_v4l2_put_xu_query(&karg.xqry, up);
1397 if (ret)
1398 return ret;
Laurent Pinchart1a5e4c82011-12-18 20:45:39 -03001399 break;
1400
1401 default:
1402 return -ENOIOCTLCMD;
1403 }
1404
Laurent Pinchart1a5e4c82011-12-18 20:45:39 -03001405 return ret;
1406}
1407#endif
1408
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001409static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1410 size_t count, loff_t *ppos)
1411{
1412 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
Laurent Pinchart350d6402009-11-04 11:53:49 -03001413 return -EINVAL;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001414}
1415
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001416static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1417{
Joe Perchesabf84382010-07-12 17:50:03 -03001418 struct uvc_fh *handle = file->private_data;
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001419 struct uvc_streaming *stream = handle->stream;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001420
1421 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
1422
Laurent Pinchart4aa27592010-11-21 15:18:08 -03001423 return uvc_queue_mmap(&stream->queue, vma);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001424}
1425
Al Viroc23e0cb2017-07-03 03:02:56 -04001426static __poll_t uvc_v4l2_poll(struct file *file, poll_table *wait)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001427{
Joe Perchesabf84382010-07-12 17:50:03 -03001428 struct uvc_fh *handle = file->private_data;
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001429 struct uvc_streaming *stream = handle->stream;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001430
1431 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
1432
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001433 return uvc_queue_poll(&stream->queue, file, wait);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001434}
1435
Bob Liu72969442011-04-29 07:11:35 -03001436#ifndef CONFIG_MMU
1437static unsigned long uvc_v4l2_get_unmapped_area(struct file *file,
1438 unsigned long addr, unsigned long len, unsigned long pgoff,
1439 unsigned long flags)
1440{
1441 struct uvc_fh *handle = file->private_data;
1442 struct uvc_streaming *stream = handle->stream;
1443
1444 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_get_unmapped_area\n");
1445
1446 return uvc_queue_get_unmapped_area(&stream->queue, pgoff);
1447}
1448#endif
1449
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001450const struct v4l2_ioctl_ops uvc_ioctl_ops = {
1451 .vidioc_querycap = uvc_ioctl_querycap,
1452 .vidioc_enum_fmt_vid_cap = uvc_ioctl_enum_fmt_vid_cap,
1453 .vidioc_enum_fmt_vid_out = uvc_ioctl_enum_fmt_vid_out,
1454 .vidioc_g_fmt_vid_cap = uvc_ioctl_g_fmt_vid_cap,
1455 .vidioc_g_fmt_vid_out = uvc_ioctl_g_fmt_vid_out,
1456 .vidioc_s_fmt_vid_cap = uvc_ioctl_s_fmt_vid_cap,
1457 .vidioc_s_fmt_vid_out = uvc_ioctl_s_fmt_vid_out,
1458 .vidioc_try_fmt_vid_cap = uvc_ioctl_try_fmt_vid_cap,
1459 .vidioc_try_fmt_vid_out = uvc_ioctl_try_fmt_vid_out,
1460 .vidioc_reqbufs = uvc_ioctl_reqbufs,
1461 .vidioc_querybuf = uvc_ioctl_querybuf,
1462 .vidioc_qbuf = uvc_ioctl_qbuf,
Laurent Pinchart7195f612015-04-14 04:19:51 -03001463 .vidioc_expbuf = uvc_ioctl_expbuf,
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001464 .vidioc_dqbuf = uvc_ioctl_dqbuf,
1465 .vidioc_create_bufs = uvc_ioctl_create_bufs,
1466 .vidioc_streamon = uvc_ioctl_streamon,
1467 .vidioc_streamoff = uvc_ioctl_streamoff,
1468 .vidioc_enum_input = uvc_ioctl_enum_input,
1469 .vidioc_g_input = uvc_ioctl_g_input,
1470 .vidioc_s_input = uvc_ioctl_s_input,
1471 .vidioc_queryctrl = uvc_ioctl_queryctrl,
Hans Verkuile1832012015-03-14 14:04:00 -03001472 .vidioc_query_ext_ctrl = uvc_ioctl_query_ext_ctrl,
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001473 .vidioc_g_ctrl = uvc_ioctl_g_ctrl,
1474 .vidioc_s_ctrl = uvc_ioctl_s_ctrl,
1475 .vidioc_g_ext_ctrls = uvc_ioctl_g_ext_ctrls,
1476 .vidioc_s_ext_ctrls = uvc_ioctl_s_ext_ctrls,
1477 .vidioc_try_ext_ctrls = uvc_ioctl_try_ext_ctrls,
1478 .vidioc_querymenu = uvc_ioctl_querymenu,
Hans Verkuil1461fe72015-04-03 06:53:42 -03001479 .vidioc_g_selection = uvc_ioctl_g_selection,
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001480 .vidioc_g_parm = uvc_ioctl_g_parm,
1481 .vidioc_s_parm = uvc_ioctl_s_parm,
1482 .vidioc_enum_framesizes = uvc_ioctl_enum_framesizes,
1483 .vidioc_enum_frameintervals = uvc_ioctl_enum_frameintervals,
1484 .vidioc_subscribe_event = uvc_ioctl_subscribe_event,
1485 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1486 .vidioc_default = uvc_ioctl_default,
1487};
1488
Hans Verkuilbec43662008-12-30 06:58:20 -03001489const struct v4l2_file_operations uvc_fops = {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001490 .owner = THIS_MODULE,
1491 .open = uvc_v4l2_open,
1492 .release = uvc_v4l2_release,
Laurent Pinchartd5e90b72010-09-30 10:17:54 -03001493 .unlocked_ioctl = video_ioctl2,
Laurent Pinchart1a5e4c82011-12-18 20:45:39 -03001494#ifdef CONFIG_COMPAT
1495 .compat_ioctl32 = uvc_v4l2_compat_ioctl32,
1496#endif
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001497 .read = uvc_v4l2_read,
1498 .mmap = uvc_v4l2_mmap,
1499 .poll = uvc_v4l2_poll,
Bob Liu72969442011-04-29 07:11:35 -03001500#ifndef CONFIG_MMU
1501 .get_unmapped_area = uvc_v4l2_get_unmapped_area,
1502#endif
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001503};
Hans Verkuilf87086e2008-07-18 00:50:58 -03001504