anysee.c 23.2 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 Anysee E30 DVB-C & DVB-T USB2.0 receiver
 *
 * Copyright (C) 2007 Antti Palosaari <crope@iki.fi>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * TODO:
 * - add smart card reader support for Conditional Access (CA)
 *
 * Card reader in Anysee is nothing more than ISO 7816 card reader.
 * There is no hardware CAM in any Anysee device sold.
 * In my understanding it should be implemented by making own module
26 27
 * for ISO 7816 card reader, like dvb_ca_en50221 is implemented. This
 * module registers serial interface that can be used to communicate
28 29 30 31 32 33 34 35 36 37 38
 * with any ISO 7816 smart card.
 *
 * Any help according to implement serial smart card reader support
 * is highly welcome!
 */

#include "anysee.h"
#include "tda1002x.h"
#include "mt352.h"
#include "mt352_priv.h"
#include "zl10353.h"
39
#include "tda18212.h"
40
#include "cx24116.h"
41 42
#include "stv0900.h"
#include "stv6110.h"
43
#include "isl6423.h"
44 45 46 47 48

/* debug */
static int dvb_usb_anysee_debug;
module_param_named(debug, dvb_usb_anysee_debug, int, 0644);
MODULE_PARM_DESC(debug, "set debugging level" DVB_USB_DEBUG_STATUS);
49
static int dvb_usb_anysee_delsys;
50 51
module_param_named(delsys, dvb_usb_anysee_delsys, int, 0644);
MODULE_PARM_DESC(delsys, "select delivery mode (0=DVB-C, 1=DVB-T)");
52 53
DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);

54
static DEFINE_MUTEX(anysee_usb_mutex);
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 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 104 105 106 107 108 109 110

static int anysee_ctrl_msg(struct dvb_usb_device *d, u8 *sbuf, u8 slen,
	u8 *rbuf, u8 rlen)
{
	struct anysee_state *state = d->priv;
	int act_len, ret;
	u8 buf[64];

	memcpy(&buf[0], sbuf, slen);
	buf[60] = state->seq++;

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

	/* We need receive one message more after dvb_usb_generic_rw due
	   to weird transaction flow, which is 1 x send + 2 x receive. */
	ret = dvb_usb_generic_rw(d, buf, sizeof(buf), buf, sizeof(buf), 0);

	if (!ret) {
		/* receive 2nd answer */
		ret = usb_bulk_msg(d->udev, usb_rcvbulkpipe(d->udev,
			d->props.generic_bulk_ctrl_endpoint), buf, sizeof(buf),
			&act_len, 2000);
		if (ret)
			err("%s: recv bulk message failed: %d", __func__, ret);
		else {
			deb_xfer("<<< ");
			debug_dump(buf, act_len, deb_xfer);
		}
	}

	/* read request, copy returned data to return buf */
	if (!ret && rbuf && rlen)
		memcpy(rbuf, buf, rlen);

	mutex_unlock(&anysee_usb_mutex);

	return ret;
}

static int anysee_read_reg(struct dvb_usb_device *d, u16 reg, u8 *val)
{
	u8 buf[] = {CMD_REG_READ, reg >> 8, reg & 0xff, 0x01};
	int ret;
	ret = anysee_ctrl_msg(d, buf, sizeof(buf), val, 1);
	deb_info("%s: reg:%04x val:%02x\n", __func__, reg, *val);
	return ret;
}

static int anysee_write_reg(struct dvb_usb_device *d, u16 reg, u8 val)
{
	u8 buf[] = {CMD_REG_WRITE, reg >> 8, reg & 0xff, 0x01, val};
	deb_info("%s: reg:%04x val:%02x\n", __func__, reg, val);
	return anysee_ctrl_msg(d, buf, sizeof(buf), NULL, 0);
}

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
/* write single register with mask */
static int anysee_wr_reg_mask(struct dvb_usb_device *d, u16 reg, u8 val,
	u8 mask)
{
	int ret;
	u8 tmp;

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

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

	return anysee_write_reg(d, reg, val);
}

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
static int anysee_get_hw_info(struct dvb_usb_device *d, u8 *id)
{
	u8 buf[] = {CMD_GET_HW_INFO};
	return anysee_ctrl_msg(d, buf, sizeof(buf), id, 3);
}

