Path: blob/master/drivers/isdn/hardware/mISDN/hfcpci.c
15111 views
/*1*2* hfcpci.c low level driver for CCD's hfc-pci based cards3*4* Author Werner Cornelius ([email protected])5* based on existing driver for CCD hfc ISA cards6* type approval valid for HFC-S PCI A based card7*8* Copyright 1999 by Werner Cornelius ([email protected])9* Copyright 2008 by Karsten Keil <[email protected]>10*11* This program is free software; you can redistribute it and/or modify12* it under the terms of the GNU General Public License as published by13* the Free Software Foundation; either version 2, or (at your option)14* any later version.15*16* This program is distributed in the hope that it will be useful,17* but WITHOUT ANY WARRANTY; without even the implied warranty of18* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the19* GNU General Public License for more details.20*21* You should have received a copy of the GNU General Public License22* along with this program; if not, write to the Free Software23* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.24*25* Module options:26*27* debug:28* NOTE: only one poll value must be given for all cards29* See hfc_pci.h for debug flags.30*31* poll:32* NOTE: only one poll value must be given for all cards33* Give the number of samples for each fifo process.34* By default 128 is used. Decrease to reduce delay, increase to35* reduce cpu load. If unsure, don't mess with it!36* A value of 128 will use controller's interrupt. Other values will37* use kernel timer, because the controller will not allow lower values38* than 128.39* Also note that the value depends on the kernel timer frequency.40* If kernel uses a frequency of 1000 Hz, steps of 8 samples are possible.41* If the kernel uses 100 Hz, steps of 80 samples are possible.42* If the kernel uses 300 Hz, steps of about 26 samples are possible.43*44*/4546#include <linux/module.h>47#include <linux/pci.h>48#include <linux/delay.h>49#include <linux/mISDNhw.h>50#include <linux/slab.h>5152#include "hfc_pci.h"5354static const char *hfcpci_revision = "2.0";5556static int HFC_cnt;57static uint debug;58static uint poll, tics;59static struct timer_list hfc_tl;60static unsigned long hfc_jiffies;6162MODULE_AUTHOR("Karsten Keil");63MODULE_LICENSE("GPL");64module_param(debug, uint, S_IRUGO | S_IWUSR);65module_param(poll, uint, S_IRUGO | S_IWUSR);6667enum {68HFC_CCD_2BD0,69HFC_CCD_B000,70HFC_CCD_B006,71HFC_CCD_B007,72HFC_CCD_B008,73HFC_CCD_B009,74HFC_CCD_B00A,75HFC_CCD_B00B,76HFC_CCD_B00C,77HFC_CCD_B100,78HFC_CCD_B700,79HFC_CCD_B701,80HFC_ASUS_0675,81HFC_BERKOM_A1T,82HFC_BERKOM_TCONCEPT,83HFC_ANIGMA_MC145575,84HFC_ZOLTRIX_2BD0,85HFC_DIGI_DF_M_IOM2_E,86HFC_DIGI_DF_M_E,87HFC_DIGI_DF_M_IOM2_A,88HFC_DIGI_DF_M_A,89HFC_ABOCOM_2BD1,90HFC_SITECOM_DC105V2,91};9293struct hfcPCI_hw {94unsigned char cirm;95unsigned char ctmt;96unsigned char clkdel;97unsigned char states;98unsigned char conn;99unsigned char mst_m;100unsigned char int_m1;101unsigned char int_m2;102unsigned char sctrl;103unsigned char sctrl_r;104unsigned char sctrl_e;105unsigned char trm;106unsigned char fifo_en;107unsigned char bswapped;108unsigned char protocol;109int nt_timer;110unsigned char __iomem *pci_io; /* start of PCI IO memory */111dma_addr_t dmahandle;112void *fifos; /* FIFO memory */113int last_bfifo_cnt[2];114/* marker saving last b-fifo frame count */115struct timer_list timer;116};117118#define HFC_CFG_MASTER 1119#define HFC_CFG_SLAVE 2120#define HFC_CFG_PCM 3121#define HFC_CFG_2HFC 4122#define HFC_CFG_SLAVEHFC 5123#define HFC_CFG_NEG_F0 6124#define HFC_CFG_SW_DD_DU 7125126#define FLG_HFC_TIMER_T1 16127#define FLG_HFC_TIMER_T3 17128129#define NT_T1_COUNT 1120 /* number of 3.125ms interrupts (3.5s) */130#define NT_T3_COUNT 31 /* number of 3.125ms interrupts (97 ms) */131#define CLKDEL_TE 0x0e /* CLKDEL in TE mode */132#define CLKDEL_NT 0x6c /* CLKDEL in NT mode */133134135struct hfc_pci {136u_char subtype;137u_char chanlimit;138u_char initdone;139u_long cfg;140u_int irq;141u_int irqcnt;142struct pci_dev *pdev;143struct hfcPCI_hw hw;144spinlock_t lock; /* card lock */145struct dchannel dch;146struct bchannel bch[2];147};148149/* Interface functions */150static void151enable_hwirq(struct hfc_pci *hc)152{153hc->hw.int_m2 |= HFCPCI_IRQ_ENABLE;154Write_hfc(hc, HFCPCI_INT_M2, hc->hw.int_m2);155}156157static void158disable_hwirq(struct hfc_pci *hc)159{160hc->hw.int_m2 &= ~((u_char)HFCPCI_IRQ_ENABLE);161Write_hfc(hc, HFCPCI_INT_M2, hc->hw.int_m2);162}163164/*165* free hardware resources used by driver166*/167static void168release_io_hfcpci(struct hfc_pci *hc)169{170/* disable memory mapped ports + busmaster */171pci_write_config_word(hc->pdev, PCI_COMMAND, 0);172del_timer(&hc->hw.timer);173pci_free_consistent(hc->pdev, 0x8000, hc->hw.fifos, hc->hw.dmahandle);174iounmap(hc->hw.pci_io);175}176177/*178* set mode (NT or TE)179*/180static void181hfcpci_setmode(struct hfc_pci *hc)182{183if (hc->hw.protocol == ISDN_P_NT_S0) {184hc->hw.clkdel = CLKDEL_NT; /* ST-Bit delay for NT-Mode */185hc->hw.sctrl |= SCTRL_MODE_NT; /* NT-MODE */186hc->hw.states = 1; /* G1 */187} else {188hc->hw.clkdel = CLKDEL_TE; /* ST-Bit delay for TE-Mode */189hc->hw.sctrl &= ~SCTRL_MODE_NT; /* TE-MODE */190hc->hw.states = 2; /* F2 */191}192Write_hfc(hc, HFCPCI_CLKDEL, hc->hw.clkdel);193Write_hfc(hc, HFCPCI_STATES, HFCPCI_LOAD_STATE | hc->hw.states);194udelay(10);195Write_hfc(hc, HFCPCI_STATES, hc->hw.states | 0x40); /* Deactivate */196Write_hfc(hc, HFCPCI_SCTRL, hc->hw.sctrl);197}198199/*200* function called to reset the HFC PCI chip. A complete software reset of chip201* and fifos is done.202*/203static void204reset_hfcpci(struct hfc_pci *hc)205{206u_char val;207int cnt = 0;208209printk(KERN_DEBUG "reset_hfcpci: entered\n");210val = Read_hfc(hc, HFCPCI_CHIP_ID);211printk(KERN_INFO "HFC_PCI: resetting HFC ChipId(%x)\n", val);212/* enable memory mapped ports, disable busmaster */213pci_write_config_word(hc->pdev, PCI_COMMAND, PCI_ENA_MEMIO);214disable_hwirq(hc);215/* enable memory ports + busmaster */216pci_write_config_word(hc->pdev, PCI_COMMAND,217PCI_ENA_MEMIO + PCI_ENA_MASTER);218val = Read_hfc(hc, HFCPCI_STATUS);219printk(KERN_DEBUG "HFC-PCI status(%x) before reset\n", val);220hc->hw.cirm = HFCPCI_RESET; /* Reset On */221Write_hfc(hc, HFCPCI_CIRM, hc->hw.cirm);222set_current_state(TASK_UNINTERRUPTIBLE);223mdelay(10); /* Timeout 10ms */224hc->hw.cirm = 0; /* Reset Off */225Write_hfc(hc, HFCPCI_CIRM, hc->hw.cirm);226val = Read_hfc(hc, HFCPCI_STATUS);227printk(KERN_DEBUG "HFC-PCI status(%x) after reset\n", val);228while (cnt < 50000) { /* max 50000 us */229udelay(5);230cnt += 5;231val = Read_hfc(hc, HFCPCI_STATUS);232if (!(val & 2))233break;234}235printk(KERN_DEBUG "HFC-PCI status(%x) after %dus\n", val, cnt);236237hc->hw.fifo_en = 0x30; /* only D fifos enabled */238239hc->hw.bswapped = 0; /* no exchange */240hc->hw.ctmt = HFCPCI_TIM3_125 | HFCPCI_AUTO_TIMER;241hc->hw.trm = HFCPCI_BTRANS_THRESMASK; /* no echo connect , threshold */242hc->hw.sctrl = 0x40; /* set tx_lo mode, error in datasheet ! */243hc->hw.sctrl_r = 0;244hc->hw.sctrl_e = HFCPCI_AUTO_AWAKE; /* S/T Auto awake */245hc->hw.mst_m = 0;246if (test_bit(HFC_CFG_MASTER, &hc->cfg))247hc->hw.mst_m |= HFCPCI_MASTER; /* HFC Master Mode */248if (test_bit(HFC_CFG_NEG_F0, &hc->cfg))249hc->hw.mst_m |= HFCPCI_F0_NEGATIV;250Write_hfc(hc, HFCPCI_FIFO_EN, hc->hw.fifo_en);251Write_hfc(hc, HFCPCI_TRM, hc->hw.trm);252Write_hfc(hc, HFCPCI_SCTRL_E, hc->hw.sctrl_e);253Write_hfc(hc, HFCPCI_CTMT, hc->hw.ctmt);254255hc->hw.int_m1 = HFCPCI_INTS_DTRANS | HFCPCI_INTS_DREC |256HFCPCI_INTS_L1STATE | HFCPCI_INTS_TIMER;257Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);258259/* Clear already pending ints */260val = Read_hfc(hc, HFCPCI_INT_S1);261262/* set NT/TE mode */263hfcpci_setmode(hc);264265Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);266Write_hfc(hc, HFCPCI_SCTRL_R, hc->hw.sctrl_r);267268/*269* Init GCI/IOM2 in master mode270* Slots 0 and 1 are set for B-chan 1 and 2271* D- and monitor/CI channel are not enabled272* STIO1 is used as output for data, B1+B2 from ST->IOM+HFC273* STIO2 is used as data input, B1+B2 from IOM->ST274* ST B-channel send disabled -> continuous 1s275* The IOM slots are always enabled276*/277if (test_bit(HFC_CFG_PCM, &hc->cfg)) {278/* set data flow directions: connect B1,B2: HFC to/from PCM */279hc->hw.conn = 0x09;280} else {281hc->hw.conn = 0x36; /* set data flow directions */282if (test_bit(HFC_CFG_SW_DD_DU, &hc->cfg)) {283Write_hfc(hc, HFCPCI_B1_SSL, 0xC0);284Write_hfc(hc, HFCPCI_B2_SSL, 0xC1);285Write_hfc(hc, HFCPCI_B1_RSL, 0xC0);286Write_hfc(hc, HFCPCI_B2_RSL, 0xC1);287} else {288Write_hfc(hc, HFCPCI_B1_SSL, 0x80);289Write_hfc(hc, HFCPCI_B2_SSL, 0x81);290Write_hfc(hc, HFCPCI_B1_RSL, 0x80);291Write_hfc(hc, HFCPCI_B2_RSL, 0x81);292}293}294Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);295val = Read_hfc(hc, HFCPCI_INT_S2);296}297298/*299* Timer function called when kernel timer expires300*/301static void302hfcpci_Timer(struct hfc_pci *hc)303{304hc->hw.timer.expires = jiffies + 75;305/* WD RESET */306/*307* WriteReg(hc, HFCD_DATA, HFCD_CTMT, hc->hw.ctmt | 0x80);308* add_timer(&hc->hw.timer);309*/310}311312313/*314* select a b-channel entry matching and active315*/316static struct bchannel *317Sel_BCS(struct hfc_pci *hc, int channel)318{319if (test_bit(FLG_ACTIVE, &hc->bch[0].Flags) &&320(hc->bch[0].nr & channel))321return &hc->bch[0];322else if (test_bit(FLG_ACTIVE, &hc->bch[1].Flags) &&323(hc->bch[1].nr & channel))324return &hc->bch[1];325else326return NULL;327}328329/*330* clear the desired B-channel rx fifo331*/332static void333hfcpci_clear_fifo_rx(struct hfc_pci *hc, int fifo)334{335u_char fifo_state;336struct bzfifo *bzr;337338if (fifo) {339bzr = &((union fifo_area *)(hc->hw.fifos))->b_chans.rxbz_b2;340fifo_state = hc->hw.fifo_en & HFCPCI_FIFOEN_B2RX;341} else {342bzr = &((union fifo_area *)(hc->hw.fifos))->b_chans.rxbz_b1;343fifo_state = hc->hw.fifo_en & HFCPCI_FIFOEN_B1RX;344}345if (fifo_state)346hc->hw.fifo_en ^= fifo_state;347Write_hfc(hc, HFCPCI_FIFO_EN, hc->hw.fifo_en);348hc->hw.last_bfifo_cnt[fifo] = 0;349bzr->f1 = MAX_B_FRAMES;350bzr->f2 = bzr->f1; /* init F pointers to remain constant */351bzr->za[MAX_B_FRAMES].z1 = cpu_to_le16(B_FIFO_SIZE + B_SUB_VAL - 1);352bzr->za[MAX_B_FRAMES].z2 = cpu_to_le16(353le16_to_cpu(bzr->za[MAX_B_FRAMES].z1));354if (fifo_state)355hc->hw.fifo_en |= fifo_state;356Write_hfc(hc, HFCPCI_FIFO_EN, hc->hw.fifo_en);357}358359/*360* clear the desired B-channel tx fifo361*/362static void hfcpci_clear_fifo_tx(struct hfc_pci *hc, int fifo)363{364u_char fifo_state;365struct bzfifo *bzt;366367if (fifo) {368bzt = &((union fifo_area *)(hc->hw.fifos))->b_chans.txbz_b2;369fifo_state = hc->hw.fifo_en & HFCPCI_FIFOEN_B2TX;370} else {371bzt = &((union fifo_area *)(hc->hw.fifos))->b_chans.txbz_b1;372fifo_state = hc->hw.fifo_en & HFCPCI_FIFOEN_B1TX;373}374if (fifo_state)375hc->hw.fifo_en ^= fifo_state;376Write_hfc(hc, HFCPCI_FIFO_EN, hc->hw.fifo_en);377if (hc->bch[fifo].debug & DEBUG_HW_BCHANNEL)378printk(KERN_DEBUG "hfcpci_clear_fifo_tx%d f1(%x) f2(%x) "379"z1(%x) z2(%x) state(%x)\n",380fifo, bzt->f1, bzt->f2,381le16_to_cpu(bzt->za[MAX_B_FRAMES].z1),382le16_to_cpu(bzt->za[MAX_B_FRAMES].z2),383fifo_state);384bzt->f2 = MAX_B_FRAMES;385bzt->f1 = bzt->f2; /* init F pointers to remain constant */386bzt->za[MAX_B_FRAMES].z1 = cpu_to_le16(B_FIFO_SIZE + B_SUB_VAL - 1);387bzt->za[MAX_B_FRAMES].z2 = cpu_to_le16(B_FIFO_SIZE + B_SUB_VAL - 2);388if (fifo_state)389hc->hw.fifo_en |= fifo_state;390Write_hfc(hc, HFCPCI_FIFO_EN, hc->hw.fifo_en);391if (hc->bch[fifo].debug & DEBUG_HW_BCHANNEL)392printk(KERN_DEBUG393"hfcpci_clear_fifo_tx%d f1(%x) f2(%x) z1(%x) z2(%x)\n",394fifo, bzt->f1, bzt->f2,395le16_to_cpu(bzt->za[MAX_B_FRAMES].z1),396le16_to_cpu(bzt->za[MAX_B_FRAMES].z2));397}398399/*400* read a complete B-frame out of the buffer401*/402static void403hfcpci_empty_bfifo(struct bchannel *bch, struct bzfifo *bz,404u_char *bdata, int count)405{406u_char *ptr, *ptr1, new_f2;407int maxlen, new_z2;408struct zt *zp;409410if ((bch->debug & DEBUG_HW_BCHANNEL) && !(bch->debug & DEBUG_HW_BFIFO))411printk(KERN_DEBUG "hfcpci_empty_fifo\n");412zp = &bz->za[bz->f2]; /* point to Z-Regs */413new_z2 = le16_to_cpu(zp->z2) + count; /* new position in fifo */414if (new_z2 >= (B_FIFO_SIZE + B_SUB_VAL))415new_z2 -= B_FIFO_SIZE; /* buffer wrap */416new_f2 = (bz->f2 + 1) & MAX_B_FRAMES;417if ((count > MAX_DATA_SIZE + 3) || (count < 4) ||418(*(bdata + (le16_to_cpu(zp->z1) - B_SUB_VAL)))) {419if (bch->debug & DEBUG_HW)420printk(KERN_DEBUG "hfcpci_empty_fifo: incoming packet "421"invalid length %d or crc\n", count);422#ifdef ERROR_STATISTIC423bch->err_inv++;424#endif425bz->za[new_f2].z2 = cpu_to_le16(new_z2);426bz->f2 = new_f2; /* next buffer */427} else {428bch->rx_skb = mI_alloc_skb(count - 3, GFP_ATOMIC);429if (!bch->rx_skb) {430printk(KERN_WARNING "HFCPCI: receive out of memory\n");431return;432}433count -= 3;434ptr = skb_put(bch->rx_skb, count);435436if (le16_to_cpu(zp->z2) + count <= B_FIFO_SIZE + B_SUB_VAL)437maxlen = count; /* complete transfer */438else439maxlen = B_FIFO_SIZE + B_SUB_VAL -440le16_to_cpu(zp->z2); /* maximum */441442ptr1 = bdata + (le16_to_cpu(zp->z2) - B_SUB_VAL);443/* start of data */444memcpy(ptr, ptr1, maxlen); /* copy data */445count -= maxlen;446447if (count) { /* rest remaining */448ptr += maxlen;449ptr1 = bdata; /* start of buffer */450memcpy(ptr, ptr1, count); /* rest */451}452bz->za[new_f2].z2 = cpu_to_le16(new_z2);453bz->f2 = new_f2; /* next buffer */454recv_Bchannel(bch, MISDN_ID_ANY);455}456}457458/*459* D-channel receive procedure460*/461static int462receive_dmsg(struct hfc_pci *hc)463{464struct dchannel *dch = &hc->dch;465int maxlen;466int rcnt, total;467int count = 5;468u_char *ptr, *ptr1;469struct dfifo *df;470struct zt *zp;471472df = &((union fifo_area *)(hc->hw.fifos))->d_chan.d_rx;473while (((df->f1 & D_FREG_MASK) != (df->f2 & D_FREG_MASK)) && count--) {474zp = &df->za[df->f2 & D_FREG_MASK];475rcnt = le16_to_cpu(zp->z1) - le16_to_cpu(zp->z2);476if (rcnt < 0)477rcnt += D_FIFO_SIZE;478rcnt++;479if (dch->debug & DEBUG_HW_DCHANNEL)480printk(KERN_DEBUG481"hfcpci recd f1(%d) f2(%d) z1(%x) z2(%x) cnt(%d)\n",482df->f1, df->f2,483le16_to_cpu(zp->z1),484le16_to_cpu(zp->z2),485rcnt);486487if ((rcnt > MAX_DFRAME_LEN + 3) || (rcnt < 4) ||488(df->data[le16_to_cpu(zp->z1)])) {489if (dch->debug & DEBUG_HW)490printk(KERN_DEBUG491"empty_fifo hfcpci paket inv. len "492"%d or crc %d\n",493rcnt,494df->data[le16_to_cpu(zp->z1)]);495#ifdef ERROR_STATISTIC496cs->err_rx++;497#endif498df->f2 = ((df->f2 + 1) & MAX_D_FRAMES) |499(MAX_D_FRAMES + 1); /* next buffer */500df->za[df->f2 & D_FREG_MASK].z2 =501cpu_to_le16((le16_to_cpu(zp->z2) + rcnt) &502(D_FIFO_SIZE - 1));503} else {504dch->rx_skb = mI_alloc_skb(rcnt - 3, GFP_ATOMIC);505if (!dch->rx_skb) {506printk(KERN_WARNING507"HFC-PCI: D receive out of memory\n");508break;509}510total = rcnt;511rcnt -= 3;512ptr = skb_put(dch->rx_skb, rcnt);513514if (le16_to_cpu(zp->z2) + rcnt <= D_FIFO_SIZE)515maxlen = rcnt; /* complete transfer */516else517maxlen = D_FIFO_SIZE - le16_to_cpu(zp->z2);518/* maximum */519520ptr1 = df->data + le16_to_cpu(zp->z2);521/* start of data */522memcpy(ptr, ptr1, maxlen); /* copy data */523rcnt -= maxlen;524525if (rcnt) { /* rest remaining */526ptr += maxlen;527ptr1 = df->data; /* start of buffer */528memcpy(ptr, ptr1, rcnt); /* rest */529}530df->f2 = ((df->f2 + 1) & MAX_D_FRAMES) |531(MAX_D_FRAMES + 1); /* next buffer */532df->za[df->f2 & D_FREG_MASK].z2 = cpu_to_le16((533le16_to_cpu(zp->z2) + total) & (D_FIFO_SIZE - 1));534recv_Dchannel(dch);535}536}537return 1;538}539540/*541* check for transparent receive data and read max one 'poll' size if avail542*/543static void544hfcpci_empty_fifo_trans(struct bchannel *bch, struct bzfifo *rxbz,545struct bzfifo *txbz, u_char *bdata)546{547__le16 *z1r, *z2r, *z1t, *z2t;548int new_z2, fcnt_rx, fcnt_tx, maxlen;549u_char *ptr, *ptr1;550551z1r = &rxbz->za[MAX_B_FRAMES].z1; /* pointer to z reg */552z2r = z1r + 1;553z1t = &txbz->za[MAX_B_FRAMES].z1;554z2t = z1t + 1;555556fcnt_rx = le16_to_cpu(*z1r) - le16_to_cpu(*z2r);557if (!fcnt_rx)558return; /* no data avail */559560if (fcnt_rx <= 0)561fcnt_rx += B_FIFO_SIZE; /* bytes actually buffered */562new_z2 = le16_to_cpu(*z2r) + fcnt_rx; /* new position in fifo */563if (new_z2 >= (B_FIFO_SIZE + B_SUB_VAL))564new_z2 -= B_FIFO_SIZE; /* buffer wrap */565566if (fcnt_rx > MAX_DATA_SIZE) { /* flush, if oversized */567*z2r = cpu_to_le16(new_z2); /* new position */568return;569}570571fcnt_tx = le16_to_cpu(*z2t) - le16_to_cpu(*z1t);572if (fcnt_tx <= 0)573fcnt_tx += B_FIFO_SIZE;574/* fcnt_tx contains available bytes in tx-fifo */575fcnt_tx = B_FIFO_SIZE - fcnt_tx;576/* remaining bytes to send (bytes in tx-fifo) */577578bch->rx_skb = mI_alloc_skb(fcnt_rx, GFP_ATOMIC);579if (bch->rx_skb) {580ptr = skb_put(bch->rx_skb, fcnt_rx);581if (le16_to_cpu(*z2r) + fcnt_rx <= B_FIFO_SIZE + B_SUB_VAL)582maxlen = fcnt_rx; /* complete transfer */583else584maxlen = B_FIFO_SIZE + B_SUB_VAL - le16_to_cpu(*z2r);585/* maximum */586587ptr1 = bdata + (le16_to_cpu(*z2r) - B_SUB_VAL);588/* start of data */589memcpy(ptr, ptr1, maxlen); /* copy data */590fcnt_rx -= maxlen;591592if (fcnt_rx) { /* rest remaining */593ptr += maxlen;594ptr1 = bdata; /* start of buffer */595memcpy(ptr, ptr1, fcnt_rx); /* rest */596}597recv_Bchannel(bch, fcnt_tx); /* bch, id */598} else599printk(KERN_WARNING "HFCPCI: receive out of memory\n");600601*z2r = cpu_to_le16(new_z2); /* new position */602}603604/*605* B-channel main receive routine606*/607static void608main_rec_hfcpci(struct bchannel *bch)609{610struct hfc_pci *hc = bch->hw;611int rcnt, real_fifo;612int receive = 0, count = 5;613struct bzfifo *txbz, *rxbz;614u_char *bdata;615struct zt *zp;616617if ((bch->nr & 2) && (!hc->hw.bswapped)) {618rxbz = &((union fifo_area *)(hc->hw.fifos))->b_chans.rxbz_b2;619txbz = &((union fifo_area *)(hc->hw.fifos))->b_chans.txbz_b2;620bdata = ((union fifo_area *)(hc->hw.fifos))->b_chans.rxdat_b2;621real_fifo = 1;622} else {623rxbz = &((union fifo_area *)(hc->hw.fifos))->b_chans.rxbz_b1;624txbz = &((union fifo_area *)(hc->hw.fifos))->b_chans.txbz_b1;625bdata = ((union fifo_area *)(hc->hw.fifos))->b_chans.rxdat_b1;626real_fifo = 0;627}628Begin:629count--;630if (rxbz->f1 != rxbz->f2) {631if (bch->debug & DEBUG_HW_BCHANNEL)632printk(KERN_DEBUG "hfcpci rec ch(%x) f1(%d) f2(%d)\n",633bch->nr, rxbz->f1, rxbz->f2);634zp = &rxbz->za[rxbz->f2];635636rcnt = le16_to_cpu(zp->z1) - le16_to_cpu(zp->z2);637if (rcnt < 0)638rcnt += B_FIFO_SIZE;639rcnt++;640if (bch->debug & DEBUG_HW_BCHANNEL)641printk(KERN_DEBUG642"hfcpci rec ch(%x) z1(%x) z2(%x) cnt(%d)\n",643bch->nr, le16_to_cpu(zp->z1),644le16_to_cpu(zp->z2), rcnt);645hfcpci_empty_bfifo(bch, rxbz, bdata, rcnt);646rcnt = rxbz->f1 - rxbz->f2;647if (rcnt < 0)648rcnt += MAX_B_FRAMES + 1;649if (hc->hw.last_bfifo_cnt[real_fifo] > rcnt + 1) {650rcnt = 0;651hfcpci_clear_fifo_rx(hc, real_fifo);652}653hc->hw.last_bfifo_cnt[real_fifo] = rcnt;654if (rcnt > 1)655receive = 1;656else657receive = 0;658} else if (test_bit(FLG_TRANSPARENT, &bch->Flags)) {659hfcpci_empty_fifo_trans(bch, rxbz, txbz, bdata);660return;661} else662receive = 0;663if (count && receive)664goto Begin;665666}667668/*669* D-channel send routine670*/671static void672hfcpci_fill_dfifo(struct hfc_pci *hc)673{674struct dchannel *dch = &hc->dch;675int fcnt;676int count, new_z1, maxlen;677struct dfifo *df;678u_char *src, *dst, new_f1;679680if ((dch->debug & DEBUG_HW_DCHANNEL) && !(dch->debug & DEBUG_HW_DFIFO))681printk(KERN_DEBUG "%s\n", __func__);682683if (!dch->tx_skb)684return;685count = dch->tx_skb->len - dch->tx_idx;686if (count <= 0)687return;688df = &((union fifo_area *) (hc->hw.fifos))->d_chan.d_tx;689690if (dch->debug & DEBUG_HW_DFIFO)691printk(KERN_DEBUG "%s:f1(%d) f2(%d) z1(f1)(%x)\n", __func__,692df->f1, df->f2,693le16_to_cpu(df->za[df->f1 & D_FREG_MASK].z1));694fcnt = df->f1 - df->f2; /* frame count actually buffered */695if (fcnt < 0)696fcnt += (MAX_D_FRAMES + 1); /* if wrap around */697if (fcnt > (MAX_D_FRAMES - 1)) {698if (dch->debug & DEBUG_HW_DCHANNEL)699printk(KERN_DEBUG700"hfcpci_fill_Dfifo more as 14 frames\n");701#ifdef ERROR_STATISTIC702cs->err_tx++;703#endif704return;705}706/* now determine free bytes in FIFO buffer */707maxlen = le16_to_cpu(df->za[df->f2 & D_FREG_MASK].z2) -708le16_to_cpu(df->za[df->f1 & D_FREG_MASK].z1) - 1;709if (maxlen <= 0)710maxlen += D_FIFO_SIZE; /* count now contains available bytes */711712if (dch->debug & DEBUG_HW_DCHANNEL)713printk(KERN_DEBUG "hfcpci_fill_Dfifo count(%d/%d)\n",714count, maxlen);715if (count > maxlen) {716if (dch->debug & DEBUG_HW_DCHANNEL)717printk(KERN_DEBUG "hfcpci_fill_Dfifo no fifo mem\n");718return;719}720new_z1 = (le16_to_cpu(df->za[df->f1 & D_FREG_MASK].z1) + count) &721(D_FIFO_SIZE - 1);722new_f1 = ((df->f1 + 1) & D_FREG_MASK) | (D_FREG_MASK + 1);723src = dch->tx_skb->data + dch->tx_idx; /* source pointer */724dst = df->data + le16_to_cpu(df->za[df->f1 & D_FREG_MASK].z1);725maxlen = D_FIFO_SIZE - le16_to_cpu(df->za[df->f1 & D_FREG_MASK].z1);726/* end fifo */727if (maxlen > count)728maxlen = count; /* limit size */729memcpy(dst, src, maxlen); /* first copy */730731count -= maxlen; /* remaining bytes */732if (count) {733dst = df->data; /* start of buffer */734src += maxlen; /* new position */735memcpy(dst, src, count);736}737df->za[new_f1 & D_FREG_MASK].z1 = cpu_to_le16(new_z1);738/* for next buffer */739df->za[df->f1 & D_FREG_MASK].z1 = cpu_to_le16(new_z1);740/* new pos actual buffer */741df->f1 = new_f1; /* next frame */742dch->tx_idx = dch->tx_skb->len;743}744745/*746* B-channel send routine747*/748static void749hfcpci_fill_fifo(struct bchannel *bch)750{751struct hfc_pci *hc = bch->hw;752int maxlen, fcnt;753int count, new_z1;754struct bzfifo *bz;755u_char *bdata;756u_char new_f1, *src, *dst;757__le16 *z1t, *z2t;758759if ((bch->debug & DEBUG_HW_BCHANNEL) && !(bch->debug & DEBUG_HW_BFIFO))760printk(KERN_DEBUG "%s\n", __func__);761if ((!bch->tx_skb) || bch->tx_skb->len <= 0)762return;763count = bch->tx_skb->len - bch->tx_idx;764if ((bch->nr & 2) && (!hc->hw.bswapped)) {765bz = &((union fifo_area *)(hc->hw.fifos))->b_chans.txbz_b2;766bdata = ((union fifo_area *)(hc->hw.fifos))->b_chans.txdat_b2;767} else {768bz = &((union fifo_area *)(hc->hw.fifos))->b_chans.txbz_b1;769bdata = ((union fifo_area *)(hc->hw.fifos))->b_chans.txdat_b1;770}771772if (test_bit(FLG_TRANSPARENT, &bch->Flags)) {773z1t = &bz->za[MAX_B_FRAMES].z1;774z2t = z1t + 1;775if (bch->debug & DEBUG_HW_BCHANNEL)776printk(KERN_DEBUG "hfcpci_fill_fifo_trans ch(%x) "777"cnt(%d) z1(%x) z2(%x)\n", bch->nr, count,778le16_to_cpu(*z1t), le16_to_cpu(*z2t));779fcnt = le16_to_cpu(*z2t) - le16_to_cpu(*z1t);780if (fcnt <= 0)781fcnt += B_FIFO_SIZE;782/* fcnt contains available bytes in fifo */783fcnt = B_FIFO_SIZE - fcnt;784/* remaining bytes to send (bytes in fifo) */785786/* "fill fifo if empty" feature */787if (test_bit(FLG_FILLEMPTY, &bch->Flags) && !fcnt) {788/* printk(KERN_DEBUG "%s: buffer empty, so we have "789"underrun\n", __func__); */790/* fill buffer, to prevent future underrun */791count = HFCPCI_FILLEMPTY;792new_z1 = le16_to_cpu(*z1t) + count;793/* new buffer Position */794if (new_z1 >= (B_FIFO_SIZE + B_SUB_VAL))795new_z1 -= B_FIFO_SIZE; /* buffer wrap */796dst = bdata + (le16_to_cpu(*z1t) - B_SUB_VAL);797maxlen = (B_FIFO_SIZE + B_SUB_VAL) - le16_to_cpu(*z1t);798/* end of fifo */799if (bch->debug & DEBUG_HW_BFIFO)800printk(KERN_DEBUG "hfcpci_FFt fillempty "801"fcnt(%d) maxl(%d) nz1(%x) dst(%p)\n",802fcnt, maxlen, new_z1, dst);803fcnt += count;804if (maxlen > count)805maxlen = count; /* limit size */806memset(dst, 0x2a, maxlen); /* first copy */807count -= maxlen; /* remaining bytes */808if (count) {809dst = bdata; /* start of buffer */810memset(dst, 0x2a, count);811}812*z1t = cpu_to_le16(new_z1); /* now send data */813}814815next_t_frame:816count = bch->tx_skb->len - bch->tx_idx;817/* maximum fill shall be poll*2 */818if (count > (poll << 1) - fcnt)819count = (poll << 1) - fcnt;820if (count <= 0)821return;822/* data is suitable for fifo */823new_z1 = le16_to_cpu(*z1t) + count;824/* new buffer Position */825if (new_z1 >= (B_FIFO_SIZE + B_SUB_VAL))826new_z1 -= B_FIFO_SIZE; /* buffer wrap */827src = bch->tx_skb->data + bch->tx_idx;828/* source pointer */829dst = bdata + (le16_to_cpu(*z1t) - B_SUB_VAL);830maxlen = (B_FIFO_SIZE + B_SUB_VAL) - le16_to_cpu(*z1t);831/* end of fifo */832if (bch->debug & DEBUG_HW_BFIFO)833printk(KERN_DEBUG "hfcpci_FFt fcnt(%d) "834"maxl(%d) nz1(%x) dst(%p)\n",835fcnt, maxlen, new_z1, dst);836fcnt += count;837bch->tx_idx += count;838if (maxlen > count)839maxlen = count; /* limit size */840memcpy(dst, src, maxlen); /* first copy */841count -= maxlen; /* remaining bytes */842if (count) {843dst = bdata; /* start of buffer */844src += maxlen; /* new position */845memcpy(dst, src, count);846}847*z1t = cpu_to_le16(new_z1); /* now send data */848if (bch->tx_idx < bch->tx_skb->len)849return;850/* send confirm, on trans, free on hdlc. */851if (test_bit(FLG_TRANSPARENT, &bch->Flags))852confirm_Bsend(bch);853dev_kfree_skb(bch->tx_skb);854if (get_next_bframe(bch))855goto next_t_frame;856return;857}858if (bch->debug & DEBUG_HW_BCHANNEL)859printk(KERN_DEBUG860"%s: ch(%x) f1(%d) f2(%d) z1(f1)(%x)\n",861__func__, bch->nr, bz->f1, bz->f2,862bz->za[bz->f1].z1);863fcnt = bz->f1 - bz->f2; /* frame count actually buffered */864if (fcnt < 0)865fcnt += (MAX_B_FRAMES + 1); /* if wrap around */866if (fcnt > (MAX_B_FRAMES - 1)) {867if (bch->debug & DEBUG_HW_BCHANNEL)868printk(KERN_DEBUG869"hfcpci_fill_Bfifo more as 14 frames\n");870return;871}872/* now determine free bytes in FIFO buffer */873maxlen = le16_to_cpu(bz->za[bz->f2].z2) -874le16_to_cpu(bz->za[bz->f1].z1) - 1;875if (maxlen <= 0)876maxlen += B_FIFO_SIZE; /* count now contains available bytes */877878if (bch->debug & DEBUG_HW_BCHANNEL)879printk(KERN_DEBUG "hfcpci_fill_fifo ch(%x) count(%d/%d)\n",880bch->nr, count, maxlen);881882if (maxlen < count) {883if (bch->debug & DEBUG_HW_BCHANNEL)884printk(KERN_DEBUG "hfcpci_fill_fifo no fifo mem\n");885return;886}887new_z1 = le16_to_cpu(bz->za[bz->f1].z1) + count;888/* new buffer Position */889if (new_z1 >= (B_FIFO_SIZE + B_SUB_VAL))890new_z1 -= B_FIFO_SIZE; /* buffer wrap */891892new_f1 = ((bz->f1 + 1) & MAX_B_FRAMES);893src = bch->tx_skb->data + bch->tx_idx; /* source pointer */894dst = bdata + (le16_to_cpu(bz->za[bz->f1].z1) - B_SUB_VAL);895maxlen = (B_FIFO_SIZE + B_SUB_VAL) - le16_to_cpu(bz->za[bz->f1].z1);896/* end fifo */897if (maxlen > count)898maxlen = count; /* limit size */899memcpy(dst, src, maxlen); /* first copy */900901count -= maxlen; /* remaining bytes */902if (count) {903dst = bdata; /* start of buffer */904src += maxlen; /* new position */905memcpy(dst, src, count);906}907bz->za[new_f1].z1 = cpu_to_le16(new_z1); /* for next buffer */908bz->f1 = new_f1; /* next frame */909dev_kfree_skb(bch->tx_skb);910get_next_bframe(bch);911}912913914915/*916* handle L1 state changes TE917*/918919static void920ph_state_te(struct dchannel *dch)921{922if (dch->debug)923printk(KERN_DEBUG "%s: TE newstate %x\n",924__func__, dch->state);925switch (dch->state) {926case 0:927l1_event(dch->l1, HW_RESET_IND);928break;929case 3:930l1_event(dch->l1, HW_DEACT_IND);931break;932case 5:933case 8:934l1_event(dch->l1, ANYSIGNAL);935break;936case 6:937l1_event(dch->l1, INFO2);938break;939case 7:940l1_event(dch->l1, INFO4_P8);941break;942}943}944945/*946* handle L1 state changes NT947*/948949static void950handle_nt_timer3(struct dchannel *dch) {951struct hfc_pci *hc = dch->hw;952953test_and_clear_bit(FLG_HFC_TIMER_T3, &dch->Flags);954hc->hw.int_m1 &= ~HFCPCI_INTS_TIMER;955Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);956hc->hw.nt_timer = 0;957test_and_set_bit(FLG_ACTIVE, &dch->Flags);958if (test_bit(HFC_CFG_MASTER, &hc->cfg))959hc->hw.mst_m |= HFCPCI_MASTER;960Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);961_queue_data(&dch->dev.D, PH_ACTIVATE_IND,962MISDN_ID_ANY, 0, NULL, GFP_ATOMIC);963}964965static void966ph_state_nt(struct dchannel *dch)967{968struct hfc_pci *hc = dch->hw;969970if (dch->debug)971printk(KERN_DEBUG "%s: NT newstate %x\n",972__func__, dch->state);973switch (dch->state) {974case 2:975if (hc->hw.nt_timer < 0) {976hc->hw.nt_timer = 0;977test_and_clear_bit(FLG_HFC_TIMER_T3, &dch->Flags);978test_and_clear_bit(FLG_HFC_TIMER_T1, &dch->Flags);979hc->hw.int_m1 &= ~HFCPCI_INTS_TIMER;980Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);981/* Clear already pending ints */982(void) Read_hfc(hc, HFCPCI_INT_S1);983Write_hfc(hc, HFCPCI_STATES, 4 | HFCPCI_LOAD_STATE);984udelay(10);985Write_hfc(hc, HFCPCI_STATES, 4);986dch->state = 4;987} else if (hc->hw.nt_timer == 0) {988hc->hw.int_m1 |= HFCPCI_INTS_TIMER;989Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);990hc->hw.nt_timer = NT_T1_COUNT;991hc->hw.ctmt &= ~HFCPCI_AUTO_TIMER;992hc->hw.ctmt |= HFCPCI_TIM3_125;993Write_hfc(hc, HFCPCI_CTMT, hc->hw.ctmt |994HFCPCI_CLTIMER);995test_and_clear_bit(FLG_HFC_TIMER_T3, &dch->Flags);996test_and_set_bit(FLG_HFC_TIMER_T1, &dch->Flags);997/* allow G2 -> G3 transition */998Write_hfc(hc, HFCPCI_STATES, 2 | HFCPCI_NT_G2_G3);999} else {1000Write_hfc(hc, HFCPCI_STATES, 2 | HFCPCI_NT_G2_G3);1001}1002break;1003case 1:1004hc->hw.nt_timer = 0;1005test_and_clear_bit(FLG_HFC_TIMER_T3, &dch->Flags);1006test_and_clear_bit(FLG_HFC_TIMER_T1, &dch->Flags);1007hc->hw.int_m1 &= ~HFCPCI_INTS_TIMER;1008Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);1009test_and_clear_bit(FLG_ACTIVE, &dch->Flags);1010hc->hw.mst_m &= ~HFCPCI_MASTER;1011Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);1012test_and_clear_bit(FLG_L2_ACTIVATED, &dch->Flags);1013_queue_data(&dch->dev.D, PH_DEACTIVATE_IND,1014MISDN_ID_ANY, 0, NULL, GFP_ATOMIC);1015break;1016case 4:1017hc->hw.nt_timer = 0;1018test_and_clear_bit(FLG_HFC_TIMER_T3, &dch->Flags);1019test_and_clear_bit(FLG_HFC_TIMER_T1, &dch->Flags);1020hc->hw.int_m1 &= ~HFCPCI_INTS_TIMER;1021Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);1022break;1023case 3:1024if (!test_and_set_bit(FLG_HFC_TIMER_T3, &dch->Flags)) {1025if (!test_and_clear_bit(FLG_L2_ACTIVATED,1026&dch->Flags)) {1027handle_nt_timer3(dch);1028break;1029}1030test_and_clear_bit(FLG_HFC_TIMER_T1, &dch->Flags);1031hc->hw.int_m1 |= HFCPCI_INTS_TIMER;1032Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);1033hc->hw.nt_timer = NT_T3_COUNT;1034hc->hw.ctmt &= ~HFCPCI_AUTO_TIMER;1035hc->hw.ctmt |= HFCPCI_TIM3_125;1036Write_hfc(hc, HFCPCI_CTMT, hc->hw.ctmt |1037HFCPCI_CLTIMER);1038}1039break;1040}1041}10421043static void1044ph_state(struct dchannel *dch)1045{1046struct hfc_pci *hc = dch->hw;10471048if (hc->hw.protocol == ISDN_P_NT_S0) {1049if (test_bit(FLG_HFC_TIMER_T3, &dch->Flags) &&1050hc->hw.nt_timer < 0)1051handle_nt_timer3(dch);1052else1053ph_state_nt(dch);1054} else1055ph_state_te(dch);1056}10571058/*1059* Layer 1 callback function1060*/1061static int1062hfc_l1callback(struct dchannel *dch, u_int cmd)1063{1064struct hfc_pci *hc = dch->hw;10651066switch (cmd) {1067case INFO3_P8:1068case INFO3_P10:1069if (test_bit(HFC_CFG_MASTER, &hc->cfg))1070hc->hw.mst_m |= HFCPCI_MASTER;1071Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);1072break;1073case HW_RESET_REQ:1074Write_hfc(hc, HFCPCI_STATES, HFCPCI_LOAD_STATE | 3);1075/* HFC ST 3 */1076udelay(6);1077Write_hfc(hc, HFCPCI_STATES, 3); /* HFC ST 2 */1078if (test_bit(HFC_CFG_MASTER, &hc->cfg))1079hc->hw.mst_m |= HFCPCI_MASTER;1080Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);1081Write_hfc(hc, HFCPCI_STATES, HFCPCI_ACTIVATE |1082HFCPCI_DO_ACTION);1083l1_event(dch->l1, HW_POWERUP_IND);1084break;1085case HW_DEACT_REQ:1086hc->hw.mst_m &= ~HFCPCI_MASTER;1087Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);1088skb_queue_purge(&dch->squeue);1089if (dch->tx_skb) {1090dev_kfree_skb(dch->tx_skb);1091dch->tx_skb = NULL;1092}1093dch->tx_idx = 0;1094if (dch->rx_skb) {1095dev_kfree_skb(dch->rx_skb);1096dch->rx_skb = NULL;1097}1098test_and_clear_bit(FLG_TX_BUSY, &dch->Flags);1099if (test_and_clear_bit(FLG_BUSY_TIMER, &dch->Flags))1100del_timer(&dch->timer);1101break;1102case HW_POWERUP_REQ:1103Write_hfc(hc, HFCPCI_STATES, HFCPCI_DO_ACTION);1104break;1105case PH_ACTIVATE_IND:1106test_and_set_bit(FLG_ACTIVE, &dch->Flags);1107_queue_data(&dch->dev.D, cmd, MISDN_ID_ANY, 0, NULL,1108GFP_ATOMIC);1109break;1110case PH_DEACTIVATE_IND:1111test_and_clear_bit(FLG_ACTIVE, &dch->Flags);1112_queue_data(&dch->dev.D, cmd, MISDN_ID_ANY, 0, NULL,1113GFP_ATOMIC);1114break;1115default:1116if (dch->debug & DEBUG_HW)1117printk(KERN_DEBUG "%s: unknown command %x\n",1118__func__, cmd);1119return -1;1120}1121return 0;1122}11231124/*1125* Interrupt handler1126*/1127static inline void1128tx_birq(struct bchannel *bch)1129{1130if (bch->tx_skb && bch->tx_idx < bch->tx_skb->len)1131hfcpci_fill_fifo(bch);1132else {1133if (bch->tx_skb)1134dev_kfree_skb(bch->tx_skb);1135if (get_next_bframe(bch))1136hfcpci_fill_fifo(bch);1137}1138}11391140static inline void1141tx_dirq(struct dchannel *dch)1142{1143if (dch->tx_skb && dch->tx_idx < dch->tx_skb->len)1144hfcpci_fill_dfifo(dch->hw);1145else {1146if (dch->tx_skb)1147dev_kfree_skb(dch->tx_skb);1148if (get_next_dframe(dch))1149hfcpci_fill_dfifo(dch->hw);1150}1151}11521153static irqreturn_t1154hfcpci_int(int intno, void *dev_id)1155{1156struct hfc_pci *hc = dev_id;1157u_char exval;1158struct bchannel *bch;1159u_char val, stat;11601161spin_lock(&hc->lock);1162if (!(hc->hw.int_m2 & 0x08)) {1163spin_unlock(&hc->lock);1164return IRQ_NONE; /* not initialised */1165}1166stat = Read_hfc(hc, HFCPCI_STATUS);1167if (HFCPCI_ANYINT & stat) {1168val = Read_hfc(hc, HFCPCI_INT_S1);1169if (hc->dch.debug & DEBUG_HW_DCHANNEL)1170printk(KERN_DEBUG1171"HFC-PCI: stat(%02x) s1(%02x)\n", stat, val);1172} else {1173/* shared */1174spin_unlock(&hc->lock);1175return IRQ_NONE;1176}1177hc->irqcnt++;11781179if (hc->dch.debug & DEBUG_HW_DCHANNEL)1180printk(KERN_DEBUG "HFC-PCI irq %x\n", val);1181val &= hc->hw.int_m1;1182if (val & 0x40) { /* state machine irq */1183exval = Read_hfc(hc, HFCPCI_STATES) & 0xf;1184if (hc->dch.debug & DEBUG_HW_DCHANNEL)1185printk(KERN_DEBUG "ph_state chg %d->%d\n",1186hc->dch.state, exval);1187hc->dch.state = exval;1188schedule_event(&hc->dch, FLG_PHCHANGE);1189val &= ~0x40;1190}1191if (val & 0x80) { /* timer irq */1192if (hc->hw.protocol == ISDN_P_NT_S0) {1193if ((--hc->hw.nt_timer) < 0)1194schedule_event(&hc->dch, FLG_PHCHANGE);1195}1196val &= ~0x80;1197Write_hfc(hc, HFCPCI_CTMT, hc->hw.ctmt | HFCPCI_CLTIMER);1198}1199if (val & 0x08) { /* B1 rx */1200bch = Sel_BCS(hc, hc->hw.bswapped ? 2 : 1);1201if (bch)1202main_rec_hfcpci(bch);1203else if (hc->dch.debug)1204printk(KERN_DEBUG "hfcpci spurious 0x08 IRQ\n");1205}1206if (val & 0x10) { /* B2 rx */1207bch = Sel_BCS(hc, 2);1208if (bch)1209main_rec_hfcpci(bch);1210else if (hc->dch.debug)1211printk(KERN_DEBUG "hfcpci spurious 0x10 IRQ\n");1212}1213if (val & 0x01) { /* B1 tx */1214bch = Sel_BCS(hc, hc->hw.bswapped ? 2 : 1);1215if (bch)1216tx_birq(bch);1217else if (hc->dch.debug)1218printk(KERN_DEBUG "hfcpci spurious 0x01 IRQ\n");1219}1220if (val & 0x02) { /* B2 tx */1221bch = Sel_BCS(hc, 2);1222if (bch)1223tx_birq(bch);1224else if (hc->dch.debug)1225printk(KERN_DEBUG "hfcpci spurious 0x02 IRQ\n");1226}1227if (val & 0x20) /* D rx */1228receive_dmsg(hc);1229if (val & 0x04) { /* D tx */1230if (test_and_clear_bit(FLG_BUSY_TIMER, &hc->dch.Flags))1231del_timer(&hc->dch.timer);1232tx_dirq(&hc->dch);1233}1234spin_unlock(&hc->lock);1235return IRQ_HANDLED;1236}12371238/*1239* timer callback for D-chan busy resolution. Currently no function1240*/1241static void1242hfcpci_dbusy_timer(struct hfc_pci *hc)1243{1244}12451246/*1247* activate/deactivate hardware for selected channels and mode1248*/1249static int1250mode_hfcpci(struct bchannel *bch, int bc, int protocol)1251{1252struct hfc_pci *hc = bch->hw;1253int fifo2;1254u_char rx_slot = 0, tx_slot = 0, pcm_mode;12551256if (bch->debug & DEBUG_HW_BCHANNEL)1257printk(KERN_DEBUG1258"HFCPCI bchannel protocol %x-->%x ch %x-->%x\n",1259bch->state, protocol, bch->nr, bc);12601261fifo2 = bc;1262pcm_mode = (bc>>24) & 0xff;1263if (pcm_mode) { /* PCM SLOT USE */1264if (!test_bit(HFC_CFG_PCM, &hc->cfg))1265printk(KERN_WARNING1266"%s: pcm channel id without HFC_CFG_PCM\n",1267__func__);1268rx_slot = (bc>>8) & 0xff;1269tx_slot = (bc>>16) & 0xff;1270bc = bc & 0xff;1271} else if (test_bit(HFC_CFG_PCM, &hc->cfg) && (protocol > ISDN_P_NONE))1272printk(KERN_WARNING "%s: no pcm channel id but HFC_CFG_PCM\n",1273__func__);1274if (hc->chanlimit > 1) {1275hc->hw.bswapped = 0; /* B1 and B2 normal mode */1276hc->hw.sctrl_e &= ~0x80;1277} else {1278if (bc & 2) {1279if (protocol != ISDN_P_NONE) {1280hc->hw.bswapped = 1; /* B1 and B2 exchanged */1281hc->hw.sctrl_e |= 0x80;1282} else {1283hc->hw.bswapped = 0; /* B1 and B2 normal mode */1284hc->hw.sctrl_e &= ~0x80;1285}1286fifo2 = 1;1287} else {1288hc->hw.bswapped = 0; /* B1 and B2 normal mode */1289hc->hw.sctrl_e &= ~0x80;1290}1291}1292switch (protocol) {1293case (-1): /* used for init */1294bch->state = -1;1295bch->nr = bc;1296case (ISDN_P_NONE):1297if (bch->state == ISDN_P_NONE)1298return 0;1299if (bc & 2) {1300hc->hw.sctrl &= ~SCTRL_B2_ENA;1301hc->hw.sctrl_r &= ~SCTRL_B2_ENA;1302} else {1303hc->hw.sctrl &= ~SCTRL_B1_ENA;1304hc->hw.sctrl_r &= ~SCTRL_B1_ENA;1305}1306if (fifo2 & 2) {1307hc->hw.fifo_en &= ~HFCPCI_FIFOEN_B2;1308hc->hw.int_m1 &= ~(HFCPCI_INTS_B2TRANS +1309HFCPCI_INTS_B2REC);1310} else {1311hc->hw.fifo_en &= ~HFCPCI_FIFOEN_B1;1312hc->hw.int_m1 &= ~(HFCPCI_INTS_B1TRANS +1313HFCPCI_INTS_B1REC);1314}1315#ifdef REVERSE_BITORDER1316if (bch->nr & 2)1317hc->hw.cirm &= 0x7f;1318else1319hc->hw.cirm &= 0xbf;1320#endif1321bch->state = ISDN_P_NONE;1322bch->nr = bc;1323test_and_clear_bit(FLG_HDLC, &bch->Flags);1324test_and_clear_bit(FLG_TRANSPARENT, &bch->Flags);1325break;1326case (ISDN_P_B_RAW):1327bch->state = protocol;1328bch->nr = bc;1329hfcpci_clear_fifo_rx(hc, (fifo2 & 2) ? 1 : 0);1330hfcpci_clear_fifo_tx(hc, (fifo2 & 2) ? 1 : 0);1331if (bc & 2) {1332hc->hw.sctrl |= SCTRL_B2_ENA;1333hc->hw.sctrl_r |= SCTRL_B2_ENA;1334#ifdef REVERSE_BITORDER1335hc->hw.cirm |= 0x80;1336#endif1337} else {1338hc->hw.sctrl |= SCTRL_B1_ENA;1339hc->hw.sctrl_r |= SCTRL_B1_ENA;1340#ifdef REVERSE_BITORDER1341hc->hw.cirm |= 0x40;1342#endif1343}1344if (fifo2 & 2) {1345hc->hw.fifo_en |= HFCPCI_FIFOEN_B2;1346if (!tics)1347hc->hw.int_m1 |= (HFCPCI_INTS_B2TRANS +1348HFCPCI_INTS_B2REC);1349hc->hw.ctmt |= 2;1350hc->hw.conn &= ~0x18;1351} else {1352hc->hw.fifo_en |= HFCPCI_FIFOEN_B1;1353if (!tics)1354hc->hw.int_m1 |= (HFCPCI_INTS_B1TRANS +1355HFCPCI_INTS_B1REC);1356hc->hw.ctmt |= 1;1357hc->hw.conn &= ~0x03;1358}1359test_and_set_bit(FLG_TRANSPARENT, &bch->Flags);1360break;1361case (ISDN_P_B_HDLC):1362bch->state = protocol;1363bch->nr = bc;1364hfcpci_clear_fifo_rx(hc, (fifo2 & 2) ? 1 : 0);1365hfcpci_clear_fifo_tx(hc, (fifo2 & 2) ? 1 : 0);1366if (bc & 2) {1367hc->hw.sctrl |= SCTRL_B2_ENA;1368hc->hw.sctrl_r |= SCTRL_B2_ENA;1369} else {1370hc->hw.sctrl |= SCTRL_B1_ENA;1371hc->hw.sctrl_r |= SCTRL_B1_ENA;1372}1373if (fifo2 & 2) {1374hc->hw.last_bfifo_cnt[1] = 0;1375hc->hw.fifo_en |= HFCPCI_FIFOEN_B2;1376hc->hw.int_m1 |= (HFCPCI_INTS_B2TRANS +1377HFCPCI_INTS_B2REC);1378hc->hw.ctmt &= ~2;1379hc->hw.conn &= ~0x18;1380} else {1381hc->hw.last_bfifo_cnt[0] = 0;1382hc->hw.fifo_en |= HFCPCI_FIFOEN_B1;1383hc->hw.int_m1 |= (HFCPCI_INTS_B1TRANS +1384HFCPCI_INTS_B1REC);1385hc->hw.ctmt &= ~1;1386hc->hw.conn &= ~0x03;1387}1388test_and_set_bit(FLG_HDLC, &bch->Flags);1389break;1390default:1391printk(KERN_DEBUG "prot not known %x\n", protocol);1392return -ENOPROTOOPT;1393}1394if (test_bit(HFC_CFG_PCM, &hc->cfg)) {1395if ((protocol == ISDN_P_NONE) ||1396(protocol == -1)) { /* init case */1397rx_slot = 0;1398tx_slot = 0;1399} else {1400if (test_bit(HFC_CFG_SW_DD_DU, &hc->cfg)) {1401rx_slot |= 0xC0;1402tx_slot |= 0xC0;1403} else {1404rx_slot |= 0x80;1405tx_slot |= 0x80;1406}1407}1408if (bc & 2) {1409hc->hw.conn &= 0xc7;1410hc->hw.conn |= 0x08;1411printk(KERN_DEBUG "%s: Write_hfc: B2_SSL 0x%x\n",1412__func__, tx_slot);1413printk(KERN_DEBUG "%s: Write_hfc: B2_RSL 0x%x\n",1414__func__, rx_slot);1415Write_hfc(hc, HFCPCI_B2_SSL, tx_slot);1416Write_hfc(hc, HFCPCI_B2_RSL, rx_slot);1417} else {1418hc->hw.conn &= 0xf8;1419hc->hw.conn |= 0x01;1420printk(KERN_DEBUG "%s: Write_hfc: B1_SSL 0x%x\n",1421__func__, tx_slot);1422printk(KERN_DEBUG "%s: Write_hfc: B1_RSL 0x%x\n",1423__func__, rx_slot);1424Write_hfc(hc, HFCPCI_B1_SSL, tx_slot);1425Write_hfc(hc, HFCPCI_B1_RSL, rx_slot);1426}1427}1428Write_hfc(hc, HFCPCI_SCTRL_E, hc->hw.sctrl_e);1429Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);1430Write_hfc(hc, HFCPCI_FIFO_EN, hc->hw.fifo_en);1431Write_hfc(hc, HFCPCI_SCTRL, hc->hw.sctrl);1432Write_hfc(hc, HFCPCI_SCTRL_R, hc->hw.sctrl_r);1433Write_hfc(hc, HFCPCI_CTMT, hc->hw.ctmt);1434Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);1435#ifdef REVERSE_BITORDER1436Write_hfc(hc, HFCPCI_CIRM, hc->hw.cirm);1437#endif1438return 0;1439}14401441static int1442set_hfcpci_rxtest(struct bchannel *bch, int protocol, int chan)1443{1444struct hfc_pci *hc = bch->hw;14451446if (bch->debug & DEBUG_HW_BCHANNEL)1447printk(KERN_DEBUG1448"HFCPCI bchannel test rx protocol %x-->%x ch %x-->%x\n",1449bch->state, protocol, bch->nr, chan);1450if (bch->nr != chan) {1451printk(KERN_DEBUG1452"HFCPCI rxtest wrong channel parameter %x/%x\n",1453bch->nr, chan);1454return -EINVAL;1455}1456switch (protocol) {1457case (ISDN_P_B_RAW):1458bch->state = protocol;1459hfcpci_clear_fifo_rx(hc, (chan & 2) ? 1 : 0);1460if (chan & 2) {1461hc->hw.sctrl_r |= SCTRL_B2_ENA;1462hc->hw.fifo_en |= HFCPCI_FIFOEN_B2RX;1463if (!tics)1464hc->hw.int_m1 |= HFCPCI_INTS_B2REC;1465hc->hw.ctmt |= 2;1466hc->hw.conn &= ~0x18;1467#ifdef REVERSE_BITORDER1468hc->hw.cirm |= 0x80;1469#endif1470} else {1471hc->hw.sctrl_r |= SCTRL_B1_ENA;1472hc->hw.fifo_en |= HFCPCI_FIFOEN_B1RX;1473if (!tics)1474hc->hw.int_m1 |= HFCPCI_INTS_B1REC;1475hc->hw.ctmt |= 1;1476hc->hw.conn &= ~0x03;1477#ifdef REVERSE_BITORDER1478hc->hw.cirm |= 0x40;1479#endif1480}1481break;1482case (ISDN_P_B_HDLC):1483bch->state = protocol;1484hfcpci_clear_fifo_rx(hc, (chan & 2) ? 1 : 0);1485if (chan & 2) {1486hc->hw.sctrl_r |= SCTRL_B2_ENA;1487hc->hw.last_bfifo_cnt[1] = 0;1488hc->hw.fifo_en |= HFCPCI_FIFOEN_B2RX;1489hc->hw.int_m1 |= HFCPCI_INTS_B2REC;1490hc->hw.ctmt &= ~2;1491hc->hw.conn &= ~0x18;1492} else {1493hc->hw.sctrl_r |= SCTRL_B1_ENA;1494hc->hw.last_bfifo_cnt[0] = 0;1495hc->hw.fifo_en |= HFCPCI_FIFOEN_B1RX;1496hc->hw.int_m1 |= HFCPCI_INTS_B1REC;1497hc->hw.ctmt &= ~1;1498hc->hw.conn &= ~0x03;1499}1500break;1501default:1502printk(KERN_DEBUG "prot not known %x\n", protocol);1503return -ENOPROTOOPT;1504}1505Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);1506Write_hfc(hc, HFCPCI_FIFO_EN, hc->hw.fifo_en);1507Write_hfc(hc, HFCPCI_SCTRL_R, hc->hw.sctrl_r);1508Write_hfc(hc, HFCPCI_CTMT, hc->hw.ctmt);1509Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);1510#ifdef REVERSE_BITORDER1511Write_hfc(hc, HFCPCI_CIRM, hc->hw.cirm);1512#endif1513return 0;1514}15151516static void1517deactivate_bchannel(struct bchannel *bch)1518{1519struct hfc_pci *hc = bch->hw;1520u_long flags;15211522spin_lock_irqsave(&hc->lock, flags);1523mISDN_clear_bchannel(bch);1524mode_hfcpci(bch, bch->nr, ISDN_P_NONE);1525spin_unlock_irqrestore(&hc->lock, flags);1526}15271528/*1529* Layer 1 B-channel hardware access1530*/1531static int1532channel_bctrl(struct bchannel *bch, struct mISDN_ctrl_req *cq)1533{1534int ret = 0;15351536switch (cq->op) {1537case MISDN_CTRL_GETOP:1538cq->op = MISDN_CTRL_FILL_EMPTY;1539break;1540case MISDN_CTRL_FILL_EMPTY: /* fill fifo, if empty */1541test_and_set_bit(FLG_FILLEMPTY, &bch->Flags);1542if (debug & DEBUG_HW_OPEN)1543printk(KERN_DEBUG "%s: FILL_EMPTY request (nr=%d "1544"off=%d)\n", __func__, bch->nr, !!cq->p1);1545break;1546default:1547printk(KERN_WARNING "%s: unknown Op %x\n", __func__, cq->op);1548ret = -EINVAL;1549break;1550}1551return ret;1552}1553static int1554hfc_bctrl(struct mISDNchannel *ch, u_int cmd, void *arg)1555{1556struct bchannel *bch = container_of(ch, struct bchannel, ch);1557struct hfc_pci *hc = bch->hw;1558int ret = -EINVAL;1559u_long flags;15601561if (bch->debug & DEBUG_HW)1562printk(KERN_DEBUG "%s: cmd:%x %p\n", __func__, cmd, arg);1563switch (cmd) {1564case HW_TESTRX_RAW:1565spin_lock_irqsave(&hc->lock, flags);1566ret = set_hfcpci_rxtest(bch, ISDN_P_B_RAW, (int)(long)arg);1567spin_unlock_irqrestore(&hc->lock, flags);1568break;1569case HW_TESTRX_HDLC:1570spin_lock_irqsave(&hc->lock, flags);1571ret = set_hfcpci_rxtest(bch, ISDN_P_B_HDLC, (int)(long)arg);1572spin_unlock_irqrestore(&hc->lock, flags);1573break;1574case HW_TESTRX_OFF:1575spin_lock_irqsave(&hc->lock, flags);1576mode_hfcpci(bch, bch->nr, ISDN_P_NONE);1577spin_unlock_irqrestore(&hc->lock, flags);1578ret = 0;1579break;1580case CLOSE_CHANNEL:1581test_and_clear_bit(FLG_OPEN, &bch->Flags);1582if (test_bit(FLG_ACTIVE, &bch->Flags))1583deactivate_bchannel(bch);1584ch->protocol = ISDN_P_NONE;1585ch->peer = NULL;1586module_put(THIS_MODULE);1587ret = 0;1588break;1589case CONTROL_CHANNEL:1590ret = channel_bctrl(bch, arg);1591break;1592default:1593printk(KERN_WARNING "%s: unknown prim(%x)\n",1594__func__, cmd);1595}1596return ret;1597}15981599/*1600* Layer2 -> Layer 1 Dchannel data1601*/1602static int1603hfcpci_l2l1D(struct mISDNchannel *ch, struct sk_buff *skb)1604{1605struct mISDNdevice *dev = container_of(ch, struct mISDNdevice, D);1606struct dchannel *dch = container_of(dev, struct dchannel, dev);1607struct hfc_pci *hc = dch->hw;1608int ret = -EINVAL;1609struct mISDNhead *hh = mISDN_HEAD_P(skb);1610unsigned int id;1611u_long flags;16121613switch (hh->prim) {1614case PH_DATA_REQ:1615spin_lock_irqsave(&hc->lock, flags);1616ret = dchannel_senddata(dch, skb);1617if (ret > 0) { /* direct TX */1618id = hh->id; /* skb can be freed */1619hfcpci_fill_dfifo(dch->hw);1620ret = 0;1621spin_unlock_irqrestore(&hc->lock, flags);1622queue_ch_frame(ch, PH_DATA_CNF, id, NULL);1623} else1624spin_unlock_irqrestore(&hc->lock, flags);1625return ret;1626case PH_ACTIVATE_REQ:1627spin_lock_irqsave(&hc->lock, flags);1628if (hc->hw.protocol == ISDN_P_NT_S0) {1629ret = 0;1630if (test_bit(HFC_CFG_MASTER, &hc->cfg))1631hc->hw.mst_m |= HFCPCI_MASTER;1632Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);1633if (test_bit(FLG_ACTIVE, &dch->Flags)) {1634spin_unlock_irqrestore(&hc->lock, flags);1635_queue_data(&dch->dev.D, PH_ACTIVATE_IND,1636MISDN_ID_ANY, 0, NULL, GFP_ATOMIC);1637break;1638}1639test_and_set_bit(FLG_L2_ACTIVATED, &dch->Flags);1640Write_hfc(hc, HFCPCI_STATES, HFCPCI_ACTIVATE |1641HFCPCI_DO_ACTION | 1);1642} else1643ret = l1_event(dch->l1, hh->prim);1644spin_unlock_irqrestore(&hc->lock, flags);1645break;1646case PH_DEACTIVATE_REQ:1647test_and_clear_bit(FLG_L2_ACTIVATED, &dch->Flags);1648spin_lock_irqsave(&hc->lock, flags);1649if (hc->hw.protocol == ISDN_P_NT_S0) {1650/* prepare deactivation */1651Write_hfc(hc, HFCPCI_STATES, 0x40);1652skb_queue_purge(&dch->squeue);1653if (dch->tx_skb) {1654dev_kfree_skb(dch->tx_skb);1655dch->tx_skb = NULL;1656}1657dch->tx_idx = 0;1658if (dch->rx_skb) {1659dev_kfree_skb(dch->rx_skb);1660dch->rx_skb = NULL;1661}1662test_and_clear_bit(FLG_TX_BUSY, &dch->Flags);1663if (test_and_clear_bit(FLG_BUSY_TIMER, &dch->Flags))1664del_timer(&dch->timer);1665#ifdef FIXME1666if (test_and_clear_bit(FLG_L1_BUSY, &dch->Flags))1667dchannel_sched_event(&hc->dch, D_CLEARBUSY);1668#endif1669hc->hw.mst_m &= ~HFCPCI_MASTER;1670Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);1671ret = 0;1672} else {1673ret = l1_event(dch->l1, hh->prim);1674}1675spin_unlock_irqrestore(&hc->lock, flags);1676break;1677}1678if (!ret)1679dev_kfree_skb(skb);1680return ret;1681}16821683/*1684* Layer2 -> Layer 1 Bchannel data1685*/1686static int1687hfcpci_l2l1B(struct mISDNchannel *ch, struct sk_buff *skb)1688{1689struct bchannel *bch = container_of(ch, struct bchannel, ch);1690struct hfc_pci *hc = bch->hw;1691int ret = -EINVAL;1692struct mISDNhead *hh = mISDN_HEAD_P(skb);1693unsigned int id;1694u_long flags;16951696switch (hh->prim) {1697case PH_DATA_REQ:1698spin_lock_irqsave(&hc->lock, flags);1699ret = bchannel_senddata(bch, skb);1700if (ret > 0) { /* direct TX */1701id = hh->id; /* skb can be freed */1702hfcpci_fill_fifo(bch);1703ret = 0;1704spin_unlock_irqrestore(&hc->lock, flags);1705if (!test_bit(FLG_TRANSPARENT, &bch->Flags))1706queue_ch_frame(ch, PH_DATA_CNF, id, NULL);1707} else1708spin_unlock_irqrestore(&hc->lock, flags);1709return ret;1710case PH_ACTIVATE_REQ:1711spin_lock_irqsave(&hc->lock, flags);1712if (!test_and_set_bit(FLG_ACTIVE, &bch->Flags))1713ret = mode_hfcpci(bch, bch->nr, ch->protocol);1714else1715ret = 0;1716spin_unlock_irqrestore(&hc->lock, flags);1717if (!ret)1718_queue_data(ch, PH_ACTIVATE_IND, MISDN_ID_ANY, 0,1719NULL, GFP_KERNEL);1720break;1721case PH_DEACTIVATE_REQ:1722deactivate_bchannel(bch);1723_queue_data(ch, PH_DEACTIVATE_IND, MISDN_ID_ANY, 0,1724NULL, GFP_KERNEL);1725ret = 0;1726break;1727}1728if (!ret)1729dev_kfree_skb(skb);1730return ret;1731}17321733/*1734* called for card init message1735*/17361737static void1738inithfcpci(struct hfc_pci *hc)1739{1740printk(KERN_DEBUG "inithfcpci: entered\n");1741hc->dch.timer.function = (void *) hfcpci_dbusy_timer;1742hc->dch.timer.data = (long) &hc->dch;1743init_timer(&hc->dch.timer);1744hc->chanlimit = 2;1745mode_hfcpci(&hc->bch[0], 1, -1);1746mode_hfcpci(&hc->bch[1], 2, -1);1747}174817491750static int1751init_card(struct hfc_pci *hc)1752{1753int cnt = 3;1754u_long flags;17551756printk(KERN_DEBUG "init_card: entered\n");175717581759spin_lock_irqsave(&hc->lock, flags);1760disable_hwirq(hc);1761spin_unlock_irqrestore(&hc->lock, flags);1762if (request_irq(hc->irq, hfcpci_int, IRQF_SHARED, "HFC PCI", hc)) {1763printk(KERN_WARNING1764"mISDN: couldn't get interrupt %d\n", hc->irq);1765return -EIO;1766}1767spin_lock_irqsave(&hc->lock, flags);1768reset_hfcpci(hc);1769while (cnt) {1770inithfcpci(hc);1771/*1772* Finally enable IRQ output1773* this is only allowed, if an IRQ routine is already1774* established for this HFC, so don't do that earlier1775*/1776enable_hwirq(hc);1777spin_unlock_irqrestore(&hc->lock, flags);1778/* Timeout 80ms */1779current->state = TASK_UNINTERRUPTIBLE;1780schedule_timeout((80*HZ)/1000);1781printk(KERN_INFO "HFC PCI: IRQ %d count %d\n",1782hc->irq, hc->irqcnt);1783/* now switch timer interrupt off */1784spin_lock_irqsave(&hc->lock, flags);1785hc->hw.int_m1 &= ~HFCPCI_INTS_TIMER;1786Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);1787/* reinit mode reg */1788Write_hfc(hc, HFCPCI_MST_MODE, hc->hw.mst_m);1789if (!hc->irqcnt) {1790printk(KERN_WARNING1791"HFC PCI: IRQ(%d) getting no interrupts "1792"during init %d\n", hc->irq, 4 - cnt);1793if (cnt == 1)1794break;1795else {1796reset_hfcpci(hc);1797cnt--;1798}1799} else {1800spin_unlock_irqrestore(&hc->lock, flags);1801hc->initdone = 1;1802return 0;1803}1804}1805disable_hwirq(hc);1806spin_unlock_irqrestore(&hc->lock, flags);1807free_irq(hc->irq, hc);1808return -EIO;1809}18101811static int1812channel_ctrl(struct hfc_pci *hc, struct mISDN_ctrl_req *cq)1813{1814int ret = 0;1815u_char slot;18161817switch (cq->op) {1818case MISDN_CTRL_GETOP:1819cq->op = MISDN_CTRL_LOOP | MISDN_CTRL_CONNECT |1820MISDN_CTRL_DISCONNECT;1821break;1822case MISDN_CTRL_LOOP:1823/* channel 0 disabled loop */1824if (cq->channel < 0 || cq->channel > 2) {1825ret = -EINVAL;1826break;1827}1828if (cq->channel & 1) {1829if (test_bit(HFC_CFG_SW_DD_DU, &hc->cfg))1830slot = 0xC0;1831else1832slot = 0x80;1833printk(KERN_DEBUG "%s: Write_hfc: B1_SSL/RSL 0x%x\n",1834__func__, slot);1835Write_hfc(hc, HFCPCI_B1_SSL, slot);1836Write_hfc(hc, HFCPCI_B1_RSL, slot);1837hc->hw.conn = (hc->hw.conn & ~7) | 6;1838Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);1839}1840if (cq->channel & 2) {1841if (test_bit(HFC_CFG_SW_DD_DU, &hc->cfg))1842slot = 0xC1;1843else1844slot = 0x81;1845printk(KERN_DEBUG "%s: Write_hfc: B2_SSL/RSL 0x%x\n",1846__func__, slot);1847Write_hfc(hc, HFCPCI_B2_SSL, slot);1848Write_hfc(hc, HFCPCI_B2_RSL, slot);1849hc->hw.conn = (hc->hw.conn & ~0x38) | 0x30;1850Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);1851}1852if (cq->channel & 3)1853hc->hw.trm |= 0x80; /* enable IOM-loop */1854else {1855hc->hw.conn = (hc->hw.conn & ~0x3f) | 0x09;1856Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);1857hc->hw.trm &= 0x7f; /* disable IOM-loop */1858}1859Write_hfc(hc, HFCPCI_TRM, hc->hw.trm);1860break;1861case MISDN_CTRL_CONNECT:1862if (cq->channel == cq->p1) {1863ret = -EINVAL;1864break;1865}1866if (cq->channel < 1 || cq->channel > 2 ||1867cq->p1 < 1 || cq->p1 > 2) {1868ret = -EINVAL;1869break;1870}1871if (test_bit(HFC_CFG_SW_DD_DU, &hc->cfg))1872slot = 0xC0;1873else1874slot = 0x80;1875printk(KERN_DEBUG "%s: Write_hfc: B1_SSL/RSL 0x%x\n",1876__func__, slot);1877Write_hfc(hc, HFCPCI_B1_SSL, slot);1878Write_hfc(hc, HFCPCI_B2_RSL, slot);1879if (test_bit(HFC_CFG_SW_DD_DU, &hc->cfg))1880slot = 0xC1;1881else1882slot = 0x81;1883printk(KERN_DEBUG "%s: Write_hfc: B2_SSL/RSL 0x%x\n",1884__func__, slot);1885Write_hfc(hc, HFCPCI_B2_SSL, slot);1886Write_hfc(hc, HFCPCI_B1_RSL, slot);1887hc->hw.conn = (hc->hw.conn & ~0x3f) | 0x36;1888Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);1889hc->hw.trm |= 0x80;1890Write_hfc(hc, HFCPCI_TRM, hc->hw.trm);1891break;1892case MISDN_CTRL_DISCONNECT:1893hc->hw.conn = (hc->hw.conn & ~0x3f) | 0x09;1894Write_hfc(hc, HFCPCI_CONNECT, hc->hw.conn);1895hc->hw.trm &= 0x7f; /* disable IOM-loop */1896break;1897default:1898printk(KERN_WARNING "%s: unknown Op %x\n",1899__func__, cq->op);1900ret = -EINVAL;1901break;1902}1903return ret;1904}19051906static int1907open_dchannel(struct hfc_pci *hc, struct mISDNchannel *ch,1908struct channel_req *rq)1909{1910int err = 0;19111912if (debug & DEBUG_HW_OPEN)1913printk(KERN_DEBUG "%s: dev(%d) open from %p\n", __func__,1914hc->dch.dev.id, __builtin_return_address(0));1915if (rq->protocol == ISDN_P_NONE)1916return -EINVAL;1917if (rq->adr.channel == 1) {1918/* TODO: E-Channel */1919return -EINVAL;1920}1921if (!hc->initdone) {1922if (rq->protocol == ISDN_P_TE_S0) {1923err = create_l1(&hc->dch, hfc_l1callback);1924if (err)1925return err;1926}1927hc->hw.protocol = rq->protocol;1928ch->protocol = rq->protocol;1929err = init_card(hc);1930if (err)1931return err;1932} else {1933if (rq->protocol != ch->protocol) {1934if (hc->hw.protocol == ISDN_P_TE_S0)1935l1_event(hc->dch.l1, CLOSE_CHANNEL);1936if (rq->protocol == ISDN_P_TE_S0) {1937err = create_l1(&hc->dch, hfc_l1callback);1938if (err)1939return err;1940}1941hc->hw.protocol = rq->protocol;1942ch->protocol = rq->protocol;1943hfcpci_setmode(hc);1944}1945}19461947if (((ch->protocol == ISDN_P_NT_S0) && (hc->dch.state == 3)) ||1948((ch->protocol == ISDN_P_TE_S0) && (hc->dch.state == 7))) {1949_queue_data(ch, PH_ACTIVATE_IND, MISDN_ID_ANY,19500, NULL, GFP_KERNEL);1951}1952rq->ch = ch;1953if (!try_module_get(THIS_MODULE))1954printk(KERN_WARNING "%s:cannot get module\n", __func__);1955return 0;1956}19571958static int1959open_bchannel(struct hfc_pci *hc, struct channel_req *rq)1960{1961struct bchannel *bch;19621963if (rq->adr.channel > 2)1964return -EINVAL;1965if (rq->protocol == ISDN_P_NONE)1966return -EINVAL;1967bch = &hc->bch[rq->adr.channel - 1];1968if (test_and_set_bit(FLG_OPEN, &bch->Flags))1969return -EBUSY; /* b-channel can be only open once */1970test_and_clear_bit(FLG_FILLEMPTY, &bch->Flags);1971bch->ch.protocol = rq->protocol;1972rq->ch = &bch->ch; /* TODO: E-channel */1973if (!try_module_get(THIS_MODULE))1974printk(KERN_WARNING "%s:cannot get module\n", __func__);1975return 0;1976}19771978/*1979* device control function1980*/1981static int1982hfc_dctrl(struct mISDNchannel *ch, u_int cmd, void *arg)1983{1984struct mISDNdevice *dev = container_of(ch, struct mISDNdevice, D);1985struct dchannel *dch = container_of(dev, struct dchannel, dev);1986struct hfc_pci *hc = dch->hw;1987struct channel_req *rq;1988int err = 0;19891990if (dch->debug & DEBUG_HW)1991printk(KERN_DEBUG "%s: cmd:%x %p\n",1992__func__, cmd, arg);1993switch (cmd) {1994case OPEN_CHANNEL:1995rq = arg;1996if ((rq->protocol == ISDN_P_TE_S0) ||1997(rq->protocol == ISDN_P_NT_S0))1998err = open_dchannel(hc, ch, rq);1999else2000err = open_bchannel(hc, rq);2001break;2002case CLOSE_CHANNEL:2003if (debug & DEBUG_HW_OPEN)2004printk(KERN_DEBUG "%s: dev(%d) close from %p\n",2005__func__, hc->dch.dev.id,2006__builtin_return_address(0));2007module_put(THIS_MODULE);2008break;2009case CONTROL_CHANNEL:2010err = channel_ctrl(hc, arg);2011break;2012default:2013if (dch->debug & DEBUG_HW)2014printk(KERN_DEBUG "%s: unknown command %x\n",2015__func__, cmd);2016return -EINVAL;2017}2018return err;2019}20202021static int2022setup_hw(struct hfc_pci *hc)2023{2024void *buffer;20252026printk(KERN_INFO "mISDN: HFC-PCI driver %s\n", hfcpci_revision);2027hc->hw.cirm = 0;2028hc->dch.state = 0;2029pci_set_master(hc->pdev);2030if (!hc->irq) {2031printk(KERN_WARNING "HFC-PCI: No IRQ for PCI card found\n");2032return 1;2033}2034hc->hw.pci_io =2035(char __iomem *)(unsigned long)hc->pdev->resource[1].start;20362037if (!hc->hw.pci_io) {2038printk(KERN_WARNING "HFC-PCI: No IO-Mem for PCI card found\n");2039return 1;2040}2041/* Allocate memory for FIFOS */2042/* the memory needs to be on a 32k boundary within the first 4G */2043pci_set_dma_mask(hc->pdev, 0xFFFF8000);2044buffer = pci_alloc_consistent(hc->pdev, 0x8000, &hc->hw.dmahandle);2045/* We silently assume the address is okay if nonzero */2046if (!buffer) {2047printk(KERN_WARNING2048"HFC-PCI: Error allocating memory for FIFO!\n");2049return 1;2050}2051hc->hw.fifos = buffer;2052pci_write_config_dword(hc->pdev, 0x80, hc->hw.dmahandle);2053hc->hw.pci_io = ioremap((ulong) hc->hw.pci_io, 256);2054printk(KERN_INFO2055"HFC-PCI: defined at mem %#lx fifo %#lx(%#lx) IRQ %d HZ %d\n",2056(u_long) hc->hw.pci_io, (u_long) hc->hw.fifos,2057(u_long) hc->hw.dmahandle, hc->irq, HZ);2058/* enable memory mapped ports, disable busmaster */2059pci_write_config_word(hc->pdev, PCI_COMMAND, PCI_ENA_MEMIO);2060hc->hw.int_m2 = 0;2061disable_hwirq(hc);2062hc->hw.int_m1 = 0;2063Write_hfc(hc, HFCPCI_INT_M1, hc->hw.int_m1);2064/* At this point the needed PCI config is done */2065/* fifos are still not enabled */2066hc->hw.timer.function = (void *) hfcpci_Timer;2067hc->hw.timer.data = (long) hc;2068init_timer(&hc->hw.timer);2069/* default PCM master */2070test_and_set_bit(HFC_CFG_MASTER, &hc->cfg);2071return 0;2072}20732074static void2075release_card(struct hfc_pci *hc) {2076u_long flags;20772078spin_lock_irqsave(&hc->lock, flags);2079hc->hw.int_m2 = 0; /* interrupt output off ! */2080disable_hwirq(hc);2081mode_hfcpci(&hc->bch[0], 1, ISDN_P_NONE);2082mode_hfcpci(&hc->bch[1], 2, ISDN_P_NONE);2083if (hc->dch.timer.function != NULL) {2084del_timer(&hc->dch.timer);2085hc->dch.timer.function = NULL;2086}2087spin_unlock_irqrestore(&hc->lock, flags);2088if (hc->hw.protocol == ISDN_P_TE_S0)2089l1_event(hc->dch.l1, CLOSE_CHANNEL);2090if (hc->initdone)2091free_irq(hc->irq, hc);2092release_io_hfcpci(hc); /* must release after free_irq! */2093mISDN_unregister_device(&hc->dch.dev);2094mISDN_freebchannel(&hc->bch[1]);2095mISDN_freebchannel(&hc->bch[0]);2096mISDN_freedchannel(&hc->dch);2097pci_set_drvdata(hc->pdev, NULL);2098kfree(hc);2099}21002101static int2102setup_card(struct hfc_pci *card)2103{2104int err = -EINVAL;2105u_int i;2106char name[MISDN_MAX_IDLEN];21072108card->dch.debug = debug;2109spin_lock_init(&card->lock);2110mISDN_initdchannel(&card->dch, MAX_DFRAME_LEN_L1, ph_state);2111card->dch.hw = card;2112card->dch.dev.Dprotocols = (1 << ISDN_P_TE_S0) | (1 << ISDN_P_NT_S0);2113card->dch.dev.Bprotocols = (1 << (ISDN_P_B_RAW & ISDN_P_B_MASK)) |2114(1 << (ISDN_P_B_HDLC & ISDN_P_B_MASK));2115card->dch.dev.D.send = hfcpci_l2l1D;2116card->dch.dev.D.ctrl = hfc_dctrl;2117card->dch.dev.nrbchan = 2;2118for (i = 0; i < 2; i++) {2119card->bch[i].nr = i + 1;2120set_channelmap(i + 1, card->dch.dev.channelmap);2121card->bch[i].debug = debug;2122mISDN_initbchannel(&card->bch[i], MAX_DATA_MEM);2123card->bch[i].hw = card;2124card->bch[i].ch.send = hfcpci_l2l1B;2125card->bch[i].ch.ctrl = hfc_bctrl;2126card->bch[i].ch.nr = i + 1;2127list_add(&card->bch[i].ch.list, &card->dch.dev.bchannels);2128}2129err = setup_hw(card);2130if (err)2131goto error;2132snprintf(name, MISDN_MAX_IDLEN - 1, "hfc-pci.%d", HFC_cnt + 1);2133err = mISDN_register_device(&card->dch.dev, &card->pdev->dev, name);2134if (err)2135goto error;2136HFC_cnt++;2137printk(KERN_INFO "HFC %d cards installed\n", HFC_cnt);2138return 0;2139error:2140mISDN_freebchannel(&card->bch[1]);2141mISDN_freebchannel(&card->bch[0]);2142mISDN_freedchannel(&card->dch);2143kfree(card);2144return err;2145}21462147/* private data in the PCI devices list */2148struct _hfc_map {2149u_int subtype;2150u_int flag;2151char *name;2152};21532154static const struct _hfc_map hfc_map[] =2155{2156{HFC_CCD_2BD0, 0, "CCD/Billion/Asuscom 2BD0"},2157{HFC_CCD_B000, 0, "Billion B000"},2158{HFC_CCD_B006, 0, "Billion B006"},2159{HFC_CCD_B007, 0, "Billion B007"},2160{HFC_CCD_B008, 0, "Billion B008"},2161{HFC_CCD_B009, 0, "Billion B009"},2162{HFC_CCD_B00A, 0, "Billion B00A"},2163{HFC_CCD_B00B, 0, "Billion B00B"},2164{HFC_CCD_B00C, 0, "Billion B00C"},2165{HFC_CCD_B100, 0, "Seyeon B100"},2166{HFC_CCD_B700, 0, "Primux II S0 B700"},2167{HFC_CCD_B701, 0, "Primux II S0 NT B701"},2168{HFC_ABOCOM_2BD1, 0, "Abocom/Magitek 2BD1"},2169{HFC_ASUS_0675, 0, "Asuscom/Askey 675"},2170{HFC_BERKOM_TCONCEPT, 0, "German telekom T-Concept"},2171{HFC_BERKOM_A1T, 0, "German telekom A1T"},2172{HFC_ANIGMA_MC145575, 0, "Motorola MC145575"},2173{HFC_ZOLTRIX_2BD0, 0, "Zoltrix 2BD0"},2174{HFC_DIGI_DF_M_IOM2_E, 0,2175"Digi International DataFire Micro V IOM2 (Europe)"},2176{HFC_DIGI_DF_M_E, 0,2177"Digi International DataFire Micro V (Europe)"},2178{HFC_DIGI_DF_M_IOM2_A, 0,2179"Digi International DataFire Micro V IOM2 (North America)"},2180{HFC_DIGI_DF_M_A, 0,2181"Digi International DataFire Micro V (North America)"},2182{HFC_SITECOM_DC105V2, 0, "Sitecom Connectivity DC-105 ISDN TA"},2183{},2184};21852186static struct pci_device_id hfc_ids[] =2187{2188{ PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_2BD0),2189(unsigned long) &hfc_map[0] },2190{ PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B000),2191(unsigned long) &hfc_map[1] },2192{ PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B006),2193(unsigned long) &hfc_map[2] },2194{ PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B007),2195(unsigned long) &hfc_map[3] },2196{ PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B008),2197(unsigned long) &hfc_map[4] },2198{ PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B009),2199(unsigned long) &hfc_map[5] },2200{ PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B00A),2201(unsigned long) &hfc_map[6] },2202{ PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B00B),2203(unsigned long) &hfc_map[7] },2204{ PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B00C),2205(unsigned long) &hfc_map[8] },2206{ PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B100),2207(unsigned long) &hfc_map[9] },2208{ PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B700),2209(unsigned long) &hfc_map[10] },2210{ PCI_VDEVICE(CCD, PCI_DEVICE_ID_CCD_B701),2211(unsigned long) &hfc_map[11] },2212{ PCI_VDEVICE(ABOCOM, PCI_DEVICE_ID_ABOCOM_2BD1),2213(unsigned long) &hfc_map[12] },2214{ PCI_VDEVICE(ASUSTEK, PCI_DEVICE_ID_ASUSTEK_0675),2215(unsigned long) &hfc_map[13] },2216{ PCI_VDEVICE(BERKOM, PCI_DEVICE_ID_BERKOM_T_CONCEPT),2217(unsigned long) &hfc_map[14] },2218{ PCI_VDEVICE(BERKOM, PCI_DEVICE_ID_BERKOM_A1T),2219(unsigned long) &hfc_map[15] },2220{ PCI_VDEVICE(ANIGMA, PCI_DEVICE_ID_ANIGMA_MC145575),2221(unsigned long) &hfc_map[16] },2222{ PCI_VDEVICE(ZOLTRIX, PCI_DEVICE_ID_ZOLTRIX_2BD0),2223(unsigned long) &hfc_map[17] },2224{ PCI_VDEVICE(DIGI, PCI_DEVICE_ID_DIGI_DF_M_IOM2_E),2225(unsigned long) &hfc_map[18] },2226{ PCI_VDEVICE(DIGI, PCI_DEVICE_ID_DIGI_DF_M_E),2227(unsigned long) &hfc_map[19] },2228{ PCI_VDEVICE(DIGI, PCI_DEVICE_ID_DIGI_DF_M_IOM2_A),2229(unsigned long) &hfc_map[20] },2230{ PCI_VDEVICE(DIGI, PCI_DEVICE_ID_DIGI_DF_M_A),2231(unsigned long) &hfc_map[21] },2232{ PCI_VDEVICE(SITECOM, PCI_DEVICE_ID_SITECOM_DC105V2),2233(unsigned long) &hfc_map[22] },2234{},2235};22362237static int __devinit2238hfc_probe(struct pci_dev *pdev, const struct pci_device_id *ent)2239{2240int err = -ENOMEM;2241struct hfc_pci *card;2242struct _hfc_map *m = (struct _hfc_map *)ent->driver_data;22432244card = kzalloc(sizeof(struct hfc_pci), GFP_ATOMIC);2245if (!card) {2246printk(KERN_ERR "No kmem for HFC card\n");2247return err;2248}2249card->pdev = pdev;2250card->subtype = m->subtype;2251err = pci_enable_device(pdev);2252if (err) {2253kfree(card);2254return err;2255}22562257printk(KERN_INFO "mISDN_hfcpci: found adapter %s at %s\n",2258m->name, pci_name(pdev));22592260card->irq = pdev->irq;2261pci_set_drvdata(pdev, card);2262err = setup_card(card);2263if (err)2264pci_set_drvdata(pdev, NULL);2265return err;2266}22672268static void __devexit2269hfc_remove_pci(struct pci_dev *pdev)2270{2271struct hfc_pci *card = pci_get_drvdata(pdev);22722273if (card)2274release_card(card);2275else2276if (debug)2277printk(KERN_DEBUG "%s: drvdata already removed\n",2278__func__);2279}228022812282static struct pci_driver hfc_driver = {2283.name = "hfcpci",2284.probe = hfc_probe,2285.remove = __devexit_p(hfc_remove_pci),2286.id_table = hfc_ids,2287};22882289static int2290_hfcpci_softirq(struct device *dev, void *arg)2291{2292struct hfc_pci *hc = dev_get_drvdata(dev);2293struct bchannel *bch;2294if (hc == NULL)2295return 0;22962297if (hc->hw.int_m2 & HFCPCI_IRQ_ENABLE) {2298spin_lock(&hc->lock);2299bch = Sel_BCS(hc, hc->hw.bswapped ? 2 : 1);2300if (bch && bch->state == ISDN_P_B_RAW) { /* B1 rx&tx */2301main_rec_hfcpci(bch);2302tx_birq(bch);2303}2304bch = Sel_BCS(hc, hc->hw.bswapped ? 1 : 2);2305if (bch && bch->state == ISDN_P_B_RAW) { /* B2 rx&tx */2306main_rec_hfcpci(bch);2307tx_birq(bch);2308}2309spin_unlock(&hc->lock);2310}2311return 0;2312}23132314static void2315hfcpci_softirq(void *arg)2316{2317(void) driver_for_each_device(&hfc_driver.driver, NULL, arg,2318_hfcpci_softirq);23192320/* if next event would be in the past ... */2321if ((s32)(hfc_jiffies + tics - jiffies) <= 0)2322hfc_jiffies = jiffies + 1;2323else2324hfc_jiffies += tics;2325hfc_tl.expires = hfc_jiffies;2326add_timer(&hfc_tl);2327}23282329static int __init2330HFC_init(void)2331{2332int err;23332334if (!poll)2335poll = HFCPCI_BTRANS_THRESHOLD;23362337if (poll != HFCPCI_BTRANS_THRESHOLD) {2338tics = (poll * HZ) / 8000;2339if (tics < 1)2340tics = 1;2341poll = (tics * 8000) / HZ;2342if (poll > 256 || poll < 8) {2343printk(KERN_ERR "%s: Wrong poll value %d not in range "2344"of 8..256.\n", __func__, poll);2345err = -EINVAL;2346return err;2347}2348}2349if (poll != HFCPCI_BTRANS_THRESHOLD) {2350printk(KERN_INFO "%s: Using alternative poll value of %d\n",2351__func__, poll);2352hfc_tl.function = (void *)hfcpci_softirq;2353hfc_tl.data = 0;2354init_timer(&hfc_tl);2355hfc_tl.expires = jiffies + tics;2356hfc_jiffies = hfc_tl.expires;2357add_timer(&hfc_tl);2358} else2359tics = 0; /* indicate the use of controller's timer */23602361err = pci_register_driver(&hfc_driver);2362if (err) {2363if (timer_pending(&hfc_tl))2364del_timer(&hfc_tl);2365}23662367return err;2368}23692370static void __exit2371HFC_cleanup(void)2372{2373if (timer_pending(&hfc_tl))2374del_timer(&hfc_tl);23752376pci_unregister_driver(&hfc_driver);2377}23782379module_init(HFC_init);2380module_exit(HFC_cleanup);23812382MODULE_DEVICE_TABLE(pci, hfc_ids);238323842385