xref: /linux/drivers/media/usb/uvc/uvc_metadata.c (revision bfd5bb6f90af092aa345b15cd78143956a13c2a8)
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 	strlcpy(cap->driver, "uvcvideo", sizeof(cap->driver));
37 	strlcpy(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->meta_format ? fmeta : V4L2_META_FMT_UVC;
78 	fmt->buffersize = UVC_METATADA_BUF_SIZE;
79 
80 	return 0;
81 }
82 
83 static int uvc_meta_v4l2_set_format(struct file *file, void *fh,
84 				    struct v4l2_format *format)
85 {
86 	struct v4l2_fh *vfh = file->private_data;
87 	struct uvc_streaming *stream = video_get_drvdata(vfh->vdev);
88 	struct v4l2_meta_format *fmt = &format->fmt.meta;
89 	int ret;
90 
91 	ret = uvc_meta_v4l2_try_format(file, fh, format);
92 	if (ret < 0)
93 		return ret;
94 
95 	/*
96 	 * We could in principle switch at any time, also during streaming.
97 	 * Metadata buffers would still be perfectly parseable, but it's more
98 	 * consistent and cleaner to disallow that.
99 	 */
100 	mutex_lock(&stream->mutex);
101 
102 	if (uvc_queue_allocated(&stream->queue))
103 		ret = -EBUSY;
104 	else
105 		stream->meta.format = fmt->dataformat;
106 
107 	mutex_unlock(&stream->mutex);
108 
109 	return ret;
110 }
111 
112 static int uvc_meta_v4l2_enum_formats(struct file *file, void *fh,
113 				      struct v4l2_fmtdesc *fdesc)
114 {
115 	struct v4l2_fh *vfh = file->private_data;
116 	struct uvc_streaming *stream = video_get_drvdata(vfh->vdev);
117 	struct uvc_device *dev = stream->dev;
118 	u32 index = fdesc->index;
119 
120 	if (fdesc->type != vfh->vdev->queue->type ||
121 	    index > 1U || (index && !dev->meta_format))
122 		return -EINVAL;
123 
124 	memset(fdesc, 0, sizeof(*fdesc));
125 
126 	fdesc->type = vfh->vdev->queue->type;
127 	fdesc->index = index;
128 	fdesc->pixelformat = index ? dev->meta_format : V4L2_META_FMT_UVC;
129 
130 	return 0;
131 }
132 
133 static const struct v4l2_ioctl_ops uvc_meta_ioctl_ops = {
134 	.vidioc_querycap		= uvc_meta_v4l2_querycap,
135 	.vidioc_g_fmt_meta_cap		= uvc_meta_v4l2_get_format,
136 	.vidioc_s_fmt_meta_cap		= uvc_meta_v4l2_set_format,
137 	.vidioc_try_fmt_meta_cap	= uvc_meta_v4l2_try_format,
138 	.vidioc_enum_fmt_meta_cap	= uvc_meta_v4l2_enum_formats,
139 	.vidioc_reqbufs			= vb2_ioctl_reqbufs,
140 	.vidioc_querybuf		= vb2_ioctl_querybuf,
141 	.vidioc_qbuf			= vb2_ioctl_qbuf,
142 	.vidioc_dqbuf			= vb2_ioctl_dqbuf,
143 	.vidioc_create_bufs		= vb2_ioctl_create_bufs,
144 	.vidioc_prepare_buf		= vb2_ioctl_prepare_buf,
145 	.vidioc_streamon		= vb2_ioctl_streamon,
146 	.vidioc_streamoff		= vb2_ioctl_streamoff,
147 };
148 
149 /* -----------------------------------------------------------------------------
150  * V4L2 File Operations
151  */
152 
153 static const struct v4l2_file_operations uvc_meta_fops = {
154 	.owner = THIS_MODULE,
155 	.unlocked_ioctl = video_ioctl2,
156 	.open = v4l2_fh_open,
157 	.release = vb2_fop_release,
158 	.poll = vb2_fop_poll,
159 	.mmap = vb2_fop_mmap,
160 };
161 
162 int uvc_meta_register(struct uvc_streaming *stream)
163 {
164 	struct uvc_device *dev = stream->dev;
165 	struct video_device *vdev = &stream->meta.vdev;
166 	struct uvc_video_queue *queue = &stream->meta.queue;
167 
168 	stream->meta.format = V4L2_META_FMT_UVC;
169 
170 	/*
171 	 * The video interface queue uses manual locking and thus does not set
172 	 * the queue pointer. Set it manually here.
173 	 */
174 	vdev->queue = &queue->queue;
175 
176 	return uvc_register_video_device(dev, stream, vdev, queue,
177 					 V4L2_BUF_TYPE_META_CAPTURE,
178 					 &uvc_meta_fops, &uvc_meta_ioctl_ops);
179 }
180