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