af9035.c 36.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * Afatech AF9035 DVB USB driver
 *
 * Copyright (C) 2009 Antti Palosaari <crope@iki.fi>
 * Copyright (C) 2012 Antti Palosaari <crope@iki.fi>
 *
 *    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; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    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.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "af9035.h"

DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
static u16 af9035_checksum(const u8 *buf, size_t len)
{
	size_t i;
	u16 checksum = 0;

	for (i = 1; i < len; i++) {
		if (i % 2)
			checksum += buf[i] << 8;
		else
			checksum += buf[i];
	}
	checksum = ~checksum;

	return checksum;
}

42
static int af9035_ctrl_msg(struct dvb_usb_device *d, struct usb_req *req)
43 44 45 46 47
{
#define REQ_HDR_LEN 4 /* send header size */
#define ACK_HDR_LEN 3 /* rece header size */
#define CHECKSUM_LEN 2
#define USB_TIMEOUT 2000
48 49
	struct state *state = d_to_priv(d);
	int ret, wlen, rlen;
50
	u16 checksum, tmp_checksum;
51

52 53
	mutex_lock(&d->usb_mutex);

54 55
	/* buffer overflow check */
	if (req->wlen > (BUF_LEN - REQ_HDR_LEN - CHECKSUM_LEN) ||
56 57 58
			req->rlen > (BUF_LEN - ACK_HDR_LEN - CHECKSUM_LEN)) {
		dev_err(&d->udev->dev, "%s: too much data wlen=%d rlen=%d\n",
				__func__, req->wlen, req->rlen);
59
		ret = -EINVAL;
60
		goto exit;
61 62
	}

63 64 65 66 67
	state->buf[0] = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN - 1;
	state->buf[1] = req->mbox;
	state->buf[2] = req->cmd;
	state->buf[3] = state->seq++;
	memcpy(&state->buf[REQ_HDR_LEN], req->wbuf, req->wlen);
68 69 70

	wlen = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN;
	rlen = ACK_HDR_LEN + req->rlen + CHECKSUM_LEN;
71 72

	/* calc and add checksum */
73 74 75
	checksum = af9035_checksum(state->buf, state->buf[0] - 1);
	state->buf[state->buf[0] - 1] = (checksum >> 8);
	state->buf[state->buf[0] - 0] = (checksum & 0xff);
76

77 78 79
	/* no ack for these packets */
	if (req->cmd == CMD_FW_DL)
		rlen = 0;
80

81 82
	ret = dvb_usbv2_generic_rw_locked(d,
			state->buf, wlen, state->buf, rlen);
83
	if (ret)
84
		goto exit;
85 86 87

	/* no ack for those packets */
	if (req->cmd == CMD_FW_DL)
88
		goto exit;
89

90
	/* verify checksum */
91 92
	checksum = af9035_checksum(state->buf, rlen - 2);
	tmp_checksum = (state->buf[rlen - 2] << 8) | state->buf[rlen - 1];
93
	if (tmp_checksum != checksum) {
94 95 96
		dev_err(&d->udev->dev, "%s: command=%02x checksum mismatch " \
				"(%04x != %04x)\n", KBUILD_MODNAME, req->cmd,
				tmp_checksum, checksum);
97
		ret = -EIO;
98
		goto exit;
99
	}
100

101
	/* check status */
102
	if (state->buf[2]) {
103
		/* fw returns status 1 when IR code was not received */
104 105 106 107
		if (req->cmd == CMD_IR_GET || state->buf[2] == 1) {
			ret = 1;
			goto exit;
		}
108

109
		dev_dbg(&d->udev->dev, "%s: command=%02x failed fw error=%d\n",
110
				__func__, req->cmd, state->buf[2]);
111
		ret = -EIO;
112
		goto exit;
113 114 115 116
	}

	/* read request, copy returned data to return buf */
	if (req->rlen)
117
		memcpy(req->rbuf, &state->buf[ACK_HDR_LEN], req->rlen);
118
exit:
119
	mutex_unlock(&d->usb_mutex);
120
	if (ret < 0)
121
		dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
	return ret;
}

/* write multiple registers */
static int af9035_wr_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
{
	u8 wbuf[6 + len];
	u8 mbox = (reg >> 16) & 0xff;
	struct usb_req req = { CMD_MEM_WR, mbox, sizeof(wbuf), wbuf, 0, NULL };

	wbuf[0] = len;
	wbuf[1] = 2;
	wbuf[2] = 0;
	wbuf[3] = 0;
	wbuf[4] = (reg >> 8) & 0xff;
	wbuf[5] = (reg >> 0) & 0xff;
	memcpy(&wbuf[6], val, len);

140
	return af9035_ctrl_msg(d, &req);
141 142 143 144 145 146 147 148 149
}

/* read multiple registers */
static int af9035_rd_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
{
	u8 wbuf[] = { len, 2, 0, 0, (reg >> 8) & 0xff, reg & 0xff };
	u8 mbox = (reg >> 16) & 0xff;
	struct usb_req req = { CMD_MEM_RD, mbox, sizeof(wbuf), wbuf, len, val };

150
	return af9035_ctrl_msg(d, &req);
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
}

/* write single register */
static int af9035_wr_reg(struct dvb_usb_device *d, u32 reg, u8 val)
{
	return af9035_wr_regs(d, reg, &val, 1);
}

/* read single register */
static int af9035_rd_reg(struct dvb_usb_device *d, u32 reg, u8 *val)
{
	return af9035_rd_regs(d, reg, val, 1);
}

/* write single register with mask */
static int af9035_wr_reg_mask(struct dvb_usb_device *d, u32 reg, u8 val,
		u8 mask)
{
	int ret;
	u8 tmp;

	/* no need for read if whole reg is written */
	if (mask != 0xff) {
		ret = af9035_rd_regs(d, reg, &tmp, 1);
		if (ret)
			return ret;

		val &= mask;
		tmp &= ~mask;
		val |= tmp;
	}

	return af9035_wr_regs(d, reg, &val, 1);
}

