az6007.c 20.4 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * Driver for AzureWave 6007 DVB-C/T USB2.0 and clones
 *
 * Copyright (c) Henry Wang <Henry.wang@AzureWave.com>
 *
 * This driver was made publicly available by Terratec, at:
 *	http://linux.terratec.de/files/TERRATEC_H7/20110323_TERRATEC_H7_Linux.tar.gz
 * The original driver's license is GPL, as declared with MODULE_LICENSE()
 *
10
 * Copyright (c) 2010-2012 Mauro Carvalho Chehab <mchehab@redhat.com>
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 15 16 17 18 19 20 21
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation under version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
22 23 24 25 26
 */

#include "drxk.h"
#include "mt2063.h"
#include "dvb_ca_en50221.h"
27 28
#include "dvb_usb.h"
#include "cypress_firmware.h"
29

30
#define AZ6007_FIRMWARE "dvb-usb-terratec-h7-az6007.fw"
31

32 33 34
static int az6007_xfer_debug;
module_param_named(xfer_debug, az6007_xfer_debug, int, 0644);
MODULE_PARM_DESC(xfer_debug, "Enable xfer debug");
35

36 37
DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);

38 39 40 41 42 43 44 45 46
/* Known requests (Cypress FX2 firmware + az6007 "private" ones*/

#define FX2_OED			0xb5
#define AZ6007_READ_DATA	0xb7
#define AZ6007_I2C_RD		0xb9
#define AZ6007_POWER		0xbc
#define AZ6007_I2C_WR		0xbd
#define FX2_SCON1		0xc0
#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 53
	struct dvb_ca_en50221	ca;
	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 61 62
	.parallel_ts = true,
	.dynamic_clk = true,
	.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 72
static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
{
73
	struct az6007_device_state *st = fe_to_priv(fe);
74
	struct dvb_usb_adapter *adap = fe->sec_priv;
75
	int status = 0;
76

77
	pr_debug("%s: %s\n", __func__, enable ? "enable" : "disable");
78

79
	if (!adap || !st)
80 81
		return -EINVAL;

82
	if (enable)
83
		status = st->gate_ctrl(fe, 1);
84
	else
85
		status = st->gate_ctrl(fe, 0);
86

87 88 89
	return status;
}

90
static struct mt2063_config az6007_mt2063_config = {
91
	.tuner_address = 0x60,
92 93 94
	.refclock = 36125000,
};

95
static int __az6007_read(struct usb_device *udev, u8 req, u16 value,
96
			    u16 index, u8 *b, int blen)
97
{
98
	int ret;
99

100 101
	ret = usb_control_msg(udev,
			      usb_rcvctrlpipe(udev, 0),
102 103 104
			      req,
			      USB_TYPE_VENDOR | USB_DIR_IN,
			      value, index, b, blen, 5000);
105
	if (ret < 0) {
106
		pr_warn("usb read operation failed. (%d)\n", ret);
107 108
		return -EIO;
	}
109

110 111 112 113 114 115
	if (az6007_xfer_debug) {
		printk(KERN_DEBUG "az6007: IN  req: %02x, value: %04x, index: %04x\n",
		       req, value, index);
		print_hex_dump_bytes("az6007: payload: ",
				     DUMP_PREFIX_NONE, b, blen);
	}
116 117 118 119

	return ret;
}

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
static int az6007_read(struct dvb_usb_device *d, u8 req, u16 value,
			    u16 index, u8 *b, int blen)
{
	struct az6007_device_state *st = d->priv;
	int ret;

	if (mutex_lock_interruptible(&st->mutex) < 0)
		return -EAGAIN;

	ret = __az6007_read(d->udev, req, value, index, b, blen);

	mutex_unlock(&st->mutex);

	return ret;
}

