af9015.c 39.1 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
/*
 * 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.
 *
 *    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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include "af9015.h"

26
static int dvb_usb_af9015_debug;
27 28
module_param_named(debug, dvb_usb_af9015_debug, int, 0644);
MODULE_PARM_DESC(debug, "set debugging level" DVB_USB_DEBUG_STATUS);
29
static int dvb_usb_af9015_remote;
30 31 32 33
module_param_named(remote, dvb_usb_af9015_remote, int, 0644);
MODULE_PARM_DESC(remote, "select remote");
DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);

34
static int af9015_ctrl_msg(struct dvb_usb_device *d, struct req_t *req)
35
{
36 37 38
#define BUF_LEN 63
#define REQ_HDR_LEN 8 /* send header size */
#define ACK_HDR_LEN 2 /* rece header size */
39
	struct af9015_state *state = d_to_priv(d);
40
	int ret, wlen, rlen;
41
	u8 buf[BUF_LEN];
42 43 44
	u8 write = 1;

	buf[0] = req->cmd;
45
	buf[1] = state->seq++;
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
	buf[2] = req->i2c_addr;
	buf[3] = req->addr >> 8;
	buf[4] = req->addr & 0xff;
	buf[5] = req->mbox;
	buf[6] = req->addr_len;
	buf[7] = req->data_len;

	switch (req->cmd) {
	case GET_CONFIG:
	case READ_MEMORY:
	case RECONNECT_USB:
		write = 0;
		break;
	case READ_I2C:
		write = 0;
		buf[2] |= 0x01; /* set I2C direction */
	case WRITE_I2C:
		buf[0] = READ_WRITE_I2C;
		break;
	case WRITE_MEMORY:
		if (((req->addr & 0xff00) == 0xff00) ||
67
		    ((req->addr & 0xff00) == 0xae00))
68 69 70 71
			buf[0] = WRITE_VIRTUAL_MEMORY;
	case WRITE_VIRTUAL_MEMORY:
	case COPY_FIRMWARE:
	case DOWNLOAD_FIRMWARE:
72
	case BOOT:
73 74 75 76
		break;
	default:
		err("unknown command:%d", req->cmd);
		ret = -1;
77
		goto error;
78 79
	}

80 81 82 83 84
	/* buffer overflow check */
	if ((write && (req->data_len > BUF_LEN - REQ_HDR_LEN)) ||
		(!write && (req->data_len > BUF_LEN - ACK_HDR_LEN))) {
		err("too much data; cmd:%d len:%d", req->cmd, req->data_len);
		ret = -EINVAL;
85
		goto error;
86 87
	}

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

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

103
	ret = dvb_usbv2_generic_rw(d, buf, wlen, buf, rlen);
104 105
	if (ret)
		goto error;
106 107

	/* check status */
108
	if (rlen && buf[1]) {
109 110
		err("command failed:%d", buf[1]);
		ret = -1;
111
		goto error;
112 113 114 115
	}

	/* read request, copy returned data to return buf */
	if (!write)
116
		memcpy(req->data, &buf[ACK_HDR_LEN], req->data_len);
117
error:
118 119 120 121 122 123 124 125 126 127 128
	return ret;
}

static int af9015_write_regs(struct dvb_usb_device *d, u16 addr, u8 *val,
	u8 len)
{
	struct req_t req = {WRITE_MEMORY, AF9015_I2C_DEMOD, addr, 0, 0, len,
		val};
	return af9015_ctrl_msg(d, &req);
}

129
static int af9015_read_regs(struct dvb_usb_device *d, u16 addr, u8 *val, u8 len)
130
{
131 132
	struct req_t req = {READ_MEMORY, AF9015_I2C_DEMOD, addr, 0, 0, len,
		val};
133 134 135
	return af9015_ctrl_msg(d, &req);
}

136 137 138 139 140
static int af9015_write_reg(struct dvb_usb_device *d, u16 addr, u8 val)
{
	return af9015_write_regs(d, addr, &val, 1);
}

141 142 143 144 145
static int af9015_read_reg(struct dvb_usb_device *d, u16 addr, u8 *val)
{
	return af9015_read_regs(d, addr, val, 1);
}

146 147 148
static int af9015_write_reg_i2c(struct dvb_usb_device *d, u8 addr, u16 reg,
	u8 val)
{
149
	struct af9015_state *state = d_to_priv(d);
150 151
	struct req_t req = {WRITE_I2C, addr, reg, 1, 1, 1, &val};

152 153
	if (addr == state->af9013_config[0].i2c_addr ||
	    addr == state->af9013_config[1].i2c_addr)
154 155 156 157 158 159 160 161
		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,
	u8 *val)
{
162
	struct af9015_state *state = d_to_priv(d);
163 164
	struct req_t req = {READ_I2C, addr, reg, 0, 1, 1, val};

165 166
	if (addr == state->af9013_config[0].i2c_addr ||
	    addr == state->af9013_config[1].i2c_addr)
167 168 169 170 171
		req.addr_len = 3;

	return af9015_ctrl_msg(d, &req);
}

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
static int af9015_do_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit, u8 op)
{
	int ret;
	u8 val, mask = 0x01;

	ret = af9015_read_reg(d, addr, &val);
	if (ret)
		return ret;

	mask <<= bit;
	if (op) {
		/* set bit */
		val |= mask;
	} else {
		/* clear bit */
		mask ^= 0xff;
		val &= mask;
	}

	return af9015_write_reg(d, addr, val);
}

static int af9015_set_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit)
{
	return af9015_do_reg_bit(d, addr, bit, 1);
}

