2 * Cronyx-Tau-PCI adapter driver for FreeBSD.
3 * Supports PPP/HDLC, Cisco/HDLC and FrameRelay protocol in synchronous mode,
4 * and asynchronous channels with full modem control.
5 * Keepalive protocol implemented in both Cisco and PPP modes.
7 * Copyright (C) 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 <net/if_var.h>
46 #include <dev/pci/pcivar.h>
47 #include <dev/pci/pcireg.h>
48 #include <machine/bus.h>
50 #include "opt_ng_cronyx.h"
51 #ifdef NETGRAPH_CRONYX
52 # include "opt_netgraph.h"
54 # error #option NETGRAPH missed from configuration
56 # include <netgraph/ng_message.h>
57 # include <netgraph/netgraph.h>
58 # include <dev/cp/ng_cp.h>
60 # include <net/if_sppp.h>
61 # include <net/if_types.h>
62 #include <dev/pci/pcivar.h>
63 # define PP_CISCO IFF_LINK2
66 #include <dev/cp/machdep.h>
67 #include <dev/cp/cpddk.h>
68 #include <machine/cserial.h>
69 #include <machine/resource.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 #define CP_LOCK(_bd) mtx_lock (&(_bd)->cp_mtx)
83 #define CP_UNLOCK(_bd) mtx_unlock (&(_bd)->cp_mtx)
84 #define CP_LOCK_ASSERT(_bd) mtx_assert (&(_bd)->cp_mtx, MA_OWNED)
86 static int cp_probe __P((device_t));
87 static int cp_attach __P((device_t));
88 static int cp_detach __P((device_t));
90 static device_method_t cp_methods[] = {
91 /* Device interface */
92 DEVMETHOD(device_probe, cp_probe),
93 DEVMETHOD(device_attach, cp_attach),
94 DEVMETHOD(device_detach, cp_detach),
99 typedef struct _cp_dma_mem_t {
107 typedef struct _drv_t {
114 char nodename [NG_NODESIZE];
118 struct ifqueue queue;
119 struct ifqueue hi_queue;
121 struct ifqueue queue;
125 struct callout timeout_handle;
129 typedef struct _bdrv_t {
131 struct resource *cp_res;
132 struct resource *cp_irq;
135 drv_t channel [NCHAN];
139 static driver_t cp_driver = {
145 static devclass_t cp_devclass;
147 static void cp_receive (cp_chan_t *c, unsigned char *data, int len);
148 static void cp_transmit (cp_chan_t *c, void *attachment, int len);
149 static void cp_error (cp_chan_t *c, int data);
150 static void cp_up (drv_t *d);
151 static void cp_start (drv_t *d);
152 static void cp_down (drv_t *d);
153 static void cp_watchdog (drv_t *d);
154 static void cp_watchdog_timer (void *arg);
156 extern struct ng_type typestruct;
158 static void cp_ifstart (struct ifnet *ifp);
159 static void cp_tlf (struct sppp *sp);
160 static void cp_tls (struct sppp *sp);
161 static int cp_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data);
162 static void cp_initialize (void *softc);
165 static cp_board_t *adapter [NBRD];
166 static drv_t *channel [NBRD*NCHAN];
167 static struct callout led_timo [NBRD];
168 static struct callout timeout_handle;
170 static int cp_destroy = 0;
172 static int cp_open (struct cdev *dev, int oflags, int devtype, struct thread *td);
173 static int cp_close (struct cdev *dev, int fflag, int devtype, struct thread *td);
174 static int cp_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td);
175 static struct cdevsw cp_cdevsw = {
176 .d_version = D_VERSION,
184 * Make an mbuf from data.
186 static struct mbuf *makembuf (void *buf, unsigned len)
190 MGETHDR (m, M_NOWAIT, MT_DATA);
193 if (!(MCLGET (m, M_NOWAIT))) {
197 m->m_pkthdr.len = m->m_len = len;
198 bcopy (buf, mtod (m, caddr_t), len);
202 static int cp_probe (device_t dev)
204 if ((pci_get_vendor (dev) == cp_vendor_id) &&
205 (pci_get_device (dev) == cp_device_id)) {
206 device_set_desc (dev, "Cronyx-Tau-PCI serial adapter");
207 return BUS_PROBE_DEFAULT;
212 static void cp_timeout (void *arg)
217 for (i = 0; i < NBRD; ++i) {
218 if (adapter[i] == NULL)
220 for (k = 0; k < NCHAN; ++k) {
226 d = channel[i * NCHAN + k];
231 CP_LOCK ((bdrv_t *)d->board->sys);
232 switch (d->chan->type) {
234 cp_g703_timer (d->chan);
237 cp_e1_timer (d->chan);
242 cp_e3_timer (d->chan);
247 CP_UNLOCK ((bdrv_t *)d->board->sys);
253 callout_reset (&timeout_handle, hz, cp_timeout, 0);
257 static void cp_led_off (void *arg)
260 bdrv_t *bd = (bdrv_t *) b->sys;
273 static void cp_intr (void *arg)
276 cp_board_t *b = bd->board;
286 /* Check if we are ready */
287 if (b->sys == NULL) {
288 /* Not we are not, just cleanup. */
289 cp_interrupt_poll (b, 1);
298 /* Turn LED off 50 msec later. */
299 callout_reset (&led_timo[b->num], hz/20, cp_led_off, b);
304 /* Pass packets in a lock-free state */
305 for (i = 0; i < NCHAN && b->chan[i].type; i++) {
306 drv_t *d = b->chan[i].sys;
308 if (!d || !d->running)
310 while (_IF_QLEN(&d->queue)) {
311 IF_DEQUEUE (&d->queue,m);
314 sppp_input (d->ifp, m);
321 cp_bus_dmamap_addr (void *arg, bus_dma_segment_t *segs, int nseg, int error)
328 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
330 *addr = segs->ds_addr;
334 cp_bus_dma_mem_alloc (int bnum, int cnum, cp_dma_mem_t *dmem)
338 error = bus_dma_tag_create (NULL, 16, 0, BUS_SPACE_MAXADDR_32BIT,
339 BUS_SPACE_MAXADDR, NULL, NULL, dmem->size, 1,
340 dmem->size, 0, NULL, NULL, &dmem->dmat);
342 if (cnum >= 0) printf ("cp%d-%d: ", bnum, cnum);
343 else printf ("cp%d: ", bnum);
344 printf ("couldn't allocate tag for dma memory\n");
347 error = bus_dmamem_alloc (dmem->dmat, (void **)&dmem->virt,
348 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &dmem->mapp);
350 if (cnum >= 0) printf ("cp%d-%d: ", bnum, cnum);
351 else printf ("cp%d: ", bnum);
352 printf ("couldn't allocate mem for dma memory\n");
353 bus_dma_tag_destroy (dmem->dmat);
356 error = bus_dmamap_load (dmem->dmat, dmem->mapp, dmem->virt,
357 dmem->size, cp_bus_dmamap_addr, &dmem->phys, 0);
359 if (cnum >= 0) printf ("cp%d-%d: ", bnum, cnum);
360 else printf ("cp%d: ", bnum);
361 printf ("couldn't load mem map for dma memory\n");
362 bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
363 bus_dma_tag_destroy (dmem->dmat);
370 cp_bus_dma_mem_free (cp_dma_mem_t *dmem)
372 bus_dmamap_unload (dmem->dmat, dmem->mapp);
373 bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
374 bus_dma_tag_destroy (dmem->dmat);
378 * Called if the probe succeeded.
380 static int cp_attach (device_t dev)
382 bdrv_t *bd = device_get_softc (dev);
383 int unit = device_get_unit (dev);
384 char *cp_ln = CP_LOCK_NAME;
393 b = malloc (sizeof(cp_board_t), M_DEVBUF, M_WAITOK);
395 printf ("cp%d: couldn't allocate memory\n", unit);
399 bzero (b, sizeof(cp_board_t));
403 bd->cp_res = bus_alloc_resource (dev, SYS_RES_MEMORY, &rid,
404 0, ~0, 1, RF_ACTIVE);
406 printf ("cp%d: cannot map memory\n", unit);
411 vbase = (vm_offset_t) rman_get_virtual (bd->cp_res);
413 cp_ln[2] = '0' + unit;
414 mtx_init (&bd->cp_mtx, cp_ln, MTX_NETWORK_LOCK, MTX_DEF|MTX_RECURSE);
415 res = cp_init (b, unit, (u_char*) vbase);
417 printf ("cp%d: can't init, error code:%x\n", unit, res);
418 bus_release_resource (dev, SYS_RES_MEMORY, PCIR_BAR(0), bd->cp_res);
424 bd->dmamem.size = sizeof(cp_qbuf_t);
425 if (! cp_bus_dma_mem_alloc (unit, -1, &bd->dmamem)) {
431 cp_reset (b, bd->dmamem.virt, bd->dmamem.phys);
435 bd->cp_irq = bus_alloc_resource (dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
436 RF_SHAREABLE | RF_ACTIVE);
439 printf ("cp%d: cannot map interrupt\n", unit);
440 bus_release_resource (dev, SYS_RES_MEMORY,
441 PCIR_BAR(0), bd->cp_res);
442 mtx_destroy (&bd->cp_mtx);
447 callout_init (&led_timo[unit], 1);
448 error = bus_setup_intr (dev, bd->cp_irq,
449 INTR_TYPE_NET|INTR_MPSAFE,
450 NULL, cp_intr, bd, &bd->cp_intrhand);
453 printf ("cp%d: cannot set up irq\n", unit);
454 bus_release_resource (dev, SYS_RES_IRQ, 0, bd->cp_irq);
455 bus_release_resource (dev, SYS_RES_MEMORY,
456 PCIR_BAR(0), bd->cp_res);
457 mtx_destroy (&bd->cp_mtx);
462 printf ("cp%d: %s, clock %ld MHz\n", unit, b->name, b->osc / 1000000);
464 for (c = b->chan; c < b->chan + NCHAN; ++c) {
467 d = &bd->channel[c->num];
468 d->dmamem.size = sizeof(cp_buf_t);
469 if (! cp_bus_dma_mem_alloc (unit, c->num, &d->dmamem))
471 channel [b->num*NCHAN + c->num] = d;
472 sprintf (d->name, "cp%d.%d", b->num, c->num);
476 callout_init (&d->timeout_handle, 1);
478 if (ng_make_node_common (&typestruct, &d->node) != 0) {
479 printf ("%s: cannot make common node\n", d->name);
483 NG_NODE_SET_PRIVATE (d->node, d);
484 sprintf (d->nodename, "%s%d", NG_CP_NODE_TYPE,
485 c->board->num*NCHAN + c->num);
486 if (ng_name_node (d->node, d->nodename)) {
487 printf ("%s: cannot name node\n", d->nodename);
488 NG_NODE_UNREF (d->node);
491 d->queue.ifq_maxlen = ifqmaxlen;
492 d->hi_queue.ifq_maxlen = ifqmaxlen;
493 mtx_init (&d->queue.ifq_mtx, "cp_queue", NULL, MTX_DEF);
494 mtx_init (&d->hi_queue.ifq_mtx, "cp_queue_hi", NULL, MTX_DEF);
496 d->ifp = if_alloc(IFT_PPP);
497 if (d->ifp == NULL) {
498 printf ("%s: cannot if_alloc() interface\n", d->name);
501 d->ifp->if_softc = d;
502 if_initname (d->ifp, "cp", b->num * NCHAN + c->num);
503 d->ifp->if_mtu = PP_MTU;
504 d->ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
505 d->ifp->if_ioctl = cp_sioctl;
506 d->ifp->if_start = cp_ifstart;
507 d->ifp->if_init = cp_initialize;
508 d->queue.ifq_maxlen = NRBUF;
509 mtx_init (&d->queue.ifq_mtx, "cp_queue", NULL, MTX_DEF);
510 sppp_attach (d->ifp);
512 IFP2SP(d->ifp)->pp_tlf = cp_tlf;
513 IFP2SP(d->ifp)->pp_tls = cp_tls;
514 /* If BPF is in the kernel, call the attach for it.
515 * The header size of PPP or Cisco/HDLC is 4 bytes. */
516 bpfattach (d->ifp, DLT_PPP, 4);
519 cp_start_chan (c, 1, 1, d->dmamem.virt, d->dmamem.phys);
521 /* Register callback functions. */
522 cp_register_transmit (c, &cp_transmit);
523 cp_register_receive (c, &cp_receive);
524 cp_register_error (c, &cp_error);
525 d->devt = make_dev (&cp_cdevsw, b->num*NCHAN+c->num, UID_ROOT,
526 GID_WHEEL, 0600, "cp%d", b->num*NCHAN+c->num);
536 static int cp_detach (device_t dev)
538 bdrv_t *bd = device_get_softc (dev);
539 cp_board_t *b = bd->board;
543 KASSERT (mtx_initialized (&bd->cp_mtx), ("cp mutex not initialized"));
546 /* Check if the device is busy (open). */
547 for (c = b->chan; c < b->chan + NCHAN; ++c) {
548 drv_t *d = (drv_t*) c->sys;
550 if (! d || ! d->chan->type)
559 /* Ok, we can unload driver */
560 /* At first we should stop all channels */
561 for (c = b->chan; c < b->chan + NCHAN; ++c) {
562 drv_t *d = (drv_t*) c->sys;
564 if (! d || ! d->chan->type)
569 cp_set_dtr (d->chan, 0);
570 cp_set_rts (d->chan, 0);
573 /* Reset the adapter. */
575 cp_interrupt_poll (b, 1);
578 callout_stop (&led_timo[b->num]);
580 /* Disable the interrupt request. */
581 bus_teardown_intr (dev, bd->cp_irq, bd->cp_intrhand);
583 for (c=b->chan; c<b->chan+NCHAN; ++c) {
584 drv_t *d = (drv_t*) c->sys;
586 if (! d || ! d->chan->type)
588 callout_stop (&d->timeout_handle);
590 /* Detach from the packet filter list of interfaces. */
593 /* Detach from the sync PPP list. */
594 sppp_detach (d->ifp);
596 /* Detach from the system list of interfaces. */
599 IF_DRAIN (&d->queue);
600 mtx_destroy (&d->queue.ifq_mtx);
603 ng_rmnode_self (d->node);
604 NG_NODE_UNREF (d->node);
607 mtx_destroy (&d->queue.ifq_mtx);
608 mtx_destroy (&d->hi_queue.ifq_mtx);
610 destroy_dev (d->devt);
616 bus_release_resource (dev, SYS_RES_IRQ, 0, bd->cp_irq);
617 bus_release_resource (dev, SYS_RES_MEMORY, PCIR_BAR(0), bd->cp_res);
622 callout_drain (&led_timo[b->num]);
625 for (c = b->chan; c < b->chan + NCHAN; ++c) {
626 drv_t *d = (drv_t*) c->sys;
628 if (! d || ! d->chan->type)
630 callout_drain (&d->timeout_handle);
631 channel [b->num*NCHAN + c->num] = NULL;
632 /* Deallocate buffers. */
633 cp_bus_dma_mem_free (&d->dmamem);
635 adapter [b->num] = NULL;
636 cp_bus_dma_mem_free (&bd->dmamem);
638 mtx_destroy (&bd->cp_mtx);
643 static void cp_ifstart (struct ifnet *ifp)
645 drv_t *d = ifp->if_softc;
646 bdrv_t *bd = d->board->sys;
653 static void cp_tlf (struct sppp *sp)
655 drv_t *d = SP2IFP(sp)->if_softc;
657 CP_DEBUG2 (d, ("cp_tlf\n"));
658 /* XXXRIK: Don't forget to protect them by LOCK, or kill them. */
659 /* cp_set_dtr (d->chan, 0);*/
660 /* cp_set_rts (d->chan, 0);*/
661 if (!(sp->pp_flags & PP_FR) && !(d->ifp->if_flags & PP_CISCO))
665 static void cp_tls (struct sppp *sp)
667 drv_t *d = SP2IFP(sp)->if_softc;
669 CP_DEBUG2 (d, ("cp_tls\n"));
670 if (!(sp->pp_flags & PP_FR) && !(d->ifp->if_flags & PP_CISCO))
675 * Process an ioctl request.
677 static int cp_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data)
679 drv_t *d = ifp->if_softc;
680 bdrv_t *bd = d->board->sys;
681 int error, s, was_up, should_be_up;
683 was_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
684 error = sppp_ioctl (ifp, cmd, data);
689 if (! (ifp->if_flags & IFF_DEBUG))
692 d->chan->debug = d->chan->debug_shadow;
695 default: CP_DEBUG2 (d, ("ioctl 0x%lx\n", cmd)); return 0;
696 case SIOCADDMULTI: CP_DEBUG2 (d, ("ioctl SIOCADDMULTI\n")); return 0;
697 case SIOCDELMULTI: CP_DEBUG2 (d, ("ioctl SIOCDELMULTI\n")); return 0;
698 case SIOCSIFFLAGS: CP_DEBUG2 (d, ("ioctl SIOCSIFFLAGS\n")); break;
699 case SIOCSIFADDR: CP_DEBUG2 (d, ("ioctl SIOCSIFADDR\n")); break;
702 /* We get here only in case of SIFFLAGS or SIFADDR. */
705 should_be_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
706 if (! was_up && should_be_up) {
707 /* Interface goes up -- start it. */
710 } else if (was_up && ! should_be_up) {
711 /* Interface is going down -- stop it. */
712 /* if ((IFP2SP(ifp)->pp_flags & PP_FR) || (ifp->if_flags & PP_CISCO))*/
715 CP_DEBUG (d, ("ioctl 0x%lx p4\n", cmd));
722 * Initialization of interface.
723 * It seems to be never called by upper level?
725 static void cp_initialize (void *softc)
729 CP_DEBUG (d, ("cp_initialize\n"));
734 * Stop the interface. Called on splimp().
736 static void cp_down (drv_t *d)
738 CP_DEBUG (d, ("cp_down\n"));
739 /* Interface is going down -- stop it. */
740 cp_set_dtr (d->chan, 0);
741 cp_set_rts (d->chan, 0);
744 callout_stop (&d->timeout_handle);
748 * Start the interface. Called on splimp().
750 static void cp_up (drv_t *d)
752 CP_DEBUG (d, ("cp_up\n"));
753 cp_set_dtr (d->chan, 1);
754 cp_set_rts (d->chan, 1);
759 * Start output on the interface. Get another datagram to send
760 * off of the interface queue, and copy it to the interface
761 * before starting the output.
763 static void cp_send (drv_t *d)
768 CP_DEBUG2 (d, ("cp_send, tn=%d te=%d\n", d->chan->tn, d->chan->te));
770 /* No output if the interface is down. */
774 /* No output if the modem is off. */
775 if (! (d->chan->lloop || d->chan->type != T_SERIAL ||
776 cp_get_dsr (d->chan)))
779 while (cp_transmit_space (d->chan)) {
780 /* Get the packet to send. */
782 IF_DEQUEUE (&d->hi_queue, m);
784 IF_DEQUEUE (&d->queue, m);
786 m = sppp_dequeue (d->ifp);
791 BPF_MTAP (d->ifp, m);
793 len = m_length (m, NULL);
795 printf ("%s: too long packet: %d bytes: ",
797 else if (! m->m_next)
798 cp_send_packet (d->chan, (u_char*) mtod (m, caddr_t), len, 0);
800 u_char *buf = d->chan->tbuf[d->chan->te];
801 m_copydata (m, 0, len, buf);
802 cp_send_packet (d->chan, buf, len, 0);
805 /* Set up transmit timeout, if the transmit ring is not empty.*/
809 d->ifp->if_drv_flags |= IFF_DRV_OACTIVE;
814 * Start output on the interface.
815 * Always called on splimp().
817 static void cp_start (drv_t *d)
821 cp_set_dtr (d->chan, 1);
823 cp_set_rts (d->chan, 1);
825 callout_reset (&d->timeout_handle, hz, cp_watchdog_timer, d);
830 * Handle transmit timeouts.
831 * Recover after lost transmit interrupts.
832 * Always called on splimp().
834 static void cp_watchdog (drv_t *d)
836 CP_DEBUG (d, ("device timeout\n"));
838 cp_stop_chan (d->chan);
839 cp_stop_e1 (d->chan);
840 cp_start_e1 (d->chan);
841 cp_start_chan (d->chan, 1, 1, 0, 0);
842 cp_set_dtr (d->chan, 1);
843 cp_set_rts (d->chan, 1);
848 static void cp_watchdog_timer (void *arg)
851 bdrv_t *bd = d->board->sys;
858 callout_reset (&d->timeout_handle, hz, cp_watchdog_timer, d);
862 static void cp_transmit (cp_chan_t *c, void *attachment, int len)
868 if_inc_counter(d->ifp, IFCOUNTER_OPACKETS, 1);
869 d->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
874 static void cp_receive (cp_chan_t *c, unsigned char *data, int len)
885 m = makembuf (data, len);
887 CP_DEBUG (d, ("no memory for packet\n"));
889 if_inc_counter(d->ifp, IFCOUNTER_IQDROPS, 1);
896 m->m_pkthdr.rcvif = 0;
897 NG_SEND_DATA_ONLY (error, d->hook, m);
899 if_inc_counter(d->ifp, IFCOUNTER_IPACKETS, 1);
900 m->m_pkthdr.rcvif = d->ifp;
901 /* Check if there's a BPF listener on this interface.
902 * If so, hand off the raw packet to bpf. */
904 IF_ENQUEUE (&d->queue, m);
908 static void cp_error (cp_chan_t *c, int data)
914 CP_DEBUG (d, ("frame error\n"));
916 if_inc_counter(d->ifp, IFCOUNTER_IERRORS, 1);
920 CP_DEBUG (d, ("crc error\n"));
922 if_inc_counter(d->ifp, IFCOUNTER_IERRORS, 1);
926 CP_DEBUG (d, ("overrun error\n"));
928 if_inc_counter(d->ifp, IFCOUNTER_COLLISIONS, 1);
929 if_inc_counter(d->ifp, IFCOUNTER_IERRORS, 1);
933 CP_DEBUG (d, ("overflow error\n"));
935 if_inc_counter(d->ifp, IFCOUNTER_IERRORS, 1);
939 CP_DEBUG (d, ("underrun error\n"));
942 if_inc_counter(d->ifp, IFCOUNTER_OERRORS, 1);
943 d->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
948 CP_DEBUG (d, ("error #%d\n", data));
954 * You also need read, write, open, close routines.
955 * This should get you started
957 static int cp_open (struct cdev *dev, int oflags, int devtype, struct thread *td)
959 int unit = dev2unit (dev);
962 if (unit >= NBRD*NCHAN || ! (d = channel[unit]))
964 CP_DEBUG2 (d, ("cp_open\n"));
969 * Only called on the LAST close.
971 static int cp_close (struct cdev *dev, int fflag, int devtype, struct thread *td)
973 drv_t *d = channel [dev2unit (dev)];
975 CP_DEBUG2 (d, ("cp_close\n"));
979 static int cp_modem_status (cp_chan_t *c)
982 bdrv_t *bd = d->board->sys;
985 status = d->running ? TIOCM_LE : 0;
988 if (cp_get_cd (c)) status |= TIOCM_CD;
989 if (cp_get_cts (c)) status |= TIOCM_CTS;
990 if (cp_get_dsr (c)) status |= TIOCM_DSR;
991 if (c->dtr) status |= TIOCM_DTR;
992 if (c->rts) status |= TIOCM_RTS;
998 static int cp_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
1000 drv_t *d = channel [dev2unit (dev)];
1001 bdrv_t *bd = d->board->sys;
1002 cp_chan_t *c = d->chan;
1003 struct serial_statistics *st;
1004 struct e1_statistics *opte1;
1005 struct e3_statistics *opte3;
1010 case SERIAL_GETREGISTERED:
1011 CP_DEBUG2 (d, ("ioctl: getregistered\n"));
1012 bzero (mask, sizeof(mask));
1013 for (s=0; s<NBRD*NCHAN; ++s)
1015 mask [s/8] |= 1 << (s & 7);
1016 bcopy (mask, data, sizeof (mask));
1020 case SERIAL_GETPROTO:
1021 CP_DEBUG2 (d, ("ioctl: getproto\n"));
1022 strcpy ((char*)data, (IFP2SP(d->ifp)->pp_flags & PP_FR) ? "fr" :
1023 (d->ifp->if_flags & PP_CISCO) ? "cisco" : "ppp");
1026 case SERIAL_SETPROTO:
1027 CP_DEBUG2 (d, ("ioctl: setproto\n"));
1028 /* Only for superuser! */
1029 error = priv_check (td, PRIV_DRIVER);
1032 if (d->ifp->if_drv_flags & IFF_DRV_RUNNING)
1034 if (! strcmp ("cisco", (char*)data)) {
1035 IFP2SP(d->ifp)->pp_flags &= ~(PP_FR);
1036 IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE;
1037 d->ifp->if_flags |= PP_CISCO;
1039 } else if (! strcmp ("fr", (char*)data)) {
1040 d->ifp->if_flags &= ~(PP_CISCO);
1041 IFP2SP(d->ifp)->pp_flags |= PP_FR | PP_KEEPALIVE;
1043 } else if (! strcmp ("ppp", (char*)data)) {
1044 IFP2SP(d->ifp)->pp_flags &= ~PP_FR;
1045 IFP2SP(d->ifp)->pp_flags &= ~PP_KEEPALIVE;
1046 d->ifp->if_flags &= ~(PP_CISCO);
1051 case SERIAL_GETKEEPALIVE:
1052 CP_DEBUG2 (d, ("ioctl: getkeepalive\n"));
1053 if ((IFP2SP(d->ifp)->pp_flags & PP_FR) ||
1054 (d->ifp->if_flags & PP_CISCO))
1056 *(int*)data = (IFP2SP(d->ifp)->pp_flags & PP_KEEPALIVE) ? 1 : 0;
1059 case SERIAL_SETKEEPALIVE:
1060 CP_DEBUG2 (d, ("ioctl: setkeepalive\n"));
1061 /* Only for superuser! */
1062 error = priv_check (td, PRIV_DRIVER);
1065 if ((IFP2SP(d->ifp)->pp_flags & PP_FR) ||
1066 (d->ifp->if_flags & PP_CISCO))
1071 IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE;
1073 IFP2SP(d->ifp)->pp_flags &= ~PP_KEEPALIVE;
1079 case SERIAL_GETMODE:
1080 CP_DEBUG2 (d, ("ioctl: getmode\n"));
1081 *(int*)data = SERIAL_HDLC;
1084 case SERIAL_SETMODE:
1085 /* Only for superuser! */
1086 error = priv_check (td, PRIV_DRIVER);
1089 if (*(int*)data != SERIAL_HDLC)
1094 CP_DEBUG2 (d, ("ioctl: getcfg\n"));
1095 if (c->type != T_E1 || c->unfram)
1097 *(char*)data = c->board->mux ? 'c' : 'a';
1101 CP_DEBUG2 (d, ("ioctl: setcfg\n"));
1102 error = priv_check (td, PRIV_DRIVER);
1105 if (c->type != T_E1)
1109 cp_set_mux (c->board, *((char*)data) == 'c');
1114 case SERIAL_GETSTAT:
1115 CP_DEBUG2 (d, ("ioctl: getstat\n"));
1116 st = (struct serial_statistics*) data;
1117 st->rintr = c->rintr;
1118 st->tintr = c->tintr;
1120 st->ibytes = c->ibytes;
1121 st->ipkts = c->ipkts;
1122 st->obytes = c->obytes;
1123 st->opkts = c->opkts;
1124 st->ierrs = c->overrun + c->frame + c->crc;
1125 st->oerrs = c->underrun;
1128 case SERIAL_GETESTAT:
1129 CP_DEBUG2 (d, ("ioctl: getestat\n"));
1130 if (c->type != T_E1 && c->type != T_G703)
1132 opte1 = (struct e1_statistics*) data;
1133 opte1->status = c->status;
1134 opte1->cursec = c->cursec;
1135 opte1->totsec = c->totsec + c->cursec;
1137 opte1->currnt.bpv = c->currnt.bpv;
1138 opte1->currnt.fse = c->currnt.fse;
1139 opte1->currnt.crce = c->currnt.crce;
1140 opte1->currnt.rcrce = c->currnt.rcrce;
1141 opte1->currnt.uas = c->currnt.uas;
1142 opte1->currnt.les = c->currnt.les;
1143 opte1->currnt.es = c->currnt.es;
1144 opte1->currnt.bes = c->currnt.bes;
1145 opte1->currnt.ses = c->currnt.ses;
1146 opte1->currnt.oofs = c->currnt.oofs;
1147 opte1->currnt.css = c->currnt.css;
1148 opte1->currnt.dm = c->currnt.dm;
1150 opte1->total.bpv = c->total.bpv + c->currnt.bpv;
1151 opte1->total.fse = c->total.fse + c->currnt.fse;
1152 opte1->total.crce = c->total.crce + c->currnt.crce;
1153 opte1->total.rcrce = c->total.rcrce + c->currnt.rcrce;
1154 opte1->total.uas = c->total.uas + c->currnt.uas;
1155 opte1->total.les = c->total.les + c->currnt.les;
1156 opte1->total.es = c->total.es + c->currnt.es;
1157 opte1->total.bes = c->total.bes + c->currnt.bes;
1158 opte1->total.ses = c->total.ses + c->currnt.ses;
1159 opte1->total.oofs = c->total.oofs + c->currnt.oofs;
1160 opte1->total.css = c->total.css + c->currnt.css;
1161 opte1->total.dm = c->total.dm + c->currnt.dm;
1162 for (s=0; s<48; ++s) {
1163 opte1->interval[s].bpv = c->interval[s].bpv;
1164 opte1->interval[s].fse = c->interval[s].fse;
1165 opte1->interval[s].crce = c->interval[s].crce;
1166 opte1->interval[s].rcrce = c->interval[s].rcrce;
1167 opte1->interval[s].uas = c->interval[s].uas;
1168 opte1->interval[s].les = c->interval[s].les;
1169 opte1->interval[s].es = c->interval[s].es;
1170 opte1->interval[s].bes = c->interval[s].bes;
1171 opte1->interval[s].ses = c->interval[s].ses;
1172 opte1->interval[s].oofs = c->interval[s].oofs;
1173 opte1->interval[s].css = c->interval[s].css;
1174 opte1->interval[s].dm = c->interval[s].dm;
1178 case SERIAL_GETE3STAT:
1179 CP_DEBUG2 (d, ("ioctl: gete3stat\n"));
1180 if (c->type != T_E3 && c->type != T_T3 && c->type != T_STS1)
1182 opte3 = (struct e3_statistics*) data;
1184 opte3->status = c->e3status;
1185 opte3->cursec = (c->e3csec_5 * 2 + 1) / 10;
1186 opte3->totsec = c->e3tsec + opte3->cursec;
1188 opte3->ccv = c->e3ccv;
1189 opte3->tcv = c->e3tcv + opte3->ccv;
1191 for (s = 0; s < 48; ++s) {
1192 opte3->icv[s] = c->e3icv[s];
1196 case SERIAL_CLRSTAT:
1197 CP_DEBUG2 (d, ("ioctl: clrstat\n"));
1198 /* Only for superuser! */
1199 error = priv_check (td, PRIV_DRIVER);
1212 bzero (&c->currnt, sizeof (c->currnt));
1213 bzero (&c->total, sizeof (c->total));
1214 bzero (c->interval, sizeof (c->interval));
1217 bzero (c->e3icv, sizeof (c->e3icv));
1220 case SERIAL_GETBAUD:
1221 CP_DEBUG2 (d, ("ioctl: getbaud\n"));
1222 *(long*)data = c->baud;
1225 case SERIAL_SETBAUD:
1226 CP_DEBUG2 (d, ("ioctl: setbaud\n"));
1227 /* Only for superuser! */
1228 error = priv_check (td, PRIV_DRIVER);
1233 cp_set_baud (c, *(long*)data);
1238 case SERIAL_GETLOOP:
1239 CP_DEBUG2 (d, ("ioctl: getloop\n"));
1240 *(int*)data = c->lloop;
1243 case SERIAL_SETLOOP:
1244 CP_DEBUG2 (d, ("ioctl: setloop\n"));
1245 /* Only for superuser! */
1246 error = priv_check (td, PRIV_DRIVER);
1251 cp_set_lloop (c, *(int*)data);
1256 case SERIAL_GETDPLL:
1257 CP_DEBUG2 (d, ("ioctl: getdpll\n"));
1258 if (c->type != T_SERIAL)
1260 *(int*)data = c->dpll;
1263 case SERIAL_SETDPLL:
1264 CP_DEBUG2 (d, ("ioctl: setdpll\n"));
1265 /* Only for superuser! */
1266 error = priv_check (td, PRIV_DRIVER);
1269 if (c->type != T_SERIAL)
1273 cp_set_dpll (c, *(int*)data);
1278 case SERIAL_GETNRZI:
1279 CP_DEBUG2 (d, ("ioctl: getnrzi\n"));
1280 if (c->type != T_SERIAL)
1282 *(int*)data = c->nrzi;
1285 case SERIAL_SETNRZI:
1286 CP_DEBUG2 (d, ("ioctl: setnrzi\n"));
1287 /* Only for superuser! */
1288 error = priv_check (td, PRIV_DRIVER);
1291 if (c->type != T_SERIAL)
1295 cp_set_nrzi (c, *(int*)data);
1300 case SERIAL_GETDEBUG:
1301 CP_DEBUG2 (d, ("ioctl: getdebug\n"));
1302 *(int*)data = d->chan->debug;
1305 case SERIAL_SETDEBUG:
1306 CP_DEBUG2 (d, ("ioctl: setdebug\n"));
1307 /* Only for superuser! */
1308 error = priv_check (td, PRIV_DRIVER);
1313 * The debug_shadow is always greater than zero for logic
1314 * simplicity. For switching debug off the IFF_DEBUG is
1317 d->chan->debug_shadow = (*(int*)data) ? (*(int*)data) : 1;
1318 if (d->ifp->if_flags & IFF_DEBUG)
1319 d->chan->debug = d->chan->debug_shadow;
1321 d->chan->debug = *(int*)data;
1325 case SERIAL_GETHIGAIN:
1326 CP_DEBUG2 (d, ("ioctl: gethigain\n"));
1327 if (c->type != T_E1)
1329 *(int*)data = c->higain;
1332 case SERIAL_SETHIGAIN:
1333 CP_DEBUG2 (d, ("ioctl: sethigain\n"));
1334 /* Only for superuser! */
1335 error = priv_check (td, PRIV_DRIVER);
1338 if (c->type != T_E1)
1342 cp_set_higain (c, *(int*)data);
1347 case SERIAL_GETPHONY:
1348 CP_DEBUG2 (d, ("ioctl: getphony\n"));
1349 if (c->type != T_E1)
1351 *(int*)data = c->phony;
1354 case SERIAL_SETPHONY:
1355 CP_DEBUG2 (d, ("ioctl: setphony\n"));
1356 /* Only for superuser! */
1357 error = priv_check (td, PRIV_DRIVER);
1360 if (c->type != T_E1)
1364 cp_set_phony (c, *(int*)data);
1369 case SERIAL_GETUNFRAM:
1370 CP_DEBUG2 (d, ("ioctl: getunfram\n"));
1371 if (c->type != T_E1)
1373 *(int*)data = c->unfram;
1376 case SERIAL_SETUNFRAM:
1377 CP_DEBUG2 (d, ("ioctl: setunfram\n"));
1378 /* Only for superuser! */
1379 error = priv_check (td, PRIV_DRIVER);
1382 if (c->type != T_E1)
1386 cp_set_unfram (c, *(int*)data);
1391 case SERIAL_GETSCRAMBLER:
1392 CP_DEBUG2 (d, ("ioctl: getscrambler\n"));
1393 if (c->type != T_G703 && !c->unfram)
1395 *(int*)data = c->scrambler;
1398 case SERIAL_SETSCRAMBLER:
1399 CP_DEBUG2 (d, ("ioctl: setscrambler\n"));
1400 /* Only for superuser! */
1401 error = priv_check (td, PRIV_DRIVER);
1404 if (c->type != T_G703 && !c->unfram)
1408 cp_set_scrambler (c, *(int*)data);
1413 case SERIAL_GETMONITOR:
1414 CP_DEBUG2 (d, ("ioctl: getmonitor\n"));
1415 if (c->type != T_E1 &&
1420 *(int*)data = c->monitor;
1423 case SERIAL_SETMONITOR:
1424 CP_DEBUG2 (d, ("ioctl: setmonitor\n"));
1425 /* Only for superuser! */
1426 error = priv_check (td, PRIV_DRIVER);
1429 if (c->type != T_E1)
1433 cp_set_monitor (c, *(int*)data);
1438 case SERIAL_GETUSE16:
1439 CP_DEBUG2 (d, ("ioctl: getuse16\n"));
1440 if (c->type != T_E1 || c->unfram)
1442 *(int*)data = c->use16;
1445 case SERIAL_SETUSE16:
1446 CP_DEBUG2 (d, ("ioctl: setuse16\n"));
1447 /* Only for superuser! */
1448 error = priv_check (td, PRIV_DRIVER);
1451 if (c->type != T_E1)
1455 cp_set_use16 (c, *(int*)data);
1460 case SERIAL_GETCRC4:
1461 CP_DEBUG2 (d, ("ioctl: getcrc4\n"));
1462 if (c->type != T_E1 || c->unfram)
1464 *(int*)data = c->crc4;
1467 case SERIAL_SETCRC4:
1468 CP_DEBUG2 (d, ("ioctl: setcrc4\n"));
1469 /* Only for superuser! */
1470 error = priv_check (td, PRIV_DRIVER);
1473 if (c->type != T_E1)
1477 cp_set_crc4 (c, *(int*)data);
1483 CP_DEBUG2 (d, ("ioctl: getclk\n"));
1484 if (c->type != T_E1 &&
1485 c->type != T_G703 &&
1491 default: *(int*)data = E1CLK_INTERNAL; break;
1492 case GSYN_RCV: *(int*)data = E1CLK_RECEIVE; break;
1493 case GSYN_RCV0: *(int*)data = E1CLK_RECEIVE_CHAN0; break;
1494 case GSYN_RCV1: *(int*)data = E1CLK_RECEIVE_CHAN1; break;
1495 case GSYN_RCV2: *(int*)data = E1CLK_RECEIVE_CHAN2; break;
1496 case GSYN_RCV3: *(int*)data = E1CLK_RECEIVE_CHAN3; break;
1501 CP_DEBUG2 (d, ("ioctl: setclk\n"));
1502 /* Only for superuser! */
1503 error = priv_check (td, PRIV_DRIVER);
1506 if (c->type != T_E1 &&
1507 c->type != T_G703 &&
1514 switch (*(int*)data) {
1515 default: cp_set_gsyn (c, GSYN_INT); break;
1516 case E1CLK_RECEIVE: cp_set_gsyn (c, GSYN_RCV); break;
1517 case E1CLK_RECEIVE_CHAN0: cp_set_gsyn (c, GSYN_RCV0); break;
1518 case E1CLK_RECEIVE_CHAN1: cp_set_gsyn (c, GSYN_RCV1); break;
1519 case E1CLK_RECEIVE_CHAN2: cp_set_gsyn (c, GSYN_RCV2); break;
1520 case E1CLK_RECEIVE_CHAN3: cp_set_gsyn (c, GSYN_RCV3); break;
1526 case SERIAL_GETTIMESLOTS:
1527 CP_DEBUG2 (d, ("ioctl: gettimeslots\n"));
1528 if ((c->type != T_E1 || c->unfram) && c->type != T_DATA)
1530 *(u_long*)data = c->ts;
1533 case SERIAL_SETTIMESLOTS:
1534 CP_DEBUG2 (d, ("ioctl: settimeslots\n"));
1535 /* Only for superuser! */
1536 error = priv_check (td, PRIV_DRIVER);
1539 if ((c->type != T_E1 || c->unfram) && c->type != T_DATA)
1543 cp_set_ts (c, *(u_long*)data);
1548 case SERIAL_GETINVCLK:
1549 CP_DEBUG2 (d, ("ioctl: getinvclk\n"));
1553 if (c->type != T_SERIAL)
1555 *(int*)data = c->invtxc;
1559 case SERIAL_SETINVCLK:
1560 CP_DEBUG2 (d, ("ioctl: setinvclk\n"));
1561 /* Only for superuser! */
1562 error = priv_check (td, PRIV_DRIVER);
1565 if (c->type != T_SERIAL)
1569 cp_set_invtxc (c, *(int*)data);
1570 cp_set_invrxc (c, *(int*)data);
1575 case SERIAL_GETINVTCLK:
1576 CP_DEBUG2 (d, ("ioctl: getinvtclk\n"));
1577 if (c->type != T_SERIAL)
1579 *(int*)data = c->invtxc;
1582 case SERIAL_SETINVTCLK:
1583 CP_DEBUG2 (d, ("ioctl: setinvtclk\n"));
1584 /* Only for superuser! */
1585 error = priv_check (td, PRIV_DRIVER);
1588 if (c->type != T_SERIAL)
1592 cp_set_invtxc (c, *(int*)data);
1597 case SERIAL_GETINVRCLK:
1598 CP_DEBUG2 (d, ("ioctl: getinvrclk\n"));
1599 if (c->type != T_SERIAL)
1601 *(int*)data = c->invrxc;
1604 case SERIAL_SETINVRCLK:
1605 CP_DEBUG2 (d, ("ioctl: setinvrclk\n"));
1606 /* Only for superuser! */
1607 error = priv_check (td, PRIV_DRIVER);
1610 if (c->type != T_SERIAL)
1614 cp_set_invrxc (c, *(int*)data);
1619 case SERIAL_GETLEVEL:
1620 CP_DEBUG2 (d, ("ioctl: getlevel\n"));
1621 if (c->type != T_G703)
1625 *(int*)data = cp_get_lq (c);
1632 CP_DEBUG2 (d, ("ioctl: reset\n"));
1633 /* Only for superuser! */
1634 error = priv_check (td, PRIV_DRIVER);
1639 cp_reset (c->board, 0, 0);
1644 case SERIAL_HARDRESET:
1645 CP_DEBUG2 (d, ("ioctl: hardreset\n"));
1646 /* Only for superuser! */
1647 error = priv_check (td, PRIV_DRIVER);
1652 /* hard_reset (c->board); */
1658 case SERIAL_GETCABLE:
1659 CP_DEBUG2 (d, ("ioctl: getcable\n"));
1660 if (c->type != T_SERIAL)
1664 *(int*)data = cp_get_cable (c);
1670 CP_DEBUG2 (d, ("ioctl: getdir\n"));
1671 if (c->type != T_E1 && c->type != T_DATA)
1673 *(int*)data = c->dir;
1677 CP_DEBUG2 (d, ("ioctl: setdir\n"));
1678 /* Only for superuser! */
1679 error = priv_check (td, PRIV_DRIVER);
1684 cp_set_dir (c, *(int*)data);
1689 case SERIAL_GETRLOOP:
1690 CP_DEBUG2 (d, ("ioctl: getrloop\n"));
1691 if (c->type != T_G703 &&
1696 *(int*)data = cp_get_rloop (c);
1699 case SERIAL_SETRLOOP:
1700 CP_DEBUG2 (d, ("ioctl: setloop\n"));
1701 if (c->type != T_E3 && c->type != T_T3 && c->type != T_STS1)
1703 /* Only for superuser! */
1704 error = priv_check (td, PRIV_DRIVER);
1709 cp_set_rloop (c, *(int*)data);
1714 case SERIAL_GETCABLEN:
1715 CP_DEBUG2 (d, ("ioctl: getcablen\n"));
1716 if (c->type != T_T3 && c->type != T_STS1)
1718 *(int*)data = c->cablen;
1721 case SERIAL_SETCABLEN:
1722 CP_DEBUG2 (d, ("ioctl: setloop\n"));
1723 if (c->type != T_T3 && c->type != T_STS1)
1725 /* Only for superuser! */
1726 error = priv_check (td, PRIV_DRIVER);
1731 cp_set_cablen (c, *(int*)data);
1736 case TIOCSDTR: /* Set DTR */
1744 case TIOCCDTR: /* Clear DTR */
1752 case TIOCMSET: /* Set DTR/RTS */
1755 cp_set_dtr (c, (*(int*)data & TIOCM_DTR) ? 1 : 0);
1756 cp_set_rts (c, (*(int*)data & TIOCM_RTS) ? 1 : 0);
1761 case TIOCMBIS: /* Add DTR/RTS */
1764 if (*(int*)data & TIOCM_DTR) cp_set_dtr (c, 1);
1765 if (*(int*)data & TIOCM_RTS) cp_set_rts (c, 1);
1770 case TIOCMBIC: /* Clear DTR/RTS */
1773 if (*(int*)data & TIOCM_DTR) cp_set_dtr (c, 0);
1774 if (*(int*)data & TIOCM_RTS) cp_set_rts (c, 0);
1779 case TIOCMGET: /* Get modem status */
1780 *(int*)data = cp_modem_status (c);
1787 static int ng_cp_constructor (node_p node)
1789 drv_t *d = NG_NODE_PRIVATE (node);
1790 CP_DEBUG (d, ("Constructor\n"));
1794 static int ng_cp_newhook (node_p node, hook_p hook, const char *name)
1797 drv_t *d = NG_NODE_PRIVATE (node);
1798 bdrv_t *bd = d->board->sys;
1800 CP_DEBUG (d, ("Newhook\n"));
1801 /* Attach debug hook */
1802 if (strcmp (name, NG_CP_HOOK_DEBUG) == 0) {
1803 NG_HOOK_SET_PRIVATE (hook, NULL);
1804 d->debug_hook = hook;
1808 /* Check for raw hook */
1809 if (strcmp (name, NG_CP_HOOK_RAW) != 0)
1812 NG_HOOK_SET_PRIVATE (hook, d);
1822 static char *format_timeslots (u_long s)
1824 static char buf [100];
1828 for (i=1; i<32; ++i)
1830 int prev = (i > 1) & (s >> (i-1));
1831 int next = (i < 31) & (s >> (i+1));
1841 *p++ = '0' + i / 10;
1842 *p++ = '0' + i % 10;
1848 static int print_modems (char *s, cp_chan_t *c, int need_header)
1850 int status = cp_modem_status (c);
1854 length += sprintf (s + length, " LE DTR DSR RTS CTS CD\n");
1855 length += sprintf (s + length, "%4s %4s %4s %4s %4s %4s\n",
1856 status & TIOCM_LE ? "On" : "-",
1857 status & TIOCM_DTR ? "On" : "-",
1858 status & TIOCM_DSR ? "On" : "-",
1859 status & TIOCM_RTS ? "On" : "-",
1860 status & TIOCM_CTS ? "On" : "-",
1861 status & TIOCM_CD ? "On" : "-");
1865 static int print_stats (char *s, cp_chan_t *c, int need_header)
1870 length += sprintf (s + length, " Rintr Tintr Mintr Ibytes Ipkts Ierrs Obytes Opkts Oerrs\n");
1871 length += sprintf (s + length, "%7ld %7ld %7ld %8lu %7ld %7ld %8lu %7ld %7ld\n",
1872 c->rintr, c->tintr, 0l, (unsigned long) c->ibytes,
1873 c->ipkts, c->overrun + c->frame + c->crc,
1874 (unsigned long) c->obytes, c->opkts, c->underrun);
1878 static char *format_e1_status (u_char status)
1880 static char buf [80];
1882 if (status & E1_NOALARM)
1885 if (status & E1_LOS) strcat (buf, ",LOS");
1886 if (status & E1_AIS) strcat (buf, ",AIS");
1887 if (status & E1_LOF) strcat (buf, ",LOF");
1888 if (status & E1_LOMF) strcat (buf, ",LOMF");
1889 if (status & E1_FARLOF) strcat (buf, ",FARLOF");
1890 if (status & E1_AIS16) strcat (buf, ",AIS16");
1891 if (status & E1_FARLOMF) strcat (buf, ",FARLOMF");
1892 if (status & E1_TSTREQ) strcat (buf, ",TSTREQ");
1893 if (status & E1_TSTERR) strcat (buf, ",TSTERR");
1899 static int print_frac (char *s, int leftalign, u_long numerator, u_long divider)
1903 if (numerator < 1 || divider < 1) {
1904 length += sprintf (s+length, leftalign ? "/- " : " -");
1907 n = (int) (0.5 + 1000.0 * numerator / divider);
1909 length += sprintf (s+length, leftalign ? "/.%-3d" : " .%03d", n);
1912 *(s + length) = leftalign ? '/' : ' ';
1915 if (n >= 1000000) n = (n+500) / 1000 * 1000;
1916 else if (n >= 100000) n = (n+50) / 100 * 100;
1917 else if (n >= 10000) n = (n+5) / 10 * 10;
1920 case 1000: length += printf (s+length, ".999"); return length;
1921 case 10000: n = 9990; break;
1922 case 100000: n = 99900; break;
1923 case 1000000: n = 999000; break;
1925 if (n < 10000) length += sprintf (s+length, "%d.%d", n/1000, n/10%100);
1926 else if (n < 100000) length += sprintf (s+length, "%d.%d", n/1000, n/100%10);
1927 else if (n < 1000000) length += sprintf (s+length, "%d.", n/1000);
1928 else length += sprintf (s+length, "%d", n/1000);
1933 static int print_e1_stats (char *s, cp_chan_t *c)
1935 struct e1_counters total;
1939 totsec = c->totsec + c->cursec;
1940 total.bpv = c->total.bpv + c->currnt.bpv;
1941 total.fse = c->total.fse + c->currnt.fse;
1942 total.crce = c->total.crce + c->currnt.crce;
1943 total.rcrce = c->total.rcrce + c->currnt.rcrce;
1944 total.uas = c->total.uas + c->currnt.uas;
1945 total.les = c->total.les + c->currnt.les;
1946 total.es = c->total.es + c->currnt.es;
1947 total.bes = c->total.bes + c->currnt.bes;
1948 total.ses = c->total.ses + c->currnt.ses;
1949 total.oofs = c->total.oofs + c->currnt.oofs;
1950 total.css = c->total.css + c->currnt.css;
1951 total.dm = c->total.dm + c->currnt.dm;
1953 length += sprintf (s + length, " Unav/Degr Bpv/Fsyn CRC/RCRC Err/Lerr Sev/Bur Oof/Slp Status\n");
1955 /* Unavailable seconds, degraded minutes */
1956 length += print_frac (s + length, 0, c->currnt.uas, c->cursec);
1957 length += print_frac (s + length, 1, 60 * c->currnt.dm, c->cursec);
1959 /* Bipolar violations, frame sync errors */
1960 length += print_frac (s + length, 0, c->currnt.bpv, c->cursec);
1961 length += print_frac (s + length, 1, c->currnt.fse, c->cursec);
1963 /* CRC errors, remote CRC errors (E-bit) */
1964 length += print_frac (s + length, 0, c->currnt.crce, c->cursec);
1965 length += print_frac (s + length, 1, c->currnt.rcrce, c->cursec);
1967 /* Errored seconds, line errored seconds */
1968 length += print_frac (s + length, 0, c->currnt.es, c->cursec);
1969 length += print_frac (s + length, 1, c->currnt.les, c->cursec);
1971 /* Severely errored seconds, burst errored seconds */
1972 length += print_frac (s + length, 0, c->currnt.ses, c->cursec);
1973 length += print_frac (s + length, 1, c->currnt.bes, c->cursec);
1975 /* Out of frame seconds, controlled slip seconds */
1976 length += print_frac (s + length, 0, c->currnt.oofs, c->cursec);
1977 length += print_frac (s + length, 1, c->currnt.css, c->cursec);
1979 length += sprintf (s + length, " %s\n", format_e1_status (c->status));
1981 /* Print total statistics. */
1982 length += print_frac (s + length, 0, total.uas, totsec);
1983 length += print_frac (s + length, 1, 60 * total.dm, totsec);
1985 length += print_frac (s + length, 0, total.bpv, totsec);
1986 length += print_frac (s + length, 1, total.fse, totsec);
1988 length += print_frac (s + length, 0, total.crce, totsec);
1989 length += print_frac (s + length, 1, total.rcrce, totsec);
1991 length += print_frac (s + length, 0, total.es, totsec);
1992 length += print_frac (s + length, 1, total.les, totsec);
1994 length += print_frac (s + length, 0, total.ses, totsec);
1995 length += print_frac (s + length, 1, total.bes, totsec);
1997 length += print_frac (s + length, 0, total.oofs, totsec);
1998 length += print_frac (s + length, 1, total.css, totsec);
2000 length += sprintf (s + length, " -- Total\n");
2004 static int print_chan (char *s, cp_chan_t *c)
2007 bdrv_t *bd = d->board->sys;
2010 length += sprintf (s + length, "cp%d", c->board->num * NCHAN + c->num);
2012 length += sprintf (s + length, " debug=%d", d->chan->debug);
2014 if (c->board->mux) {
2015 length += sprintf (s + length, " cfg=C");
2017 length += sprintf (s + length, " cfg=A");
2021 length += sprintf (s + length, " %ld", c->baud);
2023 length += sprintf (s + length, " extclock");
2025 if (c->type == T_E1 || c->type == T_G703)
2027 case GSYN_INT : length += sprintf (s + length, " syn=int"); break;
2028 case GSYN_RCV : length += sprintf (s + length, " syn=rcv"); break;
2029 case GSYN_RCV0 : length += sprintf (s + length, " syn=rcv0"); break;
2030 case GSYN_RCV1 : length += sprintf (s + length, " syn=rcv1"); break;
2031 case GSYN_RCV2 : length += sprintf (s + length, " syn=rcv2"); break;
2032 case GSYN_RCV3 : length += sprintf (s + length, " syn=rcv3"); break;
2034 if (c->type == T_SERIAL) {
2035 length += sprintf (s + length, " dpll=%s", c->dpll ? "on" : "off");
2036 length += sprintf (s + length, " nrzi=%s", c->nrzi ? "on" : "off");
2037 length += sprintf (s + length, " invclk=%s", c->invtxc ? "on" : "off");
2039 if (c->type == T_E1)
2040 length += sprintf (s + length, " higain=%s", c->higain ? "on" : "off");
2042 length += sprintf (s + length, " loop=%s", c->lloop ? "on" : "off");
2044 if (c->type == T_E1)
2045 length += sprintf (s + length, " ts=%s", format_timeslots (c->ts));
2046 if (c->type == T_G703) {
2054 length += sprintf (s + length, " (level=-%.1fdB)", lq / 10.0);
2056 length += sprintf (s + length, "\n");
2060 static int ng_cp_rcvmsg (node_p node, item_p item, hook_p lasthook)
2062 drv_t *d = NG_NODE_PRIVATE (node);
2063 struct ng_mesg *msg;
2064 struct ng_mesg *resp = NULL;
2067 CP_DEBUG (d, ("Rcvmsg\n"));
2068 NGI_GET_MSG (item, msg);
2069 switch (msg->header.typecookie) {
2075 printf ("Not implemented yet\n");
2079 case NGM_GENERIC_COOKIE:
2080 switch (msg->header.cmd) {
2085 case NGM_TEXT_STATUS: {
2088 int dl = sizeof (struct ng_mesg) + 730;
2090 NG_MKRESPONSE (resp, msg, dl, M_NOWAIT);
2097 l += print_chan (s + l, d->chan);
2098 l += print_stats (s + l, d->chan, 1);
2099 l += print_modems (s + l, d->chan, 1);
2100 l += print_e1_stats (s + l, d->chan);
2102 l += sprintf (s + l, "Error: node not connect to channel");
2103 strncpy ((resp)->header.cmdstr, "status", NG_CMDSTRSIZ);
2109 NG_RESPOND_MSG (error, node, item, resp);
2114 static int ng_cp_rcvdata (hook_p hook, item_p item)
2116 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE(hook));
2118 struct ng_tag_prio *ptag;
2119 bdrv_t *bd = d->board->sys;
2123 CP_DEBUG2 (d, ("Rcvdata\n"));
2124 NGI_GET_M (item, m);
2125 NG_FREE_ITEM (item);
2126 if (! NG_HOOK_PRIVATE (hook) || ! d) {
2131 /* Check for high priority data */
2132 if ((ptag = (struct ng_tag_prio *)m_tag_locate(m, NGM_GENERIC_COOKIE,
2133 NG_TAG_PRIO, NULL)) != NULL && (ptag->priority > NG_PRIO_CUTOFF) )
2141 if (_IF_QFULL (q)) {
2156 static int ng_cp_rmnode (node_p node)
2158 drv_t *d = NG_NODE_PRIVATE (node);
2160 CP_DEBUG (d, ("Rmnode\n"));
2161 if (d && d->running) {
2162 bdrv_t *bd = d->board->sys;
2170 if (node->nd_flags & NGF_REALLY_DIE) {
2171 NG_NODE_SET_PRIVATE (node, NULL);
2172 NG_NODE_UNREF (node);
2174 NG_NODE_REVIVE(node); /* Persistent node */
2179 static int ng_cp_connect (hook_p hook)
2181 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2184 CP_DEBUG (d, ("Connect\n"));
2185 callout_reset (&d->timeout_handle, hz, cp_watchdog_timer, d);
2191 static int ng_cp_disconnect (hook_p hook)
2193 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2196 CP_DEBUG (d, ("Disconnect\n"));
2197 if (NG_HOOK_PRIVATE (hook))
2199 bdrv_t *bd = d->board->sys;
2206 /* If we were wait it than it reasserted now, just stop it. */
2207 if (!callout_drain (&d->timeout_handle))
2208 callout_stop (&d->timeout_handle);
2214 static int cp_modevent (module_t mod, int type, void *unused)
2216 static int load_count = 0;
2221 if (ng_newtype (&typestruct))
2222 printf ("Failed to register ng_cp\n");
2225 callout_init (&timeout_handle, 1);
2226 callout_reset (&timeout_handle, hz*5, cp_timeout, 0);
2229 if (load_count == 1) {
2230 printf ("Removing device entry for Tau-PCI\n");
2232 ng_rmtype (&typestruct);
2235 /* If we were wait it than it reasserted now, just stop it.
2236 * Actually we shouldn't get this condition. But code could be
2237 * changed in the future, so just be a litle paranoid.
2239 if (!callout_drain (&timeout_handle))
2240 callout_stop (&timeout_handle);
2250 static struct ng_type typestruct = {
2251 .version = NG_ABI_VERSION,
2252 .name = NG_CP_NODE_TYPE,
2253 .constructor = ng_cp_constructor,
2254 .rcvmsg = ng_cp_rcvmsg,
2255 .shutdown = ng_cp_rmnode,
2256 .newhook = ng_cp_newhook,
2257 .connect = ng_cp_connect,
2258 .rcvdata = ng_cp_rcvdata,
2259 .disconnect = ng_cp_disconnect,
2264 MODULE_DEPEND (ng_cp, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION);
2266 MODULE_DEPEND (cp, sppp, 1, 1, 1);
2268 DRIVER_MODULE (cp, pci, cp_driver, cp_devclass, cp_modevent, NULL);
2269 MODULE_VERSION (cp, 1);