static int __az6007_write(struct usb_device *udev, u8 req, u16 value,
137 138 139 140
			     u16 index, u8 *b, int blen)
{
	int ret;

141 142 143 144 145 146
	if (az6007_xfer_debug) {
		printk(KERN_DEBUG "az6007: OUT req: %02x, value: %04x, index: %04x\n",
		       req, value, index);
		print_hex_dump_bytes("az6007: payload: ",
				     DUMP_PREFIX_NONE, b, blen);
	}
147

148
	if (blen > 64) {
149 150
		pr_err("az6007: tried to write %d bytes, but I2C max size is 64 bytes\n",
		       blen);
151
		return -EOPNOTSUPP;
152
	}
153

154 155
	ret = usb_control_msg(udev,
			      usb_sndctrlpipe(udev, 0),
156 157 158 159
			      req,
			      USB_TYPE_VENDOR | USB_DIR_OUT,
			      value, index, b, blen, 5000);
	if (ret != blen) {
160
		pr_err("usb write operation failed. (%d)\n", ret);
161
		return -EIO;
162
	}
163

164 165 166
	return 0;
}

167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
static int az6007_write(struct dvb_usb_device *d, u8 req, u16 value,
			    u16 index, u8 *b, int blen)
{
	struct az6007_device_state *st = d->priv;
	int ret;

	if (mutex_lock_interruptible(&st->mutex) < 0)
		return -EAGAIN;

	ret = __az6007_write(d->udev, req, value, index, b, blen);

	mutex_unlock(&st->mutex);

	return ret;
}

183
static int az6007_streaming_ctrl(struct dvb_frontend *fe, int onoff)
184
{
185
	struct dvb_usb_device *d = fe_to_d(fe);
186

187
	pr_debug("%s: %s\n", __func__, onoff ? "enable" : "disable");
188

189
	return az6007_write(d, 0xbc, onoff, 0, NULL, 0);
190 191 192
}

/* remote control stuff (does not work with my box) */
193
static int az6007_rc_query(struct dvb_usb_device *d)
194
{
195
	struct az6007_device_state *st = d_to_priv(d);
196
	unsigned code = 0;
197

198
	az6007_read(d, AZ6007_READ_IR, 0, 0, st->data, 10);
199

200
	if (st->data[1] == 0x44)
201 202
		return 0;

203 204 205 206 207 208 209 210
	if ((st->data[1] ^ st->data[2]) == 0xff)
		code = st->data[1];
	else
		code = st->data[1] << 8 | st->data[2];

	if ((st->data[3] ^ st->data[4]) == 0xff)
		code = code << 8 | st->data[3];
	else
211
		code = code << 16 | st->data[3] << 8 | st->data[4];
212

213
	rc_keydown(d->rc_dev, code, st->data[5]);
214

215 216 217
	return 0;
}

218 219 220 221 222
static int az6007_ci_read_attribute_mem(struct dvb_ca_en50221 *ca,
					int slot,
					int address)
{
	struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
223
	struct az6007_device_state *state = d_to_priv(d);
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247

	int ret;
	u8 req;
	u16 value;
	u16 index;
	int blen;
	u8 *b;

	if (slot != 0)
		return -EINVAL;

	b = kmalloc(12, GFP_KERNEL);
	if (!b)
		return -ENOMEM;

	mutex_lock(&state->ca_mutex);

	req = 0xC1;
	value = address;
	index = 0;
	blen = 1;

	ret = az6007_read(d, req, value, index, b, blen);
	if (ret < 0) {
248
		pr_warn("usb in operation failed. (%d)\n", ret);
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
		ret = -EINVAL;
	} else {
		ret = b[0];
	}

	mutex_unlock(&state->ca_mutex);
	kfree(b);
	return ret;
}

static int az6007_ci_write_attribute_mem(struct dvb_ca_en50221 *ca,
					 int slot,
					 int address,
					 u8 value)
{
	struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
265
	struct az6007_device_state *state = d_to_priv(d);
266 267 268 269 270 271 272

	int ret;
	u8 req;
	u16 value1;
	u16 index;
	int blen;

273
	pr_debug("%s(), slot %d\n", __func__, slot);
274 275 276 277 278 279 280 281 282 283 284
	if (slot != 0)
		return -EINVAL;

	mutex_lock(&state->ca_mutex);
	req = 0xC2;
	value1 = address;
	index = value;
	blen = 0;

	ret = az6007_write(d, req, value1, index, NULL, blen);
	if (ret != 0)
285
		pr_warn("usb out operation failed. (%d)\n", ret);
286 287 288 289 290 291 292 293 294 295

	mutex_unlock(&state->ca_mutex);
	return ret;
}

