anysee.c 31.6 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
 * with any ISO 7816 smart card.
 *
 * Any help according to implement serial smart card reader support
 * is highly welcome!
 */

#include "anysee.h"
35
#include "dvb-pll.h"
36 37 38 39
#include "tda1002x.h"
#include "mt352.h"
#include "mt352_priv.h"
#include "zl10353.h"
40
#include "tda18212.h"
41
#include "cx24116.h"
42 43
#include "stv0900.h"
#include "stv6110.h"
44
#include "isl6423.h"
45
#include "cxd2820r.h"
46 47 48 49 50 51 52

/* 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);
DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);

53
static DEFINE_MUTEX(anysee_usb_mutex);
54 55 56 57

static int anysee_ctrl_msg(struct dvb_usb_device *d, u8 *sbuf, u8 slen,
	u8 *rbuf, u8 rlen)
{
58
	struct anysee_state *state = d_to_priv(d);
59
	int act_len, ret, i;
60 61 62 63 64
	u8 buf[64];

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

65
	mutex_lock(&anysee_usb_mutex);
66

67 68 69
	deb_xfer(">>> ");
	debug_dump(buf, slen, deb_xfer);

70 71
	/* We need receive one message more after dvb_usb_generic_rw due
	   to weird transaction flow, which is 1 x send + 2 x receive. */
72
	ret = dvb_usbv2_generic_rw(d, buf, sizeof(buf), buf, sizeof(buf));
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
	if (ret)
		goto error_unlock;

	/* TODO FIXME: dvb_usb_generic_rw() fails rarely with error code -32
	 * (EPIPE, Broken pipe). Function supports currently msleep() as a
	 * parameter but I would not like to use it, since according to
	 * Documentation/timers/timers-howto.txt it should not be used such
	 * short, under < 20ms, sleeps. Repeating failed message would be
	 * better choice as not to add unwanted delays...
	 * Fixing that correctly is one of those or both;
	 * 1) use repeat if possible
	 * 2) add suitable delay
	 */

	/* get answer, retry few times if error returned */
	for (i = 0; i < 3; i++) {
89 90
		/* receive 2nd answer */
		ret = usb_bulk_msg(d->udev, usb_rcvbulkpipe(d->udev,
91
			d->props->generic_bulk_ctrl_endpoint), buf, sizeof(buf),
92
			&act_len, 2000);
93 94 95 96 97

		if (ret) {
			deb_info("%s: recv bulk message failed: %d",
					__func__, ret);
		} else {
98
			deb_xfer("<<< ");
99 100 101 102
			debug_dump(buf, rlen, deb_xfer);

			if (buf[63] != 0x4f)
				deb_info("%s: cmd failed\n", __func__);
103 104

			break;
105 106 107
		}
	}

108 109 110 111 112 113
	if (ret) {
		/* all retries failed, it is fatal */
		err("%s: recv bulk message failed: %d", __func__, ret);
		goto error_unlock;
	}

114
	/* read request, copy returned data to return buf */
115
	if (rbuf && rlen)
116 117
		memcpy(rbuf, buf, rlen);

118
error_unlock:
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
	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);
}

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
/* 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);
}

A
Antti Palosaari 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
/* read single register with mask */
static int anysee_rd_reg_mask(struct dvb_usb_device *d, u16 reg, u8 *val,
	u8 mask)
{
	int ret, i;
	u8 tmp;

	ret = anysee_read_reg(d, reg, &tmp);
	if (ret)
		return ret;

	tmp &= mask;

	/* find position of the first bit */
	for (i = 0; i < 8; i++) {
		if ((mask >> i) & 0x01)
			break;
	}
	*val = tmp >> i;

	return 0;
}

184 185 186 187 188 189
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);
}

190
static int anysee_streaming_ctrl(struct dvb_frontend *fe, int onoff)
191 192 193
{
	u8 buf[] = {CMD_STREAMING_CTRL, (u8)onoff, 0x00};
	deb_info("%s: onoff:%02x\n", __func__, onoff);
194
	return anysee_ctrl_msg(fe_to_d(fe), buf, sizeof(buf), NULL, 0);
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
}

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

/* 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);
216
	int ret = 0, inc, i = 0;
217
	u8 buf[52]; /* 4 + 48 (I2C WR USB command header + I2C WR max) */
218 219 220 221 222 223

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

	while (i < num) {
		if (num > i + 1 && (msg[i+1].flags & I2C_M_RD)) {
224 225 226 227
			if (msg[i].len > 2 || msg[i+1].len > 60) {
				ret = -EOPNOTSUPP;
				break;
			}
228
			buf[0] = CMD_I2C_READ;
229
			buf[1] = (msg[i].addr << 1) | 0x01;
230
			buf[2] = msg[i].buf[0];
231 232
			buf[3] = msg[i].buf[1];
			buf[4] = msg[i].len-1;
233
			buf[5] = msg[i+1].len;
234
			ret = anysee_ctrl_msg(d, buf, 6, msg[i+1].buf,
235 236 237
				msg[i+1].len);
			inc = 2;
		} else {
238 239 240 241
			if (msg[i].len > 48) {
				ret = -EOPNOTSUPP;
				break;
			}
242
			buf[0] = CMD_I2C_WRITE;
243
			buf[1] = (msg[i].addr << 1);
244 245 246
			buf[2] = msg[i].len;
			buf[3] = 0x01;
			memcpy(&buf[4], msg[i].buf, msg[i].len);
247
			ret = anysee_ctrl_msg(d, buf, 4 + msg[i].len, NULL, 0);
248 249 250
			inc = 1;
		}
		if (ret)
251
			break;
252 253 254 255 256 257

		i += inc;
	}

	mutex_unlock(&d->i2c_mutex);

