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