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