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