blob: 74323362c8e6df159714b818e38d9d1beb530384 [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
14#include <linux/kernel.h>
15#include <linux/version.h>
16#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>
24#include <asm/atomic.h>
25
26#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030027#include <media/v4l2-ioctl.h>
Laurent Pinchartc0efd232008-06-30 15:04:50 -030028
29#include "uvcvideo.h"
30
31/* ------------------------------------------------------------------------
Laurent Pinchart561474c22010-02-18 16:38:52 -030032 * UVC ioctls
33 */
Laurent Pinchartba2fa992010-09-20 05:53:21 -030034static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain,
35 struct uvc_xu_control_mapping *xmap, int old)
Laurent Pinchart561474c22010-02-18 16:38:52 -030036{
37 struct uvc_control_mapping *map;
38 unsigned int size;
39 int ret;
40
41 map = kzalloc(sizeof *map, GFP_KERNEL);
42 if (map == NULL)
43 return -ENOMEM;
44
45 map->id = xmap->id;
46 memcpy(map->name, xmap->name, sizeof map->name);
47 memcpy(map->entity, xmap->entity, sizeof map->entity);
48 map->selector = xmap->selector;
49 map->size = xmap->size;
50 map->offset = xmap->offset;
51 map->v4l2_type = xmap->v4l2_type;
52 map->data_type = xmap->data_type;
53
54 switch (xmap->v4l2_type) {
55 case V4L2_CTRL_TYPE_INTEGER:
56 case V4L2_CTRL_TYPE_BOOLEAN:
57 case V4L2_CTRL_TYPE_BUTTON:
58 break;
59
60 case V4L2_CTRL_TYPE_MENU:
61 if (old) {
Laurent Pinchartba2fa992010-09-20 05:53:21 -030062 uvc_trace(UVC_TRACE_CONTROL, "V4L2_CTRL_TYPE_MENU not "
63 "supported for UVCIOC_CTRL_MAP_OLD.\n");
Laurent Pinchart561474c22010-02-18 16:38:52 -030064 ret = -EINVAL;
65 goto done;
66 }
67
68 size = xmap->menu_count * sizeof(*map->menu_info);
69 map->menu_info = kmalloc(size, GFP_KERNEL);
70 if (map->menu_info == NULL) {
71 ret = -ENOMEM;
72 goto done;
73 }
74
75 if (copy_from_user(map->menu_info, xmap->menu_info, size)) {
76 ret = -EFAULT;
77 goto done;
78 }
79
80 map->menu_count = xmap->menu_count;
81 break;
82
83 default:
Laurent Pinchartba2fa992010-09-20 05:53:21 -030084 uvc_trace(UVC_TRACE_CONTROL, "Unsupported V4L2 control type "
85 "%u.\n", xmap->v4l2_type);
Laurent Pinchart561474c22010-02-18 16:38:52 -030086 ret = -EINVAL;
87 goto done;
88 }
89
Laurent Pinchartba2fa992010-09-20 05:53:21 -030090 ret = uvc_ctrl_add_mapping(chain, map);
Laurent Pinchart561474c22010-02-18 16:38:52 -030091
92done:
Laurent Pinchartba2fa992010-09-20 05:53:21 -030093 kfree(map->menu_info);
94 kfree(map);
Laurent Pinchart561474c22010-02-18 16:38:52 -030095
96 return ret;
97}
98
99/* ------------------------------------------------------------------------
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300100 * V4L2 interface
101 */
102
103/*
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300104 * Find the frame interval closest to the requested frame interval for the
105 * given frame format and size. This should be done by the device as part of
106 * the Video Probe and Commit negotiation, but some hardware don't implement
107 * that feature.
108 */
109static __u32 uvc_try_frame_interval(struct uvc_frame *frame, __u32 interval)
110{
111 unsigned int i;
112
113 if (frame->bFrameIntervalType) {
114 __u32 best = -1, dist;
115
116 for (i = 0; i < frame->bFrameIntervalType; ++i) {
117 dist = interval > frame->dwFrameInterval[i]
118 ? interval - frame->dwFrameInterval[i]
119 : frame->dwFrameInterval[i] - interval;
120
121 if (dist > best)
122 break;
123
124 best = dist;
125 }
126
127 interval = frame->dwFrameInterval[i-1];
128 } else {
129 const __u32 min = frame->dwFrameInterval[0];
130 const __u32 max = frame->dwFrameInterval[1];
131 const __u32 step = frame->dwFrameInterval[2];
132
133 interval = min + (interval - min + step/2) / step * step;
134 if (interval > max)
135 interval = max;
136 }
137
138 return interval;
139}
140
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300141static int uvc_v4l2_try_format(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300142 struct v4l2_format *fmt, struct uvc_streaming_control *probe,
143 struct uvc_format **uvc_format, struct uvc_frame **uvc_frame)
144{
145 struct uvc_format *format = NULL;
146 struct uvc_frame *frame = NULL;
147 __u16 rw, rh;
148 unsigned int d, maxd;
149 unsigned int i;
150 __u32 interval;
151 int ret = 0;
152 __u8 *fcc;
153
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300154 if (fmt->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300155 return -EINVAL;
156
157 fcc = (__u8 *)&fmt->fmt.pix.pixelformat;
158 uvc_trace(UVC_TRACE_FORMAT, "Trying format 0x%08x (%c%c%c%c): %ux%u.\n",
159 fmt->fmt.pix.pixelformat,
160 fcc[0], fcc[1], fcc[2], fcc[3],
161 fmt->fmt.pix.width, fmt->fmt.pix.height);
162
163 /* Check if the hardware supports the requested format. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300164 for (i = 0; i < stream->nformats; ++i) {
165 format = &stream->format[i];
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300166 if (format->fcc == fmt->fmt.pix.pixelformat)
167 break;
168 }
169
170 if (format == NULL || format->fcc != fmt->fmt.pix.pixelformat) {
171 uvc_trace(UVC_TRACE_FORMAT, "Unsupported format 0x%08x.\n",
172 fmt->fmt.pix.pixelformat);
173 return -EINVAL;
174 }
175
176 /* Find the closest image size. The distance between image sizes is
177 * the size in pixels of the non-overlapping regions between the
178 * requested size and the frame-specified size.
179 */
180 rw = fmt->fmt.pix.width;
181 rh = fmt->fmt.pix.height;
182 maxd = (unsigned int)-1;
183
184 for (i = 0; i < format->nframes; ++i) {
185 __u16 w = format->frame[i].wWidth;
186 __u16 h = format->frame[i].wHeight;
187
188 d = min(w, rw) * min(h, rh);
189 d = w*h + rw*rh - 2*d;
190 if (d < maxd) {
191 maxd = d;
192 frame = &format->frame[i];
193 }
194
195 if (maxd == 0)
196 break;
197 }
198
199 if (frame == NULL) {
200 uvc_trace(UVC_TRACE_FORMAT, "Unsupported size %ux%u.\n",
201 fmt->fmt.pix.width, fmt->fmt.pix.height);
202 return -EINVAL;
203 }
204
205 /* Use the default frame interval. */
206 interval = frame->dwDefaultFrameInterval;
207 uvc_trace(UVC_TRACE_FORMAT, "Using default frame interval %u.%u us "
208 "(%u.%u fps).\n", interval/10, interval%10, 10000000/interval,
209 (100000000/interval)%10);
210
211 /* Set the format index, frame index and frame interval. */
212 memset(probe, 0, sizeof *probe);
213 probe->bmHint = 1; /* dwFrameInterval */
214 probe->bFormatIndex = format->index;
215 probe->bFrameIndex = frame->bFrameIndex;
216 probe->dwFrameInterval = uvc_try_frame_interval(frame, interval);
217 /* Some webcams stall the probe control set request when the
218 * dwMaxVideoFrameSize field is set to zero. The UVC specification
219 * clearly states that the field is read-only from the host, so this
220 * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by
221 * the webcam to work around the problem.
222 *
223 * The workaround could probably be enabled for all webcams, so the
224 * quirk can be removed if needed. It's currently useful to detect
225 * webcam bugs and fix them before they hit the market (providing
226 * developers test their webcams with the Linux driver as well as with
227 * the Windows driver).
228 */
Laurent Pinchart69477562010-11-21 13:36:34 -0300229 mutex_lock(&stream->mutex);
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300230 if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300231 probe->dwMaxVideoFrameSize =
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300232 stream->ctrl.dwMaxVideoFrameSize;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300233
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300234 /* Probe the device. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300235 ret = uvc_probe_video(stream, probe);
Laurent Pinchart69477562010-11-21 13:36:34 -0300236 mutex_unlock(&stream->mutex);
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300237 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300238 goto done;
239
240 fmt->fmt.pix.width = frame->wWidth;
241 fmt->fmt.pix.height = frame->wHeight;
242 fmt->fmt.pix.field = V4L2_FIELD_NONE;
243 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
244 fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize;
245 fmt->fmt.pix.colorspace = format->colorspace;
246 fmt->fmt.pix.priv = 0;
247
248 if (uvc_format != NULL)
249 *uvc_format = format;
250 if (uvc_frame != NULL)
251 *uvc_frame = frame;
252
253done:
254 return ret;
255}
256
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300257static int uvc_v4l2_get_format(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300258 struct v4l2_format *fmt)
259{
Laurent Pinchart69477562010-11-21 13:36:34 -0300260 struct uvc_format *format;
261 struct uvc_frame *frame;
262 int ret = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300263
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300264 if (fmt->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300265 return -EINVAL;
266
Laurent Pinchart69477562010-11-21 13:36:34 -0300267 mutex_lock(&stream->mutex);
268 format = stream->cur_format;
269 frame = stream->cur_frame;
270
271 if (format == NULL || frame == NULL) {
272 ret = -EINVAL;
273 goto done;
274 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300275
276 fmt->fmt.pix.pixelformat = format->fcc;
277 fmt->fmt.pix.width = frame->wWidth;
278 fmt->fmt.pix.height = frame->wHeight;
279 fmt->fmt.pix.field = V4L2_FIELD_NONE;
280 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300281 fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300282 fmt->fmt.pix.colorspace = format->colorspace;
283 fmt->fmt.pix.priv = 0;
284
Laurent Pinchart69477562010-11-21 13:36:34 -0300285done:
286 mutex_unlock(&stream->mutex);
287 return ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300288}
289
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300290static int uvc_v4l2_set_format(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300291 struct v4l2_format *fmt)
292{
293 struct uvc_streaming_control probe;
294 struct uvc_format *format;
295 struct uvc_frame *frame;
296 int ret;
297
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300298 if (fmt->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300299 return -EINVAL;
300
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300301 ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300302 if (ret < 0)
303 return ret;
304
Laurent Pinchart69477562010-11-21 13:36:34 -0300305 mutex_lock(&stream->mutex);
306
307 if (uvc_queue_allocated(&stream->queue)) {
308 ret = -EBUSY;
309 goto done;
310 }
311
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300312 memcpy(&stream->ctrl, &probe, sizeof probe);
313 stream->cur_format = format;
314 stream->cur_frame = frame;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300315
Laurent Pinchart69477562010-11-21 13:36:34 -0300316done:
317 mutex_unlock(&stream->mutex);
318 return ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300319}
320
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300321static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300322 struct v4l2_streamparm *parm)
323{
324 uint32_t numerator, denominator;
325
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300326 if (parm->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300327 return -EINVAL;
328
Laurent Pinchart69477562010-11-21 13:36:34 -0300329 mutex_lock(&stream->mutex);
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300330 numerator = stream->ctrl.dwFrameInterval;
Laurent Pinchart69477562010-11-21 13:36:34 -0300331 mutex_unlock(&stream->mutex);
332
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300333 denominator = 10000000;
334 uvc_simplify_fraction(&numerator, &denominator, 8, 333);
335
336 memset(parm, 0, sizeof *parm);
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300337 parm->type = stream->type;
Laurent Pinchartff924202008-12-28 22:32:29 -0300338
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300339 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Laurent Pinchartff924202008-12-28 22:32:29 -0300340 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
341 parm->parm.capture.capturemode = 0;
342 parm->parm.capture.timeperframe.numerator = numerator;
343 parm->parm.capture.timeperframe.denominator = denominator;
344 parm->parm.capture.extendedmode = 0;
345 parm->parm.capture.readbuffers = 0;
346 } else {
347 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
348 parm->parm.output.outputmode = 0;
349 parm->parm.output.timeperframe.numerator = numerator;
350 parm->parm.output.timeperframe.denominator = denominator;
351 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300352
353 return 0;
354}
355
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300356static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300357 struct v4l2_streamparm *parm)
358{
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300359 struct uvc_streaming_control probe;
Laurent Pinchartff924202008-12-28 22:32:29 -0300360 struct v4l2_fract timeperframe;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300361 uint32_t interval;
362 int ret;
363
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300364 if (parm->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300365 return -EINVAL;
366
Laurent Pinchartff924202008-12-28 22:32:29 -0300367 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
368 timeperframe = parm->parm.capture.timeperframe;
369 else
370 timeperframe = parm->parm.output.timeperframe;
371
Laurent Pinchartff924202008-12-28 22:32:29 -0300372 interval = uvc_fraction_to_interval(timeperframe.numerator,
373 timeperframe.denominator);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300374 uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
Laurent Pinchartff924202008-12-28 22:32:29 -0300375 timeperframe.numerator, timeperframe.denominator, interval);
Laurent Pinchart69477562010-11-21 13:36:34 -0300376
377 mutex_lock(&stream->mutex);
378
379 if (uvc_queue_streaming(&stream->queue)) {
380 mutex_unlock(&stream->mutex);
381 return -EBUSY;
382 }
383
384 memcpy(&probe, &stream->ctrl, sizeof probe);
385 probe.dwFrameInterval =
386 uvc_try_frame_interval(stream->cur_frame, interval);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300387
388 /* Probe the device with the new settings. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300389 ret = uvc_probe_video(stream, &probe);
Laurent Pinchart69477562010-11-21 13:36:34 -0300390 if (ret < 0) {
391 mutex_unlock(&stream->mutex);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300392 return ret;
Laurent Pinchart69477562010-11-21 13:36:34 -0300393 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300394
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300395 memcpy(&stream->ctrl, &probe, sizeof probe);
Laurent Pinchart69477562010-11-21 13:36:34 -0300396 mutex_unlock(&stream->mutex);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300397
398 /* Return the actual frame period. */
Laurent Pinchartff924202008-12-28 22:32:29 -0300399 timeperframe.numerator = probe.dwFrameInterval;
400 timeperframe.denominator = 10000000;
401 uvc_simplify_fraction(&timeperframe.numerator,
402 &timeperframe.denominator, 8, 333);
403
404 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
405 parm->parm.capture.timeperframe = timeperframe;
406 else
407 parm->parm.output.timeperframe = timeperframe;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300408
409 return 0;
410}
411
412/* ------------------------------------------------------------------------
413 * Privilege management
414 */
415
416/*
417 * Privilege management is the multiple-open implementation basis. The current
418 * implementation is completely transparent for the end-user and doesn't
419 * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls.
420 * Those ioctls enable finer control on the device (by making possible for a
421 * user to request exclusive access to a device), but are not mature yet.
422 * Switching to the V4L2 priority mechanism might be considered in the future
423 * if this situation changes.
424 *
425 * Each open instance of a UVC device can either be in a privileged or
426 * unprivileged state. Only a single instance can be in a privileged state at
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300427 * a given time. Trying to perform an operation that requires privileges will
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300428 * automatically acquire the required privileges if possible, or return -EBUSY
Laurent Pinchart1a969d92009-09-02 03:12:26 -0300429 * otherwise. Privileges are dismissed when closing the instance or when
430 * freeing the video buffers using VIDIOC_REQBUFS.
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300431 *
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300432 * Operations that require privileges are:
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300433 *
434 * - VIDIOC_S_INPUT
435 * - VIDIOC_S_PARM
436 * - VIDIOC_S_FMT
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300437 * - VIDIOC_REQBUFS
438 */
439static int uvc_acquire_privileges(struct uvc_fh *handle)
440{
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300441 /* Always succeed if the handle is already privileged. */
442 if (handle->state == UVC_HANDLE_ACTIVE)
443 return 0;
444
445 /* Check if the device already has a privileged handle. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300446 if (atomic_inc_return(&handle->stream->active) != 1) {
447 atomic_dec(&handle->stream->active);
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300448 return -EBUSY;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300449 }
450
451 handle->state = UVC_HANDLE_ACTIVE;
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300452 return 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300453}
454
455static void uvc_dismiss_privileges(struct uvc_fh *handle)
456{
457 if (handle->state == UVC_HANDLE_ACTIVE)
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300458 atomic_dec(&handle->stream->active);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300459
460 handle->state = UVC_HANDLE_PASSIVE;
461}
462
463static int uvc_has_privileges(struct uvc_fh *handle)
464{
465 return handle->state == UVC_HANDLE_ACTIVE;
466}
467
468/* ------------------------------------------------------------------------
469 * V4L2 file operations
470 */
471
Hans Verkuilbec43662008-12-30 06:58:20 -0300472static int uvc_v4l2_open(struct file *file)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300473{
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300474 struct uvc_streaming *stream;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300475 struct uvc_fh *handle;
476 int ret = 0;
477
478 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300479 stream = video_drvdata(file);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300480
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300481 if (stream->dev->state & UVC_DEV_DISCONNECTED)
482 return -ENODEV;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300483
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300484 ret = usb_autopm_get_interface(stream->dev->intf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300485 if (ret < 0)
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300486 return ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300487
488 /* Create the device handle. */
489 handle = kzalloc(sizeof *handle, GFP_KERNEL);
490 if (handle == NULL) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300491 usb_autopm_put_interface(stream->dev->intf);
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300492 return -ENOMEM;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300493 }
494
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300495 if (atomic_inc_return(&stream->dev->users) == 1) {
496 ret = uvc_status_start(stream->dev);
497 if (ret < 0) {
498 usb_autopm_put_interface(stream->dev->intf);
499 atomic_dec(&stream->dev->users);
Laurent Pinchart04a37e02009-05-19 10:08:03 -0300500 kfree(handle);
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300501 return ret;
Laurent Pinchart04a37e02009-05-19 10:08:03 -0300502 }
503 }
504
Laurent Pinchart8e113592009-07-01 20:24:47 -0300505 handle->chain = stream->chain;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300506 handle->stream = stream;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300507 handle->state = UVC_HANDLE_PASSIVE;
508 file->private_data = handle;
509
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300510 return 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300511}
512
Hans Verkuilbec43662008-12-30 06:58:20 -0300513static int uvc_v4l2_release(struct file *file)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300514{
Joe Perchesabf84382010-07-12 17:50:03 -0300515 struct uvc_fh *handle = file->private_data;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300516 struct uvc_streaming *stream = handle->stream;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300517
518 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");
519
520 /* Only free resources if this is a privileged handle. */
521 if (uvc_has_privileges(handle)) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300522 uvc_video_enable(stream, 0);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300523
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300524 if (uvc_free_buffers(&stream->queue) < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300525 uvc_printk(KERN_ERR, "uvc_v4l2_release: Unable to "
526 "free buffers.\n");
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300527 }
528
529 /* Release the file handle. */
530 uvc_dismiss_privileges(handle);
531 kfree(handle);
532 file->private_data = NULL;
533
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300534 if (atomic_dec_return(&stream->dev->users) == 0)
535 uvc_status_stop(stream->dev);
Laurent Pinchart04a37e02009-05-19 10:08:03 -0300536
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300537 usb_autopm_put_interface(stream->dev->intf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300538 return 0;
539}
540
Hans Verkuil069b7472008-12-30 07:04:34 -0300541static long uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300542{
543 struct video_device *vdev = video_devdata(file);
Joe Perchesabf84382010-07-12 17:50:03 -0300544 struct uvc_fh *handle = file->private_data;
Laurent Pinchart8e113592009-07-01 20:24:47 -0300545 struct uvc_video_chain *chain = handle->chain;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300546 struct uvc_streaming *stream = handle->stream;
Hans Verkuil069b7472008-12-30 07:04:34 -0300547 long ret = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300548
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300549 switch (cmd) {
550 /* Query capabilities */
551 case VIDIOC_QUERYCAP:
552 {
553 struct v4l2_capability *cap = arg;
554
555 memset(cap, 0, sizeof *cap);
Laurent Pinchartd0ebf302009-01-08 14:05:11 -0300556 strlcpy(cap->driver, "uvcvideo", sizeof cap->driver);
557 strlcpy(cap->card, vdev->name, sizeof cap->card);
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300558 usb_make_path(stream->dev->udev,
Thierry MERLEb12049a2009-01-27 16:53:23 -0300559 cap->bus_info, sizeof(cap->bus_info));
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300560 cap->version = DRIVER_VERSION_NUMBER;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300561 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
Laurent Pinchartff924202008-12-28 22:32:29 -0300562 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
563 | V4L2_CAP_STREAMING;
564 else
565 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT
566 | V4L2_CAP_STREAMING;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300567 break;
568 }
569
570 /* Get, Set & Query control */
571 case VIDIOC_QUERYCTRL:
Laurent Pinchart8e113592009-07-01 20:24:47 -0300572 return uvc_query_v4l2_ctrl(chain, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300573
574 case VIDIOC_G_CTRL:
575 {
576 struct v4l2_control *ctrl = arg;
577 struct v4l2_ext_control xctrl;
578
579 memset(&xctrl, 0, sizeof xctrl);
580 xctrl.id = ctrl->id;
581
Laurent Pinchart8e113592009-07-01 20:24:47 -0300582 ret = uvc_ctrl_begin(chain);
583 if (ret < 0)
Robert Krakora1fcbcc42009-06-12 13:51:03 -0300584 return ret;
585
Laurent Pinchart8e113592009-07-01 20:24:47 -0300586 ret = uvc_ctrl_get(chain, &xctrl);
587 uvc_ctrl_rollback(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300588 if (ret >= 0)
589 ctrl->value = xctrl.value;
590 break;
591 }
592
593 case VIDIOC_S_CTRL:
594 {
595 struct v4l2_control *ctrl = arg;
596 struct v4l2_ext_control xctrl;
597
598 memset(&xctrl, 0, sizeof xctrl);
599 xctrl.id = ctrl->id;
600 xctrl.value = ctrl->value;
601
Laurent Pinchart8d556622010-02-04 21:43:37 -0300602 ret = uvc_ctrl_begin(chain);
Laurent Pinchart8e113592009-07-01 20:24:47 -0300603 if (ret < 0)
Robert Krakora1fcbcc42009-06-12 13:51:03 -0300604 return ret;
605
Laurent Pinchart8e113592009-07-01 20:24:47 -0300606 ret = uvc_ctrl_set(chain, &xctrl);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300607 if (ret < 0) {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300608 uvc_ctrl_rollback(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300609 return ret;
610 }
Laurent Pinchart8e113592009-07-01 20:24:47 -0300611 ret = uvc_ctrl_commit(chain);
Laurent Pincharte54532e2010-01-23 07:07:53 -0300612 if (ret == 0)
613 ctrl->value = xctrl.value;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300614 break;
615 }
616
617 case VIDIOC_QUERYMENU:
Laurent Pinchart23d9f3e2010-11-21 07:58:54 -0300618 return uvc_query_v4l2_menu(chain, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300619
620 case VIDIOC_G_EXT_CTRLS:
621 {
622 struct v4l2_ext_controls *ctrls = arg;
623 struct v4l2_ext_control *ctrl = ctrls->controls;
624 unsigned int i;
625
Laurent Pinchart8e113592009-07-01 20:24:47 -0300626 ret = uvc_ctrl_begin(chain);
627 if (ret < 0)
Robert Krakora1fcbcc42009-06-12 13:51:03 -0300628 return ret;
629
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300630 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300631 ret = uvc_ctrl_get(chain, ctrl);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300632 if (ret < 0) {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300633 uvc_ctrl_rollback(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300634 ctrls->error_idx = i;
635 return ret;
636 }
637 }
638 ctrls->error_idx = 0;
Laurent Pinchart8e113592009-07-01 20:24:47 -0300639 ret = uvc_ctrl_rollback(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300640 break;
641 }
642
643 case VIDIOC_S_EXT_CTRLS:
644 case VIDIOC_TRY_EXT_CTRLS:
645 {
646 struct v4l2_ext_controls *ctrls = arg;
647 struct v4l2_ext_control *ctrl = ctrls->controls;
648 unsigned int i;
649
Laurent Pinchart8e113592009-07-01 20:24:47 -0300650 ret = uvc_ctrl_begin(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300651 if (ret < 0)
652 return ret;
653
654 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300655 ret = uvc_ctrl_set(chain, ctrl);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300656 if (ret < 0) {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300657 uvc_ctrl_rollback(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300658 ctrls->error_idx = i;
659 return ret;
660 }
661 }
662
663 ctrls->error_idx = 0;
664
665 if (cmd == VIDIOC_S_EXT_CTRLS)
Laurent Pinchart8e113592009-07-01 20:24:47 -0300666 ret = uvc_ctrl_commit(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300667 else
Laurent Pinchart8e113592009-07-01 20:24:47 -0300668 ret = uvc_ctrl_rollback(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300669 break;
670 }
671
672 /* Get, Set & Enum input */
673 case VIDIOC_ENUMINPUT:
674 {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300675 const struct uvc_entity *selector = chain->selector;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300676 struct v4l2_input *input = arg;
677 struct uvc_entity *iterm = NULL;
678 u32 index = input->index;
679 int pin = 0;
680
681 if (selector == NULL ||
Laurent Pinchart8e113592009-07-01 20:24:47 -0300682 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300683 if (index != 0)
684 return -EINVAL;
Laurent Pinchart6241d8c2009-11-25 12:00:22 -0300685 list_for_each_entry(iterm, &chain->entities, chain) {
686 if (UVC_ENTITY_IS_ITERM(iterm))
687 break;
688 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300689 pin = iterm->id;
Laurent Pinchart8ca5a632009-11-25 12:00:30 -0300690 } else if (pin < selector->bNrInPins) {
691 pin = selector->baSourceID[index];
Laurent Pinchart6241d8c2009-11-25 12:00:22 -0300692 list_for_each_entry(iterm, &chain->entities, chain) {
693 if (!UVC_ENTITY_IS_ITERM(iterm))
694 continue;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300695 if (iterm->id == pin)
696 break;
697 }
698 }
699
700 if (iterm == NULL || iterm->id != pin)
701 return -EINVAL;
702
703 memset(input, 0, sizeof *input);
704 input->index = index;
Laurent Pinchartd0ebf302009-01-08 14:05:11 -0300705 strlcpy(input->name, iterm->name, sizeof input->name);
Laurent Pinchartb482d922009-06-26 11:39:42 -0300706 if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300707 input->type = V4L2_INPUT_TYPE_CAMERA;
708 break;
709 }
710
711 case VIDIOC_G_INPUT:
712 {
713 u8 input;
714
Laurent Pinchart8e113592009-07-01 20:24:47 -0300715 if (chain->selector == NULL ||
716 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300717 *(int *)arg = 0;
718 break;
719 }
720
Laurent Pinchart8e113592009-07-01 20:24:47 -0300721 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
722 chain->selector->id, chain->dev->intfnum,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300723 UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300724 if (ret < 0)
725 return ret;
726
727 *(int *)arg = input - 1;
728 break;
729 }
730
731 case VIDIOC_S_INPUT:
732 {
Márton Németh9086c7b992009-04-15 09:01:58 -0300733 u32 input = *(u32 *)arg + 1;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300734
735 if ((ret = uvc_acquire_privileges(handle)) < 0)
736 return ret;
737
Laurent Pinchart8e113592009-07-01 20:24:47 -0300738 if (chain->selector == NULL ||
739 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300740 if (input != 1)
741 return -EINVAL;
742 break;
743 }
744
Laurent Pinchart8ca5a632009-11-25 12:00:30 -0300745 if (input == 0 || input > chain->selector->bNrInPins)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300746 return -EINVAL;
747
Laurent Pinchart8e113592009-07-01 20:24:47 -0300748 return uvc_query_ctrl(chain->dev, UVC_SET_CUR,
749 chain->selector->id, chain->dev->intfnum,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300750 UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300751 }
752
753 /* Try, Get, Set & Enum format */
754 case VIDIOC_ENUM_FMT:
755 {
756 struct v4l2_fmtdesc *fmt = arg;
757 struct uvc_format *format;
Márton Németh8bbd90c2009-03-27 11:13:57 -0300758 enum v4l2_buf_type type = fmt->type;
759 __u32 index = fmt->index;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300760
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300761 if (fmt->type != stream->type ||
762 fmt->index >= stream->nformats)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300763 return -EINVAL;
764
Márton Németh8bbd90c2009-03-27 11:13:57 -0300765 memset(fmt, 0, sizeof(*fmt));
766 fmt->index = index;
767 fmt->type = type;
768
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300769 format = &stream->format[fmt->index];
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300770 fmt->flags = 0;
771 if (format->flags & UVC_FMT_FLAG_COMPRESSED)
772 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
Laurent Pinchartd0ebf302009-01-08 14:05:11 -0300773 strlcpy(fmt->description, format->name,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300774 sizeof fmt->description);
775 fmt->description[sizeof fmt->description - 1] = 0;
776 fmt->pixelformat = format->fcc;
777 break;
778 }
779
780 case VIDIOC_TRY_FMT:
781 {
782 struct uvc_streaming_control probe;
783
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300784 return uvc_v4l2_try_format(stream, arg, &probe, NULL, NULL);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300785 }
786
787 case VIDIOC_S_FMT:
788 if ((ret = uvc_acquire_privileges(handle)) < 0)
789 return ret;
790
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300791 return uvc_v4l2_set_format(stream, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300792
793 case VIDIOC_G_FMT:
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300794 return uvc_v4l2_get_format(stream, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300795
796 /* Frame size enumeration */
797 case VIDIOC_ENUM_FRAMESIZES:
798 {
799 struct v4l2_frmsizeenum *fsize = arg;
800 struct uvc_format *format = NULL;
801 struct uvc_frame *frame;
802 int i;
803
804 /* Look for the given pixel format */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300805 for (i = 0; i < stream->nformats; i++) {
806 if (stream->format[i].fcc ==
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300807 fsize->pixel_format) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300808 format = &stream->format[i];
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300809 break;
810 }
811 }
812 if (format == NULL)
813 return -EINVAL;
814
815 if (fsize->index >= format->nframes)
816 return -EINVAL;
817
818 frame = &format->frame[fsize->index];
819 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
820 fsize->discrete.width = frame->wWidth;
821 fsize->discrete.height = frame->wHeight;
822 break;
823 }
824
825 /* Frame interval enumeration */
826 case VIDIOC_ENUM_FRAMEINTERVALS:
827 {
828 struct v4l2_frmivalenum *fival = arg;
829 struct uvc_format *format = NULL;
830 struct uvc_frame *frame = NULL;
831 int i;
832
833 /* Look for the given pixel format and frame size */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300834 for (i = 0; i < stream->nformats; i++) {
835 if (stream->format[i].fcc ==
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300836 fival->pixel_format) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300837 format = &stream->format[i];
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300838 break;
839 }
840 }
841 if (format == NULL)
842 return -EINVAL;
843
844 for (i = 0; i < format->nframes; i++) {
845 if (format->frame[i].wWidth == fival->width &&
846 format->frame[i].wHeight == fival->height) {
847 frame = &format->frame[i];
848 break;
849 }
850 }
851 if (frame == NULL)
852 return -EINVAL;
853
854 if (frame->bFrameIntervalType) {
855 if (fival->index >= frame->bFrameIntervalType)
856 return -EINVAL;
857
858 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
859 fival->discrete.numerator =
860 frame->dwFrameInterval[fival->index];
861 fival->discrete.denominator = 10000000;
862 uvc_simplify_fraction(&fival->discrete.numerator,
863 &fival->discrete.denominator, 8, 333);
864 } else {
865 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
866 fival->stepwise.min.numerator =
867 frame->dwFrameInterval[0];
868 fival->stepwise.min.denominator = 10000000;
869 fival->stepwise.max.numerator =
870 frame->dwFrameInterval[1];
871 fival->stepwise.max.denominator = 10000000;
872 fival->stepwise.step.numerator =
873 frame->dwFrameInterval[2];
874 fival->stepwise.step.denominator = 10000000;
875 uvc_simplify_fraction(&fival->stepwise.min.numerator,
876 &fival->stepwise.min.denominator, 8, 333);
877 uvc_simplify_fraction(&fival->stepwise.max.numerator,
878 &fival->stepwise.max.denominator, 8, 333);
879 uvc_simplify_fraction(&fival->stepwise.step.numerator,
880 &fival->stepwise.step.denominator, 8, 333);
881 }
882 break;
883 }
884
885 /* Get & Set streaming parameters */
886 case VIDIOC_G_PARM:
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300887 return uvc_v4l2_get_streamparm(stream, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300888
889 case VIDIOC_S_PARM:
890 if ((ret = uvc_acquire_privileges(handle)) < 0)
891 return ret;
892
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300893 return uvc_v4l2_set_streamparm(stream, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300894
895 /* Cropping and scaling */
896 case VIDIOC_CROPCAP:
897 {
898 struct v4l2_cropcap *ccap = arg;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300899
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300900 if (ccap->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300901 return -EINVAL;
902
903 ccap->bounds.left = 0;
904 ccap->bounds.top = 0;
Laurent Pinchart69477562010-11-21 13:36:34 -0300905
906 mutex_lock(&stream->mutex);
907 ccap->bounds.width = stream->cur_frame->wWidth;
908 ccap->bounds.height = stream->cur_frame->wHeight;
909 mutex_unlock(&stream->mutex);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300910
911 ccap->defrect = ccap->bounds;
912
913 ccap->pixelaspect.numerator = 1;
914 ccap->pixelaspect.denominator = 1;
915 break;
916 }
917
918 case VIDIOC_G_CROP:
919 case VIDIOC_S_CROP:
920 return -EINVAL;
921
922 /* Buffers & streaming */
923 case VIDIOC_REQBUFS:
924 {
925 struct v4l2_requestbuffers *rb = arg;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300926
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300927 if (rb->type != stream->type ||
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300928 rb->memory != V4L2_MEMORY_MMAP)
929 return -EINVAL;
930
931 if ((ret = uvc_acquire_privileges(handle)) < 0)
932 return ret;
933
Laurent Pinchart69477562010-11-21 13:36:34 -0300934 mutex_lock(&stream->mutex);
935 ret = uvc_alloc_buffers(&stream->queue, rb->count,
936 stream->ctrl.dwMaxVideoFrameSize);
937 mutex_unlock(&stream->mutex);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300938 if (ret < 0)
939 return ret;
940
Laurent Pinchart1a969d92009-09-02 03:12:26 -0300941 if (ret == 0)
942 uvc_dismiss_privileges(handle);
943
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300944 rb->count = ret;
945 ret = 0;
946 break;
947 }
948
949 case VIDIOC_QUERYBUF:
950 {
951 struct v4l2_buffer *buf = arg;
952
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300953 if (buf->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300954 return -EINVAL;
955
956 if (!uvc_has_privileges(handle))
957 return -EBUSY;
958
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300959 return uvc_query_buffer(&stream->queue, buf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300960 }
961
962 case VIDIOC_QBUF:
963 if (!uvc_has_privileges(handle))
964 return -EBUSY;
965
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300966 return uvc_queue_buffer(&stream->queue, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300967
968 case VIDIOC_DQBUF:
969 if (!uvc_has_privileges(handle))
970 return -EBUSY;
971
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300972 return uvc_dequeue_buffer(&stream->queue, arg,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300973 file->f_flags & O_NONBLOCK);
974
975 case VIDIOC_STREAMON:
976 {
977 int *type = arg;
978
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300979 if (*type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300980 return -EINVAL;
981
982 if (!uvc_has_privileges(handle))
983 return -EBUSY;
984
Laurent Pinchart69477562010-11-21 13:36:34 -0300985 mutex_lock(&stream->mutex);
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300986 ret = uvc_video_enable(stream, 1);
Laurent Pinchart69477562010-11-21 13:36:34 -0300987 mutex_unlock(&stream->mutex);
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300988 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300989 return ret;
990 break;
991 }
992
993 case VIDIOC_STREAMOFF:
994 {
995 int *type = arg;
996
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300997 if (*type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300998 return -EINVAL;
999
1000 if (!uvc_has_privileges(handle))
1001 return -EBUSY;
1002
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001003 return uvc_video_enable(stream, 0);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001004 }
1005
1006 /* Analog video standards make no sense for digital cameras. */
1007 case VIDIOC_ENUMSTD:
1008 case VIDIOC_QUERYSTD:
1009 case VIDIOC_G_STD:
1010 case VIDIOC_S_STD:
1011
1012 case VIDIOC_OVERLAY:
1013
1014 case VIDIOC_ENUMAUDIO:
1015 case VIDIOC_ENUMAUDOUT:
1016
1017 case VIDIOC_ENUMOUTPUT:
1018 uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd);
1019 return -EINVAL;
1020
1021 /* Dynamic controls. */
1022 case UVCIOC_CTRL_ADD:
Laurent Pinchartba2fa992010-09-20 05:53:21 -03001023 /* Legacy ioctl, kept for API compatibility reasons */
Laurent Pinchartba2fa992010-09-20 05:53:21 -03001024 return -EEXIST;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001025
Laurent Pinchart561474c22010-02-18 16:38:52 -03001026 case UVCIOC_CTRL_MAP_OLD:
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001027 case UVCIOC_CTRL_MAP:
Laurent Pinchartba2fa992010-09-20 05:53:21 -03001028 return uvc_ioctl_ctrl_map(chain, arg,
1029 cmd == UVCIOC_CTRL_MAP_OLD);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001030
1031 case UVCIOC_CTRL_GET:
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001032 case UVCIOC_CTRL_SET:
Martin Rublife78d182010-10-02 19:10:16 -03001033 {
1034 struct uvc_xu_control *xctrl = arg;
1035 struct uvc_xu_control_query xqry = {
1036 .unit = xctrl->unit,
1037 .selector = xctrl->selector,
1038 .query = cmd == UVCIOC_CTRL_GET
1039 ? UVC_GET_CUR : UVC_SET_CUR,
1040 .size = xctrl->size,
1041 .data = xctrl->data,
1042 };
1043
1044 return uvc_xu_ctrl_query(chain, &xqry);
1045 }
1046
1047 case UVCIOC_CTRL_QUERY:
1048 return uvc_xu_ctrl_query(chain, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001049
1050 default:
Hans Verkuil08af2452010-12-24 10:33:19 -03001051 uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n", cmd);
1052 return -EINVAL;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001053 }
1054
1055 return ret;
1056}
1057
Hans Verkuil069b7472008-12-30 07:04:34 -03001058static long uvc_v4l2_ioctl(struct file *file,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001059 unsigned int cmd, unsigned long arg)
1060{
Laurent Pinchart308bb522008-11-17 18:32:11 -03001061 if (uvc_trace_param & UVC_TRACE_IOCTL) {
1062 uvc_printk(KERN_DEBUG, "uvc_v4l2_ioctl(");
1063 v4l_printk_ioctl(cmd);
1064 printk(")\n");
1065 }
1066
Hans Verkuilf473bf72008-11-01 08:25:11 -03001067 return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001068}
1069
1070static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1071 size_t count, loff_t *ppos)
1072{
1073 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
Laurent Pinchart350d6402009-11-04 11:53:49 -03001074 return -EINVAL;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001075}
1076
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001077static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1078{
Joe Perchesabf84382010-07-12 17:50:03 -03001079 struct uvc_fh *handle = file->private_data;
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001080 struct uvc_streaming *stream = handle->stream;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001081
1082 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
1083
Laurent Pinchart4aa27592010-11-21 15:18:08 -03001084 return uvc_queue_mmap(&stream->queue, vma);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001085}
1086
1087static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
1088{
Joe Perchesabf84382010-07-12 17:50:03 -03001089 struct uvc_fh *handle = file->private_data;
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001090 struct uvc_streaming *stream = handle->stream;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001091
1092 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
1093
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001094 return uvc_queue_poll(&stream->queue, file, wait);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001095}
1096
Hans Verkuilbec43662008-12-30 06:58:20 -03001097const struct v4l2_file_operations uvc_fops = {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001098 .owner = THIS_MODULE,
1099 .open = uvc_v4l2_open,
1100 .release = uvc_v4l2_release,
Laurent Pinchart673eb9f2010-11-21 16:54:56 -03001101 .unlocked_ioctl = uvc_v4l2_ioctl,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001102 .read = uvc_v4l2_read,
1103 .mmap = uvc_v4l2_mmap,
1104 .poll = uvc_v4l2_poll,
1105};
Hans Verkuilf87086e2008-07-18 00:50:58 -03001106