static int az6007_ci_read_cam_control(struct dvb_ca_en50221 *ca,
				      int slot,
				      u8 address)
{
	struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
296
	struct az6007_device_state *state = d_to_priv(d);
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320

	int ret;
	u8 req;
	u16 value;
	u16 index;
	int blen;
	u8 *b;

	if (slot != 0)
		return -EINVAL;

	b = kmalloc(12, GFP_KERNEL);
	if (!b)
		return -ENOMEM;

	mutex_lock(&state->ca_mutex);

	req = 0xC3;
	value = address;
	index = 0;
	blen = 2;

	ret = az6007_read(d, req, value, index, b, blen);
	if (ret < 0) {
321
		pr_warn("usb in operation failed. (%d)\n", ret);
322 323 324
		ret = -EINVAL;
	} else {
		if (b[0] == 0)
325
			pr_warn("Read CI IO error\n");
326 327

		ret = b[1];
328
		pr_debug("read cam data = %x from 0x%x\n", b[1], value);
329 330 331 332 333 334 335 336 337 338 339 340 341
	}

	mutex_unlock(&state->ca_mutex);
	kfree(b);
	return ret;
}

static int az6007_ci_write_cam_control(struct dvb_ca_en50221 *ca,
				       int slot,
				       u8 address,
				       u8 value)
{
	struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
342
	struct az6007_device_state *state = d_to_priv(d);
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360

	int ret;
	u8 req;
	u16 value1;
	u16 index;
	int blen;

	if (slot != 0)
		return -EINVAL;

	mutex_lock(&state->ca_mutex);
	req = 0xC4;
	value1 = address;
	index = value;
	blen = 0;

	ret = az6007_write(d, req, value1, index, NULL, blen);
	if (ret != 0) {
361
		pr_warn("usb out operation failed. (%d)\n", ret);
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
		goto failed;
	}

failed:
	mutex_unlock(&state->ca_mutex);
	return ret;
}

static int CI_CamReady(struct dvb_ca_en50221 *ca, int slot)
{
	struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;

	int ret;
	u8 req;
	u16 value;
	u16 index;
	int blen;
	u8 *b;

	b = kmalloc(12, GFP_KERNEL);
	if (!b)
		return -ENOMEM;

	req = 0xC8;
	value = 0;
	index = 0;
	blen = 1;

	ret = az6007_read(d, req, value, index, b, blen);
	if (ret < 0) {
392
		pr_warn("usb in operation failed. (%d)\n", ret);
393 394 395 396 397 398 399 400 401 402 403
		ret = -EIO;
	} else{
		ret = b[0];
	}
	kfree(b);
	return ret;
}

static int az6007_ci_slot_reset(struct dvb_ca_en50221 *ca, int slot)
{
	struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
404
	struct az6007_device_state *state = d_to_priv(d);
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420

	int ret, i;
	u8 req;
	u16 value;
	u16 index;
	int blen;

	mutex_lock(&state->ca_mutex);

	req = 0xC6;
	value = 1;
	index = 0;
	blen = 0;

	ret = az6007_write(d, req, value, index, NULL, blen);
	if (ret != 0) {
421
		pr_warn("usb out operation failed. (%d)\n", ret);
422 423 424 425 426 427 428 429 430 431 432
		goto failed;
	}

	msleep(500);
	req = 0xC6;
	value = 0;
	index = 0;
	blen = 0;

	ret = az6007_write(d, req, value, index, NULL, blen);
	if (ret != 0) {
433
		pr_warn("usb out operation failed. (%d)\n", ret);
434 435 436 437 438 439 440
		goto failed;
	}

	for (i = 0; i < 15; i++) {
		msleep(100);

		if (CI_CamReady(ca, slot)) {
441
			pr_debug("CAM Ready\n");
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
			break;
		}
	}
	msleep(5000);

failed:
	mutex_unlock(&state->ca_mutex);
	return ret;
}

static int az6007_ci_slot_shutdown(struct dvb_ca_en50221 *ca, int slot)
{
	return 0;
}

