summaryrefslogtreecommitdiff
path: root/wcfxs.c
blob: e4a22b29693acde087b478d876bf6ecf567be442 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
/*
 * Wilcard S100P FXS Interface Driver for Zapata Telephony interface
 *
 * Written by Mark Spencer <markster@linux-support.net>
 *            Matthew Fredrickson <creslin@linux-support.net>
 *
 * Copyright (C) 2001, Linux Support Services, Inc.
 *
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
 *
 */

#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/usb.h>
#include <linux/errno.h>
#include <linux/pci.h>

#include "proslic.h"
#include "wcfxs.h"

static alpha  indirect_regs[] =
{
{0,"DTMF_ROW_0_PEAK",0x55C2},
{1,"DTMF_ROW_1_PEAK",0x51E6},
{2,"DTMF_ROW2_PEAK",0x4B85},
{3,"DTMF_ROW3_PEAK",0x4937},
{4,"DTMF_COL1_PEAK",0x3333},
{5,"DTMF_FWD_TWIST",0x0202},
{6,"DTMF_RVS_TWIST",0x0202},
{7,"DTMF_ROW_RATIO_TRES",0x0198},
{8,"DTMF_COL_RATIO_TRES",0x0198},
{9,"DTMF_ROW_2ND_ARM",0x0611},
{10,"DTMF_COL_2ND_ARM",0x0202},
{11,"DTMF_PWR_MIN_TRES",0x00E5},
{12,"DTMF_OT_LIM_TRES",0x0A1C},
{13,"OSC1_COEF",0x7B30},
{14,"OSC1X",0x0063},
{15,"OSC1Y",0x0000},
{16,"OSC2_COEF",0x7870},
{17,"OSC2X",0x007D},
{18,"OSC2Y",0x0000},
{19,"RING_V_OFF",0x0000},
{20,"RING_OSC",0x7EF0},
{21,"RING_X",0x0160},
{22,"RING_Y",0x0000},
{23,"PULSE_ENVEL",0x2000},
{24,"PULSE_X",0x2000},
{25,"PULSE_Y",0x0000},
//{26,"RECV_DIGITAL_GAIN",0x4000},	// playback volume set lower
{26,"RECV_DIGITAL_GAIN",0x2000},	// playback volume set lower
//{27,"XMIT_DIGITAL_GAIN",0x4000},
{27,"XMIT_DIGITAL_GAIN",0x2000},
{28,"LOOP_CLOSE_TRES",0x1000},
{29,"RING_TRIP_TRES",0x3600},
{30,"COMMON_MIN_TRES",0x1000},
{31,"COMMON_MAX_TRES",0x0200},
{32,"PWR_ALARM_Q1Q2",0x07C0},
{33,"PWR_ALARM_Q3Q4",0x2600},
{34,"PWR_ALARM_Q5Q6",0x1B80},
{35,"LOOP_CLOSURE_FILTER",0x8000},
{36,"RING_TRIP_FILTER",0x0320},
{37,"TERM_LP_POLE_Q1Q2",0x008C},
{38,"TERM_LP_POLE_Q3Q4",0x0100},
{39,"TERM_LP_POLE_Q5Q6",0x0010},
{40,"CM_BIAS_RINGING",0x0C00},
{41,"DCDC_MIN_V",0x0C00},
{42,"DCDC_XTRA",0x1000},
{43,"LOOP_CLOSE_TRES_LOW",0x1000},
{97,"RCV_FLTR", 0}
};

#ifdef STANDALONE_ZAPATA
#include "zaptel.h"
#else
#include <linux/zaptel.h>
#endif

#define WC_MAX_IFACES 128

#define WC_CNTL    	0x00
#define WC_OPER		0x01
#define WC_AUXC    	0x02
#define WC_AUXD    	0x03
#define WC_MASK0   	0x04
#define WC_MASK1   	0x05
#define WC_INTSTAT 	0x06
#define WC_AUXR		0x07

#define WC_DMAWS	0x08
#define WC_DMAWI	0x0c
#define WC_DMAWE	0x10
#define WC_DMARS	0x18
#define WC_DMARI	0x1c
#define WC_DMARE	0x20

#define WC_AUXFUNC	0x2b
#define WC_SERCTL	0x2d
#define WC_FSCDELAY	0x2f

#define WC_REGBASE	0xc0

#define WC_LEDS		0x0
#define WC_TEST		0x1
#define WC_CS		0x2
#define WC_VER		0x3
#define WC_SYNC		0x4

#define BIT_CS		(1 << 2)
#define BIT_SCLK	(1 << 3)
#define BIT_SDI		(1 << 4)
#define BIT_SDO		(1 << 5)

#define FLAG_EMPTY	0
#define FLAG_WRITE	1
#define FLAG_READ	2

#define RING_DEBOUNCE	64		/* Ringer Debounce (in ms) */
#define BATT_DEBOUNCE	8		/* Battery debounce (in ms) */

#define FLAG_DOUBLE_CLOCK	(1 << 0)

#define NUM_CARDS 4

struct wcfxs {
	struct pci_dev *dev;
	char *variety;
	struct zt_span span;
	unsigned char ios;
	int usecount;
	int intcount;
	int dead;
	int pos;
	int flags;
	int freeregion;
	int alt;
	int curcard;
	int cards;
	spinlock_t lock;

