// SPDX-License-Identifier: GPL-2.0+
/*
 *	uvc_v4l2.c  --  USB Video Class Gadget driver
 *
 *	Copyright (C) 2009-2010
 *	    Laurent Pinchart (laurent.pinchart@ideasonboard.com)
 */

#include <linux/device.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/usb/g_uvc.h>
#include <linux/usb/uvc.h>
#include <linux/videodev2.h>
#include <linux/vmalloc.h>
#include <linux/wait.h>

#include <media/v4l2-dev.h>
#include <media/v4l2-event.h>
#include <media/v4l2-ioctl.h>

#include "f_uvc.h"
#include "uvc.h"
#include "uvc_queue.h"
#include "uvc_video.h"
#include "uvc_v4l2.h"
#include "uvc_configfs.h"

static const struct uvc_format_desc *to_uvc_format(struct uvcg_format *uformat)
{
	char guid[16] = UVC_GUID_FORMAT_MJPEG;
	const struct uvc_format_desc *format;
	struct uvcg_uncompressed *unc;

	if (uformat->type == UVCG_UNCOMPRESSED) {
		unc = to_uvcg_uncompressed(&uformat->group.cg_item);
		if (!unc)
			return ERR_PTR(-EINVAL);

		memcpy(guid, unc->desc.guidFormat, sizeof(guid));
	}

	format = uvc_format_by_guid(guid);
	if (!format)
		return ERR_PTR(-EINVAL);

	return format;
}

static int uvc_v4l2_get_bytesperline(struct uvcg_format *uformat,
			      struct uvcg_frame *uframe)
{
	struct uvcg_uncompressed *u;

	if (uformat->type == UVCG_UNCOMPRESSED) {
		u = to_uvcg_uncompressed(&uformat->group.cg_item);
		if (!u)
			return 0;

		return u->desc.bBitsPerPixel * uframe->frame.w_width / 8;
	}

	return 0;
}

static int uvc_get_frame_size(struct uvcg_format *uformat,
		       struct uvcg_frame *uframe)
{
	unsigned int bpl = uvc_v4l2_get_bytesperline(uformat, uframe);

	return bpl ? bpl * uframe->frame.w_height :
		uframe->frame.dw_max_video_frame_buffer_size;
}

static struct uvcg_format *find_format_by_index(struct uvc_device *uvc, int index)
{
	struct uvcg_format_ptr *format;
	struct uvcg_format *uformat = NULL;
	int i = 1;

	list_for_each_entry(format, &uvc->header->formats, entry) {
		if (index == i) {
			uformat = format->fmt;
			break;
		}
		i++;
	}

	return uformat;
}

static struct uvcg_frame *find_frame_by_index(struct uvc_device *uvc,
				       struct uvcg_format *uformat,
				       int index)
{
	struct uvcg_format_ptr *format;
	struct uvcg_frame_ptr *frame;
	struct uvcg_frame *uframe = NULL;

	list_for_each_entry(format, &uvc->header->formats, entry) {
		if (format->fmt->type != uformat->type)
			continue;
		list_for_each_entry(frame, &format->fmt->frames, entry) {
			if (index == frame->frm->frame.b_frame_index) {
				uframe = frame->frm;
				break;
			}
		}
	}

	return uframe;
}

static struct uvcg_format *find_format_by_pix(struct uvc_device *uvc,
					      u32 pixelformat)
{
	struct uvcg_format_ptr *format;
	struct uvcg_format *uformat = NULL;

	list_for_each_entry(format, &uvc->header->formats, entry) {
		const struct uvc_format_desc *fmtdesc = to_uvc_format(format->fmt);

		if (fmtdesc->fcc == pixelformat) {
			uformat = format->fmt;
			break;
		}
	}

	return uformat;
}

