~ubuntu-branches/ubuntu/quantal/zaptel/quantal

« back to all changes in this revision

Viewing changes to kernel/xpp/calibrate_slics

  • Committer: Bazaar Package Importer
  • Author(s): Tzafrir Cohen
  • Date: 2008-08-28 22:58:23 UTC
  • mfrom: (11.1.11 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080828225823-r8bdunirm8hmc76m
Tags: 1:1.4.11~dfsg-2
* Patch xpp_fxs_power: Fixed an issue with hook detection of the Astribank
  FXS module.
* Don't fail init.d script if fxotune fails. This may happen if running it
  when Asterisk is already running.
* Bump standards version to 3.8.0.0 .
* Ignore false lintian warning ("m-a a-i" has "a a").
* Patch xpp_fxo_cid_always: do always pass PCM if that's what the user
  asked.
* Patch vzaphfc_proc_root_dir: fix vzaphfc on 2.6.26.
* Patch wcte12xp_flags: Proper time for irq save flags.
* Patch headers_2627: Fix location of semaphore.h for 2.6.27 .
* Patch xpp_fxs_dtmf_leak: Don't play DTMFs to the wrong channel.
* Patch wctdm_fix_alarm: Fix sending channel alarms.
* Patch device_class_2626: Fix building 2.6.26 (Closes: #493397).
* Using dh_lintian for lintian overrides, hence requiring debhelper 6.0.7.
* Lintian: we know we have direct changes. Too bad we're half-upstream :-(
* Fix doc-base section names. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl -w
 
2
 
 
3
#
 
4
# $Id: calibrate_slics 3957 2008-03-07 00:45:53Z tzafrir $
 
5
#
 
6
 
 
7
use strict;
 
8
 
 
9
my $SlicsFile = "$ENV{XPP_BASE}/$ENV{XPD_BUS}/$ENV{XPD_NAME}/slics";
 
10
 
 
11
my @SlicNums = (0 .. 7);
 
12
 
 
13
if ( ! -f $SlicsFile ) {
 
14
        exit 1
 
15
}
 
16
 
 
17
my $debug = 0;
 
18
# set DEBUG_CALIBRATION in /etc/default/zaptel or similar
 
19
if (exists $ENV{DEBUG_CALIBRATION}) {
 
20
        $debug = 1;
 
21
}
 
22
 
 
23
sub mysleep($) {
 
24
        my $timeout = shift;
 
25
        select(undef,undef,undef,$timeout);
 
26
}
 
27
 
 
28
sub logger($) {
 
29
        print STDERR "LOG: @_\n";
 
30
        system("logger @_");
 
31
}
 
32
 
 
33
sub debug($) {
 
34
        logger(@_) if ($debug);
 
35
}
 
36
 
 
37
sub write_to_slic_file($) {
 
38
        my $write_str = shift;
 
39
 
 
40
        open(SLICS,">$SlicsFile") or 
 
41
                die("Failed writing to slics file $SlicsFile");
 
42
        print SLICS $write_str;
 
43
        close(SLICS);
 
44
        mysleep(0.001);
 
45
        
 
46
}
 
47
 
 
48
sub read_reg($$$) {
 
49
        my $read_slic = shift;
 
50
        my $read_reg = shift;
 
51
        my $direct = shift;
 
52
        
 
53
        write_to_slic_file(
 
54
                sprintf("%d R%s %02X", $read_slic, $direct, $read_reg));
 
55
        mysleep(0.001);
 
56
        open(SLICS,$SlicsFile) or 
 
57
                die("Failed reading from slics file $SlicsFile");
 
58
        #awk '/^SLIC_REPLY:/{print $5}' $SLICS | cut -dx -f2
 
59
        my @reply = ();
 
60
        while(<SLICS>){
 
61
                #if (/^ /) {
 
62
                #       debug "answer line: $_";
 
63
                #}
 
64
                if (/^ \d*\s+[RW][DIS]\s+[[:xdigit:]]+\s+([[:xdigit:]]+)\s+([[:xdigit:]]+)/){
 
65
                        @reply = (hex($1), hex($2)); 
 
66
                        #debug "got [$reply]\n";
 
67
                        last;
 
68
                }
 
69
        }
 
70
        close(SLICS);
 
71
        if ($direct eq 'I') {
 
72
                return @reply;
 
73
        } else {
 
74
                return $reply[0];
 
75
        }
 
76
}
 
77
 
 
78
# TODO: rearange arguments
 
79
sub write_reg{#($$$$$) {
 
80
        my $read_slic = shift;
 
81
        my $read_reg = shift;
 
82
        my $direct = shift;
 
83
        my $reg_val_low = shift;
 
84
        my $reg_val_hi = shift;
 
85
        
 
86
        my $str  = sprintf "%d W%s %02X %02X", 
 
87
                $read_slic, $direct, $read_reg, $reg_val_low;
 
88
        if ($direct eq 'I') {
 
89
                $str .= sprintf " %02X", $reg_val_hi;
 
90
        }
 
91
        write_to_slic_file($str);
 
92
}
 
93
 
 
94
sub log_calib_params() {
 
95
        for my $i (100 .. 107) {
 
96
                my $line="Calib Reg $i:  ";
 
97
                for my $slic (@SlicNums) {
 
98
                        $line .= " ".read_reg($slic, $i, 'D');
 
99
                }
 
100
                debug($line);
 
101
        }
 
102
}
 
103
 
 
104
sub init_indirect_registers() {
 
105
        return write_to_slic_file("#
 
106
31      WI      00      C2      55
 
107
31      WI      01      E6      51
 
108
31      WI      02      85      4B
 
109
31      WI      03      37      49
 
110
                          
 
111
31      WI      04      33      33
 
112
31      WI      05      02      02
 
113
31      WI      06      02      02
 
114
31      WI      07      98      01
 
115
                          
 
116
31      WI      08      98      01
 
117
31      WI      09      11      06
 
118
31      WI      0A      02      02
 
119
31      WI      0B      E5      00
 
120
                          
 
121
31      WI      0C      1C      0A
 
122
31      WI      0D      30      7B
 
123
31      WI      0E      63      00
 
124
31      WI      0F      00      00
 
125
                          
 
126
31      WI      10      70      78
 
127
31      WI      11      7D      00
 
128
31      WI      12      00      00
 
129
31      WI      13      00      00
 
130
                          
 
131
31      WI      14      F0      7E
 
132
31      WI      15      C0      01
 
133
31      WI      16      00      00
 
134
31      WI      17      00      20
 
135
                          
 
136
31      WI      18      00      20
 
137
31      WI      19      00      00
 
138
31      WI      1A      00      20
 
139
31      WI      1B      00      40
 
140
                          
 
141
31      WI      1C      00      10
 
142
31      WI      1D      00      36
 
143
31      WI      1E      00      10
 
144
31      WI      1F      00      02
 
145
                          
 
146
31      WI      20      C0      07
 
147
31      WI      21      00      26
 
148
31      WI      22      F4      0F
 
149
31      WI      23      00      80
 
150
 
 
151
#31     WI      24      20      03
 
152
#31     WI      25      8C      08
 
153
#31     WI      26      00      01
 
154
#31     WI      27      10      00
 
155
                          
 
156
31      WI      24      00      08
 
157
31      WI      25      00      08
 
158
31      WI      26      00      08
 
159
31      WI      27      00      08
 
160
                          
 
161
31      WI      28      00      0C
 
162
31      WI      29      00      0C
 
163
31      WI      2B      00      01
 
164
                          
 
165
31      WI      63      DA      00
 
166
31      WI      64      60      6B
 
167
31      WI      65      74      00
 
168
31      WI      66      C0      79
 
169
                          
 
170
31      WI      67      20      11
 
171
31      WI      68      E0      3B      
 
172
#");
 
173
}
 
174
 
 
175
sub init_early_direct_regs() {
 
176
        return write_to_slic_file("#
 
177
31      WD      08 00
 
178
31      WD      4A 34
 
179
31      WD      4B 10
 
180
31      WD      40      00
 
181
#")
 
182
}
 
183
 
 
184
my @FilterParams = ();
 
185
 
 
186
sub save_indirect_filter_params() {
 
187
        for my $slic (@SlicNums) {
 
188
                for my $reg (35 .. 39) {
 
189
                        $FilterParams[$slic][$reg] = 
 
190
                                [read_reg($slic, $reg, 'I')];
 
191
                        write_reg($slic, $reg, 'I', 0, 0x80);
 
192
                }
 
193
        }
 
194
        
 
195
}
 
196
 
 
197
sub restore_indirect_filter_params() {
 
198
        for my $slic (@SlicNums) {
 
199
                for my $reg (35 .. 39) {
 
200
                        write_reg($slic, $reg, 'I', 
 
201
                                @{$FilterParams[$slic][$reg]});
 
202
                }
 
203
        }
 
204
}
 
205
 
 
206
my $ManualCalibrationSleepTime = 0.04; # 40ms
 
207
 
 
208
sub manual_calibrate_loop($$) {
 
209
        my $write_reg = shift;
 
210
        my $read_reg = shift;
 
211
        
 
212
        # counters to count down to (at most) 0
 
213
        my @slic_counters = ();
 
214
        for my $i (0 .. $#SlicNums) {
 
215
                $slic_counters[$i] = 0x1F;
 
216
        }
 
217
        
 
218
        # start calibration:
 
219
        my $calibration_in_progress = 1;
 
220
        write_reg(31, $write_reg, 'D', 0x1F);
 
221
        mysleep $ManualCalibrationSleepTime;
 
222
        
 
223
        # wait until all slics have finished calibration, or for timeout
 
224
        while ($calibration_in_progress) {
 
225
                $calibration_in_progress = 0; # until proven otherwise
 
226
                my $debug_calib_str = "ManualCalib:: ";
 
227
                for my $slic(@SlicNums) {
 
228
                        my $value = read_reg($slic, $read_reg, 'D');
 
229
                        $debug_calib_str .= " [$slic_counters[$slic]:$value]";
 
230
                        if ($value != 0 && $slic_counters[$slic] >= 0) {
 
231
                                $calibration_in_progress = 1;
 
232
                                $slic_counters[$slic]--;
 
233
                                write_reg($slic,$write_reg,'D',$slic_counters[$slic]);
 
234
                        }
 
235
                }
 
236
                debug($debug_calib_str);
 
237
                # TODO: unnecessary sleep in the last round:
 
238
                mysleep $ManualCalibrationSleepTime;
 
239
        }
 
240
}
 
241
 
 
242
sub manual_calibrate() {
 
243
        manual_calibrate_loop(98, 88);
 
244
        manual_calibrate_loop(99, 89);
 
245
}
 
246
 
 
247
sub auto_calibrate($$) {
 
248
        my $calib_96 = shift;
 
249
        my $calib_97 = shift;
 
250
 
 
251
        #log_calib_params();
 
252
        # start calibration:
 
253
        write_to_slic_file(
 
254
                sprintf
 
255
                        "31 WD 61 %02X\n".
 
256
                        "31 WD 60 %02X\n".
 
257
                        "", $calib_96, $calib_97
 
258
                        
 
259
        );
 
260
        # wait until all slics have finished calibration, or for timeout
 
261
        my $sleep_cnt = 0;
 
262
        # time periods in seconds:
 
263
        my $sleep_time = 0.1;
 
264
        my $timeout_time = 2; 
 
265
        CALIB_LOOP: for my $slic (@SlicNums) {
 
266
                debug("checking slic $slic");
 
267
                while(1) {
 
268
                        if ((read_reg($slic, 60, 'D')) == 0) {
 
269
                                # move to next register
 
270
                                debug("slic $slic calibrated");
 
271
                                last;
 
272
                        }
 
273
                        if ( $sleep_cnt > $timeout_time/$sleep_time) {
 
274
                                debug("Auto Calibration: Exiting on timeout: $timeout_time.");
 
275
                                last CALIB_LOOP;
 
276
                        }
 
277
                        debug("auto_calibrate not done yet: slic #$slic\n");
 
278
                        mysleep(0.1);
 
279
                        $sleep_cnt++;
 
280
                }
 
281
        }
 
282
        #log_calib_params();
 
283
}
 
284
 
 
285
###########################################################
 
286
#
 
287
# main
 
288
#
 
289
 
 
290
# TODO: for all slics check the following reads to check communication
 
291
#read_reg($slic, 0x08, 'D'): 0x02
 
292
#read_reg($slic, 0x0B, 'D'): 0x33
 
293
#read_reg($slic, 0x40, 'D'): 0x00 (?)
 
294
 
 
295
debug "starting\n";
 
296
 
 
297
init_indirect_registers();
 
298
debug "after init_indirect_registers\n";
 
299
init_early_direct_regs();
 
300
debug "after init_early_direct_regs\n";
 
301
auto_calibrate(0x47, 0x1E);
 
302
debug "after auto_calibrate\n";
 
303
manual_calibrate();
 
304
debug "after manul_calibrate\n";
 
305
auto_calibrate(0x40, 0x01);
 
306
debug "after auto_calibrate 2\n";
 
307
 
 
308