2 * Cronyx-Tau adapter driver for FreeBSD.
3 * Supports PPP/HDLC and Cisco/HDLC protocol in synchronous mode,
4 * and asynchronous 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 <net/if_var.h>
50 #include <machine/cpufunc.h>
51 #include <machine/cserial.h>
52 #include <machine/resource.h>
53 #include <dev/cx/machdep.h>
54 #include <dev/ctau/ctddk.h>
55 #include <dev/cx/cronyxfw.h>
56 #include "opt_ng_cronyx.h"
57 #ifdef NETGRAPH_CRONYX
58 # include "opt_netgraph.h"
59 # include <netgraph/ng_message.h>
60 # include <netgraph/netgraph.h>
61 # include <dev/ctau/ng_ct.h>
63 # include <net/if_types.h>
64 # include <net/if_sppp.h>
65 # define PP_CISCO IFF_LINK2
71 /* If we don't have Cronyx's sppp version, we don't have fr support via sppp */
76 #define CT_DEBUG(d,s) ({if (d->chan->debug) {\
77 printf ("%s: ", d->name); printf s;}})
78 #define CT_DEBUG2(d,s) ({if (d->chan->debug>1) {\
79 printf ("%s: ", d->name); printf s;}})
81 #define CT_LOCK_NAME "ctX"
83 #define CT_LOCK(_bd) mtx_lock (&(_bd)->ct_mtx)
84 #define CT_UNLOCK(_bd) mtx_unlock (&(_bd)->ct_mtx)
85 #define CT_LOCK_ASSERT(_bd) mtx_assert (&(_bd)->ct_mtx, MA_OWNED)
87 static void ct_identify __P((driver_t *, device_t));
88 static int ct_probe __P((device_t));
89 static int ct_attach __P((device_t));
90 static int ct_detach __P((device_t));
92 static device_method_t ct_isa_methods [] = {
93 DEVMETHOD(device_identify, ct_identify),
94 DEVMETHOD(device_probe, ct_probe),
95 DEVMETHOD(device_attach, ct_attach),
96 DEVMETHOD(device_detach, ct_detach),
101 typedef struct _ct_dma_mem_t {
109 typedef struct _drv_t {
117 char nodename [NG_NODESIZ];
121 struct ifqueue queue;
122 struct ifqueue hi_queue;
124 struct ifqueue queue;
128 struct callout timeout_handle;
132 typedef struct _bdrv_t {
134 struct resource *base_res;
135 struct resource *drq_res;
136 struct resource *irq_res;
141 drv_t channel [NCHAN];
145 static driver_t ct_isa_driver = {
151 static devclass_t ct_devclass;
153 static void ct_receive (ct_chan_t *c, char *data, int len);
154 static void ct_transmit (ct_chan_t *c, void *attachment, int len);
155 static void ct_error (ct_chan_t *c, int data);
156 static void ct_up (drv_t *d);
157 static void ct_start (drv_t *d);
158 static void ct_down (drv_t *d);
159 static void ct_watchdog (drv_t *d);
160 static void ct_watchdog_timer (void *arg);
162 extern struct ng_type typestruct;
164 static void ct_ifstart (struct ifnet *ifp);
165 static void ct_tlf (struct sppp *sp);
166 static void ct_tls (struct sppp *sp);
167 static int ct_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data);
168 static void ct_initialize (void *softc);
171 static ct_board_t *adapter [NCTAU];
172 static drv_t *channel [NCTAU*NCHAN];
173 static struct callout led_timo [NCTAU];
174 static struct callout timeout_handle;
176 static int ct_open (struct cdev *dev, int oflags, int devtype, struct thread *td);
177 static int ct_close (struct cdev *dev, int fflag, int devtype, struct thread *td);
178 static int ct_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td);
179 static struct cdevsw ct_cdevsw = {
180 .d_version = D_VERSION,
188 * Make an mbuf from data.
190 static struct mbuf *makembuf (void *buf, u_int len)
194 MGETHDR (m, M_NOWAIT, MT_DATA);
197 if (!(MCLGET(m, M_NOWAIT))) {
201 m->m_pkthdr.len = m->m_len = len;
202 bcopy (buf, mtod (m, caddr_t), len);
206 static void ct_timeout (void *arg)
211 for (i = 0; i < NCTAU; ++i) {
212 if (adapter[i] == NULL)
214 for (k = 0; k < NCHAN; k++) {
215 d = channel[i * NCHAN + k];
218 if (d->chan->mode != M_G703)
221 CT_LOCK ((bdrv_t *)d->bd);
222 ct_g703_timer (d->chan);
223 CT_UNLOCK ((bdrv_t *)d->bd);
228 callout_reset (&timeout_handle, hz, ct_timeout, 0);
231 static void ct_led_off (void *arg)
234 bdrv_t *bd = ((drv_t *)b->chan->sys)->bd;
244 * Activate interrupt handler from DDK.
246 static void ct_intr (void *arg)
249 ct_board_t *b = bd->board;
261 /* Turn LED off 50 msec later. */
262 callout_reset (&led_timo[b->num], hz/20, ct_led_off, b);
267 /* Pass packets in a lock-free state */
268 for (i = 0; i < NCHAN && b->chan[i].type; i++) {
269 drv_t *d = b->chan[i].sys;
271 if (!d || !d->running)
273 while (_IF_QLEN(&d->queue)) {
274 IF_DEQUEUE (&d->queue,m);
277 sppp_input (d->ifp, m);
283 static int probe_irq (ct_board_t *b, int irq)
287 /* Clear pending irq, if any. */
288 ct_probe_irq (b, -irq);
290 for (cnt=0; cnt<5; ++cnt) {
291 /* Get the mask of pending irqs, assuming they are busy.
292 * Activate the adapter on given irq. */
293 busy = ct_probe_irq (b, irq);
296 /* Get the mask of active irqs.
297 * Deactivate our irq. */
298 mask = ct_probe_irq (b, -irq);
300 if ((mask & ~busy) == 1 << irq) {
302 /* printf ("ct%d: irq %d ok, mask=0x%04x, busy=0x%04x\n",
303 b->num, irq, mask, busy); */
307 /* printf ("ct%d: irq %d not functional, mask=0x%04x, busy=0x%04x\n",
308 b->num, irq, mask, busy); */
313 static short porttab [] = {
314 0x200, 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0,
315 0x300, 0x320, 0x340, 0x360, 0x380, 0x3a0, 0x3c0, 0x3e0, 0
317 static char dmatab [] = { 7, 6, 5, 0 };
318 static char irqtab [] = { 5, 10, 11, 7, 3, 15, 12, 0 };
320 static int ct_is_free_res (device_t dev, int rid, int type, rman_res_t start,
321 rman_res_t end, rman_res_t count)
323 struct resource *res;
325 if (!(res = bus_alloc_resource (dev, type, &rid, start, end, count, 0)))
328 bus_release_resource (dev, type, rid, res);
333 static void ct_identify (driver_t *driver, device_t dev)
335 rman_res_t iobase, rescount;
339 devclass_t my_devclass;
342 if ((my_devclass = devclass_find ("ct")) == NULL)
345 devclass_get_devices (my_devclass, &devices, &devcount);
348 /* We should find all devices by our self. We could alter other
349 * devices, but we don't have a choise
351 for (i = 0; (iobase = porttab [i]) != 0; i++) {
352 if (!ct_is_free_res (dev, 0, SYS_RES_IOPORT,
353 iobase, iobase + NPORT, NPORT))
355 if (ct_probe_board (iobase, -1, -1) == 0)
359 child = BUS_ADD_CHILD (dev, ISA_ORDER_SPECULATIVE, "ct",
365 device_set_desc_copy (child, "Cronyx Tau-ISA");
366 device_set_driver (child, driver);
367 bus_set_resource (child, SYS_RES_IOPORT, 0,
370 if (devcount >= NCTAU)
374 static short porttab [] = {
375 0x200, 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0,
376 0x300, 0x320, 0x340, 0x360, 0x380, 0x3a0, 0x3c0, 0x3e0, 0
378 /* Lets check user choise.
380 for (k = 0; k < devcount; k++) {
381 if (bus_get_resource (devices[k], SYS_RES_IOPORT, 0,
382 &iobase, &rescount) != 0)
385 for (i = 0; porttab [i] != 0; i++) {
386 if (porttab [i] != iobase)
389 if (!ct_is_free_res (devices[k], 0, SYS_RES_IOPORT,
390 iobase, iobase + NPORT, NPORT))
393 if (ct_probe_board (iobase, -1, -1) == 0)
396 device_set_desc_copy (devices[k], "Cronyx Tau-ISA");
399 if (porttab [i] == 0) {
400 device_delete_child (
401 device_get_parent (devices[k]),
407 for (k = 0; k < devcount; k++) {
410 if (bus_get_resource (devices[k], SYS_RES_IOPORT, 0,
411 &iobase, &rescount) == 0)
413 for (i = 0; (iobase = porttab [i]) != 0; i++) {
414 if (porttab [i] == -1)
416 if (!ct_is_free_res (devices[k], 0, SYS_RES_IOPORT,
417 iobase, iobase + NPORT, NPORT))
419 if (ct_probe_board (iobase, -1, -1) == 0)
422 bus_set_resource (devices[k], SYS_RES_IOPORT, 0,
425 device_set_desc_copy (devices[k], "Cronyx Tau-ISA");
428 if (porttab [i] == 0) {
429 device_delete_child (
430 device_get_parent (devices[k]),
434 free (devices, M_TEMP);
440 static int ct_probe (device_t dev)
442 int unit = device_get_unit (dev);
443 rman_res_t iobase, rescount;
445 if (!device_get_desc (dev) ||
446 strcmp (device_get_desc (dev), "Cronyx Tau-ISA"))
449 /* KASSERT ((bd != NULL), ("ct%d: NULL device softc\n", unit));*/
450 if (bus_get_resource (dev, SYS_RES_IOPORT, 0, &iobase, &rescount) != 0) {
451 printf ("ct%d: Couldn't get IOPORT\n", unit);
455 if (!ct_is_free_res (dev, 0, SYS_RES_IOPORT,
456 iobase, iobase + NPORT, NPORT)) {
457 printf ("ct%d: Resource IOPORT isn't free\n", unit);
461 if (!ct_probe_board (iobase, -1, -1)) {
462 printf ("ct%d: probing for Tau-ISA at %jx faild\n", unit, iobase);
470 ct_bus_dmamap_addr (void *arg, bus_dma_segment_t *segs, int nseg, int error)
477 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
479 *addr = segs->ds_addr;
483 ct_bus_dma_mem_alloc (int bnum, int cnum, ct_dma_mem_t *dmem)
487 error = bus_dma_tag_create (NULL, 16, 0, BUS_SPACE_MAXADDR_24BIT,
488 BUS_SPACE_MAXADDR, NULL, NULL, dmem->size, 1,
489 dmem->size, 0, NULL, NULL, &dmem->dmat);
491 if (cnum >= 0) printf ("ct%d-%d: ", bnum, cnum);
492 else printf ("ct%d: ", bnum);
493 printf ("couldn't allocate tag for dma memory\n");
496 error = bus_dmamem_alloc (dmem->dmat, (void **)&dmem->virt,
497 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &dmem->mapp);
499 if (cnum >= 0) printf ("ct%d-%d: ", bnum, cnum);
500 else printf ("ct%d: ", bnum);
501 printf ("couldn't allocate mem for dma memory\n");
502 bus_dma_tag_destroy (dmem->dmat);
505 error = bus_dmamap_load (dmem->dmat, dmem->mapp, dmem->virt,
506 dmem->size, ct_bus_dmamap_addr, &dmem->phys, 0);
508 if (cnum >= 0) printf ("ct%d-%d: ", bnum, cnum);
509 else printf ("ct%d: ", bnum);
510 printf ("couldn't load mem map for dma memory\n");
511 bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
512 bus_dma_tag_destroy (dmem->dmat);
519 ct_bus_dma_mem_free (ct_dma_mem_t *dmem)
521 bus_dmamap_unload (dmem->dmat, dmem->mapp);
522 bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
523 bus_dma_tag_destroy (dmem->dmat);
527 * The adapter is present, initialize the driver structures.
529 static int ct_attach (device_t dev)
531 bdrv_t *bd = device_get_softc (dev);
532 rman_res_t iobase, drq, irq, rescount;
533 int unit = device_get_unit (dev);
534 char *ct_ln = CT_LOCK_NAME;
541 KASSERT ((bd != NULL), ("ct%d: NULL device softc\n", unit));
543 bus_get_resource (dev, SYS_RES_IOPORT, 0, &iobase, &rescount);
545 bd->base_res = bus_alloc_resource (dev, SYS_RES_IOPORT, &bd->base_rid,
546 iobase, iobase + NPORT, NPORT, RF_ACTIVE);
547 if (! bd->base_res) {
548 printf ("ct%d: cannot alloc base address\n", unit);
552 if (bus_get_resource (dev, SYS_RES_DRQ, 0, &drq, &rescount) != 0) {
553 for (i = 0; (drq = dmatab [i]) != 0; i++) {
554 if (!ct_is_free_res (dev, 0, SYS_RES_DRQ,
557 bus_set_resource (dev, SYS_RES_DRQ, 0, drq, 1);
561 if (dmatab[i] == 0) {
562 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
564 printf ("ct%d: Couldn't get DRQ\n", unit);
570 bd->drq_res = bus_alloc_resource (dev, SYS_RES_DRQ, &bd->drq_rid,
571 drq, drq + 1, 1, RF_ACTIVE);
573 printf ("ct%d: cannot allocate drq\n", unit);
574 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
579 if (bus_get_resource (dev, SYS_RES_IRQ, 0, &irq, &rescount) != 0) {
580 for (i = 0; (irq = irqtab [i]) != 0; i++) {
581 if (!ct_is_free_res (dev, 0, SYS_RES_IRQ,
584 bus_set_resource (dev, SYS_RES_IRQ, 0, irq, 1);
588 if (irqtab[i] == 0) {
589 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
591 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
593 printf ("ct%d: Couldn't get IRQ\n", unit);
599 bd->irq_res = bus_alloc_resource (dev, SYS_RES_IRQ, &bd->irq_rid,
600 irq, irq + 1, 1, RF_ACTIVE);
602 printf ("ct%d: Couldn't allocate irq\n", unit);
603 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
605 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
610 b = malloc (sizeof (ct_board_t), M_DEVBUF, M_WAITOK);
612 printf ("ct:%d: Couldn't allocate memory\n", unit);
616 bzero (b, sizeof(ct_board_t));
618 if (! ct_open_board (b, unit, iobase, irq, drq)) {
619 printf ("ct%d: error loading firmware\n", unit);
621 bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid,
623 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
625 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
632 ct_ln[2] = '0' + unit;
633 mtx_init (&bd->ct_mtx, ct_ln, MTX_NETWORK_LOCK, MTX_DEF|MTX_RECURSE);
634 if (! probe_irq (b, irq)) {
635 printf ("ct%d: irq %jd not functional\n", unit, irq);
639 bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid,
641 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
643 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
645 mtx_destroy (&bd->ct_mtx);
649 callout_init (&led_timo[unit], 1);
651 if (bus_setup_intr (dev, bd->irq_res,
652 INTR_TYPE_NET|INTR_MPSAFE,
653 NULL, ct_intr, bd, &bd->intrhand)) {
654 printf ("ct%d: Can't setup irq %jd\n", unit, irq);
658 bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid,
660 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
662 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
664 mtx_destroy (&bd->ct_mtx);
670 ct_init_board (b, b->num, b->port, irq, drq, b->type, b->osc);
671 ct_setup_board (b, 0, 0, 0);
674 printf ("ct%d: <Cronyx-%s>, clock %s MHz\n", b->num, b->name,
675 b->osc == 20000000 ? "20" : "16.384");
677 for (c = b->chan; c < b->chan + NCHAN; ++c) {
678 d = &bd->channel[c->num];
679 d->dmamem.size = sizeof(ct_buf_t);
680 if (! ct_bus_dma_mem_alloc (unit, c->num, &d->dmamem))
686 channel [b->num*NCHAN + c->num] = d;
687 sprintf (d->name, "ct%d.%d", b->num, c->num);
688 callout_init (&d->timeout_handle, 1);
691 if (ng_make_node_common (&typestruct, &d->node) != 0) {
692 printf ("%s: cannot make common node\n", d->name);
693 channel [b->num*NCHAN + c->num] = 0;
695 ct_bus_dma_mem_free (&d->dmamem);
698 NG_NODE_SET_PRIVATE (d->node, d);
699 sprintf (d->nodename, "%s%d", NG_CT_NODE_TYPE,
700 c->board->num*NCHAN + c->num);
701 if (ng_name_node (d->node, d->nodename)) {
702 printf ("%s: cannot name node\n", d->nodename);
703 NG_NODE_UNREF (d->node);
704 channel [b->num*NCHAN + c->num] = 0;
706 ct_bus_dma_mem_free (&d->dmamem);
709 d->queue.ifq_maxlen = ifqmaxlen;
710 d->hi_queue.ifq_maxlen = ifqmaxlen;
711 mtx_init (&d->queue.ifq_mtx, "ct_queue", NULL, MTX_DEF);
712 mtx_init (&d->hi_queue.ifq_mtx, "ct_queue_hi", NULL, MTX_DEF);
714 d->ifp = if_alloc(IFT_PPP);
715 if (d->ifp == NULL) {
716 printf ("%s: cannot if_alloc common interface\n",
718 channel [b->num*NCHAN + c->num] = 0;
720 ct_bus_dma_mem_free (&d->dmamem);
723 d->ifp->if_softc = d;
724 if_initname (d->ifp, "ct", b->num * NCHAN + c->num);
725 d->ifp->if_mtu = PP_MTU;
726 d->ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
727 d->ifp->if_ioctl = ct_sioctl;
728 d->ifp->if_start = ct_ifstart;
729 d->ifp->if_init = ct_initialize;
730 d->queue.ifq_maxlen = NBUF;
731 mtx_init (&d->queue.ifq_mtx, "ct_queue", NULL, MTX_DEF);
732 sppp_attach (d->ifp);
734 IFP2SP(d->ifp)->pp_tlf = ct_tlf;
735 IFP2SP(d->ifp)->pp_tls = ct_tls;
736 /* If BPF is in the kernel, call the attach for it.
737 * Header size is 4 bytes. */
738 bpfattach (d->ifp, DLT_PPP, 4);
741 ct_start_chan (c, d->dmamem.virt, d->dmamem.phys);
742 ct_register_receive (c, &ct_receive);
743 ct_register_transmit (c, &ct_transmit);
744 ct_register_error (c, &ct_error);
746 d->devt = make_dev (&ct_cdevsw, b->num*NCHAN+c->num, UID_ROOT,
747 GID_WHEEL, 0600, "ct%d", b->num*NCHAN+c->num);
754 static int ct_detach (device_t dev)
756 bdrv_t *bd = device_get_softc (dev);
757 ct_board_t *b = bd->board;
761 KASSERT (mtx_initialized (&bd->ct_mtx), ("ct mutex not initialized"));
765 /* Check if the device is busy (open). */
766 for (c = b->chan; c < b->chan + NCHAN; ++c) {
767 drv_t *d = (drv_t*) c->sys;
769 if (!d || !d->chan->type)
779 /* Deactivate the timeout routine. */
780 callout_stop (&led_timo[b->num]);
784 bus_teardown_intr (dev, bd->irq_res, bd->intrhand);
785 bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid, bd->irq_res);
787 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid, bd->drq_res);
789 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid, bd->base_res);
795 /* Detach the interfaces, free buffer memory. */
796 for (c = b->chan; c < b->chan + NCHAN; ++c) {
797 drv_t *d = (drv_t*) c->sys;
799 if (!d || !d->chan->type)
802 callout_stop (&d->timeout_handle);
805 ng_rmnode_self (d->node);
806 NG_NODE_UNREF (d->node);
809 mtx_destroy (&d->queue.ifq_mtx);
810 mtx_destroy (&d->hi_queue.ifq_mtx);
812 /* Detach from the packet filter list of interfaces. */
815 /* Detach from the sync PPP list. */
816 sppp_detach (d->ifp);
820 IF_DRAIN (&d->queue);
821 mtx_destroy (&d->queue.ifq_mtx);
823 destroy_dev (d->devt);
829 callout_drain (&led_timo[b->num]);
832 for (c = b->chan; c < b->chan + NCHAN; ++c) {
833 drv_t *d = (drv_t*) c->sys;
835 if (!d || !d->chan->type)
837 callout_drain(&d->timeout_handle);
839 /* Deallocate buffers. */
840 ct_bus_dma_mem_free (&d->dmamem);
843 adapter [b->num] = NULL;
846 mtx_destroy (&bd->ct_mtx);
852 static void ct_ifstart (struct ifnet *ifp)
854 drv_t *d = ifp->if_softc;
862 static void ct_tlf (struct sppp *sp)
864 drv_t *d = SP2IFP(sp)->if_softc;
866 CT_DEBUG (d, ("ct_tlf\n"));
867 /* ct_set_dtr (d->chan, 0);*/
868 /* ct_set_rts (d->chan, 0);*/
869 if (!(sp->pp_flags & PP_FR) && !(d->ifp->if_flags & PP_CISCO))
873 static void ct_tls (struct sppp *sp)
875 drv_t *d = SP2IFP(sp)->if_softc;
877 CT_DEBUG (d, ("ct_tls\n"));
878 if (!(sp->pp_flags & PP_FR) && !(d->ifp->if_flags & PP_CISCO))
883 * Initialization of interface.
884 * Ii seems to be never called by upper level.
886 static void ct_initialize (void *softc)
890 CT_DEBUG (d, ("ct_initialize\n"));
894 * Process an ioctl request.
896 static int ct_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data)
898 drv_t *d = ifp->if_softc;
900 int error, s, was_up, should_be_up;
902 was_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
903 error = sppp_ioctl (ifp, cmd, data);
907 if (! (ifp->if_flags & IFF_DEBUG))
910 d->chan->debug = d->chan->debug_shadow;
913 default: CT_DEBUG2 (d, ("ioctl 0x%lx\n", cmd)); return 0;
914 case SIOCADDMULTI: CT_DEBUG2 (d, ("SIOCADDMULTI\n")); return 0;
915 case SIOCDELMULTI: CT_DEBUG2 (d, ("SIOCDELMULTI\n")); return 0;
916 case SIOCSIFFLAGS: CT_DEBUG2 (d, ("SIOCSIFFLAGS\n")); break;
917 case SIOCSIFADDR: CT_DEBUG2 (d, ("SIOCSIFADDR\n")); break;
920 /* We get here only in case of SIFFLAGS or SIFADDR. */
923 should_be_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
924 if (! was_up && should_be_up) {
925 /* Interface goes up -- start it. */
928 } else if (was_up && ! should_be_up) {
929 /* Interface is going down -- stop it. */
930 /* if ((IFP2SP(d->ifp)->pp_flags & PP_FR) || (ifp->if_flags & PP_CISCO))*/
940 * Stop the interface. Called on splimp().
942 static void ct_down (drv_t *d)
945 CT_DEBUG (d, ("ct_down\n"));
946 ct_set_dtr (d->chan, 0);
947 ct_set_rts (d->chan, 0);
949 callout_stop (&d->timeout_handle);
954 * Start the interface. Called on splimp().
956 static void ct_up (drv_t *d)
959 CT_DEBUG (d, ("ct_up\n"));
960 ct_set_dtr (d->chan, 1);
961 ct_set_rts (d->chan, 1);
967 * Start output on the (slave) interface. Get another datagram to send
968 * off of the interface queue, and copy it to the interface
969 * before starting the output.
971 static void ct_send (drv_t *d)
976 CT_DEBUG2 (d, ("ct_send, tn=%d\n", d->chan->tn));
978 /* No output if the interface is down. */
982 /* No output if the modem is off. */
983 if (! ct_get_dsr (d->chan) && !ct_get_loop (d->chan))
986 while (ct_buf_free (d->chan)) {
987 /* Get the packet to send. */
989 IF_DEQUEUE (&d->hi_queue, m);
991 IF_DEQUEUE (&d->queue, m);
993 m = sppp_dequeue (d->ifp);
998 BPF_MTAP (d->ifp, m);
1000 len = m_length (m, NULL);
1002 ct_send_packet (d->chan, (u_char*)mtod (m, caddr_t),
1005 m_copydata (m, 0, len, d->chan->tbuf[d->chan->te]);
1006 ct_send_packet (d->chan, d->chan->tbuf[d->chan->te],
1011 /* Set up transmit timeout, if the transmit ring is not empty.
1012 * Transmit timeout is 10 seconds. */
1016 d->ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1021 * Start output on the interface.
1022 * Always called on splimp().
1024 static void ct_start (drv_t *d)
1030 ct_set_dtr (d->chan, 1);
1032 ct_set_rts (d->chan, 1);
1034 callout_reset (&d->timeout_handle, hz, ct_watchdog_timer, d);
1041 * Handle transmit timeouts.
1042 * Recover after lost transmit interrupts.
1043 * Always called on splimp().
1045 static void ct_watchdog (drv_t *d)
1048 CT_DEBUG (d, ("device timeout\n"));
1050 ct_setup_chan (d->chan);
1051 ct_start_chan (d->chan, 0, 0);
1052 ct_set_dtr (d->chan, 1);
1053 ct_set_rts (d->chan, 1);
1058 static void ct_watchdog_timer (void *arg)
1064 if (d->timeout == 1)
1068 callout_reset (&d->timeout_handle, hz, ct_watchdog_timer, d);
1073 * Transmit callback function.
1075 static void ct_transmit (ct_chan_t *c, void *attachment, int len)
1083 if_inc_counter(d->ifp, IFCOUNTER_OPACKETS, 1);
1084 d->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1090 * Process the received packet.
1092 static void ct_receive (ct_chan_t *c, char *data, int len)
1100 if (!d || !d->running)
1103 m = makembuf (data, len);
1105 CT_DEBUG (d, ("no memory for packet\n"));
1107 if_inc_counter(d->ifp, IFCOUNTER_IQDROPS, 1);
1114 m->m_pkthdr.rcvif = 0;
1115 NG_SEND_DATA_ONLY (error, d->hook, m);
1117 if_inc_counter(d->ifp, IFCOUNTER_IPACKETS, 1);
1118 m->m_pkthdr.rcvif = d->ifp;
1119 /* Check if there's a BPF listener on this interface.
1120 * If so, hand off the raw packet to bpf. */
1121 BPF_MTAP(d->ifp, m);
1122 IF_ENQUEUE (&d->queue, m);
1127 * Error callback function.
1129 static void ct_error (ct_chan_t *c, int data)
1138 CT_DEBUG (d, ("frame error\n"));
1140 if_inc_counter(d->ifp, IFCOUNTER_IERRORS, 1);
1144 CT_DEBUG (d, ("crc error\n"));
1146 if_inc_counter(d->ifp, IFCOUNTER_IERRORS, 1);
1150 CT_DEBUG (d, ("overrun error\n"));
1152 if_inc_counter(d->ifp, IFCOUNTER_COLLISIONS, 1);
1153 if_inc_counter(d->ifp, IFCOUNTER_IERRORS, 1);
1157 CT_DEBUG (d, ("overflow error\n"));
1159 if_inc_counter(d->ifp, IFCOUNTER_IERRORS, 1);
1163 CT_DEBUG (d, ("underrun error\n"));
1166 if_inc_counter(d->ifp, IFCOUNTER_OERRORS, 1);
1167 d->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1172 CT_DEBUG (d, ("error #%d\n", data));
1176 static int ct_open (struct cdev *dev, int oflags, int devtype, struct thread *td)
1180 if (dev2unit(dev) >= NCTAU*NCHAN || ! (d = channel[dev2unit(dev)]))
1183 CT_DEBUG2 (d, ("ct_open\n"));
1187 static int ct_close (struct cdev *dev, int fflag, int devtype, struct thread *td)
1189 drv_t *d = channel [dev2unit(dev)];
1194 CT_DEBUG2 (d, ("ct_close\n"));
1198 static int ct_modem_status (ct_chan_t *c)
1209 status = d->running ? TIOCM_LE : 0;
1212 if (ct_get_cd (c)) status |= TIOCM_CD;
1213 if (ct_get_cts (c)) status |= TIOCM_CTS;
1214 if (ct_get_dsr (c)) status |= TIOCM_DSR;
1215 if (c->dtr) status |= TIOCM_DTR;
1216 if (c->rts) status |= TIOCM_RTS;
1223 * Process an ioctl request on /dev/cronyx/ctauN.
1225 static int ct_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
1227 drv_t *d = channel [dev2unit (dev)];
1230 struct serial_statistics *st;
1231 struct e1_statistics *opte1;
1242 case SERIAL_GETREGISTERED:
1243 bzero (mask, sizeof(mask));
1244 for (s=0; s<NCTAU*NCHAN; ++s)
1246 mask [s/8] |= 1 << (s & 7);
1247 bcopy (mask, data, sizeof (mask));
1251 case SERIAL_GETPROTO:
1252 strcpy ((char*)data, (IFP2SP(d->ifp)->pp_flags & PP_FR) ? "fr" :
1253 (d->ifp->if_flags & PP_CISCO) ? "cisco" : "ppp");
1256 case SERIAL_SETPROTO:
1257 /* Only for superuser! */
1258 error = priv_check (td, PRIV_DRIVER);
1261 if (d->ifp->if_drv_flags & IFF_DRV_RUNNING)
1263 if (! strcmp ("cisco", (char*)data)) {
1264 IFP2SP(d->ifp)->pp_flags &= ~(PP_FR);
1265 IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE;
1266 d->ifp->if_flags |= PP_CISCO;
1267 } else if (! strcmp ("fr", (char*)data)) {
1268 d->ifp->if_flags &= ~(PP_CISCO);
1269 IFP2SP(d->ifp)->pp_flags |= PP_FR | PP_KEEPALIVE;
1270 } else if (! strcmp ("ppp", (char*)data)) {
1271 IFP2SP(d->ifp)->pp_flags &= ~(PP_FR | PP_KEEPALIVE);
1272 d->ifp->if_flags &= ~(PP_CISCO);
1277 case SERIAL_GETKEEPALIVE:
1278 if ((IFP2SP(d->ifp)->pp_flags & PP_FR) ||
1279 (d->ifp->if_flags & PP_CISCO))
1281 *(int*)data = (IFP2SP(d->ifp)->pp_flags & PP_KEEPALIVE) ? 1 : 0;
1284 case SERIAL_SETKEEPALIVE:
1285 /* Only for superuser! */
1286 error = priv_check (td, PRIV_DRIVER);
1289 if ((IFP2SP(d->ifp)->pp_flags & PP_FR) ||
1290 (d->ifp->if_flags & PP_CISCO))
1293 IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE;
1295 IFP2SP(d->ifp)->pp_flags &= ~PP_KEEPALIVE;
1299 case SERIAL_GETMODE:
1300 *(int*)data = SERIAL_HDLC;
1304 if (c->mode == M_HDLC)
1306 switch (ct_get_config (c->board)) {
1307 default: *(char*)data = 'a'; break;
1308 case CFG_B: *(char*)data = 'b'; break;
1309 case CFG_C: *(char*)data = 'c'; break;
1314 /* Only for superuser! */
1315 error = priv_check (td, PRIV_DRIVER);
1318 if (c->mode == M_HDLC)
1322 switch (*(char*)data) {
1323 case 'a': ct_set_config (c->board, CFG_A); break;
1324 case 'b': ct_set_config (c->board, CFG_B); break;
1325 case 'c': ct_set_config (c->board, CFG_C); break;
1331 case SERIAL_GETSTAT:
1332 st = (struct serial_statistics*) data;
1333 st->rintr = c->rintr;
1334 st->tintr = c->tintr;
1335 st->mintr = c->mintr;
1336 st->ibytes = c->ibytes;
1337 st->ipkts = c->ipkts;
1338 st->ierrs = c->ierrs;
1339 st->obytes = c->obytes;
1340 st->opkts = c->opkts;
1341 st->oerrs = c->oerrs;
1344 case SERIAL_GETESTAT:
1345 opte1 = (struct e1_statistics*)data;
1346 opte1->status = c->status;
1347 opte1->cursec = c->cursec;
1348 opte1->totsec = c->totsec + c->cursec;
1350 opte1->currnt.bpv = c->currnt.bpv;
1351 opte1->currnt.fse = c->currnt.fse;
1352 opte1->currnt.crce = c->currnt.crce;
1353 opte1->currnt.rcrce = c->currnt.rcrce;
1354 opte1->currnt.uas = c->currnt.uas;
1355 opte1->currnt.les = c->currnt.les;
1356 opte1->currnt.es = c->currnt.es;
1357 opte1->currnt.bes = c->currnt.bes;
1358 opte1->currnt.ses = c->currnt.ses;
1359 opte1->currnt.oofs = c->currnt.oofs;
1360 opte1->currnt.css = c->currnt.css;
1361 opte1->currnt.dm = c->currnt.dm;
1363 opte1->total.bpv = c->total.bpv + c->currnt.bpv;
1364 opte1->total.fse = c->total.fse + c->currnt.fse;
1365 opte1->total.crce = c->total.crce + c->currnt.crce;
1366 opte1->total.rcrce = c->total.rcrce + c->currnt.rcrce;
1367 opte1->total.uas = c->total.uas + c->currnt.uas;
1368 opte1->total.les = c->total.les + c->currnt.les;
1369 opte1->total.es = c->total.es + c->currnt.es;
1370 opte1->total.bes = c->total.bes + c->currnt.bes;
1371 opte1->total.ses = c->total.ses + c->currnt.ses;
1372 opte1->total.oofs = c->total.oofs + c->currnt.oofs;
1373 opte1->total.css = c->total.css + c->currnt.css;
1374 opte1->total.dm = c->total.dm + c->currnt.dm;
1375 for (s=0; s<48; ++s) {
1376 opte1->interval[s].bpv = c->interval[s].bpv;
1377 opte1->interval[s].fse = c->interval[s].fse;
1378 opte1->interval[s].crce = c->interval[s].crce;
1379 opte1->interval[s].rcrce = c->interval[s].rcrce;
1380 opte1->interval[s].uas = c->interval[s].uas;
1381 opte1->interval[s].les = c->interval[s].les;
1382 opte1->interval[s].es = c->interval[s].es;
1383 opte1->interval[s].bes = c->interval[s].bes;
1384 opte1->interval[s].ses = c->interval[s].ses;
1385 opte1->interval[s].oofs = c->interval[s].oofs;
1386 opte1->interval[s].css = c->interval[s].css;
1387 opte1->interval[s].dm = c->interval[s].dm;
1391 case SERIAL_CLRSTAT:
1392 /* Only for superuser! */
1393 error = priv_check (td, PRIV_DRIVER);
1405 bzero (&c->currnt, sizeof (c->currnt));
1406 bzero (&c->total, sizeof (c->total));
1407 bzero (c->interval, sizeof (c->interval));
1410 case SERIAL_GETBAUD:
1411 *(long*)data = ct_get_baud(c);
1414 case SERIAL_SETBAUD:
1415 /* Only for superuser! */
1416 error = priv_check (td, PRIV_DRIVER);
1421 ct_set_baud (c, *(long*)data);
1426 case SERIAL_GETLOOP:
1427 *(int*)data = ct_get_loop (c);
1430 case SERIAL_SETLOOP:
1431 /* Only for superuser! */
1432 error = priv_check (td, PRIV_DRIVER);
1437 ct_set_loop (c, *(int*)data);
1442 case SERIAL_GETDPLL:
1443 if (c->mode == M_E1 || c->mode == M_G703)
1445 *(int*)data = ct_get_dpll (c);
1448 case SERIAL_SETDPLL:
1449 /* Only for superuser! */
1450 error = priv_check (td, PRIV_DRIVER);
1453 if (c->mode == M_E1 || c->mode == M_G703)
1457 ct_set_dpll (c, *(int*)data);
1462 case SERIAL_GETNRZI:
1463 if (c->mode == M_E1 || c->mode == M_G703)
1465 *(int*)data = ct_get_nrzi (c);
1468 case SERIAL_SETNRZI:
1469 /* Only for superuser! */
1470 error = priv_check (td, PRIV_DRIVER);
1473 if (c->mode == M_E1 || c->mode == M_G703)
1477 ct_set_nrzi (c, *(int*)data);
1482 case SERIAL_GETDEBUG:
1483 *(int*)data = c->debug;
1486 case SERIAL_SETDEBUG:
1487 /* Only for superuser! */
1488 error = priv_check (td, PRIV_DRIVER);
1493 * The debug_shadow is always greater than zero for logic
1494 * simplicity. For switching debug off the IFF_DEBUG is
1497 c->debug_shadow = (*(int*)data) ? (*(int*)data) : 1;
1498 if (d->ifp->if_flags & IFF_DEBUG)
1499 c->debug = c->debug_shadow;
1501 c->debug = *(int*)data;
1505 case SERIAL_GETHIGAIN:
1506 if (c->mode != M_E1)
1508 *(int*)data = ct_get_higain (c);
1511 case SERIAL_SETHIGAIN:
1512 /* Only for superuser! */
1513 error = priv_check (td, PRIV_DRIVER);
1518 ct_set_higain (c, *(int*)data);
1523 case SERIAL_GETPHONY:
1524 CT_DEBUG2 (d, ("ioctl: getphony\n"));
1525 if (c->mode != M_E1)
1527 *(int*)data = c->gopt.phony;
1530 case SERIAL_SETPHONY:
1531 CT_DEBUG2 (d, ("ioctl: setphony\n"));
1532 if (c->mode != M_E1)
1534 /* Only for superuser! */
1535 error = priv_check (td, PRIV_DRIVER);
1540 ct_set_phony (c, *(int*)data);
1546 if (c->mode != M_E1 && c->mode != M_G703)
1548 switch (ct_get_clk(c)) {
1549 default: *(int*)data = E1CLK_INTERNAL; break;
1550 case GCLK_RCV: *(int*)data = E1CLK_RECEIVE; break;
1551 case GCLK_RCLKO: *(int*)data = c->num ?
1552 E1CLK_RECEIVE_CHAN0 : E1CLK_RECEIVE_CHAN1; break;
1557 /* Only for superuser! */
1558 error = priv_check (td, PRIV_DRIVER);
1563 switch (*(int*)data) {
1564 default: ct_set_clk (c, GCLK_INT); break;
1565 case E1CLK_RECEIVE: ct_set_clk (c, GCLK_RCV); break;
1566 case E1CLK_RECEIVE_CHAN0:
1567 case E1CLK_RECEIVE_CHAN1:
1568 ct_set_clk (c, GCLK_RCLKO); break;
1574 case SERIAL_GETTIMESLOTS:
1575 if (c->mode != M_E1)
1577 *(long*)data = ct_get_ts (c);
1580 case SERIAL_SETTIMESLOTS:
1581 /* Only for superuser! */
1582 error = priv_check (td, PRIV_DRIVER);
1587 ct_set_ts (c, *(long*)data);
1592 case SERIAL_GETSUBCHAN:
1593 if (c->mode != M_E1)
1595 *(long*)data = ct_get_subchan (c->board);
1598 case SERIAL_SETSUBCHAN:
1599 /* Only for superuser! */
1600 error = priv_check (td, PRIV_DRIVER);
1605 ct_set_subchan (c->board, *(long*)data);
1610 case SERIAL_GETINVCLK:
1611 case SERIAL_GETINVTCLK:
1612 if (c->mode == M_E1 || c->mode == M_G703)
1614 *(int*)data = ct_get_invtxc (c);
1617 case SERIAL_GETINVRCLK:
1618 if (c->mode == M_E1 || c->mode == M_G703)
1620 *(int*)data = ct_get_invrxc (c);
1623 case SERIAL_SETINVCLK:
1624 case SERIAL_SETINVTCLK:
1625 /* Only for superuser! */
1626 error = priv_check (td, PRIV_DRIVER);
1629 if (c->mode == M_E1 || c->mode == M_G703)
1633 ct_set_invtxc (c, *(int*)data);
1638 case SERIAL_SETINVRCLK:
1639 /* Only for superuser! */
1640 error = priv_check (td, PRIV_DRIVER);
1643 if (c->mode == M_E1 || c->mode == M_G703)
1647 ct_set_invrxc (c, *(int*)data);
1652 case SERIAL_GETLEVEL:
1653 if (c->mode != M_G703)
1657 *(int*)data = ct_get_lq (c);
1662 case TIOCSDTR: /* Set DTR */
1670 case TIOCCDTR: /* Clear DTR */
1678 case TIOCMSET: /* Set DTR/RTS */
1681 ct_set_dtr (c, (*(int*)data & TIOCM_DTR) ? 1 : 0);
1682 ct_set_rts (c, (*(int*)data & TIOCM_RTS) ? 1 : 0);
1687 case TIOCMBIS: /* Add DTR/RTS */
1690 if (*(int*)data & TIOCM_DTR) ct_set_dtr (c, 1);
1691 if (*(int*)data & TIOCM_RTS) ct_set_rts (c, 1);
1696 case TIOCMBIC: /* Clear DTR/RTS */
1699 if (*(int*)data & TIOCM_DTR) ct_set_dtr (c, 0);
1700 if (*(int*)data & TIOCM_RTS) ct_set_rts (c, 0);
1705 case TIOCMGET: /* Get modem status */
1706 *(int*)data = ct_modem_status (c);
1713 static int ng_ct_constructor (node_p node)
1715 drv_t *d = NG_NODE_PRIVATE (node);
1716 CT_DEBUG (d, ("Constructor\n"));
1720 static int ng_ct_newhook (node_p node, hook_p hook, const char *name)
1723 drv_t *d = NG_NODE_PRIVATE (node);
1730 /* Attach debug hook */
1731 if (strcmp (name, NG_CT_HOOK_DEBUG) == 0) {
1732 NG_HOOK_SET_PRIVATE (hook, NULL);
1733 d->debug_hook = hook;
1737 /* Check for raw hook */
1738 if (strcmp (name, NG_CT_HOOK_RAW) != 0)
1741 NG_HOOK_SET_PRIVATE (hook, d);
1751 static char *format_timeslots (u_long s)
1753 static char buf [100];
1757 for (i=1; i<32; ++i)
1759 int prev = (i > 1) & (s >> (i-1));
1760 int next = (i < 31) & (s >> (i+1));
1770 *p++ = '0' + i / 10;
1771 *p++ = '0' + i % 10;
1777 static int print_modems (char *s, ct_chan_t *c, int need_header)
1779 int status = ct_modem_status (c);
1783 length += sprintf (s + length, " LE DTR DSR RTS CTS CD\n");
1784 length += sprintf (s + length, "%4s %4s %4s %4s %4s %4s\n",
1785 status & TIOCM_LE ? "On" : "-",
1786 status & TIOCM_DTR ? "On" : "-",
1787 status & TIOCM_DSR ? "On" : "-",
1788 status & TIOCM_RTS ? "On" : "-",
1789 status & TIOCM_CTS ? "On" : "-",
1790 status & TIOCM_CD ? "On" : "-");
1794 static int print_stats (char *s, ct_chan_t *c, int need_header)
1796 struct serial_statistics st;
1799 st.rintr = c->rintr;
1800 st.tintr = c->tintr;
1801 st.mintr = c->mintr;
1802 st.ibytes = c->ibytes;
1803 st.ipkts = c->ipkts;
1804 st.ierrs = c->ierrs;
1805 st.obytes = c->obytes;
1806 st.opkts = c->opkts;
1807 st.oerrs = c->oerrs;
1809 length += sprintf (s + length, " Rintr Tintr Mintr Ibytes Ipkts Ierrs Obytes Opkts Oerrs\n");
1810 length += sprintf (s + length, "%7ld %7ld %7ld %8ld %7ld %7ld %8ld %7ld %7ld\n",
1811 st.rintr, st.tintr, st.mintr, st.ibytes, st.ipkts,
1812 st.ierrs, st.obytes, st.opkts, st.oerrs);
1816 static char *format_e1_status (u_char status)
1818 static char buf [80];
1820 if (status & E1_NOALARM)
1823 if (status & E1_LOS) strcat (buf, ",LOS");
1824 if (status & E1_AIS) strcat (buf, ",AIS");
1825 if (status & E1_LOF) strcat (buf, ",LOF");
1826 if (status & E1_LOMF) strcat (buf, ",LOMF");
1827 if (status & E1_FARLOF) strcat (buf, ",FARLOF");
1828 if (status & E1_AIS16) strcat (buf, ",AIS16");
1829 if (status & E1_FARLOMF) strcat (buf, ",FARLOMF");
1830 if (status & E1_TSTREQ) strcat (buf, ",TSTREQ");
1831 if (status & E1_TSTERR) strcat (buf, ",TSTERR");
1837 static int print_frac (char *s, int leftalign, u_long numerator, u_long divider)
1841 if (numerator < 1 || divider < 1) {
1842 length += sprintf (s+length, leftalign ? "/- " : " -");
1845 n = (int) (0.5 + 1000.0 * numerator / divider);
1847 length += sprintf (s+length, leftalign ? "/.%-3d" : " .%03d", n);
1850 *(s + length) = leftalign ? '/' : ' ';
1853 if (n >= 1000000) n = (n+500) / 1000 * 1000;
1854 else if (n >= 100000) n = (n+50) / 100 * 100;
1855 else if (n >= 10000) n = (n+5) / 10 * 10;
1858 case 1000: length += printf (s+length, ".999"); return length;
1859 case 10000: n = 9990; break;
1860 case 100000: n = 99900; break;
1861 case 1000000: n = 999000; break;
1863 if (n < 10000) length += sprintf (s+length, "%d.%d", n/1000, n/10%100);
1864 else if (n < 100000) length += sprintf (s+length, "%d.%d", n/1000, n/100%10);
1865 else if (n < 1000000) length += sprintf (s+length, "%d.", n/1000);
1866 else length += sprintf (s+length, "%d", n/1000);
1871 static int print_e1_stats (char *s, ct_chan_t *c)
1873 struct e1_counters total;
1877 totsec = c->totsec + c->cursec;
1878 total.bpv = c->total.bpv + c->currnt.bpv;
1879 total.fse = c->total.fse + c->currnt.fse;
1880 total.crce = c->total.crce + c->currnt.crce;
1881 total.rcrce = c->total.rcrce + c->currnt.rcrce;
1882 total.uas = c->total.uas + c->currnt.uas;
1883 total.les = c->total.les + c->currnt.les;
1884 total.es = c->total.es + c->currnt.es;
1885 total.bes = c->total.bes + c->currnt.bes;
1886 total.ses = c->total.ses + c->currnt.ses;
1887 total.oofs = c->total.oofs + c->currnt.oofs;
1888 total.css = c->total.css + c->currnt.css;
1889 total.dm = c->total.dm + c->currnt.dm;
1891 length += sprintf (s + length, " Unav/Degr Bpv/Fsyn CRC/RCRC Err/Lerr Sev/Bur Oof/Slp Status\n");
1893 /* Unavailable seconds, degraded minutes */
1894 length += print_frac (s + length, 0, c->currnt.uas, c->cursec);
1895 length += print_frac (s + length, 1, 60 * c->currnt.dm, c->cursec);
1897 /* Bipolar violations, frame sync errors */
1898 length += print_frac (s + length, 0, c->currnt.bpv, c->cursec);
1899 length += print_frac (s + length, 1, c->currnt.fse, c->cursec);
1901 /* CRC errors, remote CRC errors (E-bit) */
1902 length += print_frac (s + length, 0, c->currnt.crce, c->cursec);
1903 length += print_frac (s + length, 1, c->currnt.rcrce, c->cursec);
1905 /* Errored seconds, line errored seconds */
1906 length += print_frac (s + length, 0, c->currnt.es, c->cursec);
1907 length += print_frac (s + length, 1, c->currnt.les, c->cursec);
1909 /* Severely errored seconds, burst errored seconds */
1910 length += print_frac (s + length, 0, c->currnt.ses, c->cursec);
1911 length += print_frac (s + length, 1, c->currnt.bes, c->cursec);
1913 /* Out of frame seconds, controlled slip seconds */
1914 length += print_frac (s + length, 0, c->currnt.oofs, c->cursec);
1915 length += print_frac (s + length, 1, c->currnt.css, c->cursec);
1917 length += sprintf (s + length, " %s\n", format_e1_status (c->status));
1919 /* Print total statistics. */
1920 length += print_frac (s + length, 0, total.uas, totsec);
1921 length += print_frac (s + length, 1, 60 * total.dm, totsec);
1923 length += print_frac (s + length, 0, total.bpv, totsec);
1924 length += print_frac (s + length, 1, total.fse, totsec);
1926 length += print_frac (s + length, 0, total.crce, totsec);
1927 length += print_frac (s + length, 1, total.rcrce, totsec);
1929 length += print_frac (s + length, 0, total.es, totsec);
1930 length += print_frac (s + length, 1, total.les, totsec);
1932 length += print_frac (s + length, 0, total.ses, totsec);
1933 length += print_frac (s + length, 1, total.bes, totsec);
1935 length += print_frac (s + length, 0, total.oofs, totsec);
1936 length += print_frac (s + length, 1, total.css, totsec);
1938 length += sprintf (s + length, " -- Total\n");
1942 static int print_chan (char *s, ct_chan_t *c)
1948 length += sprintf (s + length, "ct%d", c->board->num * NCHAN + c->num);
1950 length += sprintf (s + length, " debug=%d", d->chan->debug);
1952 switch (ct_get_config (c->board)) {
1953 case CFG_A: length += sprintf (s + length, " cfg=A"); break;
1954 case CFG_B: length += sprintf (s + length, " cfg=B"); break;
1955 case CFG_C: length += sprintf (s + length, " cfg=C"); break;
1956 default: length += sprintf (s + length, " cfg=unknown"); break;
1959 if (ct_get_baud (c))
1960 length += sprintf (s + length, " %ld", ct_get_baud (c));
1962 length += sprintf (s + length, " extclock");
1964 if (c->mode == M_E1 || c->mode == M_G703)
1965 switch (ct_get_clk(c)) {
1966 case GCLK_INT : length += sprintf (s + length, " syn=int"); break;
1967 case GCLK_RCV : length += sprintf (s + length, " syn=rcv"); break;
1968 case GCLK_RCLKO : length += sprintf (s + length, " syn=xrcv"); break;
1970 if (c->mode == M_HDLC) {
1971 length += sprintf (s + length, " dpll=%s", ct_get_dpll (c) ? "on" : "off");
1972 length += sprintf (s + length, " nrzi=%s", ct_get_nrzi (c) ? "on" : "off");
1973 length += sprintf (s + length, " invtclk=%s", ct_get_invtxc (c) ? "on" : "off");
1974 length += sprintf (s + length, " invrclk=%s", ct_get_invrxc (c) ? "on" : "off");
1976 if (c->mode == M_E1)
1977 length += sprintf (s + length, " higain=%s", ct_get_higain (c)? "on" : "off");
1979 length += sprintf (s + length, " loop=%s", ct_get_loop (c) ? "on" : "off");
1981 if (c->mode == M_E1)
1982 length += sprintf (s + length, " ts=%s", format_timeslots (ct_get_ts(c)));
1983 if (c->mode == M_E1 && ct_get_config (c->board) != CFG_A)
1984 length += sprintf (s + length, " pass=%s", format_timeslots (ct_get_subchan(c->board)));
1985 if (c->mode == M_G703) {
1993 length += sprintf (s + length, " (level=-%.1fdB)", lq / 10.0);
1995 length += sprintf (s + length, "\n");
1999 static int ng_ct_rcvmsg (node_p node, item_p item, hook_p lasthook)
2001 drv_t *d = NG_NODE_PRIVATE (node);
2002 struct ng_mesg *msg;
2003 struct ng_mesg *resp = NULL;
2009 CT_DEBUG (d, ("Rcvmsg\n"));
2010 NGI_GET_MSG (item, msg);
2011 switch (msg->header.typecookie) {
2017 printf ("Don't forget to implement\n");
2021 case NGM_GENERIC_COOKIE:
2022 switch (msg->header.cmd) {
2027 case NGM_TEXT_STATUS: {
2030 int dl = sizeof (struct ng_mesg) + 730;
2032 NG_MKRESPONSE (resp, msg, dl, M_NOWAIT);
2038 l += print_chan (s + l, d->chan);
2039 l += print_stats (s + l, d->chan, 1);
2040 l += print_modems (s + l, d->chan, 1);
2041 l += print_e1_stats (s + l, d->chan);
2042 strncpy ((resp)->header.cmdstr, "status", NG_CMDSTRSIZ);
2048 NG_RESPOND_MSG (error, node, item, resp);
2053 static int ng_ct_rcvdata (hook_p hook, item_p item)
2055 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE(hook));
2057 struct ng_tag_prio *ptag;
2066 NGI_GET_M (item, m);
2067 NG_FREE_ITEM (item);
2068 if (! NG_HOOK_PRIVATE (hook) || ! d) {
2073 /* Check for high priority data */
2074 if ((ptag = (struct ng_tag_prio *)m_tag_locate(m, NGM_GENERIC_COOKIE,
2075 NG_TAG_PRIO, NULL)) != NULL && (ptag->priority > NG_PRIO_CUTOFF) )
2083 if (_IF_QFULL (q)) {
2098 static int ng_ct_rmnode (node_p node)
2100 drv_t *d = NG_NODE_PRIVATE (node);
2103 CT_DEBUG (d, ("Rmnode\n"));
2104 if (d && d->running) {
2113 if (node->nd_flags & NGF_REALLY_DIE) {
2114 NG_NODE_SET_PRIVATE (node, NULL);
2115 NG_NODE_UNREF (node);
2117 NG_NODE_REVIVE(node); /* Persistant node */
2122 static int ng_ct_connect (hook_p hook)
2124 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2129 callout_reset (&d->timeout_handle, hz, ct_watchdog_timer, d);
2133 static int ng_ct_disconnect (hook_p hook)
2135 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2144 if (NG_HOOK_PRIVATE (hook))
2147 /* If we were wait it than it reasserted now, just stop it. */
2148 if (!callout_drain (&d->timeout_handle))
2149 callout_stop (&d->timeout_handle);
2154 static int ct_modevent (module_t mod, int type, void *unused)
2156 static int load_count = 0;
2161 if (ng_newtype (&typestruct))
2162 printf ("Failed to register ng_ct\n");
2165 callout_init (&timeout_handle, 1);
2166 callout_reset (&timeout_handle, hz*5, ct_timeout, 0);
2169 if (load_count == 1) {
2170 printf ("Removing device entry for Tau-ISA\n");
2172 ng_rmtype (&typestruct);
2175 /* If we were wait it than it reasserted now, just stop it. */
2176 if (!callout_drain (&timeout_handle))
2177 callout_stop (&timeout_handle);
2187 static struct ng_type typestruct = {
2188 .version = NG_ABI_VERSION,
2189 .name = NG_CT_NODE_TYPE,
2190 .constructor = ng_ct_constructor,
2191 .rcvmsg = ng_ct_rcvmsg,
2192 .shutdown = ng_ct_rmnode,
2193 .newhook = ng_ct_newhook,
2194 .connect = ng_ct_connect,
2195 .rcvdata = ng_ct_rcvdata,
2196 .disconnect = ng_ct_disconnect,
2201 MODULE_DEPEND (ng_ct, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION);
2203 MODULE_DEPEND (ct, sppp, 1, 1, 1);
2205 DRIVER_MODULE (ct, isa, ct_isa_driver, ct_devclass, ct_modevent, NULL);
2206 MODULE_VERSION (ct, 1);