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