1 /* 2 * DVB USB Linux driver for Afatech AF9015 DVB-T USB2.0 receiver 3 * 4 * Copyright (C) 2007 Antti Palosaari <crope@iki.fi> 5 * 6 * Thanks to Afatech who kindly provided information. 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; either version 2 of the License, or 11 * (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 */ 19 20 #include "af9015.h" 21 22 static int dvb_usb_af9015_remote; 23 module_param_named(remote, dvb_usb_af9015_remote, int, 0644); 24 MODULE_PARM_DESC(remote, "select remote"); 25 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr); 26 27 static int af9015_ctrl_msg(struct dvb_usb_device *d, struct req_t *req) 28 { 29 #define REQ_HDR_LEN 8 /* send header size */ 30 #define ACK_HDR_LEN 2 /* rece header size */ 31 struct af9015_state *state = d_to_priv(d); 32 int ret, wlen, rlen; 33 u8 write = 1; 34 35 mutex_lock(&d->usb_mutex); 36 37 state->buf[0] = req->cmd; 38 state->buf[1] = state->seq++; 39 state->buf[2] = req->i2c_addr << 1; 40 state->buf[3] = req->addr >> 8; 41 state->buf[4] = req->addr & 0xff; 42 state->buf[5] = req->mbox; 43 state->buf[6] = req->addr_len; 44 state->buf[7] = req->data_len; 45 46 switch (req->cmd) { 47 case GET_CONFIG: 48 case READ_MEMORY: 49 case RECONNECT_USB: 50 write = 0; 51 break; 52 case READ_I2C: 53 write = 0; 54 state->buf[2] |= 0x01; /* set I2C direction */ 55 /* fall through */ 56 case WRITE_I2C: 57 state->buf[0] = READ_WRITE_I2C; 58 break; 59 case WRITE_MEMORY: 60 if (((req->addr & 0xff00) == 0xff00) || 61 ((req->addr & 0xff00) == 0xae00)) 62 state->buf[0] = WRITE_VIRTUAL_MEMORY; 63 case WRITE_VIRTUAL_MEMORY: 64 case COPY_FIRMWARE: 65 case DOWNLOAD_FIRMWARE: 66 case BOOT: 67 break; 68 default: 69 dev_err(&d->udev->dev, "%s: unknown command=%d\n", 70 KBUILD_MODNAME, req->cmd); 71 ret = -EIO; 72 goto error; 73 } 74 75 /* buffer overflow check */ 76 if ((write && (req->data_len > BUF_LEN - REQ_HDR_LEN)) || 77 (!write && (req->data_len > BUF_LEN - ACK_HDR_LEN))) { 78 dev_err(&d->udev->dev, "%s: too much data; cmd=%d len=%d\n", 79 KBUILD_MODNAME, req->cmd, req->data_len); 80 ret = -EINVAL; 81 goto error; 82 } 83 84 /* write receives seq + status = 2 bytes 85 read receives seq + status + data = 2 + N bytes */ 86 wlen = REQ_HDR_LEN; 87 rlen = ACK_HDR_LEN; 88 if (write) { 89 wlen += req->data_len; 90 memcpy(&state->buf[REQ_HDR_LEN], req->data, req->data_len); 91 } else { 92 rlen += req->data_len; 93 } 94 95 /* no ack for these packets */ 96 if (req->cmd == DOWNLOAD_FIRMWARE || req->cmd == RECONNECT_USB) 97 rlen = 0; 98 99 ret = dvb_usbv2_generic_rw_locked(d, 100 state->buf, wlen, state->buf, rlen); 101 if (ret) 102 goto error; 103 104 /* check status */ 105 if (rlen && state->buf[1]) { 106 dev_err(&d->udev->dev, "%s: command failed=%d\n", 107 KBUILD_MODNAME, state->buf[1]); 108 ret = -EIO; 109 goto error; 110 } 111 112 /* read request, copy returned data to return buf */ 113 if (!write) 114 memcpy(req->data, &state->buf[ACK_HDR_LEN], req->data_len); 115 error: 116 mutex_unlock(&d->usb_mutex); 117 118 return ret; 119 } 120 121 static int af9015_write_regs(struct dvb_usb_device *d, u16 addr, u8 *val, 122 u8 len) 123 { 124 struct req_t req = {WRITE_MEMORY, AF9015_I2C_DEMOD, addr, 0, 0, len, 125 val}; 126 return af9015_ctrl_msg(d, &req); 127 } 128 129 static int af9015_read_regs(struct dvb_usb_device *d, u16 addr, u8 *val, u8 len) 130 { 131 struct req_t req = {READ_MEMORY, AF9015_I2C_DEMOD, addr, 0, 0, len, 132 val}; 133 return af9015_ctrl_msg(d, &req); 134 } 135 136 static int af9015_write_reg(struct dvb_usb_device *d, u16 addr, u8 val) 137 { 138 return af9015_write_regs(d, addr, &val, 1); 139 } 140 141 static int af9015_read_reg(struct dvb_usb_device *d, u16 addr, u8 *val) 142 { 143 return af9015_read_regs(d, addr, val, 1); 144 } 145 146 static int af9015_write_reg_i2c(struct dvb_usb_device *d, u8 addr, u16 reg, 147 u8 val) 148 { 149 struct af9015_state *state = d_to_priv(d); 150 struct req_t req = {WRITE_I2C, addr, reg, 1, 1, 1, &val}; 151 152 if (addr == state->af9013_config[0].i2c_addr || 153 addr == state->af9013_config[1].i2c_addr) 154 req.addr_len = 3; 155 156 return af9015_ctrl_msg(d, &req); 157 } 158 159 static int af9015_read_reg_i2c(struct dvb_usb_device *d, u8 addr, u16 reg, 160 u8 *val) 161 { 162 struct af9015_state *state = d_to_priv(d); 163 struct req_t req = {READ_I2C, addr, reg, 0, 1, 1, val}; 164 165 if (addr == state->af9013_config[0].i2c_addr || 166 addr == state->af9013_config[1].i2c_addr) 167 req.addr_len = 3; 168 169 return af9015_ctrl_msg(d, &req); 170 } 171 172 static int af9015_do_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit, u8 op) 173 { 174 int ret; 175 u8 val, mask = 0x01; 176 177 ret = af9015_read_reg(d, addr, &val); 178 if (ret) 179 return ret; 180 181 mask <<= bit; 182 if (op) { 183 /* set bit */ 184 val |= mask; 185 } else { 186 /* clear bit */ 187 mask ^= 0xff; 188 val &= mask; 189 } 190 191 return af9015_write_reg(d, addr, val); 192 } 193 194 static int af9015_set_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit) 195 { 196 return af9015_do_reg_bit(d, addr, bit, 1); 197 } 198 199 static int af9015_clear_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit) 200 { 201 return af9015_do_reg_bit(d, addr, bit, 0); 202 } 203 204 static int af9015_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[], 205 int num) 206 { 207 struct dvb_usb_device *d = i2c_get_adapdata(adap); 208 struct af9015_state *state = d_to_priv(d); 209 int ret; 210 u16 addr; 211 u8 mbox, addr_len; 212 struct req_t req; 213 214 /* 215 The bus lock is needed because there is two tuners both using same I2C-address. 216 Due to that the only way to select correct tuner is use demodulator I2C-gate. 217 218 ................................................ 219 . AF9015 includes integrated AF9013 demodulator. 220 . ____________ ____________ . ____________ 221 .| uC | | demod | . | tuner | 222 .|------------| |------------| . |------------| 223 .| AF9015 | | AF9013/5 | . | MXL5003 | 224 .| |--+----I2C-------|-----/ -----|-.-----I2C-------| | 225 .| | | | addr 0x38 | . | addr 0xc6 | 226 .|____________| | |____________| . |____________| 227 .................|.............................. 228 | ____________ ____________ 229 | | demod | | tuner | 230 | |------------| |------------| 231 | | AF9013 | | MXL5003 | 232 +----I2C-------|-----/ -----|-------I2C-------| | 233 | addr 0x3a | | addr 0xc6 | 234 |____________| |____________| 235 */ 236 237 if (msg[0].len == 0 || msg[0].flags & I2C_M_RD) { 238 addr = 0x0000; 239 mbox = 0; 240 addr_len = 0; 241 } else if (msg[0].len == 1) { 242 addr = msg[0].buf[0]; 243 mbox = 0; 244 addr_len = 1; 245 } else if (msg[0].len == 2) { 246 addr = msg[0].buf[0] << 8|msg[0].buf[1] << 0; 247 mbox = 0; 248 addr_len = 2; 249 } else { 250 addr = msg[0].buf[0] << 8|msg[0].buf[1] << 0; 251 mbox = msg[0].buf[2]; 252 addr_len = 3; 253 } 254 255 if (num == 1 && !(msg[0].flags & I2C_M_RD)) { 256 /* i2c write */ 257 if (msg[0].len > 21) { 258 ret = -EOPNOTSUPP; 259 goto err; 260 } 261 if (msg[0].addr == state->af9013_config[0].i2c_addr) 262 req.cmd = WRITE_MEMORY; 263 else 264 req.cmd = WRITE_I2C; 265 req.i2c_addr = msg[0].addr; 266 req.addr = addr; 267 req.mbox = mbox; 268 req.addr_len = addr_len; 269 req.data_len = msg[0].len-addr_len; 270 req.data = &msg[0].buf[addr_len]; 271 ret = af9015_ctrl_msg(d, &req); 272 } else if (num == 2 && !(msg[0].flags & I2C_M_RD) && 273 (msg[1].flags & I2C_M_RD)) { 274 /* i2c write + read */ 275 if (msg[0].len > 3 || msg[1].len > 61) { 276 ret = -EOPNOTSUPP; 277 goto err; 278 } 279 if (msg[0].addr == state->af9013_config[0].i2c_addr) 280 req.cmd = READ_MEMORY; 281 else 282 req.cmd = READ_I2C; 283 req.i2c_addr = msg[0].addr; 284 req.addr = addr; 285 req.mbox = mbox; 286 req.addr_len = addr_len; 287 req.data_len = msg[1].len; 288 req.data = &msg[1].buf[0]; 289 ret = af9015_ctrl_msg(d, &req); 290 } else if (num == 1 && (msg[0].flags & I2C_M_RD)) { 291 /* i2c read */ 292 if (msg[0].len > 61) { 293 ret = -EOPNOTSUPP; 294 goto err; 295 } 296 if (msg[0].addr == state->af9013_config[0].i2c_addr) { 297 ret = -EINVAL; 298 goto err; 299 } 300 req.cmd = READ_I2C; 301 req.i2c_addr = msg[0].addr; 302 req.addr = addr; 303 req.mbox = mbox; 304 req.addr_len = addr_len; 305 req.data_len = msg[0].len; 306 req.data = &msg[0].buf[0]; 307 ret = af9015_ctrl_msg(d, &req); 308 } else { 309 ret = -EOPNOTSUPP; 310 dev_dbg(&d->udev->dev, "%s: unknown msg, num %u\n", 311 __func__, num); 312 } 313 if (ret) 314 goto err; 315 316 return num; 317 err: 318 dev_dbg(&d->udev->dev, "%s: failed %d\n", __func__, ret); 319 return ret; 320 } 321 322 static u32 af9015_i2c_func(struct i2c_adapter *adapter) 323 { 324 return I2C_FUNC_I2C; 325 } 326 327 static struct i2c_algorithm af9015_i2c_algo = { 328 .master_xfer = af9015_i2c_xfer, 329 .functionality = af9015_i2c_func, 330 }; 331 332 static int af9015_identify_state(struct dvb_usb_device *d, const char **name) 333 { 334 int ret; 335 u8 reply; 336 struct req_t req = {GET_CONFIG, 0, 0, 0, 0, 1, &reply}; 337 338 ret = af9015_ctrl_msg(d, &req); 339 if (ret) 340 return ret; 341 342 dev_dbg(&d->udev->dev, "%s: reply=%02x\n", __func__, reply); 343 344 if (reply == 0x02) 345 ret = WARM; 346 else 347 ret = COLD; 348 349 return ret; 350 } 351 352 static int af9015_download_firmware(struct dvb_usb_device *d, 353 const struct firmware *fw) 354 { 355 struct af9015_state *state = d_to_priv(d); 356 int i, len, remaining, ret; 357 struct req_t req = {DOWNLOAD_FIRMWARE, 0, 0, 0, 0, 0, NULL}; 358 u16 checksum = 0; 359 dev_dbg(&d->udev->dev, "%s:\n", __func__); 360 361 /* calc checksum */ 362 for (i = 0; i < fw->size; i++) 363 checksum += fw->data[i]; 364 365 state->firmware_size = fw->size; 366 state->firmware_checksum = checksum; 367 368 #define FW_ADDR 0x5100 /* firmware start address */ 369 #define LEN_MAX 55 /* max packet size */ 370 for (remaining = fw->size; remaining > 0; remaining -= LEN_MAX) { 371 len = remaining; 372 if (len > LEN_MAX) 373 len = LEN_MAX; 374 375 req.data_len = len; 376 req.data = (u8 *) &fw->data[fw->size - remaining]; 377 req.addr = FW_ADDR + fw->size - remaining; 378 379 ret = af9015_ctrl_msg(d, &req); 380 if (ret) { 381 dev_err(&d->udev->dev, 382 "%s: firmware download failed=%d\n", 383 KBUILD_MODNAME, ret); 384 goto error; 385 } 386 } 387 388 /* firmware loaded, request boot */ 389 req.cmd = BOOT; 390 req.data_len = 0; 391 ret = af9015_ctrl_msg(d, &req); 392 if (ret) { 393 dev_err(&d->udev->dev, "%s: firmware boot failed=%d\n", 394 KBUILD_MODNAME, ret); 395 goto error; 396 } 397 398 error: 399 return ret; 400 } 401 402 #define AF9015_EEPROM_SIZE 256 403 /* 2^31 + 2^29 - 2^25 + 2^22 - 2^19 - 2^16 + 1 */ 404 #define GOLDEN_RATIO_PRIME_32 0x9e370001UL 405 406 /* hash (and dump) eeprom */ 407 static int af9015_eeprom_hash(struct dvb_usb_device *d) 408 { 409 struct af9015_state *state = d_to_priv(d); 410 int ret, i; 411 u8 buf[AF9015_EEPROM_SIZE]; 412 struct req_t req = {READ_I2C, AF9015_I2C_EEPROM, 0, 0, 1, 1, NULL}; 413 414 /* read eeprom */ 415 for (i = 0; i < AF9015_EEPROM_SIZE; i++) { 416 req.addr = i; 417 req.data = &buf[i]; 418 ret = af9015_ctrl_msg(d, &req); 419 if (ret < 0) 420 goto err; 421 } 422 423 /* calculate checksum */ 424 for (i = 0; i < AF9015_EEPROM_SIZE / sizeof(u32); i++) { 425 state->eeprom_sum *= GOLDEN_RATIO_PRIME_32; 426 state->eeprom_sum += le32_to_cpu(((__le32 *)buf)[i]); 427 } 428 429 for (i = 0; i < AF9015_EEPROM_SIZE; i += 16) 430 dev_dbg(&d->udev->dev, "%s: %*ph\n", __func__, 16, buf + i); 431 432 dev_dbg(&d->udev->dev, "%s: eeprom sum=%.8x\n", 433 __func__, state->eeprom_sum); 434 return 0; 435 err: 436 dev_err(&d->udev->dev, "%s: eeprom failed=%d\n", KBUILD_MODNAME, ret); 437 return ret; 438 } 439 440 static int af9015_read_config(struct dvb_usb_device *d) 441 { 442 struct af9015_state *state = d_to_priv(d); 443 int ret; 444 u8 val, i, offset = 0; 445 struct req_t req = {READ_I2C, AF9015_I2C_EEPROM, 0, 0, 1, 1, &val}; 446 447 dev_dbg(&d->udev->dev, "%s:\n", __func__); 448 449 /* IR remote controller */ 450 req.addr = AF9015_EEPROM_IR_MODE; 451 /* first message will timeout often due to possible hw bug */ 452 for (i = 0; i < 4; i++) { 453 ret = af9015_ctrl_msg(d, &req); 454 if (!ret) 455 break; 456 } 457 if (ret) 458 goto error; 459 460 ret = af9015_eeprom_hash(d); 461 if (ret) 462 goto error; 463 464 state->ir_mode = val; 465 dev_dbg(&d->udev->dev, "%s: IR mode=%d\n", __func__, val); 466 467 /* TS mode - one or two receivers */ 468 req.addr = AF9015_EEPROM_TS_MODE; 469 ret = af9015_ctrl_msg(d, &req); 470 if (ret) 471 goto error; 472 473 state->dual_mode = val; 474 dev_dbg(&d->udev->dev, "%s: TS mode=%d\n", __func__, state->dual_mode); 475 476 /* disable 2nd adapter because we don't have PID-filters */ 477 if (d->udev->speed == USB_SPEED_FULL) 478 state->dual_mode = 0; 479 480 state->af9013_config[0].i2c_addr = AF9015_I2C_DEMOD; 481 482 if (state->dual_mode) { 483 /* read 2nd demodulator I2C address */ 484 req.addr = AF9015_EEPROM_DEMOD2_I2C; 485 ret = af9015_ctrl_msg(d, &req); 486 if (ret) 487 goto error; 488 489 state->af9013_config[1].i2c_addr = val >> 1; 490 } 491 492 for (i = 0; i < state->dual_mode + 1; i++) { 493 if (i == 1) 494 offset = AF9015_EEPROM_OFFSET; 495 /* xtal */ 496 req.addr = AF9015_EEPROM_XTAL_TYPE1 + offset; 497 ret = af9015_ctrl_msg(d, &req); 498 if (ret) 499 goto error; 500 switch (val) { 501 case 0: 502 state->af9013_config[i].clock = 28800000; 503 break; 504 case 1: 505 state->af9013_config[i].clock = 20480000; 506 break; 507 case 2: 508 state->af9013_config[i].clock = 28000000; 509 break; 510 case 3: 511 state->af9013_config[i].clock = 25000000; 512 break; 513 } 514 dev_dbg(&d->udev->dev, "%s: [%d] xtal=%d set clock=%d\n", 515 __func__, i, val, 516 state->af9013_config[i].clock); 517 518 /* IF frequency */ 519 req.addr = AF9015_EEPROM_IF1H + offset; 520 ret = af9015_ctrl_msg(d, &req); 521 if (ret) 522 goto error; 523 524 state->af9013_config[i].if_frequency = val << 8; 525 526 req.addr = AF9015_EEPROM_IF1L + offset; 527 ret = af9015_ctrl_msg(d, &req); 528 if (ret) 529 goto error; 530 531 state->af9013_config[i].if_frequency += val; 532 state->af9013_config[i].if_frequency *= 1000; 533 dev_dbg(&d->udev->dev, "%s: [%d] IF frequency=%d\n", __func__, 534 i, state->af9013_config[i].if_frequency); 535 536 /* MT2060 IF1 */ 537 req.addr = AF9015_EEPROM_MT2060_IF1H + offset; 538 ret = af9015_ctrl_msg(d, &req); 539 if (ret) 540 goto error; 541 state->mt2060_if1[i] = val << 8; 542 req.addr = AF9015_EEPROM_MT2060_IF1L + offset; 543 ret = af9015_ctrl_msg(d, &req); 544 if (ret) 545 goto error; 546 state->mt2060_if1[i] += val; 547 dev_dbg(&d->udev->dev, "%s: [%d] MT2060 IF1=%d\n", __func__, i, 548 state->mt2060_if1[i]); 549 550 /* tuner */ 551 req.addr = AF9015_EEPROM_TUNER_ID1 + offset; 552 ret = af9015_ctrl_msg(d, &req); 553 if (ret) 554 goto error; 555 switch (val) { 556 case AF9013_TUNER_ENV77H11D5: 557 case AF9013_TUNER_MT2060: 558 case AF9013_TUNER_QT1010: 559 case AF9013_TUNER_UNKNOWN: 560 case AF9013_TUNER_MT2060_2: 561 case AF9013_TUNER_TDA18271: 562 case AF9013_TUNER_QT1010A: 563 case AF9013_TUNER_TDA18218: 564 state->af9013_config[i].spec_inv = 1; 565 break; 566 case AF9013_TUNER_MXL5003D: 567 case AF9013_TUNER_MXL5005D: 568 case AF9013_TUNER_MXL5005R: 569 case AF9013_TUNER_MXL5007T: 570 state->af9013_config[i].spec_inv = 0; 571 break; 572 case AF9013_TUNER_MC44S803: 573 state->af9013_config[i].gpio[1] = AF9013_GPIO_LO; 574 state->af9013_config[i].spec_inv = 1; 575 break; 576 default: 577 dev_err(&d->udev->dev, "%s: tuner id=%d not " \ 578 "supported, please report!\n", 579 KBUILD_MODNAME, val); 580 return -ENODEV; 581 } 582 583 state->af9013_config[i].tuner = val; 584 dev_dbg(&d->udev->dev, "%s: [%d] tuner id=%d\n", 585 __func__, i, val); 586 } 587 588 error: 589 if (ret) 590 dev_err(&d->udev->dev, "%s: eeprom read failed=%d\n", 591 KBUILD_MODNAME, ret); 592 593 /* AverMedia AVerTV Volar Black HD (A850) device have bad EEPROM 594 content :-( Override some wrong values here. Ditto for the 595 AVerTV Red HD+ (A850T) device. */ 596 if (le16_to_cpu(d->udev->descriptor.idVendor) == USB_VID_AVERMEDIA && 597 ((le16_to_cpu(d->udev->descriptor.idProduct) == 598 USB_PID_AVERMEDIA_A850) || 599 (le16_to_cpu(d->udev->descriptor.idProduct) == 600 USB_PID_AVERMEDIA_A850T))) { 601 dev_dbg(&d->udev->dev, 602 "%s: AverMedia A850: overriding config\n", 603 __func__); 604 /* disable dual mode */ 605 state->dual_mode = 0; 606 607 /* set correct IF */ 608 state->af9013_config[0].if_frequency = 4570000; 609 } 610 611 return ret; 612 } 613 614 static int af9015_get_stream_config(struct dvb_frontend *fe, u8 *ts_type, 615 struct usb_data_stream_properties *stream) 616 { 617 struct dvb_usb_device *d = fe_to_d(fe); 618 dev_dbg(&d->udev->dev, "%s: adap=%d\n", __func__, fe_to_adap(fe)->id); 619 620 if (d->udev->speed == USB_SPEED_FULL) 621 stream->u.bulk.buffersize = TS_USB11_FRAME_SIZE; 622 623 return 0; 624 } 625 626 static int af9015_get_adapter_count(struct dvb_usb_device *d) 627 { 628 struct af9015_state *state = d_to_priv(d); 629 return state->dual_mode + 1; 630 } 631 632 /* override demod callbacks for resource locking */ 633 static int af9015_af9013_set_frontend(struct dvb_frontend *fe) 634 { 635 int ret; 636 struct af9015_state *state = fe_to_priv(fe); 637 638 if (mutex_lock_interruptible(&state->fe_mutex)) 639 return -EAGAIN; 640 641 ret = state->set_frontend[fe_to_adap(fe)->id](fe); 642 643 mutex_unlock(&state->fe_mutex); 644 645 return ret; 646 } 647 648 /* override demod callbacks for resource locking */ 649 static int af9015_af9013_read_status(struct dvb_frontend *fe, 650 enum fe_status *status) 651 { 652 int ret; 653 struct af9015_state *state = fe_to_priv(fe); 654 655 if (mutex_lock_interruptible(&state->fe_mutex)) 656 return -EAGAIN; 657 658 ret = state->read_status[fe_to_adap(fe)->id](fe, status); 659 660 mutex_unlock(&state->fe_mutex); 661 662 return ret; 663 } 664 665 /* override demod callbacks for resource locking */ 666 static int af9015_af9013_init(struct dvb_frontend *fe) 667 { 668 int ret; 669 struct af9015_state *state = fe_to_priv(fe); 670 671 if (mutex_lock_interruptible(&state->fe_mutex)) 672 return -EAGAIN; 673 674 ret = state->init[fe_to_adap(fe)->id](fe); 675 676 mutex_unlock(&state->fe_mutex); 677 678 return ret; 679 } 680 681 /* override demod callbacks for resource locking */ 682 static int af9015_af9013_sleep(struct dvb_frontend *fe) 683 { 684 int ret; 685 struct af9015_state *state = fe_to_priv(fe); 686 687 if (mutex_lock_interruptible(&state->fe_mutex)) 688 return -EAGAIN; 689 690 ret = state->sleep[fe_to_adap(fe)->id](fe); 691 692 mutex_unlock(&state->fe_mutex); 693 694 return ret; 695 } 696 697 /* override tuner callbacks for resource locking */ 698 static int af9015_tuner_init(struct dvb_frontend *fe) 699 { 700 int ret; 701 struct af9015_state *state = fe_to_priv(fe); 702 703 if (mutex_lock_interruptible(&state->fe_mutex)) 704 return -EAGAIN; 705 706 ret = state->tuner_init[fe_to_adap(fe)->id](fe); 707 708 mutex_unlock(&state->fe_mutex); 709 710 return ret; 711 } 712 713 /* override tuner callbacks for resource locking */ 714 static int af9015_tuner_sleep(struct dvb_frontend *fe) 715 { 716 int ret; 717 struct af9015_state *state = fe_to_priv(fe); 718 719 if (mutex_lock_interruptible(&state->fe_mutex)) 720 return -EAGAIN; 721 722 ret = state->tuner_sleep[fe_to_adap(fe)->id](fe); 723 724 mutex_unlock(&state->fe_mutex); 725 726 return ret; 727 } 728 729 static int af9015_copy_firmware(struct dvb_usb_device *d) 730 { 731 struct af9015_state *state = d_to_priv(d); 732 int ret; 733 u8 fw_params[4]; 734 u8 val, i; 735 struct req_t req = {COPY_FIRMWARE, 0, 0x5100, 0, 0, sizeof(fw_params), 736 fw_params }; 737 dev_dbg(&d->udev->dev, "%s:\n", __func__); 738 739 fw_params[0] = state->firmware_size >> 8; 740 fw_params[1] = state->firmware_size & 0xff; 741 fw_params[2] = state->firmware_checksum >> 8; 742 fw_params[3] = state->firmware_checksum & 0xff; 743 744 ret = af9015_read_reg_i2c(d, state->af9013_config[1].i2c_addr, 745 0x98be, &val); 746 if (ret) 747 goto error; 748 else 749 dev_dbg(&d->udev->dev, "%s: firmware status=%02x\n", 750 __func__, val); 751 752 if (val == 0x0c) /* fw is running, no need for download */ 753 goto exit; 754 755 /* set I2C master clock to fast (to speed up firmware copy) */ 756 ret = af9015_write_reg(d, 0xd416, 0x04); /* 0x04 * 400ns */ 757 if (ret) 758 goto error; 759 760 msleep(50); 761 762 /* copy firmware */ 763 ret = af9015_ctrl_msg(d, &req); 764 if (ret) 765 dev_err(&d->udev->dev, "%s: firmware copy cmd failed=%d\n", 766 KBUILD_MODNAME, ret); 767 768 dev_dbg(&d->udev->dev, "%s: firmware copy done\n", __func__); 769 770 /* set I2C master clock back to normal */ 771 ret = af9015_write_reg(d, 0xd416, 0x14); /* 0x14 * 400ns */ 772 if (ret) 773 goto error; 774 775 /* request boot firmware */ 776 ret = af9015_write_reg_i2c(d, state->af9013_config[1].i2c_addr, 777 0xe205, 1); 778 dev_dbg(&d->udev->dev, "%s: firmware boot cmd status=%d\n", 779 __func__, ret); 780 if (ret) 781 goto error; 782 783 for (i = 0; i < 15; i++) { 784 msleep(100); 785 786 /* check firmware status */ 787 ret = af9015_read_reg_i2c(d, state->af9013_config[1].i2c_addr, 788 0x98be, &val); 789 dev_dbg(&d->udev->dev, "%s: firmware status cmd status=%d " \ 790 "firmware status=%02x\n", __func__, ret, val); 791 if (ret) 792 goto error; 793 794 if (val == 0x0c || val == 0x04) /* success or fail */ 795 break; 796 } 797 798 if (val == 0x04) { 799 dev_err(&d->udev->dev, "%s: firmware did not run\n", 800 KBUILD_MODNAME); 801 ret = -ETIMEDOUT; 802 } else if (val != 0x0c) { 803 dev_err(&d->udev->dev, "%s: firmware boot timeout\n", 804 KBUILD_MODNAME); 805 ret = -ETIMEDOUT; 806 } 807 808 error: 809 exit: 810 return ret; 811 } 812 813 static int af9015_af9013_frontend_attach(struct dvb_usb_adapter *adap) 814 { 815 int ret; 816 struct af9015_state *state = adap_to_priv(adap); 817 818 if (adap->id == 0) { 819 state->af9013_config[0].ts_mode = AF9013_TS_USB; 820 memcpy(state->af9013_config[0].api_version, "\x0\x1\x9\x0", 4); 821 state->af9013_config[0].gpio[0] = AF9013_GPIO_HI; 822 state->af9013_config[0].gpio[3] = AF9013_GPIO_TUNER_ON; 823 } else if (adap->id == 1) { 824 state->af9013_config[1].ts_mode = AF9013_TS_SERIAL; 825 memcpy(state->af9013_config[1].api_version, "\x0\x1\x9\x0", 4); 826 state->af9013_config[1].gpio[0] = AF9013_GPIO_TUNER_ON; 827 state->af9013_config[1].gpio[1] = AF9013_GPIO_LO; 828 829 /* copy firmware to 2nd demodulator */ 830 if (state->dual_mode) { 831 /* Wait 2nd demodulator ready */ 832 msleep(100); 833 834 ret = af9015_copy_firmware(adap_to_d(adap)); 835 if (ret) { 836 dev_err(&adap_to_d(adap)->udev->dev, 837 "%s: firmware copy to 2nd " \ 838 "frontend failed, will " \ 839 "disable it\n", KBUILD_MODNAME); 840 state->dual_mode = 0; 841 return -ENODEV; 842 } 843 } else { 844 return -ENODEV; 845 } 846 } 847 848 /* attach demodulator */ 849 adap->fe[0] = dvb_attach(af9013_attach, 850 &state->af9013_config[adap->id], &adap_to_d(adap)->i2c_adap); 851 852 /* 853 * AF9015 firmware does not like if it gets interrupted by I2C adapter 854 * request on some critical phases. During normal operation I2C adapter 855 * is used only 2nd demodulator and tuner on dual tuner devices. 856 * Override demodulator callbacks and use mutex for limit access to 857 * those "critical" paths to keep AF9015 happy. 858 */ 859 if (adap->fe[0]) { 860 state->set_frontend[adap->id] = 861 adap->fe[0]->ops.set_frontend; 862 adap->fe[0]->ops.set_frontend = 863 af9015_af9013_set_frontend; 864 865 state->read_status[adap->id] = 866 adap->fe[0]->ops.read_status; 867 adap->fe[0]->ops.read_status = 868 af9015_af9013_read_status; 869 870 state->init[adap->id] = adap->fe[0]->ops.init; 871 adap->fe[0]->ops.init = af9015_af9013_init; 872 873 state->sleep[adap->id] = adap->fe[0]->ops.sleep; 874 adap->fe[0]->ops.sleep = af9015_af9013_sleep; 875 } 876 877 return adap->fe[0] == NULL ? -ENODEV : 0; 878 } 879 880 static struct mt2060_config af9015_mt2060_config = { 881 .i2c_address = 0x60, 882 .clock_out = 0, 883 }; 884 885 static struct qt1010_config af9015_qt1010_config = { 886 .i2c_address = 0x62, 887 }; 888 889 static struct tda18271_config af9015_tda18271_config = { 890 .gate = TDA18271_GATE_DIGITAL, 891 .small_i2c = TDA18271_16_BYTE_CHUNK_INIT, 892 }; 893 894 static struct mxl5005s_config af9015_mxl5003_config = { 895 .i2c_address = 0x63, 896 .if_freq = IF_FREQ_4570000HZ, 897 .xtal_freq = CRYSTAL_FREQ_16000000HZ, 898 .agc_mode = MXL_SINGLE_AGC, 899 .tracking_filter = MXL_TF_DEFAULT, 900 .rssi_enable = MXL_RSSI_ENABLE, 901 .cap_select = MXL_CAP_SEL_ENABLE, 902 .div_out = MXL_DIV_OUT_4, 903 .clock_out = MXL_CLOCK_OUT_DISABLE, 904 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM, 905 .top = MXL5005S_TOP_25P2, 906 .mod_mode = MXL_DIGITAL_MODE, 907 .if_mode = MXL_ZERO_IF, 908 .AgcMasterByte = 0x00, 909 }; 910 911 static struct mxl5005s_config af9015_mxl5005_config = { 912 .i2c_address = 0x63, 913 .if_freq = IF_FREQ_4570000HZ, 914 .xtal_freq = CRYSTAL_FREQ_16000000HZ, 915 .agc_mode = MXL_SINGLE_AGC, 916 .tracking_filter = MXL_TF_OFF, 917 .rssi_enable = MXL_RSSI_ENABLE, 918 .cap_select = MXL_CAP_SEL_ENABLE, 919 .div_out = MXL_DIV_OUT_4, 920 .clock_out = MXL_CLOCK_OUT_DISABLE, 921 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM, 922 .top = MXL5005S_TOP_25P2, 923 .mod_mode = MXL_DIGITAL_MODE, 924 .if_mode = MXL_ZERO_IF, 925 .AgcMasterByte = 0x00, 926 }; 927 928 static struct mc44s803_config af9015_mc44s803_config = { 929 .i2c_address = 0x60, 930 .dig_out = 1, 931 }; 932 933 static struct tda18218_config af9015_tda18218_config = { 934 .i2c_address = 0x60, 935 .i2c_wr_max = 21, /* max wr bytes AF9015 I2C adap can handle at once */ 936 }; 937 938 static struct mxl5007t_config af9015_mxl5007t_config = { 939 .xtal_freq_hz = MxL_XTAL_24_MHZ, 940 .if_freq_hz = MxL_IF_4_57_MHZ, 941 }; 942 943 static int af9015_tuner_attach(struct dvb_usb_adapter *adap) 944 { 945 struct dvb_usb_device *d = adap_to_d(adap); 946 struct af9015_state *state = d_to_priv(d); 947 int ret; 948 dev_dbg(&d->udev->dev, "%s:\n", __func__); 949 950 switch (state->af9013_config[adap->id].tuner) { 951 case AF9013_TUNER_MT2060: 952 case AF9013_TUNER_MT2060_2: 953 ret = dvb_attach(mt2060_attach, adap->fe[0], 954 &adap_to_d(adap)->i2c_adap, &af9015_mt2060_config, 955 state->mt2060_if1[adap->id]) 956 == NULL ? -ENODEV : 0; 957 break; 958 case AF9013_TUNER_QT1010: 959 case AF9013_TUNER_QT1010A: 960 ret = dvb_attach(qt1010_attach, adap->fe[0], 961 &adap_to_d(adap)->i2c_adap, 962 &af9015_qt1010_config) == NULL ? -ENODEV : 0; 963 break; 964 case AF9013_TUNER_TDA18271: 965 ret = dvb_attach(tda18271_attach, adap->fe[0], 0x60, 966 &adap_to_d(adap)->i2c_adap, 967 &af9015_tda18271_config) == NULL ? -ENODEV : 0; 968 break; 969 case AF9013_TUNER_TDA18218: 970 ret = dvb_attach(tda18218_attach, adap->fe[0], 971 &adap_to_d(adap)->i2c_adap, 972 &af9015_tda18218_config) == NULL ? -ENODEV : 0; 973 break; 974 case AF9013_TUNER_MXL5003D: 975 ret = dvb_attach(mxl5005s_attach, adap->fe[0], 976 &adap_to_d(adap)->i2c_adap, 977 &af9015_mxl5003_config) == NULL ? -ENODEV : 0; 978 break; 979 case AF9013_TUNER_MXL5005D: 980 case AF9013_TUNER_MXL5005R: 981 ret = dvb_attach(mxl5005s_attach, adap->fe[0], 982 &adap_to_d(adap)->i2c_adap, 983 &af9015_mxl5005_config) == NULL ? -ENODEV : 0; 984 break; 985 case AF9013_TUNER_ENV77H11D5: 986 ret = dvb_attach(dvb_pll_attach, adap->fe[0], 0x60, 987 &adap_to_d(adap)->i2c_adap, 988 DVB_PLL_TDA665X) == NULL ? -ENODEV : 0; 989 break; 990 case AF9013_TUNER_MC44S803: 991 ret = dvb_attach(mc44s803_attach, adap->fe[0], 992 &adap_to_d(adap)->i2c_adap, 993 &af9015_mc44s803_config) == NULL ? -ENODEV : 0; 994 break; 995 case AF9013_TUNER_MXL5007T: 996 ret = dvb_attach(mxl5007t_attach, adap->fe[0], 997 &adap_to_d(adap)->i2c_adap, 998 0x60, &af9015_mxl5007t_config) == NULL ? -ENODEV : 0; 999 break; 1000 case AF9013_TUNER_UNKNOWN: 1001 default: 1002 dev_err(&d->udev->dev, "%s: unknown tuner id=%d\n", 1003 KBUILD_MODNAME, 1004 state->af9013_config[adap->id].tuner); 1005 ret = -ENODEV; 1006 } 1007 1008 if (adap->fe[0]->ops.tuner_ops.init) { 1009 state->tuner_init[adap->id] = 1010 adap->fe[0]->ops.tuner_ops.init; 1011 adap->fe[0]->ops.tuner_ops.init = af9015_tuner_init; 1012 } 1013 1014 if (adap->fe[0]->ops.tuner_ops.sleep) { 1015 state->tuner_sleep[adap->id] = 1016 adap->fe[0]->ops.tuner_ops.sleep; 1017 adap->fe[0]->ops.tuner_ops.sleep = af9015_tuner_sleep; 1018 } 1019 1020 return ret; 1021 } 1022 1023 static int af9015_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff) 1024 { 1025 struct dvb_usb_device *d = adap_to_d(adap); 1026 int ret; 1027 dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff); 1028 1029 if (onoff) 1030 ret = af9015_set_reg_bit(d, 0xd503, 0); 1031 else 1032 ret = af9015_clear_reg_bit(d, 0xd503, 0); 1033 1034 return ret; 1035 } 1036 1037 static int af9015_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid, 1038 int onoff) 1039 { 1040 struct dvb_usb_device *d = adap_to_d(adap); 1041 int ret; 1042 u8 idx; 1043 dev_dbg(&d->udev->dev, "%s: index=%d pid=%04x onoff=%d\n", 1044 __func__, index, pid, onoff); 1045 1046 ret = af9015_write_reg(d, 0xd505, (pid & 0xff)); 1047 if (ret) 1048 goto error; 1049 1050 ret = af9015_write_reg(d, 0xd506, (pid >> 8)); 1051 if (ret) 1052 goto error; 1053 1054 idx = ((index & 0x1f) | (1 << 5)); 1055 ret = af9015_write_reg(d, 0xd504, idx); 1056 1057 error: 1058 return ret; 1059 } 1060 1061 static int af9015_init_endpoint(struct dvb_usb_device *d) 1062 { 1063 struct af9015_state *state = d_to_priv(d); 1064 int ret; 1065 u16 frame_size; 1066 u8 packet_size; 1067 dev_dbg(&d->udev->dev, "%s: USB speed=%d\n", __func__, d->udev->speed); 1068 1069 if (d->udev->speed == USB_SPEED_FULL) { 1070 frame_size = TS_USB11_FRAME_SIZE/4; 1071 packet_size = TS_USB11_MAX_PACKET_SIZE/4; 1072 } else { 1073 frame_size = TS_USB20_FRAME_SIZE/4; 1074 packet_size = TS_USB20_MAX_PACKET_SIZE/4; 1075 } 1076 1077 ret = af9015_set_reg_bit(d, 0xd507, 2); /* assert EP4 reset */ 1078 if (ret) 1079 goto error; 1080 ret = af9015_set_reg_bit(d, 0xd50b, 1); /* assert EP5 reset */ 1081 if (ret) 1082 goto error; 1083 ret = af9015_clear_reg_bit(d, 0xdd11, 5); /* disable EP4 */ 1084 if (ret) 1085 goto error; 1086 ret = af9015_clear_reg_bit(d, 0xdd11, 6); /* disable EP5 */ 1087 if (ret) 1088 goto error; 1089 ret = af9015_set_reg_bit(d, 0xdd11, 5); /* enable EP4 */ 1090 if (ret) 1091 goto error; 1092 if (state->dual_mode) { 1093 ret = af9015_set_reg_bit(d, 0xdd11, 6); /* enable EP5 */ 1094 if (ret) 1095 goto error; 1096 } 1097 ret = af9015_clear_reg_bit(d, 0xdd13, 5); /* disable EP4 NAK */ 1098 if (ret) 1099 goto error; 1100 if (state->dual_mode) { 1101 ret = af9015_clear_reg_bit(d, 0xdd13, 6); /* disable EP5 NAK */ 1102 if (ret) 1103 goto error; 1104 } 1105 /* EP4 xfer length */ 1106 ret = af9015_write_reg(d, 0xdd88, frame_size & 0xff); 1107 if (ret) 1108 goto error; 1109 ret = af9015_write_reg(d, 0xdd89, frame_size >> 8); 1110 if (ret) 1111 goto error; 1112 /* EP5 xfer length */ 1113 ret = af9015_write_reg(d, 0xdd8a, frame_size & 0xff); 1114 if (ret) 1115 goto error; 1116 ret = af9015_write_reg(d, 0xdd8b, frame_size >> 8); 1117 if (ret) 1118 goto error; 1119 ret = af9015_write_reg(d, 0xdd0c, packet_size); /* EP4 packet size */ 1120 if (ret) 1121 goto error; 1122 ret = af9015_write_reg(d, 0xdd0d, packet_size); /* EP5 packet size */ 1123 if (ret) 1124 goto error; 1125 ret = af9015_clear_reg_bit(d, 0xd507, 2); /* negate EP4 reset */ 1126 if (ret) 1127 goto error; 1128 if (state->dual_mode) { 1129 ret = af9015_clear_reg_bit(d, 0xd50b, 1); /* negate EP5 reset */ 1130 if (ret) 1131 goto error; 1132 } 1133 1134 /* enable / disable mp2if2 */ 1135 if (state->dual_mode) { 1136 ret = af9015_set_reg_bit(d, 0xd50b, 0); 1137 if (ret) 1138 goto error; 1139 ret = af9015_set_reg_bit(d, 0xd520, 4); 1140 if (ret) 1141 goto error; 1142 } else { 1143 ret = af9015_clear_reg_bit(d, 0xd50b, 0); 1144 if (ret) 1145 goto error; 1146 ret = af9015_clear_reg_bit(d, 0xd520, 4); 1147 if (ret) 1148 goto error; 1149 } 1150 1151 error: 1152 if (ret) 1153 dev_err(&d->udev->dev, "%s: endpoint init failed=%d\n", 1154 KBUILD_MODNAME, ret); 1155 1156 return ret; 1157 } 1158 1159 static int af9015_init(struct dvb_usb_device *d) 1160 { 1161 struct af9015_state *state = d_to_priv(d); 1162 int ret; 1163 dev_dbg(&d->udev->dev, "%s:\n", __func__); 1164 1165 mutex_init(&state->fe_mutex); 1166 1167 /* init RC canary */ 1168 ret = af9015_write_reg(d, 0x98e9, 0xff); 1169 if (ret) 1170 goto error; 1171 1172 ret = af9015_init_endpoint(d); 1173 if (ret) 1174 goto error; 1175 1176 error: 1177 return ret; 1178 } 1179 1180 #if IS_ENABLED(CONFIG_RC_CORE) 1181 struct af9015_rc_setup { 1182 unsigned int id; 1183 char *rc_codes; 1184 }; 1185 1186 static char *af9015_rc_setup_match(unsigned int id, 1187 const struct af9015_rc_setup *table) 1188 { 1189 for (; table->rc_codes; table++) 1190 if (table->id == id) 1191 return table->rc_codes; 1192 return NULL; 1193 } 1194 1195 static const struct af9015_rc_setup af9015_rc_setup_modparam[] = { 1196 { AF9015_REMOTE_A_LINK_DTU_M, RC_MAP_ALINK_DTU_M }, 1197 { AF9015_REMOTE_MSI_DIGIVOX_MINI_II_V3, RC_MAP_MSI_DIGIVOX_II }, 1198 { AF9015_REMOTE_MYGICTV_U718, RC_MAP_TOTAL_MEDIA_IN_HAND }, 1199 { AF9015_REMOTE_DIGITTRADE_DVB_T, RC_MAP_DIGITTRADE }, 1200 { AF9015_REMOTE_AVERMEDIA_KS, RC_MAP_AVERMEDIA_RM_KS }, 1201 { } 1202 }; 1203 1204 static const struct af9015_rc_setup af9015_rc_setup_hashes[] = { 1205 { 0xb8feb708, RC_MAP_MSI_DIGIVOX_II }, 1206 { 0xa3703d00, RC_MAP_ALINK_DTU_M }, 1207 { 0x9b7dc64e, RC_MAP_TOTAL_MEDIA_IN_HAND }, /* MYGICTV U718 */ 1208 { 0x5d49e3db, RC_MAP_DIGITTRADE }, /* LC-Power LC-USB-DVBT */ 1209 { } 1210 }; 1211 1212 static int af9015_rc_query(struct dvb_usb_device *d) 1213 { 1214 struct af9015_state *state = d_to_priv(d); 1215 int ret; 1216 u8 buf[17]; 1217 1218 /* read registers needed to detect remote controller code */ 1219 ret = af9015_read_regs(d, 0x98d9, buf, sizeof(buf)); 1220 if (ret) 1221 goto error; 1222 1223 /* If any of these are non-zero, assume invalid data */ 1224 if (buf[1] || buf[2] || buf[3]) { 1225 dev_dbg(&d->udev->dev, "%s: invalid data\n", __func__); 1226 return ret; 1227 } 1228 1229 /* Check for repeat of previous code */ 1230 if ((state->rc_repeat != buf[6] || buf[0]) && 1231 !memcmp(&buf[12], state->rc_last, 4)) { 1232 dev_dbg(&d->udev->dev, "%s: key repeated\n", __func__); 1233 rc_repeat(d->rc_dev); 1234 state->rc_repeat = buf[6]; 1235 return ret; 1236 } 1237 1238 /* Only process key if canary killed */ 1239 if (buf[16] != 0xff && buf[0] != 0x01) { 1240 enum rc_proto proto; 1241 dev_dbg(&d->udev->dev, "%s: key pressed %*ph\n", 1242 __func__, 4, buf + 12); 1243 1244 /* Reset the canary */ 1245 ret = af9015_write_reg(d, 0x98e9, 0xff); 1246 if (ret) 1247 goto error; 1248 1249 /* Remember this key */ 1250 memcpy(state->rc_last, &buf[12], 4); 1251 if (buf[14] == (u8) ~buf[15]) { 1252 if (buf[12] == (u8) ~buf[13]) { 1253 /* NEC */ 1254 state->rc_keycode = RC_SCANCODE_NEC(buf[12], 1255 buf[14]); 1256 proto = RC_PROTO_NEC; 1257 } else { 1258 /* NEC extended*/ 1259 state->rc_keycode = RC_SCANCODE_NECX(buf[12] << 8 | 1260 buf[13], 1261 buf[14]); 1262 proto = RC_PROTO_NECX; 1263 } 1264 } else { 1265 /* 32 bit NEC */ 1266 state->rc_keycode = RC_SCANCODE_NEC32(buf[12] << 24 | 1267 buf[13] << 16 | 1268 buf[14] << 8 | 1269 buf[15]); 1270 proto = RC_PROTO_NEC32; 1271 } 1272 rc_keydown(d->rc_dev, proto, state->rc_keycode, 0); 1273 } else { 1274 dev_dbg(&d->udev->dev, "%s: no key press\n", __func__); 1275 /* Invalidate last keypress */ 1276 /* Not really needed, but helps with debug */ 1277 state->rc_last[2] = state->rc_last[3]; 1278 } 1279 1280 state->rc_repeat = buf[6]; 1281 state->rc_failed = false; 1282 1283 error: 1284 if (ret) { 1285 dev_warn(&d->udev->dev, "%s: rc query failed=%d\n", 1286 KBUILD_MODNAME, ret); 1287 1288 /* allow random errors as dvb-usb will stop polling on error */ 1289 if (!state->rc_failed) 1290 ret = 0; 1291 1292 state->rc_failed = true; 1293 } 1294 1295 return ret; 1296 } 1297 1298 static int af9015_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc) 1299 { 1300 struct af9015_state *state = d_to_priv(d); 1301 u16 vid = le16_to_cpu(d->udev->descriptor.idVendor); 1302 1303 if (state->ir_mode == AF9015_IR_MODE_DISABLED) 1304 return 0; 1305 1306 /* try to load remote based module param */ 1307 if (!rc->map_name) 1308 rc->map_name = af9015_rc_setup_match(dvb_usb_af9015_remote, 1309 af9015_rc_setup_modparam); 1310 1311 /* try to load remote based eeprom hash */ 1312 if (!rc->map_name) 1313 rc->map_name = af9015_rc_setup_match(state->eeprom_sum, 1314 af9015_rc_setup_hashes); 1315 1316 /* try to load remote based USB iManufacturer string */ 1317 if (!rc->map_name && vid == USB_VID_AFATECH) { 1318 /* Check USB manufacturer and product strings and try 1319 to determine correct remote in case of chip vendor 1320 reference IDs are used. 1321 DO NOT ADD ANYTHING NEW HERE. Use hashes instead. */ 1322 char manufacturer[10]; 1323 memset(manufacturer, 0, sizeof(manufacturer)); 1324 usb_string(d->udev, d->udev->descriptor.iManufacturer, 1325 manufacturer, sizeof(manufacturer)); 1326 if (!strcmp("MSI", manufacturer)) { 1327 /* iManufacturer 1 MSI 1328 iProduct 2 MSI K-VOX */ 1329 rc->map_name = af9015_rc_setup_match( 1330 AF9015_REMOTE_MSI_DIGIVOX_MINI_II_V3, 1331 af9015_rc_setup_modparam); 1332 } 1333 } 1334 1335 /* load empty to enable rc */ 1336 if (!rc->map_name) 1337 rc->map_name = RC_MAP_EMPTY; 1338 1339 rc->allowed_protos = RC_PROTO_BIT_NEC | RC_PROTO_BIT_NECX | 1340 RC_PROTO_BIT_NEC32; 1341 rc->query = af9015_rc_query; 1342 rc->interval = 500; 1343 1344 return 0; 1345 } 1346 #else 1347 #define af9015_get_rc_config NULL 1348 #endif 1349 1350 static int af9015_probe(struct usb_interface *intf, 1351 const struct usb_device_id *id) 1352 { 1353 struct usb_device *udev = interface_to_usbdev(intf); 1354 char manufacturer[sizeof("ITE Technologies, Inc.")]; 1355 1356 memset(manufacturer, 0, sizeof(manufacturer)); 1357 usb_string(udev, udev->descriptor.iManufacturer, 1358 manufacturer, sizeof(manufacturer)); 1359 /* 1360 * There is two devices having same ID but different chipset. One uses 1361 * AF9015 and the other IT9135 chipset. Only difference seen on lsusb 1362 * is iManufacturer string. 1363 * 1364 * idVendor 0x0ccd TerraTec Electronic GmbH 1365 * idProduct 0x0099 1366 * bcdDevice 2.00 1367 * iManufacturer 1 Afatech 1368 * iProduct 2 DVB-T 2 1369 * 1370 * idVendor 0x0ccd TerraTec Electronic GmbH 1371 * idProduct 0x0099 1372 * bcdDevice 2.00 1373 * iManufacturer 1 ITE Technologies, Inc. 1374 * iProduct 2 DVB-T TV Stick 1375 */ 1376 if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VID_TERRATEC) && 1377 (le16_to_cpu(udev->descriptor.idProduct) == 0x0099)) { 1378 if (!strcmp("ITE Technologies, Inc.", manufacturer)) { 1379 dev_dbg(&udev->dev, "%s: rejecting device\n", __func__); 1380 return -ENODEV; 1381 } 1382 } 1383 1384 return dvb_usbv2_probe(intf, id); 1385 } 1386 1387 /* interface 0 is used by DVB-T receiver and 1388 interface 1 is for remote controller (HID) */ 1389 static struct dvb_usb_device_properties af9015_props = { 1390 .driver_name = KBUILD_MODNAME, 1391 .owner = THIS_MODULE, 1392 .adapter_nr = adapter_nr, 1393 .size_of_priv = sizeof(struct af9015_state), 1394 1395 .generic_bulk_ctrl_endpoint = 0x02, 1396 .generic_bulk_ctrl_endpoint_response = 0x81, 1397 1398 .identify_state = af9015_identify_state, 1399 .firmware = AF9015_FIRMWARE, 1400 .download_firmware = af9015_download_firmware, 1401 1402 .i2c_algo = &af9015_i2c_algo, 1403 .read_config = af9015_read_config, 1404 .frontend_attach = af9015_af9013_frontend_attach, 1405 .tuner_attach = af9015_tuner_attach, 1406 .init = af9015_init, 1407 .get_rc_config = af9015_get_rc_config, 1408 .get_stream_config = af9015_get_stream_config, 1409 1410 .get_adapter_count = af9015_get_adapter_count, 1411 .adapter = { 1412 { 1413 .caps = DVB_USB_ADAP_HAS_PID_FILTER | 1414 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF, 1415 .pid_filter_count = 32, 1416 .pid_filter = af9015_pid_filter, 1417 .pid_filter_ctrl = af9015_pid_filter_ctrl, 1418 1419 .stream = DVB_USB_STREAM_BULK(0x84, 8, TS_USB20_FRAME_SIZE), 1420 }, { 1421 .stream = DVB_USB_STREAM_BULK(0x85, 8, TS_USB20_FRAME_SIZE), 1422 }, 1423 }, 1424 }; 1425 1426 static const struct usb_device_id af9015_id_table[] = { 1427 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9015_9015, 1428 &af9015_props, "Afatech AF9015 reference design", NULL) }, 1429 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9015_9016, 1430 &af9015_props, "Afatech AF9015 reference design", NULL) }, 1431 { DVB_USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV_DONGLE_GOLD, 1432 &af9015_props, "Leadtek WinFast DTV Dongle Gold", RC_MAP_LEADTEK_Y04G0051) }, 1433 { DVB_USB_DEVICE(USB_VID_PINNACLE, USB_PID_PINNACLE_PCTV71E, 1434 &af9015_props, "Pinnacle PCTV 71e", NULL) }, 1435 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_399U, 1436 &af9015_props, "KWorld PlusTV Dual DVB-T Stick (DVB-T 399U)", NULL) }, 1437 { DVB_USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_TINYTWIN, 1438 &af9015_props, "DigitalNow TinyTwin", RC_MAP_AZUREWAVE_AD_TU700) }, 1439 { DVB_USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_AZUREWAVE_AD_TU700, 1440 &af9015_props, "TwinHan AzureWave AD-TU700(704J)", RC_MAP_AZUREWAVE_AD_TU700) }, 1441 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_USB_XE_REV2, 1442 &af9015_props, "TerraTec Cinergy T USB XE", NULL) }, 1443 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_PC160_2T, 1444 &af9015_props, "KWorld PlusTV Dual DVB-T PCI (DVB-T PC160-2T)", NULL) }, 1445 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_X, 1446 &af9015_props, "AVerMedia AVerTV DVB-T Volar X", RC_MAP_AVERMEDIA_M135A) }, 1447 { DVB_USB_DEVICE(USB_VID_XTENSIONS, USB_PID_XTENSIONS_XD_380, 1448 &af9015_props, "Xtensions XD-380", NULL) }, 1449 { DVB_USB_DEVICE(USB_VID_MSI_2, USB_PID_MSI_DIGIVOX_DUO, 1450 &af9015_props, "MSI DIGIVOX Duo", RC_MAP_MSI_DIGIVOX_III) }, 1451 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_X_2, 1452 &af9015_props, "Fujitsu-Siemens Slim Mobile USB DVB-T", NULL) }, 1453 { DVB_USB_DEVICE(USB_VID_TELESTAR, USB_PID_TELESTAR_STARSTICK_2, 1454 &af9015_props, "Telestar Starstick 2", NULL) }, 1455 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A309, 1456 &af9015_props, "AVerMedia A309", NULL) }, 1457 { DVB_USB_DEVICE(USB_VID_MSI_2, USB_PID_MSI_DIGI_VOX_MINI_III, 1458 &af9015_props, "MSI Digi VOX mini III", RC_MAP_MSI_DIGIVOX_III) }, 1459 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U, 1460 &af9015_props, "KWorld USB DVB-T TV Stick II (VS-DVB-T 395U)", NULL) }, 1461 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_2, 1462 &af9015_props, "KWorld USB DVB-T TV Stick II (VS-DVB-T 395U)", NULL) }, 1463 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_3, 1464 &af9015_props, "KWorld USB DVB-T TV Stick II (VS-DVB-T 395U)", NULL) }, 1465 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_TREKSTOR_DVBT, 1466 &af9015_props, "TrekStor DVB-T USB Stick", RC_MAP_TREKSTOR) }, 1467 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A850, 1468 &af9015_props, "AverMedia AVerTV Volar Black HD (A850)", NULL) }, 1469 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A805, 1470 &af9015_props, "AverMedia AVerTV Volar GPS 805 (A805)", NULL) }, 1471 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_CONCEPTRONIC_CTVDIGRCU, 1472 &af9015_props, "Conceptronic USB2.0 DVB-T CTVDIGRCU V3.0", NULL) }, 1473 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_MC810, 1474 &af9015_props, "KWorld Digital MC-810", NULL) }, 1475 { DVB_USB_DEVICE(USB_VID_KYE, USB_PID_GENIUS_TVGO_DVB_T03, 1476 &af9015_props, "Genius TVGo DVB-T03", NULL) }, 1477 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_399U_2, 1478 &af9015_props, "KWorld PlusTV Dual DVB-T Stick (DVB-T 399U)", NULL) }, 1479 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_PC160_T, 1480 &af9015_props, "KWorld PlusTV DVB-T PCI Pro Card (DVB-T PC160-T)", NULL) }, 1481 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV20, 1482 &af9015_props, "Sveon STV20 Tuner USB DVB-T HDTV", NULL) }, 1483 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_TINYTWIN_2, 1484 &af9015_props, "DigitalNow TinyTwin v2", RC_MAP_DIGITALNOW_TINYTWIN) }, 1485 { DVB_USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV2000DS, 1486 &af9015_props, "Leadtek WinFast DTV2000DS", RC_MAP_LEADTEK_Y04G0051) }, 1487 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_UB383_T, 1488 &af9015_props, "KWorld USB DVB-T Stick Mobile (UB383-T)", NULL) }, 1489 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_4, 1490 &af9015_props, "KWorld USB DVB-T TV Stick II (VS-DVB-T 395U)", NULL) }, 1491 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A815M, 1492 &af9015_props, "AverMedia AVerTV Volar M (A815Mac)", NULL) }, 1493 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK_RC, 1494 &af9015_props, "TerraTec Cinergy T Stick RC", RC_MAP_TERRATEC_SLIM_2) }, 1495 /* XXX: that same ID [0ccd:0099] is used by af9035 driver too */ 1496 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK_DUAL_RC, 1497 &af9015_props, "TerraTec Cinergy T Stick Dual RC", RC_MAP_TERRATEC_SLIM) }, 1498 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A850T, 1499 &af9015_props, "AverMedia AVerTV Red HD+ (A850T)", NULL) }, 1500 { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_TINYTWIN_3, 1501 &af9015_props, "DigitalNow TinyTwin v3", RC_MAP_DIGITALNOW_TINYTWIN) }, 1502 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV22, 1503 &af9015_props, "Sveon STV22 Dual USB DVB-T Tuner HDTV", RC_MAP_MSI_DIGIVOX_III) }, 1504 { } 1505 }; 1506 MODULE_DEVICE_TABLE(usb, af9015_id_table); 1507 1508 /* usb specific object needed to register this driver with the usb subsystem */ 1509 static struct usb_driver af9015_usb_driver = { 1510 .name = KBUILD_MODNAME, 1511 .id_table = af9015_id_table, 1512 .probe = af9015_probe, 1513 .disconnect = dvb_usbv2_disconnect, 1514 .suspend = dvb_usbv2_suspend, 1515 .resume = dvb_usbv2_resume, 1516 .reset_resume = dvb_usbv2_reset_resume, 1517 .no_dynamic_id = 1, 1518 .soft_unbind = 1, 1519 }; 1520 1521 module_usb_driver(af9015_usb_driver); 1522 1523 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); 1524 MODULE_DESCRIPTION("Afatech AF9015 driver"); 1525 MODULE_LICENSE("GPL"); 1526 MODULE_FIRMWARE(AF9015_FIRMWARE); 1527