linux/drivers/media/video/em28xx/em28xx-dvb.c
<<
>>
Prefs
   1/*
   2 DVB device driver for em28xx
   3
   4 (c) 2008-2011 Mauro Carvalho Chehab <mchehab@infradead.org>
   5
   6 (c) 2008 Devin Heitmueller <devin.heitmueller@gmail.com>
   7        - Fixes for the driver to properly work with HVR-950
   8        - Fixes for the driver to properly work with Pinnacle PCTV HD Pro Stick
   9        - Fixes for the driver to properly work with AMD ATI TV Wonder HD 600
  10
  11 (c) 2008 Aidan Thornton <makosoft@googlemail.com>
  12
  13 Based on cx88-dvb, saa7134-dvb and videobuf-dvb originally written by:
  14        (c) 2004, 2005 Chris Pascoe <c.pascoe@itee.uq.edu.au>
  15        (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
  16
  17 This program is free software; you can redistribute it and/or modify
  18 it under the terms of the GNU General Public License as published by
  19 the Free Software Foundation; either version 2 of the License.
  20 */
  21
  22#include <linux/kernel.h>
  23#include <linux/slab.h>
  24#include <linux/usb.h>
  25
  26#include "em28xx.h"
  27#include <media/v4l2-common.h>
  28#include <media/videobuf-vmalloc.h>
  29#include <media/tuner.h>
  30#include "tuner-simple.h"
  31
  32#include "lgdt330x.h"
  33#include "lgdt3305.h"
  34#include "zl10353.h"
  35#include "s5h1409.h"
  36#include "mt352.h"
  37#include "mt352_priv.h" /* FIXME */
  38#include "tda1002x.h"
  39#include "tda18271.h"
  40#include "s921.h"
  41#include "drxd.h"
  42#include "cxd2820r.h"
  43#include "tda18271c2dd.h"
  44#include "drxk.h"
  45#include "tda10071.h"
  46#include "a8293.h"
  47#include "qt1010.h"
  48
  49MODULE_DESCRIPTION("driver for em28xx based DVB cards");
  50MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
  51MODULE_LICENSE("GPL");
  52
  53static unsigned int debug;
  54module_param(debug, int, 0644);
  55MODULE_PARM_DESC(debug, "enable debug messages [dvb]");
  56
  57DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  58
  59#define dprintk(level, fmt, arg...) do {                        \
  60if (debug >= level)                                             \
  61        printk(KERN_DEBUG "%s/2-dvb: " fmt, dev->name, ## arg); \
  62} while (0)
  63
  64struct em28xx_dvb {
  65        struct dvb_frontend        *fe[2];
  66
  67        /* feed count management */
  68        struct mutex               lock;
  69        int                        nfeeds;
  70
  71        /* general boilerplate stuff */
  72        struct dvb_adapter         adapter;
  73        struct dvb_demux           demux;
  74        struct dmxdev              dmxdev;
  75        struct dmx_frontend        fe_hw;
  76        struct dmx_frontend        fe_mem;
  77        struct dvb_net             net;
  78
  79        /* Due to DRX-K - probably need changes */
  80        int (*gate_ctrl)(struct dvb_frontend *, int);
  81        struct semaphore      pll_mutex;
  82        bool                    dont_attach_fe1;
  83};
  84
  85
  86static inline void print_err_status(struct em28xx *dev,
  87                                     int packet, int status)
  88{
  89        char *errmsg = "Unknown";
  90
  91        switch (status) {
  92        case -ENOENT:
  93                errmsg = "unlinked synchronuously";
  94                break;
  95        case -ECONNRESET:
  96                errmsg = "unlinked asynchronuously";
  97                break;
  98        case -ENOSR:
  99                errmsg = "Buffer error (overrun)";
 100                break;
 101        case -EPIPE:
 102                errmsg = "Stalled (device not responding)";
 103                break;
 104        case -EOVERFLOW:
 105                errmsg = "Babble (bad cable?)";
 106                break;
 107        case -EPROTO:
 108                errmsg = "Bit-stuff error (bad cable?)";
 109                break;
 110        case -EILSEQ:
 111                errmsg = "CRC/Timeout (could be anything)";
 112                break;
 113        case -ETIME:
 114                errmsg = "Device does not respond";
 115                break;
 116        }
 117        if (packet < 0) {
 118                dprintk(1, "URB status %d [%s].\n", status, errmsg);
 119        } else {
 120                dprintk(1, "URB packet %d, status %d [%s].\n",
 121                        packet, status, errmsg);
 122        }
 123}
 124
 125static inline int em28xx_dvb_isoc_copy(struct em28xx *dev, struct urb *urb)
 126{
 127        int i;
 128
 129        if (!dev)
 130                return 0;
 131
 132        if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
 133                return 0;
 134
 135        if (urb->status < 0) {
 136                print_err_status(dev, -1, urb->status);
 137                if (urb->status == -ENOENT)
 138                        return 0;
 139        }
 140
 141        for (i = 0; i < urb->number_of_packets; i++) {
 142                int status = urb->iso_frame_desc[i].status;
 143
 144                if (status < 0) {
 145                        print_err_status(dev, i, status);
 146                        if (urb->iso_frame_desc[i].status != -EPROTO)
 147                                continue;
 148                }
 149
 150                dvb_dmx_swfilter(&dev->dvb->demux, urb->transfer_buffer +
 151                                 urb->iso_frame_desc[i].offset,
 152                                 urb->iso_frame_desc[i].actual_length);
 153        }
 154
 155        return 0;
 156}
 157
 158static int em28xx_start_streaming(struct em28xx_dvb *dvb)
 159{
 160        int rc;
 161        struct em28xx *dev = dvb->adapter.priv;
 162        int max_dvb_packet_size;
 163
 164        usb_set_interface(dev->udev, 0, dev->dvb_alt);
 165        rc = em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
 166        if (rc < 0)
 167                return rc;
 168
 169        max_dvb_packet_size = dev->dvb_max_pkt_size;
 170        if (max_dvb_packet_size < 0)
 171                return max_dvb_packet_size;
 172        dprintk(1, "Using %d buffers each with %d x %d bytes\n",
 173                EM28XX_DVB_NUM_BUFS,
 174                EM28XX_DVB_MAX_PACKETS,
 175                max_dvb_packet_size);
 176
 177        return em28xx_init_isoc(dev, EM28XX_DIGITAL_MODE,
 178                                EM28XX_DVB_MAX_PACKETS, EM28XX_DVB_NUM_BUFS,
 179                                max_dvb_packet_size, em28xx_dvb_isoc_copy);
 180}
 181
 182static int em28xx_stop_streaming(struct em28xx_dvb *dvb)
 183{
 184        struct em28xx *dev = dvb->adapter.priv;
 185
 186        em28xx_capture_start(dev, 0);
 187
 188        em28xx_set_mode(dev, EM28XX_SUSPEND);
 189
 190        return 0;
 191}
 192
 193static int em28xx_start_feed(struct dvb_demux_feed *feed)
 194{
 195        struct dvb_demux *demux  = feed->demux;
 196        struct em28xx_dvb *dvb = demux->priv;
 197        int rc, ret;
 198
 199        if (!demux->dmx.frontend)
 200                return -EINVAL;
 201
 202        mutex_lock(&dvb->lock);
 203        dvb->nfeeds++;
 204        rc = dvb->nfeeds;
 205
 206        if (dvb->nfeeds == 1) {
 207                ret = em28xx_start_streaming(dvb);
 208                if (ret < 0)
 209                        rc = ret;
 210        }
 211
 212        mutex_unlock(&dvb->lock);
 213        return rc;
 214}
 215
 216static int em28xx_stop_feed(struct dvb_demux_feed *feed)
 217{
 218        struct dvb_demux *demux  = feed->demux;
 219        struct em28xx_dvb *dvb = demux->priv;
 220        int err = 0;
 221
 222        mutex_lock(&dvb->lock);
 223        dvb->nfeeds--;
 224
 225        if (0 == dvb->nfeeds)
 226                err = em28xx_stop_streaming(dvb);
 227
 228        mutex_unlock(&dvb->lock);
 229        return err;
 230}
 231
 232
 233
 234/* ------------------------------------------------------------------ */
 235static int em28xx_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
 236{
 237        struct em28xx *dev = fe->dvb->priv;
 238
 239        if (acquire)
 240                return em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
 241        else
 242                return em28xx_set_mode(dev, EM28XX_SUSPEND);
 243}
 244
 245/* ------------------------------------------------------------------ */
 246
 247static struct lgdt330x_config em2880_lgdt3303_dev = {
 248        .demod_address = 0x0e,
 249        .demod_chip = LGDT3303,
 250};
 251
 252static struct lgdt3305_config em2870_lgdt3304_dev = {
 253        .i2c_addr           = 0x0e,
 254        .demod_chip         = LGDT3304,
 255        .spectral_inversion = 1,
 256        .deny_i2c_rptr      = 1,
 257        .mpeg_mode          = LGDT3305_MPEG_PARALLEL,
 258        .tpclk_edge         = LGDT3305_TPCLK_FALLING_EDGE,
 259        .tpvalid_polarity   = LGDT3305_TP_VALID_HIGH,
 260        .vsb_if_khz         = 3250,
 261        .qam_if_khz         = 4000,
 262};
 263
 264static struct s921_config sharp_isdbt = {
 265        .demod_address = 0x30 >> 1
 266};
 267
 268static struct zl10353_config em28xx_zl10353_with_xc3028 = {
 269        .demod_address = (0x1e >> 1),
 270        .no_tuner = 1,
 271        .parallel_ts = 1,
 272        .if2 = 45600,
 273};
 274
 275static struct s5h1409_config em28xx_s5h1409_with_xc3028 = {
 276        .demod_address = 0x32 >> 1,
 277        .output_mode   = S5H1409_PARALLEL_OUTPUT,
 278        .gpio          = S5H1409_GPIO_OFF,
 279        .inversion     = S5H1409_INVERSION_OFF,
 280        .status_mode   = S5H1409_DEMODLOCKING,
 281        .mpeg_timing   = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK
 282};
 283
 284static struct tda18271_std_map kworld_a340_std_map = {
 285        .atsc_6   = { .if_freq = 3250, .agc_mode = 3, .std = 0,
 286                      .if_lvl = 1, .rfagc_top = 0x37, },
 287        .qam_6    = { .if_freq = 4000, .agc_mode = 3, .std = 1,
 288                      .if_lvl = 1, .rfagc_top = 0x37, },
 289};
 290
 291static struct tda18271_config kworld_a340_config = {
 292        .std_map           = &kworld_a340_std_map,
 293};
 294
 295static struct zl10353_config em28xx_zl10353_xc3028_no_i2c_gate = {
 296        .demod_address = (0x1e >> 1),
 297        .no_tuner = 1,
 298        .disable_i2c_gate_ctrl = 1,
 299        .parallel_ts = 1,
 300        .if2 = 45600,
 301};
 302
 303static struct drxd_config em28xx_drxd = {
 304        .demod_address = 0x70,
 305        .demod_revision = 0xa2,
 306        .pll_type = DRXD_PLL_NONE,
 307        .clock = 12000,
 308        .insert_rs_byte = 1,
 309        .IF = 42800000,
 310        .disable_i2c_gate_ctrl = 1,
 311};
 312
 313struct drxk_config terratec_h5_drxk = {
 314        .adr = 0x29,
 315        .single_master = 1,
 316        .no_i2c_bridge = 1,
 317        .microcode_name = "dvb-usb-terratec-h5-drxk.fw",
 318};
 319
 320struct drxk_config hauppauge_930c_drxk = {
 321        .adr = 0x29,
 322        .single_master = 1,
 323        .no_i2c_bridge = 1,
 324        .microcode_name = "dvb-usb-hauppauge-hvr930c-drxk.fw",
 325        .chunk_size = 56,
 326};
 327
 328struct drxk_config maxmedia_ub425_tc_drxk = {
 329        .adr = 0x29,
 330        .single_master = 1,
 331        .no_i2c_bridge = 1,
 332};
 333
 334struct drxk_config pctv_520e_drxk = {
 335        .adr = 0x29,
 336        .single_master = 1,
 337        .microcode_name = "dvb-demod-drxk-pctv.fw",
 338        .chunk_size = 58,
 339};
 340
 341static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
 342{
 343        struct em28xx_dvb *dvb = fe->sec_priv;
 344        int status;
 345
 346        if (!dvb)
 347                return -EINVAL;
 348
 349        if (enable) {
 350                down(&dvb->pll_mutex);
 351                status = dvb->gate_ctrl(fe, 1);
 352        } else {
 353                status = dvb->gate_ctrl(fe, 0);
 354                up(&dvb->pll_mutex);
 355        }
 356        return status;
 357}
 358
 359static void hauppauge_hvr930c_init(struct em28xx *dev)
 360{
 361        int i;
 362
 363        struct em28xx_reg_seq hauppauge_hvr930c_init[] = {
 364                {EM2874_R80_GPIO,       0xff,   0xff,   0x65},
 365                {EM2874_R80_GPIO,       0xfb,   0xff,   0x32},
 366                {EM2874_R80_GPIO,       0xff,   0xff,   0xb8},
 367                { -1,                   -1,     -1,     -1},
 368        };
 369        struct em28xx_reg_seq hauppauge_hvr930c_end[] = {
 370                {EM2874_R80_GPIO,       0xef,   0xff,   0x01},
 371                {EM2874_R80_GPIO,       0xaf,   0xff,   0x65},
 372                {EM2874_R80_GPIO,       0xef,   0xff,   0x76},
 373                {EM2874_R80_GPIO,       0xef,   0xff,   0x01},
 374                {EM2874_R80_GPIO,       0xcf,   0xff,   0x0b},
 375                {EM2874_R80_GPIO,       0xef,   0xff,   0x40},
 376
 377                {EM2874_R80_GPIO,       0xcf,   0xff,   0x65},
 378                {EM2874_R80_GPIO,       0xef,   0xff,   0x65},
 379                {EM2874_R80_GPIO,       0xcf,   0xff,   0x0b},
 380                {EM2874_R80_GPIO,       0xef,   0xff,   0x65},
 381
 382                { -1,                   -1,     -1,     -1},
 383        };
 384
 385        struct {
 386                unsigned char r[4];
 387                int len;
 388        } regs[] = {
 389                {{ 0x06, 0x02, 0x00, 0x31 }, 4},
 390                {{ 0x01, 0x02 }, 2},
 391                {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
 392                {{ 0x01, 0x00 }, 2},
 393                {{ 0x01, 0x00, 0xff, 0xaf }, 4},
 394                {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
 395                {{ 0x01, 0x00 }, 2},
 396                {{ 0x01, 0x00, 0x73, 0xaf }, 4},
 397                {{ 0x04, 0x00 }, 2},
 398                {{ 0x00, 0x04 }, 2},
 399                {{ 0x00, 0x04, 0x00, 0x0a }, 4},
 400                {{ 0x04, 0x14 }, 2},
 401                {{ 0x04, 0x14, 0x00, 0x00 }, 4},
 402        };
 403
 404        em28xx_gpio_set(dev, hauppauge_hvr930c_init);
 405        em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
 406        msleep(10);
 407        em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
 408        msleep(10);
 409
 410        dev->i2c_client.addr = 0x82 >> 1;
 411
 412        for (i = 0; i < ARRAY_SIZE(regs); i++)
 413                i2c_master_send(&dev->i2c_client, regs[i].r, regs[i].len);
 414        em28xx_gpio_set(dev, hauppauge_hvr930c_end);
 415
 416        msleep(100);
 417
 418        em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
 419        msleep(30);
 420
 421        em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x45);
 422        msleep(10);
 423
 424}
 425
 426static void terratec_h5_init(struct em28xx *dev)
 427{
 428        int i;
 429        struct em28xx_reg_seq terratec_h5_init[] = {
 430                {EM28XX_R08_GPIO,       0xff,   0xff,   10},
 431                {EM2874_R80_GPIO,       0xf6,   0xff,   100},
 432                {EM2874_R80_GPIO,       0xf2,   0xff,   50},
 433                {EM2874_R80_GPIO,       0xf6,   0xff,   100},
 434                { -1,                   -1,     -1,     -1},
 435        };
 436        struct em28xx_reg_seq terratec_h5_end[] = {
 437                {EM2874_R80_GPIO,       0xe6,   0xff,   100},
 438                {EM2874_R80_GPIO,       0xa6,   0xff,   50},
 439                {EM2874_R80_GPIO,       0xe6,   0xff,   100},
 440                { -1,                   -1,     -1,     -1},
 441        };
 442        struct {
 443                unsigned char r[4];
 444                int len;
 445        } regs[] = {
 446                {{ 0x06, 0x02, 0x00, 0x31 }, 4},
 447                {{ 0x01, 0x02 }, 2},
 448                {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
 449                {{ 0x01, 0x00 }, 2},
 450                {{ 0x01, 0x00, 0xff, 0xaf }, 4},
 451                {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
 452                {{ 0x01, 0x00 }, 2},
 453                {{ 0x01, 0x00, 0x73, 0xaf }, 4},
 454                {{ 0x04, 0x00 }, 2},
 455                {{ 0x00, 0x04 }, 2},
 456                {{ 0x00, 0x04, 0x00, 0x0a }, 4},
 457                {{ 0x04, 0x14 }, 2},
 458                {{ 0x04, 0x14, 0x00, 0x00 }, 4},
 459        };
 460
 461        em28xx_gpio_set(dev, terratec_h5_init);
 462        em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
 463        msleep(10);
 464        em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x45);
 465        msleep(10);
 466
 467        dev->i2c_client.addr = 0x82 >> 1;
 468
 469        for (i = 0; i < ARRAY_SIZE(regs); i++)
 470                i2c_master_send(&dev->i2c_client, regs[i].r, regs[i].len);
 471        em28xx_gpio_set(dev, terratec_h5_end);
 472};
 473
 474static void pctv_520e_init(struct em28xx *dev)
 475{
 476        /*
 477         * Init TDA8295(?) analog demodulator. Looks like I2C traffic to
 478         * digital demodulator and tuner are routed via TDA8295.
 479         */
 480        int i;
 481        struct {
 482                unsigned char r[4];
 483                int len;
 484        } regs[] = {
 485                {{ 0x06, 0x02, 0x00, 0x31 }, 4},
 486                {{ 0x01, 0x02 }, 2},
 487                {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
 488                {{ 0x01, 0x00 }, 2},
 489                {{ 0x01, 0x00, 0xff, 0xaf }, 4},
 490                {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
 491                {{ 0x01, 0x00 }, 2},
 492                {{ 0x01, 0x00, 0x73, 0xaf }, 4},
 493        };
 494
 495        dev->i2c_client.addr = 0x82 >> 1; /* 0x41 */
 496
 497        for (i = 0; i < ARRAY_SIZE(regs); i++)
 498                i2c_master_send(&dev->i2c_client, regs[i].r, regs[i].len);
 499};
 500
 501static int em28xx_mt352_terratec_xs_init(struct dvb_frontend *fe)
 502{
 503        /* Values extracted from a USB trace of the Terratec Windows driver */
 504        static u8 clock_config[]   = { CLOCK_CTL,  0x38, 0x2c };
 505        static u8 reset[]          = { RESET,      0x80 };
 506        static u8 adc_ctl_1_cfg[]  = { ADC_CTL_1,  0x40 };
 507        static u8 agc_cfg[]        = { AGC_TARGET, 0x28, 0xa0 };
 508        static u8 input_freq_cfg[] = { INPUT_FREQ_1, 0x31, 0xb8 };
 509        static u8 rs_err_cfg[]     = { RS_ERR_PER_1, 0x00, 0x4d };
 510        static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
 511        static u8 trl_nom_cfg[]    = { TRL_NOMINAL_RATE_1, 0x64, 0x00 };
 512        static u8 tps_given_cfg[]  = { TPS_GIVEN_1, 0x40, 0x80, 0x50 };
 513        static u8 tuner_go[]       = { TUNER_GO, 0x01};
 514
 515        mt352_write(fe, clock_config,   sizeof(clock_config));
 516        udelay(200);
 517        mt352_write(fe, reset,          sizeof(reset));
 518        mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
 519        mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
 520        mt352_write(fe, input_freq_cfg, sizeof(input_freq_cfg));
 521        mt352_write(fe, rs_err_cfg,     sizeof(rs_err_cfg));
 522        mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
 523        mt352_write(fe, trl_nom_cfg,    sizeof(trl_nom_cfg));
 524        mt352_write(fe, tps_given_cfg,  sizeof(tps_given_cfg));
 525        mt352_write(fe, tuner_go,       sizeof(tuner_go));
 526        return 0;
 527}
 528
 529static struct mt352_config terratec_xs_mt352_cfg = {
 530        .demod_address = (0x1e >> 1),
 531        .no_tuner = 1,
 532        .if2 = 45600,
 533        .demod_init = em28xx_mt352_terratec_xs_init,
 534};
 535
 536static struct tda10023_config em28xx_tda10023_config = {
 537        .demod_address = 0x0c,
 538        .invert = 1,
 539};
 540
 541static struct cxd2820r_config em28xx_cxd2820r_config = {
 542        .i2c_address = (0xd8 >> 1),
 543        .ts_mode = CXD2820R_TS_SERIAL,
 544
 545        /* enable LNA for DVB-T2 and DVB-C */
 546        .gpio_dvbt2[0] = CXD2820R_GPIO_E | CXD2820R_GPIO_O | CXD2820R_GPIO_L,
 547        .gpio_dvbc[0] = CXD2820R_GPIO_E | CXD2820R_GPIO_O | CXD2820R_GPIO_L,
 548};
 549
 550static struct tda18271_config em28xx_cxd2820r_tda18271_config = {
 551        .output_opt = TDA18271_OUTPUT_LT_OFF,
 552        .gate = TDA18271_GATE_DIGITAL,
 553};
 554
 555static const struct tda10071_config em28xx_tda10071_config = {
 556        .i2c_address = 0x55, /* (0xaa >> 1) */
 557        .i2c_wr_max = 64,
 558        .ts_mode = TDA10071_TS_SERIAL,
 559        .spec_inv = 0,
 560        .xtal = 40444000, /* 40.444 MHz */
 561        .pll_multiplier = 20,
 562};
 563
 564static const struct a8293_config em28xx_a8293_config = {
 565        .i2c_addr = 0x08, /* (0x10 >> 1) */
 566};
 567
 568static struct zl10353_config em28xx_zl10353_no_i2c_gate_dev = {
 569        .demod_address = (0x1e >> 1),
 570        .disable_i2c_gate_ctrl = 1,
 571        .no_tuner = 1,
 572        .parallel_ts = 1,
 573};
 574static struct qt1010_config em28xx_qt1010_config = {
 575        .i2c_address = 0x62
 576
 577};
 578
 579/* ------------------------------------------------------------------ */
 580
 581static int em28xx_attach_xc3028(u8 addr, struct em28xx *dev)
 582{
 583        struct dvb_frontend *fe;
 584        struct xc2028_config cfg;
 585
 586        memset(&cfg, 0, sizeof(cfg));
 587        cfg.i2c_adap  = &dev->i2c_adap;
 588        cfg.i2c_addr  = addr;
 589
 590        if (!dev->dvb->fe[0]) {
 591                em28xx_errdev("/2: dvb frontend not attached. "
 592                                "Can't attach xc3028\n");
 593                return -EINVAL;
 594        }
 595
 596        fe = dvb_attach(xc2028_attach, dev->dvb->fe[0], &cfg);
 597        if (!fe) {
 598                em28xx_errdev("/2: xc3028 attach failed\n");
 599                dvb_frontend_detach(dev->dvb->fe[0]);
 600                dev->dvb->fe[0] = NULL;
 601                return -EINVAL;
 602        }
 603
 604        em28xx_info("%s/2: xc3028 attached\n", dev->name);
 605
 606        return 0;
 607}
 608
 609/* ------------------------------------------------------------------ */
 610
 611static int em28xx_register_dvb(struct em28xx_dvb *dvb, struct module *module,
 612                               struct em28xx *dev, struct device *device)
 613{
 614        int result;
 615
 616        mutex_init(&dvb->lock);
 617
 618        /* register adapter */
 619        result = dvb_register_adapter(&dvb->adapter, dev->name, module, device,
 620                                      adapter_nr);
 621        if (result < 0) {
 622                printk(KERN_WARNING "%s: dvb_register_adapter failed (errno = %d)\n",
 623                       dev->name, result);
 624                goto fail_adapter;
 625        }
 626
 627        /* Ensure all frontends negotiate bus access */
 628        dvb->fe[0]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
 629        if (dvb->fe[1])
 630                dvb->fe[1]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
 631
 632        dvb->adapter.priv = dev;
 633
 634        /* register frontend */
 635        result = dvb_register_frontend(&dvb->adapter, dvb->fe[0]);
 636        if (result < 0) {
 637                printk(KERN_WARNING "%s: dvb_register_frontend failed (errno = %d)\n",
 638                       dev->name, result);
 639                goto fail_frontend0;
 640        }
 641
 642        /* register 2nd frontend */
 643        if (dvb->fe[1]) {
 644                result = dvb_register_frontend(&dvb->adapter, dvb->fe[1]);
 645                if (result < 0) {
 646                        printk(KERN_WARNING "%s: 2nd dvb_register_frontend failed (errno = %d)\n",
 647                                dev->name, result);
 648                        goto fail_frontend1;
 649                }
 650        }
 651
 652        /* register demux stuff */
 653        dvb->demux.dmx.capabilities =
 654                DMX_TS_FILTERING | DMX_SECTION_FILTERING |
 655                DMX_MEMORY_BASED_FILTERING;
 656        dvb->demux.priv       = dvb;
 657        dvb->demux.filternum  = 256;
 658        dvb->demux.feednum    = 256;
 659        dvb->demux.start_feed = em28xx_start_feed;
 660        dvb->demux.stop_feed  = em28xx_stop_feed;
 661
 662        result = dvb_dmx_init(&dvb->demux);
 663        if (result < 0) {
 664                printk(KERN_WARNING "%s: dvb_dmx_init failed (errno = %d)\n",
 665                       dev->name, result);
 666                goto fail_dmx;
 667        }
 668
 669        dvb->dmxdev.filternum    = 256;
 670        dvb->dmxdev.demux        = &dvb->demux.dmx;
 671        dvb->dmxdev.capabilities = 0;
 672        result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
 673        if (result < 0) {
 674                printk(KERN_WARNING "%s: dvb_dmxdev_init failed (errno = %d)\n",
 675                       dev->name, result);
 676                goto fail_dmxdev;
 677        }
 678
 679        dvb->fe_hw.source = DMX_FRONTEND_0;
 680        result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
 681        if (result < 0) {
 682                printk(KERN_WARNING "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
 683                       dev->name, result);
 684                goto fail_fe_hw;
 685        }
 686
 687        dvb->fe_mem.source = DMX_MEMORY_FE;
 688        result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
 689        if (result < 0) {
 690                printk(KERN_WARNING "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
 691                       dev->name, result);
 692                goto fail_fe_mem;
 693        }
 694
 695        result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
 696        if (result < 0) {
 697                printk(KERN_WARNING "%s: connect_frontend failed (errno = %d)\n",
 698                       dev->name, result);
 699                goto fail_fe_conn;
 700        }
 701
 702        /* register network adapter */
 703        dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
 704        return 0;
 705
 706fail_fe_conn:
 707        dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
 708fail_fe_mem:
 709        dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
 710fail_fe_hw:
 711        dvb_dmxdev_release(&dvb->dmxdev);
 712fail_dmxdev:
 713        dvb_dmx_release(&dvb->demux);
 714fail_dmx:
 715        if (dvb->fe[1])
 716                dvb_unregister_frontend(dvb->fe[1]);
 717        dvb_unregister_frontend(dvb->fe[0]);
 718fail_frontend1:
 719        if (dvb->fe[1])
 720                dvb_frontend_detach(dvb->fe[1]);
 721fail_frontend0:
 722        dvb_frontend_detach(dvb->fe[0]);
 723        dvb_unregister_adapter(&dvb->adapter);
 724fail_adapter:
 725        return result;
 726}
 727
 728static void em28xx_unregister_dvb(struct em28xx_dvb *dvb)
 729{
 730        dvb_net_release(&dvb->net);
 731        dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
 732        dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
 733        dvb_dmxdev_release(&dvb->dmxdev);
 734        dvb_dmx_release(&dvb->demux);
 735        if (dvb->fe[1])
 736                dvb_unregister_frontend(dvb->fe[1]);
 737        dvb_unregister_frontend(dvb->fe[0]);
 738        if (dvb->fe[1] && !dvb->dont_attach_fe1)
 739                dvb_frontend_detach(dvb->fe[1]);
 740        dvb_frontend_detach(dvb->fe[0]);
 741        dvb_unregister_adapter(&dvb->adapter);
 742}
 743
 744static int em28xx_dvb_init(struct em28xx *dev)
 745{
 746        int result = 0, mfe_shared = 0;
 747        struct em28xx_dvb *dvb;
 748
 749        if (!dev->board.has_dvb) {
 750                /* This device does not support the extension */
 751                printk(KERN_INFO "em28xx_dvb: This device does not support the extension\n");
 752                return 0;
 753        }
 754
 755        dvb = kzalloc(sizeof(struct em28xx_dvb), GFP_KERNEL);
 756
 757        if (dvb == NULL) {
 758                em28xx_info("em28xx_dvb: memory allocation failed\n");
 759                return -ENOMEM;
 760        }
 761        dev->dvb = dvb;
 762        dvb->fe[0] = dvb->fe[1] = NULL;
 763
 764        mutex_lock(&dev->lock);
 765        em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
 766        /* init frontend */
 767        switch (dev->model) {
 768        case EM2874_BOARD_LEADERSHIP_ISDBT:
 769                dvb->fe[0] = dvb_attach(s921_attach,
 770                                &sharp_isdbt, &dev->i2c_adap);
 771
 772                if (!dvb->fe[0]) {
 773                        result = -EINVAL;
 774                        goto out_free;
 775                }
 776
 777                break;
 778        case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_850:
 779        case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_950:
 780        case EM2880_BOARD_PINNACLE_PCTV_HD_PRO:
 781        case EM2880_BOARD_AMD_ATI_TV_WONDER_HD_600:
 782                dvb->fe[0] = dvb_attach(lgdt330x_attach,
 783                                           &em2880_lgdt3303_dev,
 784                                           &dev->i2c_adap);
 785                if (em28xx_attach_xc3028(0x61, dev) < 0) {
 786                        result = -EINVAL;
 787                        goto out_free;
 788                }
 789                break;
 790        case EM2880_BOARD_KWORLD_DVB_310U:
 791                dvb->fe[0] = dvb_attach(zl10353_attach,
 792                                           &em28xx_zl10353_with_xc3028,
 793                                           &dev->i2c_adap);
 794                if (em28xx_attach_xc3028(0x61, dev) < 0) {
 795                        result = -EINVAL;
 796                        goto out_free;
 797                }
 798                break;
 799        case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900:
 800        case EM2882_BOARD_TERRATEC_HYBRID_XS:
 801        case EM2880_BOARD_EMPIRE_DUAL_TV:
 802                dvb->fe[0] = dvb_attach(zl10353_attach,
 803                                           &em28xx_zl10353_xc3028_no_i2c_gate,
 804                                           &dev->i2c_adap);
 805                if (em28xx_attach_xc3028(0x61, dev) < 0) {
 806                        result = -EINVAL;
 807                        goto out_free;
 808                }
 809                break;
 810        case EM2880_BOARD_TERRATEC_HYBRID_XS:
 811        case EM2880_BOARD_TERRATEC_HYBRID_XS_FR:
 812        case EM2881_BOARD_PINNACLE_HYBRID_PRO:
 813        case EM2882_BOARD_DIKOM_DK300:
 814        case EM2882_BOARD_KWORLD_VS_DVBT:
 815                dvb->fe[0] = dvb_attach(zl10353_attach,
 816                                           &em28xx_zl10353_xc3028_no_i2c_gate,
 817                                           &dev->i2c_adap);
 818                if (dvb->fe[0] == NULL) {
 819                        /* This board could have either a zl10353 or a mt352.
 820                           If the chip id isn't for zl10353, try mt352 */
 821                        dvb->fe[0] = dvb_attach(mt352_attach,
 822                                                   &terratec_xs_mt352_cfg,
 823                                                   &dev->i2c_adap);
 824                }
 825
 826                if (em28xx_attach_xc3028(0x61, dev) < 0) {
 827                        result = -EINVAL;
 828                        goto out_free;
 829                }
 830                break;
 831        case EM2870_BOARD_KWORLD_355U:
 832                dvb->fe[0] = dvb_attach(zl10353_attach,
 833                                           &em28xx_zl10353_no_i2c_gate_dev,
 834                                           &dev->i2c_adap);
 835                if (dvb->fe[0] != NULL)
 836                        dvb_attach(qt1010_attach, dvb->fe[0],
 837                                   &dev->i2c_adap, &em28xx_qt1010_config);
 838                break;
 839        case EM2883_BOARD_KWORLD_HYBRID_330U:
 840        case EM2882_BOARD_EVGA_INDTUBE:
 841                dvb->fe[0] = dvb_attach(s5h1409_attach,
 842                                           &em28xx_s5h1409_with_xc3028,
 843                                           &dev->i2c_adap);
 844                if (em28xx_attach_xc3028(0x61, dev) < 0) {
 845                        result = -EINVAL;
 846                        goto out_free;
 847                }
 848                break;
 849        case EM2882_BOARD_KWORLD_ATSC_315U:
 850                dvb->fe[0] = dvb_attach(lgdt330x_attach,
 851                                           &em2880_lgdt3303_dev,
 852                                           &dev->i2c_adap);
 853                if (dvb->fe[0] != NULL) {
 854                        if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
 855                                &dev->i2c_adap, 0x61, TUNER_THOMSON_DTT761X)) {
 856                                result = -EINVAL;
 857                                goto out_free;
 858                        }
 859                }
 860                break;
 861        case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900_R2:
 862        case EM2882_BOARD_PINNACLE_HYBRID_PRO_330E:
 863                dvb->fe[0] = dvb_attach(drxd_attach, &em28xx_drxd, NULL,
 864                                           &dev->i2c_adap, &dev->udev->dev);
 865                if (em28xx_attach_xc3028(0x61, dev) < 0) {
 866                        result = -EINVAL;
 867                        goto out_free;
 868                }
 869                break;
 870        case EM2870_BOARD_REDDO_DVB_C_USB_BOX:
 871                /* Philips CU1216L NIM (Philips TDA10023 + Infineon TUA6034) */
 872                dvb->fe[0] = dvb_attach(tda10023_attach,
 873                        &em28xx_tda10023_config,
 874                        &dev->i2c_adap, 0x48);
 875                if (dvb->fe[0]) {
 876                        if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
 877                                &dev->i2c_adap, 0x60, TUNER_PHILIPS_CU1216L)) {
 878                                result = -EINVAL;
 879                                goto out_free;
 880                        }
 881                }
 882                break;
 883        case EM2870_BOARD_KWORLD_A340:
 884                dvb->fe[0] = dvb_attach(lgdt3305_attach,
 885                                           &em2870_lgdt3304_dev,
 886                                           &dev->i2c_adap);
 887                if (dvb->fe[0] != NULL)
 888                        dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
 889                                   &dev->i2c_adap, &kworld_a340_config);
 890                break;
 891        case EM28174_BOARD_PCTV_290E:
 892                dvb->fe[0] = dvb_attach(cxd2820r_attach,
 893                                        &em28xx_cxd2820r_config,
 894                                        &dev->i2c_adap);
 895                if (dvb->fe[0]) {
 896                        /* FE 0 attach tuner */
 897                        if (!dvb_attach(tda18271_attach,
 898                                        dvb->fe[0],
 899                                        0x60,
 900                                        &dev->i2c_adap,
 901                                        &em28xx_cxd2820r_tda18271_config)) {
 902
 903                                dvb_frontend_detach(dvb->fe[0]);
 904                                result = -EINVAL;
 905                                goto out_free;
 906                        }
 907                }
 908                break;
 909        case EM2884_BOARD_HAUPPAUGE_WINTV_HVR_930C:
 910        {
 911                struct xc5000_config cfg;
 912                hauppauge_hvr930c_init(dev);
 913
 914                dvb->fe[0] = dvb_attach(drxk_attach,
 915                                        &hauppauge_930c_drxk, &dev->i2c_adap);
 916                if (!dvb->fe[0]) {
 917                        result = -EINVAL;
 918                        goto out_free;
 919                }
 920                /* FIXME: do we need a pll semaphore? */
 921                dvb->fe[0]->sec_priv = dvb;
 922                sema_init(&dvb->pll_mutex, 1);
 923                dvb->gate_ctrl = dvb->fe[0]->ops.i2c_gate_ctrl;
 924                dvb->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
 925
 926                /* Attach xc5000 */
 927                memset(&cfg, 0, sizeof(cfg));
 928                cfg.i2c_address  = 0x61;
 929                cfg.if_khz = 4000;
 930
 931                if (dvb->fe[0]->ops.i2c_gate_ctrl)
 932                        dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 1);
 933                if (!dvb_attach(xc5000_attach, dvb->fe[0], &dev->i2c_adap,
 934                                &cfg)) {
 935                        result = -EINVAL;
 936                        goto out_free;
 937                }
 938                if (dvb->fe[0]->ops.i2c_gate_ctrl)
 939                        dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 0);
 940
 941                break;
 942        }
 943        case EM2884_BOARD_TERRATEC_H5:
 944        case EM2884_BOARD_CINERGY_HTC_STICK:
 945                terratec_h5_init(dev);
 946
 947                dvb->fe[0] = dvb_attach(drxk_attach, &terratec_h5_drxk, &dev->i2c_adap);
 948                if (!dvb->fe[0]) {
 949                        result = -EINVAL;
 950                        goto out_free;
 951                }
 952                /* FIXME: do we need a pll semaphore? */
 953                dvb->fe[0]->sec_priv = dvb;
 954                sema_init(&dvb->pll_mutex, 1);
 955                dvb->gate_ctrl = dvb->fe[0]->ops.i2c_gate_ctrl;
 956                dvb->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
 957
 958                /* Attach tda18271 to DVB-C frontend */
 959                if (dvb->fe[0]->ops.i2c_gate_ctrl)
 960                        dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 1);
 961                if (!dvb_attach(tda18271c2dd_attach, dvb->fe[0], &dev->i2c_adap, 0x60)) {
 962                        result = -EINVAL;
 963                        goto out_free;
 964                }
 965                if (dvb->fe[0]->ops.i2c_gate_ctrl)
 966                        dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 0);
 967
 968                break;
 969        case EM28174_BOARD_PCTV_460E:
 970                /* attach demod */
 971                dvb->fe[0] = dvb_attach(tda10071_attach,
 972                        &em28xx_tda10071_config, &dev->i2c_adap);
 973
 974                /* attach SEC */
 975                if (dvb->fe[0])
 976                        dvb_attach(a8293_attach, dvb->fe[0], &dev->i2c_adap,
 977                                &em28xx_a8293_config);
 978                break;
 979        case EM2874_BOARD_MAXMEDIA_UB425_TC:
 980                /* attach demodulator */
 981                dvb->fe[0] = dvb_attach(drxk_attach, &maxmedia_ub425_tc_drxk,
 982                                &dev->i2c_adap);
 983
 984                if (dvb->fe[0]) {
 985                        /* disable I2C-gate */
 986                        dvb->fe[0]->ops.i2c_gate_ctrl = NULL;
 987
 988                        /* attach tuner */
 989                        if (!dvb_attach(tda18271c2dd_attach, dvb->fe[0],
 990                                        &dev->i2c_adap, 0x60)) {
 991                                dvb_frontend_detach(dvb->fe[0]);
 992                                result = -EINVAL;
 993                                goto out_free;
 994                        }
 995                }
 996
 997                /* TODO: we need drx-3913k firmware in order to support DVB-T */
 998                em28xx_info("MaxMedia UB425-TC: only DVB-C supported by that " \
 999                                "driver version\n");
