af9015.c 43.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
/*
 * DVB USB Linux driver for Afatech AF9015 DVB-T USB2.0 receiver
 *
 * Copyright (C) 2007 Antti Palosaari <crope@iki.fi>
 *
 * Thanks to Afatech who kindly provided information.
 *
 *    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.
 *
 */

#include "af9015.h"

22
static int dvb_usb_af9015_remote;
23 24 25 26
module_param_named(remote, dvb_usb_af9015_remote, int, 0644);
MODULE_PARM_DESC(remote, "select remote");
DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);

27
static int af9015_ctrl_msg(struct dvb_usb_device *d, struct req_t *req)
28
{
29 30
#define REQ_HDR_LEN 8 /* send header size */
#define ACK_HDR_LEN 2 /* rece header size */
31
	struct af9015_state *state = d_to_priv(d);
A
Antti Palosaari 已提交
32
	struct usb_interface *intf = d->intf;
33
	int ret, wlen, rlen;
34 35
	u8 write = 1;

36 37 38 39
	mutex_lock(&d->usb_mutex);

	state->buf[0] = req->cmd;
	state->buf[1] = state->seq++;
40
	state->buf[2] = req->i2c_addr << 1;
41 42 43 44 45
	state->buf[3] = req->addr >> 8;
	state->buf[4] = req->addr & 0xff;
	state->buf[5] = req->mbox;
	state->buf[6] = req->addr_len;
	state->buf[7] = req->data_len;
46 47 48 49 50 51 52 53 54

	switch (req->cmd) {
	case GET_CONFIG:
	case READ_MEMORY:
	case RECONNECT_USB:
		write = 0;
		break;
	case READ_I2C:
		write = 0;
55
		state->buf[2] |= 0x01; /* set I2C direction */
56
		/* fall through */
57
	case WRITE_I2C:
58
		state->buf[0] = READ_WRITE_I2C;
59 60 61
		break;
	case WRITE_MEMORY:
		if (((req->addr & 0xff00) == 0xff00) ||
62
		    ((req->addr & 0xff00) == 0xae00))
63
			state->buf[0] = WRITE_VIRTUAL_MEMORY;
64 65 66
	case WRITE_VIRTUAL_MEMORY:
	case COPY_FIRMWARE:
	case DOWNLOAD_FIRMWARE:
67
	case BOOT:
68 69
		break;
	default:
A
Antti Palosaari 已提交
70
		dev_err(&intf->dev, "unknown cmd %d\n", req->cmd);
71
		ret = -EIO;
72
		goto error;
73 74
	}

75
	/* Buffer overflow check */
76
	if ((write && (req->data_len > BUF_LEN - REQ_HDR_LEN)) ||
77
	    (!write && (req->data_len > BUF_LEN - ACK_HDR_LEN))) {
A
Antti Palosaari 已提交
78 79
		dev_err(&intf->dev, "too much data, cmd %u, len %u\n",
			req->cmd, req->data_len);
80
		ret = -EINVAL;
81
		goto error;
82 83
	}

84 85 86 87
	/*
	 * Write receives seq + status = 2 bytes
	 * Read receives seq + status + data = 2 + N bytes
	 */
88 89
	wlen = REQ_HDR_LEN;
	rlen = ACK_HDR_LEN;
90
	if (write) {
91
		wlen += req->data_len;
92
		memcpy(&state->buf[REQ_HDR_LEN], req->data, req->data_len);
93 94
	} else {
		rlen += req->data_len;
95
	}
96

97
	/* no ack for these packets */
98
	if (req->cmd == DOWNLOAD_FIRMWARE || req->cmd == RECONNECT_USB)
99
		rlen = 0;
100

101 102
	ret = dvb_usbv2_generic_rw_locked(d, state->buf, wlen,
					  state->buf, rlen);
103 104
	if (ret)
		goto error;
105 106

	/* check status */
107
	if (rlen && state->buf[1]) {
A
Antti Palosaari 已提交
108
		dev_err(&intf->dev, "cmd failed %u\n", state->buf[1]);
109
		ret = -EIO;
110
		goto error;
111 112 113 114
	}

	/* read request, copy returned data to return buf */
	if (!write)
115
		memcpy(req->data, &state->buf[ACK_HDR_LEN], req->data_len);
116
error:
117 118
	mutex_unlock(&d->usb_mutex);

119 120 121 122
	return ret;
}

static int af9015_write_reg_i2c(struct dvb_usb_device *d, u8 addr, u16 reg,
123
				u8 val)
124
{
125
	struct af9015_state *state = d_to_priv(d);
126 127
	struct req_t req = {WRITE_I2C, addr, reg, 1, 1, 1, &val};

128 129
	if (addr == state->af9013_i2c_addr[0] ||
	    addr == state->af9013_i2c_addr[1])
130 131 132 133 134 135
		req.addr_len = 3;

	return af9015_ctrl_msg(d, &req);
}

static int af9015_read_reg_i2c(struct dvb_usb_device *d, u8 addr, u16 reg,
136
			       u8 *val)
137
{
138
	struct af9015_state *state = d_to_priv(d);
139 140
	struct req_t req = {READ_I2C, addr, reg, 0, 1, 1, val};

141 142
	if (addr == state->af9013_i2c_addr[0] ||
	    addr == state->af9013_i2c_addr[1])
143 144 145 146 147 148
		req.addr_len = 3;

	return af9015_ctrl_msg(d, &req);
}

static int af9015_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
149
			   int num)
150 151
{
	struct dvb_usb_device *d = i2c_get_adapdata(adap);
152
	struct af9015_state *state = d_to_priv(d);
A
Antti Palosaari 已提交
153
	struct usb_interface *intf = d->intf;
154
	int ret;
155
	u16 addr;
156
	u8 mbox, addr_len;
157 158
	struct req_t req;

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
	/*
	 * I2C multiplexing:
	 * There could be two tuners, both using same I2C address. Demodulator
	 * I2C-gate is only possibility to select correct tuner.
	 *
	 * ...........................................
	 * . AF9015 integrates AF9013 demodulator    .
	 * . ____________               ____________ .             ____________
	 * .|   USB IF   |             |   demod    |.            |   tuner    |
	 * .|------------|             |------------|.            |------------|
	 * .|   AF9015   |             |   AF9013   |.            |   MXL5003  |
	 * .|            |--+--I2C-----|-----/ -----|.----I2C-----|            |
	 * .|            |  |          | addr 0x1c  |.            |  addr 0x63 |
	 * .|____________|  |          |____________|.            |____________|
	 * .................|.........................
	 *                  |           ____________               ____________
	 *                  |          |   demod    |             |   tuner    |
	 *                  |          |------------|             |------------|
	 *                  |          |   AF9013   |             |   MXL5003  |
	 *                  +--I2C-----|-----/ -----|-----I2C-----|            |
	 *                             | addr 0x1d  |             |  addr 0x63 |
	 *                             |____________|             |____________|
	 */
182

183 184 185 186 187 188 189 190 191
	if (msg[0].len == 0 || msg[0].flags & I2C_M_RD) {
		addr = 0x0000;
		mbox = 0;
		addr_len = 0;
	} else if (msg[0].len == 1) {
		addr = msg[0].buf[0];
		mbox = 0;
		addr_len = 1;
	} else if (msg[0].len == 2) {
192
		addr = msg[0].buf[0] << 8 | msg[0].buf[1] << 0;
193 194 195
		mbox = 0;
		addr_len = 2;
	} else {
196
		addr = msg[0].buf[0] << 8 | msg[0].buf[1] << 0;
197 198 199 200 201 202 203 204 205 206
		mbox = msg[0].buf[2];
		addr_len = 3;
	}

	if (num == 1 && !(msg[0].flags & I2C_M_RD)) {
		/* i2c write */
		if (msg[0].len > 21) {
			ret = -EOPNOTSUPP;
			goto err;
		}
207
		if (msg[0].addr == state->af9013_i2c_addr[0])
208 209 210 211 212 213 214
			req.cmd = WRITE_MEMORY;
		else
			req.cmd = WRITE_I2C;
		req.i2c_addr = msg[0].addr;
		req.addr = addr;
		req.mbox = mbox;
		req.addr_len = addr_len;
215
		req.data_len = msg[0].len - addr_len;
216 217 218 219 220 221 222 223 224
		req.data = &msg[0].buf[addr_len];
		ret = af9015_ctrl_msg(d, &req);
	} else if (num == 2 && !(msg[0].flags & I2C_M_RD) &&
		   (msg[1].flags & I2C_M_RD)) {
		/* i2c write + read */
		if (msg[0].len > 3 || msg[1].len > 61) {
			ret = -EOPNOTSUPP;
			goto err;
		}
225
		if (msg[0].addr == state->af9013_i2c_addr[0])
226 227
			req.cmd = READ_MEMORY;
		else
228
			req.cmd = READ_I2C;
229 230 231 232 233 234 235 236 237 238 239 240
		req.i2c_addr = msg[0].addr;
		req.addr = addr;
		req.mbox = mbox;
		req.addr_len = addr_len;
		req.data_len = msg[1].len;
		req.data = &msg[1].buf[0];
		ret = af9015_ctrl_msg(d, &req);
	} else if (num == 1 && (msg[0].flags & I2C_M_RD)) {
		/* i2c read */
		if (msg[0].len > 61) {
			ret = -EOPNOTSUPP;
			goto err;
241
		}
242
		if (msg[0].addr == state->af9013_i2c_addr[0]) {
243 244 245 246 247 248 249 250 251 252 253 254 255
			ret = -EINVAL;
			goto err;
		}
		req.cmd = READ_I2C;
		req.i2c_addr = msg[0].addr;
		req.addr = addr;
		req.mbox = mbox;
		req.addr_len = addr_len;
		req.data_len = msg[0].len;
		req.data = &msg[0].buf[0];
		ret = af9015_ctrl_msg(d, &req);
	} else {
		ret = -EOPNOTSUPP;
A
Antti Palosaari 已提交
256
		dev_dbg(&intf->dev, "unknown msg, num %u\n", num);
257
	}
258 259
	if (ret)
		goto err;
260

261 262
	return num;
err:
A
Antti Palosaari 已提交
263
	dev_dbg(&intf->dev, "failed %d\n", ret);
264 265 266 267 268 269 270 271 272 273 274 275 276
	return ret;
}

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

