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