blob: b1dc3e507fc12a93a4d4bd9f0945eee4ba6ba9fe [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>
Arun Sharma600634972011-07-26 16:09:06 -070024#include <linux/atomic.h>
Laurent Pinchartc0efd232008-06-30 15:04:50 -030025
26#include <media/v4l2-common.h>
Hans 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,
Laurent Pinchart227bd5b2011-07-30 16:19:49 -030035 struct uvc_xu_control_mapping *xmap)
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:
Laurent Pinchart561474c22010-02-18 16:38:52 -030061 size = xmap->menu_count * sizeof(*map->menu_info);
62 map->menu_info = kmalloc(size, GFP_KERNEL);
63 if (map->menu_info == NULL) {
64 ret = -ENOMEM;
65 goto done;
66 }
67
68 if (copy_from_user(map->menu_info, xmap->menu_info, size)) {
69 ret = -EFAULT;
70 goto done;
71 }
72
73 map->menu_count = xmap->menu_count;
74 break;
75
76 default:
Laurent Pinchartba2fa992010-09-20 05:53:21 -030077 uvc_trace(UVC_TRACE_CONTROL, "Unsupported V4L2 control type "
78 "%u.\n", xmap->v4l2_type);
Mauro Carvalho Chehab7a286cc2011-06-26 10:18:03 -030079 ret = -ENOTTY;
Laurent Pinchart561474c22010-02-18 16:38:52 -030080 goto done;
81 }
82
Laurent Pinchartba2fa992010-09-20 05:53:21 -030083 ret = uvc_ctrl_add_mapping(chain, map);
Laurent Pinchart561474c22010-02-18 16:38:52 -030084
85done:
Laurent Pinchartba2fa992010-09-20 05:53:21 -030086 kfree(map->menu_info);
87 kfree(map);
Laurent Pinchart561474c22010-02-18 16:38:52 -030088
89 return ret;
90}
91
92/* ------------------------------------------------------------------------
Laurent Pinchartc0efd232008-06-30 15:04:50 -030093 * V4L2 interface
94 */
95
96/*
Laurent Pinchartc0efd232008-06-30 15:04:50 -030097 * Find the frame interval closest to the requested frame interval for the
98 * given frame format and size. This should be done by the device as part of
99 * the Video Probe and Commit negotiation, but some hardware don't implement
100 * that feature.
101 */
102static __u32 uvc_try_frame_interval(struct uvc_frame *frame, __u32 interval)
103{
104 unsigned int i;
105
106 if (frame->bFrameIntervalType) {
107 __u32 best = -1, dist;
108
109 for (i = 0; i < frame->bFrameIntervalType; ++i) {
110 dist = interval > frame->dwFrameInterval[i]
111 ? interval - frame->dwFrameInterval[i]
112 : frame->dwFrameInterval[i] - interval;
113
114 if (dist > best)
115 break;
116
117 best = dist;
118 }
119
120 interval = frame->dwFrameInterval[i-1];
121 } else {
122 const __u32 min = frame->dwFrameInterval[0];
123 const __u32 max = frame->dwFrameInterval[1];
124 const __u32 step = frame->dwFrameInterval[2];
125
126 interval = min + (interval - min + step/2) / step * step;
127 if (interval > max)
128 interval = max;
129 }
130
131 return interval;
132}
133
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300134static int uvc_v4l2_try_format(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300135 struct v4l2_format *fmt, struct uvc_streaming_control *probe,
136 struct uvc_format **uvc_format, struct uvc_frame **uvc_frame)
137{
138 struct uvc_format *format = NULL;
139 struct uvc_frame *frame = NULL;
140 __u16 rw, rh;
141 unsigned int d, maxd;
142 unsigned int i;
143 __u32 interval;
144 int ret = 0;
145 __u8 *fcc;
146
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300147 if (fmt->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300148 return -EINVAL;
149
150 fcc = (__u8 *)&fmt->fmt.pix.pixelformat;
151 uvc_trace(UVC_TRACE_FORMAT, "Trying format 0x%08x (%c%c%c%c): %ux%u.\n",
152 fmt->fmt.pix.pixelformat,
153 fcc[0], fcc[1], fcc[2], fcc[3],
154 fmt->fmt.pix.width, fmt->fmt.pix.height);
155
156 /* Check if the hardware supports the requested format. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300157 for (i = 0; i < stream->nformats; ++i) {
158 format = &stream->format[i];
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300159 if (format->fcc == fmt->fmt.pix.pixelformat)
160 break;
161 }
162
163 if (format == NULL || format->fcc != fmt->fmt.pix.pixelformat) {
164 uvc_trace(UVC_TRACE_FORMAT, "Unsupported format 0x%08x.\n",
165 fmt->fmt.pix.pixelformat);
166 return -EINVAL;
167 }
168
169 /* Find the closest image size. The distance between image sizes is
170 * the size in pixels of the non-overlapping regions between the
171 * requested size and the frame-specified size.
172 */
173 rw = fmt->fmt.pix.width;
174 rh = fmt->fmt.pix.height;
175 maxd = (unsigned int)-1;
176
177 for (i = 0; i < format->nframes; ++i) {
178 __u16 w = format->frame[i].wWidth;
179 __u16 h = format->frame[i].wHeight;
180
181 d = min(w, rw) * min(h, rh);
182 d = w*h + rw*rh - 2*d;
183 if (d < maxd) {
184 maxd = d;
185 frame = &format->frame[i];
186 }
187
188 if (maxd == 0)
189 break;
190 }
191
192 if (frame == NULL) {
193 uvc_trace(UVC_TRACE_FORMAT, "Unsupported size %ux%u.\n",
194 fmt->fmt.pix.width, fmt->fmt.pix.height);
195 return -EINVAL;
196 }
197
198 /* Use the default frame interval. */
199 interval = frame->dwDefaultFrameInterval;
200 uvc_trace(UVC_TRACE_FORMAT, "Using default frame interval %u.%u us "
201 "(%u.%u fps).\n", interval/10, interval%10, 10000000/interval,
202 (100000000/interval)%10);
203
204 /* Set the format index, frame index and frame interval. */
205 memset(probe, 0, sizeof *probe);
206 probe->bmHint = 1; /* dwFrameInterval */
207 probe->bFormatIndex = format->index;
208 probe->bFrameIndex = frame->bFrameIndex;
209 probe->dwFrameInterval = uvc_try_frame_interval(frame, interval);
210 /* Some webcams stall the probe control set request when the
211 * dwMaxVideoFrameSize field is set to zero. The UVC specification
212 * clearly states that the field is read-only from the host, so this
213 * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by
214 * the webcam to work around the problem.
215 *
216 * The workaround could probably be enabled for all webcams, so the
217 * quirk can be removed if needed. It's currently useful to detect
218 * webcam bugs and fix them before they hit the market (providing
219 * developers test their webcams with the Linux driver as well as with
220 * the Windows driver).
221 */
Laurent Pinchart69477562010-11-21 13:36:34 -0300222 mutex_lock(&stream->mutex);
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300223 if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300224 probe->dwMaxVideoFrameSize =
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300225 stream->ctrl.dwMaxVideoFrameSize;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300226
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300227 /* Probe the device. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300228 ret = uvc_probe_video(stream, probe);
Laurent Pinchart69477562010-11-21 13:36:34 -0300229 mutex_unlock(&stream->mutex);
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300230 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300231 goto done;
232
233 fmt->fmt.pix.width = frame->wWidth;
234 fmt->fmt.pix.height = frame->wHeight;
235 fmt->fmt.pix.field = V4L2_FIELD_NONE;
236 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
237 fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize;
238 fmt->fmt.pix.colorspace = format->colorspace;
239 fmt->fmt.pix.priv = 0;
240
241 if (uvc_format != NULL)
242 *uvc_format = format;
243 if (uvc_frame != NULL)
244 *uvc_frame = frame;
245
246done:
247 return ret;
248}
249
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300250static int uvc_v4l2_get_format(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300251 struct v4l2_format *fmt)
252{
Laurent Pinchart69477562010-11-21 13:36:34 -0300253 struct uvc_format *format;
254 struct uvc_frame *frame;
255 int ret = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300256
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300257 if (fmt->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300258 return -EINVAL;
259
Laurent Pinchart69477562010-11-21 13:36:34 -0300260 mutex_lock(&stream->mutex);
261 format = stream->cur_format;
262 frame = stream->cur_frame;
263
264 if (format == NULL || frame == NULL) {
265 ret = -EINVAL;
266 goto done;
267 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300268
269 fmt->fmt.pix.pixelformat = format->fcc;
270 fmt->fmt.pix.width = frame->wWidth;
271 fmt->fmt.pix.height = frame->wHeight;
272 fmt->fmt.pix.field = V4L2_FIELD_NONE;
273 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300274 fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300275 fmt->fmt.pix.colorspace = format->colorspace;
276 fmt->fmt.pix.priv = 0;
277
Laurent Pinchart69477562010-11-21 13:36:34 -0300278done:
279 mutex_unlock(&stream->mutex);
280 return ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300281}
282
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300283static int uvc_v4l2_set_format(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300284 struct v4l2_format *fmt)
285{
286 struct uvc_streaming_control probe;
287 struct uvc_format *format;
288 struct uvc_frame *frame;
289 int ret;
290
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300291 if (fmt->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300292 return -EINVAL;
293
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300294 ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300295 if (ret < 0)
296 return ret;
297
Laurent Pinchart69477562010-11-21 13:36:34 -0300298 mutex_lock(&stream->mutex);
299
300 if (uvc_queue_allocated(&stream->queue)) {
301 ret = -EBUSY;
302 goto done;
303 }
304
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300305 memcpy(&stream->ctrl, &probe, sizeof probe);
306 stream->cur_format = format;
307 stream->cur_frame = frame;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300308
Laurent Pinchart69477562010-11-21 13:36:34 -0300309done:
310 mutex_unlock(&stream->mutex);
311 return ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300312}
313
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300314static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300315 struct v4l2_streamparm *parm)
316{
317 uint32_t numerator, denominator;
318
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300319 if (parm->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300320 return -EINVAL;
321
Laurent Pinchart69477562010-11-21 13:36:34 -0300322 mutex_lock(&stream->mutex);
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300323 numerator = stream->ctrl.dwFrameInterval;
Laurent Pinchart69477562010-11-21 13:36:34 -0300324 mutex_unlock(&stream->mutex);
325
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300326 denominator = 10000000;
327 uvc_simplify_fraction(&numerator, &denominator, 8, 333);
328
329 memset(parm, 0, sizeof *parm);
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300330 parm->type = stream->type;
Laurent Pinchartff924202008-12-28 22:32:29 -0300331
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300332 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Laurent Pinchartff924202008-12-28 22:32:29 -0300333 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
334 parm->parm.capture.capturemode = 0;
335 parm->parm.capture.timeperframe.numerator = numerator;
336 parm->parm.capture.timeperframe.denominator = denominator;
337 parm->parm.capture.extendedmode = 0;
338 parm->parm.capture.readbuffers = 0;
339 } else {
340 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
341 parm->parm.output.outputmode = 0;
342 parm->parm.output.timeperframe.numerator = numerator;
343 parm->parm.output.timeperframe.denominator = denominator;
344 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300345
346 return 0;
347}
348
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300349static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300350 struct v4l2_streamparm *parm)
351{
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300352 struct uvc_streaming_control probe;
Laurent Pinchartff924202008-12-28 22:32:29 -0300353 struct v4l2_fract timeperframe;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300354 uint32_t interval;
355 int ret;
356
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300357 if (parm->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300358 return -EINVAL;
359
Laurent Pinchartff924202008-12-28 22:32:29 -0300360 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
361 timeperframe = parm->parm.capture.timeperframe;
362 else
363 timeperframe = parm->parm.output.timeperframe;
364
Laurent Pinchartff924202008-12-28 22:32:29 -0300365 interval = uvc_fraction_to_interval(timeperframe.numerator,
366 timeperframe.denominator);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300367 uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
Laurent Pinchartff924202008-12-28 22:32:29 -0300368 timeperframe.numerator, timeperframe.denominator, interval);
Laurent Pinchart69477562010-11-21 13:36:34 -0300369
370 mutex_lock(&stream->mutex);
371
372 if (uvc_queue_streaming(&stream->queue)) {
373 mutex_unlock(&stream->mutex);
374 return -EBUSY;
375 }
376
377 memcpy(&probe, &stream->ctrl, sizeof probe);
378 probe.dwFrameInterval =
379 uvc_try_frame_interval(stream->cur_frame, interval);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300380
381 /* Probe the device with the new settings. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300382 ret = uvc_probe_video(stream, &probe);
Laurent Pinchart69477562010-11-21 13:36:34 -0300383 if (ret < 0) {
384 mutex_unlock(&stream->mutex);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300385 return ret;
Laurent Pinchart69477562010-11-21 13:36:34 -0300386 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300387
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300388 memcpy(&stream->ctrl, &probe, sizeof probe);
Laurent Pinchart69477562010-11-21 13:36:34 -0300389 mutex_unlock(&stream->mutex);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300390
391 /* Return the actual frame period. */
Laurent Pinchartff924202008-12-28 22:32:29 -0300392 timeperframe.numerator = probe.dwFrameInterval;
393 timeperframe.denominator = 10000000;
394 uvc_simplify_fraction(&timeperframe.numerator,
395 &timeperframe.denominator, 8, 333);
396
397 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
398 parm->parm.capture.timeperframe = timeperframe;
399 else
400 parm->parm.output.timeperframe = timeperframe;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300401
402 return 0;
403}
404
405/* ------------------------------------------------------------------------
406 * Privilege management
407 */
408
409/*
410 * Privilege management is the multiple-open implementation basis. The current
411 * implementation is completely transparent for the end-user and doesn't
412 * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls.
413 * Those ioctls enable finer control on the device (by making possible for a
414 * user to request exclusive access to a device), but are not mature yet.
415 * Switching to the V4L2 priority mechanism might be considered in the future
416 * if this situation changes.
417 *
418 * Each open instance of a UVC device can either be in a privileged or
419 * unprivileged state. Only a single instance can be in a privileged state at
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300420 * a given time. Trying to perform an operation that requires privileges will
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300421 * automatically acquire the required privileges if possible, or return -EBUSY
Laurent Pinchart1a969d92009-09-02 03:12:26 -0300422 * otherwise. Privileges are dismissed when closing the instance or when
423 * freeing the video buffers using VIDIOC_REQBUFS.
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300424 *
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300425 * Operations that require privileges are:
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300426 *
427 * - VIDIOC_S_INPUT
428 * - VIDIOC_S_PARM
429 * - VIDIOC_S_FMT
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300430 * - VIDIOC_REQBUFS
431 */
432static int uvc_acquire_privileges(struct uvc_fh *handle)
433{
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300434 /* Always succeed if the handle is already privileged. */
435 if (handle->state == UVC_HANDLE_ACTIVE)
436 return 0;
437
438 /* Check if the device already has a privileged handle. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300439 if (atomic_inc_return(&handle->stream->active) != 1) {
440 atomic_dec(&handle->stream->active);
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300441 return -EBUSY;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300442 }
443
444 handle->state = UVC_HANDLE_ACTIVE;
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300445 return 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300446}
447
448static void uvc_dismiss_privileges(struct uvc_fh *handle)
449{
450 if (handle->state == UVC_HANDLE_ACTIVE)
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300451 atomic_dec(&handle->stream->active);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300452
453 handle->state = UVC_HANDLE_PASSIVE;
454}
455
456static int uvc_has_privileges(struct uvc_fh *handle)
457{
458 return handle->state == UVC_HANDLE_ACTIVE;
459}
460
461/* ------------------------------------------------------------------------
462 * V4L2 file operations
463 */
464
Hans Verkuilbec43662008-12-30 06:58:20 -0300465static int uvc_v4l2_open(struct file *file)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300466{
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300467 struct uvc_streaming *stream;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300468 struct uvc_fh *handle;
469 int ret = 0;
470
471 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300472 stream = video_drvdata(file);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300473
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300474 if (stream->dev->state & UVC_DEV_DISCONNECTED)
475 return -ENODEV;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300476
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300477 ret = usb_autopm_get_interface(stream->dev->intf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300478 if (ret < 0)
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300479 return ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300480
481 /* Create the device handle. */
482 handle = kzalloc(sizeof *handle, GFP_KERNEL);
483 if (handle == NULL) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300484 usb_autopm_put_interface(stream->dev->intf);
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300485 return -ENOMEM;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300486 }
487
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300488 if (atomic_inc_return(&stream->dev->users) == 1) {
489 ret = uvc_status_start(stream->dev);
490 if (ret < 0) {
491 usb_autopm_put_interface(stream->dev->intf);
492 atomic_dec(&stream->dev->users);
Laurent Pinchart04a37e02009-05-19 10:08:03 -0300493 kfree(handle);
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300494 return ret;
Laurent Pinchart04a37e02009-05-19 10:08:03 -0300495 }
496 }
497
Laurent Pinchart8e113592009-07-01 20:24:47 -0300498 handle->chain = stream->chain;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300499 handle->stream = stream;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300500 handle->state = UVC_HANDLE_PASSIVE;
501 file->private_data = handle;
502
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300503 return 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300504}
505
Hans Verkuilbec43662008-12-30 06:58:20 -0300506static int uvc_v4l2_release(struct file *file)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300507{
Joe Perchesabf84382010-07-12 17:50:03 -0300508 struct uvc_fh *handle = file->private_data;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300509 struct uvc_streaming *stream = handle->stream;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300510
511 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");
512
513 /* Only free resources if this is a privileged handle. */
514 if (uvc_has_privileges(handle)) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300515 uvc_video_enable(stream, 0);
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300516 uvc_free_buffers(&stream->queue);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300517 }
518
519 /* Release the file handle. */
520 uvc_dismiss_privileges(handle);
521 kfree(handle);
522 file->private_data = NULL;
523
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300524 if (atomic_dec_return(&stream->dev->users) == 0)
525 uvc_status_stop(stream->dev);
Laurent Pinchart04a37e02009-05-19 10:08:03 -0300526
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300527 usb_autopm_put_interface(stream->dev->intf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300528 return 0;
529}
530
Hans Verkuil069b7472008-12-30 07:04:34 -0300531static long uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300532{
533 struct video_device *vdev = video_devdata(file);
Joe Perchesabf84382010-07-12 17:50:03 -0300534 struct uvc_fh *handle = file->private_data;
Laurent Pinchart8e113592009-07-01 20:24:47 -0300535 struct uvc_video_chain *chain = handle->chain;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300536 struct uvc_streaming *stream = handle->stream;
Hans Verkuil069b7472008-12-30 07:04:34 -0300537 long ret = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300538
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300539 switch (cmd) {
540 /* Query capabilities */
541 case VIDIOC_QUERYCAP:
542 {
543 struct v4l2_capability *cap = arg;
544
545 memset(cap, 0, sizeof *cap);
Laurent Pinchartd0ebf302009-01-08 14:05:11 -0300546 strlcpy(cap->driver, "uvcvideo", sizeof cap->driver);
547 strlcpy(cap->card, vdev->name, sizeof cap->card);
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300548 usb_make_path(stream->dev->udev,
Thierry MERLEb12049a2009-01-27 16:53:23 -0300549 cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehabfd3e5822011-06-25 13:50:37 -0300550 cap->version = LINUX_VERSION_CODE;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300551 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
Laurent Pinchartff924202008-12-28 22:32:29 -0300552 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
553 | V4L2_CAP_STREAMING;
554 else
555 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT
556 | V4L2_CAP_STREAMING;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300557 break;
558 }
559
560 /* Get, Set & Query control */
561 case VIDIOC_QUERYCTRL:
Laurent Pinchart8e113592009-07-01 20:24:47 -0300562 return uvc_query_v4l2_ctrl(chain, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300563
564 case VIDIOC_G_CTRL:
565 {
566 struct v4l2_control *ctrl = arg;
567 struct v4l2_ext_control xctrl;
568
569 memset(&xctrl, 0, sizeof xctrl);
570 xctrl.id = ctrl->id;
571
Laurent Pinchart8e113592009-07-01 20:24:47 -0300572 ret = uvc_ctrl_begin(chain);
573 if (ret < 0)
Robert Krakora1fcbcc42009-06-12 13:51:03 -0300574 return ret;
575
Laurent Pinchart8e113592009-07-01 20:24:47 -0300576 ret = uvc_ctrl_get(chain, &xctrl);
577 uvc_ctrl_rollback(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300578 if (ret >= 0)
579 ctrl->value = xctrl.value;
580 break;
581 }
582
583 case VIDIOC_S_CTRL:
584 {
585 struct v4l2_control *ctrl = arg;
586 struct v4l2_ext_control xctrl;
587
588 memset(&xctrl, 0, sizeof xctrl);
589 xctrl.id = ctrl->id;
590 xctrl.value = ctrl->value;
591
Laurent Pinchart8d556622010-02-04 21:43:37 -0300592 ret = uvc_ctrl_begin(chain);
Laurent Pinchart8e113592009-07-01 20:24:47 -0300593 if (ret < 0)
Robert Krakora1fcbcc42009-06-12 13:51:03 -0300594 return ret;
595
Laurent Pinchart8e113592009-07-01 20:24:47 -0300596 ret = uvc_ctrl_set(chain, &xctrl);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300597 if (ret < 0) {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300598 uvc_ctrl_rollback(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300599 return ret;
600 }
Laurent Pinchart8e113592009-07-01 20:24:47 -0300601 ret = uvc_ctrl_commit(chain);
Laurent Pincharte54532e2010-01-23 07:07:53 -0300602 if (ret == 0)
603 ctrl->value = xctrl.value;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300604 break;
605 }
606
607 case VIDIOC_QUERYMENU:
Laurent Pinchart23d9f3e2010-11-21 07:58:54 -0300608 return uvc_query_v4l2_menu(chain, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300609
610 case VIDIOC_G_EXT_CTRLS:
611 {
612 struct v4l2_ext_controls *ctrls = arg;
613 struct v4l2_ext_control *ctrl = ctrls->controls;
614 unsigned int i;
615
Laurent Pinchart8e113592009-07-01 20:24:47 -0300616 ret = uvc_ctrl_begin(chain);
617 if (ret < 0)
Robert Krakora1fcbcc42009-06-12 13:51:03 -0300618 return ret;
619
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300620 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300621 ret = uvc_ctrl_get(chain, ctrl);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300622 if (ret < 0) {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300623 uvc_ctrl_rollback(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300624 ctrls->error_idx = i;
625 return ret;
626 }
627 }
628 ctrls->error_idx = 0;
Laurent Pinchart8e113592009-07-01 20:24:47 -0300629 ret = uvc_ctrl_rollback(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300630 break;
631 }
632
633 case VIDIOC_S_EXT_CTRLS:
634 case VIDIOC_TRY_EXT_CTRLS:
635 {
636 struct v4l2_ext_controls *ctrls = arg;
637 struct v4l2_ext_control *ctrl = ctrls->controls;
638 unsigned int i;
639
Laurent Pinchart8e113592009-07-01 20:24:47 -0300640 ret = uvc_ctrl_begin(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300641 if (ret < 0)
642 return ret;
643
644 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300645 ret = uvc_ctrl_set(chain, ctrl);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300646 if (ret < 0) {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300647 uvc_ctrl_rollback(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300648 ctrls->error_idx = i;
649 return ret;
650 }
651 }
652
653 ctrls->error_idx = 0;
654
655 if (cmd == VIDIOC_S_EXT_CTRLS)
Laurent Pinchart8e113592009-07-01 20:24:47 -0300656 ret = uvc_ctrl_commit(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300657 else
Laurent Pinchart8e113592009-07-01 20:24:47 -0300658 ret = uvc_ctrl_rollback(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300659 break;
660 }
661
662 /* Get, Set & Enum input */
663 case VIDIOC_ENUMINPUT:
664 {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300665 const struct uvc_entity *selector = chain->selector;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300666 struct v4l2_input *input = arg;
667 struct uvc_entity *iterm = NULL;
668 u32 index = input->index;
669 int pin = 0;
670
671 if (selector == NULL ||
Laurent Pinchart8e113592009-07-01 20:24:47 -0300672 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300673 if (index != 0)
674 return -EINVAL;
Laurent Pinchart6241d8c2009-11-25 12:00:22 -0300675 list_for_each_entry(iterm, &chain->entities, chain) {
676 if (UVC_ENTITY_IS_ITERM(iterm))
677 break;
678 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300679 pin = iterm->id;
Laurent Pinchart8ca5a632009-11-25 12:00:30 -0300680 } else if (pin < selector->bNrInPins) {
681 pin = selector->baSourceID[index];
Laurent Pinchart6241d8c2009-11-25 12:00:22 -0300682 list_for_each_entry(iterm, &chain->entities, chain) {
683 if (!UVC_ENTITY_IS_ITERM(iterm))
684 continue;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300685 if (iterm->id == pin)
686 break;
687 }
688 }
689
690 if (iterm == NULL || iterm->id != pin)
691 return -EINVAL;
692
693 memset(input, 0, sizeof *input);
694 input->index = index;
Laurent Pinchartd0ebf302009-01-08 14:05:11 -0300695 strlcpy(input->name, iterm->name, sizeof input->name);
Laurent Pinchartb482d922009-06-26 11:39:42 -0300696 if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300697 input->type = V4L2_INPUT_TYPE_CAMERA;
698 break;
699 }
700
701 case VIDIOC_G_INPUT:
702 {
703 u8 input;
704
Laurent Pinchart8e113592009-07-01 20:24:47 -0300705 if (chain->selector == NULL ||
706 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300707 *(int *)arg = 0;
708 break;
709 }
710
Laurent Pinchart8e113592009-07-01 20:24:47 -0300711 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
712 chain->selector->id, chain->dev->intfnum,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300713 UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300714 if (ret < 0)
715 return ret;
716
717 *(int *)arg = input - 1;
718 break;
719 }
720
721 case VIDIOC_S_INPUT:
722 {
Márton Németh9086c7b992009-04-15 09:01:58 -0300723 u32 input = *(u32 *)arg + 1;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300724
725 if ((ret = uvc_acquire_privileges(handle)) < 0)
726 return ret;
727
Laurent Pinchart8e113592009-07-01 20:24:47 -0300728 if (chain->selector == NULL ||
729 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300730 if (input != 1)
731 return -EINVAL;
732 break;
733 }
734
Laurent Pinchart8ca5a632009-11-25 12:00:30 -0300735 if (input == 0 || input > chain->selector->bNrInPins)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300736 return -EINVAL;
737
Laurent Pinchart8e113592009-07-01 20:24:47 -0300738 return uvc_query_ctrl(chain->dev, UVC_SET_CUR,
739 chain->selector->id, chain->dev->intfnum,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300740 UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300741 }
742
743 /* Try, Get, Set & Enum format */
744 case VIDIOC_ENUM_FMT:
745 {
746 struct v4l2_fmtdesc *fmt = arg;
747 struct uvc_format *format;
Márton Németh8bbd90c2009-03-27 11:13:57 -0300748 enum v4l2_buf_type type = fmt->type;
749 __u32 index = fmt->index;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300750
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300751 if (fmt->type != stream->type ||
752 fmt->index >= stream->nformats)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300753 return -EINVAL;
754
Márton Németh8bbd90c2009-03-27 11:13:57 -0300755 memset(fmt, 0, sizeof(*fmt));
756 fmt->index = index;
757 fmt->type = type;
758
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300759 format = &stream->format[fmt->index];
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300760 fmt->flags = 0;
761 if (format->flags & UVC_FMT_FLAG_COMPRESSED)
762 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
Laurent Pinchartd0ebf302009-01-08 14:05:11 -0300763 strlcpy(fmt->description, format->name,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300764 sizeof fmt->description);
765 fmt->description[sizeof fmt->description - 1] = 0;
766 fmt->pixelformat = format->fcc;
767 break;
768 }
769
770 case VIDIOC_TRY_FMT:
771 {
772 struct uvc_streaming_control probe;
773
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300774 return uvc_v4l2_try_format(stream, arg, &probe, NULL, NULL);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300775 }
776
777 case VIDIOC_S_FMT:
778 if ((ret = uvc_acquire_privileges(handle)) < 0)
779 return ret;
780
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300781 return uvc_v4l2_set_format(stream, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300782
783 case VIDIOC_G_FMT:
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300784 return uvc_v4l2_get_format(stream, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300785
786 /* Frame size enumeration */
787 case VIDIOC_ENUM_FRAMESIZES:
788 {
789 struct v4l2_frmsizeenum *fsize = arg;
790 struct uvc_format *format = NULL;
791 struct uvc_frame *frame;
792 int i;
793
794 /* Look for the given pixel format */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300795 for (i = 0; i < stream->nformats; i++) {
796 if (stream->format[i].fcc ==
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300797 fsize->pixel_format) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300798 format = &stream->format[i];
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300799 break;
800 }
801 }
802 if (format == NULL)
803 return -EINVAL;
804
805 if (fsize->index >= format->nframes)
806 return -EINVAL;
807
808 frame = &format->frame[fsize->index];
809 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
810 fsize->discrete.width = frame->wWidth;
811 fsize->discrete.height = frame->wHeight;
812 break;
813 }
814
815 /* Frame interval enumeration */
816 case VIDIOC_ENUM_FRAMEINTERVALS:
817 {
818 struct v4l2_frmivalenum *fival = arg;
819 struct uvc_format *format = NULL;
820 struct uvc_frame *frame = NULL;
821 int i;
822
823 /* Look for the given pixel format and frame size */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300824 for (i = 0; i < stream->nformats; i++) {
825 if (stream->format[i].fcc ==
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300826 fival->pixel_format) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300827 format = &stream->format[i];
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300828 break;
829 }
830 }
831 if (format == NULL)
832 return -EINVAL;
833
834 for (i = 0; i < format->nframes; i++) {
835 if (format->frame[i].wWidth == fival->width &&
836 format->frame[i].wHeight == fival->height) {
837 frame = &format->frame[i];
838 break;
839 }
840 }
841 if (frame == NULL)
842 return -EINVAL;
843
844 if (frame->bFrameIntervalType) {
845 if (fival->index >= frame->bFrameIntervalType)
846 return -EINVAL;
847
848 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
849 fival->discrete.numerator =
850 frame->dwFrameInterval[fival->index];
851 fival->discrete.denominator = 10000000;
852 uvc_simplify_fraction(&fival->discrete.numerator,
853 &fival->discrete.denominator, 8, 333);
854 } else {
855 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
856 fival->stepwise.min.numerator =
857 frame->dwFrameInterval[0];
858 fival->stepwise.min.denominator = 10000000;
859 fival->stepwise.max.numerator =
860 frame->dwFrameInterval[1];
861 fival->stepwise.max.denominator = 10000000;
862 fival->stepwise.step.numerator =
863 frame->dwFrameInterval[2];
864 fival->stepwise.step.denominator = 10000000;
865 uvc_simplify_fraction(&fival->stepwise.min.numerator,
866 &fival->stepwise.min.denominator, 8, 333);
867 uvc_simplify_fraction(&fival->stepwise.max.numerator,
868 &fival->stepwise.max.denominator, 8, 333);
869 uvc_simplify_fraction(&fival->stepwise.step.numerator,
870 &fival->stepwise.step.denominator, 8, 333);
871 }
872 break;
873 }
874
875 /* Get & Set streaming parameters */
876 case VIDIOC_G_PARM:
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300877 return uvc_v4l2_get_streamparm(stream, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300878
879 case VIDIOC_S_PARM:
880 if ((ret = uvc_acquire_privileges(handle)) < 0)
881 return ret;
882
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300883 return uvc_v4l2_set_streamparm(stream, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300884
885 /* Cropping and scaling */
886 case VIDIOC_CROPCAP:
887 {
888 struct v4l2_cropcap *ccap = arg;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300889
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300890 if (ccap->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300891 return -EINVAL;
892
893 ccap->bounds.left = 0;
894 ccap->bounds.top = 0;
Laurent Pinchart69477562010-11-21 13:36:34 -0300895
896 mutex_lock(&stream->mutex);
897 ccap->bounds.width = stream->cur_frame->wWidth;
898 ccap->bounds.height = stream->cur_frame->wHeight;
899 mutex_unlock(&stream->mutex);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300900
901 ccap->defrect = ccap->bounds;
902
903 ccap->pixelaspect.numerator = 1;
904 ccap->pixelaspect.denominator = 1;
905 break;
906 }
907
908 case VIDIOC_G_CROP:
909 case VIDIOC_S_CROP:
910 return -EINVAL;
911
912 /* Buffers & streaming */
913 case VIDIOC_REQBUFS:
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300914 if ((ret = uvc_acquire_privileges(handle)) < 0)
915 return ret;
916
Laurent Pinchart69477562010-11-21 13:36:34 -0300917 mutex_lock(&stream->mutex);
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300918 ret = uvc_alloc_buffers(&stream->queue, arg);
Laurent Pinchart69477562010-11-21 13:36:34 -0300919 mutex_unlock(&stream->mutex);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300920 if (ret < 0)
921 return ret;
922
Laurent Pinchart1a969d92009-09-02 03:12:26 -0300923 if (ret == 0)
924 uvc_dismiss_privileges(handle);
925
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300926 ret = 0;
927 break;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300928
929 case VIDIOC_QUERYBUF:
930 {
931 struct v4l2_buffer *buf = arg;
932
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300933 if (!uvc_has_privileges(handle))
934 return -EBUSY;
935
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300936 return uvc_query_buffer(&stream->queue, buf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300937 }
938
939 case VIDIOC_QBUF:
940 if (!uvc_has_privileges(handle))
941 return -EBUSY;
942
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300943 return uvc_queue_buffer(&stream->queue, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300944
945 case VIDIOC_DQBUF:
946 if (!uvc_has_privileges(handle))
947 return -EBUSY;
948
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300949 return uvc_dequeue_buffer(&stream->queue, arg,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300950 file->f_flags & O_NONBLOCK);
951
952 case VIDIOC_STREAMON:
953 {
954 int *type = arg;
955
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300956 if (*type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300957 return -EINVAL;
958
959 if (!uvc_has_privileges(handle))
960 return -EBUSY;
961
Laurent Pinchart69477562010-11-21 13:36:34 -0300962 mutex_lock(&stream->mutex);
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300963 ret = uvc_video_enable(stream, 1);
Laurent Pinchart69477562010-11-21 13:36:34 -0300964 mutex_unlock(&stream->mutex);
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300965 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300966 return ret;
967 break;
968 }
969
970 case VIDIOC_STREAMOFF:
971 {
972 int *type = arg;
973
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300974 if (*type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300975 return -EINVAL;
976
977 if (!uvc_has_privileges(handle))
978 return -EBUSY;
979
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300980 return uvc_video_enable(stream, 0);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300981 }
982
983 /* Analog video standards make no sense for digital cameras. */
984 case VIDIOC_ENUMSTD:
985 case VIDIOC_QUERYSTD:
986 case VIDIOC_G_STD:
987 case VIDIOC_S_STD:
988
989 case VIDIOC_OVERLAY:
990
991 case VIDIOC_ENUMAUDIO:
992 case VIDIOC_ENUMAUDOUT:
993
994 case VIDIOC_ENUMOUTPUT:
995 uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd);
996 return -EINVAL;
997
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300998 case UVCIOC_CTRL_MAP:
Laurent Pinchart227bd5b2011-07-30 16:19:49 -0300999 return uvc_ioctl_ctrl_map(chain, arg);
Martin Rublife78d182010-10-02 19:10:16 -03001000
1001 case UVCIOC_CTRL_QUERY:
1002 return uvc_xu_ctrl_query(chain, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001003
1004 default:
Hans Verkuil08af2452010-12-24 10:33:19 -03001005 uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n", cmd);
1006 return -EINVAL;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001007 }
1008
1009 return ret;
1010}
1011
Hans Verkuil069b7472008-12-30 07:04:34 -03001012static long uvc_v4l2_ioctl(struct file *file,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001013 unsigned int cmd, unsigned long arg)
1014{
Laurent Pinchart308bb522008-11-17 18:32:11 -03001015 if (uvc_trace_param & UVC_TRACE_IOCTL) {
1016 uvc_printk(KERN_DEBUG, "uvc_v4l2_ioctl(");
1017 v4l_printk_ioctl(cmd);
1018 printk(")\n");
1019 }
1020
Hans Verkuilf473bf72008-11-01 08:25:11 -03001021 return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001022}
1023
1024static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1025 size_t count, loff_t *ppos)
1026{
1027 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
Laurent Pinchart350d6402009-11-04 11:53:49 -03001028 return -EINVAL;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001029}
1030
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001031static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1032{
Joe Perchesabf84382010-07-12 17:50:03 -03001033 struct uvc_fh *handle = file->private_data;
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001034 struct uvc_streaming *stream = handle->stream;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001035
1036 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
1037
Laurent Pinchart4aa27592010-11-21 15:18:08 -03001038 return uvc_queue_mmap(&stream->queue, vma);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001039}
1040
1041static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
1042{
Joe Perchesabf84382010-07-12 17:50:03 -03001043 struct uvc_fh *handle = file->private_data;
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001044 struct uvc_streaming *stream = handle->stream;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001045
1046 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
1047
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001048 return uvc_queue_poll(&stream->queue, file, wait);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001049}
1050
Bob Liu72969442011-04-29 07:11:35 -03001051#ifndef CONFIG_MMU
1052static unsigned long uvc_v4l2_get_unmapped_area(struct file *file,
1053 unsigned long addr, unsigned long len, unsigned long pgoff,
1054 unsigned long flags)
1055{
1056 struct uvc_fh *handle = file->private_data;
1057 struct uvc_streaming *stream = handle->stream;
1058
1059 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_get_unmapped_area\n");
1060
1061 return uvc_queue_get_unmapped_area(&stream->queue, pgoff);
1062}
1063#endif
1064
Hans Verkuilbec43662008-12-30 06:58:20 -03001065const struct v4l2_file_operations uvc_fops = {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001066 .owner = THIS_MODULE,
1067 .open = uvc_v4l2_open,
1068 .release = uvc_v4l2_release,
Laurent Pinchart673eb9f2010-11-21 16:54:56 -03001069 .unlocked_ioctl = uvc_v4l2_ioctl,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001070 .read = uvc_v4l2_read,
1071 .mmap = uvc_v4l2_mmap,
1072 .poll = uvc_v4l2_poll,
Bob Liu72969442011-04-29 07:11:35 -03001073#ifndef CONFIG_MMU
1074 .get_unmapped_area = uvc_v4l2_get_unmapped_area,
1075#endif
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001076};
Hans Verkuilf87086e2008-07-18 00:50:58 -03001077