static int af9035_i2c_master_xfer(struct i2c_adapter *adap,
		struct i2c_msg msg[], int num)
{
	struct dvb_usb_device *d = i2c_get_adapdata(adap);
190
	struct state *state = d_to_priv(d);
191 192 193 194 195
	int ret;

	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
		return -EAGAIN;

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
	/*
	 * I2C sub header is 5 bytes long. Meaning of those bytes are:
	 * 0: data len
	 * 1: I2C addr << 1
	 * 2: reg addr len
	 *    byte 3 and 4 can be used as reg addr
	 * 3: reg addr MSB
	 *    used when reg addr len is set to 2
	 * 4: reg addr LSB
	 *    used when reg addr len is set to 1 or 2
	 *
	 * For the simplify we do not use register addr at all.
	 * NOTE: As a firmware knows tuner type there is very small possibility
	 * there could be some tuner I2C hacks done by firmware and this may
	 * lead problems if firmware expects those bytes are used.
	 */
212 213 214 215 216
	if (num == 2 && !(msg[0].flags & I2C_M_RD) &&
			(msg[1].flags & I2C_M_RD)) {
		if (msg[0].len > 40 || msg[1].len > 40) {
			/* TODO: correct limits > 40 */
			ret = -EOPNOTSUPP;
217 218
		} else if ((msg[0].addr == state->af9033_config[0].i2c_addr) ||
			   (msg[0].addr == state->af9033_config[1].i2c_addr)) {
219
			/* demod access via firmware interface */
220 221
			u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
					msg[0].buf[2];
222 223

			if (msg[0].addr == state->af9033_config[1].i2c_addr)
224
				reg |= 0x100000;
225

226 227 228 229
			ret = af9035_rd_regs(d, reg, &msg[1].buf[0],
					msg[1].len);
		} else {
			/* I2C */
230
			u8 buf[5 + msg[0].len];
231 232
			struct usb_req req = { CMD_I2C_RD, 0, sizeof(buf),
					buf, msg[1].len, msg[1].buf };
233
			req.mbox |= ((msg[0].addr & 0x80)  >>  3);
H
Hans-Frieder Vogt 已提交
234
			buf[0] = msg[1].len;
235
			buf[1] = msg[0].addr << 1;
236 237 238 239
			buf[2] = 0x00; /* reg addr len */
			buf[3] = 0x00; /* reg addr MSB */
			buf[4] = 0x00; /* reg addr LSB */
			memcpy(&buf[5], msg[0].buf, msg[0].len);
240
			ret = af9035_ctrl_msg(d, &req);
241 242 243 244 245
		}
	} else if (num == 1 && !(msg[0].flags & I2C_M_RD)) {
		if (msg[0].len > 40) {
			/* TODO: correct limits > 40 */
			ret = -EOPNOTSUPP;
246 247
		} else if ((msg[0].addr == state->af9033_config[0].i2c_addr) ||
			   (msg[0].addr == state->af9033_config[1].i2c_addr)) {
248
			/* demod access via firmware interface */
249 250
			u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
					msg[0].buf[2];
251 252

			if (msg[0].addr == state->af9033_config[1].i2c_addr)
253
				reg |= 0x100000;
254

255 256 257 258
			ret = af9035_wr_regs(d, reg, &msg[0].buf[3],
					msg[0].len - 3);
		} else {
			/* I2C */
259
			u8 buf[5 + msg[0].len];
260 261
			struct usb_req req = { CMD_I2C_WR, 0, sizeof(buf), buf,
					0, NULL };
262
			req.mbox |= ((msg[0].addr & 0x80)  >>  3);
263
			buf[0] = msg[0].len;
264
			buf[1] = msg[0].addr << 1;
265 266 267 268
			buf[2] = 0x00; /* reg addr len */
			buf[3] = 0x00; /* reg addr MSB */
			buf[4] = 0x00; /* reg addr LSB */
			memcpy(&buf[5], msg[0].buf, msg[0].len);
269
			ret = af9035_ctrl_msg(d, &req);
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
		}
	} else {
		/*
		 * We support only two kind of I2C transactions:
		 * 1) 1 x read + 1 x write
		 * 2) 1 x write
		 */
		ret = -EOPNOTSUPP;
	}

	mutex_unlock(&d->i2c_mutex);

	if (ret < 0)
		return ret;
	else
		return num;
}

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

static struct i2c_algorithm af9035_i2c_algo = {
	.master_xfer = af9035_i2c_master_xfer,
	.functionality = af9035_i2c_functionality,
};

298
static int af9035_identify_state(struct dvb_usb_device *d, const char **name)
299
{
300
	struct state *state = d_to_priv(d);
301 302 303 304 305 306
	int ret;
	u8 wbuf[1] = { 1 };
	u8 rbuf[4];
	struct usb_req req = { CMD_FW_QUERYINFO, 0, sizeof(wbuf), wbuf,
			sizeof(rbuf), rbuf };

307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
	ret = af9035_rd_regs(d, 0x1222, rbuf, 3);
	if (ret < 0)
		goto err;

	state->chip_version = rbuf[0];
	state->chip_type = rbuf[2] << 8 | rbuf[1] << 0;

	ret = af9035_rd_reg(d, 0x384f, &state->prechip_version);
	if (ret < 0)
		goto err;

	dev_info(&d->udev->dev,
			"%s: prechip_version=%02x chip_version=%02x chip_type=%04x\n",
			__func__, state->prechip_version, state->chip_version,
			state->chip_type);

	if (state->chip_type == 0x9135) {
324
		if (state->chip_version == 0x02)
325 326 327
			*name = AF9035_FIRMWARE_IT9135_V2;
		else
			*name = AF9035_FIRMWARE_IT9135_V1;
328
		state->eeprom_addr = EEPROM_BASE_IT9135;
329 330
	} else {
		*name = AF9035_FIRMWARE_AF9035;
331
		state->eeprom_addr = EEPROM_BASE_AF9035;
332 333
	}

334
	ret = af9035_ctrl_msg(d, &req);
335 336 337
	if (ret < 0)
		goto err;

338
	dev_dbg(&d->udev->dev, "%s: reply=%*ph\n", __func__, 4, rbuf);
339
	if (rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])
340
		ret = WARM;
341
	else
342
		ret = COLD;
343

344
	return ret;
345 346

err:
347
	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
348 349 350 351

	return ret;
}

