af9015.c 49.8 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
/*
 * 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.
 *
 */

24
#include <linux/hash.h>
25
#include <linux/slab.h>
26

27 28 29 30 31 32
#include "af9015.h"
#include "af9013.h"
#include "mt2060.h"
#include "qt1010.h"
#include "tda18271.h"
#include "mxl5005s.h"
33
#include "mc44s803.h"
34
#include "tda18218.h"
35
#include "mxl5007t.h"
36

37
static int dvb_usb_af9015_debug;
38 39
module_param_named(debug, dvb_usb_af9015_debug, int, 0644);
MODULE_PARM_DESC(debug, "set debugging level" DVB_USB_DEBUG_STATUS);
40
static int dvb_usb_af9015_remote;
41 42 43 44 45 46 47
module_param_named(remote, dvb_usb_af9015_remote, int, 0644);
MODULE_PARM_DESC(remote, "select remote");
DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);

static DEFINE_MUTEX(af9015_usb_mutex);

static struct af9015_config af9015_config;
48
static struct dvb_usb_device_properties af9015_properties[3];
49
static int af9015_properties_count = ARRAY_SIZE(af9015_properties);
50 51 52

static struct af9013_config af9015_af9013_config[] = {
	{
53 54
		.i2c_addr = AF9015_I2C_DEMOD,
		.ts_mode = AF9013_TS_USB,
55 56 57 58 59
		.api_version = { 0, 1, 9, 0 },
		.gpio[0] = AF9013_GPIO_HI,
		.gpio[3] = AF9013_GPIO_TUNER_ON,

	}, {
60
		.ts_mode = AF9013_TS_SERIAL,
61 62 63 64 65 66 67 68
		.api_version = { 0, 1, 9, 0 },
		.gpio[0] = AF9013_GPIO_TUNER_ON,
		.gpio[1] = AF9013_GPIO_LO,
	}
};

static int af9015_rw_udev(struct usb_device *udev, struct req_t *req)
{
69 70 71
#define BUF_LEN 63
#define REQ_HDR_LEN 8 /* send header size */
#define ACK_HDR_LEN 2 /* rece header size */
72
	int act_len, ret;
73
	u8 buf[BUF_LEN];
74
	u8 write = 1;
75
	u8 msg_len = REQ_HDR_LEN;
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
	static u8 seq; /* packet sequence number */

	if (mutex_lock_interruptible(&af9015_usb_mutex) < 0)
		return -EAGAIN;

	buf[0] = req->cmd;
	buf[1] = seq++;
	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) ||
104
		    ((req->addr & 0xff00) == 0xae00))
105 106 107 108
			buf[0] = WRITE_VIRTUAL_MEMORY;
	case WRITE_VIRTUAL_MEMORY:
	case COPY_FIRMWARE:
	case DOWNLOAD_FIRMWARE:
109
	case BOOT:
110 111 112 113 114 115 116
		break;
	default:
		err("unknown command:%d", req->cmd);
		ret = -1;
		goto error_unlock;
	}

117 118 119 120 121 122 123 124
	/* 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;
		goto error_unlock;
	}

125 126
	/* write requested */
	if (write) {
127
		memcpy(&buf[REQ_HDR_LEN], req->data, req->data_len);
128 129
		msg_len += req->data_len;
	}
130

131 132 133 134 135
	deb_xfer(">>> ");
	debug_dump(buf, msg_len, deb_xfer);

	/* send req */
	ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 0x02), buf, msg_len,
136
		&act_len, AF9015_USB_TIMEOUT);
137 138 139 140 141 142 143 144 145 146 147 148
	if (ret)
		err("bulk message failed:%d (%d/%d)", ret, msg_len, act_len);
	else
		if (act_len != msg_len)
			ret = -1; /* all data is not send */
	if (ret)
		goto error_unlock;

	/* no ack for those packets */
	if (req->cmd == DOWNLOAD_FIRMWARE || req->cmd == RECONNECT_USB)
		goto exit_unlock;

149 150 151 152 153 154
	/* write receives seq + status = 2 bytes
	   read receives seq + status + data = 2 + N bytes */
	msg_len = ACK_HDR_LEN;
	if (!write)
		msg_len += req->data_len;

155
	ret = usb_bulk_msg(udev, usb_rcvbulkpipe(udev, 0x81), buf, msg_len,
156
		&act_len, AF9015_USB_TIMEOUT);
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
	if (ret) {
		err("recv bulk message failed:%d", ret);
		ret = -1;
		goto error_unlock;
	}

	deb_xfer("<<< ");
	debug_dump(buf, act_len, deb_xfer);

	/* check status */
	if (buf[1]) {
		err("command failed:%d", buf[1]);
		ret = -1;
		goto error_unlock;
	}

	/* read request, copy returned data to return buf */
	if (!write)
175
		memcpy(req->data, &buf[ACK_HDR_LEN], req->data_len);
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

error_unlock:
exit_unlock:
	mutex_unlock(&af9015_usb_mutex);

	return ret;
}

static int af9015_ctrl_msg(struct dvb_usb_device *d, struct req_t *req)
{
	return af9015_rw_udev(d->udev, req);
}

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

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

202
static int af9015_read_regs(struct dvb_usb_device *d, u16 addr, u8 *val, u8 len)
203
{
204 205
	struct req_t req = {READ_MEMORY, AF9015_I2C_DEMOD, addr, 0, 0, len,
		val};
206 207 208
	return af9015_ctrl_msg(d, &req);
}

209 210 211 212 213
static int af9015_read_reg(struct dvb_usb_device *d, u16 addr, u8 *val)
{
	return af9015_read_regs(d, addr, val, 1);
}

214 215 216 217 218
static int af9015_write_reg_i2c(struct dvb_usb_device *d, u8 addr, u16 reg,
	u8 val)
{
	struct req_t req = {WRITE_I2C, addr, reg, 1, 1, 1, &val};

219 220
	if (addr == af9015_af9013_config[0].i2c_addr ||
	    addr == af9015_af9013_config[1].i2c_addr)
221 222 223 224 225 226 227 228 229 230
		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)
{
	struct req_t req = {READ_I2C, addr, reg, 0, 1, 1, val};

231 232
	if (addr == af9015_af9013_config[0].i2c_addr ||
	    addr == af9015_af9013_config[1].i2c_addr)
233 234 235 236 237 238 239 240 241 242 243
		req.addr_len = 3;

	return af9015_ctrl_msg(d, &req);
}

static int af9015_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
	int num)
{
	struct dvb_usb_device *d = i2c_get_adapdata(adap);
	int ret = 0, i = 0;
	u16 addr;
244
	u8 uninitialized_var(mbox), addr_len;
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
	struct req_t req;

/* TODO: implement bus lock

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) {
274 275
		if (msg[i].addr == af9015_af9013_config[0].i2c_addr ||
		    msg[i].addr == af9015_af9013_config[1].i2c_addr) {
276 277 278 279 280 281 282
			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;
283
			/* mbox is don't care in that case */
284 285 286
		}

		if (num > i + 1 && (msg[i+1].flags & I2C_M_RD)) {
287 288 289 290
			if (msg[i].len > 3 || msg[i+1].len > 61) {
				ret = -EOPNOTSUPP;
				goto error;
			}
291
			if (msg[i].addr == af9015_af9013_config[0].i2c_addr)
292 293 294 295 296 297 298 299 300 301 302
				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;
303
		} else if (msg[i].flags & I2C_M_RD) {
304 305 306 307
			if (msg[i].len > 61) {
				ret = -EOPNOTSUPP;
				goto error;
			}
308
			if (msg[i].addr ==
309
				af9015_af9013_config[0].i2c_addr) {
310
				ret = -EINVAL;
311
				goto error;
312 313
			}
			req.cmd = READ_I2C;
314 315 316 317 318 319 320 321
			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;
322
		} else {
323 324 325 326
			if (msg[i].len > 21) {
				ret = -EOPNOTSUPP;
				goto error;
			}
327
			if (msg[i].addr == af9015_af9013_config[0].i2c_addr)
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
				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,
};

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