	/* Receive hook state and debouncing */
	int oldrxhook[NUM_CARDS];
	int debouncehook[NUM_CARDS];
	int lastrxhook[NUM_CARDS];
	int debounce[NUM_CARDS];

	int idletxhookstate;		/* IDLE changing hook state */
	unsigned long ioaddr;
	dma_addr_t 	readdma;
	dma_addr_t	writedma;
	volatile int *writechunk;					/* Double-word aligned write memory */
	volatile int *readchunk;					/* Double-word aligned read memory */
	struct zt_chan chans[NUM_CARDS];
};


struct wcfxs_desc {
	char *name;
	int flags;
};

static struct wcfxs_desc wcfxs = { "Wildcard S400P Prototype", 0 };

static struct wcfxs *ifaces[WC_MAX_IFACES];

static void wcfxs_release(struct wcfxs *wc);

static int debug = 0;

static inline void wcfxs_transmitprep(struct wcfxs *wc, unsigned char ints)
{
	volatile unsigned int *writechunk;
	int x;
	if (ints & 0x01) 
		/* Write is at interrupt address.  Start writing from normal offset */
		writechunk = wc->writechunk;
	else 
		writechunk = wc->writechunk + ZT_CHUNKSIZE;
	/* Calculate Transmission */
	zt_transmit(&wc->span);

	for (x=0;x<ZT_CHUNKSIZE;x++) {
		/* Send a sample, as a 32-bit word */
		writechunk[x] = (wc->chans[0].writechunk[x] << 24);
		if (wc->cards > 1)
			writechunk[x] |= (wc->chans[1].writechunk[x] << 16);
		if (wc->cards > 2)
			writechunk[x] |= (wc->chans[2].writechunk[x] << 8);
		if (wc->cards > 3)
			writechunk[x] |= (wc->chans[3].writechunk[x]);
		
	}

}

static inline void wcfxs_receiveprep(struct wcfxs *wc, unsigned char ints)
{
	volatile unsigned int *readchunk;
	int x;

	if (ints & 0x08)
		readchunk = wc->readchunk + ZT_CHUNKSIZE;
	else
		/* Read is not at interrupt address.  Valid data is available at normal offset */
		readchunk = wc->readchunk;
	for (x=0;x<ZT_CHUNKSIZE;x++) {
		wc->chans[0].readchunk[x] = (readchunk[x] >> 24) & 0xff;
		if (wc->cards > 1)
			wc->chans[1].readchunk[x] = (readchunk[x] >> 16) & 0xff;
		if (wc->cards > 2)
			wc->chans[2].readchunk[x] = (readchunk[x] >> 8) & 0xff;
		if (wc->cards > 3)
			wc->chans[3].readchunk[x] = (readchunk[x]) & 0xff;
	}

	zt_receive(&wc->span);
}

static inline void wcfxs_check_hook(struct wcfxs *wc, int card);

static void wcfxs_interrupt(int irq, void *dev_id, struct pt_regs *regs)
{
	struct wcfxs *wc = dev_id;
	unsigned char ints;
	static int p=0;

	ints = inb(wc->ioaddr + WC_INTSTAT);
	outb(ints, wc->ioaddr + WC_INTSTAT);

	if (!p) {
		printk("Interrupt!\n");
		p++;
	}

	if (!ints)
		return;

	if (ints & 0x10) {
		printk("PCI Master abort\n");
		return;
	}
	
	if (ints & 0x20) {
		printk("PCI Target abort\n");
		return;
	}


	if (ints & 0x0f) {
		wc->intcount++;
		if ((wc->intcount % 10) < wc->cards) {
			wcfxs_check_hook(wc, wc->intcount % 10);
		}
		if (ints & 0xc) wcfxs_receiveprep(wc, ints);
		if (ints & 3) wcfxs_transmitprep(wc, ints);
	}
	
}

static inline void write_8bits(struct wcfxs *wc, unsigned char bits)
{
	/* Drop chip select */
	int x;
	wc->ios &= ~BIT_CS;
	outb(wc->ios, wc->ioaddr + WC_AUXD);
	for (x=0;x<8;x++) {
		/* Send out each bit, MSB first, drop SCLK as we do so */
		if (bits & 0x80)
			wc->ios |= BIT_SDI;
		else
			wc->ios &= ~BIT_SDI;
		wc->ios &= ~BIT_SCLK;
		outb(wc->ios, wc->ioaddr + WC_AUXD);
		/* Now raise SCLK high again and repeat */
		wc->ios |= BIT_SCLK;
		outb(wc->ios, wc->ioaddr + WC_AUXD);
		bits <<= 1;
	}
	/* Finally raise CS back high again */
	wc->ios |= BIT_CS;
	outb(wc->ios, wc->ioaddr + WC_AUXD);
	outb(wc->ios, wc->ioaddr + WC_AUXD);
	outb(wc->ios, wc->ioaddr + WC_AUXD);
	outb(wc->ios, wc->ioaddr + WC_AUXD);
	
}
static inline unsigned char read_8bits(struct wcfxs *wc)
{
	unsigned char res=0, c;
	int x;
	/* Drop chip select */
	wc->ios &= ~BIT_CS;
	outb(wc->ios, wc->ioaddr + WC_AUXD);
	for (x=0;x<8;x++) {
		res <<= 1;
		/* Get SCLK */
		wc->ios &= ~BIT_SCLK;
		outb(wc->ios, wc->ioaddr + WC_AUXD);
		/* Now raise SCLK high again */
		wc->ios |= BIT_SCLK;
		outb(wc->ios, wc->ioaddr + WC_AUXD);

		/* Read back the value */
		c = inb(wc->ioaddr + WC_AUXR);
		if (c & BIT_SDO)
			res |= 1;
	}
	/* Finally raise CS back high again */
	wc->ios |= BIT_CS;
	outb(wc->ios, wc->ioaddr + WC_AUXD);
	outb(wc->ios, wc->ioaddr + WC_AUXD);
	outb(wc->ios, wc->ioaddr + WC_AUXD);
	outb(wc->ios, wc->ioaddr + WC_AUXD);

	/* And return our result */
	return res;
}

