aboutsummaryrefslogtreecommitdiff
path: root/sys
diff options
context:
space:
mode:
Diffstat (limited to 'sys')
-rw-r--r--sys/dev/ctau/ctau.c1796
-rw-r--r--sys/dev/ctau/if_ct.c2752
-rw-r--r--sys/dev/cx/if_cx.c3270
-rw-r--r--sys/modules/ctau/Makefile37
-rw-r--r--sys/modules/netgraph/vlan/Makefile6
-rw-r--r--sys/netgraph/ng_vlan.c444
-rw-r--r--sys/netgraph/ng_vlan.h75
7 files changed, 8380 insertions, 0 deletions
diff --git a/sys/dev/ctau/ctau.c b/sys/dev/ctau/ctau.c
new file mode 100644
index 000000000000..37cbd4698caa
--- /dev/null
+++ b/sys/dev/ctau/ctau.c
@@ -0,0 +1,1796 @@
+/*
+ * Low-level subroutines for Cronyx-Tau adapter.
+ *
+ * Copyright (C) 1994-2001 Cronyx Engineering.
+ * Author: Serge Vakulenko, <vak@cronyx.ru>
+ *
+ * Copyright (C) 2003 Cronyx Engineering.
+ * Author: Roman Kurakin, <rik@cronyx.ru>
+ *
+ * This software is distributed with NO WARRANTIES, not even the implied
+ * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * Authors grant any other persons or organisations permission to use
+ * or modify this software as long as this message is kept with the software,
+ * all derivative works or modified versions.
+ *
+ * Cronyx Id: ctau.c,v 1.1.2.4 2003/12/11 17:33:43 rik Exp $
+ */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <dev/cx/machdep.h>
+#include <dev/ctau/ctddk.h>
+#include <dev/ctau/ctaureg.h>
+#include <dev/ctau/hdc64570.h>
+#include <dev/ctau/ds2153.h>
+#include <dev/ctau/am8530.h>
+#include <dev/ctau/lxt318.h>
+#include <dev/cx/cronyxfw.h>
+
+#define DMA_MASK 0xd4 /* DMA mask register */
+#define DMA_MASK_CLEAR 0x04 /* DMA clear mask */
+#define DMA_MODE 0xd6 /* DMA mode register */
+#define DMA_MODE_MASTER 0xc0 /* DMA master mode */
+
+#define BYTE *(unsigned char*)&
+
+static unsigned char irqmask [] = {
+ BCR0_IRQ_DIS, BCR0_IRQ_DIS, BCR0_IRQ_DIS, BCR0_IRQ_3,
+ BCR0_IRQ_DIS, BCR0_IRQ_5, BCR0_IRQ_DIS, BCR0_IRQ_7,
+ BCR0_IRQ_DIS, BCR0_IRQ_DIS, BCR0_IRQ_10, BCR0_IRQ_11,
+ BCR0_IRQ_12, BCR0_IRQ_DIS, BCR0_IRQ_DIS, BCR0_IRQ_15,
+};
+
+static unsigned char dmamask [] = {
+ BCR0_DMA_DIS, BCR0_DMA_DIS, BCR0_DMA_DIS, BCR0_DMA_DIS,
+ BCR0_DMA_DIS, BCR0_DMA_5, BCR0_DMA_6, BCR0_DMA_7,
+};
+
+static short porttab [] = { /* standard base port set */
+ 0x200, 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0,
+ 0x300, 0x320, 0x340, 0x360, 0x380, 0x3a0, 0x3c0, 0x3e0, 0
+};
+
+static short irqtab [] = { 3, 5, 7, 10, 11, 12, 15, 0 };
+static short dmatab [] = { 5, 6, 7, 0 };
+
+static int valid (short value, short *list)
+{
+ while (*list)
+ if (value == *list++)
+ return 1;
+ return 0;
+}
+
+long ct_baud = 256000; /* default baud rate */
+unsigned char ct_chan_mode = M_HDLC; /* default mode */
+
+static void ct_init_chan (ct_board_t *b, int num);
+static void ct_enable_loop (ct_chan_t *c);
+static void ct_disable_loop (ct_chan_t *c);
+
+int ct_download (port_t port, const unsigned char *firmware,
+ long bits, const cr_dat_tst_t *tst)
+{
+ unsigned char cr1, sr2;
+ long i, n, maxn = (bits + 7) >> 3;
+ int v, b;
+
+ inb (BSR3(port));
+ for (i=n=0; n<maxn; ++n) {
+ v = ((firmware[n] ^ ' ') << 1) | ((firmware[n] >> 7) & 1);
+ for (b=0; b<7; b+=2, i+=2) {
+ if (i >= bits)
+ break;
+ cr1 = 0;
+ if (v >> b & 1)
+ cr1 |= BCR1_TMS;
+ if (v >> b & 2)
+ cr1 |= BCR1_TDI;
+ outb (BCR1(port), cr1);
+ sr2 = inb (BSR2(port));
+ outb (BCR0(port), BCR0_TCK);
+ outb (BCR0(port), 0);
+ if (i >= tst->end)
+ ++tst;
+ if (i >= tst->start && (sr2 & BSR2_LERR))
+ return (0);
+ }
+ }
+ return (1);
+}
+
+/*
+ * Firmware unpack algorithm.
+ */
+typedef struct {
+ const unsigned char *ptr;
+ unsigned char byte;
+ unsigned char count;
+} unpack_t;
+
+static unsigned short unpack_init (unpack_t *t, const unsigned char *ptr)
+{
+ unsigned short len;
+
+ len = *ptr++;
+ len |= *ptr++ << 8;
+ t->ptr = ptr;
+ t->byte = 0;
+ t->count = 0;
+ return len;
+}
+
+static unsigned char unpack_getchar (unpack_t *t)
+{
+ if (t->count > 0) {
+ --t->count;
+ return t->byte;
+ }
+ t->byte = *t->ptr++;
+ if (t->byte == 0)
+ t->count = *t->ptr++;
+ return t->byte;
+}
+
+/*
+ * Firmware download signals.
+ */
+#define nstatus(b) (inb(BSR3(b)) & BSR3_NSTATUS)
+
+#define confdone(b) (inb(BSR3(b)) & BSR3_CONF_DN)
+
+#define nconfig_set(b) outb (bcr1_port, (bcr1 &= ~BCR1_NCONFIGI))
+#define nconfig_clr(b) outb (bcr1_port, (bcr1 |= BCR1_NCONFIGI))
+
+#define dclk_tick(b) outb (BCR3(b), 0)
+
+#define putbit(b,bit) { if (bit) bcr1 |= BCR1_1KDAT; \
+ else bcr1 &= ~BCR1_1KDAT; \
+ outb (bcr1_port, bcr1); \
+ dclk_tick (b); }
+
+#define CTAU_DEBUG(x) /*trace_str x*/
+
+int ct_download2 (port_t port, const unsigned char *fwaddr)
+{
+ unsigned short bytes;
+ unsigned char bcr1, val;
+ port_t bcr1_port;
+ unpack_t t;
+
+ /*
+ * Set NCONFIG and wait until NSTATUS is set.
+ */
+ bcr1_port = BCR1(port);
+ bcr1 = 0;
+ nconfig_set(port);
+ for (val=0; val<255; ++val)
+ if (nstatus(port))
+ break;
+
+ /*
+ * Clear NCONFIG, wait 2 usec and check that NSTATUS is cleared.
+ */
+ nconfig_clr(port);
+ for (val=0; val<2*3; ++val)
+ nconfig_clr(port);
+ if (nstatus(port)) {
+ CTAU_DEBUG (("Bad nstatus, downloading aborted (bsr3=0x%x).\n", inb(BSR3(port))));
+ nconfig_set(port);
+ return 0;
+ }
+
+ /*
+ * Set NCONFIG and wait 5 usec.
+ */
+ nconfig_set(port);
+ for (val=0; val<5*3; ++val) /* Delay 5 msec. */
+ nconfig_set(port);
+
+ /*
+ * С адреса `fwaddr' в памяти должны лежать упакованные данные
+ * для загрузки firmware. Значение должно быть согласовано с параметром
+ * вызова утилиты `megaprog' в скрипте загрузки (и Makefile).
+ */
+ bytes = unpack_init (&t, fwaddr);
+ for (; bytes>0; --bytes) {
+ val = unpack_getchar (&t);
+
+ if (nstatus(port) == 0) {
+ CTAU_DEBUG (("Bad nstatus, %d bytes remaining.\n", bytes));
+ goto failed;
+ }
+
+ if (confdone(port)) {
+ /* Ten extra clocks. Hope 50 is enough. */
+ for (val=0; val<50; ++val)
+ dclk_tick (port);
+
+ if (nstatus(port) == 0) {
+ CTAU_DEBUG (("Bad nstatus after confdone, %d bytes remaining (%d).\n",
+ bytes, t.ptr - fwaddr));
+ goto failed;
+ }
+
+ /* Succeeded. */
+ /*CTAU_DEBUG (("Download succeeded.\n"));*/
+ return 1;
+ }
+
+ putbit (port, val & 0x01);
+ putbit (port, val & 0x02);
+ putbit (port, val & 0x04);
+ putbit (port, val & 0x08);
+ putbit (port, val & 0x10);
+ putbit (port, val & 0x20);
+ putbit (port, val & 0x40);
+ putbit (port, val & 0x80);
+
+ /* if ((bytes & 1023) == 0) putch ('.'); */
+ }
+
+ CTAU_DEBUG (("Bad confdone.\n"));
+failed:
+ CTAU_DEBUG (("Downloading aborted.\n"));
+ return 0;
+}
+
+/*
+ * Detect Tau2 adapter.
+ */
+static int ct_probe2_board (port_t port)
+{
+ unsigned char sr3, osr3;
+ int i;
+
+ if (! valid (port, porttab))
+ return 0;
+
+ osr3 = inb (BSR3(port));
+ if ((osr3 & (BSR3_IB | BSR3_IB_NEG)) != BSR3_IB &&
+ (osr3 & (BSR3_IB | BSR3_IB_NEG)) != BSR3_IB_NEG)
+ return (0);
+ for (i=0; i<100; ++i) {
+ /* Do it twice */
+ sr3 = inb (BSR3(port));
+ sr3 = inb (BSR3(port));
+ if (((sr3 ^ osr3) & (BSR3_IB | BSR3_IB_NEG | BSR3_ZERO)) !=
+ (BSR3_IB | BSR3_IB_NEG))
+ return (0);
+ osr3 = sr3;
+ }
+ /* Reset the controller. */
+ outb (BCR0(port), 0);
+ return 1;
+}
+
+/*
+ * Check if the Tau board is present at the given base port.
+ * Read board status register 1 and check identification bits
+ * which should invert every next read.
+ * The "zero" bit should remain stable.
+ */
+int ct_probe_board (port_t port, int irq, int dma)
+{
+ unsigned char sr3, osr3;
+ int i;
+
+ if (! valid (port, porttab))
+ return 0;
+
+ if ((irq > 0) && (!valid (irq, irqtab)))
+ return 0;
+
+ if ((dma > 0) && (!valid (dma, dmatab)))
+ return 0;
+
+ osr3 = inb (BSR3(port));
+ if ((osr3 & (BSR3_IB | BSR3_IB_NEG)) != BSR3_IB &&
+ (osr3 & (BSR3_IB | BSR3_IB_NEG)) != BSR3_IB_NEG)
+ return (0);
+ for (i=0; i<100; ++i) {
+ sr3 = inb (BSR3(port));
+ if (((sr3 ^ osr3) & (BSR3_IB | BSR3_IB_NEG | BSR3_ZERO)) !=
+ (BSR3_IB | BSR3_IB_NEG))
+ return ct_probe2_board (port);
+ osr3 = sr3;
+ }
+ /* Reset the controller. */
+ outb (BCR0(port), 0);
+ return (1);
+}
+
+/*
+ * Check that the irq is functional.
+ * irq>0 - activate the interrupt from the adapter (irq=on)
+ * irq<0 - deactivate the interrupt (irq=off)
+ * irq==0 - free the interrupt line (irq=tri-state)
+ * Return the interrupt mask _before_ activating irq.
+ */
+int ct_probe_irq (ct_board_t *b, int irq)
+{
+ int mask;
+
+ outb (0x20, 0x0a);
+ mask = inb (0x20);
+ outb (0xa0, 0x0a);
+ mask |= inb (0xa0) << 8;
+
+ if (irq > 0) {
+ outb (BCR0(b->port), BCR0_HDRUN | irqmask[irq]);
+ outb (R(b->port,HD_TEPR_0R), 0);
+ outw (R(b->port,HD_TCONR_0R), 1);
+ outw (R(b->port,HD_TCNT_0R), 0);
+ outb (R(b->port,HD_TCSR_0R), TCSR_ENABLE | TCSR_INTR);
+ outb (IER2(b->port), IER2_RX_TME_0);
+ } else if (irq < 0) {
+ outb (BCR0(b->port), BCR0_HDRUN | irqmask[-irq]);
+ outb (IER0(b->port), 0);
+ outb (IER1(b->port), 0);
+ outb (IER2(b->port), 0);
+ outb (R(b->port,HD_TCSR_0R), 0);
+ cte_out (E1CS0 (b->port), DS_IMR2, 0);
+ cte_out (E1CS1 (b->port), DS_IMR2, 0);
+ if (-irq > 7) {
+ outb (0xa0, 0x60 | ((-irq) & 7));
+ outb (0x20, 0x62);
+ } else
+ outb (0x20, 0x60 | (-irq));
+ } else {
+ outb (BCR0(b->port), b->bcr0);
+ cte_out (E1CS0 (b->port), DS_IMR2, SR2_SEC);
+ cte_out (E1CS1 (b->port), DS_IMR2, SR2_SEC);
+ }
+
+ return mask;
+}
+
+void ct_init_board (ct_board_t *b, int num, port_t port, int irq, int dma,
+ int type, long osc)
+{
+ int i;
+
+ /* Initialize board structure. */
+ b->type = type;
+ b->port = port;
+ b->num = num;
+ b->irq = irq;
+ b->dma = dma;
+ b->osc = osc;
+
+ /* Get the board type. */
+ if (b->type == B_TAU) strcpy (b->name, "Tau");
+ else if (b->type == B_TAU_E1) strcpy (b->name, "Tau/E1");
+ else if (b->type == B_TAU_E1C) strcpy (b->name, "Tau/E1c");
+ else if (b->type == B_TAU_E1D) strcpy (b->name, "Tau/E1d");
+ else if (b->type == B_TAU_G703) strcpy (b->name, "Tau/G.703");
+ else if (b->type == B_TAU_G703C) strcpy (b->name, "Tau/G.703c");
+ else if (b->type == B_TAU2) strcpy (b->name, "Tau2");
+ else if (b->type == B_TAU2_E1) strcpy (b->name, "Tau2/E1");
+ else if (b->type == B_TAU2_E1D) strcpy (b->name, "Tau2/E1d");
+ else if (b->type == B_TAU2_G703) strcpy (b->name, "Tau2/G.703");
+ else strcpy (b->name, "Tau/???");
+
+ /* Set DMA and IRQ. */
+ b->bcr0 = BCR0_HDRUN | dmamask[b->dma] | irqmask[b->irq];
+
+ /* Clear DTR[0..1]. */
+ b->bcr1 = 0;
+ b->e1cfg = 0;
+
+ /* Initialize channel structures. */
+ for (i=0; i<NCHAN; ++i)
+ ct_init_chan (b, i);
+ ct_reinit_board (b);
+}
+
+/*
+ * Initialize the board structure.
+ */
+void ct_init (ct_board_t *b, int num, port_t port, int irq, int dma,
+ const unsigned char *firmware, long bits, const cr_dat_tst_t *tst,
+ const unsigned char *firmware2)
+{
+ static long tlen = 182;
+ static cr_dat_tst_t tvec [] = {{ 114, 178 }, { 182, 182 }};
+ static cr_dat_tst_t tvec2 [] = {{ 50, 178 }, { 182, 182 }};
+ static unsigned char tau [] = { 155,153,113,48,64,236,
+ 48,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,183,};
+ static unsigned char e1 [] = { 155,153,113,48,64,236,
+ 112,37,49,37,33,116,101,100,112,37,49,37,33,116,101,100,230,};
+ static unsigned char e1_2 [] = { 155,153,113,48,64,236,
+ 112,37,49,37,33,116,101,100,96,97,53,49,49,96,97,100,230,};
+ static unsigned char e1_3 [] = { 155,153,113,48,64,236,
+ 96,97,53,49,49,96,97,100,96,97,53,49,49,96,97,100,230,};
+ static unsigned char e1_4 [] = { 155,153,113,48,64,236,
+ 96,97,53,49,49,96,97,100,112,37,49,37,33,116,101,100,230,};
+ static unsigned char g703 [] = { 155,153,113,48,64,236,
+ 112,37,49,37,33,116,101,32,117,37,49,37,33,116,101,100,230,};
+ static unsigned char g703_2 [] = { 155,153,113,48,64,236,
+ 112,37,49,37,33,116,101,32,101,97,53,49,49,96,97,100,230,};
+ static unsigned char g703_3 [] = { 155,153,113,48,64,236,
+ 96,97,53,49,49,96,97,32,101,97,53,49,49,96,97,100,230,};
+ static unsigned char g703_4 [] = { 155,153,113,48,64,236,
+ 96,97,53,49,49,96,97,32,117,37,49,37,33,116,101,100,230,};
+
+ int type = B_TAU;
+ long osc = (inb (BSR3(port)) & BSR3_ZERO) ? 8192000 : 10000000;
+
+ /* Get the board type. */
+ if (ct_probe2_board (port) && ct_download2 (port, firmware2)) {
+ /* Tau2, 1k30-based model */
+ unsigned char sr0 = inb (BSR0(port));
+ if (! (sr0 & BSR0_T703))
+ type = B_TAU2_G703;
+ else if (sr0 & BSR0_TE1)
+ type = B_TAU2;
+ else if (inb(E1SR(port)) & E1SR_REV)
+ type = B_TAU2_E1D;
+ else
+ type = B_TAU2_E1;
+ } else if (ct_download (port, tau, tlen, tvec)) {
+ if (! ct_download (port, firmware, bits, tst))
+ type = B_TAU;
+ else {
+ unsigned char sr0 = inb (BSR0(port));
+ if (! (sr0 & BSR0_T703))
+ type = B_TAU_G703C;
+ else if (sr0 & BSR0_TE1)
+ type = B_TAU;
+ else if (inb(E1SR(port)) & E1SR_REV)
+ type = B_TAU_E1D;
+ else
+ type = B_TAU_E1C;
+ }
+ } else if (ct_download (port, e1, tlen, tvec2) ||
+ ct_download (port, e1_2, tlen, tvec2) ||
+ ct_download (port, e1_3, tlen, tvec2) ||
+ ct_download (port, e1_4, tlen, tvec2))
+ type = B_TAU_E1;
+ else if (ct_download (port, g703, tlen, tvec2) ||
+ ct_download (port, g703_2, tlen, tvec2) ||
+ ct_download (port, g703_3, tlen, tvec2) ||
+ ct_download (port, g703_4, tlen, tvec2))
+ type = B_TAU_G703;
+ ct_init_board (b, num, port, irq, dma, type, osc);
+}
+
+/*
+ * Initialize the channel structure.
+ */
+static void ct_init_chan (ct_board_t *b, int i)
+{
+ ct_chan_t *c = b->chan + i;
+ port_t port = b->port;
+
+ c->num = i;
+ c->board = b;
+ switch (b->type) {
+ case B_TAU:
+ case B_TAU2: c->type = T_SERIAL; break;
+ case B_TAU_E1:
+ case B_TAU_E1C:
+ case B_TAU_E1D:
+ case B_TAU2_E1:
+ case B_TAU2_E1D: c->type = T_E1; break;
+ case B_TAU_G703:
+ case B_TAU_G703C:
+ case B_TAU2_G703: c->type = T_G703; break;
+ }
+ if (c->num)
+ c->type |= T_SERIAL;
+
+#define reg(X,N) HD_##X##_##N
+#define set(X,N) c->X = R(port,reg(X,N))
+#define srx(X,N) c->RX.X = R(port,reg(X,N##R))
+#define stx(X,N) c->TX.X = R(port,reg(X,N##T))
+ if (i == 0) {
+ set(MD0, 0); set(MD1, 0); set(MD2, 0); set(CTL, 0);
+ set(RXS, 0); set(TXS, 0); set(TMC, 0); set(CMD, 0);
+ set(ST0, 0); set(ST1, 0); set(ST2, 0); set(ST3, 0);
+ set(FST, 0); set(IE0, 0); set(IE1, 0); set(IE2, 0);
+ set(FST, 0); set(IE0, 0); set(IE1, 0); set(IE2, 0);
+ set(FIE, 0); set(SA0, 0); set(SA1, 0); set(IDL, 0);
+ set(TRB, 0); set(RRC, 0); set(TRC0,0); set(TRC1,0);
+ set(CST, 0);
+ srx(DAR, 0); srx(DARB,0); srx(SAR, 0); srx(SARB,0);
+ srx(CDA, 0); srx(EDA, 0); srx(BFL, 0); srx(BCR, 0);
+ srx(DSR, 0); srx(DMR, 0); srx(FCT, 0); srx(DIR, 0);
+ srx(DCR, 0);
+ srx(TCNT,0); srx(TCONR,0); srx(TCSR,0); srx(TEPR,0);
+ stx(DAR, 0); stx(DARB,0); stx(SAR, 0); stx(SARB,0);
+ stx(CDA, 0); stx(EDA, 0); stx(BCR, 0);
+ stx(DSR, 0); stx(DMR, 0); stx(FCT, 0); stx(DIR, 0);
+ stx(DCR, 0);
+ stx(TCNT,0); stx(TCONR,0); stx(TCSR,0); stx(TEPR,0);
+ } else {
+ set(MD0, 1); set(MD1, 1); set(MD2, 1); set(CTL, 1);
+ set(RXS, 1); set(TXS, 1); set(TMC, 1); set(CMD, 1);
+ set(ST0, 1); set(ST1, 1); set(ST2, 1); set(ST3, 1);
+ set(FST, 1); set(IE0, 1); set(IE1, 1); set(IE2, 1);
+ set(FST, 1); set(IE0, 1); set(IE1, 1); set(IE2, 1);
+ set(FIE, 1); set(SA0, 1); set(SA1, 1); set(IDL, 1);
+ set(TRB, 1); set(RRC, 1); set(TRC0,1); set(TRC1,1);
+ set(CST, 1);
+ srx(DAR, 1); srx(DARB,1); srx(SAR, 1); srx(SARB,1);
+ srx(CDA, 1); srx(EDA, 1); srx(BFL, 1); srx(BCR, 1);
+ srx(DSR, 1); srx(DMR, 1); srx(FCT, 1); srx(DIR, 1);
+ srx(DCR, 1);
+ srx(TCNT,1); srx(TCONR,1); srx(TCSR,1); srx(TEPR,1);
+ stx(DAR, 1); stx(DARB,1); stx(SAR, 1); stx(SARB,1);
+ stx(CDA, 1); stx(EDA, 1); stx(BCR, 1);
+ stx(DSR, 1); stx(DMR, 1); stx(FCT, 1); stx(DIR, 1);
+ stx(DCR, 1);
+ stx(TCNT,1); stx(TCONR,1); stx(TCSR,1); stx(TEPR,1);
+ }
+#undef set
+#undef srx
+#undef stx
+#undef reg
+}
+
+/*
+ * Reinitialize the channels, using new options.
+ */
+void ct_reinit_chan (ct_chan_t *c)
+{
+ ct_board_t *b = c->board;
+ long s;
+ int i;
+
+ if (c->hopt.txs == CLK_LINE) {
+ /* External clock mode -- set zero baud rate. */
+ if (c->mode != M_ASYNC)
+ c->baud = 0;
+ } else if (c->baud == 0) {
+ /* No baud rate in internal clock mode -- set default values. */
+ if (c->mode == M_ASYNC)
+ c->baud = 9600;
+ else if (c->mode == M_HDLC)
+ c->baud = 64000;
+ }
+ switch (c->type) {
+ case T_E1_SERIAL:
+ if (b->opt.cfg == CFG_B)
+ break;
+ /* Fall through... */
+ case T_E1:
+ c->mode = M_E1;
+ c->hopt.txs = CLK_LINE;
+
+ /* Compute the baud value. */
+ if (c->num) {
+ s = b->opt.s1;
+ if (b->opt.cfg == CFG_C)
+ s &=~ b->opt.s0;
+ } else
+ s = b->opt.s0;
+ /* Skip timeslot 16 in CAS mode. */
+ if (c->gopt.cas)
+ s &=~ (1L << 16);
+
+ c->baud = 0;
+ for (i=0; i<32; ++i)
+ if ((s >> i) & 1)
+ c->baud += 64000;
+ c->gopt.rate = c->baud / 1000;
+
+ /* Set NRZ and clear INVCLK. */
+ c->opt.md2.encod = MD2_ENCOD_NRZ;
+ c->board->opt.bcr2 &= c->num ?
+ ~(BCR2_INVTXC1 | BCR2_INVRXC1) :
+ ~(BCR2_INVTXC0 | BCR2_INVRXC0);
+ break;
+
+ case T_G703_SERIAL:
+ if (b->opt.cfg == CFG_B)
+ break;
+ /* Fall through... */
+ case T_G703:
+ c->mode = M_G703;
+ c->hopt.txs = CLK_LINE;
+ c->baud = c->gopt.rate * 1000L;
+
+ /* Set NRZ/NRZI and clear INVCLK. */
+ if (c->opt.md2.encod != MD2_ENCOD_NRZ &&
+ c->opt.md2.encod != MD2_ENCOD_NRZI)
+ c->opt.md2.encod = MD2_ENCOD_NRZ;
+ c->board->opt.bcr2 &= c->num ?
+ ~(BCR2_INVTXC1 | BCR2_INVRXC1) :
+ ~(BCR2_INVTXC0 | BCR2_INVRXC0);
+ break;
+ }
+}
+
+/*
+ * Reinitialize all channels, using new options and baud rate.
+ */
+void ct_reinit_board (ct_board_t *b)
+{
+ ct_chan_t *c;
+
+ b->opt = ct_board_opt_dflt;
+ for (c=b->chan; c<b->chan+NCHAN; ++c) {
+ c->opt = ct_chan_opt_dflt;
+ c->hopt = ct_opt_hdlc_dflt;
+ c->gopt = ct_opt_g703_dflt;
+ c->mode = ct_chan_mode;
+ c->baud = ct_baud;
+
+ ct_reinit_chan (c);
+ }
+}
+
+/*
+ * Set up the E1 controller of the Tau/E1 board.
+ * Frame sync signals:
+ * Configuration Rsync0 Tsync0 Rsync1 Tsync1
+ * ---------------------------------------------------
+ * A (II) out out out out
+ * B,C,D (HI,K,D) in out in out
+ * ---------------------------------------------------
+ * BI out out in in -- not implemented
+ * old B,C,D (HI,K,D) out in out in -- old
+ */
+static void ct_setup_ctlr (ct_chan_t *c)
+{
+ ct_board_t *b = c->board;
+ port_t p = c->num ? E1CS1 (b->port) : E1CS0 (b->port);
+ unsigned char rcr1, rcr2, tcr1, tcr2, ccr1, licr;
+ unsigned long xcbr, tir;
+ int i;
+
+ rcr2 = RCR2_RSCLKM;
+ tcr1 = TCR1_TSIS | TCR1_TSO;
+ tcr2 = 0;
+ ccr1 = 0;
+ licr = 0;
+
+ if (b->opt.cfg != CFG_D) {
+ /* Enable monitoring channel, when not in telephony mode. */
+ rcr2 |= RCR2_SA_4;
+ tcr2 |= TCR2_SA_4;
+ }
+ if (b->opt.cfg == CFG_A) {
+ rcr1 = RCR1_RSO;
+ } else {
+ rcr1 = RCR1_RSI;
+ rcr2 |= RCR2_RESE;
+ }
+
+ if (c->gopt.cas)
+ tcr1 |= TCR1_T16S;
+ else
+ ccr1 |= CCR1_CCS;
+
+ if (c->gopt.hdb3)
+ ccr1 |= CCR1_THDB3 | CCR1_RHDB3;
+
+ if (c->gopt.crc4) {
+ ccr1 |= CCR1_TCRC4 | CCR1_RCRC4;
+ tcr2 |= TCR2_AEBE;
+ }
+
+ if (c->gopt.higain)
+ licr |= LICR_HIGAIN;
+ if (inb (E1SR (b->port)) & (c->num ? E1SR_TP1 : E1SR_TP0))
+ licr |= LICR_LB120P;
+ else
+ licr |= LICR_LB75P;
+
+ cte_out (p, DS_RCR1, rcr1); /* receive control 1 */
+ cte_out (p, DS_RCR2, rcr2); /* receive control 2 */
+ cte_out (p, DS_TCR1, tcr1); /* transmit control 1 */
+ cte_out (p, DS_TCR2, tcr2); /* transmit control 2 */
+ cte_out (p, DS_CCR1, ccr1); /* common control 1 */
+ cte_out (p, DS_CCR2, CCR2_CNTCV | CCR2_AUTORA | CCR2_LOFA1); /* common control 2 */
+ cte_out (p, DS_CCR3, CCR3_TSCLKM); /* common control 3 */
+ cte_out (p, DS_LICR, licr); /* line interface control */
+ cte_out (p, DS_IMR1, 0); /* interrupt mask 1 */
+ cte_out (p, DS_IMR2, SR2_SEC); /* interrupt mask 2 */
+ cte_out (p, DS_TEST1, 0);
+ cte_out (p, DS_TEST2, 0);
+ cte_out (p, DS_TAF, 0x9b); /* transmit align frame */
+ cte_out (p, DS_TNAF, 0xdf); /* transmit non-align frame */
+ cte_out (p, DS_TIDR, 0xff); /* transmit idle definition */
+
+ cte_out (p, DS_TS, 0x0b); /* transmit signaling 1 */
+ for (i=1; i<16; ++i)
+ /* transmit signaling 2..16 */
+ cte_out (p, (unsigned char) (DS_TS+i), 0xff);
+
+ /*
+ * S0 == list of timeslots for channel 0
+ * S1 == list of timeslots for channel 1
+ * S2 == list of timeslots for E1 subchannel (pass through)
+ *
+ * Each channel uses the same timeslots for receive and transmit,
+ * i.e. RCBRi == TCBRi.
+ */
+ if (b->opt.cfg == CFG_B)
+ b->opt.s1 = 0;
+ else if (b->opt.cfg == CFG_C)
+ b->opt.s1 &=~ b->opt.s0;
+ if (c->gopt.cas) {
+ /* Skip timeslot 16 in CAS mode. */
+ b->opt.s0 &=~ (1L << 16);
+ b->opt.s1 &=~ (1L << 16);
+ }
+ b->opt.s2 &=~ b->opt.s0;
+ b->opt.s2 &=~ b->opt.s1;
+
+ /*
+ * Configuration A:
+ * xCBRi := Si
+ * TIRi := ~Si
+ *
+ * Configuration B:
+ * xCBRi := Si
+ * TIRi := 0
+ *
+ * Configuration C: (S0 & S2 == 0)
+ * xCBR0 := S0
+ * xCBR1 := 0
+ * TIR0 := ~S0 & ~S2
+ * TIR1 := ~S2
+ *
+ * Configuration D: (Si & Sj == 0)
+ * xCBR0 := S0
+ * xCBR1 := S1
+ * TIR0 := ~S0 & ~S1 & ~S2
+ * TIR1 := ~S2
+ */
+ xcbr = c->num ? b->opt.s1 : b->opt.s0;
+ if (b->opt.cfg == CFG_A)
+ tir = ~xcbr;
+ else if (b->opt.cfg == CFG_D)
+ tir = 0;
+ else if (c->num == 0)
+ tir = ~(b->opt.s0 | b->opt.s1 | b->opt.s2);
+ else
+ tir = ~b->opt.s2;
+
+ /* Mark idle channels. */
+ cte_out (p, DS_TIR, (unsigned char) (tir & 0xfe));
+ cte_out (p, DS_TIR+1, (unsigned char) (tir >> 8));
+ cte_out (p, DS_TIR+2, (unsigned char) (tir >> 16));
+ cte_out (p, DS_TIR+3, (unsigned char) (tir >> 24));
+
+ /* Set up rx/tx timeslots. */
+ cte_out (p, DS_RCBR, (unsigned char) (xcbr & 0xfe));
+ cte_out (p, DS_RCBR+1, (unsigned char) (xcbr >> 8));
+ cte_out (p, DS_RCBR+2, (unsigned char) (xcbr >> 16));
+ cte_out (p, DS_RCBR+3, (unsigned char) (xcbr >> 24));
+ cte_out (p, DS_TCBR, (unsigned char) (xcbr & 0xfe));
+ cte_out (p, DS_TCBR+1, (unsigned char) (xcbr >> 8));
+ cte_out (p, DS_TCBR+2, (unsigned char) (xcbr >> 16));
+ cte_out (p, DS_TCBR+3, (unsigned char) (xcbr >> 24));
+
+ /* Reset the line interface. */
+ cte_out (p, DS_CCR3, CCR3_TSCLKM | CCR3_LIRESET);
+ cte_out (p, DS_CCR3, CCR3_TSCLKM);
+
+ /* Reset the elastic store. */
+ cte_out (p, DS_CCR3, CCR3_TSCLKM | CCR3_ESRESET);
+ cte_out (p, DS_CCR3, CCR3_TSCLKM);
+
+ /* Clear status registers. */
+ cte_ins (p, DS_SR1, 0xff);
+ cte_ins (p, DS_SR2, 0xff);
+ cte_ins (p, DS_RIR, 0xff);
+}
+
+/*
+ * Set up the serial controller of the Tau/E1 board.
+ */
+static void ct_setup_scc (port_t port)
+{
+#define SET(r,v) { cte_out2 (port, r, v); cte_out2 (port, AM_A | r, v); }
+
+ /* hardware reset */
+ cte_out2 (port, AM_MICR, MICR_RESET_HW);
+
+ SET (AM_PMR, 0x0c); /* 2 stop bits */
+ SET (AM_IMR, 0); /* no W/REQ signal */
+ cte_out2 (port, AM_IVR, 0); /* interrupt vector */
+ SET (AM_RCR, 0xc0); /* rx 8 bits/char */
+ SET (AM_TCR, 0x60); /* tx 8 bits/char */
+ SET (AM_SAF, 0); /* sync address field */
+ SET (AM_SFR, 0x7e); /* sync flag register */
+ cte_out2 (port, AM_MICR, 0); /* master interrupt control */
+ SET (AM_MCR, 0); /* NRZ mode */
+ SET (AM_CMR, 0x08); /* rxclk=RTxC, txclk=TRxC */
+ SET (AM_TCL, 0); /* time constant low */
+ SET (AM_TCH, 0); /* time constant high */
+ SET (AM_BCR, 0); /* disable baud rate generator */
+
+ SET (AM_RCR, 0xc1); /* enable rx */
+ SET (AM_TCR, 0x68); /* enable tx */
+
+ SET (AM_SICR, 0); /* no status interrupt */
+ SET (AM_CR, CR_RST_EXTINT); /* reset external status */
+ SET (AM_CR, CR_RST_EXTINT); /* reset ext/status twice */
+#undef SET
+}
+
+/*
+ * Set up the Tau/E1 board.
+ */
+void ct_setup_e1 (ct_board_t *b)
+{
+ /*
+ * Control register 0:
+ * 1) board configuration
+ * 2) clock modes
+ */
+ b->e1cfg &= E1CFG_LED;
+ switch (b->opt.cfg){
+ case CFG_C: b->e1cfg |= E1CFG_K; break;
+ case CFG_B: b->e1cfg |= E1CFG_HI; break;
+ case CFG_D: b->e1cfg |= E1CFG_D; break;
+ default: b->e1cfg |= E1CFG_II; break;
+ }
+
+ if (b->opt.clk0 == GCLK_RCV) b->e1cfg |= E1CFG_CLK0_RCV;
+ if (b->opt.clk0 == GCLK_RCLKO) b->e1cfg |= E1CFG_CLK0_RCLK1;
+ else b->e1cfg |= E1CFG_CLK0_INT;
+ if (b->opt.clk1 == GCLK_RCV) b->e1cfg |= E1CFG_CLK1_RCV;
+ if (b->opt.clk1 == GCLK_RCLKO) b->e1cfg |= E1CFG_CLK1_RCLK0;
+ else b->e1cfg |= E1CFG_CLK1_INT;
+
+ outb (E1CFG (b->port), b->e1cfg);
+
+ /*
+ * Set up serial controller.
+ */
+ ct_setup_scc (b->port);
+
+ /*
+ * Set up E1 controllers.
+ */
+ ct_setup_ctlr (b->chan + 0); /* channel 0 */
+ ct_setup_ctlr (b->chan + 1); /* channel 1 */
+
+ /* Start the board (GRUN). */
+ b->e1cfg |= E1CFG_GRUN;
+ outb (E1CFG (b->port), b->e1cfg);
+}
+
+/*
+ * Set up the G.703 controller of the Tau/G.703 board.
+ */
+static void ct_setup_lxt (ct_chan_t *c)
+{
+ ctg_outx (c, LX_CCR1, LX_RESET); /* reset the chip */
+ /* Delay */
+ ctg_inx (c, LX_CCR1);
+ c->lx = LX_LOS; /* disable loss of sync interrupt */
+ if (c->num && c->board->opt.cfg == CFG_B)
+ c->lx |= LX_TAOS; /* idle channel--transmit all ones */
+ if (c->gopt.hdb3)
+ c->lx |= LX_HDB3; /* enable HDB3 encoding */
+ ctg_outx (c, LX_CCR1, c->lx); /* setup the new mode */
+ ctg_outx (c, LX_CCR2, LX_CCR2_LH); /* setup Long Haul mode */
+ ctg_outx (c, LX_CCR3, LX_CCR3_E1_LH); /* setup Long Haul mode */
+}
+
+/*
+ * Set up the Tau/G.703 board.
+ */
+void ct_setup_g703 (ct_board_t *b)
+{
+ b->gmd0 = GMD_2048;
+ if (b->chan[0].gopt.pce) {
+ if (b->chan[0].gopt.pce2) b->gmd0 |= GMD_PCE_PCM2;
+ else b->gmd0 |= GMD_PCE_PCM2D;
+ }
+ if (b->opt.clk0)
+ b->gmd0 |= GMD_RSYNC;
+
+ b->gmd1 = 0;
+ if (b->chan[1].gopt.pce) {
+ if (b->chan[1].gopt.pce2) b->gmd1 |= GMD_PCE_PCM2;
+ else b->gmd1 |= GMD_PCE_PCM2D;
+ }
+ if (b->opt.clk1)
+ b->gmd1 |= GMD_RSYNC;
+
+ switch (b->chan[0].gopt.rate) {
+ case 2048: b->gmd0 |= GMD_2048; break;
+ case 1024: b->gmd0 |= GMD_1024; break;
+ case 512: b->gmd0 |= GMD_512; break;
+ case 256: b->gmd0 |= GMD_256; break;
+ case 128: b->gmd0 |= GMD_128; break;
+ case 64: b->gmd0 |= GMD_64; break;
+ }
+ switch (b->chan[1].gopt.rate) {
+ case 2048: b->gmd1 |= GMD_2048; break;
+ case 1024: b->gmd1 |= GMD_1024; break;
+ case 512: b->gmd1 |= GMD_512; break;
+ case 256: b->gmd1 |= GMD_256; break;
+
+ case 128: b->gmd1 |= GMD_128; break;
+ case 64: b->gmd1 |= GMD_64; break;
+ }
+
+ outb (GMD0(b->port), b->gmd0);
+ outb (GMD1(b->port), b->gmd1 | GMD1_NCS0 | GMD1_NCS1);
+
+ b->gmd2 &= GMD2_LED;
+ if (b->opt.cfg == CFG_B) b->gmd2 |= GMD2_SERIAL;
+ outb (GMD2(b->port), b->gmd2);
+
+ /* Set up G.703 controllers. */
+ if ((b->chan + 0)->lx & LX_LLOOP) {
+ ct_setup_lxt (b->chan + 0); /* channel 0 */
+ ct_enable_loop (b->chan + 0);
+ } else {
+ ct_setup_lxt (b->chan + 0); /* channel 0 */
+ }
+ if ((b->chan + 1)->lx & LX_LLOOP) {
+ ct_setup_lxt (b->chan + 1); /* channel 1 */
+ ct_enable_loop (b->chan + 1);
+ } else {
+ ct_setup_lxt (b->chan + 1); /* channel 1 */
+ }
+
+ /* Clear errors. */
+ outb (GERR(b->port), 0xff);
+ outb (GLDR(b->port), 0xff);
+}
+
+/*
+ * Set up the board.
+ */
+int ct_setup_board (ct_board_t *b, const unsigned char *firmware,
+ long bits, const cr_dat_tst_t *tst)
+{
+ ct_chan_t *c;
+
+ /* Disable DMA channel. */
+ outb (DMA_MASK, (b->dma & 3) | DMA_MASK_CLEAR);
+
+ /* Reset the controller. */
+ outb (BCR0(b->port), 0);
+
+ /* Load the firmware. */
+ if (firmware && (b->type == B_TAU || b->type == B_TAU_E1 ||
+ b->type == B_TAU_G703) &&
+ ! ct_download (b->port, firmware, bits, tst))
+ return (0);
+ if (firmware && (b->type == B_TAU2 || b->type == B_TAU2_E1 ||
+ b->type == B_TAU2_E1D || b->type == B_TAU2_G703) &&
+ ! ct_download2 (b->port, firmware))
+ return (0);
+
+ /* Enable DMA and IRQ. */
+ outb (BCR0(b->port), BCR0_HDRUN);
+ outb (BCR0(b->port), b->bcr0);
+
+ /* Clear DTR[0..1]. */
+ outb (BCR1(b->port), b->bcr1);
+
+ /* Set bus timing. */
+ b->bcr2 = b->opt.bcr2;
+ outb (BCR2(b->port), b->bcr2);
+
+ /*
+ * Initialize the controller.
+ */
+ /* Zero wait state mode. */
+ outb (WCRL(b->port), 0);
+ outb (WCRM(b->port), 0);
+ outb (WCRH(b->port), 0);
+
+ /* Clear interrupt modified vector register. */
+ outb (IMVR(b->port), 0);
+ outb (ITCR(b->port), ITCR_CYCLE_SINGLE | ITCR_VECT_MOD);
+
+ /* Disable all interrupts. */
+ outb (IER0(b->port), 0);
+ outb (IER1(b->port), 0);
+ outb (IER2(b->port), 0);
+
+ /* Set DMA parameters, enable master DMA mode. */
+ outb (PCR(b->port), BYTE b->opt.pcr);
+ outb (DMER(b->port), DME_ENABLE);
+
+ /* Set up DMA channel to master mode. */
+ outb (DMA_MODE, (b->dma & 3) | DMA_MODE_MASTER);
+
+ /* Enable DMA channel. */
+ outb (DMA_MASK, b->dma & 3);
+
+ /* Disable byte-sync mode for Tau/G.703. */
+ if (b->type == B_TAU_G703)
+ outb (GMD2(b->port), 0);
+
+ /* Initialize all channels. */
+ for (c=b->chan; c<b->chan+NCHAN; ++c)
+ ct_setup_chan (c);
+
+ switch (b->type) {
+ case B_TAU_G703:
+ case B_TAU_G703C:
+ case B_TAU2_G703:
+ ct_setup_g703 (b);
+ break;
+ case B_TAU_E1:
+ case B_TAU_E1C:
+ case B_TAU_E1D:
+ case B_TAU2_E1:
+ case B_TAU2_E1D:
+ ct_setup_e1 (b);
+ break;
+ }
+ return (1);
+}
+
+/*
+ * Update the channel mode options.
+ */
+void ct_update_chan (ct_chan_t *c)
+{
+ int txbr, rxbr, tmc, txcout;
+ unsigned char rxs, txs, dmr = 0;
+ ct_md0_async_t amd0;
+ ct_md0_hdlc_t hmd0;
+ ct_md1_async_t amd1;
+
+ switch (c->mode) { /* initialize the channel mode */
+ case M_ASYNC: default:
+ rxs = CLK_INT;
+ txs = CLK_INT;
+
+ amd0.mode = MD0_MODE_ASYNC;
+ amd0.stopb = MD0_STOPB_1;
+ amd0.cts_rts_dcd = 0;
+
+ amd1.clk = MD1_CLK_16;
+ amd1.txclen = amd1.rxclen = MD1_CLEN_8;
+ amd1.parmode = MD1_PAR_NO;
+
+ outb (c->MD0, BYTE amd0);
+ outb (c->MD1, BYTE amd1);
+ outb (c->CTL, c->rts ? 0 : CTL_RTS_INV);
+ break;
+
+ case M_E1:
+ case M_G703:
+ case M_HDLC:
+ rxs = c->hopt.rxs;
+ txs = c->hopt.txs;
+
+ if (c->mode == M_E1 && c->board->opt.cfg == CFG_D) {
+ hmd0 = c->hopt.md0;
+ hmd0.crc = 0;
+
+ outb (c->MD0, BYTE hmd0);
+ outb (c->MD1, BYTE c->hopt.md1);
+ outb (c->CTL, c->hopt.ctl & ~CTL_IDLE_PTRN);
+ outb (c->SA0, c->hopt.sa0);
+ outb (c->SA1, c->hopt.sa1);
+ outb (c->IDL, 0x7e); /* HDLC flag 01111110 */
+ } else {
+ outb (c->MD0, BYTE c->hopt.md0);
+ outb (c->MD1, BYTE c->hopt.md1);
+ outb (c->SA0, c->hopt.sa0);
+ outb (c->SA1, c->hopt.sa1);
+ outb (c->IDL, 0x7e); /* HDLC flag 01111110 */
+
+ if (c->rts)
+ outb (c->CTL, c->hopt.ctl & ~CTL_RTS_INV);
+ else
+ outb (c->CTL, c->hopt.ctl | CTL_RTS_INV);
+ }
+
+ /* Chained-block DMA mode with frame counter. */
+ dmr |= DMR_CHAIN_CNTE | DMR_CHAIN_NF | DMR_TMOD;
+ break;
+
+ }
+ outb (c->RX.DMR, dmr);
+ outb (c->TX.DMR, dmr);
+
+ /* set mode-independent options */
+ c->opt.md2.dpll_clk = MD2_DPLL_CLK_8;
+ outb (c->MD2, BYTE c->opt.md2);
+
+ /* set up receiver and transmitter clocks */
+ if (c->baud > 1024000) {
+ /* turn off DPLL if the baud rate is too high */
+ if (rxs == CLK_RXS_LINE_NS) rxs = CLK_LINE;
+ else if (rxs == CLK_RXS_DPLL_INT) rxs = CLK_INT;
+ }
+ if (rxs == CLK_RXS_LINE_NS || rxs == CLK_RXS_DPLL_INT) {
+ /* Using 1:8 sampling rate. */
+ ct_compute_clock (c->board->osc, c->baud * 8, &rxbr, &tmc);
+ txbr = rxbr + 3;
+ } else if (c->mode == M_ASYNC) {
+ /* Using 1:16 sampling rate. */
+ ct_compute_clock (c->board->osc, c->baud * 8, &rxbr, &tmc);
+ --rxbr;
+ txbr = rxbr;
+ } else {
+ ct_compute_clock (c->board->osc, c->baud, &rxbr, &tmc);
+ txbr = rxbr;
+ }
+ txs |= txbr;
+ rxs |= rxbr;
+ outb (c->TMC, tmc);
+ outb (c->RXS, rxs);
+
+ /* Disable TXCOUT before changing TXS
+ * to avoid two transmitters on the same line.
+ * Enable it after TXS is set, if needed. */
+ txcout = c->num ? BCR1_TXCOUT1 : BCR1_TXCOUT0;
+ c->board->bcr1 &= ~txcout;
+ outb (BCR1(c->board->port), c->board->bcr1);
+ outb (c->TXS, txs);
+ if ((txs & CLK_MASK) != CLK_LINE) {
+ c->board->bcr1 |= txcout;
+ outb (BCR1(c->board->port), c->board->bcr1);
+ }
+ if (c->board->type == B_TAU_E1D ||
+ c->board->type == B_TAU2_E1D)
+ ct_set_phony (c, c->gopt.phony);
+}
+
+/*
+ * Initialize the channel.
+ */
+void ct_setup_chan (ct_chan_t *c)
+{
+ /* reset the channel */
+ outb (c->RX.DSR, DSR_DMA_DISABLE);
+ outb (c->TX.DSR, DSR_DMA_DISABLE);
+ outb (c->CMD, CMD_TX_RESET);
+ outb (c->CMD, CMD_TX_ABORT);
+ outb (c->CMD, CMD_CHAN_RESET);
+
+ /* disable interrupts */
+ outb (c->IE0, 0);
+ outb (c->IE1, 0);
+ outb (c->IE2, 0);
+ outb (c->FIE, 0);
+
+ /* clear DTR, RTS */
+ ct_set_dtr (c, 0);
+ ct_set_rts (c, 0);
+
+ c->lx = LX_LOS;
+ ct_update_chan (c);
+}
+
+unsigned long ct_get_ts (ct_chan_t *c)
+{
+ return c->num ? c->board->opt.s1 : c->board->opt.s0;
+}
+
+/*
+ * Data transfer speed
+ */
+unsigned long ct_get_baud (ct_chan_t *c)
+{
+ unsigned long speed;
+ unsigned long ts;
+
+ if (c->mode == M_G703) {
+ speed = 1000 * c->gopt.rate;
+ } else if (c->mode == M_E1) {
+ ts = ct_get_ts (c);
+ for (speed=0; ts; ts >>= 1) /* Each timeslot is 64 Kbps */
+ if (ts & 1)
+ speed += 64000;
+ } else
+ speed = (c->hopt.txs == CLK_INT) ? c->baud : 0;
+ return speed;
+}
+
+/*
+ * Turn local loopback on
+ */
+static void ct_enable_loop (ct_chan_t *c)
+{
+ if (c->mode == M_E1) {
+ unsigned short p = c->num ? E1CS1 (c->board->port) :
+ E1CS0 (c->board->port);
+
+ /* Local loopback. */
+ cte_out (p, DS_CCR2, cte_in (p, DS_CCR2) | CCR2_LLOOP);
+
+ /* Enable jitter attenuator at the transmit side. */
+ cte_out (p, DS_LICR, cte_in (p, DS_LICR) | LICR_JA_TX);
+ return;
+ } else if (c->mode == M_G703) {
+ c->lx = LX_LOS | LX_HDB3;
+ ctg_outx (c, LX_CCR1, c->lx |= LX_LLOOP);
+ return;
+ } else if (c->mode == M_HDLC && ct_get_baud(c)) {
+ unsigned char rxs = inb (c->RXS) & ~CLK_MASK;
+ unsigned char txs = inb (c->TXS) & ~CLK_MASK;
+ int txcout = c->num ? BCR1_TXCOUT1 : BCR1_TXCOUT0;
+ c->opt.md2.loop = MD2_LLOOP;
+
+ /* Disable TXCOUT before changing TXS */
+ /* to avoid two transmitters on the same line. */
+ /* Enable it after TXS is set. */
+ outb (BCR1(c->board->port), c->board->bcr1 & ~txcout);
+
+ outb (c->RXS, rxs | CLK_INT);
+ outb (c->TXS, txs | CLK_INT);
+
+ c->board->bcr1 |= txcout;
+ outb (BCR1(c->board->port), c->board->bcr1);
+
+ outb (c->MD2, *(unsigned char*)&c->opt.md2);
+ return;
+ }
+}
+
+/*
+ * Turn local loopback off
+ */
+static void ct_disable_loop (ct_chan_t *c)
+{
+ if (c->mode == M_E1) {
+ unsigned short p = c->num ? E1CS1 (c->board->port) :
+ E1CS0 (c->board->port);
+
+ /* Local loopback. */
+ cte_out (p, DS_CCR2, cte_in (p, DS_CCR2) & ~CCR2_LLOOP);
+
+ /* Disable jitter attenuator at the transmit side. */
+ cte_out (p, DS_LICR, cte_in (p, DS_LICR) & ~LICR_JA_TX);
+ return;
+ } else if (c->mode == M_G703) {
+ c->lx = LX_LOS | LX_HDB3;
+ ctg_outx (c, LX_CCR1, c->lx);
+ return;
+ } else if (c->mode == M_HDLC && ct_get_baud(c)) {
+ unsigned char rxs = inb (c->RXS) & ~CLK_MASK;
+ unsigned char txs = inb (c->TXS) & ~CLK_MASK;
+ int txcout = c->num ? BCR1_TXCOUT1 : BCR1_TXCOUT0;
+ c->opt.md2.loop = MD2_FDX;
+
+ outb (BCR1(c->board->port), c->board->bcr1 & ~txcout);
+
+ outb (c->RXS, rxs | CLK_LINE);
+ if (ct_get_baud (c))
+ outb (c->TXS, txs | CLK_INT);
+ else
+ outb (c->TXS, txs | CLK_LINE);
+
+ c->board->bcr1 |= txcout;
+ outb (BCR1(c->board->port), c->board->bcr1);
+
+ outb (c->MD2, *(unsigned char*)&c->opt.md2);
+ return;
+ }
+}
+
+/*
+ * Turn local loopback on/off
+ */
+void ct_set_loop (ct_chan_t *c, int on)
+{
+ if (on)
+ ct_enable_loop (c);
+ else
+ ct_disable_loop (c);
+}
+
+int ct_get_loop (ct_chan_t *c)
+{
+ if (c->mode == M_E1) {
+ unsigned short p = c->num ? E1CS1 (c->board->port) :
+ E1CS0 (c->board->port);
+
+ return cte_in (p, DS_CCR2) & CCR2_LLOOP;
+ }
+ if (c->mode == M_G703)
+ return c->lx & LX_LLOOP;
+
+ /* M_HDLC */
+ return (c->opt.md2.loop & MD2_LLOOP) != 0;
+}
+
+void ct_set_phony (ct_chan_t *c, int on)
+{
+ /* Valid only for TauPCI-E1. */
+ if (c->board->type != B_TAU_E1D &&
+ c->board->type != B_TAU2_E1D)
+ return;
+ c->gopt.phony = (on != 0);
+ if (c->gopt.phony) {
+ c->board->e1syn |= c->num ? E1SYN_ENS1 : E1SYN_ENS0;
+ /* No receive/transmit crc. */
+ c->hopt.md0.crc = 0;
+ } else {
+ c->board->e1syn &= ~(c->num ? E1SYN_ENS1 : E1SYN_ENS0);
+ /* Enable crc. */
+ c->hopt.md0.crc = 1;
+ }
+ outb (c->MD0, BYTE c->hopt.md0);
+ outb (E1SYN(c->board->port), c->board->e1syn);
+}
+
+void ct_start_receiver (ct_chan_t *c, int dma, unsigned long buf,
+ unsigned len, unsigned long desc, unsigned long lim)
+{
+ int ier0 = inb (IER0(c->board->port));
+ int ier1 = inb (IER1(c->board->port));
+ int ier2 = inb (IER2(c->board->port));
+ int ie0 = inb (c->IE0);
+ int ie2 = inb (c->IE2);
+
+ if (dma) {
+ ier1 |= c->num ? (IER1_RX_DMERE_1 | IER1_RX_DME_1) :
+ (IER1_RX_DMERE_0 | IER1_RX_DME_0);
+ if (c->mode == M_ASYNC) {
+ ier0 |= c->num ? IER0_RX_INTE_1 : IER0_RX_INTE_0;
+ ie0 |= IE0_RX_INTE;
+ ie2 |= IE2_OVRNE | IE2_ASYNC_FRMEE | IE2_ASYNC_PEE;
+ }
+ } else {
+ ier0 |= c->num ? (IER0_RX_INTE_1 | IER0_RX_RDYE_1) :
+ (IER0_RX_INTE_0 | IER0_RX_RDYE_0);
+ ie0 |= IE0_RX_INTE | IE0_RX_RDYE;
+ }
+
+ /* Start timer. */
+ if (! dma) {
+ outb (c->RX.TEPR, TEPR_64); /* prescale to 16 kHz */
+ outw (c->RX.TCONR, 160); /* period is 10 msec */
+ outw (c->RX.TCNT, 0);
+ outb (c->RX.TCSR, TCSR_ENABLE | TCSR_INTR);
+ ier2 |= c->num ? IER2_RX_TME_1 : IER2_RX_TME_0;
+ }
+
+ /* Enable interrupts. */
+ outb (IER0(c->board->port), ier0);
+ outb (IER1(c->board->port), ier1);
+ outb (IER2(c->board->port), ier2);
+ outb (c->IE0, ie0);
+ outb (c->IE2, ie2);
+
+ /* RXRDY:=1 when the receive buffer has more than RRC chars */
+ outb (c->RRC, dma ? c->opt.dma_rrc : c->opt.pio_rrc);
+
+ /* Start receiver. */
+ if (dma) {
+ outb (c->RX.DCR, DCR_ABORT);
+ if (c->mode == M_ASYNC) {
+ /* Single-buffer DMA mode. */
+ outb (c->RX.DARB, (unsigned char) (buf >> 16));
+ outw (c->RX.DAR, (unsigned short) buf);
+ outw (c->RX.BCR, len);
+ outb (c->RX.DIR, DIR_EOTE);
+ } else {
+ /* Chained-buffer DMA mode. */
+ outb (c->RX.SARB, (unsigned char) (desc >> 16));
+ outw (c->RX.EDA, (unsigned short) lim);
+ outw (c->RX.CDA, (unsigned short) desc);
+ outw (c->RX.BFL, len);
+ outb (c->RX.DIR, DIR_CHAIN_EOME | DIR_CHAIN_BOFE |
+ DIR_CHAIN_COFE);
+ }
+ outb (c->RX.DSR, DSR_DMA_ENABLE);
+ }
+ outb (c->CMD, CMD_RX_ENABLE);
+}
+
+void ct_start_transmitter (ct_chan_t *c, int dma, unsigned long buf,
+ unsigned len, unsigned long desc, unsigned long lim)
+{
+ int ier0 = inb (IER0(c->board->port));
+ int ier1 = inb (IER1(c->board->port));
+ int ie0 = inb (c->IE0);
+ int ie1 = inb (c->IE1);
+
+ /* Enable underrun interrupt in HDLC and raw modes. */
+ if (c->mode != M_ASYNC) {
+ ier0 |= c->num ? IER0_TX_INTE_1 : IER0_TX_INTE_0;
+ ie0 |= IE0_TX_INTE;
+ ie1 |= IE1_HDLC_UDRNE;
+ }
+ if (dma)
+ ier1 |= c->num ? (IER1_TX_DMERE_1 | IER1_TX_DME_1) :
+ (IER1_TX_DMERE_0 | IER1_TX_DME_0);
+ else {
+ ier0 |= c->num ? IER0_TX_RDYE_1 : IER0_TX_RDYE_0;
+ ie0 |= IE0_TX_RDYE;
+ }
+
+ /* Enable interrupts. */
+ outb (IER0(c->board->port), ier0);
+ outb (IER1(c->board->port), ier1);
+ outb (c->IE0, ie0);
+ outb (c->IE1, ie1);
+
+ /* TXRDY:=1 when the transmit buffer has TRC0 or less chars,
+ * TXRDY:=0 when the transmit buffer has more than TRC1 chars */
+ outb (c->TRC0, dma ? c->opt.dma_trc0 : c->opt.pio_trc0);
+ outb (c->TRC1, dma ? c->opt.dma_trc1 : c->opt.pio_trc1);
+
+ /* Start transmitter. */
+ if (dma) {
+ outb (c->TX.DCR, DCR_ABORT);
+ if (c->mode == M_ASYNC) {
+ /* Single-buffer DMA mode. */
+ outb (c->TX.SARB, (unsigned char) (buf >> 16));
+ outw (c->TX.SAR, (unsigned short) buf);
+ outw (c->TX.BCR, len);
+ outb (c->TX.DIR, DIR_EOTE);
+ } else {
+ /* Chained-buffer DMA mode. */
+ outb (c->TX.SARB, (unsigned char) (desc >> 16));
+ outw (c->TX.EDA, (unsigned short) lim);
+ outw (c->TX.CDA, (unsigned short) desc);
+ outb (c->TX.DIR, /* DIR_CHAIN_EOME | */ DIR_CHAIN_BOFE |
+ DIR_CHAIN_COFE);
+ }
+ /* Set DSR_DMA_ENABLE to begin! */
+ }
+ outb (c->CMD, CMD_TX_ENABLE);
+
+ /* Clear errors. */
+ if (c->board->type == B_TAU_G703) {
+ outb (GERR(c->board->port), 0xff);
+ outb (GLDR(c->board->port), 0xff);
+ }
+}
+
+/*
+ * Control DTR signal for the channel.
+ * Turn it on/off.
+ */
+void ct_set_dtr (ct_chan_t *c, int on)
+{
+ if (on) {
+ c->dtr = 1;
+ c->board->bcr1 |= c->num ? BCR1_DTR1 : BCR1_DTR0;
+ } else {
+ c->dtr = 0;
+ c->board->bcr1 &= ~(c->num ? BCR1_DTR1 : BCR1_DTR0);
+ }
+ outb (BCR1(c->board->port), c->board->bcr1);
+}
+
+/*
+ * Control RTS signal for the channel.
+ * Turn it on/off.
+ */
+void ct_set_rts (ct_chan_t *c, int on)
+{
+ c->rts = (on != 0);
+ if (c->rts)
+ outb (c->CTL, inb (c->CTL) & ~CTL_RTS_INV);
+ else
+ outb (c->CTL, inb (c->CTL) | CTL_RTS_INV);
+}
+
+/*
+ * Control BREAK state in asynchronous mode.
+ * Turn it on/off.
+ */
+void ct_set_brk (ct_chan_t *c, int on)
+{
+ if (c->mode != M_ASYNC)
+ return;
+ if (on)
+ outb (c->CTL, inb (c->CTL) | CTL_BRK);
+ else
+ outb (c->CTL, inb (c->CTL) & ~CTL_BRK);
+}
+
+/*
+ * Get the state of DSR signal of the channel.
+ */
+int ct_get_dsr (ct_chan_t *c)
+{
+ return (inb (BSR1(c->board->port)) &
+ (c->num ? BSR1_DSR1 : BSR1_DSR0)) != 0;
+}
+
+/*
+ * Get the G.703 line signal level.
+ */
+int ct_get_lq (ct_chan_t *c)
+{
+ unsigned char q1, q2, q3;
+ static int lq_to_santibells [] = { 0, 95, 195, 285 };
+ int i;
+
+ if (! (c->type & T_G703))
+ return 0;
+ q1 = inb (GLQ (c->board->port));
+ /* Repeat reading the register to produce a 10-usec delay. */
+ for (i=0; i<20; ++i)
+ q2 = inb (GLQ (c->board->port));
+ for (i=0; i<20; ++i)
+ q3 = inb (GLQ (c->board->port));
+ if (c->num) {
+ q1 >>= GLQ_SHIFT;
+ q2 >>= GLQ_SHIFT;
+ q3 >>= GLQ_SHIFT;
+ }
+ q1 &= GLQ_MASK;
+ q2 &= GLQ_MASK;
+ q3 &= GLQ_MASK;
+ if (q1 <= q2 && q2 <= q3) return lq_to_santibells [q2];
+ if (q2 <= q3 && q3 <= q1) return lq_to_santibells [q3];
+ if (q3 <= q1 && q1 <= q2) return lq_to_santibells [q1];
+ if (q1 <= q3 && q3 <= q2) return lq_to_santibells [q3];
+ if (q3 <= q2 && q2 <= q1) return lq_to_santibells [q2];
+ /* if (q2 <= q1 && q1 <= q3) */ return lq_to_santibells [q1];
+}
+
+/*
+ * Get the state of CARRIER signal of the channel.
+ */
+int ct_get_cd (ct_chan_t *c)
+{
+ return (inb (c->ST3) & ST3_DCD_INV) == 0;
+}
+
+/*
+ * Get the state of CTS signal of the channel.
+ */
+int ct_get_cts (ct_chan_t *c)
+{
+ return (inb (c->ST3) & ST3_CTS_INV) == 0;
+}
+
+/*
+ * Turn LED on/off.
+ */
+void ct_led (ct_board_t *b, int on)
+{
+ switch (b->type) {
+ case B_TAU_G703:
+ case B_TAU_G703C:
+ case B_TAU2_G703:
+ if (on) b->gmd2 |= GMD2_LED;
+ else b->gmd2 &= ~GMD2_LED;
+ outb (GMD2(b->port), b->gmd2);
+ break;
+ default:
+ if (on) b->e1cfg |= E1CFG_LED;
+ else b->e1cfg &= ~E1CFG_LED;
+ outb (E1CFG(b->port), b->e1cfg);
+ break;
+ }
+}
+
+void ct_disable_dma (ct_board_t *b)
+{
+ /* Disable DMA channel. */
+ outb (DMA_MASK, (b->dma & 3) | DMA_MASK_CLEAR);
+}
+
+void ct_compute_clock (long hz, long baud, int *txbr, int *tmc)
+{
+ if (baud < 100)
+ baud = 100;
+ *txbr = 0;
+ if (4*baud > 3*hz)
+ *tmc = 1;
+ else {
+ while (((hz / baud) >> ++*txbr) > 256)
+ continue;
+ *tmc = (((2 * hz / baud) >> *txbr) + 1) / 2;
+ }
+}
+
+/*
+ * Access to DS2153 chips on the Tau/E1 board.
+ * Examples:
+ * val = cte_in (E1CSi (base), DS_RCR1)
+ * cte_out (E1CSi (base), DS_CCR1, val)
+ * val = cte_ins (E1CSi (base), DS_SSR)
+ */
+unsigned char cte_in (port_t base, unsigned char reg)
+{
+ outb (base, reg);
+ return inb (E1DAT (base & 0x3e0));
+}
+
+void cte_out (port_t base, unsigned char reg, unsigned char val)
+{
+ outb (base, reg);
+ outb (E1DAT (base & 0x3e0), val);
+}
+
+/*
+ * Get the DS2153 status register, using write-read-write scheme.
+ */
+unsigned char cte_ins (port_t base, unsigned char reg,
+ unsigned char mask)
+{
+ unsigned char val;
+ port_t rw = E1DAT (base & 0x3e0);
+
+ outb (base, reg); outb (rw, mask); /* lock bits */
+ outb (base, reg); val = inb (rw) & mask; /* get values */
+ outb (base, reg); outb (rw, val); /* unlock bits */
+ return val;
+}
+
+/*
+ * Access to 8530 chip on the Tau/E1 board.
+ * Examples:
+ * val = cte_in2 (base, AM_RSR | AM_A)
+ * cte_out2 (base, AM_IMR, val)
+ */
+unsigned char cte_in2 (port_t base, unsigned char reg)
+{
+ outb (E1CS2(base), E1CS2_SCC | reg >> 4);
+ outb (E1DAT(base), reg & 15);
+ return inb (E1DAT(base));
+}
+
+void cte_out2 (port_t base, unsigned char reg, unsigned char val)
+{
+ outb (E1CS2(base), E1CS2_SCC | reg >> 4);
+ outb (E1DAT(base), reg & 15);
+ outb (E1DAT(base), val);
+}
+
+/*
+ * Read the data from the 8530 receive fifo.
+ */
+unsigned char cte_in2d (ct_chan_t *c)
+{
+ outb (E1CS2(c->board->port), E1CS2_SCC | E1CS2_DC |
+ (c->num ? 0 : E1CS2_AB));
+ return inb (E1DAT(c->board->port));
+}
+
+/*
+ * Send the 8530 command.
+ */
+void cte_out2c (ct_chan_t *c, unsigned char val)
+{
+ outb (E1CS2(c->board->port), E1CS2_SCC | (c->num ? 0 : E1CS2_AB));
+ outb (E1DAT(c->board->port), val);
+}
+
+/*
+ * Write the data to the 8530 transmit fifo.
+ */
+void cte_out2d (ct_chan_t *c, unsigned char val)
+{
+ outb (E1CS2(c->board->port), E1CS2_SCC | E1CS2_DC |
+ (c->num ? 0 : E1CS2_AB));
+ outb (E1DAT(c->board->port), val);
+}
+
+/*
+ * Access to LXT318 chip on the Tau/G.703 board.
+ * Examples:
+ * val = ctg_inx (c)
+ * ctg_outx (c, val)
+ */
+static void ctg_output (port_t port, unsigned char val, unsigned char v0)
+{
+ int i;
+
+ for (i=0; i<8; ++i) {
+ unsigned char v = v0;
+ if ((val >> i) & 1)
+ v |= GMD0_SDI;
+ outb (port, v);
+ outb (port, v);
+ outb (port, v);
+ outb (port, v);
+ outb (port, v | GMD0_SCLK);
+ outb (port, v | GMD0_SCLK);
+ outb (port, v | GMD0_SCLK);
+ outb (port, v | GMD0_SCLK);
+ }
+ outb (port, v0);
+}
+
+void ctg_outx (ct_chan_t *c, unsigned char reg, unsigned char val)
+{
+ port_t port = GMD0(c->board->port);
+
+ outb (GMD1(c->board->port), c->board->gmd1 | GMD1_NCS0 | GMD1_NCS1);
+ outb (GMD1(c->board->port), c->board->gmd1 |
+ (c->num ? GMD1_NCS0 : GMD1_NCS1));
+ ctg_output (port, (reg << 1) | LX_WRITE, c->board->gmd0);
+ ctg_output (port, val, c->board->gmd0);
+ outb (GMD1(c->board->port), c->board->gmd1 | GMD1_NCS0 | GMD1_NCS1);
+}
+
+unsigned char ctg_inx (ct_chan_t *c, unsigned char reg)
+{
+ port_t port = GMD0(c->board->port);
+ port_t data = GLDR(c->board->port);
+ unsigned char val = 0, mask = c->num ? GLDR_C1 : GLDR_C0;
+ int i;
+
+ outb (GMD1(c->board->port), c->board->gmd1 | GMD1_NCS0 | GMD1_NCS1);
+ outb (GMD1(c->board->port), c->board->gmd1 |
+ (c->num ? GMD1_NCS0 : GMD1_NCS1));
+ ctg_output (port, (reg << 1) | LX_READ, c->board->gmd0);
+ for (i=0; i<8; ++i) {
+ outb (port, c->board->gmd0 | GMD0_SCLK);
+ if (inb (data) & mask)
+ val |= 1 << i;
+ outb (port, c->board->gmd0);
+ }
+ outb (GMD1(c->board->port), c->board->gmd1 | GMD1_NCS0 | GMD1_NCS1);
+ return val;
+}
+
+/*
+ * Adapter options
+ */
+ct_board_opt_t ct_board_opt_dflt = {
+ 0, /* board control register 2 */
+ { /* DMA priority control register */
+ PCR_PRIO_ROTATE,
+ 0, /* all channels share the the bus hold */
+ 0, /* hold the bus until all transfers done */
+ },
+ CFG_A, /* E1/G.703 config: two independent channels */
+ GCLK_INT, /* E1/G.703 chan 0 internal tx clock source */
+ GCLK_INT, /* E1/G.703 chan 1 internal tx clock source */
+ ~0UL << 1, /* E1 channel 0 timeslots 1..31 */
+ ~0UL << 1, /* E1 channel 1 timeslots 1..31 */
+ 0, /* no E1 subchannel timeslots */
+};
+
+/*
+ * Mode-independent channel options
+ */
+ct_chan_opt_t ct_chan_opt_dflt = {
+ { /* mode register 2 */
+ MD2_FDX, /* full duplex communication */
+ 0, /* empty ADPLL clock rate */
+ MD2_ENCOD_NRZ, /* NRZ encoding */
+ },
+ /* DMA mode FIFO marks */
+ 15, 24, 30, /* rx ready, tx empty, tx full */
+ /* port i/o mode FIFO marks */
+ 15, 16, 30, /* rx ready, tx empty, tx full */
+};
+
+/*
+ * Default HDLC options
+ */
+ct_opt_hdlc_t ct_opt_hdlc_dflt = {
+ { /* mode register 0 */
+ 1, /* CRC preset to all ones (V.41) */
+ 1, /* CRC-CCITT */
+ 1, /* enable CRC */
+ 0, /* disable automatic CTS/DCD */
+ MD0_MODE_HDLC, /* HDLC mode */
+ },
+ { /* mode register 1 */
+ MD1_ADDR_NOCHK, /* do not check address field */
+ },
+ CTL_IDLE_PTRN | CTL_UDRN_ABORT | CTL_RTS_INV, /* control register */
+ 0, 0, /* empty sync/address registers 0,1 */
+ CLK_LINE, /* receive clock source: RXC line input */
+ CLK_LINE, /* transmit clock source: TXC line input */
+};
+
+/*
+ * Default E1/G.703 options
+ */
+ct_opt_g703_t ct_opt_g703_dflt = {
+ 1, /* HDB3 enable */
+ 0, /* precoder disable */
+ GTEST_DIS, /* test disabled, normal operation */
+ 0, /* CRC4 disable */
+ 0, /* CCS signaling */
+ 0, /* low gain */
+ 0, /* no raw mode */
+ 0, /* no PCM2 precoder compatibility */
+ 2048, /* data rate 2048 kbit/sec */
+};
diff --git a/sys/dev/ctau/if_ct.c b/sys/dev/ctau/if_ct.c
new file mode 100644
index 000000000000..bd66f3d6b10c
--- /dev/null
+++ b/sys/dev/ctau/if_ct.c
@@ -0,0 +1,2752 @@
+/*
+ * Cronyx-Tau adapter driver for FreeBSD.
+ * Supports PPP/HDLC and Cisco/HDLC protocol in synchronous mode,
+ * and asyncronous channels with full modem control.
+ * Keepalive protocol implemented in both Cisco and PPP modes.
+ *
+ * Copyright (C) 1994-2002 Cronyx Engineering.
+ * Author: Serge Vakulenko, <vak@cronyx.ru>
+ *
+ * Copyright (C) 1999-2004 Cronyx Engineering.
+ * Author: Roman Kurakin, <rik@cronyx.ru>
+ *
+ * This software is distributed with NO WARRANTIES, not even the implied
+ * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * Authors grant any other persons or organisations a permission to use,
+ * modify and redistribute this software in source and binary forms,
+ * as long as this message is kept with the software, all derivative
+ * works or modified versions.
+ *
+ * Cronyx Id: if_ct.c,v 1.1.2.22 2004/02/26 19:06:51 rik Exp $
+ */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/param.h>
+
+#if __FreeBSD_version >= 500000
+# define NCTAU 1
+#else
+# include "ctau.h"
+#endif
+
+#if NCTAU > 0
+#include <sys/proc.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/mbuf.h>
+#include <sys/sockio.h>
+#include <sys/malloc.h>
+#include <sys/socket.h>
+#include <sys/conf.h>
+#include <sys/errno.h>
+#include <sys/tty.h>
+#if __FreeBSD_version >= 400000
+# include <sys/bus.h>
+# include <machine/bus.h>
+# include <sys/rman.h>
+# include <isa/isavar.h>
+#endif
+#include <sys/interrupt.h>
+#include <vm/vm.h>
+#include <vm/pmap.h>
+#include <net/if.h>
+#include <machine/cpufunc.h>
+#include <machine/cserial.h>
+#include <machine/clock.h>
+#if __FreeBSD_version < 500000
+#include <i386/isa/isa_device.h>
+#endif
+#if __FreeBSD_version >= 400000
+#include <machine/resource.h>
+# if __FreeBSD_version <= 501000
+# include <i386/isa/intr_machdep.h>
+# endif
+#endif
+#if __FreeBSD_version >= 500000
+#include <dev/cx/machdep.h>
+#include <dev/ctau/ctddk.h>
+#include <dev/cx/cronyxfw.h>
+#else
+#include <i386/isa/cronyx/machdep.h>
+#include <i386/isa/cronyx/ctddk.h>
+#include <i386/isa/cronyx/cronyxfw.h>
+#endif
+#include "opt_ng_cronyx.h"
+#ifdef NETGRAPH_CRONYX
+# include "opt_netgraph.h"
+# include <netgraph/ng_message.h>
+# include <netgraph/netgraph.h>
+#if __FreeBSD_version >= 500000
+# include <dev/ctau/ng_ct.h>
+#else
+# include <netgraph/ng_ct.h>
+#endif
+#else
+# include <net/if_types.h>
+# if __FreeBSD_version < 500000
+# include "sppp.h"
+# if NSPPP <= 0
+# error The device ctau requires sppp or netgraph.
+# endif
+# endif
+# include <net/if_sppp.h>
+# define PP_CISCO IFF_LINK2
+#if __FreeBSD_version < 400000
+# include <bpfilter.h>
+# if NBPFILTER > 0
+# include <net/bpf.h>
+# endif
+#else
+# if __FreeBSD_version < 500000
+# include <bpf.h>
+# endif
+# include <net/bpf.h>
+# define NBPFILTER NBPF
+#endif
+#endif
+
+#define CT_DEBUG(d,s) ({if (d->chan->debug) {\
+ printf ("%s: ", d->name); printf s;}})
+#define CT_DEBUG2(d,s) ({if (d->chan->debug>1) {\
+ printf ("%s: ", d->name); printf s;}})
+#define CDEV_MAJOR 99
+
+#if __FreeBSD_version >= 400000
+static void ct_identify __P((driver_t *, device_t));
+static int ct_probe __P((device_t));
+static int ct_attach __P((device_t));
+static int ct_detach __P((device_t));
+
+static device_method_t ct_isa_methods [] = {
+ DEVMETHOD(device_identify, ct_identify),
+ DEVMETHOD(device_probe, ct_probe),
+ DEVMETHOD(device_attach, ct_attach),
+ DEVMETHOD(device_detach, ct_detach),
+ {0, 0}
+};
+
+typedef struct _bdrv_t {
+ ct_board_t *board;
+ struct resource *base_res;
+ struct resource *drq_res;
+ struct resource *irq_res;
+ int base_rid;
+ int drq_rid;
+ int irq_rid;
+ void *intrhand;
+} bdrv_t;
+
+static driver_t ct_isa_driver = {
+ "ct",
+ ct_isa_methods,
+ sizeof (bdrv_t),
+};
+
+static devclass_t ct_devclass;
+#endif
+
+typedef struct _drv_t {
+ char name [8];
+ ct_chan_t *chan;
+ ct_board_t *board;
+ ct_buf_t buf;
+ int running;
+#ifdef NETGRAPH
+ char nodename [NG_NODELEN+1];
+ hook_p hook;
+ hook_p debug_hook;
+ node_p node;
+ struct ifqueue queue;
+ struct ifqueue hi_queue;
+ short timeout;
+ struct callout_handle timeout_handle;
+#else
+ struct sppp pp;
+#endif
+#if __FreeBSD_version >= 400000
+ dev_t devt;
+#endif
+} drv_t;
+
+static void ct_receive (ct_chan_t *c, char *data, int len);
+static void ct_transmit (ct_chan_t *c, void *attachment, int len);
+static void ct_error (ct_chan_t *c, int data);
+static void ct_up (drv_t *d);
+static void ct_start (drv_t *d);
+static void ct_down (drv_t *d);
+static void ct_watchdog (drv_t *d);
+#ifdef NETGRAPH
+extern struct ng_type typestruct;
+#else
+static void ct_ifstart (struct ifnet *ifp);
+static void ct_tlf (struct sppp *sp);
+static void ct_tls (struct sppp *sp);
+static void ct_ifwatchdog (struct ifnet *ifp);
+static int ct_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data);
+static void ct_initialize (void *softc);
+#endif
+
+static ct_board_t *adapter [NCTAU];
+static drv_t *channel [NCTAU*NCHAN];
+static struct callout_handle led_timo [NCTAU];
+static struct callout_handle timeout_handle;
+
+/*
+ * Print the mbuf chain, for debug purposes only.
+ */
+static void printmbuf (struct mbuf *m)
+{
+ printf ("mbuf:");
+ for (; m; m=m->m_next) {
+ if (m->m_flags & M_PKTHDR)
+ printf (" HDR %d:", m->m_pkthdr.len);
+ if (m->m_flags & M_EXT)
+ printf (" EXT:");
+ printf (" %d", m->m_len);
+ }
+ printf ("\n");
+}
+
+/*
+ * Make an mbuf from data.
+ */
+static struct mbuf *makembuf (void *buf, u_int len)
+{
+ struct mbuf *m;
+
+ MGETHDR (m, M_DONTWAIT, MT_DATA);
+ if (! m)
+ return 0;
+ MCLGET (m, M_DONTWAIT);
+ if (! (m->m_flags & M_EXT)) {
+ m_freem (m);
+ return 0;
+ }
+ m->m_pkthdr.len = m->m_len = len;
+ bcopy (buf, mtod (m, caddr_t), len);
+ return m;
+}
+
+static void ct_timeout (void *arg)
+{
+ drv_t *d;
+ int s, i;
+
+ for (i=0; i<NCTAU*NCHAN; ++i) {
+ d = channel[i];
+ if (! d)
+ continue;
+ if (d->chan->mode != M_G703)
+ continue;
+ s = splimp ();
+ ct_g703_timer (d->chan);
+ splx (s);
+ }
+ timeout_handle = timeout (ct_timeout, 0, hz);
+}
+
+static void ct_led_off (void *arg)
+{
+ ct_board_t *b = arg;
+ int s = splimp ();
+
+ ct_led (b, 0);
+ led_timo[b->num].callout = 0;
+ splx (s);
+}
+
+/*
+ * Activate interupt handler from DDK.
+ */
+#if __FreeBSD_version >= 400000
+static void ct_intr (void *arg)
+{
+ bdrv_t *bd = arg;
+ ct_board_t *b = bd->board;
+#else
+static void ct_intr (int bnum)
+{
+ ct_board_t *b = adapter [bnum];
+#endif
+ int s = splimp ();
+
+ /* Turn LED on. */
+ ct_led (b, 1);
+
+ ct_int_handler (b);
+
+ /* Turn LED off 50 msec later. */
+ if (! led_timo[b->num].callout)
+ led_timo[b->num] = timeout (ct_led_off, b, hz/20);
+ splx (s);
+}
+
+static int probe_irq (ct_board_t *b, int irq)
+{
+ int mask, busy, cnt;
+
+ /* Clear pending irq, if any. */
+ ct_probe_irq (b, -irq);
+ DELAY (100);
+ for (cnt=0; cnt<5; ++cnt) {
+ /* Get the mask of pending irqs, assuming they are busy.
+ * Activate the adapter on given irq. */
+ busy = ct_probe_irq (b, irq);
+ DELAY (1000);
+
+ /* Get the mask of active irqs.
+ * Deactivate our irq. */
+ mask = ct_probe_irq (b, -irq);
+ DELAY (100);
+ if ((mask & ~busy) == 1 << irq) {
+ ct_probe_irq (b, 0);
+ /* printf ("ct%d: irq %d ok, mask=0x%04x, busy=0x%04x\n",
+ b->num, irq, mask, busy); */
+ return 1;
+ }
+ }
+ /* printf ("ct%d: irq %d not functional, mask=0x%04x, busy=0x%04x\n",
+ b->num, irq, mask, busy); */
+ ct_probe_irq (b, 0);
+ return 0;
+}
+
+static short porttab [] = {
+ 0x200, 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0,
+ 0x300, 0x320, 0x340, 0x360, 0x380, 0x3a0, 0x3c0, 0x3e0, 0
+ };
+static char dmatab [] = { 7, 6, 5, 0 };
+static char irqtab [] = { 5, 10, 11, 7, 3, 15, 12, 0 };
+
+#if __FreeBSD_version >= 400000
+static int ct_is_free_res (device_t dev, int rid, int type, u_long start,
+ u_long end, u_long count)
+{
+ struct resource *res;
+
+ if (!(res = bus_alloc_resource (dev, type, &rid, start, end, count,
+ RF_ALLOCATED)))
+ return 0;
+
+ bus_release_resource (dev, type, rid, res);
+
+ return 1;
+}
+
+static void ct_identify (driver_t *driver, device_t dev)
+{
+ u_long iobase, rescount;
+ int devcount;
+ device_t *devices;
+ device_t child;
+ devclass_t my_devclass;
+ int i, k;
+
+ if ((my_devclass = devclass_find ("ct")) == NULL)
+ return;
+
+ devclass_get_devices (my_devclass, &devices, &devcount);
+
+ if (devcount == 0) {
+ /* We should find all devices by our self. We could alter other
+ * devices, but we don't have a choise
+ */
+ for (i = 0; (iobase = porttab [i]) != 0; i++) {
+ if (!ct_is_free_res (dev, 1, SYS_RES_IOPORT,
+ iobase, iobase + NPORT, NPORT))
+ continue;
+ if (ct_probe_board (iobase, -1, -1) == 0)
+ continue;
+
+ devcount++;
+ child = BUS_ADD_CHILD (dev, ISA_ORDER_SPECULATIVE, "ct",
+ -1);
+
+ if (child == NULL)
+ return;
+
+ device_set_desc_copy (child, "Cronyx Tau-ISA");
+ device_set_driver (child, driver);
+ bus_set_resource (child, SYS_RES_IOPORT, 0,
+ iobase, NPORT);
+
+ if (devcount >= NCTAU)
+ break;
+ }
+ } else {
+ static short porttab [] = {
+ 0x200, 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0,
+ 0x300, 0x320, 0x340, 0x360, 0x380, 0x3a0, 0x3c0, 0x3e0, 0
+ };
+ /* Lets check user choise.
+ */
+ for (k = 0; k < devcount; k++) {
+ if (bus_get_resource (devices[k], SYS_RES_IOPORT, 0,
+ &iobase, &rescount) != 0)
+ continue;
+
+ for (i = 0; porttab [i] != 0; i++) {
+ if (porttab [i] != iobase)
+ continue;
+
+ if (!ct_is_free_res (devices[k], 1, SYS_RES_IOPORT,
+ iobase, iobase + NPORT, NPORT))
+ continue;
+
+ if (ct_probe_board (iobase, -1, -1) == 0)
+ continue;
+ porttab [i] = -1;
+ device_set_desc_copy (devices[k], "Cronyx Tau-ISA");
+ break;
+ }
+ if (porttab [i] == 0) {
+ device_delete_child (
+ device_get_parent (devices[k]),
+ devices [k]);
+ devices[k] = 0;
+ continue;
+ }
+ }
+ for (k = 0; k < devcount; k++) {
+ if (devices[k] == 0)
+ continue;
+ if (bus_get_resource (devices[k], SYS_RES_IOPORT, 0,
+ &iobase, &rescount) == 0)
+ continue;
+ for (i = 0; (iobase = porttab [i]) != 0; i++) {
+ if (porttab [i] == -1)
+ continue;
+ if (!ct_is_free_res (devices[k], 1, SYS_RES_IOPORT,
+ iobase, iobase + NPORT, NPORT))
+ continue;
+ if (ct_probe_board (iobase, -1, -1) == 0)
+ continue;
+
+ bus_set_resource (devices[k], SYS_RES_IOPORT, 0,
+ iobase, NPORT);
+ porttab [i] = -1;
+ device_set_desc_copy (devices[k], "Cronyx Tau-ISA");
+ break;
+ }
+ if (porttab [i] == 0) {
+ device_delete_child (
+ device_get_parent (devices[k]),
+ devices [k]);
+ }
+ }
+ free (devices, M_TEMP);
+ }
+
+ return;
+}
+
+static int ct_probe (device_t dev)
+{
+ int unit = device_get_unit (dev);
+ u_long iobase, rescount;
+
+ if (!device_get_desc (dev) ||
+ strcmp (device_get_desc (dev), "Cronyx Tau-ISA"))
+ return ENXIO;
+
+/* KASSERT ((bd != NULL), ("ct%d: NULL device softc\n", unit));*/
+ if (bus_get_resource (dev, SYS_RES_IOPORT, 0, &iobase, &rescount) != 0) {
+ printf ("ct%d: Couldn't get IOPORT\n", unit);
+ return ENXIO;
+ }
+
+ if (!ct_is_free_res (dev, 1, SYS_RES_IOPORT,
+ iobase, iobase + NPORT, NPORT)) {
+ printf ("ct%d: Resource IOPORT isn't free\n", unit);
+ return ENXIO;
+ }
+
+ if (!ct_probe_board (iobase, -1, -1)) {
+ printf ("ct%d: probing for Tau-ISA at %lx faild\n", unit, iobase);
+ return ENXIO;
+ }
+
+ return 0;
+}
+#else /* __FreeBSD_version < 400000 */
+static int ct_probe (struct isa_device *id)
+{
+ int unit = id->id_unit;
+ int iobase;
+ ct_board_t *b;
+ int i;
+
+ iobase = id->id_iobase;
+ if (iobase < 0) {
+ /* Autodetect the adapter. */
+
+ for (i=0; ; i++) {
+ if (! porttab[i]) {
+ iobase = -1;
+ return 0;
+ }
+ iobase = porttab[i];
+ if (unit > 0 && adapter[0] && adapter[0]->port == iobase)
+ continue;
+ if (unit > 1 && adapter[1] && adapter[1]->port == iobase)
+ continue;
+ if (! haveseen_isadev (id, CC_IOADDR | CC_QUIET) &&
+ ct_probe_board (iobase, -1, -1))
+ break;
+ }
+ } else if (! ct_probe_board (iobase, -1, -1))
+ return 0;
+
+ if (id->id_drq < 0) {
+ /* Find available 16-bit DRQ. */
+
+ for (i=0; ; ++i) {
+ if (! dmatab[i]) {
+ printf ("ct%d: no available drq found\n",
+ unit);
+ id->id_drq = -1;
+ return 0;
+ }
+ id->id_drq = dmatab[i];
+ if (! haveseen_isadev (id, CC_DRQ | CC_QUIET)
+ && !isa_dma_acquire (id->id_drq))
+ break;
+ }
+ }
+
+ b = malloc (sizeof (ct_board_t), M_DEVBUF, M_WAITOK);
+ if (!b) {
+ printf ("ct:%d: Couldn't allocate memory\n", unit);
+ return (ENXIO);
+ }
+ adapter[unit] = b;
+ bzero (b, sizeof(ct_board_t));
+
+ if (! ct_open_board (b, unit, iobase,
+ id->id_irq ? ffs (id->id_irq) - 1 : -1, id->id_drq)) {
+ printf ("ct%d: error loading firmware\n", unit);
+ adapter [unit] = 0;
+ free (b, M_DEVBUF);
+ isa_dma_release (id->id_drq);
+ return 0;
+ }
+
+ if (id->id_irq) {
+ if (! probe_irq (b, ffs (id->id_irq) - 1))
+ printf ("ct%d: irq %d not functional\n",
+ unit, ffs (id->id_irq) - 1);
+ } else {
+ /* Find available IRQ. */
+
+ for (i=0; ; ++i) {
+ if (! irqtab[i]) {
+ printf ("ct%d: no available irq found\n",
+ unit);
+ id->id_irq = -1;
+ isa_dma_release (id->id_drq);
+ adapter [unit] = 0;
+ free (b, M_DEVBUF);
+ return 0;
+ }
+ id->id_irq = 1 << irqtab[i];
+ if (haveseen_isadev (id, CC_IRQ | CC_QUIET))
+ continue;
+#ifdef KLD_MODULE
+ if (register_intr (irqtab[i], 0, 0, (inthand2_t*)
+ ct_intr, &net_imask, unit) != 0)
+ continue;
+ unregister_intr (irqtab[i], (inthand2_t*) ct_intr);
+#endif
+ if (probe_irq (b, irqtab[i]))
+ break;
+ }
+ }
+ ct_init_board (b, b->num, b->port, ffs (id->id_irq) - 1, b->dma,
+ b->type, b->osc);
+ ct_setup_board (b, 0, 0, 0);
+
+ return 1;
+}
+#endif /* __FreeBSD_version < 400000 */
+
+extern struct cdevsw ct_cdevsw;
+/*
+ * The adapter is present, initialize the driver structures.
+ */
+#if __FreeBSD_version < 400000
+static int ct_attach (struct isa_device *id)
+{
+#else
+static int ct_attach (device_t dev)
+{
+ bdrv_t *bd = device_get_softc (dev);
+ u_long iobase, drq, irq, rescount;
+ int unit = device_get_unit (dev);
+ int i;
+ int s;
+#endif
+ ct_board_t *b;
+ ct_chan_t *c;
+ drv_t *d;
+
+#if __FreeBSD_version >= 400000
+ KASSERT ((bd != NULL), ("ct%d: NULL device softc\n", unit));
+
+ bus_get_resource (dev, SYS_RES_IOPORT, 0, &iobase, &rescount);
+ bd->base_rid = 0;
+ bd->base_res = bus_alloc_resource (dev, SYS_RES_IOPORT, &bd->base_rid,
+ iobase, iobase + NPORT, NPORT, RF_ACTIVE);
+ if (! bd->base_res) {
+ printf ("ct%d: cannot alloc base address\n", unit);
+ return ENXIO;
+ }
+
+ if (bus_get_resource (dev, SYS_RES_DRQ, 0, &drq, &rescount) != 0) {
+ for (i = 0; (drq = dmatab [i]) != 0; i++) {
+ if (!ct_is_free_res (dev, 1, SYS_RES_DRQ,
+ drq, drq + 1, 1))
+ continue;
+ bus_set_resource (dev, SYS_RES_DRQ, 0, drq, 1);
+ break;
+ }
+
+ if (dmatab[i] == 0) {
+ bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
+ bd->base_res);
+ printf ("ct%d: Couldn't get DRQ\n", unit);
+ return ENXIO;
+ }
+ }
+
+ bd->drq_rid = 0;
+ bd->drq_res = bus_alloc_resource (dev, SYS_RES_DRQ, &bd->drq_rid,
+ drq, drq + 1, 1, RF_ACTIVE);
+ if (! bd->drq_res) {
+ printf ("ct%d: cannot allocate drq\n", unit);
+ bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
+ bd->base_res);
+ return ENXIO;
+ }
+
+ if (bus_get_resource (dev, SYS_RES_IRQ, 0, &irq, &rescount) != 0) {
+ for (i = 0; (irq = irqtab [i]) != 0; i++) {
+ if (!ct_is_free_res (dev, 1, SYS_RES_IRQ,
+ irq, irq + 1, 1))
+ continue;
+ bus_set_resource (dev, SYS_RES_IRQ, 0, irq, 1);
+ break;
+ }
+
+ if (irqtab[i] == 0) {
+ bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
+ bd->drq_res);
+ bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
+ bd->base_res);
+ printf ("ct%d: Couldn't get IRQ\n", unit);
+ return ENXIO;
+ }
+ }
+
+ bd->irq_rid = 0;
+ bd->irq_res = bus_alloc_resource (dev, SYS_RES_IRQ, &bd->irq_rid,
+ irq, irq + 1, 1, RF_ACTIVE);
+ if (! bd->irq_res) {
+ printf ("ct%d: Couldn't allocate irq\n", unit);
+ bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
+ bd->drq_res);
+ bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
+ bd->base_res);
+ return ENXIO;
+ }
+
+ b = malloc (sizeof (ct_board_t), M_DEVBUF, M_WAITOK);
+ if (!b) {
+ printf ("ct:%d: Couldn't allocate memory\n", unit);
+ return (ENXIO);
+ }
+ adapter[unit] = b;
+ bzero (b, sizeof(ct_board_t));
+
+ if (! ct_open_board (b, unit, iobase, irq, drq)) {
+ printf ("ct%d: error loading firmware\n", unit);
+ free (b, M_DEVBUF);
+ bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid,
+ bd->irq_res);
+ bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
+ bd->drq_res);
+ bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
+ bd->base_res);
+ return ENXIO;
+ }
+
+ bd->board = b;
+
+ if (! probe_irq (b, irq)) {
+ printf ("ct%d: irq %ld not functional\n", unit, irq);
+ bd->board = 0;
+ adapter [unit] = 0;
+ free (b, M_DEVBUF);
+ bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid,
+ bd->irq_res);
+ bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
+ bd->drq_res);
+ bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
+ bd->base_res);
+ return ENXIO;
+ }
+
+ s = splimp ();
+ if (bus_setup_intr (dev, bd->irq_res, INTR_TYPE_NET, ct_intr, bd,
+ &bd->intrhand)) {
+ printf ("ct%d: Can't setup irq %ld\n", unit, irq);
+ bd->board = 0;
+ adapter [unit] = 0;
+ free (b, M_DEVBUF);
+ bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid,
+ bd->irq_res);
+ bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
+ bd->drq_res);
+ bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
+ bd->base_res);
+ splx (s);
+ return ENXIO;
+ }
+
+ ct_init_board (b, b->num, b->port, irq, drq, b->type, b->osc);
+ ct_setup_board (b, 0, 0, 0);
+#else
+ b = adapter [id->id_unit];
+#endif
+
+ printf ("ct%d: <Cronyx-%s>, clock %s MHz\n", b->num, b->name,
+ b->osc == 20000000 ? "20" : "16.384");
+#if __FreeBSD_version < 400000
+ id->id_ointr = ct_intr;
+#endif
+
+ for (c=b->chan; c<b->chan+NCHAN; ++c) {
+ d = contigmalloc (sizeof(drv_t), M_DEVBUF, M_WAITOK,
+ 0x100000, 0x1000000, 16, 0);
+ channel [b->num*NCHAN + c->num] = d;
+ bzero (d, sizeof(drv_t));
+ sprintf (d->name, "ct%d.%d", b->num, c->num);
+ d->board = b;
+ d->chan = c;
+ c->sys = d;
+
+#ifdef NETGRAPH
+ if (ng_make_node_common (&typestruct, &d->node) != 0) {
+ printf ("%s: cannot make common node\n", d->name);
+ channel [b->num*NCHAN + c->num] = 0;
+ c->sys = 0;
+#if __FreeBSD_version < 400000
+ free (d, M_DEVBUF);
+#else
+ contigfree (d, sizeof (*d), M_DEVBUF);
+#endif
+ continue;
+ }
+#if __FreeBSD_version >= 500000
+ NG_NODE_SET_PRIVATE (d->node, d);
+#else
+ d->node->private = d;
+#endif
+ sprintf (d->nodename, "%s%d", NG_CT_NODE_TYPE,
+ c->board->num*NCHAN + c->num);
+ if (ng_name_node (d->node, d->nodename)) {
+ printf ("%s: cannot name node\n", d->nodename);
+#if __FreeBSD_version >= 500000
+ NG_NODE_UNREF (d->node);
+#else
+ ng_rmnode (d->node);
+ ng_unref (d->node);
+#endif
+ channel [b->num*NCHAN + c->num] = 0;
+ c->sys = 0;
+#if __FreeBSD_version < 400000
+ free (d, M_DEVBUF);
+#else
+ contigfree (d, sizeof (*d), M_DEVBUF);
+#endif
+ continue;
+ }
+ d->queue.ifq_maxlen = IFQ_MAXLEN;
+ d->hi_queue.ifq_maxlen = IFQ_MAXLEN;
+#if __FreeBSD_version >= 500000
+ mtx_init (&d->queue.ifq_mtx, "ct_queue", NULL, MTX_DEF);
+ mtx_init (&d->hi_queue.ifq_mtx, "ct_queue_hi", NULL, MTX_DEF);
+#endif
+#else /*NETGRAPH*/
+ d->pp.pp_if.if_softc = d;
+#if __FreeBSD_version > 501000
+ if_initname (&d->pp.pp_if, "ct", b->num * NCHAN + c->num);
+#else
+ d->pp.pp_if.if_unit = b->num * NCHAN + c->num;
+ d->pp.pp_if.if_name = "ct";
+#endif
+ d->pp.pp_if.if_mtu = PP_MTU;
+ d->pp.pp_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
+ d->pp.pp_if.if_ioctl = ct_sioctl;
+ d->pp.pp_if.if_start = ct_ifstart;
+ d->pp.pp_if.if_watchdog = ct_ifwatchdog;
+ d->pp.pp_if.if_init = ct_initialize;
+ sppp_attach (&d->pp.pp_if);
+ if_attach (&d->pp.pp_if);
+ d->pp.pp_tlf = ct_tlf;
+ d->pp.pp_tls = ct_tls;
+#if __FreeBSD_version >= 400000 || NBPFILTER > 0
+ /* If BPF is in the kernel, call the attach for it.
+ * Header size is 4 bytes. */
+ bpfattach (&d->pp.pp_if, DLT_PPP, 4);
+#endif
+#endif /*NETGRAPH*/
+ ct_start_chan (c, &d->buf, vtophys (&d->buf));
+ ct_register_receive (c, &ct_receive);
+ ct_register_transmit (c, &ct_transmit);
+ ct_register_error (c, &ct_error);
+#if __FreeBSD_version >= 400000
+ d->devt = make_dev (&ct_cdevsw, b->num*NCHAN+c->num, UID_ROOT,
+ GID_WHEEL, 0600, "ct%d", b->num*NCHAN+c->num);
+ }
+ splx (s);
+
+ return 0;
+#else /* __FreeBSD_version < 400000 */
+ }
+ return 1;
+#endif /*__FreeBSD_version */
+}
+
+#if __FreeBSD_version >= 400000
+static int ct_detach (device_t dev)
+{
+ bdrv_t *bd = device_get_softc (dev);
+ ct_board_t *b = bd->board;
+ ct_chan_t *c;
+ int s = splimp ();
+
+ /* Check if the device is busy (open). */
+ for (c = b->chan; c < b->chan + NCHAN; ++c) {
+ drv_t *d = (drv_t*) c->sys;
+
+ if (!d || !d->chan->type)
+ continue;
+
+ if (d->running) {
+ splx (s);
+ return EBUSY;
+ }
+ }
+
+ /* Deactivate the timeout routine. */
+ if (led_timo[b->num].callout)
+ untimeout (ct_led_off, b, led_timo[b->num]);
+
+ bus_teardown_intr (dev, bd->irq_res, bd->intrhand);
+ bus_deactivate_resource (dev, SYS_RES_IRQ, bd->irq_rid, bd->irq_res);
+ bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid, bd->irq_res);
+
+ bus_deactivate_resource (dev, SYS_RES_DRQ, bd->drq_rid, bd->drq_res);
+ bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid, bd->drq_res);
+
+ bus_deactivate_resource (dev, SYS_RES_IOPORT, bd->base_rid, bd->irq_res);
+ bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid, bd->base_res);
+
+ ct_close_board (b);
+
+ /* Detach the interfaces, free buffer memory. */
+ for (c = b->chan; c < b->chan + NCHAN; ++c) {
+ drv_t *d = (drv_t*) c->sys;
+
+ if (!d || !d->chan->type)
+ continue;
+
+#ifdef NETGRAPH
+#if __FreeBSD_version >= 500000
+ if (d->node) {
+ ng_rmnode_self (d->node);
+ NG_NODE_UNREF (d->node);
+ d->node = NULL;
+ }
+ mtx_destroy (&d->queue.ifq_mtx);
+ mtx_destroy (&d->hi_queue.ifq_mtx);
+#else
+ ng_rmnode (d->node);
+ d->node = 0;
+#endif
+#else
+#if __FreeBSD_version >= 410000 && NBPFILTER > 0
+ /* Detach from the packet filter list of interfaces. */
+ bpfdetach (&d->pp.pp_if);
+#endif
+ /* Detach from the sync PPP list. */
+ sppp_detach (&d->pp.pp_if);
+
+ if_detach (&d->pp.pp_if);
+#endif
+ destroy_dev (d->devt);
+ }
+
+ ct_led_off (b);
+ if (led_timo[b->num].callout)
+ untimeout (ct_led_off, b, led_timo[b->num]);
+ splx (s);
+
+ s = splimp ();
+ for (c = b->chan; c < b->chan + NCHAN; ++c) {
+ drv_t *d = (drv_t*) c->sys;
+
+ if (!d || !d->chan->type)
+ continue;
+
+ /* Deallocate buffers. */
+#if __FreeBSD_version < 400000
+ free (d, M_DEVBUF);
+#else
+ contigfree (d, sizeof (*d), M_DEVBUF);
+#endif
+ }
+ bd->board = 0;
+ adapter [b->num] = 0;
+ free (b, M_DEVBUF);
+ splx (s);
+
+ return 0;
+}
+#endif
+
+#ifndef NETGRAPH
+static void ct_ifstart (struct ifnet *ifp)
+{
+ drv_t *d = ifp->if_softc;
+
+ ct_start (d);
+}
+
+static void ct_ifwatchdog (struct ifnet *ifp)
+{
+ drv_t *d = ifp->if_softc;
+
+ ct_watchdog (d);
+}
+
+static void ct_tlf (struct sppp *sp)
+{
+ drv_t *d = sp->pp_if.if_softc;
+
+ CT_DEBUG (d, ("ct_tlf\n"));
+/* ct_set_dtr (d->chan, 0);*/
+/* ct_set_rts (d->chan, 0);*/
+ sp->pp_down (sp);
+}
+
+static void ct_tls (struct sppp *sp)
+{
+ drv_t *d = sp->pp_if.if_softc;
+
+ CT_DEBUG (d, ("ct_tls\n"));
+ sp->pp_up (sp);
+}
+
+/*
+ * Initialization of interface.
+ * Ii seems to be never called by upper level.
+ */
+static void ct_initialize (void *softc)
+{
+ drv_t *d = softc;
+
+ CT_DEBUG (d, ("ct_initialize\n"));
+}
+
+/*
+ * Process an ioctl request.
+ */
+static int ct_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data)
+{
+ drv_t *d = ifp->if_softc;
+ int error, s, was_up, should_be_up;
+
+ was_up = (ifp->if_flags & IFF_RUNNING) != 0;
+ error = sppp_ioctl (ifp, cmd, data);
+ if (error)
+ return error;
+
+ if (! (ifp->if_flags & IFF_DEBUG))
+ d->chan->debug = 0;
+ else if (! d->chan->debug)
+ d->chan->debug = 1;
+
+ switch (cmd) {
+ default: CT_DEBUG2 (d, ("ioctl 0x%lx\n", cmd)); return 0;
+ case SIOCADDMULTI: CT_DEBUG2 (d, ("SIOCADDMULTI\n")); return 0;
+ case SIOCDELMULTI: CT_DEBUG2 (d, ("SIOCDELMULTI\n")); return 0;
+ case SIOCSIFFLAGS: CT_DEBUG2 (d, ("SIOCSIFFLAGS\n")); break;
+ case SIOCSIFADDR: CT_DEBUG2 (d, ("SIOCSIFADDR\n")); break;
+ }
+
+ /* We get here only in case of SIFFLAGS or SIFADDR. */
+ s = splimp ();
+ should_be_up = (ifp->if_flags & IFF_RUNNING) != 0;
+ if (! was_up && should_be_up) {
+ /* Interface goes up -- start it. */
+ ct_up (d);
+ ct_start (d);
+ } else if (was_up && ! should_be_up) {
+ /* Interface is going down -- stop it. */
+ /* if ((d->pp.pp_flags & PP_FR) || (ifp->if_flags & PP_CISCO))*/
+ ct_down (d);
+ }
+ splx (s);
+ return 0;
+}
+#endif /*NETGRAPH*/
+
+/*
+ * Stop the interface. Called on splimp().
+ */
+static void ct_down (drv_t *d)
+{
+ int s = splimp ();
+ CT_DEBUG (d, ("ct_down\n"));
+ ct_set_dtr (d->chan, 0);
+ ct_set_rts (d->chan, 0);
+ d->running = 0;
+ splx (s);
+}
+
+/*
+ * Start the interface. Called on splimp().
+ */
+static void ct_up (drv_t *d)
+{
+ int s = splimp ();
+ CT_DEBUG (d, ("ct_up\n"));
+ ct_set_dtr (d->chan, 1);
+ ct_set_rts (d->chan, 1);
+ d->running = 1;
+ splx (s);
+}
+
+/*
+ * Start output on the (slave) interface. Get another datagram to send
+ * off of the interface queue, and copy it to the interface
+ * before starting the output.
+ */
+static void ct_send (drv_t *d)
+{
+ struct mbuf *m;
+ u_short len;
+
+ CT_DEBUG2 (d, ("ct_send, tn=%d\n", d->chan->tn));
+
+ /* No output if the interface is down. */
+ if (! d->running)
+ return;
+
+ /* No output if the modem is off. */
+ if (! ct_get_dsr (d->chan) && !ct_get_loop (d->chan))
+ return;
+
+ while (ct_buf_free (d->chan)) {
+ /* Get the packet to send. */
+#ifdef NETGRAPH
+ IF_DEQUEUE (&d->hi_queue, m);
+ if (! m)
+ IF_DEQUEUE (&d->queue, m);
+#else
+ m = sppp_dequeue (&d->pp.pp_if);
+#endif
+ if (! m)
+ return;
+#if (__FreeBSD_version >= 400000 || NBPFILTER > 0) && !defined (NETGRAPH)
+ if (d->pp.pp_if.if_bpf)
+#if __FreeBSD_version >= 500000
+ BPF_MTAP (&d->pp.pp_if, m);
+#else
+ bpf_mtap (&d->pp.pp_if, m);
+#endif
+#endif
+ len = m->m_pkthdr.len;
+ if (! m->m_next)
+ ct_send_packet (d->chan, (u_char*)mtod (m, caddr_t),
+ len, 0);
+ else {
+ m_copydata (m, 0, len, d->chan->tbuf[d->chan->te]);
+ ct_send_packet (d->chan, d->chan->tbuf[d->chan->te],
+ len, 0);
+ }
+ m_freem (m);
+
+ /* Set up transmit timeout, if the transmit ring is not empty.
+ * Transmit timeout is 10 seconds. */
+#ifdef NETGRAPH
+ d->timeout = 10;
+#else
+ d->pp.pp_if.if_timer = 10;
+#endif
+ }
+#ifndef NETGRAPH
+ d->pp.pp_if.if_flags |= IFF_OACTIVE;
+#endif
+}
+
+/*
+ * Start output on the interface.
+ * Always called on splimp().
+ */
+static void ct_start (drv_t *d)
+{
+ int s = splimp ();
+
+ if (d->running) {
+ if (! d->chan->dtr)
+ ct_set_dtr (d->chan, 1);
+ if (! d->chan->rts)
+ ct_set_rts (d->chan, 1);
+ ct_send (d);
+ }
+
+ splx (s);
+}
+
+/*
+ * Handle transmit timeouts.
+ * Recover after lost transmit interrupts.
+ * Always called on splimp().
+ */
+static void ct_watchdog (drv_t *d)
+{
+ int s = splimp ();
+
+ CT_DEBUG (d, ("device timeout\n"));
+ if (d->running) {
+ ct_setup_chan (d->chan);
+ ct_start_chan (d->chan, 0, 0);
+ ct_set_dtr (d->chan, 1);
+ ct_set_rts (d->chan, 1);
+ ct_start (d);
+ }
+
+ splx (s);
+}
+
+/*
+ * Transmit callback function.
+ */
+static void ct_transmit (ct_chan_t *c, void *attachment, int len)
+{
+ drv_t *d = c->sys;
+
+ if (!d)
+ return;
+#ifdef NETGRAPH
+ d->timeout = 0;
+#else
+ ++d->pp.pp_if.if_opackets;
+ d->pp.pp_if.if_flags &= ~IFF_OACTIVE;
+ d->pp.pp_if.if_timer = 0;
+#endif
+ ct_start (d);
+}
+
+/*
+ * Process the received packet.
+ */
+static void ct_receive (ct_chan_t *c, char *data, int len)
+{
+ drv_t *d = c->sys;
+ struct mbuf *m;
+#if __FreeBSD_version >= 500000 && defined NETGRAPH
+ int error;
+#endif
+
+ if (!d || !d->running)
+ return;
+
+ m = makembuf (data, len);
+ if (! m) {
+ CT_DEBUG (d, ("no memory for packet\n"));
+#ifndef NETGRAPH
+ ++d->pp.pp_if.if_iqdrops;
+#endif
+ return;
+ }
+ if (c->debug > 1)
+ printmbuf (m);
+#ifdef NETGRAPH
+ m->m_pkthdr.rcvif = 0;
+#if __FreeBSD_version >= 500000
+ NG_SEND_DATA_ONLY (error, d->hook, m);
+#else
+ ng_queue_data (d->hook, m, 0);
+#endif
+#else
+ ++d->pp.pp_if.if_ipackets;
+ m->m_pkthdr.rcvif = &d->pp.pp_if;
+#if __FreeBSD_version >= 400000 || NBPFILTER > 0
+ /* Check if there's a BPF listener on this interface.
+ * If so, hand off the raw packet to bpf. */
+ if (d->pp.pp_if.if_bpf)
+#if __FreeBSD_version >= 500000
+ BPF_TAP (&d->pp.pp_if, data, len);
+#else
+ bpf_tap (&d->pp.pp_if, data, len);
+#endif
+#endif
+ sppp_input (&d->pp.pp_if, m);
+#endif
+}
+
+/*
+ * Error callback function.
+ */
+static void ct_error (ct_chan_t *c, int data)
+{
+ drv_t *d = c->sys;
+
+ if (!d)
+ return;
+
+ switch (data) {
+ case CT_FRAME:
+ CT_DEBUG (d, ("frame error\n"));
+#ifndef NETGRAPH
+ ++d->pp.pp_if.if_ierrors;
+#endif
+ break;
+ case CT_CRC:
+ CT_DEBUG (d, ("crc error\n"));
+#ifndef NETGRAPH
+ ++d->pp.pp_if.if_ierrors;
+#endif
+ break;
+ case CT_OVERRUN:
+ CT_DEBUG (d, ("overrun error\n"));
+#ifndef NETGRAPH
+ ++d->pp.pp_if.if_collisions;
+ ++d->pp.pp_if.if_ierrors;
+#endif
+ break;
+ case CT_OVERFLOW:
+ CT_DEBUG (d, ("overflow error\n"));
+#ifndef NETGRAPH
+ ++d->pp.pp_if.if_ierrors;
+#endif
+ break;
+ case CT_UNDERRUN:
+ CT_DEBUG (d, ("underrun error\n"));
+#ifdef NETGRAPH
+ d->timeout = 0;
+#else
+ ++d->pp.pp_if.if_oerrors;
+ d->pp.pp_if.if_flags &= ~IFF_OACTIVE;
+ d->pp.pp_if.if_timer = 0;
+#endif
+ ct_start (d);
+ break;
+ default:
+ CT_DEBUG (d, ("error #%d\n", data));
+ }
+}
+
+#if __FreeBSD_version < 500000
+static int ct_open (dev_t dev, int oflags, int devtype, struct proc *p)
+#else
+static int ct_open (dev_t dev, int oflags, int devtype, struct thread *td)
+#endif
+{
+ drv_t *d;
+
+ if (minor(dev) >= NCTAU*NCHAN || ! (d = channel[minor(dev)]))
+ return ENXIO;
+
+ CT_DEBUG2 (d, ("ct_open\n"));
+ return 0;
+}
+
+#if __FreeBSD_version < 500000
+static int ct_close (dev_t dev, int fflag, int devtype, struct proc *p)
+#else
+static int ct_close (dev_t dev, int fflag, int devtype, struct thread *td)
+#endif
+{
+ drv_t *d = channel [minor(dev)];
+
+ if (!d)
+ return 0;
+
+ CT_DEBUG2 (d, ("ct_close\n"));
+ return 0;
+}
+
+static int ct_modem_status (ct_chan_t *c)
+{
+ drv_t *d = c->sys;
+ int status, s;
+
+ if (!d)
+ return 0;
+
+ status = d->running ? TIOCM_LE : 0;
+ s = splimp ();
+ if (ct_get_cd (c)) status |= TIOCM_CD;
+ if (ct_get_cts (c)) status |= TIOCM_CTS;
+ if (ct_get_dsr (c)) status |= TIOCM_DSR;
+ if (c->dtr) status |= TIOCM_DTR;
+ if (c->rts) status |= TIOCM_RTS;
+ splx (s);
+ return status;
+}
+
+/*
+ * Process an ioctl request on /dev/cronyx/ctauN.
+ */
+#if __FreeBSD_version < 500000
+static int ct_ioctl (dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
+#else
+static int ct_ioctl (dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
+#endif
+{
+ drv_t *d = channel [minor (dev)];
+ ct_chan_t *c;
+ struct serial_statistics *st;
+ struct e1_statistics *opte1;
+ int error, s;
+ char mask[16];
+
+ if (!d || !d->chan)
+ return 0;
+
+ c = d->chan;
+
+ switch (cmd) {
+ case SERIAL_GETREGISTERED:
+ bzero (mask, sizeof(mask));
+ for (s=0; s<NCTAU*NCHAN; ++s)
+ if (channel [s])
+ mask [s/8] |= 1 << (s & 7);
+ bcopy (mask, data, sizeof (mask));
+ return 0;
+
+#ifndef NETGRAPH
+ case SERIAL_GETPROTO:
+ strcpy ((char*)data, /*(d->pp.pp_flags & PP_FR) ? "fr" :*/
+ (d->pp.pp_if.if_flags & PP_CISCO) ? "cisco" : "ppp");
+ return 0;
+
+ case SERIAL_SETPROTO:
+ /* Only for superuser! */
+#if __FreeBSD_version < 400000
+ error = suser (p->p_ucred, &p->p_acflag);
+#elif __FreeBSD_version < 500000
+ error = suser (p);
+#else /* __FreeBSD_version >= 500000 */
+ error = suser (td);
+#endif /* __FreeBSD_version >= 500000 */
+ if (error)
+ return error;
+ if (d->pp.pp_if.if_flags & IFF_RUNNING)
+ return EBUSY;
+ if (! strcmp ("cisco", (char*)data)) {
+/* d->pp.pp_flags &= ~(PP_FR);*/
+ d->pp.pp_flags |= PP_KEEPALIVE;
+ d->pp.pp_if.if_flags |= PP_CISCO;
+/* } else if (! strcmp ("fr", (char*)data)) {
+ d->pp.pp_if.if_flags &= ~(PP_CISCO);
+ d->pp.pp_flags |= PP_FR | PP_KEEPALIVE;*/
+ } else if (! strcmp ("ppp", (char*)data)) {
+ d->pp.pp_flags &= ~(/*PP_FR | */PP_KEEPALIVE);
+ d->pp.pp_if.if_flags &= ~(PP_CISCO);
+ } else
+ return EINVAL;
+ return 0;
+
+ case SERIAL_GETKEEPALIVE:
+ if (/*(d->pp.pp_flags & PP_FR) ||*/
+ (d->pp.pp_if.if_flags & PP_CISCO))
+ return EINVAL;
+ *(int*)data = (d->pp.pp_flags & PP_KEEPALIVE) ? 1 : 0;
+ return 0;
+
+ case SERIAL_SETKEEPALIVE:
+ /* Only for superuser! */
+#if __FreeBSD_version < 400000
+ error = suser (p->p_ucred, &p->p_acflag);
+#elif __FreeBSD_version < 500000
+ error = suser (p);
+#else /* __FreeBSD_version >= 500000 */
+ error = suser (td);
+#endif /* __FreeBSD_version >= 500000 */
+ if (error)
+ return error;
+ if (/*(d->pp.pp_flags & PP_FR) ||*/
+ (d->pp.pp_if.if_flags & PP_CISCO))
+ return EINVAL;
+ if (*(int*)data)
+ d->pp.pp_flags |= PP_KEEPALIVE;
+ else
+ d->pp.pp_flags &= ~PP_KEEPALIVE;
+ return 0;
+#endif /*NETGRAPH*/
+
+ case SERIAL_GETMODE:
+ *(int*)data = SERIAL_HDLC;
+ return 0;
+
+ case SERIAL_GETCFG:
+ if (c->mode == M_HDLC)
+ return EINVAL;
+ switch (ct_get_config (c->board)) {
+ default: *(char*)data = 'a'; break;
+ case CFG_B: *(char*)data = 'b'; break;
+ case CFG_C: *(char*)data = 'c'; break;
+ }
+ return 0;
+
+ case SERIAL_SETCFG:
+ /* Only for superuser! */
+#if __FreeBSD_version < 400000
+ error = suser (p->p_ucred, &p->p_acflag);
+#elif __FreeBSD_version < 500000
+ error = suser (p);
+#else /* __FreeBSD_version >= 500000 */
+ error = suser (td);
+#endif /* __FreeBSD_version >= 500000 */
+ if (error)
+ return error;
+ if (c->mode == M_HDLC)
+ return EINVAL;
+ s = splimp ();
+ switch (*(char*)data) {
+ case 'a': ct_set_config (c->board, CFG_A); break;
+ case 'b': ct_set_config (c->board, CFG_B); break;
+ case 'c': ct_set_config (c->board, CFG_C); break;
+ }
+ splx (s);
+ return 0;
+
+ case SERIAL_GETSTAT:
+ st = (struct serial_statistics*) data;
+ st->rintr = c->rintr;
+ st->tintr = c->tintr;
+ st->mintr = c->mintr;
+ st->ibytes = c->ibytes;
+ st->ipkts = c->ipkts;
+ st->ierrs = c->ierrs;
+ st->obytes = c->obytes;
+ st->opkts = c->opkts;
+ st->oerrs = c->oerrs;
+ return 0;
+
+ case SERIAL_GETESTAT:
+ opte1 = (struct e1_statistics*)data;
+ opte1->status = c->status;
+ opte1->cursec = c->cursec;
+ opte1->totsec = c->totsec + c->cursec;
+
+ opte1->currnt.bpv = c->currnt.bpv;
+ opte1->currnt.fse = c->currnt.fse;
+ opte1->currnt.crce = c->currnt.crce;
+ opte1->currnt.rcrce = c->currnt.rcrce;
+ opte1->currnt.uas = c->currnt.uas;
+ opte1->currnt.les = c->currnt.les;
+ opte1->currnt.es = c->currnt.es;
+ opte1->currnt.bes = c->currnt.bes;
+ opte1->currnt.ses = c->currnt.ses;
+ opte1->currnt.oofs = c->currnt.oofs;
+ opte1->currnt.css = c->currnt.css;
+ opte1->currnt.dm = c->currnt.dm;
+
+ opte1->total.bpv = c->total.bpv + c->currnt.bpv;
+ opte1->total.fse = c->total.fse + c->currnt.fse;
+ opte1->total.crce = c->total.crce + c->currnt.crce;
+ opte1->total.rcrce = c->total.rcrce + c->currnt.rcrce;
+ opte1->total.uas = c->total.uas + c->currnt.uas;
+ opte1->total.les = c->total.les + c->currnt.les;
+ opte1->total.es = c->total.es + c->currnt.es;
+ opte1->total.bes = c->total.bes + c->currnt.bes;
+ opte1->total.ses = c->total.ses + c->currnt.ses;
+ opte1->total.oofs = c->total.oofs + c->currnt.oofs;
+ opte1->total.css = c->total.css + c->currnt.css;
+ opte1->total.dm = c->total.dm + c->currnt.dm;
+ for (s=0; s<48; ++s) {
+ opte1->interval[s].bpv = c->interval[s].bpv;
+ opte1->interval[s].fse = c->interval[s].fse;
+ opte1->interval[s].crce = c->interval[s].crce;
+ opte1->interval[s].rcrce = c->interval[s].rcrce;
+ opte1->interval[s].uas = c->interval[s].uas;
+ opte1->interval[s].les = c->interval[s].les;
+ opte1->interval[s].es = c->interval[s].es;
+ opte1->interval[s].bes = c->interval[s].bes;
+ opte1->interval[s].ses = c->interval[s].ses;
+ opte1->interval[s].oofs = c->interval[s].oofs;
+ opte1->interval[s].css = c->interval[s].css;
+ opte1->interval[s].dm = c->interval[s].dm;
+ }
+ return 0;
+
+ case SERIAL_CLRSTAT:
+ /* Only for superuser! */
+#if __FreeBSD_version < 400000
+ error = suser (p->p_ucred, &p->p_acflag);
+#elif __FreeBSD_version < 500000
+ error = suser (p);
+#else /* __FreeBSD_version >= 500000 */
+ error = suser (td);
+#endif /* __FreeBSD_version >= 500000 */
+ if (error)
+ return error;
+ c->rintr = 0;
+ c->tintr = 0;
+ c->mintr = 0;
+ c->ibytes = 0;
+ c->ipkts = 0;
+ c->ierrs = 0;
+ c->obytes = 0;
+ c->opkts = 0;
+ c->oerrs = 0;
+ bzero (&c->currnt, sizeof (c->currnt));
+ bzero (&c->total, sizeof (c->total));
+ bzero (c->interval, sizeof (c->interval));
+ return 0;
+
+ case SERIAL_GETBAUD:
+ *(long*)data = ct_get_baud(c);
+ return 0;
+
+ case SERIAL_SETBAUD:
+ /* Only for superuser! */
+#if __FreeBSD_version < 400000
+ error = suser (p->p_ucred, &p->p_acflag);
+#elif __FreeBSD_version < 500000
+ error = suser (p);
+#else /* __FreeBSD_version >= 500000 */
+ error = suser (td);
+#endif /* __FreeBSD_version >= 500000 */
+ if (error)
+ return error;
+ s = splimp ();
+ ct_set_baud (c, *(long*)data);
+ splx (s);
+ return 0;
+
+ case SERIAL_GETLOOP:
+ *(int*)data = ct_get_loop (c);
+ return 0;
+
+ case SERIAL_SETLOOP:
+ /* Only for superuser! */
+#if __FreeBSD_version < 400000
+ error = suser (p->p_ucred, &p->p_acflag);
+#elif __FreeBSD_version < 500000
+ error = suser (p);
+#else /* __FreeBSD_version >= 500000 */
+ error = suser (td);
+#endif /* __FreeBSD_version >= 500000 */
+ if (error)
+ return error;
+ s = splimp ();
+ ct_set_loop (c, *(int*)data);
+ splx (s);
+ return 0;
+
+ case SERIAL_GETDPLL:
+ if (c->mode == M_E1 || c->mode == M_G703)
+ return EINVAL;
+ *(int*)data = ct_get_dpll (c);
+ return 0;
+
+ case SERIAL_SETDPLL:
+ /* Only for superuser! */
+#if __FreeBSD_version < 400000
+ error = suser (p->p_ucred, &p->p_acflag);
+#elif __FreeBSD_version < 500000
+ error = suser (p);
+#else /* __FreeBSD_version >= 500000 */
+ error = suser (td);
+#endif /* __FreeBSD_version >= 500000 */
+ if (error)
+ return error;
+ if (c->mode == M_E1 || c->mode == M_G703)
+ return EINVAL;
+ s = splimp ();
+ ct_set_dpll (c, *(int*)data);
+ splx (s);
+ return 0;
+
+ case SERIAL_GETNRZI:
+ if (c->mode == M_E1 || c->mode == M_G703)
+ return EINVAL;
+ *(int*)data = ct_get_nrzi (c);
+ return 0;
+
+ case SERIAL_SETNRZI:
+ /* Only for superuser! */
+#if __FreeBSD_version < 400000
+ error = suser (p->p_ucred, &p->p_acflag);
+#elif __FreeBSD_version < 500000
+ error = suser (p);
+#else /* __FreeBSD_version >= 500000 */
+ error = suser (td);
+#endif /* __FreeBSD_version >= 500000 */
+ if (error)
+ return error;
+ if (c->mode == M_E1 || c->mode == M_G703)
+ return EINVAL;
+ s = splimp ();
+ ct_set_nrzi (c, *(int*)data);
+ splx (s);
+ return 0;
+
+ case SERIAL_GETDEBUG:
+ *(int*)data = c->debug;
+ return 0;
+
+ case SERIAL_SETDEBUG:
+ /* Only for superuser! */
+#if __FreeBSD_version < 400000
+ error = suser (p->p_ucred, &p->p_acflag);
+#elif __FreeBSD_version < 500000
+ error = suser (p);
+#else /* __FreeBSD_version >= 500000 */
+ error = suser (td);
+#endif /* __FreeBSD_version >= 500000 */
+ if (error)
+ return error;
+ c->debug = *(int*)data;
+#ifndef NETGRAPH
+ if (d->chan->debug)
+ d->pp.pp_if.if_flags |= IFF_DEBUG;
+ else
+ d->pp.pp_if.if_flags &= (~IFF_DEBUG);
+#endif
+ return 0;
+
+ case SERIAL_GETHIGAIN:
+ if (c->mode != M_E1)
+ return EINVAL;
+ *(int*)data = ct_get_higain (c);
+ return 0;
+
+ case SERIAL_SETHIGAIN:
+ /* Only for superuser! */
+#if __FreeBSD_version < 400000
+ error = suser (p->p_ucred, &p->p_acflag);
+#elif __FreeBSD_version < 500000
+ error = suser (p);
+#else /* __FreeBSD_version >= 500000 */
+ error = suser (td);
+#endif /* __FreeBSD_version >= 500000 */
+ if (error)
+ return error;
+ s = splimp ();
+ ct_set_higain (c, *(int*)data);
+ splx (s);
+ return 0;
+
+ case SERIAL_GETPHONY:
+ CT_DEBUG2 (d, ("ioctl: getphony\n"));
+ if (c->mode != M_E1)
+ return EINVAL;
+ *(int*)data = c->gopt.phony;
+ return 0;
+
+ case SERIAL_SETPHONY:
+ CT_DEBUG2 (d, ("ioctl: setphony\n"));
+ if (c->mode != M_E1)
+ return EINVAL;
+ /* Only for superuser! */
+#if __FreeBSD_version < 400000
+ error = suser (p->p_ucred, &p->p_acflag);
+#elif __FreeBSD_version < 500000
+ error = suser (p);
+#else /* __FreeBSD_version >= 500000 */
+ error = suser (td);
+#endif /* __FreeBSD_version >= 500000 */
+ if (error)
+ return error;
+ s = splimp ();
+ ct_set_phony (c, *(int*)data);
+ splx (s);
+ return 0;
+
+ case SERIAL_GETCLK:
+ if (c->mode != M_E1 && c->mode != M_G703)
+ return EINVAL;
+ switch (ct_get_clk(c)) {
+ default: *(int*)data = E1CLK_INTERNAL; break;
+ case GCLK_RCV: *(int*)data = E1CLK_RECEIVE; break;
+ case GCLK_RCLKO: *(int*)data = c->num ?
+ E1CLK_RECEIVE_CHAN0 : E1CLK_RECEIVE_CHAN1; break;
+ }
+ return 0;
+
+ case SERIAL_SETCLK:
+ /* Only for superuser! */
+#if __FreeBSD_version < 400000
+ error = suser (p->p_ucred, &p->p_acflag);
+#elif __FreeBSD_version < 500000
+ error = suser (p);
+#else /* __FreeBSD_version >= 500000 */
+ error = suser (td);
+#endif /* __FreeBSD_version >= 500000 */
+ if (error)
+ return error;
+ s = splimp ();
+ switch (*(int*)data) {
+ default: ct_set_clk (c, GCLK_INT); break;
+ case E1CLK_RECEIVE: ct_set_clk (c, GCLK_RCV); break;
+ case E1CLK_RECEIVE_CHAN0:
+ case E1CLK_RECEIVE_CHAN1:
+ ct_set_clk (c, GCLK_RCLKO); break;
+ }
+ splx (s);
+ return 0;
+
+ case SERIAL_GETTIMESLOTS:
+ if (c->mode != M_E1)
+ return EINVAL;
+ *(long*)data = ct_get_ts (c);
+ return 0;
+
+ case SERIAL_SETTIMESLOTS:
+ /* Only for superuser! */
+#if __FreeBSD_version < 400000
+ error = suser (p->p_ucred, &p->p_acflag);
+#elif __FreeBSD_version < 500000
+ error = suser (p);
+#else /* __FreeBSD_version >= 500000 */
+ error = suser (td);
+#endif /* __FreeBSD_version >= 500000 */
+ if (error)
+ return error;
+ s = splimp ();
+ ct_set_ts (c, *(long*)data);
+ splx (s);
+ return 0;
+
+ case SERIAL_GETSUBCHAN:
+ if (c->mode != M_E1)
+ return EINVAL;
+ *(long*)data = ct_get_subchan (c->board);
+ return 0;
+
+ case SERIAL_SETSUBCHAN:
+ /* Only for superuser! */
+#if __FreeBSD_version < 400000
+ error = suser (p->p_ucred, &p->p_acflag);
+#elif __FreeBSD_version < 500000
+ error = suser (p);
+#else /* __FreeBSD_version >= 500000 */
+ error = suser (td);
+#endif /* __FreeBSD_version >= 500000 */
+ if (error)
+ return error;
+ s = splimp ();
+ ct_set_subchan (c->board, *(long*)data);
+ splx (s);
+ return 0;
+
+ case SERIAL_GETINVCLK:
+ case SERIAL_GETINVTCLK:
+ if (c->mode == M_E1 || c->mode == M_G703)
+ return EINVAL;
+ *(int*)data = ct_get_invtxc (c);
+ return 0;
+
+ case SERIAL_GETINVRCLK:
+ if (c->mode == M_E1 || c->mode == M_G703)
+ return EINVAL;
+ *(int*)data = ct_get_invrxc (c);
+ return 0;
+
+ case SERIAL_SETINVCLK:
+ case SERIAL_SETINVTCLK:
+ /* Only for superuser! */
+#if __FreeBSD_version < 400000
+ error = suser (p->p_ucred, &p->p_acflag);
+#elif __FreeBSD_version < 500000
+ error = suser (p);
+#else /* __FreeBSD_version >= 500000 */
+ error = suser (td);
+#endif /* __FreeBSD_version >= 500000 */
+ if (error)
+ return error;
+ if (c->mode == M_E1 || c->mode == M_G703)
+ return EINVAL;
+ s = splimp ();
+ ct_set_invtxc (c, *(int*)data);
+ splx (s);
+ return 0;
+
+ case SERIAL_SETINVRCLK:
+ /* Only for superuser! */
+#if __FreeBSD_version < 400000
+ error = suser (p->p_ucred, &p->p_acflag);
+#elif __FreeBSD_version < 500000
+ error = suser (p);
+#else /* __FreeBSD_version >= 500000 */
+ error = suser (td);
+#endif /* __FreeBSD_version >= 500000 */
+ if (error)
+ return error;
+ if (c->mode == M_E1 || c->mode == M_G703)
+ return EINVAL;
+ s = splimp ();
+ ct_set_invrxc (c, *(int*)data);
+ splx (s);
+ return 0;
+
+ case SERIAL_GETLEVEL:
+ if (c->mode != M_G703)
+ return EINVAL;
+ s = splimp ();
+ *(int*)data = ct_get_lq (c);
+ splx (s);
+ return 0;
+
+ case TIOCSDTR: /* Set DTR */
+ s = splimp ();
+ ct_set_dtr (c, 1);
+ splx (s);
+ return 0;
+
+ case TIOCCDTR: /* Clear DTR */
+ s = splimp ();
+ ct_set_dtr (c, 0);
+ splx (s);
+ return 0;
+
+ case TIOCMSET: /* Set DTR/RTS */
+ s = splimp ();
+ ct_set_dtr (c, (*(int*)data & TIOCM_DTR) ? 1 : 0);
+ ct_set_rts (c, (*(int*)data & TIOCM_RTS) ? 1 : 0);
+ splx (s);
+ return 0;
+
+ case TIOCMBIS: /* Add DTR/RTS */
+ s = splimp ();
+ if (*(int*)data & TIOCM_DTR) ct_set_dtr (c, 1);
+ if (*(int*)data & TIOCM_RTS) ct_set_rts (c, 1);
+ splx (s);
+ return 0;
+
+ case TIOCMBIC: /* Clear DTR/RTS */
+ s = splimp ();
+ if (*(int*)data & TIOCM_DTR) ct_set_dtr (c, 0);
+ if (*(int*)data & TIOCM_RTS) ct_set_rts (c, 0);
+ splx (s);
+ return 0;
+
+ case TIOCMGET: /* Get modem status */
+ *(int*)data = ct_modem_status (c);
+ return 0;
+ }
+ return ENOTTY;
+}
+
+#if __FreeBSD_version < 400000
+struct isa_driver ctdriver = { ct_probe, ct_attach, "ct" };
+static struct cdevsw ct_cdevsw = {
+ ct_open, ct_close, noread, nowrite,
+ ct_ioctl, nostop, noreset, nodevtotty,
+ seltrue, nommap, NULL, "ct",
+ NULL, -1,
+};
+#elif __FreeBSD_version < 500000
+static struct cdevsw ct_cdevsw = {
+ ct_open, ct_close, noread, nowrite,
+ ct_ioctl, nopoll, nommap, nostrategy,
+ "ct", CDEV_MAJOR, nodump, nopsize,
+ D_NAGGED, -1
+};
+#elif __FreeBSD_version == 500000
+static struct cdevsw ct_cdevsw = {
+ ct_open, ct_close, noread, nowrite,
+ ct_ioctl, nopoll, nommap, nostrategy,
+ "ct", CDEV_MAJOR, nodump, nopsize,
+ D_NAGGED,
+ };
+#elif __FreeBSD_version <= 501000
+static struct cdevsw ct_cdevsw = {
+ .d_open = ct_open,
+ .d_close = ct_close,
+ .d_read = noread,
+ .d_write = nowrite,
+ .d_ioctl = ct_ioctl,
+ .d_poll = nopoll,
+ .d_mmap = nommap,
+ .d_strategy = nostrategy,
+ .d_name = "ct",
+ .d_maj = CDEV_MAJOR,
+ .d_dump = nodump,
+ .d_flags = D_NAGGED,
+};
+#elif __FreeBSD_version < 502103
+static struct cdevsw ct_cdevsw = {
+ .d_open = ct_open,
+ .d_close = ct_close,
+ .d_ioctl = ct_ioctl,
+ .d_name = "ct",
+ .d_maj = CDEV_MAJOR,
+ .d_flags = D_NAGGED,
+};
+#else /* __FreeBSD_version >= 502103 */
+static struct cdevsw ct_cdevsw = {
+ .d_version = D_VERSION,
+ .d_open = ct_open,
+ .d_close = ct_close,
+ .d_ioctl = ct_ioctl,
+ .d_name = "ct",
+ .d_maj = CDEV_MAJOR,
+ .d_flags = D_NEEDGIANT,
+};
+#endif /* __FreeBSD_version > 501000 */
+
+#ifdef NETGRAPH
+#if __FreeBSD_version >= 500000
+static int ng_ct_constructor (node_p node)
+{
+ drv_t *d = NG_NODE_PRIVATE (node);
+#else
+static int ng_ct_constructor (node_p *node)
+{
+ drv_t *d = (*node)->private;
+#endif
+ CT_DEBUG (d, ("Constructor\n"));
+ return EINVAL;
+}
+
+static int ng_ct_newhook (node_p node, hook_p hook, const char *name)
+{
+ int s;
+#if __FreeBSD_version >= 500000
+ drv_t *d = NG_NODE_PRIVATE (node);
+#else
+ drv_t *d = node->private;
+#endif
+
+ if (!d)
+ return EINVAL;
+
+ /* Attach debug hook */
+ if (strcmp (name, NG_CT_HOOK_DEBUG) == 0) {
+#if __FreeBSD_version >= 500000
+ NG_HOOK_SET_PRIVATE (hook, NULL);
+#else
+ hook->private = 0;
+#endif
+ d->debug_hook = hook;
+ return 0;
+ }
+
+ /* Check for raw hook */
+ if (strcmp (name, NG_CT_HOOK_RAW) != 0)
+ return EINVAL;
+
+#if __FreeBSD_version >= 500000
+ NG_HOOK_SET_PRIVATE (hook, d);
+#else
+ hook->private = d;
+#endif
+ d->hook = hook;
+ s = splimp ();
+ ct_up (d);
+ splx (s);
+ return 0;
+}
+
+static char *format_timeslots (u_long s)
+{
+ static char buf [100];
+ char *p = buf;
+ int i;
+
+ for (i=1; i<32; ++i)
+ if ((s >> i) & 1) {
+ int prev = (i > 1) & (s >> (i-1));
+ int next = (i < 31) & (s >> (i+1));
+
+ if (prev) {
+ if (next)
+ continue;
+ *p++ = '-';
+ } else if (p > buf)
+ *p++ = ',';
+
+ if (i >= 10)
+ *p++ = '0' + i / 10;
+ *p++ = '0' + i % 10;
+ }
+ *p = 0;
+ return buf;
+}
+
+static int print_modems (char *s, ct_chan_t *c, int need_header)
+{
+ int status = ct_modem_status (c);
+ int length = 0;
+
+ if (need_header)
+ length += sprintf (s + length, " LE DTR DSR RTS CTS CD\n");
+ length += sprintf (s + length, "%4s %4s %4s %4s %4s %4s\n",
+ status & TIOCM_LE ? "On" : "-",
+ status & TIOCM_DTR ? "On" : "-",
+ status & TIOCM_DSR ? "On" : "-",
+ status & TIOCM_RTS ? "On" : "-",
+ status & TIOCM_CTS ? "On" : "-",
+ status & TIOCM_CD ? "On" : "-");
+ return length;
+}
+
+static int print_stats (char *s, ct_chan_t *c, int need_header)
+{
+ struct serial_statistics st;
+ int length = 0;
+
+ st.rintr = c->rintr;
+ st.tintr = c->tintr;
+ st.mintr = c->mintr;
+ st.ibytes = c->ibytes;
+ st.ipkts = c->ipkts;
+ st.ierrs = c->ierrs;
+ st.obytes = c->obytes;
+ st.opkts = c->opkts;
+ st.oerrs = c->oerrs;
+ if (need_header)
+ length += sprintf (s + length, " Rintr Tintr Mintr Ibytes Ipkts Ierrs Obytes Opkts Oerrs\n");
+ length += sprintf (s + length, "%7ld %7ld %7ld %8ld %7ld %7ld %8ld %7ld %7ld\n",
+ st.rintr, st.tintr, st.mintr, st.ibytes, st.ipkts,
+ st.ierrs, st.obytes, st.opkts, st.oerrs);
+ return length;
+}
+
+static char *format_e1_status (u_char status)
+{
+ static char buf [80];
+
+ if (status & E1_NOALARM)
+ return "Ok";
+ buf[0] = 0;
+ if (status & E1_LOS) strcat (buf, ",LOS");
+ if (status & E1_AIS) strcat (buf, ",AIS");
+ if (status & E1_LOF) strcat (buf, ",LOF");
+ if (status & E1_LOMF) strcat (buf, ",LOMF");
+ if (status & E1_FARLOF) strcat (buf, ",FARLOF");
+ if (status & E1_AIS16) strcat (buf, ",AIS16");
+ if (status & E1_FARLOMF) strcat (buf, ",FARLOMF");
+ if (status & E1_TSTREQ) strcat (buf, ",TSTREQ");
+ if (status & E1_TSTERR) strcat (buf, ",TSTERR");
+ if (buf[0] == ',')
+ return buf+1;
+ return "Unknown";
+}
+
+static int print_frac (char *s, int leftalign, u_long numerator, u_long divider)
+{
+ int n, length = 0;
+
+ if (numerator < 1 || divider < 1) {
+ length += sprintf (s+length, leftalign ? "/- " : " -");
+ return length;
+ }
+ n = (int) (0.5 + 1000.0 * numerator / divider);
+ if (n < 1000) {
+ length += sprintf (s+length, leftalign ? "/.%-3d" : " .%03d", n);
+ return length;
+ }
+ *(s + length) = leftalign ? '/' : ' ';
+ length ++;
+
+ if (n >= 1000000) n = (n+500) / 1000 * 1000;
+ else if (n >= 100000) n = (n+50) / 100 * 100;
+ else if (n >= 10000) n = (n+5) / 10 * 10;
+
+ switch (n) {
+ case 1000: length += printf (s+length, ".999"); return length;
+ case 10000: n = 9990; break;
+ case 100000: n = 99900; break;
+ case 1000000: n = 999000; break;
+ }
+ if (n < 10000) length += sprintf (s+length, "%d.%d", n/1000, n/10%100);
+ else if (n < 100000) length += sprintf (s+length, "%d.%d", n/1000, n/100%10);
+ else if (n < 1000000) length += sprintf (s+length, "%d.", n/1000);
+ else length += sprintf (s+length, "%d", n/1000);
+
+ return length;
+}
+
+static int print_e1_stats (char *s, ct_chan_t *c)
+{
+ struct e1_counters total;
+ u_long totsec;
+ int length = 0;
+
+ totsec = c->totsec + c->cursec;
+ total.bpv = c->total.bpv + c->currnt.bpv;
+ total.fse = c->total.fse + c->currnt.fse;
+ total.crce = c->total.crce + c->currnt.crce;
+ total.rcrce = c->total.rcrce + c->currnt.rcrce;
+ total.uas = c->total.uas + c->currnt.uas;
+ total.les = c->total.les + c->currnt.les;
+ total.es = c->total.es + c->currnt.es;
+ total.bes = c->total.bes + c->currnt.bes;
+ total.ses = c->total.ses + c->currnt.ses;
+ total.oofs = c->total.oofs + c->currnt.oofs;
+ total.css = c->total.css + c->currnt.css;
+ total.dm = c->total.dm + c->currnt.dm;
+
+ length += sprintf (s + length, " Unav/Degr Bpv/Fsyn CRC/RCRC Err/Lerr Sev/Bur Oof/Slp Status\n");
+
+ /* Unavailable seconds, degraded minutes */
+ length += print_frac (s + length, 0, c->currnt.uas, c->cursec);
+ length += print_frac (s + length, 1, 60 * c->currnt.dm, c->cursec);
+
+ /* Bipolar violations, frame sync errors */
+ length += print_frac (s + length, 0, c->currnt.bpv, c->cursec);
+ length += print_frac (s + length, 1, c->currnt.fse, c->cursec);
+
+ /* CRC errors, remote CRC errors (E-bit) */
+ length += print_frac (s + length, 0, c->currnt.crce, c->cursec);
+ length += print_frac (s + length, 1, c->currnt.rcrce, c->cursec);
+
+ /* Errored seconds, line errored seconds */
+ length += print_frac (s + length, 0, c->currnt.es, c->cursec);
+ length += print_frac (s + length, 1, c->currnt.les, c->cursec);
+
+ /* Severely errored seconds, burst errored seconds */
+ length += print_frac (s + length, 0, c->currnt.ses, c->cursec);
+ length += print_frac (s + length, 1, c->currnt.bes, c->cursec);
+
+ /* Out of frame seconds, controlled slip seconds */
+ length += print_frac (s + length, 0, c->currnt.oofs, c->cursec);
+ length += print_frac (s + length, 1, c->currnt.css, c->cursec);
+
+ length += sprintf (s + length, " %s\n", format_e1_status (c->status));
+
+ /* Print total statistics. */
+ length += print_frac (s + length, 0, total.uas, totsec);
+ length += print_frac (s + length, 1, 60 * total.dm, totsec);
+
+ length += print_frac (s + length, 0, total.bpv, totsec);
+ length += print_frac (s + length, 1, total.fse, totsec);
+
+ length += print_frac (s + length, 0, total.crce, totsec);
+ length += print_frac (s + length, 1, total.rcrce, totsec);
+
+ length += print_frac (s + length, 0, total.es, totsec);
+ length += print_frac (s + length, 1, total.les, totsec);
+
+ length += print_frac (s + length, 0, total.ses, totsec);
+ length += print_frac (s + length, 1, total.bes, totsec);
+
+ length += print_frac (s + length, 0, total.oofs, totsec);
+ length += print_frac (s + length, 1, total.css, totsec);
+
+ length += sprintf (s + length, " -- Total\n");
+ return length;
+}
+
+static int print_chan (char *s, ct_chan_t *c)
+{
+ drv_t *d = c->sys;
+ int length = 0;
+
+ length += sprintf (s + length, "ct%d", c->board->num * NCHAN + c->num);
+ if (d->chan->debug)
+ length += sprintf (s + length, " debug=%d", d->chan->debug);
+
+ switch (ct_get_config (c->board)) {
+ case CFG_A: length += sprintf (s + length, " cfg=A"); break;
+ case CFG_B: length += sprintf (s + length, " cfg=B"); break;
+ case CFG_C: length += sprintf (s + length, " cfg=C"); break;
+ default: length += sprintf (s + length, " cfg=unknown"); break;
+ }
+
+ if (ct_get_baud (c))
+ length += sprintf (s + length, " %ld", ct_get_baud (c));
+ else
+ length += sprintf (s + length, " extclock");
+
+ if (c->mode == M_E1 || c->mode == M_G703)
+ switch (ct_get_clk(c)) {
+ case GCLK_INT : length += sprintf (s + length, " syn=int"); break;
+ case GCLK_RCV : length += sprintf (s + length, " syn=rcv"); break;
+ case GCLK_RCLKO : length += sprintf (s + length, " syn=xrcv"); break;
+ }
+ if (c->mode == M_HDLC) {
+ length += sprintf (s + length, " dpll=%s", ct_get_dpll (c) ? "on" : "off");
+ length += sprintf (s + length, " nrzi=%s", ct_get_nrzi (c) ? "on" : "off");
+ length += sprintf (s + length, " invtclk=%s", ct_get_invtxc (c) ? "on" : "off");
+ length += sprintf (s + length, " invrclk=%s", ct_get_invrxc (c) ? "on" : "off");
+ }
+ if (c->mode == M_E1)
+ length += sprintf (s + length, " higain=%s", ct_get_higain (c)? "on" : "off");
+
+ length += sprintf (s + length, " loop=%s", ct_get_loop (c) ? "on" : "off");
+
+ if (c->mode == M_E1)
+ length += sprintf (s + length, " ts=%s", format_timeslots (ct_get_ts(c)));
+ if (c->mode == M_E1 && ct_get_config (c->board) != CFG_A)
+ length += sprintf (s + length, " pass=%s", format_timeslots (ct_get_subchan(c->board)));
+ if (c->mode == M_G703) {
+ int lq, x;
+
+ x = splimp ();
+ lq = ct_get_lq (c);
+ splx (x);
+ length += sprintf (s + length, " (level=-%.1fdB)", lq / 10.0);
+ }
+ length += sprintf (s + length, "\n");
+ return length;
+}
+
+#if __FreeBSD_version >= 500000
+static int ng_ct_rcvmsg (node_p node, item_p item, hook_p lasthook)
+{
+ drv_t *d = NG_NODE_PRIVATE (node);
+ struct ng_mesg *msg;
+#else
+static int ng_ct_rcvmsg (node_p node, struct ng_mesg *msg,
+ const char *retaddr, struct ng_mesg **rptr)
+{
+ drv_t *d = node->private;
+#endif
+ struct ng_mesg *resp = NULL;
+ int error = 0;
+
+ if (!d)
+ return EINVAL;
+
+ CT_DEBUG (d, ("Rcvmsg\n"));
+#if __FreeBSD_version >= 500000
+ NGI_GET_MSG (item, msg);
+#endif
+ switch (msg->header.typecookie) {
+ default:
+ error = EINVAL;
+ break;
+
+ case NGM_CT_COOKIE:
+ printf ("Don't forget to implement\n");
+ error = EINVAL;
+ break;
+
+ case NGM_GENERIC_COOKIE:
+ switch (msg->header.cmd) {
+ default:
+ error = EINVAL;
+ break;
+
+ case NGM_TEXT_STATUS: {
+ char *s;
+ int l = 0;
+ int dl = sizeof (struct ng_mesg) + 730;
+
+#if __FreeBSD_version >= 500000
+ NG_MKRESPONSE (resp, msg, dl, M_NOWAIT);
+ if (! resp) {
+ error = ENOMEM;
+ break;
+ }
+#else
+ MALLOC (resp, struct ng_mesg *, dl,
+ M_NETGRAPH, M_NOWAIT);
+ if (! resp) {
+ error = ENOMEM;
+ break;
+ }
+ bzero (resp, dl);
+#endif
+ s = (resp)->data;
+ l += print_chan (s + l, d->chan);
+ l += print_stats (s + l, d->chan, 1);
+ l += print_modems (s + l, d->chan, 1);
+ l += print_e1_stats (s + l, d->chan);
+#if __FreeBSD_version < 500000
+ (resp)->header.version = NG_VERSION;
+ (resp)->header.arglen = strlen (s) + 1;
+ (resp)->header.token = msg->header.token;
+ (resp)->header.typecookie = NGM_CT_COOKIE;
+ (resp)->header.cmd = msg->header.cmd;
+#endif
+ strncpy ((resp)->header.cmdstr, "status", NG_CMDSTRLEN);
+ }
+ break;
+ }
+ break;
+ }
+#if __FreeBSD_version >= 500000
+ NG_RESPOND_MSG (error, node, item, resp);
+ NG_FREE_MSG (msg);
+#else
+ *rptr = resp;
+ FREE (msg, M_NETGRAPH);
+#endif
+ return error;
+}
+
+#if __FreeBSD_version >= 500000
+static int ng_ct_rcvdata (hook_p hook, item_p item)
+{
+ drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE(hook));
+ struct mbuf *m;
+ meta_p meta;
+#else
+static int ng_ct_rcvdata (hook_p hook, struct mbuf *m, meta_p meta)
+{
+ drv_t *d = hook->node->private;
+#endif
+ struct ifqueue *q;
+ int s;
+
+ if (!d)
+ return ENETDOWN;
+
+#if __FreeBSD_version >= 500000
+ NGI_GET_M (item, m);
+ NGI_GET_META (item, meta);
+ NG_FREE_ITEM (item);
+ if (! NG_HOOK_PRIVATE (hook) || ! d) {
+ NG_FREE_M (m);
+ NG_FREE_META (meta);
+#else
+ if (! hook->private || ! d) {
+ NG_FREE_DATA (m,meta);
+#endif
+ return ENETDOWN;
+ }
+ q = (meta && meta->priority > 0) ? &d->hi_queue : &d->queue;
+ s = splimp ();
+#if __FreeBSD_version >= 500000
+ IF_LOCK (q);
+ if (_IF_QFULL (q)) {
+ _IF_DROP (q);
+ IF_UNLOCK (q);
+ splx (s);
+ NG_FREE_M (m);
+ NG_FREE_META (meta);
+ return ENOBUFS;
+ }
+ _IF_ENQUEUE (q, m);
+ IF_UNLOCK (q);
+#else
+ if (IF_QFULL (q)) {
+ IF_DROP (q);
+ splx (s);
+ NG_FREE_DATA (m, meta);
+ return ENOBUFS;
+ }
+ IF_ENQUEUE (q, m);
+#endif
+ ct_start (d);
+ splx (s);
+ return 0;
+}
+
+static int ng_ct_rmnode (node_p node)
+{
+#if __FreeBSD_version >= 500000
+ drv_t *d = NG_NODE_PRIVATE (node);
+
+ CT_DEBUG (d, ("Rmnode\n"));
+ if (d && d->running) {
+ int s = splimp ();
+ ct_down (d);
+ splx (s);
+ }
+#ifdef KLD_MODULE
+ if (node->nd_flags & NG_REALLY_DIE) {
+ NG_NODE_SET_PRIVATE (node, NULL);
+ NG_NODE_UNREF (node);
+ }
+ node->nd_flags &= ~NG_INVALID;
+#endif
+#else /* __FreeBSD_version < 500000 */
+ drv_t *d = node->private;
+ int s;
+
+ if (!d)
+ return 0;
+
+ s = splimp ();
+ ct_down (d);
+ splx (s);
+ node->flags |= NG_INVALID;
+ ng_cutlinks (node);
+#ifdef KLD_MODULE
+ ng_unname (node);
+ ng_unref (node);
+#else
+ node->flags &= ~NG_INVALID;
+#endif
+#endif
+ return 0;
+}
+
+static void ng_ct_watchdog (void *arg)
+{
+ drv_t *d = arg;
+
+ if (!d)
+ return;
+
+ if (d->timeout == 1)
+ ct_watchdog (d);
+ if (d->timeout)
+ d->timeout--;
+ d->timeout_handle = timeout (ng_ct_watchdog, d, hz);
+}
+
+static int ng_ct_connect (hook_p hook)
+{
+#if __FreeBSD_version >= 500000
+ drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
+#else
+ drv_t *d = hook->node->private;
+#endif
+
+ if (!d)
+ return 0;
+
+ d->timeout_handle = timeout (ng_ct_watchdog, d, hz);
+ return 0;
+}
+
+static int ng_ct_disconnect (hook_p hook)
+{
+#if __FreeBSD_version >= 500000
+ drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
+#else
+ drv_t *d = hook->node->private;
+#endif
+
+ if (!d)
+ return 0;
+
+#if __FreeBSD_version >= 500000
+ if (NG_HOOK_PRIVATE (hook))
+#else
+ if (hook->private)
+#endif
+ ct_down (d);
+ untimeout (ng_ct_watchdog, d, d->timeout_handle);
+ return 0;
+}
+#endif
+
+#ifdef KLD_MODULE
+#if __FreeBSD_version < 400000
+/*
+ * Function called when loading the driver.
+ */
+static int ct_load (void)
+{
+ int i;
+
+ for (i=0;i<NCTAU; ++i) {
+ struct isa_device id = {-1, &ctdriver, -1, 0, -1, 0, 0, (inthand2_t *)ct_intr, i, 0, 0, 0, 0 ,0 ,1 ,0 ,0};
+ disable_intr();
+ if (!ct_probe (&id)) {
+ enable_intr();
+ break;
+ }
+ ct_attach (&id);
+ register_intr ((adapter [i])->irq, 0, 0, (inthand2_t*) ct_intr,
+ &net_imask, id.id_unit);
+ enable_intr();
+ }
+ if (!i) {
+ /* Deactivate the timeout routine. */
+ untimeout (ct_timeout, 0, timeout_handle);
+
+ return ENXIO;
+ }
+ return 0;
+}
+
+/*
+ * Function called when unloading the driver.
+ */
+static int ct_unload (void)
+{
+ int i, s;
+
+ /* Check if the device is busy (open). */
+ for (i=0; i<NCTAU*NCHAN; ++i) {
+ drv_t *d = channel[i];
+
+ if (!d)
+ continue;
+ if (d->running)
+ return EBUSY;
+ }
+
+ /* OK to unload the driver, unregister the interrupt first. */
+ s = splimp ();
+
+ /* Deactivate the timeout routine. */
+ for (i=0; i<NCTAU; ++i) {
+ if (!adapter [i])
+ continue;
+ untimeout (ct_timeout, 0, timeout_handle);
+ break;
+ }
+
+ for (i=0; i<NCTAU; ++i) {
+ ct_board_t *b = adapter [i];
+
+ if (!b || ! b->port)
+ continue;
+
+ ct_close_board (b);
+ }
+
+ for (i=0; i<NCTAU; ++i) {
+ ct_board_t *b = adapter [i];
+
+ if (!b || ! b->port)
+ continue;
+
+ if (led_timo[i].callout)
+ untimeout (ct_led_off, b, led_timo[i]);
+ }
+
+ for (i=0; i<NCTAU; ++i) {
+ ct_board_t *b = adapter [i];
+
+ if (!b || ! b->port)
+ continue;
+
+ /* Disable the interrupt request. */
+ disable_intr();
+ unregister_intr (b->irq, (inthand2_t *)ct_intr);
+ isa_dma_release (b->dma);
+ enable_intr();
+ }
+
+ /* Detach the interfaces, free buffer memory. */
+ for (i=0; i<NCTAU*NCHAN; ++i) {
+ drv_t *d = channel[i];
+
+ if (!d)
+ continue;
+
+#ifndef NETGRAPH
+#if NBPFILTER > 0
+ /* Detach from the packet filter list of interfaces. */
+ {
+ struct bpf_if *q, **b = &bpf_iflist;
+
+ while ((q = *b)) {
+ if (q->bif_ifp == d->pp.pp_if) {
+ *b = q->bif_next;
+ free (q, M_DEVBUF);
+ }
+ b = &(q->bif_next);
+ }
+ }
+#endif /* NBPFILTER > 0 */
+ /* Detach from the sync PPP list. */
+ sppp_detach (&d->pp.pp_if);
+
+ /* Detach from the system list of interfaces. */
+ {
+ struct ifaddr *ifa;
+ TAILQ_FOREACH (ifa, &d->pp.pp_if.if_addrhead, ifa_link) {
+ TAILQ_REMOVE (&d->pp.pp_if.if_addrhead, ifa, ifa_link);
+ free (ifa, M_IFADDR);
+ }
+ TAILQ_REMOVE (&ifnet, &d->pp.pp_if, if_link);
+ }
+#endif /* !NETGRAPH */
+ /* Deallocate buffers. */
+/* free (d, M_DEVBUF);*/
+ }
+ for (i=0; i<NCTAU; ++i) {
+ ct_board_t *b = adapter [i];
+ if (!b)
+ continue;
+ adapter [i] = 0;
+ free (b, M_DEVBUF);
+ }
+ splx(s);
+
+ return 0;
+}
+
+#define devsw(a) cdevsw[major((a))]
+#endif /* __FreeBSD_version < 400000 */
+#endif /* KLD_MODULE */
+
+#if __FreeBSD_version < 400000
+#ifdef KLD_MODULE
+static int ct_modevent (module_t mod, int type, void *unused)
+{
+ dev_t dev;
+ int result;
+ static int load_count = 0;
+
+ dev = makedev (CDEV_MAJOR, 0);
+ switch (type) {
+ case MOD_LOAD:
+ if (devsw(dev))
+ return (ENXIO);
+ load_count ++;
+ cdevsw_add (&dev, &ct_cdevsw, NULL);
+ timeout_handle = timeout (ct_timeout, 0, hz*5);
+ result = ct_load ();
+ return result;
+ case MOD_UNLOAD:
+ result = ct_unload ();
+ if (result)
+ return result;
+ if (devsw(dev)&&!(load_count-1)) {
+ cdevsw_add (&dev, NULL, NULL);
+ }
+ load_count --;
+ return result;
+ case MOD_SHUTDOWN:
+ break;
+ }
+ return 0;
+}
+#endif /* KLD_MODULE */
+#else /* __FreeBSD_version >= 400000 */
+static int ct_modevent (module_t mod, int type, void *unused)
+{
+ dev_t dev;
+ static int load_count = 0;
+ struct cdevsw *cdsw;
+
+#if __FreeBSD_version >= 502103
+ dev = udev2dev (makeudev(CDEV_MAJOR, 0));
+#else
+ dev = makedev (CDEV_MAJOR, 0);
+#endif
+ switch (type) {
+ case MOD_LOAD:
+ if (dev != NODEV &&
+ (cdsw = devsw (dev)) &&
+ cdsw->d_maj == CDEV_MAJOR) {
+ printf ("Tau-ISA driver is already in system\n");
+ return (ENXIO);
+ }
+#if __FreeBSD_version >= 500000 && defined NETGRAPH
+ if (ng_newtype (&typestruct))
+ printf ("Failed to register ng_ct\n");
+#endif
+ ++load_count;
+#if __FreeBSD_version <= 500000
+ cdevsw_add (&ct_cdevsw);
+#endif
+ timeout_handle = timeout (ct_timeout, 0, hz*5);
+ break;
+ case MOD_UNLOAD:
+ if (load_count == 1) {
+ printf ("Removing device entry for Tau-ISA\n");
+#if __FreeBSD_version <= 500000
+ cdevsw_remove (&ct_cdevsw);
+#endif
+#if __FreeBSD_version >= 500000 && defined NETGRAPH
+ ng_rmtype (&typestruct);
+#endif
+ }
+ if (timeout_handle.callout)
+ untimeout (ct_timeout, 0, timeout_handle);
+ --load_count;
+ break;
+ case MOD_SHUTDOWN:
+ break;
+ }
+ return 0;
+}
+#endif /* __FreeBSD_version >= 400000 */
+
+#ifdef NETGRAPH
+static struct ng_type typestruct = {
+#if __FreeBSD_version >= 500000
+ NG_ABI_VERSION,
+#else
+ NG_VERSION,
+#endif
+ NG_CT_NODE_TYPE,
+#if __FreeBSD_version < 500000 && (defined KLD_MODULE)
+ ct_modevent,
+#else
+ NULL,
+#endif
+ ng_ct_constructor,
+ ng_ct_rcvmsg,
+ ng_ct_rmnode,
+ ng_ct_newhook,
+ NULL,
+ ng_ct_connect,
+ ng_ct_rcvdata,
+#if __FreeBSD_version < 500000
+ NULL,
+#endif
+ ng_ct_disconnect
+};
+
+#if __FreeBSD_version < 400000
+NETGRAPH_INIT_ORDERED (ct, &typestruct, SI_SUB_DRIVERS,\
+ SI_ORDER_MIDDLE + CDEV_MAJOR);
+#endif
+#endif /*NETGRAPH*/
+
+#if __FreeBSD_version >= 500000
+#ifdef NETGRAPH
+MODULE_DEPEND (ng_ct, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION);
+#else
+MODULE_DEPEND (ct, sppp, 1, 1, 1);
+#endif
+#ifdef KLD_MODULE
+DRIVER_MODULE (ctmod, isa, ct_isa_driver, ct_devclass, ct_modevent, NULL);
+#else
+DRIVER_MODULE (ct, isa, ct_isa_driver, ct_devclass, ct_modevent, NULL);
+#endif
+#elif __FreeBSD_version >= 400000
+#ifdef NETGRAPH
+DRIVER_MODULE(ct, isa, ct_isa_driver, ct_devclass, ng_mod_event, &typestruct);
+#else
+DRIVER_MODULE(ct, isa, ct_isa_driver, ct_devclass, ct_modevent, 0);
+#endif
+#else /* __FreeBSD_version < 400000 */
+#ifdef KLD_MODULE
+#ifndef NETGRAPH
+static moduledata_t ctmod = { "ct", ct_modevent, };
+DECLARE_MODULE (ct, ctmod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR);
+#endif /* !NETGRAPH */
+#else /* KLD_MODULE */
+
+/*
+ * Now for some driver initialisation.
+ * Occurs ONCE during boot (very early).
+ * This is if we are NOT a loadable module.
+ */
+static void ct_drvinit (void *unused)
+{
+ dev_t dev;
+
+ dev = makedev (CDEV_MAJOR, 0);
+ cdevsw_add (&dev, &ct_cdevsw, NULL);
+
+ /* Activate the timeout routine. */
+ timeout_handle = timeout (ct_timeout, 0, hz);
+#ifdef NETGRAPH
+#if 0
+ /* Register our node type in netgraph */
+ if (ng_newtype (&typestruct))
+ printf ("Failed to register ng_ct\n");
+#endif
+#endif
+}
+
+SYSINIT (ctdev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE+CDEV_MAJOR, ct_drvinit, 0)
+#endif /* KLD_MODULE */
+#endif /* __FreeBSD_version < 400000 */
+#endif /* NCTAU */
diff --git a/sys/dev/cx/if_cx.c b/sys/dev/cx/if_cx.c
new file mode 100644
index 000000000000..c287da2b4f8d
--- /dev/null
+++ b/sys/dev/cx/if_cx.c
@@ -0,0 +1,3270 @@
+/*
+ * Cronyx-Sigma adapter driver for FreeBSD.
+ * Supports PPP/HDLC and Cisco/HDLC protocol in synchronous mode,
+ * and asyncronous channels with full modem control.
+ * Keepalive protocol implemented in both Cisco and PPP modes.
+ *
+ * Copyright (C) 1994-2002 Cronyx Engineering.
+ * Author: Serge Vakulenko, <vak@cronyx.ru>
+ *
+ * Copyright (C) 1999-2004 Cronyx Engineering.
+ * Rewritten on DDK, ported to NETGRAPH, rewritten for FreeBSD 3.x-5.x by
+ * Kurakin Roman, <rik@cronyx.ru>
+ *
+ * This software is distributed with NO WARRANTIES, not even the implied
+ * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * Authors grant any other persons or organisations a permission to use,
+ * modify and redistribute this software in source and binary forms,
+ * as long as this message is kept with the software, all derivative
+ * works or modified versions.
+ *
+ * Cronyx Id: if_cx.c,v 1.1.2.23 2004/02/26 17:56:40 rik Exp $
+ */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/param.h>
+
+#if __FreeBSD_version >= 500000
+# define NCX 1
+#else
+# include "cx.h"
+#endif
+
+#if NCX > 0
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/proc.h>
+#include <sys/mbuf.h>
+#include <sys/sockio.h>
+#include <sys/malloc.h>
+#include <sys/socket.h>
+#include <sys/conf.h>
+#include <sys/errno.h>
+#include <sys/tty.h>
+#if __FreeBSD_version >= 400000
+# include <sys/bus.h>
+# include <machine/bus.h>
+# include <sys/rman.h>
+# include <isa/isavar.h>
+#endif
+#include <sys/fcntl.h>
+#include <sys/interrupt.h>
+#include <vm/vm.h>
+#include <vm/pmap.h>
+#include <net/if.h>
+#include <machine/cpufunc.h>
+#include <machine/cserial.h>
+#include <machine/clock.h>
+#if __FreeBSD_version < 500000
+#include <machine/ipl.h>
+#include <i386/isa/isa_device.h>
+#endif
+#if __FreeBSD_version >= 400000
+# include <machine/resource.h>
+# if __FreeBSD_version <= 501000
+# include <i386/isa/intr_machdep.h>
+# endif
+#endif
+#if __FreeBSD_version >= 500000
+# include <dev/cx/machdep.h>
+# include <dev/cx/cxddk.h>
+# include <dev/cx/cronyxfw.h>
+#else
+# include <i386/isa/cronyx/machdep.h>
+# include <i386/isa/cronyx/cxddk.h>
+# include <i386/isa/cronyx/cronyxfw.h>
+#endif
+#include "opt_ng_cronyx.h"
+#ifdef NETGRAPH_CRONYX
+# include "opt_netgraph.h"
+# include <netgraph/ng_message.h>
+# include <netgraph/netgraph.h>
+# if __FreeBSD_version >= 500000
+# include <dev/cx/ng_cx.h>
+# else
+# include <netgraph/ng_cx.h>
+# endif
+#else
+# include <net/if_types.h>
+# if __FreeBSD_version < 500000
+# include "sppp.h"
+# if NSPPP <= 0
+# error The device cp requires sppp or netgraph.
+# endif
+# endif
+# include <net/if_sppp.h>
+# define PP_CISCO IFF_LINK2
+#if __FreeBSD_version < 400000
+# include <bpfilter.h>
+# if NBPFILTER > 0
+# include <net/bpf.h>
+# endif
+#else
+# if __FreeBSD_version < 500000
+# include <bpf.h>
+# endif
+# include <net/bpf.h>
+# define NBPFILTER NBPF
+#endif
+#endif
+
+#define CX_DEBUG(d,s) ({if (d->chan->debug) {\
+ printf ("%s: ", d->name); printf s;}})
+#define CX_DEBUG2(d,s) ({if (d->chan->debug>1) {\
+ printf ("%s: ", d->name); printf s;}})
+
+#define UNIT(d) (minor(d) & 0x3f)
+#define IF_CUNIT(d) (minor(d) & 0x40)
+#define UNIT_CTL 0x3f
+#define CALLOUT(d) (minor(d) & 0x80)
+#define CDEV_MAJOR 42
+
+typedef struct _async_q {
+ int beg;
+ int end;
+ #define BF_SZ 14400
+ int buf[BF_SZ+1];
+} async_q;
+
+#define AQ_GSZ(q) ((BF_SZ + (q)->end - (q)->beg)%BF_SZ)
+#define AQ_PUSH(q,c) {*((q)->buf + (q)->end) = c;\
+ (q)->end = ((q)->end + 1)%BF_SZ;}
+#define AQ_POP(q,c) {c = *((q)->buf + (q)->beg);\
+ (q)->beg = ((q)->beg + 1)%BF_SZ;}
+
+#if __FreeBSD_version >= 400000
+static void cx_identify __P((driver_t *, device_t));
+static int cx_probe __P((device_t));
+static int cx_attach __P((device_t));
+static int cx_detach __P((device_t));
+
+static device_method_t cx_isa_methods [] = {
+ DEVMETHOD(device_identify, cx_identify),
+ DEVMETHOD(device_probe, cx_probe),
+ DEVMETHOD(device_attach, cx_attach),
+ DEVMETHOD(device_detach, cx_detach),
+ {0, 0}
+};
+
+typedef struct _bdrv_t {
+ cx_board_t *board;
+ struct resource *base_res;
+ struct resource *drq_res;
+ struct resource *irq_res;
+ int base_rid;
+ int drq_rid;
+ int irq_rid;
+ void *intrhand;
+} bdrv_t;
+
+static driver_t cx_isa_driver = {
+ "cx",
+ cx_isa_methods,
+ sizeof (bdrv_t),
+};
+
+static devclass_t cx_devclass;
+#endif
+
+typedef struct _drv_t {
+ char name [8];
+ cx_chan_t *chan;
+ cx_board_t *board;
+ cx_buf_t buf;
+ struct tty tty;
+ struct callout_handle dcd_timeout_handle;
+ unsigned dtrwait;
+ unsigned dtroff;
+ unsigned callout;
+ unsigned lock;
+ int open_dev;
+ int cd;
+ int running;
+ struct callout_handle dtr_timeout_handle;
+#ifdef NETGRAPH
+ char nodename [NG_NODELEN+1];
+ hook_p hook;
+ hook_p debug_hook;
+ node_p node;
+ struct ifqueue lo_queue;
+ struct ifqueue hi_queue;
+ short timeout;
+ struct callout_handle timeout_handle;
+#else
+ struct sppp pp;
+#endif
+#if __FreeBSD_version >= 400000
+ dev_t devt[3];
+#endif
+ async_q aqueue;
+ #define CX_READ 1
+ #define CX_WRITE 2
+ int intr_action;
+ short atimeout;
+} drv_t;
+
+extern long csigma_fw_len;
+extern const char *csigma_fw_version;
+extern const char *csigma_fw_date;
+extern const char *csigma_fw_copyright;
+extern const cr_dat_tst_t csigma_fw_tvec[];
+extern const u_char csigma_fw_data[];
+static void cx_oproc (struct tty *tp);
+static int cx_param (struct tty *tp, struct termios *t);
+static void cx_stop (struct tty *tp, int flag);
+static void cx_dtrwakeup (void *a);
+static void cx_receive (cx_chan_t *c, char *data, int len);
+static void cx_transmit (cx_chan_t *c, void *attachment, int len);
+static void cx_error (cx_chan_t *c, int data);
+static void cx_modem (cx_chan_t *c);
+static void cx_up (drv_t *d);
+static void cx_start (drv_t *d);
+static void disc_optim(struct tty *tp, struct termios *t);
+#if __FreeBSD_version < 500000
+static swihand_t cx_softintr;
+#else
+static void cx_softintr (void *);
+static void *cx_slow_ih;
+static void *cx_fast_ih;
+#endif
+static void cx_down (drv_t *d);
+static void cx_watchdog (drv_t *d);
+static void cx_carrier (void *arg);
+
+#ifdef NETGRAPH
+extern struct ng_type typestruct;
+#else
+static void cx_ifstart (struct ifnet *ifp);
+static void cx_tlf (struct sppp *sp);
+static void cx_tls (struct sppp *sp);
+static void cx_ifwatchdog (struct ifnet *ifp);
+static int cx_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data);
+static void cx_initialize (void *softc);
+#endif
+
+static cx_board_t *adapter [NCX];
+static drv_t *channel [NCX*NCHAN];
+static struct callout_handle led_timo [NCX];
+static struct callout_handle timeout_handle;
+#if __FreeBSD_version >= 400000
+ extern struct cdevsw cx_cdevsw;
+#endif
+
+static int MY_SOFT_INTR;
+
+/*
+ * Print the mbuf chain, for debug purposes only.
+ */
+static void printmbuf (struct mbuf *m)
+{
+ printf ("mbuf:");
+ for (; m; m=m->m_next) {
+ if (m->m_flags & M_PKTHDR)
+ printf (" HDR %d:", m->m_pkthdr.len);
+ if (m->m_flags & M_EXT)
+ printf (" EXT:");
+ printf (" %d", m->m_len);
+ }
+ printf ("\n");
+}
+
+/*
+ * Make an mbuf from data.
+ */
+static struct mbuf *makembuf (void *buf, u_int len)
+{
+ struct mbuf *m, *o, *p;
+
+ MGETHDR (m, M_DONTWAIT, MT_DATA);
+
+ if (! m)
+ return 0;
+
+ if (len >= MINCLSIZE)
+ MCLGET (m, M_DONTWAIT);
+
+ m->m_pkthdr.len = len;
+ m->m_len = 0;
+
+ p = m;
+ while (len) {
+ u_int n = M_TRAILINGSPACE (p);
+ if (n > len)
+ n = len;
+ if (! n) {
+ /* Allocate new mbuf. */
+ o = p;
+ MGET (p, M_DONTWAIT, MT_DATA);
+ if (! p) {
+ m_freem (m);
+ return 0;
+ }
+ if (len >= MINCLSIZE)
+ MCLGET (p, M_DONTWAIT);
+ p->m_len = 0;
+ o->m_next = p;
+
+ n = M_TRAILINGSPACE (p);
+ if (n > len)
+ n = len;
+ }
+ bcopy (buf, mtod (p, caddr_t) + p->m_len, n);
+
+ p->m_len += n;
+ buf = n + (char*) buf;
+ len -= n;
+ }
+ return m;
+}
+
+/*
+ * Recover after lost transmit interrupts.
+ */
+static void cx_timeout (void *arg)
+{
+ drv_t *d;
+ int s, i;
+
+ for (i=0; i<NCX*NCHAN; ++i) {
+ d = channel[i];
+ if (! d)
+ continue;
+ s = splhigh ();
+ if (d->atimeout == 1 && d->tty.t_state & TS_BUSY) {
+ d->tty.t_state &= ~TS_BUSY;
+ if (d->tty.t_dev) {
+ d->intr_action |= CX_WRITE;
+ MY_SOFT_INTR = 1;
+#if __FreeBSD_version >= 500000
+ swi_sched (cx_fast_ih, 0);
+#else
+ setsofttty ();
+#endif
+ }
+ CX_DEBUG (d, ("cx_timeout\n"));
+ }
+ if (d->atimeout)
+ d->atimeout--;
+ splx (s);
+ }
+ timeout_handle = timeout (cx_timeout, 0, hz*5);
+}
+
+static void cx_led_off (void *arg)
+{
+ cx_board_t *b = arg;
+ int s = splhigh ();
+
+ cx_led (b, 0);
+ led_timo[b->num].callout = 0;
+ splx (s);
+}
+
+/*
+ * Activate interupt handler from DDK.
+ */
+#if __FreeBSD_version >= 400000
+static void cx_intr (void *arg)
+{
+ bdrv_t *bd = arg;
+ cx_board_t *b = bd->board;
+#else
+static void cx_intr (int bnum)
+{
+ cx_board_t *b = adapter [bnum];
+#endif
+ int s = splhigh ();
+
+ /* Turn LED on. */
+ cx_led (b, 1);
+
+ cx_int_handler (b);
+
+ /* Turn LED off 50 msec later. */
+ if (! led_timo[b->num].callout)
+ led_timo[b->num] = timeout (cx_led_off, b, hz/20);
+ splx (s);
+}
+
+static int probe_irq (cx_board_t *b, int irq)
+{
+ int mask, busy, cnt;
+
+ /* Clear pending irq, if any. */
+ cx_probe_irq (b, -irq);
+ DELAY (100);
+ for (cnt=0; cnt<5; ++cnt) {
+ /* Get the mask of pending irqs, assuming they are busy.
+ * Activate the adapter on given irq. */
+ busy = cx_probe_irq (b, irq);
+ DELAY (100);
+
+ /* Get the mask of active irqs.
+ * Deactivate our irq. */
+ mask = cx_probe_irq (b, -irq);
+ DELAY (100);
+ if ((mask & ~busy) == 1 << irq) {
+ cx_probe_irq (b, 0);
+ /* printf ("cx%d: irq %d ok, mask=0x%04x, busy=0x%04x\n",
+ b->num, irq, mask, busy); */
+ return 1;
+ }
+ }
+ /* printf ("cx%d: irq %d not functional, mask=0x%04x, busy=0x%04x\n",
+ b->num, irq, mask, busy); */
+ cx_probe_irq (b, 0);
+ return 0;
+}
+
+static short porttab [] = {
+ 0x200, 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0,
+ 0x300, 0x320, 0x340, 0x360, 0x380, 0x3a0, 0x3c0, 0x3e0, 0
+};
+static char dmatab [] = { 7, 6, 5, 0 };
+static char irqtab [] = { 5, 10, 11, 7, 3, 15, 12, 0 };
+
+#if __FreeBSD_version >= 400000
+static int cx_is_free_res (device_t dev, int rid, int type, u_long start,
+ u_long end, u_long count)
+{
+ struct resource *res;
+
+ if (!(res = bus_alloc_resource (dev, type, &rid, start, end, count,
+ RF_ALLOCATED)))
+ return 0;
+
+ bus_release_resource (dev, type, rid, res);
+
+ return 1;
+}
+
+static void cx_identify (driver_t *driver, device_t dev)
+{
+ u_long iobase, rescount;
+ int devcount;
+ device_t *devices;
+ device_t child;
+ devclass_t my_devclass;
+ int i, k;
+
+ if ((my_devclass = devclass_find ("cx")) == NULL)
+ return;
+
+ devclass_get_devices (my_devclass, &devices, &devcount);
+
+ if (devcount == 0) {
+ /* We should find all devices by our self. We could alter other
+ * devices, but we don't have a choise
+ */
+ for (i = 0; (iobase = porttab [i]) != 0; i++) {
+ if (!cx_is_free_res (dev, 1, SYS_RES_IOPORT,
+ iobase, iobase + NPORT, NPORT))
+ continue;
+ if (cx_probe_board (iobase, -1, -1) == 0)
+ continue;
+
+ devcount++;
+
+ child = BUS_ADD_CHILD (dev, ISA_ORDER_SPECULATIVE, "cx",
+ -1);
+
+ if (child == NULL)
+ return;
+
+ device_set_desc_copy (child, "Cronyx Sigma");
+ device_set_driver (child, driver);
+ bus_set_resource (child, SYS_RES_IOPORT, 0,
+ iobase, NPORT);
+
+ if (devcount >= NCX)
+ break;
+ }
+ } else {
+ static short porttab [] = {
+ 0x200, 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0,
+ 0x300, 0x320, 0x340, 0x360, 0x380, 0x3a0, 0x3c0, 0x3e0, 0
+ };
+ /* Lets check user choise.
+ */
+ for (k = 0; k < devcount; k++) {
+ if (bus_get_resource (devices[k], SYS_RES_IOPORT, 0,
+ &iobase, &rescount) != 0)
+ continue;
+
+ for (i = 0; porttab [i] != 0; i++) {
+ if (porttab [i] != iobase)
+ continue;
+ if (!cx_is_free_res (devices[k], 1, SYS_RES_IOPORT,
+ iobase, iobase + NPORT, NPORT))
+ continue;
+ if (cx_probe_board (iobase, -1, -1) == 0)
+ continue;
+ porttab [i] = -1;
+ device_set_desc_copy (devices[k], "Cronyx Sigma");
+ break;
+ }
+
+ if (porttab [i] == 0) {
+ device_delete_child (
+ device_get_parent (devices[k]),
+ devices [k]);
+ devices[k] = 0;
+ continue;
+ }
+ }
+ for (k = 0; k < devcount; k++) {
+ if (devices[k] == 0)
+ continue;
+ if (bus_get_resource (devices[k], SYS_RES_IOPORT, 0,
+ &iobase, &rescount) == 0)
+ continue;
+ for (i = 0; (iobase = porttab [i]) != 0; i++) {
+ if (porttab [i] == -1) {
+ continue;
+ }
+ if (!cx_is_free_res (devices[k], 1, SYS_RES_IOPORT,
+ iobase, iobase + NPORT, NPORT))
+ continue;
+ if (cx_probe_board (iobase, -1, -1) == 0)
+ continue;
+
+ bus_set_resource (devices[k], SYS_RES_IOPORT, 0,
+ iobase, NPORT);
+ porttab [i] = -1;
+ device_set_desc_copy (devices[k], "Cronyx Sigma");
+ break;
+ }
+ if (porttab [i] == 0) {
+ device_delete_child (
+ device_get_parent (devices[k]),
+ devices [k]);
+ }
+ }
+ free (devices, M_TEMP);
+ }
+
+ return;
+}
+
+static int cx_probe (device_t dev)
+{
+ int unit = device_get_unit (dev);
+ int i;
+ u_long iobase, rescount;
+
+ if (!device_get_desc (dev) ||
+ strcmp (device_get_desc (dev), "Cronyx Sigma"))
+ return ENXIO;
+
+ if (bus_get_resource (dev, SYS_RES_IOPORT, 0, &iobase, &rescount) != 0) {
+ printf ("cx%d: Couldn't get IOPORT\n", unit);
+ return ENXIO;
+ }
+
+ if (!cx_is_free_res (dev, 1, SYS_RES_IOPORT,
+ iobase, iobase + NPORT, NPORT)) {
+ printf ("cx%d: Resource IOPORT isn't free %lx\n", unit, iobase);
+ return ENXIO;
+ }
+
+ for (i = 0; porttab [i] != 0; i++) {
+ if (porttab [i] == iobase) {
+ porttab [i] = -1;
+ break;
+ }
+ }
+
+ if (porttab [i] == 0) {
+ return ENXIO;
+ }
+
+ if (!cx_probe_board (iobase, -1, -1)) {
+ printf ("cx%d: probing for Sigma at %lx faild\n", unit, iobase);
+ return ENXIO;
+ }
+
+ return 0;
+}
+#else /* __FreeBSD_version < 400000 */
+static int cx_probe (struct isa_device *id)
+{
+ cx_board_t *b;
+ int i;
+
+#ifndef NETGRAPH
+ if (! sppp_attach) {
+ printf ("cx%d: no synchronous PPP driver configured\n",
+ id->id_unit);
+ return 0;
+ }
+#endif
+ if (id->id_iobase < 0) {
+ /* Autodetect the adapter. */
+ for (i=0; ; i++) {
+ if (! porttab[i]) {
+ id->id_iobase = -1;
+ return 0;
+ }
+ id->id_iobase = porttab[i];
+ if (id->id_unit > 0 && adapter[0] && adapter[0]->port == id->id_iobase)
+ continue;
+ if (id->id_unit > 1 && adapter[1] && adapter[1]->port == id->id_iobase)
+ continue;
+ if (! haveseen_isadev (id, CC_IOADDR | CC_QUIET) &&
+ cx_probe_board (id->id_iobase, -1, -1))
+ break;
+ }
+ } else if (! cx_probe_board (id->id_iobase, -1, -1))
+ return 0;
+
+ if (id->id_drq < 0) {
+ /* Find available 16-bit DRQ. */
+
+ for (i=0; ; ++i) {
+ if (! dmatab[i]) {
+ printf ("cx%d: no available drq found\n",
+ id->id_unit);
+ id->id_drq = -1;
+ return 0;
+ }
+ id->id_drq = dmatab[i];
+ if (! haveseen_isadev (id, CC_DRQ | CC_QUIET)
+ && !isa_dma_acquire (id->id_drq))
+ break;
+ }
+ }
+
+ b = malloc (sizeof (cx_board_t), M_DEVBUF, M_WAITOK);
+ if (!b) {
+ printf ("cx:%d: Couldn't allocate memory\n", id->id_unit);
+ return (ENXIO);
+ }
+ adapter[id->id_unit] = b;
+ bzero (b, sizeof(cx_board_t));
+
+ if (! cx_open_board (b, id->id_unit, id->id_iobase,
+ id->id_irq ? ffs (id->id_irq) - 1 : -1, id->id_drq)) {
+ printf ("cx%d: cannot initialize adapter\n", id->id_unit);
+ isa_dma_release (id->id_drq);
+ adapter[id->id_unit] = 0;
+ free (b, M_DEVBUF);
+ return 0;
+ }
+
+ if (id->id_irq) {
+ if (! probe_irq (b, ffs (id->id_irq) - 1))
+ printf ("cx%d: irq %d not functional\n",
+ id->id_unit, ffs (id->id_irq) - 1);
+ } else {
+ /* Find available IRQ. */
+
+ for (i=0; ; ++i) {
+ if (! irqtab[i]) {
+ printf ("cx%d: no available irq found\n",
+ id->id_unit);
+ id->id_irq = -1;
+ isa_dma_release (id->id_drq);
+ adapter[id->id_unit] = 0;
+ free (b, M_DEVBUF);
+ return 0;
+ }
+ id->id_irq = 1 << irqtab[i];
+ if (haveseen_isadev (id, CC_IRQ | CC_QUIET))
+ continue;
+#ifdef KLD_MODULE
+ if (register_intr (irqtab[i], 0, 0, (inthand2_t*)
+ cx_intr, &net_imask, id->id_unit) != 0)
+ continue;
+ unregister_intr (irqtab[i], (inthand2_t*) cx_intr);
+#endif
+ if (probe_irq (b, irqtab[i]))
+ break;
+ }
+ }
+ cx_init (b, b->num, b->port, ffs (id->id_irq) - 1, b->dma);
+ cx_setup_board (b, 0, 0, 0);
+
+ return 1;
+}
+#endif /* __FreeBSD_version < 400000 */
+
+/*
+ * The adapter is present, initialize the driver structures.
+ */
+#if __FreeBSD_version < 400000
+static int cx_attach (struct isa_device *id)
+{
+#else
+static int cx_attach (device_t dev)
+{
+ bdrv_t *bd = device_get_softc (dev);
+ u_long iobase, drq, irq, rescount;
+ int unit = device_get_unit (dev);
+ int i;
+ int s;
+#endif
+ cx_board_t *b;
+ cx_chan_t *c;
+ drv_t *d;
+
+#if __FreeBSD_version >= 400000
+ KASSERT ((bd != NULL), ("cx%d: NULL device softc\n", unit));
+
+ bus_get_resource (dev, SYS_RES_IOPORT, 0, &iobase, &rescount);
+ bd->base_rid = 0;
+ bd->base_res = bus_alloc_resource (dev, SYS_RES_IOPORT, &bd->base_rid,
+ iobase, iobase + NPORT, NPORT, RF_ACTIVE);
+ if (! bd->base_res) {
+ printf ("cx%d: cannot allocate base address\n", unit);
+ return ENXIO;
+ }
+
+ if (bus_get_resource (dev, SYS_RES_DRQ, 0, &drq, &rescount) != 0) {
+ for (i = 0; (drq = dmatab [i]) != 0; i++) {
+ if (!cx_is_free_res (dev, 1, SYS_RES_DRQ,
+ drq, drq + 1, 1))
+ continue;
+ bus_set_resource (dev, SYS_RES_DRQ, 0, drq, 1);
+ break;
+ }
+
+ if (dmatab[i] == 0) {
+ bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
+ bd->base_res);
+ printf ("cx%d: Couldn't get DRQ\n", unit);
+ return ENXIO;
+ }
+ }
+
+ bd->drq_rid = 0;
+ bd->drq_res = bus_alloc_resource (dev, SYS_RES_DRQ, &bd->drq_rid,
+ drq, drq + 1, 1, RF_ACTIVE);
+ if (! bd->drq_res) {
+ printf ("cx%d: cannot allocate drq\n", unit);
+ bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
+ bd->base_res);
+ return ENXIO;
+ }
+
+ if (bus_get_resource (dev, SYS_RES_IRQ, 0, &irq, &rescount) != 0) {
+ for (i = 0; (irq = irqtab [i]) != 0; i++) {
+ if (!cx_is_free_res (dev, 1, SYS_RES_IRQ,
+ irq, irq + 1, 1))
+ continue;
+ bus_set_resource (dev, SYS_RES_IRQ, 0, irq, 1);
+ break;
+ }
+
+ if (irqtab[i] == 0) {
+ bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
+ bd->drq_res);
+ bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
+ bd->base_res);
+ printf ("cx%d: Couldn't get IRQ\n", unit);
+ return ENXIO;
+ }
+ }
+
+ bd->irq_rid = 0;
+ bd->irq_res = bus_alloc_resource (dev, SYS_RES_IRQ, &bd->irq_rid,
+ irq, irq + 1, 1, RF_ACTIVE);
+ if (! bd->irq_res) {
+ printf ("cx%d: Couldn't allocate irq\n", unit);
+ bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
+ bd->drq_res);
+ bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
+ bd->base_res);
+ return ENXIO;
+ }
+
+ b = malloc (sizeof (cx_board_t), M_DEVBUF, M_WAITOK);
+ if (!b) {
+ printf ("cx:%d: Couldn't allocate memory\n", unit);
+ return (ENXIO);
+ }
+ adapter[unit] = b;
+ bzero (b, sizeof(cx_board_t));
+
+ if (! cx_open_board (b, unit, iobase, irq, drq)) {
+ printf ("cx%d: error loading firmware\n", unit);
+ free (b, M_DEVBUF);
+ bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid,
+ bd->irq_res);
+ bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
+ bd->drq_res);
+ bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
+ bd->base_res);
+ return ENXIO;
+ }
+
+ bd->board = b;
+
+ if (! probe_irq (b, irq)) {
+ printf ("cx%d: irq %ld not functional\n", unit, irq);
+ bd->board = 0;
+ adapter [unit] = 0;
+ free (b, M_DEVBUF);
+ bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid,
+ bd->irq_res);
+ bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
+ bd->drq_res);
+ bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
+ bd->base_res);
+ return ENXIO;
+ }
+
+ s = splhigh ();
+ if (bus_setup_intr (dev, bd->irq_res, INTR_TYPE_NET, cx_intr, bd,
+ &bd->intrhand)) {
+ printf ("cx%d: Can't setup irq %ld\n", unit, irq);
+ bd->board = 0;
+ adapter [unit] = 0;
+ free (b, M_DEVBUF);
+ bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid,
+ bd->irq_res);
+ bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
+ bd->drq_res);
+ bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
+ bd->base_res);
+ splx (s);
+ return ENXIO;
+ }
+
+ cx_init (b, b->num, b->port, irq, drq);
+ cx_setup_board (b, 0, 0, 0);
+#else /* __FreeBSD_version >= 400000 */
+ b = adapter[id->id_unit];
+#endif /* __FreeBSD_version >= 400000 */
+
+ printf ("cx%d: <Cronyx-Sigma-%s>\n", b->num, b->name);
+#if __FreeBSD_version < 400000
+ id->id_ointr = cx_intr;
+#endif
+
+ for (c=b->chan; c<b->chan+NCHAN; ++c) {
+#if __FreeBSD_version >= 400000
+ char *dnmt="tty %x";
+ char *dnmc="cua %x";
+#endif
+ if (c->type == T_NONE)
+ continue;
+ d = contigmalloc (sizeof(drv_t), M_DEVBUF, M_WAITOK,
+ 0x100000, 0x1000000, 16, 0);
+ channel [b->num*NCHAN + c->num] = d;
+ bzero (d, sizeof(drv_t));
+ sprintf (d->name, "cx%d.%d", b->num, c->num);
+ d->board = b;
+ d->chan = c;
+ d->tty.t_oproc = cx_oproc;
+ d->tty.t_param = cx_param;
+#if __FreeBSD_version >= 400000
+ d->tty.t_stop = cx_stop;
+#endif
+ d->dtrwait = 3 * hz; /* Default DTR off timeout is 3 seconds. */
+ d->open_dev = 0;
+ c->sys = d;
+
+ switch (c->type) {
+ case T_SYNC_RS232:
+ case T_SYNC_V35:
+ case T_SYNC_RS449:
+ case T_UNIV:
+ case T_UNIV_RS232:
+ case T_UNIV_RS449:
+ case T_UNIV_V35:
+#ifdef NETGRAPH
+ if (ng_make_node_common (&typestruct, &d->node) != 0) {
+ printf ("%s: cannot make common node\n", d->name);
+ channel [b->num*NCHAN + c->num] = 0;
+ c->sys = 0;
+#if __FreeBSD_version < 400000
+ free (d, M_DEVBUF);
+#else
+ contigfree (d, sizeof (*d), M_DEVBUF);
+#endif
+ continue;
+ }
+#if __FreeBSD_version >= 500000
+ NG_NODE_SET_PRIVATE (d->node, d);
+#else
+ d->node->private = d;
+#endif
+ sprintf (d->nodename, "%s%d", NG_CX_NODE_TYPE,
+ c->board->num*NCHAN + c->num);
+ if (ng_name_node (d->node, d->nodename)) {
+ printf ("%s: cannot name node\n", d->nodename);
+#if __FreeBSD_version >= 500000
+ NG_NODE_UNREF (d->node);
+#else
+ ng_rmnode (d->node);
+ ng_unref (d->node);
+#endif
+ channel [b->num*NCHAN + c->num] = 0;
+ c->sys = 0;
+#if __FreeBSD_version < 400000
+ free (d, M_DEVBUF);
+#else
+ contigfree (d, sizeof (*d), M_DEVBUF);
+#endif
+ continue;
+ }
+ d->lo_queue.ifq_maxlen = IFQ_MAXLEN;
+ d->hi_queue.ifq_maxlen = IFQ_MAXLEN;
+#if __FreeBSD_version >= 500000
+ mtx_init (&d->lo_queue.ifq_mtx, "cx_queue_lo", NULL, MTX_DEF);
+ mtx_init (&d->hi_queue.ifq_mtx, "cx_queue_hi", NULL, MTX_DEF);
+#endif
+#else /*NETGRAPH*/
+ d->pp.pp_if.if_softc = d;
+#if __FreeBSD_version > 501000
+ if_initname (&d->pp.pp_if, "cx", b->num * NCHAN + c->num);
+#else
+ d->pp.pp_if.if_unit = b->num * NCHAN + c->num;
+ d->pp.pp_if.if_name = "cx";
+#endif
+ d->pp.pp_if.if_mtu = PP_MTU;
+ d->pp.pp_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
+ d->pp.pp_if.if_ioctl = cx_sioctl;
+ d->pp.pp_if.if_start = cx_ifstart;
+ d->pp.pp_if.if_watchdog = cx_ifwatchdog;
+ d->pp.pp_if.if_init = cx_initialize;
+ sppp_attach (&d->pp.pp_if);
+ if_attach (&d->pp.pp_if);
+ d->pp.pp_tlf = cx_tlf;
+ d->pp.pp_tls = cx_tls;
+#if __FreeBSD_version >= 400000 || NBPFILTER > 0
+ /* If BPF is in the kernel, call the attach for it.
+ * Size of PPP header is 4 bytes. */
+ bpfattach (&d->pp.pp_if, DLT_PPP, 4);
+#endif
+#endif /*NETGRAPH*/
+ }
+ cx_start_chan (c, &d->buf, vtophys (&d->buf));
+ cx_register_receive (c, &cx_receive);
+ cx_register_transmit (c, &cx_transmit);
+ cx_register_error (c, &cx_error);
+ cx_register_modem (c, &cx_modem);
+#if __FreeBSD_version >= 400000
+ dnmt[3] = 'x'+b->num;
+ dnmc[3] = 'x'+b->num;
+ d->devt[0] = make_dev (&cx_cdevsw, b->num*NCHAN + c->num, UID_ROOT, GID_WHEEL, 0644, dnmt, b->num*NCHAN + c->num);
+ d->devt[1] = make_dev (&cx_cdevsw, b->num*NCHAN + c->num + 64, UID_ROOT, GID_WHEEL, 0600, "cx%d", b->num*NCHAN + c->num);
+ d->devt[2] = make_dev (&cx_cdevsw, b->num*NCHAN + c->num + 128, UID_ROOT, GID_WHEEL, 0660, dnmc, b->num*NCHAN + c->num);
+ }
+ splx (s);
+
+ return 0;
+#else /* __FreeBSD_version < 400000 */
+ }
+
+ return 1;
+#endif
+}
+
+#if __FreeBSD_version >= 400000
+static int cx_detach (device_t dev)
+{
+ bdrv_t *bd = device_get_softc (dev);
+ cx_board_t *b = bd->board;
+ cx_chan_t *c;
+ int s = splhigh ();
+
+ /* Check if the device is busy (open). */
+ for (c = b->chan; c < b->chan + NCHAN; ++c) {
+ drv_t *d = (drv_t*) c->sys;
+
+ if (!d || d->chan->type == T_NONE)
+ continue;
+ if (d->lock) {
+ splx (s);
+ return EBUSY;
+ }
+ if (c->mode == M_ASYNC && (d->tty.t_state & TS_ISOPEN) &&
+ (d->open_dev|0x2)) {
+ splx (s);
+ return EBUSY;
+ }
+ if (d->running) {
+ splx (s);
+ return EBUSY;
+ }
+ }
+
+ /* Deactivate the timeout routine. And soft interrupt*/
+ if (led_timo[b->num].callout)
+ untimeout (cx_led_off, b, led_timo[b->num]);
+
+ for (c = b->chan; c < b->chan + NCHAN; ++c) {
+ drv_t *d = c->sys;
+
+ if (!d || d->chan->type == T_NONE)
+ continue;
+
+ if (d->dtr_timeout_handle.callout)
+ untimeout (cx_dtrwakeup, d, d->dtr_timeout_handle);
+ if (d->dcd_timeout_handle.callout)
+ untimeout (cx_carrier, c, d->dcd_timeout_handle);
+ }
+ bus_teardown_intr (dev, bd->irq_res, bd->intrhand);
+ bus_deactivate_resource (dev, SYS_RES_IRQ, bd->irq_rid, bd->irq_res);
+ bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid, bd->irq_res);
+
+ bus_deactivate_resource (dev, SYS_RES_DRQ, bd->drq_rid, bd->drq_res);
+ bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid, bd->drq_res);
+
+ bus_deactivate_resource (dev, SYS_RES_IOPORT, bd->base_rid, bd->irq_res);
+ bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid, bd->base_res);
+
+ cx_close_board (b);
+
+ /* Detach the interfaces, free buffer memory. */
+ for (c = b->chan; c < b->chan + NCHAN; ++c) {
+ drv_t *d = (drv_t*) c->sys;
+
+ if (!d || d->chan->type == T_NONE)
+ continue;
+#ifdef NETGRAPH
+#if __FreeBSD_version >= 500000
+ if (d->node) {
+ ng_rmnode_self (d->node);
+ NG_NODE_UNREF (d->node);
+ d->node = NULL;
+ }
+ mtx_destroy (&d->lo_queue.ifq_mtx);
+ mtx_destroy (&d->hi_queue.ifq_mtx);
+#else
+ ng_rmnode (d->node);
+ d->node = NULL;
+#endif
+#else
+#if __FreeBSD_version >= 410000 && NBPFILTER > 0
+ /* Detach from the packet filter list of interfaces. */
+ bpfdetach (&d->pp.pp_if);
+#endif
+ /* Detach from the sync PPP list. */
+ sppp_detach (&d->pp.pp_if);
+
+ if_detach (&d->pp.pp_if);
+#endif
+ destroy_dev (d->devt[0]);
+ destroy_dev (d->devt[1]);
+ destroy_dev (d->devt[2]);
+ }
+
+ cx_led_off (b);
+ if (led_timo[b->num].callout)
+ untimeout (cx_led_off, b, led_timo[b->num]);
+ splx (s);
+
+ s = splhigh ();
+ for (c = b->chan; c < b->chan + NCHAN; ++c) {
+ drv_t *d = (drv_t*) c->sys;
+
+ if (!d || d->chan->type == T_NONE)
+ continue;
+
+ /* Deallocate buffers. */
+ contigfree (d, sizeof (*d), M_DEVBUF);
+ }
+ bd->board = 0;
+ adapter [b->num] = 0;
+ free (b, M_DEVBUF);
+ splx (s);
+
+ return 0;
+}
+#endif
+
+#ifndef NETGRAPH
+static void cx_ifstart (struct ifnet *ifp)
+{
+ drv_t *d = ifp->if_softc;
+
+ cx_start (d);
+}
+
+static void cx_ifwatchdog (struct ifnet *ifp)
+{
+ drv_t *d = ifp->if_softc;
+
+ cx_watchdog (d);
+}
+
+static void cx_tlf (struct sppp *sp)
+{
+ drv_t *d = sp->pp_if.if_softc;
+
+ CX_DEBUG (d, ("cx_tlf\n"));
+/* cx_set_dtr (d->chan, 0);*/
+/* cx_set_rts (d->chan, 0);*/
+ sp->pp_down (sp);
+}
+
+static void cx_tls (struct sppp *sp)
+{
+ drv_t *d = sp->pp_if.if_softc;
+
+ CX_DEBUG (d, ("cx_tls\n"));
+ sp->pp_up (sp);
+}
+
+/*
+ * Initialization of interface.
+ * It seems to be never called by upper level.
+ */
+static void cx_initialize (void *softc)
+{
+ drv_t *d = softc;
+
+ CX_DEBUG (d, ("cx_initialize\n"));
+}
+
+/*
+ * Process an ioctl request.
+ */
+static int cx_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data)
+{
+ drv_t *d = ifp->if_softc;
+ int error, s, was_up, should_be_up;
+
+ /* No socket ioctls while the channel is in async mode. */
+ if (d->chan->type == T_NONE || d->chan->mode == M_ASYNC)
+ return EBUSY;
+
+ /* Socket ioctls on slave subchannels are not allowed. */
+ was_up = (ifp->if_flags & IFF_RUNNING) != 0;
+ error = sppp_ioctl (ifp, cmd, data);
+ if (error)
+ return error;
+
+ if (! (ifp->if_flags & IFF_DEBUG))
+ d->chan->debug = 0;
+ else if (! d->chan->debug)
+ d->chan->debug = 1;
+
+ switch (cmd) {
+ default: CX_DEBUG2 (d, ("ioctl 0x%lx\n", cmd)); return 0;
+ case SIOCADDMULTI: CX_DEBUG2 (d, ("SIOCADDMULTI\n")); return 0;
+ case SIOCDELMULTI: CX_DEBUG2 (d, ("SIOCDELMULTI\n")); return 0;
+ case SIOCSIFFLAGS: CX_DEBUG2 (d, ("SIOCSIFFLAGS\n")); break;
+ case SIOCSIFADDR: CX_DEBUG2 (d, ("SIOCSIFADDR\n")); break;
+ }
+
+ /* We get here only in case of SIFFLAGS or SIFADDR. */
+ s = splhigh ();
+ should_be_up = (ifp->if_flags & IFF_RUNNING) != 0;
+ if (!was_up && should_be_up) {
+ /* Interface goes up -- start it. */
+ cx_up (d);
+ cx_start (d);
+ } else if (was_up && !should_be_up) {
+ /* Interface is going down -- stop it. */
+ /* if ((d->pp.pp_flags & PP_FR) || (ifp->if_flags & PP_CISCO))*/
+ cx_down (d);
+ }
+ splx (s);
+ return 0;
+}
+#endif /*NETGRAPH*/
+
+/*
+ * Stop the interface. Called on splimp().
+ */
+static void cx_down (drv_t *d)
+{
+ int s = splhigh ();
+ CX_DEBUG (d, ("cx_down\n"));
+ cx_set_dtr (d->chan, 0);
+ cx_set_rts (d->chan, 0);
+ d->running = 0;
+ splx (s);
+}
+
+/*
+ * Start the interface. Called on splimp().
+ */
+static void cx_up (drv_t *d)
+{
+ int s = splhigh ();
+ CX_DEBUG (d, ("cx_up\n"));
+ cx_set_dtr (d->chan, 1);
+ cx_set_rts (d->chan, 1);
+ d->running = 1;
+ splx (s);
+}
+
+/*
+ * Start output on the (slave) interface. Get another datagram to send
+ * off of the interface queue, and copy it to the interface
+ * before starting the output.
+ */
+static void cx_send (drv_t *d)
+{
+ struct mbuf *m;
+ u_short len;
+
+ CX_DEBUG2 (d, ("cx_send\n"));
+
+ /* No output if the interface is down. */
+ if (! d->running)
+ return;
+
+ /* No output if the modem is off. */
+ if (! cx_get_dsr (d->chan) && ! cx_get_loop(d->chan))
+ return;
+
+ if (cx_buf_free (d->chan)) {
+ /* Get the packet to send. */
+#ifdef NETGRAPH
+ IF_DEQUEUE (&d->hi_queue, m);
+ if (! m)
+ IF_DEQUEUE (&d->lo_queue, m);
+#else
+ m = sppp_dequeue (&d->pp.pp_if);
+#endif
+ if (! m)
+ return;
+#if (__FreeBSD_version >= 400000 || NBPFILTER > 0) && !defined (NETGRAPH)
+ if (d->pp.pp_if.if_bpf)
+#if __FreeBSD_version >= 500000
+ BPF_MTAP (&d->pp.pp_if, m);
+#else
+ bpf_mtap (&d->pp.pp_if, m);
+#endif
+#endif
+ len = m->m_pkthdr.len;
+ if (! m->m_next)
+ cx_send_packet (d->chan, (u_char*)mtod (m, caddr_t),
+ len, 0);
+ else {
+ u_char buf [DMABUFSZ];
+ m_copydata (m, 0, len, buf);
+ cx_send_packet (d->chan, buf, len, 0);
+ }
+ m_freem (m);
+
+ /* Set up transmit timeout, 10 seconds. */
+#ifdef NETGRAPH
+ d->timeout = 10;
+#else
+ d->pp.pp_if.if_timer = 10;
+#endif
+ }
+#ifndef NETGRAPH
+ d->pp.pp_if.if_flags |= IFF_OACTIVE;
+#endif
+}
+
+/*
+ * Start output on the interface.
+ * Always called on splimp().
+ */
+static void cx_start (drv_t *d)
+{
+ int s = splhigh ();
+ if (d->running) {
+ if (! d->chan->dtr)
+ cx_set_dtr (d->chan, 1);
+ if (! d->chan->rts)
+ cx_set_rts (d->chan, 1);
+ cx_send (d);
+ }
+ splx (s);
+}
+
+/*
+ * Handle transmit timeouts.
+ * Recover after lost transmit interrupts.
+ * Always called on splimp().
+ */
+static void cx_watchdog (drv_t *d)
+{
+ int s = splhigh ();
+ CX_DEBUG (d, ("device timeout\n"));
+ if (d->running) {
+ cx_setup_chan (d->chan);
+ cx_start_chan (d->chan, 0, 0);
+ cx_set_dtr (d->chan, 1);
+ cx_set_rts (d->chan, 1);
+ cx_start (d);
+ }
+ splx (s);
+}
+
+/*
+ * Transmit callback function.
+ */
+static void cx_transmit (cx_chan_t *c, void *attachment, int len)
+{
+ drv_t *d = c->sys;
+
+ if (!d)
+ return;
+
+ if (c->mode == M_ASYNC) {
+ d->tty.t_state &= ~(TS_BUSY | TS_FLUSH);
+ d->atimeout = 0;
+ if (d->tty.t_dev) {
+ d->intr_action |= CX_WRITE;
+ MY_SOFT_INTR = 1;
+#if __FreeBSD_version >= 500000
+ swi_sched (cx_fast_ih, 0);
+#else
+ setsofttty ();
+#endif
+ }
+ return;
+ }
+#ifdef NETGRAPH
+ d->timeout = 0;
+#else
+ ++d->pp.pp_if.if_opackets;
+ d->pp.pp_if.if_flags &= ~IFF_OACTIVE;
+ d->pp.pp_if.if_timer = 0;
+#endif
+ cx_start (d);
+}
+
+/*
+ * Process the received packet.
+ */
+static void cx_receive (cx_chan_t *c, char *data, int len)
+{
+ drv_t *d = c->sys;
+ struct mbuf *m;
+ char *cc = data;
+#if __FreeBSD_version >= 500000 && defined NETGRAPH
+ int error;
+#endif
+
+ if (!d)
+ return;
+
+ if (c->mode == M_ASYNC) {
+ if (d->tty.t_state & TS_ISOPEN) {
+ async_q *q = &d->aqueue;
+ int size = BF_SZ - 1 - AQ_GSZ (q);
+
+ if (len <= 0 && !size)
+ return;
+
+ if (len > size) {
+ c->ierrs++;
+ cx_error (c, CX_OVERRUN);
+ len = size - 1;
+ }
+
+ while (len--) {
+ AQ_PUSH (q, *(unsigned char *)cc);
+ cc++;
+ }
+
+ d->intr_action |= CX_READ;
+ MY_SOFT_INTR = 1;
+#if __FreeBSD_version >= 500000
+ swi_sched (cx_fast_ih, 0);
+#else
+ setsofttty ();
+#endif
+ }
+ return;
+ }
+ if (! d->running)
+ return;
+
+ m = makembuf (data, len);
+ if (! m) {
+ CX_DEBUG (d, ("no memory for packet\n"));
+#ifndef NETGRAPH
+ ++d->pp.pp_if.if_iqdrops;
+#endif
+ return;
+ }
+ if (c->debug > 1)
+ printmbuf (m);
+#ifdef NETGRAPH
+ m->m_pkthdr.rcvif = 0;
+#if __FreeBSD_version >= 500000
+ NG_SEND_DATA_ONLY (error, d->hook, m);
+#else
+ ng_queue_data (d->hook, m, 0);
+#endif
+#else
+ ++d->pp.pp_if.if_ipackets;
+ m->m_pkthdr.rcvif = &d->pp.pp_if;
+#if __FreeBSD_version >= 400000 || NBPFILTER > 0
+ /* Check if there's a BPF listener on this interface.
+ * If so, hand off the raw packet to bpf. */
+ if (d->pp.pp_if.if_bpf)
+#if __FreeBSD_version >= 500000
+ BPF_TAP (&d->pp.pp_if, data, len);
+#else
+ bpf_tap (&d->pp.pp_if, data, len);
+#endif
+#endif
+ sppp_input (&d->pp.pp_if, m);
+#endif
+}
+
+#define CONDITION(t,tp) (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))\
+ && (!(tp->t_iflag & BRKINT) || (tp->t_iflag & IGNBRK))\
+ && (!(tp->t_iflag & PARMRK)\
+ || (tp->t_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))\
+ && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))\
+ && linesw[tp->t_line].l_rint == ttyinput)
+
+/*
+ * Error callback function.
+ */
+static void cx_error (cx_chan_t *c, int data)
+{
+ drv_t *d = c->sys;
+ async_q *q;
+
+ if (!d)
+ return;
+
+ q = &(d->aqueue);
+
+ switch (data) {
+ case CX_FRAME:
+ CX_DEBUG (d, ("frame error\n"));
+ if (c->mode == M_ASYNC && (d->tty.t_state & TS_ISOPEN)
+ && (AQ_GSZ (q) < BF_SZ - 1)
+ && (!CONDITION((&d->tty.t_termios), (&d->tty))
+ || !(d->tty.t_iflag & (IGNPAR | PARMRK)))) {
+ AQ_PUSH (q, TTY_FE);
+ d->intr_action |= CX_READ;
+ MY_SOFT_INTR = 1;
+#if __FreeBSD_version >= 500000
+ swi_sched (cx_fast_ih, 0);
+#else
+ setsofttty ();
+#endif
+ }
+#ifndef NETGRAPH
+ else
+ ++d->pp.pp_if.if_ierrors;
+#endif
+ break;
+ case CX_CRC:
+ CX_DEBUG (d, ("crc error\n"));
+ if (c->mode == M_ASYNC && (d->tty.t_state & TS_ISOPEN)
+ && (AQ_GSZ (q) < BF_SZ - 1)
+ && (!CONDITION((&d->tty.t_termios), (&d->tty))
+ || !(d->tty.t_iflag & INPCK)
+ || !(d->tty.t_iflag & (IGNPAR | PARMRK)))) {
+ AQ_PUSH (q, TTY_PE);
+ d->intr_action |= CX_READ;
+ MY_SOFT_INTR = 1;
+#if __FreeBSD_version >= 500000
+ swi_sched (cx_fast_ih, 0);
+#else
+ setsofttty ();
+#endif
+ }
+#ifndef NETGRAPH
+ else
+ ++d->pp.pp_if.if_ierrors;
+#endif
+ break;
+ case CX_OVERRUN:
+ CX_DEBUG (d, ("overrun error\n"));
+#ifdef TTY_OE
+ if (c->mode == M_ASYNC && (d->tty.t_state & TS_ISOPEN)
+ && (AQ_GSZ (q) < BF_SZ - 1)
+ && (!CONDITION((&d->tty.t_termios), (&d->tty)))) {
+ AQ_PUSH (q, TTY_OE);
+ d->intr_action |= CX_READ;
+ MY_SOFT_INTR = 1;
+#if __FreeBSD_version >= 500000
+ swi_sched (cx_fast_ih, 0);
+#else
+ setsofttty ();
+#endif
+ }
+#endif
+#ifndef NETGRAPH
+ else {
+ ++d->pp.pp_if.if_collisions;
+ ++d->pp.pp_if.if_ierrors;
+ }
+#endif
+ break;
+ case CX_OVERFLOW:
+ CX_DEBUG (d, ("overflow error\n"));
+#ifndef NETGRAPH
+ if (c->mode != M_ASYNC)
+ ++d->pp.pp_if.if_ierrors;
+#endif
+ break;
+ case CX_UNDERRUN:
+ CX_DEBUG (d, ("underrun error\n"));
+ if (c->mode != M_ASYNC) {
+#ifdef NETGRAPH
+ d->timeout = 0;
+#else
+ ++d->pp.pp_if.if_oerrors;
+ d->pp.pp_if.if_flags &= ~IFF_OACTIVE;
+ d->pp.pp_if.if_timer = 0;
+ cx_start (d);
+#endif
+ }
+ break;
+ case CX_BREAK:
+ CX_DEBUG (d, ("break error\n"));
+ if (c->mode == M_ASYNC && (d->tty.t_state & TS_ISOPEN)
+ && (AQ_GSZ (q) < BF_SZ - 1)
+ && (!CONDITION((&d->tty.t_termios), (&d->tty))
+ || !(d->tty.t_iflag & (IGNBRK | BRKINT | PARMRK)))) {
+ AQ_PUSH (q, TTY_BI);
+ d->intr_action |= CX_READ;
+ MY_SOFT_INTR = 1;
+#if __FreeBSD_version >= 500000
+ swi_sched (cx_fast_ih, 0);
+#else
+ setsofttty ();
+#endif
+ }
+#ifndef NETGRAPH
+ else
+ ++d->pp.pp_if.if_ierrors;
+#endif
+ break;
+ default:
+ CX_DEBUG (d, ("error #%d\n", data));
+ }
+}
+
+#if __FreeBSD_version < 500000
+static int cx_open (dev_t dev, int flag, int mode, struct proc *p)
+#else
+static int cx_open (dev_t dev, int flag, int mode, struct thread *td)
+#endif
+{
+ int unit = UNIT (dev);
+ drv_t *d;
+ int error;
+
+ if (unit >= NCX*NCHAN || ! (d = channel[unit]))
+ return ENXIO;
+ CX_DEBUG2 (d, ("cx_open unit=%d, flag=0x%x, mode=0x%x\n",
+ unit, flag, mode));
+
+ if (d->chan->mode != M_ASYNC || IF_CUNIT(dev)) {
+ d->open_dev |= 0x1;
+ return 0;
+ }
+#if __FreeBSD_version >= 400000
+ dev->si_tty = &d->tty;
+#endif
+ d->tty.t_dev = dev;
+again:
+ if (d->dtroff) {
+ error = tsleep (&d->dtrwait, TTIPRI | PCATCH, "cxdtr", 0);
+ if (error)
+ return error;
+ goto again;
+ }
+
+ if ((d->tty.t_state & TS_ISOPEN) && (d->tty.t_state & TS_XCLUDE) &&
+#if __FreeBSD_version >= 500000
+ suser (td))
+#else
+ p->p_ucred->cr_uid != 0)
+#endif
+ return EBUSY;
+
+ if (d->tty.t_state & TS_ISOPEN) {
+ /*
+ * Cannot open /dev/cua if /dev/tty already opened.
+ */
+ if (CALLOUT (dev) && ! d->callout)
+ return EBUSY;
+
+ /*
+ * Opening /dev/tty when /dev/cua is already opened.
+ * Wait for close, then try again.
+ */
+ if (! CALLOUT (dev) && d->callout) {
+ if (flag & O_NONBLOCK)
+ return EBUSY;
+ error = tsleep (d, TTIPRI | PCATCH, "cxbi", 0);
+ if (error)
+ return error;
+ goto again;
+ }
+ } else if (d->lock && ! CALLOUT (dev) && (flag & O_NONBLOCK))
+ /*
+ * We try to open /dev/tty in non-blocking mode
+ * while somebody is already waiting for carrier on it.
+ */
+ return EBUSY;
+ else {
+ ttychars (&d->tty);
+ if (d->tty.t_ispeed == 0) {
+ d->tty.t_iflag = 0;
+ d->tty.t_oflag = 0;
+ d->tty.t_lflag = 0;
+ d->tty.t_cflag = CREAD | CS8 | HUPCL;
+ d->tty.t_ispeed = d->chan->rxbaud;
+ d->tty.t_ospeed = d->chan->txbaud;
+ }
+ if (CALLOUT (dev))
+ d->tty.t_cflag |= CLOCAL;
+ else
+ d->tty.t_cflag &= ~CLOCAL;
+ cx_param (&d->tty, &d->tty.t_termios);
+ ttsetwater (&d->tty);
+ }
+
+ splhigh ();
+ if (! (d->tty.t_state & TS_ISOPEN)) {
+ cx_start_chan (d->chan, 0, 0);
+ cx_set_dtr (d->chan, 1);
+ cx_set_rts (d->chan, 1);
+ d->cd = cx_get_cd (d->chan);
+ if (CALLOUT (dev) || cx_get_cd (d->chan))
+ (*linesw[d->tty.t_line].l_modem) (&d->tty, 1);
+ }
+
+ if (! (flag & O_NONBLOCK) && ! (d->tty.t_cflag & CLOCAL) &&
+ ! (d->tty.t_state & TS_CARR_ON)) {
+ /* Lock the channel against cxconfig while we are
+ * waiting for carrier. */
+ d->lock++;
+ error = tsleep (&d->tty.t_rawq, TTIPRI | PCATCH, "cxdcd", 0);
+ /* Unlock the channel. */
+ d->lock--;
+ spl0 ();
+ if (error)
+ goto failed;
+ goto again;
+ }
+
+ error = (*linesw[d->tty.t_line].l_open) (dev, &d->tty);
+ disc_optim (&d->tty, &d->tty.t_termios);
+ spl0 ();
+ if (error) {
+failed: if (! (d->tty.t_state & TS_ISOPEN)) {
+ splhigh ();
+ cx_set_dtr (d->chan, 0);
+ cx_set_rts (d->chan, 0);
+ if (d->dtrwait) {
+ d->dtr_timeout_handle =
+ timeout (cx_dtrwakeup, d, d->dtrwait);
+ d->dtroff = 1;
+ }
+ spl0 ();
+ }
+ return error;
+ }
+
+ if (d->tty.t_state & TS_ISOPEN)
+ d->callout = CALLOUT (dev) ? 1 : 0;
+
+ d->open_dev |= 0x2;
+ CX_DEBUG2 (d, ("cx_open done\n"));
+ return 0;
+}
+
+#if __FreeBSD_version < 500000
+static int cx_close (dev_t dev, int flag, int mode, struct proc *p)
+#else
+static int cx_close (dev_t dev, int flag, int mode, struct thread *td)
+#endif
+{
+ drv_t *d = channel [UNIT (dev)];
+ int s;
+
+ CX_DEBUG2 (d, ("cx_close\n"));
+ if ((!(d->open_dev&0x2)) || IF_CUNIT(dev)){
+ d->open_dev &= ~0x1;
+ return 0;
+ }
+ s = splhigh ();
+ (*linesw[d->tty.t_line].l_close) (&d->tty, flag);
+ disc_optim (&d->tty, &d->tty.t_termios);
+
+ /* Disable receiver.
+ * Transmitter continues sending the queued data. */
+ cx_enable_receive (d->chan, 0);
+
+ /* Clear DTR and RTS. */
+ if ((d->tty.t_cflag & HUPCL) || ! (d->tty.t_state & TS_ISOPEN)) {
+ cx_set_dtr (d->chan, 0);
+ cx_set_rts (d->chan, 0);
+ if (d->dtrwait) {
+ d->dtr_timeout_handle =
+ timeout (cx_dtrwakeup, d, d->dtrwait);
+ d->dtroff = 1;
+ }
+ }
+ ttyclose (&d->tty);
+ splx (s);
+ d->callout = 0;
+
+ /* Wake up bidirectional opens. */
+ wakeup (d);
+ d->open_dev &= ~0x2;
+
+ return 0;
+}
+
+static int cx_read (dev_t dev, struct uio *uio, int flag)
+{
+ drv_t *d = channel [UNIT (dev)];
+
+ if (d) CX_DEBUG2 (d, ("cx_read\n"));
+ if (!d || d->chan->mode != M_ASYNC || IF_CUNIT(dev))
+ return EBADF;
+
+ return (*linesw[d->tty.t_line].l_read) (&d->tty, uio, flag);
+}
+
+static int cx_write (dev_t dev, struct uio *uio, int flag)
+{
+ drv_t *d = channel [UNIT (dev)];
+
+ if (d) CX_DEBUG2 (d, ("cx_write\n"));
+ if (!d || d->chan->mode != M_ASYNC || IF_CUNIT(dev))
+ return EBADF;
+
+ return (*linesw[d->tty.t_line].l_write) (&d->tty, uio, flag);
+}
+
+static int cx_modem_status (drv_t *d)
+{
+ int status = 0, s = splhigh ();
+ /* Already opened by someone or network interface is up? */
+ if ((d->chan->mode == M_ASYNC && (d->tty.t_state & TS_ISOPEN) &&
+ (d->open_dev|0x2)) || (d->chan->mode != M_ASYNC && d->running))
+ status = TIOCM_LE; /* always enabled while open */
+
+ if (cx_get_dsr (d->chan)) status |= TIOCM_DSR;
+ if (cx_get_cd (d->chan)) status |= TIOCM_CD;
+ if (cx_get_cts (d->chan)) status |= TIOCM_CTS;
+ if (d->chan->dtr) status |= TIOCM_DTR;
+ if (d->chan->rts) status |= TIOCM_RTS;
+ splx (s);
+ return status;
+}
+
+#if __FreeBSD_version < 500000
+static int cx_ioctl (dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
+#else
+static int cx_ioctl (dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
+#endif
+{
+ drv_t *d = channel [UNIT (dev)];
+ cx_chan_t *c;
+ struct serial_statistics *st;
+ int error, s;
+ char mask[16];
+
+ if (!d || !(c = d->chan))
+ return EINVAL;
+
+ switch (cmd) {
+ case SERIAL_GETREGISTERED:
+ CX_DEBUG2 (d, ("ioctl: getregistered\n"));
+ bzero (mask, sizeof(mask));
+ for (s=0; s<NCX*NCHAN; ++s)
+ if (channel [s])
+ mask [s/8] |= 1 << (s & 7);
+ bcopy (mask, data, sizeof (mask));
+ return 0;
+
+ case SERIAL_GETPORT:
+ CX_DEBUG2 (d, ("ioctl: getport\n"));
+ s = splhigh ();
+ *(int *)data = cx_get_port (c);
+ splx (s);
+ if (*(int *)data<0)
+ return (EINVAL);
+ else
+ return 0;
+
+ case SERIAL_SETPORT:
+ CX_DEBUG2 (d, ("ioctl: setproto\n"));
+ /* Only for superuser! */
+#if __FreeBSD_version < 400000
+ error = suser (p->p_ucred, &p->p_acflag);
+#elif __FreeBSD_version < 500000
+ error = suser (p);
+#else /* __FreeBSD_version >= 500000 */
+ error = suser (td);
+#endif /* __FreeBSD_version >= 500000 */
+ if (error)
+ return error;
+
+ s = splhigh ();
+ cx_set_port (c, *(int *)data);
+ splx (s);
+ return 0;
+
+#ifndef NETGRAPH
+ case SERIAL_GETPROTO:
+ CX_DEBUG2 (d, ("ioctl: getproto\n"));
+ s = splhigh ();
+ strcpy ((char*)data, (c->mode == M_ASYNC) ? "async" :
+ /*(d->pp.pp_flags & PP_FR) ? "fr" :*/
+ (d->pp.pp_if.if_flags & PP_CISCO) ? "cisco" : "ppp");
+ splx (s);
+ return 0;
+
+ case SERIAL_SETPROTO:
+ CX_DEBUG2 (d, ("ioctl: setproto\n"));
+ /* Only for superuser! */
+#if __FreeBSD_version < 400000
+ error = suser (p->p_ucred, &p->p_acflag);
+#elif __FreeBSD_version < 500000
+ error = suser (p);
+#else /* __FreeBSD_version >= 500000 */
+ error = suser (td);
+#endif /* __FreeBSD_version >= 500000 */
+ if (error)
+ return error;
+ if (c->mode == M_ASYNC)
+ return EBUSY;
+ if (d->pp.pp_if.if_flags & IFF_RUNNING)
+ return EBUSY;
+ if (! strcmp ("cisco", (char*)data)) {
+/* d->pp.pp_flags &= ~(PP_FR);*/
+ d->pp.pp_flags |= PP_KEEPALIVE;
+ d->pp.pp_if.if_flags |= PP_CISCO;
+/* } else if (! strcmp ("fr", (char*)data)) {*/
+/* d->pp.pp_if.if_flags &= ~(PP_CISCO);*/
+/* d->pp.pp_flags |= PP_FR | PP_KEEPALIVE;*/
+ } else if (! strcmp ("ppp", (char*)data)) {
+ d->pp.pp_flags &= ~(/*PP_FR |*/ PP_KEEPALIVE);
+ d->pp.pp_if.if_flags &= ~(PP_CISCO);
+ } else
+ return EINVAL;
+ return 0;
+
+ case SERIAL_GETKEEPALIVE:
+ CX_DEBUG2 (d, ("ioctl: getkeepalive\n"));
+ if (/*(d->pp.pp_flags & PP_FR) ||*/
+ (d->pp.pp_if.if_flags & PP_CISCO) ||
+ (c->mode == M_ASYNC))
+ return EINVAL;
+ s = splhigh ();
+ *(int*)data = (d->pp.pp_flags & PP_KEEPALIVE) ? 1 : 0;
+ splx (s);
+ return 0;
+
+ case SERIAL_SETKEEPALIVE:
+ CX_DEBUG2 (d, ("ioctl: setkeepalive\n"));
+ /* Only for superuser! */
+#if __FreeBSD_version < 400000
+ error = suser (p->p_ucred, &p->p_acflag);
+#elif __FreeBSD_version < 500000
+ error = suser (p);
+#else /* __FreeBSD_version >= 500000 */
+ error = suser (td);
+#endif /* __FreeBSD_version >= 500000 */
+ if (error)
+ return error;
+ if (/*(d->pp.pp_flags & PP_FR) ||*/
+ (d->pp.pp_if.if_flags & PP_CISCO))
+ return EINVAL;
+ s = splhigh ();
+ if (*(int*)data)
+ d->pp.pp_flags |= PP_KEEPALIVE;
+ else
+ d->pp.pp_flags &= ~PP_KEEPALIVE;
+ splx (s);
+ return 0;
+#endif /*NETGRAPH*/
+
+ case SERIAL_GETMODE:
+ CX_DEBUG2 (d, ("ioctl: getmode\n"));
+ s = splhigh ();
+ *(int*)data = (c->mode == M_ASYNC) ?
+ SERIAL_ASYNC : SERIAL_HDLC;
+ splx (s);
+ return 0;
+
+ case SERIAL_SETMODE:
+ CX_DEBUG2 (d, ("ioctl: setmode\n"));
+ /* Only for superuser! */
+#if __FreeBSD_version < 400000
+ error = suser (p->p_ucred, &p->p_acflag);
+#elif __FreeBSD_version < 500000
+ error = suser (p);
+#else /* __FreeBSD_version >= 500000 */
+ error = suser (td);
+#endif /* __FreeBSD_version >= 500000 */
+ if (error)
+ return error;
+
+ /* Somebody is waiting for carrier? */
+ if (d->lock)
+ return EBUSY;
+ /* /dev/ttyXX is already opened by someone? */
+ if (c->mode == M_ASYNC && (d->tty.t_state & TS_ISOPEN) &&
+ (d->open_dev|0x2))
+ return EBUSY;
+ /* Network interface is up?
+ * Cannot change to async mode. */
+ if (c->mode != M_ASYNC && d->running &&
+ (*(int*)data == SERIAL_ASYNC))
+ return EBUSY;
+
+ s = splhigh ();
+ if (c->mode == M_HDLC && *(int*)data == SERIAL_ASYNC) {
+ cx_set_mode (c, M_ASYNC);
+ cx_enable_receive (c, 0);
+ cx_enable_transmit (c, 0);
+ } else if (c->mode == M_ASYNC && *(int*)data == SERIAL_HDLC) {
+ cx_set_mode (c, M_HDLC);
+ cx_enable_receive (c, 1);
+ cx_enable_transmit (c, 1);
+ }
+ splx (s);
+ return 0;
+
+ case SERIAL_GETSTAT:
+ CX_DEBUG2 (d, ("ioctl: getestat\n"));
+ st = (struct serial_statistics*) data;
+ s = splhigh ();
+ st->rintr = c->rintr;
+ st->tintr = c->tintr;
+ st->mintr = c->mintr;
+ st->ibytes = c->ibytes;
+ st->ipkts = c->ipkts;
+ st->ierrs = c->ierrs;
+ st->obytes = c->obytes;
+ st->opkts = c->opkts;
+ st->oerrs = c->oerrs;
+ splx (s);
+ return 0;
+
+ case SERIAL_CLRSTAT:
+ CX_DEBUG2 (d, ("ioctl: clrstat\n"));
+ /* Only for superuser! */
+#if __FreeBSD_version < 400000
+ error = suser (p->p_ucred, &p->p_acflag);
+#elif __FreeBSD_version < 500000
+ error = suser (p);
+#else /* __FreeBSD_version >= 500000 */
+ error = suser (td);
+#endif /* __FreeBSD_version >= 500000 */
+ if (error)
+ return error;
+ s = splhigh ();
+ c->rintr = 0;
+ c->tintr = 0;
+ c->mintr = 0;
+ c->ibytes = 0;
+ c->ipkts = 0;
+ c->ierrs = 0;
+ c->obytes = 0;
+ c->opkts = 0;
+ c->oerrs = 0;
+ splx (s);
+ return 0;
+
+ case SERIAL_GETBAUD:
+ CX_DEBUG2 (d, ("ioctl: getbaud\n"));
+ if (c->mode == M_ASYNC)
+ return EINVAL;
+ s = splhigh ();
+ *(long*)data = cx_get_baud(c);
+ splx (s);
+ return 0;
+
+ case SERIAL_SETBAUD:
+ CX_DEBUG2 (d, ("ioctl: setbaud\n"));
+ /* Only for superuser! */
+#if __FreeBSD_version < 400000
+ error = suser (p->p_ucred, &p->p_acflag);
+#elif __FreeBSD_version < 500000
+ error = suser (p);
+#else /* __FreeBSD_version >= 500000 */
+ error = suser (td);
+#endif /* __FreeBSD_version >= 500000 */
+ if (error)
+ return error;
+ if (c->mode == M_ASYNC)
+ return EINVAL;
+ s = splhigh ();
+ cx_set_baud (c, *(long*)data);
+ splx (s);
+ return 0;
+
+ case SERIAL_GETLOOP:
+ CX_DEBUG2 (d, ("ioctl: getloop\n"));
+ if (c->mode == M_ASYNC)
+ return EINVAL;
+ s = splhigh ();
+ *(int*)data = cx_get_loop (c);
+ splx (s);
+ return 0;
+
+ case SERIAL_SETLOOP:
+ CX_DEBUG2 (d, ("ioctl: setloop\n"));
+ /* Only for superuser! */
+#if __FreeBSD_version < 400000
+ error = suser (p->p_ucred, &p->p_acflag);
+#elif __FreeBSD_version < 500000
+ error = suser (p);
+#else /* __FreeBSD_version >= 500000 */
+ error = suser (td);
+#endif /* __FreeBSD_version >= 500000 */
+ if (error)
+ return error;
+ if (c->mode == M_ASYNC)
+ return EINVAL;
+ s = splhigh ();
+ cx_set_loop (c, *(int*)data);
+ splx (s);
+ return 0;
+
+ case SERIAL_GETDPLL:
+ CX_DEBUG2 (d, ("ioctl: getdpll\n"));
+ if (c->mode == M_ASYNC)
+ return EINVAL;
+ s = splhigh ();
+ *(int*)data = cx_get_dpll (c);
+ splx (s);
+ return 0;
+
+ case SERIAL_SETDPLL:
+ CX_DEBUG2 (d, ("ioctl: setdpll\n"));
+ /* Only for superuser! */
+#if __FreeBSD_version < 400000
+ error = suser (p->p_ucred, &p->p_acflag);
+#elif __FreeBSD_version < 500000
+ error = suser (p);
+#else /* __FreeBSD_version >= 500000 */
+ error = suser (td);
+#endif /* __FreeBSD_version >= 500000 */
+ if (error)
+ return error;
+ if (c->mode == M_ASYNC)
+ return EINVAL;
+ s = splhigh ();
+ cx_set_dpll (c, *(int*)data);
+ splx (s);
+ return 0;
+
+ case SERIAL_GETNRZI:
+ CX_DEBUG2 (d, ("ioctl: getnrzi\n"));
+ if (c->mode == M_ASYNC)
+ return EINVAL;
+ s = splhigh ();
+ *(int*)data = cx_get_nrzi (c);
+ splx (s);
+ return 0;
+
+ case SERIAL_SETNRZI:
+ CX_DEBUG2 (d, ("ioctl: setnrzi\n"));
+ /* Only for superuser! */
+#if __FreeBSD_version < 400000
+ error = suser (p->p_ucred, &p->p_acflag);
+#elif __FreeBSD_version < 500000
+ error = suser (p);
+#else /* __FreeBSD_version >= 500000 */
+ error = suser (td);
+#endif /* __FreeBSD_version >= 500000 */
+ if (error)
+ return error;
+ if (c->mode == M_ASYNC)
+ return EINVAL;
+ s = splhigh ();
+ cx_set_nrzi (c, *(int*)data);
+ splx (s);
+ return 0;
+
+ case SERIAL_GETDEBUG:
+ CX_DEBUG2 (d, ("ioctl: getdebug\n"));
+ s = splhigh ();
+ *(int*)data = c->debug;
+ splx (s);
+ return 0;
+
+ case SERIAL_SETDEBUG:
+ CX_DEBUG2 (d, ("ioctl: setdebug\n"));
+ /* Only for superuser! */
+#if __FreeBSD_version < 400000
+ error = suser (p->p_ucred, &p->p_acflag);
+#elif __FreeBSD_version < 500000
+ error = suser (p);
+#else /* __FreeBSD_version >= 500000 */
+ error = suser (td);
+#endif /* __FreeBSD_version >= 500000 */
+ if (error)
+ return error;
+ s = splhigh ();
+ c->debug = *(int*)data;
+ splx (s);
+#ifndef NETGRAPH
+ if (d->chan->debug)
+ d->pp.pp_if.if_flags |= IFF_DEBUG;
+ else
+ d->pp.pp_if.if_flags &= (~IFF_DEBUG);
+#endif
+ return 0;
+ }
+
+ if (c->mode == M_ASYNC) {
+#if __FreeBSD_version >= 500000
+ error = (*linesw[d->tty.t_line].l_ioctl) (&d->tty, cmd, data, flag, td);
+#else
+ error = (*linesw[d->tty.t_line].l_ioctl) (&d->tty, cmd, data, flag, p);
+#endif
+ disc_optim (&d->tty, &d->tty.t_termios);
+ if (error != ENOIOCTL) {
+ if (error)
+ CX_DEBUG2 (d, ("l_ioctl: 0x%lx, error %d\n", cmd, error));
+ return error;
+ }
+ error = ttioctl (&d->tty, cmd, data, flag);
+ disc_optim (&d->tty, &d->tty.t_termios);
+ if (error != ENOIOCTL) {
+ if (error)
+ CX_DEBUG2 (d, ("ttioctl: 0x%lx, error %d\n", cmd, error));
+ return error;
+ }
+ }
+
+ switch (cmd) {
+ case TIOCSBRK: /* Start sending line break */
+ CX_DEBUG2 (d, ("ioctl: tiocsbrk\n"));
+ s = splhigh ();
+ cx_send_break (c, 500);
+ splx (s);
+ return 0;
+
+ case TIOCCBRK: /* Stop sending line break */
+ CX_DEBUG2 (d, ("ioctl: tioccbrk\n"));
+ return 0;
+
+ case TIOCSDTR: /* Set DTR */
+ CX_DEBUG2 (d, ("ioctl: tiocsdtr\n"));
+ s = splhigh ();
+ cx_set_dtr (c, 1);
+ splx (s);
+ return 0;
+
+ case TIOCCDTR: /* Clear DTR */
+ CX_DEBUG2 (d, ("ioctl: tioccdtr\n"));
+ s = splhigh ();
+ cx_set_dtr (c, 0);
+ splx (s);
+ return 0;
+
+ case TIOCMSET: /* Set DTR/RTS */
+ CX_DEBUG2 (d, ("ioctl: tiocmset\n"));
+ s = splhigh ();
+ cx_set_dtr (c, (*(int*)data & TIOCM_DTR) ? 1 : 0);
+ cx_set_rts (c, (*(int*)data & TIOCM_RTS) ? 1 : 0);
+ splx (s);
+ return 0;
+
+ case TIOCMBIS: /* Add DTR/RTS */
+ CX_DEBUG2 (d, ("ioctl: tiocmbis\n"));
+ s = splhigh ();
+ if (*(int*)data & TIOCM_DTR) cx_set_dtr (c, 1);
+ if (*(int*)data & TIOCM_RTS) cx_set_rts (c, 1);
+ splx (s);
+ return 0;
+
+ case TIOCMBIC: /* Clear DTR/RTS */
+ CX_DEBUG2 (d, ("ioctl: tiocmbic\n"));
+ s = splhigh ();
+ if (*(int*)data & TIOCM_DTR) cx_set_dtr (c, 0);
+ if (*(int*)data & TIOCM_RTS) cx_set_rts (c, 0);
+ splx (s);
+ return 0;
+
+ case TIOCMGET: /* Get modem status */
+ CX_DEBUG2 (d, ("ioctl: tiocmget\n"));
+ *(int*)data = cx_modem_status (d);
+ return 0;
+
+#ifdef TIOCMSDTRWAIT
+ case TIOCMSDTRWAIT:
+ CX_DEBUG2 (d, ("ioctl: tiocmsdtrwait\n"));
+ /* Only for superuser! */
+#if __FreeBSD_version < 400000
+ error = suser (p->p_ucred, &p->p_acflag);
+#elif __FreeBSD_version < 500000
+ error = suser (p);
+#else /* __FreeBSD_version >= 500000 */
+ error = suser (td);
+#endif /* __FreeBSD_version >= 500000 */
+ if (error)
+ return error;
+ s = splhigh ();
+ d->dtrwait = *(int*)data * hz / 100;
+ splx (s);
+ return 0;
+#endif
+
+#ifdef TIOCMGDTRWAIT
+ case TIOCMGDTRWAIT:
+ CX_DEBUG2 (d, ("ioctl: tiocmgdtrwait\n"));
+ s = splhigh ();
+ *(int*)data = d->dtrwait * 100 / hz;
+ splx (s);
+ return 0;
+#endif
+ }
+ CX_DEBUG2 (d, ("ioctl: 0x%lx\n", cmd));
+ return ENOTTY;
+}
+
+/*
+ * Wake up opens() waiting for DTR ready.
+ */
+static void cx_dtrwakeup (void *arg)
+{
+ drv_t *d = arg;
+
+ d->dtroff = 0;
+ wakeup (&d->dtrwait);
+}
+
+
+static void
+disc_optim(tp, t)
+ struct tty *tp;
+ struct termios *t;
+{
+ if (CONDITION(t,tp))
+ tp->t_state |= TS_CAN_BYPASS_L_RINT;
+ else
+ tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
+}
+
+#if __FreeBSD_version >= 500000
+void cx_softintr (void *unused)
+#else
+void cx_softintr ()
+#endif
+{
+ drv_t *d;
+ async_q *q;
+ int i, s, ic, k;
+ while (MY_SOFT_INTR) {
+ MY_SOFT_INTR = 0;
+ for (i=0; i<NCX*NCHAN; ++i) {
+ d = channel [i];
+ if (!d || !d->chan || d->chan->type == T_NONE
+ || d->chan->mode != M_ASYNC || !d->tty.t_dev)
+ continue;
+ s = splhigh ();
+ if (d->intr_action & CX_READ) {
+ q = &(d->aqueue);
+ if (d->tty.t_state & TS_CAN_BYPASS_L_RINT) {
+ k = AQ_GSZ(q);
+ if (d->tty.t_rawq.c_cc + k >
+ d->tty.t_ihiwat
+ && (d->tty.t_cflag & CRTS_IFLOW
+ || d->tty.t_iflag & IXOFF)
+ && !(d->tty.t_state & TS_TBLOCK))
+ ttyblock(&d->tty);
+ d->tty.t_rawcc += k;
+ while (k>0) {
+ k--;
+ AQ_POP (q, ic);
+ splx (s);
+ putc (ic, &d->tty.t_rawq);
+ s = splhigh ();
+ }
+ ttwakeup(&d->tty);
+ if (d->tty.t_state & TS_TTSTOP
+ && (d->tty.t_iflag & IXANY
+ || d->tty.t_cc[VSTART] ==
+ d->tty.t_cc[VSTOP])) {
+ d->tty.t_state &= ~TS_TTSTOP;
+ d->tty.t_lflag &= ~FLUSHO;
+ d->intr_action |= CX_WRITE;
+ }
+ } else {
+ while (q->end != q->beg) {
+ AQ_POP (q, ic);
+ splx (s);
+ (*linesw[d->tty.t_line].l_rint)
+ (ic, &d->tty);
+ s = splhigh ();
+ }
+ }
+ d->intr_action &= ~CX_READ;
+ }
+ splx (s);
+
+ s = splhigh ();
+ if (d->intr_action & CX_WRITE) {
+ if (d->tty.t_line)
+ (*linesw[d->tty.t_line].l_start) (&d->tty);
+ else
+ cx_oproc (&d->tty);
+ d->intr_action &= ~CX_WRITE;
+ }
+ splx (s);
+
+ }
+ }
+}
+
+/*
+ * Fill transmitter buffer with data.
+ */
+static void cx_oproc (struct tty *tp)
+{
+ int s = splhigh (), k;
+ drv_t *d = channel [UNIT (tp->t_dev)];
+ static u_char buf[DMABUFSZ];
+ u_char *p;
+ u_short len = 0, sublen = 0;
+
+ if (!d) {
+ splx (s);
+ return;
+ }
+
+ CX_DEBUG2 (d, ("cx_oproc\n"));
+ if (tp->t_cflag & CRTSCTS && (tp->t_state & TS_TBLOCK) && d->chan->rts)
+ cx_set_rts (d->chan, 0);
+ else if (tp->t_cflag & CRTSCTS && ! (tp->t_state & TS_TBLOCK) && ! d->chan->rts)
+ cx_set_rts (d->chan, 1);
+
+ if (! (tp->t_state & (TS_TIMEOUT | TS_TTSTOP))) {
+ /* Start transmitter. */
+ cx_enable_transmit (d->chan, 1);
+
+ /* Is it busy? */
+ if (! cx_buf_free (d->chan)) {
+ tp->t_state |= TS_BUSY;
+ splx (s);
+ return;
+ }
+ if (tp->t_iflag & IXOFF) {
+ p = (buf + (DMABUFSZ/2));
+ sublen = q_to_b (&tp->t_outq, p, (DMABUFSZ/2));
+ k = sublen;
+ while (k--) {
+ /* Send XON/XOFF out of band. */
+ if (*p == tp->t_cc[VSTOP]) {
+ cx_xflow_ctl (d->chan, 0);
+ p++;
+ continue;
+ }
+ if (*p == tp->t_cc[VSTART]) {
+ cx_xflow_ctl (d->chan, 1);
+ p++;
+ continue;
+ }
+ buf[len] = *p;
+ len++;
+ p++;
+ }
+ } else {
+ p = buf;
+ len = q_to_b (&tp->t_outq, p, (DMABUFSZ/2));
+ }
+ if (len) {
+ cx_send_packet (d->chan, buf, len, 0);
+ tp->t_state |= TS_BUSY;
+ d->atimeout = 10;
+ CX_DEBUG2 (d, ("out %d bytes\n", len));
+ }
+ }
+ ttwwakeup (tp);
+ splx (s);
+}
+
+static int cx_param (struct tty *tp, struct termios *t)
+{
+ drv_t *d = channel [UNIT (tp->t_dev)];
+ int s, bits, parity;
+
+ if (!d)
+ return EINVAL;
+
+ s = splhigh ();
+ if (t->c_ospeed == 0) {
+ /* Clear DTR and RTS. */
+ cx_set_dtr (d->chan, 0);
+ splx (s);
+ CX_DEBUG2 (d, ("cx_param (hangup)\n"));
+ return 0;
+ }
+ CX_DEBUG2 (d, ("cx_param\n"));
+
+ /* Check requested parameters. */
+ if (t->c_ospeed < 300 || t->c_ospeed > 256*1024) {
+ splx (s);
+ return EINVAL;
+ }
+ if (t->c_ispeed && (t->c_ispeed < 300 || t->c_ispeed > 256*1024)) {
+ splx (s);
+ return EINVAL;
+ }
+
+ /* And copy them to tty and channel structures. */
+ tp->t_ispeed = t->c_ispeed = tp->t_ospeed = t->c_ospeed;
+ tp->t_cflag = t->c_cflag;
+
+ /* Set character length and parity mode. */
+ switch (t->c_cflag & CSIZE) {
+ default:
+ case CS8: bits = 8; break;
+ case CS7: bits = 7; break;
+ case CS6: bits = 6; break;
+ case CS5: bits = 5; break;
+ }
+
+ parity = ((t->c_cflag & PARENB) ? 1 : 0) *
+ (1 + ((t->c_cflag & PARODD) ? 0 : 1));
+
+ /* Set current channel number. */
+ if (! d->chan->dtr)
+ cx_set_dtr (d->chan, 1);
+
+ disc_optim (&d->tty, &d->tty.t_termios);
+ cx_set_async_param (d->chan, t->c_ospeed, bits, parity, (t->c_cflag & CSTOPB),
+ !(t->c_cflag & PARENB), (t->c_cflag & CRTSCTS),
+ (t->c_iflag & IXON), (t->c_iflag & IXANY),
+ t->c_cc[VSTART], t->c_cc[VSTOP]);
+ splx (s);
+ return 0;
+}
+
+#if __FreeBSD_version < 400000
+static struct tty *cx_devtotty (dev_t dev)
+{
+ int unit = UNIT (dev);
+
+ if (unit == UNIT_CTL || unit >= NCX*NCHAN || ! channel[unit])
+ return 0;
+ return &channel[unit]->tty;
+}
+#endif
+
+/*
+ * Stop output on a line
+ */
+static void cx_stop (struct tty *tp, int flag)
+{
+ drv_t *d = channel [UNIT (tp->t_dev)];
+ int s;
+
+ if (!d)
+ return;
+
+ s = splhigh ();
+
+ if (tp->t_state & TS_BUSY) {
+ /* Stop transmitter */
+ CX_DEBUG2 (d, ("cx_stop\n"));
+ cx_transmitter_ctl (d->chan, 0);
+ }
+ splx (s);
+}
+
+/*
+ * Process the (delayed) carrier signal setup.
+ */
+static void cx_carrier (void *arg)
+{
+ drv_t *d = arg;
+ cx_chan_t *c = d->chan;
+ int s, cd;
+
+ s = splhigh ();
+ cd = cx_get_cd (c);
+ if (d->cd != cd) {
+ if (cd) {
+ CX_DEBUG (d, ("carrier on\n"));
+ d->cd = 1;
+ splx (s);
+ (*linesw[d->tty.t_line].l_modem) (&d->tty, 1);
+ } else {
+ CX_DEBUG (d, ("carrier loss\n"));
+ d->cd = 0;
+ splx (s);
+ (*linesw[d->tty.t_line].l_modem) (&d->tty, 0);
+ }
+ }
+}
+
+/*
+ * Modem signal callback function.
+ */
+static void cx_modem (cx_chan_t *c)
+{
+ drv_t *d = c->sys;
+
+ if (!d || c->mode != M_ASYNC)
+ return;
+ /* Handle carrier detect/loss. */
+ untimeout (cx_carrier, c, d->dcd_timeout_handle);
+ /* Carrier changed - delay processing DCD for a while
+ * to give both sides some time to initialize. */
+ d->dcd_timeout_handle = timeout (cx_carrier, d, hz/2);
+}
+
+#if __FreeBSD_version < 400000
+struct isa_driver cxdriver = { cx_probe, cx_attach, "cx" };
+static struct cdevsw cx_cdevsw = {
+ cx_open, cx_close, cx_read, cx_write,
+ cx_ioctl, cx_stop, noreset, cx_devtotty,
+ ttpoll, nommap, NULL, "cx",
+ NULL, -1,
+};
+#elif __FreeBSD_version < 500000
+static struct cdevsw cx_cdevsw = {
+ cx_open, cx_close, cx_read, cx_write,
+ cx_ioctl, ttypoll, nommap, nostrategy,
+ "cx", CDEV_MAJOR, nodump, nopsize,
+ D_TTY, -1
+};
+#elif __FreeBSD_version == 500000
+static struct cdevsw cx_cdevsw = {
+ cx_open, cx_close, cx_read, cx_write,
+ cx_ioctl, ttypoll, nommap, nostrategy,
+ "cx", CDEV_MAJOR, nodump, nopsize,
+ D_TTY,
+ };
+#elif __FreeBSD_version <= 501000
+static struct cdevsw cx_cdevsw = {
+ .d_open = cx_open,
+ .d_close = cx_close,
+ .d_read = cx_read,
+ .d_write = cx_write,
+ .d_ioctl = cx_ioctl,
+ .d_poll = ttypoll,
+ .d_mmap = nommap,
+ .d_strategy = nostrategy,
+ .d_name = "cx",
+ .d_maj = CDEV_MAJOR,
+ .d_dump = nodump,
+ .d_flags = D_TTY,
+};
+#elif __FreeBSD_version < 502103
+static struct cdevsw cx_cdevsw = {
+ .d_open = cx_open,
+ .d_close = cx_close,
+ .d_read = cx_read,
+ .d_write = cx_write,
+ .d_ioctl = cx_ioctl,
+ .d_poll = ttypoll,
+ .d_name = "cx",
+ .d_maj = CDEV_MAJOR,
+ .d_flags = D_TTY,
+};
+#else /* __FreeBSD_version >= 502103 */
+static struct cdevsw cx_cdevsw = {
+ .d_version = D_VERSION,
+ .d_open = cx_open,
+ .d_close = cx_close,
+ .d_read = cx_read,
+ .d_write = cx_write,
+ .d_ioctl = cx_ioctl,
+ .d_name = "cx",
+ .d_maj = CDEV_MAJOR,
+ .d_flags = D_TTY | D_NEEDGIANT,
+};
+#endif
+
+#ifdef NETGRAPH
+#if __FreeBSD_version >= 500000
+static int ng_cx_constructor (node_p node)
+{
+ drv_t *d = NG_NODE_PRIVATE (node);
+#else
+static int ng_cx_constructor (node_p *node)
+{
+ drv_t *d = (*node)->private;
+#endif
+ CX_DEBUG (d, ("Constructor\n"));
+ return EINVAL;
+}
+
+static int ng_cx_newhook (node_p node, hook_p hook, const char *name)
+{
+ int s;
+#if __FreeBSD_version >= 500000
+ drv_t *d = NG_NODE_PRIVATE (node);
+#else
+ drv_t *d = node->private;
+#endif
+
+ if (d->chan->mode == M_ASYNC)
+ return EINVAL;
+
+ /* Attach debug hook */
+ if (strcmp (name, NG_CX_HOOK_DEBUG) == 0) {
+#if __FreeBSD_version >= 500000
+ NG_HOOK_SET_PRIVATE (hook, NULL);
+#else
+ hook->private = 0;
+#endif
+ d->debug_hook = hook;
+ return 0;
+ }
+
+ /* Check for raw hook */
+ if (strcmp (name, NG_CX_HOOK_RAW) != 0)
+ return EINVAL;
+
+#if __FreeBSD_version >= 500000
+ NG_HOOK_SET_PRIVATE (hook, d);
+#else
+ hook->private = d;
+#endif
+ d->hook = hook;
+ s = splhigh ();
+ cx_up (d);
+ splx (s);
+ return 0;
+}
+
+static int print_modems (char *s, cx_chan_t *c, int need_header)
+{
+ int status = cx_modem_status (c->sys);
+ int length = 0;
+
+ if (need_header)
+ length += sprintf (s + length, " LE DTR DSR RTS CTS CD\n");
+ length += sprintf (s + length, "%4s %4s %4s %4s %4s %4s\n",
+ status & TIOCM_LE ? "On" : "-",
+ status & TIOCM_DTR ? "On" : "-",
+ status & TIOCM_DSR ? "On" : "-",
+ status & TIOCM_RTS ? "On" : "-",
+ status & TIOCM_CTS ? "On" : "-",
+ status & TIOCM_CD ? "On" : "-");
+ return length;
+}
+
+static int print_stats (char *s, cx_chan_t *c, int need_header)
+{
+ int length = 0;
+
+ if (need_header)
+ length += sprintf (s + length, " Rintr Tintr Mintr Ibytes Ipkts Ierrs Obytes Opkts Oerrs\n");
+ length += sprintf (s + length, "%7ld %7ld %7ld %8ld %7ld %7ld %8ld %7ld %7ld\n",
+ c->rintr, c->tintr, c->mintr, c->ibytes, c->ipkts,
+ c->ierrs, c->obytes, c->opkts, c->oerrs);
+ return length;
+}
+
+static int print_chan (char *s, cx_chan_t *c)
+{
+ drv_t *d = c->sys;
+ int length = 0;
+
+ length += sprintf (s + length, "cx%d", c->board->num * NCHAN + c->num);
+ if (d->chan->debug)
+ length += sprintf (s + length, " debug=%d", d->chan->debug);
+
+ if (cx_get_baud (c))
+ length += sprintf (s + length, " %ld", cx_get_baud (c));
+ else
+ length += sprintf (s + length, " extclock");
+
+ if (c->mode == M_HDLC) {
+ length += sprintf (s + length, " dpll=%s", cx_get_dpll (c) ? "on" : "off");
+ length += sprintf (s + length, " nrzi=%s", cx_get_nrzi (c) ? "on" : "off");
+ }
+
+ length += sprintf (s + length, " loop=%s", cx_get_loop (c) ? "on\n" : "off\n");
+ return length;
+}
+
+#if __FreeBSD_version >= 500000
+static int ng_cx_rcvmsg (node_p node, item_p item, hook_p lasthook)
+{
+ drv_t *d = NG_NODE_PRIVATE (node);
+ struct ng_mesg *msg;
+#else
+static int ng_cx_rcvmsg (node_p node, struct ng_mesg *msg,
+ const char *retaddr, struct ng_mesg **rptr)
+{
+ drv_t *d = node->private;
+#endif
+ struct ng_mesg *resp = NULL;
+ int error = 0;
+
+ if (!d)
+ return EINVAL;
+
+ CX_DEBUG (d, ("Rcvmsg\n"));
+#if __FreeBSD_version >= 500000
+ NGI_GET_MSG (item, msg);
+#endif
+ switch (msg->header.typecookie) {
+ default:
+ error = EINVAL;
+ break;
+
+ case NGM_CX_COOKIE:
+ printf ("Don't forget to implement\n");
+ error = EINVAL;
+ break;
+
+ case NGM_GENERIC_COOKIE:
+ switch (msg->header.cmd) {
+ default:
+ error = EINVAL;
+ break;
+
+ case NGM_TEXT_STATUS: {
+ char *s;
+ int l = 0;
+ int dl = sizeof (struct ng_mesg) + 730;
+
+#if __FreeBSD_version >= 500000
+ NG_MKRESPONSE (resp, msg, dl, M_NOWAIT);
+ if (! resp) {
+ error = ENOMEM;
+ break;
+ }
+#else
+ MALLOC (resp, struct ng_mesg *, dl,
+ M_NETGRAPH, M_NOWAIT);
+ if (! resp) {
+ error = ENOMEM;
+ break;
+ }
+#endif
+ bzero (resp, dl);
+ s = (resp)->data;
+ l += print_chan (s + l, d->chan);
+ l += print_stats (s + l, d->chan, 1);
+ l += print_modems (s + l, d->chan, 1);
+#if __FreeBSD_version < 500000
+ (resp)->header.version = NG_VERSION;
+ (resp)->header.arglen = strlen (s) + 1;
+ (resp)->header.token = msg->header.token;
+ (resp)->header.typecookie = NGM_CX_COOKIE;
+ (resp)->header.cmd = msg->header.cmd;
+#endif
+ strncpy ((resp)->header.cmdstr, "status", NG_CMDSTRLEN);
+ }
+ break;
+ }
+ break;
+ }
+#if __FreeBSD_version >= 500000
+ NG_RESPOND_MSG (error, node, item, resp);
+ NG_FREE_MSG (msg);
+#else
+ *rptr = resp;
+ FREE (msg, M_NETGRAPH);
+#endif
+ return error;
+}
+
+#if __FreeBSD_version >= 500000
+static int ng_cx_rcvdata (hook_p hook, item_p item)
+{
+ drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE(hook));
+ struct mbuf *m;
+ meta_p meta;
+#else
+static int ng_cx_rcvdata (hook_p hook, struct mbuf *m, meta_p meta)
+{
+ drv_t *d = hook->node->private;
+#endif
+ struct ifqueue *q;
+ int s;
+
+#if __FreeBSD_version >= 500000
+ NGI_GET_M (item, m);
+ NGI_GET_META (item, meta);
+ NG_FREE_ITEM (item);
+ if (! NG_HOOK_PRIVATE (hook) || ! d) {
+ NG_FREE_M (m);
+ NG_FREE_META (meta);
+#else
+ if (! hook->private || ! d) {
+ NG_FREE_DATA (m,meta);
+#endif
+ return ENETDOWN;
+ }
+ q = (meta && meta->priority > 0) ? &d->hi_queue : &d->lo_queue;
+ s = splhigh ();
+#if __FreeBSD_version >= 500000
+ IF_LOCK (q);
+ if (_IF_QFULL (q)) {
+ _IF_DROP (q);
+ IF_UNLOCK (q);
+ splx (s);
+ NG_FREE_M (m);
+ NG_FREE_META (meta);
+ return ENOBUFS;
+ }
+ _IF_ENQUEUE (q, m);
+ IF_UNLOCK (q);
+#else
+ if (IF_QFULL (q)) {
+ IF_DROP (q);
+ splx (s);
+ NG_FREE_DATA (m, meta);
+ return ENOBUFS;
+ }
+ IF_ENQUEUE (q, m);
+#endif
+ cx_start (d);
+ splx (s);
+ return 0;
+}
+
+static int ng_cx_rmnode (node_p node)
+{
+#if __FreeBSD_version >= 500000
+ drv_t *d = NG_NODE_PRIVATE (node);
+
+ CX_DEBUG (d, ("Rmnode\n"));
+ if (d && d->running) {
+ int s = splhigh ();
+ cx_down (d);
+ splx (s);
+ }
+#ifdef KLD_MODULE
+ if (node->nd_flags & NG_REALLY_DIE) {
+ NG_NODE_SET_PRIVATE (node, NULL);
+ NG_NODE_UNREF (node);
+ }
+ node->nd_flags &= ~NG_INVALID;
+#endif
+#else /* __FreeBSD_version < 500000 */
+ drv_t *d = node->private;
+ int s;
+
+ s = splhigh ();
+ cx_down (d);
+ splx (s);
+ node->flags |= NG_INVALID;
+ ng_cutlinks (node);
+#ifdef KLD_MODULE
+ ng_unname (node);
+ ng_unref (node);
+#else
+ node->flags &= ~NG_INVALID;
+#endif
+#endif
+ return 0;
+}
+
+static void ng_cx_watchdog (void *arg)
+{
+ drv_t *d = arg;
+
+ if (d->timeout == 1)
+ cx_watchdog (d);
+ if (d->timeout)
+ d->timeout--;
+ d->timeout_handle = timeout (ng_cx_watchdog, d, hz);
+}
+
+static int ng_cx_connect (hook_p hook)
+{
+#if __FreeBSD_version >= 500000
+ drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
+#else
+ drv_t *d = hook->node->private;
+#endif
+
+ d->timeout_handle = timeout (ng_cx_watchdog, d, hz);
+ return 0;
+}
+
+static int ng_cx_disconnect (hook_p hook)
+{
+#if __FreeBSD_version >= 500000
+ drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
+#else
+ drv_t *d = hook->node->private;
+#endif
+ int s;
+
+ s = splhigh ();
+#if __FreeBSD_version >= 500000
+ if (NG_HOOK_PRIVATE (hook))
+#else
+ if (hook->private)
+#endif
+ cx_down (d);
+ splx (s);
+ untimeout (ng_cx_watchdog, d, d->timeout_handle);
+ return 0;
+}
+#endif /*NETGRAPH*/
+
+#ifdef KLD_MODULE
+#if __FreeBSD_version < 400000
+/*
+ * Function called when loading the driver.
+ */
+static int cx_load (void)
+{
+ int i;
+
+ for (i=0;i<NCX; ++i) {
+ struct isa_device id = {-1, &cxdriver, -1, 0, -1, 0, 0, (inthand2_t *)cx_intr, i, 0, 0, 0, 0 ,0 ,1 ,0 ,0};
+
+ disable_intr();
+ if (!cx_probe (&id)) {
+ enable_intr();
+ break;
+ }
+ cx_attach (&id);
+ register_intr ((adapter [i])->irq, 0, 0, (inthand2_t*) cx_intr,
+ &net_imask, id.id_unit);
+ enable_intr();
+ }
+ if (!i) {
+ /* Deactivate the timeout routine. And soft interrupt*/
+ untimeout (cx_timeout, 0, timeout_handle);
+ unregister_swi (SWI_TTY, cx_softintr);
+ return ENXIO;
+ }
+ return 0;
+}
+
+/*
+ * Function called when unloading the driver.
+ */
+static int cx_unload (void)
+{
+ int i, s;
+
+ /* Check if the device is busy (open). */
+ for (i=0; i<NCX*NCHAN; ++i) {
+ drv_t *d = channel[i];
+ cx_chan_t *c;
+
+ if (!d || (c=d->chan)->type == T_NONE)
+ continue;
+ if (d->lock)
+ return EBUSY;
+ if (c->mode == M_ASYNC && (d->tty.t_state & TS_ISOPEN) &&
+ (d->open_dev|0x2))
+ return EBUSY;
+ if (d->running)
+ return EBUSY;
+
+ }
+
+ s = splhigh ();
+
+ /* Deactivate the timeout routine. And soft interrupt*/
+ for (i=0; i<NCX; ++i) {
+ cx_board_t *b = adapter [i];
+
+ if (!b || ! b->port)
+ continue;
+ untimeout (cx_timeout, 0, timeout_handle);
+ unregister_swi (SWI_TTY, cx_softintr);
+ break;
+ }
+
+ for (i=0; i<NCX*NCHAN; ++i) {
+ drv_t *d = channel[i];
+ cx_chan_t *c;
+
+ if (!d || (c=d->chan)->type == T_NONE)
+ continue;
+
+ if (d->dtr_timeout_handle.callout)
+ untimeout (cx_dtrwakeup, d, d->dtr_timeout_handle);
+ if (d->dcd_timeout_handle.callout)
+ untimeout (cx_carrier, c, d->dcd_timeout_handle);
+ }
+
+ /* Close all active boards. */
+ for (i=0; i<NCX; ++i) {
+ cx_board_t *b = adapter [i];
+
+ if (!b || ! b->port)
+ continue;
+
+ cx_close_board (b);
+ }
+
+ for (i=0; i<NCX; ++i) {
+ cx_board_t *b = adapter [i];
+
+ if (!b || ! b->port)
+ continue;
+
+ if (led_timo[i].callout)
+ untimeout (cx_led_off, b, led_timo[i]);
+ }
+
+ /* OK to unload the driver, unregister the interrupt first. */
+ for (i=0; i<NCX; ++i) {
+ cx_board_t *b = adapter [i];
+
+ if (!b || ! b->port)
+ continue;
+ /* Disable the interrupt request. */
+ disable_intr();
+ unregister_intr (b->irq, (inthand2_t *)cx_intr);
+ isa_dma_release (b->dma);
+ enable_intr();
+ }
+ splx (s);
+
+ s = splhigh ();
+ /* Detach the interfaces, free buffer memory. */
+ for (i=0; i<NCX*NCHAN; ++i) {
+ drv_t *d = channel[i];
+ cx_chan_t *c;
+
+ if (!d || (c=d->chan)->type == T_NONE)
+ continue;
+
+#ifndef NETGRAPH
+#if NBPFILTER > 0
+ /* Detach from the packet filter list of interfaces. */
+ {
+ struct bpf_if *q, **b = &bpf_iflist;
+
+ while ((q = *b)) {
+ if (q->bif_ifp == d->pp.pp_if) {
+ *b = q->bif_next;
+ free (q, M_DEVBUF);
+ }
+ b = &(q->bif_next);
+ }
+ }
+#endif /* NBPFILTER */
+ /* Detach from the sync PPP list. */
+ sppp_detach (&d->pp.pp_if);
+
+ /* Detach from the system list of interfaces. */
+ {
+ struct ifaddr *ifa;
+ TAILQ_FOREACH (ifa, &d->pp.pp_if.if_addrhead, ifa_link) {
+ TAILQ_REMOVE (&d->pp.pp_if.if_addrhead, ifa, ifa_link);
+ free (ifa, M_IFADDR);
+ }
+ TAILQ_REMOVE (&ifnet, &d->pp.pp_if, if_link);
+ }
+#endif /* !NETGRAPH */
+ /* Deallocate buffers. */
+/* free (d, M_DEVBUF);*/
+ }
+
+ for (i=0; i<NCX; ++i) {
+ cx_board_t *b = adapter [i];
+ if (!b)
+ continue;
+ adapter [b->num] = 0;
+ free (b, M_DEVBUF);
+ }
+
+ splx (s);
+
+ return 0;
+}
+
+#define devsw(a) cdevsw[major((a))]
+#endif /* __FreeBSD_version < 400000 */
+#endif /* KLD_MODULE */
+
+#if __FreeBSD_version < 400000
+#ifdef KLD_MODULE
+static int cx_modevent (module_t mod, int type, void *unused)
+{
+ dev_t dev;
+ int result;
+ static int load_count = 0;
+
+ dev = makedev (CDEV_MAJOR, 0);
+ switch (type) {
+ case MOD_LOAD:
+ if (devsw(dev))
+ return (ENXIO);
+ load_count ++;
+ cdevsw_add (&dev, &cx_cdevsw, NULL);
+ timeout_handle = timeout (cx_timeout, 0, hz*5);
+
+ /* Software interrupt. */
+ register_swi (SWI_TTY, cx_softintr);
+
+ result = cx_load ();
+ return result;
+ case MOD_UNLOAD:
+ result = cx_unload ();
+ if (result)
+ return result;
+ if (devsw(dev)&&!(load_count-1)) {
+ cdevsw_add (&dev, NULL, NULL);
+ }
+ load_count --;
+ return result;
+ case MOD_SHUTDOWN:
+ break;
+ }
+ return 0;
+}
+#endif /* KLD_MODULE */
+#else /* __FreeBSD_version >= 400000 */
+static int cx_modevent (module_t mod, int type, void *unused)
+{
+ dev_t dev;
+ static int load_count = 0;
+ struct cdevsw *cdsw;
+
+#if __FreeBSD_version >= 502103
+ dev = udev2dev (makeudev(CDEV_MAJOR, 0));
+#else
+ dev = makedev (CDEV_MAJOR, 0);
+#endif
+ switch (type) {
+ case MOD_LOAD:
+ if (dev != NODEV &&
+ (cdsw = devsw (dev)) &&
+ cdsw->d_maj == CDEV_MAJOR) {
+ printf ("Sigma driver is already in system\n");
+ return (EEXIST);
+ }
+#if __FreeBSD_version >= 500000 && defined NETGRAPH
+ if (ng_newtype (&typestruct))
+ printf ("Failed to register ng_cx\n");
+#endif
+ ++load_count;
+#if __FreeBSD_version <= 500000
+ cdevsw_add (&cx_cdevsw);
+#endif
+ timeout_handle = timeout (cx_timeout, 0, hz*5);
+ /* Software interrupt. */
+#if __FreeBSD_version < 500000
+ register_swi (SWI_TTY, cx_softintr);
+#else
+ swi_add(&tty_ithd, "tty:cx", cx_softintr, NULL, SWI_TTY, 0,
+ &cx_fast_ih);
+ swi_add(&clk_ithd, "tty:cx", cx_softintr, NULL, SWI_TTY, 0,
+ &cx_slow_ih);
+#endif
+ break;
+ case MOD_UNLOAD:
+ if (load_count == 1) {
+ printf ("Removing device entry for Sigma\n");
+#if __FreeBSD_version <= 500000
+ cdevsw_remove (&cx_cdevsw);
+#endif
+#if __FreeBSD_version >= 500000 && defined NETGRAPH
+ ng_rmtype (&typestruct);
+#endif
+ }
+ if (timeout_handle.callout)
+ untimeout (cx_timeout, 0, timeout_handle);
+#if __FreeBSD_version >= 500000
+ ithread_remove_handler (cx_fast_ih);
+ ithread_remove_handler (cx_slow_ih);
+#else
+ unregister_swi (SWI_TTY, cx_softintr);
+#endif
+ --load_count;
+ break;
+ case MOD_SHUTDOWN:
+ break;
+ }
+ return 0;
+}
+#endif /* __FreeBSD_version >= 400000 */
+
+#ifdef NETGRAPH
+static struct ng_type typestruct = {
+#if __FreeBSD_version >= 500000
+ NG_ABI_VERSION,
+#else
+ NG_VERSION,
+#endif
+ NG_CX_NODE_TYPE,
+#if __FreeBSD_version < 500000 && defined KLD_MODULE
+ cx_modevent,
+#else
+ NULL,
+#endif
+ ng_cx_constructor,
+ ng_cx_rcvmsg,
+ ng_cx_rmnode,
+ ng_cx_newhook,
+ NULL,
+ ng_cx_connect,
+ ng_cx_rcvdata,
+#if __FreeBSD_version < 500000
+ NULL,
+#endif
+ ng_cx_disconnect
+};
+
+#if __FreeBSD_version < 400000
+NETGRAPH_INIT_ORDERED (cx, &typestruct, SI_SUB_DRIVERS,\
+ SI_ORDER_MIDDLE + CDEV_MAJOR);
+#endif
+#endif /*NETGRAPH*/
+
+#if __FreeBSD_version >= 500000
+#ifdef NETGRAPH
+MODULE_DEPEND (ng_cx, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION);
+#else
+MODULE_DEPEND (isa_cx, sppp, 1, 1, 1);
+#endif
+#ifdef KLD_MODULE
+DRIVER_MODULE (cxmod, isa, cx_isa_driver, cx_devclass, cx_modevent, NULL);
+#else
+DRIVER_MODULE (cx, isa, cx_isa_driver, cx_devclass, cx_modevent, NULL);
+#endif
+#elif __FreeBSD_version >= 400000
+#ifdef NETGRAPH
+DRIVER_MODULE(cx, isa, cx_isa_driver, cx_devclass, ng_mod_event, &typestruct);
+#else
+DRIVER_MODULE(cx, isa, cx_isa_driver, cx_devclass, cx_modevent, 0);
+#endif
+#else /* __FreeBSD_version < 400000 */
+#ifdef KLD_MODULE
+#ifndef NETGRAPH
+static moduledata_t cxmod = { "cx", cx_modevent, NULL};
+DECLARE_MODULE (cx, cxmod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR);
+#endif
+#else /* KLD_MODULE */
+
+/*
+ * Now for some driver initialisation.
+ * Occurs ONCE during boot (very early).
+ * This is if we are NOT a loadable module.
+ */
+static void cx_drvinit (void *unused)
+{
+#if __FreeBSD_version < 400000
+ dev_t dev;
+
+ dev = makedev (CDEV_MAJOR, 0);
+ cdevsw_add (&dev, &cx_cdevsw, NULL);
+#else
+ cdevsw_add (&cx_cdevsw);
+#endif
+
+ /* Activate the timeout routine. */
+ timeout_handle = timeout (cx_timeout, 0, hz*5);
+
+ /* Software interrupt. */
+ register_swi (SWI_TTY, cx_softintr);
+#ifdef NETGRAPH
+#if 0
+ /* Register our node type in netgraph */
+ if (ng_newtype (&typestruct))
+ printf ("Failed to register ng_cx\n");
+#endif
+#endif
+}
+
+SYSINIT (cxdev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE+CDEV_MAJOR, cx_drvinit, 0)
+
+#endif /* KLD_MODULE */
+#endif /* __FreeBSD_version < 400000 */
+#endif /* NCX */
diff --git a/sys/modules/ctau/Makefile b/sys/modules/ctau/Makefile
new file mode 100644
index 000000000000..8b9d139356e1
--- /dev/null
+++ b/sys/modules/ctau/Makefile
@@ -0,0 +1,37 @@
+# Cronyx Id: sys.modules.ct.Makefile,v 1.1.2.2 2003/12/08 15:39:01 rik Exp $
+# $FreeBSD$
+.PATH: ${.CURDIR}/../../dev/ctau
+KMOD= if_ct
+SRCS= if_ct.c ctddk.c ctau.c opt_netgraph.h opt_ng_cronyx.h bpf.h sppp.h \
+ device_if.h bus_if.h isa_if.h
+
+NBPF?= 0
+NCTAU?= 3
+PROTOS?= -DINET
+NG_CRONYX?= 0
+NETGRAPH= ${NG_CRONYX}
+
+CFLAGS+= ${PROTOS}
+CLEANFILES+= opt_ng_cronyx.h opt_netgraph.h bpf.h ctddk.c ctau.c sppp.h
+
+opt_netgraph.h:
+ echo "#define NETGRAPH $(NETGRAPH)" > opt_netgraph.h
+
+opt_ng_cronyx.h:
+.if ${NG_CRONYX} != 0
+ echo "#define NETGRAPH_CRONYX 1" > opt_ng_cronyx.h
+.else
+ echo "" > opt_ng_cronyx.h
+.endif
+
+sppp.h:
+.if ${NG_CRONYX} == 0
+ echo "#define NSPPP 1" > sppp.h
+.else
+ echo "#define NSPPP 0" > sppp.h
+.endif
+
+bpf.h:
+ echo "#define NBPF ${NBPF}" > bpf.h
+
+.include <bsd.kmod.mk>
diff --git a/sys/modules/netgraph/vlan/Makefile b/sys/modules/netgraph/vlan/Makefile
new file mode 100644
index 000000000000..e6cdf0dbbc16
--- /dev/null
+++ b/sys/modules/netgraph/vlan/Makefile
@@ -0,0 +1,6 @@
+# $FreeBSD$
+
+KMOD= ng_vlan
+SRCS= ng_vlan.c
+
+.include <bsd.kmod.mk>
diff --git a/sys/netgraph/ng_vlan.c b/sys/netgraph/ng_vlan.c
new file mode 100644
index 000000000000..eb36f7b012f1
--- /dev/null
+++ b/sys/netgraph/ng_vlan.c
@@ -0,0 +1,444 @@
+/*-
+ * Copyright (c) 2003 IPNET Internet Communication Company
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * Author: Ruslan Ermilov <ru@FreeBSD.org>
+ *
+ * $FreeBSD$
+ */
+
+#include <sys/param.h>
+#include <sys/errno.h>
+#include <sys/kernel.h>
+#include <sys/malloc.h>
+#include <sys/mbuf.h>
+#include <sys/queue.h>
+#include <sys/socket.h>
+#include <sys/systm.h>
+
+#include <net/ethernet.h>
+#include <net/if.h>
+#include <net/if_vlan_var.h>
+
+#include <netgraph/ng_message.h>
+#include <netgraph/ng_parse.h>
+#include <netgraph/ng_vlan.h>
+#include <netgraph/netgraph.h>
+
+static ng_constructor_t ng_vlan_constructor;
+static ng_rcvmsg_t ng_vlan_rcvmsg;
+static ng_shutdown_t ng_vlan_shutdown;
+static ng_newhook_t ng_vlan_newhook;
+static ng_rcvdata_t ng_vlan_rcvdata;
+static ng_disconnect_t ng_vlan_disconnect;
+
+/* Parse type for struct ng_vlan_filter. */
+static const struct ng_parse_struct_field ng_vlan_filter_fields[] =
+ NG_VLAN_FILTER_FIELDS;
+static const struct ng_parse_type ng_vlan_filter_type = {
+ &ng_parse_struct_type,
+ &ng_vlan_filter_fields
+};
+
+static int
+ng_vlan_getTableLength(const struct ng_parse_type *type,
+ const u_char *start, const u_char *buf)
+{
+ const struct ng_vlan_table *const table =
+ (const struct ng_vlan_table *)(buf - sizeof(u_int32_t));
+
+ return table->n;
+}
+
+/* Parse type for struct ng_vlan_table. */
+static const struct ng_parse_array_info ng_vlan_table_array_info = {
+ &ng_vlan_filter_type,
+ ng_vlan_getTableLength
+};
+static const struct ng_parse_type ng_vlan_table_array_type = {
+ &ng_parse_array_type,
+ &ng_vlan_table_array_info
+};
+static const struct ng_parse_struct_field ng_vlan_table_fields[] =
+ NG_VLAN_TABLE_FIELDS;
+static const struct ng_parse_type ng_vlan_table_type = {
+ &ng_parse_struct_type,
+ &ng_vlan_table_fields
+};
+
+/* List of commands and how to convert arguments to/from ASCII. */
+static const struct ng_cmdlist ng_vlan_cmdlist[] = {
+ {
+ NGM_VLAN_COOKIE,
+ NGM_VLAN_ADD_FILTER,
+ "addfilter",
+ &ng_vlan_filter_type,
+ NULL
+ },
+ {
+ NGM_VLAN_COOKIE,
+ NGM_VLAN_DEL_FILTER,
+ "delfilter",
+ &ng_parse_hookbuf_type,
+ NULL
+ },
+ {
+ NGM_VLAN_COOKIE,
+ NGM_VLAN_GET_TABLE,
+ "gettable",
+ NULL,
+ &ng_vlan_table_type
+ },
+ { 0 }
+};
+
+static struct ng_type ng_vlan_typestruct = {
+ NG_ABI_VERSION,
+ NG_VLAN_NODE_TYPE,
+ NULL,
+ ng_vlan_constructor,
+ ng_vlan_rcvmsg,
+ ng_vlan_shutdown,
+ ng_vlan_newhook,
+ NULL,
+ NULL,
+ ng_vlan_rcvdata,
+ ng_vlan_disconnect,
+ ng_vlan_cmdlist
+};
+NETGRAPH_INIT(vlan, &ng_vlan_typestruct);
+
+struct filter {
+ LIST_ENTRY(filter) next;
+ u_int16_t vlan;
+ hook_p hook;
+};
+
+#define HASHSIZE 16
+#define HASH(id) ((((id) >> 8) ^ ((id) >> 4) ^ (id)) & 0x0f)
+LIST_HEAD(filterhead, filter);
+
+typedef struct {
+ hook_p downstream_hook;
+ hook_p nomatch_hook;
+ struct filterhead hashtable[HASHSIZE];
+ u_int32_t nent;
+} *priv_p;
+
+static struct filter *
+ng_vlan_findentry(priv_p priv, u_int16_t vlan)
+{
+ struct filterhead *chain = &priv->hashtable[HASH(vlan)];
+ struct filter *f;
+
+ LIST_FOREACH(f, chain, next)
+ if (f->vlan == vlan)
+ return (f);
+ return (NULL);
+}
+
+static int
+ng_vlan_constructor(node_p node)
+{
+ priv_p priv;
+ int i;
+
+ MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
+ if (priv == NULL)
+ return (ENOMEM);
+ for (i = 0; i < HASHSIZE; i++)
+ LIST_INIT(&priv->hashtable[i]);
+ NG_NODE_SET_PRIVATE(node, priv);
+ return (0);
+}
+
+static int
+ng_vlan_newhook(node_p node, hook_p hook, const char *name)
+{
+ const priv_p priv = NG_NODE_PRIVATE(node);
+
+ if (strcmp(name, NG_VLAN_HOOK_DOWNSTREAM) == 0)
+ priv->downstream_hook = hook;
+ else if (strcmp(name, NG_VLAN_HOOK_NOMATCH) == 0)
+ priv->nomatch_hook = hook;
+ else {
+ /*
+ * Any other hook name is valid and can
+ * later be associated with a filter rule.
+ */
+ }
+ NG_HOOK_SET_PRIVATE(hook, NULL);
+ return (0);
+}
+
+static int
+ng_vlan_rcvmsg(node_p node, item_p item, hook_p lasthook)
+{
+ const priv_p priv = NG_NODE_PRIVATE(node);
+ int error = 0;
+ struct ng_mesg *msg, *resp = NULL;
+ struct ng_vlan_filter *vf;
+ struct filter *f;
+ hook_p hook;
+ struct ng_vlan_table *t;
+ int i;
+
+ NGI_GET_MSG(item, msg);
+ /* Deal with message according to cookie and command. */
+ switch (msg->header.typecookie) {
+ case NGM_VLAN_COOKIE:
+ switch (msg->header.cmd) {
+ case NGM_VLAN_ADD_FILTER:
+ /* Check that message is long enough. */
+ if (msg->header.arglen != sizeof(*vf)) {
+ error = EINVAL;
+ break;
+ }
+ vf = (struct ng_vlan_filter *)msg->data;
+ /* Sanity check the VLAN ID value. */
+ if (vf->vlan & ~EVL_VLID_MASK) {
+ error = EINVAL;
+ break;
+ }
+ /* Check that a referenced hook exists. */
+ hook = ng_findhook(node, vf->hook);
+ if (hook == NULL) {
+ error = ENOENT;
+ break;
+ }
+ /* And is not one of the special hooks. */
+ if (hook == priv->downstream_hook ||
+ hook == priv->nomatch_hook) {
+ error = EINVAL;
+ break;
+ }
+ /* And is not already in service. */
+ if (NG_HOOK_PRIVATE(hook) != NULL) {
+ error = EEXIST;
+ break;
+ }
+ /* Check we don't already trap this VLAN. */
+ if (ng_vlan_findentry(priv, vf->vlan)) {
+ error = EEXIST;
+ break;
+ }
+ /* Create filter. */
+ MALLOC(f, struct filter *, sizeof(*f),
+ M_NETGRAPH, M_NOWAIT | M_ZERO);
+ if (f == NULL) {
+ error = ENOMEM;
+ break;
+ }
+ /* Link filter and hook together. */
+ f->hook = hook;
+ f->vlan = vf->vlan;
+ NG_HOOK_SET_PRIVATE(hook, f);
+ /* Register filter in a hash table. */
+ LIST_INSERT_HEAD(
+ &priv->hashtable[HASH(f->vlan)], f, next);
+ priv->nent++;
+ break;
+ case NGM_VLAN_DEL_FILTER:
+ /* Check that message is long enough. */
+ if (msg->header.arglen != NG_HOOKSIZ) {
+ error = EINVAL;
+ break;
+ }
+ /* Check that hook exists and is active. */
+ hook = ng_findhook(node, (char *)msg->data);
+ if (hook == NULL ||
+ (f = NG_HOOK_PRIVATE(hook)) == NULL) {
+ error = ENOENT;
+ break;
+ }
+ /* Purge a rule that refers to this hook. */
+ NG_HOOK_SET_PRIVATE(hook, NULL);
+ LIST_REMOVE(f, next);
+ priv->nent--;
+ FREE(f, M_NETGRAPH);
+ break;
+ case NGM_VLAN_GET_TABLE:
+ NG_MKRESPONSE(resp, msg, sizeof(*t) +
+ priv->nent * sizeof(*t->filter), M_NOWAIT);
+ if (resp == NULL) {
+ error = ENOMEM;
+ break;
+ }
+ t = (struct ng_vlan_table *)resp->data;
+ t->n = priv->nent;
+ vf = &t->filter[0];
+ for (i = 0; i < HASHSIZE; i++) {
+ LIST_FOREACH(f, &priv->hashtable[i], next) {
+ vf->vlan = f->vlan;
+ strncpy(vf->hook, NG_HOOK_NAME(f->hook),
+ NG_HOOKSIZ);
+ vf++;
+ }
+ }
+ break;
+ default: /* Unknown command. */
+ error = EINVAL;
+ break;
+ }
+ break;
+ default: /* Unknown type cookie. */
+ error = EINVAL;
+ break;
+ }
+ NG_RESPOND_MSG(error, node, item, resp);
+ NG_FREE_MSG(msg);
+ return (error);
+}
+
+static int
+ng_vlan_rcvdata(hook_p hook, item_p item)
+{
+ const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
+ struct ether_header *eh;
+ struct ether_vlan_header *evl;
+ int error;
+ u_int16_t vlan;
+ struct mbuf *m;
+ struct m_tag *mtag;
+ struct filter *f;
+
+ /* Make sure we have an entire header. */
+ NGI_GET_M(item, m);
+ if (m->m_len < sizeof(*eh) &&
+ (m = m_pullup(m, sizeof(*eh))) == NULL) {
+ NG_FREE_ITEM(item);
+ return (EINVAL);
+ }
+ eh = mtod(m, struct ether_header *);
+ if (hook == priv->downstream_hook) {
+ /*
+ * If from downstream, select between a match hook
+ * or the nomatch hook.
+ */
+ mtag = m_tag_locate(m, MTAG_VLAN, MTAG_VLAN_TAG, NULL);
+ if (mtag != NULL || eh->ether_type == htons(ETHERTYPE_VLAN)) {
+ if (mtag != NULL) {
+ /*
+ * Packet is tagged, m contains a normal
+ * Ethernet frame; tag is stored out-of-band.
+ */
+ vlan = EVL_VLANOFTAG(VLAN_TAG_VALUE(mtag));
+ (void)&evl; /* XXX silence GCC */
+ } else {
+ if (m->m_len < sizeof(*evl) &&
+ (m = m_pullup(m, sizeof(*evl))) == NULL) {
+ NG_FREE_ITEM(item);
+ return (EINVAL);
+ }
+ evl = mtod(m, struct ether_vlan_header *);
+ vlan = EVL_VLANOFTAG(ntohs(evl->evl_tag));
+ }
+ if ((f = ng_vlan_findentry(priv, vlan)) != NULL) {
+ if (mtag != NULL)
+ m_tag_delete(m, mtag);
+ else {
+ evl->evl_encap_proto = evl->evl_proto;
+ bcopy(mtod(m, caddr_t),
+ mtod(m, caddr_t) +
+ ETHER_VLAN_ENCAP_LEN,
+ ETHER_HDR_LEN);
+ m_adj(m, ETHER_VLAN_ENCAP_LEN);
+ }
+ }
+ } else
+ f = NULL;
+ if (f != NULL)
+ NG_FWD_NEW_DATA(error, item, f->hook, m);
+ else
+ NG_FWD_NEW_DATA(error, item, priv->nomatch_hook, m);
+ } else {
+ /*
+ * It is heading towards the downstream.
+ * If from nomatch, pass it unmodified.
+ * Otherwise, do the VLAN encapsulation.
+ */
+ if (hook != priv->nomatch_hook) {
+ if ((f = NG_HOOK_PRIVATE(hook)) == NULL) {
+ NG_FREE_ITEM(item);
+ NG_FREE_M(m);
+ return (EOPNOTSUPP);
+ }
+ M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, M_DONTWAIT);
+ /* M_PREPEND takes care of m_len and m_pkthdr.len. */
+ if (m == NULL || (m->m_len < sizeof(*evl) &&
+ (m = m_pullup(m, sizeof(*evl))) == NULL)) {
+ NG_FREE_ITEM(item);
+ return (ENOMEM);
+ }
+ /*
+ * Transform the Ethernet header into an Ethernet header
+ * with 802.1Q encapsulation.
+ */
+ bcopy(mtod(m, char *) + ETHER_VLAN_ENCAP_LEN,
+ mtod(m, char *), ETHER_HDR_LEN);
+ evl = mtod(m, struct ether_vlan_header *);
+ evl->evl_proto = evl->evl_encap_proto;
+ evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
+ evl->evl_tag = htons(f->vlan);
+ }
+ NG_FWD_NEW_DATA(error, item, priv->downstream_hook, m);
+ }
+ return (error);
+}
+
+static int
+ng_vlan_shutdown(node_p node)
+{
+ const priv_p priv = NG_NODE_PRIVATE(node);
+
+ NG_NODE_SET_PRIVATE(node, NULL);
+ NG_NODE_UNREF(node);
+ FREE(priv, M_NETGRAPH);
+ return (0);
+}
+
+static int
+ng_vlan_disconnect(hook_p hook)
+{
+ const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
+ struct filter *f;
+
+ if (hook == priv->downstream_hook)
+ priv->downstream_hook = NULL;
+ else if (hook == priv->nomatch_hook)
+ priv->nomatch_hook = NULL;
+ else {
+ /* Purge a rule that refers to this hook. */
+ if ((f = NG_HOOK_PRIVATE(hook)) != NULL) {
+ LIST_REMOVE(f, next);
+ priv->nent--;
+ FREE(f, M_NETGRAPH);
+ }
+ }
+ NG_HOOK_SET_PRIVATE(hook, NULL);
+ if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) &&
+ (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
+ ng_rmnode_self(NG_HOOK_NODE(hook));
+ return (0);
+}
diff --git a/sys/netgraph/ng_vlan.h b/sys/netgraph/ng_vlan.h
new file mode 100644
index 000000000000..579e3baf26ae
--- /dev/null
+++ b/sys/netgraph/ng_vlan.h
@@ -0,0 +1,75 @@
+/*-
+ * Copyright (c) 2003 IPNET Internet Communication Company
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * Author: Ruslan Ermilov <ru@FreeBSD.org>
+ *
+ * $FreeBSD$
+ */
+
+#ifndef _NETGRAPH_NG_VLAN_H_
+#define _NETGRAPH_NG_VLAN_H_
+
+/* Node type name and magic cookie. */
+#define NG_VLAN_NODE_TYPE "vlan"
+#define NGM_VLAN_COOKIE 1068486472
+
+/* Hook names. */
+#define NG_VLAN_HOOK_DOWNSTREAM "downstream"
+#define NG_VLAN_HOOK_NOMATCH "nomatch"
+
+/* Netgraph commands. */
+enum {
+ NGM_VLAN_ADD_FILTER = 1,
+ NGM_VLAN_DEL_FILTER,
+ NGM_VLAN_GET_TABLE
+};
+
+/* For NGM_VLAN_ADD_FILTER control message. */
+struct ng_vlan_filter {
+ char hook[NG_HOOKSIZ];
+ u_int16_t vlan;
+};
+
+/* Keep this in sync with the above structure definition. */
+#define NG_VLAN_FILTER_FIELDS { \
+ { "hook", &ng_parse_hookbuf_type }, \
+ { "vlan", &ng_parse_uint16_type }, \
+ { NULL } \
+}
+
+/* Structure returned by NGM_VLAN_GET_TABLE. */
+struct ng_vlan_table {
+ u_int32_t n;
+ struct ng_vlan_filter filter[0];
+};
+
+/* Keep this in sync with the above structure definition. */
+#define NG_VLAN_TABLE_FIELDS { \
+ { "n", &ng_parse_uint32_type }, \
+ { "filter", &ng_vlan_table_array_type }, \
+ { NULL } \
+}
+
+#endif /* _NETGRAPH_NG_VLAN_H_ */