static struct uvcg_frame *find_closest_frame_by_size(struct uvc_device *uvc,
					   struct uvcg_format *uformat,
					   u16 rw, u16 rh)
{
	struct uvc_video *video = &uvc->video;
	struct uvcg_format_ptr *format;
	struct uvcg_frame_ptr *frame;
	struct uvcg_frame *uframe = NULL;
	unsigned int d, maxd;

	/* Find the closest image size. The distance between image sizes is
	 * the size in pixels of the non-overlapping regions between the
	 * requested size and the frame-specified size.
	 */
	maxd = (unsigned int)-1;

	list_for_each_entry(format, &uvc->header->formats, entry) {
		if (format->fmt->type != uformat->type)
			continue;

		list_for_each_entry(frame, &format->fmt->frames, entry) {
			u16 w, h;

			w = frame->frm->frame.w_width;
			h = frame->frm->frame.w_height;

			d = min(w, rw) * min(h, rh);
			d = w*h + rw*rh - 2*d;
			if (d < maxd) {
				maxd = d;
				uframe = frame->frm;
			}

			if (maxd == 0)
				break;
		}
	}

	if (!uframe)
		uvcg_dbg(&video->uvc->func, "Unsupported size %ux%u\n", rw, rh);

	return uframe;
}

/* --------------------------------------------------------------------------
 * Requests handling
 */

static int
uvc_send_response(struct uvc_device *uvc, struct uvc_request_data *data)
{
	struct usb_composite_dev *cdev = uvc->func.config->cdev;
	struct usb_request *req = uvc->control_req;

	if (data->length < 0)
		return usb_ep_set_halt(cdev->gadget->ep0);

	req->length = min_t(unsigned int, uvc->event_length, data->length);
	req->zero = data->length < uvc->event_length;

	memcpy(req->buf, data->data, req->length);

	return usb_ep_queue(cdev->gadget->ep0, req, GFP_KERNEL);
}

/* --------------------------------------------------------------------------
 * V4L2 ioctls
 */

static int
uvc_v4l2_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
{
	struct video_device *vdev = video_devdata(file);
	struct uvc_device *uvc = video_get_drvdata(vdev);
	struct usb_composite_dev *cdev = uvc->func.config->cdev;

	strscpy(cap->driver, "g_uvc", sizeof(cap->driver));
	strscpy(cap->card, cdev->gadget->name, sizeof(cap->card));
	strscpy(cap->bus_info, dev_name(&cdev->gadget->dev),
		sizeof(cap->bus_info));
	return 0;
}

static int
uvc_v4l2_get_format(struct file *file, void *fh, struct v4l2_format *fmt)
{
	struct video_device *vdev = video_devdata(file);
	struct uvc_device *uvc = video_get_drvdata(vdev);
	struct uvc_video *video = &uvc->video;

	fmt->fmt.pix.pixelformat = video->fcc;
	fmt->fmt.pix.width = video->width;
	fmt->fmt.pix.height = video->height;
	fmt->fmt.pix.field = V4L2_FIELD_NONE;
	fmt->fmt.pix.bytesperline = video->bpp * video->width / 8;
	fmt->fmt.pix.sizeimage = video->imagesize;
	fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
	fmt->fmt.pix.priv = 0;

	return 0;
}

static int
uvc_v4l2_try_format(struct file *file, void *fh, struct v4l2_format *fmt)
{
	struct video_device *vdev = video_devdata(file);
	struct uvc_device *uvc = video_get_drvdata(vdev);
	struct uvc_video *video = &uvc->video;
	struct uvcg_format *uformat;
	struct uvcg_frame *uframe;
	u8 *fcc;

	if (fmt->type != video->queue.queue.type)
		return -EINVAL;

	fcc = (u8 *)&fmt->fmt.pix.pixelformat;
	uvcg_dbg(&uvc->func, "Trying format 0x%08x (%c%c%c%c): %ux%u\n",
		fmt->fmt.pix.pixelformat,
		fcc[0], fcc[1], fcc[2], fcc[3],
		fmt->fmt.pix.width, fmt->fmt.pix.height);

	uformat = find_format_by_pix(uvc, fmt->fmt.pix.pixelformat);
	if (!uformat)
		return -EINVAL;

	uframe = find_closest_frame_by_size(uvc, uformat,
				fmt->fmt.pix.width, fmt->fmt.pix.height);
	if (!uframe)
		return -EINVAL;

	if (uformat->type == UVCG_UNCOMPRESSED) {
		struct uvcg_uncompressed *u =
			to_uvcg_uncompressed(&uformat->group.cg_item);
		if (!u)
			return 0;

		v4l2_fill_pixfmt(&fmt->fmt.pix, fmt->fmt.pix.pixelformat,
				 uframe->frame.w_width, uframe->frame.w_height);

		if (fmt->fmt.pix.sizeimage != (uvc_v4l2_get_bytesperline(uformat, uframe) *
						uframe->frame.w_height))
			return -EINVAL;
	} else {
		fmt->fmt.pix.width = uframe->frame.w_width;
		fmt->fmt.pix.height = uframe->frame.w_height;
		fmt->fmt.pix.bytesperline = uvc_v4l2_get_bytesperline(uformat, uframe);
		fmt->fmt.pix.sizeimage = uvc_get_frame_size(uformat, uframe);
		fmt->fmt.pix.pixelformat = to_uvc_format(uformat)->fcc;
	}
	fmt->fmt.pix.field = V4L2_FIELD_NONE;
	fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
	fmt->fmt.pix.priv = 0;

	return 0;
}

