linux/drivers/media/pci/saa7164/saa7164-encoder.c
<<
>>
Prefs
   1/*
   2 *  Driver for the NXP SAA7164 PCIe bridge
   3 *
   4 *  Copyright (c) 2010-2015 Steven Toth <stoth@kernellabs.com>
   5 *
   6 *  This program is free software; you can redistribute it and/or modify
   7 *  it under the terms of the GNU General Public License as published by
   8 *  the Free Software Foundation; either version 2 of the License, or
   9 *  (at your option) any later version.
  10 *
  11 *  This program is distributed in the hope that it will be useful,
  12 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14 *
  15 *  GNU General Public License for more details.
  16 */
  17
  18#include "saa7164.h"
  19
  20#define ENCODER_MAX_BITRATE 6500000
  21#define ENCODER_MIN_BITRATE 1000000
  22#define ENCODER_DEF_BITRATE 5000000
  23
  24/*
  25 * This is a dummy non-zero value for the sizeimage field of v4l2_pix_format.
  26 * It is not actually used for anything since this driver does not support
  27 * stream I/O, only read(), and because this driver produces an MPEG stream
  28 * and not discrete frames. But the V4L2 spec doesn't allow for this value
  29 * to be 0, so set it to 0x10000 instead.
  30 *
  31 * If we ever change this driver to support stream I/O, then this field
  32 * will be the size of the streaming buffers.
  33 */
  34#define SAA7164_SIZEIMAGE (0x10000)
  35
  36static struct saa7164_tvnorm saa7164_tvnorms[] = {
  37        {
  38                .name      = "NTSC-M",
  39                .id        = V4L2_STD_NTSC_M,
  40        }, {
  41                .name      = "NTSC-JP",
  42                .id        = V4L2_STD_NTSC_M_JP,
  43        }
  44};
  45
  46/* Take the encoder configuration form the port struct and
  47 * flush it to the hardware.
  48 */
  49static void saa7164_encoder_configure(struct saa7164_port *port)
  50{
  51        struct saa7164_dev *dev = port->dev;
  52        dprintk(DBGLVL_ENC, "%s()\n", __func__);
  53
  54        port->encoder_params.width = port->width;
  55        port->encoder_params.height = port->height;
  56        port->encoder_params.is_50hz =
  57                (port->encodernorm.id & V4L2_STD_625_50) != 0;
  58
  59        /* Set up the DIF (enable it) for analog mode by default */
  60        saa7164_api_initialize_dif(port);
  61
  62        /* Configure the correct video standard */
  63        saa7164_api_configure_dif(port, port->encodernorm.id);
  64
  65        /* Ensure the audio decoder is correct configured */
  66        saa7164_api_set_audio_std(port);
  67}
  68
  69static int saa7164_encoder_buffers_dealloc(struct saa7164_port *port)
  70{
  71        struct list_head *c, *n, *p, *q, *l, *v;
  72        struct saa7164_dev *dev = port->dev;
  73        struct saa7164_buffer *buf;
  74        struct saa7164_user_buffer *ubuf;
  75
  76        /* Remove any allocated buffers */
  77        mutex_lock(&port->dmaqueue_lock);
  78
  79        dprintk(DBGLVL_ENC, "%s(port=%d) dmaqueue\n", __func__, port->nr);
  80        list_for_each_safe(c, n, &port->dmaqueue.list) {
  81                buf = list_entry(c, struct saa7164_buffer, list);
  82                list_del(c);
  83                saa7164_buffer_dealloc(buf);
  84        }
  85
  86        dprintk(DBGLVL_ENC, "%s(port=%d) used\n", __func__, port->nr);
  87        list_for_each_safe(p, q, &port->list_buf_used.list) {
  88                ubuf = list_entry(p, struct saa7164_user_buffer, list);
  89                list_del(p);
  90                saa7164_buffer_dealloc_user(ubuf);
  91        }
  92
  93        dprintk(DBGLVL_ENC, "%s(port=%d) free\n", __func__, port->nr);
  94        list_for_each_safe(l, v, &port->list_buf_free.list) {
  95                ubuf = list_entry(l, struct saa7164_user_buffer, list);
  96                list_del(l);
  97                saa7164_buffer_dealloc_user(ubuf);
  98        }
  99
 100        mutex_unlock(&port->dmaqueue_lock);
 101        dprintk(DBGLVL_ENC, "%s(port=%d) done\n", __func__, port->nr);
 102
 103        return 0;
 104}
 105
 106/* Dynamic buffer switch at encoder start time */
 107static int saa7164_encoder_buffers_alloc(struct saa7164_port *port)
 108{
 109        struct saa7164_dev *dev = port->dev;
 110        struct saa7164_buffer *buf;
 111        struct saa7164_user_buffer *ubuf;
 112        struct tmHWStreamParameters *params = &port->hw_streamingparams;
 113        int result = -ENODEV, i;
 114        int len = 0;
 115
 116        dprintk(DBGLVL_ENC, "%s()\n", __func__);
 117
 118        if (port->encoder_params.stream_type ==
 119                V4L2_MPEG_STREAM_TYPE_MPEG2_PS) {
 120                dprintk(DBGLVL_ENC,
 121                        "%s() type=V4L2_MPEG_STREAM_TYPE_MPEG2_PS\n",
 122                        __func__);
 123                params->samplesperline = 128;
 124                params->numberoflines = 256;
 125                params->pitch = 128;
 126                params->numpagetables = 2 +
 127                        ((SAA7164_PS_NUMBER_OF_LINES * 128) / PAGE_SIZE);
 128        } else
 129        if (port->encoder_params.stream_type ==
 130                V4L2_MPEG_STREAM_TYPE_MPEG2_TS) {
 131                dprintk(DBGLVL_ENC,
 132                        "%s() type=V4L2_MPEG_STREAM_TYPE_MPEG2_TS\n",
 133                        __func__);
 134                params->samplesperline = 188;
 135                params->numberoflines = 312;
 136                params->pitch = 188;
 137                params->numpagetables = 2 +
 138                        ((SAA7164_TS_NUMBER_OF_LINES * 188) / PAGE_SIZE);
 139        } else
 140                BUG();
 141
 142        /* Init and establish defaults */
 143        params->bitspersample = 8;
 144        params->linethreshold = 0;
 145        params->pagetablelistvirt = NULL;
 146        params->pagetablelistphys = NULL;
 147        params->numpagetableentries = port->hwcfg.buffercount;
 148
 149        /* Allocate the PCI resources, buffers (hard) */
 150        for (i = 0; i < port->hwcfg.buffercount; i++) {
 151                buf = saa7164_buffer_alloc(port,
 152                        params->numberoflines *
 153                        params->pitch);
 154
 155                if (!buf) {
 156                        printk(KERN_ERR "%s() failed (errno = %d), unable to allocate buffer\n",
 157                                __func__, result);
 158                        result = -ENOMEM;
 159                        goto failed;
 160                } else {
 161
 162                        mutex_lock(&port->dmaqueue_lock);
 163                        list_add_tail(&buf->list, &port->dmaqueue.list);
 164                        mutex_unlock(&port->dmaqueue_lock);
 165
 166                }
 167        }
 168
 169        /* Allocate some kernel buffers for copying
 170         * to userpsace.
 171         */
 172        len = params->numberoflines * params->pitch;
 173
 174        if (encoder_buffers < 16)
 175                encoder_buffers = 16;
 176        if (encoder_buffers > 512)
 177                encoder_buffers = 512;
 178
 179        for (i = 0; i < encoder_buffers; i++) {
 180
 181                ubuf = saa7164_buffer_alloc_user(dev, len);
 182                if (ubuf) {
 183                        mutex_lock(&port->dmaqueue_lock);
 184                        list_add_tail(&ubuf->list, &port->list_buf_free.list);
 185                        mutex_unlock(&port->dmaqueue_lock);
 186                }
 187
 188        }
 189
 190        result = 0;
 191
 192failed:
 193        return result;
 194}
 195
 196static int saa7164_encoder_initialize(struct saa7164_port *port)
 197{
 198        saa7164_encoder_configure(port);
 199        return 0;
 200}
 201
 202/* -- V4L2 --------------------------------------------------------- */
 203int saa7164_s_std(struct saa7164_port *port, v4l2_std_id id)
 204{
 205        struct saa7164_dev *dev = port->dev;
 206        unsigned int i;
 207
 208        dprintk(DBGLVL_ENC, "%s(id=0x%x)\n", __func__, (u32)id);
 209
 210        for (i = 0; i < ARRAY_SIZE(saa7164_tvnorms); i++) {
 211                if (id & saa7164_tvnorms[i].id)
 212                        break;
 213        }
 214        if (i == ARRAY_SIZE(saa7164_tvnorms))
 215                return -EINVAL;
 216
 217        port->encodernorm = saa7164_tvnorms[i];
 218        port->std = id;
 219
 220        /* Update the audio decoder while is not running in
 221         * auto detect mode.
 222         */
 223        saa7164_api_set_audio_std(port);
 224
 225        dprintk(DBGLVL_ENC, "%s(id=0x%x) OK\n", __func__, (u32)id);
 226
 227        return 0;
 228}
 229
 230static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id id)
 231{
 232        struct saa7164_encoder_fh *fh = file->private_data;
 233
 234        return saa7164_s_std(fh->port, id);
 235}
 236
 237int saa7164_g_std(struct saa7164_port *port, v4l2_std_id *id)
 238{
 239        *id = port->std;
 240        return 0;
 241}
 242
 243static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
 244{
 245        struct saa7164_encoder_fh *fh = file->private_data;
 246
 247        return saa7164_g_std(fh->port, id);
 248}
 249
 250int saa7164_enum_input(struct file *file, void *priv, struct v4l2_input *i)
 251{
 252        static const char * const inputs[] = {
 253                "tuner", "composite", "svideo", "aux",
 254                "composite 2", "svideo 2", "aux 2"
 255        };
 256        int n;
 257
 258        if (i->index >= 7)
 259                return -EINVAL;
 260
 261        strscpy(i->name, inputs[i->index], sizeof(i->name));
 262
 263        if (i->index == 0)
 264                i->type = V4L2_INPUT_TYPE_TUNER;
 265        else
 266                i->type  = V4L2_INPUT_TYPE_CAMERA;
 267
 268        for (n = 0; n < ARRAY_SIZE(saa7164_tvnorms); n++)
 269                i->std |= saa7164_tvnorms[n].id;
 270
 271        return 0;
 272}
 273
 274int saa7164_g_input(struct saa7164_port *port, unsigned int *i)
 275{
 276        struct saa7164_dev *dev = port->dev;
 277
 278        if (saa7164_api_get_videomux(port) != SAA_OK)
 279                return -EIO;
 280
 281        *i = (port->mux_input - 1);
 282
 283        dprintk(DBGLVL_ENC, "%s() input=%d\n", __func__, *i);
 284
 285        return 0;
 286}
 287
 288static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
 289{
 290        struct saa7164_encoder_fh *fh = file->private_data;
 291
 292        return saa7164_g_input(fh->port, i);
 293}
 294
 295int saa7164_s_input(struct saa7164_port *port, unsigned int i)
 296{
 297        struct saa7164_dev *dev = port->dev;
 298
 299        dprintk(DBGLVL_ENC, "%s() input=%d\n", __func__, i);
 300
 301        if (i >= 7)
 302                return -EINVAL;
 303
 304        port->mux_input = i + 1;
 305
 306        if (saa7164_api_set_videomux(port) != SAA_OK)
 307                return -EIO;
 308
 309        return 0;
 310}
 311
 312static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
 313{
 314        struct saa7164_encoder_fh *fh = file->private_data;
 315
 316        return saa7164_s_input(fh->port, i);
 317}
 318
 319int saa7164_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
 320{
 321        struct saa7164_encoder_fh *fh = file->private_data;
 322        struct saa7164_port *port = fh->port;
 323        struct saa7164_dev *dev = port->dev;
 324
 325        if (0 != t->index)
 326                return -EINVAL;
 327
 328        strscpy(t->name, "tuner", sizeof(t->name));
 329        t->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO;
 330        t->rangelow = SAA7164_TV_MIN_FREQ;
 331        t->rangehigh = SAA7164_TV_MAX_FREQ;
 332
 333        dprintk(DBGLVL_ENC, "VIDIOC_G_TUNER: tuner type %d\n", t->type);
 334
 335        return 0;
 336}
 337
 338int saa7164_s_tuner(struct file *file, void *priv,
 339                           const struct v4l2_tuner *t)
 340{
 341        if (0 != t->index)
 342                return -EINVAL;
 343
 344        /* Update the A/V core */
 345        return 0;
 346}
 347
 348int saa7164_g_frequency(struct saa7164_port *port, struct v4l2_frequency *f)
 349{
 350        if (f->tuner)
 351                return -EINVAL;
 352
 353        f->frequency = port->freq;
 354        return 0;
 355}
 356
 357static int vidioc_g_frequency(struct file *file, void *priv,
 358        struct v4l2_frequency *f)
 359{
 360        struct saa7164_encoder_fh *fh = file->private_data;
 361
 362        return saa7164_g_frequency(fh->port, f);
 363}
 364
 365int saa7164_s_frequency(struct saa7164_port *port,
 366                        const struct v4l2_frequency *f)
 367{
 368        struct saa7164_dev *dev = port->dev;
 369        struct saa7164_port *tsport;
 370        struct dvb_frontend *fe;
 371
 372        /* TODO: Pull this for the std */
 373        struct analog_parameters params = {
 374                .mode      = V4L2_TUNER_ANALOG_TV,
 375                .audmode   = V4L2_TUNER_MODE_STEREO,
 376                .std       = port->encodernorm.id,
 377                .frequency = f->frequency
 378        };
 379
 380        /* Stop the encoder */
 381        dprintk(DBGLVL_ENC, "%s() frequency=%d tuner=%d\n", __func__,
 382                f->frequency, f->tuner);
 383
 384        if (f->tuner != 0)
 385                return -EINVAL;
 386
 387        port->freq = clamp(f->frequency,
 388                           SAA7164_TV_MIN_FREQ, SAA7164_TV_MAX_FREQ);
 389
 390        /* Update the hardware */
 391        if (port->nr == SAA7164_PORT_ENC1)
 392                tsport = &dev->ports[SAA7164_PORT_TS1];
 393        else if (port->nr == SAA7164_PORT_ENC2)
 394                tsport = &dev->ports[SAA7164_PORT_TS2];
 395        else
 396                BUG();
 397
 398        fe = tsport->dvb.frontend;
 399
 400        if (fe && fe->ops.tuner_ops.set_analog_params)
 401                fe->ops.tuner_ops.set_analog_params(fe, &params);
 402        else
 403                printk(KERN_ERR "%s() No analog tuner, aborting\n", __func__);
 404
 405        saa7164_encoder_initialize(port);
 406
 407        return 0;
 408}
 409
 410static int vidioc_s_frequency(struct file *file, void *priv,
 411                              const struct v4l2_frequency *f)
 412{
 413        struct saa7164_encoder_fh *fh = file->private_data;
 414
 415        return saa7164_s_frequency(fh->port, f);
 416}
 417
 418static int saa7164_s_ctrl(struct v4l2_ctrl *ctrl)
 419{
 420        struct saa7164_port *port =
 421                container_of(ctrl->handler, struct saa7164_port, ctrl_handler);
 422        struct saa7164_encoder_params *params = &port->encoder_params;
 423        int ret = 0;
 424
 425        switch (ctrl->id) {
 426        case V4L2_CID_BRIGHTNESS:
 427                port->ctl_brightness = ctrl->val;
 428                saa7164_api_set_usercontrol(port, PU_BRIGHTNESS_CONTROL);
 429                break;
 430        case V4L2_CID_CONTRAST:
 431                port->ctl_contrast = ctrl->val;
 432                saa7164_api_set_usercontrol(port, PU_CONTRAST_CONTROL);
 433                break;
 434        case V4L2_CID_SATURATION:
 435                port->ctl_saturation = ctrl->val;
 436                saa7164_api_set_usercontrol(port, PU_SATURATION_CONTROL);
 437                break;
 438        case V4L2_CID_HUE:
 439                port->ctl_hue = ctrl->val;
 440                saa7164_api_set_usercontrol(port, PU_HUE_CONTROL);
 441                break;
 442        case V4L2_CID_SHARPNESS:
 443                port->ctl_sharpness = ctrl->val;
 444                saa7164_api_set_usercontrol(port, PU_SHARPNESS_CONTROL);
 445                break;
 446        case V4L2_CID_AUDIO_VOLUME:
 447                port->ctl_volume = ctrl->val;
 448                saa7164_api_set_audio_volume(port, port->ctl_volume);
 449                break;
 450        case V4L2_CID_MPEG_VIDEO_BITRATE:
 451                params->bitrate = ctrl->val;
 452                break;
 453        case V4L2_CID_MPEG_STREAM_TYPE:
 454                params->stream_type = ctrl->val;
 455                break;
 456        case V4L2_CID_MPEG_AUDIO_MUTE:
 457                params->ctl_mute = ctrl->val;
 458                ret = saa7164_api_audio_mute(port, params->ctl_mute);
 459                if (ret != SAA_OK) {
 460                        printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__,
 461                                ret);
 462                        ret = -EIO;
 463                }
 464                break;
 465        case V4L2_CID_MPEG_VIDEO_ASPECT:
 466                params->ctl_aspect = ctrl->val;
 467                ret = saa7164_api_set_aspect_ratio(port);
 468                if (ret != SAA_OK) {
 469                        printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__,
 470                                ret);
 471                        ret = -EIO;
 472                }
 473                break;
 474        case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
 475                params->bitrate_mode = ctrl->val;
 476                break;
 477        case V4L2_CID_MPEG_VIDEO_B_FRAMES:
 478                params->refdist = ctrl->val;
 479                break;
 480        case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
 481                params->bitrate_peak = ctrl->val;
 482                break;
 483        case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
 484                params->gop_size = ctrl->val;
 485                break;
 486        default:
 487                ret = -EINVAL;
 488        }
 489
 490        return ret;
 491}
 492
 493static int vidioc_querycap(struct file *file, void  *priv,
 494        struct v4l2_capability *cap)
 495{
 496        struct saa7164_encoder_fh *fh = file->private_data;
 497        struct saa7164_port *port = fh->port;
 498        struct saa7164_dev *dev = port->dev;
 499
 500        strscpy(cap->driver, dev->name, sizeof(cap->driver));
 501        strscpy(cap->card, saa7164_boards[dev->board].name,
 502                sizeof(cap->card));
 503        sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
 504
 505        cap->device_caps =
 506                V4L2_CAP_VIDEO_CAPTURE |
 507                V4L2_CAP_READWRITE |
 508                V4L2_CAP_TUNER;
 509
 510        cap->capabilities = cap->device_caps |
 511                V4L2_CAP_VBI_CAPTURE |
 512                V4L2_CAP_DEVICE_CAPS;
 513
 514        return 0;
 515}
 516
 517static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
 518        struct v4l2_fmtdesc *f)
 519{
 520        if (f->index != 0)
 521                return -EINVAL;
 522
 523        strscpy(f->description, "MPEG", sizeof(f->description));
 524        f->pixelformat = V4L2_PIX_FMT_MPEG;
 525
 526        return 0;
 527}
 528
 529static int vidioc_fmt_vid_cap(struct file *file, void *priv,
 530                                struct v4l2_format *f)
 531{
 532        struct saa7164_encoder_fh *fh = file->private_data;
 533        struct saa7164_port *port = fh->port;
 534
 535        f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
 536        f->fmt.pix.bytesperline = 0;
 537        f->fmt.pix.sizeimage    = SAA7164_SIZEIMAGE;
 538        f->fmt.pix.field        = V4L2_FIELD_INTERLACED;
 539        f->fmt.pix.colorspace   = V4L2_COLORSPACE_SMPTE170M;
 540        f->fmt.pix.width        = port->width;
 541        f->fmt.pix.height       = port->height;
 542        return 0;
 543}
 544
 545static int saa7164_encoder_stop_port(struct saa7164_port *port)
 546{
 547        struct saa7164_dev *dev = port->dev;
 548        int ret;
 549
 550        ret = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
 551        if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
 552                printk(KERN_ERR "%s() stop transition failed, ret = 0x%x\n",
 553                        __func__, ret);
 554                ret = -EIO;
 555        } else {
 556                dprintk(DBGLVL_ENC, "%s()    Stopped\n", __func__);
 557                ret = 0;
 558        }
 559
 560        return ret;
 561}
 562
 563static int saa7164_encoder_acquire_port(struct saa7164_port *port)
 564{
 565        struct saa7164_dev *dev = port->dev;
 566        int ret;
 567
 568        ret = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
 569        if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
 570                printk(KERN_ERR "%s() acquire transition failed, ret = 0x%x\n",
 571                        __func__, ret);
 572                ret = -EIO;
 573        } else {
 574                dprintk(DBGLVL_ENC, "%s() Acquired\n", __func__);
 575                ret = 0;
 576        }
 577
 578        return ret;
 579}
 580
 581static int saa7164_encoder_pause_port(struct saa7164_port *port)
 582{
 583        struct saa7164_dev *dev = port->dev;
 584        int ret;
 585
 586        ret = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
 587        if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
 588                printk(KERN_ERR "%s() pause transition failed, ret = 0x%x\n",
 589                        __func__, ret);
 590                ret = -EIO;
 591        } else {
 592                dprintk(DBGLVL_ENC, "%s()   Paused\n", __func__);
 593                ret = 0;
 594        }
 595
 596        return ret;
 597}
 598
 599/* Firmware is very windows centric, meaning you have to transition
 600 * the part through AVStream / KS Windows stages, forwards or backwards.
 601 * States are: stopped, acquired (h/w), paused, started.
 602 * We have to leave here will all of the soft buffers on the free list,
 603 * else the cfg_post() func won't have soft buffers to correctly configure.
 604 */
 605static int saa7164_encoder_stop_streaming(struct saa7164_port *port)
 606{
 607        struct saa7164_dev *dev = port->dev;
 608        struct saa7164_buffer *buf;
 609        struct saa7164_user_buffer *ubuf;
 610        struct list_head *c, *n;
 611        int ret;
 612
 613        dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr);
 614
 615        ret = saa7164_encoder_pause_port(port);
 616        ret = saa7164_encoder_acquire_port(port);
 617        ret = saa7164_encoder_stop_port(port);
 618
 619        dprintk(DBGLVL_ENC, "%s(port=%d) Hardware stopped\n", __func__,
 620                port->nr);
 621
 622        /* Reset the state of any allocated buffer resources */
 623        mutex_lock(&port->dmaqueue_lock);
 624
 625        /* Reset the hard and soft buffer state */
 626        list_for_each_safe(c, n, &port->dmaqueue.list) {
 627                buf = list_entry(c, struct saa7164_buffer, list);
 628                buf->flags = SAA7164_BUFFER_FREE;
 629                buf->pos = 0;
 630        }
 631
 632        list_for_each_safe(c, n, &port->list_buf_used.list) {
 633                ubuf = list_entry(c, struct saa7164_user_buffer, list);
 634                ubuf->pos = 0;
 635                list_move_tail(&ubuf->list, &port->list_buf_free.list);
 636        }
 637
 638        mutex_unlock(&port->dmaqueue_lock);
 639
 640        /* Free any allocated resources */
 641        saa7164_encoder_buffers_dealloc(port);
 642
 643        dprintk(DBGLVL_ENC, "%s(port=%d) Released\n", __func__, port->nr);
 644
 645        return ret;
 646}
 647
 648static int saa7164_encoder_start_streaming(struct saa7164_port *port)
 649{
 650        struct saa7164_dev *dev = port->dev;
 651        int result, ret = 0;
 652
 653        dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr);
 654
 655        port->done_first_interrupt = 0;
 656
 657        /* allocate all of the PCIe DMA buffer resources on the fly,
 658         * allowing switching between TS and PS payloads without
 659         * requiring a complete driver reload.
 660         */
 661        saa7164_encoder_buffers_alloc(port);
 662
 663        /* Configure the encoder with any cache values */
 664        saa7164_api_set_encoder(port);
 665        saa7164_api_get_encoder(port);
 666
 667        /* Place the empty buffers on the hardware */
 668        saa7164_buffer_cfg_port(port);
 669
 670        /* Acquire the hardware */
 671        result = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
 672        if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
 673                printk(KERN_ERR "%s() acquire transition failed, res = 0x%x\n",
 674                        __func__, result);
 675
 676                /* Stop the hardware, regardless */
 677                result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
 678                if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
 679                        printk(KERN_ERR "%s() acquire/forced stop transition failed, res = 0x%x\n",
 680                               __func__, result);
 681                }
 682                ret = -EIO;
 683                goto out;
 684        } else
 685                dprintk(DBGLVL_ENC, "%s()   Acquired\n", __func__);
 686
 687        /* Pause the hardware */
 688        result = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
 689        if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
 690                printk(KERN_ERR "%s() pause transition failed, res = 0x%x\n",
 691                                __func__, result);
 692
 693                /* Stop the hardware, regardless */
 694                result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
 695                if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
 696                        printk(KERN_ERR "%s() pause/forced stop transition failed, res = 0x%x\n",
 697                               __func__, result);
 698                }
 699
 700                ret = -EIO;
 701                goto out;
 702        } else
 703                dprintk(DBGLVL_ENC, "%s()   Paused\n", __func__);
 704
 705        /* Start the hardware */
 706        result = saa7164_api_transition_port(port, SAA_DMASTATE_RUN);
 707        if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
 708                printk(KERN_ERR "%s() run transition failed, result = 0x%x\n",
 709                                __func__, result);
 710
 711                /* Stop the hardware, regardless */
 712                result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
 713                if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
 714                        printk(KERN_ERR "%s() run/forced stop transition failed, res = 0x%x\n",
 715                               __func__, result);
 716                }
 717
 718                ret = -EIO;
 719        } else
 720                dprintk(DBGLVL_ENC, "%s()   Running\n", __func__);
 721
 722out:
 723        return ret;
 724}
 725
 726static int fops_open(struct file *file)
 727{
 728        struct saa7164_dev *dev;
 729        struct saa7164_port *port;
 730        struct saa7164_encoder_fh *fh;
 731
 732        port = (struct saa7164_port *)video_get_drvdata(video_devdata(file));
 733        if (!port)
 734                return -ENODEV;
 735
 736        dev = port->dev;
 737
 738        dprintk(DBGLVL_ENC, "%s()\n", __func__);
 739
 740        /* allocate + initialize per filehandle data */
 741        fh = kzalloc(sizeof(*fh), GFP_KERNEL);
 742        if (NULL == fh)
 743                return -ENOMEM;
 744
 745        fh->port = port;
 746        v4l2_fh_init(&fh->fh, video_devdata(file));
 747        v4l2_fh_add(&fh->fh);
 748        file->private_data = fh;
 749
 750        return 0;
 751}
 752
 753static int fops_release(struct file *file)
 754{
 755        struct saa7164_encoder_fh *fh = file->private_data;
 756        struct saa7164_port *port = fh->port;
 757        struct saa7164_dev *dev = port->dev;
 758
 759        dprintk(DBGLVL_ENC, "%s()\n", __func__);
 760
 761        /* Shut device down on last close */
 762        if (atomic_cmpxchg(&fh->v4l_reading, 1, 0) == 1) {
 763                if (atomic_dec_return(&port->v4l_reader_count) == 0) {
 764                        /* stop mpeg capture then cancel buffers */
 765                        saa7164_encoder_stop_streaming(port);
 766                }
 767        }
 768
 769        v4l2_fh_del(&fh->fh);
 770        v4l2_fh_exit(&fh->fh);
 771        kfree(fh);
 772
 773        return 0;
 774}
 775
 776static struct
 777saa7164_user_buffer *saa7164_enc_next_buf(struct saa7164_port *port)
 778{
 779        struct saa7164_user_buffer *ubuf = NULL;
 780        struct saa7164_dev *dev = port->dev;
 781        u32 crc;
 782
 783        mutex_lock(&port->dmaqueue_lock);
 784        if (!list_empty(&port->list_buf_used.list)) {
 785                ubuf = list_first_entry(&port->list_buf_used.list,
 786                        struct saa7164_user_buffer, list);
 787
 788                if (crc_checking) {
 789                        crc = crc32(0, ubuf->data, ubuf->actual_size);
 790                        if (crc != ubuf->crc) {
 791                                printk(KERN_ERR
 792                "%s() ubuf %p crc became invalid, was 0x%x became 0x%x\n",
 793                                        __func__,
 794                                        ubuf, ubuf->crc, crc);
 795                        }
 796                }
 797
 798        }
 799        mutex_unlock(&port->dmaqueue_lock);
 800
 801        dprintk(DBGLVL_ENC, "%s() returns %p\n", __func__, ubuf);
 802
 803        return ubuf;
 804}
 805
 806static ssize_t fops_read(struct file *file, char __user *buffer,
 807        size_t count, loff_t *pos)
 808{
 809        struct saa7164_encoder_fh *fh = file->private_data;
 810        struct saa7164_port *port = fh->port;
 811        struct saa7164_user_buffer *ubuf = NULL;
 812        struct saa7164_dev *dev = port->dev;
 813        int ret = 0;
 814        int rem, cnt;
 815        u8 *p;
 816
 817        port->last_read_msecs_diff = port->last_read_msecs;
 818        port->last_read_msecs = jiffies_to_msecs(jiffies);
 819        port->last_read_msecs_diff = port->last_read_msecs -
 820                port->last_read_msecs_diff;
 821
 822        saa7164_histogram_update(&port->read_interval,
 823                port->last_read_msecs_diff);
 824
 825        if (*pos) {
 826                printk(KERN_ERR "%s() ESPIPE\n", __func__);
 827                return -ESPIPE;
 828        }
 829
 830        if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
 831                if (atomic_inc_return(&port->v4l_reader_count) == 1) {
 832
 833                        if (saa7164_encoder_initialize(port) < 0) {
 834                                printk(KERN_ERR "%s() EINVAL\n", __func__);
 835                                return -EINVAL;
 836                        }
 837
 838                        saa7164_encoder_start_streaming(port);
 839                        msleep(200);
 840                }
 841        }
 842
 843        /* blocking wait for buffer */
 844        if ((file->f_flags & O_NONBLOCK) == 0) {
 845                if (wait_event_interruptible(port->wait_read,
 846                        saa7164_enc_next_buf(port))) {
 847                                printk(KERN_ERR "%s() ERESTARTSYS\n", __func__);
 848                                return -ERESTARTSYS;
 849                }
 850        }
 851
 852        /* Pull the first buffer from the used list */
 853        ubuf = saa7164_enc_next_buf(port);
 854
 855        while ((count > 0) && ubuf) {
 856
 857                /* set remaining bytes to copy */
 858                rem = ubuf->actual_size - ubuf->pos;
 859                cnt = rem > count ? count : rem;
 860
 861                p = ubuf->data + ubuf->pos;
 862
 863                dprintk(DBGLVL_ENC,
 864                        "%s() count=%d cnt=%d rem=%d buf=%p buf->pos=%d\n",
 865                        __func__, (int)count, cnt, rem, ubuf, ubuf->pos);
 866
 867                if (copy_to_user(buffer, p, cnt)) {
 868                        printk(KERN_ERR "%s() copy_to_user failed\n", __func__);
 869                        if (!ret) {
 870                                printk(KERN_ERR "%s() EFAULT\n", __func__);
 871                                ret = -EFAULT;
 872                        }
 873                        goto err;
 874                }
 875
 876                ubuf->pos += cnt;
 877                count -= cnt;
 878                buffer += cnt;
 879                ret += cnt;
 880
 881                if (ubuf->pos > ubuf->actual_size)
 882                        printk(KERN_ERR "read() pos > actual, huh?\n");
 883
 884                if (ubuf->pos == ubuf->actual_size) {
 885
 886                        /* finished with current buffer, take next buffer */
 887
 888                        /* Requeue the buffer on the free list */
 889                        ubuf->pos = 0;
 890
 891                        mutex_lock(&port->dmaqueue_lock);
 892                        list_move_tail(&ubuf->list, &port->list_buf_free.list);
 893                        mutex_unlock(&port->dmaqueue_lock);
 894
 895                        /* Dequeue next */
 896                        if ((file->f_flags & O_NONBLOCK) == 0) {
 897                                if (wait_event_interruptible(port->wait_read,
 898                                        saa7164_enc_next_buf(port))) {
 899                                                break;
 900                                }
 901                        }
 902                        ubuf = saa7164_enc_next_buf(port);
 903                }
 904        }
 905err:
 906        if (!ret && !ubuf)
 907                ret = -EAGAIN;
 908
 909        return ret;
 910}
 911
 912static __poll_t fops_poll(struct file *file, poll_table *wait)
 913{
 914        __poll_t req_events = poll_requested_events(wait);
 915        struct saa7164_encoder_fh *fh =
 916                (struct saa7164_encoder_fh *)file->private_data;
 917        struct saa7164_port *port = fh->port;
 918        __poll_t mask = v4l2_ctrl_poll(file, wait);
 919
 920        port->last_poll_msecs_diff = port->last_poll_msecs;
 921        port->last_poll_msecs = jiffies_to_msecs(jiffies);
 922        port->last_poll_msecs_diff = port->last_poll_msecs -
 923                port->last_poll_msecs_diff;
 924
 925        saa7164_histogram_update(&port->poll_interval,
 926                port->last_poll_msecs_diff);
 927
 928        if (!(req_events & (EPOLLIN | EPOLLRDNORM)))
 929                return mask;
 930
 931        if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
 932                if (atomic_inc_return(&port->v4l_reader_count) == 1) {
 933                        if (saa7164_encoder_initialize(port) < 0)
 934                                return mask | EPOLLERR;
 935                        saa7164_encoder_start_streaming(port);
 936                        msleep(200);
 937                }
 938        }
 939
 940        /* Pull the first buffer from the used list */
 941        if (!list_empty(&port->list_buf_used.list))
 942                mask |= EPOLLIN | EPOLLRDNORM;
 943
 944        return mask;
 945}
 946
 947static const struct v4l2_ctrl_ops saa7164_ctrl_ops = {
 948        .s_ctrl = saa7164_s_ctrl,
 949};
 950
 951static const struct v4l2_file_operations mpeg_fops = {
 952        .owner          = THIS_MODULE,
 953        .open           = fops_open,
 954        .release        = fops_release,
 955        .read           = fops_read,
 956        .poll           = fops_poll,
 957        .unlocked_ioctl = video_ioctl2,
 958};
 959
 960static const struct v4l2_ioctl_ops mpeg_ioctl_ops = {
 961        .vidioc_s_std            = vidioc_s_std,
 962        .vidioc_g_std            = vidioc_g_std,
 963        .vidioc_enum_input       = saa7164_enum_input,
 964        .vidioc_g_input          = vidioc_g_input,
 965        .vidioc_s_input          = vidioc_s_input,
 966        .vidioc_g_tuner          = saa7164_g_tuner,
 967        .vidioc_s_tuner          = saa7164_s_tuner,
 968        .vidioc_g_frequency      = vidioc_g_frequency,
 969        .vidioc_s_frequency      = vidioc_s_frequency,
 970        .vidioc_querycap         = vidioc_querycap,
 971        .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
 972        .vidioc_g_fmt_vid_cap    = vidioc_fmt_vid_cap,
 973        .vidioc_try_fmt_vid_cap  = vidioc_fmt_vid_cap,
 974        .vidioc_s_fmt_vid_cap    = vidioc_fmt_vid_cap,
 975        .vidioc_log_status       = v4l2_ctrl_log_status,
 976        .vidioc_subscribe_event  = v4l2_ctrl_subscribe_event,
 977        .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
 978};
 979
 980static struct video_device saa7164_mpeg_template = {
 981        .name          = "saa7164",
 982        .fops          = &mpeg_fops,
 983        .ioctl_ops     = &mpeg_ioctl_ops,
 984        .minor         = -1,
 985        .tvnorms       = SAA7164_NORMS,
 986};
 987
 988static struct video_device *saa7164_encoder_alloc(
 989        struct saa7164_port *port,
 990        struct pci_dev *pci,
 991        struct video_device *template,
 992        char *type)
 993{
 994        struct video_device *vfd;
 995        struct saa7164_dev *dev = port->dev;
 996
 997        dprintk(DBGLVL_ENC, "%s()\n", __func__);
 998
 999        vfd = video_device_alloc();
1000        if (NULL == vfd)
1001                return NULL;
1002
1003        *vfd = *template;
1004        snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", dev->name,
1005                type, saa7164_boards[dev->board].name);
1006
1007        vfd->v4l2_dev  = &dev->v4l2_dev;
1008        vfd->release = video_device_release;
1009        return vfd;
1010}
1011
1012int saa7164_encoder_register(struct saa7164_port *port)
1013{
1014        struct saa7164_dev *dev = port->dev;
1015        struct v4l2_ctrl_handler *hdl = &port->ctrl_handler;
1016        int result = -ENODEV;
1017
1018        dprintk(DBGLVL_ENC, "%s()\n", __func__);
1019
1020        BUG_ON(port->type != SAA7164_MPEG_ENCODER);
1021
1022        /* Sanity check that the PCI configuration space is active */
1023        if (port->hwcfg.BARLocation == 0) {
1024                printk(KERN_ERR "%s() failed (errno = %d), NO PCI configuration\n",
1025                        __func__, result);
1026                result = -ENOMEM;
1027                goto failed;
1028        }
1029
1030        /* Establish encoder defaults here */
1031        /* Set default TV standard */
1032        port->encodernorm = saa7164_tvnorms[0];
1033        port->width = 720;
1034        port->mux_input = 1; /* Composite */
1035        port->video_format = EU_VIDEO_FORMAT_MPEG_2;
1036        port->audio_format = 0;
1037        port->video_resolution = 0;
1038        port->freq = SAA7164_TV_MIN_FREQ;
1039
1040        v4l2_ctrl_handler_init(hdl, 14);
1041        v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops,
1042                          V4L2_CID_BRIGHTNESS, 0, 255, 1, 127);
1043        v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops,
1044                          V4L2_CID_CONTRAST, 0, 255, 1, 66);
1045        v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops,
1046                          V4L2_CID_SATURATION, 0, 255, 1, 62);
1047        v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops,
1048                          V4L2_CID_HUE, 0, 255, 1, 128);
1049        v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops,
1050                          V4L2_CID_SHARPNESS, 0x0, 0x0f, 1, 8);
1051        v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops,
1052                          V4L2_CID_MPEG_AUDIO_MUTE, 0x0, 0x01, 1, 0);
1053        v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops,
1054                          V4L2_CID_AUDIO_VOLUME, -83, 24, 1, 20);
1055        v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops,
1056                          V4L2_CID_MPEG_VIDEO_BITRATE,
1057                          ENCODER_MIN_BITRATE, ENCODER_MAX_BITRATE,
1058                          100000, ENCODER_DEF_BITRATE);
1059        v4l2_ctrl_new_std_menu(hdl, &saa7164_ctrl_ops,
1060                               V4L2_CID_MPEG_STREAM_TYPE,
1061                               V4L2_MPEG_STREAM_TYPE_MPEG2_TS, 0,
1062                               V4L2_MPEG_STREAM_TYPE_MPEG2_PS);
1063        v4l2_ctrl_new_std_menu(hdl, &saa7164_ctrl_ops,
1064                               V4L2_CID_MPEG_VIDEO_ASPECT,
1065                               V4L2_MPEG_VIDEO_ASPECT_221x100, 0,
1066                               V4L2_MPEG_VIDEO_ASPECT_4x3);
1067        v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops,
1068                          V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 255, 1, 15);
1069        v4l2_ctrl_new_std_menu(hdl, &saa7164_ctrl_ops,
1070                               V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
1071                               V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 0,
1072                               V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
1073        v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops,
1074                          V4L2_CID_MPEG_VIDEO_B_FRAMES, 1, 3, 1, 1);
1075        v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops,
1076                          V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
1077                          ENCODER_MIN_BITRATE, ENCODER_MAX_BITRATE,
1078                          100000, ENCODER_DEF_BITRATE);
1079        if (hdl->error) {
1080                result = hdl->error;
1081                goto failed;
1082        }
1083
1084        port->std = V4L2_STD_NTSC_M;
1085
1086        if (port->encodernorm.id & V4L2_STD_525_60)
1087                port->height = 480;
1088        else
1089                port->height = 576;
1090
1091        /* Allocate and register the video device node */
1092        port->v4l_device = saa7164_encoder_alloc(port,
1093                dev->pci, &saa7164_mpeg_template, "mpeg");
1094
1095        if (!port->v4l_device) {
1096                printk(KERN_INFO "%s: can't allocate mpeg device\n",
1097                        dev->name);
1098                result = -ENOMEM;
1099                goto failed;
1100        }
1101
1102        port->v4l_device->ctrl_handler = hdl;
1103        v4l2_ctrl_handler_setup(hdl);
1104        video_set_drvdata(port->v4l_device, port);
1105        result = video_register_device(port->v4l_device,
1106                VFL_TYPE_GRABBER, -1);
1107        if (result < 0) {
1108                printk(KERN_INFO "%s: can't register mpeg device\n",
1109                        dev->name);
1110                /* TODO: We're going to leak here if we don't dealloc
1111                 The buffers above. The unreg function can't deal wit it.
1112                */
1113                goto failed;
1114        }
1115
1116        printk(KERN_INFO "%s: registered device video%d [mpeg]\n",
1117                dev->name, port->v4l_device->num);
1118
1119        /* Configure the hardware defaults */
1120        saa7164_api_set_videomux(port);
1121        saa7164_api_set_usercontrol(port, PU_BRIGHTNESS_CONTROL);
1122        saa7164_api_set_usercontrol(port, PU_CONTRAST_CONTROL);
1123        saa7164_api_set_usercontrol(port, PU_HUE_CONTROL);
1124        saa7164_api_set_usercontrol(port, PU_SATURATION_CONTROL);
1125        saa7164_api_set_usercontrol(port, PU_SHARPNESS_CONTROL);
1126        saa7164_api_audio_mute(port, 0);
1127        saa7164_api_set_audio_volume(port, 20);
1128        saa7164_api_set_aspect_ratio(port);
1129
1130        /* Disable audio standard detection, it's buggy */
1131        saa7164_api_set_audio_detection(port, 0);
1132
1133        saa7164_api_set_encoder(port);
1134        saa7164_api_get_encoder(port);
1135
1136        result = 0;
1137failed:
1138        return result;
1139}
1140
1141void saa7164_encoder_unregister(struct saa7164_port *port)
1142{
1143        struct saa7164_dev *dev = port->dev;
1144
1145        dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr);
1146
1147        BUG_ON(port->type != SAA7164_MPEG_ENCODER);
1148
1149        if (port->v4l_device) {
1150                if (port->v4l_device->minor != -1)
1151                        video_unregister_device(port->v4l_device);
1152                else
1153                        video_device_release(port->v4l_device);
1154
1155                port->v4l_device = NULL;
1156        }
1157        v4l2_ctrl_handler_free(&port->ctrl_handler);
1158
1159        dprintk(DBGLVL_ENC, "%s(port=%d) done\n", __func__, port->nr);
1160}
1161
1162