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 #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),
99 typedef struct _ct_dma_mem_t {
107 typedef struct _drv_t {
115 char nodename [NG_NODESIZ];
119 struct ifqueue queue;
120 struct ifqueue hi_queue;
122 struct ifqueue queue;
126 struct callout timeout_handle;
130 typedef struct _bdrv_t {
132 struct resource *base_res;
133 struct resource *drq_res;
134 struct resource *irq_res;
139 drv_t channel [NCHAN];
143 static driver_t ct_isa_driver = {
149 static devclass_t ct_devclass;
151 static void ct_receive (ct_chan_t *c, char *data, int len);
152 static void ct_transmit (ct_chan_t *c, void *attachment, int len);
153 static void ct_error (ct_chan_t *c, int data);
154 static void ct_up (drv_t *d);
155 static void ct_start (drv_t *d);
156 static void ct_down (drv_t *d);
157 static void ct_watchdog (drv_t *d);
158 static void ct_watchdog_timer (void *arg);
160 extern struct ng_type typestruct;
162 static void ct_ifstart (struct ifnet *ifp);
163 static void ct_tlf (struct sppp *sp);
164 static void ct_tls (struct sppp *sp);
165 static int ct_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data);
166 static void ct_initialize (void *softc);
169 static ct_board_t *adapter [NCTAU];
170 static drv_t *channel [NCTAU*NCHAN];
171 static struct callout led_timo [NCTAU];
172 static struct callout timeout_handle;
174 static int ct_open (struct cdev *dev, int oflags, int devtype, struct thread *td);
175 static int ct_close (struct cdev *dev, int fflag, int devtype, struct thread *td);
176 static int ct_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td);
177 static struct cdevsw ct_cdevsw = {
178 .d_version = D_VERSION,
186 * Print the mbuf chain, for debug purposes only.
188 static void printmbuf (struct mbuf *m)
191 for (; m; m=m->m_next) {
192 if (m->m_flags & M_PKTHDR)
193 printf (" HDR %d:", m->m_pkthdr.len);
194 if (m->m_flags & M_EXT)
196 printf (" %d", m->m_len);
202 * Make an mbuf from data.
204 static struct mbuf *makembuf (void *buf, u_int len)
208 MGETHDR (m, M_NOWAIT, MT_DATA);
211 MCLGET (m, M_NOWAIT);
212 if (! (m->m_flags & M_EXT)) {
216 m->m_pkthdr.len = m->m_len = len;
217 bcopy (buf, mtod (m, caddr_t), len);
221 static void ct_timeout (void *arg)
226 for (i = 0; i < NCTAU; ++i) {
227 if (adapter[i] == NULL)
229 for (k = 0; k < NCHAN; k++) {
230 d = channel[i * NCHAN + k];
233 if (d->chan->mode != M_G703)
236 CT_LOCK ((bdrv_t *)d->bd);
237 ct_g703_timer (d->chan);
238 CT_UNLOCK ((bdrv_t *)d->bd);
243 callout_reset (&timeout_handle, hz, ct_timeout, 0);
246 static void ct_led_off (void *arg)
249 bdrv_t *bd = ((drv_t *)b->chan->sys)->bd;
259 * Activate interrupt handler from DDK.
261 static void ct_intr (void *arg)
264 ct_board_t *b = bd->board;
276 /* Turn LED off 50 msec later. */
277 callout_reset (&led_timo[b->num], hz/20, ct_led_off, b);
282 /* Pass packets in a lock-free state */
283 for (i = 0; i < NCHAN && b->chan[i].type; i++) {
284 drv_t *d = b->chan[i].sys;
286 if (!d || !d->running)
288 while (_IF_QLEN(&d->queue)) {
289 IF_DEQUEUE (&d->queue,m);
292 sppp_input (d->ifp, m);
298 static int probe_irq (ct_board_t *b, int irq)
302 /* Clear pending irq, if any. */
303 ct_probe_irq (b, -irq);
305 for (cnt=0; cnt<5; ++cnt) {
306 /* Get the mask of pending irqs, assuming they are busy.
307 * Activate the adapter on given irq. */
308 busy = ct_probe_irq (b, irq);
311 /* Get the mask of active irqs.
312 * Deactivate our irq. */
313 mask = ct_probe_irq (b, -irq);
315 if ((mask & ~busy) == 1 << irq) {
317 /* printf ("ct%d: irq %d ok, mask=0x%04x, busy=0x%04x\n",
318 b->num, irq, mask, busy); */
322 /* printf ("ct%d: irq %d not functional, mask=0x%04x, busy=0x%04x\n",
323 b->num, irq, mask, busy); */
328 static short porttab [] = {
329 0x200, 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0,
330 0x300, 0x320, 0x340, 0x360, 0x380, 0x3a0, 0x3c0, 0x3e0, 0
332 static char dmatab [] = { 7, 6, 5, 0 };
333 static char irqtab [] = { 5, 10, 11, 7, 3, 15, 12, 0 };
335 static int ct_is_free_res (device_t dev, int rid, int type, u_long start,
336 u_long end, u_long count)
338 struct resource *res;
340 if (!(res = bus_alloc_resource (dev, type, &rid, start, end, count,
344 bus_release_resource (dev, type, rid, res);
349 static void ct_identify (driver_t *driver, device_t dev)
351 u_long iobase, rescount;
355 devclass_t my_devclass;
358 if ((my_devclass = devclass_find ("ct")) == NULL)
361 devclass_get_devices (my_devclass, &devices, &devcount);
364 /* We should find all devices by our self. We could alter other
365 * devices, but we don't have a choise
367 for (i = 0; (iobase = porttab [i]) != 0; i++) {
368 if (!ct_is_free_res (dev, 0, SYS_RES_IOPORT,
369 iobase, iobase + NPORT, NPORT))
371 if (ct_probe_board (iobase, -1, -1) == 0)
375 child = BUS_ADD_CHILD (dev, ISA_ORDER_SPECULATIVE, "ct",
381 device_set_desc_copy (child, "Cronyx Tau-ISA");
382 device_set_driver (child, driver);
383 bus_set_resource (child, SYS_RES_IOPORT, 0,
386 if (devcount >= NCTAU)
390 static short porttab [] = {
391 0x200, 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0,
392 0x300, 0x320, 0x340, 0x360, 0x380, 0x3a0, 0x3c0, 0x3e0, 0
394 /* Lets check user choise.
396 for (k = 0; k < devcount; k++) {
397 if (bus_get_resource (devices[k], SYS_RES_IOPORT, 0,
398 &iobase, &rescount) != 0)
401 for (i = 0; porttab [i] != 0; i++) {
402 if (porttab [i] != iobase)
405 if (!ct_is_free_res (devices[k], 0, SYS_RES_IOPORT,
406 iobase, iobase + NPORT, NPORT))
409 if (ct_probe_board (iobase, -1, -1) == 0)
412 device_set_desc_copy (devices[k], "Cronyx Tau-ISA");
415 if (porttab [i] == 0) {
416 device_delete_child (
417 device_get_parent (devices[k]),
423 for (k = 0; k < devcount; k++) {
426 if (bus_get_resource (devices[k], SYS_RES_IOPORT, 0,
427 &iobase, &rescount) == 0)
429 for (i = 0; (iobase = porttab [i]) != 0; i++) {
430 if (porttab [i] == -1)
432 if (!ct_is_free_res (devices[k], 0, SYS_RES_IOPORT,
433 iobase, iobase + NPORT, NPORT))
435 if (ct_probe_board (iobase, -1, -1) == 0)
438 bus_set_resource (devices[k], SYS_RES_IOPORT, 0,
441 device_set_desc_copy (devices[k], "Cronyx Tau-ISA");
444 if (porttab [i] == 0) {
445 device_delete_child (
446 device_get_parent (devices[k]),
450 free (devices, M_TEMP);
456 static int ct_probe (device_t dev)
458 int unit = device_get_unit (dev);
459 u_long iobase, rescount;
461 if (!device_get_desc (dev) ||
462 strcmp (device_get_desc (dev), "Cronyx Tau-ISA"))
465 /* KASSERT ((bd != NULL), ("ct%d: NULL device softc\n", unit));*/
466 if (bus_get_resource (dev, SYS_RES_IOPORT, 0, &iobase, &rescount) != 0) {
467 printf ("ct%d: Couldn't get IOPORT\n", unit);
471 if (!ct_is_free_res (dev, 0, SYS_RES_IOPORT,
472 iobase, iobase + NPORT, NPORT)) {
473 printf ("ct%d: Resource IOPORT isn't free\n", unit);
477 if (!ct_probe_board (iobase, -1, -1)) {
478 printf ("ct%d: probing for Tau-ISA at %lx faild\n", unit, iobase);
486 ct_bus_dmamap_addr (void *arg, bus_dma_segment_t *segs, int nseg, int error)
493 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
495 *addr = segs->ds_addr;
499 ct_bus_dma_mem_alloc (int bnum, int cnum, ct_dma_mem_t *dmem)
503 error = bus_dma_tag_create (NULL, 16, 0, BUS_SPACE_MAXADDR_24BIT,
504 BUS_SPACE_MAXADDR, NULL, NULL, dmem->size, 1,
505 dmem->size, 0, NULL, NULL, &dmem->dmat);
507 if (cnum >= 0) printf ("ct%d-%d: ", bnum, cnum);
508 else printf ("ct%d: ", bnum);
509 printf ("couldn't allocate tag for dma memory\n");
512 error = bus_dmamem_alloc (dmem->dmat, (void **)&dmem->virt,
513 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &dmem->mapp);
515 if (cnum >= 0) printf ("ct%d-%d: ", bnum, cnum);
516 else printf ("ct%d: ", bnum);
517 printf ("couldn't allocate mem for dma memory\n");
518 bus_dma_tag_destroy (dmem->dmat);
521 error = bus_dmamap_load (dmem->dmat, dmem->mapp, dmem->virt,
522 dmem->size, ct_bus_dmamap_addr, &dmem->phys, 0);
524 if (cnum >= 0) printf ("ct%d-%d: ", bnum, cnum);
525 else printf ("ct%d: ", bnum);
526 printf ("couldn't load mem map for dma memory\n");
527 bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
528 bus_dma_tag_destroy (dmem->dmat);
535 ct_bus_dma_mem_free (ct_dma_mem_t *dmem)
537 bus_dmamap_unload (dmem->dmat, dmem->mapp);
538 bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
539 bus_dma_tag_destroy (dmem->dmat);
543 * The adapter is present, initialize the driver structures.
545 static int ct_attach (device_t dev)
547 bdrv_t *bd = device_get_softc (dev);
548 u_long iobase, drq, irq, rescount;
549 int unit = device_get_unit (dev);
550 char *ct_ln = CT_LOCK_NAME;
557 KASSERT ((bd != NULL), ("ct%d: NULL device softc\n", unit));
559 bus_get_resource (dev, SYS_RES_IOPORT, 0, &iobase, &rescount);
561 bd->base_res = bus_alloc_resource (dev, SYS_RES_IOPORT, &bd->base_rid,
562 iobase, iobase + NPORT, NPORT, RF_ACTIVE);
563 if (! bd->base_res) {
564 printf ("ct%d: cannot alloc base address\n", unit);
568 if (bus_get_resource (dev, SYS_RES_DRQ, 0, &drq, &rescount) != 0) {
569 for (i = 0; (drq = dmatab [i]) != 0; i++) {
570 if (!ct_is_free_res (dev, 0, SYS_RES_DRQ,
573 bus_set_resource (dev, SYS_RES_DRQ, 0, drq, 1);
577 if (dmatab[i] == 0) {
578 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
580 printf ("ct%d: Couldn't get DRQ\n", unit);
586 bd->drq_res = bus_alloc_resource (dev, SYS_RES_DRQ, &bd->drq_rid,
587 drq, drq + 1, 1, RF_ACTIVE);
589 printf ("ct%d: cannot allocate drq\n", unit);
590 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
595 if (bus_get_resource (dev, SYS_RES_IRQ, 0, &irq, &rescount) != 0) {
596 for (i = 0; (irq = irqtab [i]) != 0; i++) {
597 if (!ct_is_free_res (dev, 0, SYS_RES_IRQ,
600 bus_set_resource (dev, SYS_RES_IRQ, 0, irq, 1);
604 if (irqtab[i] == 0) {
605 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
607 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
609 printf ("ct%d: Couldn't get IRQ\n", unit);
615 bd->irq_res = bus_alloc_resource (dev, SYS_RES_IRQ, &bd->irq_rid,
616 irq, irq + 1, 1, RF_ACTIVE);
618 printf ("ct%d: Couldn't allocate irq\n", unit);
619 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
621 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
626 b = malloc (sizeof (ct_board_t), M_DEVBUF, M_WAITOK);
628 printf ("ct:%d: Couldn't allocate memory\n", unit);
632 bzero (b, sizeof(ct_board_t));
634 if (! ct_open_board (b, unit, iobase, irq, drq)) {
635 printf ("ct%d: error loading firmware\n", unit);
637 bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid,
639 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
641 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
648 ct_ln[2] = '0' + unit;
649 mtx_init (&bd->ct_mtx, ct_ln, MTX_NETWORK_LOCK, MTX_DEF|MTX_RECURSE);
650 if (! probe_irq (b, irq)) {
651 printf ("ct%d: irq %ld not functional\n", unit, irq);
655 bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid,
657 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
659 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
661 mtx_destroy (&bd->ct_mtx);
665 callout_init (&led_timo[unit], CALLOUT_MPSAFE);
667 if (bus_setup_intr (dev, bd->irq_res,
668 INTR_TYPE_NET|INTR_MPSAFE,
669 NULL, ct_intr, bd, &bd->intrhand)) {
670 printf ("ct%d: Can't setup irq %ld\n", unit, irq);
674 bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid,
676 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
678 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
680 mtx_destroy (&bd->ct_mtx);
686 ct_init_board (b, b->num, b->port, irq, drq, b->type, b->osc);
687 ct_setup_board (b, 0, 0, 0);
690 printf ("ct%d: <Cronyx-%s>, clock %s MHz\n", b->num, b->name,
691 b->osc == 20000000 ? "20" : "16.384");
693 for (c = b->chan; c < b->chan + NCHAN; ++c) {
694 d = &bd->channel[c->num];
695 d->dmamem.size = sizeof(ct_buf_t);
696 if (! ct_bus_dma_mem_alloc (unit, c->num, &d->dmamem))
702 channel [b->num*NCHAN + c->num] = d;
703 sprintf (d->name, "ct%d.%d", b->num, c->num);
704 callout_init (&d->timeout_handle, CALLOUT_MPSAFE);
707 if (ng_make_node_common (&typestruct, &d->node) != 0) {
708 printf ("%s: cannot make common node\n", d->name);
709 channel [b->num*NCHAN + c->num] = 0;
711 ct_bus_dma_mem_free (&d->dmamem);
714 NG_NODE_SET_PRIVATE (d->node, d);
715 sprintf (d->nodename, "%s%d", NG_CT_NODE_TYPE,
716 c->board->num*NCHAN + c->num);
717 if (ng_name_node (d->node, d->nodename)) {
718 printf ("%s: cannot name node\n", d->nodename);
719 NG_NODE_UNREF (d->node);
720 channel [b->num*NCHAN + c->num] = 0;
722 ct_bus_dma_mem_free (&d->dmamem);
725 d->queue.ifq_maxlen = ifqmaxlen;
726 d->hi_queue.ifq_maxlen = ifqmaxlen;
727 mtx_init (&d->queue.ifq_mtx, "ct_queue", NULL, MTX_DEF);
728 mtx_init (&d->hi_queue.ifq_mtx, "ct_queue_hi", NULL, MTX_DEF);
730 d->ifp = if_alloc(IFT_PPP);
731 if (d->ifp == NULL) {
732 printf ("%s: cannot if_alloc common interface\n",
734 channel [b->num*NCHAN + c->num] = 0;
736 ct_bus_dma_mem_free (&d->dmamem);
739 d->ifp->if_softc = d;
740 if_initname (d->ifp, "ct", b->num * NCHAN + c->num);
741 d->ifp->if_mtu = PP_MTU;
742 d->ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
743 d->ifp->if_ioctl = ct_sioctl;
744 d->ifp->if_start = ct_ifstart;
745 d->ifp->if_init = ct_initialize;
746 d->queue.ifq_maxlen = NBUF;
747 mtx_init (&d->queue.ifq_mtx, "ct_queue", NULL, MTX_DEF);
748 sppp_attach (d->ifp);
750 IFP2SP(d->ifp)->pp_tlf = ct_tlf;
751 IFP2SP(d->ifp)->pp_tls = ct_tls;
752 /* If BPF is in the kernel, call the attach for it.
753 * Header size is 4 bytes. */
754 bpfattach (d->ifp, DLT_PPP, 4);
757 ct_start_chan (c, d->dmamem.virt, d->dmamem.phys);
758 ct_register_receive (c, &ct_receive);
759 ct_register_transmit (c, &ct_transmit);
760 ct_register_error (c, &ct_error);
762 d->devt = make_dev (&ct_cdevsw, b->num*NCHAN+c->num, UID_ROOT,
763 GID_WHEEL, 0600, "ct%d", b->num*NCHAN+c->num);
770 static int ct_detach (device_t dev)
772 bdrv_t *bd = device_get_softc (dev);
773 ct_board_t *b = bd->board;
777 KASSERT (mtx_initialized (&bd->ct_mtx), ("ct mutex not initialized"));
781 /* Check if the device is busy (open). */
782 for (c = b->chan; c < b->chan + NCHAN; ++c) {
783 drv_t *d = (drv_t*) c->sys;
785 if (!d || !d->chan->type)
795 /* Deactivate the timeout routine. */
796 callout_stop (&led_timo[b->num]);
800 bus_teardown_intr (dev, bd->irq_res, bd->intrhand);
801 bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid, bd->irq_res);
803 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid, bd->drq_res);
805 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid, bd->base_res);
811 /* Detach the interfaces, free buffer memory. */
812 for (c = b->chan; c < b->chan + NCHAN; ++c) {
813 drv_t *d = (drv_t*) c->sys;
815 if (!d || !d->chan->type)
818 callout_stop (&d->timeout_handle);
821 ng_rmnode_self (d->node);
822 NG_NODE_UNREF (d->node);
825 mtx_destroy (&d->queue.ifq_mtx);
826 mtx_destroy (&d->hi_queue.ifq_mtx);
828 /* Detach from the packet filter list of interfaces. */
831 /* Detach from the sync PPP list. */
832 sppp_detach (d->ifp);
836 IF_DRAIN (&d->queue);
837 mtx_destroy (&d->queue.ifq_mtx);
839 destroy_dev (d->devt);
845 callout_drain (&led_timo[b->num]);
848 for (c = b->chan; c < b->chan + NCHAN; ++c) {
849 drv_t *d = (drv_t*) c->sys;
851 if (!d || !d->chan->type)
853 callout_drain(&d->timeout_handle);
855 /* Deallocate buffers. */
856 ct_bus_dma_mem_free (&d->dmamem);
859 adapter [b->num] = 0;
862 mtx_destroy (&bd->ct_mtx);
868 static void ct_ifstart (struct ifnet *ifp)
870 drv_t *d = ifp->if_softc;
878 static void ct_tlf (struct sppp *sp)
880 drv_t *d = SP2IFP(sp)->if_softc;
882 CT_DEBUG (d, ("ct_tlf\n"));
883 /* ct_set_dtr (d->chan, 0);*/
884 /* ct_set_rts (d->chan, 0);*/
885 if (!(sp->pp_flags & PP_FR) && !(d->ifp->if_flags & PP_CISCO))
889 static void ct_tls (struct sppp *sp)
891 drv_t *d = SP2IFP(sp)->if_softc;
893 CT_DEBUG (d, ("ct_tls\n"));
894 if (!(sp->pp_flags & PP_FR) && !(d->ifp->if_flags & PP_CISCO))
899 * Initialization of interface.
900 * Ii seems to be never called by upper level.
902 static void ct_initialize (void *softc)
906 CT_DEBUG (d, ("ct_initialize\n"));
910 * Process an ioctl request.
912 static int ct_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data)
914 drv_t *d = ifp->if_softc;
916 int error, s, was_up, should_be_up;
918 was_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
919 error = sppp_ioctl (ifp, cmd, data);
923 if (! (ifp->if_flags & IFF_DEBUG))
926 d->chan->debug = d->chan->debug_shadow;
929 default: CT_DEBUG2 (d, ("ioctl 0x%lx\n", cmd)); return 0;
930 case SIOCADDMULTI: CT_DEBUG2 (d, ("SIOCADDMULTI\n")); return 0;
931 case SIOCDELMULTI: CT_DEBUG2 (d, ("SIOCDELMULTI\n")); return 0;
932 case SIOCSIFFLAGS: CT_DEBUG2 (d, ("SIOCSIFFLAGS\n")); break;
933 case SIOCSIFADDR: CT_DEBUG2 (d, ("SIOCSIFADDR\n")); break;
936 /* We get here only in case of SIFFLAGS or SIFADDR. */
939 should_be_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
940 if (! was_up && should_be_up) {
941 /* Interface goes up -- start it. */
944 } else if (was_up && ! should_be_up) {
945 /* Interface is going down -- stop it. */
946 /* if ((IFP2SP(d->ifp)->pp_flags & PP_FR) || (ifp->if_flags & PP_CISCO))*/
956 * Stop the interface. Called on splimp().
958 static void ct_down (drv_t *d)
961 CT_DEBUG (d, ("ct_down\n"));
962 ct_set_dtr (d->chan, 0);
963 ct_set_rts (d->chan, 0);
965 callout_stop (&d->timeout_handle);
970 * Start the interface. Called on splimp().
972 static void ct_up (drv_t *d)
975 CT_DEBUG (d, ("ct_up\n"));
976 ct_set_dtr (d->chan, 1);
977 ct_set_rts (d->chan, 1);
983 * Start output on the (slave) interface. Get another datagram to send
984 * off of the interface queue, and copy it to the interface
985 * before starting the output.
987 static void ct_send (drv_t *d)
992 CT_DEBUG2 (d, ("ct_send, tn=%d\n", d->chan->tn));
994 /* No output if the interface is down. */
998 /* No output if the modem is off. */
999 if (! ct_get_dsr (d->chan) && !ct_get_loop (d->chan))
1002 while (ct_buf_free (d->chan)) {
1003 /* Get the packet to send. */
1005 IF_DEQUEUE (&d->hi_queue, m);
1007 IF_DEQUEUE (&d->queue, m);
1009 m = sppp_dequeue (d->ifp);
1014 BPF_MTAP (d->ifp, m);
1016 len = m_length (m, NULL);
1018 ct_send_packet (d->chan, (u_char*)mtod (m, caddr_t),
1021 m_copydata (m, 0, len, d->chan->tbuf[d->chan->te]);
1022 ct_send_packet (d->chan, d->chan->tbuf[d->chan->te],
1027 /* Set up transmit timeout, if the transmit ring is not empty.
1028 * Transmit timeout is 10 seconds. */
1032 d->ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1037 * Start output on the interface.
1038 * Always called on splimp().
1040 static void ct_start (drv_t *d)
1046 ct_set_dtr (d->chan, 1);
1048 ct_set_rts (d->chan, 1);
1050 callout_reset (&d->timeout_handle, hz, ct_watchdog_timer, d);
1057 * Handle transmit timeouts.
1058 * Recover after lost transmit interrupts.
1059 * Always called on splimp().
1061 static void ct_watchdog (drv_t *d)
1064 CT_DEBUG (d, ("device timeout\n"));
1066 ct_setup_chan (d->chan);
1067 ct_start_chan (d->chan, 0, 0);
1068 ct_set_dtr (d->chan, 1);
1069 ct_set_rts (d->chan, 1);
1074 static void ct_watchdog_timer (void *arg)
1080 if (d->timeout == 1)
1084 callout_reset (&d->timeout_handle, hz, ct_watchdog_timer, d);
1089 * Transmit callback function.
1091 static void ct_transmit (ct_chan_t *c, void *attachment, int len)
1099 ++d->ifp->if_opackets;
1100 d->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1106 * Process the received packet.
1108 static void ct_receive (ct_chan_t *c, char *data, int len)
1116 if (!d || !d->running)
1119 m = makembuf (data, len);
1121 CT_DEBUG (d, ("no memory for packet\n"));
1123 ++d->ifp->if_iqdrops;
1130 m->m_pkthdr.rcvif = 0;
1131 NG_SEND_DATA_ONLY (error, d->hook, m);
1133 ++d->ifp->if_ipackets;
1134 m->m_pkthdr.rcvif = d->ifp;
1135 /* Check if there's a BPF listener on this interface.
1136 * If so, hand off the raw packet to bpf. */
1137 BPF_TAP (d->ifp, data, len);
1138 IF_ENQUEUE (&d->queue, m);
1143 * Error callback function.
1145 static void ct_error (ct_chan_t *c, int data)
1154 CT_DEBUG (d, ("frame error\n"));
1156 ++d->ifp->if_ierrors;
1160 CT_DEBUG (d, ("crc error\n"));
1162 ++d->ifp->if_ierrors;
1166 CT_DEBUG (d, ("overrun error\n"));
1168 ++d->ifp->if_collisions;
1169 ++d->ifp->if_ierrors;
1173 CT_DEBUG (d, ("overflow error\n"));
1175 ++d->ifp->if_ierrors;
1179 CT_DEBUG (d, ("underrun error\n"));
1182 ++d->ifp->if_oerrors;
1183 d->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1188 CT_DEBUG (d, ("error #%d\n", data));
1192 static int ct_open (struct cdev *dev, int oflags, int devtype, struct thread *td)
1196 if (dev2unit(dev) >= NCTAU*NCHAN || ! (d = channel[dev2unit(dev)]))
1199 CT_DEBUG2 (d, ("ct_open\n"));
1203 static int ct_close (struct cdev *dev, int fflag, int devtype, struct thread *td)
1205 drv_t *d = channel [dev2unit(dev)];
1210 CT_DEBUG2 (d, ("ct_close\n"));
1214 static int ct_modem_status (ct_chan_t *c)
1225 status = d->running ? TIOCM_LE : 0;
1228 if (ct_get_cd (c)) status |= TIOCM_CD;
1229 if (ct_get_cts (c)) status |= TIOCM_CTS;
1230 if (ct_get_dsr (c)) status |= TIOCM_DSR;
1231 if (c->dtr) status |= TIOCM_DTR;
1232 if (c->rts) status |= TIOCM_RTS;
1239 * Process an ioctl request on /dev/cronyx/ctauN.
1241 static int ct_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
1243 drv_t *d = channel [dev2unit (dev)];
1246 struct serial_statistics *st;
1247 struct e1_statistics *opte1;
1258 case SERIAL_GETREGISTERED:
1259 bzero (mask, sizeof(mask));
1260 for (s=0; s<NCTAU*NCHAN; ++s)
1262 mask [s/8] |= 1 << (s & 7);
1263 bcopy (mask, data, sizeof (mask));
1267 case SERIAL_GETPROTO:
1268 strcpy ((char*)data, (IFP2SP(d->ifp)->pp_flags & PP_FR) ? "fr" :
1269 (d->ifp->if_flags & PP_CISCO) ? "cisco" : "ppp");
1272 case SERIAL_SETPROTO:
1273 /* Only for superuser! */
1274 error = priv_check (td, PRIV_DRIVER);
1277 if (d->ifp->if_drv_flags & IFF_DRV_RUNNING)
1279 if (! strcmp ("cisco", (char*)data)) {
1280 IFP2SP(d->ifp)->pp_flags &= ~(PP_FR);
1281 IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE;
1282 d->ifp->if_flags |= PP_CISCO;
1283 } else if (! strcmp ("fr", (char*)data)) {
1284 d->ifp->if_flags &= ~(PP_CISCO);
1285 IFP2SP(d->ifp)->pp_flags |= PP_FR | PP_KEEPALIVE;
1286 } else if (! strcmp ("ppp", (char*)data)) {
1287 IFP2SP(d->ifp)->pp_flags &= ~(PP_FR | PP_KEEPALIVE);
1288 d->ifp->if_flags &= ~(PP_CISCO);
1293 case SERIAL_GETKEEPALIVE:
1294 if ((IFP2SP(d->ifp)->pp_flags & PP_FR) ||
1295 (d->ifp->if_flags & PP_CISCO))
1297 *(int*)data = (IFP2SP(d->ifp)->pp_flags & PP_KEEPALIVE) ? 1 : 0;
1300 case SERIAL_SETKEEPALIVE:
1301 /* Only for superuser! */
1302 error = priv_check (td, PRIV_DRIVER);
1305 if ((IFP2SP(d->ifp)->pp_flags & PP_FR) ||
1306 (d->ifp->if_flags & PP_CISCO))
1309 IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE;
1311 IFP2SP(d->ifp)->pp_flags &= ~PP_KEEPALIVE;
1315 case SERIAL_GETMODE:
1316 *(int*)data = SERIAL_HDLC;
1320 if (c->mode == M_HDLC)
1322 switch (ct_get_config (c->board)) {
1323 default: *(char*)data = 'a'; break;
1324 case CFG_B: *(char*)data = 'b'; break;
1325 case CFG_C: *(char*)data = 'c'; break;
1330 /* Only for superuser! */
1331 error = priv_check (td, PRIV_DRIVER);
1334 if (c->mode == M_HDLC)
1338 switch (*(char*)data) {
1339 case 'a': ct_set_config (c->board, CFG_A); break;
1340 case 'b': ct_set_config (c->board, CFG_B); break;
1341 case 'c': ct_set_config (c->board, CFG_C); break;
1347 case SERIAL_GETSTAT:
1348 st = (struct serial_statistics*) data;
1349 st->rintr = c->rintr;
1350 st->tintr = c->tintr;
1351 st->mintr = c->mintr;
1352 st->ibytes = c->ibytes;
1353 st->ipkts = c->ipkts;
1354 st->ierrs = c->ierrs;
1355 st->obytes = c->obytes;
1356 st->opkts = c->opkts;
1357 st->oerrs = c->oerrs;
1360 case SERIAL_GETESTAT:
1361 opte1 = (struct e1_statistics*)data;
1362 opte1->status = c->status;
1363 opte1->cursec = c->cursec;
1364 opte1->totsec = c->totsec + c->cursec;
1366 opte1->currnt.bpv = c->currnt.bpv;
1367 opte1->currnt.fse = c->currnt.fse;
1368 opte1->currnt.crce = c->currnt.crce;
1369 opte1->currnt.rcrce = c->currnt.rcrce;
1370 opte1->currnt.uas = c->currnt.uas;
1371 opte1->currnt.les = c->currnt.les;
1372 opte1->currnt.es = c->currnt.es;
1373 opte1->currnt.bes = c->currnt.bes;
1374 opte1->currnt.ses = c->currnt.ses;
1375 opte1->currnt.oofs = c->currnt.oofs;
1376 opte1->currnt.css = c->currnt.css;
1377 opte1->currnt.dm = c->currnt.dm;
1379 opte1->total.bpv = c->total.bpv + c->currnt.bpv;
1380 opte1->total.fse = c->total.fse + c->currnt.fse;
1381 opte1->total.crce = c->total.crce + c->currnt.crce;
1382 opte1->total.rcrce = c->total.rcrce + c->currnt.rcrce;
1383 opte1->total.uas = c->total.uas + c->currnt.uas;
1384 opte1->total.les = c->total.les + c->currnt.les;
1385 opte1->total.es = c->total.es + c->currnt.es;
1386 opte1->total.bes = c->total.bes + c->currnt.bes;
1387 opte1->total.ses = c->total.ses + c->currnt.ses;
1388 opte1->total.oofs = c->total.oofs + c->currnt.oofs;
1389 opte1->total.css = c->total.css + c->currnt.css;
1390 opte1->total.dm = c->total.dm + c->currnt.dm;
1391 for (s=0; s<48; ++s) {
1392 opte1->interval[s].bpv = c->interval[s].bpv;
1393 opte1->interval[s].fse = c->interval[s].fse;
1394 opte1->interval[s].crce = c->interval[s].crce;
1395 opte1->interval[s].rcrce = c->interval[s].rcrce;
1396 opte1->interval[s].uas = c->interval[s].uas;
1397 opte1->interval[s].les = c->interval[s].les;
1398 opte1->interval[s].es = c->interval[s].es;
1399 opte1->interval[s].bes = c->interval[s].bes;
1400 opte1->interval[s].ses = c->interval[s].ses;
1401 opte1->interval[s].oofs = c->interval[s].oofs;
1402 opte1->interval[s].css = c->interval[s].css;
1403 opte1->interval[s].dm = c->interval[s].dm;
1407 case SERIAL_CLRSTAT:
1408 /* Only for superuser! */
1409 error = priv_check (td, PRIV_DRIVER);
1421 bzero (&c->currnt, sizeof (c->currnt));
1422 bzero (&c->total, sizeof (c->total));
1423 bzero (c->interval, sizeof (c->interval));
1426 case SERIAL_GETBAUD:
1427 *(long*)data = ct_get_baud(c);
1430 case SERIAL_SETBAUD:
1431 /* Only for superuser! */
1432 error = priv_check (td, PRIV_DRIVER);
1437 ct_set_baud (c, *(long*)data);
1442 case SERIAL_GETLOOP:
1443 *(int*)data = ct_get_loop (c);
1446 case SERIAL_SETLOOP:
1447 /* Only for superuser! */
1448 error = priv_check (td, PRIV_DRIVER);
1453 ct_set_loop (c, *(int*)data);
1458 case SERIAL_GETDPLL:
1459 if (c->mode == M_E1 || c->mode == M_G703)
1461 *(int*)data = ct_get_dpll (c);
1464 case SERIAL_SETDPLL:
1465 /* Only for superuser! */
1466 error = priv_check (td, PRIV_DRIVER);
1469 if (c->mode == M_E1 || c->mode == M_G703)
1473 ct_set_dpll (c, *(int*)data);
1478 case SERIAL_GETNRZI:
1479 if (c->mode == M_E1 || c->mode == M_G703)
1481 *(int*)data = ct_get_nrzi (c);
1484 case SERIAL_SETNRZI:
1485 /* Only for superuser! */
1486 error = priv_check (td, PRIV_DRIVER);
1489 if (c->mode == M_E1 || c->mode == M_G703)
1493 ct_set_nrzi (c, *(int*)data);
1498 case SERIAL_GETDEBUG:
1499 *(int*)data = c->debug;
1502 case SERIAL_SETDEBUG:
1503 /* Only for superuser! */
1504 error = priv_check (td, PRIV_DRIVER);
1509 * The debug_shadow is always greater than zero for logic
1510 * simplicity. For switching debug off the IFF_DEBUG is
1513 c->debug_shadow = (*(int*)data) ? (*(int*)data) : 1;
1514 if (d->ifp->if_flags & IFF_DEBUG)
1515 c->debug = c->debug_shadow;
1517 c->debug = *(int*)data;
1521 case SERIAL_GETHIGAIN:
1522 if (c->mode != M_E1)
1524 *(int*)data = ct_get_higain (c);
1527 case SERIAL_SETHIGAIN:
1528 /* Only for superuser! */
1529 error = priv_check (td, PRIV_DRIVER);
1534 ct_set_higain (c, *(int*)data);
1539 case SERIAL_GETPHONY:
1540 CT_DEBUG2 (d, ("ioctl: getphony\n"));
1541 if (c->mode != M_E1)
1543 *(int*)data = c->gopt.phony;
1546 case SERIAL_SETPHONY:
1547 CT_DEBUG2 (d, ("ioctl: setphony\n"));
1548 if (c->mode != M_E1)
1550 /* Only for superuser! */
1551 error = priv_check (td, PRIV_DRIVER);
1556 ct_set_phony (c, *(int*)data);
1562 if (c->mode != M_E1 && c->mode != M_G703)
1564 switch (ct_get_clk(c)) {
1565 default: *(int*)data = E1CLK_INTERNAL; break;
1566 case GCLK_RCV: *(int*)data = E1CLK_RECEIVE; break;
1567 case GCLK_RCLKO: *(int*)data = c->num ?
1568 E1CLK_RECEIVE_CHAN0 : E1CLK_RECEIVE_CHAN1; break;
1573 /* Only for superuser! */
1574 error = priv_check (td, PRIV_DRIVER);
1579 switch (*(int*)data) {
1580 default: ct_set_clk (c, GCLK_INT); break;
1581 case E1CLK_RECEIVE: ct_set_clk (c, GCLK_RCV); break;
1582 case E1CLK_RECEIVE_CHAN0:
1583 case E1CLK_RECEIVE_CHAN1:
1584 ct_set_clk (c, GCLK_RCLKO); break;
1590 case SERIAL_GETTIMESLOTS:
1591 if (c->mode != M_E1)
1593 *(long*)data = ct_get_ts (c);
1596 case SERIAL_SETTIMESLOTS:
1597 /* Only for superuser! */
1598 error = priv_check (td, PRIV_DRIVER);
1603 ct_set_ts (c, *(long*)data);
1608 case SERIAL_GETSUBCHAN:
1609 if (c->mode != M_E1)
1611 *(long*)data = ct_get_subchan (c->board);
1614 case SERIAL_SETSUBCHAN:
1615 /* Only for superuser! */
1616 error = priv_check (td, PRIV_DRIVER);
1621 ct_set_subchan (c->board, *(long*)data);
1626 case SERIAL_GETINVCLK:
1627 case SERIAL_GETINVTCLK:
1628 if (c->mode == M_E1 || c->mode == M_G703)
1630 *(int*)data = ct_get_invtxc (c);
1633 case SERIAL_GETINVRCLK:
1634 if (c->mode == M_E1 || c->mode == M_G703)
1636 *(int*)data = ct_get_invrxc (c);
1639 case SERIAL_SETINVCLK:
1640 case SERIAL_SETINVTCLK:
1641 /* Only for superuser! */
1642 error = priv_check (td, PRIV_DRIVER);
1645 if (c->mode == M_E1 || c->mode == M_G703)
1649 ct_set_invtxc (c, *(int*)data);
1654 case SERIAL_SETINVRCLK:
1655 /* Only for superuser! */
1656 error = priv_check (td, PRIV_DRIVER);
1659 if (c->mode == M_E1 || c->mode == M_G703)
1663 ct_set_invrxc (c, *(int*)data);
1668 case SERIAL_GETLEVEL:
1669 if (c->mode != M_G703)
1673 *(int*)data = ct_get_lq (c);
1678 case TIOCSDTR: /* Set DTR */
1686 case TIOCCDTR: /* Clear DTR */
1694 case TIOCMSET: /* Set DTR/RTS */
1697 ct_set_dtr (c, (*(int*)data & TIOCM_DTR) ? 1 : 0);
1698 ct_set_rts (c, (*(int*)data & TIOCM_RTS) ? 1 : 0);
1703 case TIOCMBIS: /* Add DTR/RTS */
1706 if (*(int*)data & TIOCM_DTR) ct_set_dtr (c, 1);
1707 if (*(int*)data & TIOCM_RTS) ct_set_rts (c, 1);
1712 case TIOCMBIC: /* Clear DTR/RTS */
1715 if (*(int*)data & TIOCM_DTR) ct_set_dtr (c, 0);
1716 if (*(int*)data & TIOCM_RTS) ct_set_rts (c, 0);
1721 case TIOCMGET: /* Get modem status */
1722 *(int*)data = ct_modem_status (c);
1729 static int ng_ct_constructor (node_p node)
1731 drv_t *d = NG_NODE_PRIVATE (node);
1732 CT_DEBUG (d, ("Constructor\n"));
1736 static int ng_ct_newhook (node_p node, hook_p hook, const char *name)
1739 drv_t *d = NG_NODE_PRIVATE (node);
1746 /* Attach debug hook */
1747 if (strcmp (name, NG_CT_HOOK_DEBUG) == 0) {
1748 NG_HOOK_SET_PRIVATE (hook, NULL);
1749 d->debug_hook = hook;
1753 /* Check for raw hook */
1754 if (strcmp (name, NG_CT_HOOK_RAW) != 0)
1757 NG_HOOK_SET_PRIVATE (hook, d);
1767 static char *format_timeslots (u_long s)
1769 static char buf [100];
1773 for (i=1; i<32; ++i)
1775 int prev = (i > 1) & (s >> (i-1));
1776 int next = (i < 31) & (s >> (i+1));
1786 *p++ = '0' + i / 10;
1787 *p++ = '0' + i % 10;
1793 static int print_modems (char *s, ct_chan_t *c, int need_header)
1795 int status = ct_modem_status (c);
1799 length += sprintf (s + length, " LE DTR DSR RTS CTS CD\n");
1800 length += sprintf (s + length, "%4s %4s %4s %4s %4s %4s\n",
1801 status & TIOCM_LE ? "On" : "-",
1802 status & TIOCM_DTR ? "On" : "-",
1803 status & TIOCM_DSR ? "On" : "-",
1804 status & TIOCM_RTS ? "On" : "-",
1805 status & TIOCM_CTS ? "On" : "-",
1806 status & TIOCM_CD ? "On" : "-");
1810 static int print_stats (char *s, ct_chan_t *c, int need_header)
1812 struct serial_statistics st;
1815 st.rintr = c->rintr;
1816 st.tintr = c->tintr;
1817 st.mintr = c->mintr;
1818 st.ibytes = c->ibytes;
1819 st.ipkts = c->ipkts;
1820 st.ierrs = c->ierrs;
1821 st.obytes = c->obytes;
1822 st.opkts = c->opkts;
1823 st.oerrs = c->oerrs;
1825 length += sprintf (s + length, " Rintr Tintr Mintr Ibytes Ipkts Ierrs Obytes Opkts Oerrs\n");
1826 length += sprintf (s + length, "%7ld %7ld %7ld %8ld %7ld %7ld %8ld %7ld %7ld\n",
1827 st.rintr, st.tintr, st.mintr, st.ibytes, st.ipkts,
1828 st.ierrs, st.obytes, st.opkts, st.oerrs);
1832 static char *format_e1_status (u_char status)
1834 static char buf [80];
1836 if (status & E1_NOALARM)
1839 if (status & E1_LOS) strcat (buf, ",LOS");
1840 if (status & E1_AIS) strcat (buf, ",AIS");
1841 if (status & E1_LOF) strcat (buf, ",LOF");
1842 if (status & E1_LOMF) strcat (buf, ",LOMF");
1843 if (status & E1_FARLOF) strcat (buf, ",FARLOF");
1844 if (status & E1_AIS16) strcat (buf, ",AIS16");
1845 if (status & E1_FARLOMF) strcat (buf, ",FARLOMF");
1846 if (status & E1_TSTREQ) strcat (buf, ",TSTREQ");
1847 if (status & E1_TSTERR) strcat (buf, ",TSTERR");
1853 static int print_frac (char *s, int leftalign, u_long numerator, u_long divider)
1857 if (numerator < 1 || divider < 1) {
1858 length += sprintf (s+length, leftalign ? "/- " : " -");
1861 n = (int) (0.5 + 1000.0 * numerator / divider);
1863 length += sprintf (s+length, leftalign ? "/.%-3d" : " .%03d", n);
1866 *(s + length) = leftalign ? '/' : ' ';
1869 if (n >= 1000000) n = (n+500) / 1000 * 1000;
1870 else if (n >= 100000) n = (n+50) / 100 * 100;
1871 else if (n >= 10000) n = (n+5) / 10 * 10;
1874 case 1000: length += printf (s+length, ".999"); return length;
1875 case 10000: n = 9990; break;
1876 case 100000: n = 99900; break;
1877 case 1000000: n = 999000; break;
1879 if (n < 10000) length += sprintf (s+length, "%d.%d", n/1000, n/10%100);
1880 else if (n < 100000) length += sprintf (s+length, "%d.%d", n/1000, n/100%10);
1881 else if (n < 1000000) length += sprintf (s+length, "%d.", n/1000);
1882 else length += sprintf (s+length, "%d", n/1000);
1887 static int print_e1_stats (char *s, ct_chan_t *c)
1889 struct e1_counters total;
1893 totsec = c->totsec + c->cursec;
1894 total.bpv = c->total.bpv + c->currnt.bpv;
1895 total.fse = c->total.fse + c->currnt.fse;
1896 total.crce = c->total.crce + c->currnt.crce;
1897 total.rcrce = c->total.rcrce + c->currnt.rcrce;
1898 total.uas = c->total.uas + c->currnt.uas;
1899 total.les = c->total.les + c->currnt.les;
1900 total.es = c->total.es + c->currnt.es;
1901 total.bes = c->total.bes + c->currnt.bes;
1902 total.ses = c->total.ses + c->currnt.ses;
1903 total.oofs = c->total.oofs + c->currnt.oofs;
1904 total.css = c->total.css + c->currnt.css;
1905 total.dm = c->total.dm + c->currnt.dm;
1907 length += sprintf (s + length, " Unav/Degr Bpv/Fsyn CRC/RCRC Err/Lerr Sev/Bur Oof/Slp Status\n");
1909 /* Unavailable seconds, degraded minutes */
1910 length += print_frac (s + length, 0, c->currnt.uas, c->cursec);
1911 length += print_frac (s + length, 1, 60 * c->currnt.dm, c->cursec);
1913 /* Bipolar violations, frame sync errors */
1914 length += print_frac (s + length, 0, c->currnt.bpv, c->cursec);
1915 length += print_frac (s + length, 1, c->currnt.fse, c->cursec);
1917 /* CRC errors, remote CRC errors (E-bit) */
1918 length += print_frac (s + length, 0, c->currnt.crce, c->cursec);
1919 length += print_frac (s + length, 1, c->currnt.rcrce, c->cursec);
1921 /* Errored seconds, line errored seconds */
1922 length += print_frac (s + length, 0, c->currnt.es, c->cursec);
1923 length += print_frac (s + length, 1, c->currnt.les, c->cursec);
1925 /* Severely errored seconds, burst errored seconds */
1926 length += print_frac (s + length, 0, c->currnt.ses, c->cursec);
1927 length += print_frac (s + length, 1, c->currnt.bes, c->cursec);
1929 /* Out of frame seconds, controlled slip seconds */
1930 length += print_frac (s + length, 0, c->currnt.oofs, c->cursec);
1931 length += print_frac (s + length, 1, c->currnt.css, c->cursec);
1933 length += sprintf (s + length, " %s\n", format_e1_status (c->status));
1935 /* Print total statistics. */
1936 length += print_frac (s + length, 0, total.uas, totsec);
1937 length += print_frac (s + length, 1, 60 * total.dm, totsec);
1939 length += print_frac (s + length, 0, total.bpv, totsec);
1940 length += print_frac (s + length, 1, total.fse, totsec);
1942 length += print_frac (s + length, 0, total.crce, totsec);
1943 length += print_frac (s + length, 1, total.rcrce, totsec);
1945 length += print_frac (s + length, 0, total.es, totsec);
1946 length += print_frac (s + length, 1, total.les, totsec);
1948 length += print_frac (s + length, 0, total.ses, totsec);
1949 length += print_frac (s + length, 1, total.bes, totsec);
1951 length += print_frac (s + length, 0, total.oofs, totsec);
1952 length += print_frac (s + length, 1, total.css, totsec);
1954 length += sprintf (s + length, " -- Total\n");
1958 static int print_chan (char *s, ct_chan_t *c)
1964 length += sprintf (s + length, "ct%d", c->board->num * NCHAN + c->num);
1966 length += sprintf (s + length, " debug=%d", d->chan->debug);
1968 switch (ct_get_config (c->board)) {
1969 case CFG_A: length += sprintf (s + length, " cfg=A"); break;
1970 case CFG_B: length += sprintf (s + length, " cfg=B"); break;
1971 case CFG_C: length += sprintf (s + length, " cfg=C"); break;
1972 default: length += sprintf (s + length, " cfg=unknown"); break;
1975 if (ct_get_baud (c))
1976 length += sprintf (s + length, " %ld", ct_get_baud (c));
1978 length += sprintf (s + length, " extclock");
1980 if (c->mode == M_E1 || c->mode == M_G703)
1981 switch (ct_get_clk(c)) {
1982 case GCLK_INT : length += sprintf (s + length, " syn=int"); break;
1983 case GCLK_RCV : length += sprintf (s + length, " syn=rcv"); break;
1984 case GCLK_RCLKO : length += sprintf (s + length, " syn=xrcv"); break;
1986 if (c->mode == M_HDLC) {
1987 length += sprintf (s + length, " dpll=%s", ct_get_dpll (c) ? "on" : "off");
1988 length += sprintf (s + length, " nrzi=%s", ct_get_nrzi (c) ? "on" : "off");
1989 length += sprintf (s + length, " invtclk=%s", ct_get_invtxc (c) ? "on" : "off");
1990 length += sprintf (s + length, " invrclk=%s", ct_get_invrxc (c) ? "on" : "off");
1992 if (c->mode == M_E1)
1993 length += sprintf (s + length, " higain=%s", ct_get_higain (c)? "on" : "off");
1995 length += sprintf (s + length, " loop=%s", ct_get_loop (c) ? "on" : "off");
1997 if (c->mode == M_E1)
1998 length += sprintf (s + length, " ts=%s", format_timeslots (ct_get_ts(c)));
1999 if (c->mode == M_E1 && ct_get_config (c->board) != CFG_A)
2000 length += sprintf (s + length, " pass=%s", format_timeslots (ct_get_subchan(c->board)));
2001 if (c->mode == M_G703) {
2009 length += sprintf (s + length, " (level=-%.1fdB)", lq / 10.0);
2011 length += sprintf (s + length, "\n");
2015 static int ng_ct_rcvmsg (node_p node, item_p item, hook_p lasthook)
2017 drv_t *d = NG_NODE_PRIVATE (node);
2018 struct ng_mesg *msg;
2019 struct ng_mesg *resp = NULL;
2025 CT_DEBUG (d, ("Rcvmsg\n"));
2026 NGI_GET_MSG (item, msg);
2027 switch (msg->header.typecookie) {
2033 printf ("Don't forget to implement\n");
2037 case NGM_GENERIC_COOKIE:
2038 switch (msg->header.cmd) {
2043 case NGM_TEXT_STATUS: {
2046 int dl = sizeof (struct ng_mesg) + 730;
2048 NG_MKRESPONSE (resp, msg, dl, M_NOWAIT);
2054 l += print_chan (s + l, d->chan);
2055 l += print_stats (s + l, d->chan, 1);
2056 l += print_modems (s + l, d->chan, 1);
2057 l += print_e1_stats (s + l, d->chan);
2058 strncpy ((resp)->header.cmdstr, "status", NG_CMDSTRSIZ);
2064 NG_RESPOND_MSG (error, node, item, resp);
2069 static int ng_ct_rcvdata (hook_p hook, item_p item)
2071 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE(hook));
2073 struct ng_tag_prio *ptag;
2082 NGI_GET_M (item, m);
2083 NG_FREE_ITEM (item);
2084 if (! NG_HOOK_PRIVATE (hook) || ! d) {
2089 /* Check for high priority data */
2090 if ((ptag = (struct ng_tag_prio *)m_tag_locate(m, NGM_GENERIC_COOKIE,
2091 NG_TAG_PRIO, NULL)) != NULL && (ptag->priority > NG_PRIO_CUTOFF) )
2099 if (_IF_QFULL (q)) {
2115 static int ng_ct_rmnode (node_p node)
2117 drv_t *d = NG_NODE_PRIVATE (node);
2120 CT_DEBUG (d, ("Rmnode\n"));
2121 if (d && d->running) {
2130 if (node->nd_flags & NGF_REALLY_DIE) {
2131 NG_NODE_SET_PRIVATE (node, NULL);
2132 NG_NODE_UNREF (node);
2134 NG_NODE_REVIVE(node); /* Persistant node */
2139 static int ng_ct_connect (hook_p hook)
2141 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2146 callout_reset (&d->timeout_handle, hz, ct_watchdog_timer, d);
2150 static int ng_ct_disconnect (hook_p hook)
2152 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2161 if (NG_HOOK_PRIVATE (hook))
2164 /* If we were wait it than it reasserted now, just stop it. */
2165 if (!callout_drain (&d->timeout_handle))
2166 callout_stop (&d->timeout_handle);
2171 static int ct_modevent (module_t mod, int type, void *unused)
2173 static int load_count = 0;
2178 if (ng_newtype (&typestruct))
2179 printf ("Failed to register ng_ct\n");
2182 callout_init (&timeout_handle, CALLOUT_MPSAFE);
2183 callout_reset (&timeout_handle, hz*5, ct_timeout, 0);
2186 if (load_count == 1) {
2187 printf ("Removing device entry for Tau-ISA\n");
2189 ng_rmtype (&typestruct);
2192 /* If we were wait it than it reasserted now, just stop it. */
2193 if (!callout_drain (&timeout_handle))
2194 callout_stop (&timeout_handle);
2204 static struct ng_type typestruct = {
2205 .version = NG_ABI_VERSION,
2206 .name = NG_CT_NODE_TYPE,
2207 .constructor = ng_ct_constructor,
2208 .rcvmsg = ng_ct_rcvmsg,
2209 .shutdown = ng_ct_rmnode,
2210 .newhook = ng_ct_newhook,
2211 .connect = ng_ct_connect,
2212 .rcvdata = ng_ct_rcvdata,
2213 .disconnect = ng_ct_disconnect,
2218 MODULE_DEPEND (ng_ct, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION);
2220 MODULE_DEPEND (ct, sppp, 1, 1, 1);
2222 DRIVER_MODULE (ct, isa, ct_isa_driver, ct_devclass, ct_modevent, NULL);
2223 MODULE_VERSION (ct, 1);