summarylogtreecommitdiffstats
path: root/dahdi-linux-2.10.1-openvox-1.patch
diff options
context:
space:
mode:
Diffstat (limited to 'dahdi-linux-2.10.1-openvox-1.patch')
-rw-r--r--dahdi-linux-2.10.1-openvox-1.patch6228
1 files changed, 6228 insertions, 0 deletions
diff --git a/dahdi-linux-2.10.1-openvox-1.patch b/dahdi-linux-2.10.1-openvox-1.patch
new file mode 100644
index 000000000000..4a8fcf98f892
--- /dev/null
+++ b/dahdi-linux-2.10.1-openvox-1.patch
@@ -0,0 +1,6228 @@
+--- dahdi-linux-2.10.0.1/drivers/dahdi/Kbuild 2014-09-22 20:40:19.000000000 +0200
++++ dahdi-linux-2.10.0.1-openvox/drivers/dahdi/Kbuild 2015-02-10 15:12:07.233961480 +0100
+@@ -14,6 +14,10 @@
+ obj-$(DAHDI_BUILD_ALL)$(CONFIG_DAHDI_WCTE12XP) += wcte12xp/
+ obj-$(DAHDI_BUILD_ALL)$(CONFIG_DAHDI_WCTE13XP) += wcte13xp.o
+
++obj-$(DAHDI_BUILD_ALL)$(CONFIG_DAHDI_OPVXD115) += opvxd115/
++obj-$(DAHDI_BUILD_ALL)$(CONFIG_DAHDI_OPVXA1200) += opvxa1200/
++obj-$(DAHDI_BUILD_ALL)$(CONFIG_DAHDI_OPVXA24XX) += opvxa24xx/
++
+ wcte13xp-objs := wcte13xp-base.o wcxb_spi.o wcxb.o wcxb_flash.o
+ CFLAGS_wcte13xp-base.o += -I$(src)/oct612x -I$(src)/oct612x/include -I$(src)/oct612x/octdeviceapi -I$(src)/oct612x/octdeviceapi/oct6100api
+ ifeq ($(HOTPLUG_FIRMWARE),yes)
+--- dahdi-linux-2.10.0.1/drivers/dahdi/Kconfig 2014-09-22 20:40:19.000000000 +0200
++++ dahdi-linux-2.10.0.1-openvox/drivers/dahdi/Kconfig 2015-02-10 15:03:42.355884929 +0100
+@@ -291,4 +291,34 @@
+
+ If unsure, say Y.
+
++config DAHDI_OPVXD115
++ tristate "OpenVox TE410P Quad-T1/E1 PCI"
++ depends on DAHDI && PCI
++ default DAHDI
++ ---help---
++ To compile this driver as a module, choose M here: the
++ module will be called opvxd115.
++
++ If unsure, say Y.
++
++config DAHDI_OPVXA1200
++ tristate "OpenVox A1200P FXS/FXO Interface Driver"
++ depends on DAHDI && PCI
++ default DAHDI
++ ---help---
++ To compile this driver as a module, choose M here: the
++ module will be called opvxa1200.
++
++ If unsure, say Y.
++
++config DAHDI_OPVXA24XX
++ tristate "OpenVox A24xx FXS/FXO Interface Driver"
++ depends on DAHDI && PCI
++ default DAHDI
++ ---help---
++ To compile this driver as a module, choose M here: the
++ module will be called opvxa24xx.
++
++ If unsure, say Y.
++
+ source "drivers/dahdi/xpp/Kconfig"
+--- dahdi-linux-2.10.0.1/drivers/dahdi/opvxa1200/Kbuild 1970-01-01 01:00:00.000000000 +0100
++++ dahdi-linux-2.10.0.1-openvox/drivers/dahdi/opvxa1200/Kbuild 2015-02-10 14:19:03.000000000 +0100
+@@ -0,0 +1,19 @@
++obj-$(DAHDI_BUILD_ALL)$(CONFIG_DAHDI_OPVXA1200) += opvxa1200.o
++
++EXTRA_CFLAGS += -I$(src)/.. -Wno-undef
++
++opvxa1200-objs := base.o
++
++DAHDI_KERNEL_H_NAME:=kernel.h
++DAHDI_KERNEL_H_PATH:=$(DAHDI_INCLUDE)/dahdi/$(DAHDI_KERNEL_H_NAME)
++ifneq ($(DAHDI_KERNEL_H_PATH),)
++ DAHDI_SPAN_MODULE:=$(shell if grep -C 5 "struct dahdi_span {" $(DAHDI_KERNEL_H_PATH) | grep -q "struct module \*owner"; then echo "yes"; else echo "no"; fi)
++ DAHDI_SPAN_OPS:=$(shell if grep -q "struct dahdi_span_ops {" $(DAHDI_KERNEL_H_PATH); then echo "yes"; else echo "no"; fi)
++ ifeq ($(DAHDI_SPAN_MODULE),yes)
++ EXTRA_CFLAGS+=-DDAHDI_SPAN_MODULE
++ else
++ ifeq ($(DAHDI_SPAN_OPS),yes)
++ EXTRA_CFLAGS+=-DDAHDI_SPAN_OPS
++ endif
++ endif
++endif
+--- dahdi-linux-2.10.0.1/drivers/dahdi/opvxa1200/Makefile 1970-01-01 01:00:00.000000000 +0100
++++ dahdi-linux-2.10.0.1-openvox/drivers/dahdi/opvxa1200/Makefile 2015-02-10 14:19:03.000000000 +0100
+@@ -0,0 +1,8 @@
++ifdef KBUILD_EXTMOD
++# We only get here on kernels 2.6.0-2.6.9 .
++# For newer kernels, Kbuild will be included directly by the kernel
++# build system.
++include $(src)/Kbuild
++
++else
++endif
+--- dahdi-linux-2.10.0.1/drivers/dahdi/opvxa1200/base.c 1970-01-01 01:00:00.000000000 +0100
++++ dahdi-linux-2.10.0.1-openvox/drivers/dahdi/opvxa1200/base.c 2015-02-10 14:19:03.000000000 +0100
+@@ -0,0 +1,3117 @@
++/*
++ * OpenVox A1200P FXS/FXO Interface Driver for DAHDI Telephony interface
++ *
++ * Written by MiaoLin<miaolin@openvox.cn>
++ *
++ * Copyright (C) 2005-2010 OpenVox Communication Co. Ltd,
++ *
++ * All rights reserved.
++ *
++ * This program is free software; you can redistribute it and/or modify
++ * it under the terms of the GNU General Public License as published by
++ * the Free Software Foundation; either version 2 of the License, or
++ * (at your option) any later version.
++ *
++ * This program is distributed in the hope that it will be useful,
++ * but WITHOUT ANY WARRANTY; without even the implied warranty of
++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
++ * GNU General Public License for more details.
++ *
++ * You should have received a copy of the GNU General Public License
++ * along with this program; if not, write to the Free Software
++ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
++ *
++ */
++
++/* Rev histroy
++ *
++ * Rev 0.10 initial version
++ * Rev 0.11
++ * fixed the led light on/off bug.
++ * modify some wctdm print to opvxa1200
++ * support firmware version 1.2, faster i/o operation, and better LED control.
++ *
++ * Rev 0.12 patched to support new pci id 0x8519
++ * Rev 0.13 patched to remove the warning during compile under kernel 2.6.22
++ * Rev 0.14 patched to remove the bug for ZAP_IRQ_SHARED , 3/9/2007
++ * Rev 0.15 patched to support new pci ID 0X9532 by james.zhu, 23/10/2007
++ * Rev 0.16 support new pci id 0x9559 by Miao Lin 21/3/2008
++ * Rev 0.17
++ * patched a few bugs,
++ * add hwgain support.
++ * fixed A800P version check
++ * Rev 1.4.9.2
++ * Only generate 8 channels for A800P
++ * Version number synced to zaptel distribution.
++ * Rev 1.4.9.2.a
++ * Fixed freeregion.
++ *
++ * Rev 1.4.9.2.b
++ * Add cid before first ring support.
++ * New Paremeters:
++ * cidbeforering : set to 1 will cause the card enable cidbeforering function. default 0
++ * cidbuflen : length of cid buffer, in msec, default 3000 msec.
++ * cidtimeout : time out of a ring, default 6000msec
++ * User must set cidstart=polarity in zapata.conf to use with this feature
++ * cidsignalling = signalling format send before 1st ring. most likely dtmf.
++ *
++ * Rev 1.4.9.2.c
++ * add driver parameter cidtimeout.
++ *
++ * Rev 1.4.9.2.d
++ * add debug stuff to test fxs power alarm
++ *
++ * Rev 1.4.11
++ * Support enhanced full scale tx/rx for FXO required by europe standard (Register 30, acim) (module parm fxofullscale)
++ *
++ * Rev 1.4.12 2008/10/17
++ * Fixed bug cause FXS module report fake power alarm.
++ * Power alarm debug stuff removed.
++ *
++ * Rev 2.0 DAHDI 2008/10/17
++ *
++ * Rev 2.0.1 add new pci id 0x9599
++ * Re 2.0.2 12/01/2009
++ add fixedtimepolarity: set time(ms) when send polarity after 1st ring happen.
++ * Sometimes the dtmf cid is sent just after first ring off, and the system do not have
++ * enough time to start detect 1st dtmf.
++ * 0 means send polarity at the end of 1st ring.
++ * x means send ploarity after x ms of 1st ring begin.
++ *
++ * Rev 2.0.3 12/01/2009
++ * Add touch_softlockup_watchdog() in wctdm_hardware_init, to avoid cpu softlockup system message for FXS.
++ *
++ *
++ * Rev 1.4.12.4 17/04/2009 James.zhu
++ * Changed wctdm_voicedaa_check_hook() to detect FXO battery and solved the problem with dial(dahdi/go/XXXXXXXXXX)
++ * add alarm detection for FXO
++ *
++ * Rev 1.4.12.5 01/10/2009 james.zhu
++ * Add jiffies for 5 second in wctdm_hardware_init
++ *
++ * Rev 1.4.12.6 5/15/2011 Miaolin
++ * use write dma to generate irq.
++ * add parameter watchdma allow reset dma when it is not correctly started.
++ * add delay after reset
++ * change reset time to 1 sec.
++ */
++
++#include <linux/kernel.h>
++#include <linux/errno.h>
++#include <linux/module.h>
++#include <linux/init.h>
++#include <linux/errno.h>
++#include <linux/pci.h>
++#include <linux/interrupt.h>
++#include <linux/moduleparam.h>
++#include <asm/io.h>
++#include <linux/sched.h>
++#include "proslic.h"
++
++/* MiaoLin debug start */
++#include <linux/string.h>
++#include <asm/uaccess.h> /* get_fs(), set_fs(), KERNEL_DS */
++#include <linux/file.h> /* fput() */
++/* MiaoLin debug end */
++
++
++/*
++ * Define for audio vs. register based ring detection
++ *
++ */
++/* #define AUDIO_RINGCHECK */
++
++/*
++ Experimental max loop current limit for the proslic
++ Loop current limit is from 20 mA to 41 mA in steps of 3
++ (according to datasheet)
++ So set the value below to:
++ 0x00 : 20mA (default)
++ 0x01 : 23mA
++ 0x02 : 26mA
++ 0x03 : 29mA
++ 0x04 : 32mA
++ 0x05 : 35mA
++ 0x06 : 37mA
++ 0x07 : 41mA
++*/
++static int loopcurrent = 20;
++
++static int reversepolarity = 0;
++
++static alpha indirect_regs[] =
++{
++{0,255,"DTMF_ROW_0_PEAK",0x55C2},
++{1,255,"DTMF_ROW_1_PEAK",0x51E6},
++{2,255,"DTMF_ROW2_PEAK",0x4B85},
++{3,255,"DTMF_ROW3_PEAK",0x4937},
++{4,255,"DTMF_COL1_PEAK",0x3333},
++{5,255,"DTMF_FWD_TWIST",0x0202},
++{6,255,"DTMF_RVS_TWIST",0x0202},
++{7,255,"DTMF_ROW_RATIO_TRES",0x0198},
++{8,255,"DTMF_COL_RATIO_TRES",0x0198},
++{9,255,"DTMF_ROW_2ND_ARM",0x0611},
++{10,255,"DTMF_COL_2ND_ARM",0x0202},
++{11,255,"DTMF_PWR_MIN_TRES",0x00E5},
++{12,255,"DTMF_OT_LIM_TRES",0x0A1C},
++{13,0,"OSC1_COEF",0x7B30},
++{14,1,"OSC1X",0x0063},
++{15,2,"OSC1Y",0x0000},
++{16,3,"OSC2_COEF",0x7870},
++{17,4,"OSC2X",0x007D},
++{18,5,"OSC2Y",0x0000},
++{19,6,"RING_V_OFF",0x0000},
++{20,7,"RING_OSC",0x7EF0},
++{21,8,"RING_X",0x0160},
++{22,9,"RING_Y",0x0000},
++{23,255,"PULSE_ENVEL",0x2000},
++{24,255,"PULSE_X",0x2000},
++{25,255,"PULSE_Y",0x0000},
++//{26,13,"RECV_DIGITAL_GAIN",0x4000}, // playback volume set lower
++{26,13,"RECV_DIGITAL_GAIN",0x2000}, // playback volume set lower
++{27,14,"XMIT_DIGITAL_GAIN",0x4000},
++//{27,14,"XMIT_DIGITAL_GAIN",0x2000},
++{28,15,"LOOP_CLOSE_TRES",0x1000},
++{29,16,"RING_TRIP_TRES",0x3600},
++{30,17,"COMMON_MIN_TRES",0x1000},
++{31,18,"COMMON_MAX_TRES",0x0200},
++{32,19,"PWR_ALARM_Q1Q2",0x07C0},
++{33,20,"PWR_ALARM_Q3Q4",0x2600},
++{34,21,"PWR_ALARM_Q5Q6",0x1B80},
++{35,22,"LOOP_CLOSURE_FILTER",0x8000},
++{36,23,"RING_TRIP_FILTER",0x0320},
++{37,24,"TERM_LP_POLE_Q1Q2",0x008C},
++{38,25,"TERM_LP_POLE_Q3Q4",0x0100},
++{39,26,"TERM_LP_POLE_Q5Q6",0x0010},
++{40,27,"CM_BIAS_RINGING",0x0C00},
++{41,64,"DCDC_MIN_V",0x0C00},
++{42,255,"DCDC_XTRA",0x1000},
++{43,66,"LOOP_CLOSE_TRES_LOW",0x1000},
++};
++
++
++#include <dahdi/kernel.h>
++#include <dahdi/wctdm_user.h>
++
++#include "fxo_modes.h"
++
++#define NUM_FXO_REGS 60
++
++#define WC_MAX_IFACES 128
++
++#define WC_OFFSET 4 /* Offset between transmit and receive, in bytes. */
++#define WC_SYNCFLAG 0xca1ef1ac
++
++#define WC_CNTL 0x00
++#define WC_OPER 0x01
++#define WC_AUXC 0x02
++#define WC_AUXD 0x03
++#define WC_MASK0 0x04
++#define WC_MASK1 0x05
++#define WC_INTSTAT 0x06
++#define WC_AUXR 0x07
++
++#define WC_DMAWS 0x08
++#define WC_DMAWI 0x0c
++#define WC_DMAWE 0x10
++#define WC_DMARS 0x18
++#define WC_DMARI 0x1c
++#define WC_DMARE 0x20
++
++#define WC_AUXFUNC 0x2b
++#define WC_SERCTL 0x2d
++#define WC_FSCDELAY 0x2f
++
++#define WC_REGBASE 0xc0
++
++#define WC_VER 0x0
++#define WC_CS 0x1
++#define WC_SPICTRL 0x2
++#define WC_SPIDATA 0x3
++
++#define BIT_SPI_BYHW (1 << 0)
++#define BIT_SPI_BUSY (1 << 1) // 0=can read/write spi, 1=spi working.
++#define BIT_SPI_START (1 << 2)
++
++
++#define BIT_LED_CLK (1 << 0) // MiaoLin add to control the led.
++#define BIT_LED_DATA (1 << 1) // MiaoLin add to control the led.
++
++#define BIT_CS (1 << 2)
++#define BIT_SCLK (1 << 3)
++#define BIT_SDI (1 << 4)
++#define BIT_SDO (1 << 5)
++
++#define FLAG_EMPTY 0
++#define FLAG_WRITE 1
++#define FLAG_READ 2
++#define DEFAULT_RING_DEBOUNCE 64 /* Ringer Debounce (64 ms) */
++#define POLARITY_DEBOUNCE 64 /* Polarity debounce (64 ms) */
++#define OHT_TIMER 6000 /* How long after RING to retain OHT */
++
++#define FLAG_3215 (1 << 0)
++#define FLAG_A800 (1 << 7)
++
++#define MAX_NUM_CARDS 12
++#define NUM_CARDS 12
++#define NUM_FLAG 4 /* number of flag channels. */
++
++
++enum cid_hook_state {
++ CID_STATE_IDLE = 0,
++ CID_STATE_RING_ON,
++ CID_STATE_RING_OFF,
++ CID_STATE_WAIT_RING_FINISH
++};
++
++/* if you want to record the last 8 sec voice before the driver unload, uncomment it and rebuild. */
++/* #define TEST_LOG_INCOME_VOICE */
++#define voc_buffer_size (8000*8)
++
++
++#define MAX_ALARMS 10
++
++#define MOD_TYPE_FXS 0
++#define MOD_TYPE_FXO 1
++
++#define MINPEGTIME 10 * 8 /* 30 ms peak to peak gets us no more than 100 Hz */
++#define PEGTIME 50 * 8 /* 50ms peak to peak gets us rings of 10 Hz or more */
++#define PEGCOUNT 5 /* 5 cycles of pegging means RING */
++
++#define NUM_CAL_REGS 12
++
++struct calregs {
++ unsigned char vals[NUM_CAL_REGS];
++};
++
++enum proslic_power_warn {
++ PROSLIC_POWER_UNKNOWN = 0,
++ PROSLIC_POWER_ON,
++ PROSLIC_POWER_WARNED,
++};
++
++enum battery_state {
++ BATTERY_UNKNOWN = 0,
++ BATTERY_PRESENT,
++ BATTERY_LOST,
++};
++struct wctdm {
++ struct pci_dev *dev;
++ char *variety;
++ struct dahdi_span span;
++ struct dahdi_device *ddev;
++ unsigned char ios;
++ int usecount;
++ unsigned int intcount;
++ int dead;
++ int pos;
++ int flags[MAX_NUM_CARDS];
++ int freeregion;
++ int alt;
++ int curcard;
++ int cardflag; /* Bit-map of present cards */
++ enum proslic_power_warn proslic_power;
++ spinlock_t lock;
++
++ union {
++ struct fxo {
++#ifdef AUDIO_RINGCHECK
++ unsigned int pegtimer;
++ int pegcount;
++ int peg;
++ int ring;
++#else
++ int wasringing;
++ int lastrdtx;
++#endif
++ int ringdebounce;
++ int offhook;
++ unsigned int battdebounce;
++ unsigned int battalarm;
++ enum battery_state battery;
++ int lastpol;
++ int polarity;
++ int polaritydebounce;
++ } fxo;
++ struct fxs {
++ int oldrxhook;
++ int debouncehook;
++ int lastrxhook;
++ int debounce;
++ int ohttimer;
++ int idletxhookstate; /* IDLE changing hook state */
++ int lasttxhook;
++ int palarms;
++ struct calregs calregs;
++ } fxs;
++ } mod[MAX_NUM_CARDS];
++
++ /* Receive hook state and debouncing */
++ int modtype[MAX_NUM_CARDS];
++ unsigned char reg0shadow[MAX_NUM_CARDS];
++ unsigned char reg1shadow[MAX_NUM_CARDS];
++
++ unsigned long ioaddr;
++ unsigned long mem_region; /* 32 bit Region allocated to tiger320 */
++ unsigned long mem_len; /* Length of 32 bit region */
++ volatile unsigned long mem32; /* Virtual representation of 32 bit memory area */
++
++ dma_addr_t readdma;
++ dma_addr_t writedma;
++ volatile unsigned char *writechunk; /* Double-word aligned write memory */
++ volatile unsigned char *readchunk; /* Double-word aligned read memory */
++ /*struct dahdi_chan chans[MAX_NUM_CARDS];*/
++ struct dahdi_chan _chans[NUM_CARDS];
++ struct dahdi_chan *chans[NUM_CARDS];
++
++
++#ifdef TEST_LOG_INCOME_VOICE
++ char * voc_buf[MAX_NUM_CARDS + NUM_FLAG];
++ int voc_ptr[MAX_NUM_CARDS + NUM_FLAG];
++#endif
++ int lastchan;
++ unsigned short ledstate;
++ unsigned char fwversion;
++ int max_cards;
++ char *card_name;
++
++ char *cid_history_buf[MAX_NUM_CARDS];
++ int cid_history_ptr[MAX_NUM_CARDS];
++ int cid_history_clone_cnt[MAX_NUM_CARDS];
++ enum cid_hook_state cid_state[MAX_NUM_CARDS];
++ int cid_ring_on_time[MAX_NUM_CARDS];
++};
++
++static char* A1200P_Name = "A1200P";
++static char* A800P_Name = "A800P";
++
++struct wctdm_desc {
++ char *name;
++ int flags;
++};
++
++static struct wctdm_desc wctdme = { "OpenVox A1200P/A800P", 0 };
++static int acim2tiss[16] = { 0x0, 0x1, 0x4, 0x5, 0x7, 0x0, 0x0, 0x6, 0x0, 0x0, 0x0, 0x2, 0x0, 0x3 };
++
++static struct wctdm *ifaces[WC_MAX_IFACES];
++
++static void wctdm_release(struct wctdm *wc);
++
++static int watchdma=0;
++static unsigned int battdebounce;
++static unsigned int battalarm;
++static unsigned int battthresh;
++static int ringdebounce = DEFAULT_RING_DEBOUNCE;
++/* times 4, because must be a multiple of 4ms: */
++static int dialdebounce = 8 * 8;
++static int fwringdetect = 0;
++static int debug = 0;
++static int robust = 0;
++static int timingonly = 0;
++static int lowpower = 0;
++static int boostringer = 0;
++static int fastringer = 0;
++static int _opermode = 0;
++static char *opermode = "FCC";
++static int fxshonormode = 0;
++static int alawoverride = 0;
++static int fastpickup = 0;
++static int fxotxgain = 0;
++static int fxorxgain = 0;
++static int fxstxgain = 0;
++static int fxsrxgain = 0;
++/* special h/w control command */
++static int spibyhw = 1;
++static int usememio = 1;
++static int cidbeforering = 0;
++static int cidbuflen = 3000; /* in msec, default 3000 */
++static int cidtimeout = 6*1000; /* in msec, default 6000 */
++static int fxofullscale = 0; /* fxo full scale tx/rx, register 30, acim */
++static int fixedtimepolarity=0; /* time delay in ms when send polarity after rise edge of 1st ring.*/
++
++static int wctdm_init_proslic(struct wctdm *wc, int card, int fast , int manual, int sane);
++
++static void wctdm_set_led(struct wctdm* wc, int card, int onoff)
++{
++ int i;
++ unsigned char c;
++
++ wc->ledstate &= ~(0x01<<card);
++ wc->ledstate |= (onoff<<card);
++ c = (inb(wc->ioaddr + WC_AUXD)&~BIT_LED_CLK)|BIT_LED_DATA;
++ outb( c, wc->ioaddr + WC_AUXD);
++ for(i=MAX_NUM_CARDS-1; i>=0; i--)
++ {
++ if(wc->ledstate & (0x0001<<i))
++ if(wc->fwversion == 0x11)
++ c &= ~BIT_LED_DATA;
++ else
++ c |= BIT_LED_DATA;
++ else
++ if(wc->fwversion == 0x11)
++ c |= BIT_LED_DATA;
++ else
++ c &= ~BIT_LED_DATA;
++
++ outb( c, wc->ioaddr + WC_AUXD);
++ outb( c|BIT_LED_CLK, wc->ioaddr + WC_AUXD);
++ outb( (c&~BIT_LED_CLK)|BIT_LED_DATA, wc->ioaddr + WC_AUXD);
++ }
++}
++
++
++static inline void wctdm_transmitprep(struct wctdm *wc, unsigned char ints)
++{
++ int x, y, chan_offset, pos;
++ volatile unsigned char *txbuf;
++
++ //if (ints & /*0x01*/ 0x04)
++ if (ints & 0x01)
++ /* Write is at interrupt address. Start writing from normal offset */
++ txbuf = wc->writechunk;
++ else
++ txbuf = wc->writechunk + DAHDI_CHUNKSIZE * (MAX_NUM_CARDS+NUM_FLAG);
++
++ /* Calculate Transmission */
++ dahdi_transmit(&wc->span);
++
++ if(wc->lastchan == -1) // not in sync.
++ return;
++
++ chan_offset = (wc->lastchan*4 + 4 ) % (MAX_NUM_CARDS+NUM_FLAG);
++
++ for (y=0;y<DAHDI_CHUNKSIZE;y++) {
++#ifdef __BIG_ENDIAN
++ // operation pending...
++#else
++ for (x=0;x<(MAX_NUM_CARDS+NUM_FLAG);x++) {
++ pos = y * (MAX_NUM_CARDS+NUM_FLAG) + ((x + chan_offset + MAX_NUM_CARDS+NUM_FLAG - WC_OFFSET)&0x0f);
++ if(x<wc->max_cards/*MAX_NUM_CARDS*/)
++ txbuf[pos] = wc->chans[x]->writechunk[y];
++ else
++ txbuf[pos] = 0;
++ }
++#endif
++ }
++}
++
++
++#ifdef AUDIO_RINGCHECK
++static inline void ring_check(struct wctdm *wc, int card)
++{
++ int x;
++ short sample;
++ if (wc->modtype[card] != MOD_TYPE_FXO)
++ return;
++ wc->mod[card].fxo.pegtimer += DAHDI_CHUNKSIZE;
++ for (x=0;x<DAHDI_CHUNKSIZE;x++) {
++ /* Look for pegging to indicate ringing */
++ sample = DAHDI_XLAW(wc->chans[card].readchunk[x], (&(wc->chans[card])));
++ if ((sample > 10000) && (wc->mod[card].fxo.peg != 1)) {
++ if (debug > 1) printk(KERN_DEBUG "High peg!\n");
++ if ((wc->mod[card].fxo.pegtimer < PEGTIME) && (wc->mod[card].fxo.pegtimer > MINPEGTIME))
++ wc->mod[card].fxo.pegcount++;
++ wc->mod[card].fxo.pegtimer = 0;
++ wc->mod[card].fxo.peg = 1;
++ } else if ((sample < -10000) && (wc->mod[card].fxo.peg != -1)) {
++ if (debug > 1) printk(KERN_DEBUG "Low peg!\n");
++ if ((wc->mod[card].fxo.pegtimer < (PEGTIME >> 2)) && (wc->mod[card].fxo.pegtimer > (MINPEGTIME >> 2)))
++ wc->mod[card].fxo.pegcount++;
++ wc->mod[card].fxo.pegtimer = 0;
++ wc->mod[card].fxo.peg = -1;
++ }
++ }
++ if (wc->mod[card].fxo.pegtimer > PEGTIME) {
++ /* Reset pegcount if our timer expires */
++ wc->mod[card].fxo.pegcount = 0;
++ }
++ /* Decrement debouncer if appropriate */
++ if (wc->mod[card].fxo.ringdebounce)
++ wc->mod[card].fxo.ringdebounce--;
++ if (!wc->mod[card].fxo.offhook && !wc->mod[card].fxo.ringdebounce) {
++ if (!wc->mod[card].fxo.ring && (wc->mod[card].fxo.pegcount > PEGCOUNT)) {
++ /* It's ringing */
++ if (debug)
++ printk(KERN_DEBUG "RING on %d/%d!\n", wc->span.spanno, card + 1);
++ if (!wc->mod[card].fxo.offhook)
++ dahdi_hooksig(&wc->chans[card], DAHDI_RXSIG_RING);
++ wc->mod[card].fxo.ring = 1;
++ }
++ if (wc->mod[card].fxo.ring && !wc->mod[card].fxo.pegcount) {
++ /* No more ring */
++ if (debug)
++ printk(KERN_DEBUG "NO RING on %d/%d!\n", wc->span.spanno, card + 1);
++ dahdi_hooksig(&wc->chans[card], DAHDI_RXSIG_OFFHOOK);
++ wc->mod[card].fxo.ring = 0;
++ }
++ }
++}
++#endif
++
++
++static inline void wctdm_receiveprep(struct wctdm *wc, unsigned char ints)
++{
++ volatile unsigned char *rxbuf;
++ int x, y, chan_offset;
++
++
++ //if(ints & 0x08/*0x04*/)
++ if(ints & 0x01)
++ /* Read is at interrupt address. Valid data is available at normal offset */
++ rxbuf = wc->readchunk;
++ else
++ rxbuf = wc->readchunk + DAHDI_CHUNKSIZE * (MAX_NUM_CARDS+NUM_FLAG);
++
++ for (x = 0; x < 4; x++) {
++ if (*(int*)(rxbuf+x*4) == WC_SYNCFLAG) {
++ break;
++ }
++ }
++
++ if(x==4)
++ {
++ printk("buffer sync misseed!\n");
++ wc->lastchan = -1;
++ return;
++ } else if(wc->lastchan != x) {
++ printk("buffer re-sync occur from %d to %d\n", wc->lastchan, x);
++ wc->lastchan = x;
++ }
++
++ if(watchdma) {
++ if( (x!=0) && (x!=3) ) {
++ printk("Bad re-sync %d, resetting...\n", x);
++ outb(0x0f, wc->ioaddr + WC_CNTL);
++ for(x=0; x<1000*1000*1000; x++);
++ outb(0x01, wc->ioaddr + WC_CNTL);
++ outb(0x01, wc->ioaddr + WC_OPER);
++ wc->lastchan=-1;
++ return;
++ }
++ }
++
++ chan_offset = (wc->lastchan*4 + 4 ) % (MAX_NUM_CARDS+NUM_FLAG);
++
++ for (x=0;x<DAHDI_CHUNKSIZE;x++) {
++#ifdef __BIG_ENDIAN
++ // operation pending...
++#else
++ for (y=0;y<wc->max_cards/*MAX_NUM_CARDS*/;y++) {
++ if (wc->cardflag & (1 << y))
++ wc->chans[y]->readchunk[x] = rxbuf[(MAX_NUM_CARDS+NUM_FLAG) * x + ((y + chan_offset ) & 0x0f)];
++#ifdef TEST_LOG_INCOME_VOICE
++ wc->voc_buf[y][wc->voc_ptr[y]] = rxbuf[(MAX_NUM_CARDS+NUM_FLAG) * x + ((y + chan_offset) & 0x0f)];
++ wc->voc_ptr[y]++;
++ if(wc->voc_ptr[y] >= voc_buffer_size)
++ wc->voc_ptr[y] = 0;
++#endif
++ }
++#endif
++ }
++
++ if(cidbeforering)
++ {
++ for(x=0; x<wc->max_cards; x++)
++ {
++ if (wc->modtype[wc->chans[x]->chanpos - 1] == MOD_TYPE_FXO)
++ if(wc->mod[wc->chans[x]->chanpos - 1].fxo.offhook == 0)
++ {
++ /*unsigned int *p_readchunk, *p_cid_history;
++
++ p_readchunk = (unsigned int*)wc->chans[x].readchunk;
++ p_cid_history = (unsigned int*)(wc->cid_history_buf[x] + wc->cid_history_ptr[x]);*/
++
++ if(wc->cid_state[x] == CID_STATE_IDLE) /* we need copy data to the cid voice buffer */
++ {
++ memcpy(wc->cid_history_buf[x] + wc->cid_history_ptr[x], wc->chans[x]->readchunk, DAHDI_CHUNKSIZE);
++ wc->cid_history_ptr[x] = (wc->cid_history_ptr[x] + DAHDI_CHUNKSIZE)%(cidbuflen * DAHDI_MAX_CHUNKSIZE);
++ }
++ else if (wc->cid_state[x] == CID_STATE_RING_ON)
++ wc->cid_history_clone_cnt[x] = cidbuflen;
++ else if (wc->cid_state[x] == CID_STATE_RING_OFF)
++ {
++ if(wc->cid_history_clone_cnt[x])
++ {
++ memcpy(wc->chans[x]->readchunk, wc->cid_history_buf[x] + wc->cid_history_ptr[x], DAHDI_MAX_CHUNKSIZE);
++ wc->cid_history_clone_cnt[x]--;
++ wc->cid_history_ptr[x] = (wc->cid_history_ptr[x] + DAHDI_MAX_CHUNKSIZE)%(cidbuflen * DAHDI_MAX_CHUNKSIZE);
++ }
++ else
++ {
++ wc->cid_state[x] = CID_STATE_WAIT_RING_FINISH;
++ wc->cid_history_clone_cnt[x] = cidtimeout; /* wait 6 sec, if no ring, return to idle */
++ }
++ }
++ else if(wc->cid_state[x] == CID_STATE_WAIT_RING_FINISH)
++ {
++ if(wc->cid_history_clone_cnt[x] > 0)
++ wc->cid_history_clone_cnt[x]--;
++ else
++ {
++ wc->cid_state[x] = CID_STATE_IDLE;
++ wc->cid_history_ptr[x] = 0;
++ wc->cid_history_clone_cnt[x] = 0;
++ }
++ }
++ }
++ }
++ }
++
++#ifdef AUDIO_RINGCHECK
++ for (x=0;x<wc->max_cards;x++)
++ ring_check(wc, x);
++#endif
++ /* XXX We're wasting 8 taps. We should get closer :( */
++ for (x = 0; x < wc->max_cards/*MAX_NUM_CARDS*/; x++) {
++ if (wc->cardflag & (1 << x))
++ dahdi_ec_chunk(wc->chans[x], wc->chans[x]->readchunk, wc->chans[x]->writechunk);
++ }
++
++ dahdi_receive(&wc->span);
++}
++
++static void wctdm_stop_dma(struct wctdm *wc);
++static void wctdm_reset_tdm(struct wctdm *wc);
++static void wctdm_restart_dma(struct wctdm *wc);
++
++
++static unsigned char __wctdm_getcreg(struct wctdm *wc, unsigned char reg);
++static void __wctdm_setcreg(struct wctdm *wc, unsigned char reg, unsigned char val);
++
++
++static inline void __write_8bits(struct wctdm *wc, unsigned char bits)
++{
++ if(spibyhw == 0)
++ {
++ int x;
++ /* Drop chip select */
++ wc->ios |= BIT_SCLK;
++ outb(wc->ios, wc->ioaddr + WC_AUXD);
++ wc->ios &= ~BIT_CS;
++ outb(wc->ios, wc->ioaddr + WC_AUXD);
++ for (x=0;x<8;x++) {
++ /* Send out each bit, MSB first, drop SCLK as we do so */
++ if (bits & 0x80)
++ wc->ios |= BIT_SDI;
++ else
++ wc->ios &= ~BIT_SDI;
++ wc->ios &= ~BIT_SCLK;
++ outb(wc->ios, wc->ioaddr + WC_AUXD);
++ /* Now raise SCLK high again and repeat */
++ wc->ios |= BIT_SCLK;
++ outb(wc->ios, wc->ioaddr + WC_AUXD);
++ bits <<= 1;
++ }
++ /* Finally raise CS back high again */
++ wc->ios |= BIT_CS;
++ outb(wc->ios, wc->ioaddr + WC_AUXD);
++ }
++ else
++ {
++ __wctdm_setcreg(wc, WC_SPIDATA, bits);
++ __wctdm_setcreg(wc, WC_SPICTRL, BIT_SPI_BYHW | BIT_SPI_START);
++ while ((__wctdm_getcreg(wc, WC_SPICTRL) & BIT_SPI_BUSY) != 0);
++ __wctdm_setcreg(wc, WC_SPICTRL, BIT_SPI_BYHW);
++ }
++}
++
++
++static inline void __reset_spi(struct wctdm *wc)
++{
++ __wctdm_setcreg(wc, WC_SPICTRL, 0);
++
++ /* Drop chip select and clock once and raise and clock once */
++ wc->ios |= BIT_SCLK;
++ outb(wc->ios, wc->ioaddr + WC_AUXD);
++ wc->ios &= ~BIT_CS;
++ outb(wc->ios, wc->ioaddr + WC_AUXD);
++ wc->ios |= BIT_SDI;
++ wc->ios &= ~BIT_SCLK;
++ outb(wc->ios, wc->ioaddr + WC_AUXD);
++ /* Now raise SCLK high again and repeat */
++ wc->ios |= BIT_SCLK;
++ outb(wc->ios, wc->ioaddr + WC_AUXD);
++ /* Finally raise CS back high again */
++ wc->ios |= BIT_CS;
++ outb(wc->ios, wc->ioaddr + WC_AUXD);
++ /* Clock again */
++ wc->ios &= ~BIT_SCLK;
++ outb(wc->ios, wc->ioaddr + WC_AUXD);
++ /* Now raise SCLK high again and repeat */
++ wc->ios |= BIT_SCLK;
++ outb(wc->ios, wc->ioaddr + WC_AUXD);
++
++ __wctdm_setcreg(wc, WC_SPICTRL, spibyhw);
++
++}
++
++static inline unsigned char __read_8bits(struct wctdm *wc)
++{
++ unsigned char res=0, c;
++ int x;
++ if(spibyhw == 0)
++ {
++ wc->ios &= ~BIT_CS;
++ outb(wc->ios, wc->ioaddr + WC_AUXD);
++ /* Drop chip select */
++ wc->ios &= ~BIT_CS;
++ outb(wc->ios, wc->ioaddr + WC_AUXD);
++ for (x=0;x<8;x++) {
++ res <<= 1;
++ /* Get SCLK */
++ wc->ios &= ~BIT_SCLK;
++ outb(wc->ios, wc->ioaddr + WC_AUXD);
++ /* Read back the value */
++ c = inb(wc->ioaddr + WC_AUXR);
++ if (c & BIT_SDO)
++ res |= 1;
++ /* Now raise SCLK high again */
++ wc->ios |= BIT_SCLK;
++ outb(wc->ios, wc->ioaddr + WC_AUXD);
++ }
++ /* Finally raise CS back high again */
++ wc->ios |= BIT_CS;
++ outb(wc->ios, wc->ioaddr + WC_AUXD);
++ wc->ios &= ~BIT_SCLK;
++ outb(wc->ios, wc->ioaddr + WC_AUXD);
++ }
++ else
++ {
++ __wctdm_setcreg(wc, WC_SPICTRL, BIT_SPI_BYHW | BIT_SPI_START);
++ while ((__wctdm_getcreg(wc, WC_SPICTRL) & BIT_SPI_BUSY) != 0);
++ res = __wctdm_getcreg(wc, WC_SPIDATA);
++ __wctdm_setcreg(wc, WC_SPICTRL, BIT_SPI_BYHW);
++ }
++
++ /* And return our result */
++ return res;
++}
++
++static void __wctdm_setcreg_mem(struct wctdm *wc, unsigned char reg, unsigned char val)
++{
++ unsigned int *p = (unsigned int*)(wc->mem32 + WC_REGBASE + ((reg & 0xf) << 2));
++ *p = val;
++}
++
++static unsigned char __wctdm_getcreg_mem(struct wctdm *wc, unsigned char reg)
++{
++ unsigned int *p = (unsigned int*)(wc->mem32 + WC_REGBASE + ((reg & 0xf) << 2));
++ return (*p)&0x00ff;
++}
++
++
++static void __wctdm_setcreg(struct wctdm *wc, unsigned char reg, unsigned char val)
++{
++ if(usememio)
++ __wctdm_setcreg_mem(wc, reg, val);
++ else
++ outb(val, wc->ioaddr + WC_REGBASE + ((reg & 0xf) << 2));
++}
++
++static unsigned char __wctdm_getcreg(struct wctdm *wc, unsigned char reg)
++{
++ if(usememio)
++ return __wctdm_getcreg_mem(wc, reg);
++ else
++ return inb(wc->ioaddr + WC_REGBASE + ((reg & 0xf) << 2));
++}
++
++static inline void __wctdm_setcard(struct wctdm *wc, int card)
++{
++ if (wc->curcard != card) {
++ __wctdm_setcreg(wc, WC_CS, card);
++ wc->curcard = card;
++ //printk("Select card %d\n", card);
++ }
++}
++
++static void __wctdm_setreg(struct wctdm *wc, int card, unsigned char reg, unsigned char value)
++{
++ __wctdm_setcard(wc, card);
++ if (wc->modtype[card] == MOD_TYPE_FXO) {
++ __write_8bits(wc, 0x20);
++ __write_8bits(wc, reg & 0x7f);
++ } else {
++ __write_8bits(wc, reg & 0x7f);
++ }
++ __write_8bits(wc, value);
++}
++
++static void wctdm_setreg(struct wctdm *wc, int card, unsigned char reg, unsigned char value)
++{
++ unsigned long flags;
++ spin_lock_irqsave(&wc->lock, flags);
++ __wctdm_setreg(wc, card, reg, value);
++ spin_unlock_irqrestore(&wc->lock, flags);
++}
++
++static unsigned char __wctdm_getreg(struct wctdm *wc, int card, unsigned char reg)
++{
++ __wctdm_setcard(wc, card);
++ if (wc->modtype[card] == MOD_TYPE_FXO) {
++ __write_8bits(wc, 0x60);
++ __write_8bits(wc, reg & 0x7f);
++ } else {
++ __write_8bits(wc, reg | 0x80);
++ }
++ return __read_8bits(wc);
++}
++
++static inline void reset_spi(struct wctdm *wc, int card)
++{
++ unsigned long flags;
++ spin_lock_irqsave(&wc->lock, flags);
++ __wctdm_setcard(wc, card);
++ __reset_spi(wc);
++ __reset_spi(wc);
++ spin_unlock_irqrestore(&wc->lock, flags);
++}
++
++static unsigned char wctdm_getreg(struct wctdm *wc, int card, unsigned char reg)
++{
++ unsigned long flags;
++ unsigned char res;
++ spin_lock_irqsave(&wc->lock, flags);
++ res = __wctdm_getreg(wc, card, reg);
++ spin_unlock_irqrestore(&wc->lock, flags);
++ return res;
++}
++
++static int __wait_access(struct wctdm *wc, int card)
++{
++ unsigned char data = 0;
++ long origjiffies;
++ int count = 0;
++
++ #define MAX 6000 /* attempts */
++
++
++ origjiffies = jiffies;
++ /* Wait for indirect access */
++ while (count++ < MAX)
++ {
++ data = __wctdm_getreg(wc, card, I_STATUS);
++
++ if (!data)
++ return 0;
++
++ }
++
++ if(count > (MAX-1)) printk(KERN_NOTICE " ##### Loop error (%02x) #####\n", data);
++
++ return 0;
++}
++
++static unsigned char translate_3215(unsigned char address)
++{
++ int x;
++ for (x=0;x<sizeof(indirect_regs)/sizeof(indirect_regs[0]);x++) {
++ if (indirect_regs[x].address == address) {
++ address = indirect_regs[x].altaddr;
++ break;
++ }
++ }
++ return address;
++}
++
++static int wctdm_proslic_setreg_indirect(struct wctdm *wc, int card, unsigned char address, unsigned short data)
++{
++ unsigned long flags;
++ int res = -1;
++ /* Translate 3215 addresses */
++ if (wc->flags[card] & FLAG_3215) {
++ address = translate_3215(address);
++ if (address == 255)
++ return 0;
++ }
++ spin_lock_irqsave(&wc->lock, flags);
++ if(!__wait_access(wc, card)) {
++ __wctdm_setreg(wc, card, IDA_LO,(unsigned char)(data & 0xFF));
++ __wctdm_setreg(wc, card, IDA_HI,(unsigned char)((data & 0xFF00)>>8));
++ __wctdm_setreg(wc, card, IAA,address);
++ res = 0;
++ };
++ spin_unlock_irqrestore(&wc->lock, flags);
++ return res;
++}
++
++static int wctdm_proslic_getreg_indirect(struct wctdm *wc, int card, unsigned char address)
++{
++ unsigned long flags;
++ int res = -1;
++ char *p=NULL;
++ /* Translate 3215 addresses */
++ if (wc->flags[card] & FLAG_3215) {
++ address = translate_3215(address);
++ if (address == 255)
++ return 0;
++ }
++ spin_lock_irqsave(&wc->lock, flags);
++ if (!__wait_access(wc, card)) {
++ __wctdm_setreg(wc, card, IAA, address);
++ if (!__wait_access(wc, card)) {
++ unsigned char data1, data2;
++ data1 = __wctdm_getreg(wc, card, IDA_LO);
++ data2 = __wctdm_getreg(wc, card, IDA_HI);
++ res = data1 | (data2 << 8);
++ } else
++ p = "Failed to wait inside\n";
++ } else
++ p = "failed to wait\n";
++ spin_unlock_irqrestore(&wc->lock, flags);
++ if (p)
++ printk(KERN_NOTICE "%s", p);
++ return res;
++}
++
++static int wctdm_proslic_init_indirect_regs(struct wctdm *wc, int card)
++{
++ unsigned char i;
++
++ for (i=0; i<sizeof(indirect_regs) / sizeof(indirect_regs[0]); i++)
++ {
++ if(wctdm_proslic_setreg_indirect(wc, card, indirect_regs[i].address,indirect_regs[i].initial))
++ return -1;
++ }
++
++ return 0;
++}
++
++static int wctdm_proslic_verify_indirect_regs(struct wctdm *wc, int card)
++{
++ int passed = 1;
++ unsigned short i, initial;
++ int j;
++
++ for (i=0; i<sizeof(indirect_regs) / sizeof(indirect_regs[0]); i++)
++ {
++ if((j = wctdm_proslic_getreg_indirect(wc, card, (unsigned char) indirect_regs[i].address)) < 0) {
++ printk(KERN_NOTICE "Failed to read indirect register %d\n", i);
++ return -1;
++ }
++ initial= indirect_regs[i].initial;
++
++ if ( j != initial && (!(wc->flags[card] & FLAG_3215) || (indirect_regs[i].altaddr != 255)))
++ {
++ printk(KERN_NOTICE "!!!!!!! %s iREG %X = %X should be %X\n",
++ indirect_regs[i].name,indirect_regs[i].address,j,initial );
++ passed = 0;
++ }
++ }
++
++ if (passed) {
++ if (debug)
++ printk(KERN_DEBUG "Init Indirect Registers completed successfully.\n");
++ } else {
++ printk(KERN_NOTICE " !!!!! Init Indirect Registers UNSUCCESSFULLY.\n");
++ return -1;
++ }
++ return 0;
++}
++
++static inline void wctdm_proslic_recheck_sanity(struct wctdm *wc, int card)
++{
++ int res;
++ /* Check loopback */
++ res = wc->reg1shadow[card];
++
++ if (!res && (res != wc->mod[card].fxs.lasttxhook)) // read real state from register By wx
++ res=wctdm_getreg(wc, card, 64);
++
++ if (!res && (res != wc->mod[card].fxs.lasttxhook)) {
++ res = wctdm_getreg(wc, card, 8);
++ if (res) {
++ printk(KERN_NOTICE "Ouch, part reset, quickly restoring reality (%d)\n", card);
++ wctdm_init_proslic(wc, card, 1, 0, 1);
++ } else {
++ if (wc->mod[card].fxs.palarms++ < MAX_ALARMS) {
++ printk(KERN_NOTICE "Power alarm on module %d, resetting!\n", card + 1);
++ if (wc->mod[card].fxs.lasttxhook == 4)
++ wc->mod[card].fxs.lasttxhook = 1;
++ wctdm_setreg(wc, card, 64, wc->mod[card].fxs.lasttxhook);
++ } else {
++ if (wc->mod[card].fxs.palarms == MAX_ALARMS)
++ printk(KERN_NOTICE "Too many power alarms on card %d, NOT resetting!\n", card + 1);
++ }
++ }
++ }
++}
++static inline void wctdm_voicedaa_check_hook(struct wctdm *wc, int card)
++{
++#define MS_PER_CHECK_HOOK 16
++
++#ifndef AUDIO_RINGCHECK
++ unsigned char res;
++#endif
++ signed char b;
++ int errors = 0;
++ struct fxo *fxo = &wc->mod[card].fxo;
++
++ /* Try to track issues that plague slot one FXO's */
++ b = wc->reg0shadow[card];
++ if ((b & 0x2) || !(b & 0x8)) {
++ /* Not good -- don't look at anything else */
++ if (debug)
++ printk(KERN_DEBUG "Error (%02x) on card %d!\n", b, card + 1);
++ errors++;
++ }
++ b &= 0x9b;
++ if (fxo->offhook) {
++ if (b != 0x9)
++ wctdm_setreg(wc, card, 5, 0x9);
++ } else {
++ if (b != 0x8)
++ wctdm_setreg(wc, card, 5, 0x8);
++ }
++ if (errors)
++ return;
++ if (!fxo->offhook) {
++ if(fixedtimepolarity) {
++ if ( wc->cid_state[card] == CID_STATE_RING_ON && wc->cid_ring_on_time[card]>0) {
++ if(wc->cid_ring_on_time[card]>=fixedtimepolarity) {
++ dahdi_qevent_lock(wc->chans[card], DAHDI_EVENT_POLARITY);
++ wc->cid_ring_on_time[card] = -1; /* the polarity already sent */
++ } else {
++ wc->cid_ring_on_time[card] += 16;
++ }
++ }
++ }
++ if (fwringdetect) {
++ res = wc->reg0shadow[card] & 0x60;
++ if (fxo->ringdebounce) {
++ --fxo->ringdebounce;
++ if (res && (res != fxo->lastrdtx) &&
++ (fxo->battery == BATTERY_PRESENT)) {
++ if (!fxo->wasringing) {
++ fxo->wasringing = 1;
++ if (debug) {
++ printk(KERN_DEBUG "RING on %d/%d!\n", wc->span.spanno, card + 1);
++ }
++ if(cidbeforering) {
++ if(wc->cid_state[card] == CID_STATE_IDLE) {
++ wc->cid_state[card] = CID_STATE_RING_ON;
++ wc->cid_ring_on_time[card] = 16; /* check every 16ms */
++ } else {
++ dahdi_hooksig(wc->chans[card], DAHDI_RXSIG_RING);
++ }
++ } else {
++ dahdi_hooksig(wc->chans[card], DAHDI_RXSIG_RING);
++ }
++ }
++ fxo->lastrdtx = res;
++ fxo->ringdebounce = 10;
++ } else if (!res) {
++ if ((fxo->ringdebounce == 0) && fxo->wasringing) {
++ fxo->wasringing = 0;
++ if (debug) {
++ printk(KERN_DEBUG "NO RING on %d/%d!\n", wc->span.spanno, card + 1);
++ }
++ if(cidbeforering) {
++ if(wc->cid_state[card] == CID_STATE_RING_ON) {
++ if(fixedtimepolarity==0) {
++ dahdi_qevent_lock(wc->chans[card], DAHDI_EVENT_POLARITY);
++ }
++ wc->cid_state[card] = CID_STATE_RING_OFF;
++ } else {
++ if(wc->cid_state[card] == CID_STATE_WAIT_RING_FINISH) {
++ wc->cid_history_clone_cnt[card] = cidtimeout;
++ }
++ dahdi_hooksig(wc->chans[card], DAHDI_RXSIG_OFFHOOK);
++ }
++ } else {
++ dahdi_hooksig(wc->chans[card], DAHDI_RXSIG_OFFHOOK);
++ }
++ }
++ }
++ } else if (res && (fxo->battery == BATTERY_PRESENT)) {
++ fxo->lastrdtx = res;
++ fxo->ringdebounce = 10;
++ }
++ } else {
++ res = wc->reg0shadow[card];
++ if ((res & 0x60) && (fxo->battery == BATTERY_PRESENT)) {
++ fxo->ringdebounce += (DAHDI_CHUNKSIZE * 16);
++ if (fxo->ringdebounce >= DAHDI_CHUNKSIZE * ringdebounce) {
++ if (!fxo->wasringing) {
++ fxo->wasringing = 1;
++ if(cidbeforering) {
++ if(wc->cid_state[card] == CID_STATE_IDLE) {
++ wc->cid_state[card] = CID_STATE_RING_ON;
++ wc->cid_ring_on_time[card] = 16; /* check every 16ms */
++ }
++ else {
++ dahdi_hooksig(wc->chans[card], DAHDI_RXSIG_RING);
++ }
++ } else {
++ dahdi_hooksig(wc->chans[card], DAHDI_RXSIG_RING);
++ }
++ if (debug) {
++ printk(KERN_DEBUG "RING on %d/%d!\n", wc->span.spanno, card + 1);
++ }
++ }
++ fxo->ringdebounce = DAHDI_CHUNKSIZE * ringdebounce;
++ }
++ } else {
++ fxo->ringdebounce -= DAHDI_CHUNKSIZE * 4;
++ if (fxo->ringdebounce <= 0) {
++ if (fxo->wasringing) {
++ fxo->wasringing = 0;
++ if(cidbeforering) {
++ if(wc->cid_state[card] == CID_STATE_RING_ON) {
++ if(fixedtimepolarity==0) {
++ dahdi_qevent_lock(wc->chans[card], DAHDI_EVENT_POLARITY);
++ }
++ wc->cid_state[card] = CID_STATE_RING_OFF;
++ } else {
++ if(wc->cid_state[card] == CID_STATE_WAIT_RING_FINISH) {
++ wc->cid_history_clone_cnt[card] = cidtimeout;
++ }
++ dahdi_hooksig(wc->chans[card], DAHDI_RXSIG_OFFHOOK);
++ }
++ } else {
++ dahdi_hooksig(wc->chans[card], DAHDI_RXSIG_OFFHOOK);
++ }
++ if (debug) {
++ printk(KERN_DEBUG "NO RING on %d/%d!\n", wc->span.spanno, card + 1);
++ }
++ }
++ fxo->ringdebounce = 0;
++ }
++ }
++ }
++ }
++
++ b = wc->reg1shadow[card];
++ if (abs(b) < battthresh) {
++ /* possible existing states:
++ battery lost, no debounce timer
++ battery lost, debounce timer (going to battery present)
++ battery present or unknown, no debounce timer
++ battery present or unknown, debounce timer (going to battery lost)
++ */
++
++ if (fxo->battery == BATTERY_LOST) {
++ if (fxo->battdebounce) {
++ /* we were going to BATTERY_PRESENT, but battery was lost again,
++ so clear the debounce timer */
++ fxo->battdebounce = 0;
++ }
++ } else {
++ if (fxo->battdebounce) {
++ /* going to BATTERY_LOST, see if we are there yet */
++ if (--fxo->battdebounce == 0) {
++ fxo->battery = BATTERY_LOST;
++ if (debug)
++ printk(KERN_DEBUG "NO BATTERY on %d/%d!\n", wc->span.spanno, card + 1);
++#ifdef JAPAN
++ if (!wc->ohdebounce && wc->offhook) {
++ dahdi_hooksig(&wc->chans[card], DAHDI_RXSIG_ONHOOK);
++ if (debug)
++ printk(KERN_DEBUG "Signalled On Hook\n");
++#ifdef ZERO_BATT_RING
++ wc->onhook++;
++#endif
++ }
++#else
++ dahdi_hooksig(wc->chans[card], DAHDI_RXSIG_ONHOOK);
++ /* set the alarm timer, taking into account that part of its time
++ period has already passed while debouncing occurred */
++ fxo->battalarm = (battalarm - battdebounce) / MS_PER_CHECK_HOOK;
++#endif
++ }
++ } else {
++ /* start the debounce timer to verify that battery has been lost */
++ fxo->battdebounce = battdebounce / MS_PER_CHECK_HOOK;
++ }
++ }
++ } else {
++ /* possible existing states:
++ battery lost or unknown, no debounce timer
++ battery lost or unknown, debounce timer (going to battery present)
++ battery present, no debounce timer
++ battery present, debounce timer (going to battery lost)
++ */
++
++ if (fxo->battery == BATTERY_PRESENT) {
++ if (fxo->battdebounce) {
++ /* we were going to BATTERY_LOST, but battery appeared again,
++ so clear the debounce timer */
++ fxo->battdebounce = 0;
++ }
++ } else {
++ if (fxo->battdebounce) {
++ /* going to BATTERY_PRESENT, see if we are there yet */
++ if (--fxo->battdebounce == 0) {
++ fxo->battery = BATTERY_PRESENT;
++ if (debug)
++ printk(KERN_DEBUG "BATTERY on %d/%d (%s)!\n", wc->span.spanno, card + 1,
++ (b < 0) ? "-" : "+");
++#ifdef ZERO_BATT_RING
++ if (wc->onhook) {
++ wc->onhook = 0;
++ dahdi_hooksig(&wc->chans[card], DAHDI_RXSIG_OFFHOOK);
++ if (debug)
++ printk(KERN_DEBUG "Signalled Off Hook\n");
++ }
++#else
++ dahdi_hooksig(wc->chans[card], DAHDI_RXSIG_OFFHOOK);
++#endif
++ /* set the alarm timer, taking into account that part of its time
++ period has already passed while debouncing occurred */
++ fxo->battalarm = (battalarm - battdebounce) / MS_PER_CHECK_HOOK;
++ }
++ } else {
++ /* start the debounce timer to verify that battery has appeared */
++ fxo->battdebounce = battdebounce / MS_PER_CHECK_HOOK;
++ }
++ }
++ }
++
++ if (fxo->lastpol >= 0) {
++ if (b < 0) {
++ fxo->lastpol = -1;
++ fxo->polaritydebounce = POLARITY_DEBOUNCE / MS_PER_CHECK_HOOK;
++ }
++ }
++ if (fxo->lastpol <= 0) {
++ if (b > 0) {
++ fxo->lastpol = 1;
++ fxo->polaritydebounce = POLARITY_DEBOUNCE / MS_PER_CHECK_HOOK;
++ }
++ }
++
++ if (fxo->battalarm) {
++ if (--fxo->battalarm == 0) {
++ /* the alarm timer has expired, so update the battery alarm state
++ for this channel */
++ dahdi_alarm_channel(wc->chans[card], fxo->battery == BATTERY_LOST ? DAHDI_ALARM_RED : DAHDI_ALARM_NONE);
++ }
++ }
++
++ if (fxo->polaritydebounce) {
++ if (--fxo->polaritydebounce == 0) {
++ if (fxo->lastpol != fxo->polarity) {
++ if (debug)
++ printk(KERN_DEBUG "%lu Polarity reversed (%d -> %d)\n", jiffies,
++ fxo->polarity,
++ fxo->lastpol);
++ if (fxo->polarity)
++ dahdi_qevent_lock(wc->chans[card], DAHDI_EVENT_POLARITY);
++ fxo->polarity = fxo->lastpol;
++ }
++ }
++ }
++#undef MS_PER_CHECK_HOOK
++}
++
++static inline void wctdm_proslic_check_hook(struct wctdm *wc, int card)
++{
++ char res;
++ int hook;
++
++ /* For some reason we have to debounce the
++ hook detector. */
++
++ res = wc->reg0shadow[card];
++ hook = (res & 1);
++ if (hook != wc->mod[card].fxs.lastrxhook) {
++ /* Reset the debounce (must be multiple of 4ms) */
++ wc->mod[card].fxs.debounce = dialdebounce * 4;
++
++#if 0
++ printk(KERN_DEBUG "Resetting debounce card %d hook %d, %d\n", card, hook, wc->mod[card].fxs.debounce);
++#endif
++ } else {
++ if (wc->mod[card].fxs.debounce > 0) {
++ wc->mod[card].fxs.debounce-= 16 * DAHDI_CHUNKSIZE;
++#if 0
++ printk(KERN_DEBUG "Sustaining hook %d, %d\n", hook, wc->mod[card].fxs.debounce);
++#endif
++ if (!wc->mod[card].fxs.debounce) {
++#if 0
++ printk(KERN_DEBUG "Counted down debounce, newhook: %d...\n", hook);
++#endif
++ wc->mod[card].fxs.debouncehook = hook;
++ }
++ if (!wc->mod[card].fxs.oldrxhook && wc->mod[card].fxs.debouncehook) {
++ /* Off hook */
++#if 1
++ if (debug)
++#endif
++ printk(KERN_DEBUG "opvxa1200: Card %d Going off hook\n", card);
++ dahdi_hooksig(wc->chans[card], DAHDI_RXSIG_OFFHOOK);
++ if (robust)
++ wctdm_init_proslic(wc, card, 1, 0, 1);
++ wc->mod[card].fxs.oldrxhook = 1;
++
++ } else if (wc->mod[card].fxs.oldrxhook && !wc->mod[card].fxs.debouncehook) {
++ /* On hook */
++#if 1
++ if (debug)
++#endif
++ printk(KERN_DEBUG "opvxa1200: Card %d Going on hook\n", card);
++ dahdi_hooksig(wc->chans[card], DAHDI_RXSIG_ONHOOK);
++ wc->mod[card].fxs.oldrxhook = 0;
++ }
++ }
++ }
++ wc->mod[card].fxs.lastrxhook = hook;
++}
++
++DAHDI_IRQ_HANDLER(wctdm_interrupt)
++{
++ struct wctdm *wc = dev_id;
++ unsigned char ints;
++ int x, y, z;
++ int mode;
++
++ ints = inb(wc->ioaddr + WC_INTSTAT);
++
++ if (!ints)
++ return IRQ_NONE;
++
++ outb(ints, wc->ioaddr + WC_INTSTAT);
++
++ if (ints & 0x10) {
++ /* Stop DMA, wait for watchdog */
++ printk(KERN_INFO "TDM PCI Master abort\n");
++ wctdm_stop_dma(wc);
++ return IRQ_RETVAL(1);
++ }
++
++ if (ints & 0x20) {
++ printk(KERN_INFO "PCI Target abort\n");
++ return IRQ_RETVAL(1);
++ }
++
++ for (x=0;x<wc->max_cards/*4*3*/;x++) {
++ if (wc->cardflag & (1 << x) &&
++ (wc->modtype[x] == MOD_TYPE_FXS)) {
++ if (wc->mod[x].fxs.lasttxhook == 0x4) {
++ /* RINGing, prepare for OHT */
++ wc->mod[x].fxs.ohttimer = OHT_TIMER << 3;
++ if (reversepolarity)
++ wc->mod[x].fxs.idletxhookstate = 0x6; /* OHT mode when idle */
++ else
++ wc->mod[x].fxs.idletxhookstate = 0x2;
++ } else {
++ if (wc->mod[x].fxs.ohttimer) {
++ wc->mod[x].fxs.ohttimer-= DAHDI_CHUNKSIZE;
++ if (!wc->mod[x].fxs.ohttimer) {
++ if (reversepolarity)
++ wc->mod[x].fxs.idletxhookstate = 0x5; /* Switch to active */
++ else
++ wc->mod[x].fxs.idletxhookstate = 0x1;
++ if ((wc->mod[x].fxs.lasttxhook == 0x2) || (wc->mod[x].fxs.lasttxhook == 0x6)) {
++ /* Apply the change if appropriate */
++ if (reversepolarity)
++ wc->mod[x].fxs.lasttxhook = 0x5;
++ else
++ wc->mod[x].fxs.lasttxhook = 0x1;
++ wctdm_setreg(wc, x, 64, wc->mod[x].fxs.lasttxhook);
++ }
++ }
++ }
++ }
++ }
++ }
++
++ if (ints & 0x0f) {
++ wc->intcount++;
++ z = wc->intcount & 0x3;
++ mode = wc->intcount & 0xc;
++ for(y=0; y<wc->max_cards/4/*3*/; y++)
++ {
++ x = z + y*4;
++ if (wc->cardflag & (1 << x ) )
++ {
++ switch(mode)
++ {
++ case 0:
++ /* Rest */
++ break;
++ case 4:
++ /* Read first shadow reg */
++ if (wc->modtype[x] == MOD_TYPE_FXS)
++ wc->reg0shadow[x] = wctdm_getreg(wc, x, 68);
++ else if (wc->modtype[x] == MOD_TYPE_FXO)
++ wc->reg0shadow[x] = wctdm_getreg(wc, x, 5);
++ break;
++ case 8:
++ /* Read second shadow reg */
++ if (wc->modtype[x] == MOD_TYPE_FXS)
++ wc->reg1shadow[x] = wctdm_getreg(wc, x, 64);
++ else if (wc->modtype[x] == MOD_TYPE_FXO)
++ wc->reg1shadow[x] = wctdm_getreg(wc, x, 29);
++ break;
++ case 12:
++ /* Perform processing */
++ if (wc->modtype[x] == MOD_TYPE_FXS) {
++ wctdm_proslic_check_hook(wc, x);
++ if (!(wc->intcount & 0xf0))
++ wctdm_proslic_recheck_sanity(wc, x);
++ } else if (wc->modtype[x] == MOD_TYPE_FXO) {
++ wctdm_voicedaa_check_hook(wc, x);
++ }
++ break;
++ }
++ }
++ }
++ if (!(wc->intcount % 10000)) {
++ /* Accept an alarm once per 10 seconds */
++ for (x=0;x<wc->max_cards/*4*3*/;x++)
++ if (wc->modtype[x] == MOD_TYPE_FXS) {
++ if (wc->mod[x].fxs.palarms)
++ wc->mod[x].fxs.palarms--;
++ }
++ }
++ wctdm_receiveprep(wc, ints);
++ wctdm_transmitprep(wc, ints);
++ }
++
++ return IRQ_RETVAL(1);
++
++}
++
++static int wctdm_voicedaa_insane(struct wctdm *wc, int card)
++{
++ int blah;
++ blah = wctdm_getreg(wc, card, 2);
++ if (blah != 0x3)
++ return -2;
++ blah = wctdm_getreg(wc, card, 11);
++ if (debug)
++ printk(KERN_DEBUG "VoiceDAA System: %02x\n", blah & 0xf);
++ return 0;
++}
++
++static int wctdm_proslic_insane(struct wctdm *wc, int card)
++{
++ int blah,insane_report;
++ insane_report=0;
++
++ blah = wctdm_getreg(wc, card, 0);
++ if (debug)
++ printk(KERN_DEBUG "ProSLIC on module %d, product %d, version %d\n", card, (blah & 0x30) >> 4, (blah & 0xf));
++
++#if 0
++ if ((blah & 0x30) >> 4) {
++ printk(KERN_DEBUG "ProSLIC on module %d is not a 3210.\n", card);
++ return -1;
++ }
++#endif
++ if (((blah & 0xf) == 0) || ((blah & 0xf) == 0xf)) {
++ /* SLIC not loaded */
++ return -1;
++ }
++ if ((blah & 0xf) < 2) {
++ printk(KERN_NOTICE "ProSLIC 3210 version %d is too old\n", blah & 0xf);
++ return -1;
++ }
++ if (wctdm_getreg(wc, card, 1) & 0x80)
++ /* ProSLIC 3215, not a 3210 */
++ wc->flags[card] |= FLAG_3215;
++
++ blah = wctdm_getreg(wc, card, 8);
++ if (blah != 0x2) {
++ printk(KERN_NOTICE "ProSLIC on module %d insane (1) %d should be 2\n", card, blah);
++ return -1;
++ } else if ( insane_report)
++ printk(KERN_NOTICE "ProSLIC on module %d Reg 8 Reads %d Expected is 0x2\n",card,blah);
++
++ blah = wctdm_getreg(wc, card, 64);
++ if (blah != 0x0) {
++ printk(KERN_NOTICE "ProSLIC on module %d insane (2)\n", card);
++ return -1;
++ } else if ( insane_report)
++ printk(KERN_NOTICE "ProSLIC on module %d Reg 64 Reads %d Expected is 0x0\n",card,blah);
++
++ blah = wctdm_getreg(wc, card, 11);
++ if (blah != 0x33) {
++ printk(KERN_NOTICE "ProSLIC on module %d insane (3)\n", card);
++ return -1;
++ } else if ( insane_report)
++ printk(KERN_NOTICE "ProSLIC on module %d Reg 11 Reads %d Expected is 0x33\n",card,blah);
++
++ /* Just be sure it's setup right. */
++ wctdm_setreg(wc, card, 30, 0);
++
++ if (debug)
++ printk(KERN_DEBUG "ProSLIC on module %d seems sane.\n", card);
++ return 0;
++}
++
++static int wctdm_proslic_powerleak_test(struct wctdm *wc, int card)
++{
++ unsigned long origjiffies;
++ unsigned char vbat;
++
++ /* Turn off linefeed */
++ wctdm_setreg(wc, card, 64, 0);
++
++ /* Power down */
++ wctdm_setreg(wc, card, 14, 0x10);
++
++ /* Wait for one second */
++ origjiffies = jiffies;
++
++ while((vbat = wctdm_getreg(wc, card, 82)) > 0x6) {
++ if ((jiffies - origjiffies) >= (HZ/2))
++ break;
++ }
++
++ if (vbat < 0x06) {
++ printk(KERN_NOTICE "Excessive leakage detected on module %d: %d volts (%02x) after %d ms\n", card,
++ 376 * vbat / 1000, vbat, (int)((jiffies - origjiffies) * 1000 / HZ));
++ return -1;
++ } else if (debug) {
++ printk(KERN_NOTICE "Post-leakage voltage: %d volts\n", 376 * vbat / 1000);
++ }
++ return 0;
++}
++
++static int wctdm_powerup_proslic(struct wctdm *wc, int card, int fast)
++{
++ unsigned char vbat;
++ unsigned long origjiffies;
++ int lim;
++
++ /* Set period of DC-DC converter to 1/64 khz */
++ wctdm_setreg(wc, card, 92, 0xff /* was 0xff */);
++
++ /* Wait for VBat to powerup */
++ origjiffies = jiffies;
++
++ /* Disable powerdown */
++ wctdm_setreg(wc, card, 14, 0);
++
++ /* If fast, don't bother checking anymore */
++ if (fast)
++ return 0;
++
++ while((vbat = wctdm_getreg(wc, card, 82)) < 0xc0) {
++ /* Wait no more than 500ms */
++ if ((jiffies - origjiffies) > HZ/2) {
++ break;
++ }
++ }
++
++ if (vbat < 0xc0) {
++ if (wc->proslic_power == PROSLIC_POWER_UNKNOWN)
++ 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",
++ card, (int)(((jiffies - origjiffies) * 1000 / HZ)),
++ vbat * 375);
++ wc->proslic_power = PROSLIC_POWER_WARNED;
++ return -1;
++ } else if (debug) {
++ printk(KERN_DEBUG "ProSLIC on module %d powered up to -%d volts (%02x) in %d ms\n",
++ card, vbat * 376 / 1000, vbat, (int)(((jiffies - origjiffies) * 1000 / HZ)));
++ }
++ wc->proslic_power = PROSLIC_POWER_ON;
++
++ /* Proslic max allowed loop current, reg 71 LOOP_I_LIMIT */
++ /* If out of range, just set it to the default value */
++ lim = (loopcurrent - 20) / 3;
++ if ( loopcurrent > 41 ) {
++ lim = 0;
++ if (debug)
++ printk(KERN_DEBUG "Loop current out of range! Setting to default 20mA!\n");
++ }
++ else if (debug)
++ printk(KERN_DEBUG "Loop current set to %dmA!\n",(lim*3)+20);
++ wctdm_setreg(wc,card,LOOP_I_LIMIT,lim);
++
++ /* Engage DC-DC converter */
++ wctdm_setreg(wc, card, 93, 0x19 /* was 0x19 */);
++#if 0
++ origjiffies = jiffies;
++ while(0x80 & wctdm_getreg(wc, card, 93)) {
++ if ((jiffies - origjiffies) > 2 * HZ) {
++ printk(KERN_DEBUG "Timeout waiting for DC-DC calibration on module %d\n", card);
++ return -1;
++ }
++ }
++
++#if 0
++ /* Wait a full two seconds */
++ while((jiffies - origjiffies) < 2 * HZ);
++
++ /* Just check to be sure */
++ vbat = wctdm_getreg(wc, card, 82);
++ printk(KERN_DEBUG "ProSLIC on module %d powered up to -%d volts (%02x) in %d ms\n",
++ card, vbat * 376 / 1000, vbat, (int)(((jiffies - origjiffies) * 1000 / HZ)));
++#endif
++#endif
++ return 0;
++
++}
++
++static int wctdm_proslic_manual_calibrate(struct wctdm *wc, int card){
++ unsigned long origjiffies;
++ unsigned char i;
++
++ wctdm_setreg(wc, card, 21, 0);//(0) Disable all interupts in DR21
++ wctdm_setreg(wc, card, 22, 0);//(0)Disable all interupts in DR21
++ wctdm_setreg(wc, card, 23, 0);//(0)Disable all interupts in DR21
++ wctdm_setreg(wc, card, 64, 0);//(0)
++
++ wctdm_setreg(wc, card, 97, 0x18); //(0x18)Calibrations without the ADC and DAC offset and without common mode calibration.
++ wctdm_setreg(wc, card, 96, 0x47); //(0x47) Calibrate common mode and differential DAC mode DAC + ILIM
++
++ origjiffies=jiffies;
++ while( wctdm_getreg(wc,card,96)!=0 ){
++ if((jiffies-origjiffies)>80)
++ return -1;
++ }
++//Initialized DR 98 and 99 to get consistant results.
++// 98 and 99 are the results registers and the search should have same intial conditions.
++
++/*******************************The following is the manual gain mismatch calibration****************************/
++/*******************************This is also available as a function *******************************************/
++ // Delay 10ms
++ origjiffies=jiffies;
++ while((jiffies-origjiffies)<1);
++ wctdm_proslic_setreg_indirect(wc, card, 88,0);
++ wctdm_proslic_setreg_indirect(wc,card,89,0);
++ wctdm_proslic_setreg_indirect(wc,card,90,0);
++ wctdm_proslic_setreg_indirect(wc,card,91,0);
++ wctdm_proslic_setreg_indirect(wc,card,92,0);
++ wctdm_proslic_setreg_indirect(wc,card,93,0);
++
++ wctdm_setreg(wc, card, 98,0x10); // This is necessary if the calibration occurs other than at reset time
++ wctdm_setreg(wc, card, 99,0x10);
++
++ for ( i=0x1f; i>0; i--)
++ {
++ wctdm_setreg(wc, card, 98,i);
++ origjiffies=jiffies;
++ while((jiffies-origjiffies)<4);
++ if((wctdm_getreg(wc,card,88)) == 0)
++ break;
++ } // for
++
++ for ( i=0x1f; i>0; i--)
++ {
++ wctdm_setreg(wc, card, 99,i);
++ origjiffies=jiffies;
++ while((jiffies-origjiffies)<4);
++ if((wctdm_getreg(wc,card,89)) == 0)
++ break;
++ }//for
++
++/*******************************The preceding is the manual gain mismatch calibration****************************/
++/**********************************The following is the longitudinal Balance Cal***********************************/
++ wctdm_setreg(wc,card,64,1);
++ while((jiffies-origjiffies)<10); // Sleep 100?
++
++ wctdm_setreg(wc, card, 64, 0);
++ wctdm_setreg(wc, card, 23, 0x4); // enable interrupt for the balance Cal
++ wctdm_setreg(wc, card, 97, 0x1); // this is a singular calibration bit for longitudinal calibration
++ wctdm_setreg(wc, card, 96,0x40);
++
++ wctdm_getreg(wc,card,96); /* Read Reg 96 just cause */
++
++ wctdm_setreg(wc, card, 21, 0xFF);
++ wctdm_setreg(wc, card, 22, 0xFF);
++ wctdm_setreg(wc, card, 23, 0xFF);
++
++ /**The preceding is the longitudinal Balance Cal***/
++ return(0);
++
++}
++#if 1
++static int wctdm_proslic_calibrate(struct wctdm *wc, int card)
++{
++ unsigned long origjiffies;
++ int x;
++ /* Perform all calibrations */
++ wctdm_setreg(wc, card, 97, 0x1f);
++
++ /* Begin, no speedup */
++ wctdm_setreg(wc, card, 96, 0x5f);
++
++ /* Wait for it to finish */
++ origjiffies = jiffies;
++ while(wctdm_getreg(wc, card, 96)) {
++ if ((jiffies - origjiffies) > 2 * HZ) {
++ printk(KERN_NOTICE "Timeout waiting for calibration of module %d\n", card);
++ return -1;
++ }
++ }
++
++ if (debug) {
++ /* Print calibration parameters */
++ printk(KERN_DEBUG "Calibration Vector Regs 98 - 107: \n");
++ for (x=98;x<108;x++) {
++ printk(KERN_DEBUG "%d: %02x\n", x, wctdm_getreg(wc, card, x));
++ }
++ }
++ return 0;
++}
++#endif
++
++static void wait_just_a_bit(int foo)
++{
++ long newjiffies;
++ newjiffies = jiffies + foo;
++ while(jiffies < newjiffies);
++}
++
++/*********************************************************************
++ * Set the hwgain on the analog modules
++ *
++ * card = the card position for this module (0-23)
++ * gain = gain in dB x10 (e.g. -3.5dB would be gain=-35)
++ * tx = (0 for rx; 1 for tx)
++ *
++ *******************************************************************/
++static int wctdm_set_hwgain(struct wctdm *wc, int card, __s32 gain, __u32 tx)
++{
++ if (!(wc->modtype[card] == MOD_TYPE_FXO)) {
++ printk(KERN_NOTICE "Cannot adjust gain. Unsupported module type!\n");
++ return -1;
++ }
++ if (tx) {
++ if (debug)
++ printk(KERN_DEBUG "setting FXO tx gain for card=%d to %d\n", card, gain);
++ if (gain >= -150 && gain <= 0) {
++ wctdm_setreg(wc, card, 38, 16 + (gain/-10));
++ wctdm_setreg(wc, card, 40, 16 + (-gain%10));
++ } else if (gain <= 120 && gain > 0) {
++ wctdm_setreg(wc, card, 38, gain/10);
++ wctdm_setreg(wc, card, 40, (gain%10));
++ } else {
++ printk(KERN_INFO "FXO tx gain is out of range (%d)\n", gain);
++ return -1;
++ }
++ } else { /* rx */
++ if (debug)
++ printk(KERN_DEBUG "setting FXO rx gain for card=%d to %d\n", card, gain);
++ if (gain >= -150 && gain <= 0) {
++ wctdm_setreg(wc, card, 39, 16+ (gain/-10));
++ wctdm_setreg(wc, card, 41, 16 + (-gain%10));
++ } else if (gain <= 120 && gain > 0) {
++ wctdm_setreg(wc, card, 39, gain/10);
++ wctdm_setreg(wc, card, 41, (gain%10));
++ } else {
++ printk(KERN_INFO "FXO rx gain is out of range (%d)\n", gain);
++ return -1;
++ }
++ }
++
++ return 0;
++}
++
++static int wctdm_init_voicedaa(struct wctdm *wc, int card, int fast, int manual, int sane)
++{
++ unsigned char reg16=0, reg26=0, reg30=0, reg31=0;
++ unsigned int tsi;
++ long newjiffies;
++ wc->modtype[card] = MOD_TYPE_FXO;
++ /* Sanity check the ProSLIC */
++ reset_spi(wc, card);
++ if (!sane && wctdm_voicedaa_insane(wc, card))
++ return -2;
++
++ /* Software reset */
++ wctdm_setreg(wc, card, 1, 0x80);
++
++ /* Wait just a bit */
++ wait_just_a_bit(HZ/10);
++
++ /* Enable PCM, ulaw */
++ if (alawoverride)
++ wctdm_setreg(wc, card, 33, 0x20);
++ else
++ wctdm_setreg(wc, card, 33, 0x28);
++
++ /* Set On-hook speed, Ringer impedence, and ringer threshold */
++ reg16 |= (fxo_modes[_opermode].ohs << 6);
++ reg16 |= (fxo_modes[_opermode].rz << 1);
++ reg16 |= (fxo_modes[_opermode].rt);
++ wctdm_setreg(wc, card, 16, reg16);
++
++ if(fwringdetect) {
++ /* Enable ring detector full-wave rectifier mode */
++ wctdm_setreg(wc, card, 18, 2);
++ wctdm_setreg(wc, card, 24, 0);
++ } else {
++ /* Set to the device defaults */
++ wctdm_setreg(wc, card, 18, 0);
++ wctdm_setreg(wc, card, 24, 0x19);
++ }
++
++ /* Set DC Termination:
++ Tip/Ring voltage adjust, minimum operational current, current limitation */
++ reg26 |= (fxo_modes[_opermode].dcv << 6);
++ reg26 |= (fxo_modes[_opermode].mini << 4);
++ reg26 |= (fxo_modes[_opermode].ilim << 1);
++ wctdm_setreg(wc, card, 26, reg26);
++
++ /* Set AC Impedence */
++ reg30 = (fxofullscale==1) ? (fxo_modes[_opermode].acim|0x10) : (fxo_modes[_opermode].acim);
++ wctdm_setreg(wc, card, 30, reg30);
++
++ /* Misc. DAA parameters */
++ if (fastpickup)
++ reg31 = 0xb3;
++ else
++ reg31 = 0xa3;
++
++ reg31 |= (fxo_modes[_opermode].ohs2 << 3);
++ wctdm_setreg(wc, card, 31, reg31);
++
++ if((wc->fwversion&0x0f)==6)
++ {
++ tsi = (3-(card%4))*8 + (card/4) *128;
++ wctdm_setreg(wc, card, 34, tsi&0xff);
++ wctdm_setreg(wc, card, 35, (tsi>>8)&0x3);
++ wctdm_setreg(wc, card, 36, (tsi+1)&0xff);
++ wctdm_setreg(wc, card, 37, ((tsi+1)>>8)&0x3);
++ }
++ else
++ {
++ /* Set Transmit/Receive timeslot */
++ //printk("set card %d to %d\n", card, (3-(card%4)) * 8 + (card/4) * 64);
++ wctdm_setreg(wc, card, 34, (3-(card%4)) * 8 + (card/4) * 64);
++ wctdm_setreg(wc, card, 35, 0x00);
++ wctdm_setreg(wc, card, 36, (3-(card%4)) * 8 + (card/4) * 64);
++ wctdm_setreg(wc, card, 37, 0x00);
++ }
++
++ /* Enable ISO-Cap */
++ wctdm_setreg(wc, card, 6, 0x00);
++
++ if (fastpickup)
++ wctdm_setreg(wc, card, 17, wctdm_getreg(wc, card, 17) | 0x20);
++
++ /* Wait 1000ms for ISO-cap to come up */
++ newjiffies = jiffies;
++ newjiffies += 2 * HZ;
++ while((jiffies < newjiffies) && !(wctdm_getreg(wc, card, 11) & 0xf0))
++ wait_just_a_bit(HZ/10);
++
++ if (!(wctdm_getreg(wc, card, 11) & 0xf0)) {
++ printk(KERN_NOTICE "VoiceDAA did not bring up ISO link properly!\n");
++ return -1;
++ }
++ if (debug)
++ printk(KERN_DEBUG "ISO-Cap is now up, line side: %02x rev %02x\n",
++ wctdm_getreg(wc, card, 11) >> 4,
++ (wctdm_getreg(wc, card, 13) >> 2) & 0xf);
++ /* Enable on-hook line monitor */
++ wctdm_setreg(wc, card, 5, 0x08);
++
++ /* Take values for fxotxgain and fxorxgain and apply them to module */
++ wctdm_set_hwgain(wc, card, fxotxgain, 1);
++ wctdm_set_hwgain(wc, card, fxorxgain, 0);
++
++ /* NZ -- crank the tx gain up by 7 dB */
++ if (!strcmp(fxo_modes[_opermode].name, "NEWZEALAND")) {
++ printk(KERN_INFO "Adjusting gain\n");
++ wctdm_set_hwgain(wc, card, 7, 1);
++ }
++
++ if(debug)
++ 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));
++
++ return 0;
++
++}
++
++static int wctdm_init_proslic(struct wctdm *wc, int card, int fast, int manual, int sane)
++{
++
++ unsigned short tmp[5];
++ unsigned char r19, r9;
++ int x, tsi;
++ int fxsmode=0;
++// int tmpcard;
++
++ /* Sanity check the ProSLIC */
++ if (!sane && wctdm_proslic_insane(wc, card))
++ return -2;
++
++ /* By default, don't send on hook */
++ if (reversepolarity)
++ wc->mod[card].fxs.idletxhookstate = 5;
++ else
++ wc->mod[card].fxs.idletxhookstate = 1;
++
++ if (sane) {
++ /* Make sure we turn off the DC->DC converter to prevent anything from blowing up */
++ wctdm_setreg(wc, card, 14, 0x10);
++ }
++
++ if (wctdm_proslic_init_indirect_regs(wc, card)) {
++ printk(KERN_INFO "Indirect Registers failed to initialize on module %d.\n", card);
++ return -1;
++ }
++
++ /* Clear scratch pad area */
++ wctdm_proslic_setreg_indirect(wc, card, 97,0);
++
++ /* Clear digital loopback */
++ wctdm_setreg(wc, card, 8, 0);
++
++ /* Revision C optimization */
++ wctdm_setreg(wc, card, 108, 0xeb);
++
++ /* Disable automatic VBat switching for safety to prevent
++ Q7 from accidently turning on and burning out. */
++ wctdm_setreg(wc, card, 67, 0x07); /* Note, if pulse dialing has problems at high REN loads
++ change this to 0x17 */
++
++ /* Turn off Q7 */
++ wctdm_setreg(wc, card, 66, 1);
++
++ /* Flush ProSLIC digital filters by setting to clear, while
++ saving old values */
++ for (x=0;x<5;x++) {
++ tmp[x] = wctdm_proslic_getreg_indirect(wc, card, x + 35);
++ wctdm_proslic_setreg_indirect(wc, card, x + 35, 0x8000);
++ }
++
++ /* Power up the DC-DC converter */
++ if (wctdm_powerup_proslic(wc, card, fast)) {
++ printk(KERN_NOTICE "Unable to do INITIAL ProSLIC powerup on module %d\n", card);
++ return -1;
++ }
++
++ if (!fast) {
++
++ /* Check for power leaks */
++ if (wctdm_proslic_powerleak_test(wc, card)) {
++ printk(KERN_NOTICE "ProSLIC module %d failed leakage test. Check for short circuit\n", card);
++ }
++ /* Power up again */
++ if (wctdm_powerup_proslic(wc, card, fast)) {
++ printk(KERN_NOTICE "Unable to do FINAL ProSLIC powerup on module %d\n", card);
++ return -1;
++ }
++#ifndef NO_CALIBRATION
++ /* Perform calibration */
++ if(manual) {
++ if (wctdm_proslic_manual_calibrate(wc, card)) {
++ //printk(KERN_NOTICE "Proslic failed on Manual Calibration\n");
++ if (wctdm_proslic_manual_calibrate(wc, card)) {
++ printk(KERN_NOTICE "Proslic Failed on Second Attempt to Calibrate Manually. (Try -DNO_CALIBRATION in Makefile)\n");
++ return -1;
++ }
++ printk(KERN_NOTICE "Proslic Passed Manual Calibration on Second Attempt\n");
++ }
++ }
++ else {
++ if(wctdm_proslic_calibrate(wc, card)) {
++ //printk(KERN_NOTICE "ProSlic died on Auto Calibration.\n");
++ if (wctdm_proslic_calibrate(wc, card)) {
++ printk(KERN_NOTICE "Proslic Failed on Second Attempt to Auto Calibrate\n");
++ return -1;
++ }
++ printk(KERN_NOTICE "Proslic Passed Auto Calibration on Second Attempt\n");
++ }
++ }
++ /* Perform DC-DC calibration */
++ wctdm_setreg(wc, card, 93, 0x99);
++ r19 = wctdm_getreg(wc, card, 107);
++ if ((r19 < 0x2) || (r19 > 0xd)) {
++ printk(KERN_NOTICE "DC-DC cal has a surprising direct 107 of 0x%02x!\n", r19);
++ wctdm_setreg(wc, card, 107, 0x8);
++ }
++
++ /* Save calibration vectors */
++ for (x=0;x<NUM_CAL_REGS;x++)
++ wc->mod[card].fxs.calregs.vals[x] = wctdm_getreg(wc, card, 96 + x);
++#endif
++
++ } else {
++ /* Restore calibration registers */
++ for (x=0;x<NUM_CAL_REGS;x++)
++ wctdm_setreg(wc, card, 96 + x, wc->mod[card].fxs.calregs.vals[x]);
++ }
++ /* Calibration complete, restore original values */
++ for (x=0;x<5;x++) {
++ wctdm_proslic_setreg_indirect(wc, card, x + 35, tmp[x]);
++ }
++
++ if (wctdm_proslic_verify_indirect_regs(wc, card)) {
++ printk(KERN_INFO "Indirect Registers failed verification.\n");
++ return -1;
++ }
++
++
++#if 0
++ /* Disable Auto Power Alarm Detect and other "features" */
++ wctdm_setreg(wc, card, 67, 0x0e);
++ blah = wctdm_getreg(wc, card, 67);
++#endif
++
++#if 0
++ if (wctdm_proslic_setreg_indirect(wc, card, 97, 0x0)) { // Stanley: for the bad recording fix
++ printk(KERN_INFO "ProSlic IndirectReg Died.\n");
++ return -1;
++ }
++#endif
++
++ if (alawoverride)
++ wctdm_setreg(wc, card, 1, 0x20);
++ else
++ wctdm_setreg(wc, card, 1, 0x28);
++ if((wc->fwversion&0x0f)==6)
++ {
++ tsi = (3-(card%4))* 8 + (card/4) *128;
++ wctdm_setreg(wc, card, 2, (tsi)&0xff); // Tx Start count low byte 0
++ wctdm_setreg(wc, card, 3, ((tsi)>>8)&0x3); // Tx Start count high byte 0
++ wctdm_setreg(wc, card, 4, (tsi+1)&0xff); // Rx Start count low byte 0
++ wctdm_setreg(wc, card, 5, ((tsi+1)>>8)&0x3); // Rx Start count high byte 0
++ }
++ else
++ {
++ // U-Law 8-bit interface
++ wctdm_setreg(wc, card, 2, (3-(card%4)) * 8 + (card/4) * 64); // Tx Start count low byte 0
++ wctdm_setreg(wc, card, 3, 0); // Tx Start count high byte 0
++ wctdm_setreg(wc, card, 4, (3-(card%4)) * 8 + (card/4) * 64); // Rx Start count low byte 0
++ wctdm_setreg(wc, card, 5, 0); // Rx Start count high byte 0
++ }
++ wctdm_setreg(wc, card, 18, 0xff); // clear all interrupt
++ wctdm_setreg(wc, card, 19, 0xff);
++ wctdm_setreg(wc, card, 20, 0xff);
++ wctdm_setreg(wc, card, 73, 0x04);
++ if (fxshonormode) {
++ fxsmode = acim2tiss[fxo_modes[_opermode].acim];
++ wctdm_setreg(wc, card, 10, 0x08 | fxsmode);
++ if (fxo_modes[_opermode].ring_osc)
++ wctdm_proslic_setreg_indirect(wc, card, 20, fxo_modes[_opermode].ring_osc);
++ if (fxo_modes[_opermode].ring_x)
++ wctdm_proslic_setreg_indirect(wc, card, 21, fxo_modes[_opermode].ring_x);
++ }
++ if (lowpower)
++ wctdm_setreg(wc, card, 72, 0x10);
++
++#if 0
++ wctdm_setreg(wc, card, 21, 0x00); // enable interrupt
++ wctdm_setreg(wc, card, 22, 0x02); // Loop detection interrupt
++ wctdm_setreg(wc, card, 23, 0x01); // DTMF detection interrupt
++#endif
++
++#if 0
++ /* Enable loopback */
++ wctdm_setreg(wc, card, 8, 0x2);
++ wctdm_setreg(wc, card, 14, 0x0);
++ wctdm_setreg(wc, card, 64, 0x0);
++ wctdm_setreg(wc, card, 1, 0x08);
++#endif
++
++ if (fastringer) {
++ /* Speed up Ringer */
++ wctdm_proslic_setreg_indirect(wc, card, 20, 0x7e6d);
++ wctdm_proslic_setreg_indirect(wc, card, 21, 0x01b9);
++ /* Beef up Ringing voltage to 89V */
++ if (boostringer) {
++ wctdm_setreg(wc, card, 74, 0x3f);
++ if (wctdm_proslic_setreg_indirect(wc, card, 21, 0x247))
++ return -1;
++ printk(KERN_INFO "Boosting fast ringer on slot %d (89V peak)\n", card + 1);
++ } else if (lowpower) {
++ if (wctdm_proslic_setreg_indirect(wc, card, 21, 0x14b))
++ return -1;
++ printk(KERN_INFO "Reducing fast ring power on slot %d (50V peak)\n", card + 1);
++ } else
++ printk(KERN_INFO "Speeding up ringer on slot %d (25Hz)\n", card + 1);
++ } else {
++ /* Beef up Ringing voltage to 89V */
++ if (boostringer) {
++ wctdm_setreg(wc, card, 74, 0x3f);
++ if (wctdm_proslic_setreg_indirect(wc, card, 21, 0x1d1))
++ return -1;
++ printk(KERN_INFO "Boosting ringer on slot %d (89V peak)\n", card + 1);
++ } else if (lowpower) {
++ if (wctdm_proslic_setreg_indirect(wc, card, 21, 0x108))
++ return -1;
++ printk(KERN_INFO "Reducing ring power on slot %d (50V peak)\n", card + 1);
++ }
++ }
++
++ if(fxstxgain || fxsrxgain) {
++ r9 = wctdm_getreg(wc, card, 9);
++ switch (fxstxgain) {
++
++ case 35:
++ r9+=8;
++ break;
++ case -35:
++ r9+=4;
++ break;
++ case 0:
++ break;
++ }
++
++ switch (fxsrxgain) {
++
++ case 35:
++ r9+=2;
++ break;
++ case -35:
++ r9+=1;
++ break;
++ case 0:
++ break;
++ }
++ wctdm_setreg(wc,card,9,r9);
++ }
++
++ if(debug)
++ 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"));
++
++ wctdm_setreg(wc, card, 64, 0x01);
++ return 0;
++}
++
++
++static int wctdm_ioctl(struct dahdi_chan *chan, unsigned int cmd, unsigned long data)
++{
++ struct wctdm_stats stats;
++ struct wctdm_regs regs;
++ struct wctdm_regop regop;
++ struct wctdm_echo_coefs echoregs;
++ struct dahdi_hwgain hwgain;
++ struct wctdm *wc = chan->pvt;
++ int x;
++ switch (cmd) {
++ case DAHDI_ONHOOKTRANSFER:
++ if (wc->modtype[chan->chanpos - 1] != MOD_TYPE_FXS)
++ return -EINVAL;
++ if (get_user(x, (__user int *)data))
++ return -EFAULT;
++ wc->mod[chan->chanpos - 1].fxs.ohttimer = x << 3;
++ if (reversepolarity)
++ wc->mod[chan->chanpos - 1].fxs.idletxhookstate = 0x6; /* OHT mode when idle */
++ else
++ wc->mod[chan->chanpos - 1].fxs.idletxhookstate = 0x2;
++ if (wc->mod[chan->chanpos - 1].fxs.lasttxhook == 0x1 || wc->mod[chan->chanpos - 1].fxs.lasttxhook == 0x5) {
++ /* Apply the change if appropriate */
++ if (reversepolarity)
++ wc->mod[chan->chanpos - 1].fxs.lasttxhook = 0x6;
++ else
++ wc->mod[chan->chanpos - 1].fxs.lasttxhook = 0x2;
++ wctdm_setreg(wc, chan->chanpos - 1, 64, wc->mod[chan->chanpos - 1].fxs.lasttxhook);
++ }
++ break;
++ case DAHDI_SETPOLARITY:
++ if (get_user(x, (__user int *)data))
++ return -EFAULT;
++ if (wc->modtype[chan->chanpos - 1] != MOD_TYPE_FXS)
++ return -EINVAL;
++ /* Can't change polarity while ringing or when open */
++ if ((wc->mod[chan->chanpos -1 ].fxs.lasttxhook == 0x04) ||
++ (wc->mod[chan->chanpos -1 ].fxs.lasttxhook == 0x00))
++ return -EINVAL;
++
++ if ((x && !reversepolarity) || (!x && reversepolarity))
++ wc->mod[chan->chanpos - 1].fxs.lasttxhook |= 0x04;
++ else
++ wc->mod[chan->chanpos - 1].fxs.lasttxhook &= ~0x04;
++ wctdm_setreg(wc, chan->chanpos - 1, 64, wc->mod[chan->chanpos - 1].fxs.lasttxhook);
++ break;
++ case WCTDM_GET_STATS:
++ if (wc->modtype[chan->chanpos - 1] == MOD_TYPE_FXS) {
++ stats.tipvolt = wctdm_getreg(wc, chan->chanpos - 1, 80) * -376;
++ stats.ringvolt = wctdm_getreg(wc, chan->chanpos - 1, 81) * -376;
++ stats.batvolt = wctdm_getreg(wc, chan->chanpos - 1, 82) * -376;
++ } else if (wc->modtype[chan->chanpos - 1] == MOD_TYPE_FXO) {
++ stats.tipvolt = (signed char)wctdm_getreg(wc, chan->chanpos - 1, 29) * 1000;
++ stats.ringvolt = (signed char)wctdm_getreg(wc, chan->chanpos - 1, 29) * 1000;
++ stats.batvolt = (signed char)wctdm_getreg(wc, chan->chanpos - 1, 29) * 1000;
++ } else
++ return -EINVAL;
++ if (copy_to_user((__user void *)data, &stats, sizeof(stats)))
++ return -EFAULT;
++ break;
++ case WCTDM_GET_REGS:
++ if (wc->modtype[chan->chanpos - 1] == MOD_TYPE_FXS) {
++ for (x=0;x<NUM_INDIRECT_REGS;x++)
++ regs.indirect[x] = wctdm_proslic_getreg_indirect(wc, chan->chanpos -1, x);
++ for (x=0;x<NUM_REGS;x++)
++ regs.direct[x] = wctdm_getreg(wc, chan->chanpos - 1, x);
++ } else {
++ memset(&regs, 0, sizeof(regs));
++ for (x=0;x<NUM_FXO_REGS;x++)
++ regs.direct[x] = wctdm_getreg(wc, chan->chanpos - 1, x);
++ }
++ if (copy_to_user((__user void *)data, &regs, sizeof(regs)))
++ return -EFAULT;
++ break;
++ case WCTDM_SET_REG:
++ if (copy_from_user(&regop, (__user void *)data, sizeof(regop)))
++ return -EFAULT;
++ if (regop.indirect) {
++ if (wc->modtype[chan->chanpos - 1] != MOD_TYPE_FXS)
++ return -EINVAL;
++ printk(KERN_INFO "Setting indirect %d to 0x%04x on %d\n", regop.reg, regop.val, chan->chanpos);
++ wctdm_proslic_setreg_indirect(wc, chan->chanpos - 1, regop.reg, regop.val);
++ } else {
++ regop.val &= 0xff;
++ printk(KERN_INFO "Setting direct %d to %04x on %d\n", regop.reg, regop.val, chan->chanpos);
++ wctdm_setreg(wc, chan->chanpos - 1, regop.reg, regop.val);
++ }
++ break;
++ case WCTDM_SET_ECHOTUNE:
++ printk(KERN_INFO "-- Setting echo registers: \n");
++ if (copy_from_user(&echoregs, (__user void *)data, sizeof(echoregs)))
++ return -EFAULT;
++
++ if (wc->modtype[chan->chanpos - 1] == MOD_TYPE_FXO) {
++ /* Set the ACIM register */
++ wctdm_setreg(wc, chan->chanpos - 1, 30, (fxofullscale==1) ? (echoregs.acim|0x10) : echoregs.acim);
++
++ /* Set the digital echo canceller registers */
++ wctdm_setreg(wc, chan->chanpos - 1, 45, echoregs.coef1);
++ wctdm_setreg(wc, chan->chanpos - 1, 46, echoregs.coef2);
++ wctdm_setreg(wc, chan->chanpos - 1, 47, echoregs.coef3);
++ wctdm_setreg(wc, chan->chanpos - 1, 48, echoregs.coef4);
++ wctdm_setreg(wc, chan->chanpos - 1, 49, echoregs.coef5);
++ wctdm_setreg(wc, chan->chanpos - 1, 50, echoregs.coef6);
++ wctdm_setreg(wc, chan->chanpos - 1, 51, echoregs.coef7);
++ wctdm_setreg(wc, chan->chanpos - 1, 52, echoregs.coef8);
++
++ printk(KERN_INFO "-- Set echo registers successfully\n");
++
++ break;
++ } else {
++ return -EINVAL;
++
++ }
++ break;
++ case DAHDI_SET_HWGAIN:
++ if (copy_from_user(&hwgain, (__user void *) data, sizeof(hwgain)))
++ return -EFAULT;
++
++ wctdm_set_hwgain(wc, chan->chanpos-1, hwgain.newgain, hwgain.tx);
++
++ if (debug)
++ printk(KERN_DEBUG "Setting hwgain on channel %d to %d for %s direction\n",
++ chan->chanpos-1, hwgain.newgain, hwgain.tx ? "tx" : "rx");
++ break;
++ default:
++ return -ENOTTY;
++ }
++ return 0;
++
++}
++
++static int wctdm_open(struct dahdi_chan *chan)
++{
++ struct wctdm *wc = chan->pvt;
++ if (!(wc->cardflag & (1 << (chan->chanpos - 1))))
++ return -ENODEV;
++ if (wc->dead)
++ return -ENODEV;
++ wc->usecount++;
++
++ /*MOD_INC_USE_COUNT; */
++ try_module_get(THIS_MODULE);
++ return 0;
++}
++
++static inline struct wctdm *wctdm_from_span(struct dahdi_span *span)
++{
++ return container_of(span, struct wctdm, span);
++}
++
++static int wctdm_watchdog(struct dahdi_span *span, int event)
++{
++ printk(KERN_INFO "opvxa1200: Restarting DMA\n");
++ wctdm_restart_dma(wctdm_from_span(span));
++ return 0;
++}
++
++static int wctdm_close(struct dahdi_chan *chan)
++{
++ struct wctdm *wc = chan->pvt;
++ wc->usecount--;
++
++ /*MOD_DEC_USE_COUNT;*/
++ module_put(THIS_MODULE);
++
++ if (wc->modtype[chan->chanpos - 1] == MOD_TYPE_FXS) {
++ if (reversepolarity)
++ wc->mod[chan->chanpos - 1].fxs.idletxhookstate = 5;
++ else
++ wc->mod[chan->chanpos - 1].fxs.idletxhookstate = 1;
++ }
++ /* If we're dead, release us now */
++ if (!wc->usecount && wc->dead)
++ wctdm_release(wc);
++ return 0;
++}
++
++static int wctdm_hooksig(struct dahdi_chan *chan, enum dahdi_txsig txsig)
++{
++ struct wctdm *wc = chan->pvt;
++ int reg=0;
++ if (wc->modtype[chan->chanpos - 1] == MOD_TYPE_FXO) {
++ /* XXX Enable hooksig for FXO XXX */
++ switch(txsig) {
++ case DAHDI_TXSIG_START:
++ case DAHDI_TXSIG_OFFHOOK:
++ wc->mod[chan->chanpos - 1].fxo.offhook = 1;
++ wctdm_setreg(wc, chan->chanpos - 1, 5, 0x9);
++ if(cidbeforering)
++ {
++ wc->cid_state[chan->chanpos - 1] = CID_STATE_IDLE;
++ wc->cid_history_clone_cnt[chan->chanpos - 1] = 0;
++ wc->cid_history_ptr[chan->chanpos - 1] = 0;
++ memset(wc->cid_history_buf[chan->chanpos - 1], DAHDI_LIN2X(0, chan), cidbuflen * DAHDI_MAX_CHUNKSIZE);
++ }
++ break;
++ case DAHDI_TXSIG_ONHOOK:
++ wc->mod[chan->chanpos - 1].fxo.offhook = 0;
++ wctdm_setreg(wc, chan->chanpos - 1, 5, 0x8);
++ break;
++ default:
++ printk(KERN_NOTICE "wcfxo: Can't set tx state to %d\n", txsig);
++ }
++ } else {
++ switch(txsig) {
++ case DAHDI_TXSIG_ONHOOK:
++ switch(chan->sig) {
++ case DAHDI_SIG_EM:
++ case DAHDI_SIG_FXOKS:
++ case DAHDI_SIG_FXOLS:
++ wc->mod[chan->chanpos-1].fxs.lasttxhook = wc->mod[chan->chanpos-1].fxs.idletxhookstate;
++ break;
++ case DAHDI_SIG_FXOGS:
++ wc->mod[chan->chanpos-1].fxs.lasttxhook = 3;
++ break;
++ }
++ break;
++ case DAHDI_TXSIG_OFFHOOK:
++ switch(chan->sig) {
++ case DAHDI_SIG_EM:
++ wc->mod[chan->chanpos-1].fxs.lasttxhook = 5;
++ break;
++ default:
++ wc->mod[chan->chanpos-1].fxs.lasttxhook = wc->mod[chan->chanpos-1].fxs.idletxhookstate;
++ break;
++ }
++ break;
++ case DAHDI_TXSIG_START:
++ wc->mod[chan->chanpos-1].fxs.lasttxhook = 4;
++ break;
++ case DAHDI_TXSIG_KEWL:
++ wc->mod[chan->chanpos-1].fxs.lasttxhook = 0;
++ break;
++ default:
++ printk(KERN_NOTICE "opvxa1200: Can't set tx state to %d\n", txsig);
++ }
++ if (debug)
++ printk(KERN_DEBUG "Setting FXS hook state to %d (%02x)\n", txsig, reg);
++
++#if 1
++ wctdm_setreg(wc, chan->chanpos - 1, 64, wc->mod[chan->chanpos-1].fxs.lasttxhook);
++#endif
++ }
++ return 0;
++}
++
++#ifdef DAHDI_SPAN_OPS
++static const struct dahdi_span_ops wctdm_span_ops = {
++ .owner = THIS_MODULE,
++ .hooksig = wctdm_hooksig,
++ .open = wctdm_open,
++ .close = wctdm_close,
++ .ioctl = wctdm_ioctl,
++ .watchdog = wctdm_watchdog,
++};
++#endif
++
++static int wctdm_initialize(struct wctdm *wc)
++{
++ int x;
++
++ wc->ddev = dahdi_create_device(); //Dennis
++ if (!wc->ddev)
++ return -ENOMEM;
++
++ /* Dahdi stuff */
++ sprintf(wc->span.name, "OPVXA1200/%d", wc->pos);
++ snprintf(wc->span.desc, sizeof(wc->span.desc)-1, "%s Board %d", wc->variety, wc->pos + 1);
++
++ wc->ddev->location = kasprintf(GFP_KERNEL, //Dennis
++ "PCI Bus %02d Slot %02d",
++ wc->dev->bus->number,
++ PCI_SLOT(wc->dev->devfn) + 1);
++ if (!wc->ddev->location) {
++ dahdi_free_device(wc->ddev);
++ wc->ddev = NULL;
++ return -ENOMEM;
++ }
++
++ wc->ddev->manufacturer = "OpenVox"; //Dennis
++ wc->ddev->devicetype = wc->variety;
++
++ if (alawoverride) {
++ printk(KERN_INFO "ALAW override parameter detected. Device will be operating in ALAW\n");
++ wc->span.deflaw = DAHDI_LAW_ALAW;
++ } else
++ wc->span.deflaw = DAHDI_LAW_MULAW;
++
++ x = __wctdm_getcreg(wc, WC_VER);
++ wc->fwversion = x;
++ if( x & FLAG_A800)
++ {
++ wc->card_name = A800P_Name;
++ wc->max_cards = 8;
++ }
++ else
++ {
++ wc->card_name = A1200P_Name;
++ wc->max_cards = 12;
++ }
++
++ for (x = 0; x < wc->max_cards/*MAX_NUM_CARDS*/; x++) {
++ sprintf(wc->chans[x]->name, "OPVXA1200/%d/%d", wc->pos, x);
++ wc->chans[x]->sigcap = DAHDI_SIG_FXOKS | DAHDI_SIG_FXOLS | DAHDI_SIG_FXOGS | DAHDI_SIG_SF | DAHDI_SIG_EM | DAHDI_SIG_CLEAR;
++ wc->chans[x]->sigcap |= DAHDI_SIG_FXSKS | DAHDI_SIG_FXSLS | DAHDI_SIG_SF | DAHDI_SIG_CLEAR;
++ wc->chans[x]->chanpos = x+1;
++ wc->chans[x]->pvt = wc;
++ }
++
++#ifdef DAHDI_SPAN_MODULE
++ wc->span.owner = THIS_MODULE;
++#endif
++
++#ifdef DAHDI_SPAN_OPS
++ wc->span.ops = &wctdm_span_ops;
++#else
++ wc->span.hooksig = wctdm_hooksig,
++ wc->span.watchdog = wctdm_watchdog,
++ wc->span.open = wctdm_open;
++ wc->span.close = wctdm_close;
++ wc->span.ioctl = wctdm_ioctl;
++ wc->span.pvt = wc;
++#endif
++ wc->span.chans = wc->chans;
++ wc->span.channels = wc->max_cards; /*MAX_NUM_CARDS;*/
++ wc->span.flags = DAHDI_FLAG_RBS;
++// init_waitqueue_head(&wc->span.maintq);
++ list_add_tail(&wc->span.device_node, &wc->ddev->spans);
++ if (dahdi_register_device(wc->ddev, &wc->dev->dev)) {
++ printk(KERN_NOTICE "Unable to register span with Dahdi\n");
++ kfree(wc->ddev->location);
++ dahdi_free_device(wc->ddev);
++ wc->ddev = NULL;
++ return -1;
++ }
++ return 0;
++}
++
++static void wctdm_post_initialize(struct wctdm *wc)
++{
++ int x;
++
++ /* Finalize signalling */
++ for (x = 0; x < wc->max_cards/*MAX_NUM_CARDS*/; x++) {
++ if (wc->cardflag & (1 << x)) {
++ if (wc->modtype[x] == MOD_TYPE_FXO)
++ wc->chans[x]->sigcap = DAHDI_SIG_FXSKS | DAHDI_SIG_FXSLS | DAHDI_SIG_SF | DAHDI_SIG_CLEAR;
++ else
++ wc->chans[x]->sigcap = DAHDI_SIG_FXOKS | DAHDI_SIG_FXOLS | DAHDI_SIG_FXOGS | DAHDI_SIG_SF | DAHDI_SIG_EM | DAHDI_SIG_CLEAR;
++ } else if (!(wc->chans[x]->sigcap & DAHDI_SIG_BROKEN)) {
++ wc->chans[x]->sigcap = 0;
++ }
++ }
++}
++
++static int wctdm_hardware_init(struct wctdm *wc)
++{
++ /* Hardware stuff */
++ unsigned char ver;
++ unsigned char x,y;
++ int failed;
++ long origjiffies; //ML.
++
++ /* Signal Reset */
++ printk("before raise reset\n");
++ outb(0x00, wc->ioaddr + WC_CNTL);
++ /* Wait for 1 second */
++ origjiffies = jiffies;
++ while(1)
++ {
++ if ((jiffies-origjiffies) >= (HZ))
++ break;;
++ }
++ outb(0x01, wc->ioaddr + WC_CNTL);
++ origjiffies = jiffies;
++ while(1)
++ {
++ if ((jiffies-origjiffies) >= (HZ/2))
++ break;;
++ }
++
++ /* printk(KERN_INFO "after raise reset\n");*/
++
++ /* Check OpenVox chip */
++ x=inb(wc->ioaddr + WC_CNTL);
++ ver = __wctdm_getcreg(wc, WC_VER);
++ wc->fwversion = ver;
++ /*if( ver & FLAG_A800)
++ {
++ wc->card_name = A800P_Name;
++ wc->max_cards = 8;
++ }
++ else
++ {
++ wc->card_name = A1200P_Name;
++ wc->max_cards = 12;
++ }*/
++ printk(KERN_NOTICE "OpenVox %s version: %01x.%01x\n", wc->card_name, (ver&(~FLAG_A800))>>4, ver&0x0f);
++
++ failed = 0;
++ if (ver != 0x00) {
++ for (x=0;x<16;x++) {
++ /* Test registers */
++ __wctdm_setcreg(wc, WC_CS, x);
++ y = __wctdm_getcreg(wc, WC_CS) & 0x0f;
++ if (x != y) {
++ printk(KERN_INFO "%02x != %02x\n", x, y);
++ failed++;
++ }
++ }
++
++ if (!failed) {
++ printk(KERN_INFO "OpenVox %s passed register test\n", wc->card_name);
++ } else {
++ printk(KERN_NOTICE "OpenVox %s failed register test\n", wc->card_name);
++ return -1;
++ }
++ } else {
++ printk(KERN_INFO "No OpenVox chip %02x\n", ver);
++ }
++
++ if (spibyhw)
++ __wctdm_setcreg(wc, WC_SPICTRL, BIT_SPI_BYHW); // spi controled by hw MiaoLin;
++ else
++ __wctdm_setcreg(wc, WC_SPICTRL, 0);
++
++ /* Reset PCI Interface chip and registers (and serial) */
++ outb(0x06, wc->ioaddr + WC_CNTL);
++ /* Setup our proper outputs for when we switch for our "serial" port */
++ wc->ios = BIT_CS | BIT_SCLK | BIT_SDI;
++
++ outb(wc->ios, wc->ioaddr + WC_AUXD);
++
++ /* Set all to outputs except AUX 5, which is an input */
++ outb(0xdf, wc->ioaddr + WC_AUXC);
++
++ /* Select alternate function for AUX0 */ /* Useless in OpenVox by MiaoLin. */
++ /* outb(0x4, wc->ioaddr + WC_AUXFUNC); */
++
++ /* Wait 1/4 of a sec */
++ wait_just_a_bit(HZ/4);
++
++ /* Back to normal, with automatic DMA wrap around */
++ outb(0x30 | 0x01, wc->ioaddr + WC_CNTL);
++ wc->ledstate = 0;
++ wctdm_set_led(wc, 0, 0);
++
++ /* Make sure serial port and DMA are out of reset */
++ outb(inb(wc->ioaddr + WC_CNTL) & 0xf9, wc->ioaddr + WC_CNTL);
++
++ /* Configure serial port for MSB->LSB operation */
++ outb(0xc1, wc->ioaddr + WC_SERCTL);
++
++ /* Delay FSC by 0 so it's properly aligned */
++ outb(0x01, wc->ioaddr + WC_FSCDELAY); /* Modify to 1 by MiaoLin */
++
++ /* Setup DMA Addresses */
++ outl(wc->writedma, wc->ioaddr + WC_DMAWS); /* Write start */
++ outl(wc->writedma + DAHDI_CHUNKSIZE * 4 * 4 - 4, wc->ioaddr + WC_DMAWI); /* Middle (interrupt) */
++ outl(wc->writedma + DAHDI_CHUNKSIZE * 8 * 4 - 4, wc->ioaddr + WC_DMAWE); /* End */
++
++ outl(wc->readdma, wc->ioaddr + WC_DMARS); /* Read start */
++ outl(wc->readdma + DAHDI_CHUNKSIZE * 4 * 4 - 4, wc->ioaddr + WC_DMARI); /* Middle (interrupt) */
++ outl(wc->readdma + DAHDI_CHUNKSIZE * 8 * 4 - 4, wc->ioaddr + WC_DMARE); /* End */
++
++ /* Clear interrupts */
++ outb(0xff, wc->ioaddr + WC_INTSTAT);
++
++ /* Wait 1/4 of a second more */
++ wait_just_a_bit(HZ/4);
++
++ for (x = 0; x < wc->max_cards/*MAX_NUM_CARDS*/; x++) {
++ int sane=0,ret=0,readi=0;
++#if 1
++ touch_softlockup_watchdog(); // avoid showing CPU softlock message
++ /* Init with Auto Calibration */
++ if (!(ret=wctdm_init_proslic(wc, x, 0, 0, sane))) {
++ wc->cardflag |= (1 << x);
++ if (debug) {
++ readi = wctdm_getreg(wc,x,LOOP_I_LIMIT);
++ printk("Proslic module %d loop current is %dmA\n",x,
++ ((readi*3)+20));
++ }
++ printk(KERN_INFO "Module %d: Installed -- AUTO FXS/DPO\n",x);
++ wctdm_set_led(wc, (unsigned int)x, 1);
++ } else {
++ if(ret!=-2) {
++ sane=1;
++
++ printk(KERN_INFO "Init ProSlic with Manual Calibration \n");
++ /* Init with Manual Calibration */
++ if (!wctdm_init_proslic(wc, x, 0, 1, sane)) {
++ wc->cardflag |= (1 << x);
++ if (debug) {
++ readi = wctdm_getreg(wc,x,LOOP_I_LIMIT);
++ printk("Proslic module %d loop current is %dmA\n",x,
++ ((readi*3)+20));
++ }
++ printk(KERN_INFO "Module %d: Installed -- MANUAL FXS\n",x);
++ } else {
++ printk(KERN_NOTICE "Module %d: FAILED FXS (%s)\n", x, fxshonormode ? fxo_modes[_opermode].name : "FCC");
++ wc->chans[x]->sigcap = __DAHDI_SIG_FXO | DAHDI_SIG_BROKEN;
++ }
++ } else if (!(ret = wctdm_init_voicedaa(wc, x, 0, 0, sane))) {
++ wc->cardflag |= (1 << x);
++ printk(KERN_INFO "Module %d: Installed -- AUTO FXO (%s mode)\n",x, fxo_modes[_opermode].name);
++ wctdm_set_led(wc, (unsigned int)x, 1);
++ } else
++ printk(KERN_NOTICE "Module %d: Not installed\n", x);
++ }
++#endif
++ }
++
++ /* Return error if nothing initialized okay. */
++ if (!wc->cardflag && !timingonly)
++ return -1;
++ /*__wctdm_setcreg(wc, WC_SYNC, (wc->cardflag << 1) | 0x1); */ /* removed by MiaoLin */
++ return 0;
++}
++
++static void wctdm_enable_interrupts(struct wctdm *wc)
++{
++ /* Clear interrupts */
++ outb(0xff, wc->ioaddr + WC_INTSTAT);
++
++ /* Enable interrupts (we care about all of them) */
++ //outb(0x3c, wc->ioaddr + WC_MASK0);
++ outb(0x33, wc->ioaddr + WC_MASK0);
++ /* No external interrupts */
++ outb(0x00, wc->ioaddr + WC_MASK1);
++}
++
++static void wctdm_restart_dma(struct wctdm *wc)
++{
++ /* Reset Master and TDM */
++ outb(0x01, wc->ioaddr + WC_CNTL);
++ outb(0x01, wc->ioaddr + WC_OPER);
++}
++
++static void wctdm_start_dma(struct wctdm *wc)
++{
++ /* Reset Master and TDM */
++ outb(0x0f, wc->ioaddr + WC_CNTL);
++ set_current_state(TASK_INTERRUPTIBLE);
++ schedule_timeout(1);
++ outb(0x01, wc->ioaddr + WC_CNTL);
++ outb(0x01, wc->ioaddr + WC_OPER);
++}
++
++static void wctdm_stop_dma(struct wctdm *wc)
++{
++ outb(0x00, wc->ioaddr + WC_OPER);
++}
++
++static void wctdm_reset_tdm(struct wctdm *wc)
++{
++ /* Reset TDM */
++ outb(0x0f, wc->ioaddr + WC_CNTL);
++}
++
++static void wctdm_disable_interrupts(struct wctdm *wc)
++{
++ outb(0x00, wc->ioaddr + WC_MASK0);
++ outb(0x00, wc->ioaddr + WC_MASK1);
++}
++
++static int __devinit wctdm_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
++{
++ int res;
++ struct wctdm *wc;
++ struct wctdm_desc *d = (struct wctdm_desc *)ent->driver_data;
++ int x;
++ int y;
++
++ static int initd_ifaces=0;
++
++ if(initd_ifaces){
++ memset((void *)ifaces,0,(sizeof(struct wctdm *))*WC_MAX_IFACES);
++ initd_ifaces=1;
++ }
++ for (x=0;x<WC_MAX_IFACES;x++)
++ if (!ifaces[x]) break;
++ if (x >= WC_MAX_IFACES) {
++ printk(KERN_NOTICE "Too many interfaces\n");
++ return -EIO;
++ }
++
++ if (pci_enable_device(pdev)) {
++ res = -EIO;
++ } else {
++ wc = kmalloc(sizeof(struct wctdm), GFP_KERNEL);
++ if (wc) {
++ int cardcount = 0;
++
++ wc->lastchan = -1; /* first channel offset = -1; */
++ wc->ledstate = 0;
++
++ ifaces[x] = wc;
++ memset(wc, 0, sizeof(struct wctdm));
++ for (x=0; x < sizeof(wc->chans)/sizeof(wc->chans[0]); ++x) {
++ wc->chans[x] = &wc->_chans[x];
++ }
++
++ spin_lock_init(&wc->lock);
++ wc->curcard = -1;
++ wc->ioaddr = pci_resource_start(pdev, 0);
++ wc->mem_region = pci_resource_start(pdev, 1);
++ wc->mem_len = pci_resource_len(pdev, 1);
++ wc->mem32 = (unsigned long)ioremap(wc->mem_region, wc->mem_len);
++ wc->dev = pdev;
++ wc->pos = x;
++ wc->variety = d->name;
++ for (y=0;y<MAX_NUM_CARDS;y++)
++ wc->flags[y] = d->flags;
++ /* Keep track of whether we need to free the region */
++ if (request_region(wc->ioaddr, 0xff, "opvxa1200"))
++ wc->freeregion = 1;
++ else
++ wc->freeregion = 0;
++
++ if (request_mem_region(wc->mem_region, wc->mem_len, "opvxa1200"))
++ wc->freeregion |= 0x02;
++
++ /* Allocate enough memory for two zt chunks, receive and transmit. Each sample uses
++ 8 bits. */
++ wc->writechunk = pci_alloc_consistent(pdev, DAHDI_MAX_CHUNKSIZE * (MAX_NUM_CARDS+NUM_FLAG) * 2 * 2, &wc->writedma);
++ if (!wc->writechunk) {
++ printk(KERN_NOTICE "opvxa1200: Unable to allocate DMA-able memory\n");
++ if (wc->freeregion & 0x01)
++ release_region(wc->ioaddr, 0xff);
++ if (wc->freeregion & 0x02)
++ {
++ release_mem_region(wc->mem_region, wc->mem_len);
++ iounmap((void *)wc->mem32);
++ }
++ return -ENOMEM;
++ }
++
++ wc->readchunk = wc->writechunk + DAHDI_MAX_CHUNKSIZE * (MAX_NUM_CARDS+NUM_FLAG) * 2; /* in bytes */
++ wc->readdma = wc->writedma + DAHDI_MAX_CHUNKSIZE * (MAX_NUM_CARDS+NUM_FLAG) * 2; /* in bytes */
++
++ if (wctdm_initialize(wc)) {
++ printk(KERN_NOTICE "opvxa1200: Unable to intialize FXS\n");
++ /* Set Reset Low */
++ x=inb(wc->ioaddr + WC_CNTL);
++ outb((~0x1)&x, wc->ioaddr + WC_CNTL);
++ /* Free Resources */
++ free_irq(pdev->irq, wc);
++ if (wc->freeregion & 0x01)
++ release_region(wc->ioaddr, 0xff);
++ if (wc->freeregion & 0x02)
++ {
++ release_mem_region(wc->mem_region, wc->mem_len);
++ iounmap((void *)wc->mem32);
++ }
++ }
++
++ /* Enable bus mastering */
++ pci_set_master(pdev);
++
++ /* Keep track of which device we are */
++ pci_set_drvdata(pdev, wc);
++
++
++ if (request_irq(pdev->irq, wctdm_interrupt, DAHDI_IRQ_SHARED, "opvxa1200", wc)) {
++ printk(KERN_NOTICE "opvxa1200: Unable to request IRQ %d\n", pdev->irq);
++ if (wc->freeregion & 0x01)
++ release_region(wc->ioaddr, 0xff);
++ if (wc->freeregion & 0x02)
++ {
++ release_mem_region(wc->mem_region, wc->mem_len);
++ iounmap((void *)wc->mem32);
++ }
++ pci_free_consistent(pdev, DAHDI_MAX_CHUNKSIZE * (MAX_NUM_CARDS+NUM_FLAG) * 2 * 2, (void *)wc->writechunk, wc->writedma);
++ pci_set_drvdata(pdev, NULL);
++ kfree(wc);
++ return -EIO;
++ }
++
++ if (wctdm_hardware_init(wc)) {
++ unsigned char w;
++
++ /* Set Reset Low */
++ w=inb(wc->ioaddr + WC_CNTL);
++ outb((~0x1)&w, wc->ioaddr + WC_CNTL);
++ /* Free Resources */
++ free_irq(pdev->irq, wc);
++ if (wc->freeregion & 0x01)
++ release_region(wc->ioaddr, 0xff);
++ if (wc->freeregion & 0x02)
++ {
++ release_mem_region(wc->mem_region, wc->mem_len);
++ iounmap((void *)wc->mem32);
++ }
++ pci_free_consistent(pdev, DAHDI_MAX_CHUNKSIZE * (MAX_NUM_CARDS+NUM_FLAG) * 2 * 2, (void *)wc->writechunk, wc->writedma);
++ pci_set_drvdata(pdev, NULL);
++ dahdi_unregister_device(wc->ddev);
++ kfree(wc->ddev->location);
++ dahdi_free_device(wc->ddev);
++ kfree(wc);
++ return -EIO;
++
++ }
++
++#ifdef TEST_LOG_INCOME_VOICE
++ for(x=0; x<MAX_NUM_CARDS+NUM_FLAG; x++)
++ {
++ wc->voc_buf[x] = kmalloc(voc_buffer_size, GFP_KERNEL);
++ wc->voc_ptr[x] = 0;
++ }
++#endif
++
++ if(cidbeforering)
++ {
++ int len = cidbuflen * DAHDI_MAX_CHUNKSIZE;
++ if(debug)
++ printk("cidbeforering support enabled, length is %d msec\n", cidbuflen);
++ for (x = 0; x < wc->max_cards/*MAX_NUM_CARDS*/; x++)
++ {
++ wc->cid_history_buf[x] = kmalloc(len, GFP_KERNEL);
++ wc->cid_history_ptr[x] = 0;
++ wc->cid_history_clone_cnt[x] = 0;
++ wc->cid_state[x] = CID_STATE_IDLE;
++ }
++ }
++
++ wctdm_post_initialize(wc);
++
++ /* Enable interrupts */
++ wctdm_enable_interrupts(wc);
++ /* Initialize Write/Buffers to all blank data */
++ memset((void *)wc->writechunk,0, DAHDI_MAX_CHUNKSIZE * (MAX_NUM_CARDS+NUM_FLAG) * 2 * 2);
++
++ /* Start DMA */
++ wctdm_start_dma(wc);
++
++ for (x = 0; x < wc->max_cards/*MAX_NUM_CARDS*/; x++) {
++ if (wc->cardflag & (1 << x))
++ cardcount++;
++ }
++
++ 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);
++ if(debug)
++ printk(KERN_DEBUG "OpenVox %s debug On\n", wc->card_name);
++
++ res = 0;
++ } else
++ res = -ENOMEM;
++ }
++ return res;
++}
++
++static void wctdm_release(struct wctdm *wc)
++{
++#ifdef TEST_LOG_INCOME_VOICE
++ struct file * f = NULL;
++ mm_segment_t orig_fs;
++ int i;
++ char fname[20];
++#endif
++
++ dahdi_unregister_device(wc->ddev); //Dennis
++ if (wc->freeregion & 0x01)
++ release_region(wc->ioaddr, 0xff);
++
++ if (wc->freeregion & 0x02)
++ {
++ release_mem_region(wc->mem_region, wc->mem_len);
++ iounmap((void *)wc->mem32);
++ }
++
++#ifdef TEST_LOG_INCOME_VOICE
++ for(i=0; i<MAX_NUM_CARDS + NUM_FLAG; i++)
++ {
++ sprintf(fname, "//usr//%d.pcm", i);
++ f = filp_open(fname, O_RDWR|O_CREAT, 00);
++
++ if (!f || !f->f_op || !f->f_op->read)
++ {
++ printk("WARNING: File (read) object is a null pointer!!!\n");
++ continue;
++ }
++
++ f->f_pos = 0;
++
++ orig_fs = get_fs();
++ set_fs(KERNEL_DS);
++
++ if(wc->voc_buf[i])
++ {
++ f->f_op->write(f, wc->voc_buf[i], voc_buffer_size, &f->f_pos);
++ kfree(wc->voc_buf[i]);
++ }
++
++ set_fs(orig_fs);
++ fput(f);
++ }
++#endif
++
++ if(cidbeforering)
++ {
++ int x;
++ for (x = 0; x < wc->max_cards/*MAX_NUM_CARDS*/; x++)
++ kfree(wc->cid_history_buf[x]);
++ }
++ kfree(wc->ddev->location); //Dennis
++ dahdi_free_device(wc->ddev);
++ kfree(wc);
++ printk(KERN_INFO "Free an OpenVox A1200 card\n");
++}
++
++static void __devexit wctdm_remove_one(struct pci_dev *pdev)
++{
++ struct wctdm *wc = pci_get_drvdata(pdev);
++ if (wc) {
++
++ /* Stop any DMA */
++ wctdm_stop_dma(wc);
++ wctdm_reset_tdm(wc);
++
++ /* In case hardware is still there */
++ wctdm_disable_interrupts(wc);
++
++ /* Immediately free resources */
++ pci_free_consistent(pdev, DAHDI_MAX_CHUNKSIZE * (MAX_NUM_CARDS+NUM_FLAG) * 2 * 2, (void *)wc->writechunk, wc->writedma);
++ free_irq(pdev->irq, wc);
++
++ /* Reset PCI chip and registers */
++ if(wc->fwversion > 0x11)
++ outb(0x0e, wc->ioaddr + WC_CNTL);
++ else
++ {
++ wc->ledstate = 0;
++ wctdm_set_led(wc,0,0); // power off all leds.
++ }
++
++ /* Release span, possibly delayed */
++ if (!wc->usecount)
++ wctdm_release(wc);
++ else
++ wc->dead = 1;
++ }
++}
++
++static struct pci_device_id wctdm_pci_tbl[] = {
++ { 0xe159, 0x0001, 0x9100, PCI_ANY_ID, 0, 0, (unsigned long) &wctdme },
++ { 0xe159, 0x0001, 0x9519, PCI_ANY_ID, 0, 0, (unsigned long) &wctdme },
++ { 0xe159, 0x0001, 0x95D9, PCI_ANY_ID, 0, 0, (unsigned long) &wctdme },
++ { 0xe159, 0x0001, 0x9500, PCI_ANY_ID, 0, 0, (unsigned long) &wctdme },
++ { 0xe159, 0x0001, 0x9532, PCI_ANY_ID, 0, 0, (unsigned long) &wctdme },
++ { 0xe159, 0x0001, 0x8519, PCI_ANY_ID, 0, 0, (unsigned long) &wctdme },
++ { 0xe159, 0x0001, 0x9559, PCI_ANY_ID, 0, 0, (unsigned long) &wctdme },
++ { 0xe159, 0x0001, 0x9599, PCI_ANY_ID, 0, 0, (unsigned long) &wctdme },
++ { 0 }
++};
++
++MODULE_DEVICE_TABLE(pci, wctdm_pci_tbl);
++
++static struct pci_driver wctdm_driver = {
++ .name = "opvxa1200",
++ .probe = wctdm_init_one,
++ .remove = __devexit_p(wctdm_remove_one),
++ .suspend = NULL,
++ .resume = NULL,
++ .id_table = wctdm_pci_tbl,
++};
++
++static int __init wctdm_init(void)
++{
++ int res;
++ int x;
++ for (x=0;x<(sizeof(fxo_modes) / sizeof(fxo_modes[0])); x++) {
++ if (!strcmp(fxo_modes[x].name, opermode))
++ break;
++ }
++ if (x < sizeof(fxo_modes) / sizeof(fxo_modes[0])) {
++ _opermode = x;
++ } else {
++ printk(KERN_NOTICE "Invalid/unknown operating mode '%s' specified. Please choose one of:\n", opermode);
++ for (x=0;x<sizeof(fxo_modes) / sizeof(fxo_modes[0]); x++)
++ printk(KERN_INFO " %s\n", fxo_modes[x].name);
++ printk(KERN_INFO "Note this option is CASE SENSITIVE!\n");
++ return -ENODEV;
++ }
++ if (!strcmp(fxo_modes[_opermode].name, "AUSTRALIA")) {
++ boostringer=1;
++ fxshonormode=1;
++}
++ if (battdebounce == 0) {
++ battdebounce = fxo_modes[_opermode].battdebounce;
++ }
++ if (battalarm == 0) {
++ battalarm = fxo_modes[_opermode].battalarm;
++ }
++ if (battthresh == 0) {
++ battthresh = fxo_modes[_opermode].battthresh;
++ }
++
++ res = dahdi_pci_module(&wctdm_driver);
++ if (res)
++ return -ENODEV;
++ return 0;
++}
++
++static void __exit wctdm_cleanup(void)
++{
++ pci_unregister_driver(&wctdm_driver);
++}
++
++module_param(debug, int, 0600);
++module_param(loopcurrent, int, 0600);
++module_param(reversepolarity, int, 0600);
++module_param(robust, int, 0600);
++module_param(opermode, charp, 0600);
++module_param(timingonly, int, 0600);
++module_param(lowpower, int, 0600);
++module_param(boostringer, int, 0600);
++module_param(fastringer, int, 0600);
++module_param(fxshonormode, int, 0600);
++module_param(battdebounce, uint, 0600);
++module_param(battthresh, uint, 0600);
++module_param(battalarm, uint, 0600);
++module_param(ringdebounce, int, 0600);
++module_param(dialdebounce, int, 0600);
++module_param(fwringdetect, int, 0600);
++module_param(alawoverride, int, 0600);
++module_param(fastpickup, int, 0600);
++module_param(fxotxgain, int, 0600);
++module_param(fxorxgain, int, 0600);
++module_param(fxstxgain, int, 0600);
++module_param(fxsrxgain, int, 0600);
++module_param(spibyhw, int, 0600);
++module_param(usememio, int, 0600);
++module_param(cidbeforering, int, 0600);
++module_param(cidbuflen, int, 0600);
++module_param(cidtimeout, int, 0600);
++module_param(fxofullscale, int, 0600);
++module_param(fixedtimepolarity, int, 0600);
++module_param(watchdma, int, 0600);
++
++MODULE_DESCRIPTION("OpenVox A1200 Driver");
++MODULE_AUTHOR("MiaoLin <miaolin@openvox.com.cn>");
++MODULE_LICENSE("GPL v2");
++
++module_init(wctdm_init);
++module_exit(wctdm_cleanup);
++
+--- dahdi-linux-2.10.0.1/drivers/dahdi/opvxa24xx/Kbuild 1970-01-01 01:00:00.000000000 +0100
++++ dahdi-linux-2.10.0.1-openvox/drivers/dahdi/opvxa24xx/Kbuild 2015-02-10 14:19:03.000000000 +0100
+@@ -0,0 +1,40 @@
++obj-$(DAHDI_BUILD_ALL)$(CONFIG_DAHDI_OPVXA24XX) += opvxa24xx.o
++
++FIRM_DIR := ../firmware
++
++EXTRA_CFLAGS += -I$(src)/.. -I$(src)/../oct612x/ $(shell $(src)/../oct612x/octasic-helper cflags $(src)/../oct612x) -Wno-undef
++
++DAHDI_KERNEL_H_NAME:=kernel.h
++DAHDI_KERNEL_H_PATH:=$(DAHDI_INCLUDE)/dahdi/$(DAHDI_KERNEL_H_NAME)
++ifneq ($(DAHDI_KERNEL_H_PATH),)
++ DAHDI_SPAN_MODULE:=$(shell if grep -C 5 "struct dahdi_span {" $(DAHDI_KERNEL_H_PATH) | grep -q "struct module \*owner"; then echo "yes"; else echo "no"; fi)
++ DAHDI_SPAN_OPS:=$(shell if grep -q "struct dahdi_span_ops {" $(DAHDI_KERNEL_H_PATH); then echo "yes"; else echo "no"; fi)
++ ifeq ($(DAHDI_SPAN_MODULE),yes)
++ EXTRA_CFLAGS+=-DDAHDI_SPAN_MODULE
++ else
++ ifeq ($(DAHDI_SPAN_OPS),yes)
++ EXTRA_CFLAGS+=-DDAHDI_SPAN_OPS
++ endif
++ endif
++endif
++
++ifeq ($(HOTPLUG_FIRMWARE),yes)
++ EXTRA_CFLAGS+=-DHOTPLUG_FIRMWARE
++endif
++
++opvxa24xx-objs := private.o a24xx.o si321x.o si3050.o ec3000.o callerid.o busydetect.o base.o $(shell $(src)/../oct612x/octasic-helper objects ../oct612x)
++
++ifneq ($(HOTPLUG_FIRMWARE),yes)
++opvxa24xx-objs += $(FIRM_DIR)/dahdi-fw-oct6114-032.o $(FIRM_DIR)/dahdi-fw-oct6114-064.o $(FIRM_DIR)/dahdi-fw-oct6114-128.o
++endif
++
++
++$(obj)/$(FIRM_DIR)/dahdi-fw-oct6114-032.o: $(obj)/base.o
++ $(MAKE) -C $(obj)/$(FIRM_DIR) dahdi-fw-oct6114-032.o
++
++$(obj)/$(FIRM_DIR)/dahdi-fw-oct6114-064.o: $(obj)/base.o
++ $(MAKE) -C $(obj)/$(FIRM_DIR) dahdi-fw-oct6114-064.o
++
++$(obj)/$(FIRM_DIR)/dahdi-fw-oct6114-128.o: $(obj)/base.o
++ $(MAKE) -C $(obj)/$(FIRM_DIR) dahdi-fw-oct6114-128.o
++
+--- dahdi-linux-2.10.0.1/drivers/dahdi/opvxa24xx/Makefile 1970-01-01 01:00:00.000000000 +0100
++++ dahdi-linux-2.10.0.1-openvox/drivers/dahdi/opvxa24xx/Makefile 2015-02-10 14:19:03.000000000 +0100
+@@ -0,0 +1,8 @@
++ifdef KBUILD_EXTMOD
++# We only get here on kernels 2.6.0-2.6.9 .
++# For newer kernels, Kbuild will be included directly by the kernel
++# build system.
++include $(src)/Kbuild
++
++else
++endif
+--- dahdi-linux-2.10.0.1/drivers/dahdi/opvxa24xx/a24xx.c 1970-01-01 01:00:00.000000000 +0100
++++ dahdi-linux-2.10.0.1-openvox/drivers/dahdi/opvxa24xx/a24xx.c 2015-02-10 14:19:03.000000000 +0100
+@@ -0,0 +1,286 @@
++/*
++ * OpenVox A24xx FXS/FXO Interface Driver for Zapata Telephony interface
++ *
++ * Written by MiaoLin<miaolin@openvox.cn>
++ * $Id: a24xx.c 185 2010-12-14 07:58:51Z yangshugang $
++
++ * Copyright (C) 2005-2010 OpenVox Communication Co. Ltd,
++ *
++ * All rights reserved.
++ *
++ * This program is free software; you can redistribute it and/or modify
++ * it under the terms of the GNU General Public License as published by
++ * the Free Software Foundation; either version 2 of the License, or
++ * (at your option) any later version.
++ *
++ * This program is distributed in the hope that it will be useful,
++ * but WITHOUT ANY WARRANTY; without even the implied warranty of
++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
++ * GNU General Public License for more details.
++ *
++ * You should have received a copy of the GNU General Public License
++ * along with this program; if not, write to the Free Software
++ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
++ *
++ */
++
++#include <linux/spinlock.h>
++#include <linux/jiffies.h>
++
++#include <dahdi/kernel.h>
++
++#include "base.h"
++#include "proslic.h"
++
++#define BIT_EC_PRESENT (1<<0)
++
++#define VPM_DEFAULT_DTMFTHRESHOLD 1000
++
++/* indirect_resg */
++static alpha indirect_regs[] =
++{
++{0,255,"DTMF_ROW_0_PEAK",0x55C2},
++{1,255,"DTMF_ROW_1_PEAK",0x51E6},
++{2,255,"DTMF_ROW2_PEAK",0x4B85},
++{3,255,"DTMF_ROW3_PEAK",0x4937},
++{4,255,"DTMF_COL1_PEAK",0x3333},
++{5,255,"DTMF_FWD_TWIST",0x0202},
++{6,255,"DTMF_RVS_TWIST",0x0202},
++{7,255,"DTMF_ROW_RATIO_TRES",0x0198},
++{8,255,"DTMF_COL_RATIO_TRES",0x0198},
++{9,255,"DTMF_ROW_2ND_ARM",0x0611},
++{10,255,"DTMF_COL_2ND_ARM",0x0202},
++{11,255,"DTMF_PWR_MIN_TRES",0x00E5},
++{12,255,"DTMF_OT_LIM_TRES",0x0A1C},
++{13,0,"OSC1_COEF",0x7B30},
++{14,1,"OSC1X",0x0063},
++{15,2,"OSC1Y",0x0000},
++{16,3,"OSC2_COEF",0x7870},
++{17,4,"OSC2X",0x007D},
++{18,5,"OSC2Y",0x0000},
++{19,6,"RING_V_OFF",0x0000},
++{20,7,"RING_OSC",0x7EF0},
++{21,8,"RING_X",0x0160},
++{22,9,"RING_Y",0x0000},
++{23,255,"PULSE_ENVEL",0x2000},
++{24,255,"PULSE_X",0x2000},
++{25,255,"PULSE_Y",0x0000},
++//{26,13,"RECV_DIGITAL_GAIN",0x4000}, // playback volume set lower
++{26,13,"RECV_DIGITAL_GAIN",0x2000}, // playback volume set lower
++{27,14,"XMIT_DIGITAL_GAIN",0x4000},
++//{27,14,"XMIT_DIGITAL_GAIN",0x2000},
++{28,15,"LOOP_CLOSE_TRES",0x1000},
++{29,16,"RING_TRIP_TRES",0x3600},
++{30,17,"COMMON_MIN_TRES",0x1000},
++{31,18,"COMMON_MAX_TRES",0x0200},
++{32,19,"PWR_ALARM_Q1Q2",0x0ff4},
++{33,20,"PWR_ALARM_Q3Q4",0x6e7e},
++{34,21,"PWR_ALARM_Q5Q6",0x0ff4},
++{35,22,"LOOP_CLOSURE_FILTER",0x8000},
++{36,23,"RING_TRIP_FILTER",0x0320},
++{37,24,"TERM_LP_POLE_Q1Q2",0x0012},
++{38,25,"TERM_LP_POLE_Q3Q4",0x0012},
++{39,26,"TERM_LP_POLE_Q5Q6",0x0012},
++{40,27,"CM_BIAS_RINGING",0x0C00},
++{41,64,"DCDC_MIN_V",0x0C00},
++{42,255,"DCDC_XTRA",0x1000},
++{43,66,"LOOP_CLOSE_TRES_LOW",0x1000},
++};
++
++void __a24xx_wait_just_a_bit(int foo)
++{
++ long newjiffies;
++ newjiffies = jiffies + foo;
++ while(jiffies < newjiffies);
++}
++
++void __a24xx_setcard(void *wc_dev, int card)
++{
++ struct a24xx_dev *dev = (struct a24xx_dev *)(wc_dev);
++ if (dev->curcard != card) {
++ __opvx_a24xx_setcard(dev->mem32, card);
++ dev->curcard = card;
++ }
++}
++
++inline void __a24xx_spi_setreg(struct a24xx_dev *wc_dev, int card, unsigned char reg, unsigned char value)
++{
++ __opvx_a24xx_spi_setreg((void *)wc_dev, wc_dev->mem32, card, wc_dev->modtype[card], reg, value, __a24xx_setcard);
++}
++
++inline unsigned char __a24xx_spi_getreg(struct a24xx_dev *wc_dev, int card, unsigned char reg)
++{
++ return __opvx_a24xx_spi_getreg((void *)wc_dev, wc_dev->mem32, card, wc_dev->modtype[card], reg, __a24xx_setcard);
++}
++
++int __a24xx_malloc_chunk(struct a24xx_dev *wc_dev,unsigned int frq)
++{
++ __opvx_a24xx_set_chunk(&(wc_dev->readchunk), &(wc_dev->writechunk),frq);
++ wc_dev->readdma = wc_dev->writedma + frq * DAHDI_MAX_CHUNKSIZE * (MAX_NUM_CARDS) * 2;
++
++ return 0;
++}
++
++static unsigned char __translate_3215(unsigned char address)
++{
++ int x;
++ for (x=0;x<sizeof(indirect_regs)/sizeof(indirect_regs[0]);x++) {
++ if (indirect_regs[x].address == address) {
++ address = indirect_regs[x].altaddr;
++ break;
++ }
++ }
++ return address;
++}
++
++int __a24xx_wait_access(struct a24xx_dev *wc_dev, int card)
++{
++ unsigned char data = 0;
++ long origjiffies;
++ int count = 0;
++
++ #define MAX 6000 /* attempts */
++
++ origjiffies = jiffies;
++ /* Wait for indirect access */
++ while (count++ < MAX) {
++ data = __a24xx_spi_getreg(wc_dev, card, I_STATUS);
++ if (!data) {
++ return 0;
++ }
++ }
++
++ if(count > (MAX-1)) {
++ printk(" ##### Loop error (%02x) #####\n", data);
++ }
++
++ return 0;
++}
++
++int __a24xx_proslic_setreg_indirect(struct a24xx_dev *wc_dev, int card, unsigned char address, unsigned short data)
++{
++ int res = -1;
++ /* Translate 3215 addresses */
++ if (wc_dev->flags[card] & FLAG_3215) {
++ address = __translate_3215(address);
++ if (address == 255) {
++ return 0;
++ }
++ }
++ if(!__a24xx_wait_access(wc_dev, card)) {
++ __a24xx_spi_setreg(wc_dev, card, IDA_LO,(unsigned char)(data & 0xFF));
++ __a24xx_spi_setreg(wc_dev, card, IDA_HI,(unsigned char)((data & 0xFF00)>>8));
++ __a24xx_spi_setreg(wc_dev, card, IAA,address);
++ res = 0;
++ }
++
++ return res;
++}
++
++int __a24xx_proslic_getreg_indirect(struct a24xx_dev *wc_dev, int card, unsigned char address)
++{
++ int res = -1;
++ char *p=NULL;
++ /* Translate 3215 addresses */
++ if (wc_dev->flags[card] & FLAG_3215) {
++ address = __translate_3215(address);
++ if (address == 255) {
++ return 0;
++ }
++ }
++ if (!__a24xx_wait_access(wc_dev, card)) {
++ __a24xx_spi_setreg(wc_dev, card, IAA, address);
++ if (!__a24xx_wait_access(wc_dev, card)) {
++ unsigned char data1, data2;
++ data1 = __a24xx_spi_getreg(wc_dev, card, IDA_LO);
++ data2 = __a24xx_spi_getreg(wc_dev, card, IDA_HI);
++ res = data1 | (data2 << 8);
++ } else {
++ p = "Failed to wait inside\n";
++ }
++ } else {
++ p = "failed to wait\n";
++ }
++ if (p) {
++ printk("%s\n", p);
++ }
++ return res;
++}
++
++void __a24xx_vpm_setpresent(struct a24xx_dev *wc_dev)
++{
++ wc_dev->vpm = BIT_EC_PRESENT;
++ if ( (wc_dev->fwversion&0xffff) > 0x3)
++ __opvx_a24xx_vpm_setpresent_v2(wc_dev->mem32);
++ else
++ __opvx_a24xx_vpm_setpresent(wc_dev->mem32);
++ printk("OpenVox VPM: Present and operational servicing %d span(s)\n", 1/*wc_dev->numspans*/);
++}
++
++void a24xx_spi_setreg(struct a24xx_dev *wc_dev, int card, unsigned char reg, unsigned char value)
++{
++ unsigned long flags;
++ spin_lock_irqsave(&wc_dev->lock, flags);
++ __a24xx_spi_setreg(wc_dev, card, reg, value);
++ spin_unlock_irqrestore(&wc_dev->lock, flags);
++}
++
++unsigned char a24xx_spi_getreg(struct a24xx_dev *wc_dev, int card, unsigned char reg)
++{
++ unsigned long flags;
++ unsigned char ret;
++ spin_lock_irqsave(&wc_dev->lock, flags);
++ ret = __a24xx_spi_getreg(wc_dev, card, reg);
++ spin_unlock_irqrestore(&wc_dev->lock, flags);
++ return ret;
++}
++
++static inline unsigned int a24xx_oct_in(struct a24xx_dev *wc_dev, const unsigned int addr)
++{
++ unsigned long flags;
++ unsigned int ret;
++
++ spin_lock_irqsave(&wc_dev->lock, flags);
++ if ( (wc_dev->fwversion&0xffff) > 0x3)
++ ret = __opvx_a24xx_oct_in_v2(wc_dev->mem32, addr);
++ else
++ ret = __opvx_a24xx_oct_in(wc_dev->mem32, addr);
++ spin_unlock_irqrestore(&wc_dev->lock, flags);
++
++ return ret;
++}
++
++static inline void a24xx_oct_out(struct a24xx_dev *wc_dev, const unsigned int addr, const unsigned int value)
++{
++ unsigned long flags;
++
++ spin_lock_irqsave(&wc_dev->lock, flags);
++ if ( (wc_dev->fwversion&0xffff) > 0x3)
++ __opvx_a24xx_oct_out_v2(wc_dev->mem32, addr, value);
++ else
++ __opvx_a24xx_oct_out(wc_dev->mem32, addr, value);
++ spin_unlock_irqrestore(&wc_dev->lock, flags);
++}
++
++void oct_set_reg(void *data, unsigned int reg, unsigned int val)
++{
++ struct a24xx_dev *wc_dev = data;
++ a24xx_oct_out(wc_dev, reg, val);
++}
++
++unsigned int oct_get_reg(void *data, unsigned int reg)
++{
++ struct a24xx_dev *wc_dev = data;
++ unsigned int ret;
++ ret = a24xx_oct_in(wc_dev, reg);
++ return ret;
++}
++
++void a24xx_reset_spi(struct a24xx_dev *wc_dev, int card)
++{
++ unsigned long flags;
++ spin_lock_irqsave(&wc_dev->lock, flags);
++ __opvx_a24xx_reset_spi(wc_dev, card, __a24xx_setcard);
++ spin_unlock_irqrestore(&wc_dev->lock, flags);
++}
++
+--- dahdi-linux-2.10.0.1/drivers/dahdi/opvxa24xx/base.c 1970-01-01 01:00:00.000000000 +0100
++++ dahdi-linux-2.10.0.1-openvox/drivers/dahdi/opvxa24xx/base.c 2015-02-10 14:19:03.000000000 +0100
+@@ -0,0 +1,2679 @@
++/*
++ * OpenVox A24xx FXS/FXO Interface Driver for Zapata Telephony interface
++ *
++ * Written by MiaoLin<miaolin@openvox.cn>
++ * $Id: base.c 359 2011-04-06 06:11:39Z yangshugang $
++
++ * Copyright (C) 2005-2010 OpenVox Communication Co. Ltd,
++ *
++ * All rights reserved.
++ *
++ * This program is free software; you can redistribute it and/or modify
++ * it under the terms of the GNU General Public License as published by
++ * the Free Software Foundation; either version 2 of the License, or
++ * (at your option) any later version.
++ *
++ * This program is distributed in the hope that it will be useful,
++ * but WITHOUT ANY WARRANTY; without even the implied warranty of
++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
++ * GNU General Public License for more details.
++ *
++ * You should have received a copy of the GNU General Public License
++ * along with this program; if not, write to the Free Software
++ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
++ *
++ */
++
++/* Rev histroy
++ *
++ * Rev 0.10 initial version, modified from opvxa1200.c
++ * Rev 0.20 add octasic echo canceller support.
++ *
++ */
++
++#include <linux/kernel.h>
++#include <linux/errno.h>
++#include <linux/module.h>
++#include <linux/init.h>
++#include <linux/errno.h>
++#include <linux/pci.h>
++#include <linux/interrupt.h>
++#include <asm/io.h>
++#include <linux/delay.h>
++#include <linux/moduleparam.h>
++#include <linux/sched.h>
++
++#ifdef TEST_LOG_INCOME_VOICE
++#include <linux/string.h>
++#include <asm/uaccess.h> /* get_fs(), set_fs(), KERNEL_DS */
++#include <linux/file.h> /* fput() */
++#endif
++
++#include <dahdi/kernel.h>
++#include <dahdi/wctdm_user.h>
++#include "fxo_modes.h"
++
++#include "proslic.h"
++#include "base.h"
++#include "ec3000.h"
++#include "busydetect.h"
++
++/* module parameters */
++int debug;
++int spi_cmd=0x223;
++
++int reversepolarity = 0;
++int alawoverride = 0;
++int fxotxgain = 0;
++int fxorxgain = 0;
++int fastpickup = 0;
++int fxofullscale = 0; /* fxo full scale tx/rx, register 30, acim */
++int fwringdetect = 0;
++int _opermode = 0;
++int cidsupport = 1; /* default support caller id analysis */
++int cidbuflen = 3000; /* in msec, default 3000 */
++int cidtimeout = 6*1000; /* in msec, default 6000 */
++int fxstxgain = 0;
++int fxsrxgain = 0;
++int fxshonormode = 0;
++int boostringer = 0;
++int fastringer = 0;
++int lowpower = 0;
++int loopcurrent = 20;
++int timingonly = 0;
++int fixedtimepolarity=0; /* time delay in ms when send polarity after rise edge of 1st ring.*/
++int ringdebounce = DEFAULT_RING_DEBOUNCE;
++unsigned int battdebounce;
++unsigned int battalarm;
++unsigned int battthresh;
++int robust = 0;
++#ifdef VPM_SUPPORT
++/* ec debug */
++int ec_debug = 0;
++int vpmsupport = 1;
++#endif
++
++#define MS_PER_HOOKCHECK (1)
++
++int ec_spans = 4;
++
++static char* A2410P_Name = "A2410P";
++static struct a24xx_desc a2410a = { "OpenVox A2410", 0 };
++static char* A1610P_Name = "A1610P";
++static struct a24xx_desc a1610a = { "OpenVox A1610", 0 };
++static char* A810P_Name = "A810P";
++static struct a24xx_desc a810a = { "OpenVox A810", 0 };
++static struct a24xx *ifaces[WC_MAX_IFACES];
++static char *opermode = "FCC";
++
++/* If set to auto, vpmdtmfsupport is enabled for VPM400M and disabled for VPM450M */
++static int vpmdtmfsupport = -1; /* -1=auto, 0=disabled, 1=enabled*/
++static unsigned int ms_per_irq = 1; // 1/2/4/8/16 allowed
++static unsigned int max_iface_index = 0;
++static unsigned int irq_stub = 0;
++
++#define POLARITY_XOR(card) ( \
++ (reversepolarity != 0) ^ (wc_dev->mod[(card)].fxs.reversepolarity != 0) ^ \
++ (wc_dev->mod[(card)].fxs.vmwi_lrev != 0) ^\
++ ((wc_dev->mod[(card)].fxs.vmwisetting.vmwi_type & DAHDI_VMWI_HVAC) != 0)\
++ )
++
++static const struct dahdi_echocan_features vpm_ec_features = {
++ .NLP_automatic = 1,
++ .CED_tx_detect = 1,
++ .CED_rx_detect = 1,
++};
++
++static void a24xx_release(struct a24xx *wc);
++static void a24xx_echocan_free(struct dahdi_chan *chan, struct dahdi_echocan_state *ec);
++
++static const struct dahdi_echocan_ops vpm_ec_ops = {
++// .name = "OPENVOX VPM",
++ .echocan_free = a24xx_echocan_free,
++};
++
++static int a24xx_echocan_create(struct dahdi_chan *chan, struct dahdi_echocanparams *ecp,
++ struct dahdi_echocanparam *p, struct dahdi_echocan_state **ec)
++{
++ struct a24xx *wc = chan->pvt;
++ struct a24xx_dev *wc_dev = &wc->dev;
++
++ int channel;
++ const struct dahdi_echocan_ops *ops;
++ const struct dahdi_echocan_features *features;
++
++
++ if (!wc_dev->vpm) {
++ return -ENODEV;
++ }
++
++ if (chan->span->offset >= ec_spans) {
++ return -ENODEV;
++ }
++
++ if (wc_dev->vpm_ec) {
++ ops = &vpm_ec_ops;
++ features = &vpm_ec_features;
++ }
++
++ if (ecp->param_count > 0) {
++ printk(KERN_WARNING "OpenVox VPM echo canceller does not support parameters; failing request\n");
++ return -EINVAL;
++ }
++
++ *ec = wc->ec[chan->chanpos - 1];
++ (*ec)->ops = ops;
++ (*ec)->features = *features;
++
++ channel = chan->chanpos;
++
++ if (wc_dev->vpm_ec) {
++ channel -= 1;
++ if(ec_debug) {
++ printk(KERN_DEBUG "echocan: Card is %d, Channel is %d, offset is %d, length %d\n",
++ wc_dev->pos, chan->chanpos, channel, ecp->tap_length);
++ }
++ opvx_vpm_setec(wc_dev->vpm_ec, channel, ecp->tap_length);
++ msleep(10);
++ }
++
++ return 0;
++}
++
++static void a24xx_echocan_free(struct dahdi_chan *chan, struct dahdi_echocan_state *ec)
++{
++ struct a24xx *wc = chan->pvt;
++ struct a24xx_dev *wc_dev = &wc->dev;
++ int channel;
++
++ memset(ec, 0, sizeof(*ec));
++
++ channel = chan->chanpos;
++
++ if (wc_dev->vpm_ec) {
++ channel -= 1;
++ if (ec_debug)
++ printk(KERN_DEBUG "echocan: Card is %d, Channel is %d, Span is %d, offset is %d length 0\n",
++ wc_dev->pos, chan->chanpos, chan->span->offset, channel);
++ opvx_vpm_setec(wc_dev->vpm_ec, channel, 0);
++ }
++}
++
++static void free_wc(struct a24xx *wc)
++{
++ int i;
++ struct a24xx_dev *wc_dev = &wc->dev;
++
++ for (i = 0; i < wc_dev->max_cards; i++) {
++ if (wc->ec[i]) {
++ kfree(wc->ec[i]);
++ wc->ec[i] = NULL;
++ }
++ }
++ kfree(wc);
++ wc = NULL;
++}
++
++static void a24xx_vpm_init(struct a24xx *wc)
++{
++ int x;
++ struct a24xx_dev *wc_dev = &wc->dev;
++ int laws[4] = { 0, };
++ int res;
++
++ unsigned int vpm_capacity;
++ struct firmware embedded_firmware;
++ const struct firmware *firmware = &embedded_firmware;
++
++#if !defined(HOTPLUG_FIRMWARE)
++ extern void _binary_dahdi_fw_oct6114_032_bin_size;
++ extern void _binary_dahdi_fw_oct6114_064_bin_size;
++ extern void _binary_dahdi_fw_oct6114_128_bin_size;
++ extern u8 _binary_dahdi_fw_oct6114_032_bin_start[];
++ extern u8 _binary_dahdi_fw_oct6114_064_bin_start[];
++ extern u8 _binary_dahdi_fw_oct6114_128_bin_start[];
++#else
++ static const char oct032_firmware[] = "dahdi-fw-oct6114-032.bin";
++ static const char oct064_firmware[] = "dahdi-fw-oct6114-064.bin";
++ static const char oct128_firmware[] = "dahdi-fw-oct6114-128.bin";
++#endif
++
++ if ( (wc_dev->fwversion&0xffff) > 0x3)
++ res = __opvx_a24xx_check_vpm_v2(wc_dev->mem32);
++ else
++ res = __opvx_a24xx_check_vpm(wc_dev->mem32);
++
++ if (res < 0) {
++ if (-1 == res) {
++ printk("OpenVox VPM: Support Disabled\n");
++ } else if (-2 == res) {
++ printk("OpenVox VPM: Not Present\n");
++ }
++ return;
++ }
++
++ /* Setup alaw vs ulaw rules */
++ for (x = 0;x < 1; x++) {
++ if(wc->span.deflaw == DAHDI_LAW_ALAW) {
++ laws[x] = 1;
++ }
++ else {
++ laws[x] = 0;
++ }
++ }
++
++ vpm_capacity = opvx_vpm_getcapacity(wc_dev);
++ printk("OpenVox VPM: echo cancellation supports %d channels\n", vpm_capacity);
++
++ switch (vpm_capacity) {
++ case 32:
++#if defined(HOTPLUG_FIRMWARE)
++ if ((request_firmware(&firmware, oct032_firmware, &wc_dev->dev->dev) != 0) ||
++ !firmware) {
++ printk("OpenVox VPM: firmware %s not available from userspace\n", oct032_firmware);
++ return;
++ }
++#else
++ embedded_firmware.data = _binary_dahdi_fw_oct6114_032_bin_start;
++ /* Yes... this is weird. objcopy gives us a symbol containing
++ the size of the firmware, not a pointer a variable containing
++ the size. The only way we can get the value of the symbol
++ is to take its address, so we define it as a pointer and
++ then cast that value to the proper type.
++ */
++ embedded_firmware.size = (size_t) &_binary_zaptel_fw_oct6114_032_bin_size;
++#endif
++ break;
++ case 64:
++#if defined(HOTPLUG_FIRMWARE)
++ if ((request_firmware(&firmware, oct064_firmware, &wc_dev->dev->dev) != 0) ||
++ !firmware) {
++ printk("OpenVox VPM: firmware %s not available from userspace\n", oct064_firmware);
++ return;
++ }
++#else
++ embedded_firmware.data = _binary_dahdi_fw_oct6114_064_bin_start;
++ /* Yes... this is weird. objcopy gives us a symbol containing
++ the size of the firmware, not a pointer a variable containing
++ the size. The only way we can get the value of the symbol
++ is to take its address, so we define it as a pointer and
++ then cast that value to the proper type.
++ */
++ embedded_firmware.size = (size_t) &_binary_dahdi_fw_oct6114_064_bin_size;
++#endif
++ break;
++ case 128:
++#if defined(HOTPLUG_FIRMWARE)
++ if ((request_firmware(&firmware, oct128_firmware, &wc_dev->dev->dev) != 0) ||
++ !firmware) {
++ printk("OpenVox VPM: firmware %s not available from userspace\n", oct128_firmware);
++ return;
++ }
++#else
++ embedded_firmware.data = _binary_dahdi_fw_oct6114_128_bin_start;
++ /* Yes... this is weird. objcopy gives us a symbol containing
++ the size of the firmware, not a pointer a variable containing
++ the size. The only way we can get the value of the symbol
++ is to take its address, so we define it as a pointer and
++ then cast that value to the proper type.
++ */
++ embedded_firmware.size = (size_t) &_binary_dahdi_fw_oct6114_128_bin_size;
++#endif
++ break;
++ default:
++ printk("Unsupported channel capacity found on VPM module (%d).\n", vpm_capacity);
++ return;
++ }
++
++ if (!(wc_dev->vpm_ec = opvx_vpm_init(wc_dev, laws, /*wc_dev->numspans*/1, firmware))) {
++ printk("OpenVox VPM: Failed to initialize\n");
++ if (firmware != &embedded_firmware) {
++ release_firmware(firmware);
++ }
++ return;
++ }
++
++ if (firmware != &embedded_firmware) {
++ release_firmware(firmware);
++ }
++
++ if (vpmdtmfsupport == -1) {
++ printk("OpenVox VPM: hardware DTMF disabled.\n");
++ vpmdtmfsupport = 0;
++ }
++
++ __a24xx_vpm_setpresent(wc_dev);
++
++}
++
++#ifdef AUDIO_RINGCHECK
++static inline void ring_check(struct a24xx *wc, int card)
++{
++ int x;
++ short sample;
++ struct a24xx_dev *wc_dev = &wc->dev;
++
++ if (wc_dev->modtype[card] != MOD_TYPE_FXO) {
++ return;
++ }
++ wc_dev->mod[card].fxo.pegtimer += DAHDI_CHUNKSIZE;
++ for (x=0;x<DAHDI_CHUNKSIZE;x++) {
++ /* Look for pegging to indicate ringing */
++ sample = DAHDI_XLAW(wc->chans[card].readchunk[x], (&(wc->chans[card])));
++ if ((sample > 10000) && (wc_dev->mod[card].fxo.peg != 1)) {
++ if (debug > 1) {
++ printk(KERN_DEBUG "High peg!\n");
++ }
++ if ((wc_dev->mod[card].fxo.pegtimer < PEGTIME) && (wc_dev->mod[card].fxo.pegtimer > MINPEGTIME)) {
++ wc_dev->mod[card].fxo.pegcount++;
++ }
++ wc_dev->mod[card].fxo.pegtimer = 0;
++ wc_dev->mod[card].fxo.peg = 1;
++ } else if ((sample < -10000) && (wc_dev->mod[card].fxo.peg != -1)) {
++ if (debug > 1) {
++ printk(KERN_DEBUG "Low peg!\n");
++ }
++ if ((wc_dev->mod[card].fxo.pegtimer < (PEGTIME >> 2)) && (wc_dev->mod[card].fxo.pegtimer > (MINPEGTIME >> 2))) {
++ wc_dev->mod[card].fxo.pegcount++;
++ }
++ wc_dev->mod[card].fxo.pegtimer = 0;
++ wc_dev->mod[card].fxo.peg = -1;
++ }
++ }
++ if (wc_dev->mod[card].fxo.pegtimer > PEGTIME) {
++ /* Reset pegcount if our timer expires */
++ wc_dev->mod[card].fxo.pegcount = 0;
++ }
++ /* Decrement debouncer if appropriate */
++ if (wc_dev->mod[card].fxo.ringdebounce) {
++ wc_dev->mod[card].fxo.ringdebounce--;
++ }
++ if (!wc_dev->mod[card].fxo.offhook && !wc_dev->mod[card].fxo.ringdebounce) {
++ if (!wc_dev->mod[card].fxo.ring && (wc_dev->mod[card].fxo.pegcount > PEGCOUNT)) {
++ /* It's ringing */
++ if (debug) {
++ printk(KERN_DEBUG "RING on %d/%d!\n", wc->span.spanno, card + 1);
++ }
++ if (!wc_dev->mod[card].fxo.offhook) {
++ dahdi_hooksig(&wc->chans[card], DAHDI_RXSIG_RING);
++ }
++ wc_dev->mod[card].fxo.ring = 1;
++ }
++ if (wc_dev->mod[card].fxo.ring && !wc_dev->mod[card].fxo.pegcount) {
++ /* No more ring */
++ if (debug) {
++ printk(KERN_DEBUG "NO RING on %d/%d!\n", wc->span.spanno, card + 1);
++ }
++ dahdi_hooksig(&wc->chans[card], DAHDI_RXSIG_OFFHOOK);
++ wc_dev->mod[card].fxo.ring = 0;
++ }
++ }
++}
++#endif
++
++#if 0
++static int a24xx_hardware_init(struct a24xx_dev *wc_dev, char *sigcap)
++{
++ unsigned int x;
++ unsigned char ch;
++
++ /* Signal Reset */
++ if ( (wc_dev->fwversion&0xffff) > 0x3)
++ __opvx_a24xx_reset_modules_v2(wc_dev->mem32, __a24xx_wait_just_a_bit, HZ); // reset again;
++ else
++ __opvx_a24xx_reset_modules(wc_dev->mem32, __a24xx_wait_just_a_bit, HZ); // reset again;
++
++ /* Check OpenVox version */
++ printk("OpenVox %s version: %01x.%01x\n", wc_dev->card_name, wc_dev->fwversion>>16, wc_dev->fwversion&0xffff);
++
++ /* Clear interrupts */
++ __opvx_a24xx_clear_irqs(wc_dev->mem32);
++
++ /* Wait 1/4 of a second more */
++ //opvx_wait_just_a_bit(HZ/4);
++
++ /* test cards exist and type at first */
++ for(x=0; x<wc_dev->max_cards; x+=CARDS_PER_MODULE) {
++ __a24xx_setcard(wc_dev, x);
++ wc_dev->modtype[x] = MOD_TYPE_FXO;
++ ch = __a24xx_spi_getreg(wc_dev, x, 2); /* read register 2, 3050 return 0x3, 3210 return 0x0 */
++ if(0x03 == ch) {
++ wc_dev->modtype[x+1] = MOD_TYPE_FXO;
++ wc_dev->modtype[x+2] = MOD_TYPE_FXO;
++ wc_dev->modtype[x+3] = MOD_TYPE_FXO;
++ if(debug) {
++ printk("module %d is a FXO\n", x);
++ }
++ } else {
++ wc_dev->modtype[x] = MOD_TYPE_FXS;
++ wc_dev->modtype[x+1] = MOD_TYPE_FXS;
++ wc_dev->modtype[x+2] = MOD_TYPE_FXS;
++ wc_dev->modtype[x+3] = MOD_TYPE_FXS;
++ if(debug) {
++ printk("module %d is a FXS or Not Installed\n", x);
++ }
++ }
++ }
++
++ if ( (wc_dev->fwversion&0xffff) > 0x3)
++ __opvx_a24xx_reset_modules_v2(wc_dev->mem32, __a24xx_wait_just_a_bit, HZ); // reset again;
++ else
++ __opvx_a24xx_reset_modules(wc_dev->mem32, __a24xx_wait_just_a_bit, HZ); // reset again;
++
++ for (x = 0; x < wc_dev->max_cards/*MAX_NUM_CARDS*/; x++) {
++ int sane=0,ret=0,readi=0;
++
++ if( (x%4) == 0 && wc_dev->modtype[x]==MOD_TYPE_FXS) { /* set 3215 to daisy chain mode */
++ __a24xx_setcard(wc_dev, x);
++ __opvx_a24xx_write_8bits(wc_dev->mem32, 0x00);
++ __opvx_a24xx_write_8bits(wc_dev->mem32, 0x80);
++ }
++
++#if 1
++ touch_softlockup_watchdog(); // avoid showing CPU softlock message
++
++ /* Init with Auto Calibration */
++ if (!(ret=si321x_init_proslic(wc_dev, x, 0, 0, sane))) {
++ wc_dev->cardflag |= (1 << x);
++ if (debug) {
++ readi = a24xx_spi_getreg(wc_dev,x,LOOP_I_LIMIT);
++ printk("Proslic module %d loop current is %dmA\n",x,
++ ((readi*3)+20));
++ }
++ printk("Module %d: Installed -- AUTO FXS/DPO\n",x);
++ } else {
++ if(ret!=-2) {
++ sane=1;
++
++ printk("Init ProSlic with Manual Calibration \n");
++ /* Init with Manual Calibration */
++ if (!si321x_init_proslic(wc_dev, x, 0, 1, sane)) {
++ wc_dev->cardflag |= (1 << x);
++ if (debug) {
++ readi = a24xx_spi_getreg(wc_dev,x,LOOP_I_LIMIT);
++ printk("Proslic module %d loop current is %dmA\n",x,
++ ((readi*3)+20));
++ }
++ printk("Module %d: Installed -- MANUAL FXS\n",x);
++ } else {
++ printk("Module %d: FAILED FXS (%s)\n", x, fxshonormode ? fxo_modes[_opermode].name : "FCC");
++ sigcap[x] = 1;
++ }
++ } else if (!(ret = si3050_init_voicedaa(wc_dev, x, 0, 0, sane))) {
++ wc_dev->cardflag |= (1 << x);
++ printk("Module %d: Installed -- AUTO FXO (%s mode)\n",x, fxo_modes[_opermode].name);
++ } else
++ printk("Module %d: Not installed\n", x);
++ }
++#endif
++ }
++
++ /* Return error if nothing initialized okay. */
++ if (!wc_dev->cardflag && !timingonly) {
++ return -1;
++ }
++
++ return 0;
++}
++#endif
++
++static int a24xx_hardware_init_all(struct a24xx_dev *wc_dev, char *sigcap)
++{
++ unsigned int x;
++ unsigned char ch;
++ int sane=0,ret=0;
++ int flag=0,tmp_flag=0,blank_flag=0;
++
++ /* Signal Reset */
++ if ( (wc_dev->fwversion&0xffff) > 0x3)
++ __opvx_a24xx_reset_modules_v2(wc_dev->mem32, __a24xx_wait_just_a_bit, HZ); // reset again;
++ else
++ __opvx_a24xx_reset_modules(wc_dev->mem32, __a24xx_wait_just_a_bit, HZ); // reset again;
++
++ /* Check OpenVox version */
++ printk("OpenVox %s version: %01x.%01x\n", wc_dev->card_name, wc_dev->fwversion>>16, wc_dev->fwversion&0xffff);
++
++ /* Clear interrupts */
++ __opvx_a24xx_clear_irqs(wc_dev->mem32);
++
++ /* Wait 1/4 of a second more */
++ //opvx_wait_just_a_bit(HZ/4);
++
++ /* test cards exist and type at first */
++ for(x=0; x<wc_dev->max_cards; x+=CARDS_PER_MODULE) {
++ __a24xx_setcard(wc_dev, x);
++ wc_dev->modtype[x] = MOD_TYPE_FXO;
++ ch = __a24xx_spi_getreg(wc_dev, x, 2); /* read register 2, 3050 return 0x3, 3210 return 0x0 */
++ if(0x03 == ch) {
++ wc_dev->modtype[x+1] = MOD_TYPE_FXO;
++ wc_dev->modtype[x+2] = MOD_TYPE_FXO;
++ wc_dev->modtype[x+3] = MOD_TYPE_FXO;
++ if(debug) {
++ printk("module %d is a FXO\n", x);
++ }
++ } else {
++ wc_dev->modtype[x] = MOD_TYPE_FXS;
++ wc_dev->modtype[x+1] = MOD_TYPE_FXS;
++ wc_dev->modtype[x+2] = MOD_TYPE_FXS;
++ wc_dev->modtype[x+3] = MOD_TYPE_FXS;
++ if(debug) {
++ printk("module %d is a FXS or Not Installed\n", x);
++ }
++ }
++ }
++
++ if ( (wc_dev->fwversion&0xffff) > 0x3)
++ __opvx_a24xx_reset_modules_v2(wc_dev->mem32, __a24xx_wait_just_a_bit, HZ); // reset again;
++ else
++ __opvx_a24xx_reset_modules(wc_dev->mem32, __a24xx_wait_just_a_bit, HZ); // reset again;
++
++ for (x = 0; x < wc_dev->max_cards/*MAX_NUM_CARDS*/; x++) {
++ sane=1,ret=0;
++ touch_softlockup_watchdog(); // avoid showing CPU softlock message
++
++ /* Init with Auto Calibration */
++ if(wc_dev->modtype[x] == MOD_TYPE_FXO){
++ if (!(ret = si3050_init_voicedaa(wc_dev, x, 0, 0, sane))) {
++ wc_dev->cardflag |= (1 << x);
++ }
++ }
++ }
++
++ /*initial FXS modules*/
++ for (x = 0; x < wc_dev->max_cards/*MAX_NUM_CARDS*/; x++) {
++ if(wc_dev->modtype[x] == MOD_TYPE_FXS){
++ flag |=(1 << x);
++ }
++ }
++
++ touch_softlockup_watchdog();
++ if((tmp_flag=si321x_init_proslic_all(wc_dev,flag,0,0,0,&blank_flag))>0)
++ {
++ tmp_flag=si321x_init_proslic_all(wc_dev,tmp_flag,0,1,1,NULL);
++ }
++ flag &=~blank_flag;
++ flag &=~tmp_flag;
++ wc_dev->cardflag |=flag;
++
++ for (x = 0; x < wc_dev->max_cards; x++){
++ if(wc_dev->cardflag & (1<<x)){
++ spin_lock_init(&wc_dev->mod[x].fxs.lasttxhooklock);
++ if(wc_dev->modtype[x] == MOD_TYPE_FXS){
++ printk("Module %d: Installed -- AUTO FXS/DPO\n",x);
++ }else
++ printk("Module %d: Installed -- AUTO FXO (%s mode)\n",x, fxo_modes[_opermode].name);
++ }else{
++ if(tmp_flag & (1 << x)){
++ printk("Module %d: FAILED FXS (%s)\n", x, fxshonormode ? fxo_modes[_opermode].name : "FCC");
++ sigcap[x] = 1; //************
++ }
++ else
++ printk("Module %d: Not installed\n", x);
++ }
++ }
++
++ /* Return error if nothing initialized okay. */
++ if (!wc_dev->cardflag && !timingonly) {
++ return -1;
++ }
++
++ return 0;
++}
++
++static void callerid_ring_on_deal(struct a24xx *wc, int card)
++{
++ struct a24xx_dev *wc_dev = &wc->dev;
++
++ if(wc_dev->cid_state[card] == CID_STATE_IDLE) {
++ reset_parser_variable_from_chan_num(wc->span.spanno, wc->chans[card]->channo);
++ if (is_ring_delay_operation(wc->span.spanno, wc->chans[card]->channo)) {
++ wc_dev->cid_state[card] = CID_STATE_RING_DELAY;
++ } else {
++ wc_dev->cid_state[card] = CID_STATE_RING_ON;
++ wc_dev->cid_history_clone_cnt[card] = cidbuflen;
++ dahdi_hooksig(wc->chans[card], DAHDI_RXSIG_RING);
++ }
++ } else if(wc_dev->cid_state[card] == CID_STATE_RING_DELAY) {
++ wc_dev->cid_state[card] = CID_STATE_RING_ON;
++ wc_dev->cid_history_clone_cnt[card] = cidbuflen;
++ dahdi_hooksig(wc->chans[card], DAHDI_RXSIG_RING);
++ } else {
++ if (wc_dev->cid_state[card] != CID_STATE_WAIT_RING_FINISH) {
++ set_signal_unknown_from_chan_num(wc->span.spanno, wc->chans[card]->channo);
++ wc_dev->cid_state[card] = CID_STATE_WAIT_RING_FINISH;
++ }
++ wc_dev->cid_history_clone_cnt[card] = cidtimeout;
++ dahdi_hooksig(wc->chans[card], DAHDI_RXSIG_RING);
++ }
++}
++
++static void callerid_ring_off_deal(struct a24xx *wc, int card)
++{
++ struct a24xx_dev *wc_dev = &wc->dev;
++
++ if(wc_dev->cid_state[card] == CID_STATE_RING_ON) {
++ wc_dev->cid_state[card] = CID_STATE_RING_OFF;
++ }
++
++ if(wc_dev->cid_state[card] != CID_STATE_RING_DELAY) {
++ dahdi_hooksig(wc->chans[card], DAHDI_RXSIG_OFFHOOK);
++ }
++}
++
++static void a24xx_voicedaa_check_hook(struct a24xx *wc, int card)
++{
++#define MS_PER_CHECK_HOOK 16
++#ifndef AUDIO_RINGCHECK
++ unsigned char res;
++#endif
++ signed char b;
++ int errors = 0;
++ struct a24xx_dev *wc_dev = &wc->dev;
++ struct fxo *fxo = &wc_dev->mod[card].fxo;
++
++ /* Try to track issues that plague slot one FXO's */
++ b = wc_dev->reg0shadow[card];
++ if ((b & 0x2) || !(b & 0x8)) {
++ /* Not good -- don't look at anything else */
++ if (debug) {
++ printk(KERN_DEBUG "Errors (%02x) on card %d!\n", b, card + 1);
++ }
++ errors++;
++ }
++ b &= 0x9b;
++ if (fxo->offhook) {
++ if (b != 0x9) {
++ a24xx_spi_setreg(wc_dev, card, 5, 0x9);
++ }
++ } else {
++ if (b != 0x8) {
++ a24xx_spi_setreg(wc_dev, card, 5, 0x8);
++ }
++ }
++ if (errors) {
++ return;
++ }
++
++ if (!fxo->offhook) {
++ if (fwringdetect) {
++ res = wc_dev->reg0shadow[card] & 0x60;
++ if (fxo->ringdebounce--) {
++ if (res && (res != fxo->lastrdtx)
++ && (fxo->battery == BATTERY_PRESENT)) {
++ if (!fxo->wasringing) {
++ fxo->wasringing = 1;
++ if (debug) {
++ printk(KERN_DEBUG "RING on %d/%d!\n", wc->span.spanno, card + 1);
++ }
++ if(cidsupport) {
++ callerid_ring_on_deal(wc, card);
++ } else {
++ dahdi_hooksig(wc->chans[card], DAHDI_RXSIG_RING);
++ }
++ }
++ fxo->lastrdtx = res;
++ fxo->ringdebounce = 10;
++ } else if (!res) {
++ if ((fxo->ringdebounce == 0)
++ && fxo->wasringing) {
++ fxo->wasringing = 0;
++ if (debug) {
++ printk(KERN_DEBUG "NO RING on %d/%d!\n", wc->span.spanno, card + 1);
++ }
++ if(cidsupport) {
++ callerid_ring_off_deal(wc, card);
++ } else {
++ dahdi_hooksig(wc->chans[card], DAHDI_RXSIG_OFFHOOK);
++ }
++ }
++ }
++ } else if (res && (fxo->battery == BATTERY_PRESENT)) {
++ fxo->lastrdtx = res;
++ fxo->ringdebounce = 10;
++ }
++ } else {
++ res = wc_dev->reg0shadow[card];
++ if ((res & 0x60) && (fxo->battery == BATTERY_PRESENT)) {
++ fxo->ringdebounce += (DAHDI_CHUNKSIZE * 16);
++ if (fxo->ringdebounce >= DAHDI_CHUNKSIZE * ringdebounce) {
++ if (!fxo->wasringing) {
++ fxo->wasringing = 1;
++ if(cidsupport) {
++ callerid_ring_on_deal(wc, card);
++ } else {
++ dahdi_hooksig(wc->chans[card], DAHDI_RXSIG_RING);
++ }
++ if (debug) {
++ printk(KERN_DEBUG "RING on %d/%d!\n", wc->span.spanno, card + 1);
++ }
++ }
++ fxo->ringdebounce = DAHDI_CHUNKSIZE * ringdebounce;
++ }
++ } else {
++ fxo->ringdebounce -= DAHDI_CHUNKSIZE * 4;
++ if (fxo->ringdebounce <= 0) {
++ if (fxo->wasringing) {
++ fxo->wasringing = 0;
++ if(cidsupport) {
++ callerid_ring_off_deal(wc, card);
++ } else {
++ dahdi_hooksig(wc->chans[card], DAHDI_RXSIG_OFFHOOK);
++ }
++ if (debug) {
++ printk(KERN_DEBUG "NO RING on %d/%d!\n", wc->span.spanno, card + 1);
++ }
++ }
++ fxo->ringdebounce = 0;
++ }
++ }
++ }
++ }
++
++ b = wc_dev->reg1shadow[card];
++ if (abs(b) < battthresh) {
++ /* possible existing states:
++ battery lost, no debounce timer
++ battery lost, debounce timer (going to battery present)
++ battery present or unknown, no debounce timer
++ battery present or unknown, debounce timer (going to battery lost)
++ */
++
++ if (fxo->battery == BATTERY_LOST) {
++ if (fxo->battdebounce) {
++ /* we were going to BATTERY_PRESENT, but battery was lost again,
++ so clear the debounce timer */
++ fxo->battdebounce = 0;
++ }
++ } else {
++ if (fxo->battdebounce) {
++ /* going to BATTERY_LOST, see if we are there yet */
++ if (--fxo->battdebounce == 0) {
++ fxo->battery = BATTERY_LOST;
++ if (debug) {
++ printk(KERN_DEBUG "NO BATTERY on %d/%d!\n", wc->span.spanno, card + 1);
++ }
++#ifdef JAPAN
++ if (!wc_dev->ohdebounce && wc_dev->offhook) {
++ dahdi_hooksig(wc->chans[card], DAHDI_RXSIG_ONHOOK);
++ if (debug) {
++ printk(KERN_DEBUG "Signalled On Hook\n");
++ }
++#ifdef ZERO_BATT_RING
++ wc_dev->onhook++;
++#endif
++ }
++#else
++ dahdi_hooksig(wc->chans[card], DAHDI_RXSIG_ONHOOK);
++ /* set the alarm timer, taking into account that part of its time
++ period has already passed while debouncing occurred */
++ fxo->battalarm = (battalarm - battdebounce) / MS_PER_CHECK_HOOK;
++#endif
++ }
++ } else {
++ /* start the debounce timer to verify that battery has been lost */
++ fxo->battdebounce = battdebounce / MS_PER_CHECK_HOOK;
++ }
++ }
++ } else {
++ /* possible existing states:
++ battery lost or unknown, no debounce timer
++ battery lost or unknown, debounce timer (going to battery present)
++ battery present, no debounce timer
++ battery present, debounce timer (going to battery lost)
++ */
++ if (fxo->battery == BATTERY_PRESENT) {
++ if (fxo->battdebounce) {
++ /* we were going to BATTERY_LOST, but battery appeared again,
++ so clear the debounce timer */
++ fxo->battdebounce = 0;
++ }
++ } else {
++ if (fxo->battdebounce) {
++ /* going to BATTERY_PRESENT, see if we are there yet */
++ if (--fxo->battdebounce == 0) {
++ fxo->battery = BATTERY_PRESENT;
++ if (debug) {
++ printk(KERN_DEBUG "BATTERY on %d/%d (%s)!\n", wc->span.spanno, card + 1,
++ (b < 0) ? "-" : "+");
++ }
++#ifdef ZERO_BATT_RING
++ if (wc_dev->onhook) {
++ wc_dev->onhook = 0;
++ dahdi_hooksig(wc->chans[card], DAHDI_RXSIG_OFFHOOK);
++ if (debug) {
++ printk(KERN_DEBUG "Signalled Off Hook\n");
++ }
++ }
++#else
++ dahdi_hooksig(wc->chans[card], DAHDI_RXSIG_OFFHOOK);
++#endif
++ /* set the alarm timer, taking into account that part of its time
++ period has already passed while debouncing occurred */
++ fxo->battalarm = (battalarm - battdebounce) / MS_PER_CHECK_HOOK;
++ }
++ } else {
++ /* start the debounce timer to verify that battery has appeared */
++ fxo->battdebounce = battdebounce / MS_PER_CHECK_HOOK;
++ }
++ }
++ }
++
++ if (fxo->lastpol >= 0) {
++ if (b < 0) {
++ fxo->lastpol = -1;
++ fxo->polaritydebounce = POLARITY_DEBOUNCE / MS_PER_CHECK_HOOK;
++ }
++ }
++ if (fxo->lastpol <= 0) {
++ if (b > 0) {
++ fxo->lastpol = 1;
++ fxo->polaritydebounce = POLARITY_DEBOUNCE / MS_PER_CHECK_HOOK;
++ }
++ }
++ if (fxo->battalarm) {
++ if (--fxo->battalarm == 0) {
++ /* the alarm timer has expired, so update the battery alarm state
++ for this channel */
++ dahdi_alarm_channel(wc->chans[card], fxo->battery== BATTERY_LOST ? DAHDI_ALARM_RED:DAHDI_ALARM_NONE );
++ }
++ }
++ if (fxo->polaritydebounce) {
++ if (--fxo->polaritydebounce == 0) {
++ if (fxo->lastpol != fxo->polarity) {
++ if (debug) {
++ printk(KERN_DEBUG "%lu Polarity reversed (%d -> %d)\n", jiffies,
++ fxo->polarity,
++ fxo->lastpol);
++ }
++ if (fxo->polarity) {
++ if (cidsupport) {
++ set_cidstart_desc_from_chan_num(wc->span.spanno, wc->chans[card]->channo, wc_dev->cid_state[card]);
++ if (is_callerid_disable(wc->span.spanno, wc->chans[card]->channo)) {
++ dahdi_qevent_lock(wc->chans[card], DAHDI_EVENT_POLARITY);
++ }
++ } else {
++ dahdi_qevent_lock(wc->chans[card], DAHDI_EVENT_POLARITY);
++ }
++ }
++ fxo->polarity = fxo->lastpol;
++ }
++ }
++ }
++#undef MS_PER_CHECK_HOOK
++}
++
++
++static void a24xx_post_initialize(struct a24xx *wc)
++{
++ int x;
++ struct a24xx_dev *wc_dev = &wc->dev;
++
++ /* Finalize signalling */
++ for (x = 0; x < wc_dev->max_cards/*MAX_NUM_CARDS*/; x++) {
++ if (wc_dev->cardflag & (1 << x)) {
++ if (wc_dev->modtype[x] == MOD_TYPE_FXO) {
++ wc->chans[x]->sigcap = DAHDI_SIG_FXSKS | DAHDI_SIG_FXSLS | DAHDI_SIG_SF | DAHDI_SIG_CLEAR;
++ }
++ else {
++ wc->chans[x]->sigcap = DAHDI_SIG_FXOKS | DAHDI_SIG_FXOLS | DAHDI_SIG_FXOGS | DAHDI_SIG_SF | DAHDI_SIG_EM | DAHDI_SIG_CLEAR;
++ }
++ } else if (!(wc->chans[x]->sigcap & DAHDI_SIG_BROKEN)) {
++ wc->chans[x]->sigcap = 0;
++ }
++ }
++}
++
++static void a24xx_proslic_check_hook(struct a24xx *wc, int card)
++{
++ char res;
++ int hook;
++ struct a24xx_dev *wc_dev = &wc->dev;
++
++ /* For some reason we have to debounce the
++ hook detector. */
++
++ res = wc_dev->reg0shadow[card];
++ hook = (res & 1);
++ if (hook != wc_dev->mod[card].fxs.lastrxhook) {
++ /* Reset the debounce (must be multiple of 4ms) */
++ wc_dev->mod[card].fxs.debounce = 8 * (4 * 8);
++#if 0
++ printk(KERN_DEBUG "Resetting debounce card %d hook %d, %d\n", card, hook, wc_dev->mod[card].fxs.debounce);
++#endif
++ } else {
++ if (wc_dev->mod[card].fxs.debounce > 0) {
++ wc_dev->mod[card].fxs.debounce-= 16 * DAHDI_CHUNKSIZE;
++#if 0
++ printk(KERN_DEBUG "Sustaining hook %d, %d\n", hook, wc_dev->mod[card].fxs.debounce);
++#endif
++ if (!wc_dev->mod[card].fxs.debounce) {
++#if 0
++ printk(KERN_DEBUG "Counted down debounce, newhook: %d...\n", hook);
++#endif
++ wc_dev->mod[card].fxs.debouncehook = hook;
++ }
++ if (!wc_dev->mod[card].fxs.oldrxhook && wc_dev->mod[card].fxs.debouncehook) {
++ /* Off hook */
++ if (debug) {
++ printk(KERN_DEBUG "opvxa24xx: Card %d Going off hook\n", card);
++ }
++ switch (wc_dev->mod[card].fxs.lasttxhook) {
++ case SLIC_LF_RINGING: /* Ringing */
++ case SLIC_LF_OHTRAN_FWD: /* Forward On Hook Transfer */
++ case SLIC_LF_OHTRAN_REV: /* Reverse On Hook Transfer */
++ /* just detected OffHook, during Ringing or OnHookTransfer */
++ wc_dev->mod[card].fxs.idletxhookstate = POLARITY_XOR(card) ?
++ SLIC_LF_ACTIVE_REV :
++ SLIC_LF_ACTIVE_FWD;
++ break;
++ }
++ dahdi_hooksig(wc->chans[card], DAHDI_RXSIG_OFFHOOK);
++ if (robust) {
++ si321x_init_proslic(wc_dev, card, 1, 0, 1);
++ }
++ wc_dev->mod[card].fxs.oldrxhook = 1;
++
++ } else if (wc_dev->mod[card].fxs.oldrxhook && !wc_dev->mod[card].fxs.debouncehook) {
++ /* On hook */
++ if (debug) {
++ printk(KERN_DEBUG "opvxa24xx: Card %d Going on hook\n", card);
++ }
++ dahdi_hooksig(wc->chans[card], DAHDI_RXSIG_ONHOOK);
++ wc_dev->mod[card].fxs.oldrxhook = 0;
++ }
++ }
++ }
++ wc_dev->mod[card].fxs.lastrxhook = hook;
++}
++
++static void a24xx_transmit(struct a24xx *wc,unsigned int order)
++{
++ int x, y, pos;
++ volatile unsigned char *txbuf;
++ struct a24xx_dev *wc_dev = &wc->dev;
++
++ __opvx_a24xx_transmit(wc_dev->mem32, wc_dev->writechunk, &txbuf,ms_per_irq,order);
++ //printk("ints is %d\n", ints);
++ /* Calculate Transmission */
++
++ for (y=0;y<DAHDI_CHUNKSIZE;y++) {
++#ifdef __BIG_ENDIAN
++ // operation pending...
++#else
++ for (x=0;x<wc_dev->max_cards;x++) {
++ pos = y * MAX_NUM_CARDS + x;
++ txbuf[pos] = wc->chans[x]->writechunk[y];
++ }
++#endif
++ }
++}
++
++static void a24xx_receive(struct a24xx *wc,unsigned int order)
++{
++ int x, y;
++ volatile unsigned char *rxbuf;
++ struct a24xx_dev *wc_dev = &wc->dev;
++
++ __opvx_a24xx_receive(wc_dev->mem32, wc_dev->readchunk, &rxbuf ,ms_per_irq,order);
++ for (x=0;x<DAHDI_CHUNKSIZE;x++) {
++#ifdef __BIG_ENDIAN
++ // operation pending...
++#else
++ for (y=0;y<wc_dev->max_cards/*MAX_NUM_CARDS*/;y++) {
++ if (wc_dev->cardflag & (1 << y)) {
++ wc->chans[y]->readchunk[x] = rxbuf[MAX_NUM_CARDS * x + y];
++ }
++#ifdef TEST_LOG_INCOME_VOICE
++ wc_dev->voc_buf[y][wc_dev->voc_ptr[y]] = rxbuf[MAX_NUM_CARDS * x + y];
++ wc_dev->voc_ptr[y]++;
++ if(wc_dev->voc_ptr[y] >= voc_buffer_size) {
++ wc_dev->voc_ptr[y] = 0;
++ }
++#endif
++ }
++#endif
++ }
++
++#if 0
++ if( (ints&0x0f) == 0x0f) { // dump buffer every 16 times;
++ printk("dump 0x%x\n", ints);
++ for(x=0; x<48; x++) {
++ printk("0x%02x, ", (int)rxbuf[x]);
++ //if( 15== (x%15) )
++ }
++ printk("\n");
++ }
++#endif
++
++ if(cidsupport) {
++ parser_callerid_process(wc, cidbuflen, cidtimeout);
++ }
++
++#ifdef AUDIO_RINGCHECK
++ for (x=0;x<wc_dev->max_cards;x++) {
++ ring_check(wc, x);
++ }
++#endif
++ /* XXX We're wasting 8 taps. We should get closer :( */
++ for (x = 0; x < wc_dev->max_cards/*MAX_NUM_CARDS*/; x++) {
++ if (wc_dev->cardflag & (1 << x)) {
++ dahdi_ec_chunk(wc->chans[x], wc->chans[x]->readchunk, wc->chans[x]->writechunk);
++ }
++ }
++}
++
++#if 0
++DAHDI_IRQ_HANDLER(a24xx_interrupt)
++{
++ unsigned int ints;
++ int x, y, z,order;
++ int mode;
++ struct a24xx *wc = dev_id;
++ struct a24xx_dev *wc_dev = &wc->dev;
++
++ struct fxs * fxs ;
++
++ ints = __opvx_a24xx_get_irqstatus(wc_dev->mem32);
++
++ if (!ints)
++ return IRQ_NONE;
++
++ __opvx_a24xx_set_irqstatus(wc_dev->mem32, ints); // clear interrupt register.
++
++ for (x=0;x<wc_dev->max_cards; x++) {
++ if (wc_dev->cardflag & (1 << x) && (wc_dev->modtype[x] == MOD_TYPE_FXS)) {
++
++ fxs = &wc_dev->mod[x].fxs;
++
++ if (SLIC_LF_RINGING == fxs->lasttxhook && !fxs->neonringing) {
++ /* RINGing, prepare for OHT */
++ fxs->ohttimer = OHT_TIMER << 3;
++ /* OHT mode when idle */
++ fxs->idletxhookstate = POLARITY_XOR(x) ? SLIC_LF_OHTRAN_REV :
++ SLIC_LF_OHTRAN_FWD;
++ } else if (fxs->ohttimer) {
++ /* check if still OnHook */
++ if (!fxs->oldrxhook) {
++ fxs->ohttimer -= DAHDI_CHUNKSIZE;
++ if (fxs->ohttimer)
++ continue;
++
++ /* Switch to active */
++ fxs->idletxhookstate = POLARITY_XOR(x) ? SLIC_LF_ACTIVE_REV :
++ SLIC_LF_ACTIVE_FWD;
++ /* if currently OHT */
++ if ((fxs->lasttxhook == SLIC_LF_OHTRAN_FWD) || (fxs->lasttxhook == SLIC_LF_OHTRAN_REV)) {
++ if (fxs->vmwi_hvac) {
++ /* force idle polarity Forward if ringing */
++ fxs->idletxhookstate = SLIC_LF_ACTIVE_FWD;
++ /* Set ring generator for neon */
++ si321x_set_ring_generator_mode(wc_dev, x, 1);
++ fxs->lasttxhook = SLIC_LF_RINGING;
++ } else {
++ fxs->lasttxhook = fxs->idletxhookstate;
++ }
++ /* Apply the change as appropriate */
++ a24xx_spi_setreg(wc_dev, x, LINE_STATE, fxs->lasttxhook);
++ }
++ } else {
++ fxs->ohttimer = 0;
++ /* Switch to active */
++ fxs->idletxhookstate = POLARITY_XOR(x) ? SLIC_LF_ACTIVE_REV : SLIC_LF_ACTIVE_FWD;
++ printk("Channel %d OnHookTransfer abort\n",x);
++ }
++ }
++ }
++ }
++
++ if (ints & (1<<16)) { /* it is our interrupts */
++ wc_dev->intcount++;
++ switch(ms_per_irq){
++ case 1:
++ z = wc_dev->intcount & 0x3;
++ mode = wc_dev->intcount & 0xc;
++ for(y = 0; y < wc_dev->max_cards / 4; y++) { /* do some detect operate every 4ms */
++ x = z + y*4;
++ if (wc_dev->cardflag & (1 << x ) ) {
++ switch(mode) {
++ case 0:
++ /* Rest */
++ break;
++ case 4:
++ /* Read first shadow reg */
++ if (wc_dev->modtype[x] == MOD_TYPE_FXS) {
++ wc_dev->reg0shadow[x] = a24xx_spi_getreg(wc_dev, x, 68);
++ //if(x==0)
++ //printk("reg 68 of %x is 0x%x\n", x, wc_dev->reg0shadow[x]);
++ }
++ else if (wc_dev->modtype[x] == MOD_TYPE_FXO)
++ wc_dev->reg0shadow[x] = a24xx_spi_getreg(wc_dev, x, 5);
++ break;
++ case 8:
++ /* Read second shadow reg */
++ if (wc_dev->modtype[x] == MOD_TYPE_FXS) {
++ wc_dev->reg1shadow[x] = a24xx_spi_getreg(wc_dev, x, 64);
++ //if(x==1)
++ // printk("reg 64 of %x is 0x%x\n", x, wc_dev->reg1shadow[x]);
++ }
++ else if (wc_dev->modtype[x] == MOD_TYPE_FXO)
++ wc_dev->reg1shadow[x] = a24xx_spi_getreg(wc_dev, x, 29);
++ break;
++ case 12:
++ /* Perform processing */
++ if (wc_dev->modtype[x] == MOD_TYPE_FXS) {
++ a24xx_proslic_check_hook(wc, x);
++ if (!(wc_dev->intcount & 0xf0)) {
++ si321x_proslic_recheck_sanity(wc_dev, x);
++ }
++ } else if (wc_dev->modtype[x] == MOD_TYPE_FXO) {
++ a24xx_voicedaa_check_hook(wc, x);
++ }
++ break;
++ }
++ }
++ }
++ break;
++ case 2:
++ z = wc_dev->intcount & 0x1;
++ mode = wc_dev->intcount & 0x6;
++ for(y = 0; y < wc_dev->max_cards / 2; y++) { /* do some detect operate every 4ms */
++ x = z + y*2;
++ if (wc_dev->cardflag & (1 << x ) ) {
++ switch(mode) {
++ case 0:
++ /* Rest */
++ break;
++ case 2:
++ /* Read first shadow reg */
++ if (wc_dev->modtype[x] == MOD_TYPE_FXS) {
++ wc_dev->reg0shadow[x] = a24xx_spi_getreg(wc_dev, x, 68);
++ //if(x==0)
++ //printk("reg 68 of %x is 0x%x\n", x, wc_dev->reg0shadow[x]);
++ }
++ else if (wc_dev->modtype[x] == MOD_TYPE_FXO)
++ wc_dev->reg0shadow[x] = a24xx_spi_getreg(wc_dev, x, 5);
++ break;
++ case 4:
++ /* Read second shadow reg */
++ if (wc_dev->modtype[x] == MOD_TYPE_FXS) {
++ wc_dev->reg1shadow[x] = a24xx_spi_getreg(wc_dev, x, 64);
++ //if(x==1)
++ // printk("reg 64 of %x is 0x%x\n", x, wc_dev->reg1shadow[x]);
++ }
++ else if (wc_dev->modtype[x] == MOD_TYPE_FXO)
++ wc_dev->reg1shadow[x] = a24xx_spi_getreg(wc_dev, x, 29);
++ break;
++ case 6:
++ /* Perform processing */
++ if (wc_dev->modtype[x] == MOD_TYPE_FXS) {
++ a24xx_proslic_check_hook(wc, x);
++ if (!(wc_dev->intcount & (0xf0>>1))) {
++ si321x_proslic_recheck_sanity(wc_dev, x);
++ }
++ } else if (wc_dev->modtype[x] == MOD_TYPE_FXO) {
++ a24xx_voicedaa_check_hook(wc, x);
++ }
++ break;
++ }
++ }
++ }
++ break;
++ case 4:
++ mode = wc_dev->intcount & 0x3;
++ for(x=0;x<wc_dev->max_cards;x++){
++ if (wc_dev->cardflag & (1 << x ) ) {
++ switch(mode) {
++ case 0:
++ /* Rest */
++ break;
++ case 1:
++ /* Read first shadow reg */
++ if (wc_dev->modtype[x] == MOD_TYPE_FXS) {
++ wc_dev->reg0shadow[x] = a24xx_spi_getreg(wc_dev, x, 68);
++ //if(x==0)
++ //printk("reg 68 of %x is 0x%x\n", x, wc_dev->reg0shadow[x]);
++ }
++ else if (wc_dev->modtype[x] == MOD_TYPE_FXO)
++ wc_dev->reg0shadow[x] = a24xx_spi_getreg(wc_dev, x, 5);
++ break;
++ case 2:
++ /* Read second shadow reg */
++ if (wc_dev->modtype[x] == MOD_TYPE_FXS) {
++ wc_dev->reg1shadow[x] = a24xx_spi_getreg(wc_dev, x, 64);
++ //if(x==1)
++ // printk("reg 64 of %x is 0x%x\n", x, wc_dev->reg1shadow[x]);
++ }
++ else if (wc_dev->modtype[x] == MOD_TYPE_FXO)
++ wc_dev->reg1shadow[x] = a24xx_spi_getreg(wc_dev, x, 29);
++ break;
++ case 3:
++ /* Perform processing */
++ if (wc_dev->modtype[x] == MOD_TYPE_FXS) {
++ a24xx_proslic_check_hook(wc, x);
++ if (!(wc_dev->intcount & (0xf0>>2))) {
++ si321x_proslic_recheck_sanity(wc_dev, x);
++ }
++ }
++ else if (wc_dev->modtype[x] == MOD_TYPE_FXO) {
++ a24xx_voicedaa_check_hook(wc, x);
++ }
++ break;
++ }
++ }
++ }
++ break;
++ case 8:
++ mode = wc_dev->intcount & 0x1;
++ for(x=0;x<wc_dev->max_cards;x++){
++ if (wc_dev->cardflag & (1 << x ) ) {
++ switch(mode) {
++ case 1:
++ if (wc_dev->modtype[x] == MOD_TYPE_FXS) {
++ wc_dev->reg0shadow[x] = a24xx_spi_getreg(wc_dev, x, 68);
++ wc_dev->reg1shadow[x] = a24xx_spi_getreg(wc_dev, x, 64);
++ }
++ else if (wc_dev->modtype[x] == MOD_TYPE_FXO){
++ wc_dev->reg0shadow[x] = a24xx_spi_getreg(wc_dev, x, 5);
++ wc_dev->reg1shadow[x] = a24xx_spi_getreg(wc_dev, x, 29);
++ }
++ break;
++ case 0:
++ /* Perform processing */
++ if (wc_dev->modtype[x] == MOD_TYPE_FXS) {
++ a24xx_proslic_check_hook(wc, x);
++ if (!(wc_dev->intcount & (0xf0>>3))) {
++ si321x_proslic_recheck_sanity(wc_dev, x);
++ }
++ }
++ else if (wc_dev->modtype[x] == MOD_TYPE_FXO) {
++ a24xx_voicedaa_check_hook(wc, x);
++ }
++ break;
++ }
++ }
++ }
++ break;
++ case 16:
++ for(x=0;x<wc_dev->max_cards;x++){
++ if (wc_dev->cardflag & (1 << x ) ) {
++ if (wc_dev->modtype[x] == MOD_TYPE_FXS) {
++ wc_dev->reg0shadow[x] = a24xx_spi_getreg(wc_dev, x, 68);
++ wc_dev->reg1shadow[x] = a24xx_spi_getreg(wc_dev, x, 64);
++ a24xx_proslic_check_hook(wc, x);
++ if (!(wc_dev->intcount & (0xf0>>4))) {
++ si321x_proslic_recheck_sanity(wc_dev, x);
++ }
++ }
++ else if (wc_dev->modtype[x] == MOD_TYPE_FXO){
++ wc_dev->reg0shadow[x] = a24xx_spi_getreg(wc_dev, x, 5);
++ wc_dev->reg1shadow[x] = a24xx_spi_getreg(wc_dev, x, 29);
++ a24xx_voicedaa_check_hook(wc, x);
++ }
++ }
++ }
++ break;
++ }
++
++ if (!(wc_dev->intcount % ( 10000/ms_per_irq ))) {
++ /* Accept an alarm once per 10 seconds */
++ for (x=0;x<wc_dev->max_cards;x++)
++ if (wc_dev->modtype[x] == MOD_TYPE_FXS) {
++ if (wc_dev->mod[x].fxs.palarms) {
++ wc_dev->mod[x].fxs.palarms--;
++ }
++ }
++ }
++ /**/
++ for(order=0;order < ms_per_irq;order++){
++ a24xx_receive(wc, order);
++ dahdi_receive(&wc->span);
++
++ dahdi_transmit(&wc->span);
++ a24xx_transmit(wc, order);
++ }
++ }
++
++ return IRQ_RETVAL(1);
++}
++#endif
++
++#if 1
++static int interrupt_onecard_handler(struct a24xx *wc)
++{
++ unsigned int ints=0;
++ int x, y, z,order;
++ int mode;
++ struct a24xx_dev *wc_dev = &wc->dev;
++
++ struct fxs * fxs ;
++
++ if((wc_dev->fwversion < ((1 << 16)|3)) || (wc_dev->master)){
++ ints = __opvx_a24xx_get_irqstatus(wc_dev->mem32);
++ if (!ints)
++ return IRQ_NONE;
++ }
++
++ __opvx_a24xx_set_irqstatus(wc_dev->mem32, ints); // clear interrupt register.
++
++ for (x=0;x<wc_dev->max_cards; x++) {
++ if (wc_dev->cardflag & (1 << x) && (wc_dev->modtype[x] == MOD_TYPE_FXS)) {
++ fxs = &wc_dev->mod[x].fxs;
++
++ if (SLIC_LF_RINGING == fxs->lasttxhook && !fxs->neonringing) {
++ /* RINGing, prepare for OHT */
++ fxs->ohttimer = OHT_TIMER << 3;
++ /* OHT mode when idle */
++ fxs->idletxhookstate = POLARITY_XOR(x) ? SLIC_LF_OHTRAN_REV :
++ SLIC_LF_OHTRAN_FWD;
++ } else if (fxs->ohttimer) {
++ /* check if still OnHook */
++ if (!fxs->oldrxhook) {
++ fxs->ohttimer -= DAHDI_CHUNKSIZE;
++ if (fxs->ohttimer)
++ continue;
++
++ /* Switch to active */
++ fxs->idletxhookstate = POLARITY_XOR(x) ? SLIC_LF_ACTIVE_REV :
++ SLIC_LF_ACTIVE_FWD;
++ /* if currently OHT */
++ if ((fxs->lasttxhook == SLIC_LF_OHTRAN_FWD) || (fxs->lasttxhook == SLIC_LF_OHTRAN_REV)) {
++ if (fxs->vmwi_hvac) {
++ /* force idle polarity Forward if ringing */
++ fxs->idletxhookstate = SLIC_LF_ACTIVE_FWD;
++ /* Set ring generator for neon */
++ si321x_set_ring_generator_mode(wc_dev, x, 1);
++ fxs->lasttxhook = SLIC_LF_RINGING;
++ } else {
++ fxs->lasttxhook = fxs->idletxhookstate;
++ }
++ /* Apply the change as appropriate */
++ a24xx_spi_setreg(wc_dev, x, LINE_STATE, fxs->lasttxhook);
++ }
++ } else {
++ fxs->ohttimer = 0;
++ /* Switch to active */
++ fxs->idletxhookstate = POLARITY_XOR(x) ? SLIC_LF_ACTIVE_REV : SLIC_LF_ACTIVE_FWD;
++ printk("Channel %d OnHookTransfer abort\n",x);
++ }
++ }
++ }
++ }
++
++ if ( ((ints & (1<<16)) && wc_dev->master) || (!wc_dev->master)) { /* it is our interrupts */
++ wc_dev->intcount++;
++ switch(ms_per_irq){
++ case 1:
++ z = wc_dev->intcount & 0x3;
++ mode = wc_dev->intcount & 0xc;
++ for(y = 0; y < wc_dev->max_cards / 4; y++) { /* do some detect operate every 4ms */
++ x = z + y*4;
++ if (wc_dev->cardflag & (1 << x ) ) {
++ switch(mode) {
++ case 0:
++ /* Rest */
++ break;
++ case 4:
++ /* Read first shadow reg */
++ if (wc_dev->modtype[x] == MOD_TYPE_FXS) {
++ wc_dev->reg0shadow[x] = a24xx_spi_getreg(wc_dev, x, 68);
++ //if(x==0)
++ //printk("reg 68 of %x is 0x%x\n", x, wc_dev->reg0shadow[x]);
++ }
++ else if (wc_dev->modtype[x] == MOD_TYPE_FXO)
++ wc_dev->reg0shadow[x] = a24xx_spi_getreg(wc_dev, x, 5);
++ break;
++ case 8:
++ /* Read second shadow reg */
++ if (wc_dev->modtype[x] == MOD_TYPE_FXS) {
++ wc_dev->reg1shadow[x] = a24xx_spi_getreg(wc_dev, x, 64);
++ //if(x==1)
++ // printk("reg 64 of %x is 0x%x\n", x, wc_dev->reg1shadow[x]);
++ }
++ else if (wc_dev->modtype[x] == MOD_TYPE_FXO)
++ wc_dev->reg1shadow[x] = a24xx_spi_getreg(wc_dev, x, 29);
++ break;
++ case 12:
++ /* Perform processing */
++ if (wc_dev->modtype[x] == MOD_TYPE_FXS) {
++ a24xx_proslic_check_hook(wc, x);
++ if (!(wc_dev->intcount & 0xf0)) {
++ si321x_proslic_recheck_sanity(wc_dev, x);
++ }
++ } else if (wc_dev->modtype[x] == MOD_TYPE_FXO) {
++ a24xx_voicedaa_check_hook(wc, x);
++ }
++ break;
++ }
++ }
++ }
++ break;
++ case 2:
++ z = wc_dev->intcount & 0x1;
++ mode = wc_dev->intcount & 0x6;
++ for(y = 0; y < wc_dev->max_cards / 2; y++) { /* do some detect operate every 4ms */
++ x = z + y*2;
++ if (wc_dev->cardflag & (1 << x ) ) {
++ switch(mode) {
++ case 0:
++ /* Rest */
++ break;
++ case 2:
++ /* Read first shadow reg */
++ if (wc_dev->modtype[x] == MOD_TYPE_FXS) {
++ wc_dev->reg0shadow[x] = a24xx_spi_getreg(wc_dev, x, 68);
++ //if(x==0)
++ //printk("reg 68 of %x is 0x%x\n", x, wc_dev->reg0shadow[x]);
++ }
++ else if (wc_dev->modtype[x] == MOD_TYPE_FXO)
++ wc_dev->reg0shadow[x] = a24xx_spi_getreg(wc_dev, x, 5);
++ break;
++ case 4:
++ /* Read second shadow reg */
++ if (wc_dev->modtype[x] == MOD_TYPE_FXS) {
++ wc_dev->reg1shadow[x] = a24xx_spi_getreg(wc_dev, x, 64);
++ //if(x==1)
++ // printk("reg 64 of %x is 0x%x\n", x, wc_dev->reg1shadow[x]);
++ }
++ else if (wc_dev->modtype[x] == MOD_TYPE_FXO)
++ wc_dev->reg1shadow[x] = a24xx_spi_getreg(wc_dev, x, 29);
++ break;
++ case 6:
++ /* Perform processing */
++ if (wc_dev->modtype[x] == MOD_TYPE_FXS) {
++ a24xx_proslic_check_hook(wc, x);
++ if (!(wc_dev->intcount & (0xf0>>1))) {
++ si321x_proslic_recheck_sanity(wc_dev, x);
++ }
++ } else if (wc_dev->modtype[x] == MOD_TYPE_FXO) {
++ a24xx_voicedaa_check_hook(wc, x);
++ }
++ break;
++ }
++ }
++ }
++ break;
++ case 4:
++ mode = wc_dev->intcount & 0x3;
++ for(x=0;x<wc_dev->max_cards;x++){
++ if (wc_dev->cardflag & (1 << x ) ) {
++ switch(mode) {
++ case 0:
++ /* Rest */
++ break;
++ case 1:
++ /* Read first shadow reg */
++ if (wc_dev->modtype[x] == MOD_TYPE_FXS) {
++ wc_dev->reg0shadow[x] = a24xx_spi_getreg(wc_dev, x, 68);
++ //if(x==0)
++ //printk("reg 68 of %x is 0x%x\n", x, wc_dev->reg0shadow[x]);
++ }
++ else if (wc_dev->modtype[x] == MOD_TYPE_FXO)
++ wc_dev->reg0shadow[x] = a24xx_spi_getreg(wc_dev, x, 5);
++ break;
++ case 2:
++ /* Read second shadow reg */
++ if (wc_dev->modtype[x] == MOD_TYPE_FXS) {
++ wc_dev->reg1shadow[x] = a24xx_spi_getreg(wc_dev, x, 64);
++ //if(x==1)
++ // printk("reg 64 of %x is 0x%x\n", x, wc_dev->reg1shadow[x]);
++ }
++ else if (wc_dev->modtype[x] == MOD_TYPE_FXO)
++ wc_dev->reg1shadow[x] = a24xx_spi_getreg(wc_dev, x, 29);
++ break;
++ case 3:
++ /* Perform processing */
++ if (wc_dev->modtype[x] == MOD_TYPE_FXS) {
++ a24xx_proslic_check_hook(wc, x);
++ if (!(wc_dev->intcount & (0xf0>>2))) {
++ si321x_proslic_recheck_sanity(wc_dev, x);
++ }
++ }
++ else if (wc_dev->modtype[x] == MOD_TYPE_FXO) {
++ a24xx_voicedaa_check_hook(wc, x);
++ }
++ break;
++ }
++ }
++ }
++ break;
++ case 8:
++ mode = wc_dev->intcount & 0x1;
++ for(x=0;x<wc_dev->max_cards;x++){
++ if (wc_dev->cardflag & (1 << x ) ) {
++ switch(mode) {
++ case 1:
++ if (wc_dev->modtype[x] == MOD_TYPE_FXS) {
++ wc_dev->reg0shadow[x] = a24xx_spi_getreg(wc_dev, x, 68);
++ wc_dev->reg1shadow[x] = a24xx_spi_getreg(wc_dev, x, 64);
++ }
++ else if (wc_dev->modtype[x] == MOD_TYPE_FXO){
++ wc_dev->reg0shadow[x] = a24xx_spi_getreg(wc_dev, x, 5);
++ wc_dev->reg1shadow[x] = a24xx_spi_getreg(wc_dev, x, 29);
++ }
++ break;
++ case 0:
++ /* Perform processing */
++ if (wc_dev->modtype[x] == MOD_TYPE_FXS) {
++ a24xx_proslic_check_hook(wc, x);
++ if (!(wc_dev->intcount & (0xf0>>3))) {
++ si321x_proslic_recheck_sanity(wc_dev, x);
++ }
++ }
++ else if (wc_dev->modtype[x] == MOD_TYPE_FXO) {
++ a24xx_voicedaa_check_hook(wc, x);
++ }
++ break;
++ }
++ }
++ }
++ break;
++ case 16:
++ for(x=0;x<wc_dev->max_cards;x++){
++ if (wc_dev->cardflag & (1 << x ) ) {
++ if (wc_dev->modtype[x] == MOD_TYPE_FXS) {
++ wc_dev->reg0shadow[x] = a24xx_spi_getreg(wc_dev, x, 68);
++ wc_dev->reg1shadow[x] = a24xx_spi_getreg(wc_dev, x, 64);
++ a24xx_proslic_check_hook(wc, x);
++ if (!(wc_dev->intcount & (0xf0>>4))) {
++ si321x_proslic_recheck_sanity(wc_dev, x);
++ }
++ }
++ else if (wc_dev->modtype[x] == MOD_TYPE_FXO){
++ wc_dev->reg0shadow[x] = a24xx_spi_getreg(wc_dev, x, 5);
++ wc_dev->reg1shadow[x] = a24xx_spi_getreg(wc_dev, x, 29);
++ a24xx_voicedaa_check_hook(wc, x);
++ }
++ }
++ }
++ break;
++ }
++
++ if (!(wc_dev->intcount % ( 10000/ms_per_irq ))) {
++ /* Accept an alarm once per 10 seconds */
++ for (x=0;x<wc_dev->max_cards;x++)
++ if (wc_dev->modtype[x] == MOD_TYPE_FXS) {
++ if (wc_dev->mod[x].fxs.palarms) {
++ wc_dev->mod[x].fxs.palarms--;
++ }
++ }
++ }
++ /**/
++ for(order=0;order < ms_per_irq;order++){
++ dahdi_transmit(&wc->span);
++ parser_busy_silent_process(wc, 1);
++ a24xx_transmit(wc, order);
++
++ a24xx_receive(wc, order);
++ parser_busy_silent_process(wc, 0);
++ dahdi_receive(&wc->span);
++ }
++ }
++ return 0;
++}
++
++DAHDI_IRQ_HANDLER(a24xx_interrupt)
++{
++ /**/
++ struct a24xx *wc = dev_id;
++ struct a24xx_dev *wc_dev = &wc->dev;
++ int i;
++
++ if(!wc_dev->master){
++ interrupt_onecard_handler(wc);
++ }else{
++ if(irq_stub){
++ for(i=0;i<max_iface_index;i++){
++ wc = ifaces[i];
++ if(wc){
++ wc_dev = &wc->dev;
++ if( wc_dev->fwversion >= ((1 << 16)|2) ){
++ interrupt_onecard_handler(wc);
++ }
++ }
++ }
++ }else
++ interrupt_onecard_handler(wc);
++ }
++
++
++ return IRQ_RETVAL(1);
++}
++#endif
++/* Must be called from within an interruptible context */
++static int set_vmwi(struct a24xx *wc, int chan_idx)
++{
++ struct a24xx_dev *wc_dev = &wc->dev;
++ struct fxs *const fxs = &wc_dev->mod[chan_idx].fxs;
++
++ if (fxs->vmwi_active_messages) {
++ fxs->vmwi_lrev =
++ (fxs->vmwisetting.vmwi_type & DAHDI_VMWI_LREV) ? 1 : 0;
++ fxs->vmwi_hvdc =
++ (fxs->vmwisetting.vmwi_type & DAHDI_VMWI_HVDC) ? 1 : 0;
++ fxs->vmwi_hvac =
++ (fxs->vmwisetting.vmwi_type & DAHDI_VMWI_HVAC) ? 1 : 0;
++ } else {
++ fxs->vmwi_lrev = 0;
++ fxs->vmwi_hvdc = 0;
++ fxs->vmwi_hvac = 0;
++ }
++
++ if (debug) {
++ printk(KERN_DEBUG "Setting VMWI on channel %d, messages=%d, "
++ "lrev=%d, hvdc=%d, hvac=%d\n",
++ chan_idx,
++ fxs->vmwi_active_messages,
++ fxs->vmwi_lrev,
++ fxs->vmwi_hvdc,
++ fxs->vmwi_hvac
++ );
++ }
++
++ if (fxs->vmwi_hvac) {
++ /* Can't change ring generator while in On Hook Transfer mode*/
++ if (!fxs->ohttimer) {
++ if (POLARITY_XOR(chan_idx))
++ fxs->idletxhookstate |= SLIC_LF_REVMASK;
++ else
++ fxs->idletxhookstate &= ~SLIC_LF_REVMASK;
++ /* Set ring generator for neon */
++ si321x_set_ring_generator_mode(wc_dev, chan_idx, 1);
++ /* Activate ring to send neon pulses */
++ fxs->lasttxhook = SLIC_LF_RINGING;
++ a24xx_spi_setreg(wc_dev, chan_idx, LINE_STATE, fxs->lasttxhook);
++ }
++ } else {
++ if (fxs->neonringing) {
++ /* Set ring generator for normal ringer */
++ si321x_set_ring_generator_mode(wc_dev, chan_idx, 0);
++ /* ACTIVE, polarity determined later */
++ fxs->lasttxhook = SLIC_LF_ACTIVE_FWD;
++ } else if ((fxs->lasttxhook == SLIC_LF_RINGING) ||
++ (fxs->lasttxhook == SLIC_LF_OPEN)) {
++ /* Can't change polarity while ringing or when open,
++ set idlehookstate instead */
++ if (POLARITY_XOR(chan_idx))
++ fxs->idletxhookstate |= SLIC_LF_REVMASK;
++ else
++ fxs->idletxhookstate &= ~SLIC_LF_REVMASK;
++ if(debug)
++ printk(KERN_DEBUG "Unable to change polarity on channel"
++ "%d, lasttxhook=0x%X\n",
++ chan_idx,
++ fxs->lasttxhook
++ );
++ return 0;
++ }
++ if (POLARITY_XOR(chan_idx)) {
++ fxs->idletxhookstate |= SLIC_LF_REVMASK;
++ fxs->lasttxhook |= SLIC_LF_REVMASK;
++ } else {
++ fxs->idletxhookstate &= ~SLIC_LF_REVMASK;
++ fxs->lasttxhook &= ~SLIC_LF_REVMASK;
++ }
++ a24xx_spi_setreg(wc_dev, chan_idx, LINE_STATE, fxs->lasttxhook);
++ }
++ return 0;
++}
++
++static int a24xx_ioctl(struct dahdi_chan *chan, unsigned int cmd, unsigned long data)
++{
++ struct wctdm_stats stats;
++ struct wctdm_regs regs;
++ struct wctdm_regop regop;
++ struct wctdm_echo_coefs echoregs;
++ struct dahdi_hwgain hwgain;
++ struct a24xx *wc = chan->pvt;
++ struct a24xx_dev *wc_dev = &wc->dev;
++ int x;
++
++ struct fxs *const fxs = &wc_dev->mod[chan->chanpos - 1].fxs;
++
++ switch (cmd) {
++ case DAHDI_ONHOOKTRANSFER:
++ if (wc_dev->modtype[chan->chanpos - 1] != MOD_TYPE_FXS) {
++ return -EINVAL;
++ }
++ if (get_user(x, (__user int *)data)) {
++ return -EFAULT;
++ }
++#if 0
++ /**/
++ wc_dev->mod[chan->chanpos - 1].fxs.ohttimer = x << 3;
++ if (reversepolarity) {
++ wc_dev->mod[chan->chanpos - 1].fxs.idletxhookstate = 0x6; /* OHT mode when idle */
++ } else {
++ wc_dev->mod[chan->chanpos - 1].fxs.idletxhookstate = 0x2;
++ }
++ if (wc_dev->mod[chan->chanpos - 1].fxs.lasttxhook == 0x1 || wc_dev->mod[chan->chanpos - 1].fxs.lasttxhook == 0x5) {
++ /* Apply the change if appropriate */
++ if (reversepolarity) {
++ wc_dev->mod[chan->chanpos - 1].fxs.lasttxhook = 0x6;
++ } else {
++ wc_dev->mod[chan->chanpos - 1].fxs.lasttxhook = 0x2;
++ }
++ a24xx_spi_setreg(wc_dev, chan->chanpos - 1, 64, wc_dev->mod[chan->chanpos - 1].fxs.lasttxhook);
++ }
++#endif
++#if 1
++ /* Active mode when idle */
++ wc_dev->mod[chan->chanpos - 1].fxs.idletxhookstate = POLARITY_XOR(chan->chanpos - 1) ?
++ SLIC_LF_ACTIVE_REV : SLIC_LF_ACTIVE_FWD;
++ if (wc_dev->mod[chan->chanpos - 1].fxs.neonringing) {
++ /* keep same Forward polarity */
++ wc_dev->mod[chan->chanpos - 1].fxs.lasttxhook = SLIC_LF_OHTRAN_FWD;
++ //printk(KERN_INFO "ioctl: Start OnHookTrans, card %d\n",chan->chanpos - 1);
++ a24xx_spi_setreg(wc_dev, chan->chanpos - 1,
++ LINE_STATE, wc_dev->mod[chan->chanpos - 1].fxs.lasttxhook);
++ } else if (wc_dev->mod[chan->chanpos - 1].fxs.lasttxhook == SLIC_LF_ACTIVE_FWD ||
++ wc_dev->mod[chan->chanpos - 1].fxs.lasttxhook == SLIC_LF_ACTIVE_REV) {
++ /* Apply the change if appropriate */
++ wc_dev->mod[chan->chanpos - 1].fxs.lasttxhook = POLARITY_XOR(chan->chanpos - 1) ?
++ SLIC_LF_OHTRAN_REV : SLIC_LF_OHTRAN_FWD;
++ //printk(KERN_INFO "ioctl: Start OnHookTrans, card %d\n",chan->chanpos - 1);
++ a24xx_spi_setreg(wc_dev, chan->chanpos - 1,
++ LINE_STATE, wc_dev->mod[chan->chanpos - 1].fxs.lasttxhook);
++ }
++#endif
++ break;
++#if 1
++ case DAHDI_VMWI_CONFIG:
++ if (wc_dev->modtype[chan->chanpos - 1] != MOD_TYPE_FXS)
++ return -EINVAL;
++ if (copy_from_user(&(fxs->vmwisetting),
++ (__user void *)data,
++ sizeof(fxs->vmwisetting)))
++ return -EFAULT;
++ //printk("--opvxa24xx:DAHDI_VMWI_CONFIG,card=%d--\n",chan->chanpos - 1);
++ set_vmwi(wc, chan->chanpos - 1);
++ break;
++ case DAHDI_VMWI:
++ if (wc_dev->modtype[chan->chanpos - 1] != MOD_TYPE_FXS)
++ return -EINVAL;
++ if (get_user(x, (__user int *) data))
++ return -EFAULT;
++ if (0 > x)
++ return -EFAULT;
++ fxs->vmwi_active_messages = x;
++ set_vmwi(wc, chan->chanpos - 1);
++ //printk("-----opvxa24xx,DAHDI_VMWI,card=%d,set_vmwi------\n",chan->chanpos - 1);
++ break;
++#endif
++ case DAHDI_SETPOLARITY:
++ if (get_user(x, (__user int *)data)) {
++ return -EFAULT;
++ }
++ if (wc_dev->modtype[chan->chanpos - 1] != MOD_TYPE_FXS) {
++ return -EINVAL;
++ }
++ /* Can't change polarity while ringing or when open */
++ if ((wc_dev->mod[chan->chanpos -1 ].fxs.lasttxhook == SLIC_LF_RINGING) ||
++ (wc_dev->mod[chan->chanpos -1 ].fxs.lasttxhook == SLIC_LF_OPEN)) {
++ return -EINVAL;
++ }
++ wc_dev->mod[chan->chanpos -1 ].fxs.reversepolarity = x;
++#if 0
++ if ((x && !reversepolarity) || (!x && reversepolarity)) {
++ wc_dev->mod[chan->chanpos - 1].fxs.lasttxhook |= 0x04;
++ } else {
++ wc_dev->mod[chan->chanpos - 1].fxs.lasttxhook &= ~0x04;
++ }
++#endif
++ if (POLARITY_XOR(chan->chanpos - 1)) {
++ wc_dev->mod[chan->chanpos -1 ].fxs.lasttxhook |= SLIC_LF_REVMASK;
++ printk(KERN_INFO "ioctl: Reverse Polarity, card %d\n",
++ chan->chanpos - 1);
++ } else {
++ wc_dev->mod[chan->chanpos -1 ].fxs.lasttxhook &= ~SLIC_LF_REVMASK;
++ printk(KERN_INFO "ioctl: Normal Polarity, card %d\n",
++ chan->chanpos - 1);
++ }
++ a24xx_spi_setreg(wc_dev, chan->chanpos - 1, LINE_STATE, wc_dev->mod[chan->chanpos - 1].fxs.lasttxhook);
++ break;
++ case WCTDM_GET_STATS:
++ if (wc_dev->modtype[chan->chanpos - 1] == MOD_TYPE_FXS) {
++ stats.tipvolt = a24xx_spi_getreg(wc_dev, chan->chanpos - 1, 80) * -376;
++ stats.ringvolt = a24xx_spi_getreg(wc_dev, chan->chanpos - 1, 81) * -376;
++ stats.batvolt = a24xx_spi_getreg(wc_dev, chan->chanpos - 1, 82) * -376;
++ } else if (wc_dev->modtype[chan->chanpos - 1] == MOD_TYPE_FXO) {
++ stats.tipvolt = (signed char)a24xx_spi_getreg(wc_dev, chan->chanpos - 1, 29) * 1000;
++ stats.ringvolt = (signed char)a24xx_spi_getreg(wc_dev, chan->chanpos - 1, 29) * 1000;
++ stats.batvolt = (signed char)a24xx_spi_getreg(wc_dev, chan->chanpos - 1, 29) * 1000;
++ } else {
++ return -EINVAL;
++ }
++ if (copy_to_user((__user void*)data, &stats, sizeof(stats))) {
++ return -EFAULT;
++ }
++ break;
++ case WCTDM_GET_REGS:
++ if (wc_dev->modtype[chan->chanpos - 1] == MOD_TYPE_FXS) {
++ for (x=0;x<NUM_INDIRECT_REGS;x++) {
++ regs.indirect[x] = si321x_proslic_getreg_indirect(wc_dev, chan->chanpos -1, x);
++ }
++ for (x=0;x<NUM_REGS;x++) {
++ regs.direct[x] = a24xx_spi_getreg(wc_dev, chan->chanpos - 1, x);
++ }
++ } else {
++ memset(&regs, 0, sizeof(regs));
++ for (x=0;x<NUM_FXO_REGS;x++)
++ regs.direct[x] = a24xx_spi_getreg(wc_dev, chan->chanpos - 1, x);
++ }
++ if (copy_to_user((__user void *)data, &regs, sizeof(regs))) {
++ return -EFAULT;
++ }
++ break;
++ case WCTDM_SET_REG:
++ if (copy_from_user(&regop, (__user void *)data, sizeof(regop))) {
++ return -EFAULT;
++ }
++ if (regop.indirect) {
++ if (wc_dev->modtype[chan->chanpos - 1] != MOD_TYPE_FXS) {
++ return -EINVAL;
++ }
++ printk(KERN_INFO "Setting indirect %d to 0x%04x on %d\n", regop.reg, regop.val, chan->chanpos);
++ si321x_proslic_setreg_indirect(wc_dev, chan->chanpos - 1, regop.reg, regop.val);
++ } else {
++ regop.val &= 0xff;
++ printk(KERN_INFO "Setting direct %d to %04x on %d\n", regop.reg, regop.val, chan->chanpos);
++ a24xx_spi_setreg(wc_dev, chan->chanpos - 1, regop.reg, regop.val);
++ }
++ break;
++ case WCTDM_SET_ECHOTUNE:
++ printk(KERN_INFO "-- Setting echo registers: \n");
++ if (copy_from_user(&echoregs, (__user void *)data, sizeof(echoregs))) {
++ return -EFAULT;
++ }
++
++ if (wc_dev->modtype[chan->chanpos - 1] == MOD_TYPE_FXO) {
++ /* Set the ACIM register */
++ a24xx_spi_setreg(wc_dev, chan->chanpos - 1, 30, (fxofullscale==1) ? (echoregs.acim|0x10) : echoregs.acim);
++
++ /* Set the digital echo canceller registers */
++ a24xx_spi_setreg(wc_dev, chan->chanpos - 1, 45, echoregs.coef1);
++ a24xx_spi_setreg(wc_dev, chan->chanpos - 1, 46, echoregs.coef2);
++ a24xx_spi_setreg(wc_dev, chan->chanpos - 1, 47, echoregs.coef3);
++ a24xx_spi_setreg(wc_dev, chan->chanpos - 1, 48, echoregs.coef4);
++ a24xx_spi_setreg(wc_dev, chan->chanpos - 1, 49, echoregs.coef5);
++ a24xx_spi_setreg(wc_dev, chan->chanpos - 1, 50, echoregs.coef6);
++ a24xx_spi_setreg(wc_dev, chan->chanpos - 1, 51, echoregs.coef7);
++ a24xx_spi_setreg(wc_dev, chan->chanpos - 1, 52, echoregs.coef8);
++
++ printk(KERN_INFO "-- Set echo registers successfully\n");
++
++ break;
++ } else {
++ return -EINVAL;
++ }
++ break;
++ case DAHDI_SET_HWGAIN:
++ if (copy_from_user(&hwgain, (__user void *) data, sizeof(hwgain))) {
++ return -EFAULT;
++ }
++
++ si3050_set_hwgain(wc_dev, chan->chanpos-1, hwgain.newgain, hwgain.tx);
++
++ if (debug) {
++ printk(KERN_DEBUG "Setting hwgain on channel %d to %d for %s direction\n",
++ chan->chanpos-1, hwgain.newgain, hwgain.tx ? "tx" : "rx");
++ }
++ break;
++ default:
++ return -ENOTTY;
++ }
++ return 0;
++
++}
++
++static int a24xx_open(struct dahdi_chan *chan)
++{
++ int channo;
++
++ struct a24xx *wc = chan->pvt;
++ struct a24xx_dev *wc_dev = &wc->dev;
++
++ channo = chan->chanpos - 1;
++
++ if (!(wc_dev->cardflag & (1 << (chan->chanpos - 1)))) {
++ return -ENODEV;
++ }
++ if (wc_dev->dead) {
++ return -ENODEV;
++ }
++ wc_dev->usecount++;
++
++ try_module_get(THIS_MODULE);
++
++ return 0;
++}
++
++static inline struct a24xx *a24xx_from_span(struct dahdi_span *span)
++{
++ return container_of(span, struct a24xx, span);
++}
++
++static int a24xx_watchdog(struct dahdi_span *span, int event)
++{
++ struct a24xx *wc = a24xx_from_span(span);
++ struct a24xx_dev *wc_dev = &wc->dev;
++
++ printk(KERN_INFO "opvxa24xx: Restarting DMA\n");
++ __opvx_a24xx_restart_dma(wc_dev->mem32);
++
++ return 0;
++}
++
++static int a24xx_close(struct dahdi_chan *chan)
++{
++ struct a24xx *wc = chan->pvt;
++ struct a24xx_dev *wc_dev = &wc->dev;
++
++ wc_dev->usecount--;
++
++ module_put(THIS_MODULE);
++
++ if (wc_dev->modtype[chan->chanpos - 1] == MOD_TYPE_FXS) {
++ /*
++ if (reversepolarity) {
++ wc_dev->mod[chan->chanpos - 1].fxs.idletxhookstate = 5;
++ }
++ else {
++ wc_dev->mod[chan->chanpos - 1].fxs.idletxhookstate = 1;
++ }*/
++ int idlehookstate;
++ idlehookstate = POLARITY_XOR(chan->chanpos - 1)?
++ SLIC_LF_ACTIVE_REV :
++ SLIC_LF_ACTIVE_FWD;
++ wc_dev->mod[chan->chanpos - 1].fxs.idletxhookstate = idlehookstate;
++ }
++ /* If we're dead, release us now */
++ if (!wc_dev->usecount && wc_dev->dead) {
++ a24xx_release(wc);
++ }
++ return 0;
++}
++
++static int a24xx_hooksig(struct dahdi_chan *chan, enum dahdi_txsig txsig)
++{
++ int reg=0;
++ struct a24xx *wc = chan->pvt;
++ struct a24xx_dev *wc_dev = &wc->dev;
++
++ if (wc_dev->modtype[chan->chanpos - 1] == MOD_TYPE_FXO) {
++ /* XXX Enable hooksig for FXO XXX */
++ switch(txsig) {
++ case DAHDI_TXSIG_START:
++ case DAHDI_TXSIG_OFFHOOK:
++ wc_dev->mod[chan->chanpos - 1].fxo.offhook = 1;
++ a24xx_spi_setreg(wc_dev, chan->chanpos - 1, 5, 0x9);
++ if(cidsupport) {
++ wc_dev->cid_state[chan->chanpos - 1] = CID_STATE_IDLE;
++ wc_dev->cid_history_clone_cnt[chan->chanpos - 1] = 0;
++ wc_dev->cid_history_ptr[chan->chanpos - 1] = 0;
++ memset(wc_dev->cid_history_buf[chan->chanpos - 1], DAHDI_LIN2X(0, chan), cidbuflen * DAHDI_MAX_CHUNKSIZE);
++ }
++ break;
++ case DAHDI_TXSIG_ONHOOK:
++ wc_dev->mod[chan->chanpos - 1].fxo.offhook = 0;
++ a24xx_spi_setreg(wc_dev, chan->chanpos - 1, 5, 0x8);
++ break;
++ default:
++ printk(KERN_NOTICE "wcfxo: Can't set tx state to %d\n", txsig);
++ }
++ } else {
++ switch(txsig) {
++ case DAHDI_TXSIG_ONHOOK:
++ switch(chan->sig) {
++ case DAHDI_SIG_EM:
++ case DAHDI_SIG_FXOKS:
++ case DAHDI_SIG_FXOLS:
++ //wc_dev->mod[chan->chanpos-1].fxs.lasttxhook = wc_dev->mod[chan->chanpos-1].fxs.idletxhookstate;
++ /* Can't change Ring Generator during OHT */
++ if (!wc_dev->mod[chan->chanpos-1].fxs.ohttimer) {
++ si321x_set_ring_generator_mode(wc_dev,
++ chan->chanpos-1, wc_dev->mod[chan->chanpos-1].fxs.vmwi_hvac);
++ wc_dev->mod[chan->chanpos-1].fxs.lasttxhook = wc_dev->mod[chan->chanpos-1].fxs.vmwi_hvac ? SLIC_LF_RINGING : wc_dev->mod[chan->chanpos-1].fxs.idletxhookstate;
++ } else {
++ wc_dev->mod[chan->chanpos-1].fxs.lasttxhook = wc_dev->mod[chan->chanpos-1].fxs.idletxhookstate;
++ }
++ break;
++ case DAHDI_SIG_FXOGS:
++ wc_dev->mod[chan->chanpos-1].fxs.lasttxhook = 3;
++ break;
++ }
++ break;
++ case DAHDI_TXSIG_OFFHOOK:
++ switch(chan->sig) {
++ case DAHDI_SIG_EM:
++ wc_dev->mod[chan->chanpos-1].fxs.lasttxhook = 5;
++ break;
++ default:
++ wc_dev->mod[chan->chanpos-1].fxs.lasttxhook = wc_dev->mod[chan->chanpos-1].fxs.idletxhookstate;
++ break;
++ }
++ break;
++ case DAHDI_TXSIG_START:
++ //wc_dev->mod[chan->chanpos-1].fxs.lasttxhook = 4;
++ si321x_set_ring_generator_mode(wc_dev,
++ chan->chanpos-1, 0);
++ wc_dev->mod[chan->chanpos-1].fxs.lasttxhook = SLIC_LF_RINGING;
++ break;
++ case DAHDI_TXSIG_KEWL:
++ wc_dev->mod[chan->chanpos-1].fxs.lasttxhook = 0;
++ break;
++ default:
++ printk(KERN_NOTICE "opvxa24xx: Can't set tx state to %d\n", txsig);
++ }
++ if (debug) {
++ printk(KERN_DEBUG "Setting FXS hook state to %d (%02x)\n", txsig, reg);
++ }
++
++#if 1
++ a24xx_spi_setreg(wc_dev, chan->chanpos - 1, 64, wc_dev->mod[chan->chanpos-1].fxs.lasttxhook);
++#endif
++ }
++ return 0;
++}
++
++#ifdef DAHDI_SPAN_OPS
++static const struct dahdi_span_ops a24xx_span_ops = {
++ .owner = THIS_MODULE,
++ .hooksig = a24xx_hooksig,
++ .open = a24xx_open,
++ .close = a24xx_close,
++ .ioctl = a24xx_ioctl,
++ .watchdog = a24xx_watchdog,
++#ifdef VPM_SUPPORT
++ .echocan_create = a24xx_echocan_create,
++#endif
++
++};
++#endif
++
++static int a24xx_init_spans(struct a24xx *wc)
++{
++ int x;
++ struct a24xx_dev *wc_dev = &(wc->dev);
++
++ /* Zapata stuff */
++ sprintf(wc->span.name, "OPVXA24XX/%d", wc_dev->pos);
++ snprintf(wc->span.desc, sizeof(wc->span.desc)-1, "%s Board %d", wc_dev->variety, wc_dev->pos + 1);
++// snprintf(wc->span.location, sizeof(wc->span.location) - 1,
++// "PCI Bus %02d Slot %02d", wc_dev->dev->bus->number, PCI_SLOT(wc_dev->dev->devfn) + 1);
++ wc_dev->ddev->location = kasprintf(GFP_KERNEL,
++ "PCI Bus %02d Slot %02d",
++ wc_dev->dev->bus->number,
++ PCI_SLOT(wc_dev->dev->devfn) + 1);
++
++ //printk("wc is 0x%x, wc_dev ix 0x%x, ddev is 0x%x\n", wc, &(wc->dev), wc_dev->ddev);
++ if (!wc_dev->ddev->location) {
++ dahdi_free_device(wc_dev->ddev);
++ wc_dev->ddev = NULL;
++ return -ENOMEM;
++ }
++
++ wc_dev->ddev->manufacturer = "OpenVox"; //Dennis
++ wc_dev->ddev->devicetype = wc_dev->variety;
++
++ if (alawoverride) {
++ printk(KERN_INFO "ALAW override parameter detected. Device will be operating in ALAW\n");
++ wc->span.deflaw = DAHDI_LAW_ALAW;
++ } else {
++ wc->span.deflaw = DAHDI_LAW_MULAW;
++ }
++
++ for (x = 0; x < wc_dev->max_cards/*MAX_NUM_CARDS*/; x++) {
++ sprintf(wc->chans[x]->name, "OPVXA24XX/%d/%d", wc_dev->pos, x);
++ wc->chans[x]->sigcap = DAHDI_SIG_FXOKS | DAHDI_SIG_FXOLS | DAHDI_SIG_FXOGS | DAHDI_SIG_SF | DAHDI_SIG_EM | DAHDI_SIG_CLEAR;
++ wc->chans[x]->sigcap |= DAHDI_SIG_FXSKS | DAHDI_SIG_FXSLS | DAHDI_SIG_SF | DAHDI_SIG_CLEAR;
++ wc->chans[x]->chanpos = x+1;
++ wc->chans[x]->pvt = wc;
++ }
++
++#ifdef DAHDI_SPAN_MODULE
++ wc->span.owner = THIS_MODULE;
++#endif
++#ifdef DAHDI_SPAN_OPS
++ wc->span.ops = &a24xx_span_ops;
++#else
++ wc->span.hooksig = a24xx_hooksig;
++ wc->span.open = a24xx_open;
++ wc->span.close = a24xx_close;
++ wc->span.ioctl = a24xx_ioctl;
++ wc->span.watchdog = a24xx_watchdog;
++#ifdef VPM_SUPPORT
++ if (vpmsupport)
++ wc->span.echocan_create = a24xx_echocan_create;
++#endif
++ wc->span.pvt = wc;
++#endif
++ wc->span.chans = wc->chans;
++ wc->span.channels = wc_dev->max_cards; /*MAX_NUM_CARDS;*/
++ wc->span.flags = DAHDI_FLAG_RBS;
++// init_waitqueue_head(&wc->span.maintq);
++ list_add_tail(&wc->span.device_node, &wc->dev.ddev->spans);
++// init_waitqueue_head(&wc->regq)
++
++ if (dahdi_register_device(wc_dev->ddev, &wc->dev.dev->dev)) {
++ printk(KERN_NOTICE "Unable to register span with Dahdi\n");
++ kfree(wc_dev->ddev->location);
++ dahdi_free_device(wc_dev->ddev);
++ wc_dev->ddev = NULL;
++ printk("dahdi_register_device fail\n");
++ mdelay(3000);
++ return -1;
++ }
++ return 0;
++}
++
++static int __devinit a24xx_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
++{
++ int res;
++ int x;
++ int y;
++ struct a24xx *wc;
++ struct a24xx_desc *d = (struct a24xx_desc *)ent->driver_data;
++ struct a24xx_dev *wc_dev;
++ unsigned int fwbuild;
++ char span_flags[MAX_NUM_CARDS] = { 0, };
++ int z,index;
++ static int initd_ifaces=0;
++
++ if(!initd_ifaces){
++ memset((void *)ifaces,0,(sizeof(struct a24xx *))*WC_MAX_IFACES);
++ initd_ifaces=1;
++ }
++ for (x = 0; x < WC_MAX_IFACES; x++) {
++ if (!ifaces[x]) {
++ break;
++ }
++ }
++ if (x >= WC_MAX_IFACES) {
++ printk("Too many interfaces\n");
++ return -EIO;
++ }
++ index = x;
++
++ if (pci_enable_device(pdev)) {
++ res = -EIO;
++ } else {
++ int cardcount = 0;
++
++ wc = kmalloc(sizeof(struct a24xx), GFP_KERNEL);
++ if(!wc)
++ return -ENOMEM;
++
++ memset(wc, 0, sizeof(struct a24xx));
++ wc->dev.ddev = dahdi_create_device();
++#if 0
++ printk("wc is 0x%x, ddev is 0x%x\n", wc, wc->dev.ddev);
++#endif
++ if (!wc->dev.ddev) {
++ kfree(wc);
++ return -ENOMEM;
++ }
++ wc_dev = &wc->dev;
++ wc_dev->ledstate = 0;
++
++ for (x=0; x < sizeof(wc->chans)/sizeof(wc->chans[0]); ++x) {
++ wc->chans[x] = &wc->_chans[x];
++ }
++
++ spin_lock_init(&wc_dev->lock);
++ wc_dev->curcard = -1;
++
++ if(ent->device == 0x0810)
++ wc_dev->max_cards = 8;
++ else if(ent->device == 0x1610)
++ wc_dev->max_cards = 16;
++ else
++ wc_dev->max_cards = 24;
++
++ wc_dev->mem_region = pci_resource_start(pdev, 0);
++ wc_dev->mem_len = pci_resource_len(pdev, 0);
++ wc_dev->mem32 = (unsigned long)ioremap(wc_dev->mem_region, wc_dev->mem_len);
++ wc_dev->dev = pdev;
++ wc_dev->pos = x;
++ wc_dev->variety = d->name;
++ for (y = 0; y < wc_dev->max_cards; y++) {
++ wc_dev->flags[y] = d->flags;
++ }
++
++ /* Keep track of whether we need to free the region */
++ if (request_mem_region(wc_dev->mem_region, wc_dev->mem_len, "opvxa24xx")) {
++ wc_dev->freeregion = 1;
++ }
++
++ if(debug) {
++ printk("======= find a card @ mem32 0x%x, size %ud\n", (unsigned int)wc_dev->mem32, (unsigned int)wc_dev->mem_len);
++ }
++#if 0
++ printk("======= manual exit\n");
++ if(wc_dev->freeregion) {
++ release_mem_region(wc_dev->mem_region, wc_dev->mem_len);
++ iounmap((void *)wc_dev->mem32);
++ return -ENOMEM;
++ }
++#endif
++
++ wc_dev->fwversion = __opvx_a24xx_get_version(wc_dev->mem32);
++ if(wc_dev->max_cards == 24)
++ wc_dev->card_name = A2410P_Name;
++ else if(wc_dev->max_cards == 16)
++ wc_dev->card_name = A1610P_Name;
++ else if(wc_dev->max_cards == 8)
++ wc_dev->card_name = A810P_Name;
++
++ if(wc_dev->fwversion < ((1 << 16)|1) )
++ ms_per_irq = 1;
++
++ if((ms_per_irq != 1)&&
++ (ms_per_irq != 2)&&
++ (ms_per_irq != 4)&&
++ (ms_per_irq != 8)&&
++ (ms_per_irq != 16))
++ ms_per_irq = 1;
++
++ if(wc_dev->fwversion > ((1 << 16)|2)){
++ if(!index)
++ wc_dev->master = 1; //master card
++ else
++ wc_dev->master = 0;
++ if(!irq_stub)
++ wc_dev->master = 1;
++ }else
++ wc_dev->master = 0;
++
++ wc->index = index;
++
++ /* Allocate enough memory for two dahdi chunks, receive and transmit. Each sample uses
++ 8 bits. */
++ wc_dev->writechunk = pci_alloc_consistent(pdev, ms_per_irq * DAHDI_MAX_CHUNKSIZE * MAX_NUM_CARDS * 2 * 2, &wc_dev->writedma);
++ if (!wc_dev->writechunk) {
++ printk("opvxa24xx: Unable to allocate DMA-able memory\n");
++ if (wc_dev->freeregion) {
++ release_mem_region(wc_dev->mem_region, wc_dev->mem_len);
++ iounmap((void *)wc_dev->mem32);
++ }
++ return -ENOMEM;
++ }
++
++ if(debug) {
++ printk("opvxa24xx: dma buffer allocated at 0x%x, pci(0x%x)\n", (unsigned int)wc_dev->writechunk, (unsigned int)wc_dev->writedma);
++ }
++
++ __a24xx_malloc_chunk(wc_dev,ms_per_irq);
++
++ if (a24xx_init_spans(wc)) {
++ printk(KERN_NOTICE "opvxa24xx: Unable to intialize hardware\n");
++ /* Free Resources */
++ if (wc_dev->freeregion) {
++ release_mem_region(wc_dev->mem_region, wc_dev->mem_len);
++ iounmap((void *)wc_dev->mem32);
++ }
++ pci_free_consistent(pdev, ms_per_irq * DAHDI_MAX_CHUNKSIZE * MAX_NUM_CARDS * 2 * 2, (void *)wc_dev->writechunk, wc_dev->writedma);
++ return -ENOMEM;
++ }
++
++ /* Enable bus mastering */
++ pci_set_master(pdev);
++
++ /* Keep track of which device we are */
++ pci_set_drvdata(pdev, wc);
++
++ /* kmalloc ec */
++ for (x = 0; x < wc_dev->max_cards; x++) {
++ if (!(wc->ec[x] = kmalloc(sizeof(*wc->ec[x]), GFP_KERNEL))) {
++ free_wc(wc);
++ return -ENOMEM;
++ }
++ }
++
++ /* init hardware */
++#if 0
++ a24xx_hardware_init(wc_dev, span_flags);
++
++ __opvx_a24xx_setcreg(wc_dev->mem32, 0x4a0, 7, spi_cmd );
++ int spi_cmd_from_fpga = __opvx_a24xx_getcreg(wc_dev->mem32, 0x4a0, 7);
++ printk("%s:current spi_cmd is 0x%x\n", __FUNCTION__ , spi_cmd_from_fpga );
++#endif
++
++ res = a24xx_hardware_init_all(wc_dev, span_flags);
++ if (res < 0) {
++ /* Free Resources */
++ if (wc_dev->freeregion) {
++ release_mem_region(wc_dev->mem_region, wc_dev->mem_len);
++ iounmap((void *)wc_dev->mem32);
++ }
++ pci_free_consistent(pdev, ms_per_irq * DAHDI_MAX_CHUNKSIZE * MAX_NUM_CARDS * 2 * 2, (void *)wc_dev->writechunk, wc_dev->writedma);
++ pci_set_drvdata(pdev, NULL);
++ dahdi_unregister_device(wc->dev.ddev); //Dennis
++ kfree(wc->dev.ddev->location);
++ dahdi_free_device(wc->dev.ddev);
++ free_wc(wc);
++ return -EIO;
++ }
++ for (z = 0; z < wc_dev->max_cards/*MAX_NUM_CARDS*/; z++) {
++ if (span_flags[z]) {
++ wc->chans[z]->sigcap = __DAHDI_SIG_FXO | DAHDI_SIG_BROKEN;
++ }
++ }
++
++ /* init ec module */
++ if (!wc_dev->vpm) {
++ a24xx_vpm_init(wc);
++ }
++
++ if(wc_dev->master || ( wc_dev->fwversion < ((1 << 16)|2) ) ){
++ if (request_irq(pdev->irq, a24xx_interrupt, DAHDI_IRQ_SHARED, "opvxa24xx"/*wc_dev->card_name*/, wc)) {
++ printk(KERN_NOTICE "Unable to request IRQ %d\n", pdev->irq);
++ if (wc_dev->freeregion) {
++ release_mem_region(wc_dev->mem_region, wc_dev->mem_len);
++ iounmap((void *)wc_dev->mem32);
++ }
++ pci_free_consistent(pdev, ms_per_irq * DAHDI_MAX_CHUNKSIZE * MAX_NUM_CARDS * 2 * 2, (void *)wc_dev->writechunk, wc_dev->writedma);
++ pci_set_drvdata(pdev, NULL);
++ free_wc(wc);
++ return -EIO;
++ }
++ }
++
++ if(0) { // for debug;
++
++ printk("exit after alloc irq\n");
++ /* Free Resources */
++ if(wc_dev->master || ( wc_dev->fwversion < ((1 << 16)|2) ))
++ free_irq(pdev->irq, wc);
++ if (wc_dev->freeregion) {
++ release_mem_region(wc_dev->mem_region, wc_dev->mem_len);
++ iounmap((void *)wc_dev->mem32);
++ }
++ pci_free_consistent(pdev, ms_per_irq * DAHDI_MAX_CHUNKSIZE * MAX_NUM_CARDS * 2 * 2, (void *)wc_dev->writechunk, wc_dev->writedma);
++ pci_set_drvdata(pdev, NULL);
++ dahdi_unregister_device(wc->dev.ddev); //Dennis
++ kfree(wc->dev.ddev->location);
++ dahdi_free_device(wc->dev.ddev);
++ free_wc(wc);
++ return -EIO;
++ }
++
++#ifdef TEST_LOG_INCOME_VOICE
++ for(x=0; x<wc_dev->max_cards; x++) {
++ wc_dev->voc_buf[x] = kmalloc(voc_buffer_size, GFP_KERNEL);
++ wc_dev->voc_ptr[x] = 0;
++ }
++#endif
++ if(cidsupport) {
++ int len = cidbuflen * DAHDI_MAX_CHUNKSIZE;
++ if(debug) {
++ printk("cid support enabled, length is %d msec\n", cidbuflen);
++ }
++ for (x = 0; x < wc_dev->max_cards/*MAX_NUM_CARDS*/; x++) {
++ wc_dev->cid_history_buf[x] = kmalloc(len, GFP_KERNEL);
++ wc_dev->cid_history_ptr[x] = 0;
++ wc_dev->cid_history_clone_cnt[x] = 0;
++ wc_dev->cid_state[x] = CID_STATE_IDLE;
++ }
++ }
++
++ /* set channel sigcap */
++ a24xx_post_initialize(wc);
++
++ init_busydetect(wc, opermode);
++ init_callerid(wc);
++
++ /* Enable interrupts */
++ __opvx_a24xx_enable_interrupts(wc_dev->mem32);
++
++ /* Initialize Write/Buffers to all blank data */
++ memset((void *)wc_dev->writechunk,0, ms_per_irq * DAHDI_MAX_CHUNKSIZE * MAX_NUM_CARDS * 2 * 2);
++
++ /*set irq frequence*/
++ if(wc_dev->fwversion >= ((1 << 16)|1) )
++ __opvx_a24xx_set_irq_frq(wc_dev->mem32,ms_per_irq);
++
++ if(wc_dev->fwversion > ((1 << 16)|2)){
++ __opvx_a24xx_set_master(wc_dev->mem32,wc_dev->master^0x01);
++ }
++ /* Start DMA */
++ __opvx_a24xx_start_dma(wc_dev->mem32, wc_dev->writedma);
++
++ /* module count */
++ for (x = 0; x < wc_dev->max_cards/*MAX_NUM_CARDS*/; x++) {
++ if (wc_dev->cardflag & (1 << x)) {
++ cardcount++;
++ }
++ }
++ fwbuild = __opvx_a24xx_getcreg(wc_dev->mem32, 0x0, 0xe);
++ printk(KERN_NOTICE "Found an OpenVox %s: Version %x.%x (%d modules),Build 0x%08x\n", wc_dev->card_name, wc_dev->fwversion>>16, wc_dev->fwversion&0xffff, cardcount,fwbuild );
++ if(debug) {
++ printk(KERN_DEBUG "OpenVox %s debug On\n", wc_dev->card_name);
++ }
++
++ ifaces[index] = wc;
++ max_iface_index++;
++
++ res = 0;
++ }
++
++ return res;
++}
++
++
++static void a24xx_release(struct a24xx *wc)
++{
++#ifdef TEST_LOG_INCOME_VOICE
++ struct file * f = NULL;
++ mm_segment_t orig_fs;
++ int i;
++ char fname[20];
++#endif
++ int y;
++ char *s;
++ struct a24xx_dev *wc_dev = &wc->dev;
++
++ /*Unregister dahdi_span*/
++ dahdi_unregister_device(wc_dev->ddev); //Dennis
++ /* Release mem region and iounmap mem */
++ if (wc_dev->freeregion) {
++ release_mem_region(wc_dev->mem_region, wc_dev->mem_len);
++ iounmap((void *)wc_dev->mem32);
++ }
++
++#ifdef TEST_LOG_INCOME_VOICE
++ for(i=0; i<wc_dev->max_cards; i++) {
++ sprintf(fname, "//usr//%d.pcm", i);
++ f = filp_open(fname, O_RDWR|O_CREAT, 00);
++
++ if (!f || !f->f_op || !f->f_op->read) {
++ printk("WARNING: File (read) object is a null pointer!!!\n");
++ continue;
++ }
++
++ f->f_pos = 0;
++
++ orig_fs = get_fs();
++ set_fs(KERNEL_DS);
++
++ if(wc_dev->voc_buf[i]) {
++ f->f_op->write(f, wc_dev->voc_buf[i], voc_buffer_size, &f->f_pos);
++ kfree(wc_dev->voc_buf[i]);
++ }
++
++ set_fs(orig_fs);
++ fput(f);
++ }
++#endif
++
++ /* Release cid history buffer */
++ if(cidsupport) {
++ int x;
++ for (x = 0; x < wc_dev->max_cards/*MAX_NUM_CARDS*/; x++) {
++ kfree(wc_dev->cid_history_buf[x]);
++ }
++ }
++
++ /* Release a24xx */
++ s = wc_dev->card_name;
++ for(y = 0; y < max_iface_index; y++)
++ if(ifaces[y] == wc)
++ break;
++
++
++ kfree(wc_dev->ddev->location); //Dennis
++ dahdi_free_device(wc_dev->ddev);
++ free_wc(wc);
++ ifaces[y]=NULL;
++
++ printk(KERN_INFO "Free an OpenVox %s card\n", s);
++}
++
++static void __devexit a24xx_remove_one(struct pci_dev *pdev)
++{
++ struct a24xx *wc = pci_get_drvdata(pdev);
++ struct a24xx_dev *wc_dev = &wc->dev;
++
++ if (wc) {
++ /* In case hardware is still there */
++ __opvx_a24xx_disable_interrupts(wc_dev->mem32);
++
++ /* Wait some time to handle the last irq */
++ __a24xx_wait_just_a_bit(HZ/10); /* delay 1/10 sec */
++
++ /* Stop any DMA */
++ __opvx_a24xx_stop_dma(wc_dev->mem32);
++
++ /* Reset tdm controller */
++ __opvx_a24xx_reset_tdm(wc_dev->mem32);
++
++ /* Release echo canceller */
++ if (wc_dev->vpm_ec) {
++ opvx_vpm_release(wc_dev->vpm_ec);
++ }
++ wc_dev->vpm_ec = NULL;
++
++ if(wc_dev->master || ( wc_dev->fwversion < ((1 << 16)|2) )){
++ free_irq(pdev->irq, wc);
++ }
++ /* Immediately free resources */
++ pci_free_consistent(pdev, ms_per_irq * DAHDI_MAX_CHUNKSIZE * MAX_NUM_CARDS * 2 * 2, (void *)wc_dev->writechunk, wc_dev->writedma);
++
++ destroy_callerid(wc);
++ destroy_busydetect(wc);
++
++ /* Release span, possibly delayed */
++ if (!wc_dev->usecount) {
++ a24xx_release(wc);
++ } else {
++ wc_dev->dead = 1;
++ }
++ }
++}
++
++static struct pci_device_id a24xx_pci_tbl[] = {
++ { 0x1B74, 0x2410, 0x1B74, 0x0001, 0, 0, (unsigned long) &a2410a }, // Information for A2410 revsion A.
++ { 0x1B74, 0x1610, 0x1B74, 0x0001, 0, 0, (unsigned long) &a1610a }, // Information for A1610 revsion A.
++ { 0x1B74, 0x0810, 0x1B74, 0x0001, 0, 0, (unsigned long) &a810a }, // Information for A810 revsion A.
++ { 0 }
++};
++
++MODULE_DEVICE_TABLE(pci, a24xx_pci_tbl);
++
++static struct pci_driver a24xx_driver = {
++ .name = "opvxa24xx",
++ .probe = a24xx_init_one,
++ .remove = __devexit_p(a24xx_remove_one),
++ .suspend = NULL,
++ .resume = NULL,
++ .id_table = a24xx_pci_tbl,
++};
++
++static int __init a24xx_init(void)
++{
++ int res;
++ int x;
++ for (x=0;x<(sizeof(fxo_modes) / sizeof(fxo_modes[0])); x++) {
++ if (!strcmp(fxo_modes[x].name, opermode)) {
++ break;
++ }
++ }
++ if (x < sizeof(fxo_modes) / sizeof(fxo_modes[0])) {
++ _opermode = x;
++ } else {
++ printk(KERN_NOTICE "Invalid/unknown operating mode '%s' specified. Please choose one of:\n", opermode);
++ for (x=0;x<sizeof(fxo_modes) / sizeof(fxo_modes[0]); x++) {
++ printk(KERN_INFO " %s\n", fxo_modes[x].name);
++ }
++ printk(KERN_INFO "Note this option is CASE SENSITIVE!\n");
++ return -ENODEV;
++ }
++ if (!strcmp(fxo_modes[_opermode].name, "AUSTRALIA")) {
++ boostringer=1;
++ fxshonormode=1;
++ }
++ /* for the voicedaa_check_hook defaults, if the user has not overridden
++ them by specifying them as module parameters, then get the values
++ from the selected operating mode
++ */
++ if (battdebounce == 0) {
++ battdebounce = fxo_modes[_opermode].battdebounce;
++ }
++ if (battalarm == 0) {
++ battalarm = fxo_modes[_opermode].battalarm;
++ }
++ if (battthresh == 0) {
++ battthresh = fxo_modes[_opermode].battthresh;
++ }
++
++ res = dahdi_pci_module(&a24xx_driver);
++ if (res) {
++ return -ENODEV;
++ }
++ return 0;
++}
++
++static void __exit a24xx_cleanup(void)
++{
++ pci_unregister_driver(&a24xx_driver);
++}
++
++module_param(spi_cmd, int, 0600);
++module_param(debug, int, 0600);
++module_param(ec_debug, int, 0600);
++module_param(vpmsupport, int, 0600);
++module_param(loopcurrent, int, 0600);
++module_param(reversepolarity, int, 0600);
++module_param(robust, int, 0600);
++module_param(_opermode, int, 0600);
++module_param(opermode, charp, 0600);
++module_param(timingonly, int, 0600);
++module_param(lowpower, int, 0600);
++module_param(boostringer, int, 0600);
++module_param(fastringer, int, 0600);
++module_param(fxshonormode, int, 0600);
++module_param(battdebounce, uint, 0600);
++module_param(battalarm, uint, 0600);
++module_param(battthresh, uint, 0600);
++module_param(ringdebounce, int, 0600);
++module_param(fwringdetect, int, 0600);
++module_param(alawoverride, int, 0600);
++module_param(fastpickup, int, 0600);
++module_param(fxotxgain, int, 0600);
++module_param(fxorxgain, int, 0600);
++module_param(fxstxgain, int, 0600);
++module_param(fxsrxgain, int, 0600);
++module_param(cidsupport, int, 0600);
++module_param(cidbuflen, int, 0600);
++module_param(cidtimeout, int, 0600);
++module_param(fxofullscale, int, 0600);
++module_param(fixedtimepolarity, int, 0600);
++module_param(ms_per_irq, int, 0600);
++module_param(irq_stub, int, 0600);
++
++MODULE_DESCRIPTION("OpenVox A2410 Dahdi Driver");
++MODULE_AUTHOR("MiaoLin <lin.miao@openvox.cn>");
++MODULE_LICENSE("GPL v2");
++
++module_init(a24xx_init);
++module_exit(a24xx_cleanup);
++