static int af9015_init_endpoint(struct dvb_usb_device *d)
{
	int ret;
	u16 frame_size;
	u8  packet_size;
	deb_info("%s: USB speed:%d\n", __func__, d->udev->speed);

401 402
	/* Windows driver uses packet count 21 for USB1.1 and 348 for USB2.0.
	   We use smaller - about 1/4 from the original, 5 and 87. */
403 404
#define TS_PACKET_SIZE            188

405
#define TS_USB20_PACKET_COUNT      87
406 407
#define TS_USB20_FRAME_SIZE       (TS_PACKET_SIZE*TS_USB20_PACKET_COUNT)

408
#define TS_USB11_PACKET_COUNT       5
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
#define TS_USB11_FRAME_SIZE       (TS_PACKET_SIZE*TS_USB11_PACKET_COUNT)

#define TS_USB20_MAX_PACKET_SIZE  512
#define TS_USB11_MAX_PACKET_SIZE   64

	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 (af9015_config.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 (af9015_config.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 (af9015_config.dual_mode) {
		ret = af9015_clear_reg_bit(d, 0xd50b, 1); /* negate EP5 reset */
		if (ret)
			goto error;
	}

	/* enable / disable mp2if2 */
	if (af9015_config.dual_mode)
		ret = af9015_set_reg_bit(d, 0xd50b, 0);
	else
		ret = af9015_clear_reg_bit(d, 0xd50b, 0);
484

485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
error:
	if (ret)
		err("endpoint init failed:%d", ret);
	return ret;
}

static int af9015_copy_firmware(struct dvb_usb_device *d)
{
	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] = af9015_config.firmware_size >> 8;
	fw_params[1] = af9015_config.firmware_size & 0xff;
	fw_params[2] = af9015_config.firmware_checksum >> 8;
	fw_params[3] = af9015_config.firmware_checksum & 0xff;

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

508
	ret = af9015_read_reg_i2c(d,
509
		af9015_af9013_config[1].i2c_addr, 0x98be, &val);
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
	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 */
537
	ret = af9015_write_reg_i2c(d, af9015_af9013_config[1].i2c_addr,
538 539 540 541 542 543 544 545 546 547
		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);

		/* check firmware status */
		ret = af9015_read_reg_i2c(d,
548
			af9015_af9013_config[1].i2c_addr, 0x98be, &val);
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
		deb_info("%s: firmware status cmd status:%d fw status:%02x\n",
			__func__, ret, val);
		if (ret)
			goto error;

		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:
	return ret;
}

571 572
/* hash (and dump) eeprom */
static int af9015_eeprom_hash(struct usb_device *udev)
573
{
574 575 576 577 578
	static const unsigned int eeprom_size = 256;
	unsigned int reg;
	int ret;
	u8 val, *eeprom;
	struct req_t req = {READ_I2C, AF9015_I2C_EEPROM, 0, 0, 1, 1, &val};
579

580 581 582 583 584 585 586 587 588 589
	eeprom = kmalloc(eeprom_size, GFP_KERNEL);
	if (eeprom == NULL)
		return -ENOMEM;

	for (reg = 0; reg < eeprom_size; reg++) {
		req.addr = reg;
		ret = af9015_rw_udev(udev, &req);
		if (ret)
			goto free;
		eeprom[reg] = val;
590
	}
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609

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

	BUG_ON(eeprom_size % 4);

	af9015_config.eeprom_sum = 0;
	for (reg = 0; reg < eeprom_size / sizeof(u32); reg++) {
		af9015_config.eeprom_sum *= GOLDEN_RATIO_PRIME_32;
		af9015_config.eeprom_sum += le32_to_cpu(((u32 *)eeprom)[reg]);
	}

	deb_info("%s: eeprom sum=%.8x\n", __func__, af9015_config.eeprom_sum);

	ret = 0;
free:
	kfree(eeprom);
	return ret;
610 611 612 613 614 615 616
}

static int af9015_init(struct dvb_usb_device *d)
{
	int ret;
	deb_info("%s:\n", __func__);

617 618 619 620 621
	/* init RC canary */
	ret = af9015_write_reg(d, 0x98e9, 0xff);
	if (ret)
		goto error;

622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
	ret = af9015_init_endpoint(d);
	if (ret)
		goto error;

error:
	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)
		ret = af9015_set_reg_bit(adap->dev, 0xd503, 0);
	else
		ret = af9015_clear_reg_bit(adap->dev, 0xd503, 0);

	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);

	ret = af9015_write_reg(adap->dev, 0xd505, (pid & 0xff));
	if (ret)
		goto error;

	ret = af9015_write_reg(adap->dev, 0xd506, (pid >> 8));
	if (ret)
		goto error;

	idx = ((index & 0x1f) | (1 << 5));
	ret = af9015_write_reg(adap->dev, 0xd504, idx);

error:
	return ret;
}

static int af9015_download_firmware(struct usb_device *udev,
	const struct firmware *fw)
{
670
	int i, len, remaining, ret;
671 672 673 674 675 676 677 678 679 680 681 682
	struct req_t req = {DOWNLOAD_FIRMWARE, 0, 0, 0, 0, 0, NULL};
	u16 checksum = 0;

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

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

	af9015_config.firmware_size = fw->size;
	af9015_config.firmware_checksum = checksum;

683 684 685 686 687 688
	#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;
689 690

		req.data_len = len;
691 692
		req.data = (u8 *) &fw->data[fw->size - remaining];
		req.addr = FW_ADDR + fw->size - remaining;
693 694 695

		ret = af9015_rw_udev(udev, &req);
		if (ret) {
696
			err("firmware download failed:%d", ret);
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
			goto error;
		}
	}

	/* firmware loaded, request boot */
	req.cmd = BOOT;
	ret = af9015_rw_udev(udev, &req);
	if (ret) {
		err("firmware boot failed:%d", ret);
		goto error;
	}

error:
	return ret;
}

713
struct af9015_rc_setup {
714
	unsigned int id;
715
	char *rc_codes;
716 717
};

718 719
static char *af9015_rc_setup_match(unsigned int id,
	const struct af9015_rc_setup *table)
720
{
721
	for (; table->rc_codes; table++)
722
		if (table->id == id)
723
			return table->rc_codes;
724 725 726
	return NULL;
}

727 728 729 730 731 732
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 },
733 734 735
	{ }
};

736 737 738 739
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 */
740
	{ 0x5d49e3db, RC_MAP_DIGITTRADE }, /* LC-Power LC-USB-DVBT */
741 742 743
	{ }
};

