uvc_v4l2.c 27.0 KB
Newer Older
1 2 3
/*
 *      uvc_v4l2.c  --  USB Video Class driver - V4L2 API
 *
4 5
 *      Copyright (C) 2005-2010
 *          Laurent Pinchart (laurent.pinchart@ideasonboard.com)
6 7 8 9 10 11 12 13 14 15 16 17
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 */

#include <linux/kernel.h>
#include <linux/version.h>
#include <linux/list.h>
#include <linux/module.h>
18
#include <linux/slab.h>
19 20 21 22 23 24 25 26
#include <linux/usb.h>
#include <linux/videodev2.h>
#include <linux/vmalloc.h>
#include <linux/mm.h>
#include <linux/wait.h>
#include <asm/atomic.h>

#include <media/v4l2-common.h>
27
#include <media/v4l2-ioctl.h>
28 29 30

#include "uvcvideo.h"

31 32 33
/* ------------------------------------------------------------------------
 * UVC ioctls
 */
34 35
static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain,
	struct uvc_xu_control_mapping *xmap, int old)
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
{
	struct uvc_control_mapping *map;
	unsigned int size;
	int ret;

	map = kzalloc(sizeof *map, GFP_KERNEL);
	if (map == NULL)
		return -ENOMEM;

	map->id = xmap->id;
	memcpy(map->name, xmap->name, sizeof map->name);
	memcpy(map->entity, xmap->entity, sizeof map->entity);
	map->selector = xmap->selector;
	map->size = xmap->size;
	map->offset = xmap->offset;
	map->v4l2_type = xmap->v4l2_type;
	map->data_type = xmap->data_type;

	switch (xmap->v4l2_type) {
	case V4L2_CTRL_TYPE_INTEGER:
	case V4L2_CTRL_TYPE_BOOLEAN:
	case V4L2_CTRL_TYPE_BUTTON:
		break;

	case V4L2_CTRL_TYPE_MENU:
		if (old) {
62 63
			uvc_trace(UVC_TRACE_CONTROL, "V4L2_CTRL_TYPE_MENU not "
				  "supported for UVCIOC_CTRL_MAP_OLD.\n");
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
			ret = -EINVAL;
			goto done;
		}

		size = xmap->menu_count * sizeof(*map->menu_info);
		map->menu_info = kmalloc(size, GFP_KERNEL);
		if (map->menu_info == NULL) {
			ret = -ENOMEM;
			goto done;
		}

		if (copy_from_user(map->menu_info, xmap->menu_info, size)) {
			ret = -EFAULT;
			goto done;
		}

		map->menu_count = xmap->menu_count;
		break;

	default:
84 85
		uvc_trace(UVC_TRACE_CONTROL, "Unsupported V4L2 control type "
			  "%u.\n", xmap->v4l2_type);
86 87 88 89
		ret = -EINVAL;
		goto done;
	}

90
	ret = uvc_ctrl_add_mapping(chain, map);
91 92

done:
93 94
	kfree(map->menu_info);
	kfree(map);
95 96 97 98

	return ret;
}

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
/* ------------------------------------------------------------------------
 * V4L2 interface
 */

/*
 * Find the frame interval closest to the requested frame interval for the
 * given frame format and size. This should be done by the device as part of
 * the Video Probe and Commit negotiation, but some hardware don't implement
 * that feature.
 */
static __u32 uvc_try_frame_interval(struct uvc_frame *frame, __u32 interval)
{
	unsigned int i;

	if (frame->bFrameIntervalType) {
		__u32 best = -1, dist;

		for (i = 0; i < frame->bFrameIntervalType; ++i) {
			dist = interval > frame->dwFrameInterval[i]
			     ? interval - frame->dwFrameInterval[i]
			     : frame->dwFrameInterval[i] - interval;

			if (dist > best)
				break;

			best = dist;
		}

		interval = frame->dwFrameInterval[i-1];
	} else {
		const __u32 min = frame->dwFrameInterval[0];
		const __u32 max = frame->dwFrameInterval[1];
		const __u32 step = frame->dwFrameInterval[2];

		interval = min + (interval - min + step/2) / step * step;
		if (interval > max)
			interval = max;
	}

	return interval;
}

141
static int uvc_v4l2_try_format(struct uvc_streaming *stream,
142 143 144 145 146 147 148 149 150 151 152 153
	struct v4l2_format *fmt, struct uvc_streaming_control *probe,
	struct uvc_format **uvc_format, struct uvc_frame **uvc_frame)
{
	struct uvc_format *format = NULL;
	struct uvc_frame *frame = NULL;
	__u16 rw, rh;
	unsigned int d, maxd;
	unsigned int i;
	__u32 interval;
	int ret = 0;
	__u8 *fcc;

154
	if (fmt->type != stream->type)
155 156 157 158 159 160 161 162 163
		return -EINVAL;

