Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * DVB USB Linux driver for Anysee E30 DVB-C & DVB-T USB2.0 receiver
0004  *
0005  * Copyright (C) 2007 Antti Palosaari <crope@iki.fi>
0006  *
0007  * TODO:
0008  * - add smart card reader support for Conditional Access (CA)
0009  *
0010  * Card reader in Anysee is nothing more than ISO 7816 card reader.
0011  * There is no hardware CAM in any Anysee device sold.
0012  * In my understanding it should be implemented by making own module
0013  * for ISO 7816 card reader, like dvb_ca_en50221 is implemented. This
0014  * module registers serial interface that can be used to communicate
0015  * with any ISO 7816 smart card.
0016  *
0017  * Any help according to implement serial smart card reader support
0018  * is highly welcome!
0019  */
0020 
0021 #include "anysee.h"
0022 #include "dvb-pll.h"
0023 #include "tda1002x.h"
0024 #include "mt352.h"
0025 #include "mt352_priv.h"
0026 #include "zl10353.h"
0027 #include "tda18212.h"
0028 #include "cx24116.h"
0029 #include "stv0900.h"
0030 #include "stv6110.h"
0031 #include "isl6423.h"
0032 #include "cxd2820r.h"
0033 
0034 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
0035 
0036 static int anysee_ctrl_msg(struct dvb_usb_device *d,
0037         u8 *sbuf, u8 slen, u8 *rbuf, u8 rlen)
0038 {
0039     struct anysee_state *state = d_to_priv(d);
0040     int act_len, ret, i;
0041 
0042     mutex_lock(&d->usb_mutex);
0043 
0044     memcpy(&state->buf[0], sbuf, slen);
0045     state->buf[60] = state->seq++;
0046 
0047     dev_dbg(&d->udev->dev, "%s: >>> %*ph\n", __func__, slen, state->buf);
0048 
0049     /* We need receive one message more after dvb_usb_generic_rw due
0050        to weird transaction flow, which is 1 x send + 2 x receive. */
0051     ret = dvb_usbv2_generic_rw_locked(d, state->buf, sizeof(state->buf),
0052             state->buf, sizeof(state->buf));
0053     if (ret)
0054         goto error_unlock;
0055 
0056     /* TODO FIXME: dvb_usb_generic_rw() fails rarely with error code -32
0057      * (EPIPE, Broken pipe). Function supports currently msleep() as a
0058      * parameter but I would not like to use it, since according to
0059      * Documentation/timers/timers-howto.rst it should not be used such
0060      * short, under < 20ms, sleeps. Repeating failed message would be
0061      * better choice as not to add unwanted delays...
0062      * Fixing that correctly is one of those or both;
0063      * 1) use repeat if possible
0064      * 2) add suitable delay
0065      */
0066 
0067     /* get answer, retry few times if error returned */
0068     for (i = 0; i < 3; i++) {
0069         /* receive 2nd answer */
0070         ret = usb_bulk_msg(d->udev, usb_rcvbulkpipe(d->udev,
0071                 d->props->generic_bulk_ctrl_endpoint),
0072                 state->buf, sizeof(state->buf), &act_len, 2000);
0073         if (ret) {
0074             dev_dbg(&d->udev->dev,
0075                     "%s: recv bulk message failed=%d\n",
0076                     __func__, ret);
0077         } else {
0078             dev_dbg(&d->udev->dev, "%s: <<< %*ph\n", __func__,
0079                     rlen, state->buf);
0080 
0081             if (state->buf[63] != 0x4f)
0082                 dev_dbg(&d->udev->dev,
0083                         "%s: cmd failed\n", __func__);
0084             break;
0085         }
0086     }
0087 
0088     if (ret) {
0089         /* all retries failed, it is fatal */
0090         dev_err(&d->udev->dev, "%s: recv bulk message failed=%d\n",
0091                 KBUILD_MODNAME, ret);
0092         goto error_unlock;
0093     }
0094 
0095     /* read request, copy returned data to return buf */
0096     if (rbuf && rlen)
0097         memcpy(rbuf, state->buf, rlen);
0098 
0099 error_unlock:
0100     mutex_unlock(&d->usb_mutex);
0101     return ret;
0102 }
0103 
0104 static int anysee_read_reg(struct dvb_usb_device *d, u16 reg, u8 *val)
0105 {
0106     u8 buf[] = {CMD_REG_READ, reg >> 8, reg & 0xff, 0x01};
0107     int ret;
0108     ret = anysee_ctrl_msg(d, buf, sizeof(buf), val, 1);
0109     dev_dbg(&d->udev->dev, "%s: reg=%04x val=%02x\n", __func__, reg, *val);
0110     return ret;
0111 }
0112 
0113 static int anysee_write_reg(struct dvb_usb_device *d, u16 reg, u8 val)
0114 {
0115     u8 buf[] = {CMD_REG_WRITE, reg >> 8, reg & 0xff, 0x01, val};
0116     dev_dbg(&d->udev->dev, "%s: reg=%04x val=%02x\n", __func__, reg, val);
0117     return anysee_ctrl_msg(d, buf, sizeof(buf), NULL, 0);
0118 }
0119 
0120 /* write single register with mask */
0121 static int anysee_wr_reg_mask(struct dvb_usb_device *d, u16 reg, u8 val,
0122     u8 mask)
0123 {
0124     int ret;
0125     u8 tmp;
0126 
0127     /* no need for read if whole reg is written */
0128     if (mask != 0xff) {
0129         ret = anysee_read_reg(d, reg, &tmp);
0130         if (ret)
0131             return ret;
0132 
0133         val &= mask;
0134         tmp &= ~mask;
0135         val |= tmp;
0136     }
0137 
0138     return anysee_write_reg(d, reg, val);
0139 }
0140 
0141 /* read single register with mask */
0142 static int anysee_rd_reg_mask(struct dvb_usb_device *d, u16 reg, u8 *val,
0143     u8 mask)
0144 {
0145     int ret, i;
0146     u8 tmp;
0147 
0148     ret = anysee_read_reg(d, reg, &tmp);
0149     if (ret)
0150         return ret;
0151 
0152     tmp &= mask;
0153 
0154     /* find position of the first bit */
0155     for (i = 0; i < 8; i++) {
0156         if ((mask >> i) & 0x01)
0157             break;
0158     }
0159     *val = tmp >> i;
0160 
0161     return 0;
0162 }
0163 
0164 static int anysee_get_hw_info(struct dvb_usb_device *d, u8 *id)
0165 {
0166     u8 buf[] = {CMD_GET_HW_INFO};
0167     return anysee_ctrl_msg(d, buf, sizeof(buf), id, 3);
0168 }
0169 
0170 static int anysee_streaming_ctrl(struct dvb_frontend *fe, int onoff)
0171 {
0172     u8 buf[] = {CMD_STREAMING_CTRL, (u8)onoff, 0x00};
0173     dev_dbg(&fe_to_d(fe)->udev->dev, "%s: onoff=%d\n", __func__, onoff);
0174     return anysee_ctrl_msg(fe_to_d(fe), buf, sizeof(buf), NULL, 0);
0175 }
0176 
0177 static int anysee_led_ctrl(struct dvb_usb_device *d, u8 mode, u8 interval)
0178 {
0179     u8 buf[] = {CMD_LED_AND_IR_CTRL, 0x01, mode, interval};
0180     dev_dbg(&d->udev->dev, "%s: state=%d interval=%d\n", __func__,
0181             mode, interval);
0182     return anysee_ctrl_msg(d, buf, sizeof(buf), NULL, 0);
0183 }
0184 
0185 static int anysee_ir_ctrl(struct dvb_usb_device *d, u8 onoff)
0186 {
0187     u8 buf[] = {CMD_LED_AND_IR_CTRL, 0x02, onoff};
0188     dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff);
0189     return anysee_ctrl_msg(d, buf, sizeof(buf), NULL, 0);
0190 }
0191 
0192 /* I2C */
0193 static int anysee_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msg,
0194     int num)
0195 {
0196     struct dvb_usb_device *d = i2c_get_adapdata(adap);
0197     int ret = 0, inc, i = 0;
0198     u8 buf[52]; /* 4 + 48 (I2C WR USB command header + I2C WR max) */
0199 
0200     if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
0201         return -EAGAIN;
0202 
0203     while (i < num) {
0204         if (num > i + 1 && (msg[i+1].flags & I2C_M_RD)) {
0205             if (msg[i].len > 2 || msg[i+1].len > 60) {
0206                 ret = -EOPNOTSUPP;
0207                 break;
0208             }
0209             buf[0] = CMD_I2C_READ;
0210             buf[1] = (msg[i].addr << 1) | 0x01;
0211             buf[2] = msg[i].buf[0];
0212             buf[3] = msg[i].buf[1];
0213             buf[4] = msg[i].len-1;
0214             buf[5] = msg[i+1].len;
0215             ret = anysee_ctrl_msg(d, buf, 6, msg[i+1].buf,
0216                 msg[i+1].len);
0217             inc = 2;
0218         } else {
0219             if (msg[i].len > 48) {
0220                 ret = -EOPNOTSUPP;
0221                 break;
0222             }
0223             buf[0] = CMD_I2C_WRITE;
0224             buf[1] = (msg[i].addr << 1);
0225             buf[2] = msg[i].len;
0226             buf[3] = 0x01;
0227             memcpy(&buf[4], msg[i].buf, msg[i].len);
0228             ret = anysee_ctrl_msg(d, buf, 4 + msg[i].len, NULL, 0);
0229             inc = 1;
0230         }
0231         if (ret)
0232             break;
0233 
0234         i += inc;
0235     }
0236 
0237     mutex_unlock(&d->i2c_mutex);
0238 
0239     return ret ? ret : i;
0240 }
0241 
0242 static u32 anysee_i2c_func(struct i2c_adapter *adapter)
0243 {
0244     return I2C_FUNC_I2C;
0245 }
0246 
0247 static struct i2c_algorithm anysee_i2c_algo = {
0248     .master_xfer   = anysee_master_xfer,
0249     .functionality = anysee_i2c_func,
0250 };
0251 
0252 static int anysee_mt352_demod_init(struct dvb_frontend *fe)
0253 {
0254     static u8 clock_config[]   = { CLOCK_CTL,  0x38, 0x28 };
0255     static u8 reset[]          = { RESET,      0x80 };
0256     static u8 adc_ctl_1_cfg[]  = { ADC_CTL_1,  0x40 };
0257     static u8 agc_cfg[]        = { AGC_TARGET, 0x28, 0x20 };
0258     static u8 gpp_ctl_cfg[]    = { GPP_CTL,    0x33 };
0259     static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
0260 
0261     mt352_write(fe, clock_config,   sizeof(clock_config));
0262     udelay(200);
0263     mt352_write(fe, reset,          sizeof(reset));
0264     mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
0265 
0266     mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
0267     mt352_write(fe, gpp_ctl_cfg,    sizeof(gpp_ctl_cfg));
0268     mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
0269 
0270     return 0;
0271 }
0272 
0273 /* Callbacks for DVB USB */
0274 static struct tda10023_config anysee_tda10023_config = {
0275     .demod_address = (0x1a >> 1),
0276     .invert = 0,
0277     .xtal   = 16000000,
0278     .pll_m  = 11,
0279     .pll_p  = 3,
0280     .pll_n  = 1,
0281     .output_mode = TDA10023_OUTPUT_MODE_PARALLEL_C,
0282     .deltaf = 0xfeeb,
0283 };
0284 
0285 static struct mt352_config anysee_mt352_config = {
0286     .demod_address = (0x1e >> 1),
0287     .demod_init    = anysee_mt352_demod_init,
0288 };
0289 
0290 static struct zl10353_config anysee_zl10353_config = {
0291     .demod_address = (0x1e >> 1),
0292     .parallel_ts = 1,
0293 };
0294 
0295 static struct zl10353_config anysee_zl10353_tda18212_config2 = {
0296     .demod_address = (0x1e >> 1),
0297     .parallel_ts = 1,
0298     .disable_i2c_gate_ctrl = 1,
0299     .no_tuner = 1,
0300     .if2 = 41500,
0301 };
0302 
0303 static struct zl10353_config anysee_zl10353_tda18212_config = {
0304     .demod_address = (0x18 >> 1),
0305     .parallel_ts = 1,
0306     .disable_i2c_gate_ctrl = 1,
0307     .no_tuner = 1,
0308     .if2 = 41500,
0309 };
0310 
0311 static struct tda10023_config anysee_tda10023_tda18212_config = {
0312     .demod_address = (0x1a >> 1),
0313     .xtal   = 16000000,
0314     .pll_m  = 12,
0315     .pll_p  = 3,
0316     .pll_n  = 1,
0317     .output_mode = TDA10023_OUTPUT_MODE_PARALLEL_B,
0318     .deltaf = 0xba02,
0319 };
0320 
0321 static const struct tda18212_config anysee_tda18212_config = {
0322     .if_dvbt_6 = 4150,
0323     .if_dvbt_7 = 4150,
0324     .if_dvbt_8 = 4150,
0325     .if_dvbc = 5000,
0326 };
0327 
0328 static const struct tda18212_config anysee_tda18212_config2 = {
0329     .if_dvbt_6 = 3550,
0330     .if_dvbt_7 = 3700,
0331     .if_dvbt_8 = 4150,
0332     .if_dvbt2_6 = 3250,
0333     .if_dvbt2_7 = 4000,
0334     .if_dvbt2_8 = 4000,
0335     .if_dvbc = 5000,
0336 };
0337 
0338 static struct cx24116_config anysee_cx24116_config = {
0339     .demod_address = (0xaa >> 1),
0340     .mpg_clk_pos_pol = 0x00,
0341     .i2c_wr_max = 48,
0342 };
0343 
0344 static struct stv0900_config anysee_stv0900_config = {
0345     .demod_address = (0xd0 >> 1),
0346     .demod_mode = 0,
0347     .xtal = 8000000,
0348     .clkmode = 3,
0349     .diseqc_mode = 2,
0350     .tun1_maddress = 0,
0351     .tun1_adc = 1, /* 1 Vpp */
0352     .path1_mode = 3,
0353 };
0354 
0355 static struct stv6110_config anysee_stv6110_config = {
0356     .i2c_address = (0xc0 >> 1),
0357     .mclk = 16000000,
0358     .clk_div = 1,
0359 };
0360 
0361 static struct isl6423_config anysee_isl6423_config = {
0362     .current_max = SEC_CURRENT_800m,
0363     .curlim  = SEC_CURRENT_LIM_OFF,
0364     .mod_extern = 1,
0365     .addr = (0x10 >> 1),
0366 };
0367 
0368 static struct cxd2820r_config anysee_cxd2820r_config = {
0369     .i2c_address = 0x6d, /* (0xda >> 1) */
0370     .ts_mode = 0x38,
0371 };
0372 
0373 /*
0374  * New USB device strings: Mfr=1, Product=2, SerialNumber=0
0375  * Manufacturer: AMT.CO.KR
0376  *
0377  * E30 VID=04b4 PID=861f HW=2 FW=2.1 Product=????????
0378  * PCB: ?
0379  * parts: DNOS404ZH102A(MT352, DTT7579(?))
0380  *
0381  * E30 VID=04b4 PID=861f HW=2 FW=2.1 "anysee-T(LP)"
0382  * PCB: PCB 507T (rev1.61)
0383  * parts: DNOS404ZH103A(ZL10353, DTT7579(?))
0384  * OEA=0a OEB=00 OEC=00 OED=ff OEE=00
0385  * IOA=45 IOB=ff IOC=00 IOD=ff IOE=00
0386  *
0387  * E30 Plus VID=04b4 PID=861f HW=6 FW=1.0 "anysee"
0388  * PCB: 507CD (rev1.1)
0389  * parts: DNOS404ZH103A(ZL10353, DTT7579(?)), CST56I01
0390  * OEA=80 OEB=00 OEC=00 OED=ff OEE=fe
0391  * IOA=4f IOB=ff IOC=00 IOD=06 IOE=01
0392  * IOD[0] ZL10353 1=enabled
0393  * IOA[7] TS 0=enabled
0394  * tuner is not behind ZL10353 I2C-gate (no care if gate disabled or not)
0395  *
0396  * E30 C Plus VID=04b4 PID=861f HW=10 FW=1.0 "anysee-DC(LP)"
0397  * PCB: 507DC (rev0.2)
0398  * parts: TDA10023, DTOS403IH102B TM, CST56I01
0399  * OEA=80 OEB=00 OEC=00 OED=ff OEE=fe
0400  * IOA=4f IOB=ff IOC=00 IOD=26 IOE=01
0401  * IOD[0] TDA10023 1=enabled
0402  *
0403  * E30 S2 Plus VID=04b4 PID=861f HW=11 FW=0.1 "anysee-S2(LP)"
0404  * PCB: 507SI (rev2.1)
0405  * parts: BS2N10WCC01(CX24116, CX24118), ISL6423, TDA8024
0406  * OEA=80 OEB=00 OEC=ff OED=ff OEE=fe
0407  * IOA=4d IOB=ff IOC=00 IOD=26 IOE=01
0408  * IOD[0] CX24116 1=enabled
0409  *
0410  * E30 C Plus VID=1c73 PID=861f HW=15 FW=1.2 "anysee-FA(LP)"
0411  * PCB: 507FA (rev0.4)
0412  * parts: TDA10023, DTOS403IH102B TM, TDA8024
0413  * OEA=80 OEB=00 OEC=ff OED=ff OEE=ff
0414  * IOA=4d IOB=ff IOC=00 IOD=00 IOE=c0
0415  * IOD[5] TDA10023 1=enabled
0416  * IOE[0] tuner 1=enabled
0417  *
0418  * E30 Combo Plus VID=1c73 PID=861f HW=15 FW=1.2 "anysee-FA(LP)"
0419  * PCB: 507FA (rev1.1)
0420  * parts: ZL10353, TDA10023, DTOS403IH102B TM, TDA8024
0421  * OEA=80 OEB=00 OEC=ff OED=ff OEE=ff
0422  * IOA=4d IOB=ff IOC=00 IOD=00 IOE=c0
0423  * DVB-C:
0424  * IOD[5] TDA10023 1=enabled
0425  * IOE[0] tuner 1=enabled
0426  * DVB-T:
0427  * IOD[0] ZL10353 1=enabled
0428  * IOE[0] tuner 0=enabled
0429  * tuner is behind ZL10353 I2C-gate
0430  * tuner is behind TDA10023 I2C-gate
0431  *
0432  * E7 TC VID=1c73 PID=861f HW=18 FW=0.7 AMTCI=0.5 "anysee-E7TC(LP)"
0433  * PCB: 508TC (rev0.6)
0434  * parts: ZL10353, TDA10023, DNOD44CDH086A(TDA18212)
0435  * OEA=80 OEB=00 OEC=03 OED=f7 OEE=ff
0436  * IOA=4d IOB=00 IOC=cc IOD=48 IOE=e4
0437  * IOA[7] TS 1=enabled
0438  * IOE[4] TDA18212 1=enabled
0439  * DVB-C:
0440  * IOD[6] ZL10353 0=disabled
0441  * IOD[5] TDA10023 1=enabled
0442  * IOE[0] IF 1=enabled
0443  * DVB-T:
0444  * IOD[5] TDA10023 0=disabled
0445  * IOD[6] ZL10353 1=enabled
0446  * IOE[0] IF 0=enabled
0447  *
0448  * E7 S2 VID=1c73 PID=861f HW=19 FW=0.4 AMTCI=0.5 "anysee-E7S2(LP)"
0449  * PCB: 508S2 (rev0.7)
0450  * parts: DNBU10512IST(STV0903, STV6110), ISL6423
0451  * OEA=80 OEB=00 OEC=03 OED=f7 OEE=ff
0452  * IOA=4d IOB=00 IOC=c4 IOD=08 IOE=e4
0453  * IOA[7] TS 1=enabled
0454  * IOE[5] STV0903 1=enabled
0455  *
0456  * E7 T2C VID=1c73 PID=861f HW=20 FW=0.1 AMTCI=0.5 "anysee-E7T2C(LP)"
0457  * PCB: 508T2C (rev0.3)
0458  * parts: DNOQ44QCH106A(CXD2820R, TDA18212), TDA8024
0459  * OEA=80 OEB=00 OEC=03 OED=f7 OEE=ff
0460  * IOA=4d IOB=00 IOC=cc IOD=48 IOE=e4
0461  * IOA[7] TS 1=enabled
0462  * IOE[5] CXD2820R 1=enabled
0463  *
0464  * E7 PTC VID=1c73 PID=861f HW=21 FW=0.1 AMTCI=?? "anysee-E7PTC(LP)"
0465  * PCB: 508PTC (rev0.5)
0466  * parts: ZL10353, TDA10023, DNOD44CDH086A(TDA18212)
0467  * OEA=80 OEB=00 OEC=03 OED=f7 OEE=ff
0468  * IOA=4d IOB=00 IOC=cc IOD=48 IOE=e4
0469  * IOA[7] TS 1=enabled
0470  * IOE[4] TDA18212 1=enabled
0471  * DVB-C:
0472  * IOD[6] ZL10353 0=disabled
0473  * IOD[5] TDA10023 1=enabled
0474  * IOE[0] IF 1=enabled
0475  * DVB-T:
0476  * IOD[5] TDA10023 0=disabled
0477  * IOD[6] ZL10353 1=enabled
0478  * IOE[0] IF 0=enabled
0479  *
0480  * E7 PS2 VID=1c73 PID=861f HW=22 FW=0.1 AMTCI=?? "anysee-E7PS2(LP)"
0481  * PCB: 508PS2 (rev0.4)
0482  * parts: DNBU10512IST(STV0903, STV6110), ISL6423
0483  * OEA=80 OEB=00 OEC=03 OED=f7 OEE=ff
0484  * IOA=4d IOB=00 IOC=c4 IOD=08 IOE=e4
0485  * IOA[7] TS 1=enabled
0486  * IOE[5] STV0903 1=enabled
0487  */
0488 
0489 static int anysee_read_config(struct dvb_usb_device *d)
0490 {
0491     struct anysee_state *state = d_to_priv(d);
0492     int ret;
0493     u8 hw_info[3];
0494 
0495     /*
0496      * Check which hardware we have.
0497      * We must do this call two times to get reliable values (hw/fw bug).
0498      */
0499     ret = anysee_get_hw_info(d, hw_info);
0500     if (ret)
0501         goto error;
0502 
0503     ret = anysee_get_hw_info(d, hw_info);
0504     if (ret)
0505         goto error;
0506 
0507     /*
0508      * Meaning of these info bytes are guessed.
0509      */
0510     dev_info(&d->udev->dev, "%s: firmware version %d.%d hardware id %d\n",
0511             KBUILD_MODNAME, hw_info[1], hw_info[2], hw_info[0]);
0512 
0513     state->hw = hw_info[0];
0514 error:
0515     return ret;
0516 }
0517 
0518 /* external I2C gate used for DNOD44CDH086A(TDA18212) tuner module */
0519 static int anysee_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
0520 {
0521     /* enable / disable tuner access on IOE[4] */
0522     return anysee_wr_reg_mask(fe_to_d(fe), REG_IOE, (enable << 4), 0x10);
0523 }
0524 
0525 static int anysee_frontend_ctrl(struct dvb_frontend *fe, int onoff)
0526 {
0527     struct anysee_state *state = fe_to_priv(fe);
0528     struct dvb_usb_device *d = fe_to_d(fe);
0529     int ret;
0530     dev_dbg(&d->udev->dev, "%s: fe=%d onoff=%d\n", __func__, fe->id, onoff);
0531 
0532     /* no frontend sleep control */
0533     if (onoff == 0)
0534         return 0;
0535 
0536     switch (state->hw) {
0537     case ANYSEE_HW_507FA: /* 15 */
0538         /* E30 Combo Plus */
0539         /* E30 C Plus */
0540 
0541         if (fe->id == 0)  {
0542             /* disable DVB-T demod on IOD[0] */
0543             ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 0), 0x01);
0544             if (ret)
0545                 goto error;
0546 
0547             /* enable DVB-C demod on IOD[5] */
0548             ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 5), 0x20);
0549             if (ret)
0550                 goto error;
0551 
0552             /* enable DVB-C tuner on IOE[0] */
0553             ret = anysee_wr_reg_mask(d, REG_IOE, (1 << 0), 0x01);
0554             if (ret)
0555                 goto error;
0556         } else {
0557             /* disable DVB-C demod on IOD[5] */
0558             ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 5), 0x20);
0559             if (ret)
0560                 goto error;
0561 
0562             /* enable DVB-T demod on IOD[0] */
0563             ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 0), 0x01);
0564             if (ret)
0565                 goto error;
0566 
0567             /* enable DVB-T tuner on IOE[0] */
0568             ret = anysee_wr_reg_mask(d, REG_IOE, (0 << 0), 0x01);
0569             if (ret)
0570                 goto error;
0571         }
0572 
0573         break;
0574     case ANYSEE_HW_508TC: /* 18 */
0575     case ANYSEE_HW_508PTC: /* 21 */
0576         /* E7 TC */
0577         /* E7 PTC */
0578 
0579         if (fe->id == 0)  {
0580             /* disable DVB-T demod on IOD[6] */
0581             ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 6), 0x40);
0582             if (ret)
0583                 goto error;
0584 
0585             /* enable DVB-C demod on IOD[5] */
0586             ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 5), 0x20);
0587             if (ret)
0588                 goto error;
0589 
0590             /* enable IF route on IOE[0] */
0591             ret = anysee_wr_reg_mask(d, REG_IOE, (1 << 0), 0x01);
0592             if (ret)
0593                 goto error;
0594         } else {
0595             /* disable DVB-C demod on IOD[5] */
0596             ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 5), 0x20);
0597             if (ret)
0598                 goto error;
0599 
0600             /* enable DVB-T demod on IOD[6] */
0601             ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 6), 0x40);
0602             if (ret)
0603                 goto error;
0604 
0605             /* enable IF route on IOE[0] */
0606             ret = anysee_wr_reg_mask(d, REG_IOE, (0 << 0), 0x01);
0607             if (ret)
0608                 goto error;
0609         }
0610 
0611         break;
0612     default:
0613         ret = 0;
0614     }
0615 
0616 error:
0617     return ret;
0618 }
0619 
0620 static int anysee_add_i2c_dev(struct dvb_usb_device *d, const char *type,
0621         u8 addr, void *platform_data)
0622 {
0623     int ret, num;
0624     struct anysee_state *state = d_to_priv(d);
0625     struct i2c_client *client;
0626     struct i2c_adapter *adapter = &d->i2c_adap;
0627     struct i2c_board_info board_info = {
0628         .addr = addr,
0629         .platform_data = platform_data,
0630     };
0631 
0632     strscpy(board_info.type, type, I2C_NAME_SIZE);
0633 
0634     /* find first free client */
0635     for (num = 0; num < ANYSEE_I2C_CLIENT_MAX; num++) {
0636         if (state->i2c_client[num] == NULL)
0637             break;
0638     }
0639 
0640     dev_dbg(&d->udev->dev, "%s: num=%d\n", __func__, num);
0641 
0642     if (num == ANYSEE_I2C_CLIENT_MAX) {
0643         dev_err(&d->udev->dev, "%s: I2C client out of index\n",
0644                 KBUILD_MODNAME);
0645         ret = -ENODEV;
0646         goto err;
0647     }
0648 
0649     request_module("%s", board_info.type);
0650 
0651     /* register I2C device */
0652     client = i2c_new_client_device(adapter, &board_info);
0653     if (!i2c_client_has_driver(client)) {
0654         ret = -ENODEV;
0655         goto err;
0656     }
0657 
0658     /* increase I2C driver usage count */
0659     if (!try_module_get(client->dev.driver->owner)) {
0660         i2c_unregister_device(client);
0661         ret = -ENODEV;
0662         goto err;
0663     }
0664 
0665     state->i2c_client[num] = client;
0666     return 0;
0667 err:
0668     dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
0669     return ret;
0670 }
0671 
0672 static void anysee_del_i2c_dev(struct dvb_usb_device *d)
0673 {
0674     int num;
0675     struct anysee_state *state = d_to_priv(d);
0676     struct i2c_client *client;
0677 
0678     /* find last used client */
0679     num = ANYSEE_I2C_CLIENT_MAX;
0680     while (num--) {
0681         if (state->i2c_client[num] != NULL)
0682             break;
0683     }
0684 
0685     dev_dbg(&d->udev->dev, "%s: num=%d\n", __func__, num);
0686 
0687     if (num == -1) {
0688         dev_err(&d->udev->dev, "%s: I2C client out of index\n",
0689                 KBUILD_MODNAME);
0690         goto err;
0691     }
0692 
0693     client = state->i2c_client[num];
0694 
0695     /* decrease I2C driver usage count */
0696     module_put(client->dev.driver->owner);
0697 
0698     /* unregister I2C device */
0699     i2c_unregister_device(client);
0700 
0701     state->i2c_client[num] = NULL;
0702 err:
0703     dev_dbg(&d->udev->dev, "%s: failed\n", __func__);
0704 }
0705 
0706 static int anysee_frontend_attach(struct dvb_usb_adapter *adap)
0707 {
0708     struct anysee_state *state = adap_to_priv(adap);
0709     struct dvb_usb_device *d = adap_to_d(adap);
0710     int ret = 0;
0711     u8 tmp;
0712     struct i2c_msg msg[2] = {
0713         {
0714             .addr = 0x60,
0715             .flags = 0,
0716             .len = 1,
0717             .buf = "\x00",
0718         }, {
0719             .addr = 0x60,
0720             .flags = I2C_M_RD,
0721             .len = 1,
0722             .buf = &tmp,
0723         }
0724     };
0725 
0726     switch (state->hw) {
0727     case ANYSEE_HW_507T: /* 2 */
0728         /* E30 */
0729 
0730         /* attach demod */
0731         adap->fe[0] = dvb_attach(mt352_attach, &anysee_mt352_config,
0732                 &d->i2c_adap);
0733         if (adap->fe[0])
0734             break;
0735 
0736         /* attach demod */
0737         adap->fe[0] = dvb_attach(zl10353_attach, &anysee_zl10353_config,
0738                 &d->i2c_adap);
0739 
0740         break;
0741     case ANYSEE_HW_507CD: /* 6 */
0742         /* E30 Plus */
0743 
0744         /* enable DVB-T demod on IOD[0] */
0745         ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 0), 0x01);
0746         if (ret)
0747             goto error;
0748 
0749         /* enable transport stream on IOA[7] */
0750         ret = anysee_wr_reg_mask(d, REG_IOA, (0 << 7), 0x80);
0751         if (ret)
0752             goto error;
0753 
0754         /* attach demod */
0755         adap->fe[0] = dvb_attach(zl10353_attach, &anysee_zl10353_config,
0756                 &d->i2c_adap);
0757 
0758         break;
0759     case ANYSEE_HW_507DC: /* 10 */
0760         /* E30 C Plus */
0761 
0762         /* enable DVB-C demod on IOD[0] */
0763         ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 0), 0x01);
0764         if (ret)
0765             goto error;
0766 
0767         /* attach demod */
0768         adap->fe[0] = dvb_attach(tda10023_attach,
0769                 &anysee_tda10023_config, &d->i2c_adap, 0x48);
0770 
0771         break;
0772     case ANYSEE_HW_507SI: /* 11 */
0773         /* E30 S2 Plus */
0774 
0775         /* enable DVB-S/S2 demod on IOD[0] */
0776         ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 0), 0x01);
0777         if (ret)
0778             goto error;
0779 
0780         /* attach demod */
0781         adap->fe[0] = dvb_attach(cx24116_attach, &anysee_cx24116_config,
0782                 &d->i2c_adap);
0783 
0784         break;
0785     case ANYSEE_HW_507FA: /* 15 */
0786         /* E30 Combo Plus */
0787         /* E30 C Plus */
0788 
0789         /* enable tuner on IOE[4] */
0790         ret = anysee_wr_reg_mask(d, REG_IOE, (1 << 4), 0x10);
0791         if (ret)
0792             goto error;
0793 
0794         /* probe TDA18212 */
0795         tmp = 0;
0796         ret = i2c_transfer(&d->i2c_adap, msg, 2);
0797         if (ret == 2 && tmp == 0xc7) {
0798             dev_dbg(&d->udev->dev, "%s: TDA18212 found\n",
0799                     __func__);
0800             state->has_tda18212 = true;
0801         }
0802         else
0803             tmp = 0;
0804 
0805         /* disable tuner on IOE[4] */
0806         ret = anysee_wr_reg_mask(d, REG_IOE, (0 << 4), 0x10);
0807         if (ret)
0808             goto error;
0809 
0810         /* disable DVB-T demod on IOD[0] */
0811         ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 0), 0x01);
0812         if (ret)
0813             goto error;
0814 
0815         /* enable DVB-C demod on IOD[5] */
0816         ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 5), 0x20);
0817         if (ret)
0818             goto error;
0819 
0820         /* attach demod */
0821         if (tmp == 0xc7) {
0822             /* TDA18212 config */
0823             adap->fe[0] = dvb_attach(tda10023_attach,
0824                     &anysee_tda10023_tda18212_config,
0825                     &d->i2c_adap, 0x48);
0826 
0827             /* I2C gate for DNOD44CDH086A(TDA18212) tuner module */
0828             if (adap->fe[0])
0829                 adap->fe[0]->ops.i2c_gate_ctrl =
0830                         anysee_i2c_gate_ctrl;
0831         } else {
0832             /* PLL config */
0833             adap->fe[0] = dvb_attach(tda10023_attach,
0834                     &anysee_tda10023_config,
0835                     &d->i2c_adap, 0x48);
0836         }
0837 
0838         /* break out if first frontend attaching fails */
0839         if (!adap->fe[0])
0840             break;
0841 
0842         /* disable DVB-C demod on IOD[5] */
0843         ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 5), 0x20);
0844         if (ret)
0845             goto error;
0846 
0847         /* enable DVB-T demod on IOD[0] */
0848         ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 0), 0x01);
0849         if (ret)
0850             goto error;
0851 
0852         /* attach demod */
0853         if (tmp == 0xc7) {
0854             /* TDA18212 config */
0855             adap->fe[1] = dvb_attach(zl10353_attach,
0856                     &anysee_zl10353_tda18212_config2,
0857                     &d->i2c_adap);
0858 
0859             /* I2C gate for DNOD44CDH086A(TDA18212) tuner module */
0860             if (adap->fe[1])
0861                 adap->fe[1]->ops.i2c_gate_ctrl =
0862                         anysee_i2c_gate_ctrl;
0863         } else {
0864             /* PLL config */
0865             adap->fe[1] = dvb_attach(zl10353_attach,
0866                     &anysee_zl10353_config,
0867                     &d->i2c_adap);
0868         }
0869 
0870         break;
0871     case ANYSEE_HW_508TC: /* 18 */
0872     case ANYSEE_HW_508PTC: /* 21 */
0873         /* E7 TC */
0874         /* E7 PTC */
0875 
0876         /* disable DVB-T demod on IOD[6] */
0877         ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 6), 0x40);
0878         if (ret)
0879             goto error;
0880 
0881         /* enable DVB-C demod on IOD[5] */
0882         ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 5), 0x20);
0883         if (ret)
0884             goto error;
0885 
0886         /* attach demod */
0887         adap->fe[0] = dvb_attach(tda10023_attach,
0888                 &anysee_tda10023_tda18212_config,
0889                 &d->i2c_adap, 0x48);
0890 
0891         /* I2C gate for DNOD44CDH086A(TDA18212) tuner module */
0892         if (adap->fe[0])
0893             adap->fe[0]->ops.i2c_gate_ctrl = anysee_i2c_gate_ctrl;
0894 
0895         /* break out if first frontend attaching fails */
0896         if (!adap->fe[0])
0897             break;
0898 
0899         /* disable DVB-C demod on IOD[5] */
0900         ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 5), 0x20);
0901         if (ret)
0902             goto error;
0903 
0904         /* enable DVB-T demod on IOD[6] */
0905         ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 6), 0x40);
0906         if (ret)
0907             goto error;
0908 
0909         /* attach demod */
0910         adap->fe[1] = dvb_attach(zl10353_attach,
0911                 &anysee_zl10353_tda18212_config,
0912                 &d->i2c_adap);
0913 
0914         /* I2C gate for DNOD44CDH086A(TDA18212) tuner module */
0915         if (adap->fe[1])
0916             adap->fe[1]->ops.i2c_gate_ctrl = anysee_i2c_gate_ctrl;
0917 
0918         state->has_ci = true;
0919 
0920         break;
0921     case ANYSEE_HW_508S2: /* 19 */
0922     case ANYSEE_HW_508PS2: /* 22 */
0923         /* E7 S2 */
0924         /* E7 PS2 */
0925 
0926         /* enable DVB-S/S2 demod on IOE[5] */
0927         ret = anysee_wr_reg_mask(d, REG_IOE, (1 << 5), 0x20);
0928         if (ret)
0929             goto error;
0930 
0931         /* attach demod */
0932         adap->fe[0] = dvb_attach(stv0900_attach,
0933                 &anysee_stv0900_config, &d->i2c_adap, 0);
0934 
0935         state->has_ci = true;
0936 
0937         break;
0938     case ANYSEE_HW_508T2C: /* 20 */
0939         /* E7 T2C */
0940 
0941         /* enable DVB-T/T2/C demod on IOE[5] */
0942         ret = anysee_wr_reg_mask(d, REG_IOE, (1 << 5), 0x20);
0943         if (ret)
0944             goto error;
0945 
0946         /* attach demod */
0947         adap->fe[0] = dvb_attach(cxd2820r_attach,
0948                 &anysee_cxd2820r_config, &d->i2c_adap, NULL);
0949 
0950         state->has_ci = true;
0951 
0952         break;
0953     }
0954 
0955     if (!adap->fe[0]) {
0956         /* we have no frontend :-( */
0957         ret = -ENODEV;
0958         dev_err(&d->udev->dev,
0959                 "%s: Unsupported Anysee version. Please report to <linux-media@vger.kernel.org>.\n",
0960                 KBUILD_MODNAME);
0961     }
0962 error:
0963     return ret;
0964 }
0965 
0966 static int anysee_tuner_attach(struct dvb_usb_adapter *adap)
0967 {
0968     struct anysee_state *state = adap_to_priv(adap);
0969     struct dvb_usb_device *d = adap_to_d(adap);
0970     struct dvb_frontend *fe;
0971     int ret;
0972     dev_dbg(&d->udev->dev, "%s:\n", __func__);
0973 
0974     switch (state->hw) {
0975     case ANYSEE_HW_507T: /* 2 */
0976         /* E30 */
0977 
0978         /* attach tuner */
0979         fe = dvb_attach(dvb_pll_attach, adap->fe[0], (0xc2 >> 1), NULL,
0980                 DVB_PLL_THOMSON_DTT7579);
0981 
0982         break;
0983     case ANYSEE_HW_507CD: /* 6 */
0984         /* E30 Plus */
0985 
0986         /* attach tuner */
0987         fe = dvb_attach(dvb_pll_attach, adap->fe[0], (0xc2 >> 1),
0988                 &d->i2c_adap, DVB_PLL_THOMSON_DTT7579);
0989 
0990         break;
0991     case ANYSEE_HW_507DC: /* 10 */
0992         /* E30 C Plus */
0993 
0994         /* attach tuner */
0995         fe = dvb_attach(dvb_pll_attach, adap->fe[0], (0xc0 >> 1),
0996                 &d->i2c_adap, DVB_PLL_SAMSUNG_DTOS403IH102A);
0997 
0998         break;
0999     case ANYSEE_HW_507SI: /* 11 */
1000         /* E30 S2 Plus */
1001 
1002         /* attach LNB controller */
1003         fe = dvb_attach(isl6423_attach, adap->fe[0], &d->i2c_adap,
1004                 &anysee_isl6423_config);
1005 
1006         break;
1007     case ANYSEE_HW_507FA: /* 15 */
1008         /* E30 Combo Plus */
1009         /* E30 C Plus */
1010 
1011         /* Try first attach TDA18212 silicon tuner on IOE[4], if that
1012          * fails attach old simple PLL. */
1013 
1014         /* attach tuner */
1015         if (state->has_tda18212) {
1016             struct tda18212_config tda18212_config =
1017                     anysee_tda18212_config;
1018 
1019             tda18212_config.fe = adap->fe[0];
1020             ret = anysee_add_i2c_dev(d, "tda18212", 0x60,
1021                     &tda18212_config);
1022             if (ret)
1023                 goto err;
1024 
1025             /* copy tuner ops for 2nd FE as tuner is shared */
1026             if (adap->fe[1]) {
1027                 adap->fe[1]->tuner_priv =
1028                         adap->fe[0]->tuner_priv;
1029                 memcpy(&adap->fe[1]->ops.tuner_ops,
1030                         &adap->fe[0]->ops.tuner_ops,
1031                         sizeof(struct dvb_tuner_ops));
1032             }
1033 
1034             return 0;
1035         } else {
1036             /* attach tuner */
1037             fe = dvb_attach(dvb_pll_attach, adap->fe[0],
1038                     (0xc0 >> 1), &d->i2c_adap,
1039                     DVB_PLL_SAMSUNG_DTOS403IH102A);
1040 
1041             if (fe && adap->fe[1]) {
1042                 /* attach tuner for 2nd FE */
1043                 fe = dvb_attach(dvb_pll_attach, adap->fe[1],
1044                         (0xc0 >> 1), &d->i2c_adap,
1045                         DVB_PLL_SAMSUNG_DTOS403IH102A);
1046             }
1047         }
1048 
1049         break;
1050     case ANYSEE_HW_508TC: /* 18 */
1051     case ANYSEE_HW_508PTC: /* 21 */
1052     {
1053         /* E7 TC */
1054         /* E7 PTC */
1055         struct tda18212_config tda18212_config = anysee_tda18212_config;
1056 
1057         tda18212_config.fe = adap->fe[0];
1058         ret = anysee_add_i2c_dev(d, "tda18212", 0x60, &tda18212_config);
1059         if (ret)
1060             goto err;
1061 
1062         /* copy tuner ops for 2nd FE as tuner is shared */
1063         if (adap->fe[1]) {
1064             adap->fe[1]->tuner_priv = adap->fe[0]->tuner_priv;
1065             memcpy(&adap->fe[1]->ops.tuner_ops,
1066                     &adap->fe[0]->ops.tuner_ops,
1067                     sizeof(struct dvb_tuner_ops));
1068         }
1069 
1070         return 0;
1071     }
1072     case ANYSEE_HW_508S2: /* 19 */
1073     case ANYSEE_HW_508PS2: /* 22 */
1074         /* E7 S2 */
1075         /* E7 PS2 */
1076 
1077         /* attach tuner */
1078         fe = dvb_attach(stv6110_attach, adap->fe[0],
1079                 &anysee_stv6110_config, &d->i2c_adap);
1080 
1081         if (fe) {
1082             /* attach LNB controller */
1083             fe = dvb_attach(isl6423_attach, adap->fe[0],
1084                     &d->i2c_adap, &anysee_isl6423_config);
1085         }
1086 
1087         break;
1088 
1089     case ANYSEE_HW_508T2C: /* 20 */
1090     {
1091         /* E7 T2C */
1092         struct tda18212_config tda18212_config =
1093                 anysee_tda18212_config2;
1094 
1095         tda18212_config.fe = adap->fe[0];
1096         ret = anysee_add_i2c_dev(d, "tda18212", 0x60, &tda18212_config);
1097         if (ret)
1098             goto err;
1099 
1100         return 0;
1101     }
1102     default:
1103         fe = NULL;
1104     }
1105 
1106     if (fe)
1107         ret = 0;
1108     else
1109         ret = -ENODEV;
1110 err:
1111     return ret;
1112 }
1113 
1114 #if IS_ENABLED(CONFIG_RC_CORE)
1115 static int anysee_rc_query(struct dvb_usb_device *d)
1116 {
1117     u8 buf[] = {CMD_GET_IR_CODE};
1118     u8 ircode[2];
1119     int ret;
1120 
1121     /* Remote controller is basic NEC using address byte 0x08.
1122        Anysee device RC query returns only two bytes, status and code,
1123        address byte is dropped. Also it does not return any value for
1124        NEC RCs having address byte other than 0x08. Due to that, we
1125        cannot use that device as standard NEC receiver.
1126        It could be possible make hack which reads whole code directly
1127        from device memory... */
1128 
1129     ret = anysee_ctrl_msg(d, buf, sizeof(buf), ircode, sizeof(ircode));
1130     if (ret)
1131         return ret;
1132 
1133     if (ircode[0]) {
1134         dev_dbg(&d->udev->dev, "%s: key pressed %02x\n", __func__,
1135                 ircode[1]);
1136         rc_keydown(d->rc_dev, RC_PROTO_NEC,
1137                RC_SCANCODE_NEC(0x08, ircode[1]), 0);
1138     }
1139 
1140     return 0;
1141 }
1142 
1143 static int anysee_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
1144 {
1145     rc->allowed_protos = RC_PROTO_BIT_NEC;
1146     rc->query          = anysee_rc_query;
1147     rc->interval       = 250;  /* windows driver uses 500ms */
1148 
1149     return 0;
1150 }
1151 #else
1152     #define anysee_get_rc_config NULL
1153 #endif
1154 
1155 static int anysee_ci_read_attribute_mem(struct dvb_ca_en50221 *ci, int slot,
1156     int addr)
1157 {
1158     struct dvb_usb_device *d = ci->data;
1159     int ret;
1160     u8 buf[] = {CMD_CI, 0x02, 0x40 | addr >> 8, addr & 0xff, 0x00, 1};
1161     u8 val;
1162 
1163     ret = anysee_ctrl_msg(d, buf, sizeof(buf), &val, 1);
1164     if (ret)
1165         return ret;
1166 
1167     return val;
1168 }
1169 
1170 static int anysee_ci_write_attribute_mem(struct dvb_ca_en50221 *ci, int slot,
1171     int addr, u8 val)
1172 {
1173     struct dvb_usb_device *d = ci->data;
1174     u8 buf[] = {CMD_CI, 0x03, 0x40 | addr >> 8, addr & 0xff, 0x00, 1, val};
1175 
1176     return anysee_ctrl_msg(d, buf, sizeof(buf), NULL, 0);
1177 }
1178 
1179 static int anysee_ci_read_cam_control(struct dvb_ca_en50221 *ci, int slot,
1180     u8 addr)
1181 {
1182     struct dvb_usb_device *d = ci->data;
1183     int ret;
1184     u8 buf[] = {CMD_CI, 0x04, 0x40, addr, 0x00, 1};
1185     u8 val;
1186 
1187     ret = anysee_ctrl_msg(d, buf, sizeof(buf), &val, 1);
1188     if (ret)
1189         return ret;
1190 
1191     return val;
1192 }
1193 
1194 static int anysee_ci_write_cam_control(struct dvb_ca_en50221 *ci, int slot,
1195     u8 addr, u8 val)
1196 {
1197     struct dvb_usb_device *d = ci->data;
1198     u8 buf[] = {CMD_CI, 0x05, 0x40, addr, 0x00, 1, val};
1199 
1200     return anysee_ctrl_msg(d, buf, sizeof(buf), NULL, 0);
1201 }
1202 
1203 static int anysee_ci_slot_reset(struct dvb_ca_en50221 *ci, int slot)
1204 {
1205     struct dvb_usb_device *d = ci->data;
1206     int ret;
1207     struct anysee_state *state = d_to_priv(d);
1208 
1209     state->ci_cam_ready = jiffies + msecs_to_jiffies(1000);
1210 
1211     ret = anysee_wr_reg_mask(d, REG_IOA, (0 << 7), 0x80);
1212     if (ret)
1213         return ret;
1214 
1215     msleep(300);
1216 
1217     ret = anysee_wr_reg_mask(d, REG_IOA, (1 << 7), 0x80);
1218     if (ret)
1219         return ret;
1220 
1221     return 0;
1222 }
1223 
1224 static int anysee_ci_slot_shutdown(struct dvb_ca_en50221 *ci, int slot)
1225 {
1226     struct dvb_usb_device *d = ci->data;
1227     int ret;
1228 
1229     ret = anysee_wr_reg_mask(d, REG_IOA, (0 << 7), 0x80);
1230     if (ret)
1231         return ret;
1232 
1233     msleep(30);
1234 
1235     ret = anysee_wr_reg_mask(d, REG_IOA, (1 << 7), 0x80);
1236     if (ret)
1237         return ret;
1238 
1239     return 0;
1240 }
1241 
1242 static int anysee_ci_slot_ts_enable(struct dvb_ca_en50221 *ci, int slot)
1243 {
1244     struct dvb_usb_device *d = ci->data;
1245 
1246     return anysee_wr_reg_mask(d, REG_IOD, (0 << 1), 0x02);
1247 }
1248 
1249 static int anysee_ci_poll_slot_status(struct dvb_ca_en50221 *ci, int slot,
1250     int open)
1251 {
1252     struct dvb_usb_device *d = ci->data;
1253     struct anysee_state *state = d_to_priv(d);
1254     int ret;
1255     u8 tmp = 0;
1256 
1257     ret = anysee_rd_reg_mask(d, REG_IOC, &tmp, 0x40);
1258     if (ret)
1259         return ret;
1260 
1261     if (tmp == 0) {
1262         ret = DVB_CA_EN50221_POLL_CAM_PRESENT;
1263         if (time_after(jiffies, state->ci_cam_ready))
1264             ret |= DVB_CA_EN50221_POLL_CAM_READY;
1265     }
1266 
1267     return ret;
1268 }
1269 
1270 static int anysee_ci_init(struct dvb_usb_device *d)
1271 {
1272     struct anysee_state *state = d_to_priv(d);
1273     int ret;
1274 
1275     state->ci.owner               = THIS_MODULE;
1276     state->ci.read_attribute_mem  = anysee_ci_read_attribute_mem;
1277     state->ci.write_attribute_mem = anysee_ci_write_attribute_mem;
1278     state->ci.read_cam_control    = anysee_ci_read_cam_control;
1279     state->ci.write_cam_control   = anysee_ci_write_cam_control;
1280     state->ci.slot_reset          = anysee_ci_slot_reset;
1281     state->ci.slot_shutdown       = anysee_ci_slot_shutdown;
1282     state->ci.slot_ts_enable      = anysee_ci_slot_ts_enable;
1283     state->ci.poll_slot_status    = anysee_ci_poll_slot_status;
1284     state->ci.data                = d;
1285 
1286     ret = anysee_wr_reg_mask(d, REG_IOA, (1 << 7), 0x80);
1287     if (ret)
1288         return ret;
1289 
1290     ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 2)|(0 << 1)|(0 << 0), 0x07);
1291     if (ret)
1292         return ret;
1293 
1294     ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 2)|(1 << 1)|(1 << 0), 0x07);
1295     if (ret)
1296         return ret;
1297 
1298     ret = dvb_ca_en50221_init(&d->adapter[0].dvb_adap, &state->ci, 0, 1);
1299     if (ret)
1300         return ret;
1301 
1302     state->ci_attached = true;
1303 
1304     return 0;
1305 }
1306 
1307 static void anysee_ci_release(struct dvb_usb_device *d)
1308 {
1309     struct anysee_state *state = d_to_priv(d);
1310 
1311     /* detach CI */
1312     if (state->ci_attached)
1313         dvb_ca_en50221_release(&state->ci);
1314 
1315     return;
1316 }
1317 
1318 static int anysee_init(struct dvb_usb_device *d)
1319 {
1320     struct anysee_state *state = d_to_priv(d);
1321     int ret;
1322 
1323     /* There is one interface with two alternate settings.
1324        Alternate setting 0 is for bulk transfer.
1325        Alternate setting 1 is for isochronous transfer.
1326        We use bulk transfer (alternate setting 0). */
1327     ret = usb_set_interface(d->udev, 0, 0);
1328     if (ret)
1329         return ret;
1330 
1331     /* LED light */
1332     ret = anysee_led_ctrl(d, 0x01, 0x03);
1333     if (ret)
1334         return ret;
1335 
1336     /* enable IR */
1337     ret = anysee_ir_ctrl(d, 1);
1338     if (ret)
1339         return ret;
1340 
1341     /* attach CI */
1342     if (state->has_ci) {
1343         ret = anysee_ci_init(d);
1344         if (ret)
1345             return ret;
1346     }
1347 
1348     return 0;
1349 }
1350 
1351 static void anysee_exit(struct dvb_usb_device *d)
1352 {
1353     struct anysee_state *state = d_to_priv(d);
1354 
1355     if (state->i2c_client[0])
1356         anysee_del_i2c_dev(d);
1357 
1358     return anysee_ci_release(d);
1359 }
1360 
1361 /* DVB USB Driver stuff */
1362 static struct dvb_usb_device_properties anysee_props = {
1363     .driver_name = KBUILD_MODNAME,
1364     .owner = THIS_MODULE,
1365     .adapter_nr = adapter_nr,
1366     .size_of_priv = sizeof(struct anysee_state),
1367 
1368     .generic_bulk_ctrl_endpoint = 0x01,
1369     .generic_bulk_ctrl_endpoint_response = 0x81,
1370 
1371     .i2c_algo         = &anysee_i2c_algo,
1372     .read_config      = anysee_read_config,
1373     .frontend_attach  = anysee_frontend_attach,
1374     .tuner_attach     = anysee_tuner_attach,
1375     .init             = anysee_init,
1376     .get_rc_config    = anysee_get_rc_config,
1377     .frontend_ctrl    = anysee_frontend_ctrl,
1378     .streaming_ctrl   = anysee_streaming_ctrl,
1379     .exit             = anysee_exit,
1380 
1381     .num_adapters = 1,
1382     .adapter = {
1383         {
1384             .stream = DVB_USB_STREAM_BULK(0x82, 8, 16 * 512),
1385         }
1386     }
1387 };
1388 
1389 static const struct usb_device_id anysee_id_table[] = {
1390     { DVB_USB_DEVICE(USB_VID_CYPRESS, USB_PID_ANYSEE,
1391         &anysee_props, "Anysee", RC_MAP_ANYSEE) },
1392     { DVB_USB_DEVICE(USB_VID_AMT, USB_PID_ANYSEE,
1393         &anysee_props, "Anysee", RC_MAP_ANYSEE) },
1394     { }
1395 };
1396 MODULE_DEVICE_TABLE(usb, anysee_id_table);
1397 
1398 static struct usb_driver anysee_usb_driver = {
1399     .name = KBUILD_MODNAME,
1400     .id_table = anysee_id_table,
1401     .probe = dvb_usbv2_probe,
1402     .disconnect = dvb_usbv2_disconnect,
1403     .suspend = dvb_usbv2_suspend,
1404     .resume = dvb_usbv2_resume,
1405     .reset_resume = dvb_usbv2_reset_resume,
1406     .no_dynamic_id = 1,
1407     .soft_unbind = 1,
1408 };
1409 
1410 module_usb_driver(anysee_usb_driver);
1411 
1412 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1413 MODULE_DESCRIPTION("Driver Anysee E30 DVB-C & DVB-T USB2.0");
1414 MODULE_LICENSE("GPL");