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