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