static int af9015_clear_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit)
{
	return af9015_do_reg_bit(d, addr, bit, 0);
}

204 205 206 207
static int af9015_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
	int num)
{
	struct dvb_usb_device *d = i2c_get_adapdata(adap);
208
	struct af9015_state *state = d_to_priv(d);
209 210
	int ret = 0, i = 0;
	u16 addr;
211
	u8 uninitialized_var(mbox), addr_len;
212 213
	struct req_t req;

214
/*
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
The bus lock is needed because there is two tuners both using same I2C-address.
Due to that the only way to select correct tuner is use demodulator I2C-gate.

................................................
. AF9015 includes integrated AF9013 demodulator.
. ____________                   ____________  .                ____________
.|     uC     |                 |   demod    | .               |    tuner   |
.|------------|                 |------------| .               |------------|
.|   AF9015   |                 |  AF9013/5  | .               |   MXL5003  |
.|            |--+----I2C-------|-----/ -----|-.-----I2C-------|            |
.|            |  |              | addr 0x38  | .               |  addr 0xc6 |
.|____________|  |              |____________| .               |____________|
.................|..............................
		 |               ____________                   ____________
		 |              |   demod    |                 |    tuner   |
		 |              |------------|                 |------------|
		 |              |   AF9013   |                 |   MXL5003  |
		 +----I2C-------|-----/ -----|-------I2C-------|            |
				| addr 0x3a  |                 |  addr 0xc6 |
				|____________|                 |____________|
*/
	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
		return -EAGAIN;

	while (i < num) {
240 241
		if (msg[i].addr == state->af9013_config[0].i2c_addr ||
		    msg[i].addr == state->af9013_config[1].i2c_addr) {
242 243 244 245 246 247 248
			addr = msg[i].buf[0] << 8;
			addr += msg[i].buf[1];
			mbox = msg[i].buf[2];
			addr_len = 3;
		} else {
			addr = msg[i].buf[0];
			addr_len = 1;
249
			/* mbox is don't care in that case */
250 251 252
		}

		if (num > i + 1 && (msg[i+1].flags & I2C_M_RD)) {
253 254 255 256
			if (msg[i].len > 3 || msg[i+1].len > 61) {
				ret = -EOPNOTSUPP;
				goto error;
			}
257
			if (msg[i].addr == state->af9013_config[0].i2c_addr)
258 259 260 261 262 263 264 265 266 267 268
				req.cmd = READ_MEMORY;
			else
				req.cmd = READ_I2C;
			req.i2c_addr = msg[i].addr;
			req.addr = addr;
			req.mbox = mbox;
			req.addr_len = addr_len;
			req.data_len = msg[i+1].len;
			req.data = &msg[i+1].buf[0];
			ret = af9015_ctrl_msg(d, &req);
			i += 2;
269
		} else if (msg[i].flags & I2C_M_RD) {
270 271 272 273
			if (msg[i].len > 61) {
				ret = -EOPNOTSUPP;
				goto error;
			}
274
			if (msg[i].addr == state->af9013_config[0].i2c_addr) {
275
				ret = -EINVAL;
276
				goto error;
277 278
			}
			req.cmd = READ_I2C;
279 280 281 282 283 284 285 286
			req.i2c_addr = msg[i].addr;
			req.addr = addr;
			req.mbox = mbox;
			req.addr_len = addr_len;
			req.data_len = msg[i].len;
			req.data = &msg[i].buf[0];
			ret = af9015_ctrl_msg(d, &req);
			i += 1;
287
		} else {
288 289 290 291
			if (msg[i].len > 21) {
				ret = -EOPNOTSUPP;
				goto error;
			}
292
			if (msg[i].addr == state->af9013_config[0].i2c_addr)
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
				req.cmd = WRITE_MEMORY;
			else
				req.cmd = WRITE_I2C;
			req.i2c_addr = msg[i].addr;
			req.addr = addr;
			req.mbox = mbox;
			req.addr_len = addr_len;
			req.data_len = msg[i].len-addr_len;
			req.data = &msg[i].buf[addr_len];
			ret = af9015_ctrl_msg(d, &req);
			i += 1;
		}
		if (ret)
			goto error;

	}
	ret = i;

error:
	mutex_unlock(&d->i2c_mutex);

	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,
};

327
static int af9015_identify_state(struct dvb_usb_device *d, const char **name)
328 329
{
	int ret;
330 331
	u8 reply;
	struct req_t req = {GET_CONFIG, 0, 0, 0, 0, 1, &reply};
332

333
	ret = af9015_ctrl_msg(d, &req);
334 335 336
	if (ret)
		return ret;

337 338 339 340 341
	deb_info("%s: reply:%02x\n", __func__, reply);
	if (reply == 0x02)
		ret = WARM;
	else
		ret = COLD;
342

343
	return ret;
344 345
}

346 347
static int af9015_download_firmware(struct dvb_usb_device *d,
	const struct firmware *fw)