352
static int af9035_download_firmware_old(struct dvb_usb_device *d,
353 354
		const struct firmware *fw)
{
355
	int ret, i, j, len;
356 357 358
	u8 wbuf[1];
	struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
	struct usb_req req_fw_dl = { CMD_FW_DL, 0, 0, wbuf, 0, NULL };
359
	u8 hdr_core;
360
	u16 hdr_addr, hdr_data_len, hdr_checksum;
361
	#define MAX_DATA 58
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
	#define HDR_SIZE 7

	/*
	 * Thanks to Daniel Glöckner <daniel-gl@gmx.net> about that info!
	 *
	 * byte 0: MCS 51 core
	 *  There are two inside the AF9035 (1=Link and 2=OFDM) with separate
	 *  address spaces
	 * byte 1-2: Big endian destination address
	 * byte 3-4: Big endian number of data bytes following the header
	 * byte 5-6: Big endian header checksum, apparently ignored by the chip
	 *  Calculated as ~(h[0]*256+h[1]+h[2]*256+h[3]+h[4]*256)
	 */

	for (i = fw->size; i > HDR_SIZE;) {
		hdr_core = fw->data[fw->size - i + 0];
		hdr_addr = fw->data[fw->size - i + 1] << 8;
		hdr_addr |= fw->data[fw->size - i + 2] << 0;
		hdr_data_len = fw->data[fw->size - i + 3] << 8;
		hdr_data_len |= fw->data[fw->size - i + 4] << 0;
		hdr_checksum = fw->data[fw->size - i + 5] << 8;
		hdr_checksum |= fw->data[fw->size - i + 6] << 0;

385 386 387
		dev_dbg(&d->udev->dev, "%s: core=%d addr=%04x data_len=%d " \
				"checksum=%04x\n", __func__, hdr_core, hdr_addr,
				hdr_data_len, hdr_checksum);
388 389 390

		if (((hdr_core != 1) && (hdr_core != 2)) ||
				(hdr_data_len > i)) {
391
			dev_dbg(&d->udev->dev, "%s: bad firmware\n", __func__);
392 393
			break;
		}
394

395 396
		/* download begin packet */
		req.cmd = CMD_FW_DL_BEGIN;
397
		ret = af9035_ctrl_msg(d, &req);
398 399
		if (ret < 0)
			goto err;
400 401 402 403 404 405 406 407 408

		/* download firmware packet(s) */
		for (j = HDR_SIZE + hdr_data_len; j > 0; j -= MAX_DATA) {
			len = j;
			if (len > MAX_DATA)
				len = MAX_DATA;
			req_fw_dl.wlen = len;
			req_fw_dl.wbuf = (u8 *) &fw->data[fw->size - i +
					HDR_SIZE + hdr_data_len - j];
409
			ret = af9035_ctrl_msg(d, &req_fw_dl);
410
			if (ret < 0)
411 412
				goto err;
		}
413 414 415

		/* download end packet */
		req.cmd = CMD_FW_DL_END;
416
		ret = af9035_ctrl_msg(d, &req);
417 418 419 420 421
		if (ret < 0)
			goto err;

		i -= hdr_data_len + HDR_SIZE;

422 423
		dev_dbg(&d->udev->dev, "%s: data uploaded=%zu\n",
				__func__, fw->size - i);
424 425
	}

426 427 428 429
	/* print warn if firmware is bad, continue and see what happens */
	if (i)
		dev_warn(&d->udev->dev, "%s: bad firmware\n", KBUILD_MODNAME);

430 431 432
	return 0;

err:
433
	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
434 435 436 437

	return ret;
}

438
static int af9035_download_firmware_new(struct dvb_usb_device *d,
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
		const struct firmware *fw)
{
	int ret, i, i_prev;
	struct usb_req req_fw_dl = { CMD_FW_SCATTER_WR, 0, 0, NULL, 0, NULL };
	#define HDR_SIZE 7

	/*
	 * There seems to be following firmware header. Meaning of bytes 0-3
	 * is unknown.
	 *
	 * 0: 3
	 * 1: 0, 1
	 * 2: 0
	 * 3: 1, 2, 3
	 * 4: addr MSB
	 * 5: addr LSB
	 * 6: count of data bytes ?
	 */
	for (i = HDR_SIZE, i_prev = 0; i <= fw->size; i++) {
		if (i == fw->size ||
				(fw->data[i + 0] == 0x03 &&
				(fw->data[i + 1] == 0x00 ||
				fw->data[i + 1] == 0x01) &&
				fw->data[i + 2] == 0x00)) {
			req_fw_dl.wlen = i - i_prev;
			req_fw_dl.wbuf = (u8 *) &fw->data[i_prev];
			i_prev = i;
466
			ret = af9035_ctrl_msg(d, &req_fw_dl);
467 468 469
			if (ret < 0)
				goto err;

470 471
			dev_dbg(&d->udev->dev, "%s: data uploaded=%d\n",
					__func__, i);
472 473 474
		}
	}

475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
	return 0;

err:
	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);

	return ret;
}

static int af9035_download_firmware(struct dvb_usb_device *d,
		const struct firmware *fw)
{
	struct state *state = d_to_priv(d);
	int ret;
	u8 wbuf[1];
	u8 rbuf[4];
	u8 tmp;
	struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
	struct usb_req req_fw_ver = { CMD_FW_QUERYINFO, 0, 1, wbuf, 4, rbuf } ;
	dev_dbg(&d->udev->dev, "%s:\n", __func__);

	/*
	 * In case of dual tuner configuration we need to do some extra
	 * initialization in order to download firmware to slave demod too,
	 * which is done by master demod.
	 * Master feeds also clock and controls power via GPIO.
	 */
	ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_DUAL_MODE, &tmp);
	if (ret < 0)
		goto err;

	if (tmp) {
		/* configure gpioh1, reset & power slave demod */
		ret = af9035_wr_reg_mask(d, 0x00d8b0, 0x01, 0x01);
		if (ret < 0)
			goto err;

		ret = af9035_wr_reg_mask(d, 0x00d8b1, 0x01, 0x01);
		if (ret < 0)
			goto err;

		ret = af9035_wr_reg_mask(d, 0x00d8af, 0x00, 0x01);
		if (ret < 0)
			goto err;

		usleep_range(10000, 50000);

		ret = af9035_wr_reg_mask(d, 0x00d8af, 0x01, 0x01);
		if (ret < 0)
			goto err;

		/* tell the slave I2C address */
		ret = af9035_rd_reg(d,
				state->eeprom_addr + EEPROM_2ND_DEMOD_ADDR,
				&tmp);
		if (ret < 0)
			goto err;

		if (state->chip_type == 0x9135) {
			ret = af9035_wr_reg(d, 0x004bfb, tmp);
			if (ret < 0)
				goto err;
		} else {
			ret = af9035_wr_reg(d, 0x00417f, tmp);
			if (ret < 0)
				goto err;

			/* enable clock out */
			ret = af9035_wr_reg_mask(d, 0x00d81a, 0x01, 0x01);
			if (ret < 0)
				goto err;
		}
	}

548 549
	if (fw->data[0] == 0x01)
		ret = af9035_download_firmware_old(d, fw);
550
	else
551
		ret = af9035_download_firmware_new(d, fw);
552 553 554
	if (ret < 0)
		goto err;

555 556
	/* firmware loaded, request boot */
	req.cmd = CMD_FW_BOOT;
557
	ret = af9035_ctrl_msg(d, &req);
558 559 560 561 562
	if (ret < 0)
		goto err;

	/* ensure firmware starts */
	wbuf[0] = 1;
563
	ret = af9035_ctrl_msg(d, &req_fw_ver);
564 565 566 567
	if (ret < 0)
		goto err;

	if (!(rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])) {
568 569
		dev_err(&d->udev->dev, "%s: firmware did not run\n",
				KBUILD_MODNAME);
570 571 572 573
		ret = -ENODEV;
		goto err;
	}

