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