348
{
349
	struct af9015_state *state = d_to_priv(d);
350 351 352
	int i, len, remaining, ret;
	struct req_t req = {DOWNLOAD_FIRMWARE, 0, 0, 0, 0, 0, NULL};
	u16 checksum = 0;
353

354
	deb_info("%s:\n", __func__);
355

356 357 358
	/* calc checksum */
	for (i = 0; i < fw->size; i++)
		checksum += fw->data[i];
359

360 361
	state->firmware_size = fw->size;
	state->firmware_checksum = checksum;
362

363 364 365 366 367 368
	#define FW_ADDR 0x5100 /* firmware start address */
	#define LEN_MAX 55 /* max packet size */
	for (remaining = fw->size; remaining > 0; remaining -= LEN_MAX) {
		len = remaining;
		if (len > LEN_MAX)
			len = LEN_MAX;
369

370 371 372
		req.data_len = len;
		req.data = (u8 *) &fw->data[fw->size - remaining];
		req.addr = FW_ADDR + fw->size - remaining;
373

374 375 376 377 378
		ret = af9015_ctrl_msg(d, &req);
		if (ret) {
			err("firmware download failed:%d", ret);
			goto error;
		}
379 380
	}

381 382 383 384 385 386
	/* firmware loaded, request boot */
	req.cmd = BOOT;
	req.data_len = 0;
	ret = af9015_ctrl_msg(d, &req);
	if (ret) {
		err("firmware boot failed:%d", ret);
387 388
		goto error;
	}
389 390 391 392 393 394 395 396

error:
	return ret;
}

/* hash (and dump) eeprom */
static int af9015_eeprom_hash(struct dvb_usb_device *d)
{
397
	struct af9015_state *state = d_to_priv(d);
398 399 400 401 402 403 404 405 406 407 408 409 410
	int ret;
	static const unsigned int eeprom_size = 256;
	unsigned int reg;
	u8 val, *eeprom;
	struct req_t req = {READ_I2C, AF9015_I2C_EEPROM, 0, 0, 1, 1, &val};

	eeprom = kmalloc(eeprom_size, GFP_KERNEL);
	if (eeprom == NULL)
		return -ENOMEM;

	for (reg = 0; reg < eeprom_size; reg++) {
		req.addr = reg;
		ret = af9015_ctrl_msg(d, &req);
411
		if (ret)
412 413 414
			goto free;

		eeprom[reg] = val;
415
	}
416 417 418 419 420 421 422 423 424 425 426

	if (dvb_usb_af9015_debug & 0x01)
		print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, eeprom,
				eeprom_size);

	BUG_ON(eeprom_size % 4);

	state->eeprom_sum = 0;
	for (reg = 0; reg < eeprom_size / sizeof(u32); reg++) {
		state->eeprom_sum *= GOLDEN_RATIO_PRIME_32;
		state->eeprom_sum += le32_to_cpu(((u32 *)eeprom)[reg]);
427 428
	}

429
	deb_info("%s: eeprom sum=%.8x\n", __func__, state->eeprom_sum);
430

431 432 433
	ret = 0;
free:
	kfree(eeprom);
434 435 436
	return ret;
}

437
static int af9015_read_config(struct dvb_usb_device *d)
438
{
439
	struct af9015_state *state = d_to_priv(d);
440
	int ret;
441 442
	u8 val, i, offset = 0;
	struct req_t req = {READ_I2C, AF9015_I2C_EEPROM, 0, 0, 1, 1, &val};
443

444
	deb_info("%s:\n", __func__);
445

446 447 448 449 450 451 452 453 454 455
	/* 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;
456

457
	ret = af9015_eeprom_hash(d);
458 459 460
	if (ret)
		goto error;

461 462
	deb_info("%s: IR mode=%d\n", __func__, val);
	state->ir_mode = val;
463

464 465
	/* TS mode - one or two receivers */
	req.addr = AF9015_EEPROM_TS_MODE;
466 467 468
	ret = af9015_ctrl_msg(d, &req);
	if (ret)
		goto error;
469

470 471
	state->dual_mode = val;
	deb_info("%s: TS mode=%d\n", __func__, state->dual_mode);
472

473 474 475
	/* disable 2nd adapter because we don't have PID-filters */
	if (d->udev->speed == USB_SPEED_FULL)
		state->dual_mode = 0;
476

477
	if (state->dual_mode) {
478 479
		/* read 2nd demodulator I2C address */
		req.addr = AF9015_EEPROM_DEMOD2_I2C;
480
		ret = af9015_ctrl_msg(d, &req);
481 482 483
		if (ret)
			goto error;

484
		state->af9013_config[1].i2c_addr = val;
485 486
	}

487
	for (i = 0; i < state->dual_mode + 1; i++) {
488 489 490 491
		if (i == 1)
			offset = AF9015_EEPROM_OFFSET;
		/* xtal */
		req.addr = AF9015_EEPROM_XTAL_TYPE1 + offset;
492
		ret = af9015_ctrl_msg(d, &req);
493 494 495 496
		if (ret)
			goto error;
		switch (val) {
		case 0:
497
			state->af9013_config[i].clock = 28800000;
498 499
			break;
		case 1:
500
			state->af9013_config[i].clock = 20480000;
501 502
			break;
		case 2:
503
			state->af9013_config[i].clock = 28000000;
504 505
			break;
		case 3:
506
			state->af9013_config[i].clock = 25000000;
507 508
			break;
		};
509
		deb_info("%s: [%d] xtal=%d set clock=%d\n", __func__, i,
510
				val, state->af9013_config[i].clock);
511

512
		/* IF frequency */
513
		req.addr = AF9015_EEPROM_IF1H + offset;
514
		ret = af9015_ctrl_msg(d, &req);
515 516
		if (ret)
			goto error;
517

518
		state->af9013_config[i].if_frequency = val << 8;
519

520
		req.addr = AF9015_EEPROM_IF1L + offset;
521
		ret = af9015_ctrl_msg(d, &req);
522 523
		if (ret)
			goto error;
524

525 526
		state->af9013_config[i].if_frequency += val;
		state->af9013_config[i].if_frequency *= 1000;
527
		deb_info("%s: [%d] IF frequency=%d\n", __func__, i,
528
				state->af9013_config[i].if_frequency);
529 530 531

		/* MT2060 IF1 */
		req.addr = AF9015_EEPROM_MT2060_IF1H  + offset;
532
		ret = af9015_ctrl_msg(d, &req);
533 534
		if (ret)
			goto error;
535
		state->mt2060_if1[i] = val << 8;
536
		req.addr = AF9015_EEPROM_MT2060_IF1L + offset;
537
		ret = af9015_ctrl_msg(d, &req);
538 539
		if (ret)
			goto error;
540
		state->mt2060_if1[i] += val;
541
		deb_info("%s: [%d] MT2060 IF1=%d\n", __func__, i,
542
				state->mt2060_if1[i]);
543 544 545

		/* tuner */
		req.addr =  AF9015_EEPROM_TUNER_ID1 + offset;
546
		ret = af9015_ctrl_msg(d, &req);
547 548 549 550 551 552 553 554 555 556
		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:
557
		case AF9013_TUNER_TDA18218:
558
			state->af9013_config[i].spec_inv = 1;
559 560 561 562
			break;
		case AF9013_TUNER_MXL5003D:
		case AF9013_TUNER_MXL5005D:
		case AF9013_TUNER_MXL5005R:
563
		case AF9013_TUNER_MXL5007T:
564
			state->af9013_config[i].spec_inv = 0;
565
			break;
566
		case AF9013_TUNER_MC44S803:
567 568
			state->af9013_config[i].gpio[1] = AF9013_GPIO_LO;
			state->af9013_config[i].spec_inv = 1;
569
			break;
570
		default:
571
			warn("tuner id=%d not supported, please report!", val);
572 573 574
			return -ENODEV;
		};

575
		state->af9013_config[i].tuner = val;
576
		deb_info("%s: [%d] tuner id=%d\n", __func__, i, val);
577 578 579 580
	}

