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>
37 #include <sys/socket.h>
38 #include <sys/sockio.h>
39 #include <sys/sysctl.h>
45 #include <dev/pci/pcivar.h>
46 #include <dev/pci/pcireg.h>
47 #include <machine/bus.h>
49 #include "opt_ng_cronyx.h"
50 #ifdef NETGRAPH_CRONYX
51 # include "opt_netgraph.h"
53 # error #option NETGRAPH missed from configuration
55 # include <netgraph/ng_message.h>
56 # include <netgraph/netgraph.h>
57 # include <dev/cp/ng_cp.h>
59 # include <net/if_sppp.h>
60 # include <net/if_types.h>
61 #include <dev/pci/pcivar.h>
62 # define PP_CISCO IFF_LINK2
65 #include <dev/cx/machdep.h>
66 #include <dev/cp/cpddk.h>
67 #include <machine/cserial.h>
68 #include <machine/resource.h>
69 #include <machine/pmap.h>
71 /* If we don't have Cronyx's sppp version, we don't have fr support via sppp */
76 #define CP_DEBUG(d,s) ({if (d->chan->debug) {\
77 printf ("%s: ", d->name); printf s;}})
78 #define CP_DEBUG2(d,s) ({if (d->chan->debug>1) {\
79 printf ("%s: ", d->name); printf s;}})
80 #define CP_LOCK_NAME "cpX"
82 static int cp_mpsafenet = 1;
83 TUNABLE_INT("debug.cp.mpsafenet", &cp_mpsafenet);
84 SYSCTL_NODE(_debug, OID_AUTO, cp, CTLFLAG_RD, 0, "Cronyx Tau-PCI Adapters");
85 SYSCTL_INT(_debug_cp, OID_AUTO, mpsafenet, CTLFLAG_RD, &cp_mpsafenet, 0,
86 "Enable/disable MPSAFE network support for Cronyx Tau-PCI Adapters");
88 #define CP_LOCK(_bd) do { \
90 mtx_lock (&(_bd)->cp_mtx); \
92 #define CP_UNLOCK(_bd) do { \
94 mtx_unlock (&(_bd)->cp_mtx); \
97 #define CP_LOCK_ASSERT(_bd) do { \
99 mtx_assert (&(_bd)->cp_mtx, MA_OWNED); \
102 static int cp_probe __P((device_t));
103 static int cp_attach __P((device_t));
104 static int cp_detach __P((device_t));
106 static device_method_t cp_methods[] = {
107 /* Device interface */
108 DEVMETHOD(device_probe, cp_probe),
109 DEVMETHOD(device_attach, cp_attach),
110 DEVMETHOD(device_detach, cp_detach),
115 typedef struct _cp_dma_mem_t {
123 typedef struct _drv_t {
130 char nodename [NG_NODELEN+1];
134 struct ifqueue queue;
135 struct ifqueue hi_queue;
137 struct callout timeout_handle;
139 struct ifqueue queue;
145 typedef struct _bdrv_t {
147 struct resource *cp_res;
148 struct resource *cp_irq;
151 drv_t channel [NCHAN];
155 static driver_t cp_driver = {
161 static devclass_t cp_devclass;
163 static void cp_receive (cp_chan_t *c, unsigned char *data, int len);
164 static void cp_transmit (cp_chan_t *c, void *attachment, int len);
165 static void cp_error (cp_chan_t *c, int data);
166 static void cp_up (drv_t *d);
167 static void cp_start (drv_t *d);
168 static void cp_down (drv_t *d);
169 static void cp_watchdog (drv_t *d);
171 extern struct ng_type typestruct;
173 static void cp_ifstart (struct ifnet *ifp);
174 static void cp_tlf (struct sppp *sp);
175 static void cp_tls (struct sppp *sp);
176 static void cp_ifwatchdog (struct ifnet *ifp);
177 static int cp_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data);
178 static void cp_initialize (void *softc);
181 static cp_board_t *adapter [NBRD];
182 static drv_t *channel [NBRD*NCHAN];
183 static struct callout led_timo [NBRD];
184 static struct callout timeout_handle;
186 static int cp_destroy = 0;
188 static int cp_open (struct cdev *dev, int oflags, int devtype, struct thread *td);
189 static int cp_close (struct cdev *dev, int fflag, int devtype, struct thread *td);
190 static int cp_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td);
191 static struct cdevsw cp_cdevsw = {
192 .d_version = D_VERSION,
197 .d_flags = D_NEEDGIANT,
201 * Print the mbuf chain, for debug purposes only.
203 static void printmbuf (struct mbuf *m)
206 for (; m; m=m->m_next) {
207 if (m->m_flags & M_PKTHDR)
208 printf (" HDR %d:", m->m_pkthdr.len);
209 if (m->m_flags & M_EXT)
211 printf (" %d", m->m_len);
217 * Make an mbuf from data.
219 static struct mbuf *makembuf (void *buf, unsigned len)
223 MGETHDR (m, M_DONTWAIT, MT_DATA);
226 MCLGET (m, M_DONTWAIT);
227 if (! (m->m_flags & M_EXT)) {
231 m->m_pkthdr.len = m->m_len = len;
232 bcopy (buf, mtod (m, caddr_t), len);
236 static int cp_probe (device_t dev)
238 if ((pci_get_vendor (dev) == cp_vendor_id) &&
239 (pci_get_device (dev) == cp_device_id)) {
240 device_set_desc (dev, "Cronyx-Tau-PCI serial adapter");
241 return BUS_PROBE_DEFAULT;
246 static void cp_timeout (void *arg)
251 for (i = 0; i < NBRD; ++i) {
252 if (adapter[i] == NULL)
254 for (k = 0; k < NCHAN; ++k) {
260 d = channel[i * NCHAN + k];
265 CP_LOCK ((bdrv_t *)d->board->sys);
266 switch (d->chan->type) {
268 cp_g703_timer (d->chan);
271 cp_e1_timer (d->chan);
276 cp_e3_timer (d->chan);
281 CP_UNLOCK ((bdrv_t *)d->board->sys);
287 callout_reset (&timeout_handle, hz, cp_timeout, 0);
291 static void cp_led_off (void *arg)
294 bdrv_t *bd = (bdrv_t *) b->sys;
307 static void cp_intr (void *arg)
310 cp_board_t *b = bd->board;
320 /* Check if we are ready */
321 if (b->sys == NULL) {
322 /* Not we are not, just cleanup. */
323 cp_interrupt_poll (b, 1);
332 /* Turn LED off 50 msec later. */
333 callout_reset (&led_timo[b->num], hz/20, cp_led_off, b);
338 /* Pass packets in a lock-free state */
339 for (i = 0; i < NCHAN && b->chan[i].type; i++) {
340 drv_t *d = b->chan[i].sys;
342 if (!d || !d->running)
344 while (_IF_QLEN(&d->queue)) {
345 IF_DEQUEUE (&d->queue,m);
348 sppp_input (d->ifp, m);
355 cp_bus_dmamap_addr (void *arg, bus_dma_segment_t *segs, int nseg, int error)
362 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
364 *addr = segs->ds_addr;
368 cp_bus_dma_mem_alloc (int bnum, int cnum, cp_dma_mem_t *dmem)
372 error = bus_dma_tag_create (NULL, 16, 0, BUS_SPACE_MAXADDR_32BIT,
373 BUS_SPACE_MAXADDR, NULL, NULL, dmem->size, 1,
374 dmem->size, 0, NULL, NULL, &dmem->dmat);
376 if (cnum >= 0) printf ("cp%d-%d: ", bnum, cnum);
377 else printf ("cp%d: ", bnum);
378 printf ("couldn't allocate tag for dma memory\n");
381 error = bus_dmamem_alloc (dmem->dmat, (void **)&dmem->virt,
382 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &dmem->mapp);
384 if (cnum >= 0) printf ("cp%d-%d: ", bnum, cnum);
385 else printf ("cp%d: ", bnum);
386 printf ("couldn't allocate mem for dma memory\n");
387 bus_dma_tag_destroy (dmem->dmat);
390 error = bus_dmamap_load (dmem->dmat, dmem->mapp, dmem->virt,
391 dmem->size, cp_bus_dmamap_addr, &dmem->phys, 0);
393 if (cnum >= 0) printf ("cp%d-%d: ", bnum, cnum);
394 else printf ("cp%d: ", bnum);
395 printf ("couldn't load mem map for dma memory\n");
396 bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
397 bus_dma_tag_destroy (dmem->dmat);
404 cp_bus_dma_mem_free (cp_dma_mem_t *dmem)
406 bus_dmamap_unload (dmem->dmat, dmem->mapp);
407 bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
408 bus_dma_tag_destroy (dmem->dmat);
412 * Called if the probe succeeded.
414 static int cp_attach (device_t dev)
416 bdrv_t *bd = device_get_softc (dev);
417 int unit = device_get_unit (dev);
418 char *cp_ln = CP_LOCK_NAME;
427 b = malloc (sizeof(cp_board_t), M_DEVBUF, M_WAITOK);
429 printf ("cp%d: couldn't allocate memory\n", unit);
433 bzero (b, sizeof(cp_board_t));
437 bd->cp_res = bus_alloc_resource (dev, SYS_RES_MEMORY, &rid,
438 0, ~0, 1, RF_ACTIVE);
440 printf ("cp%d: cannot map memory\n", unit);
445 vbase = (vm_offset_t) rman_get_virtual (bd->cp_res);
447 cp_ln[2] = '0' + unit;
448 mtx_init (&bd->cp_mtx, cp_ln, MTX_NETWORK_LOCK, MTX_DEF|MTX_RECURSE);
449 res = cp_init (b, unit, (u_char*) vbase);
451 printf ("cp%d: can't init, error code:%x\n", unit, res);
452 bus_release_resource (dev, SYS_RES_MEMORY, PCIR_BAR(0), bd->cp_res);
458 bd->dmamem.size = sizeof(cp_qbuf_t);
459 if (! cp_bus_dma_mem_alloc (unit, -1, &bd->dmamem)) {
465 cp_reset (b, bd->dmamem.virt, bd->dmamem.phys);
469 bd->cp_irq = bus_alloc_resource (dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
470 RF_SHAREABLE | RF_ACTIVE);
473 printf ("cp%d: cannot map interrupt\n", unit);
474 bus_release_resource (dev, SYS_RES_MEMORY,
475 PCIR_BAR(0), bd->cp_res);
476 mtx_destroy (&bd->cp_mtx);
481 callout_init (&led_timo[unit], cp_mpsafenet ? CALLOUT_MPSAFE : 0);
482 error = bus_setup_intr (dev, bd->cp_irq,
483 INTR_TYPE_NET|(cp_mpsafenet?INTR_MPSAFE:0),
484 NULL, cp_intr, bd, &bd->cp_intrhand);
487 printf ("cp%d: cannot set up irq\n", unit);
488 bus_release_resource (dev, SYS_RES_IRQ, 0, bd->cp_irq);
489 bus_release_resource (dev, SYS_RES_MEMORY,
490 PCIR_BAR(0), bd->cp_res);
491 mtx_destroy (&bd->cp_mtx);
496 printf ("cp%d: %s, clock %ld MHz\n", unit, b->name, b->osc / 1000000);
498 for (c = b->chan; c < b->chan + NCHAN; ++c) {
501 d = &bd->channel[c->num];
502 d->dmamem.size = sizeof(cp_buf_t);
503 if (! cp_bus_dma_mem_alloc (unit, c->num, &d->dmamem))
505 channel [b->num*NCHAN + c->num] = d;
506 sprintf (d->name, "cp%d.%d", b->num, c->num);
511 if (ng_make_node_common (&typestruct, &d->node) != 0) {
512 printf ("%s: cannot make common node\n", d->name);
516 NG_NODE_SET_PRIVATE (d->node, d);
517 sprintf (d->nodename, "%s%d", NG_CP_NODE_TYPE,
518 c->board->num*NCHAN + c->num);
519 if (ng_name_node (d->node, d->nodename)) {
520 printf ("%s: cannot name node\n", d->nodename);
521 NG_NODE_UNREF (d->node);
524 d->queue.ifq_maxlen = IFQ_MAXLEN;
525 d->hi_queue.ifq_maxlen = IFQ_MAXLEN;
526 mtx_init (&d->queue.ifq_mtx, "cp_queue", NULL, MTX_DEF);
527 mtx_init (&d->hi_queue.ifq_mtx, "cp_queue_hi", NULL, MTX_DEF);
528 callout_init (&d->timeout_handle,
529 cp_mpsafenet ? CALLOUT_MPSAFE : 0);
531 d->ifp = if_alloc(IFT_PPP);
532 if (d->ifp == NULL) {
533 printf ("%s: cannot if_alloc() interface\n", d->name);
536 d->ifp->if_softc = d;
537 if_initname (d->ifp, "cp", b->num * NCHAN + c->num);
538 d->ifp->if_mtu = PP_MTU;
539 d->ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
541 d->ifp->if_flags |= IFF_NEEDSGIANT;
542 d->ifp->if_ioctl = cp_sioctl;
543 d->ifp->if_start = cp_ifstart;
544 d->ifp->if_watchdog = cp_ifwatchdog;
545 d->ifp->if_init = cp_initialize;
546 d->queue.ifq_maxlen = NRBUF;
547 mtx_init (&d->queue.ifq_mtx, "cp_queue", NULL, MTX_DEF);
548 sppp_attach (d->ifp);
550 IFP2SP(d->ifp)->pp_tlf = cp_tlf;
551 IFP2SP(d->ifp)->pp_tls = cp_tls;
552 /* If BPF is in the kernel, call the attach for it.
553 * The header size of PPP or Cisco/HDLC is 4 bytes. */
554 bpfattach (d->ifp, DLT_PPP, 4);
557 cp_start_chan (c, 1, 1, d->dmamem.virt, d->dmamem.phys);
559 /* Register callback functions. */
560 cp_register_transmit (c, &cp_transmit);
561 cp_register_receive (c, &cp_receive);
562 cp_register_error (c, &cp_error);
563 d->devt = make_dev (&cp_cdevsw, b->num*NCHAN+c->num, UID_ROOT,
564 GID_WHEEL, 0600, "cp%d", b->num*NCHAN+c->num);
574 static int cp_detach (device_t dev)
576 bdrv_t *bd = device_get_softc (dev);
577 cp_board_t *b = bd->board;
581 KASSERT (mtx_initialized (&bd->cp_mtx), ("cp mutex not initialized"));
584 /* Check if the device is busy (open). */
585 for (c = b->chan; c < b->chan + NCHAN; ++c) {
586 drv_t *d = (drv_t*) c->sys;
588 if (! d || ! d->chan->type)
597 /* Ok, we can unload driver */
598 /* At first we should stop all channels */
599 for (c = b->chan; c < b->chan + NCHAN; ++c) {
600 drv_t *d = (drv_t*) c->sys;
602 if (! d || ! d->chan->type)
607 cp_set_dtr (d->chan, 0);
608 cp_set_rts (d->chan, 0);
611 /* Reset the adapter. */
613 cp_interrupt_poll (b, 1);
616 callout_stop (&led_timo[b->num]);
618 /* Disable the interrupt request. */
619 bus_teardown_intr (dev, bd->cp_irq, bd->cp_intrhand);
621 for (c=b->chan; c<b->chan+NCHAN; ++c) {
622 drv_t *d = (drv_t*) c->sys;
624 if (! d || ! d->chan->type)
627 /* Detach from the packet filter list of interfaces. */
630 /* Detach from the sync PPP list. */
631 sppp_detach (d->ifp);
633 /* Detach from the system list of interfaces. */
636 IF_DRAIN (&d->queue);
637 mtx_destroy (&d->queue.ifq_mtx);
640 ng_rmnode_self (d->node);
641 NG_NODE_UNREF (d->node);
644 mtx_destroy (&d->queue.ifq_mtx);
645 mtx_destroy (&d->hi_queue.ifq_mtx);
647 destroy_dev (d->devt);
653 bus_release_resource (dev, SYS_RES_IRQ, 0, bd->cp_irq);
654 bus_release_resource (dev, SYS_RES_MEMORY, PCIR_BAR(0), bd->cp_res);
659 callout_drain (&led_timo[b->num]);
663 for (c = b->chan; c < b->chan + NCHAN; ++c) {
664 drv_t *d = (drv_t*) c->sys;
666 if (! d || ! d->chan->type)
668 channel [b->num*NCHAN + c->num] = 0;
669 /* Deallocate buffers. */
670 cp_bus_dma_mem_free (&d->dmamem);
672 adapter [b->num] = 0;
673 cp_bus_dma_mem_free (&bd->dmamem);
676 mtx_destroy (&bd->cp_mtx);
681 static void cp_ifstart (struct ifnet *ifp)
683 drv_t *d = ifp->if_softc;
684 bdrv_t *bd = d->board->sys;
691 static void cp_ifwatchdog (struct ifnet *ifp)
693 drv_t *d = ifp->if_softc;
698 static void cp_tlf (struct sppp *sp)
700 drv_t *d = SP2IFP(sp)->if_softc;
702 CP_DEBUG2 (d, ("cp_tlf\n"));
703 /* XXXRIK: Don't forget to protect them by LOCK, or kill them. */
704 /* cp_set_dtr (d->chan, 0);*/
705 /* cp_set_rts (d->chan, 0);*/
706 if (!(sp->pp_flags & PP_FR) && !(d->ifp->if_flags & PP_CISCO))
710 static void cp_tls (struct sppp *sp)
712 drv_t *d = SP2IFP(sp)->if_softc;
714 CP_DEBUG2 (d, ("cp_tls\n"));
715 if (!(sp->pp_flags & PP_FR) && !(d->ifp->if_flags & PP_CISCO))
720 * Process an ioctl request.
722 static int cp_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data)
724 drv_t *d = ifp->if_softc;
725 bdrv_t *bd = d->board->sys;
726 int error, s, was_up, should_be_up;
728 was_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
729 error = sppp_ioctl (ifp, cmd, data);
734 if (! (ifp->if_flags & IFF_DEBUG))
737 d->chan->debug = d->chan->debug_shadow;
740 default: CP_DEBUG2 (d, ("ioctl 0x%lx\n", cmd)); return 0;
741 case SIOCADDMULTI: CP_DEBUG2 (d, ("ioctl SIOCADDMULTI\n")); return 0;
742 case SIOCDELMULTI: CP_DEBUG2 (d, ("ioctl SIOCDELMULTI\n")); return 0;
743 case SIOCSIFFLAGS: CP_DEBUG2 (d, ("ioctl SIOCSIFFLAGS\n")); break;
744 case SIOCSIFADDR: CP_DEBUG2 (d, ("ioctl SIOCSIFADDR\n")); break;
747 /* We get here only in case of SIFFLAGS or SIFADDR. */
750 should_be_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
751 if (! was_up && should_be_up) {
752 /* Interface goes up -- start it. */
755 } else if (was_up && ! should_be_up) {
756 /* Interface is going down -- stop it. */
757 /* if ((IFP2SP(ifp)->pp_flags & PP_FR) || (ifp->if_flags & PP_CISCO))*/
760 CP_DEBUG (d, ("ioctl 0x%lx p4\n", cmd));
767 * Initialization of interface.
768 * It seems to be never called by upper level?
770 static void cp_initialize (void *softc)
774 CP_DEBUG (d, ("cp_initialize\n"));
779 * Stop the interface. Called on splimp().
781 static void cp_down (drv_t *d)
783 CP_DEBUG (d, ("cp_down\n"));
784 /* Interface is going down -- stop it. */
785 cp_set_dtr (d->chan, 0);
786 cp_set_rts (d->chan, 0);
792 * Start the interface. Called on splimp().
794 static void cp_up (drv_t *d)
796 CP_DEBUG (d, ("cp_up\n"));
797 cp_set_dtr (d->chan, 1);
798 cp_set_rts (d->chan, 1);
803 * Start output on the interface. Get another datagram to send
804 * off of the interface queue, and copy it to the interface
805 * before starting the output.
807 static void cp_send (drv_t *d)
812 CP_DEBUG2 (d, ("cp_send, tn=%d te=%d\n", d->chan->tn, d->chan->te));
814 /* No output if the interface is down. */
818 /* No output if the modem is off. */
819 if (! (d->chan->lloop || d->chan->type != T_SERIAL ||
820 cp_get_dsr (d->chan)))
823 while (cp_transmit_space (d->chan)) {
824 /* Get the packet to send. */
826 IF_DEQUEUE (&d->hi_queue, m);
828 IF_DEQUEUE (&d->queue, m);
830 m = sppp_dequeue (d->ifp);
835 BPF_MTAP (d->ifp, m);
837 len = m_length (m, NULL);
839 printf ("%s: too long packet: %d bytes: ",
841 else if (! m->m_next)
842 cp_send_packet (d->chan, (u_char*) mtod (m, caddr_t), len, 0);
844 u_char *buf = d->chan->tbuf[d->chan->te];
845 m_copydata (m, 0, len, buf);
846 cp_send_packet (d->chan, buf, len, 0);
849 /* Set up transmit timeout, if the transmit ring is not empty.*/
853 d->ifp->if_timer = 10;
857 d->ifp->if_drv_flags |= IFF_DRV_OACTIVE;
862 * Start output on the interface.
863 * Always called on splimp().
865 static void cp_start (drv_t *d)
869 cp_set_dtr (d->chan, 1);
871 cp_set_rts (d->chan, 1);
877 * Handle transmit timeouts.
878 * Recover after lost transmit interrupts.
879 * Always called on splimp().
881 static void cp_watchdog (drv_t *d)
883 bdrv_t *bd = d->board->sys;
884 CP_DEBUG (d, ("device timeout\n"));
889 cp_stop_chan (d->chan);
890 cp_stop_e1 (d->chan);
891 cp_start_e1 (d->chan);
892 cp_start_chan (d->chan, 1, 1, 0, 0);
893 cp_set_dtr (d->chan, 1);
894 cp_set_rts (d->chan, 1);
901 static void cp_transmit (cp_chan_t *c, void *attachment, int len)
908 ++d->ifp->if_opackets;
909 d->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
910 d->ifp->if_timer = 0;
915 static void cp_receive (cp_chan_t *c, unsigned char *data, int len)
926 m = makembuf (data, len);
928 CP_DEBUG (d, ("no memory for packet\n"));
930 ++d->ifp->if_iqdrops;
937 m->m_pkthdr.rcvif = 0;
938 NG_SEND_DATA_ONLY (error, d->hook, m);
940 ++d->ifp->if_ipackets;
941 m->m_pkthdr.rcvif = d->ifp;
942 /* Check if there's a BPF listener on this interface.
943 * If so, hand off the raw packet to bpf. */
944 BPF_TAP (d->ifp, data, len);
945 IF_ENQUEUE (&d->queue, m);
949 static void cp_error (cp_chan_t *c, int data)
955 CP_DEBUG (d, ("frame error\n"));
957 ++d->ifp->if_ierrors;
961 CP_DEBUG (d, ("crc error\n"));
963 ++d->ifp->if_ierrors;
967 CP_DEBUG (d, ("overrun error\n"));
969 ++d->ifp->if_collisions;
970 ++d->ifp->if_ierrors;
974 CP_DEBUG (d, ("overflow error\n"));
976 ++d->ifp->if_ierrors;
980 CP_DEBUG (d, ("underrun error\n"));
984 ++d->ifp->if_oerrors;
985 d->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
986 d->ifp->if_timer = 0;
991 CP_DEBUG (d, ("error #%d\n", data));
997 * You also need read, write, open, close routines.
998 * This should get you started
1000 static int cp_open (struct cdev *dev, int oflags, int devtype, struct thread *td)
1002 int unit = minor (dev);
1005 if (unit >= NBRD*NCHAN || ! (d = channel[unit]))
1007 CP_DEBUG2 (d, ("cp_open\n"));
1012 * Only called on the LAST close.
1014 static int cp_close (struct cdev *dev, int fflag, int devtype, struct thread *td)
1016 drv_t *d = channel [minor (dev)];
1018 CP_DEBUG2 (d, ("cp_close\n"));
1022 static int cp_modem_status (cp_chan_t *c)
1025 bdrv_t *bd = d->board->sys;
1028 status = d->running ? TIOCM_LE : 0;
1031 if (cp_get_cd (c)) status |= TIOCM_CD;
1032 if (cp_get_cts (c)) status |= TIOCM_CTS;
1033 if (cp_get_dsr (c)) status |= TIOCM_DSR;
1034 if (c->dtr) status |= TIOCM_DTR;
1035 if (c->rts) status |= TIOCM_RTS;
1041 static int cp_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
1043 drv_t *d = channel [minor (dev)];
1044 bdrv_t *bd = d->board->sys;
1045 cp_chan_t *c = d->chan;
1046 struct serial_statistics *st;
1047 struct e1_statistics *opte1;
1048 struct e3_statistics *opte3;
1053 case SERIAL_GETREGISTERED:
1054 CP_DEBUG2 (d, ("ioctl: getregistered\n"));
1055 bzero (mask, sizeof(mask));
1056 for (s=0; s<NBRD*NCHAN; ++s)
1058 mask [s/8] |= 1 << (s & 7);
1059 bcopy (mask, data, sizeof (mask));
1063 case SERIAL_GETPROTO:
1064 CP_DEBUG2 (d, ("ioctl: getproto\n"));
1065 strcpy ((char*)data, (IFP2SP(d->ifp)->pp_flags & PP_FR) ? "fr" :
1066 (d->ifp->if_flags & PP_CISCO) ? "cisco" : "ppp");
1069 case SERIAL_SETPROTO:
1070 CP_DEBUG2 (d, ("ioctl: setproto\n"));
1071 /* Only for superuser! */
1072 error = priv_check (td, PRIV_DRIVER);
1075 if (d->ifp->if_drv_flags & IFF_DRV_RUNNING)
1077 if (! strcmp ("cisco", (char*)data)) {
1078 IFP2SP(d->ifp)->pp_flags &= ~(PP_FR);
1079 IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE;
1080 d->ifp->if_flags |= PP_CISCO;
1081 } else if (! strcmp ("fr", (char*)data) && PP_FR) {
1082 d->ifp->if_flags &= ~(PP_CISCO);
1083 IFP2SP(d->ifp)->pp_flags |= PP_FR | PP_KEEPALIVE;
1084 } else if (! strcmp ("ppp", (char*)data)) {
1085 IFP2SP(d->ifp)->pp_flags &= ~PP_FR;
1086 IFP2SP(d->ifp)->pp_flags &= ~PP_KEEPALIVE;
1087 d->ifp->if_flags &= ~(PP_CISCO);
1092 case SERIAL_GETKEEPALIVE:
1093 CP_DEBUG2 (d, ("ioctl: getkeepalive\n"));
1094 if ((IFP2SP(d->ifp)->pp_flags & PP_FR) ||
1095 (d->ifp->if_flags & PP_CISCO))
1097 *(int*)data = (IFP2SP(d->ifp)->pp_flags & PP_KEEPALIVE) ? 1 : 0;
1100 case SERIAL_SETKEEPALIVE:
1101 CP_DEBUG2 (d, ("ioctl: setkeepalive\n"));
1102 /* Only for superuser! */
1103 error = priv_check (td, PRIV_DRIVER);
1106 if ((IFP2SP(d->ifp)->pp_flags & PP_FR) ||
1107 (d->ifp->if_flags & PP_CISCO))
1112 IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE;
1114 IFP2SP(d->ifp)->pp_flags &= ~PP_KEEPALIVE;
1120 case SERIAL_GETMODE:
1121 CP_DEBUG2 (d, ("ioctl: getmode\n"));
1122 *(int*)data = SERIAL_HDLC;
1125 case SERIAL_SETMODE:
1126 /* Only for superuser! */
1127 error = priv_check (td, PRIV_DRIVER);
1130 if (*(int*)data != SERIAL_HDLC)
1135 CP_DEBUG2 (d, ("ioctl: getcfg\n"));
1136 if (c->type != T_E1 || c->unfram)
1138 *(char*)data = c->board->mux ? 'c' : 'a';
1142 CP_DEBUG2 (d, ("ioctl: setcfg\n"));
1143 error = priv_check (td, PRIV_DRIVER);
1146 if (c->type != T_E1)
1150 cp_set_mux (c->board, *((char*)data) == 'c');
1155 case SERIAL_GETSTAT:
1156 CP_DEBUG2 (d, ("ioctl: getstat\n"));
1157 st = (struct serial_statistics*) data;
1158 st->rintr = c->rintr;
1159 st->tintr = c->tintr;
1161 st->ibytes = c->ibytes;
1162 st->ipkts = c->ipkts;
1163 st->obytes = c->obytes;
1164 st->opkts = c->opkts;
1165 st->ierrs = c->overrun + c->frame + c->crc;
1166 st->oerrs = c->underrun;
1169 case SERIAL_GETESTAT:
1170 CP_DEBUG2 (d, ("ioctl: getestat\n"));
1171 if (c->type != T_E1 && c->type != T_G703)
1173 opte1 = (struct e1_statistics*) data;
1174 opte1->status = c->status;
1175 opte1->cursec = c->cursec;
1176 opte1->totsec = c->totsec + c->cursec;
1178 opte1->currnt.bpv = c->currnt.bpv;
1179 opte1->currnt.fse = c->currnt.fse;
1180 opte1->currnt.crce = c->currnt.crce;
1181 opte1->currnt.rcrce = c->currnt.rcrce;
1182 opte1->currnt.uas = c->currnt.uas;
1183 opte1->currnt.les = c->currnt.les;
1184 opte1->currnt.es = c->currnt.es;
1185 opte1->currnt.bes = c->currnt.bes;
1186 opte1->currnt.ses = c->currnt.ses;
1187 opte1->currnt.oofs = c->currnt.oofs;
1188 opte1->currnt.css = c->currnt.css;
1189 opte1->currnt.dm = c->currnt.dm;
1191 opte1->total.bpv = c->total.bpv + c->currnt.bpv;
1192 opte1->total.fse = c->total.fse + c->currnt.fse;
1193 opte1->total.crce = c->total.crce + c->currnt.crce;
1194 opte1->total.rcrce = c->total.rcrce + c->currnt.rcrce;
1195 opte1->total.uas = c->total.uas + c->currnt.uas;
1196 opte1->total.les = c->total.les + c->currnt.les;
1197 opte1->total.es = c->total.es + c->currnt.es;
1198 opte1->total.bes = c->total.bes + c->currnt.bes;
1199 opte1->total.ses = c->total.ses + c->currnt.ses;
1200 opte1->total.oofs = c->total.oofs + c->currnt.oofs;
1201 opte1->total.css = c->total.css + c->currnt.css;
1202 opte1->total.dm = c->total.dm + c->currnt.dm;
1203 for (s=0; s<48; ++s) {
1204 opte1->interval[s].bpv = c->interval[s].bpv;
1205 opte1->interval[s].fse = c->interval[s].fse;
1206 opte1->interval[s].crce = c->interval[s].crce;
1207 opte1->interval[s].rcrce = c->interval[s].rcrce;
1208 opte1->interval[s].uas = c->interval[s].uas;
1209 opte1->interval[s].les = c->interval[s].les;
1210 opte1->interval[s].es = c->interval[s].es;
1211 opte1->interval[s].bes = c->interval[s].bes;
1212 opte1->interval[s].ses = c->interval[s].ses;
1213 opte1->interval[s].oofs = c->interval[s].oofs;
1214 opte1->interval[s].css = c->interval[s].css;
1215 opte1->interval[s].dm = c->interval[s].dm;
1219 case SERIAL_GETE3STAT:
1220 CP_DEBUG2 (d, ("ioctl: gete3stat\n"));
1221 if (c->type != T_E3 && c->type != T_T3 && c->type != T_STS1)
1223 opte3 = (struct e3_statistics*) data;
1225 opte3->status = c->e3status;
1226 opte3->cursec = (c->e3csec_5 * 2 + 1) / 10;
1227 opte3->totsec = c->e3tsec + opte3->cursec;
1229 opte3->ccv = c->e3ccv;
1230 opte3->tcv = c->e3tcv + opte3->ccv;
1232 for (s = 0; s < 48; ++s) {
1233 opte3->icv[s] = c->e3icv[s];
1237 case SERIAL_CLRSTAT:
1238 CP_DEBUG2 (d, ("ioctl: clrstat\n"));
1239 /* Only for superuser! */
1240 error = priv_check (td, PRIV_DRIVER);
1253 bzero (&c->currnt, sizeof (c->currnt));
1254 bzero (&c->total, sizeof (c->total));
1255 bzero (c->interval, sizeof (c->interval));
1258 bzero (c->e3icv, sizeof (c->e3icv));
1261 case SERIAL_GETBAUD:
1262 CP_DEBUG2 (d, ("ioctl: getbaud\n"));
1263 *(long*)data = c->baud;
1266 case SERIAL_SETBAUD:
1267 CP_DEBUG2 (d, ("ioctl: setbaud\n"));
1268 /* Only for superuser! */
1269 error = priv_check (td, PRIV_DRIVER);
1274 cp_set_baud (c, *(long*)data);
1279 case SERIAL_GETLOOP:
1280 CP_DEBUG2 (d, ("ioctl: getloop\n"));
1281 *(int*)data = c->lloop;
1284 case SERIAL_SETLOOP:
1285 CP_DEBUG2 (d, ("ioctl: setloop\n"));
1286 /* Only for superuser! */
1287 error = priv_check (td, PRIV_DRIVER);
1292 cp_set_lloop (c, *(int*)data);
1297 case SERIAL_GETDPLL:
1298 CP_DEBUG2 (d, ("ioctl: getdpll\n"));
1299 if (c->type != T_SERIAL)
1301 *(int*)data = c->dpll;
1304 case SERIAL_SETDPLL:
1305 CP_DEBUG2 (d, ("ioctl: setdpll\n"));
1306 /* Only for superuser! */
1307 error = priv_check (td, PRIV_DRIVER);
1310 if (c->type != T_SERIAL)
1314 cp_set_dpll (c, *(int*)data);
1319 case SERIAL_GETNRZI:
1320 CP_DEBUG2 (d, ("ioctl: getnrzi\n"));
1321 if (c->type != T_SERIAL)
1323 *(int*)data = c->nrzi;
1326 case SERIAL_SETNRZI:
1327 CP_DEBUG2 (d, ("ioctl: setnrzi\n"));
1328 /* Only for superuser! */
1329 error = priv_check (td, PRIV_DRIVER);
1332 if (c->type != T_SERIAL)
1336 cp_set_nrzi (c, *(int*)data);
1341 case SERIAL_GETDEBUG:
1342 CP_DEBUG2 (d, ("ioctl: getdebug\n"));
1343 *(int*)data = d->chan->debug;
1346 case SERIAL_SETDEBUG:
1347 CP_DEBUG2 (d, ("ioctl: setdebug\n"));
1348 /* Only for superuser! */
1349 error = priv_check (td, PRIV_DRIVER);
1354 * The debug_shadow is always greater than zero for logic
1355 * simplicity. For switching debug off the IFF_DEBUG is
1358 d->chan->debug_shadow = (*(int*)data) ? (*(int*)data) : 1;
1359 if (d->ifp->if_flags & IFF_DEBUG)
1360 d->chan->debug = d->chan->debug_shadow;
1362 d->chan->debug = *(int*)data;
1366 case SERIAL_GETHIGAIN:
1367 CP_DEBUG2 (d, ("ioctl: gethigain\n"));
1368 if (c->type != T_E1)
1370 *(int*)data = c->higain;
1373 case SERIAL_SETHIGAIN:
1374 CP_DEBUG2 (d, ("ioctl: sethigain\n"));
1375 /* Only for superuser! */
1376 error = priv_check (td, PRIV_DRIVER);
1379 if (c->type != T_E1)
1383 cp_set_higain (c, *(int*)data);
1388 case SERIAL_GETPHONY:
1389 CP_DEBUG2 (d, ("ioctl: getphony\n"));
1390 if (c->type != T_E1)
1392 *(int*)data = c->phony;
1395 case SERIAL_SETPHONY:
1396 CP_DEBUG2 (d, ("ioctl: setphony\n"));
1397 /* Only for superuser! */
1398 error = priv_check (td, PRIV_DRIVER);
1401 if (c->type != T_E1)
1405 cp_set_phony (c, *(int*)data);
1410 case SERIAL_GETUNFRAM:
1411 CP_DEBUG2 (d, ("ioctl: getunfram\n"));
1412 if (c->type != T_E1)
1414 *(int*)data = c->unfram;
1417 case SERIAL_SETUNFRAM:
1418 CP_DEBUG2 (d, ("ioctl: setunfram\n"));
1419 /* Only for superuser! */
1420 error = priv_check (td, PRIV_DRIVER);
1423 if (c->type != T_E1)
1427 cp_set_unfram (c, *(int*)data);
1432 case SERIAL_GETSCRAMBLER:
1433 CP_DEBUG2 (d, ("ioctl: getscrambler\n"));
1434 if (c->type != T_G703 && !c->unfram)
1436 *(int*)data = c->scrambler;
1439 case SERIAL_SETSCRAMBLER:
1440 CP_DEBUG2 (d, ("ioctl: setscrambler\n"));
1441 /* Only for superuser! */
1442 error = priv_check (td, PRIV_DRIVER);
1445 if (c->type != T_G703 && !c->unfram)
1449 cp_set_scrambler (c, *(int*)data);
1454 case SERIAL_GETMONITOR:
1455 CP_DEBUG2 (d, ("ioctl: getmonitor\n"));
1456 if (c->type != T_E1 &&
1461 *(int*)data = c->monitor;
1464 case SERIAL_SETMONITOR:
1465 CP_DEBUG2 (d, ("ioctl: setmonitor\n"));
1466 /* Only for superuser! */
1467 error = priv_check (td, PRIV_DRIVER);
1470 if (c->type != T_E1)
1474 cp_set_monitor (c, *(int*)data);
1479 case SERIAL_GETUSE16:
1480 CP_DEBUG2 (d, ("ioctl: getuse16\n"));
1481 if (c->type != T_E1 || c->unfram)
1483 *(int*)data = c->use16;
1486 case SERIAL_SETUSE16:
1487 CP_DEBUG2 (d, ("ioctl: setuse16\n"));
1488 /* Only for superuser! */
1489 error = priv_check (td, PRIV_DRIVER);
1492 if (c->type != T_E1)
1496 cp_set_use16 (c, *(int*)data);
1501 case SERIAL_GETCRC4:
1502 CP_DEBUG2 (d, ("ioctl: getcrc4\n"));
1503 if (c->type != T_E1 || c->unfram)
1505 *(int*)data = c->crc4;
1508 case SERIAL_SETCRC4:
1509 CP_DEBUG2 (d, ("ioctl: setcrc4\n"));
1510 /* Only for superuser! */
1511 error = priv_check (td, PRIV_DRIVER);
1514 if (c->type != T_E1)
1518 cp_set_crc4 (c, *(int*)data);
1524 CP_DEBUG2 (d, ("ioctl: getclk\n"));
1525 if (c->type != T_E1 &&
1526 c->type != T_G703 &&
1532 default: *(int*)data = E1CLK_INTERNAL; break;
1533 case GSYN_RCV: *(int*)data = E1CLK_RECEIVE; break;
1534 case GSYN_RCV0: *(int*)data = E1CLK_RECEIVE_CHAN0; break;
1535 case GSYN_RCV1: *(int*)data = E1CLK_RECEIVE_CHAN1; break;
1536 case GSYN_RCV2: *(int*)data = E1CLK_RECEIVE_CHAN2; break;
1537 case GSYN_RCV3: *(int*)data = E1CLK_RECEIVE_CHAN3; break;
1542 CP_DEBUG2 (d, ("ioctl: setclk\n"));
1543 /* Only for superuser! */
1544 error = priv_check (td, PRIV_DRIVER);
1547 if (c->type != T_E1 &&
1548 c->type != T_G703 &&
1555 switch (*(int*)data) {
1556 default: cp_set_gsyn (c, GSYN_INT); break;
1557 case E1CLK_RECEIVE: cp_set_gsyn (c, GSYN_RCV); break;
1558 case E1CLK_RECEIVE_CHAN0: cp_set_gsyn (c, GSYN_RCV0); break;
1559 case E1CLK_RECEIVE_CHAN1: cp_set_gsyn (c, GSYN_RCV1); break;
1560 case E1CLK_RECEIVE_CHAN2: cp_set_gsyn (c, GSYN_RCV2); break;
1561 case E1CLK_RECEIVE_CHAN3: cp_set_gsyn (c, GSYN_RCV3); break;
1567 case SERIAL_GETTIMESLOTS:
1568 CP_DEBUG2 (d, ("ioctl: gettimeslots\n"));
1569 if ((c->type != T_E1 || c->unfram) && c->type != T_DATA)
1571 *(u_long*)data = c->ts;
1574 case SERIAL_SETTIMESLOTS:
1575 CP_DEBUG2 (d, ("ioctl: settimeslots\n"));
1576 /* Only for superuser! */
1577 error = priv_check (td, PRIV_DRIVER);
1580 if ((c->type != T_E1 || c->unfram) && c->type != T_DATA)
1584 cp_set_ts (c, *(u_long*)data);
1589 case SERIAL_GETINVCLK:
1590 CP_DEBUG2 (d, ("ioctl: getinvclk\n"));
1594 if (c->type != T_SERIAL)
1596 *(int*)data = c->invtxc;
1600 case SERIAL_SETINVCLK:
1601 CP_DEBUG2 (d, ("ioctl: setinvclk\n"));
1602 /* Only for superuser! */
1603 error = priv_check (td, PRIV_DRIVER);
1606 if (c->type != T_SERIAL)
1610 cp_set_invtxc (c, *(int*)data);
1611 cp_set_invrxc (c, *(int*)data);
1616 case SERIAL_GETINVTCLK:
1617 CP_DEBUG2 (d, ("ioctl: getinvtclk\n"));
1618 if (c->type != T_SERIAL)
1620 *(int*)data = c->invtxc;
1623 case SERIAL_SETINVTCLK:
1624 CP_DEBUG2 (d, ("ioctl: setinvtclk\n"));
1625 /* Only for superuser! */
1626 error = priv_check (td, PRIV_DRIVER);
1629 if (c->type != T_SERIAL)
1633 cp_set_invtxc (c, *(int*)data);
1638 case SERIAL_GETINVRCLK:
1639 CP_DEBUG2 (d, ("ioctl: getinvrclk\n"));
1640 if (c->type != T_SERIAL)
1642 *(int*)data = c->invrxc;
1645 case SERIAL_SETINVRCLK:
1646 CP_DEBUG2 (d, ("ioctl: setinvrclk\n"));
1647 /* Only for superuser! */
1648 error = priv_check (td, PRIV_DRIVER);
1651 if (c->type != T_SERIAL)
1655 cp_set_invrxc (c, *(int*)data);
1660 case SERIAL_GETLEVEL:
1661 CP_DEBUG2 (d, ("ioctl: getlevel\n"));
1662 if (c->type != T_G703)
1666 *(int*)data = cp_get_lq (c);
1673 CP_DEBUG2 (d, ("ioctl: reset\n"));
1674 /* Only for superuser! */
1675 error = priv_check (td, PRIV_DRIVER);
1680 cp_reset (c->board, 0, 0);
1685 case SERIAL_HARDRESET:
1686 CP_DEBUG2 (d, ("ioctl: hardreset\n"));
1687 /* Only for superuser! */
1688 error = priv_check (td, PRIV_DRIVER);
1693 /* hard_reset (c->board); */
1699 case SERIAL_GETCABLE:
1700 CP_DEBUG2 (d, ("ioctl: getcable\n"));
1701 if (c->type != T_SERIAL)
1705 *(int*)data = cp_get_cable (c);
1711 CP_DEBUG2 (d, ("ioctl: getdir\n"));
1712 if (c->type != T_E1 && c->type != T_DATA)
1714 *(int*)data = c->dir;
1718 CP_DEBUG2 (d, ("ioctl: setdir\n"));
1719 /* Only for superuser! */
1720 error = priv_check (td, PRIV_DRIVER);
1725 cp_set_dir (c, *(int*)data);
1730 case SERIAL_GETRLOOP:
1731 CP_DEBUG2 (d, ("ioctl: getrloop\n"));
1732 if (c->type != T_G703 &&
1737 *(int*)data = cp_get_rloop (c);
1740 case SERIAL_SETRLOOP:
1741 CP_DEBUG2 (d, ("ioctl: setloop\n"));
1742 if (c->type != T_E3 && c->type != T_T3 && c->type != T_STS1)
1744 /* Only for superuser! */
1745 error = priv_check (td, PRIV_DRIVER);
1750 cp_set_rloop (c, *(int*)data);
1755 case SERIAL_GETCABLEN:
1756 CP_DEBUG2 (d, ("ioctl: getcablen\n"));
1757 if (c->type != T_T3 && c->type != T_STS1)
1759 *(int*)data = c->cablen;
1762 case SERIAL_SETCABLEN:
1763 CP_DEBUG2 (d, ("ioctl: setloop\n"));
1764 if (c->type != T_T3 && c->type != T_STS1)
1766 /* Only for superuser! */
1767 error = priv_check (td, PRIV_DRIVER);
1772 cp_set_cablen (c, *(int*)data);
1777 case TIOCSDTR: /* Set DTR */
1785 case TIOCCDTR: /* Clear DTR */
1793 case TIOCMSET: /* Set DTR/RTS */
1796 cp_set_dtr (c, (*(int*)data & TIOCM_DTR) ? 1 : 0);
1797 cp_set_rts (c, (*(int*)data & TIOCM_RTS) ? 1 : 0);
1802 case TIOCMBIS: /* Add DTR/RTS */
1805 if (*(int*)data & TIOCM_DTR) cp_set_dtr (c, 1);
1806 if (*(int*)data & TIOCM_RTS) cp_set_rts (c, 1);
1811 case TIOCMBIC: /* Clear DTR/RTS */
1814 if (*(int*)data & TIOCM_DTR) cp_set_dtr (c, 0);
1815 if (*(int*)data & TIOCM_RTS) cp_set_rts (c, 0);
1820 case TIOCMGET: /* Get modem status */
1821 *(int*)data = cp_modem_status (c);
1828 static int ng_cp_constructor (node_p node)
1830 drv_t *d = NG_NODE_PRIVATE (node);
1831 CP_DEBUG (d, ("Constructor\n"));
1835 static int ng_cp_newhook (node_p node, hook_p hook, const char *name)
1838 drv_t *d = NG_NODE_PRIVATE (node);
1839 bdrv_t *bd = d->board->sys;
1841 CP_DEBUG (d, ("Newhook\n"));
1842 /* Attach debug hook */
1843 if (strcmp (name, NG_CP_HOOK_DEBUG) == 0) {
1844 NG_HOOK_SET_PRIVATE (hook, NULL);
1845 d->debug_hook = hook;
1849 /* Check for raw hook */
1850 if (strcmp (name, NG_CP_HOOK_RAW) != 0)
1853 NG_HOOK_SET_PRIVATE (hook, d);
1863 static char *format_timeslots (u_long s)
1865 static char buf [100];
1869 for (i=1; i<32; ++i)
1871 int prev = (i > 1) & (s >> (i-1));
1872 int next = (i < 31) & (s >> (i+1));
1882 *p++ = '0' + i / 10;
1883 *p++ = '0' + i % 10;
1889 static int print_modems (char *s, cp_chan_t *c, int need_header)
1891 int status = cp_modem_status (c);
1895 length += sprintf (s + length, " LE DTR DSR RTS CTS CD\n");
1896 length += sprintf (s + length, "%4s %4s %4s %4s %4s %4s\n",
1897 status & TIOCM_LE ? "On" : "-",
1898 status & TIOCM_DTR ? "On" : "-",
1899 status & TIOCM_DSR ? "On" : "-",
1900 status & TIOCM_RTS ? "On" : "-",
1901 status & TIOCM_CTS ? "On" : "-",
1902 status & TIOCM_CD ? "On" : "-");
1906 static int print_stats (char *s, cp_chan_t *c, int need_header)
1911 length += sprintf (s + length, " Rintr Tintr Mintr Ibytes Ipkts Ierrs Obytes Opkts Oerrs\n");
1912 length += sprintf (s + length, "%7ld %7ld %7ld %8lu %7ld %7ld %8lu %7ld %7ld\n",
1913 c->rintr, c->tintr, 0l, (unsigned long) c->ibytes,
1914 c->ipkts, c->overrun + c->frame + c->crc,
1915 (unsigned long) c->obytes, c->opkts, c->underrun);
1919 static char *format_e1_status (u_char status)
1921 static char buf [80];
1923 if (status & E1_NOALARM)
1926 if (status & E1_LOS) strcat (buf, ",LOS");
1927 if (status & E1_AIS) strcat (buf, ",AIS");
1928 if (status & E1_LOF) strcat (buf, ",LOF");
1929 if (status & E1_LOMF) strcat (buf, ",LOMF");
1930 if (status & E1_FARLOF) strcat (buf, ",FARLOF");
1931 if (status & E1_AIS16) strcat (buf, ",AIS16");
1932 if (status & E1_FARLOMF) strcat (buf, ",FARLOMF");
1933 if (status & E1_TSTREQ) strcat (buf, ",TSTREQ");
1934 if (status & E1_TSTERR) strcat (buf, ",TSTERR");
1940 static int print_frac (char *s, int leftalign, u_long numerator, u_long divider)
1944 if (numerator < 1 || divider < 1) {
1945 length += sprintf (s+length, leftalign ? "/- " : " -");
1948 n = (int) (0.5 + 1000.0 * numerator / divider);
1950 length += sprintf (s+length, leftalign ? "/.%-3d" : " .%03d", n);
1953 *(s + length) = leftalign ? '/' : ' ';
1956 if (n >= 1000000) n = (n+500) / 1000 * 1000;
1957 else if (n >= 100000) n = (n+50) / 100 * 100;
1958 else if (n >= 10000) n = (n+5) / 10 * 10;
1961 case 1000: length += printf (s+length, ".999"); return length;
1962 case 10000: n = 9990; break;
1963 case 100000: n = 99900; break;
1964 case 1000000: n = 999000; break;
1966 if (n < 10000) length += sprintf (s+length, "%d.%d", n/1000, n/10%100);
1967 else if (n < 100000) length += sprintf (s+length, "%d.%d", n/1000, n/100%10);
1968 else if (n < 1000000) length += sprintf (s+length, "%d.", n/1000);
1969 else length += sprintf (s+length, "%d", n/1000);
1974 static int print_e1_stats (char *s, cp_chan_t *c)
1976 struct e1_counters total;
1980 totsec = c->totsec + c->cursec;
1981 total.bpv = c->total.bpv + c->currnt.bpv;
1982 total.fse = c->total.fse + c->currnt.fse;
1983 total.crce = c->total.crce + c->currnt.crce;
1984 total.rcrce = c->total.rcrce + c->currnt.rcrce;
1985 total.uas = c->total.uas + c->currnt.uas;
1986 total.les = c->total.les + c->currnt.les;
1987 total.es = c->total.es + c->currnt.es;
1988 total.bes = c->total.bes + c->currnt.bes;
1989 total.ses = c->total.ses + c->currnt.ses;
1990 total.oofs = c->total.oofs + c->currnt.oofs;
1991 total.css = c->total.css + c->currnt.css;
1992 total.dm = c->total.dm + c->currnt.dm;
1994 length += sprintf (s + length, " Unav/Degr Bpv/Fsyn CRC/RCRC Err/Lerr Sev/Bur Oof/Slp Status\n");
1996 /* Unavailable seconds, degraded minutes */
1997 length += print_frac (s + length, 0, c->currnt.uas, c->cursec);
1998 length += print_frac (s + length, 1, 60 * c->currnt.dm, c->cursec);
2000 /* Bipolar violations, frame sync errors */
2001 length += print_frac (s + length, 0, c->currnt.bpv, c->cursec);
2002 length += print_frac (s + length, 1, c->currnt.fse, c->cursec);
2004 /* CRC errors, remote CRC errors (E-bit) */
2005 length += print_frac (s + length, 0, c->currnt.crce, c->cursec);
2006 length += print_frac (s + length, 1, c->currnt.rcrce, c->cursec);
2008 /* Errored seconds, line errored seconds */
2009 length += print_frac (s + length, 0, c->currnt.es, c->cursec);
2010 length += print_frac (s + length, 1, c->currnt.les, c->cursec);
2012 /* Severely errored seconds, burst errored seconds */
2013 length += print_frac (s + length, 0, c->currnt.ses, c->cursec);
2014 length += print_frac (s + length, 1, c->currnt.bes, c->cursec);
2016 /* Out of frame seconds, controlled slip seconds */
2017 length += print_frac (s + length, 0, c->currnt.oofs, c->cursec);
2018 length += print_frac (s + length, 1, c->currnt.css, c->cursec);
2020 length += sprintf (s + length, " %s\n", format_e1_status (c->status));
2022 /* Print total statistics. */
2023 length += print_frac (s + length, 0, total.uas, totsec);
2024 length += print_frac (s + length, 1, 60 * total.dm, totsec);
2026 length += print_frac (s + length, 0, total.bpv, totsec);
2027 length += print_frac (s + length, 1, total.fse, totsec);
2029 length += print_frac (s + length, 0, total.crce, totsec);
2030 length += print_frac (s + length, 1, total.rcrce, totsec);
2032 length += print_frac (s + length, 0, total.es, totsec);
2033 length += print_frac (s + length, 1, total.les, totsec);
2035 length += print_frac (s + length, 0, total.ses, totsec);
2036 length += print_frac (s + length, 1, total.bes, totsec);
2038 length += print_frac (s + length, 0, total.oofs, totsec);
2039 length += print_frac (s + length, 1, total.css, totsec);
2041 length += sprintf (s + length, " -- Total\n");
2045 static int print_chan (char *s, cp_chan_t *c)
2048 bdrv_t *bd = d->board->sys;
2051 length += sprintf (s + length, "cp%d", c->board->num * NCHAN + c->num);
2053 length += sprintf (s + length, " debug=%d", d->chan->debug);
2055 if (c->board->mux) {
2056 length += sprintf (s + length, " cfg=C");
2058 length += sprintf (s + length, " cfg=A");
2062 length += sprintf (s + length, " %ld", c->baud);
2064 length += sprintf (s + length, " extclock");
2066 if (c->type == T_E1 || c->type == T_G703)
2068 case GSYN_INT : length += sprintf (s + length, " syn=int"); break;
2069 case GSYN_RCV : length += sprintf (s + length, " syn=rcv"); break;
2070 case GSYN_RCV0 : length += sprintf (s + length, " syn=rcv0"); break;
2071 case GSYN_RCV1 : length += sprintf (s + length, " syn=rcv1"); break;
2072 case GSYN_RCV2 : length += sprintf (s + length, " syn=rcv2"); break;
2073 case GSYN_RCV3 : length += sprintf (s + length, " syn=rcv3"); break;
2075 if (c->type == T_SERIAL) {
2076 length += sprintf (s + length, " dpll=%s", c->dpll ? "on" : "off");
2077 length += sprintf (s + length, " nrzi=%s", c->nrzi ? "on" : "off");
2078 length += sprintf (s + length, " invclk=%s", c->invtxc ? "on" : "off");
2080 if (c->type == T_E1)
2081 length += sprintf (s + length, " higain=%s", c->higain ? "on" : "off");
2083 length += sprintf (s + length, " loop=%s", c->lloop ? "on" : "off");
2085 if (c->type == T_E1)
2086 length += sprintf (s + length, " ts=%s", format_timeslots (c->ts));
2087 if (c->type == T_G703) {
2095 length += sprintf (s + length, " (level=-%.1fdB)", lq / 10.0);
2097 length += sprintf (s + length, "\n");
2101 static int ng_cp_rcvmsg (node_p node, item_p item, hook_p lasthook)
2103 drv_t *d = NG_NODE_PRIVATE (node);
2104 struct ng_mesg *msg;
2105 struct ng_mesg *resp = NULL;
2108 CP_DEBUG (d, ("Rcvmsg\n"));
2109 NGI_GET_MSG (item, msg);
2110 switch (msg->header.typecookie) {
2116 printf ("Not implemented yet\n");
2120 case NGM_GENERIC_COOKIE:
2121 switch (msg->header.cmd) {
2126 case NGM_TEXT_STATUS: {
2129 int dl = sizeof (struct ng_mesg) + 730;
2131 NG_MKRESPONSE (resp, msg, dl, M_NOWAIT);
2138 l += print_chan (s + l, d->chan);
2139 l += print_stats (s + l, d->chan, 1);
2140 l += print_modems (s + l, d->chan, 1);
2141 l += print_e1_stats (s + l, d->chan);
2143 l += sprintf (s + l, "Error: node not connect to channel");
2144 strncpy ((resp)->header.cmdstr, "status", NG_CMDSTRLEN);
2150 NG_RESPOND_MSG (error, node, item, resp);
2155 static int ng_cp_rcvdata (hook_p hook, item_p item)
2157 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE(hook));
2159 struct ng_tag_prio *ptag;
2160 bdrv_t *bd = d->board->sys;
2164 CP_DEBUG2 (d, ("Rcvdata\n"));
2165 NGI_GET_M (item, m);
2166 NG_FREE_ITEM (item);
2167 if (! NG_HOOK_PRIVATE (hook) || ! d) {
2172 /* Check for high priority data */
2173 if ((ptag = (struct ng_tag_prio *)m_tag_locate(m, NGM_GENERIC_COOKIE,
2174 NG_TAG_PRIO, NULL)) != NULL && (ptag->priority > NG_PRIO_CUTOFF) )
2182 if (_IF_QFULL (q)) {
2198 static int ng_cp_rmnode (node_p node)
2200 drv_t *d = NG_NODE_PRIVATE (node);
2202 CP_DEBUG (d, ("Rmnode\n"));
2203 if (d && d->running) {
2204 bdrv_t *bd = d->board->sys;
2212 if (node->nd_flags & NGF_REALLY_DIE) {
2213 NG_NODE_SET_PRIVATE (node, NULL);
2214 NG_NODE_UNREF (node);
2216 NG_NODE_REVIVE(node); /* Persistant node */
2221 static void ng_cp_watchdog (void *arg)
2226 if (d->timeout == 1)
2230 callout_reset (&d->timeout_handle, hz, ng_cp_watchdog, d);
2234 static int ng_cp_connect (hook_p hook)
2236 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2239 CP_DEBUG (d, ("Connect\n"));
2240 callout_reset (&d->timeout_handle, hz, ng_cp_watchdog, d);
2246 static int ng_cp_disconnect (hook_p hook)
2248 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2251 CP_DEBUG (d, ("Disconnect\n"));
2252 if (NG_HOOK_PRIVATE (hook))
2254 bdrv_t *bd = d->board->sys;
2261 /* If we were wait it than it reasserted now, just stop it. */
2262 if (!callout_drain (&d->timeout_handle))
2263 callout_stop (&d->timeout_handle);
2269 static int cp_modevent (module_t mod, int type, void *unused)
2271 static int load_count = 0;
2274 cp_cdevsw.d_flags &= ~D_NEEDGIANT;
2279 if (ng_newtype (&typestruct))
2280 printf ("Failed to register ng_cp\n");
2283 callout_init (&timeout_handle, cp_mpsafenet?CALLOUT_MPSAFE:0);
2284 callout_reset (&timeout_handle, hz*5, cp_timeout, 0);
2287 if (load_count == 1) {
2288 printf ("Removing device entry for Tau-PCI\n");
2290 ng_rmtype (&typestruct);
2293 /* If we were wait it than it reasserted now, just stop it.
2294 * Actually we shouldn't get this condition. But code could be
2295 * changed in the future, so just be a litle paranoid.
2297 if (!callout_drain (&timeout_handle))
2298 callout_stop (&timeout_handle);
2308 static struct ng_type typestruct = {
2309 .version = NG_ABI_VERSION,
2310 .name = NG_CP_NODE_TYPE,
2311 .constructor = ng_cp_constructor,
2312 .rcvmsg = ng_cp_rcvmsg,
2313 .shutdown = ng_cp_rmnode,
2314 .newhook = ng_cp_newhook,
2315 .connect = ng_cp_connect,
2316 .rcvdata = ng_cp_rcvdata,
2317 .disconnect = ng_cp_disconnect,
2322 MODULE_DEPEND (ng_cp, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION);
2324 MODULE_DEPEND (cp, sppp, 1, 1, 1);
2326 DRIVER_MODULE (cp, pci, cp_driver, cp_devclass, cp_modevent, NULL);
2327 MODULE_VERSION (cp, 1);