static int anysee_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
{
	u8 buf[] = {CMD_STREAMING_CTRL, (u8)onoff, 0x00};
	deb_info("%s: onoff:%02x\n", __func__, onoff);
	return anysee_ctrl_msg(adap->dev, buf, sizeof(buf), NULL, 0);
}

static int anysee_led_ctrl(struct dvb_usb_device *d, u8 mode, u8 interval)
{
	u8 buf[] = {CMD_LED_AND_IR_CTRL, 0x01, mode, interval};
	deb_info("%s: state:%02x interval:%02x\n", __func__, mode, interval);
	return anysee_ctrl_msg(d, buf, sizeof(buf), NULL, 0);
}

static int anysee_ir_ctrl(struct dvb_usb_device *d, u8 onoff)
{
	u8 buf[] = {CMD_LED_AND_IR_CTRL, 0x02, onoff};
	deb_info("%s: onoff:%02x\n", __func__, onoff);
	return anysee_ctrl_msg(d, buf, sizeof(buf), NULL, 0);
}

static int anysee_init(struct dvb_usb_device *d)
{
	int ret;
	/* LED light */
	ret = anysee_led_ctrl(d, 0x01, 0x03);
	if (ret)
		return ret;

	/* enable IR */
	ret = anysee_ir_ctrl(d, 1);
	if (ret)
		return ret;

	return 0;
}

/* I2C */
static int anysee_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msg,
	int num)
{
	struct dvb_usb_device *d = i2c_get_adapdata(adap);
180
	int ret = 0, inc, i = 0;
181
	u8 buf[52]; /* 4 + 48 (I2C WR USB command header + I2C WR max) */
182 183 184 185 186 187

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

	while (i < num) {
		if (num > i + 1 && (msg[i+1].flags & I2C_M_RD)) {
188 189 190 191
			if (msg[i].len > 2 || msg[i+1].len > 60) {
				ret = -EOPNOTSUPP;
				break;
			}
192
			buf[0] = CMD_I2C_READ;
193
			buf[1] = (msg[i].addr << 1) | 0x01;
194
			buf[2] = msg[i].buf[0];
195 196
			buf[3] = msg[i].buf[1];
			buf[4] = msg[i].len-1;
197
			buf[5] = msg[i+1].len;
198
			ret = anysee_ctrl_msg(d, buf, 6, msg[i+1].buf,
199 200 201
				msg[i+1].len);
			inc = 2;
		} else {
202 203 204 205
			if (msg[i].len > 48) {
				ret = -EOPNOTSUPP;
				break;
			}
206
			buf[0] = CMD_I2C_WRITE;
207
			buf[1] = (msg[i].addr << 1);
208 209 210
			buf[2] = msg[i].len;
			buf[3] = 0x01;
			memcpy(&buf[4], msg[i].buf, msg[i].len);
211
			ret = anysee_ctrl_msg(d, buf, 4 + msg[i].len, NULL, 0);
212 213 214
			inc = 1;
		}
		if (ret)
215
			break;
216 217 218 219 220 221

		i += inc;
	}

	mutex_unlock(&d->i2c_mutex);

222
	return ret ? ret : i;
223 224 225 226 227 228 229 230 231 232 233 234 235 236
}

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

static struct i2c_algorithm anysee_i2c_algo = {
	.master_xfer   = anysee_master_xfer,
	.functionality = anysee_i2c_func,
};

static int anysee_mt352_demod_init(struct dvb_frontend *fe)
{
237 238 239 240 241
	static u8 clock_config[]   = { CLOCK_CTL,  0x38, 0x28 };
	static u8 reset[]          = { RESET,      0x80 };
	static u8 adc_ctl_1_cfg[]  = { ADC_CTL_1,  0x40 };
	static u8 agc_cfg[]        = { AGC_TARGET, 0x28, 0x20 };
	static u8 gpp_ctl_cfg[]    = { GPP_CTL,    0x33 };
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
	static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };

	mt352_write(fe, clock_config,   sizeof(clock_config));
	udelay(200);
	mt352_write(fe, reset,          sizeof(reset));
	mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));

	mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
	mt352_write(fe, gpp_ctl_cfg,    sizeof(gpp_ctl_cfg));
	mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));

	return 0;
}

