linux/drivers/media/usb/dvb-usb-v2/af9035.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * Afatech AF9035 DVB USB driver
   4 *
   5 * Copyright (C) 2009 Antti Palosaari <crope@iki.fi>
   6 * Copyright (C) 2012 Antti Palosaari <crope@iki.fi>
   7 */
   8
   9#include "af9035.h"
  10
  11/* Max transfer size done by I2C transfer functions */
  12#define MAX_XFER_SIZE  64
  13
  14DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  15
  16static u16 af9035_checksum(const u8 *buf, size_t len)
  17{
  18        size_t i;
  19        u16 checksum = 0;
  20
  21        for (i = 1; i < len; i++) {
  22                if (i % 2)
  23                        checksum += buf[i] << 8;
  24                else
  25                        checksum += buf[i];
  26        }
  27        checksum = ~checksum;
  28
  29        return checksum;
  30}
  31
  32static int af9035_ctrl_msg(struct dvb_usb_device *d, struct usb_req *req)
  33{
  34#define REQ_HDR_LEN 4 /* send header size */
  35#define ACK_HDR_LEN 3 /* rece header size */
  36#define CHECKSUM_LEN 2
  37#define USB_TIMEOUT 2000
  38        struct state *state = d_to_priv(d);
  39        struct usb_interface *intf = d->intf;
  40        int ret, wlen, rlen;
  41        u16 checksum, tmp_checksum;
  42
  43        mutex_lock(&d->usb_mutex);
  44
  45        /* buffer overflow check */
  46        if (req->wlen > (BUF_LEN - REQ_HDR_LEN - CHECKSUM_LEN) ||
  47                        req->rlen > (BUF_LEN - ACK_HDR_LEN - CHECKSUM_LEN)) {
  48                dev_err(&intf->dev, "too much data wlen=%d rlen=%d\n",
  49                        req->wlen, req->rlen);
  50                ret = -EINVAL;
  51                goto exit;
  52        }
  53
  54        state->buf[0] = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN - 1;
  55        state->buf[1] = req->mbox;
  56        state->buf[2] = req->cmd;
  57        state->buf[3] = state->seq++;
  58        memcpy(&state->buf[REQ_HDR_LEN], req->wbuf, req->wlen);
  59
  60        wlen = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN;
  61        rlen = ACK_HDR_LEN + req->rlen + CHECKSUM_LEN;
  62
  63        /* calc and add checksum */
  64        checksum = af9035_checksum(state->buf, state->buf[0] - 1);
  65        state->buf[state->buf[0] - 1] = (checksum >> 8);
  66        state->buf[state->buf[0] - 0] = (checksum & 0xff);
  67
  68        /* no ack for these packets */
  69        if (req->cmd == CMD_FW_DL)
  70                rlen = 0;
  71
  72        ret = dvb_usbv2_generic_rw_locked(d,
  73                        state->buf, wlen, state->buf, rlen);
  74        if (ret)
  75                goto exit;
  76
  77        /* no ack for those packets */
  78        if (req->cmd == CMD_FW_DL)
  79                goto exit;
  80
  81        /* verify checksum */
  82        checksum = af9035_checksum(state->buf, rlen - 2);
  83        tmp_checksum = (state->buf[rlen - 2] << 8) | state->buf[rlen - 1];
  84        if (tmp_checksum != checksum) {
  85                dev_err(&intf->dev, "command=%02x checksum mismatch (%04x != %04x)\n",
  86                        req->cmd, tmp_checksum, checksum);
  87                ret = -EIO;
  88                goto exit;
  89        }
  90
  91        /* check status */
  92        if (state->buf[2]) {
  93                /* fw returns status 1 when IR code was not received */
  94                if (req->cmd == CMD_IR_GET || state->buf[2] == 1) {
  95                        ret = 1;
  96                        goto exit;
  97                }
  98
  99                dev_dbg(&intf->dev, "command=%02x failed fw error=%d\n",
 100                        req->cmd, state->buf[2]);
 101                ret = -EIO;
 102                goto exit;
 103        }
 104
 105        /* read request, copy returned data to return buf */
 106        if (req->rlen)
 107                memcpy(req->rbuf, &state->buf[ACK_HDR_LEN], req->rlen);
 108exit:
 109        mutex_unlock(&d->usb_mutex);
 110        return ret;
 111}
 112
 113/* write multiple registers */
 114static int af9035_wr_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
 115{
 116        struct usb_interface *intf = d->intf;
 117        u8 wbuf[MAX_XFER_SIZE];
 118        u8 mbox = (reg >> 16) & 0xff;
 119        struct usb_req req = { CMD_MEM_WR, mbox, 6 + len, wbuf, 0, NULL };
 120
 121        if (6 + len > sizeof(wbuf)) {
 122                dev_warn(&intf->dev, "i2c wr: len=%d is too big!\n", len);
 123                return -EOPNOTSUPP;
 124        }
 125
 126        wbuf[0] = len;
 127        wbuf[1] = 2;
 128        wbuf[2] = 0;
 129        wbuf[3] = 0;
 130        wbuf[4] = (reg >> 8) & 0xff;
 131        wbuf[5] = (reg >> 0) & 0xff;
 132        memcpy(&wbuf[6], val, len);
 133
 134        return af9035_ctrl_msg(d, &req);
 135}
 136
 137/* read multiple registers */
 138static int af9035_rd_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
 139{
 140        u8 wbuf[] = { len, 2, 0, 0, (reg >> 8) & 0xff, reg & 0xff };
 141        u8 mbox = (reg >> 16) & 0xff;
 142        struct usb_req req = { CMD_MEM_RD, mbox, sizeof(wbuf), wbuf, len, val };
 143
 144        return af9035_ctrl_msg(d, &req);
 145}
 146
 147/* write single register */
 148static int af9035_wr_reg(struct dvb_usb_device *d, u32 reg, u8 val)
 149{
 150        return af9035_wr_regs(d, reg, &val, 1);
 151}
 152
 153/* read single register */
 154static int af9035_rd_reg(struct dvb_usb_device *d, u32 reg, u8 *val)
 155{
 156        return af9035_rd_regs(d, reg, val, 1);
 157}
 158
 159/* write single register with mask */
 160static int af9035_wr_reg_mask(struct dvb_usb_device *d, u32 reg, u8 val,
 161                u8 mask)
 162{
 163        int ret;
 164        u8 tmp;
 165
 166        /* no need for read if whole reg is written */
 167        if (mask != 0xff) {
 168                ret = af9035_rd_regs(d, reg, &tmp, 1);
 169                if (ret)
 170                        return ret;
 171
 172                val &= mask;
 173                tmp &= ~mask;
 174                val |= tmp;
 175        }
 176
 177        return af9035_wr_regs(d, reg, &val, 1);
 178}
 179
 180static int af9035_add_i2c_dev(struct dvb_usb_device *d, const char *type,
 181                u8 addr, void *platform_data, struct i2c_adapter *adapter)
 182{
 183        int ret, num;
 184        struct state *state = d_to_priv(d);
 185        struct usb_interface *intf = d->intf;
 186        struct i2c_client *client;
 187        struct i2c_board_info board_info = {
 188                .addr = addr,
 189                .platform_data = platform_data,
 190        };
 191
 192        strscpy(board_info.type, type, I2C_NAME_SIZE);
 193
 194        /* find first free client */
 195        for (num = 0; num < AF9035_I2C_CLIENT_MAX; num++) {
 196                if (state->i2c_client[num] == NULL)
 197                        break;
 198        }
 199
 200        dev_dbg(&intf->dev, "num=%d\n", num);
 201
 202        if (num == AF9035_I2C_CLIENT_MAX) {
 203                dev_err(&intf->dev, "I2C client out of index\n");
 204                ret = -ENODEV;
 205                goto err;
 206        }
 207
 208        request_module("%s", board_info.type);
 209
 210        /* register I2C device */
 211        client = i2c_new_device(adapter, &board_info);
 212        if (client == NULL || client->dev.driver == NULL) {
 213                ret = -ENODEV;
 214                goto err;
 215        }
 216
 217        /* increase I2C driver usage count */
 218        if (!try_module_get(client->dev.driver->owner)) {
 219                i2c_unregister_device(client);
 220                ret = -ENODEV;
 221                goto err;
 222        }
 223
 224        state->i2c_client[num] = client;
 225        return 0;
 226err:
 227        dev_dbg(&intf->dev, "failed=%d\n", ret);
 228        return ret;
 229}
 230
 231static void af9035_del_i2c_dev(struct dvb_usb_device *d)
 232{
 233        int num;
 234        struct state *state = d_to_priv(d);
 235        struct usb_interface *intf = d->intf;
 236        struct i2c_client *client;
 237
 238        /* find last used client */
 239        num = AF9035_I2C_CLIENT_MAX;
 240        while (num--) {
 241                if (state->i2c_client[num] != NULL)
 242                        break;
 243        }
 244
 245        dev_dbg(&intf->dev, "num=%d\n", num);
 246
 247        if (num == -1) {
 248                dev_err(&intf->dev, "I2C client out of index\n");
 249                goto err;
 250        }
 251
 252        client = state->i2c_client[num];
 253
 254        /* decrease I2C driver usage count */
 255        module_put(client->dev.driver->owner);
 256
 257        /* unregister I2C device */
 258        i2c_unregister_device(client);
 259
 260        state->i2c_client[num] = NULL;
 261        return;
 262err:
 263        dev_dbg(&intf->dev, "failed\n");
 264}
 265
 266static int af9035_i2c_master_xfer(struct i2c_adapter *adap,
 267                struct i2c_msg msg[], int num)
 268{
 269        struct dvb_usb_device *d = i2c_get_adapdata(adap);
 270        struct state *state = d_to_priv(d);
 271        int ret;
 272
 273        if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
 274                return -EAGAIN;
 275
 276        /*
 277         * AF9035 I2C sub header is 5 bytes long. Meaning of those bytes are:
 278         * 0: data len
 279         * 1: I2C addr << 1
 280         * 2: reg addr len
 281         *    byte 3 and 4 can be used as reg addr
 282         * 3: reg addr MSB
 283         *    used when reg addr len is set to 2
 284         * 4: reg addr LSB
 285         *    used when reg addr len is set to 1 or 2
 286         *
 287         * For the simplify we do not use register addr at all.
 288         * NOTE: As a firmware knows tuner type there is very small possibility
 289         * there could be some tuner I2C hacks done by firmware and this may
 290         * lead problems if firmware expects those bytes are used.
 291         *
 292         * TODO: Here is few hacks. AF9035 chip integrates AF9033 demodulator.
 293         * IT9135 chip integrates AF9033 demodulator and RF tuner. For dual
 294         * tuner devices, there is also external AF9033 demodulator connected
 295         * via external I2C bus. All AF9033 demod I2C traffic, both single and
 296         * dual tuner configuration, is covered by firmware - actual USB IO
 297         * looks just like a memory access.
 298         * In case of IT913x chip, there is own tuner driver. It is implemented
 299         * currently as a I2C driver, even tuner IP block is likely build
 300         * directly into the demodulator memory space and there is no own I2C
 301         * bus. I2C subsystem does not allow register multiple devices to same
 302         * bus, having same slave address. Due to that we reuse demod address,
 303         * shifted by one bit, on that case.
 304         *
 305         * For IT930x we use a different command and the sub header is
 306         * different as well:
 307         * 0: data len
 308         * 1: I2C bus (0x03 seems to be only value used)
 309         * 2: I2C addr << 1
 310         */
 311#define AF9035_IS_I2C_XFER_WRITE_READ(_msg, _num) \
 312        (_num == 2 && !(_msg[0].flags & I2C_M_RD) && (_msg[1].flags & I2C_M_RD))
 313#define AF9035_IS_I2C_XFER_WRITE(_msg, _num) \
 314        (_num == 1 && !(_msg[0].flags & I2C_M_RD))
 315#define AF9035_IS_I2C_XFER_READ(_msg, _num) \
 316        (_num == 1 && (_msg[0].flags & I2C_M_RD))
 317
 318        if (AF9035_IS_I2C_XFER_WRITE_READ(msg, num)) {
 319                if (msg[0].len > 40 || msg[1].len > 40) {
 320                        /* TODO: correct limits > 40 */
 321                        ret = -EOPNOTSUPP;
 322                } else if ((msg[0].addr == state->af9033_i2c_addr[0]) ||
 323                           (msg[0].addr == state->af9033_i2c_addr[1])) {
 324                        /* demod access via firmware interface */
 325                        u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
 326                                        msg[0].buf[2];
 327
 328                        if (msg[0].addr == state->af9033_i2c_addr[1])
 329                                reg |= 0x100000;
 330
 331                        ret = af9035_rd_regs(d, reg, &msg[1].buf[0],
 332                                        msg[1].len);
 333                } else if (state->no_read) {
 334                        memset(msg[1].buf, 0, msg[1].len);
 335                        ret = 0;
 336                } else {
 337                        /* I2C write + read */
 338                        u8 buf[MAX_XFER_SIZE];
 339                        struct usb_req req = { CMD_I2C_RD, 0, 5 + msg[0].len,
 340                                        buf, msg[1].len, msg[1].buf };
 341
 342                        if (state->chip_type == 0x9306) {
 343                                req.cmd = CMD_GENERIC_I2C_RD;
 344                                req.wlen = 3 + msg[0].len;
 345                        }
 346                        req.mbox |= ((msg[0].addr & 0x80)  >>  3);
 347
 348                        buf[0] = msg[1].len;
 349                        if (state->chip_type == 0x9306) {
 350                                buf[1] = 0x03; /* I2C bus */
 351                                buf[2] = msg[0].addr << 1;
 352                                memcpy(&buf[3], msg[0].buf, msg[0].len);
 353                        } else {
 354                                buf[1] = msg[0].addr << 1;
 355                                buf[3] = 0x00; /* reg addr MSB */
 356                                buf[4] = 0x00; /* reg addr LSB */
 357
 358                                /* Keep prev behavior for write req len > 2*/
 359                                if (msg[0].len > 2) {
 360                                        buf[2] = 0x00; /* reg addr len */
 361                                        memcpy(&buf[5], msg[0].buf, msg[0].len);
 362
 363                                /* Use reg addr fields if write req len <= 2 */
 364                                } else {
 365                                        req.wlen = 5;
 366                                        buf[2] = msg[0].len;
 367                                        if (msg[0].len == 2) {
 368                                                buf[3] = msg[0].buf[0];
 369                                                buf[4] = msg[0].buf[1];
 370                                        } else if (msg[0].len == 1) {
 371                                                buf[4] = msg[0].buf[0];
 372                                        }
 373                                }
 374                        }
 375                        ret = af9035_ctrl_msg(d, &req);
 376                }
 377        } else if (AF9035_IS_I2C_XFER_WRITE(msg, num)) {
 378                if (msg[0].len > 40) {
 379                        /* TODO: correct limits > 40 */
 380                        ret = -EOPNOTSUPP;
 381                } else if ((msg[0].addr == state->af9033_i2c_addr[0]) ||
 382                           (msg[0].addr == state->af9033_i2c_addr[1])) {
 383                        /* demod access via firmware interface */
 384                        u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
 385                                        msg[0].buf[2];
 386
 387                        if (msg[0].addr == state->af9033_i2c_addr[1])
 388                                reg |= 0x100000;
 389
 390                        ret = (msg[0].len >= 3) ? af9035_wr_regs(d, reg,
 391                                                                 &msg[0].buf[3],
 392                                                                 msg[0].len - 3)
 393                                                : -EOPNOTSUPP;
 394                } else {
 395                        /* I2C write */
 396                        u8 buf[MAX_XFER_SIZE];
 397                        struct usb_req req = { CMD_I2C_WR, 0, 5 + msg[0].len,
 398                                        buf, 0, NULL };
 399
 400                        if (state->chip_type == 0x9306) {
 401                                req.cmd = CMD_GENERIC_I2C_WR;
 402                                req.wlen = 3 + msg[0].len;
 403                        }
 404
 405                        req.mbox |= ((msg[0].addr & 0x80)  >>  3);
 406                        buf[0] = msg[0].len;
 407                        if (state->chip_type == 0x9306) {
 408                                buf[1] = 0x03; /* I2C bus */
 409                                buf[2] = msg[0].addr << 1;
 410                                memcpy(&buf[3], msg[0].buf, msg[0].len);
 411                        } else {
 412                                buf[1] = msg[0].addr << 1;
 413                                buf[2] = 0x00; /* reg addr len */
 414                                buf[3] = 0x00; /* reg addr MSB */
 415                                buf[4] = 0x00; /* reg addr LSB */
 416                                memcpy(&buf[5], msg[0].buf, msg[0].len);
 417                        }
 418                        ret = af9035_ctrl_msg(d, &req);
 419                }
 420        } else if (AF9035_IS_I2C_XFER_READ(msg, num)) {
 421                if (msg[0].len > 40) {
 422                        /* TODO: correct limits > 40 */
 423                        ret = -EOPNOTSUPP;
 424                } else if (state->no_read) {
 425                        memset(msg[0].buf, 0, msg[0].len);
 426                        ret = 0;
 427                } else {
 428                        /* I2C read */
 429                        u8 buf[5];
 430                        struct usb_req req = { CMD_I2C_RD, 0, sizeof(buf),
 431                                                buf, msg[0].len, msg[0].buf };
 432
 433                        if (state->chip_type == 0x9306) {
 434                                req.cmd = CMD_GENERIC_I2C_RD;
 435                                req.wlen = 3;
 436                        }
 437                        req.mbox |= ((msg[0].addr & 0x80)  >>  3);
 438                        buf[0] = msg[0].len;
 439                        if (state->chip_type == 0x9306) {
 440                                buf[1] = 0x03; /* I2C bus */
 441                                buf[2] = msg[0].addr << 1;
 442                        } else {
 443                                buf[1] = msg[0].addr << 1;
 444                                buf[2] = 0x00; /* reg addr len */
 445                                buf[3] = 0x00; /* reg addr MSB */
 446                                buf[4] = 0x00; /* reg addr LSB */
 447                        }
 448                        ret = af9035_ctrl_msg(d, &req);
 449                }
 450        } else {
 451                /*
 452                 * We support only three kind of I2C transactions:
 453                 * 1) 1 x write + 1 x read (repeated start)
 454                 * 2) 1 x write
 455                 * 3) 1 x read
 456                 */
 457                ret = -EOPNOTSUPP;
 458        }
 459
 460        mutex_unlock(&d->i2c_mutex);
 461
 462        if (ret < 0)
 463                return ret;
 464        else
 465                return num;
 466}
 467
 468static u32 af9035_i2c_functionality(struct i2c_adapter *adapter)
 469{
 470        return I2C_FUNC_I2C;
 471}
 472
 473static struct i2c_algorithm af9035_i2c_algo = {
 474        .master_xfer = af9035_i2c_master_xfer,
 475        .functionality = af9035_i2c_functionality,
 476};
 477
 478static int af9035_identify_state(struct dvb_usb_device *d, const char **name)
 479{
 480        struct state *state = d_to_priv(d);
 481        struct usb_interface *intf = d->intf;
 482        int ret, i, ts_mode_invalid;
 483        unsigned int utmp, eeprom_addr;
 484        u8 tmp;
 485        u8 wbuf[1] = { 1 };
 486        u8 rbuf[4];
 487        struct usb_req req = { CMD_FW_QUERYINFO, 0, sizeof(wbuf), wbuf,
 488                        sizeof(rbuf), rbuf };
 489
 490        ret = af9035_rd_regs(d, 0x1222, rbuf, 3);
 491        if (ret < 0)
 492                goto err;
 493
 494        state->chip_version = rbuf[0];
 495        state->chip_type = rbuf[2] << 8 | rbuf[1] << 0;
 496
 497        ret = af9035_rd_reg(d, 0x384f, &state->prechip_version);
 498        if (ret < 0)
 499                goto err;
 500
 501        dev_info(&intf->dev, "prechip_version=%02x chip_version=%02x chip_type=%04x\n",
 502                 state->prechip_version, state->chip_version, state->chip_type);
 503
 504        if (state->chip_type == 0x9135) {
 505                if (state->chip_version == 0x02) {
 506                        *name = AF9035_FIRMWARE_IT9135_V2;
 507                        utmp = 0x00461d;
 508                } else {
 509                        *name = AF9035_FIRMWARE_IT9135_V1;
 510                        utmp = 0x00461b;
 511                }
 512
 513                /* Check if eeprom exists */
 514                ret = af9035_rd_reg(d, utmp, &tmp);
 515                if (ret < 0)
 516                        goto err;
 517
 518                if (tmp == 0x00) {
 519                        dev_dbg(&intf->dev, "no eeprom\n");
 520                        state->no_eeprom = true;
 521                        goto check_firmware_status;
 522                }
 523
 524                eeprom_addr = EEPROM_BASE_IT9135;
 525        } else if (state->chip_type == 0x9306) {
 526                *name = AF9035_FIRMWARE_IT9303;
 527                state->no_eeprom = true;
 528                goto check_firmware_status;
 529        } else {
 530                *name = AF9035_FIRMWARE_AF9035;
 531                eeprom_addr = EEPROM_BASE_AF9035;
 532        }
 533
 534        /* Read and store eeprom */
 535        for (i = 0; i < 256; i += 32) {
 536                ret = af9035_rd_regs(d, eeprom_addr + i, &state->eeprom[i], 32);
 537                if (ret < 0)
 538                        goto err;
 539        }
 540
 541        dev_dbg(&intf->dev, "eeprom dump:\n");
 542        for (i = 0; i < 256; i += 16)
 543                dev_dbg(&intf->dev, "%*ph\n", 16, &state->eeprom[i]);
 544
 545        /* check for dual tuner mode */
 546        tmp = state->eeprom[EEPROM_TS_MODE];
 547        ts_mode_invalid = 0;
 548        switch (tmp) {
 549        case 0:
 550                break;
 551        case 1:
 552        case 3:
 553                state->dual_mode = true;
 554                break;
 555        case 5:
 556                if (state->chip_type != 0x9135 && state->chip_type != 0x9306)
 557                        state->dual_mode = true;        /* AF9035 */
 558                else
 559                        ts_mode_invalid = 1;
 560                break;
 561        default:
 562                ts_mode_invalid = 1;
 563        }
 564
 565        dev_dbg(&intf->dev, "ts mode=%d dual mode=%d\n", tmp, state->dual_mode);
 566
 567        if (ts_mode_invalid)
 568                dev_info(&intf->dev, "ts mode=%d not supported, defaulting to single tuner mode!", tmp);
 569
 570check_firmware_status:
 571        ret = af9035_ctrl_msg(d, &req);
 572        if (ret < 0)
 573                goto err;
 574
 575        dev_dbg(&intf->dev, "reply=%*ph\n", 4, rbuf);
 576        if (rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])
 577                ret = WARM;
 578        else
 579                ret = COLD;
 580
 581        return ret;
 582
 583err:
 584        dev_dbg(&intf->dev, "failed=%d\n", ret);
 585
 586        return ret;
 587}
 588
 589static int af9035_download_firmware_old(struct dvb_usb_device *d,
 590                const struct firmware *fw)
 591{
 592        struct usb_interface *intf = d->intf;
 593        int ret, i, j, len;
 594        u8 wbuf[1];
 595        struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
 596        struct usb_req req_fw_dl = { CMD_FW_DL, 0, 0, wbuf, 0, NULL };
 597        u8 hdr_core;
 598        u16 hdr_addr, hdr_data_len, hdr_checksum;
 599        #define MAX_DATA 58
 600        #define HDR_SIZE 7
 601
 602        /*
 603         * Thanks to Daniel Glöckner <daniel-gl@gmx.net> about that info!
 604         *
 605         * byte 0: MCS 51 core
 606         *  There are two inside the AF9035 (1=Link and 2=OFDM) with separate
 607         *  address spaces
 608         * byte 1-2: Big endian destination address
 609         * byte 3-4: Big endian number of data bytes following the header
 610         * byte 5-6: Big endian header checksum, apparently ignored by the chip
 611         *  Calculated as ~(h[0]*256+h[1]+h[2]*256+h[3]+h[4]*256)
 612         */
 613
 614        for (i = fw->size; i > HDR_SIZE;) {
 615                hdr_core = fw->data[fw->size - i + 0];
 616                hdr_addr = fw->data[fw->size - i + 1] << 8;
 617                hdr_addr |= fw->data[fw->size - i + 2] << 0;
 618                hdr_data_len = fw->data[fw->size - i + 3] << 8;
 619                hdr_data_len |= fw->data[fw->size - i + 4] << 0;
 620                hdr_checksum = fw->data[fw->size - i + 5] << 8;
 621                hdr_checksum |= fw->data[fw->size - i + 6] << 0;
 622
 623                dev_dbg(&intf->dev, "core=%d addr=%04x data_len=%d checksum=%04x\n",
 624                        hdr_core, hdr_addr, hdr_data_len, hdr_checksum);
 625
 626                if (((hdr_core != 1) && (hdr_core != 2)) ||
 627                                (hdr_data_len > i)) {
 628                        dev_dbg(&intf->dev, "bad firmware\n");
 629                        break;
 630                }
 631
 632                /* download begin packet */
 633                req.cmd = CMD_FW_DL_BEGIN;
 634                ret = af9035_ctrl_msg(d, &req);
 635                if (ret < 0)
 636                        goto err;
 637
 638                /* download firmware packet(s) */
 639                for (j = HDR_SIZE + hdr_data_len; j > 0; j -= MAX_DATA) {
 640                        len = j;
 641                        if (len > MAX_DATA)
 642                                len = MAX_DATA;
 643                        req_fw_dl.wlen = len;
 644                        req_fw_dl.wbuf = (u8 *) &fw->data[fw->size - i +
 645                                        HDR_SIZE + hdr_data_len - j];
 646                        ret = af9035_ctrl_msg(d, &req_fw_dl);
 647                        if (ret < 0)
 648                                goto err;
 649                }
 650
 651                /* download end packet */
 652                req.cmd = CMD_FW_DL_END;
 653                ret = af9035_ctrl_msg(d, &req);
 654                if (ret < 0)
 655                        goto err;
 656
 657                i -= hdr_data_len + HDR_SIZE;
 658
 659                dev_dbg(&intf->dev, "data uploaded=%zu\n", fw->size - i);
 660        }
 661
 662        /* print warn if firmware is bad, continue and see what happens */
 663        if (i)
 664                dev_warn(&intf->dev, "bad firmware\n");
 665
 666        return 0;
 667
 668err:
 669        dev_dbg(&intf->dev, "failed=%d\n", ret);
 670
 671        return ret;
 672}
 673
 674static int af9035_download_firmware_new(struct dvb_usb_device *d,
 675                const struct firmware *fw)
 676{
 677        struct usb_interface *intf = d->intf;
 678        int ret, i, i_prev;
 679        struct usb_req req_fw_dl = { CMD_FW_SCATTER_WR, 0, 0, NULL, 0, NULL };
 680        #define HDR_SIZE 7
 681
 682        /*
 683         * There seems to be following firmware header. Meaning of bytes 0-3
 684         * is unknown.
 685         *
 686         * 0: 3
 687         * 1: 0, 1
 688         * 2: 0
 689         * 3: 1, 2, 3
 690         * 4: addr MSB
 691         * 5: addr LSB
 692         * 6: count of data bytes ?
 693         */
 694        for (i = HDR_SIZE, i_prev = 0; i <= fw->size; i++) {
 695                if (i == fw->size ||
 696                                (fw->data[i + 0] == 0x03 &&
 697                                (fw->data[i + 1] == 0x00 ||
 698                                fw->data[i + 1] == 0x01) &&
 699                                fw->data[i + 2] == 0x00)) {
 700                        req_fw_dl.wlen = i - i_prev;
 701                        req_fw_dl.wbuf = (u8 *) &fw->data[i_prev];
 702                        i_prev = i;
 703                        ret = af9035_ctrl_msg(d, &req_fw_dl);
 704                        if (ret < 0)
 705                                goto err;
 706
 707                        dev_dbg(&intf->dev, "data uploaded=%d\n", i);
 708                }
 709        }
 710
 711        return 0;
 712
 713err:
 714        dev_dbg(&intf->dev, "failed=%d\n", ret);
 715
 716        return ret;
 717}
 718
 719static int af9035_download_firmware(struct dvb_usb_device *d,
 720                const struct firmware *fw)
 721{
 722        struct usb_interface *intf = d->intf;
 723        struct state *state = d_to_priv(d);
 724        int ret;
 725        u8 wbuf[1];
 726        u8 rbuf[4];
 727        u8 tmp;
 728        struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
 729        struct usb_req req_fw_ver = { CMD_FW_QUERYINFO, 0, 1, wbuf, 4, rbuf };
 730
 731        dev_dbg(&intf->dev, "\n");
 732
 733        /*
 734         * In case of dual tuner configuration we need to do some extra
 735         * initialization in order to download firmware to slave demod too,
 736         * which is done by master demod.
 737         * Master feeds also clock and controls power via GPIO.
 738         */
 739        if (state->dual_mode) {
 740                /* configure gpioh1, reset & power slave demod */
 741                ret = af9035_wr_reg_mask(d, 0x00d8b0, 0x01, 0x01);
 742                if (ret < 0)
 743                        goto err;
 744
 745                ret = af9035_wr_reg_mask(d, 0x00d8b1, 0x01, 0x01);
 746                if (ret < 0)
 747                        goto err;
 748
 749                ret = af9035_wr_reg_mask(d, 0x00d8af, 0x00, 0x01);
 750                if (ret < 0)
 751                        goto err;
 752
 753                usleep_range(10000, 50000);
 754
 755                ret = af9035_wr_reg_mask(d, 0x00d8af, 0x01, 0x01);
 756                if (ret < 0)
 757                        goto err;
 758
 759                /* tell the slave I2C address */
 760                tmp = state->eeprom[EEPROM_2ND_DEMOD_ADDR];
 761
 762                /* Use default I2C address if eeprom has no address set */
 763                if (!tmp)
 764                        tmp = 0x1d << 1; /* 8-bit format used by chip */
 765
 766                if ((state->chip_type == 0x9135) ||
 767                                (state->chip_type == 0x9306)) {
 768                        ret = af9035_wr_reg(d, 0x004bfb, tmp);
 769                        if (ret < 0)
 770                                goto err;
 771                } else {
 772                        ret = af9035_wr_reg(d, 0x00417f, tmp);
 773                        if (ret < 0)
 774                                goto err;
 775
 776                        /* enable clock out */
 777                        ret = af9035_wr_reg_mask(d, 0x00d81a, 0x01, 0x01);
 778                        if (ret < 0)
 779                                goto err;
 780                }
 781        }
 782
 783        if (fw->data[0] == 0x01)
 784                ret = af9035_download_firmware_old(d, fw);
 785        else
 786                ret = af9035_download_firmware_new(d, fw);
 787        if (ret < 0)
 788                goto err;
 789
 790        /* firmware loaded, request boot */
 791        req.cmd = CMD_FW_BOOT;
 792        ret = af9035_ctrl_msg(d, &req);
 793        if (ret < 0)
 794                goto err;
 795
 796        /* ensure firmware starts */
 797        wbuf[0] = 1;
 798        ret = af9035_ctrl_msg(d, &req_fw_ver);
 799        if (ret < 0)
 800                goto err;
 801
 802        if (!(rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])) {
 803                dev_err(&intf->dev, "firmware did not run\n");
 804                ret = -ENODEV;
 805                goto err;
 806        }
 807
 808        dev_info(&intf->dev, "firmware version=%d.%d.%d.%d",
 809                 rbuf[0], rbuf[1], rbuf[2], rbuf[3]);
 810
 811        return 0;
 812
 813err:
 814        dev_dbg(&intf->dev, "failed=%d\n", ret);
 815
 816        return ret;
 817}
 818
 819static int af9035_read_config(struct dvb_usb_device *d)
 820{
 821        struct usb_interface *intf = d->intf;
 822        struct state *state = d_to_priv(d);
 823        int ret, i;
 824        u8 tmp;
 825        u16 tmp16;
 826
 827        /* Demod I2C address */
 828        state->af9033_i2c_addr[0] = 0x1c;
 829        state->af9033_i2c_addr[1] = 0x1d;
 830        state->af9033_config[0].adc_multiplier = AF9033_ADC_MULTIPLIER_2X;
 831        state->af9033_config[1].adc_multiplier = AF9033_ADC_MULTIPLIER_2X;
 832        state->af9033_config[0].ts_mode = AF9033_TS_MODE_USB;
 833        state->af9033_config[1].ts_mode = AF9033_TS_MODE_SERIAL;
 834        state->it930x_addresses = 0;
 835
 836        if (state->chip_type == 0x9135) {
 837                /* feed clock for integrated RF tuner */
 838                state->af9033_config[0].dyn0_clk = true;
 839                state->af9033_config[1].dyn0_clk = true;
 840
 841                if (state->chip_version == 0x02) {
 842                        state->af9033_config[0].tuner = AF9033_TUNER_IT9135_60;
 843                        state->af9033_config[1].tuner = AF9033_TUNER_IT9135_60;
 844                } else {
 845                        state->af9033_config[0].tuner = AF9033_TUNER_IT9135_38;
 846                        state->af9033_config[1].tuner = AF9033_TUNER_IT9135_38;
 847                }
 848
 849                if (state->no_eeprom) {
 850                        /* Remote controller to NEC polling by default */
 851                        state->ir_mode = 0x05;
 852                        state->ir_type = 0x00;
 853
 854                        goto skip_eeprom;
 855                }
 856        } else if (state->chip_type == 0x9306) {
 857                /*
 858                 * IT930x is an USB bridge, only single demod-single tuner
 859                 * configurations seen so far.
 860                 */
 861                if ((le16_to_cpu(d->udev->descriptor.idVendor) == USB_VID_AVERMEDIA) &&
 862                    (le16_to_cpu(d->udev->descriptor.idProduct) == USB_PID_AVERMEDIA_TD310)) {
 863                        state->it930x_addresses = 1;
 864                }
 865                return 0;
 866        }
 867
 868        /* Remote controller */
 869        state->ir_mode = state->eeprom[EEPROM_IR_MODE];
 870        state->ir_type = state->eeprom[EEPROM_IR_TYPE];
 871
 872        if (state->dual_mode) {
 873                /* Read 2nd demodulator I2C address. 8-bit format on eeprom */
 874                tmp = state->eeprom[EEPROM_2ND_DEMOD_ADDR];
 875                if (tmp)
 876                        state->af9033_i2c_addr[1] = tmp >> 1;
 877
 878                dev_dbg(&intf->dev, "2nd demod I2C addr=%02x\n",
 879                        state->af9033_i2c_addr[1]);
 880        }
 881
 882        for (i = 0; i < state->dual_mode + 1; i++) {
 883                unsigned int eeprom_offset = 0;
 884
 885                /* tuner */
 886                tmp = state->eeprom[EEPROM_1_TUNER_ID + eeprom_offset];
 887                dev_dbg(&intf->dev, "[%d]tuner=%02x\n", i, tmp);
 888
 889                /* tuner sanity check */
 890                if (state->chip_type == 0x9135) {
 891                        if (state->chip_version == 0x02) {
 892                                /* IT9135 BX (v2) */
 893                                switch (tmp) {
 894                                case AF9033_TUNER_IT9135_60:
 895                                case AF9033_TUNER_IT9135_61:
 896                                case AF9033_TUNER_IT9135_62:
 897                                        state->af9033_config[i].tuner = tmp;
 898                                        break;
 899                                }
 900                        } else {
 901                                /* IT9135 AX (v1) */
 902                                switch (tmp) {
 903                                case AF9033_TUNER_IT9135_38:
 904                                case AF9033_TUNER_IT9135_51:
 905                                case AF9033_TUNER_IT9135_52:
 906                                        state->af9033_config[i].tuner = tmp;
 907                                        break;
 908                                }
 909                        }
 910                } else {
 911                        /* AF9035 */
 912                        state->af9033_config[i].tuner = tmp;
 913                }
 914
 915                if (state->af9033_config[i].tuner != tmp) {
 916                        dev_info(&intf->dev, "[%d] overriding tuner from %02x to %02x\n",
 917                                 i, tmp, state->af9033_config[i].tuner);
 918                }
 919
 920                switch (state->af9033_config[i].tuner) {
 921                case AF9033_TUNER_TUA9001:
 922                case AF9033_TUNER_FC0011:
 923                case AF9033_TUNER_MXL5007T:
 924                case AF9033_TUNER_TDA18218:
 925                case AF9033_TUNER_FC2580:
 926                case AF9033_TUNER_FC0012:
 927                        state->af9033_config[i].spec_inv = 1;
 928                        break;
 929                case AF9033_TUNER_IT9135_38:
 930                case AF9033_TUNER_IT9135_51:
 931                case AF9033_TUNER_IT9135_52:
 932                case AF9033_TUNER_IT9135_60:
 933                case AF9033_TUNER_IT9135_61:
 934                case AF9033_TUNER_IT9135_62:
 935                        break;
 936                default:
 937                        dev_warn(&intf->dev, "tuner id=%02x not supported, please report!",
 938                                 tmp);
 939                }
 940
 941                /* disable dual mode if driver does not support it */
 942                if (i == 1)
 943                        switch (state->af9033_config[i].tuner) {
 944                        case AF9033_TUNER_FC0012:
 945                        case AF9033_TUNER_IT9135_38:
 946                        case AF9033_TUNER_IT9135_51:
 947                        case AF9033_TUNER_IT9135_52:
 948                        case AF9033_TUNER_IT9135_60:
 949                        case AF9033_TUNER_IT9135_61:
 950                        case AF9033_TUNER_IT9135_62:
 951                        case AF9033_TUNER_MXL5007T:
 952                                break;
 953                        default:
 954                                state->dual_mode = false;
 955                                dev_info(&intf->dev, "driver does not support 2nd tuner and will disable it");
 956                }
 957
 958                /* tuner IF frequency */
 959                tmp = state->eeprom[EEPROM_1_IF_L + eeprom_offset];
 960                tmp16 = tmp << 0;
 961                tmp = state->eeprom[EEPROM_1_IF_H + eeprom_offset];
 962                tmp16 |= tmp << 8;
 963                dev_dbg(&intf->dev, "[%d]IF=%d\n", i, tmp16);
 964
 965                eeprom_offset += 0x10; /* shift for the 2nd tuner params */
 966        }
 967
 968skip_eeprom:
 969        /* get demod clock */
 970        ret = af9035_rd_reg(d, 0x00d800, &tmp);
 971        if (ret < 0)
 972                goto err;
 973
 974        tmp = (tmp >> 0) & 0x0f;
 975
 976        for (i = 0; i < ARRAY_SIZE(state->af9033_config); i++) {
 977                if (state->chip_type == 0x9135)
 978                        state->af9033_config[i].clock = clock_lut_it9135[tmp];
 979                else
 980                        state->af9033_config[i].clock = clock_lut_af9035[tmp];
 981        }
 982
 983        state->no_read = false;
 984        /* Some MXL5007T devices cannot properly handle tuner I2C read ops. */
 985        if (state->af9033_config[0].tuner == AF9033_TUNER_MXL5007T &&
 986                le16_to_cpu(d->udev->descriptor.idVendor) == USB_VID_AVERMEDIA)
 987
 988                switch (le16_to_cpu(d->udev->descriptor.idProduct)) {
 989                case USB_PID_AVERMEDIA_A867:
 990                case USB_PID_AVERMEDIA_TWINSTAR:
 991                        dev_info(&intf->dev,
 992                                 "Device may have issues with I2C read operations. Enabling fix.\n");
 993                        state->no_read = true;
 994                        break;
 995                }
 996
 997        return 0;
 998
 999err:
1000        dev_dbg(&intf->dev, "failed=%d\n", ret);
1001
1002        return ret;
1003}
1004
1005static int af9035_tua9001_tuner_callback(struct dvb_usb_device *d,
1006                int cmd, int arg)
1007{
1008        struct usb_interface *intf = d->intf;
1009        int ret;
1010        u8 val;
1011
1012        dev_dbg(&intf->dev, "cmd=%d arg=%d\n", cmd, arg);
1013
1014        /*
1015         * CEN     always enabled by hardware wiring
1016         * RESETN  GPIOT3
1017         * RXEN    GPIOT2
1018         */
1019
1020        switch (cmd) {
1021        case TUA9001_CMD_RESETN:
1022                if (arg)
1023                        val = 0x00;
1024                else
1025                        val = 0x01;
1026
1027                ret = af9035_wr_reg_mask(d, 0x00d8e7, val, 0x01);
1028                if (ret < 0)
1029                        goto err;
1030                break;
1031        case TUA9001_CMD_RXEN:
1032                if (arg)
1033                        val = 0x01;
1034                else
1035                        val = 0x00;
1036
1037                ret = af9035_wr_reg_mask(d, 0x00d8eb, val, 0x01);
1038                if (ret < 0)
1039                        goto err;
1040                break;
1041        }
1042
1043        return 0;
1044
1045err:
1046        dev_dbg(&intf->dev, "failed=%d\n", ret);
1047
1048        return ret;
1049}
1050
1051
1052static int af9035_fc0011_tuner_callback(struct dvb_usb_device *d,
1053                int cmd, int arg)
1054{
1055        struct usb_interface *intf = d->intf;
1056        int ret;
1057
1058        switch (cmd) {
1059        case FC0011_FE_CALLBACK_POWER:
1060                /* Tuner enable */
1061                ret = af9035_wr_reg_mask(d, 0xd8eb, 1, 1);
1062                if (ret < 0)
1063                        goto err;
1064
1065                ret = af9035_wr_reg_mask(d, 0xd8ec, 1, 1);
1066                if (ret < 0)
1067                        goto err;
1068
1069                ret = af9035_wr_reg_mask(d, 0xd8ed, 1, 1);
1070                if (ret < 0)
1071                        goto err;
1072
1073                /* LED */
1074                ret = af9035_wr_reg_mask(d, 0xd8d0, 1, 1);
1075                if (ret < 0)
1076                        goto err;
1077
1078                ret = af9035_wr_reg_mask(d, 0xd8d1, 1, 1);
1079                if (ret < 0)
1080                        goto err;
1081
1082                usleep_range(10000, 50000);
1083                break;
1084        case FC0011_FE_CALLBACK_RESET:
1085                ret = af9035_wr_reg(d, 0xd8e9, 1);
1086                if (ret < 0)
1087                        goto err;
1088
1089                ret = af9035_wr_reg(d, 0xd8e8, 1);
1090                if (ret < 0)
1091                        goto err;
1092
1093                ret = af9035_wr_reg(d, 0xd8e7, 1);
1094                if (ret < 0)
1095                        goto err;
1096
1097                usleep_range(10000, 20000);
1098
1099                ret = af9035_wr_reg(d, 0xd8e7, 0);
1100                if (ret < 0)
1101                        goto err;
1102
1103                usleep_range(10000, 20000);
1104                break;
1105        default:
1106                ret = -EINVAL;
1107                goto err;
1108        }
1109
1110        return 0;
1111
1112err:
1113        dev_dbg(&intf->dev, "failed=%d\n", ret);
1114
1115        return ret;
1116}
1117
1118static int af9035_tuner_callback(struct dvb_usb_device *d, int cmd, int arg)
1119{
1120        struct state *state = d_to_priv(d);
1121
1122        switch (state->af9033_config[0].tuner) {
1123        case AF9033_TUNER_FC0011:
1124                return af9035_fc0011_tuner_callback(d, cmd, arg);
1125        case AF9033_TUNER_TUA9001:
1126                return af9035_tua9001_tuner_callback(d, cmd, arg);
1127        default:
1128                break;
1129        }
1130
1131        return 0;
1132}
1133
1134static int af9035_frontend_callback(void *adapter_priv, int component,
1135                                    int cmd, int arg)
1136{
1137        struct i2c_adapter *adap = adapter_priv;
1138        struct dvb_usb_device *d = i2c_get_adapdata(adap);
1139        struct usb_interface *intf = d->intf;
1140
1141        dev_dbg(&intf->dev, "component=%d cmd=%d arg=%d\n",
1142                component, cmd, arg);
1143
1144        switch (component) {
1145        case DVB_FRONTEND_COMPONENT_TUNER:
1146                return af9035_tuner_callback(d, cmd, arg);
1147        default:
1148                break;
1149        }
1150
1151        return 0;
1152}
1153
1154static int af9035_get_adapter_count(struct dvb_usb_device *d)
1155{
1156        struct state *state = d_to_priv(d);
1157
1158        return state->dual_mode + 1;
1159}
1160
1161static int af9035_frontend_attach(struct dvb_usb_adapter *adap)
1162{
1163        struct state *state = adap_to_priv(adap);
1164        struct dvb_usb_device *d = adap_to_d(adap);
1165        struct usb_interface *intf = d->intf;
1166        int ret;
1167
1168        dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1169
1170        if (!state->af9033_config[adap->id].tuner) {
1171                /* unsupported tuner */
1172                ret = -ENODEV;
1173                goto err;
1174        }
1175
1176        state->af9033_config[adap->id].fe = &adap->fe[0];
1177        state->af9033_config[adap->id].ops = &state->ops;
1178        ret = af9035_add_i2c_dev(d, "af9033", state->af9033_i2c_addr[adap->id],
1179                        &state->af9033_config[adap->id], &d->i2c_adap);
1180        if (ret)
1181                goto err;
1182
1183        if (adap->fe[0] == NULL) {
1184                ret = -ENODEV;
1185                goto err;
1186        }
1187
1188        /* disable I2C-gate */
1189        adap->fe[0]->ops.i2c_gate_ctrl = NULL;
1190        adap->fe[0]->callback = af9035_frontend_callback;
1191
1192        return 0;
1193
1194err:
1195        dev_dbg(&intf->dev, "failed=%d\n", ret);
1196
1197        return ret;
1198}
1199
1200static int it930x_frontend_attach(struct dvb_usb_adapter *adap)
1201{
1202        struct state *state = adap_to_priv(adap);
1203        struct dvb_usb_device *d = adap_to_d(adap);
1204        struct usb_interface *intf = d->intf;
1205        int ret;
1206        struct si2168_config si2168_config;
1207        struct i2c_adapter *adapter;
1208
1209        dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1210
1211        /* I2C master bus 2 clock speed 300k */
1212        ret = af9035_wr_reg(d, 0x00f6a7, 0x07);
1213        if (ret < 0)
1214                goto err;
1215
1216        /* I2C master bus 1,3 clock speed 300k */
1217        ret = af9035_wr_reg(d, 0x00f103, 0x07);
1218        if (ret < 0)
1219                goto err;
1220
1221        /* set gpio11 low */
1222        ret = af9035_wr_reg_mask(d, 0xd8d4, 0x01, 0x01);
1223        if (ret < 0)
1224                goto err;
1225
1226        ret = af9035_wr_reg_mask(d, 0xd8d5, 0x01, 0x01);
1227        if (ret < 0)
1228                goto err;
1229
1230        ret = af9035_wr_reg_mask(d, 0xd8d3, 0x01, 0x01);
1231        if (ret < 0)
1232                goto err;
1233
1234        /* Tuner enable using gpiot2_en, gpiot2_on and gpiot2_o (reset) */
1235        ret = af9035_wr_reg_mask(d, 0xd8b8, 0x01, 0x01);
1236        if (ret < 0)
1237                goto err;
1238
1239        ret = af9035_wr_reg_mask(d, 0xd8b9, 0x01, 0x01);
1240        if (ret < 0)
1241                goto err;
1242
1243        ret = af9035_wr_reg_mask(d, 0xd8b7, 0x00, 0x01);
1244        if (ret < 0)
1245                goto err;
1246
1247        msleep(200);
1248
1249        ret = af9035_wr_reg_mask(d, 0xd8b7, 0x01, 0x01);
1250        if (ret < 0)
1251                goto err;
1252
1253        memset(&si2168_config, 0, sizeof(si2168_config));
1254        si2168_config.i2c_adapter = &adapter;
1255        si2168_config.fe = &adap->fe[0];
1256        si2168_config.ts_mode = SI2168_TS_SERIAL;
1257
1258        state->af9033_config[adap->id].fe = &adap->fe[0];
1259        state->af9033_config[adap->id].ops = &state->ops;
1260        ret = af9035_add_i2c_dev(d, "si2168",
1261                                 it930x_addresses_table[state->it930x_addresses].frontend_i2c_addr,
1262                                 &si2168_config, &d->i2c_adap);
1263        if (ret)
1264                goto err;
1265
1266        if (adap->fe[0] == NULL) {
1267                ret = -ENODEV;
1268                goto err;
1269        }
1270        state->i2c_adapter_demod = adapter;
1271
1272        return 0;
1273
1274err:
1275        dev_dbg(&intf->dev, "failed=%d\n", ret);
1276
1277        return ret;
1278}
1279
1280static int af9035_frontend_detach(struct dvb_usb_adapter *adap)
1281{
1282        struct state *state = adap_to_priv(adap);
1283        struct dvb_usb_device *d = adap_to_d(adap);
1284        struct usb_interface *intf = d->intf;
1285
1286        dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1287
1288        if (adap->id == 1) {
1289                if (state->i2c_client[1])
1290                        af9035_del_i2c_dev(d);
1291        } else if (adap->id == 0) {
1292                if (state->i2c_client[0])
1293                        af9035_del_i2c_dev(d);
1294        }
1295
1296        return 0;
1297}
1298
1299static const struct fc0011_config af9035_fc0011_config = {
1300        .i2c_address = 0x60,
1301};
1302
1303static struct mxl5007t_config af9035_mxl5007t_config[] = {
1304        {
1305                .xtal_freq_hz = MxL_XTAL_24_MHZ,
1306                .if_freq_hz = MxL_IF_4_57_MHZ,
1307                .invert_if = 0,
1308                .loop_thru_enable = 0,
1309                .clk_out_enable = 0,
1310                .clk_out_amp = MxL_CLKOUT_AMP_0_94V,
1311        }, {
1312                .xtal_freq_hz = MxL_XTAL_24_MHZ,
1313                .if_freq_hz = MxL_IF_4_57_MHZ,
1314                .invert_if = 0,
1315                .loop_thru_enable = 1,
1316                .clk_out_enable = 1,
1317                .clk_out_amp = MxL_CLKOUT_AMP_0_94V,
1318        }
1319};
1320
1321static struct tda18218_config af9035_tda18218_config = {
1322        .i2c_address = 0x60,
1323        .i2c_wr_max = 21,
1324};
1325
1326static const struct fc0012_config af9035_fc0012_config[] = {
1327        {
1328                .i2c_address = 0x63,
1329                .xtal_freq = FC_XTAL_36_MHZ,
1330                .dual_master = true,
1331                .loop_through = true,
1332                .clock_out = true,
1333        }, {
1334                .i2c_address = 0x63 | 0x80, /* I2C bus select hack */
1335                .xtal_freq = FC_XTAL_36_MHZ,
1336                .dual_master = true,
1337        }
1338};
1339
1340static int af9035_tuner_attach(struct dvb_usb_adapter *adap)
1341{
1342        struct state *state = adap_to_priv(adap);
1343        struct dvb_usb_device *d = adap_to_d(adap);
1344        struct usb_interface *intf = d->intf;
1345        int ret;
1346        struct dvb_frontend *fe;
1347        struct i2c_msg msg[1];
1348        u8 tuner_addr;
1349
1350        dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1351
1352        /*
1353         * XXX: Hack used in that function: we abuse unused I2C address bit [7]
1354         * to carry info about used I2C bus for dual tuner configuration.
1355         */
1356
1357        switch (state->af9033_config[adap->id].tuner) {
1358        case AF9033_TUNER_TUA9001: {
1359                struct tua9001_platform_data tua9001_pdata = {
1360                        .dvb_frontend = adap->fe[0],
1361                };
1362
1363                /*
1364                 * AF9035 gpiot3 = TUA9001 RESETN
1365                 * AF9035 gpiot2 = TUA9001 RXEN
1366                 */
1367
1368                /* configure gpiot2 and gpiot2 as output */
1369                ret = af9035_wr_reg_mask(d, 0x00d8ec, 0x01, 0x01);
1370                if (ret < 0)
1371                        goto err;
1372
1373                ret = af9035_wr_reg_mask(d, 0x00d8ed, 0x01, 0x01);
1374                if (ret < 0)
1375                        goto err;
1376
1377                ret = af9035_wr_reg_mask(d, 0x00d8e8, 0x01, 0x01);
1378                if (ret < 0)
1379                        goto err;
1380
1381                ret = af9035_wr_reg_mask(d, 0x00d8e9, 0x01, 0x01);
1382                if (ret < 0)
1383                        goto err;
1384
1385                /* attach tuner */
1386                ret = af9035_add_i2c_dev(d, "tua9001", 0x60, &tua9001_pdata,
1387                                         &d->i2c_adap);
1388                if (ret)
1389                        goto err;
1390
1391                fe = adap->fe[0];
1392                break;
1393        }
1394        case AF9033_TUNER_FC0011:
1395                fe = dvb_attach(fc0011_attach, adap->fe[0],
1396                                &d->i2c_adap, &af9035_fc0011_config);
1397                break;
1398        case AF9033_TUNER_MXL5007T:
1399                if (adap->id == 0) {
1400                        ret = af9035_wr_reg(d, 0x00d8e0, 1);
1401                        if (ret < 0)
1402                                goto err;
1403
1404                        ret = af9035_wr_reg(d, 0x00d8e1, 1);
1405                        if (ret < 0)
1406                                goto err;
1407
1408                        ret = af9035_wr_reg(d, 0x00d8df, 0);
1409                        if (ret < 0)
1410                                goto err;
1411
1412                        msleep(30);
1413
1414                        ret = af9035_wr_reg(d, 0x00d8df, 1);
1415                        if (ret < 0)
1416                                goto err;
1417
1418                        msleep(300);
1419
1420                        ret = af9035_wr_reg(d, 0x00d8c0, 1);
1421                        if (ret < 0)
1422                                goto err;
1423
1424                        ret = af9035_wr_reg(d, 0x00d8c1, 1);
1425                        if (ret < 0)
1426                                goto err;
1427
1428                        ret = af9035_wr_reg(d, 0x00d8bf, 0);
1429                        if (ret < 0)
1430                                goto err;
1431
1432                        ret = af9035_wr_reg(d, 0x00d8b4, 1);
1433                        if (ret < 0)
1434                                goto err;
1435
1436                        ret = af9035_wr_reg(d, 0x00d8b5, 1);
1437                        if (ret < 0)
1438                                goto err;
1439
1440                        ret = af9035_wr_reg(d, 0x00d8b3, 1);
1441                        if (ret < 0)
1442                                goto err;
1443
1444                        tuner_addr = 0x60;
1445                } else {
1446                        tuner_addr = 0x60 | 0x80; /* I2C bus hack */
1447                }
1448
1449                /* attach tuner */
1450                fe = dvb_attach(mxl5007t_attach, adap->fe[0], &d->i2c_adap,
1451                                tuner_addr, &af9035_mxl5007t_config[adap->id]);
1452                break;
1453        case AF9033_TUNER_TDA18218:
1454                /* attach tuner */
1455                fe = dvb_attach(tda18218_attach, adap->fe[0],
1456                                &d->i2c_adap, &af9035_tda18218_config);
1457                break;
1458        case AF9033_TUNER_FC2580: {
1459                struct fc2580_platform_data fc2580_pdata = {
1460                        .dvb_frontend = adap->fe[0],
1461                };
1462
1463                /* Tuner enable using gpiot2_o, gpiot2_en and gpiot2_on  */
1464                ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
1465                if (ret < 0)
1466                        goto err;
1467
1468                ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
1469                if (ret < 0)
1470                        goto err;
1471
1472                ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
1473                if (ret < 0)
1474                        goto err;
1475
1476                usleep_range(10000, 50000);
1477                /* attach tuner */
1478                ret = af9035_add_i2c_dev(d, "fc2580", 0x56, &fc2580_pdata,
1479                                         &d->i2c_adap);
1480                if (ret)
1481                        goto err;
1482
1483                fe = adap->fe[0];
1484                break;
1485        }
1486        case AF9033_TUNER_FC0012:
1487                /*
1488                 * AF9035 gpiot2 = FC0012 enable
1489                 * XXX: there seems to be something on gpioh8 too, but on my
1490                 * my test I didn't find any difference.
1491                 */
1492
1493                if (adap->id == 0) {
1494                        /* configure gpiot2 as output and high */
1495                        ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
1496                        if (ret < 0)
1497                                goto err;
1498
1499                        ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
1500                        if (ret < 0)
1501                                goto err;
1502
1503                        ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
1504                        if (ret < 0)
1505                                goto err;
1506                } else {
1507                        /*
1508                         * FIXME: That belongs for the FC0012 driver.
1509                         * Write 02 to FC0012 master tuner register 0d directly
1510                         * in order to make slave tuner working.
1511                         */
1512                        msg[0].addr = 0x63;
1513                        msg[0].flags = 0;
1514                        msg[0].len = 2;
1515                        msg[0].buf = "\x0d\x02";
1516                        ret = i2c_transfer(&d->i2c_adap, msg, 1);
1517                        if (ret < 0)
1518                                goto err;
1519                }
1520
1521                usleep_range(10000, 50000);
1522
1523                fe = dvb_attach(fc0012_attach, adap->fe[0], &d->i2c_adap,
1524                                &af9035_fc0012_config[adap->id]);
1525                break;
1526        case AF9033_TUNER_IT9135_38:
1527        case AF9033_TUNER_IT9135_51:
1528        case AF9033_TUNER_IT9135_52:
1529        case AF9033_TUNER_IT9135_60:
1530        case AF9033_TUNER_IT9135_61:
1531        case AF9033_TUNER_IT9135_62:
1532        {
1533                struct platform_device *pdev;
1534                const char *name;
1535                struct it913x_platform_data it913x_pdata = {
1536                        .regmap = state->af9033_config[adap->id].regmap,
1537                        .fe = adap->fe[0],
1538                };
1539
1540                switch (state->af9033_config[adap->id].tuner) {
1541                case AF9033_TUNER_IT9135_38:
1542                case AF9033_TUNER_IT9135_51:
1543                case AF9033_TUNER_IT9135_52:
1544                        name = "it9133ax-tuner";
1545                        break;
1546                case AF9033_TUNER_IT9135_60:
1547                case AF9033_TUNER_IT9135_61:
1548                case AF9033_TUNER_IT9135_62:
1549                        name = "it9133bx-tuner";
1550                        break;
1551                default:
1552                        ret = -ENODEV;
1553                        goto err;
1554                }
1555
1556                if (state->dual_mode) {
1557                        if (adap->id == 0)
1558                                it913x_pdata.role = IT913X_ROLE_DUAL_MASTER;
1559                        else
1560                                it913x_pdata.role = IT913X_ROLE_DUAL_SLAVE;
1561                } else {
1562                        it913x_pdata.role = IT913X_ROLE_SINGLE;
1563                }
1564
1565                request_module("%s", "it913x");
1566                pdev = platform_device_register_data(&d->intf->dev, name,
1567                                                     PLATFORM_DEVID_AUTO,
1568                                                     &it913x_pdata,
1569                                                     sizeof(it913x_pdata));
1570                if (IS_ERR(pdev) || !pdev->dev.driver) {
1571                        ret = -ENODEV;
1572                        goto err;
1573                }
1574                if (!try_module_get(pdev->dev.driver->owner)) {
1575                        platform_device_unregister(pdev);
1576                        ret = -ENODEV;
1577                        goto err;
1578                }
1579
1580                state->platform_device_tuner[adap->id] = pdev;
1581                fe = adap->fe[0];
1582                break;
1583        }
1584        default:
1585                fe = NULL;
1586        }
1587
1588        if (fe == NULL) {
1589                ret = -ENODEV;
1590                goto err;
1591        }
1592
1593        return 0;
1594
1595err:
1596        dev_dbg(&intf->dev, "failed=%d\n", ret);
1597
1598        return ret;
1599}
1600
1601static int it930x_tuner_attach(struct dvb_usb_adapter *adap)
1602{
1603        struct state *state = adap_to_priv(adap);
1604        struct dvb_usb_device *d = adap_to_d(adap);
1605        struct usb_interface *intf = d->intf;
1606        int ret;
1607        struct si2157_config si2157_config;
1608
1609        dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1610
1611        memset(&si2157_config, 0, sizeof(si2157_config));
1612        si2157_config.fe = adap->fe[0];
1613        si2157_config.if_port = it930x_addresses_table[state->it930x_addresses].tuner_if_port;
1614        ret = af9035_add_i2c_dev(d, "si2157",
1615                                 it930x_addresses_table[state->it930x_addresses].tuner_i2c_addr,
1616                                 &si2157_config, state->i2c_adapter_demod);
1617        if (ret)
1618                goto err;
1619
1620        return 0;
1621
1622err:
1623        dev_dbg(&intf->dev, "failed=%d\n", ret);
1624
1625        return ret;
1626}
1627
1628
1629static int it930x_tuner_detach(struct dvb_usb_adapter *adap)
1630{
1631        struct state *state = adap_to_priv(adap);
1632        struct dvb_usb_device *d = adap_to_d(adap);
1633        struct usb_interface *intf = d->intf;
1634
1635        dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1636
1637        if (adap->id == 1) {
1638                if (state->i2c_client[3])
1639                        af9035_del_i2c_dev(d);
1640        } else if (adap->id == 0) {
1641                if (state->i2c_client[1])
1642                        af9035_del_i2c_dev(d);
1643        }
1644
1645        return 0;
1646}
1647
1648
1649static int af9035_tuner_detach(struct dvb_usb_adapter *adap)
1650{
1651        struct state *state = adap_to_priv(adap);
1652        struct dvb_usb_device *d = adap_to_d(adap);
1653        struct usb_interface *intf = d->intf;
1654
1655        dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1656
1657        switch (state->af9033_config[adap->id].tuner) {
1658        case AF9033_TUNER_TUA9001:
1659        case AF9033_TUNER_FC2580:
1660                if (adap->id == 1) {
1661                        if (state->i2c_client[3])
1662                                af9035_del_i2c_dev(d);
1663                } else if (adap->id == 0) {
1664                        if (state->i2c_client[1])
1665                                af9035_del_i2c_dev(d);
1666                }
1667                break;
1668        case AF9033_TUNER_IT9135_38:
1669        case AF9033_TUNER_IT9135_51:
1670        case AF9033_TUNER_IT9135_52:
1671        case AF9033_TUNER_IT9135_60:
1672        case AF9033_TUNER_IT9135_61:
1673        case AF9033_TUNER_IT9135_62:
1674        {
1675                struct platform_device *pdev;
1676
1677                pdev = state->platform_device_tuner[adap->id];
1678                if (pdev) {
1679                        module_put(pdev->dev.driver->owner);
1680                        platform_device_unregister(pdev);
1681                }
1682                break;
1683        }
1684        }
1685
1686        return 0;
1687}
1688
1689static int af9035_init(struct dvb_usb_device *d)
1690{
1691        struct state *state = d_to_priv(d);
1692        struct usb_interface *intf = d->intf;
1693        int ret, i;
1694        u16 frame_size = (d->udev->speed == USB_SPEED_FULL ? 5 : 87) * 188 / 4;
1695        u8 packet_size = (d->udev->speed == USB_SPEED_FULL ? 64 : 512) / 4;
1696        struct reg_val_mask tab[] = {
1697                { 0x80f99d, 0x01, 0x01 },
1698                { 0x80f9a4, 0x01, 0x01 },
1699                { 0x00dd11, 0x00, 0x20 },
1700                { 0x00dd11, 0x00, 0x40 },
1701                { 0x00dd13, 0x00, 0x20 },
1702                { 0x00dd13, 0x00, 0x40 },
1703                { 0x00dd11, 0x20, 0x20 },
1704                { 0x00dd88, (frame_size >> 0) & 0xff, 0xff},
1705                { 0x00dd89, (frame_size >> 8) & 0xff, 0xff},
1706                { 0x00dd0c, packet_size, 0xff},
1707                { 0x00dd11, state->dual_mode << 6, 0x40 },
1708                { 0x00dd8a, (frame_size >> 0) & 0xff, 0xff},
1709                { 0x00dd8b, (frame_size >> 8) & 0xff, 0xff},
1710                { 0x00dd0d, packet_size, 0xff },
1711                { 0x80f9a3, state->dual_mode, 0x01 },
1712                { 0x80f9cd, state->dual_mode, 0x01 },
1713                { 0x80f99d, 0x00, 0x01 },
1714                { 0x80f9a4, 0x00, 0x01 },
1715        };
1716
1717        dev_dbg(&intf->dev, "USB speed=%d frame_size=%04x packet_size=%02x\n",
1718                d->udev->speed, frame_size, packet_size);
1719
1720        /* init endpoints */
1721        for (i = 0; i < ARRAY_SIZE(tab); i++) {
1722                ret = af9035_wr_reg_mask(d, tab[i].reg, tab[i].val,
1723                                tab[i].mask);
1724                if (ret < 0)
1725                        goto err;
1726        }
1727
1728        return 0;
1729
1730err:
1731        dev_dbg(&intf->dev, "failed=%d\n", ret);
1732
1733        return ret;
1734}
1735
1736static int it930x_init(struct dvb_usb_device *d)
1737{
1738        struct state *state = d_to_priv(d);
1739        struct usb_interface *intf = d->intf;
1740        int ret, i;
1741        u16 frame_size = (d->udev->speed == USB_SPEED_FULL ? 5 : 816) * 188 / 4;
1742        u8 packet_size = (d->udev->speed == USB_SPEED_FULL ? 64 : 512) / 4;
1743        struct reg_val_mask tab[] = {
1744                { 0x00da1a, 0x00, 0x01 }, /* ignore_sync_byte */
1745                { 0x00f41f, 0x04, 0x04 }, /* dvbt_inten */
1746                { 0x00da10, 0x00, 0x01 }, /* mpeg_full_speed */
1747                { 0x00f41a, 0x01, 0x01 }, /* dvbt_en */
1748                { 0x00da1d, 0x01, 0x01 }, /* mp2_sw_rst, reset EP4 */
1749                { 0x00dd11, 0x00, 0x20 }, /* ep4_tx_en, disable EP4 */
1750                { 0x00dd13, 0x00, 0x20 }, /* ep4_tx_nak, disable EP4 NAK */
1751                { 0x00dd11, 0x20, 0x20 }, /* ep4_tx_en, enable EP4 */
1752                { 0x00dd11, 0x00, 0x40 }, /* ep5_tx_en, disable EP5 */
1753                { 0x00dd13, 0x00, 0x40 }, /* ep5_tx_nak, disable EP5 NAK */
1754                { 0x00dd11, state->dual_mode << 6, 0x40 }, /* enable EP5 */
1755                { 0x00dd88, (frame_size >> 0) & 0xff, 0xff},
1756                { 0x00dd89, (frame_size >> 8) & 0xff, 0xff},
1757                { 0x00dd0c, packet_size, 0xff},
1758                { 0x00dd8a, (frame_size >> 0) & 0xff, 0xff},
1759                { 0x00dd8b, (frame_size >> 8) & 0xff, 0xff},
1760                { 0x00dd0d, packet_size, 0xff },
1761                { 0x00da1d, 0x00, 0x01 }, /* mp2_sw_rst, disable */
1762                { 0x00d833, 0x01, 0xff }, /* slew rate ctrl: slew rate boosts */
1763                { 0x00d830, 0x00, 0xff }, /* Bit 0 of output driving control */
1764                { 0x00d831, 0x01, 0xff }, /* Bit 1 of output driving control */
1765                { 0x00d832, 0x00, 0xff }, /* Bit 2 of output driving control */
1766
1767                /* suspend gpio1 for TS-C */
1768                { 0x00d8b0, 0x01, 0xff }, /* gpio1 */
1769                { 0x00d8b1, 0x01, 0xff }, /* gpio1 */
1770                { 0x00d8af, 0x00, 0xff }, /* gpio1 */
1771
1772                /* suspend gpio7 for TS-D */
1773                { 0x00d8c4, 0x01, 0xff }, /* gpio7 */
1774                { 0x00d8c5, 0x01, 0xff }, /* gpio7 */
1775                { 0x00d8c3, 0x00, 0xff }, /* gpio7 */
1776
1777                /* suspend gpio13 for TS-B */
1778                { 0x00d8dc, 0x01, 0xff }, /* gpio13 */
1779                { 0x00d8dd, 0x01, 0xff }, /* gpio13 */
1780                { 0x00d8db, 0x00, 0xff }, /* gpio13 */
1781
1782                /* suspend gpio14 for TS-E */
1783                { 0x00d8e4, 0x01, 0xff }, /* gpio14 */
1784                { 0x00d8e5, 0x01, 0xff }, /* gpio14 */
1785                { 0x00d8e3, 0x00, 0xff }, /* gpio14 */
1786
1787                /* suspend gpio15 for TS-A */
1788                { 0x00d8e8, 0x01, 0xff }, /* gpio15 */
1789                { 0x00d8e9, 0x01, 0xff }, /* gpio15 */
1790                { 0x00d8e7, 0x00, 0xff }, /* gpio15 */
1791
1792                { 0x00da58, 0x00, 0x01 }, /* ts_in_src, serial */
1793                { 0x00da73, 0x01, 0xff }, /* ts0_aggre_mode */
1794                { 0x00da78, 0x47, 0xff }, /* ts0_sync_byte */
1795                { 0x00da4c, 0x01, 0xff }, /* ts0_en */
1796                { 0x00da5a, 0x1f, 0xff }, /* ts_fail_ignore */
1797        };
1798
1799        dev_dbg(&intf->dev, "USB speed=%d frame_size=%04x packet_size=%02x\n",
1800                d->udev->speed, frame_size, packet_size);
1801
1802        /* init endpoints */
1803        for (i = 0; i < ARRAY_SIZE(tab); i++) {
1804                ret = af9035_wr_reg_mask(d, tab[i].reg,
1805                                tab[i].val, tab[i].mask);
1806
1807                if (ret < 0)
1808                        goto err;
1809        }
1810
1811        return 0;
1812err:
1813        dev_dbg(&intf->dev, "failed=%d\n", ret);
1814
1815        return ret;
1816}
1817
1818
1819#if IS_ENABLED(CONFIG_RC_CORE)
1820static int af9035_rc_query(struct dvb_usb_device *d)
1821{
1822        struct usb_interface *intf = d->intf;
1823        int ret;
1824        enum rc_proto proto;
1825        u32 key;
1826        u8 buf[4];
1827        struct usb_req req = { CMD_IR_GET, 0, 0, NULL, 4, buf };
1828
1829        ret = af9035_ctrl_msg(d, &req);
1830        if (ret == 1)
1831                return 0;
1832        else if (ret < 0)
1833                goto err;
1834
1835        if ((buf[2] + buf[3]) == 0xff) {
1836                if ((buf[0] + buf[1]) == 0xff) {
1837                        /* NEC standard 16bit */
1838                        key = RC_SCANCODE_NEC(buf[0], buf[2]);
1839                        proto = RC_PROTO_NEC;
1840                } else {
1841                        /* NEC extended 24bit */
1842                        key = RC_SCANCODE_NECX(buf[0] << 8 | buf[1], buf[2]);
1843                        proto = RC_PROTO_NECX;
1844                }
1845        } else {
1846                /* NEC full code 32bit */
1847                key = RC_SCANCODE_NEC32(buf[0] << 24 | buf[1] << 16 |
1848                                        buf[2] << 8  | buf[3]);
1849                proto = RC_PROTO_NEC32;
1850        }
1851
1852        dev_dbg(&intf->dev, "%*ph\n", 4, buf);
1853
1854        rc_keydown(d->rc_dev, proto, key, 0);
1855
1856        return 0;
1857
1858err:
1859        dev_dbg(&intf->dev, "failed=%d\n", ret);
1860
1861        return ret;
1862}
1863
1864static int af9035_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
1865{
1866        struct state *state = d_to_priv(d);
1867        struct usb_interface *intf = d->intf;
1868
1869        dev_dbg(&intf->dev, "ir_mode=%02x ir_type=%02x\n",
1870                state->ir_mode, state->ir_type);
1871
1872        /* don't activate rc if in HID mode or if not available */
1873        if (state->ir_mode == 0x05) {
1874                switch (state->ir_type) {
1875                case 0: /* NEC */
1876                default:
1877                        rc->allowed_protos = RC_PROTO_BIT_NEC |
1878                                        RC_PROTO_BIT_NECX | RC_PROTO_BIT_NEC32;
1879                        break;
1880                case 1: /* RC6 */
1881                        rc->allowed_protos = RC_PROTO_BIT_RC6_MCE;
1882                        break;
1883                }
1884
1885                rc->query = af9035_rc_query;
1886                rc->interval = 500;
1887
1888                /* load empty to enable rc */
1889                if (!rc->map_name)
1890                        rc->map_name = RC_MAP_EMPTY;
1891        }
1892
1893        return 0;
1894}
1895#else
1896        #define af9035_get_rc_config NULL
1897#endif
1898
1899static int af9035_get_stream_config(struct dvb_frontend *fe, u8 *ts_type,
1900                struct usb_data_stream_properties *stream)
1901{
1902        struct dvb_usb_device *d = fe_to_d(fe);
1903        struct usb_interface *intf = d->intf;
1904
1905        dev_dbg(&intf->dev, "adap=%d\n", fe_to_adap(fe)->id);
1906
1907        if (d->udev->speed == USB_SPEED_FULL)
1908                stream->u.bulk.buffersize = 5 * 188;
1909
1910        return 0;
1911}
1912
1913static int af9035_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
1914{
1915        struct state *state = adap_to_priv(adap);
1916
1917        return state->ops.pid_filter_ctrl(adap->fe[0], onoff);
1918}
1919
1920static int af9035_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
1921                int onoff)
1922{
1923        struct state *state = adap_to_priv(adap);
1924
1925        return state->ops.pid_filter(adap->fe[0], index, pid, onoff);
1926}
1927
1928static int af9035_probe(struct usb_interface *intf,
1929                const struct usb_device_id *id)
1930{
1931        struct usb_device *udev = interface_to_usbdev(intf);
1932        char manufacturer[sizeof("Afatech")];
1933
1934        memset(manufacturer, 0, sizeof(manufacturer));
1935        usb_string(udev, udev->descriptor.iManufacturer,
1936                        manufacturer, sizeof(manufacturer));
1937        /*
1938         * There is two devices having same ID but different chipset. One uses
1939         * AF9015 and the other IT9135 chipset. Only difference seen on lsusb
1940         * is iManufacturer string.
1941         *
1942         * idVendor           0x0ccd TerraTec Electronic GmbH
1943         * idProduct          0x0099
1944         * bcdDevice            2.00
1945         * iManufacturer           1 Afatech
1946         * iProduct                2 DVB-T 2
1947         *
1948         * idVendor           0x0ccd TerraTec Electronic GmbH
1949         * idProduct          0x0099
1950         * bcdDevice            2.00
1951         * iManufacturer           1 ITE Technologies, Inc.
1952         * iProduct                2 DVB-T TV Stick
1953         */
1954        if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VID_TERRATEC) &&
1955                        (le16_to_cpu(udev->descriptor.idProduct) == 0x0099)) {
1956                if (!strcmp("Afatech", manufacturer)) {
1957                        dev_dbg(&udev->dev, "rejecting device\n");
1958                        return -ENODEV;
1959                }
1960        }
1961
1962        return dvb_usbv2_probe(intf, id);
1963}
1964
1965/* interface 0 is used by DVB-T receiver and
1966   interface 1 is for remote controller (HID) */
1967static const struct dvb_usb_device_properties af9035_props = {
1968        .driver_name = KBUILD_MODNAME,
1969        .owner = THIS_MODULE,
1970        .adapter_nr = adapter_nr,
1971        .size_of_priv = sizeof(struct state),
1972
1973        .generic_bulk_ctrl_endpoint = 0x02,
1974        .generic_bulk_ctrl_endpoint_response = 0x81,
1975
1976        .identify_state = af9035_identify_state,
1977        .download_firmware = af9035_download_firmware,
1978
1979        .i2c_algo = &af9035_i2c_algo,
1980        .read_config = af9035_read_config,
1981        .frontend_attach = af9035_frontend_attach,
1982        .frontend_detach = af9035_frontend_detach,
1983        .tuner_attach = af9035_tuner_attach,
1984        .tuner_detach = af9035_tuner_detach,
1985        .init = af9035_init,
1986        .get_rc_config = af9035_get_rc_config,
1987        .get_stream_config = af9035_get_stream_config,
1988
1989        .get_adapter_count = af9035_get_adapter_count,
1990        .adapter = {
1991                {
1992                        .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1993                                DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1994
1995                        .pid_filter_count = 32,
1996                        .pid_filter_ctrl = af9035_pid_filter_ctrl,
1997                        .pid_filter = af9035_pid_filter,
1998
1999                        .stream = DVB_USB_STREAM_BULK(0x84, 6, 87 * 188),
2000                }, {
2001                        .caps = DVB_USB_ADAP_HAS_PID_FILTER |
2002                                DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
2003
2004                        .pid_filter_count = 32,
2005                        .pid_filter_ctrl = af9035_pid_filter_ctrl,
2006                        .pid_filter = af9035_pid_filter,
2007
2008                        .stream = DVB_USB_STREAM_BULK(0x85, 6, 87 * 188),
2009                },
2010        },
2011};
2012
2013static const struct dvb_usb_device_properties it930x_props = {
2014        .driver_name = KBUILD_MODNAME,
2015        .owner = THIS_MODULE,
2016        .adapter_nr = adapter_nr,
2017        .size_of_priv = sizeof(struct state),
2018
2019        .generic_bulk_ctrl_endpoint = 0x02,
2020        .generic_bulk_ctrl_endpoint_response = 0x81,
2021
2022        .identify_state = af9035_identify_state,
2023        .download_firmware = af9035_download_firmware,
2024
2025        .i2c_algo = &af9035_i2c_algo,
2026        .read_config = af9035_read_config,
2027        .frontend_attach = it930x_frontend_attach,
2028        .frontend_detach = af9035_frontend_detach,
2029        .tuner_attach = it930x_tuner_attach,
2030        .tuner_detach = it930x_tuner_detach,
2031        .init = it930x_init,
2032        .get_stream_config = af9035_get_stream_config,
2033
2034        .get_adapter_count = af9035_get_adapter_count,
2035        .adapter = {
2036                {
2037                        .stream = DVB_USB_STREAM_BULK(0x84, 4, 816 * 188),
2038                }, {
2039                        .stream = DVB_USB_STREAM_BULK(0x85, 4, 816 * 188),
2040                },
2041        },
2042};
2043
2044static const struct usb_device_id af9035_id_table[] = {
2045        /* AF9035 devices */
2046        { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_9035,
2047                &af9035_props, "Afatech AF9035 reference design", NULL) },
2048        { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1000,
2049                &af9035_props, "Afatech AF9035 reference design", NULL) },
2050        { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1001,
2051                &af9035_props, "Afatech AF9035 reference design", NULL) },
2052        { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1002,
2053                &af9035_props, "Afatech AF9035 reference design", NULL) },
2054        { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1003,
2055                &af9035_props, "Afatech AF9035 reference design", NULL) },
2056        { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK,
2057                &af9035_props, "TerraTec Cinergy T Stick", NULL) },
2058        { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835,
2059                &af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
2060        { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_B835,
2061                &af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
2062        { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_1867,
2063                &af9035_props, "AVerMedia HD Volar (A867)", NULL) },
2064        { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A867,
2065                &af9035_props, "AVerMedia HD Volar (A867)", NULL) },
2066        { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_TWINSTAR,
2067                &af9035_props, "AVerMedia Twinstar (A825)", NULL) },
2068        { DVB_USB_DEVICE(USB_VID_ASUS, USB_PID_ASUS_U3100MINI_PLUS,
2069                &af9035_props, "Asus U3100Mini Plus", NULL) },
2070        { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00aa,
2071                &af9035_props, "TerraTec Cinergy T Stick (rev. 2)", NULL) },
2072        { DVB_USB_DEVICE(USB_VID_AVERMEDIA, 0x0337,
2073                &af9035_props, "AVerMedia HD Volar (A867)", NULL) },
2074       { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_EVOLVEO_XTRATV_STICK,
2075               &af9035_props, "EVOLVEO XtraTV stick", NULL) },
2076
2077        /* IT9135 devices */
2078        { DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135,
2079                &af9035_props, "ITE 9135 Generic", RC_MAP_IT913X_V1) },
2080        { DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135_9005,
2081                &af9035_props, "ITE 9135(9005) Generic", RC_MAP_IT913X_V2) },
2082        { DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135_9006,
2083                &af9035_props, "ITE 9135(9006) Generic", RC_MAP_IT913X_V1) },
2084        { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835B_1835,
2085                &af9035_props, "Avermedia A835B(1835)", RC_MAP_IT913X_V2) },
2086        { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835B_2835,
2087                &af9035_props, "Avermedia A835B(2835)", RC_MAP_IT913X_V2) },
2088        { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835B_3835,
2089                &af9035_props, "Avermedia A835B(3835)", RC_MAP_IT913X_V2) },
2090        { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835B_4835,
2091                &af9035_props, "Avermedia A835B(4835)", RC_MAP_IT913X_V2) },
2092        { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_TD110,
2093                &af9035_props, "Avermedia AverTV Volar HD 2 (TD110)", RC_MAP_AVERMEDIA_RM_KS) },
2094        { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_H335,
2095                &af9035_props, "Avermedia H335", RC_MAP_IT913X_V2) },
2096        { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_UB499_2T_T09,
2097                &af9035_props, "Kworld UB499-2T T09", RC_MAP_IT913X_V1) },
2098        { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV22_IT9137,
2099                &af9035_props, "Sveon STV22 Dual DVB-T HDTV",
2100                                                        RC_MAP_IT913X_V1) },
2101        { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_CTVDIGDUAL_V2,
2102                &af9035_props, "Digital Dual TV Receiver CTVDIGDUAL_V2",
2103                                                        RC_MAP_IT913X_V1) },
2104        { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_T1,
2105                &af9035_props, "TerraTec T1", RC_MAP_IT913X_V1) },
2106        /* XXX: that same ID [0ccd:0099] is used by af9015 driver too */
2107        { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x0099,
2108                &af9035_props, "TerraTec Cinergy T Stick Dual RC (rev. 2)",
2109                NULL) },
2110        { DVB_USB_DEVICE(USB_VID_LEADTEK, 0x6a05,
2111                &af9035_props, "Leadtek WinFast DTV Dongle Dual", NULL) },
2112        { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xf900,
2113                &af9035_props, "Hauppauge WinTV-MiniStick 2", NULL) },
2114        { DVB_USB_DEVICE(USB_VID_PCTV, USB_PID_PCTV_78E,
2115                &af9035_props, "PCTV AndroiDTV (78e)", RC_MAP_IT913X_V1) },
2116        { DVB_USB_DEVICE(USB_VID_PCTV, USB_PID_PCTV_79E,
2117                &af9035_props, "PCTV microStick (79e)", RC_MAP_IT913X_V2) },
2118
2119        /* IT930x devices */
2120        { DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9303,
2121                &it930x_props, "ITE 9303 Generic", NULL) },
2122        { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_TD310,
2123                &it930x_props, "AVerMedia TD310 DVB-T2", NULL) },
2124        { }
2125};
2126MODULE_DEVICE_TABLE(usb, af9035_id_table);
2127
2128static struct usb_driver af9035_usb_driver = {
2129        .name = KBUILD_MODNAME,
2130        .id_table = af9035_id_table,
2131        .probe = af9035_probe,
2132        .disconnect = dvb_usbv2_disconnect,
2133        .suspend = dvb_usbv2_suspend,
2134        .resume = dvb_usbv2_resume,
2135        .reset_resume = dvb_usbv2_reset_resume,
2136        .no_dynamic_id = 1,
2137        .soft_unbind = 1,
2138};
2139
2140module_usb_driver(af9035_usb_driver);
2141
2142MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
2143MODULE_DESCRIPTION("Afatech AF9035 driver");
2144MODULE_LICENSE("GPL");
2145MODULE_FIRMWARE(AF9035_FIRMWARE_AF9035);
2146MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135_V1);
2147MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135_V2);
2148MODULE_FIRMWARE(AF9035_FIRMWARE_IT9303);
2149