258
	return ret ? ret : i;
259 260 261 262 263 264 265 266 267 268 269 270 271 272
}

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)
{
273 274 275 276 277
	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 };
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
	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 = {
294
	.demod_address = (0x1a >> 1),
295 296 297 298 299
	.invert = 0,
	.xtal   = 16000000,
	.pll_m  = 11,
	.pll_p  = 3,
	.pll_n  = 1,
300 301
	.output_mode = TDA10023_OUTPUT_MODE_PARALLEL_C,
	.deltaf = 0xfeeb,
302 303 304
};

static struct mt352_config anysee_mt352_config = {
305
	.demod_address = (0x1e >> 1),
306 307 308 309
	.demod_init    = anysee_mt352_demod_init,
};

static struct zl10353_config anysee_zl10353_config = {
310
	.demod_address = (0x1e >> 1),
311 312 313
	.parallel_ts = 1,
};

314 315 316 317 318 319 320 321
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,
};

322 323 324 325 326 327 328 329 330 331 332 333 334 335
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,
A
Antti Palosaari 已提交
336
	.output_mode = TDA10023_OUTPUT_MODE_PARALLEL_B,
337 338 339 340 341 342 343 344 345 346 347
	.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,
};

348 349 350 351 352 353 354 355 356 357 358
static struct tda18212_config anysee_tda18212_config2 = {
	.i2c_address = 0x60 /* (0xc0 >> 1) */,
	.if_dvbt_6 = 3550,
	.if_dvbt_7 = 3700,
	.if_dvbt_8 = 4150,
	.if_dvbt2_6 = 3250,
	.if_dvbt2_7 = 4000,
	.if_dvbt2_8 = 4000,
	.if_dvbc = 5000,
};

359 360 361 362 363 364
static struct cx24116_config anysee_cx24116_config = {
	.demod_address = (0xaa >> 1),
	.mpg_clk_pos_pol = 0x00,
	.i2c_wr_max = 48,
};

365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
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,
};

382 383 384 385 386 387 388
static struct isl6423_config anysee_isl6423_config = {
	.current_max = SEC_CURRENT_800m,
	.curlim  = SEC_CURRENT_LIM_OFF,
	.mod_extern = 1,
	.addr = (0x10 >> 1),
};

389 390 391 392 393
static struct cxd2820r_config anysee_cxd2820r_config = {
	.i2c_address = 0x6d, /* (0xda >> 1) */
	.ts_mode = 0x38,
};

