drm/nouveau: consume the return of large GSP message
[drm/drm-misc.git] / drivers / media / usb / uvc / uvc_v4l2.c
blob97c5407f66032a8acf6156cc77be6ee471fb6f7a
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * uvc_v4l2.c -- USB Video Class driver - V4L2 API
5 * Copyright (C) 2005-2010
6 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7 */
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>
22 #include <media/v4l2-common.h>
23 #include <media/v4l2-ctrls.h>
24 #include <media/v4l2-event.h>
25 #include <media/v4l2-ioctl.h>
27 #include "uvcvideo.h"
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)
33 unsigned int i;
34 size_t size;
35 int ret;
38 * Prevent excessive memory consumption, as well as integer
39 * overflows.
41 if (xmap->menu_count == 0 ||
42 xmap->menu_count > UVC_MAX_CONTROL_MENU_ENTRIES)
43 return -EINVAL;
45 map->menu_names = NULL;
46 map->menu_mapping = NULL;
48 map->menu_mask = GENMASK(xmap->menu_count - 1, 0);
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;
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;
67 * Always use the standard naming if available, otherwise copy the
68 * names supplied by userspace.
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;
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;
89 ret = uvc_ctrl_add_mapping(chain, map);
91 done:
92 kfree(map->menu_names);
93 map->menu_names = NULL;
94 kfree(map->menu_mapping);
95 map->menu_mapping = NULL;
97 return ret;
100 /* ------------------------------------------------------------------------
101 * UVC ioctls
103 static int uvc_ioctl_xu_ctrl_map(struct uvc_video_chain *chain,
104 struct uvc_xu_control_mapping *xmap)
106 struct uvc_control_mapping *map;
107 int ret;
109 map = kzalloc(sizeof(*map), GFP_KERNEL);
110 if (map == NULL)
111 return -ENOMEM;
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;
120 xmap->name[sizeof(xmap->name) - 1] = '\0';
121 map->name = xmap->name;
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;
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;
137 case V4L2_CTRL_TYPE_MENU:
138 ret = uvc_control_add_xu_mapping(chain, map, xmap);
139 break;
141 default:
142 uvc_dbg(chain->dev, CONTROL,
143 "Unsupported V4L2 control type %u\n", xmap->v4l2_type);
144 ret = -ENOTTY;
145 break;
148 free_map:
149 kfree(map);
151 return ret;
154 /* ------------------------------------------------------------------------
155 * V4L2 interface
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.
164 static u32 uvc_try_frame_interval(const struct uvc_frame *frame, u32 interval)
166 unsigned int i;
168 if (frame->bFrameIntervalType) {
169 u32 best = -1, dist;
171 for (i = 0; i < frame->bFrameIntervalType; ++i) {
172 dist = interval > frame->dwFrameInterval[i]
173 ? interval - frame->dwFrameInterval[i]
174 : frame->dwFrameInterval[i] - interval;
176 if (dist > best)
177 break;
179 best = dist;
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];
188 interval = min + (interval - min + step/2) / step * step;
189 if (interval > max)
190 interval = max;
193 return interval;
196 static u32 uvc_v4l2_get_bytesperline(const struct uvc_format *format,
197 const struct uvc_frame *frame)
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;
206 default:
207 return format->bpp * frame->wWidth / 8;
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)
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;
225 if (fmt->type != stream->type)
226 return -EINVAL;
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);
235 * Check if the hardware supports the requested format, use the default
236 * format otherwise.
238 for (i = 0; i < stream->nformats; ++i) {
239 format = &stream->formats[i];
240 if (format->fcc == fmt->fmt.pix.pixelformat)
241 break;
244 if (i == stream->nformats) {
245 format = stream->def_format;
246 fmt->fmt.pix.pixelformat = format->fcc;
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.
254 rw = fmt->fmt.pix.width;
255 rh = fmt->fmt.pix.height;
256 maxd = (unsigned int)-1;
258 for (i = 0; i < format->nframes; ++i) {
259 u16 w = format->frames[i].wWidth;
260 u16 h = format->frames[i].wHeight;
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];
269 if (maxd == 0)
270 break;
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;
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);
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);
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.
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).
305 mutex_lock(&stream->mutex);
306 if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS)
307 probe->dwMaxVideoFrameSize =
308 stream->ctrl.dwMaxVideoFrameSize;
310 /* Probe the device. */
311 ret = uvc_probe_video(stream, probe);
312 mutex_unlock(&stream->mutex);
313 if (ret < 0)
314 return ret;
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.
322 for (i = 0; i < stream->nformats; ++i) {
323 if (probe->bFormatIndex == stream->formats[i].index) {
324 format = &stream->formats[i];
325 break;
329 if (i == stream->nformats)
330 uvc_dbg(stream->dev, FORMAT,
331 "Unknown bFormatIndex %u, using default\n",
332 probe->bFormatIndex);
334 for (i = 0; i < format->nframes; ++i) {
335 if (probe->bFrameIndex == format->frames[i].bFrameIndex) {
336 frame = &format->frames[i];
337 break;
341 if (i == format->nframes)
342 uvc_dbg(stream->dev, FORMAT,
343 "Unknown bFrameIndex %u, using default\n",
344 probe->bFrameIndex);
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;
356 if (uvc_format != NULL)
357 *uvc_format = format;
358 if (uvc_frame != NULL)
359 *uvc_frame = frame;
361 return ret;
364 static int uvc_v4l2_get_format(struct uvc_streaming *stream,
365 struct v4l2_format *fmt)
367 const struct uvc_format *format;
368 const struct uvc_frame *frame;
369 int ret = 0;
371 if (fmt->type != stream->type)
372 return -EINVAL;
374 mutex_lock(&stream->mutex);
375 format = stream->cur_format;
376 frame = stream->cur_frame;
378 if (format == NULL || frame == NULL) {
379 ret = -EINVAL;
380 goto done;
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;
393 done:
394 mutex_unlock(&stream->mutex);
395 return ret;
398 static int uvc_v4l2_set_format(struct uvc_streaming *stream,
399 struct v4l2_format *fmt)
401 struct uvc_streaming_control probe;
402 const struct uvc_format *format;
403 const struct uvc_frame *frame;
404 int ret;
406 if (fmt->type != stream->type)
407 return -EINVAL;
409 ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame);
410 if (ret < 0)
411 return ret;
413 mutex_lock(&stream->mutex);
415 if (uvc_queue_allocated(&stream->queue)) {
416 ret = -EBUSY;
417 goto done;
420 stream->ctrl = probe;
421 stream->cur_format = format;
422 stream->cur_frame = frame;
424 done:
425 mutex_unlock(&stream->mutex);
426 return ret;
429 static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream,
430 struct v4l2_streamparm *parm)
432 u32 numerator, denominator;
434 if (parm->type != stream->type)
435 return -EINVAL;
437 mutex_lock(&stream->mutex);
438 numerator = stream->ctrl.dwFrameInterval;
439 mutex_unlock(&stream->mutex);
441 denominator = 10000000;
442 v4l2_simplify_fraction(&numerator, &denominator, 8, 333);
444 memset(parm, 0, sizeof(*parm));
445 parm->type = stream->type;
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;
461 return 0;
464 static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
465 struct v4l2_streamparm *parm)
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;
475 if (parm->type != stream->type)
476 return -EINVAL;
478 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
479 timeperframe = parm->parm.capture.timeperframe;
480 else
481 timeperframe = parm->parm.output.timeperframe;
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);
488 mutex_lock(&stream->mutex);
490 if (uvc_queue_streaming(&stream->queue)) {
491 mutex_unlock(&stream->mutex);
492 return -EBUSY;
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);
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;
505 if (&format->frames[i] == stream->cur_frame)
506 continue;
508 if (format->frames[i].wWidth != stream->cur_frame->wWidth ||
509 format->frames[i].wHeight != stream->cur_frame->wHeight)
510 continue;
512 ival = uvc_try_frame_interval(&format->frames[i], interval);
513 d = abs((s32)ival - interval);
514 if (d >= maxd)
515 continue;
517 frame = &format->frames[i];
518 probe.bFrameIndex = frame->bFrameIndex;
519 probe.dwFrameInterval = ival;
520 maxd = d;
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;
530 stream->ctrl = probe;
531 stream->cur_frame = frame;
532 mutex_unlock(&stream->mutex);
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);
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;
548 return 0;
551 /* ------------------------------------------------------------------------
552 * Privilege management
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.
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.
571 * Operations that require privileges are:
573 * - VIDIOC_S_INPUT
574 * - VIDIOC_S_PARM
575 * - VIDIOC_S_FMT
576 * - VIDIOC_REQBUFS
578 static int uvc_acquire_privileges(struct uvc_fh *handle)
580 /* Always succeed if the handle is already privileged. */
581 if (handle->state == UVC_HANDLE_ACTIVE)
582 return 0;
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;
590 handle->state = UVC_HANDLE_ACTIVE;
591 return 0;
594 static void uvc_dismiss_privileges(struct uvc_fh *handle)
596 if (handle->state == UVC_HANDLE_ACTIVE)
597 atomic_dec(&handle->stream->active);
599 handle->state = UVC_HANDLE_PASSIVE;
602 static int uvc_has_privileges(struct uvc_fh *handle)
604 return handle->state == UVC_HANDLE_ACTIVE;
607 /* ------------------------------------------------------------------------
608 * V4L2 file operations
611 static int uvc_v4l2_open(struct file *file)
613 struct uvc_streaming *stream;
614 struct uvc_fh *handle;
615 int ret = 0;
617 stream = video_drvdata(file);
618 uvc_dbg(stream->dev, CALLS, "%s\n", __func__);
620 ret = usb_autopm_get_interface(stream->dev->intf);
621 if (ret < 0)
622 return ret;
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;
631 ret = uvc_status_get(stream->dev);
632 if (ret) {
633 usb_autopm_put_interface(stream->dev->intf);
634 kfree(handle);
635 return ret;
638 v4l2_fh_init(&handle->vfh, &stream->vdev);
639 v4l2_fh_add(&handle->vfh);
640 handle->chain = stream->chain;
641 handle->stream = stream;
642 handle->state = UVC_HANDLE_PASSIVE;
643 file->private_data = handle;
645 return 0;
648 static int uvc_v4l2_release(struct file *file)
650 struct uvc_fh *handle = file->private_data;
651 struct uvc_streaming *stream = handle->stream;
653 uvc_dbg(stream->dev, CALLS, "%s\n", __func__);
655 /* Only free resources if this is a privileged handle. */
656 if (uvc_has_privileges(handle))
657 uvc_queue_release(&stream->queue);
659 /* Release the file handle. */
660 uvc_dismiss_privileges(handle);
661 v4l2_fh_del(&handle->vfh);
662 v4l2_fh_exit(&handle->vfh);
663 kfree(handle);
664 file->private_data = NULL;
666 uvc_status_put(stream->dev);
668 usb_autopm_put_interface(stream->dev->intf);
669 return 0;
672 static int uvc_ioctl_querycap(struct file *file, void *fh,
673 struct v4l2_capability *cap)
675 struct uvc_fh *handle = file->private_data;
676 struct uvc_video_chain *chain = handle->chain;
677 struct uvc_streaming *stream = handle->stream;
679 strscpy(cap->driver, "uvcvideo", sizeof(cap->driver));
680 strscpy(cap->card, handle->stream->dev->name, sizeof(cap->card));
681 usb_make_path(stream->dev->udev, cap->bus_info, sizeof(cap->bus_info));
682 cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
683 | chain->caps;
685 return 0;
688 static int uvc_ioctl_enum_fmt(struct uvc_streaming *stream,
689 struct v4l2_fmtdesc *fmt)
691 const struct uvc_format *format;
692 enum v4l2_buf_type type = fmt->type;
693 u32 index = fmt->index;
695 if (fmt->type != stream->type || fmt->index >= stream->nformats)
696 return -EINVAL;
698 memset(fmt, 0, sizeof(*fmt));
699 fmt->index = index;
700 fmt->type = type;
702 format = &stream->formats[fmt->index];
703 fmt->flags = 0;
704 if (format->flags & UVC_FMT_FLAG_COMPRESSED)
705 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
706 fmt->pixelformat = format->fcc;
707 return 0;
710 static int uvc_ioctl_enum_fmt_vid_cap(struct file *file, void *fh,
711 struct v4l2_fmtdesc *fmt)
713 struct uvc_fh *handle = fh;
714 struct uvc_streaming *stream = handle->stream;
716 return uvc_ioctl_enum_fmt(stream, fmt);
719 static int uvc_ioctl_enum_fmt_vid_out(struct file *file, void *fh,
720 struct v4l2_fmtdesc *fmt)
722 struct uvc_fh *handle = fh;
723 struct uvc_streaming *stream = handle->stream;
725 return uvc_ioctl_enum_fmt(stream, fmt);
728 static int uvc_ioctl_g_fmt_vid_cap(struct file *file, void *fh,
729 struct v4l2_format *fmt)
731 struct uvc_fh *handle = fh;
732 struct uvc_streaming *stream = handle->stream;
734 return uvc_v4l2_get_format(stream, fmt);
737 static int uvc_ioctl_g_fmt_vid_out(struct file *file, void *fh,
738 struct v4l2_format *fmt)
740 struct uvc_fh *handle = fh;
741 struct uvc_streaming *stream = handle->stream;
743 return uvc_v4l2_get_format(stream, fmt);
746 static int uvc_ioctl_s_fmt_vid_cap(struct file *file, void *fh,
747 struct v4l2_format *fmt)
749 struct uvc_fh *handle = fh;
750 struct uvc_streaming *stream = handle->stream;
751 int ret;
753 ret = uvc_acquire_privileges(handle);
754 if (ret < 0)
755 return ret;
757 return uvc_v4l2_set_format(stream, fmt);
760 static int uvc_ioctl_s_fmt_vid_out(struct file *file, void *fh,
761 struct v4l2_format *fmt)
763 struct uvc_fh *handle = fh;
764 struct uvc_streaming *stream = handle->stream;
765 int ret;
767 ret = uvc_acquire_privileges(handle);
768 if (ret < 0)
769 return ret;
771 return uvc_v4l2_set_format(stream, fmt);
774 static int uvc_ioctl_try_fmt_vid_cap(struct file *file, void *fh,
775 struct v4l2_format *fmt)
777 struct uvc_fh *handle = fh;
778 struct uvc_streaming *stream = handle->stream;
779 struct uvc_streaming_control probe;
781 return uvc_v4l2_try_format(stream, fmt, &probe, NULL, NULL);
784 static int uvc_ioctl_try_fmt_vid_out(struct file *file, void *fh,
785 struct v4l2_format *fmt)
787 struct uvc_fh *handle = fh;
788 struct uvc_streaming *stream = handle->stream;
789 struct uvc_streaming_control probe;
791 return uvc_v4l2_try_format(stream, fmt, &probe, NULL, NULL);
794 static int uvc_ioctl_reqbufs(struct file *file, void *fh,
795 struct v4l2_requestbuffers *rb)
797 struct uvc_fh *handle = fh;
798 struct uvc_streaming *stream = handle->stream;
799 int ret;
801 ret = uvc_acquire_privileges(handle);
802 if (ret < 0)
803 return ret;
805 mutex_lock(&stream->mutex);
806 ret = uvc_request_buffers(&stream->queue, rb);
807 mutex_unlock(&stream->mutex);
808 if (ret < 0)
809 return ret;
811 if (ret == 0)
812 uvc_dismiss_privileges(handle);
814 return 0;
817 static int uvc_ioctl_querybuf(struct file *file, void *fh,
818 struct v4l2_buffer *buf)
820 struct uvc_fh *handle = fh;
821 struct uvc_streaming *stream = handle->stream;
823 if (!uvc_has_privileges(handle))
824 return -EBUSY;
826 return uvc_query_buffer(&stream->queue, buf);
829 static int uvc_ioctl_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
831 struct uvc_fh *handle = fh;
832 struct uvc_streaming *stream = handle->stream;
834 if (!uvc_has_privileges(handle))
835 return -EBUSY;
837 return uvc_queue_buffer(&stream->queue,
838 stream->vdev.v4l2_dev->mdev, buf);
841 static int uvc_ioctl_expbuf(struct file *file, void *fh,
842 struct v4l2_exportbuffer *exp)
844 struct uvc_fh *handle = fh;
845 struct uvc_streaming *stream = handle->stream;
847 if (!uvc_has_privileges(handle))
848 return -EBUSY;
850 return uvc_export_buffer(&stream->queue, exp);
853 static int uvc_ioctl_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
855 struct uvc_fh *handle = fh;
856 struct uvc_streaming *stream = handle->stream;
858 if (!uvc_has_privileges(handle))
859 return -EBUSY;
861 return uvc_dequeue_buffer(&stream->queue, buf,
862 file->f_flags & O_NONBLOCK);
865 static int uvc_ioctl_create_bufs(struct file *file, void *fh,
866 struct v4l2_create_buffers *cb)
868 struct uvc_fh *handle = fh;
869 struct uvc_streaming *stream = handle->stream;
870 int ret;
872 ret = uvc_acquire_privileges(handle);
873 if (ret < 0)
874 return ret;
876 return uvc_create_buffers(&stream->queue, cb);
879 static int uvc_ioctl_streamon(struct file *file, void *fh,
880 enum v4l2_buf_type type)
882 struct uvc_fh *handle = fh;
883 struct uvc_streaming *stream = handle->stream;
884 int ret;
886 if (!uvc_has_privileges(handle))
887 return -EBUSY;
889 mutex_lock(&stream->mutex);
890 ret = uvc_queue_streamon(&stream->queue, type);
891 mutex_unlock(&stream->mutex);
893 return ret;
896 static int uvc_ioctl_streamoff(struct file *file, void *fh,
897 enum v4l2_buf_type type)
899 struct uvc_fh *handle = fh;
900 struct uvc_streaming *stream = handle->stream;
902 if (!uvc_has_privileges(handle))
903 return -EBUSY;
905 mutex_lock(&stream->mutex);
906 uvc_queue_streamoff(&stream->queue, type);
907 mutex_unlock(&stream->mutex);
909 return 0;
912 static int uvc_ioctl_enum_input(struct file *file, void *fh,
913 struct v4l2_input *input)
915 struct uvc_fh *handle = fh;
916 struct uvc_video_chain *chain = handle->chain;
917 const struct uvc_entity *selector = chain->selector;
918 struct uvc_entity *iterm = NULL;
919 struct uvc_entity *it;
920 u32 index = input->index;
922 if (selector == NULL ||
923 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
924 if (index != 0)
925 return -EINVAL;
926 list_for_each_entry(it, &chain->entities, chain) {
927 if (UVC_ENTITY_IS_ITERM(it)) {
928 iterm = it;
929 break;
932 } else if (index < selector->bNrInPins) {
933 list_for_each_entry(it, &chain->entities, chain) {
934 if (!UVC_ENTITY_IS_ITERM(it))
935 continue;
936 if (it->id == selector->baSourceID[index]) {
937 iterm = it;
938 break;
943 if (iterm == NULL)
944 return -EINVAL;
946 memset(input, 0, sizeof(*input));
947 input->index = index;
948 strscpy(input->name, iterm->name, sizeof(input->name));
949 if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
950 input->type = V4L2_INPUT_TYPE_CAMERA;
952 return 0;
955 static int uvc_ioctl_g_input(struct file *file, void *fh, unsigned int *input)
957 struct uvc_fh *handle = fh;
958 struct uvc_video_chain *chain = handle->chain;
959 u8 *buf;
960 int ret;
962 if (chain->selector == NULL ||
963 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
964 *input = 0;
965 return 0;
968 buf = kmalloc(1, GFP_KERNEL);
969 if (!buf)
970 return -ENOMEM;
972 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, chain->selector->id,
973 chain->dev->intfnum, UVC_SU_INPUT_SELECT_CONTROL,
974 buf, 1);
975 if (!ret)
976 *input = *buf - 1;
978 kfree(buf);
980 return ret;
983 static int uvc_ioctl_s_input(struct file *file, void *fh, unsigned int input)
985 struct uvc_fh *handle = fh;
986 struct uvc_video_chain *chain = handle->chain;
987 u8 *buf;
988 int ret;
990 ret = uvc_acquire_privileges(handle);
991 if (ret < 0)
992 return ret;
994 if (chain->selector == NULL ||
995 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
996 if (input)
997 return -EINVAL;
998 return 0;
1001 if (input >= chain->selector->bNrInPins)
1002 return -EINVAL;
1004 buf = kmalloc(1, GFP_KERNEL);
1005 if (!buf)
1006 return -ENOMEM;
1008 *buf = input + 1;
1009 ret = uvc_query_ctrl(chain->dev, UVC_SET_CUR, chain->selector->id,
1010 chain->dev->intfnum, UVC_SU_INPUT_SELECT_CONTROL,
1011 buf, 1);
1012 kfree(buf);
1014 return ret;
1017 static int uvc_ioctl_queryctrl(struct file *file, void *fh,
1018 struct v4l2_queryctrl *qc)
1020 struct uvc_fh *handle = fh;
1021 struct uvc_video_chain *chain = handle->chain;
1023 return uvc_query_v4l2_ctrl(chain, qc);
1026 static int uvc_ioctl_query_ext_ctrl(struct file *file, void *fh,
1027 struct v4l2_query_ext_ctrl *qec)
1029 struct uvc_fh *handle = fh;
1030 struct uvc_video_chain *chain = handle->chain;
1031 struct v4l2_queryctrl qc = { qec->id };
1032 int ret;
1034 ret = uvc_query_v4l2_ctrl(chain, &qc);
1035 if (ret)
1036 return ret;
1038 qec->id = qc.id;
1039 qec->type = qc.type;
1040 strscpy(qec->name, qc.name, sizeof(qec->name));
1041 qec->minimum = qc.minimum;
1042 qec->maximum = qc.maximum;
1043 qec->step = qc.step;
1044 qec->default_value = qc.default_value;
1045 qec->flags = qc.flags;
1046 qec->elem_size = 4;
1047 qec->elems = 1;
1048 qec->nr_of_dims = 0;
1049 memset(qec->dims, 0, sizeof(qec->dims));
1050 memset(qec->reserved, 0, sizeof(qec->reserved));
1052 return 0;
1055 static int uvc_ctrl_check_access(struct uvc_video_chain *chain,
1056 struct v4l2_ext_controls *ctrls,
1057 unsigned long ioctl)
1059 struct v4l2_ext_control *ctrl = ctrls->controls;
1060 unsigned int i;
1061 int ret = 0;
1063 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
1064 ret = uvc_ctrl_is_accessible(chain, ctrl->id, ctrls, ioctl);
1065 if (ret)
1066 break;
1069 ctrls->error_idx = ioctl == VIDIOC_TRY_EXT_CTRLS ? i : ctrls->count;
1071 return ret;
1074 static int uvc_ioctl_g_ext_ctrls(struct file *file, void *fh,
1075 struct v4l2_ext_controls *ctrls)
1077 struct uvc_fh *handle = fh;
1078 struct uvc_video_chain *chain = handle->chain;
1079 struct v4l2_ext_control *ctrl = ctrls->controls;
1080 unsigned int i;
1081 int ret;
1083 ret = uvc_ctrl_check_access(chain, ctrls, VIDIOC_G_EXT_CTRLS);
1084 if (ret < 0)
1085 return ret;
1087 if (ctrls->which == V4L2_CTRL_WHICH_DEF_VAL) {
1088 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
1089 struct v4l2_queryctrl qc = { .id = ctrl->id };
1091 ret = uvc_query_v4l2_ctrl(chain, &qc);
1092 if (ret < 0) {
1093 ctrls->error_idx = i;
1094 return ret;
1097 ctrl->value = qc.default_value;
1100 return 0;
1103 ret = uvc_ctrl_begin(chain);
1104 if (ret < 0)
1105 return ret;
1107 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
1108 ret = uvc_ctrl_get(chain, ctrl);
1109 if (ret < 0) {
1110 uvc_ctrl_rollback(handle);
1111 ctrls->error_idx = i;
1112 return ret;
1116 ctrls->error_idx = 0;
1118 return uvc_ctrl_rollback(handle);
1121 static int uvc_ioctl_s_try_ext_ctrls(struct uvc_fh *handle,
1122 struct v4l2_ext_controls *ctrls,
1123 unsigned long ioctl)
1125 struct v4l2_ext_control *ctrl = ctrls->controls;
1126 struct uvc_video_chain *chain = handle->chain;
1127 unsigned int i;
1128 int ret;
1130 ret = uvc_ctrl_check_access(chain, ctrls, ioctl);
1131 if (ret < 0)
1132 return ret;
1134 ret = uvc_ctrl_begin(chain);
1135 if (ret < 0)
1136 return ret;
1138 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
1139 ret = uvc_ctrl_set(handle, ctrl);
1140 if (ret < 0) {
1141 uvc_ctrl_rollback(handle);
1142 ctrls->error_idx = ioctl == VIDIOC_S_EXT_CTRLS ?
1143 ctrls->count : i;
1144 return ret;
1148 ctrls->error_idx = 0;
1150 if (ioctl == VIDIOC_S_EXT_CTRLS)
1151 return uvc_ctrl_commit(handle, ctrls);
1152 else
1153 return uvc_ctrl_rollback(handle);
1156 static int uvc_ioctl_s_ext_ctrls(struct file *file, void *fh,
1157 struct v4l2_ext_controls *ctrls)
1159 struct uvc_fh *handle = fh;
1161 return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, VIDIOC_S_EXT_CTRLS);
1164 static int uvc_ioctl_try_ext_ctrls(struct file *file, void *fh,
1165 struct v4l2_ext_controls *ctrls)
1167 struct uvc_fh *handle = fh;
1169 return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, VIDIOC_TRY_EXT_CTRLS);
1172 static int uvc_ioctl_querymenu(struct file *file, void *fh,
1173 struct v4l2_querymenu *qm)
1175 struct uvc_fh *handle = fh;
1176 struct uvc_video_chain *chain = handle->chain;
1178 return uvc_query_v4l2_menu(chain, qm);
1181 static int uvc_ioctl_g_selection(struct file *file, void *fh,
1182 struct v4l2_selection *sel)
1184 struct uvc_fh *handle = fh;
1185 struct uvc_streaming *stream = handle->stream;
1187 if (sel->type != stream->type)
1188 return -EINVAL;
1190 switch (sel->target) {
1191 case V4L2_SEL_TGT_CROP_DEFAULT:
1192 case V4L2_SEL_TGT_CROP_BOUNDS:
1193 if (stream->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1194 return -EINVAL;
1195 break;
1196 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1197 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1198 if (stream->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1199 return -EINVAL;
1200 break;
1201 default:
1202 return -EINVAL;
1205 sel->r.left = 0;
1206 sel->r.top = 0;
1207 mutex_lock(&stream->mutex);
1208 sel->r.width = stream->cur_frame->wWidth;
1209 sel->r.height = stream->cur_frame->wHeight;
1210 mutex_unlock(&stream->mutex);
1212 return 0;
1215 static int uvc_ioctl_g_parm(struct file *file, void *fh,
1216 struct v4l2_streamparm *parm)
1218 struct uvc_fh *handle = fh;
1219 struct uvc_streaming *stream = handle->stream;
1221 return uvc_v4l2_get_streamparm(stream, parm);
1224 static int uvc_ioctl_s_parm(struct file *file, void *fh,
1225 struct v4l2_streamparm *parm)
1227 struct uvc_fh *handle = fh;
1228 struct uvc_streaming *stream = handle->stream;
1229 int ret;
1231 ret = uvc_acquire_privileges(handle);
1232 if (ret < 0)
1233 return ret;
1235 return uvc_v4l2_set_streamparm(stream, parm);
1238 static int uvc_ioctl_enum_framesizes(struct file *file, void *fh,
1239 struct v4l2_frmsizeenum *fsize)
1241 struct uvc_fh *handle = fh;
1242 struct uvc_streaming *stream = handle->stream;
1243 const struct uvc_format *format = NULL;
1244 const struct uvc_frame *frame = NULL;
1245 unsigned int index;
1246 unsigned int i;
1248 /* Look for the given pixel format */
1249 for (i = 0; i < stream->nformats; i++) {
1250 if (stream->formats[i].fcc == fsize->pixel_format) {
1251 format = &stream->formats[i];
1252 break;
1255 if (format == NULL)
1256 return -EINVAL;
1258 /* Skip duplicate frame sizes */
1259 for (i = 0, index = 0; i < format->nframes; i++) {
1260 if (frame && frame->wWidth == format->frames[i].wWidth &&
1261 frame->wHeight == format->frames[i].wHeight)
1262 continue;
1263 frame = &format->frames[i];
1264 if (index == fsize->index)
1265 break;
1266 index++;
1269 if (i == format->nframes)
1270 return -EINVAL;
1272 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1273 fsize->discrete.width = frame->wWidth;
1274 fsize->discrete.height = frame->wHeight;
1275 return 0;
1278 static int uvc_ioctl_enum_frameintervals(struct file *file, void *fh,
1279 struct v4l2_frmivalenum *fival)
1281 struct uvc_fh *handle = fh;
1282 struct uvc_streaming *stream = handle->stream;
1283 const struct uvc_format *format = NULL;
1284 const struct uvc_frame *frame = NULL;
1285 unsigned int nintervals;
1286 unsigned int index;
1287 unsigned int i;
1289 /* Look for the given pixel format and frame size */
1290 for (i = 0; i < stream->nformats; i++) {
1291 if (stream->formats[i].fcc == fival->pixel_format) {
1292 format = &stream->formats[i];
1293 break;
1296 if (format == NULL)
1297 return -EINVAL;
1299 index = fival->index;
1300 for (i = 0; i < format->nframes; i++) {
1301 if (format->frames[i].wWidth == fival->width &&
1302 format->frames[i].wHeight == fival->height) {
1303 frame = &format->frames[i];
1304 nintervals = frame->bFrameIntervalType ?: 1;
1305 if (index < nintervals)
1306 break;
1307 index -= nintervals;
1310 if (i == format->nframes)
1311 return -EINVAL;
1313 if (frame->bFrameIntervalType) {
1314 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1315 fival->discrete.numerator =
1316 frame->dwFrameInterval[index];
1317 fival->discrete.denominator = 10000000;
1318 v4l2_simplify_fraction(&fival->discrete.numerator,
1319 &fival->discrete.denominator, 8, 333);
1320 } else {
1321 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
1322 fival->stepwise.min.numerator = frame->dwFrameInterval[0];
1323 fival->stepwise.min.denominator = 10000000;
1324 fival->stepwise.max.numerator = frame->dwFrameInterval[1];
1325 fival->stepwise.max.denominator = 10000000;
1326 fival->stepwise.step.numerator = frame->dwFrameInterval[2];
1327 fival->stepwise.step.denominator = 10000000;
1328 v4l2_simplify_fraction(&fival->stepwise.min.numerator,
1329 &fival->stepwise.min.denominator, 8, 333);
1330 v4l2_simplify_fraction(&fival->stepwise.max.numerator,
1331 &fival->stepwise.max.denominator, 8, 333);
1332 v4l2_simplify_fraction(&fival->stepwise.step.numerator,
1333 &fival->stepwise.step.denominator, 8, 333);
1336 return 0;
1339 static int uvc_ioctl_subscribe_event(struct v4l2_fh *fh,
1340 const struct v4l2_event_subscription *sub)
1342 switch (sub->type) {
1343 case V4L2_EVENT_CTRL:
1344 return v4l2_event_subscribe(fh, sub, 0, &uvc_ctrl_sub_ev_ops);
1345 default:
1346 return -EINVAL;
1350 static long uvc_ioctl_default(struct file *file, void *fh, bool valid_prio,
1351 unsigned int cmd, void *arg)
1353 struct uvc_fh *handle = fh;
1354 struct uvc_video_chain *chain = handle->chain;
1356 switch (cmd) {
1357 /* Dynamic controls. */
1358 case UVCIOC_CTRL_MAP:
1359 return uvc_ioctl_xu_ctrl_map(chain, arg);
1361 case UVCIOC_CTRL_QUERY:
1362 return uvc_xu_ctrl_query(chain, arg);
1364 default:
1365 return -ENOTTY;
1369 #ifdef CONFIG_COMPAT
1370 struct uvc_xu_control_mapping32 {
1371 u32 id;
1372 u8 name[32];
1373 u8 entity[16];
1374 u8 selector;
1376 u8 size;
1377 u8 offset;
1378 u32 v4l2_type;
1379 u32 data_type;
1381 compat_caddr_t menu_info;
1382 u32 menu_count;
1384 u32 reserved[4];
1387 static int uvc_v4l2_get_xu_mapping(struct uvc_xu_control_mapping *kp,
1388 const struct uvc_xu_control_mapping32 __user *up)
1390 struct uvc_xu_control_mapping32 *p = (void *)kp;
1391 compat_caddr_t info;
1392 u32 count;
1394 if (copy_from_user(p, up, sizeof(*p)))
1395 return -EFAULT;
1397 count = p->menu_count;
1398 info = p->menu_info;
1400 memset(kp->reserved, 0, sizeof(kp->reserved));
1401 kp->menu_info = count ? compat_ptr(info) : NULL;
1402 kp->menu_count = count;
1403 return 0;
1406 static int uvc_v4l2_put_xu_mapping(const struct uvc_xu_control_mapping *kp,
1407 struct uvc_xu_control_mapping32 __user *up)
1409 if (copy_to_user(up, kp, offsetof(typeof(*up), menu_info)) ||
1410 put_user(kp->menu_count, &up->menu_count))
1411 return -EFAULT;
1413 if (clear_user(up->reserved, sizeof(up->reserved)))
1414 return -EFAULT;
1416 return 0;
1419 struct uvc_xu_control_query32 {
1420 u8 unit;
1421 u8 selector;
1422 u8 query;
1423 u16 size;
1424 compat_caddr_t data;
1427 static int uvc_v4l2_get_xu_query(struct uvc_xu_control_query *kp,
1428 const struct uvc_xu_control_query32 __user *up)
1430 struct uvc_xu_control_query32 v;
1432 if (copy_from_user(&v, up, sizeof(v)))
1433 return -EFAULT;
1435 *kp = (struct uvc_xu_control_query){
1436 .unit = v.unit,
1437 .selector = v.selector,
1438 .query = v.query,
1439 .size = v.size,
1440 .data = v.size ? compat_ptr(v.data) : NULL
1442 return 0;
1445 static int uvc_v4l2_put_xu_query(const struct uvc_xu_control_query *kp,
1446 struct uvc_xu_control_query32 __user *up)
1448 if (copy_to_user(up, kp, offsetof(typeof(*up), data)))
1449 return -EFAULT;
1450 return 0;
1453 #define UVCIOC_CTRL_MAP32 _IOWR('u', 0x20, struct uvc_xu_control_mapping32)
1454 #define UVCIOC_CTRL_QUERY32 _IOWR('u', 0x21, struct uvc_xu_control_query32)
1456 static long uvc_v4l2_compat_ioctl32(struct file *file,
1457 unsigned int cmd, unsigned long arg)
1459 struct uvc_fh *handle = file->private_data;
1460 union {
1461 struct uvc_xu_control_mapping xmap;
1462 struct uvc_xu_control_query xqry;
1463 } karg;
1464 void __user *up = compat_ptr(arg);
1465 long ret;
1467 switch (cmd) {
1468 case UVCIOC_CTRL_MAP32:
1469 ret = uvc_v4l2_get_xu_mapping(&karg.xmap, up);
1470 if (ret)
1471 return ret;
1472 ret = uvc_ioctl_xu_ctrl_map(handle->chain, &karg.xmap);
1473 if (ret)
1474 return ret;
1475 ret = uvc_v4l2_put_xu_mapping(&karg.xmap, up);
1476 if (ret)
1477 return ret;
1479 break;
1481 case UVCIOC_CTRL_QUERY32:
1482 ret = uvc_v4l2_get_xu_query(&karg.xqry, up);
1483 if (ret)
1484 return ret;
1485 ret = uvc_xu_ctrl_query(handle->chain, &karg.xqry);
1486 if (ret)
1487 return ret;
1488 ret = uvc_v4l2_put_xu_query(&karg.xqry, up);
1489 if (ret)
1490 return ret;
1491 break;
1493 default:
1494 return -ENOIOCTLCMD;
1497 return ret;
1499 #endif
1501 static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1502 size_t count, loff_t *ppos)
1504 struct uvc_fh *handle = file->private_data;
1505 struct uvc_streaming *stream = handle->stream;
1507 uvc_dbg(stream->dev, CALLS, "%s: not implemented\n", __func__);
1508 return -EINVAL;
1511 static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1513 struct uvc_fh *handle = file->private_data;
1514 struct uvc_streaming *stream = handle->stream;
1516 uvc_dbg(stream->dev, CALLS, "%s\n", __func__);
1518 return uvc_queue_mmap(&stream->queue, vma);
1521 static __poll_t uvc_v4l2_poll(struct file *file, poll_table *wait)
1523 struct uvc_fh *handle = file->private_data;
1524 struct uvc_streaming *stream = handle->stream;
1526 uvc_dbg(stream->dev, CALLS, "%s\n", __func__);
1528 return uvc_queue_poll(&stream->queue, file, wait);
1531 #ifndef CONFIG_MMU
1532 static unsigned long uvc_v4l2_get_unmapped_area(struct file *file,
1533 unsigned long addr, unsigned long len, unsigned long pgoff,
1534 unsigned long flags)
1536 struct uvc_fh *handle = file->private_data;
1537 struct uvc_streaming *stream = handle->stream;
1539 uvc_dbg(stream->dev, CALLS, "%s\n", __func__);
1541 return uvc_queue_get_unmapped_area(&stream->queue, pgoff);
1543 #endif
1545 const struct v4l2_ioctl_ops uvc_ioctl_ops = {
1546 .vidioc_querycap = uvc_ioctl_querycap,
1547 .vidioc_enum_fmt_vid_cap = uvc_ioctl_enum_fmt_vid_cap,
1548 .vidioc_enum_fmt_vid_out = uvc_ioctl_enum_fmt_vid_out,
1549 .vidioc_g_fmt_vid_cap = uvc_ioctl_g_fmt_vid_cap,
1550 .vidioc_g_fmt_vid_out = uvc_ioctl_g_fmt_vid_out,
1551 .vidioc_s_fmt_vid_cap = uvc_ioctl_s_fmt_vid_cap,
1552 .vidioc_s_fmt_vid_out = uvc_ioctl_s_fmt_vid_out,
1553 .vidioc_try_fmt_vid_cap = uvc_ioctl_try_fmt_vid_cap,
1554 .vidioc_try_fmt_vid_out = uvc_ioctl_try_fmt_vid_out,
1555 .vidioc_reqbufs = uvc_ioctl_reqbufs,
1556 .vidioc_querybuf = uvc_ioctl_querybuf,
1557 .vidioc_qbuf = uvc_ioctl_qbuf,
1558 .vidioc_expbuf = uvc_ioctl_expbuf,
1559 .vidioc_dqbuf = uvc_ioctl_dqbuf,
1560 .vidioc_create_bufs = uvc_ioctl_create_bufs,
1561 .vidioc_streamon = uvc_ioctl_streamon,
1562 .vidioc_streamoff = uvc_ioctl_streamoff,
1563 .vidioc_enum_input = uvc_ioctl_enum_input,
1564 .vidioc_g_input = uvc_ioctl_g_input,
1565 .vidioc_s_input = uvc_ioctl_s_input,
1566 .vidioc_queryctrl = uvc_ioctl_queryctrl,
1567 .vidioc_query_ext_ctrl = uvc_ioctl_query_ext_ctrl,
1568 .vidioc_g_ext_ctrls = uvc_ioctl_g_ext_ctrls,
1569 .vidioc_s_ext_ctrls = uvc_ioctl_s_ext_ctrls,
1570 .vidioc_try_ext_ctrls = uvc_ioctl_try_ext_ctrls,
1571 .vidioc_querymenu = uvc_ioctl_querymenu,
1572 .vidioc_g_selection = uvc_ioctl_g_selection,
1573 .vidioc_g_parm = uvc_ioctl_g_parm,
1574 .vidioc_s_parm = uvc_ioctl_s_parm,
1575 .vidioc_enum_framesizes = uvc_ioctl_enum_framesizes,
1576 .vidioc_enum_frameintervals = uvc_ioctl_enum_frameintervals,
1577 .vidioc_subscribe_event = uvc_ioctl_subscribe_event,
1578 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1579 .vidioc_default = uvc_ioctl_default,
1582 const struct v4l2_file_operations uvc_fops = {
1583 .owner = THIS_MODULE,
1584 .open = uvc_v4l2_open,
1585 .release = uvc_v4l2_release,
1586 .unlocked_ioctl = video_ioctl2,
1587 #ifdef CONFIG_COMPAT
1588 .compat_ioctl32 = uvc_v4l2_compat_ioctl32,
1589 #endif
1590 .read = uvc_v4l2_read,
1591 .mmap = uvc_v4l2_mmap,
1592 .poll = uvc_v4l2_poll,
1593 #ifndef CONFIG_MMU
1594 .get_unmapped_area = uvc_v4l2_get_unmapped_area,
1595 #endif