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 #if __FreeBSD_version > 501000
51 # include <dev/pci/pcivar.h>
52 # include <dev/pci/pcireg.h>
54 # include <pci/pcivar.h>
55 # include <pci/pcireg.h>
57 #include <machine/bus.h>
59 #include "opt_ng_cronyx.h"
60 #ifdef NETGRAPH_CRONYX
61 # include "opt_netgraph.h"
63 # error #option NETGRAPH missed from configuration
65 # include <netgraph/ng_message.h>
66 # include <netgraph/netgraph.h>
67 # include <dev/ce/ng_ce.h>
69 # include <net/if_types.h>
70 # include <net/if_sppp.h>
71 # define PP_CISCO IFF_LINK2
74 #include <dev/cx/machdep.h>
75 #include <dev/ce/ceddk.h>
76 #include <machine/cserial.h>
77 #include <machine/resource.h>
78 #include <machine/pmap.h>
80 /* If we don't have Cronyx's sppp version, we don't have fr support via sppp */
86 #define IFP2SP(ifp) ((struct sppp*)ifp)
89 #define SP2IFP(sp) ((struct ifnet*)sp)
93 #define PCIR_BAR(x) (PCIR_MAPS + (x) * 4)
96 /* define as our previous return value */
97 #ifndef BUS_PROBE_DEFAULT
98 #define BUS_PROBE_DEFAULT 0
101 #define CE_DEBUG(d,s) ({if (d->chan->debug) {\
102 printf ("%s: ", d->name); printf s;}})
103 #define CE_DEBUG2(d,s) ({if (d->chan->debug>1) {\
104 printf ("%s: ", d->name); printf s;}})
106 #ifndef CALLOUT_MPSAFE
107 #define CALLOUT_MPSAFE 0
111 #define IF_DRAIN(ifq) do { \
114 IF_DEQUEUE(ifq, m); \
123 #define _IF_QLEN(ifq) ((ifq)->ifq_len)
126 #ifndef callout_drain
127 #define callout_drain callout_stop
130 #define CE_LOCK_NAME "ceX"
132 #define CE_LOCK(_bd) mtx_lock (&(_bd)->ce_mtx)
133 #define CE_UNLOCK(_bd) mtx_unlock (&(_bd)->ce_mtx)
134 #define CE_LOCK_ASSERT(_bd) mtx_assert (&(_bd)->ce_mtx, MA_OWNED)
136 #define CDEV_MAJOR 185
138 static int ce_probe __P((device_t));
139 static int ce_attach __P((device_t));
140 static int ce_detach __P((device_t));
142 static device_method_t ce_methods[] = {
143 /* Device interface */
144 DEVMETHOD(device_probe, ce_probe),
145 DEVMETHOD(device_attach, ce_attach),
146 DEVMETHOD(device_detach, ce_detach),
151 typedef struct _ce_dma_mem_t {
155 #if __FreeBSD_version >= 500000
161 typedef struct _drv_t {
166 struct ifqueue rqueue;
168 char nodename [NG_NODESIZE];
172 struct ifqueue queue;
173 struct ifqueue hi_queue;
175 struct callout timeout_handle;
179 #if __FreeBSD_version >= 500000
181 #else /* __FreeBSD_version < 500000 */
187 typedef struct _bdrv_t {
189 struct resource *ce_res;
190 struct resource *ce_irq;
193 drv_t channel [NCHAN];
194 #if __FreeBSD_version >= 504000
199 static driver_t ce_driver = {
205 static devclass_t ce_devclass;
207 static void ce_receive (ce_chan_t *c, unsigned char *data, int len);
208 static void ce_transmit (ce_chan_t *c, void *attachment, int len);
209 static void ce_error (ce_chan_t *c, int data);
210 static void ce_up (drv_t *d);
211 static void ce_start (drv_t *d);
212 static void ce_down (drv_t *d);
213 static void ce_watchdog (drv_t *d);
215 extern struct ng_type typestruct;
217 static void ce_ifstart (struct ifnet *ifp);
218 static void ce_tlf (struct sppp *sp);
219 static void ce_tls (struct sppp *sp);
220 static void ce_ifwatchdog (struct ifnet *ifp);
221 static int ce_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data);
222 static void ce_initialize (void *softc);
225 static ce_board_t *adapter [NBRD];
226 static drv_t *channel [NBRD*NCHAN];
227 static struct callout led_timo [NBRD];
228 static struct callout timeout_handle;
230 static int ce_destroy = 0;
232 #if __FreeBSD_version < 500000
233 static int ce_open (dev_t dev, int oflags, int devtype, struct proc *p);
234 static int ce_close (dev_t dev, int fflag, int devtype, struct proc *p);
235 static int ce_ioctl (dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p);
237 static int ce_open (struct cdev *dev, int oflags, int devtype, struct thread *td);
238 static int ce_close (struct cdev *dev, int fflag, int devtype, struct thread *td);
239 static int ce_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td);
241 #if __FreeBSD_version < 500000
242 static struct cdevsw ce_cdevsw = {
243 ce_open, ce_close, noread, nowrite,
244 ce_ioctl, nopoll, nommap, nostrategy,
245 "ce", CDEV_MAJOR, nodump, nopsize,
248 #elif __FreeBSD_version == 500000
249 static struct cdevsw ce_cdevsw = {
250 ce_open, ce_close, noread, nowrite,
251 ce_ioctl, nopoll, nommap, nostrategy,
252 "ce", CDEV_MAJOR, nodump, nopsize,
255 #elif __FreeBSD_version <= 501000
256 static struct cdevsw ce_cdevsw = {
264 .d_strategy = nostrategy,
270 #elif __FreeBSD_version < 502103
271 static struct cdevsw ce_cdevsw = {
279 #elif __FreeBSD_version < 600000
280 static struct cdevsw ce_cdevsw = {
281 .d_version = D_VERSION,
287 .d_flags = D_NEEDGIANT,
289 #else /* __FreeBSD_version >= 600000 */
290 static struct cdevsw ce_cdevsw = {
291 .d_version = D_VERSION,
300 * Print the mbuf chain, for debug purposes only.
302 static void printmbuf (struct mbuf *m)
305 for (; m; m=m->m_next) {
306 if (m->m_flags & M_PKTHDR)
307 printf (" HDR %d:", m->m_pkthdr.len);
308 if (m->m_flags & M_EXT)
310 printf (" %d", m->m_len);
316 * Make an mbuf from data.
318 static struct mbuf *makembuf (void *buf, unsigned len)
322 MGETHDR (m, M_DONTWAIT, MT_DATA);
325 MCLGET (m, M_DONTWAIT);
326 if (! (m->m_flags & M_EXT)) {
330 m->m_pkthdr.len = m->m_len = len;
331 bcopy (buf, mtod (m, caddr_t), len);
335 static int ce_probe (device_t dev)
337 if ((pci_get_vendor (dev) == TAU32_PCI_VENDOR_ID) &&
338 (pci_get_device (dev) == TAU32_PCI_DEVICE_ID)) {
339 device_set_desc (dev, "Cronyx-Tau32-PCI serial adapter");
340 return BUS_PROBE_DEFAULT;
345 static void ce_timeout (void *arg)
350 for (i = 0; i < NBRD; ++i) {
351 if (adapter[i] == NULL)
353 for (k = 0; k < NCHAN; ++k) {
359 d = channel[i * NCHAN + k];
364 CE_LOCK ((bdrv_t *)d->board->sys);
365 switch (d->chan->type) {
367 ce_e1_timer (d->chan);
372 CE_UNLOCK ((bdrv_t *)d->board->sys);
378 callout_reset (&timeout_handle, hz, ce_timeout, 0);
382 static void ce_led_off (void *arg)
385 bdrv_t *bd = (bdrv_t *) b->sys;
393 TAU32_LedSet (b->ddk.pControllerObject, 0);
398 static void ce_intr (void *arg)
401 ce_board_t *b = bd->board;
404 #if __FreeBSD_version >= 500000 && defined NETGRAPH
414 TAU32_LedSet (b->ddk.pControllerObject, 1);
416 TAU32_HandleInterrupt (b->ddk.pControllerObject);
418 /* Turn LED off 50 msec later. */
419 callout_reset (&led_timo[b->num], hz/20, ce_led_off, b);
423 /* Pass packets in a lock-free state */
424 for (i = 0; i < NCHAN && b->chan[i].type; i++) {
425 drv_t *d = b->chan[i].sys;
427 if (!d || !d->running)
429 while (_IF_QLEN(&d->rqueue)) {
430 IF_DEQUEUE (&d->rqueue,m);
435 #if __FreeBSD_version >= 500000
436 NG_SEND_DATA_ONLY (error, d->hook, m);
438 ng_queue_data (d->hook, m, 0);
441 IF_DRAIN (&d->rqueue);
444 sppp_input (d->ifp, m);
450 #if __FreeBSD_version >= 500000
452 ce_bus_dmamap_addr (void *arg, bus_dma_segment_t *segs, int nseg, int error)
459 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
461 *addr = segs->ds_addr;
465 #define BUS_DMA_ZERO 0
469 ce_bus_dma_mem_alloc (int bnum, int cnum, ce_dma_mem_t *dmem)
473 error = bus_dma_tag_create (NULL, 16, 0, BUS_SPACE_MAXADDR_32BIT,
474 BUS_SPACE_MAXADDR, NULL, NULL, dmem->size, 1,
476 #if __FreeBSD_version >= 502000
481 if (cnum >= 0) printf ("ce%d-%d: ", bnum, cnum);
482 else printf ("ce%d: ", bnum);
483 printf ("couldn't allocate tag for dma memory\n");
486 error = bus_dmamem_alloc (dmem->dmat, (void **)&dmem->virt,
487 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &dmem->mapp);
489 if (cnum >= 0) printf ("ce%d-%d: ", bnum, cnum);
490 else printf ("ce%d: ", bnum);
491 printf ("couldn't allocate mem for dma memory\n");
492 bus_dma_tag_destroy (dmem->dmat);
495 error = bus_dmamap_load (dmem->dmat, dmem->mapp, dmem->virt,
496 dmem->size, ce_bus_dmamap_addr, &dmem->phys, 0);
498 if (cnum >= 0) printf ("ce%d-%d: ", bnum, cnum);
499 else printf ("ce%d: ", bnum);
500 printf ("couldn't load mem map for dma memory\n");
501 bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
502 bus_dma_tag_destroy (dmem->dmat);
505 #if __FreeBSD_version >= 502000
506 bzero (dmem->virt, dmem->size);
512 ce_bus_dma_mem_free (ce_dma_mem_t *dmem)
514 bus_dmamap_unload (dmem->dmat, dmem->mapp);
515 bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
516 bus_dma_tag_destroy (dmem->dmat);
520 ce_bus_dma_mem_alloc (int bnum, int cnum, ce_dma_mem_t *dmem)
522 dmem->virt = contigmalloc (dmem->size, M_DEVBUF, M_WAITOK,
523 0x100000, 0xffffffff, 16, 0);
524 if (dmem->virt == NULL) {
525 if (cnum >= 0) printf ("ce%d-%d: ", bnum, cnum);
526 else printf ("ce%d: ", bnum);
527 printf ("couldn't allocate dma memory\n");
530 dmem->phys = vtophys (dmem->virt);
531 bzero (dmem->virt, dmem->size);
536 ce_bus_dma_mem_free (ce_dma_mem_t *dmem)
538 contigfree (dmem->virt, dmem->size, M_DEVBUF);
543 * Called if the probe succeeded.
545 static int ce_attach (device_t dev)
547 bdrv_t *bd = device_get_softc (dev);
548 int unit = device_get_unit (dev);
549 #if __FreeBSD_version >= 504000
550 char *ce_ln = CE_LOCK_NAME;
559 b = malloc (sizeof(ce_board_t), M_DEVBUF, M_WAITOK);
561 printf ("ce%d: couldn't allocate memory\n", unit);
564 bzero (b, sizeof(ce_board_t));
568 #if __FreeBSD_version >= 440000
569 pci_enable_busmaster (dev);
572 bd->dmamem.size = TAU32_ControllerObjectSize;
573 if (! ce_bus_dma_mem_alloc (unit, -1, &bd->dmamem)) {
577 b->ddk.pControllerObject = bd->dmamem.virt;
582 bd->ce_res = bus_alloc_resource (dev, SYS_RES_MEMORY, &rid,
583 0, ~0, 1, RF_ACTIVE);
585 printf ("ce%d: cannot map memory\n", unit);
586 ce_bus_dma_mem_free (&bd->dmamem);
590 vbase = (vm_offset_t) rman_get_virtual (bd->ce_res);
592 b->ddk.PciBar1VirtualAddress = (void *)vbase;
593 b->ddk.ControllerObjectPhysicalAddress = bd->dmamem.phys;
594 b->ddk.pErrorNotifyCallback = ce_error_callback;
595 b->ddk.pStatusNotifyCallback = ce_status_callback;
598 TAU32_BeforeReset(&b->ddk);
599 pci_write_config (dev, TAU32_PCI_RESET_ADDRESS, TAU32_PCI_RESET_ON, 4);
600 pci_write_config (dev, TAU32_PCI_RESET_ADDRESS, TAU32_PCI_RESET_OFF, 4);
602 if(!TAU32_Initialize(&b->ddk, 0))
604 printf ("ce%d: init adapter error 0x%08x, bus dead bits 0x%08lx\n",
605 unit, b->ddk.InitErrors, b->ddk.DeadBits);
606 bus_release_resource (dev, SYS_RES_MEMORY, PCIR_BAR(0), bd->ce_res);
607 ce_bus_dma_mem_free (&bd->dmamem);
617 bd->ce_irq = bus_alloc_resource (dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
618 RF_SHAREABLE | RF_ACTIVE);
620 printf ("ce%d: cannot map interrupt\n", unit);
621 bus_release_resource (dev, SYS_RES_MEMORY, PCIR_BAR(0), bd->ce_res);
622 ce_bus_dma_mem_free (&bd->dmamem);
627 #if __FreeBSD_version >= 500000
628 callout_init (&led_timo[unit], CALLOUT_MPSAFE);
630 callout_init (&led_timo[unit]);
632 error = bus_setup_intr (dev, bd->ce_irq,
633 #if __FreeBSD_version >= 500013
634 INTR_TYPE_NET|INTR_MPSAFE,
638 NULL, ce_intr, bd, &bd->ce_intrhand);
640 printf ("ce%d: cannot set up irq\n", unit);
641 bus_release_resource (dev, SYS_RES_IRQ, 0, bd->ce_irq);
642 bus_release_resource (dev, SYS_RES_MEMORY,
643 PCIR_BAR(0), bd->ce_res);
644 ce_bus_dma_mem_free (&bd->dmamem);
650 switch (b->ddk.Model) {
651 case 1: strcpy (b->name, TAU32_BASE_NAME); break;
652 case 2: strcpy (b->name, TAU32_LITE_NAME); break;
653 case 3: strcpy (b->name, TAU32_ADPCM_NAME); break;
654 default: strcpy (b->name, TAU32_UNKNOWN_NAME); break;
657 printf ("ce%d: %s\n", unit, b->name);
659 for (c = b->chan; c < b->chan + NCHAN; ++c) {
660 c->num = (c - b->chan);
663 d = &bd->channel[c->num];
664 d->dmamem.size = sizeof(ce_buf_t);
665 if (! ce_bus_dma_mem_alloc (unit, c->num, &d->dmamem))
668 channel [b->num * NCHAN + c->num] = d;
669 sprintf (d->name, "ce%d.%d", b->num, c->num);
675 for (c = b->chan; c < b->chan + NCHAN; ++c) {
681 if (ng_make_node_common (&typestruct, &d->node) != 0) {
682 printf ("%s: cannot make common node\n", d->name);
686 #if __FreeBSD_version >= 500000
687 NG_NODE_SET_PRIVATE (d->node, d);
688 callout_init (&d->timeout_handle, CALLOUT_MPSAFE);
690 d->node->private = d;
692 sprintf (d->nodename, "%s%d", NG_CE_NODE_TYPE,
693 c->board->num * NCHAN + c->num);
694 if (ng_name_node (d->node, d->nodename)) {
695 printf ("%s: cannot name node\n", d->nodename);
696 #if __FreeBSD_version >= 500000
697 NG_NODE_UNREF (d->node);
704 d->queue.ifq_maxlen = ifqmaxlen;
705 d->hi_queue.ifq_maxlen = ifqmaxlen;
706 d->rqueue.ifq_maxlen = ifqmaxlen;
707 #if __FreeBSD_version >= 500000
708 mtx_init (&d->queue.ifq_mtx, "ce_queue", NULL, MTX_DEF);
709 mtx_init (&d->hi_queue.ifq_mtx, "ce_queue_hi", NULL, MTX_DEF);
710 mtx_init (&d->rqueue.ifq_mtx, "ce_rqueue", NULL, MTX_DEF);
713 #if __FreeBSD_version >= 600031
714 d->ifp = if_alloc(IFT_PPP);
716 d->ifp = malloc (sizeof(struct sppp), M_DEVBUF, M_WAITOK);
717 bzero (d->ifp, sizeof(struct sppp));
720 printf ("%s: cannot if_alloc() interface\n", d->name);
723 d->ifp->if_softc = d;
724 #if __FreeBSD_version > 501000
725 if_initname (d->ifp, "ce", b->num * NCHAN + c->num);
727 d->ifp->if_unit = b->num * NCHAN + c->num;
728 d->ifp->if_name = "ce";
730 d->ifp->if_mtu = PP_MTU;
731 d->ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
732 d->ifp->if_ioctl = ce_sioctl;
733 d->ifp->if_start = ce_ifstart;
734 d->ifp->if_watchdog = ce_ifwatchdog;
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)
810 /* Detach from the packet filter list of interfaces. */
813 /* Detach from the sync PPP list. */
814 sppp_detach (d->ifp);
816 /* Detach from the system list of interfaces. */
818 #if __FreeBSD_version > 600031
821 free (d->ifp, M_DEVBUF);
824 IF_DRAIN (&d->rqueue);
825 #if __FreeBSD_version >= 500000
826 mtx_destroy (&d->rqueue.ifq_mtx);
829 #if __FreeBSD_version >= 500000
831 ng_rmnode_self (d->node);
832 NG_NODE_UNREF (d->node);
835 IF_DRAIN (&d->rqueue);
836 mtx_destroy (&d->queue.ifq_mtx);
837 mtx_destroy (&d->hi_queue.ifq_mtx);
838 mtx_destroy (&d->rqueue.ifq_mtx);
844 destroy_dev (d->devt);
850 callout_drain (&led_timo[b->num]);
852 /* Disable the interrupt request. */
853 bus_teardown_intr (dev, bd->ce_irq, bd->ce_intrhand);
854 bus_release_resource (dev, SYS_RES_IRQ, 0, bd->ce_irq);
855 TAU32_DestructiveHalt (b->ddk.pControllerObject, 0);
856 bus_release_resource (dev, SYS_RES_MEMORY, PCIR_BAR(0), bd->ce_res);
860 for (c = b->chan; c < b->chan + NCHAN; ++c) {
861 drv_t *d = (drv_t*) c->sys;
863 if (! d || ! d->chan)
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);
874 #if __FreeBSD_version >= 504000
875 mtx_destroy (&bd->ce_mtx);
881 static void ce_ifstart (struct ifnet *ifp)
883 drv_t *d = ifp->if_softc;
884 bdrv_t *bd = d->board->sys;
891 static void ce_ifwatchdog (struct ifnet *ifp)
893 drv_t *d = ifp->if_softc;
898 static void ce_tlf (struct sppp *sp)
900 drv_t *d = SP2IFP(sp)->if_softc;
902 CE_DEBUG2 (d, ("ce_tlf\n"));
906 static void ce_tls (struct sppp *sp)
908 drv_t *d = SP2IFP(sp)->if_softc;
910 CE_DEBUG2 (d, ("ce_tls\n"));
915 * Process an ioctl request.
917 static int ce_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data)
919 drv_t *d = ifp->if_softc;
920 bdrv_t *bd = d->board->sys;
921 int error, s, was_up, should_be_up;
923 #if __FreeBSD_version >= 600034
924 was_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
926 was_up = (ifp->if_flags & IFF_RUNNING) != 0;
928 error = sppp_ioctl (ifp, cmd, data);
933 if (! (ifp->if_flags & IFF_DEBUG))
936 d->chan->debug = d->chan->debug_shadow;
939 default: CE_DEBUG2 (d, ("ioctl 0x%lx\n", cmd)); return 0;
940 case SIOCADDMULTI: CE_DEBUG2 (d, ("ioctl SIOCADDMULTI\n")); return 0;
941 case SIOCDELMULTI: CE_DEBUG2 (d, ("ioctl SIOCDELMULTI\n")); return 0;
942 case SIOCSIFFLAGS: CE_DEBUG2 (d, ("ioctl SIOCSIFFLAGS\n")); break;
943 case SIOCSIFADDR: CE_DEBUG2 (d, ("ioctl SIOCSIFADDR\n")); break;
946 /* We get here only in case of SIFFLAGS or SIFADDR. */
949 #if __FreeBSD_version >= 600034
950 should_be_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
952 should_be_up = (ifp->if_flags & IFF_RUNNING) != 0;
954 if (! was_up && should_be_up) {
955 /* Interface goes up -- start it. */
958 } else if (was_up && ! should_be_up) {
959 /* Interface is going down -- stop it. */
960 /* if ((IFP2SP(ifp)->pp_flags & PP_FR) || (ifp->if_flags & PP_CISCO))*/
963 CE_DEBUG (d, ("ioctl 0x%lx p4\n", cmd));
970 * Initialization of interface.
971 * It seems to be never called by upper level?
973 static void ce_initialize (void *softc)
977 CE_DEBUG (d, ("ce_initialize\n"));
982 * Stop the interface. Called on splimp().
984 static void ce_down (drv_t *d)
986 CE_DEBUG (d, ("ce_down\n"));
987 /* Interface is going down -- stop it. */
988 ce_set_dtr (d->chan, 0);
989 ce_set_rts (d->chan, 0);
995 * Start the interface. Called on splimp().
997 static void ce_up (drv_t *d)
999 CE_DEBUG (d, ("ce_up\n"));
1000 ce_set_dtr (d->chan, 1);
1001 ce_set_rts (d->chan, 1);
1007 * Start output on the interface. Get another datagram to send
1008 * off of the interface queue, and copy it to the interface
1009 * before starting the output.
1011 static void ce_send (drv_t *d)
1016 CE_DEBUG2 (d, ("ce_send\n"));
1018 /* No output if the interface is down. */
1022 while (ce_transmit_space (d->chan)) {
1023 /* Get the packet to send. */
1025 IF_DEQUEUE (&d->hi_queue, m);
1027 IF_DEQUEUE (&d->queue, m);
1029 m = sppp_dequeue (d->ifp);
1034 #if __FreeBSD_version >= 500000
1035 BPF_MTAP (d->ifp, m);
1038 bpf_mtap (d->ifp, m);
1041 #if __FreeBSD_version >= 490000
1042 len = m_length (m, NULL);
1044 len = m->m_pkthdr.len;
1047 printf ("%s: too long packet: %d bytes: ",
1049 else if (! m->m_next)
1050 ce_send_packet (d->chan, (u_char*) mtod (m, caddr_t), len, 0);
1052 ce_buf_item_t *item = (ce_buf_item_t*)d->chan->tx_queue;
1053 m_copydata (m, 0, len, item->buf);
1054 ce_send_packet (d->chan, item->buf, len, 0);
1057 /* Set up transmit timeout, if the transmit ring is not empty.*/
1061 d->ifp->if_timer = 10;
1065 #if __FreeBSD_version >= 600034
1066 d->ifp->if_flags |= IFF_DRV_OACTIVE;
1068 d->ifp->if_flags |= IFF_OACTIVE;
1074 * Start output on the interface.
1075 * Always called on splimp().
1077 static void ce_start (drv_t *d)
1081 ce_set_dtr (d->chan, 1);
1083 ce_set_rts (d->chan, 1);
1089 * Handle transmit timeouts.
1090 * Recover after lost transmit interrupts.
1091 * Always called on splimp().
1093 static void ce_watchdog (drv_t *d)
1095 bdrv_t *bd = d->board->sys;
1096 CE_DEBUG (d, ("device timeout\n"));
1100 ce_set_dtr (d->chan, 0);
1101 ce_set_rts (d->chan, 0);
1102 /* ce_stop_chan (d->chan);*/
1103 /* ce_start_chan (d->chan, 1, 1, 0, 0);*/
1104 ce_set_dtr (d->chan, 1);
1105 ce_set_rts (d->chan, 1);
1112 static void ce_transmit (ce_chan_t *c, void *attachment, int len)
1119 ++d->ifp->if_opackets;
1120 #if __FreeBSD_version >= 600034
1121 d->ifp->if_flags &= ~IFF_DRV_OACTIVE;
1123 d->ifp->if_flags &= ~IFF_OACTIVE;
1125 d->ifp->if_timer = 0;
1130 static void ce_receive (ce_chan_t *c, unsigned char *data, int len)
1138 m = makembuf (data, len);
1140 CE_DEBUG (d, ("no memory for packet\n"));
1142 ++d->ifp->if_iqdrops;
1149 m->m_pkthdr.rcvif = 0;
1150 IF_ENQUEUE(&d->rqueue, m);
1152 ++d->ifp->if_ipackets;
1153 m->m_pkthdr.rcvif = d->ifp;
1154 /* Check if there's a BPF listener on this interface.
1155 * If so, hand off the raw packet to bpf. */
1156 #if __FreeBSD_version >= 500000
1157 BPF_TAP (d->ifp, data, len);
1160 bpf_tap (d->ifp, data, len);
1162 IF_ENQUEUE(&d->rqueue, m);
1166 static void ce_error (ce_chan_t *c, int data)
1172 CE_DEBUG (d, ("frame error\n"));
1174 ++d->ifp->if_ierrors;
1178 CE_DEBUG (d, ("crc error\n"));
1180 ++d->ifp->if_ierrors;
1184 CE_DEBUG (d, ("overrun error\n"));
1186 ++d->ifp->if_collisions;
1187 ++d->ifp->if_ierrors;
1191 CE_DEBUG (d, ("overflow error\n"));
1193 ++d->ifp->if_ierrors;
1197 CE_DEBUG (d, ("underrun error\n"));
1201 ++d->ifp->if_oerrors;
1202 #if __FreeBSD_version >= 600034
1203 d->ifp->if_flags &= ~IFF_DRV_OACTIVE;
1205 d->ifp->if_flags &= ~IFF_OACTIVE;
1207 d->ifp->if_timer = 0;
1212 CE_DEBUG (d, ("error #%d\n", data));
1218 * You also need read, write, open, close routines.
1219 * This should get you started
1221 #if __FreeBSD_version < 500000
1222 static int ce_open (dev_t dev, int oflags, int devtype, struct proc *p)
1224 static int ce_open (struct cdev *dev, int oflags, int devtype, struct thread *td)
1227 int unit = dev2unit (dev);
1230 if (unit >= NBRD*NCHAN || ! (d = channel[unit]))
1232 CE_DEBUG2 (d, ("ce_open\n"));
1237 * Only called on the LAST close.
1239 #if __FreeBSD_version < 500000
1240 static int ce_close (dev_t dev, int fflag, int devtype, struct proc *p)
1242 static int ce_close (struct cdev *dev, int fflag, int devtype, struct thread *td)
1245 drv_t *d = channel [dev2unit (dev)];
1247 CE_DEBUG2 (d, ("ce_close\n"));
1251 static int ce_modem_status (ce_chan_t *c)
1254 bdrv_t *bd = d->board->sys;
1257 status = d->running ? TIOCM_LE : 0;
1260 if (ce_get_cd (c)) status |= TIOCM_CD;
1261 if (ce_get_cts (c)) status |= TIOCM_CTS;
1262 if (ce_get_dsr (c)) status |= TIOCM_DSR;
1263 if (c->dtr) status |= TIOCM_DTR;
1264 if (c->rts) status |= TIOCM_RTS;
1270 #if __FreeBSD_version < 500000
1271 static int ce_ioctl (dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
1273 static int ce_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
1276 drv_t *d = channel [dev2unit (dev)];
1277 bdrv_t *bd = d->board->sys;
1278 ce_chan_t *c = d->chan;
1279 struct serial_statistics *st;
1280 struct e1_statistics *opte1;
1285 case SERIAL_GETREGISTERED:
1286 CE_DEBUG2 (d, ("ioctl: getregistered\n"));
1287 bzero (mask, sizeof(mask));
1288 for (s=0; s<NBRD*NCHAN; ++s)
1290 mask [s/8] |= 1 << (s & 7);
1291 bcopy (mask, data, sizeof (mask));
1295 case SERIAL_GETPROTO:
1296 CE_DEBUG2 (d, ("ioctl: getproto\n"));
1297 strcpy ((char*)data, (IFP2SP(d->ifp)->pp_flags & PP_FR) ? "fr" :
1298 (d->ifp->if_flags & PP_CISCO) ? "cisco" : "ppp");
1301 case SERIAL_SETPROTO:
1302 CE_DEBUG2 (d, ("ioctl: setproto\n"));
1303 /* Only for superuser! */
1304 #if __FreeBSD_version < 500000
1306 #elif __FreeBSD_version < 700000
1309 error = priv_check (td, PRIV_DRIVER);
1313 #if __FreeBSD_version >= 600034
1314 if (d->ifp->if_flags & IFF_DRV_RUNNING)
1316 if (d->ifp->if_flags & IFF_RUNNING)
1319 if (! strcmp ("cisco", (char*)data)) {
1320 IFP2SP(d->ifp)->pp_flags &= ~(PP_FR);
1321 IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE;
1322 d->ifp->if_flags |= PP_CISCO;
1323 } else if (! strcmp ("fr", (char*)data) && PP_FR) {
1324 d->ifp->if_flags &= ~(PP_CISCO);
1325 IFP2SP(d->ifp)->pp_flags |= PP_FR | PP_KEEPALIVE;
1326 } else if (! strcmp ("ppp", (char*)data)) {
1327 IFP2SP(d->ifp)->pp_flags &= ~PP_FR;
1328 IFP2SP(d->ifp)->pp_flags &= ~PP_KEEPALIVE;
1329 d->ifp->if_flags &= ~(PP_CISCO);
1334 case SERIAL_GETKEEPALIVE:
1335 CE_DEBUG2 (d, ("ioctl: getkeepalive\n"));
1336 if ((IFP2SP(d->ifp)->pp_flags & PP_FR) ||
1337 (d->ifp->if_flags & PP_CISCO))
1339 *(int*)data = (IFP2SP(d->ifp)->pp_flags & PP_KEEPALIVE) ? 1 : 0;
1342 case SERIAL_SETKEEPALIVE:
1343 CE_DEBUG2 (d, ("ioctl: setkeepalive\n"));
1344 /* Only for superuser! */
1345 #if __FreeBSD_version < 500000
1347 #elif __FreeBSD_version < 700000
1350 error = priv_check (td, PRIV_DRIVER);
1354 if ((IFP2SP(d->ifp)->pp_flags & PP_FR) ||
1355 (d->ifp->if_flags & PP_CISCO))
1360 IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE;
1362 IFP2SP(d->ifp)->pp_flags &= ~PP_KEEPALIVE;
1368 case SERIAL_GETMODE:
1369 CE_DEBUG2 (d, ("ioctl: getmode\n"));
1370 *(int*)data = SERIAL_HDLC;
1373 case SERIAL_SETMODE:
1374 /* Only for superuser! */
1375 #if __FreeBSD_version < 500000
1377 #elif __FreeBSD_version < 700000
1380 error = priv_check (td, PRIV_DRIVER);
1384 if (*(int*)data != SERIAL_HDLC)
1389 CE_DEBUG2 (d, ("ioctl: getcfg\n"));
1394 CE_DEBUG2 (d, ("ioctl: setcfg\n"));
1395 #if __FreeBSD_version < 500000
1397 #elif __FreeBSD_version < 700000
1400 error = priv_check (td, PRIV_DRIVER);
1404 if (*((char*)data) != 'c')
1408 case SERIAL_GETSTAT:
1409 CE_DEBUG2 (d, ("ioctl: getstat\n"));
1410 st = (struct serial_statistics*) data;
1411 st->rintr = c->rintr;
1412 st->tintr = c->tintr;
1414 st->ibytes = c->ibytes;
1415 st->ipkts = c->ipkts;
1416 st->obytes = c->obytes;
1417 st->opkts = c->opkts;
1418 st->ierrs = c->overrun + c->frame + c->crc;
1419 st->oerrs = c->underrun;
1422 case SERIAL_GETESTAT:
1423 CE_DEBUG2 (d, ("ioctl: getestat\n"));
1424 if (c->type != T_E1)
1426 opte1 = (struct e1_statistics*) data;
1429 if (c->status & ESTS_NOALARM)
1430 opte1->status |= E1_NOALARM;
1431 if (c->status & ESTS_LOS)
1432 opte1->status |= E1_LOS;
1433 if (c->status & ESTS_LOF)
1434 opte1->status |= E1_LOF;
1435 if (c->status & ESTS_AIS)
1436 opte1->status |= E1_AIS;
1437 if (c->status & ESTS_LOMF)
1438 opte1->status |= E1_LOMF;
1439 if (c->status & ESTS_AIS16)
1440 opte1->status |= E1_AIS16;
1441 if (c->status & ESTS_FARLOF)
1442 opte1->status |= E1_FARLOF;
1443 if (c->status & ESTS_FARLOMF)
1444 opte1->status |= E1_FARLOMF;
1445 if (c->status & ESTS_TSTREQ)
1446 opte1->status |= E1_TSTREQ;
1447 if (c->status & ESTS_TSTERR)
1448 opte1->status |= E1_TSTERR;
1450 opte1->cursec = c->cursec;
1451 opte1->totsec = c->totsec + c->cursec;
1453 opte1->currnt.bpv = c->currnt.bpv;
1454 opte1->currnt.fse = c->currnt.fse;
1455 opte1->currnt.crce = c->currnt.crce;
1456 opte1->currnt.rcrce = c->currnt.rcrce;
1457 opte1->currnt.uas = c->currnt.uas;
1458 opte1->currnt.les = c->currnt.les;
1459 opte1->currnt.es = c->currnt.es;
1460 opte1->currnt.bes = c->currnt.bes;
1461 opte1->currnt.ses = c->currnt.ses;
1462 opte1->currnt.oofs = c->currnt.oofs;
1463 opte1->currnt.css = c->currnt.css;
1464 opte1->currnt.dm = c->currnt.dm;
1466 opte1->total.bpv = c->total.bpv + c->currnt.bpv;
1467 opte1->total.fse = c->total.fse + c->currnt.fse;
1468 opte1->total.crce = c->total.crce + c->currnt.crce;
1469 opte1->total.rcrce = c->total.rcrce + c->currnt.rcrce;
1470 opte1->total.uas = c->total.uas + c->currnt.uas;
1471 opte1->total.les = c->total.les + c->currnt.les;
1472 opte1->total.es = c->total.es + c->currnt.es;
1473 opte1->total.bes = c->total.bes + c->currnt.bes;
1474 opte1->total.ses = c->total.ses + c->currnt.ses;
1475 opte1->total.oofs = c->total.oofs + c->currnt.oofs;
1476 opte1->total.css = c->total.css + c->currnt.css;
1477 opte1->total.dm = c->total.dm + c->currnt.dm;
1478 for (s=0; s<48; ++s) {
1479 opte1->interval[s].bpv = c->interval[s].bpv;
1480 opte1->interval[s].fse = c->interval[s].fse;
1481 opte1->interval[s].crce = c->interval[s].crce;
1482 opte1->interval[s].rcrce = c->interval[s].rcrce;
1483 opte1->interval[s].uas = c->interval[s].uas;
1484 opte1->interval[s].les = c->interval[s].les;
1485 opte1->interval[s].es = c->interval[s].es;
1486 opte1->interval[s].bes = c->interval[s].bes;
1487 opte1->interval[s].ses = c->interval[s].ses;
1488 opte1->interval[s].oofs = c->interval[s].oofs;
1489 opte1->interval[s].css = c->interval[s].css;
1490 opte1->interval[s].dm = c->interval[s].dm;
1494 case SERIAL_CLRSTAT:
1495 CE_DEBUG2 (d, ("ioctl: clrstat\n"));
1496 /* Only for superuser! */
1497 #if __FreeBSD_version < 500000
1499 #elif __FreeBSD_version < 700000
1502 error = priv_check (td, PRIV_DRIVER);
1516 bzero (&c->currnt, sizeof (c->currnt));
1517 bzero (&c->total, sizeof (c->total));
1518 bzero (c->interval, sizeof (c->interval));
1521 case SERIAL_GETLOOP:
1522 CE_DEBUG2 (d, ("ioctl: getloop\n"));
1523 if (c->type != T_E1)
1525 *(int*)data = c->lloop;
1528 case SERIAL_SETLOOP:
1529 CE_DEBUG2 (d, ("ioctl: setloop\n"));
1530 if (c->type != T_E1)
1532 /* Only for superuser! */
1533 #if __FreeBSD_version < 500000
1535 #elif __FreeBSD_version < 700000
1538 error = priv_check (td, PRIV_DRIVER);
1544 ce_set_lloop (c, *(int*)data);
1549 case SERIAL_GETRLOOP:
1550 CE_DEBUG2 (d, ("ioctl: getrloop\n"));
1551 if (c->type != T_E1)
1553 *(int*)data = c->rloop;
1556 case SERIAL_SETRLOOP:
1557 CE_DEBUG2 (d, ("ioctl: setloop\n"));
1558 if (c->type != T_E1)
1560 /* Only for superuser! */
1561 #if __FreeBSD_version < 500000
1563 #elif __FreeBSD_version < 700000
1566 error = priv_check (td, PRIV_DRIVER);
1572 ce_set_rloop (c, *(int*)data);
1577 case SERIAL_GETDEBUG:
1578 CE_DEBUG2 (d, ("ioctl: getdebug\n"));
1579 *(int*)data = d->chan->debug;
1582 case SERIAL_SETDEBUG:
1583 CE_DEBUG2 (d, ("ioctl: setdebug\n"));
1584 /* Only for superuser! */
1585 #if __FreeBSD_version < 500000
1587 #elif __FreeBSD_version < 700000
1590 error = priv_check (td, PRIV_DRIVER);
1596 * The debug_shadow is always greater than zero for logic
1597 * simplicity. For switching debug off the IFF_DEBUG is
1600 d->chan->debug_shadow = (*(int*)data) ? (*(int*)data) : 1;
1601 if (d->ifp->if_flags & IFF_DEBUG)
1602 d->chan->debug = d->chan->debug_shadow;
1604 d->chan->debug = *(int*)data;
1608 case SERIAL_GETBAUD:
1609 CE_DEBUG2 (d, ("ioctl: getbaud\n"));
1610 *(long*)data = c->baud;
1613 case SERIAL_SETBAUD:
1614 CE_DEBUG2 (d, ("ioctl: setbaud\n"));
1615 if (c->type != T_E1 || !c->unfram)
1617 /* Only for superuser! */
1618 #if __FreeBSD_version < 500000
1620 #elif __FreeBSD_version < 700000
1623 error = priv_check (td, PRIV_DRIVER);
1629 ce_set_baud (c, *(long*)data);
1634 case SERIAL_GETTIMESLOTS:
1635 CE_DEBUG2 (d, ("ioctl: gettimeslots\n"));
1636 if ((c->type != T_E1 || c->unfram) && c->type != T_DATA)
1638 *(u_long*)data = c->ts;
1641 case SERIAL_SETTIMESLOTS:
1642 CE_DEBUG2 (d, ("ioctl: settimeslots\n"));
1643 /* Only for superuser! */
1644 #if __FreeBSD_version < 500000
1646 #elif __FreeBSD_version < 700000
1649 error = priv_check (td, PRIV_DRIVER);
1653 if ((c->type != T_E1 || c->unfram) && c->type != T_DATA)
1657 ce_set_ts (c, *(u_long*)data);
1662 case SERIAL_GETHIGAIN:
1663 CE_DEBUG2 (d, ("ioctl: gethigain\n"));
1664 if (c->type != T_E1)
1666 *(int*)data = c->higain;
1669 case SERIAL_SETHIGAIN:
1670 CE_DEBUG2 (d, ("ioctl: sethigain\n"));
1671 if (c->type != T_E1)
1673 /* Only for superuser! */
1674 #if __FreeBSD_version < 500000
1676 #elif __FreeBSD_version < 700000
1679 error = priv_check (td, PRIV_DRIVER);
1685 ce_set_higain (c, *(int*)data);
1690 case SERIAL_GETPHONY:
1691 CE_DEBUG2 (d, ("ioctl: getphony\n"));
1692 *(int*)data = c->phony;
1695 case SERIAL_SETPHONY:
1696 CE_DEBUG2 (d, ("ioctl: setphony\n"));
1697 /* Only for superuser! */
1698 #if __FreeBSD_version < 500000
1700 #elif __FreeBSD_version < 700000
1703 error = priv_check (td, PRIV_DRIVER);
1709 ce_set_phony (c, *(int*)data);
1714 case SERIAL_GETUNFRAM:
1715 CE_DEBUG2 (d, ("ioctl: getunfram\n"));
1716 if (c->type != T_E1 || c->num != 0)
1718 *(int*)data = c->unfram;
1721 case SERIAL_SETUNFRAM:
1722 CE_DEBUG2 (d, ("ioctl: setunfram\n"));
1723 if (c->type != T_E1 || c->num != 0)
1725 /* Only for superuser! */
1726 #if __FreeBSD_version < 500000
1728 #elif __FreeBSD_version < 700000
1731 error = priv_check (td, PRIV_DRIVER);
1737 ce_set_unfram (c, *(int*)data);
1742 case SERIAL_GETSCRAMBLER:
1743 CE_DEBUG2 (d, ("ioctl: getscrambler\n"));
1746 *(int*)data = c->scrambler;
1749 case SERIAL_SETSCRAMBLER:
1750 CE_DEBUG2 (d, ("ioctl: setscrambler\n"));
1751 /* Only for superuser! */
1752 #if __FreeBSD_version < 500000
1754 #elif __FreeBSD_version < 700000
1757 error = priv_check (td, PRIV_DRIVER);
1765 ce_set_scrambler (c, *(int*)data);
1770 case SERIAL_GETMONITOR:
1771 CE_DEBUG2 (d, ("ioctl: getmonitor\n"));
1772 if (c->type != T_E1)
1774 *(int*)data = c->monitor;
1777 case SERIAL_SETMONITOR:
1778 CE_DEBUG2 (d, ("ioctl: setmonitor\n"));
1779 /* Only for superuser! */
1780 #if __FreeBSD_version < 500000
1782 #elif __FreeBSD_version < 700000
1785 error = priv_check (td, PRIV_DRIVER);
1789 if (c->type != T_E1)
1793 ce_set_monitor (c, *(int*)data);
1798 case SERIAL_GETUSE16:
1799 CE_DEBUG2 (d, ("ioctl: getuse16\n"));
1800 if (c->type != T_E1 || c->unfram)
1802 *(int*)data = c->use16;
1805 case SERIAL_SETUSE16:
1806 CE_DEBUG2 (d, ("ioctl: setuse16\n"));
1807 /* Only for superuser! */
1808 #if __FreeBSD_version < 500000
1810 #elif __FreeBSD_version < 700000
1813 error = priv_check (td, PRIV_DRIVER);
1817 if (c->type != T_E1)
1821 ce_set_use16 (c, *(int*)data);
1826 case SERIAL_GETCRC4:
1827 CE_DEBUG2 (d, ("ioctl: getcrc4\n"));
1828 if (c->type != T_E1 || c->unfram)
1830 *(int*)data = c->crc4;
1833 case SERIAL_SETCRC4:
1834 CE_DEBUG2 (d, ("ioctl: setcrc4\n"));
1835 /* Only for superuser! */
1836 #if __FreeBSD_version < 500000
1838 #elif __FreeBSD_version < 700000
1841 error = priv_check (td, PRIV_DRIVER);
1845 if (c->type != T_E1 || c->unfram)
1849 ce_set_crc4 (c, *(int*)data);
1855 CE_DEBUG2 (d, ("ioctl: getclk\n"));
1856 if (c->type != T_E1)
1859 default: *(int*)data = E1CLK_INTERNAL; break;
1860 case GSYN_RCV: *(int*)data = E1CLK_RECEIVE; break;
1861 case GSYN_RCV0: *(int*)data = E1CLK_RECEIVE_CHAN0; break;
1862 case GSYN_RCV1: *(int*)data = E1CLK_RECEIVE_CHAN1; break;
1867 CE_DEBUG2 (d, ("ioctl: setclk\n"));
1868 /* Only for superuser! */
1869 #if __FreeBSD_version < 500000
1871 #elif __FreeBSD_version < 700000
1874 error = priv_check (td, PRIV_DRIVER);
1878 if (c->type != T_E1)
1882 switch (*(int*)data) {
1883 default: ce_set_gsyn (c, GSYN_INT); break;
1884 case E1CLK_RECEIVE: ce_set_gsyn (c, GSYN_RCV); break;
1885 case E1CLK_RECEIVE_CHAN0: ce_set_gsyn (c, GSYN_RCV0); break;
1886 case E1CLK_RECEIVE_CHAN1: ce_set_gsyn (c, GSYN_RCV1); break;
1894 CE_DEBUG2 (d, ("ioctl: reset\n"));
1895 /* Only for superuser! */
1896 #if __FreeBSD_version < 500000
1898 #elif __FreeBSD_version < 700000
1901 error = priv_check (td, PRIV_DRIVER);
1907 /* ce_reset (c->board, 0, 0);*/
1912 case SERIAL_HARDRESET:
1913 CE_DEBUG2 (d, ("ioctl: hardreset\n"));
1914 /* Only for superuser! */
1915 #if __FreeBSD_version < 500000
1917 #elif __FreeBSD_version < 700000
1920 error = priv_check (td, PRIV_DRIVER);
1926 /* hard_reset (c->board); */
1932 case SERIAL_GETCABLE:
1933 CE_DEBUG2 (d, ("ioctl: getcable\n"));
1934 if (c->type != T_E1)
1938 *(int*)data = CABLE_TP;
1944 CE_DEBUG2 (d, ("ioctl: getdir\n"));
1945 if (c->type != T_E1 && c->type != T_DATA)
1947 *(int*)data = c->dir;
1951 CE_DEBUG2 (d, ("ioctl: setdir\n"));
1952 /* Only for superuser! */
1953 #if __FreeBSD_version < 500000
1955 #elif __FreeBSD_version < 700000
1958 error = priv_check (td, PRIV_DRIVER);
1964 ce_set_dir (c, *(int*)data);
1969 case TIOCSDTR: /* Set DTR */
1977 case TIOCCDTR: /* Clear DTR */
1985 case TIOCMSET: /* Set DTR/RTS */
1988 ce_set_dtr (c, (*(int*)data & TIOCM_DTR) ? 1 : 0);
1989 ce_set_rts (c, (*(int*)data & TIOCM_RTS) ? 1 : 0);
1994 case TIOCMBIS: /* Add DTR/RTS */
1997 if (*(int*)data & TIOCM_DTR) ce_set_dtr (c, 1);
1998 if (*(int*)data & TIOCM_RTS) ce_set_rts (c, 1);
2003 case TIOCMBIC: /* Clear DTR/RTS */
2006 if (*(int*)data & TIOCM_DTR) ce_set_dtr (c, 0);
2007 if (*(int*)data & TIOCM_RTS) ce_set_rts (c, 0);
2012 case TIOCMGET: /* Get modem status */
2013 *(int*)data = ce_modem_status (c);
2020 #if __FreeBSD_version >= 500000
2021 static int ng_ce_constructor (node_p node)
2023 drv_t *d = NG_NODE_PRIVATE (node);
2025 static int ng_ce_constructor (node_p *node)
2027 drv_t *d = (*node)->private;
2029 CE_DEBUG (d, ("Constructor\n"));
2033 static int ng_ce_newhook (node_p node, hook_p hook, const char *name)
2036 #if __FreeBSD_version >= 500000
2037 drv_t *d = NG_NODE_PRIVATE (node);
2039 drv_t *d = node->private;
2041 bdrv_t *bd = d->board->sys;
2043 CE_DEBUG (d, ("Newhook\n"));
2044 /* Attach debug hook */
2045 if (strcmp (name, NG_CE_HOOK_DEBUG) == 0) {
2046 #if __FreeBSD_version >= 500000
2047 NG_HOOK_SET_PRIVATE (hook, NULL);
2051 d->debug_hook = hook;
2055 /* Check for raw hook */
2056 if (strcmp (name, NG_CE_HOOK_RAW) != 0)
2059 #if __FreeBSD_version >= 500000
2060 NG_HOOK_SET_PRIVATE (hook, d);
2073 static char *format_timeslots (u_long s)
2075 static char buf [100];
2079 for (i=1; i<32; ++i)
2081 int prev = (i > 1) & (s >> (i-1));
2082 int next = (i < 31) & (s >> (i+1));
2092 *p++ = '0' + i / 10;
2093 *p++ = '0' + i % 10;
2099 static int print_modems (char *s, ce_chan_t *c, int need_header)
2101 int status = ce_modem_status (c);
2105 length += sprintf (s + length, " LE DTR DSR RTS CTS CD\n");
2106 length += sprintf (s + length, "%4s %4s %4s %4s %4s %4s\n",
2107 status & TIOCM_LE ? "On" : "-",
2108 status & TIOCM_DTR ? "On" : "-",
2109 status & TIOCM_DSR ? "On" : "-",
2110 status & TIOCM_RTS ? "On" : "-",
2111 status & TIOCM_CTS ? "On" : "-",
2112 status & TIOCM_CD ? "On" : "-");
2116 static int print_stats (char *s, ce_chan_t *c, int need_header)
2121 length += sprintf (s + length, " Rintr Tintr Mintr Ibytes Ipkts Ierrs Obytes Opkts Oerrs\n");
2122 length += sprintf (s + length, "%7ld %7ld %7ld %8lu %7ld %7ld %8lu %7ld %7ld\n",
2123 c->rintr, c->tintr, 0l, (unsigned long) c->ibytes,
2124 c->ipkts, c->overrun + c->frame + c->crc,
2125 (unsigned long) c->obytes, c->opkts, c->underrun);
2129 static char *format_e1_status (u_char status)
2131 static char buf [80];
2133 if (status & E1_NOALARM)
2136 if (status & E1_LOS) strcat (buf, ",LOS");
2137 if (status & E1_AIS) strcat (buf, ",AIS");
2138 if (status & E1_LOF) strcat (buf, ",LOF");
2139 if (status & E1_LOMF) strcat (buf, ",LOMF");
2140 if (status & E1_FARLOF) strcat (buf, ",FARLOF");
2141 if (status & E1_AIS16) strcat (buf, ",AIS16");
2142 if (status & E1_FARLOMF) strcat (buf, ",FARLOMF");
2143 if (status & E1_TSTREQ) strcat (buf, ",TSTREQ");
2144 if (status & E1_TSTERR) strcat (buf, ",TSTERR");
2150 static int print_frac (char *s, int leftalign, u_long numerator, u_long divider)
2154 if (numerator < 1 || divider < 1) {
2155 length += sprintf (s+length, leftalign ? "/- " : " -");
2158 n = (int) (0.5 + 1000.0 * numerator / divider);
2160 length += sprintf (s+length, leftalign ? "/.%-3d" : " .%03d", n);
2163 *(s + length) = leftalign ? '/' : ' ';
2166 if (n >= 1000000) n = (n+500) / 1000 * 1000;
2167 else if (n >= 100000) n = (n+50) / 100 * 100;
2168 else if (n >= 10000) n = (n+5) / 10 * 10;
2171 case 1000: length += printf (s+length, ".999"); return length;
2172 case 10000: n = 9990; break;
2173 case 100000: n = 99900; break;
2174 case 1000000: n = 999000; break;
2176 if (n < 10000) length += sprintf (s+length, "%d.%d", n/1000, n/10%100);
2177 else if (n < 100000) length += sprintf (s+length, "%d.%d", n/1000, n/100%10);
2178 else if (n < 1000000) length += sprintf (s+length, "%d.", n/1000);
2179 else length += sprintf (s+length, "%d", n/1000);
2184 static int print_e1_stats (char *s, ce_chan_t *c)
2186 struct e1_counters total;
2190 totsec = c->totsec + c->cursec;
2191 total.bpv = c->total.bpv + c->currnt.bpv;
2192 total.fse = c->total.fse + c->currnt.fse;
2193 total.crce = c->total.crce + c->currnt.crce;
2194 total.rcrce = c->total.rcrce + c->currnt.rcrce;
2195 total.uas = c->total.uas + c->currnt.uas;
2196 total.les = c->total.les + c->currnt.les;
2197 total.es = c->total.es + c->currnt.es;
2198 total.bes = c->total.bes + c->currnt.bes;
2199 total.ses = c->total.ses + c->currnt.ses;
2200 total.oofs = c->total.oofs + c->currnt.oofs;
2201 total.css = c->total.css + c->currnt.css;
2202 total.dm = c->total.dm + c->currnt.dm;
2204 length += sprintf (s + length, " Unav/Degr Bpv/Fsyn CRC/RCRC Err/Lerr Sev/Bur Oof/Slp Status\n");
2206 /* Unavailable seconds, degraded minutes */
2207 length += print_frac (s + length, 0, c->currnt.uas, c->cursec);
2208 length += print_frac (s + length, 1, 60 * c->currnt.dm, c->cursec);
2210 /* Bipolar violations, frame sync errors */
2211 length += print_frac (s + length, 0, c->currnt.bpv, c->cursec);
2212 length += print_frac (s + length, 1, c->currnt.fse, c->cursec);
2214 /* CRC errors, remote CRC errors (E-bit) */
2215 length += print_frac (s + length, 0, c->currnt.crce, c->cursec);
2216 length += print_frac (s + length, 1, c->currnt.rcrce, c->cursec);
2218 /* Errored seconds, line errored seconds */
2219 length += print_frac (s + length, 0, c->currnt.es, c->cursec);
2220 length += print_frac (s + length, 1, c->currnt.les, c->cursec);
2222 /* Severely errored seconds, burst errored seconds */
2223 length += print_frac (s + length, 0, c->currnt.ses, c->cursec);
2224 length += print_frac (s + length, 1, c->currnt.bes, c->cursec);
2226 /* Out of frame seconds, controlled slip seconds */
2227 length += print_frac (s + length, 0, c->currnt.oofs, c->cursec);
2228 length += print_frac (s + length, 1, c->currnt.css, c->cursec);
2230 length += sprintf (s + length, " %s\n", format_e1_status (c->status));
2232 /* Print total statistics. */
2233 length += print_frac (s + length, 0, total.uas, totsec);
2234 length += print_frac (s + length, 1, 60 * total.dm, totsec);
2236 length += print_frac (s + length, 0, total.bpv, totsec);
2237 length += print_frac (s + length, 1, total.fse, totsec);
2239 length += print_frac (s + length, 0, total.crce, totsec);
2240 length += print_frac (s + length, 1, total.rcrce, totsec);
2242 length += print_frac (s + length, 0, total.es, totsec);
2243 length += print_frac (s + length, 1, total.les, totsec);
2245 length += print_frac (s + length, 0, total.ses, totsec);
2246 length += print_frac (s + length, 1, total.bes, totsec);
2248 length += print_frac (s + length, 0, total.oofs, totsec);
2249 length += print_frac (s + length, 1, total.css, totsec);
2251 length += sprintf (s + length, " -- Total\n");
2255 static int print_chan (char *s, ce_chan_t *c)
2260 length += sprintf (s + length, "ce%d", c->board->num * NCHAN + c->num);
2262 length += sprintf (s + length, " debug=%d", d->chan->debug);
2264 if (c->board->mux) {
2265 length += sprintf (s + length, " cfg=C");
2267 length += sprintf (s + length, " cfg=A");
2271 length += sprintf (s + length, " %ld", c->baud);
2273 length += sprintf (s + length, " extclock");
2275 if (c->type == T_E1)
2277 case GSYN_INT : length += sprintf (s + length, " syn=int"); break;
2278 case GSYN_RCV : length += sprintf (s + length, " syn=rcv"); break;
2279 case GSYN_RCV0 : length += sprintf (s + length, " syn=rcv0"); break;
2280 case GSYN_RCV1 : length += sprintf (s + length, " syn=rcv1"); break;
2282 if (c->type == T_E1)
2283 length += sprintf (s + length, " higain=%s", c->higain ? "on" : "off");
2285 length += sprintf (s + length, " loop=%s", c->lloop ? "on" : "off");
2287 if (c->type == T_E1)
2288 length += sprintf (s + length, " ts=%s", format_timeslots (c->ts));
2289 length += sprintf (s + length, "\n");
2293 #if __FreeBSD_version >= 500000
2294 static int ng_ce_rcvmsg (node_p node, item_p item, hook_p lasthook)
2296 drv_t *d = NG_NODE_PRIVATE (node);
2297 struct ng_mesg *msg;
2299 static int ng_ce_rcvmsg (node_p node, struct ng_mesg *msg,
2300 const char *retaddr, struct ng_mesg **rptr)
2302 drv_t *d = node->private;
2304 struct ng_mesg *resp = NULL;
2307 CE_DEBUG (d, ("Rcvmsg\n"));
2308 #if __FreeBSD_version >= 500000
2309 NGI_GET_MSG (item, msg);
2311 switch (msg->header.typecookie) {
2317 printf ("Not implemented yet\n");
2321 case NGM_GENERIC_COOKIE:
2322 switch (msg->header.cmd) {
2327 case NGM_TEXT_STATUS: {
2330 int dl = sizeof (struct ng_mesg) + 730;
2332 #if __FreeBSD_version >= 500000
2333 NG_MKRESPONSE (resp, msg, dl, M_NOWAIT);
2339 resp = malloc (M_NETGRAPH, M_NOWAIT);
2348 l += print_chan (s + l, d->chan);
2349 l += print_stats (s + l, d->chan, 1);
2350 l += print_modems (s + l, d->chan, 1);
2351 l += print_e1_stats (s + l, d->chan);
2353 l += sprintf (s + l, "Error: node not connect to channel");
2354 #if __FreeBSD_version < 500000
2355 (resp)->header.version = NG_VERSION;
2356 (resp)->header.arglen = strlen (s) + 1;
2357 (resp)->header.token = msg->header.token;
2358 (resp)->header.typecookie = NGM_CE_COOKIE;
2359 (resp)->header.cmd = msg->header.cmd;
2361 strncpy ((resp)->header.cmdstr, "status", NG_CMDSTRSIZ);
2367 #if __FreeBSD_version >= 500000
2368 NG_RESPOND_MSG (error, node, item, resp);
2372 free (msg, M_NETGRAPH);
2377 #if __FreeBSD_version >= 500000
2378 static int ng_ce_rcvdata (hook_p hook, item_p item)
2380 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE(hook));
2382 #if __FreeBSD_version < 502120
2385 struct ng_tag_prio *ptag;
2388 static int ng_ce_rcvdata (hook_p hook, struct mbuf *m, meta_p meta)
2390 drv_t *d = hook->node->private;
2392 bdrv_t *bd = d->board->sys;
2396 CE_DEBUG2 (d, ("Rcvdata\n"));
2397 #if __FreeBSD_version >= 500000
2398 NGI_GET_M (item, m);
2399 #if __FreeBSD_version < 502120
2400 NGI_GET_META (item, meta);
2402 NG_FREE_ITEM (item);
2403 if (! NG_HOOK_PRIVATE (hook) || ! d) {
2405 #if __FreeBSD_version < 502120
2406 NG_FREE_META (meta);
2409 if (! hook->private || ! d) {
2410 NG_FREE_DATA (m,meta);
2415 #if __FreeBSD_version >= 502120
2416 /* Check for high priority data */
2417 if ((ptag = (struct ng_tag_prio *)m_tag_locate(m, NGM_GENERIC_COOKIE,
2418 NG_TAG_PRIO, NULL)) != NULL && (ptag->priority > NG_PRIO_CUTOFF) )
2423 q = (meta && meta->priority > 0) ? &d->hi_queue : &d->queue;
2428 #if __FreeBSD_version >= 500000
2430 if (_IF_QFULL (q)) {
2436 #if __FreeBSD_version < 502120
2437 NG_FREE_META (meta);
2448 NG_FREE_DATA (m, meta);
2459 static int ng_ce_rmnode (node_p node)
2461 #if __FreeBSD_version >= 500000
2462 drv_t *d = NG_NODE_PRIVATE (node);
2464 CE_DEBUG (d, ("Rmnode\n"));
2465 if (d && d->running) {
2466 bdrv_t *bd = d->board->sys;
2474 #if __FreeBSD_version >= 502120
2475 if (node->nd_flags & NGF_REALLY_DIE) {
2477 if (node->nd_flags & NG_REALLY_DIE) {
2479 NG_NODE_SET_PRIVATE (node, NULL);
2480 NG_NODE_UNREF (node);
2482 #if __FreeBSD_version >= 502120
2483 NG_NODE_REVIVE(node); /* Persistant node */
2485 node->nd_flags &= ~NG_INVALID;
2488 #else /* __FreeBSD_version < 500000 */
2489 drv_t *d = node->private;
2491 if (d && d->running) {
2492 bdrv_t *bd = d->board->sys;
2500 node->flags |= NG_INVALID;
2510 static void ng_ce_watchdog (void *arg)
2515 if (d->timeout == 1)
2519 callout_reset (&d->timeout_handle, hz, ng_ce_watchdog, d);
2523 static int ng_ce_connect (hook_p hook)
2525 #if __FreeBSD_version >= 500000
2526 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2528 drv_t *d = hook->node->private;
2532 CE_DEBUG (d, ("Connect\n"));
2533 callout_reset (&d->timeout_handle, hz, ng_ce_watchdog, d);
2539 static int ng_ce_disconnect (hook_p hook)
2541 #if __FreeBSD_version >= 500000
2542 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2544 drv_t *d = hook->node->private;
2548 CE_DEBUG (d, ("Disconnect\n"));
2549 #if __FreeBSD_version >= 500000
2550 if (NG_HOOK_PRIVATE (hook))
2555 bdrv_t *bd = d->board->sys;
2562 /* If we were wait it than it reasserted now, just stop it. */
2563 if (!callout_drain (&d->timeout_handle))
2564 callout_stop (&d->timeout_handle);
2570 static int ce_modevent (module_t mod, int type, void *unused)
2572 #if __FreeBSD_version < 500000
2574 struct cdevsw *cdsw;
2576 static int load_count = 0;
2578 #if __FreeBSD_version < 500000
2579 dev = makedev (CDEV_MAJOR, 0);
2584 #if __FreeBSD_version < 500000
2586 (cdsw = devsw (dev)) &&
2587 cdsw->d_maj == CDEV_MAJOR) {
2588 printf ("Tau32-PCI driver is already in system\n");
2592 #if __FreeBSD_version >= 500000 && defined NETGRAPH
2593 if (ng_newtype (&typestruct))
2594 printf ("Failed to register ng_ce\n");
2597 #if __FreeBSD_version <= 500000
2598 cdevsw_add (&ce_cdevsw);
2600 #if __FreeBSD_version >= 500000
2601 callout_init (&timeout_handle, CALLOUT_MPSAFE);
2603 callout_init (&timeout_handle);
2605 callout_reset (&timeout_handle, hz*5, ce_timeout, 0);
2608 if (load_count == 1) {
2609 printf ("Removing device entry for Tau32-PCI\n");
2610 #if __FreeBSD_version <= 500000
2611 cdevsw_remove (&ce_cdevsw);
2613 #if __FreeBSD_version >= 500000 && defined NETGRAPH
2614 ng_rmtype (&typestruct);
2617 /* If we were wait it than it reasserted now, just stop it.
2618 * Actually we shouldn't get this condition. But code could be
2619 * changed in the future, so just be a litle paranoid.
2621 if (!callout_drain (&timeout_handle))
2622 callout_stop (&timeout_handle);
2632 #if __FreeBSD_version >= 502100
2633 static struct ng_type typestruct = {
2634 .version = NG_ABI_VERSION,
2635 .name = NG_CE_NODE_TYPE,
2636 .constructor = ng_ce_constructor,
2637 .rcvmsg = ng_ce_rcvmsg,
2638 .shutdown = ng_ce_rmnode,
2639 .newhook = ng_ce_newhook,
2640 .connect = ng_ce_connect,
2641 .rcvdata = ng_ce_rcvdata,
2642 .disconnect = ng_ce_disconnect,
2644 #else /* __FreeBSD_version < 502100 */
2645 static struct ng_type typestruct = {
2646 #if __FreeBSD_version >= 500000
2660 #if __FreeBSD_version < 500000
2666 #endif /* __FreeBSD_version < 502100 */
2670 #if __FreeBSD_version >= 500000
2672 MODULE_DEPEND (ng_ce, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION);
2674 MODULE_DEPEND (ce, sppp, 1, 1, 1);
2677 DRIVER_MODULE (cemod, pci, ce_driver, ce_devclass, ce_modevent, NULL);
2679 DRIVER_MODULE (ce, pci, ce_driver, ce_devclass, ce_modevent, NULL);
2681 #else /* if __FreeBSD_version < 500000*/
2683 DRIVER_MODULE (ce, pci, ce_driver, ce_devclass, ng_mod_event, &typestruct);
2685 DRIVER_MODULE (ce, pci, ce_driver, ce_devclass, ce_modevent, NULL);
2687 #endif /* __FreeBSD_version < 500000 */