2 * Cronyx-Tau-PCI adapter driver for FreeBSD.
3 * Supports PPP/HDLC, Cisco/HDLC and FrameRelay 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) 1999-2004 Cronyx Engineering.
8 * Author: Kurakin Roman, <rik@cronyx.ru>
10 * Copyright (C) 1999-2002 Cronyx Engineering.
11 * Author: Serge Vakulenko, <vak@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_cp.c,v 1.1.2.41 2004/06/23 17:09:13 rik Exp $
24 #include <sys/cdefs.h>
25 __FBSDID("$FreeBSD$");
27 #include <sys/param.h>
28 #include <sys/ucred.h>
30 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
35 #include <sys/malloc.h>
36 #include <sys/socket.h>
37 #include <sys/sockio.h>
38 #include <sys/sysctl.h>
44 #include <dev/pci/pcivar.h>
45 #include <dev/pci/pcireg.h>
46 #include <machine/bus.h>
48 #include "opt_ng_cronyx.h"
49 #ifdef NETGRAPH_CRONYX
50 # include "opt_netgraph.h"
52 # error #option NETGRAPH missed from configuration
54 # include <netgraph/ng_message.h>
55 # include <netgraph/netgraph.h>
56 # include <dev/cp/ng_cp.h>
58 # include <net/if_sppp.h>
59 # include <net/if_types.h>
60 #include <dev/pci/pcivar.h>
61 # define PP_CISCO IFF_LINK2
64 #include <dev/cx/machdep.h>
65 #include <dev/cp/cpddk.h>
66 #include <machine/cserial.h>
67 #include <machine/resource.h>
68 #include <machine/pmap.h>
70 /* If we don't have Cronyx's sppp version, we don't have fr support via sppp */
75 #define CP_DEBUG(d,s) ({if (d->chan->debug) {\
76 printf ("%s: ", d->name); printf s;}})
77 #define CP_DEBUG2(d,s) ({if (d->chan->debug>1) {\
78 printf ("%s: ", d->name); printf s;}})
79 #define CP_LOCK_NAME "cpX"
81 static int cp_mpsafenet = 1;
82 TUNABLE_INT("debug.cp.mpsafenet", &cp_mpsafenet);
83 SYSCTL_NODE(_debug, OID_AUTO, cp, CTLFLAG_RD, 0, "Cronyx Tau-PCI Adapters");
84 SYSCTL_INT(_debug_cp, OID_AUTO, mpsafenet, CTLFLAG_RD, &cp_mpsafenet, 0,
85 "Enable/disable MPSAFE network support for Cronyx Tau-PCI Adapters");
87 #define CP_LOCK(_bd) do { \
89 mtx_lock (&(_bd)->cp_mtx); \
91 #define CP_UNLOCK(_bd) do { \
93 mtx_unlock (&(_bd)->cp_mtx); \
96 #define CP_LOCK_ASSERT(_bd) do { \
98 mtx_assert (&(_bd)->cp_mtx, MA_OWNED); \
101 static int cp_probe __P((device_t));
102 static int cp_attach __P((device_t));
103 static int cp_detach __P((device_t));
105 static device_method_t cp_methods[] = {
106 /* Device interface */
107 DEVMETHOD(device_probe, cp_probe),
108 DEVMETHOD(device_attach, cp_attach),
109 DEVMETHOD(device_detach, cp_detach),
114 typedef struct _cp_dma_mem_t {
122 typedef struct _drv_t {
129 char nodename [NG_NODELEN+1];
133 struct ifqueue queue;
134 struct ifqueue hi_queue;
136 struct callout timeout_handle;
138 struct ifqueue queue;
144 typedef struct _bdrv_t {
146 struct resource *cp_res;
147 struct resource *cp_irq;
150 drv_t channel [NCHAN];
154 static driver_t cp_driver = {
160 static devclass_t cp_devclass;
162 static void cp_receive (cp_chan_t *c, unsigned char *data, int len);
163 static void cp_transmit (cp_chan_t *c, void *attachment, int len);
164 static void cp_error (cp_chan_t *c, int data);
165 static void cp_up (drv_t *d);
166 static void cp_start (drv_t *d);
167 static void cp_down (drv_t *d);
168 static void cp_watchdog (drv_t *d);
170 extern struct ng_type typestruct;
172 static void cp_ifstart (struct ifnet *ifp);
173 static void cp_tlf (struct sppp *sp);
174 static void cp_tls (struct sppp *sp);
175 static void cp_ifwatchdog (struct ifnet *ifp);
176 static int cp_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data);
177 static void cp_initialize (void *softc);
180 static cp_board_t *adapter [NBRD];
181 static drv_t *channel [NBRD*NCHAN];
182 static struct callout led_timo [NBRD];
183 static struct callout timeout_handle;
185 static int cp_destroy = 0;
188 * Print the mbuf chain, for debug purposes only.
190 static void printmbuf (struct mbuf *m)
193 for (; m; m=m->m_next) {
194 if (m->m_flags & M_PKTHDR)
195 printf (" HDR %d:", m->m_pkthdr.len);
196 if (m->m_flags & M_EXT)
198 printf (" %d", m->m_len);
204 * Make an mbuf from data.
206 static struct mbuf *makembuf (void *buf, unsigned len)
210 MGETHDR (m, M_DONTWAIT, MT_DATA);
213 MCLGET (m, M_DONTWAIT);
214 if (! (m->m_flags & M_EXT)) {
218 m->m_pkthdr.len = m->m_len = len;
219 bcopy (buf, mtod (m, caddr_t), len);
223 static int cp_probe (device_t dev)
225 if ((pci_get_vendor (dev) == cp_vendor_id) &&
226 (pci_get_device (dev) == cp_device_id)) {
227 device_set_desc (dev, "Cronyx-Tau-PCI serial adapter");
228 return BUS_PROBE_DEFAULT;
233 static void cp_timeout (void *arg)
238 for (i = 0; i < NBRD; ++i) {
239 if (adapter[i] == NULL)
241 for (k = 0; k < NCHAN; ++k) {
247 d = channel[i * NCHAN + k];
252 CP_LOCK ((bdrv_t *)d->board->sys);
253 switch (d->chan->type) {
255 cp_g703_timer (d->chan);
258 cp_e1_timer (d->chan);
263 cp_e3_timer (d->chan);
268 CP_UNLOCK ((bdrv_t *)d->board->sys);
274 callout_reset (&timeout_handle, hz, cp_timeout, 0);
278 static void cp_led_off (void *arg)
281 bdrv_t *bd = (bdrv_t *) b->sys;
294 static void cp_intr (void *arg)
297 cp_board_t *b = bd->board;
307 /* Check if we are ready */
308 if (b->sys == NULL) {
309 /* Not we are not, just cleanup. */
310 cp_interrupt_poll (b, 1);
319 /* Turn LED off 50 msec later. */
320 callout_reset (&led_timo[b->num], hz/20, cp_led_off, b);
325 /* Pass packets in a lock-free state */
326 for (i = 0; i < NCHAN && b->chan[i].type; i++) {
327 drv_t *d = b->chan[i].sys;
329 if (!d || !d->running)
331 while (_IF_QLEN(&d->queue)) {
332 IF_DEQUEUE (&d->queue,m);
335 sppp_input (d->ifp, m);
341 extern struct cdevsw cp_cdevsw;
344 cp_bus_dmamap_addr (void *arg, bus_dma_segment_t *segs, int nseg, int error)
351 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
353 *addr = segs->ds_addr;
357 cp_bus_dma_mem_alloc (int bnum, int cnum, cp_dma_mem_t *dmem)
361 error = bus_dma_tag_create (NULL, 16, 0, BUS_SPACE_MAXADDR_32BIT,
362 BUS_SPACE_MAXADDR, NULL, NULL, dmem->size, 1,
363 dmem->size, 0, NULL, NULL, &dmem->dmat);
365 if (cnum >= 0) printf ("cp%d-%d: ", bnum, cnum);
366 else printf ("cp%d: ", bnum);
367 printf ("couldn't allocate tag for dma memory\n");
370 error = bus_dmamem_alloc (dmem->dmat, (void **)&dmem->virt,
371 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &dmem->mapp);
373 if (cnum >= 0) printf ("cp%d-%d: ", bnum, cnum);
374 else printf ("cp%d: ", bnum);
375 printf ("couldn't allocate mem for dma memory\n");
376 bus_dma_tag_destroy (dmem->dmat);
379 error = bus_dmamap_load (dmem->dmat, dmem->mapp, dmem->virt,
380 dmem->size, cp_bus_dmamap_addr, &dmem->phys, 0);
382 if (cnum >= 0) printf ("cp%d-%d: ", bnum, cnum);
383 else printf ("cp%d: ", bnum);
384 printf ("couldn't load mem map for dma memory\n");
385 bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
386 bus_dma_tag_destroy (dmem->dmat);
393 cp_bus_dma_mem_free (cp_dma_mem_t *dmem)
395 bus_dmamap_unload (dmem->dmat, dmem->mapp);
396 bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
397 bus_dma_tag_destroy (dmem->dmat);
401 * Called if the probe succeeded.
403 static int cp_attach (device_t dev)
405 bdrv_t *bd = device_get_softc (dev);
406 int unit = device_get_unit (dev);
407 char *cp_ln = CP_LOCK_NAME;
416 b = malloc (sizeof(cp_board_t), M_DEVBUF, M_WAITOK);
418 printf ("cp%d: couldn't allocate memory\n", unit);
422 bzero (b, sizeof(cp_board_t));
426 bd->cp_res = bus_alloc_resource (dev, SYS_RES_MEMORY, &rid,
427 0, ~0, 1, RF_ACTIVE);
429 printf ("cp%d: cannot map memory\n", unit);
434 vbase = (vm_offset_t) rman_get_virtual (bd->cp_res);
436 cp_ln[2] = '0' + unit;
437 mtx_init (&bd->cp_mtx, cp_ln, MTX_NETWORK_LOCK, MTX_DEF|MTX_RECURSE);
438 res = cp_init (b, unit, (u_char*) vbase);
440 printf ("cp%d: can't init, error code:%x\n", unit, res);
441 bus_release_resource (dev, SYS_RES_MEMORY, PCIR_BAR(0), bd->cp_res);
447 bd->dmamem.size = sizeof(cp_qbuf_t);
448 if (! cp_bus_dma_mem_alloc (unit, -1, &bd->dmamem)) {
454 cp_reset (b, bd->dmamem.virt, bd->dmamem.phys);
458 bd->cp_irq = bus_alloc_resource (dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
459 RF_SHAREABLE | RF_ACTIVE);
462 printf ("cp%d: cannot map interrupt\n", unit);
463 bus_release_resource (dev, SYS_RES_MEMORY,
464 PCIR_BAR(0), bd->cp_res);
465 mtx_destroy (&bd->cp_mtx);
470 callout_init (&led_timo[unit], cp_mpsafenet ? CALLOUT_MPSAFE : 0);
471 error = bus_setup_intr (dev, bd->cp_irq,
472 INTR_TYPE_NET|(cp_mpsafenet?INTR_MPSAFE:0),
473 cp_intr, bd, &bd->cp_intrhand);
476 printf ("cp%d: cannot set up irq\n", unit);
477 bus_release_resource (dev, SYS_RES_IRQ, 0, bd->cp_irq);
478 bus_release_resource (dev, SYS_RES_MEMORY,
479 PCIR_BAR(0), bd->cp_res);
480 mtx_destroy (&bd->cp_mtx);
485 printf ("cp%d: %s, clock %ld MHz\n", unit, b->name, b->osc / 1000000);
487 for (c = b->chan; c < b->chan + NCHAN; ++c) {
490 d = &bd->channel[c->num];
491 d->dmamem.size = sizeof(cp_buf_t);
492 if (! cp_bus_dma_mem_alloc (unit, c->num, &d->dmamem))
494 channel [b->num*NCHAN + c->num] = d;
495 sprintf (d->name, "cp%d.%d", b->num, c->num);
500 if (ng_make_node_common (&typestruct, &d->node) != 0) {
501 printf ("%s: cannot make common node\n", d->name);
505 NG_NODE_SET_PRIVATE (d->node, d);
506 sprintf (d->nodename, "%s%d", NG_CP_NODE_TYPE,
507 c->board->num*NCHAN + c->num);
508 if (ng_name_node (d->node, d->nodename)) {
509 printf ("%s: cannot name node\n", d->nodename);
510 NG_NODE_UNREF (d->node);
513 d->queue.ifq_maxlen = IFQ_MAXLEN;
514 d->hi_queue.ifq_maxlen = IFQ_MAXLEN;
515 mtx_init (&d->queue.ifq_mtx, "cp_queue", NULL, MTX_DEF);
516 mtx_init (&d->hi_queue.ifq_mtx, "cp_queue_hi", NULL, MTX_DEF);
517 callout_init (&d->timeout_handle,
518 cp_mpsafenet ? CALLOUT_MPSAFE : 0);
520 d->ifp = if_alloc(IFT_PPP);
521 if (d->ifp == NULL) {
522 printf ("%s: cannot if_alloc() interface\n", d->name);
525 d->ifp->if_softc = d;
526 if_initname (d->ifp, "cp", b->num * NCHAN + c->num);
527 d->ifp->if_mtu = PP_MTU;
528 d->ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
530 d->ifp->if_flags |= IFF_NEEDSGIANT;
531 d->ifp->if_ioctl = cp_sioctl;
532 d->ifp->if_start = cp_ifstart;
533 d->ifp->if_watchdog = cp_ifwatchdog;
534 d->ifp->if_init = cp_initialize;
535 d->queue.ifq_maxlen = NRBUF;
536 mtx_init (&d->queue.ifq_mtx, "cp_queue", NULL, MTX_DEF);
537 sppp_attach (d->ifp);
539 IFP2SP(d->ifp)->pp_tlf = cp_tlf;
540 IFP2SP(d->ifp)->pp_tls = cp_tls;
541 /* If BPF is in the kernel, call the attach for it.
542 * The header size of PPP or Cisco/HDLC is 4 bytes. */
543 bpfattach (d->ifp, DLT_PPP, 4);
546 cp_start_chan (c, 1, 1, d->dmamem.virt, d->dmamem.phys);
548 /* Register callback functions. */
549 cp_register_transmit (c, &cp_transmit);
550 cp_register_receive (c, &cp_receive);
551 cp_register_error (c, &cp_error);
552 d->devt = make_dev (&cp_cdevsw, b->num*NCHAN+c->num, UID_ROOT,
553 GID_WHEEL, 0600, "cp%d", b->num*NCHAN+c->num);
563 static int cp_detach (device_t dev)
565 bdrv_t *bd = device_get_softc (dev);
566 cp_board_t *b = bd->board;
570 KASSERT (mtx_initialized (&bd->cp_mtx), ("cp mutex not initialized"));
573 /* Check if the device is busy (open). */
574 for (c = b->chan; c < b->chan + NCHAN; ++c) {
575 drv_t *d = (drv_t*) c->sys;
577 if (! d || ! d->chan->type)
586 /* Ok, we can unload driver */
587 /* At first we should stop all channels */
588 for (c = b->chan; c < b->chan + NCHAN; ++c) {
589 drv_t *d = (drv_t*) c->sys;
591 if (! d || ! d->chan->type)
596 cp_set_dtr (d->chan, 0);
597 cp_set_rts (d->chan, 0);
600 /* Reset the adapter. */
602 cp_interrupt_poll (b, 1);
605 callout_stop (&led_timo[b->num]);
607 for (c=b->chan; c<b->chan+NCHAN; ++c) {
608 drv_t *d = (drv_t*) c->sys;
610 if (! d || ! d->chan->type)
613 /* Detach from the packet filter list of interfaces. */
616 /* Detach from the sync PPP list. */
617 sppp_detach (d->ifp);
619 /* Detach from the system list of interfaces. */
622 IF_DRAIN (&d->queue);
623 mtx_destroy (&d->queue.ifq_mtx);
626 ng_rmnode_self (d->node);
627 NG_NODE_UNREF (d->node);
630 mtx_destroy (&d->queue.ifq_mtx);
631 mtx_destroy (&d->hi_queue.ifq_mtx);
633 destroy_dev (d->devt);
639 /* Disable the interrupt request. */
640 bus_teardown_intr (dev, bd->cp_irq, bd->cp_intrhand);
641 bus_release_resource (dev, SYS_RES_IRQ, 0, bd->cp_irq);
642 bus_release_resource (dev, SYS_RES_MEMORY, PCIR_BAR(0), bd->cp_res);
647 callout_drain (&led_timo[b->num]);
651 for (c = b->chan; c < b->chan + NCHAN; ++c) {
652 drv_t *d = (drv_t*) c->sys;
654 if (! d || ! d->chan->type)
656 channel [b->num*NCHAN + c->num] = 0;
657 /* Deallocate buffers. */
658 cp_bus_dma_mem_free (&d->dmamem);
660 adapter [b->num] = 0;
661 cp_bus_dma_mem_free (&bd->dmamem);
664 mtx_destroy (&bd->cp_mtx);
669 static void cp_ifstart (struct ifnet *ifp)
671 drv_t *d = ifp->if_softc;
672 bdrv_t *bd = d->board->sys;
679 static void cp_ifwatchdog (struct ifnet *ifp)
681 drv_t *d = ifp->if_softc;
686 static void cp_tlf (struct sppp *sp)
688 drv_t *d = SP2IFP(sp)->if_softc;
690 CP_DEBUG2 (d, ("cp_tlf\n"));
691 /* XXXRIK: Don't forget to protect them by LOCK, or kill them. */
692 /* cp_set_dtr (d->chan, 0);*/
693 /* cp_set_rts (d->chan, 0);*/
694 if (!(sp->pp_flags & PP_FR) && !(d->ifp->if_flags & PP_CISCO))
698 static void cp_tls (struct sppp *sp)
700 drv_t *d = SP2IFP(sp)->if_softc;
702 CP_DEBUG2 (d, ("cp_tls\n"));
703 if (!(sp->pp_flags & PP_FR) && !(d->ifp->if_flags & PP_CISCO))
708 * Process an ioctl request.
710 static int cp_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data)
712 drv_t *d = ifp->if_softc;
713 bdrv_t *bd = d->board->sys;
714 int error, s, was_up, should_be_up;
716 was_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
717 error = sppp_ioctl (ifp, cmd, data);
722 if (! (ifp->if_flags & IFF_DEBUG))
724 else if (! d->chan->debug)
728 default: CP_DEBUG2 (d, ("ioctl 0x%lx\n", cmd)); return 0;
729 case SIOCADDMULTI: CP_DEBUG2 (d, ("ioctl SIOCADDMULTI\n")); return 0;
730 case SIOCDELMULTI: CP_DEBUG2 (d, ("ioctl SIOCDELMULTI\n")); return 0;
731 case SIOCSIFFLAGS: CP_DEBUG2 (d, ("ioctl SIOCSIFFLAGS\n")); break;
732 case SIOCSIFADDR: CP_DEBUG2 (d, ("ioctl SIOCSIFADDR\n")); break;
735 /* We get here only in case of SIFFLAGS or SIFADDR. */
738 should_be_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
739 if (! was_up && should_be_up) {
740 /* Interface goes up -- start it. */
743 } else if (was_up && ! should_be_up) {
744 /* Interface is going down -- stop it. */
745 /* if ((IFP2SP(ifp)->pp_flags & PP_FR) || (ifp->if_flags & PP_CISCO))*/
748 CP_DEBUG (d, ("ioctl 0x%lx p4\n", cmd));
755 * Initialization of interface.
756 * It seems to be never called by upper level?
758 static void cp_initialize (void *softc)
762 CP_DEBUG (d, ("cp_initialize\n"));
767 * Stop the interface. Called on splimp().
769 static void cp_down (drv_t *d)
771 CP_DEBUG (d, ("cp_down\n"));
772 /* Interface is going down -- stop it. */
773 cp_set_dtr (d->chan, 0);
774 cp_set_rts (d->chan, 0);
780 * Start the interface. Called on splimp().
782 static void cp_up (drv_t *d)
784 CP_DEBUG (d, ("cp_up\n"));
785 cp_set_dtr (d->chan, 1);
786 cp_set_rts (d->chan, 1);
791 * Start output on the interface. Get another datagram to send
792 * off of the interface queue, and copy it to the interface
793 * before starting the output.
795 static void cp_send (drv_t *d)
800 CP_DEBUG2 (d, ("cp_send, tn=%d te=%d\n", d->chan->tn, d->chan->te));
802 /* No output if the interface is down. */
806 /* No output if the modem is off. */
807 if (! (d->chan->lloop || d->chan->type != T_SERIAL ||
808 cp_get_dsr (d->chan)))
811 while (cp_transmit_space (d->chan)) {
812 /* Get the packet to send. */
814 IF_DEQUEUE (&d->hi_queue, m);
816 IF_DEQUEUE (&d->queue, m);
818 m = sppp_dequeue (d->ifp);
823 BPF_MTAP (d->ifp, m);
825 len = m_length (m, NULL);
827 printf ("%s: too long packet: %d bytes: ",
829 else if (! m->m_next)
830 cp_send_packet (d->chan, (u_char*) mtod (m, caddr_t), len, 0);
832 u_char *buf = d->chan->tbuf[d->chan->te];
833 m_copydata (m, 0, len, buf);
834 cp_send_packet (d->chan, buf, len, 0);
837 /* Set up transmit timeout, if the transmit ring is not empty.*/
841 d->ifp->if_timer = 10;
845 d->ifp->if_drv_flags |= IFF_DRV_OACTIVE;
850 * Start output on the interface.
851 * Always called on splimp().
853 static void cp_start (drv_t *d)
857 cp_set_dtr (d->chan, 1);
859 cp_set_rts (d->chan, 1);
865 * Handle transmit timeouts.
866 * Recover after lost transmit interrupts.
867 * Always called on splimp().
869 static void cp_watchdog (drv_t *d)
871 bdrv_t *bd = d->board->sys;
872 CP_DEBUG (d, ("device timeout\n"));
877 cp_stop_chan (d->chan);
878 cp_stop_e1 (d->chan);
879 cp_start_e1 (d->chan);
880 cp_start_chan (d->chan, 1, 1, 0, 0);
881 cp_set_dtr (d->chan, 1);
882 cp_set_rts (d->chan, 1);
889 static void cp_transmit (cp_chan_t *c, void *attachment, int len)
896 ++d->ifp->if_opackets;
897 d->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
898 d->ifp->if_timer = 0;
903 static void cp_receive (cp_chan_t *c, unsigned char *data, int len)
914 m = makembuf (data, len);
916 CP_DEBUG (d, ("no memory for packet\n"));
918 ++d->ifp->if_iqdrops;
925 m->m_pkthdr.rcvif = 0;
926 NG_SEND_DATA_ONLY (error, d->hook, m);
928 ++d->ifp->if_ipackets;
929 m->m_pkthdr.rcvif = d->ifp;
930 /* Check if there's a BPF listener on this interface.
931 * If so, hand off the raw packet to bpf. */
932 BPF_TAP (d->ifp, data, len);
933 IF_ENQUEUE (&d->queue, m);
937 static void cp_error (cp_chan_t *c, int data)
943 CP_DEBUG (d, ("frame error\n"));
945 ++d->ifp->if_ierrors;
949 CP_DEBUG (d, ("crc error\n"));
951 ++d->ifp->if_ierrors;
955 CP_DEBUG (d, ("overrun error\n"));
957 ++d->ifp->if_collisions;
958 ++d->ifp->if_ierrors;
962 CP_DEBUG (d, ("overflow error\n"));
964 ++d->ifp->if_ierrors;
968 CP_DEBUG (d, ("underrun error\n"));
972 ++d->ifp->if_oerrors;
973 d->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
974 d->ifp->if_timer = 0;
979 CP_DEBUG (d, ("error #%d\n", data));
985 * You also need read, write, open, close routines.
986 * This should get you started
988 static int cp_open (struct cdev *dev, int oflags, int devtype, struct thread *td)
990 int unit = minor (dev);
993 if (unit >= NBRD*NCHAN || ! (d = channel[unit]))
995 CP_DEBUG2 (d, ("cp_open\n"));
1000 * Only called on the LAST close.
1002 static int cp_close (struct cdev *dev, int fflag, int devtype, struct thread *td)
1004 drv_t *d = channel [minor (dev)];
1006 CP_DEBUG2 (d, ("cp_close\n"));
1010 static int cp_modem_status (cp_chan_t *c)
1013 bdrv_t *bd = d->board->sys;
1016 status = d->running ? TIOCM_LE : 0;
1019 if (cp_get_cd (c)) status |= TIOCM_CD;
1020 if (cp_get_cts (c)) status |= TIOCM_CTS;
1021 if (cp_get_dsr (c)) status |= TIOCM_DSR;
1022 if (c->dtr) status |= TIOCM_DTR;
1023 if (c->rts) status |= TIOCM_RTS;
1029 static int cp_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
1031 drv_t *d = channel [minor (dev)];
1032 bdrv_t *bd = d->board->sys;
1033 cp_chan_t *c = d->chan;
1034 struct serial_statistics *st;
1035 struct e1_statistics *opte1;
1036 struct e3_statistics *opte3;
1041 case SERIAL_GETREGISTERED:
1042 CP_DEBUG2 (d, ("ioctl: getregistered\n"));
1043 bzero (mask, sizeof(mask));
1044 for (s=0; s<NBRD*NCHAN; ++s)
1046 mask [s/8] |= 1 << (s & 7);
1047 bcopy (mask, data, sizeof (mask));
1051 case SERIAL_GETPROTO:
1052 CP_DEBUG2 (d, ("ioctl: getproto\n"));
1053 strcpy ((char*)data, (IFP2SP(d->ifp)->pp_flags & PP_FR) ? "fr" :
1054 (d->ifp->if_flags & PP_CISCO) ? "cisco" : "ppp");
1057 case SERIAL_SETPROTO:
1058 CP_DEBUG2 (d, ("ioctl: setproto\n"));
1059 /* Only for superuser! */
1063 if (d->ifp->if_drv_flags & IFF_DRV_RUNNING)
1065 if (! strcmp ("cisco", (char*)data)) {
1066 IFP2SP(d->ifp)->pp_flags &= ~(PP_FR);
1067 IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE;
1068 d->ifp->if_flags |= PP_CISCO;
1069 } else if (! strcmp ("fr", (char*)data) && PP_FR) {
1070 d->ifp->if_flags &= ~(PP_CISCO);
1071 IFP2SP(d->ifp)->pp_flags |= PP_FR | PP_KEEPALIVE;
1072 } else if (! strcmp ("ppp", (char*)data)) {
1073 IFP2SP(d->ifp)->pp_flags &= ~PP_FR;
1074 IFP2SP(d->ifp)->pp_flags &= ~PP_KEEPALIVE;
1075 d->ifp->if_flags &= ~(PP_CISCO);
1080 case SERIAL_GETKEEPALIVE:
1081 CP_DEBUG2 (d, ("ioctl: getkeepalive\n"));
1082 if ((IFP2SP(d->ifp)->pp_flags & PP_FR) ||
1083 (d->ifp->if_flags & PP_CISCO))
1085 *(int*)data = (IFP2SP(d->ifp)->pp_flags & PP_KEEPALIVE) ? 1 : 0;
1088 case SERIAL_SETKEEPALIVE:
1089 CP_DEBUG2 (d, ("ioctl: setkeepalive\n"));
1090 /* Only for superuser! */
1094 if ((IFP2SP(d->ifp)->pp_flags & PP_FR) ||
1095 (d->ifp->if_flags & PP_CISCO))
1100 IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE;
1102 IFP2SP(d->ifp)->pp_flags &= ~PP_KEEPALIVE;
1108 case SERIAL_GETMODE:
1109 CP_DEBUG2 (d, ("ioctl: getmode\n"));
1110 *(int*)data = SERIAL_HDLC;
1113 case SERIAL_SETMODE:
1114 /* Only for superuser! */
1118 if (*(int*)data != SERIAL_HDLC)
1123 CP_DEBUG2 (d, ("ioctl: getcfg\n"));
1124 if (c->type != T_E1 || c->unfram)
1126 *(char*)data = c->board->mux ? 'c' : 'a';
1130 CP_DEBUG2 (d, ("ioctl: setcfg\n"));
1134 if (c->type != T_E1)
1138 cp_set_mux (c->board, *((char*)data) == 'c');
1143 case SERIAL_GETSTAT:
1144 CP_DEBUG2 (d, ("ioctl: getstat\n"));
1145 st = (struct serial_statistics*) data;
1146 st->rintr = c->rintr;
1147 st->tintr = c->tintr;
1149 st->ibytes = c->ibytes;
1150 st->ipkts = c->ipkts;
1151 st->obytes = c->obytes;
1152 st->opkts = c->opkts;
1153 st->ierrs = c->overrun + c->frame + c->crc;
1154 st->oerrs = c->underrun;
1157 case SERIAL_GETESTAT:
1158 CP_DEBUG2 (d, ("ioctl: getestat\n"));
1159 if (c->type != T_E1 && c->type != T_G703)
1161 opte1 = (struct e1_statistics*) data;
1162 opte1->status = c->status;
1163 opte1->cursec = c->cursec;
1164 opte1->totsec = c->totsec + c->cursec;
1166 opte1->currnt.bpv = c->currnt.bpv;
1167 opte1->currnt.fse = c->currnt.fse;
1168 opte1->currnt.crce = c->currnt.crce;
1169 opte1->currnt.rcrce = c->currnt.rcrce;
1170 opte1->currnt.uas = c->currnt.uas;
1171 opte1->currnt.les = c->currnt.les;
1172 opte1->currnt.es = c->currnt.es;
1173 opte1->currnt.bes = c->currnt.bes;
1174 opte1->currnt.ses = c->currnt.ses;
1175 opte1->currnt.oofs = c->currnt.oofs;
1176 opte1->currnt.css = c->currnt.css;
1177 opte1->currnt.dm = c->currnt.dm;
1179 opte1->total.bpv = c->total.bpv + c->currnt.bpv;
1180 opte1->total.fse = c->total.fse + c->currnt.fse;
1181 opte1->total.crce = c->total.crce + c->currnt.crce;
1182 opte1->total.rcrce = c->total.rcrce + c->currnt.rcrce;
1183 opte1->total.uas = c->total.uas + c->currnt.uas;
1184 opte1->total.les = c->total.les + c->currnt.les;
1185 opte1->total.es = c->total.es + c->currnt.es;
1186 opte1->total.bes = c->total.bes + c->currnt.bes;
1187 opte1->total.ses = c->total.ses + c->currnt.ses;
1188 opte1->total.oofs = c->total.oofs + c->currnt.oofs;
1189 opte1->total.css = c->total.css + c->currnt.css;
1190 opte1->total.dm = c->total.dm + c->currnt.dm;
1191 for (s=0; s<48; ++s) {
1192 opte1->interval[s].bpv = c->interval[s].bpv;
1193 opte1->interval[s].fse = c->interval[s].fse;
1194 opte1->interval[s].crce = c->interval[s].crce;
1195 opte1->interval[s].rcrce = c->interval[s].rcrce;
1196 opte1->interval[s].uas = c->interval[s].uas;
1197 opte1->interval[s].les = c->interval[s].les;
1198 opte1->interval[s].es = c->interval[s].es;
1199 opte1->interval[s].bes = c->interval[s].bes;
1200 opte1->interval[s].ses = c->interval[s].ses;
1201 opte1->interval[s].oofs = c->interval[s].oofs;
1202 opte1->interval[s].css = c->interval[s].css;
1203 opte1->interval[s].dm = c->interval[s].dm;
1207 case SERIAL_GETE3STAT:
1208 CP_DEBUG2 (d, ("ioctl: gete3stat\n"));
1209 if (c->type != T_E3 && c->type != T_T3 && c->type != T_STS1)
1211 opte3 = (struct e3_statistics*) data;
1213 opte3->status = c->e3status;
1214 opte3->cursec = (c->e3csec_5 * 2 + 1) / 10;
1215 opte3->totsec = c->e3tsec + opte3->cursec;
1217 opte3->ccv = c->e3ccv;
1218 opte3->tcv = c->e3tcv + opte3->ccv;
1220 for (s = 0; s < 48; ++s) {
1221 opte3->icv[s] = c->e3icv[s];
1225 case SERIAL_CLRSTAT:
1226 CP_DEBUG2 (d, ("ioctl: clrstat\n"));
1227 /* Only for superuser! */
1241 bzero (&c->currnt, sizeof (c->currnt));
1242 bzero (&c->total, sizeof (c->total));
1243 bzero (c->interval, sizeof (c->interval));
1246 bzero (c->e3icv, sizeof (c->e3icv));
1249 case SERIAL_GETBAUD:
1250 CP_DEBUG2 (d, ("ioctl: getbaud\n"));
1251 *(long*)data = c->baud;
1254 case SERIAL_SETBAUD:
1255 CP_DEBUG2 (d, ("ioctl: setbaud\n"));
1256 /* Only for superuser! */
1262 cp_set_baud (c, *(long*)data);
1267 case SERIAL_GETLOOP:
1268 CP_DEBUG2 (d, ("ioctl: getloop\n"));
1269 *(int*)data = c->lloop;
1272 case SERIAL_SETLOOP:
1273 CP_DEBUG2 (d, ("ioctl: setloop\n"));
1274 /* Only for superuser! */
1280 cp_set_lloop (c, *(int*)data);
1285 case SERIAL_GETDPLL:
1286 CP_DEBUG2 (d, ("ioctl: getdpll\n"));
1287 if (c->type != T_SERIAL)
1289 *(int*)data = c->dpll;
1292 case SERIAL_SETDPLL:
1293 CP_DEBUG2 (d, ("ioctl: setdpll\n"));
1294 /* Only for superuser! */
1298 if (c->type != T_SERIAL)
1302 cp_set_dpll (c, *(int*)data);
1307 case SERIAL_GETNRZI:
1308 CP_DEBUG2 (d, ("ioctl: getnrzi\n"));
1309 if (c->type != T_SERIAL)
1311 *(int*)data = c->nrzi;
1314 case SERIAL_SETNRZI:
1315 CP_DEBUG2 (d, ("ioctl: setnrzi\n"));
1316 /* Only for superuser! */
1320 if (c->type != T_SERIAL)
1324 cp_set_nrzi (c, *(int*)data);
1329 case SERIAL_GETDEBUG:
1330 CP_DEBUG2 (d, ("ioctl: getdebug\n"));
1331 *(int*)data = d->chan->debug;
1334 case SERIAL_SETDEBUG:
1335 CP_DEBUG2 (d, ("ioctl: setdebug\n"));
1336 /* Only for superuser! */
1340 d->chan->debug = *(int*)data;
1343 d->ifp->if_flags |= IFF_DEBUG;
1345 d->ifp->if_flags &= ~IFF_DEBUG;
1349 case SERIAL_GETHIGAIN:
1350 CP_DEBUG2 (d, ("ioctl: gethigain\n"));
1351 if (c->type != T_E1)
1353 *(int*)data = c->higain;
1356 case SERIAL_SETHIGAIN:
1357 CP_DEBUG2 (d, ("ioctl: sethigain\n"));
1358 /* Only for superuser! */
1362 if (c->type != T_E1)
1366 cp_set_higain (c, *(int*)data);
1371 case SERIAL_GETPHONY:
1372 CP_DEBUG2 (d, ("ioctl: getphony\n"));
1373 if (c->type != T_E1)
1375 *(int*)data = c->phony;
1378 case SERIAL_SETPHONY:
1379 CP_DEBUG2 (d, ("ioctl: setphony\n"));
1380 /* Only for superuser! */
1384 if (c->type != T_E1)
1388 cp_set_phony (c, *(int*)data);
1393 case SERIAL_GETUNFRAM:
1394 CP_DEBUG2 (d, ("ioctl: getunfram\n"));
1395 if (c->type != T_E1)
1397 *(int*)data = c->unfram;
1400 case SERIAL_SETUNFRAM:
1401 CP_DEBUG2 (d, ("ioctl: setunfram\n"));
1402 /* Only for superuser! */
1406 if (c->type != T_E1)
1410 cp_set_unfram (c, *(int*)data);
1415 case SERIAL_GETSCRAMBLER:
1416 CP_DEBUG2 (d, ("ioctl: getscrambler\n"));
1417 if (c->type != T_G703 && !c->unfram)
1419 *(int*)data = c->scrambler;
1422 case SERIAL_SETSCRAMBLER:
1423 CP_DEBUG2 (d, ("ioctl: setscrambler\n"));
1424 /* Only for superuser! */
1428 if (c->type != T_G703 && !c->unfram)
1432 cp_set_scrambler (c, *(int*)data);
1437 case SERIAL_GETMONITOR:
1438 CP_DEBUG2 (d, ("ioctl: getmonitor\n"));
1439 if (c->type != T_E1 &&
1444 *(int*)data = c->monitor;
1447 case SERIAL_SETMONITOR:
1448 CP_DEBUG2 (d, ("ioctl: setmonitor\n"));
1449 /* Only for superuser! */
1453 if (c->type != T_E1)
1457 cp_set_monitor (c, *(int*)data);
1462 case SERIAL_GETUSE16:
1463 CP_DEBUG2 (d, ("ioctl: getuse16\n"));
1464 if (c->type != T_E1 || c->unfram)
1466 *(int*)data = c->use16;
1469 case SERIAL_SETUSE16:
1470 CP_DEBUG2 (d, ("ioctl: setuse16\n"));
1471 /* Only for superuser! */
1475 if (c->type != T_E1)
1479 cp_set_use16 (c, *(int*)data);
1484 case SERIAL_GETCRC4:
1485 CP_DEBUG2 (d, ("ioctl: getcrc4\n"));
1486 if (c->type != T_E1 || c->unfram)
1488 *(int*)data = c->crc4;
1491 case SERIAL_SETCRC4:
1492 CP_DEBUG2 (d, ("ioctl: setcrc4\n"));
1493 /* Only for superuser! */
1497 if (c->type != T_E1)
1501 cp_set_crc4 (c, *(int*)data);
1507 CP_DEBUG2 (d, ("ioctl: getclk\n"));
1508 if (c->type != T_E1 &&
1509 c->type != T_G703 &&
1515 default: *(int*)data = E1CLK_INTERNAL; break;
1516 case GSYN_RCV: *(int*)data = E1CLK_RECEIVE; break;
1517 case GSYN_RCV0: *(int*)data = E1CLK_RECEIVE_CHAN0; break;
1518 case GSYN_RCV1: *(int*)data = E1CLK_RECEIVE_CHAN1; break;
1519 case GSYN_RCV2: *(int*)data = E1CLK_RECEIVE_CHAN2; break;
1520 case GSYN_RCV3: *(int*)data = E1CLK_RECEIVE_CHAN3; break;
1525 CP_DEBUG2 (d, ("ioctl: setclk\n"));
1526 /* Only for superuser! */
1530 if (c->type != T_E1 &&
1531 c->type != T_G703 &&
1538 switch (*(int*)data) {
1539 default: cp_set_gsyn (c, GSYN_INT); break;
1540 case E1CLK_RECEIVE: cp_set_gsyn (c, GSYN_RCV); break;
1541 case E1CLK_RECEIVE_CHAN0: cp_set_gsyn (c, GSYN_RCV0); break;
1542 case E1CLK_RECEIVE_CHAN1: cp_set_gsyn (c, GSYN_RCV1); break;
1543 case E1CLK_RECEIVE_CHAN2: cp_set_gsyn (c, GSYN_RCV2); break;
1544 case E1CLK_RECEIVE_CHAN3: cp_set_gsyn (c, GSYN_RCV3); break;
1550 case SERIAL_GETTIMESLOTS:
1551 CP_DEBUG2 (d, ("ioctl: gettimeslots\n"));
1552 if ((c->type != T_E1 || c->unfram) && c->type != T_DATA)
1554 *(u_long*)data = c->ts;
1557 case SERIAL_SETTIMESLOTS:
1558 CP_DEBUG2 (d, ("ioctl: settimeslots\n"));
1559 /* Only for superuser! */
1563 if ((c->type != T_E1 || c->unfram) && c->type != T_DATA)
1567 cp_set_ts (c, *(u_long*)data);
1572 case SERIAL_GETINVCLK:
1573 CP_DEBUG2 (d, ("ioctl: getinvclk\n"));
1577 if (c->type != T_SERIAL)
1579 *(int*)data = c->invtxc;
1583 case SERIAL_SETINVCLK:
1584 CP_DEBUG2 (d, ("ioctl: setinvclk\n"));
1585 /* Only for superuser! */
1589 if (c->type != T_SERIAL)
1593 cp_set_invtxc (c, *(int*)data);
1594 cp_set_invrxc (c, *(int*)data);
1599 case SERIAL_GETINVTCLK:
1600 CP_DEBUG2 (d, ("ioctl: getinvtclk\n"));
1601 if (c->type != T_SERIAL)
1603 *(int*)data = c->invtxc;
1606 case SERIAL_SETINVTCLK:
1607 CP_DEBUG2 (d, ("ioctl: setinvtclk\n"));
1608 /* Only for superuser! */
1612 if (c->type != T_SERIAL)
1616 cp_set_invtxc (c, *(int*)data);
1621 case SERIAL_GETINVRCLK:
1622 CP_DEBUG2 (d, ("ioctl: getinvrclk\n"));
1623 if (c->type != T_SERIAL)
1625 *(int*)data = c->invrxc;
1628 case SERIAL_SETINVRCLK:
1629 CP_DEBUG2 (d, ("ioctl: setinvrclk\n"));
1630 /* Only for superuser! */
1634 if (c->type != T_SERIAL)
1638 cp_set_invrxc (c, *(int*)data);
1643 case SERIAL_GETLEVEL:
1644 CP_DEBUG2 (d, ("ioctl: getlevel\n"));
1645 if (c->type != T_G703)
1649 *(int*)data = cp_get_lq (c);
1656 CP_DEBUG2 (d, ("ioctl: reset\n"));
1657 /* Only for superuser! */
1663 cp_reset (c->board, 0, 0);
1668 case SERIAL_HARDRESET:
1669 CP_DEBUG2 (d, ("ioctl: hardreset\n"));
1670 /* Only for superuser! */
1676 /* hard_reset (c->board); */
1682 case SERIAL_GETCABLE:
1683 CP_DEBUG2 (d, ("ioctl: getcable\n"));
1684 if (c->type != T_SERIAL)
1688 *(int*)data = cp_get_cable (c);
1694 CP_DEBUG2 (d, ("ioctl: getdir\n"));
1695 if (c->type != T_E1 && c->type != T_DATA)
1697 *(int*)data = c->dir;
1701 CP_DEBUG2 (d, ("ioctl: setdir\n"));
1702 /* Only for superuser! */
1708 cp_set_dir (c, *(int*)data);
1713 case SERIAL_GETRLOOP:
1714 CP_DEBUG2 (d, ("ioctl: getrloop\n"));
1715 if (c->type != T_G703 &&
1720 *(int*)data = cp_get_rloop (c);
1723 case SERIAL_SETRLOOP:
1724 CP_DEBUG2 (d, ("ioctl: setloop\n"));
1725 if (c->type != T_E3 && c->type != T_T3 && c->type != T_STS1)
1727 /* Only for superuser! */
1733 cp_set_rloop (c, *(int*)data);
1738 case SERIAL_GETCABLEN:
1739 CP_DEBUG2 (d, ("ioctl: getcablen\n"));
1740 if (c->type != T_T3 && c->type != T_STS1)
1742 *(int*)data = c->cablen;
1745 case SERIAL_SETCABLEN:
1746 CP_DEBUG2 (d, ("ioctl: setloop\n"));
1747 if (c->type != T_T3 && c->type != T_STS1)
1749 /* Only for superuser! */
1755 cp_set_cablen (c, *(int*)data);
1760 case TIOCSDTR: /* Set DTR */
1768 case TIOCCDTR: /* Clear DTR */
1776 case TIOCMSET: /* Set DTR/RTS */
1779 cp_set_dtr (c, (*(int*)data & TIOCM_DTR) ? 1 : 0);
1780 cp_set_rts (c, (*(int*)data & TIOCM_RTS) ? 1 : 0);
1785 case TIOCMBIS: /* Add DTR/RTS */
1788 if (*(int*)data & TIOCM_DTR) cp_set_dtr (c, 1);
1789 if (*(int*)data & TIOCM_RTS) cp_set_rts (c, 1);
1794 case TIOCMBIC: /* Clear DTR/RTS */
1797 if (*(int*)data & TIOCM_DTR) cp_set_dtr (c, 0);
1798 if (*(int*)data & TIOCM_RTS) cp_set_rts (c, 0);
1803 case TIOCMGET: /* Get modem status */
1804 *(int*)data = cp_modem_status (c);
1810 static struct cdevsw cp_cdevsw = {
1811 .d_version = D_VERSION,
1813 .d_close = cp_close,
1814 .d_ioctl = cp_ioctl,
1816 .d_flags = D_NEEDGIANT,
1820 static int ng_cp_constructor (node_p node)
1822 drv_t *d = NG_NODE_PRIVATE (node);
1823 CP_DEBUG (d, ("Constructor\n"));
1827 static int ng_cp_newhook (node_p node, hook_p hook, const char *name)
1830 drv_t *d = NG_NODE_PRIVATE (node);
1831 bdrv_t *bd = d->board->sys;
1833 CP_DEBUG (d, ("Newhook\n"));
1834 /* Attach debug hook */
1835 if (strcmp (name, NG_CP_HOOK_DEBUG) == 0) {
1836 NG_HOOK_SET_PRIVATE (hook, NULL);
1837 d->debug_hook = hook;
1841 /* Check for raw hook */
1842 if (strcmp (name, NG_CP_HOOK_RAW) != 0)
1845 NG_HOOK_SET_PRIVATE (hook, d);
1855 static char *format_timeslots (u_long s)
1857 static char buf [100];
1861 for (i=1; i<32; ++i)
1863 int prev = (i > 1) & (s >> (i-1));
1864 int next = (i < 31) & (s >> (i+1));
1874 *p++ = '0' + i / 10;
1875 *p++ = '0' + i % 10;
1881 static int print_modems (char *s, cp_chan_t *c, int need_header)
1883 int status = cp_modem_status (c);
1887 length += sprintf (s + length, " LE DTR DSR RTS CTS CD\n");
1888 length += sprintf (s + length, "%4s %4s %4s %4s %4s %4s\n",
1889 status & TIOCM_LE ? "On" : "-",
1890 status & TIOCM_DTR ? "On" : "-",
1891 status & TIOCM_DSR ? "On" : "-",
1892 status & TIOCM_RTS ? "On" : "-",
1893 status & TIOCM_CTS ? "On" : "-",
1894 status & TIOCM_CD ? "On" : "-");
1898 static int print_stats (char *s, cp_chan_t *c, int need_header)
1903 length += sprintf (s + length, " Rintr Tintr Mintr Ibytes Ipkts Ierrs Obytes Opkts Oerrs\n");
1904 length += sprintf (s + length, "%7ld %7ld %7ld %8lu %7ld %7ld %8lu %7ld %7ld\n",
1905 c->rintr, c->tintr, 0l, (unsigned long) c->ibytes,
1906 c->ipkts, c->overrun + c->frame + c->crc,
1907 (unsigned long) c->obytes, c->opkts, c->underrun);
1911 static char *format_e1_status (u_char status)
1913 static char buf [80];
1915 if (status & E1_NOALARM)
1918 if (status & E1_LOS) strcat (buf, ",LOS");
1919 if (status & E1_AIS) strcat (buf, ",AIS");
1920 if (status & E1_LOF) strcat (buf, ",LOF");
1921 if (status & E1_LOMF) strcat (buf, ",LOMF");
1922 if (status & E1_FARLOF) strcat (buf, ",FARLOF");
1923 if (status & E1_AIS16) strcat (buf, ",AIS16");
1924 if (status & E1_FARLOMF) strcat (buf, ",FARLOMF");
1925 if (status & E1_TSTREQ) strcat (buf, ",TSTREQ");
1926 if (status & E1_TSTERR) strcat (buf, ",TSTERR");
1932 static int print_frac (char *s, int leftalign, u_long numerator, u_long divider)
1936 if (numerator < 1 || divider < 1) {
1937 length += sprintf (s+length, leftalign ? "/- " : " -");
1940 n = (int) (0.5 + 1000.0 * numerator / divider);
1942 length += sprintf (s+length, leftalign ? "/.%-3d" : " .%03d", n);
1945 *(s + length) = leftalign ? '/' : ' ';
1948 if (n >= 1000000) n = (n+500) / 1000 * 1000;
1949 else if (n >= 100000) n = (n+50) / 100 * 100;
1950 else if (n >= 10000) n = (n+5) / 10 * 10;
1953 case 1000: length += printf (s+length, ".999"); return length;
1954 case 10000: n = 9990; break;
1955 case 100000: n = 99900; break;
1956 case 1000000: n = 999000; break;
1958 if (n < 10000) length += sprintf (s+length, "%d.%d", n/1000, n/10%100);
1959 else if (n < 100000) length += sprintf (s+length, "%d.%d", n/1000, n/100%10);
1960 else if (n < 1000000) length += sprintf (s+length, "%d.", n/1000);
1961 else length += sprintf (s+length, "%d", n/1000);
1966 static int print_e1_stats (char *s, cp_chan_t *c)
1968 struct e1_counters total;
1972 totsec = c->totsec + c->cursec;
1973 total.bpv = c->total.bpv + c->currnt.bpv;
1974 total.fse = c->total.fse + c->currnt.fse;
1975 total.crce = c->total.crce + c->currnt.crce;
1976 total.rcrce = c->total.rcrce + c->currnt.rcrce;
1977 total.uas = c->total.uas + c->currnt.uas;
1978 total.les = c->total.les + c->currnt.les;
1979 total.es = c->total.es + c->currnt.es;
1980 total.bes = c->total.bes + c->currnt.bes;
1981 total.ses = c->total.ses + c->currnt.ses;
1982 total.oofs = c->total.oofs + c->currnt.oofs;
1983 total.css = c->total.css + c->currnt.css;
1984 total.dm = c->total.dm + c->currnt.dm;
1986 length += sprintf (s + length, " Unav/Degr Bpv/Fsyn CRC/RCRC Err/Lerr Sev/Bur Oof/Slp Status\n");
1988 /* Unavailable seconds, degraded minutes */
1989 length += print_frac (s + length, 0, c->currnt.uas, c->cursec);
1990 length += print_frac (s + length, 1, 60 * c->currnt.dm, c->cursec);
1992 /* Bipolar violations, frame sync errors */
1993 length += print_frac (s + length, 0, c->currnt.bpv, c->cursec);
1994 length += print_frac (s + length, 1, c->currnt.fse, c->cursec);
1996 /* CRC errors, remote CRC errors (E-bit) */
1997 length += print_frac (s + length, 0, c->currnt.crce, c->cursec);
1998 length += print_frac (s + length, 1, c->currnt.rcrce, c->cursec);
2000 /* Errored seconds, line errored seconds */
2001 length += print_frac (s + length, 0, c->currnt.es, c->cursec);
2002 length += print_frac (s + length, 1, c->currnt.les, c->cursec);
2004 /* Severely errored seconds, burst errored seconds */
2005 length += print_frac (s + length, 0, c->currnt.ses, c->cursec);
2006 length += print_frac (s + length, 1, c->currnt.bes, c->cursec);
2008 /* Out of frame seconds, controlled slip seconds */
2009 length += print_frac (s + length, 0, c->currnt.oofs, c->cursec);
2010 length += print_frac (s + length, 1, c->currnt.css, c->cursec);
2012 length += sprintf (s + length, " %s\n", format_e1_status (c->status));
2014 /* Print total statistics. */
2015 length += print_frac (s + length, 0, total.uas, totsec);
2016 length += print_frac (s + length, 1, 60 * total.dm, totsec);
2018 length += print_frac (s + length, 0, total.bpv, totsec);
2019 length += print_frac (s + length, 1, total.fse, totsec);
2021 length += print_frac (s + length, 0, total.crce, totsec);
2022 length += print_frac (s + length, 1, total.rcrce, totsec);
2024 length += print_frac (s + length, 0, total.es, totsec);
2025 length += print_frac (s + length, 1, total.les, totsec);
2027 length += print_frac (s + length, 0, total.ses, totsec);
2028 length += print_frac (s + length, 1, total.bes, totsec);
2030 length += print_frac (s + length, 0, total.oofs, totsec);
2031 length += print_frac (s + length, 1, total.css, totsec);
2033 length += sprintf (s + length, " -- Total\n");
2037 static int print_chan (char *s, cp_chan_t *c)
2040 bdrv_t *bd = d->board->sys;
2043 length += sprintf (s + length, "cp%d", c->board->num * NCHAN + c->num);
2045 length += sprintf (s + length, " debug=%d", d->chan->debug);
2047 if (c->board->mux) {
2048 length += sprintf (s + length, " cfg=C");
2050 length += sprintf (s + length, " cfg=A");
2054 length += sprintf (s + length, " %ld", c->baud);
2056 length += sprintf (s + length, " extclock");
2058 if (c->type == T_E1 || c->type == T_G703)
2060 case GSYN_INT : length += sprintf (s + length, " syn=int"); break;
2061 case GSYN_RCV : length += sprintf (s + length, " syn=rcv"); break;
2062 case GSYN_RCV0 : length += sprintf (s + length, " syn=rcv0"); break;
2063 case GSYN_RCV1 : length += sprintf (s + length, " syn=rcv1"); break;
2064 case GSYN_RCV2 : length += sprintf (s + length, " syn=rcv2"); break;
2065 case GSYN_RCV3 : length += sprintf (s + length, " syn=rcv3"); break;
2067 if (c->type == T_SERIAL) {
2068 length += sprintf (s + length, " dpll=%s", c->dpll ? "on" : "off");
2069 length += sprintf (s + length, " nrzi=%s", c->nrzi ? "on" : "off");
2070 length += sprintf (s + length, " invclk=%s", c->invtxc ? "on" : "off");
2072 if (c->type == T_E1)
2073 length += sprintf (s + length, " higain=%s", c->higain ? "on" : "off");
2075 length += sprintf (s + length, " loop=%s", c->lloop ? "on" : "off");
2077 if (c->type == T_E1)
2078 length += sprintf (s + length, " ts=%s", format_timeslots (c->ts));
2079 if (c->type == T_G703) {
2087 length += sprintf (s + length, " (level=-%.1fdB)", lq / 10.0);
2089 length += sprintf (s + length, "\n");
2093 static int ng_cp_rcvmsg (node_p node, item_p item, hook_p lasthook)
2095 drv_t *d = NG_NODE_PRIVATE (node);
2096 struct ng_mesg *msg;
2097 struct ng_mesg *resp = NULL;
2100 CP_DEBUG (d, ("Rcvmsg\n"));
2101 NGI_GET_MSG (item, msg);
2102 switch (msg->header.typecookie) {
2108 printf ("Not implemented yet\n");
2112 case NGM_GENERIC_COOKIE:
2113 switch (msg->header.cmd) {
2118 case NGM_TEXT_STATUS: {
2121 int dl = sizeof (struct ng_mesg) + 730;
2123 NG_MKRESPONSE (resp, msg, dl, M_NOWAIT);
2130 l += print_chan (s + l, d->chan);
2131 l += print_stats (s + l, d->chan, 1);
2132 l += print_modems (s + l, d->chan, 1);
2133 l += print_e1_stats (s + l, d->chan);
2135 l += sprintf (s + l, "Error: node not connect to channel");
2136 strncpy ((resp)->header.cmdstr, "status", NG_CMDSTRLEN);
2142 NG_RESPOND_MSG (error, node, item, resp);
2147 static int ng_cp_rcvdata (hook_p hook, item_p item)
2149 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE(hook));
2151 struct ng_tag_prio *ptag;
2152 bdrv_t *bd = d->board->sys;
2156 CP_DEBUG2 (d, ("Rcvdata\n"));
2157 NGI_GET_M (item, m);
2158 NG_FREE_ITEM (item);
2159 if (! NG_HOOK_PRIVATE (hook) || ! d) {
2164 /* Check for high priority data */
2165 if ((ptag = (struct ng_tag_prio *)m_tag_locate(m, NGM_GENERIC_COOKIE,
2166 NG_TAG_PRIO, NULL)) != NULL && (ptag->priority > NG_PRIO_CUTOFF) )
2174 if (_IF_QFULL (q)) {
2190 static int ng_cp_rmnode (node_p node)
2192 drv_t *d = NG_NODE_PRIVATE (node);
2194 CP_DEBUG (d, ("Rmnode\n"));
2195 if (d && d->running) {
2196 bdrv_t *bd = d->board->sys;
2204 if (node->nd_flags & NGF_REALLY_DIE) {
2205 NG_NODE_SET_PRIVATE (node, NULL);
2206 NG_NODE_UNREF (node);
2208 NG_NODE_REVIVE(node); /* Persistant node */
2213 static void ng_cp_watchdog (void *arg)
2218 if (d->timeout == 1)
2222 callout_reset (&d->timeout_handle, hz, ng_cp_watchdog, d);
2226 static int ng_cp_connect (hook_p hook)
2228 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2231 CP_DEBUG (d, ("Connect\n"));
2232 callout_reset (&d->timeout_handle, hz, ng_cp_watchdog, d);
2238 static int ng_cp_disconnect (hook_p hook)
2240 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2243 CP_DEBUG (d, ("Disconnect\n"));
2244 if (NG_HOOK_PRIVATE (hook))
2246 bdrv_t *bd = d->board->sys;
2253 /* If we were wait it than it reasserted now, just stop it. */
2254 if (!callout_drain (&d->timeout_handle))
2255 callout_stop (&d->timeout_handle);
2261 static int cp_modevent (module_t mod, int type, void *unused)
2263 static int load_count = 0;
2265 if (!debug_mpsafenet && cp_mpsafenet) {
2266 printf ("WORNING! Network stack is not MPSAFE. "
2267 "Turning off debug.cp.mpsafenet.\n");
2271 cp_cdevsw.d_flags &= ~D_NEEDGIANT;
2276 if (ng_newtype (&typestruct))
2277 printf ("Failed to register ng_cp\n");
2280 callout_init (&timeout_handle, cp_mpsafenet?CALLOUT_MPSAFE:0);
2281 callout_reset (&timeout_handle, hz*5, cp_timeout, 0);
2284 if (load_count == 1) {
2285 printf ("Removing device entry for Tau-PCI\n");
2287 ng_rmtype (&typestruct);
2290 /* If we were wait it than it reasserted now, just stop it.
2291 * Actually we shouldn't get this condition. But code could be
2292 * changed in the future, so just be a litle paranoid.
2294 if (!callout_drain (&timeout_handle))
2295 callout_stop (&timeout_handle);
2305 static struct ng_type typestruct = {
2306 .version = NG_ABI_VERSION,
2307 .name = NG_CP_NODE_TYPE,
2308 .constructor = ng_cp_constructor,
2309 .rcvmsg = ng_cp_rcvmsg,
2310 .shutdown = ng_cp_rmnode,
2311 .newhook = ng_cp_newhook,
2312 .connect = ng_cp_connect,
2313 .rcvdata = ng_cp_rcvdata,
2314 .disconnect = ng_cp_disconnect,
2319 MODULE_DEPEND (ng_cp, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION);
2321 MODULE_DEPEND (cp, sppp, 1, 1, 1);
2323 DRIVER_MODULE (cp, pci, cp_driver, cp_devclass, cp_modevent, NULL);
2324 MODULE_VERSION (cp, 1);