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;}})
107 #ifndef CALLOUT_MPSAFE
108 #define CALLOUT_MPSAFE 0
112 #define IF_DRAIN(ifq) do { \
115 IF_DEQUEUE(ifq, m); \
124 #define _IF_QLEN(ifq) ((ifq)->ifq_len)
127 #ifndef callout_drain
128 #define callout_drain callout_stop
131 #define CE_LOCK_NAME "ceX"
133 #define CE_LOCK(_bd) mtx_lock (&(_bd)->ce_mtx)
134 #define CE_UNLOCK(_bd) mtx_unlock (&(_bd)->ce_mtx)
135 #define CE_LOCK_ASSERT(_bd) mtx_assert (&(_bd)->ce_mtx, MA_OWNED)
137 #define CDEV_MAJOR 185
139 static int ce_probe __P((device_t));
140 static int ce_attach __P((device_t));
141 static int ce_detach __P((device_t));
143 static device_method_t ce_methods[] = {
144 /* Device interface */
145 DEVMETHOD(device_probe, ce_probe),
146 DEVMETHOD(device_attach, ce_attach),
147 DEVMETHOD(device_detach, ce_detach),
152 typedef struct _ce_dma_mem_t {
156 #if __FreeBSD_version >= 500000
162 typedef struct _drv_t {
167 struct ifqueue rqueue;
169 char nodename [NG_NODESIZE];
173 struct ifqueue queue;
174 struct ifqueue hi_queue;
179 struct callout timeout_handle;
180 #if __FreeBSD_version >= 500000
182 #else /* __FreeBSD_version < 500000 */
188 typedef struct _bdrv_t {
190 struct resource *ce_res;
191 struct resource *ce_irq;
194 drv_t channel [NCHAN];
195 #if __FreeBSD_version >= 504000
200 static driver_t ce_driver = {
206 static devclass_t ce_devclass;
208 static void ce_receive (ce_chan_t *c, unsigned char *data, int len);
209 static void ce_transmit (ce_chan_t *c, void *attachment, int len);
210 static void ce_error (ce_chan_t *c, int data);
211 static void ce_up (drv_t *d);
212 static void ce_start (drv_t *d);
213 static void ce_down (drv_t *d);
214 static void ce_watchdog (drv_t *d);
215 static void ce_watchdog_timer (void *arg);
217 extern struct ng_type typestruct;
219 static void ce_ifstart (struct ifnet *ifp);
220 static void ce_tlf (struct sppp *sp);
221 static void ce_tls (struct sppp *sp);
222 static int ce_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data);
223 static void ce_initialize (void *softc);
226 static ce_board_t *adapter [NBRD];
227 static drv_t *channel [NBRD*NCHAN];
228 static struct callout led_timo [NBRD];
229 static struct callout timeout_handle;
231 static int ce_destroy = 0;
233 #if __FreeBSD_version < 500000
234 static int ce_open (dev_t dev, int oflags, int devtype, struct proc *p);
235 static int ce_close (dev_t dev, int fflag, int devtype, struct proc *p);
236 static int ce_ioctl (dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p);
238 static int ce_open (struct cdev *dev, int oflags, int devtype, struct thread *td);
239 static int ce_close (struct cdev *dev, int fflag, int devtype, struct thread *td);
240 static int ce_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td);
242 #if __FreeBSD_version < 500000
243 static struct cdevsw ce_cdevsw = {
244 ce_open, ce_close, noread, nowrite,
245 ce_ioctl, nopoll, nommap, nostrategy,
246 "ce", CDEV_MAJOR, nodump, nopsize,
249 #elif __FreeBSD_version == 500000
250 static struct cdevsw ce_cdevsw = {
251 ce_open, ce_close, noread, nowrite,
252 ce_ioctl, nopoll, nommap, nostrategy,
253 "ce", CDEV_MAJOR, nodump, nopsize,
256 #elif __FreeBSD_version <= 501000
257 static struct cdevsw ce_cdevsw = {
265 .d_strategy = nostrategy,
271 #elif __FreeBSD_version < 502103
272 static struct cdevsw ce_cdevsw = {
280 #elif __FreeBSD_version < 600000
281 static struct cdevsw ce_cdevsw = {
282 .d_version = D_VERSION,
288 .d_flags = D_NEEDGIANT,
290 #else /* __FreeBSD_version >= 600000 */
291 static struct cdevsw ce_cdevsw = {
292 .d_version = D_VERSION,
301 * Print the mbuf chain, for debug purposes only.
303 static void printmbuf (struct mbuf *m)
306 for (; m; m=m->m_next) {
307 if (m->m_flags & M_PKTHDR)
308 printf (" HDR %d:", m->m_pkthdr.len);
309 if (m->m_flags & M_EXT)
311 printf (" %d", m->m_len);
317 * Make an mbuf from data.
319 static struct mbuf *makembuf (void *buf, unsigned len)
323 MGETHDR (m, M_NOWAIT, MT_DATA);
326 MCLGET (m, M_NOWAIT);
327 if (! (m->m_flags & M_EXT)) {
331 m->m_pkthdr.len = m->m_len = len;
332 bcopy (buf, mtod (m, caddr_t), len);
336 static int ce_probe (device_t dev)
338 if ((pci_get_vendor (dev) == TAU32_PCI_VENDOR_ID) &&
339 (pci_get_device (dev) == TAU32_PCI_DEVICE_ID)) {
340 device_set_desc (dev, "Cronyx-Tau32-PCI serial adapter");
341 return BUS_PROBE_DEFAULT;
346 static void ce_timeout (void *arg)
351 for (i = 0; i < NBRD; ++i) {
352 if (adapter[i] == NULL)
354 for (k = 0; k < NCHAN; ++k) {
360 d = channel[i * NCHAN + k];
365 CE_LOCK ((bdrv_t *)d->board->sys);
366 switch (d->chan->type) {
368 ce_e1_timer (d->chan);
373 CE_UNLOCK ((bdrv_t *)d->board->sys);
379 callout_reset (&timeout_handle, hz, ce_timeout, 0);
383 static void ce_led_off (void *arg)
386 bdrv_t *bd = (bdrv_t *) b->sys;
394 TAU32_LedSet (b->ddk.pControllerObject, 0);
399 static void ce_intr (void *arg)
402 ce_board_t *b = bd->board;
405 #if __FreeBSD_version >= 500000 && defined NETGRAPH
415 TAU32_LedSet (b->ddk.pControllerObject, 1);
417 TAU32_HandleInterrupt (b->ddk.pControllerObject);
419 /* Turn LED off 50 msec later. */
420 callout_reset (&led_timo[b->num], hz/20, ce_led_off, b);
424 /* Pass packets in a lock-free state */
425 for (i = 0; i < NCHAN && b->chan[i].type; i++) {
426 drv_t *d = b->chan[i].sys;
428 if (!d || !d->running)
430 while (_IF_QLEN(&d->rqueue)) {
431 IF_DEQUEUE (&d->rqueue,m);
436 #if __FreeBSD_version >= 500000
437 NG_SEND_DATA_ONLY (error, d->hook, m);
439 ng_queue_data (d->hook, m, 0);
442 IF_DRAIN (&d->rqueue);
445 sppp_input (d->ifp, m);
451 #if __FreeBSD_version >= 500000
453 ce_bus_dmamap_addr (void *arg, bus_dma_segment_t *segs, int nseg, int error)
460 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
462 *addr = segs->ds_addr;
466 #define BUS_DMA_ZERO 0
470 ce_bus_dma_mem_alloc (int bnum, int cnum, ce_dma_mem_t *dmem)
474 error = bus_dma_tag_create (NULL, 16, 0, BUS_SPACE_MAXADDR_32BIT,
475 BUS_SPACE_MAXADDR, NULL, NULL, dmem->size, 1,
477 #if __FreeBSD_version >= 502000
482 if (cnum >= 0) printf ("ce%d-%d: ", bnum, cnum);
483 else printf ("ce%d: ", bnum);
484 printf ("couldn't allocate tag for dma memory\n");
487 error = bus_dmamem_alloc (dmem->dmat, (void **)&dmem->virt,
488 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &dmem->mapp);
490 if (cnum >= 0) printf ("ce%d-%d: ", bnum, cnum);
491 else printf ("ce%d: ", bnum);
492 printf ("couldn't allocate mem for dma memory\n");
493 bus_dma_tag_destroy (dmem->dmat);
496 error = bus_dmamap_load (dmem->dmat, dmem->mapp, dmem->virt,
497 dmem->size, ce_bus_dmamap_addr, &dmem->phys, 0);
499 if (cnum >= 0) printf ("ce%d-%d: ", bnum, cnum);
500 else printf ("ce%d: ", bnum);
501 printf ("couldn't load mem map for dma memory\n");
502 bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
503 bus_dma_tag_destroy (dmem->dmat);
506 #if __FreeBSD_version >= 502000
507 bzero (dmem->virt, dmem->size);
513 ce_bus_dma_mem_free (ce_dma_mem_t *dmem)
515 bus_dmamap_unload (dmem->dmat, dmem->mapp);
516 bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
517 bus_dma_tag_destroy (dmem->dmat);
521 ce_bus_dma_mem_alloc (int bnum, int cnum, ce_dma_mem_t *dmem)
523 dmem->virt = contigmalloc (dmem->size, M_DEVBUF, M_WAITOK,
524 0x100000, 0xffffffff, 16, 0);
525 if (dmem->virt == NULL) {
526 if (cnum >= 0) printf ("ce%d-%d: ", bnum, cnum);
527 else printf ("ce%d: ", bnum);
528 printf ("couldn't allocate dma memory\n");
531 dmem->phys = vtophys (dmem->virt);
532 bzero (dmem->virt, dmem->size);
537 ce_bus_dma_mem_free (ce_dma_mem_t *dmem)
539 contigfree (dmem->virt, dmem->size, M_DEVBUF);
544 * Called if the probe succeeded.
546 static int ce_attach (device_t dev)
548 bdrv_t *bd = device_get_softc (dev);
549 int unit = device_get_unit (dev);
550 #if __FreeBSD_version >= 504000
551 char *ce_ln = CE_LOCK_NAME;
560 b = malloc (sizeof(ce_board_t), M_DEVBUF, M_WAITOK);
562 printf ("ce%d: couldn't allocate memory\n", unit);
565 bzero (b, sizeof(ce_board_t));
569 #if __FreeBSD_version >= 440000
570 pci_enable_busmaster (dev);
573 bd->dmamem.size = TAU32_ControllerObjectSize;
574 if (! ce_bus_dma_mem_alloc (unit, -1, &bd->dmamem)) {
578 b->ddk.pControllerObject = bd->dmamem.virt;
583 bd->ce_res = bus_alloc_resource (dev, SYS_RES_MEMORY, &rid,
584 0, ~0, 1, RF_ACTIVE);
586 printf ("ce%d: cannot map memory\n", unit);
587 ce_bus_dma_mem_free (&bd->dmamem);
591 vbase = (vm_offset_t) rman_get_virtual (bd->ce_res);
593 b->ddk.PciBar1VirtualAddress = (void *)vbase;
594 b->ddk.ControllerObjectPhysicalAddress = bd->dmamem.phys;
595 b->ddk.pErrorNotifyCallback = ce_error_callback;
596 b->ddk.pStatusNotifyCallback = ce_status_callback;
599 TAU32_BeforeReset(&b->ddk);
600 pci_write_config (dev, TAU32_PCI_RESET_ADDRESS, TAU32_PCI_RESET_ON, 4);
601 pci_write_config (dev, TAU32_PCI_RESET_ADDRESS, TAU32_PCI_RESET_OFF, 4);
603 if(!TAU32_Initialize(&b->ddk, 0))
605 printf ("ce%d: init adapter error 0x%08x, bus dead bits 0x%08lx\n",
606 unit, b->ddk.InitErrors, b->ddk.DeadBits);
607 bus_release_resource (dev, SYS_RES_MEMORY, PCIR_BAR(0), bd->ce_res);
608 ce_bus_dma_mem_free (&bd->dmamem);
618 bd->ce_irq = bus_alloc_resource (dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
619 RF_SHAREABLE | RF_ACTIVE);
621 printf ("ce%d: cannot map interrupt\n", unit);
622 bus_release_resource (dev, SYS_RES_MEMORY, PCIR_BAR(0), bd->ce_res);
623 ce_bus_dma_mem_free (&bd->dmamem);
628 #if __FreeBSD_version >= 500000
629 callout_init (&led_timo[unit], CALLOUT_MPSAFE);
631 callout_init (&led_timo[unit]);
633 error = bus_setup_intr (dev, bd->ce_irq,
634 #if __FreeBSD_version >= 500013
635 INTR_TYPE_NET|INTR_MPSAFE,
639 NULL, ce_intr, bd, &bd->ce_intrhand);
641 printf ("ce%d: cannot set up irq\n", unit);
642 bus_release_resource (dev, SYS_RES_IRQ, 0, bd->ce_irq);
643 bus_release_resource (dev, SYS_RES_MEMORY,
644 PCIR_BAR(0), bd->ce_res);
645 ce_bus_dma_mem_free (&bd->dmamem);
651 switch (b->ddk.Model) {
652 case 1: strcpy (b->name, TAU32_BASE_NAME); break;
653 case 2: strcpy (b->name, TAU32_LITE_NAME); break;
654 case 3: strcpy (b->name, TAU32_ADPCM_NAME); break;
655 default: strcpy (b->name, TAU32_UNKNOWN_NAME); break;
658 printf ("ce%d: %s\n", unit, b->name);
660 for (c = b->chan; c < b->chan + NCHAN; ++c) {
661 c->num = (c - b->chan);
664 d = &bd->channel[c->num];
665 d->dmamem.size = sizeof(ce_buf_t);
666 if (! ce_bus_dma_mem_alloc (unit, c->num, &d->dmamem))
669 channel [b->num * NCHAN + c->num] = d;
670 sprintf (d->name, "ce%d.%d", b->num, c->num);
676 for (c = b->chan; c < b->chan + NCHAN; ++c) {
681 callout_init (&d->timeout_handle, CALLOUT_MPSAFE);
683 if (ng_make_node_common (&typestruct, &d->node) != 0) {
684 printf ("%s: cannot make common node\n", d->name);
688 #if __FreeBSD_version >= 500000
689 NG_NODE_SET_PRIVATE (d->node, d);
691 d->node->private = d;
693 sprintf (d->nodename, "%s%d", NG_CE_NODE_TYPE,
694 c->board->num * NCHAN + c->num);
695 if (ng_name_node (d->node, d->nodename)) {
696 printf ("%s: cannot name node\n", d->nodename);
697 #if __FreeBSD_version >= 500000
698 NG_NODE_UNREF (d->node);
705 d->queue.ifq_maxlen = ifqmaxlen;
706 d->hi_queue.ifq_maxlen = ifqmaxlen;
707 d->rqueue.ifq_maxlen = ifqmaxlen;
708 #if __FreeBSD_version >= 500000
709 mtx_init (&d->queue.ifq_mtx, "ce_queue", NULL, MTX_DEF);
710 mtx_init (&d->hi_queue.ifq_mtx, "ce_queue_hi", NULL, MTX_DEF);
711 mtx_init (&d->rqueue.ifq_mtx, "ce_rqueue", NULL, MTX_DEF);
714 #if __FreeBSD_version >= 600031
715 d->ifp = if_alloc(IFT_PPP);
717 d->ifp = malloc (sizeof(struct sppp), M_DEVBUF, M_WAITOK);
718 bzero (d->ifp, sizeof(struct sppp));
721 printf ("%s: cannot if_alloc() interface\n", d->name);
724 d->ifp->if_softc = d;
725 #if __FreeBSD_version > 501000
726 if_initname (d->ifp, "ce", b->num * NCHAN + c->num);
728 d->ifp->if_unit = b->num * NCHAN + c->num;
729 d->ifp->if_name = "ce";
731 d->ifp->if_mtu = PP_MTU;
732 d->ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
733 d->ifp->if_ioctl = ce_sioctl;
734 d->ifp->if_start = ce_ifstart;
735 d->ifp->if_init = ce_initialize;
736 d->rqueue.ifq_maxlen = ifqmaxlen;
737 #if __FreeBSD_version >= 500000
738 mtx_init (&d->rqueue.ifq_mtx, "ce_rqueue", NULL, MTX_DEF);
740 sppp_attach (d->ifp);
742 IFP2SP(d->ifp)->pp_tlf = ce_tlf;
743 IFP2SP(d->ifp)->pp_tls = ce_tls;
744 /* If BPF is in the kernel, call the attach for it.
745 * The header size of PPP or Cisco/HDLC is 4 bytes. */
746 bpfattach (d->ifp, DLT_PPP, 4);
748 ce_start_chan (c, 1, 1, d->dmamem.virt, d->dmamem.phys);
750 /* Register callback functions. */
751 ce_register_transmit (c, &ce_transmit);
752 ce_register_receive (c, &ce_receive);
753 ce_register_error (c, &ce_error);
754 d->devt = make_dev (&ce_cdevsw, b->num*NCHAN+c->num, UID_ROOT,
755 GID_WHEEL, 0600, "ce%d", b->num*NCHAN+c->num);
758 #if __FreeBSD_version >= 504000
759 ce_ln[2] = '0' + unit;
760 mtx_init (&bd->ce_mtx, ce_ln, MTX_NETWORK_LOCK, MTX_DEF|MTX_RECURSE);
763 TAU32_EnableInterrupts(b->ddk.pControllerObject);
771 static int ce_detach (device_t dev)
773 bdrv_t *bd = device_get_softc (dev);
774 ce_board_t *b = bd->board;
778 #if __FreeBSD_version >= 504000
779 KASSERT (mtx_initialized (&bd->ce_mtx), ("ce mutex not initialized"));
783 /* Check if the device is busy (open). */
784 for (c = b->chan; c < b->chan + NCHAN; ++c) {
785 drv_t *d = (drv_t*) c->sys;
787 /* XXX Non existen chan! */
788 if (! d || ! d->chan)
797 /* Ok, we can unload driver */
798 /* At first we should disable interrupts */
800 TAU32_DisableInterrupts(b->ddk.pControllerObject);
802 callout_stop (&led_timo[b->num]);
804 for (c = b->chan; c < b->chan + NCHAN; ++c) {
805 drv_t *d = (drv_t*) c->sys;
807 if (! d || ! d->chan)
809 callout_stop (&d->timeout_handle);
811 /* Detach from the packet filter list of interfaces. */
814 /* Detach from the sync PPP list. */
815 sppp_detach (d->ifp);
817 /* Detach from the system list of interfaces. */
819 #if __FreeBSD_version > 600031
822 free (d->ifp, M_DEVBUF);
825 IF_DRAIN (&d->rqueue);
826 #if __FreeBSD_version >= 500000
827 mtx_destroy (&d->rqueue.ifq_mtx);
830 #if __FreeBSD_version >= 500000
832 ng_rmnode_self (d->node);
833 NG_NODE_UNREF (d->node);
836 IF_DRAIN (&d->rqueue);
837 mtx_destroy (&d->queue.ifq_mtx);
838 mtx_destroy (&d->hi_queue.ifq_mtx);
839 mtx_destroy (&d->rqueue.ifq_mtx);
845 destroy_dev (d->devt);
851 callout_drain (&led_timo[b->num]);
853 /* Disable the interrupt request. */
854 bus_teardown_intr (dev, bd->ce_irq, bd->ce_intrhand);
855 bus_release_resource (dev, SYS_RES_IRQ, 0, bd->ce_irq);
856 TAU32_DestructiveHalt (b->ddk.pControllerObject, 0);
857 bus_release_resource (dev, SYS_RES_MEMORY, PCIR_BAR(0), bd->ce_res);
859 for (c = b->chan; c < b->chan + NCHAN; ++c) {
860 drv_t *d = (drv_t*) c->sys;
862 if (! d || ! d->chan)
864 callout_drain (&d->timeout_handle);
865 channel [b->num * NCHAN + c->num] = 0;
866 /* Deallocate buffers. */
867 ce_bus_dma_mem_free (&d->dmamem);
869 adapter [b->num] = 0;
870 ce_bus_dma_mem_free (&bd->dmamem);
872 #if __FreeBSD_version >= 504000
873 mtx_destroy (&bd->ce_mtx);
879 static void ce_ifstart (struct ifnet *ifp)
881 drv_t *d = ifp->if_softc;
882 bdrv_t *bd = d->board->sys;
889 static void ce_tlf (struct sppp *sp)
891 drv_t *d = SP2IFP(sp)->if_softc;
893 CE_DEBUG2 (d, ("ce_tlf\n"));
897 static void ce_tls (struct sppp *sp)
899 drv_t *d = SP2IFP(sp)->if_softc;
901 CE_DEBUG2 (d, ("ce_tls\n"));
906 * Process an ioctl request.
908 static int ce_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data)
910 drv_t *d = ifp->if_softc;
911 bdrv_t *bd = d->board->sys;
912 int error, s, was_up, should_be_up;
914 #if __FreeBSD_version >= 600034
915 was_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
917 was_up = (ifp->if_flags & IFF_RUNNING) != 0;
919 error = sppp_ioctl (ifp, cmd, data);
924 if (! (ifp->if_flags & IFF_DEBUG))
927 d->chan->debug = d->chan->debug_shadow;
930 default: CE_DEBUG2 (d, ("ioctl 0x%lx\n", cmd)); return 0;
931 case SIOCADDMULTI: CE_DEBUG2 (d, ("ioctl SIOCADDMULTI\n")); return 0;
932 case SIOCDELMULTI: CE_DEBUG2 (d, ("ioctl SIOCDELMULTI\n")); return 0;
933 case SIOCSIFFLAGS: CE_DEBUG2 (d, ("ioctl SIOCSIFFLAGS\n")); break;
934 case SIOCSIFADDR: CE_DEBUG2 (d, ("ioctl SIOCSIFADDR\n")); break;
937 /* We get here only in case of SIFFLAGS or SIFADDR. */
940 #if __FreeBSD_version >= 600034
941 should_be_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
943 should_be_up = (ifp->if_flags & IFF_RUNNING) != 0;
945 if (! was_up && should_be_up) {
946 /* Interface goes up -- start it. */
949 } else if (was_up && ! should_be_up) {
950 /* Interface is going down -- stop it. */
951 /* if ((IFP2SP(ifp)->pp_flags & PP_FR) || (ifp->if_flags & PP_CISCO))*/
954 CE_DEBUG (d, ("ioctl 0x%lx p4\n", cmd));
961 * Initialization of interface.
962 * It seems to be never called by upper level?
964 static void ce_initialize (void *softc)
968 CE_DEBUG (d, ("ce_initialize\n"));
973 * Stop the interface. Called on splimp().
975 static void ce_down (drv_t *d)
977 CE_DEBUG (d, ("ce_down\n"));
978 /* Interface is going down -- stop it. */
979 ce_set_dtr (d->chan, 0);
980 ce_set_rts (d->chan, 0);
983 callout_stop (&d->timeout_handle);
987 * Start the interface. Called on splimp().
989 static void ce_up (drv_t *d)
991 CE_DEBUG (d, ("ce_up\n"));
992 ce_set_dtr (d->chan, 1);
993 ce_set_rts (d->chan, 1);
999 * Start output on the interface. Get another datagram to send
1000 * off of the interface queue, and copy it to the interface
1001 * before starting the output.
1003 static void ce_send (drv_t *d)
1008 CE_DEBUG2 (d, ("ce_send\n"));
1010 /* No output if the interface is down. */
1014 while (ce_transmit_space (d->chan)) {
1015 /* Get the packet to send. */
1017 IF_DEQUEUE (&d->hi_queue, m);
1019 IF_DEQUEUE (&d->queue, m);
1021 m = sppp_dequeue (d->ifp);
1026 #if __FreeBSD_version >= 500000
1027 BPF_MTAP (d->ifp, m);
1030 bpf_mtap (d->ifp, m);
1033 #if __FreeBSD_version >= 490000
1034 len = m_length (m, NULL);
1036 len = m->m_pkthdr.len;
1039 printf ("%s: too long packet: %d bytes: ",
1041 else if (! m->m_next)
1042 ce_send_packet (d->chan, (u_char*) mtod (m, caddr_t), len, 0);
1044 ce_buf_item_t *item = (ce_buf_item_t*)d->chan->tx_queue;
1045 m_copydata (m, 0, len, item->buf);
1046 ce_send_packet (d->chan, item->buf, len, 0);
1049 /* Set up transmit timeout, if the transmit ring is not empty.*/
1053 #if __FreeBSD_version >= 600034
1054 d->ifp->if_flags |= IFF_DRV_OACTIVE;
1056 d->ifp->if_flags |= IFF_OACTIVE;
1062 * Start output on the interface.
1063 * Always called on splimp().
1065 static void ce_start (drv_t *d)
1069 ce_set_dtr (d->chan, 1);
1071 ce_set_rts (d->chan, 1);
1073 callout_reset (&d->timeout_handle, hz, ce_watchdog_timer, d);
1078 * Handle transmit timeouts.
1079 * Recover after lost transmit interrupts.
1080 * Always called on splimp().
1082 static void ce_watchdog (drv_t *d)
1084 CE_DEBUG (d, ("device timeout\n"));
1086 ce_set_dtr (d->chan, 0);
1087 ce_set_rts (d->chan, 0);
1088 /* ce_stop_chan (d->chan);*/
1089 /* ce_start_chan (d->chan, 1, 1, 0, 0);*/
1090 ce_set_dtr (d->chan, 1);
1091 ce_set_rts (d->chan, 1);
1096 static void ce_watchdog_timer (void *arg)
1099 bdrv_t *bd = d->board->sys;
1102 if (d->timeout == 1)
1106 callout_reset (&d->timeout_handle, hz, ce_watchdog_timer, d);
1110 static void ce_transmit (ce_chan_t *c, void *attachment, int len)
1116 ++d->ifp->if_opackets;
1117 #if __FreeBSD_version >= 600034
1118 d->ifp->if_flags &= ~IFF_DRV_OACTIVE;
1120 d->ifp->if_flags &= ~IFF_OACTIVE;
1126 static void ce_receive (ce_chan_t *c, unsigned char *data, int len)
1134 m = makembuf (data, len);
1136 CE_DEBUG (d, ("no memory for packet\n"));
1138 ++d->ifp->if_iqdrops;
1145 m->m_pkthdr.rcvif = 0;
1146 IF_ENQUEUE(&d->rqueue, m);
1148 ++d->ifp->if_ipackets;
1149 m->m_pkthdr.rcvif = d->ifp;
1150 /* Check if there's a BPF listener on this interface.
1151 * If so, hand off the raw packet to bpf. */
1152 #if __FreeBSD_version >= 500000
1153 BPF_TAP (d->ifp, data, len);
1156 bpf_tap (d->ifp, data, len);
1158 IF_ENQUEUE(&d->rqueue, m);
1162 static void ce_error (ce_chan_t *c, int data)
1168 CE_DEBUG (d, ("frame error\n"));
1170 ++d->ifp->if_ierrors;
1174 CE_DEBUG (d, ("crc error\n"));
1176 ++d->ifp->if_ierrors;
1180 CE_DEBUG (d, ("overrun error\n"));
1182 ++d->ifp->if_collisions;
1183 ++d->ifp->if_ierrors;
1187 CE_DEBUG (d, ("overflow error\n"));
1189 ++d->ifp->if_ierrors;
1193 CE_DEBUG (d, ("underrun error\n"));
1196 ++d->ifp->if_oerrors;
1197 #if __FreeBSD_version >= 600034
1198 d->ifp->if_flags &= ~IFF_DRV_OACTIVE;
1200 d->ifp->if_flags &= ~IFF_OACTIVE;
1206 CE_DEBUG (d, ("error #%d\n", data));
1212 * You also need read, write, open, close routines.
1213 * This should get you started
1215 #if __FreeBSD_version < 500000
1216 static int ce_open (dev_t dev, int oflags, int devtype, struct proc *p)
1218 static int ce_open (struct cdev *dev, int oflags, int devtype, struct thread *td)
1221 int unit = dev2unit (dev);
1224 if (unit >= NBRD*NCHAN || ! (d = channel[unit]))
1226 CE_DEBUG2 (d, ("ce_open\n"));
1231 * Only called on the LAST close.
1233 #if __FreeBSD_version < 500000
1234 static int ce_close (dev_t dev, int fflag, int devtype, struct proc *p)
1236 static int ce_close (struct cdev *dev, int fflag, int devtype, struct thread *td)
1239 drv_t *d = channel [dev2unit (dev)];
1241 CE_DEBUG2 (d, ("ce_close\n"));
1245 static int ce_modem_status (ce_chan_t *c)
1248 bdrv_t *bd = d->board->sys;
1251 status = d->running ? TIOCM_LE : 0;
1254 if (ce_get_cd (c)) status |= TIOCM_CD;
1255 if (ce_get_cts (c)) status |= TIOCM_CTS;
1256 if (ce_get_dsr (c)) status |= TIOCM_DSR;
1257 if (c->dtr) status |= TIOCM_DTR;
1258 if (c->rts) status |= TIOCM_RTS;
1264 #if __FreeBSD_version < 500000
1265 static int ce_ioctl (dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
1267 static int ce_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
1270 drv_t *d = channel [dev2unit (dev)];
1271 bdrv_t *bd = d->board->sys;
1272 ce_chan_t *c = d->chan;
1273 struct serial_statistics *st;
1274 struct e1_statistics *opte1;
1279 case SERIAL_GETREGISTERED:
1280 CE_DEBUG2 (d, ("ioctl: getregistered\n"));
1281 bzero (mask, sizeof(mask));
1282 for (s=0; s<NBRD*NCHAN; ++s)
1284 mask [s/8] |= 1 << (s & 7);
1285 bcopy (mask, data, sizeof (mask));
1289 case SERIAL_GETPROTO:
1290 CE_DEBUG2 (d, ("ioctl: getproto\n"));
1291 strcpy ((char*)data, (IFP2SP(d->ifp)->pp_flags & PP_FR) ? "fr" :
1292 (d->ifp->if_flags & PP_CISCO) ? "cisco" : "ppp");
1295 case SERIAL_SETPROTO:
1296 CE_DEBUG2 (d, ("ioctl: setproto\n"));
1297 /* Only for superuser! */
1298 #if __FreeBSD_version < 500000
1300 #elif __FreeBSD_version < 700000
1303 error = priv_check (td, PRIV_DRIVER);
1307 #if __FreeBSD_version >= 600034
1308 if (d->ifp->if_flags & IFF_DRV_RUNNING)
1310 if (d->ifp->if_flags & IFF_RUNNING)
1313 if (! strcmp ("cisco", (char*)data)) {
1314 IFP2SP(d->ifp)->pp_flags &= ~(PP_FR);
1315 IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE;
1316 d->ifp->if_flags |= PP_CISCO;
1318 } else if (! strcmp ("fr", (char*)data)) {
1319 d->ifp->if_flags &= ~(PP_CISCO);
1320 IFP2SP(d->ifp)->pp_flags |= PP_FR | PP_KEEPALIVE;
1322 } else if (! strcmp ("ppp", (char*)data)) {
1323 IFP2SP(d->ifp)->pp_flags &= ~PP_FR;
1324 IFP2SP(d->ifp)->pp_flags &= ~PP_KEEPALIVE;
1325 d->ifp->if_flags &= ~(PP_CISCO);
1330 case SERIAL_GETKEEPALIVE:
1331 CE_DEBUG2 (d, ("ioctl: getkeepalive\n"));
1332 if ((IFP2SP(d->ifp)->pp_flags & PP_FR) ||
1333 (d->ifp->if_flags & PP_CISCO))
1335 *(int*)data = (IFP2SP(d->ifp)->pp_flags & PP_KEEPALIVE) ? 1 : 0;
1338 case SERIAL_SETKEEPALIVE:
1339 CE_DEBUG2 (d, ("ioctl: setkeepalive\n"));
1340 /* Only for superuser! */
1341 #if __FreeBSD_version < 500000
1343 #elif __FreeBSD_version < 700000
1346 error = priv_check (td, PRIV_DRIVER);
1350 if ((IFP2SP(d->ifp)->pp_flags & PP_FR) ||
1351 (d->ifp->if_flags & PP_CISCO))
1356 IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE;
1358 IFP2SP(d->ifp)->pp_flags &= ~PP_KEEPALIVE;
1364 case SERIAL_GETMODE:
1365 CE_DEBUG2 (d, ("ioctl: getmode\n"));
1366 *(int*)data = SERIAL_HDLC;
1369 case SERIAL_SETMODE:
1370 /* Only for superuser! */
1371 #if __FreeBSD_version < 500000
1373 #elif __FreeBSD_version < 700000
1376 error = priv_check (td, PRIV_DRIVER);
1380 if (*(int*)data != SERIAL_HDLC)
1385 CE_DEBUG2 (d, ("ioctl: getcfg\n"));
1390 CE_DEBUG2 (d, ("ioctl: setcfg\n"));
1391 #if __FreeBSD_version < 500000
1393 #elif __FreeBSD_version < 700000
1396 error = priv_check (td, PRIV_DRIVER);
1400 if (*((char*)data) != 'c')
1404 case SERIAL_GETSTAT:
1405 CE_DEBUG2 (d, ("ioctl: getstat\n"));
1406 st = (struct serial_statistics*) data;
1407 st->rintr = c->rintr;
1408 st->tintr = c->tintr;
1410 st->ibytes = c->ibytes;
1411 st->ipkts = c->ipkts;
1412 st->obytes = c->obytes;
1413 st->opkts = c->opkts;
1414 st->ierrs = c->overrun + c->frame + c->crc;
1415 st->oerrs = c->underrun;
1418 case SERIAL_GETESTAT:
1419 CE_DEBUG2 (d, ("ioctl: getestat\n"));
1420 if (c->type != T_E1)
1422 opte1 = (struct e1_statistics*) data;
1425 if (c->status & ESTS_NOALARM)
1426 opte1->status |= E1_NOALARM;
1427 if (c->status & ESTS_LOS)
1428 opte1->status |= E1_LOS;
1429 if (c->status & ESTS_LOF)
1430 opte1->status |= E1_LOF;
1431 if (c->status & ESTS_AIS)
1432 opte1->status |= E1_AIS;
1433 if (c->status & ESTS_LOMF)
1434 opte1->status |= E1_LOMF;
1435 if (c->status & ESTS_AIS16)
1436 opte1->status |= E1_AIS16;
1437 if (c->status & ESTS_FARLOF)
1438 opte1->status |= E1_FARLOF;
1439 if (c->status & ESTS_FARLOMF)
1440 opte1->status |= E1_FARLOMF;
1441 if (c->status & ESTS_TSTREQ)
1442 opte1->status |= E1_TSTREQ;
1443 if (c->status & ESTS_TSTERR)
1444 opte1->status |= E1_TSTERR;
1446 opte1->cursec = c->cursec;
1447 opte1->totsec = c->totsec + c->cursec;
1449 opte1->currnt.bpv = c->currnt.bpv;
1450 opte1->currnt.fse = c->currnt.fse;
1451 opte1->currnt.crce = c->currnt.crce;
1452 opte1->currnt.rcrce = c->currnt.rcrce;
1453 opte1->currnt.uas = c->currnt.uas;
1454 opte1->currnt.les = c->currnt.les;
1455 opte1->currnt.es = c->currnt.es;
1456 opte1->currnt.bes = c->currnt.bes;
1457 opte1->currnt.ses = c->currnt.ses;
1458 opte1->currnt.oofs = c->currnt.oofs;
1459 opte1->currnt.css = c->currnt.css;
1460 opte1->currnt.dm = c->currnt.dm;
1462 opte1->total.bpv = c->total.bpv + c->currnt.bpv;
1463 opte1->total.fse = c->total.fse + c->currnt.fse;
1464 opte1->total.crce = c->total.crce + c->currnt.crce;
1465 opte1->total.rcrce = c->total.rcrce + c->currnt.rcrce;
1466 opte1->total.uas = c->total.uas + c->currnt.uas;
1467 opte1->total.les = c->total.les + c->currnt.les;
1468 opte1->total.es = c->total.es + c->currnt.es;
1469 opte1->total.bes = c->total.bes + c->currnt.bes;
1470 opte1->total.ses = c->total.ses + c->currnt.ses;
1471 opte1->total.oofs = c->total.oofs + c->currnt.oofs;
1472 opte1->total.css = c->total.css + c->currnt.css;
1473 opte1->total.dm = c->total.dm + c->currnt.dm;
1474 for (s=0; s<48; ++s) {
1475 opte1->interval[s].bpv = c->interval[s].bpv;
1476 opte1->interval[s].fse = c->interval[s].fse;
1477 opte1->interval[s].crce = c->interval[s].crce;
1478 opte1->interval[s].rcrce = c->interval[s].rcrce;
1479 opte1->interval[s].uas = c->interval[s].uas;
1480 opte1->interval[s].les = c->interval[s].les;
1481 opte1->interval[s].es = c->interval[s].es;
1482 opte1->interval[s].bes = c->interval[s].bes;
1483 opte1->interval[s].ses = c->interval[s].ses;
1484 opte1->interval[s].oofs = c->interval[s].oofs;
1485 opte1->interval[s].css = c->interval[s].css;
1486 opte1->interval[s].dm = c->interval[s].dm;
1490 case SERIAL_CLRSTAT:
1491 CE_DEBUG2 (d, ("ioctl: clrstat\n"));
1492 /* Only for superuser! */
1493 #if __FreeBSD_version < 500000
1495 #elif __FreeBSD_version < 700000
1498 error = priv_check (td, PRIV_DRIVER);
1512 bzero (&c->currnt, sizeof (c->currnt));
1513 bzero (&c->total, sizeof (c->total));
1514 bzero (c->interval, sizeof (c->interval));
1517 case SERIAL_GETLOOP:
1518 CE_DEBUG2 (d, ("ioctl: getloop\n"));
1519 if (c->type != T_E1)
1521 *(int*)data = c->lloop;
1524 case SERIAL_SETLOOP:
1525 CE_DEBUG2 (d, ("ioctl: setloop\n"));
1526 if (c->type != T_E1)
1528 /* Only for superuser! */
1529 #if __FreeBSD_version < 500000
1531 #elif __FreeBSD_version < 700000
1534 error = priv_check (td, PRIV_DRIVER);
1540 ce_set_lloop (c, *(int*)data);
1545 case SERIAL_GETRLOOP:
1546 CE_DEBUG2 (d, ("ioctl: getrloop\n"));
1547 if (c->type != T_E1)
1549 *(int*)data = c->rloop;
1552 case SERIAL_SETRLOOP:
1553 CE_DEBUG2 (d, ("ioctl: setloop\n"));
1554 if (c->type != T_E1)
1556 /* Only for superuser! */
1557 #if __FreeBSD_version < 500000
1559 #elif __FreeBSD_version < 700000
1562 error = priv_check (td, PRIV_DRIVER);
1568 ce_set_rloop (c, *(int*)data);
1573 case SERIAL_GETDEBUG:
1574 CE_DEBUG2 (d, ("ioctl: getdebug\n"));
1575 *(int*)data = d->chan->debug;
1578 case SERIAL_SETDEBUG:
1579 CE_DEBUG2 (d, ("ioctl: setdebug\n"));
1580 /* Only for superuser! */
1581 #if __FreeBSD_version < 500000
1583 #elif __FreeBSD_version < 700000
1586 error = priv_check (td, PRIV_DRIVER);
1592 * The debug_shadow is always greater than zero for logic
1593 * simplicity. For switching debug off the IFF_DEBUG is
1596 d->chan->debug_shadow = (*(int*)data) ? (*(int*)data) : 1;
1597 if (d->ifp->if_flags & IFF_DEBUG)
1598 d->chan->debug = d->chan->debug_shadow;
1600 d->chan->debug = *(int*)data;
1604 case SERIAL_GETBAUD:
1605 CE_DEBUG2 (d, ("ioctl: getbaud\n"));
1606 *(long*)data = c->baud;
1609 case SERIAL_SETBAUD:
1610 CE_DEBUG2 (d, ("ioctl: setbaud\n"));
1611 if (c->type != T_E1 || !c->unfram)
1613 /* Only for superuser! */
1614 #if __FreeBSD_version < 500000
1616 #elif __FreeBSD_version < 700000
1619 error = priv_check (td, PRIV_DRIVER);
1625 ce_set_baud (c, *(long*)data);
1630 case SERIAL_GETTIMESLOTS:
1631 CE_DEBUG2 (d, ("ioctl: gettimeslots\n"));
1632 if ((c->type != T_E1 || c->unfram) && c->type != T_DATA)
1634 *(u_long*)data = c->ts;
1637 case SERIAL_SETTIMESLOTS:
1638 CE_DEBUG2 (d, ("ioctl: settimeslots\n"));
1639 /* Only for superuser! */
1640 #if __FreeBSD_version < 500000
1642 #elif __FreeBSD_version < 700000
1645 error = priv_check (td, PRIV_DRIVER);
1649 if ((c->type != T_E1 || c->unfram) && c->type != T_DATA)
1653 ce_set_ts (c, *(u_long*)data);
1658 case SERIAL_GETHIGAIN:
1659 CE_DEBUG2 (d, ("ioctl: gethigain\n"));
1660 if (c->type != T_E1)
1662 *(int*)data = c->higain;
1665 case SERIAL_SETHIGAIN:
1666 CE_DEBUG2 (d, ("ioctl: sethigain\n"));
1667 if (c->type != T_E1)
1669 /* Only for superuser! */
1670 #if __FreeBSD_version < 500000
1672 #elif __FreeBSD_version < 700000
1675 error = priv_check (td, PRIV_DRIVER);
1681 ce_set_higain (c, *(int*)data);
1686 case SERIAL_GETPHONY:
1687 CE_DEBUG2 (d, ("ioctl: getphony\n"));
1688 *(int*)data = c->phony;
1691 case SERIAL_SETPHONY:
1692 CE_DEBUG2 (d, ("ioctl: setphony\n"));
1693 /* Only for superuser! */
1694 #if __FreeBSD_version < 500000
1696 #elif __FreeBSD_version < 700000
1699 error = priv_check (td, PRIV_DRIVER);
1705 ce_set_phony (c, *(int*)data);
1710 case SERIAL_GETUNFRAM:
1711 CE_DEBUG2 (d, ("ioctl: getunfram\n"));
1712 if (c->type != T_E1 || c->num != 0)
1714 *(int*)data = c->unfram;
1717 case SERIAL_SETUNFRAM:
1718 CE_DEBUG2 (d, ("ioctl: setunfram\n"));
1719 if (c->type != T_E1 || c->num != 0)
1721 /* Only for superuser! */
1722 #if __FreeBSD_version < 500000
1724 #elif __FreeBSD_version < 700000
1727 error = priv_check (td, PRIV_DRIVER);
1733 ce_set_unfram (c, *(int*)data);
1738 case SERIAL_GETSCRAMBLER:
1739 CE_DEBUG2 (d, ("ioctl: getscrambler\n"));
1742 *(int*)data = c->scrambler;
1745 case SERIAL_SETSCRAMBLER:
1746 CE_DEBUG2 (d, ("ioctl: setscrambler\n"));
1747 /* Only for superuser! */
1748 #if __FreeBSD_version < 500000
1750 #elif __FreeBSD_version < 700000
1753 error = priv_check (td, PRIV_DRIVER);
1761 ce_set_scrambler (c, *(int*)data);
1766 case SERIAL_GETMONITOR:
1767 CE_DEBUG2 (d, ("ioctl: getmonitor\n"));
1768 if (c->type != T_E1)
1770 *(int*)data = c->monitor;
1773 case SERIAL_SETMONITOR:
1774 CE_DEBUG2 (d, ("ioctl: setmonitor\n"));
1775 /* Only for superuser! */
1776 #if __FreeBSD_version < 500000
1778 #elif __FreeBSD_version < 700000
1781 error = priv_check (td, PRIV_DRIVER);
1785 if (c->type != T_E1)
1789 ce_set_monitor (c, *(int*)data);
1794 case SERIAL_GETUSE16:
1795 CE_DEBUG2 (d, ("ioctl: getuse16\n"));
1796 if (c->type != T_E1 || c->unfram)
1798 *(int*)data = c->use16;
1801 case SERIAL_SETUSE16:
1802 CE_DEBUG2 (d, ("ioctl: setuse16\n"));
1803 /* Only for superuser! */
1804 #if __FreeBSD_version < 500000
1806 #elif __FreeBSD_version < 700000
1809 error = priv_check (td, PRIV_DRIVER);
1813 if (c->type != T_E1)
1817 ce_set_use16 (c, *(int*)data);
1822 case SERIAL_GETCRC4:
1823 CE_DEBUG2 (d, ("ioctl: getcrc4\n"));
1824 if (c->type != T_E1 || c->unfram)
1826 *(int*)data = c->crc4;
1829 case SERIAL_SETCRC4:
1830 CE_DEBUG2 (d, ("ioctl: setcrc4\n"));
1831 /* Only for superuser! */
1832 #if __FreeBSD_version < 500000
1834 #elif __FreeBSD_version < 700000
1837 error = priv_check (td, PRIV_DRIVER);
1841 if (c->type != T_E1 || c->unfram)
1845 ce_set_crc4 (c, *(int*)data);
1851 CE_DEBUG2 (d, ("ioctl: getclk\n"));
1852 if (c->type != T_E1)
1855 default: *(int*)data = E1CLK_INTERNAL; break;
1856 case GSYN_RCV: *(int*)data = E1CLK_RECEIVE; break;
1857 case GSYN_RCV0: *(int*)data = E1CLK_RECEIVE_CHAN0; break;
1858 case GSYN_RCV1: *(int*)data = E1CLK_RECEIVE_CHAN1; break;
1863 CE_DEBUG2 (d, ("ioctl: setclk\n"));
1864 /* Only for superuser! */
1865 #if __FreeBSD_version < 500000
1867 #elif __FreeBSD_version < 700000
1870 error = priv_check (td, PRIV_DRIVER);
1874 if (c->type != T_E1)
1878 switch (*(int*)data) {
1879 default: ce_set_gsyn (c, GSYN_INT); break;
1880 case E1CLK_RECEIVE: ce_set_gsyn (c, GSYN_RCV); break;
1881 case E1CLK_RECEIVE_CHAN0: ce_set_gsyn (c, GSYN_RCV0); break;
1882 case E1CLK_RECEIVE_CHAN1: ce_set_gsyn (c, GSYN_RCV1); break;
1890 CE_DEBUG2 (d, ("ioctl: reset\n"));
1891 /* Only for superuser! */
1892 #if __FreeBSD_version < 500000
1894 #elif __FreeBSD_version < 700000
1897 error = priv_check (td, PRIV_DRIVER);
1903 /* ce_reset (c->board, 0, 0);*/
1908 case SERIAL_HARDRESET:
1909 CE_DEBUG2 (d, ("ioctl: hardreset\n"));
1910 /* Only for superuser! */
1911 #if __FreeBSD_version < 500000
1913 #elif __FreeBSD_version < 700000
1916 error = priv_check (td, PRIV_DRIVER);
1922 /* hard_reset (c->board); */
1928 case SERIAL_GETCABLE:
1929 CE_DEBUG2 (d, ("ioctl: getcable\n"));
1930 if (c->type != T_E1)
1934 *(int*)data = CABLE_TP;
1940 CE_DEBUG2 (d, ("ioctl: getdir\n"));
1941 if (c->type != T_E1 && c->type != T_DATA)
1943 *(int*)data = c->dir;
1947 CE_DEBUG2 (d, ("ioctl: setdir\n"));
1948 /* Only for superuser! */
1949 #if __FreeBSD_version < 500000
1951 #elif __FreeBSD_version < 700000
1954 error = priv_check (td, PRIV_DRIVER);
1960 ce_set_dir (c, *(int*)data);
1965 case TIOCSDTR: /* Set DTR */
1973 case TIOCCDTR: /* Clear DTR */
1981 case TIOCMSET: /* Set DTR/RTS */
1984 ce_set_dtr (c, (*(int*)data & TIOCM_DTR) ? 1 : 0);
1985 ce_set_rts (c, (*(int*)data & TIOCM_RTS) ? 1 : 0);
1990 case TIOCMBIS: /* Add DTR/RTS */
1993 if (*(int*)data & TIOCM_DTR) ce_set_dtr (c, 1);
1994 if (*(int*)data & TIOCM_RTS) ce_set_rts (c, 1);
1999 case TIOCMBIC: /* Clear DTR/RTS */
2002 if (*(int*)data & TIOCM_DTR) ce_set_dtr (c, 0);
2003 if (*(int*)data & TIOCM_RTS) ce_set_rts (c, 0);
2008 case TIOCMGET: /* Get modem status */
2009 *(int*)data = ce_modem_status (c);
2016 #if __FreeBSD_version >= 500000
2017 static int ng_ce_constructor (node_p node)
2019 drv_t *d = NG_NODE_PRIVATE (node);
2021 static int ng_ce_constructor (node_p *node)
2023 drv_t *d = (*node)->private;
2025 CE_DEBUG (d, ("Constructor\n"));
2029 static int ng_ce_newhook (node_p node, hook_p hook, const char *name)
2032 #if __FreeBSD_version >= 500000
2033 drv_t *d = NG_NODE_PRIVATE (node);
2035 drv_t *d = node->private;
2037 bdrv_t *bd = d->board->sys;
2039 CE_DEBUG (d, ("Newhook\n"));
2040 /* Attach debug hook */
2041 if (strcmp (name, NG_CE_HOOK_DEBUG) == 0) {
2042 #if __FreeBSD_version >= 500000
2043 NG_HOOK_SET_PRIVATE (hook, NULL);
2047 d->debug_hook = hook;
2051 /* Check for raw hook */
2052 if (strcmp (name, NG_CE_HOOK_RAW) != 0)
2055 #if __FreeBSD_version >= 500000
2056 NG_HOOK_SET_PRIVATE (hook, d);
2069 static char *format_timeslots (u_long s)
2071 static char buf [100];
2075 for (i=1; i<32; ++i)
2077 int prev = (i > 1) & (s >> (i-1));
2078 int next = (i < 31) & (s >> (i+1));
2088 *p++ = '0' + i / 10;
2089 *p++ = '0' + i % 10;
2095 static int print_modems (char *s, ce_chan_t *c, int need_header)
2097 int status = ce_modem_status (c);
2101 length += sprintf (s + length, " LE DTR DSR RTS CTS CD\n");
2102 length += sprintf (s + length, "%4s %4s %4s %4s %4s %4s\n",
2103 status & TIOCM_LE ? "On" : "-",
2104 status & TIOCM_DTR ? "On" : "-",
2105 status & TIOCM_DSR ? "On" : "-",
2106 status & TIOCM_RTS ? "On" : "-",
2107 status & TIOCM_CTS ? "On" : "-",
2108 status & TIOCM_CD ? "On" : "-");
2112 static int print_stats (char *s, ce_chan_t *c, int need_header)
2117 length += sprintf (s + length, " Rintr Tintr Mintr Ibytes Ipkts Ierrs Obytes Opkts Oerrs\n");
2118 length += sprintf (s + length, "%7ld %7ld %7ld %8lu %7ld %7ld %8lu %7ld %7ld\n",
2119 c->rintr, c->tintr, 0l, (unsigned long) c->ibytes,
2120 c->ipkts, c->overrun + c->frame + c->crc,
2121 (unsigned long) c->obytes, c->opkts, c->underrun);
2125 static char *format_e1_status (u_char status)
2127 static char buf [80];
2129 if (status & E1_NOALARM)
2132 if (status & E1_LOS) strcat (buf, ",LOS");
2133 if (status & E1_AIS) strcat (buf, ",AIS");
2134 if (status & E1_LOF) strcat (buf, ",LOF");
2135 if (status & E1_LOMF) strcat (buf, ",LOMF");
2136 if (status & E1_FARLOF) strcat (buf, ",FARLOF");
2137 if (status & E1_AIS16) strcat (buf, ",AIS16");
2138 if (status & E1_FARLOMF) strcat (buf, ",FARLOMF");
2139 if (status & E1_TSTREQ) strcat (buf, ",TSTREQ");
2140 if (status & E1_TSTERR) strcat (buf, ",TSTERR");
2146 static int print_frac (char *s, int leftalign, u_long numerator, u_long divider)
2150 if (numerator < 1 || divider < 1) {
2151 length += sprintf (s+length, leftalign ? "/- " : " -");
2154 n = (int) (0.5 + 1000.0 * numerator / divider);
2156 length += sprintf (s+length, leftalign ? "/.%-3d" : " .%03d", n);
2159 *(s + length) = leftalign ? '/' : ' ';
2162 if (n >= 1000000) n = (n+500) / 1000 * 1000;
2163 else if (n >= 100000) n = (n+50) / 100 * 100;
2164 else if (n >= 10000) n = (n+5) / 10 * 10;
2167 case 1000: length += printf (s+length, ".999"); return length;
2168 case 10000: n = 9990; break;
2169 case 100000: n = 99900; break;
2170 case 1000000: n = 999000; break;
2172 if (n < 10000) length += sprintf (s+length, "%d.%d", n/1000, n/10%100);
2173 else if (n < 100000) length += sprintf (s+length, "%d.%d", n/1000, n/100%10);
2174 else if (n < 1000000) length += sprintf (s+length, "%d.", n/1000);
2175 else length += sprintf (s+length, "%d", n/1000);
2180 static int print_e1_stats (char *s, ce_chan_t *c)
2182 struct e1_counters total;
2186 totsec = c->totsec + c->cursec;
2187 total.bpv = c->total.bpv + c->currnt.bpv;
2188 total.fse = c->total.fse + c->currnt.fse;
2189 total.crce = c->total.crce + c->currnt.crce;
2190 total.rcrce = c->total.rcrce + c->currnt.rcrce;
2191 total.uas = c->total.uas + c->currnt.uas;
2192 total.les = c->total.les + c->currnt.les;
2193 total.es = c->total.es + c->currnt.es;
2194 total.bes = c->total.bes + c->currnt.bes;
2195 total.ses = c->total.ses + c->currnt.ses;
2196 total.oofs = c->total.oofs + c->currnt.oofs;
2197 total.css = c->total.css + c->currnt.css;
2198 total.dm = c->total.dm + c->currnt.dm;
2200 length += sprintf (s + length, " Unav/Degr Bpv/Fsyn CRC/RCRC Err/Lerr Sev/Bur Oof/Slp Status\n");
2202 /* Unavailable seconds, degraded minutes */
2203 length += print_frac (s + length, 0, c->currnt.uas, c->cursec);
2204 length += print_frac (s + length, 1, 60 * c->currnt.dm, c->cursec);
2206 /* Bipolar violations, frame sync errors */
2207 length += print_frac (s + length, 0, c->currnt.bpv, c->cursec);
2208 length += print_frac (s + length, 1, c->currnt.fse, c->cursec);
2210 /* CRC errors, remote CRC errors (E-bit) */
2211 length += print_frac (s + length, 0, c->currnt.crce, c->cursec);
2212 length += print_frac (s + length, 1, c->currnt.rcrce, c->cursec);
2214 /* Errored seconds, line errored seconds */
2215 length += print_frac (s + length, 0, c->currnt.es, c->cursec);
2216 length += print_frac (s + length, 1, c->currnt.les, c->cursec);
2218 /* Severely errored seconds, burst errored seconds */
2219 length += print_frac (s + length, 0, c->currnt.ses, c->cursec);
2220 length += print_frac (s + length, 1, c->currnt.bes, c->cursec);
2222 /* Out of frame seconds, controlled slip seconds */
2223 length += print_frac (s + length, 0, c->currnt.oofs, c->cursec);
2224 length += print_frac (s + length, 1, c->currnt.css, c->cursec);
2226 length += sprintf (s + length, " %s\n", format_e1_status (c->status));
2228 /* Print total statistics. */
2229 length += print_frac (s + length, 0, total.uas, totsec);
2230 length += print_frac (s + length, 1, 60 * total.dm, totsec);
2232 length += print_frac (s + length, 0, total.bpv, totsec);
2233 length += print_frac (s + length, 1, total.fse, totsec);
2235 length += print_frac (s + length, 0, total.crce, totsec);
2236 length += print_frac (s + length, 1, total.rcrce, totsec);
2238 length += print_frac (s + length, 0, total.es, totsec);
2239 length += print_frac (s + length, 1, total.les, totsec);
2241 length += print_frac (s + length, 0, total.ses, totsec);
2242 length += print_frac (s + length, 1, total.bes, totsec);
2244 length += print_frac (s + length, 0, total.oofs, totsec);
2245 length += print_frac (s + length, 1, total.css, totsec);
2247 length += sprintf (s + length, " -- Total\n");
2251 static int print_chan (char *s, ce_chan_t *c)
2256 length += sprintf (s + length, "ce%d", c->board->num * NCHAN + c->num);
2258 length += sprintf (s + length, " debug=%d", d->chan->debug);
2260 if (c->board->mux) {
2261 length += sprintf (s + length, " cfg=C");
2263 length += sprintf (s + length, " cfg=A");
2267 length += sprintf (s + length, " %ld", c->baud);
2269 length += sprintf (s + length, " extclock");
2271 if (c->type == T_E1)
2273 case GSYN_INT : length += sprintf (s + length, " syn=int"); break;
2274 case GSYN_RCV : length += sprintf (s + length, " syn=rcv"); break;
2275 case GSYN_RCV0 : length += sprintf (s + length, " syn=rcv0"); break;
2276 case GSYN_RCV1 : length += sprintf (s + length, " syn=rcv1"); break;
2278 if (c->type == T_E1)
2279 length += sprintf (s + length, " higain=%s", c->higain ? "on" : "off");
2281 length += sprintf (s + length, " loop=%s", c->lloop ? "on" : "off");
2283 if (c->type == T_E1)
2284 length += sprintf (s + length, " ts=%s", format_timeslots (c->ts));
2285 length += sprintf (s + length, "\n");
2289 #if __FreeBSD_version >= 500000
2290 static int ng_ce_rcvmsg (node_p node, item_p item, hook_p lasthook)
2292 drv_t *d = NG_NODE_PRIVATE (node);
2293 struct ng_mesg *msg;
2295 static int ng_ce_rcvmsg (node_p node, struct ng_mesg *msg,
2296 const char *retaddr, struct ng_mesg **rptr)
2298 drv_t *d = node->private;
2300 struct ng_mesg *resp = NULL;
2303 CE_DEBUG (d, ("Rcvmsg\n"));
2304 #if __FreeBSD_version >= 500000
2305 NGI_GET_MSG (item, msg);
2307 switch (msg->header.typecookie) {
2313 printf ("Not implemented yet\n");
2317 case NGM_GENERIC_COOKIE:
2318 switch (msg->header.cmd) {
2323 case NGM_TEXT_STATUS: {
2326 int dl = sizeof (struct ng_mesg) + 730;
2328 #if __FreeBSD_version >= 500000
2329 NG_MKRESPONSE (resp, msg, dl, M_NOWAIT);
2335 resp = malloc (M_NETGRAPH, M_NOWAIT);
2344 l += print_chan (s + l, d->chan);
2345 l += print_stats (s + l, d->chan, 1);
2346 l += print_modems (s + l, d->chan, 1);
2347 l += print_e1_stats (s + l, d->chan);
2349 l += sprintf (s + l, "Error: node not connect to channel");
2350 #if __FreeBSD_version < 500000
2351 (resp)->header.version = NG_VERSION;
2352 (resp)->header.arglen = strlen (s) + 1;
2353 (resp)->header.token = msg->header.token;
2354 (resp)->header.typecookie = NGM_CE_COOKIE;
2355 (resp)->header.cmd = msg->header.cmd;
2357 strncpy ((resp)->header.cmdstr, "status", NG_CMDSTRSIZ);
2363 #if __FreeBSD_version >= 500000
2364 NG_RESPOND_MSG (error, node, item, resp);
2368 free (msg, M_NETGRAPH);
2373 #if __FreeBSD_version >= 500000
2374 static int ng_ce_rcvdata (hook_p hook, item_p item)
2376 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE(hook));
2378 #if __FreeBSD_version < 502120
2381 struct ng_tag_prio *ptag;
2384 static int ng_ce_rcvdata (hook_p hook, struct mbuf *m, meta_p meta)
2386 drv_t *d = hook->node->private;
2388 bdrv_t *bd = d->board->sys;
2392 CE_DEBUG2 (d, ("Rcvdata\n"));
2393 #if __FreeBSD_version >= 500000
2394 NGI_GET_M (item, m);
2395 #if __FreeBSD_version < 502120
2396 NGI_GET_META (item, meta);
2398 NG_FREE_ITEM (item);
2399 if (! NG_HOOK_PRIVATE (hook) || ! d) {
2401 #if __FreeBSD_version < 502120
2402 NG_FREE_META (meta);
2405 if (! hook->private || ! d) {
2406 NG_FREE_DATA (m,meta);
2411 #if __FreeBSD_version >= 502120
2412 /* Check for high priority data */
2413 if ((ptag = (struct ng_tag_prio *)m_tag_locate(m, NGM_GENERIC_COOKIE,
2414 NG_TAG_PRIO, NULL)) != NULL && (ptag->priority > NG_PRIO_CUTOFF) )
2419 q = (meta && meta->priority > 0) ? &d->hi_queue : &d->queue;
2424 #if __FreeBSD_version >= 500000
2426 if (_IF_QFULL (q)) {
2432 #if __FreeBSD_version < 502120
2433 NG_FREE_META (meta);
2444 NG_FREE_DATA (m, meta);
2455 static int ng_ce_rmnode (node_p node)
2457 #if __FreeBSD_version >= 500000
2458 drv_t *d = NG_NODE_PRIVATE (node);
2460 CE_DEBUG (d, ("Rmnode\n"));
2461 if (d && d->running) {
2462 bdrv_t *bd = d->board->sys;
2470 #if __FreeBSD_version >= 502120
2471 if (node->nd_flags & NGF_REALLY_DIE) {
2473 if (node->nd_flags & NG_REALLY_DIE) {
2475 NG_NODE_SET_PRIVATE (node, NULL);
2476 NG_NODE_UNREF (node);
2478 #if __FreeBSD_version >= 502120
2479 NG_NODE_REVIVE(node); /* Persistant node */
2481 node->nd_flags &= ~NG_INVALID;
2484 #else /* __FreeBSD_version < 500000 */
2485 drv_t *d = node->private;
2487 if (d && d->running) {
2488 bdrv_t *bd = d->board->sys;
2496 node->flags |= NG_INVALID;
2506 static int ng_ce_connect (hook_p hook)
2508 #if __FreeBSD_version >= 500000
2509 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2511 drv_t *d = hook->node->private;
2515 CE_DEBUG (d, ("Connect\n"));
2516 callout_reset (&d->timeout_handle, hz, ce_watchdog_timer, d);
2522 static int ng_ce_disconnect (hook_p hook)
2524 #if __FreeBSD_version >= 500000
2525 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2527 drv_t *d = hook->node->private;
2531 CE_DEBUG (d, ("Disconnect\n"));
2532 #if __FreeBSD_version >= 500000
2533 if (NG_HOOK_PRIVATE (hook))
2538 bdrv_t *bd = d->board->sys;
2545 /* If we were wait it than it reasserted now, just stop it. */
2546 if (!callout_drain (&d->timeout_handle))
2547 callout_stop (&d->timeout_handle);
2553 static int ce_modevent (module_t mod, int type, void *unused)
2555 #if __FreeBSD_version < 500000
2557 struct cdevsw *cdsw;
2559 static int load_count = 0;
2561 #if __FreeBSD_version < 500000
2562 dev = makedev (CDEV_MAJOR, 0);
2567 #if __FreeBSD_version < 500000
2569 (cdsw = devsw (dev)) &&
2570 cdsw->d_maj == CDEV_MAJOR) {
2571 printf ("Tau32-PCI driver is already in system\n");
2575 #if __FreeBSD_version >= 500000 && defined NETGRAPH
2576 if (ng_newtype (&typestruct))
2577 printf ("Failed to register ng_ce\n");
2580 #if __FreeBSD_version <= 500000
2581 cdevsw_add (&ce_cdevsw);
2583 #if __FreeBSD_version >= 500000
2584 callout_init (&timeout_handle, CALLOUT_MPSAFE);
2586 callout_init (&timeout_handle);
2588 callout_reset (&timeout_handle, hz*5, ce_timeout, 0);
2591 if (load_count == 1) {
2592 printf ("Removing device entry for Tau32-PCI\n");
2593 #if __FreeBSD_version <= 500000
2594 cdevsw_remove (&ce_cdevsw);
2596 #if __FreeBSD_version >= 500000 && defined NETGRAPH
2597 ng_rmtype (&typestruct);
2600 /* If we were wait it than it reasserted now, just stop it.
2601 * Actually we shouldn't get this condition. But code could be
2602 * changed in the future, so just be a litle paranoid.
2604 if (!callout_drain (&timeout_handle))
2605 callout_stop (&timeout_handle);
2615 #if __FreeBSD_version >= 502100
2616 static struct ng_type typestruct = {
2617 .version = NG_ABI_VERSION,
2618 .name = NG_CE_NODE_TYPE,
2619 .constructor = ng_ce_constructor,
2620 .rcvmsg = ng_ce_rcvmsg,
2621 .shutdown = ng_ce_rmnode,
2622 .newhook = ng_ce_newhook,
2623 .connect = ng_ce_connect,
2624 .rcvdata = ng_ce_rcvdata,
2625 .disconnect = ng_ce_disconnect,
2627 #else /* __FreeBSD_version < 502100 */
2628 static struct ng_type typestruct = {
2629 #if __FreeBSD_version >= 500000
2643 #if __FreeBSD_version < 500000
2649 #endif /* __FreeBSD_version < 502100 */
2653 #if __FreeBSD_version >= 500000
2655 MODULE_DEPEND (ng_ce, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION);
2657 MODULE_DEPEND (ce, sppp, 1, 1, 1);
2660 DRIVER_MODULE (cemod, pci, ce_driver, ce_devclass, ce_modevent, NULL);
2662 DRIVER_MODULE (ce, pci, ce_driver, ce_devclass, ce_modevent, NULL);
2664 #else /* if __FreeBSD_version < 500000*/
2666 DRIVER_MODULE (ce, pci, ce_driver, ce_devclass, ng_mod_event, &typestruct);
2668 DRIVER_MODULE (ce, pci, ce_driver, ce_devclass, ce_modevent, NULL);
2670 #endif /* __FreeBSD_version < 500000 */