744
static const struct af9015_rc_setup af9015_rc_setup_usbids[] = {
745
	{ (USB_VID_TERRATEC << 16) | USB_PID_TERRATEC_CINERGY_T_STICK_RC,
746
		RC_MAP_TERRATEC_SLIM_2 },
747
	{ (USB_VID_TERRATEC << 16) | USB_PID_TERRATEC_CINERGY_T_STICK_DUAL_RC,
748
		RC_MAP_TERRATEC_SLIM },
749
	{ (USB_VID_VISIONPLUS << 16) | USB_PID_AZUREWAVE_AD_TU700,
750
		RC_MAP_AZUREWAVE_AD_TU700 },
751
	{ (USB_VID_VISIONPLUS << 16) | USB_PID_TINYTWIN,
752
		RC_MAP_AZUREWAVE_AD_TU700 },
753
	{ (USB_VID_MSI_2 << 16) | USB_PID_MSI_DIGI_VOX_MINI_III,
754
		RC_MAP_MSI_DIGIVOX_III },
755
	{ (USB_VID_MSI_2 << 16) | USB_PID_MSI_DIGIVOX_DUO,
756
		RC_MAP_MSI_DIGIVOX_III },
757
	{ (USB_VID_LEADTEK << 16) | USB_PID_WINFAST_DTV_DONGLE_GOLD,
758
		RC_MAP_LEADTEK_Y04G0051 },
759
	{ (USB_VID_LEADTEK << 16) | USB_PID_WINFAST_DTV2000DS,
760
		RC_MAP_LEADTEK_Y04G0051 },
761
	{ (USB_VID_AVERMEDIA << 16) | USB_PID_AVERMEDIA_VOLAR_X,
762
		RC_MAP_AVERMEDIA_M135A },
763
	{ (USB_VID_AFATECH << 16) | USB_PID_TREKSTOR_DVBT,
764
		RC_MAP_TREKSTOR },
765
	{ (USB_VID_KWORLD_2 << 16) | USB_PID_TINYTWIN_2,
766
		RC_MAP_DIGITALNOW_TINYTWIN },
767
	{ (USB_VID_GTEK << 16) | USB_PID_TINYTWIN_3,
768
		RC_MAP_DIGITALNOW_TINYTWIN },
769
	{ (USB_VID_KWORLD_2 << 16) | USB_PID_SVEON_STV22,
770
		RC_MAP_MSI_DIGIVOX_III },
771 772 773
	{ }
};

774 775 776
static void af9015_set_remote_config(struct usb_device *udev,
		struct dvb_usb_device_properties *props)
{
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
	u16 vid = le16_to_cpu(udev->descriptor.idVendor);
	u16 pid = le16_to_cpu(udev->descriptor.idProduct);

	/* try to load remote based module param */
	props->rc.core.rc_codes = af9015_rc_setup_match(
		dvb_usb_af9015_remote, af9015_rc_setup_modparam);

	/* try to load remote based eeprom hash */
	if (!props->rc.core.rc_codes)
		props->rc.core.rc_codes = af9015_rc_setup_match(
			af9015_config.eeprom_sum, af9015_rc_setup_hashes);

	/* try to load remote based USB ID */
	if (!props->rc.core.rc_codes)
		props->rc.core.rc_codes = af9015_rc_setup_match(
			(vid << 16) + pid, af9015_rc_setup_usbids);

	/* try to load remote based USB iManufacturer string */
	if (!props->rc.core.rc_codes && 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(udev, udev->descriptor.iManufacturer,
			manufacturer, sizeof(manufacturer));
		if (!strcmp("MSI", manufacturer)) {
			/* iManufacturer 1 MSI
			   iProduct      2 MSI K-VOX */
			props->rc.core.rc_codes = af9015_rc_setup_match(
				AF9015_REMOTE_MSI_DIGIVOX_MINI_II_V3,
				af9015_rc_setup_modparam);
		}
811
	}
812 813 814 815 816

	/* finally load "empty" just for leaving IR receiver enabled */
	if (!props->rc.core.rc_codes)
		props->rc.core.rc_codes = RC_MAP_EMPTY;

817
	return;
818 819
}

820 821 822 823 824 825 826 827
static int af9015_read_config(struct usb_device *udev)
{
	int ret;
	u8 val, i, offset = 0;
	struct req_t req = {READ_I2C, AF9015_I2C_EEPROM, 0, 0, 1, 1, &val};

	/* IR remote controller */
	req.addr = AF9015_EEPROM_IR_MODE;
828 829 830 831 832 833
	/* first message will timeout often due to possible hw bug */
	for (i = 0; i < 4; i++) {
		ret = af9015_rw_udev(udev, &req);
		if (!ret)
			break;
	}
834 835
	if (ret)
		goto error;
836 837 838 839 840

	ret = af9015_eeprom_hash(udev);
	if (ret)
		goto error;

841
	deb_info("%s: IR mode=%d\n", __func__, val);
842
	for (i = 0; i < af9015_properties_count; i++) {
843 844 845
		if (val == AF9015_IR_MODE_DISABLED)
			af9015_properties[i].rc.core.rc_codes = NULL;
		else
846
			af9015_set_remote_config(udev, &af9015_properties[i]);
847 848 849 850 851 852 853 854
	}

	/* TS mode - one or two receivers */
	req.addr = AF9015_EEPROM_TS_MODE;
	ret = af9015_rw_udev(udev, &req);
	if (ret)
		goto error;
	af9015_config.dual_mode = val;
855
	deb_info("%s: TS mode=%d\n", __func__, af9015_config.dual_mode);
856

857 858
	/* Set adapter0 buffer size according to USB port speed, adapter1 buffer
	   size can be static because it is enabled only USB2.0 */
859 860 861
	for (i = 0; i < af9015_properties_count; i++) {
		/* USB1.1 set smaller buffersize and disable 2nd adapter */
		if (udev->speed == USB_SPEED_FULL) {
862
			af9015_properties[i].adapter[0].fe[0].stream.u.bulk.buffersize
863
				= TS_USB11_FRAME_SIZE;
864 865 866 867
			/* disable 2nd adapter because we don't have
			   PID-filters */
			af9015_config.dual_mode = 0;
		} else {
868
			af9015_properties[i].adapter[0].fe[0].stream.u.bulk.buffersize
869
				= TS_USB20_FRAME_SIZE;
870 871 872 873 874 875 876 877 878
		}
	}

	if (af9015_config.dual_mode) {
		/* read 2nd demodulator I2C address */
		req.addr = AF9015_EEPROM_DEMOD2_I2C;
		ret = af9015_rw_udev(udev, &req);
		if (ret)
			goto error;
879
		af9015_af9013_config[1].i2c_addr = val;
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900

		/* enable 2nd adapter */
		for (i = 0; i < af9015_properties_count; i++)
			af9015_properties[i].num_adapters = 2;

	} else {
		 /* disable 2nd adapter */
		for (i = 0; i < af9015_properties_count; i++)
			af9015_properties[i].num_adapters = 1;
	}

	for (i = 0; i < af9015_properties[0].num_adapters; i++) {
		if (i == 1)
			offset = AF9015_EEPROM_OFFSET;
		/* xtal */
		req.addr = AF9015_EEPROM_XTAL_TYPE1 + offset;
		ret = af9015_rw_udev(udev, &req);
		if (ret)
			goto error;
		switch (val) {
		case 0:
901
			af9015_af9013_config[i].clock = 28800000;
902 903
			break;
		case 1:
904
			af9015_af9013_config[i].clock = 20480000;
905 906
			break;
		case 2:
907
			af9015_af9013_config[i].clock = 28000000;
908 909
			break;
		case 3:
910
			af9015_af9013_config[i].clock = 25000000;
911 912
			break;
		};
913 914
		deb_info("%s: [%d] xtal=%d set clock=%d\n", __func__, i,
			val, af9015_af9013_config[i].clock);
915

916
		/* IF frequency */
917 918 919 920
		req.addr = AF9015_EEPROM_IF1H + offset;
		ret = af9015_rw_udev(udev, &req);
		if (ret)
			goto error;
921 922 923

		af9015_af9013_config[i].if_frequency = val << 8;

924 925 926 927
		req.addr = AF9015_EEPROM_IF1L + offset;
		ret = af9015_rw_udev(udev, &req);
		if (ret)
			goto error;
928 929 930 931 932

		af9015_af9013_config[i].if_frequency += val;
		af9015_af9013_config[i].if_frequency *= 1000;
		deb_info("%s: [%d] IF frequency=%d\n", __func__, i,
			af9015_af9013_config[0].if_frequency);
933 934 935 936 937 938 939 940 941 942 943 944

		/* MT2060 IF1 */
		req.addr = AF9015_EEPROM_MT2060_IF1H  + offset;
		ret = af9015_rw_udev(udev, &req);
		if (ret)
			goto error;
		af9015_config.mt2060_if1[i] = val << 8;
		req.addr = AF9015_EEPROM_MT2060_IF1L + offset;
		ret = af9015_rw_udev(udev, &req);
		if (ret)
			goto error;
		af9015_config.mt2060_if1[i] += val;
945
		deb_info("%s: [%d] MT2060 IF1=%d\n", __func__, i,
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
			af9015_config.mt2060_if1[i]);

		/* tuner */
		req.addr =  AF9015_EEPROM_TUNER_ID1 + offset;
		ret = af9015_rw_udev(udev, &req);
		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:
961
		case AF9013_TUNER_TDA18218:
962
			af9015_af9013_config[i].spec_inv = 1;
963 964 965 966
			break;
		case AF9013_TUNER_MXL5003D:
		case AF9013_TUNER_MXL5005D:
		case AF9013_TUNER_MXL5005R:
967
		case AF9013_TUNER_MXL5007T:
968
			af9015_af9013_config[i].spec_inv = 0;
969
			break;
970 971
		case AF9013_TUNER_MC44S803:
			af9015_af9013_config[i].gpio[1] = AF9013_GPIO_LO;
972
			af9015_af9013_config[i].spec_inv = 1;
973
			break;
974
		default:
975
			warn("tuner id=%d not supported, please report!", val);
976 977 978 979
			return -ENODEV;
		};

		af9015_af9013_config[i].tuner = val;
980
		deb_info("%s: [%d] tuner id=%d\n", __func__, i, val);
981 982 983 984
	}

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