error:
	if (ret)
581
		err("eeprom read failed=%d", ret);
582

583
	/* AverMedia AVerTV Volar Black HD (A850) device have bad EEPROM
584 585
	   content :-( Override some wrong values here. Ditto for the
	   AVerTV Red HD+ (A850T) device. */
586 587
	if (le16_to_cpu(d->udev->descriptor.idVendor) == USB_VID_AVERMEDIA &&
		((le16_to_cpu(d->udev->descriptor.idProduct) ==
588
			USB_PID_AVERMEDIA_A850) ||
589
		(le16_to_cpu(d->udev->descriptor.idProduct) ==
590
			USB_PID_AVERMEDIA_A850T))) {
591 592
		deb_info("%s: AverMedia A850: overriding config\n", __func__);
		/* disable dual mode */
593
		state->dual_mode = 0;
594 595

		/* set correct IF */
596
		state->af9013_config[0].if_frequency = 4570000;
597 598
	}

599 600 601
	return ret;
}

602
static int af9015_get_stream_config(struct dvb_frontend *fe, u8 *ts_type,
603
		struct usb_data_stream_properties *stream)
604
{
605
	deb_info("%s: adap=%d\n", __func__, fe_to_adap(fe)->id);
606

607
	if (fe_to_d(fe)->udev->speed == USB_SPEED_FULL)
608
		stream->u.bulk.buffersize = TS_USB11_FRAME_SIZE;
609

610 611
	return 0;
}
612

613 614
static int af9015_get_adapter_count(struct dvb_usb_device *d)
{
615
	struct af9015_state *state = d_to_priv(d);
616
	return state->dual_mode + 1;
617 618
}

619
/* override demod callbacks for resource locking */
620
static int af9015_af9013_set_frontend(struct dvb_frontend *fe)
621 622
{
	int ret;
623
	struct af9015_state *state = fe_to_priv(fe);
624

625
	if (mutex_lock_interruptible(&state->fe_mutex))
626 627
		return -EAGAIN;

628
	ret = state->set_frontend[fe_to_adap(fe)->id](fe);
629

630
	mutex_unlock(&state->fe_mutex);
631 632 633 634 635 636 637 638 639

	return ret;
}

/* override demod callbacks for resource locking */
static int af9015_af9013_read_status(struct dvb_frontend *fe,
	fe_status_t *status)
{
	int ret;
640
	struct af9015_state *state = fe_to_priv(fe);
641

642
	if (mutex_lock_interruptible(&state->fe_mutex))
643 644
		return -EAGAIN;

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

647
	mutex_unlock(&state->fe_mutex);
648 649 650 651 652 653 654 655

	return ret;
}

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

658
	if (mutex_lock_interruptible(&state->fe_mutex))
659 660
		return -EAGAIN;

661
	ret = state->init[fe_to_adap(fe)->id](fe);
662

663
	mutex_unlock(&state->fe_mutex);
664 665 666 667 668 669 670 671

	return ret;
}

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

674
	if (mutex_lock_interruptible(&state->fe_mutex))
675 676
		return -EAGAIN;

677
	ret = state->sleep[fe_to_adap(fe)->id](fe);
678

679
	mutex_unlock(&state->fe_mutex);
680 681 682 683

	return ret;
}

684 685 686 687
/* override tuner callbacks for resource locking */
static int af9015_tuner_init(struct dvb_frontend *fe)
{
	int ret;
688
	struct af9015_state *state = fe_to_priv(fe);
689

690
	if (mutex_lock_interruptible(&state->fe_mutex))
691 692
		return -EAGAIN;

693
	ret = state->tuner_init[fe_to_adap(fe)->id](fe);
694

695
	mutex_unlock(&state->fe_mutex);
696 697 698 699 700 701 702 703

	return ret;
}

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