	fcc = (__u8 *)&fmt->fmt.pix.pixelformat;
	uvc_trace(UVC_TRACE_FORMAT, "Trying format 0x%08x (%c%c%c%c): %ux%u.\n",
			fmt->fmt.pix.pixelformat,
			fcc[0], fcc[1], fcc[2], fcc[3],
			fmt->fmt.pix.width, fmt->fmt.pix.height);

	/* Check if the hardware supports the requested format. */
164 165
	for (i = 0; i < stream->nformats; ++i) {
		format = &stream->format[i];
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
		if (format->fcc == fmt->fmt.pix.pixelformat)
			break;
	}

	if (format == NULL || format->fcc != fmt->fmt.pix.pixelformat) {
		uvc_trace(UVC_TRACE_FORMAT, "Unsupported format 0x%08x.\n",
				fmt->fmt.pix.pixelformat);
		return -EINVAL;
	}

	/* Find the closest image size. The distance between image sizes is
	 * the size in pixels of the non-overlapping regions between the
	 * requested size and the frame-specified size.
	 */
	rw = fmt->fmt.pix.width;
	rh = fmt->fmt.pix.height;
	maxd = (unsigned int)-1;

	for (i = 0; i < format->nframes; ++i) {
		__u16 w = format->frame[i].wWidth;
		__u16 h = format->frame[i].wHeight;

		d = min(w, rw) * min(h, rh);
		d = w*h + rw*rh - 2*d;
		if (d < maxd) {
			maxd = d;
			frame = &format->frame[i];
		}

		if (maxd == 0)
			break;
	}

	if (frame == NULL) {
		uvc_trace(UVC_TRACE_FORMAT, "Unsupported size %ux%u.\n",
				fmt->fmt.pix.width, fmt->fmt.pix.height);
		return -EINVAL;
	}

	/* Use the default frame interval. */
	interval = frame->dwDefaultFrameInterval;
	uvc_trace(UVC_TRACE_FORMAT, "Using default frame interval %u.%u us "
		"(%u.%u fps).\n", interval/10, interval%10, 10000000/interval,
		(100000000/interval)%10);

	/* Set the format index, frame index and frame interval. */
	memset(probe, 0, sizeof *probe);
	probe->bmHint = 1;	/* dwFrameInterval */
	probe->bFormatIndex = format->index;
	probe->bFrameIndex = frame->bFrameIndex;
	probe->dwFrameInterval = uvc_try_frame_interval(frame, interval);
	/* Some webcams stall the probe control set request when the
	 * dwMaxVideoFrameSize field is set to zero. The UVC specification
	 * clearly states that the field is read-only from the host, so this
	 * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by
	 * the webcam to work around the problem.
	 *
	 * The workaround could probably be enabled for all webcams, so the
	 * quirk can be removed if needed. It's currently useful to detect
	 * webcam bugs and fix them before they hit the market (providing
	 * developers test their webcams with the Linux driver as well as with
	 * the Windows driver).
	 */
229
	mutex_lock(&stream->mutex);
230
	if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS)
231
		probe->dwMaxVideoFrameSize =
232
			stream->ctrl.dwMaxVideoFrameSize;
233

234
	/* Probe the device. */
235
	ret = uvc_probe_video(stream, probe);
236
	mutex_unlock(&stream->mutex);
237
	if (ret < 0)
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
		goto done;

	fmt->fmt.pix.width = frame->wWidth;
	fmt->fmt.pix.height = frame->wHeight;
	fmt->fmt.pix.field = V4L2_FIELD_NONE;
	fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
	fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize;
	fmt->fmt.pix.colorspace = format->colorspace;
	fmt->fmt.pix.priv = 0;

	if (uvc_format != NULL)
		*uvc_format = format;
	if (uvc_frame != NULL)
		*uvc_frame = frame;

done:
	return ret;
}

257
static int uvc_v4l2_get_format(struct uvc_streaming *stream,
258 259
	struct v4l2_format *fmt)
{
260 261 262
	struct uvc_format *format;
	struct uvc_frame *frame;
	int ret = 0;
263

264
	if (fmt->type != stream->type)
265 266
		return -EINVAL;

267 268 269 270 271 272 273 274
	mutex_lock(&stream->mutex);
	format = stream->cur_format;
	frame = stream->cur_frame;

	if (format == NULL || frame == NULL) {
		ret = -EINVAL;
		goto done;
	}
275 276 277 278 279 280

