1 /* 2 * Driver for AzureWave 6007 DVB-C/T USB2.0 and clones 3 * 4 * Copyright (c) Henry Wang <Henry.wang@AzureWave.com> 5 * 6 * This driver was made publicly available by Terratec, at: 7 * http://linux.terratec.de/files/TERRATEC_H7/20110323_TERRATEC_H7_Linux.tar.gz 8 * The original driver's license is GPL, as declared with MODULE_LICENSE() 9 * 10 * Copyright (c) 2010-2012 Mauro Carvalho Chehab 11 * Driver modified by in order to work with upstream drxk driver, and 12 * tons of bugs got fixed, and converted to use dvb-usb-v2. 13 * 14 * This program is free software; you can redistribute it and/or modify 15 * it under the terms of the GNU General Public License as published by 16 * the Free Software Foundation under version 2 of the License. 17 * 18 * This program is distributed in the hope that it will be useful, 19 * but WITHOUT ANY WARRANTY; without even the implied warranty of 20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 * GNU General Public License for more details. 22 */ 23 24 #include "drxk.h" 25 #include "mt2063.h" 26 #include <media/dvb_ca_en50221.h> 27 #include "dvb_usb.h" 28 #include "cypress_firmware.h" 29 30 #define AZ6007_FIRMWARE "dvb-usb-terratec-h7-az6007.fw" 31 32 static int az6007_xfer_debug; 33 module_param_named(xfer_debug, az6007_xfer_debug, int, 0644); 34 MODULE_PARM_DESC(xfer_debug, "Enable xfer debug"); 35 36 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr); 37 38 /* Known requests (Cypress FX2 firmware + az6007 "private" ones*/ 39 40 #define FX2_OED 0xb5 41 #define AZ6007_READ_DATA 0xb7 42 #define AZ6007_I2C_RD 0xb9 43 #define AZ6007_POWER 0xbc 44 #define AZ6007_I2C_WR 0xbd 45 #define FX2_SCON1 0xc0 46 #define AZ6007_TS_THROUGH 0xc7 47 #define AZ6007_READ_IR 0xb4 48 49 struct az6007_device_state { 50 struct mutex mutex; 51 struct mutex ca_mutex; 52 struct dvb_ca_en50221 ca; 53 unsigned warm:1; 54 int (*gate_ctrl) (struct dvb_frontend *, int); 55 unsigned char data[4096]; 56 }; 57 58 static struct drxk_config terratec_h7_drxk = { 59 .adr = 0x29, 60 .parallel_ts = true, 61 .dynamic_clk = true, 62 .single_master = true, 63 .enable_merr_cfg = true, 64 .no_i2c_bridge = false, 65 .chunk_size = 64, 66 .mpeg_out_clk_strength = 0x02, 67 .qam_demod_parameter_count = 2, 68 .microcode_name = "dvb-usb-terratec-h7-drxk.fw", 69 }; 70 71 static struct drxk_config cablestar_hdci_drxk = { 72 .adr = 0x29, 73 .parallel_ts = true, 74 .dynamic_clk = true, 75 .single_master = true, 76 .enable_merr_cfg = true, 77 .no_i2c_bridge = false, 78 .chunk_size = 64, 79 .mpeg_out_clk_strength = 0x02, 80 .qam_demod_parameter_count = 2, 81 .microcode_name = "dvb-usb-technisat-cablestar-hdci-drxk.fw", 82 }; 83 84 static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable) 85 { 86 struct az6007_device_state *st = fe_to_priv(fe); 87 struct dvb_usb_adapter *adap = fe->sec_priv; 88 int status = 0; 89 90 pr_debug("%s: %s\n", __func__, enable ? "enable" : "disable"); 91 92 if (!adap || !st) 93 return -EINVAL; 94 95 if (enable) 96 status = st->gate_ctrl(fe, 1); 97 else 98 status = st->gate_ctrl(fe, 0); 99 100 return status; 101 } 102 103 static struct mt2063_config az6007_mt2063_config = { 104 .tuner_address = 0x60, 105 .refclock = 36125000, 106 }; 107 108 static int __az6007_read(struct usb_device *udev, u8 req, u16 value, 109 u16 index, u8 *b, int blen) 110 { 111 int ret; 112 113 ret = usb_control_msg(udev, 114 usb_rcvctrlpipe(udev, 0), 115 req, 116 USB_TYPE_VENDOR | USB_DIR_IN, 117 value, index, b, blen, 5000); 118 if (ret < 0) { 119 pr_warn("usb read operation failed. (%d)\n", ret); 120 return -EIO; 121 } 122 123 if (az6007_xfer_debug) { 124 printk(KERN_DEBUG "az6007: IN req: %02x, value: %04x, index: %04x\n", 125 req, value, index); 126 print_hex_dump_bytes("az6007: payload: ", 127 DUMP_PREFIX_NONE, b, blen); 128 } 129 130 return ret; 131 } 132 133 static int az6007_read(struct dvb_usb_device *d, u8 req, u16 value, 134 u16 index, u8 *b, int blen) 135 { 136 struct az6007_device_state *st = d->priv; 137 int ret; 138 139 if (mutex_lock_interruptible(&st->mutex) < 0) 140 return -EAGAIN; 141 142 ret = __az6007_read(d->udev, req, value, index, b, blen); 143 144 mutex_unlock(&st->mutex); 145 146 return ret; 147 } 148 149 static int __az6007_write(struct usb_device *udev, u8 req, u16 value, 150 u16 index, u8 *b, int blen) 151 { 152 int ret; 153 154 if (az6007_xfer_debug) { 155 printk(KERN_DEBUG "az6007: OUT req: %02x, value: %04x, index: %04x\n", 156 req, value, index); 157 print_hex_dump_bytes("az6007: payload: ", 158 DUMP_PREFIX_NONE, b, blen); 159 } 160 161 if (blen > 64) { 162 pr_err("az6007: tried to write %d bytes, but I2C max size is 64 bytes\n", 163 blen); 164 return -EOPNOTSUPP; 165 } 166 167 ret = usb_control_msg(udev, 168 usb_sndctrlpipe(udev, 0), 169 req, 170 USB_TYPE_VENDOR | USB_DIR_OUT, 171 value, index, b, blen, 5000); 172 if (ret != blen) { 173 pr_err("usb write operation failed. (%d)\n", ret); 174 return -EIO; 175 } 176 177 return 0; 178 } 179 180 static int az6007_write(struct dvb_usb_device *d, u8 req, u16 value, 181 u16 index, u8 *b, int blen) 182 { 183 struct az6007_device_state *st = d->priv; 184 int ret; 185 186 if (mutex_lock_interruptible(&st->mutex) < 0) 187 return -EAGAIN; 188 189 ret = __az6007_write(d->udev, req, value, index, b, blen); 190 191 mutex_unlock(&st->mutex); 192 193 return ret; 194 } 195 196 static int az6007_streaming_ctrl(struct dvb_frontend *fe, int onoff) 197 { 198 struct dvb_usb_device *d = fe_to_d(fe); 199 200 pr_debug("%s: %s\n", __func__, onoff ? "enable" : "disable"); 201 202 return az6007_write(d, 0xbc, onoff, 0, NULL, 0); 203 } 204 205 #if IS_ENABLED(CONFIG_RC_CORE) 206 /* remote control stuff (does not work with my box) */ 207 static int az6007_rc_query(struct dvb_usb_device *d) 208 { 209 struct az6007_device_state *st = d_to_priv(d); 210 unsigned code; 211 enum rc_proto proto; 212 213 az6007_read(d, AZ6007_READ_IR, 0, 0, st->data, 10); 214 215 if (st->data[1] == 0x44) 216 return 0; 217 218 if ((st->data[3] ^ st->data[4]) == 0xff) { 219 if ((st->data[1] ^ st->data[2]) == 0xff) { 220 code = RC_SCANCODE_NEC(st->data[1], st->data[3]); 221 proto = RC_PROTO_NEC; 222 } else { 223 code = RC_SCANCODE_NECX(st->data[1] << 8 | st->data[2], 224 st->data[3]); 225 proto = RC_PROTO_NECX; 226 } 227 } else { 228 code = RC_SCANCODE_NEC32(st->data[1] << 24 | 229 st->data[2] << 16 | 230 st->data[3] << 8 | 231 st->data[4]); 232 proto = RC_PROTO_NEC32; 233 } 234 235 rc_keydown(d->rc_dev, proto, code, st->data[5]); 236 237 return 0; 238 } 239 240 static int az6007_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc) 241 { 242 pr_debug("Getting az6007 Remote Control properties\n"); 243 244 rc->allowed_protos = RC_PROTO_BIT_NEC | RC_PROTO_BIT_NECX | 245 RC_PROTO_BIT_NEC32; 246 rc->query = az6007_rc_query; 247 rc->interval = 400; 248 249 return 0; 250 } 251 #else 252 #define az6007_get_rc_config NULL 253 #endif 254 255 static int az6007_ci_read_attribute_mem(struct dvb_ca_en50221 *ca, 256 int slot, 257 int address) 258 { 259 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data; 260 struct az6007_device_state *state = d_to_priv(d); 261 262 int ret; 263 u8 req; 264 u16 value; 265 u16 index; 266 int blen; 267 u8 *b; 268 269 if (slot != 0) 270 return -EINVAL; 271 272 b = kmalloc(12, GFP_KERNEL); 273 if (!b) 274 return -ENOMEM; 275 276 mutex_lock(&state->ca_mutex); 277 278 req = 0xC1; 279 value = address; 280 index = 0; 281 blen = 1; 282 283 ret = az6007_read(d, req, value, index, b, blen); 284 if (ret < 0) { 285 pr_warn("usb in operation failed. (%d)\n", ret); 286 ret = -EINVAL; 287 } else { 288 ret = b[0]; 289 } 290 291 mutex_unlock(&state->ca_mutex); 292 kfree(b); 293 return ret; 294 } 295 296 static int az6007_ci_write_attribute_mem(struct dvb_ca_en50221 *ca, 297 int slot, 298 int address, 299 u8 value) 300 { 301 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data; 302 struct az6007_device_state *state = d_to_priv(d); 303 304 int ret; 305 u8 req; 306 u16 value1; 307 u16 index; 308 int blen; 309 310 pr_debug("%s(), slot %d\n", __func__, slot); 311 if (slot != 0) 312 return -EINVAL; 313 314 mutex_lock(&state->ca_mutex); 315 req = 0xC2; 316 value1 = address; 317 index = value; 318 blen = 0; 319 320 ret = az6007_write(d, req, value1, index, NULL, blen); 321 if (ret != 0) 322 pr_warn("usb out operation failed. (%d)\n", ret); 323 324 mutex_unlock(&state->ca_mutex); 325 return ret; 326 } 327 328 static int az6007_ci_read_cam_control(struct dvb_ca_en50221 *ca, 329 int slot, 330 u8 address) 331 { 332 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data; 333 struct az6007_device_state *state = d_to_priv(d); 334 335 int ret; 336 u8 req; 337 u16 value; 338 u16 index; 339 int blen; 340 u8 *b; 341 342 if (slot != 0) 343 return -EINVAL; 344 345 b = kmalloc(12, GFP_KERNEL); 346 if (!b) 347 return -ENOMEM; 348 349 mutex_lock(&state->ca_mutex); 350 351 req = 0xC3; 352 value = address; 353 index = 0; 354 blen = 2; 355 356 ret = az6007_read(d, req, value, index, b, blen); 357 if (ret < 0) { 358 pr_warn("usb in operation failed. (%d)\n", ret); 359 ret = -EINVAL; 360 } else { 361 if (b[0] == 0) 362 pr_warn("Read CI IO error\n"); 363 364 ret = b[1]; 365 pr_debug("read cam data = %x from 0x%x\n", b[1], value); 366 } 367 368 mutex_unlock(&state->ca_mutex); 369 kfree(b); 370 return ret; 371 } 372 373 static int az6007_ci_write_cam_control(struct dvb_ca_en50221 *ca, 374 int slot, 375 u8 address, 376 u8 value) 377 { 378 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data; 379 struct az6007_device_state *state = d_to_priv(d); 380 381 int ret; 382 u8 req; 383 u16 value1; 384 u16 index; 385 int blen; 386 387 if (slot != 0) 388 return -EINVAL; 389 390 mutex_lock(&state->ca_mutex); 391 req = 0xC4; 392 value1 = address; 393 index = value; 394 blen = 0; 395 396 ret = az6007_write(d, req, value1, index, NULL, blen); 397 if (ret != 0) { 398 pr_warn("usb out operation failed. (%d)\n", ret); 399 goto failed; 400 } 401 402 failed: 403 mutex_unlock(&state->ca_mutex); 404 return ret; 405 } 406 407 static int CI_CamReady(struct dvb_ca_en50221 *ca, int slot) 408 { 409 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data; 410 411 int ret; 412 u8 req; 413 u16 value; 414 u16 index; 415 int blen; 416 u8 *b; 417 418 b = kmalloc(12, GFP_KERNEL); 419 if (!b) 420 return -ENOMEM; 421 422 req = 0xC8; 423 value = 0; 424 index = 0; 425 blen = 1; 426 427 ret = az6007_read(d, req, value, index, b, blen); 428 if (ret < 0) { 429 pr_warn("usb in operation failed. (%d)\n", ret); 430 ret = -EIO; 431 } else{ 432 ret = b[0]; 433 } 434 kfree(b); 435 return ret; 436 } 437 438 static int az6007_ci_slot_reset(struct dvb_ca_en50221 *ca, int slot) 439 { 440 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data; 441 struct az6007_device_state *state = d_to_priv(d); 442 443 int ret, i; 444 u8 req; 445 u16 value; 446 u16 index; 447 int blen; 448 449 mutex_lock(&state->ca_mutex); 450 451 req = 0xC6; 452 value = 1; 453 index = 0; 454 blen = 0; 455 456 ret = az6007_write(d, req, value, index, NULL, blen); 457 if (ret != 0) { 458 pr_warn("usb out operation failed. (%d)\n", ret); 459 goto failed; 460 } 461 462 msleep(500); 463 req = 0xC6; 464 value = 0; 465 index = 0; 466 blen = 0; 467 468 ret = az6007_write(d, req, value, index, NULL, blen); 469 if (ret != 0) { 470 pr_warn("usb out operation failed. (%d)\n", ret); 471 goto failed; 472 } 473 474 for (i = 0; i < 15; i++) { 475 msleep(100); 476 477 if (CI_CamReady(ca, slot)) { 478 pr_debug("CAM Ready\n"); 479 break; 480 } 481 } 482 msleep(5000); 483 484 failed: 485 mutex_unlock(&state->ca_mutex); 486 return ret; 487 } 488 489 static int az6007_ci_slot_shutdown(struct dvb_ca_en50221 *ca, int slot) 490 { 491 return 0; 492 } 493 494 static int az6007_ci_slot_ts_enable(struct dvb_ca_en50221 *ca, int slot) 495 { 496 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data; 497 struct az6007_device_state *state = d_to_priv(d); 498 499 int ret; 500 u8 req; 501 u16 value; 502 u16 index; 503 int blen; 504 505 pr_debug("%s()\n", __func__); 506 mutex_lock(&state->ca_mutex); 507 req = 0xC7; 508 value = 1; 509 index = 0; 510 blen = 0; 511 512 ret = az6007_write(d, req, value, index, NULL, blen); 513 if (ret != 0) { 514 pr_warn("usb out operation failed. (%d)\n", ret); 515 goto failed; 516 } 517 518 failed: 519 mutex_unlock(&state->ca_mutex); 520 return ret; 521 } 522 523 static int az6007_ci_poll_slot_status(struct dvb_ca_en50221 *ca, int slot, int open) 524 { 525 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data; 526 struct az6007_device_state *state = d_to_priv(d); 527 int ret; 528 u8 req; 529 u16 value; 530 u16 index; 531 int blen; 532 u8 *b; 533 534 b = kmalloc(12, GFP_KERNEL); 535 if (!b) 536 return -ENOMEM; 537 mutex_lock(&state->ca_mutex); 538 539 req = 0xC5; 540 value = 0; 541 index = 0; 542 blen = 1; 543 544 ret = az6007_read(d, req, value, index, b, blen); 545 if (ret < 0) { 546 pr_warn("usb in operation failed. (%d)\n", ret); 547 ret = -EIO; 548 } else 549 ret = 0; 550 551 if (!ret && b[0] == 1) { 552 ret = DVB_CA_EN50221_POLL_CAM_PRESENT | 553 DVB_CA_EN50221_POLL_CAM_READY; 554 } 555 556 mutex_unlock(&state->ca_mutex); 557 kfree(b); 558 return ret; 559 } 560 561 562 static void az6007_ci_uninit(struct dvb_usb_device *d) 563 { 564 struct az6007_device_state *state; 565 566 pr_debug("%s()\n", __func__); 567 568 if (NULL == d) 569 return; 570 571 state = d_to_priv(d); 572 if (NULL == state) 573 return; 574 575 if (NULL == state->ca.data) 576 return; 577 578 dvb_ca_en50221_release(&state->ca); 579 580 memset(&state->ca, 0, sizeof(state->ca)); 581 } 582 583 584 static int az6007_ci_init(struct dvb_usb_adapter *adap) 585 { 586 struct dvb_usb_device *d = adap_to_d(adap); 587 struct az6007_device_state *state = adap_to_priv(adap); 588 int ret; 589 590 pr_debug("%s()\n", __func__); 591 592 mutex_init(&state->ca_mutex); 593 state->ca.owner = THIS_MODULE; 594 state->ca.read_attribute_mem = az6007_ci_read_attribute_mem; 595 state->ca.write_attribute_mem = az6007_ci_write_attribute_mem; 596 state->ca.read_cam_control = az6007_ci_read_cam_control; 597 state->ca.write_cam_control = az6007_ci_write_cam_control; 598 state->ca.slot_reset = az6007_ci_slot_reset; 599 state->ca.slot_shutdown = az6007_ci_slot_shutdown; 600 state->ca.slot_ts_enable = az6007_ci_slot_ts_enable; 601 state->ca.poll_slot_status = az6007_ci_poll_slot_status; 602 state->ca.data = d; 603 604 ret = dvb_ca_en50221_init(&adap->dvb_adap, 605 &state->ca, 606 0, /* flags */ 607 1);/* n_slots */ 608 if (ret != 0) { 609 pr_err("Cannot initialize CI: Error %d.\n", ret); 610 memset(&state->ca, 0, sizeof(state->ca)); 611 return ret; 612 } 613 614 pr_debug("CI initialized.\n"); 615 616 return 0; 617 } 618 619 static int az6007_read_mac_addr(struct dvb_usb_adapter *adap, u8 mac[6]) 620 { 621 struct dvb_usb_device *d = adap_to_d(adap); 622 struct az6007_device_state *st = adap_to_priv(adap); 623 int ret; 624 625 ret = az6007_read(d, AZ6007_READ_DATA, 6, 0, st->data, 6); 626 memcpy(mac, st->data, 6); 627 628 if (ret > 0) 629 pr_debug("%s: mac is %pM\n", __func__, mac); 630 631 return ret; 632 } 633 634 static int az6007_frontend_attach(struct dvb_usb_adapter *adap) 635 { 636 struct az6007_device_state *st = adap_to_priv(adap); 637 struct dvb_usb_device *d = adap_to_d(adap); 638 639 pr_debug("attaching demod drxk\n"); 640 641 adap->fe[0] = dvb_attach(drxk_attach, &terratec_h7_drxk, 642 &d->i2c_adap); 643 if (!adap->fe[0]) 644 return -EINVAL; 645 646 adap->fe[0]->sec_priv = adap; 647 st->gate_ctrl = adap->fe[0]->ops.i2c_gate_ctrl; 648 adap->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl; 649 650 az6007_ci_init(adap); 651 652 return 0; 653 } 654 655 static int az6007_cablestar_hdci_frontend_attach(struct dvb_usb_adapter *adap) 656 { 657 struct az6007_device_state *st = adap_to_priv(adap); 658 struct dvb_usb_device *d = adap_to_d(adap); 659 660 pr_debug("attaching demod drxk\n"); 661 662 adap->fe[0] = dvb_attach(drxk_attach, &cablestar_hdci_drxk, 663 &d->i2c_adap); 664 if (!adap->fe[0]) 665 return -EINVAL; 666 667 adap->fe[0]->sec_priv = adap; 668 st->gate_ctrl = adap->fe[0]->ops.i2c_gate_ctrl; 669 adap->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl; 670 671 az6007_ci_init(adap); 672 673 return 0; 674 } 675 676 static int az6007_tuner_attach(struct dvb_usb_adapter *adap) 677 { 678 struct dvb_usb_device *d = adap_to_d(adap); 679 680 pr_debug("attaching tuner mt2063\n"); 681 682 /* Attach mt2063 to DVB-C frontend */ 683 if (adap->fe[0]->ops.i2c_gate_ctrl) 684 adap->fe[0]->ops.i2c_gate_ctrl(adap->fe[0], 1); 685 if (!dvb_attach(mt2063_attach, adap->fe[0], 686 &az6007_mt2063_config, 687 &d->i2c_adap)) 688 return -EINVAL; 689 690 if (adap->fe[0]->ops.i2c_gate_ctrl) 691 adap->fe[0]->ops.i2c_gate_ctrl(adap->fe[0], 0); 692 693 return 0; 694 } 695 696 static int az6007_power_ctrl(struct dvb_usb_device *d, int onoff) 697 { 698 struct az6007_device_state *state = d_to_priv(d); 699 int ret; 700 701 pr_debug("%s()\n", __func__); 702 703 if (!state->warm) { 704 mutex_init(&state->mutex); 705 706 ret = az6007_write(d, AZ6007_POWER, 0, 2, NULL, 0); 707 if (ret < 0) 708 return ret; 709 msleep(60); 710 ret = az6007_write(d, AZ6007_POWER, 1, 4, NULL, 0); 711 if (ret < 0) 712 return ret; 713 msleep(100); 714 ret = az6007_write(d, AZ6007_POWER, 1, 3, NULL, 0); 715 if (ret < 0) 716 return ret; 717 msleep(20); 718 ret = az6007_write(d, AZ6007_POWER, 1, 4, NULL, 0); 719 if (ret < 0) 720 return ret; 721 722 msleep(400); 723 ret = az6007_write(d, FX2_SCON1, 0, 3, NULL, 0); 724 if (ret < 0) 725 return ret; 726 msleep(150); 727 ret = az6007_write(d, FX2_SCON1, 1, 3, NULL, 0); 728 if (ret < 0) 729 return ret; 730 msleep(430); 731 ret = az6007_write(d, AZ6007_POWER, 0, 0, NULL, 0); 732 if (ret < 0) 733 return ret; 734 735 state->warm = true; 736 737 return 0; 738 } 739 740 if (!onoff) 741 return 0; 742 743 az6007_write(d, AZ6007_POWER, 0, 0, NULL, 0); 744 az6007_write(d, AZ6007_TS_THROUGH, 0, 0, NULL, 0); 745 746 return 0; 747 } 748 749 /* I2C */ 750 static int az6007_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], 751 int num) 752 { 753 struct dvb_usb_device *d = i2c_get_adapdata(adap); 754 struct az6007_device_state *st = d_to_priv(d); 755 int i, j, len; 756 int ret = 0; 757 u16 index; 758 u16 value; 759 int length; 760 u8 req, addr; 761 762 if (mutex_lock_interruptible(&st->mutex) < 0) 763 return -EAGAIN; 764 765 for (i = 0; i < num; i++) { 766 addr = msgs[i].addr << 1; 767 if (((i + 1) < num) 768 && (msgs[i].len == 1) 769 && ((msgs[i].flags & I2C_M_RD) != I2C_M_RD) 770 && (msgs[i + 1].flags & I2C_M_RD) 771 && (msgs[i].addr == msgs[i + 1].addr)) { 772 /* 773 * A write + read xfer for the same address, where 774 * the first xfer has just 1 byte length. 775 * Need to join both into one operation 776 */ 777 if (az6007_xfer_debug) 778 printk(KERN_DEBUG "az6007: I2C W/R addr=0x%x len=%d/%d\n", 779 addr, msgs[i].len, msgs[i + 1].len); 780 req = AZ6007_I2C_RD; 781 index = msgs[i].buf[0]; 782 value = addr | (1 << 8); 783 length = 6 + msgs[i + 1].len; 784 len = msgs[i + 1].len; 785 ret = __az6007_read(d->udev, req, value, index, 786 st->data, length); 787 if (ret >= len) { 788 for (j = 0; j < len; j++) 789 msgs[i + 1].buf[j] = st->data[j + 5]; 790 } else 791 ret = -EIO; 792 i++; 793 } else if (!(msgs[i].flags & I2C_M_RD)) { 794 /* write bytes */ 795 if (az6007_xfer_debug) 796 printk(KERN_DEBUG "az6007: I2C W addr=0x%x len=%d\n", 797 addr, msgs[i].len); 798 req = AZ6007_I2C_WR; 799 index = msgs[i].buf[0]; 800 value = addr | (1 << 8); 801 length = msgs[i].len - 1; 802 len = msgs[i].len - 1; 803 for (j = 0; j < len; j++) 804 st->data[j] = msgs[i].buf[j + 1]; 805 ret = __az6007_write(d->udev, req, value, index, 806 st->data, length); 807 } else { 808 /* read bytes */ 809 if (az6007_xfer_debug) 810 printk(KERN_DEBUG "az6007: I2C R addr=0x%x len=%d\n", 811 addr, msgs[i].len); 812 req = AZ6007_I2C_RD; 813 index = msgs[i].buf[0]; 814 value = addr; 815 length = msgs[i].len + 6; 816 len = msgs[i].len; 817 ret = __az6007_read(d->udev, req, value, index, 818 st->data, length); 819 for (j = 0; j < len; j++) 820 msgs[i].buf[j] = st->data[j + 5]; 821 } 822 if (ret < 0) 823 goto err; 824 } 825 err: 826 mutex_unlock(&st->mutex); 827 828 if (ret < 0) { 829 pr_info("%s ERROR: %i\n", __func__, ret); 830 return ret; 831 } 832 return num; 833 } 834 835 static u32 az6007_i2c_func(struct i2c_adapter *adapter) 836 { 837 return I2C_FUNC_I2C; 838 } 839 840 static struct i2c_algorithm az6007_i2c_algo = { 841 .master_xfer = az6007_i2c_xfer, 842 .functionality = az6007_i2c_func, 843 }; 844 845 static int az6007_identify_state(struct dvb_usb_device *d, const char **name) 846 { 847 int ret; 848 u8 *mac; 849 850 pr_debug("Identifying az6007 state\n"); 851 852 mac = kmalloc(6, GFP_ATOMIC); 853 if (!mac) 854 return -ENOMEM; 855 856 /* Try to read the mac address */ 857 ret = __az6007_read(d->udev, AZ6007_READ_DATA, 6, 0, mac, 6); 858 if (ret == 6) 859 ret = WARM; 860 else 861 ret = COLD; 862 863 kfree(mac); 864 865 if (ret == COLD) { 866 __az6007_write(d->udev, 0x09, 1, 0, NULL, 0); 867 __az6007_write(d->udev, 0x00, 0, 0, NULL, 0); 868 __az6007_write(d->udev, 0x00, 0, 0, NULL, 0); 869 } 870 871 pr_debug("Device is on %s state\n", 872 ret == WARM ? "warm" : "cold"); 873 return ret; 874 } 875 876 static void az6007_usb_disconnect(struct usb_interface *intf) 877 { 878 struct dvb_usb_device *d = usb_get_intfdata(intf); 879 az6007_ci_uninit(d); 880 dvb_usbv2_disconnect(intf); 881 } 882 883 static int az6007_download_firmware(struct dvb_usb_device *d, 884 const struct firmware *fw) 885 { 886 pr_debug("Loading az6007 firmware\n"); 887 888 return cypress_load_firmware(d->udev, fw, CYPRESS_FX2); 889 } 890 891 /* DVB USB Driver stuff */ 892 static struct dvb_usb_device_properties az6007_props = { 893 .driver_name = KBUILD_MODNAME, 894 .owner = THIS_MODULE, 895 .firmware = AZ6007_FIRMWARE, 896 897 .adapter_nr = adapter_nr, 898 .size_of_priv = sizeof(struct az6007_device_state), 899 .i2c_algo = &az6007_i2c_algo, 900 .tuner_attach = az6007_tuner_attach, 901 .frontend_attach = az6007_frontend_attach, 902 .streaming_ctrl = az6007_streaming_ctrl, 903 .get_rc_config = az6007_get_rc_config, 904 .read_mac_address = az6007_read_mac_addr, 905 .download_firmware = az6007_download_firmware, 906 .identify_state = az6007_identify_state, 907 .power_ctrl = az6007_power_ctrl, 908 .num_adapters = 1, 909 .adapter = { 910 { .stream = DVB_USB_STREAM_BULK(0x02, 10, 4096), } 911 } 912 }; 913 914 static struct dvb_usb_device_properties az6007_cablestar_hdci_props = { 915 .driver_name = KBUILD_MODNAME, 916 .owner = THIS_MODULE, 917 .firmware = AZ6007_FIRMWARE, 918 919 .adapter_nr = adapter_nr, 920 .size_of_priv = sizeof(struct az6007_device_state), 921 .i2c_algo = &az6007_i2c_algo, 922 .tuner_attach = az6007_tuner_attach, 923 .frontend_attach = az6007_cablestar_hdci_frontend_attach, 924 .streaming_ctrl = az6007_streaming_ctrl, 925 /* ditch get_rc_config as it can't work (TS35 remote, I believe it's rc5) */ 926 .get_rc_config = NULL, 927 .read_mac_address = az6007_read_mac_addr, 928 .download_firmware = az6007_download_firmware, 929 .identify_state = az6007_identify_state, 930 .power_ctrl = az6007_power_ctrl, 931 .num_adapters = 1, 932 .adapter = { 933 { .stream = DVB_USB_STREAM_BULK(0x02, 10, 4096), } 934 } 935 }; 936 937 static const struct usb_device_id az6007_usb_table[] = { 938 {DVB_USB_DEVICE(USB_VID_AZUREWAVE, USB_PID_AZUREWAVE_6007, 939 &az6007_props, "Azurewave 6007", RC_MAP_EMPTY)}, 940 {DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_H7, 941 &az6007_props, "Terratec H7", RC_MAP_NEC_TERRATEC_CINERGY_XS)}, 942 {DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_H7_2, 943 &az6007_props, "Terratec H7", RC_MAP_NEC_TERRATEC_CINERGY_XS)}, 944 {DVB_USB_DEVICE(USB_VID_TECHNISAT, USB_PID_TECHNISAT_USB2_CABLESTAR_HDCI, 945 &az6007_cablestar_hdci_props, "Technisat CableStar Combo HD CI", RC_MAP_EMPTY)}, 946 {0}, 947 }; 948 949 MODULE_DEVICE_TABLE(usb, az6007_usb_table); 950 951 static int az6007_suspend(struct usb_interface *intf, pm_message_t msg) 952 { 953 struct dvb_usb_device *d = usb_get_intfdata(intf); 954 955 az6007_ci_uninit(d); 956 return dvb_usbv2_suspend(intf, msg); 957 } 958 959 static int az6007_resume(struct usb_interface *intf) 960 { 961 struct dvb_usb_device *d = usb_get_intfdata(intf); 962 struct dvb_usb_adapter *adap = &d->adapter[0]; 963 964 az6007_ci_init(adap); 965 return dvb_usbv2_resume(intf); 966 } 967 968 /* usb specific object needed to register this driver with the usb subsystem */ 969 static struct usb_driver az6007_usb_driver = { 970 .name = KBUILD_MODNAME, 971 .id_table = az6007_usb_table, 972 .probe = dvb_usbv2_probe, 973 .disconnect = az6007_usb_disconnect, 974 .no_dynamic_id = 1, 975 .soft_unbind = 1, 976 /* 977 * FIXME: need to implement reset_resume, likely with 978 * dvb-usb-v2 core support 979 */ 980 .suspend = az6007_suspend, 981 .resume = az6007_resume, 982 }; 983 984 module_usb_driver(az6007_usb_driver); 985 986 MODULE_AUTHOR("Henry Wang <Henry.wang@AzureWave.com>"); 987 MODULE_AUTHOR("Mauro Carvalho Chehab"); 988 MODULE_DESCRIPTION("Driver for AzureWave 6007 DVB-C/T USB2.0 and clones"); 989 MODULE_VERSION("2.0"); 990 MODULE_LICENSE("GPL"); 991 MODULE_FIRMWARE(AZ6007_FIRMWARE); 992