static int az6007_ci_slot_ts_enable(struct dvb_ca_en50221 *ca, int slot)
{
	struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
460
	struct az6007_device_state *state = d_to_priv(d);
461 462 463 464 465 466 467

	int ret;
	u8 req;
	u16 value;
	u16 index;
	int blen;

468
	pr_debug("%s()\n", __func__);
469 470 471 472 473 474 475 476
	mutex_lock(&state->ca_mutex);
	req = 0xC7;
	value = 1;
	index = 0;
	blen = 0;

	ret = az6007_write(d, req, value, index, NULL, blen);
	if (ret != 0) {
477
		pr_warn("usb out operation failed. (%d)\n", ret);
478 479 480 481 482 483 484 485 486 487 488
		goto failed;
	}

failed:
	mutex_unlock(&state->ca_mutex);
	return ret;
}

static int az6007_ci_poll_slot_status(struct dvb_ca_en50221 *ca, int slot, int open)
{
	struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
489
	struct az6007_device_state *state = d_to_priv(d);
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
	int ret;
	u8 req;
	u16 value;
	u16 index;
	int blen;
	u8 *b;

	b = kmalloc(12, GFP_KERNEL);
	if (!b)
		return -ENOMEM;
	mutex_lock(&state->ca_mutex);

	req = 0xC5;
	value = 0;
	index = 0;
	blen = 1;

	ret = az6007_read(d, req, value, index, b, blen);
	if (ret < 0) {
509
		pr_warn("usb in operation failed. (%d)\n", ret);
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
		ret = -EIO;
	} else
		ret = 0;

	if (!ret && b[0] == 1) {
		ret = DVB_CA_EN50221_POLL_CAM_PRESENT |
		      DVB_CA_EN50221_POLL_CAM_READY;
	}

	mutex_unlock(&state->ca_mutex);
	kfree(b);
	return ret;
}


static void az6007_ci_uninit(struct dvb_usb_device *d)
{
	struct az6007_device_state *state;

529
	pr_debug("%s()\n", __func__);
530 531 532 533

	if (NULL == d)
		return;

534
	state = d_to_priv(d);
535 536 537 538 539 540 541 542 543 544 545 546
	if (NULL == state)
		return;

	if (NULL == state->ca.data)
		return;

	dvb_ca_en50221_release(&state->ca);

	memset(&state->ca, 0, sizeof(state->ca));
}


547
static int az6007_ci_init(struct dvb_usb_adapter *adap)
548
{
549 550
	struct dvb_usb_device *d = adap_to_d(adap);
	struct az6007_device_state *state = adap_to_priv(adap);
551 552
	int ret;

553
	pr_debug("%s()\n", __func__);
554 555 556 557 558 559 560 561 562 563 564 565 566

	mutex_init(&state->ca_mutex);
	state->ca.owner			= THIS_MODULE;
	state->ca.read_attribute_mem	= az6007_ci_read_attribute_mem;
	state->ca.write_attribute_mem	= az6007_ci_write_attribute_mem;
	state->ca.read_cam_control	= az6007_ci_read_cam_control;
	state->ca.write_cam_control	= az6007_ci_write_cam_control;
	state->ca.slot_reset		= az6007_ci_slot_reset;
	state->ca.slot_shutdown		= az6007_ci_slot_shutdown;
	state->ca.slot_ts_enable	= az6007_ci_slot_ts_enable;
	state->ca.poll_slot_status	= az6007_ci_poll_slot_status;
	state->ca.data			= d;

567
	ret = dvb_ca_en50221_init(&adap->dvb_adap,
568 569 570 571
				  &state->ca,
				  0, /* flags */
				  1);/* n_slots */
	if (ret != 0) {
572
		pr_err("Cannot initialize CI: Error %d.\n", ret);
573 574 575 576
		memset(&state->ca, 0, sizeof(state->ca));
		return ret;
	}

577
	pr_debug("CI initialized.\n");
578 579 580 581

	return 0;
}

582
static int az6007_read_mac_addr(struct dvb_usb_adapter *adap, u8 mac[6])
583
{
584 585
	struct dvb_usb_device *d = adap_to_d(adap);
	struct az6007_device_state *st = adap_to_priv(adap);
586
	int ret;
587 588

	ret = az6007_read(d, AZ6007_READ_DATA, 6, 0, st->data, 6);
A
Alan Cox 已提交
589
	memcpy(mac, st->data, 6);
590

591
	if (ret > 0)
592
		pr_debug("%s: mac is %pM\n", __func__, mac);
593 594 595

	return ret;
}
596

