Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 //
0003 // DVB device driver for em28xx
0004 //
0005 // (c) 2008-2011 Mauro Carvalho Chehab <mchehab@kernel.org>
0006 //
0007 // (c) 2008 Devin Heitmueller <devin.heitmueller@gmail.com>
0008 //  - Fixes for the driver to properly work with HVR-950
0009 //  - Fixes for the driver to properly work with Pinnacle PCTV HD Pro Stick
0010 //  - Fixes for the driver to properly work with AMD ATI TV Wonder HD 600
0011 //
0012 // (c) 2008 Aidan Thornton <makosoft@googlemail.com>
0013 //
0014 // (c) 2012 Frank Schäfer <fschaefer.oss@googlemail.com>
0015 //
0016 // Based on cx88-dvb, saa7134-dvb and videobuf-dvb originally written by:
0017 //  (c) 2004, 2005 Chris Pascoe <c.pascoe@itee.uq.edu.au>
0018 //  (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
0019 
0020 #include "em28xx.h"
0021 
0022 #include <linux/kernel.h>
0023 #include <linux/slab.h>
0024 #include <linux/usb.h>
0025 
0026 #include <media/v4l2-common.h>
0027 #include <media/dvb_demux.h>
0028 #include <media/dvb_net.h>
0029 #include <media/dmxdev.h>
0030 #include <media/tuner.h>
0031 #include "tuner-simple.h"
0032 #include <linux/gpio.h>
0033 
0034 #include "lgdt330x.h"
0035 #include "lgdt3305.h"
0036 #include "lgdt3306a.h"
0037 #include "zl10353.h"
0038 #include "s5h1409.h"
0039 #include "mt2060.h"
0040 #include "mt352.h"
0041 #include "mt352_priv.h" /* FIXME */
0042 #include "tda1002x.h"
0043 #include "drx39xyj/drx39xxj.h"
0044 #include "tda18271.h"
0045 #include "s921.h"
0046 #include "drxd.h"
0047 #include "cxd2820r.h"
0048 #include "tda18271c2dd.h"
0049 #include "drxk.h"
0050 #include "tda10071.h"
0051 #include "tda18212.h"
0052 #include "a8293.h"
0053 #include "qt1010.h"
0054 #include "mb86a20s.h"
0055 #include "m88ds3103.h"
0056 #include "ts2020.h"
0057 #include "si2168.h"
0058 #include "si2157.h"
0059 #include "tc90522.h"
0060 #include "qm1d1c0042.h"
0061 #include "mxl692.h"
0062 
0063 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@kernel.org>");
0064 MODULE_LICENSE("GPL v2");
0065 MODULE_DESCRIPTION(DRIVER_DESC " - digital TV interface");
0066 MODULE_VERSION(EM28XX_VERSION);
0067 
0068 static unsigned int debug;
0069 module_param(debug, int, 0644);
0070 MODULE_PARM_DESC(debug, "enable debug messages [dvb]");
0071 
0072 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
0073 
0074 #define dprintk(level, fmt, arg...) do {                \
0075     if (debug >= level)                     \
0076         dev_printk(KERN_DEBUG, &dev->intf->dev,         \
0077                "dvb: " fmt, ## arg);            \
0078 } while (0)
0079 
0080 struct em28xx_dvb {
0081     struct dvb_frontend        *fe[2];
0082 
0083     /* feed count management */
0084     struct mutex               lock;
0085     int                        nfeeds;
0086 
0087     /* general boilerplate stuff */
0088     struct dvb_adapter         adapter;
0089     struct dvb_demux           demux;
0090     struct dmxdev              dmxdev;
0091     struct dmx_frontend        fe_hw;
0092     struct dmx_frontend        fe_mem;
0093     struct dvb_net             net;
0094 
0095     /* Due to DRX-K - probably need changes */
0096     int (*gate_ctrl)(struct dvb_frontend *fe, int gate);
0097     struct semaphore      pll_mutex;
0098     bool            dont_attach_fe1;
0099     int         lna_gpio;
0100     struct i2c_client   *i2c_client_demod;
0101     struct i2c_client   *i2c_client_tuner;
0102     struct i2c_client   *i2c_client_sec;
0103 };
0104 
0105 static inline void print_err_status(struct em28xx *dev,
0106                     int packet, int status)
0107 {
0108     char *errmsg = "Unknown";
0109 
0110     switch (status) {
0111     case -ENOENT:
0112         errmsg = "unlinked synchronously";
0113         break;
0114     case -ECONNRESET:
0115         errmsg = "unlinked asynchronously";
0116         break;
0117     case -ENOSR:
0118         errmsg = "Buffer error (overrun)";
0119         break;
0120     case -EPIPE:
0121         errmsg = "Stalled (device not responding)";
0122         break;
0123     case -EOVERFLOW:
0124         errmsg = "Babble (bad cable?)";
0125         break;
0126     case -EPROTO:
0127         errmsg = "Bit-stuff error (bad cable?)";
0128         break;
0129     case -EILSEQ:
0130         errmsg = "CRC/Timeout (could be anything)";
0131         break;
0132     case -ETIME:
0133         errmsg = "Device does not respond";
0134         break;
0135     }
0136     if (packet < 0) {
0137         dprintk(1, "URB status %d [%s].\n", status, errmsg);
0138     } else {
0139         dprintk(1, "URB packet %d, status %d [%s].\n",
0140             packet, status, errmsg);
0141     }
0142 }
0143 
0144 static inline int em28xx_dvb_urb_data_copy(struct em28xx *dev, struct urb *urb)
0145 {
0146     int xfer_bulk, num_packets, i;
0147 
0148     if (!dev)
0149         return 0;
0150 
0151     if (dev->disconnected)
0152         return 0;
0153 
0154     if (urb->status < 0)
0155         print_err_status(dev, -1, urb->status);
0156 
0157     xfer_bulk = usb_pipebulk(urb->pipe);
0158 
0159     if (xfer_bulk) /* bulk */
0160         num_packets = 1;
0161     else /* isoc */
0162         num_packets = urb->number_of_packets;
0163 
0164     for (i = 0; i < num_packets; i++) {
0165         if (xfer_bulk) {
0166             if (urb->status < 0) {
0167                 print_err_status(dev, i, urb->status);
0168                 if (urb->status != -EPROTO)
0169                     continue;
0170             }
0171             if (!urb->actual_length)
0172                 continue;
0173             dvb_dmx_swfilter(&dev->dvb->demux, urb->transfer_buffer,
0174                      urb->actual_length);
0175         } else {
0176             if (urb->iso_frame_desc[i].status < 0) {
0177                 print_err_status(dev, i,
0178                          urb->iso_frame_desc[i].status);
0179                 if (urb->iso_frame_desc[i].status != -EPROTO)
0180                     continue;
0181             }
0182             if (!urb->iso_frame_desc[i].actual_length)
0183                 continue;
0184             dvb_dmx_swfilter(&dev->dvb->demux,
0185                      urb->transfer_buffer +
0186                      urb->iso_frame_desc[i].offset,
0187                      urb->iso_frame_desc[i].actual_length);
0188         }
0189     }
0190 
0191     return 0;
0192 }
0193 
0194 static int em28xx_start_streaming(struct em28xx_dvb *dvb)
0195 {
0196     int rc;
0197     struct em28xx_i2c_bus *i2c_bus = dvb->adapter.priv;
0198     struct em28xx *dev = i2c_bus->dev;
0199     struct usb_device *udev = interface_to_usbdev(dev->intf);
0200     int dvb_max_packet_size, packet_multiplier, dvb_alt;
0201 
0202     if (dev->dvb_xfer_bulk) {
0203         if (!dev->dvb_ep_bulk)
0204             return -ENODEV;
0205         dvb_max_packet_size = 512; /* USB 2.0 spec */
0206         packet_multiplier = EM28XX_DVB_BULK_PACKET_MULTIPLIER;
0207         dvb_alt = 0;
0208     } else { /* isoc */
0209         if (!dev->dvb_ep_isoc)
0210             return -ENODEV;
0211         dvb_max_packet_size = dev->dvb_max_pkt_size_isoc;
0212         if (dvb_max_packet_size < 0)
0213             return dvb_max_packet_size;
0214         packet_multiplier = EM28XX_DVB_NUM_ISOC_PACKETS;
0215         dvb_alt = dev->dvb_alt_isoc;
0216     }
0217 
0218     if (!dev->board.has_dual_ts)
0219         usb_set_interface(udev, dev->ifnum, dvb_alt);
0220 
0221     rc = em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
0222     if (rc < 0)
0223         return rc;
0224 
0225     dprintk(1, "Using %d buffers each with %d x %d bytes, alternate %d\n",
0226         EM28XX_DVB_NUM_BUFS,
0227         packet_multiplier,
0228         dvb_max_packet_size, dvb_alt);
0229 
0230     return em28xx_init_usb_xfer(dev, EM28XX_DIGITAL_MODE,
0231                     dev->dvb_xfer_bulk,
0232                     EM28XX_DVB_NUM_BUFS,
0233                     dvb_max_packet_size,
0234                     packet_multiplier,
0235                     em28xx_dvb_urb_data_copy);
0236 }
0237 
0238 static int em28xx_stop_streaming(struct em28xx_dvb *dvb)
0239 {
0240     struct em28xx_i2c_bus *i2c_bus = dvb->adapter.priv;
0241     struct em28xx *dev = i2c_bus->dev;
0242 
0243     em28xx_stop_urbs(dev);
0244 
0245     return 0;
0246 }
0247 
0248 static int em28xx_start_feed(struct dvb_demux_feed *feed)
0249 {
0250     struct dvb_demux *demux  = feed->demux;
0251     struct em28xx_dvb *dvb = demux->priv;
0252     int rc, ret;
0253 
0254     if (!demux->dmx.frontend)
0255         return -EINVAL;
0256 
0257     mutex_lock(&dvb->lock);
0258     dvb->nfeeds++;
0259     rc = dvb->nfeeds;
0260 
0261     if (dvb->nfeeds == 1) {
0262         ret = em28xx_start_streaming(dvb);
0263         if (ret < 0)
0264             rc = ret;
0265     }
0266 
0267     mutex_unlock(&dvb->lock);
0268     return rc;
0269 }
0270 
0271 static int em28xx_stop_feed(struct dvb_demux_feed *feed)
0272 {
0273     struct dvb_demux *demux  = feed->demux;
0274     struct em28xx_dvb *dvb = demux->priv;
0275     int err = 0;
0276 
0277     mutex_lock(&dvb->lock);
0278     dvb->nfeeds--;
0279 
0280     if (!dvb->nfeeds)
0281         err = em28xx_stop_streaming(dvb);
0282 
0283     mutex_unlock(&dvb->lock);
0284     return err;
0285 }
0286 
0287 /* ------------------------------------------------------------------ */
0288 static int em28xx_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
0289 {
0290     struct em28xx_i2c_bus *i2c_bus = fe->dvb->priv;
0291     struct em28xx *dev = i2c_bus->dev;
0292 
0293     if (acquire)
0294         return em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
0295     else
0296         return em28xx_set_mode(dev, EM28XX_SUSPEND);
0297 }
0298 
0299 /* ------------------------------------------------------------------ */
0300 
0301 static struct lgdt330x_config em2880_lgdt3303_dev = {
0302     .demod_chip = LGDT3303,
0303 };
0304 
0305 static struct lgdt3305_config em2870_lgdt3304_dev = {
0306     .i2c_addr           = 0x0e,
0307     .demod_chip         = LGDT3304,
0308     .spectral_inversion = 1,
0309     .deny_i2c_rptr      = 1,
0310     .mpeg_mode          = LGDT3305_MPEG_PARALLEL,
0311     .tpclk_edge         = LGDT3305_TPCLK_FALLING_EDGE,
0312     .tpvalid_polarity   = LGDT3305_TP_VALID_HIGH,
0313     .vsb_if_khz         = 3250,
0314     .qam_if_khz         = 4000,
0315 };
0316 
0317 static struct lgdt3305_config em2874_lgdt3305_dev = {
0318     .i2c_addr           = 0x0e,
0319     .demod_chip         = LGDT3305,
0320     .spectral_inversion = 1,
0321     .deny_i2c_rptr      = 0,
0322     .mpeg_mode          = LGDT3305_MPEG_SERIAL,
0323     .tpclk_edge         = LGDT3305_TPCLK_FALLING_EDGE,
0324     .tpvalid_polarity   = LGDT3305_TP_VALID_HIGH,
0325     .vsb_if_khz         = 3250,
0326     .qam_if_khz         = 4000,
0327 };
0328 
0329 static struct lgdt3305_config em2874_lgdt3305_nogate_dev = {
0330     .i2c_addr           = 0x0e,
0331     .demod_chip         = LGDT3305,
0332     .spectral_inversion = 1,
0333     .deny_i2c_rptr      = 1,
0334     .mpeg_mode          = LGDT3305_MPEG_SERIAL,
0335     .tpclk_edge         = LGDT3305_TPCLK_FALLING_EDGE,
0336     .tpvalid_polarity   = LGDT3305_TP_VALID_HIGH,
0337     .vsb_if_khz         = 3600,
0338     .qam_if_khz         = 3600,
0339 };
0340 
0341 static struct s921_config sharp_isdbt = {
0342     .demod_address = 0x30 >> 1
0343 };
0344 
0345 static struct zl10353_config em28xx_zl10353_with_xc3028 = {
0346     .demod_address = (0x1e >> 1),
0347     .no_tuner = 1,
0348     .parallel_ts = 1,
0349     .if2 = 45600,
0350 };
0351 
0352 static struct s5h1409_config em28xx_s5h1409_with_xc3028 = {
0353     .demod_address = 0x32 >> 1,
0354     .output_mode   = S5H1409_PARALLEL_OUTPUT,
0355     .gpio          = S5H1409_GPIO_OFF,
0356     .inversion     = S5H1409_INVERSION_OFF,
0357     .status_mode   = S5H1409_DEMODLOCKING,
0358     .mpeg_timing   = S5H1409_MPEGTIMING_CONTINUOUS_NONINVERTING_CLOCK
0359 };
0360 
0361 static struct tda18271_std_map kworld_a340_std_map = {
0362     .atsc_6   = { .if_freq = 3250, .agc_mode = 3, .std = 0,
0363               .if_lvl = 1, .rfagc_top = 0x37, },
0364     .qam_6    = { .if_freq = 4000, .agc_mode = 3, .std = 1,
0365               .if_lvl = 1, .rfagc_top = 0x37, },
0366 };
0367 
0368 static struct tda18271_config kworld_a340_config = {
0369     .std_map           = &kworld_a340_std_map,
0370 };
0371 
0372 static struct tda18271_config kworld_ub435q_v2_config = {
0373     .std_map    = &kworld_a340_std_map,
0374     .gate       = TDA18271_GATE_DIGITAL,
0375 };
0376 
0377 static struct tda18212_config kworld_ub435q_v3_config = {
0378     .if_atsc_vsb    = 3600,
0379     .if_atsc_qam    = 3600,
0380 };
0381 
0382 static struct zl10353_config em28xx_zl10353_xc3028_no_i2c_gate = {
0383     .demod_address = (0x1e >> 1),
0384     .no_tuner = 1,
0385     .disable_i2c_gate_ctrl = 1,
0386     .parallel_ts = 1,
0387     .if2 = 45600,
0388 };
0389 
0390 static struct drxd_config em28xx_drxd = {
0391     .demod_address = 0x70,
0392     .demod_revision = 0xa2,
0393     .pll_type = DRXD_PLL_NONE,
0394     .clock = 12000,
0395     .insert_rs_byte = 1,
0396     .IF = 42800000,
0397     .disable_i2c_gate_ctrl = 1,
0398 };
0399 
0400 static struct drxk_config terratec_h5_drxk = {
0401     .adr = 0x29,
0402     .single_master = 1,
0403     .no_i2c_bridge = 1,
0404     .microcode_name = "dvb-usb-terratec-h5-drxk.fw",
0405     .qam_demod_parameter_count = 2,
0406 };
0407 
0408 static struct drxk_config hauppauge_930c_drxk = {
0409     .adr = 0x29,
0410     .single_master = 1,
0411     .no_i2c_bridge = 1,
0412     .microcode_name = "dvb-usb-hauppauge-hvr930c-drxk.fw",
0413     .chunk_size = 56,
0414     .qam_demod_parameter_count = 2,
0415 };
0416 
0417 static struct drxk_config terratec_htc_stick_drxk = {
0418     .adr = 0x29,
0419     .single_master = 1,
0420     .no_i2c_bridge = 1,
0421     .microcode_name = "dvb-usb-terratec-htc-stick-drxk.fw",
0422     .chunk_size = 54,
0423     .qam_demod_parameter_count = 2,
0424     /* Required for the antenna_gpio to disable LNA. */
0425     .antenna_dvbt = true,
0426     /* The windows driver uses the same. This will disable LNA. */
0427     .antenna_gpio = 0x6,
0428 };
0429 
0430 static struct drxk_config maxmedia_ub425_tc_drxk = {
0431     .adr = 0x29,
0432     .single_master = 1,
0433     .no_i2c_bridge = 1,
0434     .microcode_name = "dvb-demod-drxk-01.fw",
0435     .chunk_size = 62,
0436     .qam_demod_parameter_count = 2,
0437 };
0438 
0439 static struct drxk_config pctv_520e_drxk = {
0440     .adr = 0x29,
0441     .single_master = 1,
0442     .microcode_name = "dvb-demod-drxk-pctv.fw",
0443     .qam_demod_parameter_count = 2,
0444     .chunk_size = 58,
0445     .antenna_dvbt = true, /* disable LNA */
0446     .antenna_gpio = (1 << 2), /* disable LNA */
0447 };
0448 
0449 static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
0450 {
0451     struct em28xx_dvb *dvb = fe->sec_priv;
0452     int status;
0453 
0454     if (!dvb)
0455         return -EINVAL;
0456 
0457     if (enable) {
0458         down(&dvb->pll_mutex);
0459         status = dvb->gate_ctrl(fe, 1);
0460     } else {
0461         status = dvb->gate_ctrl(fe, 0);
0462         up(&dvb->pll_mutex);
0463     }
0464     return status;
0465 }
0466 
0467 static void hauppauge_hvr930c_init(struct em28xx *dev)
0468 {
0469     int i;
0470 
0471     static const struct em28xx_reg_seq hauppauge_hvr930c_init[] = {
0472         {EM2874_R80_GPIO_P0_CTRL,   0xff,   0xff,   0x65},
0473         {EM2874_R80_GPIO_P0_CTRL,   0xfb,   0xff,   0x32},
0474         {EM2874_R80_GPIO_P0_CTRL,   0xff,   0xff,   0xb8},
0475         {   -1,         -1, -1, -1},
0476     };
0477     static const struct em28xx_reg_seq hauppauge_hvr930c_end[] = {
0478         {EM2874_R80_GPIO_P0_CTRL,   0xef,   0xff,   0x01},
0479         {EM2874_R80_GPIO_P0_CTRL,   0xaf,   0xff,   0x65},
0480         {EM2874_R80_GPIO_P0_CTRL,   0xef,   0xff,   0x76},
0481         {EM2874_R80_GPIO_P0_CTRL,   0xef,   0xff,   0x01},
0482         {EM2874_R80_GPIO_P0_CTRL,   0xcf,   0xff,   0x0b},
0483         {EM2874_R80_GPIO_P0_CTRL,   0xef,   0xff,   0x40},
0484 
0485         {EM2874_R80_GPIO_P0_CTRL,   0xcf,   0xff,   0x65},
0486         {EM2874_R80_GPIO_P0_CTRL,   0xef,   0xff,   0x65},
0487         {EM2874_R80_GPIO_P0_CTRL,   0xcf,   0xff,   0x0b},
0488         {EM2874_R80_GPIO_P0_CTRL,   0xef,   0xff,   0x65},
0489 
0490         {   -1,         -1, -1, -1},
0491     };
0492 
0493     static const struct {
0494         unsigned char r[4];
0495         int len;
0496     } regs[] = {
0497         {{ 0x06, 0x02, 0x00, 0x31 }, 4},
0498         {{ 0x01, 0x02 }, 2},
0499         {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
0500         {{ 0x01, 0x00 }, 2},
0501         {{ 0x01, 0x00, 0xff, 0xaf }, 4},
0502         {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
0503         {{ 0x01, 0x00 }, 2},
0504         {{ 0x01, 0x00, 0x73, 0xaf }, 4},
0505         {{ 0x04, 0x00 }, 2},
0506         {{ 0x00, 0x04 }, 2},
0507         {{ 0x00, 0x04, 0x00, 0x0a }, 4},
0508         {{ 0x04, 0x14 }, 2},
0509         {{ 0x04, 0x14, 0x00, 0x00 }, 4},
0510     };
0511 
0512     em28xx_gpio_set(dev, hauppauge_hvr930c_init);
0513     em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
0514     usleep_range(10000, 11000);
0515     em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
0516     usleep_range(10000, 11000);
0517 
0518     dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1;
0519 
0520     for (i = 0; i < ARRAY_SIZE(regs); i++)
0521         i2c_master_send(&dev->i2c_client[dev->def_i2c_bus],
0522                 regs[i].r, regs[i].len);
0523     em28xx_gpio_set(dev, hauppauge_hvr930c_end);
0524 
0525     msleep(100);
0526 
0527     em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
0528     msleep(30);
0529 
0530     em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x45);
0531     usleep_range(10000, 11000);
0532 }
0533 
0534 static void terratec_h5_init(struct em28xx *dev)
0535 {
0536     int i;
0537     static const struct em28xx_reg_seq terratec_h5_init[] = {
0538         {EM2820_R08_GPIO_CTRL,      0xff,   0xff,   10},
0539         {EM2874_R80_GPIO_P0_CTRL,   0xf6,   0xff,   100},
0540         {EM2874_R80_GPIO_P0_CTRL,   0xf2,   0xff,   50},
0541         {EM2874_R80_GPIO_P0_CTRL,   0xf6,   0xff,   100},
0542         {   -1,         -1, -1, -1},
0543     };
0544     static const struct em28xx_reg_seq terratec_h5_end[] = {
0545         {EM2874_R80_GPIO_P0_CTRL,   0xe6,   0xff,   100},
0546         {EM2874_R80_GPIO_P0_CTRL,   0xa6,   0xff,   50},
0547         {EM2874_R80_GPIO_P0_CTRL,   0xe6,   0xff,   100},
0548         {   -1,         -1, -1, -1},
0549     };
0550     static const struct {
0551         unsigned char r[4];
0552         int len;
0553     } regs[] = {
0554         {{ 0x06, 0x02, 0x00, 0x31 }, 4},
0555         {{ 0x01, 0x02 }, 2},
0556         {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
0557         {{ 0x01, 0x00 }, 2},
0558         {{ 0x01, 0x00, 0xff, 0xaf }, 4},
0559         {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
0560         {{ 0x01, 0x00 }, 2},
0561         {{ 0x01, 0x00, 0x73, 0xaf }, 4},
0562         {{ 0x04, 0x00 }, 2},
0563         {{ 0x00, 0x04 }, 2},
0564         {{ 0x00, 0x04, 0x00, 0x0a }, 4},
0565         {{ 0x04, 0x14 }, 2},
0566         {{ 0x04, 0x14, 0x00, 0x00 }, 4},
0567     };
0568 
0569     em28xx_gpio_set(dev, terratec_h5_init);
0570     em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
0571     usleep_range(10000, 11000);
0572     em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x45);
0573     usleep_range(10000, 11000);
0574 
0575     dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1;
0576 
0577     for (i = 0; i < ARRAY_SIZE(regs); i++)
0578         i2c_master_send(&dev->i2c_client[dev->def_i2c_bus],
0579                 regs[i].r, regs[i].len);
0580     em28xx_gpio_set(dev, terratec_h5_end);
0581 };
0582 
0583 static void terratec_htc_stick_init(struct em28xx *dev)
0584 {
0585     int i;
0586 
0587     /*
0588      * GPIO configuration:
0589      * 0xff: unknown (does not affect DVB-T).
0590      * 0xf6: DRX-K (demodulator).
0591      * 0xe6: unknown (does not affect DVB-T).
0592      * 0xb6: unknown (does not affect DVB-T).
0593      */
0594     static const struct em28xx_reg_seq terratec_htc_stick_init[] = {
0595         {EM2820_R08_GPIO_CTRL,      0xff,   0xff,   10},
0596         {EM2874_R80_GPIO_P0_CTRL,   0xf6,   0xff,   100},
0597         {EM2874_R80_GPIO_P0_CTRL,   0xe6,   0xff,   50},
0598         {EM2874_R80_GPIO_P0_CTRL,   0xf6,   0xff,   100},
0599         {   -1,         -1, -1, -1},
0600     };
0601     static const struct em28xx_reg_seq terratec_htc_stick_end[] = {
0602         {EM2874_R80_GPIO_P0_CTRL,   0xb6,   0xff,   100},
0603         {EM2874_R80_GPIO_P0_CTRL,   0xf6,   0xff,   50},
0604         {   -1,         -1, -1, -1},
0605     };
0606 
0607     /*
0608      * Init the analog decoder (not yet supported), but
0609      * it's probably still a good idea.
0610      */
0611     static const struct {
0612         unsigned char r[4];
0613         int len;
0614     } regs[] = {
0615         {{ 0x06, 0x02, 0x00, 0x31 }, 4},
0616         {{ 0x01, 0x02 }, 2},
0617         {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
0618         {{ 0x01, 0x00 }, 2},
0619         {{ 0x01, 0x00, 0xff, 0xaf }, 4},
0620     };
0621 
0622     em28xx_gpio_set(dev, terratec_htc_stick_init);
0623 
0624     em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
0625     usleep_range(10000, 11000);
0626     em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
0627     usleep_range(10000, 11000);
0628 
0629     dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1;
0630 
0631     for (i = 0; i < ARRAY_SIZE(regs); i++)
0632         i2c_master_send(&dev->i2c_client[dev->def_i2c_bus],
0633                 regs[i].r, regs[i].len);
0634 
0635     em28xx_gpio_set(dev, terratec_htc_stick_end);
0636 };
0637 
0638 static void terratec_htc_usb_xs_init(struct em28xx *dev)
0639 {
0640     int i;
0641 
0642     static const struct em28xx_reg_seq terratec_htc_usb_xs_init[] = {
0643         {EM2820_R08_GPIO_CTRL,      0xff,   0xff,   10},
0644         {EM2874_R80_GPIO_P0_CTRL,   0xb2,   0xff,   100},
0645         {EM2874_R80_GPIO_P0_CTRL,   0xb2,   0xff,   50},
0646         {EM2874_R80_GPIO_P0_CTRL,   0xb6,   0xff,   100},
0647         {   -1,         -1, -1, -1},
0648     };
0649     static const struct em28xx_reg_seq terratec_htc_usb_xs_end[] = {
0650         {EM2874_R80_GPIO_P0_CTRL,   0xa6,   0xff,   100},
0651         {EM2874_R80_GPIO_P0_CTRL,   0xa6,   0xff,   50},
0652         {EM2874_R80_GPIO_P0_CTRL,   0xe6,   0xff,   100},
0653         {   -1,         -1, -1, -1},
0654     };
0655 
0656     /*
0657      * Init the analog decoder (not yet supported), but
0658      * it's probably still a good idea.
0659      */
0660     static const struct {
0661         unsigned char r[4];
0662         int len;
0663     } regs[] = {
0664         {{ 0x06, 0x02, 0x00, 0x31 }, 4},
0665         {{ 0x01, 0x02 }, 2},
0666         {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
0667         {{ 0x01, 0x00 }, 2},
0668         {{ 0x01, 0x00, 0xff, 0xaf }, 4},
0669         {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
0670         {{ 0x01, 0x00 }, 2},
0671         {{ 0x01, 0x00, 0x73, 0xaf }, 4},
0672         {{ 0x04, 0x00 }, 2},
0673         {{ 0x00, 0x04 }, 2},
0674         {{ 0x00, 0x04, 0x00, 0x0a }, 4},
0675         {{ 0x04, 0x14 }, 2},
0676         {{ 0x04, 0x14, 0x00, 0x00 }, 4},
0677     };
0678 
0679     em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
0680 
0681     em28xx_gpio_set(dev, terratec_htc_usb_xs_init);
0682 
0683     em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
0684     usleep_range(10000, 11000);
0685     em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
0686     usleep_range(10000, 11000);
0687 
0688     dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1;
0689 
0690     for (i = 0; i < ARRAY_SIZE(regs); i++)
0691         i2c_master_send(&dev->i2c_client[dev->def_i2c_bus],
0692                 regs[i].r, regs[i].len);
0693 
0694     em28xx_gpio_set(dev, terratec_htc_usb_xs_end);
0695 };
0696 
0697 static void pctv_520e_init(struct em28xx *dev)
0698 {
0699     /*
0700      * Init AVF4910B analog decoder. Looks like I2C traffic to
0701      * digital demodulator and tuner are routed via AVF4910B.
0702      */
0703     int i;
0704     static const struct {
0705         unsigned char r[4];
0706         int len;
0707     } regs[] = {
0708         {{ 0x06, 0x02, 0x00, 0x31 }, 4},
0709         {{ 0x01, 0x02 }, 2},
0710         {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
0711         {{ 0x01, 0x00 }, 2},
0712         {{ 0x01, 0x00, 0xff, 0xaf }, 4},
0713         {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
0714         {{ 0x01, 0x00 }, 2},
0715         {{ 0x01, 0x00, 0x73, 0xaf }, 4},
0716     };
0717 
0718     dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1; /* 0x41 */
0719 
0720     for (i = 0; i < ARRAY_SIZE(regs); i++)
0721         i2c_master_send(&dev->i2c_client[dev->def_i2c_bus],
0722                 regs[i].r, regs[i].len);
0723 };
0724 
0725 static int em28xx_pctv_290e_set_lna(struct dvb_frontend *fe)
0726 {
0727     struct dtv_frontend_properties *c = &fe->dtv_property_cache;
0728     struct em28xx_i2c_bus *i2c_bus = fe->dvb->priv;
0729     struct em28xx *dev = i2c_bus->dev;
0730 #ifdef CONFIG_GPIOLIB
0731     struct em28xx_dvb *dvb = dev->dvb;
0732     int ret;
0733     unsigned long flags;
0734 
0735     if (c->lna == 1)
0736         flags = GPIOF_OUT_INIT_HIGH; /* enable LNA */
0737     else
0738         flags = GPIOF_OUT_INIT_LOW; /* disable LNA */
0739 
0740     ret = gpio_request_one(dvb->lna_gpio, flags, NULL);
0741     if (ret)
0742         dev_err(&dev->intf->dev, "gpio request failed %d\n", ret);
0743     else
0744         gpio_free(dvb->lna_gpio);
0745 
0746     return ret;
0747 #else
0748     dev_warn(&dev->intf->dev, "%s: LNA control is disabled (lna=%u)\n",
0749          KBUILD_MODNAME, c->lna);
0750     return 0;
0751 #endif
0752 }
0753 
0754 static int em28xx_pctv_292e_set_lna(struct dvb_frontend *fe)
0755 {
0756     struct dtv_frontend_properties *c = &fe->dtv_property_cache;
0757     struct em28xx_i2c_bus *i2c_bus = fe->dvb->priv;
0758     struct em28xx *dev = i2c_bus->dev;
0759     u8 lna;
0760 
0761     if (c->lna == 1)
0762         lna = 0x01;
0763     else
0764         lna = 0x00;
0765 
0766     return em28xx_write_reg_bits(dev, EM2874_R80_GPIO_P0_CTRL, lna, 0x01);
0767 }
0768 
0769 static int em28xx_mt352_terratec_xs_init(struct dvb_frontend *fe)
0770 {
0771     /* Values extracted from a USB trace of the Terratec Windows driver */
0772     static u8 clock_config[]   = { CLOCK_CTL,  0x38, 0x2c };
0773     static u8 reset[]          = { RESET,      0x80 };
0774     static u8 adc_ctl_1_cfg[]  = { ADC_CTL_1,  0x40 };
0775     static u8 agc_cfg[]        = { AGC_TARGET, 0x28, 0xa0 };
0776     static u8 input_freq_cfg[] = { INPUT_FREQ_1, 0x31, 0xb8 };
0777     static u8 rs_err_cfg[]     = { RS_ERR_PER_1, 0x00, 0x4d };
0778     static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
0779     static u8 trl_nom_cfg[]    = { TRL_NOMINAL_RATE_1, 0x64, 0x00 };
0780     static u8 tps_given_cfg[]  = { TPS_GIVEN_1, 0x40, 0x80, 0x50 };
0781     static u8 tuner_go[]       = { TUNER_GO, 0x01};
0782 
0783     mt352_write(fe, clock_config,   sizeof(clock_config));
0784     usleep_range(200, 250);
0785     mt352_write(fe, reset,          sizeof(reset));
0786     mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
0787     mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
0788     mt352_write(fe, input_freq_cfg, sizeof(input_freq_cfg));
0789     mt352_write(fe, rs_err_cfg,     sizeof(rs_err_cfg));
0790     mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
0791     mt352_write(fe, trl_nom_cfg,    sizeof(trl_nom_cfg));
0792     mt352_write(fe, tps_given_cfg,  sizeof(tps_given_cfg));
0793     mt352_write(fe, tuner_go,       sizeof(tuner_go));
0794     return 0;
0795 }
0796 
0797 static void px_bcud_init(struct em28xx *dev)
0798 {
0799     int i;
0800     static const struct {
0801         unsigned char r[4];
0802         int len;
0803     } regs1[] = {
0804         {{ 0x0e, 0x77 }, 2},
0805         {{ 0x0f, 0x77 }, 2},
0806         {{ 0x03, 0x90 }, 2},
0807     }, regs2[] = {
0808         {{ 0x07, 0x01 }, 2},
0809         {{ 0x08, 0x10 }, 2},
0810         {{ 0x13, 0x00 }, 2},
0811         {{ 0x17, 0x00 }, 2},
0812         {{ 0x03, 0x01 }, 2},
0813         {{ 0x10, 0xb1 }, 2},
0814         {{ 0x11, 0x40 }, 2},
0815         {{ 0x85, 0x7a }, 2},
0816         {{ 0x87, 0x04 }, 2},
0817     };
0818     static const struct em28xx_reg_seq gpio[] = {
0819         {EM28XX_R06_I2C_CLK,        0x40,   0xff,   300},
0820         {EM2874_R80_GPIO_P0_CTRL,   0xfd,   0xff,   60},
0821         {EM28XX_R15_RGAIN,      0x20,   0xff,   0},
0822         {EM28XX_R16_GGAIN,      0x20,   0xff,   0},
0823         {EM28XX_R17_BGAIN,      0x20,   0xff,   0},
0824         {EM28XX_R18_ROFFSET,        0x00,   0xff,   0},
0825         {EM28XX_R19_GOFFSET,        0x00,   0xff,   0},
0826         {EM28XX_R1A_BOFFSET,        0x00,   0xff,   0},
0827         {EM28XX_R23_UOFFSET,        0x00,   0xff,   0},
0828         {EM28XX_R24_VOFFSET,        0x00,   0xff,   0},
0829         {EM28XX_R26_COMPR,      0x00,   0xff,   0},
0830         {0x13,              0x08,   0xff,   0},
0831         {EM28XX_R12_VINENABLE,      0x27,   0xff,   0},
0832         {EM28XX_R0C_USBSUSP,        0x10,   0xff,   0},
0833         {EM28XX_R27_OUTFMT,     0x00,   0xff,   0},
0834         {EM28XX_R10_VINMODE,        0x00,   0xff,   0},
0835         {EM28XX_R11_VINCTRL,        0x11,   0xff,   0},
0836         {EM2874_R50_IR_CONFIG,      0x01,   0xff,   0},
0837         {EM2874_R5F_TS_ENABLE,      0x80,   0xff,   0},
0838         {EM28XX_R06_I2C_CLK,        0x46,   0xff,   0},
0839     };
0840     em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x46);
0841     /* sleeping ISDB-T */
0842     dev->dvb->i2c_client_demod->addr = 0x14;
0843     for (i = 0; i < ARRAY_SIZE(regs1); i++)
0844         i2c_master_send(dev->dvb->i2c_client_demod,
0845                 regs1[i].r, regs1[i].len);
0846     /* sleeping ISDB-S */
0847     dev->dvb->i2c_client_demod->addr = 0x15;
0848     for (i = 0; i < ARRAY_SIZE(regs2); i++)
0849         i2c_master_send(dev->dvb->i2c_client_demod, regs2[i].r,
0850                 regs2[i].len);
0851     for (i = 0; i < ARRAY_SIZE(gpio); i++) {
0852         em28xx_write_reg_bits(dev, gpio[i].reg, gpio[i].val,
0853                       gpio[i].mask);
0854         if (gpio[i].sleep > 0)
0855             msleep(gpio[i].sleep);
0856     }
0857 };
0858 
0859 static struct mt352_config terratec_xs_mt352_cfg = {
0860     .demod_address = (0x1e >> 1),
0861     .no_tuner = 1,
0862     .if2 = 45600,
0863     .demod_init = em28xx_mt352_terratec_xs_init,
0864 };
0865 
0866 static struct tda10023_config em28xx_tda10023_config = {
0867     .demod_address = 0x0c,
0868     .invert = 1,
0869 };
0870 
0871 static struct cxd2820r_config em28xx_cxd2820r_config = {
0872     .i2c_address = (0xd8 >> 1),
0873     .ts_mode = CXD2820R_TS_SERIAL,
0874 };
0875 
0876 static struct tda18271_config em28xx_cxd2820r_tda18271_config = {
0877     .output_opt = TDA18271_OUTPUT_LT_OFF,
0878     .gate = TDA18271_GATE_DIGITAL,
0879 };
0880 
0881 static struct zl10353_config em28xx_zl10353_no_i2c_gate_dev = {
0882     .demod_address = (0x1e >> 1),
0883     .disable_i2c_gate_ctrl = 1,
0884     .no_tuner = 1,
0885     .parallel_ts = 1,
0886 };
0887 
0888 static struct mt2060_config em28xx_mt2060_config = {
0889     .i2c_address = 0x60,
0890 };
0891 
0892 static struct qt1010_config em28xx_qt1010_config = {
0893     .i2c_address = 0x62
0894 };
0895 
0896 static const struct mb86a20s_config c3tech_duo_mb86a20s_config = {
0897     .demod_address = 0x10,
0898     .is_serial = true,
0899 };
0900 
0901 static struct tda18271_std_map mb86a20s_tda18271_config = {
0902     .dvbt_6   = { .if_freq = 4000, .agc_mode = 3, .std = 4,
0903               .if_lvl = 1, .rfagc_top = 0x37, },
0904 };
0905 
0906 static struct tda18271_config c3tech_duo_tda18271_config = {
0907     .std_map = &mb86a20s_tda18271_config,
0908     .gate    = TDA18271_GATE_DIGITAL,
0909     .small_i2c = TDA18271_03_BYTE_CHUNK_INIT,
0910 };
0911 
0912 static struct tda18271_std_map drx_j_std_map = {
0913     .atsc_6   = { .if_freq = 5000, .agc_mode = 3, .std = 0, .if_lvl = 1,
0914               .rfagc_top = 0x37, },
0915     .qam_6    = { .if_freq = 5380, .agc_mode = 3, .std = 3, .if_lvl = 1,
0916               .rfagc_top = 0x37, },
0917 };
0918 
0919 static struct tda18271_config pinnacle_80e_dvb_config = {
0920     .std_map = &drx_j_std_map,
0921     .gate    = TDA18271_GATE_DIGITAL,
0922     .role    = TDA18271_MASTER,
0923 };
0924 
0925 static struct lgdt3306a_config hauppauge_01595_lgdt3306a_config = {
0926     .qam_if_khz         = 4000,
0927     .vsb_if_khz         = 3250,
0928     .spectral_inversion = 0,
0929     .deny_i2c_rptr      = 0,
0930     .mpeg_mode          = LGDT3306A_MPEG_SERIAL,
0931     .tpclk_edge         = LGDT3306A_TPCLK_RISING_EDGE,
0932     .tpvalid_polarity   = LGDT3306A_TP_VALID_HIGH,
0933     .xtalMHz            = 25,
0934 };
0935 
0936 /* ------------------------------------------------------------------ */
0937 
0938 static noinline_for_stack int em28xx_attach_xc3028(u8 addr, struct em28xx *dev)
0939 {
0940     struct dvb_frontend *fe;
0941     struct xc2028_config cfg;
0942     struct xc2028_ctrl ctl;
0943 
0944     memset(&cfg, 0, sizeof(cfg));
0945     cfg.i2c_adap  = &dev->i2c_adap[dev->def_i2c_bus];
0946     cfg.i2c_addr  = addr;
0947 
0948     memset(&ctl, 0, sizeof(ctl));
0949     em28xx_setup_xc3028(dev, &ctl);
0950     cfg.ctrl  = &ctl;
0951 
0952     if (!dev->dvb->fe[0]) {
0953         dev_err(&dev->intf->dev,
0954             "dvb frontend not attached. Can't attach xc3028\n");
0955         return -EINVAL;
0956     }
0957 
0958     fe = dvb_attach(xc2028_attach, dev->dvb->fe[0], &cfg);
0959     if (!fe) {
0960         dev_err(&dev->intf->dev, "xc3028 attach failed\n");
0961         dvb_frontend_detach(dev->dvb->fe[0]);
0962         dev->dvb->fe[0] = NULL;
0963         return -EINVAL;
0964     }
0965 
0966     dev_info(&dev->intf->dev, "xc3028 attached\n");
0967 
0968     return 0;
0969 }
0970 
0971 /* ------------------------------------------------------------------ */
0972 
0973 static int em28xx_register_dvb(struct em28xx_dvb *dvb, struct module *module,
0974                    struct em28xx *dev, struct device *device)
0975 {
0976     int result;
0977     bool create_rf_connector = false;
0978 
0979     mutex_init(&dvb->lock);
0980 
0981     /* register adapter */
0982     result = dvb_register_adapter(&dvb->adapter,
0983                       dev_name(&dev->intf->dev), module,
0984                       device, adapter_nr);
0985     if (result < 0) {
0986         dev_warn(&dev->intf->dev,
0987              "dvb_register_adapter failed (errno = %d)\n",
0988              result);
0989         goto fail_adapter;
0990     }
0991 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
0992     dvb->adapter.mdev = dev->media_dev;
0993 #endif
0994 
0995     /* Ensure all frontends negotiate bus access */
0996     dvb->fe[0]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
0997     if (dvb->fe[1])
0998         dvb->fe[1]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
0999 
1000     dvb->adapter.priv = &dev->i2c_bus[dev->def_i2c_bus];
1001 
1002     /* register frontend */
1003     result = dvb_register_frontend(&dvb->adapter, dvb->fe[0]);
1004     if (result < 0) {
1005         dev_warn(&dev->intf->dev,
1006              "dvb_register_frontend failed (errno = %d)\n",
1007              result);
1008         goto fail_frontend0;
1009     }
1010 
1011     /* register 2nd frontend */
1012     if (dvb->fe[1]) {
1013         result = dvb_register_frontend(&dvb->adapter, dvb->fe[1]);
1014         if (result < 0) {
1015             dev_warn(&dev->intf->dev,
1016                  "2nd dvb_register_frontend failed (errno = %d)\n",
1017                  result);
1018             goto fail_frontend1;
1019         }
1020     }
1021 
1022     /* register demux stuff */
1023     dvb->demux.dmx.capabilities =
1024         DMX_TS_FILTERING | DMX_SECTION_FILTERING |
1025         DMX_MEMORY_BASED_FILTERING;
1026     dvb->demux.priv       = dvb;
1027     dvb->demux.filternum  = 256;
1028     dvb->demux.feednum    = 256;
1029     dvb->demux.start_feed = em28xx_start_feed;
1030     dvb->demux.stop_feed  = em28xx_stop_feed;
1031 
1032     result = dvb_dmx_init(&dvb->demux);
1033     if (result < 0) {
1034         dev_warn(&dev->intf->dev,
1035              "dvb_dmx_init failed (errno = %d)\n",
1036              result);
1037         goto fail_dmx;
1038     }
1039 
1040     dvb->dmxdev.filternum    = 256;
1041     dvb->dmxdev.demux        = &dvb->demux.dmx;
1042     dvb->dmxdev.capabilities = 0;
1043     result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
1044     if (result < 0) {
1045         dev_warn(&dev->intf->dev,
1046              "dvb_dmxdev_init failed (errno = %d)\n",
1047              result);
1048         goto fail_dmxdev;
1049     }
1050 
1051     dvb->fe_hw.source = DMX_FRONTEND_0;
1052     result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
1053     if (result < 0) {
1054         dev_warn(&dev->intf->dev,
1055              "add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
1056              result);
1057         goto fail_fe_hw;
1058     }
1059 
1060     dvb->fe_mem.source = DMX_MEMORY_FE;
1061     result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
1062     if (result < 0) {
1063         dev_warn(&dev->intf->dev,
1064              "add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
1065              result);
1066         goto fail_fe_mem;
1067     }
1068 
1069     result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
1070     if (result < 0) {
1071         dev_warn(&dev->intf->dev,
1072              "connect_frontend failed (errno = %d)\n",
1073              result);
1074         goto fail_fe_conn;
1075     }
1076 
1077     /* register network adapter */
1078     dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
1079 
1080     /* If the analog part won't create RF connectors, DVB will do it */
1081     if (!dev->has_video || dev->tuner_type == TUNER_ABSENT)
1082         create_rf_connector = true;
1083 
1084     result = dvb_create_media_graph(&dvb->adapter, create_rf_connector);
1085     if (result < 0)
1086         goto fail_create_graph;
1087 
1088     return 0;
1089 
1090 fail_create_graph:
1091     dvb_net_release(&dvb->net);
1092 fail_fe_conn:
1093     dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
1094 fail_fe_mem:
1095     dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
1096 fail_fe_hw:
1097     dvb_dmxdev_release(&dvb->dmxdev);
1098 fail_dmxdev:
1099     dvb_dmx_release(&dvb->demux);
1100 fail_dmx:
1101     if (dvb->fe[1])
1102         dvb_unregister_frontend(dvb->fe[1]);
1103     dvb_unregister_frontend(dvb->fe[0]);
1104 fail_frontend1:
1105     if (dvb->fe[1])
1106         dvb_frontend_detach(dvb->fe[1]);
1107 fail_frontend0:
1108     dvb_frontend_detach(dvb->fe[0]);
1109     dvb_unregister_adapter(&dvb->adapter);
1110 fail_adapter:
1111     return result;
1112 }
1113 
1114 static void em28xx_unregister_dvb(struct em28xx_dvb *dvb)
1115 {
1116     dvb_net_release(&dvb->net);
1117     dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
1118     dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
1119     dvb_dmxdev_release(&dvb->dmxdev);
1120     dvb_dmx_release(&dvb->demux);
1121     if (dvb->fe[1])
1122         dvb_unregister_frontend(dvb->fe[1]);
1123     dvb_unregister_frontend(dvb->fe[0]);
1124     if (dvb->fe[1] && !dvb->dont_attach_fe1)
1125         dvb_frontend_detach(dvb->fe[1]);
1126     dvb_frontend_detach(dvb->fe[0]);
1127     dvb_unregister_adapter(&dvb->adapter);
1128 }
1129 
1130 static int em28174_dvb_init_pctv_460e(struct em28xx *dev)
1131 {
1132     struct em28xx_dvb *dvb = dev->dvb;
1133     struct tda10071_platform_data tda10071_pdata = {};
1134     struct a8293_platform_data a8293_pdata = {};
1135 
1136     /* attach demod + tuner combo */
1137     tda10071_pdata.clk = 40444000; /* 40.444 MHz */
1138     tda10071_pdata.i2c_wr_max = 64;
1139     tda10071_pdata.ts_mode = TDA10071_TS_SERIAL;
1140     tda10071_pdata.pll_multiplier = 20;
1141     tda10071_pdata.tuner_i2c_addr = 0x14;
1142 
1143     dvb->i2c_client_demod = dvb_module_probe("tda10071", "tda10071_cx24118",
1144                          &dev->i2c_adap[dev->def_i2c_bus],
1145                          0x55, &tda10071_pdata);
1146     if (!dvb->i2c_client_demod)
1147         return -ENODEV;
1148 
1149     dvb->fe[0] = tda10071_pdata.get_dvb_frontend(dvb->i2c_client_demod);
1150 
1151     /* attach SEC */
1152     a8293_pdata.dvb_frontend = dvb->fe[0];
1153 
1154     dvb->i2c_client_sec = dvb_module_probe("a8293", NULL,
1155                            &dev->i2c_adap[dev->def_i2c_bus],
1156                            0x08, &a8293_pdata);
1157     if (!dvb->i2c_client_sec) {
1158         dvb_module_release(dvb->i2c_client_demod);
1159         return -ENODEV;
1160     }
1161 
1162     return 0;
1163 }
1164 
1165 static int em28178_dvb_init_pctv_461e(struct em28xx *dev)
1166 {
1167     struct em28xx_dvb *dvb = dev->dvb;
1168     struct i2c_adapter *i2c_adapter;
1169     struct m88ds3103_platform_data m88ds3103_pdata = {};
1170     struct ts2020_config ts2020_config = {};
1171     struct a8293_platform_data a8293_pdata = {};
1172 
1173     /* attach demod */
1174     m88ds3103_pdata.clk = 27000000;
1175     m88ds3103_pdata.i2c_wr_max = 33;
1176     m88ds3103_pdata.ts_mode = M88DS3103_TS_PARALLEL;
1177     m88ds3103_pdata.ts_clk = 16000;
1178     m88ds3103_pdata.ts_clk_pol = 1;
1179     m88ds3103_pdata.agc = 0x99;
1180 
1181     dvb->i2c_client_demod = dvb_module_probe("m88ds3103", NULL,
1182                          &dev->i2c_adap[dev->def_i2c_bus],
1183                          0x68, &m88ds3103_pdata);
1184     if (!dvb->i2c_client_demod)
1185         return -ENODEV;
1186 
1187     dvb->fe[0] = m88ds3103_pdata.get_dvb_frontend(dvb->i2c_client_demod);
1188     i2c_adapter = m88ds3103_pdata.get_i2c_adapter(dvb->i2c_client_demod);
1189 
1190     /* attach tuner */
1191     ts2020_config.fe = dvb->fe[0];
1192 
1193     dvb->i2c_client_tuner = dvb_module_probe("ts2020", "ts2022",
1194                          i2c_adapter,
1195                          0x60, &ts2020_config);
1196     if (!dvb->i2c_client_tuner) {
1197         dvb_module_release(dvb->i2c_client_demod);
1198         return -ENODEV;
1199     }
1200 
1201     /* delegate signal strength measurement to tuner */
1202     dvb->fe[0]->ops.read_signal_strength =
1203             dvb->fe[0]->ops.tuner_ops.get_rf_strength;
1204 
1205     /* attach SEC */
1206     a8293_pdata.dvb_frontend = dvb->fe[0];
1207     dvb->i2c_client_sec = dvb_module_probe("a8293", NULL,
1208                            &dev->i2c_adap[dev->def_i2c_bus],
1209                            0x08, &a8293_pdata);
1210     if (!dvb->i2c_client_sec) {
1211         dvb_module_release(dvb->i2c_client_tuner);
1212         dvb_module_release(dvb->i2c_client_demod);
1213         return -ENODEV;
1214     }
1215 
1216     return 0;
1217 }
1218 
1219 static int em28178_dvb_init_pctv_461e_v2(struct em28xx *dev)
1220 {
1221     struct em28xx_dvb *dvb = dev->dvb;
1222     struct i2c_adapter *i2c_adapter;
1223     struct m88ds3103_platform_data m88ds3103_pdata = {};
1224     struct ts2020_config ts2020_config = {};
1225     struct a8293_platform_data a8293_pdata = {};
1226 
1227     /* attach demod */
1228     m88ds3103_pdata.clk = 27000000;
1229     m88ds3103_pdata.i2c_wr_max = 33;
1230     m88ds3103_pdata.ts_mode = M88DS3103_TS_PARALLEL;
1231     m88ds3103_pdata.ts_clk = 16000;
1232     m88ds3103_pdata.ts_clk_pol = 0;
1233     m88ds3103_pdata.agc = 0x99;
1234     m88ds3103_pdata.agc_inv = 0;
1235     m88ds3103_pdata.spec_inv = 0;
1236     dvb->i2c_client_demod = dvb_module_probe("m88ds3103", "m88ds3103b",
1237                          &dev->i2c_adap[dev->def_i2c_bus],
1238                          0x6a, &m88ds3103_pdata);
1239 
1240     if (!dvb->i2c_client_demod)
1241         return -ENODEV;
1242 
1243     dvb->fe[0] = m88ds3103_pdata.get_dvb_frontend(dvb->i2c_client_demod);
1244     i2c_adapter = m88ds3103_pdata.get_i2c_adapter(dvb->i2c_client_demod);
1245 
1246     /* attach tuner */
1247     ts2020_config.fe = dvb->fe[0];
1248     dvb->i2c_client_tuner = dvb_module_probe("ts2020", "ts2022",
1249                          i2c_adapter,
1250                          0x60, &ts2020_config);
1251     if (!dvb->i2c_client_tuner) {
1252         dvb_module_release(dvb->i2c_client_demod);
1253         return -ENODEV;
1254     }
1255 
1256     /* delegate signal strength measurement to tuner */
1257     dvb->fe[0]->ops.read_signal_strength =
1258             dvb->fe[0]->ops.tuner_ops.get_rf_strength;
1259 
1260     /* attach SEC */
1261     a8293_pdata.dvb_frontend = dvb->fe[0];
1262     dvb->i2c_client_sec = dvb_module_probe("a8293", NULL,
1263                            &dev->i2c_adap[dev->def_i2c_bus],
1264                            0x08, &a8293_pdata);
1265     if (!dvb->i2c_client_sec) {
1266         dvb_module_release(dvb->i2c_client_tuner);
1267         dvb_module_release(dvb->i2c_client_demod);
1268         return -ENODEV;
1269     }
1270 
1271     return 0;
1272 }
1273 
1274 static int em28178_dvb_init_pctv_292e(struct em28xx *dev)
1275 {
1276     struct em28xx_dvb *dvb = dev->dvb;
1277     struct i2c_adapter *adapter;
1278     struct si2168_config si2168_config = {};
1279     struct si2157_config si2157_config = {};
1280 
1281     /* attach demod */
1282     si2168_config.i2c_adapter = &adapter;
1283     si2168_config.fe = &dvb->fe[0];
1284     si2168_config.ts_mode = SI2168_TS_PARALLEL;
1285     si2168_config.spectral_inversion = true;
1286 
1287     dvb->i2c_client_demod = dvb_module_probe("si2168", NULL,
1288                          &dev->i2c_adap[dev->def_i2c_bus],
1289                          0x64, &si2168_config);
1290     if (!dvb->i2c_client_demod)
1291         return -ENODEV;
1292 
1293     /* attach tuner */
1294     si2157_config.fe = dvb->fe[0];
1295     si2157_config.if_port = 1;
1296 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
1297     si2157_config.mdev = dev->media_dev;
1298 #endif
1299     dvb->i2c_client_tuner = dvb_module_probe("si2157", NULL,
1300                          adapter,
1301                          0x60, &si2157_config);
1302     if (!dvb->i2c_client_tuner) {
1303         dvb_module_release(dvb->i2c_client_demod);
1304         return -ENODEV;
1305     }
1306     dvb->fe[0]->ops.set_lna = em28xx_pctv_292e_set_lna;
1307 
1308     return 0;
1309 }
1310 
1311 static int em28178_dvb_init_terratec_t2_stick_hd(struct em28xx *dev)
1312 {
1313     struct em28xx_dvb *dvb = dev->dvb;
1314     struct i2c_adapter *adapter;
1315     struct si2168_config si2168_config = {};
1316     struct si2157_config si2157_config = {};
1317 
1318     /* attach demod */
1319     si2168_config.i2c_adapter = &adapter;
1320     si2168_config.fe = &dvb->fe[0];
1321     si2168_config.ts_mode = SI2168_TS_PARALLEL;
1322 
1323     dvb->i2c_client_demod = dvb_module_probe("si2168", NULL,
1324                          &dev->i2c_adap[dev->def_i2c_bus],
1325                          0x64, &si2168_config);
1326     if (!dvb->i2c_client_demod)
1327         return -ENODEV;
1328 
1329     /* attach tuner */
1330     memset(&si2157_config, 0, sizeof(si2157_config));
1331     si2157_config.fe = dvb->fe[0];
1332     si2157_config.if_port = 0;
1333 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
1334     si2157_config.mdev = dev->media_dev;
1335 #endif
1336     dvb->i2c_client_tuner = dvb_module_probe("si2157", "si2146",
1337                          adapter,
1338                          0x60, &si2157_config);
1339     if (!dvb->i2c_client_tuner) {
1340         dvb_module_release(dvb->i2c_client_demod);
1341         return -ENODEV;
1342     }
1343 
1344     return 0;
1345 }
1346 
1347 static int em28178_dvb_init_plex_px_bcud(struct em28xx *dev)
1348 {
1349     struct em28xx_dvb *dvb = dev->dvb;
1350     struct tc90522_config tc90522_config = {};
1351     struct qm1d1c0042_config qm1d1c0042_config = {};
1352 
1353     /* attach demod */
1354     dvb->i2c_client_demod = dvb_module_probe("tc90522", "tc90522sat",
1355                          &dev->i2c_adap[dev->def_i2c_bus],
1356                          0x15, &tc90522_config);
1357     if (!dvb->i2c_client_demod)
1358         return -ENODEV;
1359 
1360     /* attach tuner */
1361     qm1d1c0042_config.fe = tc90522_config.fe;
1362     qm1d1c0042_config.lpf = 1;
1363 
1364     dvb->i2c_client_tuner = dvb_module_probe("qm1d1c0042", NULL,
1365                          tc90522_config.tuner_i2c,
1366                          0x61, &qm1d1c0042_config);
1367     if (!dvb->i2c_client_tuner) {
1368         dvb_module_release(dvb->i2c_client_demod);
1369         return -ENODEV;
1370     }
1371 
1372     dvb->fe[0] = tc90522_config.fe;
1373     px_bcud_init(dev);
1374 
1375     return 0;
1376 }
1377 
1378 static int em28174_dvb_init_hauppauge_wintv_dualhd_dvb(struct em28xx *dev)
1379 {
1380     struct em28xx_dvb *dvb = dev->dvb;
1381     struct i2c_adapter *adapter;
1382     struct si2168_config si2168_config = {};
1383     struct si2157_config si2157_config = {};
1384     unsigned char addr;
1385 
1386     /* attach demod */
1387     si2168_config.i2c_adapter = &adapter;
1388     si2168_config.fe = &dvb->fe[0];
1389     si2168_config.ts_mode = SI2168_TS_SERIAL;
1390     si2168_config.spectral_inversion = true;
1391     addr = (dev->ts == PRIMARY_TS) ? 0x64 : 0x67;
1392 
1393     dvb->i2c_client_demod = dvb_module_probe("si2168", NULL,
1394                          &dev->i2c_adap[dev->def_i2c_bus],
1395                          addr, &si2168_config);
1396     if (!dvb->i2c_client_demod)
1397         return -ENODEV;
1398 
1399     /* attach tuner */
1400     memset(&si2157_config, 0, sizeof(si2157_config));
1401     si2157_config.fe = dvb->fe[0];
1402     si2157_config.if_port = 1;
1403 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
1404     si2157_config.mdev = dev->media_dev;
1405 #endif
1406     addr = (dev->ts == PRIMARY_TS) ? 0x60 : 0x63;
1407 
1408     dvb->i2c_client_tuner = dvb_module_probe("si2157", NULL,
1409                          adapter,
1410                          addr, &si2157_config);
1411     if (!dvb->i2c_client_tuner) {
1412         dvb_module_release(dvb->i2c_client_demod);
1413         return -ENODEV;
1414     }
1415 
1416     return 0;
1417 }
1418 
1419 static int em28174_dvb_init_hauppauge_wintv_dualhd_01595(struct em28xx *dev)
1420 {
1421     struct em28xx_dvb *dvb = dev->dvb;
1422     struct i2c_adapter *adapter;
1423     struct lgdt3306a_config lgdt3306a_config =  {};
1424     struct si2157_config si2157_config = {};
1425     unsigned char addr;
1426 
1427     /* attach demod */
1428     lgdt3306a_config = hauppauge_01595_lgdt3306a_config;
1429     lgdt3306a_config.fe = &dvb->fe[0];
1430     lgdt3306a_config.i2c_adapter = &adapter;
1431     addr = (dev->ts == PRIMARY_TS) ? 0x59 : 0x0e;
1432 
1433     dvb->i2c_client_demod = dvb_module_probe("lgdt3306a", NULL,
1434                          &dev->i2c_adap[dev->def_i2c_bus],
1435                          addr, &lgdt3306a_config);
1436     if (!dvb->i2c_client_demod)
1437         return -ENODEV;
1438 
1439     /* attach tuner */
1440     si2157_config.fe = dvb->fe[0];
1441     si2157_config.if_port = 1;
1442     si2157_config.inversion = 1;
1443 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
1444     si2157_config.mdev = dev->media_dev;
1445 #endif
1446     addr = (dev->ts == PRIMARY_TS) ? 0x60 : 0x62;
1447 
1448     dvb->i2c_client_tuner = dvb_module_probe("si2157", NULL,
1449                          adapter,
1450                          addr, &si2157_config);
1451     if (!dvb->i2c_client_tuner) {
1452         dvb_module_release(dvb->i2c_client_demod);
1453         return -ENODEV;
1454     }
1455 
1456     return 0;
1457 }
1458 
1459 static int em2874_dvb_init_hauppauge_usb_quadhd(struct em28xx *dev)
1460 {
1461     struct em28xx_dvb *dvb = dev->dvb;
1462     struct mxl692_config mxl692_config = {};
1463     unsigned char addr;
1464 
1465     /* attach demod/tuner combo */
1466     mxl692_config.id = (dev->ts == PRIMARY_TS) ? 0 : 1;
1467     mxl692_config.fe = &dvb->fe[0];
1468     addr = (dev->ts == PRIMARY_TS) ? 0x60 : 0x63;
1469 
1470     dvb->i2c_client_demod = dvb_module_probe("mxl692", NULL,
1471                          &dev->i2c_adap[dev->def_i2c_bus],
1472                          addr, &mxl692_config);
1473     if (!dvb->i2c_client_demod)
1474         return -ENODEV;
1475 
1476     return 0;
1477 }
1478 
1479 static int em28xx_dvb_init(struct em28xx *dev)
1480 {
1481     int result = 0, dvb_alt = 0;
1482     struct em28xx_dvb *dvb;
1483     struct usb_device *udev;
1484 
1485     if (dev->is_audio_only) {
1486         /* Shouldn't initialize IR for this interface */
1487         return 0;
1488     }
1489 
1490     if (!dev->board.has_dvb) {
1491         /* This device does not support the extension */
1492         return 0;
1493     }
1494 
1495     dev_info(&dev->intf->dev, "Binding DVB extension\n");
1496 
1497     dvb = kzalloc(sizeof(*dvb), GFP_KERNEL);
1498     if (!dvb)
1499         return -ENOMEM;
1500 
1501     dev->dvb = dvb;
1502     dvb->fe[0] = NULL;
1503     dvb->fe[1] = NULL;
1504 
1505     /* pre-allocate DVB usb transfer buffers */
1506     if (dev->dvb_xfer_bulk) {
1507         result = em28xx_alloc_urbs(dev, EM28XX_DIGITAL_MODE,
1508                        dev->dvb_xfer_bulk,
1509                        EM28XX_DVB_NUM_BUFS,
1510                        512,
1511                        EM28XX_DVB_BULK_PACKET_MULTIPLIER);
1512     } else {
1513         result = em28xx_alloc_urbs(dev, EM28XX_DIGITAL_MODE,
1514                        dev->dvb_xfer_bulk,
1515                        EM28XX_DVB_NUM_BUFS,
1516                        dev->dvb_max_pkt_size_isoc,
1517                        EM28XX_DVB_NUM_ISOC_PACKETS);
1518     }
1519     if (result) {
1520         dev_err(&dev->intf->dev,
1521             "failed to pre-allocate USB transfer buffers for DVB.\n");
1522         kfree(dvb);
1523         dev->dvb = NULL;
1524         return result;
1525     }
1526 
1527     mutex_lock(&dev->lock);
1528     em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
1529     /* init frontend */
1530     switch (dev->model) {
1531     case EM2874_BOARD_LEADERSHIP_ISDBT:
1532         dvb->fe[0] = dvb_attach(s921_attach,
1533                     &sharp_isdbt,
1534                     &dev->i2c_adap[dev->def_i2c_bus]);
1535 
1536         if (!dvb->fe[0]) {
1537             result = -EINVAL;
1538             goto out_free;
1539         }
1540 
1541         break;
1542     case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_850:
1543     case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_950:
1544     case EM2880_BOARD_PINNACLE_PCTV_HD_PRO:
1545     case EM2880_BOARD_AMD_ATI_TV_WONDER_HD_600:
1546         dvb->fe[0] = dvb_attach(lgdt330x_attach,
1547                     &em2880_lgdt3303_dev,
1548                     0x0e,
1549                     &dev->i2c_adap[dev->def_i2c_bus]);
1550         if (em28xx_attach_xc3028(0x61, dev) < 0) {
1551             result = -EINVAL;
1552             goto out_free;
1553         }
1554         break;
1555     case EM2880_BOARD_KWORLD_DVB_310U:
1556         dvb->fe[0] = dvb_attach(zl10353_attach,
1557                     &em28xx_zl10353_with_xc3028,
1558                     &dev->i2c_adap[dev->def_i2c_bus]);
1559         if (em28xx_attach_xc3028(0x61, dev) < 0) {
1560             result = -EINVAL;
1561             goto out_free;
1562         }
1563         break;
1564     case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900:
1565     case EM2882_BOARD_TERRATEC_HYBRID_XS:
1566     case EM2880_BOARD_EMPIRE_DUAL_TV:
1567     case EM2882_BOARD_ZOLID_HYBRID_TV_STICK:
1568         dvb->fe[0] = dvb_attach(zl10353_attach,
1569                     &em28xx_zl10353_xc3028_no_i2c_gate,
1570                     &dev->i2c_adap[dev->def_i2c_bus]);
1571         if (em28xx_attach_xc3028(0x61, dev) < 0) {
1572             result = -EINVAL;
1573             goto out_free;
1574         }
1575         break;
1576     case EM2880_BOARD_TERRATEC_HYBRID_XS:
1577     case EM2880_BOARD_TERRATEC_HYBRID_XS_FR:
1578     case EM2881_BOARD_PINNACLE_HYBRID_PRO:
1579     case EM2882_BOARD_DIKOM_DK300:
1580     case EM2882_BOARD_KWORLD_VS_DVBT:
1581         /*
1582          * Those boards could have either a zl10353 or a mt352.
1583          * If the chip id isn't for zl10353, try mt352.
1584          */
1585         dvb->fe[0] = dvb_attach(zl10353_attach,
1586                     &em28xx_zl10353_xc3028_no_i2c_gate,
1587                     &dev->i2c_adap[dev->def_i2c_bus]);
1588         if (!dvb->fe[0])
1589             dvb->fe[0] = dvb_attach(mt352_attach,
1590                         &terratec_xs_mt352_cfg,
1591                         &dev->i2c_adap[dev->def_i2c_bus]);
1592 
1593         if (em28xx_attach_xc3028(0x61, dev) < 0) {
1594             result = -EINVAL;
1595             goto out_free;
1596         }
1597         break;
1598     case EM2870_BOARD_TERRATEC_XS_MT2060:
1599         dvb->fe[0] = dvb_attach(zl10353_attach,
1600                     &em28xx_zl10353_no_i2c_gate_dev,
1601                     &dev->i2c_adap[dev->def_i2c_bus]);
1602         if (dvb->fe[0]) {
1603             dvb_attach(mt2060_attach, dvb->fe[0],
1604                    &dev->i2c_adap[dev->def_i2c_bus],
1605                    &em28xx_mt2060_config, 1220);
1606         }
1607         break;
1608     case EM2870_BOARD_KWORLD_355U:
1609         dvb->fe[0] = dvb_attach(zl10353_attach,
1610                     &em28xx_zl10353_no_i2c_gate_dev,
1611                     &dev->i2c_adap[dev->def_i2c_bus]);
1612         if (dvb->fe[0])
1613             dvb_attach(qt1010_attach, dvb->fe[0],
1614                    &dev->i2c_adap[dev->def_i2c_bus],
1615                    &em28xx_qt1010_config);
1616         break;
1617     case EM2883_BOARD_KWORLD_HYBRID_330U:
1618     case EM2882_BOARD_EVGA_INDTUBE:
1619         dvb->fe[0] = dvb_attach(s5h1409_attach,
1620                     &em28xx_s5h1409_with_xc3028,
1621                     &dev->i2c_adap[dev->def_i2c_bus]);
1622         if (em28xx_attach_xc3028(0x61, dev) < 0) {
1623             result = -EINVAL;
1624             goto out_free;
1625         }
1626         break;
1627     case EM2882_BOARD_KWORLD_ATSC_315U:
1628         dvb->fe[0] = dvb_attach(lgdt330x_attach,
1629                     &em2880_lgdt3303_dev,
1630                     0x0e,
1631                     &dev->i2c_adap[dev->def_i2c_bus]);
1632         if (dvb->fe[0]) {
1633             if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
1634                     &dev->i2c_adap[dev->def_i2c_bus],
1635                     0x61, TUNER_THOMSON_DTT761X)) {
1636                 result = -EINVAL;
1637                 goto out_free;
1638             }
1639         }
1640         break;
1641     case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900_R2:
1642     case EM2882_BOARD_PINNACLE_HYBRID_PRO_330E:
1643         dvb->fe[0] = dvb_attach(drxd_attach, &em28xx_drxd, NULL,
1644                     &dev->i2c_adap[dev->def_i2c_bus],
1645                     &dev->intf->dev);
1646         if (em28xx_attach_xc3028(0x61, dev) < 0) {
1647             result = -EINVAL;
1648             goto out_free;
1649         }
1650         break;
1651     case EM2870_BOARD_REDDO_DVB_C_USB_BOX:
1652         /* Philips CU1216L NIM (Philips TDA10023 + Infineon TUA6034) */
1653         dvb->fe[0] = dvb_attach(tda10023_attach,
1654                     &em28xx_tda10023_config,
1655                     &dev->i2c_adap[dev->def_i2c_bus],
1656                     0x48);
1657         if (dvb->fe[0]) {
1658             if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
1659                     &dev->i2c_adap[dev->def_i2c_bus],
1660                     0x60, TUNER_PHILIPS_CU1216L)) {
1661                 result = -EINVAL;
1662                 goto out_free;
1663             }
1664         }
1665         break;
1666     case EM2870_BOARD_KWORLD_A340:
1667         dvb->fe[0] = dvb_attach(lgdt3305_attach,
1668                     &em2870_lgdt3304_dev,
1669                     &dev->i2c_adap[dev->def_i2c_bus]);
1670         if (!dvb->fe[0]) {
1671             result = -EINVAL;
1672             goto out_free;
1673         }
1674         if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1675                 &dev->i2c_adap[dev->def_i2c_bus],
1676                 &kworld_a340_config)) {
1677             dvb_frontend_detach(dvb->fe[0]);
1678             result = -EINVAL;
1679             goto out_free;
1680         }
1681         break;
1682     case EM28174_BOARD_PCTV_290E:
1683         /* set default GPIO0 for LNA, used if GPIOLIB is undefined */
1684         dvb->lna_gpio = CXD2820R_GPIO_E | CXD2820R_GPIO_O |
1685                 CXD2820R_GPIO_L;
1686         dvb->fe[0] = dvb_attach(cxd2820r_attach,
1687                     &em28xx_cxd2820r_config,
1688                     &dev->i2c_adap[dev->def_i2c_bus],
1689                     &dvb->lna_gpio);
1690         if (dvb->fe[0]) {
1691             /* FE 0 attach tuner */
1692             if (!dvb_attach(tda18271_attach,
1693                     dvb->fe[0],
1694                     0x60,
1695                     &dev->i2c_adap[dev->def_i2c_bus],
1696                     &em28xx_cxd2820r_tda18271_config)) {
1697                 dvb_frontend_detach(dvb->fe[0]);
1698                 result = -EINVAL;
1699                 goto out_free;
1700             }
1701 
1702 #ifdef CONFIG_GPIOLIB
1703             /* enable LNA for DVB-T, DVB-T2 and DVB-C */
1704             result = gpio_request_one(dvb->lna_gpio,
1705                           GPIOF_OUT_INIT_LOW, NULL);
1706             if (result)
1707                 dev_err(&dev->intf->dev,
1708                     "gpio request failed %d\n",
1709                     result);
1710             else
1711                 gpio_free(dvb->lna_gpio);
1712 
1713             result = 0; /* continue even set LNA fails */
1714 #endif
1715             dvb->fe[0]->ops.set_lna = em28xx_pctv_290e_set_lna;
1716         }
1717 
1718         break;
1719     case EM2884_BOARD_HAUPPAUGE_WINTV_HVR_930C:
1720     {
1721         struct xc5000_config cfg = {};
1722 
1723         hauppauge_hvr930c_init(dev);
1724 
1725         dvb->fe[0] = dvb_attach(drxk_attach,
1726                     &hauppauge_930c_drxk,
1727                     &dev->i2c_adap[dev->def_i2c_bus]);
1728         if (!dvb->fe[0]) {
1729             result = -EINVAL;
1730             goto out_free;
1731         }
1732         /* FIXME: do we need a pll semaphore? */
1733         dvb->fe[0]->sec_priv = dvb;
1734         sema_init(&dvb->pll_mutex, 1);
1735         dvb->gate_ctrl = dvb->fe[0]->ops.i2c_gate_ctrl;
1736         dvb->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
1737 
1738         /* Attach xc5000 */
1739         cfg.i2c_address  = 0x61;
1740         cfg.if_khz = 4000;
1741 
1742         if (dvb->fe[0]->ops.i2c_gate_ctrl)
1743             dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 1);
1744         if (!dvb_attach(xc5000_attach, dvb->fe[0],
1745                 &dev->i2c_adap[dev->def_i2c_bus], &cfg)) {
1746             result = -EINVAL;
1747             goto out_free;
1748         }
1749         if (dvb->fe[0]->ops.i2c_gate_ctrl)
1750             dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 0);
1751 
1752         break;
1753     }
1754     case EM2884_BOARD_TERRATEC_H5:
1755         terratec_h5_init(dev);
1756 
1757         dvb->fe[0] = dvb_attach(drxk_attach, &terratec_h5_drxk,
1758                     &dev->i2c_adap[dev->def_i2c_bus]);
1759         if (!dvb->fe[0]) {
1760             result = -EINVAL;
1761             goto out_free;
1762         }
1763         /* FIXME: do we need a pll semaphore? */
1764         dvb->fe[0]->sec_priv = dvb;
1765         sema_init(&dvb->pll_mutex, 1);
1766         dvb->gate_ctrl = dvb->fe[0]->ops.i2c_gate_ctrl;
1767         dvb->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
1768 
1769         /* Attach tda18271 to DVB-C frontend */
1770         if (dvb->fe[0]->ops.i2c_gate_ctrl)
1771             dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 1);
1772         if (!dvb_attach(tda18271c2dd_attach, dvb->fe[0],
1773                 &dev->i2c_adap[dev->def_i2c_bus], 0x60)) {
1774             result = -EINVAL;
1775             goto out_free;
1776         }
1777         if (dvb->fe[0]->ops.i2c_gate_ctrl)
1778             dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 0);
1779 
1780         break;
1781     case EM2884_BOARD_C3TECH_DIGITAL_DUO:
1782         dvb->fe[0] = dvb_attach(mb86a20s_attach,
1783                     &c3tech_duo_mb86a20s_config,
1784                     &dev->i2c_adap[dev->def_i2c_bus]);
1785         if (dvb->fe[0])
1786             dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1787                    &dev->i2c_adap[dev->def_i2c_bus],
1788                    &c3tech_duo_tda18271_config);
1789         break;
1790     case EM28174_BOARD_PCTV_460E:
1791         result = em28174_dvb_init_pctv_460e(dev);
1792         if (result)
1793             goto out_free;
1794         break;
1795     case EM2874_BOARD_DELOCK_61959:
1796     case EM2874_BOARD_MAXMEDIA_UB425_TC:
1797         /* attach demodulator */
1798         dvb->fe[0] = dvb_attach(drxk_attach, &maxmedia_ub425_tc_drxk,
1799                     &dev->i2c_adap[dev->def_i2c_bus]);
1800 
1801         if (dvb->fe[0]) {
1802             /* disable I2C-gate */
1803             dvb->fe[0]->ops.i2c_gate_ctrl = NULL;
1804 
1805             /* attach tuner */
1806             if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1807                     &dev->i2c_adap[dev->def_i2c_bus],
1808                     &em28xx_cxd2820r_tda18271_config)) {
1809                 dvb_frontend_detach(dvb->fe[0]);
1810                 result = -EINVAL;
1811                 goto out_free;
1812             }
1813         }
1814         break;
1815     case EM2884_BOARD_PCTV_510E:
1816     case EM2884_BOARD_PCTV_520E:
1817         pctv_520e_init(dev);
1818 
1819         /* attach demodulator */
1820         dvb->fe[0] = dvb_attach(drxk_attach, &pctv_520e_drxk,
1821                     &dev->i2c_adap[dev->def_i2c_bus]);
1822 
1823         if (dvb->fe[0]) {
1824             /* attach tuner */
1825             if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1826                     &dev->i2c_adap[dev->def_i2c_bus],
1827                     &em28xx_cxd2820r_tda18271_config)) {
1828                 dvb_frontend_detach(dvb->fe[0]);
1829                 result = -EINVAL;
1830                 goto out_free;
1831             }
1832         }
1833         break;
1834     case EM2884_BOARD_ELGATO_EYETV_HYBRID_2008:
1835     case EM2884_BOARD_CINERGY_HTC_STICK:
1836     case EM2884_BOARD_TERRATEC_H6:
1837         terratec_htc_stick_init(dev);
1838 
1839         /* attach demodulator */
1840         dvb->fe[0] = dvb_attach(drxk_attach, &terratec_htc_stick_drxk,
1841                     &dev->i2c_adap[dev->def_i2c_bus]);
1842         if (!dvb->fe[0]) {
1843             result = -EINVAL;
1844             goto out_free;
1845         }
1846 
1847         /* Attach the demodulator. */
1848         if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1849                 &dev->i2c_adap[dev->def_i2c_bus],
1850                 &em28xx_cxd2820r_tda18271_config)) {
1851             result = -EINVAL;
1852             goto out_free;
1853         }
1854         break;
1855     case EM2884_BOARD_TERRATEC_HTC_USB_XS:
1856         terratec_htc_usb_xs_init(dev);
1857 
1858         /* attach demodulator */
1859         dvb->fe[0] = dvb_attach(drxk_attach, &terratec_htc_stick_drxk,
1860                     &dev->i2c_adap[dev->def_i2c_bus]);
1861         if (!dvb->fe[0]) {
1862             result = -EINVAL;
1863             goto out_free;
1864         }
1865 
1866         /* Attach the demodulator. */
1867         if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1868                 &dev->i2c_adap[dev->def_i2c_bus],
1869                 &em28xx_cxd2820r_tda18271_config)) {
1870             result = -EINVAL;
1871             goto out_free;
1872         }
1873         break;
1874     case EM2874_BOARD_KWORLD_UB435Q_V2:
1875         dvb->fe[0] = dvb_attach(lgdt3305_attach,
1876                     &em2874_lgdt3305_dev,
1877                     &dev->i2c_adap[dev->def_i2c_bus]);
1878         if (!dvb->fe[0]) {
1879             result = -EINVAL;
1880             goto out_free;
1881         }
1882 
1883         /* Attach the demodulator. */
1884         if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1885                 &dev->i2c_adap[dev->def_i2c_bus],
1886                 &kworld_ub435q_v2_config)) {
1887             result = -EINVAL;
1888             goto out_free;
1889         }
1890         break;
1891     case EM2874_BOARD_KWORLD_UB435Q_V3:
1892     {
1893         struct i2c_adapter *adapter = &dev->i2c_adap[dev->def_i2c_bus];
1894 
1895         dvb->fe[0] = dvb_attach(lgdt3305_attach,
1896                     &em2874_lgdt3305_nogate_dev,
1897                     &dev->i2c_adap[dev->def_i2c_bus]);
1898         if (!dvb->fe[0]) {
1899             result = -EINVAL;
1900             goto out_free;
1901         }
1902 
1903         /* attach tuner */
1904         kworld_ub435q_v3_config.fe = dvb->fe[0];
1905 
1906         dvb->i2c_client_tuner = dvb_module_probe("tda18212", NULL,
1907                              adapter, 0x60,
1908                              &kworld_ub435q_v3_config);
1909         if (!dvb->i2c_client_tuner) {
1910             dvb_frontend_detach(dvb->fe[0]);
1911             result = -ENODEV;
1912             goto out_free;
1913         }
1914         break;
1915     }
1916     case EM2874_BOARD_PCTV_HD_MINI_80E:
1917         dvb->fe[0] = dvb_attach(drx39xxj_attach,
1918                     &dev->i2c_adap[dev->def_i2c_bus]);
1919         if (dvb->fe[0]) {
1920             dvb->fe[0] = dvb_attach(tda18271_attach, dvb->fe[0],
1921                         0x60,
1922                         &dev->i2c_adap[dev->def_i2c_bus],
1923                         &pinnacle_80e_dvb_config);
1924             if (!dvb->fe[0]) {
1925                 result = -EINVAL;
1926                 goto out_free;
1927             }
1928         }
1929         break;
1930     case EM28178_BOARD_PCTV_461E:
1931         result = em28178_dvb_init_pctv_461e(dev);
1932         if (result)
1933             goto out_free;
1934         break;
1935     case EM28178_BOARD_PCTV_461E_V2:
1936         result = em28178_dvb_init_pctv_461e_v2(dev);
1937         if (result)
1938             goto out_free;
1939         break;
1940     case EM28178_BOARD_PCTV_292E:
1941         result = em28178_dvb_init_pctv_292e(dev);
1942         if (result)
1943             goto out_free;
1944         break;
1945     case EM28178_BOARD_TERRATEC_T2_STICK_HD:
1946         result = em28178_dvb_init_terratec_t2_stick_hd(dev);
1947         if (result)
1948             goto out_free;
1949         break;
1950     case EM28178_BOARD_PLEX_PX_BCUD:
1951         result = em28178_dvb_init_plex_px_bcud(dev);
1952         if (result)
1953             goto out_free;
1954         break;
1955     case EM28174_BOARD_HAUPPAUGE_WINTV_DUALHD_DVB:
1956         result = em28174_dvb_init_hauppauge_wintv_dualhd_dvb(dev);
1957         if (result)
1958             goto out_free;
1959         break;
1960     case EM28174_BOARD_HAUPPAUGE_WINTV_DUALHD_01595:
1961         result = em28174_dvb_init_hauppauge_wintv_dualhd_01595(dev);
1962         if (result)
1963             goto out_free;
1964         break;
1965     case EM2874_BOARD_HAUPPAUGE_USB_QUADHD:
1966         result = em2874_dvb_init_hauppauge_usb_quadhd(dev);
1967         if (result)
1968             goto out_free;
1969         break;
1970     default:
1971         dev_err(&dev->intf->dev,
1972             "The frontend of your DVB/ATSC card isn't supported yet\n");
1973         break;
1974     }
1975     if (!dvb->fe[0]) {
1976         dev_err(&dev->intf->dev, "frontend initialization failed\n");
1977         result = -EINVAL;
1978         goto out_free;
1979     }
1980     /* define general-purpose callback pointer */
1981     dvb->fe[0]->callback = em28xx_tuner_callback;
1982     if (dvb->fe[1])
1983         dvb->fe[1]->callback = em28xx_tuner_callback;
1984 
1985     /* register everything */
1986     result = em28xx_register_dvb(dvb, THIS_MODULE, dev, &dev->intf->dev);
1987 
1988     if (result < 0)
1989         goto out_free;
1990 
1991     if (dev->dvb_xfer_bulk) {
1992         dvb_alt = 0;
1993     } else { /* isoc */
1994         dvb_alt = dev->dvb_alt_isoc;
1995     }
1996 
1997     udev = interface_to_usbdev(dev->intf);
1998     usb_set_interface(udev, dev->ifnum, dvb_alt);
1999     dev_info(&dev->intf->dev, "DVB extension successfully initialized\n");
2000 
2001     kref_get(&dev->ref);
2002 
2003 ret:
2004     em28xx_set_mode(dev, EM28XX_SUSPEND);
2005     mutex_unlock(&dev->lock);
2006     return result;
2007 
2008 out_free:
2009     em28xx_uninit_usb_xfer(dev, EM28XX_DIGITAL_MODE);
2010     kfree(dvb);
2011     dev->dvb = NULL;
2012     goto ret;
2013 }
2014 
2015 static inline void prevent_sleep(struct dvb_frontend_ops *ops)
2016 {
2017     ops->set_voltage = NULL;
2018     ops->sleep = NULL;
2019     ops->tuner_ops.sleep = NULL;
2020 }
2021 
2022 static int em28xx_dvb_fini(struct em28xx *dev)
2023 {
2024     struct em28xx_dvb *dvb;
2025 
2026     if (dev->is_audio_only) {
2027         /* Shouldn't initialize IR for this interface */
2028         return 0;
2029     }
2030 
2031     if (!dev->board.has_dvb) {
2032         /* This device does not support the extension */
2033         return 0;
2034     }
2035 
2036     if (!dev->dvb)
2037         return 0;
2038 
2039     dev_info(&dev->intf->dev, "Closing DVB extension\n");
2040 
2041     dvb = dev->dvb;
2042 
2043     em28xx_uninit_usb_xfer(dev, EM28XX_DIGITAL_MODE);
2044 
2045     if (dev->disconnected) {
2046         /*
2047          * We cannot tell the device to sleep
2048          * once it has been unplugged.
2049          */
2050         if (dvb->fe[0]) {
2051             prevent_sleep(&dvb->fe[0]->ops);
2052             dvb->fe[0]->exit = DVB_FE_DEVICE_REMOVED;
2053         }
2054         if (dvb->fe[1]) {
2055             prevent_sleep(&dvb->fe[1]->ops);
2056             dvb->fe[1]->exit = DVB_FE_DEVICE_REMOVED;
2057         }
2058     }
2059 
2060     em28xx_unregister_dvb(dvb);
2061 
2062     /* release I2C module bindings */
2063     dvb_module_release(dvb->i2c_client_sec);
2064     dvb_module_release(dvb->i2c_client_tuner);
2065     dvb_module_release(dvb->i2c_client_demod);
2066 
2067     kfree(dvb);
2068     dev->dvb = NULL;
2069     kref_put(&dev->ref, em28xx_free_device);
2070 
2071     return 0;
2072 }
2073 
2074 static int em28xx_dvb_suspend(struct em28xx *dev)
2075 {
2076     int ret = 0;
2077 
2078     if (dev->is_audio_only)
2079         return 0;
2080 
2081     if (!dev->board.has_dvb)
2082         return 0;
2083 
2084     dev_info(&dev->intf->dev, "Suspending DVB extension\n");
2085     if (dev->dvb) {
2086         struct em28xx_dvb *dvb = dev->dvb;
2087 
2088         if (dvb->fe[0]) {
2089             ret = dvb_frontend_suspend(dvb->fe[0]);
2090             dev_info(&dev->intf->dev, "fe0 suspend %d\n", ret);
2091         }
2092         if (dvb->fe[1]) {
2093             dvb_frontend_suspend(dvb->fe[1]);
2094             dev_info(&dev->intf->dev, "fe1 suspend %d\n", ret);
2095         }
2096     }
2097 
2098     return 0;
2099 }
2100 
2101 static int em28xx_dvb_resume(struct em28xx *dev)
2102 {
2103     int ret = 0;
2104 
2105     if (dev->is_audio_only)
2106         return 0;
2107 
2108     if (!dev->board.has_dvb)
2109         return 0;
2110 
2111     dev_info(&dev->intf->dev, "Resuming DVB extension\n");
2112     if (dev->dvb) {
2113         struct em28xx_dvb *dvb = dev->dvb;
2114 
2115         if (dvb->fe[0]) {
2116             ret = dvb_frontend_resume(dvb->fe[0]);
2117             dev_info(&dev->intf->dev, "fe0 resume %d\n", ret);
2118         }
2119 
2120         if (dvb->fe[1]) {
2121             ret = dvb_frontend_resume(dvb->fe[1]);
2122             dev_info(&dev->intf->dev, "fe1 resume %d\n", ret);
2123         }
2124     }
2125 
2126     return 0;
2127 }
2128 
2129 static struct em28xx_ops dvb_ops = {
2130     .id   = EM28XX_DVB,
2131     .name = "Em28xx dvb Extension",
2132     .init = em28xx_dvb_init,
2133     .fini = em28xx_dvb_fini,
2134     .suspend = em28xx_dvb_suspend,
2135     .resume = em28xx_dvb_resume,
2136 };
2137 
2138 static int __init em28xx_dvb_register(void)
2139 {
2140     return em28xx_register_extension(&dvb_ops);
2141 }
2142 
2143 static void __exit em28xx_dvb_unregister(void)
2144 {
2145     em28xx_unregister_extension(&dvb_ops);
2146 }
2147 
2148 module_init(em28xx_dvb_register);
2149 module_exit(em28xx_dvb_unregister);