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