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