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>
79 #include <machine/pmap.h>
81 /* If we don't have Cronyx's sppp version, we don't have fr support via sppp */
87 #define IFP2SP(ifp) ((struct sppp*)ifp)
90 #define SP2IFP(sp) ((struct ifnet*)sp)
94 #define PCIR_BAR(x) (PCIR_MAPS + (x) * 4)
97 /* define as our previous return value */
98 #ifndef BUS_PROBE_DEFAULT
99 #define BUS_PROBE_DEFAULT 0
102 #define CE_DEBUG(d,s) ({if (d->chan->debug) {\
103 printf ("%s: ", d->name); printf s;}})
104 #define CE_DEBUG2(d,s) ({if (d->chan->debug>1) {\
105 printf ("%s: ", d->name); printf s;}})
108 #define IF_DRAIN(ifq) do { \
111 IF_DEQUEUE(ifq, m); \
120 #define _IF_QLEN(ifq) ((ifq)->ifq_len)
123 #ifndef callout_drain
124 #define callout_drain callout_stop
127 #define CE_LOCK_NAME "ceX"
129 #define CE_LOCK(_bd) mtx_lock (&(_bd)->ce_mtx)
130 #define CE_UNLOCK(_bd) mtx_unlock (&(_bd)->ce_mtx)
131 #define CE_LOCK_ASSERT(_bd) mtx_assert (&(_bd)->ce_mtx, MA_OWNED)
133 #define CDEV_MAJOR 185
135 static int ce_probe __P((device_t));
136 static int ce_attach __P((device_t));
137 static int ce_detach __P((device_t));
139 static device_method_t ce_methods[] = {
140 /* Device interface */
141 DEVMETHOD(device_probe, ce_probe),
142 DEVMETHOD(device_attach, ce_attach),
143 DEVMETHOD(device_detach, ce_detach),
148 typedef struct _ce_dma_mem_t {
152 #if __FreeBSD_version >= 500000
158 typedef struct _drv_t {
163 struct ifqueue rqueue;
165 char nodename [NG_NODESIZE];
169 struct ifqueue queue;
170 struct ifqueue hi_queue;
175 struct callout timeout_handle;
176 #if __FreeBSD_version >= 500000
178 #else /* __FreeBSD_version < 500000 */
184 typedef struct _bdrv_t {
186 struct resource *ce_res;
187 struct resource *ce_irq;
190 drv_t channel [NCHAN];
191 #if __FreeBSD_version >= 504000
196 static driver_t ce_driver = {
202 static devclass_t ce_devclass;
204 static void ce_receive (ce_chan_t *c, unsigned char *data, int len);
205 static void ce_transmit (ce_chan_t *c, void *attachment, int len);
206 static void ce_error (ce_chan_t *c, int data);
207 static void ce_up (drv_t *d);
208 static void ce_start (drv_t *d);
209 static void ce_down (drv_t *d);
210 static void ce_watchdog (drv_t *d);
211 static void ce_watchdog_timer (void *arg);
213 extern struct ng_type typestruct;
215 static void ce_ifstart (struct ifnet *ifp);
216 static void ce_tlf (struct sppp *sp);
217 static void ce_tls (struct sppp *sp);
218 static int ce_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data);
219 static void ce_initialize (void *softc);
222 static ce_board_t *adapter [NBRD];
223 static drv_t *channel [NBRD*NCHAN];
224 static struct callout led_timo [NBRD];
225 static struct callout timeout_handle;
227 static int ce_destroy = 0;
229 #if __FreeBSD_version < 500000
230 static int ce_open (dev_t dev, int oflags, int devtype, struct proc *p);
231 static int ce_close (dev_t dev, int fflag, int devtype, struct proc *p);
232 static int ce_ioctl (dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p);
234 static int ce_open (struct cdev *dev, int oflags, int devtype, struct thread *td);
235 static int ce_close (struct cdev *dev, int fflag, int devtype, struct thread *td);
236 static int ce_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td);
238 #if __FreeBSD_version < 500000
239 static struct cdevsw ce_cdevsw = {
240 ce_open, ce_close, noread, nowrite,
241 ce_ioctl, nopoll, nommap, nostrategy,
242 "ce", CDEV_MAJOR, nodump, nopsize,
245 #elif __FreeBSD_version == 500000
246 static struct cdevsw ce_cdevsw = {
247 ce_open, ce_close, noread, nowrite,
248 ce_ioctl, nopoll, nommap, nostrategy,
249 "ce", CDEV_MAJOR, nodump, nopsize,
252 #elif __FreeBSD_version <= 501000
253 static struct cdevsw ce_cdevsw = {
261 .d_strategy = nostrategy,
267 #elif __FreeBSD_version < 502103
268 static struct cdevsw ce_cdevsw = {
276 #elif __FreeBSD_version < 600000
277 static struct cdevsw ce_cdevsw = {
278 .d_version = D_VERSION,
284 .d_flags = D_NEEDGIANT,
286 #else /* __FreeBSD_version >= 600000 */
287 static struct cdevsw ce_cdevsw = {
288 .d_version = D_VERSION,
297 * Make an mbuf from data.
299 static struct mbuf *makembuf (void *buf, unsigned len)
303 MGETHDR (m, M_NOWAIT, MT_DATA);
306 if (!(MCLGET(m, M_NOWAIT))) {
310 m->m_pkthdr.len = m->m_len = len;
311 bcopy (buf, mtod (m, caddr_t), len);
315 static int ce_probe (device_t dev)
317 if ((pci_get_vendor (dev) == TAU32_PCI_VENDOR_ID) &&
318 (pci_get_device (dev) == TAU32_PCI_DEVICE_ID)) {
319 device_set_desc (dev, "Cronyx-Tau32-PCI serial adapter");
320 return BUS_PROBE_DEFAULT;
325 static void ce_timeout (void *arg)
330 for (i = 0; i < NBRD; ++i) {
331 if (adapter[i] == NULL)
333 for (k = 0; k < NCHAN; ++k) {
339 d = channel[i * NCHAN + k];
344 CE_LOCK ((bdrv_t *)d->board->sys);
345 switch (d->chan->type) {
347 ce_e1_timer (d->chan);
352 CE_UNLOCK ((bdrv_t *)d->board->sys);
358 callout_reset (&timeout_handle, hz, ce_timeout, 0);
362 static void ce_led_off (void *arg)
365 bdrv_t *bd = (bdrv_t *) b->sys;
373 TAU32_LedSet (b->ddk.pControllerObject, 0);
378 static void ce_intr (void *arg)
381 ce_board_t *b = bd->board;
384 #if __FreeBSD_version >= 500000 && defined NETGRAPH
394 TAU32_LedSet (b->ddk.pControllerObject, 1);
396 TAU32_HandleInterrupt (b->ddk.pControllerObject);
398 /* Turn LED off 50 msec later. */
399 callout_reset (&led_timo[b->num], hz/20, ce_led_off, b);
403 /* Pass packets in a lock-free state */
404 for (i = 0; i < NCHAN && b->chan[i].type; i++) {
405 drv_t *d = b->chan[i].sys;
407 if (!d || !d->running)
409 while (_IF_QLEN(&d->rqueue)) {
410 IF_DEQUEUE (&d->rqueue,m);
415 #if __FreeBSD_version >= 500000
416 NG_SEND_DATA_ONLY (error, d->hook, m);
418 ng_queue_data (d->hook, m, 0);
421 IF_DRAIN (&d->rqueue);
424 sppp_input (d->ifp, m);
430 #if __FreeBSD_version >= 500000
432 ce_bus_dmamap_addr (void *arg, bus_dma_segment_t *segs, int nseg, int error)
439 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
441 *addr = segs->ds_addr;
445 #define BUS_DMA_ZERO 0
449 ce_bus_dma_mem_alloc (int bnum, int cnum, ce_dma_mem_t *dmem)
453 error = bus_dma_tag_create (NULL, 16, 0, BUS_SPACE_MAXADDR_32BIT,
454 BUS_SPACE_MAXADDR, NULL, NULL, dmem->size, 1,
456 #if __FreeBSD_version >= 502000
461 if (cnum >= 0) printf ("ce%d-%d: ", bnum, cnum);
462 else printf ("ce%d: ", bnum);
463 printf ("couldn't allocate tag for dma memory\n");
466 error = bus_dmamem_alloc (dmem->dmat, (void **)&dmem->virt,
467 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &dmem->mapp);
469 if (cnum >= 0) printf ("ce%d-%d: ", bnum, cnum);
470 else printf ("ce%d: ", bnum);
471 printf ("couldn't allocate mem for dma memory\n");
472 bus_dma_tag_destroy (dmem->dmat);
475 error = bus_dmamap_load (dmem->dmat, dmem->mapp, dmem->virt,
476 dmem->size, ce_bus_dmamap_addr, &dmem->phys, 0);
478 if (cnum >= 0) printf ("ce%d-%d: ", bnum, cnum);
479 else printf ("ce%d: ", bnum);
480 printf ("couldn't load mem map for dma memory\n");
481 bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
482 bus_dma_tag_destroy (dmem->dmat);
485 #if __FreeBSD_version >= 502000
486 bzero (dmem->virt, dmem->size);
492 ce_bus_dma_mem_free (ce_dma_mem_t *dmem)
494 bus_dmamap_unload (dmem->dmat, dmem->mapp);
495 bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
496 bus_dma_tag_destroy (dmem->dmat);
500 ce_bus_dma_mem_alloc (int bnum, int cnum, ce_dma_mem_t *dmem)
502 dmem->virt = contigmalloc (dmem->size, M_DEVBUF, M_WAITOK,
503 0x100000, 0xffffffff, 16, 0);
504 if (dmem->virt == NULL) {
505 if (cnum >= 0) printf ("ce%d-%d: ", bnum, cnum);
506 else printf ("ce%d: ", bnum);
507 printf ("couldn't allocate dma memory\n");
510 dmem->phys = vtophys (dmem->virt);
511 bzero (dmem->virt, dmem->size);
516 ce_bus_dma_mem_free (ce_dma_mem_t *dmem)
518 contigfree (dmem->virt, dmem->size, M_DEVBUF);
523 * Called if the probe succeeded.
525 static int ce_attach (device_t dev)
527 bdrv_t *bd = device_get_softc (dev);
528 int unit = device_get_unit (dev);
529 #if __FreeBSD_version >= 504000
530 char *ce_ln = CE_LOCK_NAME;
539 b = malloc (sizeof(ce_board_t), M_DEVBUF, M_WAITOK);
541 printf ("ce%d: couldn't allocate memory\n", unit);
544 bzero (b, sizeof(ce_board_t));
548 #if __FreeBSD_version >= 440000
549 pci_enable_busmaster (dev);
552 bd->dmamem.size = TAU32_ControllerObjectSize;
553 if (! ce_bus_dma_mem_alloc (unit, -1, &bd->dmamem)) {
557 b->ddk.pControllerObject = bd->dmamem.virt;
562 bd->ce_res = bus_alloc_resource (dev, SYS_RES_MEMORY, &rid,
563 0, ~0, 1, RF_ACTIVE);
565 printf ("ce%d: cannot map memory\n", unit);
566 ce_bus_dma_mem_free (&bd->dmamem);
570 vbase = (vm_offset_t) rman_get_virtual (bd->ce_res);
572 b->ddk.PciBar1VirtualAddress = (void *)vbase;
573 b->ddk.ControllerObjectPhysicalAddress = bd->dmamem.phys;
574 b->ddk.pErrorNotifyCallback = ce_error_callback;
575 b->ddk.pStatusNotifyCallback = ce_status_callback;
578 TAU32_BeforeReset(&b->ddk);
579 pci_write_config (dev, TAU32_PCI_RESET_ADDRESS, TAU32_PCI_RESET_ON, 4);
580 pci_write_config (dev, TAU32_PCI_RESET_ADDRESS, TAU32_PCI_RESET_OFF, 4);
582 if(!TAU32_Initialize(&b->ddk, 0))
584 printf ("ce%d: init adapter error 0x%08x, bus dead bits 0x%08lx\n",
585 unit, b->ddk.InitErrors, b->ddk.DeadBits);
586 bus_release_resource (dev, SYS_RES_MEMORY, PCIR_BAR(0), bd->ce_res);
587 ce_bus_dma_mem_free (&bd->dmamem);
597 bd->ce_irq = bus_alloc_resource (dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
598 RF_SHAREABLE | RF_ACTIVE);
600 printf ("ce%d: cannot map interrupt\n", unit);
601 bus_release_resource (dev, SYS_RES_MEMORY, PCIR_BAR(0), bd->ce_res);
602 ce_bus_dma_mem_free (&bd->dmamem);
607 #if __FreeBSD_version >= 500000
608 callout_init (&led_timo[unit], 1);
610 callout_init (&led_timo[unit]);
612 error = bus_setup_intr (dev, bd->ce_irq,
613 #if __FreeBSD_version >= 500013
614 INTR_TYPE_NET|INTR_MPSAFE,
618 NULL, ce_intr, bd, &bd->ce_intrhand);
620 printf ("ce%d: cannot set up irq\n", unit);
621 bus_release_resource (dev, SYS_RES_IRQ, 0, bd->ce_irq);
622 bus_release_resource (dev, SYS_RES_MEMORY,
623 PCIR_BAR(0), bd->ce_res);
624 ce_bus_dma_mem_free (&bd->dmamem);
630 switch (b->ddk.Model) {
631 case 1: strcpy (b->name, TAU32_BASE_NAME); break;
632 case 2: strcpy (b->name, TAU32_LITE_NAME); break;
633 case 3: strcpy (b->name, TAU32_ADPCM_NAME); break;
634 default: strcpy (b->name, TAU32_UNKNOWN_NAME); break;
637 printf ("ce%d: %s\n", unit, b->name);
639 for (c = b->chan; c < b->chan + NCHAN; ++c) {
640 c->num = (c - b->chan);
643 d = &bd->channel[c->num];
644 d->dmamem.size = sizeof(ce_buf_t);
645 if (! ce_bus_dma_mem_alloc (unit, c->num, &d->dmamem))
648 channel [b->num * NCHAN + c->num] = d;
649 sprintf (d->name, "ce%d.%d", b->num, c->num);
655 for (c = b->chan; c < b->chan + NCHAN; ++c) {
660 callout_init (&d->timeout_handle, 1);
662 if (ng_make_node_common (&typestruct, &d->node) != 0) {
663 printf ("%s: cannot make common node\n", d->name);
667 #if __FreeBSD_version >= 500000
668 NG_NODE_SET_PRIVATE (d->node, d);
670 d->node->private = d;
672 sprintf (d->nodename, "%s%d", NG_CE_NODE_TYPE,
673 c->board->num * NCHAN + c->num);
674 if (ng_name_node (d->node, d->nodename)) {
675 printf ("%s: cannot name node\n", d->nodename);
676 #if __FreeBSD_version >= 500000
677 NG_NODE_UNREF (d->node);
684 d->queue.ifq_maxlen = ifqmaxlen;
685 d->hi_queue.ifq_maxlen = ifqmaxlen;
686 d->rqueue.ifq_maxlen = ifqmaxlen;
687 #if __FreeBSD_version >= 500000
688 mtx_init (&d->queue.ifq_mtx, "ce_queue", NULL, MTX_DEF);
689 mtx_init (&d->hi_queue.ifq_mtx, "ce_queue_hi", NULL, MTX_DEF);
690 mtx_init (&d->rqueue.ifq_mtx, "ce_rqueue", NULL, MTX_DEF);
693 #if __FreeBSD_version >= 600031
694 d->ifp = if_alloc(IFT_PPP);
696 d->ifp = malloc (sizeof(struct sppp), M_DEVBUF, M_WAITOK);
697 bzero (d->ifp, sizeof(struct sppp));
700 printf ("%s: cannot if_alloc() interface\n", d->name);
703 d->ifp->if_softc = d;
704 #if __FreeBSD_version > 501000
705 if_initname (d->ifp, "ce", b->num * NCHAN + c->num);
707 d->ifp->if_unit = b->num * NCHAN + c->num;
708 d->ifp->if_name = "ce";
710 d->ifp->if_mtu = PP_MTU;
711 d->ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
712 d->ifp->if_ioctl = ce_sioctl;
713 d->ifp->if_start = ce_ifstart;
714 d->ifp->if_init = ce_initialize;
715 d->rqueue.ifq_maxlen = ifqmaxlen;
716 #if __FreeBSD_version >= 500000
717 mtx_init (&d->rqueue.ifq_mtx, "ce_rqueue", NULL, MTX_DEF);
719 sppp_attach (d->ifp);
721 IFP2SP(d->ifp)->pp_tlf = ce_tlf;
722 IFP2SP(d->ifp)->pp_tls = ce_tls;
723 /* If BPF is in the kernel, call the attach for it.
724 * The header size of PPP or Cisco/HDLC is 4 bytes. */
725 bpfattach (d->ifp, DLT_PPP, 4);
727 ce_start_chan (c, 1, 1, d->dmamem.virt, d->dmamem.phys);
729 /* Register callback functions. */
730 ce_register_transmit (c, &ce_transmit);
731 ce_register_receive (c, &ce_receive);
732 ce_register_error (c, &ce_error);
733 d->devt = make_dev (&ce_cdevsw, b->num*NCHAN+c->num, UID_ROOT,
734 GID_WHEEL, 0600, "ce%d", b->num*NCHAN+c->num);
737 #if __FreeBSD_version >= 504000
738 ce_ln[2] = '0' + unit;
739 mtx_init (&bd->ce_mtx, ce_ln, MTX_NETWORK_LOCK, MTX_DEF|MTX_RECURSE);
742 TAU32_EnableInterrupts(b->ddk.pControllerObject);
750 static int ce_detach (device_t dev)
752 bdrv_t *bd = device_get_softc (dev);
753 ce_board_t *b = bd->board;
757 #if __FreeBSD_version >= 504000
758 KASSERT (mtx_initialized (&bd->ce_mtx), ("ce mutex not initialized"));
762 /* Check if the device is busy (open). */
763 for (c = b->chan; c < b->chan + NCHAN; ++c) {
764 drv_t *d = (drv_t*) c->sys;
766 /* XXX Non existen chan! */
767 if (! d || ! d->chan)
776 /* Ok, we can unload driver */
777 /* At first we should disable interrupts */
779 TAU32_DisableInterrupts(b->ddk.pControllerObject);
781 callout_stop (&led_timo[b->num]);
783 for (c = b->chan; c < b->chan + NCHAN; ++c) {
784 drv_t *d = (drv_t*) c->sys;
786 if (! d || ! d->chan)
788 callout_stop (&d->timeout_handle);
790 /* Detach from the packet filter list of interfaces. */
793 /* Detach from the sync PPP list. */
794 sppp_detach (d->ifp);
796 /* Detach from the system list of interfaces. */
798 #if __FreeBSD_version > 600031
801 free (d->ifp, M_DEVBUF);
804 IF_DRAIN (&d->rqueue);
805 #if __FreeBSD_version >= 500000
806 mtx_destroy (&d->rqueue.ifq_mtx);
809 #if __FreeBSD_version >= 500000
811 ng_rmnode_self (d->node);
812 NG_NODE_UNREF (d->node);
815 IF_DRAIN (&d->rqueue);
816 mtx_destroy (&d->queue.ifq_mtx);
817 mtx_destroy (&d->hi_queue.ifq_mtx);
818 mtx_destroy (&d->rqueue.ifq_mtx);
824 destroy_dev (d->devt);
830 callout_drain (&led_timo[b->num]);
832 /* Disable the interrupt request. */
833 bus_teardown_intr (dev, bd->ce_irq, bd->ce_intrhand);
834 bus_release_resource (dev, SYS_RES_IRQ, 0, bd->ce_irq);
835 TAU32_DestructiveHalt (b->ddk.pControllerObject, 0);
836 bus_release_resource (dev, SYS_RES_MEMORY, PCIR_BAR(0), bd->ce_res);
838 for (c = b->chan; c < b->chan + NCHAN; ++c) {
839 drv_t *d = (drv_t*) c->sys;
841 if (! d || ! d->chan)
843 callout_drain (&d->timeout_handle);
844 channel [b->num * NCHAN + c->num] = 0;
845 /* Deallocate buffers. */
846 ce_bus_dma_mem_free (&d->dmamem);
848 adapter [b->num] = 0;
849 ce_bus_dma_mem_free (&bd->dmamem);
851 #if __FreeBSD_version >= 504000
852 mtx_destroy (&bd->ce_mtx);
858 static void ce_ifstart (struct ifnet *ifp)
860 drv_t *d = ifp->if_softc;
861 bdrv_t *bd = d->board->sys;
868 static void ce_tlf (struct sppp *sp)
870 drv_t *d = SP2IFP(sp)->if_softc;
872 CE_DEBUG2 (d, ("ce_tlf\n"));
876 static void ce_tls (struct sppp *sp)
878 drv_t *d = SP2IFP(sp)->if_softc;
880 CE_DEBUG2 (d, ("ce_tls\n"));
885 * Process an ioctl request.
887 static int ce_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data)
889 drv_t *d = ifp->if_softc;
890 bdrv_t *bd = d->board->sys;
891 int error, s, was_up, should_be_up;
893 #if __FreeBSD_version >= 600034
894 was_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
896 was_up = (ifp->if_flags & IFF_RUNNING) != 0;
898 error = sppp_ioctl (ifp, cmd, data);
903 if (! (ifp->if_flags & IFF_DEBUG))
906 d->chan->debug = d->chan->debug_shadow;
909 default: CE_DEBUG2 (d, ("ioctl 0x%lx\n", cmd)); return 0;
910 case SIOCADDMULTI: CE_DEBUG2 (d, ("ioctl SIOCADDMULTI\n")); return 0;
911 case SIOCDELMULTI: CE_DEBUG2 (d, ("ioctl SIOCDELMULTI\n")); return 0;
912 case SIOCSIFFLAGS: CE_DEBUG2 (d, ("ioctl SIOCSIFFLAGS\n")); break;
913 case SIOCSIFADDR: CE_DEBUG2 (d, ("ioctl SIOCSIFADDR\n")); break;
916 /* We get here only in case of SIFFLAGS or SIFADDR. */
919 #if __FreeBSD_version >= 600034
920 should_be_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
922 should_be_up = (ifp->if_flags & IFF_RUNNING) != 0;
924 if (! was_up && should_be_up) {
925 /* Interface goes up -- start it. */
928 } else if (was_up && ! should_be_up) {
929 /* Interface is going down -- stop it. */
930 /* if ((IFP2SP(ifp)->pp_flags & PP_FR) || (ifp->if_flags & PP_CISCO))*/
933 CE_DEBUG (d, ("ioctl 0x%lx p4\n", cmd));
940 * Initialization of interface.
941 * It seems to be never called by upper level?
943 static void ce_initialize (void *softc)
947 CE_DEBUG (d, ("ce_initialize\n"));
952 * Stop the interface. Called on splimp().
954 static void ce_down (drv_t *d)
956 CE_DEBUG (d, ("ce_down\n"));
957 /* Interface is going down -- stop it. */
958 ce_set_dtr (d->chan, 0);
959 ce_set_rts (d->chan, 0);
962 callout_stop (&d->timeout_handle);
966 * Start the interface. Called on splimp().
968 static void ce_up (drv_t *d)
970 CE_DEBUG (d, ("ce_up\n"));
971 ce_set_dtr (d->chan, 1);
972 ce_set_rts (d->chan, 1);
978 * Start output on the interface. Get another datagram to send
979 * off of the interface queue, and copy it to the interface
980 * before starting the output.
982 static void ce_send (drv_t *d)
987 CE_DEBUG2 (d, ("ce_send\n"));
989 /* No output if the interface is down. */
993 while (ce_transmit_space (d->chan)) {
994 /* Get the packet to send. */
996 IF_DEQUEUE (&d->hi_queue, m);
998 IF_DEQUEUE (&d->queue, m);
1000 m = sppp_dequeue (d->ifp);
1005 #if __FreeBSD_version >= 500000
1006 BPF_MTAP (d->ifp, m);
1009 bpf_mtap (d->ifp, m);
1012 #if __FreeBSD_version >= 490000
1013 len = m_length (m, NULL);
1015 len = m->m_pkthdr.len;
1018 printf ("%s: too long packet: %d bytes: ",
1020 else if (! m->m_next)
1021 ce_send_packet (d->chan, (u_char*) mtod (m, caddr_t), len, 0);
1023 ce_buf_item_t *item = (ce_buf_item_t*)d->chan->tx_queue;
1024 m_copydata (m, 0, len, item->buf);
1025 ce_send_packet (d->chan, item->buf, len, 0);
1028 /* Set up transmit timeout, if the transmit ring is not empty.*/
1032 #if __FreeBSD_version >= 600034
1033 d->ifp->if_flags |= IFF_DRV_OACTIVE;
1035 d->ifp->if_flags |= IFF_OACTIVE;
1041 * Start output on the interface.
1042 * Always called on splimp().
1044 static void ce_start (drv_t *d)
1048 ce_set_dtr (d->chan, 1);
1050 ce_set_rts (d->chan, 1);
1052 callout_reset (&d->timeout_handle, hz, ce_watchdog_timer, d);
1057 * Handle transmit timeouts.
1058 * Recover after lost transmit interrupts.
1059 * Always called on splimp().
1061 static void ce_watchdog (drv_t *d)
1063 CE_DEBUG (d, ("device timeout\n"));
1065 ce_set_dtr (d->chan, 0);
1066 ce_set_rts (d->chan, 0);
1067 /* ce_stop_chan (d->chan);*/
1068 /* ce_start_chan (d->chan, 1, 1, 0, 0);*/
1069 ce_set_dtr (d->chan, 1);
1070 ce_set_rts (d->chan, 1);
1075 static void ce_watchdog_timer (void *arg)
1078 bdrv_t *bd = d->board->sys;
1081 if (d->timeout == 1)
1085 callout_reset (&d->timeout_handle, hz, ce_watchdog_timer, d);
1089 static void ce_transmit (ce_chan_t *c, void *attachment, int len)
1095 if_inc_counter(d->ifp, IFCOUNTER_OPACKETS, 1);
1096 #if __FreeBSD_version >= 600034
1097 d->ifp->if_flags &= ~IFF_DRV_OACTIVE;
1099 d->ifp->if_flags &= ~IFF_OACTIVE;
1105 static void ce_receive (ce_chan_t *c, unsigned char *data, int len)
1113 m = makembuf (data, len);
1115 CE_DEBUG (d, ("no memory for packet\n"));
1117 if_inc_counter(d->ifp, IFCOUNTER_IQDROPS, 1);
1124 m->m_pkthdr.rcvif = 0;
1125 IF_ENQUEUE(&d->rqueue, m);
1127 if_inc_counter(d->ifp, IFCOUNTER_IPACKETS, 1);
1128 m->m_pkthdr.rcvif = d->ifp;
1129 /* Check if there's a BPF listener on this interface.
1130 * If so, hand off the raw packet to bpf. */
1131 BPF_MTAP(d->ifp, m);
1132 IF_ENQUEUE(&d->rqueue, m);
1136 static void ce_error (ce_chan_t *c, int data)
1142 CE_DEBUG (d, ("frame error\n"));
1144 if_inc_counter(d->ifp, IFCOUNTER_IERRORS, 1);
1148 CE_DEBUG (d, ("crc error\n"));
1150 if_inc_counter(d->ifp, IFCOUNTER_IERRORS, 1);
1154 CE_DEBUG (d, ("overrun error\n"));
1156 if_inc_counter(d->ifp, IFCOUNTER_COLLISIONS, 1);
1157 if_inc_counter(d->ifp, IFCOUNTER_IERRORS, 1);
1161 CE_DEBUG (d, ("overflow error\n"));
1163 if_inc_counter(d->ifp, IFCOUNTER_IERRORS, 1);
1167 CE_DEBUG (d, ("underrun error\n"));
1170 if_inc_counter(d->ifp, IFCOUNTER_OERRORS, 1);
1171 #if __FreeBSD_version >= 600034
1172 d->ifp->if_flags &= ~IFF_DRV_OACTIVE;
1174 d->ifp->if_flags &= ~IFF_OACTIVE;
1180 CE_DEBUG (d, ("error #%d\n", data));
1186 * You also need read, write, open, close routines.
1187 * This should get you started
1189 #if __FreeBSD_version < 500000
1190 static int ce_open (dev_t dev, int oflags, int devtype, struct proc *p)
1192 static int ce_open (struct cdev *dev, int oflags, int devtype, struct thread *td)
1195 int unit = dev2unit (dev);
1198 if (unit >= NBRD*NCHAN || ! (d = channel[unit]))
1200 CE_DEBUG2 (d, ("ce_open\n"));
1205 * Only called on the LAST close.
1207 #if __FreeBSD_version < 500000
1208 static int ce_close (dev_t dev, int fflag, int devtype, struct proc *p)
1210 static int ce_close (struct cdev *dev, int fflag, int devtype, struct thread *td)
1213 drv_t *d = channel [dev2unit (dev)];
1215 CE_DEBUG2 (d, ("ce_close\n"));
1219 static int ce_modem_status (ce_chan_t *c)
1222 bdrv_t *bd = d->board->sys;
1225 status = d->running ? TIOCM_LE : 0;
1228 if (ce_get_cd (c)) status |= TIOCM_CD;
1229 if (ce_get_cts (c)) status |= TIOCM_CTS;
1230 if (ce_get_dsr (c)) status |= TIOCM_DSR;
1231 if (c->dtr) status |= TIOCM_DTR;
1232 if (c->rts) status |= TIOCM_RTS;
1238 #if __FreeBSD_version < 500000
1239 static int ce_ioctl (dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
1241 static int ce_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
1244 drv_t *d = channel [dev2unit (dev)];
1245 bdrv_t *bd = d->board->sys;
1246 ce_chan_t *c = d->chan;
1247 struct serial_statistics *st;
1248 struct e1_statistics *opte1;
1253 case SERIAL_GETREGISTERED:
1254 CE_DEBUG2 (d, ("ioctl: getregistered\n"));
1255 bzero (mask, sizeof(mask));
1256 for (s=0; s<NBRD*NCHAN; ++s)
1258 mask [s/8] |= 1 << (s & 7);
1259 bcopy (mask, data, sizeof (mask));
1263 case SERIAL_GETPROTO:
1264 CE_DEBUG2 (d, ("ioctl: getproto\n"));
1265 strcpy ((char*)data, (IFP2SP(d->ifp)->pp_flags & PP_FR) ? "fr" :
1266 (d->ifp->if_flags & PP_CISCO) ? "cisco" : "ppp");
1269 case SERIAL_SETPROTO:
1270 CE_DEBUG2 (d, ("ioctl: setproto\n"));
1271 /* Only for superuser! */
1272 #if __FreeBSD_version < 500000
1274 #elif __FreeBSD_version < 700000
1277 error = priv_check (td, PRIV_DRIVER);
1281 #if __FreeBSD_version >= 600034
1282 if (d->ifp->if_flags & IFF_DRV_RUNNING)
1284 if (d->ifp->if_flags & IFF_RUNNING)
1287 if (! strcmp ("cisco", (char*)data)) {
1288 IFP2SP(d->ifp)->pp_flags &= ~(PP_FR);
1289 IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE;
1290 d->ifp->if_flags |= PP_CISCO;
1292 } else if (! strcmp ("fr", (char*)data)) {
1293 d->ifp->if_flags &= ~(PP_CISCO);
1294 IFP2SP(d->ifp)->pp_flags |= PP_FR | PP_KEEPALIVE;
1296 } else if (! strcmp ("ppp", (char*)data)) {
1297 IFP2SP(d->ifp)->pp_flags &= ~PP_FR;
1298 IFP2SP(d->ifp)->pp_flags &= ~PP_KEEPALIVE;
1299 d->ifp->if_flags &= ~(PP_CISCO);
1304 case SERIAL_GETKEEPALIVE:
1305 CE_DEBUG2 (d, ("ioctl: getkeepalive\n"));
1306 if ((IFP2SP(d->ifp)->pp_flags & PP_FR) ||
1307 (d->ifp->if_flags & PP_CISCO))
1309 *(int*)data = (IFP2SP(d->ifp)->pp_flags & PP_KEEPALIVE) ? 1 : 0;
1312 case SERIAL_SETKEEPALIVE:
1313 CE_DEBUG2 (d, ("ioctl: setkeepalive\n"));
1314 /* Only for superuser! */
1315 #if __FreeBSD_version < 500000
1317 #elif __FreeBSD_version < 700000
1320 error = priv_check (td, PRIV_DRIVER);
1324 if ((IFP2SP(d->ifp)->pp_flags & PP_FR) ||
1325 (d->ifp->if_flags & PP_CISCO))
1330 IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE;
1332 IFP2SP(d->ifp)->pp_flags &= ~PP_KEEPALIVE;
1338 case SERIAL_GETMODE:
1339 CE_DEBUG2 (d, ("ioctl: getmode\n"));
1340 *(int*)data = SERIAL_HDLC;
1343 case SERIAL_SETMODE:
1344 /* Only for superuser! */
1345 #if __FreeBSD_version < 500000
1347 #elif __FreeBSD_version < 700000
1350 error = priv_check (td, PRIV_DRIVER);
1354 if (*(int*)data != SERIAL_HDLC)
1359 CE_DEBUG2 (d, ("ioctl: getcfg\n"));
1364 CE_DEBUG2 (d, ("ioctl: setcfg\n"));
1365 #if __FreeBSD_version < 500000
1367 #elif __FreeBSD_version < 700000
1370 error = priv_check (td, PRIV_DRIVER);
1374 if (*((char*)data) != 'c')
1378 case SERIAL_GETSTAT:
1379 CE_DEBUG2 (d, ("ioctl: getstat\n"));
1380 st = (struct serial_statistics*) data;
1381 st->rintr = c->rintr;
1382 st->tintr = c->tintr;
1384 st->ibytes = c->ibytes;
1385 st->ipkts = c->ipkts;
1386 st->obytes = c->obytes;
1387 st->opkts = c->opkts;
1388 st->ierrs = c->overrun + c->frame + c->crc;
1389 st->oerrs = c->underrun;
1392 case SERIAL_GETESTAT:
1393 CE_DEBUG2 (d, ("ioctl: getestat\n"));
1394 if (c->type != T_E1)
1396 opte1 = (struct e1_statistics*) data;
1399 if (c->status & ESTS_NOALARM)
1400 opte1->status |= E1_NOALARM;
1401 if (c->status & ESTS_LOS)
1402 opte1->status |= E1_LOS;
1403 if (c->status & ESTS_LOF)
1404 opte1->status |= E1_LOF;
1405 if (c->status & ESTS_AIS)
1406 opte1->status |= E1_AIS;
1407 if (c->status & ESTS_LOMF)
1408 opte1->status |= E1_LOMF;
1409 if (c->status & ESTS_AIS16)
1410 opte1->status |= E1_AIS16;
1411 if (c->status & ESTS_FARLOF)
1412 opte1->status |= E1_FARLOF;
1413 if (c->status & ESTS_FARLOMF)
1414 opte1->status |= E1_FARLOMF;
1415 if (c->status & ESTS_TSTREQ)
1416 opte1->status |= E1_TSTREQ;
1417 if (c->status & ESTS_TSTERR)
1418 opte1->status |= E1_TSTERR;
1420 opte1->cursec = c->cursec;
1421 opte1->totsec = c->totsec + c->cursec;
1423 opte1->currnt.bpv = c->currnt.bpv;
1424 opte1->currnt.fse = c->currnt.fse;
1425 opte1->currnt.crce = c->currnt.crce;
1426 opte1->currnt.rcrce = c->currnt.rcrce;
1427 opte1->currnt.uas = c->currnt.uas;
1428 opte1->currnt.les = c->currnt.les;
1429 opte1->currnt.es = c->currnt.es;
1430 opte1->currnt.bes = c->currnt.bes;
1431 opte1->currnt.ses = c->currnt.ses;
1432 opte1->currnt.oofs = c->currnt.oofs;
1433 opte1->currnt.css = c->currnt.css;
1434 opte1->currnt.dm = c->currnt.dm;
1436 opte1->total.bpv = c->total.bpv + c->currnt.bpv;
1437 opte1->total.fse = c->total.fse + c->currnt.fse;
1438 opte1->total.crce = c->total.crce + c->currnt.crce;
1439 opte1->total.rcrce = c->total.rcrce + c->currnt.rcrce;
1440 opte1->total.uas = c->total.uas + c->currnt.uas;
1441 opte1->total.les = c->total.les + c->currnt.les;
1442 opte1->total.es = c->total.es + c->currnt.es;
1443 opte1->total.bes = c->total.bes + c->currnt.bes;
1444 opte1->total.ses = c->total.ses + c->currnt.ses;
1445 opte1->total.oofs = c->total.oofs + c->currnt.oofs;
1446 opte1->total.css = c->total.css + c->currnt.css;
1447 opte1->total.dm = c->total.dm + c->currnt.dm;
1448 for (s=0; s<48; ++s) {
1449 opte1->interval[s].bpv = c->interval[s].bpv;
1450 opte1->interval[s].fse = c->interval[s].fse;
1451 opte1->interval[s].crce = c->interval[s].crce;
1452 opte1->interval[s].rcrce = c->interval[s].rcrce;
1453 opte1->interval[s].uas = c->interval[s].uas;
1454 opte1->interval[s].les = c->interval[s].les;
1455 opte1->interval[s].es = c->interval[s].es;
1456 opte1->interval[s].bes = c->interval[s].bes;
1457 opte1->interval[s].ses = c->interval[s].ses;
1458 opte1->interval[s].oofs = c->interval[s].oofs;
1459 opte1->interval[s].css = c->interval[s].css;
1460 opte1->interval[s].dm = c->interval[s].dm;
1464 case SERIAL_CLRSTAT:
1465 CE_DEBUG2 (d, ("ioctl: clrstat\n"));
1466 /* Only for superuser! */
1467 #if __FreeBSD_version < 500000
1469 #elif __FreeBSD_version < 700000
1472 error = priv_check (td, PRIV_DRIVER);
1486 bzero (&c->currnt, sizeof (c->currnt));
1487 bzero (&c->total, sizeof (c->total));
1488 bzero (c->interval, sizeof (c->interval));
1491 case SERIAL_GETLOOP:
1492 CE_DEBUG2 (d, ("ioctl: getloop\n"));
1493 if (c->type != T_E1)
1495 *(int*)data = c->lloop;
1498 case SERIAL_SETLOOP:
1499 CE_DEBUG2 (d, ("ioctl: setloop\n"));
1500 if (c->type != T_E1)
1502 /* Only for superuser! */
1503 #if __FreeBSD_version < 500000
1505 #elif __FreeBSD_version < 700000
1508 error = priv_check (td, PRIV_DRIVER);
1514 ce_set_lloop (c, *(int*)data);
1519 case SERIAL_GETRLOOP:
1520 CE_DEBUG2 (d, ("ioctl: getrloop\n"));
1521 if (c->type != T_E1)
1523 *(int*)data = c->rloop;
1526 case SERIAL_SETRLOOP:
1527 CE_DEBUG2 (d, ("ioctl: setloop\n"));
1528 if (c->type != T_E1)
1530 /* Only for superuser! */
1531 #if __FreeBSD_version < 500000
1533 #elif __FreeBSD_version < 700000
1536 error = priv_check (td, PRIV_DRIVER);
1542 ce_set_rloop (c, *(int*)data);
1547 case SERIAL_GETDEBUG:
1548 CE_DEBUG2 (d, ("ioctl: getdebug\n"));
1549 *(int*)data = d->chan->debug;
1552 case SERIAL_SETDEBUG:
1553 CE_DEBUG2 (d, ("ioctl: setdebug\n"));
1554 /* Only for superuser! */
1555 #if __FreeBSD_version < 500000
1557 #elif __FreeBSD_version < 700000
1560 error = priv_check (td, PRIV_DRIVER);
1566 * The debug_shadow is always greater than zero for logic
1567 * simplicity. For switching debug off the IFF_DEBUG is
1570 d->chan->debug_shadow = (*(int*)data) ? (*(int*)data) : 1;
1571 if (d->ifp->if_flags & IFF_DEBUG)
1572 d->chan->debug = d->chan->debug_shadow;
1574 d->chan->debug = *(int*)data;
1578 case SERIAL_GETBAUD:
1579 CE_DEBUG2 (d, ("ioctl: getbaud\n"));
1580 *(long*)data = c->baud;
1583 case SERIAL_SETBAUD:
1584 CE_DEBUG2 (d, ("ioctl: setbaud\n"));
1585 if (c->type != T_E1 || !c->unfram)
1587 /* Only for superuser! */
1588 #if __FreeBSD_version < 500000
1590 #elif __FreeBSD_version < 700000
1593 error = priv_check (td, PRIV_DRIVER);
1599 ce_set_baud (c, *(long*)data);
1604 case SERIAL_GETTIMESLOTS:
1605 CE_DEBUG2 (d, ("ioctl: gettimeslots\n"));
1606 if ((c->type != T_E1 || c->unfram) && c->type != T_DATA)
1608 *(u_long*)data = c->ts;
1611 case SERIAL_SETTIMESLOTS:
1612 CE_DEBUG2 (d, ("ioctl: settimeslots\n"));
1613 /* Only for superuser! */
1614 #if __FreeBSD_version < 500000
1616 #elif __FreeBSD_version < 700000
1619 error = priv_check (td, PRIV_DRIVER);
1623 if ((c->type != T_E1 || c->unfram) && c->type != T_DATA)
1627 ce_set_ts (c, *(u_long*)data);
1632 case SERIAL_GETHIGAIN:
1633 CE_DEBUG2 (d, ("ioctl: gethigain\n"));
1634 if (c->type != T_E1)
1636 *(int*)data = c->higain;
1639 case SERIAL_SETHIGAIN:
1640 CE_DEBUG2 (d, ("ioctl: sethigain\n"));
1641 if (c->type != T_E1)
1643 /* Only for superuser! */
1644 #if __FreeBSD_version < 500000
1646 #elif __FreeBSD_version < 700000
1649 error = priv_check (td, PRIV_DRIVER);
1655 ce_set_higain (c, *(int*)data);
1660 case SERIAL_GETPHONY:
1661 CE_DEBUG2 (d, ("ioctl: getphony\n"));
1662 *(int*)data = c->phony;
1665 case SERIAL_SETPHONY:
1666 CE_DEBUG2 (d, ("ioctl: setphony\n"));
1667 /* Only for superuser! */
1668 #if __FreeBSD_version < 500000
1670 #elif __FreeBSD_version < 700000
1673 error = priv_check (td, PRIV_DRIVER);
1679 ce_set_phony (c, *(int*)data);
1684 case SERIAL_GETUNFRAM:
1685 CE_DEBUG2 (d, ("ioctl: getunfram\n"));
1686 if (c->type != T_E1 || c->num != 0)
1688 *(int*)data = c->unfram;
1691 case SERIAL_SETUNFRAM:
1692 CE_DEBUG2 (d, ("ioctl: setunfram\n"));
1693 if (c->type != T_E1 || c->num != 0)
1695 /* Only for superuser! */
1696 #if __FreeBSD_version < 500000
1698 #elif __FreeBSD_version < 700000
1701 error = priv_check (td, PRIV_DRIVER);
1707 ce_set_unfram (c, *(int*)data);
1712 case SERIAL_GETSCRAMBLER:
1713 CE_DEBUG2 (d, ("ioctl: getscrambler\n"));
1716 *(int*)data = c->scrambler;
1719 case SERIAL_SETSCRAMBLER:
1720 CE_DEBUG2 (d, ("ioctl: setscrambler\n"));
1721 /* Only for superuser! */
1722 #if __FreeBSD_version < 500000
1724 #elif __FreeBSD_version < 700000
1727 error = priv_check (td, PRIV_DRIVER);
1735 ce_set_scrambler (c, *(int*)data);
1740 case SERIAL_GETMONITOR:
1741 CE_DEBUG2 (d, ("ioctl: getmonitor\n"));
1742 if (c->type != T_E1)
1744 *(int*)data = c->monitor;
1747 case SERIAL_SETMONITOR:
1748 CE_DEBUG2 (d, ("ioctl: setmonitor\n"));
1749 /* Only for superuser! */
1750 #if __FreeBSD_version < 500000
1752 #elif __FreeBSD_version < 700000
1755 error = priv_check (td, PRIV_DRIVER);
1759 if (c->type != T_E1)
1763 ce_set_monitor (c, *(int*)data);
1768 case SERIAL_GETUSE16:
1769 CE_DEBUG2 (d, ("ioctl: getuse16\n"));
1770 if (c->type != T_E1 || c->unfram)
1772 *(int*)data = c->use16;
1775 case SERIAL_SETUSE16:
1776 CE_DEBUG2 (d, ("ioctl: setuse16\n"));
1777 /* Only for superuser! */
1778 #if __FreeBSD_version < 500000
1780 #elif __FreeBSD_version < 700000
1783 error = priv_check (td, PRIV_DRIVER);
1787 if (c->type != T_E1)
1791 ce_set_use16 (c, *(int*)data);
1796 case SERIAL_GETCRC4:
1797 CE_DEBUG2 (d, ("ioctl: getcrc4\n"));
1798 if (c->type != T_E1 || c->unfram)
1800 *(int*)data = c->crc4;
1803 case SERIAL_SETCRC4:
1804 CE_DEBUG2 (d, ("ioctl: setcrc4\n"));
1805 /* Only for superuser! */
1806 #if __FreeBSD_version < 500000
1808 #elif __FreeBSD_version < 700000
1811 error = priv_check (td, PRIV_DRIVER);
1815 if (c->type != T_E1 || c->unfram)
1819 ce_set_crc4 (c, *(int*)data);
1825 CE_DEBUG2 (d, ("ioctl: getclk\n"));
1826 if (c->type != T_E1)
1829 default: *(int*)data = E1CLK_INTERNAL; break;
1830 case GSYN_RCV: *(int*)data = E1CLK_RECEIVE; break;
1831 case GSYN_RCV0: *(int*)data = E1CLK_RECEIVE_CHAN0; break;
1832 case GSYN_RCV1: *(int*)data = E1CLK_RECEIVE_CHAN1; break;
1837 CE_DEBUG2 (d, ("ioctl: setclk\n"));
1838 /* Only for superuser! */
1839 #if __FreeBSD_version < 500000
1841 #elif __FreeBSD_version < 700000
1844 error = priv_check (td, PRIV_DRIVER);
1848 if (c->type != T_E1)
1852 switch (*(int*)data) {
1853 default: ce_set_gsyn (c, GSYN_INT); break;
1854 case E1CLK_RECEIVE: ce_set_gsyn (c, GSYN_RCV); break;
1855 case E1CLK_RECEIVE_CHAN0: ce_set_gsyn (c, GSYN_RCV0); break;
1856 case E1CLK_RECEIVE_CHAN1: ce_set_gsyn (c, GSYN_RCV1); break;
1864 CE_DEBUG2 (d, ("ioctl: reset\n"));
1865 /* Only for superuser! */
1866 #if __FreeBSD_version < 500000
1868 #elif __FreeBSD_version < 700000
1871 error = priv_check (td, PRIV_DRIVER);
1877 /* ce_reset (c->board, 0, 0);*/
1882 case SERIAL_HARDRESET:
1883 CE_DEBUG2 (d, ("ioctl: hardreset\n"));
1884 /* Only for superuser! */
1885 #if __FreeBSD_version < 500000
1887 #elif __FreeBSD_version < 700000
1890 error = priv_check (td, PRIV_DRIVER);
1896 /* hard_reset (c->board); */
1902 case SERIAL_GETCABLE:
1903 CE_DEBUG2 (d, ("ioctl: getcable\n"));
1904 if (c->type != T_E1)
1908 *(int*)data = CABLE_TP;
1914 CE_DEBUG2 (d, ("ioctl: getdir\n"));
1915 if (c->type != T_E1 && c->type != T_DATA)
1917 *(int*)data = c->dir;
1921 CE_DEBUG2 (d, ("ioctl: setdir\n"));
1922 /* Only for superuser! */
1923 #if __FreeBSD_version < 500000
1925 #elif __FreeBSD_version < 700000
1928 error = priv_check (td, PRIV_DRIVER);
1934 ce_set_dir (c, *(int*)data);
1939 case TIOCSDTR: /* Set DTR */
1947 case TIOCCDTR: /* Clear DTR */
1955 case TIOCMSET: /* Set DTR/RTS */
1958 ce_set_dtr (c, (*(int*)data & TIOCM_DTR) ? 1 : 0);
1959 ce_set_rts (c, (*(int*)data & TIOCM_RTS) ? 1 : 0);
1964 case TIOCMBIS: /* Add DTR/RTS */
1967 if (*(int*)data & TIOCM_DTR) ce_set_dtr (c, 1);
1968 if (*(int*)data & TIOCM_RTS) ce_set_rts (c, 1);
1973 case TIOCMBIC: /* Clear DTR/RTS */
1976 if (*(int*)data & TIOCM_DTR) ce_set_dtr (c, 0);
1977 if (*(int*)data & TIOCM_RTS) ce_set_rts (c, 0);
1982 case TIOCMGET: /* Get modem status */
1983 *(int*)data = ce_modem_status (c);
1990 #if __FreeBSD_version >= 500000
1991 static int ng_ce_constructor (node_p node)
1993 drv_t *d = NG_NODE_PRIVATE (node);
1995 static int ng_ce_constructor (node_p *node)
1997 drv_t *d = (*node)->private;
1999 CE_DEBUG (d, ("Constructor\n"));
2003 static int ng_ce_newhook (node_p node, hook_p hook, const char *name)
2006 #if __FreeBSD_version >= 500000
2007 drv_t *d = NG_NODE_PRIVATE (node);
2009 drv_t *d = node->private;
2011 bdrv_t *bd = d->board->sys;
2013 CE_DEBUG (d, ("Newhook\n"));
2014 /* Attach debug hook */
2015 if (strcmp (name, NG_CE_HOOK_DEBUG) == 0) {
2016 #if __FreeBSD_version >= 500000
2017 NG_HOOK_SET_PRIVATE (hook, NULL);
2021 d->debug_hook = hook;
2025 /* Check for raw hook */
2026 if (strcmp (name, NG_CE_HOOK_RAW) != 0)
2029 #if __FreeBSD_version >= 500000
2030 NG_HOOK_SET_PRIVATE (hook, d);
2043 static char *format_timeslots (u_long s)
2045 static char buf [100];
2049 for (i=1; i<32; ++i)
2051 int prev = (i > 1) & (s >> (i-1));
2052 int next = (i < 31) & (s >> (i+1));
2062 *p++ = '0' + i / 10;
2063 *p++ = '0' + i % 10;
2069 static int print_modems (char *s, ce_chan_t *c, int need_header)
2071 int status = ce_modem_status (c);
2075 length += sprintf (s + length, " LE DTR DSR RTS CTS CD\n");
2076 length += sprintf (s + length, "%4s %4s %4s %4s %4s %4s\n",
2077 status & TIOCM_LE ? "On" : "-",
2078 status & TIOCM_DTR ? "On" : "-",
2079 status & TIOCM_DSR ? "On" : "-",
2080 status & TIOCM_RTS ? "On" : "-",
2081 status & TIOCM_CTS ? "On" : "-",
2082 status & TIOCM_CD ? "On" : "-");
2086 static int print_stats (char *s, ce_chan_t *c, int need_header)
2091 length += sprintf (s + length, " Rintr Tintr Mintr Ibytes Ipkts Ierrs Obytes Opkts Oerrs\n");
2092 length += sprintf (s + length, "%7ld %7ld %7ld %8lu %7ld %7ld %8lu %7ld %7ld\n",
2093 c->rintr, c->tintr, 0l, (unsigned long) c->ibytes,
2094 c->ipkts, c->overrun + c->frame + c->crc,
2095 (unsigned long) c->obytes, c->opkts, c->underrun);
2099 static char *format_e1_status (u_char status)
2101 static char buf [80];
2103 if (status & E1_NOALARM)
2106 if (status & E1_LOS) strcat (buf, ",LOS");
2107 if (status & E1_AIS) strcat (buf, ",AIS");
2108 if (status & E1_LOF) strcat (buf, ",LOF");
2109 if (status & E1_LOMF) strcat (buf, ",LOMF");
2110 if (status & E1_FARLOF) strcat (buf, ",FARLOF");
2111 if (status & E1_AIS16) strcat (buf, ",AIS16");
2112 if (status & E1_FARLOMF) strcat (buf, ",FARLOMF");
2113 if (status & E1_TSTREQ) strcat (buf, ",TSTREQ");
2114 if (status & E1_TSTERR) strcat (buf, ",TSTERR");
2120 static int print_frac (char *s, int leftalign, u_long numerator, u_long divider)
2124 if (numerator < 1 || divider < 1) {
2125 length += sprintf (s+length, leftalign ? "/- " : " -");
2128 n = (int) (0.5 + 1000.0 * numerator / divider);
2130 length += sprintf (s+length, leftalign ? "/.%-3d" : " .%03d", n);
2133 *(s + length) = leftalign ? '/' : ' ';
2136 if (n >= 1000000) n = (n+500) / 1000 * 1000;
2137 else if (n >= 100000) n = (n+50) / 100 * 100;
2138 else if (n >= 10000) n = (n+5) / 10 * 10;
2141 case 1000: length += printf (s+length, ".999"); return length;
2142 case 10000: n = 9990; break;
2143 case 100000: n = 99900; break;
2144 case 1000000: n = 999000; break;
2146 if (n < 10000) length += sprintf (s+length, "%d.%d", n/1000, n/10%100);
2147 else if (n < 100000) length += sprintf (s+length, "%d.%d", n/1000, n/100%10);
2148 else if (n < 1000000) length += sprintf (s+length, "%d.", n/1000);
2149 else length += sprintf (s+length, "%d", n/1000);
2154 static int print_e1_stats (char *s, ce_chan_t *c)
2156 struct e1_counters total;
2160 totsec = c->totsec + c->cursec;
2161 total.bpv = c->total.bpv + c->currnt.bpv;
2162 total.fse = c->total.fse + c->currnt.fse;
2163 total.crce = c->total.crce + c->currnt.crce;
2164 total.rcrce = c->total.rcrce + c->currnt.rcrce;
2165 total.uas = c->total.uas + c->currnt.uas;
2166 total.les = c->total.les + c->currnt.les;
2167 total.es = c->total.es + c->currnt.es;
2168 total.bes = c->total.bes + c->currnt.bes;
2169 total.ses = c->total.ses + c->currnt.ses;
2170 total.oofs = c->total.oofs + c->currnt.oofs;
2171 total.css = c->total.css + c->currnt.css;
2172 total.dm = c->total.dm + c->currnt.dm;
2174 length += sprintf (s + length, " Unav/Degr Bpv/Fsyn CRC/RCRC Err/Lerr Sev/Bur Oof/Slp Status\n");
2176 /* Unavailable seconds, degraded minutes */
2177 length += print_frac (s + length, 0, c->currnt.uas, c->cursec);
2178 length += print_frac (s + length, 1, 60 * c->currnt.dm, c->cursec);
2180 /* Bipolar violations, frame sync errors */
2181 length += print_frac (s + length, 0, c->currnt.bpv, c->cursec);
2182 length += print_frac (s + length, 1, c->currnt.fse, c->cursec);
2184 /* CRC errors, remote CRC errors (E-bit) */
2185 length += print_frac (s + length, 0, c->currnt.crce, c->cursec);
2186 length += print_frac (s + length, 1, c->currnt.rcrce, c->cursec);
2188 /* Errored seconds, line errored seconds */
2189 length += print_frac (s + length, 0, c->currnt.es, c->cursec);
2190 length += print_frac (s + length, 1, c->currnt.les, c->cursec);
2192 /* Severely errored seconds, burst errored seconds */
2193 length += print_frac (s + length, 0, c->currnt.ses, c->cursec);
2194 length += print_frac (s + length, 1, c->currnt.bes, c->cursec);
2196 /* Out of frame seconds, controlled slip seconds */
2197 length += print_frac (s + length, 0, c->currnt.oofs, c->cursec);
2198 length += print_frac (s + length, 1, c->currnt.css, c->cursec);
2200 length += sprintf (s + length, " %s\n", format_e1_status (c->status));
2202 /* Print total statistics. */
2203 length += print_frac (s + length, 0, total.uas, totsec);
2204 length += print_frac (s + length, 1, 60 * total.dm, totsec);
2206 length += print_frac (s + length, 0, total.bpv, totsec);
2207 length += print_frac (s + length, 1, total.fse, totsec);
2209 length += print_frac (s + length, 0, total.crce, totsec);
2210 length += print_frac (s + length, 1, total.rcrce, totsec);
2212 length += print_frac (s + length, 0, total.es, totsec);
2213 length += print_frac (s + length, 1, total.les, totsec);
2215 length += print_frac (s + length, 0, total.ses, totsec);
2216 length += print_frac (s + length, 1, total.bes, totsec);
2218 length += print_frac (s + length, 0, total.oofs, totsec);
2219 length += print_frac (s + length, 1, total.css, totsec);
2221 length += sprintf (s + length, " -- Total\n");
2225 static int print_chan (char *s, ce_chan_t *c)
2230 length += sprintf (s + length, "ce%d", c->board->num * NCHAN + c->num);
2232 length += sprintf (s + length, " debug=%d", d->chan->debug);
2234 if (c->board->mux) {
2235 length += sprintf (s + length, " cfg=C");
2237 length += sprintf (s + length, " cfg=A");
2241 length += sprintf (s + length, " %ld", c->baud);
2243 length += sprintf (s + length, " extclock");
2245 if (c->type == T_E1)
2247 case GSYN_INT : length += sprintf (s + length, " syn=int"); break;
2248 case GSYN_RCV : length += sprintf (s + length, " syn=rcv"); break;
2249 case GSYN_RCV0 : length += sprintf (s + length, " syn=rcv0"); break;
2250 case GSYN_RCV1 : length += sprintf (s + length, " syn=rcv1"); break;
2252 if (c->type == T_E1)
2253 length += sprintf (s + length, " higain=%s", c->higain ? "on" : "off");
2255 length += sprintf (s + length, " loop=%s", c->lloop ? "on" : "off");
2257 if (c->type == T_E1)
2258 length += sprintf (s + length, " ts=%s", format_timeslots (c->ts));
2259 length += sprintf (s + length, "\n");
2263 #if __FreeBSD_version >= 500000
2264 static int ng_ce_rcvmsg (node_p node, item_p item, hook_p lasthook)
2266 drv_t *d = NG_NODE_PRIVATE (node);
2267 struct ng_mesg *msg;
2269 static int ng_ce_rcvmsg (node_p node, struct ng_mesg *msg,
2270 const char *retaddr, struct ng_mesg **rptr)
2272 drv_t *d = node->private;
2274 struct ng_mesg *resp = NULL;
2277 CE_DEBUG (d, ("Rcvmsg\n"));
2278 #if __FreeBSD_version >= 500000
2279 NGI_GET_MSG (item, msg);
2281 switch (msg->header.typecookie) {
2287 printf ("Not implemented yet\n");
2291 case NGM_GENERIC_COOKIE:
2292 switch (msg->header.cmd) {
2297 case NGM_TEXT_STATUS: {
2300 int dl = sizeof (struct ng_mesg) + 730;
2302 #if __FreeBSD_version >= 500000
2303 NG_MKRESPONSE (resp, msg, dl, M_NOWAIT);
2309 resp = malloc (M_NETGRAPH, M_NOWAIT);
2318 l += print_chan (s + l, d->chan);
2319 l += print_stats (s + l, d->chan, 1);
2320 l += print_modems (s + l, d->chan, 1);
2321 l += print_e1_stats (s + l, d->chan);
2323 l += sprintf (s + l, "Error: node not connect to channel");
2324 #if __FreeBSD_version < 500000
2325 (resp)->header.version = NG_VERSION;
2326 (resp)->header.arglen = strlen (s) + 1;
2327 (resp)->header.token = msg->header.token;
2328 (resp)->header.typecookie = NGM_CE_COOKIE;
2329 (resp)->header.cmd = msg->header.cmd;
2331 strncpy ((resp)->header.cmdstr, "status", NG_CMDSTRSIZ);
2337 #if __FreeBSD_version >= 500000
2338 NG_RESPOND_MSG (error, node, item, resp);
2342 free (msg, M_NETGRAPH);
2347 #if __FreeBSD_version >= 500000
2348 static int ng_ce_rcvdata (hook_p hook, item_p item)
2350 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE(hook));
2352 #if __FreeBSD_version < 502120
2355 struct ng_tag_prio *ptag;
2358 static int ng_ce_rcvdata (hook_p hook, struct mbuf *m, meta_p meta)
2360 drv_t *d = hook->node->private;
2362 bdrv_t *bd = d->board->sys;
2366 CE_DEBUG2 (d, ("Rcvdata\n"));
2367 #if __FreeBSD_version >= 500000
2368 NGI_GET_M (item, m);
2369 #if __FreeBSD_version < 502120
2370 NGI_GET_META (item, meta);
2372 NG_FREE_ITEM (item);
2373 if (! NG_HOOK_PRIVATE (hook) || ! d) {
2375 #if __FreeBSD_version < 502120
2376 NG_FREE_META (meta);
2379 if (! hook->private || ! d) {
2380 NG_FREE_DATA (m,meta);
2385 #if __FreeBSD_version >= 502120
2386 /* Check for high priority data */
2387 if ((ptag = (struct ng_tag_prio *)m_tag_locate(m, NGM_GENERIC_COOKIE,
2388 NG_TAG_PRIO, NULL)) != NULL && (ptag->priority > NG_PRIO_CUTOFF) )
2393 q = (meta && meta->priority > 0) ? &d->hi_queue : &d->queue;
2398 #if __FreeBSD_version >= 500000
2400 if (_IF_QFULL (q)) {
2405 #if __FreeBSD_version < 502120
2406 NG_FREE_META (meta);
2417 NG_FREE_DATA (m, meta);
2428 static int ng_ce_rmnode (node_p node)
2430 #if __FreeBSD_version >= 500000
2431 drv_t *d = NG_NODE_PRIVATE (node);
2433 CE_DEBUG (d, ("Rmnode\n"));
2434 if (d && d->running) {
2435 bdrv_t *bd = d->board->sys;
2443 #if __FreeBSD_version >= 502120
2444 if (node->nd_flags & NGF_REALLY_DIE) {
2446 if (node->nd_flags & NG_REALLY_DIE) {
2448 NG_NODE_SET_PRIVATE (node, NULL);
2449 NG_NODE_UNREF (node);
2451 #if __FreeBSD_version >= 502120
2452 NG_NODE_REVIVE(node); /* Persistant node */
2454 node->nd_flags &= ~NG_INVALID;
2457 #else /* __FreeBSD_version < 500000 */
2458 drv_t *d = node->private;
2460 if (d && d->running) {
2461 bdrv_t *bd = d->board->sys;
2469 node->flags |= NG_INVALID;
2479 static int ng_ce_connect (hook_p hook)
2481 #if __FreeBSD_version >= 500000
2482 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2484 drv_t *d = hook->node->private;
2488 CE_DEBUG (d, ("Connect\n"));
2489 callout_reset (&d->timeout_handle, hz, ce_watchdog_timer, d);
2495 static int ng_ce_disconnect (hook_p hook)
2497 #if __FreeBSD_version >= 500000
2498 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2500 drv_t *d = hook->node->private;
2504 CE_DEBUG (d, ("Disconnect\n"));
2505 #if __FreeBSD_version >= 500000
2506 if (NG_HOOK_PRIVATE (hook))
2511 bdrv_t *bd = d->board->sys;
2518 /* If we were wait it than it reasserted now, just stop it. */
2519 if (!callout_drain (&d->timeout_handle))
2520 callout_stop (&d->timeout_handle);
2526 static int ce_modevent (module_t mod, int type, void *unused)
2528 #if __FreeBSD_version < 500000
2530 struct cdevsw *cdsw;
2532 static int load_count = 0;
2534 #if __FreeBSD_version < 500000
2535 dev = makedev (CDEV_MAJOR, 0);
2540 #if __FreeBSD_version < 500000
2542 (cdsw = devsw (dev)) &&
2543 cdsw->d_maj == CDEV_MAJOR) {
2544 printf ("Tau32-PCI driver is already in system\n");
2548 #if __FreeBSD_version >= 500000 && defined NETGRAPH
2549 if (ng_newtype (&typestruct))
2550 printf ("Failed to register ng_ce\n");
2553 #if __FreeBSD_version <= 500000
2554 cdevsw_add (&ce_cdevsw);
2556 #if __FreeBSD_version >= 500000
2557 callout_init (&timeout_handle, 1);
2559 callout_init (&timeout_handle);
2561 callout_reset (&timeout_handle, hz*5, ce_timeout, 0);
2564 if (load_count == 1) {
2565 printf ("Removing device entry for Tau32-PCI\n");
2566 #if __FreeBSD_version <= 500000
2567 cdevsw_remove (&ce_cdevsw);
2569 #if __FreeBSD_version >= 500000 && defined NETGRAPH
2570 ng_rmtype (&typestruct);
2573 /* If we were wait it than it reasserted now, just stop it.
2574 * Actually we shouldn't get this condition. But code could be
2575 * changed in the future, so just be a litle paranoid.
2577 if (!callout_drain (&timeout_handle))
2578 callout_stop (&timeout_handle);
2588 #if __FreeBSD_version >= 502100
2589 static struct ng_type typestruct = {
2590 .version = NG_ABI_VERSION,
2591 .name = NG_CE_NODE_TYPE,
2592 .constructor = ng_ce_constructor,
2593 .rcvmsg = ng_ce_rcvmsg,
2594 .shutdown = ng_ce_rmnode,
2595 .newhook = ng_ce_newhook,
2596 .connect = ng_ce_connect,
2597 .rcvdata = ng_ce_rcvdata,
2598 .disconnect = ng_ce_disconnect,
2600 #else /* __FreeBSD_version < 502100 */
2601 static struct ng_type typestruct = {
2602 #if __FreeBSD_version >= 500000
2616 #if __FreeBSD_version < 500000
2622 #endif /* __FreeBSD_version < 502100 */
2626 #if __FreeBSD_version >= 500000
2628 MODULE_DEPEND (ng_ce, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION);
2630 MODULE_DEPEND (ce, sppp, 1, 1, 1);
2633 DRIVER_MODULE (cemod, pci, ce_driver, ce_devclass, ce_modevent, NULL);
2635 DRIVER_MODULE (ce, pci, ce_driver, ce_devclass, ce_modevent, NULL);
2637 #else /* if __FreeBSD_version < 500000*/
2639 DRIVER_MODULE (ce, pci, ce_driver, ce_devclass, ng_mod_event, &typestruct);
2641 DRIVER_MODULE (ce, pci, ce_driver, ce_devclass, ce_modevent, NULL);
2643 #endif /* __FreeBSD_version < 500000 */