/* Callbacks for DVB USB */
static struct tda10023_config anysee_tda10023_config = {
258
	.demod_address = (0x1a >> 1),
259 260 261 262 263
	.invert = 0,
	.xtal   = 16000000,
	.pll_m  = 11,
	.pll_p  = 3,
	.pll_n  = 1,
264 265
	.output_mode = TDA10023_OUTPUT_MODE_PARALLEL_C,
	.deltaf = 0xfeeb,
266 267 268
};

static struct mt352_config anysee_mt352_config = {
269
	.demod_address = (0x1e >> 1),
270 271 272 273
	.demod_init    = anysee_mt352_demod_init,
};

static struct zl10353_config anysee_zl10353_config = {
274
	.demod_address = (0x1e >> 1),
275 276 277
	.parallel_ts = 1,
};

278 279 280 281 282 283 284 285
static struct zl10353_config anysee_zl10353_tda18212_config2 = {
	.demod_address = (0x1e >> 1),
	.parallel_ts = 1,
	.disable_i2c_gate_ctrl = 1,
	.no_tuner = 1,
	.if2 = 41500,
};

286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
static struct zl10353_config anysee_zl10353_tda18212_config = {
	.demod_address = (0x18 >> 1),
	.parallel_ts = 1,
	.disable_i2c_gate_ctrl = 1,
	.no_tuner = 1,
	.if2 = 41500,
};

static struct tda10023_config anysee_tda10023_tda18212_config = {
	.demod_address = (0x1a >> 1),
	.xtal   = 16000000,
	.pll_m  = 12,
	.pll_p  = 3,
	.pll_n  = 1,
	.output_mode = TDA10023_OUTPUT_MODE_PARALLEL_C,
	.deltaf = 0xba02,
};

static struct tda18212_config anysee_tda18212_config = {
	.i2c_address = (0xc0 >> 1),
	.if_dvbt_6 = 4150,
	.if_dvbt_7 = 4150,
	.if_dvbt_8 = 4150,
	.if_dvbc = 5000,
};

312 313 314 315 316 317
static struct cx24116_config anysee_cx24116_config = {
	.demod_address = (0xaa >> 1),
	.mpg_clk_pos_pol = 0x00,
	.i2c_wr_max = 48,
};

318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
static struct stv0900_config anysee_stv0900_config = {
	.demod_address = (0xd0 >> 1),
	.demod_mode = 0,
	.xtal = 8000000,
	.clkmode = 3,
	.diseqc_mode = 2,
	.tun1_maddress = 0,
	.tun1_adc = 1, /* 1 Vpp */
	.path1_mode = 3,
};

static struct stv6110_config anysee_stv6110_config = {
	.i2c_address = (0xc0 >> 1),
	.mclk = 16000000,
	.clk_div = 1,
};

335 336 337 338 339 340 341
static struct isl6423_config anysee_isl6423_config = {
	.current_max = SEC_CURRENT_800m,
	.curlim  = SEC_CURRENT_LIM_OFF,
	.mod_extern = 1,
	.addr = (0x10 >> 1),
};

