0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
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
0050
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
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068 for (i = 0; i < 3; i++) {
0069
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
0090 dev_err(&d->udev->dev, "%s: recv bulk message failed=%d\n",
0091 KBUILD_MODNAME, ret);
0092 goto error_unlock;
0093 }
0094
0095
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
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
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
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
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
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];
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
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,
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,
0370 .ts_mode = 0x38,
0371 };
0372
0373
0374
0375
0376
0377
0378
0379
0380
0381
0382
0383
0384
0385
0386
0387
0388
0389
0390
0391
0392
0393
0394
0395
0396
0397
0398
0399
0400
0401
0402
0403
0404
0405
0406
0407
0408
0409
0410
0411
0412
0413
0414
0415
0416
0417
0418
0419
0420
0421
0422
0423
0424
0425
0426
0427
0428
0429
0430
0431
0432
0433
0434
0435
0436
0437
0438
0439
0440
0441
0442
0443
0444
0445
0446
0447
0448
0449
0450
0451
0452
0453
0454
0455
0456
0457
0458
0459
0460
0461
0462
0463
0464
0465
0466
0467
0468
0469
0470
0471
0472
0473
0474
0475
0476
0477
0478
0479
0480
0481
0482
0483
0484
0485
0486
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
0497
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
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
0519 static int anysee_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
0520 {
0521
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
0533 if (onoff == 0)
0534 return 0;
0535
0536 switch (state->hw) {
0537 case ANYSEE_HW_507FA:
0538
0539
0540
0541 if (fe->id == 0) {
0542
0543 ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 0), 0x01);
0544 if (ret)
0545 goto error;
0546
0547
0548 ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 5), 0x20);
0549 if (ret)
0550 goto error;
0551
0552
0553 ret = anysee_wr_reg_mask(d, REG_IOE, (1 << 0), 0x01);
0554 if (ret)
0555 goto error;
0556 } else {
0557
0558 ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 5), 0x20);
0559 if (ret)
0560 goto error;
0561
0562
0563 ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 0), 0x01);
0564 if (ret)
0565 goto error;
0566
0567
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:
0575 case ANYSEE_HW_508PTC:
0576
0577
0578
0579 if (fe->id == 0) {
0580
0581 ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 6), 0x40);
0582 if (ret)
0583 goto error;
0584
0585
0586 ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 5), 0x20);
0587 if (ret)
0588 goto error;
0589
0590
0591 ret = anysee_wr_reg_mask(d, REG_IOE, (1 << 0), 0x01);
0592 if (ret)
0593 goto error;
0594 } else {
0595
0596 ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 5), 0x20);
0597 if (ret)
0598 goto error;
0599
0600
0601 ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 6), 0x40);
0602 if (ret)
0603 goto error;
0604
0605
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
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
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
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
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
0696 module_put(client->dev.driver->owner);
0697
0698
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:
0728
0729
0730
0731 adap->fe[0] = dvb_attach(mt352_attach, &anysee_mt352_config,
0732 &d->i2c_adap);
0733 if (adap->fe[0])
0734 break;
0735
0736
0737 adap->fe[0] = dvb_attach(zl10353_attach, &anysee_zl10353_config,
0738 &d->i2c_adap);
0739
0740 break;
0741 case ANYSEE_HW_507CD:
0742
0743
0744
0745 ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 0), 0x01);
0746 if (ret)
0747 goto error;
0748
0749
0750 ret = anysee_wr_reg_mask(d, REG_IOA, (0 << 7), 0x80);
0751 if (ret)
0752 goto error;
0753
0754
0755 adap->fe[0] = dvb_attach(zl10353_attach, &anysee_zl10353_config,
0756 &d->i2c_adap);
0757
0758 break;
0759 case ANYSEE_HW_507DC:
0760
0761
0762
0763 ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 0), 0x01);
0764 if (ret)
0765 goto error;
0766
0767
0768 adap->fe[0] = dvb_attach(tda10023_attach,
0769 &anysee_tda10023_config, &d->i2c_adap, 0x48);
0770
0771 break;
0772 case ANYSEE_HW_507SI:
0773
0774
0775
0776 ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 0), 0x01);
0777 if (ret)
0778 goto error;
0779
0780
0781 adap->fe[0] = dvb_attach(cx24116_attach, &anysee_cx24116_config,
0782 &d->i2c_adap);
0783
0784 break;
0785 case ANYSEE_HW_507FA:
0786
0787
0788
0789
0790 ret = anysee_wr_reg_mask(d, REG_IOE, (1 << 4), 0x10);
0791 if (ret)
0792 goto error;
0793
0794
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
0806 ret = anysee_wr_reg_mask(d, REG_IOE, (0 << 4), 0x10);
0807 if (ret)
0808 goto error;
0809
0810
0811 ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 0), 0x01);
0812 if (ret)
0813 goto error;
0814
0815
0816 ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 5), 0x20);
0817 if (ret)
0818 goto error;
0819
0820
0821 if (tmp == 0xc7) {
0822
0823 adap->fe[0] = dvb_attach(tda10023_attach,
0824 &anysee_tda10023_tda18212_config,
0825 &d->i2c_adap, 0x48);
0826
0827
0828 if (adap->fe[0])
0829 adap->fe[0]->ops.i2c_gate_ctrl =
0830 anysee_i2c_gate_ctrl;
0831 } else {
0832
0833 adap->fe[0] = dvb_attach(tda10023_attach,
0834 &anysee_tda10023_config,
0835 &d->i2c_adap, 0x48);
0836 }
0837
0838
0839 if (!adap->fe[0])
0840 break;
0841
0842
0843 ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 5), 0x20);
0844 if (ret)
0845 goto error;
0846
0847
0848 ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 0), 0x01);
0849 if (ret)
0850 goto error;
0851
0852
0853 if (tmp == 0xc7) {
0854
0855 adap->fe[1] = dvb_attach(zl10353_attach,
0856 &anysee_zl10353_tda18212_config2,
0857 &d->i2c_adap);
0858
0859
0860 if (adap->fe[1])
0861 adap->fe[1]->ops.i2c_gate_ctrl =
0862 anysee_i2c_gate_ctrl;
0863 } else {
0864
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:
0872 case ANYSEE_HW_508PTC:
0873
0874
0875
0876
0877 ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 6), 0x40);
0878 if (ret)
0879 goto error;
0880
0881
0882 ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 5), 0x20);
0883 if (ret)
0884 goto error;
0885
0886
0887 adap->fe[0] = dvb_attach(tda10023_attach,
0888 &anysee_tda10023_tda18212_config,
0889 &d->i2c_adap, 0x48);
0890
0891
0892 if (adap->fe[0])
0893 adap->fe[0]->ops.i2c_gate_ctrl = anysee_i2c_gate_ctrl;
0894
0895
0896 if (!adap->fe[0])
0897 break;
0898
0899
0900 ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 5), 0x20);
0901 if (ret)
0902 goto error;
0903
0904
0905 ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 6), 0x40);
0906 if (ret)
0907 goto error;
0908
0909
0910 adap->fe[1] = dvb_attach(zl10353_attach,
0911 &anysee_zl10353_tda18212_config,
0912 &d->i2c_adap);
0913
0914
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:
0922 case ANYSEE_HW_508PS2:
0923
0924
0925
0926
0927 ret = anysee_wr_reg_mask(d, REG_IOE, (1 << 5), 0x20);
0928 if (ret)
0929 goto error;
0930
0931
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:
0939
0940
0941
0942 ret = anysee_wr_reg_mask(d, REG_IOE, (1 << 5), 0x20);
0943 if (ret)
0944 goto error;
0945
0946
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
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:
0976
0977
0978
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:
0984
0985
0986
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:
0992
0993
0994
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:
1000
1001
1002
1003 fe = dvb_attach(isl6423_attach, adap->fe[0], &d->i2c_adap,
1004 &anysee_isl6423_config);
1005
1006 break;
1007 case ANYSEE_HW_507FA:
1008
1009
1010
1011
1012
1013
1014
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
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
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
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:
1051 case ANYSEE_HW_508PTC:
1052 {
1053
1054
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
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:
1073 case ANYSEE_HW_508PS2:
1074
1075
1076
1077
1078 fe = dvb_attach(stv6110_attach, adap->fe[0],
1079 &anysee_stv6110_config, &d->i2c_adap);
1080
1081 if (fe) {
1082
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:
1090 {
1091
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
1122
1123
1124
1125
1126
1127
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;
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
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
1324
1325
1326
1327 ret = usb_set_interface(d->udev, 0, 0);
1328 if (ret)
1329 return ret;
1330
1331
1332 ret = anysee_led_ctrl(d, 0x01, 0x03);
1333 if (ret)
1334 return ret;
1335
1336
1337 ret = anysee_ir_ctrl(d, 1);
1338 if (ret)
1339 return ret;
1340
1341
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
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");