1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * vivid-meta-cap.c - meta capture support functions.
4 */
5
6 #include <linux/errno.h>
7 #include <linux/kernel.h>
8 #include <linux/videodev2.h>
9 #include <media/v4l2-common.h>
10 #include <linux/usb/video.h>
11
12 #include "vivid-core.h"
13 #include "vivid-kthread-cap.h"
14 #include "vivid-meta-cap.h"
15
meta_cap_queue_setup(struct vb2_queue * vq,unsigned int * nbuffers,unsigned int * nplanes,unsigned int sizes[],struct device * alloc_devs[])16 static int meta_cap_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers,
17 unsigned int *nplanes, unsigned int sizes[],
18 struct device *alloc_devs[])
19 {
20 struct vivid_dev *dev = vb2_get_drv_priv(vq);
21 unsigned int size = sizeof(struct vivid_uvc_meta_buf);
22
23 if (!vivid_is_webcam(dev))
24 return -EINVAL;
25
26 if (*nplanes) {
27 if (sizes[0] < size)
28 return -EINVAL;
29 } else {
30 sizes[0] = size;
31 }
32
33 *nplanes = 1;
34 return 0;
35 }
36
meta_cap_buf_prepare(struct vb2_buffer * vb)37 static int meta_cap_buf_prepare(struct vb2_buffer *vb)
38 {
39 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
40 unsigned int size = sizeof(struct vivid_uvc_meta_buf);
41
42 dprintk(dev, 1, "%s\n", __func__);
43
44 if (dev->buf_prepare_error) {
45 /*
46 * Error injection: test what happens if buf_prepare() returns
47 * an error.
48 */
49 dev->buf_prepare_error = false;
50 return -EINVAL;
51 }
52 if (vb2_plane_size(vb, 0) < size) {
53 dprintk(dev, 1, "%s data will not fit into plane (%lu < %u)\n",
54 __func__, vb2_plane_size(vb, 0), size);
55 return -EINVAL;
56 }
57 vb2_set_plane_payload(vb, 0, size);
58
59 return 0;
60 }
61
meta_cap_buf_queue(struct vb2_buffer * vb)62 static void meta_cap_buf_queue(struct vb2_buffer *vb)
63 {
64 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
65 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
66 struct vivid_buffer *buf = container_of(vbuf, struct vivid_buffer, vb);
67
68 dprintk(dev, 1, "%s\n", __func__);
69
70 spin_lock(&dev->slock);
71 list_add_tail(&buf->list, &dev->meta_cap_active);
72 spin_unlock(&dev->slock);
73 }
74
meta_cap_start_streaming(struct vb2_queue * vq,unsigned int count)75 static int meta_cap_start_streaming(struct vb2_queue *vq, unsigned int count)
76 {
77 struct vivid_dev *dev = vb2_get_drv_priv(vq);
78 int err;
79
80 dprintk(dev, 1, "%s\n", __func__);
81 dev->meta_cap_seq_count = 0;
82 if (dev->start_streaming_error) {
83 dev->start_streaming_error = false;
84 err = -EINVAL;
85 } else {
86 err = vivid_start_generating_vid_cap(dev,
87 &dev->meta_cap_streaming);
88 }
89 if (err) {
90 struct vivid_buffer *buf, *tmp;
91
92 list_for_each_entry_safe(buf, tmp,
93 &dev->meta_cap_active, list) {
94 list_del(&buf->list);
95 vb2_buffer_done(&buf->vb.vb2_buf,
96 VB2_BUF_STATE_QUEUED);
97 }
98 }
99 return err;
100 }
101
102 /* abort streaming and wait for last buffer */
meta_cap_stop_streaming(struct vb2_queue * vq)103 static void meta_cap_stop_streaming(struct vb2_queue *vq)
104 {
105 struct vivid_dev *dev = vb2_get_drv_priv(vq);
106
107 dprintk(dev, 1, "%s\n", __func__);
108 vivid_stop_generating_vid_cap(dev, &dev->meta_cap_streaming);
109 }
110
meta_cap_buf_request_complete(struct vb2_buffer * vb)111 static void meta_cap_buf_request_complete(struct vb2_buffer *vb)
112 {
113 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
114
115 v4l2_ctrl_request_complete(vb->req_obj.req, &dev->ctrl_hdl_meta_cap);
116 }
117
118 const struct vb2_ops vivid_meta_cap_qops = {
119 .queue_setup = meta_cap_queue_setup,
120 .buf_prepare = meta_cap_buf_prepare,
121 .buf_queue = meta_cap_buf_queue,
122 .start_streaming = meta_cap_start_streaming,
123 .stop_streaming = meta_cap_stop_streaming,
124 .buf_request_complete = meta_cap_buf_request_complete,
125 .wait_prepare = vb2_ops_wait_prepare,
126 .wait_finish = vb2_ops_wait_finish,
127 };
128
vidioc_enum_fmt_meta_cap(struct file * file,void * priv,struct v4l2_fmtdesc * f)129 int vidioc_enum_fmt_meta_cap(struct file *file, void *priv,
130 struct v4l2_fmtdesc *f)
131 {
132 struct vivid_dev *dev = video_drvdata(file);
133
134 if (!vivid_is_webcam(dev))
135 return -EINVAL;
136
137 if (f->index > 0)
138 return -EINVAL;
139
140 f->type = V4L2_BUF_TYPE_META_CAPTURE;
141 f->pixelformat = V4L2_META_FMT_UVC;
142 return 0;
143 }
144
vidioc_g_fmt_meta_cap(struct file * file,void * priv,struct v4l2_format * f)145 int vidioc_g_fmt_meta_cap(struct file *file, void *priv,
146 struct v4l2_format *f)
147 {
148 struct vivid_dev *dev = video_drvdata(file);
149 struct v4l2_meta_format *meta = &f->fmt.meta;
150
151 if (!vivid_is_webcam(dev) || !dev->has_meta_cap)
152 return -EINVAL;
153
154 meta->dataformat = V4L2_META_FMT_UVC;
155 meta->buffersize = sizeof(struct vivid_uvc_meta_buf);
156 return 0;
157 }
158
vivid_meta_cap_fillbuff(struct vivid_dev * dev,struct vivid_buffer * buf,u64 soe)159 void vivid_meta_cap_fillbuff(struct vivid_dev *dev,
160 struct vivid_buffer *buf, u64 soe)
161 {
162 struct vivid_uvc_meta_buf *meta = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
163 int buf_off = 0;
164
165 buf->vb.sequence = dev->meta_cap_seq_count;
166 if (dev->field_cap == V4L2_FIELD_ALTERNATE)
167 buf->vb.sequence /= 2;
168 memset(meta, 1, vb2_plane_size(&buf->vb.vb2_buf, 0));
169
170 meta->ns = ktime_get_ns();
171 meta->sof = buf->vb.sequence * 30;
172 meta->length = sizeof(*meta) - offsetof(struct vivid_uvc_meta_buf, length);
173 meta->flags = UVC_STREAM_EOH | UVC_STREAM_EOF;
174
175 if ((buf->vb.sequence % 2) == 0)
176 meta->flags |= UVC_STREAM_FID;
177
178 dprintk(dev, 2, "%s ns:%llu sof:%4d len:%u flags: 0x%02x",
179 __func__, meta->ns, meta->sof, meta->length, meta->flags);
180 if (dev->meta_pts) {
181 meta->flags |= UVC_STREAM_PTS;
182 meta->buf[0] = div_u64(soe, VIVID_META_CLOCK_UNIT);
183 buf_off = 4;
184 dprintk(dev, 2, " pts: %u\n", *(__u32 *)(meta->buf));
185 }
186
187 if (dev->meta_scr) {
188 meta->flags |= UVC_STREAM_SCR;
189 meta->buf[buf_off] = div_u64((soe + dev->cap_frame_eof_offset),
190 VIVID_META_CLOCK_UNIT);
191
192 meta->buf[buf_off + 4] = (buf->vb.sequence * 30) % 1000;
193 dprintk(dev, 2, " stc: %u, sof counter: %u\n",
194 *(__u32 *)(meta->buf + buf_off),
195 *(__u16 *)(meta->buf + buf_off + 4));
196 }
197 dprintk(dev, 2, "\n");
198 }
199