static int
uvc_v4l2_set_format(struct file *file, void *fh, struct v4l2_format *fmt)
{
	struct video_device *vdev = video_devdata(file);
	struct uvc_device *uvc = video_get_drvdata(vdev);
	struct uvc_video *video = &uvc->video;
	int ret;

	ret = uvc_v4l2_try_format(file, fh, fmt);
	if (ret)
		return ret;

	video->fcc = fmt->fmt.pix.pixelformat;
	video->bpp = fmt->fmt.pix.bytesperline * 8 / video->width;
	video->width = fmt->fmt.pix.width;
	video->height = fmt->fmt.pix.height;
	video->imagesize = fmt->fmt.pix.sizeimage;

	return ret;
}

static int
uvc_v4l2_enum_frameintervals(struct file *file, void *fh,
		struct v4l2_frmivalenum *fival)
{
	struct video_device *vdev = video_devdata(file);
	struct uvc_device *uvc = video_get_drvdata(vdev);
	struct uvcg_format *uformat = NULL;
	struct uvcg_frame *uframe = NULL;
	struct uvcg_frame_ptr *frame;

	uformat = find_format_by_pix(uvc, fival->pixel_format);
	if (!uformat)
		return -EINVAL;

	list_for_each_entry(frame, &uformat->frames, entry) {
		if (frame->frm->frame.w_width == fival->width &&
		    frame->frm->frame.w_height == fival->height) {
			uframe = frame->frm;
			break;
		}
	}
	if (!uframe)
		return -EINVAL;

	if (fival->index >= uframe->frame.b_frame_interval_type)
		return -EINVAL;

	fival->discrete.numerator =
		uframe->dw_frame_interval[fival->index];

	/* TODO: handle V4L2_FRMIVAL_TYPE_STEPWISE */
	fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
	fival->discrete.denominator = 10000000;
	v4l2_simplify_fraction(&fival->discrete.numerator,
		&fival->discrete.denominator, 8, 333);

	return 0;
}

static int
uvc_v4l2_enum_framesizes(struct file *file, void *fh,
		struct v4l2_frmsizeenum *fsize)
{
	struct video_device *vdev = video_devdata(file);
	struct uvc_device *uvc = video_get_drvdata(vdev);
	struct uvcg_format *uformat = NULL;
	struct uvcg_frame *uframe = NULL;

	uformat = find_format_by_pix(uvc, fsize->pixel_format);
	if (!uformat)
		return -EINVAL;

	if (fsize->index >= uformat->num_frames)
		return -EINVAL;

	uframe = find_frame_by_index(uvc, uformat, fsize->index + 1);
	if (!uframe)
		return -EINVAL;

	fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
	fsize->discrete.width = uframe->frame.w_width;
	fsize->discrete.height = uframe->frame.w_height;

	return 0;
}

static int
uvc_v4l2_enum_format(struct file *file, void *fh, struct v4l2_fmtdesc *f)
{
	struct video_device *vdev = video_devdata(file);
	struct uvc_device *uvc = video_get_drvdata(vdev);
	const struct uvc_format_desc *fmtdesc;
	struct uvcg_format *uformat;

	if (f->index >= uvc->header->num_fmt)
		return -EINVAL;

	uformat = find_format_by_index(uvc, f->index + 1);
	if (!uformat)
		return -EINVAL;

	fmtdesc = to_uvc_format(uformat);
	f->pixelformat = fmtdesc->fcc;

	return 0;
}

