cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

uvc_v4l2.c (39235B)


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