blob: ad7e64ff3add700d4f8bc6866bb8a53cf87113a1 [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 */
43static int uvc_v4l2_query_menu(struct uvc_video_device *video,
44 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
52 ctrl = uvc_find_control(video, query_menu->id, &mapping);
53 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
106static int uvc_v4l2_try_format(struct uvc_video_device *video,
107 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 Pinchartff924202008-12-28 22:32:29 -0300119 if (fmt->type != video->streaming->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. */
129 for (i = 0; i < video->streaming->nformats; ++i) {
130 format = &video->streaming->format[i];
131 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 */
194 if (video->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS)
195 probe->dwMaxVideoFrameSize =
196 video->streaming->ctrl.dwMaxVideoFrameSize;
197
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300198 /* Probe the device. */
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300199 if ((ret = uvc_probe_video(video, probe)) < 0)
200 goto done;
201
202 fmt->fmt.pix.width = frame->wWidth;
203 fmt->fmt.pix.height = frame->wHeight;
204 fmt->fmt.pix.field = V4L2_FIELD_NONE;
205 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
206 fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize;
207 fmt->fmt.pix.colorspace = format->colorspace;
208 fmt->fmt.pix.priv = 0;
209
210 if (uvc_format != NULL)
211 *uvc_format = format;
212 if (uvc_frame != NULL)
213 *uvc_frame = frame;
214
215done:
216 return ret;
217}
218
219static int uvc_v4l2_get_format(struct uvc_video_device *video,
220 struct v4l2_format *fmt)
221{
222 struct uvc_format *format = video->streaming->cur_format;
223 struct uvc_frame *frame = video->streaming->cur_frame;
224
Laurent Pinchartff924202008-12-28 22:32:29 -0300225 if (fmt->type != video->streaming->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300226 return -EINVAL;
227
228 if (format == NULL || frame == NULL)
229 return -EINVAL;
230
231 fmt->fmt.pix.pixelformat = format->fcc;
232 fmt->fmt.pix.width = frame->wWidth;
233 fmt->fmt.pix.height = frame->wHeight;
234 fmt->fmt.pix.field = V4L2_FIELD_NONE;
235 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
236 fmt->fmt.pix.sizeimage = video->streaming->ctrl.dwMaxVideoFrameSize;
237 fmt->fmt.pix.colorspace = format->colorspace;
238 fmt->fmt.pix.priv = 0;
239
240 return 0;
241}
242
243static int uvc_v4l2_set_format(struct uvc_video_device *video,
244 struct v4l2_format *fmt)
245{
246 struct uvc_streaming_control probe;
247 struct uvc_format *format;
248 struct uvc_frame *frame;
249 int ret;
250
Laurent Pinchartff924202008-12-28 22:32:29 -0300251 if (fmt->type != video->streaming->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300252 return -EINVAL;
253
254 if (uvc_queue_streaming(&video->queue))
255 return -EBUSY;
256
257 ret = uvc_v4l2_try_format(video, fmt, &probe, &format, &frame);
258 if (ret < 0)
259 return ret;
260
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300261 memcpy(&video->streaming->ctrl, &probe, sizeof probe);
262 video->streaming->cur_format = format;
263 video->streaming->cur_frame = frame;
264
265 return 0;
266}
267
268static int uvc_v4l2_get_streamparm(struct uvc_video_device *video,
269 struct v4l2_streamparm *parm)
270{
271 uint32_t numerator, denominator;
272
Laurent Pinchartff924202008-12-28 22:32:29 -0300273 if (parm->type != video->streaming->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300274 return -EINVAL;
275
276 numerator = video->streaming->ctrl.dwFrameInterval;
277 denominator = 10000000;
278 uvc_simplify_fraction(&numerator, &denominator, 8, 333);
279
280 memset(parm, 0, sizeof *parm);
Laurent Pinchartff924202008-12-28 22:32:29 -0300281 parm->type = video->streaming->type;
282
283 if (video->streaming->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
284 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
285 parm->parm.capture.capturemode = 0;
286 parm->parm.capture.timeperframe.numerator = numerator;
287 parm->parm.capture.timeperframe.denominator = denominator;
288 parm->parm.capture.extendedmode = 0;
289 parm->parm.capture.readbuffers = 0;
290 } else {
291 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
292 parm->parm.output.outputmode = 0;
293 parm->parm.output.timeperframe.numerator = numerator;
294 parm->parm.output.timeperframe.denominator = denominator;
295 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300296
297 return 0;
298}
299
300static int uvc_v4l2_set_streamparm(struct uvc_video_device *video,
301 struct v4l2_streamparm *parm)
302{
303 struct uvc_frame *frame = video->streaming->cur_frame;
304 struct uvc_streaming_control probe;
Laurent Pinchartff924202008-12-28 22:32:29 -0300305 struct v4l2_fract timeperframe;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300306 uint32_t interval;
307 int ret;
308
Laurent Pinchartff924202008-12-28 22:32:29 -0300309 if (parm->type != video->streaming->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300310 return -EINVAL;
311
312 if (uvc_queue_streaming(&video->queue))
313 return -EBUSY;
314
Laurent Pinchartff924202008-12-28 22:32:29 -0300315 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
316 timeperframe = parm->parm.capture.timeperframe;
317 else
318 timeperframe = parm->parm.output.timeperframe;
319
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300320 memcpy(&probe, &video->streaming->ctrl, sizeof probe);
Laurent Pinchartff924202008-12-28 22:32:29 -0300321 interval = uvc_fraction_to_interval(timeperframe.numerator,
322 timeperframe.denominator);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300323
324 uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
Laurent Pinchartff924202008-12-28 22:32:29 -0300325 timeperframe.numerator, timeperframe.denominator, interval);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300326 probe.dwFrameInterval = uvc_try_frame_interval(frame, interval);
327
328 /* Probe the device with the new settings. */
329 if ((ret = uvc_probe_video(video, &probe)) < 0)
330 return ret;
331
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300332 memcpy(&video->streaming->ctrl, &probe, sizeof probe);
333
334 /* Return the actual frame period. */
Laurent Pinchartff924202008-12-28 22:32:29 -0300335 timeperframe.numerator = probe.dwFrameInterval;
336 timeperframe.denominator = 10000000;
337 uvc_simplify_fraction(&timeperframe.numerator,
338 &timeperframe.denominator, 8, 333);
339
340 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
341 parm->parm.capture.timeperframe = timeperframe;
342 else
343 parm->parm.output.timeperframe = timeperframe;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300344
345 return 0;
346}
347
348/* ------------------------------------------------------------------------
349 * Privilege management
350 */
351
352/*
353 * Privilege management is the multiple-open implementation basis. The current
354 * implementation is completely transparent for the end-user and doesn't
355 * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls.
356 * Those ioctls enable finer control on the device (by making possible for a
357 * user to request exclusive access to a device), but are not mature yet.
358 * Switching to the V4L2 priority mechanism might be considered in the future
359 * if this situation changes.
360 *
361 * Each open instance of a UVC device can either be in a privileged or
362 * unprivileged state. Only a single instance can be in a privileged state at
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300363 * a given time. Trying to perform an operation that requires privileges will
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300364 * automatically acquire the required privileges if possible, or return -EBUSY
365 * otherwise. Privileges are dismissed when closing the instance.
366 *
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300367 * Operations that require privileges are:
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300368 *
369 * - VIDIOC_S_INPUT
370 * - VIDIOC_S_PARM
371 * - VIDIOC_S_FMT
372 * - VIDIOC_TRY_FMT
373 * - VIDIOC_REQBUFS
374 */
375static int uvc_acquire_privileges(struct uvc_fh *handle)
376{
377 int ret = 0;
378
379 /* 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. */
384 mutex_lock(&uvc_driver.open_mutex);
385 if (atomic_inc_return(&handle->device->active) != 1) {
386 atomic_dec(&handle->device->active);
387 ret = -EBUSY;
388 goto done;
389 }
390
391 handle->state = UVC_HANDLE_ACTIVE;
392
393done:
394 mutex_unlock(&uvc_driver.open_mutex);
395 return ret;
396}
397
398static void uvc_dismiss_privileges(struct uvc_fh *handle)
399{
400 if (handle->state == UVC_HANDLE_ACTIVE)
401 atomic_dec(&handle->device->active);
402
403 handle->state = UVC_HANDLE_PASSIVE;
404}
405
406static int uvc_has_privileges(struct uvc_fh *handle)
407{
408 return handle->state == UVC_HANDLE_ACTIVE;
409}
410
411/* ------------------------------------------------------------------------
412 * V4L2 file operations
413 */
414
Hans Verkuilbec43662008-12-30 06:58:20 -0300415static int uvc_v4l2_open(struct file *file)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300416{
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300417 struct uvc_video_device *video;
418 struct uvc_fh *handle;
419 int ret = 0;
420
421 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
422 mutex_lock(&uvc_driver.open_mutex);
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300423 video = video_drvdata(file);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300424
425 if (video->dev->state & UVC_DEV_DISCONNECTED) {
426 ret = -ENODEV;
427 goto done;
428 }
429
430 ret = usb_autopm_get_interface(video->dev->intf);
431 if (ret < 0)
432 goto done;
433
434 /* Create the device handle. */
435 handle = kzalloc(sizeof *handle, GFP_KERNEL);
436 if (handle == NULL) {
437 usb_autopm_put_interface(video->dev->intf);
438 ret = -ENOMEM;
439 goto done;
440 }
441
442 handle->device = video;
443 handle->state = UVC_HANDLE_PASSIVE;
444 file->private_data = handle;
445
446 kref_get(&video->dev->kref);
447
448done:
449 mutex_unlock(&uvc_driver.open_mutex);
450 return ret;
451}
452
Hans Verkuilbec43662008-12-30 06:58:20 -0300453static int uvc_v4l2_release(struct file *file)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300454{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300455 struct uvc_video_device *video = video_drvdata(file);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300456 struct uvc_fh *handle = (struct uvc_fh *)file->private_data;
457
458 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");
459
460 /* Only free resources if this is a privileged handle. */
461 if (uvc_has_privileges(handle)) {
462 uvc_video_enable(video, 0);
463
464 mutex_lock(&video->queue.mutex);
465 if (uvc_free_buffers(&video->queue) < 0)
466 uvc_printk(KERN_ERR, "uvc_v4l2_release: Unable to "
467 "free buffers.\n");
468 mutex_unlock(&video->queue.mutex);
469 }
470
471 /* Release the file handle. */
472 uvc_dismiss_privileges(handle);
473 kfree(handle);
474 file->private_data = NULL;
475
476 usb_autopm_put_interface(video->dev->intf);
477 kref_put(&video->dev->kref, uvc_delete);
478 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);
484 struct uvc_video_device *video = video_get_drvdata(vdev);
485 struct uvc_fh *handle = (struct uvc_fh *)file->private_data;
Hans Verkuil069b7472008-12-30 07:04:34 -0300486 long ret = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300487
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300488 switch (cmd) {
489 /* Query capabilities */
490 case VIDIOC_QUERYCAP:
491 {
492 struct v4l2_capability *cap = arg;
493
494 memset(cap, 0, sizeof *cap);
Laurent Pinchartd0ebf302009-01-08 14:05:11 -0300495 strlcpy(cap->driver, "uvcvideo", sizeof cap->driver);
496 strlcpy(cap->card, vdev->name, sizeof cap->card);
Thierry MERLEb12049a2009-01-27 16:53:23 -0300497 usb_make_path(video->dev->udev,
498 cap->bus_info, sizeof(cap->bus_info));
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300499 cap->version = DRIVER_VERSION_NUMBER;
Laurent Pinchartff924202008-12-28 22:32:29 -0300500 if (video->streaming->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
501 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
502 | V4L2_CAP_STREAMING;
503 else
504 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT
505 | V4L2_CAP_STREAMING;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300506 break;
507 }
508
509 /* Get, Set & Query control */
510 case VIDIOC_QUERYCTRL:
511 return uvc_query_v4l2_ctrl(video, arg);
512
513 case VIDIOC_G_CTRL:
514 {
515 struct v4l2_control *ctrl = arg;
516 struct v4l2_ext_control xctrl;
517
518 memset(&xctrl, 0, sizeof xctrl);
519 xctrl.id = ctrl->id;
520
521 uvc_ctrl_begin(video);
522 ret = uvc_ctrl_get(video, &xctrl);
523 uvc_ctrl_rollback(video);
524 if (ret >= 0)
525 ctrl->value = xctrl.value;
526 break;
527 }
528
529 case VIDIOC_S_CTRL:
530 {
531 struct v4l2_control *ctrl = arg;
532 struct v4l2_ext_control xctrl;
533
534 memset(&xctrl, 0, sizeof xctrl);
535 xctrl.id = ctrl->id;
536 xctrl.value = ctrl->value;
537
538 uvc_ctrl_begin(video);
539 ret = uvc_ctrl_set(video, &xctrl);
540 if (ret < 0) {
541 uvc_ctrl_rollback(video);
542 return ret;
543 }
544 ret = uvc_ctrl_commit(video);
545 break;
546 }
547
548 case VIDIOC_QUERYMENU:
549 return uvc_v4l2_query_menu(video, arg);
550
551 case VIDIOC_G_EXT_CTRLS:
552 {
553 struct v4l2_ext_controls *ctrls = arg;
554 struct v4l2_ext_control *ctrl = ctrls->controls;
555 unsigned int i;
556
557 uvc_ctrl_begin(video);
558 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
559 ret = uvc_ctrl_get(video, ctrl);
560 if (ret < 0) {
561 uvc_ctrl_rollback(video);
562 ctrls->error_idx = i;
563 return ret;
564 }
565 }
566 ctrls->error_idx = 0;
567 ret = uvc_ctrl_rollback(video);
568 break;
569 }
570
571 case VIDIOC_S_EXT_CTRLS:
572 case VIDIOC_TRY_EXT_CTRLS:
573 {
574 struct v4l2_ext_controls *ctrls = arg;
575 struct v4l2_ext_control *ctrl = ctrls->controls;
576 unsigned int i;
577
578 ret = uvc_ctrl_begin(video);
579 if (ret < 0)
580 return ret;
581
582 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
583 ret = uvc_ctrl_set(video, ctrl);
584 if (ret < 0) {
585 uvc_ctrl_rollback(video);
586 ctrls->error_idx = i;
587 return ret;
588 }
589 }
590
591 ctrls->error_idx = 0;
592
593 if (cmd == VIDIOC_S_EXT_CTRLS)
594 ret = uvc_ctrl_commit(video);
595 else
596 ret = uvc_ctrl_rollback(video);
597 break;
598 }
599
600 /* Get, Set & Enum input */
601 case VIDIOC_ENUMINPUT:
602 {
603 const struct uvc_entity *selector = video->selector;
604 struct v4l2_input *input = arg;
605 struct uvc_entity *iterm = NULL;
606 u32 index = input->index;
607 int pin = 0;
608
609 if (selector == NULL ||
610 (video->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
611 if (index != 0)
612 return -EINVAL;
613 iterm = list_first_entry(&video->iterms,
614 struct uvc_entity, chain);
615 pin = iterm->id;
616 } else if (pin < selector->selector.bNrInPins) {
617 pin = selector->selector.baSourceID[index];
618 list_for_each_entry(iterm, video->iterms.next, chain) {
619 if (iterm->id == pin)
620 break;
621 }
622 }
623
624 if (iterm == NULL || iterm->id != pin)
625 return -EINVAL;
626
627 memset(input, 0, sizeof *input);
628 input->index = index;
Laurent Pinchartd0ebf302009-01-08 14:05:11 -0300629 strlcpy(input->name, iterm->name, sizeof input->name);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300630 if (UVC_ENTITY_TYPE(iterm) == ITT_CAMERA)
631 input->type = V4L2_INPUT_TYPE_CAMERA;
632 break;
633 }
634
635 case VIDIOC_G_INPUT:
636 {
637 u8 input;
638
639 if (video->selector == NULL ||
640 (video->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
641 *(int *)arg = 0;
642 break;
643 }
644
645 ret = uvc_query_ctrl(video->dev, GET_CUR, video->selector->id,
646 video->dev->intfnum, SU_INPUT_SELECT_CONTROL,
647 &input, 1);
648 if (ret < 0)
649 return ret;
650
651 *(int *)arg = input - 1;
652 break;
653 }
654
655 case VIDIOC_S_INPUT:
656 {
Márton Németh9086c7b992009-04-15 09:01:58 -0300657 u32 input = *(u32 *)arg + 1;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300658
659 if ((ret = uvc_acquire_privileges(handle)) < 0)
660 return ret;
661
662 if (video->selector == NULL ||
663 (video->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
664 if (input != 1)
665 return -EINVAL;
666 break;
667 }
668
Márton Németh9086c7b992009-04-15 09:01:58 -0300669 if (input == 0 || input > video->selector->selector.bNrInPins)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300670 return -EINVAL;
671
672 return uvc_query_ctrl(video->dev, SET_CUR, video->selector->id,
673 video->dev->intfnum, SU_INPUT_SELECT_CONTROL,
674 &input, 1);
675 }
676
677 /* Try, Get, Set & Enum format */
678 case VIDIOC_ENUM_FMT:
679 {
680 struct v4l2_fmtdesc *fmt = arg;
681 struct uvc_format *format;
Márton Németh8bbd90c2009-03-27 11:13:57 -0300682 enum v4l2_buf_type type = fmt->type;
683 __u32 index = fmt->index;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300684
Laurent Pinchartff924202008-12-28 22:32:29 -0300685 if (fmt->type != video->streaming->type ||
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300686 fmt->index >= video->streaming->nformats)
687 return -EINVAL;
688
Márton Németh8bbd90c2009-03-27 11:13:57 -0300689 memset(fmt, 0, sizeof(*fmt));
690 fmt->index = index;
691 fmt->type = type;
692
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300693 format = &video->streaming->format[fmt->index];
694 fmt->flags = 0;
695 if (format->flags & UVC_FMT_FLAG_COMPRESSED)
696 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
Laurent Pinchartd0ebf302009-01-08 14:05:11 -0300697 strlcpy(fmt->description, format->name,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300698 sizeof fmt->description);
699 fmt->description[sizeof fmt->description - 1] = 0;
700 fmt->pixelformat = format->fcc;
701 break;
702 }
703
704 case VIDIOC_TRY_FMT:
705 {
706 struct uvc_streaming_control probe;
707
708 if ((ret = uvc_acquire_privileges(handle)) < 0)
709 return ret;
710
711 return uvc_v4l2_try_format(video, arg, &probe, NULL, NULL);
712 }
713
714 case VIDIOC_S_FMT:
715 if ((ret = uvc_acquire_privileges(handle)) < 0)
716 return ret;
717
718 return uvc_v4l2_set_format(video, arg);
719
720 case VIDIOC_G_FMT:
721 return uvc_v4l2_get_format(video, arg);
722
723 /* Frame size enumeration */
724 case VIDIOC_ENUM_FRAMESIZES:
725 {
726 struct v4l2_frmsizeenum *fsize = arg;
727 struct uvc_format *format = NULL;
728 struct uvc_frame *frame;
729 int i;
730
731 /* Look for the given pixel format */
732 for (i = 0; i < video->streaming->nformats; i++) {
733 if (video->streaming->format[i].fcc ==
734 fsize->pixel_format) {
735 format = &video->streaming->format[i];
736 break;
737 }
738 }
739 if (format == NULL)
740 return -EINVAL;
741
742 if (fsize->index >= format->nframes)
743 return -EINVAL;
744
745 frame = &format->frame[fsize->index];
746 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
747 fsize->discrete.width = frame->wWidth;
748 fsize->discrete.height = frame->wHeight;
749 break;
750 }
751
752 /* Frame interval enumeration */
753 case VIDIOC_ENUM_FRAMEINTERVALS:
754 {
755 struct v4l2_frmivalenum *fival = arg;
756 struct uvc_format *format = NULL;
757 struct uvc_frame *frame = NULL;
758 int i;
759
760 /* Look for the given pixel format and frame size */
761 for (i = 0; i < video->streaming->nformats; i++) {
762 if (video->streaming->format[i].fcc ==
763 fival->pixel_format) {
764 format = &video->streaming->format[i];
765 break;
766 }
767 }
768 if (format == NULL)
769 return -EINVAL;
770
771 for (i = 0; i < format->nframes; i++) {
772 if (format->frame[i].wWidth == fival->width &&
773 format->frame[i].wHeight == fival->height) {
774 frame = &format->frame[i];
775 break;
776 }
777 }
778 if (frame == NULL)
779 return -EINVAL;
780
781 if (frame->bFrameIntervalType) {
782 if (fival->index >= frame->bFrameIntervalType)
783 return -EINVAL;
784
785 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
786 fival->discrete.numerator =
787 frame->dwFrameInterval[fival->index];
788 fival->discrete.denominator = 10000000;
789 uvc_simplify_fraction(&fival->discrete.numerator,
790 &fival->discrete.denominator, 8, 333);
791 } else {
792 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
793 fival->stepwise.min.numerator =
794 frame->dwFrameInterval[0];
795 fival->stepwise.min.denominator = 10000000;
796 fival->stepwise.max.numerator =
797 frame->dwFrameInterval[1];
798 fival->stepwise.max.denominator = 10000000;
799 fival->stepwise.step.numerator =
800 frame->dwFrameInterval[2];
801 fival->stepwise.step.denominator = 10000000;
802 uvc_simplify_fraction(&fival->stepwise.min.numerator,
803 &fival->stepwise.min.denominator, 8, 333);
804 uvc_simplify_fraction(&fival->stepwise.max.numerator,
805 &fival->stepwise.max.denominator, 8, 333);
806 uvc_simplify_fraction(&fival->stepwise.step.numerator,
807 &fival->stepwise.step.denominator, 8, 333);
808 }
809 break;
810 }
811
812 /* Get & Set streaming parameters */
813 case VIDIOC_G_PARM:
814 return uvc_v4l2_get_streamparm(video, arg);
815
816 case VIDIOC_S_PARM:
817 if ((ret = uvc_acquire_privileges(handle)) < 0)
818 return ret;
819
820 return uvc_v4l2_set_streamparm(video, arg);
821
822 /* Cropping and scaling */
823 case VIDIOC_CROPCAP:
824 {
825 struct v4l2_cropcap *ccap = arg;
826 struct uvc_frame *frame = video->streaming->cur_frame;
827
Laurent Pinchartff924202008-12-28 22:32:29 -0300828 if (ccap->type != video->streaming->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300829 return -EINVAL;
830
831 ccap->bounds.left = 0;
832 ccap->bounds.top = 0;
833 ccap->bounds.width = frame->wWidth;
834 ccap->bounds.height = frame->wHeight;
835
836 ccap->defrect = ccap->bounds;
837
838 ccap->pixelaspect.numerator = 1;
839 ccap->pixelaspect.denominator = 1;
840 break;
841 }
842
843 case VIDIOC_G_CROP:
844 case VIDIOC_S_CROP:
845 return -EINVAL;
846
847 /* Buffers & streaming */
848 case VIDIOC_REQBUFS:
849 {
850 struct v4l2_requestbuffers *rb = arg;
851 unsigned int bufsize =
852 video->streaming->ctrl.dwMaxVideoFrameSize;
853
Laurent Pinchartff924202008-12-28 22:32:29 -0300854 if (rb->type != video->streaming->type ||
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300855 rb->memory != V4L2_MEMORY_MMAP)
856 return -EINVAL;
857
858 if ((ret = uvc_acquire_privileges(handle)) < 0)
859 return ret;
860
861 ret = uvc_alloc_buffers(&video->queue, rb->count, bufsize);
862 if (ret < 0)
863 return ret;
864
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300865 rb->count = ret;
866 ret = 0;
867 break;
868 }
869
870 case VIDIOC_QUERYBUF:
871 {
872 struct v4l2_buffer *buf = arg;
873
Laurent Pinchartff924202008-12-28 22:32:29 -0300874 if (buf->type != video->streaming->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300875 return -EINVAL;
876
877 if (!uvc_has_privileges(handle))
878 return -EBUSY;
879
880 return uvc_query_buffer(&video->queue, buf);
881 }
882
883 case VIDIOC_QBUF:
884 if (!uvc_has_privileges(handle))
885 return -EBUSY;
886
887 return uvc_queue_buffer(&video->queue, arg);
888
889 case VIDIOC_DQBUF:
890 if (!uvc_has_privileges(handle))
891 return -EBUSY;
892
893 return uvc_dequeue_buffer(&video->queue, arg,
894 file->f_flags & O_NONBLOCK);
895
896 case VIDIOC_STREAMON:
897 {
898 int *type = arg;
899
Laurent Pinchartff924202008-12-28 22:32:29 -0300900 if (*type != video->streaming->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300901 return -EINVAL;
902
903 if (!uvc_has_privileges(handle))
904 return -EBUSY;
905
906 if ((ret = uvc_video_enable(video, 1)) < 0)
907 return ret;
908 break;
909 }
910
911 case VIDIOC_STREAMOFF:
912 {
913 int *type = arg;
914
Laurent Pinchartff924202008-12-28 22:32:29 -0300915 if (*type != video->streaming->type)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300916 return -EINVAL;
917
918 if (!uvc_has_privileges(handle))
919 return -EBUSY;
920
921 return uvc_video_enable(video, 0);
922 }
923
924 /* Analog video standards make no sense for digital cameras. */
925 case VIDIOC_ENUMSTD:
926 case VIDIOC_QUERYSTD:
927 case VIDIOC_G_STD:
928 case VIDIOC_S_STD:
929
930 case VIDIOC_OVERLAY:
931
932 case VIDIOC_ENUMAUDIO:
933 case VIDIOC_ENUMAUDOUT:
934
935 case VIDIOC_ENUMOUTPUT:
936 uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd);
937 return -EINVAL;
938
939 /* Dynamic controls. */
940 case UVCIOC_CTRL_ADD:
941 {
942 struct uvc_xu_control_info *xinfo = arg;
943 struct uvc_control_info *info;
944
945 if (!capable(CAP_SYS_ADMIN))
946 return -EPERM;
947
Laurent Pinchart97683522008-12-16 06:46:32 -0300948 info = kzalloc(sizeof *info, GFP_KERNEL);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300949 if (info == NULL)
950 return -ENOMEM;
951
952 memcpy(info->entity, xinfo->entity, sizeof info->entity);
953 info->index = xinfo->index;
954 info->selector = xinfo->selector;
955 info->size = xinfo->size;
956 info->flags = xinfo->flags;
957
958 info->flags |= UVC_CONTROL_GET_MIN | UVC_CONTROL_GET_MAX |
959 UVC_CONTROL_GET_RES | UVC_CONTROL_GET_DEF;
960
961 ret = uvc_ctrl_add_info(info);
962 if (ret < 0)
963 kfree(info);
964 break;
965 }
966
967 case UVCIOC_CTRL_MAP:
968 {
969 struct uvc_xu_control_mapping *xmap = arg;
970 struct uvc_control_mapping *map;
971
972 if (!capable(CAP_SYS_ADMIN))
973 return -EPERM;
974
Laurent Pinchart97683522008-12-16 06:46:32 -0300975 map = kzalloc(sizeof *map, GFP_KERNEL);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300976 if (map == NULL)
977 return -ENOMEM;
978
979 map->id = xmap->id;
980 memcpy(map->name, xmap->name, sizeof map->name);
981 memcpy(map->entity, xmap->entity, sizeof map->entity);
982 map->selector = xmap->selector;
983 map->size = xmap->size;
984 map->offset = xmap->offset;
985 map->v4l2_type = xmap->v4l2_type;
986 map->data_type = xmap->data_type;
987
988 ret = uvc_ctrl_add_mapping(map);
989 if (ret < 0)
990 kfree(map);
991 break;
992 }
993
994 case UVCIOC_CTRL_GET:
995 return uvc_xu_ctrl_query(video, arg, 0);
996
997 case UVCIOC_CTRL_SET:
998 return uvc_xu_ctrl_query(video, arg, 1);
999
1000 default:
Mauro Carvalho Chehabb1f88402008-10-21 11:27:20 -03001001 if ((ret = v4l_compat_translate_ioctl(file, cmd, arg,
Hans Verkuilf473bf72008-11-01 08:25:11 -03001002 uvc_v4l2_do_ioctl)) == -ENOIOCTLCMD)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001003 uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n",
1004 cmd);
1005 return ret;
1006 }
1007
1008 return ret;
1009}
1010
Hans Verkuil069b7472008-12-30 07:04:34 -03001011static long uvc_v4l2_ioctl(struct file *file,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001012 unsigned int cmd, unsigned long arg)
1013{
Laurent Pinchart308bb522008-11-17 18:32:11 -03001014 if (uvc_trace_param & UVC_TRACE_IOCTL) {
1015 uvc_printk(KERN_DEBUG, "uvc_v4l2_ioctl(");
1016 v4l_printk_ioctl(cmd);
1017 printk(")\n");
1018 }
1019
Hans Verkuilf473bf72008-11-01 08:25:11 -03001020 return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001021}
1022
1023static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1024 size_t count, loff_t *ppos)
1025{
1026 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
1027 return -ENODEV;
1028}
1029
1030/*
1031 * VMA operations.
1032 */
1033static void uvc_vm_open(struct vm_area_struct *vma)
1034{
1035 struct uvc_buffer *buffer = vma->vm_private_data;
1036 buffer->vma_use_count++;
1037}
1038
1039static void uvc_vm_close(struct vm_area_struct *vma)
1040{
1041 struct uvc_buffer *buffer = vma->vm_private_data;
1042 buffer->vma_use_count--;
1043}
1044
1045static struct vm_operations_struct uvc_vm_ops = {
1046 .open = uvc_vm_open,
1047 .close = uvc_vm_close,
1048};
1049
1050static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1051{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001052 struct uvc_video_device *video = video_drvdata(file);
Andrew Morton6f80e1b2008-07-04 06:33:23 -03001053 struct uvc_buffer *uninitialized_var(buffer);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001054 struct page *page;
1055 unsigned long addr, start, size;
1056 unsigned int i;
1057 int ret = 0;
1058
1059 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
1060
1061 start = vma->vm_start;
1062 size = vma->vm_end - vma->vm_start;
1063
1064 mutex_lock(&video->queue.mutex);
1065
1066 for (i = 0; i < video->queue.count; ++i) {
1067 buffer = &video->queue.buffer[i];
1068 if ((buffer->buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
1069 break;
1070 }
1071
1072 if (i == video->queue.count || size != video->queue.buf_size) {
1073 ret = -EINVAL;
1074 goto done;
1075 }
1076
1077 /*
1078 * VM_IO marks the area as being an mmaped region for I/O to a
1079 * device. It also prevents the region from being core dumped.
1080 */
1081 vma->vm_flags |= VM_IO;
1082
1083 addr = (unsigned long)video->queue.mem + buffer->buf.m.offset;
1084 while (size > 0) {
1085 page = vmalloc_to_page((void *)addr);
1086 if ((ret = vm_insert_page(vma, start, page)) < 0)
1087 goto done;
1088
1089 start += PAGE_SIZE;
1090 addr += PAGE_SIZE;
1091 size -= PAGE_SIZE;
1092 }
1093
1094 vma->vm_ops = &uvc_vm_ops;
1095 vma->vm_private_data = buffer;
1096 uvc_vm_open(vma);
1097
1098done:
1099 mutex_unlock(&video->queue.mutex);
1100 return ret;
1101}
1102
1103static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
1104{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001105 struct uvc_video_device *video = video_drvdata(file);
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001106
1107 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
1108
1109 return uvc_queue_poll(&video->queue, file, wait);
1110}
1111
Hans Verkuilbec43662008-12-30 06:58:20 -03001112const struct v4l2_file_operations uvc_fops = {
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001113 .owner = THIS_MODULE,
1114 .open = uvc_v4l2_open,
1115 .release = uvc_v4l2_release,
1116 .ioctl = uvc_v4l2_ioctl,
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001117 .read = uvc_v4l2_read,
1118 .mmap = uvc_v4l2_mmap,
1119 .poll = uvc_v4l2_poll,
1120};
Hans Verkuilf87086e2008-07-18 00:50:58 -03001121