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