static struct i2c_algorithm af9015_i2c_algo = {
	.master_xfer = af9015_i2c_xfer,
	.functionality = af9015_i2c_func,
};

277
static int af9015_identify_state(struct dvb_usb_device *d, const char **name)
278
{
A
Antti Palosaari 已提交
279
	struct usb_interface *intf = d->intf;
280
	int ret;
281 282
	u8 reply;
	struct req_t req = {GET_CONFIG, 0, 0, 0, 0, 1, &reply};
283

284
	ret = af9015_ctrl_msg(d, &req);
285 286 287
	if (ret)
		return ret;

A
Antti Palosaari 已提交
288
	dev_dbg(&intf->dev, "reply %02x\n", reply);
289

290 291 292 293
	if (reply == 0x02)
		ret = WARM;
	else
		ret = COLD;
294

295
	return ret;
296 297
}

298
static int af9015_download_firmware(struct dvb_usb_device *d,
299
				    const struct firmware *firmware)
300
{
301
	struct af9015_state *state = d_to_priv(d);
A
Antti Palosaari 已提交
302
	struct usb_interface *intf = d->intf;
303
	int ret, i, rem;
304
	struct req_t req = {DOWNLOAD_FIRMWARE, 0, 0, 0, 0, 0, NULL};
305
	u16 checksum;
A
Antti Palosaari 已提交
306 307

	dev_dbg(&intf->dev, "\n");
308

309 310 311
	/* Calc checksum, we need it when copy firmware to slave demod */
	for (i = 0, checksum = 0; i < firmware->size; i++)
		checksum += firmware->data[i];
312

313
	state->firmware_size = firmware->size;
314
	state->firmware_checksum = checksum;
315

316 317 318
	#define LEN_MAX (BUF_LEN - REQ_HDR_LEN) /* Max payload size */
	for (rem = firmware->size; rem > 0; rem -= LEN_MAX) {
		req.data_len = min(LEN_MAX, rem);
319
		req.data = (u8 *)&firmware->data[firmware->size - rem];
320
		req.addr = 0x5100 + firmware->size - rem;
321 322
		ret = af9015_ctrl_msg(d, &req);
		if (ret) {
A
Antti Palosaari 已提交
323 324
			dev_err(&intf->dev, "firmware download failed %d\n",
				ret);
325
			goto err;
326
		}
327 328
	}

329 330 331 332
	req.cmd = BOOT;
	req.data_len = 0;
	ret = af9015_ctrl_msg(d, &req);
	if (ret) {
A
Antti Palosaari 已提交
333
		dev_err(&intf->dev, "firmware boot failed %d\n", ret);
334
		goto err;
335
	}
336

337 338 339
	return 0;
err:
	dev_dbg(&intf->dev, "failed %d\n", ret);
340 341 342
	return ret;
}

343
#define AF9015_EEPROM_SIZE 256
344 345
/* 2^31 + 2^29 - 2^25 + 2^22 - 2^19 - 2^16 + 1 */
#define GOLDEN_RATIO_PRIME_32 0x9e370001UL
346

347 348 349
/* hash (and dump) eeprom */
static int af9015_eeprom_hash(struct dvb_usb_device *d)
{
350
	struct af9015_state *state = d_to_priv(d);
A
Antti Palosaari 已提交
351
	struct usb_interface *intf = d->intf;
352 353 354 355 356 357 358 359
	int ret, i;
	u8 buf[AF9015_EEPROM_SIZE];
	struct req_t req = {READ_I2C, AF9015_I2C_EEPROM, 0, 0, 1, 1, NULL};

	/* read eeprom */
	for (i = 0; i < AF9015_EEPROM_SIZE; i++) {
		req.addr = i;
		req.data = &buf[i];
360
		ret = af9015_ctrl_msg(d, &req);
361 362
		if (ret < 0)
			goto err;
363
	}
364

365 366
	/* calculate checksum */
	for (i = 0; i < AF9015_EEPROM_SIZE / sizeof(u32); i++) {
367
		state->eeprom_sum *= GOLDEN_RATIO_PRIME_32;
368
		state->eeprom_sum += le32_to_cpu(((__le32 *)buf)[i]);
369 370
	}

371
	for (i = 0; i < AF9015_EEPROM_SIZE; i += 16)
A
Antti Palosaari 已提交
372
		dev_dbg(&intf->dev, "%*ph\n", 16, buf + i);
373

A
Antti Palosaari 已提交
374
	dev_dbg(&intf->dev, "eeprom sum %.8x\n", state->eeprom_sum);
375 376
	return 0;
err:
A
Antti Palosaari 已提交
377
	dev_dbg(&intf->dev, "failed %d\n", ret);
378 379 380
	return ret;
}

