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