/home/lenb/src/to-linus-stable branch 'acpi-2.6.12'
[linux-2.6] / drivers / media / dvb / bt8xx / dst.c
1 /*
2
3         Frontend/Card driver for TwinHan DST Frontend
4         Copyright (C) 2003 Jamie Honan
5         Copyright (C) 2004, 2005 Manu Abraham (manu@kromtek.com)
6
7         This program is free software; you can redistribute it and/or modify
8         it under the terms of the GNU General Public License as published by
9         the Free Software Foundation; either version 2 of the License, or
10         (at your option) any later version.
11
12         This program is distributed in the hope that it will be useful,
13         but WITHOUT ANY WARRANTY; without even the implied warranty of
14         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15         GNU General Public License for more details.
16
17         You should have received a copy of the GNU General Public License
18         along with this program; if not, write to the Free Software
19         Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/string.h>
27 #include <linux/slab.h>
28 #include <linux/vmalloc.h>
29 #include <linux/delay.h>
30 #include <asm/div64.h>
31
32 #include "dvb_frontend.h"
33 #include "dst_priv.h"
34 #include "dst_common.h"
35
36
37 static unsigned int verbose = 1;
38 module_param(verbose, int, 0644);
39 MODULE_PARM_DESC(verbose, "verbose startup messages, default is 1 (yes)");
40
41 static unsigned int debug = 1;
42 module_param(debug, int, 0644);
43 MODULE_PARM_DESC(debug, "debug messages, default is 0 (yes)");
44
45 static unsigned int dst_addons;
46 module_param(dst_addons, int, 0644);
47 MODULE_PARM_DESC(dst_addons, "CA daughterboard, default is 0 (No addons)");
48
49 #define dprintk if (debug) printk
50
51 #define HAS_LOCK        1
52 #define ATTEMPT_TUNE    2
53 #define HAS_POWER       4
54
55 static void dst_packsize(struct dst_state* state, int psize)
56 {
57         union dst_gpio_packet bits;
58
59         bits.psize = psize;
60         bt878_device_control(state->bt, DST_IG_TS, &bits);
61 }
62
63 int dst_gpio_outb(struct dst_state* state, u32 mask, u32 enbb, u32 outhigh, int delay)
64 {
65         union dst_gpio_packet enb;
66         union dst_gpio_packet bits;
67         int err;
68
69         enb.enb.mask = mask;
70         enb.enb.enable = enbb;
71         if (verbose > 4)
72                 dprintk("%s: mask=[%04x], enbb=[%04x], outhigh=[%04x]\n", __FUNCTION__, mask, enbb, outhigh);
73
74         if ((err = bt878_device_control(state->bt, DST_IG_ENABLE, &enb)) < 0) {
75                 dprintk("%s: dst_gpio_enb error (err == %i, mask == %02x, enb == %02x)\n", __FUNCTION__, err, mask, enbb);
76                 return -EREMOTEIO;
77         }
78         udelay(1000);
79         /* because complete disabling means no output, no need to do output packet */
80         if (enbb == 0)
81                 return 0;
82
83         if (delay)
84                 msleep(10);
85
86         bits.outp.mask = enbb;
87         bits.outp.highvals = outhigh;
88
89         if ((err = bt878_device_control(state->bt, DST_IG_WRITE, &bits)) < 0) {
90                 dprintk("%s: dst_gpio_outb error (err == %i, enbb == %02x, outhigh == %02x)\n", __FUNCTION__, err, enbb, outhigh);
91                 return -EREMOTEIO;
92         }
93         return 0;
94 }
95 EXPORT_SYMBOL(dst_gpio_outb);
96
97 int dst_gpio_inb(struct dst_state *state, u8 * result)
98 {
99         union dst_gpio_packet rd_packet;
100         int err;
101
102         *result = 0;
103
104         if ((err = bt878_device_control(state->bt, DST_IG_READ, &rd_packet)) < 0) {
105                 dprintk("%s: dst_gpio_inb error (err == %i)\n", __FUNCTION__, err);
106                 return -EREMOTEIO;
107         }
108
109         *result = (u8) rd_packet.rd.value;
110         return 0;
111 }
112 EXPORT_SYMBOL(dst_gpio_inb);
113
114 int rdc_reset_state(struct dst_state *state)
115 {
116         if (verbose > 1)
117                 dprintk("%s: Resetting state machine\n", __FUNCTION__);
118
119         if (dst_gpio_outb(state, RDC_8820_INT, RDC_8820_INT, 0, NO_DELAY) < 0) {
120                 dprintk("%s: dst_gpio_outb ERROR !\n", __FUNCTION__);
121                 return -1;
122         }
123
124         msleep(10);
125
126         if (dst_gpio_outb(state, RDC_8820_INT, RDC_8820_INT, RDC_8820_INT, NO_DELAY) < 0) {
127                 dprintk("%s: dst_gpio_outb ERROR !\n", __FUNCTION__);
128                 msleep(10);
129                 return -1;
130         }
131
132         return 0;
133 }
134 EXPORT_SYMBOL(rdc_reset_state);
135
136 int rdc_8820_reset(struct dst_state *state)
137 {
138         if (verbose > 1)
139                 dprintk("%s: Resetting DST\n", __FUNCTION__);
140
141         if (dst_gpio_outb(state, RDC_8820_RESET, RDC_8820_RESET, 0, NO_DELAY) < 0) {
142                 dprintk("%s: dst_gpio_outb ERROR !\n", __FUNCTION__);
143                 return -1;
144         }
145         udelay(1000);
146         if (dst_gpio_outb(state, RDC_8820_RESET, RDC_8820_RESET, RDC_8820_RESET, DELAY) < 0) {
147                 dprintk("%s: dst_gpio_outb ERROR !\n", __FUNCTION__);
148                 return -1;
149         }
150
151         return 0;
152 }
153 EXPORT_SYMBOL(rdc_8820_reset);
154
155 int dst_pio_enable(struct dst_state *state)
156 {
157         if (dst_gpio_outb(state, ~0, RDC_8820_PIO_0_ENABLE, 0, NO_DELAY) < 0) {
158                 dprintk("%s: dst_gpio_outb ERROR !\n", __FUNCTION__);
159                 return -1;
160         }
161         udelay(1000);
162         return 0;
163 }
164 EXPORT_SYMBOL(dst_pio_enable);
165
166 int dst_pio_disable(struct dst_state *state)
167 {
168         if (dst_gpio_outb(state, ~0, RDC_8820_PIO_0_DISABLE, RDC_8820_PIO_0_DISABLE, NO_DELAY) < 0) {
169                 dprintk("%s: dst_gpio_outb ERROR !\n", __FUNCTION__);
170                 return -1;
171         }
172         if (state->type_flags & DST_TYPE_HAS_FW_1)
173                 udelay(1000);
174
175         return 0;
176 }
177 EXPORT_SYMBOL(dst_pio_disable);
178
179 int dst_wait_dst_ready(struct dst_state *state, u8 delay_mode)
180 {
181         u8 reply;
182         int i;
183
184         for (i = 0; i < 200; i++) {
185                 if (dst_gpio_inb(state, &reply) < 0) {
186                         dprintk("%s: dst_gpio_inb ERROR !\n", __FUNCTION__);
187                         return -1;
188                 }
189
190                 if ((reply & RDC_8820_PIO_0_ENABLE) == 0) {
191                         if (verbose > 4)
192                                 dprintk("%s: dst wait ready after %d\n", __FUNCTION__, i);
193                         return 1;
194                 }
195                 msleep(10);
196         }
197         if (verbose > 1)
198                 dprintk("%s: dst wait NOT ready after %d\n", __FUNCTION__, i);
199
200         return 0;
201 }
202 EXPORT_SYMBOL(dst_wait_dst_ready);
203
204 int dst_error_recovery(struct dst_state *state)
205 {
206         dprintk("%s: Trying to return from previous errors...\n", __FUNCTION__);
207         dst_pio_disable(state);
208         msleep(10);
209         dst_pio_enable(state);
210         msleep(10);
211
212         return 0;
213 }
214 EXPORT_SYMBOL(dst_error_recovery);
215
216 int dst_error_bailout(struct dst_state *state)
217 {
218         dprintk("%s: Trying to bailout from previous error...\n", __FUNCTION__);
219         rdc_8820_reset(state);
220         dst_pio_disable(state);
221         msleep(10);
222
223         return 0;
224 }
225 EXPORT_SYMBOL(dst_error_bailout);
226
227
228 int dst_comm_init(struct dst_state* state)
229 {
230         if (verbose > 1)
231                 dprintk ("%s: Initializing DST..\n", __FUNCTION__);
232         if ((dst_pio_enable(state)) < 0) {
233                 dprintk("%s: PIO Enable Failed.\n", __FUNCTION__);
234                 return -1;
235         }
236         if ((rdc_reset_state(state)) < 0) {
237                 dprintk("%s: RDC 8820 State RESET Failed.\n", __FUNCTION__);
238                 return -1;
239         }
240         if (state->type_flags & DST_TYPE_HAS_FW_1)
241                 msleep(100);
242         else
243                 msleep(5);
244
245         return 0;
246 }
247 EXPORT_SYMBOL(dst_comm_init);
248
249
250 int write_dst(struct dst_state *state, u8 *data, u8 len)
251 {
252         struct i2c_msg msg = {
253                 .addr = state->config->demod_address,.flags = 0,.buf = data,.len = len
254         };
255
256         int err;
257         int cnt;
258         if (debug && (verbose > 4)) {
259                 u8 i;
260                 if (verbose > 4) {
261                         dprintk("%s writing [ ", __FUNCTION__);
262                         for (i = 0; i < len; i++)
263                                 dprintk("%02x ", data[i]);
264                         dprintk("]\n");
265                 }
266         }
267         for (cnt = 0; cnt < 2; cnt++) {
268                 if ((err = i2c_transfer(state->i2c, &msg, 1)) < 0) {
269                         dprintk("%s: _write_dst error (err == %i, len == 0x%02x, b0 == 0x%02x)\n", __FUNCTION__, err, len, data[0]);
270                         dst_error_recovery(state);
271                         continue;
272                 } else
273                         break;
274         }
275
276         if (cnt >= 2) {
277                 if (verbose > 1)
278                         printk("%s: RDC 8820 RESET...\n", __FUNCTION__);
279                 dst_error_bailout(state);
280
281                 return -1;
282         }
283
284         return 0;
285 }
286 EXPORT_SYMBOL(write_dst);
287
288 int read_dst(struct dst_state *state, u8 * ret, u8 len)
289 {
290         struct i2c_msg msg = {.addr = state->config->demod_address,.flags = I2C_M_RD,.buf = ret,.len = len };
291         int err;
292         int cnt;
293
294         for (cnt = 0; cnt < 2; cnt++) {
295                 if ((err = i2c_transfer(state->i2c, &msg, 1)) < 0) {
296
297                         dprintk("%s: read_dst error (err == %i, len == 0x%02x, b0 == 0x%02x)\n", __FUNCTION__, err, len, ret[0]);
298                         dst_error_recovery(state);
299
300                         continue;
301                 } else
302                         break;
303         }
304         if (cnt >= 2) {
305                 if (verbose > 1)
306                         printk("%s: RDC 8820 RESET...\n", __FUNCTION__);
307                 dst_error_bailout(state);
308
309                 return -1;
310         }
311         if (debug && (verbose > 4)) {
312                 dprintk("%s reply is 0x%x\n", __FUNCTION__, ret[0]);
313                 for (err = 1; err < len; err++)
314                         dprintk(" 0x%x", ret[err]);
315                 if (err > 1)
316                         dprintk("\n");
317         }
318
319         return 0;
320 }
321 EXPORT_SYMBOL(read_dst);
322
323 static int dst_set_polarization(struct dst_state *state)
324 {
325         switch (state->voltage) {
326                 case SEC_VOLTAGE_13:    // vertical
327                         printk("%s: Polarization=[Vertical]\n", __FUNCTION__);
328                         state->tx_tuna[8] &= ~0x40;  //1
329                         break;
330
331                 case SEC_VOLTAGE_18:    // horizontal
332                         printk("%s: Polarization=[Horizontal]\n", __FUNCTION__);
333                         state->tx_tuna[8] |= 0x40;  // 0
334                         break;
335
336                 case SEC_VOLTAGE_OFF:
337
338                         break;
339         }
340
341         return 0;
342 }
343
344 static int dst_set_freq(struct dst_state *state, u32 freq)
345 {
346         state->frequency = freq;
347         if (debug > 4)
348                 dprintk("%s: set Frequency %u\n", __FUNCTION__, freq);
349
350         if (state->dst_type == DST_TYPE_IS_SAT) {
351                 freq = freq / 1000;
352                 if (freq < 950 || freq > 2150)
353                         return -EINVAL;
354
355                 state->tx_tuna[2] = (freq >> 8);
356                 state->tx_tuna[3] = (u8) freq;
357                 state->tx_tuna[4] = 0x01;
358                 state->tx_tuna[8] &= ~0x04;
359                 if (state->type_flags & DST_TYPE_HAS_OBS_REGS) {
360                         if (freq < 1531)
361                                 state->tx_tuna[8] |= 0x04;
362                 }
363
364         } else if (state->dst_type == DST_TYPE_IS_TERR) {
365                 freq = freq / 1000;
366                 if (freq < 137000 || freq > 858000)
367                         return -EINVAL;
368
369                 state->tx_tuna[2] = (freq >> 16) & 0xff;
370                 state->tx_tuna[3] = (freq >> 8) & 0xff;
371                 state->tx_tuna[4] = (u8) freq;
372
373         } else if (state->dst_type == DST_TYPE_IS_CABLE) {
374                 state->tx_tuna[2] = (freq >> 16) & 0xff;
375                 state->tx_tuna[3] = (freq >> 8) & 0xff;
376                 state->tx_tuna[4] = (u8) freq;
377
378         } else
379                 return -EINVAL;
380         return 0;
381 }
382
383 static int dst_set_bandwidth(struct dst_state* state, fe_bandwidth_t bandwidth)
384 {
385         state->bandwidth = bandwidth;
386
387         if (state->dst_type != DST_TYPE_IS_TERR)
388                 return 0;
389
390         switch (bandwidth) {
391                 case BANDWIDTH_6_MHZ:
392                         if (state->dst_hw_cap & DST_TYPE_HAS_CA)
393                                 state->tx_tuna[7] = 0x06;
394                         else {
395                                 state->tx_tuna[6] = 0x06;
396                                 state->tx_tuna[7] = 0x00;
397                         }
398                         break;
399
400                 case BANDWIDTH_7_MHZ:
401                         if (state->dst_hw_cap & DST_TYPE_HAS_CA)
402                                 state->tx_tuna[7] = 0x07;
403                         else {
404                                 state->tx_tuna[6] = 0x07;
405                                 state->tx_tuna[7] = 0x00;
406                         }
407                         break;
408
409                 case BANDWIDTH_8_MHZ:
410                         if (state->dst_hw_cap & DST_TYPE_HAS_CA)
411                                 state->tx_tuna[7] = 0x08;
412                         else {
413                                 state->tx_tuna[6] = 0x08;
414                                 state->tx_tuna[7] = 0x00;
415                         }
416                         break;
417
418                 default:
419                         return -EINVAL;
420         }
421         return 0;
422 }
423
424 static int dst_set_inversion(struct dst_state* state, fe_spectral_inversion_t inversion)
425 {
426         state->inversion = inversion;
427         switch (inversion) {
428                 case INVERSION_OFF:     // Inversion = Normal
429                         state->tx_tuna[8] &= ~0x80;
430                         break;
431
432                 case INVERSION_ON:
433                         state->tx_tuna[8] |= 0x80;
434                         break;
435                 default:
436                         return -EINVAL;
437         }
438         return 0;
439 }
440
441 static int dst_set_fec(struct dst_state* state, fe_code_rate_t fec)
442 {
443         state->fec = fec;
444         return 0;
445 }
446
447 static fe_code_rate_t dst_get_fec(struct dst_state* state)
448 {
449         return state->fec;
450 }
451
452 static int dst_set_symbolrate(struct dst_state* state, u32 srate)
453 {
454         u8 *val;
455         u32 symcalc;
456         u64 sval;
457
458         state->symbol_rate = srate;
459
460         if (state->dst_type == DST_TYPE_IS_TERR) {
461                 return 0;
462         }
463         if (debug > 4)
464                 dprintk("%s: set symrate %u\n", __FUNCTION__, srate);
465         srate /= 1000;
466         val = &state->tx_tuna[0];
467
468         if (state->type_flags & DST_TYPE_HAS_SYMDIV) {
469                 sval = srate;
470                 sval <<= 20;
471                 do_div(sval, 88000);
472                 symcalc = (u32) sval;
473
474                 if (debug > 4)
475                         dprintk("%s: set symcalc %u\n", __FUNCTION__, symcalc);
476
477                 val[5] = (u8) (symcalc >> 12);
478                 val[6] = (u8) (symcalc >> 4);
479                 val[7] = (u8) (symcalc << 4);
480         } else {
481                 val[5] = (u8) (srate >> 16) & 0x7f;
482                 val[6] = (u8) (srate >> 8);
483                 val[7] = (u8) srate;
484         }
485         val[8] &= ~0x20;
486         if (srate > 8000)
487                 val[8] |= 0x20;
488         return 0;
489 }
490
491
492 static int dst_set_modulation(struct dst_state *state, fe_modulation_t modulation)
493 {
494         if (state->dst_type != DST_TYPE_IS_CABLE)
495                 return 0;
496
497         state->modulation = modulation;
498         switch (modulation) {
499                 case QAM_16:
500                         state->tx_tuna[8] = 0x10;
501                         break;
502
503                 case QAM_32:
504                         state->tx_tuna[8] = 0x20;
505                         break;
506
507                 case QAM_64:
508                         state->tx_tuna[8] = 0x40;
509                         break;
510
511                 case QAM_128:
512                         state->tx_tuna[8] = 0x80;
513                         break;
514
515                 case QAM_256:
516                         state->tx_tuna[8] = 0x00;
517                         break;
518
519                 case QPSK:
520                 case QAM_AUTO:
521                 case VSB_8:
522                 case VSB_16:
523                 default:
524                         return -EINVAL;
525
526         }
527
528         return 0;
529 }
530
531 static fe_modulation_t dst_get_modulation(struct dst_state *state)
532 {
533         return state->modulation;
534 }
535
536
537 u8 dst_check_sum(u8 * buf, u32 len)
538 {
539         u32 i;
540         u8 val = 0;
541         if (!len)
542                 return 0;
543         for (i = 0; i < len; i++) {
544                 val += buf[i];
545         }
546         return ((~val) + 1);
547 }
548 EXPORT_SYMBOL(dst_check_sum);
549
550 static void dst_type_flags_print(u32 type_flags)
551 {
552         printk("DST type flags :");
553         if (type_flags & DST_TYPE_HAS_NEWTUNE)
554                 printk(" 0x%x newtuner", DST_TYPE_HAS_NEWTUNE);
555         if (type_flags & DST_TYPE_HAS_TS204)
556                 printk(" 0x%x ts204", DST_TYPE_HAS_TS204);
557         if (type_flags & DST_TYPE_HAS_SYMDIV)
558                 printk(" 0x%x symdiv", DST_TYPE_HAS_SYMDIV);
559         if (type_flags & DST_TYPE_HAS_FW_1)
560                 printk(" 0x%x firmware version = 1", DST_TYPE_HAS_FW_1);
561         if (type_flags & DST_TYPE_HAS_FW_2)
562                 printk(" 0x%x firmware version = 2", DST_TYPE_HAS_FW_2);
563         if (type_flags & DST_TYPE_HAS_FW_3)
564                 printk(" 0x%x firmware version = 3", DST_TYPE_HAS_FW_3);
565 //      if ((type_flags & DST_TYPE_HAS_FW_BUILD) && new_fw)
566
567         printk("\n");
568 }
569
570
571 static int dst_type_print (u8 type)
572 {
573         char *otype;
574         switch (type) {
575         case DST_TYPE_IS_SAT:
576                 otype = "satellite";
577                 break;
578
579         case DST_TYPE_IS_TERR:
580                 otype = "terrestrial";
581                 break;
582
583         case DST_TYPE_IS_CABLE:
584                 otype = "cable";
585                 break;
586
587         default:
588                 printk("%s: invalid dst type %d\n", __FUNCTION__, type);
589                 return -EINVAL;
590         }
591         printk("DST type : %s\n", otype);
592
593         return 0;
594 }
595
596 /*
597         Known cards list
598         Satellite
599         -------------------
600                   200103A
601         VP-1020   DST-MOT       LG(old), TS=188
602
603         VP-1020   DST-03T       LG(new), TS=204
604         VP-1022   DST-03T       LG(new), TS=204
605         VP-1025   DST-03T       LG(new), TS=204
606
607         VP-1030   DSTMCI,       LG(new), TS=188
608         VP-1032   DSTMCI,       LG(new), TS=188
609
610         Cable
611         -------------------
612         VP-2030   DCT-CI,       Samsung, TS=204
613         VP-2021   DCT-CI,       Unknown, TS=204
614         VP-2031   DCT-CI,       Philips, TS=188
615         VP-2040   DCT-CI,       Philips, TS=188, with CA daughter board
616         VP-2040   DCT-CI,       Philips, TS=204, without CA daughter board
617
618         Terrestrial
619         -------------------
620         VP-3050  DTTNXT                  TS=188
621         VP-3040  DTT-CI,        Philips, TS=188
622         VP-3040  DTT-CI,        Philips, TS=204
623
624         ATSC
625         -------------------
626         VP-3220  ATSCDI,                 TS=188
627         VP-3250  ATSCAD,                 TS=188
628
629 */
630
631 struct dst_types dst_tlist[] = {
632         {
633                 .device_id = "200103A",
634                 .offset = 0,
635                 .dst_type =  DST_TYPE_IS_SAT,
636                 .type_flags = DST_TYPE_HAS_SYMDIV | DST_TYPE_HAS_FW_1 | DST_TYPE_HAS_OBS_REGS,
637                 .dst_feature = 0
638         },      /*      obsolete        */
639
640         {
641                 .device_id = "DST-020",
642                 .offset = 0,
643                 .dst_type =  DST_TYPE_IS_SAT,
644                 .type_flags = DST_TYPE_HAS_SYMDIV | DST_TYPE_HAS_FW_1,
645                 .dst_feature = 0
646         },      /*      obsolete        */
647
648         {
649                 .device_id = "DST-030",
650                 .offset =  0,
651                 .dst_type = DST_TYPE_IS_SAT,
652                 .type_flags = DST_TYPE_HAS_TS204 | DST_TYPE_HAS_NEWTUNE | DST_TYPE_HAS_FW_1,
653                 .dst_feature = 0
654         },      /*      obsolete        */
655
656         {
657                 .device_id = "DST-03T",
658                 .offset = 0,
659                 .dst_type = DST_TYPE_IS_SAT,
660                 .type_flags = DST_TYPE_HAS_SYMDIV | DST_TYPE_HAS_TS204 | DST_TYPE_HAS_FW_2,
661                 .dst_feature = DST_TYPE_HAS_DISEQC3 | DST_TYPE_HAS_DISEQC4 | DST_TYPE_HAS_DISEQC5
662                                                          | DST_TYPE_HAS_MAC | DST_TYPE_HAS_MOTO
663          },
664
665         {
666                 .device_id = "DST-MOT",
667                 .offset =  0,
668                 .dst_type = DST_TYPE_IS_SAT,
669                 .type_flags = DST_TYPE_HAS_SYMDIV | DST_TYPE_HAS_FW_1,
670                 .dst_feature = 0
671         },      /*      obsolete        */
672
673         {
674                 .device_id = "DST-CI",
675                 .offset = 1,
676                 .dst_type = DST_TYPE_IS_SAT,
677                 .type_flags = DST_TYPE_HAS_TS204 | DST_TYPE_HAS_NEWTUNE | DST_TYPE_HAS_FW_1,
678                 .dst_feature = DST_TYPE_HAS_CA
679         },      /*      An OEM board    */
680
681         {
682                 .device_id = "DSTMCI",
683                 .offset = 1,
684                 .dst_type = DST_TYPE_IS_SAT,
685                 .type_flags = DST_TYPE_HAS_NEWTUNE | DST_TYPE_HAS_FW_2 | DST_TYPE_HAS_FW_BUILD | DST_TYPE_HAS_INC_COUNT,
686                 .dst_feature = DST_TYPE_HAS_CA | DST_TYPE_HAS_DISEQC3 | DST_TYPE_HAS_DISEQC4
687                                                         | DST_TYPE_HAS_MOTO | DST_TYPE_HAS_MAC
688         },
689
690         {
691                 .device_id = "DSTFCI",
692                 .offset = 1,
693                 .dst_type = DST_TYPE_IS_SAT,
694                 .type_flags = DST_TYPE_HAS_NEWTUNE | DST_TYPE_HAS_FW_1,
695                 .dst_feature = 0
696         },      /* unknown to vendor    */
697
698         {
699                 .device_id = "DCT-CI",
700                 .offset = 1,
701                 .dst_type = DST_TYPE_IS_CABLE,
702                 .type_flags = DST_TYPE_HAS_TS204 | DST_TYPE_HAS_NEWTUNE | DST_TYPE_HAS_FW_1
703                                                         | DST_TYPE_HAS_FW_2 | DST_TYPE_HAS_FW_BUILD,
704                 .dst_feature = DST_TYPE_HAS_CA
705         },
706
707         {
708                 .device_id = "DCTNEW",
709                 .offset = 1,
710                 .dst_type = DST_TYPE_IS_CABLE,
711                 .type_flags = DST_TYPE_HAS_NEWTUNE | DST_TYPE_HAS_FW_3,
712                 .dst_feature = 0
713         },
714
715         {
716                 .device_id = "DTT-CI",
717                 .offset = 1,
718                 .dst_type = DST_TYPE_IS_TERR,
719                 .type_flags = DST_TYPE_HAS_TS204 | DST_TYPE_HAS_FW_2 | DST_TYPE_HAS_FW_BUILD,
720                 .dst_feature = 0
721         },
722
723         {
724                 .device_id = "DTTDIG",
725                 .offset = 1,
726                 .dst_type = DST_TYPE_IS_TERR,
727                 .type_flags = DST_TYPE_HAS_FW_2,
728                 .dst_feature = 0
729         },
730
731         {
732                 .device_id = "DTTNXT",
733                 .offset = 1,
734                 .dst_type = DST_TYPE_IS_TERR,
735                 .type_flags = DST_TYPE_HAS_FW_2,
736                 .dst_feature = DST_TYPE_HAS_ANALOG
737         },
738
739         {
740                 .device_id = "ATSCDI",
741                 .offset = 1,
742                 .dst_type = DST_TYPE_IS_ATSC,
743                 .type_flags = DST_TYPE_HAS_FW_2,
744                 .dst_feature = 0
745         },
746
747         {
748                 .device_id = "ATSCAD",
749                 .offset = 1,
750                 .dst_type = DST_TYPE_IS_ATSC,
751                 .type_flags = DST_TYPE_HAS_FW_2,
752                 .dst_feature = 0
753         },
754
755         { }
756
757 };
758
759
760 static int dst_get_device_id(struct dst_state *state)
761 {
762         u8 reply;
763
764         int i;
765         struct dst_types *p_dst_type;
766         u8 use_dst_type = 0;
767         u32 use_type_flags = 0;
768
769         static u8 device_type[8] = {0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff};
770
771         device_type[7] = dst_check_sum(device_type, 7);
772
773         if (write_dst(state, device_type, FIXED_COMM))
774                 return -1;              /*      Write failed            */
775
776         if ((dst_pio_disable(state)) < 0)
777                 return -1;
778
779         if (read_dst(state, &reply, GET_ACK))
780                 return -1;              /*      Read failure            */
781
782         if (reply != ACK) {
783                 dprintk("%s: Write not Acknowledged! [Reply=0x%02x]\n", __FUNCTION__, reply);
784                 return -1;              /*      Unack'd write           */
785         }
786
787         if (!dst_wait_dst_ready(state, DEVICE_INIT))
788                 return -1;              /*      DST not ready yet       */
789
790         if (read_dst(state, state->rxbuffer, FIXED_COMM))
791                 return -1;
792
793         dst_pio_disable(state);
794
795         if (state->rxbuffer[7] != dst_check_sum(state->rxbuffer, 7)) {
796                 dprintk("%s: Checksum failure! \n", __FUNCTION__);
797                 return -1;              /*      Checksum failure        */
798         }
799
800         state->rxbuffer[7] = '\0';
801
802         for (i = 0, p_dst_type = dst_tlist; i < ARRAY_SIZE (dst_tlist); i++, p_dst_type++) {
803                 if (!strncmp (&state->rxbuffer[p_dst_type->offset], p_dst_type->device_id, strlen (p_dst_type->device_id))) {
804                         use_type_flags = p_dst_type->type_flags;
805                         use_dst_type = p_dst_type->dst_type;
806
807                         /*      Card capabilities       */
808                         state->dst_hw_cap = p_dst_type->dst_feature;
809                         printk ("%s: Recognise [%s]\n", __FUNCTION__, p_dst_type->device_id);
810
811                         break;
812                 }
813         }
814
815         if (i >= sizeof (dst_tlist) / sizeof (dst_tlist [0])) {
816                 printk("%s: Unable to recognize %s or %s\n", __FUNCTION__, &state->rxbuffer[0], &state->rxbuffer[1]);
817                 printk("%s: please email linux-dvb@linuxtv.org with this type in\n", __FUNCTION__);
818                 use_dst_type = DST_TYPE_IS_SAT;
819                 use_type_flags = DST_TYPE_HAS_SYMDIV;
820         }
821
822         dst_type_print(use_dst_type);
823         state->type_flags = use_type_flags;
824         state->dst_type = use_dst_type;
825         dst_type_flags_print(state->type_flags);
826
827         if (state->type_flags & DST_TYPE_HAS_TS204) {
828                 dst_packsize(state, 204);
829         }
830
831         return 0;
832 }
833
834 static int dst_probe(struct dst_state *state)
835 {
836         if ((rdc_8820_reset(state)) < 0) {
837                 dprintk("%s: RDC 8820 RESET Failed.\n", __FUNCTION__);
838                 return -1;
839         }
840         if (dst_addons & DST_TYPE_HAS_CA)
841                 msleep(4000);
842         else
843                 msleep(100);
844
845         if ((dst_comm_init(state)) < 0) {
846                 dprintk("%s: DST Initialization Failed.\n", __FUNCTION__);
847                 return -1;
848         }
849         msleep(100);
850         if (dst_get_device_id(state) < 0) {
851                 dprintk("%s: unknown device.\n", __FUNCTION__);
852                 return -1;
853         }
854
855         return 0;
856 }
857
858 int dst_command(struct dst_state* state, u8 * data, u8 len)
859 {
860         u8 reply;
861         if ((dst_comm_init(state)) < 0) {
862                 dprintk("%s: DST Communication Initialization Failed.\n", __FUNCTION__);
863                 return -1;
864         }
865
866         if (write_dst(state, data, len)) {
867                 if (verbose > 1)
868                         dprintk("%s: Tring to recover.. \n", __FUNCTION__);
869                 if ((dst_error_recovery(state)) < 0) {
870                         dprintk("%s: Recovery Failed.\n", __FUNCTION__);
871                         return -1;
872                 }
873                 return -1;
874         }
875         if ((dst_pio_disable(state)) < 0) {
876                 dprintk("%s: PIO Disable Failed.\n", __FUNCTION__);
877                 return -1;
878         }
879         if (state->type_flags & DST_TYPE_HAS_FW_1)
880                 udelay(3000);
881
882         if (read_dst(state, &reply, GET_ACK)) {
883                 if (verbose > 1)
884                         dprintk("%s: Trying to recover.. \n", __FUNCTION__);
885                 if ((dst_error_recovery(state)) < 0) {
886                         dprintk("%s: Recovery Failed.\n", __FUNCTION__);
887                         return -1;
888                 }
889                 return -1;
890         }
891
892         if (reply != ACK) {
893                 dprintk("%s: write not acknowledged 0x%02x \n", __FUNCTION__, reply);
894                 return -1;
895         }
896         if (len >= 2 && data[0] == 0 && (data[1] == 1 || data[1] == 3))
897                 return 0;
898
899 //      udelay(3000);
900         if (state->type_flags & DST_TYPE_HAS_FW_1)
901                 udelay(3000);
902         else
903                 udelay(2000);
904
905         if (!dst_wait_dst_ready(state, NO_DELAY))
906                 return -1;
907
908         if (read_dst(state, state->rxbuffer, FIXED_COMM)) {
909                 if (verbose > 1)
910                         dprintk("%s: Trying to recover.. \n", __FUNCTION__);
911                 if ((dst_error_recovery(state)) < 0) {
912                         dprintk("%s: Recovery failed.\n", __FUNCTION__);
913                         return -1;
914                 }
915                 return -1;
916         }
917
918         if (state->rxbuffer[7] != dst_check_sum(state->rxbuffer, 7)) {
919                 dprintk("%s: checksum failure\n", __FUNCTION__);
920                 return -1;
921         }
922
923         return 0;
924 }
925 EXPORT_SYMBOL(dst_command);
926
927 static int dst_get_signal(struct dst_state* state)
928 {
929         int retval;
930         u8 get_signal[] = { 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfb };
931         dprintk("%s: Getting Signal strength and other parameters\n", __FUNCTION__);
932         if ((state->diseq_flags & ATTEMPT_TUNE) == 0) {
933                 state->decode_lock = state->decode_strength = state->decode_snr = 0;
934                 return 0;
935         }
936         if (0 == (state->diseq_flags & HAS_LOCK)) {
937                 state->decode_lock = state->decode_strength = state->decode_snr = 0;
938                 return 0;
939         }
940         if (time_after_eq(jiffies, state->cur_jiff + (HZ / 5))) {
941                 retval = dst_command(state, get_signal, 8);
942                 if (retval < 0)
943                         return retval;
944                 if (state->dst_type == DST_TYPE_IS_SAT) {
945                         state->decode_lock = ((state->rxbuffer[6] & 0x10) == 0) ? 1 : 0;
946                         state->decode_strength = state->rxbuffer[5] << 8;
947                         state->decode_snr = state->rxbuffer[2] << 8 | state->rxbuffer[3];
948                 } else if ((state->dst_type == DST_TYPE_IS_TERR) || (state->dst_type == DST_TYPE_IS_CABLE)) {
949                         state->decode_lock = (state->rxbuffer[1]) ? 1 : 0;
950                         state->decode_strength = state->rxbuffer[4] << 8;
951                         state->decode_snr = state->rxbuffer[3] << 8;
952                 }
953                 state->cur_jiff = jiffies;
954         }
955         return 0;
956 }
957
958 static int dst_tone_power_cmd(struct dst_state* state)
959 {
960         u8 paket[8] = { 0x00, 0x09, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00 };
961
962         if (state->dst_type == DST_TYPE_IS_TERR)
963                 return 0;
964
965         paket[4] = state->tx_tuna[4];
966         paket[2] = state->tx_tuna[2];
967         paket[3] = state->tx_tuna[3];
968         paket[7] = dst_check_sum (paket, 7);
969         dst_command(state, paket, 8);
970
971         return 0;
972 }
973
974 static int dst_get_tuna(struct dst_state* state)
975 {
976         int retval;
977
978         if ((state->diseq_flags & ATTEMPT_TUNE) == 0)
979                 return 0;
980
981         state->diseq_flags &= ~(HAS_LOCK);
982         if (!dst_wait_dst_ready(state, NO_DELAY))
983                 return 0;
984
985         if (state->type_flags & DST_TYPE_HAS_NEWTUNE) {
986                 /* how to get variable length reply ???? */
987                 retval = read_dst(state, state->rx_tuna, 10);
988         } else {
989                 retval = read_dst(state, &state->rx_tuna[2], FIXED_COMM);
990         }
991
992         if (retval < 0) {
993                 dprintk("%s: read not successful\n", __FUNCTION__);
994                 return 0;
995         }
996
997         if (state->type_flags & DST_TYPE_HAS_NEWTUNE) {
998                 if (state->rx_tuna[9] != dst_check_sum(&state->rx_tuna[0], 9)) {
999                         dprintk("%s: checksum failure?\n", __FUNCTION__);
1000                         return 0;
1001                 }
1002         } else {
1003                 if (state->rx_tuna[9] != dst_check_sum(&state->rx_tuna[2], 7)) {
1004                         dprintk("%s: checksum failure?\n", __FUNCTION__);
1005                         return 0;
1006                 }
1007         }
1008         if (state->rx_tuna[2] == 0 && state->rx_tuna[3] == 0)
1009                 return 0;
1010         state->decode_freq = ((state->rx_tuna[2] & 0x7f) << 8) + state->rx_tuna[3];
1011
1012         state->decode_lock = 1;
1013         state->diseq_flags |= HAS_LOCK;
1014
1015         return 1;
1016 }
1017
1018 static int dst_set_voltage(struct dvb_frontend* fe, fe_sec_voltage_t voltage);
1019
1020 static int dst_write_tuna(struct dvb_frontend* fe)
1021 {
1022         struct dst_state* state = fe->demodulator_priv;
1023         int retval;
1024         u8 reply;
1025
1026         if (debug > 4)
1027                 dprintk("%s: type_flags 0x%x \n", __FUNCTION__, state->type_flags);
1028
1029         state->decode_freq = 0;
1030         state->decode_lock = state->decode_strength = state->decode_snr = 0;
1031         if (state->dst_type == DST_TYPE_IS_SAT) {
1032                 if (!(state->diseq_flags & HAS_POWER))
1033                         dst_set_voltage(fe, SEC_VOLTAGE_13);
1034         }
1035         state->diseq_flags &= ~(HAS_LOCK | ATTEMPT_TUNE);
1036
1037         if ((dst_comm_init(state)) < 0) {
1038                 dprintk("%s: DST Communication initialization failed.\n", __FUNCTION__);
1039                 return -1;
1040         }
1041
1042         if (state->type_flags & DST_TYPE_HAS_NEWTUNE) {
1043                 state->tx_tuna[9] = dst_check_sum(&state->tx_tuna[0], 9);
1044                 retval = write_dst(state, &state->tx_tuna[0], 10);
1045
1046         } else {
1047                 state->tx_tuna[9] = dst_check_sum(&state->tx_tuna[2], 7);
1048                 retval = write_dst(state, &state->tx_tuna[2], FIXED_COMM);
1049         }
1050         if (retval < 0) {
1051                 dst_pio_disable(state);
1052                 dprintk("%s: write not successful\n", __FUNCTION__);
1053                 return retval;
1054         }
1055
1056         if ((dst_pio_disable(state)) < 0) {
1057                 dprintk("%s: DST PIO disable failed !\n", __FUNCTION__);
1058                 return -1;
1059         }
1060
1061         if ((read_dst(state, &reply, GET_ACK) < 0)) {
1062                 dprintk("%s: read verify not successful.\n", __FUNCTION__);
1063                 return -1;
1064         }
1065         if (reply != ACK) {
1066                 dprintk("%s: write not acknowledged 0x%02x \n", __FUNCTION__, reply);
1067                 return 0;
1068         }
1069         state->diseq_flags |= ATTEMPT_TUNE;
1070
1071         return dst_get_tuna(state);
1072 }
1073
1074 /*
1075  * line22k0    0x00, 0x09, 0x00, 0xff, 0x01, 0x00, 0x00, 0x00
1076  * line22k1    0x00, 0x09, 0x01, 0xff, 0x01, 0x00, 0x00, 0x00
1077  * line22k2    0x00, 0x09, 0x02, 0xff, 0x01, 0x00, 0x00, 0x00
1078  * tone        0x00, 0x09, 0xff, 0x00, 0x01, 0x00, 0x00, 0x00
1079  * data        0x00, 0x09, 0xff, 0x01, 0x01, 0x00, 0x00, 0x00
1080  * power_off   0x00, 0x09, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00
1081  * power_on    0x00, 0x09, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00
1082  * Diseqc 1    0x00, 0x08, 0x04, 0xe0, 0x10, 0x38, 0xf0, 0xec
1083  * Diseqc 2    0x00, 0x08, 0x04, 0xe0, 0x10, 0x38, 0xf4, 0xe8
1084  * Diseqc 3    0x00, 0x08, 0x04, 0xe0, 0x10, 0x38, 0xf8, 0xe4
1085  * Diseqc 4    0x00, 0x08, 0x04, 0xe0, 0x10, 0x38, 0xfc, 0xe0
1086  */
1087
1088 static int dst_set_diseqc(struct dvb_frontend* fe, struct dvb_diseqc_master_cmd* cmd)
1089 {
1090         struct dst_state* state = fe->demodulator_priv;
1091         u8 paket[8] = { 0x00, 0x08, 0x04, 0xe0, 0x10, 0x38, 0xf0, 0xec };
1092
1093         if (state->dst_type != DST_TYPE_IS_SAT)
1094                 return 0;
1095
1096         if (cmd->msg_len == 0 || cmd->msg_len > 4)
1097                 return -EINVAL;
1098         memcpy(&paket[3], cmd->msg, cmd->msg_len);
1099         paket[7] = dst_check_sum(&paket[0], 7);
1100         dst_command(state, paket, 8);
1101         return 0;
1102 }
1103
1104 static int dst_set_voltage(struct dvb_frontend* fe, fe_sec_voltage_t voltage)
1105 {
1106         int need_cmd;
1107         struct dst_state* state = fe->demodulator_priv;
1108
1109         state->voltage = voltage;
1110
1111         if (state->dst_type != DST_TYPE_IS_SAT)
1112                 return 0;
1113
1114         need_cmd = 0;
1115         switch (voltage) {
1116                 case SEC_VOLTAGE_13:
1117                 case SEC_VOLTAGE_18:
1118                         if ((state->diseq_flags & HAS_POWER) == 0)
1119                                 need_cmd = 1;
1120                         state->diseq_flags |= HAS_POWER;
1121                         state->tx_tuna[4] = 0x01;
1122                         break;
1123
1124                 case SEC_VOLTAGE_OFF:
1125                         need_cmd = 1;
1126                         state->diseq_flags &= ~(HAS_POWER | HAS_LOCK | ATTEMPT_TUNE);
1127                         state->tx_tuna[4] = 0x00;
1128                         break;
1129
1130                 default:
1131                         return -EINVAL;
1132         }
1133         if (need_cmd)
1134                 dst_tone_power_cmd(state);
1135
1136         return 0;
1137 }
1138
1139 static int dst_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
1140 {
1141         struct dst_state* state = fe->demodulator_priv;
1142
1143         state->tone = tone;
1144
1145         if (state->dst_type != DST_TYPE_IS_SAT)
1146                 return 0;
1147
1148         switch (tone) {
1149                 case SEC_TONE_OFF:
1150                         if (state->type_flags & DST_TYPE_HAS_OBS_REGS)
1151                             state->tx_tuna[2] = 0x00;
1152                         else
1153                             state->tx_tuna[2] = 0xff;
1154
1155                         break;
1156
1157                 case SEC_TONE_ON:
1158                         state->tx_tuna[2] = 0x02;
1159                         break;
1160
1161                 default:
1162                         return -EINVAL;
1163         }
1164         dst_tone_power_cmd(state);
1165
1166         return 0;
1167 }
1168
1169 static int dst_send_burst(struct dvb_frontend *fe, fe_sec_mini_cmd_t minicmd)
1170 {
1171         struct dst_state *state = fe->demodulator_priv;
1172
1173         if (state->dst_type != DST_TYPE_IS_SAT)
1174                 return 0;
1175
1176         state->minicmd = minicmd;
1177
1178         switch (minicmd) {
1179                 case SEC_MINI_A:
1180                         state->tx_tuna[3] = 0x02;
1181                         break;
1182                 case SEC_MINI_B:
1183                         state->tx_tuna[3] = 0xff;
1184                         break;
1185         }
1186         dst_tone_power_cmd(state);
1187
1188         return 0;
1189 }
1190
1191
1192 static int dst_init(struct dvb_frontend* fe)
1193 {
1194         struct dst_state* state = fe->demodulator_priv;
1195         static u8 ini_satci_tuna[] = { 9, 0, 3, 0xb6, 1, 0, 0x73, 0x21, 0, 0 };
1196         static u8 ini_satfta_tuna[] = { 0, 0, 3, 0xb6, 1, 0x55, 0xbd, 0x50, 0, 0 };
1197         static u8 ini_tvfta_tuna[] = { 0, 0, 3, 0xb6, 1, 7, 0x0, 0x0, 0, 0 };
1198         static u8 ini_tvci_tuna[] = { 9, 0, 3, 0xb6, 1, 7, 0x0, 0x0, 0, 0 };
1199         static u8 ini_cabfta_tuna[] = { 0, 0, 3, 0xb6, 1, 7, 0x0, 0x0, 0, 0 };
1200         static u8 ini_cabci_tuna[] = { 9, 0, 3, 0xb6, 1, 7, 0x0, 0x0, 0, 0 };
1201 //      state->inversion = INVERSION_ON;
1202         state->inversion = INVERSION_OFF;
1203         state->voltage = SEC_VOLTAGE_13;
1204         state->tone = SEC_TONE_OFF;
1205         state->symbol_rate = 29473000;
1206         state->fec = FEC_AUTO;
1207         state->diseq_flags = 0;
1208         state->k22 = 0x02;
1209         state->bandwidth = BANDWIDTH_7_MHZ;
1210         state->cur_jiff = jiffies;
1211         if (state->dst_type == DST_TYPE_IS_SAT) {
1212                 state->frequency = 950000;
1213                 memcpy(state->tx_tuna, ((state->type_flags & DST_TYPE_HAS_NEWTUNE) ? ini_satci_tuna : ini_satfta_tuna), sizeof(ini_satfta_tuna));
1214         } else if (state->dst_type == DST_TYPE_IS_TERR) {
1215                 state->frequency = 137000000;
1216                 memcpy(state->tx_tuna, ((state->type_flags & DST_TYPE_HAS_NEWTUNE) ? ini_tvci_tuna : ini_tvfta_tuna), sizeof(ini_tvfta_tuna));
1217         } else if (state->dst_type == DST_TYPE_IS_CABLE) {
1218                 state->frequency = 51000000;
1219                 memcpy(state->tx_tuna, ((state->type_flags & DST_TYPE_HAS_NEWTUNE) ? ini_cabci_tuna : ini_cabfta_tuna), sizeof(ini_cabfta_tuna));
1220         }
1221
1222         return 0;
1223 }
1224
1225 static int dst_read_status(struct dvb_frontend* fe, fe_status_t* status)
1226 {
1227         struct dst_state* state = fe->demodulator_priv;
1228
1229         *status = 0;
1230         if (state->diseq_flags & HAS_LOCK) {
1231 //              dst_get_signal(state);  // don't require(?) to ask MCU
1232                 if (state->decode_lock)
1233                         *status |= FE_HAS_LOCK | FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_SYNC | FE_HAS_VITERBI;
1234         }
1235
1236         return 0;
1237 }
1238
1239 static int dst_read_signal_strength(struct dvb_frontend* fe, u16* strength)
1240 {
1241         struct dst_state* state = fe->demodulator_priv;
1242
1243         dst_get_signal(state);
1244         *strength = state->decode_strength;
1245
1246         return 0;
1247 }
1248
1249 static int dst_read_snr(struct dvb_frontend* fe, u16* snr)
1250 {
1251         struct dst_state* state = fe->demodulator_priv;
1252
1253         dst_get_signal(state);
1254         *snr = state->decode_snr;
1255
1256         return 0;
1257 }
1258
1259 static int dst_set_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
1260 {
1261         struct dst_state* state = fe->demodulator_priv;
1262
1263         dst_set_freq(state, p->frequency);
1264         if (verbose > 4)
1265                 dprintk("Set Frequency=[%d]\n", p->frequency);
1266
1267 //      dst_set_inversion(state, p->inversion);
1268         if (state->dst_type == DST_TYPE_IS_SAT) {
1269                 if (state->type_flags & DST_TYPE_HAS_OBS_REGS)
1270                         dst_set_inversion(state, p->inversion);
1271
1272                 dst_set_fec(state, p->u.qpsk.fec_inner);
1273                 dst_set_symbolrate(state, p->u.qpsk.symbol_rate);
1274                 dst_set_polarization(state);
1275                 if (verbose > 4)
1276                         dprintk("Set Symbolrate=[%d]\n", p->u.qpsk.symbol_rate);
1277
1278         } else if (state->dst_type == DST_TYPE_IS_TERR) {
1279                 dst_set_bandwidth(state, p->u.ofdm.bandwidth);
1280         } else if (state->dst_type == DST_TYPE_IS_CABLE) {
1281                 dst_set_fec(state, p->u.qam.fec_inner);
1282                 dst_set_symbolrate(state, p->u.qam.symbol_rate);
1283                 dst_set_modulation(state, p->u.qam.modulation);
1284         }
1285         dst_write_tuna(fe);
1286
1287         return 0;
1288 }
1289
1290 static int dst_get_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
1291 {
1292         struct dst_state* state = fe->demodulator_priv;
1293
1294         p->frequency = state->decode_freq;
1295 //      p->inversion = state->inversion;
1296         if (state->dst_type == DST_TYPE_IS_SAT) {
1297                 if (state->type_flags & DST_TYPE_HAS_OBS_REGS)
1298                         p->inversion = state->inversion;
1299
1300                 p->u.qpsk.symbol_rate = state->symbol_rate;
1301                 p->u.qpsk.fec_inner = dst_get_fec(state);
1302         } else if (state->dst_type == DST_TYPE_IS_TERR) {
1303                 p->u.ofdm.bandwidth = state->bandwidth;
1304         } else if (state->dst_type == DST_TYPE_IS_CABLE) {
1305                 p->u.qam.symbol_rate = state->symbol_rate;
1306                 p->u.qam.fec_inner = dst_get_fec(state);
1307 //              p->u.qam.modulation = QAM_AUTO;
1308                 p->u.qam.modulation = dst_get_modulation(state);
1309         }
1310
1311         return 0;
1312 }
1313
1314 static void dst_release(struct dvb_frontend* fe)
1315 {
1316         struct dst_state* state = fe->demodulator_priv;
1317         kfree(state);
1318 }
1319
1320 static struct dvb_frontend_ops dst_dvbt_ops;
1321 static struct dvb_frontend_ops dst_dvbs_ops;
1322 static struct dvb_frontend_ops dst_dvbc_ops;
1323
1324 struct dst_state* dst_attach(struct dst_state *state, struct dvb_adapter *dvb_adapter)
1325 {
1326
1327         /* check if the ASIC is there */
1328         if (dst_probe(state) < 0) {
1329                 if (state)
1330                         kfree(state);
1331
1332                 return NULL;
1333         }
1334         /* determine settings based on type */
1335         switch (state->dst_type) {
1336         case DST_TYPE_IS_TERR:
1337                 memcpy(&state->ops, &dst_dvbt_ops, sizeof(struct dvb_frontend_ops));
1338                 break;
1339
1340         case DST_TYPE_IS_CABLE:
1341                 memcpy(&state->ops, &dst_dvbc_ops, sizeof(struct dvb_frontend_ops));
1342                 break;
1343
1344         case DST_TYPE_IS_SAT:
1345                 memcpy(&state->ops, &dst_dvbs_ops, sizeof(struct dvb_frontend_ops));
1346                 break;
1347
1348         default:
1349                 printk("%s: unknown DST type. please report to the LinuxTV.org DVB mailinglist.\n", __FUNCTION__);
1350                 if (state)
1351                         kfree(state);
1352
1353                 return NULL;
1354         }
1355
1356         /* create dvb_frontend */
1357         state->frontend.ops = &state->ops;
1358         state->frontend.demodulator_priv = state;
1359
1360         return state;                           /*      Manu (DST is a card not a frontend)     */
1361 }
1362
1363 EXPORT_SYMBOL(dst_attach);
1364
1365 static struct dvb_frontend_ops dst_dvbt_ops = {
1366
1367         .info = {
1368                 .name = "DST DVB-T",
1369                 .type = FE_OFDM,
1370                 .frequency_min = 137000000,
1371                 .frequency_max = 858000000,
1372                 .frequency_stepsize = 166667,
1373                 .caps = FE_CAN_FEC_AUTO | FE_CAN_QAM_AUTO | FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO
1374         },
1375
1376         .release = dst_release,
1377
1378         .init = dst_init,
1379
1380         .set_frontend = dst_set_frontend,
1381         .get_frontend = dst_get_frontend,
1382
1383         .read_status = dst_read_status,
1384         .read_signal_strength = dst_read_signal_strength,
1385         .read_snr = dst_read_snr,
1386 };
1387
1388 static struct dvb_frontend_ops dst_dvbs_ops = {
1389
1390         .info = {
1391                 .name = "DST DVB-S",
1392                 .type = FE_QPSK,
1393                 .frequency_min = 950000,
1394                 .frequency_max = 2150000,
1395                 .frequency_stepsize = 1000,     /* kHz for QPSK frontends */
1396                 .frequency_tolerance = 29500,
1397                 .symbol_rate_min = 1000000,
1398                 .symbol_rate_max = 45000000,
1399         /*     . symbol_rate_tolerance  =       ???,*/
1400                 .caps = FE_CAN_FEC_AUTO | FE_CAN_QPSK
1401         },
1402
1403         .release = dst_release,
1404
1405         .init = dst_init,
1406
1407         .set_frontend = dst_set_frontend,
1408         .get_frontend = dst_get_frontend,
1409
1410         .read_status = dst_read_status,
1411         .read_signal_strength = dst_read_signal_strength,
1412         .read_snr = dst_read_snr,
1413
1414         .diseqc_send_burst = dst_send_burst,
1415         .diseqc_send_master_cmd = dst_set_diseqc,
1416         .set_voltage = dst_set_voltage,
1417         .set_tone = dst_set_tone,
1418 };
1419
1420 static struct dvb_frontend_ops dst_dvbc_ops = {
1421
1422         .info = {
1423                 .name = "DST DVB-C",
1424                 .type = FE_QAM,
1425                 .frequency_stepsize = 62500,
1426                 .frequency_min = 51000000,
1427                 .frequency_max = 858000000,
1428                 .symbol_rate_min = 1000000,
1429                 .symbol_rate_max = 45000000,
1430         /*     . symbol_rate_tolerance  =       ???,*/
1431                 .caps = FE_CAN_FEC_AUTO | FE_CAN_QAM_AUTO
1432         },
1433
1434         .release = dst_release,
1435
1436         .init = dst_init,
1437
1438         .set_frontend = dst_set_frontend,
1439         .get_frontend = dst_get_frontend,
1440
1441         .read_status = dst_read_status,
1442         .read_signal_strength = dst_read_signal_strength,
1443         .read_snr = dst_read_snr,
1444 };
1445
1446
1447 MODULE_DESCRIPTION("DST DVB-S/T/C Combo Frontend driver");
1448 MODULE_AUTHOR("Jamie Honan, Manu Abraham");
1449 MODULE_LICENSE("GPL");