1000
1001                break;
1002        case EM2884_BOARD_PCTV_510E:
1003        case EM2884_BOARD_PCTV_520E:
1004                pctv_520e_init(dev);
1005
1006                /* attach demodulator */
1007                dvb->fe[0] = dvb_attach(drxk_attach, &pctv_520e_drxk,
1008                                &dev->i2c_adap);
1009
1010                if (dvb->fe[0]) {
1011                        /* attach tuner */
1012                        if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1013                                        &dev->i2c_adap,
1014                                        &em28xx_cxd2820r_tda18271_config)) {
1015                                dvb_frontend_detach(dvb->fe[0]);
1016                                result = -EINVAL;
1017                                goto out_free;
1018                        }
1019                }
1020                break;
1021        default:
1022                em28xx_errdev("/2: The frontend of your DVB/ATSC card"
1023                                " isn't supported yet\n");
1024                break;
1025        }
1026        if (NULL == dvb->fe[0]) {
1027                em28xx_errdev("/2: frontend initialization failed\n");
1028                result = -EINVAL;
1029                goto out_free;
1030        }
1031        /* define general-purpose callback pointer */
1032        dvb->fe[0]->callback = em28xx_tuner_callback;
1033        if (dvb->fe[1])
1034                dvb->fe[1]->callback = em28xx_tuner_callback;
1035
1036        /* register everything */
1037        result = em28xx_register_dvb(dvb, THIS_MODULE, dev, &dev->udev->dev);
1038
1039        if (result < 0)
1040                goto out_free;
1041
1042        /* MFE lock */
1043        dvb->adapter.mfe_shared = mfe_shared;
1044
1045        em28xx_info("Successfully loaded em28xx-dvb\n");
1046ret:
1047        em28xx_set_mode(dev, EM28XX_SUSPEND);
1048        mutex_unlock(&dev->lock);
1049        return result;
1050
1051out_free:
1052        kfree(dvb);
1053        dev->dvb = NULL;
1054        goto ret;
1055}
1056
1057static inline void prevent_sleep(struct dvb_frontend_ops *ops)
1058{
1059        ops->set_voltage = NULL;
1060        ops->sleep = NULL;
1061        ops->tuner_ops.sleep = NULL;
1062}
1063
1064static int em28xx_dvb_fini(struct em28xx *dev)
1065{
1066        if (!dev->board.has_dvb) {
1067                /* This device does not support the extension */
1068                return 0;
1069        }
1070
1071        if (dev->dvb) {
1072                struct em28xx_dvb *dvb = dev->dvb;
1073
1074                if (dev->state & DEV_DISCONNECTED) {
1075                        /* We cannot tell the device to sleep
1076                         * once it has been unplugged. */
1077                        if (dvb->fe[0])
1078                                prevent_sleep(&dvb->fe[0]->ops);
1079                        if (dvb->fe[1])
1080                                prevent_sleep(&dvb->fe[1]->ops);
1081                }
1082
1083                em28xx_unregister_dvb(dvb);
1084                kfree(dvb);
1085                dev->dvb = NULL;
1086        }
1087
1088        return 0;
1089}
1090
1091static struct em28xx_ops dvb_ops = {
1092        .id   = EM28XX_DVB,
1093        .name = "Em28xx dvb Extension",
1094        .init = em28xx_dvb_init,
1095        .fini = em28xx_dvb_fini,
1096};
1097
1098static int __init em28xx_dvb_register(void)
1099{
1100        return em28xx_register_extension(&dvb_ops);
1101}
1102
1103static void __exit em28xx_dvb_unregister(void)
1104{
1105        em28xx_unregister_extension(&dvb_ops);
1106}
1107
1108module_init(em28xx_dvb_register);
1109module_exit(em28xx_dvb_unregister);
1110