1 /* DVB USB compliant Linux driver for the Afatech 9005 2 * USB1.1 DVB-T receiver. 3 * 4 * Copyright (C) 2007 Luca Olivetti (luca@ventoso.org) 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 * You should have received a copy of the GNU General Public License 19 * along with this program; if not, write to the Free Software 20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 21 * 22 * see Documentation/dvb/README.dvb-usb for more information 23 */ 24 #include "af9005.h" 25 26 /* debug */ 27 int dvb_usb_af9005_debug; 28 module_param_named(debug, dvb_usb_af9005_debug, int, 0644); 29 MODULE_PARM_DESC(debug, 30 "set debugging level (1=info,xfer=2,rc=4,reg=8,i2c=16,fw=32 (or-able))." 31 DVB_USB_DEBUG_STATUS); 32 /* enable obnoxious led */ 33 bool dvb_usb_af9005_led = true; 34 module_param_named(led, dvb_usb_af9005_led, bool, 0644); 35 MODULE_PARM_DESC(led, "enable led (default: 1)."); 36 37 /* eeprom dump */ 38 static int dvb_usb_af9005_dump_eeprom; 39 module_param_named(dump_eeprom, dvb_usb_af9005_dump_eeprom, int, 0); 40 MODULE_PARM_DESC(dump_eeprom, "dump contents of the eeprom."); 41 42 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr); 43 44 /* remote control decoder */ 45 static int (*rc_decode) (struct dvb_usb_device *d, u8 *data, int len, 46 u32 *event, int *state); 47 static void *rc_keys; 48 static int *rc_keys_size; 49 50 u8 regmask[8] = { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff }; 51 52 struct af9005_device_state { 53 u8 sequence; 54 int led_state; 55 unsigned char data[256]; 56 }; 57 58 static int af9005_generic_read_write(struct dvb_usb_device *d, u16 reg, 59 int readwrite, int type, u8 * values, int len) 60 { 61 struct af9005_device_state *st = d->priv; 62 u8 command, seq; 63 int i, ret; 64 65 if (len < 1) { 66 err("generic read/write, less than 1 byte. Makes no sense."); 67 return -EINVAL; 68 } 69 if (len > 8) { 70 err("generic read/write, more than 8 bytes. Not supported."); 71 return -EINVAL; 72 } 73 74 mutex_lock(&d->data_mutex); 75 st->data[0] = 14; /* rest of buffer length low */ 76 st->data[1] = 0; /* rest of buffer length high */ 77 78 st->data[2] = AF9005_REGISTER_RW; /* register operation */ 79 st->data[3] = 12; /* rest of buffer length */ 80 81 st->data[4] = seq = st->sequence++; /* sequence number */ 82 83 st->data[5] = (u8) (reg >> 8); /* register address */ 84 st->data[6] = (u8) (reg & 0xff); 85 86 if (type == AF9005_OFDM_REG) { 87 command = AF9005_CMD_OFDM_REG; 88 } else { 89 command = AF9005_CMD_TUNER; 90 } 91 92 if (len > 1) 93 command |= 94 AF9005_CMD_BURST | AF9005_CMD_AUTOINC | (len - 1) << 3; 95 command |= readwrite; 96 if (readwrite == AF9005_CMD_WRITE) 97 for (i = 0; i < len; i++) 98 st->data[8 + i] = values[i]; 99 else if (type == AF9005_TUNER_REG) 100 /* read command for tuner, the first byte contains the i2c address */ 101 st->data[8] = values[0]; 102 st->data[7] = command; 103 104 ret = dvb_usb_generic_rw(d, st->data, 16, st->data, 17, 0); 105 if (ret) 106 goto ret; 107 108 /* sanity check */ 109 if (st->data[2] != AF9005_REGISTER_RW_ACK) { 110 err("generic read/write, wrong reply code."); 111 ret = -EIO; 112 goto ret; 113 } 114 if (st->data[3] != 0x0d) { 115 err("generic read/write, wrong length in reply."); 116 ret = -EIO; 117 goto ret; 118 } 119 if (st->data[4] != seq) { 120 err("generic read/write, wrong sequence in reply."); 121 ret = -EIO; 122 goto ret; 123 } 124 /* 125 * In thesis, both input and output buffers should have 126 * identical values for st->data[5] to st->data[8]. 127 * However, windows driver doesn't check these fields, in fact 128 * sometimes the register in the reply is different that what 129 * has been sent 130 */ 131 if (st->data[16] != 0x01) { 132 err("generic read/write wrong status code in reply."); 133 ret = -EIO; 134 goto ret; 135 } 136 137 if (readwrite == AF9005_CMD_READ) 138 for (i = 0; i < len; i++) 139 values[i] = st->data[8 + i]; 140 141 ret: 142 mutex_unlock(&d->data_mutex); 143 return ret; 144 145 } 146 147 int af9005_read_ofdm_register(struct dvb_usb_device *d, u16 reg, u8 * value) 148 { 149 int ret; 150 deb_reg("read register %x ", reg); 151 ret = af9005_generic_read_write(d, reg, 152 AF9005_CMD_READ, AF9005_OFDM_REG, 153 value, 1); 154 if (ret) 155 deb_reg("failed\n"); 156 else 157 deb_reg("value %x\n", *value); 158 return ret; 159 } 160 161 int af9005_read_ofdm_registers(struct dvb_usb_device *d, u16 reg, 162 u8 * values, int len) 163 { 164 int ret; 165 deb_reg("read %d registers %x ", len, reg); 166 ret = af9005_generic_read_write(d, reg, 167 AF9005_CMD_READ, AF9005_OFDM_REG, 168 values, len); 169 if (ret) 170 deb_reg("failed\n"); 171 else 172 debug_dump(values, len, deb_reg); 173 return ret; 174 } 175 176 int af9005_write_ofdm_register(struct dvb_usb_device *d, u16 reg, u8 value) 177 { 178 int ret; 179 u8 temp = value; 180 deb_reg("write register %x value %x ", reg, value); 181 ret = af9005_generic_read_write(d, reg, 182 AF9005_CMD_WRITE, AF9005_OFDM_REG, 183 &temp, 1); 184 if (ret) 185 deb_reg("failed\n"); 186 else 187 deb_reg("ok\n"); 188 return ret; 189 } 190 191 int af9005_write_ofdm_registers(struct dvb_usb_device *d, u16 reg, 192 u8 * values, int len) 193 { 194 int ret; 195 deb_reg("write %d registers %x values ", len, reg); 196 debug_dump(values, len, deb_reg); 197 198 ret = af9005_generic_read_write(d, reg, 199 AF9005_CMD_WRITE, AF9005_OFDM_REG, 200 values, len); 201 if (ret) 202 deb_reg("failed\n"); 203 else 204 deb_reg("ok\n"); 205 return ret; 206 } 207 208 int af9005_read_register_bits(struct dvb_usb_device *d, u16 reg, u8 pos, 209 u8 len, u8 * value) 210 { 211 u8 temp; 212 int ret; 213 deb_reg("read bits %x %x %x", reg, pos, len); 214 ret = af9005_read_ofdm_register(d, reg, &temp); 215 if (ret) { 216 deb_reg(" failed\n"); 217 return ret; 218 } 219 *value = (temp >> pos) & regmask[len - 1]; 220 deb_reg(" value %x\n", *value); 221 return 0; 222 223 } 224 225 int af9005_write_register_bits(struct dvb_usb_device *d, u16 reg, u8 pos, 226 u8 len, u8 value) 227 { 228 u8 temp, mask; 229 int ret; 230 deb_reg("write bits %x %x %x value %x\n", reg, pos, len, value); 231 if (pos == 0 && len == 8) 232 return af9005_write_ofdm_register(d, reg, value); 233 ret = af9005_read_ofdm_register(d, reg, &temp); 234 if (ret) 235 return ret; 236 mask = regmask[len - 1] << pos; 237 temp = (temp & ~mask) | ((value << pos) & mask); 238 return af9005_write_ofdm_register(d, reg, temp); 239 240 } 241 242 static int af9005_usb_read_tuner_registers(struct dvb_usb_device *d, 243 u16 reg, u8 * values, int len) 244 { 245 return af9005_generic_read_write(d, reg, 246 AF9005_CMD_READ, AF9005_TUNER_REG, 247 values, len); 248 } 249 250 static int af9005_usb_write_tuner_registers(struct dvb_usb_device *d, 251 u16 reg, u8 * values, int len) 252 { 253 return af9005_generic_read_write(d, reg, 254 AF9005_CMD_WRITE, 255 AF9005_TUNER_REG, values, len); 256 } 257 258 int af9005_write_tuner_registers(struct dvb_usb_device *d, u16 reg, 259 u8 * values, int len) 260 { 261 /* don't let the name of this function mislead you: it's just used 262 as an interface from the firmware to the i2c bus. The actual 263 i2c addresses are contained in the data */ 264 int ret, i, done = 0, fail = 0; 265 u8 temp; 266 ret = af9005_usb_write_tuner_registers(d, reg, values, len); 267 if (ret) 268 return ret; 269 if (reg != 0xffff) { 270 /* check if write done (0xa40d bit 1) or fail (0xa40d bit 2) */ 271 for (i = 0; i < 200; i++) { 272 ret = 273 af9005_read_ofdm_register(d, 274 xd_I2C_i2c_m_status_wdat_done, 275 &temp); 276 if (ret) 277 return ret; 278 done = temp & (regmask[i2c_m_status_wdat_done_len - 1] 279 << i2c_m_status_wdat_done_pos); 280 if (done) 281 break; 282 fail = temp & (regmask[i2c_m_status_wdat_fail_len - 1] 283 << i2c_m_status_wdat_fail_pos); 284 if (fail) 285 break; 286 msleep(50); 287 } 288 if (i == 200) 289 return -ETIMEDOUT; 290 if (fail) { 291 /* clear write fail bit */ 292 af9005_write_register_bits(d, 293 xd_I2C_i2c_m_status_wdat_fail, 294 i2c_m_status_wdat_fail_pos, 295 i2c_m_status_wdat_fail_len, 296 1); 297 return -EIO; 298 } 299 /* clear write done bit */ 300 ret = 301 af9005_write_register_bits(d, 302 xd_I2C_i2c_m_status_wdat_fail, 303 i2c_m_status_wdat_done_pos, 304 i2c_m_status_wdat_done_len, 1); 305 if (ret) 306 return ret; 307 } 308 return 0; 309 } 310 311 int af9005_read_tuner_registers(struct dvb_usb_device *d, u16 reg, u8 addr, 312 u8 * values, int len) 313 { 314 /* don't let the name of this function mislead you: it's just used 315 as an interface from the firmware to the i2c bus. The actual 316 i2c addresses are contained in the data */ 317 int ret, i; 318 u8 temp, buf[2]; 319 320 buf[0] = addr; /* tuner i2c address */ 321 buf[1] = values[0]; /* tuner register */ 322 323 values[0] = addr + 0x01; /* i2c read address */ 324 325 if (reg == APO_REG_I2C_RW_SILICON_TUNER) { 326 /* write tuner i2c address to tuner, 0c00c0 undocumented, found by sniffing */ 327 ret = af9005_write_tuner_registers(d, 0x00c0, buf, 2); 328 if (ret) 329 return ret; 330 } 331 332 /* send read command to ofsm */ 333 ret = af9005_usb_read_tuner_registers(d, reg, values, 1); 334 if (ret) 335 return ret; 336 337 /* check if read done */ 338 for (i = 0; i < 200; i++) { 339 ret = af9005_read_ofdm_register(d, 0xa408, &temp); 340 if (ret) 341 return ret; 342 if (temp & 0x01) 343 break; 344 msleep(50); 345 } 346 if (i == 200) 347 return -ETIMEDOUT; 348 349 /* clear read done bit (by writing 1) */ 350 ret = af9005_write_ofdm_register(d, xd_I2C_i2c_m_data8, 1); 351 if (ret) 352 return ret; 353 354 /* get read data (available from 0xa400) */ 355 for (i = 0; i < len; i++) { 356 ret = af9005_read_ofdm_register(d, 0xa400 + i, &temp); 357 if (ret) 358 return ret; 359 values[i] = temp; 360 } 361 return 0; 362 } 363 364 static int af9005_i2c_write(struct dvb_usb_device *d, u8 i2caddr, u8 reg, 365 u8 * data, int len) 366 { 367 int ret, i; 368 u8 buf[3]; 369 deb_i2c("i2c_write i2caddr %x, reg %x, len %d data ", i2caddr, 370 reg, len); 371 debug_dump(data, len, deb_i2c); 372 373 for (i = 0; i < len; i++) { 374 buf[0] = i2caddr; 375 buf[1] = reg + (u8) i; 376 buf[2] = data[i]; 377 ret = 378 af9005_write_tuner_registers(d, 379 APO_REG_I2C_RW_SILICON_TUNER, 380 buf, 3); 381 if (ret) { 382 deb_i2c("i2c_write failed\n"); 383 return ret; 384 } 385 } 386 deb_i2c("i2c_write ok\n"); 387 return 0; 388 } 389 390 static int af9005_i2c_read(struct dvb_usb_device *d, u8 i2caddr, u8 reg, 391 u8 * data, int len) 392 { 393 int ret, i; 394 u8 temp; 395 deb_i2c("i2c_read i2caddr %x, reg %x, len %d\n ", i2caddr, reg, len); 396 for (i = 0; i < len; i++) { 397 temp = reg + i; 398 ret = 399 af9005_read_tuner_registers(d, 400 APO_REG_I2C_RW_SILICON_TUNER, 401 i2caddr, &temp, 1); 402 if (ret) { 403 deb_i2c("i2c_read failed\n"); 404 return ret; 405 } 406 data[i] = temp; 407 } 408 deb_i2c("i2c data read: "); 409 debug_dump(data, len, deb_i2c); 410 return 0; 411 } 412 413 static int af9005_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[], 414 int num) 415 { 416 /* only implements what the mt2060 module does, don't know how 417 to make it really generic */ 418 struct dvb_usb_device *d = i2c_get_adapdata(adap); 419 int ret; 420 u8 reg, addr; 421 u8 *value; 422 423 if (mutex_lock_interruptible(&d->i2c_mutex) < 0) 424 return -EAGAIN; 425 426 if (num > 2) 427 warn("more than 2 i2c messages at a time is not handled yet. TODO."); 428 429 if (num == 2) { 430 /* reads a single register */ 431 reg = *msg[0].buf; 432 addr = msg[0].addr; 433 value = msg[1].buf; 434 ret = af9005_i2c_read(d, addr, reg, value, 1); 435 if (ret == 0) 436 ret = 2; 437 } else { 438 /* write one or more registers */ 439 reg = msg[0].buf[0]; 440 addr = msg[0].addr; 441 value = &msg[0].buf[1]; 442 ret = af9005_i2c_write(d, addr, reg, value, msg[0].len - 1); 443 if (ret == 0) 444 ret = 1; 445 } 446 447 mutex_unlock(&d->i2c_mutex); 448 return ret; 449 } 450 451 static u32 af9005_i2c_func(struct i2c_adapter *adapter) 452 { 453 return I2C_FUNC_I2C; 454 } 455 456 static struct i2c_algorithm af9005_i2c_algo = { 457 .master_xfer = af9005_i2c_xfer, 458 .functionality = af9005_i2c_func, 459 }; 460 461 int af9005_send_command(struct dvb_usb_device *d, u8 command, u8 * wbuf, 462 int wlen, u8 * rbuf, int rlen) 463 { 464 struct af9005_device_state *st = d->priv; 465 466 int ret, i, packet_len; 467 u8 seq; 468 469 if (wlen < 0) { 470 err("send command, wlen less than 0 bytes. Makes no sense."); 471 return -EINVAL; 472 } 473 if (wlen > 54) { 474 err("send command, wlen more than 54 bytes. Not supported."); 475 return -EINVAL; 476 } 477 if (rlen > 54) { 478 err("send command, rlen more than 54 bytes. Not supported."); 479 return -EINVAL; 480 } 481 packet_len = wlen + 5; 482 483 mutex_lock(&d->data_mutex); 484 485 st->data[0] = (u8) (packet_len & 0xff); 486 st->data[1] = (u8) ((packet_len & 0xff00) >> 8); 487 488 st->data[2] = 0x26; /* packet type */ 489 st->data[3] = wlen + 3; 490 st->data[4] = seq = st->sequence++; 491 st->data[5] = command; 492 st->data[6] = wlen; 493 for (i = 0; i < wlen; i++) 494 st->data[7 + i] = wbuf[i]; 495 ret = dvb_usb_generic_rw(d, st->data, wlen + 7, st->data, rlen + 7, 0); 496 if (st->data[2] != 0x27) { 497 err("send command, wrong reply code."); 498 ret = -EIO; 499 } else if (st->data[4] != seq) { 500 err("send command, wrong sequence in reply."); 501 ret = -EIO; 502 } else if (st->data[5] != 0x01) { 503 err("send command, wrong status code in reply."); 504 ret = -EIO; 505 } else if (st->data[6] != rlen) { 506 err("send command, invalid data length in reply."); 507 ret = -EIO; 508 } 509 if (!ret) { 510 for (i = 0; i < rlen; i++) 511 rbuf[i] = st->data[i + 7]; 512 } 513 514 mutex_unlock(&d->data_mutex); 515 return ret; 516 } 517 518 int af9005_read_eeprom(struct dvb_usb_device *d, u8 address, u8 * values, 519 int len) 520 { 521 struct af9005_device_state *st = d->priv; 522 u8 seq; 523 int ret, i; 524 525 mutex_lock(&d->data_mutex); 526 527 memset(st->data, 0, sizeof(st->data)); 528 529 st->data[0] = 14; /* length of rest of packet low */ 530 st->data[1] = 0; /* length of rest of packer high */ 531 532 st->data[2] = 0x2a; /* read/write eeprom */ 533 534 st->data[3] = 12; /* size */ 535 536 st->data[4] = seq = st->sequence++; 537 538 st->data[5] = 0; /* read */ 539 540 st->data[6] = len; 541 st->data[7] = address; 542 ret = dvb_usb_generic_rw(d, st->data, 16, st->data, 14, 0); 543 if (st->data[2] != 0x2b) { 544 err("Read eeprom, invalid reply code"); 545 ret = -EIO; 546 } else if (st->data[3] != 10) { 547 err("Read eeprom, invalid reply length"); 548 ret = -EIO; 549 } else if (st->data[4] != seq) { 550 err("Read eeprom, wrong sequence in reply "); 551 ret = -EIO; 552 } else if (st->data[5] != 1) { 553 err("Read eeprom, wrong status in reply "); 554 ret = -EIO; 555 } 556 557 if (!ret) { 558 for (i = 0; i < len; i++) 559 values[i] = st->data[6 + i]; 560 } 561 mutex_unlock(&d->data_mutex); 562 563 return ret; 564 } 565 566 static int af9005_boot_packet(struct usb_device *udev, int type, u8 *reply, 567 u8 *buf, int size) 568 { 569 u16 checksum; 570 int act_len, i, ret; 571 572 memset(buf, 0, size); 573 buf[0] = (u8) (FW_BULKOUT_SIZE & 0xff); 574 buf[1] = (u8) ((FW_BULKOUT_SIZE >> 8) & 0xff); 575 switch (type) { 576 case FW_CONFIG: 577 buf[2] = 0x11; 578 buf[3] = 0x04; 579 buf[4] = 0x00; /* sequence number, original driver doesn't increment it here */ 580 buf[5] = 0x03; 581 checksum = buf[4] + buf[5]; 582 buf[6] = (u8) ((checksum >> 8) & 0xff); 583 buf[7] = (u8) (checksum & 0xff); 584 break; 585 case FW_CONFIRM: 586 buf[2] = 0x11; 587 buf[3] = 0x04; 588 buf[4] = 0x00; /* sequence number, original driver doesn't increment it here */ 589 buf[5] = 0x01; 590 checksum = buf[4] + buf[5]; 591 buf[6] = (u8) ((checksum >> 8) & 0xff); 592 buf[7] = (u8) (checksum & 0xff); 593 break; 594 case FW_BOOT: 595 buf[2] = 0x10; 596 buf[3] = 0x08; 597 buf[4] = 0x00; /* sequence number, original driver doesn't increment it here */ 598 buf[5] = 0x97; 599 buf[6] = 0xaa; 600 buf[7] = 0x55; 601 buf[8] = 0xa5; 602 buf[9] = 0x5a; 603 checksum = 0; 604 for (i = 4; i <= 9; i++) 605 checksum += buf[i]; 606 buf[10] = (u8) ((checksum >> 8) & 0xff); 607 buf[11] = (u8) (checksum & 0xff); 608 break; 609 default: 610 err("boot packet invalid boot packet type"); 611 return -EINVAL; 612 } 613 deb_fw(">>> "); 614 debug_dump(buf, FW_BULKOUT_SIZE + 2, deb_fw); 615 616 ret = usb_bulk_msg(udev, 617 usb_sndbulkpipe(udev, 0x02), 618 buf, FW_BULKOUT_SIZE + 2, &act_len, 2000); 619 if (ret) 620 err("boot packet bulk message failed: %d (%d/%d)", ret, 621 FW_BULKOUT_SIZE + 2, act_len); 622 else 623 ret = act_len != FW_BULKOUT_SIZE + 2 ? -1 : 0; 624 if (ret) 625 return ret; 626 memset(buf, 0, 9); 627 ret = usb_bulk_msg(udev, 628 usb_rcvbulkpipe(udev, 0x01), buf, 9, &act_len, 2000); 629 if (ret) { 630 err("boot packet recv bulk message failed: %d", ret); 631 return ret; 632 } 633 deb_fw("<<< "); 634 debug_dump(buf, act_len, deb_fw); 635 checksum = 0; 636 switch (type) { 637 case FW_CONFIG: 638 if (buf[2] != 0x11) { 639 err("boot bad config header."); 640 return -EIO; 641 } 642 if (buf[3] != 0x05) { 643 err("boot bad config size."); 644 return -EIO; 645 } 646 if (buf[4] != 0x00) { 647 err("boot bad config sequence."); 648 return -EIO; 649 } 650 if (buf[5] != 0x04) { 651 err("boot bad config subtype."); 652 return -EIO; 653 } 654 for (i = 4; i <= 6; i++) 655 checksum += buf[i]; 656 if (buf[7] * 256 + buf[8] != checksum) { 657 err("boot bad config checksum."); 658 return -EIO; 659 } 660 *reply = buf[6]; 661 break; 662 case FW_CONFIRM: 663 if (buf[2] != 0x11) { 664 err("boot bad confirm header."); 665 return -EIO; 666 } 667 if (buf[3] != 0x05) { 668 err("boot bad confirm size."); 669 return -EIO; 670 } 671 if (buf[4] != 0x00) { 672 err("boot bad confirm sequence."); 673 return -EIO; 674 } 675 if (buf[5] != 0x02) { 676 err("boot bad confirm subtype."); 677 return -EIO; 678 } 679 for (i = 4; i <= 6; i++) 680 checksum += buf[i]; 681 if (buf[7] * 256 + buf[8] != checksum) { 682 err("boot bad confirm checksum."); 683 return -EIO; 684 } 685 *reply = buf[6]; 686 break; 687 case FW_BOOT: 688 if (buf[2] != 0x10) { 689 err("boot bad boot header."); 690 return -EIO; 691 } 692 if (buf[3] != 0x05) { 693 err("boot bad boot size."); 694 return -EIO; 695 } 696 if (buf[4] != 0x00) { 697 err("boot bad boot sequence."); 698 return -EIO; 699 } 700 if (buf[5] != 0x01) { 701 err("boot bad boot pattern 01."); 702 return -EIO; 703 } 704 if (buf[6] != 0x10) { 705 err("boot bad boot pattern 10."); 706 return -EIO; 707 } 708 for (i = 4; i <= 6; i++) 709 checksum += buf[i]; 710 if (buf[7] * 256 + buf[8] != checksum) { 711 err("boot bad boot checksum."); 712 return -EIO; 713 } 714 break; 715 716 } 717 718 return 0; 719 } 720 721 static int af9005_download_firmware(struct usb_device *udev, const struct firmware *fw) 722 { 723 int i, packets, ret, act_len; 724 725 u8 *buf; 726 u8 reply; 727 728 buf = kmalloc(FW_BULKOUT_SIZE + 2, GFP_KERNEL); 729 if (!buf) 730 return -ENOMEM; 731 732 ret = af9005_boot_packet(udev, FW_CONFIG, &reply, buf, 733 FW_BULKOUT_SIZE + 2); 734 if (ret) 735 goto err; 736 if (reply != 0x01) { 737 err("before downloading firmware, FW_CONFIG expected 0x01, received 0x%x", reply); 738 ret = -EIO; 739 goto err; 740 } 741 packets = fw->size / FW_BULKOUT_SIZE; 742 buf[0] = (u8) (FW_BULKOUT_SIZE & 0xff); 743 buf[1] = (u8) ((FW_BULKOUT_SIZE >> 8) & 0xff); 744 for (i = 0; i < packets; i++) { 745 memcpy(&buf[2], fw->data + i * FW_BULKOUT_SIZE, 746 FW_BULKOUT_SIZE); 747 deb_fw(">>> "); 748 debug_dump(buf, FW_BULKOUT_SIZE + 2, deb_fw); 749 ret = usb_bulk_msg(udev, 750 usb_sndbulkpipe(udev, 0x02), 751 buf, FW_BULKOUT_SIZE + 2, &act_len, 1000); 752 if (ret) { 753 err("firmware download failed at packet %d with code %d", i, ret); 754 goto err; 755 } 756 } 757 ret = af9005_boot_packet(udev, FW_CONFIRM, &reply, 758 buf, FW_BULKOUT_SIZE + 2); 759 if (ret) 760 goto err; 761 if (reply != (u8) (packets & 0xff)) { 762 err("after downloading firmware, FW_CONFIRM expected 0x%x, received 0x%x", packets & 0xff, reply); 763 ret = -EIO; 764 goto err; 765 } 766 ret = af9005_boot_packet(udev, FW_BOOT, &reply, buf, 767 FW_BULKOUT_SIZE + 2); 768 if (ret) 769 goto err; 770 ret = af9005_boot_packet(udev, FW_CONFIG, &reply, buf, 771 FW_BULKOUT_SIZE + 2); 772 if (ret) 773 goto err; 774 if (reply != 0x02) { 775 err("after downloading firmware, FW_CONFIG expected 0x02, received 0x%x", reply); 776 ret = -EIO; 777 goto err; 778 } 779 780 err: 781 kfree(buf); 782 return ret; 783 784 } 785 786 int af9005_led_control(struct dvb_usb_device *d, int onoff) 787 { 788 struct af9005_device_state *st = d->priv; 789 int temp, ret; 790 791 if (onoff && dvb_usb_af9005_led) 792 temp = 1; 793 else 794 temp = 0; 795 if (st->led_state != temp) { 796 ret = 797 af9005_write_register_bits(d, xd_p_reg_top_locken1, 798 reg_top_locken1_pos, 799 reg_top_locken1_len, temp); 800 if (ret) 801 return ret; 802 ret = 803 af9005_write_register_bits(d, xd_p_reg_top_lock1, 804 reg_top_lock1_pos, 805 reg_top_lock1_len, temp); 806 if (ret) 807 return ret; 808 st->led_state = temp; 809 } 810 return 0; 811 } 812 813 static int af9005_frontend_attach(struct dvb_usb_adapter *adap) 814 { 815 u8 buf[8]; 816 int i; 817 818 /* without these calls the first commands after downloading 819 the firmware fail. I put these calls here to simulate 820 what it is done in dvb-usb-init.c. 821 */ 822 struct usb_device *udev = adap->dev->udev; 823 usb_clear_halt(udev, usb_sndbulkpipe(udev, 2)); 824 usb_clear_halt(udev, usb_rcvbulkpipe(udev, 1)); 825 if (dvb_usb_af9005_dump_eeprom) { 826 printk("EEPROM DUMP\n"); 827 for (i = 0; i < 255; i += 8) { 828 af9005_read_eeprom(adap->dev, i, buf, 8); 829 debug_dump(buf, 8, printk); 830 } 831 } 832 adap->fe_adap[0].fe = af9005_fe_attach(adap->dev); 833 return 0; 834 } 835 836 static int af9005_rc_query(struct dvb_usb_device *d, u32 * event, int *state) 837 { 838 struct af9005_device_state *st = d->priv; 839 int ret, len; 840 u8 seq; 841 842 *state = REMOTE_NO_KEY_PRESSED; 843 if (rc_decode == NULL) { 844 /* it shouldn't never come here */ 845 return 0; 846 } 847 848 mutex_lock(&d->data_mutex); 849 850 /* deb_info("rc_query\n"); */ 851 st->data[0] = 3; /* rest of packet length low */ 852 st->data[1] = 0; /* rest of packet lentgh high */ 853 st->data[2] = 0x40; /* read remote */ 854 st->data[3] = 1; /* rest of packet length */ 855 st->data[4] = seq = st->sequence++; /* sequence number */ 856 ret = dvb_usb_generic_rw(d, st->data, 5, st->data, 256, 0); 857 if (ret) { 858 err("rc query failed"); 859 goto ret; 860 } 861 if (st->data[2] != 0x41) { 862 err("rc query bad header."); 863 ret = -EIO; 864 goto ret; 865 } else if (st->data[4] != seq) { 866 err("rc query bad sequence."); 867 ret = -EIO; 868 goto ret; 869 } 870 len = st->data[5]; 871 if (len > 246) { 872 err("rc query invalid length"); 873 ret = -EIO; 874 goto ret; 875 } 876 if (len > 0) { 877 deb_rc("rc data (%d) ", len); 878 debug_dump((st->data + 6), len, deb_rc); 879 ret = rc_decode(d, &st->data[6], len, event, state); 880 if (ret) { 881 err("rc_decode failed"); 882 goto ret; 883 } else { 884 deb_rc("rc_decode state %x event %x\n", *state, *event); 885 if (*state == REMOTE_KEY_REPEAT) 886 *event = d->last_event; 887 } 888 } 889 890 ret: 891 mutex_unlock(&d->data_mutex); 892 return ret; 893 } 894 895 static int af9005_power_ctrl(struct dvb_usb_device *d, int onoff) 896 { 897 898 return 0; 899 } 900 901 static int af9005_pid_filter_control(struct dvb_usb_adapter *adap, int onoff) 902 { 903 int ret; 904 deb_info("pid filter control onoff %d\n", onoff); 905 if (onoff) { 906 ret = 907 af9005_write_ofdm_register(adap->dev, XD_MP2IF_DMX_CTRL, 1); 908 if (ret) 909 return ret; 910 ret = 911 af9005_write_register_bits(adap->dev, 912 XD_MP2IF_DMX_CTRL, 1, 1, 1); 913 if (ret) 914 return ret; 915 ret = 916 af9005_write_ofdm_register(adap->dev, XD_MP2IF_DMX_CTRL, 1); 917 } else 918 ret = 919 af9005_write_ofdm_register(adap->dev, XD_MP2IF_DMX_CTRL, 0); 920 if (ret) 921 return ret; 922 deb_info("pid filter control ok\n"); 923 return 0; 924 } 925 926 static int af9005_pid_filter(struct dvb_usb_adapter *adap, int index, 927 u16 pid, int onoff) 928 { 929 u8 cmd = index & 0x1f; 930 int ret; 931 deb_info("set pid filter, index %d, pid %x, onoff %d\n", index, 932 pid, onoff); 933 if (onoff) { 934 /* cannot use it as pid_filter_ctrl since it has to be done 935 before setting the first pid */ 936 if (adap->feedcount == 1) { 937 deb_info("first pid set, enable pid table\n"); 938 ret = af9005_pid_filter_control(adap, onoff); 939 if (ret) 940 return ret; 941 } 942 ret = 943 af9005_write_ofdm_register(adap->dev, 944 XD_MP2IF_PID_DATA_L, 945 (u8) (pid & 0xff)); 946 if (ret) 947 return ret; 948 ret = 949 af9005_write_ofdm_register(adap->dev, 950 XD_MP2IF_PID_DATA_H, 951 (u8) (pid >> 8)); 952 if (ret) 953 return ret; 954 cmd |= 0x20 | 0x40; 955 } else { 956 if (adap->feedcount == 0) { 957 deb_info("last pid unset, disable pid table\n"); 958 ret = af9005_pid_filter_control(adap, onoff); 959 if (ret) 960 return ret; 961 } 962 } 963 ret = af9005_write_ofdm_register(adap->dev, XD_MP2IF_PID_IDX, cmd); 964 if (ret) 965 return ret; 966 deb_info("set pid ok\n"); 967 return 0; 968 } 969 970 static int af9005_identify_state(struct usb_device *udev, 971 struct dvb_usb_device_properties *props, 972 struct dvb_usb_device_description **desc, 973 int *cold) 974 { 975 int ret; 976 u8 reply, *buf; 977 978 buf = kmalloc(FW_BULKOUT_SIZE + 2, GFP_KERNEL); 979 if (!buf) 980 return -ENOMEM; 981 982 ret = af9005_boot_packet(udev, FW_CONFIG, &reply, 983 buf, FW_BULKOUT_SIZE + 2); 984 if (ret) 985 goto err; 986 deb_info("result of FW_CONFIG in identify state %d\n", reply); 987 if (reply == 0x01) 988 *cold = 1; 989 else if (reply == 0x02) 990 *cold = 0; 991 else 992 return -EIO; 993 deb_info("Identify state cold = %d\n", *cold); 994 995 err: 996 kfree(buf); 997 return ret; 998 } 999 1000 static struct dvb_usb_device_properties af9005_properties; 1001 1002 static int af9005_usb_probe(struct usb_interface *intf, 1003 const struct usb_device_id *id) 1004 { 1005 return dvb_usb_device_init(intf, &af9005_properties, 1006 THIS_MODULE, NULL, adapter_nr); 1007 } 1008 1009 enum af9005_usb_table_entry { 1010 AFATECH_AF9005, 1011 TERRATEC_AF9005, 1012 ANSONIC_AF9005, 1013 }; 1014 1015 static struct usb_device_id af9005_usb_table[] = { 1016 [AFATECH_AF9005] = {USB_DEVICE(USB_VID_AFATECH, 1017 USB_PID_AFATECH_AF9005)}, 1018 [TERRATEC_AF9005] = {USB_DEVICE(USB_VID_TERRATEC, 1019 USB_PID_TERRATEC_CINERGY_T_USB_XE)}, 1020 [ANSONIC_AF9005] = {USB_DEVICE(USB_VID_ANSONIC, 1021 USB_PID_ANSONIC_DVBT_USB)}, 1022 { } 1023 }; 1024 1025 MODULE_DEVICE_TABLE(usb, af9005_usb_table); 1026 1027 static struct dvb_usb_device_properties af9005_properties = { 1028 .caps = DVB_USB_IS_AN_I2C_ADAPTER, 1029 1030 .usb_ctrl = DEVICE_SPECIFIC, 1031 .firmware = "af9005.fw", 1032 .download_firmware = af9005_download_firmware, 1033 .no_reconnect = 1, 1034 1035 .size_of_priv = sizeof(struct af9005_device_state), 1036 1037 .num_adapters = 1, 1038 .adapter = { 1039 { 1040 .num_frontends = 1, 1041 .fe = {{ 1042 .caps = 1043 DVB_USB_ADAP_HAS_PID_FILTER | 1044 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF, 1045 .pid_filter_count = 32, 1046 .pid_filter = af9005_pid_filter, 1047 /* .pid_filter_ctrl = af9005_pid_filter_control, */ 1048 .frontend_attach = af9005_frontend_attach, 1049 /* .tuner_attach = af9005_tuner_attach, */ 1050 /* parameter for the MPEG2-data transfer */ 1051 .stream = { 1052 .type = USB_BULK, 1053 .count = 10, 1054 .endpoint = 0x04, 1055 .u = { 1056 .bulk = { 1057 .buffersize = 4096, /* actual size seen is 3948 */ 1058 } 1059 } 1060 }, 1061 }}, 1062 } 1063 }, 1064 .power_ctrl = af9005_power_ctrl, 1065 .identify_state = af9005_identify_state, 1066 1067 .i2c_algo = &af9005_i2c_algo, 1068 1069 .rc.legacy = { 1070 .rc_interval = 200, 1071 .rc_map_table = NULL, 1072 .rc_map_size = 0, 1073 .rc_query = af9005_rc_query, 1074 }, 1075 1076 .generic_bulk_ctrl_endpoint = 2, 1077 .generic_bulk_ctrl_endpoint_response = 1, 1078 1079 .num_device_descs = 3, 1080 .devices = { 1081 {.name = "Afatech DVB-T USB1.1 stick", 1082 .cold_ids = {&af9005_usb_table[AFATECH_AF9005], NULL}, 1083 .warm_ids = {NULL}, 1084 }, 1085 {.name = "TerraTec Cinergy T USB XE", 1086 .cold_ids = {&af9005_usb_table[TERRATEC_AF9005], NULL}, 1087 .warm_ids = {NULL}, 1088 }, 1089 {.name = "Ansonic DVB-T USB1.1 stick", 1090 .cold_ids = {&af9005_usb_table[ANSONIC_AF9005], NULL}, 1091 .warm_ids = {NULL}, 1092 }, 1093 {NULL}, 1094 } 1095 }; 1096 1097 /* usb specific object needed to register this driver with the usb subsystem */ 1098 static struct usb_driver af9005_usb_driver = { 1099 .name = "dvb_usb_af9005", 1100 .probe = af9005_usb_probe, 1101 .disconnect = dvb_usb_device_exit, 1102 .id_table = af9005_usb_table, 1103 }; 1104 1105 /* module stuff */ 1106 static int __init af9005_usb_module_init(void) 1107 { 1108 int result; 1109 if ((result = usb_register(&af9005_usb_driver))) { 1110 err("usb_register failed. (%d)", result); 1111 return result; 1112 } 1113 #if IS_MODULE(CONFIG_DVB_USB_AF9005) || defined(CONFIG_DVB_USB_AF9005_REMOTE) 1114 /* FIXME: convert to todays kernel IR infrastructure */ 1115 rc_decode = symbol_request(af9005_rc_decode); 1116 rc_keys = symbol_request(rc_map_af9005_table); 1117 rc_keys_size = symbol_request(rc_map_af9005_table_size); 1118 #endif 1119 if (rc_decode == NULL || rc_keys == NULL || rc_keys_size == NULL) { 1120 err("af9005_rc_decode function not found, disabling remote"); 1121 af9005_properties.rc.legacy.rc_query = NULL; 1122 } else { 1123 af9005_properties.rc.legacy.rc_map_table = rc_keys; 1124 af9005_properties.rc.legacy.rc_map_size = *rc_keys_size; 1125 } 1126 1127 return 0; 1128 } 1129 1130 static void __exit af9005_usb_module_exit(void) 1131 { 1132 /* release rc decode symbols */ 1133 if (rc_decode != NULL) 1134 symbol_put(af9005_rc_decode); 1135 if (rc_keys != NULL) 1136 symbol_put(rc_map_af9005_table); 1137 if (rc_keys_size != NULL) 1138 symbol_put(rc_map_af9005_table_size); 1139 /* deregister this driver from the USB subsystem */ 1140 usb_deregister(&af9005_usb_driver); 1141 } 1142 1143 module_init(af9005_usb_module_init); 1144 module_exit(af9005_usb_module_exit); 1145 1146 MODULE_AUTHOR("Luca Olivetti <luca@ventoso.org>"); 1147 MODULE_DESCRIPTION("Driver for Afatech 9005 DVB-T USB1.1 stick"); 1148 MODULE_VERSION("1.0"); 1149 MODULE_LICENSE("GPL"); 1150