1 // SPDX-License-Identifier: GPL-2.0-only
3 * vivid-meta-out.c - meta output support functions.
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>
12 #include "vivid-core.h"
13 #include "vivid-kthread-out.h"
14 #include "vivid-meta-out.h"
16 static int meta_out_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers,
17 unsigned int *nplanes, unsigned int sizes[],
18 struct device *alloc_devs[])
20 struct vivid_dev *dev = vb2_get_drv_priv(vq);
21 unsigned int size = sizeof(struct vivid_meta_out_buf);
23 if (!vivid_is_webcam(dev))
33 if (vq->num_buffers + *nbuffers < 2)
34 *nbuffers = 2 - vq->num_buffers;
40 static int meta_out_buf_prepare(struct vb2_buffer *vb)
42 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
43 unsigned int size = sizeof(struct vivid_meta_out_buf);
45 dprintk(dev, 1, "%s\n", __func__);
47 if (dev->buf_prepare_error) {
49 * Error injection: test what happens if buf_prepare() returns
52 dev->buf_prepare_error = false;
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);
60 vb2_set_plane_payload(vb, 0, size);
65 static void meta_out_buf_queue(struct vb2_buffer *vb)
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);
71 dprintk(dev, 1, "%s\n", __func__);
73 spin_lock(&dev->slock);
74 list_add_tail(&buf->list, &dev->meta_out_active);
75 spin_unlock(&dev->slock);
78 static int meta_out_start_streaming(struct vb2_queue *vq, unsigned int count)
80 struct vivid_dev *dev = vb2_get_drv_priv(vq);
83 dprintk(dev, 1, "%s\n", __func__);
84 dev->meta_out_seq_count = 0;
85 if (dev->start_streaming_error) {
86 dev->start_streaming_error = false;
89 err = vivid_start_generating_vid_out(dev,
90 &dev->meta_out_streaming);
93 struct vivid_buffer *buf, *tmp;
95 list_for_each_entry_safe(buf, tmp,
96 &dev->meta_out_active, list) {
98 vb2_buffer_done(&buf->vb.vb2_buf,
99 VB2_BUF_STATE_QUEUED);
105 /* abort streaming and wait for last buffer */
106 static void meta_out_stop_streaming(struct vb2_queue *vq)
108 struct vivid_dev *dev = vb2_get_drv_priv(vq);
110 dprintk(dev, 1, "%s\n", __func__);
111 vivid_stop_generating_vid_out(dev, &dev->meta_out_streaming);
114 static void meta_out_buf_request_complete(struct vb2_buffer *vb)
116 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
118 v4l2_ctrl_request_complete(vb->req_obj.req, &dev->ctrl_hdl_meta_out);
121 const struct vb2_ops vivid_meta_out_qops = {
122 .queue_setup = meta_out_queue_setup,
123 .buf_prepare = meta_out_buf_prepare,
124 .buf_queue = meta_out_buf_queue,
125 .start_streaming = meta_out_start_streaming,
126 .stop_streaming = meta_out_stop_streaming,
127 .buf_request_complete = meta_out_buf_request_complete,
128 .wait_prepare = vb2_ops_wait_prepare,
129 .wait_finish = vb2_ops_wait_finish,
132 int vidioc_enum_fmt_meta_out(struct file *file, void *priv,
133 struct v4l2_fmtdesc *f)
135 struct vivid_dev *dev = video_drvdata(file);
137 if (!vivid_is_webcam(dev))
143 f->type = V4L2_BUF_TYPE_META_OUTPUT;
144 f->pixelformat = V4L2_META_FMT_VIVID;
148 int vidioc_g_fmt_meta_out(struct file *file, void *priv,
149 struct v4l2_format *f)
151 struct vivid_dev *dev = video_drvdata(file);
152 struct v4l2_meta_format *meta = &f->fmt.meta;
154 if (!vivid_is_webcam(dev) || !dev->has_meta_out)
157 meta->dataformat = V4L2_META_FMT_VIVID;
158 meta->buffersize = sizeof(struct vivid_meta_out_buf);
162 void vivid_meta_out_process(struct vivid_dev *dev,
163 struct vivid_buffer *buf)
165 struct vivid_meta_out_buf *meta = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
167 v4l2_ctrl_s_ctrl(dev->brightness, meta->brightness);
168 v4l2_ctrl_s_ctrl(dev->contrast, meta->contrast);
169 v4l2_ctrl_s_ctrl(dev->saturation, meta->saturation);
170 v4l2_ctrl_s_ctrl(dev->hue, meta->hue);
172 dprintk(dev, 2, " %s brightness %u contrast %u saturation %u hue %d\n",
173 __func__, meta->brightness, meta->contrast,
174 meta->saturation, meta->hue);