uvc_v4l2.c 36.9 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
 *
 *      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.
 *
 */

14
#include <linux/compat.h>
15 16 17
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/module.h>
18
#include <linux/slab.h>
19 20 21 22 23
#include <linux/usb.h>
#include <linux/videodev2.h>
#include <linux/vmalloc.h>
#include <linux/mm.h>
#include <linux/wait.h>
A
Arun Sharma 已提交
24
#include <linux/atomic.h>
25 26

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

#include "uvcvideo.h"

33 34 35
/* ------------------------------------------------------------------------
 * UVC ioctls
 */
36
static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain,
37
	struct uvc_xu_control_mapping *xmap)
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
{
	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:
63 64 65 66 67 68
		/* Prevent excessive memory consumption, as well as integer
		 * overflows.
		 */
		if (xmap->menu_count == 0 ||
		    xmap->menu_count > UVC_MAX_CONTROL_MENU_ENTRIES) {
			ret = -EINVAL;
69
			goto free_map;
70 71
		}

72
		size = xmap->menu_count * sizeof(*map->menu_info);
73 74 75
		map->menu_info = memdup_user(xmap->menu_info, size);
		if (IS_ERR(map->menu_info)) {
			ret = PTR_ERR(map->menu_info);
76
			goto free_map;
77 78 79 80 81 82
		}

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

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

89
	ret = uvc_ctrl_add_mapping(chain, map);
90

91
	kfree(map->menu_info);
92
free_map:
93
	kfree(map);
94 95 96 97

	return ret;
}

98 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
/* ------------------------------------------------------------------------
 * 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;
}

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
static __u32 uvc_v4l2_get_bytesperline(const struct uvc_format *format,
	const struct uvc_frame *frame)
{
	switch (format->fcc) {
	case V4L2_PIX_FMT_NV12:
	case V4L2_PIX_FMT_YVU420:
	case V4L2_PIX_FMT_YUV420:
	case V4L2_PIX_FMT_M420:
		return frame->wWidth;

	default:
		return format->bpp * frame->wWidth / 8;
	}
}

155
static int uvc_v4l2_try_format(struct uvc_streaming *stream,
156 157 158 159 160 161 162 163 164 165 166 167
	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;

168
	if (fmt->type != stream->type)
169 170 171 172 173 174 175 176
		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);

177 178 179
	/* Check if the hardware supports the requested format, use the default
	 * format otherwise.
	 */
180 181
	for (i = 0; i < stream->nformats; ++i) {
		format = &stream->format[i];
182 183 184 185
		if (format->fcc == fmt->fmt.pix.pixelformat)
			break;
	}

186 187 188
	if (i == stream->nformats) {
		format = stream->def_format;
		fmt->fmt.pix.pixelformat = format->fcc;
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 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
	}

	/* 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).
	 */
244
	mutex_lock(&stream->mutex);
245
	if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS)
246
		probe->dwMaxVideoFrameSize =
247
			stream->ctrl.dwMaxVideoFrameSize;
248

249
	/* Probe the device. */
250
	ret = uvc_probe_video(stream, probe);
251
	mutex_unlock(&stream->mutex);
252
	if (ret < 0)
253 254 255 256 257
		goto done;

	fmt->fmt.pix.width = frame->wWidth;
	fmt->fmt.pix.height = frame->wHeight;
	fmt->fmt.pix.field = V4L2_FIELD_NONE;
258
	fmt->fmt.pix.bytesperline = uvc_v4l2_get_bytesperline(format, frame);
259 260 261 262 263 264 265 266 267 268 269 270 271
	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;
}

272
static int uvc_v4l2_get_format(struct uvc_streaming *stream,
273 274
	struct v4l2_format *fmt)
{
275 276 277
	struct uvc_format *format;
	struct uvc_frame *frame;
	int ret = 0;
278

279
	if (fmt->type != stream->type)
280 281
		return -EINVAL;

282 283 284 285 286 287 288 289
	mutex_lock(&stream->mutex);
	format = stream->cur_format;
	frame = stream->cur_frame;

	if (format == NULL || frame == NULL) {
		ret = -EINVAL;
		goto done;
	}
290 291 292 293 294

	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;
295
	fmt->fmt.pix.bytesperline = uvc_v4l2_get_bytesperline(format, frame);
296
	fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize;
297 298 299
	fmt->fmt.pix.colorspace = format->colorspace;
	fmt->fmt.pix.priv = 0;

300 301 302
done:
	mutex_unlock(&stream->mutex);
	return ret;
303 304
}

305
static int uvc_v4l2_set_format(struct uvc_streaming *stream,
306 307 308 309 310 311 312
	struct v4l2_format *fmt)
{
	struct uvc_streaming_control probe;
	struct uvc_format *format;
	struct uvc_frame *frame;
	int ret;

313
	if (fmt->type != stream->type)
314 315
		return -EINVAL;

316
	ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame);
317 318 319
	if (ret < 0)
		return ret;

320 321 322 323 324 325 326
	mutex_lock(&stream->mutex);

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

327
	stream->ctrl = probe;
328 329
	stream->cur_format = format;
	stream->cur_frame = frame;
330

331 332 333
done:
	mutex_unlock(&stream->mutex);
	return ret;
334 335
}