static void wcfxs_setcreg(struct wcfxs *wc, unsigned char reg, unsigned char val)
{
	outb(val, wc->ioaddr + WC_REGBASE + ((reg & 0xf) << 2));
}

static unsigned char wcfxs_getcreg(struct wcfxs *wc, unsigned char reg)
{
	return inb(wc->ioaddr + WC_REGBASE + ((reg & 0xf) << 2));
}

static inline void wcfxs_setcard(struct wcfxs *wc, int card)
{
	if (wc->curcard != card) {
		wcfxs_setcreg(wc, WC_CS, (1 << card));
		wc->curcard = card;
	}
}

static void __wcfxs_setreg(struct wcfxs *wc, int card, unsigned char reg, unsigned char value)
{
	wcfxs_setcard(wc, card);
	write_8bits(wc, reg & 0x7f);
	write_8bits(wc, value);
}

static void wcfxs_setreg(struct wcfxs *wc, int card, unsigned char reg, unsigned char value)
{
	long flags;
	spin_lock_irqsave(&wc->lock, flags);
	__wcfxs_setreg(wc, card, reg, value);
	spin_unlock_irqrestore(&wc->lock, flags);
}

static unsigned char __wcfxs_getreg(struct wcfxs *wc, int card, unsigned char reg)
{
	wcfxs_setcard(wc, card);
	write_8bits(wc, reg | 0x80);
	return read_8bits(wc);
}

static unsigned char wcfxs_getreg(struct wcfxs *wc, int card, unsigned char reg)
{
	long flags;
	unsigned char res;
	spin_lock_irqsave(&wc->lock, flags);
	res = __wcfxs_getreg(wc, card, reg);
	spin_unlock_irqrestore(&wc->lock, flags);
	return res;
}

static int __wait_access(struct wcfxs *wc, int card)
{
    unsigned char data;
    long origjiffies;
    int count = 0;

    #define MAX 6000 /* attempts */


    origjiffies = jiffies;
    /* Wait for indirect access */
    while (count++ < MAX)
	 {
		data = __wcfxs_getreg(wc, card, I_STATUS);

		if (!data)
			return 0;

	 }

    if(count > (MAX-1)) printk(" ##### Loop error (%02x) #####\n", data);

	return 0;
}

static int wcfxs_setreg_indirect(struct wcfxs *wc, int card, unsigned char address, unsigned short data)
{
	long flags;
	int res = -1;
	spin_lock_irqsave(&wc->lock, flags);
	if(!__wait_access(wc, card)) {
		__wcfxs_setreg(wc, card, IDA_LO,(unsigned char)(data & 0xFF));
		__wcfxs_setreg(wc, card, IDA_HI,(unsigned char)((data & 0xFF00)>>8));
		__wcfxs_setreg(wc, card, IAA,address);
		res = 0;
	};
	spin_unlock_irqrestore(&wc->lock, flags);
	return res;
}

static int wcfxs_getreg_indirect(struct wcfxs *wc, int card, unsigned char address)
{ 
	long flags;
	int res = -1;
	char *p=NULL;
	spin_lock_irqsave(&wc->lock, flags);
	if (!__wait_access(wc, card)) {
		__wcfxs_setreg(wc, card, IAA, address);
		if (!__wait_access(wc, card)) {
			unsigned char data1, data2;
			data1 = __wcfxs_getreg(wc, card, IDA_LO);
			data2 = __wcfxs_getreg(wc, card, IDA_HI);
			res = data1 | (data2 << 8);
		} else
			p = "Failed to wait inside\n";
	} else
		p = "failed to wait\n";
	spin_unlock_irqrestore(&wc->lock, flags);
	if (p)
		printk(p);
	return res;
}

static int wcfxs_init_indirect_regs(struct wcfxs *wc, int card)
{
	unsigned char i;

	for (i=0; i<43; i++)
	{
		if(wcfxs_setreg_indirect(wc, card, i,indirect_regs[i].initial))
			return -1;
	}

	return 0;
}

static int wcfxs_verify_indirect_regs(struct wcfxs *wc, int card)
{ 
	int passed = 1;
	unsigned short i, initial;
	int j;

	for (i=0; i<43; i++) 
	{
		if((j = wcfxs_getreg_indirect(wc, card, (unsigned char) i)) < 0) {
			printk("Failed to read indirect register %d\n", i);
			return -1;
		}
		initial= indirect_regs[i].initial;

		if ( j != initial )
		{
			 printk("!!!!!!! %s  iREG %X = %X  should be %X\n",
				indirect_regs[i].name,i,j,initial );
			 passed = 0;
		}	
	}

    if (passed) {
		if (debug)
			printk("Init Indirect Registers completed successfully.\n");
    } else {
		printk(" !!!!! Init Indirect Registers UNSUCCESSFULLY.\n");
		return -1;
    }
    return 0;
}

