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 <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 #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 * Print the mbuf chain, for debug purposes only.
186 static void printmbuf (struct mbuf *m)
189 for (; m; m=m->m_next) {
190 if (m->m_flags & M_PKTHDR)
191 printf (" HDR %d:", m->m_pkthdr.len);
192 if (m->m_flags & M_EXT)
194 printf (" %d", m->m_len);
200 * Make an mbuf from data.
202 static struct mbuf *makembuf (void *buf, unsigned len)
206 MGETHDR (m, M_NOWAIT, MT_DATA);
209 MCLGET (m, M_NOWAIT);
210 if (! (m->m_flags & M_EXT)) {
214 m->m_pkthdr.len = m->m_len = len;
215 bcopy (buf, mtod (m, caddr_t), len);
219 static int cp_probe (device_t dev)
221 if ((pci_get_vendor (dev) == cp_vendor_id) &&
222 (pci_get_device (dev) == cp_device_id)) {
223 device_set_desc (dev, "Cronyx-Tau-PCI serial adapter");
224 return BUS_PROBE_DEFAULT;
229 static void cp_timeout (void *arg)
234 for (i = 0; i < NBRD; ++i) {
235 if (adapter[i] == NULL)
237 for (k = 0; k < NCHAN; ++k) {
243 d = channel[i * NCHAN + k];
248 CP_LOCK ((bdrv_t *)d->board->sys);
249 switch (d->chan->type) {
251 cp_g703_timer (d->chan);
254 cp_e1_timer (d->chan);
259 cp_e3_timer (d->chan);
264 CP_UNLOCK ((bdrv_t *)d->board->sys);
270 callout_reset (&timeout_handle, hz, cp_timeout, 0);
274 static void cp_led_off (void *arg)
277 bdrv_t *bd = (bdrv_t *) b->sys;
290 static void cp_intr (void *arg)
293 cp_board_t *b = bd->board;
303 /* Check if we are ready */
304 if (b->sys == NULL) {
305 /* Not we are not, just cleanup. */
306 cp_interrupt_poll (b, 1);
315 /* Turn LED off 50 msec later. */
316 callout_reset (&led_timo[b->num], hz/20, cp_led_off, b);
321 /* Pass packets in a lock-free state */
322 for (i = 0; i < NCHAN && b->chan[i].type; i++) {
323 drv_t *d = b->chan[i].sys;
325 if (!d || !d->running)
327 while (_IF_QLEN(&d->queue)) {
328 IF_DEQUEUE (&d->queue,m);
331 sppp_input (d->ifp, m);
338 cp_bus_dmamap_addr (void *arg, bus_dma_segment_t *segs, int nseg, int error)
345 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
347 *addr = segs->ds_addr;
351 cp_bus_dma_mem_alloc (int bnum, int cnum, cp_dma_mem_t *dmem)
355 error = bus_dma_tag_create (NULL, 16, 0, BUS_SPACE_MAXADDR_32BIT,
356 BUS_SPACE_MAXADDR, NULL, NULL, dmem->size, 1,
357 dmem->size, 0, NULL, NULL, &dmem->dmat);
359 if (cnum >= 0) printf ("cp%d-%d: ", bnum, cnum);
360 else printf ("cp%d: ", bnum);
361 printf ("couldn't allocate tag for dma memory\n");
364 error = bus_dmamem_alloc (dmem->dmat, (void **)&dmem->virt,
365 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &dmem->mapp);
367 if (cnum >= 0) printf ("cp%d-%d: ", bnum, cnum);
368 else printf ("cp%d: ", bnum);
369 printf ("couldn't allocate mem for dma memory\n");
370 bus_dma_tag_destroy (dmem->dmat);
373 error = bus_dmamap_load (dmem->dmat, dmem->mapp, dmem->virt,
374 dmem->size, cp_bus_dmamap_addr, &dmem->phys, 0);
376 if (cnum >= 0) printf ("cp%d-%d: ", bnum, cnum);
377 else printf ("cp%d: ", bnum);
378 printf ("couldn't load mem map for dma memory\n");
379 bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
380 bus_dma_tag_destroy (dmem->dmat);
387 cp_bus_dma_mem_free (cp_dma_mem_t *dmem)
389 bus_dmamap_unload (dmem->dmat, dmem->mapp);
390 bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
391 bus_dma_tag_destroy (dmem->dmat);
395 * Called if the probe succeeded.
397 static int cp_attach (device_t dev)
399 bdrv_t *bd = device_get_softc (dev);
400 int unit = device_get_unit (dev);
401 char *cp_ln = CP_LOCK_NAME;
410 b = malloc (sizeof(cp_board_t), M_DEVBUF, M_WAITOK);
412 printf ("cp%d: couldn't allocate memory\n", unit);
416 bzero (b, sizeof(cp_board_t));
420 bd->cp_res = bus_alloc_resource (dev, SYS_RES_MEMORY, &rid,
421 0, ~0, 1, RF_ACTIVE);
423 printf ("cp%d: cannot map memory\n", unit);
428 vbase = (vm_offset_t) rman_get_virtual (bd->cp_res);
430 cp_ln[2] = '0' + unit;
431 mtx_init (&bd->cp_mtx, cp_ln, MTX_NETWORK_LOCK, MTX_DEF|MTX_RECURSE);
432 res = cp_init (b, unit, (u_char*) vbase);
434 printf ("cp%d: can't init, error code:%x\n", unit, res);
435 bus_release_resource (dev, SYS_RES_MEMORY, PCIR_BAR(0), bd->cp_res);
441 bd->dmamem.size = sizeof(cp_qbuf_t);
442 if (! cp_bus_dma_mem_alloc (unit, -1, &bd->dmamem)) {
448 cp_reset (b, bd->dmamem.virt, bd->dmamem.phys);
452 bd->cp_irq = bus_alloc_resource (dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
453 RF_SHAREABLE | RF_ACTIVE);
456 printf ("cp%d: cannot map interrupt\n", unit);
457 bus_release_resource (dev, SYS_RES_MEMORY,
458 PCIR_BAR(0), bd->cp_res);
459 mtx_destroy (&bd->cp_mtx);
464 callout_init (&led_timo[unit], CALLOUT_MPSAFE);
465 error = bus_setup_intr (dev, bd->cp_irq,
466 INTR_TYPE_NET|INTR_MPSAFE,
467 NULL, cp_intr, bd, &bd->cp_intrhand);
470 printf ("cp%d: cannot set up irq\n", unit);
471 bus_release_resource (dev, SYS_RES_IRQ, 0, bd->cp_irq);
472 bus_release_resource (dev, SYS_RES_MEMORY,
473 PCIR_BAR(0), bd->cp_res);
474 mtx_destroy (&bd->cp_mtx);
479 printf ("cp%d: %s, clock %ld MHz\n", unit, b->name, b->osc / 1000000);
481 for (c = b->chan; c < b->chan + NCHAN; ++c) {
484 d = &bd->channel[c->num];
485 d->dmamem.size = sizeof(cp_buf_t);
486 if (! cp_bus_dma_mem_alloc (unit, c->num, &d->dmamem))
488 channel [b->num*NCHAN + c->num] = d;
489 sprintf (d->name, "cp%d.%d", b->num, c->num);
493 callout_init (&d->timeout_handle, CALLOUT_MPSAFE);
495 if (ng_make_node_common (&typestruct, &d->node) != 0) {
496 printf ("%s: cannot make common node\n", d->name);
500 NG_NODE_SET_PRIVATE (d->node, d);
501 sprintf (d->nodename, "%s%d", NG_CP_NODE_TYPE,
502 c->board->num*NCHAN + c->num);
503 if (ng_name_node (d->node, d->nodename)) {
504 printf ("%s: cannot name node\n", d->nodename);
505 NG_NODE_UNREF (d->node);
508 d->queue.ifq_maxlen = ifqmaxlen;
509 d->hi_queue.ifq_maxlen = ifqmaxlen;
510 mtx_init (&d->queue.ifq_mtx, "cp_queue", NULL, MTX_DEF);
511 mtx_init (&d->hi_queue.ifq_mtx, "cp_queue_hi", NULL, MTX_DEF);
513 d->ifp = if_alloc(IFT_PPP);
514 if (d->ifp == NULL) {
515 printf ("%s: cannot if_alloc() interface\n", d->name);
518 d->ifp->if_softc = d;
519 if_initname (d->ifp, "cp", b->num * NCHAN + c->num);
520 d->ifp->if_mtu = PP_MTU;
521 d->ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
522 d->ifp->if_ioctl = cp_sioctl;
523 d->ifp->if_start = cp_ifstart;
524 d->ifp->if_init = cp_initialize;
525 d->queue.ifq_maxlen = NRBUF;
526 mtx_init (&d->queue.ifq_mtx, "cp_queue", NULL, MTX_DEF);
527 sppp_attach (d->ifp);
529 IFP2SP(d->ifp)->pp_tlf = cp_tlf;
530 IFP2SP(d->ifp)->pp_tls = cp_tls;
531 /* If BPF is in the kernel, call the attach for it.
532 * The header size of PPP or Cisco/HDLC is 4 bytes. */
533 bpfattach (d->ifp, DLT_PPP, 4);
536 cp_start_chan (c, 1, 1, d->dmamem.virt, d->dmamem.phys);
538 /* Register callback functions. */
539 cp_register_transmit (c, &cp_transmit);
540 cp_register_receive (c, &cp_receive);
541 cp_register_error (c, &cp_error);
542 d->devt = make_dev (&cp_cdevsw, b->num*NCHAN+c->num, UID_ROOT,
543 GID_WHEEL, 0600, "cp%d", b->num*NCHAN+c->num);
553 static int cp_detach (device_t dev)
555 bdrv_t *bd = device_get_softc (dev);
556 cp_board_t *b = bd->board;
560 KASSERT (mtx_initialized (&bd->cp_mtx), ("cp mutex not initialized"));
563 /* Check if the device is busy (open). */
564 for (c = b->chan; c < b->chan + NCHAN; ++c) {
565 drv_t *d = (drv_t*) c->sys;
567 if (! d || ! d->chan->type)
576 /* Ok, we can unload driver */
577 /* At first we should stop all channels */
578 for (c = b->chan; c < b->chan + NCHAN; ++c) {
579 drv_t *d = (drv_t*) c->sys;
581 if (! d || ! d->chan->type)
586 cp_set_dtr (d->chan, 0);
587 cp_set_rts (d->chan, 0);
590 /* Reset the adapter. */
592 cp_interrupt_poll (b, 1);
595 callout_stop (&led_timo[b->num]);
597 /* Disable the interrupt request. */
598 bus_teardown_intr (dev, bd->cp_irq, bd->cp_intrhand);
600 for (c=b->chan; c<b->chan+NCHAN; ++c) {
601 drv_t *d = (drv_t*) c->sys;
603 if (! d || ! d->chan->type)
605 callout_stop (&d->timeout_handle);
607 /* Detach from the packet filter list of interfaces. */
610 /* Detach from the sync PPP list. */
611 sppp_detach (d->ifp);
613 /* Detach from the system list of interfaces. */
616 IF_DRAIN (&d->queue);
617 mtx_destroy (&d->queue.ifq_mtx);
620 ng_rmnode_self (d->node);
621 NG_NODE_UNREF (d->node);
624 mtx_destroy (&d->queue.ifq_mtx);
625 mtx_destroy (&d->hi_queue.ifq_mtx);
627 destroy_dev (d->devt);
633 bus_release_resource (dev, SYS_RES_IRQ, 0, bd->cp_irq);
634 bus_release_resource (dev, SYS_RES_MEMORY, PCIR_BAR(0), bd->cp_res);
639 callout_drain (&led_timo[b->num]);
642 for (c = b->chan; c < b->chan + NCHAN; ++c) {
643 drv_t *d = (drv_t*) c->sys;
645 if (! d || ! d->chan->type)
647 callout_drain (&d->timeout_handle);
648 channel [b->num*NCHAN + c->num] = 0;
649 /* Deallocate buffers. */
650 cp_bus_dma_mem_free (&d->dmamem);
652 adapter [b->num] = 0;
653 cp_bus_dma_mem_free (&bd->dmamem);
655 mtx_destroy (&bd->cp_mtx);
660 static void cp_ifstart (struct ifnet *ifp)
662 drv_t *d = ifp->if_softc;
663 bdrv_t *bd = d->board->sys;
670 static void cp_tlf (struct sppp *sp)
672 drv_t *d = SP2IFP(sp)->if_softc;
674 CP_DEBUG2 (d, ("cp_tlf\n"));
675 /* XXXRIK: Don't forget to protect them by LOCK, or kill them. */
676 /* cp_set_dtr (d->chan, 0);*/
677 /* cp_set_rts (d->chan, 0);*/
678 if (!(sp->pp_flags & PP_FR) && !(d->ifp->if_flags & PP_CISCO))
682 static void cp_tls (struct sppp *sp)
684 drv_t *d = SP2IFP(sp)->if_softc;
686 CP_DEBUG2 (d, ("cp_tls\n"));
687 if (!(sp->pp_flags & PP_FR) && !(d->ifp->if_flags & PP_CISCO))
692 * Process an ioctl request.
694 static int cp_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data)
696 drv_t *d = ifp->if_softc;
697 bdrv_t *bd = d->board->sys;
698 int error, s, was_up, should_be_up;
700 was_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
701 error = sppp_ioctl (ifp, cmd, data);
706 if (! (ifp->if_flags & IFF_DEBUG))
709 d->chan->debug = d->chan->debug_shadow;
712 default: CP_DEBUG2 (d, ("ioctl 0x%lx\n", cmd)); return 0;
713 case SIOCADDMULTI: CP_DEBUG2 (d, ("ioctl SIOCADDMULTI\n")); return 0;
714 case SIOCDELMULTI: CP_DEBUG2 (d, ("ioctl SIOCDELMULTI\n")); return 0;
715 case SIOCSIFFLAGS: CP_DEBUG2 (d, ("ioctl SIOCSIFFLAGS\n")); break;
716 case SIOCSIFADDR: CP_DEBUG2 (d, ("ioctl SIOCSIFADDR\n")); break;
719 /* We get here only in case of SIFFLAGS or SIFADDR. */
722 should_be_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
723 if (! was_up && should_be_up) {
724 /* Interface goes up -- start it. */
727 } else if (was_up && ! should_be_up) {
728 /* Interface is going down -- stop it. */
729 /* if ((IFP2SP(ifp)->pp_flags & PP_FR) || (ifp->if_flags & PP_CISCO))*/
732 CP_DEBUG (d, ("ioctl 0x%lx p4\n", cmd));
739 * Initialization of interface.
740 * It seems to be never called by upper level?
742 static void cp_initialize (void *softc)
746 CP_DEBUG (d, ("cp_initialize\n"));
751 * Stop the interface. Called on splimp().
753 static void cp_down (drv_t *d)
755 CP_DEBUG (d, ("cp_down\n"));
756 /* Interface is going down -- stop it. */
757 cp_set_dtr (d->chan, 0);
758 cp_set_rts (d->chan, 0);
761 callout_stop (&d->timeout_handle);
765 * Start the interface. Called on splimp().
767 static void cp_up (drv_t *d)
769 CP_DEBUG (d, ("cp_up\n"));
770 cp_set_dtr (d->chan, 1);
771 cp_set_rts (d->chan, 1);
776 * Start output on the interface. Get another datagram to send
777 * off of the interface queue, and copy it to the interface
778 * before starting the output.
780 static void cp_send (drv_t *d)
785 CP_DEBUG2 (d, ("cp_send, tn=%d te=%d\n", d->chan->tn, d->chan->te));
787 /* No output if the interface is down. */
791 /* No output if the modem is off. */
792 if (! (d->chan->lloop || d->chan->type != T_SERIAL ||
793 cp_get_dsr (d->chan)))
796 while (cp_transmit_space (d->chan)) {
797 /* Get the packet to send. */
799 IF_DEQUEUE (&d->hi_queue, m);
801 IF_DEQUEUE (&d->queue, m);
803 m = sppp_dequeue (d->ifp);
808 BPF_MTAP (d->ifp, m);
810 len = m_length (m, NULL);
812 printf ("%s: too long packet: %d bytes: ",
814 else if (! m->m_next)
815 cp_send_packet (d->chan, (u_char*) mtod (m, caddr_t), len, 0);
817 u_char *buf = d->chan->tbuf[d->chan->te];
818 m_copydata (m, 0, len, buf);
819 cp_send_packet (d->chan, buf, len, 0);
822 /* Set up transmit timeout, if the transmit ring is not empty.*/
826 d->ifp->if_drv_flags |= IFF_DRV_OACTIVE;
831 * Start output on the interface.
832 * Always called on splimp().
834 static void cp_start (drv_t *d)
838 cp_set_dtr (d->chan, 1);
840 cp_set_rts (d->chan, 1);
842 callout_reset (&d->timeout_handle, hz, cp_watchdog_timer, d);
847 * Handle transmit timeouts.
848 * Recover after lost transmit interrupts.
849 * Always called on splimp().
851 static void cp_watchdog (drv_t *d)
853 CP_DEBUG (d, ("device timeout\n"));
855 cp_stop_chan (d->chan);
856 cp_stop_e1 (d->chan);
857 cp_start_e1 (d->chan);
858 cp_start_chan (d->chan, 1, 1, 0, 0);
859 cp_set_dtr (d->chan, 1);
860 cp_set_rts (d->chan, 1);
865 static void cp_watchdog_timer (void *arg)
868 bdrv_t *bd = d->board->sys;
875 callout_reset (&d->timeout_handle, hz, cp_watchdog_timer, d);
879 static void cp_transmit (cp_chan_t *c, void *attachment, int len)
885 ++d->ifp->if_opackets;
886 d->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
891 static void cp_receive (cp_chan_t *c, unsigned char *data, int len)
902 m = makembuf (data, len);
904 CP_DEBUG (d, ("no memory for packet\n"));
906 ++d->ifp->if_iqdrops;
913 m->m_pkthdr.rcvif = 0;
914 NG_SEND_DATA_ONLY (error, d->hook, m);
916 ++d->ifp->if_ipackets;
917 m->m_pkthdr.rcvif = d->ifp;
918 /* Check if there's a BPF listener on this interface.
919 * If so, hand off the raw packet to bpf. */
920 BPF_TAP (d->ifp, data, len);
921 IF_ENQUEUE (&d->queue, m);
925 static void cp_error (cp_chan_t *c, int data)
931 CP_DEBUG (d, ("frame error\n"));
933 ++d->ifp->if_ierrors;
937 CP_DEBUG (d, ("crc error\n"));
939 ++d->ifp->if_ierrors;
943 CP_DEBUG (d, ("overrun error\n"));
945 ++d->ifp->if_collisions;
946 ++d->ifp->if_ierrors;
950 CP_DEBUG (d, ("overflow error\n"));
952 ++d->ifp->if_ierrors;
956 CP_DEBUG (d, ("underrun error\n"));
959 ++d->ifp->if_oerrors;
960 d->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
965 CP_DEBUG (d, ("error #%d\n", data));
971 * You also need read, write, open, close routines.
972 * This should get you started
974 static int cp_open (struct cdev *dev, int oflags, int devtype, struct thread *td)
976 int unit = dev2unit (dev);
979 if (unit >= NBRD*NCHAN || ! (d = channel[unit]))
981 CP_DEBUG2 (d, ("cp_open\n"));
986 * Only called on the LAST close.
988 static int cp_close (struct cdev *dev, int fflag, int devtype, struct thread *td)
990 drv_t *d = channel [dev2unit (dev)];
992 CP_DEBUG2 (d, ("cp_close\n"));
996 static int cp_modem_status (cp_chan_t *c)
999 bdrv_t *bd = d->board->sys;
1002 status = d->running ? TIOCM_LE : 0;
1005 if (cp_get_cd (c)) status |= TIOCM_CD;
1006 if (cp_get_cts (c)) status |= TIOCM_CTS;
1007 if (cp_get_dsr (c)) status |= TIOCM_DSR;
1008 if (c->dtr) status |= TIOCM_DTR;
1009 if (c->rts) status |= TIOCM_RTS;
1015 static int cp_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
1017 drv_t *d = channel [dev2unit (dev)];
1018 bdrv_t *bd = d->board->sys;
1019 cp_chan_t *c = d->chan;
1020 struct serial_statistics *st;
1021 struct e1_statistics *opte1;
1022 struct e3_statistics *opte3;
1027 case SERIAL_GETREGISTERED:
1028 CP_DEBUG2 (d, ("ioctl: getregistered\n"));
1029 bzero (mask, sizeof(mask));
1030 for (s=0; s<NBRD*NCHAN; ++s)
1032 mask [s/8] |= 1 << (s & 7);
1033 bcopy (mask, data, sizeof (mask));
1037 case SERIAL_GETPROTO:
1038 CP_DEBUG2 (d, ("ioctl: getproto\n"));
1039 strcpy ((char*)data, (IFP2SP(d->ifp)->pp_flags & PP_FR) ? "fr" :
1040 (d->ifp->if_flags & PP_CISCO) ? "cisco" : "ppp");
1043 case SERIAL_SETPROTO:
1044 CP_DEBUG2 (d, ("ioctl: setproto\n"));
1045 /* Only for superuser! */
1046 error = priv_check (td, PRIV_DRIVER);
1049 if (d->ifp->if_drv_flags & IFF_DRV_RUNNING)
1051 if (! strcmp ("cisco", (char*)data)) {
1052 IFP2SP(d->ifp)->pp_flags &= ~(PP_FR);
1053 IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE;
1054 d->ifp->if_flags |= PP_CISCO;
1056 } else if (! strcmp ("fr", (char*)data)) {
1057 d->ifp->if_flags &= ~(PP_CISCO);
1058 IFP2SP(d->ifp)->pp_flags |= PP_FR | PP_KEEPALIVE;
1060 } else if (! strcmp ("ppp", (char*)data)) {
1061 IFP2SP(d->ifp)->pp_flags &= ~PP_FR;
1062 IFP2SP(d->ifp)->pp_flags &= ~PP_KEEPALIVE;
1063 d->ifp->if_flags &= ~(PP_CISCO);
1068 case SERIAL_GETKEEPALIVE:
1069 CP_DEBUG2 (d, ("ioctl: getkeepalive\n"));
1070 if ((IFP2SP(d->ifp)->pp_flags & PP_FR) ||
1071 (d->ifp->if_flags & PP_CISCO))
1073 *(int*)data = (IFP2SP(d->ifp)->pp_flags & PP_KEEPALIVE) ? 1 : 0;
1076 case SERIAL_SETKEEPALIVE:
1077 CP_DEBUG2 (d, ("ioctl: setkeepalive\n"));
1078 /* Only for superuser! */
1079 error = priv_check (td, PRIV_DRIVER);
1082 if ((IFP2SP(d->ifp)->pp_flags & PP_FR) ||
1083 (d->ifp->if_flags & PP_CISCO))
1088 IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE;
1090 IFP2SP(d->ifp)->pp_flags &= ~PP_KEEPALIVE;
1096 case SERIAL_GETMODE:
1097 CP_DEBUG2 (d, ("ioctl: getmode\n"));
1098 *(int*)data = SERIAL_HDLC;
1101 case SERIAL_SETMODE:
1102 /* Only for superuser! */
1103 error = priv_check (td, PRIV_DRIVER);
1106 if (*(int*)data != SERIAL_HDLC)
1111 CP_DEBUG2 (d, ("ioctl: getcfg\n"));
1112 if (c->type != T_E1 || c->unfram)
1114 *(char*)data = c->board->mux ? 'c' : 'a';
1118 CP_DEBUG2 (d, ("ioctl: setcfg\n"));
1119 error = priv_check (td, PRIV_DRIVER);
1122 if (c->type != T_E1)
1126 cp_set_mux (c->board, *((char*)data) == 'c');
1131 case SERIAL_GETSTAT:
1132 CP_DEBUG2 (d, ("ioctl: getstat\n"));
1133 st = (struct serial_statistics*) data;
1134 st->rintr = c->rintr;
1135 st->tintr = c->tintr;
1137 st->ibytes = c->ibytes;
1138 st->ipkts = c->ipkts;
1139 st->obytes = c->obytes;
1140 st->opkts = c->opkts;
1141 st->ierrs = c->overrun + c->frame + c->crc;
1142 st->oerrs = c->underrun;
1145 case SERIAL_GETESTAT:
1146 CP_DEBUG2 (d, ("ioctl: getestat\n"));
1147 if (c->type != T_E1 && c->type != T_G703)
1149 opte1 = (struct e1_statistics*) data;
1150 opte1->status = c->status;
1151 opte1->cursec = c->cursec;
1152 opte1->totsec = c->totsec + c->cursec;
1154 opte1->currnt.bpv = c->currnt.bpv;
1155 opte1->currnt.fse = c->currnt.fse;
1156 opte1->currnt.crce = c->currnt.crce;
1157 opte1->currnt.rcrce = c->currnt.rcrce;
1158 opte1->currnt.uas = c->currnt.uas;
1159 opte1->currnt.les = c->currnt.les;
1160 opte1->currnt.es = c->currnt.es;
1161 opte1->currnt.bes = c->currnt.bes;
1162 opte1->currnt.ses = c->currnt.ses;
1163 opte1->currnt.oofs = c->currnt.oofs;
1164 opte1->currnt.css = c->currnt.css;
1165 opte1->currnt.dm = c->currnt.dm;
1167 opte1->total.bpv = c->total.bpv + c->currnt.bpv;
1168 opte1->total.fse = c->total.fse + c->currnt.fse;
1169 opte1->total.crce = c->total.crce + c->currnt.crce;
1170 opte1->total.rcrce = c->total.rcrce + c->currnt.rcrce;
1171 opte1->total.uas = c->total.uas + c->currnt.uas;
1172 opte1->total.les = c->total.les + c->currnt.les;
1173 opte1->total.es = c->total.es + c->currnt.es;
1174 opte1->total.bes = c->total.bes + c->currnt.bes;
1175 opte1->total.ses = c->total.ses + c->currnt.ses;
1176 opte1->total.oofs = c->total.oofs + c->currnt.oofs;
1177 opte1->total.css = c->total.css + c->currnt.css;
1178 opte1->total.dm = c->total.dm + c->currnt.dm;
1179 for (s=0; s<48; ++s) {
1180 opte1->interval[s].bpv = c->interval[s].bpv;
1181 opte1->interval[s].fse = c->interval[s].fse;
1182 opte1->interval[s].crce = c->interval[s].crce;
1183 opte1->interval[s].rcrce = c->interval[s].rcrce;
1184 opte1->interval[s].uas = c->interval[s].uas;
1185 opte1->interval[s].les = c->interval[s].les;
1186 opte1->interval[s].es = c->interval[s].es;
1187 opte1->interval[s].bes = c->interval[s].bes;
1188 opte1->interval[s].ses = c->interval[s].ses;
1189 opte1->interval[s].oofs = c->interval[s].oofs;
1190 opte1->interval[s].css = c->interval[s].css;
1191 opte1->interval[s].dm = c->interval[s].dm;
1195 case SERIAL_GETE3STAT:
1196 CP_DEBUG2 (d, ("ioctl: gete3stat\n"));
1197 if (c->type != T_E3 && c->type != T_T3 && c->type != T_STS1)
1199 opte3 = (struct e3_statistics*) data;
1201 opte3->status = c->e3status;
1202 opte3->cursec = (c->e3csec_5 * 2 + 1) / 10;
1203 opte3->totsec = c->e3tsec + opte3->cursec;
1205 opte3->ccv = c->e3ccv;
1206 opte3->tcv = c->e3tcv + opte3->ccv;
1208 for (s = 0; s < 48; ++s) {
1209 opte3->icv[s] = c->e3icv[s];
1213 case SERIAL_CLRSTAT:
1214 CP_DEBUG2 (d, ("ioctl: clrstat\n"));
1215 /* Only for superuser! */
1216 error = priv_check (td, PRIV_DRIVER);
1229 bzero (&c->currnt, sizeof (c->currnt));
1230 bzero (&c->total, sizeof (c->total));
1231 bzero (c->interval, sizeof (c->interval));
1234 bzero (c->e3icv, sizeof (c->e3icv));
1237 case SERIAL_GETBAUD:
1238 CP_DEBUG2 (d, ("ioctl: getbaud\n"));
1239 *(long*)data = c->baud;
1242 case SERIAL_SETBAUD:
1243 CP_DEBUG2 (d, ("ioctl: setbaud\n"));
1244 /* Only for superuser! */
1245 error = priv_check (td, PRIV_DRIVER);
1250 cp_set_baud (c, *(long*)data);
1255 case SERIAL_GETLOOP:
1256 CP_DEBUG2 (d, ("ioctl: getloop\n"));
1257 *(int*)data = c->lloop;
1260 case SERIAL_SETLOOP:
1261 CP_DEBUG2 (d, ("ioctl: setloop\n"));
1262 /* Only for superuser! */
1263 error = priv_check (td, PRIV_DRIVER);
1268 cp_set_lloop (c, *(int*)data);
1273 case SERIAL_GETDPLL:
1274 CP_DEBUG2 (d, ("ioctl: getdpll\n"));
1275 if (c->type != T_SERIAL)
1277 *(int*)data = c->dpll;
1280 case SERIAL_SETDPLL:
1281 CP_DEBUG2 (d, ("ioctl: setdpll\n"));
1282 /* Only for superuser! */
1283 error = priv_check (td, PRIV_DRIVER);
1286 if (c->type != T_SERIAL)
1290 cp_set_dpll (c, *(int*)data);
1295 case SERIAL_GETNRZI:
1296 CP_DEBUG2 (d, ("ioctl: getnrzi\n"));
1297 if (c->type != T_SERIAL)
1299 *(int*)data = c->nrzi;
1302 case SERIAL_SETNRZI:
1303 CP_DEBUG2 (d, ("ioctl: setnrzi\n"));
1304 /* Only for superuser! */
1305 error = priv_check (td, PRIV_DRIVER);
1308 if (c->type != T_SERIAL)
1312 cp_set_nrzi (c, *(int*)data);
1317 case SERIAL_GETDEBUG:
1318 CP_DEBUG2 (d, ("ioctl: getdebug\n"));
1319 *(int*)data = d->chan->debug;
1322 case SERIAL_SETDEBUG:
1323 CP_DEBUG2 (d, ("ioctl: setdebug\n"));
1324 /* Only for superuser! */
1325 error = priv_check (td, PRIV_DRIVER);
1330 * The debug_shadow is always greater than zero for logic
1331 * simplicity. For switching debug off the IFF_DEBUG is
1334 d->chan->debug_shadow = (*(int*)data) ? (*(int*)data) : 1;
1335 if (d->ifp->if_flags & IFF_DEBUG)
1336 d->chan->debug = d->chan->debug_shadow;
1338 d->chan->debug = *(int*)data;
1342 case SERIAL_GETHIGAIN:
1343 CP_DEBUG2 (d, ("ioctl: gethigain\n"));
1344 if (c->type != T_E1)
1346 *(int*)data = c->higain;
1349 case SERIAL_SETHIGAIN:
1350 CP_DEBUG2 (d, ("ioctl: sethigain\n"));
1351 /* Only for superuser! */
1352 error = priv_check (td, PRIV_DRIVER);
1355 if (c->type != T_E1)
1359 cp_set_higain (c, *(int*)data);
1364 case SERIAL_GETPHONY:
1365 CP_DEBUG2 (d, ("ioctl: getphony\n"));
1366 if (c->type != T_E1)
1368 *(int*)data = c->phony;
1371 case SERIAL_SETPHONY:
1372 CP_DEBUG2 (d, ("ioctl: setphony\n"));
1373 /* Only for superuser! */
1374 error = priv_check (td, PRIV_DRIVER);
1377 if (c->type != T_E1)
1381 cp_set_phony (c, *(int*)data);
1386 case SERIAL_GETUNFRAM:
1387 CP_DEBUG2 (d, ("ioctl: getunfram\n"));
1388 if (c->type != T_E1)
1390 *(int*)data = c->unfram;
1393 case SERIAL_SETUNFRAM:
1394 CP_DEBUG2 (d, ("ioctl: setunfram\n"));
1395 /* Only for superuser! */
1396 error = priv_check (td, PRIV_DRIVER);
1399 if (c->type != T_E1)
1403 cp_set_unfram (c, *(int*)data);
1408 case SERIAL_GETSCRAMBLER:
1409 CP_DEBUG2 (d, ("ioctl: getscrambler\n"));
1410 if (c->type != T_G703 && !c->unfram)
1412 *(int*)data = c->scrambler;
1415 case SERIAL_SETSCRAMBLER:
1416 CP_DEBUG2 (d, ("ioctl: setscrambler\n"));
1417 /* Only for superuser! */
1418 error = priv_check (td, PRIV_DRIVER);
1421 if (c->type != T_G703 && !c->unfram)
1425 cp_set_scrambler (c, *(int*)data);
1430 case SERIAL_GETMONITOR:
1431 CP_DEBUG2 (d, ("ioctl: getmonitor\n"));
1432 if (c->type != T_E1 &&
1437 *(int*)data = c->monitor;
1440 case SERIAL_SETMONITOR:
1441 CP_DEBUG2 (d, ("ioctl: setmonitor\n"));
1442 /* Only for superuser! */
1443 error = priv_check (td, PRIV_DRIVER);
1446 if (c->type != T_E1)
1450 cp_set_monitor (c, *(int*)data);
1455 case SERIAL_GETUSE16:
1456 CP_DEBUG2 (d, ("ioctl: getuse16\n"));
1457 if (c->type != T_E1 || c->unfram)
1459 *(int*)data = c->use16;
1462 case SERIAL_SETUSE16:
1463 CP_DEBUG2 (d, ("ioctl: setuse16\n"));
1464 /* Only for superuser! */
1465 error = priv_check (td, PRIV_DRIVER);
1468 if (c->type != T_E1)
1472 cp_set_use16 (c, *(int*)data);
1477 case SERIAL_GETCRC4:
1478 CP_DEBUG2 (d, ("ioctl: getcrc4\n"));
1479 if (c->type != T_E1 || c->unfram)
1481 *(int*)data = c->crc4;
1484 case SERIAL_SETCRC4:
1485 CP_DEBUG2 (d, ("ioctl: setcrc4\n"));
1486 /* Only for superuser! */
1487 error = priv_check (td, PRIV_DRIVER);
1490 if (c->type != T_E1)
1494 cp_set_crc4 (c, *(int*)data);
1500 CP_DEBUG2 (d, ("ioctl: getclk\n"));
1501 if (c->type != T_E1 &&
1502 c->type != T_G703 &&
1508 default: *(int*)data = E1CLK_INTERNAL; break;
1509 case GSYN_RCV: *(int*)data = E1CLK_RECEIVE; break;
1510 case GSYN_RCV0: *(int*)data = E1CLK_RECEIVE_CHAN0; break;
1511 case GSYN_RCV1: *(int*)data = E1CLK_RECEIVE_CHAN1; break;
1512 case GSYN_RCV2: *(int*)data = E1CLK_RECEIVE_CHAN2; break;
1513 case GSYN_RCV3: *(int*)data = E1CLK_RECEIVE_CHAN3; break;
1518 CP_DEBUG2 (d, ("ioctl: setclk\n"));
1519 /* Only for superuser! */
1520 error = priv_check (td, PRIV_DRIVER);
1523 if (c->type != T_E1 &&
1524 c->type != T_G703 &&
1531 switch (*(int*)data) {
1532 default: cp_set_gsyn (c, GSYN_INT); break;
1533 case E1CLK_RECEIVE: cp_set_gsyn (c, GSYN_RCV); break;
1534 case E1CLK_RECEIVE_CHAN0: cp_set_gsyn (c, GSYN_RCV0); break;
1535 case E1CLK_RECEIVE_CHAN1: cp_set_gsyn (c, GSYN_RCV1); break;
1536 case E1CLK_RECEIVE_CHAN2: cp_set_gsyn (c, GSYN_RCV2); break;
1537 case E1CLK_RECEIVE_CHAN3: cp_set_gsyn (c, GSYN_RCV3); break;
1543 case SERIAL_GETTIMESLOTS:
1544 CP_DEBUG2 (d, ("ioctl: gettimeslots\n"));
1545 if ((c->type != T_E1 || c->unfram) && c->type != T_DATA)
1547 *(u_long*)data = c->ts;
1550 case SERIAL_SETTIMESLOTS:
1551 CP_DEBUG2 (d, ("ioctl: settimeslots\n"));
1552 /* Only for superuser! */
1553 error = priv_check (td, PRIV_DRIVER);
1556 if ((c->type != T_E1 || c->unfram) && c->type != T_DATA)
1560 cp_set_ts (c, *(u_long*)data);
1565 case SERIAL_GETINVCLK:
1566 CP_DEBUG2 (d, ("ioctl: getinvclk\n"));
1570 if (c->type != T_SERIAL)
1572 *(int*)data = c->invtxc;
1576 case SERIAL_SETINVCLK:
1577 CP_DEBUG2 (d, ("ioctl: setinvclk\n"));
1578 /* Only for superuser! */
1579 error = priv_check (td, PRIV_DRIVER);
1582 if (c->type != T_SERIAL)
1586 cp_set_invtxc (c, *(int*)data);
1587 cp_set_invrxc (c, *(int*)data);
1592 case SERIAL_GETINVTCLK:
1593 CP_DEBUG2 (d, ("ioctl: getinvtclk\n"));
1594 if (c->type != T_SERIAL)
1596 *(int*)data = c->invtxc;
1599 case SERIAL_SETINVTCLK:
1600 CP_DEBUG2 (d, ("ioctl: setinvtclk\n"));
1601 /* Only for superuser! */
1602 error = priv_check (td, PRIV_DRIVER);
1605 if (c->type != T_SERIAL)
1609 cp_set_invtxc (c, *(int*)data);
1614 case SERIAL_GETINVRCLK:
1615 CP_DEBUG2 (d, ("ioctl: getinvrclk\n"));
1616 if (c->type != T_SERIAL)
1618 *(int*)data = c->invrxc;
1621 case SERIAL_SETINVRCLK:
1622 CP_DEBUG2 (d, ("ioctl: setinvrclk\n"));
1623 /* Only for superuser! */
1624 error = priv_check (td, PRIV_DRIVER);
1627 if (c->type != T_SERIAL)
1631 cp_set_invrxc (c, *(int*)data);
1636 case SERIAL_GETLEVEL:
1637 CP_DEBUG2 (d, ("ioctl: getlevel\n"));
1638 if (c->type != T_G703)
1642 *(int*)data = cp_get_lq (c);
1649 CP_DEBUG2 (d, ("ioctl: reset\n"));
1650 /* Only for superuser! */
1651 error = priv_check (td, PRIV_DRIVER);
1656 cp_reset (c->board, 0, 0);
1661 case SERIAL_HARDRESET:
1662 CP_DEBUG2 (d, ("ioctl: hardreset\n"));
1663 /* Only for superuser! */
1664 error = priv_check (td, PRIV_DRIVER);
1669 /* hard_reset (c->board); */
1675 case SERIAL_GETCABLE:
1676 CP_DEBUG2 (d, ("ioctl: getcable\n"));
1677 if (c->type != T_SERIAL)
1681 *(int*)data = cp_get_cable (c);
1687 CP_DEBUG2 (d, ("ioctl: getdir\n"));
1688 if (c->type != T_E1 && c->type != T_DATA)
1690 *(int*)data = c->dir;
1694 CP_DEBUG2 (d, ("ioctl: setdir\n"));
1695 /* Only for superuser! */
1696 error = priv_check (td, PRIV_DRIVER);
1701 cp_set_dir (c, *(int*)data);
1706 case SERIAL_GETRLOOP:
1707 CP_DEBUG2 (d, ("ioctl: getrloop\n"));
1708 if (c->type != T_G703 &&
1713 *(int*)data = cp_get_rloop (c);
1716 case SERIAL_SETRLOOP:
1717 CP_DEBUG2 (d, ("ioctl: setloop\n"));
1718 if (c->type != T_E3 && c->type != T_T3 && c->type != T_STS1)
1720 /* Only for superuser! */
1721 error = priv_check (td, PRIV_DRIVER);
1726 cp_set_rloop (c, *(int*)data);
1731 case SERIAL_GETCABLEN:
1732 CP_DEBUG2 (d, ("ioctl: getcablen\n"));
1733 if (c->type != T_T3 && c->type != T_STS1)
1735 *(int*)data = c->cablen;
1738 case SERIAL_SETCABLEN:
1739 CP_DEBUG2 (d, ("ioctl: setloop\n"));
1740 if (c->type != T_T3 && c->type != T_STS1)
1742 /* Only for superuser! */
1743 error = priv_check (td, PRIV_DRIVER);
1748 cp_set_cablen (c, *(int*)data);
1753 case TIOCSDTR: /* Set DTR */
1761 case TIOCCDTR: /* Clear DTR */
1769 case TIOCMSET: /* Set DTR/RTS */
1772 cp_set_dtr (c, (*(int*)data & TIOCM_DTR) ? 1 : 0);
1773 cp_set_rts (c, (*(int*)data & TIOCM_RTS) ? 1 : 0);
1778 case TIOCMBIS: /* Add DTR/RTS */
1781 if (*(int*)data & TIOCM_DTR) cp_set_dtr (c, 1);
1782 if (*(int*)data & TIOCM_RTS) cp_set_rts (c, 1);
1787 case TIOCMBIC: /* Clear DTR/RTS */
1790 if (*(int*)data & TIOCM_DTR) cp_set_dtr (c, 0);
1791 if (*(int*)data & TIOCM_RTS) cp_set_rts (c, 0);
1796 case TIOCMGET: /* Get modem status */
1797 *(int*)data = cp_modem_status (c);
1804 static int ng_cp_constructor (node_p node)
1806 drv_t *d = NG_NODE_PRIVATE (node);
1807 CP_DEBUG (d, ("Constructor\n"));
1811 static int ng_cp_newhook (node_p node, hook_p hook, const char *name)
1814 drv_t *d = NG_NODE_PRIVATE (node);
1815 bdrv_t *bd = d->board->sys;
1817 CP_DEBUG (d, ("Newhook\n"));
1818 /* Attach debug hook */
1819 if (strcmp (name, NG_CP_HOOK_DEBUG) == 0) {
1820 NG_HOOK_SET_PRIVATE (hook, NULL);
1821 d->debug_hook = hook;
1825 /* Check for raw hook */
1826 if (strcmp (name, NG_CP_HOOK_RAW) != 0)
1829 NG_HOOK_SET_PRIVATE (hook, d);
1839 static char *format_timeslots (u_long s)
1841 static char buf [100];
1845 for (i=1; i<32; ++i)
1847 int prev = (i > 1) & (s >> (i-1));
1848 int next = (i < 31) & (s >> (i+1));
1858 *p++ = '0' + i / 10;
1859 *p++ = '0' + i % 10;
1865 static int print_modems (char *s, cp_chan_t *c, int need_header)
1867 int status = cp_modem_status (c);
1871 length += sprintf (s + length, " LE DTR DSR RTS CTS CD\n");
1872 length += sprintf (s + length, "%4s %4s %4s %4s %4s %4s\n",
1873 status & TIOCM_LE ? "On" : "-",
1874 status & TIOCM_DTR ? "On" : "-",
1875 status & TIOCM_DSR ? "On" : "-",
1876 status & TIOCM_RTS ? "On" : "-",
1877 status & TIOCM_CTS ? "On" : "-",
1878 status & TIOCM_CD ? "On" : "-");
1882 static int print_stats (char *s, cp_chan_t *c, int need_header)
1887 length += sprintf (s + length, " Rintr Tintr Mintr Ibytes Ipkts Ierrs Obytes Opkts Oerrs\n");
1888 length += sprintf (s + length, "%7ld %7ld %7ld %8lu %7ld %7ld %8lu %7ld %7ld\n",
1889 c->rintr, c->tintr, 0l, (unsigned long) c->ibytes,
1890 c->ipkts, c->overrun + c->frame + c->crc,
1891 (unsigned long) c->obytes, c->opkts, c->underrun);
1895 static char *format_e1_status (u_char status)
1897 static char buf [80];
1899 if (status & E1_NOALARM)
1902 if (status & E1_LOS) strcat (buf, ",LOS");
1903 if (status & E1_AIS) strcat (buf, ",AIS");
1904 if (status & E1_LOF) strcat (buf, ",LOF");
1905 if (status & E1_LOMF) strcat (buf, ",LOMF");
1906 if (status & E1_FARLOF) strcat (buf, ",FARLOF");
1907 if (status & E1_AIS16) strcat (buf, ",AIS16");
1908 if (status & E1_FARLOMF) strcat (buf, ",FARLOMF");
1909 if (status & E1_TSTREQ) strcat (buf, ",TSTREQ");
1910 if (status & E1_TSTERR) strcat (buf, ",TSTERR");
1916 static int print_frac (char *s, int leftalign, u_long numerator, u_long divider)
1920 if (numerator < 1 || divider < 1) {
1921 length += sprintf (s+length, leftalign ? "/- " : " -");
1924 n = (int) (0.5 + 1000.0 * numerator / divider);
1926 length += sprintf (s+length, leftalign ? "/.%-3d" : " .%03d", n);
1929 *(s + length) = leftalign ? '/' : ' ';
1932 if (n >= 1000000) n = (n+500) / 1000 * 1000;
1933 else if (n >= 100000) n = (n+50) / 100 * 100;
1934 else if (n >= 10000) n = (n+5) / 10 * 10;
1937 case 1000: length += printf (s+length, ".999"); return length;
1938 case 10000: n = 9990; break;
1939 case 100000: n = 99900; break;
1940 case 1000000: n = 999000; break;
1942 if (n < 10000) length += sprintf (s+length, "%d.%d", n/1000, n/10%100);
1943 else if (n < 100000) length += sprintf (s+length, "%d.%d", n/1000, n/100%10);
1944 else if (n < 1000000) length += sprintf (s+length, "%d.", n/1000);
1945 else length += sprintf (s+length, "%d", n/1000);
1950 static int print_e1_stats (char *s, cp_chan_t *c)
1952 struct e1_counters total;
1956 totsec = c->totsec + c->cursec;
1957 total.bpv = c->total.bpv + c->currnt.bpv;
1958 total.fse = c->total.fse + c->currnt.fse;
1959 total.crce = c->total.crce + c->currnt.crce;
1960 total.rcrce = c->total.rcrce + c->currnt.rcrce;
1961 total.uas = c->total.uas + c->currnt.uas;
1962 total.les = c->total.les + c->currnt.les;
1963 total.es = c->total.es + c->currnt.es;
1964 total.bes = c->total.bes + c->currnt.bes;
1965 total.ses = c->total.ses + c->currnt.ses;
1966 total.oofs = c->total.oofs + c->currnt.oofs;
1967 total.css = c->total.css + c->currnt.css;
1968 total.dm = c->total.dm + c->currnt.dm;
1970 length += sprintf (s + length, " Unav/Degr Bpv/Fsyn CRC/RCRC Err/Lerr Sev/Bur Oof/Slp Status\n");
1972 /* Unavailable seconds, degraded minutes */
1973 length += print_frac (s + length, 0, c->currnt.uas, c->cursec);
1974 length += print_frac (s + length, 1, 60 * c->currnt.dm, c->cursec);
1976 /* Bipolar violations, frame sync errors */
1977 length += print_frac (s + length, 0, c->currnt.bpv, c->cursec);
1978 length += print_frac (s + length, 1, c->currnt.fse, c->cursec);
1980 /* CRC errors, remote CRC errors (E-bit) */
1981 length += print_frac (s + length, 0, c->currnt.crce, c->cursec);
1982 length += print_frac (s + length, 1, c->currnt.rcrce, c->cursec);
1984 /* Errored seconds, line errored seconds */
1985 length += print_frac (s + length, 0, c->currnt.es, c->cursec);
1986 length += print_frac (s + length, 1, c->currnt.les, c->cursec);
1988 /* Severely errored seconds, burst errored seconds */
1989 length += print_frac (s + length, 0, c->currnt.ses, c->cursec);
1990 length += print_frac (s + length, 1, c->currnt.bes, c->cursec);
1992 /* Out of frame seconds, controlled slip seconds */
1993 length += print_frac (s + length, 0, c->currnt.oofs, c->cursec);
1994 length += print_frac (s + length, 1, c->currnt.css, c->cursec);
1996 length += sprintf (s + length, " %s\n", format_e1_status (c->status));
1998 /* Print total statistics. */
1999 length += print_frac (s + length, 0, total.uas, totsec);
2000 length += print_frac (s + length, 1, 60 * total.dm, totsec);
2002 length += print_frac (s + length, 0, total.bpv, totsec);
2003 length += print_frac (s + length, 1, total.fse, totsec);
2005 length += print_frac (s + length, 0, total.crce, totsec);
2006 length += print_frac (s + length, 1, total.rcrce, totsec);
2008 length += print_frac (s + length, 0, total.es, totsec);
2009 length += print_frac (s + length, 1, total.les, totsec);
2011 length += print_frac (s + length, 0, total.ses, totsec);
2012 length += print_frac (s + length, 1, total.bes, totsec);
2014 length += print_frac (s + length, 0, total.oofs, totsec);
2015 length += print_frac (s + length, 1, total.css, totsec);
2017 length += sprintf (s + length, " -- Total\n");
2021 static int print_chan (char *s, cp_chan_t *c)
2024 bdrv_t *bd = d->board->sys;
2027 length += sprintf (s + length, "cp%d", c->board->num * NCHAN + c->num);
2029 length += sprintf (s + length, " debug=%d", d->chan->debug);
2031 if (c->board->mux) {
2032 length += sprintf (s + length, " cfg=C");
2034 length += sprintf (s + length, " cfg=A");
2038 length += sprintf (s + length, " %ld", c->baud);
2040 length += sprintf (s + length, " extclock");
2042 if (c->type == T_E1 || c->type == T_G703)
2044 case GSYN_INT : length += sprintf (s + length, " syn=int"); break;
2045 case GSYN_RCV : length += sprintf (s + length, " syn=rcv"); break;
2046 case GSYN_RCV0 : length += sprintf (s + length, " syn=rcv0"); break;
2047 case GSYN_RCV1 : length += sprintf (s + length, " syn=rcv1"); break;
2048 case GSYN_RCV2 : length += sprintf (s + length, " syn=rcv2"); break;
2049 case GSYN_RCV3 : length += sprintf (s + length, " syn=rcv3"); break;
2051 if (c->type == T_SERIAL) {
2052 length += sprintf (s + length, " dpll=%s", c->dpll ? "on" : "off");
2053 length += sprintf (s + length, " nrzi=%s", c->nrzi ? "on" : "off");
2054 length += sprintf (s + length, " invclk=%s", c->invtxc ? "on" : "off");
2056 if (c->type == T_E1)
2057 length += sprintf (s + length, " higain=%s", c->higain ? "on" : "off");
2059 length += sprintf (s + length, " loop=%s", c->lloop ? "on" : "off");
2061 if (c->type == T_E1)
2062 length += sprintf (s + length, " ts=%s", format_timeslots (c->ts));
2063 if (c->type == T_G703) {
2071 length += sprintf (s + length, " (level=-%.1fdB)", lq / 10.0);
2073 length += sprintf (s + length, "\n");
2077 static int ng_cp_rcvmsg (node_p node, item_p item, hook_p lasthook)
2079 drv_t *d = NG_NODE_PRIVATE (node);
2080 struct ng_mesg *msg;
2081 struct ng_mesg *resp = NULL;
2084 CP_DEBUG (d, ("Rcvmsg\n"));
2085 NGI_GET_MSG (item, msg);
2086 switch (msg->header.typecookie) {
2092 printf ("Not implemented yet\n");
2096 case NGM_GENERIC_COOKIE:
2097 switch (msg->header.cmd) {
2102 case NGM_TEXT_STATUS: {
2105 int dl = sizeof (struct ng_mesg) + 730;
2107 NG_MKRESPONSE (resp, msg, dl, M_NOWAIT);
2114 l += print_chan (s + l, d->chan);
2115 l += print_stats (s + l, d->chan, 1);
2116 l += print_modems (s + l, d->chan, 1);
2117 l += print_e1_stats (s + l, d->chan);
2119 l += sprintf (s + l, "Error: node not connect to channel");
2120 strncpy ((resp)->header.cmdstr, "status", NG_CMDSTRSIZ);
2126 NG_RESPOND_MSG (error, node, item, resp);
2131 static int ng_cp_rcvdata (hook_p hook, item_p item)
2133 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE(hook));
2135 struct ng_tag_prio *ptag;
2136 bdrv_t *bd = d->board->sys;
2140 CP_DEBUG2 (d, ("Rcvdata\n"));
2141 NGI_GET_M (item, m);
2142 NG_FREE_ITEM (item);
2143 if (! NG_HOOK_PRIVATE (hook) || ! d) {
2148 /* Check for high priority data */
2149 if ((ptag = (struct ng_tag_prio *)m_tag_locate(m, NGM_GENERIC_COOKIE,
2150 NG_TAG_PRIO, NULL)) != NULL && (ptag->priority > NG_PRIO_CUTOFF) )
2158 if (_IF_QFULL (q)) {
2174 static int ng_cp_rmnode (node_p node)
2176 drv_t *d = NG_NODE_PRIVATE (node);
2178 CP_DEBUG (d, ("Rmnode\n"));
2179 if (d && d->running) {
2180 bdrv_t *bd = d->board->sys;
2188 if (node->nd_flags & NGF_REALLY_DIE) {
2189 NG_NODE_SET_PRIVATE (node, NULL);
2190 NG_NODE_UNREF (node);
2192 NG_NODE_REVIVE(node); /* Persistant node */
2197 static int ng_cp_connect (hook_p hook)
2199 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2202 CP_DEBUG (d, ("Connect\n"));
2203 callout_reset (&d->timeout_handle, hz, cp_watchdog_timer, d);
2209 static int ng_cp_disconnect (hook_p hook)
2211 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2214 CP_DEBUG (d, ("Disconnect\n"));
2215 if (NG_HOOK_PRIVATE (hook))
2217 bdrv_t *bd = d->board->sys;
2224 /* If we were wait it than it reasserted now, just stop it. */
2225 if (!callout_drain (&d->timeout_handle))
2226 callout_stop (&d->timeout_handle);
2232 static int cp_modevent (module_t mod, int type, void *unused)
2234 static int load_count = 0;
2239 if (ng_newtype (&typestruct))
2240 printf ("Failed to register ng_cp\n");
2243 callout_init (&timeout_handle, CALLOUT_MPSAFE);
2244 callout_reset (&timeout_handle, hz*5, cp_timeout, 0);
2247 if (load_count == 1) {
2248 printf ("Removing device entry for Tau-PCI\n");
2250 ng_rmtype (&typestruct);
2253 /* If we were wait it than it reasserted now, just stop it.
2254 * Actually we shouldn't get this condition. But code could be
2255 * changed in the future, so just be a litle paranoid.
2257 if (!callout_drain (&timeout_handle))
2258 callout_stop (&timeout_handle);
2268 static struct ng_type typestruct = {
2269 .version = NG_ABI_VERSION,
2270 .name = NG_CP_NODE_TYPE,
2271 .constructor = ng_cp_constructor,
2272 .rcvmsg = ng_cp_rcvmsg,
2273 .shutdown = ng_cp_rmnode,
2274 .newhook = ng_cp_newhook,
2275 .connect = ng_cp_connect,
2276 .rcvdata = ng_cp_rcvdata,
2277 .disconnect = ng_cp_disconnect,
2282 MODULE_DEPEND (ng_cp, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION);
2284 MODULE_DEPEND (cp, sppp, 1, 1, 1);
2286 DRIVER_MODULE (cp, pci, cp_driver, cp_devclass, cp_modevent, NULL);
2287 MODULE_VERSION (cp, 1);