336
static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream,
337 338 339 340
		struct v4l2_streamparm *parm)
{
	uint32_t numerator, denominator;

341
	if (parm->type != stream->type)
342 343
		return -EINVAL;

344
	mutex_lock(&stream->mutex);
345
	numerator = stream->ctrl.dwFrameInterval;
346 347
	mutex_unlock(&stream->mutex);

348 349 350 351
	denominator = 10000000;
	uvc_simplify_fraction(&numerator, &denominator, 8, 333);

	memset(parm, 0, sizeof *parm);
352
	parm->type = stream->type;
353

354
	if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
355 356 357 358 359 360 361 362 363 364 365 366
		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;
	}
367 368 369 370

	return 0;
}

371
static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
372 373 374
		struct v4l2_streamparm *parm)
{
	struct uvc_streaming_control probe;
375
	struct v4l2_fract timeperframe;
376 377 378
	uint32_t interval;
	int ret;

379
	if (parm->type != stream->type)
380 381
		return -EINVAL;

382 383 384 385 386 387 388
	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);
389
	uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
390
		timeperframe.numerator, timeperframe.denominator, interval);
391 392 393 394 395 396 397 398

	mutex_lock(&stream->mutex);

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

399
	probe = stream->ctrl;
400 401
	probe.dwFrameInterval =
		uvc_try_frame_interval(stream->cur_frame, interval);
402 403

	/* Probe the device with the new settings. */
404
	ret = uvc_probe_video(stream, &probe);
405 406
	if (ret < 0) {
		mutex_unlock(&stream->mutex);
407
		return ret;
408
	}
409

410
	stream->ctrl = probe;
411
	mutex_unlock(&stream->mutex);
412 413

	/* Return the actual frame period. */
414 415 416 417 418 419 420 421 422
	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;
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441

	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
442
 * a given time. Trying to perform an operation that requires privileges will
443
 * automatically acquire the required privileges if possible, or return -EBUSY
444 445
 * otherwise. Privileges are dismissed when closing the instance or when
 * freeing the video buffers using VIDIOC_REQBUFS.
446
 *
447
 * Operations that require privileges are:
448 449 450 451 452 453 454 455 456 457 458 459 460
 *
 * - 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. */
461 462
	if (atomic_inc_return(&handle->stream->active) != 1) {
		atomic_dec(&handle->stream->active);
463
		return -EBUSY;
464 465 466
	}

	handle->state = UVC_HANDLE_ACTIVE;
467
	return 0;
468 469 470 471 472
}

static void uvc_dismiss_privileges(struct uvc_fh *handle)
{
	if (handle->state == UVC_HANDLE_ACTIVE)
473
		atomic_dec(&handle->stream->active);
474 475 476 477 478 479 480 481 482 483 484 485 486

	handle->state = UVC_HANDLE_PASSIVE;
}

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

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

487
static int uvc_v4l2_open(struct file *file)
488
{
489
	struct uvc_streaming *stream;
490 491 492 493
	struct uvc_fh *handle;
	int ret = 0;

	uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
494
	stream = video_drvdata(file);
495

496
	ret = usb_autopm_get_interface(stream->dev->intf);
497
	if (ret < 0)
498
		return ret;
499 500 501 502

	/* Create the device handle. */
	handle = kzalloc(sizeof *handle, GFP_KERNEL);
	if (handle == NULL) {
503
		usb_autopm_put_interface(stream->dev->intf);
504
		return -ENOMEM;
505 506
	}

507 508 509
	mutex_lock(&stream->dev->lock);
	if (stream->dev->users == 0) {
		ret = uvc_status_start(stream->dev, GFP_KERNEL);
510
		if (ret < 0) {
511
			mutex_unlock(&stream->dev->lock);
512
			usb_autopm_put_interface(stream->dev->intf);
513
			kfree(handle);
514
			return ret;
515 516 517
		}
	}

518 519 520
	stream->dev->users++;
	mutex_unlock(&stream->dev->lock);

H
Hans Verkuil 已提交
521
	v4l2_fh_init(&handle->vfh, &stream->vdev);
522
	v4l2_fh_add(&handle->vfh);
523
	handle->chain = stream->chain;
524
	handle->stream = stream;
525 526 527
	handle->state = UVC_HANDLE_PASSIVE;
	file->private_data = handle;

528
	return 0;
529 530
}

531
static int uvc_v4l2_release(struct file *file)
532
{
533
	struct uvc_fh *handle = file->private_data;
534
	struct uvc_streaming *stream = handle->stream;
535 536 537 538

	uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");

	/* Only free resources if this is a privileged handle. */
539 540
	if (uvc_has_privileges(handle))
		uvc_queue_release(&stream->queue);
541 542 543

	/* Release the file handle. */
	uvc_dismiss_privileges(handle);
544 545
	v4l2_fh_del(&handle->vfh);
	v4l2_fh_exit(&handle->vfh);
546 547 548
	kfree(handle);
	file->private_data = NULL;

549 550
	mutex_lock(&stream->dev->lock);
	if (--stream->dev->users == 0)
551
		uvc_status_stop(stream->dev);
552
	mutex_unlock(&stream->dev->lock);
553

554
	usb_autopm_put_interface(stream->dev->intf);
555 556 557
	return 0;
}

