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