static int wcfxs_proslic_insane(struct wcfxs *wc, int card)
{
	int blah;

	blah = wcfxs_getreg(wc, card, 0);
	if (debug) 
		printk("ProSLIC on module %d, product %d, version %d\n", card, (blah & 0x30) >> 4, (blah & 0xf));

	if ((blah & 0x30) >> 4) {
		printk("ProSLIC on module %d is not a 3210.\n", card);
		return -1;
	}
	if ((blah & 0xf) < 3) {
		printk("ProSLIC 3210 version %d is too old\n", blah & 0xf);
		return -1;
	}

	blah = wcfxs_getreg(wc, card, 8);
	if (blah != 0x2) {
		printk("ProSLIC on module %d insane (1)\n", card);
		return -1;
	}

	blah = wcfxs_getreg(wc, card, 64);
	if (blah != 0x0) {
		printk("ProSLIC on module %d insane (2)\n", card);
		return -1;
	}

	blah = wcfxs_getreg(wc, card, 11);
	if (blah != 0x33) {
		printk("ProSLIC on module %d insane (3)\n", card);
		return -1;
	}

	/* Just be sure it's setup right. */
	wcfxs_setreg(wc, card, 30, 0);

	if (debug) 
		printk("ProSLIC on module %d seems sane.\n", card);
	return 0;
}

static int wcfxs_powerleak_test(struct wcfxs *wc, int card)
{
	unsigned long origjiffies;
	unsigned char vbat;

	/* Turn off linefeed */
	wcfxs_setreg(wc, card, 64, 0);

	/* Power down */
	wcfxs_setreg(wc, card, 14, 0x10);

	/* Wait for one second */
	origjiffies = jiffies;

	while((vbat = wcfxs_getreg(wc, card, 82)) > 0x3) {
		if ((jiffies - origjiffies) >= HZ)
			break;;
	}

	if (vbat < 0x04) {
		printk("Excessive leakage detected on module %d: %d volts (%02x) after %d ms\n", card,
		       376 * vbat / 1000, vbat, (int)((jiffies - origjiffies) * 1000 / HZ));
		return -1;
	} else if (debug) {
		printk("Post-leakage voltage: %d volts\n", 376 * vbat / 1000);
	}
	return 0;
}

static int wcfxs_powerup_proslic(struct wcfxs *wc, int card)
{
	unsigned char vbat;
	unsigned long origjiffies;

	/* Set period of DC-DC converter to 1/64 khz */
	wcfxs_setreg(wc, card, 92, 0xff);

	/* Engage DC-DC converter */
	wcfxs_setreg(wc, card, 93, 0x19);

	/* Wait for VBat to powerup */
	origjiffies = jiffies;

	/* Disable powerdown */
	wcfxs_setreg(wc, card, 14, 0);

	while((vbat = wcfxs_getreg(wc, card, 82)) < 0xc0) {
		/* Wait no more than 500ms */
		if ((jiffies - origjiffies) > HZ/2) {
			break;
		}
	}

#if 0
	printk("jiffies - origjiffies: %d\n", ((int)(jiffies - origjiffies)));
#endif

	if (vbat < 0xc0) {
		printk("ProSLIC on module %d failed to powerup within %d ms\n",
		       card, (int)(((jiffies - origjiffies) * 1000 / HZ)));
		return -1;
	} else if (debug) {
		printk("ProSLIC on module %d powered up to -%d volts (%02x) in %d ms\n",
		       card, vbat * 376 / 1000, vbat, (int)(((jiffies - origjiffies) * 1000 / HZ)));
	}
	/* Perform DC-DC calibration */
	wcfxs_setreg(wc, card, 93, 0x80);

	origjiffies = jiffies;
	while(0x80 & wcfxs_getreg(wc, card, 93)) {
		if ((jiffies - origjiffies) > 2 * HZ) {
			printk("Timeout waiting for DC-DC calibration on module %d\n", card);
			return -1;
		}
	}

#if 0
	/* Wait a full two seconds */
	while((jiffies - origjiffies) < 2 * HZ);

	/* Just check to be sure */
	vbat = wcfxs_getreg(wc, card, 82);
	printk("ProSLIC on module %d powered up to -%d volts (%02x) in %d ms\n",
		       card, vbat * 376 / 1000, vbat, (int)(((jiffies - origjiffies) * 1000 / HZ)));
#endif
	return 0;

}

static int wcfxs_calibrate(struct wcfxs *wc, int card)
{
	unsigned long origjiffies;
	int x;
	/* Perform all calibrations */
	wcfxs_setreg(wc, card, 97, 0x1f);
	
	/* Begin, no speedup */
	wcfxs_setreg(wc, card, 96, 0x5f);

	/* Wait for it to finish */
	origjiffies = jiffies;
	while(wcfxs_getreg(wc, card, 96)) {
		if ((jiffies - origjiffies) > 2 * HZ) {
			printk("Timeout waiting for calibration of module %d\n", card);
			return -1;
		}
	}
	
	if (debug) {
		/* Print calibration parameters */
		printk("Calibration Vector Regs 98 - 107: \n");
		for (x=98;x<108;x++) {
			printk("%d: %02x\n", x, wcfxs_getreg(wc, card, x));
		}
	}
	return 0;
}

