blob: b3478d0eaf41b4809ea1cfa7bb4a8a4b03b60872 [file] [log] [blame]
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001/*
2 * uvc_v4l2.c -- USB Video Class driver - V4L2 API
3 *
Laurent Pinchart2c2d2642009-01-03 19:12:40 -03004 * Copyright (C) 2005-2009
Laurent Pinchartc0efd232008-06-30 15:04:50 -03005 * Laurent Pinchart (laurent.pinchart@skynet.be)
6 *
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>
18#include <linux/usb.h>
19#include <linux/videodev2.h>
20#include <linux/vmalloc.h>
21#include <linux/mm.h>
22#include <linux/wait.h>
23#include <asm/atomic.h>
24
25#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030026#include <media/v4l2-ioctl.h>
Laurent Pinchartc0efd232008-06-30 15:04:50 -030027
28#include "uvcvideo.h"
29
30/* ------------------------------------------------------------------------
31 * V4L2 interface
32 */
33
34/*
35 * Mapping V4L2 controls to UVC controls can be straighforward if done well.
36 * Most of the UVC controls exist in V4L2, and can be mapped directly. Some
37 * must be grouped (for instance the Red Balance, Blue Balance and Do White
38 * Balance V4L2 controls use the White Balance Component UVC control) or
39 * otherwise translated. The approach we take here is to use a translation
Laurent Pinchart2c2d2642009-01-03 19:12:40 -030040 * table for the controls that can be mapped directly, and handle the others
Laurent Pinchartc0efd232008-06-30 15:04:50 -030041 * manually.
42 */
Laurent Pinchart8e113592009-07-01 20:24:47 -030043static int uvc_v4l2_query_menu(struct uvc_video_chain *chain,
Laurent Pinchartc0efd232008-06-30 15:04:50 -030044 struct v4l2_querymenu *query_menu)
45{
46 struct uvc_menu_info *menu_info;
47 struct uvc_control_mapping *mapping;
48 struct uvc_control *ctrl;
Márton Németh2460cda2009-04-20 14:51:49 -030049 u32 index = query_menu->index;
50 u32 id = query_menu->id;
Laurent Pinchartc0efd232008-06-30 15:04:50 -030051
Laurent Pinchart8e113592009-07-01 20:24:47 -030052 ctrl = uvc_find_control(chain, query_menu->id, &mapping);
Laurent Pinchartc0efd232008-06-30 15:04:50 -030053 if (ctrl == NULL || mapping->v4l2_type != V4L2_CTRL_TYPE_MENU)
54 return -EINVAL;
55
56 if (query_menu->index >= mapping->menu_count)
57 return -EINVAL;
58
Márton Németh2460cda2009-04-20 14:51:49 -030059 memset(query_menu, 0, sizeof(*query_menu));
60 query_menu->id = id;
61 query_menu->index = index;
62
Laurent Pinchartc0efd232008-06-30 15:04:50 -030063 menu_info = &mapping->menu_info[query_menu->index];
Laurent Pinchartd0ebf302009-01-08 14:05:11 -030064 strlcpy(query_menu->name, menu_info->name, sizeof query_menu->name);
Laurent Pinchartc0efd232008-06-30 15:04:50 -030065 return 0;
66}
67
68/*
69 * Find the frame interval closest to the requested frame interval for the
70 * given frame format and size. This should be done by the device as part of
71 * the Video Probe and Commit negotiation, but some hardware don't implement
72 * that feature.
73 */
74static __u32 uvc_try_frame_interval(struct uvc_frame *frame, __u32 interval)
75{
76 unsigned int i;
77
78 if (frame->bFrameIntervalType) {
79 __u32 best = -1, dist;
80
81 for (i = 0; i < frame->bFrameIntervalType; ++i) {
82 dist = interval > frame->dwFrameInterval[i]
83 ? interval - frame->dwFrameInterval[i]
84 : frame->dwFrameInterval[i] - interval;
85
86 if (dist > best)
87 break;
88
89 best = dist;
90 }
91
92 interval = frame->dwFrameInterval[i-1];
93 } else {
94 const __u32 min = frame->dwFrameInterval[0];
95 const __u32 max = frame->dwFrameInterval[1];
96 const __u32 step = frame->dwFrameInterval[2];
97
98 interval = min + (interval - min + step/2) / step * step;
99 if (interval > max)
100 interval = max;
101 }
102
103 return interval;
104}
105
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300106static int uvc_v4l2_try_format(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300107 struct v4l2_format *fmt, struct uvc_streaming_control *probe,
108 struct uvc_format **uvc_format, struct uvc_frame **uvc_frame)
109{
110 struct uvc_format *format = NULL;
111 struct uvc_frame *frame = NULL;
112 __u16 rw, rh;
113 unsigned int d, maxd;
114 unsigned int i;
115 __u32 interval;
116 int ret = 0;
117 __u8 *fcc;
118
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300119 if (fmt->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300120 return -EINVAL;
121
122 fcc = (__u8 *)&fmt->fmt.pix.pixelformat;
123 uvc_trace(UVC_TRACE_FORMAT, "Trying format 0x%08x (%c%c%c%c): %ux%u.\n",
124 fmt->fmt.pix.pixelformat,
125 fcc[0], fcc[1], fcc[2], fcc[3],
126 fmt->fmt.pix.width, fmt->fmt.pix.height);
127
128 /* Check if the hardware supports the requested format. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300129 for (i = 0; i < stream->nformats; ++i) {
130 format = &stream->format[i];
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300131 if (format->fcc == fmt->fmt.pix.pixelformat)
132 break;
133 }
134
135 if (format == NULL || format->fcc != fmt->fmt.pix.pixelformat) {
136 uvc_trace(UVC_TRACE_FORMAT, "Unsupported format 0x%08x.\n",
137 fmt->fmt.pix.pixelformat);
138 return -EINVAL;
139 }
140
141 /* Find the closest image size. The distance between image sizes is
142 * the size in pixels of the non-overlapping regions between the
143 * requested size and the frame-specified size.
144 */
145 rw = fmt->fmt.pix.width;
146 rh = fmt->fmt.pix.height;
147 maxd = (unsigned int)-1;
148
149 for (i = 0; i < format->nframes; ++i) {
150 __u16 w = format->frame[i].wWidth;
151 __u16 h = format->frame[i].wHeight;
152
153 d = min(w, rw) * min(h, rh);
154 d = w*h + rw*rh - 2*d;
155 if (d < maxd) {
156 maxd = d;
157 frame = &format->frame[i];
158 }
159
160 if (maxd == 0)
161 break;
162 }
163
164 if (frame == NULL) {
165 uvc_trace(UVC_TRACE_FORMAT, "Unsupported size %ux%u.\n",
166 fmt->fmt.pix.width, fmt->fmt.pix.height);
167 return -EINVAL;
168 }
169
170 /* Use the default frame interval. */
171 interval = frame->dwDefaultFrameInterval;
172 uvc_trace(UVC_TRACE_FORMAT, "Using default frame interval %u.%u us "
173 "(%u.%u fps).\n", interval/10, interval%10, 10000000/interval,
174 (100000000/interval)%10);
175
176 /* Set the format index, frame index and frame interval. */
177 memset(probe, 0, sizeof *probe);
178 probe->bmHint = 1; /* dwFrameInterval */
179 probe->bFormatIndex = format->index;
180 probe->bFrameIndex = frame->bFrameIndex;
181 probe->dwFrameInterval = uvc_try_frame_interval(frame, interval);
182 /* Some webcams stall the probe control set request when the
183 * dwMaxVideoFrameSize field is set to zero. The UVC specification
184 * clearly states that the field is read-only from the host, so this
185 * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by
186 * the webcam to work around the problem.
187 *
188 * The workaround could probably be enabled for all webcams, so the
189 * quirk can be removed if needed. It's currently useful to detect
190 * webcam bugs and fix them before they hit the market (providing
191 * developers test their webcams with the Linux driver as well as with
192 * the Windows driver).
193 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300194 if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300195 probe->dwMaxVideoFrameSize =
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300196 stream->ctrl.dwMaxVideoFrameSize;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300197
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300198 /* Probe the device. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300199 ret = uvc_probe_video(stream, probe);
200 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300201 goto done;
202
203 fmt->fmt.pix.width = frame->wWidth;
204 fmt->fmt.pix.height = frame->wHeight;
205 fmt->fmt.pix.field = V4L2_FIELD_NONE;
206 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
207 fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize;
208 fmt->fmt.pix.colorspace = format->colorspace;
209 fmt->fmt.pix.priv = 0;
210
211 if (uvc_format != NULL)
212 *uvc_format = format;
213 if (uvc_frame != NULL)
214 *uvc_frame = frame;
215
216done:
217 return ret;
218}
219
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300220static int uvc_v4l2_get_format(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300221 struct v4l2_format *fmt)
222{
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300223 struct uvc_format *format = stream->cur_format;
224 struct uvc_frame *frame = stream->cur_frame;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300225
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300226 if (fmt->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300227 return -EINVAL;
228
229 if (format == NULL || frame == NULL)
230 return -EINVAL;
231
232 fmt->fmt.pix.pixelformat = format->fcc;
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;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300237 fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300238 fmt->fmt.pix.colorspace = format->colorspace;
239 fmt->fmt.pix.priv = 0;
240
241 return 0;
242}
243
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300244static int uvc_v4l2_set_format(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300245 struct v4l2_format *fmt)
246{
247 struct uvc_streaming_control probe;
248 struct uvc_format *format;
249 struct uvc_frame *frame;
250 int ret;
251
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300252 if (fmt->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300253 return -EINVAL;
254
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300255 if (uvc_queue_allocated(&stream->queue))
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300256 return -EBUSY;
257
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300258 ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300259 if (ret < 0)
260 return ret;
261
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300262 memcpy(&stream->ctrl, &probe, sizeof probe);
263 stream->cur_format = format;
264 stream->cur_frame = frame;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300265
266 return 0;
267}
268
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300269static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300270 struct v4l2_streamparm *parm)
271{
272 uint32_t numerator, denominator;
273
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300274 if (parm->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300275 return -EINVAL;
276
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300277 numerator = stream->ctrl.dwFrameInterval;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300278 denominator = 10000000;
279 uvc_simplify_fraction(&numerator, &denominator, 8, 333);
280
281 memset(parm, 0, sizeof *parm);
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300282 parm->type = stream->type;
Laurent Pinchartff924202008-12-28 22:32:29 -0300283
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300284 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Laurent Pinchartff924202008-12-28 22:32:29 -0300285 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
286 parm->parm.capture.capturemode = 0;
287 parm->parm.capture.timeperframe.numerator = numerator;
288 parm->parm.capture.timeperframe.denominator = denominator;
289 parm->parm.capture.extendedmode = 0;
290 parm->parm.capture.readbuffers = 0;
291 } else {
292 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
293 parm->parm.output.outputmode = 0;
294 parm->parm.output.timeperframe.numerator = numerator;
295 parm->parm.output.timeperframe.denominator = denominator;
296 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300297
298 return 0;
299}
300
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300301static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300302 struct v4l2_streamparm *parm)
303{
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300304 struct uvc_frame *frame = stream->cur_frame;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300305 struct uvc_streaming_control probe;
Laurent Pinchartff924202008-12-28 22:32:29 -0300306 struct v4l2_fract timeperframe;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300307 uint32_t interval;
308 int ret;
309
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300310 if (parm->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300311 return -EINVAL;
312
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300313 if (uvc_queue_streaming(&stream->queue))
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300314 return -EBUSY;
315
Laurent Pinchartff924202008-12-28 22:32:29 -0300316 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
317 timeperframe = parm->parm.capture.timeperframe;
318 else
319 timeperframe = parm->parm.output.timeperframe;
320
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300321 memcpy(&probe, &stream->ctrl, sizeof probe);
Laurent Pinchartff924202008-12-28 22:32:29 -0300322 interval = uvc_fraction_to_interval(timeperframe.numerator,
323 timeperframe.denominator);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300324
325 uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
Laurent Pinchartff924202008-12-28 22:32:29 -0300326 timeperframe.numerator, timeperframe.denominator, interval);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300327 probe.dwFrameInterval = uvc_try_frame_interval(frame, interval);
328
329 /* Probe the device with the new settings. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300330 ret = uvc_probe_video(stream, &probe);
331 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300332 return ret;
333
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300334 memcpy(&stream->ctrl, &probe, sizeof probe);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300335
336 /* Return the actual frame period. */
Laurent Pinchartff924202008-12-28 22:32:29 -0300337 timeperframe.numerator = probe.dwFrameInterval;
338 timeperframe.denominator = 10000000;
339 uvc_simplify_fraction(&timeperframe.numerator,
340 &timeperframe.denominator, 8, 333);
341
342 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
343 parm->parm.capture.timeperframe = timeperframe;
344 else
345 parm->parm.output.timeperframe = timeperframe;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300346
347 return 0;
348}
349
350/* ------------------------------------------------------------------------
351 * Privilege management
352 */
353
354/*
355 * Privilege management is the multiple-open implementation basis. The current
356 * implementation is completely transparent for the end-user and doesn't
357 * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls.
358 * Those ioctls enable finer control on the device (by making possible for a
359 * user to request exclusive access to a device), but are not mature yet.
360 * Switching to the V4L2 priority mechanism might be considered in the future
361 * if this situation changes.
362 *
363 * Each open instance of a UVC device can either be in a privileged or
364 * unprivileged state. Only a single instance can be in a privileged state at
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300365 * a given time. Trying to perform an operation that requires privileges will
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300366 * automatically acquire the required privileges if possible, or return -EBUSY
Laurent Pinchart1a969d92009-09-02 03:12:26 -0300367 * otherwise. Privileges are dismissed when closing the instance or when
368 * freeing the video buffers using VIDIOC_REQBUFS.
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300369 *
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300370 * Operations that require privileges are:
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300371 *
372 * - VIDIOC_S_INPUT
373 * - VIDIOC_S_PARM
374 * - VIDIOC_S_FMT
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300375 * - VIDIOC_REQBUFS
376 */
377static int uvc_acquire_privileges(struct uvc_fh *handle)
378{
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300379 /* Always succeed if the handle is already privileged. */
380 if (handle->state == UVC_HANDLE_ACTIVE)
381 return 0;
382
383 /* Check if the device already has a privileged handle. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300384 if (atomic_inc_return(&handle->stream->active) != 1) {
385 atomic_dec(&handle->stream->active);
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300386 return -EBUSY;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300387 }
388
389 handle->state = UVC_HANDLE_ACTIVE;
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300390 return 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300391}
392
393static void uvc_dismiss_privileges(struct uvc_fh *handle)
394{
395 if (handle->state == UVC_HANDLE_ACTIVE)
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300396 atomic_dec(&handle->stream->active);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300397
398 handle->state = UVC_HANDLE_PASSIVE;
399}
400
401static int uvc_has_privileges(struct uvc_fh *handle)
402{
403 return handle->state == UVC_HANDLE_ACTIVE;
404}
405
406/* ------------------------------------------------------------------------
407 * V4L2 file operations
408 */
409
Hans Verkuilbec43662008-12-30 06:58:20 -0300410static int uvc_v4l2_open(struct file *file)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300411{
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300412 struct uvc_streaming *stream;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300413 struct uvc_fh *handle;
414 int ret = 0;
415
416 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300417 stream = video_drvdata(file);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300418
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300419 if (stream->dev->state & UVC_DEV_DISCONNECTED)
420 return -ENODEV;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300421
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300422 ret = usb_autopm_get_interface(stream->dev->intf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300423 if (ret < 0)
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300424 return ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300425
426 /* Create the device handle. */
427 handle = kzalloc(sizeof *handle, GFP_KERNEL);
428 if (handle == NULL) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300429 usb_autopm_put_interface(stream->dev->intf);
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300430 return -ENOMEM;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300431 }
432
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300433 if (atomic_inc_return(&stream->dev->users) == 1) {
434 ret = uvc_status_start(stream->dev);
435 if (ret < 0) {
436 usb_autopm_put_interface(stream->dev->intf);
437 atomic_dec(&stream->dev->users);
Laurent Pinchart04a37e02009-05-19 10:08:03 -0300438 kfree(handle);
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300439 return ret;
Laurent Pinchart04a37e02009-05-19 10:08:03 -0300440 }
441 }
442
Laurent Pinchart8e113592009-07-01 20:24:47 -0300443 handle->chain = stream->chain;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300444 handle->stream = stream;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300445 handle->state = UVC_HANDLE_PASSIVE;
446 file->private_data = handle;
447
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300448 return 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300449}
450
Hans Verkuilbec43662008-12-30 06:58:20 -0300451static int uvc_v4l2_release(struct file *file)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300452{
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300453 struct uvc_fh *handle = (struct uvc_fh *)file->private_data;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300454 struct uvc_streaming *stream = handle->stream;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300455
456 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");
457
458 /* Only free resources if this is a privileged handle. */
459 if (uvc_has_privileges(handle)) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300460 uvc_video_enable(stream, 0);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300461
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300462 mutex_lock(&stream->queue.mutex);
463 if (uvc_free_buffers(&stream->queue) < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300464 uvc_printk(KERN_ERR, "uvc_v4l2_release: Unable to "
465 "free buffers.\n");
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300466 mutex_unlock(&stream->queue.mutex);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300467 }
468
469 /* Release the file handle. */
470 uvc_dismiss_privileges(handle);
471 kfree(handle);
472 file->private_data = NULL;
473
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300474 if (atomic_dec_return(&stream->dev->users) == 0)
475 uvc_status_stop(stream->dev);
Laurent Pinchart04a37e02009-05-19 10:08:03 -0300476
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300477 usb_autopm_put_interface(stream->dev->intf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300478 return 0;
479}
480
Hans Verkuil069b7472008-12-30 07:04:34 -0300481static long uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300482{
483 struct video_device *vdev = video_devdata(file);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300484 struct uvc_fh *handle = (struct uvc_fh *)file->private_data;
Laurent Pinchart8e113592009-07-01 20:24:47 -0300485 struct uvc_video_chain *chain = handle->chain;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300486 struct uvc_streaming *stream = handle->stream;
Hans Verkuil069b7472008-12-30 07:04:34 -0300487 long ret = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300488
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300489 switch (cmd) {
490 /* Query capabilities */
491 case VIDIOC_QUERYCAP:
492 {
493 struct v4l2_capability *cap = arg;
494
495 memset(cap, 0, sizeof *cap);
Laurent Pinchartd0ebf302009-01-08 14:05:11 -0300496 strlcpy(cap->driver, "uvcvideo", sizeof cap->driver);
497 strlcpy(cap->card, vdev->name, sizeof cap->card);
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300498 usb_make_path(stream->dev->udev,
Thierry MERLEb12049a2009-01-27 16:53:23 -0300499 cap->bus_info, sizeof(cap->bus_info));
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300500 cap->version = DRIVER_VERSION_NUMBER;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300501 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
Laurent Pinchartff924202008-12-28 22:32:29 -0300502 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
503 | V4L2_CAP_STREAMING;
504 else
505 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT
506 | V4L2_CAP_STREAMING;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300507 break;
508 }
509
510 /* Get, Set & Query control */
511 case VIDIOC_QUERYCTRL:
Laurent Pinchart8e113592009-07-01 20:24:47 -0300512 return uvc_query_v4l2_ctrl(chain, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300513
514 case VIDIOC_G_CTRL:
515 {
516 struct v4l2_control *ctrl = arg;
517 struct v4l2_ext_control xctrl;
518
519 memset(&xctrl, 0, sizeof xctrl);
520 xctrl.id = ctrl->id;
521
Laurent Pinchart8e113592009-07-01 20:24:47 -0300522 ret = uvc_ctrl_begin(chain);
523 if (ret < 0)
Robert Krakora1fcbcc42009-06-12 13:51:03 -0300524 return ret;
525
Laurent Pinchart8e113592009-07-01 20:24:47 -0300526 ret = uvc_ctrl_get(chain, &xctrl);
527 uvc_ctrl_rollback(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300528 if (ret >= 0)
529 ctrl->value = xctrl.value;
530 break;
531 }
532
533 case VIDIOC_S_CTRL:
534 {
535 struct v4l2_control *ctrl = arg;
536 struct v4l2_ext_control xctrl;
537
538 memset(&xctrl, 0, sizeof xctrl);
539 xctrl.id = ctrl->id;
540 xctrl.value = ctrl->value;
541
Laurent Pinchart8e113592009-07-01 20:24:47 -0300542 uvc_ctrl_begin(chain);
543 if (ret < 0)
Robert Krakora1fcbcc42009-06-12 13:51:03 -0300544 return ret;
545
Laurent Pinchart8e113592009-07-01 20:24:47 -0300546 ret = uvc_ctrl_set(chain, &xctrl);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300547 if (ret < 0) {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300548 uvc_ctrl_rollback(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300549 return ret;
550 }
Laurent Pinchart8e113592009-07-01 20:24:47 -0300551 ret = uvc_ctrl_commit(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300552 break;
553 }
554
555 case VIDIOC_QUERYMENU:
Laurent Pinchart8e113592009-07-01 20:24:47 -0300556 return uvc_v4l2_query_menu(chain, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300557
558 case VIDIOC_G_EXT_CTRLS:
559 {
560 struct v4l2_ext_controls *ctrls = arg;
561 struct v4l2_ext_control *ctrl = ctrls->controls;
562 unsigned int i;
563
Laurent Pinchart8e113592009-07-01 20:24:47 -0300564 ret = uvc_ctrl_begin(chain);
565 if (ret < 0)
Robert Krakora1fcbcc42009-06-12 13:51:03 -0300566 return ret;
567
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300568 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300569 ret = uvc_ctrl_get(chain, ctrl);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300570 if (ret < 0) {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300571 uvc_ctrl_rollback(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300572 ctrls->error_idx = i;
573 return ret;
574 }
575 }
576 ctrls->error_idx = 0;
Laurent Pinchart8e113592009-07-01 20:24:47 -0300577 ret = uvc_ctrl_rollback(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300578 break;
579 }
580
581 case VIDIOC_S_EXT_CTRLS:
582 case VIDIOC_TRY_EXT_CTRLS:
583 {
584 struct v4l2_ext_controls *ctrls = arg;
585 struct v4l2_ext_control *ctrl = ctrls->controls;
586 unsigned int i;
587
Laurent Pinchart8e113592009-07-01 20:24:47 -0300588 ret = uvc_ctrl_begin(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300589 if (ret < 0)
590 return ret;
591
592 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300593 ret = uvc_ctrl_set(chain, ctrl);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300594 if (ret < 0) {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300595 uvc_ctrl_rollback(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300596 ctrls->error_idx = i;
597 return ret;
598 }
599 }
600
601 ctrls->error_idx = 0;
602
603 if (cmd == VIDIOC_S_EXT_CTRLS)
Laurent Pinchart8e113592009-07-01 20:24:47 -0300604 ret = uvc_ctrl_commit(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300605 else
Laurent Pinchart8e113592009-07-01 20:24:47 -0300606 ret = uvc_ctrl_rollback(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300607 break;
608 }
609
610 /* Get, Set & Enum input */
611 case VIDIOC_ENUMINPUT:
612 {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300613 const struct uvc_entity *selector = chain->selector;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300614 struct v4l2_input *input = arg;
615 struct uvc_entity *iterm = NULL;
616 u32 index = input->index;
617 int pin = 0;
618
619 if (selector == NULL ||
Laurent Pinchart8e113592009-07-01 20:24:47 -0300620 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300621 if (index != 0)
622 return -EINVAL;
Laurent Pinchart8e113592009-07-01 20:24:47 -0300623 iterm = list_first_entry(&chain->iterms,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300624 struct uvc_entity, chain);
625 pin = iterm->id;
626 } else if (pin < selector->selector.bNrInPins) {
627 pin = selector->selector.baSourceID[index];
Laurent Pinchart8e113592009-07-01 20:24:47 -0300628 list_for_each_entry(iterm, chain->iterms.next, chain) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300629 if (iterm->id == pin)
630 break;
631 }
632 }
633
634 if (iterm == NULL || iterm->id != pin)
635 return -EINVAL;
636
637 memset(input, 0, sizeof *input);
638 input->index = index;
Laurent Pinchartd0ebf302009-01-08 14:05:11 -0300639 strlcpy(input->name, iterm->name, sizeof input->name);
Laurent Pinchartb482d922009-06-26 11:39:42 -0300640 if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300641 input->type = V4L2_INPUT_TYPE_CAMERA;
642 break;
643 }
644
645 case VIDIOC_G_INPUT:
646 {
647 u8 input;
648
Laurent Pinchart8e113592009-07-01 20:24:47 -0300649 if (chain->selector == NULL ||
650 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300651 *(int *)arg = 0;
652 break;
653 }
654
Laurent Pinchart8e113592009-07-01 20:24:47 -0300655 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
656 chain->selector->id, chain->dev->intfnum,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300657 UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300658 if (ret < 0)
659 return ret;
660
661 *(int *)arg = input - 1;
662 break;
663 }
664
665 case VIDIOC_S_INPUT:
666 {
Márton Németh9086c7b992009-04-15 09:01:58 -0300667 u32 input = *(u32 *)arg + 1;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300668
669 if ((ret = uvc_acquire_privileges(handle)) < 0)
670 return ret;
671
Laurent Pinchart8e113592009-07-01 20:24:47 -0300672 if (chain->selector == NULL ||
673 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300674 if (input != 1)
675 return -EINVAL;
676 break;
677 }
678
Laurent Pinchart8e113592009-07-01 20:24:47 -0300679 if (input == 0 || input > chain->selector->selector.bNrInPins)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300680 return -EINVAL;
681
Laurent Pinchart8e113592009-07-01 20:24:47 -0300682 return uvc_query_ctrl(chain->dev, UVC_SET_CUR,
683 chain->selector->id, chain->dev->intfnum,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300684 UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300685 }
686
687 /* Try, Get, Set & Enum format */
688 case VIDIOC_ENUM_FMT:
689 {
690 struct v4l2_fmtdesc *fmt = arg;
691 struct uvc_format *format;
Márton Németh8bbd90c2009-03-27 11:13:57 -0300692 enum v4l2_buf_type type = fmt->type;
693 __u32 index = fmt->index;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300694
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300695 if (fmt->type != stream->type ||
696 fmt->index >= stream->nformats)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300697 return -EINVAL;
698
Márton Németh8bbd90c2009-03-27 11:13:57 -0300699 memset(fmt, 0, sizeof(*fmt));
700 fmt->index = index;
701 fmt->type = type;
702
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300703 format = &stream->format[fmt->index];
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300704 fmt->flags = 0;
705 if (format->flags & UVC_FMT_FLAG_COMPRESSED)
706 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
Laurent Pinchartd0ebf302009-01-08 14:05:11 -0300707 strlcpy(fmt->description, format->name,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300708 sizeof fmt->description);
709 fmt->description[sizeof fmt->description - 1] = 0;
710 fmt->pixelformat = format->fcc;
711 break;
712 }
713
714 case VIDIOC_TRY_FMT:
715 {
716 struct uvc_streaming_control probe;
717
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300718 return uvc_v4l2_try_format(stream, arg, &probe, NULL, NULL);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300719 }
720
721 case VIDIOC_S_FMT:
722 if ((ret = uvc_acquire_privileges(handle)) < 0)
723 return ret;
724
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300725 return uvc_v4l2_set_format(stream, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300726
727 case VIDIOC_G_FMT:
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300728 return uvc_v4l2_get_format(stream, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300729
730 /* Frame size enumeration */
731 case VIDIOC_ENUM_FRAMESIZES:
732 {
733 struct v4l2_frmsizeenum *fsize = arg;
734 struct uvc_format *format = NULL;
735 struct uvc_frame *frame;
736 int i;
737
738 /* Look for the given pixel format */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300739 for (i = 0; i < stream->nformats; i++) {
740 if (stream->format[i].fcc ==
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300741 fsize->pixel_format) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300742 format = &stream->format[i];
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300743 break;
744 }
745 }
746 if (format == NULL)
747 return -EINVAL;
748
749 if (fsize->index >= format->nframes)
750 return -EINVAL;
751
752 frame = &format->frame[fsize->index];
753 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
754 fsize->discrete.width = frame->wWidth;
755 fsize->discrete.height = frame->wHeight;
756 break;
757 }
758
759 /* Frame interval enumeration */
760 case VIDIOC_ENUM_FRAMEINTERVALS:
761 {
762 struct v4l2_frmivalenum *fival = arg;
763 struct uvc_format *format = NULL;
764 struct uvc_frame *frame = NULL;
765 int i;
766
767 /* Look for the given pixel format and frame size */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300768 for (i = 0; i < stream->nformats; i++) {
769 if (stream->format[i].fcc ==
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300770 fival->pixel_format) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300771 format = &stream->format[i];
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300772 break;
773 }
774 }
775 if (format == NULL)
776 return -EINVAL;
777
778 for (i = 0; i < format->nframes; i++) {
779 if (format->frame[i].wWidth == fival->width &&
780 format->frame[i].wHeight == fival->height) {
781 frame = &format->frame[i];
782 break;
783 }
784 }
785 if (frame == NULL)
786 return -EINVAL;
787
788 if (frame->bFrameIntervalType) {
789 if (fival->index >= frame->bFrameIntervalType)
790 return -EINVAL;
791
792 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
793 fival->discrete.numerator =
794 frame->dwFrameInterval[fival->index];
795 fival->discrete.denominator = 10000000;
796 uvc_simplify_fraction(&fival->discrete.numerator,
797 &fival->discrete.denominator, 8, 333);
798 } else {
799 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
800 fival->stepwise.min.numerator =
801 frame->dwFrameInterval[0];
802 fival->stepwise.min.denominator = 10000000;
803 fival->stepwise.max.numerator =
804 frame->dwFrameInterval[1];
805 fival->stepwise.max.denominator = 10000000;
806 fival->stepwise.step.numerator =
807 frame->dwFrameInterval[2];
808 fival->stepwise.step.denominator = 10000000;
809 uvc_simplify_fraction(&fival->stepwise.min.numerator,
810 &fival->stepwise.min.denominator, 8, 333);
811 uvc_simplify_fraction(&fival->stepwise.max.numerator,
812 &fival->stepwise.max.denominator, 8, 333);
813 uvc_simplify_fraction(&fival->stepwise.step.numerator,
814 &fival->stepwise.step.denominator, 8, 333);
815 }
816 break;
817 }
818
819 /* Get & Set streaming parameters */
820 case VIDIOC_G_PARM:
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300821 return uvc_v4l2_get_streamparm(stream, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300822
823 case VIDIOC_S_PARM:
824 if ((ret = uvc_acquire_privileges(handle)) < 0)
825 return ret;
826
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300827 return uvc_v4l2_set_streamparm(stream, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300828
829 /* Cropping and scaling */
830 case VIDIOC_CROPCAP:
831 {
832 struct v4l2_cropcap *ccap = arg;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300833 struct uvc_frame *frame = stream->cur_frame;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300834
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300835 if (ccap->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300836 return -EINVAL;
837
838 ccap->bounds.left = 0;
839 ccap->bounds.top = 0;
840 ccap->bounds.width = frame->wWidth;
841 ccap->bounds.height = frame->wHeight;
842
843 ccap->defrect = ccap->bounds;
844
845 ccap->pixelaspect.numerator = 1;
846 ccap->pixelaspect.denominator = 1;
847 break;
848 }
849
850 case VIDIOC_G_CROP:
851 case VIDIOC_S_CROP:
852 return -EINVAL;
853
854 /* Buffers & streaming */
855 case VIDIOC_REQBUFS:
856 {
857 struct v4l2_requestbuffers *rb = arg;
858 unsigned int bufsize =
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300859 stream->ctrl.dwMaxVideoFrameSize;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300860
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300861 if (rb->type != stream->type ||
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300862 rb->memory != V4L2_MEMORY_MMAP)
863 return -EINVAL;
864
865 if ((ret = uvc_acquire_privileges(handle)) < 0)
866 return ret;
867
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300868 ret = uvc_alloc_buffers(&stream->queue, rb->count, bufsize);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300869 if (ret < 0)
870 return ret;
871
Laurent Pinchart1a969d92009-09-02 03:12:26 -0300872 if (ret == 0)
873 uvc_dismiss_privileges(handle);
874
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300875 rb->count = ret;
876 ret = 0;
877 break;
878 }
879
880 case VIDIOC_QUERYBUF:
881 {
882 struct v4l2_buffer *buf = arg;
883
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300884 if (buf->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300885 return -EINVAL;
886
887 if (!uvc_has_privileges(handle))
888 return -EBUSY;
889
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300890 return uvc_query_buffer(&stream->queue, buf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300891 }
892
893 case VIDIOC_QBUF:
894 if (!uvc_has_privileges(handle))
895 return -EBUSY;
896
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300897 return uvc_queue_buffer(&stream->queue, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300898
899 case VIDIOC_DQBUF:
900 if (!uvc_has_privileges(handle))
901 return -EBUSY;
902
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300903 return uvc_dequeue_buffer(&stream->queue, arg,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300904 file->f_flags & O_NONBLOCK);
905
906 case VIDIOC_STREAMON:
907 {
908 int *type = arg;
909
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300910 if (*type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300911 return -EINVAL;
912
913 if (!uvc_has_privileges(handle))
914 return -EBUSY;
915
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300916 ret = uvc_video_enable(stream, 1);
917 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300918 return ret;
919 break;
920 }
921
922 case VIDIOC_STREAMOFF:
923 {
924 int *type = arg;
925
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300926 if (*type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300927 return -EINVAL;
928
929 if (!uvc_has_privileges(handle))
930 return -EBUSY;
931
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300932 return uvc_video_enable(stream, 0);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300933 }
934
935 /* Analog video standards make no sense for digital cameras. */
936 case VIDIOC_ENUMSTD:
937 case VIDIOC_QUERYSTD:
938 case VIDIOC_G_STD:
939 case VIDIOC_S_STD:
940
941 case VIDIOC_OVERLAY:
942
943 case VIDIOC_ENUMAUDIO:
944 case VIDIOC_ENUMAUDOUT:
945
946 case VIDIOC_ENUMOUTPUT:
947 uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd);
948 return -EINVAL;
949
950 /* Dynamic controls. */
951 case UVCIOC_CTRL_ADD:
952 {
953 struct uvc_xu_control_info *xinfo = arg;
954 struct uvc_control_info *info;
955
956 if (!capable(CAP_SYS_ADMIN))
957 return -EPERM;
958
Laurent Pinchart97683522008-12-16 06:46:32 -0300959 info = kzalloc(sizeof *info, GFP_KERNEL);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300960 if (info == NULL)
961 return -ENOMEM;
962
963 memcpy(info->entity, xinfo->entity, sizeof info->entity);
964 info->index = xinfo->index;
965 info->selector = xinfo->selector;
966 info->size = xinfo->size;
967 info->flags = xinfo->flags;
968
969 info->flags |= UVC_CONTROL_GET_MIN | UVC_CONTROL_GET_MAX |
970 UVC_CONTROL_GET_RES | UVC_CONTROL_GET_DEF;
971
972 ret = uvc_ctrl_add_info(info);
973 if (ret < 0)
974 kfree(info);
975 break;
976 }
977
978 case UVCIOC_CTRL_MAP:
979 {
980 struct uvc_xu_control_mapping *xmap = arg;
981 struct uvc_control_mapping *map;
982
983 if (!capable(CAP_SYS_ADMIN))
984 return -EPERM;
985
Laurent Pinchart97683522008-12-16 06:46:32 -0300986 map = kzalloc(sizeof *map, GFP_KERNEL);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300987 if (map == NULL)
988 return -ENOMEM;
989
990 map->id = xmap->id;
991 memcpy(map->name, xmap->name, sizeof map->name);
992 memcpy(map->entity, xmap->entity, sizeof map->entity);
993 map->selector = xmap->selector;
994 map->size = xmap->size;
995 map->offset = xmap->offset;
996 map->v4l2_type = xmap->v4l2_type;
997 map->data_type = xmap->data_type;
998
999 ret = uvc_ctrl_add_mapping(map);
1000 if (ret < 0)
1001 kfree(map);
1002 break;
1003 }
1004
1005 case UVCIOC_CTRL_GET:
Laurent Pinchart8e113592009-07-01 20:24:47 -03001006 return uvc_xu_ctrl_query(chain, arg, 0);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001007
1008 case UVCIOC_CTRL_SET:
Laurent Pinchart8e113592009-07-01 20:24:47 -03001009 return uvc_xu_ctrl_query(chain, arg, 1);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001010
1011 default:
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -03001012 if ((ret = v4l_compat_translate_ioctl(file, cmd, arg,
Hans Verkuilf473bf72008-11-01 08:25:11 -03001013 uvc_v4l2_do_ioctl)) == -ENOIOCTLCMD)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001014 uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n",
1015 cmd);
1016 return ret;
1017 }
1018
1019 return ret;
1020}
1021
Hans Verkuil069b7472008-12-30 07:04:34 -03001022static long uvc_v4l2_ioctl(struct file *file,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001023 unsigned int cmd, unsigned long arg)
1024{
Laurent Pinchart308bb522008-11-17 18:32:11 -03001025 if (uvc_trace_param & UVC_TRACE_IOCTL) {
1026 uvc_printk(KERN_DEBUG, "uvc_v4l2_ioctl(");
1027 v4l_printk_ioctl(cmd);
1028 printk(")\n");
1029 }
1030
Hans Verkuilf473bf72008-11-01 08:25:11 -03001031 return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001032}
1033
1034static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1035 size_t count, loff_t *ppos)
1036{
1037 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
1038 return -ENODEV;
1039}
1040
1041/*
1042 * VMA operations.
1043 */
1044static void uvc_vm_open(struct vm_area_struct *vma)
1045{
1046 struct uvc_buffer *buffer = vma->vm_private_data;
1047 buffer->vma_use_count++;
1048}
1049
1050static void uvc_vm_close(struct vm_area_struct *vma)
1051{
1052 struct uvc_buffer *buffer = vma->vm_private_data;
1053 buffer->vma_use_count--;
1054}
1055
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001056static const struct vm_operations_struct uvc_vm_ops = {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001057 .open = uvc_vm_open,
1058 .close = uvc_vm_close,
1059};
1060
1061static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1062{
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001063 struct uvc_fh *handle = (struct uvc_fh *)file->private_data;
1064 struct uvc_streaming *stream = handle->stream;
1065 struct uvc_video_queue *queue = &stream->queue;
Andrew Morton6f80e1b2008-07-04 06:33:23 -03001066 struct uvc_buffer *uninitialized_var(buffer);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001067 struct page *page;
1068 unsigned long addr, start, size;
1069 unsigned int i;
1070 int ret = 0;
1071
1072 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
1073
1074 start = vma->vm_start;
1075 size = vma->vm_end - vma->vm_start;
1076
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001077 mutex_lock(&queue->mutex);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001078
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001079 for (i = 0; i < queue->count; ++i) {
1080 buffer = &queue->buffer[i];
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001081 if ((buffer->buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
1082 break;
1083 }
1084
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001085 if (i == queue->count || size != queue->buf_size) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001086 ret = -EINVAL;
1087 goto done;
1088 }
1089
1090 /*
1091 * VM_IO marks the area as being an mmaped region for I/O to a
1092 * device. It also prevents the region from being core dumped.
1093 */
1094 vma->vm_flags |= VM_IO;
1095
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001096 addr = (unsigned long)queue->mem + buffer->buf.m.offset;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001097 while (size > 0) {
1098 page = vmalloc_to_page((void *)addr);
1099 if ((ret = vm_insert_page(vma, start, page)) < 0)
1100 goto done;
1101
1102 start += PAGE_SIZE;
1103 addr += PAGE_SIZE;
1104 size -= PAGE_SIZE;
1105 }
1106
1107 vma->vm_ops = &uvc_vm_ops;
1108 vma->vm_private_data = buffer;
1109 uvc_vm_open(vma);
1110
1111done:
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001112 mutex_unlock(&queue->mutex);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001113 return ret;
1114}
1115
1116static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
1117{
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001118 struct uvc_fh *handle = (struct uvc_fh *)file->private_data;
1119 struct uvc_streaming *stream = handle->stream;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001120
1121 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
1122
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001123 return uvc_queue_poll(&stream->queue, file, wait);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001124}
1125
Hans Verkuilbec43662008-12-30 06:58:20 -03001126const struct v4l2_file_operations uvc_fops = {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001127 .owner = THIS_MODULE,
1128 .open = uvc_v4l2_open,
1129 .release = uvc_v4l2_release,
1130 .ioctl = uvc_v4l2_ioctl,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001131 .read = uvc_v4l2_read,
1132 .mmap = uvc_v4l2_mmap,
1133 .poll = uvc_v4l2_poll,
1134};
Hans Verkuilf87086e2008-07-18 00:50:58 -03001135