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 <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 #define CT_LOCK(_bd) mtx_lock (&(_bd)->ct_mtx)
83 #define CT_UNLOCK(_bd) mtx_unlock (&(_bd)->ct_mtx)
84 #define CT_LOCK_ASSERT(_bd) mtx_assert (&(_bd)->ct_mtx, MA_OWNED)
86 static void ct_identify __P((driver_t *, device_t));
87 static int ct_probe __P((device_t));
88 static int ct_attach __P((device_t));
89 static int ct_detach __P((device_t));
91 static device_method_t ct_isa_methods [] = {
92 DEVMETHOD(device_identify, ct_identify),
93 DEVMETHOD(device_probe, ct_probe),
94 DEVMETHOD(device_attach, ct_attach),
95 DEVMETHOD(device_detach, ct_detach),
100 typedef struct _ct_dma_mem_t {
108 typedef struct _drv_t {
116 char nodename [NG_NODESIZ];
120 struct ifqueue queue;
121 struct ifqueue hi_queue;
123 struct ifqueue queue;
127 struct callout timeout_handle;
131 typedef struct _bdrv_t {
133 struct resource *base_res;
134 struct resource *drq_res;
135 struct resource *irq_res;
140 drv_t channel [NCHAN];
144 static driver_t ct_isa_driver = {
150 static devclass_t ct_devclass;
152 static void ct_receive (ct_chan_t *c, char *data, int len);
153 static void ct_transmit (ct_chan_t *c, void *attachment, int len);
154 static void ct_error (ct_chan_t *c, int data);
155 static void ct_up (drv_t *d);
156 static void ct_start (drv_t *d);
157 static void ct_down (drv_t *d);
158 static void ct_watchdog (drv_t *d);
159 static void ct_watchdog_timer (void *arg);
161 extern struct ng_type typestruct;
163 static void ct_ifstart (struct ifnet *ifp);
164 static void ct_tlf (struct sppp *sp);
165 static void ct_tls (struct sppp *sp);
166 static int ct_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data);
167 static void ct_initialize (void *softc);
170 static ct_board_t *adapter [NCTAU];
171 static drv_t *channel [NCTAU*NCHAN];
172 static struct callout led_timo [NCTAU];
173 static struct callout timeout_handle;
175 static int ct_open (struct cdev *dev, int oflags, int devtype, struct thread *td);
176 static int ct_close (struct cdev *dev, int fflag, int devtype, struct thread *td);
177 static int ct_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td);
178 static struct cdevsw ct_cdevsw = {
179 .d_version = D_VERSION,
187 * Print the mbuf chain, for debug purposes only.
189 static void printmbuf (struct mbuf *m)
192 for (; m; m=m->m_next) {
193 if (m->m_flags & M_PKTHDR)
194 printf (" HDR %d:", m->m_pkthdr.len);
195 if (m->m_flags & M_EXT)
197 printf (" %d", m->m_len);
203 * Make an mbuf from data.
205 static struct mbuf *makembuf (void *buf, u_int len)
209 MGETHDR (m, M_NOWAIT, MT_DATA);
212 MCLGET (m, M_NOWAIT);
213 if (! (m->m_flags & M_EXT)) {
217 m->m_pkthdr.len = m->m_len = len;
218 bcopy (buf, mtod (m, caddr_t), len);
222 static void ct_timeout (void *arg)
227 for (i = 0; i < NCTAU; ++i) {
228 if (adapter[i] == NULL)
230 for (k = 0; k < NCHAN; k++) {
231 d = channel[i * NCHAN + k];
234 if (d->chan->mode != M_G703)
237 CT_LOCK ((bdrv_t *)d->bd);
238 ct_g703_timer (d->chan);
239 CT_UNLOCK ((bdrv_t *)d->bd);
244 callout_reset (&timeout_handle, hz, ct_timeout, 0);
247 static void ct_led_off (void *arg)
250 bdrv_t *bd = ((drv_t *)b->chan->sys)->bd;
260 * Activate interrupt handler from DDK.
262 static void ct_intr (void *arg)
265 ct_board_t *b = bd->board;
277 /* Turn LED off 50 msec later. */
278 callout_reset (&led_timo[b->num], hz/20, ct_led_off, b);
283 /* Pass packets in a lock-free state */
284 for (i = 0; i < NCHAN && b->chan[i].type; i++) {
285 drv_t *d = b->chan[i].sys;
287 if (!d || !d->running)
289 while (_IF_QLEN(&d->queue)) {
290 IF_DEQUEUE (&d->queue,m);
293 sppp_input (d->ifp, m);
299 static int probe_irq (ct_board_t *b, int irq)
303 /* Clear pending irq, if any. */
304 ct_probe_irq (b, -irq);
306 for (cnt=0; cnt<5; ++cnt) {
307 /* Get the mask of pending irqs, assuming they are busy.
308 * Activate the adapter on given irq. */
309 busy = ct_probe_irq (b, irq);
312 /* Get the mask of active irqs.
313 * Deactivate our irq. */
314 mask = ct_probe_irq (b, -irq);
316 if ((mask & ~busy) == 1 << irq) {
318 /* printf ("ct%d: irq %d ok, mask=0x%04x, busy=0x%04x\n",
319 b->num, irq, mask, busy); */
323 /* printf ("ct%d: irq %d not functional, mask=0x%04x, busy=0x%04x\n",
324 b->num, irq, mask, busy); */
329 static short porttab [] = {
330 0x200, 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0,
331 0x300, 0x320, 0x340, 0x360, 0x380, 0x3a0, 0x3c0, 0x3e0, 0
333 static char dmatab [] = { 7, 6, 5, 0 };
334 static char irqtab [] = { 5, 10, 11, 7, 3, 15, 12, 0 };
336 static int ct_is_free_res (device_t dev, int rid, int type, u_long start,
337 u_long end, u_long count)
339 struct resource *res;
341 if (!(res = bus_alloc_resource (dev, type, &rid, start, end, count,
345 bus_release_resource (dev, type, rid, res);
350 static void ct_identify (driver_t *driver, device_t dev)
352 u_long iobase, rescount;
356 devclass_t my_devclass;
359 if ((my_devclass = devclass_find ("ct")) == NULL)
362 devclass_get_devices (my_devclass, &devices, &devcount);
365 /* We should find all devices by our self. We could alter other
366 * devices, but we don't have a choise
368 for (i = 0; (iobase = porttab [i]) != 0; i++) {
369 if (!ct_is_free_res (dev, 0, SYS_RES_IOPORT,
370 iobase, iobase + NPORT, NPORT))
372 if (ct_probe_board (iobase, -1, -1) == 0)
376 child = BUS_ADD_CHILD (dev, ISA_ORDER_SPECULATIVE, "ct",
382 device_set_desc_copy (child, "Cronyx Tau-ISA");
383 device_set_driver (child, driver);
384 bus_set_resource (child, SYS_RES_IOPORT, 0,
387 if (devcount >= NCTAU)
391 static short porttab [] = {
392 0x200, 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0,
393 0x300, 0x320, 0x340, 0x360, 0x380, 0x3a0, 0x3c0, 0x3e0, 0
395 /* Lets check user choise.
397 for (k = 0; k < devcount; k++) {
398 if (bus_get_resource (devices[k], SYS_RES_IOPORT, 0,
399 &iobase, &rescount) != 0)
402 for (i = 0; porttab [i] != 0; i++) {
403 if (porttab [i] != iobase)
406 if (!ct_is_free_res (devices[k], 0, SYS_RES_IOPORT,
407 iobase, iobase + NPORT, NPORT))
410 if (ct_probe_board (iobase, -1, -1) == 0)
413 device_set_desc_copy (devices[k], "Cronyx Tau-ISA");
416 if (porttab [i] == 0) {
417 device_delete_child (
418 device_get_parent (devices[k]),
424 for (k = 0; k < devcount; k++) {
427 if (bus_get_resource (devices[k], SYS_RES_IOPORT, 0,
428 &iobase, &rescount) == 0)
430 for (i = 0; (iobase = porttab [i]) != 0; i++) {
431 if (porttab [i] == -1)
433 if (!ct_is_free_res (devices[k], 0, SYS_RES_IOPORT,
434 iobase, iobase + NPORT, NPORT))
436 if (ct_probe_board (iobase, -1, -1) == 0)
439 bus_set_resource (devices[k], SYS_RES_IOPORT, 0,
442 device_set_desc_copy (devices[k], "Cronyx Tau-ISA");
445 if (porttab [i] == 0) {
446 device_delete_child (
447 device_get_parent (devices[k]),
451 free (devices, M_TEMP);
457 static int ct_probe (device_t dev)
459 int unit = device_get_unit (dev);
460 u_long iobase, rescount;
462 if (!device_get_desc (dev) ||
463 strcmp (device_get_desc (dev), "Cronyx Tau-ISA"))
466 /* KASSERT ((bd != NULL), ("ct%d: NULL device softc\n", unit));*/
467 if (bus_get_resource (dev, SYS_RES_IOPORT, 0, &iobase, &rescount) != 0) {
468 printf ("ct%d: Couldn't get IOPORT\n", unit);
472 if (!ct_is_free_res (dev, 0, SYS_RES_IOPORT,
473 iobase, iobase + NPORT, NPORT)) {
474 printf ("ct%d: Resource IOPORT isn't free\n", unit);
478 if (!ct_probe_board (iobase, -1, -1)) {
479 printf ("ct%d: probing for Tau-ISA at %lx faild\n", unit, iobase);
487 ct_bus_dmamap_addr (void *arg, bus_dma_segment_t *segs, int nseg, int error)
494 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
496 *addr = segs->ds_addr;
500 ct_bus_dma_mem_alloc (int bnum, int cnum, ct_dma_mem_t *dmem)
504 error = bus_dma_tag_create (NULL, 16, 0, BUS_SPACE_MAXADDR_24BIT,
505 BUS_SPACE_MAXADDR, NULL, NULL, dmem->size, 1,
506 dmem->size, 0, NULL, NULL, &dmem->dmat);
508 if (cnum >= 0) printf ("ct%d-%d: ", bnum, cnum);
509 else printf ("ct%d: ", bnum);
510 printf ("couldn't allocate tag for dma memory\n");
513 error = bus_dmamem_alloc (dmem->dmat, (void **)&dmem->virt,
514 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &dmem->mapp);
516 if (cnum >= 0) printf ("ct%d-%d: ", bnum, cnum);
517 else printf ("ct%d: ", bnum);
518 printf ("couldn't allocate mem for dma memory\n");
519 bus_dma_tag_destroy (dmem->dmat);
522 error = bus_dmamap_load (dmem->dmat, dmem->mapp, dmem->virt,
523 dmem->size, ct_bus_dmamap_addr, &dmem->phys, 0);
525 if (cnum >= 0) printf ("ct%d-%d: ", bnum, cnum);
526 else printf ("ct%d: ", bnum);
527 printf ("couldn't load mem map for dma memory\n");
528 bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
529 bus_dma_tag_destroy (dmem->dmat);
536 ct_bus_dma_mem_free (ct_dma_mem_t *dmem)
538 bus_dmamap_unload (dmem->dmat, dmem->mapp);
539 bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
540 bus_dma_tag_destroy (dmem->dmat);
544 * The adapter is present, initialize the driver structures.
546 static int ct_attach (device_t dev)
548 bdrv_t *bd = device_get_softc (dev);
549 u_long iobase, drq, irq, rescount;
550 int unit = device_get_unit (dev);
551 char *ct_ln = CT_LOCK_NAME;
558 KASSERT ((bd != NULL), ("ct%d: NULL device softc\n", unit));
560 bus_get_resource (dev, SYS_RES_IOPORT, 0, &iobase, &rescount);
562 bd->base_res = bus_alloc_resource (dev, SYS_RES_IOPORT, &bd->base_rid,
563 iobase, iobase + NPORT, NPORT, RF_ACTIVE);
564 if (! bd->base_res) {
565 printf ("ct%d: cannot alloc base address\n", unit);
569 if (bus_get_resource (dev, SYS_RES_DRQ, 0, &drq, &rescount) != 0) {
570 for (i = 0; (drq = dmatab [i]) != 0; i++) {
571 if (!ct_is_free_res (dev, 0, SYS_RES_DRQ,
574 bus_set_resource (dev, SYS_RES_DRQ, 0, drq, 1);
578 if (dmatab[i] == 0) {
579 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
581 printf ("ct%d: Couldn't get DRQ\n", unit);
587 bd->drq_res = bus_alloc_resource (dev, SYS_RES_DRQ, &bd->drq_rid,
588 drq, drq + 1, 1, RF_ACTIVE);
590 printf ("ct%d: cannot allocate drq\n", unit);
591 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
596 if (bus_get_resource (dev, SYS_RES_IRQ, 0, &irq, &rescount) != 0) {
597 for (i = 0; (irq = irqtab [i]) != 0; i++) {
598 if (!ct_is_free_res (dev, 0, SYS_RES_IRQ,
601 bus_set_resource (dev, SYS_RES_IRQ, 0, irq, 1);
605 if (irqtab[i] == 0) {
606 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
608 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
610 printf ("ct%d: Couldn't get IRQ\n", unit);
616 bd->irq_res = bus_alloc_resource (dev, SYS_RES_IRQ, &bd->irq_rid,
617 irq, irq + 1, 1, RF_ACTIVE);
619 printf ("ct%d: Couldn't allocate irq\n", unit);
620 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
622 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
627 b = malloc (sizeof (ct_board_t), M_DEVBUF, M_WAITOK);
629 printf ("ct:%d: Couldn't allocate memory\n", unit);
633 bzero (b, sizeof(ct_board_t));
635 if (! ct_open_board (b, unit, iobase, irq, drq)) {
636 printf ("ct%d: error loading firmware\n", unit);
638 bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid,
640 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
642 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
649 ct_ln[2] = '0' + unit;
650 mtx_init (&bd->ct_mtx, ct_ln, MTX_NETWORK_LOCK, MTX_DEF|MTX_RECURSE);
651 if (! probe_irq (b, irq)) {
652 printf ("ct%d: irq %ld not functional\n", unit, irq);
656 bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid,
658 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
660 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
662 mtx_destroy (&bd->ct_mtx);
666 callout_init (&led_timo[unit], CALLOUT_MPSAFE);
668 if (bus_setup_intr (dev, bd->irq_res,
669 INTR_TYPE_NET|INTR_MPSAFE,
670 NULL, ct_intr, bd, &bd->intrhand)) {
671 printf ("ct%d: Can't setup irq %ld\n", unit, irq);
675 bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid,
677 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
679 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
681 mtx_destroy (&bd->ct_mtx);
687 ct_init_board (b, b->num, b->port, irq, drq, b->type, b->osc);
688 ct_setup_board (b, 0, 0, 0);
691 printf ("ct%d: <Cronyx-%s>, clock %s MHz\n", b->num, b->name,
692 b->osc == 20000000 ? "20" : "16.384");
694 for (c = b->chan; c < b->chan + NCHAN; ++c) {
695 d = &bd->channel[c->num];
696 d->dmamem.size = sizeof(ct_buf_t);
697 if (! ct_bus_dma_mem_alloc (unit, c->num, &d->dmamem))
703 channel [b->num*NCHAN + c->num] = d;
704 sprintf (d->name, "ct%d.%d", b->num, c->num);
705 callout_init (&d->timeout_handle, CALLOUT_MPSAFE);
708 if (ng_make_node_common (&typestruct, &d->node) != 0) {
709 printf ("%s: cannot make common node\n", d->name);
710 channel [b->num*NCHAN + c->num] = 0;
712 ct_bus_dma_mem_free (&d->dmamem);
715 NG_NODE_SET_PRIVATE (d->node, d);
716 sprintf (d->nodename, "%s%d", NG_CT_NODE_TYPE,
717 c->board->num*NCHAN + c->num);
718 if (ng_name_node (d->node, d->nodename)) {
719 printf ("%s: cannot name node\n", d->nodename);
720 NG_NODE_UNREF (d->node);
721 channel [b->num*NCHAN + c->num] = 0;
723 ct_bus_dma_mem_free (&d->dmamem);
726 d->queue.ifq_maxlen = ifqmaxlen;
727 d->hi_queue.ifq_maxlen = ifqmaxlen;
728 mtx_init (&d->queue.ifq_mtx, "ct_queue", NULL, MTX_DEF);
729 mtx_init (&d->hi_queue.ifq_mtx, "ct_queue_hi", NULL, MTX_DEF);
731 d->ifp = if_alloc(IFT_PPP);
732 if (d->ifp == NULL) {
733 printf ("%s: cannot if_alloc common interface\n",
735 channel [b->num*NCHAN + c->num] = 0;
737 ct_bus_dma_mem_free (&d->dmamem);
740 d->ifp->if_softc = d;
741 if_initname (d->ifp, "ct", b->num * NCHAN + c->num);
742 d->ifp->if_mtu = PP_MTU;
743 d->ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
744 d->ifp->if_ioctl = ct_sioctl;
745 d->ifp->if_start = ct_ifstart;
746 d->ifp->if_init = ct_initialize;
747 d->queue.ifq_maxlen = NBUF;
748 mtx_init (&d->queue.ifq_mtx, "ct_queue", NULL, MTX_DEF);
749 sppp_attach (d->ifp);
751 IFP2SP(d->ifp)->pp_tlf = ct_tlf;
752 IFP2SP(d->ifp)->pp_tls = ct_tls;
753 /* If BPF is in the kernel, call the attach for it.
754 * Header size is 4 bytes. */
755 bpfattach (d->ifp, DLT_PPP, 4);
758 ct_start_chan (c, d->dmamem.virt, d->dmamem.phys);
759 ct_register_receive (c, &ct_receive);
760 ct_register_transmit (c, &ct_transmit);
761 ct_register_error (c, &ct_error);
763 d->devt = make_dev (&ct_cdevsw, b->num*NCHAN+c->num, UID_ROOT,
764 GID_WHEEL, 0600, "ct%d", b->num*NCHAN+c->num);
771 static int ct_detach (device_t dev)
773 bdrv_t *bd = device_get_softc (dev);
774 ct_board_t *b = bd->board;
778 KASSERT (mtx_initialized (&bd->ct_mtx), ("ct mutex not initialized"));
782 /* Check if the device is busy (open). */
783 for (c = b->chan; c < b->chan + NCHAN; ++c) {
784 drv_t *d = (drv_t*) c->sys;
786 if (!d || !d->chan->type)
796 /* Deactivate the timeout routine. */
797 callout_stop (&led_timo[b->num]);
801 bus_teardown_intr (dev, bd->irq_res, bd->intrhand);
802 bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid, bd->irq_res);
804 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid, bd->drq_res);
806 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid, bd->base_res);
812 /* Detach the interfaces, free buffer memory. */
813 for (c = b->chan; c < b->chan + NCHAN; ++c) {
814 drv_t *d = (drv_t*) c->sys;
816 if (!d || !d->chan->type)
819 callout_stop (&d->timeout_handle);
822 ng_rmnode_self (d->node);
823 NG_NODE_UNREF (d->node);
826 mtx_destroy (&d->queue.ifq_mtx);
827 mtx_destroy (&d->hi_queue.ifq_mtx);
829 /* Detach from the packet filter list of interfaces. */
832 /* Detach from the sync PPP list. */
833 sppp_detach (d->ifp);
837 IF_DRAIN (&d->queue);
838 mtx_destroy (&d->queue.ifq_mtx);
840 destroy_dev (d->devt);
846 callout_drain (&led_timo[b->num]);
849 for (c = b->chan; c < b->chan + NCHAN; ++c) {
850 drv_t *d = (drv_t*) c->sys;
852 if (!d || !d->chan->type)
854 callout_drain(&d->timeout_handle);
856 /* Deallocate buffers. */
857 ct_bus_dma_mem_free (&d->dmamem);
860 adapter [b->num] = 0;
863 mtx_destroy (&bd->ct_mtx);
869 static void ct_ifstart (struct ifnet *ifp)
871 drv_t *d = ifp->if_softc;
879 static void ct_tlf (struct sppp *sp)
881 drv_t *d = SP2IFP(sp)->if_softc;
883 CT_DEBUG (d, ("ct_tlf\n"));
884 /* ct_set_dtr (d->chan, 0);*/
885 /* ct_set_rts (d->chan, 0);*/
886 if (!(sp->pp_flags & PP_FR) && !(d->ifp->if_flags & PP_CISCO))
890 static void ct_tls (struct sppp *sp)
892 drv_t *d = SP2IFP(sp)->if_softc;
894 CT_DEBUG (d, ("ct_tls\n"));
895 if (!(sp->pp_flags & PP_FR) && !(d->ifp->if_flags & PP_CISCO))
900 * Initialization of interface.
901 * Ii seems to be never called by upper level.
903 static void ct_initialize (void *softc)
907 CT_DEBUG (d, ("ct_initialize\n"));
911 * Process an ioctl request.
913 static int ct_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data)
915 drv_t *d = ifp->if_softc;
917 int error, s, was_up, should_be_up;
919 was_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
920 error = sppp_ioctl (ifp, cmd, data);
924 if (! (ifp->if_flags & IFF_DEBUG))
927 d->chan->debug = d->chan->debug_shadow;
930 default: CT_DEBUG2 (d, ("ioctl 0x%lx\n", cmd)); return 0;
931 case SIOCADDMULTI: CT_DEBUG2 (d, ("SIOCADDMULTI\n")); return 0;
932 case SIOCDELMULTI: CT_DEBUG2 (d, ("SIOCDELMULTI\n")); return 0;
933 case SIOCSIFFLAGS: CT_DEBUG2 (d, ("SIOCSIFFLAGS\n")); break;
934 case SIOCSIFADDR: CT_DEBUG2 (d, ("SIOCSIFADDR\n")); break;
937 /* We get here only in case of SIFFLAGS or SIFADDR. */
940 should_be_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
941 if (! was_up && should_be_up) {
942 /* Interface goes up -- start it. */
945 } else if (was_up && ! should_be_up) {
946 /* Interface is going down -- stop it. */
947 /* if ((IFP2SP(d->ifp)->pp_flags & PP_FR) || (ifp->if_flags & PP_CISCO))*/
957 * Stop the interface. Called on splimp().
959 static void ct_down (drv_t *d)
962 CT_DEBUG (d, ("ct_down\n"));
963 ct_set_dtr (d->chan, 0);
964 ct_set_rts (d->chan, 0);
966 callout_stop (&d->timeout_handle);
971 * Start the interface. Called on splimp().
973 static void ct_up (drv_t *d)
976 CT_DEBUG (d, ("ct_up\n"));
977 ct_set_dtr (d->chan, 1);
978 ct_set_rts (d->chan, 1);
984 * Start output on the (slave) interface. Get another datagram to send
985 * off of the interface queue, and copy it to the interface
986 * before starting the output.
988 static void ct_send (drv_t *d)
993 CT_DEBUG2 (d, ("ct_send, tn=%d\n", d->chan->tn));
995 /* No output if the interface is down. */
999 /* No output if the modem is off. */
1000 if (! ct_get_dsr (d->chan) && !ct_get_loop (d->chan))
1003 while (ct_buf_free (d->chan)) {
1004 /* Get the packet to send. */
1006 IF_DEQUEUE (&d->hi_queue, m);
1008 IF_DEQUEUE (&d->queue, m);
1010 m = sppp_dequeue (d->ifp);
1015 BPF_MTAP (d->ifp, m);
1017 len = m_length (m, NULL);
1019 ct_send_packet (d->chan, (u_char*)mtod (m, caddr_t),
1022 m_copydata (m, 0, len, d->chan->tbuf[d->chan->te]);
1023 ct_send_packet (d->chan, d->chan->tbuf[d->chan->te],
1028 /* Set up transmit timeout, if the transmit ring is not empty.
1029 * Transmit timeout is 10 seconds. */
1033 d->ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1038 * Start output on the interface.
1039 * Always called on splimp().
1041 static void ct_start (drv_t *d)
1047 ct_set_dtr (d->chan, 1);
1049 ct_set_rts (d->chan, 1);
1051 callout_reset (&d->timeout_handle, hz, ct_watchdog_timer, d);
1058 * Handle transmit timeouts.
1059 * Recover after lost transmit interrupts.
1060 * Always called on splimp().
1062 static void ct_watchdog (drv_t *d)
1065 CT_DEBUG (d, ("device timeout\n"));
1067 ct_setup_chan (d->chan);
1068 ct_start_chan (d->chan, 0, 0);
1069 ct_set_dtr (d->chan, 1);
1070 ct_set_rts (d->chan, 1);
1075 static void ct_watchdog_timer (void *arg)
1081 if (d->timeout == 1)
1085 callout_reset (&d->timeout_handle, hz, ct_watchdog_timer, d);
1090 * Transmit callback function.
1092 static void ct_transmit (ct_chan_t *c, void *attachment, int len)
1100 ++d->ifp->if_opackets;
1101 d->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1107 * Process the received packet.
1109 static void ct_receive (ct_chan_t *c, char *data, int len)
1117 if (!d || !d->running)
1120 m = makembuf (data, len);
1122 CT_DEBUG (d, ("no memory for packet\n"));
1124 ++d->ifp->if_iqdrops;
1131 m->m_pkthdr.rcvif = 0;
1132 NG_SEND_DATA_ONLY (error, d->hook, m);
1134 ++d->ifp->if_ipackets;
1135 m->m_pkthdr.rcvif = d->ifp;
1136 /* Check if there's a BPF listener on this interface.
1137 * If so, hand off the raw packet to bpf. */
1138 BPF_TAP (d->ifp, data, len);
1139 IF_ENQUEUE (&d->queue, m);
1144 * Error callback function.
1146 static void ct_error (ct_chan_t *c, int data)
1155 CT_DEBUG (d, ("frame error\n"));
1157 ++d->ifp->if_ierrors;
1161 CT_DEBUG (d, ("crc error\n"));
1163 ++d->ifp->if_ierrors;
1167 CT_DEBUG (d, ("overrun error\n"));
1169 ++d->ifp->if_collisions;
1170 ++d->ifp->if_ierrors;
1174 CT_DEBUG (d, ("overflow error\n"));
1176 ++d->ifp->if_ierrors;
1180 CT_DEBUG (d, ("underrun error\n"));
1183 ++d->ifp->if_oerrors;
1184 d->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1189 CT_DEBUG (d, ("error #%d\n", data));
1193 static int ct_open (struct cdev *dev, int oflags, int devtype, struct thread *td)
1197 if (dev2unit(dev) >= NCTAU*NCHAN || ! (d = channel[dev2unit(dev)]))
1200 CT_DEBUG2 (d, ("ct_open\n"));
1204 static int ct_close (struct cdev *dev, int fflag, int devtype, struct thread *td)
1206 drv_t *d = channel [dev2unit(dev)];
1211 CT_DEBUG2 (d, ("ct_close\n"));
1215 static int ct_modem_status (ct_chan_t *c)
1226 status = d->running ? TIOCM_LE : 0;
1229 if (ct_get_cd (c)) status |= TIOCM_CD;
1230 if (ct_get_cts (c)) status |= TIOCM_CTS;
1231 if (ct_get_dsr (c)) status |= TIOCM_DSR;
1232 if (c->dtr) status |= TIOCM_DTR;
1233 if (c->rts) status |= TIOCM_RTS;
1240 * Process an ioctl request on /dev/cronyx/ctauN.
1242 static int ct_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
1244 drv_t *d = channel [dev2unit (dev)];
1247 struct serial_statistics *st;
1248 struct e1_statistics *opte1;
1259 case SERIAL_GETREGISTERED:
1260 bzero (mask, sizeof(mask));
1261 for (s=0; s<NCTAU*NCHAN; ++s)
1263 mask [s/8] |= 1 << (s & 7);
1264 bcopy (mask, data, sizeof (mask));
1268 case SERIAL_GETPROTO:
1269 strcpy ((char*)data, (IFP2SP(d->ifp)->pp_flags & PP_FR) ? "fr" :
1270 (d->ifp->if_flags & PP_CISCO) ? "cisco" : "ppp");
1273 case SERIAL_SETPROTO:
1274 /* Only for superuser! */
1275 error = priv_check (td, PRIV_DRIVER);
1278 if (d->ifp->if_drv_flags & IFF_DRV_RUNNING)
1280 if (! strcmp ("cisco", (char*)data)) {
1281 IFP2SP(d->ifp)->pp_flags &= ~(PP_FR);
1282 IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE;
1283 d->ifp->if_flags |= PP_CISCO;
1284 } else if (! strcmp ("fr", (char*)data)) {
1285 d->ifp->if_flags &= ~(PP_CISCO);
1286 IFP2SP(d->ifp)->pp_flags |= PP_FR | PP_KEEPALIVE;
1287 } else if (! strcmp ("ppp", (char*)data)) {
1288 IFP2SP(d->ifp)->pp_flags &= ~(PP_FR | PP_KEEPALIVE);
1289 d->ifp->if_flags &= ~(PP_CISCO);
1294 case SERIAL_GETKEEPALIVE:
1295 if ((IFP2SP(d->ifp)->pp_flags & PP_FR) ||
1296 (d->ifp->if_flags & PP_CISCO))
1298 *(int*)data = (IFP2SP(d->ifp)->pp_flags & PP_KEEPALIVE) ? 1 : 0;
1301 case SERIAL_SETKEEPALIVE:
1302 /* Only for superuser! */
1303 error = priv_check (td, PRIV_DRIVER);
1306 if ((IFP2SP(d->ifp)->pp_flags & PP_FR) ||
1307 (d->ifp->if_flags & PP_CISCO))
1310 IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE;
1312 IFP2SP(d->ifp)->pp_flags &= ~PP_KEEPALIVE;
1316 case SERIAL_GETMODE:
1317 *(int*)data = SERIAL_HDLC;
1321 if (c->mode == M_HDLC)
1323 switch (ct_get_config (c->board)) {
1324 default: *(char*)data = 'a'; break;
1325 case CFG_B: *(char*)data = 'b'; break;
1326 case CFG_C: *(char*)data = 'c'; break;
1331 /* Only for superuser! */
1332 error = priv_check (td, PRIV_DRIVER);
1335 if (c->mode == M_HDLC)
1339 switch (*(char*)data) {
1340 case 'a': ct_set_config (c->board, CFG_A); break;
1341 case 'b': ct_set_config (c->board, CFG_B); break;
1342 case 'c': ct_set_config (c->board, CFG_C); break;
1348 case SERIAL_GETSTAT:
1349 st = (struct serial_statistics*) data;
1350 st->rintr = c->rintr;
1351 st->tintr = c->tintr;
1352 st->mintr = c->mintr;
1353 st->ibytes = c->ibytes;
1354 st->ipkts = c->ipkts;
1355 st->ierrs = c->ierrs;
1356 st->obytes = c->obytes;
1357 st->opkts = c->opkts;
1358 st->oerrs = c->oerrs;
1361 case SERIAL_GETESTAT:
1362 opte1 = (struct e1_statistics*)data;
1363 opte1->status = c->status;
1364 opte1->cursec = c->cursec;
1365 opte1->totsec = c->totsec + c->cursec;
1367 opte1->currnt.bpv = c->currnt.bpv;
1368 opte1->currnt.fse = c->currnt.fse;
1369 opte1->currnt.crce = c->currnt.crce;
1370 opte1->currnt.rcrce = c->currnt.rcrce;
1371 opte1->currnt.uas = c->currnt.uas;
1372 opte1->currnt.les = c->currnt.les;
1373 opte1->currnt.es = c->currnt.es;
1374 opte1->currnt.bes = c->currnt.bes;
1375 opte1->currnt.ses = c->currnt.ses;
1376 opte1->currnt.oofs = c->currnt.oofs;
1377 opte1->currnt.css = c->currnt.css;
1378 opte1->currnt.dm = c->currnt.dm;
1380 opte1->total.bpv = c->total.bpv + c->currnt.bpv;
1381 opte1->total.fse = c->total.fse + c->currnt.fse;
1382 opte1->total.crce = c->total.crce + c->currnt.crce;
1383 opte1->total.rcrce = c->total.rcrce + c->currnt.rcrce;
1384 opte1->total.uas = c->total.uas + c->currnt.uas;
1385 opte1->total.les = c->total.les + c->currnt.les;
1386 opte1->total.es = c->total.es + c->currnt.es;
1387 opte1->total.bes = c->total.bes + c->currnt.bes;
1388 opte1->total.ses = c->total.ses + c->currnt.ses;
1389 opte1->total.oofs = c->total.oofs + c->currnt.oofs;
1390 opte1->total.css = c->total.css + c->currnt.css;
1391 opte1->total.dm = c->total.dm + c->currnt.dm;
1392 for (s=0; s<48; ++s) {
1393 opte1->interval[s].bpv = c->interval[s].bpv;
1394 opte1->interval[s].fse = c->interval[s].fse;
1395 opte1->interval[s].crce = c->interval[s].crce;
1396 opte1->interval[s].rcrce = c->interval[s].rcrce;
1397 opte1->interval[s].uas = c->interval[s].uas;
1398 opte1->interval[s].les = c->interval[s].les;
1399 opte1->interval[s].es = c->interval[s].es;
1400 opte1->interval[s].bes = c->interval[s].bes;
1401 opte1->interval[s].ses = c->interval[s].ses;
1402 opte1->interval[s].oofs = c->interval[s].oofs;
1403 opte1->interval[s].css = c->interval[s].css;
1404 opte1->interval[s].dm = c->interval[s].dm;
1408 case SERIAL_CLRSTAT:
1409 /* Only for superuser! */
1410 error = priv_check (td, PRIV_DRIVER);
1422 bzero (&c->currnt, sizeof (c->currnt));
1423 bzero (&c->total, sizeof (c->total));
1424 bzero (c->interval, sizeof (c->interval));
1427 case SERIAL_GETBAUD:
1428 *(long*)data = ct_get_baud(c);
1431 case SERIAL_SETBAUD:
1432 /* Only for superuser! */
1433 error = priv_check (td, PRIV_DRIVER);
1438 ct_set_baud (c, *(long*)data);
1443 case SERIAL_GETLOOP:
1444 *(int*)data = ct_get_loop (c);
1447 case SERIAL_SETLOOP:
1448 /* Only for superuser! */
1449 error = priv_check (td, PRIV_DRIVER);
1454 ct_set_loop (c, *(int*)data);
1459 case SERIAL_GETDPLL:
1460 if (c->mode == M_E1 || c->mode == M_G703)
1462 *(int*)data = ct_get_dpll (c);
1465 case SERIAL_SETDPLL:
1466 /* Only for superuser! */
1467 error = priv_check (td, PRIV_DRIVER);
1470 if (c->mode == M_E1 || c->mode == M_G703)
1474 ct_set_dpll (c, *(int*)data);
1479 case SERIAL_GETNRZI:
1480 if (c->mode == M_E1 || c->mode == M_G703)
1482 *(int*)data = ct_get_nrzi (c);
1485 case SERIAL_SETNRZI:
1486 /* Only for superuser! */
1487 error = priv_check (td, PRIV_DRIVER);
1490 if (c->mode == M_E1 || c->mode == M_G703)
1494 ct_set_nrzi (c, *(int*)data);
1499 case SERIAL_GETDEBUG:
1500 *(int*)data = c->debug;
1503 case SERIAL_SETDEBUG:
1504 /* Only for superuser! */
1505 error = priv_check (td, PRIV_DRIVER);
1510 * The debug_shadow is always greater than zero for logic
1511 * simplicity. For switching debug off the IFF_DEBUG is
1514 c->debug_shadow = (*(int*)data) ? (*(int*)data) : 1;
1515 if (d->ifp->if_flags & IFF_DEBUG)
1516 c->debug = c->debug_shadow;
1518 c->debug = *(int*)data;
1522 case SERIAL_GETHIGAIN:
1523 if (c->mode != M_E1)
1525 *(int*)data = ct_get_higain (c);
1528 case SERIAL_SETHIGAIN:
1529 /* Only for superuser! */
1530 error = priv_check (td, PRIV_DRIVER);
1535 ct_set_higain (c, *(int*)data);
1540 case SERIAL_GETPHONY:
1541 CT_DEBUG2 (d, ("ioctl: getphony\n"));
1542 if (c->mode != M_E1)
1544 *(int*)data = c->gopt.phony;
1547 case SERIAL_SETPHONY:
1548 CT_DEBUG2 (d, ("ioctl: setphony\n"));
1549 if (c->mode != M_E1)
1551 /* Only for superuser! */
1552 error = priv_check (td, PRIV_DRIVER);
1557 ct_set_phony (c, *(int*)data);
1563 if (c->mode != M_E1 && c->mode != M_G703)
1565 switch (ct_get_clk(c)) {
1566 default: *(int*)data = E1CLK_INTERNAL; break;
1567 case GCLK_RCV: *(int*)data = E1CLK_RECEIVE; break;
1568 case GCLK_RCLKO: *(int*)data = c->num ?
1569 E1CLK_RECEIVE_CHAN0 : E1CLK_RECEIVE_CHAN1; break;
1574 /* Only for superuser! */
1575 error = priv_check (td, PRIV_DRIVER);
1580 switch (*(int*)data) {
1581 default: ct_set_clk (c, GCLK_INT); break;
1582 case E1CLK_RECEIVE: ct_set_clk (c, GCLK_RCV); break;
1583 case E1CLK_RECEIVE_CHAN0:
1584 case E1CLK_RECEIVE_CHAN1:
1585 ct_set_clk (c, GCLK_RCLKO); break;
1591 case SERIAL_GETTIMESLOTS:
1592 if (c->mode != M_E1)
1594 *(long*)data = ct_get_ts (c);
1597 case SERIAL_SETTIMESLOTS:
1598 /* Only for superuser! */
1599 error = priv_check (td, PRIV_DRIVER);
1604 ct_set_ts (c, *(long*)data);
1609 case SERIAL_GETSUBCHAN:
1610 if (c->mode != M_E1)
1612 *(long*)data = ct_get_subchan (c->board);
1615 case SERIAL_SETSUBCHAN:
1616 /* Only for superuser! */
1617 error = priv_check (td, PRIV_DRIVER);
1622 ct_set_subchan (c->board, *(long*)data);
1627 case SERIAL_GETINVCLK:
1628 case SERIAL_GETINVTCLK:
1629 if (c->mode == M_E1 || c->mode == M_G703)
1631 *(int*)data = ct_get_invtxc (c);
1634 case SERIAL_GETINVRCLK:
1635 if (c->mode == M_E1 || c->mode == M_G703)
1637 *(int*)data = ct_get_invrxc (c);
1640 case SERIAL_SETINVCLK:
1641 case SERIAL_SETINVTCLK:
1642 /* Only for superuser! */
1643 error = priv_check (td, PRIV_DRIVER);
1646 if (c->mode == M_E1 || c->mode == M_G703)
1650 ct_set_invtxc (c, *(int*)data);
1655 case SERIAL_SETINVRCLK:
1656 /* Only for superuser! */
1657 error = priv_check (td, PRIV_DRIVER);
1660 if (c->mode == M_E1 || c->mode == M_G703)
1664 ct_set_invrxc (c, *(int*)data);
1669 case SERIAL_GETLEVEL:
1670 if (c->mode != M_G703)
1674 *(int*)data = ct_get_lq (c);
1679 case TIOCSDTR: /* Set DTR */
1687 case TIOCCDTR: /* Clear DTR */
1695 case TIOCMSET: /* Set DTR/RTS */
1698 ct_set_dtr (c, (*(int*)data & TIOCM_DTR) ? 1 : 0);
1699 ct_set_rts (c, (*(int*)data & TIOCM_RTS) ? 1 : 0);
1704 case TIOCMBIS: /* Add DTR/RTS */
1707 if (*(int*)data & TIOCM_DTR) ct_set_dtr (c, 1);
1708 if (*(int*)data & TIOCM_RTS) ct_set_rts (c, 1);
1713 case TIOCMBIC: /* Clear DTR/RTS */
1716 if (*(int*)data & TIOCM_DTR) ct_set_dtr (c, 0);
1717 if (*(int*)data & TIOCM_RTS) ct_set_rts (c, 0);
1722 case TIOCMGET: /* Get modem status */
1723 *(int*)data = ct_modem_status (c);
1730 static int ng_ct_constructor (node_p node)
1732 drv_t *d = NG_NODE_PRIVATE (node);
1733 CT_DEBUG (d, ("Constructor\n"));
1737 static int ng_ct_newhook (node_p node, hook_p hook, const char *name)
1740 drv_t *d = NG_NODE_PRIVATE (node);
1747 /* Attach debug hook */
1748 if (strcmp (name, NG_CT_HOOK_DEBUG) == 0) {
1749 NG_HOOK_SET_PRIVATE (hook, NULL);
1750 d->debug_hook = hook;
1754 /* Check for raw hook */
1755 if (strcmp (name, NG_CT_HOOK_RAW) != 0)
1758 NG_HOOK_SET_PRIVATE (hook, d);
1768 static char *format_timeslots (u_long s)
1770 static char buf [100];
1774 for (i=1; i<32; ++i)
1776 int prev = (i > 1) & (s >> (i-1));
1777 int next = (i < 31) & (s >> (i+1));
1787 *p++ = '0' + i / 10;
1788 *p++ = '0' + i % 10;
1794 static int print_modems (char *s, ct_chan_t *c, int need_header)
1796 int status = ct_modem_status (c);
1800 length += sprintf (s + length, " LE DTR DSR RTS CTS CD\n");
1801 length += sprintf (s + length, "%4s %4s %4s %4s %4s %4s\n",
1802 status & TIOCM_LE ? "On" : "-",
1803 status & TIOCM_DTR ? "On" : "-",
1804 status & TIOCM_DSR ? "On" : "-",
1805 status & TIOCM_RTS ? "On" : "-",
1806 status & TIOCM_CTS ? "On" : "-",
1807 status & TIOCM_CD ? "On" : "-");
1811 static int print_stats (char *s, ct_chan_t *c, int need_header)
1813 struct serial_statistics st;
1816 st.rintr = c->rintr;
1817 st.tintr = c->tintr;
1818 st.mintr = c->mintr;
1819 st.ibytes = c->ibytes;
1820 st.ipkts = c->ipkts;
1821 st.ierrs = c->ierrs;
1822 st.obytes = c->obytes;
1823 st.opkts = c->opkts;
1824 st.oerrs = c->oerrs;
1826 length += sprintf (s + length, " Rintr Tintr Mintr Ibytes Ipkts Ierrs Obytes Opkts Oerrs\n");
1827 length += sprintf (s + length, "%7ld %7ld %7ld %8ld %7ld %7ld %8ld %7ld %7ld\n",
1828 st.rintr, st.tintr, st.mintr, st.ibytes, st.ipkts,
1829 st.ierrs, st.obytes, st.opkts, st.oerrs);
1833 static char *format_e1_status (u_char status)
1835 static char buf [80];
1837 if (status & E1_NOALARM)
1840 if (status & E1_LOS) strcat (buf, ",LOS");
1841 if (status & E1_AIS) strcat (buf, ",AIS");
1842 if (status & E1_LOF) strcat (buf, ",LOF");
1843 if (status & E1_LOMF) strcat (buf, ",LOMF");
1844 if (status & E1_FARLOF) strcat (buf, ",FARLOF");
1845 if (status & E1_AIS16) strcat (buf, ",AIS16");
1846 if (status & E1_FARLOMF) strcat (buf, ",FARLOMF");
1847 if (status & E1_TSTREQ) strcat (buf, ",TSTREQ");
1848 if (status & E1_TSTERR) strcat (buf, ",TSTERR");
1854 static int print_frac (char *s, int leftalign, u_long numerator, u_long divider)
1858 if (numerator < 1 || divider < 1) {
1859 length += sprintf (s+length, leftalign ? "/- " : " -");
1862 n = (int) (0.5 + 1000.0 * numerator / divider);
1864 length += sprintf (s+length, leftalign ? "/.%-3d" : " .%03d", n);
1867 *(s + length) = leftalign ? '/' : ' ';
1870 if (n >= 1000000) n = (n+500) / 1000 * 1000;
1871 else if (n >= 100000) n = (n+50) / 100 * 100;
1872 else if (n >= 10000) n = (n+5) / 10 * 10;
1875 case 1000: length += printf (s+length, ".999"); return length;
1876 case 10000: n = 9990; break;
1877 case 100000: n = 99900; break;
1878 case 1000000: n = 999000; break;
1880 if (n < 10000) length += sprintf (s+length, "%d.%d", n/1000, n/10%100);
1881 else if (n < 100000) length += sprintf (s+length, "%d.%d", n/1000, n/100%10);
1882 else if (n < 1000000) length += sprintf (s+length, "%d.", n/1000);
1883 else length += sprintf (s+length, "%d", n/1000);
1888 static int print_e1_stats (char *s, ct_chan_t *c)
1890 struct e1_counters total;
1894 totsec = c->totsec + c->cursec;
1895 total.bpv = c->total.bpv + c->currnt.bpv;
1896 total.fse = c->total.fse + c->currnt.fse;
1897 total.crce = c->total.crce + c->currnt.crce;
1898 total.rcrce = c->total.rcrce + c->currnt.rcrce;
1899 total.uas = c->total.uas + c->currnt.uas;
1900 total.les = c->total.les + c->currnt.les;
1901 total.es = c->total.es + c->currnt.es;
1902 total.bes = c->total.bes + c->currnt.bes;
1903 total.ses = c->total.ses + c->currnt.ses;
1904 total.oofs = c->total.oofs + c->currnt.oofs;
1905 total.css = c->total.css + c->currnt.css;
1906 total.dm = c->total.dm + c->currnt.dm;
1908 length += sprintf (s + length, " Unav/Degr Bpv/Fsyn CRC/RCRC Err/Lerr Sev/Bur Oof/Slp Status\n");
1910 /* Unavailable seconds, degraded minutes */
1911 length += print_frac (s + length, 0, c->currnt.uas, c->cursec);
1912 length += print_frac (s + length, 1, 60 * c->currnt.dm, c->cursec);
1914 /* Bipolar violations, frame sync errors */
1915 length += print_frac (s + length, 0, c->currnt.bpv, c->cursec);
1916 length += print_frac (s + length, 1, c->currnt.fse, c->cursec);
1918 /* CRC errors, remote CRC errors (E-bit) */
1919 length += print_frac (s + length, 0, c->currnt.crce, c->cursec);
1920 length += print_frac (s + length, 1, c->currnt.rcrce, c->cursec);
1922 /* Errored seconds, line errored seconds */
1923 length += print_frac (s + length, 0, c->currnt.es, c->cursec);
1924 length += print_frac (s + length, 1, c->currnt.les, c->cursec);
1926 /* Severely errored seconds, burst errored seconds */
1927 length += print_frac (s + length, 0, c->currnt.ses, c->cursec);
1928 length += print_frac (s + length, 1, c->currnt.bes, c->cursec);
1930 /* Out of frame seconds, controlled slip seconds */
1931 length += print_frac (s + length, 0, c->currnt.oofs, c->cursec);
1932 length += print_frac (s + length, 1, c->currnt.css, c->cursec);
1934 length += sprintf (s + length, " %s\n", format_e1_status (c->status));
1936 /* Print total statistics. */
1937 length += print_frac (s + length, 0, total.uas, totsec);
1938 length += print_frac (s + length, 1, 60 * total.dm, totsec);
1940 length += print_frac (s + length, 0, total.bpv, totsec);
1941 length += print_frac (s + length, 1, total.fse, totsec);
1943 length += print_frac (s + length, 0, total.crce, totsec);
1944 length += print_frac (s + length, 1, total.rcrce, totsec);
1946 length += print_frac (s + length, 0, total.es, totsec);
1947 length += print_frac (s + length, 1, total.les, totsec);
1949 length += print_frac (s + length, 0, total.ses, totsec);
1950 length += print_frac (s + length, 1, total.bes, totsec);
1952 length += print_frac (s + length, 0, total.oofs, totsec);
1953 length += print_frac (s + length, 1, total.css, totsec);
1955 length += sprintf (s + length, " -- Total\n");
1959 static int print_chan (char *s, ct_chan_t *c)
1965 length += sprintf (s + length, "ct%d", c->board->num * NCHAN + c->num);
1967 length += sprintf (s + length, " debug=%d", d->chan->debug);
1969 switch (ct_get_config (c->board)) {
1970 case CFG_A: length += sprintf (s + length, " cfg=A"); break;
1971 case CFG_B: length += sprintf (s + length, " cfg=B"); break;
1972 case CFG_C: length += sprintf (s + length, " cfg=C"); break;
1973 default: length += sprintf (s + length, " cfg=unknown"); break;
1976 if (ct_get_baud (c))
1977 length += sprintf (s + length, " %ld", ct_get_baud (c));
1979 length += sprintf (s + length, " extclock");
1981 if (c->mode == M_E1 || c->mode == M_G703)
1982 switch (ct_get_clk(c)) {
1983 case GCLK_INT : length += sprintf (s + length, " syn=int"); break;
1984 case GCLK_RCV : length += sprintf (s + length, " syn=rcv"); break;
1985 case GCLK_RCLKO : length += sprintf (s + length, " syn=xrcv"); break;
1987 if (c->mode == M_HDLC) {
1988 length += sprintf (s + length, " dpll=%s", ct_get_dpll (c) ? "on" : "off");
1989 length += sprintf (s + length, " nrzi=%s", ct_get_nrzi (c) ? "on" : "off");
1990 length += sprintf (s + length, " invtclk=%s", ct_get_invtxc (c) ? "on" : "off");
1991 length += sprintf (s + length, " invrclk=%s", ct_get_invrxc (c) ? "on" : "off");
1993 if (c->mode == M_E1)
1994 length += sprintf (s + length, " higain=%s", ct_get_higain (c)? "on" : "off");
1996 length += sprintf (s + length, " loop=%s", ct_get_loop (c) ? "on" : "off");
1998 if (c->mode == M_E1)
1999 length += sprintf (s + length, " ts=%s", format_timeslots (ct_get_ts(c)));
2000 if (c->mode == M_E1 && ct_get_config (c->board) != CFG_A)
2001 length += sprintf (s + length, " pass=%s", format_timeslots (ct_get_subchan(c->board)));
2002 if (c->mode == M_G703) {
2010 length += sprintf (s + length, " (level=-%.1fdB)", lq / 10.0);
2012 length += sprintf (s + length, "\n");
2016 static int ng_ct_rcvmsg (node_p node, item_p item, hook_p lasthook)
2018 drv_t *d = NG_NODE_PRIVATE (node);
2019 struct ng_mesg *msg;
2020 struct ng_mesg *resp = NULL;
2026 CT_DEBUG (d, ("Rcvmsg\n"));
2027 NGI_GET_MSG (item, msg);
2028 switch (msg->header.typecookie) {
2034 printf ("Don't forget to implement\n");
2038 case NGM_GENERIC_COOKIE:
2039 switch (msg->header.cmd) {
2044 case NGM_TEXT_STATUS: {
2047 int dl = sizeof (struct ng_mesg) + 730;
2049 NG_MKRESPONSE (resp, msg, dl, M_NOWAIT);
2055 l += print_chan (s + l, d->chan);
2056 l += print_stats (s + l, d->chan, 1);
2057 l += print_modems (s + l, d->chan, 1);
2058 l += print_e1_stats (s + l, d->chan);
2059 strncpy ((resp)->header.cmdstr, "status", NG_CMDSTRSIZ);
2065 NG_RESPOND_MSG (error, node, item, resp);
2070 static int ng_ct_rcvdata (hook_p hook, item_p item)
2072 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE(hook));
2074 struct ng_tag_prio *ptag;
2083 NGI_GET_M (item, m);
2084 NG_FREE_ITEM (item);
2085 if (! NG_HOOK_PRIVATE (hook) || ! d) {
2090 /* Check for high priority data */
2091 if ((ptag = (struct ng_tag_prio *)m_tag_locate(m, NGM_GENERIC_COOKIE,
2092 NG_TAG_PRIO, NULL)) != NULL && (ptag->priority > NG_PRIO_CUTOFF) )
2100 if (_IF_QFULL (q)) {
2116 static int ng_ct_rmnode (node_p node)
2118 drv_t *d = NG_NODE_PRIVATE (node);
2121 CT_DEBUG (d, ("Rmnode\n"));
2122 if (d && d->running) {
2131 if (node->nd_flags & NGF_REALLY_DIE) {
2132 NG_NODE_SET_PRIVATE (node, NULL);
2133 NG_NODE_UNREF (node);
2135 NG_NODE_REVIVE(node); /* Persistant node */
2140 static int ng_ct_connect (hook_p hook)
2142 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2147 callout_reset (&d->timeout_handle, hz, ct_watchdog_timer, d);
2151 static int ng_ct_disconnect (hook_p hook)
2153 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2162 if (NG_HOOK_PRIVATE (hook))
2165 /* If we were wait it than it reasserted now, just stop it. */
2166 if (!callout_drain (&d->timeout_handle))
2167 callout_stop (&d->timeout_handle);
2172 static int ct_modevent (module_t mod, int type, void *unused)
2174 static int load_count = 0;
2179 if (ng_newtype (&typestruct))
2180 printf ("Failed to register ng_ct\n");
2183 callout_init (&timeout_handle, CALLOUT_MPSAFE);
2184 callout_reset (&timeout_handle, hz*5, ct_timeout, 0);
2187 if (load_count == 1) {
2188 printf ("Removing device entry for Tau-ISA\n");
2190 ng_rmtype (&typestruct);
2193 /* If we were wait it than it reasserted now, just stop it. */
2194 if (!callout_drain (&timeout_handle))
2195 callout_stop (&timeout_handle);
2205 static struct ng_type typestruct = {
2206 .version = NG_ABI_VERSION,
2207 .name = NG_CT_NODE_TYPE,
2208 .constructor = ng_ct_constructor,
2209 .rcvmsg = ng_ct_rcvmsg,
2210 .shutdown = ng_ct_rmnode,
2211 .newhook = ng_ct_newhook,
2212 .connect = ng_ct_connect,
2213 .rcvdata = ng_ct_rcvdata,
2214 .disconnect = ng_ct_disconnect,
2219 MODULE_DEPEND (ng_ct, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION);
2221 MODULE_DEPEND (ct, sppp, 1, 1, 1);
2223 DRIVER_MODULE (ct, isa, ct_isa_driver, ct_devclass, ct_modevent, NULL);
2224 MODULE_VERSION (ct, 1);