	fmt->fmt.pix.pixelformat = format->fcc;
	fmt->fmt.pix.width = frame->wWidth;
	fmt->fmt.pix.height = frame->wHeight;
	fmt->fmt.pix.field = V4L2_FIELD_NONE;
	fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
281
	fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize;
282 283 284
	fmt->fmt.pix.colorspace = format->colorspace;
	fmt->fmt.pix.priv = 0;

285 286 287
done:
	mutex_unlock(&stream->mutex);
	return ret;
288 289
}

290
static int uvc_v4l2_set_format(struct uvc_streaming *stream,
291 292 293 294 295 296 297
	struct v4l2_format *fmt)
{
	struct uvc_streaming_control probe;
	struct uvc_format *format;
	struct uvc_frame *frame;
	int ret;

298
	if (fmt->type != stream->type)
299 300
		return -EINVAL;

301
	ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame);
302 303 304
	if (ret < 0)
		return ret;

305 306 307 308 309 310 311
	mutex_lock(&stream->mutex);

	if (uvc_queue_allocated(&stream->queue)) {
		ret = -EBUSY;
		goto done;
	}

312 313 314
	memcpy(&stream->ctrl, &probe, sizeof probe);
	stream->cur_format = format;
	stream->cur_frame = frame;
315

316 317 318
done:
	mutex_unlock(&stream->mutex);
	return ret;
319 320
}

321
static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream,
322 323 324 325
		struct v4l2_streamparm *parm)
{
	uint32_t numerator, denominator;

326
	if (parm->type != stream->type)
327 328
		return -EINVAL;

329
	mutex_lock(&stream->mutex);
330
	numerator = stream->ctrl.dwFrameInterval;
331 332
	mutex_unlock(&stream->mutex);

333 334 335 336
	denominator = 10000000;
	uvc_simplify_fraction(&numerator, &denominator, 8, 333);

	memset(parm, 0, sizeof *parm);
337
	parm->type = stream->type;
338

339
	if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
340 341 342 343 344 345 346 347 348 349 350 351
		parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
		parm->parm.capture.capturemode = 0;
		parm->parm.capture.timeperframe.numerator = numerator;
		parm->parm.capture.timeperframe.denominator = denominator;
		parm->parm.capture.extendedmode = 0;
		parm->parm.capture.readbuffers = 0;
	} else {
		parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
		parm->parm.output.outputmode = 0;
		parm->parm.output.timeperframe.numerator = numerator;
		parm->parm.output.timeperframe.denominator = denominator;
	}
352 353 354 355

	return 0;
}

356
static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
357 358 359
		struct v4l2_streamparm *parm)
{
	struct uvc_streaming_control probe;
360
	struct v4l2_fract timeperframe;
361 362 363
	uint32_t interval;
	int ret;

364
	if (parm->type != stream->type)
365 366
		return -EINVAL;

367 368 369 370 371 372 373
	if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
		timeperframe = parm->parm.capture.timeperframe;
	else
		timeperframe = parm->parm.output.timeperframe;

	interval = uvc_fraction_to_interval(timeperframe.numerator,
		timeperframe.denominator);
374
	uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
375
		timeperframe.numerator, timeperframe.denominator, interval);
376 377 378 379 380 381 382 383 384 385 386

	mutex_lock(&stream->mutex);

	if (uvc_queue_streaming(&stream->queue)) {
		mutex_unlock(&stream->mutex);
		return -EBUSY;
	}

	memcpy(&probe, &stream->ctrl, sizeof probe);
	probe.dwFrameInterval =
		uvc_try_frame_interval(stream->cur_frame, interval);
387 388

	/* Probe the device with the new settings. */
389
	ret = uvc_probe_video(stream, &probe);
390 391
	if (ret < 0) {
		mutex_unlock(&stream->mutex);
392
		return ret;
393
	}
394

395
	memcpy(&stream->ctrl, &probe, sizeof probe);
396
	mutex_unlock(&stream->mutex);
397 398

	/* Return the actual frame period. */
399 400 401 402 403 404 405 406 407
	timeperframe.numerator = probe.dwFrameInterval;
	timeperframe.denominator = 10000000;
	uvc_simplify_fraction(&timeperframe.numerator,
		&timeperframe.denominator, 8, 333);

	if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
		parm->parm.capture.timeperframe = timeperframe;
	else
		parm->parm.output.timeperframe = timeperframe;
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426

	return 0;
}

/* ------------------------------------------------------------------------
 * Privilege management
 */