574 575
	dev_info(&d->udev->dev, "%s: firmware version=%d.%d.%d.%d",
			KBUILD_MODNAME, rbuf[0], rbuf[1], rbuf[2], rbuf[3]);
576 577 578 579

	return 0;

err:
580
	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
581 582 583 584

	return ret;
}

585
static int af9035_read_config(struct dvb_usb_device *d)
586
{
587
	struct state *state = d_to_priv(d);
588
	int ret, i;
589
	u8 tmp;
590
	u16 tmp16, addr;
591

592 593
	/* demod I2C "address" */
	state->af9033_config[0].i2c_addr = 0x38;
594
	state->af9033_config[0].adc_multiplier = AF9033_ADC_MULTIPLIER_2X;
595
	state->af9033_config[1].adc_multiplier = AF9033_ADC_MULTIPLIER_2X;
596 597
	state->af9033_config[0].ts_mode = AF9033_TS_MODE_USB;
	state->af9033_config[1].ts_mode = AF9033_TS_MODE_SERIAL;
598

599 600
	/* eeprom memory mapped location */
	if (state->chip_type == 0x9135) {
601 602
		if (state->chip_version == 0x02) {
			state->af9033_config[0].tuner = AF9033_TUNER_IT9135_60;
603
			state->af9033_config[1].tuner = AF9033_TUNER_IT9135_60;
604 605 606
			tmp16 = 0x00461d;
		} else {
			state->af9033_config[0].tuner = AF9033_TUNER_IT9135_38;
607
			state->af9033_config[1].tuner = AF9033_TUNER_IT9135_38;
608 609 610
			tmp16 = 0x00461b;
		}

611
		/* check if eeprom exists */
612
		ret = af9035_rd_reg(d, tmp16, &tmp);
613 614 615
		if (ret < 0)
			goto err;

616
		if (tmp == 0x00) {
617
			dev_dbg(&d->udev->dev, "%s: no eeprom\n", __func__);
618 619 620 621
			goto skip_eeprom;
		}
	}

622
	/* check if there is dual tuners */
623
	ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_DUAL_MODE, &tmp);
624 625 626
	if (ret < 0)
		goto err;

627
	state->dual_mode = tmp;
628 629
	dev_dbg(&d->udev->dev, "%s: dual mode=%d\n", __func__,
			state->dual_mode);
630

631 632
	if (state->dual_mode) {
		/* read 2nd demodulator I2C address */
633 634 635
		ret = af9035_rd_reg(d,
				state->eeprom_addr + EEPROM_2ND_DEMOD_ADDR,
				&tmp);
636 637
		if (ret < 0)
			goto err;
638

639
		state->af9033_config[1].i2c_addr = tmp;
640 641
		dev_dbg(&d->udev->dev, "%s: 2nd demod I2C addr=%02x\n",
				__func__, tmp);
642 643
	}

644 645
	addr = state->eeprom_addr;

646
	for (i = 0; i < state->dual_mode + 1; i++) {
647
		/* tuner */
648
		ret = af9035_rd_reg(d, addr + EEPROM_1_TUNER_ID, &tmp);
649 650 651
		if (ret < 0)
			goto err;

652 653 654 655 656 657
		if (tmp == 0x00)
			dev_dbg(&d->udev->dev,
					"%s: [%d]tuner not set, using default\n",
					__func__, i);
		else
			state->af9033_config[i].tuner = tmp;
658

659 660
		dev_dbg(&d->udev->dev, "%s: [%d]tuner=%02x\n",
				__func__, i, state->af9033_config[i].tuner);
661 662

		switch (state->af9033_config[i].tuner) {
663
		case AF9033_TUNER_TUA9001:
664
		case AF9033_TUNER_FC0011:
665
		case AF9033_TUNER_MXL5007T:
666
		case AF9033_TUNER_TDA18218:
667
		case AF9033_TUNER_FC2580:
668
		case AF9033_TUNER_FC0012:
669
			state->af9033_config[i].spec_inv = 1;
670
			break;
671 672 673 674 675 676 677
		case AF9033_TUNER_IT9135_38:
		case AF9033_TUNER_IT9135_51:
		case AF9033_TUNER_IT9135_52:
		case AF9033_TUNER_IT9135_60:
		case AF9033_TUNER_IT9135_61:
		case AF9033_TUNER_IT9135_62:
			break;
678
		default:
679 680
			dev_warn(&d->udev->dev,
					"%s: tuner id=%02x not supported, please report!",
681
					KBUILD_MODNAME, tmp);
682
		}
683

684 685
		/* disable dual mode if driver does not support it */
		if (i == 1)
686
			switch (state->af9033_config[i].tuner) {
687
			case AF9033_TUNER_FC0012:
688 689 690 691 692 693
			case AF9033_TUNER_IT9135_38:
			case AF9033_TUNER_IT9135_51:
			case AF9033_TUNER_IT9135_52:
			case AF9033_TUNER_IT9135_60:
			case AF9033_TUNER_IT9135_61:
			case AF9033_TUNER_IT9135_62:
694
			case AF9033_TUNER_MXL5007T:
695
				break;
696 697
			default:
				state->dual_mode = false;
698 699 700
				dev_info(&d->udev->dev,
						"%s: driver does not support 2nd tuner and will disable it",
						KBUILD_MODNAME);
701 702
		}

703
		/* tuner IF frequency */
704
		ret = af9035_rd_reg(d, addr + EEPROM_1_IF_L, &tmp);
705 706 707 708 709
		if (ret < 0)
			goto err;

		tmp16 = tmp;

710
		ret = af9035_rd_reg(d, addr + EEPROM_1_IF_H, &tmp);
711 712 713 714 715
		if (ret < 0)
			goto err;

		tmp16 |= tmp << 8;

716
		dev_dbg(&d->udev->dev, "%s: [%d]IF=%d\n", __func__, i, tmp16);
717

718
		addr += 0x10; /* shift for the 2nd tuner params */
719 720
	}

721
skip_eeprom:
722 723 724 725 726 727 728
	/* get demod clock */
	ret = af9035_rd_reg(d, 0x00d800, &tmp);
	if (ret < 0)
		goto err;

	tmp = (tmp >> 0) & 0x0f;

729 730 731 732 733
	for (i = 0; i < ARRAY_SIZE(state->af9033_config); i++) {
		if (state->chip_type == 0x9135)
			state->af9033_config[i].clock = clock_lut_it9135[tmp];
		else
			state->af9033_config[i].clock = clock_lut_af9035[tmp];
734 735
	}

736 737 738
	return 0;

err:
739
	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
740 741 742 743

	return ret;
}

