2 * Cronyx-Tau adapter driver for FreeBSD.
3 * Supports PPP/HDLC and Cisco/HDLC protocol in synchronous mode,
4 * and asyncronous channels with full modem control.
5 * Keepalive protocol implemented in both Cisco and PPP modes.
7 * Copyright (C) 1994-2002 Cronyx Engineering.
8 * Author: Serge Vakulenko, <vak@cronyx.ru>
10 * Copyright (C) 1999-2004 Cronyx Engineering.
11 * Author: Roman Kurakin, <rik@cronyx.ru>
13 * This software is distributed with NO WARRANTIES, not even the implied
14 * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 * Authors grant any other persons or organisations a permission to use,
17 * modify and redistribute this software in source and binary forms,
18 * as long as this message is kept with the software, all derivative
19 * works or modified versions.
21 * Cronyx Id: if_ct.c,v 1.1.2.31 2004/06/23 17:09:13 rik Exp $
24 #include <sys/cdefs.h>
25 __FBSDID("$FreeBSD$");
27 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/kernel.h>
31 #include <sys/module.h>
33 #include <sys/sockio.h>
34 #include <sys/malloc.h>
36 #include <sys/socket.h>
37 #include <sys/sysctl.h>
39 #include <sys/errno.h>
42 #include <machine/bus.h>
44 #include <isa/isavar.h>
45 #include <sys/interrupt.h>
49 #include <machine/cpufunc.h>
50 #include <machine/cserial.h>
51 #include <machine/resource.h>
52 #include <dev/cx/machdep.h>
53 #include <dev/ctau/ctddk.h>
54 #include <dev/cx/cronyxfw.h>
55 #include "opt_ng_cronyx.h"
56 #ifdef NETGRAPH_CRONYX
57 # include "opt_netgraph.h"
58 # include <netgraph/ng_message.h>
59 # include <netgraph/netgraph.h>
60 # include <dev/ctau/ng_ct.h>
62 # include <net/if_types.h>
63 # include <net/if_sppp.h>
64 # define PP_CISCO IFF_LINK2
70 /* If we don't have Cronyx's sppp version, we don't have fr support via sppp */
75 #define CT_DEBUG(d,s) ({if (d->chan->debug) {\
76 printf ("%s: ", d->name); printf s;}})
77 #define CT_DEBUG2(d,s) ({if (d->chan->debug>1) {\
78 printf ("%s: ", d->name); printf s;}})
80 #define CT_LOCK_NAME "ctX"
82 static int ct_mpsafenet = 1;
83 TUNABLE_INT("debug.ctau.mpsafenet", &ct_mpsafenet);
84 SYSCTL_NODE(_debug, OID_AUTO, ctau, CTLFLAG_RD, 0, "Cronyx Tau-ISA Adapters");
85 SYSCTL_INT(_debug_ctau, OID_AUTO, mpsafenet, CTLFLAG_RD, &ct_mpsafenet, 0,
86 "Enable/disable MPSAFE network support for Cronyx Tau-ISA Adapters");
88 #define CT_LOCK(_bd) do { \
90 mtx_lock (&(_bd)->ct_mtx); \
92 #define CT_UNLOCK(_bd) do { \
94 mtx_unlock (&(_bd)->ct_mtx); \
96 #define CT_LOCK_ASSERT(_bd) do { \
98 mtx_assert (&(_bd)->ct_mtx, MA_OWNED); \
101 static void ct_identify __P((driver_t *, device_t));
102 static int ct_probe __P((device_t));
103 static int ct_attach __P((device_t));
104 static int ct_detach __P((device_t));
106 static device_method_t ct_isa_methods [] = {
107 DEVMETHOD(device_identify, ct_identify),
108 DEVMETHOD(device_probe, ct_probe),
109 DEVMETHOD(device_attach, ct_attach),
110 DEVMETHOD(device_detach, ct_detach),
114 typedef struct _ct_dma_mem_t {
122 typedef struct _drv_t {
130 char nodename [NG_NODELEN+1];
134 struct ifqueue queue;
135 struct ifqueue hi_queue;
137 struct callout timeout_handle;
139 struct ifqueue queue;
145 typedef struct _bdrv_t {
147 struct resource *base_res;
148 struct resource *drq_res;
149 struct resource *irq_res;
154 drv_t channel [NCHAN];
158 static driver_t ct_isa_driver = {
164 static devclass_t ct_devclass;
166 static void ct_receive (ct_chan_t *c, char *data, int len);
167 static void ct_transmit (ct_chan_t *c, void *attachment, int len);
168 static void ct_error (ct_chan_t *c, int data);
169 static void ct_up (drv_t *d);
170 static void ct_start (drv_t *d);
171 static void ct_down (drv_t *d);
172 static void ct_watchdog (drv_t *d);
174 extern struct ng_type typestruct;
176 static void ct_ifstart (struct ifnet *ifp);
177 static void ct_tlf (struct sppp *sp);
178 static void ct_tls (struct sppp *sp);
179 static void ct_ifwatchdog (struct ifnet *ifp);
180 static int ct_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data);
181 static void ct_initialize (void *softc);
184 static ct_board_t *adapter [NCTAU];
185 static drv_t *channel [NCTAU*NCHAN];
186 static struct callout led_timo [NCTAU];
187 static struct callout timeout_handle;
189 static int ct_open (struct cdev *dev, int oflags, int devtype, struct thread *td);
190 static int ct_close (struct cdev *dev, int fflag, int devtype, struct thread *td);
191 static int ct_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td);
192 static struct cdevsw ct_cdevsw = {
193 .d_version = D_VERSION,
198 .d_flags = D_NEEDGIANT,
202 * Print the mbuf chain, for debug purposes only.
204 static void printmbuf (struct mbuf *m)
207 for (; m; m=m->m_next) {
208 if (m->m_flags & M_PKTHDR)
209 printf (" HDR %d:", m->m_pkthdr.len);
210 if (m->m_flags & M_EXT)
212 printf (" %d", m->m_len);
218 * Make an mbuf from data.
220 static struct mbuf *makembuf (void *buf, u_int len)
224 MGETHDR (m, M_DONTWAIT, MT_DATA);
227 MCLGET (m, M_DONTWAIT);
228 if (! (m->m_flags & M_EXT)) {
232 m->m_pkthdr.len = m->m_len = len;
233 bcopy (buf, mtod (m, caddr_t), len);
237 static void ct_timeout (void *arg)
242 for (i = 0; i < NCTAU; ++i) {
243 if (adapter[i] == NULL)
245 for (k = 0; k < NCHAN; k++) {
246 d = channel[i * NCHAN + k];
249 if (d->chan->mode != M_G703)
252 CT_LOCK ((bdrv_t *)d->bd);
253 ct_g703_timer (d->chan);
254 CT_UNLOCK ((bdrv_t *)d->bd);
259 callout_reset (&timeout_handle, hz, ct_timeout, 0);
262 static void ct_led_off (void *arg)
265 bdrv_t *bd = ((drv_t *)b->chan->sys)->bd;
275 * Activate interupt handler from DDK.
277 static void ct_intr (void *arg)
280 ct_board_t *b = bd->board;
292 /* Turn LED off 50 msec later. */
293 callout_reset (&led_timo[b->num], hz/20, ct_led_off, b);
298 /* Pass packets in a lock-free state */
299 for (i = 0; i < NCHAN && b->chan[i].type; i++) {
300 drv_t *d = b->chan[i].sys;
302 if (!d || !d->running)
304 while (_IF_QLEN(&d->queue)) {
305 IF_DEQUEUE (&d->queue,m);
308 sppp_input (d->ifp, m);
314 static int probe_irq (ct_board_t *b, int irq)
318 /* Clear pending irq, if any. */
319 ct_probe_irq (b, -irq);
321 for (cnt=0; cnt<5; ++cnt) {
322 /* Get the mask of pending irqs, assuming they are busy.
323 * Activate the adapter on given irq. */
324 busy = ct_probe_irq (b, irq);
327 /* Get the mask of active irqs.
328 * Deactivate our irq. */
329 mask = ct_probe_irq (b, -irq);
331 if ((mask & ~busy) == 1 << irq) {
333 /* printf ("ct%d: irq %d ok, mask=0x%04x, busy=0x%04x\n",
334 b->num, irq, mask, busy); */
338 /* printf ("ct%d: irq %d not functional, mask=0x%04x, busy=0x%04x\n",
339 b->num, irq, mask, busy); */
344 static short porttab [] = {
345 0x200, 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0,
346 0x300, 0x320, 0x340, 0x360, 0x380, 0x3a0, 0x3c0, 0x3e0, 0
348 static char dmatab [] = { 7, 6, 5, 0 };
349 static char irqtab [] = { 5, 10, 11, 7, 3, 15, 12, 0 };
351 static int ct_is_free_res (device_t dev, int rid, int type, u_long start,
352 u_long end, u_long count)
354 struct resource *res;
356 if (!(res = bus_alloc_resource (dev, type, &rid, start, end, count,
360 bus_release_resource (dev, type, rid, res);
365 static void ct_identify (driver_t *driver, device_t dev)
367 u_long iobase, rescount;
371 devclass_t my_devclass;
374 if ((my_devclass = devclass_find ("ct")) == NULL)
377 devclass_get_devices (my_devclass, &devices, &devcount);
380 /* We should find all devices by our self. We could alter other
381 * devices, but we don't have a choise
383 for (i = 0; (iobase = porttab [i]) != 0; i++) {
384 if (!ct_is_free_res (dev, 0, SYS_RES_IOPORT,
385 iobase, iobase + NPORT, NPORT))
387 if (ct_probe_board (iobase, -1, -1) == 0)
391 child = BUS_ADD_CHILD (dev, ISA_ORDER_SPECULATIVE, "ct",
397 device_set_desc_copy (child, "Cronyx Tau-ISA");
398 device_set_driver (child, driver);
399 bus_set_resource (child, SYS_RES_IOPORT, 0,
402 if (devcount >= NCTAU)
406 static short porttab [] = {
407 0x200, 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0,
408 0x300, 0x320, 0x340, 0x360, 0x380, 0x3a0, 0x3c0, 0x3e0, 0
410 /* Lets check user choise.
412 for (k = 0; k < devcount; k++) {
413 if (bus_get_resource (devices[k], SYS_RES_IOPORT, 0,
414 &iobase, &rescount) != 0)
417 for (i = 0; porttab [i] != 0; i++) {
418 if (porttab [i] != iobase)
421 if (!ct_is_free_res (devices[k], 0, SYS_RES_IOPORT,
422 iobase, iobase + NPORT, NPORT))
425 if (ct_probe_board (iobase, -1, -1) == 0)
428 device_set_desc_copy (devices[k], "Cronyx Tau-ISA");
431 if (porttab [i] == 0) {
432 device_delete_child (
433 device_get_parent (devices[k]),
439 for (k = 0; k < devcount; k++) {
442 if (bus_get_resource (devices[k], SYS_RES_IOPORT, 0,
443 &iobase, &rescount) == 0)
445 for (i = 0; (iobase = porttab [i]) != 0; i++) {
446 if (porttab [i] == -1)
448 if (!ct_is_free_res (devices[k], 0, SYS_RES_IOPORT,
449 iobase, iobase + NPORT, NPORT))
451 if (ct_probe_board (iobase, -1, -1) == 0)
454 bus_set_resource (devices[k], SYS_RES_IOPORT, 0,
457 device_set_desc_copy (devices[k], "Cronyx Tau-ISA");
460 if (porttab [i] == 0) {
461 device_delete_child (
462 device_get_parent (devices[k]),
466 free (devices, M_TEMP);
472 static int ct_probe (device_t dev)
474 int unit = device_get_unit (dev);
475 u_long iobase, rescount;
477 if (!device_get_desc (dev) ||
478 strcmp (device_get_desc (dev), "Cronyx Tau-ISA"))
481 /* KASSERT ((bd != NULL), ("ct%d: NULL device softc\n", unit));*/
482 if (bus_get_resource (dev, SYS_RES_IOPORT, 0, &iobase, &rescount) != 0) {
483 printf ("ct%d: Couldn't get IOPORT\n", unit);
487 if (!ct_is_free_res (dev, 0, SYS_RES_IOPORT,
488 iobase, iobase + NPORT, NPORT)) {
489 printf ("ct%d: Resource IOPORT isn't free\n", unit);
493 if (!ct_probe_board (iobase, -1, -1)) {
494 printf ("ct%d: probing for Tau-ISA at %lx faild\n", unit, iobase);
502 ct_bus_dmamap_addr (void *arg, bus_dma_segment_t *segs, int nseg, int error)
509 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
511 *addr = segs->ds_addr;
515 ct_bus_dma_mem_alloc (int bnum, int cnum, ct_dma_mem_t *dmem)
519 error = bus_dma_tag_create (NULL, 16, 0, BUS_SPACE_MAXADDR_24BIT,
520 BUS_SPACE_MAXADDR, NULL, NULL, dmem->size, 1,
521 dmem->size, 0, NULL, NULL, &dmem->dmat);
523 if (cnum >= 0) printf ("ct%d-%d: ", bnum, cnum);
524 else printf ("ct%d: ", bnum);
525 printf ("couldn't allocate tag for dma memory\n");
528 error = bus_dmamem_alloc (dmem->dmat, (void **)&dmem->virt,
529 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &dmem->mapp);
531 if (cnum >= 0) printf ("ct%d-%d: ", bnum, cnum);
532 else printf ("ct%d: ", bnum);
533 printf ("couldn't allocate mem for dma memory\n");
534 bus_dma_tag_destroy (dmem->dmat);
537 error = bus_dmamap_load (dmem->dmat, dmem->mapp, dmem->virt,
538 dmem->size, ct_bus_dmamap_addr, &dmem->phys, 0);
540 if (cnum >= 0) printf ("ct%d-%d: ", bnum, cnum);
541 else printf ("ct%d: ", bnum);
542 printf ("couldn't load mem map for dma memory\n");
543 bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
544 bus_dma_tag_destroy (dmem->dmat);
551 ct_bus_dma_mem_free (ct_dma_mem_t *dmem)
553 bus_dmamap_unload (dmem->dmat, dmem->mapp);
554 bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
555 bus_dma_tag_destroy (dmem->dmat);
559 * The adapter is present, initialize the driver structures.
561 static int ct_attach (device_t dev)
563 bdrv_t *bd = device_get_softc (dev);
564 u_long iobase, drq, irq, rescount;
565 int unit = device_get_unit (dev);
566 char *ct_ln = CT_LOCK_NAME;
573 KASSERT ((bd != NULL), ("ct%d: NULL device softc\n", unit));
575 bus_get_resource (dev, SYS_RES_IOPORT, 0, &iobase, &rescount);
577 bd->base_res = bus_alloc_resource (dev, SYS_RES_IOPORT, &bd->base_rid,
578 iobase, iobase + NPORT, NPORT, RF_ACTIVE);
579 if (! bd->base_res) {
580 printf ("ct%d: cannot alloc base address\n", unit);
584 if (bus_get_resource (dev, SYS_RES_DRQ, 0, &drq, &rescount) != 0) {
585 for (i = 0; (drq = dmatab [i]) != 0; i++) {
586 if (!ct_is_free_res (dev, 0, SYS_RES_DRQ,
589 bus_set_resource (dev, SYS_RES_DRQ, 0, drq, 1);
593 if (dmatab[i] == 0) {
594 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
596 printf ("ct%d: Couldn't get DRQ\n", unit);
602 bd->drq_res = bus_alloc_resource (dev, SYS_RES_DRQ, &bd->drq_rid,
603 drq, drq + 1, 1, RF_ACTIVE);
605 printf ("ct%d: cannot allocate drq\n", unit);
606 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
611 if (bus_get_resource (dev, SYS_RES_IRQ, 0, &irq, &rescount) != 0) {
612 for (i = 0; (irq = irqtab [i]) != 0; i++) {
613 if (!ct_is_free_res (dev, 0, SYS_RES_IRQ,
616 bus_set_resource (dev, SYS_RES_IRQ, 0, irq, 1);
620 if (irqtab[i] == 0) {
621 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
623 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
625 printf ("ct%d: Couldn't get IRQ\n", unit);
631 bd->irq_res = bus_alloc_resource (dev, SYS_RES_IRQ, &bd->irq_rid,
632 irq, irq + 1, 1, RF_ACTIVE);
634 printf ("ct%d: Couldn't allocate irq\n", unit);
635 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
637 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
642 b = malloc (sizeof (ct_board_t), M_DEVBUF, M_WAITOK);
644 printf ("ct:%d: Couldn't allocate memory\n", unit);
648 bzero (b, sizeof(ct_board_t));
650 if (! ct_open_board (b, unit, iobase, irq, drq)) {
651 printf ("ct%d: error loading firmware\n", unit);
653 bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid,
655 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
657 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
664 ct_ln[2] = '0' + unit;
665 mtx_init (&bd->ct_mtx, ct_ln, MTX_NETWORK_LOCK, MTX_DEF|MTX_RECURSE);
666 if (! probe_irq (b, irq)) {
667 printf ("ct%d: irq %ld not functional\n", unit, irq);
671 bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid,
673 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
675 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
677 mtx_destroy (&bd->ct_mtx);
681 callout_init (&led_timo[unit], ct_mpsafenet ? CALLOUT_MPSAFE : 0);
683 if (bus_setup_intr (dev, bd->irq_res,
684 INTR_TYPE_NET|(ct_mpsafenet?INTR_MPSAFE:0),
685 NULL, ct_intr, bd, &bd->intrhand)) {
686 printf ("ct%d: Can't setup irq %ld\n", unit, irq);
690 bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid,
692 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
694 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
696 mtx_destroy (&bd->ct_mtx);
702 ct_init_board (b, b->num, b->port, irq, drq, b->type, b->osc);
703 ct_setup_board (b, 0, 0, 0);
706 printf ("ct%d: <Cronyx-%s>, clock %s MHz\n", b->num, b->name,
707 b->osc == 20000000 ? "20" : "16.384");
709 for (c = b->chan; c < b->chan + NCHAN; ++c) {
710 d = &bd->channel[c->num];
711 d->dmamem.size = sizeof(ct_buf_t);
712 if (! ct_bus_dma_mem_alloc (unit, c->num, &d->dmamem))
718 channel [b->num*NCHAN + c->num] = d;
719 sprintf (d->name, "ct%d.%d", b->num, c->num);
722 if (ng_make_node_common (&typestruct, &d->node) != 0) {
723 printf ("%s: cannot make common node\n", d->name);
724 channel [b->num*NCHAN + c->num] = 0;
726 ct_bus_dma_mem_free (&d->dmamem);
729 NG_NODE_SET_PRIVATE (d->node, d);
730 sprintf (d->nodename, "%s%d", NG_CT_NODE_TYPE,
731 c->board->num*NCHAN + c->num);
732 if (ng_name_node (d->node, d->nodename)) {
733 printf ("%s: cannot name node\n", d->nodename);
734 NG_NODE_UNREF (d->node);
735 channel [b->num*NCHAN + c->num] = 0;
737 ct_bus_dma_mem_free (&d->dmamem);
740 d->queue.ifq_maxlen = IFQ_MAXLEN;
741 d->hi_queue.ifq_maxlen = IFQ_MAXLEN;
742 mtx_init (&d->queue.ifq_mtx, "ct_queue", NULL, MTX_DEF);
743 mtx_init (&d->hi_queue.ifq_mtx, "ct_queue_hi", NULL, MTX_DEF);
744 callout_init (&d->timeout_handle,
745 ct_mpsafenet ? CALLOUT_MPSAFE : 0);
747 d->ifp = if_alloc(IFT_PPP);
748 if (d->ifp == NULL) {
749 printf ("%s: cannot if_alloc common interface\n",
751 channel [b->num*NCHAN + c->num] = 0;
753 ct_bus_dma_mem_free (&d->dmamem);
756 d->ifp->if_softc = d;
757 if_initname (d->ifp, "ct", b->num * NCHAN + c->num);
758 d->ifp->if_mtu = PP_MTU;
759 d->ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
761 d->ifp->if_flags |= IFF_NEEDSGIANT;
762 d->ifp->if_ioctl = ct_sioctl;
763 d->ifp->if_start = ct_ifstart;
764 d->ifp->if_watchdog = ct_ifwatchdog;
765 d->ifp->if_init = ct_initialize;
766 d->queue.ifq_maxlen = NBUF;
767 mtx_init (&d->queue.ifq_mtx, "ct_queue", NULL, MTX_DEF);
768 sppp_attach (d->ifp);
770 IFP2SP(d->ifp)->pp_tlf = ct_tlf;
771 IFP2SP(d->ifp)->pp_tls = ct_tls;
772 /* If BPF is in the kernel, call the attach for it.
773 * Header size is 4 bytes. */
774 bpfattach (d->ifp, DLT_PPP, 4);
777 ct_start_chan (c, d->dmamem.virt, d->dmamem.phys);
778 ct_register_receive (c, &ct_receive);
779 ct_register_transmit (c, &ct_transmit);
780 ct_register_error (c, &ct_error);
782 d->devt = make_dev (&ct_cdevsw, b->num*NCHAN+c->num, UID_ROOT,
783 GID_WHEEL, 0600, "ct%d", b->num*NCHAN+c->num);
790 static int ct_detach (device_t dev)
792 bdrv_t *bd = device_get_softc (dev);
793 ct_board_t *b = bd->board;
797 KASSERT (mtx_initialized (&bd->ct_mtx), ("ct mutex not initialized"));
801 /* Check if the device is busy (open). */
802 for (c = b->chan; c < b->chan + NCHAN; ++c) {
803 drv_t *d = (drv_t*) c->sys;
805 if (!d || !d->chan->type)
815 /* Deactivate the timeout routine. */
816 callout_stop (&led_timo[b->num]);
820 bus_teardown_intr (dev, bd->irq_res, bd->intrhand);
821 bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid, bd->irq_res);
823 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid, bd->drq_res);
825 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid, bd->base_res);
831 /* Detach the interfaces, free buffer memory. */
832 for (c = b->chan; c < b->chan + NCHAN; ++c) {
833 drv_t *d = (drv_t*) c->sys;
835 if (!d || !d->chan->type)
840 ng_rmnode_self (d->node);
841 NG_NODE_UNREF (d->node);
844 mtx_destroy (&d->queue.ifq_mtx);
845 mtx_destroy (&d->hi_queue.ifq_mtx);
847 /* Detach from the packet filter list of interfaces. */
850 /* Detach from the sync PPP list. */
851 sppp_detach (d->ifp);
855 IF_DRAIN (&d->queue);
856 mtx_destroy (&d->queue.ifq_mtx);
858 destroy_dev (d->devt);
864 callout_drain (&led_timo[b->num]);
868 for (c = b->chan; c < b->chan + NCHAN; ++c) {
869 drv_t *d = (drv_t*) c->sys;
871 if (!d || !d->chan->type)
874 /* Deallocate buffers. */
875 ct_bus_dma_mem_free (&d->dmamem);
878 adapter [b->num] = 0;
882 mtx_destroy (&bd->ct_mtx);
888 static void ct_ifstart (struct ifnet *ifp)
890 drv_t *d = ifp->if_softc;
898 static void ct_ifwatchdog (struct ifnet *ifp)
900 drv_t *d = ifp->if_softc;
905 static void ct_tlf (struct sppp *sp)
907 drv_t *d = SP2IFP(sp)->if_softc;
909 CT_DEBUG (d, ("ct_tlf\n"));
910 /* ct_set_dtr (d->chan, 0);*/
911 /* ct_set_rts (d->chan, 0);*/
912 if (!(sp->pp_flags & PP_FR) && !(d->ifp->if_flags & PP_CISCO))
916 static void ct_tls (struct sppp *sp)
918 drv_t *d = SP2IFP(sp)->if_softc;
920 CT_DEBUG (d, ("ct_tls\n"));
921 if (!(sp->pp_flags & PP_FR) && !(d->ifp->if_flags & PP_CISCO))
926 * Initialization of interface.
927 * Ii seems to be never called by upper level.
929 static void ct_initialize (void *softc)
933 CT_DEBUG (d, ("ct_initialize\n"));
937 * Process an ioctl request.
939 static int ct_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data)
941 drv_t *d = ifp->if_softc;
943 int error, s, was_up, should_be_up;
945 was_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
946 error = sppp_ioctl (ifp, cmd, data);
950 if (! (ifp->if_flags & IFF_DEBUG))
952 else if (! d->chan->debug)
956 default: CT_DEBUG2 (d, ("ioctl 0x%lx\n", cmd)); return 0;
957 case SIOCADDMULTI: CT_DEBUG2 (d, ("SIOCADDMULTI\n")); return 0;
958 case SIOCDELMULTI: CT_DEBUG2 (d, ("SIOCDELMULTI\n")); return 0;
959 case SIOCSIFFLAGS: CT_DEBUG2 (d, ("SIOCSIFFLAGS\n")); break;
960 case SIOCSIFADDR: CT_DEBUG2 (d, ("SIOCSIFADDR\n")); break;
963 /* We get here only in case of SIFFLAGS or SIFADDR. */
966 should_be_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
967 if (! was_up && should_be_up) {
968 /* Interface goes up -- start it. */
971 } else if (was_up && ! should_be_up) {
972 /* Interface is going down -- stop it. */
973 /* if ((IFP2SP(d->ifp)->pp_flags & PP_FR) || (ifp->if_flags & PP_CISCO))*/
983 * Stop the interface. Called on splimp().
985 static void ct_down (drv_t *d)
988 CT_DEBUG (d, ("ct_down\n"));
989 ct_set_dtr (d->chan, 0);
990 ct_set_rts (d->chan, 0);
996 * Start the interface. Called on splimp().
998 static void ct_up (drv_t *d)
1001 CT_DEBUG (d, ("ct_up\n"));
1002 ct_set_dtr (d->chan, 1);
1003 ct_set_rts (d->chan, 1);
1009 * Start output on the (slave) interface. Get another datagram to send
1010 * off of the interface queue, and copy it to the interface
1011 * before starting the output.
1013 static void ct_send (drv_t *d)
1018 CT_DEBUG2 (d, ("ct_send, tn=%d\n", d->chan->tn));
1020 /* No output if the interface is down. */
1024 /* No output if the modem is off. */
1025 if (! ct_get_dsr (d->chan) && !ct_get_loop (d->chan))
1028 while (ct_buf_free (d->chan)) {
1029 /* Get the packet to send. */
1031 IF_DEQUEUE (&d->hi_queue, m);
1033 IF_DEQUEUE (&d->queue, m);
1035 m = sppp_dequeue (d->ifp);
1040 BPF_MTAP (d->ifp, m);
1042 len = m_length (m, NULL);
1044 ct_send_packet (d->chan, (u_char*)mtod (m, caddr_t),
1047 m_copydata (m, 0, len, d->chan->tbuf[d->chan->te]);
1048 ct_send_packet (d->chan, d->chan->tbuf[d->chan->te],
1053 /* Set up transmit timeout, if the transmit ring is not empty.
1054 * Transmit timeout is 10 seconds. */
1058 d->ifp->if_timer = 10;
1062 d->ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1067 * Start output on the interface.
1068 * Always called on splimp().
1070 static void ct_start (drv_t *d)
1076 ct_set_dtr (d->chan, 1);
1078 ct_set_rts (d->chan, 1);
1086 * Handle transmit timeouts.
1087 * Recover after lost transmit interrupts.
1088 * Always called on splimp().
1090 static void ct_watchdog (drv_t *d)
1097 CT_DEBUG (d, ("device timeout\n"));
1099 ct_setup_chan (d->chan);
1100 ct_start_chan (d->chan, 0, 0);
1101 ct_set_dtr (d->chan, 1);
1102 ct_set_rts (d->chan, 1);
1110 * Transmit callback function.
1112 static void ct_transmit (ct_chan_t *c, void *attachment, int len)
1121 ++d->ifp->if_opackets;
1122 d->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1123 d->ifp->if_timer = 0;
1129 * Process the received packet.
1131 static void ct_receive (ct_chan_t *c, char *data, int len)
1139 if (!d || !d->running)
1142 m = makembuf (data, len);
1144 CT_DEBUG (d, ("no memory for packet\n"));
1146 ++d->ifp->if_iqdrops;
1153 m->m_pkthdr.rcvif = 0;
1154 NG_SEND_DATA_ONLY (error, d->hook, m);
1156 ++d->ifp->if_ipackets;
1157 m->m_pkthdr.rcvif = d->ifp;
1158 /* Check if there's a BPF listener on this interface.
1159 * If so, hand off the raw packet to bpf. */
1160 BPF_TAP (d->ifp, data, len);
1161 IF_ENQUEUE (&d->queue, m);
1166 * Error callback function.
1168 static void ct_error (ct_chan_t *c, int data)
1177 CT_DEBUG (d, ("frame error\n"));
1179 ++d->ifp->if_ierrors;
1183 CT_DEBUG (d, ("crc error\n"));
1185 ++d->ifp->if_ierrors;
1189 CT_DEBUG (d, ("overrun error\n"));
1191 ++d->ifp->if_collisions;
1192 ++d->ifp->if_ierrors;
1196 CT_DEBUG (d, ("overflow error\n"));
1198 ++d->ifp->if_ierrors;
1202 CT_DEBUG (d, ("underrun error\n"));
1206 ++d->ifp->if_oerrors;
1207 d->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1208 d->ifp->if_timer = 0;
1213 CT_DEBUG (d, ("error #%d\n", data));
1217 static int ct_open (struct cdev *dev, int oflags, int devtype, struct thread *td)
1221 if (minor(dev) >= NCTAU*NCHAN || ! (d = channel[minor(dev)]))
1224 CT_DEBUG2 (d, ("ct_open\n"));
1228 static int ct_close (struct cdev *dev, int fflag, int devtype, struct thread *td)
1230 drv_t *d = channel [minor(dev)];
1235 CT_DEBUG2 (d, ("ct_close\n"));
1239 static int ct_modem_status (ct_chan_t *c)
1250 status = d->running ? TIOCM_LE : 0;
1253 if (ct_get_cd (c)) status |= TIOCM_CD;
1254 if (ct_get_cts (c)) status |= TIOCM_CTS;
1255 if (ct_get_dsr (c)) status |= TIOCM_DSR;
1256 if (c->dtr) status |= TIOCM_DTR;
1257 if (c->rts) status |= TIOCM_RTS;
1264 * Process an ioctl request on /dev/cronyx/ctauN.
1266 static int ct_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
1268 drv_t *d = channel [minor (dev)];
1271 struct serial_statistics *st;
1272 struct e1_statistics *opte1;
1283 case SERIAL_GETREGISTERED:
1284 bzero (mask, sizeof(mask));
1285 for (s=0; s<NCTAU*NCHAN; ++s)
1287 mask [s/8] |= 1 << (s & 7);
1288 bcopy (mask, data, sizeof (mask));
1292 case SERIAL_GETPROTO:
1293 strcpy ((char*)data, (IFP2SP(d->ifp)->pp_flags & PP_FR) ? "fr" :
1294 (d->ifp->if_flags & PP_CISCO) ? "cisco" : "ppp");
1297 case SERIAL_SETPROTO:
1298 /* Only for superuser! */
1299 error = priv_check (td, PRIV_DRIVER);
1302 if (d->ifp->if_drv_flags & IFF_DRV_RUNNING)
1304 if (! strcmp ("cisco", (char*)data)) {
1305 IFP2SP(d->ifp)->pp_flags &= ~(PP_FR);
1306 IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE;
1307 d->ifp->if_flags |= PP_CISCO;
1308 } else if (! strcmp ("fr", (char*)data)) {
1309 d->ifp->if_flags &= ~(PP_CISCO);
1310 IFP2SP(d->ifp)->pp_flags |= PP_FR | PP_KEEPALIVE;
1311 } else if (! strcmp ("ppp", (char*)data)) {
1312 IFP2SP(d->ifp)->pp_flags &= ~(PP_FR | PP_KEEPALIVE);
1313 d->ifp->if_flags &= ~(PP_CISCO);
1318 case SERIAL_GETKEEPALIVE:
1319 if ((IFP2SP(d->ifp)->pp_flags & PP_FR) ||
1320 (d->ifp->if_flags & PP_CISCO))
1322 *(int*)data = (IFP2SP(d->ifp)->pp_flags & PP_KEEPALIVE) ? 1 : 0;
1325 case SERIAL_SETKEEPALIVE:
1326 /* Only for superuser! */
1327 error = priv_check (td, PRIV_DRIVER);
1330 if ((IFP2SP(d->ifp)->pp_flags & PP_FR) ||
1331 (d->ifp->if_flags & PP_CISCO))
1334 IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE;
1336 IFP2SP(d->ifp)->pp_flags &= ~PP_KEEPALIVE;
1340 case SERIAL_GETMODE:
1341 *(int*)data = SERIAL_HDLC;
1345 if (c->mode == M_HDLC)
1347 switch (ct_get_config (c->board)) {
1348 default: *(char*)data = 'a'; break;
1349 case CFG_B: *(char*)data = 'b'; break;
1350 case CFG_C: *(char*)data = 'c'; break;
1355 /* Only for superuser! */
1356 error = priv_check (td, PRIV_DRIVER);
1359 if (c->mode == M_HDLC)
1363 switch (*(char*)data) {
1364 case 'a': ct_set_config (c->board, CFG_A); break;
1365 case 'b': ct_set_config (c->board, CFG_B); break;
1366 case 'c': ct_set_config (c->board, CFG_C); break;
1372 case SERIAL_GETSTAT:
1373 st = (struct serial_statistics*) data;
1374 st->rintr = c->rintr;
1375 st->tintr = c->tintr;
1376 st->mintr = c->mintr;
1377 st->ibytes = c->ibytes;
1378 st->ipkts = c->ipkts;
1379 st->ierrs = c->ierrs;
1380 st->obytes = c->obytes;
1381 st->opkts = c->opkts;
1382 st->oerrs = c->oerrs;
1385 case SERIAL_GETESTAT:
1386 opte1 = (struct e1_statistics*)data;
1387 opte1->status = c->status;
1388 opte1->cursec = c->cursec;
1389 opte1->totsec = c->totsec + c->cursec;
1391 opte1->currnt.bpv = c->currnt.bpv;
1392 opte1->currnt.fse = c->currnt.fse;
1393 opte1->currnt.crce = c->currnt.crce;
1394 opte1->currnt.rcrce = c->currnt.rcrce;
1395 opte1->currnt.uas = c->currnt.uas;
1396 opte1->currnt.les = c->currnt.les;
1397 opte1->currnt.es = c->currnt.es;
1398 opte1->currnt.bes = c->currnt.bes;
1399 opte1->currnt.ses = c->currnt.ses;
1400 opte1->currnt.oofs = c->currnt.oofs;
1401 opte1->currnt.css = c->currnt.css;
1402 opte1->currnt.dm = c->currnt.dm;
1404 opte1->total.bpv = c->total.bpv + c->currnt.bpv;
1405 opte1->total.fse = c->total.fse + c->currnt.fse;
1406 opte1->total.crce = c->total.crce + c->currnt.crce;
1407 opte1->total.rcrce = c->total.rcrce + c->currnt.rcrce;
1408 opte1->total.uas = c->total.uas + c->currnt.uas;
1409 opte1->total.les = c->total.les + c->currnt.les;
1410 opte1->total.es = c->total.es + c->currnt.es;
1411 opte1->total.bes = c->total.bes + c->currnt.bes;
1412 opte1->total.ses = c->total.ses + c->currnt.ses;
1413 opte1->total.oofs = c->total.oofs + c->currnt.oofs;
1414 opte1->total.css = c->total.css + c->currnt.css;
1415 opte1->total.dm = c->total.dm + c->currnt.dm;
1416 for (s=0; s<48; ++s) {
1417 opte1->interval[s].bpv = c->interval[s].bpv;
1418 opte1->interval[s].fse = c->interval[s].fse;
1419 opte1->interval[s].crce = c->interval[s].crce;
1420 opte1->interval[s].rcrce = c->interval[s].rcrce;
1421 opte1->interval[s].uas = c->interval[s].uas;
1422 opte1->interval[s].les = c->interval[s].les;
1423 opte1->interval[s].es = c->interval[s].es;
1424 opte1->interval[s].bes = c->interval[s].bes;
1425 opte1->interval[s].ses = c->interval[s].ses;
1426 opte1->interval[s].oofs = c->interval[s].oofs;
1427 opte1->interval[s].css = c->interval[s].css;
1428 opte1->interval[s].dm = c->interval[s].dm;
1432 case SERIAL_CLRSTAT:
1433 /* Only for superuser! */
1434 error = priv_check (td, PRIV_DRIVER);
1446 bzero (&c->currnt, sizeof (c->currnt));
1447 bzero (&c->total, sizeof (c->total));
1448 bzero (c->interval, sizeof (c->interval));
1451 case SERIAL_GETBAUD:
1452 *(long*)data = ct_get_baud(c);
1455 case SERIAL_SETBAUD:
1456 /* Only for superuser! */
1457 error = priv_check (td, PRIV_DRIVER);
1462 ct_set_baud (c, *(long*)data);
1467 case SERIAL_GETLOOP:
1468 *(int*)data = ct_get_loop (c);
1471 case SERIAL_SETLOOP:
1472 /* Only for superuser! */
1473 error = priv_check (td, PRIV_DRIVER);
1478 ct_set_loop (c, *(int*)data);
1483 case SERIAL_GETDPLL:
1484 if (c->mode == M_E1 || c->mode == M_G703)
1486 *(int*)data = ct_get_dpll (c);
1489 case SERIAL_SETDPLL:
1490 /* Only for superuser! */
1491 error = priv_check (td, PRIV_DRIVER);
1494 if (c->mode == M_E1 || c->mode == M_G703)
1498 ct_set_dpll (c, *(int*)data);
1503 case SERIAL_GETNRZI:
1504 if (c->mode == M_E1 || c->mode == M_G703)
1506 *(int*)data = ct_get_nrzi (c);
1509 case SERIAL_SETNRZI:
1510 /* Only for superuser! */
1511 error = priv_check (td, PRIV_DRIVER);
1514 if (c->mode == M_E1 || c->mode == M_G703)
1518 ct_set_nrzi (c, *(int*)data);
1523 case SERIAL_GETDEBUG:
1524 *(int*)data = c->debug;
1527 case SERIAL_SETDEBUG:
1528 /* Only for superuser! */
1529 error = priv_check (td, PRIV_DRIVER);
1532 c->debug = *(int*)data;
1535 d->ifp->if_flags |= IFF_DEBUG;
1537 d->ifp->if_flags &= (~IFF_DEBUG);
1541 case SERIAL_GETHIGAIN:
1542 if (c->mode != M_E1)
1544 *(int*)data = ct_get_higain (c);
1547 case SERIAL_SETHIGAIN:
1548 /* Only for superuser! */
1549 error = priv_check (td, PRIV_DRIVER);
1554 ct_set_higain (c, *(int*)data);
1559 case SERIAL_GETPHONY:
1560 CT_DEBUG2 (d, ("ioctl: getphony\n"));
1561 if (c->mode != M_E1)
1563 *(int*)data = c->gopt.phony;
1566 case SERIAL_SETPHONY:
1567 CT_DEBUG2 (d, ("ioctl: setphony\n"));
1568 if (c->mode != M_E1)
1570 /* Only for superuser! */
1571 error = priv_check (td, PRIV_DRIVER);
1576 ct_set_phony (c, *(int*)data);
1582 if (c->mode != M_E1 && c->mode != M_G703)
1584 switch (ct_get_clk(c)) {
1585 default: *(int*)data = E1CLK_INTERNAL; break;
1586 case GCLK_RCV: *(int*)data = E1CLK_RECEIVE; break;
1587 case GCLK_RCLKO: *(int*)data = c->num ?
1588 E1CLK_RECEIVE_CHAN0 : E1CLK_RECEIVE_CHAN1; break;
1593 /* Only for superuser! */
1594 error = priv_check (td, PRIV_DRIVER);
1599 switch (*(int*)data) {
1600 default: ct_set_clk (c, GCLK_INT); break;
1601 case E1CLK_RECEIVE: ct_set_clk (c, GCLK_RCV); break;
1602 case E1CLK_RECEIVE_CHAN0:
1603 case E1CLK_RECEIVE_CHAN1:
1604 ct_set_clk (c, GCLK_RCLKO); break;
1610 case SERIAL_GETTIMESLOTS:
1611 if (c->mode != M_E1)
1613 *(long*)data = ct_get_ts (c);
1616 case SERIAL_SETTIMESLOTS:
1617 /* Only for superuser! */
1618 error = priv_check (td, PRIV_DRIVER);
1623 ct_set_ts (c, *(long*)data);
1628 case SERIAL_GETSUBCHAN:
1629 if (c->mode != M_E1)
1631 *(long*)data = ct_get_subchan (c->board);
1634 case SERIAL_SETSUBCHAN:
1635 /* Only for superuser! */
1636 error = priv_check (td, PRIV_DRIVER);
1641 ct_set_subchan (c->board, *(long*)data);
1646 case SERIAL_GETINVCLK:
1647 case SERIAL_GETINVTCLK:
1648 if (c->mode == M_E1 || c->mode == M_G703)
1650 *(int*)data = ct_get_invtxc (c);
1653 case SERIAL_GETINVRCLK:
1654 if (c->mode == M_E1 || c->mode == M_G703)
1656 *(int*)data = ct_get_invrxc (c);
1659 case SERIAL_SETINVCLK:
1660 case SERIAL_SETINVTCLK:
1661 /* Only for superuser! */
1662 error = priv_check (td, PRIV_DRIVER);
1665 if (c->mode == M_E1 || c->mode == M_G703)
1669 ct_set_invtxc (c, *(int*)data);
1674 case SERIAL_SETINVRCLK:
1675 /* Only for superuser! */
1676 error = priv_check (td, PRIV_DRIVER);
1679 if (c->mode == M_E1 || c->mode == M_G703)
1683 ct_set_invrxc (c, *(int*)data);
1688 case SERIAL_GETLEVEL:
1689 if (c->mode != M_G703)
1693 *(int*)data = ct_get_lq (c);
1698 case TIOCSDTR: /* Set DTR */
1706 case TIOCCDTR: /* Clear DTR */
1714 case TIOCMSET: /* Set DTR/RTS */
1717 ct_set_dtr (c, (*(int*)data & TIOCM_DTR) ? 1 : 0);
1718 ct_set_rts (c, (*(int*)data & TIOCM_RTS) ? 1 : 0);
1723 case TIOCMBIS: /* Add DTR/RTS */
1726 if (*(int*)data & TIOCM_DTR) ct_set_dtr (c, 1);
1727 if (*(int*)data & TIOCM_RTS) ct_set_rts (c, 1);
1732 case TIOCMBIC: /* Clear DTR/RTS */
1735 if (*(int*)data & TIOCM_DTR) ct_set_dtr (c, 0);
1736 if (*(int*)data & TIOCM_RTS) ct_set_rts (c, 0);
1741 case TIOCMGET: /* Get modem status */
1742 *(int*)data = ct_modem_status (c);
1749 static int ng_ct_constructor (node_p node)
1751 drv_t *d = NG_NODE_PRIVATE (node);
1752 CT_DEBUG (d, ("Constructor\n"));
1756 static int ng_ct_newhook (node_p node, hook_p hook, const char *name)
1759 drv_t *d = NG_NODE_PRIVATE (node);
1766 /* Attach debug hook */
1767 if (strcmp (name, NG_CT_HOOK_DEBUG) == 0) {
1768 NG_HOOK_SET_PRIVATE (hook, NULL);
1769 d->debug_hook = hook;
1773 /* Check for raw hook */
1774 if (strcmp (name, NG_CT_HOOK_RAW) != 0)
1777 NG_HOOK_SET_PRIVATE (hook, d);
1787 static char *format_timeslots (u_long s)
1789 static char buf [100];
1793 for (i=1; i<32; ++i)
1795 int prev = (i > 1) & (s >> (i-1));
1796 int next = (i < 31) & (s >> (i+1));
1806 *p++ = '0' + i / 10;
1807 *p++ = '0' + i % 10;
1813 static int print_modems (char *s, ct_chan_t *c, int need_header)
1815 int status = ct_modem_status (c);
1819 length += sprintf (s + length, " LE DTR DSR RTS CTS CD\n");
1820 length += sprintf (s + length, "%4s %4s %4s %4s %4s %4s\n",
1821 status & TIOCM_LE ? "On" : "-",
1822 status & TIOCM_DTR ? "On" : "-",
1823 status & TIOCM_DSR ? "On" : "-",
1824 status & TIOCM_RTS ? "On" : "-",
1825 status & TIOCM_CTS ? "On" : "-",
1826 status & TIOCM_CD ? "On" : "-");
1830 static int print_stats (char *s, ct_chan_t *c, int need_header)
1832 struct serial_statistics st;
1835 st.rintr = c->rintr;
1836 st.tintr = c->tintr;
1837 st.mintr = c->mintr;
1838 st.ibytes = c->ibytes;
1839 st.ipkts = c->ipkts;
1840 st.ierrs = c->ierrs;
1841 st.obytes = c->obytes;
1842 st.opkts = c->opkts;
1843 st.oerrs = c->oerrs;
1845 length += sprintf (s + length, " Rintr Tintr Mintr Ibytes Ipkts Ierrs Obytes Opkts Oerrs\n");
1846 length += sprintf (s + length, "%7ld %7ld %7ld %8ld %7ld %7ld %8ld %7ld %7ld\n",
1847 st.rintr, st.tintr, st.mintr, st.ibytes, st.ipkts,
1848 st.ierrs, st.obytes, st.opkts, st.oerrs);
1852 static char *format_e1_status (u_char status)
1854 static char buf [80];
1856 if (status & E1_NOALARM)
1859 if (status & E1_LOS) strcat (buf, ",LOS");
1860 if (status & E1_AIS) strcat (buf, ",AIS");
1861 if (status & E1_LOF) strcat (buf, ",LOF");
1862 if (status & E1_LOMF) strcat (buf, ",LOMF");
1863 if (status & E1_FARLOF) strcat (buf, ",FARLOF");
1864 if (status & E1_AIS16) strcat (buf, ",AIS16");
1865 if (status & E1_FARLOMF) strcat (buf, ",FARLOMF");
1866 if (status & E1_TSTREQ) strcat (buf, ",TSTREQ");
1867 if (status & E1_TSTERR) strcat (buf, ",TSTERR");
1873 static int print_frac (char *s, int leftalign, u_long numerator, u_long divider)
1877 if (numerator < 1 || divider < 1) {
1878 length += sprintf (s+length, leftalign ? "/- " : " -");
1881 n = (int) (0.5 + 1000.0 * numerator / divider);
1883 length += sprintf (s+length, leftalign ? "/.%-3d" : " .%03d", n);
1886 *(s + length) = leftalign ? '/' : ' ';
1889 if (n >= 1000000) n = (n+500) / 1000 * 1000;
1890 else if (n >= 100000) n = (n+50) / 100 * 100;
1891 else if (n >= 10000) n = (n+5) / 10 * 10;
1894 case 1000: length += printf (s+length, ".999"); return length;
1895 case 10000: n = 9990; break;
1896 case 100000: n = 99900; break;
1897 case 1000000: n = 999000; break;
1899 if (n < 10000) length += sprintf (s+length, "%d.%d", n/1000, n/10%100);
1900 else if (n < 100000) length += sprintf (s+length, "%d.%d", n/1000, n/100%10);
1901 else if (n < 1000000) length += sprintf (s+length, "%d.", n/1000);
1902 else length += sprintf (s+length, "%d", n/1000);
1907 static int print_e1_stats (char *s, ct_chan_t *c)
1909 struct e1_counters total;
1913 totsec = c->totsec + c->cursec;
1914 total.bpv = c->total.bpv + c->currnt.bpv;
1915 total.fse = c->total.fse + c->currnt.fse;
1916 total.crce = c->total.crce + c->currnt.crce;
1917 total.rcrce = c->total.rcrce + c->currnt.rcrce;
1918 total.uas = c->total.uas + c->currnt.uas;
1919 total.les = c->total.les + c->currnt.les;
1920 total.es = c->total.es + c->currnt.es;
1921 total.bes = c->total.bes + c->currnt.bes;
1922 total.ses = c->total.ses + c->currnt.ses;
1923 total.oofs = c->total.oofs + c->currnt.oofs;
1924 total.css = c->total.css + c->currnt.css;
1925 total.dm = c->total.dm + c->currnt.dm;
1927 length += sprintf (s + length, " Unav/Degr Bpv/Fsyn CRC/RCRC Err/Lerr Sev/Bur Oof/Slp Status\n");
1929 /* Unavailable seconds, degraded minutes */
1930 length += print_frac (s + length, 0, c->currnt.uas, c->cursec);
1931 length += print_frac (s + length, 1, 60 * c->currnt.dm, c->cursec);
1933 /* Bipolar violations, frame sync errors */
1934 length += print_frac (s + length, 0, c->currnt.bpv, c->cursec);
1935 length += print_frac (s + length, 1, c->currnt.fse, c->cursec);
1937 /* CRC errors, remote CRC errors (E-bit) */
1938 length += print_frac (s + length, 0, c->currnt.crce, c->cursec);
1939 length += print_frac (s + length, 1, c->currnt.rcrce, c->cursec);
1941 /* Errored seconds, line errored seconds */
1942 length += print_frac (s + length, 0, c->currnt.es, c->cursec);
1943 length += print_frac (s + length, 1, c->currnt.les, c->cursec);
1945 /* Severely errored seconds, burst errored seconds */
1946 length += print_frac (s + length, 0, c->currnt.ses, c->cursec);
1947 length += print_frac (s + length, 1, c->currnt.bes, c->cursec);
1949 /* Out of frame seconds, controlled slip seconds */
1950 length += print_frac (s + length, 0, c->currnt.oofs, c->cursec);
1951 length += print_frac (s + length, 1, c->currnt.css, c->cursec);
1953 length += sprintf (s + length, " %s\n", format_e1_status (c->status));
1955 /* Print total statistics. */
1956 length += print_frac (s + length, 0, total.uas, totsec);
1957 length += print_frac (s + length, 1, 60 * total.dm, totsec);
1959 length += print_frac (s + length, 0, total.bpv, totsec);
1960 length += print_frac (s + length, 1, total.fse, totsec);
1962 length += print_frac (s + length, 0, total.crce, totsec);
1963 length += print_frac (s + length, 1, total.rcrce, totsec);
1965 length += print_frac (s + length, 0, total.es, totsec);
1966 length += print_frac (s + length, 1, total.les, totsec);
1968 length += print_frac (s + length, 0, total.ses, totsec);
1969 length += print_frac (s + length, 1, total.bes, totsec);
1971 length += print_frac (s + length, 0, total.oofs, totsec);
1972 length += print_frac (s + length, 1, total.css, totsec);
1974 length += sprintf (s + length, " -- Total\n");
1978 static int print_chan (char *s, ct_chan_t *c)
1984 length += sprintf (s + length, "ct%d", c->board->num * NCHAN + c->num);
1986 length += sprintf (s + length, " debug=%d", d->chan->debug);
1988 switch (ct_get_config (c->board)) {
1989 case CFG_A: length += sprintf (s + length, " cfg=A"); break;
1990 case CFG_B: length += sprintf (s + length, " cfg=B"); break;
1991 case CFG_C: length += sprintf (s + length, " cfg=C"); break;
1992 default: length += sprintf (s + length, " cfg=unknown"); break;
1995 if (ct_get_baud (c))
1996 length += sprintf (s + length, " %ld", ct_get_baud (c));
1998 length += sprintf (s + length, " extclock");
2000 if (c->mode == M_E1 || c->mode == M_G703)
2001 switch (ct_get_clk(c)) {
2002 case GCLK_INT : length += sprintf (s + length, " syn=int"); break;
2003 case GCLK_RCV : length += sprintf (s + length, " syn=rcv"); break;
2004 case GCLK_RCLKO : length += sprintf (s + length, " syn=xrcv"); break;
2006 if (c->mode == M_HDLC) {
2007 length += sprintf (s + length, " dpll=%s", ct_get_dpll (c) ? "on" : "off");
2008 length += sprintf (s + length, " nrzi=%s", ct_get_nrzi (c) ? "on" : "off");
2009 length += sprintf (s + length, " invtclk=%s", ct_get_invtxc (c) ? "on" : "off");
2010 length += sprintf (s + length, " invrclk=%s", ct_get_invrxc (c) ? "on" : "off");
2012 if (c->mode == M_E1)
2013 length += sprintf (s + length, " higain=%s", ct_get_higain (c)? "on" : "off");
2015 length += sprintf (s + length, " loop=%s", ct_get_loop (c) ? "on" : "off");
2017 if (c->mode == M_E1)
2018 length += sprintf (s + length, " ts=%s", format_timeslots (ct_get_ts(c)));
2019 if (c->mode == M_E1 && ct_get_config (c->board) != CFG_A)
2020 length += sprintf (s + length, " pass=%s", format_timeslots (ct_get_subchan(c->board)));
2021 if (c->mode == M_G703) {
2029 length += sprintf (s + length, " (level=-%.1fdB)", lq / 10.0);
2031 length += sprintf (s + length, "\n");
2035 static int ng_ct_rcvmsg (node_p node, item_p item, hook_p lasthook)
2037 drv_t *d = NG_NODE_PRIVATE (node);
2038 struct ng_mesg *msg;
2039 struct ng_mesg *resp = NULL;
2045 CT_DEBUG (d, ("Rcvmsg\n"));
2046 NGI_GET_MSG (item, msg);
2047 switch (msg->header.typecookie) {
2053 printf ("Don't forget to implement\n");
2057 case NGM_GENERIC_COOKIE:
2058 switch (msg->header.cmd) {
2063 case NGM_TEXT_STATUS: {
2066 int dl = sizeof (struct ng_mesg) + 730;
2068 NG_MKRESPONSE (resp, msg, dl, M_NOWAIT);
2074 l += print_chan (s + l, d->chan);
2075 l += print_stats (s + l, d->chan, 1);
2076 l += print_modems (s + l, d->chan, 1);
2077 l += print_e1_stats (s + l, d->chan);
2078 strncpy ((resp)->header.cmdstr, "status", NG_CMDSTRLEN);
2084 NG_RESPOND_MSG (error, node, item, resp);
2089 static int ng_ct_rcvdata (hook_p hook, item_p item)
2091 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE(hook));
2093 struct ng_tag_prio *ptag;
2102 NGI_GET_M (item, m);
2103 NG_FREE_ITEM (item);
2104 if (! NG_HOOK_PRIVATE (hook) || ! d) {
2109 /* Check for high priority data */
2110 if ((ptag = (struct ng_tag_prio *)m_tag_locate(m, NGM_GENERIC_COOKIE,
2111 NG_TAG_PRIO, NULL)) != NULL && (ptag->priority > NG_PRIO_CUTOFF) )
2119 if (_IF_QFULL (q)) {
2135 static int ng_ct_rmnode (node_p node)
2137 drv_t *d = NG_NODE_PRIVATE (node);
2140 CT_DEBUG (d, ("Rmnode\n"));
2141 if (d && d->running) {
2150 if (node->nd_flags & NGF_REALLY_DIE) {
2151 NG_NODE_SET_PRIVATE (node, NULL);
2152 NG_NODE_UNREF (node);
2154 NG_NODE_REVIVE(node); /* Persistant node */
2159 static void ng_ct_watchdog (void *arg)
2166 if (d->timeout == 1)
2170 callout_reset (&d->timeout_handle, hz, ng_ct_watchdog, d);
2173 static int ng_ct_connect (hook_p hook)
2175 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2180 callout_reset (&d->timeout_handle, hz, ng_ct_watchdog, d);
2184 static int ng_ct_disconnect (hook_p hook)
2186 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2195 if (NG_HOOK_PRIVATE (hook))
2198 /* If we were wait it than it reasserted now, just stop it. */
2199 if (!callout_drain (&d->timeout_handle))
2200 callout_stop (&d->timeout_handle);
2205 static int ct_modevent (module_t mod, int type, void *unused)
2207 static int load_count = 0;
2209 if (!debug_mpsafenet && ct_mpsafenet) {
2210 printf ("WORNING! Network stack is not MPSAFE. "
2211 "Turning off debug.ct.mpsafenet.\n");
2215 ct_cdevsw.d_flags &= ~D_NEEDGIANT;
2220 if (ng_newtype (&typestruct))
2221 printf ("Failed to register ng_ct\n");
2224 callout_init (&timeout_handle, ct_mpsafenet?CALLOUT_MPSAFE:0);
2225 callout_reset (&timeout_handle, hz*5, ct_timeout, 0);
2228 if (load_count == 1) {
2229 printf ("Removing device entry for Tau-ISA\n");
2231 ng_rmtype (&typestruct);
2234 /* If we were wait it than it reasserted now, just stop it. */
2235 if (!callout_drain (&timeout_handle))
2236 callout_stop (&timeout_handle);
2246 static struct ng_type typestruct = {
2247 .version = NG_ABI_VERSION,
2248 .name = NG_CT_NODE_TYPE,
2249 .constructor = ng_ct_constructor,
2250 .rcvmsg = ng_ct_rcvmsg,
2251 .shutdown = ng_ct_rmnode,
2252 .newhook = ng_ct_newhook,
2253 .connect = ng_ct_connect,
2254 .rcvdata = ng_ct_rcvdata,
2255 .disconnect = ng_ct_disconnect,
2260 MODULE_DEPEND (ng_ct, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION);
2262 MODULE_DEPEND (ct, sppp, 1, 1, 1);
2264 DRIVER_MODULE (ct, isa, ct_isa_driver, ct_devclass, ct_modevent, NULL);
2265 MODULE_VERSION (ct, 1);