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