381
static int af9015_read_config(struct dvb_usb_device *d)
382
{
383
	struct af9015_state *state = d_to_priv(d);
A
Antti Palosaari 已提交
384
	struct usb_interface *intf = d->intf;
385
	int ret;
386 387
	u8 val, i, offset = 0;
	struct req_t req = {READ_I2C, AF9015_I2C_EEPROM, 0, 0, 1, 1, &val};
388

A
Antti Palosaari 已提交
389
	dev_dbg(&intf->dev, "\n");
390

391 392 393 394 395 396 397 398 399 400
	/* IR remote controller */
	req.addr = AF9015_EEPROM_IR_MODE;
	/* first message will timeout often due to possible hw bug */
	for (i = 0; i < 4; i++) {
		ret = af9015_ctrl_msg(d, &req);
		if (!ret)
			break;
	}
	if (ret)
		goto error;
401

402
	ret = af9015_eeprom_hash(d);
403 404 405
	if (ret)
		goto error;

406
	state->ir_mode = val;
A
Antti Palosaari 已提交
407
	dev_dbg(&intf->dev, "ir mode %02x\n", val);
408

409 410
	/* TS mode - one or two receivers */
	req.addr = AF9015_EEPROM_TS_MODE;
411 412 413
	ret = af9015_ctrl_msg(d, &req);
	if (ret)
		goto error;
414

415
	state->dual_mode = val;
A
Antti Palosaari 已提交
416
	dev_dbg(&intf->dev, "ts mode %02x\n", state->dual_mode);
417

418
	state->af9013_i2c_addr[0] = AF9015_I2C_DEMOD;
419

420
	if (state->dual_mode) {
421 422
		/* read 2nd demodulator I2C address */
		req.addr = AF9015_EEPROM_DEMOD2_I2C;
423
		ret = af9015_ctrl_msg(d, &req);
424 425 426
		if (ret)
			goto error;

427
		state->af9013_i2c_addr[1] = val >> 1;
428 429
	}

430
	for (i = 0; i < state->dual_mode + 1; i++) {
431 432 433 434
		if (i == 1)
			offset = AF9015_EEPROM_OFFSET;
		/* xtal */
		req.addr = AF9015_EEPROM_XTAL_TYPE1 + offset;
435
		ret = af9015_ctrl_msg(d, &req);
436 437 438 439
		if (ret)
			goto error;
		switch (val) {
		case 0:
440
			state->af9013_pdata[i].clk = 28800000;
441 442
			break;
		case 1:
443
			state->af9013_pdata[i].clk = 20480000;
444 445
			break;
		case 2:
446
			state->af9013_pdata[i].clk = 28000000;
447 448
			break;
		case 3:
449
			state->af9013_pdata[i].clk = 25000000;
450
			break;
451
		}
452 453
		dev_dbg(&intf->dev, "[%d] xtal %02x, clk %u\n",
			i, val, state->af9013_pdata[i].clk);
454

455
		/* IF frequency */
456
		req.addr = AF9015_EEPROM_IF1H + offset;
457
		ret = af9015_ctrl_msg(d, &req);
458 459
		if (ret)
			goto error;
460

461
		state->af9013_pdata[i].if_frequency = val << 8;
462

463
		req.addr = AF9015_EEPROM_IF1L + offset;
464
		ret = af9015_ctrl_msg(d, &req);
465 466
		if (ret)
			goto error;
467

468 469
		state->af9013_pdata[i].if_frequency += val;
		state->af9013_pdata[i].if_frequency *= 1000;
A
Antti Palosaari 已提交
470
		dev_dbg(&intf->dev, "[%d] if frequency %u\n",
471
			i, state->af9013_pdata[i].if_frequency);
472 473 474

		/* MT2060 IF1 */
		req.addr = AF9015_EEPROM_MT2060_IF1H  + offset;
475
		ret = af9015_ctrl_msg(d, &req);
476 477
		if (ret)
			goto error;
478
		state->mt2060_if1[i] = val << 8;
479
		req.addr = AF9015_EEPROM_MT2060_IF1L + offset;
480
		ret = af9015_ctrl_msg(d, &req);
481 482
		if (ret)
			goto error;
483
		state->mt2060_if1[i] += val;
A
Antti Palosaari 已提交
484 485
		dev_dbg(&intf->dev, "[%d] MT2060 IF1 %u\n",
			i, state->mt2060_if1[i]);
486 487 488

		/* tuner */
		req.addr =  AF9015_EEPROM_TUNER_ID1 + offset;
489
		ret = af9015_ctrl_msg(d, &req);
490 491 492 493 494 495 496 497 498 499
		if (ret)
			goto error;
		switch (val) {
		case AF9013_TUNER_ENV77H11D5:
		case AF9013_TUNER_MT2060:
		case AF9013_TUNER_QT1010:
		case AF9013_TUNER_UNKNOWN:
		case AF9013_TUNER_MT2060_2:
		case AF9013_TUNER_TDA18271:
		case AF9013_TUNER_QT1010A:
500
		case AF9013_TUNER_TDA18218:
501
			state->af9013_pdata[i].spec_inv = 1;
502 503 504 505
			break;
		case AF9013_TUNER_MXL5003D:
		case AF9013_TUNER_MXL5005D:
		case AF9013_TUNER_MXL5005R:
506
		case AF9013_TUNER_MXL5007T:
507
			state->af9013_pdata[i].spec_inv = 0;
508
			break;
509
		case AF9013_TUNER_MC44S803:
510 511
			state->af9013_pdata[i].gpio[1] = AF9013_GPIO_LO;
			state->af9013_pdata[i].spec_inv = 1;
512
			break;
513
		default:
A
Antti Palosaari 已提交
514 515 516
			dev_err(&intf->dev,
				"tuner id %02x not supported, please report!\n",
				val);
517
			return -ENODEV;
518
		}
519

520
		state->af9013_pdata[i].tuner = val;
A
Antti Palosaari 已提交
521
		dev_dbg(&intf->dev, "[%d] tuner id %02x\n", i, val);
522 523 524 525
	}

error:
	if (ret)
A
Antti Palosaari 已提交
526
		dev_err(&intf->dev, "eeprom read failed %d\n", ret);
527

528 529 530 531 532
	/*
	 * AverMedia AVerTV Volar Black HD (A850) device have bad EEPROM
	 * content :-( Override some wrong values here. Ditto for the
	 * AVerTV Red HD+ (A850T) device.
	 */
533
	if (le16_to_cpu(d->udev->descriptor.idVendor) == USB_VID_AVERMEDIA &&
534 535
	    ((le16_to_cpu(d->udev->descriptor.idProduct) == USB_PID_AVERMEDIA_A850) ||
	    (le16_to_cpu(d->udev->descriptor.idProduct) == USB_PID_AVERMEDIA_A850T))) {
A
Antti Palosaari 已提交
536
		dev_dbg(&intf->dev, "AverMedia A850: overriding config\n");
537
		/* disable dual mode */
538
		state->dual_mode = 0;
539 540

		/* set correct IF */
541
		state->af9013_pdata[0].if_frequency = 4570000;
542 543
	}

544 545 546
	return ret;
}

547
static int af9015_get_stream_config(struct dvb_frontend *fe, u8 *ts_type,
548
				    struct usb_data_stream_properties *stream)
549
{
550
	struct dvb_usb_device *d = fe_to_d(fe);
A
Antti Palosaari 已提交
551 552 553
	struct usb_interface *intf = d->intf;

	dev_dbg(&intf->dev, "adap %u\n", fe_to_adap(fe)->id);
554

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

558 559
	return 0;
}
560

561 562 563 564 565 566 567 568 569 570 571 572
static int af9015_streaming_ctrl(struct dvb_frontend *fe, int onoff)
{
	struct dvb_usb_device *d = fe_to_d(fe);
	struct af9015_state *state = d_to_priv(d);
	struct usb_interface *intf = d->intf;
	int ret;
	unsigned int utmp1, utmp2, reg1, reg2;
	u8 buf[2];
	const unsigned int adap_id = fe_to_adap(fe)->id;

	dev_dbg(&intf->dev, "adap id %d, onoff %d\n", adap_id, onoff);

573
	if (!state->usb_ts_if_configured[adap_id]) {
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
		dev_dbg(&intf->dev, "set usb and ts interface\n");

		/* USB IF stream settings */
		utmp1 = (d->udev->speed == USB_SPEED_FULL ? 5 : 87) * 188 / 4;
		utmp2 = (d->udev->speed == USB_SPEED_FULL ? 64 : 512) / 4;

		buf[0] = (utmp1 >> 0) & 0xff;
		buf[1] = (utmp1 >> 8) & 0xff;
		if (adap_id == 0) {
			/* 1st USB IF (EP4) stream settings */
			reg1 = 0xdd88;
			reg2 = 0xdd0c;
		} else {
			/* 2nd USB IF (EP5) stream settings */
			reg1 = 0xdd8a;
			reg2 = 0xdd0d;
		}
591
		ret = regmap_bulk_write(state->regmap, reg1, buf, 2);
592 593
		if (ret)
			goto err;
594
		ret = regmap_write(state->regmap, reg2, utmp2);
595 596 597 598 599
		if (ret)
			goto err;

		/* TS IF settings */
		if (state->dual_mode) {
600 601
			utmp1 = 0x01;
			utmp2 = 0x10;
602
		} else {
603 604
			utmp1 = 0x00;
			utmp2 = 0x00;
605
		}
606 607 608 609 610 611
		ret = regmap_update_bits(state->regmap, 0xd50b, 0x01, utmp1);
		if (ret)
			goto err;
		ret = regmap_update_bits(state->regmap, 0xd520, 0x10, utmp2);
		if (ret)
			goto err;
612 613 614 615 616 617

		state->usb_ts_if_configured[adap_id] = true;
	}

	if (adap_id == 0 && onoff) {
		/* Adapter 0 stream on. EP4: clear NAK, enable, clear reset */
618
		ret = regmap_update_bits(state->regmap, 0xdd13, 0x20, 0x00);
619 620
		if (ret)
			goto err;
621
		ret = regmap_update_bits(state->regmap, 0xdd11, 0x20, 0x20);
622 623
		if (ret)
			goto err;
624
		ret = regmap_update_bits(state->regmap, 0xd507, 0x04, 0x00);
625 626 627 628
		if (ret)
			goto err;
	} else if (adap_id == 1 && onoff) {
		/* Adapter 1 stream on. EP5: clear NAK, enable, clear reset */
629
		ret = regmap_update_bits(state->regmap, 0xdd13, 0x40, 0x00);
630 631
		if (ret)
			goto err;
632
		ret = regmap_update_bits(state->regmap, 0xdd11, 0x40, 0x40);
633 634
		if (ret)
			goto err;
635
		ret = regmap_update_bits(state->regmap, 0xd50b, 0x02, 0x00);
636 637 638 639
		if (ret)
			goto err;
	} else if (adap_id == 0 && !onoff) {
		/* Adapter 0 stream off. EP4: set reset, disable, set NAK */
640
		ret = regmap_update_bits(state->regmap, 0xd507, 0x04, 0x04);
641 642
		if (ret)
			goto err;
643
		ret = regmap_update_bits(state->regmap, 0xdd11, 0x20, 0x00);
644 645
		if (ret)
			goto err;
646
		ret = regmap_update_bits(state->regmap, 0xdd13, 0x20, 0x20);
647 648 649 650
		if (ret)
			goto err;
	} else if (adap_id == 1 && !onoff) {
		/* Adapter 1 stream off. EP5: set reset, disable, set NAK */
651
		ret = regmap_update_bits(state->regmap, 0xd50b, 0x02, 0x02);
652 653
		if (ret)
			goto err;
654
		ret = regmap_update_bits(state->regmap, 0xdd11, 0x40, 0x00);
655 656
		if (ret)
			goto err;
657
		ret = regmap_update_bits(state->regmap, 0xdd13, 0x40, 0x40);
658 659 660 661 662 663 664 665 666 667
		if (ret)
			goto err;
	}

	return 0;
err:
	dev_dbg(&intf->dev, "failed %d\n", ret);
	return ret;
}

