linux/drivers/media/pci/ttpci/av7110.c
<<
>>
Prefs
   1/*
   2 * driver for the SAA7146 based AV110 cards (like the Fujitsu-Siemens DVB)
   3 * av7110.c: initialization and demux stuff
   4 *
   5 * Copyright (C) 1999-2002 Ralph  Metzler
   6 *                       & Marcus Metzler for convergence integrated media GmbH
   7 *
   8 * originally based on code by:
   9 * Copyright (C) 1998,1999 Christian Theiss <mistert@rz.fh-augsburg.de>
  10 *
  11 * This program is free software; you can redistribute it and/or
  12 * modify it under the terms of the GNU General Public License
  13 * as published by the Free Software Foundation; either version 2
  14 * of the License, or (at your option) any later version.
  15 *
  16 *
  17 * This program is distributed in the hope that it will be useful,
  18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20 * GNU General Public License for more details.
  21 *
  22 * To obtain the license, point your browser to
  23 * http://www.gnu.org/copyleft/gpl.html
  24 *
  25 *
  26 * the project's page is at https://linuxtv.org
  27 */
  28
  29
  30#include <linux/module.h>
  31#include <linux/kmod.h>
  32#include <linux/delay.h>
  33#include <linux/fs.h>
  34#include <linux/timer.h>
  35#include <linux/poll.h>
  36
  37#include <linux/kernel.h>
  38#include <linux/sched.h>
  39#include <linux/types.h>
  40#include <linux/fcntl.h>
  41#include <linux/interrupt.h>
  42#include <linux/string.h>
  43#include <linux/pci.h>
  44#include <linux/vmalloc.h>
  45#include <linux/firmware.h>
  46#include <linux/crc32.h>
  47#include <linux/i2c.h>
  48#include <linux/kthread.h>
  49#include <linux/slab.h>
  50#include <asm/unaligned.h>
  51#include <asm/byteorder.h>
  52
  53
  54#include <linux/dvb/frontend.h>
  55
  56#include <media/dvb_frontend.h>
  57
  58#include "ttpci-eeprom.h"
  59#include "av7110.h"
  60#include "av7110_hw.h"
  61#include "av7110_av.h"
  62#include "av7110_ca.h"
  63#include "av7110_ipack.h"
  64
  65#include "bsbe1.h"
  66#include "lnbp21.h"
  67#include "bsru6.h"
  68
  69#define TS_WIDTH  376
  70#define TS_HEIGHT 512
  71#define TS_BUFLEN (TS_WIDTH*TS_HEIGHT)
  72#define TS_MAX_PACKETS (TS_BUFLEN/TS_SIZE)
  73
  74
  75int av7110_debug;
  76
  77static int vidmode = CVBS_RGB_OUT;
  78static int pids_off;
  79static int adac = DVB_ADAC_TI;
  80static int hw_sections;
  81static int rgb_on;
  82static int volume = 255;
  83static int budgetpatch;
  84static int wss_cfg_4_3 = 0x4008;
  85static int wss_cfg_16_9 = 0x0007;
  86static int tv_standard;
  87static int full_ts;
  88
  89module_param_named(debug, av7110_debug, int, 0644);
  90MODULE_PARM_DESC(debug, "debug level (bitmask, default 0)");
  91module_param(vidmode, int, 0444);
  92MODULE_PARM_DESC(vidmode,"analog video out: 0 off, 1 CVBS+RGB (default), 2 CVBS+YC, 3 YC");
  93module_param(pids_off, int, 0444);
  94MODULE_PARM_DESC(pids_off,"clear video/audio/PCR PID filters when demux is closed");
  95module_param(adac, int, 0444);
  96MODULE_PARM_DESC(adac,"audio DAC type: 0 TI, 1 CRYSTAL, 2 MSP (use if autodetection fails)");
  97module_param(hw_sections, int, 0444);
  98MODULE_PARM_DESC(hw_sections, "0 use software section filter, 1 use hardware");
  99module_param(rgb_on, int, 0444);
 100MODULE_PARM_DESC(rgb_on, "For Siemens DVB-C cards only: Enable RGB control signal on SCART pin 16 to switch SCART video mode from CVBS to RGB");
 101module_param(volume, int, 0444);
 102MODULE_PARM_DESC(volume, "initial volume: default 255 (range 0-255)");
 103module_param(budgetpatch, int, 0444);
 104MODULE_PARM_DESC(budgetpatch, "use budget-patch hardware modification: default 0 (0 no, 1 autodetect, 2 always)");
 105module_param(full_ts, int, 0444);
 106MODULE_PARM_DESC(full_ts, "enable code for full-ts hardware modification: 0 disable (default), 1 enable");
 107module_param(wss_cfg_4_3, int, 0444);
 108MODULE_PARM_DESC(wss_cfg_4_3, "WSS 4:3 - default 0x4008 - bit 15: disable, 14: burst mode, 13..0: wss data");
 109module_param(wss_cfg_16_9, int, 0444);
 110MODULE_PARM_DESC(wss_cfg_16_9, "WSS 16:9 - default 0x0007 - bit 15: disable, 14: burst mode, 13..0: wss data");
 111module_param(tv_standard, int, 0444);
 112MODULE_PARM_DESC(tv_standard, "TV standard: 0 PAL (default), 1 NTSC");
 113
 114DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
 115
 116static void restart_feeds(struct av7110 *av7110);
 117static int budget_start_feed(struct dvb_demux_feed *feed);
 118static int budget_stop_feed(struct dvb_demux_feed *feed);
 119
 120static int av7110_num;
 121
 122#define FE_FUNC_OVERRIDE(fe_func, av7110_copy, av7110_func) \
 123{\
 124        if (fe_func != NULL) { \
 125                av7110_copy = fe_func; \
 126                fe_func = av7110_func; \
 127        } \
 128}
 129
 130
 131static void init_av7110_av(struct av7110 *av7110)
 132{
 133        int ret;
 134        struct saa7146_dev *dev = av7110->dev;
 135
 136        /* set internal volume control to maximum */
 137        av7110->adac_type = DVB_ADAC_TI;
 138        ret = av7110_set_volume(av7110, av7110->mixer.volume_left, av7110->mixer.volume_right);
 139        if (ret < 0)
 140                printk("dvb-ttpci:cannot set internal volume to maximum:%d\n",ret);
 141
 142        ret = av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetMonitorType,
 143                            1, (u16) av7110->display_ar);
 144        if (ret < 0)
 145                printk("dvb-ttpci: unable to set aspect ratio\n");
 146        ret = av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetPanScanType,
 147                            1, av7110->display_panscan);
 148        if (ret < 0)
 149                printk("dvb-ttpci: unable to set pan scan\n");
 150
 151        ret = av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetWSSConfig, 2, 2, wss_cfg_4_3);
 152        if (ret < 0)
 153                printk("dvb-ttpci: unable to configure 4:3 wss\n");
 154        ret = av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetWSSConfig, 2, 3, wss_cfg_16_9);
 155        if (ret < 0)
 156                printk("dvb-ttpci: unable to configure 16:9 wss\n");
 157
 158        ret = av7710_set_video_mode(av7110, vidmode);
 159        if (ret < 0)
 160                printk("dvb-ttpci:cannot set video mode:%d\n",ret);
 161
 162        /* handle different card types */
 163        /* remaining inits according to card and frontend type */
 164        av7110->analog_tuner_flags = 0;
 165        av7110->current_input = 0;
 166        if (dev->pci->subsystem_vendor == 0x13c2 && dev->pci->subsystem_device == 0x000a)
 167                av7110_fw_cmd(av7110, COMTYPE_AUDIODAC, ADSwitch, 1, 0); // SPDIF on
 168        if (i2c_writereg(av7110, 0x20, 0x00, 0x00) == 1) {
 169                printk ("dvb-ttpci: Crystal audio DAC @ card %d detected\n",
 170                        av7110->dvb_adapter.num);
 171                av7110->adac_type = DVB_ADAC_CRYSTAL;
 172                i2c_writereg(av7110, 0x20, 0x01, 0xd2);
 173                i2c_writereg(av7110, 0x20, 0x02, 0x49);
 174                i2c_writereg(av7110, 0x20, 0x03, 0x00);
 175                i2c_writereg(av7110, 0x20, 0x04, 0x00);
 176
 177                /**
 178                 * some special handling for the Siemens DVB-C cards...
 179                 */
 180        } else if (0 == av7110_init_analog_module(av7110)) {
 181                /* done. */
 182        }
 183        else if (dev->pci->subsystem_vendor == 0x110a) {
 184                printk("dvb-ttpci: DVB-C w/o analog module @ card %d detected\n",
 185                        av7110->dvb_adapter.num);
 186                av7110->adac_type = DVB_ADAC_NONE;
 187        }
 188        else {
 189                av7110->adac_type = adac;
 190                printk("dvb-ttpci: adac type set to %d @ card %d\n",
 191                        av7110->adac_type, av7110->dvb_adapter.num);
 192        }
 193
 194        if (av7110->adac_type == DVB_ADAC_NONE || av7110->adac_type == DVB_ADAC_MSP34x0) {
 195                // switch DVB SCART on
 196                ret = av7110_fw_cmd(av7110, COMTYPE_AUDIODAC, MainSwitch, 1, 0);
 197                if (ret < 0)
 198                        printk("dvb-ttpci:cannot switch on SCART(Main):%d\n",ret);
 199                ret = av7110_fw_cmd(av7110, COMTYPE_AUDIODAC, ADSwitch, 1, 1);
 200                if (ret < 0)
 201                        printk("dvb-ttpci:cannot switch on SCART(AD):%d\n",ret);
 202                if (rgb_on &&
 203                    ((av7110->dev->pci->subsystem_vendor == 0x110a) ||
 204                     (av7110->dev->pci->subsystem_vendor == 0x13c2)) &&
 205                     (av7110->dev->pci->subsystem_device == 0x0000)) {
 206                        saa7146_setgpio(dev, 1, SAA7146_GPIO_OUTHI); // RGB on, SCART pin 16
 207                        //saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTLO); // SCARTpin 8
 208                }
 209        }
 210
 211        if (dev->pci->subsystem_vendor == 0x13c2 && dev->pci->subsystem_device == 0x000e)
 212                av7110_fw_cmd(av7110, COMTYPE_AUDIODAC, SpdifSwitch, 1, 0); // SPDIF on
 213
 214        ret = av7110_set_volume(av7110, av7110->mixer.volume_left, av7110->mixer.volume_right);
 215        if (ret < 0)
 216                printk("dvb-ttpci:cannot set volume :%d\n",ret);
 217}
 218
 219static void recover_arm(struct av7110 *av7110)
 220{
 221        dprintk(4, "%p\n",av7110);
 222
 223        av7110_bootarm(av7110);
 224        msleep(100);
 225
 226        init_av7110_av(av7110);
 227
 228        /* card-specific recovery */
 229        if (av7110->recover)
 230                av7110->recover(av7110);
 231
 232        restart_feeds(av7110);
 233
 234#if IS_ENABLED(CONFIG_DVB_AV7110_IR)
 235        av7110_check_ir_config(av7110, true);
 236#endif
 237}
 238
 239static void av7110_arm_sync(struct av7110 *av7110)
 240{
 241        if (av7110->arm_thread)
 242                kthread_stop(av7110->arm_thread);
 243
 244        av7110->arm_thread = NULL;
 245}
 246
 247static int arm_thread(void *data)
 248{
 249        struct av7110 *av7110 = data;
 250        u16 newloops = 0;
 251        int timeout;
 252
 253        dprintk(4, "%p\n",av7110);
 254
 255        for (;;) {
 256                timeout = wait_event_interruptible_timeout(av7110->arm_wait,
 257                        kthread_should_stop(), 5 * HZ);
 258
 259                if (-ERESTARTSYS == timeout || kthread_should_stop()) {
 260                        /* got signal or told to quit*/
 261                        break;
 262                }
 263
 264                if (!av7110->arm_ready)
 265                        continue;
 266
 267#if IS_ENABLED(CONFIG_DVB_AV7110_IR)
 268                av7110_check_ir_config(av7110, false);
 269#endif
 270
 271                if (mutex_lock_interruptible(&av7110->dcomlock))
 272                        break;
 273                newloops = rdebi(av7110, DEBINOSWAP, STATUS_LOOPS, 0, 2);
 274                mutex_unlock(&av7110->dcomlock);
 275
 276                if (newloops == av7110->arm_loops || av7110->arm_errors > 3) {
 277                        printk(KERN_ERR "dvb-ttpci: ARM crashed @ card %d\n",
 278                               av7110->dvb_adapter.num);
 279
 280                        recover_arm(av7110);
 281
 282                        if (mutex_lock_interruptible(&av7110->dcomlock))
 283                                break;
 284                        newloops = rdebi(av7110, DEBINOSWAP, STATUS_LOOPS, 0, 2) - 1;
 285                        mutex_unlock(&av7110->dcomlock);
 286                }
 287                av7110->arm_loops = newloops;
 288                av7110->arm_errors = 0;
 289        }
 290
 291        return 0;
 292}
 293
 294
 295/****************************************************************************
 296 * IRQ handling
 297 ****************************************************************************/
 298
 299static int DvbDmxFilterCallback(u8 *buffer1, size_t buffer1_len,
 300                                u8 *buffer2, size_t buffer2_len,
 301                                struct dvb_demux_filter *dvbdmxfilter,
 302                                struct av7110 *av7110)
 303{
 304        if (!dvbdmxfilter->feed->demux->dmx.frontend)
 305                return 0;
 306        if (dvbdmxfilter->feed->demux->dmx.frontend->source == DMX_MEMORY_FE)
 307                return 0;
 308
 309        switch (dvbdmxfilter->type) {
 310        case DMX_TYPE_SEC:
 311                if ((((buffer1[1] << 8) | buffer1[2]) & 0xfff) + 3 != buffer1_len)
 312                        return 0;
 313                if (dvbdmxfilter->doneq) {
 314                        struct dmx_section_filter *filter = &dvbdmxfilter->filter;
 315                        int i;
 316                        u8 xor, neq = 0;
 317
 318                        for (i = 0; i < DVB_DEMUX_MASK_MAX; i++) {
 319                                xor = filter->filter_value[i] ^ buffer1[i];
 320                                neq |= dvbdmxfilter->maskandnotmode[i] & xor;
 321                        }
 322                        if (!neq)
 323                                return 0;
 324                }
 325                return dvbdmxfilter->feed->cb.sec(buffer1, buffer1_len,
 326                                                  buffer2, buffer2_len,
 327                                                  &dvbdmxfilter->filter, NULL);
 328        case DMX_TYPE_TS:
 329                if (!(dvbdmxfilter->feed->ts_type & TS_PACKET))
 330                        return 0;
 331                if (dvbdmxfilter->feed->ts_type & TS_PAYLOAD_ONLY)
 332                        return dvbdmxfilter->feed->cb.ts(buffer1, buffer1_len,
 333                                                         buffer2, buffer2_len,
 334                                                         &dvbdmxfilter->feed->feed.ts,
 335                                                         NULL);
 336                else
 337                        av7110_p2t_write(buffer1, buffer1_len,
 338                                         dvbdmxfilter->feed->pid,
 339                                         &av7110->p2t_filter[dvbdmxfilter->index]);
 340                return 0;
 341        default:
 342                return 0;
 343        }
 344}
 345
 346
 347//#define DEBUG_TIMING
 348static inline void print_time(char *s)
 349{
 350#ifdef DEBUG_TIMING
 351        struct timespec64 ts;
 352        ktime_get_real_ts64(&ts);
 353        printk("%s: %lld.%09ld\n", s, (s64)ts.tv_sec, ts.tv_nsec);
 354#endif
 355}
 356
 357#define DEBI_READ 0
 358#define DEBI_WRITE 1
 359static inline void start_debi_dma(struct av7110 *av7110, int dir,
 360                                  unsigned long addr, unsigned int len)
 361{
 362        dprintk(8, "%c %08lx %u\n", dir == DEBI_READ ? 'R' : 'W', addr, len);
 363        if (saa7146_wait_for_debi_done(av7110->dev, 0)) {
 364                printk(KERN_ERR "%s: saa7146_wait_for_debi_done timed out\n", __func__);
 365                return;
 366        }
 367
 368        SAA7146_ISR_CLEAR(av7110->dev, MASK_19); /* for good measure */
 369        SAA7146_IER_ENABLE(av7110->dev, MASK_19);
 370        if (len < 5)
 371                len = 5; /* we want a real DEBI DMA */
 372        if (dir == DEBI_WRITE)
 373                iwdebi(av7110, DEBISWAB, addr, 0, (len + 3) & ~3);
 374        else
 375                irdebi(av7110, DEBISWAB, addr, 0, len);
 376}
 377
 378static void debiirq(unsigned long cookie)
 379{
 380        struct av7110 *av7110 = (struct av7110 *)cookie;
 381        int type = av7110->debitype;
 382        int handle = (type >> 8) & 0x1f;
 383        unsigned int xfer = 0;
 384
 385        print_time("debi");
 386        dprintk(4, "type 0x%04x\n", type);
 387
 388        if (type == -1) {
 389                printk("DEBI irq oops @ %ld, psr:0x%08x, ssr:0x%08x\n",
 390                       jiffies, saa7146_read(av7110->dev, PSR),
 391                       saa7146_read(av7110->dev, SSR));
 392                goto debi_done;
 393        }
 394        av7110->debitype = -1;
 395
 396        switch (type & 0xff) {
 397
 398        case DATA_TS_RECORD:
 399                dvb_dmx_swfilter_packets(&av7110->demux,
 400                                         (const u8 *) av7110->debi_virt,
 401                                         av7110->debilen / 188);
 402                xfer = RX_BUFF;
 403                break;
 404
 405        case DATA_PES_RECORD:
 406                if (av7110->demux.recording)
 407                        av7110_record_cb(&av7110->p2t[handle],
 408                                         (u8 *) av7110->debi_virt,
 409                                         av7110->debilen);
 410                xfer = RX_BUFF;
 411                break;
 412
 413        case DATA_IPMPE:
 414        case DATA_FSECTION:
 415        case DATA_PIPING:
 416                if (av7110->handle2filter[handle])
 417                        DvbDmxFilterCallback((u8 *)av7110->debi_virt,
 418                                             av7110->debilen, NULL, 0,
 419                                             av7110->handle2filter[handle],
 420                                             av7110);
 421                xfer = RX_BUFF;
 422                break;
 423
 424        case DATA_CI_GET:
 425        {
 426                u8 *data = av7110->debi_virt;
 427
 428                if ((data[0] < 2) && data[2] == 0xff) {
 429                        int flags = 0;
 430                        if (data[5] > 0)
 431                                flags |= CA_CI_MODULE_PRESENT;
 432                        if (data[5] > 5)
 433                                flags |= CA_CI_MODULE_READY;
 434                        av7110->ci_slot[data[0]].flags = flags;
 435                } else
 436                        ci_get_data(&av7110->ci_rbuffer,
 437                                    av7110->debi_virt,
 438                                    av7110->debilen);
 439                xfer = RX_BUFF;
 440                break;
 441        }
 442
 443        case DATA_COMMON_INTERFACE:
 444                CI_handle(av7110, (u8 *)av7110->debi_virt, av7110->debilen);
 445                xfer = RX_BUFF;
 446                break;
 447
 448        case DATA_DEBUG_MESSAGE:
 449                ((s8*)av7110->debi_virt)[Reserved_SIZE - 1] = 0;
 450                printk("%s\n", (s8 *) av7110->debi_virt);
 451                xfer = RX_BUFF;
 452                break;
 453
 454        case DATA_CI_PUT:
 455                dprintk(4, "debi DATA_CI_PUT\n");
 456                xfer = TX_BUFF;
 457                break;
 458        case DATA_MPEG_PLAY:
 459                dprintk(4, "debi DATA_MPEG_PLAY\n");
 460                xfer = TX_BUFF;
 461                break;
 462        case DATA_BMP_LOAD:
 463                dprintk(4, "debi DATA_BMP_LOAD\n");
 464                xfer = TX_BUFF;
 465                break;
 466        default:
 467                break;
 468        }
 469debi_done:
 470        spin_lock(&av7110->debilock);
 471        if (xfer)
 472                iwdebi(av7110, DEBINOSWAP, xfer, 0, 2);
 473        ARM_ClearMailBox(av7110);
 474        spin_unlock(&av7110->debilock);
 475}
 476
 477/* irq from av7110 firmware writing the mailbox register in the DPRAM */
 478static void gpioirq(unsigned long cookie)
 479{
 480        struct av7110 *av7110 = (struct av7110 *)cookie;
 481        u32 rxbuf, txbuf;
 482        int len;
 483
 484        if (av7110->debitype != -1)
 485                /* we shouldn't get any irq while a debi xfer is running */
 486                printk("dvb-ttpci: GPIO0 irq oops @ %ld, psr:0x%08x, ssr:0x%08x\n",
 487                       jiffies, saa7146_read(av7110->dev, PSR),
 488                       saa7146_read(av7110->dev, SSR));
 489
 490        if (saa7146_wait_for_debi_done(av7110->dev, 0)) {
 491                printk(KERN_ERR "%s: saa7146_wait_for_debi_done timed out\n", __func__);
 492                BUG(); /* maybe we should try resetting the debi? */
 493        }
 494
 495        spin_lock(&av7110->debilock);
 496        ARM_ClearIrq(av7110);
 497
 498        /* see what the av7110 wants */
 499        av7110->debitype = irdebi(av7110, DEBINOSWAP, IRQ_STATE, 0, 2);
 500        av7110->debilen  = irdebi(av7110, DEBINOSWAP, IRQ_STATE_EXT, 0, 2);
 501        rxbuf = irdebi(av7110, DEBINOSWAP, RX_BUFF, 0, 2);
 502        txbuf = irdebi(av7110, DEBINOSWAP, TX_BUFF, 0, 2);
 503        len = (av7110->debilen + 3) & ~3;
 504
 505        print_time("gpio");
 506        dprintk(8, "GPIO0 irq 0x%04x %d\n", av7110->debitype, av7110->debilen);
 507
 508        switch (av7110->debitype & 0xff) {
 509
 510        case DATA_TS_PLAY:
 511        case DATA_PES_PLAY:
 512                break;
 513
 514        case DATA_MPEG_VIDEO_EVENT:
 515        {
 516                u32 h_ar;
 517                struct video_event event;
 518
 519                av7110->video_size.w = irdebi(av7110, DEBINOSWAP, STATUS_MPEG_WIDTH, 0, 2);
 520                h_ar = irdebi(av7110, DEBINOSWAP, STATUS_MPEG_HEIGHT_AR, 0, 2);
 521
 522                iwdebi(av7110, DEBINOSWAP, IRQ_STATE_EXT, 0, 2);
 523                iwdebi(av7110, DEBINOSWAP, RX_BUFF, 0, 2);
 524
 525                av7110->video_size.h = h_ar & 0xfff;
 526
 527                event.type = VIDEO_EVENT_SIZE_CHANGED;
 528                event.u.size.w = av7110->video_size.w;
 529                event.u.size.h = av7110->video_size.h;
 530                switch ((h_ar >> 12) & 0xf)
 531                {
 532                case 3:
 533                        av7110->video_size.aspect_ratio = VIDEO_FORMAT_16_9;
 534                        event.u.size.aspect_ratio = VIDEO_FORMAT_16_9;
 535                        av7110->videostate.video_format = VIDEO_FORMAT_16_9;
 536                        break;
 537                case 4:
 538                        av7110->video_size.aspect_ratio = VIDEO_FORMAT_221_1;
 539                        event.u.size.aspect_ratio = VIDEO_FORMAT_221_1;
 540                        av7110->videostate.video_format = VIDEO_FORMAT_221_1;
 541                        break;
 542                default:
 543                        av7110->video_size.aspect_ratio = VIDEO_FORMAT_4_3;
 544                        event.u.size.aspect_ratio = VIDEO_FORMAT_4_3;
 545                        av7110->videostate.video_format = VIDEO_FORMAT_4_3;
 546                }
 547
 548                dprintk(8, "GPIO0 irq: DATA_MPEG_VIDEO_EVENT: w/h/ar = %u/%u/%u\n",
 549                        av7110->video_size.w, av7110->video_size.h,
 550                        av7110->video_size.aspect_ratio);
 551
 552                dvb_video_add_event(av7110, &event);
 553                break;
 554        }
 555
 556        case DATA_CI_PUT:
 557        {
 558                int avail;
 559                struct dvb_ringbuffer *cibuf = &av7110->ci_wbuffer;
 560
 561                avail = dvb_ringbuffer_avail(cibuf);
 562                if (avail <= 2) {
 563                        iwdebi(av7110, DEBINOSWAP, IRQ_STATE_EXT, 0, 2);
 564                        iwdebi(av7110, DEBINOSWAP, TX_LEN, 0, 2);
 565                        iwdebi(av7110, DEBINOSWAP, TX_BUFF, 0, 2);
 566                        break;
 567                }
 568                len = DVB_RINGBUFFER_PEEK(cibuf, 0) << 8;
 569                len |= DVB_RINGBUFFER_PEEK(cibuf, 1);
 570                if (avail < len + 2) {
 571                        iwdebi(av7110, DEBINOSWAP, IRQ_STATE_EXT, 0, 2);
 572                        iwdebi(av7110, DEBINOSWAP, TX_LEN, 0, 2);
 573                        iwdebi(av7110, DEBINOSWAP, TX_BUFF, 0, 2);
 574                        break;
 575                }
 576                DVB_RINGBUFFER_SKIP(cibuf, 2);
 577
 578                dvb_ringbuffer_read(cibuf, av7110->debi_virt, len);
 579
 580                iwdebi(av7110, DEBINOSWAP, TX_LEN, len, 2);
 581                iwdebi(av7110, DEBINOSWAP, IRQ_STATE_EXT, len, 2);
 582                dprintk(8, "DMA: CI\n");
 583                start_debi_dma(av7110, DEBI_WRITE, DPRAM_BASE + txbuf, len);
 584                spin_unlock(&av7110->debilock);
 585                wake_up(&cibuf->queue);
 586                return;
 587        }
 588
 589        case DATA_MPEG_PLAY:
 590                if (!av7110->playing) {
 591                        iwdebi(av7110, DEBINOSWAP, IRQ_STATE_EXT, 0, 2);
 592                        iwdebi(av7110, DEBINOSWAP, TX_LEN, 0, 2);
 593                        iwdebi(av7110, DEBINOSWAP, TX_BUFF, 0, 2);
 594                        break;
 595                }
 596                len = 0;
 597                if (av7110->debitype & 0x100) {
 598                        spin_lock(&av7110->aout.lock);
 599                        len = av7110_pes_play(av7110->debi_virt, &av7110->aout, 2048);
 600                        spin_unlock(&av7110->aout.lock);
 601                }
 602                if (len <= 0 && (av7110->debitype & 0x200)
 603                    &&av7110->videostate.play_state != VIDEO_FREEZED) {
 604                        spin_lock(&av7110->avout.lock);
 605                        len = av7110_pes_play(av7110->debi_virt, &av7110->avout, 2048);
 606                        spin_unlock(&av7110->avout.lock);
 607                }
 608                if (len <= 0) {
 609                        iwdebi(av7110, DEBINOSWAP, IRQ_STATE_EXT, 0, 2);
 610                        iwdebi(av7110, DEBINOSWAP, TX_LEN, 0, 2);
 611                        iwdebi(av7110, DEBINOSWAP, TX_BUFF, 0, 2);
 612                        break;
 613                }
 614                dprintk(8, "GPIO0 PES_PLAY len=%04x\n", len);
 615                iwdebi(av7110, DEBINOSWAP, TX_LEN, len, 2);
 616                iwdebi(av7110, DEBINOSWAP, IRQ_STATE_EXT, len, 2);
 617                dprintk(8, "DMA: MPEG_PLAY\n");
 618                start_debi_dma(av7110, DEBI_WRITE, DPRAM_BASE + txbuf, len);
 619                spin_unlock(&av7110->debilock);
 620                return;
 621
 622        case DATA_BMP_LOAD:
 623                len = av7110->debilen;
 624                dprintk(8, "gpio DATA_BMP_LOAD len %d\n", len);
 625                if (!len) {
 626                        av7110->bmp_state = BMP_LOADED;
 627                        iwdebi(av7110, DEBINOSWAP, IRQ_STATE_EXT, 0, 2);
 628                        iwdebi(av7110, DEBINOSWAP, TX_LEN, 0, 2);
 629                        iwdebi(av7110, DEBINOSWAP, TX_BUFF, 0, 2);
 630                        wake_up(&av7110->bmpq);
 631                        dprintk(8, "gpio DATA_BMP_LOAD done\n");
 632                        break;
 633                }
 634                if (len > av7110->bmplen)
 635                        len = av7110->bmplen;
 636                if (len > 2 * 1024)
 637                        len = 2 * 1024;
 638                iwdebi(av7110, DEBINOSWAP, TX_LEN, len, 2);
 639                iwdebi(av7110, DEBINOSWAP, IRQ_STATE_EXT, len, 2);
 640                memcpy(av7110->debi_virt, av7110->bmpbuf+av7110->bmpp, len);
 641                av7110->bmpp += len;
 642                av7110->bmplen -= len;
 643                dprintk(8, "gpio DATA_BMP_LOAD DMA len %d\n", len);
 644                start_debi_dma(av7110, DEBI_WRITE, DPRAM_BASE+txbuf, len);
 645                spin_unlock(&av7110->debilock);
 646                return;
 647
 648        case DATA_CI_GET:
 649        case DATA_COMMON_INTERFACE:
 650        case DATA_FSECTION:
 651        case DATA_IPMPE:
 652        case DATA_PIPING:
 653                if (!len || len > 4 * 1024) {
 654                        iwdebi(av7110, DEBINOSWAP, RX_BUFF, 0, 2);
 655                        break;
 656                }
 657                /* fall through */
 658
 659        case DATA_TS_RECORD:
 660        case DATA_PES_RECORD:
 661                dprintk(8, "DMA: TS_REC etc.\n");
 662                start_debi_dma(av7110, DEBI_READ, DPRAM_BASE+rxbuf, len);
 663                spin_unlock(&av7110->debilock);
 664                return;
 665
 666        case DATA_DEBUG_MESSAGE:
 667                if (!len || len > 0xff) {
 668                        iwdebi(av7110, DEBINOSWAP, RX_BUFF, 0, 2);
 669                        break;
 670                }
 671                start_debi_dma(av7110, DEBI_READ, Reserved, len);
 672                spin_unlock(&av7110->debilock);
 673                return;
 674
 675        case DATA_IRCOMMAND:
 676                if (av7110->ir.ir_handler)
 677                        av7110->ir.ir_handler(av7110,
 678                                swahw32(irdebi(av7110, DEBINOSWAP, Reserved, 0, 4)));
 679                iwdebi(av7110, DEBINOSWAP, RX_BUFF, 0, 2);
 680                break;
 681
 682        default:
 683                printk("dvb-ttpci: gpioirq unknown type=%d len=%d\n",
 684                       av7110->debitype, av7110->debilen);
 685                break;
 686        }
 687        av7110->debitype = -1;
 688        ARM_ClearMailBox(av7110);
 689        spin_unlock(&av7110->debilock);
 690}
 691
 692
 693#ifdef CONFIG_DVB_AV7110_OSD
 694static int dvb_osd_ioctl(struct file *file,
 695                         unsigned int cmd, void *parg)
 696{
 697        struct dvb_device *dvbdev = file->private_data;
 698        struct av7110 *av7110 = dvbdev->priv;
 699
 700        dprintk(4, "%p\n", av7110);
 701
 702        if (cmd == OSD_SEND_CMD)
 703                return av7110_osd_cmd(av7110, (osd_cmd_t *) parg);
 704        if (cmd == OSD_GET_CAPABILITY)
 705                return av7110_osd_capability(av7110, (osd_cap_t *) parg);
 706
 707        return -EINVAL;
 708}
 709
 710
 711static const struct file_operations dvb_osd_fops = {
 712        .owner          = THIS_MODULE,
 713        .unlocked_ioctl = dvb_generic_ioctl,
 714        .open           = dvb_generic_open,
 715        .release        = dvb_generic_release,
 716        .llseek         = noop_llseek,
 717};
 718
 719static struct dvb_device dvbdev_osd = {
 720        .priv           = NULL,
 721        .users          = 1,
 722        .writers        = 1,
 723        .fops           = &dvb_osd_fops,
 724        .kernel_ioctl   = dvb_osd_ioctl,
 725};
 726#endif /* CONFIG_DVB_AV7110_OSD */
 727
 728
 729static inline int SetPIDs(struct av7110 *av7110, u16 vpid, u16 apid, u16 ttpid,
 730                          u16 subpid, u16 pcrpid)
 731{
 732        u16 aflags = 0;
 733
 734        dprintk(4, "%p\n", av7110);
 735
 736        if (vpid == 0x1fff || apid == 0x1fff ||
 737            ttpid == 0x1fff || subpid == 0x1fff || pcrpid == 0x1fff) {
 738                vpid = apid = ttpid = subpid = pcrpid = 0;
 739                av7110->pids[DMX_PES_VIDEO] = 0;
 740                av7110->pids[DMX_PES_AUDIO] = 0;
 741                av7110->pids[DMX_PES_TELETEXT] = 0;
 742                av7110->pids[DMX_PES_PCR] = 0;
 743        }
 744
 745        if (av7110->audiostate.bypass_mode)
 746                aflags |= 0x8000;
 747
 748        return av7110_fw_cmd(av7110, COMTYPE_PIDFILTER, MultiPID, 6,
 749                             pcrpid, vpid, apid, ttpid, subpid, aflags);
 750}
 751
 752int ChangePIDs(struct av7110 *av7110, u16 vpid, u16 apid, u16 ttpid,
 753                u16 subpid, u16 pcrpid)
 754{
 755        int ret = 0;
 756        dprintk(4, "%p\n", av7110);
 757
 758        if (mutex_lock_interruptible(&av7110->pid_mutex))
 759                return -ERESTARTSYS;
 760
 761        if (!(vpid & 0x8000))
 762                av7110->pids[DMX_PES_VIDEO] = vpid;
 763        if (!(apid & 0x8000))
 764                av7110->pids[DMX_PES_AUDIO] = apid;
 765        if (!(ttpid & 0x8000))
 766                av7110->pids[DMX_PES_TELETEXT] = ttpid;
 767        if (!(pcrpid & 0x8000))
 768                av7110->pids[DMX_PES_PCR] = pcrpid;
 769
 770        av7110->pids[DMX_PES_SUBTITLE] = 0;
 771
 772        if (av7110->fe_synced) {
 773                pcrpid = av7110->pids[DMX_PES_PCR];
 774                ret = SetPIDs(av7110, vpid, apid, ttpid, subpid, pcrpid);
 775        }
 776
 777        mutex_unlock(&av7110->pid_mutex);
 778        return ret;
 779}
 780
 781
 782/******************************************************************************
 783 * hardware filter functions
 784 ******************************************************************************/
 785
 786static int StartHWFilter(struct dvb_demux_filter *dvbdmxfilter)
 787{
 788        struct dvb_demux_feed *dvbdmxfeed = dvbdmxfilter->feed;
 789        struct av7110 *av7110 = dvbdmxfeed->demux->priv;
 790        u16 buf[20];
 791        int ret, i;
 792        u16 handle;
 793//      u16 mode = 0x0320;
 794        u16 mode = 0xb96a;
 795
 796        dprintk(4, "%p\n", av7110);
 797
 798        if (av7110->full_ts)
 799                return 0;
 800
 801        if (dvbdmxfilter->type == DMX_TYPE_SEC) {
 802                if (hw_sections) {
 803                        buf[4] = (dvbdmxfilter->filter.filter_value[0] << 8) |
 804                                dvbdmxfilter->maskandmode[0];
 805                        for (i = 3; i < 18; i++)
 806                                buf[i + 4 - 2] =
 807                                        (dvbdmxfilter->filter.filter_value[i] << 8) |
 808                                        dvbdmxfilter->maskandmode[i];
 809                        mode = 4;
 810                }
 811        } else if ((dvbdmxfeed->ts_type & TS_PACKET) &&
 812                   !(dvbdmxfeed->ts_type & TS_PAYLOAD_ONLY)) {
 813                av7110_p2t_init(&av7110->p2t_filter[dvbdmxfilter->index], dvbdmxfeed);
 814        }
 815
 816        buf[0] = (COMTYPE_PID_FILTER << 8) + AddPIDFilter;
 817        buf[1] = 16;
 818        buf[2] = dvbdmxfeed->pid;
 819        buf[3] = mode;
 820
 821        ret = av7110_fw_request(av7110, buf, 20, &handle, 1);
 822        if (ret != 0 || handle >= 32) {
 823                printk(KERN_ERR "dvb-ttpci: %s error  buf %04x %04x %04x %04x  ret %d  handle %04x\n",
 824                                __func__, buf[0], buf[1], buf[2], buf[3],
 825                                ret, handle);
 826                dvbdmxfilter->hw_handle = 0xffff;
 827                if (!ret)
 828                        ret = -1;
 829                return ret;
 830        }
 831
 832        av7110->handle2filter[handle] = dvbdmxfilter;
 833        dvbdmxfilter->hw_handle = handle;
 834
 835        return ret;
 836}
 837
 838static int StopHWFilter(struct dvb_demux_filter *dvbdmxfilter)
 839{
 840        struct av7110 *av7110 = dvbdmxfilter->feed->demux->priv;
 841        u16 buf[3];
 842        u16 answ[2];
 843        int ret;
 844        u16 handle;
 845
 846        dprintk(4, "%p\n", av7110);
 847
 848        if (av7110->full_ts)
 849                return 0;
 850
 851        handle = dvbdmxfilter->hw_handle;
 852        if (handle >= 32) {
 853                printk("%s tried to stop invalid filter %04x, filter type = %x\n",
 854                                __func__, handle, dvbdmxfilter->type);
 855                return -EINVAL;
 856        }
 857
 858        av7110->handle2filter[handle] = NULL;
 859
 860        buf[0] = (COMTYPE_PID_FILTER << 8) + DelPIDFilter;
 861        buf[1] = 1;
 862        buf[2] = handle;
 863        ret = av7110_fw_request(av7110, buf, 3, answ, 2);
 864        if (ret != 0 || answ[1] != handle) {
 865                printk(KERN_ERR "dvb-ttpci: %s error  cmd %04x %04x %04x  ret %x  resp %04x %04x  pid %d\n",
 866                                __func__, buf[0], buf[1], buf[2], ret,
 867                                answ[0], answ[1], dvbdmxfilter->feed->pid);
 868                if (!ret)
 869                        ret = -1;
 870        }
 871        return ret;
 872}
 873
 874
 875static int dvb_feed_start_pid(struct dvb_demux_feed *dvbdmxfeed)
 876{
 877        struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
 878        struct av7110 *av7110 = dvbdmx->priv;
 879        u16 *pid = dvbdmx->pids, npids[5];
 880        int i;
 881        int ret = 0;
 882
 883        dprintk(4, "%p\n", av7110);
 884
 885        npids[0] = npids[1] = npids[2] = npids[3] = npids[4] = 0xffff;
 886        i = dvbdmxfeed->pes_type;
 887        npids[i] = (pid[i]&0x8000) ? 0 : pid[i];
 888        if ((i == 2) && npids[i] && (dvbdmxfeed->ts_type & TS_PACKET)) {
 889                npids[i] = 0;
 890                ret = ChangePIDs(av7110, npids[1], npids[0], npids[2], npids[3], npids[4]);
 891                if (!ret)
 892                        ret = StartHWFilter(dvbdmxfeed->filter);
 893                return ret;
 894        }
 895        if (dvbdmxfeed->pes_type <= 2 || dvbdmxfeed->pes_type == 4) {
 896                ret = ChangePIDs(av7110, npids[1], npids[0], npids[2], npids[3], npids[4]);
 897                if (ret)
 898                        return ret;
 899        }
 900
 901        if (dvbdmxfeed->pes_type < 2 && npids[0])
 902                if (av7110->fe_synced)
 903                {
 904                        ret = av7110_fw_cmd(av7110, COMTYPE_PIDFILTER, Scan, 0);
 905                        if (ret)
 906                                return ret;
 907                }
 908
 909        if ((dvbdmxfeed->ts_type & TS_PACKET) && !av7110->full_ts) {
 910                if (dvbdmxfeed->pes_type == 0 && !(dvbdmx->pids[0] & 0x8000))
 911                        ret = av7110_av_start_record(av7110, RP_AUDIO, dvbdmxfeed);
 912                if (dvbdmxfeed->pes_type == 1 && !(dvbdmx->pids[1] & 0x8000))
 913                        ret = av7110_av_start_record(av7110, RP_VIDEO, dvbdmxfeed);
 914        }
 915        return ret;
 916}
 917
 918static int dvb_feed_stop_pid(struct dvb_demux_feed *dvbdmxfeed)
 919{
 920        struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
 921        struct av7110 *av7110 = dvbdmx->priv;
 922        u16 *pid = dvbdmx->pids, npids[5];
 923        int i;
 924
 925        int ret = 0;
 926
 927        dprintk(4, "%p\n", av7110);
 928
 929        if (dvbdmxfeed->pes_type <= 1) {
 930                ret = av7110_av_stop(av7110, dvbdmxfeed->pes_type ?  RP_VIDEO : RP_AUDIO);
 931                if (ret)
 932                        return ret;
 933                if (!av7110->rec_mode)
 934                        dvbdmx->recording = 0;
 935                if (!av7110->playing)
 936                        dvbdmx->playing = 0;
 937        }
 938        npids[0] = npids[1] = npids[2] = npids[3] = npids[4] = 0xffff;
 939        i = dvbdmxfeed->pes_type;
 940        switch (i) {
 941        case 2: //teletext
 942                if (dvbdmxfeed->ts_type & TS_PACKET)
 943                        ret = StopHWFilter(dvbdmxfeed->filter);
 944                npids[2] = 0;
 945                break;
 946        case 0:
 947        case 1:
 948        case 4:
 949                if (!pids_off)
 950                        return 0;
 951                npids[i] = (pid[i]&0x8000) ? 0 : pid[i];
 952                break;
 953        }
 954        if (!ret)
 955                ret = ChangePIDs(av7110, npids[1], npids[0], npids[2], npids[3], npids[4]);
 956        return ret;
 957}
 958
 959static int av7110_start_feed(struct dvb_demux_feed *feed)
 960{
 961        struct dvb_demux *demux = feed->demux;
 962        struct av7110 *av7110 = demux->priv;
 963        int ret = 0;
 964
 965        dprintk(4, "%p\n", av7110);
 966
 967        if (!demux->dmx.frontend)
 968                return -EINVAL;
 969
 970        if (!av7110->full_ts && feed->pid > 0x1fff)
 971                return -EINVAL;
 972
 973        if (feed->type == DMX_TYPE_TS) {
 974                if ((feed->ts_type & TS_DECODER) &&
 975                    (feed->pes_type <= DMX_PES_PCR)) {
 976                        switch (demux->dmx.frontend->source) {
 977                        case DMX_MEMORY_FE:
 978                                if (feed->ts_type & TS_DECODER)
 979                                       if (feed->pes_type < 2 &&
 980                                           !(demux->pids[0] & 0x8000) &&
 981                                           !(demux->pids[1] & 0x8000)) {
 982                                               dvb_ringbuffer_flush_spinlock_wakeup(&av7110->avout);
 983                                               dvb_ringbuffer_flush_spinlock_wakeup(&av7110->aout);
 984                                               ret = av7110_av_start_play(av7110,RP_AV);
 985                                               if (!ret)
 986                                                       demux->playing = 1;
 987                                        }
 988                                break;
 989                        default:
 990                                ret = dvb_feed_start_pid(feed);
 991                                break;
 992                        }
 993                } else if ((feed->ts_type & TS_PACKET) &&
 994                           (demux->dmx.frontend->source != DMX_MEMORY_FE)) {
 995                        ret = StartHWFilter(feed->filter);
 996                }
 997        }
 998
 999        if (av7110->full_ts) {
1000                budget_start_feed(feed);
1001                return ret;
1002        }
1003
1004        if (feed->type == DMX_TYPE_SEC) {
1005                int i;
1006
1007                for (i = 0; i < demux->filternum; i++) {
1008                        if (demux->filter[i].state != DMX_STATE_READY)
1009                                continue;
1010                        if (demux->filter[i].type != DMX_TYPE_SEC)
1011                                continue;
1012                        if (demux->filter[i].filter.parent != &feed->feed.sec)
1013                                continue;
1014                        demux->filter[i].state = DMX_STATE_GO;
1015                        if (demux->dmx.frontend->source != DMX_MEMORY_FE) {
1016                                ret = StartHWFilter(&demux->filter[i]);
1017                                if (ret)
1018                                        break;
1019                        }
1020                }
1021        }
1022
1023        return ret;
1024}
1025
1026
1027static int av7110_stop_feed(struct dvb_demux_feed *feed)
1028{
1029        struct dvb_demux *demux = feed->demux;
1030        struct av7110 *av7110 = demux->priv;
1031        int i, rc, ret = 0;
1032        dprintk(4, "%p\n", av7110);
1033
1034        if (feed->type == DMX_TYPE_TS) {
1035                if (feed->ts_type & TS_DECODER) {
1036                        if (feed->pes_type >= DMX_PES_OTHER ||
1037                            !demux->pesfilter[feed->pes_type])
1038                                return -EINVAL;
1039                        demux->pids[feed->pes_type] |= 0x8000;
1040                        demux->pesfilter[feed->pes_type] = NULL;
1041                }
1042                if (feed->ts_type & TS_DECODER &&
1043                    feed->pes_type < DMX_PES_OTHER) {
1044                        ret = dvb_feed_stop_pid(feed);
1045                } else
1046                        if ((feed->ts_type & TS_PACKET) &&
1047                            (demux->dmx.frontend->source != DMX_MEMORY_FE))
1048                                ret = StopHWFilter(feed->filter);
1049        }
1050
1051        if (av7110->full_ts) {
1052                budget_stop_feed(feed);
1053                return ret;
1054        }
1055
1056        if (feed->type == DMX_TYPE_SEC) {
1057                for (i = 0; i<demux->filternum; i++) {
1058                        if (demux->filter[i].state == DMX_STATE_GO &&
1059                            demux->filter[i].filter.parent == &feed->feed.sec) {
1060                                demux->filter[i].state = DMX_STATE_READY;
1061                                if (demux->dmx.frontend->source != DMX_MEMORY_FE) {
1062                                        rc = StopHWFilter(&demux->filter[i]);
1063                                        if (!ret)
1064                                                ret = rc;
1065                                        /* keep going, stop as many filters as possible */
1066                                }
1067                        }
1068                }
1069        }
1070
1071        return ret;
1072}
1073
1074
1075static void restart_feeds(struct av7110 *av7110)
1076{
1077        struct dvb_demux *dvbdmx = &av7110->demux;
1078        struct dvb_demux_feed *feed;
1079        int mode;
1080        int feeding;
1081        int i, j;
1082
1083        dprintk(4, "%p\n", av7110);
1084
1085        mode = av7110->playing;
1086        av7110->playing = 0;
1087        av7110->rec_mode = 0;
1088
1089        feeding = av7110->feeding1; /* full_ts mod */
1090
1091        for (i = 0; i < dvbdmx->feednum; i++) {
1092                feed = &dvbdmx->feed[i];
1093                if (feed->state == DMX_STATE_GO) {
1094                        if (feed->type == DMX_TYPE_SEC) {
1095                                for (j = 0; j < dvbdmx->filternum; j++) {
1096                                        if (dvbdmx->filter[j].type != DMX_TYPE_SEC)
1097                                                continue;
1098                                        if (dvbdmx->filter[j].filter.parent != &feed->feed.sec)
1099                                                continue;
1100                                        if (dvbdmx->filter[j].state == DMX_STATE_GO)
1101                                                dvbdmx->filter[j].state = DMX_STATE_READY;
1102                                }
1103                        }
1104                        av7110_start_feed(feed);
1105                }
1106        }
1107
1108        av7110->feeding1 = feeding; /* full_ts mod */
1109
1110        if (mode)
1111                av7110_av_start_play(av7110, mode);
1112}
1113
1114static int dvb_get_stc(struct dmx_demux *demux, unsigned int num,
1115                       uint64_t *stc, unsigned int *base)
1116{
1117        int ret;
1118        u16 fwstc[4];
1119        u16 tag = ((COMTYPE_REQUEST << 8) + ReqSTC);
1120        struct dvb_demux *dvbdemux;
1121        struct av7110 *av7110;
1122
1123        /* pointer casting paranoia... */
1124        BUG_ON(!demux);
1125        dvbdemux = demux->priv;
1126        BUG_ON(!dvbdemux);
1127        av7110 = dvbdemux->priv;
1128
1129        dprintk(4, "%p\n", av7110);
1130
1131        if (num != 0)
1132                return -EINVAL;
1133
1134        ret = av7110_fw_request(av7110, &tag, 0, fwstc, 4);
1135        if (ret) {
1136                printk(KERN_ERR "%s: av7110_fw_request error\n", __func__);
1137                return ret;
1138        }
1139        dprintk(2, "fwstc = %04hx %04hx %04hx %04hx\n",
1140                fwstc[0], fwstc[1], fwstc[2], fwstc[3]);
1141
1142        *stc =  (((uint64_t) ((fwstc[3] & 0x8000) >> 15)) << 32) |
1143                (((uint64_t)  fwstc[1]) << 16) | ((uint64_t) fwstc[0]);
1144        *base = 1;
1145
1146        dprintk(4, "stc = %lu\n", (unsigned long)*stc);
1147
1148        return 0;
1149}
1150
1151
1152/******************************************************************************
1153 * SEC device file operations
1154 ******************************************************************************/
1155
1156
1157static int av7110_set_tone(struct dvb_frontend *fe, enum fe_sec_tone_mode tone)
1158{
1159        struct av7110* av7110 = fe->dvb->priv;
1160
1161        switch (tone) {
1162        case SEC_TONE_ON:
1163                return Set22K(av7110, 1);
1164
1165        case SEC_TONE_OFF:
1166                return Set22K(av7110, 0);
1167
1168        default:
1169                return -EINVAL;
1170        }
1171}
1172
1173static int av7110_diseqc_send_master_cmd(struct dvb_frontend* fe,
1174                                         struct dvb_diseqc_master_cmd* cmd)
1175{
1176        struct av7110* av7110 = fe->dvb->priv;
1177
1178        return av7110_diseqc_send(av7110, cmd->msg_len, cmd->msg, -1);
1179}
1180
1181static int av7110_diseqc_send_burst(struct dvb_frontend* fe,
1182                                    enum fe_sec_mini_cmd minicmd)
1183{
1184        struct av7110* av7110 = fe->dvb->priv;
1185
1186        return av7110_diseqc_send(av7110, 0, NULL, minicmd);
1187}
1188
1189/* simplified code from budget-core.c */
1190static int stop_ts_capture(struct av7110 *budget)
1191{
1192        dprintk(2, "budget: %p\n", budget);
1193
1194        if (--budget->feeding1)
1195                return budget->feeding1;
1196        saa7146_write(budget->dev, MC1, MASK_20);       /* DMA3 off */
1197        SAA7146_IER_DISABLE(budget->dev, MASK_10);
1198        SAA7146_ISR_CLEAR(budget->dev, MASK_10);
1199        return 0;
1200}
1201
1202static int start_ts_capture(struct av7110 *budget)
1203{
1204        unsigned y;
1205
1206        dprintk(2, "budget: %p\n", budget);
1207
1208        if (budget->feeding1)
1209                return ++budget->feeding1;
1210        for (y = 0; y < TS_HEIGHT; y++)
1211                memset(budget->grabbing + y * TS_WIDTH, 0x00, TS_WIDTH);
1212        budget->ttbp = 0;
1213        SAA7146_ISR_CLEAR(budget->dev, MASK_10);  /* VPE */
1214        SAA7146_IER_ENABLE(budget->dev, MASK_10); /* VPE */
1215        saa7146_write(budget->dev, MC1, (MASK_04 | MASK_20)); /* DMA3 on */
1216        return ++budget->feeding1;
1217}
1218
1219static int budget_start_feed(struct dvb_demux_feed *feed)
1220{
1221        struct dvb_demux *demux = feed->demux;
1222        struct av7110 *budget = demux->priv;
1223        int status;
1224
1225        dprintk(2, "av7110: %p\n", budget);
1226
1227        spin_lock(&budget->feedlock1);
1228        feed->pusi_seen = false; /* have a clean section start */
1229        status = start_ts_capture(budget);
1230        spin_unlock(&budget->feedlock1);
1231        return status;
1232}
1233
1234static int budget_stop_feed(struct dvb_demux_feed *feed)
1235{
1236        struct dvb_demux *demux = feed->demux;
1237        struct av7110 *budget = demux->priv;
1238        int status;
1239
1240        dprintk(2, "budget: %p\n", budget);
1241
1242        spin_lock(&budget->feedlock1);
1243        status = stop_ts_capture(budget);
1244        spin_unlock(&budget->feedlock1);
1245        return status;
1246}
1247
1248static void vpeirq(unsigned long cookie)
1249{
1250        struct av7110 *budget = (struct av7110 *)cookie;
1251        u8 *mem = (u8 *) (budget->grabbing);
1252        u32 olddma = budget->ttbp;
1253        u32 newdma = saa7146_read(budget->dev, PCI_VDP3);
1254        struct dvb_demux *demux = budget->full_ts ? &budget->demux : &budget->demux1;
1255
1256        /* nearest lower position divisible by 188 */
1257        newdma -= newdma % 188;
1258
1259        if (newdma >= TS_BUFLEN)
1260                return;
1261
1262        budget->ttbp = newdma;
1263
1264        if (!budget->feeding1 || (newdma == olddma))
1265                return;
1266
1267        /* Ensure streamed PCI data is synced to CPU */
1268        pci_dma_sync_sg_for_cpu(budget->dev->pci, budget->pt.slist, budget->pt.nents, PCI_DMA_FROMDEVICE);
1269
1270#if 0
1271        /* track rps1 activity */
1272        printk("vpeirq: %02x Event Counter 1 0x%04x\n",
1273               mem[olddma],
1274               saa7146_read(budget->dev, EC1R) & 0x3fff);
1275#endif
1276
1277        if (newdma > olddma)
1278                /* no wraparound, dump olddma..newdma */
1279                dvb_dmx_swfilter_packets(demux, mem + olddma, (newdma - olddma) / 188);
1280        else {
1281                /* wraparound, dump olddma..buflen and 0..newdma */
1282                dvb_dmx_swfilter_packets(demux, mem + olddma, (TS_BUFLEN - olddma) / 188);
1283                dvb_dmx_swfilter_packets(demux, mem, newdma / 188);
1284        }
1285}
1286
1287static int av7110_register(struct av7110 *av7110)
1288{
1289        int ret, i;
1290        struct dvb_demux *dvbdemux = &av7110->demux;
1291        struct dvb_demux *dvbdemux1 = &av7110->demux1;
1292
1293        dprintk(4, "%p\n", av7110);
1294
1295        if (av7110->registered)
1296                return -1;
1297
1298        av7110->registered = 1;
1299
1300        dvbdemux->priv = (void *) av7110;
1301
1302        for (i = 0; i < 32; i++)
1303                av7110->handle2filter[i] = NULL;
1304
1305        dvbdemux->filternum = (av7110->full_ts) ? 256 : 32;
1306        dvbdemux->feednum = (av7110->full_ts) ? 256 : 32;
1307        dvbdemux->start_feed = av7110_start_feed;
1308        dvbdemux->stop_feed = av7110_stop_feed;
1309        dvbdemux->write_to_decoder = av7110_write_to_decoder;
1310        dvbdemux->dmx.capabilities = (DMX_TS_FILTERING | DMX_SECTION_FILTERING |
1311                                      DMX_MEMORY_BASED_FILTERING);
1312
1313        dvb_dmx_init(&av7110->demux);
1314        av7110->demux.dmx.get_stc = dvb_get_stc;
1315
1316        av7110->dmxdev.filternum = (av7110->full_ts) ? 256 : 32;
1317        av7110->dmxdev.demux = &dvbdemux->dmx;
1318        av7110->dmxdev.capabilities = 0;
1319
1320        dvb_dmxdev_init(&av7110->dmxdev, &av7110->dvb_adapter);
1321
1322        av7110->hw_frontend.source = DMX_FRONTEND_0;
1323
1324        ret = dvbdemux->dmx.add_frontend(&dvbdemux->dmx, &av7110->hw_frontend);
1325
1326        if (ret < 0)
1327                return ret;
1328
1329        av7110->mem_frontend.source = DMX_MEMORY_FE;
1330
1331        ret = dvbdemux->dmx.add_frontend(&dvbdemux->dmx, &av7110->mem_frontend);
1332
1333        if (ret < 0)
1334                return ret;
1335
1336        ret = dvbdemux->dmx.connect_frontend(&dvbdemux->dmx,
1337                                             &av7110->hw_frontend);
1338        if (ret < 0)
1339                return ret;
1340
1341        av7110_av_register(av7110);
1342        av7110_ca_register(av7110);
1343
1344#ifdef CONFIG_DVB_AV7110_OSD
1345        dvb_register_device(&av7110->dvb_adapter, &av7110->osd_dev,
1346                            &dvbdev_osd, av7110, DVB_DEVICE_OSD, 0);
1347#endif
1348
1349        dvb_net_init(&av7110->dvb_adapter, &av7110->dvb_net, &dvbdemux->dmx);
1350
1351        if (budgetpatch) {
1352                /* initialize software demux1 without its own frontend
1353                 * demux1 hardware is connected to frontend0 of demux0
1354                 */
1355                dvbdemux1->priv = (void *) av7110;
1356
1357                dvbdemux1->filternum = 256;
1358                dvbdemux1->feednum = 256;
1359                dvbdemux1->start_feed = budget_start_feed;
1360                dvbdemux1->stop_feed = budget_stop_feed;
1361                dvbdemux1->write_to_decoder = NULL;
1362
1363                dvbdemux1->dmx.capabilities = (DMX_TS_FILTERING | DMX_SECTION_FILTERING |
1364                                               DMX_MEMORY_BASED_FILTERING);
1365
1366                dvb_dmx_init(&av7110->demux1);
1367
1368                av7110->dmxdev1.filternum = 256;
1369                av7110->dmxdev1.demux = &dvbdemux1->dmx;
1370                av7110->dmxdev1.capabilities = 0;
1371
1372                dvb_dmxdev_init(&av7110->dmxdev1, &av7110->dvb_adapter);
1373
1374                dvb_net_init(&av7110->dvb_adapter, &av7110->dvb_net1, &dvbdemux1->dmx);
1375                printk("dvb-ttpci: additional demux1 for budget-patch registered\n");
1376        }
1377        return 0;
1378}
1379
1380
1381static void dvb_unregister(struct av7110 *av7110)
1382{
1383        struct dvb_demux *dvbdemux = &av7110->demux;
1384        struct dvb_demux *dvbdemux1 = &av7110->demux1;
1385
1386        dprintk(4, "%p\n", av7110);
1387
1388        if (!av7110->registered)
1389                return;
1390
1391        if (budgetpatch) {
1392                dvb_net_release(&av7110->dvb_net1);
1393                dvbdemux->dmx.close(&dvbdemux1->dmx);
1394                dvb_dmxdev_release(&av7110->dmxdev1);
1395                dvb_dmx_release(&av7110->demux1);
1396        }
1397
1398        dvb_net_release(&av7110->dvb_net);
1399
1400        dvbdemux->dmx.close(&dvbdemux->dmx);
1401        dvbdemux->dmx.remove_frontend(&dvbdemux->dmx, &av7110->hw_frontend);
1402        dvbdemux->dmx.remove_frontend(&dvbdemux->dmx, &av7110->mem_frontend);
1403
1404        dvb_dmxdev_release(&av7110->dmxdev);
1405        dvb_dmx_release(&av7110->demux);
1406
1407        if (av7110->fe != NULL) {
1408                dvb_unregister_frontend(av7110->fe);
1409                dvb_frontend_detach(av7110->fe);
1410        }
1411        dvb_unregister_device(av7110->osd_dev);
1412        av7110_av_unregister(av7110);
1413        av7110_ca_unregister(av7110);
1414}
1415
1416
1417/****************************************************************************
1418 * I2C client commands
1419 ****************************************************************************/
1420
1421int i2c_writereg(struct av7110 *av7110, u8 id, u8 reg, u8 val)
1422{
1423        u8 msg[2] = { reg, val };
1424        struct i2c_msg msgs;
1425
1426        msgs.flags = 0;
1427        msgs.addr = id / 2;
1428        msgs.len = 2;
1429        msgs.buf = msg;
1430        return i2c_transfer(&av7110->i2c_adap, &msgs, 1);
1431}
1432
1433u8 i2c_readreg(struct av7110 *av7110, u8 id, u8 reg)
1434{
1435        u8 mm1[] = {0x00};
1436        u8 mm2[] = {0x00};
1437        struct i2c_msg msgs[2];
1438
1439        msgs[0].flags = 0;
1440        msgs[1].flags = I2C_M_RD;
1441        msgs[0].addr = msgs[1].addr = id / 2;
1442        mm1[0] = reg;
1443        msgs[0].len = 1; msgs[1].len = 1;
1444        msgs[0].buf = mm1; msgs[1].buf = mm2;
1445        i2c_transfer(&av7110->i2c_adap, msgs, 2);
1446
1447        return mm2[0];
1448}
1449
1450/****************************************************************************
1451 * INITIALIZATION
1452 ****************************************************************************/
1453
1454
1455static int check_firmware(struct av7110* av7110)
1456{
1457        u32 crc = 0, len = 0;
1458        unsigned char *ptr;
1459
1460        /* check for firmware magic */
1461        ptr = av7110->bin_fw;
1462        if (ptr[0] != 'A' || ptr[1] != 'V' ||
1463            ptr[2] != 'F' || ptr[3] != 'W') {
1464                printk("dvb-ttpci: this is not an av7110 firmware\n");
1465                return -EINVAL;
1466        }
1467        ptr += 4;
1468
1469        /* check dpram file */
1470        crc = get_unaligned_be32(ptr);
1471        ptr += 4;
1472        len = get_unaligned_be32(ptr);
1473        ptr += 4;
1474        if (len >= 512) {
1475                printk("dvb-ttpci: dpram file is way too big.\n");
1476                return -EINVAL;
1477        }
1478        if (crc != crc32_le(0, ptr, len)) {
1479                printk("dvb-ttpci: crc32 of dpram file does not match.\n");
1480                return -EINVAL;
1481        }
1482        av7110->bin_dpram = ptr;
1483        av7110->size_dpram = len;
1484        ptr += len;
1485
1486        /* check root file */
1487        crc = get_unaligned_be32(ptr);
1488        ptr += 4;
1489        len = get_unaligned_be32(ptr);
1490        ptr += 4;
1491
1492        if (len <= 200000 || len >= 300000 ||
1493            len > ((av7110->bin_fw + av7110->size_fw) - ptr)) {
1494                printk("dvb-ttpci: root file has strange size (%d). aborting.\n", len);
1495                return -EINVAL;
1496        }
1497        if( crc != crc32_le(0, ptr, len)) {
1498                printk("dvb-ttpci: crc32 of root file does not match.\n");
1499                return -EINVAL;
1500        }
1501        av7110->bin_root = ptr;
1502        av7110->size_root = len;
1503        return 0;
1504}
1505
1506static void put_firmware(struct av7110* av7110)
1507{
1508        vfree(av7110->bin_fw);
1509}
1510
1511static int get_firmware(struct av7110* av7110)
1512{
1513        int ret;
1514        const struct firmware *fw;
1515
1516        /* request the av7110 firmware, this will block until someone uploads it */
1517        ret = request_firmware(&fw, "dvb-ttpci-01.fw", &av7110->dev->pci->dev);
1518        if (ret) {
1519                if (ret == -ENOENT) {
1520                        printk(KERN_ERR "dvb-ttpci: could not load firmware, file not found: dvb-ttpci-01.fw\n");
1521                        printk(KERN_ERR "dvb-ttpci: usually this should be in /usr/lib/hotplug/firmware or /lib/firmware\n");
1522                        printk(KERN_ERR "dvb-ttpci: and can be downloaded from https://linuxtv.org/download/dvb/firmware/\n");
1523                } else
1524                        printk(KERN_ERR "dvb-ttpci: cannot request firmware (error %i)\n",
1525                               ret);
1526                return -EINVAL;
1527        }
1528
1529        if (fw->size <= 200000) {
1530                printk("dvb-ttpci: this firmware is way too small.\n");
1531                release_firmware(fw);
1532                return -EINVAL;
1533        }
1534
1535        /* check if the firmware is available */
1536        av7110->bin_fw = vmalloc(fw->size);
1537        if (NULL == av7110->bin_fw) {
1538                dprintk(1, "out of memory\n");
1539                release_firmware(fw);
1540                return -ENOMEM;
1541        }
1542
1543        memcpy(av7110->bin_fw, fw->data, fw->size);
1544        av7110->size_fw = fw->size;
1545        if ((ret = check_firmware(av7110)))
1546                vfree(av7110->bin_fw);
1547
1548        release_firmware(fw);
1549        return ret;
1550}
1551
1552static int alps_bsrv2_tuner_set_params(struct dvb_frontend *fe)
1553{
1554        struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1555        struct av7110* av7110 = fe->dvb->priv;
1556        u8 pwr = 0;
1557        u8 buf[4];
1558        struct i2c_msg msg = { .addr = 0x61, .flags = 0, .buf = buf, .len = sizeof(buf) };
1559        u32 div = (p->frequency + 479500) / 125;
1560
1561        if (p->frequency > 2000000)
1562                pwr = 3;
1563        else if (p->frequency > 1800000)
1564                pwr = 2;
1565        else if (p->frequency > 1600000)
1566                pwr = 1;
1567        else if (p->frequency > 1200000)
1568                pwr = 0;
1569        else if (p->frequency >= 1100000)
1570                pwr = 1;
1571        else
1572                pwr = 2;
1573
1574        buf[0] = (div >> 8) & 0x7f;
1575        buf[1] = div & 0xff;
1576        buf[2] = ((div & 0x18000) >> 10) | 0x95;
1577        buf[3] = (pwr << 6) | 0x30;
1578
1579        // NOTE: since we're using a prescaler of 2, we set the
1580        // divisor frequency to 62.5kHz and divide by 125 above
1581
1582        if (fe->ops.i2c_gate_ctrl)
1583                fe->ops.i2c_gate_ctrl(fe, 1);
1584        if (i2c_transfer (&av7110->i2c_adap, &msg, 1) != 1)
1585                return -EIO;
1586        return 0;
1587}
1588
1589static struct ves1x93_config alps_bsrv2_config = {
1590        .demod_address = 0x08,
1591        .xin = 90100000UL,
1592        .invert_pwm = 0,
1593};
1594
1595static int alps_tdbe2_tuner_set_params(struct dvb_frontend *fe)
1596{
1597        struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1598        struct av7110* av7110 = fe->dvb->priv;
1599        u32 div;
1600        u8 data[4];
1601        struct i2c_msg msg = { .addr = 0x62, .flags = 0, .buf = data, .len = sizeof(data) };
1602
1603        div = (p->frequency + 35937500 + 31250) / 62500;
1604
1605        data[0] = (div >> 8) & 0x7f;
1606        data[1] = div & 0xff;
1607        data[2] = 0x85 | ((div >> 10) & 0x60);
1608        data[3] = (p->frequency < 174000000 ? 0x88 : p->frequency < 470000000 ? 0x84 : 0x81);
1609
1610        if (fe->ops.i2c_gate_ctrl)
1611                fe->ops.i2c_gate_ctrl(fe, 1);
1612        if (i2c_transfer(&av7110->i2c_adap, &msg, 1) != 1)
1613                return -EIO;
1614        return 0;
1615}
1616
1617static struct ves1820_config alps_tdbe2_config = {
1618        .demod_address = 0x09,
1619        .xin = 57840000UL,
1620        .invert = 1,
1621        .selagc = VES1820_SELAGC_SIGNAMPERR,
1622};
1623
1624
1625
1626
1627static int grundig_29504_451_tuner_set_params(struct dvb_frontend *fe)
1628{
1629        struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1630        struct av7110* av7110 = fe->dvb->priv;
1631        u32 div;
1632        u8 data[4];
1633        struct i2c_msg msg = { .addr = 0x61, .flags = 0, .buf = data, .len = sizeof(data) };
1634
1635        div = p->frequency / 125;
1636        data[0] = (div >> 8) & 0x7f;
1637        data[1] = div & 0xff;
1638        data[2] = 0x8e;
1639        data[3] = 0x00;
1640
1641        if (fe->ops.i2c_gate_ctrl)
1642                fe->ops.i2c_gate_ctrl(fe, 1);
1643        if (i2c_transfer(&av7110->i2c_adap, &msg, 1) != 1)
1644                return -EIO;
1645        return 0;
1646}
1647
1648static struct tda8083_config grundig_29504_451_config = {
1649        .demod_address = 0x68,
1650};
1651
1652
1653
1654static int philips_cd1516_tuner_set_params(struct dvb_frontend *fe)
1655{
1656        struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1657        struct av7110* av7110 = fe->dvb->priv;
1658        u32 div;
1659        u32 f = p->frequency;
1660        u8 data[4];
1661        struct i2c_msg msg = { .addr = 0x61, .flags = 0, .buf = data, .len = sizeof(data) };
1662
1663        div = (f + 36125000 + 31250) / 62500;
1664
1665        data[0] = (div >> 8) & 0x7f;
1666        data[1] = div & 0xff;
1667        data[2] = 0x8e;
1668        data[3] = (f < 174000000 ? 0xa1 : f < 470000000 ? 0x92 : 0x34);
1669
1670        if (fe->ops.i2c_gate_ctrl)
1671                fe->ops.i2c_gate_ctrl(fe, 1);
1672        if (i2c_transfer(&av7110->i2c_adap, &msg, 1) != 1)
1673                return -EIO;
1674        return 0;
1675}
1676
1677static struct ves1820_config philips_cd1516_config = {
1678        .demod_address = 0x09,
1679        .xin = 57840000UL,
1680        .invert = 1,
1681        .selagc = VES1820_SELAGC_SIGNAMPERR,
1682};
1683
1684
1685
1686static int alps_tdlb7_tuner_set_params(struct dvb_frontend *fe)
1687{
1688        struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1689        struct av7110* av7110 = fe->dvb->priv;
1690        u32 div, pwr;
1691        u8 data[4];
1692        struct i2c_msg msg = { .addr = 0x60, .flags = 0, .buf = data, .len = sizeof(data) };
1693
1694        div = (p->frequency + 36200000) / 166666;
1695
1696        if (p->frequency <= 782000000)
1697                pwr = 1;
1698        else
1699                pwr = 2;
1700
1701        data[0] = (div >> 8) & 0x7f;
1702        data[1] = div & 0xff;
1703        data[2] = 0x85;
1704        data[3] = pwr << 6;
1705
1706        if (fe->ops.i2c_gate_ctrl)
1707                fe->ops.i2c_gate_ctrl(fe, 1);
1708        if (i2c_transfer(&av7110->i2c_adap, &msg, 1) != 1)
1709                return -EIO;
1710        return 0;
1711}
1712
1713static int alps_tdlb7_request_firmware(struct dvb_frontend* fe, const struct firmware **fw, char* name)
1714{
1715#if IS_ENABLED(CONFIG_DVB_SP8870)
1716        struct av7110* av7110 = fe->dvb->priv;
1717
1718        return request_firmware(fw, name, &av7110->dev->pci->dev);
1719#else
1720        return -EINVAL;
1721#endif
1722}
1723
1724static const struct sp8870_config alps_tdlb7_config = {
1725
1726        .demod_address = 0x71,
1727        .request_firmware = alps_tdlb7_request_firmware,
1728};
1729
1730
1731static u8 nexusca_stv0297_inittab[] = {
1732        0x80, 0x01,
1733        0x80, 0x00,
1734        0x81, 0x01,
1735        0x81, 0x00,
1736        0x00, 0x09,
1737        0x01, 0x69,
1738        0x03, 0x00,
1739        0x04, 0x00,
1740        0x07, 0x00,
1741        0x08, 0x00,
1742        0x20, 0x00,
1743        0x21, 0x40,
1744        0x22, 0x00,
1745        0x23, 0x00,
1746        0x24, 0x40,
1747        0x25, 0x88,
1748        0x30, 0xff,
1749        0x31, 0x00,
1750        0x32, 0xff,
1751        0x33, 0x00,
1752        0x34, 0x50,
1753        0x35, 0x7f,
1754        0x36, 0x00,
1755        0x37, 0x20,
1756        0x38, 0x00,
1757        0x40, 0x1c,
1758        0x41, 0xff,
1759        0x42, 0x29,
1760        0x43, 0x00,
1761        0x44, 0xff,
1762        0x45, 0x00,
1763        0x46, 0x00,
1764        0x49, 0x04,
1765        0x4a, 0x00,
1766        0x4b, 0x7b,
1767        0x52, 0x30,
1768        0x55, 0xae,
1769        0x56, 0x47,
1770        0x57, 0xe1,
1771        0x58, 0x3a,
1772        0x5a, 0x1e,
1773        0x5b, 0x34,
1774        0x60, 0x00,
1775        0x63, 0x00,
1776        0x64, 0x00,
1777        0x65, 0x00,
1778        0x66, 0x00,
1779        0x67, 0x00,
1780        0x68, 0x00,
1781        0x69, 0x00,
1782        0x6a, 0x02,
1783        0x6b, 0x00,
1784        0x70, 0xff,
1785        0x71, 0x00,
1786        0x72, 0x00,
1787        0x73, 0x00,
1788        0x74, 0x0c,
1789        0x80, 0x00,
1790        0x81, 0x00,
1791        0x82, 0x00,
1792        0x83, 0x00,
1793        0x84, 0x04,
1794        0x85, 0x80,
1795        0x86, 0x24,
1796        0x87, 0x78,
1797        0x88, 0x10,
1798        0x89, 0x00,
1799        0x90, 0x01,
1800        0x91, 0x01,
1801        0xa0, 0x04,
1802        0xa1, 0x00,
1803        0xa2, 0x00,
1804        0xb0, 0x91,
1805        0xb1, 0x0b,
1806        0xc0, 0x53,
1807        0xc1, 0x70,
1808        0xc2, 0x12,
1809        0xd0, 0x00,
1810        0xd1, 0x00,
1811        0xd2, 0x00,
1812        0xd3, 0x00,
1813        0xd4, 0x00,
1814        0xd5, 0x00,
1815        0xde, 0x00,
1816        0xdf, 0x00,
1817        0x61, 0x49,
1818        0x62, 0x0b,
1819        0x53, 0x08,
1820        0x59, 0x08,
1821        0xff, 0xff,
1822};
1823
1824static int nexusca_stv0297_tuner_set_params(struct dvb_frontend *fe)
1825{
1826        struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1827        struct av7110* av7110 = fe->dvb->priv;
1828        u32 div;
1829        u8 data[4];
1830        struct i2c_msg msg = { .addr = 0x63, .flags = 0, .buf = data, .len = sizeof(data) };
1831        struct i2c_msg readmsg = { .addr = 0x63, .flags = I2C_M_RD, .buf = data, .len = 1 };
1832        int i;
1833
1834        div = (p->frequency + 36150000 + 31250) / 62500;
1835
1836        data[0] = (div >> 8) & 0x7f;
1837        data[1] = div & 0xff;
1838        data[2] = 0xce;
1839
1840        if (p->frequency < 45000000)
1841                return -EINVAL;
1842        else if (p->frequency < 137000000)
1843                data[3] = 0x01;
1844        else if (p->frequency < 403000000)
1845                data[3] = 0x02;
1846        else if (p->frequency < 860000000)
1847                data[3] = 0x04;
1848        else
1849                return -EINVAL;
1850
1851        if (fe->ops.i2c_gate_ctrl)
1852                fe->ops.i2c_gate_ctrl(fe, 1);
1853        if (i2c_transfer(&av7110->i2c_adap, &msg, 1) != 1) {
1854                printk("nexusca: pll transfer failed!\n");
1855                return -EIO;
1856        }
1857
1858        // wait for PLL lock
1859        for(i = 0; i < 20; i++) {
1860                if (fe->ops.i2c_gate_ctrl)
1861                        fe->ops.i2c_gate_ctrl(fe, 1);
1862                if (i2c_transfer(&av7110->i2c_adap, &readmsg, 1) == 1)
1863                        if (data[0] & 0x40) break;
1864                msleep(10);
1865        }
1866
1867        return 0;
1868}
1869
1870static struct stv0297_config nexusca_stv0297_config = {
1871
1872        .demod_address = 0x1C,
1873        .inittab = nexusca_stv0297_inittab,
1874        .invert = 1,
1875        .stop_during_read = 1,
1876};
1877
1878
1879
1880static int grundig_29504_401_tuner_set_params(struct dvb_frontend *fe)
1881{
1882        struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1883        struct av7110* av7110 = fe->dvb->priv;
1884        u32 div;
1885        u8 cfg, cpump, band_select;
1886        u8 data[4];
1887        struct i2c_msg msg = { .addr = 0x61, .flags = 0, .buf = data, .len = sizeof(data) };
1888
1889        div = (36125000 + p->frequency) / 166666;
1890
1891        cfg = 0x88;
1892
1893        if (p->frequency < 175000000)
1894                cpump = 2;
1895        else if (p->frequency < 390000000)
1896                cpump = 1;
1897        else if (p->frequency < 470000000)
1898                cpump = 2;
1899        else if (p->frequency < 750000000)
1900                cpump = 1;
1901        else
1902                cpump = 3;
1903
1904        if (p->frequency < 175000000)
1905                band_select = 0x0e;
1906        else if (p->frequency < 470000000)
1907                band_select = 0x05;
1908        else
1909                band_select = 0x03;
1910
1911        data[0] = (div >> 8) & 0x7f;
1912        data[1] = div & 0xff;
1913        data[2] = ((div >> 10) & 0x60) | cfg;
1914        data[3] = (cpump << 6) | band_select;
1915
1916        if (fe->ops.i2c_gate_ctrl)
1917                fe->ops.i2c_gate_ctrl(fe, 1);
1918        if (i2c_transfer (&av7110->i2c_adap, &msg, 1) != 1) return -EIO;
1919        return 0;
1920}
1921
1922static struct l64781_config grundig_29504_401_config = {
1923        .demod_address = 0x55,
1924};
1925
1926
1927
1928static int av7110_fe_lock_fix(struct av7110 *av7110, enum fe_status status)
1929{
1930        int ret = 0;
1931        int synced = (status & FE_HAS_LOCK) ? 1 : 0;
1932
1933        av7110->fe_status = status;
1934
1935        if (av7110->fe_synced == synced)
1936                return 0;
1937
1938        if (av7110->playing) {
1939                av7110->fe_synced = synced;
1940                return 0;
1941        }
1942
1943        if (mutex_lock_interruptible(&av7110->pid_mutex))
1944                return -ERESTARTSYS;
1945
1946        if (synced) {
1947                ret = SetPIDs(av7110, av7110->pids[DMX_PES_VIDEO],
1948                        av7110->pids[DMX_PES_AUDIO],
1949                        av7110->pids[DMX_PES_TELETEXT], 0,
1950                        av7110->pids[DMX_PES_PCR]);
1951                if (!ret)
1952                        ret = av7110_fw_cmd(av7110, COMTYPE_PIDFILTER, Scan, 0);
1953        } else {
1954                ret = SetPIDs(av7110, 0, 0, 0, 0, 0);
1955                if (!ret) {
1956                        ret = av7110_fw_cmd(av7110, COMTYPE_PID_FILTER, FlushTSQueue, 0);
1957                        if (!ret)
1958                                ret = av7110_wait_msgstate(av7110, GPMQBusy);
1959                }
1960        }
1961
1962        if (!ret)
1963                av7110->fe_synced = synced;
1964
1965        mutex_unlock(&av7110->pid_mutex);
1966        return ret;
1967}
1968
1969static int av7110_fe_set_frontend(struct dvb_frontend *fe)
1970{
1971        struct av7110* av7110 = fe->dvb->priv;
1972
1973        int ret = av7110_fe_lock_fix(av7110, 0);
1974        if (!ret)
1975                ret = av7110->fe_set_frontend(fe);
1976
1977        return ret;
1978}
1979
1980static int av7110_fe_init(struct dvb_frontend* fe)
1981{
1982        struct av7110* av7110 = fe->dvb->priv;
1983
1984        int ret = av7110_fe_lock_fix(av7110, 0);
1985        if (!ret)
1986                ret = av7110->fe_init(fe);
1987        return ret;
1988}
1989
1990static int av7110_fe_read_status(struct dvb_frontend *fe,
1991                                 enum fe_status *status)
1992{
1993        struct av7110* av7110 = fe->dvb->priv;
1994
1995        /* call the real implementation */
1996        int ret = av7110->fe_read_status(fe, status);
1997        if (!ret)
1998                if (((*status ^ av7110->fe_status) & FE_HAS_LOCK) && (*status & FE_HAS_LOCK))
1999                        ret = av7110_fe_lock_fix(av7110, *status);
2000        return ret;
2001}
2002
2003static int av7110_fe_diseqc_reset_overload(struct dvb_frontend* fe)
2004{
2005        struct av7110* av7110 = fe->dvb->priv;
2006
2007        int ret = av7110_fe_lock_fix(av7110, 0);
2008        if (!ret)
2009                ret = av7110->fe_diseqc_reset_overload(fe);
2010        return ret;
2011}
2012
2013static int av7110_fe_diseqc_send_master_cmd(struct dvb_frontend* fe,
2014                                            struct dvb_diseqc_master_cmd* cmd)
2015{
2016        struct av7110* av7110 = fe->dvb->priv;
2017
2018        int ret = av7110_fe_lock_fix(av7110, 0);
2019        if (!ret) {
2020                av7110->saved_master_cmd = *cmd;
2021                ret = av7110->fe_diseqc_send_master_cmd(fe, cmd);
2022        }
2023        return ret;
2024}
2025
2026static int av7110_fe_diseqc_send_burst(struct dvb_frontend *fe,
2027                                       enum fe_sec_mini_cmd minicmd)
2028{
2029        struct av7110* av7110 = fe->dvb->priv;
2030
2031        int ret = av7110_fe_lock_fix(av7110, 0);
2032        if (!ret) {
2033                av7110->saved_minicmd = minicmd;
2034                ret = av7110->fe_diseqc_send_burst(fe, minicmd);
2035        }
2036        return ret;
2037}
2038
2039static int av7110_fe_set_tone(struct dvb_frontend *fe,
2040                              enum fe_sec_tone_mode tone)
2041{
2042        struct av7110* av7110 = fe->dvb->priv;
2043
2044        int ret = av7110_fe_lock_fix(av7110, 0);
2045        if (!ret) {
2046                av7110->saved_tone = tone;
2047                ret = av7110->fe_set_tone(fe, tone);
2048        }
2049        return ret;
2050}
2051
2052static int av7110_fe_set_voltage(struct dvb_frontend *fe,
2053                                 enum fe_sec_voltage voltage)
2054{
2055        struct av7110* av7110 = fe->dvb->priv;
2056
2057        int ret = av7110_fe_lock_fix(av7110, 0);
2058        if (!ret) {
2059                av7110->saved_voltage = voltage;
2060                ret = av7110->fe_set_voltage(fe, voltage);
2061        }
2062        return ret;
2063}
2064
2065static int av7110_fe_dishnetwork_send_legacy_command(struct dvb_frontend* fe, unsigned long cmd)
2066{
2067        struct av7110* av7110 = fe->dvb->priv;
2068
2069        int ret = av7110_fe_lock_fix(av7110, 0);
2070        if (!ret)
2071                ret = av7110->fe_dishnetwork_send_legacy_command(fe, cmd);
2072        return ret;
2073}
2074
2075static void dvb_s_recover(struct av7110* av7110)
2076{
2077        av7110_fe_init(av7110->fe);
2078
2079        av7110_fe_set_voltage(av7110->fe, av7110->saved_voltage);
2080        if (av7110->saved_master_cmd.msg_len) {
2081                msleep(20);
2082                av7110_fe_diseqc_send_master_cmd(av7110->fe, &av7110->saved_master_cmd);
2083        }
2084        msleep(20);
2085        av7110_fe_diseqc_send_burst(av7110->fe, av7110->saved_minicmd);
2086        msleep(20);
2087        av7110_fe_set_tone(av7110->fe, av7110->saved_tone);
2088
2089        av7110_fe_set_frontend(av7110->fe);
2090}
2091
2092static u8 read_pwm(struct av7110* av7110)
2093{
2094        u8 b = 0xff;
2095        u8 pwm;
2096        struct i2c_msg msg[] = { { .addr = 0x50,.flags = 0,.buf = &b,.len = 1 },
2097                                 { .addr = 0x50,.flags = I2C_M_RD,.buf = &pwm,.len = 1} };
2098
2099        if ((i2c_transfer(&av7110->i2c_adap, msg, 2) != 2) || (pwm == 0xff))
2100                pwm = 0x48;
2101
2102        return pwm;
2103}
2104
2105static int frontend_init(struct av7110 *av7110)
2106{
2107        int ret;
2108
2109        if (av7110->dev->pci->subsystem_vendor == 0x110a) {
2110                switch(av7110->dev->pci->subsystem_device) {
2111                case 0x0000: // Fujitsu/Siemens DVB-Cable (ves1820/Philips CD1516(??))
2112                        av7110->fe = dvb_attach(ves1820_attach, &philips_cd1516_config,
2113                                                    &av7110->i2c_adap, read_pwm(av7110));
2114                        if (av7110->fe) {
2115                                av7110->fe->ops.tuner_ops.set_params = philips_cd1516_tuner_set_params;
2116                        }
2117                        break;
2118                }
2119
2120        } else if (av7110->dev->pci->subsystem_vendor == 0x13c2) {
2121                switch(av7110->dev->pci->subsystem_device) {
2122                case 0x0000: // Hauppauge/TT WinTV DVB-S rev1.X
2123                case 0x0003: // Hauppauge/TT WinTV Nexus-S Rev 2.X
2124                case 0x1002: // Hauppauge/TT WinTV DVB-S rev1.3SE
2125
2126                        // try the ALPS BSRV2 first of all
2127                        av7110->fe = dvb_attach(ves1x93_attach, &alps_bsrv2_config, &av7110->i2c_adap);
2128                        if (av7110->fe) {
2129                                av7110->fe->ops.tuner_ops.set_params = alps_bsrv2_tuner_set_params;
2130                                av7110->fe->ops.diseqc_send_master_cmd = av7110_diseqc_send_master_cmd;
2131                                av7110->fe->ops.diseqc_send_burst = av7110_diseqc_send_burst;
2132                                av7110->fe->ops.set_tone = av7110_set_tone;
2133                                av7110->recover = dvb_s_recover;
2134                                break;
2135                        }
2136
2137                        // try the ALPS BSRU6 now
2138                        av7110->fe = dvb_attach(stv0299_attach, &alps_bsru6_config, &av7110->i2c_adap);
2139                        if (av7110->fe) {
2140                                av7110->fe->ops.tuner_ops.set_params = alps_bsru6_tuner_set_params;
2141                                av7110->fe->tuner_priv = &av7110->i2c_adap;
2142
2143                                av7110->fe->ops.diseqc_send_master_cmd = av7110_diseqc_send_master_cmd;
2144                                av7110->fe->ops.diseqc_send_burst = av7110_diseqc_send_burst;
2145                                av7110->fe->ops.set_tone = av7110_set_tone;
2146                                av7110->recover = dvb_s_recover;
2147                                break;
2148                        }
2149
2150                        // Try the grundig 29504-451
2151                        av7110->fe = dvb_attach(tda8083_attach, &grundig_29504_451_config, &av7110->i2c_adap);
2152                        if (av7110->fe) {
2153                                av7110->fe->ops.tuner_ops.set_params = grundig_29504_451_tuner_set_params;
2154                                av7110->fe->ops.diseqc_send_master_cmd = av7110_diseqc_send_master_cmd;
2155                                av7110->fe->ops.diseqc_send_burst = av7110_diseqc_send_burst;
2156                                av7110->fe->ops.set_tone = av7110_set_tone;
2157                                av7110->recover = dvb_s_recover;
2158                                break;
2159                        }
2160
2161                        /* Try DVB-C cards */
2162                        switch(av7110->dev->pci->subsystem_device) {
2163                        case 0x0000:
2164                                /* Siemens DVB-C (full-length card) VES1820/Philips CD1516 */
2165                                av7110->fe = dvb_attach(ves1820_attach, &philips_cd1516_config, &av7110->i2c_adap,
2166                                                        read_pwm(av7110));
2167                                if (av7110->fe) {
2168                                        av7110->fe->ops.tuner_ops.set_params = philips_cd1516_tuner_set_params;
2169                                }
2170                                break;
2171                        case 0x0003:
2172                                /* Hauppauge DVB-C 2.1 VES1820/ALPS TDBE2 */
2173                                av7110->fe = dvb_attach(ves1820_attach, &alps_tdbe2_config, &av7110->i2c_adap,
2174                                                        read_pwm(av7110));
2175                                if (av7110->fe) {
2176                                        av7110->fe->ops.tuner_ops.set_params = alps_tdbe2_tuner_set_params;
2177                                }
2178                                break;
2179                        }
2180                        break;
2181
2182                case 0x0001: // Hauppauge/TT Nexus-T premium rev1.X
2183                {
2184                        struct dvb_frontend *fe;
2185
2186                        // try ALPS TDLB7 first, then Grundig 29504-401
2187                        fe = dvb_attach(sp8870_attach, &alps_tdlb7_config, &av7110->i2c_adap);
2188                        if (fe) {
2189                                fe->ops.tuner_ops.set_params = alps_tdlb7_tuner_set_params;
2190                                av7110->fe = fe;
2191                                break;
2192                        }
2193                }
2194                /* fall-thru */
2195
2196                case 0x0008: // Hauppauge/TT DVB-T
2197                        // Grundig 29504-401
2198                        av7110->fe = dvb_attach(l64781_attach, &grundig_29504_401_config, &av7110->i2c_adap);
2199                        if (av7110->fe)
2200                                av7110->fe->ops.tuner_ops.set_params = grundig_29504_401_tuner_set_params;
2201                        break;
2202
2203                case 0x0002: // Hauppauge/TT DVB-C premium rev2.X
2204
2205                        av7110->fe = dvb_attach(ves1820_attach, &alps_tdbe2_config, &av7110->i2c_adap, read_pwm(av7110));
2206                        if (av7110->fe) {
2207                                av7110->fe->ops.tuner_ops.set_params = alps_tdbe2_tuner_set_params;
2208                        }
2209                        break;
2210
2211                case 0x0004: // Galaxis DVB-S rev1.3
2212                        /* ALPS BSRV2 */
2213                        av7110->fe = dvb_attach(ves1x93_attach, &alps_bsrv2_config, &av7110->i2c_adap);
2214                        if (av7110->fe) {
2215                                av7110->fe->ops.tuner_ops.set_params = alps_bsrv2_tuner_set_params;
2216                                av7110->fe->ops.diseqc_send_master_cmd = av7110_diseqc_send_master_cmd;
2217                                av7110->fe->ops.diseqc_send_burst = av7110_diseqc_send_burst;
2218                                av7110->fe->ops.set_tone = av7110_set_tone;
2219                                av7110->recover = dvb_s_recover;
2220                        }
2221                        break;
2222
2223                case 0x0006: /* Fujitsu-Siemens DVB-S rev 1.6 */
2224                        /* Grundig 29504-451 */
2225                        av7110->fe = dvb_attach(tda8083_attach, &grundig_29504_451_config, &av7110->i2c_adap);
2226                        if (av7110->fe) {
2227                                av7110->fe->ops.tuner_ops.set_params = grundig_29504_451_tuner_set_params;
2228                                av7110->fe->ops.diseqc_send_master_cmd = av7110_diseqc_send_master_cmd;
2229                                av7110->fe->ops.diseqc_send_burst = av7110_diseqc_send_burst;
2230                                av7110->fe->ops.set_tone = av7110_set_tone;
2231                                av7110->recover = dvb_s_recover;
2232                        }
2233                        break;
2234
2235                case 0x000A: // Hauppauge/TT Nexus-CA rev1.X
2236
2237                        av7110->fe = dvb_attach(stv0297_attach, &nexusca_stv0297_config, &av7110->i2c_adap);
2238                        if (av7110->fe) {
2239                                av7110->fe->ops.tuner_ops.set_params = nexusca_stv0297_tuner_set_params;
2240
2241                                /* set TDA9819 into DVB mode */
2242                                saa7146_setgpio(av7110->dev, 1, SAA7146_GPIO_OUTLO); // TDA9819 pin9(STD)
2243                                saa7146_setgpio(av7110->dev, 3, SAA7146_GPIO_OUTLO); // TDA9819 pin30(VIF)
2244
2245                                /* tuner on this needs a slower i2c bus speed */
2246                                av7110->dev->i2c_bitrate = SAA7146_I2C_BUS_BIT_RATE_240;
2247                                break;
2248                        }
2249                        break;
2250
2251                case 0x000E: /* Hauppauge/TT Nexus-S rev 2.3 */
2252                        /* ALPS BSBE1 */
2253                        av7110->fe = dvb_attach(stv0299_attach, &alps_bsbe1_config, &av7110->i2c_adap);
2254                        if (av7110->fe) {
2255                                av7110->fe->ops.tuner_ops.set_params = alps_bsbe1_tuner_set_params;
2256                                av7110->fe->tuner_priv = &av7110->i2c_adap;
2257
2258                                if (dvb_attach(lnbp21_attach, av7110->fe, &av7110->i2c_adap, 0, 0) == NULL) {
2259                                        printk("dvb-ttpci: LNBP21 not found!\n");
2260                                        if (av7110->fe->ops.release)
2261                                                av7110->fe->ops.release(av7110->fe);
2262                                        av7110->fe = NULL;
2263                                } else {
2264                                        av7110->fe->ops.dishnetwork_send_legacy_command = NULL;
2265                                        av7110->recover = dvb_s_recover;
2266                                }
2267                        }
2268                        break;
2269                }
2270        }
2271
2272        if (!av7110->fe) {
2273                /* FIXME: propagate the failure code from the lower layers */
2274                ret = -ENOMEM;
2275                printk("dvb-ttpci: A frontend driver was not found for device [%04x:%04x] subsystem [%04x:%04x]\n",
2276                       av7110->dev->pci->vendor,
2277                       av7110->dev->pci->device,
2278                       av7110->dev->pci->subsystem_vendor,
2279                       av7110->dev->pci->subsystem_device);
2280        } else {
2281                FE_FUNC_OVERRIDE(av7110->fe->ops.init, av7110->fe_init, av7110_fe_init);
2282                FE_FUNC_OVERRIDE(av7110->fe->ops.read_status, av7110->fe_read_status, av7110_fe_read_status);
2283                FE_FUNC_OVERRIDE(av7110->fe->ops.diseqc_reset_overload, av7110->fe_diseqc_reset_overload, av7110_fe_diseqc_reset_overload);
2284                FE_FUNC_OVERRIDE(av7110->fe->ops.diseqc_send_master_cmd, av7110->fe_diseqc_send_master_cmd, av7110_fe_diseqc_send_master_cmd);
2285                FE_FUNC_OVERRIDE(av7110->fe->ops.diseqc_send_burst, av7110->fe_diseqc_send_burst, av7110_fe_diseqc_send_burst);
2286                FE_FUNC_OVERRIDE(av7110->fe->ops.set_tone, av7110->fe_set_tone, av7110_fe_set_tone);
2287                FE_FUNC_OVERRIDE(av7110->fe->ops.set_voltage, av7110->fe_set_voltage, av7110_fe_set_voltage);
2288                FE_FUNC_OVERRIDE(av7110->fe->ops.dishnetwork_send_legacy_command, av7110->fe_dishnetwork_send_legacy_command, av7110_fe_dishnetwork_send_legacy_command);
2289                FE_FUNC_OVERRIDE(av7110->fe->ops.set_frontend, av7110->fe_set_frontend, av7110_fe_set_frontend);
2290
2291                ret = dvb_register_frontend(&av7110->dvb_adapter, av7110->fe);
2292                if (ret < 0) {
2293                        printk("av7110: Frontend registration failed!\n");
2294                        dvb_frontend_detach(av7110->fe);
2295                        av7110->fe = NULL;
2296                }
2297        }
2298        return ret;
2299}
2300
2301/* Budgetpatch note:
2302 * Original hardware design by Roberto Deza:
2303 * There is a DVB_Wiki at
2304 * https://linuxtv.org
2305 *
2306 * New software triggering design by Emard that works on
2307 * original Roberto Deza's hardware:
2308 *
2309 * rps1 code for budgetpatch will copy internal HS event to GPIO3 pin.
2310 * GPIO3 is in budget-patch hardware connectd to port B VSYNC
2311 * HS is an internal event of 7146, accessible with RPS
2312 * and temporarily raised high every n lines
2313 * (n in defined in the RPS_THRESH1 counter threshold)
2314 * I think HS is raised high on the beginning of the n-th line
2315 * and remains high until this n-th line that triggered
2316 * it is completely received. When the receiption of n-th line
2317 * ends, HS is lowered.
2318 *
2319 * To transmit data over DMA, 7146 needs changing state at
2320 * port B VSYNC pin. Any changing of port B VSYNC will
2321 * cause some DMA data transfer, with more or less packets loss.
2322 * It depends on the phase and frequency of VSYNC and
2323 * the way of 7146 is instructed to trigger on port B (defined
2324 * in DD1_INIT register, 3rd nibble from the right valid
2325 * numbers are 0-7, see datasheet)
2326 *
2327 * The correct triggering can minimize packet loss,
2328 * dvbtraffic should give this stable bandwidths:
2329 *   22k transponder = 33814 kbit/s
2330 * 27.5k transponder = 38045 kbit/s
2331 * by experiment it is found that the best results
2332 * (stable bandwidths and almost no packet loss)
2333 * are obtained using DD1_INIT triggering number 2
2334 * (Va at rising edge of VS Fa = HS x VS-failing forced toggle)
2335 * and a VSYNC phase that occurs in the middle of DMA transfer
2336 * (about byte 188*512=96256 in the DMA window).
2337 *
2338 * Phase of HS is still not clear to me how to control,
2339 * It just happens to be so. It can be seen if one enables
2340 * RPS_IRQ and print Event Counter 1 in vpeirq(). Every
2341 * time RPS_INTERRUPT is called, the Event Counter 1 will
2342 * increment. That's how the 7146 is programmed to do event
2343 * counting in this budget-patch.c
2344 * I *think* HPS setting has something to do with the phase
2345 * of HS but I can't be 100% sure in that.
2346 *
2347 * hardware debug note: a working budget card (including budget patch)
2348 * with vpeirq() interrupt setup in mode "0x90" (every 64K) will
2349 * generate 3 interrupts per 25-Hz DMA frame of 2*188*512 bytes
2350 * and that means 3*25=75 Hz of interrupt freqency, as seen by
2351 * watch cat /proc/interrupts
2352 *
2353 * If this frequency is 3x lower (and data received in the DMA
2354 * buffer don't start with 0x47, but in the middle of packets,
2355 * whose lengths appear to be like 188 292 188 104 etc.
2356 * this means VSYNC line is not connected in the hardware.
2357 * (check soldering pcb and pins)
2358 * The same behaviour of missing VSYNC can be duplicated on budget
2359 * cards, by seting DD1_INIT trigger mode 7 in 3rd nibble.
2360 */
2361static int av7110_attach(struct saa7146_dev* dev,
2362                         struct saa7146_pci_extension_data *pci_ext)
2363{
2364        const int length = TS_WIDTH * TS_HEIGHT;
2365        struct pci_dev *pdev = dev->pci;
2366        struct av7110 *av7110;
2367        struct task_struct *thread;
2368        int ret, count = 0;
2369
2370        dprintk(4, "dev: %p\n", dev);
2371
2372        /* Set RPS_IRQ to 1 to track rps1 activity.
2373         * Enabling this won't send any interrupt to PC CPU.
2374         */
2375#define RPS_IRQ 0
2376
2377        if (budgetpatch == 1) {
2378                budgetpatch = 0;
2379                /* autodetect the presence of budget patch
2380                 * this only works if saa7146 has been recently
2381                 * reset with with MASK_31 to MC1
2382                 *
2383                 * will wait for VBI_B event (vertical blank at port B)
2384                 * and will reset GPIO3 after VBI_B is detected.
2385                 * (GPIO3 should be raised high by CPU to
2386                 * test if GPIO3 will generate vertical blank signal
2387                 * in budget patch GPIO3 is connected to VSYNC_B
2388                 */
2389
2390                /* RESET SAA7146 */
2391                saa7146_write(dev, MC1, MASK_31);
2392                /* autodetection success seems to be time-dependend after reset */
2393
2394                /* Fix VSYNC level */
2395                saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTLO);
2396                /* set vsync_b triggering */
2397                saa7146_write(dev, DD1_STREAM_B, 0);
2398                /* port B VSYNC at rising edge */
2399                saa7146_write(dev, DD1_INIT, 0x00000200);
2400                saa7146_write(dev, BRS_CTRL, 0x00000000);  // VBI
2401                saa7146_write(dev, MC2,
2402                              1 * (MASK_08 | MASK_24)  |   // BRS control
2403                              0 * (MASK_09 | MASK_25)  |   // a
2404                              1 * (MASK_10 | MASK_26)  |   // b
2405                              0 * (MASK_06 | MASK_22)  |   // HPS_CTRL1
2406                              0 * (MASK_05 | MASK_21)  |   // HPS_CTRL2
2407                              0 * (MASK_01 | MASK_15)      // DEBI
2408                );
2409
2410                /* start writing RPS1 code from beginning */
2411                count = 0;
2412                /* Disable RPS1 */
2413                saa7146_write(dev, MC1, MASK_29);
2414                /* RPS1 timeout disable */
2415                saa7146_write(dev, RPS_TOV1, 0);
2416                WRITE_RPS1(CMD_PAUSE | EVT_VBI_B);
2417                WRITE_RPS1(CMD_WR_REG_MASK | (GPIO_CTRL>>2));
2418                WRITE_RPS1(GPIO3_MSK);
2419                WRITE_RPS1(SAA7146_GPIO_OUTLO<<24);
2420#if RPS_IRQ
2421                /* issue RPS1 interrupt to increment counter */
2422                WRITE_RPS1(CMD_INTERRUPT);
2423#endif
2424                WRITE_RPS1(CMD_STOP);
2425                /* Jump to begin of RPS program as safety measure               (p37) */
2426                WRITE_RPS1(CMD_JUMP);
2427                WRITE_RPS1(dev->d_rps1.dma_handle);
2428
2429#if RPS_IRQ
2430                /* set event counter 1 source as RPS1 interrupt (0x03)          (rE4 p53)
2431                 * use 0x03 to track RPS1 interrupts - increase by 1 every gpio3 is toggled
2432                 * use 0x15 to track VPE  interrupts - increase by 1 every vpeirq() is called
2433                 */
2434                saa7146_write(dev, EC1SSR, (0x03<<2) | 3 );
2435                /* set event counter 1 threshold to maximum allowed value        (rEC p55) */
2436                saa7146_write(dev, ECT1R,  0x3fff );
2437#endif
2438                /* Set RPS1 Address register to point to RPS code               (r108 p42) */
2439                saa7146_write(dev, RPS_ADDR1, dev->d_rps1.dma_handle);
2440                /* Enable RPS1,                                                 (rFC p33) */
2441                saa7146_write(dev, MC1, (MASK_13 | MASK_29 ));
2442
2443                mdelay(10);
2444                /* now send VSYNC_B to rps1 by rising GPIO3 */
2445                saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTHI);
2446                mdelay(10);
2447                /* if rps1 responded by lowering the GPIO3,
2448                 * then we have budgetpatch hardware
2449                 */
2450                if ((saa7146_read(dev, GPIO_CTRL) & 0x10000000) == 0) {
2451                        budgetpatch = 1;
2452                        printk("dvb-ttpci: BUDGET-PATCH DETECTED.\n");
2453                }
2454                /* Disable RPS1 */
2455                saa7146_write(dev, MC1, ( MASK_29 ));
2456#if RPS_IRQ
2457                printk("dvb-ttpci: Event Counter 1 0x%04x\n", saa7146_read(dev, EC1R) & 0x3fff );
2458#endif
2459        }
2460
2461        /* prepare the av7110 device struct */
2462        av7110 = kzalloc(sizeof(struct av7110), GFP_KERNEL);
2463        if (!av7110) {
2464                dprintk(1, "out of memory\n");
2465                return -ENOMEM;
2466        }
2467
2468        av7110->card_name = (char*) pci_ext->ext_priv;
2469        av7110->dev = dev;
2470        dev->ext_priv = av7110;
2471
2472        ret = get_firmware(av7110);
2473        if (ret < 0)
2474                goto err_kfree_0;
2475
2476        ret = dvb_register_adapter(&av7110->dvb_adapter, av7110->card_name,
2477                                   THIS_MODULE, &dev->pci->dev, adapter_nr);
2478        if (ret < 0)
2479                goto err_put_firmware_1;
2480
2481        /* the Siemens DVB needs this if you want to have the i2c chips
2482           get recognized before the main driver is fully loaded */
2483        saa7146_write(dev, GPIO_CTRL, 0x500000);
2484
2485        strscpy(av7110->i2c_adap.name, pci_ext->ext_priv,
2486                sizeof(av7110->i2c_adap.name));
2487
2488        saa7146_i2c_adapter_prepare(dev, &av7110->i2c_adap, SAA7146_I2C_BUS_BIT_RATE_120); /* 275 kHz */
2489
2490        ret = i2c_add_adapter(&av7110->i2c_adap);
2491        if (ret < 0)
2492                goto err_dvb_unregister_adapter_2;
2493
2494        ttpci_eeprom_parse_mac(&av7110->i2c_adap,
2495                               av7110->dvb_adapter.proposed_mac);
2496        ret = -ENOMEM;
2497
2498        /* full-ts mod? */
2499        if (full_ts)
2500                av7110->full_ts = true;
2501
2502        /* check for full-ts flag in eeprom */
2503        if (i2c_readreg(av7110, 0xaa, 0) == 0x4f && i2c_readreg(av7110, 0xaa, 1) == 0x45) {
2504                u8 flags = i2c_readreg(av7110, 0xaa, 2);
2505                if (flags != 0xff && (flags & 0x01))
2506                        av7110->full_ts = true;
2507        }
2508
2509        if (av7110->full_ts) {
2510                printk(KERN_INFO "dvb-ttpci: full-ts mode enabled for saa7146 port B\n");
2511                spin_lock_init(&av7110->feedlock1);
2512                av7110->grabbing = saa7146_vmalloc_build_pgtable(pdev, length,
2513                                                                 &av7110->pt);
2514                if (!av7110->grabbing)
2515                        goto err_i2c_del_3;
2516
2517                saa7146_write(dev, DD1_STREAM_B, 0x00000000);
2518                saa7146_write(dev, MC2, (MASK_10 | MASK_26));
2519
2520                saa7146_write(dev, DD1_INIT, 0x00000600);
2521                saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
2522
2523                saa7146_write(dev, BRS_CTRL, 0x60000000);
2524                saa7146_write(dev, MC2, MASK_08 | MASK_24);
2525
2526                /* dma3 */
2527                saa7146_write(dev, PCI_BT_V1, 0x001c0000 | (saa7146_read(dev, PCI_BT_V1) & ~0x001f0000));
2528                saa7146_write(dev, BASE_ODD3, 0);
2529                saa7146_write(dev, BASE_EVEN3, 0);
2530                saa7146_write(dev, PROT_ADDR3, TS_WIDTH * TS_HEIGHT);
2531                saa7146_write(dev, PITCH3, TS_WIDTH);
2532                saa7146_write(dev, BASE_PAGE3, av7110->pt.dma | ME1 | 0x90);
2533                saa7146_write(dev, NUM_LINE_BYTE3, (TS_HEIGHT << 16) | TS_WIDTH);
2534                saa7146_write(dev, MC2, MASK_04 | MASK_20);
2535
2536                tasklet_init(&av7110->vpe_tasklet, vpeirq, (unsigned long) av7110);
2537
2538        } else if (budgetpatch) {
2539                spin_lock_init(&av7110->feedlock1);
2540                av7110->grabbing = saa7146_vmalloc_build_pgtable(pdev, length,
2541                                                                 &av7110->pt);
2542                if (!av7110->grabbing)
2543                        goto err_i2c_del_3;
2544
2545                saa7146_write(dev, PCI_BT_V1, 0x1c1f101f);
2546                saa7146_write(dev, BCS_CTRL, 0x80400040);
2547                /* set dd1 stream a & b */
2548                saa7146_write(dev, DD1_STREAM_B, 0x00000000);
2549                saa7146_write(dev, DD1_INIT, 0x03000200);
2550                saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
2551                saa7146_write(dev, BRS_CTRL, 0x60000000);
2552                saa7146_write(dev, BASE_ODD3, 0);
2553                saa7146_write(dev, BASE_EVEN3, 0);
2554                saa7146_write(dev, PROT_ADDR3, TS_WIDTH * TS_HEIGHT);
2555                saa7146_write(dev, BASE_PAGE3, av7110->pt.dma | ME1 | 0x90);
2556
2557                saa7146_write(dev, PITCH3, TS_WIDTH);
2558                saa7146_write(dev, NUM_LINE_BYTE3, (TS_HEIGHT << 16) | TS_WIDTH);
2559
2560                /* upload all */
2561                saa7146_write(dev, MC2, 0x077c077c);
2562                saa7146_write(dev, GPIO_CTRL, 0x000000);
2563#if RPS_IRQ
2564                /* set event counter 1 source as RPS1 interrupt (0x03)          (rE4 p53)
2565                 * use 0x03 to track RPS1 interrupts - increase by 1 every gpio3 is toggled
2566                 * use 0x15 to track VPE  interrupts - increase by 1 every vpeirq() is called
2567                 */
2568                saa7146_write(dev, EC1SSR, (0x03<<2) | 3 );
2569                /* set event counter 1 threshold to maximum allowed value        (rEC p55) */
2570                saa7146_write(dev, ECT1R,  0x3fff );
2571#endif
2572                /* Setup BUDGETPATCH MAIN RPS1 "program" (p35) */
2573                count = 0;
2574
2575                /* Wait Source Line Counter Threshold                           (p36) */
2576                WRITE_RPS1(CMD_PAUSE | EVT_HS);
2577                /* Set GPIO3=1                                                  (p42) */
2578                WRITE_RPS1(CMD_WR_REG_MASK | (GPIO_CTRL>>2));
2579                WRITE_RPS1(GPIO3_MSK);
2580                WRITE_RPS1(SAA7146_GPIO_OUTHI<<24);
2581#if RPS_IRQ
2582                /* issue RPS1 interrupt */
2583                WRITE_RPS1(CMD_INTERRUPT);
2584#endif
2585                /* Wait reset Source Line Counter Threshold                     (p36) */
2586                WRITE_RPS1(CMD_PAUSE | RPS_INV | EVT_HS);
2587                /* Set GPIO3=0                                                  (p42) */
2588                WRITE_RPS1(CMD_WR_REG_MASK | (GPIO_CTRL>>2));
2589                WRITE_RPS1(GPIO3_MSK);
2590                WRITE_RPS1(SAA7146_GPIO_OUTLO<<24);
2591#if RPS_IRQ
2592                /* issue RPS1 interrupt */
2593                WRITE_RPS1(CMD_INTERRUPT);
2594#endif
2595                /* Jump to begin of RPS program                                 (p37) */
2596                WRITE_RPS1(CMD_JUMP);
2597                WRITE_RPS1(dev->d_rps1.dma_handle);
2598
2599                /* Fix VSYNC level */
2600                saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTLO);
2601                /* Set RPS1 Address register to point to RPS code               (r108 p42) */
2602                saa7146_write(dev, RPS_ADDR1, dev->d_rps1.dma_handle);
2603                /* Set Source Line Counter Threshold, using BRS                 (rCC p43)
2604                 * It generates HS event every TS_HEIGHT lines
2605                 * this is related to TS_WIDTH set in register
2606                 * NUM_LINE_BYTE3. If NUM_LINE_BYTE low 16 bits
2607                 * are set to TS_WIDTH bytes (TS_WIDTH=2*188),
2608                 * then RPS_THRESH1 should be set to trigger
2609                 * every TS_HEIGHT (512) lines.
2610                 */
2611                saa7146_write(dev, RPS_THRESH1, (TS_HEIGHT*1) | MASK_12 );
2612
2613                /* Enable RPS1                                                  (rFC p33) */
2614                saa7146_write(dev, MC1, (MASK_13 | MASK_29));
2615
2616                /* end of budgetpatch register initialization */
2617                tasklet_init (&av7110->vpe_tasklet,  vpeirq,  (unsigned long) av7110);
2618        } else {
2619                saa7146_write(dev, PCI_BT_V1, 0x1c00101f);
2620                saa7146_write(dev, BCS_CTRL, 0x80400040);
2621
2622                /* set dd1 stream a & b */
2623                saa7146_write(dev, DD1_STREAM_B, 0x00000000);
2624                saa7146_write(dev, DD1_INIT, 0x03000000);
2625                saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
2626
2627                /* upload all */
2628                saa7146_write(dev, MC2, 0x077c077c);
2629                saa7146_write(dev, GPIO_CTRL, 0x000000);
2630        }
2631
2632        tasklet_init (&av7110->debi_tasklet, debiirq, (unsigned long) av7110);
2633        tasklet_init (&av7110->gpio_tasklet, gpioirq, (unsigned long) av7110);
2634
2635        mutex_init(&av7110->pid_mutex);
2636
2637        /* locks for data transfers from/to AV7110 */
2638        spin_lock_init(&av7110->debilock);
2639        mutex_init(&av7110->dcomlock);
2640        av7110->debitype = -1;
2641
2642        /* default OSD window */
2643        av7110->osdwin = 1;
2644        mutex_init(&av7110->osd_mutex);
2645
2646        /* TV standard */
2647        av7110->vidmode = tv_standard == 1 ? AV7110_VIDEO_MODE_NTSC
2648                                           : AV7110_VIDEO_MODE_PAL;
2649
2650        /* ARM "watchdog" */
2651        init_waitqueue_head(&av7110->arm_wait);
2652        av7110->arm_thread = NULL;
2653
2654        /* allocate and init buffers */
2655        av7110->debi_virt = pci_alloc_consistent(pdev, 8192, &av7110->debi_bus);
2656        if (!av7110->debi_virt)
2657                goto err_saa71466_vfree_4;
2658
2659
2660        av7110->iobuf = vmalloc(AVOUTLEN+AOUTLEN+BMPLEN+4*IPACKS);
2661        if (!av7110->iobuf)
2662                goto err_pci_free_5;
2663
2664        ret = av7110_av_init(av7110);
2665        if (ret < 0)
2666                goto err_iobuf_vfree_6;
2667
2668        /* init BMP buffer */
2669        av7110->bmpbuf = av7110->iobuf+AVOUTLEN+AOUTLEN;
2670        init_waitqueue_head(&av7110->bmpq);
2671
2672        ret = av7110_ca_init(av7110);
2673        if (ret < 0)
2674                goto err_av7110_av_exit_7;
2675
2676        /* load firmware into AV7110 cards */
2677        ret = av7110_bootarm(av7110);
2678        if (ret < 0)
2679                goto err_av7110_ca_exit_8;
2680
2681        ret = av7110_firmversion(av7110);
2682        if (ret < 0)
2683                goto err_stop_arm_9;
2684
2685        if (FW_VERSION(av7110->arm_app)<0x2501)
2686                printk(KERN_WARNING
2687                       "dvb-ttpci: Warning, firmware version 0x%04x is too old. System might be unstable!\n",
2688                       FW_VERSION(av7110->arm_app));
2689
2690        thread = kthread_run(arm_thread, (void *) av7110, "arm_mon");
2691        if (IS_ERR(thread)) {
2692                ret = PTR_ERR(thread);
2693                goto err_stop_arm_9;
2694        }
2695        av7110->arm_thread = thread;
2696
2697        /* set initial volume in mixer struct */
2698        av7110->mixer.volume_left  = volume;
2699        av7110->mixer.volume_right = volume;
2700
2701        ret = av7110_register(av7110);
2702        if (ret < 0)
2703                goto err_arm_thread_stop_10;
2704
2705        init_av7110_av(av7110);
2706
2707        /* special case DVB-C: these cards have an analog tuner
2708           plus need some special handling, so we have separate
2709           saa7146_ext_vv data for these... */
2710        ret = av7110_init_v4l(av7110);
2711        if (ret < 0)
2712                goto err_av7110_unregister_11;
2713
2714        av7110->dvb_adapter.priv = av7110;
2715        ret = frontend_init(av7110);
2716        if (ret < 0)
2717                goto err_av7110_exit_v4l_12;
2718
2719        mutex_init(&av7110->ioctl_mutex);
2720
2721#if IS_ENABLED(CONFIG_DVB_AV7110_IR)
2722        av7110_ir_init(av7110);
2723#endif
2724        printk(KERN_INFO "dvb-ttpci: found av7110-%d.\n", av7110_num);
2725        av7110_num++;
2726out:
2727        return ret;
2728
2729err_av7110_exit_v4l_12:
2730        av7110_exit_v4l(av7110);
2731err_av7110_unregister_11:
2732        dvb_unregister(av7110);
2733err_arm_thread_stop_10:
2734        av7110_arm_sync(av7110);
2735err_stop_arm_9:
2736        /* Nothing to do. Rejoice. */
2737err_av7110_ca_exit_8:
2738        av7110_ca_exit(av7110);
2739err_av7110_av_exit_7:
2740        av7110_av_exit(av7110);
2741err_iobuf_vfree_6:
2742        vfree(av7110->iobuf);
2743err_pci_free_5:
2744        pci_free_consistent(pdev, 8192, av7110->debi_virt, av7110->debi_bus);
2745err_saa71466_vfree_4:
2746        if (av7110->grabbing)
2747                saa7146_vfree_destroy_pgtable(pdev, av7110->grabbing, &av7110->pt);
2748err_i2c_del_3:
2749        i2c_del_adapter(&av7110->i2c_adap);
2750err_dvb_unregister_adapter_2:
2751        dvb_unregister_adapter(&av7110->dvb_adapter);
2752err_put_firmware_1:
2753        put_firmware(av7110);
2754err_kfree_0:
2755        kfree(av7110);
2756        goto out;
2757}
2758
2759static int av7110_detach(struct saa7146_dev* saa)
2760{
2761        struct av7110 *av7110 = saa->ext_priv;
2762        dprintk(4, "%p\n", av7110);
2763
2764#if IS_ENABLED(CONFIG_DVB_AV7110_IR)
2765        av7110_ir_exit(av7110);
2766#endif
2767        if (budgetpatch || av7110->full_ts) {
2768                if (budgetpatch) {
2769                        /* Disable RPS1 */
2770                        saa7146_write(saa, MC1, MASK_29);
2771                        /* VSYNC LOW (inactive) */
2772                        saa7146_setgpio(saa, 3, SAA7146_GPIO_OUTLO);
2773                }
2774                saa7146_write(saa, MC1, MASK_20);       /* DMA3 off */
2775                SAA7146_IER_DISABLE(saa, MASK_10);
2776                SAA7146_ISR_CLEAR(saa, MASK_10);
2777                msleep(50);
2778                tasklet_kill(&av7110->vpe_tasklet);
2779                saa7146_vfree_destroy_pgtable(saa->pci, av7110->grabbing, &av7110->pt);
2780        }
2781        av7110_exit_v4l(av7110);
2782
2783        av7110_arm_sync(av7110);
2784
2785        tasklet_kill(&av7110->debi_tasklet);
2786        tasklet_kill(&av7110->gpio_tasklet);
2787
2788        dvb_unregister(av7110);
2789
2790        SAA7146_IER_DISABLE(saa, MASK_19 | MASK_03);
2791        SAA7146_ISR_CLEAR(saa, MASK_19 | MASK_03);
2792
2793        av7110_ca_exit(av7110);
2794        av7110_av_exit(av7110);
2795
2796        vfree(av7110->iobuf);
2797        pci_free_consistent(saa->pci, 8192, av7110->debi_virt,
2798                            av7110->debi_bus);
2799
2800        i2c_del_adapter(&av7110->i2c_adap);
2801
2802        dvb_unregister_adapter (&av7110->dvb_adapter);
2803
2804        av7110_num--;
2805
2806        put_firmware(av7110);
2807
2808        kfree(av7110);
2809
2810        saa->ext_priv = NULL;
2811
2812        return 0;
2813}
2814
2815
2816static void av7110_irq(struct saa7146_dev* dev, u32 *isr)
2817{
2818        struct av7110 *av7110 = dev->ext_priv;
2819
2820        //print_time("av7110_irq");
2821
2822        /* Note: Don't try to handle the DEBI error irq (MASK_18), in
2823         * intel mode the timeout is asserted all the time...
2824         */
2825
2826        if (*isr & MASK_19) {
2827                //printk("av7110_irq: DEBI\n");
2828                /* Note 1: The DEBI irq is level triggered: We must enable it
2829                 * only after we started a DMA xfer, and disable it here
2830                 * immediately, or it will be signalled all the time while
2831                 * DEBI is idle.
2832                 * Note 2: You would think that an irq which is masked is
2833                 * not signalled by the hardware. Not so for the SAA7146:
2834                 * An irq is signalled as long as the corresponding bit
2835                 * in the ISR is set, and disabling irqs just prevents the
2836                 * hardware from setting the ISR bit. This means a) that we
2837                 * must clear the ISR *after* disabling the irq (which is why
2838                 * we must do it here even though saa7146_core did it already),
2839                 * and b) that if we were to disable an edge triggered irq
2840                 * (like the gpio irqs sadly are) temporarily we would likely
2841                 * loose some. This sucks :-(
2842                 */
2843                SAA7146_IER_DISABLE(av7110->dev, MASK_19);
2844                SAA7146_ISR_CLEAR(av7110->dev, MASK_19);
2845                tasklet_schedule(&av7110->debi_tasklet);
2846        }
2847
2848        if (*isr & MASK_03) {
2849                //printk("av7110_irq: GPIO\n");
2850                tasklet_schedule(&av7110->gpio_tasklet);
2851        }
2852
2853        if (*isr & MASK_10)
2854                tasklet_schedule(&av7110->vpe_tasklet);
2855}
2856
2857
2858static struct saa7146_extension av7110_extension_driver;
2859
2860#define MAKE_AV7110_INFO(x_var,x_name) \
2861static struct saa7146_pci_extension_data x_var = { \
2862        .ext_priv = x_name, \
2863        .ext = &av7110_extension_driver }
2864
2865MAKE_AV7110_INFO(tts_1_X_fsc,"Technotrend/Hauppauge WinTV DVB-S rev1.X or Fujitsu Siemens DVB-C");
2866MAKE_AV7110_INFO(ttt_1_X,    "Technotrend/Hauppauge WinTV DVB-T rev1.X");
2867MAKE_AV7110_INFO(ttc_1_X,    "Technotrend/Hauppauge WinTV Nexus-CA rev1.X");
2868MAKE_AV7110_INFO(ttc_2_X,    "Technotrend/Hauppauge WinTV DVB-C rev2.X");
2869MAKE_AV7110_INFO(tts_2_X,    "Technotrend/Hauppauge WinTV Nexus-S rev2.X");
2870MAKE_AV7110_INFO(tts_2_3,    "Technotrend/Hauppauge WinTV Nexus-S rev2.3");
2871MAKE_AV7110_INFO(tts_1_3se,  "Technotrend/Hauppauge WinTV DVB-S rev1.3 SE");
2872MAKE_AV7110_INFO(ttt,        "Technotrend/Hauppauge DVB-T");
2873MAKE_AV7110_INFO(fsc,        "Fujitsu Siemens DVB-C");
2874MAKE_AV7110_INFO(fss,        "Fujitsu Siemens DVB-S rev1.6");
2875MAKE_AV7110_INFO(gxs_1_3,    "Galaxis DVB-S rev1.3");
2876
2877static const struct pci_device_id pci_tbl[] = {
2878        MAKE_EXTENSION_PCI(fsc,         0x110a, 0x0000),
2879        MAKE_EXTENSION_PCI(tts_1_X_fsc, 0x13c2, 0x0000),
2880        MAKE_EXTENSION_PCI(ttt_1_X,     0x13c2, 0x0001),
2881        MAKE_EXTENSION_PCI(ttc_2_X,     0x13c2, 0x0002),
2882        MAKE_EXTENSION_PCI(tts_2_X,     0x13c2, 0x0003),
2883        MAKE_EXTENSION_PCI(gxs_1_3,     0x13c2, 0x0004),
2884        MAKE_EXTENSION_PCI(fss,         0x13c2, 0x0006),
2885        MAKE_EXTENSION_PCI(ttt,         0x13c2, 0x0008),
2886        MAKE_EXTENSION_PCI(ttc_1_X,     0x13c2, 0x000a),
2887        MAKE_EXTENSION_PCI(tts_2_3,     0x13c2, 0x000e),
2888        MAKE_EXTENSION_PCI(tts_1_3se,   0x13c2, 0x1002),
2889
2890/*      MAKE_EXTENSION_PCI(???, 0x13c2, 0x0005), UNDEFINED CARD */ // Technisat SkyStar1
2891/*      MAKE_EXTENSION_PCI(???, 0x13c2, 0x0009), UNDEFINED CARD */ // TT/Hauppauge WinTV Nexus-CA v????
2892
2893        {
2894                .vendor    = 0,
2895        }
2896};
2897
2898MODULE_DEVICE_TABLE(pci, pci_tbl);
2899
2900
2901static struct saa7146_extension av7110_extension_driver = {
2902        .name           = "av7110",
2903        .flags          = SAA7146_USE_I2C_IRQ,
2904
2905        .module         = THIS_MODULE,
2906        .pci_tbl        = &pci_tbl[0],
2907        .attach         = av7110_attach,
2908        .detach         = av7110_detach,
2909
2910        .irq_mask       = MASK_19 | MASK_03 | MASK_10,
2911        .irq_func       = av7110_irq,
2912};
2913
2914
2915static int __init av7110_init(void)
2916{
2917        return saa7146_register_extension(&av7110_extension_driver);
2918}
2919
2920
2921static void __exit av7110_exit(void)
2922{
2923        saa7146_unregister_extension(&av7110_extension_driver);
2924}
2925
2926module_init(av7110_init);
2927module_exit(av7110_exit);
2928
2929MODULE_DESCRIPTION("driver for the SAA7146 based AV110 PCI DVB cards by Siemens, Technotrend, Hauppauge");
2930MODULE_AUTHOR("Ralph Metzler, Marcus Metzler, others");
2931MODULE_LICENSE("GPL");
2932