597 598
static int az6007_frontend_attach(struct dvb_usb_adapter *adap)
{
599 600
	struct az6007_device_state *st = adap_to_priv(adap);
	struct dvb_usb_device *d = adap_to_d(adap);
601

602
	pr_debug("attaching demod drxk\n");
603

604 605 606
	adap->fe[0] = dvb_attach(drxk_attach, &terratec_h7_drxk,
				 &d->i2c_adap);
	if (!adap->fe[0])
607
		return -EINVAL;
608

609 610 611
	adap->fe[0]->sec_priv = adap;
	st->gate_ctrl = adap->fe[0]->ops.i2c_gate_ctrl;
	adap->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
612

613 614
	az6007_ci_init(adap);

615 616 617 618 619
	return 0;
}

static int az6007_tuner_attach(struct dvb_usb_adapter *adap)
{
620 621 622
	struct dvb_usb_device *d = adap_to_d(adap);

	pr_debug("attaching tuner mt2063\n");
623

624
	/* Attach mt2063 to DVB-C frontend */
625 626 627
	if (adap->fe[0]->ops.i2c_gate_ctrl)
		adap->fe[0]->ops.i2c_gate_ctrl(adap->fe[0], 1);
	if (!dvb_attach(mt2063_attach, adap->fe[0],
628
			&az6007_mt2063_config,
629
			&d->i2c_adap))
630
		return -EINVAL;
631

632 633
	if (adap->fe[0]->ops.i2c_gate_ctrl)
		adap->fe[0]->ops.i2c_gate_ctrl(adap->fe[0], 0);
634

635 636 637
	return 0;
}

638
static int az6007_power_ctrl(struct dvb_usb_device *d, int onoff)
639
{
640
	struct az6007_device_state *state = d_to_priv(d);
641 642
	int ret;

643
	pr_debug("%s()\n", __func__);
644

645 646
	if (!state->warm) {
		mutex_init(&state->mutex);
647

648
		ret = az6007_write(d, AZ6007_POWER, 0, 2, NULL, 0);
649 650 651
		if (ret < 0)
			return ret;
		msleep(60);
652
		ret = az6007_write(d, AZ6007_POWER, 1, 4, NULL, 0);
653 654 655
		if (ret < 0)
			return ret;
		msleep(100);
656
		ret = az6007_write(d, AZ6007_POWER, 1, 3, NULL, 0);
657 658 659
		if (ret < 0)
			return ret;
		msleep(20);
660
		ret = az6007_write(d, AZ6007_POWER, 1, 4, NULL, 0);
661 662 663 664
		if (ret < 0)
			return ret;

		msleep(400);
665
		ret = az6007_write(d, FX2_SCON1, 0, 3, NULL, 0);
666 667
		if (ret < 0)
			return ret;
668
		msleep(150);
669
		ret = az6007_write(d, FX2_SCON1, 1, 3, NULL, 0);
670 671
		if (ret < 0)
			return ret;
672
		msleep(430);
673
		ret = az6007_write(d, AZ6007_POWER, 0, 0, NULL, 0);
674 675
		if (ret < 0)
			return ret;
676

677
		state->warm = true;
678

679 680
		return 0;
	}
681

682 683 684
	if (!onoff)
		return 0;

685 686
	az6007_write(d, AZ6007_POWER, 0, 0, NULL, 0);
	az6007_write(d, AZ6007_TS_THROUGH, 0, 0, NULL, 0);
687 688 689 690

	return 0;
}

691
/* I2C */
692 693
static int az6007_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
			   int num)