static int wcfxs_init_proslic(struct wcfxs *wc, int card)
{

	unsigned short tmp[5];
	int x;

	/* By default, always send on hook */
	wc->idletxhookstate = 2;

	/* Sanity check the ProSLIC */
	if (wcfxs_proslic_insane(wc, card))
		return -1;

	if (wcfxs_init_indirect_regs(wc, card)) {
		printk(KERN_INFO "Indirect Registers failed to initialize on module %d.\n", card);
		return -1;
	}

	/* Clear scratch pad area */
	wcfxs_setreg_indirect(wc, card, 97,0);

	/* Clear digital loopback */
	wcfxs_setreg(wc, card, 8, 0);

	/* Revision C optimization */
	wcfxs_setreg(wc, card, 108, 0xeb);

	/* Disable automatic VBat switching for safety to prevent
	   Q7 from accidently turning on and burning out. */
	wcfxs_setreg(wc, card, 67, 0x17);

	/* Turn off Q7 */
	wcfxs_setreg(wc, card, 66, 1);

	/* Flush ProSLIC digital filters by setting to clear, while
	   saving old values */
	for (x=0;x<5;x++) {
		tmp[x] = wcfxs_getreg_indirect(wc, card, x + 35);
		wcfxs_setreg_indirect(wc, card, x + 35, 0x8000);
	}

	/* Power up the DC-DC converter */
	if (wcfxs_powerup_proslic(wc, card)) {
		printk("Unable to do INITIAL ProSLIC powerup on module %d\n", card);
		return -1;
	}

	/* Check for power leaks */
	if (wcfxs_powerleak_test(wc, card)) {
		printk("ProSLIC module %d failed leakage test.  Check for short circuit\n", card);
		return -1;
	}

	/* Power up again */
	if (wcfxs_powerup_proslic(wc, card)) {
		printk("Unable to do FINAL ProSLIC powerup on module %d\n", card);
		return -1;
	}

	/* Perform calibration */
	if (wcfxs_calibrate(wc, card)) {
		printk("ProSlic died on Calibration.\n");
		return -1;
	}

	/* Calibration complete, restore original values */
	for (x=0;x<5;x++) {
		wcfxs_setreg_indirect(wc, card, x + 35, tmp[x]);
	}

	if (wcfxs_verify_indirect_regs(wc, card)) {
		printk(KERN_INFO "Indirect Registers failed verification.\n");
		return -1;
	}


#if 0
    /* Disable Auto Power Alarm Detect and other "features" */
    wcfxs_setreg(wc, card, 67, 0x0e);
    blah = wcfxs_getreg(wc, card, 67);
#endif

#if 0
    if (wcfxs_setreg_indirect(wc, card, 97, 0x0)) { // Stanley: for the bad recording fix
		 printk(KERN_INFO "ProSlic IndirectReg Died.\n");
		 return -1;
	}
#endif

    wcfxs_setreg(wc, card, 1, 0x28);
 	// U-Law 8-bit interface
    wcfxs_setreg(wc, card, 2, 0);    // Tx Start count low byte  0
    wcfxs_setreg(wc, card, 3, 0);    // Tx Start count high byte 0
    wcfxs_setreg(wc, card, 4, 0);    // Rx Start count low byte  0
    wcfxs_setreg(wc, card, 5, 0);    // Rx Start count high byte 0
    wcfxs_setreg(wc, card, 18, 0xff);     // clear all interrupt
    wcfxs_setreg(wc, card, 19, 0xff);
    wcfxs_setreg(wc, card, 20, 0xff);
    wcfxs_setreg(wc, card, 73, 0x04);

#if 0
    wcfxs_setreg(wc, card, 21, 0x00); 	// enable interrupt
    wcfxs_setreg(wc, card, 22, 0x02); 	// Loop detection interrupt
    wcfxs_setreg(wc, card, 23, 0x01); 	// DTMF detection interrupt
    wcfxs_setreg(wc, card, 72, 0x20);
#endif

#if 0
    /* Enable loopback */
    wcfxs_setreg(wc, card, 8, 0x2);
    wcfxs_setreg(wc, card, 14, 0x0);
    wcfxs_setreg(wc, card, 64, 0x0);
    wcfxs_setreg(wc, card, 1, 0x08);
#endif

    printk("Loopback: %02x\n", wcfxs_getreg(wc, card, 8));

#ifdef BOOST_RINGER
	/* Beef up Ringing voltage to 89V */
	if (wcfxs_setreg_indirect(wc, card, 23, 0x1d1))
			return -1;
#endif
	return 0;
}

