Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /* DVB USB compliant linux driver for Conexant USB reference design.
0003  *
0004  * The Conexant reference design I saw on their website was only for analogue
0005  * capturing (using the cx25842). The box I took to write this driver (reverse
0006  * engineered) is the one labeled Medion MD95700. In addition to the cx25842
0007  * for analogue capturing it also has a cx22702 DVB-T demodulator on the main
0008  * board. Besides it has a atiremote (X10) and a USB2.0 hub onboard.
0009  *
0010  * Maybe it is a little bit premature to call this driver cxusb, but I assume
0011  * the USB protocol is identical or at least inherited from the reference
0012  * design, so it can be reused for the "analogue-only" device (if it will
0013  * appear at all).
0014  *
0015  *
0016  * Copyright (C) 2005 Patrick Boettcher (patrick.boettcher@posteo.de)
0017  * Copyright (C) 2006 Michael Krufky (mkrufky@linuxtv.org)
0018  * Copyright (C) 2006, 2007 Chris Pascoe (c.pascoe@itee.uq.edu.au)
0019  * Copyright (C) 2011, 2017 Maciej S. Szmigiero (mail@maciej.szmigiero.name)
0020  *
0021  * see Documentation/driver-api/media/drivers/dvb-usb.rst for more information
0022  */
0023 #include <media/tuner.h>
0024 #include <linux/delay.h>
0025 #include <linux/device.h>
0026 #include <linux/kernel.h>
0027 #include <linux/slab.h>
0028 #include <linux/string.h>
0029 #include <linux/vmalloc.h>
0030 
0031 #include "cxusb.h"
0032 
0033 #include "cx22702.h"
0034 #include "lgdt330x.h"
0035 #include "mt352.h"
0036 #include "mt352_priv.h"
0037 #include "zl10353.h"
0038 #include "xc2028.h"
0039 #include "tuner-simple.h"
0040 #include "mxl5005s.h"
0041 #include "max2165.h"
0042 #include "dib7000p.h"
0043 #include "dib0070.h"
0044 #include "lgs8gxx.h"
0045 #include "atbm8830.h"
0046 #include "si2168.h"
0047 #include "si2157.h"
0048 
0049 /* debug */
0050 int dvb_usb_cxusb_debug;
0051 module_param_named(debug, dvb_usb_cxusb_debug, int, 0644);
0052 MODULE_PARM_DESC(debug, "set debugging level (see cxusb.h)."
0053          DVB_USB_DEBUG_STATUS);
0054 
0055 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
0056 
0057 enum cxusb_table_index {
0058     MEDION_MD95700,
0059     DVICO_BLUEBIRD_LG064F_COLD,
0060     DVICO_BLUEBIRD_LG064F_WARM,
0061     DVICO_BLUEBIRD_DUAL_1_COLD,
0062     DVICO_BLUEBIRD_DUAL_1_WARM,
0063     DVICO_BLUEBIRD_LGZ201_COLD,
0064     DVICO_BLUEBIRD_LGZ201_WARM,
0065     DVICO_BLUEBIRD_TH7579_COLD,
0066     DVICO_BLUEBIRD_TH7579_WARM,
0067     DIGITALNOW_BLUEBIRD_DUAL_1_COLD,
0068     DIGITALNOW_BLUEBIRD_DUAL_1_WARM,
0069     DVICO_BLUEBIRD_DUAL_2_COLD,
0070     DVICO_BLUEBIRD_DUAL_2_WARM,
0071     DVICO_BLUEBIRD_DUAL_4,
0072     DVICO_BLUEBIRD_DVB_T_NANO_2,
0073     DVICO_BLUEBIRD_DVB_T_NANO_2_NFW_WARM,
0074     AVERMEDIA_VOLAR_A868R,
0075     DVICO_BLUEBIRD_DUAL_4_REV_2,
0076     CONEXANT_D680_DMB,
0077     MYGICA_D689,
0078     NR__cxusb_table_index
0079 };
0080 
0081 static struct usb_device_id cxusb_table[];
0082 
0083 int cxusb_ctrl_msg(struct dvb_usb_device *d,
0084            u8 cmd, const u8 *wbuf, int wlen, u8 *rbuf, int rlen)
0085 {
0086     struct cxusb_state *st = d->priv;
0087     int ret;
0088 
0089     if (1 + wlen > MAX_XFER_SIZE) {
0090         warn("i2c wr: len=%d is too big!\n", wlen);
0091         return -EOPNOTSUPP;
0092     }
0093 
0094     if (rlen > MAX_XFER_SIZE) {
0095         warn("i2c rd: len=%d is too big!\n", rlen);
0096         return -EOPNOTSUPP;
0097     }
0098 
0099     mutex_lock(&d->data_mutex);
0100     st->data[0] = cmd;
0101     memcpy(&st->data[1], wbuf, wlen);
0102     ret = dvb_usb_generic_rw(d, st->data, 1 + wlen, st->data, rlen, 0);
0103     if (!ret && rbuf && rlen)
0104         memcpy(rbuf, st->data, rlen);
0105 
0106     mutex_unlock(&d->data_mutex);
0107     return ret;
0108 }
0109 
0110 /* GPIO */
0111 static void cxusb_gpio_tuner(struct dvb_usb_device *d, int onoff)
0112 {
0113     struct cxusb_state *st = d->priv;
0114     u8 o[2], i;
0115 
0116     if (st->gpio_write_state[GPIO_TUNER] == onoff &&
0117         !st->gpio_write_refresh[GPIO_TUNER])
0118         return;
0119 
0120     o[0] = GPIO_TUNER;
0121     o[1] = onoff;
0122     cxusb_ctrl_msg(d, CMD_GPIO_WRITE, o, 2, &i, 1);
0123 
0124     if (i != 0x01)
0125         dev_info(&d->udev->dev, "gpio_write failed.\n");
0126 
0127     st->gpio_write_state[GPIO_TUNER] = onoff;
0128     st->gpio_write_refresh[GPIO_TUNER] = false;
0129 }
0130 
0131 static int cxusb_bluebird_gpio_rw(struct dvb_usb_device *d, u8 changemask,
0132                   u8 newval)
0133 {
0134     u8 o[2], gpio_state;
0135     int rc;
0136 
0137     o[0] = 0xff & ~changemask;  /* mask of bits to keep */
0138     o[1] = newval & changemask; /* new values for bits  */
0139 
0140     rc = cxusb_ctrl_msg(d, CMD_BLUEBIRD_GPIO_RW, o, 2, &gpio_state, 1);
0141     if (rc < 0 || (gpio_state & changemask) != (newval & changemask))
0142         dev_info(&d->udev->dev, "bluebird_gpio_write failed.\n");
0143 
0144     return rc < 0 ? rc : gpio_state;
0145 }
0146 
0147 static void cxusb_bluebird_gpio_pulse(struct dvb_usb_device *d, u8 pin, int low)
0148 {
0149     cxusb_bluebird_gpio_rw(d, pin, low ? 0 : pin);
0150     msleep(5);
0151     cxusb_bluebird_gpio_rw(d, pin, low ? pin : 0);
0152 }
0153 
0154 static void cxusb_nano2_led(struct dvb_usb_device *d, int onoff)
0155 {
0156     cxusb_bluebird_gpio_rw(d, 0x40, onoff ? 0 : 0x40);
0157 }
0158 
0159 static int cxusb_d680_dmb_gpio_tuner(struct dvb_usb_device *d,
0160                      u8 addr, int onoff)
0161 {
0162     u8  o[2] = {addr, onoff};
0163     u8  i;
0164     int rc;
0165 
0166     rc = cxusb_ctrl_msg(d, CMD_GPIO_WRITE, o, 2, &i, 1);
0167 
0168     if (rc < 0)
0169         return rc;
0170 
0171     if (i == 0x01)
0172         return 0;
0173 
0174     dev_info(&d->udev->dev, "gpio_write failed.\n");
0175     return -EIO;
0176 }
0177 
0178 /* I2C */
0179 static int cxusb_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
0180               int num)
0181 {
0182     struct dvb_usb_device *d = i2c_get_adapdata(adap);
0183     int ret;
0184     int i;
0185 
0186     if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
0187         return -EAGAIN;
0188 
0189     for (i = 0; i < num; i++) {
0190         if (le16_to_cpu(d->udev->descriptor.idVendor) == USB_VID_MEDION)
0191             switch (msg[i].addr) {
0192             case 0x63:
0193                 cxusb_gpio_tuner(d, 0);
0194                 break;
0195             default:
0196                 cxusb_gpio_tuner(d, 1);
0197                 break;
0198             }
0199 
0200         if (msg[i].flags & I2C_M_RD) {
0201             /* read only */
0202             u8 obuf[3], ibuf[MAX_XFER_SIZE];
0203 
0204             if (1 + msg[i].len > sizeof(ibuf)) {
0205                 warn("i2c rd: len=%d is too big!\n",
0206                      msg[i].len);
0207                 ret = -EOPNOTSUPP;
0208                 goto unlock;
0209             }
0210             obuf[0] = 0;
0211             obuf[1] = msg[i].len;
0212             obuf[2] = msg[i].addr;
0213             if (cxusb_ctrl_msg(d, CMD_I2C_READ,
0214                        obuf, 3,
0215                        ibuf, 1 + msg[i].len) < 0) {
0216                 warn("i2c read failed");
0217                 break;
0218             }
0219             memcpy(msg[i].buf, &ibuf[1], msg[i].len);
0220         } else if (i + 1 < num && (msg[i + 1].flags & I2C_M_RD) &&
0221                msg[i].addr == msg[i + 1].addr) {
0222             /* write to then read from same address */
0223             u8 obuf[MAX_XFER_SIZE], ibuf[MAX_XFER_SIZE];
0224 
0225             if (3 + msg[i].len > sizeof(obuf)) {
0226                 warn("i2c wr: len=%d is too big!\n",
0227                      msg[i].len);
0228                 ret = -EOPNOTSUPP;
0229                 goto unlock;
0230             }
0231             if (1 + msg[i + 1].len > sizeof(ibuf)) {
0232                 warn("i2c rd: len=%d is too big!\n",
0233                      msg[i + 1].len);
0234                 ret = -EOPNOTSUPP;
0235                 goto unlock;
0236             }
0237             obuf[0] = msg[i].len;
0238             obuf[1] = msg[i + 1].len;
0239             obuf[2] = msg[i].addr;
0240             memcpy(&obuf[3], msg[i].buf, msg[i].len);
0241 
0242             if (cxusb_ctrl_msg(d, CMD_I2C_READ,
0243                        obuf, 3 + msg[i].len,
0244                        ibuf, 1 + msg[i + 1].len) < 0)
0245                 break;
0246 
0247             if (ibuf[0] != 0x08)
0248                 dev_info(&d->udev->dev, "i2c read may have failed\n");
0249 
0250             memcpy(msg[i + 1].buf, &ibuf[1], msg[i + 1].len);
0251 
0252             i++;
0253         } else {
0254             /* write only */
0255             u8 obuf[MAX_XFER_SIZE], ibuf;
0256 
0257             if (2 + msg[i].len > sizeof(obuf)) {
0258                 warn("i2c wr: len=%d is too big!\n",
0259                      msg[i].len);
0260                 ret = -EOPNOTSUPP;
0261                 goto unlock;
0262             }
0263             obuf[0] = msg[i].addr;
0264             obuf[1] = msg[i].len;
0265             memcpy(&obuf[2], msg[i].buf, msg[i].len);
0266 
0267             if (cxusb_ctrl_msg(d, CMD_I2C_WRITE, obuf,
0268                        2 + msg[i].len, &ibuf, 1) < 0)
0269                 break;
0270             if (ibuf != 0x08)
0271                 dev_info(&d->udev->dev, "i2c write may have failed\n");
0272         }
0273     }
0274 
0275     if (i == num)
0276         ret = num;
0277     else
0278         ret = -EREMOTEIO;
0279 
0280 unlock:
0281     mutex_unlock(&d->i2c_mutex);
0282     return ret;
0283 }
0284 
0285 static u32 cxusb_i2c_func(struct i2c_adapter *adapter)
0286 {
0287     return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
0288 }
0289 
0290 static struct i2c_algorithm cxusb_i2c_algo = {
0291     .master_xfer   = cxusb_i2c_xfer,
0292     .functionality = cxusb_i2c_func,
0293 };
0294 
0295 static int _cxusb_power_ctrl(struct dvb_usb_device *d, int onoff)
0296 {
0297     u8 b = 0;
0298 
0299     dev_info(&d->udev->dev, "setting power %s\n", onoff ? "ON" : "OFF");
0300 
0301     if (onoff)
0302         return cxusb_ctrl_msg(d, CMD_POWER_ON, &b, 1, NULL, 0);
0303     else
0304         return cxusb_ctrl_msg(d, CMD_POWER_OFF, &b, 1, NULL, 0);
0305 }
0306 
0307 static int cxusb_power_ctrl(struct dvb_usb_device *d, int onoff)
0308 {
0309     bool is_medion = d->props.devices[0].warm_ids[0] == &cxusb_table[MEDION_MD95700];
0310     int ret;
0311 
0312     if (is_medion && !onoff) {
0313         struct cxusb_medion_dev *cxdev = d->priv;
0314 
0315         mutex_lock(&cxdev->open_lock);
0316 
0317         if (cxdev->open_type == CXUSB_OPEN_ANALOG) {
0318             dev_info(&d->udev->dev, "preventing DVB core from setting power OFF while we are in analog mode\n");
0319             ret = -EBUSY;
0320             goto ret_unlock;
0321         }
0322     }
0323 
0324     ret = _cxusb_power_ctrl(d, onoff);
0325 
0326 ret_unlock:
0327     if (is_medion && !onoff) {
0328         struct cxusb_medion_dev *cxdev = d->priv;
0329 
0330         mutex_unlock(&cxdev->open_lock);
0331     }
0332 
0333     return ret;
0334 }
0335 
0336 static int cxusb_aver_power_ctrl(struct dvb_usb_device *d, int onoff)
0337 {
0338     int ret;
0339 
0340     if (!onoff)
0341         return cxusb_ctrl_msg(d, CMD_POWER_OFF, NULL, 0, NULL, 0);
0342     if (d->state == DVB_USB_STATE_INIT &&
0343         usb_set_interface(d->udev, 0, 0) < 0)
0344         err("set interface failed");
0345     do {
0346         /* Nothing */
0347     } while (!(ret = cxusb_ctrl_msg(d, CMD_POWER_ON, NULL, 0, NULL, 0)) &&
0348          !(ret = cxusb_ctrl_msg(d, 0x15, NULL, 0, NULL, 0)) &&
0349          !(ret = cxusb_ctrl_msg(d, 0x17, NULL, 0, NULL, 0)) && 0);
0350 
0351     if (!ret) {
0352         /*
0353          * FIXME: We don't know why, but we need to configure the
0354          * lgdt3303 with the register settings below on resume
0355          */
0356         int i;
0357         u8 buf;
0358         static const u8 bufs[] = {
0359             0x0e, 0x2, 0x00, 0x7f,
0360             0x0e, 0x2, 0x02, 0xfe,
0361             0x0e, 0x2, 0x02, 0x01,
0362             0x0e, 0x2, 0x00, 0x03,
0363             0x0e, 0x2, 0x0d, 0x40,
0364             0x0e, 0x2, 0x0e, 0x87,
0365             0x0e, 0x2, 0x0f, 0x8e,
0366             0x0e, 0x2, 0x10, 0x01,
0367             0x0e, 0x2, 0x14, 0xd7,
0368             0x0e, 0x2, 0x47, 0x88,
0369         };
0370         msleep(20);
0371         for (i = 0; i < ARRAY_SIZE(bufs); i += 4 / sizeof(u8)) {
0372             ret = cxusb_ctrl_msg(d, CMD_I2C_WRITE,
0373                          bufs + i, 4, &buf, 1);
0374             if (ret)
0375                 break;
0376             if (buf != 0x8)
0377                 return -EREMOTEIO;
0378         }
0379     }
0380     return ret;
0381 }
0382 
0383 static int cxusb_bluebird_power_ctrl(struct dvb_usb_device *d, int onoff)
0384 {
0385     u8 b = 0;
0386 
0387     if (onoff)
0388         return cxusb_ctrl_msg(d, CMD_POWER_ON, &b, 1, NULL, 0);
0389     else
0390         return 0;
0391 }
0392 
0393 static int cxusb_nano2_power_ctrl(struct dvb_usb_device *d, int onoff)
0394 {
0395     int rc = 0;
0396 
0397     rc = cxusb_power_ctrl(d, onoff);
0398     if (!onoff)
0399         cxusb_nano2_led(d, 0);
0400 
0401     return rc;
0402 }
0403 
0404 static int cxusb_d680_dmb_power_ctrl(struct dvb_usb_device *d, int onoff)
0405 {
0406     int ret;
0407     u8  b;
0408 
0409     ret = cxusb_power_ctrl(d, onoff);
0410     if (!onoff)
0411         return ret;
0412 
0413     msleep(128);
0414     cxusb_ctrl_msg(d, CMD_DIGITAL, NULL, 0, &b, 1);
0415     msleep(100);
0416     return ret;
0417 }
0418 
0419 static int cxusb_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
0420 {
0421     struct dvb_usb_device *dvbdev = adap->dev;
0422     bool is_medion = dvbdev->props.devices[0].warm_ids[0] ==
0423         &cxusb_table[MEDION_MD95700];
0424     u8 buf[2] = { 0x03, 0x00 };
0425 
0426     if (is_medion && onoff) {
0427         int ret;
0428 
0429         ret = cxusb_medion_get(dvbdev, CXUSB_OPEN_DIGITAL);
0430         if (ret != 0)
0431             return ret;
0432     }
0433 
0434     if (onoff)
0435         cxusb_ctrl_msg(dvbdev, CMD_STREAMING_ON, buf, 2, NULL, 0);
0436     else
0437         cxusb_ctrl_msg(dvbdev, CMD_STREAMING_OFF, NULL, 0, NULL, 0);
0438 
0439     if (is_medion && !onoff)
0440         cxusb_medion_put(dvbdev);
0441 
0442     return 0;
0443 }
0444 
0445 static int cxusb_aver_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
0446 {
0447     if (onoff)
0448         cxusb_ctrl_msg(adap->dev, CMD_AVER_STREAM_ON, NULL, 0, NULL, 0);
0449     else
0450         cxusb_ctrl_msg(adap->dev, CMD_AVER_STREAM_OFF,
0451                    NULL, 0, NULL, 0);
0452     return 0;
0453 }
0454 
0455 static void cxusb_d680_dmb_drain_message(struct dvb_usb_device *d)
0456 {
0457     int       ep = d->props.generic_bulk_ctrl_endpoint;
0458     const int timeout = 100;
0459     const int junk_len = 32;
0460     u8        *junk;
0461     int       rd_count;
0462 
0463     /* Discard remaining data in video pipe */
0464     junk = kmalloc(junk_len, GFP_KERNEL);
0465     if (!junk)
0466         return;
0467     while (1) {
0468         if (usb_bulk_msg(d->udev,
0469                  usb_rcvbulkpipe(d->udev, ep),
0470                  junk, junk_len, &rd_count, timeout) < 0)
0471             break;
0472         if (!rd_count)
0473             break;
0474     }
0475     kfree(junk);
0476 }
0477 
0478 static void cxusb_d680_dmb_drain_video(struct dvb_usb_device *d)
0479 {
0480     struct usb_data_stream_properties *p = &d->props.adapter[0].fe[0].stream;
0481     const int timeout = 100;
0482     const int junk_len = p->u.bulk.buffersize;
0483     u8        *junk;
0484     int       rd_count;
0485 
0486     /* Discard remaining data in video pipe */
0487     junk = kmalloc(junk_len, GFP_KERNEL);
0488     if (!junk)
0489         return;
0490     while (1) {
0491         if (usb_bulk_msg(d->udev,
0492                  usb_rcvbulkpipe(d->udev, p->endpoint),
0493                  junk, junk_len, &rd_count, timeout) < 0)
0494             break;
0495         if (!rd_count)
0496             break;
0497     }
0498     kfree(junk);
0499 }
0500 
0501 static int cxusb_d680_dmb_streaming_ctrl(struct dvb_usb_adapter *adap,
0502                      int onoff)
0503 {
0504     if (onoff) {
0505         u8 buf[2] = { 0x03, 0x00 };
0506 
0507         cxusb_d680_dmb_drain_video(adap->dev);
0508         return cxusb_ctrl_msg(adap->dev, CMD_STREAMING_ON,
0509                       buf, sizeof(buf), NULL, 0);
0510     } else {
0511         int ret = cxusb_ctrl_msg(adap->dev,
0512                      CMD_STREAMING_OFF, NULL, 0, NULL, 0);
0513         return ret;
0514     }
0515 }
0516 
0517 static int cxusb_rc_query(struct dvb_usb_device *d)
0518 {
0519     u8 ircode[4];
0520 
0521     if (cxusb_ctrl_msg(d, CMD_GET_IR_CODE, NULL, 0, ircode, 4) < 0)
0522         return 0;
0523 
0524     if (ircode[2] || ircode[3])
0525         rc_keydown(d->rc_dev, RC_PROTO_NEC,
0526                RC_SCANCODE_NEC(~ircode[2] & 0xff, ircode[3]), 0);
0527     return 0;
0528 }
0529 
0530 static int cxusb_bluebird2_rc_query(struct dvb_usb_device *d)
0531 {
0532     u8 ircode[4];
0533     struct i2c_msg msg = {
0534         .addr = 0x6b,
0535         .flags = I2C_M_RD,
0536         .buf = ircode,
0537         .len = 4
0538     };
0539 
0540     if (cxusb_i2c_xfer(&d->i2c_adap, &msg, 1) != 1)
0541         return 0;
0542 
0543     if (ircode[1] || ircode[2])
0544         rc_keydown(d->rc_dev, RC_PROTO_NEC,
0545                RC_SCANCODE_NEC(~ircode[1] & 0xff, ircode[2]), 0);
0546     return 0;
0547 }
0548 
0549 static int cxusb_d680_dmb_rc_query(struct dvb_usb_device *d)
0550 {
0551     u8 ircode[2];
0552 
0553     if (cxusb_ctrl_msg(d, 0x10, NULL, 0, ircode, 2) < 0)
0554         return 0;
0555 
0556     if (ircode[0] || ircode[1])
0557         rc_keydown(d->rc_dev, RC_PROTO_UNKNOWN,
0558                RC_SCANCODE_RC5(ircode[0], ircode[1]), 0);
0559     return 0;
0560 }
0561 
0562 static int cxusb_dee1601_demod_init(struct dvb_frontend *fe)
0563 {
0564     static u8 clock_config[]   = { CLOCK_CTL,  0x38, 0x28 };
0565     static u8 reset[]          = { RESET,      0x80 };
0566     static u8 adc_ctl_1_cfg[]  = { ADC_CTL_1,  0x40 };
0567     static u8 agc_cfg[]        = { AGC_TARGET, 0x28, 0x20 };
0568     static u8 gpp_ctl_cfg[]    = { GPP_CTL,    0x33 };
0569     static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
0570 
0571     mt352_write(fe, clock_config,   sizeof(clock_config));
0572     udelay(200);
0573     mt352_write(fe, reset,          sizeof(reset));
0574     mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
0575 
0576     mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
0577     mt352_write(fe, gpp_ctl_cfg,    sizeof(gpp_ctl_cfg));
0578     mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
0579 
0580     return 0;
0581 }
0582 
0583 static int cxusb_mt352_demod_init(struct dvb_frontend *fe)
0584 {
0585     /* used in both lgz201 and th7579 */
0586     static u8 clock_config[]   = { CLOCK_CTL,  0x38, 0x29 };
0587     static u8 reset[]          = { RESET,      0x80 };
0588     static u8 adc_ctl_1_cfg[]  = { ADC_CTL_1,  0x40 };
0589     static u8 agc_cfg[]        = { AGC_TARGET, 0x24, 0x20 };
0590     static u8 gpp_ctl_cfg[]    = { GPP_CTL,    0x33 };
0591     static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
0592 
0593     mt352_write(fe, clock_config,   sizeof(clock_config));
0594     udelay(200);
0595     mt352_write(fe, reset,          sizeof(reset));
0596     mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
0597 
0598     mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
0599     mt352_write(fe, gpp_ctl_cfg,    sizeof(gpp_ctl_cfg));
0600     mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
0601     return 0;
0602 }
0603 
0604 static struct cx22702_config cxusb_cx22702_config = {
0605     .demod_address = 0x63,
0606     .output_mode = CX22702_PARALLEL_OUTPUT,
0607 };
0608 
0609 static struct lgdt330x_config cxusb_lgdt3303_config = {
0610     .demod_chip    = LGDT3303,
0611 };
0612 
0613 static struct lgdt330x_config cxusb_aver_lgdt3303_config = {
0614     .demod_chip          = LGDT3303,
0615     .clock_polarity_flip = 2,
0616 };
0617 
0618 static struct mt352_config cxusb_dee1601_config = {
0619     .demod_address = 0x0f,
0620     .demod_init    = cxusb_dee1601_demod_init,
0621 };
0622 
0623 static struct zl10353_config cxusb_zl10353_dee1601_config = {
0624     .demod_address = 0x0f,
0625     .parallel_ts = 1,
0626 };
0627 
0628 static struct mt352_config cxusb_mt352_config = {
0629     /* used in both lgz201 and th7579 */
0630     .demod_address = 0x0f,
0631     .demod_init    = cxusb_mt352_demod_init,
0632 };
0633 
0634 static struct zl10353_config cxusb_zl10353_xc3028_config = {
0635     .demod_address = 0x0f,
0636     .if2 = 45600,
0637     .no_tuner = 1,
0638     .parallel_ts = 1,
0639 };
0640 
0641 static struct zl10353_config cxusb_zl10353_xc3028_config_no_i2c_gate = {
0642     .demod_address = 0x0f,
0643     .if2 = 45600,
0644     .no_tuner = 1,
0645     .parallel_ts = 1,
0646     .disable_i2c_gate_ctrl = 1,
0647 };
0648 
0649 static struct mt352_config cxusb_mt352_xc3028_config = {
0650     .demod_address = 0x0f,
0651     .if2 = 4560,
0652     .no_tuner = 1,
0653     .demod_init = cxusb_mt352_demod_init,
0654 };
0655 
0656 /* FIXME: needs tweaking */
0657 static struct mxl5005s_config aver_a868r_tuner = {
0658     .i2c_address     = 0x63,
0659     .if_freq         = 6000000UL,
0660     .xtal_freq       = CRYSTAL_FREQ_16000000HZ,
0661     .agc_mode        = MXL_SINGLE_AGC,
0662     .tracking_filter = MXL_TF_C,
0663     .rssi_enable     = MXL_RSSI_ENABLE,
0664     .cap_select      = MXL_CAP_SEL_ENABLE,
0665     .div_out         = MXL_DIV_OUT_4,
0666     .clock_out       = MXL_CLOCK_OUT_DISABLE,
0667     .output_load     = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
0668     .top         = MXL5005S_TOP_25P2,
0669     .mod_mode        = MXL_DIGITAL_MODE,
0670     .if_mode         = MXL_ZERO_IF,
0671     .AgcMasterByte   = 0x00,
0672 };
0673 
0674 /* FIXME: needs tweaking */
0675 static struct mxl5005s_config d680_dmb_tuner = {
0676     .i2c_address     = 0x63,
0677     .if_freq         = 36125000UL,
0678     .xtal_freq       = CRYSTAL_FREQ_16000000HZ,
0679     .agc_mode        = MXL_SINGLE_AGC,
0680     .tracking_filter = MXL_TF_C,
0681     .rssi_enable     = MXL_RSSI_ENABLE,
0682     .cap_select      = MXL_CAP_SEL_ENABLE,
0683     .div_out         = MXL_DIV_OUT_4,
0684     .clock_out       = MXL_CLOCK_OUT_DISABLE,
0685     .output_load     = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
0686     .top         = MXL5005S_TOP_25P2,
0687     .mod_mode        = MXL_DIGITAL_MODE,
0688     .if_mode         = MXL_ZERO_IF,
0689     .AgcMasterByte   = 0x00,
0690 };
0691 
0692 static struct max2165_config mygica_d689_max2165_cfg = {
0693     .i2c_address = 0x60,
0694     .osc_clk = 20
0695 };
0696 
0697 /* Callbacks for DVB USB */
0698 static int cxusb_fmd1216me_tuner_attach(struct dvb_usb_adapter *adap)
0699 {
0700     struct dvb_usb_device *dvbdev = adap->dev;
0701     bool is_medion = dvbdev->props.devices[0].warm_ids[0] ==
0702         &cxusb_table[MEDION_MD95700];
0703 
0704     dvb_attach(simple_tuner_attach, adap->fe_adap[0].fe,
0705            &dvbdev->i2c_adap, 0x61,
0706            TUNER_PHILIPS_FMD1216ME_MK3);
0707 
0708     if (is_medion && adap->fe_adap[0].fe)
0709         /*
0710          * make sure that DVB core won't put to sleep (reset, really)
0711          * tuner when we might be open in analog mode
0712          */
0713         adap->fe_adap[0].fe->ops.tuner_ops.sleep = NULL;
0714 
0715     return 0;
0716 }
0717 
0718 static int cxusb_dee1601_tuner_attach(struct dvb_usb_adapter *adap)
0719 {
0720     dvb_attach(dvb_pll_attach, adap->fe_adap[0].fe, 0x61,
0721            NULL, DVB_PLL_THOMSON_DTT7579);
0722     return 0;
0723 }
0724 
0725 static int cxusb_lgz201_tuner_attach(struct dvb_usb_adapter *adap)
0726 {
0727     dvb_attach(dvb_pll_attach, adap->fe_adap[0].fe, 0x61,
0728            NULL, DVB_PLL_LG_Z201);
0729     return 0;
0730 }
0731 
0732 static int cxusb_dtt7579_tuner_attach(struct dvb_usb_adapter *adap)
0733 {
0734     dvb_attach(dvb_pll_attach, adap->fe_adap[0].fe, 0x60,
0735            NULL, DVB_PLL_THOMSON_DTT7579);
0736     return 0;
0737 }
0738 
0739 static int cxusb_lgh064f_tuner_attach(struct dvb_usb_adapter *adap)
0740 {
0741     dvb_attach(simple_tuner_attach, adap->fe_adap[0].fe,
0742            &adap->dev->i2c_adap, 0x61, TUNER_LG_TDVS_H06XF);
0743     return 0;
0744 }
0745 
0746 static int dvico_bluebird_xc2028_callback(void *ptr, int component,
0747                       int command, int arg)
0748 {
0749     struct dvb_usb_adapter *adap = ptr;
0750     struct dvb_usb_device *d = adap->dev;
0751 
0752     switch (command) {
0753     case XC2028_TUNER_RESET:
0754         dev_info(&d->udev->dev, "XC2028_TUNER_RESET %d\n", arg);
0755         cxusb_bluebird_gpio_pulse(d, 0x01, 1);
0756         break;
0757     case XC2028_RESET_CLK:
0758         dev_info(&d->udev->dev, "XC2028_RESET_CLK %d\n", arg);
0759         break;
0760     case XC2028_I2C_FLUSH:
0761         break;
0762     default:
0763         dev_info(&d->udev->dev, "unknown command %d, arg %d\n",
0764              command, arg);
0765         return -EINVAL;
0766     }
0767 
0768     return 0;
0769 }
0770 
0771 static int cxusb_dvico_xc3028_tuner_attach(struct dvb_usb_adapter *adap)
0772 {
0773     struct dvb_frontend  *fe;
0774     struct xc2028_config      cfg = {
0775         .i2c_adap  = &adap->dev->i2c_adap,
0776         .i2c_addr  = 0x61,
0777     };
0778     static struct xc2028_ctrl ctl = {
0779         .fname       = XC2028_DEFAULT_FIRMWARE,
0780         .max_len     = 64,
0781         .demod       = XC3028_FE_ZARLINK456,
0782     };
0783 
0784     /* FIXME: generalize & move to common area */
0785     adap->fe_adap[0].fe->callback = dvico_bluebird_xc2028_callback;
0786 
0787     fe = dvb_attach(xc2028_attach, adap->fe_adap[0].fe, &cfg);
0788     if (!fe || !fe->ops.tuner_ops.set_config)
0789         return -EIO;
0790 
0791     fe->ops.tuner_ops.set_config(fe, &ctl);
0792 
0793     return 0;
0794 }
0795 
0796 static int cxusb_mxl5003s_tuner_attach(struct dvb_usb_adapter *adap)
0797 {
0798     dvb_attach(mxl5005s_attach, adap->fe_adap[0].fe,
0799            &adap->dev->i2c_adap, &aver_a868r_tuner);
0800     return 0;
0801 }
0802 
0803 static int cxusb_d680_dmb_tuner_attach(struct dvb_usb_adapter *adap)
0804 {
0805     struct dvb_frontend *fe;
0806 
0807     fe = dvb_attach(mxl5005s_attach, adap->fe_adap[0].fe,
0808             &adap->dev->i2c_adap, &d680_dmb_tuner);
0809     return (!fe) ? -EIO : 0;
0810 }
0811 
0812 static int cxusb_mygica_d689_tuner_attach(struct dvb_usb_adapter *adap)
0813 {
0814     struct dvb_frontend *fe;
0815 
0816     fe = dvb_attach(max2165_attach, adap->fe_adap[0].fe,
0817             &adap->dev->i2c_adap, &mygica_d689_max2165_cfg);
0818     return (!fe) ? -EIO : 0;
0819 }
0820 
0821 static int cxusb_medion_fe_ts_bus_ctrl(struct dvb_frontend *fe, int acquire)
0822 {
0823     struct dvb_usb_adapter *adap = fe->dvb->priv;
0824     struct dvb_usb_device *dvbdev = adap->dev;
0825 
0826     if (acquire)
0827         return cxusb_medion_get(dvbdev, CXUSB_OPEN_DIGITAL);
0828 
0829     cxusb_medion_put(dvbdev);
0830 
0831     return 0;
0832 }
0833 
0834 static int cxusb_medion_set_mode(struct dvb_usb_device *dvbdev, bool digital)
0835 {
0836     struct cxusb_state *st = dvbdev->priv;
0837     int ret;
0838     u8 b;
0839     unsigned int i;
0840 
0841     /*
0842      * switching mode while doing an I2C transaction often causes
0843      * the device to crash
0844      */
0845     mutex_lock(&dvbdev->i2c_mutex);
0846 
0847     if (digital) {
0848         ret = usb_set_interface(dvbdev->udev, 0, 6);
0849         if (ret != 0) {
0850             dev_err(&dvbdev->udev->dev,
0851                 "digital interface selection failed (%d)\n",
0852                 ret);
0853             goto ret_unlock;
0854         }
0855     } else {
0856         ret = usb_set_interface(dvbdev->udev, 0, 1);
0857         if (ret != 0) {
0858             dev_err(&dvbdev->udev->dev,
0859                 "analog interface selection failed (%d)\n",
0860                 ret);
0861             goto ret_unlock;
0862         }
0863     }
0864 
0865     /* pipes need to be cleared after setting interface */
0866     ret = usb_clear_halt(dvbdev->udev, usb_rcvbulkpipe(dvbdev->udev, 1));
0867     if (ret != 0)
0868         dev_warn(&dvbdev->udev->dev,
0869              "clear halt on IN pipe failed (%d)\n",
0870              ret);
0871 
0872     ret = usb_clear_halt(dvbdev->udev, usb_sndbulkpipe(dvbdev->udev, 1));
0873     if (ret != 0)
0874         dev_warn(&dvbdev->udev->dev,
0875              "clear halt on OUT pipe failed (%d)\n",
0876              ret);
0877 
0878     ret = cxusb_ctrl_msg(dvbdev, digital ? CMD_DIGITAL : CMD_ANALOG,
0879                  NULL, 0, &b, 1);
0880     if (ret != 0) {
0881         dev_err(&dvbdev->udev->dev, "mode switch failed (%d)\n",
0882             ret);
0883         goto ret_unlock;
0884     }
0885 
0886     /* mode switch seems to reset GPIO states */
0887     for (i = 0; i < ARRAY_SIZE(st->gpio_write_refresh); i++)
0888         st->gpio_write_refresh[i] = true;
0889 
0890 ret_unlock:
0891     mutex_unlock(&dvbdev->i2c_mutex);
0892 
0893     return ret;
0894 }
0895 
0896 static int cxusb_cx22702_frontend_attach(struct dvb_usb_adapter *adap)
0897 {
0898     struct dvb_usb_device *dvbdev = adap->dev;
0899     bool is_medion = dvbdev->props.devices[0].warm_ids[0] ==
0900         &cxusb_table[MEDION_MD95700];
0901 
0902     if (is_medion) {
0903         int ret;
0904 
0905         ret = cxusb_medion_set_mode(dvbdev, true);
0906         if (ret)
0907             return ret;
0908     }
0909 
0910     adap->fe_adap[0].fe = dvb_attach(cx22702_attach, &cxusb_cx22702_config,
0911                      &dvbdev->i2c_adap);
0912     if (!adap->fe_adap[0].fe)
0913         return -EIO;
0914 
0915     if (is_medion)
0916         adap->fe_adap[0].fe->ops.ts_bus_ctrl =
0917             cxusb_medion_fe_ts_bus_ctrl;
0918 
0919     return 0;
0920 }
0921 
0922 static int cxusb_lgdt3303_frontend_attach(struct dvb_usb_adapter *adap)
0923 {
0924     if (usb_set_interface(adap->dev->udev, 0, 7) < 0)
0925         err("set interface failed");
0926 
0927     cxusb_ctrl_msg(adap->dev, CMD_DIGITAL, NULL, 0, NULL, 0);
0928 
0929     adap->fe_adap[0].fe = dvb_attach(lgdt330x_attach,
0930                      &cxusb_lgdt3303_config,
0931                      0x0e,
0932                      &adap->dev->i2c_adap);
0933     if (adap->fe_adap[0].fe)
0934         return 0;
0935 
0936     return -EIO;
0937 }
0938 
0939 static int cxusb_aver_lgdt3303_frontend_attach(struct dvb_usb_adapter *adap)
0940 {
0941     adap->fe_adap[0].fe = dvb_attach(lgdt330x_attach,
0942                      &cxusb_aver_lgdt3303_config,
0943                      0x0e,
0944                      &adap->dev->i2c_adap);
0945     if (adap->fe_adap[0].fe)
0946         return 0;
0947 
0948     return -EIO;
0949 }
0950 
0951 static int cxusb_mt352_frontend_attach(struct dvb_usb_adapter *adap)
0952 {
0953     /* used in both lgz201 and th7579 */
0954     if (usb_set_interface(adap->dev->udev, 0, 0) < 0)
0955         err("set interface failed");
0956 
0957     cxusb_ctrl_msg(adap->dev, CMD_DIGITAL, NULL, 0, NULL, 0);
0958 
0959     adap->fe_adap[0].fe = dvb_attach(mt352_attach, &cxusb_mt352_config,
0960                      &adap->dev->i2c_adap);
0961     if (adap->fe_adap[0].fe)
0962         return 0;
0963 
0964     return -EIO;
0965 }
0966 
0967 static int cxusb_dee1601_frontend_attach(struct dvb_usb_adapter *adap)
0968 {
0969     if (usb_set_interface(adap->dev->udev, 0, 0) < 0)
0970         err("set interface failed");
0971 
0972     cxusb_ctrl_msg(adap->dev, CMD_DIGITAL, NULL, 0, NULL, 0);
0973 
0974     adap->fe_adap[0].fe = dvb_attach(mt352_attach, &cxusb_dee1601_config,
0975                      &adap->dev->i2c_adap);
0976     if (adap->fe_adap[0].fe)
0977         return 0;
0978 
0979     adap->fe_adap[0].fe = dvb_attach(zl10353_attach,
0980                      &cxusb_zl10353_dee1601_config,
0981                      &adap->dev->i2c_adap);
0982     if (adap->fe_adap[0].fe)
0983         return 0;
0984 
0985     return -EIO;
0986 }
0987 
0988 static int cxusb_dualdig4_frontend_attach(struct dvb_usb_adapter *adap)
0989 {
0990     u8 ircode[4];
0991     int i;
0992     struct i2c_msg msg = {
0993         .addr = 0x6b,
0994         .flags = I2C_M_RD,
0995         .buf = ircode,
0996         .len = 4
0997     };
0998 
0999     if (usb_set_interface(adap->dev->udev, 0, 1) < 0)
1000         err("set interface failed");
1001 
1002     cxusb_ctrl_msg(adap->dev, CMD_DIGITAL, NULL, 0, NULL, 0);
1003 
1004     /* reset the tuner and demodulator */
1005     cxusb_bluebird_gpio_rw(adap->dev, 0x04, 0);
1006     cxusb_bluebird_gpio_pulse(adap->dev, 0x01, 1);
1007     cxusb_bluebird_gpio_pulse(adap->dev, 0x02, 1);
1008 
1009     adap->fe_adap[0].fe =
1010         dvb_attach(zl10353_attach,
1011                &cxusb_zl10353_xc3028_config_no_i2c_gate,
1012                &adap->dev->i2c_adap);
1013     if (!adap->fe_adap[0].fe)
1014         return -EIO;
1015 
1016     /* try to determine if there is no IR decoder on the I2C bus */
1017     for (i = 0; adap->dev->props.rc.core.rc_codes && i < 5; i++) {
1018         msleep(20);
1019         if (cxusb_i2c_xfer(&adap->dev->i2c_adap, &msg, 1) != 1)
1020             goto no_IR;
1021         if (ircode[0] == 0 && ircode[1] == 0)
1022             continue;
1023         if (ircode[2] + ircode[3] != 0xff) {
1024 no_IR:
1025             adap->dev->props.rc.core.rc_codes = NULL;
1026             info("No IR receiver detected on this device.");
1027             break;
1028         }
1029     }
1030 
1031     return 0;
1032 }
1033 
1034 static struct dibx000_agc_config dib7070_agc_config = {
1035     .band_caps = BAND_UHF | BAND_VHF | BAND_LBAND | BAND_SBAND,
1036 
1037     /*
1038      * P_agc_use_sd_mod1=0, P_agc_use_sd_mod2=0, P_agc_freq_pwm_div=5,
1039      * P_agc_inv_pwm1=0, P_agc_inv_pwm2=0, P_agc_inh_dc_rv_est=0,
1040      * P_agc_time_est=3, P_agc_freeze=0, P_agc_nb_est=5, P_agc_write=0
1041      */
1042     .setup = (0 << 15) | (0 << 14) | (5 << 11) | (0 << 10) | (0 << 9) |
1043          (0 << 8) | (3 << 5) | (0 << 4) | (5 << 1) | (0 << 0),
1044     .inv_gain = 600,
1045     .time_stabiliz = 10,
1046     .alpha_level = 0,
1047     .thlock = 118,
1048     .wbd_inv = 0,
1049     .wbd_ref = 3530,
1050     .wbd_sel = 1,
1051     .wbd_alpha = 5,
1052     .agc1_max = 65535,
1053     .agc1_min = 0,
1054     .agc2_max = 65535,
1055     .agc2_min = 0,
1056     .agc1_pt1 = 0,
1057     .agc1_pt2 = 40,
1058     .agc1_pt3 = 183,
1059     .agc1_slope1 = 206,
1060     .agc1_slope2 = 255,
1061     .agc2_pt1 = 72,
1062     .agc2_pt2 = 152,
1063     .agc2_slope1 = 88,
1064     .agc2_slope2 = 90,
1065     .alpha_mant = 17,
1066     .alpha_exp = 27,
1067     .beta_mant = 23,
1068     .beta_exp = 51,
1069     .perform_agc_softsplit = 0,
1070 };
1071 
1072 static struct dibx000_bandwidth_config dib7070_bw_config_12_mhz = {
1073     .internal = 60000,
1074     .sampling = 15000,
1075     .pll_prediv = 1,
1076     .pll_ratio = 20,
1077     .pll_range = 3,
1078     .pll_reset = 1,
1079     .pll_bypass = 0,
1080     .enable_refdiv = 0,
1081     .bypclk_div = 0,
1082     .IO_CLK_en_core = 1,
1083     .ADClkSrc = 1,
1084     .modulo = 2,
1085     /* refsel, sel, freq_15k */
1086     .sad_cfg = (3 << 14) | (1 << 12) | (524 << 0),
1087     .ifreq = (0 << 25) | 0,
1088     .timf = 20452225,
1089     .xtal_hz = 12000000,
1090 };
1091 
1092 static struct dib7000p_config cxusb_dualdig4_rev2_config = {
1093     .output_mode = OUTMODE_MPEG2_PAR_GATED_CLK,
1094     .output_mpeg2_in_188_bytes = 1,
1095 
1096     .agc_config_count = 1,
1097     .agc = &dib7070_agc_config,
1098     .bw  = &dib7070_bw_config_12_mhz,
1099     .tuner_is_baseband = 1,
1100     .spur_protect = 1,
1101 
1102     .gpio_dir = 0xfcef,
1103     .gpio_val = 0x0110,
1104 
1105     .gpio_pwm_pos = DIB7000P_GPIO_DEFAULT_PWM_POS,
1106 
1107     .hostbus_diversity = 1,
1108 };
1109 
1110 struct dib0700_adapter_state {
1111     int (*set_param_save)(struct dvb_frontend *fe);
1112     struct dib7000p_ops dib7000p_ops;
1113 };
1114 
1115 static int cxusb_dualdig4_rev2_frontend_attach(struct dvb_usb_adapter *adap)
1116 {
1117     struct dib0700_adapter_state *state = adap->priv;
1118 
1119     if (usb_set_interface(adap->dev->udev, 0, 1) < 0)
1120         err("set interface failed");
1121 
1122     cxusb_ctrl_msg(adap->dev, CMD_DIGITAL, NULL, 0, NULL, 0);
1123 
1124     cxusb_bluebird_gpio_pulse(adap->dev, 0x02, 1);
1125 
1126     if (!dvb_attach(dib7000p_attach, &state->dib7000p_ops))
1127         return -ENODEV;
1128 
1129     if (state->dib7000p_ops.i2c_enumeration(&adap->dev->i2c_adap, 1, 18,
1130                         &cxusb_dualdig4_rev2_config) < 0) {
1131         pr_warn("Unable to enumerate dib7000p\n");
1132         return -ENODEV;
1133     }
1134 
1135     adap->fe_adap[0].fe = state->dib7000p_ops.init(&adap->dev->i2c_adap,
1136                                0x80,
1137                                &cxusb_dualdig4_rev2_config);
1138     if (!adap->fe_adap[0].fe)
1139         return -EIO;
1140 
1141     return 0;
1142 }
1143 
1144 static int dib7070_tuner_reset(struct dvb_frontend *fe, int onoff)
1145 {
1146     struct dvb_usb_adapter *adap = fe->dvb->priv;
1147     struct dib0700_adapter_state *state = adap->priv;
1148 
1149     return state->dib7000p_ops.set_gpio(fe, 8, 0, !onoff);
1150 }
1151 
1152 static int dib7070_tuner_sleep(struct dvb_frontend *fe, int onoff)
1153 {
1154     return 0;
1155 }
1156 
1157 static struct dib0070_config dib7070p_dib0070_config = {
1158     .i2c_address = DEFAULT_DIB0070_I2C_ADDRESS,
1159     .reset = dib7070_tuner_reset,
1160     .sleep = dib7070_tuner_sleep,
1161     .clock_khz = 12000,
1162 };
1163 
1164 static int dib7070_set_param_override(struct dvb_frontend *fe)
1165 {
1166     struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1167     struct dvb_usb_adapter *adap = fe->dvb->priv;
1168     struct dib0700_adapter_state *state = adap->priv;
1169 
1170     u16 offset;
1171     u8 band = BAND_OF_FREQUENCY(p->frequency / 1000);
1172 
1173     switch (band) {
1174     case BAND_VHF:
1175         offset = 950;
1176         break;
1177     default:
1178     case BAND_UHF:
1179         offset = 550;
1180         break;
1181     }
1182 
1183     state->dib7000p_ops.set_wbd_ref(fe, offset + dib0070_wbd_offset(fe));
1184 
1185     return state->set_param_save(fe);
1186 }
1187 
1188 static int cxusb_dualdig4_rev2_tuner_attach(struct dvb_usb_adapter *adap)
1189 {
1190     struct dib0700_adapter_state *st = adap->priv;
1191     struct i2c_adapter *tun_i2c;
1192 
1193     /*
1194      * No need to call dvb7000p_attach here, as it was called
1195      * already, as frontend_attach method is called first, and
1196      * tuner_attach is only called on success.
1197      */
1198     tun_i2c = st->dib7000p_ops.get_i2c_master(adap->fe_adap[0].fe,
1199                     DIBX000_I2C_INTERFACE_TUNER, 1);
1200 
1201     if (dvb_attach(dib0070_attach, adap->fe_adap[0].fe, tun_i2c,
1202                &dib7070p_dib0070_config) == NULL)
1203         return -ENODEV;
1204 
1205     st->set_param_save = adap->fe_adap[0].fe->ops.tuner_ops.set_params;
1206     adap->fe_adap[0].fe->ops.tuner_ops.set_params = dib7070_set_param_override;
1207     return 0;
1208 }
1209 
1210 static int cxusb_nano2_frontend_attach(struct dvb_usb_adapter *adap)
1211 {
1212     if (usb_set_interface(adap->dev->udev, 0, 1) < 0)
1213         err("set interface failed");
1214 
1215     cxusb_ctrl_msg(adap->dev, CMD_DIGITAL, NULL, 0, NULL, 0);
1216 
1217     /* reset the tuner and demodulator */
1218     cxusb_bluebird_gpio_rw(adap->dev, 0x04, 0);
1219     cxusb_bluebird_gpio_pulse(adap->dev, 0x01, 1);
1220     cxusb_bluebird_gpio_pulse(adap->dev, 0x02, 1);
1221 
1222     adap->fe_adap[0].fe = dvb_attach(zl10353_attach,
1223                      &cxusb_zl10353_xc3028_config,
1224                      &adap->dev->i2c_adap);
1225     if (adap->fe_adap[0].fe)
1226         return 0;
1227 
1228     adap->fe_adap[0].fe = dvb_attach(mt352_attach,
1229                      &cxusb_mt352_xc3028_config,
1230                      &adap->dev->i2c_adap);
1231     if (adap->fe_adap[0].fe)
1232         return 0;
1233 
1234     return -EIO;
1235 }
1236 
1237 static struct lgs8gxx_config d680_lgs8gl5_cfg = {
1238     .prod = LGS8GXX_PROD_LGS8GL5,
1239     .demod_address = 0x19,
1240     .serial_ts = 0,
1241     .ts_clk_pol = 0,
1242     .ts_clk_gated = 1,
1243     .if_clk_freq = 30400, /* 30.4 MHz */
1244     .if_freq = 5725, /* 5.725 MHz */
1245     .if_neg_center = 0,
1246     .ext_adc = 0,
1247     .adc_signed = 0,
1248     .if_neg_edge = 0,
1249 };
1250 
1251 static int cxusb_d680_dmb_frontend_attach(struct dvb_usb_adapter *adap)
1252 {
1253     struct dvb_usb_device *d = adap->dev;
1254     int n;
1255 
1256     /* Select required USB configuration */
1257     if (usb_set_interface(d->udev, 0, 0) < 0)
1258         err("set interface failed");
1259 
1260     /* Unblock all USB pipes */
1261     usb_clear_halt(d->udev,
1262                usb_sndbulkpipe(d->udev,
1263                        d->props.generic_bulk_ctrl_endpoint));
1264     usb_clear_halt(d->udev,
1265                usb_rcvbulkpipe(d->udev,
1266                        d->props.generic_bulk_ctrl_endpoint));
1267     usb_clear_halt(d->udev,
1268                usb_rcvbulkpipe(d->udev,
1269                        d->props.adapter[0].fe[0].stream.endpoint));
1270 
1271     /* Drain USB pipes to avoid hang after reboot */
1272     for (n = 0;  n < 5;  n++) {
1273         cxusb_d680_dmb_drain_message(d);
1274         cxusb_d680_dmb_drain_video(d);
1275         msleep(200);
1276     }
1277 
1278     /* Reset the tuner */
1279     if (cxusb_d680_dmb_gpio_tuner(d, 0x07, 0) < 0) {
1280         err("clear tuner gpio failed");
1281         return -EIO;
1282     }
1283     msleep(100);
1284     if (cxusb_d680_dmb_gpio_tuner(d, 0x07, 1) < 0) {
1285         err("set tuner gpio failed");
1286         return -EIO;
1287     }
1288     msleep(100);
1289 
1290     /* Attach frontend */
1291     adap->fe_adap[0].fe = dvb_attach(lgs8gxx_attach,
1292                      &d680_lgs8gl5_cfg, &d->i2c_adap);
1293     if (!adap->fe_adap[0].fe)
1294         return -EIO;
1295 
1296     return 0;
1297 }
1298 
1299 static struct atbm8830_config mygica_d689_atbm8830_cfg = {
1300     .prod = ATBM8830_PROD_8830,
1301     .demod_address = 0x40,
1302     .serial_ts = 0,
1303     .ts_sampling_edge = 1,
1304     .ts_clk_gated = 0,
1305     .osc_clk_freq = 30400, /* in kHz */
1306     .if_freq = 0, /* zero IF */
1307     .zif_swap_iq = 1,
1308     .agc_min = 0x2E,
1309     .agc_max = 0x90,
1310     .agc_hold_loop = 0,
1311 };
1312 
1313 static int cxusb_mygica_d689_frontend_attach(struct dvb_usb_adapter *adap)
1314 {
1315     struct dvb_usb_device *d = adap->dev;
1316 
1317     /* Select required USB configuration */
1318     if (usb_set_interface(d->udev, 0, 0) < 0)
1319         err("set interface failed");
1320 
1321     /* Unblock all USB pipes */
1322     usb_clear_halt(d->udev,
1323                usb_sndbulkpipe(d->udev,
1324                        d->props.generic_bulk_ctrl_endpoint));
1325     usb_clear_halt(d->udev,
1326                usb_rcvbulkpipe(d->udev,
1327                        d->props.generic_bulk_ctrl_endpoint));
1328     usb_clear_halt(d->udev,
1329                usb_rcvbulkpipe(d->udev,
1330                        d->props.adapter[0].fe[0].stream.endpoint));
1331 
1332     /* Reset the tuner */
1333     if (cxusb_d680_dmb_gpio_tuner(d, 0x07, 0) < 0) {
1334         err("clear tuner gpio failed");
1335         return -EIO;
1336     }
1337     msleep(100);
1338     if (cxusb_d680_dmb_gpio_tuner(d, 0x07, 1) < 0) {
1339         err("set tuner gpio failed");
1340         return -EIO;
1341     }
1342     msleep(100);
1343 
1344     /* Attach frontend */
1345     adap->fe_adap[0].fe = dvb_attach(atbm8830_attach,
1346                      &mygica_d689_atbm8830_cfg,
1347                      &d->i2c_adap);
1348     if (!adap->fe_adap[0].fe)
1349         return -EIO;
1350 
1351     return 0;
1352 }
1353 
1354 /*
1355  * DViCO has shipped two devices with the same USB ID, but only one of them
1356  * needs a firmware download.  Check the device class details to see if they
1357  * have non-default values to decide whether the device is actually cold or
1358  * not, and forget a match if it turns out we selected the wrong device.
1359  */
1360 static int bluebird_fx2_identify_state(struct usb_device *udev,
1361                        const struct dvb_usb_device_properties *props,
1362                        const struct dvb_usb_device_description **desc,
1363                        int *cold)
1364 {
1365     int wascold = *cold;
1366 
1367     *cold = udev->descriptor.bDeviceClass == 0xff &&
1368         udev->descriptor.bDeviceSubClass == 0xff &&
1369         udev->descriptor.bDeviceProtocol == 0xff;
1370 
1371     if (*cold && !wascold)
1372         *desc = NULL;
1373 
1374     return 0;
1375 }
1376 
1377 /*
1378  * DViCO bluebird firmware needs the "warm" product ID to be patched into the
1379  * firmware file before download.
1380  */
1381 
1382 static const int dvico_firmware_id_offsets[] = { 6638, 3204 };
1383 static int bluebird_patch_dvico_firmware_download(struct usb_device *udev,
1384                           const struct firmware *fw)
1385 {
1386     int pos;
1387 
1388     for (pos = 0; pos < ARRAY_SIZE(dvico_firmware_id_offsets); pos++) {
1389         int idoff = dvico_firmware_id_offsets[pos];
1390 
1391         if (fw->size < idoff + 4)
1392             continue;
1393 
1394         if (fw->data[idoff] == (USB_VID_DVICO & 0xff) &&
1395             fw->data[idoff + 1] == USB_VID_DVICO >> 8) {
1396             struct firmware new_fw;
1397             u8 *new_fw_data = vmalloc(fw->size);
1398             int ret;
1399 
1400             if (!new_fw_data)
1401                 return -ENOMEM;
1402 
1403             memcpy(new_fw_data, fw->data, fw->size);
1404             new_fw.size = fw->size;
1405             new_fw.data = new_fw_data;
1406 
1407             new_fw_data[idoff + 2] =
1408                 le16_to_cpu(udev->descriptor.idProduct) + 1;
1409             new_fw_data[idoff + 3] =
1410                 le16_to_cpu(udev->descriptor.idProduct) >> 8;
1411 
1412             ret = usb_cypress_load_firmware(udev, &new_fw,
1413                             CYPRESS_FX2);
1414             vfree(new_fw_data);
1415             return ret;
1416         }
1417     }
1418 
1419     return -EINVAL;
1420 }
1421 
1422 int cxusb_medion_get(struct dvb_usb_device *dvbdev,
1423              enum cxusb_open_type open_type)
1424 {
1425     struct cxusb_medion_dev *cxdev = dvbdev->priv;
1426     int ret = 0;
1427 
1428     mutex_lock(&cxdev->open_lock);
1429 
1430     if (WARN_ON((cxdev->open_type == CXUSB_OPEN_INIT ||
1431              cxdev->open_type == CXUSB_OPEN_NONE) &&
1432             cxdev->open_ctr != 0)) {
1433         ret = -EINVAL;
1434         goto ret_unlock;
1435     }
1436 
1437     if (cxdev->open_type == CXUSB_OPEN_INIT) {
1438         ret = -EAGAIN;
1439         goto ret_unlock;
1440     }
1441 
1442     if (cxdev->open_ctr == 0) {
1443         if (cxdev->open_type != open_type) {
1444             dev_info(&dvbdev->udev->dev, "will acquire and switch to %s\n",
1445                  open_type == CXUSB_OPEN_ANALOG ?
1446                  "analog" : "digital");
1447 
1448             if (open_type == CXUSB_OPEN_ANALOG) {
1449                 ret = _cxusb_power_ctrl(dvbdev, 1);
1450                 if (ret != 0)
1451                     dev_warn(&dvbdev->udev->dev,
1452                          "powerup for analog switch failed (%d)\n",
1453                          ret);
1454 
1455                 ret = cxusb_medion_set_mode(dvbdev, false);
1456                 if (ret != 0)
1457                     goto ret_unlock;
1458 
1459                 ret = cxusb_medion_analog_init(dvbdev);
1460                 if (ret != 0)
1461                     goto ret_unlock;
1462             } else { /* digital */
1463                 ret = _cxusb_power_ctrl(dvbdev, 1);
1464                 if (ret != 0)
1465                     dev_warn(&dvbdev->udev->dev,
1466                          "powerup for digital switch failed (%d)\n",
1467                          ret);
1468 
1469                 ret = cxusb_medion_set_mode(dvbdev, true);
1470                 if (ret != 0)
1471                     goto ret_unlock;
1472             }
1473 
1474             cxdev->open_type = open_type;
1475         } else {
1476             dev_info(&dvbdev->udev->dev, "reacquired idle %s\n",
1477                  open_type == CXUSB_OPEN_ANALOG ?
1478                  "analog" : "digital");
1479         }
1480 
1481         cxdev->open_ctr = 1;
1482     } else if (cxdev->open_type == open_type) {
1483         cxdev->open_ctr++;
1484         dev_info(&dvbdev->udev->dev, "acquired %s\n",
1485              open_type == CXUSB_OPEN_ANALOG ? "analog" : "digital");
1486     } else {
1487         ret = -EBUSY;
1488     }
1489 
1490 ret_unlock:
1491     mutex_unlock(&cxdev->open_lock);
1492 
1493     return ret;
1494 }
1495 
1496 void cxusb_medion_put(struct dvb_usb_device *dvbdev)
1497 {
1498     struct cxusb_medion_dev *cxdev = dvbdev->priv;
1499 
1500     mutex_lock(&cxdev->open_lock);
1501 
1502     if (cxdev->open_type == CXUSB_OPEN_INIT) {
1503         WARN_ON(cxdev->open_ctr != 0);
1504         cxdev->open_type = CXUSB_OPEN_NONE;
1505         goto unlock;
1506     }
1507 
1508     if (!WARN_ON(cxdev->open_ctr < 1)) {
1509         cxdev->open_ctr--;
1510 
1511         dev_info(&dvbdev->udev->dev, "release %s\n",
1512              cxdev->open_type == CXUSB_OPEN_ANALOG ?
1513              "analog" : "digital");
1514     }
1515 
1516 unlock:
1517     mutex_unlock(&cxdev->open_lock);
1518 }
1519 
1520 /* DVB USB Driver stuff */
1521 static struct dvb_usb_device_properties cxusb_medion_properties;
1522 static struct dvb_usb_device_properties cxusb_bluebird_lgh064f_properties;
1523 static struct dvb_usb_device_properties cxusb_bluebird_dee1601_properties;
1524 static struct dvb_usb_device_properties cxusb_bluebird_lgz201_properties;
1525 static struct dvb_usb_device_properties cxusb_bluebird_dtt7579_properties;
1526 static struct dvb_usb_device_properties cxusb_bluebird_dualdig4_properties;
1527 static struct dvb_usb_device_properties cxusb_bluebird_dualdig4_rev2_properties;
1528 static struct dvb_usb_device_properties cxusb_bluebird_nano2_properties;
1529 static struct dvb_usb_device_properties cxusb_bluebird_nano2_needsfirmware_properties;
1530 static struct dvb_usb_device_properties cxusb_aver_a868r_properties;
1531 static struct dvb_usb_device_properties cxusb_d680_dmb_properties;
1532 static struct dvb_usb_device_properties cxusb_mygica_d689_properties;
1533 
1534 static int cxusb_medion_priv_init(struct dvb_usb_device *dvbdev)
1535 {
1536     struct cxusb_medion_dev *cxdev = dvbdev->priv;
1537 
1538     cxdev->dvbdev = dvbdev;
1539     cxdev->open_type = CXUSB_OPEN_INIT;
1540     mutex_init(&cxdev->open_lock);
1541 
1542     return 0;
1543 }
1544 
1545 static void cxusb_medion_priv_destroy(struct dvb_usb_device *dvbdev)
1546 {
1547     struct cxusb_medion_dev *cxdev = dvbdev->priv;
1548 
1549     mutex_destroy(&cxdev->open_lock);
1550 }
1551 
1552 static bool cxusb_medion_check_altsetting(struct usb_host_interface *as)
1553 {
1554     unsigned int ctr;
1555 
1556     for (ctr = 0; ctr < as->desc.bNumEndpoints; ctr++) {
1557         if ((as->endpoint[ctr].desc.bEndpointAddress &
1558              USB_ENDPOINT_NUMBER_MASK) != 2)
1559             continue;
1560 
1561         if (as->endpoint[ctr].desc.bEndpointAddress & USB_DIR_IN &&
1562             ((as->endpoint[ctr].desc.bmAttributes &
1563               USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_ISOC))
1564             return true;
1565 
1566         break;
1567     }
1568 
1569     return false;
1570 }
1571 
1572 static bool cxusb_medion_check_intf(struct usb_interface *intf)
1573 {
1574     unsigned int ctr;
1575 
1576     if (intf->num_altsetting < 2) {
1577         dev_err(intf->usb_dev, "no alternate interface");
1578 
1579         return false;
1580     }
1581 
1582     for (ctr = 0; ctr < intf->num_altsetting; ctr++) {
1583         if (intf->altsetting[ctr].desc.bAlternateSetting != 1)
1584             continue;
1585 
1586         if (cxusb_medion_check_altsetting(&intf->altsetting[ctr]))
1587             return true;
1588 
1589         break;
1590     }
1591 
1592     dev_err(intf->usb_dev, "no iso interface");
1593 
1594     return false;
1595 }
1596 
1597 static int cxusb_probe(struct usb_interface *intf,
1598                const struct usb_device_id *id)
1599 {
1600     struct dvb_usb_device *dvbdev;
1601     int ret;
1602 
1603     /* Medion 95700 */
1604     if (!dvb_usb_device_init(intf, &cxusb_medion_properties,
1605                  THIS_MODULE, &dvbdev, adapter_nr)) {
1606         if (!cxusb_medion_check_intf(intf)) {
1607             ret = -ENODEV;
1608             goto ret_uninit;
1609         }
1610 
1611         _cxusb_power_ctrl(dvbdev, 1);
1612         ret = cxusb_medion_set_mode(dvbdev, false);
1613         if (ret)
1614             goto ret_uninit;
1615 
1616         ret = cxusb_medion_register_analog(dvbdev);
1617 
1618         cxusb_medion_set_mode(dvbdev, true);
1619         _cxusb_power_ctrl(dvbdev, 0);
1620 
1621         if (ret != 0)
1622             goto ret_uninit;
1623 
1624         /* release device from INIT mode to normal operation */
1625         cxusb_medion_put(dvbdev);
1626 
1627         return 0;
1628     } else if (!dvb_usb_device_init(intf,
1629                     &cxusb_bluebird_lgh064f_properties,
1630                     THIS_MODULE, NULL, adapter_nr) ||
1631            !dvb_usb_device_init(intf,
1632                     &cxusb_bluebird_dee1601_properties,
1633                     THIS_MODULE, NULL, adapter_nr) ||
1634            !dvb_usb_device_init(intf,
1635                     &cxusb_bluebird_lgz201_properties,
1636                     THIS_MODULE, NULL, adapter_nr) ||
1637            !dvb_usb_device_init(intf,
1638                     &cxusb_bluebird_dtt7579_properties,
1639                     THIS_MODULE, NULL, adapter_nr) ||
1640            !dvb_usb_device_init(intf,
1641                     &cxusb_bluebird_dualdig4_properties,
1642                     THIS_MODULE, NULL, adapter_nr) ||
1643            !dvb_usb_device_init(intf,
1644                     &cxusb_bluebird_nano2_properties,
1645                     THIS_MODULE, NULL, adapter_nr) ||
1646            !dvb_usb_device_init(intf,
1647                     &cxusb_bluebird_nano2_needsfirmware_properties,
1648                     THIS_MODULE, NULL, adapter_nr) ||
1649            !dvb_usb_device_init(intf, &cxusb_aver_a868r_properties,
1650                     THIS_MODULE, NULL, adapter_nr) ||
1651            !dvb_usb_device_init(intf,
1652                     &cxusb_bluebird_dualdig4_rev2_properties,
1653                     THIS_MODULE, NULL, adapter_nr) ||
1654            !dvb_usb_device_init(intf, &cxusb_d680_dmb_properties,
1655                     THIS_MODULE, NULL, adapter_nr) ||
1656            !dvb_usb_device_init(intf, &cxusb_mygica_d689_properties,
1657                     THIS_MODULE, NULL, adapter_nr) ||
1658            0)
1659         return 0;
1660 
1661     return -EINVAL;
1662 
1663 ret_uninit:
1664     dvb_usb_device_exit(intf);
1665 
1666     return ret;
1667 }
1668 
1669 static void cxusb_disconnect(struct usb_interface *intf)
1670 {
1671     struct dvb_usb_device *d = usb_get_intfdata(intf);
1672     struct cxusb_state *st = d->priv;
1673     struct i2c_client *client;
1674 
1675     if (d->props.devices[0].warm_ids[0] == &cxusb_table[MEDION_MD95700])
1676         cxusb_medion_unregister_analog(d);
1677 
1678     /* remove I2C client for tuner */
1679     client = st->i2c_client_tuner;
1680     if (client) {
1681         module_put(client->dev.driver->owner);
1682         i2c_unregister_device(client);
1683     }
1684 
1685     /* remove I2C client for demodulator */
1686     client = st->i2c_client_demod;
1687     if (client) {
1688         module_put(client->dev.driver->owner);
1689         i2c_unregister_device(client);
1690     }
1691 
1692     dvb_usb_device_exit(intf);
1693 }
1694 
1695 static struct usb_device_id cxusb_table[] = {
1696     DVB_USB_DEV(MEDION, MEDION_MD95700),
1697     DVB_USB_DEV(DVICO, DVICO_BLUEBIRD_LG064F_COLD),
1698     DVB_USB_DEV(DVICO, DVICO_BLUEBIRD_LG064F_WARM),
1699     DVB_USB_DEV(DVICO, DVICO_BLUEBIRD_DUAL_1_COLD),
1700     DVB_USB_DEV(DVICO, DVICO_BLUEBIRD_DUAL_1_WARM),
1701     DVB_USB_DEV(DVICO, DVICO_BLUEBIRD_LGZ201_COLD),
1702     DVB_USB_DEV(DVICO, DVICO_BLUEBIRD_LGZ201_WARM),
1703     DVB_USB_DEV(DVICO, DVICO_BLUEBIRD_TH7579_COLD),
1704     DVB_USB_DEV(DVICO, DVICO_BLUEBIRD_TH7579_WARM),
1705     DVB_USB_DEV(DVICO, DIGITALNOW_BLUEBIRD_DUAL_1_COLD),
1706     DVB_USB_DEV(DVICO, DIGITALNOW_BLUEBIRD_DUAL_1_WARM),
1707     DVB_USB_DEV(DVICO, DVICO_BLUEBIRD_DUAL_2_COLD),
1708     DVB_USB_DEV(DVICO, DVICO_BLUEBIRD_DUAL_2_WARM),
1709     DVB_USB_DEV(DVICO, DVICO_BLUEBIRD_DUAL_4),
1710     DVB_USB_DEV(DVICO, DVICO_BLUEBIRD_DVB_T_NANO_2),
1711     DVB_USB_DEV(DVICO, DVICO_BLUEBIRD_DVB_T_NANO_2_NFW_WARM),
1712     DVB_USB_DEV(AVERMEDIA, AVERMEDIA_VOLAR_A868R),
1713     DVB_USB_DEV(DVICO, DVICO_BLUEBIRD_DUAL_4_REV_2),
1714     DVB_USB_DEV(CONEXANT, CONEXANT_D680_DMB),
1715     DVB_USB_DEV(CONEXANT, MYGICA_D689),
1716     { }
1717 };
1718 
1719 MODULE_DEVICE_TABLE(usb, cxusb_table);
1720 
1721 static struct dvb_usb_device_properties cxusb_medion_properties = {
1722     .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1723 
1724     .usb_ctrl = CYPRESS_FX2,
1725 
1726     .size_of_priv     = sizeof(struct cxusb_medion_dev),
1727     .priv_init        = cxusb_medion_priv_init,
1728     .priv_destroy     = cxusb_medion_priv_destroy,
1729 
1730     .num_adapters = 1,
1731     .adapter = {
1732         {
1733         .num_frontends = 1,
1734         .fe = {{
1735             .streaming_ctrl   = cxusb_streaming_ctrl,
1736             .frontend_attach  = cxusb_cx22702_frontend_attach,
1737             .tuner_attach     = cxusb_fmd1216me_tuner_attach,
1738             /* parameter for the MPEG2-data transfer */
1739                     .stream = {
1740                         .type = USB_BULK,
1741                 .count = 5,
1742                 .endpoint = 0x02,
1743                 .u = {
1744                     .bulk = {
1745                         .buffersize = 8192,
1746                     }
1747                 }
1748             },
1749         } },
1750         },
1751     },
1752     .power_ctrl       = cxusb_power_ctrl,
1753 
1754     .i2c_algo         = &cxusb_i2c_algo,
1755 
1756     .generic_bulk_ctrl_endpoint = 0x01,
1757 
1758     .num_device_descs = 1,
1759     .devices = {
1760         {
1761             "Medion MD95700 (MDUSBTV-HYBRID)",
1762             { NULL },
1763             { &cxusb_table[MEDION_MD95700], NULL },
1764         },
1765     }
1766 };
1767 
1768 static struct dvb_usb_device_properties cxusb_bluebird_lgh064f_properties = {
1769     .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1770 
1771     .usb_ctrl          = DEVICE_SPECIFIC,
1772     .firmware          = "dvb-usb-bluebird-01.fw",
1773     .download_firmware = bluebird_patch_dvico_firmware_download,
1774     /*
1775      * use usb alt setting 0 for EP4 transfer (dvb-t),
1776      * use usb alt setting 7 for EP2 transfer (atsc)
1777      */
1778 
1779     .size_of_priv     = sizeof(struct cxusb_state),
1780 
1781     .num_adapters = 1,
1782     .adapter = {
1783         {
1784         .num_frontends = 1,
1785         .fe = {{
1786             .streaming_ctrl   = cxusb_streaming_ctrl,
1787             .frontend_attach  = cxusb_lgdt3303_frontend_attach,
1788             .tuner_attach     = cxusb_lgh064f_tuner_attach,
1789 
1790             /* parameter for the MPEG2-data transfer */
1791                     .stream = {
1792                         .type = USB_BULK,
1793                 .count = 5,
1794                 .endpoint = 0x02,
1795                 .u = {
1796                     .bulk = {
1797                         .buffersize = 8192,
1798                     }
1799                 }
1800             },
1801         } },
1802         },
1803     },
1804 
1805     .power_ctrl       = cxusb_bluebird_power_ctrl,
1806 
1807     .i2c_algo         = &cxusb_i2c_algo,
1808 
1809     .rc.core = {
1810         .rc_interval    = 100,
1811         .rc_codes   = RC_MAP_DVICO_PORTABLE,
1812         .module_name    = KBUILD_MODNAME,
1813         .rc_query   = cxusb_rc_query,
1814         .allowed_protos = RC_PROTO_BIT_NEC,
1815     },
1816 
1817     .generic_bulk_ctrl_endpoint = 0x01,
1818 
1819     .num_device_descs = 1,
1820     .devices = {
1821         {   "DViCO FusionHDTV5 USB Gold",
1822             { &cxusb_table[DVICO_BLUEBIRD_LG064F_COLD], NULL },
1823             { &cxusb_table[DVICO_BLUEBIRD_LG064F_WARM], NULL },
1824         },
1825     }
1826 };
1827 
1828 static struct dvb_usb_device_properties cxusb_bluebird_dee1601_properties = {
1829     .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1830 
1831     .usb_ctrl          = DEVICE_SPECIFIC,
1832     .firmware          = "dvb-usb-bluebird-01.fw",
1833     .download_firmware = bluebird_patch_dvico_firmware_download,
1834     /*
1835      * use usb alt setting 0 for EP4 transfer (dvb-t),
1836      * use usb alt setting 7 for EP2 transfer (atsc)
1837      */
1838 
1839     .size_of_priv     = sizeof(struct cxusb_state),
1840 
1841     .num_adapters = 1,
1842     .adapter = {
1843         {
1844         .num_frontends = 1,
1845         .fe = {{
1846             .streaming_ctrl   = cxusb_streaming_ctrl,
1847             .frontend_attach  = cxusb_dee1601_frontend_attach,
1848             .tuner_attach     = cxusb_dee1601_tuner_attach,
1849             /* parameter for the MPEG2-data transfer */
1850             .stream = {
1851                 .type = USB_BULK,
1852                 .count = 5,
1853                 .endpoint = 0x04,
1854                 .u = {
1855                     .bulk = {
1856                         .buffersize = 8192,
1857                     }
1858                 }
1859             },
1860         } },
1861         },
1862     },
1863 
1864     .power_ctrl       = cxusb_bluebird_power_ctrl,
1865 
1866     .i2c_algo         = &cxusb_i2c_algo,
1867 
1868     .rc.core = {
1869         .rc_interval    = 100,
1870         .rc_codes   = RC_MAP_DVICO_MCE,
1871         .module_name    = KBUILD_MODNAME,
1872         .rc_query   = cxusb_rc_query,
1873         .allowed_protos = RC_PROTO_BIT_NEC,
1874     },
1875 
1876     .generic_bulk_ctrl_endpoint = 0x01,
1877 
1878     .num_device_descs = 3,
1879     .devices = {
1880         {   "DViCO FusionHDTV DVB-T Dual USB",
1881             { &cxusb_table[DVICO_BLUEBIRD_DUAL_1_COLD], NULL },
1882             { &cxusb_table[DVICO_BLUEBIRD_DUAL_1_WARM], NULL },
1883         },
1884         {   "DigitalNow DVB-T Dual USB",
1885             { &cxusb_table[DIGITALNOW_BLUEBIRD_DUAL_1_COLD], NULL },
1886             { &cxusb_table[DIGITALNOW_BLUEBIRD_DUAL_1_WARM], NULL },
1887         },
1888         {   "DViCO FusionHDTV DVB-T Dual Digital 2",
1889             { &cxusb_table[DVICO_BLUEBIRD_DUAL_2_COLD], NULL },
1890             { &cxusb_table[DVICO_BLUEBIRD_DUAL_2_WARM], NULL },
1891         },
1892     }
1893 };
1894 
1895 static struct dvb_usb_device_properties cxusb_bluebird_lgz201_properties = {
1896     .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1897 
1898     .usb_ctrl          = DEVICE_SPECIFIC,
1899     .firmware          = "dvb-usb-bluebird-01.fw",
1900     .download_firmware = bluebird_patch_dvico_firmware_download,
1901     /*
1902      * use usb alt setting 0 for EP4 transfer (dvb-t),
1903      * use usb alt setting 7 for EP2 transfer (atsc)
1904      */
1905 
1906     .size_of_priv     = sizeof(struct cxusb_state),
1907 
1908     .num_adapters = 1,
1909     .adapter = {
1910         {
1911         .num_frontends = 1,
1912         .fe = {{
1913             .streaming_ctrl   = cxusb_streaming_ctrl,
1914             .frontend_attach  = cxusb_mt352_frontend_attach,
1915             .tuner_attach     = cxusb_lgz201_tuner_attach,
1916 
1917             /* parameter for the MPEG2-data transfer */
1918             .stream = {
1919                 .type = USB_BULK,
1920                 .count = 5,
1921                 .endpoint = 0x04,
1922                 .u = {
1923                     .bulk = {
1924                         .buffersize = 8192,
1925                     }
1926                 }
1927             },
1928         } },
1929         },
1930     },
1931     .power_ctrl       = cxusb_bluebird_power_ctrl,
1932 
1933     .i2c_algo         = &cxusb_i2c_algo,
1934 
1935     .rc.core = {
1936         .rc_interval    = 100,
1937         .rc_codes   = RC_MAP_DVICO_PORTABLE,
1938         .module_name    = KBUILD_MODNAME,
1939         .rc_query   = cxusb_rc_query,
1940         .allowed_protos = RC_PROTO_BIT_NEC,
1941     },
1942 
1943     .generic_bulk_ctrl_endpoint = 0x01,
1944     .num_device_descs = 1,
1945     .devices = {
1946         {   "DViCO FusionHDTV DVB-T USB (LGZ201)",
1947             { &cxusb_table[DVICO_BLUEBIRD_LGZ201_COLD], NULL },
1948             { &cxusb_table[DVICO_BLUEBIRD_LGZ201_WARM], NULL },
1949         },
1950     }
1951 };
1952 
1953 static struct dvb_usb_device_properties cxusb_bluebird_dtt7579_properties = {
1954     .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1955 
1956     .usb_ctrl          = DEVICE_SPECIFIC,
1957     .firmware          = "dvb-usb-bluebird-01.fw",
1958     .download_firmware = bluebird_patch_dvico_firmware_download,
1959 
1960     /*
1961      * use usb alt setting 0 for EP4 transfer (dvb-t),
1962      * use usb alt setting 7 for EP2 transfer (atsc)
1963      */
1964 
1965     .size_of_priv     = sizeof(struct cxusb_state),
1966 
1967     .num_adapters = 1,
1968     .adapter = {
1969         {
1970         .num_frontends = 1,
1971         .fe = {{
1972             .streaming_ctrl   = cxusb_streaming_ctrl,
1973             .frontend_attach  = cxusb_mt352_frontend_attach,
1974             .tuner_attach     = cxusb_dtt7579_tuner_attach,
1975 
1976             /* parameter for the MPEG2-data transfer */
1977             .stream = {
1978                 .type = USB_BULK,
1979                 .count = 5,
1980                 .endpoint = 0x04,
1981                 .u = {
1982                     .bulk = {
1983                         .buffersize = 8192,
1984                     }
1985                 }
1986             },
1987         } },
1988         },
1989     },
1990     .power_ctrl       = cxusb_bluebird_power_ctrl,
1991 
1992     .i2c_algo         = &cxusb_i2c_algo,
1993 
1994     .rc.core = {
1995         .rc_interval    = 100,
1996         .rc_codes   = RC_MAP_DVICO_PORTABLE,
1997         .module_name    = KBUILD_MODNAME,
1998         .rc_query   = cxusb_rc_query,
1999         .allowed_protos = RC_PROTO_BIT_NEC,
2000     },
2001 
2002     .generic_bulk_ctrl_endpoint = 0x01,
2003 
2004     .num_device_descs = 1,
2005     .devices = {
2006         {   "DViCO FusionHDTV DVB-T USB (TH7579)",
2007             { &cxusb_table[DVICO_BLUEBIRD_TH7579_COLD], NULL },
2008             { &cxusb_table[DVICO_BLUEBIRD_TH7579_WARM], NULL },
2009         },
2010     }
2011 };
2012 
2013 static struct dvb_usb_device_properties cxusb_bluebird_dualdig4_properties = {
2014     .caps = DVB_USB_IS_AN_I2C_ADAPTER,
2015 
2016     .usb_ctrl         = CYPRESS_FX2,
2017 
2018     .size_of_priv     = sizeof(struct cxusb_state),
2019 
2020     .num_adapters = 1,
2021     .adapter = {
2022         {
2023         .num_frontends = 1,
2024         .fe = {{
2025             .streaming_ctrl   = cxusb_streaming_ctrl,
2026             .frontend_attach  = cxusb_dualdig4_frontend_attach,
2027             .tuner_attach     = cxusb_dvico_xc3028_tuner_attach,
2028             /* parameter for the MPEG2-data transfer */
2029             .stream = {
2030                 .type = USB_BULK,
2031                 .count = 5,
2032                 .endpoint = 0x02,
2033                 .u = {
2034                     .bulk = {
2035                         .buffersize = 8192,
2036                     }
2037                 }
2038             },
2039         } },
2040         },
2041     },
2042 
2043     .power_ctrl       = cxusb_power_ctrl,
2044 
2045     .i2c_algo         = &cxusb_i2c_algo,
2046 
2047     .generic_bulk_ctrl_endpoint = 0x01,
2048 
2049     .rc.core = {
2050         .rc_interval    = 100,
2051         .rc_codes   = RC_MAP_DVICO_MCE,
2052         .module_name    = KBUILD_MODNAME,
2053         .rc_query   = cxusb_bluebird2_rc_query,
2054         .allowed_protos = RC_PROTO_BIT_NEC,
2055     },
2056 
2057     .num_device_descs = 1,
2058     .devices = {
2059         {   "DViCO FusionHDTV DVB-T Dual Digital 4",
2060             { NULL },
2061             { &cxusb_table[DVICO_BLUEBIRD_DUAL_4], NULL },
2062         },
2063     }
2064 };
2065 
2066 static struct dvb_usb_device_properties cxusb_bluebird_nano2_properties = {
2067     .caps = DVB_USB_IS_AN_I2C_ADAPTER,
2068 
2069     .usb_ctrl         = CYPRESS_FX2,
2070     .identify_state   = bluebird_fx2_identify_state,
2071 
2072     .size_of_priv     = sizeof(struct cxusb_state),
2073 
2074     .num_adapters = 1,
2075     .adapter = {
2076         {
2077         .num_frontends = 1,
2078         .fe = {{
2079             .streaming_ctrl   = cxusb_streaming_ctrl,
2080             .frontend_attach  = cxusb_nano2_frontend_attach,
2081             .tuner_attach     = cxusb_dvico_xc3028_tuner_attach,
2082             /* parameter for the MPEG2-data transfer */
2083             .stream = {
2084                 .type = USB_BULK,
2085                 .count = 5,
2086                 .endpoint = 0x02,
2087                 .u = {
2088                     .bulk = {
2089                         .buffersize = 8192,
2090                     }
2091                 }
2092             },
2093         } },
2094         },
2095     },
2096 
2097     .power_ctrl       = cxusb_nano2_power_ctrl,
2098 
2099     .i2c_algo         = &cxusb_i2c_algo,
2100 
2101     .generic_bulk_ctrl_endpoint = 0x01,
2102 
2103     .rc.core = {
2104         .rc_interval    = 100,
2105         .rc_codes   = RC_MAP_DVICO_PORTABLE,
2106         .module_name    = KBUILD_MODNAME,
2107         .rc_query       = cxusb_bluebird2_rc_query,
2108         .allowed_protos = RC_PROTO_BIT_NEC,
2109     },
2110 
2111     .num_device_descs = 1,
2112     .devices = {
2113         {   "DViCO FusionHDTV DVB-T NANO2",
2114             { NULL },
2115             { &cxusb_table[DVICO_BLUEBIRD_DVB_T_NANO_2], NULL },
2116         },
2117     }
2118 };
2119 
2120 static struct dvb_usb_device_properties
2121 cxusb_bluebird_nano2_needsfirmware_properties = {
2122     .caps = DVB_USB_IS_AN_I2C_ADAPTER,
2123 
2124     .usb_ctrl          = DEVICE_SPECIFIC,
2125     .firmware          = "dvb-usb-bluebird-02.fw",
2126     .download_firmware = bluebird_patch_dvico_firmware_download,
2127     .identify_state    = bluebird_fx2_identify_state,
2128 
2129     .size_of_priv      = sizeof(struct cxusb_state),
2130 
2131     .num_adapters = 1,
2132     .adapter = {
2133         {
2134         .num_frontends = 1,
2135         .fe = {{
2136             .streaming_ctrl   = cxusb_streaming_ctrl,
2137             .frontend_attach  = cxusb_nano2_frontend_attach,
2138             .tuner_attach     = cxusb_dvico_xc3028_tuner_attach,
2139             /* parameter for the MPEG2-data transfer */
2140             .stream = {
2141                 .type = USB_BULK,
2142                 .count = 5,
2143                 .endpoint = 0x02,
2144                 .u = {
2145                     .bulk = {
2146                         .buffersize = 8192,
2147                     }
2148                 }
2149             },
2150         } },
2151         },
2152     },
2153 
2154     .power_ctrl       = cxusb_nano2_power_ctrl,
2155 
2156     .i2c_algo         = &cxusb_i2c_algo,
2157 
2158     .generic_bulk_ctrl_endpoint = 0x01,
2159 
2160     .rc.core = {
2161         .rc_interval    = 100,
2162         .rc_codes   = RC_MAP_DVICO_PORTABLE,
2163         .module_name    = KBUILD_MODNAME,
2164         .rc_query   = cxusb_rc_query,
2165         .allowed_protos = RC_PROTO_BIT_NEC,
2166     },
2167 
2168     .num_device_descs = 1,
2169     .devices = { {
2170             "DViCO FusionHDTV DVB-T NANO2 w/o firmware",
2171             { &cxusb_table[DVICO_BLUEBIRD_DVB_T_NANO_2], NULL },
2172             { &cxusb_table[DVICO_BLUEBIRD_DVB_T_NANO_2_NFW_WARM],
2173               NULL },
2174         },
2175     }
2176 };
2177 
2178 static struct dvb_usb_device_properties cxusb_aver_a868r_properties = {
2179     .caps = DVB_USB_IS_AN_I2C_ADAPTER,
2180 
2181     .usb_ctrl         = CYPRESS_FX2,
2182 
2183     .size_of_priv     = sizeof(struct cxusb_state),
2184 
2185     .num_adapters = 1,
2186     .adapter = {
2187         {
2188         .num_frontends = 1,
2189         .fe = {{
2190             .streaming_ctrl   = cxusb_aver_streaming_ctrl,
2191             .frontend_attach  = cxusb_aver_lgdt3303_frontend_attach,
2192             .tuner_attach     = cxusb_mxl5003s_tuner_attach,
2193             /* parameter for the MPEG2-data transfer */
2194             .stream = {
2195                 .type = USB_BULK,
2196                 .count = 5,
2197                 .endpoint = 0x04,
2198                 .u = {
2199                     .bulk = {
2200                         .buffersize = 8192,
2201                     }
2202                 }
2203             },
2204         } },
2205         },
2206     },
2207     .power_ctrl       = cxusb_aver_power_ctrl,
2208 
2209     .i2c_algo         = &cxusb_i2c_algo,
2210 
2211     .generic_bulk_ctrl_endpoint = 0x01,
2212 
2213     .num_device_descs = 1,
2214     .devices = {
2215         {   "AVerMedia AVerTVHD Volar (A868R)",
2216             { NULL },
2217             { &cxusb_table[AVERMEDIA_VOLAR_A868R], NULL },
2218         },
2219     }
2220 };
2221 
2222 static
2223 struct dvb_usb_device_properties cxusb_bluebird_dualdig4_rev2_properties = {
2224     .caps = DVB_USB_IS_AN_I2C_ADAPTER,
2225 
2226     .usb_ctrl         = CYPRESS_FX2,
2227 
2228     .size_of_priv     = sizeof(struct cxusb_state),
2229 
2230     .num_adapters = 1,
2231     .adapter = {
2232         {
2233         .size_of_priv    = sizeof(struct dib0700_adapter_state),
2234         .num_frontends = 1,
2235         .fe = {{
2236             .streaming_ctrl  = cxusb_streaming_ctrl,
2237             .frontend_attach = cxusb_dualdig4_rev2_frontend_attach,
2238             .tuner_attach    = cxusb_dualdig4_rev2_tuner_attach,
2239             /* parameter for the MPEG2-data transfer */
2240             .stream = {
2241                 .type = USB_BULK,
2242                 .count = 7,
2243                 .endpoint = 0x02,
2244                 .u = {
2245                     .bulk = {
2246                         .buffersize = 4096,
2247                     }
2248                 }
2249             },
2250         } },
2251         },
2252     },
2253 
2254     .power_ctrl       = cxusb_bluebird_power_ctrl,
2255 
2256     .i2c_algo         = &cxusb_i2c_algo,
2257 
2258     .generic_bulk_ctrl_endpoint = 0x01,
2259 
2260     .rc.core = {
2261         .rc_interval    = 100,
2262         .rc_codes   = RC_MAP_DVICO_MCE,
2263         .module_name    = KBUILD_MODNAME,
2264         .rc_query   = cxusb_rc_query,
2265         .allowed_protos = RC_PROTO_BIT_NEC,
2266     },
2267 
2268     .num_device_descs = 1,
2269     .devices = {
2270         {   "DViCO FusionHDTV DVB-T Dual Digital 4 (rev 2)",
2271             { NULL },
2272             { &cxusb_table[DVICO_BLUEBIRD_DUAL_4_REV_2], NULL },
2273         },
2274     }
2275 };
2276 
2277 static struct dvb_usb_device_properties cxusb_d680_dmb_properties = {
2278     .caps = DVB_USB_IS_AN_I2C_ADAPTER,
2279 
2280     .usb_ctrl         = CYPRESS_FX2,
2281 
2282     .size_of_priv     = sizeof(struct cxusb_state),
2283 
2284     .num_adapters = 1,
2285     .adapter = {
2286         {
2287         .num_frontends = 1,
2288         .fe = {{
2289             .streaming_ctrl   = cxusb_d680_dmb_streaming_ctrl,
2290             .frontend_attach  = cxusb_d680_dmb_frontend_attach,
2291             .tuner_attach     = cxusb_d680_dmb_tuner_attach,
2292 
2293             /* parameter for the MPEG2-data transfer */
2294             .stream = {
2295                 .type = USB_BULK,
2296                 .count = 5,
2297                 .endpoint = 0x02,
2298                 .u = {
2299                     .bulk = {
2300                         .buffersize = 8192,
2301                     }
2302                 }
2303             },
2304         } },
2305         },
2306     },
2307 
2308     .power_ctrl       = cxusb_d680_dmb_power_ctrl,
2309 
2310     .i2c_algo         = &cxusb_i2c_algo,
2311 
2312     .generic_bulk_ctrl_endpoint = 0x01,
2313 
2314     .rc.core = {
2315         .rc_interval    = 100,
2316         .rc_codes   = RC_MAP_TOTAL_MEDIA_IN_HAND_02,
2317         .module_name    = KBUILD_MODNAME,
2318         .rc_query       = cxusb_d680_dmb_rc_query,
2319         .allowed_protos = RC_PROTO_BIT_UNKNOWN,
2320     },
2321 
2322     .num_device_descs = 1,
2323     .devices = {
2324         {
2325             "Conexant DMB-TH Stick",
2326             { NULL },
2327             { &cxusb_table[CONEXANT_D680_DMB], NULL },
2328         },
2329     }
2330 };
2331 
2332 static struct dvb_usb_device_properties cxusb_mygica_d689_properties = {
2333     .caps = DVB_USB_IS_AN_I2C_ADAPTER,
2334 
2335     .usb_ctrl         = CYPRESS_FX2,
2336 
2337     .size_of_priv     = sizeof(struct cxusb_state),
2338 
2339     .num_adapters = 1,
2340     .adapter = {
2341         {
2342         .num_frontends = 1,
2343         .fe = {{
2344             .streaming_ctrl   = cxusb_d680_dmb_streaming_ctrl,
2345             .frontend_attach  = cxusb_mygica_d689_frontend_attach,
2346             .tuner_attach     = cxusb_mygica_d689_tuner_attach,
2347 
2348             /* parameter for the MPEG2-data transfer */
2349             .stream = {
2350                 .type = USB_BULK,
2351                 .count = 5,
2352                 .endpoint = 0x02,
2353                 .u = {
2354                     .bulk = {
2355                         .buffersize = 8192,
2356                     }
2357                 }
2358             },
2359         } },
2360         },
2361     },
2362 
2363     .power_ctrl       = cxusb_d680_dmb_power_ctrl,
2364 
2365     .i2c_algo         = &cxusb_i2c_algo,
2366 
2367     .generic_bulk_ctrl_endpoint = 0x01,
2368 
2369     .rc.core = {
2370         .rc_interval    = 100,
2371         .rc_codes   = RC_MAP_D680_DMB,
2372         .module_name    = KBUILD_MODNAME,
2373         .rc_query       = cxusb_d680_dmb_rc_query,
2374         .allowed_protos = RC_PROTO_BIT_UNKNOWN,
2375     },
2376 
2377     .num_device_descs = 1,
2378     .devices = {
2379         {
2380             "Mygica D689 DMB-TH",
2381             { NULL },
2382             { &cxusb_table[MYGICA_D689], NULL },
2383         },
2384     }
2385 };
2386 
2387 static struct usb_driver cxusb_driver = {
2388     .name       = "dvb_usb_cxusb",
2389     .probe      = cxusb_probe,
2390     .disconnect     = cxusb_disconnect,
2391     .id_table   = cxusb_table,
2392 };
2393 
2394 module_usb_driver(cxusb_driver);
2395 
2396 MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@posteo.de>");
2397 MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
2398 MODULE_AUTHOR("Chris Pascoe <c.pascoe@itee.uq.edu.au>");
2399 MODULE_AUTHOR("Maciej S. Szmigiero <mail@maciej.szmigiero.name>");
2400 MODULE_DESCRIPTION("Driver for Conexant USB2.0 hybrid reference design");
2401 MODULE_LICENSE("GPL");