987
	/* AverMedia AVerTV Volar Black HD (A850) device have bad EEPROM
988 989
	   content :-( Override some wrong values here. Ditto for the
	   AVerTV Red HD+ (A850T) device. */
990
	if (le16_to_cpu(udev->descriptor.idVendor) == USB_VID_AVERMEDIA &&
991 992 993 994
		((le16_to_cpu(udev->descriptor.idProduct) ==
			USB_PID_AVERMEDIA_A850) ||
		(le16_to_cpu(udev->descriptor.idProduct) ==
			USB_PID_AVERMEDIA_A850T))) {
995 996 997 998 999 1000 1001 1002
		deb_info("%s: AverMedia A850: overriding config\n", __func__);
		/* disable dual mode */
		af9015_config.dual_mode = 0;
		 /* disable 2nd adapter */
		for (i = 0; i < af9015_properties_count; i++)
			af9015_properties[i].num_adapters = 1;

		/* set correct IF */
1003
		af9015_af9013_config[0].if_frequency = 4570000;
1004 1005
	}

1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
	return ret;
}

static int af9015_identify_state(struct usb_device *udev,
				 struct dvb_usb_device_properties *props,
				 struct dvb_usb_device_description **desc,
				 int *cold)
{
	int ret;
	u8 reply;
	struct req_t req = {GET_CONFIG, 0, 0, 0, 0, 1, &reply};

	ret = af9015_rw_udev(udev, &req);
	if (ret)
		return ret;

	deb_info("%s: reply:%02x\n", __func__, reply);
	if (reply == 0x02)
		*cold = 0;
	else
		*cold = 1;

	return ret;
}

1031
static int af9015_rc_query(struct dvb_usb_device *d)
1032
{
1033 1034
	struct af9015_state *priv = d->priv;
	int ret;
I
Ian Armstrong 已提交
1035
	u8 buf[17];
1036

1037
	/* read registers needed to detect remote controller code */
1038
	ret = af9015_read_regs(d, 0x98d9, buf, sizeof(buf));
1039 1040
	if (ret)
		goto error;
1041

I
Ian Armstrong 已提交
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
	/* If any of these are non-zero, assume invalid data */
	if (buf[1] || buf[2] || buf[3])
		return ret;

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

	/* Only process key if canary killed */
	if (buf[16] != 0xff && buf[0] != 0x01) {
1057 1058
		deb_rc("%s: key pressed %02x %02x %02x %02x\n", __func__,
			buf[12], buf[13], buf[14], buf[15]);
1059

1060 1061 1062 1063 1064
		/* Reset the canary */
		ret = af9015_write_reg(d, 0x98e9, 0xff);
		if (ret)
			goto error;

I
Ian Armstrong 已提交
1065 1066
		/* Remember this key */
		memcpy(priv->rc_last, &buf[12], 4);
1067 1068
		if (buf[14] == (u8) ~buf[15]) {
			if (buf[12] == (u8) ~buf[13]) {
1069
				/* NEC */
1070
				priv->rc_keycode = buf[12] << 8 | buf[14];
1071 1072
			} else {
				/* NEC extended*/
1073 1074
				priv->rc_keycode = buf[12] << 16 |
					buf[13] << 8 | buf[14];
1075 1076
			}
		} else {
1077
			/* 32 bit NEC */
I
Ian Armstrong 已提交
1078 1079
			priv->rc_keycode = buf[12] << 24 | buf[13] << 16 |
					buf[14] << 8 | buf[15];
1080
		}
1081
		rc_keydown(d->rc_dev, priv->rc_keycode, 0);
1082 1083
	} else {
		deb_rc("%s: no key press\n", __func__);
I
Ian Armstrong 已提交
1084 1085 1086
		/* Invalidate last keypress */
		/* Not really needed, but helps with debug */
		priv->rc_last[2] = priv->rc_last[3];
1087 1088
	}

1089
	priv->rc_repeat = buf[6];
1090 1091 1092 1093 1094 1095

error:
	if (ret)
		err("%s: failed:%d", __func__, ret);

	return ret;
1096 1097
}

1098
/* override demod callbacks for resource locking */
1099
static int af9015_af9013_set_frontend(struct dvb_frontend *fe)
1100 1101 1102 1103 1104 1105 1106 1107
{
	int ret;
	struct dvb_usb_adapter *adap = fe->dvb->priv;
	struct af9015_state *priv = adap->dev->priv;

	if (mutex_lock_interruptible(&adap->dev->usb_mutex))
		return -EAGAIN;

1108
	ret = priv->set_frontend[adap->id](fe);
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143

	mutex_unlock(&adap->dev->usb_mutex);

	return ret;
}

/* override demod callbacks for resource locking */
static int af9015_af9013_read_status(struct dvb_frontend *fe,
	fe_status_t *status)
{
	int ret;
	struct dvb_usb_adapter *adap = fe->dvb->priv;
	struct af9015_state *priv = adap->dev->priv;

	if (mutex_lock_interruptible(&adap->dev->usb_mutex))
		return -EAGAIN;

	ret = priv->read_status[adap->id](fe, status);

	mutex_unlock(&adap->dev->usb_mutex);

	return ret;
}

/* override demod callbacks for resource locking */
static int af9015_af9013_init(struct dvb_frontend *fe)
{
	int ret;
	struct dvb_usb_adapter *adap = fe->dvb->priv;
	struct af9015_state *priv = adap->dev->priv;

	if (mutex_lock_interruptible(&adap->dev->usb_mutex))
		return -EAGAIN;

	ret = priv->init[adap->id](fe);
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
	if (ret)
		goto err_unlock;

	if (priv->tuner_ops_init[adap->id]) {
		if (fe->ops.i2c_gate_ctrl)
			fe->ops.i2c_gate_ctrl(fe, 1);
		ret = priv->tuner_ops_init[adap->id](fe);
		if (fe->ops.i2c_gate_ctrl)
			fe->ops.i2c_gate_ctrl(fe, 0);
	}
1154

1155
err_unlock:
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
	mutex_unlock(&adap->dev->usb_mutex);

	return ret;
}

