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