342 343 344 345 346 347
/*
 * New USB device strings: Mfr=1, Product=2, SerialNumber=0
 * Manufacturer: AMT.CO.KR
 *
 * E30 VID=04b4 PID=861f HW=2 FW=2.1 Product=????????
 * PCB: ?
348
 * parts: DNOS404ZH102A(MT352, DTT7579(?))
349 350 351
 *
 * E30 VID=04b4 PID=861f HW=2 FW=2.1 Product=????????
 * PCB: ?
352
 * parts: DNOS404ZH103A(ZL10353, DTT7579(?))
353 354 355
 *
 * E30 Plus VID=04b4 PID=861f HW=6 FW=1.0 "anysee"
 * PCB: 507CD (rev1.1)
356
 * parts: DNOS404ZH103A(ZL10353, DTT7579(?)), CST56I01
357
 * OEA=80 OEB=00 OEC=00 OED=ff OEF=fe
358
 * IOA=4f IOB=ff IOC=00 IOD=06 IOF=01
359 360 361 362 363 364
 * IOD[0] ZL10353 1=enabled
 * IOA[7] TS 0=enabled
 * tuner is not behind ZL10353 I2C-gate (no care if gate disabled or not)
 *
 * E30 C Plus VID=04b4 PID=861f HW=10 FW=1.0 "anysee-DC(LP)"
 * PCB: 507DC (rev0.2)
365
 * parts: TDA10023, DTOS403IH102B TM, CST56I01
366
 * OEA=80 OEB=00 OEC=00 OED=ff OEF=fe
367
 * IOA=4f IOB=ff IOC=00 IOD=26 IOF=01
368 369
 * IOD[0] TDA10023 1=enabled
 *
370 371 372 373 374 375 376
 * E30 S2 Plus VID=04b4 PID=861f HW=11 FW=0.1 "anysee-S2(LP)"
 * PCB: 507SI (rev2.1)
 * parts: BS2N10WCC01(CX24116, CX24118), ISL6423, TDA8024
 * OEA=80 OEB=00 OEC=ff OED=ff OEF=fe
 * IOA=4d IOB=ff IOC=00 IOD=26 IOF=01
 * IOD[0] CX24116 1=enabled
 *
377 378
 * E30 C Plus VID=1c73 PID=861f HW=15 FW=1.2 "anysee-FA(LP)"
 * PCB: 507FA (rev0.4)
379
 * parts: TDA10023, DTOS403IH102B TM, TDA8024
380
 * OEA=80 OEB=00 OEC=ff OED=ff OEF=ff
381
 * IOA=4d IOB=ff IOC=00 IOD=00 IOF=c0
382 383 384 385 386
 * IOD[5] TDA10023 1=enabled
 * IOE[0] tuner 1=enabled
 *
 * E30 Combo Plus VID=1c73 PID=861f HW=15 FW=1.2 "anysee-FA(LP)"
 * PCB: 507FA (rev1.1)
387
 * parts: ZL10353, TDA10023, DTOS403IH102B TM, TDA8024
388
 * OEA=80 OEB=00 OEC=ff OED=ff OEF=ff
389
 * IOA=4d IOB=ff IOC=00 IOD=00 IOF=c0
390 391 392 393 394 395 396
 * DVB-C:
 * IOD[5] TDA10023 1=enabled
 * IOE[0] tuner 1=enabled
 * DVB-T:
 * IOD[0] ZL10353 1=enabled
 * IOE[0] tuner 0=enabled
 * tuner is behind ZL10353 I2C-gate
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
 *
 * E7 TC VID=1c73 PID=861f HW=18 FW=0.7 AMTCI=0.5 "anysee-E7TC(LP)"
 * PCB: 508TC (rev0.6)
 * parts: ZL10353, TDA10023, DNOD44CDH086A(TDA18212)
 * OEA=80 OEB=00 OEC=03 OED=f7 OEF=ff
 * IOA=4d IOB=00 IOC=cc IOD=48 IOF=e4
 * IOA[7] TS 1=enabled
 * IOE[4] TDA18212 1=enabled
 * DVB-C:
 * IOD[6] ZL10353 0=disabled
 * IOD[5] TDA10023 1=enabled
 * IOE[0] IF 1=enabled
 * DVB-T:
 * IOD[5] TDA10023 0=disabled
 * IOD[6] ZL10353 1=enabled
 * IOE[0] IF 0=enabled
413 414 415 416 417 418 419 420 421
 *
 * E7 S2 VID=1c73 PID=861f HW=19 FW=0.4 AMTCI=0.5 "anysee-E7S2(LP)"
 * PCB: 508S2 (rev0.7)
 * parts: DNBU10512IST(STV0903, STV6110), ISL6423
 * OEA=80 OEB=00 OEC=03 OED=f7 OEF=ff
 * IOA=4d IOB=00 IOC=c4 IOD=08 IOF=e4
 * IOA[7] TS 1=enabled
 * IOE[5] STV0903 1=enabled
 *
422 423
 */

