blob: 0f865e97cf481b1784ca48a989f28d952aafee16 [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 Pinchart35f02a62009-06-28 08:37:50 -0300229 if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300230 probe->dwMaxVideoFrameSize =
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300231 stream->ctrl.dwMaxVideoFrameSize;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300232
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300233 /* Probe the device. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300234 ret = uvc_probe_video(stream, probe);
235 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300236 goto done;
237
238 fmt->fmt.pix.width = frame->wWidth;
239 fmt->fmt.pix.height = frame->wHeight;
240 fmt->fmt.pix.field = V4L2_FIELD_NONE;
241 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
242 fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize;
243 fmt->fmt.pix.colorspace = format->colorspace;
244 fmt->fmt.pix.priv = 0;
245
246 if (uvc_format != NULL)
247 *uvc_format = format;
248 if (uvc_frame != NULL)
249 *uvc_frame = frame;
250
251done:
252 return ret;
253}
254
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300255static int uvc_v4l2_get_format(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300256 struct v4l2_format *fmt)
257{
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300258 struct uvc_format *format = stream->cur_format;
259 struct uvc_frame *frame = stream->cur_frame;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300260
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300261 if (fmt->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300262 return -EINVAL;
263
264 if (format == NULL || frame == NULL)
265 return -EINVAL;
266
267 fmt->fmt.pix.pixelformat = format->fcc;
268 fmt->fmt.pix.width = frame->wWidth;
269 fmt->fmt.pix.height = frame->wHeight;
270 fmt->fmt.pix.field = V4L2_FIELD_NONE;
271 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300272 fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300273 fmt->fmt.pix.colorspace = format->colorspace;
274 fmt->fmt.pix.priv = 0;
275
276 return 0;
277}
278
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300279static int uvc_v4l2_set_format(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300280 struct v4l2_format *fmt)
281{
282 struct uvc_streaming_control probe;
283 struct uvc_format *format;
284 struct uvc_frame *frame;
285 int ret;
286
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300287 if (fmt->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300288 return -EINVAL;
289
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300290 if (uvc_queue_allocated(&stream->queue))
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300291 return -EBUSY;
292
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300293 ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300294 if (ret < 0)
295 return ret;
296
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300297 memcpy(&stream->ctrl, &probe, sizeof probe);
298 stream->cur_format = format;
299 stream->cur_frame = frame;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300300
301 return 0;
302}
303
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300304static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300305 struct v4l2_streamparm *parm)
306{
307 uint32_t numerator, denominator;
308
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300309 if (parm->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300310 return -EINVAL;
311
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300312 numerator = stream->ctrl.dwFrameInterval;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300313 denominator = 10000000;
314 uvc_simplify_fraction(&numerator, &denominator, 8, 333);
315
316 memset(parm, 0, sizeof *parm);
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300317 parm->type = stream->type;
Laurent Pinchartff924202008-12-28 22:32:29 -0300318
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300319 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Laurent Pinchartff924202008-12-28 22:32:29 -0300320 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
321 parm->parm.capture.capturemode = 0;
322 parm->parm.capture.timeperframe.numerator = numerator;
323 parm->parm.capture.timeperframe.denominator = denominator;
324 parm->parm.capture.extendedmode = 0;
325 parm->parm.capture.readbuffers = 0;
326 } else {
327 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
328 parm->parm.output.outputmode = 0;
329 parm->parm.output.timeperframe.numerator = numerator;
330 parm->parm.output.timeperframe.denominator = denominator;
331 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300332
333 return 0;
334}
335
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300336static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300337 struct v4l2_streamparm *parm)
338{
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300339 struct uvc_frame *frame = stream->cur_frame;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300340 struct uvc_streaming_control probe;
Laurent Pinchartff924202008-12-28 22:32:29 -0300341 struct v4l2_fract timeperframe;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300342 uint32_t interval;
343 int ret;
344
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300345 if (parm->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300346 return -EINVAL;
347
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300348 if (uvc_queue_streaming(&stream->queue))
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300349 return -EBUSY;
350
Laurent Pinchartff924202008-12-28 22:32:29 -0300351 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
352 timeperframe = parm->parm.capture.timeperframe;
353 else
354 timeperframe = parm->parm.output.timeperframe;
355
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300356 memcpy(&probe, &stream->ctrl, sizeof probe);
Laurent Pinchartff924202008-12-28 22:32:29 -0300357 interval = uvc_fraction_to_interval(timeperframe.numerator,
358 timeperframe.denominator);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300359
360 uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
Laurent Pinchartff924202008-12-28 22:32:29 -0300361 timeperframe.numerator, timeperframe.denominator, interval);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300362 probe.dwFrameInterval = uvc_try_frame_interval(frame, interval);
363
364 /* Probe the device with the new settings. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300365 ret = uvc_probe_video(stream, &probe);
366 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300367 return ret;
368
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300369 memcpy(&stream->ctrl, &probe, sizeof probe);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300370
371 /* Return the actual frame period. */
Laurent Pinchartff924202008-12-28 22:32:29 -0300372 timeperframe.numerator = probe.dwFrameInterval;
373 timeperframe.denominator = 10000000;
374 uvc_simplify_fraction(&timeperframe.numerator,
375 &timeperframe.denominator, 8, 333);
376
377 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
378 parm->parm.capture.timeperframe = timeperframe;
379 else
380 parm->parm.output.timeperframe = timeperframe;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300381
382 return 0;
383}
384
385/* ------------------------------------------------------------------------
386 * Privilege management
387 */
388
389/*
390 * Privilege management is the multiple-open implementation basis. The current
391 * implementation is completely transparent for the end-user and doesn't
392 * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls.
393 * Those ioctls enable finer control on the device (by making possible for a
394 * user to request exclusive access to a device), but are not mature yet.
395 * Switching to the V4L2 priority mechanism might be considered in the future
396 * if this situation changes.
397 *
398 * Each open instance of a UVC device can either be in a privileged or
399 * unprivileged state. Only a single instance can be in a privileged state at
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300400 * a given time. Trying to perform an operation that requires privileges will
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300401 * automatically acquire the required privileges if possible, or return -EBUSY
Laurent Pinchart1a969d92009-09-02 03:12:26 -0300402 * otherwise. Privileges are dismissed when closing the instance or when
403 * freeing the video buffers using VIDIOC_REQBUFS.
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300404 *
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300405 * Operations that require privileges are:
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300406 *
407 * - VIDIOC_S_INPUT
408 * - VIDIOC_S_PARM
409 * - VIDIOC_S_FMT
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300410 * - VIDIOC_REQBUFS
411 */
412static int uvc_acquire_privileges(struct uvc_fh *handle)
413{
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300414 /* Always succeed if the handle is already privileged. */
415 if (handle->state == UVC_HANDLE_ACTIVE)
416 return 0;
417
418 /* Check if the device already has a privileged handle. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300419 if (atomic_inc_return(&handle->stream->active) != 1) {
420 atomic_dec(&handle->stream->active);
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300421 return -EBUSY;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300422 }
423
424 handle->state = UVC_HANDLE_ACTIVE;
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300425 return 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300426}
427
428static void uvc_dismiss_privileges(struct uvc_fh *handle)
429{
430 if (handle->state == UVC_HANDLE_ACTIVE)
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300431 atomic_dec(&handle->stream->active);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300432
433 handle->state = UVC_HANDLE_PASSIVE;
434}
435
436static int uvc_has_privileges(struct uvc_fh *handle)
437{
438 return handle->state == UVC_HANDLE_ACTIVE;
439}
440
441/* ------------------------------------------------------------------------
442 * V4L2 file operations
443 */
444
Hans Verkuilbec43662008-12-30 06:58:20 -0300445static int uvc_v4l2_open(struct file *file)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300446{
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300447 struct uvc_streaming *stream;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300448 struct uvc_fh *handle;
449 int ret = 0;
450
451 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300452 stream = video_drvdata(file);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300453
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300454 if (stream->dev->state & UVC_DEV_DISCONNECTED)
455 return -ENODEV;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300456
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300457 ret = usb_autopm_get_interface(stream->dev->intf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300458 if (ret < 0)
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300459 return ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300460
461 /* Create the device handle. */
462 handle = kzalloc(sizeof *handle, GFP_KERNEL);
463 if (handle == NULL) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300464 usb_autopm_put_interface(stream->dev->intf);
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300465 return -ENOMEM;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300466 }
467
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300468 if (atomic_inc_return(&stream->dev->users) == 1) {
469 ret = uvc_status_start(stream->dev);
470 if (ret < 0) {
471 usb_autopm_put_interface(stream->dev->intf);
472 atomic_dec(&stream->dev->users);
Laurent Pinchart04a37e02009-05-19 10:08:03 -0300473 kfree(handle);
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300474 return ret;
Laurent Pinchart04a37e02009-05-19 10:08:03 -0300475 }
476 }
477
Laurent Pinchart8e113592009-07-01 20:24:47 -0300478 handle->chain = stream->chain;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300479 handle->stream = stream;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300480 handle->state = UVC_HANDLE_PASSIVE;
481 file->private_data = handle;
482
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300483 return 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300484}
485
Hans Verkuilbec43662008-12-30 06:58:20 -0300486static int uvc_v4l2_release(struct file *file)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300487{
Joe Perchesabf84382010-07-12 17:50:03 -0300488 struct uvc_fh *handle = file->private_data;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300489 struct uvc_streaming *stream = handle->stream;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300490
491 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");
492
493 /* Only free resources if this is a privileged handle. */
494 if (uvc_has_privileges(handle)) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300495 uvc_video_enable(stream, 0);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300496
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300497 mutex_lock(&stream->queue.mutex);
498 if (uvc_free_buffers(&stream->queue) < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300499 uvc_printk(KERN_ERR, "uvc_v4l2_release: Unable to "
500 "free buffers.\n");
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300501 mutex_unlock(&stream->queue.mutex);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300502 }
503
504 /* Release the file handle. */
505 uvc_dismiss_privileges(handle);
506 kfree(handle);
507 file->private_data = NULL;
508
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300509 if (atomic_dec_return(&stream->dev->users) == 0)
510 uvc_status_stop(stream->dev);
Laurent Pinchart04a37e02009-05-19 10:08:03 -0300511
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300512 usb_autopm_put_interface(stream->dev->intf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300513 return 0;
514}
515
Hans Verkuil069b7472008-12-30 07:04:34 -0300516static long uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300517{
518 struct video_device *vdev = video_devdata(file);
Joe Perchesabf84382010-07-12 17:50:03 -0300519 struct uvc_fh *handle = file->private_data;
Laurent Pinchart8e113592009-07-01 20:24:47 -0300520 struct uvc_video_chain *chain = handle->chain;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300521 struct uvc_streaming *stream = handle->stream;
Hans Verkuil069b7472008-12-30 07:04:34 -0300522 long ret = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300523
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300524 switch (cmd) {
525 /* Query capabilities */
526 case VIDIOC_QUERYCAP:
527 {
528 struct v4l2_capability *cap = arg;
529
530 memset(cap, 0, sizeof *cap);
Laurent Pinchartd0ebf302009-01-08 14:05:11 -0300531 strlcpy(cap->driver, "uvcvideo", sizeof cap->driver);
532 strlcpy(cap->card, vdev->name, sizeof cap->card);
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300533 usb_make_path(stream->dev->udev,
Thierry MERLEb12049a2009-01-27 16:53:23 -0300534 cap->bus_info, sizeof(cap->bus_info));
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300535 cap->version = DRIVER_VERSION_NUMBER;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300536 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
Laurent Pinchartff924202008-12-28 22:32:29 -0300537 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
538 | V4L2_CAP_STREAMING;
539 else
540 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT
541 | V4L2_CAP_STREAMING;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300542 break;
543 }
544
545 /* Get, Set & Query control */
546 case VIDIOC_QUERYCTRL:
Laurent Pinchart8e113592009-07-01 20:24:47 -0300547 return uvc_query_v4l2_ctrl(chain, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300548
549 case VIDIOC_G_CTRL:
550 {
551 struct v4l2_control *ctrl = arg;
552 struct v4l2_ext_control xctrl;
553
554 memset(&xctrl, 0, sizeof xctrl);
555 xctrl.id = ctrl->id;
556
Laurent Pinchart8e113592009-07-01 20:24:47 -0300557 ret = uvc_ctrl_begin(chain);
558 if (ret < 0)
Robert Krakora1fcbcc42009-06-12 13:51:03 -0300559 return ret;
560
Laurent Pinchart8e113592009-07-01 20:24:47 -0300561 ret = uvc_ctrl_get(chain, &xctrl);
562 uvc_ctrl_rollback(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300563 if (ret >= 0)
564 ctrl->value = xctrl.value;
565 break;
566 }
567
568 case VIDIOC_S_CTRL:
569 {
570 struct v4l2_control *ctrl = arg;
571 struct v4l2_ext_control xctrl;
572
573 memset(&xctrl, 0, sizeof xctrl);
574 xctrl.id = ctrl->id;
575 xctrl.value = ctrl->value;
576
Laurent Pinchart8d556622010-02-04 21:43:37 -0300577 ret = uvc_ctrl_begin(chain);
Laurent Pinchart8e113592009-07-01 20:24:47 -0300578 if (ret < 0)
Robert Krakora1fcbcc42009-06-12 13:51:03 -0300579 return ret;
580
Laurent Pinchart8e113592009-07-01 20:24:47 -0300581 ret = uvc_ctrl_set(chain, &xctrl);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300582 if (ret < 0) {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300583 uvc_ctrl_rollback(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300584 return ret;
585 }
Laurent Pinchart8e113592009-07-01 20:24:47 -0300586 ret = uvc_ctrl_commit(chain);
Laurent Pincharte54532e2010-01-23 07:07:53 -0300587 if (ret == 0)
588 ctrl->value = xctrl.value;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300589 break;
590 }
591
592 case VIDIOC_QUERYMENU:
Laurent Pinchart23d9f3e2010-11-21 07:58:54 -0300593 return uvc_query_v4l2_menu(chain, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300594
595 case VIDIOC_G_EXT_CTRLS:
596 {
597 struct v4l2_ext_controls *ctrls = arg;
598 struct v4l2_ext_control *ctrl = ctrls->controls;
599 unsigned int i;
600
Laurent Pinchart8e113592009-07-01 20:24:47 -0300601 ret = uvc_ctrl_begin(chain);
602 if (ret < 0)
Robert Krakora1fcbcc42009-06-12 13:51:03 -0300603 return ret;
604
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300605 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300606 ret = uvc_ctrl_get(chain, ctrl);
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 ctrls->error_idx = i;
610 return ret;
611 }
612 }
613 ctrls->error_idx = 0;
Laurent Pinchart8e113592009-07-01 20:24:47 -0300614 ret = uvc_ctrl_rollback(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300615 break;
616 }
617
618 case VIDIOC_S_EXT_CTRLS:
619 case VIDIOC_TRY_EXT_CTRLS:
620 {
621 struct v4l2_ext_controls *ctrls = arg;
622 struct v4l2_ext_control *ctrl = ctrls->controls;
623 unsigned int i;
624
Laurent Pinchart8e113592009-07-01 20:24:47 -0300625 ret = uvc_ctrl_begin(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300626 if (ret < 0)
627 return ret;
628
629 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300630 ret = uvc_ctrl_set(chain, ctrl);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300631 if (ret < 0) {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300632 uvc_ctrl_rollback(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300633 ctrls->error_idx = i;
634 return ret;
635 }
636 }
637
638 ctrls->error_idx = 0;
639
640 if (cmd == VIDIOC_S_EXT_CTRLS)
Laurent Pinchart8e113592009-07-01 20:24:47 -0300641 ret = uvc_ctrl_commit(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300642 else
Laurent Pinchart8e113592009-07-01 20:24:47 -0300643 ret = uvc_ctrl_rollback(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300644 break;
645 }
646
647 /* Get, Set & Enum input */
648 case VIDIOC_ENUMINPUT:
649 {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300650 const struct uvc_entity *selector = chain->selector;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300651 struct v4l2_input *input = arg;
652 struct uvc_entity *iterm = NULL;
653 u32 index = input->index;
654 int pin = 0;
655
656 if (selector == NULL ||
Laurent Pinchart8e113592009-07-01 20:24:47 -0300657 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300658 if (index != 0)
659 return -EINVAL;
Laurent Pinchart6241d8c2009-11-25 12:00:22 -0300660 list_for_each_entry(iterm, &chain->entities, chain) {
661 if (UVC_ENTITY_IS_ITERM(iterm))
662 break;
663 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300664 pin = iterm->id;
Laurent Pinchart8ca5a632009-11-25 12:00:30 -0300665 } else if (pin < selector->bNrInPins) {
666 pin = selector->baSourceID[index];
Laurent Pinchart6241d8c2009-11-25 12:00:22 -0300667 list_for_each_entry(iterm, &chain->entities, chain) {
668 if (!UVC_ENTITY_IS_ITERM(iterm))
669 continue;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300670 if (iterm->id == pin)
671 break;
672 }
673 }
674
675 if (iterm == NULL || iterm->id != pin)
676 return -EINVAL;
677
678 memset(input, 0, sizeof *input);
679 input->index = index;
Laurent Pinchartd0ebf302009-01-08 14:05:11 -0300680 strlcpy(input->name, iterm->name, sizeof input->name);
Laurent Pinchartb482d922009-06-26 11:39:42 -0300681 if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300682 input->type = V4L2_INPUT_TYPE_CAMERA;
683 break;
684 }
685
686 case VIDIOC_G_INPUT:
687 {
688 u8 input;
689
Laurent Pinchart8e113592009-07-01 20:24:47 -0300690 if (chain->selector == NULL ||
691 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300692 *(int *)arg = 0;
693 break;
694 }
695
Laurent Pinchart8e113592009-07-01 20:24:47 -0300696 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
697 chain->selector->id, chain->dev->intfnum,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300698 UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300699 if (ret < 0)
700 return ret;
701
702 *(int *)arg = input - 1;
703 break;
704 }
705
706 case VIDIOC_S_INPUT:
707 {
Márton Németh9086c7b992009-04-15 09:01:58 -0300708 u32 input = *(u32 *)arg + 1;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300709
710 if ((ret = uvc_acquire_privileges(handle)) < 0)
711 return ret;
712
Laurent Pinchart8e113592009-07-01 20:24:47 -0300713 if (chain->selector == NULL ||
714 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300715 if (input != 1)
716 return -EINVAL;
717 break;
718 }
719
Laurent Pinchart8ca5a632009-11-25 12:00:30 -0300720 if (input == 0 || input > chain->selector->bNrInPins)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300721 return -EINVAL;
722
Laurent Pinchart8e113592009-07-01 20:24:47 -0300723 return uvc_query_ctrl(chain->dev, UVC_SET_CUR,
724 chain->selector->id, chain->dev->intfnum,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300725 UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300726 }
727
728 /* Try, Get, Set & Enum format */
729 case VIDIOC_ENUM_FMT:
730 {
731 struct v4l2_fmtdesc *fmt = arg;
732 struct uvc_format *format;
Márton Németh8bbd90c2009-03-27 11:13:57 -0300733 enum v4l2_buf_type type = fmt->type;
734 __u32 index = fmt->index;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300735
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300736 if (fmt->type != stream->type ||
737 fmt->index >= stream->nformats)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300738 return -EINVAL;
739
Márton Németh8bbd90c2009-03-27 11:13:57 -0300740 memset(fmt, 0, sizeof(*fmt));
741 fmt->index = index;
742 fmt->type = type;
743
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300744 format = &stream->format[fmt->index];
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300745 fmt->flags = 0;
746 if (format->flags & UVC_FMT_FLAG_COMPRESSED)
747 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
Laurent Pinchartd0ebf302009-01-08 14:05:11 -0300748 strlcpy(fmt->description, format->name,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300749 sizeof fmt->description);
750 fmt->description[sizeof fmt->description - 1] = 0;
751 fmt->pixelformat = format->fcc;
752 break;
753 }
754
755 case VIDIOC_TRY_FMT:
756 {
757 struct uvc_streaming_control probe;
758
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300759 return uvc_v4l2_try_format(stream, arg, &probe, NULL, NULL);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300760 }
761
762 case VIDIOC_S_FMT:
763 if ((ret = uvc_acquire_privileges(handle)) < 0)
764 return ret;
765
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300766 return uvc_v4l2_set_format(stream, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300767
768 case VIDIOC_G_FMT:
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300769 return uvc_v4l2_get_format(stream, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300770
771 /* Frame size enumeration */
772 case VIDIOC_ENUM_FRAMESIZES:
773 {
774 struct v4l2_frmsizeenum *fsize = arg;
775 struct uvc_format *format = NULL;
776 struct uvc_frame *frame;
777 int i;
778
779 /* Look for the given pixel format */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300780 for (i = 0; i < stream->nformats; i++) {
781 if (stream->format[i].fcc ==
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300782 fsize->pixel_format) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300783 format = &stream->format[i];
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300784 break;
785 }
786 }
787 if (format == NULL)
788 return -EINVAL;
789
790 if (fsize->index >= format->nframes)
791 return -EINVAL;
792
793 frame = &format->frame[fsize->index];
794 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
795 fsize->discrete.width = frame->wWidth;
796 fsize->discrete.height = frame->wHeight;
797 break;
798 }
799
800 /* Frame interval enumeration */
801 case VIDIOC_ENUM_FRAMEINTERVALS:
802 {
803 struct v4l2_frmivalenum *fival = arg;
804 struct uvc_format *format = NULL;
805 struct uvc_frame *frame = NULL;
806 int i;
807
808 /* Look for the given pixel format and frame size */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300809 for (i = 0; i < stream->nformats; i++) {
810 if (stream->format[i].fcc ==
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300811 fival->pixel_format) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300812 format = &stream->format[i];
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300813 break;
814 }
815 }
816 if (format == NULL)
817 return -EINVAL;
818
819 for (i = 0; i < format->nframes; i++) {
820 if (format->frame[i].wWidth == fival->width &&
821 format->frame[i].wHeight == fival->height) {
822 frame = &format->frame[i];
823 break;
824 }
825 }
826 if (frame == NULL)
827 return -EINVAL;
828
829 if (frame->bFrameIntervalType) {
830 if (fival->index >= frame->bFrameIntervalType)
831 return -EINVAL;
832
833 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
834 fival->discrete.numerator =
835 frame->dwFrameInterval[fival->index];
836 fival->discrete.denominator = 10000000;
837 uvc_simplify_fraction(&fival->discrete.numerator,
838 &fival->discrete.denominator, 8, 333);
839 } else {
840 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
841 fival->stepwise.min.numerator =
842 frame->dwFrameInterval[0];
843 fival->stepwise.min.denominator = 10000000;
844 fival->stepwise.max.numerator =
845 frame->dwFrameInterval[1];
846 fival->stepwise.max.denominator = 10000000;
847 fival->stepwise.step.numerator =
848 frame->dwFrameInterval[2];
849 fival->stepwise.step.denominator = 10000000;
850 uvc_simplify_fraction(&fival->stepwise.min.numerator,
851 &fival->stepwise.min.denominator, 8, 333);
852 uvc_simplify_fraction(&fival->stepwise.max.numerator,
853 &fival->stepwise.max.denominator, 8, 333);
854 uvc_simplify_fraction(&fival->stepwise.step.numerator,
855 &fival->stepwise.step.denominator, 8, 333);
856 }
857 break;
858 }
859
860 /* Get & Set streaming parameters */
861 case VIDIOC_G_PARM:
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300862 return uvc_v4l2_get_streamparm(stream, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300863
864 case VIDIOC_S_PARM:
865 if ((ret = uvc_acquire_privileges(handle)) < 0)
866 return ret;
867
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300868 return uvc_v4l2_set_streamparm(stream, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300869
870 /* Cropping and scaling */
871 case VIDIOC_CROPCAP:
872 {
873 struct v4l2_cropcap *ccap = arg;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300874 struct uvc_frame *frame = stream->cur_frame;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300875
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300876 if (ccap->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300877 return -EINVAL;
878
879 ccap->bounds.left = 0;
880 ccap->bounds.top = 0;
881 ccap->bounds.width = frame->wWidth;
882 ccap->bounds.height = frame->wHeight;
883
884 ccap->defrect = ccap->bounds;
885
886 ccap->pixelaspect.numerator = 1;
887 ccap->pixelaspect.denominator = 1;
888 break;
889 }
890
891 case VIDIOC_G_CROP:
892 case VIDIOC_S_CROP:
893 return -EINVAL;
894
895 /* Buffers & streaming */
896 case VIDIOC_REQBUFS:
897 {
898 struct v4l2_requestbuffers *rb = arg;
899 unsigned int bufsize =
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300900 stream->ctrl.dwMaxVideoFrameSize;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300901
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300902 if (rb->type != stream->type ||
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300903 rb->memory != V4L2_MEMORY_MMAP)
904 return -EINVAL;
905
906 if ((ret = uvc_acquire_privileges(handle)) < 0)
907 return ret;
908
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300909 ret = uvc_alloc_buffers(&stream->queue, rb->count, bufsize);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300910 if (ret < 0)
911 return ret;
912
Laurent Pinchart1a969d92009-09-02 03:12:26 -0300913 if (ret == 0)
914 uvc_dismiss_privileges(handle);
915
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300916 rb->count = ret;
917 ret = 0;
918 break;
919 }
920
921 case VIDIOC_QUERYBUF:
922 {
923 struct v4l2_buffer *buf = arg;
924
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300925 if (buf->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300926 return -EINVAL;
927
928 if (!uvc_has_privileges(handle))
929 return -EBUSY;
930
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300931 return uvc_query_buffer(&stream->queue, buf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300932 }
933
934 case VIDIOC_QBUF:
935 if (!uvc_has_privileges(handle))
936 return -EBUSY;
937
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300938 return uvc_queue_buffer(&stream->queue, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300939
940 case VIDIOC_DQBUF:
941 if (!uvc_has_privileges(handle))
942 return -EBUSY;
943
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300944 return uvc_dequeue_buffer(&stream->queue, arg,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300945 file->f_flags & O_NONBLOCK);
946
947 case VIDIOC_STREAMON:
948 {
949 int *type = arg;
950
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300951 if (*type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300952 return -EINVAL;
953
954 if (!uvc_has_privileges(handle))
955 return -EBUSY;
956
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300957 ret = uvc_video_enable(stream, 1);
958 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300959 return ret;
960 break;
961 }
962
963 case VIDIOC_STREAMOFF:
964 {
965 int *type = arg;
966
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300967 if (*type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300968 return -EINVAL;
969
970 if (!uvc_has_privileges(handle))
971 return -EBUSY;
972
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300973 return uvc_video_enable(stream, 0);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300974 }
975
976 /* Analog video standards make no sense for digital cameras. */
977 case VIDIOC_ENUMSTD:
978 case VIDIOC_QUERYSTD:
979 case VIDIOC_G_STD:
980 case VIDIOC_S_STD:
981
982 case VIDIOC_OVERLAY:
983
984 case VIDIOC_ENUMAUDIO:
985 case VIDIOC_ENUMAUDOUT:
986
987 case VIDIOC_ENUMOUTPUT:
988 uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd);
989 return -EINVAL;
990
991 /* Dynamic controls. */
992 case UVCIOC_CTRL_ADD:
Laurent Pinchartba2fa992010-09-20 05:53:21 -0300993 /* Legacy ioctl, kept for API compatibility reasons */
Laurent Pinchartba2fa992010-09-20 05:53:21 -0300994 return -EEXIST;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300995
Laurent Pinchart561474c22010-02-18 16:38:52 -0300996 case UVCIOC_CTRL_MAP_OLD:
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300997 case UVCIOC_CTRL_MAP:
Laurent Pinchartba2fa992010-09-20 05:53:21 -0300998 return uvc_ioctl_ctrl_map(chain, arg,
999 cmd == UVCIOC_CTRL_MAP_OLD);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001000
1001 case UVCIOC_CTRL_GET:
Laurent Pinchart8e113592009-07-01 20:24:47 -03001002 return uvc_xu_ctrl_query(chain, arg, 0);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001003
1004 case UVCIOC_CTRL_SET:
Laurent Pinchart8e113592009-07-01 20:24:47 -03001005 return uvc_xu_ctrl_query(chain, arg, 1);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001006
1007 default:
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -03001008 if ((ret = v4l_compat_translate_ioctl(file, cmd, arg,
Hans Verkuilf473bf72008-11-01 08:25:11 -03001009 uvc_v4l2_do_ioctl)) == -ENOIOCTLCMD)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001010 uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n",
1011 cmd);
1012 return ret;
1013 }
1014
1015 return ret;
1016}
1017
Hans Verkuil069b7472008-12-30 07:04:34 -03001018static long uvc_v4l2_ioctl(struct file *file,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001019 unsigned int cmd, unsigned long arg)
1020{
Laurent Pinchart308bb522008-11-17 18:32:11 -03001021 if (uvc_trace_param & UVC_TRACE_IOCTL) {
1022 uvc_printk(KERN_DEBUG, "uvc_v4l2_ioctl(");
1023 v4l_printk_ioctl(cmd);
1024 printk(")\n");
1025 }
1026
Hans Verkuilf473bf72008-11-01 08:25:11 -03001027 return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001028}
1029
1030static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1031 size_t count, loff_t *ppos)
1032{
1033 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
Laurent Pinchart350d6402009-11-04 11:53:49 -03001034 return -EINVAL;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001035}
1036
1037/*
1038 * VMA operations.
1039 */
1040static void uvc_vm_open(struct vm_area_struct *vma)
1041{
1042 struct uvc_buffer *buffer = vma->vm_private_data;
1043 buffer->vma_use_count++;
1044}
1045
1046static void uvc_vm_close(struct vm_area_struct *vma)
1047{
1048 struct uvc_buffer *buffer = vma->vm_private_data;
1049 buffer->vma_use_count--;
1050}
1051
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001052static const struct vm_operations_struct uvc_vm_ops = {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001053 .open = uvc_vm_open,
1054 .close = uvc_vm_close,
1055};
1056
1057static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1058{
Joe Perchesabf84382010-07-12 17:50:03 -03001059 struct uvc_fh *handle = file->private_data;
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001060 struct uvc_streaming *stream = handle->stream;
1061 struct uvc_video_queue *queue = &stream->queue;
Andrew Morton6f80e1b2008-07-04 06:33:23 -03001062 struct uvc_buffer *uninitialized_var(buffer);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001063 struct page *page;
1064 unsigned long addr, start, size;
1065 unsigned int i;
1066 int ret = 0;
1067
1068 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
1069
1070 start = vma->vm_start;
1071 size = vma->vm_end - vma->vm_start;
1072
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001073 mutex_lock(&queue->mutex);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001074
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001075 for (i = 0; i < queue->count; ++i) {
1076 buffer = &queue->buffer[i];
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001077 if ((buffer->buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
1078 break;
1079 }
1080
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001081 if (i == queue->count || size != queue->buf_size) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001082 ret = -EINVAL;
1083 goto done;
1084 }
1085
1086 /*
1087 * VM_IO marks the area as being an mmaped region for I/O to a
1088 * device. It also prevents the region from being core dumped.
1089 */
1090 vma->vm_flags |= VM_IO;
1091
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001092 addr = (unsigned long)queue->mem + buffer->buf.m.offset;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001093 while (size > 0) {
1094 page = vmalloc_to_page((void *)addr);
1095 if ((ret = vm_insert_page(vma, start, page)) < 0)
1096 goto done;
1097
1098 start += PAGE_SIZE;
1099 addr += PAGE_SIZE;
1100 size -= PAGE_SIZE;
1101 }
1102
1103 vma->vm_ops = &uvc_vm_ops;
1104 vma->vm_private_data = buffer;
1105 uvc_vm_open(vma);
1106
1107done:
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001108 mutex_unlock(&queue->mutex);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001109 return ret;
1110}
1111
1112static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
1113{
Joe Perchesabf84382010-07-12 17:50:03 -03001114 struct uvc_fh *handle = file->private_data;
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001115 struct uvc_streaming *stream = handle->stream;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001116
1117 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
1118
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001119 return uvc_queue_poll(&stream->queue, file, wait);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001120}
1121
Hans Verkuilbec43662008-12-30 06:58:20 -03001122const struct v4l2_file_operations uvc_fops = {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001123 .owner = THIS_MODULE,
1124 .open = uvc_v4l2_open,
1125 .release = uvc_v4l2_release,
1126 .ioctl = uvc_v4l2_ioctl,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001127 .read = uvc_v4l2_read,
1128 .mmap = uvc_v4l2_mmap,
1129 .poll = uvc_v4l2_poll,
1130};
Hans Verkuilf87086e2008-07-18 00:50:58 -03001131