linux/drivers/media/usb/em28xx/em28xx-dvb.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2//
   3// DVB device driver for em28xx
   4//
   5// (c) 2008-2011 Mauro Carvalho Chehab <mchehab@kernel.org>
   6//
   7// (c) 2008 Devin Heitmueller <devin.heitmueller@gmail.com>
   8//      - Fixes for the driver to properly work with HVR-950
   9//      - Fixes for the driver to properly work with Pinnacle PCTV HD Pro Stick
  10//      - Fixes for the driver to properly work with AMD ATI TV Wonder HD 600
  11//
  12// (c) 2008 Aidan Thornton <makosoft@googlemail.com>
  13//
  14// (c) 2012 Frank Schäfer <fschaefer.oss@googlemail.com>
  15//
  16// Based on cx88-dvb, saa7134-dvb and videobuf-dvb originally written by:
  17//      (c) 2004, 2005 Chris Pascoe <c.pascoe@itee.uq.edu.au>
  18//      (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
  19//
  20// This program is free software; you can redistribute it and/or modify
  21// it under the terms of the GNU General Public License as published by
  22// the Free Software Foundation version 2 of the License.
  23
  24#include "em28xx.h"
  25
  26#include <linux/kernel.h>
  27#include <linux/slab.h>
  28#include <linux/usb.h>
  29
  30#include <media/v4l2-common.h>
  31#include <media/dvb_demux.h>
  32#include <media/dvb_net.h>
  33#include <media/dmxdev.h>
  34#include <media/tuner.h>
  35#include "tuner-simple.h"
  36#include <linux/gpio.h>
  37
  38#include "lgdt330x.h"
  39#include "lgdt3305.h"
  40#include "lgdt3306a.h"
  41#include "zl10353.h"
  42#include "s5h1409.h"
  43#include "mt2060.h"
  44#include "mt352.h"
  45#include "mt352_priv.h" /* FIXME */
  46#include "tda1002x.h"
  47#include "drx39xyj/drx39xxj.h"
  48#include "tda18271.h"
  49#include "s921.h"
  50#include "drxd.h"
  51#include "cxd2820r.h"
  52#include "tda18271c2dd.h"
  53#include "drxk.h"
  54#include "tda10071.h"
  55#include "tda18212.h"
  56#include "a8293.h"
  57#include "qt1010.h"
  58#include "mb86a20s.h"
  59#include "m88ds3103.h"
  60#include "ts2020.h"
  61#include "si2168.h"
  62#include "si2157.h"
  63#include "tc90522.h"
  64#include "qm1d1c0042.h"
  65#include "mxl692.h"
  66
  67MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@kernel.org>");
  68MODULE_LICENSE("GPL v2");
  69MODULE_DESCRIPTION(DRIVER_DESC " - digital TV interface");
  70MODULE_VERSION(EM28XX_VERSION);
  71
  72static unsigned int debug;
  73module_param(debug, int, 0644);
  74MODULE_PARM_DESC(debug, "enable debug messages [dvb]");
  75
  76DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  77
  78#define dprintk(level, fmt, arg...) do {                                \
  79        if (debug >= level)                                             \
  80                dev_printk(KERN_DEBUG, &dev->intf->dev,                 \
  81                           "dvb: " fmt, ## arg);                        \
  82} while (0)
  83
  84struct em28xx_dvb {
  85        struct dvb_frontend        *fe[2];
  86
  87        /* feed count management */
  88        struct mutex               lock;
  89        int                        nfeeds;
  90
  91        /* general boilerplate stuff */
  92        struct dvb_adapter         adapter;
  93        struct dvb_demux           demux;
  94        struct dmxdev              dmxdev;
  95        struct dmx_frontend        fe_hw;
  96        struct dmx_frontend        fe_mem;
  97        struct dvb_net             net;
  98
  99        /* Due to DRX-K - probably need changes */
 100        int (*gate_ctrl)(struct dvb_frontend *fe, int gate);
 101        struct semaphore      pll_mutex;
 102        bool                    dont_attach_fe1;
 103        int                     lna_gpio;
 104        struct i2c_client       *i2c_client_demod;
 105        struct i2c_client       *i2c_client_tuner;
 106        struct i2c_client       *i2c_client_sec;
 107};
 108
 109static inline void print_err_status(struct em28xx *dev,
 110                                    int packet, int status)
 111{
 112        char *errmsg = "Unknown";
 113
 114        switch (status) {
 115        case -ENOENT:
 116                errmsg = "unlinked synchronously";
 117                break;
 118        case -ECONNRESET:
 119                errmsg = "unlinked asynchronously";
 120                break;
 121        case -ENOSR:
 122                errmsg = "Buffer error (overrun)";
 123                break;
 124        case -EPIPE:
 125                errmsg = "Stalled (device not responding)";
 126                break;
 127        case -EOVERFLOW:
 128                errmsg = "Babble (bad cable?)";
 129                break;
 130        case -EPROTO:
 131                errmsg = "Bit-stuff error (bad cable?)";
 132                break;
 133        case -EILSEQ:
 134                errmsg = "CRC/Timeout (could be anything)";
 135                break;
 136        case -ETIME:
 137                errmsg = "Device does not respond";
 138                break;
 139        }
 140        if (packet < 0) {
 141                dprintk(1, "URB status %d [%s].\n", status, errmsg);
 142        } else {
 143                dprintk(1, "URB packet %d, status %d [%s].\n",
 144                        packet, status, errmsg);
 145        }
 146}
 147
 148static inline int em28xx_dvb_urb_data_copy(struct em28xx *dev, struct urb *urb)
 149{
 150        int xfer_bulk, num_packets, i;
 151
 152        if (!dev)
 153                return 0;
 154
 155        if (dev->disconnected)
 156                return 0;
 157
 158        if (urb->status < 0)
 159                print_err_status(dev, -1, urb->status);
 160
 161        xfer_bulk = usb_pipebulk(urb->pipe);
 162
 163        if (xfer_bulk) /* bulk */
 164                num_packets = 1;
 165        else /* isoc */
 166                num_packets = urb->number_of_packets;
 167
 168        for (i = 0; i < num_packets; i++) {
 169                if (xfer_bulk) {
 170                        if (urb->status < 0) {
 171                                print_err_status(dev, i, urb->status);
 172                                if (urb->status != -EPROTO)
 173                                        continue;
 174                        }
 175                        if (!urb->actual_length)
 176                                continue;
 177                        dvb_dmx_swfilter(&dev->dvb->demux, urb->transfer_buffer,
 178                                         urb->actual_length);
 179                } else {
 180                        if (urb->iso_frame_desc[i].status < 0) {
 181                                print_err_status(dev, i,
 182                                                 urb->iso_frame_desc[i].status);
 183                                if (urb->iso_frame_desc[i].status != -EPROTO)
 184                                        continue;
 185                        }
 186                        if (!urb->iso_frame_desc[i].actual_length)
 187                                continue;
 188                        dvb_dmx_swfilter(&dev->dvb->demux,
 189                                         urb->transfer_buffer +
 190                                         urb->iso_frame_desc[i].offset,
 191                                         urb->iso_frame_desc[i].actual_length);
 192                }
 193        }
 194
 195        return 0;
 196}
 197
 198static int em28xx_start_streaming(struct em28xx_dvb *dvb)
 199{
 200        int rc;
 201        struct em28xx_i2c_bus *i2c_bus = dvb->adapter.priv;
 202        struct em28xx *dev = i2c_bus->dev;
 203        struct usb_device *udev = interface_to_usbdev(dev->intf);
 204        int dvb_max_packet_size, packet_multiplier, dvb_alt;
 205
 206        if (dev->dvb_xfer_bulk) {
 207                if (!dev->dvb_ep_bulk)
 208                        return -ENODEV;
 209                dvb_max_packet_size = 512; /* USB 2.0 spec */
 210                packet_multiplier = EM28XX_DVB_BULK_PACKET_MULTIPLIER;
 211                dvb_alt = 0;
 212        } else { /* isoc */
 213                if (!dev->dvb_ep_isoc)
 214                        return -ENODEV;
 215                dvb_max_packet_size = dev->dvb_max_pkt_size_isoc;
 216                if (dvb_max_packet_size < 0)
 217                        return dvb_max_packet_size;
 218                packet_multiplier = EM28XX_DVB_NUM_ISOC_PACKETS;
 219                dvb_alt = dev->dvb_alt_isoc;
 220        }
 221
 222        if (!dev->board.has_dual_ts)
 223                usb_set_interface(udev, dev->ifnum, dvb_alt);
 224
 225        rc = em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
 226        if (rc < 0)
 227                return rc;
 228
 229        dprintk(1, "Using %d buffers each with %d x %d bytes, alternate %d\n",
 230                EM28XX_DVB_NUM_BUFS,
 231                packet_multiplier,
 232                dvb_max_packet_size, dvb_alt);
 233
 234        return em28xx_init_usb_xfer(dev, EM28XX_DIGITAL_MODE,
 235                                    dev->dvb_xfer_bulk,
 236                                    EM28XX_DVB_NUM_BUFS,
 237                                    dvb_max_packet_size,
 238                                    packet_multiplier,
 239                                    em28xx_dvb_urb_data_copy);
 240}
 241
 242static int em28xx_stop_streaming(struct em28xx_dvb *dvb)
 243{
 244        struct em28xx_i2c_bus *i2c_bus = dvb->adapter.priv;
 245        struct em28xx *dev = i2c_bus->dev;
 246
 247        em28xx_stop_urbs(dev);
 248
 249        return 0;
 250}
 251
 252static int em28xx_start_feed(struct dvb_demux_feed *feed)
 253{
 254        struct dvb_demux *demux  = feed->demux;
 255        struct em28xx_dvb *dvb = demux->priv;
 256        int rc, ret;
 257
 258        if (!demux->dmx.frontend)
 259                return -EINVAL;
 260
 261        mutex_lock(&dvb->lock);
 262        dvb->nfeeds++;
 263        rc = dvb->nfeeds;
 264
 265        if (dvb->nfeeds == 1) {
 266                ret = em28xx_start_streaming(dvb);
 267                if (ret < 0)
 268                        rc = ret;
 269        }
 270
 271        mutex_unlock(&dvb->lock);
 272        return rc;
 273}
 274
 275static int em28xx_stop_feed(struct dvb_demux_feed *feed)
 276{
 277        struct dvb_demux *demux  = feed->demux;
 278        struct em28xx_dvb *dvb = demux->priv;
 279        int err = 0;
 280
 281        mutex_lock(&dvb->lock);
 282        dvb->nfeeds--;
 283
 284        if (!dvb->nfeeds)
 285                err = em28xx_stop_streaming(dvb);
 286
 287        mutex_unlock(&dvb->lock);
 288        return err;
 289}
 290
 291/* ------------------------------------------------------------------ */
 292static int em28xx_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
 293{
 294        struct em28xx_i2c_bus *i2c_bus = fe->dvb->priv;
 295        struct em28xx *dev = i2c_bus->dev;
 296
 297        if (acquire)
 298                return em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
 299        else
 300                return em28xx_set_mode(dev, EM28XX_SUSPEND);
 301}
 302
 303/* ------------------------------------------------------------------ */
 304
 305static struct lgdt330x_config em2880_lgdt3303_dev = {
 306        .demod_chip = LGDT3303,
 307};
 308
 309static struct lgdt3305_config em2870_lgdt3304_dev = {
 310        .i2c_addr           = 0x0e,
 311        .demod_chip         = LGDT3304,
 312        .spectral_inversion = 1,
 313        .deny_i2c_rptr      = 1,
 314        .mpeg_mode          = LGDT3305_MPEG_PARALLEL,
 315        .tpclk_edge         = LGDT3305_TPCLK_FALLING_EDGE,
 316        .tpvalid_polarity   = LGDT3305_TP_VALID_HIGH,
 317        .vsb_if_khz         = 3250,
 318        .qam_if_khz         = 4000,
 319};
 320
 321static struct lgdt3305_config em2874_lgdt3305_dev = {
 322        .i2c_addr           = 0x0e,
 323        .demod_chip         = LGDT3305,
 324        .spectral_inversion = 1,
 325        .deny_i2c_rptr      = 0,
 326        .mpeg_mode          = LGDT3305_MPEG_SERIAL,
 327        .tpclk_edge         = LGDT3305_TPCLK_FALLING_EDGE,
 328        .tpvalid_polarity   = LGDT3305_TP_VALID_HIGH,
 329        .vsb_if_khz         = 3250,
 330        .qam_if_khz         = 4000,
 331};
 332
 333static struct lgdt3305_config em2874_lgdt3305_nogate_dev = {
 334        .i2c_addr           = 0x0e,
 335        .demod_chip         = LGDT3305,
 336        .spectral_inversion = 1,
 337        .deny_i2c_rptr      = 1,
 338        .mpeg_mode          = LGDT3305_MPEG_SERIAL,
 339        .tpclk_edge         = LGDT3305_TPCLK_FALLING_EDGE,
 340        .tpvalid_polarity   = LGDT3305_TP_VALID_HIGH,
 341        .vsb_if_khz         = 3600,
 342        .qam_if_khz         = 3600,
 343};
 344
 345static struct s921_config sharp_isdbt = {
 346        .demod_address = 0x30 >> 1
 347};
 348
 349static struct zl10353_config em28xx_zl10353_with_xc3028 = {
 350        .demod_address = (0x1e >> 1),
 351        .no_tuner = 1,
 352        .parallel_ts = 1,
 353        .if2 = 45600,
 354};
 355
 356static struct s5h1409_config em28xx_s5h1409_with_xc3028 = {
 357        .demod_address = 0x32 >> 1,
 358        .output_mode   = S5H1409_PARALLEL_OUTPUT,
 359        .gpio          = S5H1409_GPIO_OFF,
 360        .inversion     = S5H1409_INVERSION_OFF,
 361        .status_mode   = S5H1409_DEMODLOCKING,
 362        .mpeg_timing   = S5H1409_MPEGTIMING_CONTINUOUS_NONINVERTING_CLOCK
 363};
 364
 365static struct tda18271_std_map kworld_a340_std_map = {
 366        .atsc_6   = { .if_freq = 3250, .agc_mode = 3, .std = 0,
 367                      .if_lvl = 1, .rfagc_top = 0x37, },
 368        .qam_6    = { .if_freq = 4000, .agc_mode = 3, .std = 1,
 369                      .if_lvl = 1, .rfagc_top = 0x37, },
 370};
 371
 372static struct tda18271_config kworld_a340_config = {
 373        .std_map           = &kworld_a340_std_map,
 374};
 375
 376static struct tda18271_config kworld_ub435q_v2_config = {
 377        .std_map        = &kworld_a340_std_map,
 378        .gate           = TDA18271_GATE_DIGITAL,
 379};
 380
 381static struct tda18212_config kworld_ub435q_v3_config = {
 382        .if_atsc_vsb    = 3600,
 383        .if_atsc_qam    = 3600,
 384};
 385
 386static struct zl10353_config em28xx_zl10353_xc3028_no_i2c_gate = {
 387        .demod_address = (0x1e >> 1),
 388        .no_tuner = 1,
 389        .disable_i2c_gate_ctrl = 1,
 390        .parallel_ts = 1,
 391        .if2 = 45600,
 392};
 393
 394static struct drxd_config em28xx_drxd = {
 395        .demod_address = 0x70,
 396        .demod_revision = 0xa2,
 397        .pll_type = DRXD_PLL_NONE,
 398        .clock = 12000,
 399        .insert_rs_byte = 1,
 400        .IF = 42800000,
 401        .disable_i2c_gate_ctrl = 1,
 402};
 403
 404static struct drxk_config terratec_h5_drxk = {
 405        .adr = 0x29,
 406        .single_master = 1,
 407        .no_i2c_bridge = 1,
 408        .microcode_name = "dvb-usb-terratec-h5-drxk.fw",
 409        .qam_demod_parameter_count = 2,
 410};
 411
 412static struct drxk_config hauppauge_930c_drxk = {
 413        .adr = 0x29,
 414        .single_master = 1,
 415        .no_i2c_bridge = 1,
 416        .microcode_name = "dvb-usb-hauppauge-hvr930c-drxk.fw",
 417        .chunk_size = 56,
 418        .qam_demod_parameter_count = 2,
 419};
 420
 421static struct drxk_config terratec_htc_stick_drxk = {
 422        .adr = 0x29,
 423        .single_master = 1,
 424        .no_i2c_bridge = 1,
 425        .microcode_name = "dvb-usb-terratec-htc-stick-drxk.fw",
 426        .chunk_size = 54,
 427        .qam_demod_parameter_count = 2,
 428        /* Required for the antenna_gpio to disable LNA. */
 429        .antenna_dvbt = true,
 430        /* The windows driver uses the same. This will disable LNA. */
 431        .antenna_gpio = 0x6,
 432};
 433
 434static struct drxk_config maxmedia_ub425_tc_drxk = {
 435        .adr = 0x29,
 436        .single_master = 1,
 437        .no_i2c_bridge = 1,
 438        .microcode_name = "dvb-demod-drxk-01.fw",
 439        .chunk_size = 62,
 440        .qam_demod_parameter_count = 2,
 441};
 442
 443static struct drxk_config pctv_520e_drxk = {
 444        .adr = 0x29,
 445        .single_master = 1,
 446        .microcode_name = "dvb-demod-drxk-pctv.fw",
 447        .qam_demod_parameter_count = 2,
 448        .chunk_size = 58,
 449        .antenna_dvbt = true, /* disable LNA */
 450        .antenna_gpio = (1 << 2), /* disable LNA */
 451};
 452
 453static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
 454{
 455        struct em28xx_dvb *dvb = fe->sec_priv;
 456        int status;
 457
 458        if (!dvb)
 459                return -EINVAL;
 460
 461        if (enable) {
 462                down(&dvb->pll_mutex);
 463                status = dvb->gate_ctrl(fe, 1);
 464        } else {
 465                status = dvb->gate_ctrl(fe, 0);
 466                up(&dvb->pll_mutex);
 467        }
 468        return status;
 469}
 470
 471static void hauppauge_hvr930c_init(struct em28xx *dev)
 472{
 473        int i;
 474
 475        static const struct em28xx_reg_seq hauppauge_hvr930c_init[] = {
 476                {EM2874_R80_GPIO_P0_CTRL,       0xff,   0xff,   0x65},
 477                {EM2874_R80_GPIO_P0_CTRL,       0xfb,   0xff,   0x32},
 478                {EM2874_R80_GPIO_P0_CTRL,       0xff,   0xff,   0xb8},
 479                {       -1,                     -1,     -1,     -1},
 480        };
 481        static const struct em28xx_reg_seq hauppauge_hvr930c_end[] = {
 482                {EM2874_R80_GPIO_P0_CTRL,       0xef,   0xff,   0x01},
 483                {EM2874_R80_GPIO_P0_CTRL,       0xaf,   0xff,   0x65},
 484                {EM2874_R80_GPIO_P0_CTRL,       0xef,   0xff,   0x76},
 485                {EM2874_R80_GPIO_P0_CTRL,       0xef,   0xff,   0x01},
 486                {EM2874_R80_GPIO_P0_CTRL,       0xcf,   0xff,   0x0b},
 487                {EM2874_R80_GPIO_P0_CTRL,       0xef,   0xff,   0x40},
 488
 489                {EM2874_R80_GPIO_P0_CTRL,       0xcf,   0xff,   0x65},
 490                {EM2874_R80_GPIO_P0_CTRL,       0xef,   0xff,   0x65},
 491                {EM2874_R80_GPIO_P0_CTRL,       0xcf,   0xff,   0x0b},
 492                {EM2874_R80_GPIO_P0_CTRL,       0xef,   0xff,   0x65},
 493
 494                {       -1,                     -1,     -1,     -1},
 495        };
 496
 497        static const struct {
 498                unsigned char r[4];
 499                int len;
 500        } regs[] = {
 501                {{ 0x06, 0x02, 0x00, 0x31 }, 4},
 502                {{ 0x01, 0x02 }, 2},
 503                {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
 504                {{ 0x01, 0x00 }, 2},
 505                {{ 0x01, 0x00, 0xff, 0xaf }, 4},
 506                {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
 507                {{ 0x01, 0x00 }, 2},
 508                {{ 0x01, 0x00, 0x73, 0xaf }, 4},
 509                {{ 0x04, 0x00 }, 2},
 510                {{ 0x00, 0x04 }, 2},
 511                {{ 0x00, 0x04, 0x00, 0x0a }, 4},
 512                {{ 0x04, 0x14 }, 2},
 513                {{ 0x04, 0x14, 0x00, 0x00 }, 4},
 514        };
 515
 516        em28xx_gpio_set(dev, hauppauge_hvr930c_init);
 517        em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
 518        usleep_range(10000, 11000);
 519        em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
 520        usleep_range(10000, 11000);
 521
 522        dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1;
 523
 524        for (i = 0; i < ARRAY_SIZE(regs); i++)
 525                i2c_master_send(&dev->i2c_client[dev->def_i2c_bus],
 526                                regs[i].r, regs[i].len);
 527        em28xx_gpio_set(dev, hauppauge_hvr930c_end);
 528
 529        msleep(100);
 530
 531        em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
 532        msleep(30);
 533
 534        em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x45);
 535        usleep_range(10000, 11000);
 536}
 537
 538static void terratec_h5_init(struct em28xx *dev)
 539{
 540        int i;
 541        static const struct em28xx_reg_seq terratec_h5_init[] = {
 542                {EM2820_R08_GPIO_CTRL,          0xff,   0xff,   10},
 543                {EM2874_R80_GPIO_P0_CTRL,       0xf6,   0xff,   100},
 544                {EM2874_R80_GPIO_P0_CTRL,       0xf2,   0xff,   50},
 545                {EM2874_R80_GPIO_P0_CTRL,       0xf6,   0xff,   100},
 546                {       -1,                     -1,     -1,     -1},
 547        };
 548        static const struct em28xx_reg_seq terratec_h5_end[] = {
 549                {EM2874_R80_GPIO_P0_CTRL,       0xe6,   0xff,   100},
 550                {EM2874_R80_GPIO_P0_CTRL,       0xa6,   0xff,   50},
 551                {EM2874_R80_GPIO_P0_CTRL,       0xe6,   0xff,   100},
 552                {       -1,                     -1,     -1,     -1},
 553        };
 554        static const struct {
 555                unsigned char r[4];
 556                int len;
 557        } regs[] = {
 558                {{ 0x06, 0x02, 0x00, 0x31 }, 4},
 559                {{ 0x01, 0x02 }, 2},
 560                {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
 561                {{ 0x01, 0x00 }, 2},
 562                {{ 0x01, 0x00, 0xff, 0xaf }, 4},
 563                {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
 564                {{ 0x01, 0x00 }, 2},
 565                {{ 0x01, 0x00, 0x73, 0xaf }, 4},
 566                {{ 0x04, 0x00 }, 2},
 567                {{ 0x00, 0x04 }, 2},
 568                {{ 0x00, 0x04, 0x00, 0x0a }, 4},
 569                {{ 0x04, 0x14 }, 2},
 570                {{ 0x04, 0x14, 0x00, 0x00 }, 4},
 571        };
 572
 573        em28xx_gpio_set(dev, terratec_h5_init);
 574        em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
 575        usleep_range(10000, 11000);
 576        em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x45);
 577        usleep_range(10000, 11000);
 578
 579        dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1;
 580
 581        for (i = 0; i < ARRAY_SIZE(regs); i++)
 582                i2c_master_send(&dev->i2c_client[dev->def_i2c_bus],
 583                                regs[i].r, regs[i].len);
 584        em28xx_gpio_set(dev, terratec_h5_end);
 585};
 586
 587static void terratec_htc_stick_init(struct em28xx *dev)
 588{
 589        int i;
 590
 591        /*
 592         * GPIO configuration:
 593         * 0xff: unknown (does not affect DVB-T).
 594         * 0xf6: DRX-K (demodulator).
 595         * 0xe6: unknown (does not affect DVB-T).
 596         * 0xb6: unknown (does not affect DVB-T).
 597         */
 598        static const struct em28xx_reg_seq terratec_htc_stick_init[] = {
 599                {EM2820_R08_GPIO_CTRL,          0xff,   0xff,   10},
 600                {EM2874_R80_GPIO_P0_CTRL,       0xf6,   0xff,   100},
 601                {EM2874_R80_GPIO_P0_CTRL,       0xe6,   0xff,   50},
 602                {EM2874_R80_GPIO_P0_CTRL,       0xf6,   0xff,   100},
 603                {       -1,                     -1,     -1,     -1},
 604        };
 605        static const struct em28xx_reg_seq terratec_htc_stick_end[] = {
 606                {EM2874_R80_GPIO_P0_CTRL,       0xb6,   0xff,   100},
 607                {EM2874_R80_GPIO_P0_CTRL,       0xf6,   0xff,   50},
 608                {       -1,                     -1,     -1,     -1},
 609        };
 610
 611        /*
 612         * Init the analog decoder (not yet supported), but
 613         * it's probably still a good idea.
 614         */
 615        static const struct {
 616                unsigned char r[4];
 617                int len;
 618        } regs[] = {
 619                {{ 0x06, 0x02, 0x00, 0x31 }, 4},
 620                {{ 0x01, 0x02 }, 2},
 621                {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
 622                {{ 0x01, 0x00 }, 2},
 623                {{ 0x01, 0x00, 0xff, 0xaf }, 4},
 624        };
 625
 626        em28xx_gpio_set(dev, terratec_htc_stick_init);
 627
 628        em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
 629        usleep_range(10000, 11000);
 630        em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
 631        usleep_range(10000, 11000);
 632
 633        dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1;
 634
 635        for (i = 0; i < ARRAY_SIZE(regs); i++)
 636                i2c_master_send(&dev->i2c_client[dev->def_i2c_bus],
 637                                regs[i].r, regs[i].len);
 638
 639        em28xx_gpio_set(dev, terratec_htc_stick_end);
 640};
 641
 642static void terratec_htc_usb_xs_init(struct em28xx *dev)
 643{
 644        int i;
 645
 646        static const struct em28xx_reg_seq terratec_htc_usb_xs_init[] = {
 647                {EM2820_R08_GPIO_CTRL,          0xff,   0xff,   10},
 648                {EM2874_R80_GPIO_P0_CTRL,       0xb2,   0xff,   100},
 649                {EM2874_R80_GPIO_P0_CTRL,       0xb2,   0xff,   50},
 650                {EM2874_R80_GPIO_P0_CTRL,       0xb6,   0xff,   100},
 651                {       -1,                     -1,     -1,     -1},
 652        };
 653        static const struct em28xx_reg_seq terratec_htc_usb_xs_end[] = {
 654                {EM2874_R80_GPIO_P0_CTRL,       0xa6,   0xff,   100},
 655                {EM2874_R80_GPIO_P0_CTRL,       0xa6,   0xff,   50},
 656                {EM2874_R80_GPIO_P0_CTRL,       0xe6,   0xff,   100},
 657                {       -1,                     -1,     -1,     -1},
 658        };
 659
 660        /*
 661         * Init the analog decoder (not yet supported), but
 662         * it's probably still a good idea.
 663         */
 664        static const struct {
 665                unsigned char r[4];
 666                int len;
 667        } regs[] = {
 668                {{ 0x06, 0x02, 0x00, 0x31 }, 4},
 669                {{ 0x01, 0x02 }, 2},
 670                {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
 671                {{ 0x01, 0x00 }, 2},
 672                {{ 0x01, 0x00, 0xff, 0xaf }, 4},
 673                {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
 674                {{ 0x01, 0x00 }, 2},
 675                {{ 0x01, 0x00, 0x73, 0xaf }, 4},
 676                {{ 0x04, 0x00 }, 2},
 677                {{ 0x00, 0x04 }, 2},
 678                {{ 0x00, 0x04, 0x00, 0x0a }, 4},
 679                {{ 0x04, 0x14 }, 2},
 680                {{ 0x04, 0x14, 0x00, 0x00 }, 4},
 681        };
 682
 683        em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
 684
 685        em28xx_gpio_set(dev, terratec_htc_usb_xs_init);
 686
 687        em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
 688        usleep_range(10000, 11000);
 689        em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
 690        usleep_range(10000, 11000);
 691
 692        dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1;
 693
 694        for (i = 0; i < ARRAY_SIZE(regs); i++)
 695                i2c_master_send(&dev->i2c_client[dev->def_i2c_bus],
 696                                regs[i].r, regs[i].len);
 697
 698        em28xx_gpio_set(dev, terratec_htc_usb_xs_end);
 699};
 700
 701static void pctv_520e_init(struct em28xx *dev)
 702{
 703        /*
 704         * Init AVF4910B analog decoder. Looks like I2C traffic to
 705         * digital demodulator and tuner are routed via AVF4910B.
 706         */
 707        int i;
 708        static const struct {
 709                unsigned char r[4];
 710                int len;
 711        } regs[] = {
 712                {{ 0x06, 0x02, 0x00, 0x31 }, 4},
 713                {{ 0x01, 0x02 }, 2},
 714                {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
 715                {{ 0x01, 0x00 }, 2},
 716                {{ 0x01, 0x00, 0xff, 0xaf }, 4},
 717                {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
 718                {{ 0x01, 0x00 }, 2},
 719                {{ 0x01, 0x00, 0x73, 0xaf }, 4},
 720        };
 721
 722        dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1; /* 0x41 */
 723
 724        for (i = 0; i < ARRAY_SIZE(regs); i++)
 725                i2c_master_send(&dev->i2c_client[dev->def_i2c_bus],
 726                                regs[i].r, regs[i].len);
 727};
 728
 729static int em28xx_pctv_290e_set_lna(struct dvb_frontend *fe)
 730{
 731        struct dtv_frontend_properties *c = &fe->dtv_property_cache;
 732        struct em28xx_i2c_bus *i2c_bus = fe->dvb->priv;
 733        struct em28xx *dev = i2c_bus->dev;
 734#ifdef CONFIG_GPIOLIB
 735        struct em28xx_dvb *dvb = dev->dvb;
 736        int ret;
 737        unsigned long flags;
 738
 739        if (c->lna == 1)
 740                flags = GPIOF_OUT_INIT_HIGH; /* enable LNA */
 741        else
 742                flags = GPIOF_OUT_INIT_LOW; /* disable LNA */
 743
 744        ret = gpio_request_one(dvb->lna_gpio, flags, NULL);
 745        if (ret)
 746                dev_err(&dev->intf->dev, "gpio request failed %d\n", ret);
 747        else
 748                gpio_free(dvb->lna_gpio);
 749
 750        return ret;
 751#else
 752        dev_warn(&dev->intf->dev, "%s: LNA control is disabled (lna=%u)\n",
 753                 KBUILD_MODNAME, c->lna);
 754        return 0;
 755#endif
 756}
 757
 758static int em28xx_pctv_292e_set_lna(struct dvb_frontend *fe)
 759{
 760        struct dtv_frontend_properties *c = &fe->dtv_property_cache;
 761        struct em28xx_i2c_bus *i2c_bus = fe->dvb->priv;
 762        struct em28xx *dev = i2c_bus->dev;
 763        u8 lna;
 764
 765        if (c->lna == 1)
 766                lna = 0x01;
 767        else
 768                lna = 0x00;
 769
 770        return em28xx_write_reg_bits(dev, EM2874_R80_GPIO_P0_CTRL, lna, 0x01);
 771}
 772
 773static int em28xx_mt352_terratec_xs_init(struct dvb_frontend *fe)
 774{
 775        /* Values extracted from a USB trace of the Terratec Windows driver */
 776        static u8 clock_config[]   = { CLOCK_CTL,  0x38, 0x2c };
 777        static u8 reset[]          = { RESET,      0x80 };
 778        static u8 adc_ctl_1_cfg[]  = { ADC_CTL_1,  0x40 };
 779        static u8 agc_cfg[]        = { AGC_TARGET, 0x28, 0xa0 };
 780        static u8 input_freq_cfg[] = { INPUT_FREQ_1, 0x31, 0xb8 };
 781        static u8 rs_err_cfg[]     = { RS_ERR_PER_1, 0x00, 0x4d };
 782        static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
 783        static u8 trl_nom_cfg[]    = { TRL_NOMINAL_RATE_1, 0x64, 0x00 };
 784        static u8 tps_given_cfg[]  = { TPS_GIVEN_1, 0x40, 0x80, 0x50 };
 785        static u8 tuner_go[]       = { TUNER_GO, 0x01};
 786
 787        mt352_write(fe, clock_config,   sizeof(clock_config));
 788        usleep_range(200, 250);
 789        mt352_write(fe, reset,          sizeof(reset));
 790        mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
 791        mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
 792        mt352_write(fe, input_freq_cfg, sizeof(input_freq_cfg));
 793        mt352_write(fe, rs_err_cfg,     sizeof(rs_err_cfg));
 794        mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
 795        mt352_write(fe, trl_nom_cfg,    sizeof(trl_nom_cfg));
 796        mt352_write(fe, tps_given_cfg,  sizeof(tps_given_cfg));
 797        mt352_write(fe, tuner_go,       sizeof(tuner_go));
 798        return 0;
 799}
 800
 801static void px_bcud_init(struct em28xx *dev)
 802{
 803        int i;
 804        static const struct {
 805                unsigned char r[4];
 806                int len;
 807        } regs1[] = {
 808                {{ 0x0e, 0x77 }, 2},
 809                {{ 0x0f, 0x77 }, 2},
 810                {{ 0x03, 0x90 }, 2},
 811        }, regs2[] = {
 812                {{ 0x07, 0x01 }, 2},
 813                {{ 0x08, 0x10 }, 2},
 814                {{ 0x13, 0x00 }, 2},
 815                {{ 0x17, 0x00 }, 2},
 816                {{ 0x03, 0x01 }, 2},
 817                {{ 0x10, 0xb1 }, 2},
 818                {{ 0x11, 0x40 }, 2},
 819                {{ 0x85, 0x7a }, 2},
 820                {{ 0x87, 0x04 }, 2},
 821        };
 822        static const struct em28xx_reg_seq gpio[] = {
 823                {EM28XX_R06_I2C_CLK,            0x40,   0xff,   300},
 824                {EM2874_R80_GPIO_P0_CTRL,       0xfd,   0xff,   60},
 825                {EM28XX_R15_RGAIN,              0x20,   0xff,   0},
 826                {EM28XX_R16_GGAIN,              0x20,   0xff,   0},
 827                {EM28XX_R17_BGAIN,              0x20,   0xff,   0},
 828                {EM28XX_R18_ROFFSET,            0x00,   0xff,   0},
 829                {EM28XX_R19_GOFFSET,            0x00,   0xff,   0},
 830                {EM28XX_R1A_BOFFSET,            0x00,   0xff,   0},
 831                {EM28XX_R23_UOFFSET,            0x00,   0xff,   0},
 832                {EM28XX_R24_VOFFSET,            0x00,   0xff,   0},
 833                {EM28XX_R26_COMPR,              0x00,   0xff,   0},
 834                {0x13,                          0x08,   0xff,   0},
 835                {EM28XX_R12_VINENABLE,          0x27,   0xff,   0},
 836                {EM28XX_R0C_USBSUSP,            0x10,   0xff,   0},
 837                {EM28XX_R27_OUTFMT,             0x00,   0xff,   0},
 838                {EM28XX_R10_VINMODE,            0x00,   0xff,   0},
 839                {EM28XX_R11_VINCTRL,            0x11,   0xff,   0},
 840                {EM2874_R50_IR_CONFIG,          0x01,   0xff,   0},
 841                {EM2874_R5F_TS_ENABLE,          0x80,   0xff,   0},
 842                {EM28XX_R06_I2C_CLK,            0x46,   0xff,   0},
 843        };
 844        em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x46);
 845        /* sleeping ISDB-T */
 846        dev->dvb->i2c_client_demod->addr = 0x14;
 847        for (i = 0; i < ARRAY_SIZE(regs1); i++)
 848                i2c_master_send(dev->dvb->i2c_client_demod,
 849                                regs1[i].r, regs1[i].len);
 850        /* sleeping ISDB-S */
 851        dev->dvb->i2c_client_demod->addr = 0x15;
 852        for (i = 0; i < ARRAY_SIZE(regs2); i++)
 853                i2c_master_send(dev->dvb->i2c_client_demod, regs2[i].r,
 854                                regs2[i].len);
 855        for (i = 0; i < ARRAY_SIZE(gpio); i++) {
 856                em28xx_write_reg_bits(dev, gpio[i].reg, gpio[i].val,
 857                                      gpio[i].mask);
 858                if (gpio[i].sleep > 0)
 859                        msleep(gpio[i].sleep);
 860        }
 861};
 862
 863static struct mt352_config terratec_xs_mt352_cfg = {
 864        .demod_address = (0x1e >> 1),
 865        .no_tuner = 1,
 866        .if2 = 45600,
 867        .demod_init = em28xx_mt352_terratec_xs_init,
 868};
 869
 870static struct tda10023_config em28xx_tda10023_config = {
 871        .demod_address = 0x0c,
 872        .invert = 1,
 873};
 874
 875static struct cxd2820r_config em28xx_cxd2820r_config = {
 876        .i2c_address = (0xd8 >> 1),
 877        .ts_mode = CXD2820R_TS_SERIAL,
 878};
 879
 880static struct tda18271_config em28xx_cxd2820r_tda18271_config = {
 881        .output_opt = TDA18271_OUTPUT_LT_OFF,
 882        .gate = TDA18271_GATE_DIGITAL,
 883};
 884
 885static struct zl10353_config em28xx_zl10353_no_i2c_gate_dev = {
 886        .demod_address = (0x1e >> 1),
 887        .disable_i2c_gate_ctrl = 1,
 888        .no_tuner = 1,
 889        .parallel_ts = 1,
 890};
 891
 892static struct mt2060_config em28xx_mt2060_config = {
 893        .i2c_address = 0x60,
 894};
 895
 896static struct qt1010_config em28xx_qt1010_config = {
 897        .i2c_address = 0x62
 898};
 899
 900static const struct mb86a20s_config c3tech_duo_mb86a20s_config = {
 901        .demod_address = 0x10,
 902        .is_serial = true,
 903};
 904
 905static struct tda18271_std_map mb86a20s_tda18271_config = {
 906        .dvbt_6   = { .if_freq = 4000, .agc_mode = 3, .std = 4,
 907                      .if_lvl = 1, .rfagc_top = 0x37, },
 908};
 909
 910static struct tda18271_config c3tech_duo_tda18271_config = {
 911        .std_map = &mb86a20s_tda18271_config,
 912        .gate    = TDA18271_GATE_DIGITAL,
 913        .small_i2c = TDA18271_03_BYTE_CHUNK_INIT,
 914};
 915
 916static struct tda18271_std_map drx_j_std_map = {
 917        .atsc_6   = { .if_freq = 5000, .agc_mode = 3, .std = 0, .if_lvl = 1,
 918                      .rfagc_top = 0x37, },
 919        .qam_6    = { .if_freq = 5380, .agc_mode = 3, .std = 3, .if_lvl = 1,
 920                      .rfagc_top = 0x37, },
 921};
 922
 923static struct tda18271_config pinnacle_80e_dvb_config = {
 924        .std_map = &drx_j_std_map,
 925        .gate    = TDA18271_GATE_DIGITAL,
 926        .role    = TDA18271_MASTER,
 927};
 928
 929static struct lgdt3306a_config hauppauge_01595_lgdt3306a_config = {
 930        .qam_if_khz         = 4000,
 931        .vsb_if_khz         = 3250,
 932        .spectral_inversion = 0,
 933        .deny_i2c_rptr      = 0,
 934        .mpeg_mode          = LGDT3306A_MPEG_SERIAL,
 935        .tpclk_edge         = LGDT3306A_TPCLK_RISING_EDGE,
 936        .tpvalid_polarity   = LGDT3306A_TP_VALID_HIGH,
 937        .xtalMHz            = 25,
 938};
 939
 940/* ------------------------------------------------------------------ */
 941
 942static noinline_for_stack int em28xx_attach_xc3028(u8 addr, struct em28xx *dev)
 943{
 944        struct dvb_frontend *fe;
 945        struct xc2028_config cfg;
 946        struct xc2028_ctrl ctl;
 947
 948        memset(&cfg, 0, sizeof(cfg));
 949        cfg.i2c_adap  = &dev->i2c_adap[dev->def_i2c_bus];
 950        cfg.i2c_addr  = addr;
 951
 952        memset(&ctl, 0, sizeof(ctl));
 953        em28xx_setup_xc3028(dev, &ctl);
 954        cfg.ctrl  = &ctl;
 955
 956        if (!dev->dvb->fe[0]) {
 957                dev_err(&dev->intf->dev,
 958                        "dvb frontend not attached. Can't attach xc3028\n");
 959                return -EINVAL;
 960        }
 961
 962        fe = dvb_attach(xc2028_attach, dev->dvb->fe[0], &cfg);
 963        if (!fe) {
 964                dev_err(&dev->intf->dev, "xc3028 attach failed\n");
 965                dvb_frontend_detach(dev->dvb->fe[0]);
 966                dev->dvb->fe[0] = NULL;
 967                return -EINVAL;
 968        }
 969
 970        dev_info(&dev->intf->dev, "xc3028 attached\n");
 971
 972        return 0;
 973}
 974
 975/* ------------------------------------------------------------------ */
 976
 977static int em28xx_register_dvb(struct em28xx_dvb *dvb, struct module *module,
 978                               struct em28xx *dev, struct device *device)
 979{
 980        int result;
 981        bool create_rf_connector = false;
 982
 983        mutex_init(&dvb->lock);
 984
 985        /* register adapter */
 986        result = dvb_register_adapter(&dvb->adapter,
 987                                      dev_name(&dev->intf->dev), module,
 988                                      device, adapter_nr);
 989        if (result < 0) {
 990                dev_warn(&dev->intf->dev,
 991                         "dvb_register_adapter failed (errno = %d)\n",
 992                         result);
 993                goto fail_adapter;
 994        }
 995#ifdef CONFIG_MEDIA_CONTROLLER_DVB
 996        dvb->adapter.mdev = dev->media_dev;
 997#endif
 998
 999        /* Ensure all frontends negotiate bus access */
1000        dvb->fe[0]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
1001        if (dvb->fe[1])
1002                dvb->fe[1]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
1003
1004        dvb->adapter.priv = &dev->i2c_bus[dev->def_i2c_bus];
1005
1006        /* register frontend */
1007        result = dvb_register_frontend(&dvb->adapter, dvb->fe[0]);
1008        if (result < 0) {
1009                dev_warn(&dev->intf->dev,
1010                         "dvb_register_frontend failed (errno = %d)\n",
1011                         result);
1012                goto fail_frontend0;
1013        }
1014
1015        /* register 2nd frontend */
1016        if (dvb->fe[1]) {
1017                result = dvb_register_frontend(&dvb->adapter, dvb->fe[1]);
1018                if (result < 0) {
1019                        dev_warn(&dev->intf->dev,
1020                                 "2nd dvb_register_frontend failed (errno = %d)\n",
1021                                 result);
1022                        goto fail_frontend1;
1023                }
1024        }
1025
1026        /* register demux stuff */
1027        dvb->demux.dmx.capabilities =
1028                DMX_TS_FILTERING | DMX_SECTION_FILTERING |
1029                DMX_MEMORY_BASED_FILTERING;
1030        dvb->demux.priv       = dvb;
1031        dvb->demux.filternum  = 256;
1032        dvb->demux.feednum    = 256;
1033        dvb->demux.start_feed = em28xx_start_feed;
1034        dvb->demux.stop_feed  = em28xx_stop_feed;
1035
1036        result = dvb_dmx_init(&dvb->demux);
1037        if (result < 0) {
1038                dev_warn(&dev->intf->dev,
1039                         "dvb_dmx_init failed (errno = %d)\n",
1040                         result);
1041                goto fail_dmx;
1042        }
1043
1044        dvb->dmxdev.filternum    = 256;
1045        dvb->dmxdev.demux        = &dvb->demux.dmx;
1046        dvb->dmxdev.capabilities = 0;
1047        result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
1048        if (result < 0) {
1049                dev_warn(&dev->intf->dev,
1050                         "dvb_dmxdev_init failed (errno = %d)\n",
1051                         result);
1052                goto fail_dmxdev;
1053        }
1054
1055        dvb->fe_hw.source = DMX_FRONTEND_0;
1056        result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
1057        if (result < 0) {
1058                dev_warn(&dev->intf->dev,
1059                         "add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
1060                         result);
1061                goto fail_fe_hw;
1062        }
1063
1064        dvb->fe_mem.source = DMX_MEMORY_FE;
1065        result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
1066        if (result < 0) {
1067                dev_warn(&dev->intf->dev,
1068                         "add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
1069                         result);
1070                goto fail_fe_mem;
1071        }
1072
1073        result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
1074        if (result < 0) {
1075                dev_warn(&dev->intf->dev,
1076                         "connect_frontend failed (errno = %d)\n",
1077                         result);
1078                goto fail_fe_conn;
1079        }
1080
1081        /* register network adapter */
1082        dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
1083
1084        /* If the analog part won't create RF connectors, DVB will do it */
1085        if (!dev->has_video || dev->tuner_type == TUNER_ABSENT)
1086                create_rf_connector = true;
1087
1088        result = dvb_create_media_graph(&dvb->adapter, create_rf_connector);
1089        if (result < 0)
1090                goto fail_create_graph;
1091
1092        return 0;
1093
1094fail_create_graph:
1095        dvb_net_release(&dvb->net);
1096fail_fe_conn:
1097        dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
1098fail_fe_mem:
1099        dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
1100fail_fe_hw:
1101        dvb_dmxdev_release(&dvb->dmxdev);
1102fail_dmxdev:
1103        dvb_dmx_release(&dvb->demux);
1104fail_dmx:
1105        if (dvb->fe[1])
1106                dvb_unregister_frontend(dvb->fe[1]);
1107        dvb_unregister_frontend(dvb->fe[0]);
1108fail_frontend1:
1109        if (dvb->fe[1])
1110                dvb_frontend_detach(dvb->fe[1]);
1111fail_frontend0:
1112        dvb_frontend_detach(dvb->fe[0]);
1113        dvb_unregister_adapter(&dvb->adapter);
1114fail_adapter:
1115        return result;
1116}
1117
1118static void em28xx_unregister_dvb(struct em28xx_dvb *dvb)
1119{
1120        dvb_net_release(&dvb->net);
1121        dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
1122        dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
1123        dvb_dmxdev_release(&dvb->dmxdev);
1124        dvb_dmx_release(&dvb->demux);
1125        if (dvb->fe[1])
1126                dvb_unregister_frontend(dvb->fe[1]);
1127        dvb_unregister_frontend(dvb->fe[0]);
1128        if (dvb->fe[1] && !dvb->dont_attach_fe1)
1129                dvb_frontend_detach(dvb->fe[1]);
1130        dvb_frontend_detach(dvb->fe[0]);
1131        dvb_unregister_adapter(&dvb->adapter);
1132}
1133
1134static int em28174_dvb_init_pctv_460e(struct em28xx *dev)
1135{
1136        struct em28xx_dvb *dvb = dev->dvb;
1137        struct tda10071_platform_data tda10071_pdata = {};
1138        struct a8293_platform_data a8293_pdata = {};
1139
1140        /* attach demod + tuner combo */
1141        tda10071_pdata.clk = 40444000; /* 40.444 MHz */
1142        tda10071_pdata.i2c_wr_max = 64;
1143        tda10071_pdata.ts_mode = TDA10071_TS_SERIAL;
1144        tda10071_pdata.pll_multiplier = 20;
1145        tda10071_pdata.tuner_i2c_addr = 0x14;
1146
1147        dvb->i2c_client_demod = dvb_module_probe("tda10071", "tda10071_cx24118",
1148                                                 &dev->i2c_adap[dev->def_i2c_bus],
1149                                                 0x55, &tda10071_pdata);
1150        if (!dvb->i2c_client_demod)
1151                return -ENODEV;
1152
1153        dvb->fe[0] = tda10071_pdata.get_dvb_frontend(dvb->i2c_client_demod);
1154
1155        /* attach SEC */
1156        a8293_pdata.dvb_frontend = dvb->fe[0];
1157
1158        dvb->i2c_client_sec = dvb_module_probe("a8293", NULL,
1159                                               &dev->i2c_adap[dev->def_i2c_bus],
1160                                               0x08, &a8293_pdata);
1161        if (!dvb->i2c_client_sec) {
1162                dvb_module_release(dvb->i2c_client_demod);
1163                return -ENODEV;
1164        }
1165
1166        return 0;
1167}
1168
1169static int em28178_dvb_init_pctv_461e(struct em28xx *dev)
1170{
1171        struct em28xx_dvb *dvb = dev->dvb;
1172        struct i2c_adapter *i2c_adapter;
1173        struct m88ds3103_platform_data m88ds3103_pdata = {};
1174        struct ts2020_config ts2020_config = {};
1175        struct a8293_platform_data a8293_pdata = {};
1176
1177        /* attach demod */
1178        m88ds3103_pdata.clk = 27000000;
1179        m88ds3103_pdata.i2c_wr_max = 33;
1180        m88ds3103_pdata.ts_mode = M88DS3103_TS_PARALLEL;
1181        m88ds3103_pdata.ts_clk = 16000;
1182        m88ds3103_pdata.ts_clk_pol = 1;
1183        m88ds3103_pdata.agc = 0x99;
1184
1185        dvb->i2c_client_demod = dvb_module_probe("m88ds3103", NULL,
1186                                                 &dev->i2c_adap[dev->def_i2c_bus],
1187                                                 0x68, &m88ds3103_pdata);
1188        if (!dvb->i2c_client_demod)
1189                return -ENODEV;
1190
1191        dvb->fe[0] = m88ds3103_pdata.get_dvb_frontend(dvb->i2c_client_demod);
1192        i2c_adapter = m88ds3103_pdata.get_i2c_adapter(dvb->i2c_client_demod);
1193
1194        /* attach tuner */
1195        ts2020_config.fe = dvb->fe[0];
1196
1197        dvb->i2c_client_tuner = dvb_module_probe("ts2020", "ts2022",
1198                                                 i2c_adapter,
1199                                                 0x60, &ts2020_config);
1200        if (!dvb->i2c_client_tuner) {
1201                dvb_module_release(dvb->i2c_client_demod);
1202                return -ENODEV;
1203        }
1204
1205        /* delegate signal strength measurement to tuner */
1206        dvb->fe[0]->ops.read_signal_strength =
1207                        dvb->fe[0]->ops.tuner_ops.get_rf_strength;
1208
1209        /* attach SEC */
1210        a8293_pdata.dvb_frontend = dvb->fe[0];
1211        dvb->i2c_client_sec = dvb_module_probe("a8293", NULL,
1212                                               &dev->i2c_adap[dev->def_i2c_bus],
1213                                               0x08, &a8293_pdata);
1214        if (!dvb->i2c_client_sec) {
1215                dvb_module_release(dvb->i2c_client_tuner);
1216                dvb_module_release(dvb->i2c_client_demod);
1217                return -ENODEV;
1218        }
1219
1220        return 0;
1221}
1222
1223static int em28178_dvb_init_pctv_461e_v2(struct em28xx *dev)
1224{
1225        struct em28xx_dvb *dvb = dev->dvb;
1226        struct i2c_adapter *i2c_adapter;
1227        struct m88ds3103_platform_data m88ds3103_pdata = {};
1228        struct ts2020_config ts2020_config = {};
1229        struct a8293_platform_data a8293_pdata = {};
1230
1231        /* attach demod */
1232        m88ds3103_pdata.clk = 27000000;
1233        m88ds3103_pdata.i2c_wr_max = 33;
1234        m88ds3103_pdata.ts_mode = M88DS3103_TS_PARALLEL;
1235        m88ds3103_pdata.ts_clk = 16000;
1236        m88ds3103_pdata.ts_clk_pol = 0;
1237        m88ds3103_pdata.agc = 0x99;
1238        m88ds3103_pdata.agc_inv = 0;
1239        m88ds3103_pdata.spec_inv = 0;
1240        dvb->i2c_client_demod = dvb_module_probe("m88ds3103", "m88ds3103b",
1241                                                 &dev->i2c_adap[dev->def_i2c_bus],
1242                                                 0x6a, &m88ds3103_pdata);
1243
1244        if (!dvb->i2c_client_demod)
1245                return -ENODEV;
1246
1247        dvb->fe[0] = m88ds3103_pdata.get_dvb_frontend(dvb->i2c_client_demod);
1248        i2c_adapter = m88ds3103_pdata.get_i2c_adapter(dvb->i2c_client_demod);
1249
1250        /* attach tuner */
1251        ts2020_config.fe = dvb->fe[0];
1252        dvb->i2c_client_tuner = dvb_module_probe("ts2020", "ts2022",
1253                                                 i2c_adapter,
1254                                                 0x60, &ts2020_config);
1255        if (!dvb->i2c_client_tuner) {
1256                dvb_module_release(dvb->i2c_client_demod);
1257                return -ENODEV;
1258        }
1259
1260        /* delegate signal strength measurement to tuner */
1261        dvb->fe[0]->ops.read_signal_strength =
1262                        dvb->fe[0]->ops.tuner_ops.get_rf_strength;
1263
1264        /* attach SEC */
1265        a8293_pdata.dvb_frontend = dvb->fe[0];
1266        dvb->i2c_client_sec = dvb_module_probe("a8293", NULL,
1267                                               &dev->i2c_adap[dev->def_i2c_bus],
1268                                               0x08, &a8293_pdata);
1269        if (!dvb->i2c_client_sec) {
1270                dvb_module_release(dvb->i2c_client_tuner);
1271                dvb_module_release(dvb->i2c_client_demod);
1272                return -ENODEV;
1273        }
1274
1275        return 0;
1276}
1277
1278static int em28178_dvb_init_pctv_292e(struct em28xx *dev)
1279{
1280        struct em28xx_dvb *dvb = dev->dvb;
1281        struct i2c_adapter *adapter;
1282        struct si2168_config si2168_config = {};
1283        struct si2157_config si2157_config = {};
1284
1285        /* attach demod */
1286        si2168_config.i2c_adapter = &adapter;
1287        si2168_config.fe = &dvb->fe[0];
1288        si2168_config.ts_mode = SI2168_TS_PARALLEL;
1289        si2168_config.spectral_inversion = true;
1290
1291        dvb->i2c_client_demod = dvb_module_probe("si2168", NULL,
1292                                                 &dev->i2c_adap[dev->def_i2c_bus],
1293                                                 0x64, &si2168_config);
1294        if (!dvb->i2c_client_demod)
1295                return -ENODEV;
1296
1297        /* attach tuner */
1298        si2157_config.fe = dvb->fe[0];
1299        si2157_config.if_port = 1;
1300#ifdef CONFIG_MEDIA_CONTROLLER_DVB
1301        si2157_config.mdev = dev->media_dev;
1302#endif
1303        dvb->i2c_client_tuner = dvb_module_probe("si2157", NULL,
1304                                                 adapter,
1305                                                 0x60, &si2157_config);
1306        if (!dvb->i2c_client_tuner) {
1307                dvb_module_release(dvb->i2c_client_demod);
1308                return -ENODEV;
1309        }
1310        dvb->fe[0]->ops.set_lna = em28xx_pctv_292e_set_lna;
1311
1312        return 0;
1313}
1314
1315static int em28178_dvb_init_terratec_t2_stick_hd(struct em28xx *dev)
1316{
1317        struct em28xx_dvb *dvb = dev->dvb;
1318        struct i2c_adapter *adapter;
1319        struct si2168_config si2168_config = {};
1320        struct si2157_config si2157_config = {};
1321
1322        /* attach demod */
1323        si2168_config.i2c_adapter = &adapter;
1324        si2168_config.fe = &dvb->fe[0];
1325        si2168_config.ts_mode = SI2168_TS_PARALLEL;
1326
1327        dvb->i2c_client_demod = dvb_module_probe("si2168", NULL,
1328                                                 &dev->i2c_adap[dev->def_i2c_bus],
1329                                                 0x64, &si2168_config);
1330        if (!dvb->i2c_client_demod)
1331                return -ENODEV;
1332
1333        /* attach tuner */
1334        memset(&si2157_config, 0, sizeof(si2157_config));
1335        si2157_config.fe = dvb->fe[0];
1336        si2157_config.if_port = 0;
1337#ifdef CONFIG_MEDIA_CONTROLLER_DVB
1338        si2157_config.mdev = dev->media_dev;
1339#endif
1340        dvb->i2c_client_tuner = dvb_module_probe("si2157", "si2146",
1341                                                 adapter,
1342                                                 0x60, &si2157_config);
1343        if (!dvb->i2c_client_tuner) {
1344                dvb_module_release(dvb->i2c_client_demod);
1345                return -ENODEV;
1346        }
1347
1348        return 0;
1349}
1350
1351static int em28178_dvb_init_plex_px_bcud(struct em28xx *dev)
1352{
1353        struct em28xx_dvb *dvb = dev->dvb;
1354        struct tc90522_config tc90522_config = {};
1355        struct qm1d1c0042_config qm1d1c0042_config = {};
1356
1357        /* attach demod */
1358        dvb->i2c_client_demod = dvb_module_probe("tc90522", "tc90522sat",
1359                                                 &dev->i2c_adap[dev->def_i2c_bus],
1360                                                 0x15, &tc90522_config);
1361        if (!dvb->i2c_client_demod)
1362                return -ENODEV;
1363
1364        /* attach tuner */
1365        qm1d1c0042_config.fe = tc90522_config.fe;
1366        qm1d1c0042_config.lpf = 1;
1367
1368        dvb->i2c_client_tuner = dvb_module_probe("qm1d1c0042", NULL,
1369                                                 tc90522_config.tuner_i2c,
1370                                                 0x61, &qm1d1c0042_config);
1371        if (!dvb->i2c_client_tuner) {
1372                dvb_module_release(dvb->i2c_client_demod);
1373                return -ENODEV;
1374        }
1375
1376        dvb->fe[0] = tc90522_config.fe;
1377        px_bcud_init(dev);
1378
1379        return 0;
1380}
1381
1382static int em28174_dvb_init_hauppauge_wintv_dualhd_dvb(struct em28xx *dev)
1383{
1384        struct em28xx_dvb *dvb = dev->dvb;
1385        struct i2c_adapter *adapter;
1386        struct si2168_config si2168_config = {};
1387        struct si2157_config si2157_config = {};
1388        unsigned char addr;
1389
1390        /* attach demod */
1391        si2168_config.i2c_adapter = &adapter;
1392        si2168_config.fe = &dvb->fe[0];
1393        si2168_config.ts_mode = SI2168_TS_SERIAL;
1394        si2168_config.spectral_inversion = true;
1395        addr = (dev->ts == PRIMARY_TS) ? 0x64 : 0x67;
1396
1397        dvb->i2c_client_demod = dvb_module_probe("si2168", NULL,
1398                                                 &dev->i2c_adap[dev->def_i2c_bus],
1399                                                 addr, &si2168_config);
1400        if (!dvb->i2c_client_demod)
1401                return -ENODEV;
1402
1403        /* attach tuner */
1404        memset(&si2157_config, 0, sizeof(si2157_config));
1405        si2157_config.fe = dvb->fe[0];
1406        si2157_config.if_port = 1;
1407#ifdef CONFIG_MEDIA_CONTROLLER_DVB
1408        si2157_config.mdev = dev->media_dev;
1409#endif
1410        addr = (dev->ts == PRIMARY_TS) ? 0x60 : 0x63;
1411
1412        dvb->i2c_client_tuner = dvb_module_probe("si2157", NULL,
1413                                                 adapter,
1414                                                 addr, &si2157_config);
1415        if (!dvb->i2c_client_tuner) {
1416                dvb_module_release(dvb->i2c_client_demod);
1417                return -ENODEV;
1418        }
1419
1420        return 0;
1421}
1422
1423static int em28174_dvb_init_hauppauge_wintv_dualhd_01595(struct em28xx *dev)
1424{
1425        struct em28xx_dvb *dvb = dev->dvb;
1426        struct i2c_adapter *adapter;
1427        struct lgdt3306a_config lgdt3306a_config =  {};
1428        struct si2157_config si2157_config = {};
1429        unsigned char addr;
1430
1431        /* attach demod */
1432        lgdt3306a_config = hauppauge_01595_lgdt3306a_config;
1433        lgdt3306a_config.fe = &dvb->fe[0];
1434        lgdt3306a_config.i2c_adapter = &adapter;
1435        addr = (dev->ts == PRIMARY_TS) ? 0x59 : 0x0e;
1436
1437        dvb->i2c_client_demod = dvb_module_probe("lgdt3306a", NULL,
1438                                                 &dev->i2c_adap[dev->def_i2c_bus],
1439                                                 addr, &lgdt3306a_config);
1440        if (!dvb->i2c_client_demod)
1441                return -ENODEV;
1442
1443        /* attach tuner */
1444        si2157_config.fe = dvb->fe[0];
1445        si2157_config.if_port = 1;
1446        si2157_config.inversion = 1;
1447#ifdef CONFIG_MEDIA_CONTROLLER_DVB
1448        si2157_config.mdev = dev->media_dev;
1449#endif
1450        addr = (dev->ts == PRIMARY_TS) ? 0x60 : 0x62;
1451
1452        dvb->i2c_client_tuner = dvb_module_probe("si2157", NULL,
1453                                                 adapter,
1454                                                 addr, &si2157_config);
1455        if (!dvb->i2c_client_tuner) {
1456                dvb_module_release(dvb->i2c_client_demod);
1457                return -ENODEV;
1458        }
1459
1460        return 0;
1461}
1462
1463static int em2874_dvb_init_hauppauge_usb_quadhd(struct em28xx *dev)
1464{
1465        struct em28xx_dvb *dvb = dev->dvb;
1466        struct mxl692_config mxl692_config = {};
1467        unsigned char addr;
1468
1469        /* attach demod/tuner combo */
1470        mxl692_config.id = (dev->ts == PRIMARY_TS) ? 0 : 1;
1471        mxl692_config.fe = &dvb->fe[0];
1472        addr = (dev->ts == PRIMARY_TS) ? 0x60 : 0x63;
1473
1474        dvb->i2c_client_demod = dvb_module_probe("mxl692", NULL,
1475                                                 &dev->i2c_adap[dev->def_i2c_bus],
1476                                                 addr, &mxl692_config);
1477        if (!dvb->i2c_client_demod)
1478                return -ENODEV;
1479
1480        return 0;
1481}
1482
1483static int em28xx_dvb_init(struct em28xx *dev)
1484{
1485        int result = 0, dvb_alt = 0;
1486        struct em28xx_dvb *dvb;
1487        struct usb_device *udev;
1488
1489        if (dev->is_audio_only) {
1490                /* Shouldn't initialize IR for this interface */
1491                return 0;
1492        }
1493
1494        if (!dev->board.has_dvb) {
1495                /* This device does not support the extension */
1496                return 0;
1497        }
1498
1499        dev_info(&dev->intf->dev, "Binding DVB extension\n");
1500
1501        dvb = kzalloc(sizeof(*dvb), GFP_KERNEL);
1502        if (!dvb)
1503                return -ENOMEM;
1504
1505        dev->dvb = dvb;
1506        dvb->fe[0] = NULL;
1507        dvb->fe[1] = NULL;
1508
1509        /* pre-allocate DVB usb transfer buffers */
1510        if (dev->dvb_xfer_bulk) {
1511                result = em28xx_alloc_urbs(dev, EM28XX_DIGITAL_MODE,
1512                                           dev->dvb_xfer_bulk,
1513                                           EM28XX_DVB_NUM_BUFS,
1514                                           512,
1515                                           EM28XX_DVB_BULK_PACKET_MULTIPLIER);
1516        } else {
1517                result = em28xx_alloc_urbs(dev, EM28XX_DIGITAL_MODE,
1518                                           dev->dvb_xfer_bulk,
1519                                           EM28XX_DVB_NUM_BUFS,
1520                                           dev->dvb_max_pkt_size_isoc,
1521                                           EM28XX_DVB_NUM_ISOC_PACKETS);
1522        }
1523        if (result) {
1524                dev_err(&dev->intf->dev,
1525                        "failed to pre-allocate USB transfer buffers for DVB.\n");
1526                kfree(dvb);
1527                dev->dvb = NULL;
1528                return result;
1529        }
1530
1531        mutex_lock(&dev->lock);
1532        em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
1533        /* init frontend */
1534        switch (dev->model) {
1535        case EM2874_BOARD_LEADERSHIP_ISDBT:
1536                dvb->fe[0] = dvb_attach(s921_attach,
1537                                        &sharp_isdbt,
1538                                        &dev->i2c_adap[dev->def_i2c_bus]);
1539
1540                if (!dvb->fe[0]) {
1541                        result = -EINVAL;
1542                        goto out_free;
1543                }
1544
1545                break;
1546        case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_850:
1547        case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_950:
1548        case EM2880_BOARD_PINNACLE_PCTV_HD_PRO:
1549        case EM2880_BOARD_AMD_ATI_TV_WONDER_HD_600:
1550                dvb->fe[0] = dvb_attach(lgdt330x_attach,
1551                                        &em2880_lgdt3303_dev,
1552                                        0x0e,
1553                                        &dev->i2c_adap[dev->def_i2c_bus]);
1554                if (em28xx_attach_xc3028(0x61, dev) < 0) {
1555                        result = -EINVAL;
1556                        goto out_free;
1557                }
1558                break;
1559        case EM2880_BOARD_KWORLD_DVB_310U:
1560                dvb->fe[0] = dvb_attach(zl10353_attach,
1561                                        &em28xx_zl10353_with_xc3028,
1562                                        &dev->i2c_adap[dev->def_i2c_bus]);
1563                if (em28xx_attach_xc3028(0x61, dev) < 0) {
1564                        result = -EINVAL;
1565                        goto out_free;
1566                }
1567                break;
1568        case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900:
1569        case EM2882_BOARD_TERRATEC_HYBRID_XS:
1570        case EM2880_BOARD_EMPIRE_DUAL_TV:
1571        case EM2882_BOARD_ZOLID_HYBRID_TV_STICK:
1572                dvb->fe[0] = dvb_attach(zl10353_attach,
1573                                        &em28xx_zl10353_xc3028_no_i2c_gate,
1574                                        &dev->i2c_adap[dev->def_i2c_bus]);
1575                if (em28xx_attach_xc3028(0x61, dev) < 0) {
1576                        result = -EINVAL;
1577                        goto out_free;
1578                }
1579                break;
1580        case EM2880_BOARD_TERRATEC_HYBRID_XS:
1581        case EM2880_BOARD_TERRATEC_HYBRID_XS_FR:
1582        case EM2881_BOARD_PINNACLE_HYBRID_PRO:
1583        case EM2882_BOARD_DIKOM_DK300:
1584        case EM2882_BOARD_KWORLD_VS_DVBT:
1585                /*
1586                 * Those boards could have either a zl10353 or a mt352.
1587                 * If the chip id isn't for zl10353, try mt352.
1588                 */
1589                dvb->fe[0] = dvb_attach(zl10353_attach,
1590                                        &em28xx_zl10353_xc3028_no_i2c_gate,
1591                                        &dev->i2c_adap[dev->def_i2c_bus]);
1592                if (!dvb->fe[0])
1593                        dvb->fe[0] = dvb_attach(mt352_attach,
1594                                                &terratec_xs_mt352_cfg,
1595                                                &dev->i2c_adap[dev->def_i2c_bus]);
1596
1597                if (em28xx_attach_xc3028(0x61, dev) < 0) {
1598                        result = -EINVAL;
1599                        goto out_free;
1600                }
1601                break;
1602        case EM2870_BOARD_TERRATEC_XS_MT2060:
1603                dvb->fe[0] = dvb_attach(zl10353_attach,
1604                                        &em28xx_zl10353_no_i2c_gate_dev,
1605                                        &dev->i2c_adap[dev->def_i2c_bus]);
1606                if (dvb->fe[0]) {
1607                        dvb_attach(mt2060_attach, dvb->fe[0],
1608                                   &dev->i2c_adap[dev->def_i2c_bus],
1609                                   &em28xx_mt2060_config, 1220);
1610                }
1611                break;
1612        case EM2870_BOARD_KWORLD_355U:
1613                dvb->fe[0] = dvb_attach(zl10353_attach,
1614                                        &em28xx_zl10353_no_i2c_gate_dev,
1615                                        &dev->i2c_adap[dev->def_i2c_bus]);
1616                if (dvb->fe[0])
1617                        dvb_attach(qt1010_attach, dvb->fe[0],
1618                                   &dev->i2c_adap[dev->def_i2c_bus],
1619                                   &em28xx_qt1010_config);
1620                break;
1621        case EM2883_BOARD_KWORLD_HYBRID_330U:
1622        case EM2882_BOARD_EVGA_INDTUBE:
1623                dvb->fe[0] = dvb_attach(s5h1409_attach,
1624                                        &em28xx_s5h1409_with_xc3028,
1625                                        &dev->i2c_adap[dev->def_i2c_bus]);
1626                if (em28xx_attach_xc3028(0x61, dev) < 0) {
1627                        result = -EINVAL;
1628                        goto out_free;
1629                }
1630                break;
1631        case EM2882_BOARD_KWORLD_ATSC_315U:
1632                dvb->fe[0] = dvb_attach(lgdt330x_attach,
1633                                        &em2880_lgdt3303_dev,
1634                                        0x0e,
1635                                        &dev->i2c_adap[dev->def_i2c_bus]);
1636                if (dvb->fe[0]) {
1637                        if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
1638                                        &dev->i2c_adap[dev->def_i2c_bus],
1639                                        0x61, TUNER_THOMSON_DTT761X)) {
1640                                result = -EINVAL;
1641                                goto out_free;
1642                        }
1643                }
1644                break;
1645        case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900_R2:
1646        case EM2882_BOARD_PINNACLE_HYBRID_PRO_330E:
1647                dvb->fe[0] = dvb_attach(drxd_attach, &em28xx_drxd, NULL,
1648                                        &dev->i2c_adap[dev->def_i2c_bus],
1649                                        &dev->intf->dev);
1650                if (em28xx_attach_xc3028(0x61, dev) < 0) {
1651                        result = -EINVAL;
1652                        goto out_free;
1653                }
1654                break;
1655        case EM2870_BOARD_REDDO_DVB_C_USB_BOX:
1656                /* Philips CU1216L NIM (Philips TDA10023 + Infineon TUA6034) */
1657                dvb->fe[0] = dvb_attach(tda10023_attach,
1658                                        &em28xx_tda10023_config,
1659                                        &dev->i2c_adap[dev->def_i2c_bus],
1660                                        0x48);
1661                if (dvb->fe[0]) {
1662                        if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
1663                                        &dev->i2c_adap[dev->def_i2c_bus],
1664                                        0x60, TUNER_PHILIPS_CU1216L)) {
1665                                result = -EINVAL;
1666                                goto out_free;
1667                        }
1668                }
1669                break;
1670        case EM2870_BOARD_KWORLD_A340:
1671                dvb->fe[0] = dvb_attach(lgdt3305_attach,
1672                                        &em2870_lgdt3304_dev,
1673                                        &dev->i2c_adap[dev->def_i2c_bus]);
1674                if (!dvb->fe[0]) {
1675                        result = -EINVAL;
1676                        goto out_free;
1677                }
1678                if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1679                                &dev->i2c_adap[dev->def_i2c_bus],
1680                                &kworld_a340_config)) {
1681                        dvb_frontend_detach(dvb->fe[0]);
1682                        result = -EINVAL;
1683                        goto out_free;
1684                }
1685                break;
1686        case EM28174_BOARD_PCTV_290E:
1687                /* set default GPIO0 for LNA, used if GPIOLIB is undefined */
1688                dvb->lna_gpio = CXD2820R_GPIO_E | CXD2820R_GPIO_O |
1689                                CXD2820R_GPIO_L;
1690                dvb->fe[0] = dvb_attach(cxd2820r_attach,
1691                                        &em28xx_cxd2820r_config,
1692                                        &dev->i2c_adap[dev->def_i2c_bus],
1693                                        &dvb->lna_gpio);
1694                if (dvb->fe[0]) {
1695                        /* FE 0 attach tuner */
1696                        if (!dvb_attach(tda18271_attach,
1697                                        dvb->fe[0],
1698                                        0x60,
1699                                        &dev->i2c_adap[dev->def_i2c_bus],
1700                                        &em28xx_cxd2820r_tda18271_config)) {
1701                                dvb_frontend_detach(dvb->fe[0]);
1702                                result = -EINVAL;
1703                                goto out_free;
1704                        }
1705
1706#ifdef CONFIG_GPIOLIB
1707                        /* enable LNA for DVB-T, DVB-T2 and DVB-C */
1708                        result = gpio_request_one(dvb->lna_gpio,
1709                                                  GPIOF_OUT_INIT_LOW, NULL);
1710                        if (result)
1711                                dev_err(&dev->intf->dev,
1712                                        "gpio request failed %d\n",
1713                                        result);
1714                        else
1715                                gpio_free(dvb->lna_gpio);
1716
1717                        result = 0; /* continue even set LNA fails */
1718#endif
1719                        dvb->fe[0]->ops.set_lna = em28xx_pctv_290e_set_lna;
1720                }
1721
1722                break;
1723        case EM2884_BOARD_HAUPPAUGE_WINTV_HVR_930C:
1724        {
1725                struct xc5000_config cfg = {};
1726
1727                hauppauge_hvr930c_init(dev);
1728
1729                dvb->fe[0] = dvb_attach(drxk_attach,
1730                                        &hauppauge_930c_drxk,
1731                                        &dev->i2c_adap[dev->def_i2c_bus]);
1732                if (!dvb->fe[0]) {
1733                        result = -EINVAL;
1734                        goto out_free;
1735                }
1736                /* FIXME: do we need a pll semaphore? */
1737                dvb->fe[0]->sec_priv = dvb;
1738                sema_init(&dvb->pll_mutex, 1);
1739                dvb->gate_ctrl = dvb->fe[0]->ops.i2c_gate_ctrl;
1740                dvb->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
1741
1742                /* Attach xc5000 */
1743                cfg.i2c_address  = 0x61;
1744                cfg.if_khz = 4000;
1745
1746                if (dvb->fe[0]->ops.i2c_gate_ctrl)
1747                        dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 1);
1748                if (!dvb_attach(xc5000_attach, dvb->fe[0],
1749                                &dev->i2c_adap[dev->def_i2c_bus], &cfg)) {
1750                        result = -EINVAL;
1751                        goto out_free;
1752                }
1753                if (dvb->fe[0]->ops.i2c_gate_ctrl)
1754                        dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 0);
1755
1756                break;
1757        }
1758        case EM2884_BOARD_TERRATEC_H5:
1759                terratec_h5_init(dev);
1760
1761                dvb->fe[0] = dvb_attach(drxk_attach, &terratec_h5_drxk,
1762                                        &dev->i2c_adap[dev->def_i2c_bus]);
1763                if (!dvb->fe[0]) {
1764                        result = -EINVAL;
1765                        goto out_free;
1766                }
1767                /* FIXME: do we need a pll semaphore? */
1768                dvb->fe[0]->sec_priv = dvb;
1769                sema_init(&dvb->pll_mutex, 1);
1770                dvb->gate_ctrl = dvb->fe[0]->ops.i2c_gate_ctrl;
1771                dvb->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
1772
1773                /* Attach tda18271 to DVB-C frontend */
1774                if (dvb->fe[0]->ops.i2c_gate_ctrl)
1775                        dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 1);
1776                if (!dvb_attach(tda18271c2dd_attach, dvb->fe[0],
1777                                &dev->i2c_adap[dev->def_i2c_bus], 0x60)) {
1778                        result = -EINVAL;
1779                        goto out_free;
1780                }
1781                if (dvb->fe[0]->ops.i2c_gate_ctrl)
1782                        dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 0);
1783
1784                break;
1785        case EM2884_BOARD_C3TECH_DIGITAL_DUO:
1786                dvb->fe[0] = dvb_attach(mb86a20s_attach,
1787                                        &c3tech_duo_mb86a20s_config,
1788                                        &dev->i2c_adap[dev->def_i2c_bus]);
1789                if (dvb->fe[0])
1790                        dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1791                                   &dev->i2c_adap[dev->def_i2c_bus],
1792                                   &c3tech_duo_tda18271_config);
1793                break;
1794        case EM28174_BOARD_PCTV_460E:
1795                result = em28174_dvb_init_pctv_460e(dev);
1796                if (result)
1797                        goto out_free;
1798                break;
1799        case EM2874_BOARD_DELOCK_61959:
1800        case EM2874_BOARD_MAXMEDIA_UB425_TC:
1801                /* attach demodulator */
1802                dvb->fe[0] = dvb_attach(drxk_attach, &maxmedia_ub425_tc_drxk,
1803                                        &dev->i2c_adap[dev->def_i2c_bus]);
1804
1805                if (dvb->fe[0]) {
1806                        /* disable I2C-gate */
1807                        dvb->fe[0]->ops.i2c_gate_ctrl = NULL;
1808
1809                        /* attach tuner */
1810                        if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1811                                        &dev->i2c_adap[dev->def_i2c_bus],
1812                                        &em28xx_cxd2820r_tda18271_config)) {
1813                                dvb_frontend_detach(dvb->fe[0]);
1814                                result = -EINVAL;
1815                                goto out_free;
1816                        }
1817                }
1818                break;
1819        case EM2884_BOARD_PCTV_510E:
1820        case EM2884_BOARD_PCTV_520E:
1821                pctv_520e_init(dev);
1822
1823                /* attach demodulator */
1824                dvb->fe[0] = dvb_attach(drxk_attach, &pctv_520e_drxk,
1825                                        &dev->i2c_adap[dev->def_i2c_bus]);
1826
1827                if (dvb->fe[0]) {
1828                        /* attach tuner */
1829                        if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1830                                        &dev->i2c_adap[dev->def_i2c_bus],
1831                                        &em28xx_cxd2820r_tda18271_config)) {
1832                                dvb_frontend_detach(dvb->fe[0]);
1833                                result = -EINVAL;
1834                                goto out_free;
1835                        }
1836                }
1837                break;
1838        case EM2884_BOARD_ELGATO_EYETV_HYBRID_2008:
1839        case EM2884_BOARD_CINERGY_HTC_STICK:
1840        case EM2884_BOARD_TERRATEC_H6:
1841                terratec_htc_stick_init(dev);
1842
1843                /* attach demodulator */
1844                dvb->fe[0] = dvb_attach(drxk_attach, &terratec_htc_stick_drxk,
1845                                        &dev->i2c_adap[dev->def_i2c_bus]);
1846                if (!dvb->fe[0]) {
1847                        result = -EINVAL;
1848                        goto out_free;
1849                }
1850
1851                /* Attach the demodulator. */
1852                if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1853                                &dev->i2c_adap[dev->def_i2c_bus],
1854                                &em28xx_cxd2820r_tda18271_config)) {
1855                        result = -EINVAL;
1856                        goto out_free;
1857                }
1858                break;
1859        case EM2884_BOARD_TERRATEC_HTC_USB_XS:
1860                terratec_htc_usb_xs_init(dev);
1861
1862                /* attach demodulator */
1863                dvb->fe[0] = dvb_attach(drxk_attach, &terratec_htc_stick_drxk,
1864                                        &dev->i2c_adap[dev->def_i2c_bus]);
1865                if (!dvb->fe[0]) {
1866                        result = -EINVAL;
1867                        goto out_free;
1868                }
1869
1870                /* Attach the demodulator. */
1871                if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1872                                &dev->i2c_adap[dev->def_i2c_bus],
1873                                &em28xx_cxd2820r_tda18271_config)) {
1874                        result = -EINVAL;
1875                        goto out_free;
1876                }
1877                break;
1878        case EM2874_BOARD_KWORLD_UB435Q_V2:
1879                dvb->fe[0] = dvb_attach(lgdt3305_attach,
1880                                        &em2874_lgdt3305_dev,
1881                                        &dev->i2c_adap[dev->def_i2c_bus]);
1882                if (!dvb->fe[0]) {
1883                        result = -EINVAL;
1884                        goto out_free;
1885                }
1886
1887                /* Attach the demodulator. */
1888                if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1889                                &dev->i2c_adap[dev->def_i2c_bus],
1890                                &kworld_ub435q_v2_config)) {
1891                        result = -EINVAL;
1892                        goto out_free;
1893                }
1894                break;
1895        case EM2874_BOARD_KWORLD_UB435Q_V3:
1896        {
1897                struct i2c_adapter *adapter = &dev->i2c_adap[dev->def_i2c_bus];
1898
1899                dvb->fe[0] = dvb_attach(lgdt3305_attach,
1900                                        &em2874_lgdt3305_nogate_dev,
1901                                        &dev->i2c_adap[dev->def_i2c_bus]);
1902                if (!dvb->fe[0]) {
1903                        result = -EINVAL;
1904                        goto out_free;
1905                }
1906
1907                /* attach tuner */
1908                kworld_ub435q_v3_config.fe = dvb->fe[0];
1909
1910                dvb->i2c_client_tuner = dvb_module_probe("tda18212", NULL,
1911                                                         adapter, 0x60,
1912                                                         &kworld_ub435q_v3_config);
1913                if (!dvb->i2c_client_tuner) {
1914                        dvb_frontend_detach(dvb->fe[0]);
1915                        result = -ENODEV;
1916                        goto out_free;
1917                }
1918                break;
1919        }
1920        case EM2874_BOARD_PCTV_HD_MINI_80E:
1921                dvb->fe[0] = dvb_attach(drx39xxj_attach,
1922                                        &dev->i2c_adap[dev->def_i2c_bus]);
1923                if (dvb->fe[0]) {
1924                        dvb->fe[0] = dvb_attach(tda18271_attach, dvb->fe[0],
1925                                                0x60,
1926                                                &dev->i2c_adap[dev->def_i2c_bus],
1927                                                &pinnacle_80e_dvb_config);
1928                        if (!dvb->fe[0]) {
1929                                result = -EINVAL;
1930                                goto out_free;
1931                        }
1932                }
1933                break;
1934        case EM28178_BOARD_PCTV_461E:
1935                result = em28178_dvb_init_pctv_461e(dev);
1936                if (result)
1937                        goto out_free;
1938                break;
1939        case EM28178_BOARD_PCTV_461E_V2:
1940                result = em28178_dvb_init_pctv_461e_v2(dev);
1941                if (result)
1942                        goto out_free;
1943                break;
1944        case EM28178_BOARD_PCTV_292E:
1945                result = em28178_dvb_init_pctv_292e(dev);
1946                if (result)
1947                        goto out_free;
1948                break;
1949        case EM28178_BOARD_TERRATEC_T2_STICK_HD:
1950                result = em28178_dvb_init_terratec_t2_stick_hd(dev);
1951                if (result)
1952                        goto out_free;
1953                break;
1954        case EM28178_BOARD_PLEX_PX_BCUD:
1955                result = em28178_dvb_init_plex_px_bcud(dev);
1956                if (result)
1957                        goto out_free;
1958                break;
1959        case EM28174_BOARD_HAUPPAUGE_WINTV_DUALHD_DVB:
1960                result = em28174_dvb_init_hauppauge_wintv_dualhd_dvb(dev);
1961                if (result)
1962                        goto out_free;
1963                break;
1964        case EM28174_BOARD_HAUPPAUGE_WINTV_DUALHD_01595:
1965                result = em28174_dvb_init_hauppauge_wintv_dualhd_01595(dev);
1966                if (result)
1967                        goto out_free;
1968                break;
1969        case EM2874_BOARD_HAUPPAUGE_USB_QUADHD:
1970                result = em2874_dvb_init_hauppauge_usb_quadhd(dev);
1971                if (result)
1972                        goto out_free;
1973                break;
1974        default:
1975                dev_err(&dev->intf->dev,
1976                        "The frontend of your DVB/ATSC card isn't supported yet\n");
1977                break;
1978        }
1979        if (!dvb->fe[0]) {
1980                dev_err(&dev->intf->dev, "frontend initialization failed\n");
1981                result = -EINVAL;
1982                goto out_free;
1983        }
1984        /* define general-purpose callback pointer */
1985        dvb->fe[0]->callback = em28xx_tuner_callback;
1986        if (dvb->fe[1])
1987                dvb->fe[1]->callback = em28xx_tuner_callback;
1988
1989        /* register everything */
1990        result = em28xx_register_dvb(dvb, THIS_MODULE, dev, &dev->intf->dev);
1991
1992        if (result < 0)
1993                goto out_free;
1994
1995        if (dev->dvb_xfer_bulk) {
1996                dvb_alt = 0;
1997        } else { /* isoc */
1998                dvb_alt = dev->dvb_alt_isoc;
1999        }
2000
2001        udev = interface_to_usbdev(dev->intf);
2002        usb_set_interface(udev, dev->ifnum, dvb_alt);
2003        dev_info(&dev->intf->dev, "DVB extension successfully initialized\n");
2004
2005        kref_get(&dev->ref);
2006
2007ret:
2008        em28xx_set_mode(dev, EM28XX_SUSPEND);
2009        mutex_unlock(&dev->lock);
2010        return result;
2011
2012out_free:
2013        em28xx_uninit_usb_xfer(dev, EM28XX_DIGITAL_MODE);
2014        kfree(dvb);
2015        dev->dvb = NULL;
2016        goto ret;
2017}
2018
2019static inline void prevent_sleep(struct dvb_frontend_ops *ops)
2020{
2021        ops->set_voltage = NULL;
2022        ops->sleep = NULL;
2023        ops->tuner_ops.sleep = NULL;
2024}
2025
2026static int em28xx_dvb_fini(struct em28xx *dev)
2027{
2028        struct em28xx_dvb *dvb;
2029
2030        if (dev->is_audio_only) {
2031                /* Shouldn't initialize IR for this interface */
2032                return 0;
2033        }
2034
2035        if (!dev->board.has_dvb) {
2036                /* This device does not support the extension */
2037                return 0;
2038        }
2039
2040        if (!dev->dvb)
2041                return 0;
2042
2043        dev_info(&dev->intf->dev, "Closing DVB extension\n");
2044
2045        dvb = dev->dvb;
2046
2047        em28xx_uninit_usb_xfer(dev, EM28XX_DIGITAL_MODE);
2048
2049        if (dev->disconnected) {
2050                /*
2051                 * We cannot tell the device to sleep
2052                 * once it has been unplugged.
2053                 */
2054                if (dvb->fe[0]) {
2055                        prevent_sleep(&dvb->fe[0]->ops);
2056                        dvb->fe[0]->exit = DVB_FE_DEVICE_REMOVED;
2057                }
2058                if (dvb->fe[1]) {
2059                        prevent_sleep(&dvb->fe[1]->ops);
2060                        dvb->fe[1]->exit = DVB_FE_DEVICE_REMOVED;
2061                }
2062        }
2063
2064        em28xx_unregister_dvb(dvb);
2065
2066        /* release I2C module bindings */
2067        dvb_module_release(dvb->i2c_client_sec);
2068        dvb_module_release(dvb->i2c_client_tuner);
2069        dvb_module_release(dvb->i2c_client_demod);
2070
2071        kfree(dvb);
2072        dev->dvb = NULL;
2073        kref_put(&dev->ref, em28xx_free_device);
2074
2075        return 0;
2076}
2077
2078static int em28xx_dvb_suspend(struct em28xx *dev)
2079{
2080        int ret = 0;
2081
2082        if (dev->is_audio_only)
2083                return 0;
2084
2085        if (!dev->board.has_dvb)
2086                return 0;
2087
2088        dev_info(&dev->intf->dev, "Suspending DVB extension\n");
2089        if (dev->dvb) {
2090                struct em28xx_dvb *dvb = dev->dvb;
2091
2092                if (dvb->fe[0]) {
2093                        ret = dvb_frontend_suspend(dvb->fe[0]);
2094                        dev_info(&dev->intf->dev, "fe0 suspend %d\n", ret);
2095                }
2096                if (dvb->fe[1]) {
2097                        dvb_frontend_suspend(dvb->fe[1]);
2098                        dev_info(&dev->intf->dev, "fe1 suspend %d\n", ret);
2099                }
2100        }
2101
2102        return 0;
2103}
2104
2105static int em28xx_dvb_resume(struct em28xx *dev)
2106{
2107        int ret = 0;
2108
2109        if (dev->is_audio_only)
2110                return 0;
2111
2112        if (!dev->board.has_dvb)
2113                return 0;
2114
2115        dev_info(&dev->intf->dev, "Resuming DVB extension\n");
2116        if (dev->dvb) {
2117                struct em28xx_dvb *dvb = dev->dvb;
2118
2119                if (dvb->fe[0]) {
2120                        ret = dvb_frontend_resume(dvb->fe[0]);
2121                        dev_info(&dev->intf->dev, "fe0 resume %d\n", ret);
2122                }
2123
2124                if (dvb->fe[1]) {
2125                        ret = dvb_frontend_resume(dvb->fe[1]);
2126                        dev_info(&dev->intf->dev, "fe1 resume %d\n", ret);
2127                }
2128        }
2129
2130        return 0;
2131}
2132
2133static struct em28xx_ops dvb_ops = {
2134        .id   = EM28XX_DVB,
2135        .name = "Em28xx dvb Extension",
2136        .init = em28xx_dvb_init,
2137        .fini = em28xx_dvb_fini,
2138        .suspend = em28xx_dvb_suspend,
2139        .resume = em28xx_dvb_resume,
2140};
2141
2142static int __init em28xx_dvb_register(void)
2143{
2144        return em28xx_register_extension(&dvb_ops);
2145}
2146
2147static void __exit em28xx_dvb_unregister(void)
2148{
2149        em28xx_unregister_extension(&dvb_ops);
2150}
2151
2152module_init(em28xx_dvb_register);
2153module_exit(em28xx_dvb_unregister);
2154