394 395 396 397 398 399
/*
 * 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: ?
400
 * parts: DNOS404ZH102A(MT352, DTT7579(?))
401
 *
402 403
 * E30 VID=04b4 PID=861f HW=2 FW=2.1 "anysee-T(LP)"
 * PCB: PCB 507T (rev1.61)
404
 * parts: DNOS404ZH103A(ZL10353, DTT7579(?))
405 406
 * OEA=0a OEB=00 OEC=00 OED=ff OEE=00
 * IOA=45 IOB=ff IOC=00 IOD=ff IOE=00
407 408 409
 *
 * E30 Plus VID=04b4 PID=861f HW=6 FW=1.0 "anysee"
 * PCB: 507CD (rev1.1)
410
 * parts: DNOS404ZH103A(ZL10353, DTT7579(?)), CST56I01
411 412
 * OEA=80 OEB=00 OEC=00 OED=ff OEE=fe
 * IOA=4f IOB=ff IOC=00 IOD=06 IOE=01
413 414 415 416 417 418
 * 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)
419
 * parts: TDA10023, DTOS403IH102B TM, CST56I01
420 421
 * OEA=80 OEB=00 OEC=00 OED=ff OEE=fe
 * IOA=4f IOB=ff IOC=00 IOD=26 IOE=01
422 423
 * IOD[0] TDA10023 1=enabled
 *
424 425 426
 * 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
427 428
 * OEA=80 OEB=00 OEC=ff OED=ff OEE=fe
 * IOA=4d IOB=ff IOC=00 IOD=26 IOE=01
429 430
 * IOD[0] CX24116 1=enabled
 *
431 432
 * E30 C Plus VID=1c73 PID=861f HW=15 FW=1.2 "anysee-FA(LP)"
 * PCB: 507FA (rev0.4)
433
 * parts: TDA10023, DTOS403IH102B TM, TDA8024
434 435
 * OEA=80 OEB=00 OEC=ff OED=ff OEE=ff
 * IOA=4d IOB=ff IOC=00 IOD=00 IOE=c0
436 437 438 439 440
 * 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)
441
 * parts: ZL10353, TDA10023, DTOS403IH102B TM, TDA8024
442 443
 * OEA=80 OEB=00 OEC=ff OED=ff OEE=ff
 * IOA=4d IOB=ff IOC=00 IOD=00 IOE=c0
444 445 446 447 448 449 450
 * 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
451 452 453 454
 *
 * 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)
455 456
 * OEA=80 OEB=00 OEC=03 OED=f7 OEE=ff
 * IOA=4d IOB=00 IOC=cc IOD=48 IOE=e4
457 458 459 460 461 462 463 464 465 466
 * 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
467 468 469 470
 *
 * 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
471 472
 * OEA=80 OEB=00 OEC=03 OED=f7 OEE=ff
 * IOA=4d IOB=00 IOC=c4 IOD=08 IOE=e4
473 474 475
 * IOA[7] TS 1=enabled
 * IOE[5] STV0903 1=enabled
 *
476 477 478 479 480 481 482 483
 * E7 T2C VID=1c73 PID=861f HW=20 FW=0.1 AMTCI=0.5 "anysee-E7T2C(LP)"
 * PCB: 508T2C (rev0.3)
 * parts: DNOQ44QCH106A(CXD2820R, TDA18212), TDA8024
 * OEA=80 OEB=00 OEC=03 OED=f7 OEE=ff
 * IOA=4d IOB=00 IOC=cc IOD=48 IOE=e4
 * IOA[7] TS 1=enabled
 * IOE[5] CXD2820R 1=enabled
 *
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
 * E7 PTC VID=1c73 PID=861f HW=21 FW=0.1 AMTCI=?? "anysee-E7PTC(LP)"
 * PCB: 508PTC (rev0.5)
 * parts: ZL10353, TDA10023, DNOD44CDH086A(TDA18212)
 * OEA=80 OEB=00 OEC=03 OED=f7 OEE=ff
 * IOA=4d IOB=00 IOC=cc IOD=48 IOE=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
499
 *
500
 * E7 PS2 VID=1c73 PID=861f HW=22 FW=0.1 AMTCI=?? "anysee-E7PS2(LP)"
501 502 503 504 505 506
 * PCB: 508PS2 (rev0.4)
 * parts: DNBU10512IST(STV0903, STV6110), ISL6423
 * OEA=80 OEB=00 OEC=03 OED=f7 OEE=ff
 * IOA=4d IOB=00 IOC=c4 IOD=08 IOE=e4
 * IOA[7] TS 1=enabled
 * IOE[5] STV0903 1=enabled
507 508
 */

509 510
static int anysee_read_config(struct dvb_usb_device *d)
{
511
	struct anysee_state *state = d_to_priv(d);
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
	int ret;
	u8 hw_info[3];

	/*
	 * Check which hardware we have.
	 * We must do this call two times to get reliable values (hw/fw bug).
	 */
	ret = anysee_get_hw_info(d, hw_info);
	if (ret)
		goto error;

	ret = anysee_get_hw_info(d, hw_info);
	if (ret)
		goto error;

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

	state->hw = hw_info[0];
error:
	return ret;
}
535 536 537 538 539

/* external I2C gate used for DNOD44CDH086A(TDA18212) tuner module */
static int anysee_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
{
	/* enable / disable tuner access on IOE[4] */
540
	return anysee_wr_reg_mask(fe_to_d(fe), REG_IOE, (enable << 4), 0x10);
541 542
}

543 544
static int anysee_frontend_ctrl(struct dvb_frontend *fe, int onoff)
{
545 546
	struct anysee_state *state = fe_to_priv(fe);
	struct dvb_usb_device *d = fe_to_d(fe);
547 548 549 550 551 552 553 554 555 556 557 558 559
	int ret;

	deb_info("%s: fe=%d onoff=%d\n", __func__, fe->id, onoff);

	/* no frontend sleep control */
	if (onoff == 0)
		return 0;

	switch (state->hw) {
	case ANYSEE_HW_507FA: /* 15 */
		/* E30 Combo Plus */
		/* E30 C Plus */

560
		if (fe->id == 0)  {
561
			/* disable DVB-T demod on IOD[0] */
562
			ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 0), 0x01);
563 564 565 566
			if (ret)
				goto error;

			/* enable DVB-C demod on IOD[5] */
567
			ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 5), 0x20);
568 569 570 571
			if (ret)
				goto error;

			/* enable DVB-C tuner on IOE[0] */
572
			ret = anysee_wr_reg_mask(d, REG_IOE, (1 << 0), 0x01);
573 574 575 576
			if (ret)
				goto error;
		} else {
			/* disable DVB-C demod on IOD[5] */
577
			ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 5), 0x20);
578 579 580 581
			if (ret)
				goto error;

			/* enable DVB-T demod on IOD[0] */
