linux/drivers/media/pci/cx23885/cx23885-video.c
<<
>>
Prefs
   1/*
   2 *  Driver for the Conexant CX23885 PCIe bridge
   3 *
   4 *  Copyright (c) 2007 Steven Toth <stoth@linuxtv.org>
   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 <linux/init.h>
  19#include <linux/list.h>
  20#include <linux/module.h>
  21#include <linux/moduleparam.h>
  22#include <linux/kmod.h>
  23#include <linux/kernel.h>
  24#include <linux/slab.h>
  25#include <linux/interrupt.h>
  26#include <linux/delay.h>
  27#include <linux/kthread.h>
  28#include <asm/div64.h>
  29
  30#include "cx23885.h"
  31#include "cx23885-video.h"
  32#include <media/v4l2-common.h>
  33#include <media/v4l2-ioctl.h>
  34#include <media/v4l2-event.h>
  35#include "cx23885-ioctl.h"
  36#include "tuner-xc2028.h"
  37
  38#include <media/drv-intf/cx25840.h>
  39
  40MODULE_DESCRIPTION("v4l2 driver module for cx23885 based TV cards");
  41MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
  42MODULE_LICENSE("GPL");
  43
  44/* ------------------------------------------------------------------ */
  45
  46static unsigned int video_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
  47static unsigned int vbi_nr[]   = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
  48
  49module_param_array(video_nr, int, NULL, 0444);
  50module_param_array(vbi_nr,   int, NULL, 0444);
  51
  52MODULE_PARM_DESC(video_nr, "video device numbers");
  53MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
  54
  55static unsigned int video_debug;
  56module_param(video_debug, int, 0644);
  57MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
  58
  59static unsigned int irq_debug;
  60module_param(irq_debug, int, 0644);
  61MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
  62
  63static unsigned int vid_limit = 16;
  64module_param(vid_limit, int, 0644);
  65MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
  66
  67#define dprintk(level, fmt, arg...)\
  68        do { if (video_debug >= level)\
  69                printk(KERN_DEBUG "%s: " fmt, dev->name, ## arg);\
  70        } while (0)
  71
  72/* ------------------------------------------------------------------- */
  73/* static data                                                         */
  74
  75#define FORMAT_FLAGS_PACKED       0x01
  76static struct cx23885_fmt formats[] = {
  77        {
  78                .name     = "4:2:2, packed, YUYV",
  79                .fourcc   = V4L2_PIX_FMT_YUYV,
  80                .depth    = 16,
  81                .flags    = FORMAT_FLAGS_PACKED,
  82        }
  83};
  84
  85static struct cx23885_fmt *format_by_fourcc(unsigned int fourcc)
  86{
  87        unsigned int i;
  88
  89        for (i = 0; i < ARRAY_SIZE(formats); i++)
  90                if (formats[i].fourcc == fourcc)
  91                        return formats+i;
  92        return NULL;
  93}
  94
  95/* ------------------------------------------------------------------- */
  96
  97void cx23885_video_wakeup(struct cx23885_dev *dev,
  98        struct cx23885_dmaqueue *q, u32 count)
  99{
 100        struct cx23885_buffer *buf;
 101
 102        if (list_empty(&q->active))
 103                return;
 104        buf = list_entry(q->active.next,
 105                        struct cx23885_buffer, queue);
 106
 107        buf->vb.sequence = q->count++;
 108        buf->vb.vb2_buf.timestamp = ktime_get_ns();
 109        dprintk(2, "[%p/%d] wakeup reg=%d buf=%d\n", buf,
 110                        buf->vb.vb2_buf.index, count, q->count);
 111        list_del(&buf->queue);
 112        vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
 113}
 114
 115int cx23885_set_tvnorm(struct cx23885_dev *dev, v4l2_std_id norm)
 116{
 117        struct v4l2_subdev_format format = {
 118                .which = V4L2_SUBDEV_FORMAT_ACTIVE,
 119                .format.code = MEDIA_BUS_FMT_FIXED,
 120        };
 121
 122        dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
 123                __func__,
 124                (unsigned int)norm,
 125                v4l2_norm_to_name(norm));
 126
 127        if (dev->tvnorm == norm)
 128                return 0;
 129
 130        if (dev->tvnorm != norm) {
 131                if (vb2_is_busy(&dev->vb2_vidq) || vb2_is_busy(&dev->vb2_vbiq) ||
 132                    vb2_is_busy(&dev->vb2_mpegq))
 133                        return -EBUSY;
 134        }
 135
 136        dev->tvnorm = norm;
 137        dev->width = 720;
 138        dev->height = norm_maxh(norm);
 139        dev->field = V4L2_FIELD_INTERLACED;
 140
 141        call_all(dev, video, s_std, norm);
 142
 143        format.format.width = dev->width;
 144        format.format.height = dev->height;
 145        format.format.field = dev->field;
 146        call_all(dev, pad, set_fmt, NULL, &format);
 147
 148        return 0;
 149}
 150
 151static struct video_device *cx23885_vdev_init(struct cx23885_dev *dev,
 152                                    struct pci_dev *pci,
 153                                    struct video_device *template,
 154                                    char *type)
 155{
 156        struct video_device *vfd;
 157        dprintk(1, "%s()\n", __func__);
 158
 159        vfd = video_device_alloc();
 160        if (NULL == vfd)
 161                return NULL;
 162        *vfd = *template;
 163        vfd->v4l2_dev = &dev->v4l2_dev;
 164        vfd->release = video_device_release;
 165        vfd->lock = &dev->lock;
 166        snprintf(vfd->name, sizeof(vfd->name), "%s (%s)",
 167                 cx23885_boards[dev->board].name, type);
 168        video_set_drvdata(vfd, dev);
 169        return vfd;
 170}
 171
 172int cx23885_flatiron_write(struct cx23885_dev *dev, u8 reg, u8 data)
 173{
 174        /* 8 bit registers, 8 bit values */
 175        u8 buf[] = { reg, data };
 176
 177        struct i2c_msg msg = { .addr = 0x98 >> 1,
 178                .flags = 0, .buf = buf, .len = 2 };
 179
 180        return i2c_transfer(&dev->i2c_bus[2].i2c_adap, &msg, 1);
 181}
 182
 183u8 cx23885_flatiron_read(struct cx23885_dev *dev, u8 reg)
 184{
 185        /* 8 bit registers, 8 bit values */
 186        int ret;
 187        u8 b0[] = { reg };
 188        u8 b1[] = { 0 };
 189
 190        struct i2c_msg msg[] = {
 191                { .addr = 0x98 >> 1, .flags = 0, .buf = b0, .len = 1 },
 192                { .addr = 0x98 >> 1, .flags = I2C_M_RD, .buf = b1, .len = 1 }
 193        };
 194
 195        ret = i2c_transfer(&dev->i2c_bus[2].i2c_adap, &msg[0], 2);
 196        if (ret != 2)
 197                printk(KERN_ERR "%s() error\n", __func__);
 198
 199        return b1[0];
 200}
 201
 202static void cx23885_flatiron_dump(struct cx23885_dev *dev)
 203{
 204        int i;
 205        dprintk(1, "Flatiron dump\n");
 206        for (i = 0; i < 0x24; i++) {
 207                dprintk(1, "FI[%02x] = %02x\n", i,
 208                        cx23885_flatiron_read(dev, i));
 209        }
 210}
 211
 212static int cx23885_flatiron_mux(struct cx23885_dev *dev, int input)
 213{
 214        u8 val;
 215        dprintk(1, "%s(input = %d)\n", __func__, input);
 216
 217        if (input == 1)
 218                val = cx23885_flatiron_read(dev, CH_PWR_CTRL1) & ~FLD_CH_SEL;
 219        else if (input == 2)
 220                val = cx23885_flatiron_read(dev, CH_PWR_CTRL1) | FLD_CH_SEL;
 221        else
 222                return -EINVAL;
 223
 224        val |= 0x20; /* Enable clock to delta-sigma and dec filter */
 225
 226        cx23885_flatiron_write(dev, CH_PWR_CTRL1, val);
 227
 228        /* Wake up */
 229        cx23885_flatiron_write(dev, CH_PWR_CTRL2, 0);
 230
 231        if (video_debug)
 232                cx23885_flatiron_dump(dev);
 233
 234        return 0;
 235}
 236
 237static int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input)
 238{
 239        dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
 240                __func__,
 241                input, INPUT(input)->vmux,
 242                INPUT(input)->gpio0, INPUT(input)->gpio1,
 243                INPUT(input)->gpio2, INPUT(input)->gpio3);
 244        dev->input = input;
 245
 246        if (dev->board == CX23885_BOARD_MYGICA_X8506 ||
 247                dev->board == CX23885_BOARD_MAGICPRO_PROHDTVE2 ||
 248                dev->board == CX23885_BOARD_MYGICA_X8507) {
 249                /* Select Analog TV */
 250                if (INPUT(input)->type == CX23885_VMUX_TELEVISION)
 251                        cx23885_gpio_clear(dev, GPIO_0);
 252        }
 253
 254        /* Tell the internal A/V decoder */
 255        v4l2_subdev_call(dev->sd_cx25840, video, s_routing,
 256                        INPUT(input)->vmux, 0, 0);
 257
 258        if ((dev->board == CX23885_BOARD_HAUPPAUGE_HVR1800) ||
 259                (dev->board == CX23885_BOARD_MPX885) ||
 260                (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1250) ||
 261                (dev->board == CX23885_BOARD_HAUPPAUGE_IMPACTVCBE) ||
 262                (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255) ||
 263                (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255_22111) ||
 264                (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1850) ||
 265                (dev->board == CX23885_BOARD_MYGICA_X8507) ||
 266                (dev->board == CX23885_BOARD_AVERMEDIA_HC81R) ||
 267                (dev->board == CX23885_BOARD_VIEWCAST_260E) ||
 268                (dev->board == CX23885_BOARD_VIEWCAST_460E)) {
 269                /* Configure audio routing */
 270                v4l2_subdev_call(dev->sd_cx25840, audio, s_routing,
 271                        INPUT(input)->amux, 0, 0);
 272
 273                if (INPUT(input)->amux == CX25840_AUDIO7)
 274                        cx23885_flatiron_mux(dev, 1);
 275                else if (INPUT(input)->amux == CX25840_AUDIO6)
 276                        cx23885_flatiron_mux(dev, 2);
 277        }
 278
 279        return 0;
 280}
 281
 282static int cx23885_audio_mux(struct cx23885_dev *dev, unsigned int input)
 283{
 284        dprintk(1, "%s(input=%d)\n", __func__, input);
 285
 286        /* The baseband video core of the cx23885 has two audio inputs.
 287         * LR1 and LR2. In almost every single case so far only HVR1xxx
 288         * cards we've only ever supported LR1. Time to support LR2,
 289         * which is available via the optional white breakout header on
 290         * the board.
 291         * We'll use a could of existing enums in the card struct to allow
 292         * devs to specify which baseband input they need, or just default
 293         * to what we've always used.
 294         */
 295        if (INPUT(input)->amux == CX25840_AUDIO7)
 296                cx23885_flatiron_mux(dev, 1);
 297        else if (INPUT(input)->amux == CX25840_AUDIO6)
 298                cx23885_flatiron_mux(dev, 2);
 299        else {
 300                /* Not specifically defined, assume the default. */
 301                cx23885_flatiron_mux(dev, 1);
 302        }
 303
 304        return 0;
 305}
 306
 307/* ------------------------------------------------------------------ */
 308static int cx23885_start_video_dma(struct cx23885_dev *dev,
 309                           struct cx23885_dmaqueue *q,
 310                           struct cx23885_buffer *buf)
 311{
 312        dprintk(1, "%s()\n", __func__);
 313
 314        /* Stop the dma/fifo before we tamper with it's risc programs */
 315        cx_clear(VID_A_DMA_CTL, 0x11);
 316
 317        /* setup fifo + format */
 318        cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
 319                                buf->bpl, buf->risc.dma);
 320
 321        /* reset counter */
 322        cx_write(VID_A_GPCNT_CTL, 3);
 323        q->count = 0;
 324
 325        /* enable irq */
 326        cx23885_irq_add_enable(dev, 0x01);
 327        cx_set(VID_A_INT_MSK, 0x000011);
 328
 329        /* start dma */
 330        cx_set(DEV_CNTRL2, (1<<5));
 331        cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
 332
 333        return 0;
 334}
 335
 336static int queue_setup(struct vb2_queue *q,
 337                           unsigned int *num_buffers, unsigned int *num_planes,
 338                           unsigned int sizes[], struct device *alloc_devs[])
 339{
 340        struct cx23885_dev *dev = q->drv_priv;
 341
 342        *num_planes = 1;
 343        sizes[0] = (dev->fmt->depth * dev->width * dev->height) >> 3;
 344        return 0;
 345}
 346
 347static int buffer_prepare(struct vb2_buffer *vb)
 348{
 349        struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
 350        struct cx23885_dev *dev = vb->vb2_queue->drv_priv;
 351        struct cx23885_buffer *buf =
 352                container_of(vbuf, struct cx23885_buffer, vb);
 353        u32 line0_offset, line1_offset;
 354        struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
 355        int field_tff;
 356
 357        buf->bpl = (dev->width * dev->fmt->depth) >> 3;
 358
 359        if (vb2_plane_size(vb, 0) < dev->height * buf->bpl)
 360                return -EINVAL;
 361        vb2_set_plane_payload(vb, 0, dev->height * buf->bpl);
 362
 363        switch (dev->field) {
 364        case V4L2_FIELD_TOP:
 365                cx23885_risc_buffer(dev->pci, &buf->risc,
 366                                sgt->sgl, 0, UNSET,
 367                                buf->bpl, 0, dev->height);
 368                break;
 369        case V4L2_FIELD_BOTTOM:
 370                cx23885_risc_buffer(dev->pci, &buf->risc,
 371                                sgt->sgl, UNSET, 0,
 372                                buf->bpl, 0, dev->height);
 373                break;
 374        case V4L2_FIELD_INTERLACED:
 375                if (dev->tvnorm & V4L2_STD_525_60)
 376                        /* NTSC or  */
 377                        field_tff = 1;
 378                else
 379                        field_tff = 0;
 380
 381                if (cx23885_boards[dev->board].force_bff)
 382                        /* PAL / SECAM OR 888 in NTSC MODE */
 383                        field_tff = 0;
 384
 385                if (field_tff) {
 386                        /* cx25840 transmits NTSC bottom field first */
 387                        dprintk(1, "%s() Creating TFF/NTSC risc\n",
 388                                        __func__);
 389                        line0_offset = buf->bpl;
 390                        line1_offset = 0;
 391                } else {
 392                        /* All other formats are top field first */
 393                        dprintk(1, "%s() Creating BFF/PAL/SECAM risc\n",
 394                                        __func__);
 395                        line0_offset = 0;
 396                        line1_offset = buf->bpl;
 397                }
 398                cx23885_risc_buffer(dev->pci, &buf->risc,
 399                                sgt->sgl, line0_offset,
 400                                line1_offset,
 401                                buf->bpl, buf->bpl,
 402                                dev->height >> 1);
 403                break;
 404        case V4L2_FIELD_SEQ_TB:
 405                cx23885_risc_buffer(dev->pci, &buf->risc,
 406                                sgt->sgl,
 407                                0, buf->bpl * (dev->height >> 1),
 408                                buf->bpl, 0,
 409                                dev->height >> 1);
 410                break;
 411        case V4L2_FIELD_SEQ_BT:
 412                cx23885_risc_buffer(dev->pci, &buf->risc,
 413                                sgt->sgl,
 414                                buf->bpl * (dev->height >> 1), 0,
 415                                buf->bpl, 0,
 416                                dev->height >> 1);
 417                break;
 418        default:
 419                BUG();
 420        }
 421        dprintk(2, "[%p/%d] buffer_init - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
 422                buf, buf->vb.vb2_buf.index,
 423                dev->width, dev->height, dev->fmt->depth, dev->fmt->name,
 424                (unsigned long)buf->risc.dma);
 425        return 0;
 426}
 427
 428static void buffer_finish(struct vb2_buffer *vb)
 429{
 430        struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
 431        struct cx23885_buffer *buf = container_of(vbuf,
 432                struct cx23885_buffer, vb);
 433
 434        cx23885_free_buffer(vb->vb2_queue->drv_priv, buf);
 435}
 436
 437/*
 438 * The risc program for each buffer works as follows: it starts with a simple
 439 * 'JUMP to addr + 12', which is effectively a NOP. Then the code to DMA the
 440 * buffer follows and at the end we have a JUMP back to the start + 12 (skipping
 441 * the initial JUMP).
 442 *
 443 * This is the risc program of the first buffer to be queued if the active list
 444 * is empty and it just keeps DMAing this buffer without generating any
 445 * interrupts.
 446 *
 447 * If a new buffer is added then the initial JUMP in the code for that buffer
 448 * will generate an interrupt which signals that the previous buffer has been
 449 * DMAed successfully and that it can be returned to userspace.
 450 *
 451 * It also sets the final jump of the previous buffer to the start of the new
 452 * buffer, thus chaining the new buffer into the DMA chain. This is a single
 453 * atomic u32 write, so there is no race condition.
 454 *
 455 * The end-result of all this that you only get an interrupt when a buffer
 456 * is ready, so the control flow is very easy.
 457 */
 458static void buffer_queue(struct vb2_buffer *vb)
 459{
 460        struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
 461        struct cx23885_dev *dev = vb->vb2_queue->drv_priv;
 462        struct cx23885_buffer   *buf = container_of(vbuf,
 463                struct cx23885_buffer, vb);
 464        struct cx23885_buffer   *prev;
 465        struct cx23885_dmaqueue *q    = &dev->vidq;
 466        unsigned long flags;
 467
 468        /* add jump to start */
 469        buf->risc.cpu[1] = cpu_to_le32(buf->risc.dma + 12);
 470        buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_CNT_INC);
 471        buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma + 12);
 472        buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
 473
 474        spin_lock_irqsave(&dev->slock, flags);
 475        if (list_empty(&q->active)) {
 476                list_add_tail(&buf->queue, &q->active);
 477                dprintk(2, "[%p/%d] buffer_queue - first active\n",
 478                        buf, buf->vb.vb2_buf.index);
 479        } else {
 480                buf->risc.cpu[0] |= cpu_to_le32(RISC_IRQ1);
 481                prev = list_entry(q->active.prev, struct cx23885_buffer,
 482                        queue);
 483                list_add_tail(&buf->queue, &q->active);
 484                prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
 485                dprintk(2, "[%p/%d] buffer_queue - append to active\n",
 486                                buf, buf->vb.vb2_buf.index);
 487        }
 488        spin_unlock_irqrestore(&dev->slock, flags);
 489}
 490
 491static int cx23885_start_streaming(struct vb2_queue *q, unsigned int count)
 492{
 493        struct cx23885_dev *dev = q->drv_priv;
 494        struct cx23885_dmaqueue *dmaq = &dev->vidq;
 495        struct cx23885_buffer *buf = list_entry(dmaq->active.next,
 496                        struct cx23885_buffer, queue);
 497
 498        cx23885_start_video_dma(dev, dmaq, buf);
 499        return 0;
 500}
 501
 502static void cx23885_stop_streaming(struct vb2_queue *q)
 503{
 504        struct cx23885_dev *dev = q->drv_priv;
 505        struct cx23885_dmaqueue *dmaq = &dev->vidq;
 506        unsigned long flags;
 507
 508        cx_clear(VID_A_DMA_CTL, 0x11);
 509        spin_lock_irqsave(&dev->slock, flags);
 510        while (!list_empty(&dmaq->active)) {
 511                struct cx23885_buffer *buf = list_entry(dmaq->active.next,
 512                        struct cx23885_buffer, queue);
 513
 514                list_del(&buf->queue);
 515                vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
 516        }
 517        spin_unlock_irqrestore(&dev->slock, flags);
 518}
 519
 520static const struct vb2_ops cx23885_video_qops = {
 521        .queue_setup    = queue_setup,
 522        .buf_prepare  = buffer_prepare,
 523        .buf_finish = buffer_finish,
 524        .buf_queue    = buffer_queue,
 525        .wait_prepare = vb2_ops_wait_prepare,
 526        .wait_finish = vb2_ops_wait_finish,
 527        .start_streaming = cx23885_start_streaming,
 528        .stop_streaming = cx23885_stop_streaming,
 529};
 530
 531/* ------------------------------------------------------------------ */
 532/* VIDEO IOCTLS                                                       */
 533
 534static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
 535        struct v4l2_format *f)
 536{
 537        struct cx23885_dev *dev = video_drvdata(file);
 538
 539        f->fmt.pix.width        = dev->width;
 540        f->fmt.pix.height       = dev->height;
 541        f->fmt.pix.field        = dev->field;
 542        f->fmt.pix.pixelformat  = dev->fmt->fourcc;
 543        f->fmt.pix.bytesperline =
 544                (f->fmt.pix.width * dev->fmt->depth) >> 3;
 545        f->fmt.pix.sizeimage =
 546                f->fmt.pix.height * f->fmt.pix.bytesperline;
 547        f->fmt.pix.colorspace   = V4L2_COLORSPACE_SMPTE170M;
 548
 549        return 0;
 550}
 551
 552static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
 553        struct v4l2_format *f)
 554{
 555        struct cx23885_dev *dev = video_drvdata(file);
 556        struct cx23885_fmt *fmt;
 557        enum v4l2_field   field;
 558        unsigned int      maxw, maxh;
 559
 560        fmt = format_by_fourcc(f->fmt.pix.pixelformat);
 561        if (NULL == fmt)
 562                return -EINVAL;
 563
 564        field = f->fmt.pix.field;
 565        maxw  = 720;
 566        maxh  = norm_maxh(dev->tvnorm);
 567
 568        if (V4L2_FIELD_ANY == field) {
 569                field = (f->fmt.pix.height > maxh/2)
 570                        ? V4L2_FIELD_INTERLACED
 571                        : V4L2_FIELD_BOTTOM;
 572        }
 573
 574        switch (field) {
 575        case V4L2_FIELD_TOP:
 576        case V4L2_FIELD_BOTTOM:
 577                maxh = maxh / 2;
 578                break;
 579        case V4L2_FIELD_INTERLACED:
 580        case V4L2_FIELD_SEQ_TB:
 581        case V4L2_FIELD_SEQ_BT:
 582                break;
 583        default:
 584                field = V4L2_FIELD_INTERLACED;
 585                break;
 586        }
 587
 588        f->fmt.pix.field = field;
 589        v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
 590                              &f->fmt.pix.height, 32, maxh, 0, 0);
 591        f->fmt.pix.bytesperline =
 592                (f->fmt.pix.width * fmt->depth) >> 3;
 593        f->fmt.pix.sizeimage =
 594                f->fmt.pix.height * f->fmt.pix.bytesperline;
 595        f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
 596
 597        return 0;
 598}
 599
 600static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
 601        struct v4l2_format *f)
 602{
 603        struct cx23885_dev *dev = video_drvdata(file);
 604        struct v4l2_subdev_format format = {
 605                .which = V4L2_SUBDEV_FORMAT_ACTIVE,
 606        };
 607        int err;
 608
 609        dprintk(2, "%s()\n", __func__);
 610        err = vidioc_try_fmt_vid_cap(file, priv, f);
 611
 612        if (0 != err)
 613                return err;
 614
 615        if (vb2_is_busy(&dev->vb2_vidq) || vb2_is_busy(&dev->vb2_vbiq) ||
 616            vb2_is_busy(&dev->vb2_mpegq))
 617                return -EBUSY;
 618
 619        dev->fmt        = format_by_fourcc(f->fmt.pix.pixelformat);
 620        dev->width      = f->fmt.pix.width;
 621        dev->height     = f->fmt.pix.height;
 622        dev->field      = f->fmt.pix.field;
 623        dprintk(2, "%s() width=%d height=%d field=%d\n", __func__,
 624                dev->width, dev->height, dev->field);
 625        v4l2_fill_mbus_format(&format.format, &f->fmt.pix, MEDIA_BUS_FMT_FIXED);
 626        call_all(dev, pad, set_fmt, NULL, &format);
 627        v4l2_fill_pix_format(&f->fmt.pix, &format.format);
 628        /* set_fmt overwrites f->fmt.pix.field, restore it */
 629        f->fmt.pix.field = dev->field;
 630        return 0;
 631}
 632
 633static int vidioc_querycap(struct file *file, void  *priv,
 634        struct v4l2_capability *cap)
 635{
 636        struct cx23885_dev *dev = video_drvdata(file);
 637        struct video_device *vdev = video_devdata(file);
 638
 639        strcpy(cap->driver, "cx23885");
 640        strlcpy(cap->card, cx23885_boards[dev->board].name,
 641                sizeof(cap->card));
 642        sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
 643        cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING | V4L2_CAP_AUDIO;
 644        if (dev->tuner_type != TUNER_ABSENT)
 645                cap->device_caps |= V4L2_CAP_TUNER;
 646        if (vdev->vfl_type == VFL_TYPE_VBI)
 647                cap->device_caps |= V4L2_CAP_VBI_CAPTURE;
 648        else
 649                cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
 650        cap->capabilities = cap->device_caps | V4L2_CAP_VBI_CAPTURE |
 651                V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_DEVICE_CAPS;
 652        return 0;
 653}
 654
 655static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
 656        struct v4l2_fmtdesc *f)
 657{
 658        if (unlikely(f->index >= ARRAY_SIZE(formats)))
 659                return -EINVAL;
 660
 661        strlcpy(f->description, formats[f->index].name,
 662                sizeof(f->description));
 663        f->pixelformat = formats[f->index].fourcc;
 664
 665        return 0;
 666}
 667
 668static int vidioc_cropcap(struct file *file, void *priv,
 669                          struct v4l2_cropcap *cc)
 670{
 671        struct cx23885_dev *dev = video_drvdata(file);
 672        bool is_50hz = dev->tvnorm & V4L2_STD_625_50;
 673
 674        if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
 675                return -EINVAL;
 676
 677        cc->bounds.left = 0;
 678        cc->bounds.top = 0;
 679        cc->bounds.width = 720;
 680        cc->bounds.height = norm_maxh(dev->tvnorm);
 681        cc->defrect = cc->bounds;
 682        cc->pixelaspect.numerator = is_50hz ? 54 : 11;
 683        cc->pixelaspect.denominator = is_50hz ? 59 : 10;
 684
 685        return 0;
 686}
 687
 688static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
 689{
 690        struct cx23885_dev *dev = video_drvdata(file);
 691        dprintk(1, "%s()\n", __func__);
 692
 693        *id = dev->tvnorm;
 694        return 0;
 695}
 696
 697static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms)
 698{
 699        struct cx23885_dev *dev = video_drvdata(file);
 700        dprintk(1, "%s()\n", __func__);
 701
 702        return cx23885_set_tvnorm(dev, tvnorms);
 703}
 704
 705int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
 706{
 707        static const char *iname[] = {
 708                [CX23885_VMUX_COMPOSITE1] = "Composite1",
 709                [CX23885_VMUX_COMPOSITE2] = "Composite2",
 710                [CX23885_VMUX_COMPOSITE3] = "Composite3",
 711                [CX23885_VMUX_COMPOSITE4] = "Composite4",
 712                [CX23885_VMUX_SVIDEO]     = "S-Video",
 713                [CX23885_VMUX_COMPONENT]  = "Component",
 714                [CX23885_VMUX_TELEVISION] = "Television",
 715                [CX23885_VMUX_CABLE]      = "Cable TV",
 716                [CX23885_VMUX_DVB]        = "DVB",
 717                [CX23885_VMUX_DEBUG]      = "for debug only",
 718        };
 719        unsigned int n;
 720        dprintk(1, "%s()\n", __func__);
 721
 722        n = i->index;
 723        if (n >= MAX_CX23885_INPUT)
 724                return -EINVAL;
 725
 726        if (0 == INPUT(n)->type)
 727                return -EINVAL;
 728
 729        i->index = n;
 730        i->type  = V4L2_INPUT_TYPE_CAMERA;
 731        strcpy(i->name, iname[INPUT(n)->type]);
 732        i->std = CX23885_NORMS;
 733        if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
 734                (CX23885_VMUX_CABLE == INPUT(n)->type)) {
 735                i->type = V4L2_INPUT_TYPE_TUNER;
 736                i->audioset = 4;
 737        } else {
 738                /* Two selectable audio inputs for non-tv inputs */
 739                i->audioset = 3;
 740        }
 741
 742        if (dev->input == n) {
 743                /* enum'd input matches our configured input.
 744                 * Ask the video decoder to process the call
 745                 * and give it an oppertunity to update the
 746                 * status field.
 747                 */
 748                call_all(dev, video, g_input_status, &i->status);
 749        }
 750
 751        return 0;
 752}
 753
 754static int vidioc_enum_input(struct file *file, void *priv,
 755                                struct v4l2_input *i)
 756{
 757        struct cx23885_dev *dev = video_drvdata(file);
 758        dprintk(1, "%s()\n", __func__);
 759        return cx23885_enum_input(dev, i);
 760}
 761
 762int cx23885_get_input(struct file *file, void *priv, unsigned int *i)
 763{
 764        struct cx23885_dev *dev = video_drvdata(file);
 765
 766        *i = dev->input;
 767        dprintk(1, "%s() returns %d\n", __func__, *i);
 768        return 0;
 769}
 770
 771static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
 772{
 773        return cx23885_get_input(file, priv, i);
 774}
 775
 776int cx23885_set_input(struct file *file, void *priv, unsigned int i)
 777{
 778        struct cx23885_dev *dev = video_drvdata(file);
 779
 780        dprintk(1, "%s(%d)\n", __func__, i);
 781
 782        if (i >= MAX_CX23885_INPUT) {
 783                dprintk(1, "%s() -EINVAL\n", __func__);
 784                return -EINVAL;
 785        }
 786
 787        if (INPUT(i)->type == 0)
 788                return -EINVAL;
 789
 790        cx23885_video_mux(dev, i);
 791
 792        /* By default establish the default audio input for the card also */
 793        /* Caller is free to use VIDIOC_S_AUDIO to override afterwards */
 794        cx23885_audio_mux(dev, i);
 795        return 0;
 796}
 797
 798static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
 799{
 800        return cx23885_set_input(file, priv, i);
 801}
 802
 803static int vidioc_log_status(struct file *file, void *priv)
 804{
 805        struct cx23885_dev *dev = video_drvdata(file);
 806
 807        call_all(dev, core, log_status);
 808        return 0;
 809}
 810
 811static int cx23885_query_audinput(struct file *file, void *priv,
 812        struct v4l2_audio *i)
 813{
 814        struct cx23885_dev *dev = video_drvdata(file);
 815        static const char *iname[] = {
 816                [0] = "Baseband L/R 1",
 817                [1] = "Baseband L/R 2",
 818                [2] = "TV",
 819        };
 820        unsigned int n;
 821        dprintk(1, "%s()\n", __func__);
 822
 823        n = i->index;
 824        if (n >= 3)
 825                return -EINVAL;
 826
 827        memset(i, 0, sizeof(*i));
 828        i->index = n;
 829        strcpy(i->name, iname[n]);
 830        i->capability = V4L2_AUDCAP_STEREO;
 831        return 0;
 832
 833}
 834
 835static int vidioc_enum_audinput(struct file *file, void *priv,
 836                                struct v4l2_audio *i)
 837{
 838        return cx23885_query_audinput(file, priv, i);
 839}
 840
 841static int vidioc_g_audinput(struct file *file, void *priv,
 842        struct v4l2_audio *i)
 843{
 844        struct cx23885_dev *dev = video_drvdata(file);
 845
 846        if ((CX23885_VMUX_TELEVISION == INPUT(dev->input)->type) ||
 847                (CX23885_VMUX_CABLE == INPUT(dev->input)->type))
 848                i->index = 2;
 849        else
 850                i->index = dev->audinput;
 851        dprintk(1, "%s(input=%d)\n", __func__, i->index);
 852
 853        return cx23885_query_audinput(file, priv, i);
 854}
 855
 856static int vidioc_s_audinput(struct file *file, void *priv,
 857        const struct v4l2_audio *i)
 858{
 859        struct cx23885_dev *dev = video_drvdata(file);
 860
 861        if ((CX23885_VMUX_TELEVISION == INPUT(dev->input)->type) ||
 862                (CX23885_VMUX_CABLE == INPUT(dev->input)->type)) {
 863                return i->index != 2 ? -EINVAL : 0;
 864        }
 865        if (i->index > 1)
 866                return -EINVAL;
 867
 868        dprintk(1, "%s(%d)\n", __func__, i->index);
 869
 870        dev->audinput = i->index;
 871
 872        /* Skip the audio defaults from the cards struct, caller wants
 873         * directly touch the audio mux hardware. */
 874        cx23885_flatiron_mux(dev, dev->audinput + 1);
 875        return 0;
 876}
 877
 878static int vidioc_g_tuner(struct file *file, void *priv,
 879                                struct v4l2_tuner *t)
 880{
 881        struct cx23885_dev *dev = video_drvdata(file);
 882
 883        if (dev->tuner_type == TUNER_ABSENT)
 884                return -EINVAL;
 885        if (0 != t->index)
 886                return -EINVAL;
 887
 888        strcpy(t->name, "Television");
 889
 890        call_all(dev, tuner, g_tuner, t);
 891        return 0;
 892}
 893
 894static int vidioc_s_tuner(struct file *file, void *priv,
 895                                const struct v4l2_tuner *t)
 896{
 897        struct cx23885_dev *dev = video_drvdata(file);
 898
 899        if (dev->tuner_type == TUNER_ABSENT)
 900                return -EINVAL;
 901        if (0 != t->index)
 902                return -EINVAL;
 903        /* Update the A/V core */
 904        call_all(dev, tuner, s_tuner, t);
 905
 906        return 0;
 907}
 908
 909static int vidioc_g_frequency(struct file *file, void *priv,
 910                                struct v4l2_frequency *f)
 911{
 912        struct cx23885_dev *dev = video_drvdata(file);
 913
 914        if (dev->tuner_type == TUNER_ABSENT)
 915                return -EINVAL;
 916
 917        f->type = V4L2_TUNER_ANALOG_TV;
 918        f->frequency = dev->freq;
 919
 920        call_all(dev, tuner, g_frequency, f);
 921
 922        return 0;
 923}
 924
 925static int cx23885_set_freq(struct cx23885_dev *dev, const struct v4l2_frequency *f)
 926{
 927        struct v4l2_ctrl *mute;
 928        int old_mute_val = 1;
 929
 930        if (dev->tuner_type == TUNER_ABSENT)
 931                return -EINVAL;
 932        if (unlikely(f->tuner != 0))
 933                return -EINVAL;
 934
 935        dev->freq = f->frequency;
 936
 937        /* I need to mute audio here */
 938        mute = v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_AUDIO_MUTE);
 939        if (mute) {
 940                old_mute_val = v4l2_ctrl_g_ctrl(mute);
 941                if (!old_mute_val)
 942                        v4l2_ctrl_s_ctrl(mute, 1);
 943        }
 944
 945        call_all(dev, tuner, s_frequency, f);
 946
 947        /* When changing channels it is required to reset TVAUDIO */
 948        msleep(100);
 949
 950        /* I need to unmute audio here */
 951        if (old_mute_val == 0)
 952                v4l2_ctrl_s_ctrl(mute, old_mute_val);
 953
 954        return 0;
 955}
 956
 957static int cx23885_set_freq_via_ops(struct cx23885_dev *dev,
 958        const struct v4l2_frequency *f)
 959{
 960        struct v4l2_ctrl *mute;
 961        int old_mute_val = 1;
 962        struct vb2_dvb_frontend *vfe;
 963        struct dvb_frontend *fe;
 964
 965        struct analog_parameters params = {
 966                .mode      = V4L2_TUNER_ANALOG_TV,
 967                .audmode   = V4L2_TUNER_MODE_STEREO,
 968                .std       = dev->tvnorm,
 969                .frequency = f->frequency
 970        };
 971
 972        dev->freq = f->frequency;
 973
 974        /* I need to mute audio here */
 975        mute = v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_AUDIO_MUTE);
 976        if (mute) {
 977                old_mute_val = v4l2_ctrl_g_ctrl(mute);
 978                if (!old_mute_val)
 979                        v4l2_ctrl_s_ctrl(mute, 1);
 980        }
 981
 982        /* If HVR1850 */
 983        dprintk(1, "%s() frequency=%d tuner=%d std=0x%llx\n", __func__,
 984                params.frequency, f->tuner, params.std);
 985
 986        vfe = vb2_dvb_get_frontend(&dev->ts2.frontends, 1);
 987        if (!vfe) {
 988                return -EINVAL;
 989        }
 990
 991        fe = vfe->dvb.frontend;
 992
 993        if ((dev->board == CX23885_BOARD_HAUPPAUGE_HVR1850) ||
 994            (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255) ||
 995            (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255_22111))
 996                fe = &dev->ts1.analog_fe;
 997
 998        if (fe && fe->ops.tuner_ops.set_analog_params) {
 999                call_all(dev, video, s_std, dev->tvnorm);
1000                fe->ops.tuner_ops.set_analog_params(fe, &params);
1001        }
1002        else
1003                printk(KERN_ERR "%s() No analog tuner, aborting\n", __func__);
1004
1005        /* When changing channels it is required to reset TVAUDIO */
1006        msleep(100);
1007
1008        /* I need to unmute audio here */
1009        if (old_mute_val == 0)
1010                v4l2_ctrl_s_ctrl(mute, old_mute_val);
1011
1012        return 0;
1013}
1014
1015int cx23885_set_frequency(struct file *file, void *priv,
1016        const struct v4l2_frequency *f)
1017{
1018        struct cx23885_dev *dev = video_drvdata(file);
1019        int ret;
1020
1021        switch (dev->board) {
1022        case CX23885_BOARD_HAUPPAUGE_HVR1255:
1023        case CX23885_BOARD_HAUPPAUGE_HVR1255_22111:
1024        case CX23885_BOARD_HAUPPAUGE_HVR1850:
1025                ret = cx23885_set_freq_via_ops(dev, f);
1026                break;
1027        default:
1028                ret = cx23885_set_freq(dev, f);
1029        }
1030
1031        return ret;
1032}
1033
1034static int vidioc_s_frequency(struct file *file, void *priv,
1035        const struct v4l2_frequency *f)
1036{
1037        return cx23885_set_frequency(file, priv, f);
1038}
1039
1040/* ----------------------------------------------------------- */
1041
1042int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
1043{
1044        u32 mask, count;
1045        int handled = 0;
1046
1047        mask   = cx_read(VID_A_INT_MSK);
1048        if (0 == (status & mask))
1049                return handled;
1050
1051        cx_write(VID_A_INT_STAT, status);
1052
1053        /* risc op code error, fifo overflow or line sync detection error */
1054        if ((status & VID_BC_MSK_OPC_ERR) ||
1055                (status & VID_BC_MSK_SYNC) ||
1056                (status & VID_BC_MSK_OF)) {
1057
1058                if (status & VID_BC_MSK_OPC_ERR) {
1059                        dprintk(7, " (VID_BC_MSK_OPC_ERR 0x%08x)\n",
1060                                VID_BC_MSK_OPC_ERR);
1061                        printk(KERN_WARNING "%s: video risc op code error\n",
1062                                dev->name);
1063                        cx23885_sram_channel_dump(dev,
1064                                &dev->sram_channels[SRAM_CH01]);
1065                }
1066
1067                if (status & VID_BC_MSK_SYNC)
1068                        dprintk(7, " (VID_BC_MSK_SYNC 0x%08x) "
1069                                "video lines miss-match\n",
1070                                VID_BC_MSK_SYNC);
1071
1072                if (status & VID_BC_MSK_OF)
1073                        dprintk(7, " (VID_BC_MSK_OF 0x%08x) fifo overflow\n",
1074                                VID_BC_MSK_OF);
1075
1076        }
1077
1078        /* Video */
1079        if (status & VID_BC_MSK_RISCI1) {
1080                spin_lock(&dev->slock);
1081                count = cx_read(VID_A_GPCNT);
1082                cx23885_video_wakeup(dev, &dev->vidq, count);
1083                spin_unlock(&dev->slock);
1084                handled++;
1085        }
1086
1087        /* Allow the VBI framework to process it's payload */
1088        handled += cx23885_vbi_irq(dev, status);
1089
1090        return handled;
1091}
1092
1093/* ----------------------------------------------------------- */
1094/* exported stuff                                              */
1095
1096static const struct v4l2_file_operations video_fops = {
1097        .owner         = THIS_MODULE,
1098        .open           = v4l2_fh_open,
1099        .release        = vb2_fop_release,
1100        .read           = vb2_fop_read,
1101        .poll           = vb2_fop_poll,
1102        .unlocked_ioctl = video_ioctl2,
1103        .mmap           = vb2_fop_mmap,
1104};
1105
1106static const struct v4l2_ioctl_ops video_ioctl_ops = {
1107        .vidioc_querycap      = vidioc_querycap,
1108        .vidioc_enum_fmt_vid_cap  = vidioc_enum_fmt_vid_cap,
1109        .vidioc_g_fmt_vid_cap     = vidioc_g_fmt_vid_cap,
1110        .vidioc_try_fmt_vid_cap   = vidioc_try_fmt_vid_cap,
1111        .vidioc_s_fmt_vid_cap     = vidioc_s_fmt_vid_cap,
1112        .vidioc_g_fmt_vbi_cap     = cx23885_vbi_fmt,
1113        .vidioc_try_fmt_vbi_cap   = cx23885_vbi_fmt,
1114        .vidioc_s_fmt_vbi_cap     = cx23885_vbi_fmt,
1115        .vidioc_reqbufs       = vb2_ioctl_reqbufs,
1116        .vidioc_prepare_buf   = vb2_ioctl_prepare_buf,
1117        .vidioc_querybuf      = vb2_ioctl_querybuf,
1118        .vidioc_qbuf          = vb2_ioctl_qbuf,
1119        .vidioc_dqbuf         = vb2_ioctl_dqbuf,
1120        .vidioc_streamon      = vb2_ioctl_streamon,
1121        .vidioc_streamoff     = vb2_ioctl_streamoff,
1122        .vidioc_cropcap       = vidioc_cropcap,
1123        .vidioc_s_std         = vidioc_s_std,
1124        .vidioc_g_std         = vidioc_g_std,
1125        .vidioc_enum_input    = vidioc_enum_input,
1126        .vidioc_g_input       = vidioc_g_input,
1127        .vidioc_s_input       = vidioc_s_input,
1128        .vidioc_log_status    = vidioc_log_status,
1129        .vidioc_g_tuner       = vidioc_g_tuner,
1130        .vidioc_s_tuner       = vidioc_s_tuner,
1131        .vidioc_g_frequency   = vidioc_g_frequency,
1132        .vidioc_s_frequency   = vidioc_s_frequency,
1133#ifdef CONFIG_VIDEO_ADV_DEBUG
1134        .vidioc_g_chip_info   = cx23885_g_chip_info,
1135        .vidioc_g_register    = cx23885_g_register,
1136        .vidioc_s_register    = cx23885_s_register,
1137#endif
1138        .vidioc_enumaudio     = vidioc_enum_audinput,
1139        .vidioc_g_audio       = vidioc_g_audinput,
1140        .vidioc_s_audio       = vidioc_s_audinput,
1141        .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1142        .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1143};
1144
1145static struct video_device cx23885_vbi_template;
1146static struct video_device cx23885_video_template = {
1147        .name                 = "cx23885-video",
1148        .fops                 = &video_fops,
1149        .ioctl_ops            = &video_ioctl_ops,
1150        .tvnorms              = CX23885_NORMS,
1151};
1152
1153void cx23885_video_unregister(struct cx23885_dev *dev)
1154{
1155        dprintk(1, "%s()\n", __func__);
1156        cx23885_irq_remove(dev, 0x01);
1157
1158        if (dev->vbi_dev) {
1159                if (video_is_registered(dev->vbi_dev))
1160                        video_unregister_device(dev->vbi_dev);
1161                else
1162                        video_device_release(dev->vbi_dev);
1163                dev->vbi_dev = NULL;
1164        }
1165        if (dev->video_dev) {
1166                if (video_is_registered(dev->video_dev))
1167                        video_unregister_device(dev->video_dev);
1168                else
1169                        video_device_release(dev->video_dev);
1170                dev->video_dev = NULL;
1171        }
1172
1173        if (dev->audio_dev)
1174                cx23885_audio_unregister(dev);
1175}
1176
1177int cx23885_video_register(struct cx23885_dev *dev)
1178{
1179        struct vb2_queue *q;
1180        int err;
1181
1182        dprintk(1, "%s()\n", __func__);
1183
1184        /* Initialize VBI template */
1185        cx23885_vbi_template = cx23885_video_template;
1186        strcpy(cx23885_vbi_template.name, "cx23885-vbi");
1187
1188        dev->tvnorm = V4L2_STD_NTSC_M;
1189        dev->fmt = format_by_fourcc(V4L2_PIX_FMT_YUYV);
1190        dev->field = V4L2_FIELD_INTERLACED;
1191        dev->width = 720;
1192        dev->height = norm_maxh(dev->tvnorm);
1193
1194        /* init video dma queues */
1195        INIT_LIST_HEAD(&dev->vidq.active);
1196
1197        /* init vbi dma queues */
1198        INIT_LIST_HEAD(&dev->vbiq.active);
1199
1200        cx23885_irq_add_enable(dev, 0x01);
1201
1202        if ((TUNER_ABSENT != dev->tuner_type) &&
1203                        ((dev->tuner_bus == 0) || (dev->tuner_bus == 1))) {
1204                struct v4l2_subdev *sd = NULL;
1205
1206                if (dev->tuner_addr)
1207                        sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1208                                &dev->i2c_bus[dev->tuner_bus].i2c_adap,
1209                                "tuner", dev->tuner_addr, NULL);
1210                else
1211                        sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1212                                &dev->i2c_bus[dev->tuner_bus].i2c_adap,
1213                                "tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_TV));
1214                if (sd) {
1215                        struct tuner_setup tun_setup;
1216
1217                        memset(&tun_setup, 0, sizeof(tun_setup));
1218                        tun_setup.mode_mask = T_ANALOG_TV;
1219                        tun_setup.type = dev->tuner_type;
1220                        tun_setup.addr = v4l2_i2c_subdev_addr(sd);
1221                        tun_setup.tuner_callback = cx23885_tuner_callback;
1222
1223                        v4l2_subdev_call(sd, tuner, s_type_addr, &tun_setup);
1224
1225                        if ((dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXTV1200) ||
1226                            (dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXPVR2200)) {
1227                                struct xc2028_ctrl ctrl = {
1228                                        .fname = XC2028_DEFAULT_FIRMWARE,
1229                                        .max_len = 64
1230                                };
1231                                struct v4l2_priv_tun_config cfg = {
1232                                        .tuner = dev->tuner_type,
1233                                        .priv = &ctrl
1234                                };
1235                                v4l2_subdev_call(sd, tuner, s_config, &cfg);
1236                        }
1237
1238                        if (dev->board == CX23885_BOARD_AVERMEDIA_HC81R) {
1239                                struct xc2028_ctrl ctrl = {
1240                                        .fname = "xc3028L-v36.fw",
1241                                        .max_len = 64
1242                                };
1243                                struct v4l2_priv_tun_config cfg = {
1244                                        .tuner = dev->tuner_type,
1245                                        .priv = &ctrl
1246                                };
1247                                v4l2_subdev_call(sd, tuner, s_config, &cfg);
1248                        }
1249                }
1250        }
1251
1252        /* initial device configuration */
1253        mutex_lock(&dev->lock);
1254        cx23885_set_tvnorm(dev, dev->tvnorm);
1255        cx23885_video_mux(dev, 0);
1256        cx23885_audio_mux(dev, 0);
1257        mutex_unlock(&dev->lock);
1258
1259        q = &dev->vb2_vidq;
1260        q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1261        q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1262        q->gfp_flags = GFP_DMA32;
1263        q->min_buffers_needed = 2;
1264        q->drv_priv = dev;
1265        q->buf_struct_size = sizeof(struct cx23885_buffer);
1266        q->ops = &cx23885_video_qops;
1267        q->mem_ops = &vb2_dma_sg_memops;
1268        q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1269        q->lock = &dev->lock;
1270        q->dev = &dev->pci->dev;
1271
1272        err = vb2_queue_init(q);
1273        if (err < 0)
1274                goto fail_unreg;
1275
1276        q = &dev->vb2_vbiq;
1277        q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1278        q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1279        q->gfp_flags = GFP_DMA32;
1280        q->min_buffers_needed = 2;
1281        q->drv_priv = dev;
1282        q->buf_struct_size = sizeof(struct cx23885_buffer);
1283        q->ops = &cx23885_vbi_qops;
1284        q->mem_ops = &vb2_dma_sg_memops;
1285        q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1286        q->lock = &dev->lock;
1287        q->dev = &dev->pci->dev;
1288
1289        err = vb2_queue_init(q);
1290        if (err < 0)
1291                goto fail_unreg;
1292
1293        /* register Video device */
1294        dev->video_dev = cx23885_vdev_init(dev, dev->pci,
1295                &cx23885_video_template, "video");
1296        dev->video_dev->queue = &dev->vb2_vidq;
1297        err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
1298                                    video_nr[dev->nr]);
1299        if (err < 0) {
1300                printk(KERN_INFO "%s: can't register video device\n",
1301                        dev->name);
1302                goto fail_unreg;
1303        }
1304        printk(KERN_INFO "%s: registered device %s [v4l2]\n",
1305               dev->name, video_device_node_name(dev->video_dev));
1306
1307        /* register VBI device */
1308        dev->vbi_dev = cx23885_vdev_init(dev, dev->pci,
1309                &cx23885_vbi_template, "vbi");
1310        dev->vbi_dev->queue = &dev->vb2_vbiq;
1311        err = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1312                                    vbi_nr[dev->nr]);
1313        if (err < 0) {
1314                printk(KERN_INFO "%s: can't register vbi device\n",
1315                        dev->name);
1316                goto fail_unreg;
1317        }
1318        printk(KERN_INFO "%s: registered device %s\n",
1319               dev->name, video_device_node_name(dev->vbi_dev));
1320
1321        /* Register ALSA audio device */
1322        dev->audio_dev = cx23885_audio_register(dev);
1323
1324        return 0;
1325
1326fail_unreg:
1327        cx23885_video_unregister(dev);
1328        return err;
1329}
1330