2 * uvc_metadata.c -- USB Video Class driver - Metadata handling
5 * Guennadi Liakhovetski (guennadi.liakhovetski@intel.com)
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.
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>
19 #include <media/v4l2-ioctl.h>
20 #include <media/videobuf2-v4l2.h>
21 #include <media/videobuf2-vmalloc.h>
25 /* -----------------------------------------------------------------------------
29 static int uvc_meta_v4l2_querycap(struct file *file, void *fh,
30 struct v4l2_capability *cap)
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;
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
45 static int uvc_meta_v4l2_get_format(struct file *file, void *fh,
46 struct v4l2_format *format)
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;
52 if (format->type != vfh->vdev->queue->type)
55 memset(fmt, 0, sizeof(*fmt));
57 fmt->dataformat = stream->meta.format;
58 fmt->buffersize = UVC_METATADA_BUF_SIZE;
63 static int uvc_meta_v4l2_try_format(struct file *file, void *fh,
64 struct v4l2_format *format)
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;
72 if (format->type != vfh->vdev->queue->type)
75 memset(fmt, 0, sizeof(*fmt));
77 fmt->dataformat = fmeta == dev->meta_format ? fmeta : V4L2_META_FMT_UVC;
78 fmt->buffersize = UVC_METATADA_BUF_SIZE;
83 static int uvc_meta_v4l2_set_format(struct file *file, void *fh,
84 struct v4l2_format *format)
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;
91 ret = uvc_meta_v4l2_try_format(file, fh, format);
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.
100 mutex_lock(&stream->mutex);
102 if (uvc_queue_allocated(&stream->queue))
105 stream->meta.format = fmt->dataformat;
107 mutex_unlock(&stream->mutex);
112 static int uvc_meta_v4l2_enum_formats(struct file *file, void *fh,
113 struct v4l2_fmtdesc *fdesc)
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;
120 if (fdesc->type != vfh->vdev->queue->type ||
121 index > 1U || (index && !dev->meta_format))
124 memset(fdesc, 0, sizeof(*fdesc));
126 fdesc->type = vfh->vdev->queue->type;
127 fdesc->index = index;
128 fdesc->pixelformat = index ? dev->meta_format : V4L2_META_FMT_UVC;
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,
149 /* -----------------------------------------------------------------------------
150 * V4L2 File Operations
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,
162 int uvc_meta_register(struct uvc_streaming *stream)
164 struct uvc_device *dev = stream->dev;
165 struct video_device *vdev = &stream->meta.vdev;
166 struct uvc_video_queue *queue = &stream->meta.queue;
168 stream->meta.format = V4L2_META_FMT_UVC;
171 * The video interface queue uses manual locking and thus does not set
172 * the queue pointer. Set it manually here.
174 vdev->queue = &queue->queue;
176 return uvc_register_video_device(dev, stream, vdev, queue,
177 V4L2_BUF_TYPE_META_CAPTURE,
178 &uvc_meta_fops, &uvc_meta_ioctl_ops);