694 695
{
	struct dvb_usb_device *d = i2c_get_adapdata(adap);
696
	struct az6007_device_state *st = d_to_priv(d);
697 698
	int i, j, len;
	int ret = 0;
699 700 701
	u16 index;
	u16 value;
	int length;
702
	u8 req, addr;
703

704
	if (mutex_lock_interruptible(&st->mutex) < 0)
705 706
		return -EAGAIN;

707 708 709 710
	for (i = 0; i < num; i++) {
		addr = msgs[i].addr << 1;
		if (((i + 1) < num)
		    && (msgs[i].len == 1)
711
		    && ((msgs[i].flags & I2C_M_RD) != I2C_M_RD)
712 713 714 715 716 717 718
		    && (msgs[i + 1].flags & I2C_M_RD)
		    && (msgs[i].addr == msgs[i + 1].addr)) {
			/*
			 * A write + read xfer for the same address, where
			 * the first xfer has just 1 byte length.
			 * Need to join both into one operation
			 */
719 720
			if (az6007_xfer_debug)
				printk(KERN_DEBUG "az6007: I2C W/R addr=0x%x len=%d/%d\n",
721
				       addr, msgs[i].len, msgs[i + 1].len);
722
			req = AZ6007_I2C_RD;
723 724
			index = msgs[i].buf[0];
			value = addr | (1 << 8);
725 726
			length = 6 + msgs[i + 1].len;
			len = msgs[i + 1].len;
727 728
			ret = __az6007_read(d->udev, req, value, index,
					    st->data, length);
729
			if (ret >= len) {
730
				for (j = 0; j < len; j++)
731
					msgs[i + 1].buf[j] = st->data[j + 5];
732 733 734 735 736
			} else
				ret = -EIO;
			i++;
		} else if (!(msgs[i].flags & I2C_M_RD)) {
			/* write bytes */
737 738
			if (az6007_xfer_debug)
				printk(KERN_DEBUG "az6007: I2C W addr=0x%x len=%d\n",
739
				       addr, msgs[i].len);
740
			req = AZ6007_I2C_WR;
741 742 743 744
			index = msgs[i].buf[0];
			value = addr | (1 << 8);
			length = msgs[i].len - 1;
			len = msgs[i].len - 1;
745
			for (j = 0; j < len; j++)
746
				st->data[j] = msgs[i].buf[j + 1];
747 748
			ret =  __az6007_write(d->udev, req, value, index,
					      st->data, length);
749 750
		} else {
			/* read bytes */
751 752
			if (az6007_xfer_debug)
				printk(KERN_DEBUG "az6007: I2C R addr=0x%x len=%d\n",
753
				       addr, msgs[i].len);
754
			req = AZ6007_I2C_RD;
755 756 757 758
			index = msgs[i].buf[0];
			value = addr;
			length = msgs[i].len + 6;
			len = msgs[i].len;
759 760
			ret = __az6007_read(d->udev, req, value, index,
					    st->data, length);
761
			for (j = 0; j < len; j++)
762
				msgs[i].buf[j] = st->data[j + 5];
763
		}
764 765
		if (ret < 0)
			goto err;
766
	}
767
err:
768
	mutex_unlock(&st->mutex);
769 770

	if (ret < 0) {
771
		pr_info("%s ERROR: %i\n", __func__, ret);
772 773 774
		return ret;
	}
	return num;
775 776 777 778 779 780 781 782
}

static u32 az6007_i2c_func(struct i2c_adapter *adapter)
{
	return I2C_FUNC_I2C;
}

static struct i2c_algorithm az6007_i2c_algo = {
783
	.master_xfer = az6007_i2c_xfer,
784 785 786
	.functionality = az6007_i2c_func,
};

787
static int az6007_identify_state(struct dvb_usb_device *d, const char **name)
788
{
789
	int ret;
790 791
	u8 *mac;

792 793
	pr_debug("Identifying az6007 state\n");

794 795 796
	mac = kmalloc(6, GFP_ATOMIC);
	if (!mac)
		return -ENOMEM;
797

798
	/* Try to read the mac address */
799
	ret = __az6007_read(d->udev, AZ6007_READ_DATA, 6, 0, mac, 6);
800
	if (ret == 6)
801
		ret = WARM;
802
	else
803
		ret = COLD;
804

805 806
	kfree(mac);

807 808 809 810
	if (ret == COLD) {
		__az6007_write(d->udev, 0x09, 1, 0, NULL, 0);
		__az6007_write(d->udev, 0x00, 0, 0, NULL, 0);
		__az6007_write(d->udev, 0x00, 0, 0, NULL, 0);
811 812
	}

813 814 815
	pr_debug("Device is on %s state\n",
		 ret == WARM ? "warm" : "cold");
	return ret;
816 817
}