582
			ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 0), 0x01);
583 584 585 586
			if (ret)
				goto error;

			/* enable DVB-T tuner on IOE[0] */
587
			ret = anysee_wr_reg_mask(d, REG_IOE, (0 << 0), 0x01);
588 589 590 591 592 593 594 595 596 597
			if (ret)
				goto error;
		}

		break;
	case ANYSEE_HW_508TC: /* 18 */
	case ANYSEE_HW_508PTC: /* 21 */
		/* E7 TC */
		/* E7 PTC */

598
		if (fe->id == 0)  {
599
			/* disable DVB-T demod on IOD[6] */
600
			ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 6), 0x40);
601 602 603 604
			if (ret)
				goto error;

			/* enable DVB-C demod on IOD[5] */
605
			ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 5), 0x20);
606 607 608 609
			if (ret)
				goto error;

			/* enable IF route on IOE[0] */
610
			ret = anysee_wr_reg_mask(d, REG_IOE, (1 << 0), 0x01);
611 612 613 614
			if (ret)
				goto error;
		} else {
			/* disable DVB-C demod on IOD[5] */
615
			ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 5), 0x20);
616 617 618 619
			if (ret)
				goto error;

			/* enable DVB-T demod on IOD[6] */
620
			ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 6), 0x40);
621 622 623 624
			if (ret)
				goto error;

			/* enable IF route on IOE[0] */
625
			ret = anysee_wr_reg_mask(d, REG_IOE, (0 << 0), 0x01);
626 627 628 629 630 631 632 633 634 635 636 637 638
			if (ret)
				goto error;
		}

		break;
	default:
		ret = 0;
	}

error:
	return ret;
}

639 640
static int anysee_frontend_attach(struct dvb_usb_adapter *adap)
{
641 642
	struct anysee_state *state = adap_to_priv(adap);
	struct dvb_usb_device *d = adap_to_d(adap);
643
	int ret;
644 645 646 647 648 649 650 651 652 653 654 655 656 657
	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,
		}
	};
658

659
	switch (state->hw) {
660
	case ANYSEE_HW_507T: /* 2 */
661
		/* E30 */
662

663
		/* attach demod */
664 665
		adap->fe[0] = dvb_attach(mt352_attach, &anysee_mt352_config,
				&d->i2c_adap);
666
		if (adap->fe[0])
667
			break;
668

669
		/* attach demod */
670 671
		adap->fe[0] = dvb_attach(zl10353_attach, &anysee_zl10353_config,
				&d->i2c_adap);
672

673 674 675
		break;
	case ANYSEE_HW_507CD: /* 6 */
		/* E30 Plus */
676

677
		/* enable DVB-T demod on IOD[0] */
678
		ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 0), 0x01);
679 680
		if (ret)
			goto error;
681

682
		/* enable transport stream on IOA[7] */
683
		ret = anysee_wr_reg_mask(d, REG_IOA, (0 << 7), 0x80);
684 685
		if (ret)
			goto error;
686

687
		/* attach demod */
688 689
		adap->fe[0] = dvb_attach(zl10353_attach, &anysee_zl10353_config,
				&d->i2c_adap);
690

691 692 693 694 695
		break;
	case ANYSEE_HW_507DC: /* 10 */
		/* E30 C Plus */

		/* enable DVB-C demod on IOD[0] */
696
		ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 0), 0x01);
697 698 699 700
		if (ret)
			goto error;

		/* attach demod */
701
		adap->fe[0] = dvb_attach(tda10023_attach,
702
				&anysee_tda10023_config, &d->i2c_adap, 0x48);
703

704 705 706 707 708
		break;
	case ANYSEE_HW_507SI: /* 11 */
		/* E30 S2 Plus */

		/* enable DVB-S/S2 demod on IOD[0] */
709
		ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 0), 0x01);
710 711 712 713
		if (ret)
			goto error;

		/* attach demod */
714 715
		adap->fe[0] = dvb_attach(cx24116_attach, &anysee_cx24116_config,
				&d->i2c_adap);
716

717 718 719 720 721
		break;
	case ANYSEE_HW_507FA: /* 15 */
		/* E30 Combo Plus */
		/* E30 C Plus */

722
		/* enable tuner on IOE[4] */
723
		ret = anysee_wr_reg_mask(d, REG_IOE, (1 << 4), 0x10);
724 725 726 727 728
		if (ret)
			goto error;

		/* probe TDA18212 */
		tmp = 0;
729
		ret = i2c_transfer(&d->i2c_adap, msg, 2);
730 731 732 733 734 735
		if (ret == 2 && tmp == 0xc7)
			deb_info("%s: TDA18212 found\n", __func__);
		else
			tmp = 0;

		/* disable tuner on IOE[4] */
736
		ret = anysee_wr_reg_mask(d, REG_IOE, (0 << 4), 0x10);
737 738 739
		if (ret)
			goto error;

740
		/* disable DVB-T demod on IOD[0] */
741
		ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 0), 0x01);
742 743
		if (ret)
			goto error;
744

