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/cx/machdep.h>
67 #include <dev/cp/cpddk.h>
68 #include <machine/cserial.h>
69 #include <machine/resource.h>
70 #include <machine/pmap.h>
72 /* If we don't have Cronyx's sppp version, we don't have fr support via sppp */
77 #define CP_DEBUG(d,s) ({if (d->chan->debug) {\
78 printf ("%s: ", d->name); printf s;}})
79 #define CP_DEBUG2(d,s) ({if (d->chan->debug>1) {\
80 printf ("%s: ", d->name); printf s;}})
81 #define CP_LOCK_NAME "cpX"
83 #define CP_LOCK(_bd) mtx_lock (&(_bd)->cp_mtx)
84 #define CP_UNLOCK(_bd) mtx_unlock (&(_bd)->cp_mtx)
85 #define CP_LOCK_ASSERT(_bd) mtx_assert (&(_bd)->cp_mtx, MA_OWNED)
87 static int cp_probe __P((device_t));
88 static int cp_attach __P((device_t));
89 static int cp_detach __P((device_t));
91 static device_method_t cp_methods[] = {
92 /* Device interface */
93 DEVMETHOD(device_probe, cp_probe),
94 DEVMETHOD(device_attach, cp_attach),
95 DEVMETHOD(device_detach, cp_detach),
100 typedef struct _cp_dma_mem_t {
108 typedef struct _drv_t {
115 char nodename [NG_NODESIZE];
119 struct ifqueue queue;
120 struct ifqueue hi_queue;
122 struct ifqueue queue;
126 struct callout timeout_handle;
130 typedef struct _bdrv_t {
132 struct resource *cp_res;
133 struct resource *cp_irq;
136 drv_t channel [NCHAN];
140 static driver_t cp_driver = {
146 static devclass_t cp_devclass;
148 static void cp_receive (cp_chan_t *c, unsigned char *data, int len);
149 static void cp_transmit (cp_chan_t *c, void *attachment, int len);
150 static void cp_error (cp_chan_t *c, int data);
151 static void cp_up (drv_t *d);
152 static void cp_start (drv_t *d);
153 static void cp_down (drv_t *d);
154 static void cp_watchdog (drv_t *d);
155 static void cp_watchdog_timer (void *arg);
157 extern struct ng_type typestruct;
159 static void cp_ifstart (struct ifnet *ifp);
160 static void cp_tlf (struct sppp *sp);
161 static void cp_tls (struct sppp *sp);
162 static int cp_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data);
163 static void cp_initialize (void *softc);
166 static cp_board_t *adapter [NBRD];
167 static drv_t *channel [NBRD*NCHAN];
168 static struct callout led_timo [NBRD];
169 static struct callout timeout_handle;
171 static int cp_destroy = 0;
173 static int cp_open (struct cdev *dev, int oflags, int devtype, struct thread *td);
174 static int cp_close (struct cdev *dev, int fflag, int devtype, struct thread *td);
175 static int cp_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td);
176 static struct cdevsw cp_cdevsw = {
177 .d_version = D_VERSION,
185 * Make an mbuf from data.
187 static struct mbuf *makembuf (void *buf, unsigned len)
191 MGETHDR (m, M_NOWAIT, MT_DATA);
194 if (!(MCLGET (m, M_NOWAIT))) {
198 m->m_pkthdr.len = m->m_len = len;
199 bcopy (buf, mtod (m, caddr_t), len);
203 static int cp_probe (device_t dev)
205 if ((pci_get_vendor (dev) == cp_vendor_id) &&
206 (pci_get_device (dev) == cp_device_id)) {
207 device_set_desc (dev, "Cronyx-Tau-PCI serial adapter");
208 return BUS_PROBE_DEFAULT;
213 static void cp_timeout (void *arg)
218 for (i = 0; i < NBRD; ++i) {
219 if (adapter[i] == NULL)
221 for (k = 0; k < NCHAN; ++k) {
227 d = channel[i * NCHAN + k];
232 CP_LOCK ((bdrv_t *)d->board->sys);
233 switch (d->chan->type) {
235 cp_g703_timer (d->chan);
238 cp_e1_timer (d->chan);
243 cp_e3_timer (d->chan);
248 CP_UNLOCK ((bdrv_t *)d->board->sys);
254 callout_reset (&timeout_handle, hz, cp_timeout, 0);
258 static void cp_led_off (void *arg)
261 bdrv_t *bd = (bdrv_t *) b->sys;
274 static void cp_intr (void *arg)
277 cp_board_t *b = bd->board;
287 /* Check if we are ready */
288 if (b->sys == NULL) {
289 /* Not we are not, just cleanup. */
290 cp_interrupt_poll (b, 1);
299 /* Turn LED off 50 msec later. */
300 callout_reset (&led_timo[b->num], hz/20, cp_led_off, b);
305 /* Pass packets in a lock-free state */
306 for (i = 0; i < NCHAN && b->chan[i].type; i++) {
307 drv_t *d = b->chan[i].sys;
309 if (!d || !d->running)
311 while (_IF_QLEN(&d->queue)) {
312 IF_DEQUEUE (&d->queue,m);
315 sppp_input (d->ifp, m);
322 cp_bus_dmamap_addr (void *arg, bus_dma_segment_t *segs, int nseg, int error)
329 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
331 *addr = segs->ds_addr;
335 cp_bus_dma_mem_alloc (int bnum, int cnum, cp_dma_mem_t *dmem)
339 error = bus_dma_tag_create (NULL, 16, 0, BUS_SPACE_MAXADDR_32BIT,
340 BUS_SPACE_MAXADDR, NULL, NULL, dmem->size, 1,
341 dmem->size, 0, NULL, NULL, &dmem->dmat);
343 if (cnum >= 0) printf ("cp%d-%d: ", bnum, cnum);
344 else printf ("cp%d: ", bnum);
345 printf ("couldn't allocate tag for dma memory\n");
348 error = bus_dmamem_alloc (dmem->dmat, (void **)&dmem->virt,
349 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &dmem->mapp);
351 if (cnum >= 0) printf ("cp%d-%d: ", bnum, cnum);
352 else printf ("cp%d: ", bnum);
353 printf ("couldn't allocate mem for dma memory\n");
354 bus_dma_tag_destroy (dmem->dmat);
357 error = bus_dmamap_load (dmem->dmat, dmem->mapp, dmem->virt,
358 dmem->size, cp_bus_dmamap_addr, &dmem->phys, 0);
360 if (cnum >= 0) printf ("cp%d-%d: ", bnum, cnum);
361 else printf ("cp%d: ", bnum);
362 printf ("couldn't load mem map for dma memory\n");
363 bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
364 bus_dma_tag_destroy (dmem->dmat);
371 cp_bus_dma_mem_free (cp_dma_mem_t *dmem)
373 bus_dmamap_unload (dmem->dmat, dmem->mapp);
374 bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
375 bus_dma_tag_destroy (dmem->dmat);
379 * Called if the probe succeeded.
381 static int cp_attach (device_t dev)
383 bdrv_t *bd = device_get_softc (dev);
384 int unit = device_get_unit (dev);
385 char *cp_ln = CP_LOCK_NAME;
394 b = malloc (sizeof(cp_board_t), M_DEVBUF, M_WAITOK);
396 printf ("cp%d: couldn't allocate memory\n", unit);
400 bzero (b, sizeof(cp_board_t));
404 bd->cp_res = bus_alloc_resource (dev, SYS_RES_MEMORY, &rid,
405 0, ~0, 1, RF_ACTIVE);
407 printf ("cp%d: cannot map memory\n", unit);
412 vbase = (vm_offset_t) rman_get_virtual (bd->cp_res);
414 cp_ln[2] = '0' + unit;
415 mtx_init (&bd->cp_mtx, cp_ln, MTX_NETWORK_LOCK, MTX_DEF|MTX_RECURSE);
416 res = cp_init (b, unit, (u_char*) vbase);
418 printf ("cp%d: can't init, error code:%x\n", unit, res);
419 bus_release_resource (dev, SYS_RES_MEMORY, PCIR_BAR(0), bd->cp_res);
425 bd->dmamem.size = sizeof(cp_qbuf_t);
426 if (! cp_bus_dma_mem_alloc (unit, -1, &bd->dmamem)) {
432 cp_reset (b, bd->dmamem.virt, bd->dmamem.phys);
436 bd->cp_irq = bus_alloc_resource (dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
437 RF_SHAREABLE | RF_ACTIVE);
440 printf ("cp%d: cannot map interrupt\n", unit);
441 bus_release_resource (dev, SYS_RES_MEMORY,
442 PCIR_BAR(0), bd->cp_res);
443 mtx_destroy (&bd->cp_mtx);
448 callout_init (&led_timo[unit], 1);
449 error = bus_setup_intr (dev, bd->cp_irq,
450 INTR_TYPE_NET|INTR_MPSAFE,
451 NULL, cp_intr, bd, &bd->cp_intrhand);
454 printf ("cp%d: cannot set up irq\n", unit);
455 bus_release_resource (dev, SYS_RES_IRQ, 0, bd->cp_irq);
456 bus_release_resource (dev, SYS_RES_MEMORY,
457 PCIR_BAR(0), bd->cp_res);
458 mtx_destroy (&bd->cp_mtx);
463 printf ("cp%d: %s, clock %ld MHz\n", unit, b->name, b->osc / 1000000);
465 for (c = b->chan; c < b->chan + NCHAN; ++c) {
468 d = &bd->channel[c->num];
469 d->dmamem.size = sizeof(cp_buf_t);
470 if (! cp_bus_dma_mem_alloc (unit, c->num, &d->dmamem))
472 channel [b->num*NCHAN + c->num] = d;
473 sprintf (d->name, "cp%d.%d", b->num, c->num);
477 callout_init (&d->timeout_handle, 1);
479 if (ng_make_node_common (&typestruct, &d->node) != 0) {
480 printf ("%s: cannot make common node\n", d->name);
484 NG_NODE_SET_PRIVATE (d->node, d);
485 sprintf (d->nodename, "%s%d", NG_CP_NODE_TYPE,
486 c->board->num*NCHAN + c->num);
487 if (ng_name_node (d->node, d->nodename)) {
488 printf ("%s: cannot name node\n", d->nodename);
489 NG_NODE_UNREF (d->node);
492 d->queue.ifq_maxlen = ifqmaxlen;
493 d->hi_queue.ifq_maxlen = ifqmaxlen;
494 mtx_init (&d->queue.ifq_mtx, "cp_queue", NULL, MTX_DEF);
495 mtx_init (&d->hi_queue.ifq_mtx, "cp_queue_hi", NULL, MTX_DEF);
497 d->ifp = if_alloc(IFT_PPP);
498 if (d->ifp == NULL) {
499 printf ("%s: cannot if_alloc() interface\n", d->name);
502 d->ifp->if_softc = d;
503 if_initname (d->ifp, "cp", b->num * NCHAN + c->num);
504 d->ifp->if_mtu = PP_MTU;
505 d->ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
506 d->ifp->if_ioctl = cp_sioctl;
507 d->ifp->if_start = cp_ifstart;
508 d->ifp->if_init = cp_initialize;
509 d->queue.ifq_maxlen = NRBUF;
510 mtx_init (&d->queue.ifq_mtx, "cp_queue", NULL, MTX_DEF);
511 sppp_attach (d->ifp);
513 IFP2SP(d->ifp)->pp_tlf = cp_tlf;
514 IFP2SP(d->ifp)->pp_tls = cp_tls;
515 /* If BPF is in the kernel, call the attach for it.
516 * The header size of PPP or Cisco/HDLC is 4 bytes. */
517 bpfattach (d->ifp, DLT_PPP, 4);
520 cp_start_chan (c, 1, 1, d->dmamem.virt, d->dmamem.phys);
522 /* Register callback functions. */
523 cp_register_transmit (c, &cp_transmit);
524 cp_register_receive (c, &cp_receive);
525 cp_register_error (c, &cp_error);
526 d->devt = make_dev (&cp_cdevsw, b->num*NCHAN+c->num, UID_ROOT,
527 GID_WHEEL, 0600, "cp%d", b->num*NCHAN+c->num);
537 static int cp_detach (device_t dev)
539 bdrv_t *bd = device_get_softc (dev);
540 cp_board_t *b = bd->board;
544 KASSERT (mtx_initialized (&bd->cp_mtx), ("cp mutex not initialized"));
547 /* Check if the device is busy (open). */
548 for (c = b->chan; c < b->chan + NCHAN; ++c) {
549 drv_t *d = (drv_t*) c->sys;
551 if (! d || ! d->chan->type)
560 /* Ok, we can unload driver */
561 /* At first we should stop all channels */
562 for (c = b->chan; c < b->chan + NCHAN; ++c) {
563 drv_t *d = (drv_t*) c->sys;
565 if (! d || ! d->chan->type)
570 cp_set_dtr (d->chan, 0);
571 cp_set_rts (d->chan, 0);
574 /* Reset the adapter. */
576 cp_interrupt_poll (b, 1);
579 callout_stop (&led_timo[b->num]);
581 /* Disable the interrupt request. */
582 bus_teardown_intr (dev, bd->cp_irq, bd->cp_intrhand);
584 for (c=b->chan; c<b->chan+NCHAN; ++c) {
585 drv_t *d = (drv_t*) c->sys;
587 if (! d || ! d->chan->type)
589 callout_stop (&d->timeout_handle);
591 /* Detach from the packet filter list of interfaces. */
594 /* Detach from the sync PPP list. */
595 sppp_detach (d->ifp);
597 /* Detach from the system list of interfaces. */
600 IF_DRAIN (&d->queue);
601 mtx_destroy (&d->queue.ifq_mtx);
604 ng_rmnode_self (d->node);
605 NG_NODE_UNREF (d->node);
608 mtx_destroy (&d->queue.ifq_mtx);
609 mtx_destroy (&d->hi_queue.ifq_mtx);
611 destroy_dev (d->devt);
617 bus_release_resource (dev, SYS_RES_IRQ, 0, bd->cp_irq);
618 bus_release_resource (dev, SYS_RES_MEMORY, PCIR_BAR(0), bd->cp_res);
623 callout_drain (&led_timo[b->num]);
626 for (c = b->chan; c < b->chan + NCHAN; ++c) {
627 drv_t *d = (drv_t*) c->sys;
629 if (! d || ! d->chan->type)
631 callout_drain (&d->timeout_handle);
632 channel [b->num*NCHAN + c->num] = 0;
633 /* Deallocate buffers. */
634 cp_bus_dma_mem_free (&d->dmamem);
636 adapter [b->num] = 0;
637 cp_bus_dma_mem_free (&bd->dmamem);
639 mtx_destroy (&bd->cp_mtx);
644 static void cp_ifstart (struct ifnet *ifp)
646 drv_t *d = ifp->if_softc;
647 bdrv_t *bd = d->board->sys;
654 static void cp_tlf (struct sppp *sp)
656 drv_t *d = SP2IFP(sp)->if_softc;
658 CP_DEBUG2 (d, ("cp_tlf\n"));
659 /* XXXRIK: Don't forget to protect them by LOCK, or kill them. */
660 /* cp_set_dtr (d->chan, 0);*/
661 /* cp_set_rts (d->chan, 0);*/
662 if (!(sp->pp_flags & PP_FR) && !(d->ifp->if_flags & PP_CISCO))
666 static void cp_tls (struct sppp *sp)
668 drv_t *d = SP2IFP(sp)->if_softc;
670 CP_DEBUG2 (d, ("cp_tls\n"));
671 if (!(sp->pp_flags & PP_FR) && !(d->ifp->if_flags & PP_CISCO))
676 * Process an ioctl request.
678 static int cp_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data)
680 drv_t *d = ifp->if_softc;
681 bdrv_t *bd = d->board->sys;
682 int error, s, was_up, should_be_up;
684 was_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
685 error = sppp_ioctl (ifp, cmd, data);
690 if (! (ifp->if_flags & IFF_DEBUG))
693 d->chan->debug = d->chan->debug_shadow;
696 default: CP_DEBUG2 (d, ("ioctl 0x%lx\n", cmd)); return 0;
697 case SIOCADDMULTI: CP_DEBUG2 (d, ("ioctl SIOCADDMULTI\n")); return 0;
698 case SIOCDELMULTI: CP_DEBUG2 (d, ("ioctl SIOCDELMULTI\n")); return 0;
699 case SIOCSIFFLAGS: CP_DEBUG2 (d, ("ioctl SIOCSIFFLAGS\n")); break;
700 case SIOCSIFADDR: CP_DEBUG2 (d, ("ioctl SIOCSIFADDR\n")); break;
703 /* We get here only in case of SIFFLAGS or SIFADDR. */
706 should_be_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
707 if (! was_up && should_be_up) {
708 /* Interface goes up -- start it. */
711 } else if (was_up && ! should_be_up) {
712 /* Interface is going down -- stop it. */
713 /* if ((IFP2SP(ifp)->pp_flags & PP_FR) || (ifp->if_flags & PP_CISCO))*/
716 CP_DEBUG (d, ("ioctl 0x%lx p4\n", cmd));
723 * Initialization of interface.
724 * It seems to be never called by upper level?
726 static void cp_initialize (void *softc)
730 CP_DEBUG (d, ("cp_initialize\n"));
735 * Stop the interface. Called on splimp().
737 static void cp_down (drv_t *d)
739 CP_DEBUG (d, ("cp_down\n"));
740 /* Interface is going down -- stop it. */
741 cp_set_dtr (d->chan, 0);
742 cp_set_rts (d->chan, 0);
745 callout_stop (&d->timeout_handle);
749 * Start the interface. Called on splimp().
751 static void cp_up (drv_t *d)
753 CP_DEBUG (d, ("cp_up\n"));
754 cp_set_dtr (d->chan, 1);
755 cp_set_rts (d->chan, 1);
760 * Start output on the interface. Get another datagram to send
761 * off of the interface queue, and copy it to the interface
762 * before starting the output.
764 static void cp_send (drv_t *d)
769 CP_DEBUG2 (d, ("cp_send, tn=%d te=%d\n", d->chan->tn, d->chan->te));
771 /* No output if the interface is down. */
775 /* No output if the modem is off. */
776 if (! (d->chan->lloop || d->chan->type != T_SERIAL ||
777 cp_get_dsr (d->chan)))
780 while (cp_transmit_space (d->chan)) {
781 /* Get the packet to send. */
783 IF_DEQUEUE (&d->hi_queue, m);
785 IF_DEQUEUE (&d->queue, m);
787 m = sppp_dequeue (d->ifp);
792 BPF_MTAP (d->ifp, m);
794 len = m_length (m, NULL);
796 printf ("%s: too long packet: %d bytes: ",
798 else if (! m->m_next)
799 cp_send_packet (d->chan, (u_char*) mtod (m, caddr_t), len, 0);
801 u_char *buf = d->chan->tbuf[d->chan->te];
802 m_copydata (m, 0, len, buf);
803 cp_send_packet (d->chan, buf, len, 0);
806 /* Set up transmit timeout, if the transmit ring is not empty.*/
810 d->ifp->if_drv_flags |= IFF_DRV_OACTIVE;
815 * Start output on the interface.
816 * Always called on splimp().
818 static void cp_start (drv_t *d)
822 cp_set_dtr (d->chan, 1);
824 cp_set_rts (d->chan, 1);
826 callout_reset (&d->timeout_handle, hz, cp_watchdog_timer, d);
831 * Handle transmit timeouts.
832 * Recover after lost transmit interrupts.
833 * Always called on splimp().
835 static void cp_watchdog (drv_t *d)
837 CP_DEBUG (d, ("device timeout\n"));
839 cp_stop_chan (d->chan);
840 cp_stop_e1 (d->chan);
841 cp_start_e1 (d->chan);
842 cp_start_chan (d->chan, 1, 1, 0, 0);
843 cp_set_dtr (d->chan, 1);
844 cp_set_rts (d->chan, 1);
849 static void cp_watchdog_timer (void *arg)
852 bdrv_t *bd = d->board->sys;
859 callout_reset (&d->timeout_handle, hz, cp_watchdog_timer, d);
863 static void cp_transmit (cp_chan_t *c, void *attachment, int len)
869 if_inc_counter(d->ifp, IFCOUNTER_OPACKETS, 1);
870 d->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
875 static void cp_receive (cp_chan_t *c, unsigned char *data, int len)
886 m = makembuf (data, len);
888 CP_DEBUG (d, ("no memory for packet\n"));
890 if_inc_counter(d->ifp, IFCOUNTER_IQDROPS, 1);
897 m->m_pkthdr.rcvif = 0;
898 NG_SEND_DATA_ONLY (error, d->hook, m);
900 if_inc_counter(d->ifp, IFCOUNTER_IPACKETS, 1);
901 m->m_pkthdr.rcvif = d->ifp;
902 /* Check if there's a BPF listener on this interface.
903 * If so, hand off the raw packet to bpf. */
905 IF_ENQUEUE (&d->queue, m);
909 static void cp_error (cp_chan_t *c, int data)
915 CP_DEBUG (d, ("frame error\n"));
917 if_inc_counter(d->ifp, IFCOUNTER_IERRORS, 1);
921 CP_DEBUG (d, ("crc error\n"));
923 if_inc_counter(d->ifp, IFCOUNTER_IERRORS, 1);
927 CP_DEBUG (d, ("overrun error\n"));
929 if_inc_counter(d->ifp, IFCOUNTER_COLLISIONS, 1);
930 if_inc_counter(d->ifp, IFCOUNTER_IERRORS, 1);
934 CP_DEBUG (d, ("overflow error\n"));
936 if_inc_counter(d->ifp, IFCOUNTER_IERRORS, 1);
940 CP_DEBUG (d, ("underrun error\n"));
943 if_inc_counter(d->ifp, IFCOUNTER_OERRORS, 1);
944 d->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
949 CP_DEBUG (d, ("error #%d\n", data));
955 * You also need read, write, open, close routines.
956 * This should get you started
958 static int cp_open (struct cdev *dev, int oflags, int devtype, struct thread *td)
960 int unit = dev2unit (dev);
963 if (unit >= NBRD*NCHAN || ! (d = channel[unit]))
965 CP_DEBUG2 (d, ("cp_open\n"));
970 * Only called on the LAST close.
972 static int cp_close (struct cdev *dev, int fflag, int devtype, struct thread *td)
974 drv_t *d = channel [dev2unit (dev)];
976 CP_DEBUG2 (d, ("cp_close\n"));
980 static int cp_modem_status (cp_chan_t *c)
983 bdrv_t *bd = d->board->sys;
986 status = d->running ? TIOCM_LE : 0;
989 if (cp_get_cd (c)) status |= TIOCM_CD;
990 if (cp_get_cts (c)) status |= TIOCM_CTS;
991 if (cp_get_dsr (c)) status |= TIOCM_DSR;
992 if (c->dtr) status |= TIOCM_DTR;
993 if (c->rts) status |= TIOCM_RTS;
999 static int cp_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
1001 drv_t *d = channel [dev2unit (dev)];
1002 bdrv_t *bd = d->board->sys;
1003 cp_chan_t *c = d->chan;
1004 struct serial_statistics *st;
1005 struct e1_statistics *opte1;
1006 struct e3_statistics *opte3;
1011 case SERIAL_GETREGISTERED:
1012 CP_DEBUG2 (d, ("ioctl: getregistered\n"));
1013 bzero (mask, sizeof(mask));
1014 for (s=0; s<NBRD*NCHAN; ++s)
1016 mask [s/8] |= 1 << (s & 7);
1017 bcopy (mask, data, sizeof (mask));
1021 case SERIAL_GETPROTO:
1022 CP_DEBUG2 (d, ("ioctl: getproto\n"));
1023 strcpy ((char*)data, (IFP2SP(d->ifp)->pp_flags & PP_FR) ? "fr" :
1024 (d->ifp->if_flags & PP_CISCO) ? "cisco" : "ppp");
1027 case SERIAL_SETPROTO:
1028 CP_DEBUG2 (d, ("ioctl: setproto\n"));
1029 /* Only for superuser! */
1030 error = priv_check (td, PRIV_DRIVER);
1033 if (d->ifp->if_drv_flags & IFF_DRV_RUNNING)
1035 if (! strcmp ("cisco", (char*)data)) {
1036 IFP2SP(d->ifp)->pp_flags &= ~(PP_FR);
1037 IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE;
1038 d->ifp->if_flags |= PP_CISCO;
1040 } else if (! strcmp ("fr", (char*)data)) {
1041 d->ifp->if_flags &= ~(PP_CISCO);
1042 IFP2SP(d->ifp)->pp_flags |= PP_FR | PP_KEEPALIVE;
1044 } else if (! strcmp ("ppp", (char*)data)) {
1045 IFP2SP(d->ifp)->pp_flags &= ~PP_FR;
1046 IFP2SP(d->ifp)->pp_flags &= ~PP_KEEPALIVE;
1047 d->ifp->if_flags &= ~(PP_CISCO);
1052 case SERIAL_GETKEEPALIVE:
1053 CP_DEBUG2 (d, ("ioctl: getkeepalive\n"));
1054 if ((IFP2SP(d->ifp)->pp_flags & PP_FR) ||
1055 (d->ifp->if_flags & PP_CISCO))
1057 *(int*)data = (IFP2SP(d->ifp)->pp_flags & PP_KEEPALIVE) ? 1 : 0;
1060 case SERIAL_SETKEEPALIVE:
1061 CP_DEBUG2 (d, ("ioctl: setkeepalive\n"));
1062 /* Only for superuser! */
1063 error = priv_check (td, PRIV_DRIVER);
1066 if ((IFP2SP(d->ifp)->pp_flags & PP_FR) ||
1067 (d->ifp->if_flags & PP_CISCO))
1072 IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE;
1074 IFP2SP(d->ifp)->pp_flags &= ~PP_KEEPALIVE;
1080 case SERIAL_GETMODE:
1081 CP_DEBUG2 (d, ("ioctl: getmode\n"));
1082 *(int*)data = SERIAL_HDLC;
1085 case SERIAL_SETMODE:
1086 /* Only for superuser! */
1087 error = priv_check (td, PRIV_DRIVER);
1090 if (*(int*)data != SERIAL_HDLC)
1095 CP_DEBUG2 (d, ("ioctl: getcfg\n"));
1096 if (c->type != T_E1 || c->unfram)
1098 *(char*)data = c->board->mux ? 'c' : 'a';
1102 CP_DEBUG2 (d, ("ioctl: setcfg\n"));
1103 error = priv_check (td, PRIV_DRIVER);
1106 if (c->type != T_E1)
1110 cp_set_mux (c->board, *((char*)data) == 'c');
1115 case SERIAL_GETSTAT:
1116 CP_DEBUG2 (d, ("ioctl: getstat\n"));
1117 st = (struct serial_statistics*) data;
1118 st->rintr = c->rintr;
1119 st->tintr = c->tintr;
1121 st->ibytes = c->ibytes;
1122 st->ipkts = c->ipkts;
1123 st->obytes = c->obytes;
1124 st->opkts = c->opkts;
1125 st->ierrs = c->overrun + c->frame + c->crc;
1126 st->oerrs = c->underrun;
1129 case SERIAL_GETESTAT:
1130 CP_DEBUG2 (d, ("ioctl: getestat\n"));
1131 if (c->type != T_E1 && c->type != T_G703)
1133 opte1 = (struct e1_statistics*) data;
1134 opte1->status = c->status;
1135 opte1->cursec = c->cursec;
1136 opte1->totsec = c->totsec + c->cursec;
1138 opte1->currnt.bpv = c->currnt.bpv;
1139 opte1->currnt.fse = c->currnt.fse;
1140 opte1->currnt.crce = c->currnt.crce;
1141 opte1->currnt.rcrce = c->currnt.rcrce;
1142 opte1->currnt.uas = c->currnt.uas;
1143 opte1->currnt.les = c->currnt.les;
1144 opte1->currnt.es = c->currnt.es;
1145 opte1->currnt.bes = c->currnt.bes;
1146 opte1->currnt.ses = c->currnt.ses;
1147 opte1->currnt.oofs = c->currnt.oofs;
1148 opte1->currnt.css = c->currnt.css;
1149 opte1->currnt.dm = c->currnt.dm;
1151 opte1->total.bpv = c->total.bpv + c->currnt.bpv;
1152 opte1->total.fse = c->total.fse + c->currnt.fse;
1153 opte1->total.crce = c->total.crce + c->currnt.crce;
1154 opte1->total.rcrce = c->total.rcrce + c->currnt.rcrce;
1155 opte1->total.uas = c->total.uas + c->currnt.uas;
1156 opte1->total.les = c->total.les + c->currnt.les;
1157 opte1->total.es = c->total.es + c->currnt.es;
1158 opte1->total.bes = c->total.bes + c->currnt.bes;
1159 opte1->total.ses = c->total.ses + c->currnt.ses;
1160 opte1->total.oofs = c->total.oofs + c->currnt.oofs;
1161 opte1->total.css = c->total.css + c->currnt.css;
1162 opte1->total.dm = c->total.dm + c->currnt.dm;
1163 for (s=0; s<48; ++s) {
1164 opte1->interval[s].bpv = c->interval[s].bpv;
1165 opte1->interval[s].fse = c->interval[s].fse;
1166 opte1->interval[s].crce = c->interval[s].crce;
1167 opte1->interval[s].rcrce = c->interval[s].rcrce;
1168 opte1->interval[s].uas = c->interval[s].uas;
1169 opte1->interval[s].les = c->interval[s].les;
1170 opte1->interval[s].es = c->interval[s].es;
1171 opte1->interval[s].bes = c->interval[s].bes;
1172 opte1->interval[s].ses = c->interval[s].ses;
1173 opte1->interval[s].oofs = c->interval[s].oofs;
1174 opte1->interval[s].css = c->interval[s].css;
1175 opte1->interval[s].dm = c->interval[s].dm;
1179 case SERIAL_GETE3STAT:
1180 CP_DEBUG2 (d, ("ioctl: gete3stat\n"));
1181 if (c->type != T_E3 && c->type != T_T3 && c->type != T_STS1)
1183 opte3 = (struct e3_statistics*) data;
1185 opte3->status = c->e3status;
1186 opte3->cursec = (c->e3csec_5 * 2 + 1) / 10;
1187 opte3->totsec = c->e3tsec + opte3->cursec;
1189 opte3->ccv = c->e3ccv;
1190 opte3->tcv = c->e3tcv + opte3->ccv;
1192 for (s = 0; s < 48; ++s) {
1193 opte3->icv[s] = c->e3icv[s];
1197 case SERIAL_CLRSTAT:
1198 CP_DEBUG2 (d, ("ioctl: clrstat\n"));
1199 /* Only for superuser! */
1200 error = priv_check (td, PRIV_DRIVER);
1213 bzero (&c->currnt, sizeof (c->currnt));
1214 bzero (&c->total, sizeof (c->total));
1215 bzero (c->interval, sizeof (c->interval));
1218 bzero (c->e3icv, sizeof (c->e3icv));
1221 case SERIAL_GETBAUD:
1222 CP_DEBUG2 (d, ("ioctl: getbaud\n"));
1223 *(long*)data = c->baud;
1226 case SERIAL_SETBAUD:
1227 CP_DEBUG2 (d, ("ioctl: setbaud\n"));
1228 /* Only for superuser! */
1229 error = priv_check (td, PRIV_DRIVER);
1234 cp_set_baud (c, *(long*)data);
1239 case SERIAL_GETLOOP:
1240 CP_DEBUG2 (d, ("ioctl: getloop\n"));
1241 *(int*)data = c->lloop;
1244 case SERIAL_SETLOOP:
1245 CP_DEBUG2 (d, ("ioctl: setloop\n"));
1246 /* Only for superuser! */
1247 error = priv_check (td, PRIV_DRIVER);
1252 cp_set_lloop (c, *(int*)data);
1257 case SERIAL_GETDPLL:
1258 CP_DEBUG2 (d, ("ioctl: getdpll\n"));
1259 if (c->type != T_SERIAL)
1261 *(int*)data = c->dpll;
1264 case SERIAL_SETDPLL:
1265 CP_DEBUG2 (d, ("ioctl: setdpll\n"));
1266 /* Only for superuser! */
1267 error = priv_check (td, PRIV_DRIVER);
1270 if (c->type != T_SERIAL)
1274 cp_set_dpll (c, *(int*)data);
1279 case SERIAL_GETNRZI:
1280 CP_DEBUG2 (d, ("ioctl: getnrzi\n"));
1281 if (c->type != T_SERIAL)
1283 *(int*)data = c->nrzi;
1286 case SERIAL_SETNRZI:
1287 CP_DEBUG2 (d, ("ioctl: setnrzi\n"));
1288 /* Only for superuser! */
1289 error = priv_check (td, PRIV_DRIVER);
1292 if (c->type != T_SERIAL)
1296 cp_set_nrzi (c, *(int*)data);
1301 case SERIAL_GETDEBUG:
1302 CP_DEBUG2 (d, ("ioctl: getdebug\n"));
1303 *(int*)data = d->chan->debug;
1306 case SERIAL_SETDEBUG:
1307 CP_DEBUG2 (d, ("ioctl: setdebug\n"));
1308 /* Only for superuser! */
1309 error = priv_check (td, PRIV_DRIVER);
1314 * The debug_shadow is always greater than zero for logic
1315 * simplicity. For switching debug off the IFF_DEBUG is
1318 d->chan->debug_shadow = (*(int*)data) ? (*(int*)data) : 1;
1319 if (d->ifp->if_flags & IFF_DEBUG)
1320 d->chan->debug = d->chan->debug_shadow;
1322 d->chan->debug = *(int*)data;
1326 case SERIAL_GETHIGAIN:
1327 CP_DEBUG2 (d, ("ioctl: gethigain\n"));
1328 if (c->type != T_E1)
1330 *(int*)data = c->higain;
1333 case SERIAL_SETHIGAIN:
1334 CP_DEBUG2 (d, ("ioctl: sethigain\n"));
1335 /* Only for superuser! */
1336 error = priv_check (td, PRIV_DRIVER);
1339 if (c->type != T_E1)
1343 cp_set_higain (c, *(int*)data);
1348 case SERIAL_GETPHONY:
1349 CP_DEBUG2 (d, ("ioctl: getphony\n"));
1350 if (c->type != T_E1)
1352 *(int*)data = c->phony;
1355 case SERIAL_SETPHONY:
1356 CP_DEBUG2 (d, ("ioctl: setphony\n"));
1357 /* Only for superuser! */
1358 error = priv_check (td, PRIV_DRIVER);
1361 if (c->type != T_E1)
1365 cp_set_phony (c, *(int*)data);
1370 case SERIAL_GETUNFRAM:
1371 CP_DEBUG2 (d, ("ioctl: getunfram\n"));
1372 if (c->type != T_E1)
1374 *(int*)data = c->unfram;
1377 case SERIAL_SETUNFRAM:
1378 CP_DEBUG2 (d, ("ioctl: setunfram\n"));
1379 /* Only for superuser! */
1380 error = priv_check (td, PRIV_DRIVER);
1383 if (c->type != T_E1)
1387 cp_set_unfram (c, *(int*)data);
1392 case SERIAL_GETSCRAMBLER:
1393 CP_DEBUG2 (d, ("ioctl: getscrambler\n"));
1394 if (c->type != T_G703 && !c->unfram)
1396 *(int*)data = c->scrambler;
1399 case SERIAL_SETSCRAMBLER:
1400 CP_DEBUG2 (d, ("ioctl: setscrambler\n"));
1401 /* Only for superuser! */
1402 error = priv_check (td, PRIV_DRIVER);
1405 if (c->type != T_G703 && !c->unfram)
1409 cp_set_scrambler (c, *(int*)data);
1414 case SERIAL_GETMONITOR:
1415 CP_DEBUG2 (d, ("ioctl: getmonitor\n"));
1416 if (c->type != T_E1 &&
1421 *(int*)data = c->monitor;
1424 case SERIAL_SETMONITOR:
1425 CP_DEBUG2 (d, ("ioctl: setmonitor\n"));
1426 /* Only for superuser! */
1427 error = priv_check (td, PRIV_DRIVER);
1430 if (c->type != T_E1)
1434 cp_set_monitor (c, *(int*)data);
1439 case SERIAL_GETUSE16:
1440 CP_DEBUG2 (d, ("ioctl: getuse16\n"));
1441 if (c->type != T_E1 || c->unfram)
1443 *(int*)data = c->use16;
1446 case SERIAL_SETUSE16:
1447 CP_DEBUG2 (d, ("ioctl: setuse16\n"));
1448 /* Only for superuser! */
1449 error = priv_check (td, PRIV_DRIVER);
1452 if (c->type != T_E1)
1456 cp_set_use16 (c, *(int*)data);
1461 case SERIAL_GETCRC4:
1462 CP_DEBUG2 (d, ("ioctl: getcrc4\n"));
1463 if (c->type != T_E1 || c->unfram)
1465 *(int*)data = c->crc4;
1468 case SERIAL_SETCRC4:
1469 CP_DEBUG2 (d, ("ioctl: setcrc4\n"));
1470 /* Only for superuser! */
1471 error = priv_check (td, PRIV_DRIVER);
1474 if (c->type != T_E1)
1478 cp_set_crc4 (c, *(int*)data);
1484 CP_DEBUG2 (d, ("ioctl: getclk\n"));
1485 if (c->type != T_E1 &&
1486 c->type != T_G703 &&
1492 default: *(int*)data = E1CLK_INTERNAL; break;
1493 case GSYN_RCV: *(int*)data = E1CLK_RECEIVE; break;
1494 case GSYN_RCV0: *(int*)data = E1CLK_RECEIVE_CHAN0; break;
1495 case GSYN_RCV1: *(int*)data = E1CLK_RECEIVE_CHAN1; break;
1496 case GSYN_RCV2: *(int*)data = E1CLK_RECEIVE_CHAN2; break;
1497 case GSYN_RCV3: *(int*)data = E1CLK_RECEIVE_CHAN3; break;
1502 CP_DEBUG2 (d, ("ioctl: setclk\n"));
1503 /* Only for superuser! */
1504 error = priv_check (td, PRIV_DRIVER);
1507 if (c->type != T_E1 &&
1508 c->type != T_G703 &&
1515 switch (*(int*)data) {
1516 default: cp_set_gsyn (c, GSYN_INT); break;
1517 case E1CLK_RECEIVE: cp_set_gsyn (c, GSYN_RCV); break;
1518 case E1CLK_RECEIVE_CHAN0: cp_set_gsyn (c, GSYN_RCV0); break;
1519 case E1CLK_RECEIVE_CHAN1: cp_set_gsyn (c, GSYN_RCV1); break;
1520 case E1CLK_RECEIVE_CHAN2: cp_set_gsyn (c, GSYN_RCV2); break;
1521 case E1CLK_RECEIVE_CHAN3: cp_set_gsyn (c, GSYN_RCV3); break;
1527 case SERIAL_GETTIMESLOTS:
1528 CP_DEBUG2 (d, ("ioctl: gettimeslots\n"));
1529 if ((c->type != T_E1 || c->unfram) && c->type != T_DATA)
1531 *(u_long*)data = c->ts;
1534 case SERIAL_SETTIMESLOTS:
1535 CP_DEBUG2 (d, ("ioctl: settimeslots\n"));
1536 /* Only for superuser! */
1537 error = priv_check (td, PRIV_DRIVER);
1540 if ((c->type != T_E1 || c->unfram) && c->type != T_DATA)
1544 cp_set_ts (c, *(u_long*)data);
1549 case SERIAL_GETINVCLK:
1550 CP_DEBUG2 (d, ("ioctl: getinvclk\n"));
1554 if (c->type != T_SERIAL)
1556 *(int*)data = c->invtxc;
1560 case SERIAL_SETINVCLK:
1561 CP_DEBUG2 (d, ("ioctl: setinvclk\n"));
1562 /* Only for superuser! */
1563 error = priv_check (td, PRIV_DRIVER);
1566 if (c->type != T_SERIAL)
1570 cp_set_invtxc (c, *(int*)data);
1571 cp_set_invrxc (c, *(int*)data);
1576 case SERIAL_GETINVTCLK:
1577 CP_DEBUG2 (d, ("ioctl: getinvtclk\n"));
1578 if (c->type != T_SERIAL)
1580 *(int*)data = c->invtxc;
1583 case SERIAL_SETINVTCLK:
1584 CP_DEBUG2 (d, ("ioctl: setinvtclk\n"));
1585 /* Only for superuser! */
1586 error = priv_check (td, PRIV_DRIVER);
1589 if (c->type != T_SERIAL)
1593 cp_set_invtxc (c, *(int*)data);
1598 case SERIAL_GETINVRCLK:
1599 CP_DEBUG2 (d, ("ioctl: getinvrclk\n"));
1600 if (c->type != T_SERIAL)
1602 *(int*)data = c->invrxc;
1605 case SERIAL_SETINVRCLK:
1606 CP_DEBUG2 (d, ("ioctl: setinvrclk\n"));
1607 /* Only for superuser! */
1608 error = priv_check (td, PRIV_DRIVER);
1611 if (c->type != T_SERIAL)
1615 cp_set_invrxc (c, *(int*)data);
1620 case SERIAL_GETLEVEL:
1621 CP_DEBUG2 (d, ("ioctl: getlevel\n"));
1622 if (c->type != T_G703)
1626 *(int*)data = cp_get_lq (c);
1633 CP_DEBUG2 (d, ("ioctl: reset\n"));
1634 /* Only for superuser! */
1635 error = priv_check (td, PRIV_DRIVER);
1640 cp_reset (c->board, 0, 0);
1645 case SERIAL_HARDRESET:
1646 CP_DEBUG2 (d, ("ioctl: hardreset\n"));
1647 /* Only for superuser! */
1648 error = priv_check (td, PRIV_DRIVER);
1653 /* hard_reset (c->board); */
1659 case SERIAL_GETCABLE:
1660 CP_DEBUG2 (d, ("ioctl: getcable\n"));
1661 if (c->type != T_SERIAL)
1665 *(int*)data = cp_get_cable (c);
1671 CP_DEBUG2 (d, ("ioctl: getdir\n"));
1672 if (c->type != T_E1 && c->type != T_DATA)
1674 *(int*)data = c->dir;
1678 CP_DEBUG2 (d, ("ioctl: setdir\n"));
1679 /* Only for superuser! */
1680 error = priv_check (td, PRIV_DRIVER);
1685 cp_set_dir (c, *(int*)data);
1690 case SERIAL_GETRLOOP:
1691 CP_DEBUG2 (d, ("ioctl: getrloop\n"));
1692 if (c->type != T_G703 &&
1697 *(int*)data = cp_get_rloop (c);
1700 case SERIAL_SETRLOOP:
1701 CP_DEBUG2 (d, ("ioctl: setloop\n"));
1702 if (c->type != T_E3 && c->type != T_T3 && c->type != T_STS1)
1704 /* Only for superuser! */
1705 error = priv_check (td, PRIV_DRIVER);
1710 cp_set_rloop (c, *(int*)data);
1715 case SERIAL_GETCABLEN:
1716 CP_DEBUG2 (d, ("ioctl: getcablen\n"));
1717 if (c->type != T_T3 && c->type != T_STS1)
1719 *(int*)data = c->cablen;
1722 case SERIAL_SETCABLEN:
1723 CP_DEBUG2 (d, ("ioctl: setloop\n"));
1724 if (c->type != T_T3 && c->type != T_STS1)
1726 /* Only for superuser! */
1727 error = priv_check (td, PRIV_DRIVER);
1732 cp_set_cablen (c, *(int*)data);
1737 case TIOCSDTR: /* Set DTR */
1745 case TIOCCDTR: /* Clear DTR */
1753 case TIOCMSET: /* Set DTR/RTS */
1756 cp_set_dtr (c, (*(int*)data & TIOCM_DTR) ? 1 : 0);
1757 cp_set_rts (c, (*(int*)data & TIOCM_RTS) ? 1 : 0);
1762 case TIOCMBIS: /* Add DTR/RTS */
1765 if (*(int*)data & TIOCM_DTR) cp_set_dtr (c, 1);
1766 if (*(int*)data & TIOCM_RTS) cp_set_rts (c, 1);
1771 case TIOCMBIC: /* Clear DTR/RTS */
1774 if (*(int*)data & TIOCM_DTR) cp_set_dtr (c, 0);
1775 if (*(int*)data & TIOCM_RTS) cp_set_rts (c, 0);
1780 case TIOCMGET: /* Get modem status */
1781 *(int*)data = cp_modem_status (c);
1788 static int ng_cp_constructor (node_p node)
1790 drv_t *d = NG_NODE_PRIVATE (node);
1791 CP_DEBUG (d, ("Constructor\n"));
1795 static int ng_cp_newhook (node_p node, hook_p hook, const char *name)
1798 drv_t *d = NG_NODE_PRIVATE (node);
1799 bdrv_t *bd = d->board->sys;
1801 CP_DEBUG (d, ("Newhook\n"));
1802 /* Attach debug hook */
1803 if (strcmp (name, NG_CP_HOOK_DEBUG) == 0) {
1804 NG_HOOK_SET_PRIVATE (hook, NULL);
1805 d->debug_hook = hook;
1809 /* Check for raw hook */
1810 if (strcmp (name, NG_CP_HOOK_RAW) != 0)
1813 NG_HOOK_SET_PRIVATE (hook, d);
1823 static char *format_timeslots (u_long s)
1825 static char buf [100];
1829 for (i=1; i<32; ++i)
1831 int prev = (i > 1) & (s >> (i-1));
1832 int next = (i < 31) & (s >> (i+1));
1842 *p++ = '0' + i / 10;
1843 *p++ = '0' + i % 10;
1849 static int print_modems (char *s, cp_chan_t *c, int need_header)
1851 int status = cp_modem_status (c);
1855 length += sprintf (s + length, " LE DTR DSR RTS CTS CD\n");
1856 length += sprintf (s + length, "%4s %4s %4s %4s %4s %4s\n",
1857 status & TIOCM_LE ? "On" : "-",
1858 status & TIOCM_DTR ? "On" : "-",
1859 status & TIOCM_DSR ? "On" : "-",
1860 status & TIOCM_RTS ? "On" : "-",
1861 status & TIOCM_CTS ? "On" : "-",
1862 status & TIOCM_CD ? "On" : "-");
1866 static int print_stats (char *s, cp_chan_t *c, int need_header)
1871 length += sprintf (s + length, " Rintr Tintr Mintr Ibytes Ipkts Ierrs Obytes Opkts Oerrs\n");
1872 length += sprintf (s + length, "%7ld %7ld %7ld %8lu %7ld %7ld %8lu %7ld %7ld\n",
1873 c->rintr, c->tintr, 0l, (unsigned long) c->ibytes,
1874 c->ipkts, c->overrun + c->frame + c->crc,
1875 (unsigned long) c->obytes, c->opkts, c->underrun);
1879 static char *format_e1_status (u_char status)
1881 static char buf [80];
1883 if (status & E1_NOALARM)
1886 if (status & E1_LOS) strcat (buf, ",LOS");
1887 if (status & E1_AIS) strcat (buf, ",AIS");
1888 if (status & E1_LOF) strcat (buf, ",LOF");
1889 if (status & E1_LOMF) strcat (buf, ",LOMF");
1890 if (status & E1_FARLOF) strcat (buf, ",FARLOF");
1891 if (status & E1_AIS16) strcat (buf, ",AIS16");
1892 if (status & E1_FARLOMF) strcat (buf, ",FARLOMF");
1893 if (status & E1_TSTREQ) strcat (buf, ",TSTREQ");
1894 if (status & E1_TSTERR) strcat (buf, ",TSTERR");
1900 static int print_frac (char *s, int leftalign, u_long numerator, u_long divider)
1904 if (numerator < 1 || divider < 1) {
1905 length += sprintf (s+length, leftalign ? "/- " : " -");
1908 n = (int) (0.5 + 1000.0 * numerator / divider);
1910 length += sprintf (s+length, leftalign ? "/.%-3d" : " .%03d", n);
1913 *(s + length) = leftalign ? '/' : ' ';
1916 if (n >= 1000000) n = (n+500) / 1000 * 1000;
1917 else if (n >= 100000) n = (n+50) / 100 * 100;
1918 else if (n >= 10000) n = (n+5) / 10 * 10;
1921 case 1000: length += printf (s+length, ".999"); return length;
1922 case 10000: n = 9990; break;
1923 case 100000: n = 99900; break;
1924 case 1000000: n = 999000; break;
1926 if (n < 10000) length += sprintf (s+length, "%d.%d", n/1000, n/10%100);
1927 else if (n < 100000) length += sprintf (s+length, "%d.%d", n/1000, n/100%10);
1928 else if (n < 1000000) length += sprintf (s+length, "%d.", n/1000);
1929 else length += sprintf (s+length, "%d", n/1000);
1934 static int print_e1_stats (char *s, cp_chan_t *c)
1936 struct e1_counters total;
1940 totsec = c->totsec + c->cursec;
1941 total.bpv = c->total.bpv + c->currnt.bpv;
1942 total.fse = c->total.fse + c->currnt.fse;
1943 total.crce = c->total.crce + c->currnt.crce;
1944 total.rcrce = c->total.rcrce + c->currnt.rcrce;
1945 total.uas = c->total.uas + c->currnt.uas;
1946 total.les = c->total.les + c->currnt.les;
1947 total.es = c->total.es + c->currnt.es;
1948 total.bes = c->total.bes + c->currnt.bes;
1949 total.ses = c->total.ses + c->currnt.ses;
1950 total.oofs = c->total.oofs + c->currnt.oofs;
1951 total.css = c->total.css + c->currnt.css;
1952 total.dm = c->total.dm + c->currnt.dm;
1954 length += sprintf (s + length, " Unav/Degr Bpv/Fsyn CRC/RCRC Err/Lerr Sev/Bur Oof/Slp Status\n");
1956 /* Unavailable seconds, degraded minutes */
1957 length += print_frac (s + length, 0, c->currnt.uas, c->cursec);
1958 length += print_frac (s + length, 1, 60 * c->currnt.dm, c->cursec);
1960 /* Bipolar violations, frame sync errors */
1961 length += print_frac (s + length, 0, c->currnt.bpv, c->cursec);
1962 length += print_frac (s + length, 1, c->currnt.fse, c->cursec);
1964 /* CRC errors, remote CRC errors (E-bit) */
1965 length += print_frac (s + length, 0, c->currnt.crce, c->cursec);
1966 length += print_frac (s + length, 1, c->currnt.rcrce, c->cursec);
1968 /* Errored seconds, line errored seconds */
1969 length += print_frac (s + length, 0, c->currnt.es, c->cursec);
1970 length += print_frac (s + length, 1, c->currnt.les, c->cursec);
1972 /* Severely errored seconds, burst errored seconds */
1973 length += print_frac (s + length, 0, c->currnt.ses, c->cursec);
1974 length += print_frac (s + length, 1, c->currnt.bes, c->cursec);
1976 /* Out of frame seconds, controlled slip seconds */
1977 length += print_frac (s + length, 0, c->currnt.oofs, c->cursec);
1978 length += print_frac (s + length, 1, c->currnt.css, c->cursec);
1980 length += sprintf (s + length, " %s\n", format_e1_status (c->status));
1982 /* Print total statistics. */
1983 length += print_frac (s + length, 0, total.uas, totsec);
1984 length += print_frac (s + length, 1, 60 * total.dm, totsec);
1986 length += print_frac (s + length, 0, total.bpv, totsec);
1987 length += print_frac (s + length, 1, total.fse, totsec);
1989 length += print_frac (s + length, 0, total.crce, totsec);
1990 length += print_frac (s + length, 1, total.rcrce, totsec);
1992 length += print_frac (s + length, 0, total.es, totsec);
1993 length += print_frac (s + length, 1, total.les, totsec);
1995 length += print_frac (s + length, 0, total.ses, totsec);
1996 length += print_frac (s + length, 1, total.bes, totsec);
1998 length += print_frac (s + length, 0, total.oofs, totsec);
1999 length += print_frac (s + length, 1, total.css, totsec);
2001 length += sprintf (s + length, " -- Total\n");
2005 static int print_chan (char *s, cp_chan_t *c)
2008 bdrv_t *bd = d->board->sys;
2011 length += sprintf (s + length, "cp%d", c->board->num * NCHAN + c->num);
2013 length += sprintf (s + length, " debug=%d", d->chan->debug);
2015 if (c->board->mux) {
2016 length += sprintf (s + length, " cfg=C");
2018 length += sprintf (s + length, " cfg=A");
2022 length += sprintf (s + length, " %ld", c->baud);
2024 length += sprintf (s + length, " extclock");
2026 if (c->type == T_E1 || c->type == T_G703)
2028 case GSYN_INT : length += sprintf (s + length, " syn=int"); break;
2029 case GSYN_RCV : length += sprintf (s + length, " syn=rcv"); break;
2030 case GSYN_RCV0 : length += sprintf (s + length, " syn=rcv0"); break;
2031 case GSYN_RCV1 : length += sprintf (s + length, " syn=rcv1"); break;
2032 case GSYN_RCV2 : length += sprintf (s + length, " syn=rcv2"); break;
2033 case GSYN_RCV3 : length += sprintf (s + length, " syn=rcv3"); break;
2035 if (c->type == T_SERIAL) {
2036 length += sprintf (s + length, " dpll=%s", c->dpll ? "on" : "off");
2037 length += sprintf (s + length, " nrzi=%s", c->nrzi ? "on" : "off");
2038 length += sprintf (s + length, " invclk=%s", c->invtxc ? "on" : "off");
2040 if (c->type == T_E1)
2041 length += sprintf (s + length, " higain=%s", c->higain ? "on" : "off");
2043 length += sprintf (s + length, " loop=%s", c->lloop ? "on" : "off");
2045 if (c->type == T_E1)
2046 length += sprintf (s + length, " ts=%s", format_timeslots (c->ts));
2047 if (c->type == T_G703) {
2055 length += sprintf (s + length, " (level=-%.1fdB)", lq / 10.0);
2057 length += sprintf (s + length, "\n");
2061 static int ng_cp_rcvmsg (node_p node, item_p item, hook_p lasthook)
2063 drv_t *d = NG_NODE_PRIVATE (node);
2064 struct ng_mesg *msg;
2065 struct ng_mesg *resp = NULL;
2068 CP_DEBUG (d, ("Rcvmsg\n"));
2069 NGI_GET_MSG (item, msg);
2070 switch (msg->header.typecookie) {
2076 printf ("Not implemented yet\n");
2080 case NGM_GENERIC_COOKIE:
2081 switch (msg->header.cmd) {
2086 case NGM_TEXT_STATUS: {
2089 int dl = sizeof (struct ng_mesg) + 730;
2091 NG_MKRESPONSE (resp, msg, dl, M_NOWAIT);
2098 l += print_chan (s + l, d->chan);
2099 l += print_stats (s + l, d->chan, 1);
2100 l += print_modems (s + l, d->chan, 1);
2101 l += print_e1_stats (s + l, d->chan);
2103 l += sprintf (s + l, "Error: node not connect to channel");
2104 strncpy ((resp)->header.cmdstr, "status", NG_CMDSTRSIZ);
2110 NG_RESPOND_MSG (error, node, item, resp);
2115 static int ng_cp_rcvdata (hook_p hook, item_p item)
2117 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE(hook));
2119 struct ng_tag_prio *ptag;
2120 bdrv_t *bd = d->board->sys;
2124 CP_DEBUG2 (d, ("Rcvdata\n"));
2125 NGI_GET_M (item, m);
2126 NG_FREE_ITEM (item);
2127 if (! NG_HOOK_PRIVATE (hook) || ! d) {
2132 /* Check for high priority data */
2133 if ((ptag = (struct ng_tag_prio *)m_tag_locate(m, NGM_GENERIC_COOKIE,
2134 NG_TAG_PRIO, NULL)) != NULL && (ptag->priority > NG_PRIO_CUTOFF) )
2142 if (_IF_QFULL (q)) {
2157 static int ng_cp_rmnode (node_p node)
2159 drv_t *d = NG_NODE_PRIVATE (node);
2161 CP_DEBUG (d, ("Rmnode\n"));
2162 if (d && d->running) {
2163 bdrv_t *bd = d->board->sys;
2171 if (node->nd_flags & NGF_REALLY_DIE) {
2172 NG_NODE_SET_PRIVATE (node, NULL);
2173 NG_NODE_UNREF (node);
2175 NG_NODE_REVIVE(node); /* Persistant node */
2180 static int ng_cp_connect (hook_p hook)
2182 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2185 CP_DEBUG (d, ("Connect\n"));
2186 callout_reset (&d->timeout_handle, hz, cp_watchdog_timer, d);
2192 static int ng_cp_disconnect (hook_p hook)
2194 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2197 CP_DEBUG (d, ("Disconnect\n"));
2198 if (NG_HOOK_PRIVATE (hook))
2200 bdrv_t *bd = d->board->sys;
2207 /* If we were wait it than it reasserted now, just stop it. */
2208 if (!callout_drain (&d->timeout_handle))
2209 callout_stop (&d->timeout_handle);
2215 static int cp_modevent (module_t mod, int type, void *unused)
2217 static int load_count = 0;
2222 if (ng_newtype (&typestruct))
2223 printf ("Failed to register ng_cp\n");
2226 callout_init (&timeout_handle, 1);
2227 callout_reset (&timeout_handle, hz*5, cp_timeout, 0);
2230 if (load_count == 1) {
2231 printf ("Removing device entry for Tau-PCI\n");
2233 ng_rmtype (&typestruct);
2236 /* If we were wait it than it reasserted now, just stop it.
2237 * Actually we shouldn't get this condition. But code could be
2238 * changed in the future, so just be a litle paranoid.
2240 if (!callout_drain (&timeout_handle))
2241 callout_stop (&timeout_handle);
2251 static struct ng_type typestruct = {
2252 .version = NG_ABI_VERSION,
2253 .name = NG_CP_NODE_TYPE,
2254 .constructor = ng_cp_constructor,
2255 .rcvmsg = ng_cp_rcvmsg,
2256 .shutdown = ng_cp_rmnode,
2257 .newhook = ng_cp_newhook,
2258 .connect = ng_cp_connect,
2259 .rcvdata = ng_cp_rcvdata,
2260 .disconnect = ng_cp_disconnect,
2265 MODULE_DEPEND (ng_cp, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION);
2267 MODULE_DEPEND (cp, sppp, 1, 1, 1);
2269 DRIVER_MODULE (cp, pci, cp_driver, cp_devclass, cp_modevent, NULL);
2270 MODULE_VERSION (cp, 1);