static int
uvc_v4l2_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *b)
{
	struct video_device *vdev = video_devdata(file);
	struct uvc_device *uvc = video_get_drvdata(vdev);
	struct uvc_video *video = &uvc->video;

	if (b->type != video->queue.queue.type)
		return -EINVAL;

	return uvcg_alloc_buffers(&video->queue, b);
}

static int
uvc_v4l2_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
{
	struct video_device *vdev = video_devdata(file);
	struct uvc_device *uvc = video_get_drvdata(vdev);
	struct uvc_video *video = &uvc->video;

	return uvcg_query_buffer(&video->queue, b);
}

static int
uvc_v4l2_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
{
	struct video_device *vdev = video_devdata(file);
	struct uvc_device *uvc = video_get_drvdata(vdev);
	struct uvc_video *video = &uvc->video;
	int ret;

	ret = uvcg_queue_buffer(&video->queue, b);
	if (ret < 0)
		return ret;

	if (uvc->state == UVC_STATE_STREAMING)
		queue_work(video->async_wq, &video->pump);

	return ret;
}

static int
uvc_v4l2_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
{
	struct video_device *vdev = video_devdata(file);
	struct uvc_device *uvc = video_get_drvdata(vdev);
	struct uvc_video *video = &uvc->video;

	return uvcg_dequeue_buffer(&video->queue, b, file->f_flags & O_NONBLOCK);
}

static int
uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
{
	struct video_device *vdev = video_devdata(file);
	struct uvc_device *uvc = video_get_drvdata(vdev);
	struct uvc_video *video = &uvc->video;
	int ret;

	if (type != video->queue.queue.type)
		return -EINVAL;

	/* Enable UVC video. */
	ret = uvcg_video_enable(video);
	if (ret < 0)
		return ret;

	/*
	 * Complete the alternate setting selection setup phase now that
	 * userspace is ready to provide video frames.
	 */
	uvc_function_setup_continue(uvc, 0);
	uvc->state = UVC_STATE_STREAMING;

	return 0;
}

static int
uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
{
	struct video_device *vdev = video_devdata(file);
	struct uvc_device *uvc = video_get_drvdata(vdev);
	struct uvc_video *video = &uvc->video;
	int ret = 0;

	if (type != video->queue.queue.type)
		return -EINVAL;

	ret = uvcg_video_disable(video);
	if (ret < 0)
		return ret;

	uvc->state = UVC_STATE_CONNECTED;
	uvc_function_setup_continue(uvc, 1);
	return 0;
}

static int
uvc_v4l2_subscribe_event(struct v4l2_fh *fh,
			 const struct v4l2_event_subscription *sub)
{
	struct uvc_device *uvc = video_get_drvdata(fh->vdev);
	struct uvc_file_handle *handle = to_uvc_file_handle(fh);
	int ret;

	if (sub->type < UVC_EVENT_FIRST || sub->type > UVC_EVENT_LAST)
		return -EINVAL;

	if (sub->type == UVC_EVENT_SETUP && uvc->func_connected)
		return -EBUSY;

	ret = v4l2_event_subscribe(fh, sub, 2, NULL);
	if (ret < 0)
		return ret;

	if (sub->type == UVC_EVENT_SETUP) {
		uvc->func_connected = true;
		handle->is_uvc_app_handle = true;
		uvc_function_connect(uvc);
	}

	return 0;
}

static void uvc_v4l2_disable(struct uvc_device *uvc)
{
	uvc_function_disconnect(uvc);
	uvcg_video_disable(&uvc->video);
	uvcg_free_buffers(&uvc->video.queue);
	uvc->func_connected = false;
	wake_up_interruptible(&uvc->func_connected_queue);
}

static int
uvc_v4l2_unsubscribe_event(struct v4l2_fh *fh,
			   const struct v4l2_event_subscription *sub)
{
	struct uvc_device *uvc = video_get_drvdata(fh->vdev);
	struct uvc_file_handle *handle = to_uvc_file_handle(fh);
	int ret;

	ret = v4l2_event_unsubscribe(fh, sub);
	if (ret < 0)
		return ret;

	if (sub->type == UVC_EVENT_SETUP && handle->is_uvc_app_handle) {
		uvc_v4l2_disable(uvc);
		handle->is_uvc_app_handle = false;
	}

	return 0;
}