/*
 * Privilege management is the multiple-open implementation basis. The current
 * implementation is completely transparent for the end-user and doesn't
 * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls.
 * Those ioctls enable finer control on the device (by making possible for a
 * user to request exclusive access to a device), but are not mature yet.
 * Switching to the V4L2 priority mechanism might be considered in the future
 * if this situation changes.
 *
 * Each open instance of a UVC device can either be in a privileged or
 * unprivileged state. Only a single instance can be in a privileged state at
427
 * a given time. Trying to perform an operation that requires privileges will
428
 * automatically acquire the required privileges if possible, or return -EBUSY
429 430
 * otherwise. Privileges are dismissed when closing the instance or when
 * freeing the video buffers using VIDIOC_REQBUFS.
431
 *
432
 * Operations that require privileges are:
433 434 435 436 437 438 439 440 441 442 443 444 445
 *
 * - VIDIOC_S_INPUT
 * - VIDIOC_S_PARM
 * - VIDIOC_S_FMT
 * - VIDIOC_REQBUFS
 */
static int uvc_acquire_privileges(struct uvc_fh *handle)
{
	/* Always succeed if the handle is already privileged. */
	if (handle->state == UVC_HANDLE_ACTIVE)
		return 0;

	/* Check if the device already has a privileged handle. */
446 447
	if (atomic_inc_return(&handle->stream->active) != 1) {
		atomic_dec(&handle->stream->active);
448
		return -EBUSY;
449 450 451
	}

	handle->state = UVC_HANDLE_ACTIVE;
452
	return 0;
453 454 455 456 457
}

static void uvc_dismiss_privileges(struct uvc_fh *handle)
{
	if (handle->state == UVC_HANDLE_ACTIVE)
458
		atomic_dec(&handle->stream->active);
459 460 461 462 463 464 465 466 467 468 469 470 471

	handle->state = UVC_HANDLE_PASSIVE;
}

static int uvc_has_privileges(struct uvc_fh *handle)
{
	return handle->state == UVC_HANDLE_ACTIVE;
}

/* ------------------------------------------------------------------------
 * V4L2 file operations
 */

472
static int uvc_v4l2_open(struct file *file)
473
{
474
	struct uvc_streaming *stream;
475 476 477 478
	struct uvc_fh *handle;
	int ret = 0;

	uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
479
	stream = video_drvdata(file);
480

481 482
	if (stream->dev->state & UVC_DEV_DISCONNECTED)
		return -ENODEV;
483

484
	ret = usb_autopm_get_interface(stream->dev->intf);
485
	if (ret < 0)
486
		return ret;
487 488 489 490

	/* Create the device handle. */
	handle = kzalloc(sizeof *handle, GFP_KERNEL);
	if (handle == NULL) {
491
		usb_autopm_put_interface(stream->dev->intf);
492
		return -ENOMEM;
493 494
	}

495 496 497 498 499
	if (atomic_inc_return(&stream->dev->users) == 1) {
		ret = uvc_status_start(stream->dev);
		if (ret < 0) {
			usb_autopm_put_interface(stream->dev->intf);
			atomic_dec(&stream->dev->users);
500
			kfree(handle);
501
			return ret;
502 503 504
		}
	}

505
	handle->chain = stream->chain;
506
	handle->stream = stream;
507 508 509
	handle->state = UVC_HANDLE_PASSIVE;
	file->private_data = handle;

510
	return 0;
511 512
}

513
static int uvc_v4l2_release(struct file *file)
514
{
515
	struct uvc_fh *handle = file->private_data;
516
	struct uvc_streaming *stream = handle->stream;
517 518 519 520 521

	uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");

	/* Only free resources if this is a privileged handle. */
	if (uvc_has_privileges(handle)) {
522
		uvc_video_enable(stream, 0);
523

524
		if (uvc_free_buffers(&stream->queue) < 0)
525 526 527 528 529 530 531 532 533
			uvc_printk(KERN_ERR, "uvc_v4l2_release: Unable to "
					"free buffers.\n");
	}

	/* Release the file handle. */
	uvc_dismiss_privileges(handle);
	kfree(handle);
	file->private_data = NULL;

534 535
	if (atomic_dec_return(&stream->dev->users) == 0)
		uvc_status_stop(stream->dev);
536

537
	usb_autopm_put_interface(stream->dev->intf);
538 539 540
	return 0;
}

541 542 543 544 545 546 547 548 549 550 551 552 553 554
static void uvc_v4l2_ioctl_warn(void)
{
	static int warned;

	if (warned)
		return;

	uvc_printk(KERN_INFO, "Deprecated UVCIOC_CTRL_{ADD,MAP_OLD,GET,SET} "
		   "ioctls will be removed in 2.6.42.\n");
	uvc_printk(KERN_INFO, "See http://www.ideasonboard.org/uvc/upgrade/ "
		   "for upgrade instructions.\n");
	warned = 1;
}

