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