424 425 426 427 428
static int anysee_frontend_attach(struct dvb_usb_adapter *adap)
{
	int ret;
	struct anysee_state *state = adap->dev->priv;
	u8 hw_info[3];
429 430 431 432 433 434 435 436 437 438 439 440 441 442
	u8 tmp;
	struct i2c_msg msg[2] = {
		{
			.addr = anysee_tda18212_config.i2c_address,
			.flags = 0,
			.len = 1,
			.buf = "\x00",
		}, {
			.addr = anysee_tda18212_config.i2c_address,
			.flags = I2C_M_RD,
			.len = 1,
			.buf = &tmp,
		}
	};
443

444 445 446
	/* Check which hardware we have.
	 * We must do this call two times to get reliable values (hw bug).
	 */
447 448
	ret = anysee_get_hw_info(adap->dev, hw_info);
	if (ret)
449 450
		goto error;

451 452
	ret = anysee_get_hw_info(adap->dev, hw_info);
	if (ret)
453
		goto error;
454 455

	/* Meaning of these info bytes are guessed. */
456 457
	info("firmware version:%d.%d hardware id:%d",
		hw_info[1], hw_info[2], hw_info[0]);
458

459
	state->hw = hw_info[0];
460

461 462 463
	switch (state->hw) {
	case ANYSEE_HW_02: /* 2 */
		/* E30 */
464

465 466 467 468 469
		/* attach demod */
		adap->fe = dvb_attach(mt352_attach, &anysee_mt352_config,
			&adap->dev->i2c_adap);
		if (adap->fe)
			break;
470

471
		/* attach demod */
472
		adap->fe = dvb_attach(zl10353_attach, &anysee_zl10353_config,
473
			&adap->dev->i2c_adap);
474

475 476 477
		break;
	case ANYSEE_HW_507CD: /* 6 */
		/* E30 Plus */
478

479 480 481 482
		/* enable DVB-T demod on IOD[0] */
		ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (1 << 0), 0x01);
		if (ret)
			goto error;
483

484 485 486 487
		/* enable transport stream on IOA[7] */
		ret = anysee_wr_reg_mask(adap->dev, REG_IOA, (0 << 7), 0x80);
		if (ret)
			goto error;
488

489 490 491
		/* attach demod */
		adap->fe = dvb_attach(zl10353_attach, &anysee_zl10353_config,
			&adap->dev->i2c_adap);
492

493 494 495 496 497 498 499 500 501 502 503 504
		break;
	case ANYSEE_HW_507DC: /* 10 */
		/* E30 C Plus */

		/* enable DVB-C demod on IOD[0] */
		ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (1 << 0), 0x01);
		if (ret)
			goto error;

		/* attach demod */
		adap->fe = dvb_attach(tda10023_attach, &anysee_tda10023_config,
			&adap->dev->i2c_adap, 0x48);
505

506 507 508 509 510 511 512 513 514 515 516 517 518
		break;
	case ANYSEE_HW_507SI: /* 11 */
		/* E30 S2 Plus */

		/* enable DVB-S/S2 demod on IOD[0] */
		ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (1 << 0), 0x01);
		if (ret)
			goto error;

		/* attach demod */
		adap->fe = dvb_attach(cx24116_attach, &anysee_cx24116_config,
			&adap->dev->i2c_adap);

519 520 521 522 523
		break;
	case ANYSEE_HW_507FA: /* 15 */
		/* E30 Combo Plus */
		/* E30 C Plus */

524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
		/* enable tuner on IOE[4] */
		ret = anysee_wr_reg_mask(adap->dev, REG_IOE, (1 << 4), 0x10);
		if (ret)
			goto error;

		/* probe TDA18212 */
		tmp = 0;
		ret = i2c_transfer(&adap->dev->i2c_adap, msg, 2);
		if (ret == 2 && tmp == 0xc7)
			deb_info("%s: TDA18212 found\n", __func__);
		else
			tmp = 0;

		/* disable tuner on IOE[4] */
		ret = anysee_wr_reg_mask(adap->dev, REG_IOE, (0 << 4), 0x10);
		if (ret)
			goto error;

542 543 544 545 546 547 548 549 550 551 552 553 554 555
		if (dvb_usb_anysee_delsys) {
			/* disable DVB-C demod on IOD[5] */
			ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (0 << 5),
				0x20);
			if (ret)
				goto error;

			/* enable DVB-T demod on IOD[0] */
			ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (1 << 0),
				0x01);
			if (ret)
				goto error;

			/* attach demod */
556 557 558
			if (tmp == 0xc7) {
				/* TDA18212 config */
				adap->fe = dvb_attach(zl10353_attach,
559
					&anysee_zl10353_tda18212_config2,
560 561 562 563 564 565 566
					&adap->dev->i2c_adap);
			} else {
				/* PLL config */
				adap->fe = dvb_attach(zl10353_attach,
					&anysee_zl10353_config,
					&adap->dev->i2c_adap);
			}
