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