blob: 759bef8897e9d9d4645d837a451b2b2c96fee92d [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>
16#include <linux/version.h>
17#include <linux/list.h>
18#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Laurent Pinchartc0efd232008-06-30 15:04:50 -030020#include <linux/usb.h>
21#include <linux/videodev2.h>
22#include <linux/vmalloc.h>
23#include <linux/mm.h>
24#include <linux/wait.h>
Arun Sharma600634972011-07-26 16:09:06 -070025#include <linux/atomic.h>
Laurent Pinchartc0efd232008-06-30 15:04:50 -030026
27#include <media/v4l2-common.h>
Hans de Goedeb4012002012-04-08 12:59:51 -030028#include <media/v4l2-ctrls.h>
29#include <media/v4l2-event.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030030#include <media/v4l2-ioctl.h>
Laurent Pinchartc0efd232008-06-30 15:04:50 -030031
32#include "uvcvideo.h"
33
34/* ------------------------------------------------------------------------
Laurent Pinchart561474c22010-02-18 16:38:52 -030035 * UVC ioctls
36 */
Laurent Pinchartba2fa992010-09-20 05:53:21 -030037static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain,
Laurent Pinchart227bd5b2011-07-30 16:19:49 -030038 struct uvc_xu_control_mapping *xmap)
Laurent Pinchart561474c22010-02-18 16:38:52 -030039{
40 struct uvc_control_mapping *map;
41 unsigned int size;
42 int ret;
43
44 map = kzalloc(sizeof *map, GFP_KERNEL);
45 if (map == NULL)
46 return -ENOMEM;
47
48 map->id = xmap->id;
49 memcpy(map->name, xmap->name, sizeof map->name);
50 memcpy(map->entity, xmap->entity, sizeof map->entity);
51 map->selector = xmap->selector;
52 map->size = xmap->size;
53 map->offset = xmap->offset;
54 map->v4l2_type = xmap->v4l2_type;
55 map->data_type = xmap->data_type;
56
57 switch (xmap->v4l2_type) {
58 case V4L2_CTRL_TYPE_INTEGER:
59 case V4L2_CTRL_TYPE_BOOLEAN:
60 case V4L2_CTRL_TYPE_BUTTON:
61 break;
62
63 case V4L2_CTRL_TYPE_MENU:
Haogang Chen806e23e2011-11-29 18:32:25 -030064 /* Prevent excessive memory consumption, as well as integer
65 * overflows.
66 */
67 if (xmap->menu_count == 0 ||
68 xmap->menu_count > UVC_MAX_CONTROL_MENU_ENTRIES) {
69 ret = -EINVAL;
70 goto done;
71 }
72
Laurent Pinchart561474c22010-02-18 16:38:52 -030073 size = xmap->menu_count * sizeof(*map->menu_info);
74 map->menu_info = kmalloc(size, GFP_KERNEL);
75 if (map->menu_info == NULL) {
76 ret = -ENOMEM;
77 goto done;
78 }
79
80 if (copy_from_user(map->menu_info, xmap->menu_info, size)) {
81 ret = -EFAULT;
82 goto done;
83 }
84
85 map->menu_count = xmap->menu_count;
86 break;
87
88 default:
Laurent Pinchartba2fa992010-09-20 05:53:21 -030089 uvc_trace(UVC_TRACE_CONTROL, "Unsupported V4L2 control type "
90 "%u.\n", xmap->v4l2_type);
Mauro Carvalho Chehab7a286cc2011-06-26 10:18:03 -030091 ret = -ENOTTY;
Laurent Pinchart561474c22010-02-18 16:38:52 -030092 goto done;
93 }
94
Laurent Pinchartba2fa992010-09-20 05:53:21 -030095 ret = uvc_ctrl_add_mapping(chain, map);
Laurent Pinchart561474c22010-02-18 16:38:52 -030096
97done:
Laurent Pinchartba2fa992010-09-20 05:53:21 -030098 kfree(map->menu_info);
99 kfree(map);
Laurent Pinchart561474c22010-02-18 16:38:52 -0300100
101 return ret;
102}
103
104/* ------------------------------------------------------------------------
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300105 * V4L2 interface
106 */
107
108/*
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300109 * Find the frame interval closest to the requested frame interval for the
110 * given frame format and size. This should be done by the device as part of
111 * the Video Probe and Commit negotiation, but some hardware don't implement
112 * that feature.
113 */
114static __u32 uvc_try_frame_interval(struct uvc_frame *frame, __u32 interval)
115{
116 unsigned int i;
117
118 if (frame->bFrameIntervalType) {
119 __u32 best = -1, dist;
120
121 for (i = 0; i < frame->bFrameIntervalType; ++i) {
122 dist = interval > frame->dwFrameInterval[i]
123 ? interval - frame->dwFrameInterval[i]
124 : frame->dwFrameInterval[i] - interval;
125
126 if (dist > best)
127 break;
128
129 best = dist;
130 }
131
132 interval = frame->dwFrameInterval[i-1];
133 } else {
134 const __u32 min = frame->dwFrameInterval[0];
135 const __u32 max = frame->dwFrameInterval[1];
136 const __u32 step = frame->dwFrameInterval[2];
137
138 interval = min + (interval - min + step/2) / step * step;
139 if (interval > max)
140 interval = max;
141 }
142
143 return interval;
144}
145
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300146static int uvc_v4l2_try_format(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300147 struct v4l2_format *fmt, struct uvc_streaming_control *probe,
148 struct uvc_format **uvc_format, struct uvc_frame **uvc_frame)
149{
150 struct uvc_format *format = NULL;
151 struct uvc_frame *frame = NULL;
152 __u16 rw, rh;
153 unsigned int d, maxd;
154 unsigned int i;
155 __u32 interval;
156 int ret = 0;
157 __u8 *fcc;
158
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300159 if (fmt->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300160 return -EINVAL;
161
162 fcc = (__u8 *)&fmt->fmt.pix.pixelformat;
163 uvc_trace(UVC_TRACE_FORMAT, "Trying format 0x%08x (%c%c%c%c): %ux%u.\n",
164 fmt->fmt.pix.pixelformat,
165 fcc[0], fcc[1], fcc[2], fcc[3],
166 fmt->fmt.pix.width, fmt->fmt.pix.height);
167
168 /* Check if the hardware supports the requested format. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300169 for (i = 0; i < stream->nformats; ++i) {
170 format = &stream->format[i];
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300171 if (format->fcc == fmt->fmt.pix.pixelformat)
172 break;
173 }
174
175 if (format == NULL || format->fcc != fmt->fmt.pix.pixelformat) {
176 uvc_trace(UVC_TRACE_FORMAT, "Unsupported format 0x%08x.\n",
177 fmt->fmt.pix.pixelformat);
178 return -EINVAL;
179 }
180
181 /* Find the closest image size. The distance between image sizes is
182 * the size in pixels of the non-overlapping regions between the
183 * requested size and the frame-specified size.
184 */
185 rw = fmt->fmt.pix.width;
186 rh = fmt->fmt.pix.height;
187 maxd = (unsigned int)-1;
188
189 for (i = 0; i < format->nframes; ++i) {
190 __u16 w = format->frame[i].wWidth;
191 __u16 h = format->frame[i].wHeight;
192
193 d = min(w, rw) * min(h, rh);
194 d = w*h + rw*rh - 2*d;
195 if (d < maxd) {
196 maxd = d;
197 frame = &format->frame[i];
198 }
199
200 if (maxd == 0)
201 break;
202 }
203
204 if (frame == NULL) {
205 uvc_trace(UVC_TRACE_FORMAT, "Unsupported size %ux%u.\n",
206 fmt->fmt.pix.width, fmt->fmt.pix.height);
207 return -EINVAL;
208 }
209
210 /* Use the default frame interval. */
211 interval = frame->dwDefaultFrameInterval;
212 uvc_trace(UVC_TRACE_FORMAT, "Using default frame interval %u.%u us "
213 "(%u.%u fps).\n", interval/10, interval%10, 10000000/interval,
214 (100000000/interval)%10);
215
216 /* Set the format index, frame index and frame interval. */
217 memset(probe, 0, sizeof *probe);
218 probe->bmHint = 1; /* dwFrameInterval */
219 probe->bFormatIndex = format->index;
220 probe->bFrameIndex = frame->bFrameIndex;
221 probe->dwFrameInterval = uvc_try_frame_interval(frame, interval);
222 /* Some webcams stall the probe control set request when the
223 * dwMaxVideoFrameSize field is set to zero. The UVC specification
224 * clearly states that the field is read-only from the host, so this
225 * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by
226 * the webcam to work around the problem.
227 *
228 * The workaround could probably be enabled for all webcams, so the
229 * quirk can be removed if needed. It's currently useful to detect
230 * webcam bugs and fix them before they hit the market (providing
231 * developers test their webcams with the Linux driver as well as with
232 * the Windows driver).
233 */
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
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300317 memcpy(&stream->ctrl, &probe, sizeof probe);
318 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
389 memcpy(&probe, &stream->ctrl, sizeof probe);
390 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
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300400 memcpy(&stream->ctrl, &probe, sizeof 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 Pinchart35f02a62009-06-28 08:37:50 -0300500 if (atomic_inc_return(&stream->dev->users) == 1) {
501 ret = uvc_status_start(stream->dev);
502 if (ret < 0) {
503 usb_autopm_put_interface(stream->dev->intf);
504 atomic_dec(&stream->dev->users);
Laurent Pinchart04a37e02009-05-19 10:08:03 -0300505 kfree(handle);
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300506 return ret;
Laurent Pinchart04a37e02009-05-19 10:08:03 -0300507 }
508 }
509
Hans de Goedeb4012002012-04-08 12:59:51 -0300510 v4l2_fh_init(&handle->vfh, stream->vdev);
511 v4l2_fh_add(&handle->vfh);
Laurent Pinchart8e113592009-07-01 20:24:47 -0300512 handle->chain = stream->chain;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300513 handle->stream = stream;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300514 handle->state = UVC_HANDLE_PASSIVE;
515 file->private_data = handle;
516
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300517 return 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300518}
519
Hans Verkuilbec43662008-12-30 06:58:20 -0300520static int uvc_v4l2_release(struct file *file)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300521{
Joe Perchesabf84382010-07-12 17:50:03 -0300522 struct uvc_fh *handle = file->private_data;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300523 struct uvc_streaming *stream = handle->stream;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300524
525 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");
526
527 /* Only free resources if this is a privileged handle. */
528 if (uvc_has_privileges(handle)) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300529 uvc_video_enable(stream, 0);
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300530 uvc_free_buffers(&stream->queue);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300531 }
532
533 /* Release the file handle. */
534 uvc_dismiss_privileges(handle);
Hans de Goedeb4012002012-04-08 12:59:51 -0300535 v4l2_fh_del(&handle->vfh);
536 v4l2_fh_exit(&handle->vfh);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300537 kfree(handle);
538 file->private_data = NULL;
539
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300540 if (atomic_dec_return(&stream->dev->users) == 0)
541 uvc_status_stop(stream->dev);
Laurent Pinchart04a37e02009-05-19 10:08:03 -0300542
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300543 usb_autopm_put_interface(stream->dev->intf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300544 return 0;
545}
546
Hans Verkuil069b7472008-12-30 07:04:34 -0300547static long uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300548{
549 struct video_device *vdev = video_devdata(file);
Joe Perchesabf84382010-07-12 17:50:03 -0300550 struct uvc_fh *handle = file->private_data;
Laurent Pinchart8e113592009-07-01 20:24:47 -0300551 struct uvc_video_chain *chain = handle->chain;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300552 struct uvc_streaming *stream = handle->stream;
Hans Verkuil069b7472008-12-30 07:04:34 -0300553 long ret = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300554
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300555 switch (cmd) {
556 /* Query capabilities */
557 case VIDIOC_QUERYCAP:
558 {
559 struct v4l2_capability *cap = arg;
560
561 memset(cap, 0, sizeof *cap);
Laurent Pinchartd0ebf302009-01-08 14:05:11 -0300562 strlcpy(cap->driver, "uvcvideo", sizeof cap->driver);
563 strlcpy(cap->card, vdev->name, sizeof cap->card);
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300564 usb_make_path(stream->dev->udev,
Thierry MERLEb12049a2009-01-27 16:53:23 -0300565 cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehabfd3e5822011-06-25 13:50:37 -0300566 cap->version = LINUX_VERSION_CODE;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300567 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
Laurent Pinchartff924202008-12-28 22:32:29 -0300568 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
569 | V4L2_CAP_STREAMING;
570 else
571 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT
572 | V4L2_CAP_STREAMING;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300573 break;
574 }
575
576 /* Get, Set & Query control */
577 case VIDIOC_QUERYCTRL:
Laurent Pinchart8e113592009-07-01 20:24:47 -0300578 return uvc_query_v4l2_ctrl(chain, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300579
580 case VIDIOC_G_CTRL:
581 {
582 struct v4l2_control *ctrl = arg;
583 struct v4l2_ext_control xctrl;
584
585 memset(&xctrl, 0, sizeof xctrl);
586 xctrl.id = ctrl->id;
587
Laurent Pinchart8e113592009-07-01 20:24:47 -0300588 ret = uvc_ctrl_begin(chain);
589 if (ret < 0)
Robert Krakora1fcbcc42009-06-12 13:51:03 -0300590 return ret;
591
Laurent Pinchart8e113592009-07-01 20:24:47 -0300592 ret = uvc_ctrl_get(chain, &xctrl);
Hans de Goedeb4012002012-04-08 12:59:51 -0300593 uvc_ctrl_rollback(handle);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300594 if (ret >= 0)
595 ctrl->value = xctrl.value;
596 break;
597 }
598
599 case VIDIOC_S_CTRL:
600 {
601 struct v4l2_control *ctrl = arg;
602 struct v4l2_ext_control xctrl;
603
604 memset(&xctrl, 0, sizeof xctrl);
605 xctrl.id = ctrl->id;
606 xctrl.value = ctrl->value;
607
Laurent Pinchart8d556622010-02-04 21:43:37 -0300608 ret = uvc_ctrl_begin(chain);
Laurent Pinchart8e113592009-07-01 20:24:47 -0300609 if (ret < 0)
Robert Krakora1fcbcc42009-06-12 13:51:03 -0300610 return ret;
611
Laurent Pinchart8e113592009-07-01 20:24:47 -0300612 ret = uvc_ctrl_set(chain, &xctrl);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300613 if (ret < 0) {
Hans de Goedeb4012002012-04-08 12:59:51 -0300614 uvc_ctrl_rollback(handle);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300615 return ret;
616 }
Hans de Goedeb4012002012-04-08 12:59:51 -0300617 ret = uvc_ctrl_commit(handle, &xctrl, 1);
Laurent Pincharte54532e2010-01-23 07:07:53 -0300618 if (ret == 0)
619 ctrl->value = xctrl.value;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300620 break;
621 }
622
623 case VIDIOC_QUERYMENU:
Laurent Pinchart23d9f3e2010-11-21 07:58:54 -0300624 return uvc_query_v4l2_menu(chain, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300625
626 case VIDIOC_G_EXT_CTRLS:
627 {
628 struct v4l2_ext_controls *ctrls = arg;
629 struct v4l2_ext_control *ctrl = ctrls->controls;
630 unsigned int i;
631
Laurent Pinchart8e113592009-07-01 20:24:47 -0300632 ret = uvc_ctrl_begin(chain);
633 if (ret < 0)
Robert Krakora1fcbcc42009-06-12 13:51:03 -0300634 return ret;
635
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300636 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300637 ret = uvc_ctrl_get(chain, ctrl);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300638 if (ret < 0) {
Hans de Goedeb4012002012-04-08 12:59:51 -0300639 uvc_ctrl_rollback(handle);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300640 ctrls->error_idx = i;
641 return ret;
642 }
643 }
644 ctrls->error_idx = 0;
Hans de Goedeb4012002012-04-08 12:59:51 -0300645 ret = uvc_ctrl_rollback(handle);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300646 break;
647 }
648
649 case VIDIOC_S_EXT_CTRLS:
650 case VIDIOC_TRY_EXT_CTRLS:
651 {
652 struct v4l2_ext_controls *ctrls = arg;
653 struct v4l2_ext_control *ctrl = ctrls->controls;
654 unsigned int i;
655
Laurent Pinchart8e113592009-07-01 20:24:47 -0300656 ret = uvc_ctrl_begin(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300657 if (ret < 0)
658 return ret;
659
660 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300661 ret = uvc_ctrl_set(chain, ctrl);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300662 if (ret < 0) {
Hans de Goedeb4012002012-04-08 12:59:51 -0300663 uvc_ctrl_rollback(handle);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300664 ctrls->error_idx = i;
665 return ret;
666 }
667 }
668
669 ctrls->error_idx = 0;
670
671 if (cmd == VIDIOC_S_EXT_CTRLS)
Hans de Goedeb4012002012-04-08 12:59:51 -0300672 ret = uvc_ctrl_commit(handle,
673 ctrls->controls, ctrls->count);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300674 else
Hans de Goedeb4012002012-04-08 12:59:51 -0300675 ret = uvc_ctrl_rollback(handle);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300676 break;
677 }
678
679 /* Get, Set & Enum input */
680 case VIDIOC_ENUMINPUT:
681 {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300682 const struct uvc_entity *selector = chain->selector;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300683 struct v4l2_input *input = arg;
684 struct uvc_entity *iterm = NULL;
685 u32 index = input->index;
686 int pin = 0;
687
688 if (selector == NULL ||
Laurent Pinchart8e113592009-07-01 20:24:47 -0300689 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300690 if (index != 0)
691 return -EINVAL;
Laurent Pinchart6241d8c2009-11-25 12:00:22 -0300692 list_for_each_entry(iterm, &chain->entities, chain) {
693 if (UVC_ENTITY_IS_ITERM(iterm))
694 break;
695 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300696 pin = iterm->id;
Laurent Pinchart31c5f0c2012-03-21 09:50:36 -0300697 } else if (index < selector->bNrInPins) {
Laurent Pinchart8ca5a632009-11-25 12:00:30 -0300698 pin = selector->baSourceID[index];
Laurent Pinchart6241d8c2009-11-25 12:00:22 -0300699 list_for_each_entry(iterm, &chain->entities, chain) {
700 if (!UVC_ENTITY_IS_ITERM(iterm))
701 continue;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300702 if (iterm->id == pin)
703 break;
704 }
705 }
706
707 if (iterm == NULL || iterm->id != pin)
708 return -EINVAL;
709
710 memset(input, 0, sizeof *input);
711 input->index = index;
Laurent Pinchartd0ebf302009-01-08 14:05:11 -0300712 strlcpy(input->name, iterm->name, sizeof input->name);
Laurent Pinchartb482d922009-06-26 11:39:42 -0300713 if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300714 input->type = V4L2_INPUT_TYPE_CAMERA;
715 break;
716 }
717
718 case VIDIOC_G_INPUT:
719 {
720 u8 input;
721
Laurent Pinchart8e113592009-07-01 20:24:47 -0300722 if (chain->selector == NULL ||
723 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300724 *(int *)arg = 0;
725 break;
726 }
727
Laurent Pinchart8e113592009-07-01 20:24:47 -0300728 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
729 chain->selector->id, chain->dev->intfnum,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300730 UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300731 if (ret < 0)
732 return ret;
733
734 *(int *)arg = input - 1;
735 break;
736 }
737
738 case VIDIOC_S_INPUT:
739 {
Márton Németh9086c7b992009-04-15 09:01:58 -0300740 u32 input = *(u32 *)arg + 1;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300741
742 if ((ret = uvc_acquire_privileges(handle)) < 0)
743 return ret;
744
Laurent Pinchart8e113592009-07-01 20:24:47 -0300745 if (chain->selector == NULL ||
746 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300747 if (input != 1)
748 return -EINVAL;
749 break;
750 }
751
Laurent Pinchart8ca5a632009-11-25 12:00:30 -0300752 if (input == 0 || input > chain->selector->bNrInPins)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300753 return -EINVAL;
754
Laurent Pinchart8e113592009-07-01 20:24:47 -0300755 return uvc_query_ctrl(chain->dev, UVC_SET_CUR,
756 chain->selector->id, chain->dev->intfnum,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300757 UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300758 }
759
760 /* Try, Get, Set & Enum format */
761 case VIDIOC_ENUM_FMT:
762 {
763 struct v4l2_fmtdesc *fmt = arg;
764 struct uvc_format *format;
Márton Németh8bbd90c2009-03-27 11:13:57 -0300765 enum v4l2_buf_type type = fmt->type;
766 __u32 index = fmt->index;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300767
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300768 if (fmt->type != stream->type ||
769 fmt->index >= stream->nformats)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300770 return -EINVAL;
771
Márton Németh8bbd90c2009-03-27 11:13:57 -0300772 memset(fmt, 0, sizeof(*fmt));
773 fmt->index = index;
774 fmt->type = type;
775
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300776 format = &stream->format[fmt->index];
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300777 fmt->flags = 0;
778 if (format->flags & UVC_FMT_FLAG_COMPRESSED)
779 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
Laurent Pinchartd0ebf302009-01-08 14:05:11 -0300780 strlcpy(fmt->description, format->name,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300781 sizeof fmt->description);
782 fmt->description[sizeof fmt->description - 1] = 0;
783 fmt->pixelformat = format->fcc;
784 break;
785 }
786
787 case VIDIOC_TRY_FMT:
788 {
789 struct uvc_streaming_control probe;
790
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300791 return uvc_v4l2_try_format(stream, arg, &probe, NULL, NULL);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300792 }
793
794 case VIDIOC_S_FMT:
795 if ((ret = uvc_acquire_privileges(handle)) < 0)
796 return ret;
797
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300798 return uvc_v4l2_set_format(stream, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300799
800 case VIDIOC_G_FMT:
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300801 return uvc_v4l2_get_format(stream, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300802
803 /* Frame size enumeration */
804 case VIDIOC_ENUM_FRAMESIZES:
805 {
806 struct v4l2_frmsizeenum *fsize = arg;
807 struct uvc_format *format = NULL;
808 struct uvc_frame *frame;
809 int i;
810
811 /* Look for the given pixel format */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300812 for (i = 0; i < stream->nformats; i++) {
813 if (stream->format[i].fcc ==
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300814 fsize->pixel_format) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300815 format = &stream->format[i];
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300816 break;
817 }
818 }
819 if (format == NULL)
820 return -EINVAL;
821
822 if (fsize->index >= format->nframes)
823 return -EINVAL;
824
825 frame = &format->frame[fsize->index];
826 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
827 fsize->discrete.width = frame->wWidth;
828 fsize->discrete.height = frame->wHeight;
829 break;
830 }
831
832 /* Frame interval enumeration */
833 case VIDIOC_ENUM_FRAMEINTERVALS:
834 {
835 struct v4l2_frmivalenum *fival = arg;
836 struct uvc_format *format = NULL;
837 struct uvc_frame *frame = NULL;
838 int i;
839
840 /* Look for the given pixel format and frame size */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300841 for (i = 0; i < stream->nformats; i++) {
842 if (stream->format[i].fcc ==
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300843 fival->pixel_format) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300844 format = &stream->format[i];
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300845 break;
846 }
847 }
848 if (format == NULL)
849 return -EINVAL;
850
851 for (i = 0; i < format->nframes; i++) {
852 if (format->frame[i].wWidth == fival->width &&
853 format->frame[i].wHeight == fival->height) {
854 frame = &format->frame[i];
855 break;
856 }
857 }
858 if (frame == NULL)
859 return -EINVAL;
860
861 if (frame->bFrameIntervalType) {
862 if (fival->index >= frame->bFrameIntervalType)
863 return -EINVAL;
864
865 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
866 fival->discrete.numerator =
867 frame->dwFrameInterval[fival->index];
868 fival->discrete.denominator = 10000000;
869 uvc_simplify_fraction(&fival->discrete.numerator,
870 &fival->discrete.denominator, 8, 333);
871 } else {
872 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
873 fival->stepwise.min.numerator =
874 frame->dwFrameInterval[0];
875 fival->stepwise.min.denominator = 10000000;
876 fival->stepwise.max.numerator =
877 frame->dwFrameInterval[1];
878 fival->stepwise.max.denominator = 10000000;
879 fival->stepwise.step.numerator =
880 frame->dwFrameInterval[2];
881 fival->stepwise.step.denominator = 10000000;
882 uvc_simplify_fraction(&fival->stepwise.min.numerator,
883 &fival->stepwise.min.denominator, 8, 333);
884 uvc_simplify_fraction(&fival->stepwise.max.numerator,
885 &fival->stepwise.max.denominator, 8, 333);
886 uvc_simplify_fraction(&fival->stepwise.step.numerator,
887 &fival->stepwise.step.denominator, 8, 333);
888 }
889 break;
890 }
891
892 /* Get & Set streaming parameters */
893 case VIDIOC_G_PARM:
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300894 return uvc_v4l2_get_streamparm(stream, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300895
896 case VIDIOC_S_PARM:
897 if ((ret = uvc_acquire_privileges(handle)) < 0)
898 return ret;
899
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300900 return uvc_v4l2_set_streamparm(stream, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300901
902 /* Cropping and scaling */
903 case VIDIOC_CROPCAP:
904 {
905 struct v4l2_cropcap *ccap = arg;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300906
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300907 if (ccap->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300908 return -EINVAL;
909
910 ccap->bounds.left = 0;
911 ccap->bounds.top = 0;
Laurent Pinchart69477562010-11-21 13:36:34 -0300912
913 mutex_lock(&stream->mutex);
914 ccap->bounds.width = stream->cur_frame->wWidth;
915 ccap->bounds.height = stream->cur_frame->wHeight;
916 mutex_unlock(&stream->mutex);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300917
918 ccap->defrect = ccap->bounds;
919
920 ccap->pixelaspect.numerator = 1;
921 ccap->pixelaspect.denominator = 1;
922 break;
923 }
924
925 case VIDIOC_G_CROP:
926 case VIDIOC_S_CROP:
927 return -EINVAL;
928
929 /* Buffers & streaming */
930 case VIDIOC_REQBUFS:
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300931 if ((ret = uvc_acquire_privileges(handle)) < 0)
932 return ret;
933
Laurent Pinchart69477562010-11-21 13:36:34 -0300934 mutex_lock(&stream->mutex);
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300935 ret = uvc_alloc_buffers(&stream->queue, arg);
Laurent Pinchart69477562010-11-21 13:36:34 -0300936 mutex_unlock(&stream->mutex);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300937 if (ret < 0)
938 return ret;
939
Laurent Pinchart1a969d92009-09-02 03:12:26 -0300940 if (ret == 0)
941 uvc_dismiss_privileges(handle);
942
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300943 ret = 0;
944 break;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300945
946 case VIDIOC_QUERYBUF:
947 {
948 struct v4l2_buffer *buf = arg;
949
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300950 if (!uvc_has_privileges(handle))
951 return -EBUSY;
952
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300953 return uvc_query_buffer(&stream->queue, buf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300954 }
955
956 case VIDIOC_QBUF:
957 if (!uvc_has_privileges(handle))
958 return -EBUSY;
959
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300960 return uvc_queue_buffer(&stream->queue, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300961
962 case VIDIOC_DQBUF:
963 if (!uvc_has_privileges(handle))
964 return -EBUSY;
965
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300966 return uvc_dequeue_buffer(&stream->queue, arg,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300967 file->f_flags & O_NONBLOCK);
968
969 case VIDIOC_STREAMON:
970 {
971 int *type = arg;
972
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300973 if (*type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300974 return -EINVAL;
975
976 if (!uvc_has_privileges(handle))
977 return -EBUSY;
978
Laurent Pinchart69477562010-11-21 13:36:34 -0300979 mutex_lock(&stream->mutex);
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300980 ret = uvc_video_enable(stream, 1);
Laurent Pinchart69477562010-11-21 13:36:34 -0300981 mutex_unlock(&stream->mutex);
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300982 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300983 return ret;
984 break;
985 }
986
987 case VIDIOC_STREAMOFF:
988 {
989 int *type = arg;
990
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300991 if (*type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300992 return -EINVAL;
993
994 if (!uvc_has_privileges(handle))
995 return -EBUSY;
996
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300997 return uvc_video_enable(stream, 0);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300998 }
999
Hans de Goedeb4012002012-04-08 12:59:51 -03001000 case VIDIOC_SUBSCRIBE_EVENT:
1001 {
1002 struct v4l2_event_subscription *sub = arg;
1003
1004 switch (sub->type) {
1005 case V4L2_EVENT_CTRL:
1006 return v4l2_event_subscribe(&handle->vfh, sub, 0,
1007 &uvc_ctrl_sub_ev_ops);
1008 default:
1009 return -EINVAL;
1010 }
1011 }
1012
1013 case VIDIOC_UNSUBSCRIBE_EVENT:
1014 return v4l2_event_unsubscribe(&handle->vfh, arg);
1015
1016 case VIDIOC_DQEVENT:
1017 return v4l2_event_dequeue(&handle->vfh, arg,
1018 file->f_flags & O_NONBLOCK);
1019
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001020 /* Analog video standards make no sense for digital cameras. */
1021 case VIDIOC_ENUMSTD:
1022 case VIDIOC_QUERYSTD:
1023 case VIDIOC_G_STD:
1024 case VIDIOC_S_STD:
1025
1026 case VIDIOC_OVERLAY:
1027
1028 case VIDIOC_ENUMAUDIO:
1029 case VIDIOC_ENUMAUDOUT:
1030
1031 case VIDIOC_ENUMOUTPUT:
1032 uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd);
1033 return -EINVAL;
1034
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001035 case UVCIOC_CTRL_MAP:
Laurent Pinchart227bd5b2011-07-30 16:19:49 -03001036 return uvc_ioctl_ctrl_map(chain, arg);
Martin Rublife78d182010-10-02 19:10:16 -03001037
1038 case UVCIOC_CTRL_QUERY:
1039 return uvc_xu_ctrl_query(chain, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001040
1041 default:
Hans Verkuil08af2452010-12-24 10:33:19 -03001042 uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n", cmd);
Laurent Pincharta3ec69b2011-12-18 20:22:50 -03001043 return -ENOTTY;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001044 }
1045
1046 return ret;
1047}
1048
Hans Verkuil069b7472008-12-30 07:04:34 -03001049static long uvc_v4l2_ioctl(struct file *file,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001050 unsigned int cmd, unsigned long arg)
1051{
Laurent Pinchart308bb522008-11-17 18:32:11 -03001052 if (uvc_trace_param & UVC_TRACE_IOCTL) {
1053 uvc_printk(KERN_DEBUG, "uvc_v4l2_ioctl(");
1054 v4l_printk_ioctl(cmd);
1055 printk(")\n");
1056 }
1057
Hans Verkuilf473bf72008-11-01 08:25:11 -03001058 return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001059}
1060
Laurent Pinchart1a5e4c82011-12-18 20:45:39 -03001061#ifdef CONFIG_COMPAT
1062struct uvc_xu_control_mapping32 {
1063 __u32 id;
1064 __u8 name[32];
1065 __u8 entity[16];
1066 __u8 selector;
1067
1068 __u8 size;
1069 __u8 offset;
1070 __u32 v4l2_type;
1071 __u32 data_type;
1072
1073 compat_caddr_t menu_info;
1074 __u32 menu_count;
1075
1076 __u32 reserved[4];
1077};
1078
1079static int uvc_v4l2_get_xu_mapping(struct uvc_xu_control_mapping *kp,
1080 const struct uvc_xu_control_mapping32 __user *up)
1081{
1082 struct uvc_menu_info __user *umenus;
1083 struct uvc_menu_info __user *kmenus;
1084 compat_caddr_t p;
1085
1086 if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
1087 __copy_from_user(kp, up, offsetof(typeof(*up), menu_info)) ||
1088 __get_user(kp->menu_count, &up->menu_count))
1089 return -EFAULT;
1090
1091 memset(kp->reserved, 0, sizeof(kp->reserved));
1092
1093 if (kp->menu_count == 0) {
1094 kp->menu_info = NULL;
1095 return 0;
1096 }
1097
1098 if (__get_user(p, &up->menu_info))
1099 return -EFAULT;
1100 umenus = compat_ptr(p);
1101 if (!access_ok(VERIFY_READ, umenus, kp->menu_count * sizeof(*umenus)))
1102 return -EFAULT;
1103
1104 kmenus = compat_alloc_user_space(kp->menu_count * sizeof(*kmenus));
1105 if (kmenus == NULL)
1106 return -EFAULT;
1107 kp->menu_info = kmenus;
1108
1109 if (copy_in_user(kmenus, umenus, kp->menu_count * sizeof(*umenus)))
1110 return -EFAULT;
1111
1112 return 0;
1113}
1114
1115static int uvc_v4l2_put_xu_mapping(const struct uvc_xu_control_mapping *kp,
1116 struct uvc_xu_control_mapping32 __user *up)
1117{
1118 struct uvc_menu_info __user *umenus;
1119 struct uvc_menu_info __user *kmenus = kp->menu_info;
1120 compat_caddr_t p;
1121
1122 if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
1123 __copy_to_user(up, kp, offsetof(typeof(*up), menu_info)) ||
1124 __put_user(kp->menu_count, &up->menu_count))
1125 return -EFAULT;
1126
Hans de Goede57fb4a42012-04-08 12:59:48 -03001127 if (__clear_user(up->reserved, sizeof(up->reserved)))
1128 return -EFAULT;
Laurent Pinchart1a5e4c82011-12-18 20:45:39 -03001129
1130 if (kp->menu_count == 0)
1131 return 0;
1132
1133 if (get_user(p, &up->menu_info))
1134 return -EFAULT;
1135 umenus = compat_ptr(p);
Laurent Pinchart1a5e4c82011-12-18 20:45:39 -03001136
1137 if (copy_in_user(umenus, kmenus, kp->menu_count * sizeof(*umenus)))
1138 return -EFAULT;
1139
1140 return 0;
1141}
1142
1143struct uvc_xu_control_query32 {
1144 __u8 unit;
1145 __u8 selector;
1146 __u8 query;
1147 __u16 size;
1148 compat_caddr_t data;
1149};
1150
1151static int uvc_v4l2_get_xu_query(struct uvc_xu_control_query *kp,
1152 const struct uvc_xu_control_query32 __user *up)
1153{
1154 u8 __user *udata;
1155 u8 __user *kdata;
1156 compat_caddr_t p;
1157
1158 if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
1159 __copy_from_user(kp, up, offsetof(typeof(*up), data)))
1160 return -EFAULT;
1161
1162 if (kp->size == 0) {
1163 kp->data = NULL;
1164 return 0;
1165 }
1166
1167 if (__get_user(p, &up->data))
1168 return -EFAULT;
1169 udata = compat_ptr(p);
1170 if (!access_ok(VERIFY_READ, udata, kp->size))
1171 return -EFAULT;
1172
1173 kdata = compat_alloc_user_space(kp->size);
1174 if (kdata == NULL)
1175 return -EFAULT;
1176 kp->data = kdata;
1177
1178 if (copy_in_user(kdata, udata, kp->size))
1179 return -EFAULT;
1180
1181 return 0;
1182}
1183
1184static int uvc_v4l2_put_xu_query(const struct uvc_xu_control_query *kp,
1185 struct uvc_xu_control_query32 __user *up)
1186{
1187 u8 __user *udata;
1188 u8 __user *kdata = kp->data;
1189 compat_caddr_t p;
1190
1191 if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
1192 __copy_to_user(up, kp, offsetof(typeof(*up), data)))
1193 return -EFAULT;
1194
1195 if (kp->size == 0)
1196 return 0;
1197
1198 if (get_user(p, &up->data))
1199 return -EFAULT;
1200 udata = compat_ptr(p);
1201 if (!access_ok(VERIFY_READ, udata, kp->size))
1202 return -EFAULT;
1203
1204 if (copy_in_user(udata, kdata, kp->size))
1205 return -EFAULT;
1206
1207 return 0;
1208}
1209
1210#define UVCIOC_CTRL_MAP32 _IOWR('u', 0x20, struct uvc_xu_control_mapping32)
1211#define UVCIOC_CTRL_QUERY32 _IOWR('u', 0x21, struct uvc_xu_control_query32)
1212
1213static long uvc_v4l2_compat_ioctl32(struct file *file,
1214 unsigned int cmd, unsigned long arg)
1215{
1216 union {
1217 struct uvc_xu_control_mapping xmap;
1218 struct uvc_xu_control_query xqry;
1219 } karg;
1220 void __user *up = compat_ptr(arg);
1221 mm_segment_t old_fs;
1222 long ret;
1223
1224 switch (cmd) {
1225 case UVCIOC_CTRL_MAP32:
1226 cmd = UVCIOC_CTRL_MAP;
1227 ret = uvc_v4l2_get_xu_mapping(&karg.xmap, up);
1228 break;
1229
1230 case UVCIOC_CTRL_QUERY32:
1231 cmd = UVCIOC_CTRL_QUERY;
1232 ret = uvc_v4l2_get_xu_query(&karg.xqry, up);
1233 break;
1234
1235 default:
1236 return -ENOIOCTLCMD;
1237 }
1238
1239 old_fs = get_fs();
1240 set_fs(KERNEL_DS);
1241 ret = uvc_v4l2_ioctl(file, cmd, (unsigned long)&karg);
1242 set_fs(old_fs);
1243
1244 if (ret < 0)
1245 return ret;
1246
1247 switch (cmd) {
1248 case UVCIOC_CTRL_MAP:
1249 ret = uvc_v4l2_put_xu_mapping(&karg.xmap, up);
1250 break;
1251
1252 case UVCIOC_CTRL_QUERY:
1253 ret = uvc_v4l2_put_xu_query(&karg.xqry, up);
1254 break;
1255 }
1256
1257 return ret;
1258}
1259#endif
1260
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001261static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1262 size_t count, loff_t *ppos)
1263{
1264 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
Laurent Pinchart350d6402009-11-04 11:53:49 -03001265 return -EINVAL;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001266}
1267
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001268static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1269{
Joe Perchesabf84382010-07-12 17:50:03 -03001270 struct uvc_fh *handle = file->private_data;
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001271 struct uvc_streaming *stream = handle->stream;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001272
1273 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
1274
Laurent Pinchart4aa27592010-11-21 15:18:08 -03001275 return uvc_queue_mmap(&stream->queue, vma);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001276}
1277
1278static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
1279{
Joe Perchesabf84382010-07-12 17:50:03 -03001280 struct uvc_fh *handle = file->private_data;
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001281 struct uvc_streaming *stream = handle->stream;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001282
1283 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
1284
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001285 return uvc_queue_poll(&stream->queue, file, wait);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001286}
1287
Bob Liu72969442011-04-29 07:11:35 -03001288#ifndef CONFIG_MMU
1289static unsigned long uvc_v4l2_get_unmapped_area(struct file *file,
1290 unsigned long addr, unsigned long len, unsigned long pgoff,
1291 unsigned long flags)
1292{
1293 struct uvc_fh *handle = file->private_data;
1294 struct uvc_streaming *stream = handle->stream;
1295
1296 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_get_unmapped_area\n");
1297
1298 return uvc_queue_get_unmapped_area(&stream->queue, pgoff);
1299}
1300#endif
1301
Hans Verkuilbec43662008-12-30 06:58:20 -03001302const struct v4l2_file_operations uvc_fops = {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001303 .owner = THIS_MODULE,
1304 .open = uvc_v4l2_open,
1305 .release = uvc_v4l2_release,
Laurent Pinchart673eb9f2010-11-21 16:54:56 -03001306 .unlocked_ioctl = uvc_v4l2_ioctl,
Laurent Pinchart1a5e4c82011-12-18 20:45:39 -03001307#ifdef CONFIG_COMPAT
1308 .compat_ioctl32 = uvc_v4l2_compat_ioctl32,
1309#endif
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001310 .read = uvc_v4l2_read,
1311 .mmap = uvc_v4l2_mmap,
1312 .poll = uvc_v4l2_poll,
Bob Liu72969442011-04-29 07:11:35 -03001313#ifndef CONFIG_MMU
1314 .get_unmapped_area = uvc_v4l2_get_unmapped_area,
1315#endif
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001316};
Hans Verkuilf87086e2008-07-18 00:50:58 -03001317