567 568 569 570 571 572 573 574 575 576 577 578 579 580
		} else {
			/* disable DVB-T demod on IOD[0] */
			ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (0 << 0),
				0x01);
			if (ret)
				goto error;

			/* enable DVB-C demod on IOD[5] */
			ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (1 << 5),
				0x20);
			if (ret)
				goto error;

			/* attach demod */
581 582 583 584 585 586 587 588 589 590 591
			if (tmp == 0xc7) {
				/* TDA18212 config */
				adap->fe = dvb_attach(tda10023_attach,
					&anysee_tda10023_tda18212_config,
					&adap->dev->i2c_adap, 0x48);
			} else {
				/* PLL config */
				adap->fe = dvb_attach(tda10023_attach,
					&anysee_tda10023_config,
					&adap->dev->i2c_adap, 0x48);
			}
592
		}
593

594
		break;
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 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
	case ANYSEE_HW_508TC: /* 18 */
		/* E7 TC */

		/* enable transport stream on IOA[7] */
		ret = anysee_wr_reg_mask(adap->dev, REG_IOA, (1 << 7), 0x80);
		if (ret)
			goto error;

		if (dvb_usb_anysee_delsys) {
			/* disable DVB-C demod on IOD[5] */
			ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (0 << 5),
				0x20);
			if (ret)
				goto error;

			/* enable DVB-T demod on IOD[6] */
			ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (1 << 6),
				0x40);
			if (ret)
				goto error;

			/* enable IF route on IOE[0] */
			ret = anysee_wr_reg_mask(adap->dev, REG_IOE, (0 << 0),
				0x01);
			if (ret)
				goto error;

			/* attach demod */
			adap->fe = dvb_attach(zl10353_attach,
				&anysee_zl10353_tda18212_config,
				&adap->dev->i2c_adap);
		} else {
			/* disable DVB-T demod on IOD[6] */
			ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (0 << 6),
				0x40);
			if (ret)
				goto error;

			/* enable DVB-C demod on IOD[5] */
			ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (1 << 5),
				0x20);
			if (ret)
				goto error;

			/* enable IF route on IOE[0] */
			ret = anysee_wr_reg_mask(adap->dev, REG_IOE, (1 << 0),
				0x01);
			if (ret)
				goto error;

			/* attach demod */
			adap->fe = dvb_attach(tda10023_attach,
				&anysee_tda10023_tda18212_config,
				&adap->dev->i2c_adap, 0x48);
		}
650

651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
		break;
	case ANYSEE_HW_508S2: /* 19 */
		/* E7 S2 */

		/* enable transport stream on IOA[7] */
		ret = anysee_wr_reg_mask(adap->dev, REG_IOA, (1 << 7), 0x80);
		if (ret)
			goto error;

		/* enable DVB-S/S2 demod on IOE[5] */
		ret = anysee_wr_reg_mask(adap->dev, REG_IOE, (1 << 5), 0x20);
		if (ret)
			goto error;

		/* attach demod */
		adap->fe = dvb_attach(stv0900_attach, &anysee_stv0900_config,
			&adap->dev->i2c_adap, 0);

669
		break;
670
	}
671

672 673 674
	if (!adap->fe) {
		/* we have no frontend :-( */
		ret = -ENODEV;
675 676
		err("Unsupported Anysee version. " \
			"Please report the <linux-media@vger.kernel.org>.");
677 678 679
	}
error:
	return ret;
680 681 682 683 684
}