558 559
static int uvc_ioctl_querycap(struct file *file, void *fh,
			      struct v4l2_capability *cap)
560 561
{
	struct video_device *vdev = video_devdata(file);
562
	struct uvc_fh *handle = file->private_data;
563
	struct uvc_video_chain *chain = handle->chain;
564
	struct uvc_streaming *stream = handle->stream;
565

566 567 568 569 570 571 572 573 574
	strlcpy(cap->driver, "uvcvideo", sizeof(cap->driver));
	strlcpy(cap->card, vdev->name, sizeof(cap->card));
	usb_make_path(stream->dev->udev, cap->bus_info, sizeof(cap->bus_info));
	cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
			  | chain->caps;
	if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
		cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
	else
		cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
575

576 577
	return 0;
}
578

579 580 581 582 583 584
static int uvc_ioctl_enum_fmt(struct uvc_streaming *stream,
			      struct v4l2_fmtdesc *fmt)
{
	struct uvc_format *format;
	enum v4l2_buf_type type = fmt->type;
	__u32 index = fmt->index;
585

586 587
	if (fmt->type != stream->type || fmt->index >= stream->nformats)
		return -EINVAL;
588

589 590 591 592 593 594 595 596 597 598 599 600 601
	memset(fmt, 0, sizeof(*fmt));
	fmt->index = index;
	fmt->type = type;

	format = &stream->format[fmt->index];
	fmt->flags = 0;
	if (format->flags & UVC_FMT_FLAG_COMPRESSED)
		fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
	strlcpy(fmt->description, format->name, sizeof(fmt->description));
	fmt->description[sizeof(fmt->description) - 1] = 0;
	fmt->pixelformat = format->fcc;
	return 0;
}
602

603 604 605 606 607
static int uvc_ioctl_enum_fmt_vid_cap(struct file *file, void *fh,
				      struct v4l2_fmtdesc *fmt)
{
	struct uvc_fh *handle = fh;
	struct uvc_streaming *stream = handle->stream;
608

609 610
	return uvc_ioctl_enum_fmt(stream, fmt);
}
611

612 613 614 615 616
static int uvc_ioctl_enum_fmt_vid_out(struct file *file, void *fh,
				      struct v4l2_fmtdesc *fmt)
{
	struct uvc_fh *handle = fh;
	struct uvc_streaming *stream = handle->stream;
617

618 619
	return uvc_ioctl_enum_fmt(stream, fmt);
}
620

621 622 623 624 625
static int uvc_ioctl_g_fmt_vid_cap(struct file *file, void *fh,
				   struct v4l2_format *fmt)
{
	struct uvc_fh *handle = fh;
	struct uvc_streaming *stream = handle->stream;
626

627 628
	return uvc_v4l2_get_format(stream, fmt);
}
629

630 631 632 633 634
static int uvc_ioctl_g_fmt_vid_out(struct file *file, void *fh,
				   struct v4l2_format *fmt)
{
	struct uvc_fh *handle = fh;
	struct uvc_streaming *stream = handle->stream;
635

636 637
	return uvc_v4l2_get_format(stream, fmt);
}
638

639 640 641 642 643 644
static int uvc_ioctl_s_fmt_vid_cap(struct file *file, void *fh,
				   struct v4l2_format *fmt)
{
	struct uvc_fh *handle = fh;
	struct uvc_streaming *stream = handle->stream;
	int ret;
645

646 647 648
	ret = uvc_acquire_privileges(handle);
	if (ret < 0)
		return ret;
649

650 651
	return uvc_v4l2_set_format(stream, fmt);
}
652

653 654 655 656 657 658
static int uvc_ioctl_s_fmt_vid_out(struct file *file, void *fh,
				   struct v4l2_format *fmt)
{
	struct uvc_fh *handle = fh;
	struct uvc_streaming *stream = handle->stream;
	int ret;
659

660 661 662
	ret = uvc_acquire_privileges(handle);
	if (ret < 0)
		return ret;
663

664 665
	return uvc_v4l2_set_format(stream, fmt);
}
666

667 668 669 670 671 672
static int uvc_ioctl_try_fmt_vid_cap(struct file *file, void *fh,
				     struct v4l2_format *fmt)
{
	struct uvc_fh *handle = fh;
	struct uvc_streaming *stream = handle->stream;
	struct uvc_streaming_control probe;
673

674 675
	return uvc_v4l2_try_format(stream, fmt, &probe, NULL, NULL);
}
676

677 678 679 680 681 682
static int uvc_ioctl_try_fmt_vid_out(struct file *file, void *fh,
				     struct v4l2_format *fmt)
{
	struct uvc_fh *handle = fh;
	struct uvc_streaming *stream = handle->stream;
	struct uvc_streaming_control probe;
683

684 685
	return uvc_v4l2_try_format(stream, fmt, &probe, NULL, NULL);
}
686

687 688 689 690 691 692
static int uvc_ioctl_reqbufs(struct file *file, void *fh,
			     struct v4l2_requestbuffers *rb)
{
	struct uvc_fh *handle = fh;
	struct uvc_streaming *stream = handle->stream;
	int ret;
693

694 695 696
	ret = uvc_acquire_privileges(handle);
	if (ret < 0)
		return ret;
697

698
	mutex_lock(&stream->mutex);
699
	ret = uvc_request_buffers(&stream->queue, rb);
700 701 702
	mutex_unlock(&stream->mutex);
	if (ret < 0)
		return ret;
703

704 705
	if (ret == 0)
		uvc_dismiss_privileges(handle);
706

707 708
	return 0;
}
709