668 669
static int af9015_get_adapter_count(struct dvb_usb_device *d)
{
670
	struct af9015_state *state = d_to_priv(d);
671

672
	return state->dual_mode + 1;
673 674
}

675
/* override demod callbacks for resource locking */
676
static int af9015_af9013_set_frontend(struct dvb_frontend *fe)
677 678
{
	int ret;
679
	struct af9015_state *state = fe_to_priv(fe);
680

681
	if (mutex_lock_interruptible(&state->fe_mutex))
682 683
		return -EAGAIN;

684
	ret = state->set_frontend[fe_to_adap(fe)->id](fe);
685

686
	mutex_unlock(&state->fe_mutex);
687 688 689 690 691 692

	return ret;
}

/* override demod callbacks for resource locking */
static int af9015_af9013_read_status(struct dvb_frontend *fe,
693
				     enum fe_status *status)
694 695
{
	int ret;
696
	struct af9015_state *state = fe_to_priv(fe);
697

698
	if (mutex_lock_interruptible(&state->fe_mutex))
699 700
		return -EAGAIN;

701
	ret = state->read_status[fe_to_adap(fe)->id](fe, status);
702

703
	mutex_unlock(&state->fe_mutex);
704 705 706 707 708 709 710 711

	return ret;
}

/* override demod callbacks for resource locking */
static int af9015_af9013_init(struct dvb_frontend *fe)
{
	int ret;
712
	struct af9015_state *state = fe_to_priv(fe);
713

714
	if (mutex_lock_interruptible(&state->fe_mutex))
715 716
		return -EAGAIN;

717
	ret = state->init[fe_to_adap(fe)->id](fe);
718

719
	mutex_unlock(&state->fe_mutex);
720 721 722 723 724 725 726 727

	return ret;
}

/* override demod callbacks for resource locking */
static int af9015_af9013_sleep(struct dvb_frontend *fe)
{
	int ret;
728
	struct af9015_state *state = fe_to_priv(fe);
729

730
	if (mutex_lock_interruptible(&state->fe_mutex))
731 732
		return -EAGAIN;

733
	ret = state->sleep[fe_to_adap(fe)->id](fe);
734

735
	mutex_unlock(&state->fe_mutex);
736 737 738 739

	return ret;
}

740 741 742 743
/* override tuner callbacks for resource locking */
static int af9015_tuner_init(struct dvb_frontend *fe)
{
	int ret;
744
	struct af9015_state *state = fe_to_priv(fe);
745

746
	if (mutex_lock_interruptible(&state->fe_mutex))
747 748
		return -EAGAIN;

749
	ret = state->tuner_init[fe_to_adap(fe)->id](fe);
750

751
	mutex_unlock(&state->fe_mutex);
752 753 754 755 756 757 758 759

	return ret;
}

/* override tuner callbacks for resource locking */
static int af9015_tuner_sleep(struct dvb_frontend *fe)
{
	int ret;
760
	struct af9015_state *state = fe_to_priv(fe);
761

762
	if (mutex_lock_interruptible(&state->fe_mutex))
763 764
		return -EAGAIN;

765
	ret = state->tuner_sleep[fe_to_adap(fe)->id](fe);
766 767 768 769 770 771 772 773

	mutex_unlock(&state->fe_mutex);

	return ret;
}

static int af9015_copy_firmware(struct dvb_usb_device *d)
{
774
	struct af9015_state *state = d_to_priv(d);
A
Antti Palosaari 已提交
775
	struct usb_interface *intf = d->intf;
776
	int ret;
777 778 779
	unsigned long timeout;
	u8 val, firmware_info[4];
	struct req_t req = {COPY_FIRMWARE, 0, 0x5100, 0, 0, 4, firmware_info};
A
Antti Palosaari 已提交
780 781

	dev_dbg(&intf->dev, "\n");
782

783 784 785 786
	firmware_info[0] = (state->firmware_size >> 8) & 0xff;
	firmware_info[1] = (state->firmware_size >> 0) & 0xff;
	firmware_info[2] = (state->firmware_checksum >> 8) & 0xff;
	firmware_info[3] = (state->firmware_checksum >> 0) & 0xff;
787

788 789
	/* Check whether firmware is already running */
	ret = af9015_read_reg_i2c(d, state->af9013_i2c_addr[1], 0x98be, &val);
790
	if (ret)
791
		goto err;
792

793
	dev_dbg(&intf->dev, "firmware status %02x\n", val);
794

795 796
	if (val == 0x0c)
		return 0;
797

798
	/* Set i2c clock to 625kHz to speed up firmware copy */
799
	ret = regmap_write(state->regmap, 0xd416, 0x04);
800 801
	if (ret)
		goto err;
802

803
	/* Copy firmware from master demod to slave demod */
804
	ret = af9015_ctrl_msg(d, &req);
805
	if (ret) {
A
Antti Palosaari 已提交
806
		dev_err(&intf->dev, "firmware copy cmd failed %d\n", ret);
807 808
		goto err;
	}
809

810
	/* Set i2c clock to 125kHz */
811
	ret = regmap_write(state->regmap, 0xd416, 0x14);
812
	if (ret)
813
		goto err;
814

815 816
	/* Boot firmware */
	ret = af9015_write_reg_i2c(d, state->af9013_i2c_addr[1], 0xe205, 0x01);
817
	if (ret)
818
		goto err;
819

820 821 822 823
	/* Poll firmware ready */
	for (val = 0x00, timeout = jiffies + msecs_to_jiffies(1000);
	     !time_after(jiffies, timeout) && val != 0x0c && val != 0x04;) {
		msleep(20);
824

825
		/* Check firmware status. 0c=OK, 04=fail */
826
		ret = af9015_read_reg_i2c(d, state->af9013_i2c_addr[1],
827
					  0x98be, &val);
828
		if (ret)
829
			goto err;
830

831
		dev_dbg(&intf->dev, "firmware status %02x\n", val);
832 833
	}

834 835 836
	dev_dbg(&intf->dev, "firmware boot took %u ms\n",
		jiffies_to_msecs(jiffies) - (jiffies_to_msecs(timeout) - 1000));

837
	if (val == 0x04) {
838
		ret = -ENODEV;
A
Antti Palosaari 已提交
839
		dev_err(&intf->dev, "firmware did not run\n");
840
		goto err;
841
	} else if (val != 0x0c) {
842
		ret = -ETIMEDOUT;
A
Antti Palosaari 已提交
843
		dev_err(&intf->dev, "firmware boot timeout\n");
844
		goto err;
845 846
	}

847 848 849
	return 0;
err:
	dev_dbg(&intf->dev, "failed %d\n", ret);
850 851 852
	return ret;
}