706
	if (mutex_lock_interruptible(&state->fe_mutex))
707 708
		return -EAGAIN;

709
	ret = state->tuner_sleep[fe_to_adap(fe)->id](fe);
710 711 712 713 714 715 716 717

	mutex_unlock(&state->fe_mutex);

	return ret;
}

static int af9015_copy_firmware(struct dvb_usb_device *d)
{
718
	struct af9015_state *state = d_to_priv(d);
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 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
	int ret;
	u8 fw_params[4];
	u8 val, i;
	struct req_t req = {COPY_FIRMWARE, 0, 0x5100, 0, 0, sizeof(fw_params),
		fw_params };
	deb_info("%s:\n", __func__);

	fw_params[0] = state->firmware_size >> 8;
	fw_params[1] = state->firmware_size & 0xff;
	fw_params[2] = state->firmware_checksum >> 8;
	fw_params[3] = state->firmware_checksum & 0xff;

	/* wait 2nd demodulator ready */
	msleep(100);

	ret = af9015_read_reg_i2c(d, state->af9013_config[1].i2c_addr,
			0x98be, &val);
	if (ret)
		goto error;
	else
		deb_info("%s: firmware status:%02x\n", __func__, val);

	if (val == 0x0c) /* fw is running, no need for download */
		goto exit;

	/* set I2C master clock to fast (to speed up firmware copy) */
	ret = af9015_write_reg(d, 0xd416, 0x04); /* 0x04 * 400ns */
	if (ret)
		goto error;

	msleep(50);

	/* copy firmware */
	ret = af9015_ctrl_msg(d, &req);
	if (ret)
		err("firmware copy cmd failed:%d", ret);
	deb_info("%s: firmware copy done\n", __func__);

	/* set I2C master clock back to normal */
	ret = af9015_write_reg(d, 0xd416, 0x14); /* 0x14 * 400ns */
	if (ret)
		goto error;

	/* request boot firmware */
	ret = af9015_write_reg_i2c(d, state->af9013_config[1].i2c_addr,
			0xe205, 1);
	deb_info("%s: firmware boot cmd status:%d\n", __func__, ret);
	if (ret)
		goto error;

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

772 773 774 775 776 777 778
		/* check firmware status */
		ret = af9015_read_reg_i2c(d, state->af9013_config[1].i2c_addr,
				0x98be, &val);
		deb_info("%s: firmware status cmd status:%d fw status:%02x\n",
			__func__, ret, val);
		if (ret)
			goto error;
779

780 781 782 783 784 785 786 787 788 789 790 791 792 793
		if (val == 0x0c || val == 0x04) /* success or fail */
			break;
	}

	if (val == 0x04) {
		err("firmware did not run");
		ret = -1;
	} else if (val != 0x0c) {
		err("firmware boot timeout");
		ret = -1;
	}

error:
exit:
794 795 796
	return ret;
}

797 798 799
static int af9015_af9013_frontend_attach(struct dvb_usb_adapter *adap)
{
	int ret;
800
	struct af9015_state *state = adap_to_priv(adap);
801

802 803 804 805 806 807 808 809 810 811 812
	if (adap->id == 0) {
		state->af9013_config[0].ts_mode = AF9013_TS_USB;
		memcpy(state->af9013_config[0].api_version, "\x0\x1\x9\x0", 4);
		state->af9013_config[0].gpio[0] = AF9013_GPIO_HI;
		state->af9013_config[0].gpio[3] = AF9013_GPIO_TUNER_ON;
	} else if (adap->id == 1) {
		state->af9013_config[1].ts_mode = AF9013_TS_SERIAL;
		memcpy(state->af9013_config[1].api_version, "\x0\x1\x9\x0", 4);
		state->af9013_config[1].gpio[0] = AF9013_GPIO_TUNER_ON;
		state->af9013_config[1].gpio[1] = AF9013_GPIO_LO;

813
		/* copy firmware to 2nd demodulator */
814
		if (state->dual_mode) {
815
			ret = af9015_copy_firmware(adap_to_d(adap));
816 817 818
			if (ret) {
				err("firmware copy to 2nd frontend " \
					"failed, will disable it");
819
				state->dual_mode = 0;
820 821 822 823 824 825 826 827
				return -ENODEV;
			}
		} else {
			return -ENODEV;
		}
	}

	/* attach demodulator */
828
	adap->fe[0] = dvb_attach(af9013_attach,
829
		&state->af9013_config[adap->id], &adap_to_d(adap)->i2c_adap);
830

831 832 833 834 835 836 837
	/*
	 * 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.
	 */
838
	if (adap->fe[0]) {
839
		state->set_frontend[adap->id] =
840 841
			adap->fe[0]->ops.set_frontend;
		adap->fe[0]->ops.set_frontend =
842 843 844
			af9015_af9013_set_frontend;

		state->read_status[adap->id] =
845 846
			adap->fe[0]->ops.read_status;
		adap->fe[0]->ops.read_status =
847 848
			af9015_af9013_read_status;

849 850
		state->init[adap->id] = adap->fe[0]->ops.init;
		adap->fe[0]->ops.init = af9015_af9013_init;
851

852 853
		state->sleep[adap->id] = adap->fe[0]->ops.sleep;
		adap->fe[0]->ops.sleep = af9015_af9013_sleep;
854 855
	}

856
	return adap->fe[0] == NULL ? -ENODEV : 0;
857 858 859 860 861 862 863 864 865 866 867 868 869
}

static struct mt2060_config af9015_mt2060_config = {
	.i2c_address = 0xc0,
	.clock_out = 0,
};