818 819 820 821
static void az6007_usb_disconnect(struct usb_interface *intf)
{
	struct dvb_usb_device *d = usb_get_intfdata(intf);
	az6007_ci_uninit(d);
822
	dvb_usbv2_disconnect(intf);
823 824
}

825
static int az6007_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
826
{
827 828
	pr_debug("Getting az6007 Remote Control properties\n");

829
	rc->allowed_protos = RC_BIT_NEC;
830 831 832 833
	rc->query          = az6007_rc_query;
	rc->interval       = 400;

	return 0;
834 835
}

836 837 838 839
static int az6007_download_firmware(struct dvb_usb_device *d,
	const struct firmware *fw)
{
	pr_debug("Loading az6007 firmware\n");
840

841 842
	return usbv2_cypress_load_firmware(d->udev, fw, CYPRESS_FX2);
}
843

844 845 846 847 848 849 850
/* DVB USB Driver stuff */
static struct dvb_usb_device_properties az6007_props = {
	.driver_name         = KBUILD_MODNAME,
	.owner               = THIS_MODULE,
	.firmware            = AZ6007_FIRMWARE,

	.adapter_nr          = adapter_nr,
851
	.size_of_priv        = sizeof(struct az6007_device_state),
852 853 854 855 856 857 858
	.i2c_algo            = &az6007_i2c_algo,
	.tuner_attach        = az6007_tuner_attach,
	.frontend_attach     = az6007_frontend_attach,
	.streaming_ctrl      = az6007_streaming_ctrl,
	.get_rc_config       = az6007_get_rc_config,
	.read_mac_address    = az6007_read_mac_addr,
	.download_firmware   = az6007_download_firmware,
859
	.identify_state	     = az6007_identify_state,
860 861 862 863
	.power_ctrl          = az6007_power_ctrl,
	.num_adapters        = 1,
	.adapter             = {
		{ .stream = DVB_USB_STREAM_BULK(0x02, 10, 4096), }
864 865
	}
};
866

867 868 869 870 871 872 873 874 875 876 877 878
static struct usb_device_id az6007_usb_table[] = {
	{DVB_USB_DEVICE(USB_VID_AZUREWAVE, USB_PID_AZUREWAVE_6007,
		&az6007_props, "Azurewave 6007", RC_MAP_EMPTY)},
	{DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_H7,
		&az6007_props, "Terratec H7", RC_MAP_NEC_TERRATEC_CINERGY_XS)},
	{DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_H7_2,
		&az6007_props, "Terratec H7", RC_MAP_NEC_TERRATEC_CINERGY_XS)},
	{0},
};

MODULE_DEVICE_TABLE(usb, az6007_usb_table);

879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
static int az6007_suspend(struct usb_interface *intf, pm_message_t msg)
{
	struct dvb_usb_device *d = usb_get_intfdata(intf);

	az6007_ci_uninit(d);
	return dvb_usbv2_suspend(intf, msg);
}

static int az6007_resume(struct usb_interface *intf)
{
	struct dvb_usb_device *d = usb_get_intfdata(intf);
	struct dvb_usb_adapter *adap = &d->adapter[0];

	az6007_ci_init(adap);
	return dvb_usbv2_resume(intf);
}

896 897
/* usb specific object needed to register this driver with the usb subsystem */
static struct usb_driver az6007_usb_driver = {
898
	.name		= KBUILD_MODNAME,
899
	.id_table	= az6007_usb_table,
900 901 902 903
	.probe		= dvb_usbv2_probe,
	.disconnect	= az6007_usb_disconnect,
	.no_dynamic_id	= 1,
	.soft_unbind	= 1,
904 905 906 907 908 909
	/*
	 * FIXME: need to implement reset_resume, likely with
	 * dvb-usb-v2 core support
	 */
	.suspend	= az6007_suspend,
	.resume		= az6007_resume,
910 911
};

912
module_usb_driver(az6007_usb_driver);
913 914

MODULE_AUTHOR("Henry Wang <Henry.wang@AzureWave.com>");
915
MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
916
MODULE_DESCRIPTION("Driver for AzureWave 6007 DVB-C/T USB2.0 and clones");
917
MODULE_VERSION("2.0");
918
MODULE_LICENSE("GPL");
919
MODULE_FIRMWARE(AZ6007_FIRMWARE);