linux/drivers/media/pci/ivtv/ivtv-streams.c
<<
>>
Prefs
   1/*
   2    init/start/stop/exit stream functions
   3    Copyright (C) 2003-2004  Kevin Thayer <nufan_wfk at yahoo.com>
   4    Copyright (C) 2004  Chris Kennedy <c@groovy.org>
   5    Copyright (C) 2005-2007  Hans Verkuil <hverkuil@xs4all.nl>
   6
   7    This program is free software; you can redistribute it and/or modify
   8    it under the terms of the GNU General Public License as published by
   9    the Free Software Foundation; either version 2 of the License, or
  10    (at your option) any later version.
  11
  12    This program is distributed in the hope that it will be useful,
  13    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15    GNU General Public License for more details.
  16
  17    You should have received a copy of the GNU General Public License
  18    along with this program; if not, write to the Free Software
  19    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  20 */
  21
  22/* License: GPL
  23 * Author: Kevin Thayer <nufan_wfk at yahoo dot com>
  24 *
  25 * This file will hold API related functions, both internal (firmware api)
  26 * and external (v4l2, etc)
  27 *
  28 * -----
  29 * MPG600/MPG160 support by  T.Adachi <tadachi@tadachi-net.com>
  30 *                      and Takeru KOMORIYA<komoriya@paken.org>
  31 *
  32 * AVerMedia M179 GPIO info by Chris Pinkham <cpinkham@bc2va.org>
  33 *                using information provided by Jiun-Kuei Jung @ AVerMedia.
  34 */
  35
  36#include "ivtv-driver.h"
  37#include "ivtv-fileops.h"
  38#include "ivtv-queue.h"
  39#include "ivtv-mailbox.h"
  40#include "ivtv-ioctl.h"
  41#include "ivtv-irq.h"
  42#include "ivtv-yuv.h"
  43#include "ivtv-cards.h"
  44#include "ivtv-streams.h"
  45#include "ivtv-firmware.h"
  46#include <media/v4l2-event.h>
  47
  48static const struct v4l2_file_operations ivtv_v4l2_enc_fops = {
  49        .owner = THIS_MODULE,
  50        .read = ivtv_v4l2_read,
  51        .write = ivtv_v4l2_write,
  52        .open = ivtv_v4l2_open,
  53        .unlocked_ioctl = video_ioctl2,
  54        .release = ivtv_v4l2_close,
  55        .poll = ivtv_v4l2_enc_poll,
  56};
  57
  58static const struct v4l2_file_operations ivtv_v4l2_dec_fops = {
  59        .owner = THIS_MODULE,
  60        .read = ivtv_v4l2_read,
  61        .write = ivtv_v4l2_write,
  62        .open = ivtv_v4l2_open,
  63        .unlocked_ioctl = video_ioctl2,
  64        .release = ivtv_v4l2_close,
  65        .poll = ivtv_v4l2_dec_poll,
  66};
  67
  68static const struct v4l2_file_operations ivtv_v4l2_radio_fops = {
  69        .owner = THIS_MODULE,
  70        .open = ivtv_v4l2_open,
  71        .unlocked_ioctl = video_ioctl2,
  72        .release = ivtv_v4l2_close,
  73        .poll = ivtv_v4l2_enc_poll,
  74};
  75
  76#define IVTV_V4L2_DEC_MPG_OFFSET  16    /* offset from 0 to register decoder mpg v4l2 minors on */
  77#define IVTV_V4L2_ENC_PCM_OFFSET  24    /* offset from 0 to register pcm v4l2 minors on */
  78#define IVTV_V4L2_ENC_YUV_OFFSET  32    /* offset from 0 to register yuv v4l2 minors on */
  79#define IVTV_V4L2_DEC_YUV_OFFSET  48    /* offset from 0 to register decoder yuv v4l2 minors on */
  80#define IVTV_V4L2_DEC_VBI_OFFSET   8    /* offset from 0 to register decoder vbi input v4l2 minors on */
  81#define IVTV_V4L2_DEC_VOUT_OFFSET 16    /* offset from 0 to register vbi output v4l2 minors on */
  82
  83static struct {
  84        const char *name;
  85        int vfl_type;
  86        int num_offset;
  87        int dma, pio;
  88        u32 v4l2_caps;
  89        const struct v4l2_file_operations *fops;
  90} ivtv_stream_info[] = {
  91        {       /* IVTV_ENC_STREAM_TYPE_MPG */
  92                "encoder MPG",
  93                VFL_TYPE_GRABBER, 0,
  94                PCI_DMA_FROMDEVICE, 0,
  95                V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TUNER |
  96                        V4L2_CAP_AUDIO | V4L2_CAP_READWRITE,
  97                &ivtv_v4l2_enc_fops
  98        },
  99        {       /* IVTV_ENC_STREAM_TYPE_YUV */
 100                "encoder YUV",
 101                VFL_TYPE_GRABBER, IVTV_V4L2_ENC_YUV_OFFSET,
 102                PCI_DMA_FROMDEVICE, 0,
 103                V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TUNER |
 104                        V4L2_CAP_AUDIO | V4L2_CAP_READWRITE,
 105                &ivtv_v4l2_enc_fops
 106        },
 107        {       /* IVTV_ENC_STREAM_TYPE_VBI */
 108                "encoder VBI",
 109                VFL_TYPE_VBI, 0,
 110                PCI_DMA_FROMDEVICE, 0,
 111                V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_CAPTURE | V4L2_CAP_TUNER |
 112                        V4L2_CAP_AUDIO | V4L2_CAP_READWRITE,
 113                &ivtv_v4l2_enc_fops
 114        },
 115        {       /* IVTV_ENC_STREAM_TYPE_PCM */
 116                "encoder PCM",
 117                VFL_TYPE_GRABBER, IVTV_V4L2_ENC_PCM_OFFSET,
 118                PCI_DMA_FROMDEVICE, 0,
 119                V4L2_CAP_TUNER | V4L2_CAP_AUDIO | V4L2_CAP_READWRITE,
 120                &ivtv_v4l2_enc_fops
 121        },
 122        {       /* IVTV_ENC_STREAM_TYPE_RAD */
 123                "encoder radio",
 124                VFL_TYPE_RADIO, 0,
 125                PCI_DMA_NONE, 1,
 126                V4L2_CAP_RADIO | V4L2_CAP_TUNER,
 127                &ivtv_v4l2_radio_fops
 128        },
 129        {       /* IVTV_DEC_STREAM_TYPE_MPG */
 130                "decoder MPG",
 131                VFL_TYPE_GRABBER, IVTV_V4L2_DEC_MPG_OFFSET,
 132                PCI_DMA_TODEVICE, 0,
 133                V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_AUDIO | V4L2_CAP_READWRITE |
 134                V4L2_CAP_VIDEO_OUTPUT_OVERLAY,
 135                &ivtv_v4l2_dec_fops
 136        },
 137        {       /* IVTV_DEC_STREAM_TYPE_VBI */
 138                "decoder VBI",
 139                VFL_TYPE_VBI, IVTV_V4L2_DEC_VBI_OFFSET,
 140                PCI_DMA_NONE, 1,
 141                V4L2_CAP_SLICED_VBI_CAPTURE | V4L2_CAP_READWRITE,
 142                &ivtv_v4l2_enc_fops
 143        },
 144        {       /* IVTV_DEC_STREAM_TYPE_VOUT */
 145                "decoder VOUT",
 146                VFL_TYPE_VBI, IVTV_V4L2_DEC_VOUT_OFFSET,
 147                PCI_DMA_NONE, 1,
 148                V4L2_CAP_SLICED_VBI_OUTPUT | V4L2_CAP_AUDIO | V4L2_CAP_READWRITE,
 149                &ivtv_v4l2_dec_fops
 150        },
 151        {       /* IVTV_DEC_STREAM_TYPE_YUV */
 152                "decoder YUV",
 153                VFL_TYPE_GRABBER, IVTV_V4L2_DEC_YUV_OFFSET,
 154                PCI_DMA_TODEVICE, 0,
 155                V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_AUDIO | V4L2_CAP_READWRITE |
 156                V4L2_CAP_VIDEO_OUTPUT_OVERLAY,
 157                &ivtv_v4l2_dec_fops
 158        }
 159};
 160
 161static void ivtv_stream_init(struct ivtv *itv, int type)
 162{
 163        struct ivtv_stream *s = &itv->streams[type];
 164
 165        /* we need to keep vdev, so restore it afterwards */
 166        memset(s, 0, sizeof(*s));
 167
 168        /* initialize ivtv_stream fields */
 169        s->itv = itv;
 170        s->type = type;
 171        s->name = ivtv_stream_info[type].name;
 172        s->caps = ivtv_stream_info[type].v4l2_caps;
 173
 174        if (ivtv_stream_info[type].pio)
 175                s->dma = PCI_DMA_NONE;
 176        else
 177                s->dma = ivtv_stream_info[type].dma;
 178        s->buf_size = itv->stream_buf_size[type];
 179        if (s->buf_size)
 180                s->buffers = (itv->options.kilobytes[type] * 1024 + s->buf_size - 1) / s->buf_size;
 181        spin_lock_init(&s->qlock);
 182        init_waitqueue_head(&s->waitq);
 183        s->sg_handle = IVTV_DMA_UNMAPPED;
 184        ivtv_queue_init(&s->q_free);
 185        ivtv_queue_init(&s->q_full);
 186        ivtv_queue_init(&s->q_dma);
 187        ivtv_queue_init(&s->q_predma);
 188        ivtv_queue_init(&s->q_io);
 189}
 190
 191static int ivtv_prep_dev(struct ivtv *itv, int type)
 192{
 193        struct ivtv_stream *s = &itv->streams[type];
 194        int num_offset = ivtv_stream_info[type].num_offset;
 195        int num = itv->instance + ivtv_first_minor + num_offset;
 196
 197        /* These four fields are always initialized. If vdev.v4l2_dev == NULL, then
 198           this stream is not in use. In that case no other fields but these
 199           four can be used. */
 200        s->vdev.v4l2_dev = NULL;
 201        s->itv = itv;
 202        s->type = type;
 203        s->name = ivtv_stream_info[type].name;
 204
 205        /* Check whether the radio is supported */
 206        if (type == IVTV_ENC_STREAM_TYPE_RAD && !(itv->v4l2_cap & V4L2_CAP_RADIO))
 207                return 0;
 208        if (type >= IVTV_DEC_STREAM_TYPE_MPG && !(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
 209                return 0;
 210
 211        /* User explicitly selected 0 buffers for these streams, so don't
 212           create them. */
 213        if (ivtv_stream_info[type].dma != PCI_DMA_NONE &&
 214            itv->options.kilobytes[type] == 0) {
 215                IVTV_INFO("Disabled %s device\n", ivtv_stream_info[type].name);
 216                return 0;
 217        }
 218
 219        ivtv_stream_init(itv, type);
 220
 221        snprintf(s->vdev.name, sizeof(s->vdev.name), "%s %s",
 222                        itv->v4l2_dev.name, s->name);
 223
 224        s->vdev.num = num;
 225        s->vdev.v4l2_dev = &itv->v4l2_dev;
 226        if (ivtv_stream_info[type].v4l2_caps &
 227                        (V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_SLICED_VBI_OUTPUT))
 228                s->vdev.vfl_dir = VFL_DIR_TX;
 229        s->vdev.fops = ivtv_stream_info[type].fops;
 230        s->vdev.ctrl_handler = itv->v4l2_dev.ctrl_handler;
 231        s->vdev.release = video_device_release_empty;
 232        s->vdev.tvnorms = V4L2_STD_ALL;
 233        s->vdev.lock = &itv->serialize_lock;
 234        if (s->type == IVTV_DEC_STREAM_TYPE_VBI) {
 235                v4l2_disable_ioctl(&s->vdev, VIDIOC_S_AUDIO);
 236                v4l2_disable_ioctl(&s->vdev, VIDIOC_G_AUDIO);
 237                v4l2_disable_ioctl(&s->vdev, VIDIOC_ENUMAUDIO);
 238                v4l2_disable_ioctl(&s->vdev, VIDIOC_ENUMINPUT);
 239                v4l2_disable_ioctl(&s->vdev, VIDIOC_S_INPUT);
 240                v4l2_disable_ioctl(&s->vdev, VIDIOC_G_INPUT);
 241                v4l2_disable_ioctl(&s->vdev, VIDIOC_S_FREQUENCY);
 242                v4l2_disable_ioctl(&s->vdev, VIDIOC_G_FREQUENCY);
 243                v4l2_disable_ioctl(&s->vdev, VIDIOC_S_TUNER);
 244                v4l2_disable_ioctl(&s->vdev, VIDIOC_G_TUNER);
 245                v4l2_disable_ioctl(&s->vdev, VIDIOC_S_STD);
 246        }
 247        ivtv_set_funcs(&s->vdev);
 248        return 0;
 249}
 250
 251/* Initialize v4l2 variables and prepare v4l2 devices */
 252int ivtv_streams_setup(struct ivtv *itv)
 253{
 254        int type;
 255
 256        /* Setup V4L2 Devices */
 257        for (type = 0; type < IVTV_MAX_STREAMS; type++) {
 258                /* Prepare device */
 259                if (ivtv_prep_dev(itv, type))
 260                        break;
 261
 262                if (itv->streams[type].vdev.v4l2_dev == NULL)
 263                        continue;
 264
 265                /* Allocate Stream */
 266                if (ivtv_stream_alloc(&itv->streams[type]))
 267                        break;
 268        }
 269        if (type == IVTV_MAX_STREAMS)
 270                return 0;
 271
 272        /* One or more streams could not be initialized. Clean 'em all up. */
 273        ivtv_streams_cleanup(itv);
 274        return -ENOMEM;
 275}
 276
 277static int ivtv_reg_dev(struct ivtv *itv, int type)
 278{
 279        struct ivtv_stream *s = &itv->streams[type];
 280        int vfl_type = ivtv_stream_info[type].vfl_type;
 281        const char *name;
 282        int num;
 283
 284        if (s->vdev.v4l2_dev == NULL)
 285                return 0;
 286
 287        num = s->vdev.num;
 288        /* card number + user defined offset + device offset */
 289        if (type != IVTV_ENC_STREAM_TYPE_MPG) {
 290                struct ivtv_stream *s_mpg = &itv->streams[IVTV_ENC_STREAM_TYPE_MPG];
 291
 292                if (s_mpg->vdev.v4l2_dev)
 293                        num = s_mpg->vdev.num + ivtv_stream_info[type].num_offset;
 294        }
 295        video_set_drvdata(&s->vdev, s);
 296
 297        /* Register device. First try the desired minor, then any free one. */
 298        if (video_register_device_no_warn(&s->vdev, vfl_type, num)) {
 299                IVTV_ERR("Couldn't register v4l2 device for %s (device node number %d)\n",
 300                                s->name, num);
 301                return -ENOMEM;
 302        }
 303        name = video_device_node_name(&s->vdev);
 304
 305        switch (vfl_type) {
 306        case VFL_TYPE_GRABBER:
 307                IVTV_INFO("Registered device %s for %s (%d kB)\n",
 308                        name, s->name, itv->options.kilobytes[type]);
 309                break;
 310        case VFL_TYPE_RADIO:
 311                IVTV_INFO("Registered device %s for %s\n",
 312                        name, s->name);
 313                break;
 314        case VFL_TYPE_VBI:
 315                if (itv->options.kilobytes[type])
 316                        IVTV_INFO("Registered device %s for %s (%d kB)\n",
 317                                name, s->name, itv->options.kilobytes[type]);
 318                else
 319                        IVTV_INFO("Registered device %s for %s\n",
 320                                name, s->name);
 321                break;
 322        }
 323        return 0;
 324}
 325
 326/* Register v4l2 devices */
 327int ivtv_streams_register(struct ivtv *itv)
 328{
 329        int type;
 330        int err = 0;
 331
 332        /* Register V4L2 devices */
 333        for (type = 0; type < IVTV_MAX_STREAMS; type++)
 334                err |= ivtv_reg_dev(itv, type);
 335
 336        if (err == 0)
 337                return 0;
 338
 339        /* One or more streams could not be initialized. Clean 'em all up. */
 340        ivtv_streams_cleanup(itv);
 341        return -ENOMEM;
 342}
 343
 344/* Unregister v4l2 devices */
 345void ivtv_streams_cleanup(struct ivtv *itv)
 346{
 347        int type;
 348
 349        /* Teardown all streams */
 350        for (type = 0; type < IVTV_MAX_STREAMS; type++) {
 351                struct video_device *vdev = &itv->streams[type].vdev;
 352
 353                if (vdev->v4l2_dev == NULL)
 354                        continue;
 355
 356                video_unregister_device(vdev);
 357                ivtv_stream_free(&itv->streams[type]);
 358                itv->streams[type].vdev.v4l2_dev = NULL;
 359        }
 360}
 361
 362static void ivtv_vbi_setup(struct ivtv *itv)
 363{
 364        int raw = ivtv_raw_vbi(itv);
 365        u32 data[CX2341X_MBOX_MAX_DATA];
 366        int lines;
 367        int i;
 368
 369        /* Reset VBI */
 370        ivtv_vapi(itv, CX2341X_ENC_SET_VBI_LINE, 5, 0xffff , 0, 0, 0, 0);
 371
 372        /* setup VBI registers */
 373        if (raw)
 374                v4l2_subdev_call(itv->sd_video, vbi, s_raw_fmt, &itv->vbi.in.fmt.vbi);
 375        else
 376                v4l2_subdev_call(itv->sd_video, vbi, s_sliced_fmt, &itv->vbi.in.fmt.sliced);
 377
 378        /* determine number of lines and total number of VBI bytes.
 379           A raw line takes 1443 bytes: 2 * 720 + 4 byte frame header - 1
 380           The '- 1' byte is probably an unused U or V byte. Or something...
 381           A sliced line takes 51 bytes: 4 byte frame header, 4 byte internal
 382           header, 42 data bytes + checksum (to be confirmed) */
 383        if (raw) {
 384                lines = itv->vbi.count * 2;
 385        } else {
 386                lines = itv->is_60hz ? 24 : 38;
 387                if (itv->is_60hz && (itv->hw_flags & IVTV_HW_CX25840))
 388                        lines += 2;
 389        }
 390
 391        itv->vbi.enc_size = lines * (raw ? itv->vbi.raw_size : itv->vbi.sliced_size);
 392
 393        /* Note: sliced vs raw flag doesn't seem to have any effect
 394           TODO: check mode (0x02) value with older ivtv versions. */
 395        data[0] = raw | 0x02 | (0xbd << 8);
 396
 397        /* Every X number of frames a VBI interrupt arrives (frames as in 25 or 30 fps) */
 398        data[1] = 1;
 399        /* The VBI frames are stored in a ringbuffer with this size (with a VBI frame as unit) */
 400        data[2] = raw ? 4 : 4 * (itv->vbi.raw_size / itv->vbi.enc_size);
 401        /* The start/stop codes determine which VBI lines end up in the raw VBI data area.
 402           The codes are from table 24 in the saa7115 datasheet. Each raw/sliced/video line
 403           is framed with codes FF0000XX where XX is the SAV/EAV (Start/End of Active Video)
 404           code. These values for raw VBI are obtained from a driver disassembly. The sliced
 405           start/stop codes was deduced from this, but they do not appear in the driver.
 406           Other code pairs that I found are: 0x250E6249/0x13545454 and 0x25256262/0x38137F54.
 407           However, I have no idea what these values are for. */
 408        if (itv->hw_flags & IVTV_HW_CX25840) {
 409                /* Setup VBI for the cx25840 digitizer */
 410                if (raw) {
 411                        data[3] = 0x20602060;
 412                        data[4] = 0x30703070;
 413                } else {
 414                        data[3] = 0xB0F0B0F0;
 415                        data[4] = 0xA0E0A0E0;
 416                }
 417                /* Lines per frame */
 418                data[5] = lines;
 419                /* bytes per line */
 420                data[6] = (raw ? itv->vbi.raw_size : itv->vbi.sliced_size);
 421        } else {
 422                /* Setup VBI for the saa7115 digitizer */
 423                if (raw) {
 424                        data[3] = 0x25256262;
 425                        data[4] = 0x387F7F7F;
 426                } else {
 427                        data[3] = 0xABABECEC;
 428                        data[4] = 0xB6F1F1F1;
 429                }
 430                /* Lines per frame */
 431                data[5] = lines;
 432                /* bytes per line */
 433                data[6] = itv->vbi.enc_size / lines;
 434        }
 435
 436        IVTV_DEBUG_INFO(
 437                "Setup VBI API header 0x%08x pkts %d buffs %d ln %d sz %d\n",
 438                        data[0], data[1], data[2], data[5], data[6]);
 439
 440        ivtv_api(itv, CX2341X_ENC_SET_VBI_CONFIG, 7, data);
 441
 442        /* returns the VBI encoder memory area. */
 443        itv->vbi.enc_start = data[2];
 444        itv->vbi.fpi = data[0];
 445        if (!itv->vbi.fpi)
 446                itv->vbi.fpi = 1;
 447
 448        IVTV_DEBUG_INFO("Setup VBI start 0x%08x frames %d fpi %d\n",
 449                itv->vbi.enc_start, data[1], itv->vbi.fpi);
 450
 451        /* select VBI lines.
 452           Note that the sliced argument seems to have no effect. */
 453        for (i = 2; i <= 24; i++) {
 454                int valid;
 455
 456                if (itv->is_60hz) {
 457                        valid = i >= 10 && i < 22;
 458                } else {
 459                        valid = i >= 6 && i < 24;
 460                }
 461                ivtv_vapi(itv, CX2341X_ENC_SET_VBI_LINE, 5, i - 1,
 462                                valid, 0 , 0, 0);
 463                ivtv_vapi(itv, CX2341X_ENC_SET_VBI_LINE, 5, (i - 1) | 0x80000000,
 464                                valid, 0, 0, 0);
 465        }
 466
 467        /* Remaining VBI questions:
 468           - Is it possible to select particular VBI lines only for inclusion in the MPEG
 469           stream? Currently you can only get the first X lines.
 470           - Is mixed raw and sliced VBI possible?
 471           - What's the meaning of the raw/sliced flag?
 472           - What's the meaning of params 2, 3 & 4 of the Select VBI command? */
 473}
 474
 475int ivtv_start_v4l2_encode_stream(struct ivtv_stream *s)
 476{
 477        u32 data[CX2341X_MBOX_MAX_DATA];
 478        struct ivtv *itv = s->itv;
 479        int captype = 0, subtype = 0;
 480        int enable_passthrough = 0;
 481
 482        if (s->vdev.v4l2_dev == NULL)
 483                return -EINVAL;
 484
 485        IVTV_DEBUG_INFO("Start encoder stream %s\n", s->name);
 486
 487        switch (s->type) {
 488        case IVTV_ENC_STREAM_TYPE_MPG:
 489                captype = 0;
 490                subtype = 3;
 491
 492                /* Stop Passthrough */
 493                if (itv->output_mode == OUT_PASSTHROUGH) {
 494                        ivtv_passthrough_mode(itv, 0);
 495                        enable_passthrough = 1;
 496                }
 497                itv->mpg_data_received = itv->vbi_data_inserted = 0;
 498                itv->dualwatch_jiffies = jiffies;
 499                itv->dualwatch_stereo_mode = v4l2_ctrl_g_ctrl(itv->cxhdl.audio_mode);
 500                itv->search_pack_header = 0;
 501                break;
 502
 503        case IVTV_ENC_STREAM_TYPE_YUV:
 504                if (itv->output_mode == OUT_PASSTHROUGH) {
 505                        captype = 2;
 506                        subtype = 11;   /* video+audio+decoder */
 507                        break;
 508                }
 509                captype = 1;
 510                subtype = 1;
 511                break;
 512        case IVTV_ENC_STREAM_TYPE_PCM:
 513                captype = 1;
 514                subtype = 2;
 515                break;
 516        case IVTV_ENC_STREAM_TYPE_VBI:
 517                captype = 1;
 518                subtype = 4;
 519
 520                itv->vbi.frame = 0;
 521                itv->vbi.inserted_frame = 0;
 522                memset(itv->vbi.sliced_mpeg_size,
 523                        0, sizeof(itv->vbi.sliced_mpeg_size));
 524                break;
 525        default:
 526                return -EINVAL;
 527        }
 528        s->subtype = subtype;
 529        s->buffers_stolen = 0;
 530
 531        /* Clear Streamoff flags in case left from last capture */
 532        clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
 533
 534        if (atomic_read(&itv->capturing) == 0) {
 535                int digitizer;
 536
 537                /* Always use frame based mode. Experiments have demonstrated that byte
 538                   stream based mode results in dropped frames and corruption. Not often,
 539                   but occasionally. Many thanks go to Leonard Orb who spent a lot of
 540                   effort and time trying to trace the cause of the drop outs. */
 541                /* 1 frame per DMA */
 542                /*ivtv_vapi(itv, CX2341X_ENC_SET_DMA_BLOCK_SIZE, 2, 128, 0); */
 543                ivtv_vapi(itv, CX2341X_ENC_SET_DMA_BLOCK_SIZE, 2, 1, 1);
 544
 545                /* Stuff from Windows, we don't know what it is */
 546                ivtv_vapi(itv, CX2341X_ENC_SET_VERT_CROP_LINE, 1, 0);
 547                /* According to the docs, this should be correct. However, this is
 548                   untested. I don't dare enable this without having tested it.
 549                   Only very few old cards actually have this hardware combination.
 550                ivtv_vapi(itv, CX2341X_ENC_SET_VERT_CROP_LINE, 1,
 551                        ((itv->hw_flags & IVTV_HW_SAA7114) && itv->is_60hz) ? 10001 : 0);
 552                */
 553                ivtv_vapi(itv, CX2341X_ENC_MISC, 2, 3, !itv->has_cx23415);
 554                ivtv_vapi(itv, CX2341X_ENC_MISC, 2, 8, 0);
 555                ivtv_vapi(itv, CX2341X_ENC_MISC, 2, 4, 1);
 556                ivtv_vapi(itv, CX2341X_ENC_MISC, 1, 12);
 557
 558                /* assign placeholder */
 559                ivtv_vapi(itv, CX2341X_ENC_SET_PLACEHOLDER, 12,
 560                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
 561
 562                if (itv->card->hw_all & (IVTV_HW_SAA7115 | IVTV_HW_SAA717X))
 563                    digitizer = 0xF1;
 564                else if (itv->card->hw_all & IVTV_HW_SAA7114)
 565                    digitizer = 0xEF;
 566                else /* cx25840 */
 567                    digitizer = 0x140;
 568
 569                ivtv_vapi(itv, CX2341X_ENC_SET_NUM_VSYNC_LINES, 2, digitizer, digitizer);
 570
 571                /* Setup VBI */
 572                if (itv->v4l2_cap & V4L2_CAP_VBI_CAPTURE) {
 573                        ivtv_vbi_setup(itv);
 574                }
 575
 576                /* assign program index info. Mask 7: select I/P/B, Num_req: 400 max */
 577                ivtv_vapi_result(itv, data, CX2341X_ENC_SET_PGM_INDEX_INFO, 2, 7, 400);
 578                itv->pgm_info_offset = data[0];
 579                itv->pgm_info_num = data[1];
 580                itv->pgm_info_write_idx = 0;
 581                itv->pgm_info_read_idx = 0;
 582
 583                IVTV_DEBUG_INFO("PGM Index at 0x%08x with %d elements\n",
 584                                itv->pgm_info_offset, itv->pgm_info_num);
 585
 586                /* Setup API for Stream */
 587                cx2341x_handler_setup(&itv->cxhdl);
 588
 589                /* mute if capturing radio */
 590                if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags))
 591                        ivtv_vapi(itv, CX2341X_ENC_MUTE_VIDEO, 1,
 592                                1 | (v4l2_ctrl_g_ctrl(itv->cxhdl.video_mute_yuv) << 8));
 593        }
 594
 595        /* Vsync Setup */
 596        if (itv->has_cx23415 && !test_and_set_bit(IVTV_F_I_DIG_RST, &itv->i_flags)) {
 597                /* event notification (on) */
 598                ivtv_vapi(itv, CX2341X_ENC_SET_EVENT_NOTIFICATION, 4, 0, 1, IVTV_IRQ_ENC_VIM_RST, -1);
 599                ivtv_clear_irq_mask(itv, IVTV_IRQ_ENC_VIM_RST);
 600        }
 601
 602        if (atomic_read(&itv->capturing) == 0) {
 603                /* Clear all Pending Interrupts */
 604                ivtv_set_irq_mask(itv, IVTV_IRQ_MASK_CAPTURE);
 605
 606                clear_bit(IVTV_F_I_EOS, &itv->i_flags);
 607
 608                cx2341x_handler_set_busy(&itv->cxhdl, 1);
 609
 610                /* Initialize Digitizer for Capture */
 611                /* Avoid tinny audio problem - ensure audio clocks are going */
 612                v4l2_subdev_call(itv->sd_audio, audio, s_stream, 1);
 613                /* Avoid unpredictable PCI bus hang - disable video clocks */
 614                v4l2_subdev_call(itv->sd_video, video, s_stream, 0);
 615                ivtv_msleep_timeout(300, 0);
 616                ivtv_vapi(itv, CX2341X_ENC_INITIALIZE_INPUT, 0);
 617                v4l2_subdev_call(itv->sd_video, video, s_stream, 1);
 618        }
 619
 620        /* begin_capture */
 621        if (ivtv_vapi(itv, CX2341X_ENC_START_CAPTURE, 2, captype, subtype))
 622        {
 623                IVTV_DEBUG_WARN( "Error starting capture!\n");
 624                return -EINVAL;
 625        }
 626
 627        /* Start Passthrough */
 628        if (enable_passthrough) {
 629                ivtv_passthrough_mode(itv, 1);
 630        }
 631
 632        if (s->type == IVTV_ENC_STREAM_TYPE_VBI)
 633                ivtv_clear_irq_mask(itv, IVTV_IRQ_ENC_VBI_CAP);
 634        else
 635                ivtv_clear_irq_mask(itv, IVTV_IRQ_MASK_CAPTURE);
 636
 637        /* you're live! sit back and await interrupts :) */
 638        atomic_inc(&itv->capturing);
 639        return 0;
 640}
 641EXPORT_SYMBOL(ivtv_start_v4l2_encode_stream);
 642
 643static int ivtv_setup_v4l2_decode_stream(struct ivtv_stream *s)
 644{
 645        u32 data[CX2341X_MBOX_MAX_DATA];
 646        struct ivtv *itv = s->itv;
 647        int datatype;
 648        u16 width;
 649        u16 height;
 650
 651        if (s->vdev.v4l2_dev == NULL)
 652                return -EINVAL;
 653
 654        IVTV_DEBUG_INFO("Setting some initial decoder settings\n");
 655
 656        width = itv->cxhdl.width;
 657        height = itv->cxhdl.height;
 658
 659        /* set audio mode to left/stereo  for dual/stereo mode. */
 660        ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode);
 661
 662        /* set number of internal decoder buffers */
 663        ivtv_vapi(itv, CX2341X_DEC_SET_DISPLAY_BUFFERS, 1, 0);
 664
 665        /* prebuffering */
 666        ivtv_vapi(itv, CX2341X_DEC_SET_PREBUFFERING, 1, 1);
 667
 668        /* extract from user packets */
 669        ivtv_vapi_result(itv, data, CX2341X_DEC_EXTRACT_VBI, 1, 1);
 670        itv->vbi.dec_start = data[0];
 671
 672        IVTV_DEBUG_INFO("Decoder VBI RE-Insert start 0x%08x size 0x%08x\n",
 673                itv->vbi.dec_start, data[1]);
 674
 675        /* set decoder source settings */
 676        /* Data type: 0 = mpeg from host,
 677           1 = yuv from encoder,
 678           2 = yuv_from_host */
 679        switch (s->type) {
 680        case IVTV_DEC_STREAM_TYPE_YUV:
 681                if (itv->output_mode == OUT_PASSTHROUGH) {
 682                        datatype = 1;
 683                } else {
 684                        /* Fake size to avoid switching video standard */
 685                        datatype = 2;
 686                        width = 720;
 687                        height = itv->is_out_50hz ? 576 : 480;
 688                }
 689                IVTV_DEBUG_INFO("Setup DEC YUV Stream data[0] = %d\n", datatype);
 690                break;
 691        case IVTV_DEC_STREAM_TYPE_MPG:
 692        default:
 693                datatype = 0;
 694                break;
 695        }
 696        if (ivtv_vapi(itv, CX2341X_DEC_SET_DECODER_SOURCE, 4, datatype,
 697                        width, height, itv->cxhdl.audio_properties)) {
 698                IVTV_DEBUG_WARN("Couldn't initialize decoder source\n");
 699        }
 700
 701        /* Decoder sometimes dies here, so wait a moment */
 702        ivtv_msleep_timeout(10, 0);
 703
 704        /* Known failure point for firmware, so check */
 705        return ivtv_firmware_check(itv, "ivtv_setup_v4l2_decode_stream");
 706}
 707
 708int ivtv_start_v4l2_decode_stream(struct ivtv_stream *s, int gop_offset)
 709{
 710        struct ivtv *itv = s->itv;
 711        int rc;
 712
 713        if (s->vdev.v4l2_dev == NULL)
 714                return -EINVAL;
 715
 716        if (test_and_set_bit(IVTV_F_S_STREAMING, &s->s_flags))
 717                return 0;       /* already started */
 718
 719        IVTV_DEBUG_INFO("Starting decode stream %s (gop_offset %d)\n", s->name, gop_offset);
 720
 721        rc = ivtv_setup_v4l2_decode_stream(s);
 722        if (rc < 0) {
 723                clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
 724                return rc;
 725        }
 726
 727        /* set dma size to 65536 bytes */
 728        ivtv_vapi(itv, CX2341X_DEC_SET_DMA_BLOCK_SIZE, 1, 65536);
 729
 730        /* Clear Streamoff */
 731        clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
 732
 733        /* Zero out decoder counters */
 734        writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA_END].data[0]);
 735        writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA_END].data[1]);
 736        writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA_END].data[2]);
 737        writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA_END].data[3]);
 738        writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA].data[0]);
 739        writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA].data[1]);
 740        writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA].data[2]);
 741        writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA].data[3]);
 742
 743        /* turn on notification of dual/stereo mode change */
 744        ivtv_vapi(itv, CX2341X_DEC_SET_EVENT_NOTIFICATION, 4, 0, 1, IVTV_IRQ_DEC_AUD_MODE_CHG, -1);
 745
 746        /* start playback */
 747        ivtv_vapi(itv, CX2341X_DEC_START_PLAYBACK, 2, gop_offset, 0);
 748
 749        /* Let things settle before we actually start */
 750        ivtv_msleep_timeout(10, 0);
 751
 752        /* Clear the following Interrupt mask bits for decoding */
 753        ivtv_clear_irq_mask(itv, IVTV_IRQ_MASK_DECODE);
 754        IVTV_DEBUG_IRQ("IRQ Mask is now: 0x%08x\n", itv->irqmask);
 755
 756        /* you're live! sit back and await interrupts :) */
 757        atomic_inc(&itv->decoding);
 758        return 0;
 759}
 760
 761void ivtv_stop_all_captures(struct ivtv *itv)
 762{
 763        int i;
 764
 765        for (i = IVTV_MAX_STREAMS - 1; i >= 0; i--) {
 766                struct ivtv_stream *s = &itv->streams[i];
 767
 768                if (s->vdev.v4l2_dev == NULL)
 769                        continue;
 770                if (test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
 771                        ivtv_stop_v4l2_encode_stream(s, 0);
 772                }
 773        }
 774}
 775
 776int ivtv_stop_v4l2_encode_stream(struct ivtv_stream *s, int gop_end)
 777{
 778        struct ivtv *itv = s->itv;
 779        DECLARE_WAITQUEUE(wait, current);
 780        int cap_type;
 781        int stopmode;
 782
 783        if (s->vdev.v4l2_dev == NULL)
 784                return -EINVAL;
 785
 786        /* This function assumes that you are allowed to stop the capture
 787           and that we are actually capturing */
 788
 789        IVTV_DEBUG_INFO("Stop Capture\n");
 790
 791        if (s->type == IVTV_DEC_STREAM_TYPE_VOUT)
 792                return 0;
 793        if (atomic_read(&itv->capturing) == 0)
 794                return 0;
 795
 796        switch (s->type) {
 797        case IVTV_ENC_STREAM_TYPE_YUV:
 798                cap_type = 1;
 799                break;
 800        case IVTV_ENC_STREAM_TYPE_PCM:
 801                cap_type = 1;
 802                break;
 803        case IVTV_ENC_STREAM_TYPE_VBI:
 804                cap_type = 1;
 805                break;
 806        case IVTV_ENC_STREAM_TYPE_MPG:
 807        default:
 808                cap_type = 0;
 809                break;
 810        }
 811
 812        /* Stop Capture Mode */
 813        if (s->type == IVTV_ENC_STREAM_TYPE_MPG && gop_end) {
 814                stopmode = 0;
 815        } else {
 816                stopmode = 1;
 817        }
 818
 819        /* end_capture */
 820        /* when: 0 =  end of GOP  1 = NOW!, type: 0 = mpeg, subtype: 3 = video+audio */
 821        ivtv_vapi(itv, CX2341X_ENC_STOP_CAPTURE, 3, stopmode, cap_type, s->subtype);
 822
 823        if (!test_bit(IVTV_F_S_PASSTHROUGH, &s->s_flags)) {
 824                if (s->type == IVTV_ENC_STREAM_TYPE_MPG && gop_end) {
 825                        /* only run these if we're shutting down the last cap */
 826                        unsigned long duration;
 827                        unsigned long then = jiffies;
 828
 829                        add_wait_queue(&itv->eos_waitq, &wait);
 830
 831                        set_current_state(TASK_INTERRUPTIBLE);
 832
 833                        /* wait 2s for EOS interrupt */
 834                        while (!test_bit(IVTV_F_I_EOS, &itv->i_flags) &&
 835                                time_before(jiffies,
 836                                            then + msecs_to_jiffies(2000))) {
 837                                schedule_timeout(msecs_to_jiffies(10));
 838                        }
 839
 840                        /* To convert jiffies to ms, we must multiply by 1000
 841                         * and divide by HZ.  To avoid runtime division, we
 842                         * convert this to multiplication by 1000/HZ.
 843                         * Since integer division truncates, we get the best
 844                         * accuracy if we do a rounding calculation of the constant.
 845                         * Think of the case where HZ is 1024.
 846                         */
 847                        duration = ((1000 + HZ / 2) / HZ) * (jiffies - then);
 848
 849                        if (!test_bit(IVTV_F_I_EOS, &itv->i_flags)) {
 850                                IVTV_DEBUG_WARN("%s: EOS interrupt not received! stopping anyway.\n", s->name);
 851                                IVTV_DEBUG_WARN("%s: waited %lu ms.\n", s->name, duration);
 852                        } else {
 853                                IVTV_DEBUG_INFO("%s: EOS took %lu ms to occur.\n", s->name, duration);
 854                        }
 855                        set_current_state(TASK_RUNNING);
 856                        remove_wait_queue(&itv->eos_waitq, &wait);
 857                        set_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
 858                }
 859
 860                /* Handle any pending interrupts */
 861                ivtv_msleep_timeout(100, 0);
 862        }
 863
 864        atomic_dec(&itv->capturing);
 865
 866        /* Clear capture and no-read bits */
 867        clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
 868
 869        if (s->type == IVTV_ENC_STREAM_TYPE_VBI)
 870                ivtv_set_irq_mask(itv, IVTV_IRQ_ENC_VBI_CAP);
 871
 872        if (atomic_read(&itv->capturing) > 0) {
 873                return 0;
 874        }
 875
 876        cx2341x_handler_set_busy(&itv->cxhdl, 0);
 877
 878        /* Set the following Interrupt mask bits for capture */
 879        ivtv_set_irq_mask(itv, IVTV_IRQ_MASK_CAPTURE);
 880        del_timer(&itv->dma_timer);
 881
 882        /* event notification (off) */
 883        if (test_and_clear_bit(IVTV_F_I_DIG_RST, &itv->i_flags)) {
 884                /* type: 0 = refresh */
 885                /* on/off: 0 = off, intr: 0x10000000, mbox_id: -1: none */
 886                ivtv_vapi(itv, CX2341X_ENC_SET_EVENT_NOTIFICATION, 4, 0, 0, IVTV_IRQ_ENC_VIM_RST, -1);
 887                ivtv_set_irq_mask(itv, IVTV_IRQ_ENC_VIM_RST);
 888        }
 889
 890        /* Raw-passthrough is implied on start. Make sure it's stopped so
 891           the encoder will re-initialize when next started */
 892        ivtv_vapi(itv, CX2341X_ENC_STOP_CAPTURE, 3, 1, 2, 7);
 893
 894        wake_up(&s->waitq);
 895
 896        return 0;
 897}
 898EXPORT_SYMBOL(ivtv_stop_v4l2_encode_stream);
 899
 900int ivtv_stop_v4l2_decode_stream(struct ivtv_stream *s, int flags, u64 pts)
 901{
 902        static const struct v4l2_event ev = {
 903                .type = V4L2_EVENT_EOS,
 904        };
 905        struct ivtv *itv = s->itv;
 906
 907        if (s->vdev.v4l2_dev == NULL)
 908                return -EINVAL;
 909
 910        if (s->type != IVTV_DEC_STREAM_TYPE_YUV && s->type != IVTV_DEC_STREAM_TYPE_MPG)
 911                return -EINVAL;
 912
 913        if (!test_bit(IVTV_F_S_STREAMING, &s->s_flags))
 914                return 0;
 915
 916        IVTV_DEBUG_INFO("Stop Decode at %llu, flags: %x\n", (unsigned long long)pts, flags);
 917
 918        /* Stop Decoder */
 919        if (!(flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) || pts) {
 920                u32 tmp = 0;
 921
 922                /* Wait until the decoder is no longer running */
 923                if (pts) {
 924                        ivtv_vapi(itv, CX2341X_DEC_STOP_PLAYBACK, 3,
 925                                0, (u32)(pts & 0xffffffff), (u32)(pts >> 32));
 926                }
 927                while (1) {
 928                        u32 data[CX2341X_MBOX_MAX_DATA];
 929                        ivtv_vapi_result(itv, data, CX2341X_DEC_GET_XFER_INFO, 0);
 930                        if (s->q_full.buffers + s->q_dma.buffers == 0) {
 931                                if (tmp == data[3])
 932                                        break;
 933                                tmp = data[3];
 934                        }
 935                        if (ivtv_msleep_timeout(100, 1))
 936                                break;
 937                }
 938        }
 939        ivtv_vapi(itv, CX2341X_DEC_STOP_PLAYBACK, 3, flags & V4L2_DEC_CMD_STOP_TO_BLACK, 0, 0);
 940
 941        /* turn off notification of dual/stereo mode change */
 942        ivtv_vapi(itv, CX2341X_DEC_SET_EVENT_NOTIFICATION, 4, 0, 0, IVTV_IRQ_DEC_AUD_MODE_CHG, -1);
 943
 944        ivtv_set_irq_mask(itv, IVTV_IRQ_MASK_DECODE);
 945        del_timer(&itv->dma_timer);
 946
 947        clear_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags);
 948        clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
 949        ivtv_flush_queues(s);
 950
 951        /* decoder needs time to settle */
 952        ivtv_msleep_timeout(40, 0);
 953
 954        /* decrement decoding */
 955        atomic_dec(&itv->decoding);
 956
 957        set_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags);
 958        wake_up(&itv->event_waitq);
 959        v4l2_event_queue(&s->vdev, &ev);
 960
 961        /* wake up wait queues */
 962        wake_up(&s->waitq);
 963
 964        return 0;
 965}
 966
 967int ivtv_passthrough_mode(struct ivtv *itv, int enable)
 968{
 969        struct ivtv_stream *yuv_stream = &itv->streams[IVTV_ENC_STREAM_TYPE_YUV];
 970        struct ivtv_stream *dec_stream = &itv->streams[IVTV_DEC_STREAM_TYPE_YUV];
 971
 972        if (yuv_stream->vdev.v4l2_dev == NULL || dec_stream->vdev.v4l2_dev == NULL)
 973                return -EINVAL;
 974
 975        IVTV_DEBUG_INFO("ivtv ioctl: Select passthrough mode\n");
 976
 977        /* Prevent others from starting/stopping streams while we
 978           initiate/terminate passthrough mode */
 979        if (enable) {
 980                if (itv->output_mode == OUT_PASSTHROUGH) {
 981                        return 0;
 982                }
 983                if (ivtv_set_output_mode(itv, OUT_PASSTHROUGH) != OUT_PASSTHROUGH)
 984                        return -EBUSY;
 985
 986                /* Fully initialize stream, and then unflag init */
 987                set_bit(IVTV_F_S_PASSTHROUGH, &dec_stream->s_flags);
 988                set_bit(IVTV_F_S_STREAMING, &dec_stream->s_flags);
 989
 990                /* Setup YUV Decoder */
 991                ivtv_setup_v4l2_decode_stream(dec_stream);
 992
 993                /* Start Decoder */
 994                ivtv_vapi(itv, CX2341X_DEC_START_PLAYBACK, 2, 0, 1);
 995                atomic_inc(&itv->decoding);
 996
 997                /* Setup capture if not already done */
 998                if (atomic_read(&itv->capturing) == 0) {
 999                        cx2341x_handler_setup(&itv->cxhdl);
1000                        cx2341x_handler_set_busy(&itv->cxhdl, 1);
1001                }
1002
1003                /* Start Passthrough Mode */
1004                ivtv_vapi(itv, CX2341X_ENC_START_CAPTURE, 2, 2, 11);
1005                atomic_inc(&itv->capturing);
1006                return 0;
1007        }
1008
1009        if (itv->output_mode != OUT_PASSTHROUGH)
1010                return 0;
1011
1012        /* Stop Passthrough Mode */
1013        ivtv_vapi(itv, CX2341X_ENC_STOP_CAPTURE, 3, 1, 2, 11);
1014        ivtv_vapi(itv, CX2341X_DEC_STOP_PLAYBACK, 3, 1, 0, 0);
1015
1016        atomic_dec(&itv->capturing);
1017        atomic_dec(&itv->decoding);
1018        clear_bit(IVTV_F_S_PASSTHROUGH, &dec_stream->s_flags);
1019        clear_bit(IVTV_F_S_STREAMING, &dec_stream->s_flags);
1020        itv->output_mode = OUT_NONE;
1021        if (atomic_read(&itv->capturing) == 0)
1022                cx2341x_handler_set_busy(&itv->cxhdl, 0);
1023
1024        return 0;
1025}
1026