1 /*
2  *      uvc_metadata.c  --  USB Video Class driver - Metadata handling
3  *
4  *      Copyright (C) 2016
5  *          Guennadi Liakhovetski (guennadi.liakhovetski@intel.com)
6  *
7  *      This program is free software; you can redistribute it and/or modify
8  *      it under the terms of the GNU General Public License as published by
9  *      the Free Software Foundation; either version 2 of the License, or
10  *      (at your option) any later version.
11  */
12 
13 #include <linux/kernel.h>
14 #include <linux/list.h>
15 #include <linux/module.h>
16 #include <linux/usb.h>
17 #include <linux/videodev2.h>
18 
19 #include <media/v4l2-ioctl.h>
20 #include <media/videobuf2-v4l2.h>
21 #include <media/videobuf2-vmalloc.h>
22 
23 #include "uvcvideo.h"
24 
25 /* -----------------------------------------------------------------------------
26  * V4L2 ioctls
27  */
28 
29 static int uvc_meta_v4l2_querycap(struct file *file, void *fh,
30 				  struct v4l2_capability *cap)
31 {
32 	struct v4l2_fh *vfh = file->private_data;
33 	struct uvc_streaming *stream = video_get_drvdata(vfh->vdev);
34 	struct uvc_video_chain *chain = stream->chain;
35 
36 	strscpy(cap->driver, "uvcvideo", sizeof(cap->driver));
37 	strscpy(cap->card, vfh->vdev->name, sizeof(cap->card));
38 	usb_make_path(stream->dev->udev, cap->bus_info, sizeof(cap->bus_info));
39 	cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
40 			  | chain->caps;
41 
42 	return 0;
43 }
44 
45 static int uvc_meta_v4l2_get_format(struct file *file, void *fh,
46 				    struct v4l2_format *format)
47 {
48 	struct v4l2_fh *vfh = file->private_data;
49 	struct uvc_streaming *stream = video_get_drvdata(vfh->vdev);
50 	struct v4l2_meta_format *fmt = &format->fmt.meta;
51 
52 	if (format->type != vfh->vdev->queue->type)
53 		return -EINVAL;
54 
55 	memset(fmt, 0, sizeof(*fmt));
56 
57 	fmt->dataformat = stream->meta.format;
58 	fmt->buffersize = UVC_METATADA_BUF_SIZE;
59 
60 	return 0;
61 }
62 
63 static int uvc_meta_v4l2_try_format(struct file *file, void *fh,
64 				    struct v4l2_format *format)
65 {
66 	struct v4l2_fh *vfh = file->private_data;
67 	struct uvc_streaming *stream = video_get_drvdata(vfh->vdev);
68 	struct uvc_device *dev = stream->dev;
69 	struct v4l2_meta_format *fmt = &format->fmt.meta;
70 	u32 fmeta = fmt->dataformat;
71 
72 	if (format->type != vfh->vdev->queue->type)
73 		return -EINVAL;
74 
75 	memset(fmt, 0, sizeof(*fmt));
76 
77 	fmt->dataformat = fmeta == dev->info->meta_format
78 			? fmeta : V4L2_META_FMT_UVC;
79 	fmt->buffersize = UVC_METATADA_BUF_SIZE;
80 
81 	return 0;
82 }
83 
84 static int uvc_meta_v4l2_set_format(struct file *file, void *fh,
85 				    struct v4l2_format *format)
86 {
87 	struct v4l2_fh *vfh = file->private_data;
88 	struct uvc_streaming *stream = video_get_drvdata(vfh->vdev);
89 	struct v4l2_meta_format *fmt = &format->fmt.meta;
90 	int ret;
91 
92 	ret = uvc_meta_v4l2_try_format(file, fh, format);
93 	if (ret < 0)
94 		return ret;
95 
96 	/*
97 	 * We could in principle switch at any time, also during streaming.
98 	 * Metadata buffers would still be perfectly parseable, but it's more
99 	 * consistent and cleaner to disallow that.
100 	 */
101 	mutex_lock(&stream->mutex);
102 
103 	if (uvc_queue_allocated(&stream->queue))
104 		ret = -EBUSY;
105 	else
106 		stream->meta.format = fmt->dataformat;
107 
108 	mutex_unlock(&stream->mutex);
109 
110 	return ret;
111 }
112 
113 static int uvc_meta_v4l2_enum_formats(struct file *file, void *fh,
114 				      struct v4l2_fmtdesc *fdesc)
115 {
116 	struct v4l2_fh *vfh = file->private_data;
117 	struct uvc_streaming *stream = video_get_drvdata(vfh->vdev);
118 	struct uvc_device *dev = stream->dev;
119 	u32 index = fdesc->index;
120 
121 	if (fdesc->type != vfh->vdev->queue->type ||
122 	    index > 1U || (index && !dev->info->meta_format))
123 		return -EINVAL;
124 
125 	memset(fdesc, 0, sizeof(*fdesc));
126 
127 	fdesc->type = vfh->vdev->queue->type;
128 	fdesc->index = index;
129 	fdesc->pixelformat = index ? dev->info->meta_format : V4L2_META_FMT_UVC;
130 
131 	return 0;
132 }
133 
134 static const struct v4l2_ioctl_ops uvc_meta_ioctl_ops = {
135 	.vidioc_querycap		= uvc_meta_v4l2_querycap,
136 	.vidioc_g_fmt_meta_cap		= uvc_meta_v4l2_get_format,
137 	.vidioc_s_fmt_meta_cap		= uvc_meta_v4l2_set_format,
138 	.vidioc_try_fmt_meta_cap	= uvc_meta_v4l2_try_format,
139 	.vidioc_enum_fmt_meta_cap	= uvc_meta_v4l2_enum_formats,
140 	.vidioc_reqbufs			= vb2_ioctl_reqbufs,
141 	.vidioc_querybuf		= vb2_ioctl_querybuf,
142 	.vidioc_qbuf			= vb2_ioctl_qbuf,
143 	.vidioc_dqbuf			= vb2_ioctl_dqbuf,
144 	.vidioc_create_bufs		= vb2_ioctl_create_bufs,
145 	.vidioc_prepare_buf		= vb2_ioctl_prepare_buf,
146 	.vidioc_streamon		= vb2_ioctl_streamon,
147 	.vidioc_streamoff		= vb2_ioctl_streamoff,
148 };
149 
150 /* -----------------------------------------------------------------------------
151  * V4L2 File Operations
152  */
153 
154 static const struct v4l2_file_operations uvc_meta_fops = {
155 	.owner = THIS_MODULE,
156 	.unlocked_ioctl = video_ioctl2,
157 	.open = v4l2_fh_open,
158 	.release = vb2_fop_release,
159 	.poll = vb2_fop_poll,
160 	.mmap = vb2_fop_mmap,
161 };
162 
163 int uvc_meta_register(struct uvc_streaming *stream)
164 {
165 	struct uvc_device *dev = stream->dev;
166 	struct video_device *vdev = &stream->meta.vdev;
167 	struct uvc_video_queue *queue = &stream->meta.queue;
168 
169 	stream->meta.format = V4L2_META_FMT_UVC;
170 
171 	/*
172 	 * The video interface queue uses manual locking and thus does not set
173 	 * the queue pointer. Set it manually here.
174 	 */
175 	vdev->queue = &queue->queue;
176 
177 	return uvc_register_video_device(dev, stream, vdev, queue,
178 					 V4L2_BUF_TYPE_META_CAPTURE,
179 					 &uvc_meta_fops, &uvc_meta_ioctl_ops);
180 }
181