745
		/* enable DVB-C demod on IOD[5] */
746
		ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 5), 0x20);
747 748
		if (ret)
			goto error;
749

750 751 752 753
		/* attach demod */
		if (tmp == 0xc7) {
			/* TDA18212 config */
			adap->fe[0] = dvb_attach(tda10023_attach,
754
					&anysee_tda10023_tda18212_config,
755
					&d->i2c_adap, 0x48);
756 757 758 759 760 761 762 763

			/* I2C gate for DNOD44CDH086A(TDA18212) tuner module */
			if (adap->fe[0])
				adap->fe[0]->ops.i2c_gate_ctrl =
						anysee_i2c_gate_ctrl;
		} else {
			/* PLL config */
			adap->fe[0] = dvb_attach(tda10023_attach,
764
					&anysee_tda10023_config,
765
					&d->i2c_adap, 0x48);
766
		}
767

768 769 770 771 772
		/* break out if first frontend attaching fails */
		if (!adap->fe[0])
			break;

		/* disable DVB-C demod on IOD[5] */
773
		ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 5), 0x20);
774 775 776 777
		if (ret)
			goto error;

		/* enable DVB-T demod on IOD[0] */
778
		ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 0), 0x01);
779 780
		if (ret)
			goto error;
781

782 783 784 785
		/* attach demod */
		if (tmp == 0xc7) {
			/* TDA18212 config */
			adap->fe[1] = dvb_attach(zl10353_attach,
786
					&anysee_zl10353_tda18212_config2,
787
					&d->i2c_adap);
788 789 790 791 792 793 794 795

			/* I2C gate for DNOD44CDH086A(TDA18212) tuner module */
			if (adap->fe[1])
				adap->fe[1]->ops.i2c_gate_ctrl =
						anysee_i2c_gate_ctrl;
		} else {
			/* PLL config */
			adap->fe[1] = dvb_attach(zl10353_attach,
796
					&anysee_zl10353_config,
797
					&d->i2c_adap);
798 799
		}

800
		break;
801
	case ANYSEE_HW_508TC: /* 18 */
802
	case ANYSEE_HW_508PTC: /* 21 */
803
		/* E7 TC */
804
		/* E7 PTC */
805

806
		/* disable DVB-T demod on IOD[6] */
807
		ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 6), 0x40);
808 809
		if (ret)
			goto error;
810

811
		/* enable DVB-C demod on IOD[5] */
812
		ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 5), 0x20);
813 814
		if (ret)
			goto error;
815

816 817
		/* attach demod */
		adap->fe[0] = dvb_attach(tda10023_attach,
818
				&anysee_tda10023_tda18212_config,
819
				&d->i2c_adap, 0x48);
820

821 822 823 824 825 826 827 828 829
		/* I2C gate for DNOD44CDH086A(TDA18212) tuner module */
		if (adap->fe[0])
			adap->fe[0]->ops.i2c_gate_ctrl = anysee_i2c_gate_ctrl;

		/* break out if first frontend attaching fails */
		if (!adap->fe[0])
			break;

		/* disable DVB-C demod on IOD[5] */
830
		ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 5), 0x20);
831 832 833 834
		if (ret)
			goto error;

		/* enable DVB-T demod on IOD[6] */
835
		ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 6), 0x40);
836 837
		if (ret)
			goto error;
838

839 840
		/* attach demod */
		adap->fe[1] = dvb_attach(zl10353_attach,
841
				&anysee_zl10353_tda18212_config,
842
				&d->i2c_adap);
843

844
		/* I2C gate for DNOD44CDH086A(TDA18212) tuner module */
845 846
		if (adap->fe[1])
			adap->fe[1]->ops.i2c_gate_ctrl = anysee_i2c_gate_ctrl;
847

A
Antti Palosaari 已提交
848 849
		state->has_ci = true;

850 851
		break;
	case ANYSEE_HW_508S2: /* 19 */
852
	case ANYSEE_HW_508PS2: /* 22 */
853
		/* E7 S2 */
854
		/* E7 PS2 */
855 856

		/* enable DVB-S/S2 demod on IOE[5] */
857
		ret = anysee_wr_reg_mask(d, REG_IOE, (1 << 5), 0x20);
858 859 860 861
		if (ret)
			goto error;

		/* attach demod */
862
		adap->fe[0] = dvb_attach(stv0900_attach,
863
				&anysee_stv0900_config, &d->i2c_adap, 0);
864

A
Antti Palosaari 已提交
865 866
		state->has_ci = true;

867 868 869 870 871
		break;
	case ANYSEE_HW_508T2C: /* 20 */
		/* E7 T2C */

		/* enable DVB-T/T2/C demod on IOE[5] */
872
		ret = anysee_wr_reg_mask(d, REG_IOE, (1 << 5), 0x20);
873 874 875
		if (ret)
			goto error;

876
		/* attach demod */
877
		adap->fe[0] = dvb_attach(cxd2820r_attach,
878
				&anysee_cxd2820r_config, &d->i2c_adap);
879

A
Antti Palosaari 已提交
880
		state->has_ci = true;