853 854
static int af9015_af9013_frontend_attach(struct dvb_usb_adapter *adap)
{
855
	struct af9015_state *state = adap_to_priv(adap);
A
Antti Palosaari 已提交
856 857
	struct dvb_usb_device *d = adap_to_d(adap);
	struct usb_interface *intf = d->intf;
858
	struct i2c_client *client;
A
Antti Palosaari 已提交
859
	int ret;
860

861 862
	dev_dbg(&intf->dev, "adap id %u\n", adap->id);

863
	if (adap->id == 0) {
864 865 866 867
		state->af9013_pdata[0].ts_mode = AF9013_TS_MODE_USB;
		memcpy(state->af9013_pdata[0].api_version, "\x0\x1\x9\x0", 4);
		state->af9013_pdata[0].gpio[0] = AF9013_GPIO_HI;
		state->af9013_pdata[0].gpio[3] = AF9013_GPIO_TUNER_ON;
868
	} else if (adap->id == 1) {
869 870 871 872 873
		state->af9013_pdata[1].ts_mode = AF9013_TS_MODE_SERIAL;
		state->af9013_pdata[1].ts_output_pin = 7;
		memcpy(state->af9013_pdata[1].api_version, "\x0\x1\x9\x0", 4);
		state->af9013_pdata[1].gpio[0] = AF9013_GPIO_TUNER_ON;
		state->af9013_pdata[1].gpio[1] = AF9013_GPIO_LO;
874

875
		/* copy firmware to 2nd demodulator */
876
		if (state->dual_mode) {
877 878 879
			/* Wait 2nd demodulator ready */
			msleep(100);

880
			ret = af9015_copy_firmware(adap_to_d(adap));
881
			if (ret) {
A
Antti Palosaari 已提交
882 883
				dev_err(&intf->dev,
					"firmware copy to 2nd frontend failed, will disable it\n");
884
				state->dual_mode = 0;
885
				goto err;
886 887
			}
		} else {
888 889
			ret = -ENODEV;
			goto err;
890 891 892
		}
	}

893 894 895 896 897 898 899 900 901 902
	/* Add I2C demod */
	client = dvb_module_probe("af9013", NULL, &d->i2c_adap,
				  state->af9013_i2c_addr[adap->id],
				  &state->af9013_pdata[adap->id]);
	if (!client) {
		ret = -ENODEV;
		goto err;
	}
	adap->fe[0] = state->af9013_pdata[adap->id].get_dvb_frontend(client);
	state->demod_i2c_client[adap->id] = client;
903

904 905 906 907 908 909 910
	/*
	 * AF9015 firmware does not like if it gets interrupted by I2C adapter
	 * request on some critical phases. During normal operation I2C adapter
	 * is used only 2nd demodulator and tuner on dual tuner devices.
	 * Override demodulator callbacks and use mutex for limit access to
	 * those "critical" paths to keep AF9015 happy.
	 */
911
	if (adap->fe[0]) {
912 913 914 915
		state->set_frontend[adap->id] = adap->fe[0]->ops.set_frontend;
		adap->fe[0]->ops.set_frontend = af9015_af9013_set_frontend;
		state->read_status[adap->id] = adap->fe[0]->ops.read_status;
		adap->fe[0]->ops.read_status = af9015_af9013_read_status;
916 917 918 919
		state->init[adap->id] = adap->fe[0]->ops.init;
		adap->fe[0]->ops.init = af9015_af9013_init;
		state->sleep[adap->id] = adap->fe[0]->ops.sleep;
		adap->fe[0]->ops.sleep = af9015_af9013_sleep;
920 921
	}

922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
	return 0;
err:
	dev_dbg(&intf->dev, "failed %d\n", ret);
	return ret;
}

static int af9015_frontend_detach(struct dvb_usb_adapter *adap)
{
	struct af9015_state *state = adap_to_priv(adap);
	struct dvb_usb_device *d = adap_to_d(adap);
	struct usb_interface *intf = d->intf;
	struct i2c_client *client;

	dev_dbg(&intf->dev, "adap id %u\n", adap->id);

	/* Remove I2C demod */
	client = state->demod_i2c_client[adap->id];
	dvb_module_release(client);

	return 0;
942 943 944
}

static struct mt2060_config af9015_mt2060_config = {
945
	.i2c_address = 0x60,
946 947 948 949
	.clock_out = 0,
};

static struct qt1010_config af9015_qt1010_config = {
950
	.i2c_address = 0x62,
951 952 953 954
};

static struct tda18271_config af9015_tda18271_config = {
	.gate = TDA18271_GATE_DIGITAL,
955
	.small_i2c = TDA18271_16_BYTE_CHUNK_INIT,
956 957 958
};

static struct mxl5005s_config af9015_mxl5003_config = {
959
	.i2c_address     = 0x63,
960 961 962 963
	.if_freq         = IF_FREQ_4570000HZ,
	.xtal_freq       = CRYSTAL_FREQ_16000000HZ,
	.agc_mode        = MXL_SINGLE_AGC,
	.tracking_filter = MXL_TF_DEFAULT,
964
	.rssi_enable     = MXL_RSSI_ENABLE,
965 966 967 968 969 970 971 972 973 974 975
	.cap_select      = MXL_CAP_SEL_ENABLE,
	.div_out         = MXL_DIV_OUT_4,
	.clock_out       = MXL_CLOCK_OUT_DISABLE,
	.output_load     = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
	.top		 = MXL5005S_TOP_25P2,
	.mod_mode        = MXL_DIGITAL_MODE,
	.if_mode         = MXL_ZERO_IF,
	.AgcMasterByte   = 0x00,
};

static struct mxl5005s_config af9015_mxl5005_config = {
976
	.i2c_address     = 0x63,
977 978 979 980
	.if_freq         = IF_FREQ_4570000HZ,
	.xtal_freq       = CRYSTAL_FREQ_16000000HZ,
	.agc_mode        = MXL_SINGLE_AGC,
	.tracking_filter = MXL_TF_OFF,
981
	.rssi_enable     = MXL_RSSI_ENABLE,
982 983 984 985 986 987 988 989 990 991
	.cap_select      = MXL_CAP_SEL_ENABLE,
	.div_out         = MXL_DIV_OUT_4,
	.clock_out       = MXL_CLOCK_OUT_DISABLE,
	.output_load     = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
	.top		 = MXL5005S_TOP_25P2,
	.mod_mode        = MXL_DIGITAL_MODE,
	.if_mode         = MXL_ZERO_IF,
	.AgcMasterByte   = 0x00,
};

992
static struct mc44s803_config af9015_mc44s803_config = {
993
	.i2c_address = 0x60,
994 995 996
	.dig_out = 1,
};

997
static struct tda18218_config af9015_tda18218_config = {
998
	.i2c_address = 0x60,
999 1000 1001
	.i2c_wr_max = 21, /* max wr bytes AF9015 I2C adap can handle at once */
};

1002 1003 1004 1005 1006
static struct mxl5007t_config af9015_mxl5007t_config = {
	.xtal_freq_hz = MxL_XTAL_24_MHZ,
	.if_freq_hz = MxL_IF_4_57_MHZ,
};