744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
static int af9035_tua9001_tuner_callback(struct dvb_usb_device *d,
		int cmd, int arg)
{
	int ret;
	u8 val;

	dev_dbg(&d->udev->dev, "%s: cmd=%d arg=%d\n", __func__, cmd, arg);

	/*
	 * CEN     always enabled by hardware wiring
	 * RESETN  GPIOT3
	 * RXEN    GPIOT2
	 */

	switch (cmd) {
	case TUA9001_CMD_RESETN:
		if (arg)
			val = 0x00;
		else
			val = 0x01;

		ret = af9035_wr_reg_mask(d, 0x00d8e7, val, 0x01);
		if (ret < 0)
			goto err;
		break;
	case TUA9001_CMD_RXEN:
		if (arg)
			val = 0x01;
		else
			val = 0x00;

		ret = af9035_wr_reg_mask(d, 0x00d8eb, val, 0x01);
		if (ret < 0)
			goto err;
		break;
	}

	return 0;

err:
	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);

	return ret;
}


790
static int af9035_fc0011_tuner_callback(struct dvb_usb_device *d,
791
		int cmd, int arg)
792
{
793
	int ret;
794 795 796 797

	switch (cmd) {
	case FC0011_FE_CALLBACK_POWER:
		/* Tuner enable */
798 799 800 801 802 803 804 805 806 807 808 809
		ret = af9035_wr_reg_mask(d, 0xd8eb, 1, 1);
		if (ret < 0)
			goto err;

		ret = af9035_wr_reg_mask(d, 0xd8ec, 1, 1);
		if (ret < 0)
			goto err;

		ret = af9035_wr_reg_mask(d, 0xd8ed, 1, 1);
		if (ret < 0)
			goto err;

810
		/* LED */
811 812 813 814 815 816 817 818
		ret = af9035_wr_reg_mask(d, 0xd8d0, 1, 1);
		if (ret < 0)
			goto err;

		ret = af9035_wr_reg_mask(d, 0xd8d1, 1, 1);
		if (ret < 0)
			goto err;

819
		usleep_range(10000, 50000);
820 821
		break;
	case FC0011_FE_CALLBACK_RESET:
822 823 824 825 826 827 828 829 830 831 832 833
		ret = af9035_wr_reg(d, 0xd8e9, 1);
		if (ret < 0)
			goto err;

		ret = af9035_wr_reg(d, 0xd8e8, 1);
		if (ret < 0)
			goto err;

		ret = af9035_wr_reg(d, 0xd8e7, 1);
		if (ret < 0)
			goto err;

834
		usleep_range(10000, 20000);
835 836 837 838 839

		ret = af9035_wr_reg(d, 0xd8e7, 0);
		if (ret < 0)
			goto err;

840
		usleep_range(10000, 20000);
841 842
		break;
	default:
843 844
		ret = -EINVAL;
		goto err;
845 846 847
	}

	return 0;
848 849

err:
850
	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
851 852

	return ret;
853 854 855 856
}

static int af9035_tuner_callback(struct dvb_usb_device *d, int cmd, int arg)
{
857
	struct state *state = d_to_priv(d);
858 859

	switch (state->af9033_config[0].tuner) {
860 861
	case AF9033_TUNER_FC0011:
		return af9035_fc0011_tuner_callback(d, cmd, arg);
862 863
	case AF9033_TUNER_TUA9001:
		return af9035_tua9001_tuner_callback(d, cmd, arg);
864 865 866 867
	default:
		break;
	}

868
	return 0;
869 870 871 872 873 874 875 876
}

static int af9035_frontend_callback(void *adapter_priv, int component,
				    int cmd, int arg)
{
	struct i2c_adapter *adap = adapter_priv;
	struct dvb_usb_device *d = i2c_get_adapdata(adap);

877 878 879
	dev_dbg(&d->udev->dev, "%s: component=%d cmd=%d arg=%d\n",
			__func__, component, cmd, arg);

880 881 882 883 884 885 886
	switch (component) {
	case DVB_FRONTEND_COMPONENT_TUNER:
		return af9035_tuner_callback(d, cmd, arg);
	default:
		break;
	}

887
	return 0;
888 889
}

890 891 892
static int af9035_get_adapter_count(struct dvb_usb_device *d)
{
	struct state *state = d_to_priv(d);
893 894 895 896 897 898

	/* disable 2nd adapter as we don't have PID filters implemented */
	if (d->udev->speed == USB_SPEED_FULL)
		return 1;
	else
		return state->dual_mode + 1;
899 900
}

901 902
static int af9035_frontend_attach(struct dvb_usb_adapter *adap)
{
903 904
	struct state *state = adap_to_priv(adap);
	struct dvb_usb_device *d = adap_to_d(adap);
905
	int ret;
906
	dev_dbg(&d->udev->dev, "%s:\n", __func__);
907

908 909
	if (!state->af9033_config[adap->id].tuner) {
		/* unsupported tuner */
910 911 912 913
		ret = -ENODEV;
		goto err;
	}

914
	/* attach demodulator */
915 916
	adap->fe[0] = dvb_attach(af9033_attach, &state->af9033_config[adap->id],
			&d->i2c_adap);
917
	if (adap->fe[0] == NULL) {
918 919 920
		ret = -ENODEV;
		goto err;
	}
921 922

	/* disable I2C-gate */
923 924
	adap->fe[0]->ops.i2c_gate_ctrl = NULL;
	adap->fe[0]->callback = af9035_frontend_callback;
925 926 927 928

	return 0;

err:
929
	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
930 931 932 933 934 935 936 937

	return ret;
}

static struct tua9001_config af9035_tua9001_config = {
	.i2c_addr = 0x60,
};

938 939 940 941
static const struct fc0011_config af9035_fc0011_config = {
	.i2c_address = 0x60,
};

942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
static struct mxl5007t_config af9035_mxl5007t_config[] = {
	{
		.xtal_freq_hz = MxL_XTAL_24_MHZ,
		.if_freq_hz = MxL_IF_4_57_MHZ,
		.invert_if = 0,
		.loop_thru_enable = 0,
		.clk_out_enable = 0,
		.clk_out_amp = MxL_CLKOUT_AMP_0_94V,
	}, {
		.xtal_freq_hz = MxL_XTAL_24_MHZ,
		.if_freq_hz = MxL_IF_4_57_MHZ,
		.invert_if = 0,
		.loop_thru_enable = 1,
		.clk_out_enable = 1,
		.clk_out_amp = MxL_CLKOUT_AMP_0_94V,
	}
958 959
};

960 961 962 963 964
static struct tda18218_config af9035_tda18218_config = {
	.i2c_address = 0x60,
	.i2c_wr_max = 21,
};

