Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / media / platform / vivid / vivid-vbi-out.c
blobd2989195cf03c8399380615eac9aea168ece3bca
1 /*
2 * vivid-vbi-out.c - vbi output support functions.
4 * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
6 * This program is free software; you may redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
10 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
11 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
12 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
13 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
14 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
15 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
16 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17 * SOFTWARE.
20 #include <linux/errno.h>
21 #include <linux/kernel.h>
22 #include <linux/videodev2.h>
23 #include <media/v4l2-common.h>
25 #include "vivid-core.h"
26 #include "vivid-kthread-out.h"
27 #include "vivid-vbi-out.h"
28 #include "vivid-vbi-cap.h"
30 static int vbi_out_queue_setup(struct vb2_queue *vq,
31 unsigned *nbuffers, unsigned *nplanes,
32 unsigned sizes[], struct device *alloc_devs[])
34 struct vivid_dev *dev = vb2_get_drv_priv(vq);
35 bool is_60hz = dev->std_out & V4L2_STD_525_60;
36 unsigned size = vq->type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT ?
37 36 * sizeof(struct v4l2_sliced_vbi_data) :
38 1440 * 2 * (is_60hz ? 12 : 18);
40 if (!vivid_is_svid_out(dev))
41 return -EINVAL;
43 sizes[0] = size;
45 if (vq->num_buffers + *nbuffers < 2)
46 *nbuffers = 2 - vq->num_buffers;
48 *nplanes = 1;
49 return 0;
52 static int vbi_out_buf_prepare(struct vb2_buffer *vb)
54 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
55 bool is_60hz = dev->std_out & V4L2_STD_525_60;
56 unsigned size = vb->vb2_queue->type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT ?
57 36 * sizeof(struct v4l2_sliced_vbi_data) :
58 1440 * 2 * (is_60hz ? 12 : 18);
60 dprintk(dev, 1, "%s\n", __func__);
62 if (dev->buf_prepare_error) {
64 * Error injection: test what happens if buf_prepare() returns
65 * an error.
67 dev->buf_prepare_error = false;
68 return -EINVAL;
70 if (vb2_plane_size(vb, 0) < size) {
71 dprintk(dev, 1, "%s data will not fit into plane (%lu < %u)\n",
72 __func__, vb2_plane_size(vb, 0), size);
73 return -EINVAL;
75 vb2_set_plane_payload(vb, 0, size);
77 return 0;
80 static void vbi_out_buf_queue(struct vb2_buffer *vb)
82 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
83 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
84 struct vivid_buffer *buf = container_of(vbuf, struct vivid_buffer, vb);
86 dprintk(dev, 1, "%s\n", __func__);
88 spin_lock(&dev->slock);
89 list_add_tail(&buf->list, &dev->vbi_out_active);
90 spin_unlock(&dev->slock);
93 static int vbi_out_start_streaming(struct vb2_queue *vq, unsigned count)
95 struct vivid_dev *dev = vb2_get_drv_priv(vq);
96 int err;
98 dprintk(dev, 1, "%s\n", __func__);
99 dev->vbi_out_seq_count = 0;
100 if (dev->start_streaming_error) {
101 dev->start_streaming_error = false;
102 err = -EINVAL;
103 } else {
104 err = vivid_start_generating_vid_out(dev, &dev->vbi_out_streaming);
106 if (err) {
107 struct vivid_buffer *buf, *tmp;
109 list_for_each_entry_safe(buf, tmp, &dev->vbi_out_active, list) {
110 list_del(&buf->list);
111 vb2_buffer_done(&buf->vb.vb2_buf,
112 VB2_BUF_STATE_QUEUED);
115 return err;
118 /* abort streaming and wait for last buffer */
119 static void vbi_out_stop_streaming(struct vb2_queue *vq)
121 struct vivid_dev *dev = vb2_get_drv_priv(vq);
123 dprintk(dev, 1, "%s\n", __func__);
124 vivid_stop_generating_vid_out(dev, &dev->vbi_out_streaming);
125 dev->vbi_out_have_wss = false;
126 dev->vbi_out_have_cc[0] = false;
127 dev->vbi_out_have_cc[1] = false;
130 const struct vb2_ops vivid_vbi_out_qops = {
131 .queue_setup = vbi_out_queue_setup,
132 .buf_prepare = vbi_out_buf_prepare,
133 .buf_queue = vbi_out_buf_queue,
134 .start_streaming = vbi_out_start_streaming,
135 .stop_streaming = vbi_out_stop_streaming,
136 .wait_prepare = vb2_ops_wait_prepare,
137 .wait_finish = vb2_ops_wait_finish,
140 int vidioc_g_fmt_vbi_out(struct file *file, void *priv,
141 struct v4l2_format *f)
143 struct vivid_dev *dev = video_drvdata(file);
144 struct v4l2_vbi_format *vbi = &f->fmt.vbi;
145 bool is_60hz = dev->std_out & V4L2_STD_525_60;
147 if (!vivid_is_svid_out(dev) || !dev->has_raw_vbi_out)
148 return -EINVAL;
150 vbi->sampling_rate = 25000000;
151 vbi->offset = 24;
152 vbi->samples_per_line = 1440;
153 vbi->sample_format = V4L2_PIX_FMT_GREY;
154 vbi->start[0] = is_60hz ? V4L2_VBI_ITU_525_F1_START + 9 : V4L2_VBI_ITU_625_F1_START + 5;
155 vbi->start[1] = is_60hz ? V4L2_VBI_ITU_525_F2_START + 9 : V4L2_VBI_ITU_625_F2_START + 5;
156 vbi->count[0] = vbi->count[1] = is_60hz ? 12 : 18;
157 vbi->flags = dev->vbi_cap_interlaced ? V4L2_VBI_INTERLACED : 0;
158 vbi->reserved[0] = 0;
159 vbi->reserved[1] = 0;
160 return 0;
163 int vidioc_s_fmt_vbi_out(struct file *file, void *priv,
164 struct v4l2_format *f)
166 struct vivid_dev *dev = video_drvdata(file);
167 int ret = vidioc_g_fmt_vbi_out(file, priv, f);
169 if (ret)
170 return ret;
171 if (vb2_is_busy(&dev->vb_vbi_out_q))
172 return -EBUSY;
173 dev->stream_sliced_vbi_out = false;
174 dev->vbi_out_dev.queue->type = V4L2_BUF_TYPE_VBI_OUTPUT;
175 return 0;
178 int vidioc_g_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
180 struct vivid_dev *dev = video_drvdata(file);
181 struct v4l2_sliced_vbi_format *vbi = &fmt->fmt.sliced;
183 if (!vivid_is_svid_out(dev) || !dev->has_sliced_vbi_out)
184 return -EINVAL;
186 vivid_fill_service_lines(vbi, dev->service_set_out);
187 return 0;
190 int vidioc_try_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
192 struct vivid_dev *dev = video_drvdata(file);
193 struct v4l2_sliced_vbi_format *vbi = &fmt->fmt.sliced;
194 bool is_60hz = dev->std_out & V4L2_STD_525_60;
195 u32 service_set = vbi->service_set;
197 if (!vivid_is_svid_out(dev) || !dev->has_sliced_vbi_out)
198 return -EINVAL;
200 service_set &= is_60hz ? V4L2_SLICED_CAPTION_525 :
201 V4L2_SLICED_WSS_625 | V4L2_SLICED_TELETEXT_B;
202 vivid_fill_service_lines(vbi, service_set);
203 return 0;
206 int vidioc_s_fmt_sliced_vbi_out(struct file *file, void *fh,
207 struct v4l2_format *fmt)
209 struct vivid_dev *dev = video_drvdata(file);
210 struct v4l2_sliced_vbi_format *vbi = &fmt->fmt.sliced;
211 int ret = vidioc_try_fmt_sliced_vbi_out(file, fh, fmt);
213 if (ret)
214 return ret;
215 if (vb2_is_busy(&dev->vb_vbi_out_q))
216 return -EBUSY;
217 dev->service_set_out = vbi->service_set;
218 dev->stream_sliced_vbi_out = true;
219 dev->vbi_out_dev.queue->type = V4L2_BUF_TYPE_SLICED_VBI_OUTPUT;
220 return 0;
223 void vivid_sliced_vbi_out_process(struct vivid_dev *dev,
224 struct vivid_buffer *buf)
226 struct v4l2_sliced_vbi_data *vbi =
227 vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
228 unsigned elems =
229 vb2_get_plane_payload(&buf->vb.vb2_buf, 0) / sizeof(*vbi);
231 dev->vbi_out_have_cc[0] = false;
232 dev->vbi_out_have_cc[1] = false;
233 dev->vbi_out_have_wss = false;
234 while (elems--) {
235 switch (vbi->id) {
236 case V4L2_SLICED_CAPTION_525:
237 if ((dev->std_out & V4L2_STD_525_60) && vbi->line == 21) {
238 dev->vbi_out_have_cc[!!vbi->field] = true;
239 dev->vbi_out_cc[!!vbi->field][0] = vbi->data[0];
240 dev->vbi_out_cc[!!vbi->field][1] = vbi->data[1];
242 break;
243 case V4L2_SLICED_WSS_625:
244 if ((dev->std_out & V4L2_STD_625_50) &&
245 vbi->field == 0 && vbi->line == 23) {
246 dev->vbi_out_have_wss = true;
247 dev->vbi_out_wss[0] = vbi->data[0];
248 dev->vbi_out_wss[1] = vbi->data[1];
250 break;
252 vbi++;