blob: 07dd2357fbb9c49bcd62f0ddf71188e13da274bf [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 if (uvc_free_buffers(&stream->queue) < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300498 uvc_printk(KERN_ERR, "uvc_v4l2_release: Unable to "
499 "free buffers.\n");
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300500 }
501
502 /* Release the file handle. */
503 uvc_dismiss_privileges(handle);
504 kfree(handle);
505 file->private_data = NULL;
506
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300507 if (atomic_dec_return(&stream->dev->users) == 0)
508 uvc_status_stop(stream->dev);
Laurent Pinchart04a37e02009-05-19 10:08:03 -0300509
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300510 usb_autopm_put_interface(stream->dev->intf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300511 return 0;
512}
513
Hans Verkuil069b7472008-12-30 07:04:34 -0300514static long uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300515{
516 struct video_device *vdev = video_devdata(file);
Joe Perchesabf84382010-07-12 17:50:03 -0300517 struct uvc_fh *handle = file->private_data;
Laurent Pinchart8e113592009-07-01 20:24:47 -0300518 struct uvc_video_chain *chain = handle->chain;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300519 struct uvc_streaming *stream = handle->stream;
Hans Verkuil069b7472008-12-30 07:04:34 -0300520 long ret = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300521
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300522 switch (cmd) {
523 /* Query capabilities */
524 case VIDIOC_QUERYCAP:
525 {
526 struct v4l2_capability *cap = arg;
527
528 memset(cap, 0, sizeof *cap);
Laurent Pinchartd0ebf302009-01-08 14:05:11 -0300529 strlcpy(cap->driver, "uvcvideo", sizeof cap->driver);
530 strlcpy(cap->card, vdev->name, sizeof cap->card);
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300531 usb_make_path(stream->dev->udev,
Thierry MERLEb12049a2009-01-27 16:53:23 -0300532 cap->bus_info, sizeof(cap->bus_info));
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300533 cap->version = DRIVER_VERSION_NUMBER;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300534 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
Laurent Pinchartff924202008-12-28 22:32:29 -0300535 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
536 | V4L2_CAP_STREAMING;
537 else
538 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT
539 | V4L2_CAP_STREAMING;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300540 break;
541 }
542
543 /* Get, Set & Query control */
544 case VIDIOC_QUERYCTRL:
Laurent Pinchart8e113592009-07-01 20:24:47 -0300545 return uvc_query_v4l2_ctrl(chain, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300546
547 case VIDIOC_G_CTRL:
548 {
549 struct v4l2_control *ctrl = arg;
550 struct v4l2_ext_control xctrl;
551
552 memset(&xctrl, 0, sizeof xctrl);
553 xctrl.id = ctrl->id;
554
Laurent Pinchart8e113592009-07-01 20:24:47 -0300555 ret = uvc_ctrl_begin(chain);
556 if (ret < 0)
Robert Krakora1fcbcc42009-06-12 13:51:03 -0300557 return ret;
558
Laurent Pinchart8e113592009-07-01 20:24:47 -0300559 ret = uvc_ctrl_get(chain, &xctrl);
560 uvc_ctrl_rollback(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300561 if (ret >= 0)
562 ctrl->value = xctrl.value;
563 break;
564 }
565
566 case VIDIOC_S_CTRL:
567 {
568 struct v4l2_control *ctrl = arg;
569 struct v4l2_ext_control xctrl;
570
571 memset(&xctrl, 0, sizeof xctrl);
572 xctrl.id = ctrl->id;
573 xctrl.value = ctrl->value;
574
Laurent Pinchart8d556622010-02-04 21:43:37 -0300575 ret = uvc_ctrl_begin(chain);
Laurent Pinchart8e113592009-07-01 20:24:47 -0300576 if (ret < 0)
Robert Krakora1fcbcc42009-06-12 13:51:03 -0300577 return ret;
578
Laurent Pinchart8e113592009-07-01 20:24:47 -0300579 ret = uvc_ctrl_set(chain, &xctrl);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300580 if (ret < 0) {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300581 uvc_ctrl_rollback(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300582 return ret;
583 }
Laurent Pinchart8e113592009-07-01 20:24:47 -0300584 ret = uvc_ctrl_commit(chain);
Laurent Pincharte54532e2010-01-23 07:07:53 -0300585 if (ret == 0)
586 ctrl->value = xctrl.value;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300587 break;
588 }
589
590 case VIDIOC_QUERYMENU:
Laurent Pinchart23d9f3e2010-11-21 07:58:54 -0300591 return uvc_query_v4l2_menu(chain, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300592
593 case VIDIOC_G_EXT_CTRLS:
594 {
595 struct v4l2_ext_controls *ctrls = arg;
596 struct v4l2_ext_control *ctrl = ctrls->controls;
597 unsigned int i;
598
Laurent Pinchart8e113592009-07-01 20:24:47 -0300599 ret = uvc_ctrl_begin(chain);
600 if (ret < 0)
Robert Krakora1fcbcc42009-06-12 13:51:03 -0300601 return ret;
602
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300603 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300604 ret = uvc_ctrl_get(chain, ctrl);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300605 if (ret < 0) {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300606 uvc_ctrl_rollback(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300607 ctrls->error_idx = i;
608 return ret;
609 }
610 }
611 ctrls->error_idx = 0;
Laurent Pinchart8e113592009-07-01 20:24:47 -0300612 ret = uvc_ctrl_rollback(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300613 break;
614 }
615
616 case VIDIOC_S_EXT_CTRLS:
617 case VIDIOC_TRY_EXT_CTRLS:
618 {
619 struct v4l2_ext_controls *ctrls = arg;
620 struct v4l2_ext_control *ctrl = ctrls->controls;
621 unsigned int i;
622
Laurent Pinchart8e113592009-07-01 20:24:47 -0300623 ret = uvc_ctrl_begin(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300624 if (ret < 0)
625 return ret;
626
627 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300628 ret = uvc_ctrl_set(chain, ctrl);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300629 if (ret < 0) {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300630 uvc_ctrl_rollback(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300631 ctrls->error_idx = i;
632 return ret;
633 }
634 }
635
636 ctrls->error_idx = 0;
637
638 if (cmd == VIDIOC_S_EXT_CTRLS)
Laurent Pinchart8e113592009-07-01 20:24:47 -0300639 ret = uvc_ctrl_commit(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300640 else
Laurent Pinchart8e113592009-07-01 20:24:47 -0300641 ret = uvc_ctrl_rollback(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300642 break;
643 }
644
645 /* Get, Set & Enum input */
646 case VIDIOC_ENUMINPUT:
647 {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300648 const struct uvc_entity *selector = chain->selector;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300649 struct v4l2_input *input = arg;
650 struct uvc_entity *iterm = NULL;
651 u32 index = input->index;
652 int pin = 0;
653
654 if (selector == NULL ||
Laurent Pinchart8e113592009-07-01 20:24:47 -0300655 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300656 if (index != 0)
657 return -EINVAL;
Laurent Pinchart6241d8c2009-11-25 12:00:22 -0300658 list_for_each_entry(iterm, &chain->entities, chain) {
659 if (UVC_ENTITY_IS_ITERM(iterm))
660 break;
661 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300662 pin = iterm->id;
Laurent Pinchart8ca5a632009-11-25 12:00:30 -0300663 } else if (pin < selector->bNrInPins) {
664 pin = selector->baSourceID[index];
Laurent Pinchart6241d8c2009-11-25 12:00:22 -0300665 list_for_each_entry(iterm, &chain->entities, chain) {
666 if (!UVC_ENTITY_IS_ITERM(iterm))
667 continue;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300668 if (iterm->id == pin)
669 break;
670 }
671 }
672
673 if (iterm == NULL || iterm->id != pin)
674 return -EINVAL;
675
676 memset(input, 0, sizeof *input);
677 input->index = index;
Laurent Pinchartd0ebf302009-01-08 14:05:11 -0300678 strlcpy(input->name, iterm->name, sizeof input->name);
Laurent Pinchartb482d922009-06-26 11:39:42 -0300679 if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300680 input->type = V4L2_INPUT_TYPE_CAMERA;
681 break;
682 }
683
684 case VIDIOC_G_INPUT:
685 {
686 u8 input;
687
Laurent Pinchart8e113592009-07-01 20:24:47 -0300688 if (chain->selector == NULL ||
689 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300690 *(int *)arg = 0;
691 break;
692 }
693
Laurent Pinchart8e113592009-07-01 20:24:47 -0300694 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
695 chain->selector->id, chain->dev->intfnum,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300696 UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300697 if (ret < 0)
698 return ret;
699
700 *(int *)arg = input - 1;
701 break;
702 }
703
704 case VIDIOC_S_INPUT:
705 {
Márton Németh9086c7b992009-04-15 09:01:58 -0300706 u32 input = *(u32 *)arg + 1;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300707
708 if ((ret = uvc_acquire_privileges(handle)) < 0)
709 return ret;
710
Laurent Pinchart8e113592009-07-01 20:24:47 -0300711 if (chain->selector == NULL ||
712 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300713 if (input != 1)
714 return -EINVAL;
715 break;
716 }
717
Laurent Pinchart8ca5a632009-11-25 12:00:30 -0300718 if (input == 0 || input > chain->selector->bNrInPins)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300719 return -EINVAL;
720
Laurent Pinchart8e113592009-07-01 20:24:47 -0300721 return uvc_query_ctrl(chain->dev, UVC_SET_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 }
725
726 /* Try, Get, Set & Enum format */
727 case VIDIOC_ENUM_FMT:
728 {
729 struct v4l2_fmtdesc *fmt = arg;
730 struct uvc_format *format;
Márton Németh8bbd90c2009-03-27 11:13:57 -0300731 enum v4l2_buf_type type = fmt->type;
732 __u32 index = fmt->index;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300733
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300734 if (fmt->type != stream->type ||
735 fmt->index >= stream->nformats)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300736 return -EINVAL;
737
Márton Németh8bbd90c2009-03-27 11:13:57 -0300738 memset(fmt, 0, sizeof(*fmt));
739 fmt->index = index;
740 fmt->type = type;
741
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300742 format = &stream->format[fmt->index];
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300743 fmt->flags = 0;
744 if (format->flags & UVC_FMT_FLAG_COMPRESSED)
745 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
Laurent Pinchartd0ebf302009-01-08 14:05:11 -0300746 strlcpy(fmt->description, format->name,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300747 sizeof fmt->description);
748 fmt->description[sizeof fmt->description - 1] = 0;
749 fmt->pixelformat = format->fcc;
750 break;
751 }
752
753 case VIDIOC_TRY_FMT:
754 {
755 struct uvc_streaming_control probe;
756
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300757 return uvc_v4l2_try_format(stream, arg, &probe, NULL, NULL);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300758 }
759
760 case VIDIOC_S_FMT:
761 if ((ret = uvc_acquire_privileges(handle)) < 0)
762 return ret;
763
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300764 return uvc_v4l2_set_format(stream, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300765
766 case VIDIOC_G_FMT:
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300767 return uvc_v4l2_get_format(stream, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300768
769 /* Frame size enumeration */
770 case VIDIOC_ENUM_FRAMESIZES:
771 {
772 struct v4l2_frmsizeenum *fsize = arg;
773 struct uvc_format *format = NULL;
774 struct uvc_frame *frame;
775 int i;
776
777 /* Look for the given pixel format */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300778 for (i = 0; i < stream->nformats; i++) {
779 if (stream->format[i].fcc ==
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300780 fsize->pixel_format) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300781 format = &stream->format[i];
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300782 break;
783 }
784 }
785 if (format == NULL)
786 return -EINVAL;
787
788 if (fsize->index >= format->nframes)
789 return -EINVAL;
790
791 frame = &format->frame[fsize->index];
792 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
793 fsize->discrete.width = frame->wWidth;
794 fsize->discrete.height = frame->wHeight;
795 break;
796 }
797
798 /* Frame interval enumeration */
799 case VIDIOC_ENUM_FRAMEINTERVALS:
800 {
801 struct v4l2_frmivalenum *fival = arg;
802 struct uvc_format *format = NULL;
803 struct uvc_frame *frame = NULL;
804 int i;
805
806 /* Look for the given pixel format and frame size */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300807 for (i = 0; i < stream->nformats; i++) {
808 if (stream->format[i].fcc ==
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300809 fival->pixel_format) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300810 format = &stream->format[i];
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300811 break;
812 }
813 }
814 if (format == NULL)
815 return -EINVAL;
816
817 for (i = 0; i < format->nframes; i++) {
818 if (format->frame[i].wWidth == fival->width &&
819 format->frame[i].wHeight == fival->height) {
820 frame = &format->frame[i];
821 break;
822 }
823 }
824 if (frame == NULL)
825 return -EINVAL;
826
827 if (frame->bFrameIntervalType) {
828 if (fival->index >= frame->bFrameIntervalType)
829 return -EINVAL;
830
831 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
832 fival->discrete.numerator =
833 frame->dwFrameInterval[fival->index];
834 fival->discrete.denominator = 10000000;
835 uvc_simplify_fraction(&fival->discrete.numerator,
836 &fival->discrete.denominator, 8, 333);
837 } else {
838 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
839 fival->stepwise.min.numerator =
840 frame->dwFrameInterval[0];
841 fival->stepwise.min.denominator = 10000000;
842 fival->stepwise.max.numerator =
843 frame->dwFrameInterval[1];
844 fival->stepwise.max.denominator = 10000000;
845 fival->stepwise.step.numerator =
846 frame->dwFrameInterval[2];
847 fival->stepwise.step.denominator = 10000000;
848 uvc_simplify_fraction(&fival->stepwise.min.numerator,
849 &fival->stepwise.min.denominator, 8, 333);
850 uvc_simplify_fraction(&fival->stepwise.max.numerator,
851 &fival->stepwise.max.denominator, 8, 333);
852 uvc_simplify_fraction(&fival->stepwise.step.numerator,
853 &fival->stepwise.step.denominator, 8, 333);
854 }
855 break;
856 }
857
858 /* Get & Set streaming parameters */
859 case VIDIOC_G_PARM:
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300860 return uvc_v4l2_get_streamparm(stream, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300861
862 case VIDIOC_S_PARM:
863 if ((ret = uvc_acquire_privileges(handle)) < 0)
864 return ret;
865
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300866 return uvc_v4l2_set_streamparm(stream, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300867
868 /* Cropping and scaling */
869 case VIDIOC_CROPCAP:
870 {
871 struct v4l2_cropcap *ccap = arg;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300872 struct uvc_frame *frame = stream->cur_frame;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300873
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300874 if (ccap->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300875 return -EINVAL;
876
877 ccap->bounds.left = 0;
878 ccap->bounds.top = 0;
879 ccap->bounds.width = frame->wWidth;
880 ccap->bounds.height = frame->wHeight;
881
882 ccap->defrect = ccap->bounds;
883
884 ccap->pixelaspect.numerator = 1;
885 ccap->pixelaspect.denominator = 1;
886 break;
887 }
888
889 case VIDIOC_G_CROP:
890 case VIDIOC_S_CROP:
891 return -EINVAL;
892
893 /* Buffers & streaming */
894 case VIDIOC_REQBUFS:
895 {
896 struct v4l2_requestbuffers *rb = arg;
897 unsigned int bufsize =
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300898 stream->ctrl.dwMaxVideoFrameSize;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300899
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300900 if (rb->type != stream->type ||
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300901 rb->memory != V4L2_MEMORY_MMAP)
902 return -EINVAL;
903
904 if ((ret = uvc_acquire_privileges(handle)) < 0)
905 return ret;
906
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300907 ret = uvc_alloc_buffers(&stream->queue, rb->count, bufsize);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300908 if (ret < 0)
909 return ret;
910
Laurent Pinchart1a969d92009-09-02 03:12:26 -0300911 if (ret == 0)
912 uvc_dismiss_privileges(handle);
913
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300914 rb->count = ret;
915 ret = 0;
916 break;
917 }
918
919 case VIDIOC_QUERYBUF:
920 {
921 struct v4l2_buffer *buf = arg;
922
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300923 if (buf->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300924 return -EINVAL;
925
926 if (!uvc_has_privileges(handle))
927 return -EBUSY;
928
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300929 return uvc_query_buffer(&stream->queue, buf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300930 }
931
932 case VIDIOC_QBUF:
933 if (!uvc_has_privileges(handle))
934 return -EBUSY;
935
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300936 return uvc_queue_buffer(&stream->queue, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300937
938 case VIDIOC_DQBUF:
939 if (!uvc_has_privileges(handle))
940 return -EBUSY;
941
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300942 return uvc_dequeue_buffer(&stream->queue, arg,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300943 file->f_flags & O_NONBLOCK);
944
945 case VIDIOC_STREAMON:
946 {
947 int *type = arg;
948
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300949 if (*type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300950 return -EINVAL;
951
952 if (!uvc_has_privileges(handle))
953 return -EBUSY;
954
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300955 ret = uvc_video_enable(stream, 1);
956 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300957 return ret;
958 break;
959 }
960
961 case VIDIOC_STREAMOFF:
962 {
963 int *type = arg;
964
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300965 if (*type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300966 return -EINVAL;
967
968 if (!uvc_has_privileges(handle))
969 return -EBUSY;
970
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300971 return uvc_video_enable(stream, 0);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300972 }
973
974 /* Analog video standards make no sense for digital cameras. */
975 case VIDIOC_ENUMSTD:
976 case VIDIOC_QUERYSTD:
977 case VIDIOC_G_STD:
978 case VIDIOC_S_STD:
979
980 case VIDIOC_OVERLAY:
981
982 case VIDIOC_ENUMAUDIO:
983 case VIDIOC_ENUMAUDOUT:
984
985 case VIDIOC_ENUMOUTPUT:
986 uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd);
987 return -EINVAL;
988
989 /* Dynamic controls. */
990 case UVCIOC_CTRL_ADD:
Laurent Pinchartba2fa992010-09-20 05:53:21 -0300991 /* Legacy ioctl, kept for API compatibility reasons */
Laurent Pinchartba2fa992010-09-20 05:53:21 -0300992 return -EEXIST;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300993
Laurent Pinchart561474c22010-02-18 16:38:52 -0300994 case UVCIOC_CTRL_MAP_OLD:
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300995 case UVCIOC_CTRL_MAP:
Laurent Pinchartba2fa992010-09-20 05:53:21 -0300996 return uvc_ioctl_ctrl_map(chain, arg,
997 cmd == UVCIOC_CTRL_MAP_OLD);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300998
999 case UVCIOC_CTRL_GET:
Laurent Pinchart8e113592009-07-01 20:24:47 -03001000 return uvc_xu_ctrl_query(chain, arg, 0);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001001
1002 case UVCIOC_CTRL_SET:
Laurent Pinchart8e113592009-07-01 20:24:47 -03001003 return uvc_xu_ctrl_query(chain, arg, 1);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001004
1005 default:
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -03001006 if ((ret = v4l_compat_translate_ioctl(file, cmd, arg,
Hans Verkuilf473bf72008-11-01 08:25:11 -03001007 uvc_v4l2_do_ioctl)) == -ENOIOCTLCMD)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001008 uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n",
1009 cmd);
1010 return ret;
1011 }
1012
1013 return ret;
1014}
1015
Hans Verkuil069b7472008-12-30 07:04:34 -03001016static long uvc_v4l2_ioctl(struct file *file,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001017 unsigned int cmd, unsigned long arg)
1018{
Laurent Pinchart308bb522008-11-17 18:32:11 -03001019 if (uvc_trace_param & UVC_TRACE_IOCTL) {
1020 uvc_printk(KERN_DEBUG, "uvc_v4l2_ioctl(");
1021 v4l_printk_ioctl(cmd);
1022 printk(")\n");
1023 }
1024
Hans Verkuilf473bf72008-11-01 08:25:11 -03001025 return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001026}
1027
1028static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1029 size_t count, loff_t *ppos)
1030{
1031 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
Laurent Pinchart350d6402009-11-04 11:53:49 -03001032 return -EINVAL;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001033}
1034
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001035static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1036{
Joe Perchesabf84382010-07-12 17:50:03 -03001037 struct uvc_fh *handle = file->private_data;
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001038 struct uvc_streaming *stream = handle->stream;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001039
1040 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
1041
Laurent Pinchart4aa27592010-11-21 15:18:08 -03001042 return uvc_queue_mmap(&stream->queue, vma);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001043}
1044
1045static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
1046{
Joe Perchesabf84382010-07-12 17:50:03 -03001047 struct uvc_fh *handle = file->private_data;
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001048 struct uvc_streaming *stream = handle->stream;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001049
1050 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
1051
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001052 return uvc_queue_poll(&stream->queue, file, wait);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001053}
1054
Hans Verkuilbec43662008-12-30 06:58:20 -03001055const struct v4l2_file_operations uvc_fops = {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001056 .owner = THIS_MODULE,
1057 .open = uvc_v4l2_open,
1058 .release = uvc_v4l2_release,
1059 .ioctl = uvc_v4l2_ioctl,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001060 .read = uvc_v4l2_read,
1061 .mmap = uvc_v4l2_mmap,
1062 .poll = uvc_v4l2_poll,
1063};
Hans Verkuilf87086e2008-07-18 00:50:58 -03001064