static inline void wcfxs_check_hook(struct wcfxs *wc, int card)
{
	char res;
	int hook;

	/* For some reason we have to debounce the
	   hook detector.  */

	res = wcfxs_getreg(wc, card, 68);
	hook = (res & 1);
	if (hook != wc->lastrxhook[card]) {
		/* Reset the debounce */
		wc->debounce[card] = 50;
#if 0
		printk("Resetting debounce card %d hook %d, %d\n", card, hook, wc->debounce[card]);
#endif
	} else {
		if (wc->debounce[card] > -1) {
			wc->debounce[card]--;
#if 0
			printk("Sustaining hook %d, %d\n", hook, wc->debounce[card]);
#endif
		}
	}
	wc->lastrxhook[card] = hook;
	if (!wc->debounce[card]) {
#if 0
		printk("Counted down debounce, newhook: %d...\n", hook);
#endif
		wc->debouncehook[card] = hook;
	}

	if (!wc->oldrxhook[card] && wc->debouncehook[card]) {
		/* Off hook */
		if (debug)
			printk("wcfxs: Card %d Going off hook\n", card);
		zt_hooksig(&wc->chans[card], ZT_RXSIG_OFFHOOK);
		wc->oldrxhook[card] = 1;
		
	} else if (wc->oldrxhook[card] && !wc->debouncehook[card]) {
		/* On hook */
		if (debug)
			printk("wcfxs: Card %d Going on hook\n", card);
		zt_hooksig(&wc->chans[card], ZT_RXSIG_ONHOOK);
		wc->oldrxhook[card] = 0;
	}
	
}

static int wcfxs_ioctl(struct zt_chan *chan, unsigned int cmd, unsigned long data)
{
	struct wcfxs_stats stats;
	struct wcfxs_regs regs;
	struct wcfxs *wc = chan->pvt;
	int x;
	switch (cmd) {
	case WCFXS_GET_STATS:
		stats.tipvolt = wcfxs_getreg(wc, chan->chanpos - 1, 80) * -376;
		stats.ringvolt = wcfxs_getreg(wc, chan->chanpos - 1, 81) * -376;
		stats.batvolt = wcfxs_getreg(wc, chan->chanpos - 1, 82) * -376;
		if (copy_to_user((struct wcfxs_stats *)data, &stats, sizeof(stats)))
			return -EFAULT;
		break;
	case WCFXS_GET_REGS:
		for (x=0;x<NUM_INDIRECT_REGS;x++)
			regs.indirect[x] = wcfxs_getreg_indirect(wc, chan->chanpos -1, x);
		for (x=0;x<NUM_REGS;x++)
			regs.direct[x] = wcfxs_getreg(wc, chan->chanpos - 1, x);
		if (copy_to_user((struct wcfxs_regs *)data, &regs, sizeof(regs)))
			return -EFAULT;
		break;
	default:
		return -ENOTTY;
	}
	return 0;

}

static int wcfxs_open(struct zt_chan *chan)
{
	struct wcfxs *wc = chan->pvt;
	if (wc->dead)
		return -ENODEV;
	wc->usecount++;
	MOD_INC_USE_COUNT;
	return 0;
}

static int wcfxs_close(struct zt_chan *chan)
{
	struct wcfxs *wc = chan->pvt;
	wc->usecount--;
	MOD_DEC_USE_COUNT;
	/* If we're dead, release us now */
	if (!wc->usecount && wc->dead) 
		wcfxs_release(wc);
	return 0;
}

static int wcfxs_hooksig(struct zt_chan *chan, zt_txsig_t txsig)
{
	struct wcfxs *wc = chan->pvt;
	int reg=0;
	unsigned char txhook = 0;
	switch(txsig) {
	case ZT_TXSIG_ONHOOK:
		switch(chan->sig) {
		case ZT_SIG_FXOKS:
		case ZT_SIG_FXOLS:
			txhook = wc->idletxhookstate;
			break;
		case ZT_SIG_FXOGS:
			txhook = 3;
			break;
		}
		break;
	case ZT_TXSIG_OFFHOOK:
		txhook = wc->idletxhookstate;
		break;
	case ZT_TXSIG_START:
		txhook = 4;
		break;
	case ZT_TXSIG_KEWL:
		txhook = 0;
		break;
	default:
		printk("wcfxs: Can't set tx state to %d\n", txsig);
	}
	if (debug)
		printk("Setting hook state to %d (%02x)\n", txsig, reg);

#if 1
	wcfxs_setreg(wc, chan->chanpos - 1, 64, txhook);
#endif
	return 0;
}

static int wcfxs_initialize(struct wcfxs *wc)
{
	int x;
	/* Zapata stuff */
	sprintf(wc->span.name, "WCFXS/%d", wc->pos);
	sprintf(wc->span.desc, "%s Board %d, %d modules\n", wc->variety, wc->pos + 1, wc->cards);
	wc->span.deflaw = ZT_LAW_MULAW;
	for (x=0;x<wc->cards;x++) {
		sprintf(wc->chans[x].name, "WCFXS/%d/%d", wc->pos, x);
		wc->chans[x].sigcap = ZT_SIG_FXOKS | ZT_SIG_FXOLS | ZT_SIG_FXOGS;
		wc->chans[x].chanpos = x+1;
		wc->chans[x].pvt = wc;
	}
	wc->span.chans = wc->chans;
	wc->span.channels = wc->cards;
	wc->span.hooksig = wcfxs_hooksig;
	wc->span.open = wcfxs_open;
	wc->span.close = wcfxs_close;
	wc->span.flags = ZT_FLAG_RBS;
	wc->span.ioctl = wcfxs_ioctl;
	init_waitqueue_head(&wc->span.maintq);

	wc->span.pvt = wc;
	if (zt_register(&wc->span, 0)) {
		printk("Unable to register span with zaptel\n");
		return -1;
	}
	return 0;
}