710 711 712 713 714
static int uvc_ioctl_querybuf(struct file *file, void *fh,
			      struct v4l2_buffer *buf)
{
	struct uvc_fh *handle = fh;
	struct uvc_streaming *stream = handle->stream;
715

716 717
	if (!uvc_has_privileges(handle))
		return -EBUSY;
718

719 720
	return uvc_query_buffer(&stream->queue, buf);
}
721

722 723 724 725
static int uvc_ioctl_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
{
	struct uvc_fh *handle = fh;
	struct uvc_streaming *stream = handle->stream;
726

727 728
	if (!uvc_has_privileges(handle))
		return -EBUSY;
729

730 731
	return uvc_queue_buffer(&stream->queue, buf);
}
732

733 734 735 736 737 738 739 740 741 742 743 744
static int uvc_ioctl_expbuf(struct file *file, void *fh,
			    struct v4l2_exportbuffer *exp)
{
	struct uvc_fh *handle = fh;
	struct uvc_streaming *stream = handle->stream;

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

	return uvc_export_buffer(&stream->queue, exp);
}

745 746 747 748
static int uvc_ioctl_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
{
	struct uvc_fh *handle = fh;
	struct uvc_streaming *stream = handle->stream;
749

750 751
	if (!uvc_has_privileges(handle))
		return -EBUSY;
752

753 754 755
	return uvc_dequeue_buffer(&stream->queue, buf,
				  file->f_flags & O_NONBLOCK);
}
756

757 758 759 760 761 762
static int uvc_ioctl_create_bufs(struct file *file, void *fh,
				  struct v4l2_create_buffers *cb)
{
	struct uvc_fh *handle = fh;
	struct uvc_streaming *stream = handle->stream;
	int ret;
763

764 765 766
	ret = uvc_acquire_privileges(handle);
	if (ret < 0)
		return ret;
767

768 769
	return uvc_create_buffers(&stream->queue, cb);
}
770

771 772 773 774 775 776
static int uvc_ioctl_streamon(struct file *file, void *fh,
			      enum v4l2_buf_type type)
{
	struct uvc_fh *handle = fh;
	struct uvc_streaming *stream = handle->stream;
	int ret;
777

778 779 780 781
	if (!uvc_has_privileges(handle))
		return -EBUSY;

	mutex_lock(&stream->mutex);
782
	ret = uvc_queue_streamon(&stream->queue, type);
783
	mutex_unlock(&stream->mutex);
784

785 786
	return ret;
}
787

788 789 790 791 792
static int uvc_ioctl_streamoff(struct file *file, void *fh,
			       enum v4l2_buf_type type)
{
	struct uvc_fh *handle = fh;
	struct uvc_streaming *stream = handle->stream;
793

794 795
	if (!uvc_has_privileges(handle))
		return -EBUSY;
796

797
	mutex_lock(&stream->mutex);
798
	uvc_queue_streamoff(&stream->queue, type);
799
	mutex_unlock(&stream->mutex);
800

801
	return 0;
802
}
803

804 805 806 807 808 809 810 811 812 813 814 815 816
static int uvc_ioctl_enum_input(struct file *file, void *fh,
				struct v4l2_input *input)
{
	struct uvc_fh *handle = fh;
	struct uvc_video_chain *chain = handle->chain;
	const struct uvc_entity *selector = chain->selector;
	struct uvc_entity *iterm = NULL;
	u32 index = input->index;
	int pin = 0;

