11590Srgrimes/*-
21590Srgrimes * Cronyx-Sigma adapter driver for FreeBSD.
31590Srgrimes * Supports PPP/HDLC and Cisco/HDLC protocol in synchronous mode,
41590Srgrimes * and asynchronous channels with full modem control.
51590Srgrimes * Keepalive protocol implemented in both Cisco and PPP modes.
61590Srgrimes *
71590Srgrimes * Copyright (C) 1994-2002 Cronyx Engineering.
81590Srgrimes * Author: Serge Vakulenko, <vak@cronyx.ru>
91590Srgrimes *
101590Srgrimes * Copyright (C) 1999-2004 Cronyx Engineering.
111590Srgrimes * Rewritten on DDK, ported to NETGRAPH, rewritten for FreeBSD 3.x-5.x by
121590Srgrimes * Kurakin Roman, <rik@cronyx.ru>
131590Srgrimes *
141590Srgrimes * This software is distributed with NO WARRANTIES, not even the implied
151590Srgrimes * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
161590Srgrimes *
171590Srgrimes * Authors grant any other persons or organisations a permission to use,
181590Srgrimes * modify and redistribute this software in source and binary forms,
191590Srgrimes * as long as this message is kept with the software, all derivative
201590Srgrimes * works or modified versions.
211590Srgrimes *
221590Srgrimes * Cronyx Id: if_cx.c,v 1.1.2.34 2004/06/23 17:09:13 rik Exp $
231590Srgrimes */
241590Srgrimes
251590Srgrimes#include <sys/cdefs.h>
261590Srgrimes__FBSDID("$FreeBSD: stable/11/sys/dev/cx/if_cx.c 358804 2020-03-09 13:06:02Z emaste $");
271590Srgrimes
281590Srgrimes#include <sys/param.h>
291590Srgrimes
301590Srgrimes#include <sys/systm.h>
311590Srgrimes#include <sys/kernel.h>
321590Srgrimes#include <sys/module.h>
331590Srgrimes#include <sys/priv.h>
341590Srgrimes#include <sys/proc.h>
351590Srgrimes#include <sys/mbuf.h>
361590Srgrimes#include <sys/sockio.h>
371590Srgrimes#include <sys/malloc.h>
38194792Sdelphij#include <sys/socket.h>
391590Srgrimes#include <sys/sysctl.h>
401590Srgrimes#include <sys/conf.h>
411590Srgrimes#include <sys/errno.h>
421590Srgrimes#include <sys/serial.h>
431590Srgrimes#include <sys/tty.h>
4432649Sbde#include <sys/bus.h>
4532649Sbde#include <machine/bus.h>
4632649Sbde#include <sys/rman.h>
473819Swollman#include <isa/isavar.h>
4850477Speter#include <sys/fcntl.h>
491590Srgrimes#include <sys/interrupt.h>
501590Srgrimes#include <vm/vm.h>
511590Srgrimes#include <vm/pmap.h>
52194190Sed#include <net/if.h>
531590Srgrimes#include <net/if_var.h>
5411936Sphk#include <machine/cpufunc.h>
553819Swollman#include <machine/cserial.h>
569336Sdfr#include <machine/resource.h>
579336Sdfr#include <dev/cx/machdep.h>
5883653Speter#include <dev/cx/cxddk.h>
5983653Speter#include <dev/cx/cronyxfw.h>
60192762Srmacklem#include "opt_ng_cronyx.h"
61192762Srmacklem#ifdef NETGRAPH_CRONYX
62192762Srmacklem#   include "opt_netgraph.h"
63192762Srmacklem#   include <netgraph/ng_message.h>
641590Srgrimes#   include <netgraph/netgraph.h>
651590Srgrimes#   include <dev/cx/ng_cx.h>
661590Srgrimes#else
671590Srgrimes#   include <net/if_types.h>
681590Srgrimes#   include <net/if_sppp.h>
6977207Stmm#   define PP_CISCO IFF_LINK2
701590Srgrimes#   include <net/bpf.h>
711590Srgrimes#endif
721590Srgrimes
731590Srgrimes#define NCX	1
741590Srgrimes
751590Srgrimes/* If we don't have Cronyx's sppp version, we don't have fr support via sppp */
763819Swollman#ifndef PP_FR
771590Srgrimes#define PP_FR 0
781590Srgrimes#endif
791590Srgrimes
80194792Sdelphij#define CX_DEBUG(d,s)	({if (d->chan->debug) {\
8183653Speter				printf ("%s: ", d->name); printf s;}})
82194792Sdelphij#define CX_DEBUG2(d,s)	({if (d->chan->debug>1) {\
83194792Sdelphij				printf ("%s: ", d->name); printf s;}})
841590Srgrimes
851590Srgrimes#define CX_LOCK_NAME	"cxX"
861590Srgrimes
873819Swollman#define CX_LOCK(_bd)		mtx_lock (&(_bd)->cx_mtx)
8852493Sdillon#define CX_UNLOCK(_bd)		mtx_unlock (&(_bd)->cx_mtx)
89172759Sjhb#define CX_LOCK_ASSERT(_bd)	mtx_assert (&(_bd)->cx_mtx, MA_OWNED)
90192762Srmacklem
91192762Srmacklemtypedef struct _async_q {
92192762Srmacklem	int beg;
931590Srgrimes	int end;
9492921Simp	#define BF_SZ 14400
9592921Simp	int buf[BF_SZ+1];
9692921Simp} async_q;
9792921Simp
9892921Simp#define AQ_GSZ(q)	((BF_SZ + (q)->end - (q)->beg)%BF_SZ)
9992921Simp#define AQ_PUSH(q,c)	{*((q)->buf + (q)->end) = c;\
100192762Srmacklem			(q)->end = ((q)->end + 1)%BF_SZ;}
101192762Srmacklem#define AQ_POP(q,c)	{c = *((q)->buf + (q)->beg);\
1023819Swollman			(q)->beg = ((q)->beg + 1)%BF_SZ;}
10352493Sdillon
10452493Sdillonstatic void cx_identify		__P((driver_t *, device_t));
105131990Sstefanfstatic int cx_probe		__P((device_t));
106172759Sjhbstatic int cx_attach		__P((device_t));
1071590Srgrimesstatic int cx_detach		__P((device_t));
1081590Srgrimesstatic t_open_t			cx_topen;
10952493Sdillonstatic t_modem_t		cx_tmodem;
11052493Sdillonstatic t_close_t		cx_tclose;
1111590Srgrimes
1121590Srgrimesstatic device_method_t cx_isa_methods [] = {
11377207Stmm	DEVMETHOD(device_identify,	cx_identify),
1141590Srgrimes	DEVMETHOD(device_probe,		cx_probe),
1151590Srgrimes	DEVMETHOD(device_attach,	cx_attach),
1161590Srgrimes	DEVMETHOD(device_detach,	cx_detach),
117193258Srmacklem
1181590Srgrimes	DEVMETHOD_END
1191590Srgrimes};
1201590Srgrimes
1211590Srgrimestypedef struct _cx_dma_mem_t {
1221590Srgrimes	unsigned long	phys;
1231590Srgrimes	void		*virt;
1241590Srgrimes	size_t		size;
12552493Sdillon	bus_dma_tag_t	dmat;
12652493Sdillon	bus_dmamap_t	mapp;
12752493Sdillon} cx_dma_mem_t;
1281590Srgrimes
1291590Srgrimestypedef struct _drv_t {
1301590Srgrimes	char name [8];
13152493Sdillon	cx_chan_t *chan;
13252493Sdillon	cx_board_t *board;
13352493Sdillon	cx_dma_mem_t dmamem;
13452493Sdillon	struct tty *tty;
13552493Sdillon	struct callout dcd_timeout_handle;
13652493Sdillon	unsigned callout;
13752493Sdillon	unsigned lock;
13852493Sdillon	int open_dev;
13952493Sdillon	int cd;
14052493Sdillon	int running;
141172759Sjhb#ifdef NETGRAPH
142172759Sjhb	char	nodename [NG_NODESIZ];
143172759Sjhb	hook_p	hook;
144193258Srmacklem	hook_p	debug_hook;
145192762Srmacklem	node_p	node;
146192762Srmacklem	struct	ifqueue lo_queue;
1471590Srgrimes	struct	ifqueue hi_queue;
1481590Srgrimes#else
1491590Srgrimes	struct	ifqueue queue;
1501590Srgrimes	struct	ifnet *ifp;
1511590Srgrimes#endif
1521590Srgrimes	short	timeout;
1531590Srgrimes	struct	callout timeout_handle;
1541590Srgrimes	struct	cdev *devt;
1551590Srgrimes	async_q	aqueue;
1561590Srgrimes#define CX_READ 1
1571590Srgrimes#define CX_WRITE 2
1581590Srgrimes	int intr_action;
1591590Srgrimes	short atimeout;
1601590Srgrimes} drv_t;
1611590Srgrimes
1621590Srgrimestypedef struct _bdrv_t {
1631590Srgrimes	cx_board_t	*board;
1641590Srgrimes	struct resource	*base_res;
165192762Srmacklem	struct resource	*drq_res;
166192762Srmacklem	struct resource	*irq_res;
167192762Srmacklem	int		base_rid;
168192762Srmacklem	int		drq_rid;
169192762Srmacklem	int		irq_rid;
170192762Srmacklem	void		*intrhand;
171192762Srmacklem	drv_t		channel [NCHAN];
1723819Swollman	struct mtx	cx_mtx;
1731590Srgrimes} bdrv_t;
1743819Swollman
1753819Swollmanstatic driver_t cx_isa_driver = {
1763819Swollman	"cx",
1773819Swollman	cx_isa_methods,
1783819Swollman	sizeof (bdrv_t),
1793819Swollman};
1803819Swollman
1811590Srgrimesstatic devclass_t cx_devclass;
1821590Srgrimes
183192762Srmacklemextern long csigma_fw_len;
184192762Srmacklemextern const char *csigma_fw_version;
185192762Srmacklemextern const char *csigma_fw_date;
186192762Srmacklemextern const char *csigma_fw_copyright;
187192762Srmacklemextern const cr_dat_tst_t csigma_fw_tvec[];
188192762Srmacklemextern const u_char csigma_fw_data[];
189192762Srmacklemstatic void cx_oproc (struct tty *tp);
190192762Srmacklemstatic int cx_param (struct tty *tp, struct termios *t);
191192762Srmacklemstatic void cx_stop (struct tty *tp, int flag);
192192762Srmacklemstatic void cx_receive (cx_chan_t *c, char *data, int len);
193192762Srmacklemstatic void cx_transmit (cx_chan_t *c, void *attachment, int len);
1941590Srgrimesstatic void cx_error (cx_chan_t *c, int data);
1951590Srgrimesstatic void cx_modem (cx_chan_t *c);
1961590Srgrimesstatic void cx_up (drv_t *d);
1971590Srgrimesstatic void cx_start (drv_t *d);
1983819Swollmanstatic void cx_softintr (void *);
1993819Swollmanstatic void *cx_fast_ih;
2003819Swollmanstatic void cx_down (drv_t *d);
201194792Sdelphijstatic void cx_watchdog (drv_t *d);
202172759Sjhbstatic void cx_watchdog_timer (void *arg);
2033819Swollmanstatic void cx_carrier (void *arg);
204172759Sjhb
205172759Sjhb#ifdef NETGRAPH
206172759Sjhbextern struct ng_type typestruct;
207172759Sjhb#else
20883653Speterstatic void cx_ifstart (struct ifnet *ifp);
20983653Speterstatic void cx_tlf (struct sppp *sp);
21083653Speterstatic void cx_tls (struct sppp *sp);
211172759Sjhbstatic int cx_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data);
212172759Sjhbstatic void cx_initialize (void *softc);
21383653Speter#endif
2143819Swollman
215172759Sjhbstatic cx_board_t *adapter [NCX];
216172759Sjhbstatic drv_t *channel [NCX*NCHAN];
217172759Sjhbstatic struct callout led_timo [NCX];
21883653Speterstatic struct callout timeout_handle;
21983653Speter
2203819Swollmanstatic int cx_open (struct cdev *dev, int flag, int mode, struct thread *td);
221172759Sjhbstatic int cx_close (struct cdev *dev, int flag, int mode, struct thread *td);
222172759Sjhbstatic int cx_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td);
22383653Speterstatic struct cdevsw cx_cdevsw = {
224172759Sjhb	.d_version  = D_VERSION,
225172759Sjhb	.d_open     = cx_open,
226172759Sjhb	.d_close    = cx_close,
227172759Sjhb	.d_ioctl    = cx_ioctl,
22883653Speter	.d_name     = "cx",
2293819Swollman	.d_flags    = D_TTY,
23083653Speter};
231172759Sjhb
232172759Sjhbstatic int MY_SOFT_INTR;
233172759Sjhb
234172759Sjhb/*
235172759Sjhb * Make an mbuf from data.
23683653Speter */
23783653Speterstatic struct mbuf *makembuf (void *buf, u_int len)
2383819Swollman{
2393819Swollman	struct mbuf *m, *o, *p;
2403819Swollman
2413819Swollman	MGETHDR (m, M_NOWAIT, MT_DATA);
2421590Srgrimes
2431590Srgrimes	if (! m)
2441590Srgrimes		return 0;
24552493Sdillon
2461590Srgrimes	if (len >= MINCLSIZE)
24783653Speter		MCLGET (m, M_NOWAIT);
24883653Speter
2491590Srgrimes	m->m_pkthdr.len = len;
250172759Sjhb	m->m_len = 0;
251172759Sjhb
252172759Sjhb	p = m;
253172759Sjhb	while (len) {
254172759Sjhb		u_int n = M_TRAILINGSPACE (p);
255172759Sjhb		if (n > len)
256172759Sjhb			n = len;
257172759Sjhb		if (! n) {
258172759Sjhb			/* Allocate new mbuf. */
259172759Sjhb			o = p;
260172759Sjhb			MGET (p, M_NOWAIT, MT_DATA);
261172759Sjhb			if (! p) {
2623819Swollman				m_freem (m);
263172759Sjhb				return 0;
26483653Speter			}
26583653Speter			if (len >= MINCLSIZE)
26683653Speter				MCLGET (p, M_NOWAIT);
26783653Speter			p->m_len = 0;
26883653Speter			o->m_next = p;
26952493Sdillon
27052493Sdillon			n = M_TRAILINGSPACE (p);
27152493Sdillon			if (n > len)
27252493Sdillon				n = len;
27352493Sdillon		}
27452493Sdillon		bcopy (buf, mtod (p, caddr_t) + p->m_len, n);
27552493Sdillon
27652493Sdillon		p->m_len += n;
27752493Sdillon		buf = n + (char*) buf;
27852493Sdillon		len -= n;
27952493Sdillon	}
28052493Sdillon	return m;
28152493Sdillon}
28252493Sdillon
28352493Sdillon/*
28452493Sdillon * Recover after lost transmit interrupts.
28552493Sdillon */
28652493Sdillonstatic void cx_timeout (void *arg)
28752493Sdillon{
28852493Sdillon	drv_t *d;
28952493Sdillon	int s, i, k;
29052493Sdillon
29152493Sdillon	for (i = 0; i < NCX; i++) {
29252493Sdillon		if (adapter[i] == NULL)
29352493Sdillon			continue;
29452493Sdillon		for (k = 0; k < NCHAN; ++k) {
29552493Sdillon			d = channel[i * NCHAN + k];
29683653Speter			if (! d)
29783653Speter				continue;
29883653Speter			s = splhigh ();
29952493Sdillon			CX_LOCK ((bdrv_t *)d->board->sys);
30052493Sdillon			if (d->atimeout == 1 && d->tty && d->tty->t_state & TS_BUSY) {
30152493Sdillon				d->tty->t_state &= ~TS_BUSY;
30252493Sdillon				if (d->tty->t_dev) {
30383653Speter					d->intr_action |= CX_WRITE;
30452493Sdillon					MY_SOFT_INTR = 1;
30552493Sdillon					swi_sched (cx_fast_ih, 0);
30652493Sdillon				}
30752493Sdillon				CX_DEBUG (d, ("cx_timeout\n"));
30852493Sdillon			}
30952493Sdillon			if (d->atimeout)
31052493Sdillon				d->atimeout--;
31152493Sdillon			CX_UNLOCK ((bdrv_t *)d->board->sys);
31252493Sdillon			splx (s);
31352493Sdillon		}
31452493Sdillon	}
31552493Sdillon	callout_reset (&timeout_handle, hz*5, cx_timeout, 0);
31652493Sdillon}
31752493Sdillon
31852493Sdillonstatic void cx_led_off (void *arg)
31952493Sdillon{
32052493Sdillon	cx_board_t *b = arg;
32152493Sdillon	bdrv_t *bd = b->sys;
32252493Sdillon	int s;
32352493Sdillon
32452493Sdillon	s = splhigh ();
32552493Sdillon	CX_LOCK (bd);
32652493Sdillon	cx_led (b, 0);
32752493Sdillon	CX_UNLOCK (bd);
32852493Sdillon	splx (s);
32952493Sdillon}
33052493Sdillon
33152493Sdillon/*
33252493Sdillon * Activate interrupt handler from DDK.
33352493Sdillon */
33452493Sdillonstatic void cx_intr (void *arg)
33552493Sdillon{
33652493Sdillon	bdrv_t *bd = arg;
33752493Sdillon	cx_board_t *b = bd->board;
33883653Speter#ifndef NETGRAPH
33983653Speter	int i;
34083653Speter#endif
34183653Speter	int s = splhigh ();
34252493Sdillon
34352493Sdillon	CX_LOCK (bd);
34452493Sdillon	/* Turn LED on. */
34552493Sdillon	cx_led (b, 1);
34652493Sdillon
34752493Sdillon	cx_int_handler (b);
34883653Speter
34983653Speter	/* Turn LED off 50 msec later. */
35083653Speter	callout_reset (&led_timo[b->num], hz/20, cx_led_off, b);
35183653Speter	CX_UNLOCK (bd);
35283653Speter	splx (s);
35383653Speter
35483653Speter#ifndef NETGRAPH
35583653Speter	/* Pass packets in a lock-free state */
35652493Sdillon	for (i = 0; i < NCHAN && b->chan[i].type; i++) {
35752493Sdillon		drv_t *d = b->chan[i].sys;
35852493Sdillon		struct mbuf *m;
35952493Sdillon		if (!d || !d->running)
36083653Speter			continue;
36183653Speter		while (_IF_QLEN(&d->queue)) {
36283653Speter			IF_DEQUEUE (&d->queue,m);
36383653Speter			if (!m)
36483653Speter				continue;
36583653Speter			sppp_input (d->ifp, m);
36683653Speter		}
36783653Speter	}
36883653Speter#endif
36983653Speter}
37083653Speter
37183653Speterstatic int probe_irq (cx_board_t *b, int irq)
37283653Speter{
37383653Speter	int mask, busy, cnt;
37483653Speter
37583653Speter	/* Clear pending irq, if any. */
37652493Sdillon	cx_probe_irq (b, -irq);
37783653Speter	DELAY (100);
37852493Sdillon	for (cnt=0; cnt<5; ++cnt) {
37983653Speter		/* Get the mask of pending irqs, assuming they are busy.
38052493Sdillon		 * Activate the adapter on given irq. */
38152493Sdillon		busy = cx_probe_irq (b, irq);
38252493Sdillon		DELAY (100);
38352493Sdillon
38483653Speter		/* Get the mask of active irqs.
38583653Speter		 * Deactivate our irq. */
38683653Speter		mask = cx_probe_irq (b, -irq);
38783653Speter		DELAY (100);
38852493Sdillon		if ((mask & ~busy) == 1 << irq) {
38952493Sdillon			cx_probe_irq (b, 0);
39052493Sdillon			/* printf ("cx%d: irq %d ok, mask=0x%04x, busy=0x%04x\n",
39152493Sdillon				b->num, irq, mask, busy); */
39283653Speter			return 1;
39383653Speter		}
39483653Speter	}
39583653Speter	/* printf ("cx%d: irq %d not functional, mask=0x%04x, busy=0x%04x\n",
39652493Sdillon		b->num, irq, mask, busy); */
3971590Srgrimes	cx_probe_irq (b, 0);
3981590Srgrimes	return 0;
3991590Srgrimes}
4001590Srgrimes
4011590Srgrimesstatic short porttab [] = {
4021590Srgrimes	0x200, 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0,
4031590Srgrimes	0x300, 0x320, 0x340, 0x360, 0x380, 0x3a0, 0x3c0, 0x3e0, 0
4041590Srgrimes};
4051590Srgrimesstatic char dmatab [] = { 7, 6, 5, 0 };
4061590Srgrimesstatic char irqtab [] = { 5, 10, 11, 7, 3, 15, 12, 0 };
4071590Srgrimes
40852493Sdillonstatic int cx_is_free_res (device_t dev, int rid, int type, rman_res_t start,
4091590Srgrimes	rman_res_t end, rman_res_t count)
41083653Speter{
41183653Speter	struct resource *res;
41252493Sdillon
4131590Srgrimes	if (!(res = bus_alloc_resource (dev, type, &rid, start, end, count, 0)))
41483653Speter		return 0;
41583653Speter
416172759Sjhb	bus_release_resource (dev, type, rid, res);
41783653Speter
41883653Speter	return 1;
41983653Speter}
42083653Speter
42183653Speterstatic void cx_identify (driver_t *driver, device_t dev)
42283653Speter{
42383653Speter	rman_res_t iobase, rescount;
42483653Speter	int devcount;
42552493Sdillon	device_t *devices;
4261590Srgrimes	device_t child;
42752493Sdillon	devclass_t my_devclass;
42883653Speter	int i, k;
42983653Speter
430172759Sjhb	if ((my_devclass = devclass_find ("cx")) == NULL)
43152493Sdillon		return;
43252493Sdillon
43352493Sdillon	devclass_get_devices (my_devclass, &devices, &devcount);
43452493Sdillon
43552493Sdillon	if (devcount == 0) {
43652493Sdillon		/* We should find all devices by our self. We could alter other
43752493Sdillon		 * devices, but we don't have a choise
4381590Srgrimes		 */
43952493Sdillon		for (i = 0; (iobase = porttab [i]) != 0; i++) {
44052493Sdillon			if (!cx_is_free_res (dev, 0, SYS_RES_IOPORT,
44152493Sdillon			    iobase, iobase + NPORT, NPORT))
44252493Sdillon				continue;
44352493Sdillon			if (cx_probe_board (iobase, -1, -1) == 0)
44452493Sdillon				continue;
44552493Sdillon
44652493Sdillon			devcount++;
44752493Sdillon
44852493Sdillon			child = BUS_ADD_CHILD (dev, ISA_ORDER_SPECULATIVE, "cx",
44952493Sdillon			    -1);
45052493Sdillon
45152493Sdillon			if (child == NULL)
45252493Sdillon				return;
45352493Sdillon
45452493Sdillon			device_set_desc_copy (child, "Cronyx Sigma");
45552493Sdillon			device_set_driver (child, driver);
45652493Sdillon			bus_set_resource (child, SYS_RES_IOPORT, 0,
45752493Sdillon			    iobase, NPORT);
45852493Sdillon
45952493Sdillon			if (devcount >= NCX)
46052493Sdillon				break;
46152493Sdillon		}
46252493Sdillon	} else {
46352493Sdillon		static short porttab [] = {
46452493Sdillon			0x200, 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0,
46552493Sdillon			0x300, 0x320, 0x340, 0x360, 0x380, 0x3a0, 0x3c0, 0x3e0, 0
46652493Sdillon		};
46752493Sdillon		/* Lets check user choise.
46883653Speter		 */
46952493Sdillon		for (k = 0; k < devcount; k++) {
47052493Sdillon			if (bus_get_resource (devices[k], SYS_RES_IOPORT, 0,
47152493Sdillon			    &iobase, &rescount) != 0)
47252493Sdillon				continue;
47383653Speter
47483653Speter			for (i = 0; porttab [i] != 0; i++) {
47583653Speter				if (porttab [i] != iobase)
47683653Speter					continue;
47783653Speter				if (!cx_is_free_res (devices[k], 0, SYS_RES_IOPORT,
47883653Speter				    iobase, iobase + NPORT, NPORT))
47983653Speter					continue;
48083653Speter				if (cx_probe_board (iobase, -1, -1) == 0)
48183653Speter					continue;
48252493Sdillon				porttab [i] = -1;
48383653Speter				device_set_desc_copy (devices[k], "Cronyx Sigma");
48452493Sdillon				break;
4851590Srgrimes			}
48652493Sdillon
4871590Srgrimes			if (porttab [i] == 0) {
4881590Srgrimes				device_delete_child (
4891590Srgrimes				    device_get_parent (devices[k]),
4901590Srgrimes				    devices [k]);
4911590Srgrimes				devices[k] = 0;
49252493Sdillon				continue;
4931590Srgrimes			}
49452493Sdillon		}
49552493Sdillon		for (k = 0; k < devcount; k++) {
49652493Sdillon			if (devices[k] == 0)
49752493Sdillon				continue;
49852493Sdillon			if (bus_get_resource (devices[k], SYS_RES_IOPORT, 0,
49952493Sdillon			    &iobase, &rescount) == 0)
50052493Sdillon				continue;
50152493Sdillon			for (i = 0; (iobase = porttab [i]) != 0; i++) {
5021590Srgrimes				if (porttab [i] == -1) {
5031590Srgrimes					continue;
5041590Srgrimes				}
5051590Srgrimes				if (!cx_is_free_res (devices[k], 0, SYS_RES_IOPORT,
506172759Sjhb				    iobase, iobase + NPORT, NPORT))
5071590Srgrimes					continue;
5081590Srgrimes				if (cx_probe_board (iobase, -1, -1) == 0)
509193258Srmacklem					continue;
5101590Srgrimes
5111590Srgrimes				bus_set_resource (devices[k], SYS_RES_IOPORT, 0,
51252493Sdillon				    iobase, NPORT);
51352493Sdillon				porttab [i] = -1;
51452493Sdillon				device_set_desc_copy (devices[k], "Cronyx Sigma");
51552493Sdillon				break;
51652493Sdillon			}
51752493Sdillon			if (porttab [i] == 0) {
51852493Sdillon				device_delete_child (
51952493Sdillon				    device_get_parent (devices[k]),
52052493Sdillon				    devices [k]);
52152493Sdillon			}
52252493Sdillon		}
52352493Sdillon		free (devices, M_TEMP);
52452493Sdillon	}
52552493Sdillon
52652493Sdillon	return;
52752493Sdillon}
52852493Sdillon
52952493Sdillonstatic int cx_probe (device_t dev)
53052493Sdillon{
53152493Sdillon	int unit = device_get_unit (dev);
53252493Sdillon	int i;
53352493Sdillon	rman_res_t iobase, rescount;
53452493Sdillon
53552493Sdillon	if (!device_get_desc (dev) ||
53652493Sdillon	    strcmp (device_get_desc (dev), "Cronyx Sigma"))
53752493Sdillon		return ENXIO;
53852493Sdillon
53952493Sdillon	if (bus_get_resource (dev, SYS_RES_IOPORT, 0, &iobase, &rescount) != 0) {
54052493Sdillon		printf ("cx%d: Couldn't get IOPORT\n", unit);
54152493Sdillon		return ENXIO;
54252493Sdillon	}
54352493Sdillon
54452493Sdillon	if (!cx_is_free_res (dev, 0, SYS_RES_IOPORT,
54552493Sdillon	    iobase, iobase + NPORT, NPORT)) {
546192762Srmacklem		printf ("cx%d: Resource IOPORT isn't free %lx\n", unit, iobase);
547192762Srmacklem		return ENXIO;
548192762Srmacklem	}
549192762Srmacklem
550192762Srmacklem	for (i = 0; porttab [i] != 0; i++) {
551192762Srmacklem		if (porttab [i] == iobase) {
552192762Srmacklem			porttab [i] = -1;
553192762Srmacklem			break;
554192762Srmacklem		}
555192762Srmacklem	}
556192762Srmacklem
557192762Srmacklem	if (porttab [i] == 0) {
558192762Srmacklem		return ENXIO;
559192762Srmacklem	}
560192762Srmacklem
561192762Srmacklem	if (!cx_probe_board (iobase, -1, -1)) {
562192762Srmacklem		printf ("cx%d: probing for Sigma at %lx faild\n", unit, iobase);
563192762Srmacklem		return ENXIO;
564192762Srmacklem	}
565192762Srmacklem
566192762Srmacklem	return 0;
567192762Srmacklem}
568192762Srmacklem
569192762Srmacklemstatic void
570192762Srmacklemcx_bus_dmamap_addr (void *arg, bus_dma_segment_t *segs, int nseg, int error)
571192762Srmacklem{
572192762Srmacklem	unsigned long *addr;
573192762Srmacklem
574192762Srmacklem	if (error)
575192762Srmacklem		return;
576192762Srmacklem
577192762Srmacklem	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
578192762Srmacklem	addr = arg;
579192762Srmacklem	*addr = segs->ds_addr;
580192762Srmacklem}
581192762Srmacklem
582192762Srmacklemstatic int
583192762Srmacklemcx_bus_dma_mem_alloc (int bnum, int cnum, cx_dma_mem_t *dmem)
584192762Srmacklem{
585192762Srmacklem	int error;
586192762Srmacklem
587192762Srmacklem	error = bus_dma_tag_create (NULL, 16, 0, BUS_SPACE_MAXADDR_24BIT,
588192762Srmacklem		BUS_SPACE_MAXADDR, NULL, NULL, dmem->size, 1,
589192762Srmacklem		dmem->size, 0, NULL, NULL, &dmem->dmat);
590192762Srmacklem	if (error) {
591192762Srmacklem		if (cnum >= 0)	printf ("cx%d-%d: ", bnum, cnum);
592192762Srmacklem		else		printf ("cx%d: ", bnum);
593192762Srmacklem		printf ("couldn't allocate tag for dma memory\n");
594192762Srmacklem 		return 0;
595192762Srmacklem	}
596192762Srmacklem	error = bus_dmamem_alloc (dmem->dmat, (void **)&dmem->virt,
597192762Srmacklem		BUS_DMA_NOWAIT | BUS_DMA_ZERO, &dmem->mapp);
598192762Srmacklem	if (error) {
599192762Srmacklem		if (cnum >= 0)	printf ("cx%d-%d: ", bnum, cnum);
600192762Srmacklem		else		printf ("cx%d: ", bnum);
601192762Srmacklem		printf ("couldn't allocate mem for dma memory\n");
602192762Srmacklem		bus_dma_tag_destroy (dmem->dmat);
603192762Srmacklem 		return 0;
604192762Srmacklem	}
605192762Srmacklem	error = bus_dmamap_load (dmem->dmat, dmem->mapp, dmem->virt,
606192762Srmacklem		dmem->size, cx_bus_dmamap_addr, &dmem->phys, 0);
607192762Srmacklem	if (error) {
608192762Srmacklem		if (cnum >= 0)	printf ("cx%d-%d: ", bnum, cnum);
609192762Srmacklem		else		printf ("cx%d: ", bnum);
610192762Srmacklem		printf ("couldn't load mem map for dma memory\n");
611192762Srmacklem		bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
612192762Srmacklem		bus_dma_tag_destroy (dmem->dmat);
613192762Srmacklem 		return 0;
614192762Srmacklem	}
615192762Srmacklem	return 1;
616192762Srmacklem}
617192762Srmacklem
618192762Srmacklemstatic void
619192762Srmacklemcx_bus_dma_mem_free (cx_dma_mem_t *dmem)
620192762Srmacklem{
621192762Srmacklem	bus_dmamap_unload (dmem->dmat, dmem->mapp);
622192762Srmacklem	bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
623192762Srmacklem	bus_dma_tag_destroy (dmem->dmat);
624192762Srmacklem}
625192762Srmacklem
626192762Srmacklem/*
627192762Srmacklem * The adapter is present, initialize the driver structures.
628192762Srmacklem */
629192762Srmacklemstatic int cx_attach (device_t dev)
630192762Srmacklem{
631192762Srmacklem	bdrv_t *bd = device_get_softc (dev);
632192762Srmacklem	rman_res_t iobase, drq, irq, rescount;
633192762Srmacklem	int unit = device_get_unit (dev);
634192762Srmacklem	char *cx_ln = CX_LOCK_NAME;
635192762Srmacklem	cx_board_t *b;
636192762Srmacklem	cx_chan_t *c;
637192762Srmacklem	drv_t *d;
638192762Srmacklem	int i;
639192762Srmacklem	int s;
640192762Srmacklem
641192762Srmacklem	KASSERT ((bd != NULL), ("cx%d: NULL device softc\n", unit));
642192762Srmacklem
643192762Srmacklem	bus_get_resource (dev, SYS_RES_IOPORT, 0, &iobase, &rescount);
644192762Srmacklem	bd->base_rid = 0;
645192762Srmacklem	bd->base_res = bus_alloc_resource (dev, SYS_RES_IOPORT, &bd->base_rid,
646192762Srmacklem		iobase, iobase + NPORT, NPORT, RF_ACTIVE);
647192762Srmacklem	if (! bd->base_res) {
648192762Srmacklem		printf ("cx%d: cannot allocate base address\n", unit);
649192762Srmacklem		return ENXIO;
650192762Srmacklem	}
651192762Srmacklem
652192762Srmacklem	if (bus_get_resource (dev, SYS_RES_DRQ, 0, &drq, &rescount) != 0) {
653192762Srmacklem		for (i = 0; (drq = dmatab [i]) != 0; i++) {
654192762Srmacklem			if (!cx_is_free_res (dev, 0, SYS_RES_DRQ,
655192762Srmacklem			    drq, drq + 1, 1))
656192762Srmacklem				continue;
657192762Srmacklem			bus_set_resource (dev, SYS_RES_DRQ, 0, drq, 1);
658192762Srmacklem			break;
659192762Srmacklem		}
660192762Srmacklem
661192762Srmacklem		if (dmatab[i] == 0) {
662192762Srmacklem			bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
663192762Srmacklem				bd->base_res);
664192762Srmacklem			printf ("cx%d: Couldn't get DRQ\n", unit);
665192762Srmacklem			return ENXIO;
666192762Srmacklem		}
667192762Srmacklem	}
668192762Srmacklem
669192762Srmacklem	bd->drq_rid = 0;
670192762Srmacklem	bd->drq_res = bus_alloc_resource (dev, SYS_RES_DRQ, &bd->drq_rid,
671192762Srmacklem		drq, drq + 1, 1, RF_ACTIVE);
672192762Srmacklem	if (! bd->drq_res) {
673192762Srmacklem		printf ("cx%d: cannot allocate drq\n", unit);
674192762Srmacklem		bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
675192762Srmacklem			bd->base_res);
676192762Srmacklem		return ENXIO;
677192762Srmacklem	}
678192762Srmacklem
679192762Srmacklem	if (bus_get_resource (dev, SYS_RES_IRQ, 0, &irq, &rescount) != 0) {
680192762Srmacklem		for (i = 0; (irq = irqtab [i]) != 0; i++) {
681192762Srmacklem			if (!cx_is_free_res (dev, 0, SYS_RES_IRQ,
682192762Srmacklem			    irq, irq + 1, 1))
683192762Srmacklem				continue;
684192762Srmacklem			bus_set_resource (dev, SYS_RES_IRQ, 0, irq, 1);
685192762Srmacklem			break;
686192762Srmacklem		}
687192762Srmacklem
688192762Srmacklem		if (irqtab[i] == 0) {
689192762Srmacklem			bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
690192762Srmacklem				bd->drq_res);
691192762Srmacklem			bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
692192762Srmacklem				bd->base_res);
693192762Srmacklem			printf ("cx%d: Couldn't get IRQ\n", unit);
694192762Srmacklem			return ENXIO;
695192762Srmacklem		}
696192762Srmacklem	}
697192762Srmacklem
698192762Srmacklem	bd->irq_rid = 0;
699192762Srmacklem	bd->irq_res = bus_alloc_resource (dev, SYS_RES_IRQ, &bd->irq_rid,
700192762Srmacklem		irq, irq + 1, 1, RF_ACTIVE);
701192762Srmacklem	if (! bd->irq_res) {
702192762Srmacklem		printf ("cx%d: Couldn't allocate irq\n", unit);
703192762Srmacklem		bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
704192762Srmacklem			bd->drq_res);
705192762Srmacklem		bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
706192762Srmacklem			bd->base_res);
707192762Srmacklem		return ENXIO;
708192762Srmacklem	}
709192762Srmacklem
710192762Srmacklem	b = malloc (sizeof (cx_board_t), M_DEVBUF, M_WAITOK);
711192762Srmacklem	if (!b) {
712192762Srmacklem		printf ("cx:%d: Couldn't allocate memory\n", unit);
713192762Srmacklem		return (ENXIO);
714192762Srmacklem	}
715192762Srmacklem	adapter[unit] = b;
716192762Srmacklem	bzero (b, sizeof(cx_board_t));
717192762Srmacklem
718192762Srmacklem	if (! cx_open_board (b, unit, iobase, irq, drq)) {
719192762Srmacklem		printf ("cx%d: error loading firmware\n", unit);
720192762Srmacklem		free (b, M_DEVBUF);
721192762Srmacklem		bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid,
722192762Srmacklem			bd->irq_res);
723192762Srmacklem		bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
724192762Srmacklem			bd->drq_res);
725192762Srmacklem		bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
726192762Srmacklem			bd->base_res);
727192762Srmacklem 		return ENXIO;
728192762Srmacklem	}
729192762Srmacklem
730192762Srmacklem	bd->board = b;
731192762Srmacklem
732192762Srmacklem	cx_ln[2] = '0' + unit;
733192762Srmacklem	mtx_init (&bd->cx_mtx, cx_ln, MTX_NETWORK_LOCK, MTX_DEF|MTX_RECURSE);
734192762Srmacklem	if (! probe_irq (b, irq)) {
735192762Srmacklem		printf ("cx%d: irq %ld not functional\n", unit, irq);
736192762Srmacklem		bd->board = 0;
737192762Srmacklem		adapter [unit] = 0;
738192762Srmacklem		mtx_destroy (&bd->cx_mtx);
739192762Srmacklem		free (b, M_DEVBUF);
740192762Srmacklem		bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid,
741192762Srmacklem			bd->irq_res);
742192762Srmacklem		bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
743192762Srmacklem			bd->drq_res);
744192762Srmacklem		bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
745192762Srmacklem			bd->base_res);
746192762Srmacklem 		return ENXIO;
747192762Srmacklem	}
748192762Srmacklem	b->sys = bd;
749192762Srmacklem	callout_init (&led_timo[b->num], 1);
750192762Srmacklem	s = splhigh ();
751192762Srmacklem	if (bus_setup_intr (dev, bd->irq_res,
752192762Srmacklem			   INTR_TYPE_NET|INTR_MPSAFE,
753192762Srmacklem			   NULL, cx_intr, bd, &bd->intrhand)) {
754192762Srmacklem		printf ("cx%d: Can't setup irq %ld\n", unit, irq);
755192762Srmacklem		bd->board = 0;
756192762Srmacklem		b->sys = 0;
757192762Srmacklem		adapter [unit] = 0;
758192762Srmacklem		mtx_destroy (&bd->cx_mtx);
759192762Srmacklem		free (b, M_DEVBUF);
760192762Srmacklem		bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid,
761192762Srmacklem			bd->irq_res);
762192762Srmacklem		bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
763192762Srmacklem			bd->drq_res);
764192762Srmacklem		bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
765192762Srmacklem			bd->base_res);
766192762Srmacklem		splx (s);
767192762Srmacklem 		return ENXIO;
768192762Srmacklem	}
769192762Srmacklem
770192762Srmacklem	CX_LOCK (bd);
771192762Srmacklem	cx_init (b, b->num, b->port, irq, drq);
772192762Srmacklem	cx_setup_board (b, 0, 0, 0);
773192762Srmacklem	CX_UNLOCK (bd);
774192762Srmacklem
775192762Srmacklem	printf ("cx%d: <Cronyx-Sigma-%s>\n", b->num, b->name);
776192762Srmacklem
777192762Srmacklem	for (c=b->chan; c<b->chan+NCHAN; ++c) {
778192762Srmacklem		if (c->type == T_NONE)
779192762Srmacklem			continue;
780192762Srmacklem		d = &bd->channel[c->num];
781192762Srmacklem		d->dmamem.size = sizeof(cx_buf_t);
782192762Srmacklem		if (! cx_bus_dma_mem_alloc (unit, c->num, &d->dmamem))
783192762Srmacklem			continue;
784192762Srmacklem		d->board = b;
785192762Srmacklem		d->chan = c;
786192762Srmacklem		d->open_dev = 0;
787192762Srmacklem		c->sys = d;
788192762Srmacklem		channel [b->num*NCHAN + c->num] = d;
789192762Srmacklem		sprintf (d->name, "cx%d.%d", b->num, c->num);
790192762Srmacklem
791192762Srmacklem		switch (c->type) {
792192762Srmacklem		case T_SYNC_RS232:
793192762Srmacklem		case T_SYNC_V35:
794192762Srmacklem		case T_SYNC_RS449:
795192762Srmacklem		case T_UNIV:
796192762Srmacklem		case T_UNIV_RS232:
797192762Srmacklem		case T_UNIV_RS449:
798192762Srmacklem		case T_UNIV_V35:
799192762Srmacklem		callout_init (&d->timeout_handle, 1);
800192762Srmacklem#ifdef NETGRAPH
801192762Srmacklem		if (ng_make_node_common (&typestruct, &d->node) != 0) {
802192762Srmacklem			printf ("%s: cannot make common node\n", d->name);
803192762Srmacklem			channel [b->num*NCHAN + c->num] = 0;
804192762Srmacklem			c->sys = 0;
805192762Srmacklem			cx_bus_dma_mem_free (&d->dmamem);
806192762Srmacklem			continue;
807192762Srmacklem		}
808192762Srmacklem		NG_NODE_SET_PRIVATE (d->node, d);
809192762Srmacklem		sprintf (d->nodename, "%s%d", NG_CX_NODE_TYPE,
810192762Srmacklem			 c->board->num*NCHAN + c->num);
811192762Srmacklem		if (ng_name_node (d->node, d->nodename)) {
812192762Srmacklem			printf ("%s: cannot name node\n", d->nodename);
813192762Srmacklem			NG_NODE_UNREF (d->node);
814192762Srmacklem			channel [b->num*NCHAN + c->num] = 0;
815192762Srmacklem			c->sys = 0;
816192762Srmacklem			cx_bus_dma_mem_free (&d->dmamem);
817192762Srmacklem			continue;
818192762Srmacklem		}
819192762Srmacklem		d->lo_queue.ifq_maxlen = ifqmaxlen;
820192762Srmacklem		d->hi_queue.ifq_maxlen = ifqmaxlen;
821192762Srmacklem		mtx_init (&d->lo_queue.ifq_mtx, "cx_queue_lo", NULL, MTX_DEF);
822192762Srmacklem		mtx_init (&d->hi_queue.ifq_mtx, "cx_queue_hi", NULL, MTX_DEF);
823192762Srmacklem#else /*NETGRAPH*/
824192762Srmacklem		d->ifp = if_alloc(IFT_PPP);
825192762Srmacklem		if (d->ifp == NULL) {
826192762Srmacklem			printf ("%s: cannot if_alloc() common interface\n",
827192762Srmacklem			    d->name);
828192762Srmacklem			channel [b->num*NCHAN + c->num] = 0;
829192762Srmacklem			c->sys = 0;
830192762Srmacklem			cx_bus_dma_mem_free (&d->dmamem);
831192762Srmacklem			continue;
832192762Srmacklem		}
833192762Srmacklem		d->ifp->if_softc	= d;
834192762Srmacklem		if_initname (d->ifp, "cx", b->num * NCHAN + c->num);
835192762Srmacklem		d->ifp->if_mtu		= PP_MTU;
836192762Srmacklem		d->ifp->if_flags	= IFF_POINTOPOINT | IFF_MULTICAST;
837192762Srmacklem		d->ifp->if_ioctl	= cx_sioctl;
838192762Srmacklem		d->ifp->if_start	= cx_ifstart;
839192762Srmacklem		d->ifp->if_init		= cx_initialize;
840192762Srmacklem		d->queue.ifq_maxlen	= 2;
841192762Srmacklem		mtx_init (&d->queue.ifq_mtx, "cx_queue", NULL, MTX_DEF);
842192762Srmacklem		sppp_attach (d->ifp);
843192762Srmacklem		if_attach (d->ifp);
844192762Srmacklem		IFP2SP(d->ifp)->pp_tlf	= cx_tlf;
845192762Srmacklem		IFP2SP(d->ifp)->pp_tls	= cx_tls;
846192762Srmacklem		/* If BPF is in the kernel, call the attach for it.
847192762Srmacklem		 * Size of PPP header is 4 bytes. */
848192762Srmacklem		bpfattach (d->ifp, DLT_PPP, 4);
849192762Srmacklem#endif /*NETGRAPH*/
850192762Srmacklem		}
851192762Srmacklem		d->tty = ttyalloc ();
852192762Srmacklem		d->tty->t_open	= cx_topen;
853192762Srmacklem		d->tty->t_close	= cx_tclose;
854192762Srmacklem		d->tty->t_param	= cx_param;
855192762Srmacklem		d->tty->t_stop	= cx_stop;
856192762Srmacklem		d->tty->t_modem	= cx_tmodem;
857192762Srmacklem		d->tty->t_oproc	= cx_oproc;
858192762Srmacklem		d->tty->t_sc	= d;
859192762Srmacklem		CX_LOCK (bd);
860192762Srmacklem		cx_start_chan (c, d->dmamem.virt, d->dmamem.phys);
861192762Srmacklem		cx_register_receive (c, &cx_receive);
862192762Srmacklem		cx_register_transmit (c, &cx_transmit);
863192762Srmacklem		cx_register_error (c, &cx_error);
864192762Srmacklem		cx_register_modem (c, &cx_modem);
865192762Srmacklem		CX_UNLOCK (bd);
866192762Srmacklem
867192762Srmacklem		ttycreate(d->tty, TS_CALLOUT, "x%r%r", b->num, c->num);
868192762Srmacklem		d->devt = make_dev (&cx_cdevsw, b->num*NCHAN + c->num + 64, UID_ROOT, GID_WHEEL, 0600, "cx%d", b->num*NCHAN + c->num);
869192762Srmacklem		d->devt->si_drv1 = d;
870192762Srmacklem		callout_init (&d->dcd_timeout_handle, 1);
871192762Srmacklem	}
872192762Srmacklem	splx (s);
873192762Srmacklem
874192762Srmacklem	gone_in_dev(dev, 13, "sync serial (T1/E1) ISA card drivers");
875192762Srmacklem	return 0;
876192762Srmacklem}
877192762Srmacklem
878static int cx_detach (device_t dev)
879{
880	bdrv_t *bd = device_get_softc (dev);
881	cx_board_t *b = bd->board;
882	cx_chan_t *c;
883	int s;
884
885	KASSERT (mtx_initialized (&bd->cx_mtx), ("cx mutex not initialized"));
886
887	s = splhigh ();
888	CX_LOCK (bd);
889	/* Check if the device is busy (open). */
890	for (c = b->chan; c < b->chan + NCHAN; ++c) {
891		drv_t *d = (drv_t*) c->sys;
892
893		if (!d || d->chan->type == T_NONE)
894			continue;
895		if (d->lock) {
896			CX_UNLOCK (bd);
897			splx (s);
898			return EBUSY;
899		}
900		if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN) &&
901		    (d->open_dev|0x2)) {
902			CX_UNLOCK (bd);
903			splx (s);
904			return EBUSY;
905		}
906		if (d->running) {
907			CX_UNLOCK (bd);
908			splx (s);
909			return EBUSY;
910		}
911	}
912
913	/* Deactivate the timeout routine. And soft interrupt*/
914	callout_stop (&led_timo[b->num]);
915
916	for (c = b->chan; c < b->chan + NCHAN; ++c) {
917		drv_t *d = c->sys;
918
919		if (!d || d->chan->type == T_NONE)
920			continue;
921
922		callout_stop (&d->dcd_timeout_handle);
923	}
924	CX_UNLOCK (bd);
925	bus_teardown_intr (dev, bd->irq_res, bd->intrhand);
926	bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid, bd->irq_res);
927
928	bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid, bd->drq_res);
929
930	bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid, bd->base_res);
931
932	CX_LOCK (bd);
933	cx_close_board (b);
934
935	/* Detach the interfaces, free buffer memory. */
936	for (c = b->chan; c < b->chan + NCHAN; ++c) {
937		drv_t *d = (drv_t*) c->sys;
938
939		if (!d || d->chan->type == T_NONE)
940			continue;
941
942		if (d->tty) {
943			ttyfree (d->tty);
944			d->tty = NULL;
945		}
946
947		callout_stop (&d->timeout_handle);
948#ifdef NETGRAPH
949		if (d->node) {
950			ng_rmnode_self (d->node);
951			NG_NODE_UNREF (d->node);
952			d->node = NULL;
953		}
954		mtx_destroy (&d->lo_queue.ifq_mtx);
955		mtx_destroy (&d->hi_queue.ifq_mtx);
956#else
957		/* Detach from the packet filter list of interfaces. */
958		bpfdetach (d->ifp);
959		/* Detach from the sync PPP list. */
960		sppp_detach (d->ifp);
961
962		if_detach (d->ifp);
963		if_free(d->ifp);
964		/* XXXRIK: check interconnection with irq handler */
965		IF_DRAIN (&d->queue);
966		mtx_destroy (&d->queue.ifq_mtx);
967#endif
968		destroy_dev (d->devt);
969	}
970
971	cx_led_off (b);
972	CX_UNLOCK (bd);
973	callout_drain (&led_timo[b->num]);
974	for (c = b->chan; c < b->chan + NCHAN; ++c) {
975		drv_t *d = c->sys;
976
977		if (!d || d->chan->type == T_NONE)
978			continue;
979
980		callout_drain (&d->dcd_timeout_handle);
981		callout_drain (&d->timeout_handle);
982	}
983	splx (s);
984
985	s = splhigh ();
986	for (c = b->chan; c < b->chan + NCHAN; ++c) {
987		drv_t *d = (drv_t*) c->sys;
988
989		if (!d || d->chan->type == T_NONE)
990			continue;
991
992		/* Deallocate buffers. */
993		cx_bus_dma_mem_free (&d->dmamem);
994	}
995	bd->board = NULL;
996	adapter [b->num] = NULL;
997	free (b, M_DEVBUF);
998	splx (s);
999
1000	mtx_destroy (&bd->cx_mtx);
1001
1002	return 0;
1003}
1004
1005#ifndef NETGRAPH
1006static void cx_ifstart (struct ifnet *ifp)
1007{
1008	drv_t *d = ifp->if_softc;
1009	bdrv_t *bd = d->board->sys;
1010
1011	CX_LOCK (bd);
1012	cx_start (d);
1013	CX_UNLOCK (bd);
1014}
1015
1016static void cx_tlf (struct sppp *sp)
1017{
1018	drv_t *d = SP2IFP(sp)->if_softc;
1019
1020	CX_DEBUG (d, ("cx_tlf\n"));
1021/*	cx_set_dtr (d->chan, 0);*/
1022/*	cx_set_rts (d->chan, 0);*/
1023	if (!(IFP2SP(d->ifp)->pp_flags & PP_FR) && !(d->ifp->if_flags & PP_CISCO))
1024		sp->pp_down (sp);
1025}
1026
1027static void cx_tls (struct sppp *sp)
1028{
1029	drv_t *d = SP2IFP(sp)->if_softc;
1030
1031	CX_DEBUG (d, ("cx_tls\n"));
1032	if (!(IFP2SP(d->ifp)->pp_flags & PP_FR) && !(d->ifp->if_flags & PP_CISCO))
1033		sp->pp_up (sp);
1034}
1035
1036/*
1037 * Initialization of interface.
1038 * It seems to be never called by upper level.
1039 */
1040static void cx_initialize (void *softc)
1041{
1042	drv_t *d = softc;
1043
1044	CX_DEBUG (d, ("cx_initialize\n"));
1045}
1046
1047/*
1048 * Process an ioctl request.
1049 */
1050static int cx_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data)
1051{
1052	drv_t *d = ifp->if_softc;
1053	bdrv_t *bd = d->board->sys;
1054	int error, s, was_up, should_be_up;
1055
1056	/* No socket ioctls while the channel is in async mode. */
1057	if (d->chan->type == T_NONE || d->chan->mode == M_ASYNC)
1058		return EBUSY;
1059
1060	/* Socket ioctls on slave subchannels are not allowed. */
1061	was_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
1062	error = sppp_ioctl (ifp, cmd, data);
1063	if (error)
1064		return error;
1065
1066	s = splhigh ();
1067	CX_LOCK (bd);
1068	if (! (ifp->if_flags & IFF_DEBUG))
1069		d->chan->debug = 0;
1070	else
1071		d->chan->debug = d->chan->debug_shadow;
1072	CX_UNLOCK (bd);
1073	splx (s);
1074
1075	switch (cmd) {
1076	default:	   CX_DEBUG2 (d, ("ioctl 0x%lx\n", cmd)); return 0;
1077	case SIOCADDMULTI: CX_DEBUG2 (d, ("SIOCADDMULTI\n"));     return 0;
1078	case SIOCDELMULTI: CX_DEBUG2 (d, ("SIOCDELMULTI\n"));     return 0;
1079	case SIOCSIFFLAGS: CX_DEBUG2 (d, ("SIOCSIFFLAGS\n"));     break;
1080	case SIOCSIFADDR:  CX_DEBUG2 (d, ("SIOCSIFADDR\n"));      break;
1081	}
1082
1083	/* We get here only in case of SIFFLAGS or SIFADDR. */
1084	s = splhigh ();
1085	CX_LOCK (bd);
1086	should_be_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
1087	if (!was_up && should_be_up) {
1088		/* Interface goes up -- start it. */
1089		cx_up (d);
1090		cx_start (d);
1091	} else if (was_up && !should_be_up) {
1092		/* Interface is going down -- stop it. */
1093		/* if ((IFP2SP(d->ifp)->pp_flags & PP_FR) || (ifp->if_flags & PP_CISCO))*/
1094		cx_down (d);
1095	}
1096	CX_UNLOCK (bd);
1097	splx (s);
1098	return 0;
1099}
1100#endif /*NETGRAPH*/
1101
1102/*
1103 * Stop the interface.  Called on splimp().
1104 */
1105static void cx_down (drv_t *d)
1106{
1107	int s = splhigh ();
1108	CX_DEBUG (d, ("cx_down\n"));
1109	cx_set_dtr (d->chan, 0);
1110	cx_set_rts (d->chan, 0);
1111	d->running = 0;
1112	callout_stop (&d->timeout_handle);
1113	splx (s);
1114}
1115
1116/*
1117 * Start the interface.  Called on splimp().
1118 */
1119static void cx_up (drv_t *d)
1120{
1121	int s = splhigh ();
1122	CX_DEBUG (d, ("cx_up\n"));
1123	cx_set_dtr (d->chan, 1);
1124	cx_set_rts (d->chan, 1);
1125	d->running = 1;
1126	splx (s);
1127}
1128
1129/*
1130 * Start output on the (slave) interface.  Get another datagram to send
1131 * off of the interface queue, and copy it to the interface
1132 * before starting the output.
1133 */
1134static void cx_send (drv_t *d)
1135{
1136	struct mbuf *m;
1137	u_short len;
1138
1139	CX_DEBUG2 (d, ("cx_send\n"));
1140
1141	/* No output if the interface is down. */
1142	if (! d->running)
1143		return;
1144
1145	/* No output if the modem is off. */
1146	if (! cx_get_dsr (d->chan) && ! cx_get_loop(d->chan))
1147		return;
1148
1149	if (cx_buf_free (d->chan)) {
1150		/* Get the packet to send. */
1151#ifdef NETGRAPH
1152		IF_DEQUEUE (&d->hi_queue, m);
1153		if (! m)
1154			IF_DEQUEUE (&d->lo_queue, m);
1155#else
1156		m = sppp_dequeue (d->ifp);
1157#endif
1158		if (! m)
1159			return;
1160#ifndef NETGRAPH
1161		BPF_MTAP (d->ifp, m);
1162#endif
1163		len = m_length (m, NULL);
1164		if (! m->m_next)
1165			cx_send_packet (d->chan, (u_char*)mtod (m, caddr_t),
1166				len, 0);
1167		else {
1168			u_char buf [DMABUFSZ];
1169			m_copydata (m, 0, len, buf);
1170			cx_send_packet (d->chan, buf, len, 0);
1171		}
1172		m_freem (m);
1173
1174		/* Set up transmit timeout, 10 seconds. */
1175		d->timeout = 10;
1176	}
1177#ifndef NETGRAPH
1178	d->ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1179#endif
1180}
1181
1182/*
1183 * Start output on the interface.
1184 * Always called on splimp().
1185 */
1186static void cx_start (drv_t *d)
1187{
1188	int s = splhigh ();
1189	if (d->running) {
1190		if (! d->chan->dtr)
1191			cx_set_dtr (d->chan, 1);
1192		if (! d->chan->rts)
1193			cx_set_rts (d->chan, 1);
1194		cx_send (d);
1195		callout_reset (&d->timeout_handle, hz, cx_watchdog_timer, d);
1196	}
1197	splx (s);
1198}
1199
1200/*
1201 * Handle transmit timeouts.
1202 * Recover after lost transmit interrupts.
1203 * Always called on splimp().
1204 */
1205static void cx_watchdog (drv_t *d)
1206{
1207
1208	CX_DEBUG (d, ("device timeout\n"));
1209	if (d->running) {
1210		cx_setup_chan (d->chan);
1211		cx_start_chan (d->chan, 0, 0);
1212		cx_set_dtr (d->chan, 1);
1213		cx_set_rts (d->chan, 1);
1214		cx_start (d);
1215	}
1216}
1217
1218static void cx_watchdog_timer (void *arg)
1219{
1220	drv_t *d = arg;
1221	bdrv_t *bd = d->board->sys;
1222
1223	CX_LOCK (bd);
1224	if (d->timeout == 1)
1225		cx_watchdog (d);
1226	if (d->timeout)
1227		d->timeout--;
1228	callout_reset (&d->timeout_handle, hz, cx_watchdog_timer, d);
1229	CX_UNLOCK (bd);
1230}
1231
1232/*
1233 * Transmit callback function.
1234 */
1235static void cx_transmit (cx_chan_t *c, void *attachment, int len)
1236{
1237	drv_t *d = c->sys;
1238
1239	if (!d)
1240		return;
1241
1242	if (c->mode == M_ASYNC && d->tty) {
1243		d->tty->t_state &= ~(TS_BUSY | TS_FLUSH);
1244		d->atimeout = 0;
1245		if (d->tty->t_dev) {
1246			d->intr_action |= CX_WRITE;
1247			MY_SOFT_INTR = 1;
1248			swi_sched (cx_fast_ih, 0);
1249		}
1250		return;
1251	}
1252	d->timeout = 0;
1253#ifndef NETGRAPH
1254	if_inc_counter(d->ifp, IFCOUNTER_OPACKETS, 1);
1255	d->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1256#endif
1257	cx_start (d);
1258}
1259
1260/*
1261 * Process the received packet.
1262 */
1263static void cx_receive (cx_chan_t *c, char *data, int len)
1264{
1265	drv_t *d = c->sys;
1266	struct mbuf *m;
1267	char *cc = data;
1268#ifdef NETGRAPH
1269	int error;
1270#endif
1271
1272	if (!d)
1273		return;
1274
1275	if (c->mode == M_ASYNC && d->tty) {
1276		if (d->tty->t_state & TS_ISOPEN) {
1277			async_q *q = &d->aqueue;
1278			int size = BF_SZ - 1 - AQ_GSZ (q);
1279
1280			if (len <= 0 && !size)
1281				return;
1282
1283			if (len > size) {
1284				c->ierrs++;
1285				cx_error (c, CX_OVERRUN);
1286				len = size - 1;
1287			}
1288
1289			while (len--) {
1290				AQ_PUSH (q, *(unsigned char *)cc);
1291				cc++;
1292			}
1293
1294			d->intr_action |= CX_READ;
1295			MY_SOFT_INTR = 1;
1296			swi_sched (cx_fast_ih, 0);
1297		}
1298		return;
1299	}
1300	if (! d->running)
1301		return;
1302
1303	m = makembuf (data, len);
1304	if (! m) {
1305		CX_DEBUG (d, ("no memory for packet\n"));
1306#ifndef NETGRAPH
1307		if_inc_counter(d->ifp, IFCOUNTER_IQDROPS, 1);
1308#endif
1309		return;
1310	}
1311	if (c->debug > 1)
1312		m_print (m, 0);
1313#ifdef NETGRAPH
1314	m->m_pkthdr.rcvif = 0;
1315	NG_SEND_DATA_ONLY (error, d->hook, m);
1316#else
1317	if_inc_counter(d->ifp, IFCOUNTER_IPACKETS, 1);
1318	m->m_pkthdr.rcvif = d->ifp;
1319	/* Check if there's a BPF listener on this interface.
1320	 * If so, hand off the raw packet to bpf. */
1321	BPF_MTAP(d->ifp, m);
1322	IF_ENQUEUE (&d->queue, m);
1323#endif
1324}
1325
1326#define CONDITION(t,tp) (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))\
1327	    && (!(tp->t_iflag & BRKINT) || (tp->t_iflag & IGNBRK))\
1328	    && (!(tp->t_iflag & PARMRK)\
1329		|| (tp->t_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))\
1330	    && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))\
1331	    && linesw[tp->t_line]->l_rint == ttyinput)
1332
1333/*
1334 * Error callback function.
1335 */
1336static void cx_error (cx_chan_t *c, int data)
1337{
1338	drv_t *d = c->sys;
1339	async_q *q;
1340
1341	if (!d)
1342		return;
1343
1344	q = &(d->aqueue);
1345
1346	switch (data) {
1347	case CX_FRAME:
1348		CX_DEBUG (d, ("frame error\n"));
1349		if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN)
1350			&& (AQ_GSZ (q) < BF_SZ - 1)
1351			&& (!CONDITION((&d->tty->t_termios), (d->tty))
1352			|| !(d->tty->t_iflag & (IGNPAR | PARMRK)))) {
1353			AQ_PUSH (q, TTY_FE);
1354			d->intr_action |= CX_READ;
1355			MY_SOFT_INTR = 1;
1356			swi_sched (cx_fast_ih, 0);
1357		}
1358#ifndef NETGRAPH
1359		else
1360			if_inc_counter(d->ifp, IFCOUNTER_IERRORS, 1);
1361#endif
1362		break;
1363	case CX_CRC:
1364		CX_DEBUG (d, ("crc error\n"));
1365		if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN)
1366			&& (AQ_GSZ (q) < BF_SZ - 1)
1367			&& (!CONDITION((&d->tty->t_termios), (d->tty))
1368			|| !(d->tty->t_iflag & INPCK)
1369			|| !(d->tty->t_iflag & (IGNPAR | PARMRK)))) {
1370			AQ_PUSH (q, TTY_PE);
1371			d->intr_action |= CX_READ;
1372			MY_SOFT_INTR = 1;
1373			swi_sched (cx_fast_ih, 0);
1374		}
1375#ifndef NETGRAPH
1376		else
1377			if_inc_counter(d->ifp, IFCOUNTER_IERRORS, 1);
1378#endif
1379		break;
1380	case CX_OVERRUN:
1381		CX_DEBUG (d, ("overrun error\n"));
1382#ifdef TTY_OE
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			AQ_PUSH (q, TTY_OE);
1387			d->intr_action |= CX_READ;
1388			MY_SOFT_INTR = 1;
1389			swi_sched (cx_fast_ih, 0);
1390		}
1391#endif
1392#ifndef NETGRAPH
1393		else {
1394			if_inc_counter(d->ifp, IFCOUNTER_COLLISIONS, 1);
1395			if_inc_counter(d->ifp, IFCOUNTER_IERRORS, 1);
1396		}
1397#endif
1398		break;
1399	case CX_OVERFLOW:
1400		CX_DEBUG (d, ("overflow error\n"));
1401#ifndef NETGRAPH
1402		if (c->mode != M_ASYNC)
1403			if_inc_counter(d->ifp, IFCOUNTER_IERRORS, 1);
1404#endif
1405		break;
1406	case CX_UNDERRUN:
1407		CX_DEBUG (d, ("underrun error\n"));
1408		if (c->mode != M_ASYNC) {
1409			d->timeout = 0;
1410#ifndef NETGRAPH
1411			if_inc_counter(d->ifp, IFCOUNTER_OERRORS, 1);
1412			d->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1413#endif
1414			cx_start (d);
1415		}
1416		break;
1417	case CX_BREAK:
1418		CX_DEBUG (d, ("break error\n"));
1419		if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN)
1420			&& (AQ_GSZ (q) < BF_SZ - 1)
1421			&& (!CONDITION((&d->tty->t_termios), (d->tty))
1422			|| !(d->tty->t_iflag & (IGNBRK | BRKINT | PARMRK)))) {
1423			AQ_PUSH (q, TTY_BI);
1424			d->intr_action |= CX_READ;
1425			MY_SOFT_INTR = 1;
1426			swi_sched (cx_fast_ih, 0);
1427		}
1428#ifndef NETGRAPH
1429		else
1430			if_inc_counter(d->ifp, IFCOUNTER_IERRORS, 1);
1431#endif
1432		break;
1433	default:
1434		CX_DEBUG (d, ("error #%d\n", data));
1435	}
1436}
1437
1438static int cx_topen (struct tty *tp, struct cdev *dev)
1439{
1440	bdrv_t *bd;
1441	drv_t *d;
1442
1443	d = tp->t_sc;
1444	CX_DEBUG2 (d, ("cx_open (serial)\n"));
1445
1446	bd = d->board->sys;
1447
1448	if (d->chan->mode != M_ASYNC)
1449		return (EBUSY);
1450
1451	d->open_dev |= 0x2;
1452	CX_LOCK (bd);
1453	cx_start_chan (d->chan, 0, 0);
1454	cx_set_dtr (d->chan, 1);
1455	cx_set_rts (d->chan, 1);
1456	d->cd = cx_get_cd (d->chan);
1457	CX_UNLOCK (bd);
1458
1459	CX_DEBUG2 (d, ("cx_open done\n"));
1460
1461	return 0;
1462}
1463
1464static void cx_tclose (struct tty *tp)
1465{
1466	drv_t *d;
1467	bdrv_t *bd;
1468
1469	d = tp->t_sc;
1470	CX_DEBUG2 (d, ("cx_close\n"));
1471	bd = d->board->sys;
1472	CX_LOCK (bd);
1473	/* Disable receiver.
1474	 * Transmitter continues sending the queued data. */
1475	cx_enable_receive (d->chan, 0);
1476	CX_UNLOCK (bd);
1477	d->open_dev &= ~0x2;
1478}
1479
1480static int cx_tmodem (struct tty *tp, int sigon, int sigoff)
1481{
1482	drv_t *d;
1483	bdrv_t *bd;
1484
1485	d = tp->t_sc;
1486	bd = d->board->sys;
1487
1488	CX_LOCK (bd);
1489	if (!sigon && !sigoff) {
1490		if (cx_get_dsr (d->chan)) sigon |= SER_DSR;
1491		if (cx_get_cd  (d->chan)) sigon |= SER_DCD;
1492		if (cx_get_cts (d->chan)) sigon |= SER_CTS;
1493		if (d->chan->dtr)	  sigon |= SER_DTR;
1494		if (d->chan->rts)	  sigon |= SER_RTS;
1495		CX_UNLOCK (bd);
1496		return sigon;
1497	}
1498
1499	if (sigon & SER_DTR)
1500		cx_set_dtr (d->chan, 1);
1501	if (sigoff & SER_DTR)
1502		cx_set_dtr (d->chan, 0);
1503	if (sigon & SER_RTS)
1504		cx_set_rts (d->chan, 1);
1505	if (sigoff & SER_RTS)
1506		cx_set_rts (d->chan, 0);
1507	CX_UNLOCK (bd);
1508
1509	return (0);
1510}
1511
1512static int cx_open (struct cdev *dev, int flag, int mode, struct thread *td)
1513{
1514	int unit;
1515	drv_t *d;
1516
1517	d = dev->si_drv1;
1518	unit = d->chan->num;
1519
1520	CX_DEBUG2 (d, ("cx_open unit=%d, flag=0x%x, mode=0x%x\n",
1521		    unit, flag, mode));
1522
1523	d->open_dev |= 0x1;
1524
1525	CX_DEBUG2 (d, ("cx_open done\n"));
1526
1527	return 0;
1528}
1529
1530static int cx_close (struct cdev *dev, int flag, int mode, struct thread *td)
1531{
1532	drv_t *d;
1533
1534	d = dev->si_drv1;
1535	CX_DEBUG2 (d, ("cx_close\n"));
1536	d->open_dev &= ~0x1;
1537	return 0;
1538}
1539
1540static int cx_modem_status (drv_t *d)
1541{
1542	bdrv_t *bd = d->board->sys;
1543	int status = 0, s = splhigh ();
1544	CX_LOCK (bd);
1545	/* Already opened by someone or network interface is up? */
1546	if ((d->chan->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN) &&
1547	    (d->open_dev|0x2)) || (d->chan->mode != M_ASYNC && d->running))
1548		status = TIOCM_LE;	/* always enabled while open */
1549
1550	if (cx_get_dsr (d->chan)) status |= TIOCM_DSR;
1551	if (cx_get_cd  (d->chan)) status |= TIOCM_CD;
1552	if (cx_get_cts (d->chan)) status |= TIOCM_CTS;
1553	if (d->chan->dtr)	  status |= TIOCM_DTR;
1554	if (d->chan->rts)	  status |= TIOCM_RTS;
1555	CX_UNLOCK (bd);
1556	splx (s);
1557	return status;
1558}
1559
1560static int cx_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
1561{
1562	drv_t *d;
1563	bdrv_t *bd;
1564	cx_chan_t *c;
1565	struct serial_statistics *st;
1566	int error, s;
1567	char mask[16];
1568
1569	d = dev->si_drv1;
1570	c = d->chan;
1571
1572	bd = d->board->sys;
1573
1574	switch (cmd) {
1575	case SERIAL_GETREGISTERED:
1576		CX_DEBUG2 (d, ("ioctl: getregistered\n"));
1577		bzero (mask, sizeof(mask));
1578		for (s=0; s<NCX*NCHAN; ++s)
1579			if (channel [s])
1580				mask [s/8] |= 1 << (s & 7);
1581		bcopy (mask, data, sizeof (mask));
1582		return 0;
1583
1584	case SERIAL_GETPORT:
1585		CX_DEBUG2 (d, ("ioctl: getport\n"));
1586		s = splhigh ();
1587		CX_LOCK (bd);
1588		*(int *)data = cx_get_port (c);
1589		CX_UNLOCK (bd);
1590		splx (s);
1591		if (*(int *)data<0)
1592			return (EINVAL);
1593		else
1594			return 0;
1595
1596	case SERIAL_SETPORT:
1597		CX_DEBUG2 (d, ("ioctl: setproto\n"));
1598		/* Only for superuser! */
1599		error = priv_check (td, PRIV_DRIVER);
1600		if (error)
1601			return error;
1602
1603		s = splhigh ();
1604		CX_LOCK (bd);
1605		cx_set_port (c, *(int *)data);
1606		CX_UNLOCK (bd);
1607		splx (s);
1608		return 0;
1609
1610#ifndef NETGRAPH
1611	case SERIAL_GETPROTO:
1612		CX_DEBUG2 (d, ("ioctl: getproto\n"));
1613		s = splhigh ();
1614		CX_LOCK (bd);
1615		strcpy ((char*)data, (c->mode == M_ASYNC) ? "async" :
1616			(IFP2SP(d->ifp)->pp_flags & PP_FR) ? "fr" :
1617			(d->ifp->if_flags & PP_CISCO) ? "cisco" : "ppp");
1618		CX_UNLOCK (bd);
1619		splx (s);
1620		return 0;
1621
1622	case SERIAL_SETPROTO:
1623		CX_DEBUG2 (d, ("ioctl: setproto\n"));
1624		/* Only for superuser! */
1625		error = priv_check (td, PRIV_DRIVER);
1626		if (error)
1627			return error;
1628		if (c->mode == M_ASYNC)
1629			return EBUSY;
1630		if (d->ifp->if_drv_flags & IFF_DRV_RUNNING)
1631			return EBUSY;
1632		if (! strcmp ("cisco", (char*)data)) {
1633			IFP2SP(d->ifp)->pp_flags &= ~(PP_FR);
1634			IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE;
1635			d->ifp->if_flags |= PP_CISCO;
1636		} else if (! strcmp ("fr", (char*)data)) {
1637			d->ifp->if_flags &= ~(PP_CISCO);
1638			IFP2SP(d->ifp)->pp_flags |= PP_FR | PP_KEEPALIVE;
1639		} else if (! strcmp ("ppp", (char*)data)) {
1640			IFP2SP(d->ifp)->pp_flags &= ~(PP_FR | PP_KEEPALIVE);
1641			d->ifp->if_flags &= ~(PP_CISCO);
1642		} else
1643			return EINVAL;
1644		return 0;
1645
1646	case SERIAL_GETKEEPALIVE:
1647		CX_DEBUG2 (d, ("ioctl: getkeepalive\n"));
1648		if ((IFP2SP(d->ifp)->pp_flags & PP_FR) ||
1649		    (d->ifp->if_flags & PP_CISCO) ||
1650		    (c->mode == M_ASYNC))
1651			return EINVAL;
1652		s = splhigh ();
1653		CX_LOCK (bd);
1654		*(int*)data = (IFP2SP(d->ifp)->pp_flags & PP_KEEPALIVE) ? 1 : 0;
1655		CX_UNLOCK (bd);
1656		splx (s);
1657		return 0;
1658
1659	case SERIAL_SETKEEPALIVE:
1660		CX_DEBUG2 (d, ("ioctl: setkeepalive\n"));
1661		/* Only for superuser! */
1662		error = priv_check (td, PRIV_DRIVER);
1663		if (error)
1664			return error;
1665		if ((IFP2SP(d->ifp)->pp_flags & PP_FR) ||
1666			(d->ifp->if_flags & PP_CISCO))
1667			return EINVAL;
1668		s = splhigh ();
1669		CX_LOCK (bd);
1670		if (*(int*)data)
1671			IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE;
1672		else
1673			IFP2SP(d->ifp)->pp_flags &= ~PP_KEEPALIVE;
1674		CX_UNLOCK (bd);
1675		splx (s);
1676		return 0;
1677#endif /*NETGRAPH*/
1678
1679	case SERIAL_GETMODE:
1680		CX_DEBUG2 (d, ("ioctl: getmode\n"));
1681		s = splhigh ();
1682		CX_LOCK (bd);
1683		*(int*)data = (c->mode == M_ASYNC) ?
1684			SERIAL_ASYNC : SERIAL_HDLC;
1685		CX_UNLOCK (bd);
1686		splx (s);
1687		return 0;
1688
1689	case SERIAL_SETMODE:
1690		CX_DEBUG2 (d, ("ioctl: setmode\n"));
1691		/* Only for superuser! */
1692		error = priv_check (td, PRIV_DRIVER);
1693		if (error)
1694			return error;
1695
1696		/* Somebody is waiting for carrier? */
1697		if (d->lock)
1698			return EBUSY;
1699		/* /dev/ttyXX is already opened by someone? */
1700		if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN) &&
1701		    (d->open_dev|0x2))
1702			return EBUSY;
1703		/* Network interface is up?
1704		 * Cannot change to async mode. */
1705		if (c->mode != M_ASYNC && d->running &&
1706		    (*(int*)data == SERIAL_ASYNC))
1707			return EBUSY;
1708
1709		s = splhigh ();
1710		CX_LOCK (bd);
1711		if (c->mode == M_HDLC && *(int*)data == SERIAL_ASYNC) {
1712			cx_set_mode (c, M_ASYNC);
1713			cx_enable_receive (c, 0);
1714			cx_enable_transmit (c, 0);
1715		} else if (c->mode == M_ASYNC && *(int*)data == SERIAL_HDLC) {
1716			if (d->ifp->if_flags & IFF_DEBUG)
1717				c->debug = c->debug_shadow;
1718			cx_set_mode (c, M_HDLC);
1719			cx_enable_receive (c, 1);
1720			cx_enable_transmit (c, 1);
1721		}
1722		CX_UNLOCK (bd);
1723		splx (s);
1724		return 0;
1725
1726	case SERIAL_GETSTAT:
1727		CX_DEBUG2 (d, ("ioctl: getestat\n"));
1728		st = (struct serial_statistics*) data;
1729		s = splhigh ();
1730		CX_LOCK (bd);
1731		st->rintr  = c->rintr;
1732		st->tintr  = c->tintr;
1733		st->mintr  = c->mintr;
1734		st->ibytes = c->ibytes;
1735		st->ipkts  = c->ipkts;
1736		st->ierrs  = c->ierrs;
1737		st->obytes = c->obytes;
1738		st->opkts  = c->opkts;
1739		st->oerrs  = c->oerrs;
1740		CX_UNLOCK (bd);
1741		splx (s);
1742		return 0;
1743
1744	case SERIAL_CLRSTAT:
1745		CX_DEBUG2 (d, ("ioctl: clrstat\n"));
1746		/* Only for superuser! */
1747		error = priv_check (td, PRIV_DRIVER);
1748		if (error)
1749			return error;
1750		s = splhigh ();
1751		CX_LOCK (bd);
1752		c->rintr = 0;
1753		c->tintr = 0;
1754		c->mintr = 0;
1755		c->ibytes = 0;
1756		c->ipkts = 0;
1757		c->ierrs = 0;
1758		c->obytes = 0;
1759		c->opkts = 0;
1760		c->oerrs = 0;
1761		CX_UNLOCK (bd);
1762		splx (s);
1763		return 0;
1764
1765	case SERIAL_GETBAUD:
1766		CX_DEBUG2 (d, ("ioctl: getbaud\n"));
1767		if (c->mode == M_ASYNC)
1768			return EINVAL;
1769		s = splhigh ();
1770		CX_LOCK (bd);
1771		*(long*)data = cx_get_baud(c);
1772		CX_UNLOCK (bd);
1773		splx (s);
1774		return 0;
1775
1776	case SERIAL_SETBAUD:
1777		CX_DEBUG2 (d, ("ioctl: setbaud\n"));
1778		/* Only for superuser! */
1779		error = priv_check (td, PRIV_DRIVER);
1780		if (error)
1781			return error;
1782		if (c->mode == M_ASYNC)
1783			return EINVAL;
1784		s = splhigh ();
1785		CX_LOCK (bd);
1786		cx_set_baud (c, *(long*)data);
1787		CX_UNLOCK (bd);
1788		splx (s);
1789		return 0;
1790
1791	case SERIAL_GETLOOP:
1792		CX_DEBUG2 (d, ("ioctl: getloop\n"));
1793		if (c->mode == M_ASYNC)
1794			return EINVAL;
1795		s = splhigh ();
1796		CX_LOCK (bd);
1797		*(int*)data = cx_get_loop (c);
1798		CX_UNLOCK (bd);
1799		splx (s);
1800		return 0;
1801
1802	case SERIAL_SETLOOP:
1803		CX_DEBUG2 (d, ("ioctl: setloop\n"));
1804		/* Only for superuser! */
1805		error = priv_check (td, PRIV_DRIVER);
1806		if (error)
1807			return error;
1808		if (c->mode == M_ASYNC)
1809			return EINVAL;
1810		s = splhigh ();
1811		CX_LOCK (bd);
1812		cx_set_loop (c, *(int*)data);
1813		CX_UNLOCK (bd);
1814		splx (s);
1815		return 0;
1816
1817	case SERIAL_GETDPLL:
1818		CX_DEBUG2 (d, ("ioctl: getdpll\n"));
1819		if (c->mode == M_ASYNC)
1820			return EINVAL;
1821		s = splhigh ();
1822		CX_LOCK (bd);
1823		*(int*)data = cx_get_dpll (c);
1824		CX_UNLOCK (bd);
1825		splx (s);
1826		return 0;
1827
1828	case SERIAL_SETDPLL:
1829		CX_DEBUG2 (d, ("ioctl: setdpll\n"));
1830		/* Only for superuser! */
1831		error = priv_check (td, PRIV_DRIVER);
1832		if (error)
1833			return error;
1834		if (c->mode == M_ASYNC)
1835			return EINVAL;
1836		s = splhigh ();
1837		CX_LOCK (bd);
1838		cx_set_dpll (c, *(int*)data);
1839		CX_UNLOCK (bd);
1840		splx (s);
1841		return 0;
1842
1843	case SERIAL_GETNRZI:
1844		CX_DEBUG2 (d, ("ioctl: getnrzi\n"));
1845		if (c->mode == M_ASYNC)
1846			return EINVAL;
1847		s = splhigh ();
1848		CX_LOCK (bd);
1849		*(int*)data = cx_get_nrzi (c);
1850		CX_UNLOCK (bd);
1851		splx (s);
1852		return 0;
1853
1854	case SERIAL_SETNRZI:
1855		CX_DEBUG2 (d, ("ioctl: setnrzi\n"));
1856		/* Only for superuser! */
1857		error = priv_check (td, PRIV_DRIVER);
1858		if (error)
1859			return error;
1860		if (c->mode == M_ASYNC)
1861			return EINVAL;
1862		s = splhigh ();
1863		CX_LOCK (bd);
1864		cx_set_nrzi (c, *(int*)data);
1865		CX_UNLOCK (bd);
1866		splx (s);
1867		return 0;
1868
1869	case SERIAL_GETDEBUG:
1870		CX_DEBUG2 (d, ("ioctl: getdebug\n"));
1871		s = splhigh ();
1872		CX_LOCK (bd);
1873		*(int*)data = c->debug;
1874		CX_UNLOCK (bd);
1875		splx (s);
1876		return 0;
1877
1878	case SERIAL_SETDEBUG:
1879		CX_DEBUG2 (d, ("ioctl: setdebug\n"));
1880		/* Only for superuser! */
1881		error = priv_check (td, PRIV_DRIVER);
1882		if (error)
1883			return error;
1884		s = splhigh ();
1885		CX_LOCK (bd);
1886#ifndef	NETGRAPH
1887		if (c->mode == M_ASYNC) {
1888			c->debug = *(int*)data;
1889		} else {
1890			/*
1891			 * The debug_shadow is always greater than zero for
1892			 * logic simplicity.  For switching debug off the
1893			 * IFF_DEBUG is responsible (for !M_ASYNC mode).
1894			 */
1895			c->debug_shadow = (*(int*)data) ? (*(int*)data) : 1;
1896			if (d->ifp->if_flags & IFF_DEBUG)
1897				c->debug = c->debug_shadow;
1898		}
1899#else
1900		c->debug = *(int*)data;
1901#endif
1902		CX_UNLOCK (bd);
1903		splx (s);
1904		return 0;
1905	}
1906
1907	switch (cmd) {
1908	case TIOCSDTR:	/* Set DTR */
1909		CX_DEBUG2 (d, ("ioctl: tiocsdtr\n"));
1910		s = splhigh ();
1911		CX_LOCK (bd);
1912		cx_set_dtr (c, 1);
1913		CX_UNLOCK (bd);
1914		splx (s);
1915		return 0;
1916
1917	case TIOCCDTR:	/* Clear DTR */
1918		CX_DEBUG2 (d, ("ioctl: tioccdtr\n"));
1919		s = splhigh ();
1920		CX_LOCK (bd);
1921		cx_set_dtr (c, 0);
1922		CX_UNLOCK (bd);
1923		splx (s);
1924		return 0;
1925
1926	case TIOCMSET:	/* Set DTR/RTS */
1927		CX_DEBUG2 (d, ("ioctl: tiocmset\n"));
1928		s = splhigh ();
1929		CX_LOCK (bd);
1930		cx_set_dtr (c, (*(int*)data & TIOCM_DTR) ? 1 : 0);
1931		cx_set_rts (c, (*(int*)data & TIOCM_RTS) ? 1 : 0);
1932		CX_UNLOCK (bd);
1933		splx (s);
1934		return 0;
1935
1936	case TIOCMBIS:	/* Add DTR/RTS */
1937		CX_DEBUG2 (d, ("ioctl: tiocmbis\n"));
1938		s = splhigh ();
1939		CX_LOCK (bd);
1940		if (*(int*)data & TIOCM_DTR) cx_set_dtr (c, 1);
1941		if (*(int*)data & TIOCM_RTS) cx_set_rts (c, 1);
1942		CX_UNLOCK (bd);
1943		splx (s);
1944		return 0;
1945
1946	case TIOCMBIC:	/* Clear DTR/RTS */
1947		CX_DEBUG2 (d, ("ioctl: tiocmbic\n"));
1948		s = splhigh ();
1949		CX_LOCK (bd);
1950		if (*(int*)data & TIOCM_DTR) cx_set_dtr (c, 0);
1951		if (*(int*)data & TIOCM_RTS) cx_set_rts (c, 0);
1952		CX_UNLOCK (bd);
1953		splx (s);
1954		return 0;
1955
1956	case TIOCMGET:	/* Get modem status */
1957		CX_DEBUG2 (d, ("ioctl: tiocmget\n"));
1958		*(int*)data = cx_modem_status (d);
1959		return 0;
1960
1961	}
1962
1963	CX_DEBUG2 (d, ("ioctl: 0x%lx\n", cmd));
1964	return ENOTTY;
1965}
1966
1967void cx_softintr (void *unused)
1968{
1969	drv_t *d;
1970	bdrv_t *bd;
1971	async_q *q;
1972	int i, s, ic, k;
1973	while (MY_SOFT_INTR) {
1974		MY_SOFT_INTR = 0;
1975		for (i=0; i<NCX*NCHAN; ++i) {
1976			d = channel [i];
1977			if (!d || !d->chan || d->chan->type == T_NONE
1978			    || d->chan->mode != M_ASYNC || !d->tty
1979			    || !d->tty->t_dev)
1980				continue;
1981			bd = d->board->sys;
1982			s = splhigh ();
1983			CX_LOCK (bd);
1984			if (d->intr_action & CX_READ) {
1985				q = &(d->aqueue);
1986				if (d->tty->t_state & TS_CAN_BYPASS_L_RINT) {
1987					k = AQ_GSZ(q);
1988					if (d->tty->t_rawq.c_cc + k >
1989						d->tty->t_ihiwat
1990					    && (d->tty->t_cflag & CRTS_IFLOW
1991						|| d->tty->t_iflag & IXOFF)
1992					    && !(d->tty->t_state & TS_TBLOCK))
1993						ttyblock(d->tty);
1994					d->tty->t_rawcc += k;
1995					while (k>0) {
1996						k--;
1997						AQ_POP (q, ic);
1998						CX_UNLOCK (bd);
1999						splx (s);
2000						putc (ic, &d->tty->t_rawq);
2001						s = splhigh ();
2002						CX_LOCK (bd);
2003					}
2004					ttwakeup(d->tty);
2005					if (d->tty->t_state & TS_TTSTOP
2006					    && (d->tty->t_iflag & IXANY
2007						|| d->tty->t_cc[VSTART] ==
2008						d->tty->t_cc[VSTOP])) {
2009						d->tty->t_state &= ~TS_TTSTOP;
2010						d->tty->t_lflag &= ~FLUSHO;
2011						d->intr_action |= CX_WRITE;
2012					}
2013				} else {
2014					while (q->end != q->beg) {
2015						AQ_POP (q, ic);
2016						CX_UNLOCK (bd);
2017						splx (s);
2018						ttyld_rint (d->tty, ic);
2019						s = splhigh ();
2020						CX_LOCK (bd);
2021					}
2022				}
2023				d->intr_action &= ~CX_READ;
2024			}
2025			splx (s);
2026			CX_UNLOCK (bd);
2027
2028			s = splhigh ();
2029			CX_LOCK (bd);
2030			if (d->intr_action & CX_WRITE) {
2031				if (d->tty->t_line)
2032					ttyld_start (d->tty);
2033				else
2034					cx_oproc (d->tty);
2035				d->intr_action &= ~CX_WRITE;
2036			}
2037			CX_UNLOCK (bd);
2038			splx (s);
2039
2040		}
2041	}
2042}
2043
2044/*
2045 * Fill transmitter buffer with data.
2046 */
2047static void cx_oproc (struct tty *tp)
2048{
2049	int s, k;
2050	drv_t *d;
2051	bdrv_t *bd;
2052	static u_char buf[DMABUFSZ];
2053	u_char *p;
2054	u_short len = 0, sublen = 0;
2055
2056	d = tp->t_sc;
2057	bd = d->board->sys;
2058
2059	CX_DEBUG2 (d, ("cx_oproc\n"));
2060
2061	s = splhigh ();
2062	CX_LOCK (bd);
2063
2064	if (tp->t_cflag & CRTSCTS && (tp->t_state & TS_TBLOCK) && d->chan->rts)
2065		cx_set_rts (d->chan, 0);
2066	else if (tp->t_cflag & CRTSCTS && ! (tp->t_state & TS_TBLOCK) && ! d->chan->rts)
2067		cx_set_rts (d->chan, 1);
2068
2069	if (! (tp->t_state & (TS_TIMEOUT | TS_TTSTOP))) {
2070		/* Start transmitter. */
2071		cx_enable_transmit (d->chan, 1);
2072
2073		/* Is it busy? */
2074		if (! cx_buf_free (d->chan)) {
2075			tp->t_state |= TS_BUSY;
2076			CX_UNLOCK (bd);
2077			splx (s);
2078			return;
2079		}
2080		if (tp->t_iflag & IXOFF) {
2081			p = (buf + (DMABUFSZ/2));
2082			sublen = q_to_b (&tp->t_outq, p, (DMABUFSZ/2));
2083			k = sublen;
2084			while (k--) {
2085				/* Send XON/XOFF out of band. */
2086				if (*p == tp->t_cc[VSTOP]) {
2087					cx_xflow_ctl (d->chan, 0);
2088					p++;
2089					continue;
2090				}
2091				if (*p == tp->t_cc[VSTART]) {
2092					cx_xflow_ctl (d->chan, 1);
2093					p++;
2094					continue;
2095				}
2096				buf[len] = *p;
2097				len++;
2098				p++;
2099			}
2100		} else {
2101			p = buf;
2102			len = q_to_b (&tp->t_outq, p, (DMABUFSZ/2));
2103		}
2104		if (len) {
2105			cx_send_packet (d->chan, buf, len, 0);
2106			tp->t_state |= TS_BUSY;
2107			d->atimeout = 10;
2108			CX_DEBUG2 (d, ("out %d bytes\n", len));
2109		}
2110	}
2111	ttwwakeup (tp);
2112	CX_UNLOCK (bd);
2113	splx (s);
2114}
2115
2116static int cx_param (struct tty *tp, struct termios *t)
2117{
2118	drv_t *d;
2119	bdrv_t *bd;
2120	int s, bits, parity;
2121
2122	d = tp->t_sc;
2123	bd = d->board->sys;
2124
2125	s = splhigh ();
2126	CX_LOCK (bd);
2127	if (t->c_ospeed == 0) {
2128		/* Clear DTR and RTS. */
2129		cx_set_dtr (d->chan, 0);
2130		CX_UNLOCK (bd);
2131		splx (s);
2132		CX_DEBUG2 (d, ("cx_param (hangup)\n"));
2133		return 0;
2134	}
2135	CX_DEBUG2 (d, ("cx_param\n"));
2136
2137	/* Check requested parameters. */
2138	if (t->c_ospeed < 300 || t->c_ospeed > 256*1024) {
2139		CX_UNLOCK (bd);
2140		splx (s);
2141		return EINVAL;
2142	}
2143	if (t->c_ispeed && (t->c_ispeed < 300 || t->c_ispeed > 256*1024)) {
2144		CX_UNLOCK (bd);
2145		splx (s);
2146		return EINVAL;
2147	}
2148
2149  	/* And copy them to tty and channel structures. */
2150	tp->t_ispeed = t->c_ispeed = tp->t_ospeed = t->c_ospeed;
2151	tp->t_cflag = t->c_cflag;
2152
2153	/* Set character length and parity mode. */
2154	switch (t->c_cflag & CSIZE) {
2155	default:
2156	case CS8: bits = 8; break;
2157	case CS7: bits = 7; break;
2158	case CS6: bits = 6; break;
2159	case CS5: bits = 5; break;
2160	}
2161
2162	parity = ((t->c_cflag & PARENB) ? 1 : 0) *
2163		 (1 + ((t->c_cflag & PARODD) ? 0 : 1));
2164
2165	/* Set current channel number. */
2166	if (! d->chan->dtr)
2167		cx_set_dtr (d->chan, 1);
2168
2169	ttyldoptim (tp);
2170	cx_set_async_param (d->chan, t->c_ospeed, bits, parity, (t->c_cflag & CSTOPB),
2171		!(t->c_cflag & PARENB), (t->c_cflag & CRTSCTS),
2172		(t->c_iflag & IXON), (t->c_iflag & IXANY),
2173		t->c_cc[VSTART], t->c_cc[VSTOP]);
2174	CX_UNLOCK (bd);
2175	splx (s);
2176	return 0;
2177}
2178
2179/*
2180 * Stop output on a line
2181 */
2182static void cx_stop (struct tty *tp, int flag)
2183{
2184	drv_t *d;
2185	bdrv_t *bd;
2186	int s;
2187
2188	d = tp->t_sc;
2189	bd = d->board->sys;
2190
2191	s = splhigh ();
2192	CX_LOCK (bd);
2193	if (tp->t_state & TS_BUSY) {
2194		/* Stop transmitter */
2195		CX_DEBUG2 (d, ("cx_stop\n"));
2196		cx_transmitter_ctl (d->chan, 0);
2197	}
2198	CX_UNLOCK (bd);
2199	splx (s);
2200}
2201
2202/*
2203 * Process the (delayed) carrier signal setup.
2204 */
2205static void cx_carrier (void *arg)
2206{
2207	drv_t *d = arg;
2208	bdrv_t *bd = d->board->sys;
2209	cx_chan_t *c = d->chan;
2210	int s, cd;
2211
2212	s = splhigh ();
2213	CX_LOCK (bd);
2214	cd = cx_get_cd (c);
2215	if (d->cd != cd) {
2216		if (cd) {
2217			CX_DEBUG (d, ("carrier on\n"));
2218			d->cd = 1;
2219			CX_UNLOCK (bd);
2220			splx (s);
2221			if (d->tty)
2222				ttyld_modem(d->tty, 1);
2223		} else {
2224			CX_DEBUG (d, ("carrier loss\n"));
2225			d->cd = 0;
2226			CX_UNLOCK (bd);
2227			splx (s);
2228			if (d->tty)
2229				ttyld_modem(d->tty, 0);
2230		}
2231	} else {
2232		CX_UNLOCK (bd);
2233		splx (s);
2234	}
2235}
2236
2237/*
2238 * Modem signal callback function.
2239 */
2240static void cx_modem (cx_chan_t *c)
2241{
2242	drv_t *d = c->sys;
2243
2244	if (!d || c->mode != M_ASYNC)
2245		return;
2246	/* Handle carrier detect/loss. */
2247	/* Carrier changed - delay processing DCD for a while
2248	 * to give both sides some time to initialize. */
2249	callout_reset (&d->dcd_timeout_handle, hz/2, cx_carrier, d);
2250}
2251
2252#ifdef NETGRAPH
2253static int ng_cx_constructor (node_p node)
2254{
2255	drv_t *d = NG_NODE_PRIVATE (node);
2256	CX_DEBUG (d, ("Constructor\n"));
2257	return EINVAL;
2258}
2259
2260static int ng_cx_newhook (node_p node, hook_p hook, const char *name)
2261{
2262	int s;
2263	drv_t *d = NG_NODE_PRIVATE (node);
2264	bdrv_t *bd = d->board->sys;
2265
2266	if (d->chan->mode == M_ASYNC)
2267		return EINVAL;
2268
2269	/* Attach debug hook */
2270	if (strcmp (name, NG_CX_HOOK_DEBUG) == 0) {
2271		NG_HOOK_SET_PRIVATE (hook, NULL);
2272		d->debug_hook = hook;
2273		return 0;
2274	}
2275
2276	/* Check for raw hook */
2277	if (strcmp (name, NG_CX_HOOK_RAW) != 0)
2278		return EINVAL;
2279
2280	NG_HOOK_SET_PRIVATE (hook, d);
2281	d->hook = hook;
2282	s = splhigh ();
2283	CX_LOCK (bd);
2284	cx_up (d);
2285	CX_UNLOCK (bd);
2286	splx (s);
2287	return 0;
2288}
2289
2290static int print_modems (char *s, cx_chan_t *c, int need_header)
2291{
2292	int status = cx_modem_status (c->sys);
2293	int length = 0;
2294
2295	if (need_header)
2296		length += sprintf (s + length, "  LE   DTR  DSR  RTS  CTS  CD\n");
2297	length += sprintf (s + length, "%4s %4s %4s %4s %4s %4s\n",
2298		status & TIOCM_LE  ? "On" : "-",
2299		status & TIOCM_DTR ? "On" : "-",
2300		status & TIOCM_DSR ? "On" : "-",
2301		status & TIOCM_RTS ? "On" : "-",
2302		status & TIOCM_CTS ? "On" : "-",
2303		status & TIOCM_CD  ? "On" : "-");
2304	return length;
2305}
2306
2307static int print_stats (char *s, cx_chan_t *c, int need_header)
2308{
2309	int length = 0;
2310
2311	if (need_header)
2312		length += sprintf (s + length, "  Rintr   Tintr   Mintr   Ibytes   Ipkts   Ierrs   Obytes   Opkts   Oerrs\n");
2313	length += sprintf (s + length, "%7ld %7ld %7ld %8ld %7ld %7ld %8ld %7ld %7ld\n",
2314		c->rintr, c->tintr, c->mintr, c->ibytes, c->ipkts,
2315		c->ierrs, c->obytes, c->opkts, c->oerrs);
2316	return length;
2317}
2318
2319static int print_chan (char *s, cx_chan_t *c)
2320{
2321	drv_t *d = c->sys;
2322	int length = 0;
2323
2324	length += sprintf (s + length, "cx%d", c->board->num * NCHAN + c->num);
2325	if (d->chan->debug)
2326		length += sprintf (s + length, " debug=%d", d->chan->debug);
2327
2328	if (cx_get_baud (c))
2329		length += sprintf (s + length, " %ld", cx_get_baud (c));
2330	else
2331		length += sprintf (s + length, " extclock");
2332
2333	if (c->mode == M_HDLC) {
2334		length += sprintf (s + length, " dpll=%s", cx_get_dpll (c) ? "on" : "off");
2335		length += sprintf (s + length, " nrzi=%s", cx_get_nrzi (c) ? "on" : "off");
2336	}
2337
2338	length += sprintf (s + length, " loop=%s", cx_get_loop (c) ? "on\n" : "off\n");
2339	return length;
2340}
2341
2342static int ng_cx_rcvmsg (node_p node, item_p item, hook_p lasthook)
2343{
2344	drv_t *d = NG_NODE_PRIVATE (node);
2345	struct ng_mesg *msg;
2346	struct ng_mesg *resp = NULL;
2347	int error = 0;
2348
2349	if (!d)
2350		return EINVAL;
2351
2352	CX_DEBUG (d, ("Rcvmsg\n"));
2353	NGI_GET_MSG (item, msg);
2354	switch (msg->header.typecookie) {
2355	default:
2356		error = EINVAL;
2357		break;
2358
2359	case NGM_CX_COOKIE:
2360		printf ("Don't forget to implement\n");
2361		error = EINVAL;
2362		break;
2363
2364	case NGM_GENERIC_COOKIE:
2365		switch (msg->header.cmd) {
2366		default:
2367			error = EINVAL;
2368			break;
2369
2370		case NGM_TEXT_STATUS: {
2371			char *s;
2372			int l = 0;
2373			int dl = sizeof (struct ng_mesg) + 730;
2374
2375			NG_MKRESPONSE (resp, msg, dl, M_NOWAIT);
2376			if (! resp) {
2377				error = ENOMEM;
2378				break;
2379			}
2380			bzero (resp, dl);
2381			s = (resp)->data;
2382			l += print_chan (s + l, d->chan);
2383			l += print_stats (s + l, d->chan, 1);
2384			l += print_modems (s + l, d->chan, 1);
2385			strncpy ((resp)->header.cmdstr, "status", NG_CMDSTRSIZ);
2386			}
2387			break;
2388		}
2389		break;
2390	}
2391	NG_RESPOND_MSG (error, node, item, resp);
2392	NG_FREE_MSG (msg);
2393	return error;
2394}
2395
2396static int ng_cx_rcvdata (hook_p hook, item_p item)
2397{
2398	drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE(hook));
2399	struct mbuf *m;
2400	struct ng_tag_prio *ptag;
2401	bdrv_t *bd;
2402	struct ifqueue *q;
2403	int s;
2404
2405	NGI_GET_M (item, m);
2406	NG_FREE_ITEM (item);
2407	if (! NG_HOOK_PRIVATE (hook) || ! d) {
2408		NG_FREE_M (m);
2409		return ENETDOWN;
2410	}
2411
2412	bd = d->board->sys;
2413	/* Check for high priority data */
2414	if ((ptag = (struct ng_tag_prio *)m_tag_locate(m, NGM_GENERIC_COOKIE,
2415	    NG_TAG_PRIO, NULL)) != NULL && (ptag->priority > NG_PRIO_CUTOFF) )
2416		q = &d->hi_queue;
2417	else
2418		q = &d->lo_queue;
2419
2420	s = splhigh ();
2421	CX_LOCK (bd);
2422	IF_LOCK (q);
2423	if (_IF_QFULL (q)) {
2424		IF_UNLOCK (q);
2425		CX_UNLOCK (bd);
2426		splx (s);
2427		NG_FREE_M (m);
2428		return ENOBUFS;
2429	}
2430	_IF_ENQUEUE (q, m);
2431	IF_UNLOCK (q);
2432	cx_start (d);
2433	CX_UNLOCK (bd);
2434	splx (s);
2435	return 0;
2436}
2437
2438static int ng_cx_rmnode (node_p node)
2439{
2440	drv_t *d = NG_NODE_PRIVATE (node);
2441	bdrv_t *bd;
2442
2443	CX_DEBUG (d, ("Rmnode\n"));
2444	if (d && d->running) {
2445		int s = splhigh ();
2446		bd = d->board->sys;
2447		CX_LOCK (bd);
2448		cx_down (d);
2449		CX_UNLOCK (bd);
2450		splx (s);
2451	}
2452#ifdef	KLD_MODULE
2453	if (node->nd_flags & NGF_REALLY_DIE) {
2454		NG_NODE_SET_PRIVATE (node, NULL);
2455		NG_NODE_UNREF (node);
2456	}
2457	NG_NODE_REVIVE(node);		/* Persistent node */
2458#endif
2459	return 0;
2460}
2461
2462static int ng_cx_connect (hook_p hook)
2463{
2464	drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2465
2466	callout_reset (&d->timeout_handle, hz, cx_watchdog_timer, d);
2467	return 0;
2468}
2469
2470static int ng_cx_disconnect (hook_p hook)
2471{
2472	drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2473	bdrv_t *bd = d->board->sys;
2474	int s;
2475
2476	s = splhigh ();
2477	CX_LOCK (bd);
2478	if (NG_HOOK_PRIVATE (hook))
2479		cx_down (d);
2480	CX_UNLOCK (bd);
2481	splx (s);
2482	/* If we were wait it than it reasserted now, just stop it. */
2483	if (!callout_drain (&d->timeout_handle))
2484		callout_stop (&d->timeout_handle);
2485	return 0;
2486}
2487#endif /*NETGRAPH*/
2488
2489static int cx_modevent (module_t mod, int type, void *unused)
2490{
2491	static int load_count = 0;
2492
2493	switch (type) {
2494	case MOD_LOAD:
2495#ifdef NETGRAPH
2496		if (ng_newtype (&typestruct))
2497			printf ("Failed to register ng_cx\n");
2498#endif
2499		++load_count;
2500
2501		callout_init (&timeout_handle, 1);
2502		callout_reset (&timeout_handle, hz*5, cx_timeout, 0);
2503		/* Software interrupt. */
2504		swi_add(&tty_intr_event, "cx", cx_softintr, NULL, SWI_TTY,
2505		    INTR_MPSAFE, &cx_fast_ih);
2506		break;
2507	case MOD_UNLOAD:
2508		if (load_count == 1) {
2509			printf ("Removing device entry for Sigma\n");
2510#ifdef NETGRAPH
2511			ng_rmtype (&typestruct);
2512#endif
2513		}
2514		/* If we were wait it than it reasserted now, just stop it. */
2515		if (!callout_drain (&timeout_handle))
2516			callout_stop (&timeout_handle);
2517		swi_remove (cx_fast_ih);
2518		--load_count;
2519		break;
2520	case MOD_SHUTDOWN:
2521		break;
2522	}
2523	return 0;
2524}
2525
2526#ifdef NETGRAPH
2527static struct ng_type typestruct = {
2528	.version	= NG_ABI_VERSION,
2529	.name		= NG_CX_NODE_TYPE,
2530	.constructor	= ng_cx_constructor,
2531	.rcvmsg		= ng_cx_rcvmsg,
2532	.shutdown	= ng_cx_rmnode,
2533	.newhook	= ng_cx_newhook,
2534	.connect	= ng_cx_connect,
2535	.rcvdata	= ng_cx_rcvdata,
2536	.disconnect	= ng_cx_disconnect,
2537};
2538#endif /*NETGRAPH*/
2539
2540#ifdef NETGRAPH
2541MODULE_DEPEND (ng_cx, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION);
2542#else
2543MODULE_DEPEND (isa_cx, sppp, 1, 1, 1);
2544#endif
2545DRIVER_MODULE (cx, isa, cx_isa_driver, cx_devclass, cx_modevent, NULL);
2546MODULE_VERSION (cx, 1);
2547