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