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 #include "cxd2820r.h"
45 
46 /* debug */
47 static int dvb_usb_anysee_debug;
48 module_param_named(debug, dvb_usb_anysee_debug, int, 0644);
49 MODULE_PARM_DESC(debug, "set debugging level" DVB_USB_DEBUG_STATUS);
50 static int dvb_usb_anysee_delsys;
51 module_param_named(delsys, dvb_usb_anysee_delsys, int, 0644);
52 MODULE_PARM_DESC(delsys, "select delivery mode (0=DVB-C, 1=DVB-T)");
53 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
54 
55 static DEFINE_MUTEX(anysee_usb_mutex);
56 
anysee_ctrl_msg(struct dvb_usb_device * d,u8 * sbuf,u8 slen,u8 * rbuf,u8 rlen)57 static int anysee_ctrl_msg(struct dvb_usb_device *d, u8 *sbuf, u8 slen,
58 	u8 *rbuf, u8 rlen)
59 {
60 	struct anysee_state *state = d->priv;
61 	int act_len, ret;
62 	u8 buf[64];
63 
64 	memcpy(&buf[0], sbuf, slen);
65 	buf[60] = state->seq++;
66 
67 	if (mutex_lock_interruptible(&anysee_usb_mutex) < 0)
68 		return -EAGAIN;
69 
70 	deb_xfer(">>> ");
71 	debug_dump(buf, slen, deb_xfer);
72 
73 	/* We need receive one message more after dvb_usb_generic_rw due
74 	   to weird transaction flow, which is 1 x send + 2 x receive. */
75 	ret = dvb_usb_generic_rw(d, buf, sizeof(buf), buf, sizeof(buf), 0);
76 	if (!ret) {
77 		/* receive 2nd answer */
78 		ret = usb_bulk_msg(d->udev, usb_rcvbulkpipe(d->udev,
79 			d->props.generic_bulk_ctrl_endpoint), buf, sizeof(buf),
80 			&act_len, 2000);
81 		if (ret)
82 			err("%s: recv bulk message failed: %d", __func__, ret);
83 		else {
84 			deb_xfer("<<< ");
85 			debug_dump(buf, rlen, deb_xfer);
86 
87 			if (buf[63] != 0x4f)
88 				deb_info("%s: cmd failed\n", __func__);
89 		}
90 	}
91 
92 	/* read request, copy returned data to return buf */
93 	if (!ret && rbuf && rlen)
94 		memcpy(rbuf, buf, rlen);
95 
96 	mutex_unlock(&anysee_usb_mutex);
97 
98 	return ret;
99 }
100 
anysee_read_reg(struct dvb_usb_device * d,u16 reg,u8 * val)101 static int anysee_read_reg(struct dvb_usb_device *d, u16 reg, u8 *val)
102 {
103 	u8 buf[] = {CMD_REG_READ, reg >> 8, reg & 0xff, 0x01};
104 	int ret;
105 	ret = anysee_ctrl_msg(d, buf, sizeof(buf), val, 1);
106 	deb_info("%s: reg:%04x val:%02x\n", __func__, reg, *val);
107 	return ret;
108 }
109 
anysee_write_reg(struct dvb_usb_device * d,u16 reg,u8 val)110 static int anysee_write_reg(struct dvb_usb_device *d, u16 reg, u8 val)
111 {
112 	u8 buf[] = {CMD_REG_WRITE, reg >> 8, reg & 0xff, 0x01, val};
113 	deb_info("%s: reg:%04x val:%02x\n", __func__, reg, val);
114 	return anysee_ctrl_msg(d, buf, sizeof(buf), NULL, 0);
115 }
116 
117 /* write single register with mask */
anysee_wr_reg_mask(struct dvb_usb_device * d,u16 reg,u8 val,u8 mask)118 static int anysee_wr_reg_mask(struct dvb_usb_device *d, u16 reg, u8 val,
119 	u8 mask)
120 {
121 	int ret;
122 	u8 tmp;
123 
124 	/* no need for read if whole reg is written */
125 	if (mask != 0xff) {
126 		ret = anysee_read_reg(d, reg, &tmp);
127 		if (ret)
128 			return ret;
129 
130 		val &= mask;
131 		tmp &= ~mask;
132 		val |= tmp;
133 	}
134 
135 	return anysee_write_reg(d, reg, val);
136 }
137 
138 /* read single register with mask */
anysee_rd_reg_mask(struct dvb_usb_device * d,u16 reg,u8 * val,u8 mask)139 static int anysee_rd_reg_mask(struct dvb_usb_device *d, u16 reg, u8 *val,
140 	u8 mask)
141 {
142 	int ret, i;
143 	u8 tmp;
144 
145 	ret = anysee_read_reg(d, reg, &tmp);
146 	if (ret)
147 		return ret;
148 
149 	tmp &= mask;
150 
151 	/* find position of the first bit */
152 	for (i = 0; i < 8; i++) {
153 		if ((mask >> i) & 0x01)
154 			break;
155 	}
156 	*val = tmp >> i;
157 
158 	return 0;
159 }
160 
anysee_get_hw_info(struct dvb_usb_device * d,u8 * id)161 static int anysee_get_hw_info(struct dvb_usb_device *d, u8 *id)
162 {
163 	u8 buf[] = {CMD_GET_HW_INFO};
164 	return anysee_ctrl_msg(d, buf, sizeof(buf), id, 3);
165 }
166 
anysee_streaming_ctrl(struct dvb_usb_adapter * adap,int onoff)167 static int anysee_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
168 {
169 	u8 buf[] = {CMD_STREAMING_CTRL, (u8)onoff, 0x00};
170 	deb_info("%s: onoff:%02x\n", __func__, onoff);
171 	return anysee_ctrl_msg(adap->dev, buf, sizeof(buf), NULL, 0);
172 }
173 
anysee_led_ctrl(struct dvb_usb_device * d,u8 mode,u8 interval)174 static int anysee_led_ctrl(struct dvb_usb_device *d, u8 mode, u8 interval)
175 {
176 	u8 buf[] = {CMD_LED_AND_IR_CTRL, 0x01, mode, interval};
177 	deb_info("%s: state:%02x interval:%02x\n", __func__, mode, interval);
178 	return anysee_ctrl_msg(d, buf, sizeof(buf), NULL, 0);
179 }
180 
anysee_ir_ctrl(struct dvb_usb_device * d,u8 onoff)181 static int anysee_ir_ctrl(struct dvb_usb_device *d, u8 onoff)
182 {
183 	u8 buf[] = {CMD_LED_AND_IR_CTRL, 0x02, onoff};
184 	deb_info("%s: onoff:%02x\n", __func__, onoff);
185 	return anysee_ctrl_msg(d, buf, sizeof(buf), NULL, 0);
186 }
187 
188 /* I2C */
anysee_master_xfer(struct i2c_adapter * adap,struct i2c_msg * msg,int num)189 static int anysee_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msg,
190 	int num)
191 {
192 	struct dvb_usb_device *d = i2c_get_adapdata(adap);
193 	int ret = 0, inc, i = 0;
194 	u8 buf[52]; /* 4 + 48 (I2C WR USB command header + I2C WR max) */
195 
196 	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
197 		return -EAGAIN;
198 
199 	while (i < num) {
200 		if (num > i + 1 && (msg[i+1].flags & I2C_M_RD)) {
201 			if (msg[i].len > 2 || msg[i+1].len > 60) {
202 				ret = -EOPNOTSUPP;
203 				break;
204 			}
205 			buf[0] = CMD_I2C_READ;
206 			buf[1] = (msg[i].addr << 1) | 0x01;
207 			buf[2] = msg[i].buf[0];
208 			buf[3] = msg[i].buf[1];
209 			buf[4] = msg[i].len-1;
210 			buf[5] = msg[i+1].len;
211 			ret = anysee_ctrl_msg(d, buf, 6, msg[i+1].buf,
212 				msg[i+1].len);
213 			inc = 2;
214 		} else {
215 			if (msg[i].len > 48) {
216 				ret = -EOPNOTSUPP;
217 				break;
218 			}
219 			buf[0] = CMD_I2C_WRITE;
220 			buf[1] = (msg[i].addr << 1);
221 			buf[2] = msg[i].len;
222 			buf[3] = 0x01;
223 			memcpy(&buf[4], msg[i].buf, msg[i].len);
224 			ret = anysee_ctrl_msg(d, buf, 4 + msg[i].len, NULL, 0);
225 			inc = 1;
226 		}
227 		if (ret)
228 			break;
229 
230 		i += inc;
231 	}
232 
233 	mutex_unlock(&d->i2c_mutex);
234 
235 	return ret ? ret : i;
236 }
237 
anysee_i2c_func(struct i2c_adapter * adapter)238 static u32 anysee_i2c_func(struct i2c_adapter *adapter)
239 {
240 	return I2C_FUNC_I2C;
241 }
242 
243 static struct i2c_algorithm anysee_i2c_algo = {
244 	.master_xfer   = anysee_master_xfer,
245 	.functionality = anysee_i2c_func,
246 };
247 
anysee_mt352_demod_init(struct dvb_frontend * fe)248 static int anysee_mt352_demod_init(struct dvb_frontend *fe)
249 {
250 	static u8 clock_config[]   = { CLOCK_CTL,  0x38, 0x28 };
251 	static u8 reset[]          = { RESET,      0x80 };
252 	static u8 adc_ctl_1_cfg[]  = { ADC_CTL_1,  0x40 };
253 	static u8 agc_cfg[]        = { AGC_TARGET, 0x28, 0x20 };
254 	static u8 gpp_ctl_cfg[]    = { GPP_CTL,    0x33 };
255 	static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
256 
257 	mt352_write(fe, clock_config,   sizeof(clock_config));
258 	udelay(200);
259 	mt352_write(fe, reset,          sizeof(reset));
260 	mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
261 
262 	mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
263 	mt352_write(fe, gpp_ctl_cfg,    sizeof(gpp_ctl_cfg));
264 	mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
265 
266 	return 0;
267 }
268 
269 /* Callbacks for DVB USB */
270 static struct tda10023_config anysee_tda10023_config = {
271 	.demod_address = (0x1a >> 1),
272 	.invert = 0,
273 	.xtal   = 16000000,
274 	.pll_m  = 11,
275 	.pll_p  = 3,
276 	.pll_n  = 1,
277 	.output_mode = TDA10023_OUTPUT_MODE_PARALLEL_C,
278 	.deltaf = 0xfeeb,
279 };
280 
281 static struct mt352_config anysee_mt352_config = {
282 	.demod_address = (0x1e >> 1),
283 	.demod_init    = anysee_mt352_demod_init,
284 };
285 
286 static struct zl10353_config anysee_zl10353_config = {
287 	.demod_address = (0x1e >> 1),
288 	.parallel_ts = 1,
289 };
290 
291 static struct zl10353_config anysee_zl10353_tda18212_config2 = {
292 	.demod_address = (0x1e >> 1),
293 	.parallel_ts = 1,
294 	.disable_i2c_gate_ctrl = 1,
295 	.no_tuner = 1,
296 	.if2 = 41500,
297 };
298 
299 static struct zl10353_config anysee_zl10353_tda18212_config = {
300 	.demod_address = (0x18 >> 1),
301 	.parallel_ts = 1,
302 	.disable_i2c_gate_ctrl = 1,
303 	.no_tuner = 1,
304 	.if2 = 41500,
305 };
306 
307 static struct tda10023_config anysee_tda10023_tda18212_config = {
308 	.demod_address = (0x1a >> 1),
309 	.xtal   = 16000000,
310 	.pll_m  = 12,
311 	.pll_p  = 3,
312 	.pll_n  = 1,
313 	.output_mode = TDA10023_OUTPUT_MODE_PARALLEL_B,
314 	.deltaf = 0xba02,
315 };
316 
317 static struct tda18212_config anysee_tda18212_config = {
318 	.i2c_address = (0xc0 >> 1),
319 	.if_dvbt_6 = 4150,
320 	.if_dvbt_7 = 4150,
321 	.if_dvbt_8 = 4150,
322 	.if_dvbc = 5000,
323 };
324 
325 static struct tda18212_config anysee_tda18212_config2 = {
326 	.i2c_address = 0x60 /* (0xc0 >> 1) */,
327 	.if_dvbt_6 = 3550,
328 	.if_dvbt_7 = 3700,
329 	.if_dvbt_8 = 4150,
330 	.if_dvbt2_6 = 3250,
331 	.if_dvbt2_7 = 4000,
332 	.if_dvbt2_8 = 4000,
333 	.if_dvbc = 5000,
334 };
335 
336 static struct cx24116_config anysee_cx24116_config = {
337 	.demod_address = (0xaa >> 1),
338 	.mpg_clk_pos_pol = 0x00,
339 	.i2c_wr_max = 48,
340 };
341 
342 static struct stv0900_config anysee_stv0900_config = {
343 	.demod_address = (0xd0 >> 1),
344 	.demod_mode = 0,
345 	.xtal = 8000000,
346 	.clkmode = 3,
347 	.diseqc_mode = 2,
348 	.tun1_maddress = 0,
349 	.tun1_adc = 1, /* 1 Vpp */
350 	.path1_mode = 3,
351 };
352 
353 static struct stv6110_config anysee_stv6110_config = {
354 	.i2c_address = (0xc0 >> 1),
355 	.mclk = 16000000,
356 	.clk_div = 1,
357 };
358 
359 static struct isl6423_config anysee_isl6423_config = {
360 	.current_max = SEC_CURRENT_800m,
361 	.curlim  = SEC_CURRENT_LIM_OFF,
362 	.mod_extern = 1,
363 	.addr = (0x10 >> 1),
364 };
365 
366 static struct cxd2820r_config anysee_cxd2820r_config = {
367 	.i2c_address = 0x6d, /* (0xda >> 1) */
368 	.ts_mode = 0x38,
369 };
370 
371 /*
372  * New USB device strings: Mfr=1, Product=2, SerialNumber=0
373  * Manufacturer: AMT.CO.KR
374  *
375  * E30 VID=04b4 PID=861f HW=2 FW=2.1 Product=????????
376  * PCB: ?
377  * parts: DNOS404ZH102A(MT352, DTT7579(?))
378  *
379  * E30 VID=04b4 PID=861f HW=2 FW=2.1 "anysee-T(LP)"
380  * PCB: PCB 507T (rev1.61)
381  * parts: DNOS404ZH103A(ZL10353, DTT7579(?))
382  * OEA=0a OEB=00 OEC=00 OED=ff OEE=00
383  * IOA=45 IOB=ff IOC=00 IOD=ff IOE=00
384  *
385  * E30 Plus VID=04b4 PID=861f HW=6 FW=1.0 "anysee"
386  * PCB: 507CD (rev1.1)
387  * parts: DNOS404ZH103A(ZL10353, DTT7579(?)), CST56I01
388  * OEA=80 OEB=00 OEC=00 OED=ff OEE=fe
389  * IOA=4f IOB=ff IOC=00 IOD=06 IOE=01
390  * IOD[0] ZL10353 1=enabled
391  * IOA[7] TS 0=enabled
392  * tuner is not behind ZL10353 I2C-gate (no care if gate disabled or not)
393  *
394  * E30 C Plus VID=04b4 PID=861f HW=10 FW=1.0 "anysee-DC(LP)"
395  * PCB: 507DC (rev0.2)
396  * parts: TDA10023, DTOS403IH102B TM, CST56I01
397  * OEA=80 OEB=00 OEC=00 OED=ff OEE=fe
398  * IOA=4f IOB=ff IOC=00 IOD=26 IOE=01
399  * IOD[0] TDA10023 1=enabled
400  *
401  * E30 S2 Plus VID=04b4 PID=861f HW=11 FW=0.1 "anysee-S2(LP)"
402  * PCB: 507SI (rev2.1)
403  * parts: BS2N10WCC01(CX24116, CX24118), ISL6423, TDA8024
404  * OEA=80 OEB=00 OEC=ff OED=ff OEE=fe
405  * IOA=4d IOB=ff IOC=00 IOD=26 IOE=01
406  * IOD[0] CX24116 1=enabled
407  *
408  * E30 C Plus VID=1c73 PID=861f HW=15 FW=1.2 "anysee-FA(LP)"
409  * PCB: 507FA (rev0.4)
410  * parts: TDA10023, DTOS403IH102B TM, TDA8024
411  * OEA=80 OEB=00 OEC=ff OED=ff OEE=ff
412  * IOA=4d IOB=ff IOC=00 IOD=00 IOE=c0
413  * IOD[5] TDA10023 1=enabled
414  * IOE[0] tuner 1=enabled
415  *
416  * E30 Combo Plus VID=1c73 PID=861f HW=15 FW=1.2 "anysee-FA(LP)"
417  * PCB: 507FA (rev1.1)
418  * parts: ZL10353, TDA10023, DTOS403IH102B TM, TDA8024
419  * OEA=80 OEB=00 OEC=ff OED=ff OEE=ff
420  * IOA=4d IOB=ff IOC=00 IOD=00 IOE=c0
421  * DVB-C:
422  * IOD[5] TDA10023 1=enabled
423  * IOE[0] tuner 1=enabled
424  * DVB-T:
425  * IOD[0] ZL10353 1=enabled
426  * IOE[0] tuner 0=enabled
427  * tuner is behind ZL10353 I2C-gate
428  *
429  * E7 TC VID=1c73 PID=861f HW=18 FW=0.7 AMTCI=0.5 "anysee-E7TC(LP)"
430  * PCB: 508TC (rev0.6)
431  * parts: ZL10353, TDA10023, DNOD44CDH086A(TDA18212)
432  * OEA=80 OEB=00 OEC=03 OED=f7 OEE=ff
433  * IOA=4d IOB=00 IOC=cc IOD=48 IOE=e4
434  * IOA[7] TS 1=enabled
435  * IOE[4] TDA18212 1=enabled
436  * DVB-C:
437  * IOD[6] ZL10353 0=disabled
438  * IOD[5] TDA10023 1=enabled
439  * IOE[0] IF 1=enabled
440  * DVB-T:
441  * IOD[5] TDA10023 0=disabled
442  * IOD[6] ZL10353 1=enabled
443  * IOE[0] IF 0=enabled
444  *
445  * E7 S2 VID=1c73 PID=861f HW=19 FW=0.4 AMTCI=0.5 "anysee-E7S2(LP)"
446  * PCB: 508S2 (rev0.7)
447  * parts: DNBU10512IST(STV0903, STV6110), ISL6423
448  * OEA=80 OEB=00 OEC=03 OED=f7 OEE=ff
449  * IOA=4d IOB=00 IOC=c4 IOD=08 IOE=e4
450  * IOA[7] TS 1=enabled
451  * IOE[5] STV0903 1=enabled
452  *
453  * E7 T2C VID=1c73 PID=861f HW=20 FW=0.1 AMTCI=0.5 "anysee-E7T2C(LP)"
454  * PCB: 508T2C (rev0.3)
455  * parts: DNOQ44QCH106A(CXD2820R, TDA18212), TDA8024
456  * OEA=80 OEB=00 OEC=03 OED=f7 OEE=ff
457  * IOA=4d IOB=00 IOC=cc IOD=48 IOE=e4
458  * IOA[7] TS 1=enabled
459  * IOE[5] CXD2820R 1=enabled
460  *
461  * E7 PTC VID=1c73 PID=861f HW=21 FW=0.1 AMTCI=?? "anysee-E7PTC(LP)"
462  * PCB: 508PTC (rev0.5)
463  * parts: ZL10353, TDA10023, DNOD44CDH086A(TDA18212)
464  * OEA=80 OEB=00 OEC=03 OED=f7 OEE=ff
465  * IOA=4d IOB=00 IOC=cc IOD=48 IOE=e4
466  * IOA[7] TS 1=enabled
467  * IOE[4] TDA18212 1=enabled
468  * DVB-C:
469  * IOD[6] ZL10353 0=disabled
470  * IOD[5] TDA10023 1=enabled
471  * IOE[0] IF 1=enabled
472  * DVB-T:
473  * IOD[5] TDA10023 0=disabled
474  * IOD[6] ZL10353 1=enabled
475  * IOE[0] IF 0=enabled
476  *
477  * E7 PS2 VID=1c73 PID=861f HW=22 FW=0.1 AMTCI=?? "anysee-E7PS2(LP)"
478  * PCB: 508PS2 (rev0.4)
479  * parts: DNBU10512IST(STV0903, STV6110), ISL6423
480  * OEA=80 OEB=00 OEC=03 OED=f7 OEE=ff
481  * IOA=4d IOB=00 IOC=c4 IOD=08 IOE=e4
482  * IOA[7] TS 1=enabled
483  * IOE[5] STV0903 1=enabled
484  */
485 
486 
487 /* external I2C gate used for DNOD44CDH086A(TDA18212) tuner module */
anysee_i2c_gate_ctrl(struct dvb_frontend * fe,int enable)488 static int anysee_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
489 {
490 	struct dvb_usb_adapter *adap = fe->dvb->priv;
491 
492 	/* enable / disable tuner access on IOE[4] */
493 	return anysee_wr_reg_mask(adap->dev, REG_IOE, (enable << 4), 0x10);
494 }
495 
anysee_frontend_ctrl(struct dvb_frontend * fe,int onoff)496 static int anysee_frontend_ctrl(struct dvb_frontend *fe, int onoff)
497 {
498 	struct dvb_usb_adapter *adap = fe->dvb->priv;
499 	struct anysee_state *state = adap->dev->priv;
500 	int ret;
501 
502 	deb_info("%s: fe=%d onoff=%d\n", __func__, fe->id, onoff);
503 
504 	/* no frontend sleep control */
505 	if (onoff == 0)
506 		return 0;
507 
508 	switch (state->hw) {
509 	case ANYSEE_HW_507FA: /* 15 */
510 		/* E30 Combo Plus */
511 		/* E30 C Plus */
512 
513 		if ((fe->id ^ dvb_usb_anysee_delsys) == 0)  {
514 			/* disable DVB-T demod on IOD[0] */
515 			ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (0 << 0),
516 				0x01);
517 			if (ret)
518 				goto error;
519 
520 			/* enable DVB-C demod on IOD[5] */
521 			ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (1 << 5),
522 				0x20);
523 			if (ret)
524 				goto error;
525 
526 			/* enable DVB-C tuner on IOE[0] */
527 			ret = anysee_wr_reg_mask(adap->dev, REG_IOE, (1 << 0),
528 				0x01);
529 			if (ret)
530 				goto error;
531 		} else {
532 			/* disable DVB-C demod on IOD[5] */
533 			ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (0 << 5),
534 				0x20);
535 			if (ret)
536 				goto error;
537 
538 			/* enable DVB-T demod on IOD[0] */
539 			ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (1 << 0),
540 				0x01);
541 			if (ret)
542 				goto error;
543 
544 			/* enable DVB-T tuner on IOE[0] */
545 			ret = anysee_wr_reg_mask(adap->dev, REG_IOE, (0 << 0),
546 				0x01);
547 			if (ret)
548 				goto error;
549 		}
550 
551 		break;
552 	case ANYSEE_HW_508TC: /* 18 */
553 	case ANYSEE_HW_508PTC: /* 21 */
554 		/* E7 TC */
555 		/* E7 PTC */
556 
557 		if ((fe->id ^ dvb_usb_anysee_delsys) == 0)  {
558 			/* disable DVB-T demod on IOD[6] */
559 			ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (0 << 6),
560 				0x40);
561 			if (ret)
562 				goto error;
563 
564 			/* enable DVB-C demod on IOD[5] */
565 			ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (1 << 5),
566 				0x20);
567 			if (ret)
568 				goto error;
569 
570 			/* enable IF route on IOE[0] */
571 			ret = anysee_wr_reg_mask(adap->dev, REG_IOE, (1 << 0),
572 				0x01);
573 			if (ret)
574 				goto error;
575 		} else {
576 			/* disable DVB-C demod on IOD[5] */
577 			ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (0 << 5),
578 				0x20);
579 			if (ret)
580 				goto error;
581 
582 			/* enable DVB-T demod on IOD[6] */
583 			ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (1 << 6),
584 				0x40);
585 			if (ret)
586 				goto error;
587 
588 			/* enable IF route on IOE[0] */
589 			ret = anysee_wr_reg_mask(adap->dev, REG_IOE, (0 << 0),
590 				0x01);
591 			if (ret)
592 				goto error;
593 		}
594 
595 		break;
596 	default:
597 		ret = 0;
598 	}
599 
600 error:
601 	return ret;
602 }
603 
anysee_frontend_attach(struct dvb_usb_adapter * adap)604 static int anysee_frontend_attach(struct dvb_usb_adapter *adap)
605 {
606 	int ret;
607 	struct anysee_state *state = adap->dev->priv;
608 	u8 hw_info[3];
609 	u8 tmp;
610 	struct i2c_msg msg[2] = {
611 		{
612 			.addr = anysee_tda18212_config.i2c_address,
613 			.flags = 0,
614 			.len = 1,
615 			.buf = "\x00",
616 		}, {
617 			.addr = anysee_tda18212_config.i2c_address,
618 			.flags = I2C_M_RD,
619 			.len = 1,
620 			.buf = &tmp,
621 		}
622 	};
623 
624 	/* detect hardware only once */
625 	if (adap->fe_adap[0].fe == NULL) {
626 		/* Check which hardware we have.
627 		 * We must do this call two times to get reliable values
628 		 * (hw/fw bug).
629 		 */
630 		ret = anysee_get_hw_info(adap->dev, hw_info);
631 		if (ret)
632 			goto error;
633 
634 		ret = anysee_get_hw_info(adap->dev, hw_info);
635 		if (ret)
636 			goto error;
637 
638 		/* Meaning of these info bytes are guessed. */
639 		info("firmware version:%d.%d hardware id:%d",
640 			hw_info[1], hw_info[2], hw_info[0]);
641 
642 		state->hw = hw_info[0];
643 	}
644 
645 	/* set current frondend ID for devices having two frondends */
646 	if (adap->fe_adap[0].fe)
647 		state->fe_id++;
648 
649 	switch (state->hw) {
650 	case ANYSEE_HW_507T: /* 2 */
651 		/* E30 */
652 
653 		if (state->fe_id)
654 			break;
655 
656 		/* attach demod */
657 		adap->fe_adap[0].fe = dvb_attach(mt352_attach,
658 			&anysee_mt352_config, &adap->dev->i2c_adap);
659 		if (adap->fe_adap[0].fe)
660 			break;
661 
662 		/* attach demod */
663 		adap->fe_adap[0].fe = dvb_attach(zl10353_attach,
664 			&anysee_zl10353_config, &adap->dev->i2c_adap);
665 
666 		break;
667 	case ANYSEE_HW_507CD: /* 6 */
668 		/* E30 Plus */
669 
670 		if (state->fe_id)
671 			break;
672 
673 		/* enable DVB-T demod on IOD[0] */
674 		ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (1 << 0), 0x01);
675 		if (ret)
676 			goto error;
677 
678 		/* enable transport stream on IOA[7] */
679 		ret = anysee_wr_reg_mask(adap->dev, REG_IOA, (0 << 7), 0x80);
680 		if (ret)
681 			goto error;
682 
683 		/* attach demod */
684 		adap->fe_adap[0].fe = dvb_attach(zl10353_attach,
685 			&anysee_zl10353_config, &adap->dev->i2c_adap);
686 
687 		break;
688 	case ANYSEE_HW_507DC: /* 10 */
689 		/* E30 C Plus */
690 
691 		if (state->fe_id)
692 			break;
693 
694 		/* enable DVB-C demod on IOD[0] */
695 		ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (1 << 0), 0x01);
696 		if (ret)
697 			goto error;
698 
699 		/* attach demod */
700 		adap->fe_adap[0].fe = dvb_attach(tda10023_attach,
701 			&anysee_tda10023_config, &adap->dev->i2c_adap, 0x48);
702 
703 		break;
704 	case ANYSEE_HW_507SI: /* 11 */
705 		/* E30 S2 Plus */
706 
707 		if (state->fe_id)
708 			break;
709 
710 		/* enable DVB-S/S2 demod on IOD[0] */
711 		ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (1 << 0), 0x01);
712 		if (ret)
713 			goto error;
714 
715 		/* attach demod */
716 		adap->fe_adap[0].fe = dvb_attach(cx24116_attach,
717 			&anysee_cx24116_config, &adap->dev->i2c_adap);
718 
719 		break;
720 	case ANYSEE_HW_507FA: /* 15 */
721 		/* E30 Combo Plus */
722 		/* E30 C Plus */
723 
724 		/* enable tuner on IOE[4] */
725 		ret = anysee_wr_reg_mask(adap->dev, REG_IOE, (1 << 4), 0x10);
726 		if (ret)
727 			goto error;
728 
729 		/* probe TDA18212 */
730 		tmp = 0;
731 		ret = i2c_transfer(&adap->dev->i2c_adap, msg, 2);
732 		if (ret == 2 && tmp == 0xc7)
733 			deb_info("%s: TDA18212 found\n", __func__);
734 		else
735 			tmp = 0;
736 
737 		/* disable tuner on IOE[4] */
738 		ret = anysee_wr_reg_mask(adap->dev, REG_IOE, (0 << 4), 0x10);
739 		if (ret)
740 			goto error;
741 
742 		if ((state->fe_id ^ dvb_usb_anysee_delsys) == 0)  {
743 			/* disable DVB-T demod on IOD[0] */
744 			ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (0 << 0),
745 				0x01);
746 			if (ret)
747 				goto error;
748 
749 			/* enable DVB-C demod on IOD[5] */
750 			ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (1 << 5),
751 				0x20);
752 			if (ret)
753 				goto error;
754 
755 			/* attach demod */
756 			if (tmp == 0xc7) {
757 				/* TDA18212 config */
758 				adap->fe_adap[state->fe_id].fe = dvb_attach(
759 					tda10023_attach,
760 					&anysee_tda10023_tda18212_config,
761 					&adap->dev->i2c_adap, 0x48);
762 			} else {
763 				/* PLL config */
764 				adap->fe_adap[state->fe_id].fe = dvb_attach(
765 					tda10023_attach,
766 					&anysee_tda10023_config,
767 					&adap->dev->i2c_adap, 0x48);
768 			}
769 		} else {
770 			/* disable DVB-C demod on IOD[5] */
771 			ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (0 << 5),
772 				0x20);
773 			if (ret)
774 				goto error;
775 
776 			/* enable DVB-T demod on IOD[0] */
777 			ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (1 << 0),
778 				0x01);
779 			if (ret)
780 				goto error;
781 
782 			/* attach demod */
783 			if (tmp == 0xc7) {
784 				/* TDA18212 config */
785 				adap->fe_adap[state->fe_id].fe = dvb_attach(
786 					zl10353_attach,
787 					&anysee_zl10353_tda18212_config2,
788 					&adap->dev->i2c_adap);
789 			} else {
790 				/* PLL config */
791 				adap->fe_adap[state->fe_id].fe = dvb_attach(
792 					zl10353_attach,
793 					&anysee_zl10353_config,
794 					&adap->dev->i2c_adap);
795 			}
796 		}
797 
798 		/* I2C gate for DNOD44CDH086A(TDA18212) tuner module */
799 		if (tmp == 0xc7) {
800 			if (adap->fe_adap[state->fe_id].fe)
801 				adap->fe_adap[state->fe_id].fe->ops.i2c_gate_ctrl =
802 					anysee_i2c_gate_ctrl;
803 		}
804 
805 		break;
806 	case ANYSEE_HW_508TC: /* 18 */
807 	case ANYSEE_HW_508PTC: /* 21 */
808 		/* E7 TC */
809 		/* E7 PTC */
810 
811 		if ((state->fe_id ^ dvb_usb_anysee_delsys) == 0)  {
812 			/* disable DVB-T demod on IOD[6] */
813 			ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (0 << 6),
814 				0x40);
815 			if (ret)
816 				goto error;
817 
818 			/* enable DVB-C demod on IOD[5] */
819 			ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (1 << 5),
820 				0x20);
821 			if (ret)
822 				goto error;
823 
824 			/* attach demod */
825 			adap->fe_adap[state->fe_id].fe =
826 				dvb_attach(tda10023_attach,
827 				&anysee_tda10023_tda18212_config,
828 				&adap->dev->i2c_adap, 0x48);
829 		} else {
830 			/* disable DVB-C demod on IOD[5] */
831 			ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (0 << 5),
832 				0x20);
833 			if (ret)
834 				goto error;
835 
836 			/* enable DVB-T demod on IOD[6] */
837 			ret = anysee_wr_reg_mask(adap->dev, REG_IOD, (1 << 6),
838 				0x40);
839 			if (ret)
840 				goto error;
841 
842 			/* attach demod */
843 			adap->fe_adap[state->fe_id].fe =
844 				dvb_attach(zl10353_attach,
845 				&anysee_zl10353_tda18212_config,
846 				&adap->dev->i2c_adap);
847 		}
848 
849 		/* I2C gate for DNOD44CDH086A(TDA18212) tuner module */
850 		if (adap->fe_adap[state->fe_id].fe)
851 			adap->fe_adap[state->fe_id].fe->ops.i2c_gate_ctrl =
852 				anysee_i2c_gate_ctrl;
853 
854 		state->has_ci = true;
855 
856 		break;
857 	case ANYSEE_HW_508S2: /* 19 */
858 	case ANYSEE_HW_508PS2: /* 22 */
859 		/* E7 S2 */
860 		/* E7 PS2 */
861 
862 		if (state->fe_id)
863 			break;
864 
865 		/* enable DVB-S/S2 demod on IOE[5] */
866 		ret = anysee_wr_reg_mask(adap->dev, REG_IOE, (1 << 5), 0x20);
867 		if (ret)
868 			goto error;
869 
870 		/* attach demod */
871 		adap->fe_adap[0].fe = dvb_attach(stv0900_attach,
872 			&anysee_stv0900_config, &adap->dev->i2c_adap, 0);
873 
874 		state->has_ci = true;
875 
876 		break;
877 	case ANYSEE_HW_508T2C: /* 20 */
878 		/* E7 T2C */
879 
880 		if (state->fe_id)
881 			break;
882 
883 		/* enable DVB-T/T2/C demod on IOE[5] */
884 		ret = anysee_wr_reg_mask(adap->dev, REG_IOE, (1 << 5), 0x20);
885 		if (ret)
886 			goto error;
887 
888 		/* attach demod */
889 		adap->fe_adap[state->fe_id].fe = dvb_attach(cxd2820r_attach,
890 				&anysee_cxd2820r_config, &adap->dev->i2c_adap);
891 
892 		state->has_ci = true;
893 
894 		break;
895 	}
896 
897 	if (!adap->fe_adap[0].fe) {
898 		/* we have no frontend :-( */
899 		ret = -ENODEV;
900 		err("Unsupported Anysee version. " \
901 			"Please report the <linux-media@vger.kernel.org>.");
902 	}
903 error:
904 	return ret;
905 }
906 
anysee_tuner_attach(struct dvb_usb_adapter * adap)907 static int anysee_tuner_attach(struct dvb_usb_adapter *adap)
908 {
909 	struct anysee_state *state = adap->dev->priv;
910 	struct dvb_frontend *fe;
911 	int ret;
912 	deb_info("%s: fe=%d\n", __func__, state->fe_id);
913 
914 	switch (state->hw) {
915 	case ANYSEE_HW_507T: /* 2 */
916 		/* E30 */
917 
918 		/* attach tuner */
919 		fe = dvb_attach(dvb_pll_attach, adap->fe_adap[0].fe,
920 			(0xc2 >> 1), NULL, DVB_PLL_THOMSON_DTT7579);
921 
922 		break;
923 	case ANYSEE_HW_507CD: /* 6 */
924 		/* E30 Plus */
925 
926 		/* attach tuner */
927 		fe = dvb_attach(dvb_pll_attach, adap->fe_adap[0].fe,
928 			(0xc2 >> 1), &adap->dev->i2c_adap,
929 			DVB_PLL_THOMSON_DTT7579);
930 
931 		break;
932 	case ANYSEE_HW_507DC: /* 10 */
933 		/* E30 C Plus */
934 
935 		/* attach tuner */
936 		fe = dvb_attach(dvb_pll_attach, adap->fe_adap[0].fe,
937 			(0xc0 >> 1), &adap->dev->i2c_adap,
938 			DVB_PLL_SAMSUNG_DTOS403IH102A);
939 
940 		break;
941 	case ANYSEE_HW_507SI: /* 11 */
942 		/* E30 S2 Plus */
943 
944 		/* attach LNB controller */
945 		fe = dvb_attach(isl6423_attach, adap->fe_adap[0].fe,
946 			&adap->dev->i2c_adap, &anysee_isl6423_config);
947 
948 		break;
949 	case ANYSEE_HW_507FA: /* 15 */
950 		/* E30 Combo Plus */
951 		/* E30 C Plus */
952 
953 		/* Try first attach TDA18212 silicon tuner on IOE[4], if that
954 		 * fails attach old simple PLL. */
955 
956 		/* attach tuner */
957 		fe = dvb_attach(tda18212_attach, adap->fe_adap[state->fe_id].fe,
958 			&adap->dev->i2c_adap, &anysee_tda18212_config);
959 		if (fe)
960 			break;
961 
962 		/* attach tuner */
963 		fe = dvb_attach(dvb_pll_attach, adap->fe_adap[state->fe_id].fe,
964 			(0xc0 >> 1), &adap->dev->i2c_adap,
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 		fe = dvb_attach(tda18212_attach, adap->fe_adap[state->fe_id].fe,
975 			&adap->dev->i2c_adap, &anysee_tda18212_config);
976 
977 		break;
978 	case ANYSEE_HW_508S2: /* 19 */
979 	case ANYSEE_HW_508PS2: /* 22 */
980 		/* E7 S2 */
981 		/* E7 PS2 */
982 
983 		/* attach tuner */
984 		fe = dvb_attach(stv6110_attach, adap->fe_adap[0].fe,
985 			&anysee_stv6110_config, &adap->dev->i2c_adap);
986 
987 		if (fe) {
988 			/* attach LNB controller */
989 			fe = dvb_attach(isl6423_attach, adap->fe_adap[0].fe,
990 				&adap->dev->i2c_adap, &anysee_isl6423_config);
991 		}
992 
993 		break;
994 
995 	case ANYSEE_HW_508T2C: /* 20 */
996 		/* E7 T2C */
997 
998 		/* attach tuner */
999 		fe = dvb_attach(tda18212_attach, adap->fe_adap[state->fe_id].fe,
1000 			&adap->dev->i2c_adap, &anysee_tda18212_config2);
1001 
1002 		break;
1003 	default:
1004 		fe = NULL;
1005 	}
1006 
1007 	if (fe)
1008 		ret = 0;
1009 	else
1010 		ret = -ENODEV;
1011 
1012 	return ret;
1013 }
1014 
anysee_rc_query(struct dvb_usb_device * d)1015 static int anysee_rc_query(struct dvb_usb_device *d)
1016 {
1017 	u8 buf[] = {CMD_GET_IR_CODE};
1018 	u8 ircode[2];
1019 	int ret;
1020 
1021 	/* Remote controller is basic NEC using address byte 0x08.
1022 	   Anysee device RC query returns only two bytes, status and code,
1023 	   address byte is dropped. Also it does not return any value for
1024 	   NEC RCs having address byte other than 0x08. Due to that, we
1025 	   cannot use that device as standard NEC receiver.
1026 	   It could be possible make hack which reads whole code directly
1027 	   from device memory... */
1028 
1029 	ret = anysee_ctrl_msg(d, buf, sizeof(buf), ircode, sizeof(ircode));
1030 	if (ret)
1031 		return ret;
1032 
1033 	if (ircode[0]) {
1034 		deb_rc("%s: key pressed %02x\n", __func__, ircode[1]);
1035 		rc_keydown(d->rc_dev, 0x08 << 8 | ircode[1], 0);
1036 	}
1037 
1038 	return 0;
1039 }
1040 
anysee_ci_read_attribute_mem(struct dvb_ca_en50221 * ci,int slot,int addr)1041 static int anysee_ci_read_attribute_mem(struct dvb_ca_en50221 *ci, int slot,
1042 	int addr)
1043 {
1044 	struct dvb_usb_device *d = ci->data;
1045 	int ret;
1046 	u8 buf[] = {CMD_CI, 0x02, 0x40 | addr >> 8, addr & 0xff, 0x00, 1};
1047 	u8 val;
1048 
1049 	ret = anysee_ctrl_msg(d, buf, sizeof(buf), &val, 1);
1050 	if (ret)
1051 		return ret;
1052 
1053 	return val;
1054 }
1055 
anysee_ci_write_attribute_mem(struct dvb_ca_en50221 * ci,int slot,int addr,u8 val)1056 static int anysee_ci_write_attribute_mem(struct dvb_ca_en50221 *ci, int slot,
1057 	int addr, u8 val)
1058 {
1059 	struct dvb_usb_device *d = ci->data;
1060 	int ret;
1061 	u8 buf[] = {CMD_CI, 0x03, 0x40 | addr >> 8, addr & 0xff, 0x00, 1, val};
1062 
1063 	ret = anysee_ctrl_msg(d, buf, sizeof(buf), NULL, 0);
1064 	if (ret)
1065 		return ret;
1066 
1067 	return 0;
1068 }
1069 
anysee_ci_read_cam_control(struct dvb_ca_en50221 * ci,int slot,u8 addr)1070 static int anysee_ci_read_cam_control(struct dvb_ca_en50221 *ci, int slot,
1071 	u8 addr)
1072 {
1073 	struct dvb_usb_device *d = ci->data;
1074 	int ret;
1075 	u8 buf[] = {CMD_CI, 0x04, 0x40, addr, 0x00, 1};
1076 	u8 val;
1077 
1078 	ret = anysee_ctrl_msg(d, buf, sizeof(buf), &val, 1);
1079 	if (ret)
1080 		return ret;
1081 
1082 	return val;
1083 }
1084 
anysee_ci_write_cam_control(struct dvb_ca_en50221 * ci,int slot,u8 addr,u8 val)1085 static int anysee_ci_write_cam_control(struct dvb_ca_en50221 *ci, int slot,
1086 	u8 addr, u8 val)
1087 {
1088 	struct dvb_usb_device *d = ci->data;
1089 	int ret;
1090 	u8 buf[] = {CMD_CI, 0x05, 0x40, addr, 0x00, 1, val};
1091 
1092 	ret = anysee_ctrl_msg(d, buf, sizeof(buf), NULL, 0);
1093 	if (ret)
1094 		return ret;
1095 
1096 	return 0;
1097 }
1098 
anysee_ci_slot_reset(struct dvb_ca_en50221 * ci,int slot)1099 static int anysee_ci_slot_reset(struct dvb_ca_en50221 *ci, int slot)
1100 {
1101 	struct dvb_usb_device *d = ci->data;
1102 	int ret;
1103 	struct anysee_state *state = d->priv;
1104 
1105 	state->ci_cam_ready = jiffies + msecs_to_jiffies(1000);
1106 
1107 	ret = anysee_wr_reg_mask(d, REG_IOA, (0 << 7), 0x80);
1108 	if (ret)
1109 		return ret;
1110 
1111 	msleep(300);
1112 
1113 	ret = anysee_wr_reg_mask(d, REG_IOA, (1 << 7), 0x80);
1114 	if (ret)
1115 		return ret;
1116 
1117 	return 0;
1118 }
1119 
anysee_ci_slot_shutdown(struct dvb_ca_en50221 * ci,int slot)1120 static int anysee_ci_slot_shutdown(struct dvb_ca_en50221 *ci, int slot)
1121 {
1122 	struct dvb_usb_device *d = ci->data;
1123 	int ret;
1124 
1125 	ret = anysee_wr_reg_mask(d, REG_IOA, (0 << 7), 0x80);
1126 	if (ret)
1127 		return ret;
1128 
1129 	msleep(30);
1130 
1131 	ret = anysee_wr_reg_mask(d, REG_IOA, (1 << 7), 0x80);
1132 	if (ret)
1133 		return ret;
1134 
1135 	return 0;
1136 }
1137 
anysee_ci_slot_ts_enable(struct dvb_ca_en50221 * ci,int slot)1138 static int anysee_ci_slot_ts_enable(struct dvb_ca_en50221 *ci, int slot)
1139 {
1140 	struct dvb_usb_device *d = ci->data;
1141 	int ret;
1142 
1143 	ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 1), 0x02);
1144 	if (ret)
1145 		return ret;
1146 
1147 	return 0;
1148 }
1149 
anysee_ci_poll_slot_status(struct dvb_ca_en50221 * ci,int slot,int open)1150 static int anysee_ci_poll_slot_status(struct dvb_ca_en50221 *ci, int slot,
1151 	int open)
1152 {
1153 	struct dvb_usb_device *d = ci->data;
1154 	struct anysee_state *state = d->priv;
1155 	int ret;
1156 	u8 tmp;
1157 
1158 	ret = anysee_rd_reg_mask(d, REG_IOC, &tmp, 0x40);
1159 	if (ret)
1160 		return ret;
1161 
1162 	if (tmp == 0) {
1163 		ret = DVB_CA_EN50221_POLL_CAM_PRESENT;
1164 		if (time_after(jiffies, state->ci_cam_ready))
1165 			ret |= DVB_CA_EN50221_POLL_CAM_READY;
1166 	}
1167 
1168 	return ret;
1169 }
1170 
anysee_ci_init(struct dvb_usb_device * d)1171 static int anysee_ci_init(struct dvb_usb_device *d)
1172 {
1173 	struct anysee_state *state = d->priv;
1174 	int ret;
1175 
1176 	state->ci.owner               = THIS_MODULE;
1177 	state->ci.read_attribute_mem  = anysee_ci_read_attribute_mem;
1178 	state->ci.write_attribute_mem = anysee_ci_write_attribute_mem;
1179 	state->ci.read_cam_control    = anysee_ci_read_cam_control;
1180 	state->ci.write_cam_control   = anysee_ci_write_cam_control;
1181 	state->ci.slot_reset          = anysee_ci_slot_reset;
1182 	state->ci.slot_shutdown       = anysee_ci_slot_shutdown;
1183 	state->ci.slot_ts_enable      = anysee_ci_slot_ts_enable;
1184 	state->ci.poll_slot_status    = anysee_ci_poll_slot_status;
1185 	state->ci.data                = d;
1186 
1187 	ret = anysee_wr_reg_mask(d, REG_IOA, (1 << 7), 0x80);
1188 	if (ret)
1189 		return ret;
1190 
1191 	ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 2)|(0 << 1)|(0 << 0), 0x07);
1192 	if (ret)
1193 		return ret;
1194 
1195 	ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 2)|(1 << 1)|(1 << 0), 0x07);
1196 	if (ret)
1197 		return ret;
1198 
1199 	ret = dvb_ca_en50221_init(&d->adapter[0].dvb_adap, &state->ci, 0, 1);
1200 	if (ret)
1201 		return ret;
1202 
1203 	return 0;
1204 }
1205 
anysee_ci_release(struct dvb_usb_device * d)1206 static void anysee_ci_release(struct dvb_usb_device *d)
1207 {
1208 	struct anysee_state *state = d->priv;
1209 
1210 	/* detach CI */
1211 	if (state->has_ci)
1212 		dvb_ca_en50221_release(&state->ci);
1213 
1214 	return;
1215 }
1216 
anysee_init(struct dvb_usb_device * d)1217 static int anysee_init(struct dvb_usb_device *d)
1218 {
1219 	struct anysee_state *state = d->priv;
1220 	int ret;
1221 
1222 	/* LED light */
1223 	ret = anysee_led_ctrl(d, 0x01, 0x03);
1224 	if (ret)
1225 		return ret;
1226 
1227 	/* enable IR */
1228 	ret = anysee_ir_ctrl(d, 1);
1229 	if (ret)
1230 		return ret;
1231 
1232 	/* attach CI */
1233 	if (state->has_ci) {
1234 		ret = anysee_ci_init(d);
1235 		if (ret) {
1236 			state->has_ci = false;
1237 			return ret;
1238 		}
1239 	}
1240 
1241 	return 0;
1242 }
1243 
1244 /* DVB USB Driver stuff */
1245 static struct dvb_usb_device_properties anysee_properties;
1246 
anysee_probe(struct usb_interface * intf,const struct usb_device_id * id)1247 static int anysee_probe(struct usb_interface *intf,
1248 			const struct usb_device_id *id)
1249 {
1250 	struct dvb_usb_device *d;
1251 	struct usb_host_interface *alt;
1252 	int ret;
1253 
1254 	/* There is one interface with two alternate settings.
1255 	   Alternate setting 0 is for bulk transfer.
1256 	   Alternate setting 1 is for isochronous transfer.
1257 	   We use bulk transfer (alternate setting 0). */
1258 	if (intf->num_altsetting < 1)
1259 		return -ENODEV;
1260 
1261 	/*
1262 	 * Anysee is always warm (its USB-bridge, Cypress FX2, uploads
1263 	 * firmware from eeprom).  If dvb_usb_device_init() succeeds that
1264 	 * means d is a valid pointer.
1265 	 */
1266 	ret = dvb_usb_device_init(intf, &anysee_properties, THIS_MODULE, &d,
1267 		adapter_nr);
1268 	if (ret)
1269 		return ret;
1270 
1271 	alt = usb_altnum_to_altsetting(intf, 0);
1272 	if (alt == NULL) {
1273 		deb_info("%s: no alt found!\n", __func__);
1274 		return -ENODEV;
1275 	}
1276 
1277 	ret = usb_set_interface(d->udev, alt->desc.bInterfaceNumber,
1278 		alt->desc.bAlternateSetting);
1279 	if (ret)
1280 		return ret;
1281 
1282 	return anysee_init(d);
1283 }
1284 
anysee_disconnect(struct usb_interface * intf)1285 static void anysee_disconnect(struct usb_interface *intf)
1286 {
1287 	struct dvb_usb_device *d = usb_get_intfdata(intf);
1288 
1289 	anysee_ci_release(d);
1290 	dvb_usb_device_exit(intf);
1291 
1292 	return;
1293 }
1294 
1295 static struct usb_device_id anysee_table[] = {
1296 	{ USB_DEVICE(USB_VID_CYPRESS, USB_PID_ANYSEE) },
1297 	{ USB_DEVICE(USB_VID_AMT,     USB_PID_ANYSEE) },
1298 	{ }		/* Terminating entry */
1299 };
1300 MODULE_DEVICE_TABLE(usb, anysee_table);
1301 
1302 static struct dvb_usb_device_properties anysee_properties = {
1303 	.caps             = DVB_USB_IS_AN_I2C_ADAPTER,
1304 
1305 	.usb_ctrl         = DEVICE_SPECIFIC,
1306 
1307 	.size_of_priv     = sizeof(struct anysee_state),
1308 
1309 	.num_adapters = 1,
1310 	.adapter = {
1311 		{
1312 		.num_frontends    = 2,
1313 		.frontend_ctrl    = anysee_frontend_ctrl,
1314 		.fe = { {
1315 			.streaming_ctrl   = anysee_streaming_ctrl,
1316 			.frontend_attach  = anysee_frontend_attach,
1317 			.tuner_attach     = anysee_tuner_attach,
1318 			.stream = {
1319 				.type = USB_BULK,
1320 				.count = 8,
1321 				.endpoint = 0x82,
1322 				.u = {
1323 					.bulk = {
1324 						.buffersize = (16*512),
1325 					}
1326 				}
1327 			},
1328 		}, {
1329 			.streaming_ctrl   = anysee_streaming_ctrl,
1330 			.frontend_attach  = anysee_frontend_attach,
1331 			.tuner_attach     = anysee_tuner_attach,
1332 			.stream = {
1333 				.type = USB_BULK,
1334 				.count = 8,
1335 				.endpoint = 0x82,
1336 				.u = {
1337 					.bulk = {
1338 						.buffersize = (16*512),
1339 					}
1340 				}
1341 			},
1342 		} },
1343 		}
1344 	},
1345 
1346 	.rc.core = {
1347 		.rc_codes         = RC_MAP_ANYSEE,
1348 		.protocol         = RC_TYPE_OTHER,
1349 		.module_name      = "anysee",
1350 		.rc_query         = anysee_rc_query,
1351 		.rc_interval      = 250,  /* windows driver uses 500ms */
1352 	},
1353 
1354 	.i2c_algo         = &anysee_i2c_algo,
1355 
1356 	.generic_bulk_ctrl_endpoint = 1,
1357 
1358 	.num_device_descs = 1,
1359 	.devices = {
1360 		{
1361 			.name = "Anysee DVB USB2.0",
1362 			.cold_ids = {NULL},
1363 			.warm_ids = {&anysee_table[0],
1364 				     &anysee_table[1], NULL},
1365 		},
1366 	}
1367 };
1368 
1369 static struct usb_driver anysee_driver = {
1370 	.name       = "dvb_usb_anysee",
1371 	.probe      = anysee_probe,
1372 	.disconnect = anysee_disconnect,
1373 	.id_table   = anysee_table,
1374 };
1375 
1376 module_usb_driver(anysee_driver);
1377 
1378 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1379 MODULE_DESCRIPTION("Driver Anysee E30 DVB-C & DVB-T USB2.0");
1380 MODULE_LICENSE("GPL");
1381