static struct qt1010_config af9015_qt1010_config = {
	.i2c_address = 0xc4,
};

static struct tda18271_config af9015_tda18271_config = {
	.gate = TDA18271_GATE_DIGITAL,
870
	.small_i2c = TDA18271_16_BYTE_CHUNK_INIT,
871 872 873 874 875 876 877 878
};

static struct mxl5005s_config af9015_mxl5003_config = {
	.i2c_address     = 0xc6,
	.if_freq         = IF_FREQ_4570000HZ,
	.xtal_freq       = CRYSTAL_FREQ_16000000HZ,
	.agc_mode        = MXL_SINGLE_AGC,
	.tracking_filter = MXL_TF_DEFAULT,
879
	.rssi_enable     = MXL_RSSI_ENABLE,
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
	.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 = {
	.i2c_address     = 0xc6,
	.if_freq         = IF_FREQ_4570000HZ,
	.xtal_freq       = CRYSTAL_FREQ_16000000HZ,
	.agc_mode        = MXL_SINGLE_AGC,
	.tracking_filter = MXL_TF_OFF,
896
	.rssi_enable     = MXL_RSSI_ENABLE,
897 898 899 900 901 902 903 904 905 906
	.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,
};

907 908 909 910 911
static struct mc44s803_config af9015_mc44s803_config = {
	.i2c_address = 0xc0,
	.dig_out = 1,
};

912 913 914 915 916
static struct tda18218_config af9015_tda18218_config = {
	.i2c_address = 0xc0,
	.i2c_wr_max = 21, /* max wr bytes AF9015 I2C adap can handle at once */
};

917 918 919 920 921
static struct mxl5007t_config af9015_mxl5007t_config = {
	.xtal_freq_hz = MxL_XTAL_24_MHZ,
	.if_freq_hz = MxL_IF_4_57_MHZ,
};

922 923
static int af9015_tuner_attach(struct dvb_usb_adapter *adap)
{
924
	struct af9015_state *state = adap_to_priv(adap);
925
	int ret;
926
	deb_info("%s:\n", __func__);
927

928
	switch (state->af9013_config[adap->id].tuner) {
929 930
	case AF9013_TUNER_MT2060:
	case AF9013_TUNER_MT2060_2:
931
		ret = dvb_attach(mt2060_attach, adap->fe[0],
932
			&adap_to_d(adap)->i2c_adap, &af9015_mt2060_config,
933
			state->mt2060_if1[adap->id])
934 935 936 937
			== NULL ? -ENODEV : 0;
		break;
	case AF9013_TUNER_QT1010:
	case AF9013_TUNER_QT1010A:
938
		ret = dvb_attach(qt1010_attach, adap->fe[0],
939
			&adap_to_d(adap)->i2c_adap,
940 941 942
			&af9015_qt1010_config) == NULL ? -ENODEV : 0;
		break;
	case AF9013_TUNER_TDA18271:
943
		ret = dvb_attach(tda18271_attach, adap->fe[0], 0xc0,
944
			&adap_to_d(adap)->i2c_adap,
945 946
			&af9015_tda18271_config) == NULL ? -ENODEV : 0;
		break;
947
	case AF9013_TUNER_TDA18218:
948
		ret = dvb_attach(tda18218_attach, adap->fe[0],
949
			&adap_to_d(adap)->i2c_adap,
950 951
			&af9015_tda18218_config) == NULL ? -ENODEV : 0;
		break;
952
	case AF9013_TUNER_MXL5003D:
953
		ret = dvb_attach(mxl5005s_attach, adap->fe[0],
954
			&adap_to_d(adap)->i2c_adap,
955 956 957 958
			&af9015_mxl5003_config) == NULL ? -ENODEV : 0;
		break;
	case AF9013_TUNER_MXL5005D:
	case AF9013_TUNER_MXL5005R:
959
		ret = dvb_attach(mxl5005s_attach, adap->fe[0],
960
			&adap_to_d(adap)->i2c_adap,
961 962 963
			&af9015_mxl5005_config) == NULL ? -ENODEV : 0;
		break;
	case AF9013_TUNER_ENV77H11D5:
964
		ret = dvb_attach(dvb_pll_attach, adap->fe[0], 0xc0,
965
			&adap_to_d(adap)->i2c_adap,
966 967 968
			DVB_PLL_TDA665X) == NULL ? -ENODEV : 0;
		break;
	case AF9013_TUNER_MC44S803:
969
		ret = dvb_attach(mc44s803_attach, adap->fe[0],
970
			&adap_to_d(adap)->i2c_adap,
971
			&af9015_mc44s803_config) == NULL ? -ENODEV : 0;
972
		break;
973
	case AF9013_TUNER_MXL5007T:
974
		ret = dvb_attach(mxl5007t_attach, adap->fe[0],
975
			&adap_to_d(adap)->i2c_adap,
976 977
			0xc0, &af9015_mxl5007t_config) == NULL ? -ENODEV : 0;
		break;
978 979 980 981
	case AF9013_TUNER_UNKNOWN:
	default:
		ret = -ENODEV;
		err("Unknown tuner id:%d",
982
			state->af9013_config[adap->id].tuner);
983
	}
984

985
	if (adap->fe[0]->ops.tuner_ops.init) {
986
		state->tuner_init[adap->id] =
987 988
			adap->fe[0]->ops.tuner_ops.init;
		adap->fe[0]->ops.tuner_ops.init = af9015_tuner_init;
989 990
	}

991
	if (adap->fe[0]->ops.tuner_ops.sleep) {
992
		state->tuner_sleep[adap->id] =
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
			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)
{
	int ret;
	deb_info("%s: onoff:%d\n", __func__, onoff);

	if (onoff)
1006
		ret = af9015_set_reg_bit(adap_to_d(adap), 0xd503, 0);
1007
	else
1008
		ret = af9015_clear_reg_bit(adap_to_d(adap), 0xd503, 0);
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021

	return ret;
}

static int af9015_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
	int onoff)
{
	int ret;
	u8 idx;

	deb_info("%s: set pid filter, index %d, pid %x, onoff %d\n",
		__func__, index, pid, onoff);

1022
	ret = af9015_write_reg(adap_to_d(adap), 0xd505, (pid & 0xff));
1023 1024 1025
	if (ret)
		goto error;

1026
	ret = af9015_write_reg(adap_to_d(adap), 0xd506, (pid >> 8));
1027 1028 1029 1030
	if (ret)
		goto error;

	idx = ((index & 0x1f) | (1 << 5));
1031
	ret = af9015_write_reg(adap_to_d(adap), 0xd504, idx);
1032 1033 1034 1035 1036 1037 1038

error:
	return ret;
}

static int af9015_init_endpoint(struct dvb_usb_device *d)
{
1039
	struct af9015_state *state = d_to_priv(d);
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
	int ret;
	u16 frame_size;
	u8  packet_size;
	deb_info("%s: USB speed:%d\n", __func__, d->udev->speed);

	if (d->udev->speed == USB_SPEED_FULL) {
		frame_size = TS_USB11_FRAME_SIZE/4;
		packet_size = TS_USB11_MAX_PACKET_SIZE/4;
	} else {
		frame_size = TS_USB20_FRAME_SIZE/4;
		packet_size = TS_USB20_MAX_PACKET_SIZE/4;
	}

	ret = af9015_set_reg_bit(d, 0xd507, 2); /* assert EP4 reset */
	if (ret)
		goto error;
	ret = af9015_set_reg_bit(d, 0xd50b, 1); /* assert EP5 reset */
	if (ret)
		goto error;
	ret = af9015_clear_reg_bit(d, 0xdd11, 5); /* disable EP4 */
	if (ret)
		goto error;
	ret = af9015_clear_reg_bit(d, 0xdd11, 6); /* disable EP5 */
	if (ret)
		goto error;
	ret = af9015_set_reg_bit(d, 0xdd11, 5); /* enable EP4 */
	if (ret)
		goto error;
	if (state->dual_mode) {
		ret = af9015_set_reg_bit(d, 0xdd11, 6); /* enable EP5 */
		if (ret)
			goto error;
	}
	ret = af9015_clear_reg_bit(d, 0xdd13, 5); /* disable EP4 NAK */
	if (ret)
		goto error;
	if (state->dual_mode) {
		ret = af9015_clear_reg_bit(d, 0xdd13, 6); /* disable EP5 NAK */
		if (ret)
			goto error;
	}
	/* EP4 xfer length */
	ret = af9015_write_reg(d, 0xdd88, frame_size & 0xff);
	if (ret)
		goto error;
	ret = af9015_write_reg(d, 0xdd89, frame_size >> 8);
	if (ret)
		goto error;
	/* EP5 xfer length */
	ret = af9015_write_reg(d, 0xdd8a, frame_size & 0xff);
	if (ret)
		goto error;
	ret = af9015_write_reg(d, 0xdd8b, frame_size >> 8);
	if (ret)
		goto error;
	ret = af9015_write_reg(d, 0xdd0c, packet_size); /* EP4 packet size */
	if (ret)
		goto error;
	ret = af9015_write_reg(d, 0xdd0d, packet_size); /* EP5 packet size */
	if (ret)
		goto error;
	ret = af9015_clear_reg_bit(d, 0xd507, 2); /* negate EP4 reset */
	if (ret)
		goto error;
	if (state->dual_mode) {
		ret = af9015_clear_reg_bit(d, 0xd50b, 1); /* negate EP5 reset */
		if (ret)
			goto error;
1108
	}
1109

1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
	/* enable / disable mp2if2 */
	if (state->dual_mode)
		ret = af9015_set_reg_bit(d, 0xd50b, 0);
	else
		ret = af9015_clear_reg_bit(d, 0xd50b, 0);

error:
	if (ret)
		err("endpoint init failed:%d", ret);
	return ret;
}

static int af9015_init(struct dvb_usb_device *d)
{
1124
	struct af9015_state *state = d_to_priv(d);
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
	int ret;
	deb_info("%s:\n", __func__);

	mutex_init(&state->fe_mutex);

	/* init RC canary */
	ret = af9015_write_reg(d, 0x98e9, 0xff);
	if (ret)
		goto error;

	ret = af9015_init_endpoint(d);
	if (ret)
		goto error;

error:
1140 1141 1142
	return ret;
}

1143 1144 1145
struct af9015_rc_setup {
	unsigned int id;
	char *rc_codes;
1146 1147
};

1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
static char *af9015_rc_setup_match(unsigned int id,
	const struct af9015_rc_setup *table)
{
	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 },
1163
	{ }
1164 1165
};