static long
uvc_v4l2_ioctl_default(struct file *file, void *fh, bool valid_prio,
		       unsigned int cmd, void *arg)
{
	struct video_device *vdev = video_devdata(file);
	struct uvc_device *uvc = video_get_drvdata(vdev);

	switch (cmd) {
	case UVCIOC_SEND_RESPONSE:
		return uvc_send_response(uvc, arg);

	default:
		return -ENOIOCTLCMD;
	}
}

const struct v4l2_ioctl_ops uvc_v4l2_ioctl_ops = {
	.vidioc_querycap = uvc_v4l2_querycap,
	.vidioc_try_fmt_vid_out = uvc_v4l2_try_format,
	.vidioc_g_fmt_vid_out = uvc_v4l2_get_format,
	.vidioc_s_fmt_vid_out = uvc_v4l2_set_format,
	.vidioc_enum_frameintervals = uvc_v4l2_enum_frameintervals,
	.vidioc_enum_framesizes = uvc_v4l2_enum_framesizes,
	.vidioc_enum_fmt_vid_out = uvc_v4l2_enum_format,
	.vidioc_reqbufs = uvc_v4l2_reqbufs,
	.vidioc_querybuf = uvc_v4l2_querybuf,
	.vidioc_qbuf = uvc_v4l2_qbuf,
	.vidioc_dqbuf = uvc_v4l2_dqbuf,
	.vidioc_streamon = uvc_v4l2_streamon,
	.vidioc_streamoff = uvc_v4l2_streamoff,
	.vidioc_subscribe_event = uvc_v4l2_subscribe_event,
	.vidioc_unsubscribe_event = uvc_v4l2_unsubscribe_event,
	.vidioc_default = uvc_v4l2_ioctl_default,
};

/* --------------------------------------------------------------------------
 * V4L2
 */

static int
uvc_v4l2_open(struct file *file)
{
	struct video_device *vdev = video_devdata(file);
	struct uvc_device *uvc = video_get_drvdata(vdev);
	struct uvc_file_handle *handle;

	handle = kzalloc(sizeof(*handle), GFP_KERNEL);
	if (handle == NULL)
		return -ENOMEM;

	v4l2_fh_init(&handle->vfh, vdev);
	v4l2_fh_add(&handle->vfh);

	handle->device = &uvc->video;
	file->private_data = &handle->vfh;

	return 0;
}

static int
uvc_v4l2_release(struct file *file)
{
	struct video_device *vdev = video_devdata(file);
	struct uvc_device *uvc = video_get_drvdata(vdev);
	struct uvc_file_handle *handle = to_uvc_file_handle(file->private_data);
	struct uvc_video *video = handle->device;

	mutex_lock(&video->mutex);
	if (handle->is_uvc_app_handle)
		uvc_v4l2_disable(uvc);
	mutex_unlock(&video->mutex);

	file->private_data = NULL;
	v4l2_fh_del(&handle->vfh);
	v4l2_fh_exit(&handle->vfh);
	kfree(handle);

	return 0;
}

static int
uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
{
	struct video_device *vdev = video_devdata(file);
	struct uvc_device *uvc = video_get_drvdata(vdev);

	return uvcg_queue_mmap(&uvc->video.queue, vma);
}

static __poll_t
uvc_v4l2_poll(struct file *file, poll_table *wait)
{
	struct video_device *vdev = video_devdata(file);
	struct uvc_device *uvc = video_get_drvdata(vdev);

	return uvcg_queue_poll(&uvc->video.queue, file, wait);
}

#ifndef CONFIG_MMU
static unsigned long uvcg_v4l2_get_unmapped_area(struct file *file,
		unsigned long addr, unsigned long len, unsigned long pgoff,
		unsigned long flags)
{
	struct video_device *vdev = video_devdata(file);
	struct uvc_device *uvc = video_get_drvdata(vdev);

	return uvcg_queue_get_unmapped_area(&uvc->video.queue, pgoff);
}
#endif

const struct v4l2_file_operations uvc_v4l2_fops = {
	.owner		= THIS_MODULE,
	.open		= uvc_v4l2_open,
	.release	= uvc_v4l2_release,
	.unlocked_ioctl	= video_ioctl2,
	.mmap		= uvc_v4l2_mmap,
	.poll		= uvc_v4l2_poll,
#ifndef CONFIG_MMU
	.get_unmapped_area = uvcg_v4l2_get_unmapped_area,
#endif
};
