Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
View | Details | Raw Unified | Return to bug 302316 | Differences between
and this patch

Collapse All | Expand All

(-)dahdi-linux-2.2.0.2/drivers/dahdi/Kbuild (+3 lines)
Lines 1-10 Link Here
1
obj-$(DAHDI_BUILD_ALL)$(CONFIG_DAHDI)			+= dahdi.o
1
obj-$(DAHDI_BUILD_ALL)$(CONFIG_DAHDI)			+= dahdi.o
2
obj-$(DAHDI_BUILD_ALL)$(CONFIG_DAHDI_OPVXA1200)		+= opvxa1200.o
2
obj-$(DAHDI_BUILD_ALL)$(CONFIG_DAHDI_DUMMY)		+= dahdi_dummy.o
3
obj-$(DAHDI_BUILD_ALL)$(CONFIG_DAHDI_DUMMY)		+= dahdi_dummy.o
4
obj-$(DAHDI_BUILD_ALL)$(CONFIG_DAHDI_WCOPENPCI)		+= wcopenpci.o
3
obj-$(DAHDI_BUILD_ALL)$(CONFIG_DAHDI_DYNAMIC)		+= dahdi_dynamic.o
5
obj-$(DAHDI_BUILD_ALL)$(CONFIG_DAHDI_DYNAMIC)		+= dahdi_dynamic.o
6
obj-$(DAHDI_BUILD_ALL)$(CONFIG_DAHDI_ZAPHFC)		+= zaphfc/
4
obj-$(DAHDI_BUILD_ALL)$(CONFIG_DAHDI_DYNAMIC_LOC)	+= dahdi_dynamic_loc.o
7
obj-$(DAHDI_BUILD_ALL)$(CONFIG_DAHDI_DYNAMIC_LOC)	+= dahdi_dynamic_loc.o
5
obj-$(DAHDI_BUILD_ALL)$(CONFIG_DAHDI_DYNAMIC_ETH)	+= dahdi_dynamic_eth.o
8
obj-$(DAHDI_BUILD_ALL)$(CONFIG_DAHDI_DYNAMIC_ETH)	+= dahdi_dynamic_eth.o
6
obj-$(DAHDI_BUILD_ALL)$(CONFIG_DAHDI_TRANSCODE)		+= dahdi_transcode.o
9
obj-$(DAHDI_BUILD_ALL)$(CONFIG_DAHDI_TRANSCODE)		+= dahdi_transcode.o
7
10
8
obj-$(DAHDI_BUILD_ALL)$(CONFIG_DAHDI_WCT4XXP)		+= wct4xxp/
11
obj-$(DAHDI_BUILD_ALL)$(CONFIG_DAHDI_WCT4XXP)		+= wct4xxp/
9
obj-$(DAHDI_BUILD_ALL)$(CONFIG_DAHDI_WCTC4XXP)		+= wctc4xxp/
12
obj-$(DAHDI_BUILD_ALL)$(CONFIG_DAHDI_WCTC4XXP)		+= wctc4xxp/
10
obj-$(DAHDI_BUILD_ALL)$(CONFIG_DAHDI_WCTDM24XXP)	+= wctdm24xxp/
13
obj-$(DAHDI_BUILD_ALL)$(CONFIG_DAHDI_WCTDM24XXP)	+= wctdm24xxp/
(-)dahdi-linux-2.2.0.2/drivers/dahdi/Kconfig (+76 lines)
Lines 279-281 Link Here
279
	  If unsure, say Y.
279
	  If unsure, say Y.
280
280
281
source "drivers/dahdi/xpp/Kconfig"
281
source "drivers/dahdi/xpp/Kconfig"
282
283
284
config DAHDI_OPVXA1200
285
	tristate "OpenVox A1200P FXS/FXO Interface"
286
	depends on DAHDI && PCI
287
	default DAHDI
288
	---help---
289
	  This driver provides support for the OpenVox A1200P FXS/FXO Interface.
290
291
	  To compile this driver as a module, choose M here: the
292
	  module will be called opvxa1200.
293
294
	  If unsure, say Y.
295
296
config DAHDI_WCOPENPCI
297
	tristate "Voicetronix OpenPCI Interface DAHDI driver"
298
	depends on DAHDI && PCI
299
	default DAHDI
300
	---help---
301
	  This driver provides support for the Voicetronix OpenPCI Interface.
302
303
	  To compile this driver as a module, choose M here: the
304
	  module will be called wcopenpci.
305
306
	  If unsure, say Y.
307
308
config DAHDI_ZAPHFC
309
	tristate "HFC-S DAHDI Driver"
310
	depends on DAHDI && PCI
311
	default DAHDI
312
	---help---
313
	  This driver provides DAHDI support for various HFC-S single-port 
314
          ISDN (BRI) cards.
315
316
	  To compile this driver as a module, choose M here: the
317
	  module will be called zaphfc.
318
319
	  If unsure, say Y.
320
321
config DAHDI_OPVXA1200
322
	tristate "OpenVox A1200P FXS/FXO Interface"
323
	depends on DAHDI && PCI
324
	default DAHDI
325
	---help---
326
	  This driver provides support for the OpenVox A1200P FXS/FXO Interface.
327
328
	  To compile this driver as a module, choose M here: the
329
	  module will be called opvxa1200.
330
331
	  If unsure, say Y.
332
333
config DAHDI_WCOPENPCI
334
	tristate "Voicetronix OpenPCI Interface DAHDI driver"
335
	depends on DAHDI && PCI
336
	default DAHDI
337
	---help---
338
	  This driver provides support for the Voicetronix OpenPCI Interface.
339
340
	  To compile this driver as a module, choose M here: the
341
	  module will be called wcopenpci.
342
343
	  If unsure, say Y.
344
345
config DAHDI_ZAPHFC
346
	tristate "HFC-S DAHDI Driver"
347
	depends on DAHDI && PCI
348
	default DAHDI
349
	---help---
350
	  This driver provides DAHDI support for various HFC-S single-port 
351
          ISDN (BRI) cards.
352
353
	  To compile this driver as a module, choose M here: the
354
	  module will be called zaphfc.
355
356
	  If unsure, say Y.
357
(-)dahdi-linux-2.2.0.2/drivers/dahdi/opvxa1200.c (+3018 lines)
Line 0 Link Here
1
/*
2
 * OpenVox A1200P FXS/FXO Interface Driver for DAHDI Telephony interface
3
 *
4
 * Modify from wctdm.c by MiaoLin<miaolin@openvox.com.cn>
5
 *
6
 * All rights reserved.
7
 *
8
 * This program is free software; you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation; either version 2 of the License, or
11
 * (at your option) any later version.
12
 * 
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 * 
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
21
 *
22
 */
23
24
/* Rev histroy
25
 *
26
 * Rev 0.10 initial version	
27
 * Rev 0.11 
28
 * 	fixed the led light on/off bug.
29
 * 	modify some wctdm print to opvxa1200
30
 * 	support firmware version 1.2, faster i/o operation, and better LED control.
31
 * 
32
 * Rev 0.12 patched to support new pci id 0x8519
33
 * Rev 0.13 patched to remove the warning during compile under kernel 2.6.22 
34
 * Rev 0.14 patched to remove the bug for ZAP_IRQ_SHARED , 3/9/2007 
35
 * Rev 0.15 patched to support new pci ID 0X9532 by james.zhu, 23/10/2007
36
 * Rev 0.16 support new pci id 0x9559 by Miao Lin 21/3/2008
37
 * Rev 0.17 
38
 *	patched a few bugs, 
39
 *	add hwgain support.
40
 *	fixed A800P version check
41
 * Rev 1.4.9.2 
42
 *		Only generate 8 channels for A800P
43
 * 		Version number synced to zaptel distribution.
44
 * Rev 1.4.9.2.a
45
 *		Fixed freeregion.
46
 * 		
47
 * Rev 1.4.9.2.b
48
 *    Add cid before first ring support.
49
 *    New Paremeters:
50
 *          	cidbeforering : set to 1 will cause the card enable cidbeforering function. default 0
51
 * 		cidbuflen : length of cid buffer, in msec, default 3000 msec.
52
 *              cidtimeout : time out of a ring, default 6000msec
53
 *   	User must set cidstart=polarity in zapata.conf to use with this feature
54
 * 		cidsignalling = signalling format send before 1st ring. most likely dtmf.
55
 * 
56
 * Rev 1.4.9.2.c
57
 * 	add driver parameter cidtimeout.
58
 * 
59
 * Rev 1.4.9.2.d 
60
 *  	add debug stuff to test fxs power alarm
61
 *  
62
 * Rev 1.4.11
63
 *  	Support enhanced full scale tx/rx for FXO required by europe standard (Register 30, acim) (module parm fxofullscale)
64
 *  
65
 * Rev 1.4.12 2008/10/17
66
 *      Fixed bug cause FXS module report fake power alarm.
67
 *      Power alarm debug stuff removed.
68
 * 
69
 * Rev 2.0 DAHDI 2008/10/17
70
 *
71
 * Rev 2.0.1 add new pci id 0x9599
72
 * Re 2.0.2 12/01/2009  
73
       add fixedtimepolarity: set time(ms) when send polarity after 1st ring happen. 
74
 *				Sometimes the dtmf cid is sent just after first ring off, and the system do not have 
75
 *				enough time to start detect 1st dtmf.
76
 *				0 means send polarity at the end of 1st ring.
77
 *				x means send ploarity after x ms of 1st ring begin.
78
 * 
79
 * Rev 2.0.3 12/01/2009 
80
 *        Add touch_softlockup_watchdog() in wctdm_hardware_init, to avoid cpu softlockup system message for FXS.
81
 *
82
 *
83
 * Rev 1.4.12.4  17/04/2009 James.zhu
84
 *       Changed wctdm_voicedaa_check_hook() to detect FXO battery and solved the problem with dial(dahdi/go/XXXXXXXXXX)
85
 *       add alarm detection for FXO
86
 *
87
 * Rev 1.4.12.5 01/10/2009 james.zhu
88
 *       Add jiffies for 5 second in wctdm_hardware_init
89
 *
90
 *
91
 */ 
92
93
#include <linux/kernel.h>
94
#include <linux/errno.h>
95
#include <linux/module.h>
96
#include <linux/init.h>
97
#include <linux/errno.h>
98
#include <linux/pci.h>
99
#include <linux/sched.h>
100
#include <linux/interrupt.h>
101
#include <linux/moduleparam.h>
102
#include <asm/io.h>
103
#include "proslic.h"
104
   
105
/* MiaoLin debug start */
106
#include <linux/string.h>
107
#include <asm/uaccess.h> 	/* get_fs(), set_fs(), KERNEL_DS */
108
#include <linux/file.h> 	/* fput() */
109
/* MiaoLin debug end */
110
  
111
112
/*
113
 *  Define for audio vs. register based ring detection
114
 *  
115
 */
116
/* #define AUDIO_RINGCHECK  */
117
118
/*
119
  Experimental max loop current limit for the proslic
120
  Loop current limit is from 20 mA to 41 mA in steps of 3
121
  (according to datasheet)
122
  So set the value below to:
123
  0x00 : 20mA (default)
124
  0x01 : 23mA
125
  0x02 : 26mA
126
  0x03 : 29mA
127
  0x04 : 32mA
128
  0x05 : 35mA
129
  0x06 : 37mA
130
  0x07 : 41mA
131
*/
132
static int loopcurrent = 20;
133
134
static int reversepolarity = 0;
135
136
static alpha  indirect_regs[] =
137
{
138
{0,255,"DTMF_ROW_0_PEAK",0x55C2},
139
{1,255,"DTMF_ROW_1_PEAK",0x51E6},
140
{2,255,"DTMF_ROW2_PEAK",0x4B85},
141
{3,255,"DTMF_ROW3_PEAK",0x4937},
142
{4,255,"DTMF_COL1_PEAK",0x3333},
143
{5,255,"DTMF_FWD_TWIST",0x0202},
144
{6,255,"DTMF_RVS_TWIST",0x0202},
145
{7,255,"DTMF_ROW_RATIO_TRES",0x0198},
146
{8,255,"DTMF_COL_RATIO_TRES",0x0198},
147
{9,255,"DTMF_ROW_2ND_ARM",0x0611},
148
{10,255,"DTMF_COL_2ND_ARM",0x0202},
149
{11,255,"DTMF_PWR_MIN_TRES",0x00E5},
150
{12,255,"DTMF_OT_LIM_TRES",0x0A1C},
151
{13,0,"OSC1_COEF",0x7B30},
152
{14,1,"OSC1X",0x0063},
153
{15,2,"OSC1Y",0x0000},
154
{16,3,"OSC2_COEF",0x7870},
155
{17,4,"OSC2X",0x007D},
156
{18,5,"OSC2Y",0x0000},
157
{19,6,"RING_V_OFF",0x0000},
158
{20,7,"RING_OSC",0x7EF0},
159
{21,8,"RING_X",0x0160},
160
{22,9,"RING_Y",0x0000},
161
{23,255,"PULSE_ENVEL",0x2000},
162
{24,255,"PULSE_X",0x2000},
163
{25,255,"PULSE_Y",0x0000},
164
//{26,13,"RECV_DIGITAL_GAIN",0x4000},	// playback volume set lower
165
{26,13,"RECV_DIGITAL_GAIN",0x2000},	// playback volume set lower
166
{27,14,"XMIT_DIGITAL_GAIN",0x4000},
167
//{27,14,"XMIT_DIGITAL_GAIN",0x2000},
168
{28,15,"LOOP_CLOSE_TRES",0x1000},
169
{29,16,"RING_TRIP_TRES",0x3600},
170
{30,17,"COMMON_MIN_TRES",0x1000},
171
{31,18,"COMMON_MAX_TRES",0x0200},
172
{32,19,"PWR_ALARM_Q1Q2",0x07C0},
173
{33,20,"PWR_ALARM_Q3Q4",0x2600},
174
{34,21,"PWR_ALARM_Q5Q6",0x1B80},
175
{35,22,"LOOP_CLOSURE_FILTER",0x8000},
176
{36,23,"RING_TRIP_FILTER",0x0320},
177
{37,24,"TERM_LP_POLE_Q1Q2",0x008C},
178
{38,25,"TERM_LP_POLE_Q3Q4",0x0100},
179
{39,26,"TERM_LP_POLE_Q5Q6",0x0010},
180
{40,27,"CM_BIAS_RINGING",0x0C00},
181
{41,64,"DCDC_MIN_V",0x0C00},
182
{42,255,"DCDC_XTRA",0x1000},
183
{43,66,"LOOP_CLOSE_TRES_LOW",0x1000},
184
};
185
186
187
#include <dahdi/kernel.h>
188
#include <dahdi/wctdm_user.h>
189
190
#include "fxo_modes.h"
191
192
#define NUM_FXO_REGS 60
193
194
#define WC_MAX_IFACES 128
195
196
#define WC_OFFSET	4	/* Offset between transmit and receive, in bytes. */
197
#define WC_SYNCFLAG	0xca1ef1ac
198
199
#define WC_CNTL    	0x00
200
#define WC_OPER		0x01
201
#define WC_AUXC    	0x02
202
#define WC_AUXD    	0x03
203
#define WC_MASK0   	0x04
204
#define WC_MASK1   	0x05
205
#define WC_INTSTAT 	0x06
206
#define WC_AUXR		0x07
207
208
#define WC_DMAWS	0x08
209
#define WC_DMAWI	0x0c
210
#define WC_DMAWE	0x10
211
#define WC_DMARS	0x18
212
#define WC_DMARI	0x1c
213
#define WC_DMARE	0x20
214
215
#define WC_AUXFUNC	0x2b
216
#define WC_SERCTL	0x2d
217
#define WC_FSCDELAY	0x2f
218
219
#define WC_REGBASE	0xc0
220
221
#define WC_VER		0x0
222
#define WC_CS		0x1
223
#define WC_SPICTRL	0x2
224
#define WC_SPIDATA	0x3
225
226
#define BIT_SPI_BYHW 	(1 << 0)
227
#define BIT_SPI_BUSY    (1 << 1)	// 0=can read/write spi, 1=spi working.
228
#define BIT_SPI_START	(1 << 2)
229
230
231
#define BIT_LED_CLK     (1 << 0)	// MiaoLin add to control the led. 
232
#define BIT_LED_DATA    (1 << 1)	// MiaoLin add to control the led.
233
234
#define BIT_CS		(1 << 2)
235
#define BIT_SCLK	(1 << 3)
236
#define BIT_SDI		(1 << 4)
237
#define BIT_SDO		(1 << 5)
238
239
#define FLAG_EMPTY	0
240
#define FLAG_WRITE	1
241
#define FLAG_READ	2
242
#define DEFAULT_RING_DEBOUNCE		64		/* Ringer Debounce (64 ms) */
243
#define POLARITY_DEBOUNCE 	64  	/* Polarity debounce (64 ms) */
244
#define OHT_TIMER		6000	/* How long after RING to retain OHT */
245
246
#define FLAG_3215	(1 << 0)
247
#define FLAG_A800	(1 << 7)
248
249
#define MAX_NUM_CARDS 12
250
#define NUM_CARDS 12
251
#define NUM_FLAG  4	/* number of flag channels. */
252
253
254
enum cid_hook_state {
255
	CID_STATE_IDLE = 0,
256
	CID_STATE_RING_ON,
257
	CID_STATE_RING_OFF,
258
	CID_STATE_WAIT_RING_FINISH
259
};
260
261
/* if you want to record the last 8 sec voice before the driver unload, uncomment it and rebuild. */
262
/* #define TEST_LOG_INCOME_VOICE */
263
#define voc_buffer_size (8000*8)
264
265
266
#define MAX_ALARMS 10
267
268
#define MOD_TYPE_FXS	0
269
#define MOD_TYPE_FXO	1
270
271
#define MINPEGTIME	10 * 8		/* 30 ms peak to peak gets us no more than 100 Hz */
272
#define PEGTIME		50 * 8		/* 50ms peak to peak gets us rings of 10 Hz or more */
273
#define PEGCOUNT	5		/* 5 cycles of pegging means RING */
274
275
#define NUM_CAL_REGS 12
276
277
struct calregs {
278
	unsigned char vals[NUM_CAL_REGS];
279
};
280
281
enum proslic_power_warn {
282
	PROSLIC_POWER_UNKNOWN = 0,
283
	PROSLIC_POWER_ON,
284
	PROSLIC_POWER_WARNED,
285
};
286
287
enum battery_state {
288
	BATTERY_UNKNOWN = 0,
289
	BATTERY_PRESENT,
290
	BATTERY_LOST,
291
};
292
struct wctdm {
293
	struct pci_dev *dev;
294
	char *variety;
295
	struct dahdi_span span;
296
	unsigned char ios;
297
	int usecount;
298
	unsigned int intcount;
299
	int dead;
300
	int pos;
301
	int flags[MAX_NUM_CARDS];
302
	int freeregion;
303
	int alt;
304
	int curcard;
305
	int cardflag;		/* Bit-map of present cards */
306
	enum proslic_power_warn proslic_power;
307
	spinlock_t lock;
308
309
	union {
310
		struct fxo {
311
#ifdef AUDIO_RINGCHECK
312
			unsigned int pegtimer;
313
			int pegcount;
314
			int peg;
315
			int ring;
316
#else			
317
			int wasringing;
318
			int lastrdtx;
319
#endif			
320
			int ringdebounce;
321
			int offhook;
322
		    unsigned int battdebounce;
323
			unsigned int battalarm;
324
			enum battery_state battery;
325
		        int lastpol;
326
		        int polarity;
327
		        int polaritydebounce;
328
		} fxo;
329
		struct fxs {
330
			int oldrxhook;
331
			int debouncehook;
332
			int lastrxhook;
333
			int debounce;
334
			int ohttimer;
335
			int idletxhookstate;		/* IDLE changing hook state */
336
			int lasttxhook;
337
			int palarms;
338
			struct calregs calregs;
339
		} fxs;
340
	} mod[MAX_NUM_CARDS];
341
342
	/* Receive hook state and debouncing */
343
	int modtype[MAX_NUM_CARDS];
344
	unsigned char reg0shadow[MAX_NUM_CARDS];
345
	unsigned char reg1shadow[MAX_NUM_CARDS];
346
347
	unsigned long ioaddr;
348
	unsigned long mem_region;	/* 32 bit Region allocated to tiger320 */
349
	unsigned long mem_len;		/* Length of 32 bit region */
350
	volatile unsigned long mem32;	/* Virtual representation of 32 bit memory area */
351
	
352
	dma_addr_t 	readdma;
353
	dma_addr_t	writedma;
354
	volatile unsigned char *writechunk;					/* Double-word aligned write memory */
355
	volatile unsigned char *readchunk;					/* Double-word aligned read memory */
356
	/*struct dahdi_chan chans[MAX_NUM_CARDS];*/
357
	struct dahdi_chan _chans[NUM_CARDS];
358
	struct dahdi_chan *chans[NUM_CARDS];
359
360
361
#ifdef TEST_LOG_INCOME_VOICE	
362
	char * voc_buf[MAX_NUM_CARDS + NUM_FLAG];
363
	int voc_ptr[MAX_NUM_CARDS + NUM_FLAG];
364
#endif
365
	int lastchan;
366
	unsigned short ledstate;
367
	unsigned char fwversion;
368
	int max_cards;
369
	char *card_name;
370
	
371
	char *cid_history_buf[MAX_NUM_CARDS];
372
	int	 cid_history_ptr[MAX_NUM_CARDS];
373
	int  cid_history_clone_cnt[MAX_NUM_CARDS];
374
	enum cid_hook_state cid_state[MAX_NUM_CARDS];
375
   int 	cid_ring_on_time[MAX_NUM_CARDS];
376
};
377
378
static char* A1200P_Name = "A1200P";
379
static char* A800P_Name  = "A800P";
380
381
struct wctdm_desc {
382
	char *name;
383
	int flags;
384
};
385
386
static struct wctdm_desc wctdme = { "OpenVox A1200P/A800P", 0 };
387
static int acim2tiss[16] = { 0x0, 0x1, 0x4, 0x5, 0x7, 0x0, 0x0, 0x6, 0x0, 0x0, 0x0, 0x2, 0x0, 0x3 };
388
389
static struct wctdm *ifaces[WC_MAX_IFACES];
390
391
static void wctdm_release(struct wctdm *wc);
392
393
static unsigned int battdebounce;
394
static unsigned int battalarm;
395
static unsigned int battthresh;
396
static int ringdebounce = DEFAULT_RING_DEBOUNCE;
397
static int fwringdetect = 0;
398
static int debug = 0;
399
static int robust = 0;
400
static int timingonly = 0;
401
static int lowpower = 0;
402
static int boostringer = 0;
403
static int fastringer = 0;
404
static int _opermode = 0;
405
static char *opermode = "FCC";
406
static int fxshonormode = 0;
407
static int alawoverride = 0;
408
static int fastpickup = 0;
409
static int fxotxgain = 0;
410
static int fxorxgain = 0;
411
static int fxstxgain = 0;
412
static int fxsrxgain = 0;
413
/* special h/w control command */
414
static int spibyhw = 1;
415
static int usememio = 1;
416
static int cidbeforering = 0;
417
static int cidbuflen = 3000;	/* in msec, default 3000 */
418
static int cidtimeout = 6*1000;	/* in msec, default 6000 */
419
static int fxofullscale = 0;	/* fxo full scale tx/rx, register 30, acim */
420
static int fixedtimepolarity=0;	/* time delay in ms when send polarity after rise edge of 1st ring.*/
421
422
static int wctdm_init_proslic(struct wctdm *wc, int card, int fast , int manual, int sane);
423
424
static void wctdm_set_led(struct wctdm* wc, int card, int onoff)
425
{
426
	int i;
427
	unsigned char c;
428
	
429
	wc->ledstate &= ~(0x01<<card);
430
	wc->ledstate |= (onoff<<card);
431
	c = (inb(wc->ioaddr + WC_AUXD)&~BIT_LED_CLK)|BIT_LED_DATA;
432
	outb( c,  wc->ioaddr + WC_AUXD);
433
	for(i=MAX_NUM_CARDS-1; i>=0; i--)
434
	{
435
		if(wc->ledstate & (0x0001<<i))
436
			if(wc->fwversion == 0x11)
437
				c &= ~BIT_LED_DATA;
438
			else
439
				c |= BIT_LED_DATA;
440
		else
441
			if(wc->fwversion == 0x11)
442
				c |= BIT_LED_DATA;
443
			else
444
				c &= ~BIT_LED_DATA;
445
			
446
		outb( c,  wc->ioaddr + WC_AUXD);
447
		outb( c|BIT_LED_CLK,  wc->ioaddr + WC_AUXD);
448
		outb( (c&~BIT_LED_CLK)|BIT_LED_DATA,  wc->ioaddr + WC_AUXD);
449
	}	
450
}
451
 
452
453
static inline void wctdm_transmitprep(struct wctdm *wc, unsigned char ints)
454
{
455
	int x, y, chan_offset, pos;
456
	volatile unsigned char *txbuf;
457
	
458
	if (ints & /*0x01*/ 0x04) 
459
		/* Write is at interrupt address.  Start writing from normal offset */
460
		txbuf = wc->writechunk;
461
	else 
462
		txbuf = wc->writechunk + DAHDI_CHUNKSIZE * (MAX_NUM_CARDS+NUM_FLAG);
463
		
464
	/* Calculate Transmission */
465
	dahdi_transmit(&wc->span);
466
	
467
	if(wc->lastchan == -1)	// not in sync.
468
		return;
469
	
470
	chan_offset = (wc->lastchan*4 + 4 ) % (MAX_NUM_CARDS+NUM_FLAG);
471
472
	for (y=0;y<DAHDI_CHUNKSIZE;y++) {
473
#ifdef __BIG_ENDIAN
474
	// operation pending...
475
#else
476
		for (x=0;x<(MAX_NUM_CARDS+NUM_FLAG);x++) {
477
			pos = y * (MAX_NUM_CARDS+NUM_FLAG) + ((x + chan_offset + MAX_NUM_CARDS+NUM_FLAG - WC_OFFSET)&0x0f);
478
			if(x<wc->max_cards/*MAX_NUM_CARDS*/)
479
				txbuf[pos] = wc->chans[x]->writechunk[y]; 
480
			else
481
				txbuf[pos] = 0; 
482
		}
483
#endif
484
	}
485
}
486
487
488
#ifdef AUDIO_RINGCHECK
489
static inline void ring_check(struct wctdm *wc, int card)
490
{
491
	int x;
492
	short sample;
493
	if (wc->modtype[card] != MOD_TYPE_FXO)
494
		return;
495
	wc->mod[card].fxo.pegtimer += DAHDI_CHUNKSIZE;
496
	for (x=0;x<DAHDI_CHUNKSIZE;x++) {
497
		/* Look for pegging to indicate ringing */
498
		sample = DAHDI_XLAW(wc->chans[card].readchunk[x], (&(wc->chans[card])));
499
		if ((sample > 10000) && (wc->mod[card].fxo.peg != 1)) {
500
			if (debug > 1) printk(KERN_DEBUG "High peg!\n");
501
			if ((wc->mod[card].fxo.pegtimer < PEGTIME) && (wc->mod[card].fxo.pegtimer > MINPEGTIME))
502
				wc->mod[card].fxo.pegcount++;
503
			wc->mod[card].fxo.pegtimer = 0;
504
			wc->mod[card].fxo.peg = 1;
505
		} else if ((sample < -10000) && (wc->mod[card].fxo.peg != -1)) {
506
			if (debug > 1) printk(KERN_DEBUG "Low peg!\n");
507
			if ((wc->mod[card].fxo.pegtimer < (PEGTIME >> 2)) && (wc->mod[card].fxo.pegtimer > (MINPEGTIME >> 2)))
508
				wc->mod[card].fxo.pegcount++;
509
			wc->mod[card].fxo.pegtimer = 0;
510
			wc->mod[card].fxo.peg = -1;
511
		}
512
	}
513
	if (wc->mod[card].fxo.pegtimer > PEGTIME) {
514
		/* Reset pegcount if our timer expires */
515
		wc->mod[card].fxo.pegcount = 0;
516
	}
517
	/* Decrement debouncer if appropriate */
518
	if (wc->mod[card].fxo.ringdebounce)
519
		wc->mod[card].fxo.ringdebounce--;
520
	if (!wc->mod[card].fxo.offhook && !wc->mod[card].fxo.ringdebounce) {
521
		if (!wc->mod[card].fxo.ring && (wc->mod[card].fxo.pegcount > PEGCOUNT)) {
522
			/* It's ringing */
523
			if (debug)
524
				printk(KERN_DEBUG "RING on %d/%d!\n", wc->span.spanno, card + 1);
525
			if (!wc->mod[card].fxo.offhook)
526
				dahdi_hooksig(&wc->chans[card], DAHDI_RXSIG_RING);
527
			wc->mod[card].fxo.ring = 1;
528
		}
529
		if (wc->mod[card].fxo.ring && !wc->mod[card].fxo.pegcount) {
530
			/* No more ring */
531
			if (debug)
532
				printk(KERN_DEBUG "NO RING on %d/%d!\n", wc->span.spanno, card + 1);
533
			dahdi_hooksig(&wc->chans[card], DAHDI_RXSIG_OFFHOOK);
534
			wc->mod[card].fxo.ring = 0;
535
		}
536
	}
537
}
538
#endif
539
540
541
static inline void wctdm_receiveprep(struct wctdm *wc, unsigned char ints)
542
{
543
	volatile unsigned char *rxbuf;
544
	int x, y, chan_offset;
545
546
547
	if (ints & 0x08/*0x04*/)
548
		/* Read is at interrupt address.  Valid data is available at normal offset */
549
		rxbuf = wc->readchunk;
550
	else
551
		rxbuf = wc->readchunk + DAHDI_CHUNKSIZE * (MAX_NUM_CARDS+NUM_FLAG);
552
553
	for(x=0; x<4; x++)
554
		if(  *(int*)(rxbuf+x*4) == WC_SYNCFLAG)
555
			break;
556
	if(x==4)
557
	{
558
		printk("buffer sync misseed!\n");
559
		wc->lastchan = -1;
560
		return;
561
	}
562
	else if(wc->lastchan != x)
563
	{
564
		printk("buffer re-sync occur from %d to %d\n", wc->lastchan, x);
565
		wc->lastchan = x;
566
	}
567
	chan_offset = (wc->lastchan*4 + 4 ) % (MAX_NUM_CARDS+NUM_FLAG);
568
569
	for (x=0;x<DAHDI_CHUNKSIZE;x++) {
570
#ifdef __BIG_ENDIAN
571
	// operation pending...
572
#else
573
		for (y=0;y<wc->max_cards/*MAX_NUM_CARDS*/;y++) { 
574
			if (wc->cardflag & (1 << y))
575
				wc->chans[y]->readchunk[x] = rxbuf[(MAX_NUM_CARDS+NUM_FLAG) * x + ((y + chan_offset ) & 0x0f)];
576
#ifdef TEST_LOG_INCOME_VOICE
577
			wc->voc_buf[y][wc->voc_ptr[y]] = rxbuf[(MAX_NUM_CARDS+NUM_FLAG) * x + ((y + chan_offset) & 0x0f)];
578
			wc->voc_ptr[y]++;
579
			if(wc->voc_ptr[y] >= voc_buffer_size)
580
				wc->voc_ptr[y] = 0;
581
#endif		
582
		}
583
#endif
584
	}
585
	
586
	if(cidbeforering)
587
	{
588
		for(x=0; x<wc->max_cards; x++)
589
		{
590
			if (wc->modtype[wc->chans[x]->chanpos - 1] == MOD_TYPE_FXO)
591
				if(wc->mod[wc->chans[x]->chanpos - 1].fxo.offhook == 0)
592
				{
593
					/*unsigned int *p_readchunk, *p_cid_history;
594
					
595
					p_readchunk = (unsigned int*)wc->chans[x].readchunk;
596
					p_cid_history = (unsigned int*)(wc->cid_history_buf[x] + wc->cid_history_ptr[x]);*/
597
					
598
					if(wc->cid_state[x] == CID_STATE_IDLE)	/* we need copy data to the cid voice buffer */
599
					{
600
						memcpy(wc->cid_history_buf[x] + wc->cid_history_ptr[x], wc->chans[x]->readchunk, DAHDI_CHUNKSIZE);
601
						wc->cid_history_ptr[x] = (wc->cid_history_ptr[x] + DAHDI_CHUNKSIZE)%(cidbuflen * DAHDI_MAX_CHUNKSIZE);
602
					}
603
					else if (wc->cid_state[x] == CID_STATE_RING_ON)
604
						wc->cid_history_clone_cnt[x] = cidbuflen;
605
					else if (wc->cid_state[x] == CID_STATE_RING_OFF)
606
					{ 
607
						if(wc->cid_history_clone_cnt[x])
608
						{	
609
							memcpy(wc->chans[x]->readchunk, wc->cid_history_buf[x] + wc->cid_history_ptr[x], DAHDI_MAX_CHUNKSIZE);
610
							wc->cid_history_clone_cnt[x]--;
611
							wc->cid_history_ptr[x] = (wc->cid_history_ptr[x] + DAHDI_MAX_CHUNKSIZE)%(cidbuflen * DAHDI_MAX_CHUNKSIZE);
612
						}
613
						else
614
						{
615
							wc->cid_state[x] = CID_STATE_WAIT_RING_FINISH;
616
							wc->cid_history_clone_cnt[x] = cidtimeout; /* wait 6 sec, if no ring, return to idle */
617
						}
618
					}
619
					else if(wc->cid_state[x] == CID_STATE_WAIT_RING_FINISH)
620
					{
621
						if(wc->cid_history_clone_cnt[x] > 0)
622
							wc->cid_history_clone_cnt[x]--;
623
						else
624
						{
625
							wc->cid_state[x] = CID_STATE_IDLE;
626
							wc->cid_history_ptr[x] = 0;
627
							wc->cid_history_clone_cnt[x] = 0;
628
						}
629
					}
630
				}
631
		}		
632
	}
633
	
634
#ifdef AUDIO_RINGCHECK
635
	for (x=0;x<wc->max_cards;x++)
636
		ring_check(wc, x);
637
#endif		
638
	/* XXX We're wasting 8 taps.  We should get closer :( */
639
	for (x = 0; x < wc->max_cards/*MAX_NUM_CARDS*/; x++) {
640
		if (wc->cardflag & (1 << x))
641
			dahdi_ec_chunk(wc->chans[x], wc->chans[x]->readchunk, wc->chans[x]->writechunk);
642
	}
643
	dahdi_receive(&wc->span);
644
}
645
646
static void wctdm_stop_dma(struct wctdm *wc);
647
static void wctdm_reset_tdm(struct wctdm *wc);
648
static void wctdm_restart_dma(struct wctdm *wc);
649
650
651
static unsigned char __wctdm_getcreg(struct wctdm *wc, unsigned char reg);
652
static void __wctdm_setcreg(struct wctdm *wc, unsigned char reg, unsigned char val);
653
654
655
static inline void __write_8bits(struct wctdm *wc, unsigned char bits)
656
{
657
	if(spibyhw == 0)
658
	{
659
		int x;
660
		/* Drop chip select */
661
		wc->ios |= BIT_SCLK;
662
		outb(wc->ios, wc->ioaddr + WC_AUXD);
663
		wc->ios &= ~BIT_CS;
664
		outb(wc->ios, wc->ioaddr + WC_AUXD);
665
		for (x=0;x<8;x++) {
666
			/* Send out each bit, MSB first, drop SCLK as we do so */
667
			if (bits & 0x80)
668
				wc->ios |= BIT_SDI;
669
			else
670
				wc->ios &= ~BIT_SDI;
671
			wc->ios &= ~BIT_SCLK;
672
			outb(wc->ios, wc->ioaddr + WC_AUXD);
673
			/* Now raise SCLK high again and repeat */
674
			wc->ios |= BIT_SCLK;
675
			outb(wc->ios, wc->ioaddr + WC_AUXD);
676
			bits <<= 1;
677
		}
678
		/* Finally raise CS back high again */
679
		wc->ios |= BIT_CS;
680
		outb(wc->ios, wc->ioaddr + WC_AUXD);
681
	}
682
	else
683
	{
684
		__wctdm_setcreg(wc, WC_SPIDATA, bits);
685
		__wctdm_setcreg(wc, WC_SPICTRL, BIT_SPI_BYHW | BIT_SPI_START);
686
		while ((__wctdm_getcreg(wc, WC_SPICTRL) & BIT_SPI_BUSY) != 0);
687
		__wctdm_setcreg(wc, WC_SPICTRL, BIT_SPI_BYHW);
688
	}
689
}
690
691
692
static inline void __reset_spi(struct wctdm *wc)
693
{
694
	__wctdm_setcreg(wc, WC_SPICTRL, 0);
695
	
696
	/* Drop chip select and clock once and raise and clock once */
697
	wc->ios |= BIT_SCLK;
698
	outb(wc->ios, wc->ioaddr + WC_AUXD);
699
	wc->ios &= ~BIT_CS;
700
	outb(wc->ios, wc->ioaddr + WC_AUXD);
701
	wc->ios |= BIT_SDI;
702
	wc->ios &= ~BIT_SCLK;
703
	outb(wc->ios, wc->ioaddr + WC_AUXD);
704
	/* Now raise SCLK high again and repeat */
705
	wc->ios |= BIT_SCLK;
706
	outb(wc->ios, wc->ioaddr + WC_AUXD);
707
	/* Finally raise CS back high again */
708
	wc->ios |= BIT_CS;
709
	outb(wc->ios, wc->ioaddr + WC_AUXD);
710
	/* Clock again */
711
	wc->ios &= ~BIT_SCLK;
712
	outb(wc->ios, wc->ioaddr + WC_AUXD);
713
	/* Now raise SCLK high again and repeat */
714
	wc->ios |= BIT_SCLK;
715
	outb(wc->ios, wc->ioaddr + WC_AUXD);
716
	
717
	__wctdm_setcreg(wc, WC_SPICTRL, spibyhw);
718
719
}
720
721
static inline unsigned char __read_8bits(struct wctdm *wc)
722
{
723
	unsigned char res=0, c;
724
	int x;
725
	if(spibyhw == 0)
726
	{
727
		wc->ios &= ~BIT_CS;
728
		outb(wc->ios, wc->ioaddr + WC_AUXD);
729
		/* Drop chip select */
730
		wc->ios &= ~BIT_CS;
731
		outb(wc->ios, wc->ioaddr + WC_AUXD);
732
		for (x=0;x<8;x++) {
733
			res <<= 1;
734
			/* Get SCLK */
735
			wc->ios &= ~BIT_SCLK;
736
			outb(wc->ios, wc->ioaddr + WC_AUXD);
737
			/* Read back the value */
738
			c = inb(wc->ioaddr + WC_AUXR);
739
			if (c & BIT_SDO)
740
				res |= 1;
741
			/* Now raise SCLK high again */
742
			wc->ios |= BIT_SCLK;
743
			outb(wc->ios, wc->ioaddr + WC_AUXD);
744
		}
745
		/* Finally raise CS back high again */
746
		wc->ios |= BIT_CS;
747
		outb(wc->ios, wc->ioaddr + WC_AUXD);
748
		wc->ios &= ~BIT_SCLK;
749
		outb(wc->ios, wc->ioaddr + WC_AUXD);
750
	}
751
	else
752
	{
753
		__wctdm_setcreg(wc, WC_SPICTRL, BIT_SPI_BYHW | BIT_SPI_START);
754
		while ((__wctdm_getcreg(wc, WC_SPICTRL) & BIT_SPI_BUSY) != 0);
755
		res = __wctdm_getcreg(wc, WC_SPIDATA);
756
		__wctdm_setcreg(wc, WC_SPICTRL, BIT_SPI_BYHW);
757
	}
758
	
759
	/* And return our result */
760
	return res;
761
}
762
763
static void __wctdm_setcreg_mem(struct wctdm *wc, unsigned char reg, unsigned char val)
764
{
765
	unsigned int *p = (unsigned int*)(wc->mem32 + WC_REGBASE + ((reg & 0xf) << 2));
766
	*p = val;
767
}
768
769
static unsigned char __wctdm_getcreg_mem(struct wctdm *wc, unsigned char reg)
770
{
771
	unsigned int *p = (unsigned int*)(wc->mem32 + WC_REGBASE + ((reg & 0xf) << 2));
772
	return (*p)&0x00ff;
773
}
774
775
776
static void __wctdm_setcreg(struct wctdm *wc, unsigned char reg, unsigned char val)
777
{
778
	if(usememio)
779
		__wctdm_setcreg_mem(wc, reg, val);
780
	else
781
		outb(val, wc->ioaddr + WC_REGBASE + ((reg & 0xf) << 2));
782
}
783
784
static unsigned char __wctdm_getcreg(struct wctdm *wc, unsigned char reg)
785
{
786
	if(usememio)
787
		return __wctdm_getcreg_mem(wc, reg);
788
	else
789
		return inb(wc->ioaddr + WC_REGBASE + ((reg & 0xf) << 2));
790
}
791
792
static inline void __wctdm_setcard(struct wctdm *wc, int card)
793
{
794
	if (wc->curcard != card) {
795
		__wctdm_setcreg(wc, WC_CS, card);
796
		wc->curcard = card;
797
		//printk("Select card %d\n", card);
798
	}
799
}
800
801
static void __wctdm_setreg(struct wctdm *wc, int card, unsigned char reg, unsigned char value)
802
{
803
	__wctdm_setcard(wc, card);
804
	if (wc->modtype[card] == MOD_TYPE_FXO) {
805
		__write_8bits(wc, 0x20);
806
		__write_8bits(wc, reg & 0x7f);
807
	} else {
808
		__write_8bits(wc, reg & 0x7f);
809
	}
810
	__write_8bits(wc, value);
811
}
812
813
static void wctdm_setreg(struct wctdm *wc, int card, unsigned char reg, unsigned char value)
814
{
815
	unsigned long flags;
816
	spin_lock_irqsave(&wc->lock, flags);
817
	__wctdm_setreg(wc, card, reg, value);
818
	spin_unlock_irqrestore(&wc->lock, flags);
819
}
820
821
static unsigned char __wctdm_getreg(struct wctdm *wc, int card, unsigned char reg)
822
{
823
	__wctdm_setcard(wc, card);
824
	if (wc->modtype[card] == MOD_TYPE_FXO) {
825
		__write_8bits(wc, 0x60);
826
		__write_8bits(wc, reg & 0x7f);
827
	} else {
828
		__write_8bits(wc, reg | 0x80);
829
	}
830
	return __read_8bits(wc);
831
}
832
833
static inline void reset_spi(struct wctdm *wc, int card)
834
{
835
	unsigned long flags;
836
	spin_lock_irqsave(&wc->lock, flags);
837
	__wctdm_setcard(wc, card);
838
	__reset_spi(wc);
839
	__reset_spi(wc);
840
	spin_unlock_irqrestore(&wc->lock, flags);
841
}
842
843
static unsigned char wctdm_getreg(struct wctdm *wc, int card, unsigned char reg)
844
{
845
	unsigned long flags;
846
	unsigned char res;
847
	spin_lock_irqsave(&wc->lock, flags);
848
	res = __wctdm_getreg(wc, card, reg);
849
	spin_unlock_irqrestore(&wc->lock, flags);
850
	return res;
851
}
852
853
static int __wait_access(struct wctdm *wc, int card)
854
{
855
    unsigned char data = 0;
856
    long origjiffies;
857
    int count = 0;
858
859
    #define MAX 6000 /* attempts */
860
861
862
    origjiffies = jiffies;
863
    /* Wait for indirect access */
864
    while (count++ < MAX)
865
	 {
866
		data = __wctdm_getreg(wc, card, I_STATUS);
867
868
		if (!data)
869
			return 0;
870
871
	 }
872
873
    if(count > (MAX-1)) printk(KERN_NOTICE " ##### Loop error (%02x) #####\n", data);
874
875
	return 0;
876
}
877
878
static unsigned char translate_3215(unsigned char address)
879
{
880
	int x;
881
	for (x=0;x<sizeof(indirect_regs)/sizeof(indirect_regs[0]);x++) {
882
		if (indirect_regs[x].address == address) {
883
			address = indirect_regs[x].altaddr;
884
			break;
885
		}
886
	}
887
	return address;
888
}
889
890
static int wctdm_proslic_setreg_indirect(struct wctdm *wc, int card, unsigned char address, unsigned short data)
891
{
892
	unsigned long flags;
893
	int res = -1;
894
	/* Translate 3215 addresses */
895
	if (wc->flags[card] & FLAG_3215) {
896
		address = translate_3215(address);
897
		if (address == 255)
898
			return 0;
899
	}
900
	spin_lock_irqsave(&wc->lock, flags);
901
	if(!__wait_access(wc, card)) {
902
		__wctdm_setreg(wc, card, IDA_LO,(unsigned char)(data & 0xFF));
903
		__wctdm_setreg(wc, card, IDA_HI,(unsigned char)((data & 0xFF00)>>8));
904
		__wctdm_setreg(wc, card, IAA,address);
905
		res = 0;
906
	};
907
	spin_unlock_irqrestore(&wc->lock, flags);
908
	return res;
909
}
910
911
static int wctdm_proslic_getreg_indirect(struct wctdm *wc, int card, unsigned char address)
912
{ 
913
	unsigned long flags;
914
	int res = -1;
915
	char *p=NULL;
916
	/* Translate 3215 addresses */
917
	if (wc->flags[card] & FLAG_3215) {
918
		address = translate_3215(address);
919
		if (address == 255)
920
			return 0;
921
	}
922
	spin_lock_irqsave(&wc->lock, flags);
923
	if (!__wait_access(wc, card)) {
924
		__wctdm_setreg(wc, card, IAA, address);
925
		if (!__wait_access(wc, card)) {
926
			unsigned char data1, data2;
927
			data1 = __wctdm_getreg(wc, card, IDA_LO);
928
			data2 = __wctdm_getreg(wc, card, IDA_HI);
929
			res = data1 | (data2 << 8);
930
		} else
931
			p = "Failed to wait inside\n";
932
	} else
933
		p = "failed to wait\n";
934
	spin_unlock_irqrestore(&wc->lock, flags);
935
	if (p)
936
		printk(KERN_NOTICE "%s", p);
937
	return res;
938
}
939
940
static int wctdm_proslic_init_indirect_regs(struct wctdm *wc, int card)
941
{
942
	unsigned char i;
943
944
	for (i=0; i<sizeof(indirect_regs) / sizeof(indirect_regs[0]); i++)
945
	{
946
		if(wctdm_proslic_setreg_indirect(wc, card, indirect_regs[i].address,indirect_regs[i].initial))
947
			return -1;
948
	}
949
950
	return 0;
951
}
952
953
static int wctdm_proslic_verify_indirect_regs(struct wctdm *wc, int card)
954
{ 
955
	int passed = 1;
956
	unsigned short i, initial;
957
	int j;
958
959
	for (i=0; i<sizeof(indirect_regs) / sizeof(indirect_regs[0]); i++) 
960
	{
961
		if((j = wctdm_proslic_getreg_indirect(wc, card, (unsigned char) indirect_regs[i].address)) < 0) {
962
			printk(KERN_NOTICE "Failed to read indirect register %d\n", i);
963
			return -1;
964
		}
965
		initial= indirect_regs[i].initial;
966
967
		if ( j != initial && (!(wc->flags[card] & FLAG_3215) || (indirect_regs[i].altaddr != 255)))
968
		{
969
			 printk(KERN_NOTICE "!!!!!!! %s  iREG %X = %X  should be %X\n",
970
				indirect_regs[i].name,indirect_regs[i].address,j,initial );
971
			 passed = 0;
972
		}	
973
	}
974
975
    if (passed) {
976
		if (debug)
977
			printk(KERN_DEBUG "Init Indirect Registers completed successfully.\n");
978
    } else {
979
		printk(KERN_NOTICE " !!!!! Init Indirect Registers UNSUCCESSFULLY.\n");
980
		return -1;
981
    }
982
    return 0;
983
}
984
985
static inline void wctdm_proslic_recheck_sanity(struct wctdm *wc, int card)
986
{
987
	int res;
988
	/* Check loopback */
989
	res = wc->reg1shadow[card];
990
	
991
	if (!res && (res != wc->mod[card].fxs.lasttxhook))     // read real state from register   By wx
992
		res=wctdm_getreg(wc, card, 64);
993
	
994
	if (!res && (res != wc->mod[card].fxs.lasttxhook)) {
995
		res = wctdm_getreg(wc, card, 8);
996
		if (res) {
997
			printk(KERN_NOTICE "Ouch, part reset, quickly restoring reality (%d)\n", card);
998
			wctdm_init_proslic(wc, card, 1, 0, 1);
999
		} else {
1000
			if (wc->mod[card].fxs.palarms++ < MAX_ALARMS) {
1001
				printk(KERN_NOTICE "Power alarm on module %d, resetting!\n", card + 1);
1002
				if (wc->mod[card].fxs.lasttxhook == 4)
1003
					wc->mod[card].fxs.lasttxhook = 1;
1004
				wctdm_setreg(wc, card, 64, wc->mod[card].fxs.lasttxhook);
1005
			} else {
1006
				if (wc->mod[card].fxs.palarms == MAX_ALARMS)
1007
					printk(KERN_NOTICE "Too many power alarms on card %d, NOT resetting!\n", card + 1);
1008
			}
1009
		}
1010
	}
1011
}
1012
static inline void wctdm_voicedaa_check_hook(struct wctdm *wc, int card)
1013
{
1014
#define MS_PER_CHECK_HOOK 16
1015
1016
#ifndef AUDIO_RINGCHECK
1017
	unsigned char res;
1018
#endif	
1019
	signed char b;
1020
	int errors = 0;
1021
	struct fxo *fxo = &wc->mod[card].fxo;
1022
1023
	/* Try to track issues that plague slot one FXO's */
1024
	b = wc->reg0shadow[card];
1025
	if ((b & 0x2) || !(b & 0x8)) {
1026
		/* Not good -- don't look at anything else */
1027
		if (debug)
1028
			printk(KERN_DEBUG "Error (%02x) on card %d!\n", b, card + 1); 
1029
		errors++;
1030
	}
1031
	b &= 0x9b;
1032
	if (fxo->offhook) {
1033
		if (b != 0x9)
1034
			wctdm_setreg(wc, card, 5, 0x9);
1035
	} else {
1036
		if (b != 0x8)
1037
			wctdm_setreg(wc, card, 5, 0x8);
1038
	}
1039
	if (errors)
1040
		return;
1041
	if (!fxo->offhook) {
1042
 if(fixedtimepolarity) {
1043
			if ( wc->cid_state[card] == CID_STATE_RING_ON && wc->cid_ring_on_time[card]>0)
1044
			{
1045
 	if(wc->cid_ring_on_time[card]>=fixedtimepolarity )
1046
			{
1047
			dahdi_qevent_lock(wc->chans[card], DAHDI_EVENT_POLARITY);
1048
			wc->cid_ring_on_time[card] = -1;	/* the polarity already sent */	
1049
			}
1050
			else
1051
		wc->cid_ring_on_time[card] += 16;
1052
    }
1053
}
1054
		if (fwringdetect) {
1055
			res = wc->reg0shadow[card] & 0x60;
1056
			if (fxo->ringdebounce) {
1057
				--fxo->ringdebounce;
1058
				if (res && (res != fxo->lastrdtx) &&
1059
				    (fxo->battery == BATTERY_PRESENT)) {
1060
					if (!fxo->wasringing) {
1061
						fxo->wasringing = 1;
1062
						if (debug)
1063
          printk(KERN_DEBUG "RING on %d/%d!\n", wc->span.spanno, card + 1);
1064
	if(cidbeforering)
1065
						{
1066
							if(wc->cid_state[card] == CID_STATE_IDLE)
1067
							{
1068
								wc->cid_state[card] = CID_STATE_RING_ON;
1069
								wc->cid_ring_on_time[card] = 16;	/* check every 16ms */
1070
							}
1071
							else
1072
								dahdi_hooksig(wc->chans[card], DAHDI_RXSIG_RING);
1073
						}
1074
						else 							
1075
        dahdi_hooksig(wc->chans[card], DAHDI_RXSIG_RING);
1076
					}
1077
					fxo->lastrdtx = res;
1078
					fxo->ringdebounce = 10;
1079
				} else if (!res) {
1080
					if ((fxo->ringdebounce == 0) && fxo->wasringing) {
1081
				fxo->wasringing = 0;
1082
				if (debug)
1083
				printk(KERN_DEBUG "NO RING on %d/%d!\n", wc->span.spanno, card + 1);
1084
	if(cidbeforering)
1085
						{
1086
							if(wc->cid_state[card] == CID_STATE_RING_ON)
1087
							{
1088
								if(fixedtimepolarity==0)
1089
									dahdi_qevent_lock(wc->chans[card], DAHDI_EVENT_POLARITY);
1090
								wc->cid_state[card] = CID_STATE_RING_OFF;
1091
							}
1092
							else 
1093
							{
1094
								if(wc->cid_state[card] == CID_STATE_WAIT_RING_FINISH)
1095
									wc->cid_history_clone_cnt[card] = cidtimeout;
1096
								dahdi_hooksig(wc->chans[card], DAHDI_RXSIG_OFFHOOK);
1097
							}
1098
						}
1099
						else
1100
1101
						dahdi_hooksig(wc->chans[card], DAHDI_RXSIG_OFFHOOK);
1102
				}
1103
				}
1104
			} else if (res && (fxo->battery == BATTERY_PRESENT)) {
1105
				fxo->lastrdtx = res;
1106
				fxo->ringdebounce = 10;
1107
			}
1108
		} else {
1109
			res = wc->reg0shadow[card];
1110
			if ((res & 0x60) && (fxo->battery == BATTERY_PRESENT)) {
1111
				fxo->ringdebounce += (DAHDI_CHUNKSIZE * 16);
1112
				if (fxo->ringdebounce >= DAHDI_CHUNKSIZE * ringdebounce) {
1113
					if (!fxo->wasringing) {
1114
						fxo->wasringing = 1;
1115
 if(cidbeforering)
1116
						{
1117
							if(wc->cid_state[card] == CID_STATE_IDLE)
1118
							{	
1119
								wc->cid_state[card] = CID_STATE_RING_ON;
1120
								wc->cid_ring_on_time[card] = 16;		/* check every 16ms */
1121
							}
1122
							else
1123
								dahdi_hooksig(wc->chans[card], DAHDI_RXSIG_RING);
1124
						}
1125
						else      
1126
						dahdi_hooksig(wc->chans[card], DAHDI_RXSIG_RING);
1127
						if (debug)
1128
							printk(KERN_DEBUG "RING on %d/%d!\n", wc->span.spanno, card + 1);
1129
					}
1130
					fxo->ringdebounce = DAHDI_CHUNKSIZE * ringdebounce;
1131
				}
1132
			} else {
1133
				fxo->ringdebounce -= DAHDI_CHUNKSIZE * 4;
1134
				if (fxo->ringdebounce <= 0) {
1135
					if (fxo->wasringing) {
1136
						fxo->wasringing = 0;
1137
	if(cidbeforering)
1138
						{
1139
							if(wc->cid_state[card] == CID_STATE_RING_ON)
1140
							{
1141
								if(fixedtimepolarity==0)
1142
									dahdi_qevent_lock(wc->chans[card], DAHDI_EVENT_POLARITY);
1143
								wc->cid_state[card] = CID_STATE_RING_OFF;
1144
							}
1145
							else 
1146
							{
1147
								if(wc->cid_state[card] == CID_STATE_WAIT_RING_FINISH)
1148
									wc->cid_history_clone_cnt[card] = cidtimeout;
1149
								dahdi_hooksig(wc->chans[card], DAHDI_RXSIG_OFFHOOK);
1150
							}
1151
						}
1152
						else
1153
						dahdi_hooksig(wc->chans[card], DAHDI_RXSIG_OFFHOOK);
1154
						if (debug)
1155
							printk(KERN_DEBUG "NO RING on %d/%d!\n", wc->span.spanno, card + 1);
1156
					}
1157
					fxo->ringdebounce = 0;
1158
				}
1159
			}
1160
		}
1161
	}
1162
1163
	b = wc->reg1shadow[card];
1164
	if (abs(b) < battthresh) {
1165
		/* possible existing states:
1166
		   battery lost, no debounce timer
1167
		   battery lost, debounce timer (going to battery present)
1168
		   battery present or unknown, no debounce timer
1169
		   battery present or unknown, debounce timer (going to battery lost)
1170
		*/
1171
1172
		if (fxo->battery == BATTERY_LOST) {
1173
			if (fxo->battdebounce) {
1174
				/* we were going to BATTERY_PRESENT, but battery was lost again,
1175
				   so clear the debounce timer */
1176
				fxo->battdebounce = 0;
1177
			}
1178
		} else {
1179
			if (fxo->battdebounce) {
1180
				/* going to BATTERY_LOST, see if we are there yet */
1181
				if (--fxo->battdebounce == 0) {
1182
					fxo->battery = BATTERY_LOST;
1183
					if (debug)
1184
						printk(KERN_DEBUG "NO BATTERY on %d/%d!\n", wc->span.spanno, card + 1);
1185
#ifdef	JAPAN
1186
					if (!wc->ohdebounce && wc->offhook) {
1187
						dahdi_hooksig(&wc->chans[card], DAHDI_RXSIG_ONHOOK);
1188
						if (debug)
1189
							printk(KERN_DEBUG "Signalled On Hook\n");
1190
#ifdef	ZERO_BATT_RING
1191
						wc->onhook++;
1192
#endif
1193
					}
1194
#else
1195
					dahdi_hooksig(wc->chans[card], DAHDI_RXSIG_ONHOOK);
1196
					/* set the alarm timer, taking into account that part of its time
1197
					   period has already passed while debouncing occurred */
1198
					fxo->battalarm = (battalarm - battdebounce) / MS_PER_CHECK_HOOK;
1199
#endif
1200
				}
1201
			} else {
1202
				/* start the debounce timer to verify that battery has been lost */
1203
				fxo->battdebounce = battdebounce / MS_PER_CHECK_HOOK;
1204
			}
1205
		}
1206
	} else {
1207
		/* possible existing states:
1208
		   battery lost or unknown, no debounce timer
1209
		   battery lost or unknown, debounce timer (going to battery present)
1210
		   battery present, no debounce timer
1211
		   battery present, debounce timer (going to battery lost)
1212
		*/
1213
1214
		if (fxo->battery == BATTERY_PRESENT) {
1215
			if (fxo->battdebounce) {
1216
				/* we were going to BATTERY_LOST, but battery appeared again,
1217
				   so clear the debounce timer */
1218
				fxo->battdebounce = 0;
1219
			}
1220
		} else {
1221
			if (fxo->battdebounce) {
1222
				/* going to BATTERY_PRESENT, see if we are there yet */
1223
				if (--fxo->battdebounce == 0) {
1224
					fxo->battery = BATTERY_PRESENT;
1225
					if (debug)
1226
						printk(KERN_DEBUG "BATTERY on %d/%d (%s)!\n", wc->span.spanno, card + 1, 
1227
						       (b < 0) ? "-" : "+");			    
1228
#ifdef	ZERO_BATT_RING
1229
					if (wc->onhook) {
1230
						wc->onhook = 0;
1231
						dahdi_hooksig(&wc->chans[card], DAHDI_RXSIG_OFFHOOK);
1232
						if (debug)
1233
							printk(KERN_DEBUG "Signalled Off Hook\n");
1234
					}
1235
#else
1236
					dahdi_hooksig(wc->chans[card], DAHDI_RXSIG_OFFHOOK);
1237
#endif
1238
					/* set the alarm timer, taking into account that part of its time
1239
					   period has already passed while debouncing occurred */
1240
					fxo->battalarm = (battalarm - battdebounce) / MS_PER_CHECK_HOOK;
1241
				}
1242
			} else {
1243
				/* start the debounce timer to verify that battery has appeared */
1244
				fxo->battdebounce = battdebounce / MS_PER_CHECK_HOOK;
1245
			}
1246
		}
1247
	}
1248
1249
	if (fxo->lastpol >= 0) {
1250
		if (b < 0) {
1251
			fxo->lastpol = -1;
1252
			fxo->polaritydebounce = POLARITY_DEBOUNCE / MS_PER_CHECK_HOOK;
1253
		}
1254
	} 
1255
	if (fxo->lastpol <= 0) {
1256
		if (b > 0) {
1257
			fxo->lastpol = 1;
1258
			fxo->polaritydebounce = POLARITY_DEBOUNCE / MS_PER_CHECK_HOOK;
1259
		}
1260
	}
1261
1262
	if (fxo->battalarm) {
1263
		if (--fxo->battalarm == 0) {
1264
			/* the alarm timer has expired, so update the battery alarm state
1265
			   for this channel */
1266
			dahdi_alarm_channel(wc->chans[card], fxo->battery == BATTERY_LOST ? DAHDI_ALARM_RED : DAHDI_ALARM_NONE);
1267
		}
1268
	}
1269
1270
	if (fxo->polaritydebounce) {
1271
		if (--fxo->polaritydebounce == 0) {
1272
		    if (fxo->lastpol != fxo->polarity) {
1273
				if (debug)
1274
					printk(KERN_DEBUG "%lu Polarity reversed (%d -> %d)\n", jiffies, 
1275
				       fxo->polarity, 
1276
				       fxo->lastpol);
1277
				if (fxo->polarity)
1278
					dahdi_qevent_lock(wc->chans[card], DAHDI_EVENT_POLARITY);
1279
				fxo->polarity = fxo->lastpol;
1280
		    }
1281
		}
1282
	}
1283
#undef MS_PER_CHECK_HOOK
1284
}
1285
1286
static inline void wctdm_proslic_check_hook(struct wctdm *wc, int card)
1287
{
1288
	char res;
1289
	int hook;
1290
1291
	/* For some reason we have to debounce the
1292
	   hook detector.  */
1293
1294
	res = wc->reg0shadow[card];
1295
	hook = (res & 1);
1296
	if (hook != wc->mod[card].fxs.lastrxhook) {
1297
		/* Reset the debounce (must be multiple of 4ms) */
1298
		wc->mod[card].fxs.debounce = 8 * (4 * 8);
1299
#if 0
1300
		printk(KERN_DEBUG "Resetting debounce card %d hook %d, %d\n", card, hook, wc->mod[card].fxs.debounce);
1301
#endif
1302
	} else {
1303
		if (wc->mod[card].fxs.debounce > 0) {
1304
			wc->mod[card].fxs.debounce-= 16 * DAHDI_CHUNKSIZE;
1305
#if 0
1306
			printk(KERN_DEBUG "Sustaining hook %d, %d\n", hook, wc->mod[card].fxs.debounce);
1307
#endif
1308
			if (!wc->mod[card].fxs.debounce) {
1309
#if 0
1310
				printk(KERN_DEBUG "Counted down debounce, newhook: %d...\n", hook);
1311
#endif
1312
				wc->mod[card].fxs.debouncehook = hook;
1313
			}
1314
			if (!wc->mod[card].fxs.oldrxhook && wc->mod[card].fxs.debouncehook) {
1315
				/* Off hook */
1316
#if 1
1317
				if (debug)
1318
#endif				
1319
					printk(KERN_DEBUG "opvxa1200: Card %d Going off hook\n", card);
1320
				dahdi_hooksig(wc->chans[card], DAHDI_RXSIG_OFFHOOK);
1321
				if (robust)
1322
					wctdm_init_proslic(wc, card, 1, 0, 1);
1323
				wc->mod[card].fxs.oldrxhook = 1;
1324
			
1325
			} else if (wc->mod[card].fxs.oldrxhook && !wc->mod[card].fxs.debouncehook) {
1326
				/* On hook */
1327
#if 1
1328
				if (debug)
1329
#endif				
1330
					printk(KERN_DEBUG "opvxa1200: Card %d Going on hook\n", card);
1331
				dahdi_hooksig(wc->chans[card], DAHDI_RXSIG_ONHOOK);
1332
				wc->mod[card].fxs.oldrxhook = 0;
1333
			}
1334
		}
1335
	}
1336
	wc->mod[card].fxs.lastrxhook = hook;
1337
}
1338
1339
DAHDI_IRQ_HANDLER(wctdm_interrupt)
1340
{
1341
	struct wctdm *wc = dev_id;
1342
	unsigned char ints;
1343
	int x, y, z;
1344
	int mode;
1345
1346
	ints = inb(wc->ioaddr + WC_INTSTAT);
1347
1348
	if (!ints)
1349
		return IRQ_NONE;
1350
1351
	outb(ints, wc->ioaddr + WC_INTSTAT);
1352
	
1353
	if (ints & 0x10) {
1354
		/* Stop DMA, wait for watchdog */
1355
		printk(KERN_INFO "TDM PCI Master abort\n");
1356
		wctdm_stop_dma(wc);
1357
		return IRQ_RETVAL(1);
1358
	}
1359
	
1360
	if (ints & 0x20) {
1361
		printk(KERN_INFO "PCI Target abort\n");
1362
		return IRQ_RETVAL(1);
1363
	}
1364
1365
	for (x=0;x<wc->max_cards/*4*3*/;x++) {
1366
		if (wc->cardflag & (1 << x) &&
1367
		    (wc->modtype[x] == MOD_TYPE_FXS)) {
1368
			if (wc->mod[x].fxs.lasttxhook == 0x4) {
1369
				/* RINGing, prepare for OHT */
1370
				wc->mod[x].fxs.ohttimer = OHT_TIMER << 3;
1371
				if (reversepolarity)
1372
					wc->mod[x].fxs.idletxhookstate = 0x6;	/* OHT mode when idle */
1373
				else
1374
					wc->mod[x].fxs.idletxhookstate = 0x2; 
1375
			} else {
1376
				if (wc->mod[x].fxs.ohttimer) {
1377
					wc->mod[x].fxs.ohttimer-= DAHDI_CHUNKSIZE;
1378
					if (!wc->mod[x].fxs.ohttimer) {
1379
						if (reversepolarity)
1380
							wc->mod[x].fxs.idletxhookstate = 0x5;	/* Switch to active */
1381
						else
1382
							wc->mod[x].fxs.idletxhookstate = 0x1;
1383
						if ((wc->mod[x].fxs.lasttxhook == 0x2) || (wc->mod[x].fxs.lasttxhook == 0x6)) {
1384
							/* Apply the change if appropriate */
1385
							if (reversepolarity) 
1386
								wc->mod[x].fxs.lasttxhook = 0x5;
1387
							else
1388
								wc->mod[x].fxs.lasttxhook = 0x1;
1389
							wctdm_setreg(wc, x, 64, wc->mod[x].fxs.lasttxhook);
1390
						}
1391
					}
1392
				}
1393
			}
1394
		}
1395
	}
1396
1397
	if (ints & 0x0f) {
1398
		wc->intcount++;
1399
		z = wc->intcount & 0x3;
1400
		mode = wc->intcount & 0xc;
1401
		for(y=0; y<wc->max_cards/4/*3*/; y++)
1402
		{
1403
			x = z + y*4;
1404
			if (wc->cardflag & (1 << x ) ) 
1405
			{
1406
				switch(mode) 
1407
				{
1408
				case 0:
1409
					/* Rest */
1410
					break;
1411
				case 4:
1412
					/* Read first shadow reg */
1413
					if (wc->modtype[x] == MOD_TYPE_FXS)
1414
						wc->reg0shadow[x] = wctdm_getreg(wc, x, 68);
1415
					else if (wc->modtype[x] == MOD_TYPE_FXO)
1416
						wc->reg0shadow[x] = wctdm_getreg(wc, x, 5);
1417
					break;
1418
				case 8:
1419
					/* Read second shadow reg */
1420
					if (wc->modtype[x] == MOD_TYPE_FXS)
1421
						wc->reg1shadow[x] = wctdm_getreg(wc, x, 64);
1422
					else if (wc->modtype[x] == MOD_TYPE_FXO)
1423
						wc->reg1shadow[x] = wctdm_getreg(wc, x, 29);
1424
					break;
1425
				case 12:
1426
					/* Perform processing */
1427
					if (wc->modtype[x] == MOD_TYPE_FXS) {
1428
						wctdm_proslic_check_hook(wc, x);
1429
						if (!(wc->intcount & 0xf0))
1430
							wctdm_proslic_recheck_sanity(wc, x);
1431
					} else if (wc->modtype[x] == MOD_TYPE_FXO) {
1432
						wctdm_voicedaa_check_hook(wc, x);
1433
					}
1434
					break;
1435
				}
1436
			}
1437
		}
1438
		if (!(wc->intcount % 10000)) {
1439
			/* Accept an alarm once per 10 seconds */
1440
			for (x=0;x<wc->max_cards/*4*3*/;x++) 
1441
				if (wc->modtype[x] == MOD_TYPE_FXS) {
1442
					if (wc->mod[x].fxs.palarms)
1443
						wc->mod[x].fxs.palarms--;
1444
				}
1445
		}
1446
		wctdm_receiveprep(wc, ints);
1447
		wctdm_transmitprep(wc, ints);
1448
	}
1449
1450
	return IRQ_RETVAL(1);
1451
1452
}
1453
1454
static int wctdm_voicedaa_insane(struct wctdm *wc, int card)
1455
{
1456
	int blah;
1457
	blah = wctdm_getreg(wc, card, 2);
1458
	if (blah != 0x3)
1459
		return -2;
1460
	blah = wctdm_getreg(wc, card, 11);
1461
	if (debug)
1462
		printk(KERN_DEBUG "VoiceDAA System: %02x\n", blah & 0xf);
1463
	return 0;
1464
}
1465
1466
static int wctdm_proslic_insane(struct wctdm *wc, int card)
1467
{
1468
	int blah,insane_report;
1469
	insane_report=0;
1470
1471
	blah = wctdm_getreg(wc, card, 0);
1472
	if (debug) 
1473
		printk(KERN_DEBUG "ProSLIC on module %d, product %d, version %d\n", card, (blah & 0x30) >> 4, (blah & 0xf));
1474
1475
#if 0
1476
	if ((blah & 0x30) >> 4) {
1477
		printk(KERN_DEBUG "ProSLIC on module %d is not a 3210.\n", card);
1478
		return -1;
1479
	}
1480
#endif
1481
	if (((blah & 0xf) == 0) || ((blah & 0xf) == 0xf)) {
1482
		/* SLIC not loaded */
1483
		return -1;
1484
	}
1485
	if ((blah & 0xf) < 2) {
1486
		printk(KERN_NOTICE "ProSLIC 3210 version %d is too old\n", blah & 0xf);
1487
		return -1;
1488
	}
1489
	if (wctdm_getreg(wc, card, 1) & 0x80)
1490
	/* ProSLIC 3215, not a 3210 */
1491
		wc->flags[card] |= FLAG_3215;
1492
	
1493
	blah = wctdm_getreg(wc, card, 8);
1494
	if (blah != 0x2) {
1495
		printk(KERN_NOTICE  "ProSLIC on module %d insane (1) %d should be 2\n", card, blah);
1496
		return -1;
1497
	} else if ( insane_report)
1498
		printk(KERN_NOTICE  "ProSLIC on module %d Reg 8 Reads %d Expected is 0x2\n",card,blah);
1499
1500
	blah = wctdm_getreg(wc, card, 64);
1501
	if (blah != 0x0) {
1502
		printk(KERN_NOTICE  "ProSLIC on module %d insane (2)\n", card);
1503
		return -1;
1504
	} else if ( insane_report)
1505
		printk(KERN_NOTICE  "ProSLIC on module %d Reg 64 Reads %d Expected is 0x0\n",card,blah);
1506
1507
	blah = wctdm_getreg(wc, card, 11);
1508
	if (blah != 0x33) {
1509
		printk(KERN_NOTICE  "ProSLIC on module %d insane (3)\n", card);
1510
		return -1;
1511
	} else if ( insane_report)
1512
		printk(KERN_NOTICE  "ProSLIC on module %d Reg 11 Reads %d Expected is 0x33\n",card,blah);
1513
1514
	/* Just be sure it's setup right. */
1515
	wctdm_setreg(wc, card, 30, 0);
1516
1517
	if (debug) 
1518
		printk(KERN_DEBUG "ProSLIC on module %d seems sane.\n", card);
1519
	return 0;
1520
}
1521
1522
static int wctdm_proslic_powerleak_test(struct wctdm *wc, int card)
1523
{
1524
	unsigned long origjiffies;
1525
	unsigned char vbat;
1526
1527
	/* Turn off linefeed */
1528
	wctdm_setreg(wc, card, 64, 0);
1529
1530
	/* Power down */
1531
	wctdm_setreg(wc, card, 14, 0x10);
1532
1533
	/* Wait for one second */
1534
	origjiffies = jiffies;
1535
1536
	while((vbat = wctdm_getreg(wc, card, 82)) > 0x6) {
1537
		if ((jiffies - origjiffies) >= (HZ/2))
1538
			break;;
1539
	}
1540
1541
	if (vbat < 0x06) {
1542
		printk(KERN_NOTICE "Excessive leakage detected on module %d: %d volts (%02x) after %d ms\n", card,
1543
		       376 * vbat / 1000, vbat, (int)((jiffies - origjiffies) * 1000 / HZ));
1544
		return -1;
1545
	} else if (debug) {
1546
		printk(KERN_NOTICE "Post-leakage voltage: %d volts\n", 376 * vbat / 1000);
1547
	}
1548
	return 0;
1549
}
1550
1551
static int wctdm_powerup_proslic(struct wctdm *wc, int card, int fast)
1552
{
1553
	unsigned char vbat;
1554
	unsigned long origjiffies;
1555
	int lim;
1556
1557
	/* Set period of DC-DC converter to 1/64 khz */
1558
	wctdm_setreg(wc, card, 92, 0xff /* was 0xff */);
1559
1560
	/* Wait for VBat to powerup */
1561
	origjiffies = jiffies;
1562
1563
	/* Disable powerdown */
1564
	wctdm_setreg(wc, card, 14, 0);
1565
1566
	/* If fast, don't bother checking anymore */
1567
	if (fast)
1568
		return 0;
1569
1570
	while((vbat = wctdm_getreg(wc, card, 82)) < 0xc0) {
1571
		/* Wait no more than 500ms */
1572
		if ((jiffies - origjiffies) > HZ/2) {
1573
			break;
1574
		}
1575
	}
1576
1577
	if (vbat < 0xc0) {
1578
		if (wc->proslic_power == PROSLIC_POWER_UNKNOWN)
1579
				 printk(KERN_NOTICE "ProSLIC on module %d failed to powerup within %d ms (%d mV only)\n\n -- DID YOU REMEMBER TO PLUG IN THE HD POWER CABLE TO THE A1200P??\n",
1580
					card, (int)(((jiffies - origjiffies) * 1000 / HZ)),
1581
					vbat * 375);
1582
		wc->proslic_power = PROSLIC_POWER_WARNED;
1583
		return -1;
1584
	} else if (debug) {
1585
		printk(KERN_DEBUG "ProSLIC on module %d powered up to -%d volts (%02x) in %d ms\n",
1586
		       card, vbat * 376 / 1000, vbat, (int)(((jiffies - origjiffies) * 1000 / HZ)));
1587
	}
1588
	wc->proslic_power = PROSLIC_POWER_ON;
1589
1590
        /* Proslic max allowed loop current, reg 71 LOOP_I_LIMIT */
1591
        /* If out of range, just set it to the default value     */
1592
        lim = (loopcurrent - 20) / 3;
1593
        if ( loopcurrent > 41 ) {
1594
                lim = 0;
1595
                if (debug)
1596
                        printk(KERN_DEBUG "Loop current out of range! Setting to default 20mA!\n");
1597
        }
1598
        else if (debug)
1599
                        printk(KERN_DEBUG "Loop current set to %dmA!\n",(lim*3)+20);
1600
        wctdm_setreg(wc,card,LOOP_I_LIMIT,lim);
1601
1602
	/* Engage DC-DC converter */
1603
	wctdm_setreg(wc, card, 93, 0x19 /* was 0x19 */);
1604
#if 0
1605
	origjiffies = jiffies;
1606
	while(0x80 & wctdm_getreg(wc, card, 93)) {
1607
		if ((jiffies - origjiffies) > 2 * HZ) {
1608
			printk(KERN_DEBUG "Timeout waiting for DC-DC calibration on module %d\n", card);
1609
			return -1;
1610
		}
1611
	}
1612
1613
#if 0
1614
	/* Wait a full two seconds */
1615
	while((jiffies - origjiffies) < 2 * HZ);
1616
1617
	/* Just check to be sure */
1618
	vbat = wctdm_getreg(wc, card, 82);
1619
	printk(KERN_DEBUG "ProSLIC on module %d powered up to -%d volts (%02x) in %d ms\n",
1620
		       card, vbat * 376 / 1000, vbat, (int)(((jiffies - origjiffies) * 1000 / HZ)));
1621
#endif
1622
#endif
1623
	return 0;
1624
1625
}
1626
1627
static int wctdm_proslic_manual_calibrate(struct wctdm *wc, int card){
1628
	unsigned long origjiffies;
1629
	unsigned char i;
1630
1631
	wctdm_setreg(wc, card, 21, 0);//(0)  Disable all interupts in DR21
1632
	wctdm_setreg(wc, card, 22, 0);//(0)Disable all interupts in DR21
1633
	wctdm_setreg(wc, card, 23, 0);//(0)Disable all interupts in DR21
1634
	wctdm_setreg(wc, card, 64, 0);//(0)
1635
1636
	wctdm_setreg(wc, card, 97, 0x18); //(0x18)Calibrations without the ADC and DAC offset and without common mode calibration.
1637
	wctdm_setreg(wc, card, 96, 0x47); //(0x47)	Calibrate common mode and differential DAC mode DAC + ILIM
1638
1639
	origjiffies=jiffies;
1640
	while( wctdm_getreg(wc,card,96)!=0 ){
1641
		if((jiffies-origjiffies)>80)
1642
			return -1;
1643
	}
1644
//Initialized DR 98 and 99 to get consistant results.
1645
// 98 and 99 are the results registers and the search should have same intial conditions.
1646
1647
/*******************************The following is the manual gain mismatch calibration****************************/
1648
/*******************************This is also available as a function *******************************************/
1649
	// Delay 10ms
1650
	origjiffies=jiffies; 
1651
	while((jiffies-origjiffies)<1);
1652
	wctdm_proslic_setreg_indirect(wc, card, 88,0);
1653
	wctdm_proslic_setreg_indirect(wc,card,89,0);
1654
	wctdm_proslic_setreg_indirect(wc,card,90,0);
1655
	wctdm_proslic_setreg_indirect(wc,card,91,0);
1656
	wctdm_proslic_setreg_indirect(wc,card,92,0);
1657
	wctdm_proslic_setreg_indirect(wc,card,93,0);
1658
1659
	wctdm_setreg(wc, card, 98,0x10); // This is necessary if the calibration occurs other than at reset time
1660
	wctdm_setreg(wc, card, 99,0x10);
1661
1662
	for ( i=0x1f; i>0; i--)
1663
	{
1664
		wctdm_setreg(wc, card, 98,i);
1665
		origjiffies=jiffies; 
1666
		while((jiffies-origjiffies)<4);
1667
		if((wctdm_getreg(wc,card,88)) == 0)
1668
			break;
1669
	} // for
1670
1671
	for ( i=0x1f; i>0; i--)
1672
	{
1673
		wctdm_setreg(wc, card, 99,i);
1674
		origjiffies=jiffies; 
1675
		while((jiffies-origjiffies)<4);
1676
		if((wctdm_getreg(wc,card,89)) == 0)
1677
			break;
1678
	}//for
1679
1680
/*******************************The preceding is the manual gain mismatch calibration****************************/
1681
/**********************************The following is the longitudinal Balance Cal***********************************/
1682
	wctdm_setreg(wc,card,64,1);
1683
	while((jiffies-origjiffies)<10); // Sleep 100?
1684
1685
	wctdm_setreg(wc, card, 64, 0);
1686
	wctdm_setreg(wc, card, 23, 0x4);  // enable interrupt for the balance Cal
1687
	wctdm_setreg(wc, card, 97, 0x1); // this is a singular calibration bit for longitudinal calibration
1688
	wctdm_setreg(wc, card, 96,0x40);
1689
1690
	wctdm_getreg(wc,card,96); /* Read Reg 96 just cause */
1691
1692
	wctdm_setreg(wc, card, 21, 0xFF);
1693
	wctdm_setreg(wc, card, 22, 0xFF);
1694
	wctdm_setreg(wc, card, 23, 0xFF);
1695
1696
	/**The preceding is the longitudinal Balance Cal***/
1697
	return(0);
1698
1699
}
1700
#if 1
1701
static int wctdm_proslic_calibrate(struct wctdm *wc, int card)
1702
{
1703
	unsigned long origjiffies;
1704
	int x;
1705
	/* Perform all calibrations */
1706
	wctdm_setreg(wc, card, 97, 0x1f);
1707
	
1708
	/* Begin, no speedup */
1709
	wctdm_setreg(wc, card, 96, 0x5f);
1710
1711
	/* Wait for it to finish */
1712
	origjiffies = jiffies;
1713
	while(wctdm_getreg(wc, card, 96)) {
1714
		if ((jiffies - origjiffies) > 2 * HZ) {
1715
			printk(KERN_NOTICE "Timeout waiting for calibration of module %d\n", card);
1716
			return -1;
1717
		}
1718
	}
1719
	
1720
	if (debug) {
1721
		/* Print calibration parameters */
1722
		printk(KERN_DEBUG "Calibration Vector Regs 98 - 107: \n");
1723
		for (x=98;x<108;x++) {
1724
			printk(KERN_DEBUG "%d: %02x\n", x, wctdm_getreg(wc, card, x));
1725
		}
1726
	}
1727
	return 0;
1728
}
1729
#endif
1730
1731
static void wait_just_a_bit(int foo)
1732
{
1733
	long newjiffies;
1734
	newjiffies = jiffies + foo;
1735
	while(jiffies < newjiffies);
1736
}
1737
1738
/*********************************************************************
1739
 * Set the hwgain on the analog modules
1740
 *
1741
 * card = the card position for this module (0-23)
1742
 * gain = gain in dB x10 (e.g. -3.5dB  would be gain=-35)
1743
 * tx = (0 for rx; 1 for tx)
1744
 *
1745
 *******************************************************************/
1746
static int wctdm_set_hwgain(struct wctdm *wc, int card, __s32 gain, __u32 tx)
1747
{
1748
	if (!(wc->modtype[card] == MOD_TYPE_FXO)) {
1749
		printk(KERN_NOTICE "Cannot adjust gain.  Unsupported module type!\n");
1750
		return -1;
1751
	}
1752
	if (tx) {
1753
		if (debug)
1754
			printk(KERN_DEBUG "setting FXO tx gain for card=%d to %d\n", card, gain);
1755
		if (gain >=  -150 && gain <= 0) {
1756
			wctdm_setreg(wc, card, 38, 16 + (gain/-10));
1757
			wctdm_setreg(wc, card, 40, 16 + (-gain%10));
1758
		} else if (gain <= 120 && gain > 0) {
1759
			wctdm_setreg(wc, card, 38, gain/10);
1760
			wctdm_setreg(wc, card, 40, (gain%10));
1761
		} else {
1762
			printk(KERN_INFO "FXO tx gain is out of range (%d)\n", gain);
1763
			return -1;
1764
		}
1765
	} else { /* rx */
1766
		if (debug)
1767
			printk(KERN_DEBUG "setting FXO rx gain for card=%d to %d\n", card, gain);
1768
		if (gain >=  -150 && gain <= 0) {
1769
			wctdm_setreg(wc, card, 39, 16+ (gain/-10));
1770
			wctdm_setreg(wc, card, 41, 16 + (-gain%10));
1771
		} else if (gain <= 120 && gain > 0) {
1772
			wctdm_setreg(wc, card, 39, gain/10);
1773
			wctdm_setreg(wc, card, 41, (gain%10));
1774
		} else {
1775
			printk(KERN_INFO "FXO rx gain is out of range (%d)\n", gain);
1776
			return -1;
1777
		}
1778
	}
1779
1780
	return 0;
1781
}
1782
1783
static int wctdm_init_voicedaa(struct wctdm *wc, int card, int fast, int manual, int sane)
1784
{
1785
	unsigned char reg16=0, reg26=0, reg30=0, reg31=0;
1786
	long newjiffies;
1787
	wc->modtype[card] = MOD_TYPE_FXO;
1788
	/* Sanity check the ProSLIC */
1789
	reset_spi(wc, card);
1790
	if (!sane && wctdm_voicedaa_insane(wc, card))
1791
		return -2;
1792
1793
	/* Software reset */
1794
	wctdm_setreg(wc, card, 1, 0x80);
1795
1796
	/* Wait just a bit */
1797
	wait_just_a_bit(HZ/10);
1798
1799
	/* Enable PCM, ulaw */
1800
	if (alawoverride)
1801
		wctdm_setreg(wc, card, 33, 0x20);
1802
	else
1803
		wctdm_setreg(wc, card, 33, 0x28);
1804
1805
	/* Set On-hook speed, Ringer impedence, and ringer threshold */
1806
	reg16 |= (fxo_modes[_opermode].ohs << 6);
1807
	reg16 |= (fxo_modes[_opermode].rz << 1);
1808
	reg16 |= (fxo_modes[_opermode].rt);
1809
	wctdm_setreg(wc, card, 16, reg16);
1810
1811
	if(fwringdetect) {
1812
		/* Enable ring detector full-wave rectifier mode */
1813
		wctdm_setreg(wc, card, 18, 2);
1814
		wctdm_setreg(wc, card, 24, 0);
1815
	} else { 
1816
		/* Set to the device defaults */
1817
		wctdm_setreg(wc, card, 18, 0);
1818
		wctdm_setreg(wc, card, 24, 0x19);
1819
	}
1820
	
1821
	/* Set DC Termination:
1822
	   Tip/Ring voltage adjust, minimum operational current, current limitation */
1823
	reg26 |= (fxo_modes[_opermode].dcv << 6);
1824
	reg26 |= (fxo_modes[_opermode].mini << 4);
1825
	reg26 |= (fxo_modes[_opermode].ilim << 1);
1826
	wctdm_setreg(wc, card, 26, reg26);
1827
1828
	/* Set AC Impedence */ 
1829
	reg30 = (fxofullscale==1) ? (fxo_modes[_opermode].acim|0x10) :  (fxo_modes[_opermode].acim);
1830
	wctdm_setreg(wc, card, 30, reg30);
1831
1832
	/* Misc. DAA parameters */
1833
	if (fastpickup)
1834
		reg31 = 0xb3;
1835
	else
1836
		reg31 = 0xa3;
1837
1838
	reg31 |= (fxo_modes[_opermode].ohs2 << 3);
1839
	wctdm_setreg(wc, card, 31, reg31);
1840
1841
	/* Set Transmit/Receive timeslot */
1842
	//printk("set card %d to %d\n", card, (3-(card%4)) * 8 + (card/4) * 64);
1843
	wctdm_setreg(wc, card, 34, (3-(card%4)) * 8 + (card/4) * 64);
1844
	wctdm_setreg(wc, card, 35, 0x00);
1845
	wctdm_setreg(wc, card, 36, (3-(card%4)) * 8 + (card/4) * 64);
1846
	wctdm_setreg(wc, card, 37, 0x00);
1847
1848
	/* Enable ISO-Cap */
1849
	wctdm_setreg(wc, card, 6, 0x00);
1850
1851
	if (fastpickup)
1852
		wctdm_setreg(wc, card, 17, wctdm_getreg(wc, card, 17) | 0x20);
1853
1854
	/* Wait 1000ms for ISO-cap to come up */
1855
	newjiffies = jiffies;
1856
	newjiffies += 2 * HZ;
1857
	while((jiffies < newjiffies) && !(wctdm_getreg(wc, card, 11) & 0xf0))
1858
		wait_just_a_bit(HZ/10);
1859
1860
	if (!(wctdm_getreg(wc, card, 11) & 0xf0)) {
1861
		printk(KERN_NOTICE "VoiceDAA did not bring up ISO link properly!\n");
1862
		return -1;
1863
	}
1864
	if (debug)
1865
		printk(KERN_DEBUG "ISO-Cap is now up, line side: %02x rev %02x\n", 
1866
		       wctdm_getreg(wc, card, 11) >> 4,
1867
		       (wctdm_getreg(wc, card, 13) >> 2) & 0xf);
1868
	/* Enable on-hook line monitor */
1869
	wctdm_setreg(wc, card, 5, 0x08);
1870
1871
	/* Take values for fxotxgain and fxorxgain and apply them to module */
1872
	wctdm_set_hwgain(wc, card, fxotxgain, 1);
1873
	wctdm_set_hwgain(wc, card, fxorxgain, 0);
1874
1875
	/* NZ -- crank the tx gain up by 7 dB */
1876
	if (!strcmp(fxo_modes[_opermode].name, "NEWZEALAND")) {
1877
		printk(KERN_INFO "Adjusting gain\n");
1878
		wctdm_set_hwgain(wc, card, 7, 1);
1879
	}
1880
1881
	if(debug)
1882
		printk(KERN_DEBUG "DEBUG fxotxgain:%i.%i fxorxgain:%i.%i\n", (wctdm_getreg(wc, card, 38)/16)?-(wctdm_getreg(wc, card, 38) - 16) : wctdm_getreg(wc, card, 38), (wctdm_getreg(wc, card, 40)/16)? -(wctdm_getreg(wc, card, 40) - 16):wctdm_getreg(wc, card, 40), (wctdm_getreg(wc, card, 39)/16)? -(wctdm_getreg(wc, card, 39) - 16) : wctdm_getreg(wc, card, 39),(wctdm_getreg(wc, card, 41)/16)?-(wctdm_getreg(wc, card, 41) - 16):wctdm_getreg(wc, card, 41));
1883
1884
    return 0;
1885
		
1886
}
1887
1888
static int wctdm_init_proslic(struct wctdm *wc, int card, int fast, int manual, int sane)
1889
{
1890
1891
	unsigned short tmp[5];
1892
	unsigned char r19, r9;
1893
	int x;
1894
	int fxsmode=0;
1895
1896
	/* Sanity check the ProSLIC */
1897
	if (!sane && wctdm_proslic_insane(wc, card))
1898
		return -2;
1899
1900
	/* By default, don't send on hook */
1901
	if (reversepolarity)
1902
		wc->mod[card].fxs.idletxhookstate = 5;
1903
	else
1904
		wc->mod[card].fxs.idletxhookstate = 1;
1905
		
1906
	if (sane) {
1907
		/* Make sure we turn off the DC->DC converter to prevent anything from blowing up */
1908
		wctdm_setreg(wc, card, 14, 0x10);
1909
	}
1910
1911
	if (wctdm_proslic_init_indirect_regs(wc, card)) {
1912
		printk(KERN_INFO "Indirect Registers failed to initialize on module %d.\n", card);
1913
		return -1;
1914
	}
1915
1916
	/* Clear scratch pad area */
1917
	wctdm_proslic_setreg_indirect(wc, card, 97,0);
1918
1919
	/* Clear digital loopback */
1920
	wctdm_setreg(wc, card, 8, 0);
1921
1922
	/* Revision C optimization */
1923
	wctdm_setreg(wc, card, 108, 0xeb);
1924
1925
	/* Disable automatic VBat switching for safety to prevent
1926
	   Q7 from accidently turning on and burning out. */
1927
	wctdm_setreg(wc, card, 67, 0x07);  /* Note, if pulse dialing has problems at high REN loads
1928
					      change this to 0x17 */
1929
1930
	/* Turn off Q7 */
1931
	wctdm_setreg(wc, card, 66, 1);
1932
1933
	/* Flush ProSLIC digital filters by setting to clear, while
1934
	   saving old values */
1935
	for (x=0;x<5;x++) {
1936
		tmp[x] = wctdm_proslic_getreg_indirect(wc, card, x + 35);
1937
		wctdm_proslic_setreg_indirect(wc, card, x + 35, 0x8000);
1938
	}
1939
1940
	/* Power up the DC-DC converter */
1941
	if (wctdm_powerup_proslic(wc, card, fast)) {
1942
		printk(KERN_NOTICE "Unable to do INITIAL ProSLIC powerup on module %d\n", card);
1943
		return -1;
1944
	}
1945
1946
	if (!fast) {
1947
1948
		/* Check for power leaks */
1949
		if (wctdm_proslic_powerleak_test(wc, card)) {
1950
			printk(KERN_NOTICE "ProSLIC module %d failed leakage test.  Check for short circuit\n", card);
1951
		}
1952
		/* Power up again */
1953
		if (wctdm_powerup_proslic(wc, card, fast)) {
1954
			printk(KERN_NOTICE "Unable to do FINAL ProSLIC powerup on module %d\n", card);
1955
			return -1;
1956
		}
1957
#ifndef NO_CALIBRATION
1958
		/* Perform calibration */
1959
		if(manual) {
1960
			if (wctdm_proslic_manual_calibrate(wc, card)) {
1961
				//printk(KERN_NOTICE "Proslic failed on Manual Calibration\n");
1962
				if (wctdm_proslic_manual_calibrate(wc, card)) {
1963
					printk(KERN_NOTICE "Proslic Failed on Second Attempt to Calibrate Manually. (Try -DNO_CALIBRATION in Makefile)\n");
1964
					return -1;
1965
				}
1966
				printk(KERN_NOTICE "Proslic Passed Manual Calibration on Second Attempt\n");
1967
			}
1968
		}
1969
		else {
1970
			if(wctdm_proslic_calibrate(wc, card))  {
1971
				//printk(KERN_NOTICE "ProSlic died on Auto Calibration.\n");
1972
				if (wctdm_proslic_calibrate(wc, card)) {
1973
					printk(KERN_NOTICE "Proslic Failed on Second Attempt to Auto Calibrate\n");
1974
					return -1;
1975
				}
1976
				printk(KERN_NOTICE "Proslic Passed Auto Calibration on Second Attempt\n");
1977
			}
1978
		}
1979
		/* Perform DC-DC calibration */
1980
		wctdm_setreg(wc, card, 93, 0x99);
1981
		r19 = wctdm_getreg(wc, card, 107);
1982
		if ((r19 < 0x2) || (r19 > 0xd)) {
1983
			printk(KERN_NOTICE "DC-DC cal has a surprising direct 107 of 0x%02x!\n", r19);
1984
			wctdm_setreg(wc, card, 107, 0x8);
1985
		}
1986
1987
		/* Save calibration vectors */
1988
		for (x=0;x<NUM_CAL_REGS;x++)
1989
			wc->mod[card].fxs.calregs.vals[x] = wctdm_getreg(wc, card, 96 + x);
1990
#endif
1991
1992
	} else {
1993
		/* Restore calibration registers */
1994
		for (x=0;x<NUM_CAL_REGS;x++)
1995
			wctdm_setreg(wc, card, 96 + x, wc->mod[card].fxs.calregs.vals[x]);
1996
	}
1997
	/* Calibration complete, restore original values */
1998
	for (x=0;x<5;x++) {
1999
		wctdm_proslic_setreg_indirect(wc, card, x + 35, tmp[x]);
2000
	}
2001
2002
	if (wctdm_proslic_verify_indirect_regs(wc, card)) {
2003
		printk(KERN_INFO "Indirect Registers failed verification.\n");
2004
		return -1;
2005
	}
2006
2007
2008
#if 0
2009
    /* Disable Auto Power Alarm Detect and other "features" */
2010
    wctdm_setreg(wc, card, 67, 0x0e);
2011
    blah = wctdm_getreg(wc, card, 67);
2012
#endif
2013
2014
#if 0
2015
    if (wctdm_proslic_setreg_indirect(wc, card, 97, 0x0)) { // Stanley: for the bad recording fix
2016
		 printk(KERN_INFO "ProSlic IndirectReg Died.\n");
2017
		 return -1;
2018
	}
2019
#endif
2020
2021
    if (alawoverride)
2022
    	wctdm_setreg(wc, card, 1, 0x20);
2023
    else
2024
    	wctdm_setreg(wc, card, 1, 0x28);
2025
  // U-Law 8-bit interface
2026
    wctdm_setreg(wc, card, 2, (3-(card%4)) * 8 + (card/4) * 64);    // Tx Start count low byte  0
2027
    wctdm_setreg(wc, card, 3, 0);    // Tx Start count high byte 0
2028
    wctdm_setreg(wc, card, 4, (3-(card%4)) * 8 + (card/4) * 64);    // Rx Start count low byte  0
2029
    wctdm_setreg(wc, card, 5, 0);    // Rx Start count high byte 0
2030
    wctdm_setreg(wc, card, 18, 0xff);     // clear all interrupt
2031
    wctdm_setreg(wc, card, 19, 0xff);
2032
    wctdm_setreg(wc, card, 20, 0xff);
2033
    wctdm_setreg(wc, card, 73, 0x04);
2034
	if (fxshonormode) {
2035
		fxsmode = acim2tiss[fxo_modes[_opermode].acim];
2036
		wctdm_setreg(wc, card, 10, 0x08 | fxsmode);
2037
		if (fxo_modes[_opermode].ring_osc)
2038
			wctdm_proslic_setreg_indirect(wc, card, 20, fxo_modes[_opermode].ring_osc);
2039
		if (fxo_modes[_opermode].ring_x)
2040
			wctdm_proslic_setreg_indirect(wc, card, 21, fxo_modes[_opermode].ring_x);
2041
	}
2042
    if (lowpower)
2043
    	wctdm_setreg(wc, card, 72, 0x10);
2044
2045
#if 0
2046
    wctdm_setreg(wc, card, 21, 0x00); 	// enable interrupt
2047
    wctdm_setreg(wc, card, 22, 0x02); 	// Loop detection interrupt
2048
    wctdm_setreg(wc, card, 23, 0x01); 	// DTMF detection interrupt
2049
#endif
2050
2051
#if 0
2052
    /* Enable loopback */
2053
    wctdm_setreg(wc, card, 8, 0x2);
2054
    wctdm_setreg(wc, card, 14, 0x0);
2055
    wctdm_setreg(wc, card, 64, 0x0);
2056
    wctdm_setreg(wc, card, 1, 0x08);
2057
#endif
2058
2059
	if (fastringer) {
2060
		/* Speed up Ringer */
2061
		wctdm_proslic_setreg_indirect(wc, card, 20, 0x7e6d);
2062
		wctdm_proslic_setreg_indirect(wc, card, 21, 0x01b9);
2063
		/* Beef up Ringing voltage to 89V */
2064
		if (boostringer) {
2065
			wctdm_setreg(wc, card, 74, 0x3f);
2066
			if (wctdm_proslic_setreg_indirect(wc, card, 21, 0x247)) 
2067
				return -1;
2068
			printk(KERN_INFO  "Boosting fast ringer on slot %d (89V peak)\n", card + 1);
2069
		} else if (lowpower) {
2070
			if (wctdm_proslic_setreg_indirect(wc, card, 21, 0x14b)) 
2071
				return -1;
2072
			printk(KERN_INFO  "Reducing fast ring power on slot %d (50V peak)\n", card + 1);
2073
		} else
2074
			printk(KERN_INFO  "Speeding up ringer on slot %d (25Hz)\n", card + 1);
2075
	} else {
2076
		/* Beef up Ringing voltage to 89V */
2077
		if (boostringer) {
2078
			wctdm_setreg(wc, card, 74, 0x3f);
2079
			if (wctdm_proslic_setreg_indirect(wc, card, 21, 0x1d1)) 
2080
				return -1;
2081
			printk(KERN_INFO  "Boosting ringer on slot %d (89V peak)\n", card + 1);
2082
		} else if (lowpower) {
2083
			if (wctdm_proslic_setreg_indirect(wc, card, 21, 0x108)) 
2084
				return -1;
2085
			printk(KERN_INFO  "Reducing ring power on slot %d (50V peak)\n", card + 1);
2086
		}
2087
	}
2088
2089
	if(fxstxgain || fxsrxgain) {
2090
		r9 = wctdm_getreg(wc, card, 9);
2091
		switch (fxstxgain) {
2092
		
2093
			case 35:
2094
				r9+=8;
2095
				break;
2096
			case -35:
2097
				r9+=4;
2098
				break;
2099
			case 0: 
2100
				break;
2101
		}
2102
	
2103
		switch (fxsrxgain) {
2104
			
2105
			case 35:
2106
				r9+=2;
2107
				break;
2108
			case -35:
2109
				r9+=1;
2110
				break;
2111
			case 0:
2112
				break;
2113
		}
2114
		wctdm_setreg(wc,card,9,r9);
2115
	}
2116
2117
	if(debug)
2118
		printk(KERN_DEBUG "DEBUG: fxstxgain:%s fxsrxgain:%s\n",((wctdm_getreg(wc, card, 9)/8) == 1)?"3.5":(((wctdm_getreg(wc,card,9)/4) == 1)?"-3.5":"0.0"),((wctdm_getreg(wc, card, 9)/2) == 1)?"3.5":((wctdm_getreg(wc,card,9)%2)?"-3.5":"0.0"));
2119
2120
	wctdm_setreg(wc, card, 64, 0x01);
2121
	return 0;
2122
}
2123
2124
2125
static int wctdm_ioctl(struct dahdi_chan *chan, unsigned int cmd, unsigned long data)
2126
{
2127
	struct wctdm_stats stats;
2128
	struct wctdm_regs regs;
2129
	struct wctdm_regop regop;
2130
	struct wctdm_echo_coefs echoregs;
2131
	struct dahdi_hwgain hwgain;
2132
	struct wctdm *wc = chan->pvt;
2133
	int x;
2134
	switch (cmd) {
2135
	case DAHDI_ONHOOKTRANSFER:
2136
		if (wc->modtype[chan->chanpos - 1] != MOD_TYPE_FXS)
2137
			return -EINVAL;
2138
		if (get_user(x, (__user  int *)data))
2139
			return -EFAULT;
2140
		wc->mod[chan->chanpos - 1].fxs.ohttimer = x << 3;
2141
		if (reversepolarity)
2142
			wc->mod[chan->chanpos - 1].fxs.idletxhookstate = 0x6;	/* OHT mode when idle */
2143
		else
2144
			wc->mod[chan->chanpos - 1].fxs.idletxhookstate = 0x2;
2145
		if (wc->mod[chan->chanpos - 1].fxs.lasttxhook == 0x1 || wc->mod[chan->chanpos - 1].fxs.lasttxhook == 0x5) {
2146
				/* Apply the change if appropriate */
2147
				if (reversepolarity)
2148
					wc->mod[chan->chanpos - 1].fxs.lasttxhook = 0x6;
2149
				else
2150
					wc->mod[chan->chanpos - 1].fxs.lasttxhook = 0x2;
2151
				wctdm_setreg(wc, chan->chanpos - 1, 64, wc->mod[chan->chanpos - 1].fxs.lasttxhook);
2152
		}
2153
		break;
2154
	case DAHDI_SETPOLARITY:
2155
		if (get_user(x, (__user int *)data))
2156
			return -EFAULT;
2157
		if (wc->modtype[chan->chanpos - 1] != MOD_TYPE_FXS)
2158
			return -EINVAL;
2159
		/* Can't change polarity while ringing or when open */
2160
		if ((wc->mod[chan->chanpos -1 ].fxs.lasttxhook == 0x04) ||
2161
		    (wc->mod[chan->chanpos -1 ].fxs.lasttxhook == 0x00))
2162
			return -EINVAL;
2163
2164
		if ((x && !reversepolarity) || (!x && reversepolarity))
2165
			wc->mod[chan->chanpos - 1].fxs.lasttxhook |= 0x04;
2166
		else
2167
			wc->mod[chan->chanpos - 1].fxs.lasttxhook &= ~0x04;
2168
		wctdm_setreg(wc, chan->chanpos - 1, 64, wc->mod[chan->chanpos - 1].fxs.lasttxhook);
2169
		break;
2170
	case WCTDM_GET_STATS:
2171
		if (wc->modtype[chan->chanpos - 1] == MOD_TYPE_FXS) {
2172
			stats.tipvolt = wctdm_getreg(wc, chan->chanpos - 1, 80) * -376;
2173
			stats.ringvolt = wctdm_getreg(wc, chan->chanpos - 1, 81) * -376;
2174
			stats.batvolt = wctdm_getreg(wc, chan->chanpos - 1, 82) * -376;
2175
		} else if (wc->modtype[chan->chanpos - 1] == MOD_TYPE_FXO) {
2176
			stats.tipvolt = (signed char)wctdm_getreg(wc, chan->chanpos - 1, 29) * 1000;
2177
			stats.ringvolt = (signed char)wctdm_getreg(wc, chan->chanpos - 1, 29) * 1000;
2178
			stats.batvolt = (signed char)wctdm_getreg(wc, chan->chanpos - 1, 29) * 1000;
2179
		} else 
2180
			return -EINVAL;
2181
		if (copy_to_user((__user void *)data, &stats, sizeof(stats)))
2182
			return -EFAULT;
2183
		break;
2184
	case WCTDM_GET_REGS:
2185
		if (wc->modtype[chan->chanpos - 1] == MOD_TYPE_FXS) {
2186
			for (x=0;x<NUM_INDIRECT_REGS;x++)
2187
				regs.indirect[x] = wctdm_proslic_getreg_indirect(wc, chan->chanpos -1, x);
2188
			for (x=0;x<NUM_REGS;x++)
2189
				regs.direct[x] = wctdm_getreg(wc, chan->chanpos - 1, x);
2190
		} else {
2191
			memset(&regs, 0, sizeof(regs));
2192
			for (x=0;x<NUM_FXO_REGS;x++)
2193
				regs.direct[x] = wctdm_getreg(wc, chan->chanpos - 1, x);
2194
		}
2195
		if (copy_to_user((__user void *)data, &regs, sizeof(regs)))
2196
			return -EFAULT;
2197
		break;
2198
	case WCTDM_SET_REG:
2199
		if (copy_from_user(&regop, (__user void *)data, sizeof(regop)))
2200
			return -EFAULT;
2201
		if (regop.indirect) {
2202
			if (wc->modtype[chan->chanpos - 1] != MOD_TYPE_FXS)
2203
				return -EINVAL;
2204
			printk(KERN_INFO  "Setting indirect %d to 0x%04x on %d\n", regop.reg, regop.val, chan->chanpos);
2205
			wctdm_proslic_setreg_indirect(wc, chan->chanpos - 1, regop.reg, regop.val);
2206
		} else {
2207
			regop.val &= 0xff;
2208
			printk(KERN_INFO  "Setting direct %d to %04x on %d\n", regop.reg, regop.val, chan->chanpos);
2209
			wctdm_setreg(wc, chan->chanpos - 1, regop.reg, regop.val);
2210
		}
2211
		break;
2212
	case WCTDM_SET_ECHOTUNE:
2213
		printk(KERN_INFO  "-- Setting echo registers: \n");
2214
		if (copy_from_user(&echoregs, (__user void *)data, sizeof(echoregs)))
2215
			return -EFAULT;
2216
2217
		if (wc->modtype[chan->chanpos - 1] == MOD_TYPE_FXO) {
2218
			/* Set the ACIM register */
2219
			wctdm_setreg(wc, chan->chanpos - 1, 30, (fxofullscale==1) ? (echoregs.acim|0x10) : echoregs.acim);
2220
2221
			/* Set the digital echo canceller registers */
2222
			wctdm_setreg(wc, chan->chanpos - 1, 45, echoregs.coef1);
2223
			wctdm_setreg(wc, chan->chanpos - 1, 46, echoregs.coef2);
2224
			wctdm_setreg(wc, chan->chanpos - 1, 47, echoregs.coef3);
2225
			wctdm_setreg(wc, chan->chanpos - 1, 48, echoregs.coef4);
2226
			wctdm_setreg(wc, chan->chanpos - 1, 49, echoregs.coef5);
2227
			wctdm_setreg(wc, chan->chanpos - 1, 50, echoregs.coef6);
2228
			wctdm_setreg(wc, chan->chanpos - 1, 51, echoregs.coef7);
2229
			wctdm_setreg(wc, chan->chanpos - 1, 52, echoregs.coef8);
2230
2231
			printk(KERN_INFO  "-- Set echo registers successfully\n");
2232
2233
			break;
2234
		} else {
2235
			return -EINVAL;
2236
2237
		}
2238
		break;
2239
	case DAHDI_SET_HWGAIN:
2240
		if (copy_from_user(&hwgain, (__user void *) data, sizeof(hwgain)))
2241
			return -EFAULT;
2242
2243
		wctdm_set_hwgain(wc, chan->chanpos-1, hwgain.newgain, hwgain.tx);
2244
2245
		if (debug)
2246
			printk(KERN_DEBUG  "Setting hwgain on channel %d to %d for %s direction\n", 
2247
				chan->chanpos-1, hwgain.newgain, hwgain.tx ? "tx" : "rx");
2248
		break;
2249
	default:
2250
		return -ENOTTY;
2251
	}
2252
	return 0;
2253
2254
}
2255
2256
static int wctdm_open(struct dahdi_chan *chan)
2257
{
2258
	struct wctdm *wc = chan->pvt;
2259
	if (!(wc->cardflag & (1 << (chan->chanpos - 1))))
2260
		return -ENODEV;
2261
	if (wc->dead)
2262
		return -ENODEV;
2263
	wc->usecount++;
2264
2265
	/*MOD_INC_USE_COUNT; */
2266
	try_module_get(THIS_MODULE);
2267
	return 0;
2268
}
2269
2270
static int wctdm_watchdog(struct dahdi_span *span, int event)
2271
{
2272
	printk(KERN_INFO "opvxa1200: Restarting DMA\n");
2273
	wctdm_restart_dma(span->pvt);
2274
	return 0;
2275
}
2276
2277
static int wctdm_close(struct dahdi_chan *chan)
2278
{
2279
	struct wctdm *wc = chan->pvt;
2280
	wc->usecount--;
2281
2282
	/*MOD_DEC_USE_COUNT;*/
2283
	module_put(THIS_MODULE);
2284
2285
	if (wc->modtype[chan->chanpos - 1] == MOD_TYPE_FXS) {
2286
		if (reversepolarity)
2287
			wc->mod[chan->chanpos - 1].fxs.idletxhookstate = 5;
2288
		else
2289
			wc->mod[chan->chanpos - 1].fxs.idletxhookstate = 1;
2290
	}
2291
	/* If we're dead, release us now */
2292
	if (!wc->usecount && wc->dead) 
2293
		wctdm_release(wc);
2294
	return 0;
2295
}
2296
2297
static int wctdm_hooksig(struct dahdi_chan *chan, enum dahdi_txsig txsig)
2298
{
2299
	struct wctdm *wc = chan->pvt;
2300
	int reg=0;
2301
	if (wc->modtype[chan->chanpos - 1] == MOD_TYPE_FXO) {
2302
		/* XXX Enable hooksig for FXO XXX */
2303
		switch(txsig) {
2304
		case DAHDI_TXSIG_START:
2305
		case DAHDI_TXSIG_OFFHOOK:
2306
			wc->mod[chan->chanpos - 1].fxo.offhook = 1;
2307
			wctdm_setreg(wc, chan->chanpos - 1, 5, 0x9);
2308
			if(cidbeforering)
2309
			{
2310
				wc->cid_state[chan->chanpos - 1] = CID_STATE_IDLE;
2311
				wc->cid_history_clone_cnt[chan->chanpos - 1] = 0;
2312
				wc->cid_history_ptr[chan->chanpos - 1] = 0;
2313
				memset(wc->cid_history_buf[chan->chanpos - 1], DAHDI_LIN2X(0, chan), cidbuflen * DAHDI_MAX_CHUNKSIZE);
2314
			}
2315
			break;
2316
		case DAHDI_TXSIG_ONHOOK:
2317
			wc->mod[chan->chanpos - 1].fxo.offhook = 0;
2318
			wctdm_setreg(wc, chan->chanpos - 1, 5, 0x8);
2319
			break;
2320
		default:
2321
			printk(KERN_NOTICE "wcfxo: Can't set tx state to %d\n", txsig);
2322
		}
2323
	} else {
2324
		switch(txsig) {
2325
		case DAHDI_TXSIG_ONHOOK:
2326
			switch(chan->sig) {
2327
			case DAHDI_SIG_EM:
2328
			case DAHDI_SIG_FXOKS:
2329
			case DAHDI_SIG_FXOLS:
2330
				wc->mod[chan->chanpos-1].fxs.lasttxhook = wc->mod[chan->chanpos-1].fxs.idletxhookstate;
2331
				break;
2332
			case DAHDI_SIG_FXOGS:
2333
				wc->mod[chan->chanpos-1].fxs.lasttxhook = 3;
2334
				break;
2335
			}
2336
			break;
2337
		case DAHDI_TXSIG_OFFHOOK:
2338
			switch(chan->sig) {
2339
			case DAHDI_SIG_EM:
2340
				wc->mod[chan->chanpos-1].fxs.lasttxhook = 5;
2341
				break;
2342
			default:
2343
				wc->mod[chan->chanpos-1].fxs.lasttxhook = wc->mod[chan->chanpos-1].fxs.idletxhookstate;
2344
				break;
2345
			}
2346
			break;
2347
		case DAHDI_TXSIG_START:
2348
			wc->mod[chan->chanpos-1].fxs.lasttxhook = 4;
2349
			break;
2350
		case DAHDI_TXSIG_KEWL:
2351
			wc->mod[chan->chanpos-1].fxs.lasttxhook = 0;
2352
			break;
2353
		default:
2354
			printk(KERN_NOTICE "opvxa1200: Can't set tx state to %d\n", txsig);
2355
		}
2356
		if (debug)
2357
			printk(KERN_DEBUG "Setting FXS hook state to %d (%02x)\n", txsig, reg);
2358
2359
#if 1
2360
		wctdm_setreg(wc, chan->chanpos - 1, 64, wc->mod[chan->chanpos-1].fxs.lasttxhook);
2361
#endif
2362
	}
2363
	return 0;
2364
}
2365
2366
static int wctdm_initialize(struct wctdm *wc)
2367
{
2368
	int x;
2369
2370
	/* Dahdi stuff */
2371
	sprintf(wc->span.name, "OPVXA1200/%d", wc->pos);
2372
	snprintf(wc->span.desc, sizeof(wc->span.desc)-1, "%s Board %d", wc->variety, wc->pos + 1);
2373
	snprintf(wc->span.location, sizeof(wc->span.location) - 1,
2374
		 "PCI Bus %02d Slot %02d", wc->dev->bus->number, PCI_SLOT(wc->dev->devfn) + 1);
2375
	wc->span.manufacturer = "OpenVox";
2376
	dahdi_copy_string(wc->span.devicetype, wc->variety, sizeof(wc->span.devicetype));
2377
	if (alawoverride) {
2378
		printk(KERN_INFO "ALAW override parameter detected.  Device will be operating in ALAW\n");
2379
		wc->span.deflaw = DAHDI_LAW_ALAW;
2380
	} else
2381
		wc->span.deflaw = DAHDI_LAW_MULAW;
2382
		
2383
	x = __wctdm_getcreg(wc, WC_VER);
2384
	wc->fwversion = x;
2385
	if( x & FLAG_A800)
2386
	{
2387
		wc->card_name = A800P_Name;
2388
		wc->max_cards = 8;
2389
	}
2390
	else
2391
	{
2392
		wc->card_name = A1200P_Name;
2393
		wc->max_cards = 12;
2394
	}
2395
		
2396
	for (x = 0; x < wc->max_cards/*MAX_NUM_CARDS*/; x++) {
2397
		sprintf(wc->chans[x]->name, "OPVXA1200/%d/%d", wc->pos, x);
2398
		wc->chans[x]->sigcap = DAHDI_SIG_FXOKS | DAHDI_SIG_FXOLS | DAHDI_SIG_FXOGS | DAHDI_SIG_SF | DAHDI_SIG_EM | DAHDI_SIG_CLEAR;
2399
		wc->chans[x]->sigcap |= DAHDI_SIG_FXSKS | DAHDI_SIG_FXSLS | DAHDI_SIG_SF | DAHDI_SIG_CLEAR;
2400
		wc->chans[x]->chanpos = x+1;
2401
		wc->chans[x]->pvt = wc;
2402
	}
2403
	wc->span.chans = wc->chans;
2404
	wc->span.channels = wc->max_cards;	/*MAX_NUM_CARDS;*/
2405
	wc->span.hooksig = wctdm_hooksig;
2406
	wc->span.irq = wc->dev->irq;
2407
	wc->span.open = wctdm_open;
2408
	wc->span.close = wctdm_close;
2409
	wc->span.flags = DAHDI_FLAG_RBS;
2410
	wc->span.ioctl = wctdm_ioctl;
2411
	wc->span.watchdog = wctdm_watchdog;
2412
	init_waitqueue_head(&wc->span.maintq);
2413
2414
	wc->span.pvt = wc;
2415
	if (dahdi_register(&wc->span, 0)) {
2416
		printk(KERN_NOTICE "Unable to register span with Dahdi\n");
2417
		return -1;
2418
	}
2419
	return 0;
2420
}
2421
2422
static void wctdm_post_initialize(struct wctdm *wc)
2423
{
2424
	int x;
2425
2426
	/* Finalize signalling  */
2427
	for (x = 0; x < wc->max_cards/*MAX_NUM_CARDS*/; x++) {
2428
		if (wc->cardflag & (1 << x)) {
2429
			if (wc->modtype[x] == MOD_TYPE_FXO)
2430
				wc->chans[x]->sigcap = DAHDI_SIG_FXSKS | DAHDI_SIG_FXSLS | DAHDI_SIG_SF | DAHDI_SIG_CLEAR;
2431
			else
2432
				wc->chans[x]->sigcap = DAHDI_SIG_FXOKS | DAHDI_SIG_FXOLS | DAHDI_SIG_FXOGS | DAHDI_SIG_SF | DAHDI_SIG_EM | DAHDI_SIG_CLEAR;
2433
		} else if (!(wc->chans[x]->sigcap & DAHDI_SIG_BROKEN)) {
2434
			wc->chans[x]->sigcap = 0;
2435
		}
2436
	}
2437
}
2438
2439
static int wctdm_hardware_init(struct wctdm *wc)
2440
{
2441
	/* Hardware stuff */
2442
	unsigned char ver;
2443
	unsigned char x,y;
2444
	int failed;
2445
	long origjiffies; //ml.
2446
	
2447
	/* Signal Reset */
2448
	printk("before raise reset\n");
2449
	outb(0x01, wc->ioaddr + WC_CNTL);
2450
2451
	/* Wait for 5 second */
2452
	
2453
	origjiffies = jiffies;
2454
2455
	while(1) 
2456
	{
2457
		if ((jiffies - origjiffies) >= (HZ*5))
2458
			break;;
2459
	}
2460
2461
	/* printk(KERN_INFO "after raise reset\n");*/
2462
2463
	/* Check OpenVox chip */
2464
	x=inb(wc->ioaddr + WC_CNTL);
2465
	ver = __wctdm_getcreg(wc, WC_VER);
2466
	wc->fwversion = ver;
2467
	/*if( ver & FLAG_A800)
2468
	{
2469
		wc->card_name = A800P_Name;
2470
		wc->max_cards = 8;
2471
	}
2472
	else
2473
	{
2474
		wc->card_name = A1200P_Name;
2475
		wc->max_cards = 12;
2476
	}*/
2477
	printk(KERN_NOTICE "OpenVox %s version: %01x.%01x\n", wc->card_name, (ver&(~FLAG_A800))>>4, ver&0x0f);
2478
	
2479
	failed = 0;
2480
	if (ver != 0x00) {
2481
		for (x=0;x<16;x++) {
2482
			/* Test registers */
2483
			__wctdm_setcreg(wc, WC_CS, x);
2484
			y = __wctdm_getcreg(wc, WC_CS) & 0x0f;
2485
			if (x != y) {
2486
				printk(KERN_INFO "%02x != %02x\n", x, y);
2487
				failed++;
2488
			}
2489
		}
2490
2491
		if (!failed) {
2492
			printk(KERN_INFO "OpenVox %s passed register test\n", wc->card_name);
2493
		} else {
2494
			printk(KERN_NOTICE "OpenVox %s failed register test\n", wc->card_name);
2495
			return -1;
2496
		}
2497
	} else {
2498
		printk(KERN_INFO "No OpenVox chip %02x\n", ver);
2499
	}
2500
2501
	if (spibyhw)
2502
		__wctdm_setcreg(wc, WC_SPICTRL, BIT_SPI_BYHW);	// spi controled by hw MiaoLin;
2503
	else
2504
		__wctdm_setcreg(wc, WC_SPICTRL, 0);	
2505
		
2506
	/* Reset PCI Interface chip and registers (and serial) */
2507
	outb(0x06, wc->ioaddr + WC_CNTL);
2508
	/* Setup our proper outputs for when we switch for our "serial" port */
2509
	wc->ios = BIT_CS | BIT_SCLK | BIT_SDI;
2510
2511
	outb(wc->ios, wc->ioaddr + WC_AUXD);
2512
2513
	/* Set all to outputs except AUX 5, which is an input */
2514
	outb(0xdf, wc->ioaddr + WC_AUXC);
2515
2516
	/* Select alternate function for AUX0 */  /* Useless in OpenVox by MiaoLin. */
2517
	/* outb(0x4, wc->ioaddr + WC_AUXFUNC); */
2518
	
2519
	/* Wait 1/4 of a sec */
2520
	wait_just_a_bit(HZ/4);
2521
2522
	/* Back to normal, with automatic DMA wrap around */
2523
	outb(0x30 | 0x01, wc->ioaddr + WC_CNTL);
2524
	wc->ledstate = 0;
2525
	wctdm_set_led(wc, 0, 0);
2526
	
2527
	/* Make sure serial port and DMA are out of reset */
2528
	outb(inb(wc->ioaddr + WC_CNTL) & 0xf9, wc->ioaddr + WC_CNTL);
2529
	
2530
	/* Configure serial port for MSB->LSB operation */
2531
	outb(0xc1, wc->ioaddr + WC_SERCTL);
2532
2533
	/* Delay FSC by 0 so it's properly aligned */
2534
	outb(0x01, wc->ioaddr + WC_FSCDELAY);  /* Modify to 1 by MiaoLin */
2535
2536
	/* Setup DMA Addresses */
2537
	outl(wc->writedma,                    wc->ioaddr + WC_DMAWS);		/* Write start */
2538
	outl(wc->writedma + DAHDI_CHUNKSIZE * 4 * 4 - 4, wc->ioaddr + WC_DMAWI);		/* Middle (interrupt) */
2539
	outl(wc->writedma + DAHDI_CHUNKSIZE * 8 * 4 - 4, wc->ioaddr + WC_DMAWE);			/* End */
2540
	
2541
	outl(wc->readdma,                    	 wc->ioaddr + WC_DMARS);	/* Read start */
2542
	outl(wc->readdma + DAHDI_CHUNKSIZE * 4 * 4 - 4, 	 wc->ioaddr + WC_DMARI);	/* Middle (interrupt) */
2543
	outl(wc->readdma + DAHDI_CHUNKSIZE * 8 * 4 - 4, wc->ioaddr + WC_DMARE);	/* End */
2544
	
2545
	/* Clear interrupts */
2546
	outb(0xff, wc->ioaddr + WC_INTSTAT);
2547
2548
	/* Wait 1/4 of a second more */
2549
	wait_just_a_bit(HZ/4);
2550
2551
	for (x = 0; x < wc->max_cards/*MAX_NUM_CARDS*/; x++) {
2552
		int sane=0,ret=0,readi=0;
2553
#if 1
2554
		touch_softlockup_watchdog();  // avoid showing CPU softlock message
2555
		/* Init with Auto Calibration */
2556
		if (!(ret=wctdm_init_proslic(wc, x, 0, 0, sane))) {
2557
			wc->cardflag |= (1 << x);
2558
                        if (debug) {
2559
                                readi = wctdm_getreg(wc,x,LOOP_I_LIMIT);
2560
                                printk("Proslic module %d loop current is %dmA\n",x,
2561
                                ((readi*3)+20));
2562
                        }
2563
			printk(KERN_INFO "Module %d: Installed -- AUTO FXS/DPO\n",x);
2564
			wctdm_set_led(wc, (unsigned int)x, 1);
2565
		} else {
2566
			if(ret!=-2) {
2567
				sane=1;
2568
				
2569
				printk(KERN_INFO "Init ProSlic with Manual Calibration \n");
2570
				/* Init with Manual Calibration */
2571
				if (!wctdm_init_proslic(wc, x, 0, 1, sane)) {
2572
					wc->cardflag |= (1 << x);
2573
                                if (debug) {
2574
                                        readi = wctdm_getreg(wc,x,LOOP_I_LIMIT);
2575
                                        printk("Proslic module %d loop current is %dmA\n",x,
2576
                                        ((readi*3)+20));
2577
                                }
2578
					printk(KERN_INFO "Module %d: Installed -- MANUAL FXS\n",x);
2579
				} else {
2580
					printk(KERN_NOTICE "Module %d: FAILED FXS (%s)\n", x, fxshonormode ? fxo_modes[_opermode].name : "FCC");
2581
					wc->chans[x]->sigcap = __DAHDI_SIG_FXO | DAHDI_SIG_BROKEN;
2582
				} 
2583
			} else if (!(ret = wctdm_init_voicedaa(wc, x, 0, 0, sane))) {
2584
				wc->cardflag |= (1 << x);
2585
				printk(KERN_INFO "Module %d: Installed -- AUTO FXO (%s mode)\n",x, fxo_modes[_opermode].name);
2586
				wctdm_set_led(wc, (unsigned int)x, 1);
2587
			} else
2588
				printk(KERN_NOTICE "Module %d: Not installed\n", x);
2589
		}
2590
#endif
2591
	}
2592
2593
	/* Return error if nothing initialized okay. */
2594
	if (!wc->cardflag && !timingonly)
2595
		return -1;
2596
	/*__wctdm_setcreg(wc, WC_SYNC, (wc->cardflag << 1) | 0x1); */  /* removed by MiaoLin */
2597
	return 0;
2598
}
2599
2600
static void wctdm_enable_interrupts(struct wctdm *wc)
2601
{
2602
	/* Clear interrupts */
2603
	outb(0xff, wc->ioaddr + WC_INTSTAT);
2604
2605
	/* Enable interrupts (we care about all of them) */
2606
	outb(0x3c, wc->ioaddr + WC_MASK0);
2607
	/* No external interrupts */
2608
	outb(0x00, wc->ioaddr + WC_MASK1);
2609
}
2610
2611
static void wctdm_restart_dma(struct wctdm *wc)
2612
{
2613
	/* Reset Master and TDM */
2614
	outb(0x01, wc->ioaddr + WC_CNTL);
2615
	outb(0x01, wc->ioaddr + WC_OPER);
2616
}
2617
2618
static void wctdm_start_dma(struct wctdm *wc)
2619
{
2620
	/* Reset Master and TDM */
2621
	outb(0x0f, wc->ioaddr + WC_CNTL);
2622
	set_current_state(TASK_INTERRUPTIBLE);
2623
	schedule_timeout(1);
2624
	outb(0x01, wc->ioaddr + WC_CNTL);
2625
	outb(0x01, wc->ioaddr + WC_OPER);
2626
}
2627
2628
static void wctdm_stop_dma(struct wctdm *wc)
2629
{
2630
	outb(0x00, wc->ioaddr + WC_OPER);
2631
}
2632
2633
static void wctdm_reset_tdm(struct wctdm *wc)
2634
{
2635
	/* Reset TDM */
2636
	outb(0x0f, wc->ioaddr + WC_CNTL);
2637
}
2638
2639
static void wctdm_disable_interrupts(struct wctdm *wc)	
2640
{
2641
	outb(0x00, wc->ioaddr + WC_MASK0);
2642
	outb(0x00, wc->ioaddr + WC_MASK1);
2643
}
2644
2645
static int __devinit wctdm_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
2646
{
2647
	int res;
2648
	struct wctdm *wc;
2649
	struct wctdm_desc *d = (struct wctdm_desc *)ent->driver_data;
2650
	int x;
2651
	int y;
2652
2653
	static int initd_ifaces=0;
2654
	
2655
	if(initd_ifaces){
2656
		memset((void *)ifaces,0,(sizeof(struct wctdm *))*WC_MAX_IFACES);
2657
		initd_ifaces=1;
2658
	}
2659
	for (x=0;x<WC_MAX_IFACES;x++)
2660
		if (!ifaces[x]) break;
2661
	if (x >= WC_MAX_IFACES) {
2662
		printk(KERN_NOTICE "Too many interfaces\n");
2663
		return -EIO;
2664
	}
2665
	
2666
	if (pci_enable_device(pdev)) {
2667
		res = -EIO;
2668
	} else {
2669
		wc = kmalloc(sizeof(struct wctdm), GFP_KERNEL);
2670
		if (wc) {
2671
			int cardcount = 0;
2672
			
2673
			wc->lastchan = -1;	/* first channel offset = -1; */
2674
			wc->ledstate = 0;
2675
			
2676
			ifaces[x] = wc;
2677
			memset(wc, 0, sizeof(struct wctdm));
2678
			for (x=0; x < sizeof(wc->chans)/sizeof(wc->chans[0]); ++x) {
2679
				wc->chans[x] = &wc->_chans[x];
2680
			}
2681
2682
			spin_lock_init(&wc->lock);
2683
			wc->curcard = -1;
2684
			wc->ioaddr = pci_resource_start(pdev, 0);
2685
			wc->mem_region = pci_resource_start(pdev, 1);
2686
			wc->mem_len = pci_resource_len(pdev, 1);
2687
			wc->mem32 = (unsigned long)ioremap(wc->mem_region, wc->mem_len);
2688
			wc->dev = pdev;
2689
			wc->pos = x;
2690
			wc->variety = d->name;
2691
			for (y=0;y<MAX_NUM_CARDS;y++)
2692
				wc->flags[y] = d->flags;
2693
			/* Keep track of whether we need to free the region */
2694
			if (request_region(wc->ioaddr, 0xff, "opvxa1200")) 
2695
				wc->freeregion = 1;
2696
			else
2697
				wc->freeregion = 0;
2698
			
2699
			if (request_mem_region(wc->mem_region, wc->mem_len, "opvxa1200"))
2700
				wc->freeregion |= 0x02;
2701
2702
			/* Allocate enough memory for two zt chunks, receive and transmit.  Each sample uses
2703
			   8 bits.  */
2704
			wc->writechunk = pci_alloc_consistent(pdev, DAHDI_MAX_CHUNKSIZE * (MAX_NUM_CARDS+NUM_FLAG) * 2 * 2, &wc->writedma);
2705
			if (!wc->writechunk) {
2706
				printk(KERN_NOTICE "opvxa1200: Unable to allocate DMA-able memory\n");
2707
				if (wc->freeregion & 0x01)
2708
					release_region(wc->ioaddr, 0xff);
2709
				if (wc->freeregion & 0x02)
2710
				{
2711
					release_mem_region(wc->mem_region, wc->mem_len);
2712
					iounmap((void *)wc->mem32);
2713
				}
2714
				return -ENOMEM;
2715
			}
2716
2717
			wc->readchunk = wc->writechunk + DAHDI_MAX_CHUNKSIZE * (MAX_NUM_CARDS+NUM_FLAG) * 2;	/* in bytes */
2718
			wc->readdma = wc->writedma + DAHDI_MAX_CHUNKSIZE * (MAX_NUM_CARDS+NUM_FLAG) * 2;	/* in bytes */
2719
			
2720
			if (wctdm_initialize(wc)) {
2721
				printk(KERN_NOTICE "opvxa1200: Unable to intialize FXS\n");
2722
				/* Set Reset Low */
2723
				x=inb(wc->ioaddr + WC_CNTL);
2724
				outb((~0x1)&x, wc->ioaddr + WC_CNTL);
2725
				/* Free Resources */
2726
				free_irq(pdev->irq, wc);
2727
				if (wc->freeregion & 0x01)
2728
					release_region(wc->ioaddr, 0xff);
2729
				if (wc->freeregion & 0x02)
2730
				{
2731
					release_mem_region(wc->mem_region, wc->mem_len);
2732
					iounmap((void *)wc->mem32);
2733
				}
2734
			}
2735
2736
			/* Enable bus mastering */
2737
			pci_set_master(pdev);
2738
2739
			/* Keep track of which device we are */
2740
			pci_set_drvdata(pdev, wc);
2741
2742
2743
			if (request_irq(pdev->irq, wctdm_interrupt, DAHDI_IRQ_SHARED, "opvxa1200", wc)) {
2744
				printk(KERN_NOTICE "opvxa1200: Unable to request IRQ %d\n", pdev->irq);
2745
				if (wc->freeregion & 0x01)
2746
					release_region(wc->ioaddr, 0xff);
2747
				if (wc->freeregion & 0x02)
2748
				{
2749
					release_mem_region(wc->mem_region, wc->mem_len);
2750
					iounmap((void *)wc->mem32);
2751
				}
2752
				pci_free_consistent(pdev,  DAHDI_MAX_CHUNKSIZE * (MAX_NUM_CARDS+NUM_FLAG) * 2 * 2, (void *)wc->writechunk, wc->writedma);
2753
				pci_set_drvdata(pdev, NULL);
2754
				kfree(wc);
2755
				return -EIO;
2756
			}
2757
2758
			if (wctdm_hardware_init(wc)) {
2759
				unsigned char w;
2760
2761
				/* Set Reset Low */
2762
				w=inb(wc->ioaddr + WC_CNTL);
2763
				outb((~0x1)&w, wc->ioaddr + WC_CNTL);
2764
				/* Free Resources */
2765
				free_irq(pdev->irq, wc);
2766
				if (wc->freeregion & 0x01)
2767
					release_region(wc->ioaddr, 0xff);
2768
				if (wc->freeregion & 0x02)
2769
				{
2770
					release_mem_region(wc->mem_region, wc->mem_len);
2771
					iounmap((void *)wc->mem32);
2772
				}
2773
				pci_free_consistent(pdev,  DAHDI_MAX_CHUNKSIZE * (MAX_NUM_CARDS+NUM_FLAG) * 2 * 2, (void *)wc->writechunk, wc->writedma);
2774
				pci_set_drvdata(pdev, NULL);
2775
				dahdi_unregister(&wc->span);
2776
				kfree(wc);
2777
				return -EIO;
2778
2779
			}
2780
2781
#ifdef TEST_LOG_INCOME_VOICE
2782
			for(x=0; x<MAX_NUM_CARDS+NUM_FLAG; x++)
2783
			{
2784
				wc->voc_buf[x] = kmalloc(voc_buffer_size, GFP_KERNEL);
2785
				wc->voc_ptr[x] = 0;
2786
			}
2787
#endif
2788
2789
			if(cidbeforering) 
2790
			{		
2791
				int len = cidbuflen * DAHDI_MAX_CHUNKSIZE;
2792
				if(debug)
2793
					printk("cidbeforering support enabled, length is %d msec\n", cidbuflen);
2794
				for (x = 0; x < wc->max_cards/*MAX_NUM_CARDS*/; x++) 
2795
				{
2796
					wc->cid_history_buf[x] = kmalloc(len, GFP_KERNEL);
2797
					wc->cid_history_ptr[x] = 0;
2798
					wc->cid_history_clone_cnt[x] = 0;
2799
					wc->cid_state[x] = CID_STATE_IDLE;
2800
				}
2801
			}
2802
			
2803
			wctdm_post_initialize(wc);
2804
2805
			/* Enable interrupts */
2806
			wctdm_enable_interrupts(wc);
2807
			/* Initialize Write/Buffers to all blank data */
2808
			memset((void *)wc->writechunk,0, DAHDI_MAX_CHUNKSIZE * (MAX_NUM_CARDS+NUM_FLAG) * 2 * 2);
2809
2810
			/* Start DMA */
2811
			wctdm_start_dma(wc);
2812
2813
			for (x = 0; x < wc->max_cards/*MAX_NUM_CARDS*/; x++) {
2814
				if (wc->cardflag & (1 << x))
2815
					cardcount++;
2816
			}
2817
2818
			printk(KERN_INFO "Found an OpenVox %s: Version %x.%x (%d modules)\n", wc->card_name, (wc->fwversion&(~FLAG_A800))>>4, wc->fwversion&0x0f, cardcount);
2819
			if(debug)
2820
				printk(KERN_DEBUG "OpenVox %s debug On\n", wc->card_name);
2821
			
2822
			res = 0;
2823
		} else
2824
			res = -ENOMEM;
2825
	}
2826
	return res;
2827
}
2828
2829
static void wctdm_release(struct wctdm *wc)
2830
{
2831
#ifdef TEST_LOG_INCOME_VOICE
2832
	struct file * f = NULL;
2833
	mm_segment_t orig_fs;
2834
	int i;
2835
	char fname[20];
2836
#endif
2837
	
2838
	dahdi_unregister(&wc->span);
2839
	if (wc->freeregion & 0x01)
2840
		release_region(wc->ioaddr, 0xff);
2841
	if (wc->freeregion & 0x02)
2842
	{
2843
		release_mem_region(wc->mem_region, wc->mem_len);
2844
		iounmap((void *)wc->mem32);
2845
	}
2846
	
2847
#ifdef TEST_LOG_INCOME_VOICE
2848
	for(i=0; i<MAX_NUM_CARDS + NUM_FLAG; i++)
2849
	{
2850
		sprintf(fname, "//usr//%d.pcm", i); 
2851
		f = filp_open(fname, O_RDWR|O_CREAT, 00);
2852
	
2853
		if (!f || !f->f_op || !f->f_op->read)
2854
		{
2855
			printk("WARNING: File (read) object is a null pointer!!!\n");
2856
			continue;
2857
		}
2858
	
2859
		f->f_pos = 0;
2860
		
2861
		orig_fs = get_fs();
2862
		set_fs(KERNEL_DS); 
2863
		
2864
		if(wc->voc_buf[i])
2865
		{
2866
			f->f_op->write(f, wc->voc_buf[i], voc_buffer_size, &f->f_pos);
2867
			kfree(wc->voc_buf[i]);
2868
		}
2869
		
2870
		set_fs(orig_fs); 
2871
		fput(f);
2872
	}
2873
#endif
2874
 
2875
	if(cidbeforering) 
2876
	{
2877
		int x;
2878
		for (x = 0; x < wc->max_cards/*MAX_NUM_CARDS*/; x++) 
2879
			kfree(wc->cid_history_buf[x]);
2880
	}
2881
 
2882
	kfree(wc);
2883
	printk(KERN_INFO "Freed a OpenVox A1200 card\n");
2884
}
2885
2886
static void __devexit wctdm_remove_one(struct pci_dev *pdev)
2887
{
2888
	struct wctdm *wc = pci_get_drvdata(pdev);
2889
	if (wc) {
2890
2891
		/* Stop any DMA */
2892
		wctdm_stop_dma(wc);
2893
		wctdm_reset_tdm(wc);
2894
2895
		/* In case hardware is still there */
2896
		wctdm_disable_interrupts(wc);
2897
		
2898
		/* Immediately free resources */
2899
		pci_free_consistent(pdev,  DAHDI_MAX_CHUNKSIZE * (MAX_NUM_CARDS+NUM_FLAG) * 2 * 2, (void *)wc->writechunk, wc->writedma);
2900
		free_irq(pdev->irq, wc);
2901
2902
		/* Reset PCI chip and registers */
2903
		if(wc->fwversion > 0x11)
2904
			outb(0x0e, wc->ioaddr + WC_CNTL);
2905
		else
2906
		{
2907
			wc->ledstate = 0;
2908
			wctdm_set_led(wc,0,0);	// power off all leds.
2909
		}
2910
2911
		/* Release span, possibly delayed */
2912
		if (!wc->usecount)
2913
			wctdm_release(wc);
2914
		else
2915
			wc->dead = 1;
2916
	}
2917
}
2918
2919
static struct pci_device_id wctdm_pci_tbl[] = {
2920
	{ 0xe159, 0x0001, 0x9100, PCI_ANY_ID, 0, 0, (unsigned long) &wctdme },
2921
	{ 0xe159, 0x0001, 0x9519, PCI_ANY_ID, 0, 0, (unsigned long) &wctdme },
2922
	{ 0xe159, 0x0001, 0x95D9, PCI_ANY_ID, 0, 0, (unsigned long) &wctdme },
2923
	{ 0xe159, 0x0001, 0x9500, PCI_ANY_ID, 0, 0, (unsigned long) &wctdme },
2924
	{ 0xe159, 0x0001, 0x9532, PCI_ANY_ID, 0, 0, (unsigned long) &wctdme }, 
2925
	{ 0xe159, 0x0001, 0x8519, PCI_ANY_ID, 0, 0, (unsigned long) &wctdme },
2926
	{ 0xe159, 0x0001, 0x9559, PCI_ANY_ID, 0, 0, (unsigned long) &wctdme },
2927
	{ 0xe159, 0x0001, 0x9599, PCI_ANY_ID, 0, 0, (unsigned long) &wctdme },
2928
	{ 0 }
2929
};
2930
2931
MODULE_DEVICE_TABLE(pci, wctdm_pci_tbl);
2932
2933
static struct pci_driver wctdm_driver = {
2934
	.name = "opvxa1200",
2935
	.probe =	wctdm_init_one,
2936
	.remove =	__devexit_p(wctdm_remove_one),
2937
	.suspend = NULL,
2938
	.resume =	NULL,
2939
	.id_table = wctdm_pci_tbl,
2940
};
2941
2942
static int __init wctdm_init(void)
2943
{
2944
	int res;
2945
	int x;
2946
	for (x=0;x<(sizeof(fxo_modes) / sizeof(fxo_modes[0])); x++) {
2947
		if (!strcmp(fxo_modes[x].name, opermode))
2948
			break;
2949
	}
2950
	if (x < sizeof(fxo_modes) / sizeof(fxo_modes[0])) {
2951
		_opermode = x;
2952
	} else {
2953
		printk(KERN_NOTICE "Invalid/unknown operating mode '%s' specified.  Please choose one of:\n", opermode);
2954
		for (x=0;x<sizeof(fxo_modes) / sizeof(fxo_modes[0]); x++)
2955
			printk(KERN_INFO "  %s\n", fxo_modes[x].name);
2956
		printk(KERN_INFO "Note this option is CASE SENSITIVE!\n");
2957
		return -ENODEV;
2958
	}
2959
	if (!strcmp(fxo_modes[_opermode].name, "AUSTRALIA")) {
2960
		boostringer=1;
2961
		fxshonormode=1;
2962
}
2963
	if (battdebounce == 0) {
2964
		battdebounce = fxo_modes[_opermode].battdebounce;
2965
	}
2966
	if (battalarm == 0) {
2967
		battalarm = fxo_modes[_opermode].battalarm;
2968
	}
2969
	if (battthresh == 0) {
2970
		battthresh = fxo_modes[_opermode].battthresh;
2971
	}
2972
2973
	res = dahdi_pci_module(&wctdm_driver);
2974
	if (res)
2975
		return -ENODEV;
2976
	return 0;
2977
}
2978
2979
static void __exit wctdm_cleanup(void)
2980
{
2981
	pci_unregister_driver(&wctdm_driver);
2982
}
2983
2984
module_param(debug, int, 0600);
2985
module_param(loopcurrent, int, 0600);
2986
module_param(reversepolarity, int, 0600);
2987
module_param(robust, int, 0600);
2988
module_param(opermode, charp, 0600);
2989
module_param(timingonly, int, 0600);
2990
module_param(lowpower, int, 0600);
2991
module_param(boostringer, int, 0600);
2992
module_param(fastringer, int, 0600);
2993
module_param(fxshonormode, int, 0600);
2994
module_param(battdebounce, uint, 0600);
2995
module_param(battthresh, uint, 0600);
2996
module_param(battalarm, uint, 0600);
2997
module_param(ringdebounce, int, 0600);
2998
module_param(fwringdetect, int, 0600);
2999
module_param(alawoverride, int, 0600);
3000
module_param(fastpickup, int, 0600);
3001
module_param(fxotxgain, int, 0600);
3002
module_param(fxorxgain, int, 0600);
3003
module_param(fxstxgain, int, 0600);
3004
module_param(fxsrxgain, int, 0600);
3005
module_param(spibyhw, int, 0600);
3006
module_param(usememio, int, 0600);
3007
module_param(cidbeforering, int, 0600);
3008
module_param(cidbuflen, int, 0600);
3009
module_param(cidtimeout, int, 0600);
3010
module_param(fxofullscale, int, 0600);
3011
module_param(fixedtimepolarity, int, 0600);
3012
3013
MODULE_DESCRIPTION("OpenVox A1200 Driver");
3014
MODULE_AUTHOR("MiaoLin <miaolin@openvox.com.cn>");
3015
MODULE_LICENSE("GPL v2");
3016
3017
module_init(wctdm_init);
3018
module_exit(wctdm_cleanup);
(-)dahdi-linux-2.2.0.2/drivers/dahdi/wcopenpci.c (+1842 lines)
Line 0 Link Here
1
/*
2
 * Voicetronix OpenPCI Interface Driver for Zapata Telephony interface
3
 *
4
 * Written by Mark Spencer <markster@linux-support.net>
5
 *            Matthew Fredrickson <creslin@linux-support.net>
6
 *            Ben Kramer <ben@voicetronix.com.au>
7
 *            Ron Lee <ron@voicetronix.com.au>
8
 *
9
 * Copyright (C) 2001, Linux Support Services, Inc.
10
 * Copyright (C) 2005 - 2007, Voicetronix
11
 *
12
 * All rights reserved.
13
 *
14
 * This program is free software; you can redistribute it and/or modify
15
 * it under the terms of the GNU General Public License as published by
16
 * the Free Software Foundation; either version 2 of the License, or
17
 * (at your option) any later version.
18
 * 
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 * GNU General Public License for more details.
23
 * 
24
 * You should have received a copy of the GNU General Public License
25
 * along with this program; if not, write to the Free Software
26
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
27
 *
28
 */
29
30
/* Conditional debug options */
31
#define VERBOSE_TIMING 0
32
33
/* Driver constants */
34
#define DRIVER_DESCRIPTION  "Voicetronix OpenPCI DAHDI driver"
35
#define DRIVER_AUTHOR       "Mark Spencer <markster@digium.com> "\
36
                            "Voicetronix <support@voicetronix.com.au>"
37
38
#define NAME      "wcopenpci"
39
#define MAX_PORTS 8	    /* Maximum number of ports on each carrier */
40
#define MAX_CARDS 8	    /* Maximum number of carriers per host */
41
42
#define DEFAULT_COUNTRY  "AUSTRALIA"
43
44
45
#include <linux/init.h>
46
#include <linux/module.h>
47
#include <linux/pci.h>
48
#include <linux/delay.h>
49
#include <linux/sched.h>
50
51
#include <dahdi/kernel.h>
52
#include <dahdi/version.h>
53
#include "proslic.h"
54
#include <dahdi/wctdm_user.h>
55
56
57
58
/* Compatibility helpers */
59
#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
60
 #include <linux/interrupt.h>
61
#else
62
 typedef void irqreturn_t;
63
 #define IRQ_NONE
64
 #define IRQ_HANDLED
65
 #define IRQ_RETVAL(x)
66
 #define __devexit_p(x) x
67
#endif
68
69
// Centos4.3 uses a modified 2.6.9 kernel, with no indication that
70
// it is different from the mainstream (or even Centos4.2 2.6.9)
71
// kernel, so we must crowbar off the dunce-hat manually here.
72
#if !defined CENTOS4_3 && LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
73
 typedef int gfp_t;
74
 static inline void *kzalloc( size_t n, gfp_t flags ){
75
	void *p = kmalloc(n,flags);
76
	if (p) memset(p, 0, n);
77
	return p;
78
 }
79
#endif
80
81
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,16)
82
 #define DEFINE_MUTEX(x)		DECLARE_MUTEX(x)
83
 #define mutex_init(x)			init_MUTEX(x)
84
 #define mutex_lock(x)			down(x)
85
 #define mutex_lock_interruptible(x)	down_interruptible(x)
86
 #define mutex_trylock(x)		down_trylock(x)
87
 #define mutex_unlock(x)		up(x)
88
#else
89
 #include <linux/mutex.h>
90
#endif
91
92
93
static struct fxo_mode {
94
	char *name;
95
	int ohs;
96
	int ohs2;
97
	int rz;
98
	int rt;
99
	int ilim;
100
	int dcv;
101
	int mini;
102
	int acim;
103
	int ring_osc;
104
	int ring_x;
105
} fxo_modes[] =
106
{
107
	{ "FCC", 0, 0, 0, 1, 0, 0x3, 0, 0, }, 	/* US, Canada */
108
	{ "TBR21", 0, 0, 0, 0, 1, 0x3, 0, 0x2, 0x7e6c, 0x023a, },
109
		/* Austria, Belgium, Denmark, Finland, France, Germany, 
110
		   Greece, Iceland, Ireland, Italy, Luxembourg, Netherlands,
111
		   Norway, Portugal, Spain, Sweden, Switzerland, and UK */
112
	{ "ARGENTINA", 0, 0, 0, 0, 0, 0x3, 0, 0, },
113
	{ "AUSTRALIA", 1, 0, 0, 0, 0, 0, 0x3, 0x3, },
114
	{ "AUSTRIA", 0, 1, 0, 0, 1, 0x3, 0, 0x3, },
115
	{ "BAHRAIN", 0, 0, 0, 0, 1, 0x3, 0, 0x2, },
116
	{ "BELGIUM", 0, 1, 0, 0, 1, 0x3, 0, 0x2, },
117
	{ "BRAZIL", 0, 0, 0, 0, 0, 0, 0x3, 0, },
118
	{ "BULGARIA", 0, 0, 0, 0, 1, 0x3, 0x0, 0x3, },
119
	{ "CANADA", 0, 0, 0, 0, 0, 0x3, 0, 0, },
120
	{ "CHILE", 0, 0, 0, 0, 0, 0x3, 0, 0, },
121
	{ "CHINA", 0, 0, 0, 0, 0, 0, 0x3, 0xf, },
122
	{ "COLUMBIA", 0, 0, 0, 0, 0, 0x3, 0, 0, },
123
	{ "CROATIA", 0, 0, 0, 0, 1, 0x3, 0, 0x2, },
124
	{ "CYRPUS", 0, 0, 0, 0, 1, 0x3, 0, 0x2, },
125
	{ "CZECH", 0, 0, 0, 0, 1, 0x3, 0, 0x2, },
126
	{ "DENMARK", 0, 1, 0, 0, 1, 0x3, 0, 0x2, },
127
	{ "ECUADOR", 0, 0, 0, 0, 0, 0x3, 0, 0, },
128
	{ "EGYPT", 0, 0, 0, 0, 0, 0, 0x3, 0, },
129
	{ "ELSALVADOR", 0, 0, 0, 0, 0, 0x3, 0, 0, },
130
	{ "FINLAND", 0, 1, 0, 0, 1, 0x3, 0, 0x2, },
131
	{ "FRANCE", 0, 1, 0, 0, 1, 0x3, 0, 0x2, },
132
	{ "GERMANY", 0, 1, 0, 0, 1, 0x3, 0, 0x3, },
133
	{ "GREECE", 0, 1, 0, 0, 1, 0x3, 0, 0x2, },
134
	{ "GUAM", 0, 0, 0, 0, 0, 0x3, 0, 0, },
135
	{ "HONGKONG", 0, 0, 0, 0, 0, 0x3, 0, 0, },
136
	{ "HUNGARY", 0, 0, 0, 0, 0, 0x3, 0, 0, },
137
	{ "ICELAND", 0, 1, 0, 0, 1, 0x3, 0, 0x2, },
138
	{ "INDIA", 0, 0, 0, 0, 0, 0x3, 0, 0x4, },
139
	{ "INDONESIA", 0, 0, 0, 0, 0, 0x3, 0, 0, },
140
	{ "IRELAND", 0, 1, 0, 0, 1, 0x3, 0, 0x2, },
141
	{ "ISRAEL", 0, 0, 0, 0, 1, 0x3, 0, 0x2, },
142
	{ "ITALY", 0, 1, 0, 0, 1, 0x3, 0, 0x2, },
143
	{ "JAPAN", 0, 0, 0, 0, 0, 0, 0x3, 0, },
144
	{ "JORDAN", 0, 0, 0, 0, 0, 0, 0x3, 0, },
145
	{ "KAZAKHSTAN", 0, 0, 0, 0, 0, 0x3, 0, },
146
	{ "KUWAIT", 0, 0, 0, 0, 0, 0x3, 0, 0, },
147
	{ "LATVIA", 0, 0, 0, 0, 1, 0x3, 0, 0x2, },
148
	{ "LEBANON", 0, 0, 0, 0, 1, 0x3, 0, 0x2, },
149
	{ "LUXEMBOURG", 0, 1, 0, 0, 1, 0x3, 0, 0x2, },
150
	{ "MACAO", 0, 0, 0, 0, 0, 0x3, 0, 0, },
151
	{ "MALAYSIA", 0, 0, 0, 0, 0, 0, 0x3, 0, },	/* Current loop >= 20ma */
152
	{ "MALTA", 0, 0, 0, 0, 1, 0x3, 0, 0x2, },
153
	{ "MEXICO", 0, 0, 0, 0, 0, 0x3, 0, 0, },
154
	{ "MOROCCO", 0, 0, 0, 0, 1, 0x3, 0, 0x2, },
155
	{ "NETHERLANDS", 0, 1, 0, 0, 1, 0x3, 0, 0x2, },
156
	{ "NEWZEALAND", 0, 0, 0, 0, 0, 0x3, 0, 0x4, },
157
	{ "NIGERIA", 0, 0, 0, 0, 0x1, 0x3, 0, 0x2, },
158
	{ "NORWAY", 0, 1, 0, 0, 1, 0x3, 0, 0x2, },
159
	{ "OMAN", 0, 0, 0, 0, 0, 0, 0x3, 0, },
160
	{ "PAKISTAN", 0, 0, 0, 0, 0, 0, 0x3, 0, },
161
	{ "PERU", 0, 0, 0, 0, 0, 0x3, 0, 0, },
162
	{ "PHILIPPINES", 0, 0, 0, 0, 0, 0, 0x3, 0, },
163
	{ "POLAND", 0, 0, 1, 1, 0, 0x3, 0, 0, },
164
	{ "PORTUGAL", 0, 1, 0, 0, 1, 0x3, 0, 0x2, },
165
	{ "ROMANIA", 0, 0, 0, 0, 0, 3, 0, 0, },
166
	{ "RUSSIA", 0, 0, 0, 0, 0, 0, 0x3, 0, },
167
	{ "SAUDIARABIA", 0, 0, 0, 0, 0, 0x3, 0, 0, },
168
	{ "SINGAPORE", 0, 0, 0, 0, 0, 0x3, 0, 0, },
169
	{ "SLOVAKIA", 0, 0, 0, 0, 0, 0x3, 0, 0x3, },
170
	{ "SLOVENIA", 0, 0, 0, 0, 0, 0x3, 0, 0x2, },
171
	{ "SOUTHAFRICA", 1, 0, 1, 0, 0, 0x3, 0, 0x3, },
172
	{ "SOUTHKOREA", 0, 0, 0, 0, 0, 0x3, 0, 0, },
173
	{ "SPAIN", 0, 1, 0, 0, 1, 0x3, 0, 0x2, },
174
	{ "SWEDEN", 0, 1, 0, 0, 1, 0x3, 0, 0x2, },
175
	{ "SWITZERLAND", 0, 1, 0, 0, 1, 0x3, 0, 0x2, },
176
	{ "SYRIA", 0, 0, 0, 0, 0, 0, 0x3, 0, },
177
	{ "TAIWAN", 0, 0, 0, 0, 0, 0, 0x3, 0, },
178
	{ "THAILAND", 0, 0, 0, 0, 0, 0, 0x3, 0, },
179
	{ "UAE", 0, 0, 0, 0, 0, 0x3, 0, 0, },
180
	{ "UK", 0, 1, 0, 0, 1, 0x3, 0, 0x5, },
181
	{ "USA", 0, 0, 0, 0, 0, 0x3, 0, 0, },
182
	{ "YEMEN", 0, 0, 0, 0, 0, 0x3, 0, 0, },
183
};
184
185
static struct ps_country_reg {
186
	const char *country;
187
	unsigned short value;
188
} ps_country_regs[] = {
189
	{"ARGENTINA",  0x8},
190
	{"AUSTRALIA",  0xD},
191
	{"AUSTRIA",    0xD},
192
	{"BAHRAIN",    0xC},
193
	{"BELGIUM",    0xC},
194
	{"BRAZIL",     0x8},
195
	{"BULGARIA",   0xD},
196
	{"CANADA",     0x8},
197
	{"CHILE",      0x8},
198
	{"CHINA",      0xC},
199
	{"COLOMBIA",   0x8},
200
	{"CROATIA",    0xC},
201
	{"CYPRUS",     0xC},
202
	{"CZECH",      0xC},
203
	{"DENMARK",    0xC},
204
	{"ECUADOR",    0x8},
205
	{"EGYPT",      0x8},
206
	{"ELSALVADOR", 0x8},
207
	{"FINLAND",    0xC},
208
	{"FRANCE",     0xC},
209
	{"GERMANY",    0xD},
210
	{"GREECE",     0xC},
211
	{"GUAM",       0x8},
212
	{"HONGKONG",   0x8},
213
	{"HUNGARY",    0x8},
214
	{"ICELAND",    0xC},
215
	{"INDIA",      0xF},
216
	{"INDONESIA",  0x8},
217
	{"IRELAND",    0xC},
218
	{"ISRAEL",     0xC},
219
	{"ITALY",      0xC},
220
	{"JAPAN",      0x8},
221
	{"JORDAN",     0x8},
222
	{"KAZAKHSTAN", 0x8},
223
	{"KUWAIT",     0x8},
224
	{"LATVIA",     0xC},
225
	{"LEBANON",    0xC},
226
	{"LUXEMBOURG", 0xC},
227
	{"MACAO",      0x8},
228
	{"MALAYSIA",   0x8},
229
	{"MALTA",      0xC},
230
	{"MEXICO",     0x8},
231
	{"MOROCCO",    0xC},
232
	{"NETHERLANDS",0xC},
233
	{"NEWZEALAND", 0xF},
234
	{"NIGERIA",    0xC},
235
	{"NORWAY",     0xC},
236
	{"OMAN",       0x8},
237
	{"PAKISTAN",   0x8},
238
	{"PERU",       0x8},
239
	{"PHILIPPINES",0x8},
240
	{"POLAND",     0x8},
241
	{"PORTUGAL",   0xC},
242
	{"ROMANIA",    0x8},
243
	{"RUSSIA",     0x8},
244
	{"SAUDIARABIA",0x8},
245
	{"SINGAPORE",  0x8},
246
	{"SLOVAKIA",   0xE},
247
	{"SLOVENIA",   0xE},
248
	{"SOUTHAFRICA",0xE},
249
	{"SOUTHKOREA", 0x8},
250
	{"SPAIN",      0xC},
251
	{"SWEDEN",     0xC},
252
	{"SWITZERLAND",0xC},
253
	{"SYRIA",      0x8},
254
	{"TAIWAN",     0x8},
255
	{"THAILAND",   0x8},
256
	{"UAE",        0x8},
257
	{"UK",         0xC},
258
	{"USA",        0x8},
259
	{"YEMEN",      0x8}
260
};
261
262
#define INOUT 2
263
264
/* Allocate enough memory for two zt chunks, receive and transmit.  Each sample uses
265
   32 bits.  Allocate an extra set just for control too */
266
#define VT_PCIDMA_BLOCKSIZE (DAHDI_MAX_CHUNKSIZE * INOUT * MAX_PORTS * 2 * 2)
267
#define VT_PCIDMA_MIDDLE    (DAHDI_MAX_CHUNKSIZE * MAX_PORTS - 4)
268
#define VT_PCIDMA_END       (DAHDI_MAX_CHUNKSIZE * MAX_PORTS * 2 - 4)
269
270
#define ID_DATA_MAXSIZE         30
271
272
#define NUM_CAL_REGS 12
273
#define NUM_FXO_REGS 60
274
275
#define TREG(addr)      (wc->ioaddr + addr)
276
277
#define TJ_CNTL         TREG(0x00)
278
#define TJ_OPER         TREG(0x01)
279
#define TJ_AUXC         TREG(0x02)
280
#define TJ_AUXD         TREG(0x03)
281
#define TJ_MASK0        TREG(0x04)
282
#define TJ_MASK1        TREG(0x05)
283
#define TJ_INTSTAT      TREG(0x06)
284
#define TJ_AUXR         TREG(0x07)
285
286
#define TJ_DMAWS        TREG(0x08)
287
#define TJ_DMAWI        TREG(0x0c)
288
#define TJ_DMAWE        TREG(0x10)
289
#define TJ_DMAWC        TREG(0x14)
290
#define TJ_DMARS        TREG(0x18)
291
#define TJ_DMARI        TREG(0x1c)
292
#define TJ_DMARE        TREG(0x20)
293
#define TJ_DMARC        TREG(0x24)
294
295
#define TJ_AUXINTPOL    TREG(0x2A)
296
297
#define TJ_AUXFUNC      TREG(0x2b)
298
#define TJ_SFDELAY      TREG(0x2c)
299
#define TJ_SERCTL       TREG(0x2d)
300
#define TJ_SFLC         TREG(0x2e)
301
#define TJ_FSCDELAY     TREG(0x2f)
302
303
#define TJ_REGBASE      TREG(0xc0)
304
305
#define PIB(addr)       (TJ_REGBASE + addr * 4)
306
307
#define HTXF_READY      (inb(PIB(0)) & 0x10)
308
#define HRXF_READY      (inb(PIB(0)) & 0x20)
309
310
311
#define VT_PORT_EMPTY	0
312
#define VT_PORT_VDAA	1   /* Voice DAA - FXO */
313
#define VT_PORT_PROSLIC	2   /* ProSLIC - FXS */
314
315
#define VBAT 0xC7
316
317
#define HKMODE_FWDACT   1
318
#define HKMODE_FWDONACT	2
319
#define HKMODE_RINGING	4
320
321
#define HOOK_ONHOOK     0
322
#define HOOK_OFFHOOK    1
323
324
#define	DSP_CODEC_RING		12	/* RING rising edge detected		*/
325
#define	DSP_CODEC_HKOFF		22	/* station port off hook                */
326
#define	DSP_CODEC_HKON		23	/* station port on hook                 */
327
#define	DSP_RING_OFF		24	/* RING falling edge detected		*/
328
#define DSP_DROP		25
329
330
#define	DSP_CODEC_FLASH		26	/* station port hook flash              */
331
332
#define DSP_LOOP_OFFHOOK	38	/* Loop Off hook from OpenPCI           */
333
#define DSP_LOOP_ONHOOK		39	/* Loop On hook from OpenPCI            */
334
#define DSP_LOOP_POLARITY	40	/* Loop Polarity from OpenPCI           */
335
#define DSP_LOOP_NOBATT		41
336
337
#define DSP_PROSLIC_SANITY	50	/* Sanity alert from a ProSLIC port 	*/
338
#define DSP_PROSLIC_PWR_ALARM	51	/* Power Alarm from a ProSLIC port 	*/
339
#define DSP_VDAA_ISO_FRAME_E	52	/* ISO-cap frame sync lost on VDAA port*/
340
341
#if VERBOSE_TIMING
342
 #define REPORT_WAIT(n,x)						    \
343
	 cardinfo(card->cardnum, #n " wait at %d, " #x " = %d", __LINE__, x )
344
#else
345
 #define REPORT_WAIT(n,x)
346
#endif
347
348
#define BUSY_WAIT(countvar,cond,delay,iter,failret)			    \
349
	countvar=0;							    \
350
	while(cond){							    \
351
	    udelay(delay);						    \
352
	    if(++countvar > iter){					    \
353
		cardcrit(wc->boardnum, "busy wait FAILED at %d", __LINE__); \
354
		return failret;						    \
355
	    }								    \
356
	}								    \
357
	REPORT_WAIT(busy,i)
358
359
#define LOCKED_WAIT(countvar,cond,delay,iter,failret)			    \
360
	countvar=0;							    \
361
	while(cond){							    \
362
	    udelay(delay);						    \
363
	    if(++countvar > iter){					    \
364
		dbginfo(wc->boardnum,"busy wait failed at %d",__LINE__);    \
365
		spin_unlock_irqrestore(&wc->lock, flags);		    \
366
		return failret;						    \
367
	    }								    \
368
	}								    \
369
	REPORT_WAIT(locked,i)
370
371
#define HTXF_WAIT()                 BUSY_WAIT(i,HTXF_READY,5,500,RET_FAIL)
372
#define HRXF_WAIT()                 BUSY_WAIT(i,!HRXF_READY,5,70000,RET_FAIL)
373
#define HTXF_WAIT_RET(failret)      BUSY_WAIT(i,HTXF_READY,5,500,failret)
374
#define HRXF_WAIT_RET(failret)      BUSY_WAIT(i,!HRXF_READY,5,1000,failret)
375
376
#define HTXF_WAIT_LOCKED()	    LOCKED_WAIT(i,HTXF_READY,5,500,RET_FAIL)
377
#define HRXF_WAIT_LOCKED()	    LOCKED_WAIT(i,!HRXF_READY,5,1000,RET_FAIL)
378
#define HTXF_WAIT_LOCKED_RET(failret) LOCKED_WAIT(i,HTXF_READY,5,500,failret)
379
#define HRXF_WAIT_LOCKED_RET(failret) LOCKED_WAIT(i,!HRXF_READY,5,1000,failret)
380
381
382
struct openpci {
383
	struct pci_dev *dev;
384
	char *variety;
385
	int boardnum;
386
	int portcount;
387
	int porttype[MAX_PORTS];
388
389
        int firmware;
390
        char serial[ID_DATA_MAXSIZE];
391
392
	spinlock_t lock;
393
394
	//XXX Replace these with proper try_module_get locking in the dahdi driver.
395
	//int usecount;	//XXX
396
	//int dead;	//XXX
397
	union {
398
		struct {
399
			int offhook;
400
		} fxo;
401
		struct {
402
			int ohttimer;
403
			int idletxhookstate;  /* IDLE changing hook state */
404
			int lasttxhook;
405
		} fxs;
406
	} mod[MAX_PORTS];
407
408
	unsigned long		ioaddr;
409
	dma_addr_t		readdma;
410
	dma_addr_t		writedma;
411
	volatile unsigned int  *writechunk;  /* Double-word aligned write memory */
412
	volatile unsigned int  *readchunk;   /* Double-word aligned read memory */
413
414
	struct dahdi_chan _chans[MAX_PORTS];
415
	struct dahdi_chan *chans[MAX_PORTS];
416
	struct dahdi_span span;
417
} *cards[MAX_CARDS];
418
419
// You must hold this lock anytime you access or modify the cards[] array.
420
DEFINE_MUTEX(cards_mutex);
421
422
static unsigned char fxo_port_lookup[8] = { 0x0, 0x8, 0x4, 0xc, 0x10, 0x18, 0x14, 0x1c};
423
static unsigned char fxs_port_lookup[8] = { 0x0, 0x1, 0x2, 0x3, 0x10, 0x11, 0x12, 0x13};
424
static char wcopenpci[] = "Voicetronix OpenPCI";
425
426
static char *country = DEFAULT_COUNTRY;
427
static int reversepolarity;  // = 0
428
static int debug;            // = 0
429
430
module_param(country, charp, 0444);
431
module_param(debug, int, 0600);
432
module_param(reversepolarity, int, 0600);
433
MODULE_PARM_DESC(country, "Set the default country name");
434
MODULE_PARM_DESC(debug, "Enable verbose logging");
435
436
//#define DEBUG_LOOP_VOLTAGE 1
437
#ifdef DEBUG_LOOP_VOLTAGE
438
 // This param is a 32 bit bitfield where bit 1 << cardnum * 8 << portnum
439
 // will enable voltage monitoring on that port (fxo only presently)
440
 static int voltmeter;        // = 0
441
 module_param(voltmeter, int, 0600);
442
 MODULE_PARM_DESC(voltmeter, "Enable loop voltage metering");
443
#endif
444
445
446
/* boolean return values */
447
#define RET_OK   1
448
#define RET_FAIL 0
449
450
/* Convenience macros for logging */
451
#define info(format,...) printk(KERN_INFO NAME ": " format "\n" , ## __VA_ARGS__)
452
#define warn(format,...) printk(KERN_WARNING NAME ": " format "\n" , ## __VA_ARGS__)
453
#define crit(format,...) printk(KERN_CRIT NAME ": " format "\n" , ## __VA_ARGS__)
454
#define cardinfo(cardnum,format,...) info("[%02d] " format, cardnum , ## __VA_ARGS__)
455
#define cardwarn(cardnum,format,...) warn("[%02d] " format, cardnum , ## __VA_ARGS__)
456
#define cardcrit(cardnum,format,...) crit("[%02d] " format, cardnum , ## __VA_ARGS__)
457
#define dbginfo(cardnum,format,...) if(debug) info("[%02d] " format, cardnum , ## __VA_ARGS__)
458
459
460
static inline const char *porttype(struct openpci *wc, int port)
461
{ //{{{
462
	switch( wc->porttype[port] ) {
463
	    case VT_PORT_VDAA:    return "VDAA";
464
	    case VT_PORT_PROSLIC: return "ProSLIC";
465
	    case VT_PORT_EMPTY:   return "empty port";
466
	    default:              return "unknown type";
467
	}
468
} //}}}
469
470
471
static int __read_reg_fxo(struct openpci *wc, int port, unsigned char reg, unsigned char *value)
472
{ //{{{
473
	unsigned char portadr = fxo_port_lookup[port];
474
	int i;
475
476
	if (HRXF_READY) *value = inb(PIB(1));
477
478
	outb(0x11, PIB(1));    HTXF_WAIT();
479
	outb(0x2, PIB(1));     HTXF_WAIT();
480
	outb(portadr, PIB(1)); HTXF_WAIT();
481
	outb(reg, PIB(1));     HTXF_WAIT();
482
	HRXF_WAIT(); *value = inb(PIB(1));
483
484
	return RET_OK;
485
} //}}}
486
487
static int read_reg_fxo(struct openpci *wc, int port, unsigned char reg, unsigned char *value)
488
{ //{{{
489
	unsigned long flags;
490
491
	spin_lock_irqsave(&wc->lock, flags);
492
	if( __read_reg_fxo(wc, port, reg, value) ){
493
		spin_unlock_irqrestore(&wc->lock, flags);
494
		return RET_OK;
495
	}
496
	spin_unlock_irqrestore(&wc->lock, flags);
497
	cardcrit(wc->boardnum, "FXO port %d, reg %d, read failed!", port, reg);
498
	return RET_FAIL;
499
} //}}}
500
501
static int __read_reg_fxs(struct openpci *wc, int port, unsigned char reg, unsigned char *value)
502
{ //{{{
503
	unsigned char portadr = fxs_port_lookup[port];
504
	int i;
505
506
	if (HRXF_READY) *value = inb(PIB(1));
507
508
	outb(0x13, PIB(1));    HTXF_WAIT();
509
	outb(0x2, PIB(1));     HTXF_WAIT();
510
	outb(portadr, PIB(1)); HTXF_WAIT();
511
	outb(reg, PIB(1));     HTXF_WAIT();
512
	HRXF_WAIT(); *value = inb(PIB(1));
513
514
	return RET_OK;
515
} //}}}
516
517
static int read_reg_fxs(struct openpci *wc, int port, unsigned char reg, unsigned char *value)
518
{ //{{{
519
	unsigned long flags;
520
521
	spin_lock_irqsave(&wc->lock, flags);
522
	if( __read_reg_fxs(wc, port, reg, value) ) {
523
		spin_unlock_irqrestore(&wc->lock, flags);
524
		return RET_OK;
525
	}
526
	spin_unlock_irqrestore(&wc->lock, flags);
527
	cardcrit(wc->boardnum, "FXS port %d, reg %d, read failed!", port, reg);
528
	return RET_FAIL;
529
} //}}}
530
531
static int __write_reg_fxo(struct openpci *wc, int port, unsigned char reg, unsigned char value)
532
{ //{{{
533
	unsigned char portadr = fxo_port_lookup[port];
534
	int i;
535
536
        outb(0x10, PIB(1) );   HTXF_WAIT();
537
        outb(0x3, PIB(1));     HTXF_WAIT();
538
        outb(portadr, PIB(1)); HTXF_WAIT();
539
        outb(reg, PIB(1));     HTXF_WAIT();
540
        outb(value, PIB(1));   HTXF_WAIT();
541
542
	return RET_OK;
543
} //}}}
544
545
static int write_reg_fxo(struct openpci *wc, int port, unsigned char reg, unsigned char value)
546
{ //{{{
547
	unsigned long flags;
548
549
	spin_lock_irqsave(&wc->lock, flags);
550
	if( __write_reg_fxo(wc, port, reg, value) ){
551
		spin_unlock_irqrestore(&wc->lock, flags);
552
		return RET_OK;
553
	}
554
	spin_unlock_irqrestore(&wc->lock, flags);
555
	cardcrit(wc->boardnum, "FXO port %d, reg %d, write(%d) failed!", port, reg, value);
556
	return RET_FAIL;
557
} //}}}
558
559
static int __write_reg_fxs(struct openpci *wc, int port, unsigned char reg, unsigned char value)
560
{ //{{{
561
	unsigned char portadr = fxs_port_lookup[port];
562
	int i;
563
564
        outb(0x12, PIB(1) );   HTXF_WAIT();
565
        outb(0x3, PIB(1));     HTXF_WAIT();
566
        outb(portadr, PIB(1)); HTXF_WAIT();
567
        outb(reg, PIB(1));     HTXF_WAIT();
568
        outb(value, PIB(1));   HTXF_WAIT();
569
570
	return RET_OK;
571
} //}}}
572
573
static int write_reg_fxs(struct openpci *wc, int port, unsigned char reg, unsigned char value)
574
{ //{{{
575
	unsigned long flags;
576
577
	spin_lock_irqsave(&wc->lock, flags);
578
	if( __write_reg_fxs(wc, port, reg, value) ){
579
		spin_unlock_irqrestore(&wc->lock, flags);
580
		return RET_OK;
581
	}
582
	spin_unlock_irqrestore(&wc->lock, flags);
583
	cardcrit(wc->boardnum, "FXS port %d, reg %d, write(%d) failed!", port, reg, value);
584
	return RET_FAIL;
585
} //}}}
586
587
static int __wait_indreg_fxs(struct openpci *wc, int port)
588
{ //{{{
589
	unsigned char value;
590
	int count = 100;
591
592
	while (--count)
593
	{
594
		if( __read_reg_fxs(wc, port, I_STATUS, &value) ){
595
			if( value == 0 )
596
				return RET_OK;
597
		} else {
598
			cardcrit(wc->boardnum,
599
				 "failed to read port %d PS_IND_ADDR_ST, retrying...",
600
				 port);
601
		}
602
		udelay(5);
603
	}
604
	cardcrit(wc->boardnum, "Failed to wait for indirect reg write to port %d", port);
605
	return RET_FAIL;
606
} //}}}
607
608
static int write_indreg_fxs(struct openpci *wc, int port, unsigned char reg, unsigned short value)
609
{ //{{{
610
	unsigned long flags;
611
612
	spin_lock_irqsave(&wc->lock, flags);
613
	if( __wait_indreg_fxs(wc, port)
614
	 && __write_reg_fxs(wc, port, IDA_LO, value & 0xff)
615
	 && __write_reg_fxs(wc, port, IDA_HI, (value & 0xff00)>>8)
616
	 && __write_reg_fxs(wc, port, IAA, reg)
617
	 && __wait_indreg_fxs(wc, port) )
618
	{
619
		spin_unlock_irqrestore(&wc->lock, flags);
620
		return RET_OK;
621
	}
622
	spin_unlock_irqrestore(&wc->lock, flags);
623
	cardcrit(wc->boardnum, "FXS indreg %d write failed on port %d", reg, port);
624
	return RET_FAIL;
625
} //}}}
626
627
static int read_indreg_fxs(struct openpci *wc, int port, unsigned char reg, unsigned short *value)
628
{ //{{{
629
	unsigned long flags;
630
	unsigned char lo, hi;
631
632
	spin_lock_irqsave(&wc->lock, flags);
633
	if( __wait_indreg_fxs(wc, port)
634
	 && __write_reg_fxs(wc, port, IAA, reg)
635
	 && __wait_indreg_fxs(wc, port)
636
	 && __read_reg_fxs(wc, port, IDA_LO, &lo)
637
	 && __read_reg_fxs(wc, port, IDA_HI, &hi) )
638
	{
639
		*value = lo | hi << 8;
640
		spin_unlock_irqrestore(&wc->lock, flags);
641
		return RET_OK;
642
	}
643
	spin_unlock_irqrestore(&wc->lock, flags);
644
	return RET_FAIL;
645
} //}}}
646
647
static void start_dma(struct openpci *wc)
648
{ //{{{
649
	outb(0x0f, TJ_CNTL);
650
	set_current_state(TASK_INTERRUPTIBLE);
651
	schedule_timeout(1);
652
	outb(0x01, TJ_CNTL);
653
	outb(0x01, TJ_OPER);
654
} //}}}
655
656
static void restart_dma(struct openpci *wc)
657
{ //{{{
658
	/* Reset Master and TDM */
659
	outb(0x01, TJ_CNTL);
660
	outb(0x01, TJ_OPER);
661
} //}}}
662
663
/* You must hold the card spinlock to call this function */
664
static int __ping_arm(struct openpci *wc)
665
{ //{{{
666
	int i;
667
	int pong=0;
668
669
	while(pong != 0x02){
670
		outb(0x02, PIB(1)); HTXF_WAIT();
671
		HRXF_WAIT(); pong = inb(PIB(1));
672
		dbginfo(wc->boardnum, "ping_arm returned %x", pong);
673
	}
674
	while(pong == 0x02){
675
		// Poke no-ops into the arm while it is still returning data,
676
		// if 500 usec elapses with no further response from it then
677
		// the message queue is should be completely cleared.
678
		outb(0x00, PIB(1)); HTXF_WAIT();
679
		i = 100;
680
		while( !HRXF_READY && --i ) udelay(5);
681
		if( i == 0 ) break;
682
		pong = inb(PIB(1));
683
		dbginfo(wc->boardnum, "ping_arm returned %x.", pong);
684
	}
685
	return RET_OK;
686
} //}}}
687
688
static void arm_event(struct openpci *wc, char *msg)
689
{ //{{{
690
	int port = msg[0];
691
692
	switch(msg[1]){
693
		case DSP_LOOP_OFFHOOK:
694
			dahdi_hooksig(wc->chans[port], DAHDI_RXSIG_OFFHOOK);
695
			dbginfo(wc->boardnum, "Port %d Loop OffHook", port);
696
			break;
697
698
		case DSP_LOOP_ONHOOK:
699
			dahdi_hooksig(wc->chans[port], DAHDI_RXSIG_ONHOOK);
700
			dbginfo(wc->boardnum, "Port %d Loop OnHook", port);
701
			break;
702
703
		case DSP_LOOP_POLARITY:
704
			dahdi_qevent_lock(wc->chans[port], DAHDI_EVENT_POLARITY);
705
			dbginfo(wc->boardnum, "Port %d Loop Polarity", port);
706
			break;
707
708
		case DSP_CODEC_RING:
709
			dahdi_hooksig(wc->chans[port], DAHDI_RXSIG_RING);
710
			dbginfo(wc->boardnum, "Port %d Ring On", port);
711
			break;
712
713
		case DSP_RING_OFF:
714
			dahdi_hooksig(wc->chans[port], DAHDI_RXSIG_OFFHOOK);
715
			dbginfo(wc->boardnum, "Port %d Ring Off", port);
716
			break;
717
718
		case DSP_CODEC_HKOFF:
719
			dahdi_hooksig(wc->chans[port], DAHDI_RXSIG_OFFHOOK);
720
			dbginfo(wc->boardnum, "Port %d Station OffHook", port);
721
			if (reversepolarity)
722
				wc->mod[port].fxs.idletxhookstate = 5;
723
			else
724
				wc->mod[port].fxs.idletxhookstate = 1;
725
			break;
726
727
		case DSP_CODEC_HKON:
728
			dahdi_hooksig(wc->chans[port], DAHDI_RXSIG_ONHOOK);
729
			dbginfo(wc->boardnum, "Port %d Station OnHook", port);
730
			if (reversepolarity)
731
				wc->mod[port].fxs.idletxhookstate = 6;
732
			else
733
				wc->mod[port].fxs.idletxhookstate = 2;
734
			break;
735
736
		case DSP_CODEC_FLASH:
737
			dahdi_qevent_lock(wc->chans[port], DAHDI_EVENT_WINKFLASH);
738
			dbginfo(wc->boardnum, "Port %d Station Flash", port);
739
			break;
740
741
		case DSP_DROP:
742
		case DSP_LOOP_NOBATT:
743
			break;
744
745
			//XXX What to do to recover from these?
746
		case DSP_PROSLIC_SANITY:
747
			dbginfo(wc->boardnum, "Port %d ProSlic has gone insane!", port);
748
			break;
749
750
		case DSP_PROSLIC_PWR_ALARM:
751
		{
752
			char errbuf[32] = " Unknown", *p = errbuf;
753
			int i = 49;
754
755
			msg[2] >>= 2;
756
			for(; i < 55; ++i, msg[2] >>= 1 )
757
			    if(msg[2] & 1){ *(++p)='Q'; *(++p)=i; *(++p)=','; }
758
			if( p != errbuf ) *p = '\0';
759
			cardcrit(wc->boardnum,"%d: ProSlic power ALARM:%s",msg[0],errbuf);
760
			//write_reg_fxs(wc, port, 64, wc->mod[port].fxs.lasttxhook );
761
			return;
762
		}
763
764
		case DSP_VDAA_ISO_FRAME_E:
765
			dbginfo(wc->boardnum, "Port %d VDAA has lost ISO-Cap frame lock", port);
766
			break;
767
768
		default:
769
			cardwarn(wc->boardnum, "Unknown message from Arm[%d] for port %d",
770
						msg[1], port);
771
			break;
772
	}
773
} //}}}
774
775
/* You must hold the card spinlock to call this function */
776
static inline int __read_arm_byte( struct openpci *wc, unsigned char *msg )
777
{ //{{{
778
	int i;
779
780
	HRXF_WAIT(); *msg = inb(PIB(1));
781
	return RET_OK;
782
} //}}}
783
784
static inline int read_arm_msg( struct openpci *wc, unsigned char *msg )
785
{ //{{{
786
	unsigned long flags;
787
	int i, d, count;
788
	int ret = RET_OK;
789
790
	spin_lock_irqsave(&wc->lock, flags);
791
	outb(0x08, PIB(1)); HTXF_WAIT_LOCKED();
792
	//XXX Do we need to clear the interrupt flag even if this fails?
793
	HRXF_WAIT_LOCKED(); count = inb(PIB(1));
794
	if( count == 0 ){
795
		ret = RET_FAIL;
796
	} else if( count < 3 || count > 4 ){
797
		cardcrit(wc->boardnum, "BOGUS arm message size %d, flushing queue", count);
798
		// NB: This may take a while (up to 500usec or more) to complete
799
		//     and we are in the isr at present when this is called, so
800
		//     we may miss an interrupt or two while this is done in the
801
		//     bottom half, but we are already in trouble, so...
802
		d = debug; debug = 5; __ping_arm( wc ); debug = d;
803
		ret = RET_FAIL;
804
	} else while( --count ){
805
		if( ! __read_arm_byte(wc, msg) ){
806
			cardcrit(wc->boardnum,
807
				 "Failed to read arm message %d more bytes expected",
808
				 count);
809
			ret = RET_FAIL;
810
			break;
811
		}
812
		++msg;
813
	}
814
	outb(0x09, PIB(1)); HTXF_WAIT_LOCKED();
815
	spin_unlock_irqrestore(&wc->lock, flags);
816
	return ret;
817
} //}}}
818
819
static void openpci_arm_work( void *cardptr )
820
{ //{{{
821
	struct openpci *wc = (struct openpci*)cardptr;
822
	unsigned char armmsg[4];
823
824
	if( read_arm_msg(wc, armmsg) ) arm_event(wc, armmsg);
825
} //}}}
826
827
828
static inline void openpci_write(struct openpci *wc, unsigned char flags)
829
{ //{{{
830
	int x,y;
831
	volatile unsigned int *writechunk;
832
833
	if (flags & 0x01)
834
		writechunk = wc->writechunk;
835
	else if (flags & 0x02)
836
		writechunk = wc->writechunk + DAHDI_CHUNKSIZE*2;
837
	else {
838
		cardcrit(wc->boardnum, "bad write interrupt flags %x, at %x",
839
					flags, inb(TJ_DMAWC) );
840
		return;
841
	}
842
	/* get data */
843
	dahdi_transmit(&wc->span);
844
	for (y=0,x=0;x<DAHDI_CHUNKSIZE;++x) {
845
		/* Send a sample, as a 32-bit word */
846
#ifdef __BIG_ENDIAN
847
#error No big endian support (yet)
848
#else
849
		/* transmit second 4 ports */
850
		writechunk[y]=0;
851
		if (wc->porttype[4])
852
			writechunk[y] |= (wc->chans[4]->writechunk[x] << 24);
853
		else
854
			writechunk[y] |= (0x01 << 24);
855
		if (wc->porttype[5])
856
			writechunk[y] |= (wc->chans[5]->writechunk[x] << 16);
857
		if (wc->porttype[6])
858
			writechunk[y] |= (wc->chans[6]->writechunk[x] << 8);
859
		if (wc->porttype[7])
860
			writechunk[y] |= (wc->chans[7]->writechunk[x]);
861
		++y;
862
863
		/* transmit first 4 ports */
864
		writechunk[y]=0x01000000;
865
		/* Make sure first port doesnt equal 0x00 */
866
		if (wc->porttype[0]){
867
			if (wc->chans[0]->writechunk[x] == 0)
868
				writechunk[y] |= (0x01 << 24);
869
			else
870
				writechunk[y] |= (wc->chans[0]->writechunk[x] << 24);
871
		}
872
		//else writechunk[y] |= (0x00 << 24);
873
		if (wc->porttype[1])
874
			writechunk[y] |= (wc->chans[1]->writechunk[x] << 16);
875
		if (wc->porttype[2])
876
			writechunk[y] |= (wc->chans[2]->writechunk[x] << 8);
877
		if (wc->porttype[3])
878
			writechunk[y] |= (wc->chans[3]->writechunk[x]);
879
		++y;
880
#endif
881
	}
882
} //}}}
883
884
static inline void openpci_read(struct openpci *wc, unsigned char flags)
885
{ //{{{
886
	int x,y;
887
	volatile unsigned int *readchunk;
888
889
	if (flags & 0x08)
890
		readchunk = wc->readchunk + DAHDI_CHUNKSIZE*2;
891
	else if (flags & 0x04)
892
		readchunk = wc->readchunk;
893
	else {
894
		cardcrit(wc->boardnum, "bad read interrupt flags %x, at %x",
895
					flags, inb(TJ_DMARC));
896
		return;
897
	}
898
899
	for (y=0,x=0;x<DAHDI_CHUNKSIZE;++x) {
900
#ifdef __BIG_ENDIAN
901
#error No big endian support (yet)
902
#else
903
		/* Receive first 4 ports */
904
905
		if (wc->porttype[0])
906
			wc->chans[0]->readchunk[x] = (readchunk[y] >> 24) & 0xff;
907
		if (wc->porttype[1])
908
			wc->chans[1]->readchunk[x] = (readchunk[y] >> 16) & 0xff;
909
		if (wc->porttype[2])
910
			wc->chans[2]->readchunk[x] = (readchunk[y] >> 8) & 0xff;
911
		if (wc->porttype[3])
912
			wc->chans[3]->readchunk[x] = (readchunk[y]) & 0xff;
913
		++y;
914
		/* Receive second 4 ports */
915
		if (wc->porttype[4])
916
			wc->chans[4]->readchunk[x] = (readchunk[y] >> 24) & 0xff;
917
		if (wc->porttype[5])
918
			wc->chans[5]->readchunk[x] = (readchunk[y] >> 16) & 0xff;
919
		if (wc->porttype[6])
920
			wc->chans[6]->readchunk[x] = (readchunk[y] >> 8) & 0xff;
921
		if (wc->porttype[7])
922
			wc->chans[7]->readchunk[x] = (readchunk[y]) & 0xff;
923
		++y;
924
#endif
925
	}
926
	/* XXX We're wasting 8 taps.  We should get closer :( */
927
	for (x = 0; x < MAX_PORTS; x++) {
928
		if (wc->porttype[x])
929
			dahdi_ec_chunk(wc->chans[x], wc->chans[x]->readchunk, wc->chans[x]->writechunk);
930
	}
931
	dahdi_receive(&wc->span);
932
} //}}}
933
934
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
935
static irqreturn_t openpci_isr(int irq, void *dev_id, struct pt_regs *regs)
936
#else
937
static irqreturn_t openpci_isr(int irq, void *dev_id)
938
#endif
939
{ //{{{
940
	struct openpci *wc = dev_id;
941
	unsigned long flags;
942
	unsigned char status;
943
944
	spin_lock_irqsave(&wc->lock, flags);
945
	status = inb(TJ_INTSTAT);
946
	outb(status, TJ_INTSTAT);
947
948
	if (!status) {
949
		if(inb(TJ_AUXR) & 0x02) {
950
			spin_unlock_irqrestore(&wc->lock, flags);
951
			return IRQ_NONE;
952
		}
953
		spin_unlock_irqrestore(&wc->lock, flags);
954
		openpci_arm_work(wc);
955
		return IRQ_HANDLED;
956
	}
957
	if (status & 0x10){
958
		/* PCI Master abort */
959
		cardcrit(wc->boardnum, "PCI Master Abort.");
960
		/* Stop DMA, wait for watchdog */
961
		outb(0x00, TJ_OPER);
962
		spin_unlock_irqrestore(&wc->lock, flags);
963
		return IRQ_HANDLED;
964
	}
965
	spin_unlock_irqrestore(&wc->lock, flags);
966
967
	if (status & 0x20){
968
		/* PCI Target abort */
969
		cardcrit(wc->boardnum, "PCI Target Abort.");
970
		return IRQ_HANDLED;
971
	}
972
	if (status & 0x03){
973
		openpci_write(wc, status);
974
	}
975
	if (status & 0x0c){
976
	    #ifdef DEBUG_LOOP_VOLTAGE
977
	    //{{{
978
		static int counter[MAX_CARDS];
979
		int card = wc->boardnum;
980
		int port = ++counter[card] & 0x07;
981
		int ignore = counter[card] & 0xf0;
982
983
		if( ! ignore && (voltmeter & ((1 << (card * 8)) << port)) ) {
984
			unsigned char lv;
985
			if( wc->porttype[port] == VT_PORT_VDAA && read_reg_fxo(wc, port, 29, &lv) )
986
				cardinfo(wc->boardnum, "Port %d loop voltage %d",
987
							port, lv < 128 ? lv : lv - 256);
988
		}
989
	    //}}}
990
	    #endif
991
		openpci_read(wc, status);
992
	}
993
994
	return IRQ_HANDLED;
995
} //}}}
996
997
static int openpci_ioctl(struct dahdi_chan *chan, unsigned int cmd, unsigned long data)
998
{ //{{{
999
	struct wctdm_stats stats;
1000
	struct wctdm_regs regs;
1001
	struct wctdm_regop regop;
1002
	struct wctdm_echo_coefs echoregs;
1003
	struct openpci *wc = chan->pvt;
1004
	int port = chan->chanpos - 1;
1005
	int x;
1006
1007
	switch (cmd) {
1008
	case DAHDI_ONHOOKTRANSFER:
1009
		if (wc->porttype[port] != VT_PORT_PROSLIC)
1010
			return -EINVAL;
1011
		if (get_user(x, (int *)data))
1012
			return -EFAULT;
1013
		wc->mod[port].fxs.ohttimer = x << 3;
1014
		if (reversepolarity)
1015
			wc->mod[port].fxs.idletxhookstate = 0x6;	/* OHT mode when idle */
1016
		else
1017
			wc->mod[port].fxs.idletxhookstate = 0x2;
1018
		switch(wc->mod[port].fxs.lasttxhook) {
1019
		    case 0x1:
1020
		    case 0x5:
1021
			if (reversepolarity)
1022
				wc->mod[port].fxs.lasttxhook = 0x6;
1023
			else
1024
				wc->mod[port].fxs.lasttxhook = 0x2;
1025
			if( ! write_reg_fxs(wc, port, 64, wc->mod[port].fxs.lasttxhook) )
1026
				return -EIO;
1027
		}
1028
		break;
1029
	case DAHDI_SETPOLARITY:
1030
		if (get_user(x, (int *)data))
1031
			return -EFAULT;
1032
		if (wc->porttype[port] != VT_PORT_PROSLIC)
1033
			return -EINVAL;
1034
		/* Can't change polarity while ringing or when open */
1035
		if ((wc->mod[port].fxs.lasttxhook == 0x04) ||
1036
		    (wc->mod[port].fxs.lasttxhook == 0x00))
1037
			return -EINVAL;
1038
1039
		if ((x && !reversepolarity) || (!x && reversepolarity))
1040
			wc->mod[port].fxs.lasttxhook |= 0x04;
1041
		else
1042
			wc->mod[port].fxs.lasttxhook &= ~0x04;
1043
		if( ! write_reg_fxs(wc, port, 64, wc->mod[port].fxs.lasttxhook) )
1044
			return -EIO;
1045
		break;
1046
	case WCTDM_GET_STATS:
1047
		if (wc->porttype[port] == VT_PORT_PROSLIC) {
1048
			unsigned char	linevolt;
1049
			if( read_reg_fxs(wc, port, 80, &linevolt) )
1050
				stats.tipvolt = linevolt * -376;
1051
			else
1052
				return -EIO;
1053
			if( read_reg_fxs(wc, port, 81, &linevolt) )
1054
				stats.ringvolt = linevolt * -376;
1055
			else
1056
				return -EIO;
1057
			if( read_reg_fxs(wc, port, 82, &linevolt) )
1058
				stats.batvolt = linevolt * -376;
1059
			else
1060
				return -EIO;
1061
		} else if (wc->porttype[port] == VT_PORT_VDAA) {
1062
			unsigned char	linevolt;
1063
			if( read_reg_fxo(wc, port, 29, &linevolt) )
1064
				stats.tipvolt = stats.ringvolt = stats.batvolt = linevolt * 1000;
1065
			else
1066
				return -EIO;
1067
		} else
1068
			return -EINVAL;
1069
		if (copy_to_user((struct wctdm_stats *)data, &stats, sizeof(stats)))
1070
			return -EFAULT;
1071
		break;
1072
	case WCTDM_GET_REGS:
1073
		if (wc->porttype[port] == VT_PORT_PROSLIC) {
1074
			for (x=0;x<NUM_INDIRECT_REGS;x++)
1075
				if( ! read_indreg_fxs(wc, port, x, &regs.indirect[x]) )
1076
					return -EIO;
1077
			for (x=0;x<NUM_REGS;x++)
1078
				if( ! read_reg_fxs(wc, port, x, &regs.direct[x]) )
1079
					return -EIO;
1080
		} else {
1081
			memset(&regs, 0, sizeof(regs));
1082
			for (x=0;x<NUM_FXO_REGS;x++){
1083
				if( ! read_reg_fxo(wc, port, x, &regs.direct[x]) )
1084
					return -EIO;
1085
			}
1086
		}
1087
		if (copy_to_user((struct wctdm_regs *)data, &regs, sizeof(regs)))
1088
			return -EFAULT;
1089
		break;
1090
	case WCTDM_SET_REG:
1091
		if (copy_from_user(&regop, (struct wctdm_regop *)data, sizeof(regop)))
1092
			return -EFAULT;
1093
		if (regop.indirect) {
1094
			if (wc->porttype[port] != VT_PORT_PROSLIC)
1095
				return -EINVAL;
1096
			printk("Setting indirect %d to 0x%04x on %d\n",
1097
				regop.reg, regop.val, chan->chanpos);
1098
			if( ! write_indreg_fxs(wc, port, regop.reg, regop.val) )
1099
				return -EIO;
1100
		} else {
1101
			regop.val &= 0xff;
1102
			printk("Setting direct %d to %04x on %d\n",
1103
				regop.reg, regop.val, chan->chanpos);
1104
			if (wc->porttype[port] == VT_PORT_PROSLIC) {
1105
				if( ! write_reg_fxs(wc, port, regop.reg, regop.val) )
1106
					return -EIO;
1107
			} else {
1108
				if( ! write_reg_fxo(wc, port, regop.reg, regop.val) )
1109
					return -EIO;
1110
			}
1111
		}
1112
		break;
1113
	case WCTDM_SET_ECHOTUNE:
1114
		cardinfo(wc->boardnum, "Setting echo registers");
1115
		if (copy_from_user(&echoregs, (struct wctdm_echo_coefs*)data, sizeof(echoregs)))
1116
			return -EFAULT;
1117
1118
		if (wc->porttype[port] == VT_PORT_VDAA) {
1119
			/* Set the ACIM and digital echo canceller registers */
1120
			if( ! write_reg_fxo(wc, port, 30, echoregs.acim)
1121
			 || ! write_reg_fxo(wc, port, 45, echoregs.coef1)
1122
			 || ! write_reg_fxo(wc, port, 46, echoregs.coef2)
1123
			 || ! write_reg_fxo(wc, port, 47, echoregs.coef3)
1124
			 || ! write_reg_fxo(wc, port, 48, echoregs.coef4)
1125
			 || ! write_reg_fxo(wc, port, 49, echoregs.coef5)
1126
			 || ! write_reg_fxo(wc, port, 50, echoregs.coef6)
1127
			 || ! write_reg_fxo(wc, port, 51, echoregs.coef7)
1128
			 || ! write_reg_fxo(wc, port, 52, echoregs.coef8) )
1129
			{
1130
				cardcrit(wc->boardnum, "Failed to set echo registers");
1131
				return -EIO;
1132
			}
1133
			break;
1134
		} else {
1135
			return -EINVAL;
1136
		}
1137
		break;
1138
	default:
1139
		return -ENOTTY;
1140
	}
1141
	return 0;
1142
} //}}}
1143
1144
static int openpci_open(struct dahdi_chan *chan)
1145
{
1146
	struct openpci *wc = chan->pvt;
1147
	if( ! wc->porttype[chan->chanpos-1] )
1148
		return -ENODEV;
1149
1150
	//XXX This is WRONG and can prang in a race.  We must pass THIS_MODULE
1151
	//    as the owner of the span that holds the pointer to this function,
1152
	//    then bump the refcount in the dahdi code _BEFORE_ the potentially
1153
	//    fatal call to an invalid pointer is made.
1154
	//if( wc->dead ) return -ENODEV;
1155
	//wc->usecount++;
1156
	try_module_get(THIS_MODULE);  //XXX
1157
1158
	return 0;
1159
}
1160
1161
static int openpci_watchdog(struct dahdi_span *span, int event)
1162
{
1163
	info("TDM: Restarting DMA");
1164
	restart_dma(span->pvt);
1165
	return 0;
1166
}
1167
1168
static int openpci_close(struct dahdi_chan *chan)
1169
{
1170
	struct openpci *wc = chan->pvt;
1171
	int port = chan->chanpos - 1;
1172
1173
	//XXX wc->usecount--;
1174
	//XXX This is WRONG and can prang in a race.  We must pass THIS_MODULE
1175
	//    as the owner of the span that holds the pointer to this function,
1176
	//    then bump the refcount in the dahdi code _BEFORE_ the potentially
1177
	//    fatal call to an invalid pointer is made.
1178
	module_put(THIS_MODULE);
1179
	if (wc->porttype[port] == VT_PORT_PROSLIC) {
1180
		if (reversepolarity)
1181
			wc->mod[port].fxs.idletxhookstate = 5;
1182
		else
1183
			wc->mod[port].fxs.idletxhookstate = 1;
1184
	}
1185
	/* If we're dead, release us now */
1186
	//XXX if (!wc->usecount && wc->dead) openpci_release(wc);
1187
1188
	return 0;
1189
}
1190
1191
static int openpci_hooksig(struct dahdi_chan *chan, enum dahdi_txsig txsig)
1192
{ //{{{
1193
	struct openpci *wc = chan->pvt;
1194
	int port = chan->chanpos - 1;
1195
	int new_hk_state;
1196
1197
	dbginfo(wc->boardnum, "Setting %s port %d hook state %s",
1198
		 wc->porttype[port] == VT_PORT_VDAA ? "FXO" : "FXS",
1199
		 port,
1200
		 txsig == 0 ? "ONHOOK" :
1201
		 txsig == 1 ? "OFFHOOK" :
1202
		 txsig == 2 ? "START" :
1203
		 txsig == 3 ? "KEWL" : "UNKNOWN" );
1204
1205
	switch(wc->porttype[port]) {
1206
	    case VT_PORT_VDAA:
1207
		switch(txsig) {
1208
		    case DAHDI_TXSIG_START:
1209
		    case DAHDI_TXSIG_OFFHOOK:
1210
			if( write_reg_fxo(wc, port, 5, 0x9)
1211
			 && write_reg_fxo(wc, port, 0x20, 0x0) )
1212
				wc->mod[port].fxo.offhook = 1;
1213
			else
1214
				cardcrit(wc->boardnum, "Failed set fxo off-hook");
1215
			break;
1216
1217
		    case DAHDI_TXSIG_ONHOOK:
1218
			if( write_reg_fxo(wc, port, 5, 0x8)
1219
			 && write_reg_fxo(wc, port, 0x20, 0x3) )
1220
				wc->mod[port].fxo.offhook = 0;
1221
			else
1222
				cardcrit(wc->boardnum, "Failed set fxo on-hook");
1223
			break;
1224
1225
		    default:
1226
			cardcrit(wc->boardnum,
1227
				 "Can't set FXO port %d tx state to %d",
1228
				 port, txsig);
1229
		}
1230
		break;
1231
1232
	    case VT_PORT_PROSLIC:
1233
		new_hk_state = wc->mod[port].fxs.lasttxhook;
1234
		switch(txsig) {
1235
		    case DAHDI_TXSIG_ONHOOK:
1236
			switch(chan->sig) {
1237
			case DAHDI_SIG_EM:
1238
			case DAHDI_SIG_FXOKS:
1239
			case DAHDI_SIG_FXOLS:
1240
				new_hk_state = wc->mod[port].fxs.idletxhookstate;
1241
				break;
1242
			case DAHDI_SIG_FXOGS:
1243
				new_hk_state = 3;
1244
				break;
1245
			}
1246
			break;
1247
1248
		    case DAHDI_TXSIG_OFFHOOK:
1249
			switch(chan->sig) {
1250
			case DAHDI_SIG_EM:
1251
				new_hk_state = 5;
1252
				break;
1253
			default:
1254
				new_hk_state = wc->mod[port].fxs.idletxhookstate;
1255
				break;
1256
			}
1257
			break;
1258
1259
		    case DAHDI_TXSIG_START:
1260
			new_hk_state = 4;
1261
			break;
1262
1263
		    case DAHDI_TXSIG_KEWL:
1264
			new_hk_state = 0;
1265
			break;
1266
1267
		    default:
1268
			cardinfo(wc->boardnum,
1269
				 "Can't set FXS port %d tx state to %d",
1270
				 port, txsig);
1271
		}
1272
		dbginfo(wc->boardnum, "%s port %d hook state old %d, new %d",
1273
			 wc->porttype[port] == VT_PORT_VDAA ? "FXO" : "FXS",
1274
			 port, wc->mod[port].fxs.lasttxhook, new_hk_state );
1275
1276
		if (new_hk_state != wc->mod[port].fxs.lasttxhook){
1277
			if( write_reg_fxs(wc, port, 64, new_hk_state) )
1278
				wc->mod[port].fxs.lasttxhook = new_hk_state;
1279
			else
1280
				cardcrit(wc->boardnum,
1281
					 "Failed to set port %d fxs hookstate from %d to %d",
1282
					 port, wc->mod[port].fxs.lasttxhook, new_hk_state);
1283
		}
1284
		break;
1285
1286
	    default:
1287
		cardcrit(wc->boardnum,
1288
			 "Unknown module type %d in openpci_hooksig",
1289
			 wc->porttype[port] );
1290
	}
1291
	return 0;
1292
} //}}}
1293
1294
static int span_initialize(struct openpci *wc)
1295
{ //{{{
1296
	int x;
1297
1298
	//XXX Set a THIS_MODULE as the owner of the span...
1299
	/* Zapata stuff */
1300
	sprintf(wc->span.name, "WCTDM/%d", wc->boardnum);
1301
	sprintf(wc->span.desc, "%s Board %d", wc->variety, wc->boardnum + 1);
1302
	for (x = 0; x < MAX_PORTS; x++) {
1303
		struct dahdi_chan *chan = &wc->_chans[x];
1304
		wc->chans[x] = chan;
1305
		sprintf(chan->name, "WCTDM/%d/%d", wc->boardnum, x);
1306
		chan->sigcap = DAHDI_SIG_FXOKS | DAHDI_SIG_FXOLS | DAHDI_SIG_FXOGS
1307
				    | DAHDI_SIG_SF | DAHDI_SIG_EM | DAHDI_SIG_CLEAR;
1308
		chan->sigcap |= DAHDI_SIG_FXSKS | DAHDI_SIG_FXSLS | DAHDI_SIG_SF | DAHDI_SIG_CLEAR;
1309
		chan->chanpos = x+1;
1310
		chan->pvt = wc;
1311
	}
1312
	wc->span.deflaw = DAHDI_LAW_MULAW;
1313
	wc->span.chans = wc->chans;
1314
	wc->span.channels = MAX_PORTS;
1315
	wc->span.hooksig = openpci_hooksig;
1316
	wc->span.open = openpci_open;
1317
	wc->span.close = openpci_close;
1318
	wc->span.flags = DAHDI_FLAG_RBS;
1319
	wc->span.ioctl = openpci_ioctl;
1320
	wc->span.watchdog = openpci_watchdog;
1321
	init_waitqueue_head(&wc->span.maintq);
1322
1323
	wc->span.pvt = wc;
1324
	if (dahdi_register(&wc->span, 0)) {
1325
		cardcrit(wc->boardnum, "Unable to register span with dahdi");
1326
		return RET_FAIL;
1327
	}
1328
	return RET_OK;
1329
} //}}}
1330
1331
static int get_port_type(struct openpci *wc, int port)
1332
{ //{{{
1333
	int i, type;
1334
	unsigned long flags;
1335
1336
	spin_lock_irqsave(&wc->lock, flags);
1337
	outb(0x20, PIB(1)); HTXF_WAIT_LOCKED_RET(VT_PORT_EMPTY);
1338
	outb(port, PIB(1)); HTXF_WAIT_LOCKED_RET(VT_PORT_EMPTY);
1339
	HRXF_WAIT_LOCKED_RET(VT_PORT_EMPTY); type = inb(PIB(1));
1340
	spin_unlock_irqrestore(&wc->lock, flags);
1341
1342
	return type;
1343
} //}}}
1344
1345
static int check_ports(struct openpci *wc)
1346
{ //{{{
1347
	int i = 0;
1348
1349
	wc->portcount = 0;
1350
	for(; i < MAX_PORTS; ++i ){
1351
		wc->porttype[i] = get_port_type(wc, i);
1352
		dbginfo(wc->boardnum,"%d: %s", i, porttype(wc,i));
1353
1354
		switch( wc->porttype[i] ) {
1355
		    case VT_PORT_PROSLIC:
1356
			/* By default, don't send on hook */
1357
			if (reversepolarity)
1358
				wc->mod[i].fxs.idletxhookstate = 5;
1359
			else
1360
				wc->mod[i].fxs.idletxhookstate = 1;
1361
1362
		    case VT_PORT_VDAA:
1363
			++wc->portcount;
1364
		}
1365
	}
1366
	// we 'succeed' if any ports were discovered.
1367
	return wc->portcount ? RET_OK : RET_FAIL;
1368
} //}}}
1369
1370
static int configure_vdaa_country(struct openpci *wc, int port, char *name)
1371
{ //{{{
1372
	unsigned char value;
1373
	int i;
1374
1375
	for (i=0; i < sizeof(fxo_modes)/sizeof(struct fxo_mode); ++i){
1376
		if(!strcmp(fxo_modes[i].name, name)){
1377
			dbginfo(wc->boardnum, "%d: Setting country to %s", port, name);
1378
			goto part2;
1379
		}
1380
	}
1381
	i = 3;
1382
	cardinfo(wc->boardnum, "Using default country %s", fxo_modes[i].name);
1383
1384
    part2:
1385
	value  = (fxo_modes[i].ohs << 6);
1386
	value |= (fxo_modes[i].rz << 1);
1387
	value |= (fxo_modes[i].rt << 0);
1388
	if( ! write_reg_fxo(wc, port, 16, value) ) goto hell;
1389
1390
	/* DC Termination Control - Register 26 */
1391
	value  = (fxo_modes[i].dcv << 6);
1392
	value |= (fxo_modes[i].mini << 4);
1393
	value |= (fxo_modes[i].ilim << 1);
1394
	if( ! write_reg_fxo(wc, port, 26, value) ) goto hell;
1395
1396
	/* AC Termination Control - Register 30 */
1397
	value = (fxo_modes[i].acim << 0);
1398
	if( ! write_reg_fxo(wc, port, 30, value) ) goto hell;
1399
1400
	/* DAA Control 5 - Register 31 */
1401
	msleep(1);
1402
	if( ! read_reg_fxo(wc, port, 31, &value) ) goto hell;
1403
1404
	value = (value & 0xf7) | (fxo_modes[i].ohs2 << 3);
1405
	value = value | 0x02;
1406
	if( ! write_reg_fxo(wc, port, 31, value) ) goto hell;
1407
1408
	return RET_OK;
1409
1410
    hell:
1411
	cardcrit(wc->boardnum, "port %d failed configure vdaa country", port);
1412
	return RET_FAIL;
1413
} //}}}
1414
1415
// Do not call this from an interrupt context, it may sleep.
1416
static void configure_vdaa_port(struct openpci *wc, int port)
1417
{ //{{{
1418
	/* Set Country - default to Australia */
1419
	if( configure_vdaa_country(wc, port, country) )
1420
		++wc->portcount;
1421
	else {
1422
		cardcrit(wc->boardnum, "FAILED to configure vdaa port %d.  Disabled.", port);
1423
		wc->porttype[port] = VT_PORT_EMPTY;
1424
	}
1425
} //}}}
1426
1427
static int configure_proslic_country(struct openpci *wc, int port, const char *name)
1428
{ //{{{
1429
	int i;
1430
1431
	for(i=0; i < sizeof(ps_country_regs)/sizeof(struct ps_country_reg); ++i) {
1432
		if(!strcmp(ps_country_regs[i].country, name)){
1433
			dbginfo(wc->boardnum, "%d: Setting country to %s", port, name);
1434
			goto part2;
1435
		}
1436
	}
1437
	return -EINVAL;
1438
1439
    part2:
1440
1441
	if( ! write_reg_fxs(wc, port, 10, ps_country_regs[i].value) ){
1442
		cardcrit(wc->boardnum,"%d: failed to write PS_IMPEDANCE", port);
1443
		return -EIO;
1444
	}
1445
	return 0;
1446
} //}}}
1447
1448
// Do not call this from an interrupt context, it may sleep.
1449
static void configure_proslic_port(struct openpci *wc, int port)
1450
{ //{{{
1451
	/* Set Country - default to Australia */
1452
	switch( configure_proslic_country(wc, port, country) ){
1453
	    case 0:
1454
		break;
1455
1456
	    case -EINVAL:
1457
		cardwarn(wc->boardnum,"%d: Country '%s' unknown, using default", port, country);
1458
		if( configure_proslic_country(wc, port, DEFAULT_COUNTRY) == 0 )
1459
			goto hell;
1460
1461
	    default:
1462
		goto hell;
1463
	}
1464
1465
	++wc->portcount;
1466
	return;
1467
1468
    hell:
1469
	cardcrit(wc->boardnum, "FAILED to configure proslic port %d.  Disabled.", port);
1470
	wc->porttype[port] = VT_PORT_EMPTY;
1471
} //}}}
1472
1473
// Do not call this from an interrupt context, it may (indirectly) sleep.
1474
static int configure_ports(struct openpci *wc)
1475
{ //{{{
1476
	unsigned long flags;
1477
	int i;
1478
1479
	wc->portcount = 0;
1480
	for(i=0; i < MAX_PORTS; ++i){
1481
		switch (wc->porttype[i]){
1482
		    case VT_PORT_VDAA:    configure_vdaa_port(wc,i);    break;
1483
		    case VT_PORT_PROSLIC: configure_proslic_port(wc,i); break;
1484
		}
1485
	}
1486
1487
	spin_lock_irqsave(&wc->lock, flags);
1488
	outb(0x2c, PIB(1)); HTXF_WAIT_LOCKED();
1489
	outb(0xff, PIB(1)); HTXF_WAIT_LOCKED();
1490
	spin_unlock_irqrestore(&wc->lock, flags);
1491
1492
	// otherwise we 'succeed' if any ports were configured successfully.
1493
	return wc->portcount ? RET_OK : RET_FAIL;
1494
} //}}}
1495
1496
static int __get_arm_id(struct openpci *wc, int field, char *value)
1497
{ //{{{
1498
	int i;
1499
	int x=0;
1500
	int count=0;
1501
1502
	outb(0x01, PIB(1));  HTXF_WAIT();
1503
	outb(field, PIB(1)); HTXF_WAIT();
1504
	HRXF_WAIT(); count = inb(PIB(1));
1505
	if (count > ID_DATA_MAXSIZE){
1506
		cardcrit(wc->boardnum, "Too many bytes of id(%d) data %d/%d",
1507
					 field, count, ID_DATA_MAXSIZE);
1508
		return RET_FAIL;
1509
	}
1510
	//cardinfo(wc->boardnum, "get_arm_id(%d): byte count %d",field,count);
1511
	for(; x < count; ++x){
1512
		HRXF_WAIT(); *value = inb(PIB(1));
1513
		//cardinfo(wc->boardnum, "get_arm_id(%d): byte %d => 0x%02x",field,x,tmp);
1514
		++value;
1515
	}
1516
	return RET_OK;
1517
} //}}}
1518
1519
static void enable_interrupts(struct openpci *wc)
1520
{ //{{{
1521
	outb(0x3f, TJ_MASK0);
1522
	outb(0x02, TJ_MASK1);
1523
} //}}}
1524
1525
static void disable_interrupts(struct openpci *wc)
1526
{ //{{{
1527
	outb(0x00, TJ_MASK0);
1528
	outb(0x00, TJ_MASK1);
1529
} //}}}
1530
1531
// Do not call this from an interrupt context, it may sleep.
1532
static int check_arm(struct openpci *wc)
1533
{ //{{{
1534
	char model[ID_DATA_MAXSIZE+1] = { 0 };
1535
	char date[ID_DATA_MAXSIZE+1]  = { 0 };
1536
	unsigned long flags;
1537
	int i=0;
1538
	int tmp=0;
1539
1540
	spin_lock_irqsave(&wc->lock, flags);
1541
	while ((tmp != 0x88)&&(++i<100)){
1542
		outb(0x88, PIB(0));
1543
		msleep(1);
1544
		tmp = inb(PIB(1));
1545
	}
1546
	if (i>=1000) goto limbo;
1547
	dbginfo(wc->boardnum, "Arm responded on attempt %d",i);
1548
1549
	// Flush out the queue if we sent several pings before a response.
1550
	if(i>1)	__ping_arm(wc);
1551
1552
	if( ! __get_arm_id(wc, 0, model) )  goto hell;
1553
	sscanf(model, "OpenPCI8.%02d", &(wc->firmware));
1554
	cardinfo(wc->boardnum, "  model: %s", model);
1555
1556
	if( ! __get_arm_id(wc, 1, date) )   goto hell;
1557
	cardinfo(wc->boardnum, "  date: %s", date);
1558
1559
	if( ! __get_arm_id(wc, 2, wc->serial) ) goto hell;
1560
	cardinfo(wc->boardnum, "  serial: %s", wc->serial);
1561
1562
	spin_unlock_irqrestore(&wc->lock, flags);
1563
	return RET_OK;
1564
1565
    hell:
1566
	spin_unlock_irqrestore(&wc->lock, flags);
1567
        cardwarn(wc->boardnum, "Found ARM processor, dumb firmware.");
1568
	return RET_OK;
1569
1570
    limbo:
1571
	spin_unlock_irqrestore(&wc->lock, flags);
1572
	return RET_FAIL;
1573
} //}}}
1574
1575
static int arm_monitor(struct openpci *wc, int on)
1576
{ //{{{
1577
	int i;
1578
	outb( on ? 0x06 : 0x07, PIB(1) ); HTXF_WAIT();
1579
	return RET_OK;
1580
} //}}}
1581
1582
static int __devinit openpci_probe_board(struct pci_dev *pdev, const struct pci_device_id *ent)
1583
{ //{{{
1584
	struct openpci *wc;
1585
	int boardnum = 0;
1586
	int failret = -ENOMEM;
1587
	int tmp = 0;
1588
	int i;
1589
	unsigned long flags;
1590
1591
	if( ent->driver_data != (kernel_ulong_t)&wcopenpci )
1592
	{
1593
	    info("Probe of non-OpenPCI card, ignoring.");
1594
	    return -EINVAL;
1595
	}
1596
	wc = kzalloc(sizeof(struct openpci), GFP_KERNEL);
1597
	if (!wc){
1598
		return -ENOMEM;
1599
	}
1600
1601
	mutex_lock(&cards_mutex);
1602
	for (; boardnum < MAX_CARDS && cards[boardnum]; ++boardnum);
1603
	if (boardnum >= MAX_CARDS){
1604
		crit("Too many OpenPCI cards(%d), max is %d.", boardnum, MAX_CARDS);
1605
		mutex_unlock(&cards_mutex);
1606
		goto hell;
1607
	}
1608
	cards[boardnum] = wc;
1609
	mutex_unlock(&cards_mutex);
1610
1611
	spin_lock_init(&wc->lock);
1612
	pci_set_drvdata(pdev, wc);
1613
1614
	wc->boardnum = boardnum;
1615
	wc->dev      = pdev;
1616
	wc->variety  = wcopenpci;
1617
1618
	cardinfo(boardnum, "Initialising card");
1619
	if (pci_enable_device(pdev)) {
1620
		failret = -EIO;
1621
		goto hell_2;
1622
	}
1623
	wc->ioaddr = pci_resource_start(pdev, 0);
1624
	if( ! request_region(wc->ioaddr, 0xff, NAME) ){
1625
                cardcrit(boardnum, "Failed to lock IO region, another driver already using it");
1626
		failret = -EBUSY;
1627
		goto hell_2;
1628
	}
1629
1630
	spin_lock_irqsave(&wc->lock, flags);
1631
	outb(0xff, TJ_AUXD);            /* Set up TJ to access the ARM */
1632
	outb(0x78, TJ_AUXC);            /* Set up for Jtag */
1633
	outb(0x00, TJ_CNTL);            /* pull ERST low */
1634
	spin_unlock_irqrestore(&wc->lock, flags);
1635
	msleep(1);	                /* Wait a bit */
1636
1637
	dbginfo(boardnum,"Starting ARM");
1638
	spin_lock_irqsave(&wc->lock, flags);
1639
	outb(0x01, TJ_CNTL);            /* pull ERST high again */
1640
	spin_unlock_irqrestore(&wc->lock, flags);
1641
	msleep(100);                    /* Give it all a chance to boot */
1642
1643
	if( ! check_arm(wc) ){
1644
		cardcrit(boardnum, "Couldnt find ARM processor");
1645
		failret = -EIO;
1646
		goto hell_3;
1647
	}
1648
	if( wc->firmware < 11 ){
1649
		cardcrit(boardnum,
1650
			 "Firmware version %d not supported by this driver",
1651
			 wc->firmware);
1652
		cardcrit(boardnum, " contact Voicetronix to have it updated");
1653
		failret = -ENODEV;
1654
		goto hell_3;
1655
	}
1656
	if( ! check_ports(wc) ){
1657
		cardcrit(boardnum, "Couldnt find ports!");
1658
		failret = -EIO;
1659
		goto hell_3;
1660
	}
1661
1662
	wc->writechunk = pci_alloc_consistent(pdev, VT_PCIDMA_BLOCKSIZE, &wc->writedma);
1663
	if (!wc->writechunk) {
1664
		cardcrit(boardnum, "Couldnt get DMA memory.");
1665
		goto hell_3;
1666
	}
1667
	wc->readchunk = wc->writechunk + DAHDI_MAX_CHUNKSIZE * (MAX_PORTS*2 / sizeof(int));
1668
	wc->readdma = wc->writedma + DAHDI_MAX_CHUNKSIZE * (MAX_PORTS*2);
1669
1670
	memset((void*)wc->writechunk,0,VT_PCIDMA_BLOCKSIZE);
1671
1672
	spin_lock_irqsave(&wc->lock, flags);
1673
	outb(0xc1, TJ_SERCTL);
1674
	outb(0x0, TJ_FSCDELAY);
1675
1676
	outl(wc->writedma,                    TJ_DMAWS);
1677
	outl(wc->writedma + VT_PCIDMA_MIDDLE, TJ_DMAWI);
1678
	outl(wc->writedma + VT_PCIDMA_END,    TJ_DMAWE);
1679
	outl(wc->readdma,                     TJ_DMARS);
1680
	outl(wc->readdma + VT_PCIDMA_MIDDLE,  TJ_DMARI);
1681
	outl(wc->readdma + VT_PCIDMA_END,     TJ_DMARE);
1682
1683
	/* Clear interrupts */
1684
	outb(0xff, TJ_INTSTAT);
1685
	spin_unlock_irqrestore(&wc->lock, flags);
1686
1687
	if( ! arm_monitor(wc, 1) ){
1688
		cardcrit(boardnum, "failed to start arm monitoring");
1689
		failret = -EIO;
1690
		goto hell_4;
1691
	}
1692
	msleep(1000);
1693
1694
	i = 0;
1695
	while(tmp != 0x88 && ++i < 1000) {
1696
		outb(0x88, PIB(0));
1697
		msleep(250);
1698
		tmp = inb(PIB(1));
1699
	}
1700
	if(i>=1000) {
1701
		cardcrit(boardnum, "FAILED to initialise board");
1702
		goto hell_4;
1703
	}
1704
1705
	if( ! check_ports(wc) ) {
1706
		cardcrit(boardnum, "FAILED to initialise ports");
1707
		failret = -EIO;
1708
		goto hell_4;
1709
	}
1710
	if( ! configure_ports(wc) ){
1711
		cardcrit(boardnum, "Failed to configure ports.");
1712
		failret = -EIO;
1713
		goto hell_4;
1714
	}
1715
	cardinfo(wc->boardnum, "have %d configured ports", wc->portcount);
1716
1717
	if( ! span_initialize(wc) ) {
1718
		cardcrit(boardnum, "Failed to register with dahdi driver");
1719
		failret = -EFAULT;
1720
		goto hell_4;
1721
	}
1722
1723
	/* Finalize signalling  */
1724
	for (i=0; i < MAX_PORTS; ++i) {
1725
		if (wc->porttype[i] == VT_PORT_VDAA)
1726
		    wc->chans[i]->sigcap = DAHDI_SIG_FXSKS | DAHDI_SIG_FXSLS
1727
					| DAHDI_SIG_CLEAR | DAHDI_SIG_SF;
1728
		else if (wc->porttype[i] == VT_PORT_PROSLIC)
1729
		    wc->chans[i]->sigcap = DAHDI_SIG_FXOKS | DAHDI_SIG_FXOLS
1730
					| DAHDI_SIG_FXOGS | DAHDI_SIG_SF
1731
					| DAHDI_SIG_CLEAR | DAHDI_SIG_EM;
1732
		else if (wc->porttype[i])
1733
		    cardcrit(wc->boardnum, "Port %d has unknown type (%d)",
1734
					   i, wc->porttype[i]);
1735
	}
1736
1737
	/* Enable bus mastering */
1738
	pci_set_master(pdev);
1739
1740
	if (request_irq(pdev->irq, openpci_isr, DAHDI_IRQ_SHARED, NAME, wc)) {
1741
		cardcrit(boardnum, "Cant get IRQ!");
1742
		failret = -EIO;
1743
		goto hell_5;
1744
	}
1745
	cardinfo(boardnum, "Got IRQ %d", pdev->irq);
1746
1747
	enable_interrupts(wc);
1748
	start_dma(wc);
1749
1750
	cardinfo(boardnum,"Initialised card.");
1751
	return 0;
1752
1753
    hell_5:
1754
	dahdi_unregister(&wc->span);
1755
    hell_4:
1756
	if (wc->writechunk){
1757
		pci_free_consistent(pdev, VT_PCIDMA_BLOCKSIZE,
1758
				    (void*)wc->writechunk, wc->writedma);
1759
	}
1760
    hell_3:
1761
	outb(0x00, TJ_CNTL);
1762
	release_region(wc->ioaddr, 0xff);
1763
    hell_2:
1764
	cards[boardnum] = NULL;
1765
    hell:
1766
	kfree(wc);
1767
	return failret;
1768
} //}}}
1769
1770
static void __devexit openpci_remove_board(struct pci_dev *pdev)
1771
{ //{{{
1772
	struct openpci *wc = pci_get_drvdata(pdev);
1773
1774
	if(!wc) return;
1775
1776
	arm_monitor(wc,0);
1777
1778
	/* Stop DMA */
1779
	outb(0x00, TJ_OPER);
1780
	disable_interrupts(wc);
1781
1782
	//XXX Replace this usecount business...
1783
	//    and do this BEFORE we invalidate everything above...
1784
	//    check that we wont try to write to it in the meantime.
1785
	/* Release span, possibly delayed */
1786
	//XXX if (!wc->usecount) openpci_release(wc); else wc->dead = 1;
1787
1788
	dahdi_unregister(&wc->span);
1789
	outb(0x00, TJ_CNTL);
1790
1791
	pci_free_consistent(pdev, VT_PCIDMA_BLOCKSIZE, (void *)wc->writechunk, wc->writedma);
1792
	free_irq(pdev->irq, wc);
1793
1794
	release_region(wc->ioaddr, 0xff);
1795
1796
	mutex_lock(&cards_mutex);
1797
	cards[wc->boardnum] = NULL;
1798
	mutex_unlock(&cards_mutex);
1799
1800
	kfree(wc);
1801
	cardinfo(wc->boardnum, "Removed OpenPCI card.");
1802
} //}}}
1803
1804
static struct pci_device_id openpci_pci_tbl[] = {
1805
	{ 0xe159, 0x0001, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t) &wcopenpci },
1806
	{ 0 }
1807
};
1808
1809
MODULE_DEVICE_TABLE(pci, openpci_pci_tbl);
1810
1811
static struct pci_driver openpci_driver = {
1812
	name: 	  NAME,
1813
	probe: 	  openpci_probe_board,
1814
	remove:	  __devexit_p(openpci_remove_board),
1815
	suspend:  NULL,
1816
	resume:	  NULL,
1817
	id_table: openpci_pci_tbl,
1818
};
1819
1820
static int __init openpci_init(void)
1821
{
1822
	if( dahdi_pci_module(&openpci_driver) )
1823
		return -ENODEV;
1824
1825
	info("Module loaded %s", debug ? "with debug enabled" : "");
1826
	return 0;
1827
}
1828
1829
static void __exit openpci_cleanup(void)
1830
{
1831
	pci_unregister_driver(&openpci_driver);
1832
	info("Module exit");
1833
}
1834
1835
module_init(openpci_init);
1836
module_exit(openpci_cleanup);
1837
1838
MODULE_DESCRIPTION(DRIVER_DESCRIPTION);
1839
MODULE_AUTHOR(DRIVER_AUTHOR);
1840
MODULE_VERSION(DAHDI_VERSION);
1841
MODULE_LICENSE("GPL");
1842
(-)dahdi-linux-2.2.0.2/drivers/dahdi/zaphfc/base.c (+1706 lines)
Line 0 Link Here
1
/*
2
 * zaphfc.c - Dahdi driver for HFC-S PCI A based ISDN BRI cards
3
 *
4
 * Dahdi rewrite in hardhdlc mode
5
 * Jose A. Deniz <odicha@hotmail.com>
6
 *
7
 * Copyright (C) 2009, Jose A. Deniz
8
 * Copyright (C) 2006, headiisue GmbH; Jens Wilke
9
 * Copyright (C) 2004 Daniele Orlandi
10
 * Copyright (C) 2002, 2003, 2004, Junghanns.NET GmbH
11
 *
12
 * Jens Wilke <jw_vzaphfc@headissue.com>
13
 *
14
 * Original author of this code is
15
 * Daniele "Vihai" Orlandi <daniele@orlandi.com>
16
 *
17
 * Major rewrite of the driver made by
18
 * Klaus-Peter Junghanns <kpj@junghanns.net>
19
 *
20
 * This program is free software and may be modified and
21
 * distributed under the terms of the GNU Public License.
22
 *
23
 * Please read the README file for important infos.
24
 */
25
26
#include <linux/spinlock.h>
27
#include <linux/init.h>
28
#include <linux/pci.h>
29
#include <linux/interrupt.h>
30
#include <linux/module.h>
31
#include <linux/moduleparam.h>
32
#include <linux/version.h>
33
#include <linux/kernel.h>
34
#include <linux/delay.h>
35
#include <linux/proc_fs.h>
36
#include <linux/if_arp.h>
37
#include <linux/sched.h>
38
39
#include <dahdi/kernel.h>
40
41
#include "zaphfc.h"
42
#include "fifo.h"
43
44
#if CONFIG_PCI
45
46
#define DAHDI_B1 0
47
#define DAHDI_B2 1
48
#define DAHDI_D 2
49
50
#define D 0
51
#define B1 1
52
#define B2 2
53
54
/*
55
 * Mode Te for all
56
 */
57
static int modes;
58
static int nt_modes[hfc_MAX_BOARDS];
59
static int nt_modes_count;
60
static int force_l1_up;
61
static struct proc_dir_entry *hfc_proc_zaphfc_dir;
62
63
#ifdef DEBUG
64
int debug_level;
65
#endif
66
67
#ifndef FALSE
68
#define FALSE 0
69
#endif
70
#ifndef TRUE
71
#define TRUE (!FALSE)
72
#endif
73
74
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
75
#define	SET_PROC_DIRENTRY_OWNER(p)	do { (p)->owner = THIS_MODULE; } while(0);
76
#else
77
#define	SET_PROC_DIRENTRY_OWNER(p)	do { } while(0);
78
#endif
79
80
static struct pci_device_id hfc_pci_ids[] = {
81
	{PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_2BD0,
82
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
83
	{PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B000,
84
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
85
	{PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B006,
86
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
87
	{PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B007,
88
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
89
	{PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B008,
90
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
91
	{PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B009,
92
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
93
	{PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B00A,
94
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
95
	{PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B00B,
96
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
97
	{PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B00C,
98
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
99
	{PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B100,
100
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
101
	{PCI_VENDOR_ID_ABOCOM, PCI_DEVICE_ID_ABOCOM_2BD1,
102
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
103
	{PCI_VENDOR_ID_ASUSTEK, PCI_DEVICE_ID_ASUSTEK_0675,
104
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
105
	{PCI_VENDOR_ID_BERKOM, PCI_DEVICE_ID_BERKOM_T_CONCEPT,
106
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
107
	{PCI_VENDOR_ID_BERKOM, PCI_DEVICE_ID_BERKOM_A1T,
108
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
109
	{PCI_VENDOR_ID_ANIGMA, PCI_DEVICE_ID_ANIGMA_MC145575,
110
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
111
	{PCI_VENDOR_ID_ZOLTRIX, PCI_DEVICE_ID_ZOLTRIX_2BD0,
112
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
113
	{PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_DIGI_DF_M_IOM2_E,
114
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
115
	{PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_DIGI_DF_M_E,
116
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
117
	{PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_DIGI_DF_M_IOM2_A,
118
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
119
	{PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_DIGI_DF_M_A,
120
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
121
	{PCI_VENDOR_ID_SITECOM, PCI_DEVICE_ID_SITECOM_3069,
122
		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
123
	{0,}
124
};
125
126
MODULE_DEVICE_TABLE(pci, hfc_pci_ids);
127
128
static int __devinit hfc_probe(struct pci_dev *dev
129
			, const struct pci_device_id *ent);
130
static void __devexit hfc_remove(struct pci_dev *dev);
131
132
static struct pci_driver hfc_driver = {
133
	.name     = hfc_DRIVER_NAME,
134
	.id_table = hfc_pci_ids,
135
	.probe    = hfc_probe,
136
	.remove   = hfc_remove,
137
};
138
139
/******************************************
140
 * HW routines
141
 ******************************************/
142
143
static void hfc_softreset(struct hfc_card *card)
144
{
145
	printk(KERN_INFO hfc_DRIVER_PREFIX
146
		"card %d: "
147
		"resetting\n",
148
		card->cardnum);
149
150
/*
151
 * Softreset procedure. Put it on, wait and off again
152
 */
153
	hfc_outb(card, hfc_CIRM, hfc_CIRM_RESET);
154
	udelay(6);
155
	hfc_outb(card, hfc_CIRM, 0);
156
157
	set_current_state(TASK_UNINTERRUPTIBLE);
158
	schedule_timeout((hfc_RESET_DELAY * HZ) / 1000);
159
}
160
161
static void hfc_resetCard(struct hfc_card *card)
162
{
163
	card->regs.m1 = 0;
164
	hfc_outb(card, hfc_INT_M1, card->regs.m1);
165
166
	card->regs.m2 = 0;
167
	hfc_outb(card, hfc_INT_M2, card->regs.m2);
168
169
	hfc_softreset(card);
170
171
	card->regs.trm = 0;
172
	hfc_outb(card, hfc_TRM, card->regs.trm);
173
174
	/*
175
	 * Select the non-capacitive line mode for the S/T interface
176
	 */
177
	card->regs.sctrl = hfc_SCTRL_NONE_CAP;
178
179
	if (card->nt_mode) {
180
		/*
181
		 * ST-Bit delay for NT-Mode
182
		 */
183
		hfc_outb(card, hfc_CLKDEL, hfc_CLKDEL_NT);
184
185
		card->regs.sctrl |= hfc_SCTRL_MODE_NT;
186
	} else {
187
		/*
188
		 * ST-Bit delay for TE-Mode
189
		 */
190
		hfc_outb(card, hfc_CLKDEL, hfc_CLKDEL_TE);
191
192
		card->regs.sctrl |= hfc_SCTRL_MODE_TE;
193
	}
194
195
	hfc_outb(card, hfc_SCTRL, card->regs.sctrl);
196
197
	/*
198
	 * S/T Auto awake
199
	 */
200
	card->regs.sctrl_e = hfc_SCTRL_E_AUTO_AWAKE;
201
	hfc_outb(card, hfc_SCTRL_E, card->regs.sctrl_e);
202
203
	/*
204
	 * No B-channel enabled at startup
205
	 */
206
	card->regs.sctrl_r = 0;
207
	hfc_outb(card, hfc_SCTRL_R, card->regs.sctrl_r);
208
209
	/*
210
	 * HFC Master Mode
211
	 */
212
	hfc_outb(card, hfc_MST_MODE, hfc_MST_MODE_MASTER);
213
214
	/*
215
	 * Connect internal blocks
216
	 */
217
	card->regs.connect =
218
		hfc_CONNECT_B1_HFC_from_ST |
219
		hfc_CONNECT_B1_ST_from_HFC |
220
		hfc_CONNECT_B1_GCI_from_HFC |
221
		hfc_CONNECT_B2_HFC_from_ST |
222
		hfc_CONNECT_B2_ST_from_HFC |
223
		hfc_CONNECT_B2_GCI_from_HFC;
224
	hfc_outb(card, hfc_CONNECT, card->regs.connect);
225
226
	/*
227
	 * All bchans are HDLC by default, not useful, actually
228
	 * since mode is set during open()
229
	 */
230
	hfc_outb(card, hfc_CTMT, 0);
231
232
	/*
233
	 * bit order
234
	 */
235
	hfc_outb(card, hfc_CIRM, 0);
236
237
	/*
238
	 * Enable D-rx FIFO. At least one FIFO must be enabled (by specs)
239
	 */
240
	card->regs.fifo_en = hfc_FIFOEN_DRX;
241
	hfc_outb(card, hfc_FIFO_EN, card->regs.fifo_en);
242
243
	card->late_irqs = 0;
244
245
	/*
246
	 * Clear already pending ints
247
	 */
248
	hfc_inb(card, hfc_INT_S1);
249
	hfc_inb(card, hfc_INT_S2);
250
251
	/*
252
	 * Enable IRQ output
253
	 */
254
	card->regs.m1 = hfc_INTS_DREC | hfc_INTS_L1STATE | hfc_INTS_TIMER;
255
	hfc_outb(card, hfc_INT_M1, card->regs.m1);
256
257
	card->regs.m2 = hfc_M2_IRQ_ENABLE;
258
	hfc_outb(card, hfc_INT_M2, card->regs.m2);
259
260
	/*
261
	 * Unlocks the states machine
262
	 */
263
	hfc_outb(card, hfc_STATES, 0);
264
265
	/*
266
	 * There's no need to explicitly activate L1 now.
267
	 * Activation is managed inside the interrupt routine.
268
	 */
269
}
270
271
static void hfc_update_fifo_state(struct hfc_card *card)
272
{
273
	/*
274
	 * I'm not sure if irqsave is needed but there could be a race
275
	 * condition since hfc_update_fifo_state could be called from
276
	 * both the IRQ handler and the *_(open|close) functions
277
	 */
278
279
	unsigned long flags;
280
	spin_lock_irqsave(&card->chans[B1].lock, flags);
281
	if (!card->fifo_suspended &&
282
		(card->chans[B1].status == open_framed ||
283
		card->chans[B1].status == open_voice)) {
284
285
		if (!(card->regs.fifo_en & hfc_FIFOEN_B1RX)) {
286
			card->regs.fifo_en |= hfc_FIFOEN_B1RX;
287
			hfc_clear_fifo_rx(&card->chans[B1].rx);
288
		}
289
290
		if (!(card->regs.fifo_en & hfc_FIFOEN_B1TX)) {
291
			card->regs.fifo_en |= hfc_FIFOEN_B1TX;
292
			hfc_clear_fifo_tx(&card->chans[B1].tx);
293
		}
294
	} else {
295
		if (card->regs.fifo_en & hfc_FIFOEN_B1RX)
296
			card->regs.fifo_en &= ~hfc_FIFOEN_B1RX;
297
		if (card->regs.fifo_en & hfc_FIFOEN_B1TX)
298
			card->regs.fifo_en &= ~hfc_FIFOEN_B1TX;
299
	}
300
	spin_unlock_irqrestore(&card->chans[B1].lock, flags);
301
302
	spin_lock_irqsave(&card->chans[B2].lock, flags);
303
	if (!card->fifo_suspended &&
304
		(card->chans[B2].status == open_framed ||
305
		card->chans[B2].status == open_voice ||
306
		card->chans[B2].status == sniff_aux)) {
307
308
		if (!(card->regs.fifo_en & hfc_FIFOEN_B2RX)) {
309
			card->regs.fifo_en |= hfc_FIFOEN_B2RX;
310
			hfc_clear_fifo_rx(&card->chans[B2].rx);
311
		}
312
313
		if (!(card->regs.fifo_en & hfc_FIFOEN_B2TX)) {
314
			card->regs.fifo_en |= hfc_FIFOEN_B2TX;
315
			hfc_clear_fifo_tx(&card->chans[B2].tx);
316
		}
317
	} else {
318
		if (card->regs.fifo_en & hfc_FIFOEN_B2RX)
319
			card->regs.fifo_en &= ~hfc_FIFOEN_B2RX;
320
		if (card->regs.fifo_en & hfc_FIFOEN_B2TX)
321
			card->regs.fifo_en &= ~hfc_FIFOEN_B2TX;
322
	}
323
	spin_unlock_irqrestore(&card->chans[B2].lock, flags);
324
325
	spin_lock_irqsave(&card->chans[D].lock, flags);
326
	if (!card->fifo_suspended &&
327
		card->chans[D].status == open_framed) {
328
329
		if (!(card->regs.fifo_en & hfc_FIFOEN_DTX)) {
330
			card->regs.fifo_en |= hfc_FIFOEN_DTX;
331
332
			card->chans[D].tx.ugly_framebuf_size = 0;
333
			card->chans[D].tx.ugly_framebuf_off = 0;
334
		}
335
	} else {
336
		if (card->regs.fifo_en & hfc_FIFOEN_DTX)
337
			card->regs.fifo_en &= ~hfc_FIFOEN_DTX;
338
	}
339
	spin_unlock_irqrestore(&card->chans[D].lock, flags);
340
341
	hfc_outb(card, hfc_FIFO_EN, card->regs.fifo_en);
342
}
343
344
static inline void hfc_suspend_fifo(struct hfc_card *card)
345
{
346
	card->fifo_suspended = TRUE;
347
348
	hfc_update_fifo_state(card);
349
350
	/*
351
	 * When L1 goes down D rx receives garbage; it is nice to
352
	 * clear it to avoid a CRC error on reactivation
353
	 * udelay is needed because the FIFO deactivation happens
354
	 * in 250us
355
	 */
356
	udelay(250);
357
	hfc_clear_fifo_rx(&card->chans[D].rx);
358
359
#ifdef DEBUG
360
	if (debug_level >= 3) {
361
		printk(KERN_DEBUG hfc_DRIVER_PREFIX
362
			"card %d: "
363
			"FIFOs suspended\n",
364
			card->cardnum);
365
	}
366
#endif
367
}
368
369
static inline void hfc_resume_fifo(struct hfc_card *card)
370
{
371
	card->fifo_suspended = FALSE;
372
373
	hfc_update_fifo_state(card);
374
375
#ifdef DEBUG
376
	if (debug_level >= 3) {
377
		printk(KERN_DEBUG hfc_DRIVER_PREFIX
378
			"card %d: "
379
			"FIFOs resumed\n",
380
			card->cardnum);
381
	}
382
#endif
383
}
384
385
static void hfc_check_l1_up(struct hfc_card *card)
386
{
387
	if ((!card->nt_mode && card->l1_state != 7)
388
		|| (card->nt_mode && card->l1_state != 3)) {
389
390
		hfc_outb(card, hfc_STATES, hfc_STATES_DO_ACTION |
391
			hfc_STATES_ACTIVATE|
392
				hfc_STATES_NT_G2_G3);
393
394
	/*
395
	 * 0 because this is quite verbose when an inferface is unconnected, jaw
396
	 */
397
#if 0
398
		if (debug_level >= 1) {
399
			printk(KERN_DEBUG hfc_DRIVER_PREFIX
400
				"card %d: "
401
				"L1 is down, bringing up L1.\n",
402
				card->cardnum);
403
		}
404
#endif
405
	}
406
}
407
408
409
/*******************
410
 * Dahdi interface *
411
 *******************/
412
413
static int hfc_zap_open(struct dahdi_chan *zaptel_chan)
414
{
415
	struct hfc_chan_duplex *chan = zaptel_chan->pvt;
416
	struct hfc_card *card = chan->card;
417
418
	spin_lock(&chan->lock);
419
420
	switch (chan->number) {
421
	case D:
422
		if (chan->status != free &&
423
			chan->status != open_framed) {
424
			spin_unlock(&chan->lock);
425
			return -EBUSY;
426
		}
427
		chan->status = open_framed;
428
	break;
429
430
	case B1:
431
	case B2:
432
		if (chan->status != free) {
433
			spin_unlock(&chan->lock);
434
			return -EBUSY;
435
		}
436
		chan->status = open_voice;
437
	break;
438
	}
439
440
	chan->open_by_zaptel = TRUE;
441
	try_module_get(THIS_MODULE);
442
	spin_unlock(&chan->lock);
443
444
	switch (chan->number) {
445
	case D:
446
	break;
447
448
	case B1:
449
		card->regs.m2 |= hfc_M2_PROC_TRANS;
450
		/*
451
		 * Enable transparent mode
452
		 */
453
		card->regs.ctmt |= hfc_CTMT_TRANSB1;
454
		/*
455
		* Reversed bit order
456
		*/
457
		card->regs.cirm |= hfc_CIRM_B1_REV;
458
		/*
459
		 * Enable transmission
460
		 */
461
		card->regs.sctrl |= hfc_SCTRL_B1_ENA;
462
		/*
463
		 * Enable reception
464
		 */
465
		card->regs.sctrl_r |= hfc_SCTRL_R_B1_ENA;
466
	break;
467
468
	case B2:
469
		card->regs.m2 |= hfc_M2_PROC_TRANS;
470
		card->regs.ctmt |= hfc_CTMT_TRANSB2;
471
		card->regs.cirm |= hfc_CIRM_B2_REV;
472
		card->regs.sctrl |= hfc_SCTRL_B2_ENA;
473
		card->regs.sctrl_r |= hfc_SCTRL_R_B2_ENA;
474
	break;
475
476
	}
477
478
	/*
479
	 * If not already enabled, enable processing transition (8KHz)
480
	 * interrupt
481
	 */
482
	hfc_outb(card, hfc_INT_M2, card->regs.m2);
483
	hfc_outb(card, hfc_CTMT, card->regs.ctmt);
484
	hfc_outb(card, hfc_CIRM, card->regs.cirm);
485
	hfc_outb(card, hfc_SCTRL, card->regs.sctrl);
486
	hfc_outb(card, hfc_SCTRL_R, card->regs.sctrl_r);
487
488
	hfc_update_fifo_state(card);
489
490
	printk(KERN_INFO hfc_DRIVER_PREFIX
491
		"card %d: "
492
		"chan %s opened as %s.\n",
493
		card->cardnum,
494
		chan->name,
495
		zaptel_chan->name);
496
497
	return 0;
498
}
499
500
static int hfc_zap_close(struct dahdi_chan *zaptel_chan)
501
{
502
	struct hfc_chan_duplex *chan = zaptel_chan->pvt;
503
	struct hfc_card *card = chan->card;
504
505
	if (!card) {
506
		printk(KERN_CRIT hfc_DRIVER_PREFIX
507
			"hfc_zap_close called with NULL card\n");
508
		return -1;
509
	}
510
511
	spin_lock(&chan->lock);
512
513
	if (chan->status == free) {
514
		spin_unlock(&chan->lock);
515
		return -EINVAL;
516
	}
517
518
	chan->status = free;
519
	chan->open_by_zaptel = FALSE;
520
521
	spin_unlock(&chan->lock);
522
523
	switch (chan->number) {
524
	case D:
525
	break;
526
527
	case B1:
528
		card->regs.ctmt &= ~hfc_CTMT_TRANSB1;
529
		card->regs.cirm &= ~hfc_CIRM_B1_REV;
530
		card->regs.sctrl &= ~hfc_SCTRL_B1_ENA;
531
		card->regs.sctrl_r &= ~hfc_SCTRL_R_B1_ENA;
532
	break;
533
534
	case B2:
535
		card->regs.ctmt &= ~hfc_CTMT_TRANSB2;
536
		card->regs.cirm &= ~hfc_CIRM_B2_REV;
537
		card->regs.sctrl &= ~hfc_SCTRL_B2_ENA;
538
		card->regs.sctrl_r &= ~hfc_SCTRL_R_B2_ENA;
539
	break;
540
	}
541
542
	if (card->chans[B1].status == free &&
543
		card->chans[B2].status == free)
544
		card->regs.m2 &= ~hfc_M2_PROC_TRANS;
545
546
	hfc_outb(card, hfc_INT_M2, card->regs.m2);
547
	hfc_outb(card, hfc_CTMT, card->regs.ctmt);
548
	hfc_outb(card, hfc_CIRM, card->regs.cirm);
549
	hfc_outb(card, hfc_SCTRL, card->regs.sctrl);
550
	hfc_outb(card, hfc_SCTRL_R, card->regs.sctrl_r);
551
552
	hfc_update_fifo_state(card);
553
554
	module_put(THIS_MODULE);
555
556
	printk(KERN_INFO hfc_DRIVER_PREFIX
557
		"card %d: "
558
		"chan %s closed as %s.\n",
559
		card->cardnum,
560
		chan->name,
561
		zaptel_chan->name);
562
563
	return 0;
564
}
565
566
static int hfc_zap_rbsbits(struct dahdi_chan *chan, int bits)
567
{
568
	return 0;
569
}
570
571
static int hfc_zap_ioctl(struct dahdi_chan *chan,
572
		unsigned int cmd, unsigned long data)
573
{
574
	switch (cmd) {
575
576
	default:
577
		return -ENOTTY;
578
	}
579
580
	return 0;
581
}
582
583
static void hfc_hdlc_hard_xmit(struct dahdi_chan *d_chan)
584
{
585
	struct hfc_chan_duplex *chan = d_chan->pvt;
586
	struct hfc_card *card = chan->card;
587
	struct dahdi_hfc *hfccard = card->ztdev;
588
589
	atomic_inc(&hfccard->hdlc_pending);
590
591
}
592
593
static int hfc_zap_startup(struct dahdi_span *span)
594
{
595
    struct dahdi_hfc *zthfc = span->pvt;
596
    struct hfc_card *hfctmp = zthfc->card;
597
    int alreadyrunning;
598
599
	if (!hfctmp) {
600
		printk(KERN_INFO hfc_DRIVER_PREFIX
601
			"card %d: "
602
			"no card for span at startup!\n",
603
			hfctmp->cardnum);
604
	}
605
606
	alreadyrunning = span->flags & DAHDI_FLAG_RUNNING;
607
608
	if (!alreadyrunning)
609
		span->flags |= DAHDI_FLAG_RUNNING;
610
611
	return 0;
612
}
613
614
static int hfc_zap_shutdown(struct dahdi_span *span)
615
{
616
	return 0;
617
}
618
619
static int hfc_zap_maint(struct dahdi_span *span, int cmd)
620
{
621
	return 0;
622
}
623
624
static int hfc_zap_chanconfig(struct dahdi_chan *d_chan, int sigtype)
625
{
626
	struct hfc_chan_duplex *chan = d_chan->pvt;
627
	struct hfc_card *card = chan->card;
628
	struct dahdi_hfc *hfccard = card->ztdev;
629
630
	if ((sigtype == DAHDI_SIG_HARDHDLC) && (hfccard->sigchan == d_chan)) {
631
		hfccard->sigactive = 0;
632
		atomic_set(&hfccard->hdlc_pending, 0);
633
	}
634
635
	return 0;
636
}
637
638
static int hfc_zap_spanconfig(struct dahdi_span *span,
639
		struct dahdi_lineconfig *lc)
640
{
641
	span->lineconfig = lc->lineconfig;
642
643
	return 0;
644
}
645
646
static int hfc_zap_initialize(struct dahdi_hfc *hfccard)
647
{
648
	 struct hfc_card *hfctmp = hfccard->card;
649
	int i;
650
651
	memset(&hfccard->span, 0x0, sizeof(struct dahdi_span));
652
	sprintf(hfccard->span.name, "ZTHFC%d", hfctmp->cardnum + 1);
653
	sprintf(hfccard->span.desc,
654
			"HFC-S PCI A ISDN card %d [%s] ",
655
			hfctmp->cardnum,
656
			hfctmp->nt_mode ? "NT" : "TE");
657
	hfccard->span.spantype = hfctmp->nt_mode ? "NT" : "TE";
658
	hfccard->span.manufacturer = "Cologne Chips";
659
	hfccard->span.spanconfig = hfc_zap_spanconfig;
660
	hfccard->span.chanconfig = hfc_zap_chanconfig;
661
	hfccard->span.startup = hfc_zap_startup;
662
	hfccard->span.shutdown = hfc_zap_shutdown;
663
	hfccard->span.maint = hfc_zap_maint;
664
	hfccard->span.rbsbits = hfc_zap_rbsbits;
665
	hfccard->span.open = hfc_zap_open;
666
	hfccard->span.close = hfc_zap_close;
667
	hfccard->span.ioctl = hfc_zap_ioctl;
668
	hfccard->span.hdlc_hard_xmit = hfc_hdlc_hard_xmit;
669
	hfccard->span.flags = 0;
670
	hfccard->span.irq = hfctmp->pcidev->irq;
671
	dahdi_copy_string(hfccard->span.devicetype, "HFC-S PCI-A ISDN",
672
			sizeof(hfccard->span.devicetype));
673
	sprintf(hfccard->span.location, "PCI Bus %02d Slot %02d",
674
			hfctmp->pcidev->bus->number,
675
			PCI_SLOT(hfctmp->pcidev->devfn) + 1);
676
	hfccard->span.chans = hfccard->_chans;
677
	hfccard->span.channels = 3;
678
	for (i = 0; i < hfccard->span.channels; i++)
679
		hfccard->_chans[i] = &hfccard->chans[i];
680
	hfccard->span.deflaw = DAHDI_LAW_ALAW;
681
	hfccard->span.linecompat = DAHDI_CONFIG_AMI | DAHDI_CONFIG_CCS;
682
	hfccard->span.offset = 0;
683
	init_waitqueue_head(&hfccard->span.maintq);
684
	hfccard->span.pvt = hfccard;
685
686
	for (i = 0; i < hfccard->span.channels; i++) {
687
		memset(&hfccard->chans[i], 0x0, sizeof(struct dahdi_chan));
688
689
		sprintf(hfccard->chans[i].name,
690
				"ZTHFC%d/%d/%d",
691
				hfctmp->cardnum + 1, 0, i + 1);
692
693
		printk(KERN_INFO hfc_DRIVER_PREFIX
694
			"card %d: "
695
			"registered %s\n",
696
			hfctmp->cardnum,
697
			hfccard->chans[i].name);
698
699
		if (i == hfccard->span.channels - 1) {
700
			hfccard->chans[i].sigcap = DAHDI_SIG_HARDHDLC;
701
			hfccard->sigchan = &hfccard->chans[D];
702
			hfccard->sigactive = 0;
703
			atomic_set(&hfccard->hdlc_pending, 0);
704
		} else {
705
			hfccard->chans[i].sigcap =
706
				DAHDI_SIG_CLEAR | DAHDI_SIG_DACS;
707
		}
708
709
		hfccard->chans[i].chanpos = i + 1;
710
	}
711
712
	hfccard->chans[DAHDI_D].readchunk  =
713
		hfctmp->chans[D].rx.zaptel_buffer;
714
715
	hfccard->chans[DAHDI_D].writechunk =
716
		hfctmp->chans[D].tx.zaptel_buffer;
717
718
	hfccard->chans[DAHDI_D].pvt = &hfctmp->chans[D];
719
720
	hfccard->chans[DAHDI_B1].readchunk  =
721
		hfctmp->chans[B1].rx.zaptel_buffer;
722
723
	hfccard->chans[DAHDI_B1].writechunk =
724
		hfctmp->chans[B1].tx.zaptel_buffer;
725
726
	hfccard->chans[DAHDI_B1].pvt = &hfctmp->chans[B1];
727
728
	hfccard->chans[DAHDI_B2].readchunk  =
729
		hfctmp->chans[B2].rx.zaptel_buffer;
730
731
	hfccard->chans[DAHDI_B2].writechunk =
732
		hfctmp->chans[B2].tx.zaptel_buffer;
733
734
	hfccard->chans[DAHDI_B2].pvt = &hfctmp->chans[B2];
735
736
	if (dahdi_register(&hfccard->span, 0)) {
737
		printk(KERN_CRIT "unable to register zaptel device!\n");
738
		return -1;
739
	}
740
741
	return 0;
742
}
743
744
static void hfc_zap_transmit(struct hfc_chan_simplex *chan)
745
{
746
	hfc_fifo_put(chan, chan->zaptel_buffer, DAHDI_CHUNKSIZE);
747
}
748
749
static void hfc_zap_receive(struct hfc_chan_simplex *chan)
750
{
751
	hfc_fifo_get(chan, chan->zaptel_buffer, DAHDI_CHUNKSIZE);
752
}
753
754
/******************************************
755
 * Interrupt Handler
756
 ******************************************/
757
758
static void hfc_handle_timer_interrupt(struct hfc_card *card);
759
static void hfc_handle_state_interrupt(struct hfc_card *card);
760
static void hfc_handle_processing_interrupt(struct hfc_card *card);
761
static void hfc_frame_arrived(struct hfc_chan_duplex *chan);
762
static void hfc_handle_voice(struct hfc_card *card);
763
764
#if (KERNEL_VERSION(2, 6, 24) < LINUX_VERSION_CODE)
765
static irqreturn_t hfc_interrupt(int irq, void *dev_id)
766
#else
767
static irqreturn_t hfc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
768
#endif
769
{
770
	struct hfc_card *card = dev_id;
771
	unsigned long flags;
772
	u8 status, s1, s2;
773
774
	if (!card) {
775
		printk(KERN_CRIT hfc_DRIVER_PREFIX
776
			"spurious interrupt (IRQ %d)\n",
777
			irq);
778
		return IRQ_NONE;
779
	}
780
781
	spin_lock_irqsave(&card->lock, flags);
782
	status = hfc_inb(card, hfc_STATUS);
783
	if (!(status & hfc_STATUS_ANYINT)) {
784
		/*
785
		 * maybe we are sharing the irq
786
		 */
787
		spin_unlock_irqrestore(&card->lock, flags);
788
		return IRQ_NONE;
789
	}
790
791
	/* We used to ingore the IRQ when the card was in processing
792
	 * state but apparently there is no restriction to access the
793
	 * card in such state:
794
	 *
795
	 * Joerg Ciesielski wrote:
796
	 * > There is no restriction for the IRQ handler to access
797
	 * > HFC-S PCI during processing phase. A IRQ latency of 375 us
798
	 * > is also no problem since there are no interrupt sources in
799
	 * > HFC-S PCI which must be handled very fast.
800
	 * > Due to its deep fifos the IRQ latency can be several ms with
801
	 * > out the risk of loosing data. Even the S/T state interrupts
802
	 * > must not be handled with a latency less than <5ms.
803
	 * >
804
	 * > The processing phase only indicates that HFC-S PCI is
805
	 * > processing the Fifos as PCI master so that data is read and
806
	 * > written in the 32k memory window. But there is no restriction
807
	 * > to access data in the memory window during this time.
808
	 *
809
	 * // if (status & hfc_STATUS_PCI_PROC) {
810
	 * // return IRQ_HANDLED;
811
	 * // }
812
	 */
813
814
	s1 = hfc_inb(card, hfc_INT_S1);
815
	s2 = hfc_inb(card, hfc_INT_S2);
816
817
	if (s1 != 0) {
818
		if (s1 & hfc_INTS_TIMER) {
819
			/*
820
			 * timer (bit 7)
821
			 */
822
			hfc_handle_timer_interrupt(card);
823
		}
824
825
		if (s1 & hfc_INTS_L1STATE) {
826
			/*
827
			 * state machine (bit 6)
828
			 */
829
			hfc_handle_state_interrupt(card);
830
		}
831
832
		if (s1 & hfc_INTS_DREC) {
833
			/*
834
			 * D chan RX (bit 5)
835
			 */
836
			hfc_frame_arrived(&card->chans[D]);
837
		}
838
839
		if (s1 & hfc_INTS_B1REC) {
840
			/*
841
			 * B1 chan RX (bit 3)
842
			 */
843
			hfc_frame_arrived(&card->chans[B1]);
844
		}
845
846
		if (s1 & hfc_INTS_B2REC) {
847
			/*
848
			 * B2 chan RX (bit 4)
849
			 */
850
			hfc_frame_arrived(&card->chans[B2]);
851
		}
852
853
		if (s1 & hfc_INTS_DTRANS) {
854
			/*
855
			 * D chan TX (bit 2)
856
			 */
857
		}
858
859
		if (s1 & hfc_INTS_B1TRANS) {
860
			/*
861
			 * B1 chan TX (bit 0)
862
			 */
863
		}
864
865
		if (s1 & hfc_INTS_B2TRANS) {
866
			/*
867
			 * B2 chan TX (bit 1)
868
			 */
869
		}
870
871
	}
872
873
	if (s2 != 0) {
874
		if (s2 & hfc_M2_PMESEL) {
875
			/*
876
			 * kaboom irq (bit 7)
877
			 *
878
			 * CologneChip says:
879
			 *
880
			 * the meaning of this fatal error bit is that HFC-S
881
			 * PCI as PCI master could not access the PCI bus
882
			 * within 125us to finish its data processing. If this
883
			 * happens only very seldom it does not cause big
884
			 * problems but of course some B-channel or D-channel
885
			 * data will be corrupted due to this event.
886
			 *
887
			 * Unfortunately this bit is only set once after the
888
			 * problem occurs and can only be reseted by a
889
			 * software reset. That means it is not easily
890
			 * possible to check how often this fatal error
891
			 * happens.
892
			 *
893
			 */
894
895
			if (!card->sync_loss_reported) {
896
				printk(KERN_CRIT hfc_DRIVER_PREFIX
897
					"card %d: "
898
					"sync lost, pci performance too low!\n",
899
					card->cardnum);
900
901
				card->sync_loss_reported = TRUE;
902
			}
903
		}
904
905
		if (s2 & hfc_M2_GCI_MON_REC) {
906
			/*
907
			 * RxR monitor channel (bit 2)
908
			 */
909
		}
910
911
		if (s2 & hfc_M2_GCI_I_CHG) {
912
			/*
913
			 * GCI I-change  (bit 1)
914
			 */
915
		}
916
917
		if (s2 & hfc_M2_PROC_TRANS) {
918
			/*
919
			 * processing/non-processing transition  (bit 0)
920
			 */
921
			hfc_handle_processing_interrupt(card);
922
		}
923
924
	}
925
926
	spin_unlock_irqrestore(&card->lock, flags);
927
928
	return IRQ_HANDLED;
929
}
930
931
static void hfc_handle_timer_interrupt(struct hfc_card *card)
932
{
933
	if (card->ignore_first_timer_interrupt) {
934
		card->ignore_first_timer_interrupt = FALSE;
935
		return;
936
	}
937
938
	if ((card->nt_mode && card->l1_state == 3) ||
939
		(!card->nt_mode && card->l1_state == 7)) {
940
941
		card->regs.ctmt &= ~hfc_CTMT_TIMER_MASK;
942
		hfc_outb(card, hfc_CTMT, card->regs.ctmt);
943
944
		hfc_resume_fifo(card);
945
	}
946
}
947
948
static void hfc_handle_state_interrupt(struct hfc_card *card)
949
{
950
	u8 new_state = hfc_inb(card, hfc_STATES)  & hfc_STATES_STATE_MASK;
951
952
#ifdef DEBUG
953
	if (debug_level >= 1) {
954
		printk(KERN_DEBUG hfc_DRIVER_PREFIX
955
			"card %d: "
956
			"layer 1 state = %c%d\n",
957
			card->cardnum,
958
			card->nt_mode ? 'G' : 'F',
959
			new_state);
960
	}
961
#endif
962
963
	if (card->nt_mode) {
964
		/*
965
		 * NT mode
966
		 */
967
968
		if (new_state == 3) {
969
			/*
970
			 * fix to G3 state (see specs)
971
			 */
972
			hfc_outb(card, hfc_STATES, hfc_STATES_LOAD_STATE | 3);
973
		}
974
975
		if (new_state == 3 && card->l1_state != 3)
976
			hfc_resume_fifo(card);
977
978
		if (new_state != 3 && card->l1_state == 3)
979
			hfc_suspend_fifo(card);
980
981
	} else {
982
		if (new_state == 3) {
983
			/*
984
			 * Keep L1 up... zaptel & libpri expects
985
			 * a always up L1...
986
			 * Enable only  when using an unpatched libpri
987
			 */
988
989
			if (force_l1_up) {
990
				hfc_outb(card, hfc_STATES,
991
					hfc_STATES_DO_ACTION |
992
					hfc_STATES_ACTIVATE|
993
					hfc_STATES_NT_G2_G3);
994
			}
995
		}
996
997
		if (new_state == 7 && card->l1_state != 7) {
998
			/*
999
			 * TE is now active, schedule FIFO activation after
1000
			 * some time, otherwise the first frames are lost
1001
			 */
1002
1003
			card->regs.ctmt |= hfc_CTMT_TIMER_50 |
1004
				hfc_CTMT_TIMER_CLEAR;
1005
			hfc_outb(card, hfc_CTMT, card->regs.ctmt);
1006
1007
			/*
1008
			 * Activating the timer firest an
1009
			 * interrupt immediately, we
1010
			 * obviously need to ignore it
1011
			 */
1012
			card->ignore_first_timer_interrupt = TRUE;
1013
		}
1014
1015
		if (new_state != 7 && card->l1_state == 7) {
1016
			/*
1017
			 * TE has become inactive, disable FIFO
1018
			 */
1019
			hfc_suspend_fifo(card);
1020
		}
1021
	}
1022
1023
	card->l1_state = new_state;
1024
}
1025
1026
static void hfc_handle_processing_interrupt(struct hfc_card *card)
1027
{
1028
	int available_bytes = 0;
1029
1030
	/*
1031
	 * Synchronize with the first enabled channel
1032
	 */
1033
	if (card->regs.fifo_en & hfc_FIFOEN_B1RX)
1034
		available_bytes = hfc_fifo_used_rx(&card->chans[B1].rx);
1035
	if (card->regs.fifo_en & hfc_FIFOEN_B2RX)
1036
		available_bytes = hfc_fifo_used_rx(&card->chans[B2].rx);
1037
	else
1038
		available_bytes = -1;
1039
1040
	if ((available_bytes == -1 && card->ticks == 8) ||
1041
		available_bytes >= DAHDI_CHUNKSIZE + hfc_RX_FIFO_PRELOAD) {
1042
		card->ticks = 0;
1043
1044
		if (available_bytes > DAHDI_CHUNKSIZE*2 + hfc_RX_FIFO_PRELOAD) {
1045
			card->late_irqs++;
1046
			/*
1047
			 * we are out of sync, clear fifos, jaw
1048
			 */
1049
			hfc_clear_fifo_rx(&card->chans[B1].rx);
1050
			hfc_clear_fifo_tx(&card->chans[B1].tx);
1051
			hfc_clear_fifo_rx(&card->chans[B2].rx);
1052
			hfc_clear_fifo_tx(&card->chans[B2].tx);
1053
1054
#ifdef DEBUG
1055
			if (debug_level >= 4) {
1056
				printk(KERN_DEBUG hfc_DRIVER_PREFIX
1057
					"card %d: "
1058
					"late IRQ, %d bytes late\n",
1059
					card->cardnum,
1060
					available_bytes -
1061
						(DAHDI_CHUNKSIZE +
1062
						 hfc_RX_FIFO_PRELOAD));
1063
			}
1064
#endif
1065
		} else {
1066
			hfc_handle_voice(card);
1067
		}
1068
	}
1069
1070
	card->ticks++;
1071
}
1072
1073
1074
static void hfc_handle_voice(struct hfc_card *card)
1075
{
1076
	struct dahdi_hfc *hfccard = card->ztdev;
1077
	int frame_left, res;
1078
	unsigned char buf[hfc_HDLC_BUF_LEN];
1079
	unsigned int size = sizeof(buf) / sizeof(buf[0]);
1080
1081
1082
	if (card->chans[B1].status != open_voice &&
1083
		card->chans[B2].status != open_voice)
1084
		return;
1085
1086
	dahdi_transmit(&hfccard->span);
1087
1088
	if (card->regs.fifo_en & hfc_FIFOEN_B1TX)
1089
		hfc_zap_transmit(&card->chans[B1].tx);
1090
	if (card->regs.fifo_en & hfc_FIFOEN_B2TX)
1091
		hfc_zap_transmit(&card->chans[B2].tx);
1092
1093
	/*
1094
	 * dahdi hdlc frame tx
1095
	 */
1096
1097
	if (atomic_read(&hfccard->hdlc_pending)) {
1098
		hfc_check_l1_up(card);
1099
		res = dahdi_hdlc_getbuf(hfccard->sigchan, buf, &size);
1100
			if (size > 0) {
1101
				hfccard->sigactive = 1;
1102
				memcpy(card->chans[D].tx.ugly_framebuf +
1103
				card->chans[D].tx.ugly_framebuf_size,
1104
				buf, size);
1105
				card->chans[D].tx.ugly_framebuf_size += size;
1106
			if (res != 0) {
1107
					hfc_fifo_put_frame(&card->chans[D].tx,
1108
					card->chans[D].tx.ugly_framebuf,
1109
					card->chans[D].tx.ugly_framebuf_size);
1110
					++hfccard->frames_out;
1111
					hfccard->sigactive = 0;
1112
					card->chans[D].tx.ugly_framebuf_size
1113
						= 0;
1114
					atomic_dec(&hfccard->hdlc_pending);
1115
				}
1116
			}
1117
	}
1118
	/*
1119
	 * dahdi hdlc frame tx done
1120
	 */
1121
1122
	if (card->regs.fifo_en & hfc_FIFOEN_B1RX)
1123
		hfc_zap_receive(&card->chans[B1].rx);
1124
	else
1125
		memset(&card->chans[B1].rx.zaptel_buffer, 0x7f,
1126
			sizeof(card->chans[B1].rx.zaptel_buffer));
1127
1128
	if (card->regs.fifo_en & hfc_FIFOEN_B2RX)
1129
		hfc_zap_receive(&card->chans[B2].rx);
1130
	else
1131
		memset(&card->chans[B2].rx.zaptel_buffer, 0x7f,
1132
			sizeof(card->chans[B1].rx.zaptel_buffer));
1133
1134
	/*
1135
	 * Echo cancellation
1136
	 */
1137
	dahdi_ec_chunk(&hfccard->chans[DAHDI_B1],
1138
			card->chans[B1].rx.zaptel_buffer,
1139
			card->chans[B1].tx.zaptel_buffer);
1140
	dahdi_ec_chunk(&hfccard->chans[DAHDI_B2],
1141
			card->chans[B2].rx.zaptel_buffer,
1142
			card->chans[B2].tx.zaptel_buffer);
1143
1144
	/*
1145
	 * dahdi hdlc frame rx
1146
	 */
1147
	if (hfc_fifo_has_frames(&card->chans[D].rx))
1148
		hfc_frame_arrived(&card->chans[D]);
1149
1150
	if (card->chans[D].rx.ugly_framebuf_size) {
1151
		frame_left = card->chans[D].rx.ugly_framebuf_size -
1152
			card->chans[D].rx.ugly_framebuf_off ;
1153
		if (frame_left > hfc_HDLC_BUF_LEN) {
1154
			dahdi_hdlc_putbuf(hfccard->sigchan,
1155
					card->chans[D].rx.ugly_framebuf +
1156
					card->chans[D].rx.ugly_framebuf_off,
1157
					hfc_HDLC_BUF_LEN);
1158
			card->chans[D].rx.ugly_framebuf_off +=
1159
				hfc_HDLC_BUF_LEN;
1160
		} else {
1161
			dahdi_hdlc_putbuf(hfccard->sigchan,
1162
					card->chans[D].rx.ugly_framebuf +
1163
					card->chans[D].rx.ugly_framebuf_off,
1164
					frame_left);
1165
			dahdi_hdlc_finish(hfccard->sigchan);
1166
			card->chans[D].rx.ugly_framebuf_size = 0;
1167
			card->chans[D].rx.ugly_framebuf_off = 0;
1168
		}
1169
	}
1170
	/*
1171
	 * dahdi hdlc frame rx done
1172
	 */
1173
1174
	if (hfccard->span.flags & DAHDI_FLAG_RUNNING)
1175
		dahdi_receive(&hfccard->span);
1176
1177
}
1178
1179
static void hfc_frame_arrived(struct hfc_chan_duplex *chan)
1180
{
1181
	struct hfc_card *card = chan->card;
1182
	int antiloop = 16;
1183
	struct sk_buff *skb;
1184
1185
	while (hfc_fifo_has_frames(&chan->rx) && --antiloop) {
1186
		int frame_size = hfc_fifo_get_frame_size(&chan->rx);
1187
1188
		if (frame_size < 3) {
1189
#ifdef DEBUG
1190
			if (debug_level >= 2)
1191
				printk(KERN_DEBUG hfc_DRIVER_PREFIX
1192
					"card %d: "
1193
					"chan %s: "
1194
					"invalid frame received, "
1195
					"just %d bytes\n",
1196
					card->cardnum,
1197
					chan->name,
1198
					frame_size);
1199
#endif
1200
1201
			hfc_fifo_drop_frame(&chan->rx);
1202
1203
1204
			continue;
1205
		} else if (frame_size == 3) {
1206
#ifdef DEBUG
1207
			if (debug_level >= 2)
1208
				printk(KERN_DEBUG hfc_DRIVER_PREFIX
1209
					"card %d: "
1210
					"chan %s: "
1211
					"empty frame received\n",
1212
					card->cardnum,
1213
					chan->name);
1214
#endif
1215
1216
			hfc_fifo_drop_frame(&chan->rx);
1217
1218
1219
			continue;
1220
		}
1221
1222
		if (chan->open_by_zaptel &&
1223
			card->chans[D].rx.ugly_framebuf_size) {
1224
1225
				/*
1226
				 * We have to wait for Dahdi to transmit the
1227
				 * frame... wait for next time
1228
				 */
1229
1230
				 break;
1231
		}
1232
1233
		skb = dev_alloc_skb(frame_size - 3);
1234
1235
		if (!skb) {
1236
			printk(KERN_ERR hfc_DRIVER_PREFIX
1237
				"card %d: "
1238
				"chan %s: "
1239
				"cannot allocate skb: frame dropped\n",
1240
				card->cardnum,
1241
				chan->name);
1242
1243
			hfc_fifo_drop_frame(&chan->rx);
1244
1245
1246
			continue;
1247
		}
1248
1249
1250
		/*
1251
		* HFC does the checksum
1252
		*/
1253
#ifndef CHECKSUM_HW
1254
		skb->ip_summed = CHECKSUM_COMPLETE;
1255
#else
1256
		skb->ip_summed = CHECKSUM_HW;
1257
#endif
1258
1259
		if (chan->open_by_zaptel) {
1260
			card->chans[D].rx.ugly_framebuf_size = frame_size - 1;
1261
1262
			if (hfc_fifo_get_frame(&card->chans[D].rx,
1263
				card->chans[D].rx.ugly_framebuf,
1264
				frame_size - 1) == -1) {
1265
				dev_kfree_skb(skb);
1266
				continue;
1267
			}
1268
1269
			memcpy(skb_put(skb, frame_size - 3),
1270
				card->chans[D].rx.ugly_framebuf,
1271
				frame_size - 3);
1272
		} else {
1273
			if (hfc_fifo_get_frame(&chan->rx,
1274
				skb_put(skb, frame_size - 3),
1275
				frame_size - 3) == -1) {
1276
				dev_kfree_skb(skb);
1277
				continue;
1278
			}
1279
		}
1280
	}
1281
1282
	if (!antiloop)
1283
		printk(KERN_CRIT hfc_DRIVER_PREFIX
1284
			"card %d: "
1285
			"Infinite loop detected\n",
1286
			card->cardnum);
1287
}
1288
1289
/******************************************
1290
 * Module initialization and cleanup
1291
 ******************************************/
1292
1293
static int __devinit hfc_probe(struct pci_dev *pci_dev,
1294
	const struct pci_device_id *ent)
1295
{
1296
	static int cardnum;
1297
	int err;
1298
	int i;
1299
1300
	struct hfc_card *card = NULL;
1301
	struct dahdi_hfc *zthfc = NULL;
1302
	card = kmalloc(sizeof(struct hfc_card), GFP_KERNEL);
1303
	if (!card) {
1304
		printk(KERN_CRIT hfc_DRIVER_PREFIX
1305
			"unable to kmalloc!\n");
1306
		err = -ENOMEM;
1307
		goto err_alloc_hfccard;
1308
	}
1309
1310
	memset(card, 0x00, sizeof(struct hfc_card));
1311
	card->cardnum = cardnum;
1312
	card->pcidev = pci_dev;
1313
	spin_lock_init(&card->lock);
1314
1315
	pci_set_drvdata(pci_dev, card);
1316
1317
	err = pci_enable_device(pci_dev);
1318
	if (err)
1319
		goto err_pci_enable_device;
1320
1321
	err = pci_set_dma_mask(pci_dev, PCI_DMA_32BIT);
1322
	if (err) {
1323
		printk(KERN_ERR hfc_DRIVER_PREFIX
1324
			"card %d: "
1325
			"No suitable DMA configuration available.\n",
1326
			card->cardnum);
1327
		goto err_pci_set_dma_mask;
1328
	}
1329
1330
	pci_write_config_word(pci_dev, PCI_COMMAND, PCI_COMMAND_MEMORY);
1331
	err = pci_request_regions(pci_dev, hfc_DRIVER_NAME);
1332
	if (err) {
1333
		printk(KERN_CRIT hfc_DRIVER_PREFIX
1334
			"card %d: "
1335
			"cannot request I/O memory region\n",
1336
			card->cardnum);
1337
		goto err_pci_request_regions;
1338
	}
1339
1340
	pci_set_master(pci_dev);
1341
1342
	if (!pci_dev->irq) {
1343
		printk(KERN_CRIT hfc_DRIVER_PREFIX
1344
			"card %d: "
1345
			"no irq!\n",
1346
			card->cardnum);
1347
		err = -ENODEV;
1348
		goto err_noirq;
1349
	}
1350
1351
	card->io_bus_mem = pci_resource_start(pci_dev, 1);
1352
	if (!card->io_bus_mem) {
1353
		printk(KERN_CRIT hfc_DRIVER_PREFIX
1354
			"card %d: "
1355
			"no iomem!\n",
1356
			card->cardnum);
1357
		err = -ENODEV;
1358
		goto err_noiobase;
1359
	}
1360
1361
	card->io_mem = ioremap(card->io_bus_mem, hfc_PCI_MEM_SIZE);
1362
	if (!(card->io_mem)) {
1363
		printk(KERN_CRIT hfc_DRIVER_PREFIX
1364
			"card %d: "
1365
			"cannot ioremap I/O memory\n",
1366
			card->cardnum);
1367
		err = -ENODEV;
1368
		goto err_ioremap;
1369
	}
1370
1371
	/*
1372
	 * pci_alloc_consistent guarantees alignment
1373
	 * (Documentation/DMA-mapping.txt)
1374
	 */
1375
	card->fifo_mem = pci_alloc_consistent(pci_dev,
1376
			hfc_FIFO_SIZE, &card->fifo_bus_mem);
1377
	if (!card->fifo_mem) {
1378
		printk(KERN_CRIT hfc_DRIVER_PREFIX
1379
			"card %d: "
1380
			"unable to allocate FIFO DMA memory!\n",
1381
			card->cardnum);
1382
		err = -ENOMEM;
1383
		goto err_alloc_fifo;
1384
	}
1385
1386
	memset(card->fifo_mem, 0x00, hfc_FIFO_SIZE);
1387
1388
	card->fifos = card->fifo_mem;
1389
1390
	pci_write_config_dword(card->pcidev, hfc_PCI_MWBA, card->fifo_bus_mem);
1391
1392
	err = request_irq(card->pcidev->irq, &hfc_interrupt,
1393
1394
#if (KERNEL_VERSION(2, 6, 23) < LINUX_VERSION_CODE)
1395
		IRQF_SHARED, hfc_DRIVER_NAME, card);
1396
#else
1397
		SA_SHIRQ, hfc_DRIVER_NAME, card);
1398
#endif
1399
1400
	if (err) {
1401
		printk(KERN_CRIT hfc_DRIVER_PREFIX
1402
			"card %d: "
1403
			"unable to register irq\n",
1404
			card->cardnum);
1405
		goto err_request_irq;
1406
	}
1407
1408
	card->nt_mode = FALSE;
1409
1410
	if (modes & (1 << card->cardnum))
1411
		card->nt_mode = TRUE;
1412
1413
	for (i = 0; i < nt_modes_count; i++) {
1414
		if (nt_modes[i] == card->cardnum)
1415
			card->nt_mode = TRUE;
1416
	}
1417
1418
	/*
1419
	 * D Channel
1420
	 */
1421
	card->chans[D].card = card;
1422
	card->chans[D].name = "D";
1423
	card->chans[D].status = free;
1424
	card->chans[D].number = D;
1425
	spin_lock_init(&card->chans[D].lock);
1426
1427
	card->chans[D].rx.chan      = &card->chans[D];
1428
	card->chans[D].rx.fifo_base = card->fifos + 0x4000;
1429
	card->chans[D].rx.z_base    = card->fifos + 0x4000;
1430
	card->chans[D].rx.z1_base   = card->fifos + 0x6080;
1431
	card->chans[D].rx.z2_base   = card->fifos + 0x6082;
1432
	card->chans[D].rx.z_min     = 0x0000;
1433
	card->chans[D].rx.z_max     = 0x01FF;
1434
	card->chans[D].rx.f_min     = 0x10;
1435
	card->chans[D].rx.f_max     = 0x1F;
1436
	card->chans[D].rx.f1        = card->fifos + 0x60a0;
1437
	card->chans[D].rx.f2        = card->fifos + 0x60a1;
1438
	card->chans[D].rx.fifo_size = card->chans[D].rx.z_max
1439
		- card->chans[D].rx.z_min + 1;
1440
	card->chans[D].rx.f_num     = card->chans[D].rx.f_max
1441
		- card->chans[D].rx.f_min + 1;
1442
1443
	card->chans[D].tx.chan      = &card->chans[D];
1444
	card->chans[D].tx.fifo_base = card->fifos + 0x0000;
1445
	card->chans[D].tx.z_base    = card->fifos + 0x0000;
1446
	card->chans[D].tx.z1_base   = card->fifos + 0x2080;
1447
	card->chans[D].tx.z2_base   = card->fifos + 0x2082;
1448
	card->chans[D].tx.z_min     = 0x0000;
1449
	card->chans[D].tx.z_max     = 0x01FF;
1450
	card->chans[D].tx.f_min     = 0x10;
1451
	card->chans[D].tx.f_max     = 0x1F;
1452
	card->chans[D].tx.f1        = card->fifos + 0x20a0;
1453
	card->chans[D].tx.f2        = card->fifos + 0x20a1;
1454
	card->chans[D].tx.fifo_size = card->chans[D].tx.z_max -
1455
		card->chans[D].tx.z_min + 1;
1456
	card->chans[D].tx.f_num     = card->chans[D].tx.f_max -
1457
		card->chans[D].tx.f_min + 1;
1458
1459
	/*
1460
	 * B1 Channel
1461
	 */
1462
	card->chans[B1].card = card;
1463
	card->chans[B1].name = "B1";
1464
	card->chans[B1].status = free;
1465
	card->chans[B1].number = B1;
1466
	card->chans[B1].protocol = 0;
1467
	spin_lock_init(&card->chans[B1].lock);
1468
1469
	card->chans[B1].rx.chan      = &card->chans[B1];
1470
	card->chans[B1].rx.fifo_base = card->fifos + 0x4200;
1471
	card->chans[B1].rx.z_base    = card->fifos + 0x4000;
1472
	card->chans[B1].rx.z1_base   = card->fifos + 0x6000;
1473
	card->chans[B1].rx.z2_base   = card->fifos + 0x6002;
1474
	card->chans[B1].rx.z_min     = 0x0200;
1475
	card->chans[B1].rx.z_max     = 0x1FFF;
1476
	card->chans[B1].rx.f_min     = 0x00;
1477
	card->chans[B1].rx.f_max     = 0x1F;
1478
	card->chans[B1].rx.f1        = card->fifos + 0x6080;
1479
	card->chans[B1].rx.f2        = card->fifos + 0x6081;
1480
	card->chans[B1].rx.fifo_size = card->chans[B1].rx.z_max -
1481
		card->chans[B1].rx.z_min + 1;
1482
	card->chans[B1].rx.f_num     = card->chans[B1].rx.f_max -
1483
		card->chans[B1].rx.f_min + 1;
1484
1485
	card->chans[B1].tx.chan      = &card->chans[B1];
1486
	card->chans[B1].tx.fifo_base = card->fifos + 0x0200;
1487
	card->chans[B1].tx.z_base    = card->fifos + 0x0000;
1488
	card->chans[B1].tx.z1_base   = card->fifos + 0x2000;
1489
	card->chans[B1].tx.z2_base   = card->fifos + 0x2002;
1490
	card->chans[B1].tx.z_min     = 0x0200;
1491
	card->chans[B1].tx.z_max     = 0x1FFF;
1492
	card->chans[B1].tx.f_min     = 0x00;
1493
	card->chans[B1].tx.f_max     = 0x1F;
1494
	card->chans[B1].tx.f1        = card->fifos + 0x2080;
1495
	card->chans[B1].tx.f2        = card->fifos + 0x2081;
1496
	card->chans[B1].tx.fifo_size = card->chans[B1].tx.z_max -
1497
		card->chans[B1].tx.z_min + 1;
1498
	card->chans[B1].tx.f_num     = card->chans[B1].tx.f_max -
1499
		card->chans[B1].tx.f_min + 1;
1500
1501
	/*
1502
	 * B2 Channel
1503
	 */
1504
	card->chans[B2].card = card;
1505
	card->chans[B2].name = "B2";
1506
	card->chans[B2].status = free;
1507
	card->chans[B2].number = B2;
1508
	card->chans[B2].protocol = 0;
1509
	spin_lock_init(&card->chans[B2].lock);
1510
1511
	card->chans[B2].rx.chan      = &card->chans[B2];
1512
	card->chans[B2].rx.fifo_base = card->fifos + 0x6200,
1513
	card->chans[B2].rx.z_base    = card->fifos + 0x6000;
1514
	card->chans[B2].rx.z1_base   = card->fifos + 0x6100;
1515
	card->chans[B2].rx.z2_base   = card->fifos + 0x6102;
1516
	card->chans[B2].rx.z_min     = 0x0200;
1517
	card->chans[B2].rx.z_max     = 0x1FFF;
1518
	card->chans[B2].rx.f_min     = 0x00;
1519
	card->chans[B2].rx.f_max     = 0x1F;
1520
	card->chans[B2].rx.f1        = card->fifos + 0x6180;
1521
	card->chans[B2].rx.f2        = card->fifos + 0x6181;
1522
	card->chans[B2].rx.fifo_size = card->chans[B2].rx.z_max -
1523
		card->chans[B2].rx.z_min + 1;
1524
	card->chans[B2].rx.f_num     = card->chans[B2].rx.f_max -
1525
		card->chans[B2].rx.f_min + 1;
1526
1527
	card->chans[B2].tx.chan      = &card->chans[B2];
1528
	card->chans[B2].tx.fifo_base = card->fifos + 0x2200;
1529
	card->chans[B2].tx.z_base    = card->fifos + 0x2000;
1530
	card->chans[B2].tx.z1_base   = card->fifos + 0x2100;
1531
	card->chans[B2].tx.z2_base   = card->fifos + 0x2102;
1532
	card->chans[B2].tx.z_min     = 0x0200;
1533
	card->chans[B2].tx.z_max     = 0x1FFF;
1534
	card->chans[B2].tx.f_min     = 0x00;
1535
	card->chans[B2].tx.f_max     = 0x1F;
1536
	card->chans[B2].tx.f1        = card->fifos + 0x2180;
1537
	card->chans[B2].tx.f2        = card->fifos + 0x2181;
1538
	card->chans[B2].tx.fifo_size = card->chans[B2].tx.z_max -
1539
		card->chans[B2].tx.z_min + 1;
1540
	card->chans[B2].tx.f_num     = card->chans[B2].tx.f_max -
1541
		card->chans[B2].tx.f_min + 1;
1542
1543
	/*
1544
	 * All done
1545
	 */
1546
1547
	zthfc = kmalloc(sizeof(struct dahdi_hfc), GFP_KERNEL);
1548
	if (!zthfc) {
1549
		printk(KERN_CRIT hfc_DRIVER_PREFIX
1550
			"unable to kmalloc!\n");
1551
		goto err_request_irq;
1552
	}
1553
	memset(zthfc, 0x0, sizeof(struct dahdi_hfc));
1554
1555
	zthfc->card = card;
1556
	hfc_zap_initialize(zthfc);
1557
	card->ztdev = zthfc;
1558
1559
	snprintf(card->proc_dir_name,
1560
			sizeof(card->proc_dir_name),
1561
			"%d", card->cardnum);
1562
	card->proc_dir = proc_mkdir(card->proc_dir_name, hfc_proc_zaphfc_dir);
1563
	SET_PROC_DIRENTRY_OWNER(card->proc_dir);
1564
1565
	hfc_resetCard(card);
1566
1567
	printk(KERN_INFO hfc_DRIVER_PREFIX
1568
		"card %d configured for %s mode at mem %#lx (0x%p) IRQ %u\n",
1569
		card->cardnum,
1570
		card->nt_mode ? "NT" : "TE",
1571
		card->io_bus_mem,
1572
		card->io_mem,
1573
		card->pcidev->irq);
1574
1575
	cardnum++;
1576
1577
	return 0;
1578
1579
err_request_irq:
1580
	pci_free_consistent(pci_dev, hfc_FIFO_SIZE,
1581
		card->fifo_mem, card->fifo_bus_mem);
1582
err_alloc_fifo:
1583
	iounmap(card->io_mem);
1584
err_ioremap:
1585
err_noiobase:
1586
err_noirq:
1587
	pci_release_regions(pci_dev);
1588
err_pci_request_regions:
1589
err_pci_set_dma_mask:
1590
err_pci_enable_device:
1591
	kfree(card);
1592
err_alloc_hfccard:
1593
	return err;
1594
}
1595
1596
static void __devexit hfc_remove(struct pci_dev *pci_dev)
1597
{
1598
	struct hfc_card *card = pci_get_drvdata(pci_dev);
1599
1600
1601
	printk(KERN_INFO hfc_DRIVER_PREFIX
1602
		"card %d: "
1603
		"shutting down card at %p.\n",
1604
		card->cardnum,
1605
		card->io_mem);
1606
1607
	hfc_softreset(card);
1608
1609
	dahdi_unregister(&card->ztdev->span);
1610
1611
1612
	/*
1613
	 * disable memio and bustmaster
1614
	 */
1615
	pci_write_config_word(pci_dev, PCI_COMMAND, 0);
1616
1617
	remove_proc_entry("bufs", card->proc_dir);
1618
	remove_proc_entry("fifos", card->proc_dir);
1619
	remove_proc_entry("info", card->proc_dir);
1620
	remove_proc_entry(card->proc_dir_name, hfc_proc_zaphfc_dir);
1621
1622
	free_irq(pci_dev->irq, card);
1623
1624
	pci_free_consistent(pci_dev, hfc_FIFO_SIZE,
1625
		card->fifo_mem, card->fifo_bus_mem);
1626
1627
	iounmap(card->io_mem);
1628
1629
	pci_release_regions(pci_dev);
1630
1631
	pci_disable_device(pci_dev);
1632
1633
	kfree(card);
1634
}
1635
1636
/******************************************
1637
 * Module stuff
1638
 ******************************************/
1639
1640
static int __init hfc_init_module(void)
1641
{
1642
	int ret;
1643
1644
	printk(KERN_INFO hfc_DRIVER_PREFIX
1645
		hfc_DRIVER_STRING " loading\n");
1646
1647
#if (KERNEL_VERSION(2, 6, 26) <= LINUX_VERSION_CODE)
1648
	hfc_proc_zaphfc_dir = proc_mkdir(hfc_DRIVER_NAME, NULL);
1649
#else
1650
	hfc_proc_zaphfc_dir = proc_mkdir(hfc_DRIVER_NAME, proc_root_driver);
1651
#endif
1652
1653
	ret = dahdi_pci_module(&hfc_driver);
1654
	return ret;
1655
}
1656
1657
module_init(hfc_init_module);
1658
1659
static void __exit hfc_module_exit(void)
1660
{
1661
	pci_unregister_driver(&hfc_driver);
1662
1663
#if (KERNEL_VERSION(2, 6, 26) <= LINUX_VERSION_CODE)
1664
	remove_proc_entry(hfc_DRIVER_NAME, NULL);
1665
#else
1666
	remove_proc_entry(hfc_DRIVER_NAME, proc_root_driver);
1667
#endif
1668
1669
	printk(KERN_INFO hfc_DRIVER_PREFIX
1670
		hfc_DRIVER_STRING " unloaded\n");
1671
}
1672
1673
module_exit(hfc_module_exit);
1674
1675
#endif
1676
1677
MODULE_DESCRIPTION(hfc_DRIVER_DESCR);
1678
MODULE_AUTHOR("Jens Wilke <jw_vzaphfc@headissue.com>, "
1679
		"Daniele (Vihai) Orlandi <daniele@orlandi.com>, "
1680
		"Jose A. Deniz <odicha@hotmail.com>");
1681
MODULE_ALIAS("vzaphfc");
1682
#ifdef MODULE_LICENSE
1683
MODULE_LICENSE("GPL");
1684
#endif
1685
1686
1687
module_param(modes, int, 0444);
1688
1689
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 10)
1690
module_param_array(nt_modes, int, &nt_modes_count, 0444);
1691
#else
1692
module_param_array(nt_modes, int, nt_modes_count, 0444);
1693
#endif
1694
1695
module_param(force_l1_up, int, 0444);
1696
#ifdef DEBUG
1697
module_param(debug_level, int, 0444);
1698
#endif
1699
1700
MODULE_PARM_DESC(modes, "[Deprecated] bit-mask to configure NT mode");
1701
MODULE_PARM_DESC(nt_modes,
1702
		"Comma-separated list of card IDs to configure in NT mode");
1703
MODULE_PARM_DESC(force_l1_up, "Don't allow L1 to go down");
1704
#ifdef DEBUG
1705
MODULE_PARM_DESC(debug_level, "Debug verbosity level");
1706
#endif
(-)dahdi-linux-2.2.0.2/drivers/dahdi/zaphfc/fifo.c (+375 lines)
Line 0 Link Here
1
/*
2
 * fifo.c - HFC FIFO management routines
3
 *
4
 * Copyright (C) 2006 headissue GmbH; Jens Wilke
5
 * Copyright (C) 2004 Daniele Orlandi
6
 * Copyright (C) 2002, 2003, 2004, Junghanns.NET GmbH
7
 *
8
 * Original author of this code is
9
 * Daniele "Vihai" Orlandi <daniele@orlandi.com>
10
 *
11
 * This program is free software and may be modified and
12
 * distributed under the terms of the GNU Public License.
13
 *
14
 */
15
16
#include <linux/kernel.h>
17
18
#include <dahdi/kernel.h>
19
20
#include "fifo.h"
21
22
static void hfc_fifo_mem_read(struct hfc_chan_simplex *chan,
23
	int z_start,
24
	void *data, int size)
25
{
26
	int bytes_to_boundary = chan->z_max - z_start + 1;
27
	if (bytes_to_boundary >= size) {
28
		memcpy(data,
29
			chan->z_base + z_start,
30
			size);
31
	} else {
32
		/*
33
		 * Buffer wrap
34
		 */
35
		memcpy(data,
36
			chan->z_base + z_start,
37
			bytes_to_boundary);
38
39
		memcpy(data + bytes_to_boundary,
40
			chan->fifo_base,
41
			size - bytes_to_boundary);
42
	}
43
}
44
45
static void hfc_fifo_mem_write(struct hfc_chan_simplex *chan,
46
	void *data, int size)
47
{
48
	int bytes_to_boundary = chan->z_max - *Z1_F1(chan) + 1;
49
	if (bytes_to_boundary >= size) {
50
		memcpy(chan->z_base + *Z1_F1(chan),
51
			data,
52
			size);
53
	} else {
54
		/*
55
		 * FIFO wrap
56
		 */
57
58
		memcpy(chan->z_base + *Z1_F1(chan),
59
			data,
60
			bytes_to_boundary);
61
62
		memcpy(chan->fifo_base,
63
			data + bytes_to_boundary,
64
			size - bytes_to_boundary);
65
	}
66
}
67
68
int hfc_fifo_get(struct hfc_chan_simplex *chan,
69
		void *data, int size)
70
{
71
	int available_bytes;
72
73
	/*
74
	 * Some useless statistic
75
	 */
76
	chan->bytes += size;
77
78
	available_bytes = hfc_fifo_used_rx(chan);
79
80
	if (available_bytes < size && !chan->fifo_underrun++) {
81
		/*
82
		 * print the warning only once
83
		 */
84
		printk(KERN_WARNING hfc_DRIVER_PREFIX
85
			"card %d: "
86
			"chan %s: "
87
			"RX FIFO not enough (%d) bytes to receive!\n",
88
			chan->chan->card->cardnum,
89
			chan->chan->name,
90
			available_bytes);
91
		return -1;
92
	}
93
94
	hfc_fifo_mem_read(chan, *Z2_F2(chan), data, size);
95
	*Z2_F2(chan) = Z_inc(chan, *Z2_F2(chan), size);
96
	return available_bytes - size;
97
}
98
99
void hfc_fifo_put(struct hfc_chan_simplex *chan,
100
			void *data, int size)
101
{
102
	struct hfc_card *card = chan->chan->card;
103
	int used_bytes = hfc_fifo_used_tx(chan);
104
	int free_bytes = hfc_fifo_free_tx(chan);
105
106
	if (!used_bytes && !chan->fifo_underrun++) {
107
		/*
108
		 * print warning only once, to make timing not worse
109
		 */
110
		printk(KERN_WARNING hfc_DRIVER_PREFIX
111
			"card %d: "
112
			"chan %s: "
113
			"TX FIFO has become empty\n",
114
			card->cardnum,
115
			chan->chan->name);
116
	}
117
	if (free_bytes < size) {
118
		printk(KERN_CRIT hfc_DRIVER_PREFIX
119
			"card %d: "
120
			"chan %s: "
121
			"TX FIFO full!\n",
122
			chan->chan->card->cardnum,
123
			chan->chan->name);
124
		chan->fifo_full++;
125
		hfc_clear_fifo_tx(chan);
126
	}
127
128
	hfc_fifo_mem_write(chan, data, size);
129
	chan->bytes += size;
130
	*Z1_F1(chan) = Z_inc(chan, *Z1_F1(chan), size);
131
}
132
133
int hfc_fifo_get_frame(struct hfc_chan_simplex *chan, void *data, int max_size)
134
{
135
	int frame_size;
136
	u16 newz2 ;
137
138
	if (*chan->f1 == *chan->f2) {
139
		/*
140
		 * nothing received, strange uh?
141
		 */
142
		printk(KERN_WARNING hfc_DRIVER_PREFIX
143
			"card %d: "
144
			"chan %s: "
145
			"get_frame called with no frame in FIFO.\n",
146
			chan->chan->card->cardnum,
147
			chan->chan->name);
148
149
		return -1;
150
	}
151
152
	/*
153
	 * frame_size includes CRC+CRC+STAT
154
	 */
155
	frame_size = hfc_fifo_get_frame_size(chan);
156
157
#ifdef DEBUG
158
	if (debug_level == 3) {
159
		printk(KERN_DEBUG hfc_DRIVER_PREFIX
160
			"card %d: "
161
			"chan %s: "
162
			"RX len %2d: ",
163
			chan->chan->card->cardnum,
164
			chan->chan->name,
165
			frame_size);
166
	} else if (debug_level >= 4) {
167
		printk(KERN_DEBUG hfc_DRIVER_PREFIX
168
			"card %d: "
169
			"chan %s: "
170
			"RX (f1=%02x, f2=%02x, z1=%04x, z2=%04x) len %2d: ",
171
			chan->chan->card->cardnum,
172
			chan->chan->name,
173
			*chan->f1, *chan->f2, *Z1_F2(chan), *Z2_F2(chan),
174
			frame_size);
175
	}
176
177
	if (debug_level >= 3) {
178
		int i;
179
		for (i = 0; i < frame_size; i++) {
180
			printk("%02x", hfc_fifo_u8(chan,
181
				Z_inc(chan, *Z2_F2(chan), i)));
182
		}
183
184
		printk("\n");
185
	}
186
#endif
187
188
	if (frame_size <= 0) {
189
#ifdef DEBUG
190
		if (debug_level >= 2) {
191
			printk(KERN_DEBUG hfc_DRIVER_PREFIX
192
				"card %d: "
193
				"chan %s: "
194
				"invalid (empty) frame received.\n",
195
				chan->chan->card->cardnum,
196
				chan->chan->name);
197
		}
198
#endif
199
200
		hfc_fifo_drop_frame(chan);
201
		return -1;
202
	}
203
204
	/*
205
	 * STAT is not really received
206
	 */
207
	chan->bytes += frame_size - 1;
208
209
	/*
210
	 * Calculate beginning of the next frame
211
	 */
212
	newz2 = Z_inc(chan, *Z2_F2(chan), frame_size);
213
214
	/*
215
	 * We cannot use hfc_fifo_get because of different semantic of
216
	 * "available bytes" and to avoid useless increment of Z2
217
	 */
218
	hfc_fifo_mem_read(chan, *Z2_F2(chan), data,
219
		frame_size < max_size ? frame_size : max_size);
220
221
	if (hfc_fifo_u8(chan, Z_inc(chan, *Z2_F2(chan),
222
		frame_size - 1)) != 0x00) {
223
		/*
224
		 * CRC not ok, frame broken, skipping
225
		 */
226
#ifdef DEBUG
227
		if (debug_level >= 2) {
228
			printk(KERN_WARNING hfc_DRIVER_PREFIX
229
				"card %d: "
230
				"chan %s: "
231
				"Received frame with wrong CRC\n",
232
				chan->chan->card->cardnum,
233
				chan->chan->name);
234
		}
235
#endif
236
237
		chan->crc++;
238
239
		hfc_fifo_drop_frame(chan);
240
		return -1;
241
	}
242
243
	chan->frames++;
244
245
	*chan->f2 = F_inc(chan, *chan->f2, 1);
246
247
	/*
248
	 * Set Z2 for the next frame we're going to receive
249
	 */
250
	*Z2_F2(chan) = newz2;
251
252
	return frame_size;
253
}
254
255
void hfc_fifo_drop_frame(struct hfc_chan_simplex *chan)
256
{
257
	int available_bytes;
258
	u16 newz2;
259
260
	if (*chan->f1 == *chan->f2) {
261
		/*
262
		 * nothing received, strange eh?
263
		 */
264
		printk(KERN_WARNING hfc_DRIVER_PREFIX
265
			"card %d: "
266
			"chan %s: "
267
			"skip_frame called with no frame in FIFO.\n",
268
			chan->chan->card->cardnum,
269
			chan->chan->name);
270
271
		return;
272
	}
273
274
	available_bytes = hfc_fifo_used_rx(chan) + 1;
275
276
	/*
277
	 * Calculate beginning of the next frame
278
	 */
279
	newz2 = Z_inc(chan, *Z2_F2(chan), available_bytes);
280
281
	*chan->f2 = F_inc(chan, *chan->f2, 1);
282
283
	/*
284
	 * Set Z2 for the next frame we're going to receive
285
	 */
286
	*Z2_F2(chan) = newz2;
287
}
288
289
void hfc_fifo_put_frame(struct hfc_chan_simplex *chan,
290
		 void *data, int size)
291
{
292
	u16 newz1;
293
	int available_frames;
294
295
#ifdef DEBUG
296
	if (debug_level == 3) {
297
		printk(KERN_DEBUG hfc_DRIVER_PREFIX
298
			"card %d: "
299
			"chan %s: "
300
			"TX len %2d: ",
301
			chan->chan->card->cardnum,
302
			chan->chan->name,
303
			size);
304
	} else if (debug_level >= 4) {
305
		printk(KERN_DEBUG hfc_DRIVER_PREFIX
306
			"card %d: "
307
			"chan %s: "
308
			"TX (f1=%02x, f2=%02x, z1=%04x, z2=%04x) len %2d: ",
309
			chan->chan->card->cardnum,
310
			chan->chan->name,
311
			*chan->f1, *chan->f2, *Z1_F1(chan), *Z2_F1(chan),
312
			size);
313
	}
314
315
	if (debug_level >= 3) {
316
		int i;
317
		for (i = 0; i < size; i++)
318
			printk("%02x", ((u8 *)data)[i]);
319
320
		printk("\n");
321
	}
322
#endif
323
324
	available_frames = hfc_fifo_free_frames(chan);
325
326
	if (available_frames >= chan->f_num) {
327
		printk(KERN_CRIT hfc_DRIVER_PREFIX
328
			"card %d: "
329
			"chan %s: "
330
			"TX FIFO total number of frames exceeded!\n",
331
			chan->chan->card->cardnum,
332
			chan->chan->name);
333
334
		chan->fifo_full++;
335
336
		return;
337
	}
338
339
	hfc_fifo_put(chan, data, size);
340
341
	newz1 = *Z1_F1(chan);
342
343
	*chan->f1 = F_inc(chan, *chan->f1, 1);
344
345
	*Z1_F1(chan) = newz1;
346
347
	chan->frames++;
348
}
349
350
void hfc_clear_fifo_rx(struct hfc_chan_simplex *chan)
351
{
352
	*chan->f2 = *chan->f1;
353
	*Z2_F2(chan) = *Z1_F2(chan);
354
}
355
356
void hfc_clear_fifo_tx(struct hfc_chan_simplex *chan)
357
{
358
	*chan->f1 = *chan->f2;
359
	*Z1_F1(chan) = *Z2_F1(chan);
360
361
	if (chan->chan->status == open_voice) {
362
		/*
363
		 * Make sure that at least hfc_TX_FIFO_PRELOAD bytes are
364
		 * present in the TX FIFOs
365
		 * Create hfc_TX_FIFO_PRELOAD bytes of empty data
366
		 * (0x7f is mute audio)
367
		 */
368
		u8 empty_fifo[hfc_TX_FIFO_PRELOAD +
369
			DAHDI_CHUNKSIZE + hfc_RX_FIFO_PRELOAD];
370
		memset(empty_fifo, 0x7f, sizeof(empty_fifo));
371
372
		hfc_fifo_put(chan, empty_fifo, sizeof(empty_fifo));
373
	}
374
}
375
(-)dahdi-linux-2.2.0.2/drivers/dahdi/zaphfc/fifo.h (+139 lines)
Line 0 Link Here
1
/*
2
 * fifo.h - Dahdi driver for HFC-S PCI A based ISDN BRI cards
3
 *
4
 * Copyright (C) 2004 Daniele Orlandi
5
 * Copyright (C) 2002, 2003, 2004, Junghanns.NET GmbH
6
 *
7
 * Daniele "Vihai" Orlandi <daniele@orlandi.com>
8
 *
9
 * Major rewrite of the driver made by
10
 * Klaus-Peter Junghanns <kpj@junghanns.net>
11
 *
12
 * This program is free software and may be modified and
13
 * distributed under the terms of the GNU Public License.
14
 *
15
 */
16
17
#ifndef _HFC_FIFO_H
18
#define _HFC_FIFO_H
19
20
#include "zaphfc.h"
21
22
static inline u16 *Z1_F1(struct hfc_chan_simplex *chan)
23
{
24
	return chan->z1_base + (*chan->f1 * 4);
25
}
26
27
static inline u16 *Z2_F1(struct hfc_chan_simplex *chan)
28
{
29
	return chan->z2_base + (*chan->f1 * 4);
30
}
31
32
static inline u16 *Z1_F2(struct hfc_chan_simplex *chan)
33
{
34
	return chan->z1_base + (*chan->f2 * 4);
35
}
36
37
static inline u16 *Z2_F2(struct hfc_chan_simplex *chan)
38
{
39
	return chan->z2_base + (*chan->f2 * 4);
40
}
41
42
static inline u16 Z_inc(struct hfc_chan_simplex *chan, u16 z, u16 inc)
43
{
44
	/*
45
	 * declared as u32 in order to manage overflows
46
	 */
47
	u32 newz = z + inc;
48
	if (newz > chan->z_max)
49
		newz -= chan->fifo_size;
50
51
	return newz;
52
}
53
54
static inline u8 F_inc(struct hfc_chan_simplex *chan, u8 f, u8 inc)
55
{
56
	/*
57
	 * declared as u16 in order to manage overflows
58
	 */
59
	u16 newf = f + inc;
60
	if (newf > chan->f_max)
61
		newf -= chan->f_num;
62
63
	return newf;
64
}
65
66
static inline u16 hfc_fifo_used_rx(struct hfc_chan_simplex *chan)
67
{
68
	return (*Z1_F2(chan) - *Z2_F2(chan) +
69
			chan->fifo_size) % chan->fifo_size;
70
}
71
72
static inline u16 hfc_fifo_get_frame_size(struct hfc_chan_simplex *chan)
73
{
74
 /*
75
  * This +1 is needed because in frame mode the available bytes are Z2-Z1+1
76
  * while in transparent mode I wouldn't consider the byte pointed by Z2 to
77
  * be available, otherwise, the FIFO would always contain one byte, even
78
  * when Z1==Z2
79
  */
80
81
	return hfc_fifo_used_rx(chan) + 1;
82
}
83
84
static inline u8 hfc_fifo_u8(struct hfc_chan_simplex *chan, u16 z)
85
{
86
	return *((u8 *)(chan->z_base + z));
87
}
88
89
static inline u16 hfc_fifo_used_tx(struct hfc_chan_simplex *chan)
90
{
91
	return (*Z1_F1(chan) - *Z2_F1(chan) +
92
			chan->fifo_size) % chan->fifo_size;
93
}
94
95
static inline u16 hfc_fifo_free_rx(struct hfc_chan_simplex *chan)
96
{
97
	u16 free_bytes = *Z2_F1(chan) - *Z1_F1(chan);
98
99
	if (free_bytes > 0)
100
		return free_bytes;
101
	else
102
		return free_bytes + chan->fifo_size;
103
}
104
105
static inline u16 hfc_fifo_free_tx(struct hfc_chan_simplex *chan)
106
{
107
	u16 free_bytes = *Z2_F1(chan) - *Z1_F1(chan);
108
109
	if (free_bytes > 0)
110
		return free_bytes;
111
	else
112
		return free_bytes + chan->fifo_size;
113
}
114
115
static inline int hfc_fifo_has_frames(struct hfc_chan_simplex *chan)
116
{
117
	return *chan->f1 != *chan->f2;
118
}
119
120
static inline u8 hfc_fifo_used_frames(struct hfc_chan_simplex *chan)
121
{
122
	return (*chan->f1 - *chan->f2 + chan->f_num) % chan->f_num;
123
}
124
125
static inline u8 hfc_fifo_free_frames(struct hfc_chan_simplex *chan)
126
{
127
	return (*chan->f2 - *chan->f1 + chan->f_num) % chan->f_num;
128
}
129
130
int hfc_fifo_get(struct hfc_chan_simplex *chan, void *data, int size);
131
void hfc_fifo_put(struct hfc_chan_simplex *chan, void *data, int size);
132
void hfc_fifo_drop(struct hfc_chan_simplex *chan, int size);
133
int hfc_fifo_get_frame(struct hfc_chan_simplex *chan, void *data, int max_size);
134
void hfc_fifo_drop_frame(struct hfc_chan_simplex *chan);
135
void hfc_fifo_put_frame(struct hfc_chan_simplex *chan, void *data, int size);
136
void hfc_clear_fifo_rx(struct hfc_chan_simplex *chan);
137
void hfc_clear_fifo_tx(struct hfc_chan_simplex *chan);
138
139
#endif
(-)dahdi-linux-2.2.0.2/drivers/dahdi/zaphfc/Kbuild (+10 lines)
Line 0 Link Here
1
obj-m += zaphfc.o
2
3
EXTRA_CFLAGS := -I$(src)/.. -Wno-undef
4
5
zaphfc-objs := base.o fifo.o
6
7
$(obj)/base.o: $(src)/zaphfc.h
8
$(obj)/fifo.o: $(src)/fifo.h
9
10
(-)dahdi-linux-2.2.0.2/drivers/dahdi/zaphfc/zaphfc.h (+414 lines)
Line 0 Link Here
1
/*
2
 * zaphfc.h - Dahdi driver for HFC-S PCI A based ISDN BRI cards
3
 *
4
 * Dahdi port by Jose A. Deniz <odicha@hotmail.com>
5
 *
6
 * Copyright (C) 2009 Jose A. Deniz
7
 * Copyright (C) 2006 headissue GmbH; Jens Wilke
8
 * Copyright (C) 2004 Daniele Orlandi
9
 * Copyright (C) 2002, 2003, 2004, Junghanns.NET GmbH
10
 *
11
 * Jens Wilke <jw_vzaphfc@headissue.com>
12
 *
13
 * Orginal author of this code is
14
 * Daniele "Vihai" Orlandi <daniele@orlandi.com>
15
 *
16
 * Major rewrite of the driver made by
17
 * Klaus-Peter Junghanns <kpj@junghanns.net>
18
 *
19
 * This program is free software and may be modified and
20
 * distributed under the terms of the GNU Public License.
21
 *
22
 */
23
24
#ifndef _HFC_ZAPHFC_H
25
#define _HFC_ZAPHFC_H
26
27
#include <asm/io.h>
28
29
#define hfc_DRIVER_NAME "vzaphfc"
30
#define hfc_DRIVER_PREFIX hfc_DRIVER_NAME ": "
31
#define hfc_DRIVER_DESCR "HFC-S PCI A ISDN"
32
#define hfc_DRIVER_VERSION "1.42"
33
#define hfc_DRIVER_STRING hfc_DRIVER_DESCR " (V" hfc_DRIVER_VERSION ")"
34
35
#define hfc_MAX_BOARDS 32
36
37
#ifndef PCI_DMA_32BIT
38
#define PCI_DMA_32BIT	0x00000000ffffffffULL
39
#endif
40
41
#ifndef PCI_VENDOR_ID_SITECOM
42
#define PCI_VENDOR_ID_SITECOM 0x182D
43
#endif
44
45
#ifndef PCI_DEVICE_ID_SITECOM_3069
46
#define PCI_DEVICE_ID_SITECOM_3069 0x3069
47
#endif
48
49
#define hfc_RESET_DELAY 20
50
51
#define hfc_CLKDEL_TE	0x0f	/* CLKDEL in TE mode */
52
#define hfc_CLKDEL_NT	0x6c	/* CLKDEL in NT mode */
53
54
/* PCI memory mapped I/O */
55
56
#define hfc_PCI_MEM_SIZE	0x0100
57
#define hfc_PCI_MWBA		0x80
58
59
/* GCI/IOM bus monitor registers */
60
61
#define hfc_C_I       0x08
62
#define hfc_TRxR      0x0C
63
#define hfc_MON1_D    0x28
64
#define hfc_MON2_D    0x2C
65
66
67
/* GCI/IOM bus timeslot registers */
68
69
#define hfc_B1_SSL    0x80
70
#define hfc_B2_SSL    0x84
71
#define hfc_AUX1_SSL  0x88
72
#define hfc_AUX2_SSL  0x8C
73
#define hfc_B1_RSL    0x90
74
#define hfc_B2_RSL    0x94
75
#define hfc_AUX1_RSL  0x98
76
#define hfc_AUX2_RSL  0x9C
77
78
/* GCI/IOM bus data registers */
79
80
#define hfc_B1_D      0xA0
81
#define hfc_B2_D      0xA4
82
#define hfc_AUX1_D    0xA8
83
#define hfc_AUX2_D    0xAC
84
85
/* GCI/IOM bus configuration registers */
86
87
#define hfc_MST_EMOD  0xB4
88
#define hfc_MST_MODE	 0xB8
89
#define hfc_CONNECT 	 0xBC
90
91
92
/* Interrupt and status registers */
93
94
#define hfc_FIFO_EN   0x44
95
#define hfc_TRM       0x48
96
#define hfc_B_MODE    0x4C
97
#define hfc_CHIP_ID   0x58
98
#define hfc_CIRM  	 0x60
99
#define hfc_CTMT	 0x64
100
#define hfc_INT_M1  	 0x68
101
#define hfc_INT_M2  	 0x6C
102
#define hfc_INT_S1  	 0x78
103
#define hfc_INT_S2  	 0x7C
104
#define hfc_STATUS  	 0x70
105
106
/* S/T section registers */
107
108
#define hfc_STATES  	 0xC0
109
#define hfc_SCTRL  	 0xC4
110
#define hfc_SCTRL_E   0xC8
111
#define hfc_SCTRL_R   0xCC
112
#define hfc_SQ  	 0xD0
113
#define hfc_CLKDEL  	 0xDC
114
#define hfc_B1_REC    0xF0
115
#define hfc_B1_SEND   0xF0
116
#define hfc_B2_REC    0xF4
117
#define hfc_B2_SEND   0xF4
118
#define hfc_D_REC     0xF8
119
#define hfc_D_SEND    0xF8
120
#define hfc_E_REC     0xFC
121
122
/* Bits and values in various HFC PCI registers */
123
124
/* bits in status register (READ) */
125
#define hfc_STATUS_PCI_PROC   0x02
126
#define hfc_STATUS_NBUSY	0x04
127
#define hfc_STATUS_TIMER_ELAP 0x10
128
#define hfc_STATUS_STATINT	  0x20
129
#define hfc_STATUS_FRAMEINT	  0x40
130
#define hfc_STATUS_ANYINT	  0x80
131
132
/* bits in CTMT (Write) */
133
#define hfc_CTMT_TRANSB1	0x01
134
#define hfc_CTMT_TRANSB2	0x02
135
#define hfc_CTMT_TIMER_CLEAR	0x80
136
#define hfc_CTMT_TIMER_MASK	0x1C
137
#define hfc_CTMT_TIMER_3_125	(0x01 << 2)
138
#define hfc_CTMT_TIMER_6_25	(0x02 << 2)
139
#define hfc_CTMT_TIMER_12_5	(0x03 << 2)
140
#define hfc_CTMT_TIMER_25	(0x04 << 2)
141
#define hfc_CTMT_TIMER_50	(0x05 << 2)
142
#define hfc_CTMT_TIMER_400	(0x06 << 2)
143
#define hfc_CTMT_TIMER_800	(0x07 << 2)
144
#define hfc_CTMT_AUTO_TIMER	0x20
145
146
/* bits in CIRM (Write) */
147
#define hfc_CIRM_AUX_MSK    0x07
148
#define hfc_CIRM_RESET  	  0x08
149
#define hfc_CIRM_B1_REV     0x40
150
#define hfc_CIRM_B2_REV     0x80
151
152
/* bits in INT_M1 and INT_S1 */
153
#define hfc_INTS_B1TRANS  0x01
154
#define hfc_INTS_B2TRANS  0x02
155
#define hfc_INTS_DTRANS   0x04
156
#define hfc_INTS_B1REC    0x08
157
#define hfc_INTS_B2REC    0x10
158
#define hfc_INTS_DREC     0x20
159
#define hfc_INTS_L1STATE  0x40
160
#define hfc_INTS_TIMER    0x80
161
162
/* bits in INT_M2 */
163
#define hfc_M2_PROC_TRANS    0x01
164
#define hfc_M2_GCI_I_CHG     0x02
165
#define hfc_M2_GCI_MON_REC   0x04
166
#define hfc_M2_IRQ_ENABLE    0x08
167
#define hfc_M2_PMESEL        0x80
168
169
/* bits in STATES */
170
#define hfc_STATES_STATE_MASK     0x0F
171
#define hfc_STATES_LOAD_STATE    0x10
172
#define hfc_STATES_ACTIVATE	     0x20
173
#define hfc_STATES_DO_ACTION     0x40
174
#define hfc_STATES_NT_G2_G3      0x80
175
176
/* bits in HFCD_MST_MODE */
177
#define hfc_MST_MODE_MASTER	     0x01
178
#define hfc_MST_MODE_SLAVE         0x00
179
/* remaining bits are for codecs control */
180
181
/* bits in HFCD_SCTRL */
182
#define hfc_SCTRL_B1_ENA	     0x01
183
#define hfc_SCTRL_B2_ENA	     0x02
184
#define hfc_SCTRL_MODE_TE        0x00
185
#define hfc_SCTRL_MODE_NT        0x04
186
#define hfc_SCTRL_LOW_PRIO	     0x08
187
#define hfc_SCTRL_SQ_ENA	     0x10
188
#define hfc_SCTRL_TEST	     0x20
189
#define hfc_SCTRL_NONE_CAP	     0x40
190
#define hfc_SCTRL_PWR_DOWN	     0x80
191
192
/* bits in SCTRL_E  */
193
#define hfc_SCTRL_E_AUTO_AWAKE    0x01
194
#define hfc_SCTRL_E_DBIT_1        0x04
195
#define hfc_SCTRL_E_IGNORE_COL    0x08
196
#define hfc_SCTRL_E_CHG_B1_B2     0x80
197
198
/* bits in SCTRL_R  */
199
#define hfc_SCTRL_R_B1_ENA	     0x01
200
#define hfc_SCTRL_R_B2_ENA	     0x02
201
202
/* bits in FIFO_EN register */
203
#define hfc_FIFOEN_B1TX   0x01
204
#define hfc_FIFOEN_B1RX   0x02
205
#define hfc_FIFOEN_B2TX   0x04
206
#define hfc_FIFOEN_B2RX   0x08
207
#define hfc_FIFOEN_DTX    0x10
208
#define hfc_FIFOEN_DRX    0x20
209
210
#define hfc_FIFOEN_B1     (hfc_FIFOEN_B1TX|hfc_FIFOEN_B1RX)
211
#define hfc_FIFOEN_B2     (hfc_FIFOEN_B2TX|hfc_FIFOEN_B2RX)
212
#define hfc_FIFOEN_D      (hfc_FIFOEN_DTX|hfc_FIFOEN_DRX)
213
214
/* bits in the CONNECT register */
215
#define	hfc_CONNECT_B1_HFC_from_ST		0x00
216
#define	hfc_CONNECT_B1_HFC_from_GCI		0x01
217
#define hfc_CONNECT_B1_ST_from_HFC		0x00
218
#define hfc_CONNECT_B1_ST_from_GCI		0x02
219
#define hfc_CONNECT_B1_GCI_from_HFC		0x00
220
#define hfc_CONNECT_B1_GCI_from_ST		0x04
221
222
#define	hfc_CONNECT_B2_HFC_from_ST		0x00
223
#define	hfc_CONNECT_B2_HFC_from_GCI		0x08
224
#define hfc_CONNECT_B2_ST_from_HFC		0x00
225
#define hfc_CONNECT_B2_ST_from_GCI		0x10
226
#define hfc_CONNECT_B2_GCI_from_HFC		0x00
227
#define hfc_CONNECT_B2_GCI_from_ST		0x20
228
229
/* bits in the TRM register */
230
#define hfc_TRM_TRANS_INT_00	0x00
231
#define hfc_TRM_TRANS_INT_01	0x01
232
#define hfc_TRM_TRANS_INT_10	0x02
233
#define hfc_TRM_TRANS_INT_11	0x04
234
#define hfc_TRM_ECHO		0x20
235
#define hfc_TRM_B1_PLUS_B2	0x40
236
#define hfc_TRM_IOM_TEST_LOOP	0x80
237
238
/* bits in the __SSL and __RSL registers */
239
#define	hfc_SRSL_STIO		0x40
240
#define hfc_SRSL_ENABLE		0x80
241
#define hfc_SRCL_SLOT_MASK	0x1f
242
243
/* FIFO memory definitions */
244
245
#define hfc_FIFO_SIZE   0x8000
246
247
#define hfc_UGLY_FRAMEBUF 0x2000
248
249
#define hfc_TX_FIFO_PRELOAD (DAHDI_CHUNKSIZE + 2)
250
#define hfc_RX_FIFO_PRELOAD 4
251
252
/* HDLC STUFF */
253
#define hfc_HDLC_BUF_LEN	32
254
/* arbitrary, just the max # of byts we will send to DAHDI per call */
255
256
257
/* NOTE: FIFO pointers are not declared volatile because accesses to the
258
 *       FIFOs are inherently safe.
259
 */
260
261
#ifdef DEBUG
262
extern int debug_level;
263
#endif
264
265
struct hfc_chan;
266
267
struct hfc_chan_simplex {
268
	struct hfc_chan_duplex *chan;
269
270
	u8 zaptel_buffer[DAHDI_CHUNKSIZE];
271
272
	u8 ugly_framebuf[hfc_UGLY_FRAMEBUF];
273
	int ugly_framebuf_size;
274
	u16 ugly_framebuf_off;
275
276
	void *z1_base, *z2_base;
277
	void *fifo_base;
278
	void *z_base;
279
	u16 z_min;
280
	u16 z_max;
281
	u16 fifo_size;
282
283
	u8 *f1, *f2;
284
	u8 f_min;
285
	u8 f_max;
286
	u8 f_num;
287
288
	unsigned long long frames;
289
	unsigned long long bytes;
290
	unsigned long long fifo_full;
291
	unsigned long long crc;
292
	unsigned long long fifo_underrun;
293
};
294
295
enum hfc_chan_status {
296
	free,
297
	open_framed,
298
	open_voice,
299
	sniff_aux,
300
	loopback,
301
};
302
303
struct hfc_chan_duplex {
304
	struct hfc_card *card;
305
306
	char *name;
307
	int number;
308
309
	enum hfc_chan_status status;
310
	int open_by_netdev;
311
	int open_by_zaptel;
312
313
	unsigned short protocol;
314
315
	spinlock_t lock;
316
317
	struct hfc_chan_simplex rx;
318
	struct hfc_chan_simplex tx;
319
320
};
321
322
typedef struct hfc_card {
323
	int cardnum;
324
	struct pci_dev *pcidev;
325
	struct dahdi_hfc *ztdev;
326
	struct proc_dir_entry *proc_dir;
327
	char proc_dir_name[32];
328
329
	struct proc_dir_entry *proc_info;
330
	struct proc_dir_entry *proc_fifos;
331
	struct proc_dir_entry *proc_bufs;
332
333
	unsigned long io_bus_mem;
334
	void __iomem *io_mem;
335
336
	dma_addr_t fifo_bus_mem;
337
	void *fifo_mem;
338
	void *fifos;
339
340
	int nt_mode;
341
	int sync_loss_reported;
342
	int late_irqs;
343
344
	u8 l1_state;
345
	int fifo_suspended;
346
	int ignore_first_timer_interrupt;
347
348
	struct {
349
		u8 m1;
350
		u8 m2;
351
		u8 fifo_en;
352
		u8 trm;
353
		u8 connect;
354
		u8 sctrl;
355
		u8 sctrl_r;
356
		u8 sctrl_e;
357
		u8 ctmt;
358
		u8 cirm;
359
	} regs;
360
361
	struct hfc_chan_duplex chans[3];
362
	int echo_enabled;
363
364
365
366
	int debug_event;
367
368
    spinlock_t lock;
369
    unsigned int irq;
370
    unsigned int iomem;
371
    int ticks;
372
    int clicks;
373
    unsigned char *pci_io;
374
    void *fifomem;		/* start of the shared mem */
375
376
    unsigned int pcibus;
377
    unsigned int pcidevfn;
378
379
    int	drecinframe;
380
381
    unsigned char cardno;
382
    struct hfc_card *next;
383
384
} hfc_card;
385
386
typedef struct dahdi_hfc {
387
    unsigned int usecount;
388
    struct dahdi_span span;
389
    struct dahdi_chan chans[3];
390
    struct dahdi_chan *_chans[3];
391
    struct hfc_card *card;
392
393
    /* pointer to the signalling channel for this span */
394
    struct dahdi_chan *sigchan;
395
    /* nonzero means we're in the middle of sending an HDLC frame */
396
    int sigactive;
397
    /* hdlc_hard_xmit() increments, hdlc_tx_frame() decrements */
398
    atomic_t hdlc_pending;
399
    int frames_out;
400
    int frames_in;
401
402
} dahdi_hfc;
403
404
static inline u8 hfc_inb(struct hfc_card *card, int offset)
405
{
406
 return readb(card->io_mem + offset);
407
}
408
409
static inline void hfc_outb(struct hfc_card *card, int offset, u8 value)
410
{
411
 writeb(value, card->io_mem + offset);
412
}
413
414
#endif

Return to bug 302316