1166 1167 1168 1169 1170 1171 1172
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 */
	{ }
};
1173

1174 1175
static int af9015_rc_query(struct dvb_usb_device *d)
{
1176
	struct af9015_state *state = d_to_priv(d);
1177 1178
	int ret;
	u8 buf[17];
1179

1180
	deb_info("%s:\n", __func__);
1181

1182 1183 1184 1185
	/* read registers needed to detect remote controller code */
	ret = af9015_read_regs(d, 0x98d9, buf, sizeof(buf));
	if (ret)
		goto error;
1186

1187 1188 1189
	/* If any of these are non-zero, assume invalid data */
	if (buf[1] || buf[2] || buf[3])
		return ret;
1190

1191 1192 1193 1194 1195 1196 1197 1198
	/* Check for repeat of previous code */
	if ((state->rc_repeat != buf[6] || buf[0]) &&
			!memcmp(&buf[12], state->rc_last, 4)) {
		deb_rc("%s: key repeated\n", __func__);
		rc_keydown(d->rc_dev, state->rc_keycode, 0);
		state->rc_repeat = buf[6];
		return ret;
	}
1199

1200 1201
	/* Only process key if canary killed */
	if (buf[16] != 0xff && buf[0] != 0x01) {
1202
		deb_rc("%s: key pressed %*ph\n", __func__, 4, buf + 12);
1203

1204 1205 1206 1207
		/* Reset the canary */
		ret = af9015_write_reg(d, 0x98e9, 0xff);
		if (ret)
			goto error;
1208

1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
		/* Remember this key */
		memcpy(state->rc_last, &buf[12], 4);
		if (buf[14] == (u8) ~buf[15]) {
			if (buf[12] == (u8) ~buf[13]) {
				/* NEC */
				state->rc_keycode = buf[12] << 8 | buf[14];
			} else {
				/* NEC extended*/
				state->rc_keycode = buf[12] << 16 |
					buf[13] << 8 | buf[14];
			}
		} else {
			/* 32 bit NEC */
			state->rc_keycode = buf[12] << 24 | buf[13] << 16 |
					buf[14] << 8 | buf[15];
1224
		}
1225 1226 1227 1228 1229 1230 1231 1232 1233
		rc_keydown(d->rc_dev, state->rc_keycode, 0);
	} else {
		deb_rc("%s: no key press\n", __func__);
		/* Invalidate last keypress */
		/* Not really needed, but helps with debug */
		state->rc_last[2] = state->rc_last[3];
	}

	state->rc_repeat = buf[6];
1234
	state->rc_failed = false;
1235 1236

error:
1237
	if (ret) {
1238 1239
		err("%s: failed:%d", __func__, ret);

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

		state->rc_failed = true;
	}

1247 1248
	return ret;
}
1249

