2 * Cronyx-Tau32-PCI adapter driver for FreeBSD.
4 * Copyright (C) 2003-2005 Cronyx Engineering.
5 * Copyright (C) 2003-2005 Kurakin Roman, <rik@FreeBSD.org>
7 * This software is distributed with NO WARRANTIES, not even the implied
8 * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 * Authors grant any other persons or organisations a permission to use,
11 * modify and redistribute this software in source and binary forms,
12 * as long as this message is kept with the software, all derivative
13 * works or modified versions.
15 * $Cronyx: if_ce.c,v 1.9.2.8 2005/11/21 14:17:44 rik Exp $
18 #include <sys/cdefs.h>
19 __FBSDID("$FreeBSD$");
21 #include <sys/param.h>
23 #if __FreeBSD_version >= 500000
31 #include <sys/ucred.h>
34 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
39 #include <sys/malloc.h>
40 #include <sys/socket.h>
41 #include <sys/sockio.h>
42 #if __FreeBSD_version >= 504000
43 #include <sys/sysctl.h>
50 #include <net/if_var.h>
51 #if __FreeBSD_version > 501000
52 # include <dev/pci/pcivar.h>
53 # include <dev/pci/pcireg.h>
55 # include <pci/pcivar.h>
56 # include <pci/pcireg.h>
58 #include <machine/bus.h>
60 #include "opt_ng_cronyx.h"
61 #ifdef NETGRAPH_CRONYX
62 # include "opt_netgraph.h"
64 # error #option NETGRAPH missed from configuration
66 # include <netgraph/ng_message.h>
67 # include <netgraph/netgraph.h>
68 # include <dev/ce/ng_ce.h>
70 # include <net/if_types.h>
71 # include <net/if_sppp.h>
72 # define PP_CISCO IFF_LINK2
75 #include <dev/cx/machdep.h>
76 #include <dev/ce/ceddk.h>
77 #include <machine/cserial.h>
78 #include <machine/resource.h>
80 /* If we don't have Cronyx's sppp version, we don't have fr support via sppp */
86 #define IFP2SP(ifp) ((struct sppp*)ifp)
89 #define SP2IFP(sp) ((struct ifnet*)sp)
93 #define PCIR_BAR(x) (PCIR_MAPS + (x) * 4)
96 /* define as our previous return value */
97 #ifndef BUS_PROBE_DEFAULT
98 #define BUS_PROBE_DEFAULT 0
101 #define CE_DEBUG(d,s) ({if (d->chan->debug) {\
102 printf ("%s: ", d->name); printf s;}})
103 #define CE_DEBUG2(d,s) ({if (d->chan->debug>1) {\
104 printf ("%s: ", d->name); printf s;}})
107 #define IF_DRAIN(ifq) do { \
110 IF_DEQUEUE(ifq, m); \
119 #define _IF_QLEN(ifq) ((ifq)->ifq_len)
122 #ifndef callout_drain
123 #define callout_drain callout_stop
126 #define CE_LOCK_NAME "ceX"
128 #define CE_LOCK(_bd) mtx_lock (&(_bd)->ce_mtx)
129 #define CE_UNLOCK(_bd) mtx_unlock (&(_bd)->ce_mtx)
130 #define CE_LOCK_ASSERT(_bd) mtx_assert (&(_bd)->ce_mtx, MA_OWNED)
132 #define CDEV_MAJOR 185
134 static int ce_probe __P((device_t));
135 static int ce_attach __P((device_t));
136 static int ce_detach __P((device_t));
138 static device_method_t ce_methods[] = {
139 /* Device interface */
140 DEVMETHOD(device_probe, ce_probe),
141 DEVMETHOD(device_attach, ce_attach),
142 DEVMETHOD(device_detach, ce_detach),
147 typedef struct _ce_dma_mem_t {
151 #if __FreeBSD_version >= 500000
157 typedef struct _drv_t {
162 struct ifqueue rqueue;
164 char nodename [NG_NODESIZE];
168 struct ifqueue queue;
169 struct ifqueue hi_queue;
174 struct callout timeout_handle;
175 #if __FreeBSD_version >= 500000
177 #else /* __FreeBSD_version < 500000 */
183 typedef struct _bdrv_t {
185 struct resource *ce_res;
186 struct resource *ce_irq;
189 drv_t channel [NCHAN];
190 #if __FreeBSD_version >= 504000
195 static driver_t ce_driver = {
201 static devclass_t ce_devclass;
203 static void ce_receive (ce_chan_t *c, unsigned char *data, int len);
204 static void ce_transmit (ce_chan_t *c, void *attachment, int len);
205 static void ce_error (ce_chan_t *c, int data);
206 static void ce_up (drv_t *d);
207 static void ce_start (drv_t *d);
208 static void ce_down (drv_t *d);
209 static void ce_watchdog (drv_t *d);
210 static void ce_watchdog_timer (void *arg);
212 extern struct ng_type typestruct;
214 static void ce_ifstart (struct ifnet *ifp);
215 static void ce_tlf (struct sppp *sp);
216 static void ce_tls (struct sppp *sp);
217 static int ce_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data);
218 static void ce_initialize (void *softc);
221 static ce_board_t *adapter [NBRD];
222 static drv_t *channel [NBRD*NCHAN];
223 static struct callout led_timo [NBRD];
224 static struct callout timeout_handle;
226 static int ce_destroy = 0;
228 #if __FreeBSD_version < 500000
229 static int ce_open (dev_t dev, int oflags, int devtype, struct proc *p);
230 static int ce_close (dev_t dev, int fflag, int devtype, struct proc *p);
231 static int ce_ioctl (dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p);
233 static int ce_open (struct cdev *dev, int oflags, int devtype, struct thread *td);
234 static int ce_close (struct cdev *dev, int fflag, int devtype, struct thread *td);
235 static int ce_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td);
237 #if __FreeBSD_version < 500000
238 static struct cdevsw ce_cdevsw = {
239 ce_open, ce_close, noread, nowrite,
240 ce_ioctl, nopoll, nommap, nostrategy,
241 "ce", CDEV_MAJOR, nodump, nopsize,
244 #elif __FreeBSD_version == 500000
245 static struct cdevsw ce_cdevsw = {
246 ce_open, ce_close, noread, nowrite,
247 ce_ioctl, nopoll, nommap, nostrategy,
248 "ce", CDEV_MAJOR, nodump, nopsize,
251 #elif __FreeBSD_version <= 501000
252 static struct cdevsw ce_cdevsw = {
260 .d_strategy = nostrategy,
266 #elif __FreeBSD_version < 502103
267 static struct cdevsw ce_cdevsw = {
275 #elif __FreeBSD_version < 600000
276 static struct cdevsw ce_cdevsw = {
277 .d_version = D_VERSION,
283 .d_flags = D_NEEDGIANT,
285 #else /* __FreeBSD_version >= 600000 */
286 static struct cdevsw ce_cdevsw = {
287 .d_version = D_VERSION,
296 * Make an mbuf from data.
298 static struct mbuf *makembuf (void *buf, unsigned len)
302 MGETHDR (m, M_NOWAIT, MT_DATA);
305 if (!(MCLGET(m, M_NOWAIT))) {
309 m->m_pkthdr.len = m->m_len = len;
310 bcopy (buf, mtod (m, caddr_t), len);
314 static int ce_probe (device_t dev)
316 if ((pci_get_vendor (dev) == TAU32_PCI_VENDOR_ID) &&
317 (pci_get_device (dev) == TAU32_PCI_DEVICE_ID)) {
318 device_set_desc (dev, "Cronyx-Tau32-PCI serial adapter");
319 return BUS_PROBE_DEFAULT;
324 static void ce_timeout (void *arg)
329 for (i = 0; i < NBRD; ++i) {
330 if (adapter[i] == NULL)
332 for (k = 0; k < NCHAN; ++k) {
338 d = channel[i * NCHAN + k];
343 CE_LOCK ((bdrv_t *)d->board->sys);
344 switch (d->chan->type) {
346 ce_e1_timer (d->chan);
351 CE_UNLOCK ((bdrv_t *)d->board->sys);
357 callout_reset (&timeout_handle, hz, ce_timeout, 0);
361 static void ce_led_off (void *arg)
364 bdrv_t *bd = (bdrv_t *) b->sys;
372 TAU32_LedSet (b->ddk.pControllerObject, 0);
377 static void ce_intr (void *arg)
380 ce_board_t *b = bd->board;
383 #if __FreeBSD_version >= 500000 && defined NETGRAPH
393 TAU32_LedSet (b->ddk.pControllerObject, 1);
395 TAU32_HandleInterrupt (b->ddk.pControllerObject);
397 /* Turn LED off 50 msec later. */
398 callout_reset (&led_timo[b->num], hz/20, ce_led_off, b);
402 /* Pass packets in a lock-free state */
403 for (i = 0; i < NCHAN && b->chan[i].type; i++) {
404 drv_t *d = b->chan[i].sys;
406 if (!d || !d->running)
408 while (_IF_QLEN(&d->rqueue)) {
409 IF_DEQUEUE (&d->rqueue,m);
414 #if __FreeBSD_version >= 500000
415 NG_SEND_DATA_ONLY (error, d->hook, m);
417 ng_queue_data (d->hook, m, 0);
420 IF_DRAIN (&d->rqueue);
423 sppp_input (d->ifp, m);
429 #if __FreeBSD_version >= 500000
431 ce_bus_dmamap_addr (void *arg, bus_dma_segment_t *segs, int nseg, int error)
438 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
440 *addr = segs->ds_addr;
444 #define BUS_DMA_ZERO 0
448 ce_bus_dma_mem_alloc (int bnum, int cnum, ce_dma_mem_t *dmem)
452 error = bus_dma_tag_create (NULL, 16, 0, BUS_SPACE_MAXADDR_32BIT,
453 BUS_SPACE_MAXADDR, NULL, NULL, dmem->size, 1,
455 #if __FreeBSD_version >= 502000
460 if (cnum >= 0) printf ("ce%d-%d: ", bnum, cnum);
461 else printf ("ce%d: ", bnum);
462 printf ("couldn't allocate tag for dma memory\n");
465 error = bus_dmamem_alloc (dmem->dmat, (void **)&dmem->virt,
466 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &dmem->mapp);
468 if (cnum >= 0) printf ("ce%d-%d: ", bnum, cnum);
469 else printf ("ce%d: ", bnum);
470 printf ("couldn't allocate mem for dma memory\n");
471 bus_dma_tag_destroy (dmem->dmat);
474 error = bus_dmamap_load (dmem->dmat, dmem->mapp, dmem->virt,
475 dmem->size, ce_bus_dmamap_addr, &dmem->phys, 0);
477 if (cnum >= 0) printf ("ce%d-%d: ", bnum, cnum);
478 else printf ("ce%d: ", bnum);
479 printf ("couldn't load mem map for dma memory\n");
480 bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
481 bus_dma_tag_destroy (dmem->dmat);
484 #if __FreeBSD_version >= 502000
485 bzero (dmem->virt, dmem->size);
491 ce_bus_dma_mem_free (ce_dma_mem_t *dmem)
493 bus_dmamap_unload (dmem->dmat, dmem->mapp);
494 bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
495 bus_dma_tag_destroy (dmem->dmat);
499 ce_bus_dma_mem_alloc (int bnum, int cnum, ce_dma_mem_t *dmem)
501 dmem->virt = contigmalloc (dmem->size, M_DEVBUF, M_WAITOK,
502 0x100000, 0xffffffff, 16, 0);
503 if (dmem->virt == NULL) {
504 if (cnum >= 0) printf ("ce%d-%d: ", bnum, cnum);
505 else printf ("ce%d: ", bnum);
506 printf ("couldn't allocate dma memory\n");
509 dmem->phys = vtophys (dmem->virt);
510 bzero (dmem->virt, dmem->size);
515 ce_bus_dma_mem_free (ce_dma_mem_t *dmem)
517 contigfree (dmem->virt, dmem->size, M_DEVBUF);
522 * Called if the probe succeeded.
524 static int ce_attach (device_t dev)
526 bdrv_t *bd = device_get_softc (dev);
527 int unit = device_get_unit (dev);
528 #if __FreeBSD_version >= 504000
529 char *ce_ln = CE_LOCK_NAME;
538 b = malloc (sizeof(ce_board_t), M_DEVBUF, M_WAITOK);
540 printf ("ce%d: couldn't allocate memory\n", unit);
543 bzero (b, sizeof(ce_board_t));
547 #if __FreeBSD_version >= 440000
548 pci_enable_busmaster (dev);
551 bd->dmamem.size = TAU32_ControllerObjectSize;
552 if (! ce_bus_dma_mem_alloc (unit, -1, &bd->dmamem)) {
556 b->ddk.pControllerObject = bd->dmamem.virt;
561 bd->ce_res = bus_alloc_resource (dev, SYS_RES_MEMORY, &rid,
562 0, ~0, 1, RF_ACTIVE);
564 printf ("ce%d: cannot map memory\n", unit);
565 ce_bus_dma_mem_free (&bd->dmamem);
569 vbase = (vm_offset_t) rman_get_virtual (bd->ce_res);
571 b->ddk.PciBar1VirtualAddress = (void *)vbase;
572 b->ddk.ControllerObjectPhysicalAddress = bd->dmamem.phys;
573 b->ddk.pErrorNotifyCallback = ce_error_callback;
574 b->ddk.pStatusNotifyCallback = ce_status_callback;
577 TAU32_BeforeReset(&b->ddk);
578 pci_write_config (dev, TAU32_PCI_RESET_ADDRESS, TAU32_PCI_RESET_ON, 4);
579 pci_write_config (dev, TAU32_PCI_RESET_ADDRESS, TAU32_PCI_RESET_OFF, 4);
581 if(!TAU32_Initialize(&b->ddk, 0))
583 printf ("ce%d: init adapter error 0x%08x, bus dead bits 0x%08lx\n",
584 unit, b->ddk.InitErrors, b->ddk.DeadBits);
585 bus_release_resource (dev, SYS_RES_MEMORY, PCIR_BAR(0), bd->ce_res);
586 ce_bus_dma_mem_free (&bd->dmamem);
596 bd->ce_irq = bus_alloc_resource (dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
597 RF_SHAREABLE | RF_ACTIVE);
599 printf ("ce%d: cannot map interrupt\n", unit);
600 bus_release_resource (dev, SYS_RES_MEMORY, PCIR_BAR(0), bd->ce_res);
601 ce_bus_dma_mem_free (&bd->dmamem);
606 #if __FreeBSD_version >= 500000
607 callout_init (&led_timo[unit], 1);
609 callout_init (&led_timo[unit]);
611 error = bus_setup_intr (dev, bd->ce_irq,
612 #if __FreeBSD_version >= 500013
613 INTR_TYPE_NET|INTR_MPSAFE,
617 NULL, ce_intr, bd, &bd->ce_intrhand);
619 printf ("ce%d: cannot set up irq\n", unit);
620 bus_release_resource (dev, SYS_RES_IRQ, 0, bd->ce_irq);
621 bus_release_resource (dev, SYS_RES_MEMORY,
622 PCIR_BAR(0), bd->ce_res);
623 ce_bus_dma_mem_free (&bd->dmamem);
629 switch (b->ddk.Model) {
630 case 1: strcpy (b->name, TAU32_BASE_NAME); break;
631 case 2: strcpy (b->name, TAU32_LITE_NAME); break;
632 case 3: strcpy (b->name, TAU32_ADPCM_NAME); break;
633 default: strcpy (b->name, TAU32_UNKNOWN_NAME); break;
636 printf ("ce%d: %s\n", unit, b->name);
638 for (c = b->chan; c < b->chan + NCHAN; ++c) {
639 c->num = (c - b->chan);
642 d = &bd->channel[c->num];
643 d->dmamem.size = sizeof(ce_buf_t);
644 if (! ce_bus_dma_mem_alloc (unit, c->num, &d->dmamem))
647 channel [b->num * NCHAN + c->num] = d;
648 sprintf (d->name, "ce%d.%d", b->num, c->num);
654 for (c = b->chan; c < b->chan + NCHAN; ++c) {
659 callout_init (&d->timeout_handle, 1);
661 if (ng_make_node_common (&typestruct, &d->node) != 0) {
662 printf ("%s: cannot make common node\n", d->name);
666 #if __FreeBSD_version >= 500000
667 NG_NODE_SET_PRIVATE (d->node, d);
669 d->node->private = d;
671 sprintf (d->nodename, "%s%d", NG_CE_NODE_TYPE,
672 c->board->num * NCHAN + c->num);
673 if (ng_name_node (d->node, d->nodename)) {
674 printf ("%s: cannot name node\n", d->nodename);
675 #if __FreeBSD_version >= 500000
676 NG_NODE_UNREF (d->node);
683 d->queue.ifq_maxlen = ifqmaxlen;
684 d->hi_queue.ifq_maxlen = ifqmaxlen;
685 d->rqueue.ifq_maxlen = ifqmaxlen;
686 #if __FreeBSD_version >= 500000
687 mtx_init (&d->queue.ifq_mtx, "ce_queue", NULL, MTX_DEF);
688 mtx_init (&d->hi_queue.ifq_mtx, "ce_queue_hi", NULL, MTX_DEF);
689 mtx_init (&d->rqueue.ifq_mtx, "ce_rqueue", NULL, MTX_DEF);
692 #if __FreeBSD_version >= 600031
693 d->ifp = if_alloc(IFT_PPP);
695 d->ifp = malloc (sizeof(struct sppp), M_DEVBUF, M_WAITOK);
696 bzero (d->ifp, sizeof(struct sppp));
699 printf ("%s: cannot if_alloc() interface\n", d->name);
702 d->ifp->if_softc = d;
703 #if __FreeBSD_version > 501000
704 if_initname (d->ifp, "ce", b->num * NCHAN + c->num);
706 d->ifp->if_unit = b->num * NCHAN + c->num;
707 d->ifp->if_name = "ce";
709 d->ifp->if_mtu = PP_MTU;
710 d->ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
711 d->ifp->if_ioctl = ce_sioctl;
712 d->ifp->if_start = ce_ifstart;
713 d->ifp->if_init = ce_initialize;
714 d->rqueue.ifq_maxlen = ifqmaxlen;
715 #if __FreeBSD_version >= 500000
716 mtx_init (&d->rqueue.ifq_mtx, "ce_rqueue", NULL, MTX_DEF);
718 sppp_attach (d->ifp);
720 IFP2SP(d->ifp)->pp_tlf = ce_tlf;
721 IFP2SP(d->ifp)->pp_tls = ce_tls;
722 /* If BPF is in the kernel, call the attach for it.
723 * The header size of PPP or Cisco/HDLC is 4 bytes. */
724 bpfattach (d->ifp, DLT_PPP, 4);
726 ce_start_chan (c, 1, 1, d->dmamem.virt, d->dmamem.phys);
728 /* Register callback functions. */
729 ce_register_transmit (c, &ce_transmit);
730 ce_register_receive (c, &ce_receive);
731 ce_register_error (c, &ce_error);
732 d->devt = make_dev (&ce_cdevsw, b->num*NCHAN+c->num, UID_ROOT,
733 GID_WHEEL, 0600, "ce%d", b->num*NCHAN+c->num);
736 #if __FreeBSD_version >= 504000
737 ce_ln[2] = '0' + unit;
738 mtx_init (&bd->ce_mtx, ce_ln, MTX_NETWORK_LOCK, MTX_DEF|MTX_RECURSE);
741 TAU32_EnableInterrupts(b->ddk.pControllerObject);
749 static int ce_detach (device_t dev)
751 bdrv_t *bd = device_get_softc (dev);
752 ce_board_t *b = bd->board;
756 #if __FreeBSD_version >= 504000
757 KASSERT (mtx_initialized (&bd->ce_mtx), ("ce mutex not initialized"));
761 /* Check if the device is busy (open). */
762 for (c = b->chan; c < b->chan + NCHAN; ++c) {
763 drv_t *d = (drv_t*) c->sys;
765 /* XXX Non existen chan! */
766 if (! d || ! d->chan)
775 /* Ok, we can unload driver */
776 /* At first we should disable interrupts */
778 TAU32_DisableInterrupts(b->ddk.pControllerObject);
780 callout_stop (&led_timo[b->num]);
782 for (c = b->chan; c < b->chan + NCHAN; ++c) {
783 drv_t *d = (drv_t*) c->sys;
785 if (! d || ! d->chan)
787 callout_stop (&d->timeout_handle);
789 /* Detach from the packet filter list of interfaces. */
792 /* Detach from the sync PPP list. */
793 sppp_detach (d->ifp);
795 /* Detach from the system list of interfaces. */
797 #if __FreeBSD_version > 600031
800 free (d->ifp, M_DEVBUF);
803 IF_DRAIN (&d->rqueue);
804 #if __FreeBSD_version >= 500000
805 mtx_destroy (&d->rqueue.ifq_mtx);
808 #if __FreeBSD_version >= 500000
810 ng_rmnode_self (d->node);
811 NG_NODE_UNREF (d->node);
814 IF_DRAIN (&d->rqueue);
815 mtx_destroy (&d->queue.ifq_mtx);
816 mtx_destroy (&d->hi_queue.ifq_mtx);
817 mtx_destroy (&d->rqueue.ifq_mtx);
823 destroy_dev (d->devt);
829 callout_drain (&led_timo[b->num]);
831 /* Disable the interrupt request. */
832 bus_teardown_intr (dev, bd->ce_irq, bd->ce_intrhand);
833 bus_release_resource (dev, SYS_RES_IRQ, 0, bd->ce_irq);
834 TAU32_DestructiveHalt (b->ddk.pControllerObject, 0);
835 bus_release_resource (dev, SYS_RES_MEMORY, PCIR_BAR(0), bd->ce_res);
837 for (c = b->chan; c < b->chan + NCHAN; ++c) {
838 drv_t *d = (drv_t*) c->sys;
840 if (! d || ! d->chan)
842 callout_drain (&d->timeout_handle);
843 channel [b->num * NCHAN + c->num] = 0;
844 /* Deallocate buffers. */
845 ce_bus_dma_mem_free (&d->dmamem);
847 adapter [b->num] = 0;
848 ce_bus_dma_mem_free (&bd->dmamem);
850 #if __FreeBSD_version >= 504000
851 mtx_destroy (&bd->ce_mtx);
857 static void ce_ifstart (struct ifnet *ifp)
859 drv_t *d = ifp->if_softc;
860 bdrv_t *bd = d->board->sys;
867 static void ce_tlf (struct sppp *sp)
869 drv_t *d = SP2IFP(sp)->if_softc;
871 CE_DEBUG2 (d, ("ce_tlf\n"));
875 static void ce_tls (struct sppp *sp)
877 drv_t *d = SP2IFP(sp)->if_softc;
879 CE_DEBUG2 (d, ("ce_tls\n"));
884 * Process an ioctl request.
886 static int ce_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data)
888 drv_t *d = ifp->if_softc;
889 bdrv_t *bd = d->board->sys;
890 int error, s, was_up, should_be_up;
892 #if __FreeBSD_version >= 600034
893 was_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
895 was_up = (ifp->if_flags & IFF_RUNNING) != 0;
897 error = sppp_ioctl (ifp, cmd, data);
902 if (! (ifp->if_flags & IFF_DEBUG))
905 d->chan->debug = d->chan->debug_shadow;
908 default: CE_DEBUG2 (d, ("ioctl 0x%lx\n", cmd)); return 0;
909 case SIOCADDMULTI: CE_DEBUG2 (d, ("ioctl SIOCADDMULTI\n")); return 0;
910 case SIOCDELMULTI: CE_DEBUG2 (d, ("ioctl SIOCDELMULTI\n")); return 0;
911 case SIOCSIFFLAGS: CE_DEBUG2 (d, ("ioctl SIOCSIFFLAGS\n")); break;
912 case SIOCSIFADDR: CE_DEBUG2 (d, ("ioctl SIOCSIFADDR\n")); break;
915 /* We get here only in case of SIFFLAGS or SIFADDR. */
918 #if __FreeBSD_version >= 600034
919 should_be_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
921 should_be_up = (ifp->if_flags & IFF_RUNNING) != 0;
923 if (! was_up && should_be_up) {
924 /* Interface goes up -- start it. */
927 } else if (was_up && ! should_be_up) {
928 /* Interface is going down -- stop it. */
929 /* if ((IFP2SP(ifp)->pp_flags & PP_FR) || (ifp->if_flags & PP_CISCO))*/
932 CE_DEBUG (d, ("ioctl 0x%lx p4\n", cmd));
939 * Initialization of interface.
940 * It seems to be never called by upper level?
942 static void ce_initialize (void *softc)
946 CE_DEBUG (d, ("ce_initialize\n"));
951 * Stop the interface. Called on splimp().
953 static void ce_down (drv_t *d)
955 CE_DEBUG (d, ("ce_down\n"));
956 /* Interface is going down -- stop it. */
957 ce_set_dtr (d->chan, 0);
958 ce_set_rts (d->chan, 0);
961 callout_stop (&d->timeout_handle);
965 * Start the interface. Called on splimp().
967 static void ce_up (drv_t *d)
969 CE_DEBUG (d, ("ce_up\n"));
970 ce_set_dtr (d->chan, 1);
971 ce_set_rts (d->chan, 1);
977 * Start output on the interface. Get another datagram to send
978 * off of the interface queue, and copy it to the interface
979 * before starting the output.
981 static void ce_send (drv_t *d)
986 CE_DEBUG2 (d, ("ce_send\n"));
988 /* No output if the interface is down. */
992 while (ce_transmit_space (d->chan)) {
993 /* Get the packet to send. */
995 IF_DEQUEUE (&d->hi_queue, m);
997 IF_DEQUEUE (&d->queue, m);
999 m = sppp_dequeue (d->ifp);
1004 #if __FreeBSD_version >= 500000
1005 BPF_MTAP (d->ifp, m);
1008 bpf_mtap (d->ifp, m);
1011 #if __FreeBSD_version >= 490000
1012 len = m_length (m, NULL);
1014 len = m->m_pkthdr.len;
1017 printf ("%s: too long packet: %d bytes: ",
1019 else if (! m->m_next)
1020 ce_send_packet (d->chan, (u_char*) mtod (m, caddr_t), len, 0);
1022 ce_buf_item_t *item = (ce_buf_item_t*)d->chan->tx_queue;
1023 m_copydata (m, 0, len, item->buf);
1024 ce_send_packet (d->chan, item->buf, len, 0);
1027 /* Set up transmit timeout, if the transmit ring is not empty.*/
1031 #if __FreeBSD_version >= 600034
1032 d->ifp->if_flags |= IFF_DRV_OACTIVE;
1034 d->ifp->if_flags |= IFF_OACTIVE;
1040 * Start output on the interface.
1041 * Always called on splimp().
1043 static void ce_start (drv_t *d)
1047 ce_set_dtr (d->chan, 1);
1049 ce_set_rts (d->chan, 1);
1051 callout_reset (&d->timeout_handle, hz, ce_watchdog_timer, d);
1056 * Handle transmit timeouts.
1057 * Recover after lost transmit interrupts.
1058 * Always called on splimp().
1060 static void ce_watchdog (drv_t *d)
1062 CE_DEBUG (d, ("device timeout\n"));
1064 ce_set_dtr (d->chan, 0);
1065 ce_set_rts (d->chan, 0);
1066 /* ce_stop_chan (d->chan);*/
1067 /* ce_start_chan (d->chan, 1, 1, 0, 0);*/
1068 ce_set_dtr (d->chan, 1);
1069 ce_set_rts (d->chan, 1);
1074 static void ce_watchdog_timer (void *arg)
1077 bdrv_t *bd = d->board->sys;
1080 if (d->timeout == 1)
1084 callout_reset (&d->timeout_handle, hz, ce_watchdog_timer, d);
1088 static void ce_transmit (ce_chan_t *c, void *attachment, int len)
1094 if_inc_counter(d->ifp, IFCOUNTER_OPACKETS, 1);
1095 #if __FreeBSD_version >= 600034
1096 d->ifp->if_flags &= ~IFF_DRV_OACTIVE;
1098 d->ifp->if_flags &= ~IFF_OACTIVE;
1104 static void ce_receive (ce_chan_t *c, unsigned char *data, int len)
1112 m = makembuf (data, len);
1114 CE_DEBUG (d, ("no memory for packet\n"));
1116 if_inc_counter(d->ifp, IFCOUNTER_IQDROPS, 1);
1123 m->m_pkthdr.rcvif = 0;
1124 IF_ENQUEUE(&d->rqueue, m);
1126 if_inc_counter(d->ifp, IFCOUNTER_IPACKETS, 1);
1127 m->m_pkthdr.rcvif = d->ifp;
1128 /* Check if there's a BPF listener on this interface.
1129 * If so, hand off the raw packet to bpf. */
1130 BPF_MTAP(d->ifp, m);
1131 IF_ENQUEUE(&d->rqueue, m);
1135 static void ce_error (ce_chan_t *c, int data)
1141 CE_DEBUG (d, ("frame error\n"));
1143 if_inc_counter(d->ifp, IFCOUNTER_IERRORS, 1);
1147 CE_DEBUG (d, ("crc error\n"));
1149 if_inc_counter(d->ifp, IFCOUNTER_IERRORS, 1);
1153 CE_DEBUG (d, ("overrun error\n"));
1155 if_inc_counter(d->ifp, IFCOUNTER_COLLISIONS, 1);
1156 if_inc_counter(d->ifp, IFCOUNTER_IERRORS, 1);
1160 CE_DEBUG (d, ("overflow error\n"));
1162 if_inc_counter(d->ifp, IFCOUNTER_IERRORS, 1);
1166 CE_DEBUG (d, ("underrun error\n"));
1169 if_inc_counter(d->ifp, IFCOUNTER_OERRORS, 1);
1170 #if __FreeBSD_version >= 600034
1171 d->ifp->if_flags &= ~IFF_DRV_OACTIVE;
1173 d->ifp->if_flags &= ~IFF_OACTIVE;
1179 CE_DEBUG (d, ("error #%d\n", data));
1185 * You also need read, write, open, close routines.
1186 * This should get you started
1188 #if __FreeBSD_version < 500000
1189 static int ce_open (dev_t dev, int oflags, int devtype, struct proc *p)
1191 static int ce_open (struct cdev *dev, int oflags, int devtype, struct thread *td)
1194 int unit = dev2unit (dev);
1197 if (unit >= NBRD*NCHAN || ! (d = channel[unit]))
1199 CE_DEBUG2 (d, ("ce_open\n"));
1204 * Only called on the LAST close.
1206 #if __FreeBSD_version < 500000
1207 static int ce_close (dev_t dev, int fflag, int devtype, struct proc *p)
1209 static int ce_close (struct cdev *dev, int fflag, int devtype, struct thread *td)
1212 drv_t *d = channel [dev2unit (dev)];
1214 CE_DEBUG2 (d, ("ce_close\n"));
1218 static int ce_modem_status (ce_chan_t *c)
1221 bdrv_t *bd = d->board->sys;
1224 status = d->running ? TIOCM_LE : 0;
1227 if (ce_get_cd (c)) status |= TIOCM_CD;
1228 if (ce_get_cts (c)) status |= TIOCM_CTS;
1229 if (ce_get_dsr (c)) status |= TIOCM_DSR;
1230 if (c->dtr) status |= TIOCM_DTR;
1231 if (c->rts) status |= TIOCM_RTS;
1237 #if __FreeBSD_version < 500000
1238 static int ce_ioctl (dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
1240 static int ce_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
1243 drv_t *d = channel [dev2unit (dev)];
1244 bdrv_t *bd = d->board->sys;
1245 ce_chan_t *c = d->chan;
1246 struct serial_statistics *st;
1247 struct e1_statistics *opte1;
1252 case SERIAL_GETREGISTERED:
1253 CE_DEBUG2 (d, ("ioctl: getregistered\n"));
1254 bzero (mask, sizeof(mask));
1255 for (s=0; s<NBRD*NCHAN; ++s)
1257 mask [s/8] |= 1 << (s & 7);
1258 bcopy (mask, data, sizeof (mask));
1262 case SERIAL_GETPROTO:
1263 CE_DEBUG2 (d, ("ioctl: getproto\n"));
1264 strcpy ((char*)data, (IFP2SP(d->ifp)->pp_flags & PP_FR) ? "fr" :
1265 (d->ifp->if_flags & PP_CISCO) ? "cisco" : "ppp");
1268 case SERIAL_SETPROTO:
1269 CE_DEBUG2 (d, ("ioctl: setproto\n"));
1270 /* Only for superuser! */
1271 #if __FreeBSD_version < 500000
1273 #elif __FreeBSD_version < 700000
1276 error = priv_check (td, PRIV_DRIVER);
1280 #if __FreeBSD_version >= 600034
1281 if (d->ifp->if_flags & IFF_DRV_RUNNING)
1283 if (d->ifp->if_flags & IFF_RUNNING)
1286 if (! strcmp ("cisco", (char*)data)) {
1287 IFP2SP(d->ifp)->pp_flags &= ~(PP_FR);
1288 IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE;
1289 d->ifp->if_flags |= PP_CISCO;
1291 } else if (! strcmp ("fr", (char*)data)) {
1292 d->ifp->if_flags &= ~(PP_CISCO);
1293 IFP2SP(d->ifp)->pp_flags |= PP_FR | PP_KEEPALIVE;
1295 } else if (! strcmp ("ppp", (char*)data)) {
1296 IFP2SP(d->ifp)->pp_flags &= ~PP_FR;
1297 IFP2SP(d->ifp)->pp_flags &= ~PP_KEEPALIVE;
1298 d->ifp->if_flags &= ~(PP_CISCO);
1303 case SERIAL_GETKEEPALIVE:
1304 CE_DEBUG2 (d, ("ioctl: getkeepalive\n"));
1305 if ((IFP2SP(d->ifp)->pp_flags & PP_FR) ||
1306 (d->ifp->if_flags & PP_CISCO))
1308 *(int*)data = (IFP2SP(d->ifp)->pp_flags & PP_KEEPALIVE) ? 1 : 0;
1311 case SERIAL_SETKEEPALIVE:
1312 CE_DEBUG2 (d, ("ioctl: setkeepalive\n"));
1313 /* Only for superuser! */
1314 #if __FreeBSD_version < 500000
1316 #elif __FreeBSD_version < 700000
1319 error = priv_check (td, PRIV_DRIVER);
1323 if ((IFP2SP(d->ifp)->pp_flags & PP_FR) ||
1324 (d->ifp->if_flags & PP_CISCO))
1329 IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE;
1331 IFP2SP(d->ifp)->pp_flags &= ~PP_KEEPALIVE;
1337 case SERIAL_GETMODE:
1338 CE_DEBUG2 (d, ("ioctl: getmode\n"));
1339 *(int*)data = SERIAL_HDLC;
1342 case SERIAL_SETMODE:
1343 /* Only for superuser! */
1344 #if __FreeBSD_version < 500000
1346 #elif __FreeBSD_version < 700000
1349 error = priv_check (td, PRIV_DRIVER);
1353 if (*(int*)data != SERIAL_HDLC)
1358 CE_DEBUG2 (d, ("ioctl: getcfg\n"));
1363 CE_DEBUG2 (d, ("ioctl: setcfg\n"));
1364 #if __FreeBSD_version < 500000
1366 #elif __FreeBSD_version < 700000
1369 error = priv_check (td, PRIV_DRIVER);
1373 if (*((char*)data) != 'c')
1377 case SERIAL_GETSTAT:
1378 CE_DEBUG2 (d, ("ioctl: getstat\n"));
1379 st = (struct serial_statistics*) data;
1380 st->rintr = c->rintr;
1381 st->tintr = c->tintr;
1383 st->ibytes = c->ibytes;
1384 st->ipkts = c->ipkts;
1385 st->obytes = c->obytes;
1386 st->opkts = c->opkts;
1387 st->ierrs = c->overrun + c->frame + c->crc;
1388 st->oerrs = c->underrun;
1391 case SERIAL_GETESTAT:
1392 CE_DEBUG2 (d, ("ioctl: getestat\n"));
1393 if (c->type != T_E1)
1395 opte1 = (struct e1_statistics*) data;
1398 if (c->status & ESTS_NOALARM)
1399 opte1->status |= E1_NOALARM;
1400 if (c->status & ESTS_LOS)
1401 opte1->status |= E1_LOS;
1402 if (c->status & ESTS_LOF)
1403 opte1->status |= E1_LOF;
1404 if (c->status & ESTS_AIS)
1405 opte1->status |= E1_AIS;
1406 if (c->status & ESTS_LOMF)
1407 opte1->status |= E1_LOMF;
1408 if (c->status & ESTS_AIS16)
1409 opte1->status |= E1_AIS16;
1410 if (c->status & ESTS_FARLOF)
1411 opte1->status |= E1_FARLOF;
1412 if (c->status & ESTS_FARLOMF)
1413 opte1->status |= E1_FARLOMF;
1414 if (c->status & ESTS_TSTREQ)
1415 opte1->status |= E1_TSTREQ;
1416 if (c->status & ESTS_TSTERR)
1417 opte1->status |= E1_TSTERR;
1419 opte1->cursec = c->cursec;
1420 opte1->totsec = c->totsec + c->cursec;
1422 opte1->currnt.bpv = c->currnt.bpv;
1423 opte1->currnt.fse = c->currnt.fse;
1424 opte1->currnt.crce = c->currnt.crce;
1425 opte1->currnt.rcrce = c->currnt.rcrce;
1426 opte1->currnt.uas = c->currnt.uas;
1427 opte1->currnt.les = c->currnt.les;
1428 opte1->currnt.es = c->currnt.es;
1429 opte1->currnt.bes = c->currnt.bes;
1430 opte1->currnt.ses = c->currnt.ses;
1431 opte1->currnt.oofs = c->currnt.oofs;
1432 opte1->currnt.css = c->currnt.css;
1433 opte1->currnt.dm = c->currnt.dm;
1435 opte1->total.bpv = c->total.bpv + c->currnt.bpv;
1436 opte1->total.fse = c->total.fse + c->currnt.fse;
1437 opte1->total.crce = c->total.crce + c->currnt.crce;
1438 opte1->total.rcrce = c->total.rcrce + c->currnt.rcrce;
1439 opte1->total.uas = c->total.uas + c->currnt.uas;
1440 opte1->total.les = c->total.les + c->currnt.les;
1441 opte1->total.es = c->total.es + c->currnt.es;
1442 opte1->total.bes = c->total.bes + c->currnt.bes;
1443 opte1->total.ses = c->total.ses + c->currnt.ses;
1444 opte1->total.oofs = c->total.oofs + c->currnt.oofs;
1445 opte1->total.css = c->total.css + c->currnt.css;
1446 opte1->total.dm = c->total.dm + c->currnt.dm;
1447 for (s=0; s<48; ++s) {
1448 opte1->interval[s].bpv = c->interval[s].bpv;
1449 opte1->interval[s].fse = c->interval[s].fse;
1450 opte1->interval[s].crce = c->interval[s].crce;
1451 opte1->interval[s].rcrce = c->interval[s].rcrce;
1452 opte1->interval[s].uas = c->interval[s].uas;
1453 opte1->interval[s].les = c->interval[s].les;
1454 opte1->interval[s].es = c->interval[s].es;
1455 opte1->interval[s].bes = c->interval[s].bes;
1456 opte1->interval[s].ses = c->interval[s].ses;
1457 opte1->interval[s].oofs = c->interval[s].oofs;
1458 opte1->interval[s].css = c->interval[s].css;
1459 opte1->interval[s].dm = c->interval[s].dm;
1463 case SERIAL_CLRSTAT:
1464 CE_DEBUG2 (d, ("ioctl: clrstat\n"));
1465 /* Only for superuser! */
1466 #if __FreeBSD_version < 500000
1468 #elif __FreeBSD_version < 700000
1471 error = priv_check (td, PRIV_DRIVER);
1485 bzero (&c->currnt, sizeof (c->currnt));
1486 bzero (&c->total, sizeof (c->total));
1487 bzero (c->interval, sizeof (c->interval));
1490 case SERIAL_GETLOOP:
1491 CE_DEBUG2 (d, ("ioctl: getloop\n"));
1492 if (c->type != T_E1)
1494 *(int*)data = c->lloop;
1497 case SERIAL_SETLOOP:
1498 CE_DEBUG2 (d, ("ioctl: setloop\n"));
1499 if (c->type != T_E1)
1501 /* Only for superuser! */
1502 #if __FreeBSD_version < 500000
1504 #elif __FreeBSD_version < 700000
1507 error = priv_check (td, PRIV_DRIVER);
1513 ce_set_lloop (c, *(int*)data);
1518 case SERIAL_GETRLOOP:
1519 CE_DEBUG2 (d, ("ioctl: getrloop\n"));
1520 if (c->type != T_E1)
1522 *(int*)data = c->rloop;
1525 case SERIAL_SETRLOOP:
1526 CE_DEBUG2 (d, ("ioctl: setloop\n"));
1527 if (c->type != T_E1)
1529 /* Only for superuser! */
1530 #if __FreeBSD_version < 500000
1532 #elif __FreeBSD_version < 700000
1535 error = priv_check (td, PRIV_DRIVER);
1541 ce_set_rloop (c, *(int*)data);
1546 case SERIAL_GETDEBUG:
1547 CE_DEBUG2 (d, ("ioctl: getdebug\n"));
1548 *(int*)data = d->chan->debug;
1551 case SERIAL_SETDEBUG:
1552 CE_DEBUG2 (d, ("ioctl: setdebug\n"));
1553 /* Only for superuser! */
1554 #if __FreeBSD_version < 500000
1556 #elif __FreeBSD_version < 700000
1559 error = priv_check (td, PRIV_DRIVER);
1565 * The debug_shadow is always greater than zero for logic
1566 * simplicity. For switching debug off the IFF_DEBUG is
1569 d->chan->debug_shadow = (*(int*)data) ? (*(int*)data) : 1;
1570 if (d->ifp->if_flags & IFF_DEBUG)
1571 d->chan->debug = d->chan->debug_shadow;
1573 d->chan->debug = *(int*)data;
1577 case SERIAL_GETBAUD:
1578 CE_DEBUG2 (d, ("ioctl: getbaud\n"));
1579 *(long*)data = c->baud;
1582 case SERIAL_SETBAUD:
1583 CE_DEBUG2 (d, ("ioctl: setbaud\n"));
1584 if (c->type != T_E1 || !c->unfram)
1586 /* Only for superuser! */
1587 #if __FreeBSD_version < 500000
1589 #elif __FreeBSD_version < 700000
1592 error = priv_check (td, PRIV_DRIVER);
1598 ce_set_baud (c, *(long*)data);
1603 case SERIAL_GETTIMESLOTS:
1604 CE_DEBUG2 (d, ("ioctl: gettimeslots\n"));
1605 if ((c->type != T_E1 || c->unfram) && c->type != T_DATA)
1607 *(u_long*)data = c->ts;
1610 case SERIAL_SETTIMESLOTS:
1611 CE_DEBUG2 (d, ("ioctl: settimeslots\n"));
1612 /* Only for superuser! */
1613 #if __FreeBSD_version < 500000
1615 #elif __FreeBSD_version < 700000
1618 error = priv_check (td, PRIV_DRIVER);
1622 if ((c->type != T_E1 || c->unfram) && c->type != T_DATA)
1626 ce_set_ts (c, *(u_long*)data);
1631 case SERIAL_GETHIGAIN:
1632 CE_DEBUG2 (d, ("ioctl: gethigain\n"));
1633 if (c->type != T_E1)
1635 *(int*)data = c->higain;
1638 case SERIAL_SETHIGAIN:
1639 CE_DEBUG2 (d, ("ioctl: sethigain\n"));
1640 if (c->type != T_E1)
1642 /* Only for superuser! */
1643 #if __FreeBSD_version < 500000
1645 #elif __FreeBSD_version < 700000
1648 error = priv_check (td, PRIV_DRIVER);
1654 ce_set_higain (c, *(int*)data);
1659 case SERIAL_GETPHONY:
1660 CE_DEBUG2 (d, ("ioctl: getphony\n"));
1661 *(int*)data = c->phony;
1664 case SERIAL_SETPHONY:
1665 CE_DEBUG2 (d, ("ioctl: setphony\n"));
1666 /* Only for superuser! */
1667 #if __FreeBSD_version < 500000
1669 #elif __FreeBSD_version < 700000
1672 error = priv_check (td, PRIV_DRIVER);
1678 ce_set_phony (c, *(int*)data);
1683 case SERIAL_GETUNFRAM:
1684 CE_DEBUG2 (d, ("ioctl: getunfram\n"));
1685 if (c->type != T_E1 || c->num != 0)
1687 *(int*)data = c->unfram;
1690 case SERIAL_SETUNFRAM:
1691 CE_DEBUG2 (d, ("ioctl: setunfram\n"));
1692 if (c->type != T_E1 || c->num != 0)
1694 /* Only for superuser! */
1695 #if __FreeBSD_version < 500000
1697 #elif __FreeBSD_version < 700000
1700 error = priv_check (td, PRIV_DRIVER);
1706 ce_set_unfram (c, *(int*)data);
1711 case SERIAL_GETSCRAMBLER:
1712 CE_DEBUG2 (d, ("ioctl: getscrambler\n"));
1715 *(int*)data = c->scrambler;
1718 case SERIAL_SETSCRAMBLER:
1719 CE_DEBUG2 (d, ("ioctl: setscrambler\n"));
1720 /* Only for superuser! */
1721 #if __FreeBSD_version < 500000
1723 #elif __FreeBSD_version < 700000
1726 error = priv_check (td, PRIV_DRIVER);
1734 ce_set_scrambler (c, *(int*)data);
1739 case SERIAL_GETMONITOR:
1740 CE_DEBUG2 (d, ("ioctl: getmonitor\n"));
1741 if (c->type != T_E1)
1743 *(int*)data = c->monitor;
1746 case SERIAL_SETMONITOR:
1747 CE_DEBUG2 (d, ("ioctl: setmonitor\n"));
1748 /* Only for superuser! */
1749 #if __FreeBSD_version < 500000
1751 #elif __FreeBSD_version < 700000
1754 error = priv_check (td, PRIV_DRIVER);
1758 if (c->type != T_E1)
1762 ce_set_monitor (c, *(int*)data);
1767 case SERIAL_GETUSE16:
1768 CE_DEBUG2 (d, ("ioctl: getuse16\n"));
1769 if (c->type != T_E1 || c->unfram)
1771 *(int*)data = c->use16;
1774 case SERIAL_SETUSE16:
1775 CE_DEBUG2 (d, ("ioctl: setuse16\n"));
1776 /* Only for superuser! */
1777 #if __FreeBSD_version < 500000
1779 #elif __FreeBSD_version < 700000
1782 error = priv_check (td, PRIV_DRIVER);
1786 if (c->type != T_E1)
1790 ce_set_use16 (c, *(int*)data);
1795 case SERIAL_GETCRC4:
1796 CE_DEBUG2 (d, ("ioctl: getcrc4\n"));
1797 if (c->type != T_E1 || c->unfram)
1799 *(int*)data = c->crc4;
1802 case SERIAL_SETCRC4:
1803 CE_DEBUG2 (d, ("ioctl: setcrc4\n"));
1804 /* Only for superuser! */
1805 #if __FreeBSD_version < 500000
1807 #elif __FreeBSD_version < 700000
1810 error = priv_check (td, PRIV_DRIVER);
1814 if (c->type != T_E1 || c->unfram)
1818 ce_set_crc4 (c, *(int*)data);
1824 CE_DEBUG2 (d, ("ioctl: getclk\n"));
1825 if (c->type != T_E1)
1828 default: *(int*)data = E1CLK_INTERNAL; break;
1829 case GSYN_RCV: *(int*)data = E1CLK_RECEIVE; break;
1830 case GSYN_RCV0: *(int*)data = E1CLK_RECEIVE_CHAN0; break;
1831 case GSYN_RCV1: *(int*)data = E1CLK_RECEIVE_CHAN1; break;
1836 CE_DEBUG2 (d, ("ioctl: setclk\n"));
1837 /* Only for superuser! */
1838 #if __FreeBSD_version < 500000
1840 #elif __FreeBSD_version < 700000
1843 error = priv_check (td, PRIV_DRIVER);
1847 if (c->type != T_E1)
1851 switch (*(int*)data) {
1852 default: ce_set_gsyn (c, GSYN_INT); break;
1853 case E1CLK_RECEIVE: ce_set_gsyn (c, GSYN_RCV); break;
1854 case E1CLK_RECEIVE_CHAN0: ce_set_gsyn (c, GSYN_RCV0); break;
1855 case E1CLK_RECEIVE_CHAN1: ce_set_gsyn (c, GSYN_RCV1); break;
1863 CE_DEBUG2 (d, ("ioctl: reset\n"));
1864 /* Only for superuser! */
1865 #if __FreeBSD_version < 500000
1867 #elif __FreeBSD_version < 700000
1870 error = priv_check (td, PRIV_DRIVER);
1876 /* ce_reset (c->board, 0, 0);*/
1881 case SERIAL_HARDRESET:
1882 CE_DEBUG2 (d, ("ioctl: hardreset\n"));
1883 /* Only for superuser! */
1884 #if __FreeBSD_version < 500000
1886 #elif __FreeBSD_version < 700000
1889 error = priv_check (td, PRIV_DRIVER);
1895 /* hard_reset (c->board); */
1901 case SERIAL_GETCABLE:
1902 CE_DEBUG2 (d, ("ioctl: getcable\n"));
1903 if (c->type != T_E1)
1907 *(int*)data = CABLE_TP;
1913 CE_DEBUG2 (d, ("ioctl: getdir\n"));
1914 if (c->type != T_E1 && c->type != T_DATA)
1916 *(int*)data = c->dir;
1920 CE_DEBUG2 (d, ("ioctl: setdir\n"));
1921 /* Only for superuser! */
1922 #if __FreeBSD_version < 500000
1924 #elif __FreeBSD_version < 700000
1927 error = priv_check (td, PRIV_DRIVER);
1933 ce_set_dir (c, *(int*)data);
1938 case TIOCSDTR: /* Set DTR */
1946 case TIOCCDTR: /* Clear DTR */
1954 case TIOCMSET: /* Set DTR/RTS */
1957 ce_set_dtr (c, (*(int*)data & TIOCM_DTR) ? 1 : 0);
1958 ce_set_rts (c, (*(int*)data & TIOCM_RTS) ? 1 : 0);
1963 case TIOCMBIS: /* Add DTR/RTS */
1966 if (*(int*)data & TIOCM_DTR) ce_set_dtr (c, 1);
1967 if (*(int*)data & TIOCM_RTS) ce_set_rts (c, 1);
1972 case TIOCMBIC: /* Clear DTR/RTS */
1975 if (*(int*)data & TIOCM_DTR) ce_set_dtr (c, 0);
1976 if (*(int*)data & TIOCM_RTS) ce_set_rts (c, 0);
1981 case TIOCMGET: /* Get modem status */
1982 *(int*)data = ce_modem_status (c);
1989 #if __FreeBSD_version >= 500000
1990 static int ng_ce_constructor (node_p node)
1992 drv_t *d = NG_NODE_PRIVATE (node);
1994 static int ng_ce_constructor (node_p *node)
1996 drv_t *d = (*node)->private;
1998 CE_DEBUG (d, ("Constructor\n"));
2002 static int ng_ce_newhook (node_p node, hook_p hook, const char *name)
2005 #if __FreeBSD_version >= 500000
2006 drv_t *d = NG_NODE_PRIVATE (node);
2008 drv_t *d = node->private;
2010 bdrv_t *bd = d->board->sys;
2012 CE_DEBUG (d, ("Newhook\n"));
2013 /* Attach debug hook */
2014 if (strcmp (name, NG_CE_HOOK_DEBUG) == 0) {
2015 #if __FreeBSD_version >= 500000
2016 NG_HOOK_SET_PRIVATE (hook, NULL);
2020 d->debug_hook = hook;
2024 /* Check for raw hook */
2025 if (strcmp (name, NG_CE_HOOK_RAW) != 0)
2028 #if __FreeBSD_version >= 500000
2029 NG_HOOK_SET_PRIVATE (hook, d);
2042 static char *format_timeslots (u_long s)
2044 static char buf [100];
2048 for (i=1; i<32; ++i)
2050 int prev = (i > 1) & (s >> (i-1));
2051 int next = (i < 31) & (s >> (i+1));
2061 *p++ = '0' + i / 10;
2062 *p++ = '0' + i % 10;
2068 static int print_modems (char *s, ce_chan_t *c, int need_header)
2070 int status = ce_modem_status (c);
2074 length += sprintf (s + length, " LE DTR DSR RTS CTS CD\n");
2075 length += sprintf (s + length, "%4s %4s %4s %4s %4s %4s\n",
2076 status & TIOCM_LE ? "On" : "-",
2077 status & TIOCM_DTR ? "On" : "-",
2078 status & TIOCM_DSR ? "On" : "-",
2079 status & TIOCM_RTS ? "On" : "-",
2080 status & TIOCM_CTS ? "On" : "-",
2081 status & TIOCM_CD ? "On" : "-");
2085 static int print_stats (char *s, ce_chan_t *c, int need_header)
2090 length += sprintf (s + length, " Rintr Tintr Mintr Ibytes Ipkts Ierrs Obytes Opkts Oerrs\n");
2091 length += sprintf (s + length, "%7ld %7ld %7ld %8lu %7ld %7ld %8lu %7ld %7ld\n",
2092 c->rintr, c->tintr, 0l, (unsigned long) c->ibytes,
2093 c->ipkts, c->overrun + c->frame + c->crc,
2094 (unsigned long) c->obytes, c->opkts, c->underrun);
2098 static char *format_e1_status (u_char status)
2100 static char buf [80];
2102 if (status & E1_NOALARM)
2105 if (status & E1_LOS) strcat (buf, ",LOS");
2106 if (status & E1_AIS) strcat (buf, ",AIS");
2107 if (status & E1_LOF) strcat (buf, ",LOF");
2108 if (status & E1_LOMF) strcat (buf, ",LOMF");
2109 if (status & E1_FARLOF) strcat (buf, ",FARLOF");
2110 if (status & E1_AIS16) strcat (buf, ",AIS16");
2111 if (status & E1_FARLOMF) strcat (buf, ",FARLOMF");
2112 if (status & E1_TSTREQ) strcat (buf, ",TSTREQ");
2113 if (status & E1_TSTERR) strcat (buf, ",TSTERR");
2119 static int print_frac (char *s, int leftalign, u_long numerator, u_long divider)
2123 if (numerator < 1 || divider < 1) {
2124 length += sprintf (s+length, leftalign ? "/- " : " -");
2127 n = (int) (0.5 + 1000.0 * numerator / divider);
2129 length += sprintf (s+length, leftalign ? "/.%-3d" : " .%03d", n);
2132 *(s + length) = leftalign ? '/' : ' ';
2135 if (n >= 1000000) n = (n+500) / 1000 * 1000;
2136 else if (n >= 100000) n = (n+50) / 100 * 100;
2137 else if (n >= 10000) n = (n+5) / 10 * 10;
2140 case 1000: length += printf (s+length, ".999"); return length;
2141 case 10000: n = 9990; break;
2142 case 100000: n = 99900; break;
2143 case 1000000: n = 999000; break;
2145 if (n < 10000) length += sprintf (s+length, "%d.%d", n/1000, n/10%100);
2146 else if (n < 100000) length += sprintf (s+length, "%d.%d", n/1000, n/100%10);
2147 else if (n < 1000000) length += sprintf (s+length, "%d.", n/1000);
2148 else length += sprintf (s+length, "%d", n/1000);
2153 static int print_e1_stats (char *s, ce_chan_t *c)
2155 struct e1_counters total;
2159 totsec = c->totsec + c->cursec;
2160 total.bpv = c->total.bpv + c->currnt.bpv;
2161 total.fse = c->total.fse + c->currnt.fse;
2162 total.crce = c->total.crce + c->currnt.crce;
2163 total.rcrce = c->total.rcrce + c->currnt.rcrce;
2164 total.uas = c->total.uas + c->currnt.uas;
2165 total.les = c->total.les + c->currnt.les;
2166 total.es = c->total.es + c->currnt.es;
2167 total.bes = c->total.bes + c->currnt.bes;
2168 total.ses = c->total.ses + c->currnt.ses;
2169 total.oofs = c->total.oofs + c->currnt.oofs;
2170 total.css = c->total.css + c->currnt.css;
2171 total.dm = c->total.dm + c->currnt.dm;
2173 length += sprintf (s + length, " Unav/Degr Bpv/Fsyn CRC/RCRC Err/Lerr Sev/Bur Oof/Slp Status\n");
2175 /* Unavailable seconds, degraded minutes */
2176 length += print_frac (s + length, 0, c->currnt.uas, c->cursec);
2177 length += print_frac (s + length, 1, 60 * c->currnt.dm, c->cursec);
2179 /* Bipolar violations, frame sync errors */
2180 length += print_frac (s + length, 0, c->currnt.bpv, c->cursec);
2181 length += print_frac (s + length, 1, c->currnt.fse, c->cursec);
2183 /* CRC errors, remote CRC errors (E-bit) */
2184 length += print_frac (s + length, 0, c->currnt.crce, c->cursec);
2185 length += print_frac (s + length, 1, c->currnt.rcrce, c->cursec);
2187 /* Errored seconds, line errored seconds */
2188 length += print_frac (s + length, 0, c->currnt.es, c->cursec);
2189 length += print_frac (s + length, 1, c->currnt.les, c->cursec);
2191 /* Severely errored seconds, burst errored seconds */
2192 length += print_frac (s + length, 0, c->currnt.ses, c->cursec);
2193 length += print_frac (s + length, 1, c->currnt.bes, c->cursec);
2195 /* Out of frame seconds, controlled slip seconds */
2196 length += print_frac (s + length, 0, c->currnt.oofs, c->cursec);
2197 length += print_frac (s + length, 1, c->currnt.css, c->cursec);
2199 length += sprintf (s + length, " %s\n", format_e1_status (c->status));
2201 /* Print total statistics. */
2202 length += print_frac (s + length, 0, total.uas, totsec);
2203 length += print_frac (s + length, 1, 60 * total.dm, totsec);
2205 length += print_frac (s + length, 0, total.bpv, totsec);
2206 length += print_frac (s + length, 1, total.fse, totsec);
2208 length += print_frac (s + length, 0, total.crce, totsec);
2209 length += print_frac (s + length, 1, total.rcrce, totsec);
2211 length += print_frac (s + length, 0, total.es, totsec);
2212 length += print_frac (s + length, 1, total.les, totsec);
2214 length += print_frac (s + length, 0, total.ses, totsec);
2215 length += print_frac (s + length, 1, total.bes, totsec);
2217 length += print_frac (s + length, 0, total.oofs, totsec);
2218 length += print_frac (s + length, 1, total.css, totsec);
2220 length += sprintf (s + length, " -- Total\n");
2224 static int print_chan (char *s, ce_chan_t *c)
2229 length += sprintf (s + length, "ce%d", c->board->num * NCHAN + c->num);
2231 length += sprintf (s + length, " debug=%d", d->chan->debug);
2233 if (c->board->mux) {
2234 length += sprintf (s + length, " cfg=C");
2236 length += sprintf (s + length, " cfg=A");
2240 length += sprintf (s + length, " %ld", c->baud);
2242 length += sprintf (s + length, " extclock");
2244 if (c->type == T_E1)
2246 case GSYN_INT : length += sprintf (s + length, " syn=int"); break;
2247 case GSYN_RCV : length += sprintf (s + length, " syn=rcv"); break;
2248 case GSYN_RCV0 : length += sprintf (s + length, " syn=rcv0"); break;
2249 case GSYN_RCV1 : length += sprintf (s + length, " syn=rcv1"); break;
2251 if (c->type == T_E1)
2252 length += sprintf (s + length, " higain=%s", c->higain ? "on" : "off");
2254 length += sprintf (s + length, " loop=%s", c->lloop ? "on" : "off");
2256 if (c->type == T_E1)
2257 length += sprintf (s + length, " ts=%s", format_timeslots (c->ts));
2258 length += sprintf (s + length, "\n");
2262 #if __FreeBSD_version >= 500000
2263 static int ng_ce_rcvmsg (node_p node, item_p item, hook_p lasthook)
2265 drv_t *d = NG_NODE_PRIVATE (node);
2266 struct ng_mesg *msg;
2268 static int ng_ce_rcvmsg (node_p node, struct ng_mesg *msg,
2269 const char *retaddr, struct ng_mesg **rptr)
2271 drv_t *d = node->private;
2273 struct ng_mesg *resp = NULL;
2276 CE_DEBUG (d, ("Rcvmsg\n"));
2277 #if __FreeBSD_version >= 500000
2278 NGI_GET_MSG (item, msg);
2280 switch (msg->header.typecookie) {
2286 printf ("Not implemented yet\n");
2290 case NGM_GENERIC_COOKIE:
2291 switch (msg->header.cmd) {
2296 case NGM_TEXT_STATUS: {
2299 int dl = sizeof (struct ng_mesg) + 730;
2301 #if __FreeBSD_version >= 500000
2302 NG_MKRESPONSE (resp, msg, dl, M_NOWAIT);
2308 resp = malloc (M_NETGRAPH, M_NOWAIT);
2317 l += print_chan (s + l, d->chan);
2318 l += print_stats (s + l, d->chan, 1);
2319 l += print_modems (s + l, d->chan, 1);
2320 l += print_e1_stats (s + l, d->chan);
2322 l += sprintf (s + l, "Error: node not connect to channel");
2323 #if __FreeBSD_version < 500000
2324 (resp)->header.version = NG_VERSION;
2325 (resp)->header.arglen = strlen (s) + 1;
2326 (resp)->header.token = msg->header.token;
2327 (resp)->header.typecookie = NGM_CE_COOKIE;
2328 (resp)->header.cmd = msg->header.cmd;
2330 strncpy ((resp)->header.cmdstr, "status", NG_CMDSTRSIZ);
2336 #if __FreeBSD_version >= 500000
2337 NG_RESPOND_MSG (error, node, item, resp);
2341 free (msg, M_NETGRAPH);
2346 #if __FreeBSD_version >= 500000
2347 static int ng_ce_rcvdata (hook_p hook, item_p item)
2349 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE(hook));
2351 #if __FreeBSD_version < 502120
2354 struct ng_tag_prio *ptag;
2357 static int ng_ce_rcvdata (hook_p hook, struct mbuf *m, meta_p meta)
2359 drv_t *d = hook->node->private;
2361 bdrv_t *bd = d->board->sys;
2365 CE_DEBUG2 (d, ("Rcvdata\n"));
2366 #if __FreeBSD_version >= 500000
2367 NGI_GET_M (item, m);
2368 #if __FreeBSD_version < 502120
2369 NGI_GET_META (item, meta);
2371 NG_FREE_ITEM (item);
2372 if (! NG_HOOK_PRIVATE (hook) || ! d) {
2374 #if __FreeBSD_version < 502120
2375 NG_FREE_META (meta);
2378 if (! hook->private || ! d) {
2379 NG_FREE_DATA (m,meta);
2384 #if __FreeBSD_version >= 502120
2385 /* Check for high priority data */
2386 if ((ptag = (struct ng_tag_prio *)m_tag_locate(m, NGM_GENERIC_COOKIE,
2387 NG_TAG_PRIO, NULL)) != NULL && (ptag->priority > NG_PRIO_CUTOFF) )
2392 q = (meta && meta->priority > 0) ? &d->hi_queue : &d->queue;
2397 #if __FreeBSD_version >= 500000
2399 if (_IF_QFULL (q)) {
2404 #if __FreeBSD_version < 502120
2405 NG_FREE_META (meta);
2416 NG_FREE_DATA (m, meta);
2427 static int ng_ce_rmnode (node_p node)
2429 #if __FreeBSD_version >= 500000
2430 drv_t *d = NG_NODE_PRIVATE (node);
2432 CE_DEBUG (d, ("Rmnode\n"));
2433 if (d && d->running) {
2434 bdrv_t *bd = d->board->sys;
2442 #if __FreeBSD_version >= 502120
2443 if (node->nd_flags & NGF_REALLY_DIE) {
2445 if (node->nd_flags & NG_REALLY_DIE) {
2447 NG_NODE_SET_PRIVATE (node, NULL);
2448 NG_NODE_UNREF (node);
2450 #if __FreeBSD_version >= 502120
2451 NG_NODE_REVIVE(node); /* Persistent node */
2453 node->nd_flags &= ~NG_INVALID;
2456 #else /* __FreeBSD_version < 500000 */
2457 drv_t *d = node->private;
2459 if (d && d->running) {
2460 bdrv_t *bd = d->board->sys;
2468 node->flags |= NG_INVALID;
2478 static int ng_ce_connect (hook_p hook)
2480 #if __FreeBSD_version >= 500000
2481 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2483 drv_t *d = hook->node->private;
2487 CE_DEBUG (d, ("Connect\n"));
2488 callout_reset (&d->timeout_handle, hz, ce_watchdog_timer, d);
2494 static int ng_ce_disconnect (hook_p hook)
2496 #if __FreeBSD_version >= 500000
2497 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2499 drv_t *d = hook->node->private;
2503 CE_DEBUG (d, ("Disconnect\n"));
2504 #if __FreeBSD_version >= 500000
2505 if (NG_HOOK_PRIVATE (hook))
2510 bdrv_t *bd = d->board->sys;
2517 /* If we were wait it than it reasserted now, just stop it. */
2518 if (!callout_drain (&d->timeout_handle))
2519 callout_stop (&d->timeout_handle);
2525 static int ce_modevent (module_t mod, int type, void *unused)
2527 #if __FreeBSD_version < 500000
2529 struct cdevsw *cdsw;
2531 static int load_count = 0;
2533 #if __FreeBSD_version < 500000
2534 dev = makedev (CDEV_MAJOR, 0);
2539 #if __FreeBSD_version < 500000
2541 (cdsw = devsw (dev)) &&
2542 cdsw->d_maj == CDEV_MAJOR) {
2543 printf ("Tau32-PCI driver is already in system\n");
2547 #if __FreeBSD_version >= 500000 && defined NETGRAPH
2548 if (ng_newtype (&typestruct))
2549 printf ("Failed to register ng_ce\n");
2552 #if __FreeBSD_version <= 500000
2553 cdevsw_add (&ce_cdevsw);
2555 #if __FreeBSD_version >= 500000
2556 callout_init (&timeout_handle, 1);
2558 callout_init (&timeout_handle);
2560 callout_reset (&timeout_handle, hz*5, ce_timeout, 0);
2563 if (load_count == 1) {
2564 printf ("Removing device entry for Tau32-PCI\n");
2565 #if __FreeBSD_version <= 500000
2566 cdevsw_remove (&ce_cdevsw);
2568 #if __FreeBSD_version >= 500000 && defined NETGRAPH
2569 ng_rmtype (&typestruct);
2572 /* If we were wait it than it reasserted now, just stop it.
2573 * Actually we shouldn't get this condition. But code could be
2574 * changed in the future, so just be a litle paranoid.
2576 if (!callout_drain (&timeout_handle))
2577 callout_stop (&timeout_handle);
2587 #if __FreeBSD_version >= 502100
2588 static struct ng_type typestruct = {
2589 .version = NG_ABI_VERSION,
2590 .name = NG_CE_NODE_TYPE,
2591 .constructor = ng_ce_constructor,
2592 .rcvmsg = ng_ce_rcvmsg,
2593 .shutdown = ng_ce_rmnode,
2594 .newhook = ng_ce_newhook,
2595 .connect = ng_ce_connect,
2596 .rcvdata = ng_ce_rcvdata,
2597 .disconnect = ng_ce_disconnect,
2599 #else /* __FreeBSD_version < 502100 */
2600 static struct ng_type typestruct = {
2601 #if __FreeBSD_version >= 500000
2615 #if __FreeBSD_version < 500000
2621 #endif /* __FreeBSD_version < 502100 */
2625 #if __FreeBSD_version >= 500000
2627 MODULE_DEPEND (ng_ce, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION);
2629 MODULE_DEPEND (ce, sppp, 1, 1, 1);
2632 DRIVER_MODULE (cemod, pci, ce_driver, ce_devclass, ce_modevent, NULL);
2634 DRIVER_MODULE (ce, pci, ce_driver, ce_devclass, ce_modevent, NULL);
2636 #else /* if __FreeBSD_version < 500000*/
2638 DRIVER_MODULE (ce, pci, ce_driver, ce_devclass, ng_mod_event, &typestruct);
2640 DRIVER_MODULE (ce, pci, ce_driver, ce_devclass, ce_modevent, NULL);
2642 #endif /* __FreeBSD_version < 500000 */