blob: 4a510483f7c54a34903d6c912dbc2cd6b702d856 [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/*
104 * Mapping V4L2 controls to UVC controls can be straighforward if done well.
105 * Most of the UVC controls exist in V4L2, and can be mapped directly. Some
106 * must be grouped (for instance the Red Balance, Blue Balance and Do White
107 * Balance V4L2 controls use the White Balance Component UVC control) or
108 * otherwise translated. The approach we take here is to use a translation
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300109 * table for the controls that can be mapped directly, and handle the others
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300110 * manually.
111 */
Laurent Pinchart8e113592009-07-01 20:24:47 -0300112static int uvc_v4l2_query_menu(struct uvc_video_chain *chain,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300113 struct v4l2_querymenu *query_menu)
114{
115 struct uvc_menu_info *menu_info;
116 struct uvc_control_mapping *mapping;
117 struct uvc_control *ctrl;
Márton Németh2460cda2009-04-20 14:51:49 -0300118 u32 index = query_menu->index;
119 u32 id = query_menu->id;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300120
Laurent Pinchart8e113592009-07-01 20:24:47 -0300121 ctrl = uvc_find_control(chain, query_menu->id, &mapping);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300122 if (ctrl == NULL || mapping->v4l2_type != V4L2_CTRL_TYPE_MENU)
123 return -EINVAL;
124
125 if (query_menu->index >= mapping->menu_count)
126 return -EINVAL;
127
Márton Németh2460cda2009-04-20 14:51:49 -0300128 memset(query_menu, 0, sizeof(*query_menu));
129 query_menu->id = id;
130 query_menu->index = index;
131
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300132 menu_info = &mapping->menu_info[query_menu->index];
Laurent Pinchartd0ebf302009-01-08 14:05:11 -0300133 strlcpy(query_menu->name, menu_info->name, sizeof query_menu->name);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300134 return 0;
135}
136
137/*
138 * Find the frame interval closest to the requested frame interval for the
139 * given frame format and size. This should be done by the device as part of
140 * the Video Probe and Commit negotiation, but some hardware don't implement
141 * that feature.
142 */
143static __u32 uvc_try_frame_interval(struct uvc_frame *frame, __u32 interval)
144{
145 unsigned int i;
146
147 if (frame->bFrameIntervalType) {
148 __u32 best = -1, dist;
149
150 for (i = 0; i < frame->bFrameIntervalType; ++i) {
151 dist = interval > frame->dwFrameInterval[i]
152 ? interval - frame->dwFrameInterval[i]
153 : frame->dwFrameInterval[i] - interval;
154
155 if (dist > best)
156 break;
157
158 best = dist;
159 }
160
161 interval = frame->dwFrameInterval[i-1];
162 } else {
163 const __u32 min = frame->dwFrameInterval[0];
164 const __u32 max = frame->dwFrameInterval[1];
165 const __u32 step = frame->dwFrameInterval[2];
166
167 interval = min + (interval - min + step/2) / step * step;
168 if (interval > max)
169 interval = max;
170 }
171
172 return interval;
173}
174
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300175static int uvc_v4l2_try_format(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300176 struct v4l2_format *fmt, struct uvc_streaming_control *probe,
177 struct uvc_format **uvc_format, struct uvc_frame **uvc_frame)
178{
179 struct uvc_format *format = NULL;
180 struct uvc_frame *frame = NULL;
181 __u16 rw, rh;
182 unsigned int d, maxd;
183 unsigned int i;
184 __u32 interval;
185 int ret = 0;
186 __u8 *fcc;
187
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300188 if (fmt->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300189 return -EINVAL;
190
191 fcc = (__u8 *)&fmt->fmt.pix.pixelformat;
192 uvc_trace(UVC_TRACE_FORMAT, "Trying format 0x%08x (%c%c%c%c): %ux%u.\n",
193 fmt->fmt.pix.pixelformat,
194 fcc[0], fcc[1], fcc[2], fcc[3],
195 fmt->fmt.pix.width, fmt->fmt.pix.height);
196
197 /* Check if the hardware supports the requested format. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300198 for (i = 0; i < stream->nformats; ++i) {
199 format = &stream->format[i];
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300200 if (format->fcc == fmt->fmt.pix.pixelformat)
201 break;
202 }
203
204 if (format == NULL || format->fcc != fmt->fmt.pix.pixelformat) {
205 uvc_trace(UVC_TRACE_FORMAT, "Unsupported format 0x%08x.\n",
206 fmt->fmt.pix.pixelformat);
207 return -EINVAL;
208 }
209
210 /* Find the closest image size. The distance between image sizes is
211 * the size in pixels of the non-overlapping regions between the
212 * requested size and the frame-specified size.
213 */
214 rw = fmt->fmt.pix.width;
215 rh = fmt->fmt.pix.height;
216 maxd = (unsigned int)-1;
217
218 for (i = 0; i < format->nframes; ++i) {
219 __u16 w = format->frame[i].wWidth;
220 __u16 h = format->frame[i].wHeight;
221
222 d = min(w, rw) * min(h, rh);
223 d = w*h + rw*rh - 2*d;
224 if (d < maxd) {
225 maxd = d;
226 frame = &format->frame[i];
227 }
228
229 if (maxd == 0)
230 break;
231 }
232
233 if (frame == NULL) {
234 uvc_trace(UVC_TRACE_FORMAT, "Unsupported size %ux%u.\n",
235 fmt->fmt.pix.width, fmt->fmt.pix.height);
236 return -EINVAL;
237 }
238
239 /* Use the default frame interval. */
240 interval = frame->dwDefaultFrameInterval;
241 uvc_trace(UVC_TRACE_FORMAT, "Using default frame interval %u.%u us "
242 "(%u.%u fps).\n", interval/10, interval%10, 10000000/interval,
243 (100000000/interval)%10);
244
245 /* Set the format index, frame index and frame interval. */
246 memset(probe, 0, sizeof *probe);
247 probe->bmHint = 1; /* dwFrameInterval */
248 probe->bFormatIndex = format->index;
249 probe->bFrameIndex = frame->bFrameIndex;
250 probe->dwFrameInterval = uvc_try_frame_interval(frame, interval);
251 /* Some webcams stall the probe control set request when the
252 * dwMaxVideoFrameSize field is set to zero. The UVC specification
253 * clearly states that the field is read-only from the host, so this
254 * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by
255 * the webcam to work around the problem.
256 *
257 * The workaround could probably be enabled for all webcams, so the
258 * quirk can be removed if needed. It's currently useful to detect
259 * webcam bugs and fix them before they hit the market (providing
260 * developers test their webcams with the Linux driver as well as with
261 * the Windows driver).
262 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300263 if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300264 probe->dwMaxVideoFrameSize =
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300265 stream->ctrl.dwMaxVideoFrameSize;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300266
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300267 /* Probe the device. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300268 ret = uvc_probe_video(stream, probe);
269 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300270 goto done;
271
272 fmt->fmt.pix.width = frame->wWidth;
273 fmt->fmt.pix.height = frame->wHeight;
274 fmt->fmt.pix.field = V4L2_FIELD_NONE;
275 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
276 fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize;
277 fmt->fmt.pix.colorspace = format->colorspace;
278 fmt->fmt.pix.priv = 0;
279
280 if (uvc_format != NULL)
281 *uvc_format = format;
282 if (uvc_frame != NULL)
283 *uvc_frame = frame;
284
285done:
286 return ret;
287}
288
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300289static int uvc_v4l2_get_format(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300290 struct v4l2_format *fmt)
291{
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300292 struct uvc_format *format = stream->cur_format;
293 struct uvc_frame *frame = stream->cur_frame;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300294
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300295 if (fmt->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300296 return -EINVAL;
297
298 if (format == NULL || frame == NULL)
299 return -EINVAL;
300
301 fmt->fmt.pix.pixelformat = format->fcc;
302 fmt->fmt.pix.width = frame->wWidth;
303 fmt->fmt.pix.height = frame->wHeight;
304 fmt->fmt.pix.field = V4L2_FIELD_NONE;
305 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300306 fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300307 fmt->fmt.pix.colorspace = format->colorspace;
308 fmt->fmt.pix.priv = 0;
309
310 return 0;
311}
312
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300313static int uvc_v4l2_set_format(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300314 struct v4l2_format *fmt)
315{
316 struct uvc_streaming_control probe;
317 struct uvc_format *format;
318 struct uvc_frame *frame;
319 int ret;
320
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300321 if (fmt->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300322 return -EINVAL;
323
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300324 if (uvc_queue_allocated(&stream->queue))
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300325 return -EBUSY;
326
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300327 ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300328 if (ret < 0)
329 return ret;
330
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300331 memcpy(&stream->ctrl, &probe, sizeof probe);
332 stream->cur_format = format;
333 stream->cur_frame = frame;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300334
335 return 0;
336}
337
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300338static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300339 struct v4l2_streamparm *parm)
340{
341 uint32_t numerator, denominator;
342
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300343 if (parm->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300344 return -EINVAL;
345
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300346 numerator = stream->ctrl.dwFrameInterval;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300347 denominator = 10000000;
348 uvc_simplify_fraction(&numerator, &denominator, 8, 333);
349
350 memset(parm, 0, sizeof *parm);
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300351 parm->type = stream->type;
Laurent Pinchartff924202008-12-28 22:32:29 -0300352
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300353 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Laurent Pinchartff924202008-12-28 22:32:29 -0300354 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
355 parm->parm.capture.capturemode = 0;
356 parm->parm.capture.timeperframe.numerator = numerator;
357 parm->parm.capture.timeperframe.denominator = denominator;
358 parm->parm.capture.extendedmode = 0;
359 parm->parm.capture.readbuffers = 0;
360 } else {
361 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
362 parm->parm.output.outputmode = 0;
363 parm->parm.output.timeperframe.numerator = numerator;
364 parm->parm.output.timeperframe.denominator = denominator;
365 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300366
367 return 0;
368}
369
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300370static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300371 struct v4l2_streamparm *parm)
372{
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300373 struct uvc_frame *frame = stream->cur_frame;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300374 struct uvc_streaming_control probe;
Laurent Pinchartff924202008-12-28 22:32:29 -0300375 struct v4l2_fract timeperframe;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300376 uint32_t interval;
377 int ret;
378
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300379 if (parm->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300380 return -EINVAL;
381
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300382 if (uvc_queue_streaming(&stream->queue))
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300383 return -EBUSY;
384
Laurent Pinchartff924202008-12-28 22:32:29 -0300385 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
386 timeperframe = parm->parm.capture.timeperframe;
387 else
388 timeperframe = parm->parm.output.timeperframe;
389
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300390 memcpy(&probe, &stream->ctrl, sizeof probe);
Laurent Pinchartff924202008-12-28 22:32:29 -0300391 interval = uvc_fraction_to_interval(timeperframe.numerator,
392 timeperframe.denominator);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300393
394 uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
Laurent Pinchartff924202008-12-28 22:32:29 -0300395 timeperframe.numerator, timeperframe.denominator, interval);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300396 probe.dwFrameInterval = uvc_try_frame_interval(frame, interval);
397
398 /* Probe the device with the new settings. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300399 ret = uvc_probe_video(stream, &probe);
400 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300401 return ret;
402
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300403 memcpy(&stream->ctrl, &probe, sizeof probe);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300404
405 /* Return the actual frame period. */
Laurent Pinchartff924202008-12-28 22:32:29 -0300406 timeperframe.numerator = probe.dwFrameInterval;
407 timeperframe.denominator = 10000000;
408 uvc_simplify_fraction(&timeperframe.numerator,
409 &timeperframe.denominator, 8, 333);
410
411 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
412 parm->parm.capture.timeperframe = timeperframe;
413 else
414 parm->parm.output.timeperframe = timeperframe;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300415
416 return 0;
417}
418
419/* ------------------------------------------------------------------------
420 * Privilege management
421 */
422
423/*
424 * Privilege management is the multiple-open implementation basis. The current
425 * implementation is completely transparent for the end-user and doesn't
426 * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls.
427 * Those ioctls enable finer control on the device (by making possible for a
428 * user to request exclusive access to a device), but are not mature yet.
429 * Switching to the V4L2 priority mechanism might be considered in the future
430 * if this situation changes.
431 *
432 * Each open instance of a UVC device can either be in a privileged or
433 * unprivileged state. Only a single instance can be in a privileged state at
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300434 * a given time. Trying to perform an operation that requires privileges will
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300435 * automatically acquire the required privileges if possible, or return -EBUSY
Laurent Pinchart1a969d92009-09-02 03:12:26 -0300436 * otherwise. Privileges are dismissed when closing the instance or when
437 * freeing the video buffers using VIDIOC_REQBUFS.
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300438 *
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300439 * Operations that require privileges are:
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300440 *
441 * - VIDIOC_S_INPUT
442 * - VIDIOC_S_PARM
443 * - VIDIOC_S_FMT
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300444 * - VIDIOC_REQBUFS
445 */
446static int uvc_acquire_privileges(struct uvc_fh *handle)
447{
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300448 /* Always succeed if the handle is already privileged. */
449 if (handle->state == UVC_HANDLE_ACTIVE)
450 return 0;
451
452 /* Check if the device already has a privileged handle. */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300453 if (atomic_inc_return(&handle->stream->active) != 1) {
454 atomic_dec(&handle->stream->active);
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300455 return -EBUSY;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300456 }
457
458 handle->state = UVC_HANDLE_ACTIVE;
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300459 return 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300460}
461
462static void uvc_dismiss_privileges(struct uvc_fh *handle)
463{
464 if (handle->state == UVC_HANDLE_ACTIVE)
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300465 atomic_dec(&handle->stream->active);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300466
467 handle->state = UVC_HANDLE_PASSIVE;
468}
469
470static int uvc_has_privileges(struct uvc_fh *handle)
471{
472 return handle->state == UVC_HANDLE_ACTIVE;
473}
474
475/* ------------------------------------------------------------------------
476 * V4L2 file operations
477 */
478
Hans Verkuilbec43662008-12-30 06:58:20 -0300479static int uvc_v4l2_open(struct file *file)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300480{
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300481 struct uvc_streaming *stream;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300482 struct uvc_fh *handle;
483 int ret = 0;
484
485 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300486 stream = video_drvdata(file);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300487
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300488 if (stream->dev->state & UVC_DEV_DISCONNECTED)
489 return -ENODEV;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300490
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300491 ret = usb_autopm_get_interface(stream->dev->intf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300492 if (ret < 0)
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300493 return ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300494
495 /* Create the device handle. */
496 handle = kzalloc(sizeof *handle, GFP_KERNEL);
497 if (handle == NULL) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300498 usb_autopm_put_interface(stream->dev->intf);
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300499 return -ENOMEM;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300500 }
501
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300502 if (atomic_inc_return(&stream->dev->users) == 1) {
503 ret = uvc_status_start(stream->dev);
504 if (ret < 0) {
505 usb_autopm_put_interface(stream->dev->intf);
506 atomic_dec(&stream->dev->users);
Laurent Pinchart04a37e02009-05-19 10:08:03 -0300507 kfree(handle);
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300508 return ret;
Laurent Pinchart04a37e02009-05-19 10:08:03 -0300509 }
510 }
511
Laurent Pinchart8e113592009-07-01 20:24:47 -0300512 handle->chain = stream->chain;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300513 handle->stream = stream;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300514 handle->state = UVC_HANDLE_PASSIVE;
515 file->private_data = handle;
516
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300517 return 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300518}
519
Hans Verkuilbec43662008-12-30 06:58:20 -0300520static int uvc_v4l2_release(struct file *file)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300521{
Joe Perchesabf84382010-07-12 17:50:03 -0300522 struct uvc_fh *handle = file->private_data;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300523 struct uvc_streaming *stream = handle->stream;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300524
525 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");
526
527 /* Only free resources if this is a privileged handle. */
528 if (uvc_has_privileges(handle)) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300529 uvc_video_enable(stream, 0);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300530
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300531 mutex_lock(&stream->queue.mutex);
532 if (uvc_free_buffers(&stream->queue) < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300533 uvc_printk(KERN_ERR, "uvc_v4l2_release: Unable to "
534 "free buffers.\n");
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300535 mutex_unlock(&stream->queue.mutex);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300536 }
537
538 /* Release the file handle. */
539 uvc_dismiss_privileges(handle);
540 kfree(handle);
541 file->private_data = NULL;
542
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300543 if (atomic_dec_return(&stream->dev->users) == 0)
544 uvc_status_stop(stream->dev);
Laurent Pinchart04a37e02009-05-19 10:08:03 -0300545
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300546 usb_autopm_put_interface(stream->dev->intf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300547 return 0;
548}
549
Hans Verkuil069b7472008-12-30 07:04:34 -0300550static long uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300551{
552 struct video_device *vdev = video_devdata(file);
Joe Perchesabf84382010-07-12 17:50:03 -0300553 struct uvc_fh *handle = file->private_data;
Laurent Pinchart8e113592009-07-01 20:24:47 -0300554 struct uvc_video_chain *chain = handle->chain;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300555 struct uvc_streaming *stream = handle->stream;
Hans Verkuil069b7472008-12-30 07:04:34 -0300556 long ret = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300557
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300558 switch (cmd) {
559 /* Query capabilities */
560 case VIDIOC_QUERYCAP:
561 {
562 struct v4l2_capability *cap = arg;
563
564 memset(cap, 0, sizeof *cap);
Laurent Pinchartd0ebf302009-01-08 14:05:11 -0300565 strlcpy(cap->driver, "uvcvideo", sizeof cap->driver);
566 strlcpy(cap->card, vdev->name, sizeof cap->card);
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300567 usb_make_path(stream->dev->udev,
Thierry MERLEb12049a2009-01-27 16:53:23 -0300568 cap->bus_info, sizeof(cap->bus_info));
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300569 cap->version = DRIVER_VERSION_NUMBER;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300570 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
Laurent Pinchartff924202008-12-28 22:32:29 -0300571 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
572 | V4L2_CAP_STREAMING;
573 else
574 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT
575 | V4L2_CAP_STREAMING;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300576 break;
577 }
578
579 /* Get, Set & Query control */
580 case VIDIOC_QUERYCTRL:
Laurent Pinchart8e113592009-07-01 20:24:47 -0300581 return uvc_query_v4l2_ctrl(chain, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300582
583 case VIDIOC_G_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
Laurent Pinchart8e113592009-07-01 20:24:47 -0300591 ret = uvc_ctrl_begin(chain);
592 if (ret < 0)
Robert Krakora1fcbcc42009-06-12 13:51:03 -0300593 return ret;
594
Laurent Pinchart8e113592009-07-01 20:24:47 -0300595 ret = uvc_ctrl_get(chain, &xctrl);
596 uvc_ctrl_rollback(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300597 if (ret >= 0)
598 ctrl->value = xctrl.value;
599 break;
600 }
601
602 case VIDIOC_S_CTRL:
603 {
604 struct v4l2_control *ctrl = arg;
605 struct v4l2_ext_control xctrl;
606
607 memset(&xctrl, 0, sizeof xctrl);
608 xctrl.id = ctrl->id;
609 xctrl.value = ctrl->value;
610
Laurent Pinchart8d556622010-02-04 21:43:37 -0300611 ret = uvc_ctrl_begin(chain);
Laurent Pinchart8e113592009-07-01 20:24:47 -0300612 if (ret < 0)
Robert Krakora1fcbcc42009-06-12 13:51:03 -0300613 return ret;
614
Laurent Pinchart8e113592009-07-01 20:24:47 -0300615 ret = uvc_ctrl_set(chain, &xctrl);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300616 if (ret < 0) {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300617 uvc_ctrl_rollback(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300618 return ret;
619 }
Laurent Pinchart8e113592009-07-01 20:24:47 -0300620 ret = uvc_ctrl_commit(chain);
Laurent Pincharte54532e2010-01-23 07:07:53 -0300621 if (ret == 0)
622 ctrl->value = xctrl.value;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300623 break;
624 }
625
626 case VIDIOC_QUERYMENU:
Laurent Pinchart8e113592009-07-01 20:24:47 -0300627 return uvc_v4l2_query_menu(chain, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300628
629 case VIDIOC_G_EXT_CTRLS:
630 {
631 struct v4l2_ext_controls *ctrls = arg;
632 struct v4l2_ext_control *ctrl = ctrls->controls;
633 unsigned int i;
634
Laurent Pinchart8e113592009-07-01 20:24:47 -0300635 ret = uvc_ctrl_begin(chain);
636 if (ret < 0)
Robert Krakora1fcbcc42009-06-12 13:51:03 -0300637 return ret;
638
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300639 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300640 ret = uvc_ctrl_get(chain, ctrl);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300641 if (ret < 0) {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300642 uvc_ctrl_rollback(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300643 ctrls->error_idx = i;
644 return ret;
645 }
646 }
647 ctrls->error_idx = 0;
Laurent Pinchart8e113592009-07-01 20:24:47 -0300648 ret = uvc_ctrl_rollback(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300649 break;
650 }
651
652 case VIDIOC_S_EXT_CTRLS:
653 case VIDIOC_TRY_EXT_CTRLS:
654 {
655 struct v4l2_ext_controls *ctrls = arg;
656 struct v4l2_ext_control *ctrl = ctrls->controls;
657 unsigned int i;
658
Laurent Pinchart8e113592009-07-01 20:24:47 -0300659 ret = uvc_ctrl_begin(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300660 if (ret < 0)
661 return ret;
662
663 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300664 ret = uvc_ctrl_set(chain, ctrl);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300665 if (ret < 0) {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300666 uvc_ctrl_rollback(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300667 ctrls->error_idx = i;
668 return ret;
669 }
670 }
671
672 ctrls->error_idx = 0;
673
674 if (cmd == VIDIOC_S_EXT_CTRLS)
Laurent Pinchart8e113592009-07-01 20:24:47 -0300675 ret = uvc_ctrl_commit(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300676 else
Laurent Pinchart8e113592009-07-01 20:24:47 -0300677 ret = uvc_ctrl_rollback(chain);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300678 break;
679 }
680
681 /* Get, Set & Enum input */
682 case VIDIOC_ENUMINPUT:
683 {
Laurent Pinchart8e113592009-07-01 20:24:47 -0300684 const struct uvc_entity *selector = chain->selector;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300685 struct v4l2_input *input = arg;
686 struct uvc_entity *iterm = NULL;
687 u32 index = input->index;
688 int pin = 0;
689
690 if (selector == NULL ||
Laurent Pinchart8e113592009-07-01 20:24:47 -0300691 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300692 if (index != 0)
693 return -EINVAL;
Laurent Pinchart6241d8c2009-11-25 12:00:22 -0300694 list_for_each_entry(iterm, &chain->entities, chain) {
695 if (UVC_ENTITY_IS_ITERM(iterm))
696 break;
697 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300698 pin = iterm->id;
Laurent Pinchart8ca5a632009-11-25 12:00:30 -0300699 } else if (pin < selector->bNrInPins) {
700 pin = selector->baSourceID[index];
Laurent Pinchart6241d8c2009-11-25 12:00:22 -0300701 list_for_each_entry(iterm, &chain->entities, chain) {
702 if (!UVC_ENTITY_IS_ITERM(iterm))
703 continue;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300704 if (iterm->id == pin)
705 break;
706 }
707 }
708
709 if (iterm == NULL || iterm->id != pin)
710 return -EINVAL;
711
712 memset(input, 0, sizeof *input);
713 input->index = index;
Laurent Pinchartd0ebf302009-01-08 14:05:11 -0300714 strlcpy(input->name, iterm->name, sizeof input->name);
Laurent Pinchartb482d922009-06-26 11:39:42 -0300715 if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300716 input->type = V4L2_INPUT_TYPE_CAMERA;
717 break;
718 }
719
720 case VIDIOC_G_INPUT:
721 {
722 u8 input;
723
Laurent Pinchart8e113592009-07-01 20:24:47 -0300724 if (chain->selector == NULL ||
725 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300726 *(int *)arg = 0;
727 break;
728 }
729
Laurent Pinchart8e113592009-07-01 20:24:47 -0300730 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
731 chain->selector->id, chain->dev->intfnum,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300732 UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300733 if (ret < 0)
734 return ret;
735
736 *(int *)arg = input - 1;
737 break;
738 }
739
740 case VIDIOC_S_INPUT:
741 {
Márton Németh9086c7b992009-04-15 09:01:58 -0300742 u32 input = *(u32 *)arg + 1;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300743
744 if ((ret = uvc_acquire_privileges(handle)) < 0)
745 return ret;
746
Laurent Pinchart8e113592009-07-01 20:24:47 -0300747 if (chain->selector == NULL ||
748 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300749 if (input != 1)
750 return -EINVAL;
751 break;
752 }
753
Laurent Pinchart8ca5a632009-11-25 12:00:30 -0300754 if (input == 0 || input > chain->selector->bNrInPins)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300755 return -EINVAL;
756
Laurent Pinchart8e113592009-07-01 20:24:47 -0300757 return uvc_query_ctrl(chain->dev, UVC_SET_CUR,
758 chain->selector->id, chain->dev->intfnum,
Laurent Pinchartb482d922009-06-26 11:39:42 -0300759 UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300760 }
761
762 /* Try, Get, Set & Enum format */
763 case VIDIOC_ENUM_FMT:
764 {
765 struct v4l2_fmtdesc *fmt = arg;
766 struct uvc_format *format;
Márton Németh8bbd90c2009-03-27 11:13:57 -0300767 enum v4l2_buf_type type = fmt->type;
768 __u32 index = fmt->index;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300769
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300770 if (fmt->type != stream->type ||
771 fmt->index >= stream->nformats)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300772 return -EINVAL;
773
Márton Németh8bbd90c2009-03-27 11:13:57 -0300774 memset(fmt, 0, sizeof(*fmt));
775 fmt->index = index;
776 fmt->type = type;
777
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300778 format = &stream->format[fmt->index];
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300779 fmt->flags = 0;
780 if (format->flags & UVC_FMT_FLAG_COMPRESSED)
781 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
Laurent Pinchartd0ebf302009-01-08 14:05:11 -0300782 strlcpy(fmt->description, format->name,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300783 sizeof fmt->description);
784 fmt->description[sizeof fmt->description - 1] = 0;
785 fmt->pixelformat = format->fcc;
786 break;
787 }
788
789 case VIDIOC_TRY_FMT:
790 {
791 struct uvc_streaming_control probe;
792
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300793 return uvc_v4l2_try_format(stream, arg, &probe, NULL, NULL);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300794 }
795
796 case VIDIOC_S_FMT:
797 if ((ret = uvc_acquire_privileges(handle)) < 0)
798 return ret;
799
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300800 return uvc_v4l2_set_format(stream, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300801
802 case VIDIOC_G_FMT:
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300803 return uvc_v4l2_get_format(stream, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300804
805 /* Frame size enumeration */
806 case VIDIOC_ENUM_FRAMESIZES:
807 {
808 struct v4l2_frmsizeenum *fsize = arg;
809 struct uvc_format *format = NULL;
810 struct uvc_frame *frame;
811 int i;
812
813 /* Look for the given pixel format */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300814 for (i = 0; i < stream->nformats; i++) {
815 if (stream->format[i].fcc ==
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300816 fsize->pixel_format) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300817 format = &stream->format[i];
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300818 break;
819 }
820 }
821 if (format == NULL)
822 return -EINVAL;
823
824 if (fsize->index >= format->nframes)
825 return -EINVAL;
826
827 frame = &format->frame[fsize->index];
828 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
829 fsize->discrete.width = frame->wWidth;
830 fsize->discrete.height = frame->wHeight;
831 break;
832 }
833
834 /* Frame interval enumeration */
835 case VIDIOC_ENUM_FRAMEINTERVALS:
836 {
837 struct v4l2_frmivalenum *fival = arg;
838 struct uvc_format *format = NULL;
839 struct uvc_frame *frame = NULL;
840 int i;
841
842 /* Look for the given pixel format and frame size */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300843 for (i = 0; i < stream->nformats; i++) {
844 if (stream->format[i].fcc ==
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300845 fival->pixel_format) {
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300846 format = &stream->format[i];
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300847 break;
848 }
849 }
850 if (format == NULL)
851 return -EINVAL;
852
853 for (i = 0; i < format->nframes; i++) {
854 if (format->frame[i].wWidth == fival->width &&
855 format->frame[i].wHeight == fival->height) {
856 frame = &format->frame[i];
857 break;
858 }
859 }
860 if (frame == NULL)
861 return -EINVAL;
862
863 if (frame->bFrameIntervalType) {
864 if (fival->index >= frame->bFrameIntervalType)
865 return -EINVAL;
866
867 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
868 fival->discrete.numerator =
869 frame->dwFrameInterval[fival->index];
870 fival->discrete.denominator = 10000000;
871 uvc_simplify_fraction(&fival->discrete.numerator,
872 &fival->discrete.denominator, 8, 333);
873 } else {
874 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
875 fival->stepwise.min.numerator =
876 frame->dwFrameInterval[0];
877 fival->stepwise.min.denominator = 10000000;
878 fival->stepwise.max.numerator =
879 frame->dwFrameInterval[1];
880 fival->stepwise.max.denominator = 10000000;
881 fival->stepwise.step.numerator =
882 frame->dwFrameInterval[2];
883 fival->stepwise.step.denominator = 10000000;
884 uvc_simplify_fraction(&fival->stepwise.min.numerator,
885 &fival->stepwise.min.denominator, 8, 333);
886 uvc_simplify_fraction(&fival->stepwise.max.numerator,
887 &fival->stepwise.max.denominator, 8, 333);
888 uvc_simplify_fraction(&fival->stepwise.step.numerator,
889 &fival->stepwise.step.denominator, 8, 333);
890 }
891 break;
892 }
893
894 /* Get & Set streaming parameters */
895 case VIDIOC_G_PARM:
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300896 return uvc_v4l2_get_streamparm(stream, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300897
898 case VIDIOC_S_PARM:
899 if ((ret = uvc_acquire_privileges(handle)) < 0)
900 return ret;
901
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300902 return uvc_v4l2_set_streamparm(stream, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300903
904 /* Cropping and scaling */
905 case VIDIOC_CROPCAP:
906 {
907 struct v4l2_cropcap *ccap = arg;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300908 struct uvc_frame *frame = stream->cur_frame;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300909
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300910 if (ccap->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300911 return -EINVAL;
912
913 ccap->bounds.left = 0;
914 ccap->bounds.top = 0;
915 ccap->bounds.width = frame->wWidth;
916 ccap->bounds.height = frame->wHeight;
917
918 ccap->defrect = ccap->bounds;
919
920 ccap->pixelaspect.numerator = 1;
921 ccap->pixelaspect.denominator = 1;
922 break;
923 }
924
925 case VIDIOC_G_CROP:
926 case VIDIOC_S_CROP:
927 return -EINVAL;
928
929 /* Buffers & streaming */
930 case VIDIOC_REQBUFS:
931 {
932 struct v4l2_requestbuffers *rb = arg;
933 unsigned int bufsize =
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300934 stream->ctrl.dwMaxVideoFrameSize;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300935
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300936 if (rb->type != stream->type ||
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300937 rb->memory != V4L2_MEMORY_MMAP)
938 return -EINVAL;
939
940 if ((ret = uvc_acquire_privileges(handle)) < 0)
941 return ret;
942
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300943 ret = uvc_alloc_buffers(&stream->queue, rb->count, bufsize);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300944 if (ret < 0)
945 return ret;
946
Laurent Pinchart1a969d92009-09-02 03:12:26 -0300947 if (ret == 0)
948 uvc_dismiss_privileges(handle);
949
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300950 rb->count = ret;
951 ret = 0;
952 break;
953 }
954
955 case VIDIOC_QUERYBUF:
956 {
957 struct v4l2_buffer *buf = arg;
958
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300959 if (buf->type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300960 return -EINVAL;
961
962 if (!uvc_has_privileges(handle))
963 return -EBUSY;
964
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300965 return uvc_query_buffer(&stream->queue, buf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300966 }
967
968 case VIDIOC_QBUF:
969 if (!uvc_has_privileges(handle))
970 return -EBUSY;
971
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300972 return uvc_queue_buffer(&stream->queue, arg);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300973
974 case VIDIOC_DQBUF:
975 if (!uvc_has_privileges(handle))
976 return -EBUSY;
977
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300978 return uvc_dequeue_buffer(&stream->queue, arg,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300979 file->f_flags & O_NONBLOCK);
980
981 case VIDIOC_STREAMON:
982 {
983 int *type = arg;
984
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300985 if (*type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300986 return -EINVAL;
987
988 if (!uvc_has_privileges(handle))
989 return -EBUSY;
990
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300991 ret = uvc_video_enable(stream, 1);
992 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300993 return ret;
994 break;
995 }
996
997 case VIDIOC_STREAMOFF:
998 {
999 int *type = arg;
1000
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001001 if (*type != stream->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001002 return -EINVAL;
1003
1004 if (!uvc_has_privileges(handle))
1005 return -EBUSY;
1006
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001007 return uvc_video_enable(stream, 0);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001008 }
1009
1010 /* Analog video standards make no sense for digital cameras. */
1011 case VIDIOC_ENUMSTD:
1012 case VIDIOC_QUERYSTD:
1013 case VIDIOC_G_STD:
1014 case VIDIOC_S_STD:
1015
1016 case VIDIOC_OVERLAY:
1017
1018 case VIDIOC_ENUMAUDIO:
1019 case VIDIOC_ENUMAUDOUT:
1020
1021 case VIDIOC_ENUMOUTPUT:
1022 uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd);
1023 return -EINVAL;
1024
1025 /* Dynamic controls. */
1026 case UVCIOC_CTRL_ADD:
Laurent Pinchartba2fa992010-09-20 05:53:21 -03001027 /* Legacy ioctl, kept for API compatibility reasons */
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001028 if (!capable(CAP_SYS_ADMIN))
1029 return -EPERM;
1030
Laurent Pinchartba2fa992010-09-20 05:53:21 -03001031 return -EEXIST;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001032
Laurent Pinchart561474c22010-02-18 16:38:52 -03001033 case UVCIOC_CTRL_MAP_OLD:
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001034 case UVCIOC_CTRL_MAP:
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001035 if (!capable(CAP_SYS_ADMIN))
1036 return -EPERM;
1037
Laurent Pinchartba2fa992010-09-20 05:53:21 -03001038 return uvc_ioctl_ctrl_map(chain, arg,
1039 cmd == UVCIOC_CTRL_MAP_OLD);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001040
1041 case UVCIOC_CTRL_GET:
Laurent Pinchart8e113592009-07-01 20:24:47 -03001042 return uvc_xu_ctrl_query(chain, arg, 0);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001043
1044 case UVCIOC_CTRL_SET:
Laurent Pinchart8e113592009-07-01 20:24:47 -03001045 return uvc_xu_ctrl_query(chain, arg, 1);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001046
1047 default:
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -03001048 if ((ret = v4l_compat_translate_ioctl(file, cmd, arg,
Hans Verkuilf473bf72008-11-01 08:25:11 -03001049 uvc_v4l2_do_ioctl)) == -ENOIOCTLCMD)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001050 uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n",
1051 cmd);
1052 return ret;
1053 }
1054
1055 return ret;
1056}
1057
Hans Verkuil069b7472008-12-30 07:04:34 -03001058static long uvc_v4l2_ioctl(struct file *file,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001059 unsigned int cmd, unsigned long arg)
1060{
Laurent Pinchart308bb522008-11-17 18:32:11 -03001061 if (uvc_trace_param & UVC_TRACE_IOCTL) {
1062 uvc_printk(KERN_DEBUG, "uvc_v4l2_ioctl(");
1063 v4l_printk_ioctl(cmd);
1064 printk(")\n");
1065 }
1066
Hans Verkuilf473bf72008-11-01 08:25:11 -03001067 return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001068}
1069
1070static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1071 size_t count, loff_t *ppos)
1072{
1073 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
Laurent Pinchart350d6402009-11-04 11:53:49 -03001074 return -EINVAL;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001075}
1076
1077/*
1078 * VMA operations.
1079 */
1080static void uvc_vm_open(struct vm_area_struct *vma)
1081{
1082 struct uvc_buffer *buffer = vma->vm_private_data;
1083 buffer->vma_use_count++;
1084}
1085
1086static void uvc_vm_close(struct vm_area_struct *vma)
1087{
1088 struct uvc_buffer *buffer = vma->vm_private_data;
1089 buffer->vma_use_count--;
1090}
1091
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001092static const struct vm_operations_struct uvc_vm_ops = {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001093 .open = uvc_vm_open,
1094 .close = uvc_vm_close,
1095};
1096
1097static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1098{
Joe Perchesabf84382010-07-12 17:50:03 -03001099 struct uvc_fh *handle = file->private_data;
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001100 struct uvc_streaming *stream = handle->stream;
1101 struct uvc_video_queue *queue = &stream->queue;
Andrew Morton6f80e1b2008-07-04 06:33:23 -03001102 struct uvc_buffer *uninitialized_var(buffer);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001103 struct page *page;
1104 unsigned long addr, start, size;
1105 unsigned int i;
1106 int ret = 0;
1107
1108 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
1109
1110 start = vma->vm_start;
1111 size = vma->vm_end - vma->vm_start;
1112
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001113 mutex_lock(&queue->mutex);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001114
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001115 for (i = 0; i < queue->count; ++i) {
1116 buffer = &queue->buffer[i];
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001117 if ((buffer->buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
1118 break;
1119 }
1120
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001121 if (i == queue->count || size != queue->buf_size) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001122 ret = -EINVAL;
1123 goto done;
1124 }
1125
1126 /*
1127 * VM_IO marks the area as being an mmaped region for I/O to a
1128 * device. It also prevents the region from being core dumped.
1129 */
1130 vma->vm_flags |= VM_IO;
1131
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001132 addr = (unsigned long)queue->mem + buffer->buf.m.offset;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001133 while (size > 0) {
1134 page = vmalloc_to_page((void *)addr);
1135 if ((ret = vm_insert_page(vma, start, page)) < 0)
1136 goto done;
1137
1138 start += PAGE_SIZE;
1139 addr += PAGE_SIZE;
1140 size -= PAGE_SIZE;
1141 }
1142
1143 vma->vm_ops = &uvc_vm_ops;
1144 vma->vm_private_data = buffer;
1145 uvc_vm_open(vma);
1146
1147done:
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001148 mutex_unlock(&queue->mutex);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001149 return ret;
1150}
1151
1152static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
1153{
Joe Perchesabf84382010-07-12 17:50:03 -03001154 struct uvc_fh *handle = file->private_data;
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001155 struct uvc_streaming *stream = handle->stream;
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001156
1157 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
1158
Laurent Pinchart35f02a62009-06-28 08:37:50 -03001159 return uvc_queue_poll(&stream->queue, file, wait);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001160}
1161
Hans Verkuilbec43662008-12-30 06:58:20 -03001162const struct v4l2_file_operations uvc_fops = {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001163 .owner = THIS_MODULE,
1164 .open = uvc_v4l2_open,
1165 .release = uvc_v4l2_release,
1166 .ioctl = uvc_v4l2_ioctl,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001167 .read = uvc_v4l2_read,
1168 .mmap = uvc_v4l2_mmap,
1169 .poll = uvc_v4l2_poll,
1170};
Hans Verkuilf87086e2008-07-18 00:50:58 -03001171