linux/drivers/media/platform/vivid/vivid-vbi-cap.c
<<
>>
Prefs
   1/*
   2 * vivid-vbi-cap.c - vbi capture 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-cap.h"
  27#include "vivid-vbi-cap.h"
  28#include "vivid-vbi-gen.h"
  29
  30static void vivid_sliced_vbi_cap_fill(struct vivid_dev *dev, unsigned seqnr)
  31{
  32        struct vivid_vbi_gen_data *vbi_gen = &dev->vbi_gen;
  33        bool is_60hz = dev->std_cap & V4L2_STD_525_60;
  34
  35        vivid_vbi_gen_sliced(vbi_gen, is_60hz, seqnr);
  36
  37        if (!is_60hz) {
  38                if (dev->loop_video) {
  39                        if (dev->vbi_out_have_wss) {
  40                                vbi_gen->data[12].data[0] = dev->vbi_out_wss[0];
  41                                vbi_gen->data[12].data[1] = dev->vbi_out_wss[1];
  42                        } else {
  43                                vbi_gen->data[12].id = 0;
  44                        }
  45                } else {
  46                        switch (tpg_g_video_aspect(&dev->tpg)) {
  47                        case TPG_VIDEO_ASPECT_14X9_CENTRE:
  48                                vbi_gen->data[12].data[0] = 0x01;
  49                                break;
  50                        case TPG_VIDEO_ASPECT_16X9_CENTRE:
  51                                vbi_gen->data[12].data[0] = 0x0b;
  52                                break;
  53                        case TPG_VIDEO_ASPECT_16X9_ANAMORPHIC:
  54                                vbi_gen->data[12].data[0] = 0x07;
  55                                break;
  56                        case TPG_VIDEO_ASPECT_4X3:
  57                        default:
  58                                vbi_gen->data[12].data[0] = 0x08;
  59                                break;
  60                        }
  61                }
  62        } else if (dev->loop_video && is_60hz) {
  63                if (dev->vbi_out_have_cc[0]) {
  64                        vbi_gen->data[0].data[0] = dev->vbi_out_cc[0][0];
  65                        vbi_gen->data[0].data[1] = dev->vbi_out_cc[0][1];
  66                } else {
  67                        vbi_gen->data[0].id = 0;
  68                }
  69                if (dev->vbi_out_have_cc[1]) {
  70                        vbi_gen->data[1].data[0] = dev->vbi_out_cc[1][0];
  71                        vbi_gen->data[1].data[1] = dev->vbi_out_cc[1][1];
  72                } else {
  73                        vbi_gen->data[1].id = 0;
  74                }
  75        }
  76}
  77
  78static void vivid_g_fmt_vbi_cap(struct vivid_dev *dev, struct v4l2_vbi_format *vbi)
  79{
  80        bool is_60hz = dev->std_cap & V4L2_STD_525_60;
  81
  82        vbi->sampling_rate = 27000000;
  83        vbi->offset = 24;
  84        vbi->samples_per_line = 1440;
  85        vbi->sample_format = V4L2_PIX_FMT_GREY;
  86        vbi->start[0] = is_60hz ? V4L2_VBI_ITU_525_F1_START + 9 : V4L2_VBI_ITU_625_F1_START + 5;
  87        vbi->start[1] = is_60hz ? V4L2_VBI_ITU_525_F2_START + 9 : V4L2_VBI_ITU_625_F2_START + 5;
  88        vbi->count[0] = vbi->count[1] = is_60hz ? 12 : 18;
  89        vbi->flags = dev->vbi_cap_interlaced ? V4L2_VBI_INTERLACED : 0;
  90        vbi->reserved[0] = 0;
  91        vbi->reserved[1] = 0;
  92}
  93
  94void vivid_raw_vbi_cap_process(struct vivid_dev *dev, struct vivid_buffer *buf)
  95{
  96        struct v4l2_vbi_format vbi;
  97        u8 *vbuf = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
  98
  99        vivid_g_fmt_vbi_cap(dev, &vbi);
 100        buf->vb.sequence = dev->vbi_cap_seq_count;
 101        if (dev->field_cap == V4L2_FIELD_ALTERNATE)
 102                buf->vb.sequence /= 2;
 103
 104        vivid_sliced_vbi_cap_fill(dev, buf->vb.sequence);
 105
 106        memset(vbuf, 0x10, vb2_plane_size(&buf->vb.vb2_buf, 0));
 107
 108        if (!VIVID_INVALID_SIGNAL(dev->std_signal_mode))
 109                vivid_vbi_gen_raw(&dev->vbi_gen, &vbi, vbuf);
 110
 111        buf->vb.vb2_buf.timestamp = ktime_get_ns() + dev->time_wrap_offset;
 112}
 113
 114
 115void vivid_sliced_vbi_cap_process(struct vivid_dev *dev,
 116                        struct vivid_buffer *buf)
 117{
 118        struct v4l2_sliced_vbi_data *vbuf =
 119                        vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
 120
 121        buf->vb.sequence = dev->vbi_cap_seq_count;
 122        if (dev->field_cap == V4L2_FIELD_ALTERNATE)
 123                buf->vb.sequence /= 2;
 124
 125        vivid_sliced_vbi_cap_fill(dev, buf->vb.sequence);
 126
 127        memset(vbuf, 0, vb2_plane_size(&buf->vb.vb2_buf, 0));
 128        if (!VIVID_INVALID_SIGNAL(dev->std_signal_mode)) {
 129                unsigned i;
 130
 131                for (i = 0; i < 25; i++)
 132                        vbuf[i] = dev->vbi_gen.data[i];
 133        }
 134
 135        buf->vb.vb2_buf.timestamp = ktime_get_ns() + dev->time_wrap_offset;
 136}
 137
 138static int vbi_cap_queue_setup(struct vb2_queue *vq,
 139                       unsigned *nbuffers, unsigned *nplanes,
 140                       unsigned sizes[], void *alloc_ctxs[])
 141{
 142        struct vivid_dev *dev = vb2_get_drv_priv(vq);
 143        bool is_60hz = dev->std_cap & V4L2_STD_525_60;
 144        unsigned size = vq->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE ?
 145                36 * sizeof(struct v4l2_sliced_vbi_data) :
 146                1440 * 2 * (is_60hz ? 12 : 18);
 147
 148        if (!vivid_is_sdtv_cap(dev))
 149                return -EINVAL;
 150
 151        sizes[0] = size;
 152
 153        if (vq->num_buffers + *nbuffers < 2)
 154                *nbuffers = 2 - vq->num_buffers;
 155
 156        *nplanes = 1;
 157        return 0;
 158}
 159
 160static int vbi_cap_buf_prepare(struct vb2_buffer *vb)
 161{
 162        struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
 163        bool is_60hz = dev->std_cap & V4L2_STD_525_60;
 164        unsigned size = vb->vb2_queue->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE ?
 165                36 * sizeof(struct v4l2_sliced_vbi_data) :
 166                1440 * 2 * (is_60hz ? 12 : 18);
 167
 168        dprintk(dev, 1, "%s\n", __func__);
 169
 170        if (dev->buf_prepare_error) {
 171                /*
 172                 * Error injection: test what happens if buf_prepare() returns
 173                 * an error.
 174                 */
 175                dev->buf_prepare_error = false;
 176                return -EINVAL;
 177        }
 178        if (vb2_plane_size(vb, 0) < size) {
 179                dprintk(dev, 1, "%s data will not fit into plane (%lu < %u)\n",
 180                                __func__, vb2_plane_size(vb, 0), size);
 181                return -EINVAL;
 182        }
 183        vb2_set_plane_payload(vb, 0, size);
 184
 185        return 0;
 186}
 187
 188static void vbi_cap_buf_queue(struct vb2_buffer *vb)
 189{
 190        struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
 191        struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
 192        struct vivid_buffer *buf = container_of(vbuf, struct vivid_buffer, vb);
 193
 194        dprintk(dev, 1, "%s\n", __func__);
 195
 196        spin_lock(&dev->slock);
 197        list_add_tail(&buf->list, &dev->vbi_cap_active);
 198        spin_unlock(&dev->slock);
 199}
 200
 201static int vbi_cap_start_streaming(struct vb2_queue *vq, unsigned count)
 202{
 203        struct vivid_dev *dev = vb2_get_drv_priv(vq);
 204        int err;
 205
 206        dprintk(dev, 1, "%s\n", __func__);
 207        dev->vbi_cap_seq_count = 0;
 208        if (dev->start_streaming_error) {
 209                dev->start_streaming_error = false;
 210                err = -EINVAL;
 211        } else {
 212                err = vivid_start_generating_vid_cap(dev, &dev->vbi_cap_streaming);
 213        }
 214        if (err) {
 215                struct vivid_buffer *buf, *tmp;
 216
 217                list_for_each_entry_safe(buf, tmp, &dev->vbi_cap_active, list) {
 218                        list_del(&buf->list);
 219                        vb2_buffer_done(&buf->vb.vb2_buf,
 220                                        VB2_BUF_STATE_QUEUED);
 221                }
 222        }
 223        return err;
 224}
 225
 226/* abort streaming and wait for last buffer */
 227static void vbi_cap_stop_streaming(struct vb2_queue *vq)
 228{
 229        struct vivid_dev *dev = vb2_get_drv_priv(vq);
 230
 231        dprintk(dev, 1, "%s\n", __func__);
 232        vivid_stop_generating_vid_cap(dev, &dev->vbi_cap_streaming);
 233}
 234
 235const struct vb2_ops vivid_vbi_cap_qops = {
 236        .queue_setup            = vbi_cap_queue_setup,
 237        .buf_prepare            = vbi_cap_buf_prepare,
 238        .buf_queue              = vbi_cap_buf_queue,
 239        .start_streaming        = vbi_cap_start_streaming,
 240        .stop_streaming         = vbi_cap_stop_streaming,
 241        .wait_prepare           = vb2_ops_wait_prepare,
 242        .wait_finish            = vb2_ops_wait_finish,
 243};
 244
 245int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
 246                                        struct v4l2_format *f)
 247{
 248        struct vivid_dev *dev = video_drvdata(file);
 249        struct v4l2_vbi_format *vbi = &f->fmt.vbi;
 250
 251        if (!vivid_is_sdtv_cap(dev) || !dev->has_raw_vbi_cap)
 252                return -EINVAL;
 253
 254        vivid_g_fmt_vbi_cap(dev, vbi);
 255        return 0;
 256}
 257
 258int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
 259                                        struct v4l2_format *f)
 260{
 261        struct vivid_dev *dev = video_drvdata(file);
 262        int ret = vidioc_g_fmt_vbi_cap(file, priv, f);
 263
 264        if (ret)
 265                return ret;
 266        if (dev->stream_sliced_vbi_cap && vb2_is_busy(&dev->vb_vbi_cap_q))
 267                return -EBUSY;
 268        dev->stream_sliced_vbi_cap = false;
 269        dev->vbi_cap_dev.queue->type = V4L2_BUF_TYPE_VBI_CAPTURE;
 270        return 0;
 271}
 272
 273void vivid_fill_service_lines(struct v4l2_sliced_vbi_format *vbi, u32 service_set)
 274{
 275        vbi->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
 276        vbi->service_set = service_set;
 277        memset(vbi->service_lines, 0, sizeof(vbi->service_lines));
 278        memset(vbi->reserved, 0, sizeof(vbi->reserved));
 279
 280        if (vbi->service_set == 0)
 281                return;
 282
 283        if (vbi->service_set & V4L2_SLICED_CAPTION_525) {
 284                vbi->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
 285                vbi->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
 286        }
 287        if (vbi->service_set & V4L2_SLICED_WSS_625) {
 288                unsigned i;
 289
 290                for (i = 7; i <= 18; i++)
 291                        vbi->service_lines[0][i] =
 292                        vbi->service_lines[1][i] = V4L2_SLICED_TELETEXT_B;
 293                vbi->service_lines[0][23] = V4L2_SLICED_WSS_625;
 294        }
 295}
 296
 297int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
 298{
 299        struct vivid_dev *dev = video_drvdata(file);
 300        struct v4l2_sliced_vbi_format *vbi = &fmt->fmt.sliced;
 301
 302        if (!vivid_is_sdtv_cap(dev) || !dev->has_sliced_vbi_cap)
 303                return -EINVAL;
 304
 305        vivid_fill_service_lines(vbi, dev->service_set_cap);
 306        return 0;
 307}
 308
 309int vidioc_try_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
 310{
 311        struct vivid_dev *dev = video_drvdata(file);
 312        struct v4l2_sliced_vbi_format *vbi = &fmt->fmt.sliced;
 313        bool is_60hz = dev->std_cap & V4L2_STD_525_60;
 314        u32 service_set = vbi->service_set;
 315
 316        if (!vivid_is_sdtv_cap(dev) || !dev->has_sliced_vbi_cap)
 317                return -EINVAL;
 318
 319        service_set &= is_60hz ? V4L2_SLICED_CAPTION_525 :
 320                                 V4L2_SLICED_WSS_625 | V4L2_SLICED_TELETEXT_B;
 321        vivid_fill_service_lines(vbi, service_set);
 322        return 0;
 323}
 324
 325int vidioc_s_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
 326{
 327        struct vivid_dev *dev = video_drvdata(file);
 328        struct v4l2_sliced_vbi_format *vbi = &fmt->fmt.sliced;
 329        int ret = vidioc_try_fmt_sliced_vbi_cap(file, fh, fmt);
 330
 331        if (ret)
 332                return ret;
 333        if (!dev->stream_sliced_vbi_cap && vb2_is_busy(&dev->vb_vbi_cap_q))
 334                return -EBUSY;
 335        dev->service_set_cap = vbi->service_set;
 336        dev->stream_sliced_vbi_cap = true;
 337        dev->vbi_cap_dev.queue->type = V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
 338        return 0;
 339}
 340
 341int vidioc_g_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_sliced_vbi_cap *cap)
 342{
 343        struct vivid_dev *dev = video_drvdata(file);
 344        struct video_device *vdev = video_devdata(file);
 345        bool is_60hz;
 346
 347        if (vdev->vfl_dir == VFL_DIR_RX) {
 348                is_60hz = dev->std_cap & V4L2_STD_525_60;
 349                if (!vivid_is_sdtv_cap(dev) || !dev->has_sliced_vbi_cap ||
 350                    cap->type != V4L2_BUF_TYPE_SLICED_VBI_CAPTURE)
 351                        return -EINVAL;
 352        } else {
 353                is_60hz = dev->std_out & V4L2_STD_525_60;
 354                if (!vivid_is_svid_out(dev) || !dev->has_sliced_vbi_out ||
 355                    cap->type != V4L2_BUF_TYPE_SLICED_VBI_OUTPUT)
 356                        return -EINVAL;
 357        }
 358
 359        cap->service_set = is_60hz ? V4L2_SLICED_CAPTION_525 :
 360                                     V4L2_SLICED_WSS_625 | V4L2_SLICED_TELETEXT_B;
 361        if (is_60hz) {
 362                cap->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
 363                cap->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
 364        } else {
 365                unsigned i;
 366
 367                for (i = 7; i <= 18; i++)
 368                        cap->service_lines[0][i] =
 369                        cap->service_lines[1][i] = V4L2_SLICED_TELETEXT_B;
 370                cap->service_lines[0][23] = V4L2_SLICED_WSS_625;
 371        }
 372        return 0;
 373}
 374