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