2 * Cronyx-Sigma adapter driver for FreeBSD.
3 * Supports PPP/HDLC and Cisco/HDLC protocol in synchronous mode,
4 * and asyncronous channels with full modem control.
5 * Keepalive protocol implemented in both Cisco and PPP modes.
7 * Copyright (C) 1994-2002 Cronyx Engineering.
8 * Author: Serge Vakulenko, <vak@cronyx.ru>
10 * Copyright (C) 1999-2004 Cronyx Engineering.
11 * Rewritten on DDK, ported to NETGRAPH, rewritten for FreeBSD 3.x-5.x by
12 * Kurakin Roman, <rik@cronyx.ru>
14 * This software is distributed with NO WARRANTIES, not even the implied
15 * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 * Authors grant any other persons or organisations a permission to use,
18 * modify and redistribute this software in source and binary forms,
19 * as long as this message is kept with the software, all derivative
20 * works or modified versions.
22 * Cronyx Id: if_cx.c,v 1.1.2.34 2004/06/23 17:09:13 rik Exp $
25 #include <sys/cdefs.h>
26 __FBSDID("$FreeBSD$");
28 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/module.h>
35 #include <sys/sockio.h>
36 #include <sys/malloc.h>
37 #include <sys/socket.h>
38 #include <sys/sysctl.h>
40 #include <sys/errno.h>
41 #include <sys/serial.h>
44 #include <machine/bus.h>
46 #include <isa/isavar.h>
47 #include <sys/fcntl.h>
48 #include <sys/interrupt.h>
52 #include <machine/cpufunc.h>
53 #include <machine/cserial.h>
54 #include <machine/clock.h>
55 #include <machine/resource.h>
56 #include <dev/cx/machdep.h>
57 #include <dev/cx/cxddk.h>
58 #include <dev/cx/cronyxfw.h>
59 #include "opt_ng_cronyx.h"
60 #ifdef NETGRAPH_CRONYX
61 # include "opt_netgraph.h"
62 # include <netgraph/ng_message.h>
63 # include <netgraph/netgraph.h>
64 # include <dev/cx/ng_cx.h>
66 # include <net/if_types.h>
67 # include <net/if_sppp.h>
68 # define PP_CISCO IFF_LINK2
74 /* If we don't have Cronyx's sppp version, we don't have fr support via sppp */
79 #define CX_DEBUG(d,s) ({if (d->chan->debug) {\
80 printf ("%s: ", d->name); printf s;}})
81 #define CX_DEBUG2(d,s) ({if (d->chan->debug>1) {\
82 printf ("%s: ", d->name); printf s;}})
84 #define CX_LOCK_NAME "cxX"
86 static int cx_mpsafenet = 1;
87 TUNABLE_INT("debug.cx.mpsafenet", &cx_mpsafenet);
88 SYSCTL_NODE(_debug, OID_AUTO, cx, CTLFLAG_RD, 0, "Cronyx Sigma Adapters");
89 SYSCTL_INT(_debug_cx, OID_AUTO, mpsafenet, CTLFLAG_RD, &cx_mpsafenet, 0,
90 "Enable/disable MPSAFE network support for Cronyx Sigma Adapters");
92 #define CX_LOCK(_bd) do { \
94 mtx_lock (&(_bd)->cx_mtx); \
96 #define CX_UNLOCK(_bd) do { \
98 mtx_unlock (&(_bd)->cx_mtx); \
100 #define CX_LOCK_ASSERT(_bd) do { \
102 mtx_assert (&(_bd)->cx_mtx, MA_OWNED); \
105 typedef struct _async_q {
112 #define AQ_GSZ(q) ((BF_SZ + (q)->end - (q)->beg)%BF_SZ)
113 #define AQ_PUSH(q,c) {*((q)->buf + (q)->end) = c;\
114 (q)->end = ((q)->end + 1)%BF_SZ;}
115 #define AQ_POP(q,c) {c = *((q)->buf + (q)->beg);\
116 (q)->beg = ((q)->beg + 1)%BF_SZ;}
118 static void cx_identify __P((driver_t *, device_t));
119 static int cx_probe __P((device_t));
120 static int cx_attach __P((device_t));
121 static int cx_detach __P((device_t));
122 static t_open_t cx_topen;
123 static t_modem_t cx_tmodem;
124 static t_close_t cx_tclose;
126 static device_method_t cx_isa_methods [] = {
127 DEVMETHOD(device_identify, cx_identify),
128 DEVMETHOD(device_probe, cx_probe),
129 DEVMETHOD(device_attach, cx_attach),
130 DEVMETHOD(device_detach, cx_detach),
134 typedef struct _cx_dma_mem_t {
142 typedef struct _drv_t {
148 struct callout dcd_timeout_handle;
155 char nodename [NG_NODELEN+1];
159 struct ifqueue lo_queue;
160 struct ifqueue hi_queue;
162 struct callout timeout_handle;
164 struct ifqueue queue;
175 typedef struct _bdrv_t {
177 struct resource *base_res;
178 struct resource *drq_res;
179 struct resource *irq_res;
184 drv_t channel [NCHAN];
188 static driver_t cx_isa_driver = {
194 static devclass_t cx_devclass;
196 extern long csigma_fw_len;
197 extern const char *csigma_fw_version;
198 extern const char *csigma_fw_date;
199 extern const char *csigma_fw_copyright;
200 extern const cr_dat_tst_t csigma_fw_tvec[];
201 extern const u_char csigma_fw_data[];
202 static void cx_oproc (struct tty *tp);
203 static int cx_param (struct tty *tp, struct termios *t);
204 static void cx_stop (struct tty *tp, int flag);
205 static void cx_receive (cx_chan_t *c, char *data, int len);
206 static void cx_transmit (cx_chan_t *c, void *attachment, int len);
207 static void cx_error (cx_chan_t *c, int data);
208 static void cx_modem (cx_chan_t *c);
209 static void cx_up (drv_t *d);
210 static void cx_start (drv_t *d);
211 static void cx_softintr (void *);
212 static void *cx_fast_ih;
213 static void cx_down (drv_t *d);
214 static void cx_watchdog (drv_t *d);
215 static void cx_carrier (void *arg);
218 extern struct ng_type typestruct;
220 static void cx_ifstart (struct ifnet *ifp);
221 static void cx_tlf (struct sppp *sp);
222 static void cx_tls (struct sppp *sp);
223 static void cx_ifwatchdog (struct ifnet *ifp);
224 static int cx_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data);
225 static void cx_initialize (void *softc);
228 static cx_board_t *adapter [NCX];
229 static drv_t *channel [NCX*NCHAN];
230 static struct callout led_timo [NCX];
231 static struct callout timeout_handle;
232 extern struct cdevsw cx_cdevsw;
234 static int MY_SOFT_INTR;
237 * Print the mbuf chain, for debug purposes only.
239 static void printmbuf (struct mbuf *m)
242 for (; m; m=m->m_next) {
243 if (m->m_flags & M_PKTHDR)
244 printf (" HDR %d:", m->m_pkthdr.len);
245 if (m->m_flags & M_EXT)
247 printf (" %d", m->m_len);
253 * Make an mbuf from data.
255 static struct mbuf *makembuf (void *buf, u_int len)
257 struct mbuf *m, *o, *p;
259 MGETHDR (m, M_DONTWAIT, MT_DATA);
264 if (len >= MINCLSIZE)
265 MCLGET (m, M_DONTWAIT);
267 m->m_pkthdr.len = len;
272 u_int n = M_TRAILINGSPACE (p);
276 /* Allocate new mbuf. */
278 MGET (p, M_DONTWAIT, MT_DATA);
283 if (len >= MINCLSIZE)
284 MCLGET (p, M_DONTWAIT);
288 n = M_TRAILINGSPACE (p);
292 bcopy (buf, mtod (p, caddr_t) + p->m_len, n);
295 buf = n + (char*) buf;
302 * Recover after lost transmit interrupts.
304 static void cx_timeout (void *arg)
309 for (i = 0; i < NCX; i++) {
310 if (adapter[i] == NULL)
312 for (k = 0; k < NCHAN; ++k) {
313 d = channel[i * NCHAN + k];
317 CX_LOCK ((bdrv_t *)d->board->sys);
318 if (d->atimeout == 1 && d->tty && d->tty->t_state & TS_BUSY) {
319 d->tty->t_state &= ~TS_BUSY;
321 d->intr_action |= CX_WRITE;
323 swi_sched (cx_fast_ih, 0);
325 CX_DEBUG (d, ("cx_timeout\n"));
329 CX_UNLOCK ((bdrv_t *)d->board->sys);
333 callout_reset (&timeout_handle, hz*5, cx_timeout, 0);
336 static void cx_led_off (void *arg)
350 * Activate interupt handler from DDK.
352 static void cx_intr (void *arg)
355 cx_board_t *b = bd->board;
367 /* Turn LED off 50 msec later. */
368 callout_reset (&led_timo[b->num], hz/20, cx_led_off, b);
373 /* Pass packets in a lock-free state */
374 for (i = 0; i < NCHAN && b->chan[i].type; i++) {
375 drv_t *d = b->chan[i].sys;
377 while (_IF_QLEN(&d->queue)) {
378 IF_DEQUEUE (&d->queue,m);
381 sppp_input (&d->pp.pp_if, m);
387 static int probe_irq (cx_board_t *b, int irq)
391 /* Clear pending irq, if any. */
392 cx_probe_irq (b, -irq);
394 for (cnt=0; cnt<5; ++cnt) {
395 /* Get the mask of pending irqs, assuming they are busy.
396 * Activate the adapter on given irq. */
397 busy = cx_probe_irq (b, irq);
400 /* Get the mask of active irqs.
401 * Deactivate our irq. */
402 mask = cx_probe_irq (b, -irq);
404 if ((mask & ~busy) == 1 << irq) {
406 /* printf ("cx%d: irq %d ok, mask=0x%04x, busy=0x%04x\n",
407 b->num, irq, mask, busy); */
411 /* printf ("cx%d: irq %d not functional, mask=0x%04x, busy=0x%04x\n",
412 b->num, irq, mask, busy); */
417 static short porttab [] = {
418 0x200, 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0,
419 0x300, 0x320, 0x340, 0x360, 0x380, 0x3a0, 0x3c0, 0x3e0, 0
421 static char dmatab [] = { 7, 6, 5, 0 };
422 static char irqtab [] = { 5, 10, 11, 7, 3, 15, 12, 0 };
424 static int cx_is_free_res (device_t dev, int rid, int type, u_long start,
425 u_long end, u_long count)
427 struct resource *res;
429 if (!(res = bus_alloc_resource (dev, type, &rid, start, end, count,
433 bus_release_resource (dev, type, rid, res);
438 static void cx_identify (driver_t *driver, device_t dev)
440 u_long iobase, rescount;
444 devclass_t my_devclass;
447 if ((my_devclass = devclass_find ("cx")) == NULL)
450 devclass_get_devices (my_devclass, &devices, &devcount);
453 /* We should find all devices by our self. We could alter other
454 * devices, but we don't have a choise
456 for (i = 0; (iobase = porttab [i]) != 0; i++) {
457 if (!cx_is_free_res (dev, 0, SYS_RES_IOPORT,
458 iobase, iobase + NPORT, NPORT))
460 if (cx_probe_board (iobase, -1, -1) == 0)
465 child = BUS_ADD_CHILD (dev, ISA_ORDER_SPECULATIVE, "cx",
471 device_set_desc_copy (child, "Cronyx Sigma");
472 device_set_driver (child, driver);
473 bus_set_resource (child, SYS_RES_IOPORT, 0,
480 static short porttab [] = {
481 0x200, 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0,
482 0x300, 0x320, 0x340, 0x360, 0x380, 0x3a0, 0x3c0, 0x3e0, 0
484 /* Lets check user choise.
486 for (k = 0; k < devcount; k++) {
487 if (bus_get_resource (devices[k], SYS_RES_IOPORT, 0,
488 &iobase, &rescount) != 0)
491 for (i = 0; porttab [i] != 0; i++) {
492 if (porttab [i] != iobase)
494 if (!cx_is_free_res (devices[k], 0, SYS_RES_IOPORT,
495 iobase, iobase + NPORT, NPORT))
497 if (cx_probe_board (iobase, -1, -1) == 0)
500 device_set_desc_copy (devices[k], "Cronyx Sigma");
504 if (porttab [i] == 0) {
505 device_delete_child (
506 device_get_parent (devices[k]),
512 for (k = 0; k < devcount; k++) {
515 if (bus_get_resource (devices[k], SYS_RES_IOPORT, 0,
516 &iobase, &rescount) == 0)
518 for (i = 0; (iobase = porttab [i]) != 0; i++) {
519 if (porttab [i] == -1) {
522 if (!cx_is_free_res (devices[k], 0, SYS_RES_IOPORT,
523 iobase, iobase + NPORT, NPORT))
525 if (cx_probe_board (iobase, -1, -1) == 0)
528 bus_set_resource (devices[k], SYS_RES_IOPORT, 0,
531 device_set_desc_copy (devices[k], "Cronyx Sigma");
534 if (porttab [i] == 0) {
535 device_delete_child (
536 device_get_parent (devices[k]),
540 free (devices, M_TEMP);
546 static int cx_probe (device_t dev)
548 int unit = device_get_unit (dev);
550 u_long iobase, rescount;
552 if (!device_get_desc (dev) ||
553 strcmp (device_get_desc (dev), "Cronyx Sigma"))
556 if (bus_get_resource (dev, SYS_RES_IOPORT, 0, &iobase, &rescount) != 0) {
557 printf ("cx%d: Couldn't get IOPORT\n", unit);
561 if (!cx_is_free_res (dev, 0, SYS_RES_IOPORT,
562 iobase, iobase + NPORT, NPORT)) {
563 printf ("cx%d: Resource IOPORT isn't free %lx\n", unit, iobase);
567 for (i = 0; porttab [i] != 0; i++) {
568 if (porttab [i] == iobase) {
574 if (porttab [i] == 0) {
578 if (!cx_probe_board (iobase, -1, -1)) {
579 printf ("cx%d: probing for Sigma at %lx faild\n", unit, iobase);
587 cx_bus_dmamap_addr (void *arg, bus_dma_segment_t *segs, int nseg, int error)
594 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
596 *addr = segs->ds_addr;
600 cx_bus_dma_mem_alloc (int bnum, int cnum, cx_dma_mem_t *dmem)
604 error = bus_dma_tag_create (NULL, 16, 0, BUS_SPACE_MAXADDR_24BIT,
605 BUS_SPACE_MAXADDR, NULL, NULL, dmem->size, 1,
606 dmem->size, 0, NULL, NULL, &dmem->dmat);
608 if (cnum >= 0) printf ("cx%d-%d: ", bnum, cnum);
609 else printf ("cx%d: ", bnum);
610 printf ("couldn't allocate tag for dma memory\n");
613 error = bus_dmamem_alloc (dmem->dmat, (void **)&dmem->virt,
614 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &dmem->mapp);
616 if (cnum >= 0) printf ("cx%d-%d: ", bnum, cnum);
617 else printf ("cx%d: ", bnum);
618 printf ("couldn't allocate mem for dma memory\n");
619 bus_dma_tag_destroy (dmem->dmat);
622 error = bus_dmamap_load (dmem->dmat, dmem->mapp, dmem->virt,
623 dmem->size, cx_bus_dmamap_addr, &dmem->phys, 0);
625 if (cnum >= 0) printf ("cx%d-%d: ", bnum, cnum);
626 else printf ("cx%d: ", bnum);
627 printf ("couldn't load mem map for dma memory\n");
628 bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
629 bus_dma_tag_destroy (dmem->dmat);
636 cx_bus_dma_mem_free (cx_dma_mem_t *dmem)
638 bus_dmamap_unload (dmem->dmat, dmem->mapp);
639 bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
640 bus_dma_tag_destroy (dmem->dmat);
644 * The adapter is present, initialize the driver structures.
646 static int cx_attach (device_t dev)
648 bdrv_t *bd = device_get_softc (dev);
649 u_long iobase, drq, irq, rescount;
650 int unit = device_get_unit (dev);
651 char *cx_ln = CX_LOCK_NAME;
658 KASSERT ((bd != NULL), ("cx%d: NULL device softc\n", unit));
660 bus_get_resource (dev, SYS_RES_IOPORT, 0, &iobase, &rescount);
662 bd->base_res = bus_alloc_resource (dev, SYS_RES_IOPORT, &bd->base_rid,
663 iobase, iobase + NPORT, NPORT, RF_ACTIVE);
664 if (! bd->base_res) {
665 printf ("cx%d: cannot allocate base address\n", unit);
669 if (bus_get_resource (dev, SYS_RES_DRQ, 0, &drq, &rescount) != 0) {
670 for (i = 0; (drq = dmatab [i]) != 0; i++) {
671 if (!cx_is_free_res (dev, 0, SYS_RES_DRQ,
674 bus_set_resource (dev, SYS_RES_DRQ, 0, drq, 1);
678 if (dmatab[i] == 0) {
679 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
681 printf ("cx%d: Couldn't get DRQ\n", unit);
687 bd->drq_res = bus_alloc_resource (dev, SYS_RES_DRQ, &bd->drq_rid,
688 drq, drq + 1, 1, RF_ACTIVE);
690 printf ("cx%d: cannot allocate drq\n", unit);
691 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
696 if (bus_get_resource (dev, SYS_RES_IRQ, 0, &irq, &rescount) != 0) {
697 for (i = 0; (irq = irqtab [i]) != 0; i++) {
698 if (!cx_is_free_res (dev, 0, SYS_RES_IRQ,
701 bus_set_resource (dev, SYS_RES_IRQ, 0, irq, 1);
705 if (irqtab[i] == 0) {
706 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
708 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
710 printf ("cx%d: Couldn't get IRQ\n", unit);
716 bd->irq_res = bus_alloc_resource (dev, SYS_RES_IRQ, &bd->irq_rid,
717 irq, irq + 1, 1, RF_ACTIVE);
719 printf ("cx%d: Couldn't allocate irq\n", unit);
720 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
722 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
727 b = malloc (sizeof (cx_board_t), M_DEVBUF, M_WAITOK);
729 printf ("cx:%d: Couldn't allocate memory\n", unit);
733 bzero (b, sizeof(cx_board_t));
735 if (! cx_open_board (b, unit, iobase, irq, drq)) {
736 printf ("cx%d: error loading firmware\n", unit);
738 bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid,
740 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
742 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
749 cx_ln[2] = '0' + unit;
750 mtx_init (&bd->cx_mtx, cx_ln, MTX_NETWORK_LOCK, MTX_DEF|MTX_RECURSE);
751 if (! probe_irq (b, irq)) {
752 printf ("cx%d: irq %ld not functional\n", unit, irq);
755 mtx_destroy (&bd->cx_mtx);
757 bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid,
759 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
761 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
766 callout_init (&led_timo[b->num], cx_mpsafenet ? CALLOUT_MPSAFE : 0);
768 if (bus_setup_intr (dev, bd->irq_res,
769 INTR_TYPE_NET|(cx_mpsafenet?INTR_MPSAFE:0),
770 cx_intr, bd, &bd->intrhand)) {
771 printf ("cx%d: Can't setup irq %ld\n", unit, irq);
775 mtx_destroy (&bd->cx_mtx);
777 bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid,
779 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
781 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
788 cx_init (b, b->num, b->port, irq, drq);
789 cx_setup_board (b, 0, 0, 0);
792 printf ("cx%d: <Cronyx-Sigma-%s>\n", b->num, b->name);
794 for (c=b->chan; c<b->chan+NCHAN; ++c) {
795 if (c->type == T_NONE)
797 d = &bd->channel[c->num];
798 d->dmamem.size = sizeof(cx_buf_t);
799 if (! cx_bus_dma_mem_alloc (unit, c->num, &d->dmamem))
805 channel [b->num*NCHAN + c->num] = d;
806 sprintf (d->name, "cx%d.%d", b->num, c->num);
817 if (ng_make_node_common (&typestruct, &d->node) != 0) {
818 printf ("%s: cannot make common node\n", d->name);
819 channel [b->num*NCHAN + c->num] = 0;
821 cx_bus_dma_mem_free (&d->dmamem);
824 NG_NODE_SET_PRIVATE (d->node, d);
825 sprintf (d->nodename, "%s%d", NG_CX_NODE_TYPE,
826 c->board->num*NCHAN + c->num);
827 if (ng_name_node (d->node, d->nodename)) {
828 printf ("%s: cannot name node\n", d->nodename);
829 NG_NODE_UNREF (d->node);
830 channel [b->num*NCHAN + c->num] = 0;
832 cx_bus_dma_mem_free (&d->dmamem);
835 d->lo_queue.ifq_maxlen = IFQ_MAXLEN;
836 d->hi_queue.ifq_maxlen = IFQ_MAXLEN;
837 mtx_init (&d->lo_queue.ifq_mtx, "cx_queue_lo", NULL, MTX_DEF);
838 mtx_init (&d->hi_queue.ifq_mtx, "cx_queue_hi", NULL, MTX_DEF);
839 callout_init (&d->timeout_handle,
840 cx_mpsafenet ? CALLOUT_MPSAFE : 0);
842 d->pp.pp_if.if_softc = d;
843 if_initname (&d->pp.pp_if, "cx", b->num * NCHAN + c->num);
844 d->pp.pp_if.if_mtu = PP_MTU;
845 d->pp.pp_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST |
847 d->pp.pp_if.if_ioctl = cx_sioctl;
848 d->pp.pp_if.if_start = cx_ifstart;
849 d->pp.pp_if.if_watchdog = cx_ifwatchdog;
850 d->pp.pp_if.if_init = cx_initialize;
851 d->queue.ifq_maxlen = 2;
852 mtx_init (&d->queue.ifq_mtx, "cx_queue", NULL, MTX_DEF);
853 sppp_attach (&d->pp.pp_if);
854 if_attach (&d->pp.pp_if);
855 d->pp.pp_tlf = cx_tlf;
856 d->pp.pp_tls = cx_tls;
857 /* If BPF is in the kernel, call the attach for it.
858 * Size of PPP header is 4 bytes. */
859 bpfattach (&d->pp.pp_if, DLT_PPP, 4);
862 d->tty = ttyalloc ();
863 d->tty->t_open = cx_topen;
864 d->tty->t_close = cx_tclose;
865 d->tty->t_param = cx_param;
866 d->tty->t_stop = cx_stop;
867 d->tty->t_modem = cx_tmodem;
870 cx_start_chan (c, d->dmamem.virt, d->dmamem.phys);
871 cx_register_receive (c, &cx_receive);
872 cx_register_transmit (c, &cx_transmit);
873 cx_register_error (c, &cx_error);
874 cx_register_modem (c, &cx_modem);
877 ttycreate(d->tty, NULL, 0, MINOR_CALLOUT,
878 "x%r%r", b->num, c->num);
879 d->devt = make_dev (&cx_cdevsw, b->num*NCHAN + c->num + 64, UID_ROOT, GID_WHEEL, 0600, "cx%d", b->num*NCHAN + c->num);
880 d->devt->si_drv1 = d;
881 callout_init (&d->dcd_timeout_handle,
882 cx_mpsafenet ? CALLOUT_MPSAFE : 0);
889 static int cx_detach (device_t dev)
891 bdrv_t *bd = device_get_softc (dev);
892 cx_board_t *b = bd->board;
896 KASSERT (mtx_initialized (&bd->cx_mtx), ("cx mutex not initialized"));
900 /* Check if the device is busy (open). */
901 for (c = b->chan; c < b->chan + NCHAN; ++c) {
902 drv_t *d = (drv_t*) c->sys;
904 if (!d || d->chan->type == T_NONE)
911 if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN) &&
924 /* Deactivate the timeout routine. And soft interrupt*/
925 callout_stop (&led_timo[b->num]);
927 for (c = b->chan; c < b->chan + NCHAN; ++c) {
930 if (!d || d->chan->type == T_NONE)
933 callout_stop (&d->dcd_timeout_handle);
936 bus_teardown_intr (dev, bd->irq_res, bd->intrhand);
937 bus_deactivate_resource (dev, SYS_RES_IRQ, bd->irq_rid, bd->irq_res);
938 bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid, bd->irq_res);
940 bus_deactivate_resource (dev, SYS_RES_DRQ, bd->drq_rid, bd->drq_res);
941 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid, bd->drq_res);
943 bus_deactivate_resource (dev, SYS_RES_IOPORT, bd->base_rid, bd->irq_res);
944 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid, bd->base_res);
949 /* Detach the interfaces, free buffer memory. */
950 for (c = b->chan; c < b->chan + NCHAN; ++c) {
951 drv_t *d = (drv_t*) c->sys;
953 if (!d || d->chan->type == T_NONE)
963 ng_rmnode_self (d->node);
964 NG_NODE_UNREF (d->node);
967 mtx_destroy (&d->lo_queue.ifq_mtx);
968 mtx_destroy (&d->hi_queue.ifq_mtx);
970 /* Detach from the packet filter list of interfaces. */
971 bpfdetach (&d->pp.pp_if);
972 /* Detach from the sync PPP list. */
973 sppp_detach (&d->pp.pp_if);
975 if_detach (&d->pp.pp_if);
976 /* XXXRIK: check interconnection with irq handler */
977 IF_DRAIN (&d->queue);
978 mtx_destroy (&d->queue.ifq_mtx);
980 destroy_dev (d->devt);
985 callout_drain (&led_timo[b->num]);
986 for (c = b->chan; c < b->chan + NCHAN; ++c) {
989 if (!d || d->chan->type == T_NONE)
992 callout_drain (&d->dcd_timeout_handle);
997 for (c = b->chan; c < b->chan + NCHAN; ++c) {
998 drv_t *d = (drv_t*) c->sys;
1000 if (!d || d->chan->type == T_NONE)
1003 /* Deallocate buffers. */
1004 cx_bus_dma_mem_free (&d->dmamem);
1007 adapter [b->num] = 0;
1011 mtx_destroy (&bd->cx_mtx);
1017 static void cx_ifstart (struct ifnet *ifp)
1019 drv_t *d = ifp->if_softc;
1020 bdrv_t *bd = d->board->sys;
1027 static void cx_ifwatchdog (struct ifnet *ifp)
1029 drv_t *d = ifp->if_softc;
1034 static void cx_tlf (struct sppp *sp)
1036 drv_t *d = sp->pp_if.if_softc;
1038 CX_DEBUG (d, ("cx_tlf\n"));
1039 /* cx_set_dtr (d->chan, 0);*/
1040 /* cx_set_rts (d->chan, 0);*/
1041 if (!(d->pp.pp_flags & PP_FR) && !(d->pp.pp_if.if_flags & PP_CISCO))
1045 static void cx_tls (struct sppp *sp)
1047 drv_t *d = sp->pp_if.if_softc;
1049 CX_DEBUG (d, ("cx_tls\n"));
1050 if (!(d->pp.pp_flags & PP_FR) && !(d->pp.pp_if.if_flags & PP_CISCO))
1055 * Initialization of interface.
1056 * It seems to be never called by upper level.
1058 static void cx_initialize (void *softc)
1062 CX_DEBUG (d, ("cx_initialize\n"));
1066 * Process an ioctl request.
1068 static int cx_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data)
1070 drv_t *d = ifp->if_softc;
1071 bdrv_t *bd = d->board->sys;
1072 int error, s, was_up, should_be_up;
1074 /* No socket ioctls while the channel is in async mode. */
1075 if (d->chan->type == T_NONE || d->chan->mode == M_ASYNC)
1078 /* Socket ioctls on slave subchannels are not allowed. */
1079 was_up = (ifp->if_flags & IFF_RUNNING) != 0;
1080 error = sppp_ioctl (ifp, cmd, data);
1084 if (! (ifp->if_flags & IFF_DEBUG))
1086 else if (! d->chan->debug)
1090 default: CX_DEBUG2 (d, ("ioctl 0x%lx\n", cmd)); return 0;
1091 case SIOCADDMULTI: CX_DEBUG2 (d, ("SIOCADDMULTI\n")); return 0;
1092 case SIOCDELMULTI: CX_DEBUG2 (d, ("SIOCDELMULTI\n")); return 0;
1093 case SIOCSIFFLAGS: CX_DEBUG2 (d, ("SIOCSIFFLAGS\n")); break;
1094 case SIOCSIFADDR: CX_DEBUG2 (d, ("SIOCSIFADDR\n")); break;
1097 /* We get here only in case of SIFFLAGS or SIFADDR. */
1100 should_be_up = (ifp->if_flags & IFF_RUNNING) != 0;
1101 if (!was_up && should_be_up) {
1102 /* Interface goes up -- start it. */
1105 } else if (was_up && !should_be_up) {
1106 /* Interface is going down -- stop it. */
1107 /* if ((d->pp.pp_flags & PP_FR) || (ifp->if_flags & PP_CISCO))*/
1117 * Stop the interface. Called on splimp().
1119 static void cx_down (drv_t *d)
1122 CX_DEBUG (d, ("cx_down\n"));
1123 cx_set_dtr (d->chan, 0);
1124 cx_set_rts (d->chan, 0);
1130 * Start the interface. Called on splimp().
1132 static void cx_up (drv_t *d)
1135 CX_DEBUG (d, ("cx_up\n"));
1136 cx_set_dtr (d->chan, 1);
1137 cx_set_rts (d->chan, 1);
1143 * Start output on the (slave) interface. Get another datagram to send
1144 * off of the interface queue, and copy it to the interface
1145 * before starting the output.
1147 static void cx_send (drv_t *d)
1152 CX_DEBUG2 (d, ("cx_send\n"));
1154 /* No output if the interface is down. */
1158 /* No output if the modem is off. */
1159 if (! cx_get_dsr (d->chan) && ! cx_get_loop(d->chan))
1162 if (cx_buf_free (d->chan)) {
1163 /* Get the packet to send. */
1165 IF_DEQUEUE (&d->hi_queue, m);
1167 IF_DEQUEUE (&d->lo_queue, m);
1169 m = sppp_dequeue (&d->pp.pp_if);
1174 if (d->pp.pp_if.if_bpf)
1175 BPF_MTAP (&d->pp.pp_if, m);
1177 len = m->m_pkthdr.len;
1179 cx_send_packet (d->chan, (u_char*)mtod (m, caddr_t),
1182 u_char buf [DMABUFSZ];
1183 m_copydata (m, 0, len, buf);
1184 cx_send_packet (d->chan, buf, len, 0);
1188 /* Set up transmit timeout, 10 seconds. */
1192 d->pp.pp_if.if_timer = 10;
1196 d->pp.pp_if.if_flags |= IFF_OACTIVE;
1201 * Start output on the interface.
1202 * Always called on splimp().
1204 static void cx_start (drv_t *d)
1209 cx_set_dtr (d->chan, 1);
1211 cx_set_rts (d->chan, 1);
1218 * Handle transmit timeouts.
1219 * Recover after lost transmit interrupts.
1220 * Always called on splimp().
1222 static void cx_watchdog (drv_t *d)
1224 bdrv_t *bd = d->board->sys;
1228 CX_DEBUG (d, ("device timeout\n"));
1230 cx_setup_chan (d->chan);
1231 cx_start_chan (d->chan, 0, 0);
1232 cx_set_dtr (d->chan, 1);
1233 cx_set_rts (d->chan, 1);
1241 * Transmit callback function.
1243 static void cx_transmit (cx_chan_t *c, void *attachment, int len)
1250 if (c->mode == M_ASYNC && d->tty) {
1251 d->tty->t_state &= ~(TS_BUSY | TS_FLUSH);
1253 if (d->tty->t_dev) {
1254 d->intr_action |= CX_WRITE;
1256 swi_sched (cx_fast_ih, 0);
1263 ++d->pp.pp_if.if_opackets;
1264 d->pp.pp_if.if_flags &= ~IFF_OACTIVE;
1265 d->pp.pp_if.if_timer = 0;
1271 * Process the received packet.
1273 static void cx_receive (cx_chan_t *c, char *data, int len)
1285 if (c->mode == M_ASYNC && d->tty) {
1286 if (d->tty->t_state & TS_ISOPEN) {
1287 async_q *q = &d->aqueue;
1288 int size = BF_SZ - 1 - AQ_GSZ (q);
1290 if (len <= 0 && !size)
1295 cx_error (c, CX_OVERRUN);
1300 AQ_PUSH (q, *(unsigned char *)cc);
1304 d->intr_action |= CX_READ;
1306 swi_sched (cx_fast_ih, 0);
1313 m = makembuf (data, len);
1315 CX_DEBUG (d, ("no memory for packet\n"));
1317 ++d->pp.pp_if.if_iqdrops;
1324 m->m_pkthdr.rcvif = 0;
1325 NG_SEND_DATA_ONLY (error, d->hook, m);
1327 ++d->pp.pp_if.if_ipackets;
1328 m->m_pkthdr.rcvif = &d->pp.pp_if;
1329 /* Check if there's a BPF listener on this interface.
1330 * If so, hand off the raw packet to bpf. */
1331 if (d->pp.pp_if.if_bpf)
1332 BPF_TAP (&d->pp.pp_if, data, len);
1333 IF_ENQUEUE (&d->queue, m);
1337 #define CONDITION(t,tp) (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))\
1338 && (!(tp->t_iflag & BRKINT) || (tp->t_iflag & IGNBRK))\
1339 && (!(tp->t_iflag & PARMRK)\
1340 || (tp->t_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))\
1341 && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))\
1342 && linesw[tp->t_line]->l_rint == ttyinput)
1345 * Error callback function.
1347 static void cx_error (cx_chan_t *c, int data)
1359 CX_DEBUG (d, ("frame error\n"));
1360 if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN)
1361 && (AQ_GSZ (q) < BF_SZ - 1)
1362 && (!CONDITION((&d->tty->t_termios), (d->tty))
1363 || !(d->tty->t_iflag & (IGNPAR | PARMRK)))) {
1364 AQ_PUSH (q, TTY_FE);
1365 d->intr_action |= CX_READ;
1367 swi_sched (cx_fast_ih, 0);
1371 ++d->pp.pp_if.if_ierrors;
1375 CX_DEBUG (d, ("crc error\n"));
1376 if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN)
1377 && (AQ_GSZ (q) < BF_SZ - 1)
1378 && (!CONDITION((&d->tty->t_termios), (d->tty))
1379 || !(d->tty->t_iflag & INPCK)
1380 || !(d->tty->t_iflag & (IGNPAR | PARMRK)))) {
1381 AQ_PUSH (q, TTY_PE);
1382 d->intr_action |= CX_READ;
1384 swi_sched (cx_fast_ih, 0);
1388 ++d->pp.pp_if.if_ierrors;
1392 CX_DEBUG (d, ("overrun error\n"));
1394 if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN)
1395 && (AQ_GSZ (q) < BF_SZ - 1)
1396 && (!CONDITION((&d->tty->t_termios), (d->tty)))) {
1397 AQ_PUSH (q, TTY_OE);
1398 d->intr_action |= CX_READ;
1400 swi_sched (cx_fast_ih, 0);
1405 ++d->pp.pp_if.if_collisions;
1406 ++d->pp.pp_if.if_ierrors;
1411 CX_DEBUG (d, ("overflow error\n"));
1413 if (c->mode != M_ASYNC)
1414 ++d->pp.pp_if.if_ierrors;
1418 CX_DEBUG (d, ("underrun error\n"));
1419 if (c->mode != M_ASYNC) {
1423 ++d->pp.pp_if.if_oerrors;
1424 d->pp.pp_if.if_flags &= ~IFF_OACTIVE;
1425 d->pp.pp_if.if_timer = 0;
1431 CX_DEBUG (d, ("break error\n"));
1432 if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN)
1433 && (AQ_GSZ (q) < BF_SZ - 1)
1434 && (!CONDITION((&d->tty->t_termios), (d->tty))
1435 || !(d->tty->t_iflag & (IGNBRK | BRKINT | PARMRK)))) {
1436 AQ_PUSH (q, TTY_BI);
1437 d->intr_action |= CX_READ;
1439 swi_sched (cx_fast_ih, 0);
1443 ++d->pp.pp_if.if_ierrors;
1447 CX_DEBUG (d, ("error #%d\n", data));
1451 static int cx_topen (struct tty *tp, struct cdev *dev)
1457 CX_DEBUG2 (d, ("cx_open (serial)\n"));
1461 if (d->chan->mode != M_ASYNC)
1466 cx_start_chan (d->chan, 0, 0);
1467 cx_set_dtr (d->chan, 1);
1468 cx_set_rts (d->chan, 1);
1469 d->cd = cx_get_cd (d->chan);
1472 CX_DEBUG2 (d, ("cx_open done\n"));
1477 static void cx_tclose (struct tty *tp)
1483 CX_DEBUG2 (d, ("cx_close\n"));
1486 /* Disable receiver.
1487 * Transmitter continues sending the queued data. */
1488 cx_enable_receive (d->chan, 0);
1490 d->open_dev &= ~0x2;
1493 static int cx_tmodem (struct tty *tp, int sigon, int sigoff)
1502 if (!sigon && !sigoff) {
1503 if (cx_get_dsr (d->chan)) sigon |= SER_DSR;
1504 if (cx_get_cd (d->chan)) sigon |= SER_DCD;
1505 if (cx_get_cts (d->chan)) sigon |= SER_CTS;
1506 if (d->chan->dtr) sigon |= SER_DTR;
1507 if (d->chan->rts) sigon |= SER_RTS;
1511 if (sigon & SER_DTR)
1512 cx_set_dtr (d->chan, 1);
1513 if (sigoff & SER_DTR)
1514 cx_set_dtr (d->chan, 0);
1515 if (sigon & SER_RTS)
1516 cx_set_rts (d->chan, 1);
1517 if (sigoff & SER_RTS)
1518 cx_set_rts (d->chan, 0);
1524 static int cx_open (struct cdev *dev, int flag, int mode, struct thread *td)
1530 unit = d->chan->num;
1532 CX_DEBUG2 (d, ("cx_open unit=%d, flag=0x%x, mode=0x%x\n",
1537 CX_DEBUG2 (d, ("cx_open done\n"));
1542 static int cx_close (struct cdev *dev, int flag, int mode, struct thread *td)
1547 CX_DEBUG2 (d, ("cx_close\n"));
1548 d->open_dev &= ~0x1;
1552 static int cx_modem_status (drv_t *d)
1554 bdrv_t *bd = d->board->sys;
1555 int status = 0, s = splhigh ();
1557 /* Already opened by someone or network interface is up? */
1558 if ((d->chan->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN) &&
1559 (d->open_dev|0x2)) || (d->chan->mode != M_ASYNC && d->running))
1560 status = TIOCM_LE; /* always enabled while open */
1562 if (cx_get_dsr (d->chan)) status |= TIOCM_DSR;
1563 if (cx_get_cd (d->chan)) status |= TIOCM_CD;
1564 if (cx_get_cts (d->chan)) status |= TIOCM_CTS;
1565 if (d->chan->dtr) status |= TIOCM_DTR;
1566 if (d->chan->rts) status |= TIOCM_RTS;
1572 static int cx_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
1577 struct serial_statistics *st;
1587 case SERIAL_GETREGISTERED:
1588 CX_DEBUG2 (d, ("ioctl: getregistered\n"));
1589 bzero (mask, sizeof(mask));
1590 for (s=0; s<NCX*NCHAN; ++s)
1592 mask [s/8] |= 1 << (s & 7);
1593 bcopy (mask, data, sizeof (mask));
1596 case SERIAL_GETPORT:
1597 CX_DEBUG2 (d, ("ioctl: getport\n"));
1600 *(int *)data = cx_get_port (c);
1608 case SERIAL_SETPORT:
1609 CX_DEBUG2 (d, ("ioctl: setproto\n"));
1610 /* Only for superuser! */
1617 cx_set_port (c, *(int *)data);
1623 case SERIAL_GETPROTO:
1624 CX_DEBUG2 (d, ("ioctl: getproto\n"));
1627 strcpy ((char*)data, (c->mode == M_ASYNC) ? "async" :
1628 (d->pp.pp_flags & PP_FR) ? "fr" :
1629 (d->pp.pp_if.if_flags & PP_CISCO) ? "cisco" : "ppp");
1634 case SERIAL_SETPROTO:
1635 CX_DEBUG2 (d, ("ioctl: setproto\n"));
1636 /* Only for superuser! */
1640 if (c->mode == M_ASYNC)
1642 if (d->pp.pp_if.if_flags & IFF_RUNNING)
1644 if (! strcmp ("cisco", (char*)data)) {
1645 d->pp.pp_flags &= ~(PP_FR);
1646 d->pp.pp_flags |= PP_KEEPALIVE;
1647 d->pp.pp_if.if_flags |= PP_CISCO;
1648 } else if (! strcmp ("fr", (char*)data)) {
1649 d->pp.pp_if.if_flags &= ~(PP_CISCO);
1650 d->pp.pp_flags |= PP_FR | PP_KEEPALIVE;
1651 } else if (! strcmp ("ppp", (char*)data)) {
1652 d->pp.pp_flags &= ~(PP_FR | PP_KEEPALIVE);
1653 d->pp.pp_if.if_flags &= ~(PP_CISCO);
1658 case SERIAL_GETKEEPALIVE:
1659 CX_DEBUG2 (d, ("ioctl: getkeepalive\n"));
1660 if ((d->pp.pp_flags & PP_FR) ||
1661 (d->pp.pp_if.if_flags & PP_CISCO) ||
1662 (c->mode == M_ASYNC))
1666 *(int*)data = (d->pp.pp_flags & PP_KEEPALIVE) ? 1 : 0;
1671 case SERIAL_SETKEEPALIVE:
1672 CX_DEBUG2 (d, ("ioctl: setkeepalive\n"));
1673 /* Only for superuser! */
1677 if ((d->pp.pp_flags & PP_FR) ||
1678 (d->pp.pp_if.if_flags & PP_CISCO))
1683 d->pp.pp_flags |= PP_KEEPALIVE;
1685 d->pp.pp_flags &= ~PP_KEEPALIVE;
1691 case SERIAL_GETMODE:
1692 CX_DEBUG2 (d, ("ioctl: getmode\n"));
1695 *(int*)data = (c->mode == M_ASYNC) ?
1696 SERIAL_ASYNC : SERIAL_HDLC;
1701 case SERIAL_SETMODE:
1702 CX_DEBUG2 (d, ("ioctl: setmode\n"));
1703 /* Only for superuser! */
1708 /* Somebody is waiting for carrier? */
1711 /* /dev/ttyXX is already opened by someone? */
1712 if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN) &&
1715 /* Network interface is up?
1716 * Cannot change to async mode. */
1717 if (c->mode != M_ASYNC && d->running &&
1718 (*(int*)data == SERIAL_ASYNC))
1723 if (c->mode == M_HDLC && *(int*)data == SERIAL_ASYNC) {
1724 cx_set_mode (c, M_ASYNC);
1725 cx_enable_receive (c, 0);
1726 cx_enable_transmit (c, 0);
1727 } else if (c->mode == M_ASYNC && *(int*)data == SERIAL_HDLC) {
1728 cx_set_mode (c, M_HDLC);
1729 cx_enable_receive (c, 1);
1730 cx_enable_transmit (c, 1);
1736 case SERIAL_GETSTAT:
1737 CX_DEBUG2 (d, ("ioctl: getestat\n"));
1738 st = (struct serial_statistics*) data;
1741 st->rintr = c->rintr;
1742 st->tintr = c->tintr;
1743 st->mintr = c->mintr;
1744 st->ibytes = c->ibytes;
1745 st->ipkts = c->ipkts;
1746 st->ierrs = c->ierrs;
1747 st->obytes = c->obytes;
1748 st->opkts = c->opkts;
1749 st->oerrs = c->oerrs;
1754 case SERIAL_CLRSTAT:
1755 CX_DEBUG2 (d, ("ioctl: clrstat\n"));
1756 /* Only for superuser! */
1775 case SERIAL_GETBAUD:
1776 CX_DEBUG2 (d, ("ioctl: getbaud\n"));
1777 if (c->mode == M_ASYNC)
1781 *(long*)data = cx_get_baud(c);
1786 case SERIAL_SETBAUD:
1787 CX_DEBUG2 (d, ("ioctl: setbaud\n"));
1788 /* Only for superuser! */
1792 if (c->mode == M_ASYNC)
1796 cx_set_baud (c, *(long*)data);
1801 case SERIAL_GETLOOP:
1802 CX_DEBUG2 (d, ("ioctl: getloop\n"));
1803 if (c->mode == M_ASYNC)
1807 *(int*)data = cx_get_loop (c);
1812 case SERIAL_SETLOOP:
1813 CX_DEBUG2 (d, ("ioctl: setloop\n"));
1814 /* Only for superuser! */
1818 if (c->mode == M_ASYNC)
1822 cx_set_loop (c, *(int*)data);
1827 case SERIAL_GETDPLL:
1828 CX_DEBUG2 (d, ("ioctl: getdpll\n"));
1829 if (c->mode == M_ASYNC)
1833 *(int*)data = cx_get_dpll (c);
1838 case SERIAL_SETDPLL:
1839 CX_DEBUG2 (d, ("ioctl: setdpll\n"));
1840 /* Only for superuser! */
1844 if (c->mode == M_ASYNC)
1848 cx_set_dpll (c, *(int*)data);
1853 case SERIAL_GETNRZI:
1854 CX_DEBUG2 (d, ("ioctl: getnrzi\n"));
1855 if (c->mode == M_ASYNC)
1859 *(int*)data = cx_get_nrzi (c);
1864 case SERIAL_SETNRZI:
1865 CX_DEBUG2 (d, ("ioctl: setnrzi\n"));
1866 /* Only for superuser! */
1870 if (c->mode == M_ASYNC)
1874 cx_set_nrzi (c, *(int*)data);
1879 case SERIAL_GETDEBUG:
1880 CX_DEBUG2 (d, ("ioctl: getdebug\n"));
1883 *(int*)data = c->debug;
1888 case SERIAL_SETDEBUG:
1889 CX_DEBUG2 (d, ("ioctl: setdebug\n"));
1890 /* Only for superuser! */
1896 c->debug = *(int*)data;
1901 d->pp.pp_if.if_flags |= IFF_DEBUG;
1903 d->pp.pp_if.if_flags &= (~IFF_DEBUG);
1909 case TIOCSDTR: /* Set DTR */
1910 CX_DEBUG2 (d, ("ioctl: tiocsdtr\n"));
1918 case TIOCCDTR: /* Clear DTR */
1919 CX_DEBUG2 (d, ("ioctl: tioccdtr\n"));
1927 case TIOCMSET: /* Set DTR/RTS */
1928 CX_DEBUG2 (d, ("ioctl: tiocmset\n"));
1931 cx_set_dtr (c, (*(int*)data & TIOCM_DTR) ? 1 : 0);
1932 cx_set_rts (c, (*(int*)data & TIOCM_RTS) ? 1 : 0);
1937 case TIOCMBIS: /* Add DTR/RTS */
1938 CX_DEBUG2 (d, ("ioctl: tiocmbis\n"));
1941 if (*(int*)data & TIOCM_DTR) cx_set_dtr (c, 1);
1942 if (*(int*)data & TIOCM_RTS) cx_set_rts (c, 1);
1947 case TIOCMBIC: /* Clear DTR/RTS */
1948 CX_DEBUG2 (d, ("ioctl: tiocmbic\n"));
1951 if (*(int*)data & TIOCM_DTR) cx_set_dtr (c, 0);
1952 if (*(int*)data & TIOCM_RTS) cx_set_rts (c, 0);
1957 case TIOCMGET: /* Get modem status */
1958 CX_DEBUG2 (d, ("ioctl: tiocmget\n"));
1959 *(int*)data = cx_modem_status (d);
1964 CX_DEBUG2 (d, ("ioctl: 0x%lx\n", cmd));
1968 void cx_softintr (void *unused)
1974 while (MY_SOFT_INTR) {
1976 for (i=0; i<NCX*NCHAN; ++i) {
1978 if (!d || !d->chan || d->chan->type == T_NONE
1979 || d->chan->mode != M_ASYNC || !d->tty
1985 if (d->intr_action & CX_READ) {
1987 if (d->tty->t_state & TS_CAN_BYPASS_L_RINT) {
1989 if (d->tty->t_rawq.c_cc + k >
1991 && (d->tty->t_cflag & CRTS_IFLOW
1992 || d->tty->t_iflag & IXOFF)
1993 && !(d->tty->t_state & TS_TBLOCK))
1995 d->tty->t_rawcc += k;
2001 putc (ic, &d->tty->t_rawq);
2006 if (d->tty->t_state & TS_TTSTOP
2007 && (d->tty->t_iflag & IXANY
2008 || d->tty->t_cc[VSTART] ==
2009 d->tty->t_cc[VSTOP])) {
2010 d->tty->t_state &= ~TS_TTSTOP;
2011 d->tty->t_lflag &= ~FLUSHO;
2012 d->intr_action |= CX_WRITE;
2015 while (q->end != q->beg) {
2019 ttyld_rint (d->tty, ic);
2024 d->intr_action &= ~CX_READ;
2031 if (d->intr_action & CX_WRITE) {
2033 ttyld_start (d->tty);
2036 d->intr_action &= ~CX_WRITE;
2046 * Fill transmitter buffer with data.
2048 static void cx_oproc (struct tty *tp)
2053 static u_char buf[DMABUFSZ];
2055 u_short len = 0, sublen = 0;
2060 CX_DEBUG2 (d, ("cx_oproc\n"));
2065 if (tp->t_cflag & CRTSCTS && (tp->t_state & TS_TBLOCK) && d->chan->rts)
2066 cx_set_rts (d->chan, 0);
2067 else if (tp->t_cflag & CRTSCTS && ! (tp->t_state & TS_TBLOCK) && ! d->chan->rts)
2068 cx_set_rts (d->chan, 1);
2070 if (! (tp->t_state & (TS_TIMEOUT | TS_TTSTOP))) {
2071 /* Start transmitter. */
2072 cx_enable_transmit (d->chan, 1);
2075 if (! cx_buf_free (d->chan)) {
2076 tp->t_state |= TS_BUSY;
2081 if (tp->t_iflag & IXOFF) {
2082 p = (buf + (DMABUFSZ/2));
2083 sublen = q_to_b (&tp->t_outq, p, (DMABUFSZ/2));
2086 /* Send XON/XOFF out of band. */
2087 if (*p == tp->t_cc[VSTOP]) {
2088 cx_xflow_ctl (d->chan, 0);
2092 if (*p == tp->t_cc[VSTART]) {
2093 cx_xflow_ctl (d->chan, 1);
2103 len = q_to_b (&tp->t_outq, p, (DMABUFSZ/2));
2106 cx_send_packet (d->chan, buf, len, 0);
2107 tp->t_state |= TS_BUSY;
2109 CX_DEBUG2 (d, ("out %d bytes\n", len));
2117 static int cx_param (struct tty *tp, struct termios *t)
2121 int s, bits, parity;
2128 if (t->c_ospeed == 0) {
2129 /* Clear DTR and RTS. */
2130 cx_set_dtr (d->chan, 0);
2133 CX_DEBUG2 (d, ("cx_param (hangup)\n"));
2136 CX_DEBUG2 (d, ("cx_param\n"));
2138 /* Check requested parameters. */
2139 if (t->c_ospeed < 300 || t->c_ospeed > 256*1024) {
2144 if (t->c_ispeed && (t->c_ispeed < 300 || t->c_ispeed > 256*1024)) {
2150 /* And copy them to tty and channel structures. */
2151 tp->t_ispeed = t->c_ispeed = tp->t_ospeed = t->c_ospeed;
2152 tp->t_cflag = t->c_cflag;
2154 /* Set character length and parity mode. */
2155 switch (t->c_cflag & CSIZE) {
2157 case CS8: bits = 8; break;
2158 case CS7: bits = 7; break;
2159 case CS6: bits = 6; break;
2160 case CS5: bits = 5; break;
2163 parity = ((t->c_cflag & PARENB) ? 1 : 0) *
2164 (1 + ((t->c_cflag & PARODD) ? 0 : 1));
2166 /* Set current channel number. */
2168 cx_set_dtr (d->chan, 1);
2171 cx_set_async_param (d->chan, t->c_ospeed, bits, parity, (t->c_cflag & CSTOPB),
2172 !(t->c_cflag & PARENB), (t->c_cflag & CRTSCTS),
2173 (t->c_iflag & IXON), (t->c_iflag & IXANY),
2174 t->c_cc[VSTART], t->c_cc[VSTOP]);
2181 * Stop output on a line
2183 static void cx_stop (struct tty *tp, int flag)
2194 if (tp->t_state & TS_BUSY) {
2195 /* Stop transmitter */
2196 CX_DEBUG2 (d, ("cx_stop\n"));
2197 cx_transmitter_ctl (d->chan, 0);
2204 * Process the (delayed) carrier signal setup.
2206 static void cx_carrier (void *arg)
2209 bdrv_t *bd = d->board->sys;
2210 cx_chan_t *c = d->chan;
2218 CX_DEBUG (d, ("carrier on\n"));
2223 ttyld_modem(d->tty, 1);
2225 CX_DEBUG (d, ("carrier loss\n"));
2230 ttyld_modem(d->tty, 0);
2236 * Modem signal callback function.
2238 static void cx_modem (cx_chan_t *c)
2242 if (!d || c->mode != M_ASYNC)
2244 /* Handle carrier detect/loss. */
2245 /* Carrier changed - delay processing DCD for a while
2246 * to give both sides some time to initialize. */
2247 callout_reset (&d->dcd_timeout_handle, hz/2, cx_carrier, d);
2250 static struct cdevsw cx_cdevsw = {
2251 .d_version = D_VERSION,
2253 .d_close = cx_close,
2254 .d_ioctl = cx_ioctl,
2256 .d_flags = D_TTY | D_NEEDGIANT,
2260 static int ng_cx_constructor (node_p node)
2262 drv_t *d = NG_NODE_PRIVATE (node);
2263 CX_DEBUG (d, ("Constructor\n"));
2267 static int ng_cx_newhook (node_p node, hook_p hook, const char *name)
2270 drv_t *d = NG_NODE_PRIVATE (node);
2271 bdrv_t *bd = d->board->sys;
2273 if (d->chan->mode == M_ASYNC)
2276 /* Attach debug hook */
2277 if (strcmp (name, NG_CX_HOOK_DEBUG) == 0) {
2278 NG_HOOK_SET_PRIVATE (hook, NULL);
2279 d->debug_hook = hook;
2283 /* Check for raw hook */
2284 if (strcmp (name, NG_CX_HOOK_RAW) != 0)
2287 NG_HOOK_SET_PRIVATE (hook, d);
2297 static int print_modems (char *s, cx_chan_t *c, int need_header)
2299 int status = cx_modem_status (c->sys);
2303 length += sprintf (s + length, " LE DTR DSR RTS CTS CD\n");
2304 length += sprintf (s + length, "%4s %4s %4s %4s %4s %4s\n",
2305 status & TIOCM_LE ? "On" : "-",
2306 status & TIOCM_DTR ? "On" : "-",
2307 status & TIOCM_DSR ? "On" : "-",
2308 status & TIOCM_RTS ? "On" : "-",
2309 status & TIOCM_CTS ? "On" : "-",
2310 status & TIOCM_CD ? "On" : "-");
2314 static int print_stats (char *s, cx_chan_t *c, int need_header)
2319 length += sprintf (s + length, " Rintr Tintr Mintr Ibytes Ipkts Ierrs Obytes Opkts Oerrs\n");
2320 length += sprintf (s + length, "%7ld %7ld %7ld %8ld %7ld %7ld %8ld %7ld %7ld\n",
2321 c->rintr, c->tintr, c->mintr, c->ibytes, c->ipkts,
2322 c->ierrs, c->obytes, c->opkts, c->oerrs);
2326 static int print_chan (char *s, cx_chan_t *c)
2331 length += sprintf (s + length, "cx%d", c->board->num * NCHAN + c->num);
2333 length += sprintf (s + length, " debug=%d", d->chan->debug);
2335 if (cx_get_baud (c))
2336 length += sprintf (s + length, " %ld", cx_get_baud (c));
2338 length += sprintf (s + length, " extclock");
2340 if (c->mode == M_HDLC) {
2341 length += sprintf (s + length, " dpll=%s", cx_get_dpll (c) ? "on" : "off");
2342 length += sprintf (s + length, " nrzi=%s", cx_get_nrzi (c) ? "on" : "off");
2345 length += sprintf (s + length, " loop=%s", cx_get_loop (c) ? "on\n" : "off\n");
2349 static int ng_cx_rcvmsg (node_p node, item_p item, hook_p lasthook)
2351 drv_t *d = NG_NODE_PRIVATE (node);
2352 struct ng_mesg *msg;
2353 struct ng_mesg *resp = NULL;
2359 CX_DEBUG (d, ("Rcvmsg\n"));
2360 NGI_GET_MSG (item, msg);
2361 switch (msg->header.typecookie) {
2367 printf ("Don't forget to implement\n");
2371 case NGM_GENERIC_COOKIE:
2372 switch (msg->header.cmd) {
2377 case NGM_TEXT_STATUS: {
2380 int dl = sizeof (struct ng_mesg) + 730;
2382 NG_MKRESPONSE (resp, msg, dl, M_NOWAIT);
2389 l += print_chan (s + l, d->chan);
2390 l += print_stats (s + l, d->chan, 1);
2391 l += print_modems (s + l, d->chan, 1);
2392 strncpy ((resp)->header.cmdstr, "status", NG_CMDSTRLEN);
2398 NG_RESPOND_MSG (error, node, item, resp);
2403 static int ng_cx_rcvdata (hook_p hook, item_p item)
2405 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE(hook));
2407 struct ng_tag_prio *ptag;
2412 NGI_GET_M (item, m);
2413 NG_FREE_ITEM (item);
2414 if (! NG_HOOK_PRIVATE (hook) || ! d) {
2420 /* Check for high priority data */
2421 if ((ptag = (struct ng_tag_prio *)m_tag_locate(m, NGM_GENERIC_COOKIE,
2422 NG_TAG_PRIO, NULL)) != NULL && (ptag->priority > NG_PRIO_CUTOFF) )
2430 if (_IF_QFULL (q)) {
2446 static int ng_cx_rmnode (node_p node)
2448 drv_t *d = NG_NODE_PRIVATE (node);
2451 CX_DEBUG (d, ("Rmnode\n"));
2452 if (d && d->running) {
2461 if (node->nd_flags & NGF_REALLY_DIE) {
2462 NG_NODE_SET_PRIVATE (node, NULL);
2463 NG_NODE_UNREF (node);
2465 NG_NODE_REVIVE(node); /* Persistant node */
2470 static void ng_cx_watchdog (void *arg)
2474 if (d->timeout == 1)
2478 callout_reset (&d->timeout_handle, hz, ng_cx_watchdog, d);
2481 static int ng_cx_connect (hook_p hook)
2483 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2485 callout_reset (&d->timeout_handle, hz, ng_cx_watchdog, d);
2489 static int ng_cx_disconnect (hook_p hook)
2491 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2492 bdrv_t *bd = d->board->sys;
2497 if (NG_HOOK_PRIVATE (hook))
2501 /* If we were wait it than it reasserted now, just stop it. */
2502 if (!callout_drain (&d->timeout_handle))
2503 callout_stop (&d->timeout_handle);
2508 static int cx_modevent (module_t mod, int type, void *unused)
2510 static int load_count = 0;
2512 if (!debug_mpsafenet && cx_mpsafenet) {
2513 printf ("WORNING! Network stack is not MPSAFE. "
2514 "Turning off debug.cx.mpsafenet.\n");
2518 cx_cdevsw.d_flags &= ~D_NEEDGIANT;
2523 if (ng_newtype (&typestruct))
2524 printf ("Failed to register ng_cx\n");
2528 callout_init (&timeout_handle, cx_mpsafenet?CALLOUT_MPSAFE:0);
2529 callout_reset (&timeout_handle, hz*5, cx_timeout, 0);
2530 /* Software interrupt. */
2531 swi_add(&tty_ithd, "cx", cx_softintr, NULL, SWI_TTY,
2532 (cx_mpsafenet?INTR_MPSAFE:0), &cx_fast_ih);
2535 if (load_count == 1) {
2536 printf ("Removing device entry for Sigma\n");
2538 ng_rmtype (&typestruct);
2541 /* If we were wait it than it reasserted now, just stop it. */
2542 if (!callout_drain (&timeout_handle))
2543 callout_stop (&timeout_handle);
2544 ithread_remove_handler (cx_fast_ih);
2554 static struct ng_type typestruct = {
2555 .version = NG_ABI_VERSION,
2556 .name = NG_CX_NODE_TYPE,
2557 .constructor = ng_cx_constructor,
2558 .rcvmsg = ng_cx_rcvmsg,
2559 .shutdown = ng_cx_rmnode,
2560 .newhook = ng_cx_newhook,
2561 .connect = ng_cx_connect,
2562 .rcvdata = ng_cx_rcvdata,
2563 .disconnect = ng_cx_disconnect,
2568 MODULE_DEPEND (ng_cx, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION);
2570 MODULE_DEPEND (isa_cx, sppp, 1, 1, 1);
2572 DRIVER_MODULE (cx, isa, cx_isa_driver, cx_devclass, cx_modevent, NULL);
2573 MODULE_VERSION (cx, 1);