881

882
		break;
883
	}
884

885
	if (!adap->fe[0]) {
886 887
		/* we have no frontend :-( */
		ret = -ENODEV;
888 889
		err("Unsupported Anysee version. " \
			"Please report the <linux-media@vger.kernel.org>.");
890 891 892
	}
error:
	return ret;
893 894 895 896
}

static int anysee_tuner_attach(struct dvb_usb_adapter *adap)
{
897 898
	struct anysee_state *state = adap_to_priv(adap);
	struct dvb_usb_device *d = adap_to_d(adap);
899
	struct dvb_frontend *fe;
900
	int ret;
901
	deb_info("%s: adap=%d\n", __func__, adap->id);
902

903
	switch (state->hw) {
904
	case ANYSEE_HW_507T: /* 2 */
905 906 907
		/* E30 */

		/* attach tuner */
908 909
		fe = dvb_attach(dvb_pll_attach, adap->fe[0], (0xc2 >> 1), NULL,
				DVB_PLL_THOMSON_DTT7579);
910

911
		break;
912 913 914 915
	case ANYSEE_HW_507CD: /* 6 */
		/* E30 Plus */

		/* attach tuner */
916 917
		fe = dvb_attach(dvb_pll_attach, adap->fe[0], (0xc2 >> 1),
				&d->i2c_adap, DVB_PLL_THOMSON_DTT7579);
918 919 920 921 922 923

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

		/* attach tuner */
924 925
		fe = dvb_attach(dvb_pll_attach, adap->fe[0], (0xc0 >> 1),
				&d->i2c_adap, DVB_PLL_SAMSUNG_DTOS403IH102A);
926

927 928 929 930 931
		break;
	case ANYSEE_HW_507SI: /* 11 */
		/* E30 S2 Plus */

		/* attach LNB controller */
932 933
		fe = dvb_attach(isl6423_attach, adap->fe[0], &d->i2c_adap,
				&anysee_isl6423_config);
934

935 936 937 938 939
		break;
	case ANYSEE_HW_507FA: /* 15 */
		/* E30 Combo Plus */
		/* E30 C Plus */

940 941 942 943
		/* Try first attach TDA18212 silicon tuner on IOE[4], if that
		 * fails attach old simple PLL. */

		/* attach tuner */
944 945
		fe = dvb_attach(tda18212_attach, adap->fe[0], &d->i2c_adap,
				&anysee_tda18212_config);
946 947 948 949

		if (fe && adap->fe[1]) {
			/* attach tuner for 2nd FE */
			fe = dvb_attach(tda18212_attach, adap->fe[1],
950
					&d->i2c_adap, &anysee_tda18212_config);
951 952
			break;
		} else if (fe) {
953
			break;
954
		}
955

956
		/* attach tuner */
957
		fe = dvb_attach(dvb_pll_attach, adap->fe[0], (0xc0 >> 1),
958
				&d->i2c_adap, DVB_PLL_SAMSUNG_DTOS403IH102A);
959 960 961 962

		if (fe && adap->fe[1]) {
			/* attach tuner for 2nd FE */
			fe = dvb_attach(dvb_pll_attach, adap->fe[0],
963
					(0xc0 >> 1), &d->i2c_adap,
964 965
					DVB_PLL_SAMSUNG_DTOS403IH102A);
		}
966

967
		break;
968
	case ANYSEE_HW_508TC: /* 18 */
969
	case ANYSEE_HW_508PTC: /* 21 */
970
		/* E7 TC */
971
		/* E7 PTC */
972 973

		/* attach tuner */
974 975
		fe = dvb_attach(tda18212_attach, adap->fe[0], &d->i2c_adap,
				&anysee_tda18212_config);
976

977 978 979
		if (fe) {
			/* attach tuner for 2nd FE */
			fe = dvb_attach(tda18212_attach, adap->fe[1],
980
					&d->i2c_adap, &anysee_tda18212_config);
981 982
		}

983 984
		break;
	case ANYSEE_HW_508S2: /* 19 */
985
	case ANYSEE_HW_508PS2: /* 22 */
986
		/* E7 S2 */
987
		/* E7 PS2 */
988 989

		/* attach tuner */
990
		fe = dvb_attach(stv6110_attach, adap->fe[0],
991
				&anysee_stv6110_config, &d->i2c_adap);
992 993 994

		if (fe) {
			/* attach LNB controller */
995
			fe = dvb_attach(isl6423_attach, adap->fe[0],
996
					&d->i2c_adap, &anysee_isl6423_config);
997 998
		}

999
		break;
1000 1001 1002 1003 1004

	case ANYSEE_HW_508T2C: /* 20 */
		/* E7 T2C */

		/* attach tuner */
1005 1006
		fe = dvb_attach(tda18212_attach, adap->fe[0], &d->i2c_adap,
				&anysee_tda18212_config2);
1007 1008

		break;
1009
	default:
1010
		fe = NULL;
1011 1012
	}

1013 1014 1015 1016 1017
	if (fe)
		ret = 0;
	else
		ret = -ENODEV;

1018
	return ret;
1019 1020
}