1250
static int af9015_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
1251
{
1252
	struct af9015_state *state = d_to_priv(d);
1253 1254 1255 1256 1257 1258
	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 */
1259 1260 1261
	if (!rc->map_name)
		rc->map_name = af9015_rc_setup_match(dvb_usb_af9015_remote,
				af9015_rc_setup_modparam);
1262

1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
	/* try to load remote based eeprom hash */
	if (!rc->map_name)
		rc->map_name = af9015_rc_setup_match(state->eeprom_sum,
				af9015_rc_setup_hashes);

	/* try to load remote based USB iManufacturer string */
	if (!rc->map_name && vid == USB_VID_AFATECH) {
		/* 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. */
		char manufacturer[10];
		memset(manufacturer, 0, sizeof(manufacturer));
		usb_string(d->udev, d->udev->descriptor.iManufacturer,
			manufacturer, sizeof(manufacturer));
		if (!strcmp("MSI", manufacturer)) {
			/* 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);
		}
1285 1286
	}

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

1291 1292 1293 1294 1295
	rc->allowed_protos = RC_TYPE_NEC;
	rc->query = af9015_rc_query;
	rc->interval = 500;

	return 0;
1296 1297
}

1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
/* interface 0 is used by DVB-T receiver and
   interface 1 is for remote controller (HID) */
static struct dvb_usb_device_properties af9015_props = {
	.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,

	.identify_state = af9015_identify_state,
1310
	.firmware = AF9015_FIRMWARE,
1311 1312 1313
	.download_firmware = af9015_download_firmware,

	.i2c_algo = &af9015_i2c_algo,
1314 1315 1316
	.read_config = af9015_read_config,
	.frontend_attach = af9015_af9013_frontend_attach,
	.tuner_attach = af9015_tuner_attach,
1317 1318
	.init = af9015_init,
	.get_rc_config = af9015_get_rc_config,
1319
	.get_stream_config = af9015_get_stream_config,
1320 1321 1322 1323 1324 1325 1326 1327 1328

	.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,
1329 1330 1331 1332

			.stream = DVB_USB_STREAM_BULK(0x84, 8, TS_USB20_FRAME_SIZE),
		}, {
			.stream = DVB_USB_STREAM_BULK(0x85, 8, TS_USB20_FRAME_SIZE),
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 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 1415 1416 1417
		},
	},
};

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,
		&af9015_props, "KWorld Digial MC-810", NULL) },
	{ 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) },
	{ 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);

1418 1419
/* usb specific object needed to register this driver with the usb subsystem */
static struct usb_driver af9015_usb_driver = {
1420
	.name = KBUILD_MODNAME,
1421
	.id_table = af9015_id_table,
1422 1423
	.probe = dvb_usbv2_probe,
	.disconnect = dvb_usbv2_disconnect,
A
Antti Palosaari 已提交
1424 1425
	.suspend = dvb_usbv2_suspend,
	.resume = dvb_usbv2_resume,
1426
	.reset_resume = dvb_usbv2_reset_resume,
1427
	.no_dynamic_id = 1,
1428
	.soft_unbind = 1,
1429 1430
};

1431
module_usb_driver(af9015_usb_driver);
1432 1433

MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1434
MODULE_DESCRIPTION("Afatech AF9015 driver");
1435
MODULE_LICENSE("GPL");
1436
MODULE_FIRMWARE(AF9015_FIRMWARE);