/* override demod callbacks for resource locking */
static int af9015_af9013_sleep(struct dvb_frontend *fe)
{
	int ret;
	struct dvb_usb_adapter *adap = fe->dvb->priv;
	struct af9015_state *priv = adap->dev->priv;

	if (mutex_lock_interruptible(&adap->dev->usb_mutex))
		return -EAGAIN;

1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
	if (priv->tuner_ops_sleep[adap->id]) {
		if (fe->ops.i2c_gate_ctrl)
			fe->ops.i2c_gate_ctrl(fe, 1);
		ret = priv->tuner_ops_sleep[adap->id](fe);
		if (fe->ops.i2c_gate_ctrl)
			fe->ops.i2c_gate_ctrl(fe, 0);
		if (ret)
			goto err_unlock;
	}

1181
	ret = priv->sleep[adap->id](fe);
1182

1183
err_unlock:
1184 1185 1186 1187 1188
	mutex_unlock(&adap->dev->usb_mutex);

	return ret;
}

1189 1190 1191
static int af9015_af9013_frontend_attach(struct dvb_usb_adapter *adap)
{
	int ret;
1192
	struct af9015_state *state = adap->dev->priv;
1193

1194
	if (adap->id == 1) {
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
		/* copy firmware to 2nd demodulator */
		if (af9015_config.dual_mode) {
			ret = af9015_copy_firmware(adap->dev);
			if (ret) {
				err("firmware copy to 2nd frontend " \
					"failed, will disable it");
				af9015_config.dual_mode = 0;
				return -ENODEV;
			}
		} else {
			return -ENODEV;
		}
	}

	/* attach demodulator */
1210
	adap->fe_adap[0].fe = dvb_attach(af9013_attach, &af9015_af9013_config[adap->id],
1211
		&adap->dev->i2c_adap);
1212

1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
	/*
	 * 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.
	 * Note: we abuse unused usb_mutex here.
	 */
	if (adap->fe_adap[0].fe) {
		state->set_frontend[adap->id] =
			adap->fe_adap[0].fe->ops.set_frontend;
		adap->fe_adap[0].fe->ops.set_frontend =
			af9015_af9013_set_frontend;

		state->read_status[adap->id] =
			adap->fe_adap[0].fe->ops.read_status;
		adap->fe_adap[0].fe->ops.read_status =
			af9015_af9013_read_status;

		state->init[adap->id] = adap->fe_adap[0].fe->ops.init;
		adap->fe_adap[0].fe->ops.init = af9015_af9013_init;

		state->sleep[adap->id] = adap->fe_adap[0].fe->ops.sleep;
		adap->fe_adap[0].fe->ops.sleep = af9015_af9013_sleep;
	}

1239
	return adap->fe_adap[0].fe == NULL ? -ENODEV : 0;
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
}

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,
1253
	.small_i2c = TDA18271_16_BYTE_CHUNK_INIT,
1254 1255 1256 1257 1258 1259 1260 1261
};

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,
1262
	.rssi_enable     = MXL_RSSI_ENABLE,
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
	.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,
1279
	.rssi_enable     = MXL_RSSI_ENABLE,
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
	.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,
};

1290 1291 1292 1293 1294
static struct mc44s803_config af9015_mc44s803_config = {
	.i2c_address = 0xc0,
	.dig_out = 1,
};

1295 1296 1297 1298 1299
static struct tda18218_config af9015_tda18218_config = {
	.i2c_address = 0xc0,
	.i2c_wr_max = 21, /* max wr bytes AF9015 I2C adap can handle at once */
};

1300 1301 1302 1303 1304
static struct mxl5007t_config af9015_mxl5007t_config = {
	.xtal_freq_hz = MxL_XTAL_24_MHZ,
	.if_freq_hz = MxL_IF_4_57_MHZ,
};

1305 1306 1307
static int af9015_tuner_attach(struct dvb_usb_adapter *adap)
{
	int ret;
1308
	struct af9015_state *state = adap->dev->priv;
1309
	deb_info("%s:\n", __func__);
1310 1311 1312 1313

	switch (af9015_af9013_config[adap->id].tuner) {
	case AF9013_TUNER_MT2060:
	case AF9013_TUNER_MT2060_2:
1314
		ret = dvb_attach(mt2060_attach, adap->fe_adap[0].fe, &adap->dev->i2c_adap,
1315 1316 1317 1318 1319 1320
			&af9015_mt2060_config,
			af9015_config.mt2060_if1[adap->id])
			== NULL ? -ENODEV : 0;
		break;
	case AF9013_TUNER_QT1010:
	case AF9013_TUNER_QT1010A:
1321
		ret = dvb_attach(qt1010_attach, adap->fe_adap[0].fe, &adap->dev->i2c_adap,
1322 1323 1324
			&af9015_qt1010_config) == NULL ? -ENODEV : 0;
		break;
	case AF9013_TUNER_TDA18271:
1325
		ret = dvb_attach(tda18271_attach, adap->fe_adap[0].fe, 0xc0,
1326
			&adap->dev->i2c_adap,
1327 1328
			&af9015_tda18271_config) == NULL ? -ENODEV : 0;
		break;
1329
	case AF9013_TUNER_TDA18218:
1330
		ret = dvb_attach(tda18218_attach, adap->fe_adap[0].fe,
1331
			&adap->dev->i2c_adap,
1332 1333
			&af9015_tda18218_config) == NULL ? -ENODEV : 0;
		break;
1334
	case AF9013_TUNER_MXL5003D:
1335
		ret = dvb_attach(mxl5005s_attach, adap->fe_adap[0].fe,
1336
			&adap->dev->i2c_adap,
1337 1338 1339 1340
			&af9015_mxl5003_config) == NULL ? -ENODEV : 0;
		break;
	case AF9013_TUNER_MXL5005D:
	case AF9013_TUNER_MXL5005R:
1341
		ret = dvb_attach(mxl5005s_attach, adap->fe_adap[0].fe,
1342
			&adap->dev->i2c_adap,
1343 1344 1345
			&af9015_mxl5005_config) == NULL ? -ENODEV : 0;
		break;
	case AF9013_TUNER_ENV77H11D5:
1346
		ret = dvb_attach(dvb_pll_attach, adap->fe_adap[0].fe, 0xc0,
1347
			&adap->dev->i2c_adap,
1348 1349 1350
			DVB_PLL_TDA665X) == NULL ? -ENODEV : 0;
		break;
	case AF9013_TUNER_MC44S803:
1351
		ret = dvb_attach(mc44s803_attach, adap->fe_adap[0].fe,
1352
			&adap->dev->i2c_adap,
1353
			&af9015_mc44s803_config) == NULL ? -ENODEV : 0;
1354
		break;
1355
	case AF9013_TUNER_MXL5007T:
1356
		ret = dvb_attach(mxl5007t_attach, adap->fe_adap[0].fe,
1357
			&adap->dev->i2c_adap,
1358 1359
			0xc0, &af9015_mxl5007t_config) == NULL ? -ENODEV : 0;
		break;
1360 1361 1362 1363 1364 1365
	case AF9013_TUNER_UNKNOWN:
	default:
		ret = -ENODEV;
		err("Unknown tuner id:%d",
			af9015_af9013_config[adap->id].tuner);
	}
1366 1367 1368 1369 1370 1371 1372 1373

	state->tuner_ops_sleep[adap->id] =
				adap->fe_adap[0].fe->ops.tuner_ops.sleep;
	adap->fe_adap[0].fe->ops.tuner_ops.sleep = 0;

	state->tuner_ops_init[adap->id] =
				adap->fe_adap[0].fe->ops.tuner_ops.init;
	adap->fe_adap[0].fe->ops.tuner_ops.init = 0;
1374 1375 1376
	return ret;
}

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
enum af9015_usb_table_entry {
	AFATECH_9015,
	AFATECH_9016,
	WINFAST_DTV_GOLD,
	PINNACLE_PCTV_71E,
	KWORLD_PLUSTV_399U,
	TINYTWIN,
	AZUREWAVE_TU700,
	TERRATEC_AF9015,
	KWORLD_PLUSTV_PC160,
	AVERTV_VOLAR_X,
	XTENSIONS_380U,
	MSI_DIGIVOX_DUO,
	AVERTV_VOLAR_X_REV2,
	TELESTAR_STARSTICK_2,
	AVERMEDIA_A309_USB,
	MSI_DIGIVOX_MINI_III,
	KWORLD_E396,
	KWORLD_E39B,
	KWORLD_E395,
	TREKSTOR_DVBT,
	AVERTV_A850,
	AVERTV_A805,
	CONCEPTRONIC_CTVDIGRCU,
	KWORLD_MC810,
	GENIUS_TVGO_DVB_T03,
	KWORLD_399U_2,
	KWORLD_PC160_T,
	SVEON_STV20,
	TINYTWIN_2,
	WINFAST_DTV2000DS,
	KWORLD_UB383_T,
	KWORLD_E39A,
	AVERMEDIA_A815M,
	CINERGY_T_STICK_RC,
	CINERGY_T_DUAL_RC,
	AVERTV_A850T,
	TINYTWIN_3,
	SVEON_STV22,
};