965 966 967 968 969
static const struct fc2580_config af9035_fc2580_config = {
	.i2c_addr = 0x56,
	.clock = 16384000,
};

970 971 972 973
static const struct fc0012_config af9035_fc0012_config[] = {
	{
		.i2c_address = 0x63,
		.xtal_freq = FC_XTAL_36_MHZ,
974
		.dual_master = true,
975 976 977 978 979
		.loop_through = true,
		.clock_out = true,
	}, {
		.i2c_address = 0x63 | 0x80, /* I2C bus select hack */
		.xtal_freq = FC_XTAL_36_MHZ,
980
		.dual_master = true,
981
	}
982 983
};

984 985
static int af9035_tuner_attach(struct dvb_usb_adapter *adap)
{
986 987
	struct state *state = adap_to_priv(adap);
	struct dvb_usb_device *d = adap_to_d(adap);
988 989
	int ret;
	struct dvb_frontend *fe;
990
	struct i2c_msg msg[1];
991
	u8 tuner_addr;
992 993
	dev_dbg(&d->udev->dev, "%s:\n", __func__);

994 995 996 997
	/*
	 * XXX: Hack used in that function: we abuse unused I2C address bit [7]
	 * to carry info about used I2C bus for dual tuner configuration.
	 */
998

999
	switch (state->af9033_config[adap->id].tuner) {
1000 1001 1002 1003 1004
	case AF9033_TUNER_TUA9001:
		/* AF9035 gpiot3 = TUA9001 RESETN
		   AF9035 gpiot2 = TUA9001 RXEN */

		/* configure gpiot2 and gpiot2 as output */
1005
		ret = af9035_wr_reg_mask(d, 0x00d8ec, 0x01, 0x01);
1006 1007 1008
		if (ret < 0)
			goto err;

1009
		ret = af9035_wr_reg_mask(d, 0x00d8ed, 0x01, 0x01);
1010 1011 1012
		if (ret < 0)
			goto err;

1013
		ret = af9035_wr_reg_mask(d, 0x00d8e8, 0x01, 0x01);
1014 1015 1016
		if (ret < 0)
			goto err;

1017
		ret = af9035_wr_reg_mask(d, 0x00d8e9, 0x01, 0x01);
1018 1019 1020 1021
		if (ret < 0)
			goto err;

		/* attach tuner */
1022 1023
		fe = dvb_attach(tua9001_attach, adap->fe[0],
				&d->i2c_adap, &af9035_tua9001_config);
1024
		break;
1025
	case AF9033_TUNER_FC0011:
1026 1027
		fe = dvb_attach(fc0011_attach, adap->fe[0],
				&d->i2c_adap, &af9035_fc0011_config);
1028
		break;
1029
	case AF9033_TUNER_MXL5007T:
1030 1031 1032 1033
		if (adap->id == 0) {
			ret = af9035_wr_reg(d, 0x00d8e0, 1);
			if (ret < 0)
				goto err;
1034

1035 1036 1037
			ret = af9035_wr_reg(d, 0x00d8e1, 1);
			if (ret < 0)
				goto err;
1038

1039 1040 1041
			ret = af9035_wr_reg(d, 0x00d8df, 0);
			if (ret < 0)
				goto err;
1042

1043
			msleep(30);
1044

1045 1046 1047
			ret = af9035_wr_reg(d, 0x00d8df, 1);
			if (ret < 0)
				goto err;
1048

1049
			msleep(300);
1050

1051 1052 1053
			ret = af9035_wr_reg(d, 0x00d8c0, 1);
			if (ret < 0)
				goto err;
1054

1055 1056 1057
			ret = af9035_wr_reg(d, 0x00d8c1, 1);
			if (ret < 0)
				goto err;
1058

1059 1060 1061
			ret = af9035_wr_reg(d, 0x00d8bf, 0);
			if (ret < 0)
				goto err;
1062

1063 1064 1065
			ret = af9035_wr_reg(d, 0x00d8b4, 1);
			if (ret < 0)
				goto err;
1066

1067 1068 1069
			ret = af9035_wr_reg(d, 0x00d8b5, 1);
			if (ret < 0)
				goto err;
1070

1071 1072 1073
			ret = af9035_wr_reg(d, 0x00d8b3, 1);
			if (ret < 0)
				goto err;
1074 1075 1076 1077

			tuner_addr = 0x60;
		} else {
			tuner_addr = 0x60 | 0x80; /* I2C bus hack */
1078
		}
1079 1080

		/* attach tuner */
1081 1082
		fe = dvb_attach(mxl5007t_attach, adap->fe[0], &d->i2c_adap,
				tuner_addr, &af9035_mxl5007t_config[adap->id]);
1083
		break;
1084 1085
	case AF9033_TUNER_TDA18218:
		/* attach tuner */
1086 1087
		fe = dvb_attach(tda18218_attach, adap->fe[0],
				&d->i2c_adap, &af9035_tda18218_config);
1088
		break;
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
	case AF9033_TUNER_FC2580:
		/* Tuner enable using gpiot2_o, gpiot2_en and gpiot2_on  */
		ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
		if (ret < 0)
			goto err;

		ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
		if (ret < 0)
			goto err;

		ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
		if (ret < 0)
			goto err;

		usleep_range(10000, 50000);
		/* attach tuner */
		fe = dvb_attach(fc2580_attach, adap->fe[0],
				&d->i2c_adap, &af9035_fc2580_config);
		break;
1108 1109 1110 1111 1112 1113 1114
	case AF9033_TUNER_FC0012:
		/*
		 * AF9035 gpiot2 = FC0012 enable
		 * XXX: there seems to be something on gpioh8 too, but on my
		 * my test I didn't find any difference.
		 */

1115 1116 1117 1118 1119
		if (adap->id == 0) {
			/* configure gpiot2 as output and high */
			ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
			if (ret < 0)
				goto err;
1120

1121 1122 1123
			ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
			if (ret < 0)
				goto err;
1124

1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
			ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
			if (ret < 0)
				goto err;
		} else {
			/*
			 * FIXME: That belongs for the FC0012 driver.
			 * Write 02 to FC0012 master tuner register 0d directly
			 * in order to make slave tuner working.
			 */
			msg[0].addr = 0x63;
			msg[0].flags = 0;
			msg[0].len = 2;
			msg[0].buf = "\x0d\x02";
			ret = i2c_transfer(&d->i2c_adap, msg, 1);
			if (ret < 0)
				goto err;
		}
1142 1143 1144

		usleep_range(10000, 50000);

1145
		fe = dvb_attach(fc0012_attach, adap->fe[0], &d->i2c_adap,
1146
				&af9035_fc0012_config[adap->id]);
1147
		break;
1148
	case AF9033_TUNER_IT9135_38:
1149 1150 1151 1152 1153
	case AF9033_TUNER_IT9135_51:
	case AF9033_TUNER_IT9135_52:
	case AF9033_TUNER_IT9135_60:
	case AF9033_TUNER_IT9135_61:
	case AF9033_TUNER_IT9135_62:
1154 1155 1156
		/* attach tuner */
		fe = dvb_attach(it913x_attach, adap->fe[0], &d->i2c_adap,
				state->af9033_config[adap->id].i2c_addr,
1157
				state->af9033_config[0].tuner);
1158
		break;
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
	default:
		fe = NULL;
	}

	if (fe == NULL) {
		ret = -ENODEV;
		goto err;
	}

	return 0;

err:
1171
	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1172 1173 1174 1175

	return ret;
}

