Hans Verkuil | 7bb70ca | 2014-08-25 07:56:49 -0300 | [diff] [blame] | 1 | /* |
| 2 | * vivid-vbi-out.c - vbi output support functions. |
| 3 | * |
| 4 | * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved. |
| 5 | * |
| 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. |
| 9 | * |
| 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. |
| 18 | */ |
| 19 | |
| 20 | #include <linux/errno.h> |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/videodev2.h> |
| 23 | #include <media/v4l2-common.h> |
| 24 | |
| 25 | #include "vivid-core.h" |
| 26 | #include "vivid-kthread-out.h" |
| 27 | #include "vivid-vbi-out.h" |
| 28 | #include "vivid-vbi-cap.h" |
| 29 | |
Hans Verkuil | df9ecb0 | 2015-10-28 00:50:37 -0200 | [diff] [blame] | 30 | static int vbi_out_queue_setup(struct vb2_queue *vq, |
Hans Verkuil | 7bb70ca | 2014-08-25 07:56:49 -0300 | [diff] [blame] | 31 | unsigned *nbuffers, unsigned *nplanes, |
| 32 | unsigned sizes[], void *alloc_ctxs[]) |
| 33 | { |
| 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); |
| 39 | |
| 40 | if (!vivid_is_svid_out(dev)) |
| 41 | return -EINVAL; |
| 42 | |
| 43 | sizes[0] = size; |
| 44 | |
| 45 | if (vq->num_buffers + *nbuffers < 2) |
| 46 | *nbuffers = 2 - vq->num_buffers; |
| 47 | |
| 48 | *nplanes = 1; |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | static int vbi_out_buf_prepare(struct vb2_buffer *vb) |
| 53 | { |
| 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); |
| 59 | |
| 60 | dprintk(dev, 1, "%s\n", __func__); |
| 61 | |
| 62 | if (dev->buf_prepare_error) { |
| 63 | /* |
| 64 | * Error injection: test what happens if buf_prepare() returns |
| 65 | * an error. |
| 66 | */ |
| 67 | dev->buf_prepare_error = false; |
| 68 | return -EINVAL; |
| 69 | } |
| 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; |
| 74 | } |
| 75 | vb2_set_plane_payload(vb, 0, size); |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | static void vbi_out_buf_queue(struct vb2_buffer *vb) |
| 81 | { |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 82 | struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); |
Hans Verkuil | 7bb70ca | 2014-08-25 07:56:49 -0300 | [diff] [blame] | 83 | struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue); |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 84 | struct vivid_buffer *buf = container_of(vbuf, struct vivid_buffer, vb); |
Hans Verkuil | 7bb70ca | 2014-08-25 07:56:49 -0300 | [diff] [blame] | 85 | |
| 86 | dprintk(dev, 1, "%s\n", __func__); |
| 87 | |
| 88 | spin_lock(&dev->slock); |
| 89 | list_add_tail(&buf->list, &dev->vbi_out_active); |
| 90 | spin_unlock(&dev->slock); |
| 91 | } |
| 92 | |
| 93 | static int vbi_out_start_streaming(struct vb2_queue *vq, unsigned count) |
| 94 | { |
| 95 | struct vivid_dev *dev = vb2_get_drv_priv(vq); |
| 96 | int err; |
| 97 | |
| 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); |
| 105 | } |
| 106 | if (err) { |
| 107 | struct vivid_buffer *buf, *tmp; |
| 108 | |
| 109 | list_for_each_entry_safe(buf, tmp, &dev->vbi_out_active, list) { |
| 110 | list_del(&buf->list); |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 111 | vb2_buffer_done(&buf->vb.vb2_buf, |
| 112 | VB2_BUF_STATE_QUEUED); |
Hans Verkuil | 7bb70ca | 2014-08-25 07:56:49 -0300 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | return err; |
| 116 | } |
| 117 | |
| 118 | /* abort streaming and wait for last buffer */ |
| 119 | static void vbi_out_stop_streaming(struct vb2_queue *vq) |
| 120 | { |
| 121 | struct vivid_dev *dev = vb2_get_drv_priv(vq); |
| 122 | |
| 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; |
| 128 | } |
| 129 | |
| 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, |
Prabhakar Lad | 6dfa513 | 2014-11-18 08:23:39 -0300 | [diff] [blame] | 136 | .wait_prepare = vb2_ops_wait_prepare, |
| 137 | .wait_finish = vb2_ops_wait_finish, |
Hans Verkuil | 7bb70ca | 2014-08-25 07:56:49 -0300 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | int vidioc_g_fmt_vbi_out(struct file *file, void *priv, |
| 141 | struct v4l2_format *f) |
| 142 | { |
| 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; |
| 146 | |
| 147 | if (!vivid_is_svid_out(dev) || !dev->has_raw_vbi_out) |
| 148 | return -EINVAL; |
| 149 | |
| 150 | vbi->sampling_rate = 25000000; |
| 151 | vbi->offset = 24; |
| 152 | vbi->samples_per_line = 1440; |
| 153 | vbi->sample_format = V4L2_PIX_FMT_GREY; |
Hans Verkuil | 62f2872 | 2014-09-20 06:11:44 -0300 | [diff] [blame] | 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; |
Hans Verkuil | 7bb70ca | 2014-08-25 07:56:49 -0300 | [diff] [blame] | 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; |
| 161 | } |
| 162 | |
| 163 | int vidioc_s_fmt_vbi_out(struct file *file, void *priv, |
| 164 | struct v4l2_format *f) |
| 165 | { |
| 166 | struct vivid_dev *dev = video_drvdata(file); |
| 167 | int ret = vidioc_g_fmt_vbi_out(file, priv, f); |
| 168 | |
| 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; |
| 176 | } |
| 177 | |
| 178 | int vidioc_g_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt) |
| 179 | { |
| 180 | struct vivid_dev *dev = video_drvdata(file); |
| 181 | struct v4l2_sliced_vbi_format *vbi = &fmt->fmt.sliced; |
| 182 | |
| 183 | if (!vivid_is_svid_out(dev) || !dev->has_sliced_vbi_out) |
| 184 | return -EINVAL; |
| 185 | |
| 186 | vivid_fill_service_lines(vbi, dev->service_set_out); |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | int vidioc_try_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt) |
| 191 | { |
| 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; |
| 196 | |
| 197 | if (!vivid_is_svid_out(dev) || !dev->has_sliced_vbi_out) |
| 198 | return -EINVAL; |
| 199 | |
Hans Verkuil | 62f2872 | 2014-09-20 06:11:44 -0300 | [diff] [blame] | 200 | service_set &= is_60hz ? V4L2_SLICED_CAPTION_525 : |
| 201 | V4L2_SLICED_WSS_625 | V4L2_SLICED_TELETEXT_B; |
Hans Verkuil | 7bb70ca | 2014-08-25 07:56:49 -0300 | [diff] [blame] | 202 | vivid_fill_service_lines(vbi, service_set); |
| 203 | return 0; |
| 204 | } |
| 205 | |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 206 | int vidioc_s_fmt_sliced_vbi_out(struct file *file, void *fh, |
| 207 | struct v4l2_format *fmt) |
Hans Verkuil | 7bb70ca | 2014-08-25 07:56:49 -0300 | [diff] [blame] | 208 | { |
| 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); |
| 212 | |
| 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; |
| 221 | } |
| 222 | |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 223 | void vivid_sliced_vbi_out_process(struct vivid_dev *dev, |
| 224 | struct vivid_buffer *buf) |
Hans Verkuil | 7bb70ca | 2014-08-25 07:56:49 -0300 | [diff] [blame] | 225 | { |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 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); |
Hans Verkuil | 7bb70ca | 2014-08-25 07:56:49 -0300 | [diff] [blame] | 230 | |
| 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]; |
| 241 | } |
| 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]; |
| 249 | } |
| 250 | break; |
| 251 | } |
| 252 | vbi++; |
| 253 | } |
| 254 | } |