	if (selector == NULL ||
	    (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
		if (index != 0)
817
			return -EINVAL;
818 819
		list_for_each_entry(iterm, &chain->entities, chain) {
			if (UVC_ENTITY_IS_ITERM(iterm))
820 821
				break;
		}
822 823 824 825 826 827 828 829
		pin = iterm->id;
	} else if (index < selector->bNrInPins) {
		pin = selector->baSourceID[index];
		list_for_each_entry(iterm, &chain->entities, chain) {
			if (!UVC_ENTITY_IS_ITERM(iterm))
				continue;
			if (iterm->id == pin)
				break;
830 831 832
		}
	}

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

836 837 838 839 840
	memset(input, 0, sizeof(*input));
	input->index = index;
	strlcpy(input->name, iterm->name, sizeof(input->name));
	if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
		input->type = V4L2_INPUT_TYPE_CAMERA;
841

842 843
	return 0;
}
844

845 846 847 848 849 850
static int uvc_ioctl_g_input(struct file *file, void *fh, unsigned int *input)
{
	struct uvc_fh *handle = fh;
	struct uvc_video_chain *chain = handle->chain;
	int ret;
	u8 i;
851

852 853 854 855 856
	if (chain->selector == NULL ||
	    (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
		*input = 0;
		return 0;
	}
857

858 859 860 861 862
	ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, chain->selector->id,
			     chain->dev->intfnum,  UVC_SU_INPUT_SELECT_CONTROL,
			     &i, 1);
	if (ret < 0)
		return ret;
863

864 865 866
	*input = i - 1;
	return 0;
}
867

868 869 870 871 872 873
static int uvc_ioctl_s_input(struct file *file, void *fh, unsigned int input)
{
	struct uvc_fh *handle = fh;
	struct uvc_video_chain *chain = handle->chain;
	int ret;
	u32 i;
874

875 876 877
	ret = uvc_acquire_privileges(handle);
	if (ret < 0)
		return ret;
878

879 880 881 882 883
	if (chain->selector == NULL ||
	    (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
		if (input)
			return -EINVAL;
		return 0;
884 885
	}

886 887
	if (input >= chain->selector->bNrInPins)
		return -EINVAL;
888

889 890 891 892 893
	i = input + 1;
	return uvc_query_ctrl(chain->dev, UVC_SET_CUR, chain->selector->id,
			      chain->dev->intfnum, UVC_SU_INPUT_SELECT_CONTROL,
			      &i, 1);
}
894

895 896 897 898 899
static int uvc_ioctl_queryctrl(struct file *file, void *fh,
			       struct v4l2_queryctrl *qc)
{
	struct uvc_fh *handle = fh;
	struct uvc_video_chain *chain = handle->chain;
900

901 902
	return uvc_query_v4l2_ctrl(chain, qc);
}
903

904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932
static int uvc_ioctl_query_ext_ctrl(struct file *file, void *fh,
				    struct v4l2_query_ext_ctrl *qec)
{
	struct uvc_fh *handle = fh;
	struct uvc_video_chain *chain = handle->chain;
	struct v4l2_queryctrl qc = { qec->id };
	int ret;

	ret = uvc_query_v4l2_ctrl(chain, &qc);
	if (ret)
		return ret;

	qec->id = qc.id;
	qec->type = qc.type;
	strlcpy(qec->name, qc.name, sizeof(qec->name));
	qec->minimum = qc.minimum;
	qec->maximum = qc.maximum;
	qec->step = qc.step;
	qec->default_value = qc.default_value;
	qec->flags = qc.flags;
	qec->elem_size = 4;
	qec->elems = 1;
	qec->nr_of_dims = 0;
	memset(qec->dims, 0, sizeof(qec->dims));
	memset(qec->reserved, 0, sizeof(qec->reserved));

	return 0;
}

933 934 935 936 937 938 939
static int uvc_ioctl_g_ctrl(struct file *file, void *fh,
			    struct v4l2_control *ctrl)
{
	struct uvc_fh *handle = fh;
	struct uvc_video_chain *chain = handle->chain;
	struct v4l2_ext_control xctrl;
	int ret;
940

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

944 945 946
	ret = uvc_ctrl_begin(chain);
	if (ret < 0)
		return ret;
947

948 949 950 951
	ret = uvc_ctrl_get(chain, &xctrl);
	uvc_ctrl_rollback(handle);
	if (ret < 0)
		return ret;
952

953 954 955
	ctrl->value = xctrl.value;
	return 0;
}
956

957 958 959 960 961 962 963
static int uvc_ioctl_s_ctrl(struct file *file, void *fh,
			    struct v4l2_control *ctrl)
{
	struct uvc_fh *handle = fh;
	struct uvc_video_chain *chain = handle->chain;
	struct v4l2_ext_control xctrl;
	int ret;
964

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

969 970 971 972 973 974 975 976
	ret = uvc_ctrl_begin(chain);
	if (ret < 0)
		return ret;

	ret = uvc_ctrl_set(chain, &xctrl);
	if (ret < 0) {
		uvc_ctrl_rollback(handle);
		return ret;
977 978
	}

979 980 981
	ret = uvc_ctrl_commit(handle, &xctrl, 1);
	if (ret < 0)
		return ret;
982

983 984 985
	ctrl->value = xctrl.value;
	return 0;
}
986

987 988 989 990 991 992 993 994
static int uvc_ioctl_g_ext_ctrls(struct file *file, void *fh,
				 struct v4l2_ext_controls *ctrls)
{
	struct uvc_fh *handle = fh;
	struct uvc_video_chain *chain = handle->chain;
	struct v4l2_ext_control *ctrl = ctrls->controls;
	unsigned int i;
	int ret;
995

996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
	if (ctrls->which == V4L2_CTRL_WHICH_DEF_VAL) {
		for (i = 0; i < ctrls->count; ++ctrl, ++i) {
			struct v4l2_queryctrl qc = { .id = ctrl->id };

			ret = uvc_query_v4l2_ctrl(chain, &qc);
			if (ret < 0) {
				ctrls->error_idx = i;
				return ret;
			}

			ctrl->value = qc.default_value;
		}

		return 0;
	}

1012 1013 1014
	ret = uvc_ctrl_begin(chain);
	if (ret < 0)
		return ret;
1015

1016 1017 1018 1019 1020 1021 1022 1023
	for (i = 0; i < ctrls->count; ++ctrl, ++i) {
		ret = uvc_ctrl_get(chain, ctrl);
		if (ret < 0) {
			uvc_ctrl_rollback(handle);
			ctrls->error_idx = i;
			return ret;
		}
	}
1024

1025
	ctrls->error_idx = 0;
1026

1027 1028
	return uvc_ctrl_rollback(handle);
}
1029

1030 1031 1032 1033 1034 1035 1036 1037
static int uvc_ioctl_s_try_ext_ctrls(struct uvc_fh *handle,
				     struct v4l2_ext_controls *ctrls,
				     bool commit)
{
	struct v4l2_ext_control *ctrl = ctrls->controls;
	struct uvc_video_chain *chain = handle->chain;
	unsigned int i;
	int ret;
1038

1039 1040 1041 1042
	/* Default value cannot be changed */
	if (ctrls->which == V4L2_CTRL_WHICH_DEF_VAL)
		return -EINVAL;

1043 1044 1045 1046 1047 1048 1049 1050 1051
	ret = uvc_ctrl_begin(chain);
	if (ret < 0)
		return ret;

	for (i = 0; i < ctrls->count; ++ctrl, ++i) {
		ret = uvc_ctrl_set(chain, ctrl);
		if (ret < 0) {
			uvc_ctrl_rollback(handle);
			ctrls->error_idx = commit ? ctrls->count : i;
1052
			return ret;
1053
		}
1054 1055
	}

1056
	ctrls->error_idx = 0;
1057

1058 1059 1060 1061 1062
	if (commit)
		return uvc_ctrl_commit(handle, ctrls->controls, ctrls->count);
	else
		return uvc_ctrl_rollback(handle);
}
1063

1064 1065 1066 1067
static int uvc_ioctl_s_ext_ctrls(struct file *file, void *fh,
				 struct v4l2_ext_controls *ctrls)
{
	struct uvc_fh *handle = fh;
1068

1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
	return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, true);
}

static int uvc_ioctl_try_ext_ctrls(struct file *file, void *fh,
				   struct v4l2_ext_controls *ctrls)
{
	struct uvc_fh *handle = fh;

