1dacca5f0SHans Verkuil // SPDX-License-Identifier: GPL-2.0-only
2dacca5f0SHans Verkuil /*
3dacca5f0SHans Verkuil * vivid-meta-out.c - meta output support functions.
4dacca5f0SHans Verkuil */
5dacca5f0SHans Verkuil
6dacca5f0SHans Verkuil #include <linux/errno.h>
7dacca5f0SHans Verkuil #include <linux/kernel.h>
8dacca5f0SHans Verkuil #include <linux/videodev2.h>
9dacca5f0SHans Verkuil #include <media/v4l2-common.h>
10dacca5f0SHans Verkuil #include <linux/usb/video.h>
11dacca5f0SHans Verkuil
12dacca5f0SHans Verkuil #include "vivid-core.h"
13dacca5f0SHans Verkuil #include "vivid-kthread-out.h"
14dacca5f0SHans Verkuil #include "vivid-meta-out.h"
15dacca5f0SHans Verkuil
meta_out_queue_setup(struct vb2_queue * vq,unsigned int * nbuffers,unsigned int * nplanes,unsigned int sizes[],struct device * alloc_devs[])16dacca5f0SHans Verkuil static int meta_out_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers,
17dacca5f0SHans Verkuil unsigned int *nplanes, unsigned int sizes[],
18dacca5f0SHans Verkuil struct device *alloc_devs[])
19dacca5f0SHans Verkuil {
20dacca5f0SHans Verkuil struct vivid_dev *dev = vb2_get_drv_priv(vq);
21dacca5f0SHans Verkuil unsigned int size = sizeof(struct vivid_meta_out_buf);
22dacca5f0SHans Verkuil
23dacca5f0SHans Verkuil if (!vivid_is_webcam(dev))
24dacca5f0SHans Verkuil return -EINVAL;
25dacca5f0SHans Verkuil
26dacca5f0SHans Verkuil if (*nplanes) {
27dacca5f0SHans Verkuil if (sizes[0] < size)
28dacca5f0SHans Verkuil return -EINVAL;
29dacca5f0SHans Verkuil } else {
30dacca5f0SHans Verkuil sizes[0] = size;
31dacca5f0SHans Verkuil }
32dacca5f0SHans Verkuil
33dacca5f0SHans Verkuil if (vq->num_buffers + *nbuffers < 2)
34dacca5f0SHans Verkuil *nbuffers = 2 - vq->num_buffers;
35dacca5f0SHans Verkuil
36dacca5f0SHans Verkuil *nplanes = 1;
37dacca5f0SHans Verkuil return 0;
38dacca5f0SHans Verkuil }
39dacca5f0SHans Verkuil
meta_out_buf_prepare(struct vb2_buffer * vb)40dacca5f0SHans Verkuil static int meta_out_buf_prepare(struct vb2_buffer *vb)
41dacca5f0SHans Verkuil {
42dacca5f0SHans Verkuil struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
43dacca5f0SHans Verkuil unsigned int size = sizeof(struct vivid_meta_out_buf);
44dacca5f0SHans Verkuil
45dacca5f0SHans Verkuil dprintk(dev, 1, "%s\n", __func__);
46dacca5f0SHans Verkuil
47dacca5f0SHans Verkuil if (dev->buf_prepare_error) {
48dacca5f0SHans Verkuil /*
49dacca5f0SHans Verkuil * Error injection: test what happens if buf_prepare() returns
50dacca5f0SHans Verkuil * an error.
51dacca5f0SHans Verkuil */
52dacca5f0SHans Verkuil dev->buf_prepare_error = false;
53dacca5f0SHans Verkuil return -EINVAL;
54dacca5f0SHans Verkuil }
55dacca5f0SHans Verkuil if (vb2_plane_size(vb, 0) < size) {
56dacca5f0SHans Verkuil dprintk(dev, 1, "%s data will not fit into plane (%lu < %u)\n",
57dacca5f0SHans Verkuil __func__, vb2_plane_size(vb, 0), size);
58dacca5f0SHans Verkuil return -EINVAL;
59dacca5f0SHans Verkuil }
60dacca5f0SHans Verkuil vb2_set_plane_payload(vb, 0, size);
61dacca5f0SHans Verkuil
62dacca5f0SHans Verkuil return 0;
63dacca5f0SHans Verkuil }
64dacca5f0SHans Verkuil
meta_out_buf_queue(struct vb2_buffer * vb)65dacca5f0SHans Verkuil static void meta_out_buf_queue(struct vb2_buffer *vb)
66dacca5f0SHans Verkuil {
67dacca5f0SHans Verkuil struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
68dacca5f0SHans Verkuil struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
69dacca5f0SHans Verkuil struct vivid_buffer *buf = container_of(vbuf, struct vivid_buffer, vb);
70dacca5f0SHans Verkuil
71dacca5f0SHans Verkuil dprintk(dev, 1, "%s\n", __func__);
72dacca5f0SHans Verkuil
73dacca5f0SHans Verkuil spin_lock(&dev->slock);
74dacca5f0SHans Verkuil list_add_tail(&buf->list, &dev->meta_out_active);
75dacca5f0SHans Verkuil spin_unlock(&dev->slock);
76dacca5f0SHans Verkuil }
77dacca5f0SHans Verkuil
meta_out_start_streaming(struct vb2_queue * vq,unsigned int count)78dacca5f0SHans Verkuil static int meta_out_start_streaming(struct vb2_queue *vq, unsigned int count)
79dacca5f0SHans Verkuil {
80dacca5f0SHans Verkuil struct vivid_dev *dev = vb2_get_drv_priv(vq);
81dacca5f0SHans Verkuil int err;
82dacca5f0SHans Verkuil
83dacca5f0SHans Verkuil dprintk(dev, 1, "%s\n", __func__);
84dacca5f0SHans Verkuil dev->meta_out_seq_count = 0;
85dacca5f0SHans Verkuil if (dev->start_streaming_error) {
86dacca5f0SHans Verkuil dev->start_streaming_error = false;
87dacca5f0SHans Verkuil err = -EINVAL;
88dacca5f0SHans Verkuil } else {
89dacca5f0SHans Verkuil err = vivid_start_generating_vid_out(dev,
90dacca5f0SHans Verkuil &dev->meta_out_streaming);
91dacca5f0SHans Verkuil }
92dacca5f0SHans Verkuil if (err) {
93dacca5f0SHans Verkuil struct vivid_buffer *buf, *tmp;
94dacca5f0SHans Verkuil
95dacca5f0SHans Verkuil list_for_each_entry_safe(buf, tmp,
96dacca5f0SHans Verkuil &dev->meta_out_active, list) {
97dacca5f0SHans Verkuil list_del(&buf->list);
98dacca5f0SHans Verkuil vb2_buffer_done(&buf->vb.vb2_buf,
99dacca5f0SHans Verkuil VB2_BUF_STATE_QUEUED);
100dacca5f0SHans Verkuil }
101dacca5f0SHans Verkuil }
102dacca5f0SHans Verkuil return err;
103dacca5f0SHans Verkuil }
104dacca5f0SHans Verkuil
105dacca5f0SHans Verkuil /* abort streaming and wait for last buffer */
meta_out_stop_streaming(struct vb2_queue * vq)106dacca5f0SHans Verkuil static void meta_out_stop_streaming(struct vb2_queue *vq)
107dacca5f0SHans Verkuil {
108dacca5f0SHans Verkuil struct vivid_dev *dev = vb2_get_drv_priv(vq);
109dacca5f0SHans Verkuil
110dacca5f0SHans Verkuil dprintk(dev, 1, "%s\n", __func__);
111dacca5f0SHans Verkuil vivid_stop_generating_vid_out(dev, &dev->meta_out_streaming);
112dacca5f0SHans Verkuil }
113dacca5f0SHans Verkuil
meta_out_buf_request_complete(struct vb2_buffer * vb)114dacca5f0SHans Verkuil static void meta_out_buf_request_complete(struct vb2_buffer *vb)
115dacca5f0SHans Verkuil {
116dacca5f0SHans Verkuil struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
117dacca5f0SHans Verkuil
118dacca5f0SHans Verkuil v4l2_ctrl_request_complete(vb->req_obj.req, &dev->ctrl_hdl_meta_out);
119dacca5f0SHans Verkuil }
120dacca5f0SHans Verkuil
121dacca5f0SHans Verkuil const struct vb2_ops vivid_meta_out_qops = {
122dacca5f0SHans Verkuil .queue_setup = meta_out_queue_setup,
123dacca5f0SHans Verkuil .buf_prepare = meta_out_buf_prepare,
124dacca5f0SHans Verkuil .buf_queue = meta_out_buf_queue,
125dacca5f0SHans Verkuil .start_streaming = meta_out_start_streaming,
126dacca5f0SHans Verkuil .stop_streaming = meta_out_stop_streaming,
127dacca5f0SHans Verkuil .buf_request_complete = meta_out_buf_request_complete,
128dacca5f0SHans Verkuil .wait_prepare = vb2_ops_wait_prepare,
129dacca5f0SHans Verkuil .wait_finish = vb2_ops_wait_finish,
130dacca5f0SHans Verkuil };
131dacca5f0SHans Verkuil
vidioc_enum_fmt_meta_out(struct file * file,void * priv,struct v4l2_fmtdesc * f)132dacca5f0SHans Verkuil int vidioc_enum_fmt_meta_out(struct file *file, void *priv,
133dacca5f0SHans Verkuil struct v4l2_fmtdesc *f)
134dacca5f0SHans Verkuil {
135dacca5f0SHans Verkuil struct vivid_dev *dev = video_drvdata(file);
136dacca5f0SHans Verkuil
137dacca5f0SHans Verkuil if (!vivid_is_webcam(dev))
138dacca5f0SHans Verkuil return -EINVAL;
139dacca5f0SHans Verkuil
140dacca5f0SHans Verkuil if (f->index > 0)
141dacca5f0SHans Verkuil return -EINVAL;
142dacca5f0SHans Verkuil
143dacca5f0SHans Verkuil f->type = V4L2_BUF_TYPE_META_OUTPUT;
144dacca5f0SHans Verkuil f->pixelformat = V4L2_META_FMT_VIVID;
145dacca5f0SHans Verkuil return 0;
146dacca5f0SHans Verkuil }
147dacca5f0SHans Verkuil
vidioc_g_fmt_meta_out(struct file * file,void * priv,struct v4l2_format * f)148dacca5f0SHans Verkuil int vidioc_g_fmt_meta_out(struct file *file, void *priv,
149dacca5f0SHans Verkuil struct v4l2_format *f)
150dacca5f0SHans Verkuil {
151dacca5f0SHans Verkuil struct vivid_dev *dev = video_drvdata(file);
152dacca5f0SHans Verkuil struct v4l2_meta_format *meta = &f->fmt.meta;
153dacca5f0SHans Verkuil
154dacca5f0SHans Verkuil if (!vivid_is_webcam(dev) || !dev->has_meta_out)
155dacca5f0SHans Verkuil return -EINVAL;
156dacca5f0SHans Verkuil
157dacca5f0SHans Verkuil meta->dataformat = V4L2_META_FMT_VIVID;
158dacca5f0SHans Verkuil meta->buffersize = sizeof(struct vivid_meta_out_buf);
159dacca5f0SHans Verkuil return 0;
160dacca5f0SHans Verkuil }
161dacca5f0SHans Verkuil
vivid_meta_out_process(struct vivid_dev * dev,struct vivid_buffer * buf)162dacca5f0SHans Verkuil void vivid_meta_out_process(struct vivid_dev *dev,
163dacca5f0SHans Verkuil struct vivid_buffer *buf)
164dacca5f0SHans Verkuil {
165dacca5f0SHans Verkuil struct vivid_meta_out_buf *meta = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
166dacca5f0SHans Verkuil
167*e3158a5eSPeilin Ye v4l2_ctrl_s_ctrl(dev->brightness, meta->brightness);
168*e3158a5eSPeilin Ye v4l2_ctrl_s_ctrl(dev->contrast, meta->contrast);
169*e3158a5eSPeilin Ye v4l2_ctrl_s_ctrl(dev->saturation, meta->saturation);
170*e3158a5eSPeilin Ye v4l2_ctrl_s_ctrl(dev->hue, meta->hue);
171*e3158a5eSPeilin Ye
172dacca5f0SHans Verkuil dprintk(dev, 2, " %s brightness %u contrast %u saturation %u hue %d\n",
173dacca5f0SHans Verkuil __func__, meta->brightness, meta->contrast,
174dacca5f0SHans Verkuil meta->saturation, meta->hue);
175dacca5f0SHans Verkuil }
176