1021
static int anysee_rc_query(struct dvb_usb_device *d)
1022 1023 1024
{
	u8 buf[] = {CMD_GET_IR_CODE};
	u8 ircode[2];
1025 1026 1027 1028 1029 1030 1031 1032 1033
	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... */
1034

1035
	ret = anysee_ctrl_msg(d, buf, sizeof(buf), ircode, sizeof(ircode));
1036 1037 1038
	if (ret)
		return ret;

1039 1040
	if (ircode[0]) {
		deb_rc("%s: key pressed %02x\n", __func__, ircode[1]);
1041
		rc_keydown(d->rc_dev, 0x08 << 8 | ircode[1], 0);
1042
	}
1043

1044 1045 1046
	return 0;
}

1047 1048 1049 1050 1051 1052 1053 1054 1055
static int anysee_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
{
	rc->allowed_protos = RC_TYPE_NEC;
	rc->query          = anysee_rc_query;
	rc->interval       = 250;  /* windows driver uses 500ms */

	return 0;
}

A
Antti Palosaari 已提交
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 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
static int anysee_ci_read_attribute_mem(struct dvb_ca_en50221 *ci, int slot,
	int addr)
{
	struct dvb_usb_device *d = ci->data;
	int ret;
	u8 buf[] = {CMD_CI, 0x02, 0x40 | addr >> 8, addr & 0xff, 0x00, 1};
	u8 val;

	ret = anysee_ctrl_msg(d, buf, sizeof(buf), &val, 1);
	if (ret)
		return ret;

	return val;
}

static int anysee_ci_write_attribute_mem(struct dvb_ca_en50221 *ci, int slot,
	int addr, u8 val)
{
	struct dvb_usb_device *d = ci->data;
	int ret;
	u8 buf[] = {CMD_CI, 0x03, 0x40 | addr >> 8, addr & 0xff, 0x00, 1, val};

	ret = anysee_ctrl_msg(d, buf, sizeof(buf), NULL, 0);
	if (ret)
		return ret;

	return 0;
}

static int anysee_ci_read_cam_control(struct dvb_ca_en50221 *ci, int slot,
	u8 addr)
{
	struct dvb_usb_device *d = ci->data;
	int ret;
	u8 buf[] = {CMD_CI, 0x04, 0x40, addr, 0x00, 1};
	u8 val;

	ret = anysee_ctrl_msg(d, buf, sizeof(buf), &val, 1);
	if (ret)
		return ret;

	return val;
}

static int anysee_ci_write_cam_control(struct dvb_ca_en50221 *ci, int slot,
	u8 addr, u8 val)
{
	struct dvb_usb_device *d = ci->data;
	int ret;
	u8 buf[] = {CMD_CI, 0x05, 0x40, addr, 0x00, 1, val};

	ret = anysee_ctrl_msg(d, buf, sizeof(buf), NULL, 0);
	if (ret)
		return ret;

	return 0;
}

static int anysee_ci_slot_reset(struct dvb_ca_en50221 *ci, int slot)
{
	struct dvb_usb_device *d = ci->data;
	int ret;
1118
	struct anysee_state *state = d_to_priv(d);
A
Antti Palosaari 已提交
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 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168

	state->ci_cam_ready = jiffies + msecs_to_jiffies(1000);

	ret = anysee_wr_reg_mask(d, REG_IOA, (0 << 7), 0x80);
	if (ret)
		return ret;

	msleep(300);

	ret = anysee_wr_reg_mask(d, REG_IOA, (1 << 7), 0x80);
	if (ret)
		return ret;

	return 0;
}

static int anysee_ci_slot_shutdown(struct dvb_ca_en50221 *ci, int slot)
{
	struct dvb_usb_device *d = ci->data;
	int ret;

	ret = anysee_wr_reg_mask(d, REG_IOA, (0 << 7), 0x80);
	if (ret)
		return ret;

	msleep(30);

	ret = anysee_wr_reg_mask(d, REG_IOA, (1 << 7), 0x80);
	if (ret)
		return ret;

	return 0;
}

static int anysee_ci_slot_ts_enable(struct dvb_ca_en50221 *ci, int slot)
{
	struct dvb_usb_device *d = ci->data;
	int ret;

	ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 1), 0x02);
	if (ret)
		return ret;

	return 0;
}

static int anysee_ci_poll_slot_status(struct dvb_ca_en50221 *ci, int slot,
	int open)
{
	struct dvb_usb_device *d = ci->data;
1169
	struct anysee_state *state = d_to_priv(d);
A
Antti Palosaari 已提交
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
	int ret;
	u8 tmp;

	ret = anysee_rd_reg_mask(d, REG_IOC, &tmp, 0x40);
	if (ret)
		return ret;

	if (tmp == 0) {
		ret = DVB_CA_EN50221_POLL_CAM_PRESENT;
		if (time_after(jiffies, state->ci_cam_ready))
			ret |= DVB_CA_EN50221_POLL_CAM_READY;
	}

	return ret;
}