	return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, false);
}
1079

1080 1081 1082 1083 1084 1085 1086 1087 1088
static int uvc_ioctl_querymenu(struct file *file, void *fh,
			       struct v4l2_querymenu *qm)
{
	struct uvc_fh *handle = fh;
	struct uvc_video_chain *chain = handle->chain;

	return uvc_query_v4l2_menu(chain, qm);
}

1089 1090
static int uvc_ioctl_g_selection(struct file *file, void *fh,
				 struct v4l2_selection *sel)
1091 1092 1093 1094
{
	struct uvc_fh *handle = fh;
	struct uvc_streaming *stream = handle->stream;

1095
	if (sel->type != stream->type)
1096 1097
		return -EINVAL;

1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
	switch (sel->target) {
	case V4L2_SEL_TGT_CROP_DEFAULT:
	case V4L2_SEL_TGT_CROP_BOUNDS:
		if (stream->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
			return -EINVAL;
		break;
	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
		if (stream->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
			return -EINVAL;
		break;
	default:
		return -EINVAL;
	}

	sel->r.left = 0;
	sel->r.top = 0;
1115
	mutex_lock(&stream->mutex);
1116 1117
	sel->r.width = stream->cur_frame->wWidth;
	sel->r.height = stream->cur_frame->wHeight;
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
	mutex_unlock(&stream->mutex);

	return 0;
}

static int uvc_ioctl_g_parm(struct file *file, void *fh,
			    struct v4l2_streamparm *parm)
{
	struct uvc_fh *handle = fh;
	struct uvc_streaming *stream = handle->stream;

	return uvc_v4l2_get_streamparm(stream, parm);
}

static int uvc_ioctl_s_parm(struct file *file, void *fh,
			    struct v4l2_streamparm *parm)
{
	struct uvc_fh *handle = fh;
	struct uvc_streaming *stream = handle->stream;
	int ret;

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

	return uvc_v4l2_set_streamparm(stream, parm);
}

static int uvc_ioctl_enum_framesizes(struct file *file, void *fh,
				     struct v4l2_frmsizeenum *fsize)
{
	struct uvc_fh *handle = fh;
	struct uvc_streaming *stream = handle->stream;
	struct uvc_format *format = NULL;
	struct uvc_frame *frame;
	int i;

	/* Look for the given pixel format */
	for (i = 0; i < stream->nformats; i++) {
		if (stream->format[i].fcc == fsize->pixel_format) {
			format = &stream->format[i];
			break;
		}
1161
	}
1162 1163
	if (format == NULL)
		return -EINVAL;
1164

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

1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
	frame = &format->frame[fsize->index];
	fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
	fsize->discrete.width = frame->wWidth;
	fsize->discrete.height = frame->wHeight;
	return 0;
}

static int uvc_ioctl_enum_frameintervals(struct file *file, void *fh,
					 struct v4l2_frmivalenum *fival)
{
	struct uvc_fh *handle = fh;
	struct uvc_streaming *stream = handle->stream;
	struct uvc_format *format = NULL;
	struct uvc_frame *frame = NULL;
	int i;

	/* Look for the given pixel format and frame size */
	for (i = 0; i < stream->nformats; i++) {
		if (stream->format[i].fcc == fival->pixel_format) {
			format = &stream->format[i];
			break;
1189 1190
		}
	}
1191 1192
	if (format == NULL)
		return -EINVAL;
1193

1194 1195 1196 1197 1198 1199 1200 1201 1202
	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;
1203

1204 1205 1206
	if (frame->bFrameIntervalType) {
		if (fival->index >= frame->bFrameIntervalType)
			return -EINVAL;
1207

1208 1209 1210 1211 1212 1213 1214
		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 {
1215 1216 1217
		if (fival->index)
			return -EINVAL;

1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
		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);
	}
1232

1233 1234
	return 0;
}
1235

1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
static int uvc_ioctl_subscribe_event(struct v4l2_fh *fh,
				     const struct v4l2_event_subscription *sub)
{
	switch (sub->type) {
	case V4L2_EVENT_CTRL:
		return v4l2_event_subscribe(fh, sub, 0, &uvc_ctrl_sub_ev_ops);
	default:
		return -EINVAL;
	}
}
1246

1247 1248 1249 1250 1251
static long uvc_ioctl_default(struct file *file, void *fh, bool valid_prio,
			      unsigned int cmd, void *arg)
{
	struct uvc_fh *handle = fh;
	struct uvc_video_chain *chain = handle->chain;
1252

1253 1254
	switch (cmd) {
	/* Dynamic controls. */
1255
	case UVCIOC_CTRL_MAP:
1256
		return uvc_ioctl_ctrl_map(chain, arg);
1257 1258 1259

	case UVCIOC_CTRL_QUERY:
		return uvc_xu_ctrl_query(chain, arg);
1260 1261

	default:
1262
		return -ENOTTY;
1263 1264 1265
	}
}

1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
#ifdef CONFIG_COMPAT
struct uvc_xu_control_mapping32 {
	__u32 id;
	__u8 name[32];
	__u8 entity[16];
	__u8 selector;

	__u8 size;
	__u8 offset;
	__u32 v4l2_type;
	__u32 data_type;

	compat_caddr_t menu_info;
	__u32 menu_count;

	__u32 reserved[4];
};

static int uvc_v4l2_get_xu_mapping(struct uvc_xu_control_mapping *kp,
			const struct uvc_xu_control_mapping32 __user *up)
{
	compat_caddr_t p;

	if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
	    __copy_from_user(kp, up, offsetof(typeof(*up), menu_info)) ||
	    __get_user(kp->menu_count, &up->menu_count))
		return -EFAULT;

	memset(kp->reserved, 0, sizeof(kp->reserved));

	if (kp->menu_count == 0) {
		kp->menu_info = NULL;
		return 0;
	}

	if (__get_user(p, &up->menu_info))
		return -EFAULT;
1303
	kp->menu_info = compat_ptr(p);
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315

	return 0;
}

static int uvc_v4l2_put_xu_mapping(const struct uvc_xu_control_mapping *kp,
			struct uvc_xu_control_mapping32 __user *up)
{
	if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
	    __copy_to_user(up, kp, offsetof(typeof(*up), menu_info)) ||
	    __put_user(kp->menu_count, &up->menu_count))
		return -EFAULT;

1316 1317
	if (__clear_user(up->reserved, sizeof(up->reserved)))
		return -EFAULT;
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345

	return 0;
}