static int wcfxs_hardware_init(struct wcfxs *wc)
{
	/* Hardware stuff */
	long oldjiffies;
	unsigned char ver;
	unsigned char x,y;
	int failed;

	/* Check Freshmaker chip */
	ver = wcfxs_getcreg(wc, WC_VER);
	failed = 0;
	if (ver != 0x59) {
		printk("Freshmaker version: %02x\n", ver);
		for (x=0;x<254;x++) {
			/* Test registers */
			wcfxs_setcreg(wc, WC_TEST, x);
			y = wcfxs_getcreg(wc, WC_TEST);
			if (x != y) {
				printk("%02x != %02x\n", x, y);
				failed++;
			}
		}
		if (!failed) {
			printk("Freshmaker passed register test\n");
		} else {
			printk("Freshmaker failed register test\n");
			return -1;
		}
		/* Go to half-duty FSYNC */
		wcfxs_setcreg(wc, WC_SYNC, 1);
	} else {
		printk("No freshmaker chip\n");
	}

	/* Reset PCI Interface chip and registers (and serial) */
	outb(0x0e, wc->ioaddr + WC_CNTL);
	/* Setup our proper outputs for when we switch for our "serial" port */
	wc->ios = BIT_CS | BIT_SCLK | BIT_SDI;

	outb(wc->ios, wc->ioaddr + WC_AUXD);

	/* Set all to outputs except AUX 5, which is an input */
	outb(0xdf, wc->ioaddr + WC_AUXC);
	
	/* Wait 1/4 of a sec */
	oldjiffies = jiffies;
	while(jiffies - oldjiffies < (HZ / 4) + 1);

	/* Back to normal, with automatic DMA wrap around */
	outb(0x30 | 0x01, wc->ioaddr + WC_CNTL);
	
	/* Make sure serial port and DMA are out of reset */
	outb(inb(wc->ioaddr + WC_CNTL) & 0xf9, WC_CNTL);
	
	/* Configure serial port for MSB->LSB operation */
	outb(0xc1, wc->ioaddr + WC_SERCTL);

	/* Delay FSC by 0 so it's properly aligned */
	outb(0x0, wc->ioaddr + WC_FSCDELAY);

	/* Setup DMA Addresses */
	outl(wc->writedma,                    wc->ioaddr + WC_DMAWS);		/* Write start */
	outl(wc->writedma + ZT_CHUNKSIZE * 4, wc->ioaddr + WC_DMAWI);		/* Middle (interrupt) */
	outl(wc->writedma + ZT_CHUNKSIZE * 8 - 4, wc->ioaddr + WC_DMAWE);			/* End */
	
	outl(wc->readdma,                    	 wc->ioaddr + WC_DMARS);	/* Read start */
	outl(wc->readdma + ZT_CHUNKSIZE * 4, 	 wc->ioaddr + WC_DMARI);	/* Middle (interrupt) */
	outl(wc->readdma + ZT_CHUNKSIZE * 8 - 4, wc->ioaddr + WC_DMARE);	/* End */
	
	/* Clear interrupts */
	outb(0xff, wc->ioaddr + WC_INTSTAT);

	/* Wait 1/4 of a second more */
	oldjiffies = jiffies;
	while(jiffies - oldjiffies < (HZ / 4) + 1);

	for (x=0;x<wc->cards;x++) {
		if (wcfxs_init_proslic(wc, x)) {
			printk("Error initializing card %d\n", x);
			return -1;
		}
	}
	return 0;
}

static void wcfxs_enable_interrupts(struct wcfxs *wc)
{
	/* Enable interrupts (we care about all of them) */
	outb(0x3f, wc->ioaddr + WC_MASK0);
	/* No external interrupts */
	outb(0x00, wc->ioaddr + WC_MASK1);
}

static void wcfxs_start_dma(struct wcfxs *wc)
{
	/* Reset Master and TDM */
	outb(0x0f, wc->ioaddr + WC_CNTL);
	set_current_state(TASK_INTERRUPTIBLE);
	schedule_timeout(1);
	outb(0x01, wc->ioaddr + WC_CNTL);
	outb(0x01, wc->ioaddr + WC_OPER);
}

static void wcfxs_stop_dma(struct wcfxs *wc)
{
	outb(0x00, wc->ioaddr + WC_OPER);
}

static void wcfxs_disable_interrupts(struct wcfxs *wc)	
{
	outb(0x00, wc->ioaddr + WC_MASK0);
	outb(0x00, wc->ioaddr + WC_MASK1);
}