static int anysee_tuner_attach(struct dvb_usb_adapter *adap)
{
	struct anysee_state *state = adap->dev->priv;
685
	struct dvb_frontend *fe;
686
	int ret;
687
	deb_info("%s:\n", __func__);
688

689 690 691 692 693
	switch (state->hw) {
	case ANYSEE_HW_02: /* 2 */
		/* E30 */

		/* attach tuner */
694
		fe = dvb_attach(dvb_pll_attach, adap->fe, (0xc2 >> 1),
695
			NULL, DVB_PLL_THOMSON_DTT7579);
696

697
		break;
698 699 700 701
	case ANYSEE_HW_507CD: /* 6 */
		/* E30 Plus */

		/* attach tuner */
702
		fe = dvb_attach(dvb_pll_attach, adap->fe, (0xc2 >> 1),
703 704 705 706 707 708 709
			&adap->dev->i2c_adap, DVB_PLL_THOMSON_DTT7579);

		break;
	case ANYSEE_HW_507DC: /* 10 */
		/* E30 C Plus */

		/* attach tuner */
710
		fe = dvb_attach(dvb_pll_attach, adap->fe, (0xc0 >> 1),
711
			&adap->dev->i2c_adap, DVB_PLL_SAMSUNG_DTOS403IH102A);
712

713 714 715 716 717 718 719 720
		break;
	case ANYSEE_HW_507SI: /* 11 */
		/* E30 S2 Plus */

		/* attach LNB controller */
		fe = dvb_attach(isl6423_attach, adap->fe, &adap->dev->i2c_adap,
			&anysee_isl6423_config);

721 722 723 724 725
		break;
	case ANYSEE_HW_507FA: /* 15 */
		/* E30 Combo Plus */
		/* E30 C Plus */

726 727 728 729 730 731 732 733 734 735 736 737 738 739
		if (dvb_usb_anysee_delsys) {
			/* enable DVB-T tuner on IOE[0] */
			ret = anysee_wr_reg_mask(adap->dev, REG_IOE, (0 << 0),
				0x01);
			if (ret)
				goto error;
		} else {
			/* enable DVB-C tuner on IOE[0] */
			ret = anysee_wr_reg_mask(adap->dev, REG_IOE, (1 << 0),
				0x01);
			if (ret)
				goto error;
		}

740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
		/* Try first attach TDA18212 silicon tuner on IOE[4], if that
		 * fails attach old simple PLL. */

		/* enable tuner on IOE[4] */
		ret = anysee_wr_reg_mask(adap->dev, REG_IOE, (1 << 4), 0x10);
		if (ret)
			goto error;

		/* attach tuner */
		fe = dvb_attach(tda18212_attach, adap->fe, &adap->dev->i2c_adap,
			&anysee_tda18212_config);
		if (fe)
			break;

		/* disable tuner on IOE[4] */
		ret = anysee_wr_reg_mask(adap->dev, REG_IOE, (0 << 4), 0x10);
		if (ret)
			goto error;

759
		/* attach tuner */
760
		fe = dvb_attach(dvb_pll_attach, adap->fe, (0xc0 >> 1),
761
			&adap->dev->i2c_adap, DVB_PLL_SAMSUNG_DTOS403IH102A);
762

763
		break;
764 765 766 767 768 769 770 771 772 773 774 775
	case ANYSEE_HW_508TC: /* 18 */
		/* E7 TC */

		/* enable tuner on IOE[4] */
		ret = anysee_wr_reg_mask(adap->dev, REG_IOE, (1 << 4), 0x10);
		if (ret)
			goto error;

		/* attach tuner */
		fe = dvb_attach(tda18212_attach, adap->fe, &adap->dev->i2c_adap,
			&anysee_tda18212_config);

776 777 778 779 780 781 782 783 784 785 786 787 788 789
		break;
	case ANYSEE_HW_508S2: /* 19 */
		/* E7 S2 */

		/* attach tuner */
		fe = dvb_attach(stv6110_attach, adap->fe,
			&anysee_stv6110_config, &adap->dev->i2c_adap);

		if (fe) {
			/* attach LNB controller */
			fe = dvb_attach(isl6423_attach, adap->fe,
				&adap->dev->i2c_adap, &anysee_isl6423_config);
		}

790
		break;
791
	default:
792
		fe = NULL;
793 794
	}

795 796 797 798 799
	if (fe)
		ret = 0;
	else
		ret = -ENODEV;

800 801
error:
	return ret;
802 803
}

804
static int anysee_rc_query(struct dvb_usb_device *d)
805 806 807
{
	u8 buf[] = {CMD_GET_IR_CODE};
	u8 ircode[2];
808 809 810 811 812 813 814 815 816
	int ret;

	/* Remote controller is basic NEC using address byte 0x08.
	   Anysee device RC query returns only two bytes, status and code,
	   address byte is dropped. Also it does not return any value for
	   NEC RCs having address byte other than 0x08. Due to that, we
	   cannot use that device as standard NEC receiver.
	   It could be possible make hack which reads whole code directly
	   from device memory... */
817

818
	ret = anysee_ctrl_msg(d, buf, sizeof(buf), ircode, sizeof(ircode));
819 820 821
	if (ret)
		return ret;

822 823
	if (ircode[0]) {
		deb_rc("%s: key pressed %02x\n", __func__, ircode[1]);
824
		rc_keydown(d->rc_dev, 0x08 << 8 | ircode[1], 0);
825
	}
826

827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
	return 0;
}