struct uvc_xu_control_query32 {
	__u8 unit;
	__u8 selector;
	__u8 query;
	__u16 size;
	compat_caddr_t data;
};

static int uvc_v4l2_get_xu_query(struct uvc_xu_control_query *kp,
			const struct uvc_xu_control_query32 __user *up)
{
	compat_caddr_t p;

	if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
	    __copy_from_user(kp, up, offsetof(typeof(*up), data)))
		return -EFAULT;

	if (kp->size == 0) {
		kp->data = NULL;
		return 0;
	}

	if (__get_user(p, &up->data))
		return -EFAULT;
1346
	kp->data = compat_ptr(p);
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366

	return 0;
}

static int uvc_v4l2_put_xu_query(const struct uvc_xu_control_query *kp,
			struct uvc_xu_control_query32 __user *up)
{
	if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
	    __copy_to_user(up, kp, offsetof(typeof(*up), data)))
		return -EFAULT;

	return 0;
}

#define UVCIOC_CTRL_MAP32	_IOWR('u', 0x20, struct uvc_xu_control_mapping32)
#define UVCIOC_CTRL_QUERY32	_IOWR('u', 0x21, struct uvc_xu_control_query32)

static long uvc_v4l2_compat_ioctl32(struct file *file,
		     unsigned int cmd, unsigned long arg)
{
1367
	struct uvc_fh *handle = file->private_data;
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
	union {
		struct uvc_xu_control_mapping xmap;
		struct uvc_xu_control_query xqry;
	} karg;
	void __user *up = compat_ptr(arg);
	long ret;

	switch (cmd) {
	case UVCIOC_CTRL_MAP32:
		ret = uvc_v4l2_get_xu_mapping(&karg.xmap, up);
1378 1379 1380 1381 1382 1383 1384 1385 1386
		if (ret)
			return ret;
		ret = uvc_ioctl_ctrl_map(handle->chain, &karg.xmap);
		if (ret)
			return ret;
		ret = uvc_v4l2_put_xu_mapping(&karg.xmap, up);
		if (ret)
			return ret;

1387 1388 1389 1390
		break;

	case UVCIOC_CTRL_QUERY32:
		ret = uvc_v4l2_get_xu_query(&karg.xqry, up);
1391 1392 1393 1394 1395 1396 1397 1398
		if (ret)
			return ret;
		ret = uvc_xu_ctrl_query(handle->chain, &karg.xqry);
		if (ret)
			return ret;
		ret = uvc_v4l2_put_xu_query(&karg.xqry, up);
		if (ret)
			return ret;
1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
		break;

	default:
		return -ENOIOCTLCMD;
	}

	return ret;
}
#endif

