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_queryctrl(struct file * file,void * fh,struct v4l2_queryctrl * qc)1035 static int uvc_ioctl_queryctrl(struct file *file, void *fh,
1036 struct v4l2_queryctrl *qc)
1037 {
1038 struct uvc_fh *handle = fh;
1039 struct uvc_video_chain *chain = handle->chain;
1040
1041 return uvc_query_v4l2_ctrl(chain, qc);
1042 }
1043
uvc_ioctl_query_ext_ctrl(struct file * file,void * fh,struct v4l2_query_ext_ctrl * qec)1044 static int uvc_ioctl_query_ext_ctrl(struct file *file, void *fh,
1045 struct v4l2_query_ext_ctrl *qec)
1046 {
1047 struct uvc_fh *handle = fh;
1048 struct uvc_video_chain *chain = handle->chain;
1049 struct v4l2_queryctrl qc = { qec->id };
1050 int ret;
1051
1052 ret = uvc_query_v4l2_ctrl(chain, &qc);
1053 if (ret)
1054 return ret;
1055
1056 qec->id = qc.id;
1057 qec->type = qc.type;
1058 strscpy(qec->name, qc.name, sizeof(qec->name));
1059 qec->minimum = qc.minimum;
1060 qec->maximum = qc.maximum;
1061 qec->step = qc.step;
1062 qec->default_value = qc.default_value;
1063 qec->flags = qc.flags;
1064 qec->elem_size = 4;
1065 qec->elems = 1;
1066 qec->nr_of_dims = 0;
1067 memset(qec->dims, 0, sizeof(qec->dims));
1068 memset(qec->reserved, 0, sizeof(qec->reserved));
1069
1070 return 0;
1071 }
1072
uvc_ctrl_check_access(struct uvc_video_chain * chain,struct v4l2_ext_controls * ctrls,unsigned long ioctl)1073 static int uvc_ctrl_check_access(struct uvc_video_chain *chain,
1074 struct v4l2_ext_controls *ctrls,
1075 unsigned long ioctl)
1076 {
1077 struct v4l2_ext_control *ctrl = ctrls->controls;
1078 unsigned int i;
1079 int ret = 0;
1080
1081 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
1082 ret = uvc_ctrl_is_accessible(chain, ctrl->id, ctrls, ioctl);
1083 if (ret)
1084 break;
1085 }
1086
1087 ctrls->error_idx = ioctl == VIDIOC_TRY_EXT_CTRLS ? i : ctrls->count;
1088
1089 return ret;
1090 }
1091
uvc_ioctl_g_ext_ctrls(struct file * file,void * fh,struct v4l2_ext_controls * ctrls)1092 static int uvc_ioctl_g_ext_ctrls(struct file *file, void *fh,
1093 struct v4l2_ext_controls *ctrls)
1094 {
1095 struct uvc_fh *handle = fh;
1096 struct uvc_video_chain *chain = handle->chain;
1097 struct v4l2_ext_control *ctrl = ctrls->controls;
1098 unsigned int i;
1099 int ret;
1100
1101 ret = uvc_ctrl_check_access(chain, ctrls, VIDIOC_G_EXT_CTRLS);
1102 if (ret < 0)
1103 return ret;
1104
1105 if (ctrls->which == V4L2_CTRL_WHICH_DEF_VAL) {
1106 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
1107 struct v4l2_queryctrl qc = { .id = ctrl->id };
1108
1109 ret = uvc_query_v4l2_ctrl(chain, &qc);
1110 if (ret < 0) {
1111 ctrls->error_idx = i;
1112 return ret;
1113 }
1114
1115 ctrl->value = qc.default_value;
1116 }
1117
1118 return 0;
1119 }
1120
1121 ret = uvc_ctrl_begin(chain);
1122 if (ret < 0)
1123 return ret;
1124
1125 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
1126 ret = uvc_ctrl_get(chain, ctrl);
1127 if (ret < 0) {
1128 uvc_ctrl_rollback(handle);
1129 ctrls->error_idx = i;
1130 return ret;
1131 }
1132 }
1133
1134 ctrls->error_idx = 0;
1135
1136 return uvc_ctrl_rollback(handle);
1137 }
1138
uvc_ioctl_s_try_ext_ctrls(struct uvc_fh * handle,struct v4l2_ext_controls * ctrls,unsigned long ioctl)1139 static int uvc_ioctl_s_try_ext_ctrls(struct uvc_fh *handle,
1140 struct v4l2_ext_controls *ctrls,
1141 unsigned long ioctl)
1142 {
1143 struct v4l2_ext_control *ctrl = ctrls->controls;
1144 struct uvc_video_chain *chain = handle->chain;
1145 unsigned int i;
1146 int ret;
1147
1148 ret = uvc_ctrl_check_access(chain, ctrls, ioctl);
1149 if (ret < 0)
1150 return ret;
1151
1152 ret = uvc_ctrl_begin(chain);
1153 if (ret < 0)
1154 return ret;
1155
1156 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
1157 ret = uvc_ctrl_set(handle, ctrl);
1158 if (ret < 0) {
1159 uvc_ctrl_rollback(handle);
1160 ctrls->error_idx = ioctl == VIDIOC_S_EXT_CTRLS ?
1161 ctrls->count : i;
1162 return ret;
1163 }
1164 }
1165
1166 ctrls->error_idx = 0;
1167
1168 if (ioctl == VIDIOC_S_EXT_CTRLS)
1169 return uvc_ctrl_commit(handle, ctrls);
1170 else
1171 return uvc_ctrl_rollback(handle);
1172 }
1173
uvc_ioctl_s_ext_ctrls(struct file * file,void * fh,struct v4l2_ext_controls * ctrls)1174 static int uvc_ioctl_s_ext_ctrls(struct file *file, void *fh,
1175 struct v4l2_ext_controls *ctrls)
1176 {
1177 struct uvc_fh *handle = fh;
1178
1179 return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, VIDIOC_S_EXT_CTRLS);
1180 }
1181
uvc_ioctl_try_ext_ctrls(struct file * file,void * fh,struct v4l2_ext_controls * ctrls)1182 static int uvc_ioctl_try_ext_ctrls(struct file *file, void *fh,
1183 struct v4l2_ext_controls *ctrls)
1184 {
1185 struct uvc_fh *handle = fh;
1186
1187 return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, VIDIOC_TRY_EXT_CTRLS);
1188 }
1189
uvc_ioctl_querymenu(struct file * file,void * fh,struct v4l2_querymenu * qm)1190 static int uvc_ioctl_querymenu(struct file *file, void *fh,
1191 struct v4l2_querymenu *qm)
1192 {
1193 struct uvc_fh *handle = fh;
1194 struct uvc_video_chain *chain = handle->chain;
1195
1196 return uvc_query_v4l2_menu(chain, qm);
1197 }
1198
uvc_ioctl_g_selection(struct file * file,void * fh,struct v4l2_selection * sel)1199 static int uvc_ioctl_g_selection(struct file *file, void *fh,
1200 struct v4l2_selection *sel)
1201 {
1202 struct uvc_fh *handle = fh;
1203 struct uvc_streaming *stream = handle->stream;
1204
1205 if (sel->type != stream->type)
1206 return -EINVAL;
1207
1208 switch (sel->target) {
1209 case V4L2_SEL_TGT_CROP_DEFAULT:
1210 case V4L2_SEL_TGT_CROP_BOUNDS:
1211 if (stream->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1212 return -EINVAL;
1213 break;
1214 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1215 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1216 if (stream->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1217 return -EINVAL;
1218 break;
1219 default:
1220 return -EINVAL;
1221 }
1222
1223 sel->r.left = 0;
1224 sel->r.top = 0;
1225 mutex_lock(&stream->mutex);
1226 sel->r.width = stream->cur_frame->wWidth;
1227 sel->r.height = stream->cur_frame->wHeight;
1228 mutex_unlock(&stream->mutex);
1229
1230 return 0;
1231 }
1232
uvc_ioctl_g_parm(struct file * file,void * fh,struct v4l2_streamparm * parm)1233 static int uvc_ioctl_g_parm(struct file *file, void *fh,
1234 struct v4l2_streamparm *parm)
1235 {
1236 struct uvc_fh *handle = fh;
1237 struct uvc_streaming *stream = handle->stream;
1238
1239 return uvc_v4l2_get_streamparm(stream, parm);
1240 }
1241
uvc_ioctl_s_parm(struct file * file,void * fh,struct v4l2_streamparm * parm)1242 static int uvc_ioctl_s_parm(struct file *file, void *fh,
1243 struct v4l2_streamparm *parm)
1244 {
1245 struct uvc_fh *handle = fh;
1246 struct uvc_streaming *stream = handle->stream;
1247 int ret;
1248
1249 ret = uvc_acquire_privileges(handle);
1250 if (ret < 0)
1251 return ret;
1252
1253 return uvc_v4l2_set_streamparm(stream, parm);
1254 }
1255
uvc_ioctl_enum_framesizes(struct file * file,void * fh,struct v4l2_frmsizeenum * fsize)1256 static int uvc_ioctl_enum_framesizes(struct file *file, void *fh,
1257 struct v4l2_frmsizeenum *fsize)
1258 {
1259 struct uvc_fh *handle = fh;
1260 struct uvc_streaming *stream = handle->stream;
1261 const struct uvc_format *format = NULL;
1262 const struct uvc_frame *frame = NULL;
1263 unsigned int index;
1264 unsigned int i;
1265
1266 /* Look for the given pixel format */
1267 for (i = 0; i < stream->nformats; i++) {
1268 if (stream->formats[i].fcc == fsize->pixel_format) {
1269 format = &stream->formats[i];
1270 break;
1271 }
1272 }
1273 if (format == NULL)
1274 return -EINVAL;
1275
1276 /* Skip duplicate frame sizes */
1277 for (i = 0, index = 0; i < format->nframes; i++) {
1278 if (frame && frame->wWidth == format->frames[i].wWidth &&
1279 frame->wHeight == format->frames[i].wHeight)
1280 continue;
1281 frame = &format->frames[i];
1282 if (index == fsize->index)
1283 break;
1284 index++;
1285 }
1286
1287 if (i == format->nframes)
1288 return -EINVAL;
1289
1290 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1291 fsize->discrete.width = frame->wWidth;
1292 fsize->discrete.height = frame->wHeight;
1293 return 0;
1294 }
1295
uvc_ioctl_enum_frameintervals(struct file * file,void * fh,struct v4l2_frmivalenum * fival)1296 static int uvc_ioctl_enum_frameintervals(struct file *file, void *fh,
1297 struct v4l2_frmivalenum *fival)
1298 {
1299 struct uvc_fh *handle = fh;
1300 struct uvc_streaming *stream = handle->stream;
1301 const struct uvc_format *format = NULL;
1302 const struct uvc_frame *frame = NULL;
1303 unsigned int nintervals;
1304 unsigned int index;
1305 unsigned int i;
1306
1307 /* Look for the given pixel format and frame size */
1308 for (i = 0; i < stream->nformats; i++) {
1309 if (stream->formats[i].fcc == fival->pixel_format) {
1310 format = &stream->formats[i];
1311 break;
1312 }
1313 }
1314 if (format == NULL)
1315 return -EINVAL;
1316
1317 index = fival->index;
1318 for (i = 0; i < format->nframes; i++) {
1319 if (format->frames[i].wWidth == fival->width &&
1320 format->frames[i].wHeight == fival->height) {
1321 frame = &format->frames[i];
1322 nintervals = frame->bFrameIntervalType ?: 1;
1323 if (index < nintervals)
1324 break;
1325 index -= nintervals;
1326 }
1327 }
1328 if (i == format->nframes)
1329 return -EINVAL;
1330
1331 if (frame->bFrameIntervalType) {
1332 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1333 fival->discrete.numerator =
1334 frame->dwFrameInterval[index];
1335 fival->discrete.denominator = 10000000;
1336 v4l2_simplify_fraction(&fival->discrete.numerator,
1337 &fival->discrete.denominator, 8, 333);
1338 } else {
1339 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
1340 fival->stepwise.min.numerator = frame->dwFrameInterval[0];
1341 fival->stepwise.min.denominator = 10000000;
1342 fival->stepwise.max.numerator = frame->dwFrameInterval[1];
1343 fival->stepwise.max.denominator = 10000000;
1344 fival->stepwise.step.numerator = frame->dwFrameInterval[2];
1345 fival->stepwise.step.denominator = 10000000;
1346 v4l2_simplify_fraction(&fival->stepwise.min.numerator,
1347 &fival->stepwise.min.denominator, 8, 333);
1348 v4l2_simplify_fraction(&fival->stepwise.max.numerator,
1349 &fival->stepwise.max.denominator, 8, 333);
1350 v4l2_simplify_fraction(&fival->stepwise.step.numerator,
1351 &fival->stepwise.step.denominator, 8, 333);
1352 }
1353
1354 return 0;
1355 }
1356
uvc_ioctl_subscribe_event(struct v4l2_fh * fh,const struct v4l2_event_subscription * sub)1357 static int uvc_ioctl_subscribe_event(struct v4l2_fh *fh,
1358 const struct v4l2_event_subscription *sub)
1359 {
1360 switch (sub->type) {
1361 case V4L2_EVENT_CTRL:
1362 return v4l2_event_subscribe(fh, sub, 0, &uvc_ctrl_sub_ev_ops);
1363 default:
1364 return -EINVAL;
1365 }
1366 }
1367
uvc_ioctl_default(struct file * file,void * fh,bool valid_prio,unsigned int cmd,void * arg)1368 static long uvc_ioctl_default(struct file *file, void *fh, bool valid_prio,
1369 unsigned int cmd, void *arg)
1370 {
1371 struct uvc_fh *handle = fh;
1372 struct uvc_video_chain *chain = handle->chain;
1373
1374 switch (cmd) {
1375 /* Dynamic controls. */
1376 case UVCIOC_CTRL_MAP:
1377 return uvc_ioctl_xu_ctrl_map(chain, arg);
1378
1379 case UVCIOC_CTRL_QUERY:
1380 return uvc_xu_ctrl_query(chain, arg);
1381
1382 default:
1383 return -ENOTTY;
1384 }
1385 }
1386
1387 #ifdef CONFIG_COMPAT
1388 struct uvc_xu_control_mapping32 {
1389 u32 id;
1390 u8 name[32];
1391 u8 entity[16];
1392 u8 selector;
1393
1394 u8 size;
1395 u8 offset;
1396 u32 v4l2_type;
1397 u32 data_type;
1398
1399 compat_caddr_t menu_info;
1400 u32 menu_count;
1401
1402 u32 reserved[4];
1403 };
1404
uvc_v4l2_get_xu_mapping(struct uvc_xu_control_mapping * kp,const struct uvc_xu_control_mapping32 __user * up)1405 static int uvc_v4l2_get_xu_mapping(struct uvc_xu_control_mapping *kp,
1406 const struct uvc_xu_control_mapping32 __user *up)
1407 {
1408 struct uvc_xu_control_mapping32 *p = (void *)kp;
1409 compat_caddr_t info;
1410 u32 count;
1411
1412 if (copy_from_user(p, up, sizeof(*p)))
1413 return -EFAULT;
1414
1415 count = p->menu_count;
1416 info = p->menu_info;
1417
1418 memset(kp->reserved, 0, sizeof(kp->reserved));
1419 kp->menu_info = count ? compat_ptr(info) : NULL;
1420 kp->menu_count = count;
1421 return 0;
1422 }
1423
uvc_v4l2_put_xu_mapping(const struct uvc_xu_control_mapping * kp,struct uvc_xu_control_mapping32 __user * up)1424 static int uvc_v4l2_put_xu_mapping(const struct uvc_xu_control_mapping *kp,
1425 struct uvc_xu_control_mapping32 __user *up)
1426 {
1427 if (copy_to_user(up, kp, offsetof(typeof(*up), menu_info)) ||
1428 put_user(kp->menu_count, &up->menu_count))
1429 return -EFAULT;
1430
1431 if (clear_user(up->reserved, sizeof(up->reserved)))
1432 return -EFAULT;
1433
1434 return 0;
1435 }
1436
1437 struct uvc_xu_control_query32 {
1438 u8 unit;
1439 u8 selector;
1440 u8 query;
1441 u16 size;
1442 compat_caddr_t data;
1443 };
1444
uvc_v4l2_get_xu_query(struct uvc_xu_control_query * kp,const struct uvc_xu_control_query32 __user * up)1445 static int uvc_v4l2_get_xu_query(struct uvc_xu_control_query *kp,
1446 const struct uvc_xu_control_query32 __user *up)
1447 {
1448 struct uvc_xu_control_query32 v;
1449
1450 if (copy_from_user(&v, up, sizeof(v)))
1451 return -EFAULT;
1452
1453 *kp = (struct uvc_xu_control_query){
1454 .unit = v.unit,
1455 .selector = v.selector,
1456 .query = v.query,
1457 .size = v.size,
1458 .data = v.size ? compat_ptr(v.data) : NULL
1459 };
1460 return 0;
1461 }
1462
uvc_v4l2_put_xu_query(const struct uvc_xu_control_query * kp,struct uvc_xu_control_query32 __user * up)1463 static int uvc_v4l2_put_xu_query(const struct uvc_xu_control_query *kp,
1464 struct uvc_xu_control_query32 __user *up)
1465 {
1466 if (copy_to_user(up, kp, offsetof(typeof(*up), data)))
1467 return -EFAULT;
1468 return 0;
1469 }
1470
1471 #define UVCIOC_CTRL_MAP32 _IOWR('u', 0x20, struct uvc_xu_control_mapping32)
1472 #define UVCIOC_CTRL_QUERY32 _IOWR('u', 0x21, struct uvc_xu_control_query32)
1473
uvc_v4l2_compat_ioctl32(struct file * file,unsigned int cmd,unsigned long arg)1474 static long uvc_v4l2_compat_ioctl32(struct file *file,
1475 unsigned int cmd, unsigned long arg)
1476 {
1477 struct uvc_fh *handle = file->private_data;
1478 union {
1479 struct uvc_xu_control_mapping xmap;
1480 struct uvc_xu_control_query xqry;
1481 } karg;
1482 void __user *up = compat_ptr(arg);
1483 long ret;
1484
1485 switch (cmd) {
1486 case UVCIOC_CTRL_MAP32:
1487 ret = uvc_v4l2_get_xu_mapping(&karg.xmap, up);
1488 if (ret)
1489 return ret;
1490 ret = uvc_ioctl_xu_ctrl_map(handle->chain, &karg.xmap);
1491 if (ret)
1492 return ret;
1493 ret = uvc_v4l2_put_xu_mapping(&karg.xmap, up);
1494 if (ret)
1495 return ret;
1496
1497 break;
1498
1499 case UVCIOC_CTRL_QUERY32:
1500 ret = uvc_v4l2_get_xu_query(&karg.xqry, up);
1501 if (ret)
1502 return ret;
1503 ret = uvc_xu_ctrl_query(handle->chain, &karg.xqry);
1504 if (ret)
1505 return ret;
1506 ret = uvc_v4l2_put_xu_query(&karg.xqry, up);
1507 if (ret)
1508 return ret;
1509 break;
1510
1511 default:
1512 return -ENOIOCTLCMD;
1513 }
1514
1515 return ret;
1516 }
1517 #endif
1518
uvc_v4l2_read(struct file * file,char __user * data,size_t count,loff_t * ppos)1519 static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1520 size_t count, loff_t *ppos)
1521 {
1522 struct uvc_fh *handle = file->private_data;
1523 struct uvc_streaming *stream = handle->stream;
1524
1525 uvc_dbg(stream->dev, CALLS, "%s: not implemented\n", __func__);
1526 return -EINVAL;
1527 }
1528
uvc_v4l2_mmap(struct file * file,struct vm_area_struct * vma)1529 static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1530 {
1531 struct uvc_fh *handle = file->private_data;
1532 struct uvc_streaming *stream = handle->stream;
1533
1534 uvc_dbg(stream->dev, CALLS, "%s\n", __func__);
1535
1536 return uvc_queue_mmap(&stream->queue, vma);
1537 }
1538
uvc_v4l2_poll(struct file * file,poll_table * wait)1539 static __poll_t uvc_v4l2_poll(struct file *file, poll_table *wait)
1540 {
1541 struct uvc_fh *handle = file->private_data;
1542 struct uvc_streaming *stream = handle->stream;
1543
1544 uvc_dbg(stream->dev, CALLS, "%s\n", __func__);
1545
1546 return uvc_queue_poll(&stream->queue, file, wait);
1547 }
1548
1549 #ifndef CONFIG_MMU
uvc_v4l2_get_unmapped_area(struct file * file,unsigned long addr,unsigned long len,unsigned long pgoff,unsigned long flags)1550 static unsigned long uvc_v4l2_get_unmapped_area(struct file *file,
1551 unsigned long addr, unsigned long len, unsigned long pgoff,
1552 unsigned long flags)
1553 {
1554 struct uvc_fh *handle = file->private_data;
1555 struct uvc_streaming *stream = handle->stream;
1556
1557 uvc_dbg(stream->dev, CALLS, "%s\n", __func__);
1558
1559 return uvc_queue_get_unmapped_area(&stream->queue, pgoff);
1560 }
1561 #endif
1562
1563 const struct v4l2_ioctl_ops uvc_ioctl_ops = {
1564 .vidioc_querycap = uvc_ioctl_querycap,
1565 .vidioc_enum_fmt_vid_cap = uvc_ioctl_enum_fmt_vid_cap,
1566 .vidioc_enum_fmt_vid_out = uvc_ioctl_enum_fmt_vid_out,
1567 .vidioc_g_fmt_vid_cap = uvc_ioctl_g_fmt_vid_cap,
1568 .vidioc_g_fmt_vid_out = uvc_ioctl_g_fmt_vid_out,
1569 .vidioc_s_fmt_vid_cap = uvc_ioctl_s_fmt_vid_cap,
1570 .vidioc_s_fmt_vid_out = uvc_ioctl_s_fmt_vid_out,
1571 .vidioc_try_fmt_vid_cap = uvc_ioctl_try_fmt_vid_cap,
1572 .vidioc_try_fmt_vid_out = uvc_ioctl_try_fmt_vid_out,
1573 .vidioc_reqbufs = uvc_ioctl_reqbufs,
1574 .vidioc_querybuf = uvc_ioctl_querybuf,
1575 .vidioc_qbuf = uvc_ioctl_qbuf,
1576 .vidioc_expbuf = uvc_ioctl_expbuf,
1577 .vidioc_dqbuf = uvc_ioctl_dqbuf,
1578 .vidioc_create_bufs = uvc_ioctl_create_bufs,
1579 .vidioc_streamon = uvc_ioctl_streamon,
1580 .vidioc_streamoff = uvc_ioctl_streamoff,
1581 .vidioc_enum_input = uvc_ioctl_enum_input,
1582 .vidioc_g_input = uvc_ioctl_g_input,
1583 .vidioc_s_input = uvc_ioctl_s_input,
1584 .vidioc_queryctrl = uvc_ioctl_queryctrl,
1585 .vidioc_query_ext_ctrl = uvc_ioctl_query_ext_ctrl,
1586 .vidioc_g_ext_ctrls = uvc_ioctl_g_ext_ctrls,
1587 .vidioc_s_ext_ctrls = uvc_ioctl_s_ext_ctrls,
1588 .vidioc_try_ext_ctrls = uvc_ioctl_try_ext_ctrls,
1589 .vidioc_querymenu = uvc_ioctl_querymenu,
1590 .vidioc_g_selection = uvc_ioctl_g_selection,
1591 .vidioc_g_parm = uvc_ioctl_g_parm,
1592 .vidioc_s_parm = uvc_ioctl_s_parm,
1593 .vidioc_enum_framesizes = uvc_ioctl_enum_framesizes,
1594 .vidioc_enum_frameintervals = uvc_ioctl_enum_frameintervals,
1595 .vidioc_subscribe_event = uvc_ioctl_subscribe_event,
1596 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1597 .vidioc_default = uvc_ioctl_default,
1598 };
1599
1600 const struct v4l2_file_operations uvc_fops = {
1601 .owner = THIS_MODULE,
1602 .open = uvc_v4l2_open,
1603 .release = uvc_v4l2_release,
1604 .unlocked_ioctl = video_ioctl2,
1605 #ifdef CONFIG_COMPAT
1606 .compat_ioctl32 = uvc_v4l2_compat_ioctl32,
1607 #endif
1608 .read = uvc_v4l2_read,
1609 .mmap = uvc_v4l2_mmap,
1610 .poll = uvc_v4l2_poll,
1611 #ifndef CONFIG_MMU
1612 .get_unmapped_area = uvc_v4l2_get_unmapped_area,
1613 #endif
1614 };
1615
1616