555
static long uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
556 557
{
	struct video_device *vdev = video_devdata(file);
558
	struct uvc_fh *handle = file->private_data;
559
	struct uvc_video_chain *chain = handle->chain;
560
	struct uvc_streaming *stream = handle->stream;
561
	long ret = 0;
562 563 564 565 566 567 568 569

	switch (cmd) {
	/* Query capabilities */
	case VIDIOC_QUERYCAP:
	{
		struct v4l2_capability *cap = arg;

		memset(cap, 0, sizeof *cap);
570 571
		strlcpy(cap->driver, "uvcvideo", sizeof cap->driver);
		strlcpy(cap->card, vdev->name, sizeof cap->card);
572
		usb_make_path(stream->dev->udev,
573
			      cap->bus_info, sizeof(cap->bus_info));
574
		cap->version = DRIVER_VERSION_NUMBER;
575
		if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
576 577 578 579 580
			cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
					  | V4L2_CAP_STREAMING;
		else
			cap->capabilities = V4L2_CAP_VIDEO_OUTPUT
					  | V4L2_CAP_STREAMING;
581 582 583 584 585
		break;
	}

	/* Get, Set & Query control */
	case VIDIOC_QUERYCTRL:
586
		return uvc_query_v4l2_ctrl(chain, arg);
587 588 589 590 591 592 593 594 595

	case VIDIOC_G_CTRL:
	{
		struct v4l2_control *ctrl = arg;
		struct v4l2_ext_control xctrl;

		memset(&xctrl, 0, sizeof xctrl);
		xctrl.id = ctrl->id;

596 597
		ret = uvc_ctrl_begin(chain);
		if (ret < 0)
598 599
			return ret;

600 601
		ret = uvc_ctrl_get(chain, &xctrl);
		uvc_ctrl_rollback(chain);
602 603 604 605 606 607 608 609 610 611 612 613 614 615
		if (ret >= 0)
			ctrl->value = xctrl.value;
		break;
	}

	case VIDIOC_S_CTRL:
	{
		struct v4l2_control *ctrl = arg;
		struct v4l2_ext_control xctrl;

		memset(&xctrl, 0, sizeof xctrl);
		xctrl.id = ctrl->id;
		xctrl.value = ctrl->value;

616
		ret = uvc_ctrl_begin(chain);
617
		if (ret < 0)
618 619
			return ret;

620
		ret = uvc_ctrl_set(chain, &xctrl);
621
		if (ret < 0) {
622
			uvc_ctrl_rollback(chain);
623 624
			return ret;
		}
625
		ret = uvc_ctrl_commit(chain);
626 627
		if (ret == 0)
			ctrl->value = xctrl.value;
628 629 630 631
		break;
	}

	case VIDIOC_QUERYMENU:
632
		return uvc_query_v4l2_menu(chain, arg);
633 634 635 636 637 638 639

	case VIDIOC_G_EXT_CTRLS:
	{
		struct v4l2_ext_controls *ctrls = arg;
		struct v4l2_ext_control *ctrl = ctrls->controls;
		unsigned int i;

640 641
		ret = uvc_ctrl_begin(chain);
		if (ret < 0)
642 643
			return ret;

644
		for (i = 0; i < ctrls->count; ++ctrl, ++i) {
645
			ret = uvc_ctrl_get(chain, ctrl);
646
			if (ret < 0) {
647
				uvc_ctrl_rollback(chain);
648 649 650 651 652
				ctrls->error_idx = i;
				return ret;
			}
		}
		ctrls->error_idx = 0;
653
		ret = uvc_ctrl_rollback(chain);
654 655 656 657 658 659 660 661 662 663
		break;
	}

	case VIDIOC_S_EXT_CTRLS:
	case VIDIOC_TRY_EXT_CTRLS:
	{
		struct v4l2_ext_controls *ctrls = arg;
		struct v4l2_ext_control *ctrl = ctrls->controls;
		unsigned int i;

664
		ret = uvc_ctrl_begin(chain);
665 666 667 668
		if (ret < 0)
			return ret;

		for (i = 0; i < ctrls->count; ++ctrl, ++i) {
669
			ret = uvc_ctrl_set(chain, ctrl);
670
			if (ret < 0) {
671
				uvc_ctrl_rollback(chain);
672 673 674 675 676 677 678 679
				ctrls->error_idx = i;
				return ret;
			}
		}

		ctrls->error_idx = 0;

		if (cmd == VIDIOC_S_EXT_CTRLS)
680
			ret = uvc_ctrl_commit(chain);
681
		else
682
			ret = uvc_ctrl_rollback(chain);
683 684 685 686 687 688
		break;
	}

	/* Get, Set & Enum input */
	case VIDIOC_ENUMINPUT:
	{
689
		const struct uvc_entity *selector = chain->selector;
690 691 692 693 694 695
		struct v4l2_input *input = arg;
		struct uvc_entity *iterm = NULL;
		u32 index = input->index;
		int pin = 0;

		if (selector == NULL ||
696
		    (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
697 698
			if (index != 0)
				return -EINVAL;
699 700 701 702
			list_for_each_entry(iterm, &chain->entities, chain) {
				if (UVC_ENTITY_IS_ITERM(iterm))
					break;
			}
703
			pin = iterm->id;
704 705
		} else if (pin < selector->bNrInPins) {
			pin = selector->baSourceID[index];
706 707 708
			list_for_each_entry(iterm, &chain->entities, chain) {
				if (!UVC_ENTITY_IS_ITERM(iterm))
					continue;
709 710 711 712 713 714 715 716 717 718
				if (iterm->id == pin)
					break;
			}
		}

		if (iterm == NULL || iterm->id != pin)
			return -EINVAL;

		memset(input, 0, sizeof *input);
		input->index = index;
719
		strlcpy(input->name, iterm->name, sizeof input->name);
720
		if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
721 722 723 724 725 726 727 728
			input->type = V4L2_INPUT_TYPE_CAMERA;
		break;
	}

	case VIDIOC_G_INPUT:
	{
		u8 input;

729 730
		if (chain->selector == NULL ||
		    (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
731 732 733 734
			*(int *)arg = 0;
			break;
		}

735 736
		ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
			chain->selector->id, chain->dev->intfnum,
737
			UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
738 739 740 741 742 743 744 745 746
		if (ret < 0)
			return ret;

		*(int *)arg = input - 1;
		break;
	}

	case VIDIOC_S_INPUT:
	{
747
		u32 input = *(u32 *)arg + 1;
748 749 750 751

		if ((ret = uvc_acquire_privileges(handle)) < 0)
			return ret;

752 753
		if (chain->selector == NULL ||
		    (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
754 755 756 757 758
			if (input != 1)
				return -EINVAL;
			break;
		}

759
		if (input == 0 || input > chain->selector->bNrInPins)
760 761
			return -EINVAL;

762 763
		return uvc_query_ctrl(chain->dev, UVC_SET_CUR,
			chain->selector->id, chain->dev->intfnum,
764
			UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
765 766 767 768 769 770 771
	}

	/* Try, Get, Set & Enum format */
	case VIDIOC_ENUM_FMT:
	{
		struct v4l2_fmtdesc *fmt = arg;
		struct uvc_format *format;
772 773
		enum v4l2_buf_type type = fmt->type;
		__u32 index = fmt->index;
774

775 776
		if (fmt->type != stream->type ||
		    fmt->index >= stream->nformats)
777 778
			return -EINVAL;

779 780 781 782
		memset(fmt, 0, sizeof(*fmt));
		fmt->index = index;
		fmt->type = type;

783
		format = &stream->format[fmt->index];
784 785 786
		fmt->flags = 0;
		if (format->flags & UVC_FMT_FLAG_COMPRESSED)
			fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
787
		strlcpy(fmt->description, format->name,
788 789 790 791 792 793 794 795 796 797
			sizeof fmt->description);
		fmt->description[sizeof fmt->description - 1] = 0;
		fmt->pixelformat = format->fcc;
		break;
	}

	case VIDIOC_TRY_FMT:
	{
		struct uvc_streaming_control probe;

798
		return uvc_v4l2_try_format(stream, arg, &probe, NULL, NULL);
799 800 801 802 803 804
	}

	case VIDIOC_S_FMT:
		if ((ret = uvc_acquire_privileges(handle)) < 0)
			return ret;

805
		return uvc_v4l2_set_format(stream, arg);
806 807

	case VIDIOC_G_FMT:
808
		return uvc_v4l2_get_format(stream, arg);
809 810 811 812 813 814 815 816 817 818

	/* Frame size enumeration */
	case VIDIOC_ENUM_FRAMESIZES:
	{
		struct v4l2_frmsizeenum *fsize = arg;
		struct uvc_format *format = NULL;
		struct uvc_frame *frame;
		int i;

		/* Look for the given pixel format */
819 820
		for (i = 0; i < stream->nformats; i++) {
			if (stream->format[i].fcc ==
821
					fsize->pixel_format) {
822
				format = &stream->format[i];
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
				break;
			}
		}
		if (format == NULL)
			return -EINVAL;

		if (fsize->index >= format->nframes)
			return -EINVAL;

		frame = &format->frame[fsize->index];
		fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
		fsize->discrete.width = frame->wWidth;
		fsize->discrete.height = frame->wHeight;
		break;
	}

	/* Frame interval enumeration */
	case VIDIOC_ENUM_FRAMEINTERVALS:
	{
		struct v4l2_frmivalenum *fival = arg;
		struct uvc_format *format = NULL;
		struct uvc_frame *frame = NULL;
		int i;

		/* Look for the given pixel format and frame size */
848 849
		for (i = 0; i < stream->nformats; i++) {
			if (stream->format[i].fcc ==
850
					fival->pixel_format) {
851
				format = &stream->format[i];
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
				break;
			}
		}
		if (format == NULL)
			return -EINVAL;

		for (i = 0; i < format->nframes; i++) {
			if (format->frame[i].wWidth == fival->width &&
			    format->frame[i].wHeight == fival->height) {
				frame = &format->frame[i];
				break;
			}
		}
		if (frame == NULL)
			return -EINVAL;

		if (frame->bFrameIntervalType) {
			if (fival->index >= frame->bFrameIntervalType)
				return -EINVAL;

			fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
			fival->discrete.numerator =
				frame->dwFrameInterval[fival->index];
			fival->discrete.denominator = 10000000;
			uvc_simplify_fraction(&fival->discrete.numerator,
				&fival->discrete.denominator, 8, 333);
		} else {
			fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
			fival->stepwise.min.numerator =
				frame->dwFrameInterval[0];
			fival->stepwise.min.denominator = 10000000;
			fival->stepwise.max.numerator =
				frame->dwFrameInterval[1];
			fival->stepwise.max.denominator = 10000000;
			fival->stepwise.step.numerator =
				frame->dwFrameInterval[2];
			fival->stepwise.step.denominator = 10000000;
			uvc_simplify_fraction(&fival->stepwise.min.numerator,
				&fival->stepwise.min.denominator, 8, 333);
			uvc_simplify_fraction(&fival->stepwise.max.numerator,
				&fival->stepwise.max.denominator, 8, 333);
			uvc_simplify_fraction(&fival->stepwise.step.numerator,
				&fival->stepwise.step.denominator, 8, 333);
		}
		break;
	}

	/* Get & Set streaming parameters */
	case VIDIOC_G_PARM:
901
		return uvc_v4l2_get_streamparm(stream, arg);
902 903 904 905 906

	case VIDIOC_S_PARM:
		if ((ret = uvc_acquire_privileges(handle)) < 0)
			return ret;

907
		return uvc_v4l2_set_streamparm(stream, arg);
908 909 910 911 912 913

	/* Cropping and scaling */
	case VIDIOC_CROPCAP:
	{
		struct v4l2_cropcap *ccap = arg;

914
		if (ccap->type != stream->type)
915 916 917 918
			return -EINVAL;

		ccap->bounds.left = 0;
		ccap->bounds.top = 0;
919 920 921 922 923

		mutex_lock(&stream->mutex);
		ccap->bounds.width = stream->cur_frame->wWidth;
		ccap->bounds.height = stream->cur_frame->wHeight;
		mutex_unlock(&stream->mutex);
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940

		ccap->defrect = ccap->bounds;

		ccap->pixelaspect.numerator = 1;
		ccap->pixelaspect.denominator = 1;
		break;
	}

	case VIDIOC_G_CROP:
	case VIDIOC_S_CROP:
		return -EINVAL;

	/* Buffers & streaming */
	case VIDIOC_REQBUFS:
	{
		struct v4l2_requestbuffers *rb = arg;

941
		if (rb->type != stream->type ||
942 943 944 945 946 947
		    rb->memory != V4L2_MEMORY_MMAP)
			return -EINVAL;

		if ((ret = uvc_acquire_privileges(handle)) < 0)
			return ret;

948 949 950 951
		mutex_lock(&stream->mutex);
		ret = uvc_alloc_buffers(&stream->queue, rb->count,
					stream->ctrl.dwMaxVideoFrameSize);
		mutex_unlock(&stream->mutex);
952 953 954
		if (ret < 0)
			return ret;

955 956 957
		if (ret == 0)
			uvc_dismiss_privileges(handle);

958 959 960 961 962 963 964 965 966
		rb->count = ret;
		ret = 0;
		break;
	}

	case VIDIOC_QUERYBUF:
	{
		struct v4l2_buffer *buf = arg;

967
		if (buf->type != stream->type)
968 969 970 971 972
			return -EINVAL;

		if (!uvc_has_privileges(handle))
			return -EBUSY;

973
		return uvc_query_buffer(&stream->queue, buf);
974 975 976 977 978 979
	}

	case VIDIOC_QBUF:
		if (!uvc_has_privileges(handle))
			return -EBUSY;

980
		return uvc_queue_buffer(&stream->queue, arg);
981 982 983 984 985

	case VIDIOC_DQBUF:
		if (!uvc_has_privileges(handle))
			return -EBUSY;

986
		return uvc_dequeue_buffer(&stream->queue, arg,
987 988 989 990 991 992
			file->f_flags & O_NONBLOCK);

	case VIDIOC_STREAMON:
	{
		int *type = arg;

993
		if (*type != stream->type)
994 995 996 997 998
			return -EINVAL;

		if (!uvc_has_privileges(handle))
			return -EBUSY;

999
		mutex_lock(&stream->mutex);
1000
		ret = uvc_video_enable(stream, 1);
1001
		mutex_unlock(&stream->mutex);
1002
		if (ret < 0)
1003 1004 1005 1006 1007 1008 1009 1010
			return ret;
		break;
	}

	case VIDIOC_STREAMOFF:
	{
		int *type = arg;

1011
		if (*type != stream->type)
1012 1013 1014 1015 1016
			return -EINVAL;

		if (!uvc_has_privileges(handle))
			return -EBUSY;

1017
		return uvc_video_enable(stream, 0);
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
	}

	/* Analog video standards make no sense for digital cameras. */
	case VIDIOC_ENUMSTD:
	case VIDIOC_QUERYSTD:
	case VIDIOC_G_STD:
	case VIDIOC_S_STD:

	case VIDIOC_OVERLAY:

	case VIDIOC_ENUMAUDIO:
	case VIDIOC_ENUMAUDOUT:

	case VIDIOC_ENUMOUTPUT:
		uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd);
		return -EINVAL;

1035 1036 1037 1038
	/* Dynamic controls. UVCIOC_CTRL_ADD, UVCIOC_CTRL_MAP_OLD,
	 * UVCIOC_CTRL_GET and UVCIOC_CTRL_SET are deprecated and scheduled for
	 * removal in 2.6.42.
	 */
1039
	case UVCIOC_CTRL_ADD:
1040
		uvc_v4l2_ioctl_warn();
1041
		return -EEXIST;
1042

1043
	case UVCIOC_CTRL_MAP_OLD:
1044
		uvc_v4l2_ioctl_warn();
1045
	case UVCIOC_CTRL_MAP:
1046 1047
		return uvc_ioctl_ctrl_map(chain, arg,
					  cmd == UVCIOC_CTRL_MAP_OLD);
1048 1049 1050

	case UVCIOC_CTRL_GET:
	case UVCIOC_CTRL_SET:
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
	{
		struct uvc_xu_control *xctrl = arg;
		struct uvc_xu_control_query xqry = {
			.unit		= xctrl->unit,
			.selector	= xctrl->selector,
			.query		= cmd == UVCIOC_CTRL_GET
					? UVC_GET_CUR : UVC_SET_CUR,
			.size		= xctrl->size,
			.data		= xctrl->data,
		};

1062
		uvc_v4l2_ioctl_warn();
1063 1064 1065 1066 1067
		return uvc_xu_ctrl_query(chain, &xqry);
	}

	case UVCIOC_CTRL_QUERY:
		return uvc_xu_ctrl_query(chain, arg);
1068 1069

	default:
1070 1071
		uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n", cmd);
		return -EINVAL;
1072 1073 1074 1075 1076
	}

	return ret;
}

1077
static long uvc_v4l2_ioctl(struct file *file,
1078 1079
		     unsigned int cmd, unsigned long arg)
{
1080 1081 1082 1083 1084 1085
	if (uvc_trace_param & UVC_TRACE_IOCTL) {
		uvc_printk(KERN_DEBUG, "uvc_v4l2_ioctl(");
		v4l_printk_ioctl(cmd);
		printk(")\n");
	}

1086
	return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl);
1087 1088 1089 1090 1091 1092
}

static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
		    size_t count, loff_t *ppos)
{
	uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
1093
	return -EINVAL;
1094 1095 1096 1097
}

static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
{
1098
	struct uvc_fh *handle = file->private_data;
1099
	struct uvc_streaming *stream = handle->stream;
1100 1101 1102

	uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");

1103
	return uvc_queue_mmap(&stream->queue, vma);
1104 1105 1106 1107
}

static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
{
1108
	struct uvc_fh *handle = file->private_data;
1109
	struct uvc_streaming *stream = handle->stream;
1110 1111 1112

	uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");

1113
	return uvc_queue_poll(&stream->queue, file, wait);
1114 1115
}

1116
const struct v4l2_file_operations uvc_fops = {
1117 1118 1119
	.owner		= THIS_MODULE,
	.open		= uvc_v4l2_open,
	.release	= uvc_v4l2_release,
1120
	.unlocked_ioctl	= uvc_v4l2_ioctl,
1121 1122 1123 1124
	.read		= uvc_v4l2_read,
	.mmap		= uvc_v4l2_mmap,
	.poll		= uvc_v4l2_poll,
};
1125