1409 1410 1411 1412
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");
1413
	return -EINVAL;
1414 1415 1416 1417
}

static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
{
1418
	struct uvc_fh *handle = file->private_data;
1419
	struct uvc_streaming *stream = handle->stream;
1420 1421 1422

	uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");

1423
	return uvc_queue_mmap(&stream->queue, vma);
1424 1425 1426 1427
}

static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
{
1428
	struct uvc_fh *handle = file->private_data;
1429
	struct uvc_streaming *stream = handle->stream;
1430 1431 1432

	uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");

1433
	return uvc_queue_poll(&stream->queue, file, wait);
1434 1435
}

1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449
#ifndef CONFIG_MMU
static unsigned long uvc_v4l2_get_unmapped_area(struct file *file,
		unsigned long addr, unsigned long len, unsigned long pgoff,
		unsigned long flags)
{
	struct uvc_fh *handle = file->private_data;
	struct uvc_streaming *stream = handle->stream;

	uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_get_unmapped_area\n");

	return uvc_queue_get_unmapped_area(&stream->queue, pgoff);
}
#endif

1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462
const struct v4l2_ioctl_ops uvc_ioctl_ops = {
	.vidioc_querycap = uvc_ioctl_querycap,
	.vidioc_enum_fmt_vid_cap = uvc_ioctl_enum_fmt_vid_cap,
	.vidioc_enum_fmt_vid_out = uvc_ioctl_enum_fmt_vid_out,
	.vidioc_g_fmt_vid_cap = uvc_ioctl_g_fmt_vid_cap,
	.vidioc_g_fmt_vid_out = uvc_ioctl_g_fmt_vid_out,
	.vidioc_s_fmt_vid_cap = uvc_ioctl_s_fmt_vid_cap,
	.vidioc_s_fmt_vid_out = uvc_ioctl_s_fmt_vid_out,
	.vidioc_try_fmt_vid_cap = uvc_ioctl_try_fmt_vid_cap,
	.vidioc_try_fmt_vid_out = uvc_ioctl_try_fmt_vid_out,
	.vidioc_reqbufs = uvc_ioctl_reqbufs,
	.vidioc_querybuf = uvc_ioctl_querybuf,
	.vidioc_qbuf = uvc_ioctl_qbuf,
1463
	.vidioc_expbuf = uvc_ioctl_expbuf,
1464 1465 1466 1467 1468 1469 1470 1471
	.vidioc_dqbuf = uvc_ioctl_dqbuf,
	.vidioc_create_bufs = uvc_ioctl_create_bufs,
	.vidioc_streamon = uvc_ioctl_streamon,
	.vidioc_streamoff = uvc_ioctl_streamoff,
	.vidioc_enum_input = uvc_ioctl_enum_input,
	.vidioc_g_input = uvc_ioctl_g_input,
	.vidioc_s_input = uvc_ioctl_s_input,
	.vidioc_queryctrl = uvc_ioctl_queryctrl,
1472
	.vidioc_query_ext_ctrl = uvc_ioctl_query_ext_ctrl,
1473 1474 1475 1476 1477 1478
	.vidioc_g_ctrl = uvc_ioctl_g_ctrl,
	.vidioc_s_ctrl = uvc_ioctl_s_ctrl,
	.vidioc_g_ext_ctrls = uvc_ioctl_g_ext_ctrls,
	.vidioc_s_ext_ctrls = uvc_ioctl_s_ext_ctrls,
	.vidioc_try_ext_ctrls = uvc_ioctl_try_ext_ctrls,
	.vidioc_querymenu = uvc_ioctl_querymenu,
1479
	.vidioc_g_selection = uvc_ioctl_g_selection,
1480 1481 1482 1483 1484 1485 1486 1487 1488
	.vidioc_g_parm = uvc_ioctl_g_parm,
	.vidioc_s_parm = uvc_ioctl_s_parm,
	.vidioc_enum_framesizes = uvc_ioctl_enum_framesizes,
	.vidioc_enum_frameintervals = uvc_ioctl_enum_frameintervals,
	.vidioc_subscribe_event = uvc_ioctl_subscribe_event,
	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
	.vidioc_default = uvc_ioctl_default,
};

1489
const struct v4l2_file_operations uvc_fops = {
1490 1491 1492
	.owner		= THIS_MODULE,
	.open		= uvc_v4l2_open,
	.release	= uvc_v4l2_release,
1493
	.unlocked_ioctl	= video_ioctl2,
1494 1495 1496
#ifdef CONFIG_COMPAT
	.compat_ioctl32	= uvc_v4l2_compat_ioctl32,
#endif
1497 1498 1499
	.read		= uvc_v4l2_read,
	.mmap		= uvc_v4l2_mmap,
	.poll		= uvc_v4l2_poll,
1500 1501 1502
#ifndef CONFIG_MMU
	.get_unmapped_area = uvc_v4l2_get_unmapped_area,
#endif
1503
};
1504