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 * Make an mbuf from data.
303 static struct mbuf *makembuf (void *buf, unsigned len)
307 MGETHDR (m, M_NOWAIT, MT_DATA);
310 MCLGET (m, M_NOWAIT);
311 if (! (m->m_flags & M_EXT)) {
315 m->m_pkthdr.len = m->m_len = len;
316 bcopy (buf, mtod (m, caddr_t), len);
320 static int ce_probe (device_t dev)
322 if ((pci_get_vendor (dev) == TAU32_PCI_VENDOR_ID) &&
323 (pci_get_device (dev) == TAU32_PCI_DEVICE_ID)) {
324 device_set_desc (dev, "Cronyx-Tau32-PCI serial adapter");
325 return BUS_PROBE_DEFAULT;
330 static void ce_timeout (void *arg)
335 for (i = 0; i < NBRD; ++i) {
336 if (adapter[i] == NULL)
338 for (k = 0; k < NCHAN; ++k) {
344 d = channel[i * NCHAN + k];
349 CE_LOCK ((bdrv_t *)d->board->sys);
350 switch (d->chan->type) {
352 ce_e1_timer (d->chan);
357 CE_UNLOCK ((bdrv_t *)d->board->sys);
363 callout_reset (&timeout_handle, hz, ce_timeout, 0);
367 static void ce_led_off (void *arg)
370 bdrv_t *bd = (bdrv_t *) b->sys;
378 TAU32_LedSet (b->ddk.pControllerObject, 0);
383 static void ce_intr (void *arg)
386 ce_board_t *b = bd->board;
389 #if __FreeBSD_version >= 500000 && defined NETGRAPH
399 TAU32_LedSet (b->ddk.pControllerObject, 1);
401 TAU32_HandleInterrupt (b->ddk.pControllerObject);
403 /* Turn LED off 50 msec later. */
404 callout_reset (&led_timo[b->num], hz/20, ce_led_off, b);
408 /* Pass packets in a lock-free state */
409 for (i = 0; i < NCHAN && b->chan[i].type; i++) {
410 drv_t *d = b->chan[i].sys;
412 if (!d || !d->running)
414 while (_IF_QLEN(&d->rqueue)) {
415 IF_DEQUEUE (&d->rqueue,m);
420 #if __FreeBSD_version >= 500000
421 NG_SEND_DATA_ONLY (error, d->hook, m);
423 ng_queue_data (d->hook, m, 0);
426 IF_DRAIN (&d->rqueue);
429 sppp_input (d->ifp, m);
435 #if __FreeBSD_version >= 500000
437 ce_bus_dmamap_addr (void *arg, bus_dma_segment_t *segs, int nseg, int error)
444 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
446 *addr = segs->ds_addr;
450 #define BUS_DMA_ZERO 0
454 ce_bus_dma_mem_alloc (int bnum, int cnum, ce_dma_mem_t *dmem)
458 error = bus_dma_tag_create (NULL, 16, 0, BUS_SPACE_MAXADDR_32BIT,
459 BUS_SPACE_MAXADDR, NULL, NULL, dmem->size, 1,
461 #if __FreeBSD_version >= 502000
466 if (cnum >= 0) printf ("ce%d-%d: ", bnum, cnum);
467 else printf ("ce%d: ", bnum);
468 printf ("couldn't allocate tag for dma memory\n");
471 error = bus_dmamem_alloc (dmem->dmat, (void **)&dmem->virt,
472 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &dmem->mapp);
474 if (cnum >= 0) printf ("ce%d-%d: ", bnum, cnum);
475 else printf ("ce%d: ", bnum);
476 printf ("couldn't allocate mem for dma memory\n");
477 bus_dma_tag_destroy (dmem->dmat);
480 error = bus_dmamap_load (dmem->dmat, dmem->mapp, dmem->virt,
481 dmem->size, ce_bus_dmamap_addr, &dmem->phys, 0);
483 if (cnum >= 0) printf ("ce%d-%d: ", bnum, cnum);
484 else printf ("ce%d: ", bnum);
485 printf ("couldn't load mem map for dma memory\n");
486 bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
487 bus_dma_tag_destroy (dmem->dmat);
490 #if __FreeBSD_version >= 502000
491 bzero (dmem->virt, dmem->size);
497 ce_bus_dma_mem_free (ce_dma_mem_t *dmem)
499 bus_dmamap_unload (dmem->dmat, dmem->mapp);
500 bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
501 bus_dma_tag_destroy (dmem->dmat);
505 ce_bus_dma_mem_alloc (int bnum, int cnum, ce_dma_mem_t *dmem)
507 dmem->virt = contigmalloc (dmem->size, M_DEVBUF, M_WAITOK,
508 0x100000, 0xffffffff, 16, 0);
509 if (dmem->virt == NULL) {
510 if (cnum >= 0) printf ("ce%d-%d: ", bnum, cnum);
511 else printf ("ce%d: ", bnum);
512 printf ("couldn't allocate dma memory\n");
515 dmem->phys = vtophys (dmem->virt);
516 bzero (dmem->virt, dmem->size);
521 ce_bus_dma_mem_free (ce_dma_mem_t *dmem)
523 contigfree (dmem->virt, dmem->size, M_DEVBUF);
528 * Called if the probe succeeded.
530 static int ce_attach (device_t dev)
532 bdrv_t *bd = device_get_softc (dev);
533 int unit = device_get_unit (dev);
534 #if __FreeBSD_version >= 504000
535 char *ce_ln = CE_LOCK_NAME;
544 b = malloc (sizeof(ce_board_t), M_DEVBUF, M_WAITOK);
546 printf ("ce%d: couldn't allocate memory\n", unit);
549 bzero (b, sizeof(ce_board_t));
553 #if __FreeBSD_version >= 440000
554 pci_enable_busmaster (dev);
557 bd->dmamem.size = TAU32_ControllerObjectSize;
558 if (! ce_bus_dma_mem_alloc (unit, -1, &bd->dmamem)) {
562 b->ddk.pControllerObject = bd->dmamem.virt;
567 bd->ce_res = bus_alloc_resource (dev, SYS_RES_MEMORY, &rid,
568 0, ~0, 1, RF_ACTIVE);
570 printf ("ce%d: cannot map memory\n", unit);
571 ce_bus_dma_mem_free (&bd->dmamem);
575 vbase = (vm_offset_t) rman_get_virtual (bd->ce_res);
577 b->ddk.PciBar1VirtualAddress = (void *)vbase;
578 b->ddk.ControllerObjectPhysicalAddress = bd->dmamem.phys;
579 b->ddk.pErrorNotifyCallback = ce_error_callback;
580 b->ddk.pStatusNotifyCallback = ce_status_callback;
583 TAU32_BeforeReset(&b->ddk);
584 pci_write_config (dev, TAU32_PCI_RESET_ADDRESS, TAU32_PCI_RESET_ON, 4);
585 pci_write_config (dev, TAU32_PCI_RESET_ADDRESS, TAU32_PCI_RESET_OFF, 4);
587 if(!TAU32_Initialize(&b->ddk, 0))
589 printf ("ce%d: init adapter error 0x%08x, bus dead bits 0x%08lx\n",
590 unit, b->ddk.InitErrors, b->ddk.DeadBits);
591 bus_release_resource (dev, SYS_RES_MEMORY, PCIR_BAR(0), bd->ce_res);
592 ce_bus_dma_mem_free (&bd->dmamem);
602 bd->ce_irq = bus_alloc_resource (dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
603 RF_SHAREABLE | RF_ACTIVE);
605 printf ("ce%d: cannot map interrupt\n", unit);
606 bus_release_resource (dev, SYS_RES_MEMORY, PCIR_BAR(0), bd->ce_res);
607 ce_bus_dma_mem_free (&bd->dmamem);
612 #if __FreeBSD_version >= 500000
613 callout_init (&led_timo[unit], CALLOUT_MPSAFE);
615 callout_init (&led_timo[unit]);
617 error = bus_setup_intr (dev, bd->ce_irq,
618 #if __FreeBSD_version >= 500013
619 INTR_TYPE_NET|INTR_MPSAFE,
623 NULL, ce_intr, bd, &bd->ce_intrhand);
625 printf ("ce%d: cannot set up irq\n", unit);
626 bus_release_resource (dev, SYS_RES_IRQ, 0, bd->ce_irq);
627 bus_release_resource (dev, SYS_RES_MEMORY,
628 PCIR_BAR(0), bd->ce_res);
629 ce_bus_dma_mem_free (&bd->dmamem);
635 switch (b->ddk.Model) {
636 case 1: strcpy (b->name, TAU32_BASE_NAME); break;
637 case 2: strcpy (b->name, TAU32_LITE_NAME); break;
638 case 3: strcpy (b->name, TAU32_ADPCM_NAME); break;
639 default: strcpy (b->name, TAU32_UNKNOWN_NAME); break;
642 printf ("ce%d: %s\n", unit, b->name);
644 for (c = b->chan; c < b->chan + NCHAN; ++c) {
645 c->num = (c - b->chan);
648 d = &bd->channel[c->num];
649 d->dmamem.size = sizeof(ce_buf_t);
650 if (! ce_bus_dma_mem_alloc (unit, c->num, &d->dmamem))
653 channel [b->num * NCHAN + c->num] = d;
654 sprintf (d->name, "ce%d.%d", b->num, c->num);
660 for (c = b->chan; c < b->chan + NCHAN; ++c) {
665 callout_init (&d->timeout_handle, CALLOUT_MPSAFE);
667 if (ng_make_node_common (&typestruct, &d->node) != 0) {
668 printf ("%s: cannot make common node\n", d->name);
672 #if __FreeBSD_version >= 500000
673 NG_NODE_SET_PRIVATE (d->node, d);
675 d->node->private = d;
677 sprintf (d->nodename, "%s%d", NG_CE_NODE_TYPE,
678 c->board->num * NCHAN + c->num);
679 if (ng_name_node (d->node, d->nodename)) {
680 printf ("%s: cannot name node\n", d->nodename);
681 #if __FreeBSD_version >= 500000
682 NG_NODE_UNREF (d->node);
689 d->queue.ifq_maxlen = ifqmaxlen;
690 d->hi_queue.ifq_maxlen = ifqmaxlen;
691 d->rqueue.ifq_maxlen = ifqmaxlen;
692 #if __FreeBSD_version >= 500000
693 mtx_init (&d->queue.ifq_mtx, "ce_queue", NULL, MTX_DEF);
694 mtx_init (&d->hi_queue.ifq_mtx, "ce_queue_hi", NULL, MTX_DEF);
695 mtx_init (&d->rqueue.ifq_mtx, "ce_rqueue", NULL, MTX_DEF);
698 #if __FreeBSD_version >= 600031
699 d->ifp = if_alloc(IFT_PPP);
701 d->ifp = malloc (sizeof(struct sppp), M_DEVBUF, M_WAITOK);
702 bzero (d->ifp, sizeof(struct sppp));
705 printf ("%s: cannot if_alloc() interface\n", d->name);
708 d->ifp->if_softc = d;
709 #if __FreeBSD_version > 501000
710 if_initname (d->ifp, "ce", b->num * NCHAN + c->num);
712 d->ifp->if_unit = b->num * NCHAN + c->num;
713 d->ifp->if_name = "ce";
715 d->ifp->if_mtu = PP_MTU;
716 d->ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
717 d->ifp->if_ioctl = ce_sioctl;
718 d->ifp->if_start = ce_ifstart;
719 d->ifp->if_init = ce_initialize;
720 d->rqueue.ifq_maxlen = ifqmaxlen;
721 #if __FreeBSD_version >= 500000
722 mtx_init (&d->rqueue.ifq_mtx, "ce_rqueue", NULL, MTX_DEF);
724 sppp_attach (d->ifp);
726 IFP2SP(d->ifp)->pp_tlf = ce_tlf;
727 IFP2SP(d->ifp)->pp_tls = ce_tls;
728 /* If BPF is in the kernel, call the attach for it.
729 * The header size of PPP or Cisco/HDLC is 4 bytes. */
730 bpfattach (d->ifp, DLT_PPP, 4);
732 ce_start_chan (c, 1, 1, d->dmamem.virt, d->dmamem.phys);
734 /* Register callback functions. */
735 ce_register_transmit (c, &ce_transmit);
736 ce_register_receive (c, &ce_receive);
737 ce_register_error (c, &ce_error);
738 d->devt = make_dev (&ce_cdevsw, b->num*NCHAN+c->num, UID_ROOT,
739 GID_WHEEL, 0600, "ce%d", b->num*NCHAN+c->num);
742 #if __FreeBSD_version >= 504000
743 ce_ln[2] = '0' + unit;
744 mtx_init (&bd->ce_mtx, ce_ln, MTX_NETWORK_LOCK, MTX_DEF|MTX_RECURSE);
747 TAU32_EnableInterrupts(b->ddk.pControllerObject);
755 static int ce_detach (device_t dev)
757 bdrv_t *bd = device_get_softc (dev);
758 ce_board_t *b = bd->board;
762 #if __FreeBSD_version >= 504000
763 KASSERT (mtx_initialized (&bd->ce_mtx), ("ce mutex not initialized"));
767 /* Check if the device is busy (open). */
768 for (c = b->chan; c < b->chan + NCHAN; ++c) {
769 drv_t *d = (drv_t*) c->sys;
771 /* XXX Non existen chan! */
772 if (! d || ! d->chan)
781 /* Ok, we can unload driver */
782 /* At first we should disable interrupts */
784 TAU32_DisableInterrupts(b->ddk.pControllerObject);
786 callout_stop (&led_timo[b->num]);
788 for (c = b->chan; c < b->chan + NCHAN; ++c) {
789 drv_t *d = (drv_t*) c->sys;
791 if (! d || ! d->chan)
793 callout_stop (&d->timeout_handle);
795 /* Detach from the packet filter list of interfaces. */
798 /* Detach from the sync PPP list. */
799 sppp_detach (d->ifp);
801 /* Detach from the system list of interfaces. */
803 #if __FreeBSD_version > 600031
806 free (d->ifp, M_DEVBUF);
809 IF_DRAIN (&d->rqueue);
810 #if __FreeBSD_version >= 500000
811 mtx_destroy (&d->rqueue.ifq_mtx);
814 #if __FreeBSD_version >= 500000
816 ng_rmnode_self (d->node);
817 NG_NODE_UNREF (d->node);
820 IF_DRAIN (&d->rqueue);
821 mtx_destroy (&d->queue.ifq_mtx);
822 mtx_destroy (&d->hi_queue.ifq_mtx);
823 mtx_destroy (&d->rqueue.ifq_mtx);
829 destroy_dev (d->devt);
835 callout_drain (&led_timo[b->num]);
837 /* Disable the interrupt request. */
838 bus_teardown_intr (dev, bd->ce_irq, bd->ce_intrhand);
839 bus_release_resource (dev, SYS_RES_IRQ, 0, bd->ce_irq);
840 TAU32_DestructiveHalt (b->ddk.pControllerObject, 0);
841 bus_release_resource (dev, SYS_RES_MEMORY, PCIR_BAR(0), bd->ce_res);
843 for (c = b->chan; c < b->chan + NCHAN; ++c) {
844 drv_t *d = (drv_t*) c->sys;
846 if (! d || ! d->chan)
848 callout_drain (&d->timeout_handle);
849 channel [b->num * NCHAN + c->num] = 0;
850 /* Deallocate buffers. */
851 ce_bus_dma_mem_free (&d->dmamem);
853 adapter [b->num] = 0;
854 ce_bus_dma_mem_free (&bd->dmamem);
856 #if __FreeBSD_version >= 504000
857 mtx_destroy (&bd->ce_mtx);
863 static void ce_ifstart (struct ifnet *ifp)
865 drv_t *d = ifp->if_softc;
866 bdrv_t *bd = d->board->sys;
873 static void ce_tlf (struct sppp *sp)
875 drv_t *d = SP2IFP(sp)->if_softc;
877 CE_DEBUG2 (d, ("ce_tlf\n"));
881 static void ce_tls (struct sppp *sp)
883 drv_t *d = SP2IFP(sp)->if_softc;
885 CE_DEBUG2 (d, ("ce_tls\n"));
890 * Process an ioctl request.
892 static int ce_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data)
894 drv_t *d = ifp->if_softc;
895 bdrv_t *bd = d->board->sys;
896 int error, s, was_up, should_be_up;
898 #if __FreeBSD_version >= 600034
899 was_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
901 was_up = (ifp->if_flags & IFF_RUNNING) != 0;
903 error = sppp_ioctl (ifp, cmd, data);
908 if (! (ifp->if_flags & IFF_DEBUG))
911 d->chan->debug = d->chan->debug_shadow;
914 default: CE_DEBUG2 (d, ("ioctl 0x%lx\n", cmd)); return 0;
915 case SIOCADDMULTI: CE_DEBUG2 (d, ("ioctl SIOCADDMULTI\n")); return 0;
916 case SIOCDELMULTI: CE_DEBUG2 (d, ("ioctl SIOCDELMULTI\n")); return 0;
917 case SIOCSIFFLAGS: CE_DEBUG2 (d, ("ioctl SIOCSIFFLAGS\n")); break;
918 case SIOCSIFADDR: CE_DEBUG2 (d, ("ioctl SIOCSIFADDR\n")); break;
921 /* We get here only in case of SIFFLAGS or SIFADDR. */
924 #if __FreeBSD_version >= 600034
925 should_be_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
927 should_be_up = (ifp->if_flags & IFF_RUNNING) != 0;
929 if (! was_up && should_be_up) {
930 /* Interface goes up -- start it. */
933 } else if (was_up && ! should_be_up) {
934 /* Interface is going down -- stop it. */
935 /* if ((IFP2SP(ifp)->pp_flags & PP_FR) || (ifp->if_flags & PP_CISCO))*/
938 CE_DEBUG (d, ("ioctl 0x%lx p4\n", cmd));
945 * Initialization of interface.
946 * It seems to be never called by upper level?
948 static void ce_initialize (void *softc)
952 CE_DEBUG (d, ("ce_initialize\n"));
957 * Stop the interface. Called on splimp().
959 static void ce_down (drv_t *d)
961 CE_DEBUG (d, ("ce_down\n"));
962 /* Interface is going down -- stop it. */
963 ce_set_dtr (d->chan, 0);
964 ce_set_rts (d->chan, 0);
967 callout_stop (&d->timeout_handle);
971 * Start the interface. Called on splimp().
973 static void ce_up (drv_t *d)
975 CE_DEBUG (d, ("ce_up\n"));
976 ce_set_dtr (d->chan, 1);
977 ce_set_rts (d->chan, 1);
983 * Start output on the interface. Get another datagram to send
984 * off of the interface queue, and copy it to the interface
985 * before starting the output.
987 static void ce_send (drv_t *d)
992 CE_DEBUG2 (d, ("ce_send\n"));
994 /* No output if the interface is down. */
998 while (ce_transmit_space (d->chan)) {
999 /* Get the packet to send. */
1001 IF_DEQUEUE (&d->hi_queue, m);
1003 IF_DEQUEUE (&d->queue, m);
1005 m = sppp_dequeue (d->ifp);
1010 #if __FreeBSD_version >= 500000
1011 BPF_MTAP (d->ifp, m);
1014 bpf_mtap (d->ifp, m);
1017 #if __FreeBSD_version >= 490000
1018 len = m_length (m, NULL);
1020 len = m->m_pkthdr.len;
1023 printf ("%s: too long packet: %d bytes: ",
1025 else if (! m->m_next)
1026 ce_send_packet (d->chan, (u_char*) mtod (m, caddr_t), len, 0);
1028 ce_buf_item_t *item = (ce_buf_item_t*)d->chan->tx_queue;
1029 m_copydata (m, 0, len, item->buf);
1030 ce_send_packet (d->chan, item->buf, len, 0);
1033 /* Set up transmit timeout, if the transmit ring is not empty.*/
1037 #if __FreeBSD_version >= 600034
1038 d->ifp->if_flags |= IFF_DRV_OACTIVE;
1040 d->ifp->if_flags |= IFF_OACTIVE;
1046 * Start output on the interface.
1047 * Always called on splimp().
1049 static void ce_start (drv_t *d)
1053 ce_set_dtr (d->chan, 1);
1055 ce_set_rts (d->chan, 1);
1057 callout_reset (&d->timeout_handle, hz, ce_watchdog_timer, d);
1062 * Handle transmit timeouts.
1063 * Recover after lost transmit interrupts.
1064 * Always called on splimp().
1066 static void ce_watchdog (drv_t *d)
1068 CE_DEBUG (d, ("device timeout\n"));
1070 ce_set_dtr (d->chan, 0);
1071 ce_set_rts (d->chan, 0);
1072 /* ce_stop_chan (d->chan);*/
1073 /* ce_start_chan (d->chan, 1, 1, 0, 0);*/
1074 ce_set_dtr (d->chan, 1);
1075 ce_set_rts (d->chan, 1);
1080 static void ce_watchdog_timer (void *arg)
1083 bdrv_t *bd = d->board->sys;
1086 if (d->timeout == 1)
1090 callout_reset (&d->timeout_handle, hz, ce_watchdog_timer, d);
1094 static void ce_transmit (ce_chan_t *c, void *attachment, int len)
1100 if_inc_counter(d->ifp, IFCOUNTER_OPACKETS, 1);
1101 #if __FreeBSD_version >= 600034
1102 d->ifp->if_flags &= ~IFF_DRV_OACTIVE;
1104 d->ifp->if_flags &= ~IFF_OACTIVE;
1110 static void ce_receive (ce_chan_t *c, unsigned char *data, int len)
1118 m = makembuf (data, len);
1120 CE_DEBUG (d, ("no memory for packet\n"));
1122 if_inc_counter(d->ifp, IFCOUNTER_IQDROPS, 1);
1129 m->m_pkthdr.rcvif = 0;
1130 IF_ENQUEUE(&d->rqueue, m);
1132 if_inc_counter(d->ifp, IFCOUNTER_IPACKETS, 1);
1133 m->m_pkthdr.rcvif = d->ifp;
1134 /* Check if there's a BPF listener on this interface.
1135 * If so, hand off the raw packet to bpf. */
1136 BPF_MTAP(d->ifp, m);
1137 IF_ENQUEUE(&d->rqueue, m);
1141 static void ce_error (ce_chan_t *c, int data)
1147 CE_DEBUG (d, ("frame error\n"));
1149 if_inc_counter(d->ifp, IFCOUNTER_IERRORS, 1);
1153 CE_DEBUG (d, ("crc error\n"));
1155 if_inc_counter(d->ifp, IFCOUNTER_IERRORS, 1);
1159 CE_DEBUG (d, ("overrun error\n"));
1161 if_inc_counter(d->ifp, IFCOUNTER_COLLISIONS, 1);
1162 if_inc_counter(d->ifp, IFCOUNTER_IERRORS, 1);
1166 CE_DEBUG (d, ("overflow error\n"));
1168 if_inc_counter(d->ifp, IFCOUNTER_IERRORS, 1);
1172 CE_DEBUG (d, ("underrun error\n"));
1175 if_inc_counter(d->ifp, IFCOUNTER_OERRORS, 1);
1176 #if __FreeBSD_version >= 600034
1177 d->ifp->if_flags &= ~IFF_DRV_OACTIVE;
1179 d->ifp->if_flags &= ~IFF_OACTIVE;
1185 CE_DEBUG (d, ("error #%d\n", data));
1191 * You also need read, write, open, close routines.
1192 * This should get you started
1194 #if __FreeBSD_version < 500000
1195 static int ce_open (dev_t dev, int oflags, int devtype, struct proc *p)
1197 static int ce_open (struct cdev *dev, int oflags, int devtype, struct thread *td)
1200 int unit = dev2unit (dev);
1203 if (unit >= NBRD*NCHAN || ! (d = channel[unit]))
1205 CE_DEBUG2 (d, ("ce_open\n"));
1210 * Only called on the LAST close.
1212 #if __FreeBSD_version < 500000
1213 static int ce_close (dev_t dev, int fflag, int devtype, struct proc *p)
1215 static int ce_close (struct cdev *dev, int fflag, int devtype, struct thread *td)
1218 drv_t *d = channel [dev2unit (dev)];
1220 CE_DEBUG2 (d, ("ce_close\n"));
1224 static int ce_modem_status (ce_chan_t *c)
1227 bdrv_t *bd = d->board->sys;
1230 status = d->running ? TIOCM_LE : 0;
1233 if (ce_get_cd (c)) status |= TIOCM_CD;
1234 if (ce_get_cts (c)) status |= TIOCM_CTS;
1235 if (ce_get_dsr (c)) status |= TIOCM_DSR;
1236 if (c->dtr) status |= TIOCM_DTR;
1237 if (c->rts) status |= TIOCM_RTS;
1243 #if __FreeBSD_version < 500000
1244 static int ce_ioctl (dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
1246 static int ce_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
1249 drv_t *d = channel [dev2unit (dev)];
1250 bdrv_t *bd = d->board->sys;
1251 ce_chan_t *c = d->chan;
1252 struct serial_statistics *st;
1253 struct e1_statistics *opte1;
1258 case SERIAL_GETREGISTERED:
1259 CE_DEBUG2 (d, ("ioctl: getregistered\n"));
1260 bzero (mask, sizeof(mask));
1261 for (s=0; s<NBRD*NCHAN; ++s)
1263 mask [s/8] |= 1 << (s & 7);
1264 bcopy (mask, data, sizeof (mask));
1268 case SERIAL_GETPROTO:
1269 CE_DEBUG2 (d, ("ioctl: getproto\n"));
1270 strcpy ((char*)data, (IFP2SP(d->ifp)->pp_flags & PP_FR) ? "fr" :
1271 (d->ifp->if_flags & PP_CISCO) ? "cisco" : "ppp");
1274 case SERIAL_SETPROTO:
1275 CE_DEBUG2 (d, ("ioctl: setproto\n"));
1276 /* Only for superuser! */
1277 #if __FreeBSD_version < 500000
1279 #elif __FreeBSD_version < 700000
1282 error = priv_check (td, PRIV_DRIVER);
1286 #if __FreeBSD_version >= 600034
1287 if (d->ifp->if_flags & IFF_DRV_RUNNING)
1289 if (d->ifp->if_flags & IFF_RUNNING)
1292 if (! strcmp ("cisco", (char*)data)) {
1293 IFP2SP(d->ifp)->pp_flags &= ~(PP_FR);
1294 IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE;
1295 d->ifp->if_flags |= PP_CISCO;
1297 } else if (! strcmp ("fr", (char*)data)) {
1298 d->ifp->if_flags &= ~(PP_CISCO);
1299 IFP2SP(d->ifp)->pp_flags |= PP_FR | PP_KEEPALIVE;
1301 } else if (! strcmp ("ppp", (char*)data)) {
1302 IFP2SP(d->ifp)->pp_flags &= ~PP_FR;
1303 IFP2SP(d->ifp)->pp_flags &= ~PP_KEEPALIVE;
1304 d->ifp->if_flags &= ~(PP_CISCO);
1309 case SERIAL_GETKEEPALIVE:
1310 CE_DEBUG2 (d, ("ioctl: getkeepalive\n"));
1311 if ((IFP2SP(d->ifp)->pp_flags & PP_FR) ||
1312 (d->ifp->if_flags & PP_CISCO))
1314 *(int*)data = (IFP2SP(d->ifp)->pp_flags & PP_KEEPALIVE) ? 1 : 0;
1317 case SERIAL_SETKEEPALIVE:
1318 CE_DEBUG2 (d, ("ioctl: setkeepalive\n"));
1319 /* Only for superuser! */
1320 #if __FreeBSD_version < 500000
1322 #elif __FreeBSD_version < 700000
1325 error = priv_check (td, PRIV_DRIVER);
1329 if ((IFP2SP(d->ifp)->pp_flags & PP_FR) ||
1330 (d->ifp->if_flags & PP_CISCO))
1335 IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE;
1337 IFP2SP(d->ifp)->pp_flags &= ~PP_KEEPALIVE;
1343 case SERIAL_GETMODE:
1344 CE_DEBUG2 (d, ("ioctl: getmode\n"));
1345 *(int*)data = SERIAL_HDLC;
1348 case SERIAL_SETMODE:
1349 /* Only for superuser! */
1350 #if __FreeBSD_version < 500000
1352 #elif __FreeBSD_version < 700000
1355 error = priv_check (td, PRIV_DRIVER);
1359 if (*(int*)data != SERIAL_HDLC)
1364 CE_DEBUG2 (d, ("ioctl: getcfg\n"));
1369 CE_DEBUG2 (d, ("ioctl: setcfg\n"));
1370 #if __FreeBSD_version < 500000
1372 #elif __FreeBSD_version < 700000
1375 error = priv_check (td, PRIV_DRIVER);
1379 if (*((char*)data) != 'c')
1383 case SERIAL_GETSTAT:
1384 CE_DEBUG2 (d, ("ioctl: getstat\n"));
1385 st = (struct serial_statistics*) data;
1386 st->rintr = c->rintr;
1387 st->tintr = c->tintr;
1389 st->ibytes = c->ibytes;
1390 st->ipkts = c->ipkts;
1391 st->obytes = c->obytes;
1392 st->opkts = c->opkts;
1393 st->ierrs = c->overrun + c->frame + c->crc;
1394 st->oerrs = c->underrun;
1397 case SERIAL_GETESTAT:
1398 CE_DEBUG2 (d, ("ioctl: getestat\n"));
1399 if (c->type != T_E1)
1401 opte1 = (struct e1_statistics*) data;
1404 if (c->status & ESTS_NOALARM)
1405 opte1->status |= E1_NOALARM;
1406 if (c->status & ESTS_LOS)
1407 opte1->status |= E1_LOS;
1408 if (c->status & ESTS_LOF)
1409 opte1->status |= E1_LOF;
1410 if (c->status & ESTS_AIS)
1411 opte1->status |= E1_AIS;
1412 if (c->status & ESTS_LOMF)
1413 opte1->status |= E1_LOMF;
1414 if (c->status & ESTS_AIS16)
1415 opte1->status |= E1_AIS16;
1416 if (c->status & ESTS_FARLOF)
1417 opte1->status |= E1_FARLOF;
1418 if (c->status & ESTS_FARLOMF)
1419 opte1->status |= E1_FARLOMF;
1420 if (c->status & ESTS_TSTREQ)
1421 opte1->status |= E1_TSTREQ;
1422 if (c->status & ESTS_TSTERR)
1423 opte1->status |= E1_TSTERR;
1425 opte1->cursec = c->cursec;
1426 opte1->totsec = c->totsec + c->cursec;
1428 opte1->currnt.bpv = c->currnt.bpv;
1429 opte1->currnt.fse = c->currnt.fse;
1430 opte1->currnt.crce = c->currnt.crce;
1431 opte1->currnt.rcrce = c->currnt.rcrce;
1432 opte1->currnt.uas = c->currnt.uas;
1433 opte1->currnt.les = c->currnt.les;
1434 opte1->currnt.es = c->currnt.es;
1435 opte1->currnt.bes = c->currnt.bes;
1436 opte1->currnt.ses = c->currnt.ses;
1437 opte1->currnt.oofs = c->currnt.oofs;
1438 opte1->currnt.css = c->currnt.css;
1439 opte1->currnt.dm = c->currnt.dm;
1441 opte1->total.bpv = c->total.bpv + c->currnt.bpv;
1442 opte1->total.fse = c->total.fse + c->currnt.fse;
1443 opte1->total.crce = c->total.crce + c->currnt.crce;
1444 opte1->total.rcrce = c->total.rcrce + c->currnt.rcrce;
1445 opte1->total.uas = c->total.uas + c->currnt.uas;
1446 opte1->total.les = c->total.les + c->currnt.les;
1447 opte1->total.es = c->total.es + c->currnt.es;
1448 opte1->total.bes = c->total.bes + c->currnt.bes;
1449 opte1->total.ses = c->total.ses + c->currnt.ses;
1450 opte1->total.oofs = c->total.oofs + c->currnt.oofs;
1451 opte1->total.css = c->total.css + c->currnt.css;
1452 opte1->total.dm = c->total.dm + c->currnt.dm;
1453 for (s=0; s<48; ++s) {
1454 opte1->interval[s].bpv = c->interval[s].bpv;
1455 opte1->interval[s].fse = c->interval[s].fse;
1456 opte1->interval[s].crce = c->interval[s].crce;
1457 opte1->interval[s].rcrce = c->interval[s].rcrce;
1458 opte1->interval[s].uas = c->interval[s].uas;
1459 opte1->interval[s].les = c->interval[s].les;
1460 opte1->interval[s].es = c->interval[s].es;
1461 opte1->interval[s].bes = c->interval[s].bes;
1462 opte1->interval[s].ses = c->interval[s].ses;
1463 opte1->interval[s].oofs = c->interval[s].oofs;
1464 opte1->interval[s].css = c->interval[s].css;
1465 opte1->interval[s].dm = c->interval[s].dm;
1469 case SERIAL_CLRSTAT:
1470 CE_DEBUG2 (d, ("ioctl: clrstat\n"));
1471 /* Only for superuser! */
1472 #if __FreeBSD_version < 500000
1474 #elif __FreeBSD_version < 700000
1477 error = priv_check (td, PRIV_DRIVER);
1491 bzero (&c->currnt, sizeof (c->currnt));
1492 bzero (&c->total, sizeof (c->total));
1493 bzero (c->interval, sizeof (c->interval));
1496 case SERIAL_GETLOOP:
1497 CE_DEBUG2 (d, ("ioctl: getloop\n"));
1498 if (c->type != T_E1)
1500 *(int*)data = c->lloop;
1503 case SERIAL_SETLOOP:
1504 CE_DEBUG2 (d, ("ioctl: setloop\n"));
1505 if (c->type != T_E1)
1507 /* Only for superuser! */
1508 #if __FreeBSD_version < 500000
1510 #elif __FreeBSD_version < 700000
1513 error = priv_check (td, PRIV_DRIVER);
1519 ce_set_lloop (c, *(int*)data);
1524 case SERIAL_GETRLOOP:
1525 CE_DEBUG2 (d, ("ioctl: getrloop\n"));
1526 if (c->type != T_E1)
1528 *(int*)data = c->rloop;
1531 case SERIAL_SETRLOOP:
1532 CE_DEBUG2 (d, ("ioctl: setloop\n"));
1533 if (c->type != T_E1)
1535 /* Only for superuser! */
1536 #if __FreeBSD_version < 500000
1538 #elif __FreeBSD_version < 700000
1541 error = priv_check (td, PRIV_DRIVER);
1547 ce_set_rloop (c, *(int*)data);
1552 case SERIAL_GETDEBUG:
1553 CE_DEBUG2 (d, ("ioctl: getdebug\n"));
1554 *(int*)data = d->chan->debug;
1557 case SERIAL_SETDEBUG:
1558 CE_DEBUG2 (d, ("ioctl: setdebug\n"));
1559 /* Only for superuser! */
1560 #if __FreeBSD_version < 500000
1562 #elif __FreeBSD_version < 700000
1565 error = priv_check (td, PRIV_DRIVER);
1571 * The debug_shadow is always greater than zero for logic
1572 * simplicity. For switching debug off the IFF_DEBUG is
1575 d->chan->debug_shadow = (*(int*)data) ? (*(int*)data) : 1;
1576 if (d->ifp->if_flags & IFF_DEBUG)
1577 d->chan->debug = d->chan->debug_shadow;
1579 d->chan->debug = *(int*)data;
1583 case SERIAL_GETBAUD:
1584 CE_DEBUG2 (d, ("ioctl: getbaud\n"));
1585 *(long*)data = c->baud;
1588 case SERIAL_SETBAUD:
1589 CE_DEBUG2 (d, ("ioctl: setbaud\n"));
1590 if (c->type != T_E1 || !c->unfram)
1592 /* Only for superuser! */
1593 #if __FreeBSD_version < 500000
1595 #elif __FreeBSD_version < 700000
1598 error = priv_check (td, PRIV_DRIVER);
1604 ce_set_baud (c, *(long*)data);
1609 case SERIAL_GETTIMESLOTS:
1610 CE_DEBUG2 (d, ("ioctl: gettimeslots\n"));
1611 if ((c->type != T_E1 || c->unfram) && c->type != T_DATA)
1613 *(u_long*)data = c->ts;
1616 case SERIAL_SETTIMESLOTS:
1617 CE_DEBUG2 (d, ("ioctl: settimeslots\n"));
1618 /* Only for superuser! */
1619 #if __FreeBSD_version < 500000
1621 #elif __FreeBSD_version < 700000
1624 error = priv_check (td, PRIV_DRIVER);
1628 if ((c->type != T_E1 || c->unfram) && c->type != T_DATA)
1632 ce_set_ts (c, *(u_long*)data);
1637 case SERIAL_GETHIGAIN:
1638 CE_DEBUG2 (d, ("ioctl: gethigain\n"));
1639 if (c->type != T_E1)
1641 *(int*)data = c->higain;
1644 case SERIAL_SETHIGAIN:
1645 CE_DEBUG2 (d, ("ioctl: sethigain\n"));
1646 if (c->type != T_E1)
1648 /* Only for superuser! */
1649 #if __FreeBSD_version < 500000
1651 #elif __FreeBSD_version < 700000
1654 error = priv_check (td, PRIV_DRIVER);
1660 ce_set_higain (c, *(int*)data);
1665 case SERIAL_GETPHONY:
1666 CE_DEBUG2 (d, ("ioctl: getphony\n"));
1667 *(int*)data = c->phony;
1670 case SERIAL_SETPHONY:
1671 CE_DEBUG2 (d, ("ioctl: setphony\n"));
1672 /* Only for superuser! */
1673 #if __FreeBSD_version < 500000
1675 #elif __FreeBSD_version < 700000
1678 error = priv_check (td, PRIV_DRIVER);
1684 ce_set_phony (c, *(int*)data);
1689 case SERIAL_GETUNFRAM:
1690 CE_DEBUG2 (d, ("ioctl: getunfram\n"));
1691 if (c->type != T_E1 || c->num != 0)
1693 *(int*)data = c->unfram;
1696 case SERIAL_SETUNFRAM:
1697 CE_DEBUG2 (d, ("ioctl: setunfram\n"));
1698 if (c->type != T_E1 || c->num != 0)
1700 /* Only for superuser! */
1701 #if __FreeBSD_version < 500000
1703 #elif __FreeBSD_version < 700000
1706 error = priv_check (td, PRIV_DRIVER);
1712 ce_set_unfram (c, *(int*)data);
1717 case SERIAL_GETSCRAMBLER:
1718 CE_DEBUG2 (d, ("ioctl: getscrambler\n"));
1721 *(int*)data = c->scrambler;
1724 case SERIAL_SETSCRAMBLER:
1725 CE_DEBUG2 (d, ("ioctl: setscrambler\n"));
1726 /* Only for superuser! */
1727 #if __FreeBSD_version < 500000
1729 #elif __FreeBSD_version < 700000
1732 error = priv_check (td, PRIV_DRIVER);
1740 ce_set_scrambler (c, *(int*)data);
1745 case SERIAL_GETMONITOR:
1746 CE_DEBUG2 (d, ("ioctl: getmonitor\n"));
1747 if (c->type != T_E1)
1749 *(int*)data = c->monitor;
1752 case SERIAL_SETMONITOR:
1753 CE_DEBUG2 (d, ("ioctl: setmonitor\n"));
1754 /* Only for superuser! */
1755 #if __FreeBSD_version < 500000
1757 #elif __FreeBSD_version < 700000
1760 error = priv_check (td, PRIV_DRIVER);
1764 if (c->type != T_E1)
1768 ce_set_monitor (c, *(int*)data);
1773 case SERIAL_GETUSE16:
1774 CE_DEBUG2 (d, ("ioctl: getuse16\n"));
1775 if (c->type != T_E1 || c->unfram)
1777 *(int*)data = c->use16;
1780 case SERIAL_SETUSE16:
1781 CE_DEBUG2 (d, ("ioctl: setuse16\n"));
1782 /* Only for superuser! */
1783 #if __FreeBSD_version < 500000
1785 #elif __FreeBSD_version < 700000
1788 error = priv_check (td, PRIV_DRIVER);
1792 if (c->type != T_E1)
1796 ce_set_use16 (c, *(int*)data);
1801 case SERIAL_GETCRC4:
1802 CE_DEBUG2 (d, ("ioctl: getcrc4\n"));
1803 if (c->type != T_E1 || c->unfram)
1805 *(int*)data = c->crc4;
1808 case SERIAL_SETCRC4:
1809 CE_DEBUG2 (d, ("ioctl: setcrc4\n"));
1810 /* Only for superuser! */
1811 #if __FreeBSD_version < 500000
1813 #elif __FreeBSD_version < 700000
1816 error = priv_check (td, PRIV_DRIVER);
1820 if (c->type != T_E1 || c->unfram)
1824 ce_set_crc4 (c, *(int*)data);
1830 CE_DEBUG2 (d, ("ioctl: getclk\n"));
1831 if (c->type != T_E1)
1834 default: *(int*)data = E1CLK_INTERNAL; break;
1835 case GSYN_RCV: *(int*)data = E1CLK_RECEIVE; break;
1836 case GSYN_RCV0: *(int*)data = E1CLK_RECEIVE_CHAN0; break;
1837 case GSYN_RCV1: *(int*)data = E1CLK_RECEIVE_CHAN1; break;
1842 CE_DEBUG2 (d, ("ioctl: setclk\n"));
1843 /* Only for superuser! */
1844 #if __FreeBSD_version < 500000
1846 #elif __FreeBSD_version < 700000
1849 error = priv_check (td, PRIV_DRIVER);
1853 if (c->type != T_E1)
1857 switch (*(int*)data) {
1858 default: ce_set_gsyn (c, GSYN_INT); break;
1859 case E1CLK_RECEIVE: ce_set_gsyn (c, GSYN_RCV); break;
1860 case E1CLK_RECEIVE_CHAN0: ce_set_gsyn (c, GSYN_RCV0); break;
1861 case E1CLK_RECEIVE_CHAN1: ce_set_gsyn (c, GSYN_RCV1); break;
1869 CE_DEBUG2 (d, ("ioctl: reset\n"));
1870 /* Only for superuser! */
1871 #if __FreeBSD_version < 500000
1873 #elif __FreeBSD_version < 700000
1876 error = priv_check (td, PRIV_DRIVER);
1882 /* ce_reset (c->board, 0, 0);*/
1887 case SERIAL_HARDRESET:
1888 CE_DEBUG2 (d, ("ioctl: hardreset\n"));
1889 /* Only for superuser! */
1890 #if __FreeBSD_version < 500000
1892 #elif __FreeBSD_version < 700000
1895 error = priv_check (td, PRIV_DRIVER);
1901 /* hard_reset (c->board); */
1907 case SERIAL_GETCABLE:
1908 CE_DEBUG2 (d, ("ioctl: getcable\n"));
1909 if (c->type != T_E1)
1913 *(int*)data = CABLE_TP;
1919 CE_DEBUG2 (d, ("ioctl: getdir\n"));
1920 if (c->type != T_E1 && c->type != T_DATA)
1922 *(int*)data = c->dir;
1926 CE_DEBUG2 (d, ("ioctl: setdir\n"));
1927 /* Only for superuser! */
1928 #if __FreeBSD_version < 500000
1930 #elif __FreeBSD_version < 700000
1933 error = priv_check (td, PRIV_DRIVER);
1939 ce_set_dir (c, *(int*)data);
1944 case TIOCSDTR: /* Set DTR */
1952 case TIOCCDTR: /* Clear DTR */
1960 case TIOCMSET: /* Set DTR/RTS */
1963 ce_set_dtr (c, (*(int*)data & TIOCM_DTR) ? 1 : 0);
1964 ce_set_rts (c, (*(int*)data & TIOCM_RTS) ? 1 : 0);
1969 case TIOCMBIS: /* Add DTR/RTS */
1972 if (*(int*)data & TIOCM_DTR) ce_set_dtr (c, 1);
1973 if (*(int*)data & TIOCM_RTS) ce_set_rts (c, 1);
1978 case TIOCMBIC: /* Clear DTR/RTS */
1981 if (*(int*)data & TIOCM_DTR) ce_set_dtr (c, 0);
1982 if (*(int*)data & TIOCM_RTS) ce_set_rts (c, 0);
1987 case TIOCMGET: /* Get modem status */
1988 *(int*)data = ce_modem_status (c);
1995 #if __FreeBSD_version >= 500000
1996 static int ng_ce_constructor (node_p node)
1998 drv_t *d = NG_NODE_PRIVATE (node);
2000 static int ng_ce_constructor (node_p *node)
2002 drv_t *d = (*node)->private;
2004 CE_DEBUG (d, ("Constructor\n"));
2008 static int ng_ce_newhook (node_p node, hook_p hook, const char *name)
2011 #if __FreeBSD_version >= 500000
2012 drv_t *d = NG_NODE_PRIVATE (node);
2014 drv_t *d = node->private;
2016 bdrv_t *bd = d->board->sys;
2018 CE_DEBUG (d, ("Newhook\n"));
2019 /* Attach debug hook */
2020 if (strcmp (name, NG_CE_HOOK_DEBUG) == 0) {
2021 #if __FreeBSD_version >= 500000
2022 NG_HOOK_SET_PRIVATE (hook, NULL);
2026 d->debug_hook = hook;
2030 /* Check for raw hook */
2031 if (strcmp (name, NG_CE_HOOK_RAW) != 0)
2034 #if __FreeBSD_version >= 500000
2035 NG_HOOK_SET_PRIVATE (hook, d);
2048 static char *format_timeslots (u_long s)
2050 static char buf [100];
2054 for (i=1; i<32; ++i)
2056 int prev = (i > 1) & (s >> (i-1));
2057 int next = (i < 31) & (s >> (i+1));
2067 *p++ = '0' + i / 10;
2068 *p++ = '0' + i % 10;
2074 static int print_modems (char *s, ce_chan_t *c, int need_header)
2076 int status = ce_modem_status (c);
2080 length += sprintf (s + length, " LE DTR DSR RTS CTS CD\n");
2081 length += sprintf (s + length, "%4s %4s %4s %4s %4s %4s\n",
2082 status & TIOCM_LE ? "On" : "-",
2083 status & TIOCM_DTR ? "On" : "-",
2084 status & TIOCM_DSR ? "On" : "-",
2085 status & TIOCM_RTS ? "On" : "-",
2086 status & TIOCM_CTS ? "On" : "-",
2087 status & TIOCM_CD ? "On" : "-");
2091 static int print_stats (char *s, ce_chan_t *c, int need_header)
2096 length += sprintf (s + length, " Rintr Tintr Mintr Ibytes Ipkts Ierrs Obytes Opkts Oerrs\n");
2097 length += sprintf (s + length, "%7ld %7ld %7ld %8lu %7ld %7ld %8lu %7ld %7ld\n",
2098 c->rintr, c->tintr, 0l, (unsigned long) c->ibytes,
2099 c->ipkts, c->overrun + c->frame + c->crc,
2100 (unsigned long) c->obytes, c->opkts, c->underrun);
2104 static char *format_e1_status (u_char status)
2106 static char buf [80];
2108 if (status & E1_NOALARM)
2111 if (status & E1_LOS) strcat (buf, ",LOS");
2112 if (status & E1_AIS) strcat (buf, ",AIS");
2113 if (status & E1_LOF) strcat (buf, ",LOF");
2114 if (status & E1_LOMF) strcat (buf, ",LOMF");
2115 if (status & E1_FARLOF) strcat (buf, ",FARLOF");
2116 if (status & E1_AIS16) strcat (buf, ",AIS16");
2117 if (status & E1_FARLOMF) strcat (buf, ",FARLOMF");
2118 if (status & E1_TSTREQ) strcat (buf, ",TSTREQ");
2119 if (status & E1_TSTERR) strcat (buf, ",TSTERR");
2125 static int print_frac (char *s, int leftalign, u_long numerator, u_long divider)
2129 if (numerator < 1 || divider < 1) {
2130 length += sprintf (s+length, leftalign ? "/- " : " -");
2133 n = (int) (0.5 + 1000.0 * numerator / divider);
2135 length += sprintf (s+length, leftalign ? "/.%-3d" : " .%03d", n);
2138 *(s + length) = leftalign ? '/' : ' ';
2141 if (n >= 1000000) n = (n+500) / 1000 * 1000;
2142 else if (n >= 100000) n = (n+50) / 100 * 100;
2143 else if (n >= 10000) n = (n+5) / 10 * 10;
2146 case 1000: length += printf (s+length, ".999"); return length;
2147 case 10000: n = 9990; break;
2148 case 100000: n = 99900; break;
2149 case 1000000: n = 999000; break;
2151 if (n < 10000) length += sprintf (s+length, "%d.%d", n/1000, n/10%100);
2152 else if (n < 100000) length += sprintf (s+length, "%d.%d", n/1000, n/100%10);
2153 else if (n < 1000000) length += sprintf (s+length, "%d.", n/1000);
2154 else length += sprintf (s+length, "%d", n/1000);
2159 static int print_e1_stats (char *s, ce_chan_t *c)
2161 struct e1_counters total;
2165 totsec = c->totsec + c->cursec;
2166 total.bpv = c->total.bpv + c->currnt.bpv;
2167 total.fse = c->total.fse + c->currnt.fse;
2168 total.crce = c->total.crce + c->currnt.crce;
2169 total.rcrce = c->total.rcrce + c->currnt.rcrce;
2170 total.uas = c->total.uas + c->currnt.uas;
2171 total.les = c->total.les + c->currnt.les;
2172 total.es = c->total.es + c->currnt.es;
2173 total.bes = c->total.bes + c->currnt.bes;
2174 total.ses = c->total.ses + c->currnt.ses;
2175 total.oofs = c->total.oofs + c->currnt.oofs;
2176 total.css = c->total.css + c->currnt.css;
2177 total.dm = c->total.dm + c->currnt.dm;
2179 length += sprintf (s + length, " Unav/Degr Bpv/Fsyn CRC/RCRC Err/Lerr Sev/Bur Oof/Slp Status\n");
2181 /* Unavailable seconds, degraded minutes */
2182 length += print_frac (s + length, 0, c->currnt.uas, c->cursec);
2183 length += print_frac (s + length, 1, 60 * c->currnt.dm, c->cursec);
2185 /* Bipolar violations, frame sync errors */
2186 length += print_frac (s + length, 0, c->currnt.bpv, c->cursec);
2187 length += print_frac (s + length, 1, c->currnt.fse, c->cursec);
2189 /* CRC errors, remote CRC errors (E-bit) */
2190 length += print_frac (s + length, 0, c->currnt.crce, c->cursec);
2191 length += print_frac (s + length, 1, c->currnt.rcrce, c->cursec);
2193 /* Errored seconds, line errored seconds */
2194 length += print_frac (s + length, 0, c->currnt.es, c->cursec);
2195 length += print_frac (s + length, 1, c->currnt.les, c->cursec);
2197 /* Severely errored seconds, burst errored seconds */
2198 length += print_frac (s + length, 0, c->currnt.ses, c->cursec);
2199 length += print_frac (s + length, 1, c->currnt.bes, c->cursec);
2201 /* Out of frame seconds, controlled slip seconds */
2202 length += print_frac (s + length, 0, c->currnt.oofs, c->cursec);
2203 length += print_frac (s + length, 1, c->currnt.css, c->cursec);
2205 length += sprintf (s + length, " %s\n", format_e1_status (c->status));
2207 /* Print total statistics. */
2208 length += print_frac (s + length, 0, total.uas, totsec);
2209 length += print_frac (s + length, 1, 60 * total.dm, totsec);
2211 length += print_frac (s + length, 0, total.bpv, totsec);
2212 length += print_frac (s + length, 1, total.fse, totsec);
2214 length += print_frac (s + length, 0, total.crce, totsec);
2215 length += print_frac (s + length, 1, total.rcrce, totsec);
2217 length += print_frac (s + length, 0, total.es, totsec);
2218 length += print_frac (s + length, 1, total.les, totsec);
2220 length += print_frac (s + length, 0, total.ses, totsec);
2221 length += print_frac (s + length, 1, total.bes, totsec);
2223 length += print_frac (s + length, 0, total.oofs, totsec);
2224 length += print_frac (s + length, 1, total.css, totsec);
2226 length += sprintf (s + length, " -- Total\n");
2230 static int print_chan (char *s, ce_chan_t *c)
2235 length += sprintf (s + length, "ce%d", c->board->num * NCHAN + c->num);
2237 length += sprintf (s + length, " debug=%d", d->chan->debug);
2239 if (c->board->mux) {
2240 length += sprintf (s + length, " cfg=C");
2242 length += sprintf (s + length, " cfg=A");
2246 length += sprintf (s + length, " %ld", c->baud);
2248 length += sprintf (s + length, " extclock");
2250 if (c->type == T_E1)
2252 case GSYN_INT : length += sprintf (s + length, " syn=int"); break;
2253 case GSYN_RCV : length += sprintf (s + length, " syn=rcv"); break;
2254 case GSYN_RCV0 : length += sprintf (s + length, " syn=rcv0"); break;
2255 case GSYN_RCV1 : length += sprintf (s + length, " syn=rcv1"); break;
2257 if (c->type == T_E1)
2258 length += sprintf (s + length, " higain=%s", c->higain ? "on" : "off");
2260 length += sprintf (s + length, " loop=%s", c->lloop ? "on" : "off");
2262 if (c->type == T_E1)
2263 length += sprintf (s + length, " ts=%s", format_timeslots (c->ts));
2264 length += sprintf (s + length, "\n");
2268 #if __FreeBSD_version >= 500000
2269 static int ng_ce_rcvmsg (node_p node, item_p item, hook_p lasthook)
2271 drv_t *d = NG_NODE_PRIVATE (node);
2272 struct ng_mesg *msg;
2274 static int ng_ce_rcvmsg (node_p node, struct ng_mesg *msg,
2275 const char *retaddr, struct ng_mesg **rptr)
2277 drv_t *d = node->private;
2279 struct ng_mesg *resp = NULL;
2282 CE_DEBUG (d, ("Rcvmsg\n"));
2283 #if __FreeBSD_version >= 500000
2284 NGI_GET_MSG (item, msg);
2286 switch (msg->header.typecookie) {
2292 printf ("Not implemented yet\n");
2296 case NGM_GENERIC_COOKIE:
2297 switch (msg->header.cmd) {
2302 case NGM_TEXT_STATUS: {
2305 int dl = sizeof (struct ng_mesg) + 730;
2307 #if __FreeBSD_version >= 500000
2308 NG_MKRESPONSE (resp, msg, dl, M_NOWAIT);
2314 resp = malloc (M_NETGRAPH, M_NOWAIT);
2323 l += print_chan (s + l, d->chan);
2324 l += print_stats (s + l, d->chan, 1);
2325 l += print_modems (s + l, d->chan, 1);
2326 l += print_e1_stats (s + l, d->chan);
2328 l += sprintf (s + l, "Error: node not connect to channel");
2329 #if __FreeBSD_version < 500000
2330 (resp)->header.version = NG_VERSION;
2331 (resp)->header.arglen = strlen (s) + 1;
2332 (resp)->header.token = msg->header.token;
2333 (resp)->header.typecookie = NGM_CE_COOKIE;
2334 (resp)->header.cmd = msg->header.cmd;
2336 strncpy ((resp)->header.cmdstr, "status", NG_CMDSTRSIZ);
2342 #if __FreeBSD_version >= 500000
2343 NG_RESPOND_MSG (error, node, item, resp);
2347 free (msg, M_NETGRAPH);
2352 #if __FreeBSD_version >= 500000
2353 static int ng_ce_rcvdata (hook_p hook, item_p item)
2355 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE(hook));
2357 #if __FreeBSD_version < 502120
2360 struct ng_tag_prio *ptag;
2363 static int ng_ce_rcvdata (hook_p hook, struct mbuf *m, meta_p meta)
2365 drv_t *d = hook->node->private;
2367 bdrv_t *bd = d->board->sys;
2371 CE_DEBUG2 (d, ("Rcvdata\n"));
2372 #if __FreeBSD_version >= 500000
2373 NGI_GET_M (item, m);
2374 #if __FreeBSD_version < 502120
2375 NGI_GET_META (item, meta);
2377 NG_FREE_ITEM (item);
2378 if (! NG_HOOK_PRIVATE (hook) || ! d) {
2380 #if __FreeBSD_version < 502120
2381 NG_FREE_META (meta);
2384 if (! hook->private || ! d) {
2385 NG_FREE_DATA (m,meta);
2390 #if __FreeBSD_version >= 502120
2391 /* Check for high priority data */
2392 if ((ptag = (struct ng_tag_prio *)m_tag_locate(m, NGM_GENERIC_COOKIE,
2393 NG_TAG_PRIO, NULL)) != NULL && (ptag->priority > NG_PRIO_CUTOFF) )
2398 q = (meta && meta->priority > 0) ? &d->hi_queue : &d->queue;
2403 #if __FreeBSD_version >= 500000
2405 if (_IF_QFULL (q)) {
2410 #if __FreeBSD_version < 502120
2411 NG_FREE_META (meta);
2422 NG_FREE_DATA (m, meta);
2433 static int ng_ce_rmnode (node_p node)
2435 #if __FreeBSD_version >= 500000
2436 drv_t *d = NG_NODE_PRIVATE (node);
2438 CE_DEBUG (d, ("Rmnode\n"));
2439 if (d && d->running) {
2440 bdrv_t *bd = d->board->sys;
2448 #if __FreeBSD_version >= 502120
2449 if (node->nd_flags & NGF_REALLY_DIE) {
2451 if (node->nd_flags & NG_REALLY_DIE) {
2453 NG_NODE_SET_PRIVATE (node, NULL);
2454 NG_NODE_UNREF (node);
2456 #if __FreeBSD_version >= 502120
2457 NG_NODE_REVIVE(node); /* Persistant node */
2459 node->nd_flags &= ~NG_INVALID;
2462 #else /* __FreeBSD_version < 500000 */
2463 drv_t *d = node->private;
2465 if (d && d->running) {
2466 bdrv_t *bd = d->board->sys;
2474 node->flags |= NG_INVALID;
2484 static int ng_ce_connect (hook_p hook)
2486 #if __FreeBSD_version >= 500000
2487 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2489 drv_t *d = hook->node->private;
2493 CE_DEBUG (d, ("Connect\n"));
2494 callout_reset (&d->timeout_handle, hz, ce_watchdog_timer, d);
2500 static int ng_ce_disconnect (hook_p hook)
2502 #if __FreeBSD_version >= 500000
2503 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2505 drv_t *d = hook->node->private;
2509 CE_DEBUG (d, ("Disconnect\n"));
2510 #if __FreeBSD_version >= 500000
2511 if (NG_HOOK_PRIVATE (hook))
2516 bdrv_t *bd = d->board->sys;
2523 /* If we were wait it than it reasserted now, just stop it. */
2524 if (!callout_drain (&d->timeout_handle))
2525 callout_stop (&d->timeout_handle);
2531 static int ce_modevent (module_t mod, int type, void *unused)
2533 #if __FreeBSD_version < 500000
2535 struct cdevsw *cdsw;
2537 static int load_count = 0;
2539 #if __FreeBSD_version < 500000
2540 dev = makedev (CDEV_MAJOR, 0);
2545 #if __FreeBSD_version < 500000
2547 (cdsw = devsw (dev)) &&
2548 cdsw->d_maj == CDEV_MAJOR) {
2549 printf ("Tau32-PCI driver is already in system\n");
2553 #if __FreeBSD_version >= 500000 && defined NETGRAPH
2554 if (ng_newtype (&typestruct))
2555 printf ("Failed to register ng_ce\n");
2558 #if __FreeBSD_version <= 500000
2559 cdevsw_add (&ce_cdevsw);
2561 #if __FreeBSD_version >= 500000
2562 callout_init (&timeout_handle, CALLOUT_MPSAFE);
2564 callout_init (&timeout_handle);
2566 callout_reset (&timeout_handle, hz*5, ce_timeout, 0);
2569 if (load_count == 1) {
2570 printf ("Removing device entry for Tau32-PCI\n");
2571 #if __FreeBSD_version <= 500000
2572 cdevsw_remove (&ce_cdevsw);
2574 #if __FreeBSD_version >= 500000 && defined NETGRAPH
2575 ng_rmtype (&typestruct);
2578 /* If we were wait it than it reasserted now, just stop it.
2579 * Actually we shouldn't get this condition. But code could be
2580 * changed in the future, so just be a litle paranoid.
2582 if (!callout_drain (&timeout_handle))
2583 callout_stop (&timeout_handle);
2593 #if __FreeBSD_version >= 502100
2594 static struct ng_type typestruct = {
2595 .version = NG_ABI_VERSION,
2596 .name = NG_CE_NODE_TYPE,
2597 .constructor = ng_ce_constructor,
2598 .rcvmsg = ng_ce_rcvmsg,
2599 .shutdown = ng_ce_rmnode,
2600 .newhook = ng_ce_newhook,
2601 .connect = ng_ce_connect,
2602 .rcvdata = ng_ce_rcvdata,
2603 .disconnect = ng_ce_disconnect,
2605 #else /* __FreeBSD_version < 502100 */
2606 static struct ng_type typestruct = {
2607 #if __FreeBSD_version >= 500000
2621 #if __FreeBSD_version < 500000
2627 #endif /* __FreeBSD_version < 502100 */
2631 #if __FreeBSD_version >= 500000
2633 MODULE_DEPEND (ng_ce, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION);
2635 MODULE_DEPEND (ce, sppp, 1, 1, 1);
2638 DRIVER_MODULE (cemod, pci, ce_driver, ce_devclass, ce_modevent, NULL);
2640 DRIVER_MODULE (ce, pci, ce_driver, ce_devclass, ce_modevent, NULL);
2642 #else /* if __FreeBSD_version < 500000*/
2644 DRIVER_MODULE (ce, pci, ce_driver, ce_devclass, ng_mod_event, &typestruct);
2646 DRIVER_MODULE (ce, pci, ce_driver, ce_devclass, ce_modevent, NULL);
2648 #endif /* __FreeBSD_version < 500000 */