1176 1177 1178 1179
static int af9035_init(struct dvb_usb_device *d)
{
	struct state *state = d_to_priv(d);
	int ret, i;
1180 1181
	u16 frame_size = (d->udev->speed == USB_SPEED_FULL ? 5 : 87) * 188 / 4;
	u8 packet_size = (d->udev->speed == USB_SPEED_FULL ? 64 : 512) / 4;
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
	struct reg_val_mask tab[] = {
		{ 0x80f99d, 0x01, 0x01 },
		{ 0x80f9a4, 0x01, 0x01 },
		{ 0x00dd11, 0x00, 0x20 },
		{ 0x00dd11, 0x00, 0x40 },
		{ 0x00dd13, 0x00, 0x20 },
		{ 0x00dd13, 0x00, 0x40 },
		{ 0x00dd11, 0x20, 0x20 },
		{ 0x00dd88, (frame_size >> 0) & 0xff, 0xff},
		{ 0x00dd89, (frame_size >> 8) & 0xff, 0xff},
		{ 0x00dd0c, packet_size, 0xff},
		{ 0x00dd11, state->dual_mode << 6, 0x40 },
		{ 0x00dd8a, (frame_size >> 0) & 0xff, 0xff},
		{ 0x00dd8b, (frame_size >> 8) & 0xff, 0xff},
		{ 0x00dd0d, packet_size, 0xff },
1197 1198
		{ 0x80f9a3, state->dual_mode, 0x01 },
		{ 0x80f9cd, state->dual_mode, 0x01 },
1199 1200 1201
		{ 0x80f99d, 0x00, 0x01 },
		{ 0x80f9a4, 0x00, 0x01 },
	};
1202

1203 1204 1205
	dev_dbg(&d->udev->dev, "%s: USB speed=%d frame_size=%04x " \
			"packet_size=%02x\n", __func__,
			d->udev->speed, frame_size, packet_size);
1206

1207 1208 1209 1210 1211 1212 1213
	/* init endpoints */
	for (i = 0; i < ARRAY_SIZE(tab); i++) {
		ret = af9035_wr_reg_mask(d, tab[i].reg, tab[i].val,
				tab[i].mask);
		if (ret < 0)
			goto err;
	}
1214

1215
	return 0;
1216

1217
err:
1218
	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1219

1220 1221
	return ret;
}
1222

1223
#if IS_ENABLED(CONFIG_RC_CORE)
1224 1225 1226
static int af9035_rc_query(struct dvb_usb_device *d)
{
	int ret;
1227 1228 1229
	u32 key;
	u8 buf[4];
	struct usb_req req = { CMD_IR_GET, 0, 0, NULL, 4, buf };
1230

1231
	ret = af9035_ctrl_msg(d, &req);
1232 1233 1234
	if (ret == 1)
		return 0;
	else if (ret < 0)
1235
		goto err;
1236

1237 1238 1239 1240
	if ((buf[2] + buf[3]) == 0xff) {
		if ((buf[0] + buf[1]) == 0xff) {
			/* NEC standard 16bit */
			key = buf[0] << 8 | buf[2];
1241
		} else {
1242 1243
			/* NEC extended 24bit */
			key = buf[0] << 16 | buf[1] << 8 | buf[2];
1244
		}
1245
	} else {
1246 1247
		/* NEC full code 32bit */
		key = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1248 1249
	}

1250 1251
	dev_dbg(&d->udev->dev, "%s: %*ph\n", __func__, 4, buf);

1252
	rc_keydown(d->rc_dev, key, 0);
1253

1254
	return 0;
1255 1256 1257 1258 1259

err:
	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);

	return ret;
1260
}
1261

1262 1263
static int af9035_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
{
1264
	struct state *state = d_to_priv(d);
1265 1266
	int ret;
	u8 tmp;
1267

1268
	ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_IR_MODE, &tmp);
1269 1270 1271
	if (ret < 0)
		goto err;

1272
	dev_dbg(&d->udev->dev, "%s: ir_mode=%02x\n", __func__, tmp);
1273 1274 1275

	/* don't activate rc if in HID mode or if not available */
	if (tmp == 5) {
1276
		ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_IR_TYPE,
1277
				&tmp);
1278 1279
		if (ret < 0)
			goto err;
1280

1281
		dev_dbg(&d->udev->dev, "%s: ir_type=%02x\n", __func__, tmp);
1282 1283 1284 1285

		switch (tmp) {
		case 0: /* NEC */
		default:
1286
			rc->allowed_protos = RC_BIT_NEC;
1287 1288
			break;
		case 1: /* RC6 */
1289
			rc->allowed_protos = RC_BIT_RC6_MCE;
1290 1291 1292 1293 1294
			break;
		}

		rc->query = af9035_rc_query;
		rc->interval = 500;
1295 1296 1297 1298

		/* load empty to enable rc */
		if (!rc->map_name)
			rc->map_name = RC_MAP_EMPTY;
1299 1300 1301 1302 1303
	}

	return 0;

err:
1304
	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1305 1306 1307

	return ret;
}
1308 1309 1310
#else
	#define af9035_get_rc_config NULL
#endif
1311

1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
static int af9035_get_stream_config(struct dvb_frontend *fe, u8 *ts_type,
		struct usb_data_stream_properties *stream)
{
	struct dvb_usb_device *d = fe_to_d(fe);
	dev_dbg(&d->udev->dev, "%s: adap=%d\n", __func__, fe_to_adap(fe)->id);

	if (d->udev->speed == USB_SPEED_FULL)
		stream->u.bulk.buffersize = 5 * 188;

	return 0;
}

/*
 * FIXME: PID filter is property of demodulator and should be moved to the
 * correct driver. Also we support only adapter #0 PID filter and will
 * disable adapter #1 if USB1.1 is used.
 */
static int af9035_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
{
	struct dvb_usb_device *d = adap_to_d(adap);
	int ret;

	dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff);

	ret = af9035_wr_reg_mask(d, 0x80f993, onoff, 0x01);
	if (ret < 0)
		goto err;

	return 0;

err:
	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);

	return ret;
}