static int __devinit wcfxs_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
{
	int res;
	struct wcfxs *wc;
	struct wcfxs_desc *d = (struct wcfxs_desc *)ent->driver_data;
	int x;
	static int initd_ifaces=0;
	
	if(initd_ifaces){
		memset((void *)ifaces,0,(sizeof(struct wcfxs *))*WC_MAX_IFACES);
		initd_ifaces=1;
	}
	for (x=0;x<WC_MAX_IFACES;x++)
		if (!ifaces[x]) break;
	if (x >= WC_MAX_IFACES) {
		printk("Too many interfaces\n");
		return -EIO;
	}
	
	if (pci_enable_device(pdev)) {
		res = -EIO;
	} else {
		wc = kmalloc(sizeof(struct wcfxs), GFP_KERNEL);
		if (wc) {
			ifaces[x] = wc;
			memset(wc, 0, sizeof(struct wcfxs));
			spin_lock_init(&wc->lock);
			wc->curcard = -1;
			wc->cards = 1;
			wc->ioaddr = pci_resource_start(pdev, 0);
			wc->dev = pdev;
			wc->pos = x;
			wc->variety = d->name;
			wc->flags = d->flags;
			/* Keep track of whether we need to free the region */
			if (request_region(wc->ioaddr, 0xff, "wcfxs")) 
				wc->freeregion = 1;

			/* Allocate enough memory for two zt chunks, receive and transmit.  Each sample uses
			   32 bits.  Allocate an extra set just for control too */
			wc->writechunk = (int *)pci_alloc_consistent(pdev, ZT_MAX_CHUNKSIZE * 2 * 2 * 2 * 4, &wc->writedma);
			if (!wc->writechunk) {
				printk("wcfxs: Unable to allocate DMA-able memory\n");
				if (wc->freeregion)
					release_region(wc->ioaddr, 0xff);
				return -ENOMEM;
			}

			wc->readchunk = wc->writechunk + ZT_MAX_CHUNKSIZE * 2;	/* in doublewords */
			wc->readdma = wc->writedma + ZT_MAX_CHUNKSIZE * 8;		/* in bytes */

			if (wcfxs_initialize(wc)) {
				printk("wcfxs: Unable to intialize FXS\n");
				if (wc->freeregion)
					release_region(wc->ioaddr, 0xff);
				pci_free_consistent(pdev, ZT_MAX_CHUNKSIZE * 2 * 2 * 2 * 4, (void *)wc->writechunk, wc->writedma);
				kfree(wc);
				return -EIO;
			}

			/* Enable bus mastering */
			pci_set_master(pdev);

			/* Keep track of which device we are */
			pci_set_drvdata(pdev, wc);

			if (request_irq(pdev->irq, wcfxs_interrupt, SA_SHIRQ, "wcfxs", wc)) {
				printk("wcfxs: Unable to request IRQ %d\n", pdev->irq);
				if (wc->freeregion)
					release_region(wc->ioaddr, 0xff);
				pci_free_consistent(pdev, ZT_MAX_CHUNKSIZE * 2 * 2 * 2 * 4, (void *)wc->writechunk, wc->writedma);
				pci_set_drvdata(pdev, NULL);
				kfree(wc);
				return -EIO;
			}


			if (wcfxs_hardware_init(wc)) {
				free_irq(pdev->irq, wc);
				zt_unregister(&wc->span);
				if (wc->freeregion)
					release_region(wc->ioaddr, 0xff);
				pci_free_consistent(pdev, ZT_MAX_CHUNKSIZE * 2 * 2 * 2 * 4, (void *)wc->writechunk, wc->writedma);
				pci_set_drvdata(pdev, NULL);
				kfree(wc);
				return -EIO;
			}
			/* Enable interrupts */
			wcfxs_enable_interrupts(wc);
			/* Initialize Write/Buffers to all blank data */
			memset((void *)wc->writechunk,0,ZT_MAX_CHUNKSIZE * 2 * 2 * 4);

			/* Start DMA */
			wcfxs_start_dma(wc);

			printk("Found a Wildcard FXS: %s (%d modules)\n", wc->variety, wc->cards);
			res = 0;
		} else
			res = -ENOMEM;
	}
	return res;
}

static void wcfxs_release(struct wcfxs *wc)
{
	zt_unregister(&wc->span);
	if (wc->freeregion)
		release_region(wc->ioaddr, 0xff);
	kfree(wc);
	printk("Freed a Wildcard\n");
}

static void __devexit wcfxs_remove_one(struct pci_dev *pdev)
{
	struct wcfxs *wc = pci_get_drvdata(pdev);
	if (wc) {

		/* Stop any DMA */
		wcfxs_stop_dma(wc);

		/* In case hardware is still there */
		wcfxs_disable_interrupts(wc);
		
		/* Immediately free resources */
		pci_free_consistent(pdev, ZT_MAX_CHUNKSIZE * 2 * 2 * 2 * 4, (void *)wc->writechunk, wc->writedma);
		free_irq(pdev->irq, wc);

		/* Reset PCI chip and registers */
		outb(0x0e, wc->ioaddr + WC_CNTL);

		/* Release span, possibly delayed */
		if (!wc->usecount)
			wcfxs_release(wc);
		else
			wc->dead = 1;
	}
}

static struct pci_device_id wcfxs_pci_tbl[] __devinitdata = {
	{ 0xe159, 0x0001, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &wcfxs },
};

static struct pci_driver wcfxs_driver = {
	name: 	"wcfxs",
	probe: 	wcfxs_init_one,
	remove:	wcfxs_remove_one,
	suspend: NULL,
	resume:	NULL,
	id_table: wcfxs_pci_tbl,
};

static int __init wcfxs_init(void)
{
	int res;
	res = pci_module_init(&wcfxs_driver);
	if (res)
		return -ENODEV;
	return 0;
}

static void __exit wcfxs_cleanup(void)
{
	pci_unregister_driver(&wcfxs_driver);
}

MODULE_PARM(debug, "i");
MODULE_DESCRIPTION("Wildcard S100P Zaptel Driver");
MODULE_AUTHOR("Mark Spencer <markster@linux-support.net>");
#ifdef MODULE_LICENSE
MODULE_LICENSE("GPL");
#endif

module_init(wcfxs_init);
module_exit(wcfxs_cleanup);