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>
36 #include <sys/sockio.h>
37 #include <sys/malloc.h>
38 #include <sys/socket.h>
39 #include <sys/sysctl.h>
41 #include <sys/errno.h>
42 #include <sys/serial.h>
45 #include <machine/bus.h>
47 #include <isa/isavar.h>
48 #include <sys/fcntl.h>
49 #include <sys/interrupt.h>
53 #include <machine/cpufunc.h>
54 #include <machine/cserial.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;
233 static int cx_open (struct cdev *dev, int flag, int mode, struct thread *td);
234 static int cx_close (struct cdev *dev, int flag, int mode, struct thread *td);
235 static int cx_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td);
236 static struct cdevsw cx_cdevsw = {
237 .d_version = D_VERSION,
242 .d_flags = D_TTY | D_NEEDGIANT,
245 static int MY_SOFT_INTR;
248 * Print the mbuf chain, for debug purposes only.
250 static void printmbuf (struct mbuf *m)
253 for (; m; m=m->m_next) {
254 if (m->m_flags & M_PKTHDR)
255 printf (" HDR %d:", m->m_pkthdr.len);
256 if (m->m_flags & M_EXT)
258 printf (" %d", m->m_len);
264 * Make an mbuf from data.
266 static struct mbuf *makembuf (void *buf, u_int len)
268 struct mbuf *m, *o, *p;
270 MGETHDR (m, M_DONTWAIT, MT_DATA);
275 if (len >= MINCLSIZE)
276 MCLGET (m, M_DONTWAIT);
278 m->m_pkthdr.len = len;
283 u_int n = M_TRAILINGSPACE (p);
287 /* Allocate new mbuf. */
289 MGET (p, M_DONTWAIT, MT_DATA);
294 if (len >= MINCLSIZE)
295 MCLGET (p, M_DONTWAIT);
299 n = M_TRAILINGSPACE (p);
303 bcopy (buf, mtod (p, caddr_t) + p->m_len, n);
306 buf = n + (char*) buf;
313 * Recover after lost transmit interrupts.
315 static void cx_timeout (void *arg)
320 for (i = 0; i < NCX; i++) {
321 if (adapter[i] == NULL)
323 for (k = 0; k < NCHAN; ++k) {
324 d = channel[i * NCHAN + k];
328 CX_LOCK ((bdrv_t *)d->board->sys);
329 if (d->atimeout == 1 && d->tty && d->tty->t_state & TS_BUSY) {
330 d->tty->t_state &= ~TS_BUSY;
332 d->intr_action |= CX_WRITE;
334 swi_sched (cx_fast_ih, 0);
336 CX_DEBUG (d, ("cx_timeout\n"));
340 CX_UNLOCK ((bdrv_t *)d->board->sys);
344 callout_reset (&timeout_handle, hz*5, cx_timeout, 0);
347 static void cx_led_off (void *arg)
361 * Activate interrupt handler from DDK.
363 static void cx_intr (void *arg)
366 cx_board_t *b = bd->board;
378 /* Turn LED off 50 msec later. */
379 callout_reset (&led_timo[b->num], hz/20, cx_led_off, b);
384 /* Pass packets in a lock-free state */
385 for (i = 0; i < NCHAN && b->chan[i].type; i++) {
386 drv_t *d = b->chan[i].sys;
388 if (!d || !d->running)
390 while (_IF_QLEN(&d->queue)) {
391 IF_DEQUEUE (&d->queue,m);
394 sppp_input (d->ifp, m);
400 static int probe_irq (cx_board_t *b, int irq)
404 /* Clear pending irq, if any. */
405 cx_probe_irq (b, -irq);
407 for (cnt=0; cnt<5; ++cnt) {
408 /* Get the mask of pending irqs, assuming they are busy.
409 * Activate the adapter on given irq. */
410 busy = cx_probe_irq (b, irq);
413 /* Get the mask of active irqs.
414 * Deactivate our irq. */
415 mask = cx_probe_irq (b, -irq);
417 if ((mask & ~busy) == 1 << irq) {
419 /* printf ("cx%d: irq %d ok, mask=0x%04x, busy=0x%04x\n",
420 b->num, irq, mask, busy); */
424 /* printf ("cx%d: irq %d not functional, mask=0x%04x, busy=0x%04x\n",
425 b->num, irq, mask, busy); */
430 static short porttab [] = {
431 0x200, 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0,
432 0x300, 0x320, 0x340, 0x360, 0x380, 0x3a0, 0x3c0, 0x3e0, 0
434 static char dmatab [] = { 7, 6, 5, 0 };
435 static char irqtab [] = { 5, 10, 11, 7, 3, 15, 12, 0 };
437 static int cx_is_free_res (device_t dev, int rid, int type, u_long start,
438 u_long end, u_long count)
440 struct resource *res;
442 if (!(res = bus_alloc_resource (dev, type, &rid, start, end, count,
446 bus_release_resource (dev, type, rid, res);
451 static void cx_identify (driver_t *driver, device_t dev)
453 u_long iobase, rescount;
457 devclass_t my_devclass;
460 if ((my_devclass = devclass_find ("cx")) == NULL)
463 devclass_get_devices (my_devclass, &devices, &devcount);
466 /* We should find all devices by our self. We could alter other
467 * devices, but we don't have a choise
469 for (i = 0; (iobase = porttab [i]) != 0; i++) {
470 if (!cx_is_free_res (dev, 0, SYS_RES_IOPORT,
471 iobase, iobase + NPORT, NPORT))
473 if (cx_probe_board (iobase, -1, -1) == 0)
478 child = BUS_ADD_CHILD (dev, ISA_ORDER_SPECULATIVE, "cx",
484 device_set_desc_copy (child, "Cronyx Sigma");
485 device_set_driver (child, driver);
486 bus_set_resource (child, SYS_RES_IOPORT, 0,
493 static short porttab [] = {
494 0x200, 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0,
495 0x300, 0x320, 0x340, 0x360, 0x380, 0x3a0, 0x3c0, 0x3e0, 0
497 /* Lets check user choise.
499 for (k = 0; k < devcount; k++) {
500 if (bus_get_resource (devices[k], SYS_RES_IOPORT, 0,
501 &iobase, &rescount) != 0)
504 for (i = 0; porttab [i] != 0; i++) {
505 if (porttab [i] != iobase)
507 if (!cx_is_free_res (devices[k], 0, SYS_RES_IOPORT,
508 iobase, iobase + NPORT, NPORT))
510 if (cx_probe_board (iobase, -1, -1) == 0)
513 device_set_desc_copy (devices[k], "Cronyx Sigma");
517 if (porttab [i] == 0) {
518 device_delete_child (
519 device_get_parent (devices[k]),
525 for (k = 0; k < devcount; k++) {
528 if (bus_get_resource (devices[k], SYS_RES_IOPORT, 0,
529 &iobase, &rescount) == 0)
531 for (i = 0; (iobase = porttab [i]) != 0; i++) {
532 if (porttab [i] == -1) {
535 if (!cx_is_free_res (devices[k], 0, SYS_RES_IOPORT,
536 iobase, iobase + NPORT, NPORT))
538 if (cx_probe_board (iobase, -1, -1) == 0)
541 bus_set_resource (devices[k], SYS_RES_IOPORT, 0,
544 device_set_desc_copy (devices[k], "Cronyx Sigma");
547 if (porttab [i] == 0) {
548 device_delete_child (
549 device_get_parent (devices[k]),
553 free (devices, M_TEMP);
559 static int cx_probe (device_t dev)
561 int unit = device_get_unit (dev);
563 u_long iobase, rescount;
565 if (!device_get_desc (dev) ||
566 strcmp (device_get_desc (dev), "Cronyx Sigma"))
569 if (bus_get_resource (dev, SYS_RES_IOPORT, 0, &iobase, &rescount) != 0) {
570 printf ("cx%d: Couldn't get IOPORT\n", unit);
574 if (!cx_is_free_res (dev, 0, SYS_RES_IOPORT,
575 iobase, iobase + NPORT, NPORT)) {
576 printf ("cx%d: Resource IOPORT isn't free %lx\n", unit, iobase);
580 for (i = 0; porttab [i] != 0; i++) {
581 if (porttab [i] == iobase) {
587 if (porttab [i] == 0) {
591 if (!cx_probe_board (iobase, -1, -1)) {
592 printf ("cx%d: probing for Sigma at %lx faild\n", unit, iobase);
600 cx_bus_dmamap_addr (void *arg, bus_dma_segment_t *segs, int nseg, int error)
607 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
609 *addr = segs->ds_addr;
613 cx_bus_dma_mem_alloc (int bnum, int cnum, cx_dma_mem_t *dmem)
617 error = bus_dma_tag_create (NULL, 16, 0, BUS_SPACE_MAXADDR_24BIT,
618 BUS_SPACE_MAXADDR, NULL, NULL, dmem->size, 1,
619 dmem->size, 0, NULL, NULL, &dmem->dmat);
621 if (cnum >= 0) printf ("cx%d-%d: ", bnum, cnum);
622 else printf ("cx%d: ", bnum);
623 printf ("couldn't allocate tag for dma memory\n");
626 error = bus_dmamem_alloc (dmem->dmat, (void **)&dmem->virt,
627 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &dmem->mapp);
629 if (cnum >= 0) printf ("cx%d-%d: ", bnum, cnum);
630 else printf ("cx%d: ", bnum);
631 printf ("couldn't allocate mem for dma memory\n");
632 bus_dma_tag_destroy (dmem->dmat);
635 error = bus_dmamap_load (dmem->dmat, dmem->mapp, dmem->virt,
636 dmem->size, cx_bus_dmamap_addr, &dmem->phys, 0);
638 if (cnum >= 0) printf ("cx%d-%d: ", bnum, cnum);
639 else printf ("cx%d: ", bnum);
640 printf ("couldn't load mem map for dma memory\n");
641 bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
642 bus_dma_tag_destroy (dmem->dmat);
649 cx_bus_dma_mem_free (cx_dma_mem_t *dmem)
651 bus_dmamap_unload (dmem->dmat, dmem->mapp);
652 bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
653 bus_dma_tag_destroy (dmem->dmat);
657 * The adapter is present, initialize the driver structures.
659 static int cx_attach (device_t dev)
661 bdrv_t *bd = device_get_softc (dev);
662 u_long iobase, drq, irq, rescount;
663 int unit = device_get_unit (dev);
664 char *cx_ln = CX_LOCK_NAME;
671 KASSERT ((bd != NULL), ("cx%d: NULL device softc\n", unit));
673 bus_get_resource (dev, SYS_RES_IOPORT, 0, &iobase, &rescount);
675 bd->base_res = bus_alloc_resource (dev, SYS_RES_IOPORT, &bd->base_rid,
676 iobase, iobase + NPORT, NPORT, RF_ACTIVE);
677 if (! bd->base_res) {
678 printf ("cx%d: cannot allocate base address\n", unit);
682 if (bus_get_resource (dev, SYS_RES_DRQ, 0, &drq, &rescount) != 0) {
683 for (i = 0; (drq = dmatab [i]) != 0; i++) {
684 if (!cx_is_free_res (dev, 0, SYS_RES_DRQ,
687 bus_set_resource (dev, SYS_RES_DRQ, 0, drq, 1);
691 if (dmatab[i] == 0) {
692 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
694 printf ("cx%d: Couldn't get DRQ\n", unit);
700 bd->drq_res = bus_alloc_resource (dev, SYS_RES_DRQ, &bd->drq_rid,
701 drq, drq + 1, 1, RF_ACTIVE);
703 printf ("cx%d: cannot allocate drq\n", unit);
704 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
709 if (bus_get_resource (dev, SYS_RES_IRQ, 0, &irq, &rescount) != 0) {
710 for (i = 0; (irq = irqtab [i]) != 0; i++) {
711 if (!cx_is_free_res (dev, 0, SYS_RES_IRQ,
714 bus_set_resource (dev, SYS_RES_IRQ, 0, irq, 1);
718 if (irqtab[i] == 0) {
719 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
721 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
723 printf ("cx%d: Couldn't get IRQ\n", unit);
729 bd->irq_res = bus_alloc_resource (dev, SYS_RES_IRQ, &bd->irq_rid,
730 irq, irq + 1, 1, RF_ACTIVE);
732 printf ("cx%d: Couldn't allocate irq\n", unit);
733 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
735 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
740 b = malloc (sizeof (cx_board_t), M_DEVBUF, M_WAITOK);
742 printf ("cx:%d: Couldn't allocate memory\n", unit);
746 bzero (b, sizeof(cx_board_t));
748 if (! cx_open_board (b, unit, iobase, irq, drq)) {
749 printf ("cx%d: error loading firmware\n", unit);
751 bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid,
753 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
755 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
762 cx_ln[2] = '0' + unit;
763 mtx_init (&bd->cx_mtx, cx_ln, MTX_NETWORK_LOCK, MTX_DEF|MTX_RECURSE);
764 if (! probe_irq (b, irq)) {
765 printf ("cx%d: irq %ld not functional\n", unit, irq);
768 mtx_destroy (&bd->cx_mtx);
770 bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid,
772 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
774 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
779 callout_init (&led_timo[b->num], cx_mpsafenet ? CALLOUT_MPSAFE : 0);
781 if (bus_setup_intr (dev, bd->irq_res,
782 INTR_TYPE_NET|(cx_mpsafenet?INTR_MPSAFE:0),
783 NULL, cx_intr, bd, &bd->intrhand)) {
784 printf ("cx%d: Can't setup irq %ld\n", unit, irq);
788 mtx_destroy (&bd->cx_mtx);
790 bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid,
792 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
794 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
801 cx_init (b, b->num, b->port, irq, drq);
802 cx_setup_board (b, 0, 0, 0);
805 printf ("cx%d: <Cronyx-Sigma-%s>\n", b->num, b->name);
807 for (c=b->chan; c<b->chan+NCHAN; ++c) {
808 if (c->type == T_NONE)
810 d = &bd->channel[c->num];
811 d->dmamem.size = sizeof(cx_buf_t);
812 if (! cx_bus_dma_mem_alloc (unit, c->num, &d->dmamem))
818 channel [b->num*NCHAN + c->num] = d;
819 sprintf (d->name, "cx%d.%d", b->num, c->num);
830 if (ng_make_node_common (&typestruct, &d->node) != 0) {
831 printf ("%s: cannot make common node\n", d->name);
832 channel [b->num*NCHAN + c->num] = 0;
834 cx_bus_dma_mem_free (&d->dmamem);
837 NG_NODE_SET_PRIVATE (d->node, d);
838 sprintf (d->nodename, "%s%d", NG_CX_NODE_TYPE,
839 c->board->num*NCHAN + c->num);
840 if (ng_name_node (d->node, d->nodename)) {
841 printf ("%s: cannot name node\n", d->nodename);
842 NG_NODE_UNREF (d->node);
843 channel [b->num*NCHAN + c->num] = 0;
845 cx_bus_dma_mem_free (&d->dmamem);
848 d->lo_queue.ifq_maxlen = IFQ_MAXLEN;
849 d->hi_queue.ifq_maxlen = IFQ_MAXLEN;
850 mtx_init (&d->lo_queue.ifq_mtx, "cx_queue_lo", NULL, MTX_DEF);
851 mtx_init (&d->hi_queue.ifq_mtx, "cx_queue_hi", NULL, MTX_DEF);
852 callout_init (&d->timeout_handle,
853 cx_mpsafenet ? CALLOUT_MPSAFE : 0);
855 d->ifp = if_alloc(IFT_PPP);
856 if (d->ifp == NULL) {
857 printf ("%s: cannot if_alloc() common interface\n",
859 channel [b->num*NCHAN + c->num] = 0;
861 cx_bus_dma_mem_free (&d->dmamem);
864 d->ifp->if_softc = d;
865 if_initname (d->ifp, "cx", b->num * NCHAN + c->num);
866 d->ifp->if_mtu = PP_MTU;
867 d->ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
869 d->ifp->if_flags |= IFF_NEEDSGIANT;
870 d->ifp->if_ioctl = cx_sioctl;
871 d->ifp->if_start = cx_ifstart;
872 d->ifp->if_watchdog = cx_ifwatchdog;
873 d->ifp->if_init = cx_initialize;
874 d->queue.ifq_maxlen = 2;
875 mtx_init (&d->queue.ifq_mtx, "cx_queue", NULL, MTX_DEF);
876 sppp_attach (d->ifp);
878 IFP2SP(d->ifp)->pp_tlf = cx_tlf;
879 IFP2SP(d->ifp)->pp_tls = cx_tls;
880 /* If BPF is in the kernel, call the attach for it.
881 * Size of PPP header is 4 bytes. */
882 bpfattach (d->ifp, DLT_PPP, 4);
885 d->tty = ttyalloc ();
886 d->tty->t_open = cx_topen;
887 d->tty->t_close = cx_tclose;
888 d->tty->t_param = cx_param;
889 d->tty->t_stop = cx_stop;
890 d->tty->t_modem = cx_tmodem;
891 d->tty->t_oproc = cx_oproc;
894 cx_start_chan (c, d->dmamem.virt, d->dmamem.phys);
895 cx_register_receive (c, &cx_receive);
896 cx_register_transmit (c, &cx_transmit);
897 cx_register_error (c, &cx_error);
898 cx_register_modem (c, &cx_modem);
901 ttycreate(d->tty, TS_CALLOUT, "x%r%r", b->num, c->num);
902 d->devt = make_dev (&cx_cdevsw, b->num*NCHAN + c->num + 64, UID_ROOT, GID_WHEEL, 0600, "cx%d", b->num*NCHAN + c->num);
903 d->devt->si_drv1 = d;
904 callout_init (&d->dcd_timeout_handle,
905 cx_mpsafenet ? CALLOUT_MPSAFE : 0);
912 static int cx_detach (device_t dev)
914 bdrv_t *bd = device_get_softc (dev);
915 cx_board_t *b = bd->board;
919 KASSERT (mtx_initialized (&bd->cx_mtx), ("cx mutex not initialized"));
923 /* Check if the device is busy (open). */
924 for (c = b->chan; c < b->chan + NCHAN; ++c) {
925 drv_t *d = (drv_t*) c->sys;
927 if (!d || d->chan->type == T_NONE)
934 if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN) &&
947 /* Deactivate the timeout routine. And soft interrupt*/
948 callout_stop (&led_timo[b->num]);
950 for (c = b->chan; c < b->chan + NCHAN; ++c) {
953 if (!d || d->chan->type == T_NONE)
956 callout_stop (&d->dcd_timeout_handle);
959 bus_teardown_intr (dev, bd->irq_res, bd->intrhand);
960 bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid, bd->irq_res);
962 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid, bd->drq_res);
964 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid, bd->base_res);
969 /* Detach the interfaces, free buffer memory. */
970 for (c = b->chan; c < b->chan + NCHAN; ++c) {
971 drv_t *d = (drv_t*) c->sys;
973 if (!d || d->chan->type == T_NONE)
983 ng_rmnode_self (d->node);
984 NG_NODE_UNREF (d->node);
987 mtx_destroy (&d->lo_queue.ifq_mtx);
988 mtx_destroy (&d->hi_queue.ifq_mtx);
990 /* Detach from the packet filter list of interfaces. */
992 /* Detach from the sync PPP list. */
993 sppp_detach (d->ifp);
997 /* XXXRIK: check interconnection with irq handler */
998 IF_DRAIN (&d->queue);
999 mtx_destroy (&d->queue.ifq_mtx);
1001 destroy_dev (d->devt);
1006 callout_drain (&led_timo[b->num]);
1007 for (c = b->chan; c < b->chan + NCHAN; ++c) {
1010 if (!d || d->chan->type == T_NONE)
1013 callout_drain (&d->dcd_timeout_handle);
1018 for (c = b->chan; c < b->chan + NCHAN; ++c) {
1019 drv_t *d = (drv_t*) c->sys;
1021 if (!d || d->chan->type == T_NONE)
1024 /* Deallocate buffers. */
1025 cx_bus_dma_mem_free (&d->dmamem);
1028 adapter [b->num] = 0;
1032 mtx_destroy (&bd->cx_mtx);
1038 static void cx_ifstart (struct ifnet *ifp)
1040 drv_t *d = ifp->if_softc;
1041 bdrv_t *bd = d->board->sys;
1048 static void cx_ifwatchdog (struct ifnet *ifp)
1050 drv_t *d = ifp->if_softc;
1055 static void cx_tlf (struct sppp *sp)
1057 drv_t *d = SP2IFP(sp)->if_softc;
1059 CX_DEBUG (d, ("cx_tlf\n"));
1060 /* cx_set_dtr (d->chan, 0);*/
1061 /* cx_set_rts (d->chan, 0);*/
1062 if (!(IFP2SP(d->ifp)->pp_flags & PP_FR) && !(d->ifp->if_flags & PP_CISCO))
1066 static void cx_tls (struct sppp *sp)
1068 drv_t *d = SP2IFP(sp)->if_softc;
1070 CX_DEBUG (d, ("cx_tls\n"));
1071 if (!(IFP2SP(d->ifp)->pp_flags & PP_FR) && !(d->ifp->if_flags & PP_CISCO))
1076 * Initialization of interface.
1077 * It seems to be never called by upper level.
1079 static void cx_initialize (void *softc)
1083 CX_DEBUG (d, ("cx_initialize\n"));
1087 * Process an ioctl request.
1089 static int cx_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data)
1091 drv_t *d = ifp->if_softc;
1092 bdrv_t *bd = d->board->sys;
1093 int error, s, was_up, should_be_up;
1095 /* No socket ioctls while the channel is in async mode. */
1096 if (d->chan->type == T_NONE || d->chan->mode == M_ASYNC)
1099 /* Socket ioctls on slave subchannels are not allowed. */
1100 was_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
1101 error = sppp_ioctl (ifp, cmd, data);
1107 if (! (ifp->if_flags & IFF_DEBUG))
1110 d->chan->debug = d->chan->debug_shadow;
1115 default: CX_DEBUG2 (d, ("ioctl 0x%lx\n", cmd)); return 0;
1116 case SIOCADDMULTI: CX_DEBUG2 (d, ("SIOCADDMULTI\n")); return 0;
1117 case SIOCDELMULTI: CX_DEBUG2 (d, ("SIOCDELMULTI\n")); return 0;
1118 case SIOCSIFFLAGS: CX_DEBUG2 (d, ("SIOCSIFFLAGS\n")); break;
1119 case SIOCSIFADDR: CX_DEBUG2 (d, ("SIOCSIFADDR\n")); break;
1122 /* We get here only in case of SIFFLAGS or SIFADDR. */
1125 should_be_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
1126 if (!was_up && should_be_up) {
1127 /* Interface goes up -- start it. */
1130 } else if (was_up && !should_be_up) {
1131 /* Interface is going down -- stop it. */
1132 /* if ((IFP2SP(d->ifp)->pp_flags & PP_FR) || (ifp->if_flags & PP_CISCO))*/
1142 * Stop the interface. Called on splimp().
1144 static void cx_down (drv_t *d)
1147 CX_DEBUG (d, ("cx_down\n"));
1148 cx_set_dtr (d->chan, 0);
1149 cx_set_rts (d->chan, 0);
1155 * Start the interface. Called on splimp().
1157 static void cx_up (drv_t *d)
1160 CX_DEBUG (d, ("cx_up\n"));
1161 cx_set_dtr (d->chan, 1);
1162 cx_set_rts (d->chan, 1);
1168 * Start output on the (slave) interface. Get another datagram to send
1169 * off of the interface queue, and copy it to the interface
1170 * before starting the output.
1172 static void cx_send (drv_t *d)
1177 CX_DEBUG2 (d, ("cx_send\n"));
1179 /* No output if the interface is down. */
1183 /* No output if the modem is off. */
1184 if (! cx_get_dsr (d->chan) && ! cx_get_loop(d->chan))
1187 if (cx_buf_free (d->chan)) {
1188 /* Get the packet to send. */
1190 IF_DEQUEUE (&d->hi_queue, m);
1192 IF_DEQUEUE (&d->lo_queue, m);
1194 m = sppp_dequeue (d->ifp);
1199 BPF_MTAP (d->ifp, m);
1201 len = m_length (m, NULL);
1203 cx_send_packet (d->chan, (u_char*)mtod (m, caddr_t),
1206 u_char buf [DMABUFSZ];
1207 m_copydata (m, 0, len, buf);
1208 cx_send_packet (d->chan, buf, len, 0);
1212 /* Set up transmit timeout, 10 seconds. */
1216 d->ifp->if_timer = 10;
1220 d->ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1225 * Start output on the interface.
1226 * Always called on splimp().
1228 static void cx_start (drv_t *d)
1233 cx_set_dtr (d->chan, 1);
1235 cx_set_rts (d->chan, 1);
1242 * Handle transmit timeouts.
1243 * Recover after lost transmit interrupts.
1244 * Always called on splimp().
1246 static void cx_watchdog (drv_t *d)
1248 bdrv_t *bd = d->board->sys;
1252 CX_DEBUG (d, ("device timeout\n"));
1254 cx_setup_chan (d->chan);
1255 cx_start_chan (d->chan, 0, 0);
1256 cx_set_dtr (d->chan, 1);
1257 cx_set_rts (d->chan, 1);
1265 * Transmit callback function.
1267 static void cx_transmit (cx_chan_t *c, void *attachment, int len)
1274 if (c->mode == M_ASYNC && d->tty) {
1275 d->tty->t_state &= ~(TS_BUSY | TS_FLUSH);
1277 if (d->tty->t_dev) {
1278 d->intr_action |= CX_WRITE;
1280 swi_sched (cx_fast_ih, 0);
1287 ++d->ifp->if_opackets;
1288 d->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1289 d->ifp->if_timer = 0;
1295 * Process the received packet.
1297 static void cx_receive (cx_chan_t *c, char *data, int len)
1309 if (c->mode == M_ASYNC && d->tty) {
1310 if (d->tty->t_state & TS_ISOPEN) {
1311 async_q *q = &d->aqueue;
1312 int size = BF_SZ - 1 - AQ_GSZ (q);
1314 if (len <= 0 && !size)
1319 cx_error (c, CX_OVERRUN);
1324 AQ_PUSH (q, *(unsigned char *)cc);
1328 d->intr_action |= CX_READ;
1330 swi_sched (cx_fast_ih, 0);
1337 m = makembuf (data, len);
1339 CX_DEBUG (d, ("no memory for packet\n"));
1341 ++d->ifp->if_iqdrops;
1348 m->m_pkthdr.rcvif = 0;
1349 NG_SEND_DATA_ONLY (error, d->hook, m);
1351 ++d->ifp->if_ipackets;
1352 m->m_pkthdr.rcvif = d->ifp;
1353 /* Check if there's a BPF listener on this interface.
1354 * If so, hand off the raw packet to bpf. */
1355 BPF_TAP (d->ifp, data, len);
1356 IF_ENQUEUE (&d->queue, m);
1360 #define CONDITION(t,tp) (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))\
1361 && (!(tp->t_iflag & BRKINT) || (tp->t_iflag & IGNBRK))\
1362 && (!(tp->t_iflag & PARMRK)\
1363 || (tp->t_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))\
1364 && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))\
1365 && linesw[tp->t_line]->l_rint == ttyinput)
1368 * Error callback function.
1370 static void cx_error (cx_chan_t *c, int data)
1382 CX_DEBUG (d, ("frame error\n"));
1383 if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN)
1384 && (AQ_GSZ (q) < BF_SZ - 1)
1385 && (!CONDITION((&d->tty->t_termios), (d->tty))
1386 || !(d->tty->t_iflag & (IGNPAR | PARMRK)))) {
1387 AQ_PUSH (q, TTY_FE);
1388 d->intr_action |= CX_READ;
1390 swi_sched (cx_fast_ih, 0);
1394 ++d->ifp->if_ierrors;
1398 CX_DEBUG (d, ("crc error\n"));
1399 if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN)
1400 && (AQ_GSZ (q) < BF_SZ - 1)
1401 && (!CONDITION((&d->tty->t_termios), (d->tty))
1402 || !(d->tty->t_iflag & INPCK)
1403 || !(d->tty->t_iflag & (IGNPAR | PARMRK)))) {
1404 AQ_PUSH (q, TTY_PE);
1405 d->intr_action |= CX_READ;
1407 swi_sched (cx_fast_ih, 0);
1411 ++d->ifp->if_ierrors;
1415 CX_DEBUG (d, ("overrun error\n"));
1417 if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN)
1418 && (AQ_GSZ (q) < BF_SZ - 1)
1419 && (!CONDITION((&d->tty->t_termios), (d->tty)))) {
1420 AQ_PUSH (q, TTY_OE);
1421 d->intr_action |= CX_READ;
1423 swi_sched (cx_fast_ih, 0);
1428 ++d->ifp->if_collisions;
1429 ++d->ifp->if_ierrors;
1434 CX_DEBUG (d, ("overflow error\n"));
1436 if (c->mode != M_ASYNC)
1437 ++d->ifp->if_ierrors;
1441 CX_DEBUG (d, ("underrun error\n"));
1442 if (c->mode != M_ASYNC) {
1446 ++d->ifp->if_oerrors;
1447 d->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1448 d->ifp->if_timer = 0;
1454 CX_DEBUG (d, ("break error\n"));
1455 if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN)
1456 && (AQ_GSZ (q) < BF_SZ - 1)
1457 && (!CONDITION((&d->tty->t_termios), (d->tty))
1458 || !(d->tty->t_iflag & (IGNBRK | BRKINT | PARMRK)))) {
1459 AQ_PUSH (q, TTY_BI);
1460 d->intr_action |= CX_READ;
1462 swi_sched (cx_fast_ih, 0);
1466 ++d->ifp->if_ierrors;
1470 CX_DEBUG (d, ("error #%d\n", data));
1474 static int cx_topen (struct tty *tp, struct cdev *dev)
1480 CX_DEBUG2 (d, ("cx_open (serial)\n"));
1484 if (d->chan->mode != M_ASYNC)
1489 cx_start_chan (d->chan, 0, 0);
1490 cx_set_dtr (d->chan, 1);
1491 cx_set_rts (d->chan, 1);
1492 d->cd = cx_get_cd (d->chan);
1495 CX_DEBUG2 (d, ("cx_open done\n"));
1500 static void cx_tclose (struct tty *tp)
1506 CX_DEBUG2 (d, ("cx_close\n"));
1509 /* Disable receiver.
1510 * Transmitter continues sending the queued data. */
1511 cx_enable_receive (d->chan, 0);
1513 d->open_dev &= ~0x2;
1516 static int cx_tmodem (struct tty *tp, int sigon, int sigoff)
1525 if (!sigon && !sigoff) {
1526 if (cx_get_dsr (d->chan)) sigon |= SER_DSR;
1527 if (cx_get_cd (d->chan)) sigon |= SER_DCD;
1528 if (cx_get_cts (d->chan)) sigon |= SER_CTS;
1529 if (d->chan->dtr) sigon |= SER_DTR;
1530 if (d->chan->rts) sigon |= SER_RTS;
1535 if (sigon & SER_DTR)
1536 cx_set_dtr (d->chan, 1);
1537 if (sigoff & SER_DTR)
1538 cx_set_dtr (d->chan, 0);
1539 if (sigon & SER_RTS)
1540 cx_set_rts (d->chan, 1);
1541 if (sigoff & SER_RTS)
1542 cx_set_rts (d->chan, 0);
1548 static int cx_open (struct cdev *dev, int flag, int mode, struct thread *td)
1554 unit = d->chan->num;
1556 CX_DEBUG2 (d, ("cx_open unit=%d, flag=0x%x, mode=0x%x\n",
1561 CX_DEBUG2 (d, ("cx_open done\n"));
1566 static int cx_close (struct cdev *dev, int flag, int mode, struct thread *td)
1571 CX_DEBUG2 (d, ("cx_close\n"));
1572 d->open_dev &= ~0x1;
1576 static int cx_modem_status (drv_t *d)
1578 bdrv_t *bd = d->board->sys;
1579 int status = 0, s = splhigh ();
1581 /* Already opened by someone or network interface is up? */
1582 if ((d->chan->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN) &&
1583 (d->open_dev|0x2)) || (d->chan->mode != M_ASYNC && d->running))
1584 status = TIOCM_LE; /* always enabled while open */
1586 if (cx_get_dsr (d->chan)) status |= TIOCM_DSR;
1587 if (cx_get_cd (d->chan)) status |= TIOCM_CD;
1588 if (cx_get_cts (d->chan)) status |= TIOCM_CTS;
1589 if (d->chan->dtr) status |= TIOCM_DTR;
1590 if (d->chan->rts) status |= TIOCM_RTS;
1596 static int cx_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
1601 struct serial_statistics *st;
1611 case SERIAL_GETREGISTERED:
1612 CX_DEBUG2 (d, ("ioctl: getregistered\n"));
1613 bzero (mask, sizeof(mask));
1614 for (s=0; s<NCX*NCHAN; ++s)
1616 mask [s/8] |= 1 << (s & 7);
1617 bcopy (mask, data, sizeof (mask));
1620 case SERIAL_GETPORT:
1621 CX_DEBUG2 (d, ("ioctl: getport\n"));
1624 *(int *)data = cx_get_port (c);
1632 case SERIAL_SETPORT:
1633 CX_DEBUG2 (d, ("ioctl: setproto\n"));
1634 /* Only for superuser! */
1635 error = priv_check (td, PRIV_DRIVER);
1641 cx_set_port (c, *(int *)data);
1647 case SERIAL_GETPROTO:
1648 CX_DEBUG2 (d, ("ioctl: getproto\n"));
1651 strcpy ((char*)data, (c->mode == M_ASYNC) ? "async" :
1652 (IFP2SP(d->ifp)->pp_flags & PP_FR) ? "fr" :
1653 (d->ifp->if_flags & PP_CISCO) ? "cisco" : "ppp");
1658 case SERIAL_SETPROTO:
1659 CX_DEBUG2 (d, ("ioctl: setproto\n"));
1660 /* Only for superuser! */
1661 error = priv_check (td, PRIV_DRIVER);
1664 if (c->mode == M_ASYNC)
1666 if (d->ifp->if_drv_flags & IFF_DRV_RUNNING)
1668 if (! strcmp ("cisco", (char*)data)) {
1669 IFP2SP(d->ifp)->pp_flags &= ~(PP_FR);
1670 IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE;
1671 d->ifp->if_flags |= PP_CISCO;
1672 } else if (! strcmp ("fr", (char*)data)) {
1673 d->ifp->if_flags &= ~(PP_CISCO);
1674 IFP2SP(d->ifp)->pp_flags |= PP_FR | PP_KEEPALIVE;
1675 } else if (! strcmp ("ppp", (char*)data)) {
1676 IFP2SP(d->ifp)->pp_flags &= ~(PP_FR | PP_KEEPALIVE);
1677 d->ifp->if_flags &= ~(PP_CISCO);
1682 case SERIAL_GETKEEPALIVE:
1683 CX_DEBUG2 (d, ("ioctl: getkeepalive\n"));
1684 if ((IFP2SP(d->ifp)->pp_flags & PP_FR) ||
1685 (d->ifp->if_flags & PP_CISCO) ||
1686 (c->mode == M_ASYNC))
1690 *(int*)data = (IFP2SP(d->ifp)->pp_flags & PP_KEEPALIVE) ? 1 : 0;
1695 case SERIAL_SETKEEPALIVE:
1696 CX_DEBUG2 (d, ("ioctl: setkeepalive\n"));
1697 /* Only for superuser! */
1698 error = priv_check (td, PRIV_DRIVER);
1701 if ((IFP2SP(d->ifp)->pp_flags & PP_FR) ||
1702 (d->ifp->if_flags & PP_CISCO))
1707 IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE;
1709 IFP2SP(d->ifp)->pp_flags &= ~PP_KEEPALIVE;
1715 case SERIAL_GETMODE:
1716 CX_DEBUG2 (d, ("ioctl: getmode\n"));
1719 *(int*)data = (c->mode == M_ASYNC) ?
1720 SERIAL_ASYNC : SERIAL_HDLC;
1725 case SERIAL_SETMODE:
1726 CX_DEBUG2 (d, ("ioctl: setmode\n"));
1727 /* Only for superuser! */
1728 error = priv_check (td, PRIV_DRIVER);
1732 /* Somebody is waiting for carrier? */
1735 /* /dev/ttyXX is already opened by someone? */
1736 if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN) &&
1739 /* Network interface is up?
1740 * Cannot change to async mode. */
1741 if (c->mode != M_ASYNC && d->running &&
1742 (*(int*)data == SERIAL_ASYNC))
1747 if (c->mode == M_HDLC && *(int*)data == SERIAL_ASYNC) {
1748 cx_set_mode (c, M_ASYNC);
1749 cx_enable_receive (c, 0);
1750 cx_enable_transmit (c, 0);
1751 } else if (c->mode == M_ASYNC && *(int*)data == SERIAL_HDLC) {
1752 if (d->ifp->if_flags & IFF_DEBUG)
1753 c->debug = c->debug_shadow;
1754 cx_set_mode (c, M_HDLC);
1755 cx_enable_receive (c, 1);
1756 cx_enable_transmit (c, 1);
1762 case SERIAL_GETSTAT:
1763 CX_DEBUG2 (d, ("ioctl: getestat\n"));
1764 st = (struct serial_statistics*) data;
1767 st->rintr = c->rintr;
1768 st->tintr = c->tintr;
1769 st->mintr = c->mintr;
1770 st->ibytes = c->ibytes;
1771 st->ipkts = c->ipkts;
1772 st->ierrs = c->ierrs;
1773 st->obytes = c->obytes;
1774 st->opkts = c->opkts;
1775 st->oerrs = c->oerrs;
1780 case SERIAL_CLRSTAT:
1781 CX_DEBUG2 (d, ("ioctl: clrstat\n"));
1782 /* Only for superuser! */
1783 error = priv_check (td, PRIV_DRIVER);
1801 case SERIAL_GETBAUD:
1802 CX_DEBUG2 (d, ("ioctl: getbaud\n"));
1803 if (c->mode == M_ASYNC)
1807 *(long*)data = cx_get_baud(c);
1812 case SERIAL_SETBAUD:
1813 CX_DEBUG2 (d, ("ioctl: setbaud\n"));
1814 /* Only for superuser! */
1815 error = priv_check (td, PRIV_DRIVER);
1818 if (c->mode == M_ASYNC)
1822 cx_set_baud (c, *(long*)data);
1827 case SERIAL_GETLOOP:
1828 CX_DEBUG2 (d, ("ioctl: getloop\n"));
1829 if (c->mode == M_ASYNC)
1833 *(int*)data = cx_get_loop (c);
1838 case SERIAL_SETLOOP:
1839 CX_DEBUG2 (d, ("ioctl: setloop\n"));
1840 /* Only for superuser! */
1841 error = priv_check (td, PRIV_DRIVER);
1844 if (c->mode == M_ASYNC)
1848 cx_set_loop (c, *(int*)data);
1853 case SERIAL_GETDPLL:
1854 CX_DEBUG2 (d, ("ioctl: getdpll\n"));
1855 if (c->mode == M_ASYNC)
1859 *(int*)data = cx_get_dpll (c);
1864 case SERIAL_SETDPLL:
1865 CX_DEBUG2 (d, ("ioctl: setdpll\n"));
1866 /* Only for superuser! */
1867 error = priv_check (td, PRIV_DRIVER);
1870 if (c->mode == M_ASYNC)
1874 cx_set_dpll (c, *(int*)data);
1879 case SERIAL_GETNRZI:
1880 CX_DEBUG2 (d, ("ioctl: getnrzi\n"));
1881 if (c->mode == M_ASYNC)
1885 *(int*)data = cx_get_nrzi (c);
1890 case SERIAL_SETNRZI:
1891 CX_DEBUG2 (d, ("ioctl: setnrzi\n"));
1892 /* Only for superuser! */
1893 error = priv_check (td, PRIV_DRIVER);
1896 if (c->mode == M_ASYNC)
1900 cx_set_nrzi (c, *(int*)data);
1905 case SERIAL_GETDEBUG:
1906 CX_DEBUG2 (d, ("ioctl: getdebug\n"));
1909 *(int*)data = c->debug;
1914 case SERIAL_SETDEBUG:
1915 CX_DEBUG2 (d, ("ioctl: setdebug\n"));
1916 /* Only for superuser! */
1917 error = priv_check (td, PRIV_DRIVER);
1923 if (c->mode == M_ASYNC) {
1924 c->debug = *(int*)data;
1927 * The debug_shadow is always greater than zero for
1928 * logic simplicity. For switching debug off the
1929 * IFF_DEBUG is responsible (for !M_ASYNC mode).
1931 c->debug_shadow = (*(int*)data) ? (*(int*)data) : 1;
1932 if (d->ifp->if_flags & IFF_DEBUG)
1933 c->debug = c->debug_shadow;
1936 c->debug = *(int*)data;
1944 case TIOCSDTR: /* Set DTR */
1945 CX_DEBUG2 (d, ("ioctl: tiocsdtr\n"));
1953 case TIOCCDTR: /* Clear DTR */
1954 CX_DEBUG2 (d, ("ioctl: tioccdtr\n"));
1962 case TIOCMSET: /* Set DTR/RTS */
1963 CX_DEBUG2 (d, ("ioctl: tiocmset\n"));
1966 cx_set_dtr (c, (*(int*)data & TIOCM_DTR) ? 1 : 0);
1967 cx_set_rts (c, (*(int*)data & TIOCM_RTS) ? 1 : 0);
1972 case TIOCMBIS: /* Add DTR/RTS */
1973 CX_DEBUG2 (d, ("ioctl: tiocmbis\n"));
1976 if (*(int*)data & TIOCM_DTR) cx_set_dtr (c, 1);
1977 if (*(int*)data & TIOCM_RTS) cx_set_rts (c, 1);
1982 case TIOCMBIC: /* Clear DTR/RTS */
1983 CX_DEBUG2 (d, ("ioctl: tiocmbic\n"));
1986 if (*(int*)data & TIOCM_DTR) cx_set_dtr (c, 0);
1987 if (*(int*)data & TIOCM_RTS) cx_set_rts (c, 0);
1992 case TIOCMGET: /* Get modem status */
1993 CX_DEBUG2 (d, ("ioctl: tiocmget\n"));
1994 *(int*)data = cx_modem_status (d);
1999 CX_DEBUG2 (d, ("ioctl: 0x%lx\n", cmd));
2003 void cx_softintr (void *unused)
2009 while (MY_SOFT_INTR) {
2011 for (i=0; i<NCX*NCHAN; ++i) {
2013 if (!d || !d->chan || d->chan->type == T_NONE
2014 || d->chan->mode != M_ASYNC || !d->tty
2020 if (d->intr_action & CX_READ) {
2022 if (d->tty->t_state & TS_CAN_BYPASS_L_RINT) {
2024 if (d->tty->t_rawq.c_cc + k >
2026 && (d->tty->t_cflag & CRTS_IFLOW
2027 || d->tty->t_iflag & IXOFF)
2028 && !(d->tty->t_state & TS_TBLOCK))
2030 d->tty->t_rawcc += k;
2036 putc (ic, &d->tty->t_rawq);
2041 if (d->tty->t_state & TS_TTSTOP
2042 && (d->tty->t_iflag & IXANY
2043 || d->tty->t_cc[VSTART] ==
2044 d->tty->t_cc[VSTOP])) {
2045 d->tty->t_state &= ~TS_TTSTOP;
2046 d->tty->t_lflag &= ~FLUSHO;
2047 d->intr_action |= CX_WRITE;
2050 while (q->end != q->beg) {
2054 ttyld_rint (d->tty, ic);
2059 d->intr_action &= ~CX_READ;
2066 if (d->intr_action & CX_WRITE) {
2068 ttyld_start (d->tty);
2071 d->intr_action &= ~CX_WRITE;
2081 * Fill transmitter buffer with data.
2083 static void cx_oproc (struct tty *tp)
2088 static u_char buf[DMABUFSZ];
2090 u_short len = 0, sublen = 0;
2095 CX_DEBUG2 (d, ("cx_oproc\n"));
2100 if (tp->t_cflag & CRTSCTS && (tp->t_state & TS_TBLOCK) && d->chan->rts)
2101 cx_set_rts (d->chan, 0);
2102 else if (tp->t_cflag & CRTSCTS && ! (tp->t_state & TS_TBLOCK) && ! d->chan->rts)
2103 cx_set_rts (d->chan, 1);
2105 if (! (tp->t_state & (TS_TIMEOUT | TS_TTSTOP))) {
2106 /* Start transmitter. */
2107 cx_enable_transmit (d->chan, 1);
2110 if (! cx_buf_free (d->chan)) {
2111 tp->t_state |= TS_BUSY;
2116 if (tp->t_iflag & IXOFF) {
2117 p = (buf + (DMABUFSZ/2));
2118 sublen = q_to_b (&tp->t_outq, p, (DMABUFSZ/2));
2121 /* Send XON/XOFF out of band. */
2122 if (*p == tp->t_cc[VSTOP]) {
2123 cx_xflow_ctl (d->chan, 0);
2127 if (*p == tp->t_cc[VSTART]) {
2128 cx_xflow_ctl (d->chan, 1);
2138 len = q_to_b (&tp->t_outq, p, (DMABUFSZ/2));
2141 cx_send_packet (d->chan, buf, len, 0);
2142 tp->t_state |= TS_BUSY;
2144 CX_DEBUG2 (d, ("out %d bytes\n", len));
2152 static int cx_param (struct tty *tp, struct termios *t)
2156 int s, bits, parity;
2163 if (t->c_ospeed == 0) {
2164 /* Clear DTR and RTS. */
2165 cx_set_dtr (d->chan, 0);
2168 CX_DEBUG2 (d, ("cx_param (hangup)\n"));
2171 CX_DEBUG2 (d, ("cx_param\n"));
2173 /* Check requested parameters. */
2174 if (t->c_ospeed < 300 || t->c_ospeed > 256*1024) {
2179 if (t->c_ispeed && (t->c_ispeed < 300 || t->c_ispeed > 256*1024)) {
2185 /* And copy them to tty and channel structures. */
2186 tp->t_ispeed = t->c_ispeed = tp->t_ospeed = t->c_ospeed;
2187 tp->t_cflag = t->c_cflag;
2189 /* Set character length and parity mode. */
2190 switch (t->c_cflag & CSIZE) {
2192 case CS8: bits = 8; break;
2193 case CS7: bits = 7; break;
2194 case CS6: bits = 6; break;
2195 case CS5: bits = 5; break;
2198 parity = ((t->c_cflag & PARENB) ? 1 : 0) *
2199 (1 + ((t->c_cflag & PARODD) ? 0 : 1));
2201 /* Set current channel number. */
2203 cx_set_dtr (d->chan, 1);
2206 cx_set_async_param (d->chan, t->c_ospeed, bits, parity, (t->c_cflag & CSTOPB),
2207 !(t->c_cflag & PARENB), (t->c_cflag & CRTSCTS),
2208 (t->c_iflag & IXON), (t->c_iflag & IXANY),
2209 t->c_cc[VSTART], t->c_cc[VSTOP]);
2216 * Stop output on a line
2218 static void cx_stop (struct tty *tp, int flag)
2229 if (tp->t_state & TS_BUSY) {
2230 /* Stop transmitter */
2231 CX_DEBUG2 (d, ("cx_stop\n"));
2232 cx_transmitter_ctl (d->chan, 0);
2239 * Process the (delayed) carrier signal setup.
2241 static void cx_carrier (void *arg)
2244 bdrv_t *bd = d->board->sys;
2245 cx_chan_t *c = d->chan;
2253 CX_DEBUG (d, ("carrier on\n"));
2258 ttyld_modem(d->tty, 1);
2260 CX_DEBUG (d, ("carrier loss\n"));
2265 ttyld_modem(d->tty, 0);
2274 * Modem signal callback function.
2276 static void cx_modem (cx_chan_t *c)
2280 if (!d || c->mode != M_ASYNC)
2282 /* Handle carrier detect/loss. */
2283 /* Carrier changed - delay processing DCD for a while
2284 * to give both sides some time to initialize. */
2285 callout_reset (&d->dcd_timeout_handle, hz/2, cx_carrier, d);
2289 static int ng_cx_constructor (node_p node)
2291 drv_t *d = NG_NODE_PRIVATE (node);
2292 CX_DEBUG (d, ("Constructor\n"));
2296 static int ng_cx_newhook (node_p node, hook_p hook, const char *name)
2299 drv_t *d = NG_NODE_PRIVATE (node);
2300 bdrv_t *bd = d->board->sys;
2302 if (d->chan->mode == M_ASYNC)
2305 /* Attach debug hook */
2306 if (strcmp (name, NG_CX_HOOK_DEBUG) == 0) {
2307 NG_HOOK_SET_PRIVATE (hook, NULL);
2308 d->debug_hook = hook;
2312 /* Check for raw hook */
2313 if (strcmp (name, NG_CX_HOOK_RAW) != 0)
2316 NG_HOOK_SET_PRIVATE (hook, d);
2326 static int print_modems (char *s, cx_chan_t *c, int need_header)
2328 int status = cx_modem_status (c->sys);
2332 length += sprintf (s + length, " LE DTR DSR RTS CTS CD\n");
2333 length += sprintf (s + length, "%4s %4s %4s %4s %4s %4s\n",
2334 status & TIOCM_LE ? "On" : "-",
2335 status & TIOCM_DTR ? "On" : "-",
2336 status & TIOCM_DSR ? "On" : "-",
2337 status & TIOCM_RTS ? "On" : "-",
2338 status & TIOCM_CTS ? "On" : "-",
2339 status & TIOCM_CD ? "On" : "-");
2343 static int print_stats (char *s, cx_chan_t *c, int need_header)
2348 length += sprintf (s + length, " Rintr Tintr Mintr Ibytes Ipkts Ierrs Obytes Opkts Oerrs\n");
2349 length += sprintf (s + length, "%7ld %7ld %7ld %8ld %7ld %7ld %8ld %7ld %7ld\n",
2350 c->rintr, c->tintr, c->mintr, c->ibytes, c->ipkts,
2351 c->ierrs, c->obytes, c->opkts, c->oerrs);
2355 static int print_chan (char *s, cx_chan_t *c)
2360 length += sprintf (s + length, "cx%d", c->board->num * NCHAN + c->num);
2362 length += sprintf (s + length, " debug=%d", d->chan->debug);
2364 if (cx_get_baud (c))
2365 length += sprintf (s + length, " %ld", cx_get_baud (c));
2367 length += sprintf (s + length, " extclock");
2369 if (c->mode == M_HDLC) {
2370 length += sprintf (s + length, " dpll=%s", cx_get_dpll (c) ? "on" : "off");
2371 length += sprintf (s + length, " nrzi=%s", cx_get_nrzi (c) ? "on" : "off");
2374 length += sprintf (s + length, " loop=%s", cx_get_loop (c) ? "on\n" : "off\n");
2378 static int ng_cx_rcvmsg (node_p node, item_p item, hook_p lasthook)
2380 drv_t *d = NG_NODE_PRIVATE (node);
2381 struct ng_mesg *msg;
2382 struct ng_mesg *resp = NULL;
2388 CX_DEBUG (d, ("Rcvmsg\n"));
2389 NGI_GET_MSG (item, msg);
2390 switch (msg->header.typecookie) {
2396 printf ("Don't forget to implement\n");
2400 case NGM_GENERIC_COOKIE:
2401 switch (msg->header.cmd) {
2406 case NGM_TEXT_STATUS: {
2409 int dl = sizeof (struct ng_mesg) + 730;
2411 NG_MKRESPONSE (resp, msg, dl, M_NOWAIT);
2418 l += print_chan (s + l, d->chan);
2419 l += print_stats (s + l, d->chan, 1);
2420 l += print_modems (s + l, d->chan, 1);
2421 strncpy ((resp)->header.cmdstr, "status", NG_CMDSTRLEN);
2427 NG_RESPOND_MSG (error, node, item, resp);
2432 static int ng_cx_rcvdata (hook_p hook, item_p item)
2434 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE(hook));
2436 struct ng_tag_prio *ptag;
2441 NGI_GET_M (item, m);
2442 NG_FREE_ITEM (item);
2443 if (! NG_HOOK_PRIVATE (hook) || ! d) {
2449 /* Check for high priority data */
2450 if ((ptag = (struct ng_tag_prio *)m_tag_locate(m, NGM_GENERIC_COOKIE,
2451 NG_TAG_PRIO, NULL)) != NULL && (ptag->priority > NG_PRIO_CUTOFF) )
2459 if (_IF_QFULL (q)) {
2475 static int ng_cx_rmnode (node_p node)
2477 drv_t *d = NG_NODE_PRIVATE (node);
2480 CX_DEBUG (d, ("Rmnode\n"));
2481 if (d && d->running) {
2490 if (node->nd_flags & NGF_REALLY_DIE) {
2491 NG_NODE_SET_PRIVATE (node, NULL);
2492 NG_NODE_UNREF (node);
2494 NG_NODE_REVIVE(node); /* Persistant node */
2499 static void ng_cx_watchdog (void *arg)
2503 if (d->timeout == 1)
2507 callout_reset (&d->timeout_handle, hz, ng_cx_watchdog, d);
2510 static int ng_cx_connect (hook_p hook)
2512 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2514 callout_reset (&d->timeout_handle, hz, ng_cx_watchdog, d);
2518 static int ng_cx_disconnect (hook_p hook)
2520 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2521 bdrv_t *bd = d->board->sys;
2526 if (NG_HOOK_PRIVATE (hook))
2530 /* If we were wait it than it reasserted now, just stop it. */
2531 if (!callout_drain (&d->timeout_handle))
2532 callout_stop (&d->timeout_handle);
2537 static int cx_modevent (module_t mod, int type, void *unused)
2539 static int load_count = 0;
2542 cx_cdevsw.d_flags &= ~D_NEEDGIANT;
2547 if (ng_newtype (&typestruct))
2548 printf ("Failed to register ng_cx\n");
2552 callout_init (&timeout_handle, cx_mpsafenet?CALLOUT_MPSAFE:0);
2553 callout_reset (&timeout_handle, hz*5, cx_timeout, 0);
2554 /* Software interrupt. */
2555 swi_add(&tty_intr_event, "cx", cx_softintr, NULL, SWI_TTY,
2556 (cx_mpsafenet?INTR_MPSAFE:0), &cx_fast_ih);
2559 if (load_count == 1) {
2560 printf ("Removing device entry for Sigma\n");
2562 ng_rmtype (&typestruct);
2565 /* If we were wait it than it reasserted now, just stop it. */
2566 if (!callout_drain (&timeout_handle))
2567 callout_stop (&timeout_handle);
2568 swi_remove (cx_fast_ih);
2578 static struct ng_type typestruct = {
2579 .version = NG_ABI_VERSION,
2580 .name = NG_CX_NODE_TYPE,
2581 .constructor = ng_cx_constructor,
2582 .rcvmsg = ng_cx_rcvmsg,
2583 .shutdown = ng_cx_rmnode,
2584 .newhook = ng_cx_newhook,
2585 .connect = ng_cx_connect,
2586 .rcvdata = ng_cx_rcvdata,
2587 .disconnect = ng_cx_disconnect,
2592 MODULE_DEPEND (ng_cx, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION);
2594 MODULE_DEPEND (isa_cx, sppp, 1, 1, 1);
2596 DRIVER_MODULE (cx, isa, cx_isa_driver, cx_devclass, cx_modevent, NULL);
2597 MODULE_VERSION (cx, 1);