1418
static struct usb_device_id af9015_usb_table[] = {
1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482
	[AFATECH_9015] =
		{USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9015_9015)},
	[AFATECH_9016] =
		{USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9015_9016)},
	[WINFAST_DTV_GOLD] =
		{USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV_DONGLE_GOLD)},
	[PINNACLE_PCTV_71E] =
		{USB_DEVICE(USB_VID_PINNACLE, USB_PID_PINNACLE_PCTV71E)},
	[KWORLD_PLUSTV_399U] =
		{USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_399U)},
	[TINYTWIN] = {USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_TINYTWIN)},
	[AZUREWAVE_TU700] =
		{USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_AZUREWAVE_AD_TU700)},
	[TERRATEC_AF9015] = {USB_DEVICE(USB_VID_TERRATEC,
				USB_PID_TERRATEC_CINERGY_T_USB_XE_REV2)},
	[KWORLD_PLUSTV_PC160] =
		{USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_PC160_2T)},
	[AVERTV_VOLAR_X] =
		{USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_X)},
	[XTENSIONS_380U] =
		{USB_DEVICE(USB_VID_XTENSIONS, USB_PID_XTENSIONS_XD_380)},
	[MSI_DIGIVOX_DUO] =
		{USB_DEVICE(USB_VID_MSI_2, USB_PID_MSI_DIGIVOX_DUO)},
	[AVERTV_VOLAR_X_REV2] =
		{USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_X_2)},
	[TELESTAR_STARSTICK_2] =
		{USB_DEVICE(USB_VID_TELESTAR,  USB_PID_TELESTAR_STARSTICK_2)},
	[AVERMEDIA_A309_USB] =
		{USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A309)},
	[MSI_DIGIVOX_MINI_III] =
		{USB_DEVICE(USB_VID_MSI_2, USB_PID_MSI_DIGI_VOX_MINI_III)},
	[KWORLD_E396] = {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U)},
	[KWORLD_E39B] = {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_2)},
	[KWORLD_E395] = {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_3)},
	[TREKSTOR_DVBT] = {USB_DEVICE(USB_VID_AFATECH, USB_PID_TREKSTOR_DVBT)},
	[AVERTV_A850] = {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A850)},
	[AVERTV_A805] = {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A805)},
	[CONCEPTRONIC_CTVDIGRCU] =
		{USB_DEVICE(USB_VID_KWORLD_2, USB_PID_CONCEPTRONIC_CTVDIGRCU)},
	[KWORLD_MC810] = {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_MC810)},
	[GENIUS_TVGO_DVB_T03] =
		{USB_DEVICE(USB_VID_KYE, USB_PID_GENIUS_TVGO_DVB_T03)},
	[KWORLD_399U_2] = {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_399U_2)},
	[KWORLD_PC160_T] =
		{USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_PC160_T)},
	[SVEON_STV20] = {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV20)},
	[TINYTWIN_2] = {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_TINYTWIN_2)},
	[WINFAST_DTV2000DS] =
		{USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV2000DS)},
	[KWORLD_UB383_T] =
		{USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_UB383_T)},
	[KWORLD_E39A] =
		{USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_4)},
	[AVERMEDIA_A815M] =
		{USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A815M)},
	[CINERGY_T_STICK_RC] = {USB_DEVICE(USB_VID_TERRATEC,
				USB_PID_TERRATEC_CINERGY_T_STICK_RC)},
	[CINERGY_T_DUAL_RC] = {USB_DEVICE(USB_VID_TERRATEC,
				USB_PID_TERRATEC_CINERGY_T_STICK_DUAL_RC)},
	[AVERTV_A850T] =
		{USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A850T)},
	[TINYTWIN_3] = {USB_DEVICE(USB_VID_GTEK, USB_PID_TINYTWIN_3)},
	[SVEON_STV22] = {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV22)},
	{ }
1483 1484 1485
};
MODULE_DEVICE_TABLE(usb, af9015_usb_table);

1486
#define AF9015_RC_INTERVAL 500
1487 1488 1489 1490 1491 1492 1493
static struct dvb_usb_device_properties af9015_properties[] = {
	{
		.caps = DVB_USB_IS_AN_I2C_ADAPTER,

		.usb_ctrl = DEVICE_SPECIFIC,
		.download_firmware = af9015_download_firmware,
		.firmware = "dvb-usb-af9015.fw",
1494
		.no_reconnect = 1,
1495

1496
		.size_of_priv = sizeof(struct af9015_state),
1497 1498 1499 1500

		.num_adapters = 2,
		.adapter = {
			{
1501 1502
			.num_frontends = 1,
			.fe = {{
1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517
				.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,

				.frontend_attach =
					af9015_af9013_frontend_attach,
				.tuner_attach    = af9015_tuner_attach,
				.stream = {
					.type = USB_BULK,
					.count = 6,
					.endpoint = 0x84,
				},
1518
			}},
1519 1520
			},
			{
1521 1522
			.num_frontends = 1,
			.fe = {{
1523 1524 1525 1526 1527 1528 1529
				.frontend_attach =
					af9015_af9013_frontend_attach,
				.tuner_attach    = af9015_tuner_attach,
				.stream = {
					.type = USB_BULK,
					.count = 6,
					.endpoint = 0x85,
1530 1531 1532
					.u = {
						.bulk = {
							.buffersize =
1533
						TS_USB20_FRAME_SIZE,
1534 1535
						}
					}
1536
				},
1537
			}},
1538 1539 1540 1541 1542
			}
		},

