anysee.c revision 449d1a0ad1732476d394fb2b885092a5c554f983
1/*
2 * DVB USB Linux driver for Anysee E30 DVB-C & DVB-T USB2.0 receiver
3 *
4 * Copyright (C) 2007 Antti Palosaari <crope@iki.fi>
5 *
6 *    This program is free software; you can redistribute it and/or modify
7 *    it under the terms of the GNU General Public License as published by
8 *    the Free Software Foundation; either version 2 of the License, or
9 *    (at your option) any later version.
10 *
11 *    This program is distributed in the hope that it will be useful,
12 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 *    GNU General Public License for more details.
15 *
16 *    You should have received a copy of the GNU General Public License
17 *    along with this program; if not, write to the Free Software
18 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 * TODO:
21 * - add smart card reader support for Conditional Access (CA)
22 *
23 * Card reader in Anysee is nothing more than ISO 7816 card reader.
24 * There is no hardware CAM in any Anysee device sold.
25 * In my understanding it should be implemented by making own module
26 * for ISO 7816 card reader, like dvb_ca_en50221 is implemented. This
27 * module registers serial interface that can be used to communicate
28 * with any ISO 7816 smart card.
29 *
30 * Any help according to implement serial smart card reader support
31 * is highly welcome!
32 */
33
34#include "anysee.h"
35#include "tda1002x.h"
36#include "mt352.h"
37#include "mt352_priv.h"
38#include "zl10353.h"
39#include "tda18212.h"
40#include "cx24116.h"
41#include "stv0900.h"
42#include "stv6110.h"
43#include "isl6423.h"
44
45/* debug */
46static int dvb_usb_anysee_debug;
47module_param_named(debug, dvb_usb_anysee_debug, int, 0644);
48MODULE_PARM_DESC(debug, "set debugging level" DVB_USB_DEBUG_STATUS);
49static int dvb_usb_anysee_delsys;
50module_param_named(delsys, dvb_usb_anysee_delsys, int, 0644);
51MODULE_PARM_DESC(delsys, "select delivery mode (0=DVB-C, 1=DVB-T)");
52DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
53
54static DEFINE_MUTEX(anysee_usb_mutex);
55
56static int anysee_ctrl_msg(struct dvb_usb_device *d, u8 *sbuf, u8 slen,
57	u8 *rbuf, u8 rlen)
58{
59	struct anysee_state *state = d->priv;
60	int act_len, ret;
61	u8 buf[64];
62
63	memcpy(&buf[0], sbuf, slen);
64	buf[60] = state->seq++;
65
66	if (mutex_lock_interruptible(&anysee_usb_mutex) < 0)
67		return -EAGAIN;
68
69	/* We need receive one message more after dvb_usb_generic_rw due
70	   to weird transaction flow, which is 1 x send + 2 x receive. */
71	ret = dvb_usb_generic_rw(d, buf, sizeof(buf), buf, sizeof(buf), 0);
72
73	if (!ret) {
74		/* receive 2nd answer */
75		ret = usb_bulk_msg(d->udev, usb_rcvbulkpipe(d->udev,
76			d->props.generic_bulk_ctrl_endpoint), buf, sizeof(buf),
77			&act_len, 2000);
78		if (ret)
79			err("%s: recv bulk message failed: %d", __func__, ret);
80		else {
81			deb_xfer("<<< ");
82			debug_dump(buf, act_len, deb_xfer);
83		}
84	}
85
86	/* read request, copy returned data to return buf */
87	if (!ret && rbuf && rlen)
88		memcpy(rbuf, buf, rlen);
89
90	mutex_unlock(&anysee_usb_mutex);
91
92	return ret;
93}
94
95static int anysee_read_reg(struct dvb_usb_device *d, u16 reg, u8 *val)
96{
97	u8 buf[] = {CMD_REG_READ, reg >> 8, reg & 0xff, 0x01};
98	int ret;
99	ret = anysee_ctrl_msg(d, buf, sizeof(buf), val, 1);
100	deb_info("%s: reg:%04x val:%02x\n", __func__, reg, *val);
101	return ret;
102}
103
104static int anysee_write_reg(struct dvb_usb_device *d, u16 reg, u8 val)
105{
106	u8 buf[] = {CMD_REG_WRITE, reg >> 8, reg & 0xff, 0x01, val};
107	deb_info("%s: reg:%04x val:%02x\n", __func__, reg, val);
108	return anysee_ctrl_msg(d, buf, sizeof(buf), NULL, 0);
109}
110
111/* write single register with mask */
112static int anysee_wr_reg_mask(struct dvb_usb_device *d, u16 reg, u8 val,
113	u8 mask)
114{
115	int ret;
116	u8 tmp;
117
118	/* no need for read if whole reg is written */
119	if (mask != 0xff) {
120		ret = anysee_read_reg(d, reg, &tmp);
121		if (ret)
122			return ret;
123
124		val &= mask;
125		tmp &= ~mask;
126		val |= tmp;
127	}
128
129	return anysee_write_reg(d, reg, val);
130}
131
132static int anysee_get_hw_info(struct dvb_usb_device *d, u8 *id)
133{
134	u8 buf[] = {CMD_GET_HW_INFO};
135	return anysee_ctrl_msg(d, buf, sizeof(buf), id, 3);
136}
137
138static int anysee_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
139{
140	u8 buf[] = {CMD_STREAMING_CTRL, (u8)onoff, 0x00};
141	deb_info("%s: onoff:%02x\n", __func__, onoff);
142	return anysee_ctrl_msg(adap->dev, buf, sizeof(buf), NULL, 0);
143}
144
145static int anysee_led_ctrl(struct dvb_usb_device *d, u8 mode, u8 interval)
146{
147	u8 buf[] = {CMD_LED_AND_IR_CTRL, 0x01, mode, interval};
148	deb_info("%s: state:%02x interval:%02x\n", __func__, mode, interval);
149	return anysee_ctrl_msg(d, buf, sizeof(buf), NULL, 0);
150}
151
152static int anysee_ir_ctrl(struct dvb_usb_device *d, u8 onoff)
153{
154	u8 buf[] = {CMD_LED_AND_IR_CTRL, 0x02, onoff};
155	deb_info("%s: onoff:%02x\n", __func__, onoff);
156	return anysee_ctrl_msg(d, buf, sizeof(buf), NULL, 0);
157}
158
159static int anysee_init(struct dvb_usb_device *d)
160{
161	int ret;
162	/* LED light */
163	ret = anysee_led_ctrl(d, 0x01, 0x03);
164	if (ret)
165		return ret;
166
167	/* enable IR */
168	ret = anysee_ir_ctrl(d, 1);
169	if (ret)
170		return ret;
171
172	return 0;
173}
174
175/* I2C */
176static int anysee_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msg,
177	int num)
178{
179	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	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
184		return -EAGAIN;
185
186	while (i < num) {
187		if (num > i + 1 && (msg[i+1].flags & I2C_M_RD)) {
188			if (msg[i].len > 2 || msg[i+1].len > 60) {
189				ret = -EOPNOTSUPP;
190				break;
191			}
192			buf[0] = CMD_I2C_READ;
193			buf[1] = (msg[i].addr << 1) | 0x01;
194			buf[2] = msg[i].buf[0];
195			buf[3] = msg[i].buf[1];
196			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				msg[i+1].len);
200			inc = 2;
201		} else {
202			if (msg[i].len > 48) {
203				ret = -EOPNOTSUPP;
204				break;
205			}
206			buf[0] = CMD_I2C_WRITE;
207			buf[1] = (msg[i].addr << 1);
208			buf[2] = msg[i].len;
209			buf[3] = 0x01;
210			memcpy(&buf[4], msg[i].buf, msg[i].len);
211			ret = anysee_ctrl_msg(d, buf, 4 + msg[i].len, NULL, 0);
212			inc = 1;
213		}
214		if (ret)
215			break;
216
217		i += inc;
218	}
219
220	mutex_unlock(&d->i2c_mutex);
221
222	return ret ? ret : i;
223}
224
225static u32 anysee_i2c_func(struct i2c_adapter *adapter)
226{
227	return I2C_FUNC_I2C;
228}
229
230static struct i2c_algorithm anysee_i2c_algo = {
231	.master_xfer   = anysee_master_xfer,
232	.functionality = anysee_i2c_func,
233};
234
235static int anysee_mt352_demod_init(struct dvb_frontend *fe)
236{
237	static u8 clock_config[]   = { CLOCK_CTL,  0x38, 0x28 };
238	static u8 reset[]          = { RESET,      0x80 };
239	static u8 adc_ctl_1_cfg[]  = { ADC_CTL_1,  0x40 };
240	static u8 agc_cfg[]        = { AGC_TARGET, 0x28, 0x20 };
241	static u8 gpp_ctl_cfg[]    = { GPP_CTL,    0x33 };
242	static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
243
244	mt352_write(fe, clock_config,   sizeof(clock_config));
245	udelay(200);
246	mt352_write(fe, reset,          sizeof(reset));
247	mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
248
249	mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
250	mt352_write(fe, gpp_ctl_cfg,    sizeof(gpp_ctl_cfg));
251	mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
252
253	return 0;
254}
255
256/* Callbacks for DVB USB */
257static struct tda10023_config anysee_tda10023_config = {
258	.demod_address = (0x1a >> 1),
259	.invert = 0,
260	.xtal   = 16000000,
261	.pll_m  = 11,
262	.pll_p  = 3,
263	.pll_n  = 1,
264	.output_mode = TDA10023_OUTPUT_MODE_PARALLEL_C,
265	.deltaf = 0xfeeb,
266};
267
268static struct mt352_config anysee_mt352_config = {
269	.demod_address = (0x1e >> 1),
270	.demod_init    = anysee_mt352_demod_init,
271};
272
273static struct zl10353_config anysee_zl10353_config = {
274	.demod_address = (0x1e >> 1),
275	.parallel_ts = 1,
276};
277
278static struct zl10353_config anysee_zl10353_tda18212_config2 = {
279	.demod_address = (0x1e >> 1),
280	.parallel_ts = 1,
281	.disable_i2c_gate_ctrl = 1,
282	.no_tuner = 1,
283	.if2 = 41500,
284};
285
286static struct zl10353_config anysee_zl10353_tda18212_config = {
287	.demod_address = (0x18 >> 1),
288	.parallel_ts = 1,
289	.disable_i2c_gate_ctrl = 1,
290	.no_tuner = 1,
291	.if2 = 41500,
292};
293
294static struct tda10023_config anysee_tda10023_tda18212_config = {
295	.demod_address = (0x1a >> 1),
296	.xtal   = 16000000,
297	.pll_m  = 12,
298	.pll_p  = 3,
299	.pll_n  = 1,
300	.output_mode = TDA10023_OUTPUT_MODE_PARALLEL_C,
301	.deltaf = 0xba02,
302};
303
304static struct tda18212_config anysee_tda18212_config = {
305	.i2c_address = (0xc0 >> 1),
306	.if_dvbt_6 = 4150,
307	.if_dvbt_7 = 4150,
308	.if_dvbt_8 = 4150,
309	.if_dvbc = 5000,
310};
311
312static struct cx24116_config anysee_cx24116_config = {
313	.demod_address = (0xaa >> 1),
314	.mpg_clk_pos_pol = 0x00,
315	.i2c_wr_max = 48,
316};
317
318static struct stv0900_config anysee_stv0900_config = {
319	.demod_address = (0xd0 >> 1),
320	.demod_mode = 0,
321	.xtal = 8000000,
322	.clkmode = 3,
323	.diseqc_mode = 2,
324	.tun1_maddress = 0,
325	.tun1_adc = 1, /* 1 Vpp */
326	.path1_mode = 3,
327};
328
329static struct stv6110_config anysee_stv6110_config = {
330	.i2c_address = (0xc0 >> 1),
331	.mclk = 16000000,
332	.clk_div = 1,
333};
334
335static struct isl6423_config anysee_isl6423_config = {
336	.current_max = SEC_CURRENT_800m,
337	.curlim  = SEC_CURRENT_LIM_OFF,
338	.mod_extern = 1,
339	.addr = (0x10 >> 1),
340};
341
342/*
343 * New USB device strings: Mfr=1, Product=2, SerialNumber=0
344 * Manufacturer: AMT.CO.KR
345 *
346 * E30 VID=04b4 PID=861f HW=2 FW=2.1 Product=????????
347 * PCB: ?
348 * parts: DNOS404ZH102A(MT352, DTT7579(?))
349 *
350 * E30 VID=04b4 PID=861f HW=2 FW=2.1 "anysee-T(LP)"
351 * PCB: PCB 507T (rev1.61)
352 * parts: DNOS404ZH103A(ZL10353, DTT7579(?))
353 * OEA=0a OEB=00 OEC=00 OED=ff OEE=00
354 * IOA=45 IOB=ff IOC=00 IOD=ff IOE=00
355 *
356 * E30 Plus VID=04b4 PID=861f HW=6 FW=1.0 "anysee"
357 * PCB: 507CD (rev1.1)
358 * parts: DNOS404ZH103A(ZL10353, DTT7579(?)), CST56I01
359 * OEA=80 OEB=00 OEC=00 OED=ff OEE=fe
360 * IOA=4f IOB=ff IOC=00 IOD=06 IOE=01
361 * IOD[0] ZL10353 1=enabled
362 * IOA[7] TS 0=enabled
363 * tuner is not behind ZL10353 I2C-gate (no care if gate disabled or not)
364 *
365 * E30 C Plus VID=04b4 PID=861f HW=10 FW=1.0 "anysee-DC(LP)"
366 * PCB: 507DC (rev0.2)
367 * parts: TDA10023, DTOS403IH102B TM, CST56I01
368 * OEA=80 OEB=00 OEC=00 OED=ff OEE=fe
369 * IOA=4f IOB=ff IOC=00 IOD=26 IOE=01
370 * IOD[0] TDA10023 1=enabled
371 *
372 * E30 S2 Plus VID=04b4 PID=861f HW=11 FW=0.1 "anysee-S2(LP)"
373 * PCB: 507SI (rev2.1)
374 * parts: BS2N10WCC01(CX24116, CX24118), ISL6423, TDA8024
375 * OEA=80 OEB=00 OEC=ff OED=ff OEE=fe
376 * IOA=4d IOB=ff IOC=00 IOD=26 IOE=01
377 * IOD[0] CX24116 1=enabled
378 *
379 * E30 C Plus VID=1c73 PID=861f HW=15 FW=1.2 "anysee-FA(LP)"
380 * PCB: 507FA (rev0.4)
381 * parts: TDA10023, DTOS403IH102B TM, TDA8024
382 * OEA=80 OEB=00 OEC=ff OED=ff OEE=ff
383 * IOA=4d IOB=ff IOC=00 IOD=00 IOE=c0
384 * IOD[5] TDA10023 1=enabled
385 * IOE[0] tuner 1=enabled
386 *
387 * E30 Combo Plus VID=1c73 PID=861f HW=15 FW=1.2 "anysee-FA(LP)"
388 * PCB: 507FA (rev1.1)
389 * parts: ZL10353, TDA10023, DTOS403IH102B TM, TDA8024
390 * OEA=80 OEB=00 OEC=ff OED=ff OEE=ff
391 * IOA=4d IOB=ff IOC=00 IOD=00 IOE=c0
392 * DVB-C:
393 * IOD[5] TDA10023 1=enabled
394 * IOE[0] tuner 1=enabled
395 * DVB-T:
396 * IOD[0] ZL10353 1=enabled
397 * IOE[0] tuner 0=enabled
398 * tuner is behind ZL10353 I2C-gate
399 *
400 * E7 TC VID=1c73 PID=861f HW=18 FW=0.7 AMTCI=0.5 "anysee-E7TC(LP)"
401 * PCB: 508TC (rev0.6)
402 * parts: ZL10353, TDA10023, DNOD44CDH086A(TDA18212)
403 * OEA=80 OEB=00 OEC=03 OED=f7 OEE=ff
404 * IOA=4d IOB=00 IOC=cc IOD=48 IOE=e4
405 * IOA[7] TS 1=enabled
406 * IOE[4] TDA18212 1=enabled
407 * DVB-C:
408 * IOD[6] ZL10353 0=disabled
409 * IOD[5] TDA10023 1=enabled
410 * IOE[0] IF 1=enabled
411 * DVB-T:
412 * IOD[5] TDA10023 0=disabled
413 * IOD[6] ZL10353 1=enabled
414 * IOE[0] IF 0=enabled
415 *
416 * E7 S2 VID=1c73 PID=861f HW=19 FW=0.4 AMTCI=0.5 "anysee-E7S2(LP)"
417 * PCB: 508S2 (rev0.7)
418 * parts: DNBU10512IST(STV0903, STV6110), ISL6423
419 * OEA=80 OEB=00 OEC=03 OED=f7 OEE=ff
420 * IOA=4d IOB=00 IOC=c4 IOD=08 IOE=e4
421 * IOA[7] TS 1=enabled
422 * IOE[5] STV0903 1=enabled
423 *
424 * E7 PTC VID=1c73 PID=861f HW=21 FW=0.1 AMTCI=?? "anysee-E7PTC(LP)"
425 * PCB: 508PTC (rev0.5)
426 * parts: ZL10353, TDA10023, DNOD44CDH086A(TDA18212)
427 * OEA=80 OEB=00 OEC=03 OED=f7 OEE=ff
428 * IOA=4d IOB=00 IOC=cc IOD=48 IOE=e4
429 * IOA[7] TS 1=enabled
430 * IOE[4] TDA18212 1=enabled
431 * DVB-C:
432 * IOD[6] ZL10353 0=disabled
433 * IOD[5] TDA10023 1=enabled
434 * IOE[0] IF 1=enabled
435 * DVB-T:
436 * IOD[5] TDA10023 0=disabled
437 * IOD[6] ZL10353 1=enabled
438 * IOE[0] IF 0=enabled
439 *
440 * E7 S2 VID=1c73 PID=861f HW=22 FW=0.1 AMTCI=?? "anysee-E7PS2(LP)"
441 * PCB: 508PS2 (rev0.4)
442 * parts: DNBU10512IST(STV0903, STV6110), ISL6423
443 * OEA=80 OEB=00 OEC=03 OED=f7 OEE=ff
444 * IOA=4d IOB=00 IOC=c4 IOD=08 IOE=e4
445 * IOA[7] TS 1=enabled
446 * IOE[5] STV0903 1=enabled
447 */
448
449static int anysee_frontend_ctrl(struct dvb_frontend *fe, int onoff)
450{
451	struct dvb_usb_adapter *adap = fe->dvb->priv;
452	struct anysee_state *state = adap->dev->priv;
453	int ret;
454
455	deb_info("%s: fe=%d onoff=%d\n", __func__, fe->id, onoff);
456
457	/* no frontend sleep control */
458	if (onoff == 0)
459		return 0;
460
461	switch (state->hw) {
462	case ANYSEE_HW_507FA: /* 15 */
463		/* E30 Combo Plus */
464		/* E30 C Plus */
465
466		if ((fe->id ^ dvb_usb_anysee_delsys) == 0)  {
467			/* disable DVB-T demod on IOD[0] */
468			ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (0 << 0),
469				0x01);
470			if (ret)
471				goto error;
472
473			/* enable DVB-C demod on IOD[5] */
474			ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (1 << 5),
475				0x20);
476			if (ret)
477				goto error;
478
479			/* enable DVB-C tuner on IOE[0] */
480			ret = anysee_wr_reg_mask(adap->dev, REG_IOE, (1 << 0),
481				0x01);
482			if (ret)
483				goto error;
484		} else {
485			/* disable DVB-C demod on IOD[5] */
486			ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (0 << 5),
487				0x20);
488			if (ret)
489				goto error;
490
491			/* enable DVB-T demod on IOD[0] */
492			ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (1 << 0),
493				0x01);
494			if (ret)
495				goto error;
496
497			/* enable DVB-T tuner on IOE[0] */
498			ret = anysee_wr_reg_mask(adap->dev, REG_IOE, (0 << 0),
499				0x01);
500			if (ret)
501				goto error;
502		}
503
504		break;
505	case ANYSEE_HW_508TC: /* 18 */
506	case ANYSEE_HW_508PTC: /* 21 */
507		/* E7 TC */
508		/* E7 PTC */
509
510		if ((fe->id ^ dvb_usb_anysee_delsys) == 0)  {
511			/* disable DVB-T demod on IOD[6] */
512			ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (0 << 6),
513				0x40);
514			if (ret)
515				goto error;
516
517			/* enable DVB-C demod on IOD[5] */
518			ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (1 << 5),
519				0x20);
520			if (ret)
521				goto error;
522
523			/* enable IF route on IOE[0] */
524			ret = anysee_wr_reg_mask(adap->dev, REG_IOE, (1 << 0),
525				0x01);
526			if (ret)
527				goto error;
528		} else {
529			/* disable DVB-C demod on IOD[5] */
530			ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (0 << 5),
531				0x20);
532			if (ret)
533				goto error;
534
535			/* enable DVB-T demod on IOD[6] */
536			ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (1 << 6),
537				0x40);
538			if (ret)
539				goto error;
540
541			/* enable IF route on IOE[0] */
542			ret = anysee_wr_reg_mask(adap->dev, REG_IOE, (0 << 0),
543				0x01);
544			if (ret)
545				goto error;
546		}
547
548		break;
549	default:
550		ret = 0;
551	}
552
553error:
554	return ret;
555}
556
557static int anysee_frontend_attach(struct dvb_usb_adapter *adap)
558{
559	int ret;
560	struct anysee_state *state = adap->dev->priv;
561	u8 hw_info[3];
562	u8 tmp;
563	struct i2c_msg msg[2] = {
564		{
565			.addr = anysee_tda18212_config.i2c_address,
566			.flags = 0,
567			.len = 1,
568			.buf = "\x00",
569		}, {
570			.addr = anysee_tda18212_config.i2c_address,
571			.flags = I2C_M_RD,
572			.len = 1,
573			.buf = &tmp,
574		}
575	};
576
577	/* detect hardware only once */
578	if (adap->fe[0] == NULL) {
579		/* Check which hardware we have.
580		 * We must do this call two times to get reliable values (hw bug).
581		 */
582		ret = anysee_get_hw_info(adap->dev, hw_info);
583		if (ret)
584			goto error;
585
586		ret = anysee_get_hw_info(adap->dev, hw_info);
587		if (ret)
588			goto error;
589
590		/* Meaning of these info bytes are guessed. */
591		info("firmware version:%d.%d hardware id:%d",
592			hw_info[1], hw_info[2], hw_info[0]);
593
594		state->hw = hw_info[0];
595	}
596
597	/* set current frondend ID for devices having two frondends */
598	if (adap->fe[0])
599		state->fe_id++;
600
601	switch (state->hw) {
602	case ANYSEE_HW_507T: /* 2 */
603		/* E30 */
604
605		if (state->fe_id)
606			break;
607
608		/* attach demod */
609		adap->fe[0] = dvb_attach(mt352_attach, &anysee_mt352_config,
610			&adap->dev->i2c_adap);
611		if (adap->fe[0])
612			break;
613
614		/* attach demod */
615		adap->fe[0] = dvb_attach(zl10353_attach, &anysee_zl10353_config,
616			&adap->dev->i2c_adap);
617
618		break;
619	case ANYSEE_HW_507CD: /* 6 */
620		/* E30 Plus */
621
622		if (state->fe_id)
623			break;
624
625		/* enable DVB-T demod on IOD[0] */
626		ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (1 << 0), 0x01);
627		if (ret)
628			goto error;
629
630		/* enable transport stream on IOA[7] */
631		ret = anysee_wr_reg_mask(adap->dev, REG_IOA, (0 << 7), 0x80);
632		if (ret)
633			goto error;
634
635		/* attach demod */
636		adap->fe[0] = dvb_attach(zl10353_attach,
637			&anysee_zl10353_config, &adap->dev->i2c_adap);
638
639		break;
640	case ANYSEE_HW_507DC: /* 10 */
641		/* E30 C Plus */
642
643		if (state->fe_id)
644			break;
645
646		/* enable DVB-C demod on IOD[0] */
647		ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (1 << 0), 0x01);
648		if (ret)
649			goto error;
650
651		/* attach demod */
652		adap->fe[0] = dvb_attach(tda10023_attach,
653			&anysee_tda10023_config, &adap->dev->i2c_adap, 0x48);
654
655		break;
656	case ANYSEE_HW_507SI: /* 11 */
657		/* E30 S2 Plus */
658
659		if (state->fe_id)
660			break;
661
662		/* enable DVB-S/S2 demod on IOD[0] */
663		ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (1 << 0), 0x01);
664		if (ret)
665			goto error;
666
667		/* attach demod */
668		adap->fe[0] = dvb_attach(cx24116_attach, &anysee_cx24116_config,
669			&adap->dev->i2c_adap);
670
671		break;
672	case ANYSEE_HW_507FA: /* 15 */
673		/* E30 Combo Plus */
674		/* E30 C Plus */
675
676		/* enable tuner on IOE[4] */
677		ret = anysee_wr_reg_mask(adap->dev, REG_IOE, (1 << 4), 0x10);
678		if (ret)
679			goto error;
680
681		/* probe TDA18212 */
682		tmp = 0;
683		ret = i2c_transfer(&adap->dev->i2c_adap, msg, 2);
684		if (ret == 2 && tmp == 0xc7)
685			deb_info("%s: TDA18212 found\n", __func__);
686		else
687			tmp = 0;
688
689		/* disable tuner on IOE[4] */
690		ret = anysee_wr_reg_mask(adap->dev, REG_IOE, (0 << 4), 0x10);
691		if (ret)
692			goto error;
693
694		if ((state->fe_id ^ dvb_usb_anysee_delsys) == 0)  {
695			/* disable DVB-T demod on IOD[0] */
696			ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (0 << 0),
697				0x01);
698			if (ret)
699				goto error;
700
701			/* enable DVB-C demod on IOD[5] */
702			ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (1 << 5),
703				0x20);
704			if (ret)
705				goto error;
706
707			/* attach demod */
708			if (tmp == 0xc7) {
709				/* TDA18212 config */
710				adap->fe[state->fe_id] = dvb_attach(
711					tda10023_attach,
712					&anysee_tda10023_tda18212_config,
713					&adap->dev->i2c_adap, 0x48);
714			} else {
715				/* PLL config */
716				adap->fe[state->fe_id] = dvb_attach(
717					tda10023_attach,
718					&anysee_tda10023_config,
719					&adap->dev->i2c_adap, 0x48);
720			}
721		} else {
722			/* disable DVB-C demod on IOD[5] */
723			ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (0 << 5),
724				0x20);
725			if (ret)
726				goto error;
727
728			/* enable DVB-T demod on IOD[0] */
729			ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (1 << 0),
730				0x01);
731			if (ret)
732				goto error;
733
734			/* attach demod */
735			if (tmp == 0xc7) {
736				/* TDA18212 config */
737				adap->fe[state->fe_id] = dvb_attach(
738					zl10353_attach,
739					&anysee_zl10353_tda18212_config2,
740					&adap->dev->i2c_adap);
741			} else {
742				/* PLL config */
743				adap->fe[state->fe_id] = dvb_attach(
744					zl10353_attach,
745					&anysee_zl10353_config,
746					&adap->dev->i2c_adap);
747			}
748		}
749
750		break;
751	case ANYSEE_HW_508TC: /* 18 */
752	case ANYSEE_HW_508PTC: /* 21 */
753		/* E7 TC */
754		/* E7 PTC */
755
756		/* enable transport stream on IOA[7] */
757		ret = anysee_wr_reg_mask(adap->dev, REG_IOA, (1 << 7), 0x80);
758		if (ret)
759			goto error;
760
761		if ((state->fe_id ^ dvb_usb_anysee_delsys) == 0)  {
762			/* disable DVB-T demod on IOD[6] */
763			ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (0 << 6),
764				0x40);
765			if (ret)
766				goto error;
767
768			/* enable DVB-C demod on IOD[5] */
769			ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (1 << 5),
770				0x20);
771			if (ret)
772				goto error;
773
774			/* attach demod */
775			adap->fe[state->fe_id] = dvb_attach(tda10023_attach,
776				&anysee_tda10023_tda18212_config,
777				&adap->dev->i2c_adap, 0x48);
778		} else {
779			/* disable DVB-C demod on IOD[5] */
780			ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (0 << 5),
781				0x20);
782			if (ret)
783				goto error;
784
785			/* enable DVB-T demod on IOD[6] */
786			ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (1 << 6),
787				0x40);
788			if (ret)
789				goto error;
790
791			/* attach demod */
792			adap->fe[state->fe_id] = dvb_attach(zl10353_attach,
793				&anysee_zl10353_tda18212_config,
794				&adap->dev->i2c_adap);
795		}
796
797		break;
798	case ANYSEE_HW_508S2: /* 19 */
799	case ANYSEE_HW_508PS2: /* 22 */
800		/* E7 S2 */
801		/* E7 PS2 */
802
803		if (state->fe_id)
804			break;
805
806		/* enable transport stream on IOA[7] */
807		ret = anysee_wr_reg_mask(adap->dev, REG_IOA, (1 << 7), 0x80);
808		if (ret)
809			goto error;
810
811		/* enable DVB-S/S2 demod on IOE[5] */
812		ret = anysee_wr_reg_mask(adap->dev, REG_IOE, (1 << 5), 0x20);
813		if (ret)
814			goto error;
815
816		/* attach demod */
817		adap->fe[0] = dvb_attach(stv0900_attach, &anysee_stv0900_config,
818			&adap->dev->i2c_adap, 0);
819
820		break;
821	}
822
823	if (!adap->fe[0]) {
824		/* we have no frontend :-( */
825		ret = -ENODEV;
826		err("Unsupported Anysee version. " \
827			"Please report the <linux-media@vger.kernel.org>.");
828	}
829error:
830	return ret;
831}
832
833static int anysee_tuner_attach(struct dvb_usb_adapter *adap)
834{
835	struct anysee_state *state = adap->dev->priv;
836	struct dvb_frontend *fe;
837	int ret;
838	deb_info("%s: fe=%d\n", __func__, state->fe_id);
839
840	switch (state->hw) {
841	case ANYSEE_HW_507T: /* 2 */
842		/* E30 */
843
844		/* attach tuner */
845		fe = dvb_attach(dvb_pll_attach, adap->fe[0], (0xc2 >> 1),
846			NULL, DVB_PLL_THOMSON_DTT7579);
847
848		break;
849	case ANYSEE_HW_507CD: /* 6 */
850		/* E30 Plus */
851
852		/* attach tuner */
853		fe = dvb_attach(dvb_pll_attach, adap->fe[0], (0xc2 >> 1),
854			&adap->dev->i2c_adap, DVB_PLL_THOMSON_DTT7579);
855
856		break;
857	case ANYSEE_HW_507DC: /* 10 */
858		/* E30 C Plus */
859
860		/* attach tuner */
861		fe = dvb_attach(dvb_pll_attach, adap->fe[0], (0xc0 >> 1),
862			&adap->dev->i2c_adap, DVB_PLL_SAMSUNG_DTOS403IH102A);
863
864		break;
865	case ANYSEE_HW_507SI: /* 11 */
866		/* E30 S2 Plus */
867
868		/* attach LNB controller */
869		fe = dvb_attach(isl6423_attach, adap->fe[0],
870			&adap->dev->i2c_adap, &anysee_isl6423_config);
871
872		break;
873	case ANYSEE_HW_507FA: /* 15 */
874		/* E30 Combo Plus */
875		/* E30 C Plus */
876
877		/* Try first attach TDA18212 silicon tuner on IOE[4], if that
878		 * fails attach old simple PLL. */
879
880		/* enable tuner on IOE[4] */
881		ret = anysee_wr_reg_mask(adap->dev, REG_IOE, (1 << 4), 0x10);
882		if (ret)
883			goto error;
884
885		/* attach tuner */
886		fe = dvb_attach(tda18212_attach, adap->fe[state->fe_id],
887			&adap->dev->i2c_adap, &anysee_tda18212_config);
888		if (fe)
889			break;
890
891		/* disable tuner on IOE[4] */
892		ret = anysee_wr_reg_mask(adap->dev, REG_IOE, (0 << 4), 0x10);
893		if (ret)
894			goto error;
895
896		/* attach tuner */
897		fe = dvb_attach(dvb_pll_attach, adap->fe[state->fe_id],
898			(0xc0 >> 1), &adap->dev->i2c_adap,
899			DVB_PLL_SAMSUNG_DTOS403IH102A);
900
901		break;
902	case ANYSEE_HW_508TC: /* 18 */
903	case ANYSEE_HW_508PTC: /* 21 */
904		/* E7 TC */
905		/* E7 PTC */
906
907		/* enable tuner on IOE[4] */
908		ret = anysee_wr_reg_mask(adap->dev, REG_IOE, (1 << 4), 0x10);
909		if (ret)
910			goto error;
911
912		/* attach tuner */
913		fe = dvb_attach(tda18212_attach, adap->fe[state->fe_id],
914			&adap->dev->i2c_adap, &anysee_tda18212_config);
915
916		break;
917	case ANYSEE_HW_508S2: /* 19 */
918	case ANYSEE_HW_508PS2: /* 22 */
919		/* E7 S2 */
920		/* E7 PS2 */
921
922		/* attach tuner */
923		fe = dvb_attach(stv6110_attach, adap->fe[0],
924			&anysee_stv6110_config, &adap->dev->i2c_adap);
925
926		if (fe) {
927			/* attach LNB controller */
928			fe = dvb_attach(isl6423_attach, adap->fe[0],
929				&adap->dev->i2c_adap, &anysee_isl6423_config);
930		}
931
932		break;
933	default:
934		fe = NULL;
935	}
936
937	if (fe)
938		ret = 0;
939	else
940		ret = -ENODEV;
941
942error:
943	return ret;
944}
945
946static int anysee_rc_query(struct dvb_usb_device *d)
947{
948	u8 buf[] = {CMD_GET_IR_CODE};
949	u8 ircode[2];
950	int ret;
951
952	/* Remote controller is basic NEC using address byte 0x08.
953	   Anysee device RC query returns only two bytes, status and code,
954	   address byte is dropped. Also it does not return any value for
955	   NEC RCs having address byte other than 0x08. Due to that, we
956	   cannot use that device as standard NEC receiver.
957	   It could be possible make hack which reads whole code directly
958	   from device memory... */
959
960	ret = anysee_ctrl_msg(d, buf, sizeof(buf), ircode, sizeof(ircode));
961	if (ret)
962		return ret;
963
964	if (ircode[0]) {
965		deb_rc("%s: key pressed %02x\n", __func__, ircode[1]);
966		rc_keydown(d->rc_dev, 0x08 << 8 | ircode[1], 0);
967	}
968
969	return 0;
970}
971
972/* DVB USB Driver stuff */
973static struct dvb_usb_device_properties anysee_properties;
974
975static int anysee_probe(struct usb_interface *intf,
976			const struct usb_device_id *id)
977{
978	struct dvb_usb_device *d;
979	struct usb_host_interface *alt;
980	int ret;
981
982	/* There is one interface with two alternate settings.
983	   Alternate setting 0 is for bulk transfer.
984	   Alternate setting 1 is for isochronous transfer.
985	   We use bulk transfer (alternate setting 0). */
986	if (intf->num_altsetting < 1)
987		return -ENODEV;
988
989	/*
990	 * Anysee is always warm (its USB-bridge, Cypress FX2, uploads
991	 * firmware from eeprom).  If dvb_usb_device_init() succeeds that
992	 * means d is a valid pointer.
993	 */
994	ret = dvb_usb_device_init(intf, &anysee_properties, THIS_MODULE, &d,
995		adapter_nr);
996	if (ret)
997		return ret;
998
999	alt = usb_altnum_to_altsetting(intf, 0);
1000	if (alt == NULL) {
1001		deb_info("%s: no alt found!\n", __func__);
1002		return -ENODEV;
1003	}
1004
1005	ret = usb_set_interface(d->udev, alt->desc.bInterfaceNumber,
1006		alt->desc.bAlternateSetting);
1007	if (ret)
1008		return ret;
1009
1010	return anysee_init(d);
1011}
1012
1013static struct usb_device_id anysee_table[] = {
1014	{ USB_DEVICE(USB_VID_CYPRESS, USB_PID_ANYSEE) },
1015	{ USB_DEVICE(USB_VID_AMT,     USB_PID_ANYSEE) },
1016	{ }		/* Terminating entry */
1017};
1018MODULE_DEVICE_TABLE(usb, anysee_table);
1019
1020static struct dvb_usb_device_properties anysee_properties = {
1021	.caps             = DVB_USB_IS_AN_I2C_ADAPTER,
1022
1023	.usb_ctrl         = DEVICE_SPECIFIC,
1024
1025	.size_of_priv     = sizeof(struct anysee_state),
1026
1027	.num_adapters = 1,
1028	.adapter = {
1029		{
1030			.num_frontends    = 2,
1031			.frontend_ctrl    = anysee_frontend_ctrl,
1032			.streaming_ctrl   = anysee_streaming_ctrl,
1033			.frontend_attach  = anysee_frontend_attach,
1034			.tuner_attach     = anysee_tuner_attach,
1035			.stream = {
1036				.type = USB_BULK,
1037				.count = 8,
1038				.endpoint = 0x82,
1039				.u = {
1040					.bulk = {
1041						.buffersize = (16*512),
1042					}
1043				}
1044			},
1045		}
1046	},
1047
1048	.rc.core = {
1049		.rc_codes         = RC_MAP_ANYSEE,
1050		.protocol         = RC_TYPE_OTHER,
1051		.module_name      = "anysee",
1052		.rc_query         = anysee_rc_query,
1053		.rc_interval      = 250,  /* windows driver uses 500ms */
1054	},
1055
1056	.i2c_algo         = &anysee_i2c_algo,
1057
1058	.generic_bulk_ctrl_endpoint = 1,
1059
1060	.num_device_descs = 1,
1061	.devices = {
1062		{
1063			.name = "Anysee DVB USB2.0",
1064			.cold_ids = {NULL},
1065			.warm_ids = {&anysee_table[0],
1066				     &anysee_table[1], NULL},
1067		},
1068	}
1069};
1070
1071static struct usb_driver anysee_driver = {
1072	.name       = "dvb_usb_anysee",
1073	.probe      = anysee_probe,
1074	.disconnect = dvb_usb_device_exit,
1075	.id_table   = anysee_table,
1076};
1077
1078/* module stuff */
1079static int __init anysee_module_init(void)
1080{
1081	int ret;
1082
1083	ret = usb_register(&anysee_driver);
1084	if (ret)
1085		err("%s: usb_register failed. Error number %d", __func__, ret);
1086
1087	return ret;
1088}
1089
1090static void __exit anysee_module_exit(void)
1091{
1092	/* deregister this driver from the USB subsystem */
1093	usb_deregister(&anysee_driver);
1094}
1095
1096module_init(anysee_module_init);
1097module_exit(anysee_module_exit);
1098
1099MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1100MODULE_DESCRIPTION("Driver Anysee E30 DVB-C & DVB-T USB2.0");
1101MODULE_LICENSE("GPL");
1102