linux/drivers/media/usb/dvb-usb-v2/anysee.c
<<
>>
Prefs
   1/*
   2 * DVB USB Linux driver for Anysee E30 DVB-C & DVB-T USB2.0 receiver
   3 *
   4 * Copyright (C) 2007 Antti Palosaari <crope@iki.fi>
   5 *
   6 *    This program is free software; you can redistribute it and/or modify
   7 *    it under the terms of the GNU General Public License as published by
   8 *    the Free Software Foundation; either version 2 of the License, or
   9 *    (at your option) any later version.
  10 *
  11 *    This program is distributed in the hope that it will be useful,
  12 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14 *    GNU General Public License for more details.
  15 *
  16 * TODO:
  17 * - add smart card reader support for Conditional Access (CA)
  18 *
  19 * Card reader in Anysee is nothing more than ISO 7816 card reader.
  20 * There is no hardware CAM in any Anysee device sold.
  21 * In my understanding it should be implemented by making own module
  22 * for ISO 7816 card reader, like dvb_ca_en50221 is implemented. This
  23 * module registers serial interface that can be used to communicate
  24 * with any ISO 7816 smart card.
  25 *
  26 * Any help according to implement serial smart card reader support
  27 * is highly welcome!
  28 */
  29
  30#include "anysee.h"
  31#include "dvb-pll.h"
  32#include "tda1002x.h"
  33#include "mt352.h"
  34#include "mt352_priv.h"
  35#include "zl10353.h"
  36#include "tda18212.h"
  37#include "cx24116.h"
  38#include "stv0900.h"
  39#include "stv6110.h"
  40#include "isl6423.h"
  41#include "cxd2820r.h"
  42
  43DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  44
  45static int anysee_ctrl_msg(struct dvb_usb_device *d,
  46                u8 *sbuf, u8 slen, u8 *rbuf, u8 rlen)
  47{
  48        struct anysee_state *state = d_to_priv(d);
  49        int act_len, ret, i;
  50
  51        mutex_lock(&d->usb_mutex);
  52
  53        memcpy(&state->buf[0], sbuf, slen);
  54        state->buf[60] = state->seq++;
  55
  56        dev_dbg(&d->udev->dev, "%s: >>> %*ph\n", __func__, slen, state->buf);
  57
  58        /* We need receive one message more after dvb_usb_generic_rw due
  59           to weird transaction flow, which is 1 x send + 2 x receive. */
  60        ret = dvb_usbv2_generic_rw_locked(d, state->buf, sizeof(state->buf),
  61                        state->buf, sizeof(state->buf));
  62        if (ret)
  63                goto error_unlock;
  64
  65        /* TODO FIXME: dvb_usb_generic_rw() fails rarely with error code -32
  66         * (EPIPE, Broken pipe). Function supports currently msleep() as a
  67         * parameter but I would not like to use it, since according to
  68         * Documentation/timers/timers-howto.txt it should not be used such
  69         * short, under < 20ms, sleeps. Repeating failed message would be
  70         * better choice as not to add unwanted delays...
  71         * Fixing that correctly is one of those or both;
  72         * 1) use repeat if possible
  73         * 2) add suitable delay
  74         */
  75
  76        /* get answer, retry few times if error returned */
  77        for (i = 0; i < 3; i++) {
  78                /* receive 2nd answer */
  79                ret = usb_bulk_msg(d->udev, usb_rcvbulkpipe(d->udev,
  80                                d->props->generic_bulk_ctrl_endpoint),
  81                                state->buf, sizeof(state->buf), &act_len, 2000);
  82                if (ret) {
  83                        dev_dbg(&d->udev->dev,
  84                                        "%s: recv bulk message failed=%d\n",
  85                                        __func__, ret);
  86                } else {
  87                        dev_dbg(&d->udev->dev, "%s: <<< %*ph\n", __func__,
  88                                        rlen, state->buf);
  89
  90                        if (state->buf[63] != 0x4f)
  91                                dev_dbg(&d->udev->dev,
  92                                                "%s: cmd failed\n", __func__);
  93                        break;
  94                }
  95        }
  96
  97        if (ret) {
  98                /* all retries failed, it is fatal */
  99                dev_err(&d->udev->dev, "%s: recv bulk message failed=%d\n",
 100                                KBUILD_MODNAME, ret);
 101                goto error_unlock;
 102        }
 103
 104        /* read request, copy returned data to return buf */
 105        if (rbuf && rlen)
 106                memcpy(rbuf, state->buf, rlen);
 107
 108error_unlock:
 109        mutex_unlock(&d->usb_mutex);
 110        return ret;
 111}
 112
 113static int anysee_read_reg(struct dvb_usb_device *d, u16 reg, u8 *val)
 114{
 115        u8 buf[] = {CMD_REG_READ, reg >> 8, reg & 0xff, 0x01};
 116        int ret;
 117        ret = anysee_ctrl_msg(d, buf, sizeof(buf), val, 1);
 118        dev_dbg(&d->udev->dev, "%s: reg=%04x val=%02x\n", __func__, reg, *val);
 119        return ret;
 120}
 121
 122static int anysee_write_reg(struct dvb_usb_device *d, u16 reg, u8 val)
 123{
 124        u8 buf[] = {CMD_REG_WRITE, reg >> 8, reg & 0xff, 0x01, val};
 125        dev_dbg(&d->udev->dev, "%s: reg=%04x val=%02x\n", __func__, reg, val);
 126        return anysee_ctrl_msg(d, buf, sizeof(buf), NULL, 0);
 127}
 128
 129/* write single register with mask */
 130static int anysee_wr_reg_mask(struct dvb_usb_device *d, u16 reg, u8 val,
 131        u8 mask)
 132{
 133        int ret;
 134        u8 tmp;
 135
 136        /* no need for read if whole reg is written */
 137        if (mask != 0xff) {
 138                ret = anysee_read_reg(d, reg, &tmp);
 139                if (ret)
 140                        return ret;
 141
 142                val &= mask;
 143                tmp &= ~mask;
 144                val |= tmp;
 145        }
 146
 147        return anysee_write_reg(d, reg, val);
 148}
 149
 150/* read single register with mask */
 151static int anysee_rd_reg_mask(struct dvb_usb_device *d, u16 reg, u8 *val,
 152        u8 mask)
 153{
 154        int ret, i;
 155        u8 tmp;
 156
 157        ret = anysee_read_reg(d, reg, &tmp);
 158        if (ret)
 159                return ret;
 160
 161        tmp &= mask;
 162
 163        /* find position of the first bit */
 164        for (i = 0; i < 8; i++) {
 165                if ((mask >> i) & 0x01)
 166                        break;
 167        }
 168        *val = tmp >> i;
 169
 170        return 0;
 171}
 172
 173static int anysee_get_hw_info(struct dvb_usb_device *d, u8 *id)
 174{
 175        u8 buf[] = {CMD_GET_HW_INFO};
 176        return anysee_ctrl_msg(d, buf, sizeof(buf), id, 3);
 177}
 178
 179static int anysee_streaming_ctrl(struct dvb_frontend *fe, int onoff)
 180{
 181        u8 buf[] = {CMD_STREAMING_CTRL, (u8)onoff, 0x00};
 182        dev_dbg(&fe_to_d(fe)->udev->dev, "%s: onoff=%d\n", __func__, onoff);
 183        return anysee_ctrl_msg(fe_to_d(fe), buf, sizeof(buf), NULL, 0);
 184}
 185
 186static int anysee_led_ctrl(struct dvb_usb_device *d, u8 mode, u8 interval)
 187{
 188        u8 buf[] = {CMD_LED_AND_IR_CTRL, 0x01, mode, interval};
 189        dev_dbg(&d->udev->dev, "%s: state=%d interval=%d\n", __func__,
 190                        mode, interval);
 191        return anysee_ctrl_msg(d, buf, sizeof(buf), NULL, 0);
 192}
 193
 194static int anysee_ir_ctrl(struct dvb_usb_device *d, u8 onoff)
 195{
 196        u8 buf[] = {CMD_LED_AND_IR_CTRL, 0x02, onoff};
 197        dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff);
 198        return anysee_ctrl_msg(d, buf, sizeof(buf), NULL, 0);
 199}
 200
 201/* I2C */
 202static int anysee_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msg,
 203        int num)
 204{
 205        struct dvb_usb_device *d = i2c_get_adapdata(adap);
 206        int ret = 0, inc, i = 0;
 207        u8 buf[52]; /* 4 + 48 (I2C WR USB command header + I2C WR max) */
 208
 209        if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
 210                return -EAGAIN;
 211
 212        while (i < num) {
 213                if (num > i + 1 && (msg[i+1].flags & I2C_M_RD)) {
 214                        if (msg[i].len > 2 || msg[i+1].len > 60) {
 215                                ret = -EOPNOTSUPP;
 216                                break;
 217                        }
 218                        buf[0] = CMD_I2C_READ;
 219                        buf[1] = (msg[i].addr << 1) | 0x01;
 220                        buf[2] = msg[i].buf[0];
 221                        buf[3] = msg[i].buf[1];
 222                        buf[4] = msg[i].len-1;
 223                        buf[5] = msg[i+1].len;
 224                        ret = anysee_ctrl_msg(d, buf, 6, msg[i+1].buf,
 225                                msg[i+1].len);
 226                        inc = 2;
 227                } else {
 228                        if (msg[i].len > 48) {
 229                                ret = -EOPNOTSUPP;
 230                                break;
 231                        }
 232                        buf[0] = CMD_I2C_WRITE;
 233                        buf[1] = (msg[i].addr << 1);
 234                        buf[2] = msg[i].len;
 235                        buf[3] = 0x01;
 236                        memcpy(&buf[4], msg[i].buf, msg[i].len);
 237                        ret = anysee_ctrl_msg(d, buf, 4 + msg[i].len, NULL, 0);
 238                        inc = 1;
 239                }
 240                if (ret)
 241                        break;
 242
 243                i += inc;
 244        }
 245
 246        mutex_unlock(&d->i2c_mutex);
 247
 248        return ret ? ret : i;
 249}
 250
 251static u32 anysee_i2c_func(struct i2c_adapter *adapter)
 252{
 253        return I2C_FUNC_I2C;
 254}
 255
 256static struct i2c_algorithm anysee_i2c_algo = {
 257        .master_xfer   = anysee_master_xfer,
 258        .functionality = anysee_i2c_func,
 259};
 260
 261static int anysee_mt352_demod_init(struct dvb_frontend *fe)
 262{
 263        static u8 clock_config[]   = { CLOCK_CTL,  0x38, 0x28 };
 264        static u8 reset[]          = { RESET,      0x80 };
 265        static u8 adc_ctl_1_cfg[]  = { ADC_CTL_1,  0x40 };
 266        static u8 agc_cfg[]        = { AGC_TARGET, 0x28, 0x20 };
 267        static u8 gpp_ctl_cfg[]    = { GPP_CTL,    0x33 };
 268        static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
 269
 270        mt352_write(fe, clock_config,   sizeof(clock_config));
 271        udelay(200);
 272        mt352_write(fe, reset,          sizeof(reset));
 273        mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
 274
 275        mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
 276        mt352_write(fe, gpp_ctl_cfg,    sizeof(gpp_ctl_cfg));
 277        mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
 278
 279        return 0;
 280}
 281
 282/* Callbacks for DVB USB */
 283static struct tda10023_config anysee_tda10023_config = {
 284        .demod_address = (0x1a >> 1),
 285        .invert = 0,
 286        .xtal   = 16000000,
 287        .pll_m  = 11,
 288        .pll_p  = 3,
 289        .pll_n  = 1,
 290        .output_mode = TDA10023_OUTPUT_MODE_PARALLEL_C,
 291        .deltaf = 0xfeeb,
 292};
 293
 294static struct mt352_config anysee_mt352_config = {
 295        .demod_address = (0x1e >> 1),
 296        .demod_init    = anysee_mt352_demod_init,
 297};
 298
 299static struct zl10353_config anysee_zl10353_config = {
 300        .demod_address = (0x1e >> 1),
 301        .parallel_ts = 1,
 302};
 303
 304static struct zl10353_config anysee_zl10353_tda18212_config2 = {
 305        .demod_address = (0x1e >> 1),
 306        .parallel_ts = 1,
 307        .disable_i2c_gate_ctrl = 1,
 308        .no_tuner = 1,
 309        .if2 = 41500,
 310};
 311
 312static struct zl10353_config anysee_zl10353_tda18212_config = {
 313        .demod_address = (0x18 >> 1),
 314        .parallel_ts = 1,
 315        .disable_i2c_gate_ctrl = 1,
 316        .no_tuner = 1,
 317        .if2 = 41500,
 318};
 319
 320static struct tda10023_config anysee_tda10023_tda18212_config = {
 321        .demod_address = (0x1a >> 1),
 322        .xtal   = 16000000,
 323        .pll_m  = 12,
 324        .pll_p  = 3,
 325        .pll_n  = 1,
 326        .output_mode = TDA10023_OUTPUT_MODE_PARALLEL_B,
 327        .deltaf = 0xba02,
 328};
 329
 330static struct tda18212_config anysee_tda18212_config = {
 331        .if_dvbt_6 = 4150,
 332        .if_dvbt_7 = 4150,
 333        .if_dvbt_8 = 4150,
 334        .if_dvbc = 5000,
 335};
 336
 337static struct tda18212_config anysee_tda18212_config2 = {
 338        .if_dvbt_6 = 3550,
 339        .if_dvbt_7 = 3700,
 340        .if_dvbt_8 = 4150,
 341        .if_dvbt2_6 = 3250,
 342        .if_dvbt2_7 = 4000,
 343        .if_dvbt2_8 = 4000,
 344        .if_dvbc = 5000,
 345};
 346
 347static struct cx24116_config anysee_cx24116_config = {
 348        .demod_address = (0xaa >> 1),
 349        .mpg_clk_pos_pol = 0x00,
 350        .i2c_wr_max = 48,
 351};
 352
 353static struct stv0900_config anysee_stv0900_config = {
 354        .demod_address = (0xd0 >> 1),
 355        .demod_mode = 0,
 356        .xtal = 8000000,
 357        .clkmode = 3,
 358        .diseqc_mode = 2,
 359        .tun1_maddress = 0,
 360        .tun1_adc = 1, /* 1 Vpp */
 361        .path1_mode = 3,
 362};
 363
 364static struct stv6110_config anysee_stv6110_config = {
 365        .i2c_address = (0xc0 >> 1),
 366        .mclk = 16000000,
 367        .clk_div = 1,
 368};
 369
 370static struct isl6423_config anysee_isl6423_config = {
 371        .current_max = SEC_CURRENT_800m,
 372        .curlim  = SEC_CURRENT_LIM_OFF,
 373        .mod_extern = 1,
 374        .addr = (0x10 >> 1),
 375};
 376
 377static struct cxd2820r_config anysee_cxd2820r_config = {
 378        .i2c_address = 0x6d, /* (0xda >> 1) */
 379        .ts_mode = 0x38,
 380};
 381
 382/*
 383 * New USB device strings: Mfr=1, Product=2, SerialNumber=0
 384 * Manufacturer: AMT.CO.KR
 385 *
 386 * E30 VID=04b4 PID=861f HW=2 FW=2.1 Product=????????
 387 * PCB: ?
 388 * parts: DNOS404ZH102A(MT352, DTT7579(?))
 389 *
 390 * E30 VID=04b4 PID=861f HW=2 FW=2.1 "anysee-T(LP)"
 391 * PCB: PCB 507T (rev1.61)
 392 * parts: DNOS404ZH103A(ZL10353, DTT7579(?))
 393 * OEA=0a OEB=00 OEC=00 OED=ff OEE=00
 394 * IOA=45 IOB=ff IOC=00 IOD=ff IOE=00
 395 *
 396 * E30 Plus VID=04b4 PID=861f HW=6 FW=1.0 "anysee"
 397 * PCB: 507CD (rev1.1)
 398 * parts: DNOS404ZH103A(ZL10353, DTT7579(?)), CST56I01
 399 * OEA=80 OEB=00 OEC=00 OED=ff OEE=fe
 400 * IOA=4f IOB=ff IOC=00 IOD=06 IOE=01
 401 * IOD[0] ZL10353 1=enabled
 402 * IOA[7] TS 0=enabled
 403 * tuner is not behind ZL10353 I2C-gate (no care if gate disabled or not)
 404 *
 405 * E30 C Plus VID=04b4 PID=861f HW=10 FW=1.0 "anysee-DC(LP)"
 406 * PCB: 507DC (rev0.2)
 407 * parts: TDA10023, DTOS403IH102B TM, CST56I01
 408 * OEA=80 OEB=00 OEC=00 OED=ff OEE=fe
 409 * IOA=4f IOB=ff IOC=00 IOD=26 IOE=01
 410 * IOD[0] TDA10023 1=enabled
 411 *
 412 * E30 S2 Plus VID=04b4 PID=861f HW=11 FW=0.1 "anysee-S2(LP)"
 413 * PCB: 507SI (rev2.1)
 414 * parts: BS2N10WCC01(CX24116, CX24118), ISL6423, TDA8024
 415 * OEA=80 OEB=00 OEC=ff OED=ff OEE=fe
 416 * IOA=4d IOB=ff IOC=00 IOD=26 IOE=01
 417 * IOD[0] CX24116 1=enabled
 418 *
 419 * E30 C Plus VID=1c73 PID=861f HW=15 FW=1.2 "anysee-FA(LP)"
 420 * PCB: 507FA (rev0.4)
 421 * parts: TDA10023, DTOS403IH102B TM, TDA8024
 422 * OEA=80 OEB=00 OEC=ff OED=ff OEE=ff
 423 * IOA=4d IOB=ff IOC=00 IOD=00 IOE=c0
 424 * IOD[5] TDA10023 1=enabled
 425 * IOE[0] tuner 1=enabled
 426 *
 427 * E30 Combo Plus VID=1c73 PID=861f HW=15 FW=1.2 "anysee-FA(LP)"
 428 * PCB: 507FA (rev1.1)
 429 * parts: ZL10353, TDA10023, DTOS403IH102B TM, TDA8024
 430 * OEA=80 OEB=00 OEC=ff OED=ff OEE=ff
 431 * IOA=4d IOB=ff IOC=00 IOD=00 IOE=c0
 432 * DVB-C:
 433 * IOD[5] TDA10023 1=enabled
 434 * IOE[0] tuner 1=enabled
 435 * DVB-T:
 436 * IOD[0] ZL10353 1=enabled
 437 * IOE[0] tuner 0=enabled
 438 * tuner is behind ZL10353 I2C-gate
 439 * tuner is behind TDA10023 I2C-gate
 440 *
 441 * E7 TC VID=1c73 PID=861f HW=18 FW=0.7 AMTCI=0.5 "anysee-E7TC(LP)"
 442 * PCB: 508TC (rev0.6)
 443 * parts: ZL10353, TDA10023, DNOD44CDH086A(TDA18212)
 444 * OEA=80 OEB=00 OEC=03 OED=f7 OEE=ff
 445 * IOA=4d IOB=00 IOC=cc IOD=48 IOE=e4
 446 * IOA[7] TS 1=enabled
 447 * IOE[4] TDA18212 1=enabled
 448 * DVB-C:
 449 * IOD[6] ZL10353 0=disabled
 450 * IOD[5] TDA10023 1=enabled
 451 * IOE[0] IF 1=enabled
 452 * DVB-T:
 453 * IOD[5] TDA10023 0=disabled
 454 * IOD[6] ZL10353 1=enabled
 455 * IOE[0] IF 0=enabled
 456 *
 457 * E7 S2 VID=1c73 PID=861f HW=19 FW=0.4 AMTCI=0.5 "anysee-E7S2(LP)"
 458 * PCB: 508S2 (rev0.7)
 459 * parts: DNBU10512IST(STV0903, STV6110), ISL6423
 460 * OEA=80 OEB=00 OEC=03 OED=f7 OEE=ff
 461 * IOA=4d IOB=00 IOC=c4 IOD=08 IOE=e4
 462 * IOA[7] TS 1=enabled
 463 * IOE[5] STV0903 1=enabled
 464 *
 465 * E7 T2C VID=1c73 PID=861f HW=20 FW=0.1 AMTCI=0.5 "anysee-E7T2C(LP)"
 466 * PCB: 508T2C (rev0.3)
 467 * parts: DNOQ44QCH106A(CXD2820R, TDA18212), TDA8024
 468 * OEA=80 OEB=00 OEC=03 OED=f7 OEE=ff
 469 * IOA=4d IOB=00 IOC=cc IOD=48 IOE=e4
 470 * IOA[7] TS 1=enabled
 471 * IOE[5] CXD2820R 1=enabled
 472 *
 473 * E7 PTC VID=1c73 PID=861f HW=21 FW=0.1 AMTCI=?? "anysee-E7PTC(LP)"
 474 * PCB: 508PTC (rev0.5)
 475 * parts: ZL10353, TDA10023, DNOD44CDH086A(TDA18212)
 476 * OEA=80 OEB=00 OEC=03 OED=f7 OEE=ff
 477 * IOA=4d IOB=00 IOC=cc IOD=48 IOE=e4
 478 * IOA[7] TS 1=enabled
 479 * IOE[4] TDA18212 1=enabled
 480 * DVB-C:
 481 * IOD[6] ZL10353 0=disabled
 482 * IOD[5] TDA10023 1=enabled
 483 * IOE[0] IF 1=enabled
 484 * DVB-T:
 485 * IOD[5] TDA10023 0=disabled
 486 * IOD[6] ZL10353 1=enabled
 487 * IOE[0] IF 0=enabled
 488 *
 489 * E7 PS2 VID=1c73 PID=861f HW=22 FW=0.1 AMTCI=?? "anysee-E7PS2(LP)"
 490 * PCB: 508PS2 (rev0.4)
 491 * parts: DNBU10512IST(STV0903, STV6110), ISL6423
 492 * OEA=80 OEB=00 OEC=03 OED=f7 OEE=ff
 493 * IOA=4d IOB=00 IOC=c4 IOD=08 IOE=e4
 494 * IOA[7] TS 1=enabled
 495 * IOE[5] STV0903 1=enabled
 496 */
 497
 498static int anysee_read_config(struct dvb_usb_device *d)
 499{
 500        struct anysee_state *state = d_to_priv(d);
 501        int ret;
 502        u8 hw_info[3];
 503
 504        /*
 505         * Check which hardware we have.
 506         * We must do this call two times to get reliable values (hw/fw bug).
 507         */
 508        ret = anysee_get_hw_info(d, hw_info);
 509        if (ret)
 510                goto error;
 511
 512        ret = anysee_get_hw_info(d, hw_info);
 513        if (ret)
 514                goto error;
 515
 516        /*
 517         * Meaning of these info bytes are guessed.
 518         */
 519        dev_info(&d->udev->dev, "%s: firmware version %d.%d hardware id %d\n",
 520                        KBUILD_MODNAME, hw_info[1], hw_info[2], hw_info[0]);
 521
 522        state->hw = hw_info[0];
 523error:
 524        return ret;
 525}
 526
 527/* external I2C gate used for DNOD44CDH086A(TDA18212) tuner module */
 528static int anysee_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
 529{
 530        /* enable / disable tuner access on IOE[4] */
 531        return anysee_wr_reg_mask(fe_to_d(fe), REG_IOE, (enable << 4), 0x10);
 532}
 533
 534static int anysee_frontend_ctrl(struct dvb_frontend *fe, int onoff)
 535{
 536        struct anysee_state *state = fe_to_priv(fe);
 537        struct dvb_usb_device *d = fe_to_d(fe);
 538        int ret;
 539        dev_dbg(&d->udev->dev, "%s: fe=%d onoff=%d\n", __func__, fe->id, onoff);
 540
 541        /* no frontend sleep control */
 542        if (onoff == 0)
 543                return 0;
 544
 545        switch (state->hw) {
 546        case ANYSEE_HW_507FA: /* 15 */
 547                /* E30 Combo Plus */
 548                /* E30 C Plus */
 549
 550                if (fe->id == 0)  {
 551                        /* disable DVB-T demod on IOD[0] */
 552                        ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 0), 0x01);
 553                        if (ret)
 554                                goto error;
 555
 556                        /* enable DVB-C demod on IOD[5] */
 557                        ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 5), 0x20);
 558                        if (ret)
 559                                goto error;
 560
 561                        /* enable DVB-C tuner on IOE[0] */
 562                        ret = anysee_wr_reg_mask(d, REG_IOE, (1 << 0), 0x01);
 563                        if (ret)
 564                                goto error;
 565                } else {
 566                        /* disable DVB-C demod on IOD[5] */
 567                        ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 5), 0x20);
 568                        if (ret)
 569                                goto error;
 570
 571                        /* enable DVB-T demod on IOD[0] */
 572                        ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 0), 0x01);
 573                        if (ret)
 574                                goto error;
 575
 576                        /* enable DVB-T tuner on IOE[0] */
 577                        ret = anysee_wr_reg_mask(d, REG_IOE, (0 << 0), 0x01);
 578                        if (ret)
 579                                goto error;
 580                }
 581
 582                break;
 583        case ANYSEE_HW_508TC: /* 18 */
 584        case ANYSEE_HW_508PTC: /* 21 */
 585                /* E7 TC */
 586                /* E7 PTC */
 587
 588                if (fe->id == 0)  {
 589                        /* disable DVB-T demod on IOD[6] */
 590                        ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 6), 0x40);
 591                        if (ret)
 592                                goto error;
 593
 594                        /* enable DVB-C demod on IOD[5] */
 595                        ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 5), 0x20);
 596                        if (ret)
 597                                goto error;
 598
 599                        /* enable IF route on IOE[0] */
 600                        ret = anysee_wr_reg_mask(d, REG_IOE, (1 << 0), 0x01);
 601                        if (ret)
 602                                goto error;
 603                } else {
 604                        /* disable DVB-C demod on IOD[5] */
 605                        ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 5), 0x20);
 606                        if (ret)
 607                                goto error;
 608
 609                        /* enable DVB-T demod on IOD[6] */
 610                        ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 6), 0x40);
 611                        if (ret)
 612                                goto error;
 613
 614                        /* enable IF route on IOE[0] */
 615                        ret = anysee_wr_reg_mask(d, REG_IOE, (0 << 0), 0x01);
 616                        if (ret)
 617                                goto error;
 618                }
 619
 620                break;
 621        default:
 622                ret = 0;
 623        }
 624
 625error:
 626        return ret;
 627}
 628
 629static int anysee_add_i2c_dev(struct dvb_usb_device *d, const char *type,
 630                u8 addr, void *platform_data)
 631{
 632        int ret, num;
 633        struct anysee_state *state = d_to_priv(d);
 634        struct i2c_client *client;
 635        struct i2c_adapter *adapter = &d->i2c_adap;
 636        struct i2c_board_info board_info = {
 637                .addr = addr,
 638                .platform_data = platform_data,
 639        };
 640
 641        strlcpy(board_info.type, type, I2C_NAME_SIZE);
 642
 643        /* find first free client */
 644        for (num = 0; num < ANYSEE_I2C_CLIENT_MAX; num++) {
 645                if (state->i2c_client[num] == NULL)
 646                        break;
 647        }
 648
 649        dev_dbg(&d->udev->dev, "%s: num=%d\n", __func__, num);
 650
 651        if (num == ANYSEE_I2C_CLIENT_MAX) {
 652                dev_err(&d->udev->dev, "%s: I2C client out of index\n",
 653                                KBUILD_MODNAME);
 654                ret = -ENODEV;
 655                goto err;
 656        }
 657
 658        request_module("%s", board_info.type);
 659
 660        /* register I2C device */
 661        client = i2c_new_device(adapter, &board_info);
 662        if (client == NULL || client->dev.driver == NULL) {
 663                ret = -ENODEV;
 664                goto err;
 665        }
 666
 667        /* increase I2C driver usage count */
 668        if (!try_module_get(client->dev.driver->owner)) {
 669                i2c_unregister_device(client);
 670                ret = -ENODEV;
 671                goto err;
 672        }
 673
 674        state->i2c_client[num] = client;
 675        return 0;
 676err:
 677        dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
 678        return ret;
 679}
 680
 681static void anysee_del_i2c_dev(struct dvb_usb_device *d)
 682{
 683        int num;
 684        struct anysee_state *state = d_to_priv(d);
 685        struct i2c_client *client;
 686
 687        /* find last used client */
 688        num = ANYSEE_I2C_CLIENT_MAX;
 689        while (num--) {
 690                if (state->i2c_client[num] != NULL)
 691                        break;
 692        }
 693
 694        dev_dbg(&d->udev->dev, "%s: num=%d\n", __func__, num);
 695
 696        if (num == -1) {
 697                dev_err(&d->udev->dev, "%s: I2C client out of index\n",
 698                                KBUILD_MODNAME);
 699                goto err;
 700        }
 701
 702        client = state->i2c_client[num];
 703
 704        /* decrease I2C driver usage count */
 705        module_put(client->dev.driver->owner);
 706
 707        /* unregister I2C device */
 708        i2c_unregister_device(client);
 709
 710        state->i2c_client[num] = NULL;
 711err:
 712        dev_dbg(&d->udev->dev, "%s: failed\n", __func__);
 713}
 714
 715static int anysee_frontend_attach(struct dvb_usb_adapter *adap)
 716{
 717        struct anysee_state *state = adap_to_priv(adap);
 718        struct dvb_usb_device *d = adap_to_d(adap);
 719        int ret = 0;
 720        u8 tmp;
 721        struct i2c_msg msg[2] = {
 722                {
 723                        .addr = 0x60,
 724                        .flags = 0,
 725                        .len = 1,
 726                        .buf = "\x00",
 727                }, {
 728                        .addr = 0x60,
 729                        .flags = I2C_M_RD,
 730                        .len = 1,
 731                        .buf = &tmp,
 732                }
 733        };
 734
 735        switch (state->hw) {
 736        case ANYSEE_HW_507T: /* 2 */
 737                /* E30 */
 738
 739                /* attach demod */
 740                adap->fe[0] = dvb_attach(mt352_attach, &anysee_mt352_config,
 741                                &d->i2c_adap);
 742                if (adap->fe[0])
 743                        break;
 744
 745                /* attach demod */
 746                adap->fe[0] = dvb_attach(zl10353_attach, &anysee_zl10353_config,
 747                                &d->i2c_adap);
 748
 749                break;
 750        case ANYSEE_HW_507CD: /* 6 */
 751                /* E30 Plus */
 752
 753                /* enable DVB-T demod on IOD[0] */
 754                ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 0), 0x01);
 755                if (ret)
 756                        goto error;
 757
 758                /* enable transport stream on IOA[7] */
 759                ret = anysee_wr_reg_mask(d, REG_IOA, (0 << 7), 0x80);
 760                if (ret)
 761                        goto error;
 762
 763                /* attach demod */
 764                adap->fe[0] = dvb_attach(zl10353_attach, &anysee_zl10353_config,
 765                                &d->i2c_adap);
 766
 767                break;
 768        case ANYSEE_HW_507DC: /* 10 */
 769                /* E30 C Plus */
 770
 771                /* enable DVB-C demod on IOD[0] */
 772                ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 0), 0x01);
 773                if (ret)
 774                        goto error;
 775
 776                /* attach demod */
 777                adap->fe[0] = dvb_attach(tda10023_attach,
 778                                &anysee_tda10023_config, &d->i2c_adap, 0x48);
 779
 780                break;
 781        case ANYSEE_HW_507SI: /* 11 */
 782                /* E30 S2 Plus */
 783
 784                /* enable DVB-S/S2 demod on IOD[0] */
 785                ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 0), 0x01);
 786                if (ret)
 787                        goto error;
 788
 789                /* attach demod */
 790                adap->fe[0] = dvb_attach(cx24116_attach, &anysee_cx24116_config,
 791                                &d->i2c_adap);
 792
 793                break;
 794        case ANYSEE_HW_507FA: /* 15 */
 795                /* E30 Combo Plus */
 796                /* E30 C Plus */
 797
 798                /* enable tuner on IOE[4] */
 799                ret = anysee_wr_reg_mask(d, REG_IOE, (1 << 4), 0x10);
 800                if (ret)
 801                        goto error;
 802
 803                /* probe TDA18212 */
 804                tmp = 0;
 805                ret = i2c_transfer(&d->i2c_adap, msg, 2);
 806                if (ret == 2 && tmp == 0xc7) {
 807                        dev_dbg(&d->udev->dev, "%s: TDA18212 found\n",
 808                                        __func__);
 809                        state->has_tda18212 = true;
 810                }
 811                else
 812                        tmp = 0;
 813
 814                /* disable tuner on IOE[4] */
 815                ret = anysee_wr_reg_mask(d, REG_IOE, (0 << 4), 0x10);
 816                if (ret)
 817                        goto error;
 818
 819                /* disable DVB-T demod on IOD[0] */
 820                ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 0), 0x01);
 821                if (ret)
 822                        goto error;
 823
 824                /* enable DVB-C demod on IOD[5] */
 825                ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 5), 0x20);
 826                if (ret)
 827                        goto error;
 828
 829                /* attach demod */
 830                if (tmp == 0xc7) {
 831                        /* TDA18212 config */
 832                        adap->fe[0] = dvb_attach(tda10023_attach,
 833                                        &anysee_tda10023_tda18212_config,
 834                                        &d->i2c_adap, 0x48);
 835
 836                        /* I2C gate for DNOD44CDH086A(TDA18212) tuner module */
 837                        if (adap->fe[0])
 838                                adap->fe[0]->ops.i2c_gate_ctrl =
 839                                                anysee_i2c_gate_ctrl;
 840                } else {
 841                        /* PLL config */
 842                        adap->fe[0] = dvb_attach(tda10023_attach,
 843                                        &anysee_tda10023_config,
 844                                        &d->i2c_adap, 0x48);
 845                }
 846
 847                /* break out if first frontend attaching fails */
 848                if (!adap->fe[0])
 849                        break;
 850
 851                /* disable DVB-C demod on IOD[5] */
 852                ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 5), 0x20);
 853                if (ret)
 854                        goto error;
 855
 856                /* enable DVB-T demod on IOD[0] */
 857                ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 0), 0x01);
 858                if (ret)
 859                        goto error;
 860
 861                /* attach demod */
 862                if (tmp == 0xc7) {
 863                        /* TDA18212 config */
 864                        adap->fe[1] = dvb_attach(zl10353_attach,
 865                                        &anysee_zl10353_tda18212_config2,
 866                                        &d->i2c_adap);
 867
 868                        /* I2C gate for DNOD44CDH086A(TDA18212) tuner module */
 869                        if (adap->fe[1])
 870                                adap->fe[1]->ops.i2c_gate_ctrl =
 871                                                anysee_i2c_gate_ctrl;
 872                } else {
 873                        /* PLL config */
 874                        adap->fe[1] = dvb_attach(zl10353_attach,
 875                                        &anysee_zl10353_config,
 876                                        &d->i2c_adap);
 877                }
 878
 879                break;
 880        case ANYSEE_HW_508TC: /* 18 */
 881        case ANYSEE_HW_508PTC: /* 21 */
 882                /* E7 TC */
 883                /* E7 PTC */
 884
 885                /* disable DVB-T demod on IOD[6] */
 886                ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 6), 0x40);
 887                if (ret)
 888                        goto error;
 889
 890                /* enable DVB-C demod on IOD[5] */
 891                ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 5), 0x20);
 892                if (ret)
 893                        goto error;
 894
 895                /* attach demod */
 896                adap->fe[0] = dvb_attach(tda10023_attach,
 897                                &anysee_tda10023_tda18212_config,
 898                                &d->i2c_adap, 0x48);
 899
 900                /* I2C gate for DNOD44CDH086A(TDA18212) tuner module */
 901                if (adap->fe[0])
 902                        adap->fe[0]->ops.i2c_gate_ctrl = anysee_i2c_gate_ctrl;
 903
 904                /* break out if first frontend attaching fails */
 905                if (!adap->fe[0])
 906                        break;
 907
 908                /* disable DVB-C demod on IOD[5] */
 909                ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 5), 0x20);
 910                if (ret)
 911                        goto error;
 912
 913                /* enable DVB-T demod on IOD[6] */
 914                ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 6), 0x40);
 915                if (ret)
 916                        goto error;
 917
 918                /* attach demod */
 919                adap->fe[1] = dvb_attach(zl10353_attach,
 920                                &anysee_zl10353_tda18212_config,
 921                                &d->i2c_adap);
 922
 923                /* I2C gate for DNOD44CDH086A(TDA18212) tuner module */
 924                if (adap->fe[1])
 925                        adap->fe[1]->ops.i2c_gate_ctrl = anysee_i2c_gate_ctrl;
 926
 927                state->has_ci = true;
 928
 929                break;
 930        case ANYSEE_HW_508S2: /* 19 */
 931        case ANYSEE_HW_508PS2: /* 22 */
 932                /* E7 S2 */
 933                /* E7 PS2 */
 934
 935                /* enable DVB-S/S2 demod on IOE[5] */
 936                ret = anysee_wr_reg_mask(d, REG_IOE, (1 << 5), 0x20);
 937                if (ret)
 938                        goto error;
 939
 940                /* attach demod */
 941                adap->fe[0] = dvb_attach(stv0900_attach,
 942                                &anysee_stv0900_config, &d->i2c_adap, 0);
 943
 944                state->has_ci = true;
 945
 946                break;
 947        case ANYSEE_HW_508T2C: /* 20 */
 948                /* E7 T2C */
 949
 950                /* enable DVB-T/T2/C demod on IOE[5] */
 951                ret = anysee_wr_reg_mask(d, REG_IOE, (1 << 5), 0x20);
 952                if (ret)
 953                        goto error;
 954
 955                /* attach demod */
 956                adap->fe[0] = dvb_attach(cxd2820r_attach,
 957                                &anysee_cxd2820r_config, &d->i2c_adap, NULL);
 958
 959                state->has_ci = true;
 960
 961                break;
 962        }
 963
 964        if (!adap->fe[0]) {
 965                /* we have no frontend :-( */
 966                ret = -ENODEV;
 967                dev_err(&d->udev->dev,
 968                                "%s: Unsupported Anysee version. Please report to <linux-media@vger.kernel.org>.\n",
 969                                KBUILD_MODNAME);
 970        }
 971error:
 972        return ret;
 973}
 974
 975static int anysee_tuner_attach(struct dvb_usb_adapter *adap)
 976{
 977        struct anysee_state *state = adap_to_priv(adap);
 978        struct dvb_usb_device *d = adap_to_d(adap);
 979        struct dvb_frontend *fe;
 980        int ret;
 981        dev_dbg(&d->udev->dev, "%s:\n", __func__);
 982
 983        switch (state->hw) {
 984        case ANYSEE_HW_507T: /* 2 */
 985                /* E30 */
 986
 987                /* attach tuner */
 988                fe = dvb_attach(dvb_pll_attach, adap->fe[0], (0xc2 >> 1), NULL,
 989                                DVB_PLL_THOMSON_DTT7579);
 990
 991                break;
 992        case ANYSEE_HW_507CD: /* 6 */
 993                /* E30 Plus */
 994
 995                /* attach tuner */
 996                fe = dvb_attach(dvb_pll_attach, adap->fe[0], (0xc2 >> 1),
 997                                &d->i2c_adap, DVB_PLL_THOMSON_DTT7579);
 998
 999                break;
1000        case ANYSEE_HW_507DC: /* 10 */
1001                /* E30 C Plus */
1002
1003                /* attach tuner */
1004                fe = dvb_attach(dvb_pll_attach, adap->fe[0], (0xc0 >> 1),
1005                                &d->i2c_adap, DVB_PLL_SAMSUNG_DTOS403IH102A);
1006
1007                break;
1008        case ANYSEE_HW_507SI: /* 11 */
1009                /* E30 S2 Plus */
1010
1011                /* attach LNB controller */
1012                fe = dvb_attach(isl6423_attach, adap->fe[0], &d->i2c_adap,
1013                                &anysee_isl6423_config);
1014
1015                break;
1016        case ANYSEE_HW_507FA: /* 15 */
1017                /* E30 Combo Plus */
1018                /* E30 C Plus */
1019
1020                /* Try first attach TDA18212 silicon tuner on IOE[4], if that
1021                 * fails attach old simple PLL. */
1022
1023                /* attach tuner */
1024                if (state->has_tda18212) {
1025                        struct tda18212_config tda18212_config =
1026                                        anysee_tda18212_config;
1027
1028                        tda18212_config.fe = adap->fe[0];
1029                        ret = anysee_add_i2c_dev(d, "tda18212", 0x60,
1030                                        &tda18212_config);
1031                        if (ret)
1032                                goto err;
1033
1034                        /* copy tuner ops for 2nd FE as tuner is shared */
1035                        if (adap->fe[1]) {
1036                                adap->fe[1]->tuner_priv =
1037                                                adap->fe[0]->tuner_priv;
1038                                memcpy(&adap->fe[1]->ops.tuner_ops,
1039                                                &adap->fe[0]->ops.tuner_ops,
1040                                                sizeof(struct dvb_tuner_ops));
1041                        }
1042
1043                        return 0;
1044                } else {
1045                        /* attach tuner */
1046                        fe = dvb_attach(dvb_pll_attach, adap->fe[0],
1047                                        (0xc0 >> 1), &d->i2c_adap,
1048                                        DVB_PLL_SAMSUNG_DTOS403IH102A);
1049
1050                        if (fe && adap->fe[1]) {
1051                                /* attach tuner for 2nd FE */
1052                                fe = dvb_attach(dvb_pll_attach, adap->fe[1],
1053                                                (0xc0 >> 1), &d->i2c_adap,
1054                                                DVB_PLL_SAMSUNG_DTOS403IH102A);
1055                        }
1056                }
1057
1058                break;
1059        case ANYSEE_HW_508TC: /* 18 */
1060        case ANYSEE_HW_508PTC: /* 21 */
1061        {
1062                /* E7 TC */
1063                /* E7 PTC */
1064                struct tda18212_config tda18212_config = anysee_tda18212_config;
1065
1066                tda18212_config.fe = adap->fe[0];
1067                ret = anysee_add_i2c_dev(d, "tda18212", 0x60, &tda18212_config);
1068                if (ret)
1069                        goto err;
1070
1071                /* copy tuner ops for 2nd FE as tuner is shared */
1072                if (adap->fe[1]) {
1073                        adap->fe[1]->tuner_priv = adap->fe[0]->tuner_priv;
1074                        memcpy(&adap->fe[1]->ops.tuner_ops,
1075                                        &adap->fe[0]->ops.tuner_ops,
1076                                        sizeof(struct dvb_tuner_ops));
1077                }
1078
1079                return 0;
1080        }
1081        case ANYSEE_HW_508S2: /* 19 */
1082        case ANYSEE_HW_508PS2: /* 22 */
1083                /* E7 S2 */
1084                /* E7 PS2 */
1085
1086                /* attach tuner */
1087                fe = dvb_attach(stv6110_attach, adap->fe[0],
1088                                &anysee_stv6110_config, &d->i2c_adap);
1089
1090                if (fe) {
1091                        /* attach LNB controller */
1092                        fe = dvb_attach(isl6423_attach, adap->fe[0],
1093                                        &d->i2c_adap, &anysee_isl6423_config);
1094                }
1095
1096                break;
1097
1098        case ANYSEE_HW_508T2C: /* 20 */
1099        {
1100                /* E7 T2C */
1101                struct tda18212_config tda18212_config =
1102                                anysee_tda18212_config2;
1103
1104                tda18212_config.fe = adap->fe[0];
1105                ret = anysee_add_i2c_dev(d, "tda18212", 0x60, &tda18212_config);
1106                if (ret)
1107                        goto err;
1108
1109                return 0;
1110        }
1111        default:
1112                fe = NULL;
1113        }
1114
1115        if (fe)
1116                ret = 0;
1117        else
1118                ret = -ENODEV;
1119err:
1120        return ret;
1121}
1122
1123#if IS_ENABLED(CONFIG_RC_CORE)
1124static int anysee_rc_query(struct dvb_usb_device *d)
1125{
1126        u8 buf[] = {CMD_GET_IR_CODE};
1127        u8 ircode[2];
1128        int ret;
1129
1130        /* Remote controller is basic NEC using address byte 0x08.
1131           Anysee device RC query returns only two bytes, status and code,
1132           address byte is dropped. Also it does not return any value for
1133           NEC RCs having address byte other than 0x08. Due to that, we
1134           cannot use that device as standard NEC receiver.
1135           It could be possible make hack which reads whole code directly
1136           from device memory... */
1137
1138        ret = anysee_ctrl_msg(d, buf, sizeof(buf), ircode, sizeof(ircode));
1139        if (ret)
1140                return ret;
1141
1142        if (ircode[0]) {
1143                dev_dbg(&d->udev->dev, "%s: key pressed %02x\n", __func__,
1144                                ircode[1]);
1145                rc_keydown(d->rc_dev, RC_PROTO_NEC,
1146                           RC_SCANCODE_NEC(0x08, ircode[1]), 0);
1147        }
1148
1149        return 0;
1150}
1151
1152static int anysee_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
1153{
1154        rc->allowed_protos = RC_PROTO_BIT_NEC;
1155        rc->query          = anysee_rc_query;
1156        rc->interval       = 250;  /* windows driver uses 500ms */
1157
1158        return 0;
1159}
1160#else
1161        #define anysee_get_rc_config NULL
1162#endif
1163
1164static int anysee_ci_read_attribute_mem(struct dvb_ca_en50221 *ci, int slot,
1165        int addr)
1166{
1167        struct dvb_usb_device *d = ci->data;
1168        int ret;
1169        u8 buf[] = {CMD_CI, 0x02, 0x40 | addr >> 8, addr & 0xff, 0x00, 1};
1170        u8 val;
1171
1172        ret = anysee_ctrl_msg(d, buf, sizeof(buf), &val, 1);
1173        if (ret)
1174                return ret;
1175
1176        return val;
1177}
1178
1179static int anysee_ci_write_attribute_mem(struct dvb_ca_en50221 *ci, int slot,
1180        int addr, u8 val)
1181{
1182        struct dvb_usb_device *d = ci->data;
1183        int ret;
1184        u8 buf[] = {CMD_CI, 0x03, 0x40 | addr >> 8, addr & 0xff, 0x00, 1, val};
1185
1186        ret = anysee_ctrl_msg(d, buf, sizeof(buf), NULL, 0);
1187        if (ret)
1188                return ret;
1189
1190        return 0;
1191}
1192
1193static int anysee_ci_read_cam_control(struct dvb_ca_en50221 *ci, int slot,
1194        u8 addr)
1195{
1196        struct dvb_usb_device *d = ci->data;
1197        int ret;
1198        u8 buf[] = {CMD_CI, 0x04, 0x40, addr, 0x00, 1};
1199        u8 val;
1200
1201        ret = anysee_ctrl_msg(d, buf, sizeof(buf), &val, 1);
1202        if (ret)
1203                return ret;
1204
1205        return val;
1206}
1207
1208static int anysee_ci_write_cam_control(struct dvb_ca_en50221 *ci, int slot,
1209        u8 addr, u8 val)
1210{
1211        struct dvb_usb_device *d = ci->data;
1212        int ret;
1213        u8 buf[] = {CMD_CI, 0x05, 0x40, addr, 0x00, 1, val};
1214
1215        ret = anysee_ctrl_msg(d, buf, sizeof(buf), NULL, 0);
1216        if (ret)
1217                return ret;
1218
1219        return 0;
1220}
1221
1222static int anysee_ci_slot_reset(struct dvb_ca_en50221 *ci, int slot)
1223{
1224        struct dvb_usb_device *d = ci->data;
1225        int ret;
1226        struct anysee_state *state = d_to_priv(d);
1227
1228        state->ci_cam_ready = jiffies + msecs_to_jiffies(1000);
1229
1230        ret = anysee_wr_reg_mask(d, REG_IOA, (0 << 7), 0x80);
1231        if (ret)
1232                return ret;
1233
1234        msleep(300);
1235
1236        ret = anysee_wr_reg_mask(d, REG_IOA, (1 << 7), 0x80);
1237        if (ret)
1238                return ret;
1239
1240        return 0;
1241}
1242
1243static int anysee_ci_slot_shutdown(struct dvb_ca_en50221 *ci, int slot)
1244{
1245        struct dvb_usb_device *d = ci->data;
1246        int ret;
1247
1248        ret = anysee_wr_reg_mask(d, REG_IOA, (0 << 7), 0x80);
1249        if (ret)
1250                return ret;
1251
1252        msleep(30);
1253
1254        ret = anysee_wr_reg_mask(d, REG_IOA, (1 << 7), 0x80);
1255        if (ret)
1256                return ret;
1257
1258        return 0;
1259}
1260
1261static int anysee_ci_slot_ts_enable(struct dvb_ca_en50221 *ci, int slot)
1262{
1263        struct dvb_usb_device *d = ci->data;
1264        int ret;
1265
1266        ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 1), 0x02);
1267        if (ret)
1268                return ret;
1269
1270        return 0;
1271}
1272
1273static int anysee_ci_poll_slot_status(struct dvb_ca_en50221 *ci, int slot,
1274        int open)
1275{
1276        struct dvb_usb_device *d = ci->data;
1277        struct anysee_state *state = d_to_priv(d);
1278        int ret;
1279        u8 tmp = 0;
1280
1281        ret = anysee_rd_reg_mask(d, REG_IOC, &tmp, 0x40);
1282        if (ret)
1283                return ret;
1284
1285        if (tmp == 0) {
1286                ret = DVB_CA_EN50221_POLL_CAM_PRESENT;
1287                if (time_after(jiffies, state->ci_cam_ready))
1288                        ret |= DVB_CA_EN50221_POLL_CAM_READY;
1289        }
1290
1291        return ret;
1292}
1293
1294static int anysee_ci_init(struct dvb_usb_device *d)
1295{
1296        struct anysee_state *state = d_to_priv(d);
1297        int ret;
1298
1299        state->ci.owner               = THIS_MODULE;
1300        state->ci.read_attribute_mem  = anysee_ci_read_attribute_mem;
1301        state->ci.write_attribute_mem = anysee_ci_write_attribute_mem;
1302        state->ci.read_cam_control    = anysee_ci_read_cam_control;
1303        state->ci.write_cam_control   = anysee_ci_write_cam_control;
1304        state->ci.slot_reset          = anysee_ci_slot_reset;
1305        state->ci.slot_shutdown       = anysee_ci_slot_shutdown;
1306        state->ci.slot_ts_enable      = anysee_ci_slot_ts_enable;
1307        state->ci.poll_slot_status    = anysee_ci_poll_slot_status;
1308        state->ci.data                = d;
1309
1310        ret = anysee_wr_reg_mask(d, REG_IOA, (1 << 7), 0x80);
1311        if (ret)
1312                return ret;
1313
1314        ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 2)|(0 << 1)|(0 << 0), 0x07);
1315        if (ret)
1316                return ret;
1317
1318        ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 2)|(1 << 1)|(1 << 0), 0x07);
1319        if (ret)
1320                return ret;
1321
1322        ret = dvb_ca_en50221_init(&d->adapter[0].dvb_adap, &state->ci, 0, 1);
1323        if (ret)
1324                return ret;
1325
1326        state->ci_attached = true;
1327
1328        return 0;
1329}
1330
1331static void anysee_ci_release(struct dvb_usb_device *d)
1332{
1333        struct anysee_state *state = d_to_priv(d);
1334
1335        /* detach CI */
1336        if (state->ci_attached)
1337                dvb_ca_en50221_release(&state->ci);
1338
1339        return;
1340}
1341
1342static int anysee_init(struct dvb_usb_device *d)
1343{
1344        struct anysee_state *state = d_to_priv(d);
1345        int ret;
1346
1347        /* There is one interface with two alternate settings.
1348           Alternate setting 0 is for bulk transfer.
1349           Alternate setting 1 is for isochronous transfer.
1350           We use bulk transfer (alternate setting 0). */
1351        ret = usb_set_interface(d->udev, 0, 0);
1352        if (ret)
1353                return ret;
1354
1355        /* LED light */
1356        ret = anysee_led_ctrl(d, 0x01, 0x03);
1357        if (ret)
1358                return ret;
1359
1360        /* enable IR */
1361        ret = anysee_ir_ctrl(d, 1);
1362        if (ret)
1363                return ret;
1364
1365        /* attach CI */
1366        if (state->has_ci) {
1367                ret = anysee_ci_init(d);
1368                if (ret)
1369                        return ret;
1370        }
1371
1372        return 0;
1373}
1374
1375static void anysee_exit(struct dvb_usb_device *d)
1376{
1377        struct anysee_state *state = d_to_priv(d);
1378
1379        if (state->i2c_client[0])
1380                anysee_del_i2c_dev(d);
1381
1382        return anysee_ci_release(d);
1383}
1384
1385/* DVB USB Driver stuff */
1386static struct dvb_usb_device_properties anysee_props = {
1387        .driver_name = KBUILD_MODNAME,
1388        .owner = THIS_MODULE,
1389        .adapter_nr = adapter_nr,
1390        .size_of_priv = sizeof(struct anysee_state),
1391
1392        .generic_bulk_ctrl_endpoint = 0x01,
1393        .generic_bulk_ctrl_endpoint_response = 0x81,
1394
1395        .i2c_algo         = &anysee_i2c_algo,
1396        .read_config      = anysee_read_config,
1397        .frontend_attach  = anysee_frontend_attach,
1398        .tuner_attach     = anysee_tuner_attach,
1399        .init             = anysee_init,
1400        .get_rc_config    = anysee_get_rc_config,
1401        .frontend_ctrl    = anysee_frontend_ctrl,
1402        .streaming_ctrl   = anysee_streaming_ctrl,
1403        .exit             = anysee_exit,
1404
1405        .num_adapters = 1,
1406        .adapter = {
1407                {
1408                        .stream = DVB_USB_STREAM_BULK(0x82, 8, 16 * 512),
1409                }
1410        }
1411};
1412
1413static const struct usb_device_id anysee_id_table[] = {
1414        { DVB_USB_DEVICE(USB_VID_CYPRESS, USB_PID_ANYSEE,
1415                &anysee_props, "Anysee", RC_MAP_ANYSEE) },
1416        { DVB_USB_DEVICE(USB_VID_AMT, USB_PID_ANYSEE,
1417                &anysee_props, "Anysee", RC_MAP_ANYSEE) },
1418        { }
1419};
1420MODULE_DEVICE_TABLE(usb, anysee_id_table);
1421
1422static struct usb_driver anysee_usb_driver = {
1423        .name = KBUILD_MODNAME,
1424        .id_table = anysee_id_table,
1425        .probe = dvb_usbv2_probe,
1426        .disconnect = dvb_usbv2_disconnect,
1427        .suspend = dvb_usbv2_suspend,
1428        .resume = dvb_usbv2_resume,
1429        .reset_resume = dvb_usbv2_reset_resume,
1430        .no_dynamic_id = 1,
1431        .soft_unbind = 1,
1432};
1433
1434module_usb_driver(anysee_usb_driver);
1435
1436MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1437MODULE_DESCRIPTION("Driver Anysee E30 DVB-C & DVB-T USB2.0");
1438MODULE_LICENSE("GPL");
1439