		.identify_state = af9015_identify_state,

1543
		.rc.core = {
1544
			.protocol         = RC_TYPE_NEC,
1545
			.module_name      = "af9015",
1546
			.rc_query         = af9015_rc_query,
1547
			.rc_interval      = AF9015_RC_INTERVAL,
1548
			.allowed_protos   = RC_TYPE_NEC,
1549
		},
1550 1551 1552

		.i2c_algo = &af9015_i2c_algo,

1553
		.num_device_descs = 12, /* check max from dvb-usb.h */
1554 1555 1556
		.devices = {
			{
				.name = "Afatech AF9015 DVB-T USB2.0 stick",
1557 1558 1559 1560 1561
				.cold_ids = {
					&af9015_usb_table[AFATECH_9015],
					&af9015_usb_table[AFATECH_9016],
					NULL
				},
1562 1563 1564 1565
				.warm_ids = {NULL},
			},
			{
				.name = "Leadtek WinFast DTV Dongle Gold",
1566 1567 1568 1569
				.cold_ids = {
					&af9015_usb_table[WINFAST_DTV_GOLD],
					NULL
				},
1570 1571 1572 1573
				.warm_ids = {NULL},
			},
			{
				.name = "Pinnacle PCTV 71e",
1574 1575 1576 1577
				.cold_ids = {
					&af9015_usb_table[PINNACLE_PCTV_71E],
					NULL
				},
1578 1579 1580 1581 1582
				.warm_ids = {NULL},
			},
			{
				.name = "KWorld PlusTV Dual DVB-T Stick " \
					"(DVB-T 399U)",
1583 1584 1585 1586 1587
				.cold_ids = {
					&af9015_usb_table[KWORLD_PLUSTV_399U],
					&af9015_usb_table[KWORLD_399U_2],
					NULL
				},
1588 1589 1590 1591
				.warm_ids = {NULL},
			},
			{
				.name = "DigitalNow TinyTwin DVB-T Receiver",
1592 1593 1594 1595 1596 1597
				.cold_ids = {
					&af9015_usb_table[TINYTWIN],
					&af9015_usb_table[TINYTWIN_2],
					&af9015_usb_table[TINYTWIN_3],
					NULL
				},
1598 1599 1600 1601
				.warm_ids = {NULL},
			},
			{
				.name = "TwinHan AzureWave AD-TU700(704J)",
1602 1603 1604 1605
				.cold_ids = {
					&af9015_usb_table[AZUREWAVE_TU700],
					NULL
				},
1606 1607 1608 1609
				.warm_ids = {NULL},
			},
			{
				.name = "TerraTec Cinergy T USB XE",
1610 1611 1612 1613
				.cold_ids = {
					&af9015_usb_table[TERRATEC_AF9015],
					NULL
				},
1614 1615 1616 1617 1618
				.warm_ids = {NULL},
			},
			{
				.name = "KWorld PlusTV Dual DVB-T PCI " \
					"(DVB-T PC160-2T)",
1619 1620 1621 1622
				.cold_ids = {
					&af9015_usb_table[KWORLD_PLUSTV_PC160],
					NULL
				},
1623 1624 1625 1626
				.warm_ids = {NULL},
			},
			{
				.name = "AVerMedia AVerTV DVB-T Volar X",
1627 1628 1629 1630
				.cold_ids = {
					&af9015_usb_table[AVERTV_VOLAR_X],
					NULL
				},
1631 1632
				.warm_ids = {NULL},
			},
1633 1634
			{
				.name = "TerraTec Cinergy T Stick RC",
1635 1636 1637 1638
				.cold_ids = {
					&af9015_usb_table[CINERGY_T_STICK_RC],
					NULL
				},
1639 1640
				.warm_ids = {NULL},
			},
1641 1642
			{
				.name = "TerraTec Cinergy T Stick Dual RC",
1643 1644 1645 1646
				.cold_ids = {
					&af9015_usb_table[CINERGY_T_DUAL_RC],
					NULL
				},
1647 1648
				.warm_ids = {NULL},
			},
1649 1650
			{
				.name = "AverMedia AVerTV Red HD+ (A850T)",
1651 1652 1653 1654
				.cold_ids = {
					&af9015_usb_table[AVERTV_A850T],
					NULL
				},
1655 1656
				.warm_ids = {NULL},
			},
1657 1658 1659 1660 1661 1662 1663
		}
	}, {
		.caps = DVB_USB_IS_AN_I2C_ADAPTER,

		.usb_ctrl = DEVICE_SPECIFIC,
		.download_firmware = af9015_download_firmware,
		.firmware = "dvb-usb-af9015.fw",
1664
		.no_reconnect = 1,
1665

1666
		.size_of_priv = sizeof(struct af9015_state),
1667 1668 1669 1670

		.num_adapters = 2,
		.adapter = {
			{
1671 1672
			.num_frontends = 1,
			.fe = {{
1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687
				.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,

				.frontend_attach =
					af9015_af9013_frontend_attach,
				.tuner_attach    = af9015_tuner_attach,
				.stream = {
					.type = USB_BULK,
					.count = 6,
					.endpoint = 0x84,
				},
1688
			}},
1689 1690
			},
			{
1691 1692
			.num_frontends = 1,
			.fe = {{
1693 1694 1695 1696 1697 1698 1699
				.frontend_attach =
					af9015_af9013_frontend_attach,
				.tuner_attach    = af9015_tuner_attach,
				.stream = {
					.type = USB_BULK,
					.count = 6,
					.endpoint = 0x85,
1700 1701 1702
					.u = {
						.bulk = {
							.buffersize =
1703
						TS_USB20_FRAME_SIZE,
1704 1705
						}
					}
1706
				},
1707
			}},
1708 1709 1710 1711 1712
			}
		},

		.identify_state = af9015_identify_state,

1713
		.rc.core = {
1714
			.protocol         = RC_TYPE_NEC,
1715
			.module_name      = "af9015",
1716
			.rc_query         = af9015_rc_query,
1717
			.rc_interval      = AF9015_RC_INTERVAL,
1718
			.allowed_protos   = RC_TYPE_NEC,
1719
		},
1720 1721 1722

		.i2c_algo = &af9015_i2c_algo,

1723
		.num_device_descs = 10, /* check max from dvb-usb.h */
1724 1725 1726
		.devices = {
			{
				.name = "Xtensions XD-380",
1727 1728 1729 1730
				.cold_ids = {
					&af9015_usb_table[XTENSIONS_380U],
					NULL
				},
1731 1732 1733 1734
				.warm_ids = {NULL},
			},
			{
				.name = "MSI DIGIVOX Duo",
1735 1736 1737 1738
				.cold_ids = {
					&af9015_usb_table[MSI_DIGIVOX_DUO],
					NULL
				},
1739 1740 1741 1742
				.warm_ids = {NULL},
			},
			{
				.name = "Fujitsu-Siemens Slim Mobile USB DVB-T",
1743 1744 1745 1746
				.cold_ids = {
					&af9015_usb_table[AVERTV_VOLAR_X_REV2],
					NULL
				},
1747 1748
				.warm_ids = {NULL},
			},
1749
			{
A
Antti Palosaari 已提交
1750
				.name = "Telestar Starstick 2",
1751 1752 1753 1754
				.cold_ids = {
					&af9015_usb_table[TELESTAR_STARSTICK_2],
					NULL
				},
1755 1756
				.warm_ids = {NULL},
			},
1757 1758
			{
				.name = "AVerMedia A309",
1759 1760 1761 1762
				.cold_ids = {
					&af9015_usb_table[AVERMEDIA_A309_USB],
					NULL
				},
1763 1764
				.warm_ids = {NULL},
			},
1765 1766
			{
				.name = "MSI Digi VOX mini III",
1767 1768 1769 1770
				.cold_ids = {
					&af9015_usb_table[MSI_DIGIVOX_MINI_III],
					NULL
				},
1771 1772
				.warm_ids = {NULL},
			},
1773 1774 1775
			{
				.name = "KWorld USB DVB-T TV Stick II " \
					"(VS-DVB-T 395U)",
1776 1777 1778 1779 1780 1781 1782
				.cold_ids = {
					&af9015_usb_table[KWORLD_E396],
					&af9015_usb_table[KWORLD_E39B],
					&af9015_usb_table[KWORLD_E395],
					&af9015_usb_table[KWORLD_E39A],
					NULL
				},
1783 1784
				.warm_ids = {NULL},
			},
1785 1786
			{
				.name = "TrekStor DVB-T USB Stick",
1787 1788 1789 1790
				.cold_ids = {
					&af9015_usb_table[TREKSTOR_DVBT],
					NULL
				},
1791 1792
				.warm_ids = {NULL},
			},
1793 1794 1795
			{
				.name = "AverMedia AVerTV Volar Black HD " \
					"(A850)",
1796 1797 1798 1799
				.cold_ids = {
					&af9015_usb_table[AVERTV_A850],
					NULL
				},
1800 1801
				.warm_ids = {NULL},
			},
1802 1803
			{
				.name = "Sveon STV22 Dual USB DVB-T Tuner HDTV",
1804 1805 1806 1807
				.cold_ids = {
					&af9015_usb_table[SVEON_STV22],
					NULL
				},
1808 1809
				.warm_ids = {NULL},
			},
1810
		}
1811 1812 1813 1814 1815 1816 1817 1818
	}, {
		.caps = DVB_USB_IS_AN_I2C_ADAPTER,

		.usb_ctrl = DEVICE_SPECIFIC,
		.download_firmware = af9015_download_firmware,
		.firmware = "dvb-usb-af9015.fw",
		.no_reconnect = 1,

1819
		.size_of_priv = sizeof(struct af9015_state),
1820 1821 1822 1823

		.num_adapters = 2,
		.adapter = {
			{
1824 1825
			.num_frontends = 1,
			.fe = {{
1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840
				.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,

				.frontend_attach =
					af9015_af9013_frontend_attach,
				.tuner_attach    = af9015_tuner_attach,
				.stream = {
					.type = USB_BULK,
					.count = 6,
					.endpoint = 0x84,
				},
1841
			}},
1842 1843
			},
			{
1844 1845
			.num_frontends = 1,
			.fe = {{
1846 1847 1848 1849 1850 1851 1852 1853 1854 1855
				.frontend_attach =
					af9015_af9013_frontend_attach,
				.tuner_attach    = af9015_tuner_attach,
				.stream = {
					.type = USB_BULK,
					.count = 6,
					.endpoint = 0x85,
					.u = {
						.bulk = {
							.buffersize =
1856
						TS_USB20_FRAME_SIZE,
1857 1858 1859
						}
					}
				},
1860
			}},
1861 1862 1863 1864 1865
			}
		},

		.identify_state = af9015_identify_state,

1866
		.rc.core = {
1867
			.protocol         = RC_TYPE_NEC,
1868
			.module_name      = "af9015",
1869
			.rc_query         = af9015_rc_query,
1870
			.rc_interval      = AF9015_RC_INTERVAL,
1871
			.allowed_protos   = RC_TYPE_NEC,
1872
		},
1873 1874 1875

		.i2c_algo = &af9015_i2c_algo,

1876
		.num_device_descs = 9, /* check max from dvb-usb.h */
1877
		.devices = {
1878 1879
			{
				.name = "AverMedia AVerTV Volar GPS 805 (A805)",
1880 1881 1882 1883
				.cold_ids = {
					&af9015_usb_table[AVERTV_A805],
					NULL
				},
1884 1885
				.warm_ids = {NULL},
			},
1886 1887 1888
			{
				.name = "Conceptronic USB2.0 DVB-T CTVDIGRCU " \
					"V3.0",
1889 1890 1891 1892
				.cold_ids = {
					&af9015_usb_table[CONCEPTRONIC_CTVDIGRCU],
					NULL
				},
1893 1894
				.warm_ids = {NULL},
			},
1895 1896
			{
				.name = "KWorld Digial MC-810",
1897 1898 1899 1900
				.cold_ids = {
					&af9015_usb_table[KWORLD_MC810],
					NULL
				},
1901 1902
				.warm_ids = {NULL},
			},
1903 1904
			{
				.name = "Genius TVGo DVB-T03",
1905 1906 1907 1908
				.cold_ids = {
					&af9015_usb_table[GENIUS_TVGO_DVB_T03],
					NULL
				},
1909 1910
				.warm_ids = {NULL},
			},
1911 1912 1913
			{
				.name = "KWorld PlusTV DVB-T PCI Pro Card " \
					"(DVB-T PC160-T)",
1914 1915 1916 1917
				.cold_ids = {
					&af9015_usb_table[KWORLD_PC160_T],
					NULL
				},
1918 1919
				.warm_ids = {NULL},
			},
1920 1921
			{
				.name = "Sveon STV20 Tuner USB DVB-T HDTV",
1922 1923 1924 1925
				.cold_ids = {
					&af9015_usb_table[SVEON_STV20],
					NULL
				},
1926 1927
				.warm_ids = {NULL},
			},
1928 1929
			{
				.name = "Leadtek WinFast DTV2000DS",
1930 1931 1932 1933
				.cold_ids = {
					&af9015_usb_table[WINFAST_DTV2000DS],
					NULL
				},
1934 1935
				.warm_ids = {NULL},
			},
1936 1937 1938
			{
				.name = "KWorld USB DVB-T Stick Mobile " \
					"(UB383-T)",
1939 1940 1941 1942
				.cold_ids = {
					&af9015_usb_table[KWORLD_UB383_T],
					NULL
				},
1943 1944
				.warm_ids = {NULL},
			},
1945 1946
			{
				.name = "AverMedia AVerTV Volar M (A815Mac)",
1947 1948 1949 1950
				.cold_ids = {
					&af9015_usb_table[AVERMEDIA_A815M],
					NULL
				},
1951 1952
				.warm_ids = {NULL},
			},
1953 1954
		}
	},
1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996
};