1007 1008
static int af9015_tuner_attach(struct dvb_usb_adapter *adap)
{
1009 1010
	struct dvb_usb_device *d = adap_to_d(adap);
	struct af9015_state *state = d_to_priv(d);
A
Antti Palosaari 已提交
1011
	struct usb_interface *intf = d->intf;
1012 1013
	struct i2c_client *client;
	struct i2c_adapter *adapter;
1014
	int ret;
A
Antti Palosaari 已提交
1015

1016 1017 1018 1019
	dev_dbg(&intf->dev, "adap id %u\n", adap->id);

	client = state->demod_i2c_client[adap->id];
	adapter = state->af9013_pdata[adap->id].get_i2c_adapter(client);
1020

1021
	switch (state->af9013_pdata[adap->id].tuner) {
1022 1023
	case AF9013_TUNER_MT2060:
	case AF9013_TUNER_MT2060_2:
1024
		ret = dvb_attach(mt2060_attach, adap->fe[0], adapter,
1025 1026
				 &af9015_mt2060_config,
				 state->mt2060_if1[adap->id]) == NULL ? -ENODEV : 0;
1027 1028 1029
		break;
	case AF9013_TUNER_QT1010:
	case AF9013_TUNER_QT1010A:
1030
		ret = dvb_attach(qt1010_attach, adap->fe[0], adapter,
1031
				 &af9015_qt1010_config) == NULL ? -ENODEV : 0;
1032 1033
		break;
	case AF9013_TUNER_TDA18271:
1034
		ret = dvb_attach(tda18271_attach, adap->fe[0], 0x60, adapter,
1035
				 &af9015_tda18271_config) == NULL ? -ENODEV : 0;
1036
		break;
1037
	case AF9013_TUNER_TDA18218:
1038
		ret = dvb_attach(tda18218_attach, adap->fe[0], adapter,
1039
				 &af9015_tda18218_config) == NULL ? -ENODEV : 0;
1040
		break;
1041
	case AF9013_TUNER_MXL5003D:
1042
		ret = dvb_attach(mxl5005s_attach, adap->fe[0], adapter,
1043
				 &af9015_mxl5003_config) == NULL ? -ENODEV : 0;
1044 1045 1046
		break;
	case AF9013_TUNER_MXL5005D:
	case AF9013_TUNER_MXL5005R:
1047
		ret = dvb_attach(mxl5005s_attach, adap->fe[0], adapter,
1048
				 &af9015_mxl5005_config) == NULL ? -ENODEV : 0;
1049 1050
		break;
	case AF9013_TUNER_ENV77H11D5:
1051
		ret = dvb_attach(dvb_pll_attach, adap->fe[0], 0x60, adapter,
1052
				 DVB_PLL_TDA665X) == NULL ? -ENODEV : 0;
1053 1054
		break;
	case AF9013_TUNER_MC44S803:
1055
		ret = dvb_attach(mc44s803_attach, adap->fe[0], adapter,
1056
				 &af9015_mc44s803_config) == NULL ? -ENODEV : 0;
1057
		break;
1058
	case AF9013_TUNER_MXL5007T:
1059
		ret = dvb_attach(mxl5007t_attach, adap->fe[0], adapter,
1060
				 0x60, &af9015_mxl5007t_config) == NULL ? -ENODEV : 0;
1061
		break;
1062 1063
	case AF9013_TUNER_UNKNOWN:
	default:
A
Antti Palosaari 已提交
1064
		dev_err(&intf->dev, "unknown tuner, tuner id %02x\n",
1065
			state->af9013_pdata[adap->id].tuner);
1066 1067
		ret = -ENODEV;
	}
1068

1069
	if (adap->fe[0]->ops.tuner_ops.init) {
1070
		state->tuner_init[adap->id] =
1071 1072
			adap->fe[0]->ops.tuner_ops.init;
		adap->fe[0]->ops.tuner_ops.init = af9015_tuner_init;
1073 1074
	}

1075
	if (adap->fe[0]->ops.tuner_ops.sleep) {
1076
		state->tuner_sleep[adap->id] =
1077 1078 1079 1080 1081 1082 1083 1084 1085
			adap->fe[0]->ops.tuner_ops.sleep;
		adap->fe[0]->ops.tuner_ops.sleep = af9015_tuner_sleep;
	}

	return ret;
}

static int af9015_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
{
1086 1087
	struct af9015_state *state = adap_to_priv(adap);
	struct af9013_platform_data *pdata = &state->af9013_pdata[adap->id];
1088
	int ret;
A
Antti Palosaari 已提交
1089

1090 1091 1092
	mutex_lock(&state->fe_mutex);
	ret = pdata->pid_filter_ctrl(adap->fe[0], onoff);
	mutex_unlock(&state->fe_mutex);
1093 1094 1095 1096

	return ret;
}

1097 1098
static int af9015_pid_filter(struct dvb_usb_adapter *adap, int index,
			     u16 pid, int onoff)
1099
{
1100 1101
	struct af9015_state *state = adap_to_priv(adap);
	struct af9013_platform_data *pdata = &state->af9013_pdata[adap->id];
1102 1103
	int ret;

1104 1105 1106
	mutex_lock(&state->fe_mutex);
	ret = pdata->pid_filter(adap->fe[0], index, pid, onoff);
	mutex_unlock(&state->fe_mutex);
1107 1108 1109 1110 1111 1112

	return ret;
}

static int af9015_init(struct dvb_usb_device *d)
{
1113
	struct af9015_state *state = d_to_priv(d);
A
Antti Palosaari 已提交
1114
	struct usb_interface *intf = d->intf;
1115
	int ret;
A
Antti Palosaari 已提交
1116 1117

	dev_dbg(&intf->dev, "\n");
1118 1119 1120 1121

	mutex_init(&state->fe_mutex);

	/* init RC canary */
1122
	ret = regmap_write(state->regmap, 0x98e9, 0xff);
1123 1124 1125 1126
	if (ret)
		goto error;

error:
1127 1128 1129
	return ret;
}

1130
#if IS_ENABLED(CONFIG_RC_CORE)
1131 1132 1133
struct af9015_rc_setup {
	unsigned int id;
	char *rc_codes;
1134 1135
};

1136
static char *af9015_rc_setup_match(unsigned int id,
1137
				   const struct af9015_rc_setup *table)
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
{
	for (; table->rc_codes; table++)
		if (table->id == id)
			return table->rc_codes;
	return NULL;
}

static const struct af9015_rc_setup af9015_rc_setup_modparam[] = {
	{ AF9015_REMOTE_A_LINK_DTU_M, RC_MAP_ALINK_DTU_M },
	{ AF9015_REMOTE_MSI_DIGIVOX_MINI_II_V3, RC_MAP_MSI_DIGIVOX_II },
	{ AF9015_REMOTE_MYGICTV_U718, RC_MAP_TOTAL_MEDIA_IN_HAND },
	{ AF9015_REMOTE_DIGITTRADE_DVB_T, RC_MAP_DIGITTRADE },
	{ AF9015_REMOTE_AVERMEDIA_KS, RC_MAP_AVERMEDIA_RM_KS },
1151
	{ }
1152 1153
};

1154 1155 1156 1157 1158 1159 1160
static const struct af9015_rc_setup af9015_rc_setup_hashes[] = {
	{ 0xb8feb708, RC_MAP_MSI_DIGIVOX_II },
	{ 0xa3703d00, RC_MAP_ALINK_DTU_M },
	{ 0x9b7dc64e, RC_MAP_TOTAL_MEDIA_IN_HAND }, /* MYGICTV U718 */
	{ 0x5d49e3db, RC_MAP_DIGITTRADE }, /* LC-Power LC-USB-DVBT */
	{ }
};
1161

