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