static int af9015_usb_probe(struct usb_interface *intf,
			    const struct usb_device_id *id)
{
	int ret = 0;
	struct dvb_usb_device *d = NULL;
	struct usb_device *udev = interface_to_usbdev(intf);
	u8 i;

	deb_info("%s: interface:%d\n", __func__,
		intf->cur_altsetting->desc.bInterfaceNumber);

	/* interface 0 is used by DVB-T receiver and
	   interface 1 is for remote controller (HID) */
	if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
		ret = af9015_read_config(udev);
		if (ret)
			return ret;

		for (i = 0; i < af9015_properties_count; i++) {
			ret = dvb_usb_device_init(intf, &af9015_properties[i],
				THIS_MODULE, &d, adapter_nr);
			if (!ret)
				break;
			if (ret != -ENODEV)
				return ret;
		}
		if (ret)
			return ret;

		if (d)
			ret = af9015_init(d);
	}

	return ret;
}

/* usb specific object needed to register this driver with the usb subsystem */
static struct usb_driver af9015_usb_driver = {
	.name = "dvb_usb_af9015",
	.probe = af9015_usb_probe,
1997
	.disconnect = dvb_usb_device_exit,
1998 1999 2000
	.id_table = af9015_usb_table,
};

2001
module_usb_driver(af9015_usb_driver);
2002 2003 2004 2005

MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
MODULE_DESCRIPTION("Driver for Afatech AF9015 DVB-T");
MODULE_LICENSE("GPL");