linux/drivers/media/usb/dvb-usb-v2/af9035.c
<<
>>
Prefs
   1/*
   2 * Afatech AF9035 DVB USB driver
   3 *
   4 * Copyright (C) 2009 Antti Palosaari <crope@iki.fi>
   5 * Copyright (C) 2012 Antti Palosaari <crope@iki.fi>
   6 *
   7 *    This program is free software; you can redistribute it and/or modify
   8 *    it under the terms of the GNU General Public License as published by
   9 *    the Free Software Foundation; either version 2 of the License, or
  10 *    (at your option) any later version.
  11 *
  12 *    This program is distributed in the hope that it will be useful,
  13 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15 *    GNU General Public License for more details.
  16 *
  17 *    You should have received a copy of the GNU General Public License along
  18 *    with this program; if not, write to the Free Software Foundation, Inc.,
  19 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20 */
  21
  22#include "af9035.h"
  23
  24DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  25
  26static u16 af9035_checksum(const u8 *buf, size_t len)
  27{
  28        size_t i;
  29        u16 checksum = 0;
  30
  31        for (i = 1; i < len; i++) {
  32                if (i % 2)
  33                        checksum += buf[i] << 8;
  34                else
  35                        checksum += buf[i];
  36        }
  37        checksum = ~checksum;
  38
  39        return checksum;
  40}
  41
  42static int af9035_ctrl_msg(struct dvb_usb_device *d, struct usb_req *req)
  43{
  44#define REQ_HDR_LEN 4 /* send header size */
  45#define ACK_HDR_LEN 3 /* rece header size */
  46#define CHECKSUM_LEN 2
  47#define USB_TIMEOUT 2000
  48        struct state *state = d_to_priv(d);
  49        int ret, wlen, rlen;
  50        u16 checksum, tmp_checksum;
  51
  52        mutex_lock(&d->usb_mutex);
  53
  54        /* buffer overflow check */
  55        if (req->wlen > (BUF_LEN - REQ_HDR_LEN - CHECKSUM_LEN) ||
  56                        req->rlen > (BUF_LEN - ACK_HDR_LEN - CHECKSUM_LEN)) {
  57                dev_err(&d->udev->dev, "%s: too much data wlen=%d rlen=%d\n",
  58                                KBUILD_MODNAME, req->wlen, req->rlen);
  59                ret = -EINVAL;
  60                goto exit;
  61        }
  62
  63        state->buf[0] = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN - 1;
  64        state->buf[1] = req->mbox;
  65        state->buf[2] = req->cmd;
  66        state->buf[3] = state->seq++;
  67        memcpy(&state->buf[REQ_HDR_LEN], req->wbuf, req->wlen);
  68
  69        wlen = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN;
  70        rlen = ACK_HDR_LEN + req->rlen + CHECKSUM_LEN;
  71
  72        /* calc and add checksum */
  73        checksum = af9035_checksum(state->buf, state->buf[0] - 1);
  74        state->buf[state->buf[0] - 1] = (checksum >> 8);
  75        state->buf[state->buf[0] - 0] = (checksum & 0xff);
  76
  77        /* no ack for these packets */
  78        if (req->cmd == CMD_FW_DL)
  79                rlen = 0;
  80
  81        ret = dvb_usbv2_generic_rw_locked(d,
  82                        state->buf, wlen, state->buf, rlen);
  83        if (ret)
  84                goto exit;
  85
  86        /* no ack for those packets */
  87        if (req->cmd == CMD_FW_DL)
  88                goto exit;
  89
  90        /* verify checksum */
  91        checksum = af9035_checksum(state->buf, rlen - 2);
  92        tmp_checksum = (state->buf[rlen - 2] << 8) | state->buf[rlen - 1];
  93        if (tmp_checksum != checksum) {
  94                dev_err(&d->udev->dev,
  95                                "%s: command=%02x checksum mismatch (%04x != %04x)\n",
  96                                KBUILD_MODNAME, req->cmd, tmp_checksum,
  97                                checksum);
  98                ret = -EIO;
  99                goto exit;
 100        }
 101
 102        /* check status */
 103        if (state->buf[2]) {
 104                /* fw returns status 1 when IR code was not received */
 105                if (req->cmd == CMD_IR_GET || state->buf[2] == 1) {
 106                        ret = 1;
 107                        goto exit;
 108                }
 109
 110                dev_dbg(&d->udev->dev, "%s: command=%02x failed fw error=%d\n",
 111                                __func__, req->cmd, state->buf[2]);
 112                ret = -EIO;
 113                goto exit;
 114        }
 115
 116        /* read request, copy returned data to return buf */
 117        if (req->rlen)
 118                memcpy(req->rbuf, &state->buf[ACK_HDR_LEN], req->rlen);
 119exit:
 120        mutex_unlock(&d->usb_mutex);
 121        if (ret < 0)
 122                dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
 123        return ret;
 124}
 125
 126/* write multiple registers */
 127static int af9035_wr_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
 128{
 129        u8 wbuf[6 + len];
 130        u8 mbox = (reg >> 16) & 0xff;
 131        struct usb_req req = { CMD_MEM_WR, mbox, sizeof(wbuf), wbuf, 0, NULL };
 132
 133        wbuf[0] = len;
 134        wbuf[1] = 2;
 135        wbuf[2] = 0;
 136        wbuf[3] = 0;
 137        wbuf[4] = (reg >> 8) & 0xff;
 138        wbuf[5] = (reg >> 0) & 0xff;
 139        memcpy(&wbuf[6], val, len);
 140
 141        return af9035_ctrl_msg(d, &req);
 142}
 143
 144/* read multiple registers */
 145static int af9035_rd_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
 146{
 147        u8 wbuf[] = { len, 2, 0, 0, (reg >> 8) & 0xff, reg & 0xff };
 148        u8 mbox = (reg >> 16) & 0xff;
 149        struct usb_req req = { CMD_MEM_RD, mbox, sizeof(wbuf), wbuf, len, val };
 150
 151        return af9035_ctrl_msg(d, &req);
 152}
 153
 154/* write single register */
 155static int af9035_wr_reg(struct dvb_usb_device *d, u32 reg, u8 val)
 156{
 157        return af9035_wr_regs(d, reg, &val, 1);
 158}
 159
 160/* read single register */
 161static int af9035_rd_reg(struct dvb_usb_device *d, u32 reg, u8 *val)
 162{
 163        return af9035_rd_regs(d, reg, val, 1);
 164}
 165
 166/* write single register with mask */
 167static int af9035_wr_reg_mask(struct dvb_usb_device *d, u32 reg, u8 val,
 168                u8 mask)
 169{
 170        int ret;
 171        u8 tmp;
 172
 173        /* no need for read if whole reg is written */
 174        if (mask != 0xff) {
 175                ret = af9035_rd_regs(d, reg, &tmp, 1);
 176                if (ret)
 177                        return ret;
 178
 179                val &= mask;
 180                tmp &= ~mask;
 181                val |= tmp;
 182        }
 183
 184        return af9035_wr_regs(d, reg, &val, 1);
 185}
 186
 187static int af9035_i2c_master_xfer(struct i2c_adapter *adap,
 188                struct i2c_msg msg[], int num)
 189{
 190        struct dvb_usb_device *d = i2c_get_adapdata(adap);
 191        struct state *state = d_to_priv(d);
 192        int ret;
 193
 194        if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
 195                return -EAGAIN;
 196
 197        /*
 198         * I2C sub header is 5 bytes long. Meaning of those bytes are:
 199         * 0: data len
 200         * 1: I2C addr << 1
 201         * 2: reg addr len
 202         *    byte 3 and 4 can be used as reg addr
 203         * 3: reg addr MSB
 204         *    used when reg addr len is set to 2
 205         * 4: reg addr LSB
 206         *    used when reg addr len is set to 1 or 2
 207         *
 208         * For the simplify we do not use register addr at all.
 209         * NOTE: As a firmware knows tuner type there is very small possibility
 210         * there could be some tuner I2C hacks done by firmware and this may
 211         * lead problems if firmware expects those bytes are used.
 212         */
 213        if (num == 2 && !(msg[0].flags & I2C_M_RD) &&
 214                        (msg[1].flags & I2C_M_RD)) {
 215                if (msg[0].len > 40 || msg[1].len > 40) {
 216                        /* TODO: correct limits > 40 */
 217                        ret = -EOPNOTSUPP;
 218                } else if ((msg[0].addr == state->af9033_config[0].i2c_addr) ||
 219                           (msg[0].addr == state->af9033_config[1].i2c_addr)) {
 220                        /* demod access via firmware interface */
 221                        u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
 222                                        msg[0].buf[2];
 223
 224                        if (msg[0].addr == state->af9033_config[1].i2c_addr)
 225                                reg |= 0x100000;
 226
 227                        ret = af9035_rd_regs(d, reg, &msg[1].buf[0],
 228                                        msg[1].len);
 229                } else {
 230                        /* I2C */
 231                        u8 buf[5 + msg[0].len];
 232                        struct usb_req req = { CMD_I2C_RD, 0, sizeof(buf),
 233                                        buf, msg[1].len, msg[1].buf };
 234                        req.mbox |= ((msg[0].addr & 0x80)  >>  3);
 235                        buf[0] = msg[1].len;
 236                        buf[1] = msg[0].addr << 1;
 237                        buf[2] = 0x00; /* reg addr len */
 238                        buf[3] = 0x00; /* reg addr MSB */
 239                        buf[4] = 0x00; /* reg addr LSB */
 240                        memcpy(&buf[5], msg[0].buf, msg[0].len);
 241                        ret = af9035_ctrl_msg(d, &req);
 242                }
 243        } else if (num == 1 && !(msg[0].flags & I2C_M_RD)) {
 244                if (msg[0].len > 40) {
 245                        /* TODO: correct limits > 40 */
 246                        ret = -EOPNOTSUPP;
 247                } else if ((msg[0].addr == state->af9033_config[0].i2c_addr) ||
 248                           (msg[0].addr == state->af9033_config[1].i2c_addr)) {
 249                        /* demod access via firmware interface */
 250                        u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
 251                                        msg[0].buf[2];
 252
 253                        if (msg[0].addr == state->af9033_config[1].i2c_addr)
 254                                reg |= 0x100000;
 255
 256                        ret = af9035_wr_regs(d, reg, &msg[0].buf[3],
 257                                        msg[0].len - 3);
 258                } else {
 259                        /* I2C */
 260                        u8 buf[5 + msg[0].len];
 261                        struct usb_req req = { CMD_I2C_WR, 0, sizeof(buf), buf,
 262                                        0, NULL };
 263                        req.mbox |= ((msg[0].addr & 0x80)  >>  3);
 264                        buf[0] = msg[0].len;
 265                        buf[1] = msg[0].addr << 1;
 266                        buf[2] = 0x00; /* reg addr len */
 267                        buf[3] = 0x00; /* reg addr MSB */
 268                        buf[4] = 0x00; /* reg addr LSB */
 269                        memcpy(&buf[5], msg[0].buf, msg[0].len);
 270                        ret = af9035_ctrl_msg(d, &req);
 271                }
 272        } else if (num == 1 && (msg[0].flags & I2C_M_RD)) {
 273                if (msg[0].len > 40) {
 274                        /* TODO: correct limits > 40 */
 275                        ret = -EOPNOTSUPP;
 276                } else {
 277                        /* I2C */
 278                        u8 buf[5];
 279                        struct usb_req req = { CMD_I2C_RD, 0, sizeof(buf),
 280                                        buf, msg[0].len, msg[0].buf };
 281                        req.mbox |= ((msg[0].addr & 0x80)  >>  3);
 282                        buf[0] = msg[0].len;
 283                        buf[1] = msg[0].addr << 1;
 284                        buf[2] = 0x00; /* reg addr len */
 285                        buf[3] = 0x00; /* reg addr MSB */
 286                        buf[4] = 0x00; /* reg addr LSB */
 287                        ret = af9035_ctrl_msg(d, &req);
 288                }
 289        } else {
 290                /*
 291                 * We support only three kind of I2C transactions:
 292                 * 1) 1 x read + 1 x write (repeated start)
 293                 * 2) 1 x write
 294                 * 3) 1 x read
 295                 */
 296                ret = -EOPNOTSUPP;
 297        }
 298
 299        mutex_unlock(&d->i2c_mutex);
 300
 301        if (ret < 0)
 302                return ret;
 303        else
 304                return num;
 305}
 306
 307static u32 af9035_i2c_functionality(struct i2c_adapter *adapter)
 308{
 309        return I2C_FUNC_I2C;
 310}
 311
 312static struct i2c_algorithm af9035_i2c_algo = {
 313        .master_xfer = af9035_i2c_master_xfer,
 314        .functionality = af9035_i2c_functionality,
 315};
 316
 317static int af9035_identify_state(struct dvb_usb_device *d, const char **name)
 318{
 319        struct state *state = d_to_priv(d);
 320        int ret;
 321        u8 wbuf[1] = { 1 };
 322        u8 rbuf[4];
 323        struct usb_req req = { CMD_FW_QUERYINFO, 0, sizeof(wbuf), wbuf,
 324                        sizeof(rbuf), rbuf };
 325
 326        ret = af9035_rd_regs(d, 0x1222, rbuf, 3);
 327        if (ret < 0)
 328                goto err;
 329
 330        state->chip_version = rbuf[0];
 331        state->chip_type = rbuf[2] << 8 | rbuf[1] << 0;
 332
 333        ret = af9035_rd_reg(d, 0x384f, &state->prechip_version);
 334        if (ret < 0)
 335                goto err;
 336
 337        dev_info(&d->udev->dev,
 338                        "%s: prechip_version=%02x chip_version=%02x chip_type=%04x\n",
 339                        KBUILD_MODNAME, state->prechip_version,
 340                        state->chip_version, state->chip_type);
 341
 342        if (state->chip_type == 0x9135) {
 343                if (state->chip_version == 0x02)
 344                        *name = AF9035_FIRMWARE_IT9135_V2;
 345                else
 346                        *name = AF9035_FIRMWARE_IT9135_V1;
 347                state->eeprom_addr = EEPROM_BASE_IT9135;
 348        } else {
 349                *name = AF9035_FIRMWARE_AF9035;
 350                state->eeprom_addr = EEPROM_BASE_AF9035;
 351        }
 352
 353        ret = af9035_ctrl_msg(d, &req);
 354        if (ret < 0)
 355                goto err;
 356
 357        dev_dbg(&d->udev->dev, "%s: reply=%*ph\n", __func__, 4, rbuf);
 358        if (rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])
 359                ret = WARM;
 360        else
 361                ret = COLD;
 362
 363        return ret;
 364
 365err:
 366        dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
 367
 368        return ret;
 369}
 370
 371static int af9035_download_firmware_old(struct dvb_usb_device *d,
 372                const struct firmware *fw)
 373{
 374        int ret, i, j, len;
 375        u8 wbuf[1];
 376        struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
 377        struct usb_req req_fw_dl = { CMD_FW_DL, 0, 0, wbuf, 0, NULL };
 378        u8 hdr_core;
 379        u16 hdr_addr, hdr_data_len, hdr_checksum;
 380        #define MAX_DATA 58
 381        #define HDR_SIZE 7
 382
 383        /*
 384         * Thanks to Daniel Glöckner <daniel-gl@gmx.net> about that info!
 385         *
 386         * byte 0: MCS 51 core
 387         *  There are two inside the AF9035 (1=Link and 2=OFDM) with separate
 388         *  address spaces
 389         * byte 1-2: Big endian destination address
 390         * byte 3-4: Big endian number of data bytes following the header
 391         * byte 5-6: Big endian header checksum, apparently ignored by the chip
 392         *  Calculated as ~(h[0]*256+h[1]+h[2]*256+h[3]+h[4]*256)
 393         */
 394
 395        for (i = fw->size; i > HDR_SIZE;) {
 396                hdr_core = fw->data[fw->size - i + 0];
 397                hdr_addr = fw->data[fw->size - i + 1] << 8;
 398                hdr_addr |= fw->data[fw->size - i + 2] << 0;
 399                hdr_data_len = fw->data[fw->size - i + 3] << 8;
 400                hdr_data_len |= fw->data[fw->size - i + 4] << 0;
 401                hdr_checksum = fw->data[fw->size - i + 5] << 8;
 402                hdr_checksum |= fw->data[fw->size - i + 6] << 0;
 403
 404                dev_dbg(&d->udev->dev,
 405                                "%s: core=%d addr=%04x data_len=%d checksum=%04x\n",
 406                                __func__, hdr_core, hdr_addr, hdr_data_len,
 407                                hdr_checksum);
 408
 409                if (((hdr_core != 1) && (hdr_core != 2)) ||
 410                                (hdr_data_len > i)) {
 411                        dev_dbg(&d->udev->dev, "%s: bad firmware\n", __func__);
 412                        break;
 413                }
 414
 415                /* download begin packet */
 416                req.cmd = CMD_FW_DL_BEGIN;
 417                ret = af9035_ctrl_msg(d, &req);
 418                if (ret < 0)
 419                        goto err;
 420
 421                /* download firmware packet(s) */
 422                for (j = HDR_SIZE + hdr_data_len; j > 0; j -= MAX_DATA) {
 423                        len = j;
 424                        if (len > MAX_DATA)
 425                                len = MAX_DATA;
 426                        req_fw_dl.wlen = len;
 427                        req_fw_dl.wbuf = (u8 *) &fw->data[fw->size - i +
 428                                        HDR_SIZE + hdr_data_len - j];
 429                        ret = af9035_ctrl_msg(d, &req_fw_dl);
 430                        if (ret < 0)
 431                                goto err;
 432                }
 433
 434                /* download end packet */
 435                req.cmd = CMD_FW_DL_END;
 436                ret = af9035_ctrl_msg(d, &req);
 437                if (ret < 0)
 438                        goto err;
 439
 440                i -= hdr_data_len + HDR_SIZE;
 441
 442                dev_dbg(&d->udev->dev, "%s: data uploaded=%zu\n",
 443                                __func__, fw->size - i);
 444        }
 445
 446        /* print warn if firmware is bad, continue and see what happens */
 447        if (i)
 448                dev_warn(&d->udev->dev, "%s: bad firmware\n", KBUILD_MODNAME);
 449
 450        return 0;
 451
 452err:
 453        dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
 454
 455        return ret;
 456}
 457
 458static int af9035_download_firmware_new(struct dvb_usb_device *d,
 459                const struct firmware *fw)
 460{
 461        int ret, i, i_prev;
 462        struct usb_req req_fw_dl = { CMD_FW_SCATTER_WR, 0, 0, NULL, 0, NULL };
 463        #define HDR_SIZE 7
 464
 465        /*
 466         * There seems to be following firmware header. Meaning of bytes 0-3
 467         * is unknown.
 468         *
 469         * 0: 3
 470         * 1: 0, 1
 471         * 2: 0
 472         * 3: 1, 2, 3
 473         * 4: addr MSB
 474         * 5: addr LSB
 475         * 6: count of data bytes ?
 476         */
 477        for (i = HDR_SIZE, i_prev = 0; i <= fw->size; i++) {
 478                if (i == fw->size ||
 479                                (fw->data[i + 0] == 0x03 &&
 480                                (fw->data[i + 1] == 0x00 ||
 481                                fw->data[i + 1] == 0x01) &&
 482                                fw->data[i + 2] == 0x00)) {
 483                        req_fw_dl.wlen = i - i_prev;
 484                        req_fw_dl.wbuf = (u8 *) &fw->data[i_prev];
 485                        i_prev = i;
 486                        ret = af9035_ctrl_msg(d, &req_fw_dl);
 487                        if (ret < 0)
 488                                goto err;
 489
 490                        dev_dbg(&d->udev->dev, "%s: data uploaded=%d\n",
 491                                        __func__, i);
 492                }
 493        }
 494
 495        return 0;
 496
 497err:
 498        dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
 499
 500        return ret;
 501}
 502
 503static int af9035_download_firmware(struct dvb_usb_device *d,
 504                const struct firmware *fw)
 505{
 506        struct state *state = d_to_priv(d);
 507        int ret;
 508        u8 wbuf[1];
 509        u8 rbuf[4];
 510        u8 tmp;
 511        struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
 512        struct usb_req req_fw_ver = { CMD_FW_QUERYINFO, 0, 1, wbuf, 4, rbuf };
 513        dev_dbg(&d->udev->dev, "%s:\n", __func__);
 514
 515        /*
 516         * In case of dual tuner configuration we need to do some extra
 517         * initialization in order to download firmware to slave demod too,
 518         * which is done by master demod.
 519         * Master feeds also clock and controls power via GPIO.
 520         */
 521        ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_TS_MODE, &tmp);
 522        if (ret < 0)
 523                goto err;
 524
 525        if (tmp == 1 || tmp == 3) {
 526                /* configure gpioh1, reset & power slave demod */
 527                ret = af9035_wr_reg_mask(d, 0x00d8b0, 0x01, 0x01);
 528                if (ret < 0)
 529                        goto err;
 530
 531                ret = af9035_wr_reg_mask(d, 0x00d8b1, 0x01, 0x01);
 532                if (ret < 0)
 533                        goto err;
 534
 535                ret = af9035_wr_reg_mask(d, 0x00d8af, 0x00, 0x01);
 536                if (ret < 0)
 537                        goto err;
 538
 539                usleep_range(10000, 50000);
 540
 541                ret = af9035_wr_reg_mask(d, 0x00d8af, 0x01, 0x01);
 542                if (ret < 0)
 543                        goto err;
 544
 545                /* tell the slave I2C address */
 546                ret = af9035_rd_reg(d,
 547                                state->eeprom_addr + EEPROM_2ND_DEMOD_ADDR,
 548                                &tmp);
 549                if (ret < 0)
 550                        goto err;
 551
 552                if (state->chip_type == 0x9135) {
 553                        ret = af9035_wr_reg(d, 0x004bfb, tmp);
 554                        if (ret < 0)
 555                                goto err;
 556                } else {
 557                        ret = af9035_wr_reg(d, 0x00417f, tmp);
 558                        if (ret < 0)
 559                                goto err;
 560
 561                        /* enable clock out */
 562                        ret = af9035_wr_reg_mask(d, 0x00d81a, 0x01, 0x01);
 563                        if (ret < 0)
 564                                goto err;
 565                }
 566        }
 567
 568        if (fw->data[0] == 0x01)
 569                ret = af9035_download_firmware_old(d, fw);
 570        else
 571                ret = af9035_download_firmware_new(d, fw);
 572        if (ret < 0)
 573                goto err;
 574
 575        /* firmware loaded, request boot */
 576        req.cmd = CMD_FW_BOOT;
 577        ret = af9035_ctrl_msg(d, &req);
 578        if (ret < 0)
 579                goto err;
 580
 581        /* ensure firmware starts */
 582        wbuf[0] = 1;
 583        ret = af9035_ctrl_msg(d, &req_fw_ver);
 584        if (ret < 0)
 585                goto err;
 586
 587        if (!(rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])) {
 588                dev_err(&d->udev->dev, "%s: firmware did not run\n",
 589                                KBUILD_MODNAME);
 590                ret = -ENODEV;
 591                goto err;
 592        }
 593
 594        dev_info(&d->udev->dev, "%s: firmware version=%d.%d.%d.%d",
 595                        KBUILD_MODNAME, rbuf[0], rbuf[1], rbuf[2], rbuf[3]);
 596
 597        return 0;
 598
 599err:
 600        dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
 601
 602        return ret;
 603}
 604
 605static int af9035_read_config(struct dvb_usb_device *d)
 606{
 607        struct state *state = d_to_priv(d);
 608        int ret, i;
 609        u8 tmp;
 610        u16 tmp16, addr;
 611
 612        /* demod I2C "address" */
 613        state->af9033_config[0].i2c_addr = 0x38;
 614        state->af9033_config[0].adc_multiplier = AF9033_ADC_MULTIPLIER_2X;
 615        state->af9033_config[1].adc_multiplier = AF9033_ADC_MULTIPLIER_2X;
 616        state->af9033_config[0].ts_mode = AF9033_TS_MODE_USB;
 617        state->af9033_config[1].ts_mode = AF9033_TS_MODE_SERIAL;
 618
 619        /* eeprom memory mapped location */
 620        if (state->chip_type == 0x9135) {
 621                if (state->chip_version == 0x02) {
 622                        state->af9033_config[0].tuner = AF9033_TUNER_IT9135_60;
 623                        state->af9033_config[1].tuner = AF9033_TUNER_IT9135_60;
 624                        tmp16 = 0x00461d;
 625                } else {
 626                        state->af9033_config[0].tuner = AF9033_TUNER_IT9135_38;
 627                        state->af9033_config[1].tuner = AF9033_TUNER_IT9135_38;
 628                        tmp16 = 0x00461b;
 629                }
 630
 631                /* check if eeprom exists */
 632                ret = af9035_rd_reg(d, tmp16, &tmp);
 633                if (ret < 0)
 634                        goto err;
 635
 636                if (tmp == 0x00) {
 637                        dev_dbg(&d->udev->dev, "%s: no eeprom\n", __func__);
 638                        goto skip_eeprom;
 639                }
 640        }
 641
 642        /* check if there is dual tuners */
 643        ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_TS_MODE, &tmp);
 644        if (ret < 0)
 645                goto err;
 646
 647        if (tmp == 1 || tmp == 3)
 648                state->dual_mode = true;
 649
 650        dev_dbg(&d->udev->dev, "%s: ts mode=%d dual mode=%d\n", __func__,
 651                        tmp, state->dual_mode);
 652
 653        if (state->dual_mode) {
 654                /* read 2nd demodulator I2C address */
 655                ret = af9035_rd_reg(d,
 656                                state->eeprom_addr + EEPROM_2ND_DEMOD_ADDR,
 657                                &tmp);
 658                if (ret < 0)
 659                        goto err;
 660
 661                state->af9033_config[1].i2c_addr = tmp;
 662                dev_dbg(&d->udev->dev, "%s: 2nd demod I2C addr=%02x\n",
 663                                __func__, tmp);
 664        }
 665
 666        addr = state->eeprom_addr;
 667
 668        for (i = 0; i < state->dual_mode + 1; i++) {
 669                /* tuner */
 670                ret = af9035_rd_reg(d, addr + EEPROM_1_TUNER_ID, &tmp);
 671                if (ret < 0)
 672                        goto err;
 673
 674                if (tmp == 0x00)
 675                        dev_dbg(&d->udev->dev,
 676                                        "%s: [%d]tuner not set, using default\n",
 677                                        __func__, i);
 678                else
 679                        state->af9033_config[i].tuner = tmp;
 680
 681                dev_dbg(&d->udev->dev, "%s: [%d]tuner=%02x\n",
 682                                __func__, i, state->af9033_config[i].tuner);
 683
 684                switch (state->af9033_config[i].tuner) {
 685                case AF9033_TUNER_TUA9001:
 686                case AF9033_TUNER_FC0011:
 687                case AF9033_TUNER_MXL5007T:
 688                case AF9033_TUNER_TDA18218:
 689                case AF9033_TUNER_FC2580:
 690                case AF9033_TUNER_FC0012:
 691                        state->af9033_config[i].spec_inv = 1;
 692                        break;
 693                case AF9033_TUNER_IT9135_38:
 694                case AF9033_TUNER_IT9135_51:
 695                case AF9033_TUNER_IT9135_52:
 696                case AF9033_TUNER_IT9135_60:
 697                case AF9033_TUNER_IT9135_61:
 698                case AF9033_TUNER_IT9135_62:
 699                        break;
 700                default:
 701                        dev_warn(&d->udev->dev,
 702                                        "%s: tuner id=%02x not supported, please report!",
 703                                        KBUILD_MODNAME, tmp);
 704                }
 705
 706                /* disable dual mode if driver does not support it */
 707                if (i == 1)
 708                        switch (state->af9033_config[i].tuner) {
 709                        case AF9033_TUNER_FC0012:
 710                        case AF9033_TUNER_IT9135_38:
 711                        case AF9033_TUNER_IT9135_51:
 712                        case AF9033_TUNER_IT9135_52:
 713                        case AF9033_TUNER_IT9135_60:
 714                        case AF9033_TUNER_IT9135_61:
 715                        case AF9033_TUNER_IT9135_62:
 716                        case AF9033_TUNER_MXL5007T:
 717                                break;
 718                        default:
 719                                state->dual_mode = false;
 720                                dev_info(&d->udev->dev,
 721                                                "%s: driver does not support 2nd tuner and will disable it",
 722                                                KBUILD_MODNAME);
 723                }
 724
 725                /* tuner IF frequency */
 726                ret = af9035_rd_reg(d, addr + EEPROM_1_IF_L, &tmp);
 727                if (ret < 0)
 728                        goto err;
 729
 730                tmp16 = tmp;
 731
 732                ret = af9035_rd_reg(d, addr + EEPROM_1_IF_H, &tmp);
 733                if (ret < 0)
 734                        goto err;
 735
 736                tmp16 |= tmp << 8;
 737
 738                dev_dbg(&d->udev->dev, "%s: [%d]IF=%d\n", __func__, i, tmp16);
 739
 740                addr += 0x10; /* shift for the 2nd tuner params */
 741        }
 742
 743skip_eeprom:
 744        /* get demod clock */
 745        ret = af9035_rd_reg(d, 0x00d800, &tmp);
 746        if (ret < 0)
 747                goto err;
 748
 749        tmp = (tmp >> 0) & 0x0f;
 750
 751        for (i = 0; i < ARRAY_SIZE(state->af9033_config); i++) {
 752                if (state->chip_type == 0x9135)
 753                        state->af9033_config[i].clock = clock_lut_it9135[tmp];
 754                else
 755                        state->af9033_config[i].clock = clock_lut_af9035[tmp];
 756        }
 757
 758        return 0;
 759
 760err:
 761        dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
 762
 763        return ret;
 764}
 765
 766static int af9035_tua9001_tuner_callback(struct dvb_usb_device *d,
 767                int cmd, int arg)
 768{
 769        int ret;
 770        u8 val;
 771
 772        dev_dbg(&d->udev->dev, "%s: cmd=%d arg=%d\n", __func__, cmd, arg);
 773
 774        /*
 775         * CEN     always enabled by hardware wiring
 776         * RESETN  GPIOT3
 777         * RXEN    GPIOT2
 778         */
 779
 780        switch (cmd) {
 781        case TUA9001_CMD_RESETN:
 782                if (arg)
 783                        val = 0x00;
 784                else
 785                        val = 0x01;
 786
 787                ret = af9035_wr_reg_mask(d, 0x00d8e7, val, 0x01);
 788                if (ret < 0)
 789                        goto err;
 790                break;
 791        case TUA9001_CMD_RXEN:
 792                if (arg)
 793                        val = 0x01;
 794                else
 795                        val = 0x00;
 796
 797                ret = af9035_wr_reg_mask(d, 0x00d8eb, val, 0x01);
 798                if (ret < 0)
 799                        goto err;
 800                break;
 801        }
 802
 803        return 0;
 804
 805err:
 806        dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
 807
 808        return ret;
 809}
 810
 811
 812static int af9035_fc0011_tuner_callback(struct dvb_usb_device *d,
 813                int cmd, int arg)
 814{
 815        int ret;
 816
 817        switch (cmd) {
 818        case FC0011_FE_CALLBACK_POWER:
 819                /* Tuner enable */
 820                ret = af9035_wr_reg_mask(d, 0xd8eb, 1, 1);
 821                if (ret < 0)
 822                        goto err;
 823
 824                ret = af9035_wr_reg_mask(d, 0xd8ec, 1, 1);
 825                if (ret < 0)
 826                        goto err;
 827
 828                ret = af9035_wr_reg_mask(d, 0xd8ed, 1, 1);
 829                if (ret < 0)
 830                        goto err;
 831
 832                /* LED */
 833                ret = af9035_wr_reg_mask(d, 0xd8d0, 1, 1);
 834                if (ret < 0)
 835                        goto err;
 836
 837                ret = af9035_wr_reg_mask(d, 0xd8d1, 1, 1);
 838                if (ret < 0)
 839                        goto err;
 840
 841                usleep_range(10000, 50000);
 842                break;
 843        case FC0011_FE_CALLBACK_RESET:
 844                ret = af9035_wr_reg(d, 0xd8e9, 1);
 845                if (ret < 0)
 846                        goto err;
 847
 848                ret = af9035_wr_reg(d, 0xd8e8, 1);
 849                if (ret < 0)
 850                        goto err;
 851
 852                ret = af9035_wr_reg(d, 0xd8e7, 1);
 853                if (ret < 0)
 854                        goto err;
 855
 856                usleep_range(10000, 20000);
 857
 858                ret = af9035_wr_reg(d, 0xd8e7, 0);
 859                if (ret < 0)
 860                        goto err;
 861
 862                usleep_range(10000, 20000);
 863                break;
 864        default:
 865                ret = -EINVAL;
 866                goto err;
 867        }
 868
 869        return 0;
 870
 871err:
 872        dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
 873
 874        return ret;
 875}
 876
 877static int af9035_tuner_callback(struct dvb_usb_device *d, int cmd, int arg)
 878{
 879        struct state *state = d_to_priv(d);
 880
 881        switch (state->af9033_config[0].tuner) {
 882        case AF9033_TUNER_FC0011:
 883                return af9035_fc0011_tuner_callback(d, cmd, arg);
 884        case AF9033_TUNER_TUA9001:
 885                return af9035_tua9001_tuner_callback(d, cmd, arg);
 886        default:
 887                break;
 888        }
 889
 890        return 0;
 891}
 892
 893static int af9035_frontend_callback(void *adapter_priv, int component,
 894                                    int cmd, int arg)
 895{
 896        struct i2c_adapter *adap = adapter_priv;
 897        struct dvb_usb_device *d = i2c_get_adapdata(adap);
 898
 899        dev_dbg(&d->udev->dev, "%s: component=%d cmd=%d arg=%d\n",
 900                        __func__, component, cmd, arg);
 901
 902        switch (component) {
 903        case DVB_FRONTEND_COMPONENT_TUNER:
 904                return af9035_tuner_callback(d, cmd, arg);
 905        default:
 906                break;
 907        }
 908
 909        return 0;
 910}
 911
 912static int af9035_get_adapter_count(struct dvb_usb_device *d)
 913{
 914        struct state *state = d_to_priv(d);
 915
 916        /* disable 2nd adapter as we don't have PID filters implemented */
 917        if (d->udev->speed == USB_SPEED_FULL)
 918                return 1;
 919        else
 920                return state->dual_mode + 1;
 921}
 922
 923static int af9035_frontend_attach(struct dvb_usb_adapter *adap)
 924{
 925        struct state *state = adap_to_priv(adap);
 926        struct dvb_usb_device *d = adap_to_d(adap);
 927        int ret;
 928        dev_dbg(&d->udev->dev, "%s:\n", __func__);
 929
 930        if (!state->af9033_config[adap->id].tuner) {
 931                /* unsupported tuner */
 932                ret = -ENODEV;
 933                goto err;
 934        }
 935
 936        /* attach demodulator */
 937        adap->fe[0] = dvb_attach(af9033_attach, &state->af9033_config[adap->id],
 938                        &d->i2c_adap);
 939        if (adap->fe[0] == NULL) {
 940                ret = -ENODEV;
 941                goto err;
 942        }
 943
 944        /* disable I2C-gate */
 945        adap->fe[0]->ops.i2c_gate_ctrl = NULL;
 946        adap->fe[0]->callback = af9035_frontend_callback;
 947
 948        return 0;
 949
 950err:
 951        dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
 952
 953        return ret;
 954}
 955
 956static struct tua9001_config af9035_tua9001_config = {
 957        .i2c_addr = 0x60,
 958};
 959
 960static const struct fc0011_config af9035_fc0011_config = {
 961        .i2c_address = 0x60,
 962};
 963
 964static struct mxl5007t_config af9035_mxl5007t_config[] = {
 965        {
 966                .xtal_freq_hz = MxL_XTAL_24_MHZ,
 967                .if_freq_hz = MxL_IF_4_57_MHZ,
 968                .invert_if = 0,
 969                .loop_thru_enable = 0,
 970                .clk_out_enable = 0,
 971                .clk_out_amp = MxL_CLKOUT_AMP_0_94V,
 972        }, {
 973                .xtal_freq_hz = MxL_XTAL_24_MHZ,
 974                .if_freq_hz = MxL_IF_4_57_MHZ,
 975                .invert_if = 0,
 976                .loop_thru_enable = 1,
 977                .clk_out_enable = 1,
 978                .clk_out_amp = MxL_CLKOUT_AMP_0_94V,
 979        }
 980};
 981
 982static struct tda18218_config af9035_tda18218_config = {
 983        .i2c_address = 0x60,
 984        .i2c_wr_max = 21,
 985};
 986
 987static const struct fc2580_config af9035_fc2580_config = {
 988        .i2c_addr = 0x56,
 989        .clock = 16384000,
 990};
 991
 992static const struct fc0012_config af9035_fc0012_config[] = {
 993        {
 994                .i2c_address = 0x63,
 995                .xtal_freq = FC_XTAL_36_MHZ,
 996                .dual_master = true,
 997                .loop_through = true,
 998                .clock_out = true,
 999        }, {
1000                .i2c_address = 0x63 | 0x80, /* I2C bus select hack */
1001                .xtal_freq = FC_XTAL_36_MHZ,
1002                .dual_master = true,
1003        }
1004};
1005
1006static int af9035_tuner_attach(struct dvb_usb_adapter *adap)
1007{
1008        struct state *state = adap_to_priv(adap);
1009        struct dvb_usb_device *d = adap_to_d(adap);
1010        int ret;
1011        struct dvb_frontend *fe;
1012        struct i2c_msg msg[1];
1013        u8 tuner_addr;
1014        dev_dbg(&d->udev->dev, "%s:\n", __func__);
1015
1016        /*
1017         * XXX: Hack used in that function: we abuse unused I2C address bit [7]
1018         * to carry info about used I2C bus for dual tuner configuration.
1019         */
1020
1021        switch (state->af9033_config[adap->id].tuner) {
1022        case AF9033_TUNER_TUA9001:
1023                /* AF9035 gpiot3 = TUA9001 RESETN
1024                   AF9035 gpiot2 = TUA9001 RXEN */
1025
1026                /* configure gpiot2 and gpiot2 as output */
1027                ret = af9035_wr_reg_mask(d, 0x00d8ec, 0x01, 0x01);
1028                if (ret < 0)
1029                        goto err;
1030
1031                ret = af9035_wr_reg_mask(d, 0x00d8ed, 0x01, 0x01);
1032                if (ret < 0)
1033                        goto err;
1034
1035                ret = af9035_wr_reg_mask(d, 0x00d8e8, 0x01, 0x01);
1036                if (ret < 0)
1037                        goto err;
1038
1039                ret = af9035_wr_reg_mask(d, 0x00d8e9, 0x01, 0x01);
1040                if (ret < 0)
1041                        goto err;
1042
1043                /* attach tuner */
1044                fe = dvb_attach(tua9001_attach, adap->fe[0],
1045                                &d->i2c_adap, &af9035_tua9001_config);
1046                break;
1047        case AF9033_TUNER_FC0011:
1048                fe = dvb_attach(fc0011_attach, adap->fe[0],
1049                                &d->i2c_adap, &af9035_fc0011_config);
1050                break;
1051        case AF9033_TUNER_MXL5007T:
1052                if (adap->id == 0) {
1053                        ret = af9035_wr_reg(d, 0x00d8e0, 1);
1054                        if (ret < 0)
1055                                goto err;
1056
1057                        ret = af9035_wr_reg(d, 0x00d8e1, 1);
1058                        if (ret < 0)
1059                                goto err;
1060
1061                        ret = af9035_wr_reg(d, 0x00d8df, 0);
1062                        if (ret < 0)
1063                                goto err;
1064
1065                        msleep(30);
1066
1067                        ret = af9035_wr_reg(d, 0x00d8df, 1);
1068                        if (ret < 0)
1069                                goto err;
1070
1071                        msleep(300);
1072
1073                        ret = af9035_wr_reg(d, 0x00d8c0, 1);
1074                        if (ret < 0)
1075                                goto err;
1076
1077                        ret = af9035_wr_reg(d, 0x00d8c1, 1);
1078                        if (ret < 0)
1079                                goto err;
1080
1081                        ret = af9035_wr_reg(d, 0x00d8bf, 0);
1082                        if (ret < 0)
1083                                goto err;
1084
1085                        ret = af9035_wr_reg(d, 0x00d8b4, 1);
1086                        if (ret < 0)
1087                                goto err;
1088
1089                        ret = af9035_wr_reg(d, 0x00d8b5, 1);
1090                        if (ret < 0)
1091                                goto err;
1092
1093                        ret = af9035_wr_reg(d, 0x00d8b3, 1);
1094                        if (ret < 0)
1095                                goto err;
1096
1097                        tuner_addr = 0x60;
1098                } else {
1099                        tuner_addr = 0x60 | 0x80; /* I2C bus hack */
1100                }
1101
1102                /* attach tuner */
1103                fe = dvb_attach(mxl5007t_attach, adap->fe[0], &d->i2c_adap,
1104                                tuner_addr, &af9035_mxl5007t_config[adap->id]);
1105                break;
1106        case AF9033_TUNER_TDA18218:
1107                /* attach tuner */
1108                fe = dvb_attach(tda18218_attach, adap->fe[0],
1109                                &d->i2c_adap, &af9035_tda18218_config);
1110                break;
1111        case AF9033_TUNER_FC2580:
1112                /* Tuner enable using gpiot2_o, gpiot2_en and gpiot2_on  */
1113                ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
1114                if (ret < 0)
1115                        goto err;
1116
1117                ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
1118                if (ret < 0)
1119                        goto err;
1120
1121                ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
1122                if (ret < 0)
1123                        goto err;
1124
1125                usleep_range(10000, 50000);
1126                /* attach tuner */
1127                fe = dvb_attach(fc2580_attach, adap->fe[0],
1128                                &d->i2c_adap, &af9035_fc2580_config);
1129                break;
1130        case AF9033_TUNER_FC0012:
1131                /*
1132                 * AF9035 gpiot2 = FC0012 enable
1133                 * XXX: there seems to be something on gpioh8 too, but on my
1134                 * my test I didn't find any difference.
1135                 */
1136
1137                if (adap->id == 0) {
1138                        /* configure gpiot2 as output and high */
1139                        ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
1140                        if (ret < 0)
1141                                goto err;
1142
1143                        ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
1144                        if (ret < 0)
1145                                goto err;
1146
1147                        ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
1148                        if (ret < 0)
1149                                goto err;
1150                } else {
1151                        /*
1152                         * FIXME: That belongs for the FC0012 driver.
1153                         * Write 02 to FC0012 master tuner register 0d directly
1154                         * in order to make slave tuner working.
1155                         */
1156                        msg[0].addr = 0x63;
1157                        msg[0].flags = 0;
1158                        msg[0].len = 2;
1159                        msg[0].buf = "\x0d\x02";
1160                        ret = i2c_transfer(&d->i2c_adap, msg, 1);
1161                        if (ret < 0)
1162                                goto err;
1163                }
1164
1165                usleep_range(10000, 50000);
1166
1167                fe = dvb_attach(fc0012_attach, adap->fe[0], &d->i2c_adap,
1168                                &af9035_fc0012_config[adap->id]);
1169                break;
1170        case AF9033_TUNER_IT9135_38:
1171        case AF9033_TUNER_IT9135_51:
1172        case AF9033_TUNER_IT9135_52:
1173        case AF9033_TUNER_IT9135_60:
1174        case AF9033_TUNER_IT9135_61:
1175        case AF9033_TUNER_IT9135_62:
1176                /* attach tuner */
1177                fe = dvb_attach(it913x_attach, adap->fe[0], &d->i2c_adap,
1178                                state->af9033_config[adap->id].i2c_addr,
1179                                state->af9033_config[0].tuner);
1180                break;
1181        default:
1182                fe = NULL;
1183        }
1184
1185        if (fe == NULL) {
1186                ret = -ENODEV;
1187                goto err;
1188        }
1189
1190        return 0;
1191
1192err:
1193        dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1194
1195        return ret;
1196}
1197
1198static int af9035_init(struct dvb_usb_device *d)
1199{
1200        struct state *state = d_to_priv(d);
1201        int ret, i;
1202        u16 frame_size = (d->udev->speed == USB_SPEED_FULL ? 5 : 87) * 188 / 4;
1203        u8 packet_size = (d->udev->speed == USB_SPEED_FULL ? 64 : 512) / 4;
1204        struct reg_val_mask tab[] = {
1205                { 0x80f99d, 0x01, 0x01 },
1206                { 0x80f9a4, 0x01, 0x01 },
1207                { 0x00dd11, 0x00, 0x20 },
1208                { 0x00dd11, 0x00, 0x40 },
1209                { 0x00dd13, 0x00, 0x20 },
1210                { 0x00dd13, 0x00, 0x40 },
1211                { 0x00dd11, 0x20, 0x20 },
1212                { 0x00dd88, (frame_size >> 0) & 0xff, 0xff},
1213                { 0x00dd89, (frame_size >> 8) & 0xff, 0xff},
1214                { 0x00dd0c, packet_size, 0xff},
1215                { 0x00dd11, state->dual_mode << 6, 0x40 },
1216                { 0x00dd8a, (frame_size >> 0) & 0xff, 0xff},
1217                { 0x00dd8b, (frame_size >> 8) & 0xff, 0xff},
1218                { 0x00dd0d, packet_size, 0xff },
1219                { 0x80f9a3, state->dual_mode, 0x01 },
1220                { 0x80f9cd, state->dual_mode, 0x01 },
1221                { 0x80f99d, 0x00, 0x01 },
1222                { 0x80f9a4, 0x00, 0x01 },
1223        };
1224
1225        dev_dbg(&d->udev->dev,
1226                        "%s: USB speed=%d frame_size=%04x packet_size=%02x\n",
1227                        __func__, d->udev->speed, frame_size, packet_size);
1228
1229        /* init endpoints */
1230        for (i = 0; i < ARRAY_SIZE(tab); i++) {
1231                ret = af9035_wr_reg_mask(d, tab[i].reg, tab[i].val,
1232                                tab[i].mask);
1233                if (ret < 0)
1234                        goto err;
1235        }
1236
1237        return 0;
1238
1239err:
1240        dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1241
1242        return ret;
1243}
1244
1245#if IS_ENABLED(CONFIG_RC_CORE)
1246static int af9035_rc_query(struct dvb_usb_device *d)
1247{
1248        int ret;
1249        u32 key;
1250        u8 buf[4];
1251        struct usb_req req = { CMD_IR_GET, 0, 0, NULL, 4, buf };
1252
1253        ret = af9035_ctrl_msg(d, &req);
1254        if (ret == 1)
1255                return 0;
1256        else if (ret < 0)
1257                goto err;
1258
1259        if ((buf[2] + buf[3]) == 0xff) {
1260                if ((buf[0] + buf[1]) == 0xff) {
1261                        /* NEC standard 16bit */
1262                        key = buf[0] << 8 | buf[2];
1263                } else {
1264                        /* NEC extended 24bit */
1265                        key = buf[0] << 16 | buf[1] << 8 | buf[2];
1266                }
1267        } else {
1268                /* NEC full code 32bit */
1269                key = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1270        }
1271
1272        dev_dbg(&d->udev->dev, "%s: %*ph\n", __func__, 4, buf);
1273
1274        rc_keydown(d->rc_dev, key, 0);
1275
1276        return 0;
1277
1278err:
1279        dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1280
1281        return ret;
1282}
1283
1284static int af9035_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
1285{
1286        struct state *state = d_to_priv(d);
1287        int ret;
1288        u8 tmp;
1289
1290        ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_IR_MODE, &tmp);
1291        if (ret < 0)
1292                goto err;
1293
1294        dev_dbg(&d->udev->dev, "%s: ir_mode=%02x\n", __func__, tmp);
1295
1296        /* don't activate rc if in HID mode or if not available */
1297        if (tmp == 5) {
1298                ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_IR_TYPE,
1299                                &tmp);
1300                if (ret < 0)
1301                        goto err;
1302
1303                dev_dbg(&d->udev->dev, "%s: ir_type=%02x\n", __func__, tmp);
1304
1305                switch (tmp) {
1306                case 0: /* NEC */
1307                default:
1308                        rc->allowed_protos = RC_BIT_NEC;
1309                        break;
1310                case 1: /* RC6 */
1311                        rc->allowed_protos = RC_BIT_RC6_MCE;
1312                        break;
1313                }
1314
1315                rc->query = af9035_rc_query;
1316                rc->interval = 500;
1317
1318                /* load empty to enable rc */
1319                if (!rc->map_name)
1320                        rc->map_name = RC_MAP_EMPTY;
1321        }
1322
1323        return 0;
1324
1325err:
1326        dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1327
1328        return ret;
1329}
1330#else
1331        #define af9035_get_rc_config NULL
1332#endif
1333
1334static int af9035_get_stream_config(struct dvb_frontend *fe, u8 *ts_type,
1335                struct usb_data_stream_properties *stream)
1336{
1337        struct dvb_usb_device *d = fe_to_d(fe);
1338        dev_dbg(&d->udev->dev, "%s: adap=%d\n", __func__, fe_to_adap(fe)->id);
1339
1340        if (d->udev->speed == USB_SPEED_FULL)
1341                stream->u.bulk.buffersize = 5 * 188;
1342
1343        return 0;
1344}
1345
1346/*
1347 * FIXME: PID filter is property of demodulator and should be moved to the
1348 * correct driver. Also we support only adapter #0 PID filter and will
1349 * disable adapter #1 if USB1.1 is used.
1350 */
1351static int af9035_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
1352{
1353        struct dvb_usb_device *d = adap_to_d(adap);
1354        int ret;
1355
1356        dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff);
1357
1358        ret = af9035_wr_reg_mask(d, 0x80f993, onoff, 0x01);
1359        if (ret < 0)
1360                goto err;
1361
1362        return 0;
1363
1364err:
1365        dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1366
1367        return ret;
1368}
1369
1370static int af9035_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
1371                int onoff)
1372{
1373        struct dvb_usb_device *d = adap_to_d(adap);
1374        int ret;
1375        u8 wbuf[2] = {(pid >> 0) & 0xff, (pid >> 8) & 0xff};
1376
1377        dev_dbg(&d->udev->dev, "%s: index=%d pid=%04x onoff=%d\n",
1378                        __func__, index, pid, onoff);
1379
1380        ret = af9035_wr_regs(d, 0x80f996, wbuf, 2);
1381        if (ret < 0)
1382                goto err;
1383
1384        ret = af9035_wr_reg(d, 0x80f994, onoff);
1385        if (ret < 0)
1386                goto err;
1387
1388        ret = af9035_wr_reg(d, 0x80f995, index);
1389        if (ret < 0)
1390                goto err;
1391
1392        return 0;
1393
1394err:
1395        dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1396
1397        return ret;
1398}
1399
1400static int af9035_probe(struct usb_interface *intf,
1401                const struct usb_device_id *id)
1402{
1403        struct usb_device *udev = interface_to_usbdev(intf);
1404        char manufacturer[sizeof("Afatech")];
1405
1406        memset(manufacturer, 0, sizeof(manufacturer));
1407        usb_string(udev, udev->descriptor.iManufacturer,
1408                        manufacturer, sizeof(manufacturer));
1409        /*
1410         * There is two devices having same ID but different chipset. One uses
1411         * AF9015 and the other IT9135 chipset. Only difference seen on lsusb
1412         * is iManufacturer string.
1413         *
1414         * idVendor           0x0ccd TerraTec Electronic GmbH
1415         * idProduct          0x0099
1416         * bcdDevice            2.00
1417         * iManufacturer           1 Afatech
1418         * iProduct                2 DVB-T 2
1419         *
1420         * idVendor           0x0ccd TerraTec Electronic GmbH
1421         * idProduct          0x0099
1422         * bcdDevice            2.00
1423         * iManufacturer           1 ITE Technologies, Inc.
1424         * iProduct                2 DVB-T TV Stick
1425         */
1426        if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VID_TERRATEC) &&
1427                        (le16_to_cpu(udev->descriptor.idProduct) == 0x0099)) {
1428                if (!strcmp("Afatech", manufacturer)) {
1429                        dev_dbg(&udev->dev, "%s: rejecting device\n", __func__);
1430                        return -ENODEV;
1431                }
1432        }
1433
1434        return dvb_usbv2_probe(intf, id);
1435}
1436
1437/* interface 0 is used by DVB-T receiver and
1438   interface 1 is for remote controller (HID) */
1439static const struct dvb_usb_device_properties af9035_props = {
1440        .driver_name = KBUILD_MODNAME,
1441        .owner = THIS_MODULE,
1442        .adapter_nr = adapter_nr,
1443        .size_of_priv = sizeof(struct state),
1444
1445        .generic_bulk_ctrl_endpoint = 0x02,
1446        .generic_bulk_ctrl_endpoint_response = 0x81,
1447
1448        .identify_state = af9035_identify_state,
1449        .download_firmware = af9035_download_firmware,
1450
1451        .i2c_algo = &af9035_i2c_algo,
1452        .read_config = af9035_read_config,
1453        .frontend_attach = af9035_frontend_attach,
1454        .tuner_attach = af9035_tuner_attach,
1455        .init = af9035_init,
1456        .get_rc_config = af9035_get_rc_config,
1457        .get_stream_config = af9035_get_stream_config,
1458
1459        .get_adapter_count = af9035_get_adapter_count,
1460        .adapter = {
1461                {
1462                        .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1463                                DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1464
1465                        .pid_filter_count = 32,
1466                        .pid_filter_ctrl = af9035_pid_filter_ctrl,
1467                        .pid_filter = af9035_pid_filter,
1468
1469                        .stream = DVB_USB_STREAM_BULK(0x84, 6, 87 * 188),
1470                }, {
1471                        .stream = DVB_USB_STREAM_BULK(0x85, 6, 87 * 188),
1472                },
1473        },
1474};
1475
1476static const struct usb_device_id af9035_id_table[] = {
1477        /* AF9035 devices */
1478        { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_9035,
1479                &af9035_props, "Afatech AF9035 reference design", NULL) },
1480        { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1000,
1481                &af9035_props, "Afatech AF9035 reference design", NULL) },
1482        { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1001,
1483                &af9035_props, "Afatech AF9035 reference design", NULL) },
1484        { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1002,
1485                &af9035_props, "Afatech AF9035 reference design", NULL) },
1486        { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1003,
1487                &af9035_props, "Afatech AF9035 reference design", NULL) },
1488        { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK,
1489                &af9035_props, "TerraTec Cinergy T Stick", NULL) },
1490        { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835,
1491                &af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
1492        { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_B835,
1493                &af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
1494        { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_1867,
1495                &af9035_props, "AVerMedia HD Volar (A867)", NULL) },
1496        { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A867,
1497                &af9035_props, "AVerMedia HD Volar (A867)", NULL) },
1498        { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_TWINSTAR,
1499                &af9035_props, "AVerMedia Twinstar (A825)", NULL) },
1500        { DVB_USB_DEVICE(USB_VID_ASUS, USB_PID_ASUS_U3100MINI_PLUS,
1501                &af9035_props, "Asus U3100Mini Plus", NULL) },
1502        { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00aa,
1503                &af9035_props, "TerraTec Cinergy T Stick (rev. 2)", NULL) },
1504        /* IT9135 devices */
1505#if 0
1506        { DVB_USB_DEVICE(0x048d, 0x9135,
1507                &af9035_props, "IT9135 reference design", NULL) },
1508        { DVB_USB_DEVICE(0x048d, 0x9006,
1509                &af9035_props, "IT9135 reference design", NULL) },
1510#endif
1511        /* XXX: that same ID [0ccd:0099] is used by af9015 driver too */
1512        { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x0099,
1513                &af9035_props, "TerraTec Cinergy T Stick Dual RC (rev. 2)", NULL) },
1514        { }
1515};
1516MODULE_DEVICE_TABLE(usb, af9035_id_table);
1517
1518static struct usb_driver af9035_usb_driver = {
1519        .name = KBUILD_MODNAME,
1520        .id_table = af9035_id_table,
1521        .probe = af9035_probe,
1522        .disconnect = dvb_usbv2_disconnect,
1523        .suspend = dvb_usbv2_suspend,
1524        .resume = dvb_usbv2_resume,
1525        .reset_resume = dvb_usbv2_reset_resume,
1526        .no_dynamic_id = 1,
1527        .soft_unbind = 1,
1528};
1529
1530module_usb_driver(af9035_usb_driver);
1531
1532MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1533MODULE_DESCRIPTION("Afatech AF9035 driver");
1534MODULE_LICENSE("GPL");
1535MODULE_FIRMWARE(AF9035_FIRMWARE_AF9035);
1536MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135_V1);
1537MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135_V2);
1538