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