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