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