1162 1163
static int af9015_rc_query(struct dvb_usb_device *d)
{
1164
	struct af9015_state *state = d_to_priv(d);
A
Antti Palosaari 已提交
1165
	struct usb_interface *intf = d->intf;
1166 1167
	int ret;
	u8 buf[17];
1168

1169
	/* read registers needed to detect remote controller code */
1170
	ret = regmap_bulk_read(state->regmap, 0x98d9, buf, sizeof(buf));
1171 1172
	if (ret)
		goto error;
1173

1174
	/* If any of these are non-zero, assume invalid data */
1175
	if (buf[1] || buf[2] || buf[3]) {
A
Antti Palosaari 已提交
1176
		dev_dbg(&intf->dev, "invalid data\n");
1177
		return ret;
1178
	}
1179

1180 1181
	/* Check for repeat of previous code */
	if ((state->rc_repeat != buf[6] || buf[0]) &&
1182
	    !memcmp(&buf[12], state->rc_last, 4)) {
A
Antti Palosaari 已提交
1183
		dev_dbg(&intf->dev, "key repeated\n");
1184
		rc_repeat(d->rc_dev);
1185 1186 1187
		state->rc_repeat = buf[6];
		return ret;
	}
1188

1189 1190
	/* Only process key if canary killed */
	if (buf[16] != 0xff && buf[0] != 0x01) {
1191
		enum rc_proto proto;
1192

A
Antti Palosaari 已提交
1193
		dev_dbg(&intf->dev, "key pressed %*ph\n", 4, buf + 12);
1194

1195
		/* Reset the canary */
1196
		ret = regmap_write(state->regmap, 0x98e9, 0xff);
1197 1198
		if (ret)
			goto error;
1199

1200 1201
		/* Remember this key */
		memcpy(state->rc_last, &buf[12], 4);
1202 1203
		if (buf[14] == (u8)~buf[15]) {
			if (buf[12] == (u8)~buf[13]) {
1204
				/* NEC */
1205 1206
				state->rc_keycode = RC_SCANCODE_NEC(buf[12],
								    buf[14]);
1207
				proto = RC_PROTO_NEC;
1208 1209
			} else {
				/* NEC extended*/
1210 1211 1212
				state->rc_keycode = RC_SCANCODE_NECX(buf[12] << 8 |
								     buf[13],
								     buf[14]);
1213
				proto = RC_PROTO_NECX;
1214 1215 1216
			}
		} else {
			/* 32 bit NEC */
1217 1218 1219 1220
			state->rc_keycode = RC_SCANCODE_NEC32(buf[12] << 24 |
							      buf[13] << 16 |
							      buf[14] << 8  |
							      buf[15]);
1221
			proto = RC_PROTO_NEC32;
1222
		}
1223
		rc_keydown(d->rc_dev, proto, state->rc_keycode, 0);
1224
	} else {
A
Antti Palosaari 已提交
1225
		dev_dbg(&intf->dev, "no key press\n");
1226 1227 1228 1229 1230 1231
		/* Invalidate last keypress */
		/* Not really needed, but helps with debug */
		state->rc_last[2] = state->rc_last[3];
	}

	state->rc_repeat = buf[6];
1232
	state->rc_failed = false;
1233 1234

error:
1235
	if (ret) {
A
Antti Palosaari 已提交
1236
		dev_warn(&intf->dev, "rc query failed %d\n", ret);
1237

1238 1239 1240 1241 1242 1243 1244
		/* allow random errors as dvb-usb will stop polling on error */
		if (!state->rc_failed)
			ret = 0;

		state->rc_failed = true;
	}

1245 1246
	return ret;
}
1247

1248
static int af9015_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
1249
{
1250
	struct af9015_state *state = d_to_priv(d);
1251 1252 1253 1254 1255 1256
	u16 vid = le16_to_cpu(d->udev->descriptor.idVendor);

	if (state->ir_mode == AF9015_IR_MODE_DISABLED)
		return 0;

	/* try to load remote based module param */
1257 1258
	if (!rc->map_name)
		rc->map_name = af9015_rc_setup_match(dvb_usb_af9015_remote,
1259
						     af9015_rc_setup_modparam);
1260

1261 1262 1263
	/* try to load remote based eeprom hash */
	if (!rc->map_name)
		rc->map_name = af9015_rc_setup_match(state->eeprom_sum,
1264
						     af9015_rc_setup_hashes);
1265 1266 1267

	/* try to load remote based USB iManufacturer string */
	if (!rc->map_name && vid == USB_VID_AFATECH) {
1268 1269 1270 1271 1272 1273
		/*
		 * Check USB manufacturer and product strings and try
		 * to determine correct remote in case of chip vendor
		 * reference IDs are used.
		 * DO NOT ADD ANYTHING NEW HERE. Use hashes instead.
		 */
1274
		char manufacturer[10];
1275

1276 1277
		memset(manufacturer, 0, sizeof(manufacturer));
		usb_string(d->udev, d->udev->descriptor.iManufacturer,
1278
			   manufacturer, sizeof(manufacturer));
1279
		if (!strcmp("MSI", manufacturer)) {
1280 1281 1282 1283 1284 1285
			/*
			 * iManufacturer 1 MSI
			 * iProduct      2 MSI K-VOX
			 */
			rc->map_name = af9015_rc_setup_match(AF9015_REMOTE_MSI_DIGIVOX_MINI_II_V3,
							     af9015_rc_setup_modparam);
1286
		}
1287 1288
	}

1289 1290 1291 1292
	/* load empty to enable rc */
	if (!rc->map_name)
		rc->map_name = RC_MAP_EMPTY;

1293 1294
	rc->allowed_protos = RC_PROTO_BIT_NEC | RC_PROTO_BIT_NECX |
						RC_PROTO_BIT_NEC32;
1295 1296 1297 1298
	rc->query = af9015_rc_query;
	rc->interval = 500;

	return 0;
1299
}
1300 1301 1302
#else
	#define af9015_get_rc_config NULL
#endif
1303

1304 1305 1306 1307 1308 1309 1310 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
static int af9015_regmap_write(void *context, const void *data, size_t count)
{
	struct dvb_usb_device *d = context;
	struct usb_interface *intf = d->intf;
	int ret;
	u16 reg = ((u8 *)data)[0] << 8 | ((u8 *)data)[1] << 0;
	u8 *val = &((u8 *)data)[2];
	const unsigned int len = count - 2;
	struct req_t req = {WRITE_MEMORY, 0, reg, 0, 0, len, val};

	ret = af9015_ctrl_msg(d, &req);
	if (ret)
		goto err;

	return 0;
err:
	dev_dbg(&intf->dev, "failed %d\n", ret);
	return ret;
}

static int af9015_regmap_read(void *context, const void *reg_buf,
			      size_t reg_size, void *val_buf, size_t val_size)
{
	struct dvb_usb_device *d = context;
	struct usb_interface *intf = d->intf;
	int ret;
	u16 reg = ((u8 *)reg_buf)[0] << 8 | ((u8 *)reg_buf)[1] << 0;
	u8 *val = &((u8 *)val_buf)[0];
	const unsigned int len = val_size;
	struct req_t req = {READ_MEMORY, 0, reg, 0, 0, len, val};

	ret = af9015_ctrl_msg(d, &req);
	if (ret)
		goto err;

	return 0;
err:
	dev_dbg(&intf->dev, "failed %d\n", ret);
	return ret;
}