/* DVB USB Driver stuff */
static struct dvb_usb_device_properties anysee_properties;

static int anysee_probe(struct usb_interface *intf,
			const struct usb_device_id *id)
{
	struct dvb_usb_device *d;
	struct usb_host_interface *alt;
	int ret;

	/* There is one interface with two alternate settings.
	   Alternate setting 0 is for bulk transfer.
	   Alternate setting 1 is for isochronous transfer.
	   We use bulk transfer (alternate setting 0). */
	if (intf->num_altsetting < 1)
		return -ENODEV;

847 848 849 850 851
	/*
	 * Anysee is always warm (its USB-bridge, Cypress FX2, uploads
	 * firmware from eeprom).  If dvb_usb_device_init() succeeds that
	 * means d is a valid pointer.
	 */
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
	ret = dvb_usb_device_init(intf, &anysee_properties, THIS_MODULE, &d,
		adapter_nr);
	if (ret)
		return ret;

	alt = usb_altnum_to_altsetting(intf, 0);
	if (alt == NULL) {
		deb_info("%s: no alt found!\n", __func__);
		return -ENODEV;
	}

	ret = usb_set_interface(d->udev, alt->desc.bInterfaceNumber,
		alt->desc.bAlternateSetting);
	if (ret)
		return ret;

868
	return anysee_init(d);
869 870
}

871
static struct usb_device_id anysee_table[] = {
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
	{ USB_DEVICE(USB_VID_CYPRESS, USB_PID_ANYSEE) },
	{ USB_DEVICE(USB_VID_AMT,     USB_PID_ANYSEE) },
	{ }		/* Terminating entry */
};
MODULE_DEVICE_TABLE(usb, anysee_table);

static struct dvb_usb_device_properties anysee_properties = {
	.caps             = DVB_USB_IS_AN_I2C_ADAPTER,

	.usb_ctrl         = DEVICE_SPECIFIC,

	.size_of_priv     = sizeof(struct anysee_state),

	.num_adapters = 1,
	.adapter = {
		{
			.streaming_ctrl   = anysee_streaming_ctrl,
			.frontend_attach  = anysee_frontend_attach,
			.tuner_attach     = anysee_tuner_attach,
			.stream = {
				.type = USB_BULK,
				.count = 8,
				.endpoint = 0x82,
				.u = {
					.bulk = {
897
						.buffersize = (16*512),
898 899 900 901 902 903
					}
				}
			},
		}
	},

904 905
	.rc.core = {
		.rc_codes         = RC_MAP_ANYSEE,
906
		.protocol         = RC_TYPE_OTHER,
907
		.module_name      = "anysee",
908
		.rc_query         = anysee_rc_query,
909
		.rc_interval      = 250,  /* windows driver uses 500ms */
910
	},
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957

	.i2c_algo         = &anysee_i2c_algo,

	.generic_bulk_ctrl_endpoint = 1,

	.num_device_descs = 1,
	.devices = {
		{
			.name = "Anysee DVB USB2.0",
			.cold_ids = {NULL},
			.warm_ids = {&anysee_table[0],
				     &anysee_table[1], NULL},
		},
	}
};

static struct usb_driver anysee_driver = {
	.name       = "dvb_usb_anysee",
	.probe      = anysee_probe,
	.disconnect = dvb_usb_device_exit,
	.id_table   = anysee_table,
};

/* module stuff */
static int __init anysee_module_init(void)
{
	int ret;

	ret = usb_register(&anysee_driver);
	if (ret)
		err("%s: usb_register failed. Error number %d", __func__, ret);

	return ret;
}

static void __exit anysee_module_exit(void)
{
	/* deregister this driver from the USB subsystem */
	usb_deregister(&anysee_driver);
}

module_init(anysee_module_init);
module_exit(anysee_module_exit);

MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
MODULE_DESCRIPTION("Driver Anysee E30 DVB-C & DVB-T USB2.0");
MODULE_LICENSE("GPL");