static int af9035_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
		int onoff)
{
	struct dvb_usb_device *d = adap_to_d(adap);
	int ret;
	u8 wbuf[2] = {(pid >> 0) & 0xff, (pid >> 8) & 0xff};

	dev_dbg(&d->udev->dev, "%s: index=%d pid=%04x onoff=%d\n",
			__func__, index, pid, onoff);

	ret = af9035_wr_regs(d, 0x80f996, wbuf, 2);
	if (ret < 0)
		goto err;

	ret = af9035_wr_reg(d, 0x80f994, onoff);
	if (ret < 0)
		goto err;

	ret = af9035_wr_reg(d, 0x80f995, index);
	if (ret < 0)
		goto err;

	return 0;

err:
	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);

	return ret;
}

1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414
static int af9035_probe(struct usb_interface *intf,
		const struct usb_device_id *id)
{
	struct usb_device *udev = interface_to_usbdev(intf);
	char manufacturer[sizeof("Afatech")];

	memset(manufacturer, 0, sizeof(manufacturer));
	usb_string(udev, udev->descriptor.iManufacturer,
			manufacturer, sizeof(manufacturer));
	/*
	 * There is two devices having same ID but different chipset. One uses
	 * AF9015 and the other IT9135 chipset. Only difference seen on lsusb
	 * is iManufacturer string.
	 *
	 * idVendor           0x0ccd TerraTec Electronic GmbH
	 * idProduct          0x0099
	 * bcdDevice            2.00
	 * iManufacturer           1 Afatech
	 * iProduct                2 DVB-T 2
	 *
	 * idVendor           0x0ccd TerraTec Electronic GmbH
	 * idProduct          0x0099
	 * bcdDevice            2.00
	 * iManufacturer           1 ITE Technologies, Inc.
	 * iProduct                2 DVB-T TV Stick
	 */
	if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VID_TERRATEC) &&
			(le16_to_cpu(udev->descriptor.idProduct) == 0x0099)) {
		if (!strcmp("Afatech", manufacturer)) {
			dev_dbg(&udev->dev, "%s: rejecting device\n", __func__);
			return -ENODEV;
		}
	}

	return dvb_usbv2_probe(intf, id);
}

1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
/* interface 0 is used by DVB-T receiver and
   interface 1 is for remote controller (HID) */
static const struct dvb_usb_device_properties af9035_props = {
	.driver_name = KBUILD_MODNAME,
	.owner = THIS_MODULE,
	.adapter_nr = adapter_nr,
	.size_of_priv = sizeof(struct state),

	.generic_bulk_ctrl_endpoint = 0x02,
	.generic_bulk_ctrl_endpoint_response = 0x81,

	.identify_state = af9035_identify_state,
	.download_firmware = af9035_download_firmware,

	.i2c_algo = &af9035_i2c_algo,
	.read_config = af9035_read_config,
	.frontend_attach = af9035_frontend_attach,
	.tuner_attach = af9035_tuner_attach,
	.init = af9035_init,
	.get_rc_config = af9035_get_rc_config,
1435
	.get_stream_config = af9035_get_stream_config,
1436

1437
	.get_adapter_count = af9035_get_adapter_count,
1438 1439
	.adapter = {
		{
1440 1441 1442 1443 1444 1445 1446
			.caps = DVB_USB_ADAP_HAS_PID_FILTER |
				DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,

			.pid_filter_count = 32,
			.pid_filter_ctrl = af9035_pid_filter_ctrl,
			.pid_filter = af9035_pid_filter,

1447 1448 1449 1450 1451 1452 1453 1454
			.stream = DVB_USB_STREAM_BULK(0x84, 6, 87 * 188),
		}, {
			.stream = DVB_USB_STREAM_BULK(0x85, 6, 87 * 188),
		},
	},
};

static const struct usb_device_id af9035_id_table[] = {
1455
	/* AF9035 devices */
1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477
	{ DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_9035,
		&af9035_props, "Afatech AF9035 reference design", NULL) },
	{ DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1000,
		&af9035_props, "Afatech AF9035 reference design", NULL) },
	{ DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1001,
		&af9035_props, "Afatech AF9035 reference design", NULL) },
	{ DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1002,
		&af9035_props, "Afatech AF9035 reference design", NULL) },
	{ DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1003,
		&af9035_props, "Afatech AF9035 reference design", NULL) },
	{ DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK,
		&af9035_props, "TerraTec Cinergy T Stick", NULL) },
	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835,
		&af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_B835,
		&af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_1867,
		&af9035_props, "AVerMedia HD Volar (A867)", NULL) },
	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A867,
		&af9035_props, "AVerMedia HD Volar (A867)", NULL) },
	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_TWINSTAR,
		&af9035_props, "AVerMedia Twinstar (A825)", NULL) },
1478 1479
	{ DVB_USB_DEVICE(USB_VID_ASUS, USB_PID_ASUS_U3100MINI_PLUS,
		&af9035_props, "Asus U3100Mini Plus", NULL) },
1480 1481
        { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00aa,
		&af9035_props, "TerraTec Cinergy T Stick (rev. 2)", NULL) },
1482 1483 1484 1485 1486 1487 1488
	/* IT9135 devices */
#if 0
	{ DVB_USB_DEVICE(0x048d, 0x9135,
		&af9035_props, "IT9135 reference design", NULL) },
	{ DVB_USB_DEVICE(0x048d, 0x9006,
		&af9035_props, "IT9135 reference design", NULL) },
#endif
1489 1490 1491
	/* XXX: that same ID [0ccd:0099] is used by af9015 driver too */
	{ DVB_USB_DEVICE(USB_VID_TERRATEC, 0x0099,
		&af9035_props, "TerraTec Cinergy T Stick Dual RC (rev. 2)", NULL) },
1492 1493 1494 1495
	{ }
};
MODULE_DEVICE_TABLE(usb, af9035_id_table);

1496
static struct usb_driver af9035_usb_driver = {
1497 1498
	.name = KBUILD_MODNAME,
	.id_table = af9035_id_table,
1499
	.probe = af9035_probe,
1500 1501 1502
	.disconnect = dvb_usbv2_disconnect,
	.suspend = dvb_usbv2_suspend,
	.resume = dvb_usbv2_resume,
1503
	.reset_resume = dvb_usbv2_reset_resume,
1504 1505
	.no_dynamic_id = 1,
	.soft_unbind = 1,
1506 1507
};

1508
module_usb_driver(af9035_usb_driver);
1509 1510 1511 1512

MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
MODULE_DESCRIPTION("Afatech AF9035 driver");
MODULE_LICENSE("GPL");
1513
MODULE_FIRMWARE(AF9035_FIRMWARE_AF9035);
1514 1515
MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135_V1);
MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135_V2);