static int anysee_ci_init(struct dvb_usb_device *d)
{
1188
	struct anysee_state *state = d_to_priv(d);
A
Antti Palosaari 已提交
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
	int ret;

	state->ci.owner               = THIS_MODULE;
	state->ci.read_attribute_mem  = anysee_ci_read_attribute_mem;
	state->ci.write_attribute_mem = anysee_ci_write_attribute_mem;
	state->ci.read_cam_control    = anysee_ci_read_cam_control;
	state->ci.write_cam_control   = anysee_ci_write_cam_control;
	state->ci.slot_reset          = anysee_ci_slot_reset;
	state->ci.slot_shutdown       = anysee_ci_slot_shutdown;
	state->ci.slot_ts_enable      = anysee_ci_slot_ts_enable;
	state->ci.poll_slot_status    = anysee_ci_poll_slot_status;
	state->ci.data                = d;

	ret = anysee_wr_reg_mask(d, REG_IOA, (1 << 7), 0x80);
	if (ret)
		return ret;

A
Antti Palosaari 已提交
1206 1207 1208 1209 1210 1211 1212 1213
	ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 2)|(0 << 1)|(0 << 0), 0x07);
	if (ret)
		return ret;

	ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 2)|(1 << 1)|(1 << 0), 0x07);
	if (ret)
		return ret;

A
Antti Palosaari 已提交
1214 1215 1216 1217 1218 1219 1220 1221 1222
	ret = dvb_ca_en50221_init(&d->adapter[0].dvb_adap, &state->ci, 0, 1);
	if (ret)
		return ret;

	return 0;
}

static void anysee_ci_release(struct dvb_usb_device *d)
{
1223
	struct anysee_state *state = d_to_priv(d);
A
Antti Palosaari 已提交
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233

	/* detach CI */
	if (state->has_ci)
		dvb_ca_en50221_release(&state->ci);

	return;
}

static int anysee_init(struct dvb_usb_device *d)
{
1234
	struct anysee_state *state = d_to_priv(d);
A
Antti Palosaari 已提交
1235 1236
	int ret;

1237 1238 1239 1240 1241 1242 1243 1244
	/* 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). */
	ret = usb_set_interface(d->udev, 0, 0);
	if (ret)
		return ret;

A
Antti Palosaari 已提交
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
	/* 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;

	/* attach CI */
	if (state->has_ci) {
		ret = anysee_ci_init(d);
		if (ret) {
			state->has_ci = false;
			return ret;
		}
	}

	return 0;
}

1267
static void anysee_exit(struct dvb_usb_device *d)
A
Antti Palosaari 已提交
1268
{
1269
	return anysee_ci_release(d);
A
Antti Palosaari 已提交
1270 1271
}

1272 1273 1274 1275 1276 1277
/* DVB USB Driver stuff */
static struct dvb_usb_device_properties anysee_props = {
	.driver_name = KBUILD_MODNAME,
	.owner = THIS_MODULE,
	.adapter_nr = adapter_nr,
	.size_of_priv = sizeof(struct anysee_state),
1278

1279 1280
	.generic_bulk_ctrl_endpoint = 0x01,
	.generic_bulk_ctrl_endpoint_response = 0x81,
1281

1282 1283 1284 1285 1286 1287 1288 1289
	.i2c_algo         = &anysee_i2c_algo,
	.read_config      = anysee_read_config,
	.frontend_attach  = anysee_frontend_attach,
	.tuner_attach     = anysee_tuner_attach,
	.init             = anysee_init,
	.get_rc_config    = anysee_get_rc_config,
	.frontend_ctrl    = anysee_frontend_ctrl,
	.streaming_ctrl   = anysee_streaming_ctrl,
1290
	.exit             = anysee_exit,
1291 1292 1293 1294

	.num_adapters = 1,
	.adapter = {
		{
1295
			.stream = DVB_USB_STREAM_BULK(0x82, 8, 16 * 512),
1296 1297 1298 1299
		}
	}
};

1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
static const struct usb_device_id anysee_id_table[] = {
	{ DVB_USB_DEVICE(USB_VID_CYPRESS, USB_PID_ANYSEE,
		&anysee_props, "Anysee", RC_MAP_ANYSEE) },
	{ DVB_USB_DEVICE(USB_VID_AMT, USB_PID_ANYSEE,
		&anysee_props, "Anysee", RC_MAP_ANYSEE) },
	{ }
};
MODULE_DEVICE_TABLE(usb, anysee_id_table);

static struct usb_driver anysee_usb_driver = {
	.name = KBUILD_MODNAME,
	.id_table = anysee_id_table,
	.probe = dvb_usbv2_probe,
	.disconnect = dvb_usbv2_disconnect,
	.suspend = dvb_usbv2_suspend,
	.resume = dvb_usbv2_resume,
	.no_dynamic_id = 1,
	.soft_unbind = 1,
1318 1319
};

1320
module_usb_driver(anysee_usb_driver);
1321 1322 1323 1324

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