xref: /openbmc/linux/drivers/media/usb/uvc/uvc_v4l2.c (revision caa80275)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *      uvc_v4l2.c  --  USB Video Class driver - V4L2 API
4  *
5  *      Copyright (C) 2005-2010
6  *          Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7  */
8 
9 #include <linux/compat.h>
10 #include <linux/kernel.h>
11 #include <linux/list.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/usb.h>
15 #include <linux/videodev2.h>
16 #include <linux/vmalloc.h>
17 #include <linux/mm.h>
18 #include <linux/wait.h>
19 #include <linux/atomic.h>
20 
21 #include <media/v4l2-common.h>
22 #include <media/v4l2-ctrls.h>
23 #include <media/v4l2-event.h>
24 #include <media/v4l2-ioctl.h>
25 
26 #include "uvcvideo.h"
27 
28 /* ------------------------------------------------------------------------
29  * UVC ioctls
30  */
31 static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain,
32 	struct uvc_xu_control_mapping *xmap)
33 {
34 	struct uvc_control_mapping *map;
35 	unsigned int size;
36 	int ret;
37 
38 	map = kzalloc(sizeof(*map), GFP_KERNEL);
39 	if (map == NULL)
40 		return -ENOMEM;
41 
42 	map->id = xmap->id;
43 	memcpy(map->name, xmap->name, sizeof(map->name));
44 	memcpy(map->entity, xmap->entity, sizeof(map->entity));
45 	map->selector = xmap->selector;
46 	map->size = xmap->size;
47 	map->offset = xmap->offset;
48 	map->v4l2_type = xmap->v4l2_type;
49 	map->data_type = xmap->data_type;
50 
51 	switch (xmap->v4l2_type) {
52 	case V4L2_CTRL_TYPE_INTEGER:
53 	case V4L2_CTRL_TYPE_BOOLEAN:
54 	case V4L2_CTRL_TYPE_BUTTON:
55 		break;
56 
57 	case V4L2_CTRL_TYPE_MENU:
58 		/* Prevent excessive memory consumption, as well as integer
59 		 * overflows.
60 		 */
61 		if (xmap->menu_count == 0 ||
62 		    xmap->menu_count > UVC_MAX_CONTROL_MENU_ENTRIES) {
63 			ret = -EINVAL;
64 			goto free_map;
65 		}
66 
67 		size = xmap->menu_count * sizeof(*map->menu_info);
68 		map->menu_info = memdup_user(xmap->menu_info, size);
69 		if (IS_ERR(map->menu_info)) {
70 			ret = PTR_ERR(map->menu_info);
71 			goto free_map;
72 		}
73 
74 		map->menu_count = xmap->menu_count;
75 		break;
76 
77 	default:
78 		uvc_dbg(chain->dev, CONTROL,
79 			"Unsupported V4L2 control type %u\n", xmap->v4l2_type);
80 		ret = -ENOTTY;
81 		goto free_map;
82 	}
83 
84 	ret = uvc_ctrl_add_mapping(chain, map);
85 
86 	kfree(map->menu_info);
87 free_map:
88 	kfree(map);
89 
90 	return ret;
91 }
92 
93 /* ------------------------------------------------------------------------
94  * V4L2 interface
95  */
96 
97 /*
98  * Find the frame interval closest to the requested frame interval for the
99  * given frame format and size. This should be done by the device as part of
100  * the Video Probe and Commit negotiation, but some hardware don't implement
101  * that feature.
102  */
103 static u32 uvc_try_frame_interval(struct uvc_frame *frame, u32 interval)
104 {
105 	unsigned int i;
106 
107 	if (frame->bFrameIntervalType) {
108 		u32 best = -1, dist;
109 
110 		for (i = 0; i < frame->bFrameIntervalType; ++i) {
111 			dist = interval > frame->dwFrameInterval[i]
112 			     ? interval - frame->dwFrameInterval[i]
113 			     : frame->dwFrameInterval[i] - interval;
114 
115 			if (dist > best)
116 				break;
117 
118 			best = dist;
119 		}
120 
121 		interval = frame->dwFrameInterval[i-1];
122 	} else {
123 		const u32 min = frame->dwFrameInterval[0];
124 		const u32 max = frame->dwFrameInterval[1];
125 		const u32 step = frame->dwFrameInterval[2];
126 
127 		interval = min + (interval - min + step/2) / step * step;
128 		if (interval > max)
129 			interval = max;
130 	}
131 
132 	return interval;
133 }
134 
135 static u32 uvc_v4l2_get_bytesperline(const struct uvc_format *format,
136 	const struct uvc_frame *frame)
137 {
138 	switch (format->fcc) {
139 	case V4L2_PIX_FMT_NV12:
140 	case V4L2_PIX_FMT_YVU420:
141 	case V4L2_PIX_FMT_YUV420:
142 	case V4L2_PIX_FMT_M420:
143 		return frame->wWidth;
144 
145 	default:
146 		return format->bpp * frame->wWidth / 8;
147 	}
148 }
149 
150 static int uvc_v4l2_try_format(struct uvc_streaming *stream,
151 	struct v4l2_format *fmt, struct uvc_streaming_control *probe,
152 	struct uvc_format **uvc_format, struct uvc_frame **uvc_frame)
153 {
154 	struct uvc_format *format = NULL;
155 	struct uvc_frame *frame = NULL;
156 	u16 rw, rh;
157 	unsigned int d, maxd;
158 	unsigned int i;
159 	u32 interval;
160 	int ret = 0;
161 	u8 *fcc;
162 
163 	if (fmt->type != stream->type)
164 		return -EINVAL;
165 
166 	fcc = (u8 *)&fmt->fmt.pix.pixelformat;
167 	uvc_dbg(stream->dev, FORMAT, "Trying format 0x%08x (%c%c%c%c): %ux%u\n",
168 		fmt->fmt.pix.pixelformat,
169 		fcc[0], fcc[1], fcc[2], fcc[3],
170 		fmt->fmt.pix.width, fmt->fmt.pix.height);
171 
172 	/* Check if the hardware supports the requested format, use the default
173 	 * format otherwise.
174 	 */
175 	for (i = 0; i < stream->nformats; ++i) {
176 		format = &stream->format[i];
177 		if (format->fcc == fmt->fmt.pix.pixelformat)
178 			break;
179 	}
180 
181 	if (i == stream->nformats) {
182 		format = stream->def_format;
183 		fmt->fmt.pix.pixelformat = format->fcc;
184 	}
185 
186 	/* Find the closest image size. The distance between image sizes is
187 	 * the size in pixels of the non-overlapping regions between the
188 	 * requested size and the frame-specified size.
189 	 */
190 	rw = fmt->fmt.pix.width;
191 	rh = fmt->fmt.pix.height;
192 	maxd = (unsigned int)-1;
193 
194 	for (i = 0; i < format->nframes; ++i) {
195 		u16 w = format->frame[i].wWidth;
196 		u16 h = format->frame[i].wHeight;
197 
198 		d = min(w, rw) * min(h, rh);
199 		d = w*h + rw*rh - 2*d;
200 		if (d < maxd) {
201 			maxd = d;
202 			frame = &format->frame[i];
203 		}
204 
205 		if (maxd == 0)
206 			break;
207 	}
208 
209 	if (frame == NULL) {
210 		uvc_dbg(stream->dev, FORMAT, "Unsupported size %ux%u\n",
211 			fmt->fmt.pix.width, fmt->fmt.pix.height);
212 		return -EINVAL;
213 	}
214 
215 	/* Use the default frame interval. */
216 	interval = frame->dwDefaultFrameInterval;
217 	uvc_dbg(stream->dev, FORMAT,
218 		"Using default frame interval %u.%u us (%u.%u fps)\n",
219 		interval / 10, interval % 10, 10000000 / interval,
220 		(100000000 / interval) % 10);
221 
222 	/* Set the format index, frame index and frame interval. */
223 	memset(probe, 0, sizeof(*probe));
224 	probe->bmHint = 1;	/* dwFrameInterval */
225 	probe->bFormatIndex = format->index;
226 	probe->bFrameIndex = frame->bFrameIndex;
227 	probe->dwFrameInterval = uvc_try_frame_interval(frame, interval);
228 	/* Some webcams stall the probe control set request when the
229 	 * dwMaxVideoFrameSize field is set to zero. The UVC specification
230 	 * clearly states that the field is read-only from the host, so this
231 	 * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by
232 	 * the webcam to work around the problem.
233 	 *
234 	 * The workaround could probably be enabled for all webcams, so the
235 	 * quirk can be removed if needed. It's currently useful to detect
236 	 * webcam bugs and fix them before they hit the market (providing
237 	 * developers test their webcams with the Linux driver as well as with
238 	 * the Windows driver).
239 	 */
240 	mutex_lock(&stream->mutex);
241 	if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS)
242 		probe->dwMaxVideoFrameSize =
243 			stream->ctrl.dwMaxVideoFrameSize;
244 
245 	/* Probe the device. */
246 	ret = uvc_probe_video(stream, probe);
247 	mutex_unlock(&stream->mutex);
248 	if (ret < 0)
249 		goto done;
250 
251 	/* After the probe, update fmt with the values returned from
252 	 * negotiation with the device. Some devices return invalid bFormatIndex
253 	 * and bFrameIndex values, in which case we can only assume they have
254 	 * accepted the requested format as-is.
255 	 */
256 	for (i = 0; i < stream->nformats; ++i) {
257 		if (probe->bFormatIndex == stream->format[i].index) {
258 			format = &stream->format[i];
259 			break;
260 		}
261 	}
262 
263 	if (i == stream->nformats)
264 		uvc_dbg(stream->dev, FORMAT,
265 			"Unknown bFormatIndex %u, using default\n",
266 			probe->bFormatIndex);
267 
268 	for (i = 0; i < format->nframes; ++i) {
269 		if (probe->bFrameIndex == format->frame[i].bFrameIndex) {
270 			frame = &format->frame[i];
271 			break;
272 		}
273 	}
274 
275 	if (i == format->nframes)
276 		uvc_dbg(stream->dev, FORMAT,
277 			"Unknown bFrameIndex %u, using default\n",
278 			probe->bFrameIndex);
279 
280 	fmt->fmt.pix.width = frame->wWidth;
281 	fmt->fmt.pix.height = frame->wHeight;
282 	fmt->fmt.pix.field = V4L2_FIELD_NONE;
283 	fmt->fmt.pix.bytesperline = uvc_v4l2_get_bytesperline(format, frame);
284 	fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize;
285 	fmt->fmt.pix.pixelformat = format->fcc;
286 	fmt->fmt.pix.colorspace = format->colorspace;
287 	fmt->fmt.pix.xfer_func = format->xfer_func;
288 	fmt->fmt.pix.ycbcr_enc = format->ycbcr_enc;
289 
290 	if (uvc_format != NULL)
291 		*uvc_format = format;
292 	if (uvc_frame != NULL)
293 		*uvc_frame = frame;
294 
295 done:
296 	return ret;
297 }
298 
299 static int uvc_v4l2_get_format(struct uvc_streaming *stream,
300 	struct v4l2_format *fmt)
301 {
302 	struct uvc_format *format;
303 	struct uvc_frame *frame;
304 	int ret = 0;
305 
306 	if (fmt->type != stream->type)
307 		return -EINVAL;
308 
309 	mutex_lock(&stream->mutex);
310 	format = stream->cur_format;
311 	frame = stream->cur_frame;
312 
313 	if (format == NULL || frame == NULL) {
314 		ret = -EINVAL;
315 		goto done;
316 	}
317 
318 	fmt->fmt.pix.pixelformat = format->fcc;
319 	fmt->fmt.pix.width = frame->wWidth;
320 	fmt->fmt.pix.height = frame->wHeight;
321 	fmt->fmt.pix.field = V4L2_FIELD_NONE;
322 	fmt->fmt.pix.bytesperline = uvc_v4l2_get_bytesperline(format, frame);
323 	fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize;
324 	fmt->fmt.pix.colorspace = format->colorspace;
325 	fmt->fmt.pix.xfer_func = format->xfer_func;
326 	fmt->fmt.pix.ycbcr_enc = format->ycbcr_enc;
327 
328 done:
329 	mutex_unlock(&stream->mutex);
330 	return ret;
331 }
332 
333 static int uvc_v4l2_set_format(struct uvc_streaming *stream,
334 	struct v4l2_format *fmt)
335 {
336 	struct uvc_streaming_control probe;
337 	struct uvc_format *format;
338 	struct uvc_frame *frame;
339 	int ret;
340 
341 	if (fmt->type != stream->type)
342 		return -EINVAL;
343 
344 	ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame);
345 	if (ret < 0)
346 		return ret;
347 
348 	mutex_lock(&stream->mutex);
349 
350 	if (uvc_queue_allocated(&stream->queue)) {
351 		ret = -EBUSY;
352 		goto done;
353 	}
354 
355 	stream->ctrl = probe;
356 	stream->cur_format = format;
357 	stream->cur_frame = frame;
358 
359 done:
360 	mutex_unlock(&stream->mutex);
361 	return ret;
362 }
363 
364 static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream,
365 		struct v4l2_streamparm *parm)
366 {
367 	u32 numerator, denominator;
368 
369 	if (parm->type != stream->type)
370 		return -EINVAL;
371 
372 	mutex_lock(&stream->mutex);
373 	numerator = stream->ctrl.dwFrameInterval;
374 	mutex_unlock(&stream->mutex);
375 
376 	denominator = 10000000;
377 	uvc_simplify_fraction(&numerator, &denominator, 8, 333);
378 
379 	memset(parm, 0, sizeof(*parm));
380 	parm->type = stream->type;
381 
382 	if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
383 		parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
384 		parm->parm.capture.capturemode = 0;
385 		parm->parm.capture.timeperframe.numerator = numerator;
386 		parm->parm.capture.timeperframe.denominator = denominator;
387 		parm->parm.capture.extendedmode = 0;
388 		parm->parm.capture.readbuffers = 0;
389 	} else {
390 		parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
391 		parm->parm.output.outputmode = 0;
392 		parm->parm.output.timeperframe.numerator = numerator;
393 		parm->parm.output.timeperframe.denominator = denominator;
394 	}
395 
396 	return 0;
397 }
398 
399 static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
400 		struct v4l2_streamparm *parm)
401 {
402 	struct uvc_streaming_control probe;
403 	struct v4l2_fract timeperframe;
404 	struct uvc_format *format;
405 	struct uvc_frame *frame;
406 	u32 interval, maxd;
407 	unsigned int i;
408 	int ret;
409 
410 	if (parm->type != stream->type)
411 		return -EINVAL;
412 
413 	if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
414 		timeperframe = parm->parm.capture.timeperframe;
415 	else
416 		timeperframe = parm->parm.output.timeperframe;
417 
418 	interval = uvc_fraction_to_interval(timeperframe.numerator,
419 		timeperframe.denominator);
420 	uvc_dbg(stream->dev, FORMAT, "Setting frame interval to %u/%u (%u)\n",
421 		timeperframe.numerator, timeperframe.denominator, interval);
422 
423 	mutex_lock(&stream->mutex);
424 
425 	if (uvc_queue_streaming(&stream->queue)) {
426 		mutex_unlock(&stream->mutex);
427 		return -EBUSY;
428 	}
429 
430 	format = stream->cur_format;
431 	frame = stream->cur_frame;
432 	probe = stream->ctrl;
433 	probe.dwFrameInterval = uvc_try_frame_interval(frame, interval);
434 	maxd = abs((s32)probe.dwFrameInterval - interval);
435 
436 	/* Try frames with matching size to find the best frame interval. */
437 	for (i = 0; i < format->nframes && maxd != 0; i++) {
438 		u32 d, ival;
439 
440 		if (&format->frame[i] == stream->cur_frame)
441 			continue;
442 
443 		if (format->frame[i].wWidth != stream->cur_frame->wWidth ||
444 		    format->frame[i].wHeight != stream->cur_frame->wHeight)
445 			continue;
446 
447 		ival = uvc_try_frame_interval(&format->frame[i], interval);
448 		d = abs((s32)ival - interval);
449 		if (d >= maxd)
450 			continue;
451 
452 		frame = &format->frame[i];
453 		probe.bFrameIndex = frame->bFrameIndex;
454 		probe.dwFrameInterval = ival;
455 		maxd = d;
456 	}
457 
458 	/* Probe the device with the new settings. */
459 	ret = uvc_probe_video(stream, &probe);
460 	if (ret < 0) {
461 		mutex_unlock(&stream->mutex);
462 		return ret;
463 	}
464 
465 	stream->ctrl = probe;
466 	stream->cur_frame = frame;
467 	mutex_unlock(&stream->mutex);
468 
469 	/* Return the actual frame period. */
470 	timeperframe.numerator = probe.dwFrameInterval;
471 	timeperframe.denominator = 10000000;
472 	uvc_simplify_fraction(&timeperframe.numerator,
473 		&timeperframe.denominator, 8, 333);
474 
475 	if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
476 		parm->parm.capture.timeperframe = timeperframe;
477 	else
478 		parm->parm.output.timeperframe = timeperframe;
479 
480 	return 0;
481 }
482 
483 /* ------------------------------------------------------------------------
484  * Privilege management
485  */
486 
487 /*
488  * Privilege management is the multiple-open implementation basis. The current
489  * implementation is completely transparent for the end-user and doesn't
490  * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls.
491  * Those ioctls enable finer control on the device (by making possible for a
492  * user to request exclusive access to a device), but are not mature yet.
493  * Switching to the V4L2 priority mechanism might be considered in the future
494  * if this situation changes.
495  *
496  * Each open instance of a UVC device can either be in a privileged or
497  * unprivileged state. Only a single instance can be in a privileged state at
498  * a given time. Trying to perform an operation that requires privileges will
499  * automatically acquire the required privileges if possible, or return -EBUSY
500  * otherwise. Privileges are dismissed when closing the instance or when
501  * freeing the video buffers using VIDIOC_REQBUFS.
502  *
503  * Operations that require privileges are:
504  *
505  * - VIDIOC_S_INPUT
506  * - VIDIOC_S_PARM
507  * - VIDIOC_S_FMT
508  * - VIDIOC_REQBUFS
509  */
510 static int uvc_acquire_privileges(struct uvc_fh *handle)
511 {
512 	/* Always succeed if the handle is already privileged. */
513 	if (handle->state == UVC_HANDLE_ACTIVE)
514 		return 0;
515 
516 	/* Check if the device already has a privileged handle. */
517 	if (atomic_inc_return(&handle->stream->active) != 1) {
518 		atomic_dec(&handle->stream->active);
519 		return -EBUSY;
520 	}
521 
522 	handle->state = UVC_HANDLE_ACTIVE;
523 	return 0;
524 }
525 
526 static void uvc_dismiss_privileges(struct uvc_fh *handle)
527 {
528 	if (handle->state == UVC_HANDLE_ACTIVE)
529 		atomic_dec(&handle->stream->active);
530 
531 	handle->state = UVC_HANDLE_PASSIVE;
532 }
533 
534 static int uvc_has_privileges(struct uvc_fh *handle)
535 {
536 	return handle->state == UVC_HANDLE_ACTIVE;
537 }
538 
539 /* ------------------------------------------------------------------------
540  * V4L2 file operations
541  */
542 
543 static int uvc_v4l2_open(struct file *file)
544 {
545 	struct uvc_streaming *stream;
546 	struct uvc_fh *handle;
547 	int ret = 0;
548 
549 	stream = video_drvdata(file);
550 	uvc_dbg(stream->dev, CALLS, "%s\n", __func__);
551 
552 	ret = usb_autopm_get_interface(stream->dev->intf);
553 	if (ret < 0)
554 		return ret;
555 
556 	/* Create the device handle. */
557 	handle = kzalloc(sizeof(*handle), GFP_KERNEL);
558 	if (handle == NULL) {
559 		usb_autopm_put_interface(stream->dev->intf);
560 		return -ENOMEM;
561 	}
562 
563 	mutex_lock(&stream->dev->lock);
564 	if (stream->dev->users == 0) {
565 		ret = uvc_status_start(stream->dev, GFP_KERNEL);
566 		if (ret < 0) {
567 			mutex_unlock(&stream->dev->lock);
568 			usb_autopm_put_interface(stream->dev->intf);
569 			kfree(handle);
570 			return ret;
571 		}
572 	}
573 
574 	stream->dev->users++;
575 	mutex_unlock(&stream->dev->lock);
576 
577 	v4l2_fh_init(&handle->vfh, &stream->vdev);
578 	v4l2_fh_add(&handle->vfh);
579 	handle->chain = stream->chain;
580 	handle->stream = stream;
581 	handle->state = UVC_HANDLE_PASSIVE;
582 	file->private_data = handle;
583 
584 	return 0;
585 }
586 
587 static int uvc_v4l2_release(struct file *file)
588 {
589 	struct uvc_fh *handle = file->private_data;
590 	struct uvc_streaming *stream = handle->stream;
591 
592 	uvc_dbg(stream->dev, CALLS, "%s\n", __func__);
593 
594 	/* Only free resources if this is a privileged handle. */
595 	if (uvc_has_privileges(handle))
596 		uvc_queue_release(&stream->queue);
597 
598 	/* Release the file handle. */
599 	uvc_dismiss_privileges(handle);
600 	v4l2_fh_del(&handle->vfh);
601 	v4l2_fh_exit(&handle->vfh);
602 	kfree(handle);
603 	file->private_data = NULL;
604 
605 	mutex_lock(&stream->dev->lock);
606 	if (--stream->dev->users == 0)
607 		uvc_status_stop(stream->dev);
608 	mutex_unlock(&stream->dev->lock);
609 
610 	usb_autopm_put_interface(stream->dev->intf);
611 	return 0;
612 }
613 
614 static int uvc_ioctl_querycap(struct file *file, void *fh,
615 			      struct v4l2_capability *cap)
616 {
617 	struct video_device *vdev = video_devdata(file);
618 	struct uvc_fh *handle = file->private_data;
619 	struct uvc_video_chain *chain = handle->chain;
620 	struct uvc_streaming *stream = handle->stream;
621 
622 	strscpy(cap->driver, "uvcvideo", sizeof(cap->driver));
623 	strscpy(cap->card, vdev->name, sizeof(cap->card));
624 	usb_make_path(stream->dev->udev, cap->bus_info, sizeof(cap->bus_info));
625 	cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
626 			  | chain->caps;
627 
628 	return 0;
629 }
630 
631 static int uvc_ioctl_enum_fmt(struct uvc_streaming *stream,
632 			      struct v4l2_fmtdesc *fmt)
633 {
634 	struct uvc_format *format;
635 	enum v4l2_buf_type type = fmt->type;
636 	u32 index = fmt->index;
637 
638 	if (fmt->type != stream->type || fmt->index >= stream->nformats)
639 		return -EINVAL;
640 
641 	memset(fmt, 0, sizeof(*fmt));
642 	fmt->index = index;
643 	fmt->type = type;
644 
645 	format = &stream->format[fmt->index];
646 	fmt->flags = 0;
647 	if (format->flags & UVC_FMT_FLAG_COMPRESSED)
648 		fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
649 	strscpy(fmt->description, format->name, sizeof(fmt->description));
650 	fmt->description[sizeof(fmt->description) - 1] = 0;
651 	fmt->pixelformat = format->fcc;
652 	return 0;
653 }
654 
655 static int uvc_ioctl_enum_fmt_vid_cap(struct file *file, void *fh,
656 				      struct v4l2_fmtdesc *fmt)
657 {
658 	struct uvc_fh *handle = fh;
659 	struct uvc_streaming *stream = handle->stream;
660 
661 	return uvc_ioctl_enum_fmt(stream, fmt);
662 }
663 
664 static int uvc_ioctl_enum_fmt_vid_out(struct file *file, void *fh,
665 				      struct v4l2_fmtdesc *fmt)
666 {
667 	struct uvc_fh *handle = fh;
668 	struct uvc_streaming *stream = handle->stream;
669 
670 	return uvc_ioctl_enum_fmt(stream, fmt);
671 }
672 
673 static int uvc_ioctl_g_fmt_vid_cap(struct file *file, void *fh,
674 				   struct v4l2_format *fmt)
675 {
676 	struct uvc_fh *handle = fh;
677 	struct uvc_streaming *stream = handle->stream;
678 
679 	return uvc_v4l2_get_format(stream, fmt);
680 }
681 
682 static int uvc_ioctl_g_fmt_vid_out(struct file *file, void *fh,
683 				   struct v4l2_format *fmt)
684 {
685 	struct uvc_fh *handle = fh;
686 	struct uvc_streaming *stream = handle->stream;
687 
688 	return uvc_v4l2_get_format(stream, fmt);
689 }
690 
691 static int uvc_ioctl_s_fmt_vid_cap(struct file *file, void *fh,
692 				   struct v4l2_format *fmt)
693 {
694 	struct uvc_fh *handle = fh;
695 	struct uvc_streaming *stream = handle->stream;
696 	int ret;
697 
698 	ret = uvc_acquire_privileges(handle);
699 	if (ret < 0)
700 		return ret;
701 
702 	return uvc_v4l2_set_format(stream, fmt);
703 }
704 
705 static int uvc_ioctl_s_fmt_vid_out(struct file *file, void *fh,
706 				   struct v4l2_format *fmt)
707 {
708 	struct uvc_fh *handle = fh;
709 	struct uvc_streaming *stream = handle->stream;
710 	int ret;
711 
712 	ret = uvc_acquire_privileges(handle);
713 	if (ret < 0)
714 		return ret;
715 
716 	return uvc_v4l2_set_format(stream, fmt);
717 }
718 
719 static int uvc_ioctl_try_fmt_vid_cap(struct file *file, void *fh,
720 				     struct v4l2_format *fmt)
721 {
722 	struct uvc_fh *handle = fh;
723 	struct uvc_streaming *stream = handle->stream;
724 	struct uvc_streaming_control probe;
725 
726 	return uvc_v4l2_try_format(stream, fmt, &probe, NULL, NULL);
727 }
728 
729 static int uvc_ioctl_try_fmt_vid_out(struct file *file, void *fh,
730 				     struct v4l2_format *fmt)
731 {
732 	struct uvc_fh *handle = fh;
733 	struct uvc_streaming *stream = handle->stream;
734 	struct uvc_streaming_control probe;
735 
736 	return uvc_v4l2_try_format(stream, fmt, &probe, NULL, NULL);
737 }
738 
739 static int uvc_ioctl_reqbufs(struct file *file, void *fh,
740 			     struct v4l2_requestbuffers *rb)
741 {
742 	struct uvc_fh *handle = fh;
743 	struct uvc_streaming *stream = handle->stream;
744 	int ret;
745 
746 	ret = uvc_acquire_privileges(handle);
747 	if (ret < 0)
748 		return ret;
749 
750 	mutex_lock(&stream->mutex);
751 	ret = uvc_request_buffers(&stream->queue, rb);
752 	mutex_unlock(&stream->mutex);
753 	if (ret < 0)
754 		return ret;
755 
756 	if (ret == 0)
757 		uvc_dismiss_privileges(handle);
758 
759 	return 0;
760 }
761 
762 static int uvc_ioctl_querybuf(struct file *file, void *fh,
763 			      struct v4l2_buffer *buf)
764 {
765 	struct uvc_fh *handle = fh;
766 	struct uvc_streaming *stream = handle->stream;
767 
768 	if (!uvc_has_privileges(handle))
769 		return -EBUSY;
770 
771 	return uvc_query_buffer(&stream->queue, buf);
772 }
773 
774 static int uvc_ioctl_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
775 {
776 	struct uvc_fh *handle = fh;
777 	struct uvc_streaming *stream = handle->stream;
778 
779 	if (!uvc_has_privileges(handle))
780 		return -EBUSY;
781 
782 	return uvc_queue_buffer(&stream->queue,
783 				stream->vdev.v4l2_dev->mdev, buf);
784 }
785 
786 static int uvc_ioctl_expbuf(struct file *file, void *fh,
787 			    struct v4l2_exportbuffer *exp)
788 {
789 	struct uvc_fh *handle = fh;
790 	struct uvc_streaming *stream = handle->stream;
791 
792 	if (!uvc_has_privileges(handle))
793 		return -EBUSY;
794 
795 	return uvc_export_buffer(&stream->queue, exp);
796 }
797 
798 static int uvc_ioctl_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
799 {
800 	struct uvc_fh *handle = fh;
801 	struct uvc_streaming *stream = handle->stream;
802 
803 	if (!uvc_has_privileges(handle))
804 		return -EBUSY;
805 
806 	return uvc_dequeue_buffer(&stream->queue, buf,
807 				  file->f_flags & O_NONBLOCK);
808 }
809 
810 static int uvc_ioctl_create_bufs(struct file *file, void *fh,
811 				  struct v4l2_create_buffers *cb)
812 {
813 	struct uvc_fh *handle = fh;
814 	struct uvc_streaming *stream = handle->stream;
815 	int ret;
816 
817 	ret = uvc_acquire_privileges(handle);
818 	if (ret < 0)
819 		return ret;
820 
821 	return uvc_create_buffers(&stream->queue, cb);
822 }
823 
824 static int uvc_ioctl_streamon(struct file *file, void *fh,
825 			      enum v4l2_buf_type type)
826 {
827 	struct uvc_fh *handle = fh;
828 	struct uvc_streaming *stream = handle->stream;
829 	int ret;
830 
831 	if (!uvc_has_privileges(handle))
832 		return -EBUSY;
833 
834 	mutex_lock(&stream->mutex);
835 	ret = uvc_queue_streamon(&stream->queue, type);
836 	mutex_unlock(&stream->mutex);
837 
838 	return ret;
839 }
840 
841 static int uvc_ioctl_streamoff(struct file *file, void *fh,
842 			       enum v4l2_buf_type type)
843 {
844 	struct uvc_fh *handle = fh;
845 	struct uvc_streaming *stream = handle->stream;
846 
847 	if (!uvc_has_privileges(handle))
848 		return -EBUSY;
849 
850 	mutex_lock(&stream->mutex);
851 	uvc_queue_streamoff(&stream->queue, type);
852 	mutex_unlock(&stream->mutex);
853 
854 	return 0;
855 }
856 
857 static int uvc_ioctl_enum_input(struct file *file, void *fh,
858 				struct v4l2_input *input)
859 {
860 	struct uvc_fh *handle = fh;
861 	struct uvc_video_chain *chain = handle->chain;
862 	const struct uvc_entity *selector = chain->selector;
863 	struct uvc_entity *iterm = NULL;
864 	u32 index = input->index;
865 	int pin = 0;
866 
867 	if (selector == NULL ||
868 	    (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
869 		if (index != 0)
870 			return -EINVAL;
871 		list_for_each_entry(iterm, &chain->entities, chain) {
872 			if (UVC_ENTITY_IS_ITERM(iterm))
873 				break;
874 		}
875 		pin = iterm->id;
876 	} else if (index < selector->bNrInPins) {
877 		pin = selector->baSourceID[index];
878 		list_for_each_entry(iterm, &chain->entities, chain) {
879 			if (!UVC_ENTITY_IS_ITERM(iterm))
880 				continue;
881 			if (iterm->id == pin)
882 				break;
883 		}
884 	}
885 
886 	if (iterm == NULL || iterm->id != pin)
887 		return -EINVAL;
888 
889 	memset(input, 0, sizeof(*input));
890 	input->index = index;
891 	strscpy(input->name, iterm->name, sizeof(input->name));
892 	if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
893 		input->type = V4L2_INPUT_TYPE_CAMERA;
894 
895 	return 0;
896 }
897 
898 static int uvc_ioctl_g_input(struct file *file, void *fh, unsigned int *input)
899 {
900 	struct uvc_fh *handle = fh;
901 	struct uvc_video_chain *chain = handle->chain;
902 	u8 *buf;
903 	int ret;
904 
905 	if (chain->selector == NULL ||
906 	    (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
907 		*input = 0;
908 		return 0;
909 	}
910 
911 	buf = kmalloc(1, GFP_KERNEL);
912 	if (!buf)
913 		return -ENOMEM;
914 
915 	ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, chain->selector->id,
916 			     chain->dev->intfnum,  UVC_SU_INPUT_SELECT_CONTROL,
917 			     buf, 1);
918 	if (!ret)
919 		*input = *buf - 1;
920 
921 	kfree(buf);
922 
923 	return ret;
924 }
925 
926 static int uvc_ioctl_s_input(struct file *file, void *fh, unsigned int input)
927 {
928 	struct uvc_fh *handle = fh;
929 	struct uvc_video_chain *chain = handle->chain;
930 	u8 *buf;
931 	int ret;
932 
933 	ret = uvc_acquire_privileges(handle);
934 	if (ret < 0)
935 		return ret;
936 
937 	if (chain->selector == NULL ||
938 	    (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
939 		if (input)
940 			return -EINVAL;
941 		return 0;
942 	}
943 
944 	if (input >= chain->selector->bNrInPins)
945 		return -EINVAL;
946 
947 	buf = kmalloc(1, GFP_KERNEL);
948 	if (!buf)
949 		return -ENOMEM;
950 
951 	*buf = input + 1;
952 	ret = uvc_query_ctrl(chain->dev, UVC_SET_CUR, chain->selector->id,
953 			     chain->dev->intfnum, UVC_SU_INPUT_SELECT_CONTROL,
954 			     buf, 1);
955 	kfree(buf);
956 
957 	return ret;
958 }
959 
960 static int uvc_ioctl_queryctrl(struct file *file, void *fh,
961 			       struct v4l2_queryctrl *qc)
962 {
963 	struct uvc_fh *handle = fh;
964 	struct uvc_video_chain *chain = handle->chain;
965 
966 	return uvc_query_v4l2_ctrl(chain, qc);
967 }
968 
969 static int uvc_ioctl_query_ext_ctrl(struct file *file, void *fh,
970 				    struct v4l2_query_ext_ctrl *qec)
971 {
972 	struct uvc_fh *handle = fh;
973 	struct uvc_video_chain *chain = handle->chain;
974 	struct v4l2_queryctrl qc = { qec->id };
975 	int ret;
976 
977 	ret = uvc_query_v4l2_ctrl(chain, &qc);
978 	if (ret)
979 		return ret;
980 
981 	qec->id = qc.id;
982 	qec->type = qc.type;
983 	strscpy(qec->name, qc.name, sizeof(qec->name));
984 	qec->minimum = qc.minimum;
985 	qec->maximum = qc.maximum;
986 	qec->step = qc.step;
987 	qec->default_value = qc.default_value;
988 	qec->flags = qc.flags;
989 	qec->elem_size = 4;
990 	qec->elems = 1;
991 	qec->nr_of_dims = 0;
992 	memset(qec->dims, 0, sizeof(qec->dims));
993 	memset(qec->reserved, 0, sizeof(qec->reserved));
994 
995 	return 0;
996 }
997 
998 static int uvc_ioctl_g_ctrl(struct file *file, void *fh,
999 			    struct v4l2_control *ctrl)
1000 {
1001 	struct uvc_fh *handle = fh;
1002 	struct uvc_video_chain *chain = handle->chain;
1003 	struct v4l2_ext_control xctrl;
1004 	int ret;
1005 
1006 	memset(&xctrl, 0, sizeof(xctrl));
1007 	xctrl.id = ctrl->id;
1008 
1009 	ret = uvc_ctrl_begin(chain);
1010 	if (ret < 0)
1011 		return ret;
1012 
1013 	ret = uvc_ctrl_get(chain, &xctrl);
1014 	uvc_ctrl_rollback(handle);
1015 	if (ret < 0)
1016 		return ret;
1017 
1018 	ctrl->value = xctrl.value;
1019 	return 0;
1020 }
1021 
1022 static int uvc_ioctl_s_ctrl(struct file *file, void *fh,
1023 			    struct v4l2_control *ctrl)
1024 {
1025 	struct uvc_fh *handle = fh;
1026 	struct uvc_video_chain *chain = handle->chain;
1027 	struct v4l2_ext_control xctrl;
1028 	int ret;
1029 
1030 	memset(&xctrl, 0, sizeof(xctrl));
1031 	xctrl.id = ctrl->id;
1032 	xctrl.value = ctrl->value;
1033 
1034 	ret = uvc_ctrl_begin(chain);
1035 	if (ret < 0)
1036 		return ret;
1037 
1038 	ret = uvc_ctrl_set(handle, &xctrl);
1039 	if (ret < 0) {
1040 		uvc_ctrl_rollback(handle);
1041 		return ret;
1042 	}
1043 
1044 	ret = uvc_ctrl_commit(handle, &xctrl, 1);
1045 	if (ret < 0)
1046 		return ret;
1047 
1048 	ctrl->value = xctrl.value;
1049 	return 0;
1050 }
1051 
1052 static int uvc_ioctl_g_ext_ctrls(struct file *file, void *fh,
1053 				 struct v4l2_ext_controls *ctrls)
1054 {
1055 	struct uvc_fh *handle = fh;
1056 	struct uvc_video_chain *chain = handle->chain;
1057 	struct v4l2_ext_control *ctrl = ctrls->controls;
1058 	unsigned int i;
1059 	int ret;
1060 
1061 	if (ctrls->which == V4L2_CTRL_WHICH_DEF_VAL) {
1062 		for (i = 0; i < ctrls->count; ++ctrl, ++i) {
1063 			struct v4l2_queryctrl qc = { .id = ctrl->id };
1064 
1065 			ret = uvc_query_v4l2_ctrl(chain, &qc);
1066 			if (ret < 0) {
1067 				ctrls->error_idx = i;
1068 				return ret;
1069 			}
1070 
1071 			ctrl->value = qc.default_value;
1072 		}
1073 
1074 		return 0;
1075 	}
1076 
1077 	ret = uvc_ctrl_begin(chain);
1078 	if (ret < 0)
1079 		return ret;
1080 
1081 	for (i = 0; i < ctrls->count; ++ctrl, ++i) {
1082 		ret = uvc_ctrl_get(chain, ctrl);
1083 		if (ret < 0) {
1084 			uvc_ctrl_rollback(handle);
1085 			ctrls->error_idx = i;
1086 			return ret;
1087 		}
1088 	}
1089 
1090 	ctrls->error_idx = 0;
1091 
1092 	return uvc_ctrl_rollback(handle);
1093 }
1094 
1095 static int uvc_ioctl_s_try_ext_ctrls(struct uvc_fh *handle,
1096 				     struct v4l2_ext_controls *ctrls,
1097 				     bool commit)
1098 {
1099 	struct v4l2_ext_control *ctrl = ctrls->controls;
1100 	struct uvc_video_chain *chain = handle->chain;
1101 	unsigned int i;
1102 	int ret;
1103 
1104 	/* Default value cannot be changed */
1105 	if (ctrls->which == V4L2_CTRL_WHICH_DEF_VAL)
1106 		return -EINVAL;
1107 
1108 	ret = uvc_ctrl_begin(chain);
1109 	if (ret < 0)
1110 		return ret;
1111 
1112 	for (i = 0; i < ctrls->count; ++ctrl, ++i) {
1113 		ret = uvc_ctrl_set(handle, ctrl);
1114 		if (ret < 0) {
1115 			uvc_ctrl_rollback(handle);
1116 			ctrls->error_idx = commit ? ctrls->count : i;
1117 			return ret;
1118 		}
1119 	}
1120 
1121 	ctrls->error_idx = 0;
1122 
1123 	if (commit)
1124 		return uvc_ctrl_commit(handle, ctrls->controls, ctrls->count);
1125 	else
1126 		return uvc_ctrl_rollback(handle);
1127 }
1128 
1129 static int uvc_ioctl_s_ext_ctrls(struct file *file, void *fh,
1130 				 struct v4l2_ext_controls *ctrls)
1131 {
1132 	struct uvc_fh *handle = fh;
1133 
1134 	return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, true);
1135 }
1136 
1137 static int uvc_ioctl_try_ext_ctrls(struct file *file, void *fh,
1138 				   struct v4l2_ext_controls *ctrls)
1139 {
1140 	struct uvc_fh *handle = fh;
1141 
1142 	return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, false);
1143 }
1144 
1145 static int uvc_ioctl_querymenu(struct file *file, void *fh,
1146 			       struct v4l2_querymenu *qm)
1147 {
1148 	struct uvc_fh *handle = fh;
1149 	struct uvc_video_chain *chain = handle->chain;
1150 
1151 	return uvc_query_v4l2_menu(chain, qm);
1152 }
1153 
1154 static int uvc_ioctl_g_selection(struct file *file, void *fh,
1155 				 struct v4l2_selection *sel)
1156 {
1157 	struct uvc_fh *handle = fh;
1158 	struct uvc_streaming *stream = handle->stream;
1159 
1160 	if (sel->type != stream->type)
1161 		return -EINVAL;
1162 
1163 	switch (sel->target) {
1164 	case V4L2_SEL_TGT_CROP_DEFAULT:
1165 	case V4L2_SEL_TGT_CROP_BOUNDS:
1166 		if (stream->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1167 			return -EINVAL;
1168 		break;
1169 	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1170 	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1171 		if (stream->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1172 			return -EINVAL;
1173 		break;
1174 	default:
1175 		return -EINVAL;
1176 	}
1177 
1178 	sel->r.left = 0;
1179 	sel->r.top = 0;
1180 	mutex_lock(&stream->mutex);
1181 	sel->r.width = stream->cur_frame->wWidth;
1182 	sel->r.height = stream->cur_frame->wHeight;
1183 	mutex_unlock(&stream->mutex);
1184 
1185 	return 0;
1186 }
1187 
1188 static int uvc_ioctl_g_parm(struct file *file, void *fh,
1189 			    struct v4l2_streamparm *parm)
1190 {
1191 	struct uvc_fh *handle = fh;
1192 	struct uvc_streaming *stream = handle->stream;
1193 
1194 	return uvc_v4l2_get_streamparm(stream, parm);
1195 }
1196 
1197 static int uvc_ioctl_s_parm(struct file *file, void *fh,
1198 			    struct v4l2_streamparm *parm)
1199 {
1200 	struct uvc_fh *handle = fh;
1201 	struct uvc_streaming *stream = handle->stream;
1202 	int ret;
1203 
1204 	ret = uvc_acquire_privileges(handle);
1205 	if (ret < 0)
1206 		return ret;
1207 
1208 	return uvc_v4l2_set_streamparm(stream, parm);
1209 }
1210 
1211 static int uvc_ioctl_enum_framesizes(struct file *file, void *fh,
1212 				     struct v4l2_frmsizeenum *fsize)
1213 {
1214 	struct uvc_fh *handle = fh;
1215 	struct uvc_streaming *stream = handle->stream;
1216 	struct uvc_format *format = NULL;
1217 	struct uvc_frame *frame = NULL;
1218 	unsigned int index;
1219 	unsigned int i;
1220 
1221 	/* Look for the given pixel format */
1222 	for (i = 0; i < stream->nformats; i++) {
1223 		if (stream->format[i].fcc == fsize->pixel_format) {
1224 			format = &stream->format[i];
1225 			break;
1226 		}
1227 	}
1228 	if (format == NULL)
1229 		return -EINVAL;
1230 
1231 	/* Skip duplicate frame sizes */
1232 	for (i = 0, index = 0; i < format->nframes; i++) {
1233 		if (frame && frame->wWidth == format->frame[i].wWidth &&
1234 		    frame->wHeight == format->frame[i].wHeight)
1235 			continue;
1236 		frame = &format->frame[i];
1237 		if (index == fsize->index)
1238 			break;
1239 		index++;
1240 	}
1241 
1242 	if (i == format->nframes)
1243 		return -EINVAL;
1244 
1245 	fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1246 	fsize->discrete.width = frame->wWidth;
1247 	fsize->discrete.height = frame->wHeight;
1248 	return 0;
1249 }
1250 
1251 static int uvc_ioctl_enum_frameintervals(struct file *file, void *fh,
1252 					 struct v4l2_frmivalenum *fival)
1253 {
1254 	struct uvc_fh *handle = fh;
1255 	struct uvc_streaming *stream = handle->stream;
1256 	struct uvc_format *format = NULL;
1257 	struct uvc_frame *frame = NULL;
1258 	unsigned int nintervals;
1259 	unsigned int index;
1260 	unsigned int i;
1261 
1262 	/* Look for the given pixel format and frame size */
1263 	for (i = 0; i < stream->nformats; i++) {
1264 		if (stream->format[i].fcc == fival->pixel_format) {
1265 			format = &stream->format[i];
1266 			break;
1267 		}
1268 	}
1269 	if (format == NULL)
1270 		return -EINVAL;
1271 
1272 	index = fival->index;
1273 	for (i = 0; i < format->nframes; i++) {
1274 		if (format->frame[i].wWidth == fival->width &&
1275 		    format->frame[i].wHeight == fival->height) {
1276 			frame = &format->frame[i];
1277 			nintervals = frame->bFrameIntervalType ?: 1;
1278 			if (index < nintervals)
1279 				break;
1280 			index -= nintervals;
1281 		}
1282 	}
1283 	if (i == format->nframes)
1284 		return -EINVAL;
1285 
1286 	if (frame->bFrameIntervalType) {
1287 		fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1288 		fival->discrete.numerator =
1289 			frame->dwFrameInterval[index];
1290 		fival->discrete.denominator = 10000000;
1291 		uvc_simplify_fraction(&fival->discrete.numerator,
1292 			&fival->discrete.denominator, 8, 333);
1293 	} else {
1294 		fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
1295 		fival->stepwise.min.numerator = frame->dwFrameInterval[0];
1296 		fival->stepwise.min.denominator = 10000000;
1297 		fival->stepwise.max.numerator = frame->dwFrameInterval[1];
1298 		fival->stepwise.max.denominator = 10000000;
1299 		fival->stepwise.step.numerator = frame->dwFrameInterval[2];
1300 		fival->stepwise.step.denominator = 10000000;
1301 		uvc_simplify_fraction(&fival->stepwise.min.numerator,
1302 			&fival->stepwise.min.denominator, 8, 333);
1303 		uvc_simplify_fraction(&fival->stepwise.max.numerator,
1304 			&fival->stepwise.max.denominator, 8, 333);
1305 		uvc_simplify_fraction(&fival->stepwise.step.numerator,
1306 			&fival->stepwise.step.denominator, 8, 333);
1307 	}
1308 
1309 	return 0;
1310 }
1311 
1312 static int uvc_ioctl_subscribe_event(struct v4l2_fh *fh,
1313 				     const struct v4l2_event_subscription *sub)
1314 {
1315 	switch (sub->type) {
1316 	case V4L2_EVENT_CTRL:
1317 		return v4l2_event_subscribe(fh, sub, 0, &uvc_ctrl_sub_ev_ops);
1318 	default:
1319 		return -EINVAL;
1320 	}
1321 }
1322 
1323 static long uvc_ioctl_default(struct file *file, void *fh, bool valid_prio,
1324 			      unsigned int cmd, void *arg)
1325 {
1326 	struct uvc_fh *handle = fh;
1327 	struct uvc_video_chain *chain = handle->chain;
1328 
1329 	switch (cmd) {
1330 	/* Dynamic controls. */
1331 	case UVCIOC_CTRL_MAP:
1332 		return uvc_ioctl_ctrl_map(chain, arg);
1333 
1334 	case UVCIOC_CTRL_QUERY:
1335 		return uvc_xu_ctrl_query(chain, arg);
1336 
1337 	default:
1338 		return -ENOTTY;
1339 	}
1340 }
1341 
1342 #ifdef CONFIG_COMPAT
1343 struct uvc_xu_control_mapping32 {
1344 	u32 id;
1345 	u8 name[32];
1346 	u8 entity[16];
1347 	u8 selector;
1348 
1349 	u8 size;
1350 	u8 offset;
1351 	u32 v4l2_type;
1352 	u32 data_type;
1353 
1354 	compat_caddr_t menu_info;
1355 	u32 menu_count;
1356 
1357 	u32 reserved[4];
1358 };
1359 
1360 static int uvc_v4l2_get_xu_mapping(struct uvc_xu_control_mapping *kp,
1361 			const struct uvc_xu_control_mapping32 __user *up)
1362 {
1363 	struct uvc_xu_control_mapping32 *p = (void *)kp;
1364 	compat_caddr_t info;
1365 	u32 count;
1366 
1367 	if (copy_from_user(p, up, sizeof(*p)))
1368 		return -EFAULT;
1369 
1370 	count = p->menu_count;
1371 	info = p->menu_info;
1372 
1373 	memset(kp->reserved, 0, sizeof(kp->reserved));
1374 	kp->menu_info = count ? compat_ptr(info) : NULL;
1375 	kp->menu_count = count;
1376 	return 0;
1377 }
1378 
1379 static int uvc_v4l2_put_xu_mapping(const struct uvc_xu_control_mapping *kp,
1380 			struct uvc_xu_control_mapping32 __user *up)
1381 {
1382 	if (copy_to_user(up, kp, offsetof(typeof(*up), menu_info)) ||
1383 	    put_user(kp->menu_count, &up->menu_count))
1384 		return -EFAULT;
1385 
1386 	if (clear_user(up->reserved, sizeof(up->reserved)))
1387 		return -EFAULT;
1388 
1389 	return 0;
1390 }
1391 
1392 struct uvc_xu_control_query32 {
1393 	u8 unit;
1394 	u8 selector;
1395 	u8 query;
1396 	u16 size;
1397 	compat_caddr_t data;
1398 };
1399 
1400 static int uvc_v4l2_get_xu_query(struct uvc_xu_control_query *kp,
1401 			const struct uvc_xu_control_query32 __user *up)
1402 {
1403 	struct uvc_xu_control_query32 v;
1404 
1405 	if (copy_from_user(&v, up, sizeof(v)))
1406 		return -EFAULT;
1407 
1408 	*kp = (struct uvc_xu_control_query){
1409 		.unit = v.unit,
1410 		.selector = v.selector,
1411 		.query = v.query,
1412 		.size = v.size,
1413 		.data = v.size ? compat_ptr(v.data) : NULL
1414 	};
1415 	return 0;
1416 }
1417 
1418 static int uvc_v4l2_put_xu_query(const struct uvc_xu_control_query *kp,
1419 			struct uvc_xu_control_query32 __user *up)
1420 {
1421 	if (copy_to_user(up, kp, offsetof(typeof(*up), data)))
1422 		return -EFAULT;
1423 	return 0;
1424 }
1425 
1426 #define UVCIOC_CTRL_MAP32	_IOWR('u', 0x20, struct uvc_xu_control_mapping32)
1427 #define UVCIOC_CTRL_QUERY32	_IOWR('u', 0x21, struct uvc_xu_control_query32)
1428 
1429 static long uvc_v4l2_compat_ioctl32(struct file *file,
1430 		     unsigned int cmd, unsigned long arg)
1431 {
1432 	struct uvc_fh *handle = file->private_data;
1433 	union {
1434 		struct uvc_xu_control_mapping xmap;
1435 		struct uvc_xu_control_query xqry;
1436 	} karg;
1437 	void __user *up = compat_ptr(arg);
1438 	long ret;
1439 
1440 	switch (cmd) {
1441 	case UVCIOC_CTRL_MAP32:
1442 		ret = uvc_v4l2_get_xu_mapping(&karg.xmap, up);
1443 		if (ret)
1444 			return ret;
1445 		ret = uvc_ioctl_ctrl_map(handle->chain, &karg.xmap);
1446 		if (ret)
1447 			return ret;
1448 		ret = uvc_v4l2_put_xu_mapping(&karg.xmap, up);
1449 		if (ret)
1450 			return ret;
1451 
1452 		break;
1453 
1454 	case UVCIOC_CTRL_QUERY32:
1455 		ret = uvc_v4l2_get_xu_query(&karg.xqry, up);
1456 		if (ret)
1457 			return ret;
1458 		ret = uvc_xu_ctrl_query(handle->chain, &karg.xqry);
1459 		if (ret)
1460 			return ret;
1461 		ret = uvc_v4l2_put_xu_query(&karg.xqry, up);
1462 		if (ret)
1463 			return ret;
1464 		break;
1465 
1466 	default:
1467 		return -ENOIOCTLCMD;
1468 	}
1469 
1470 	return ret;
1471 }
1472 #endif
1473 
1474 static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1475 		    size_t count, loff_t *ppos)
1476 {
1477 	struct uvc_fh *handle = file->private_data;
1478 	struct uvc_streaming *stream = handle->stream;
1479 
1480 	uvc_dbg(stream->dev, CALLS, "%s: not implemented\n", __func__);
1481 	return -EINVAL;
1482 }
1483 
1484 static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1485 {
1486 	struct uvc_fh *handle = file->private_data;
1487 	struct uvc_streaming *stream = handle->stream;
1488 
1489 	uvc_dbg(stream->dev, CALLS, "%s\n", __func__);
1490 
1491 	return uvc_queue_mmap(&stream->queue, vma);
1492 }
1493 
1494 static __poll_t uvc_v4l2_poll(struct file *file, poll_table *wait)
1495 {
1496 	struct uvc_fh *handle = file->private_data;
1497 	struct uvc_streaming *stream = handle->stream;
1498 
1499 	uvc_dbg(stream->dev, CALLS, "%s\n", __func__);
1500 
1501 	return uvc_queue_poll(&stream->queue, file, wait);
1502 }
1503 
1504 #ifndef CONFIG_MMU
1505 static unsigned long uvc_v4l2_get_unmapped_area(struct file *file,
1506 		unsigned long addr, unsigned long len, unsigned long pgoff,
1507 		unsigned long flags)
1508 {
1509 	struct uvc_fh *handle = file->private_data;
1510 	struct uvc_streaming *stream = handle->stream;
1511 
1512 	uvc_dbg(stream->dev, CALLS, "%s\n", __func__);
1513 
1514 	return uvc_queue_get_unmapped_area(&stream->queue, pgoff);
1515 }
1516 #endif
1517 
1518 const struct v4l2_ioctl_ops uvc_ioctl_ops = {
1519 	.vidioc_querycap = uvc_ioctl_querycap,
1520 	.vidioc_enum_fmt_vid_cap = uvc_ioctl_enum_fmt_vid_cap,
1521 	.vidioc_enum_fmt_vid_out = uvc_ioctl_enum_fmt_vid_out,
1522 	.vidioc_g_fmt_vid_cap = uvc_ioctl_g_fmt_vid_cap,
1523 	.vidioc_g_fmt_vid_out = uvc_ioctl_g_fmt_vid_out,
1524 	.vidioc_s_fmt_vid_cap = uvc_ioctl_s_fmt_vid_cap,
1525 	.vidioc_s_fmt_vid_out = uvc_ioctl_s_fmt_vid_out,
1526 	.vidioc_try_fmt_vid_cap = uvc_ioctl_try_fmt_vid_cap,
1527 	.vidioc_try_fmt_vid_out = uvc_ioctl_try_fmt_vid_out,
1528 	.vidioc_reqbufs = uvc_ioctl_reqbufs,
1529 	.vidioc_querybuf = uvc_ioctl_querybuf,
1530 	.vidioc_qbuf = uvc_ioctl_qbuf,
1531 	.vidioc_expbuf = uvc_ioctl_expbuf,
1532 	.vidioc_dqbuf = uvc_ioctl_dqbuf,
1533 	.vidioc_create_bufs = uvc_ioctl_create_bufs,
1534 	.vidioc_streamon = uvc_ioctl_streamon,
1535 	.vidioc_streamoff = uvc_ioctl_streamoff,
1536 	.vidioc_enum_input = uvc_ioctl_enum_input,
1537 	.vidioc_g_input = uvc_ioctl_g_input,
1538 	.vidioc_s_input = uvc_ioctl_s_input,
1539 	.vidioc_queryctrl = uvc_ioctl_queryctrl,
1540 	.vidioc_query_ext_ctrl = uvc_ioctl_query_ext_ctrl,
1541 	.vidioc_g_ctrl = uvc_ioctl_g_ctrl,
1542 	.vidioc_s_ctrl = uvc_ioctl_s_ctrl,
1543 	.vidioc_g_ext_ctrls = uvc_ioctl_g_ext_ctrls,
1544 	.vidioc_s_ext_ctrls = uvc_ioctl_s_ext_ctrls,
1545 	.vidioc_try_ext_ctrls = uvc_ioctl_try_ext_ctrls,
1546 	.vidioc_querymenu = uvc_ioctl_querymenu,
1547 	.vidioc_g_selection = uvc_ioctl_g_selection,
1548 	.vidioc_g_parm = uvc_ioctl_g_parm,
1549 	.vidioc_s_parm = uvc_ioctl_s_parm,
1550 	.vidioc_enum_framesizes = uvc_ioctl_enum_framesizes,
1551 	.vidioc_enum_frameintervals = uvc_ioctl_enum_frameintervals,
1552 	.vidioc_subscribe_event = uvc_ioctl_subscribe_event,
1553 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1554 	.vidioc_default = uvc_ioctl_default,
1555 };
1556 
1557 const struct v4l2_file_operations uvc_fops = {
1558 	.owner		= THIS_MODULE,
1559 	.open		= uvc_v4l2_open,
1560 	.release	= uvc_v4l2_release,
1561 	.unlocked_ioctl	= video_ioctl2,
1562 #ifdef CONFIG_COMPAT
1563 	.compat_ioctl32	= uvc_v4l2_compat_ioctl32,
1564 #endif
1565 	.read		= uvc_v4l2_read,
1566 	.mmap		= uvc_v4l2_mmap,
1567 	.poll		= uvc_v4l2_poll,
1568 #ifndef CONFIG_MMU
1569 	.get_unmapped_area = uvc_v4l2_get_unmapped_area,
1570 #endif
1571 };
1572 
1573