static int af9015_probe(struct dvb_usb_device *d)
1346
{
1347 1348
	struct af9015_state *state = d_to_priv(d);
	struct usb_interface *intf = d->intf;
1349
	struct usb_device *udev = interface_to_usbdev(intf);
1350
	int ret;
1351
	char manufacturer[sizeof("ITE Technologies, Inc.")];
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
	static const struct regmap_config regmap_config = {
		.reg_bits    =  16,
		.val_bits    =  8,
	};
	static const struct regmap_bus regmap_bus = {
		.read = af9015_regmap_read,
		.write = af9015_regmap_write,
	};

	dev_dbg(&intf->dev, "\n");
1362 1363 1364

	memset(manufacturer, 0, sizeof(manufacturer));
	usb_string(udev, udev->descriptor.iManufacturer,
1365
		   manufacturer, sizeof(manufacturer));
1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383
	/*
	 * 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) &&
1384
	    (le16_to_cpu(udev->descriptor.idProduct) == 0x0099)) {
1385
		if (!strcmp("ITE Technologies, Inc.", manufacturer)) {
1386
			ret = -ENODEV;
A
Antti Palosaari 已提交
1387
			dev_dbg(&intf->dev, "rejecting device\n");
1388
			goto err;
1389 1390 1391
		}
	}

1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411
	state->regmap = regmap_init(&intf->dev, &regmap_bus, d, &regmap_config);
	if (IS_ERR(state->regmap)) {
		ret = PTR_ERR(state->regmap);
		goto err;
	}

	return 0;
err:
	dev_dbg(&intf->dev, "failed %d\n", ret);
	return ret;
}

static void af9015_disconnect(struct dvb_usb_device *d)
{
	struct af9015_state *state = d_to_priv(d);
	struct usb_interface *intf = d->intf;

	dev_dbg(&intf->dev, "\n");

	regmap_exit(state->regmap);
1412 1413
}

1414 1415 1416 1417
/*
 * Interface 0 is used by DVB-T receiver and
 * interface 1 is for remote controller (HID)
 */
1418
static const struct dvb_usb_device_properties af9015_props = {
1419 1420 1421 1422 1423 1424 1425 1426
	.driver_name = KBUILD_MODNAME,
	.owner = THIS_MODULE,
	.adapter_nr = adapter_nr,
	.size_of_priv = sizeof(struct af9015_state),

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

1427 1428
	.probe = af9015_probe,
	.disconnect = af9015_disconnect,
1429
	.identify_state = af9015_identify_state,
1430
	.firmware = AF9015_FIRMWARE,
1431 1432 1433
	.download_firmware = af9015_download_firmware,

	.i2c_algo = &af9015_i2c_algo,
1434 1435
	.read_config = af9015_read_config,
	.frontend_attach = af9015_af9013_frontend_attach,
1436
	.frontend_detach = af9015_frontend_detach,
1437
	.tuner_attach = af9015_tuner_attach,
1438 1439
	.init = af9015_init,
	.get_rc_config = af9015_get_rc_config,
1440
	.get_stream_config = af9015_get_stream_config,
1441
	.streaming_ctrl = af9015_streaming_ctrl,
1442 1443 1444 1445 1446 1447 1448 1449 1450

	.get_adapter_count = af9015_get_adapter_count,
	.adapter = {
		{
			.caps = DVB_USB_ADAP_HAS_PID_FILTER |
				DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
			.pid_filter_count = 32,
			.pid_filter = af9015_pid_filter,
			.pid_filter_ctrl = af9015_pid_filter_ctrl,
1451

1452
			.stream = DVB_USB_STREAM_BULK(0x84, 6, 87 * 188),
1453
		}, {
1454 1455 1456 1457 1458 1459
			.caps = DVB_USB_ADAP_HAS_PID_FILTER |
				DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
			.pid_filter_count = 32,
			.pid_filter = af9015_pid_filter,
			.pid_filter_ctrl = af9015_pid_filter_ctrl,

1460
			.stream = DVB_USB_STREAM_BULK(0x85, 6, 87 * 188),
1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512
		},
	},
};

static const struct usb_device_id af9015_id_table[] = {
	{ DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9015_9015,
		&af9015_props, "Afatech AF9015 reference design", NULL) },
	{ DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9015_9016,
		&af9015_props, "Afatech AF9015 reference design", NULL) },
	{ DVB_USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV_DONGLE_GOLD,
		&af9015_props, "Leadtek WinFast DTV Dongle Gold", RC_MAP_LEADTEK_Y04G0051) },
	{ DVB_USB_DEVICE(USB_VID_PINNACLE, USB_PID_PINNACLE_PCTV71E,
		&af9015_props, "Pinnacle PCTV 71e", NULL) },
	{ DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_399U,
		&af9015_props, "KWorld PlusTV Dual DVB-T Stick (DVB-T 399U)", NULL) },
	{ DVB_USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_TINYTWIN,
		&af9015_props, "DigitalNow TinyTwin", RC_MAP_AZUREWAVE_AD_TU700) },
	{ DVB_USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_AZUREWAVE_AD_TU700,
		&af9015_props, "TwinHan AzureWave AD-TU700(704J)", RC_MAP_AZUREWAVE_AD_TU700) },
	{ DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_USB_XE_REV2,
		&af9015_props, "TerraTec Cinergy T USB XE", NULL) },
	{ DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_PC160_2T,
		&af9015_props, "KWorld PlusTV Dual DVB-T PCI (DVB-T PC160-2T)", NULL) },
	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_X,
		&af9015_props, "AVerMedia AVerTV DVB-T Volar X", RC_MAP_AVERMEDIA_M135A) },
	{ DVB_USB_DEVICE(USB_VID_XTENSIONS, USB_PID_XTENSIONS_XD_380,
		&af9015_props, "Xtensions XD-380", NULL) },
	{ DVB_USB_DEVICE(USB_VID_MSI_2, USB_PID_MSI_DIGIVOX_DUO,
		&af9015_props, "MSI DIGIVOX Duo", RC_MAP_MSI_DIGIVOX_III) },
	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_X_2,
		&af9015_props, "Fujitsu-Siemens Slim Mobile USB DVB-T", NULL) },
	{ DVB_USB_DEVICE(USB_VID_TELESTAR,  USB_PID_TELESTAR_STARSTICK_2,
		&af9015_props, "Telestar Starstick 2", NULL) },
	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A309,
		&af9015_props, "AVerMedia A309", NULL) },
	{ DVB_USB_DEVICE(USB_VID_MSI_2, USB_PID_MSI_DIGI_VOX_MINI_III,
		&af9015_props, "MSI Digi VOX mini III", RC_MAP_MSI_DIGIVOX_III) },
	{ DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U,
		&af9015_props, "KWorld USB DVB-T TV Stick II (VS-DVB-T 395U)", NULL) },
	{ DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_2,
		&af9015_props, "KWorld USB DVB-T TV Stick II (VS-DVB-T 395U)", NULL) },
	{ DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_3,
		&af9015_props, "KWorld USB DVB-T TV Stick II (VS-DVB-T 395U)", NULL) },
	{ DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_TREKSTOR_DVBT,
		&af9015_props, "TrekStor DVB-T USB Stick", RC_MAP_TREKSTOR) },
	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A850,
		&af9015_props, "AverMedia AVerTV Volar Black HD (A850)", NULL) },
	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A805,
		&af9015_props, "AverMedia AVerTV Volar GPS 805 (A805)", NULL) },
	{ DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_CONCEPTRONIC_CTVDIGRCU,
		&af9015_props, "Conceptronic USB2.0 DVB-T CTVDIGRCU V3.0", NULL) },
	{ DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_MC810,
1513
		&af9015_props, "KWorld Digital MC-810", NULL) },
1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533
	{ DVB_USB_DEVICE(USB_VID_KYE, USB_PID_GENIUS_TVGO_DVB_T03,
		&af9015_props, "Genius TVGo DVB-T03", NULL) },
	{ DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_399U_2,
		&af9015_props, "KWorld PlusTV Dual DVB-T Stick (DVB-T 399U)", NULL) },
	{ DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_PC160_T,
		&af9015_props, "KWorld PlusTV DVB-T PCI Pro Card (DVB-T PC160-T)", NULL) },
	{ DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV20,
		&af9015_props, "Sveon STV20 Tuner USB DVB-T HDTV", NULL) },
	{ DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_TINYTWIN_2,
		&af9015_props, "DigitalNow TinyTwin v2", RC_MAP_DIGITALNOW_TINYTWIN) },
	{ DVB_USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV2000DS,
		&af9015_props, "Leadtek WinFast DTV2000DS", RC_MAP_LEADTEK_Y04G0051) },
	{ DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_UB383_T,
		&af9015_props, "KWorld USB DVB-T Stick Mobile (UB383-T)", NULL) },
	{ DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_4,
		&af9015_props, "KWorld USB DVB-T TV Stick II (VS-DVB-T 395U)", NULL) },
	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A815M,
		&af9015_props, "AverMedia AVerTV Volar M (A815Mac)", NULL) },
	{ DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK_RC,
		&af9015_props, "TerraTec Cinergy T Stick RC", RC_MAP_TERRATEC_SLIM_2) },
1534
	/* XXX: that same ID [0ccd:0099] is used by af9035 driver too */
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
	{ DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK_DUAL_RC,
		&af9015_props, "TerraTec Cinergy T Stick Dual RC", RC_MAP_TERRATEC_SLIM) },
	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A850T,
		&af9015_props, "AverMedia AVerTV Red HD+ (A850T)", NULL) },
	{ DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_TINYTWIN_3,
		&af9015_props, "DigitalNow TinyTwin v3", RC_MAP_DIGITALNOW_TINYTWIN) },
	{ DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV22,
		&af9015_props, "Sveon STV22 Dual USB DVB-T Tuner HDTV", RC_MAP_MSI_DIGIVOX_III) },
	{ }
};
MODULE_DEVICE_TABLE(usb, af9015_id_table);

1547 1548
/* usb specific object needed to register this driver with the usb subsystem */
static struct usb_driver af9015_usb_driver = {
1549
	.name = KBUILD_MODNAME,
1550
	.id_table = af9015_id_table,
1551
	.probe = dvb_usbv2_probe,
1552
	.disconnect = dvb_usbv2_disconnect,
A
Antti Palosaari 已提交
1553 1554
	.suspend = dvb_usbv2_suspend,
	.resume = dvb_usbv2_resume,
1555
	.reset_resume = dvb_usbv2_reset_resume,
1556
	.no_dynamic_id = 1,
1557
	.soft_unbind = 1,
1558 1559
};

1560
module_usb_driver(af9015_usb_driver);
1561 1562

MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1563
MODULE_DESCRIPTION("Afatech AF9015 driver");
1564
MODULE_LICENSE("GPL");
1565
MODULE_FIRMWARE(AF9015_FIRMWARE);