1139749Simp/*-
2123120Simp * Cronyx-Sigma adapter driver for FreeBSD.
3123120Simp * Supports PPP/HDLC and Cisco/HDLC protocol in synchronous mode,
4256219Smav * and asynchronous channels with full modem control.
5123120Simp * Keepalive protocol implemented in both Cisco and PPP modes.
6123120Simp *
7123120Simp * Copyright (C) 1994-2002 Cronyx Engineering.
8123120Simp * Author: Serge Vakulenko, <vak@cronyx.ru>
9123120Simp *
10125813Srik * Copyright (C) 1999-2004 Cronyx Engineering.
11123120Simp * Rewritten on DDK, ported to NETGRAPH, rewritten for FreeBSD 3.x-5.x by
12123120Simp * Kurakin Roman, <rik@cronyx.ru>
13123120Simp *
14123120Simp * This software is distributed with NO WARRANTIES, not even the implied
15123120Simp * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16123120Simp *
17123120Simp * Authors grant any other persons or organisations a permission to use,
18123120Simp * modify and redistribute this software in source and binary forms,
19123120Simp * as long as this message is kept with the software, all derivative
20123120Simp * works or modified versions.
21123120Simp *
22130985Srik * Cronyx Id: if_cx.c,v 1.1.2.34 2004/06/23 17:09:13 rik Exp $
23123120Simp */
24130985Srik
25123120Simp#include <sys/cdefs.h>
26123120Simp__FBSDID("$FreeBSD$");
27123120Simp
28123120Simp#include <sys/param.h>
29123120Simp
30123120Simp#include <sys/systm.h>
31123120Simp#include <sys/kernel.h>
32129879Sphk#include <sys/module.h>
33164033Srwatson#include <sys/priv.h>
34123120Simp#include <sys/proc.h>
35123120Simp#include <sys/mbuf.h>
36123120Simp#include <sys/sockio.h>
37123120Simp#include <sys/malloc.h>
38123120Simp#include <sys/socket.h>
39138823Srik#include <sys/sysctl.h>
40123120Simp#include <sys/conf.h>
41123120Simp#include <sys/errno.h>
42136480Sphk#include <sys/serial.h>
43123120Simp#include <sys/tty.h>
44130971Srik#include <sys/bus.h>
45130971Srik#include <machine/bus.h>
46130971Srik#include <sys/rman.h>
47130971Srik#include <isa/isavar.h>
48123120Simp#include <sys/fcntl.h>
49123120Simp#include <sys/interrupt.h>
50123120Simp#include <vm/vm.h>
51123120Simp#include <vm/pmap.h>
52123120Simp#include <net/if.h>
53123120Simp#include <machine/cpufunc.h>
54123120Simp#include <machine/cserial.h>
55130971Srik#include <machine/resource.h>
56130971Srik#include <dev/cx/machdep.h>
57130971Srik#include <dev/cx/cxddk.h>
58130971Srik#include <dev/cx/cronyxfw.h>
59123120Simp#include "opt_ng_cronyx.h"
60123120Simp#ifdef NETGRAPH_CRONYX
61123120Simp#   include "opt_netgraph.h"
62123120Simp#   include <netgraph/ng_message.h>
63123120Simp#   include <netgraph/netgraph.h>
64130971Srik#   include <dev/cx/ng_cx.h>
65123120Simp#else
66123120Simp#   include <net/if_types.h>
67123120Simp#   include <net/if_sppp.h>
68123120Simp#   define PP_CISCO IFF_LINK2
69123120Simp#   include <net/bpf.h>
70123120Simp#endif
71123120Simp
72136472Sphk#define NCX	1
73130095Sphk
74129029Srik/* If we don't have Cronyx's sppp version, we don't have fr support via sppp */
75129029Srik#ifndef PP_FR
76129029Srik#define PP_FR 0
77129029Srik#endif
78129029Srik
79123120Simp#define CX_DEBUG(d,s)	({if (d->chan->debug) {\
80123120Simp				printf ("%s: ", d->name); printf s;}})
81123120Simp#define CX_DEBUG2(d,s)	({if (d->chan->debug>1) {\
82123120Simp				printf ("%s: ", d->name); printf s;}})
83123120Simp
84138823Srik#define CX_LOCK_NAME	"cxX"
85138823Srik
86188768Srwatson#define CX_LOCK(_bd)		mtx_lock (&(_bd)->cx_mtx)
87188768Srwatson#define CX_UNLOCK(_bd)		mtx_unlock (&(_bd)->cx_mtx)
88188768Srwatson#define CX_LOCK_ASSERT(_bd)	mtx_assert (&(_bd)->cx_mtx, MA_OWNED)
89138823Srik
90123120Simptypedef struct _async_q {
91123120Simp	int beg;
92123120Simp	int end;
93123120Simp	#define BF_SZ 14400
94123120Simp	int buf[BF_SZ+1];
95123120Simp} async_q;
96123120Simp
97123120Simp#define AQ_GSZ(q)	((BF_SZ + (q)->end - (q)->beg)%BF_SZ)
98123120Simp#define AQ_PUSH(q,c)	{*((q)->buf + (q)->end) = c;\
99123120Simp			(q)->end = ((q)->end + 1)%BF_SZ;}
100123120Simp#define AQ_POP(q,c)	{c = *((q)->buf + (q)->beg);\
101123120Simp			(q)->beg = ((q)->beg + 1)%BF_SZ;}
102123120Simp
103123120Simpstatic void cx_identify		__P((driver_t *, device_t));
104123120Simpstatic int cx_probe		__P((device_t));
105123120Simpstatic int cx_attach		__P((device_t));
106123120Simpstatic int cx_detach		__P((device_t));
107136480Sphkstatic t_open_t			cx_topen;
108136480Sphkstatic t_modem_t		cx_tmodem;
109136480Sphkstatic t_close_t		cx_tclose;
110123120Simp
111123120Simpstatic device_method_t cx_isa_methods [] = {
112123120Simp	DEVMETHOD(device_identify,	cx_identify),
113123120Simp	DEVMETHOD(device_probe,		cx_probe),
114123120Simp	DEVMETHOD(device_attach,	cx_attach),
115123120Simp	DEVMETHOD(device_detach,	cx_detach),
116123120Simp	{0, 0}
117123120Simp};
118123120Simp
119130985Sriktypedef struct _cx_dma_mem_t {
120130985Srik	unsigned long	phys;
121130985Srik	void		*virt;
122130985Srik	size_t		size;
123130985Srik	bus_dma_tag_t	dmat;
124130985Srik	bus_dmamap_t	mapp;
125130985Srik} cx_dma_mem_t;
126123120Simp
127123120Simptypedef struct _drv_t {
128123120Simp	char name [8];
129123120Simp	cx_chan_t *chan;
130123120Simp	cx_board_t *board;
131130985Srik	cx_dma_mem_t dmamem;
132130240Srik	struct tty *tty;
133138823Srik	struct callout dcd_timeout_handle;
134123120Simp	unsigned callout;
135123120Simp	unsigned lock;
136123120Simp	int open_dev;
137123120Simp	int cd;
138123120Simp	int running;
139123120Simp#ifdef NETGRAPH
140193813Simp	char	nodename [NG_NODESIZ];
141123120Simp	hook_p	hook;
142123120Simp	hook_p	debug_hook;
143123120Simp	node_p	node;
144123120Simp	struct	ifqueue lo_queue;
145123120Simp	struct	ifqueue hi_queue;
146123120Simp#else
147138823Srik	struct	ifqueue queue;
148147256Sbrooks	struct	ifnet *ifp;
149123120Simp#endif
150199407Sjhb	short	timeout;
151199407Sjhb	struct	callout timeout_handle;
152138823Srik	struct	cdev *devt;
153138823Srik	async_q	aqueue;
154130971Srik#define CX_READ 1
155130971Srik#define CX_WRITE 2
156123120Simp	int intr_action;
157123120Simp	short atimeout;
158123120Simp} drv_t;
159123120Simp
160130985Sriktypedef struct _bdrv_t {
161130985Srik	cx_board_t	*board;
162130985Srik	struct resource	*base_res;
163130985Srik	struct resource	*drq_res;
164130985Srik	struct resource	*irq_res;
165130985Srik	int		base_rid;
166130985Srik	int		drq_rid;
167130985Srik	int		irq_rid;
168130985Srik	void		*intrhand;
169130985Srik	drv_t		channel [NCHAN];
170138823Srik	struct mtx	cx_mtx;
171130985Srik} bdrv_t;
172130985Srik
173130985Srikstatic driver_t cx_isa_driver = {
174130985Srik	"cx",
175130985Srik	cx_isa_methods,
176130985Srik	sizeof (bdrv_t),
177130985Srik};
178130985Srik
179130985Srikstatic devclass_t cx_devclass;
180130985Srik
181123120Simpextern long csigma_fw_len;
182123120Simpextern const char *csigma_fw_version;
183123120Simpextern const char *csigma_fw_date;
184123120Simpextern const char *csigma_fw_copyright;
185123120Simpextern const cr_dat_tst_t csigma_fw_tvec[];
186123120Simpextern const u_char csigma_fw_data[];
187123120Simpstatic void cx_oproc (struct tty *tp);
188123120Simpstatic int cx_param (struct tty *tp, struct termios *t);
189123120Simpstatic void cx_stop (struct tty *tp, int flag);
190123120Simpstatic void cx_receive (cx_chan_t *c, char *data, int len);
191123120Simpstatic void cx_transmit (cx_chan_t *c, void *attachment, int len);
192123120Simpstatic void cx_error (cx_chan_t *c, int data);
193123120Simpstatic void cx_modem (cx_chan_t *c);
194123120Simpstatic void cx_up (drv_t *d);
195123120Simpstatic void cx_start (drv_t *d);
196123120Simpstatic void cx_softintr (void *);
197123120Simpstatic void *cx_fast_ih;
198123120Simpstatic void cx_down (drv_t *d);
199123120Simpstatic void cx_watchdog (drv_t *d);
200199407Sjhbstatic void cx_watchdog_timer (void *arg);
201123120Simpstatic void cx_carrier (void *arg);
202123120Simp
203123120Simp#ifdef NETGRAPH
204123120Simpextern struct ng_type typestruct;
205123120Simp#else
206123120Simpstatic void cx_ifstart (struct ifnet *ifp);
207123120Simpstatic void cx_tlf (struct sppp *sp);
208123120Simpstatic void cx_tls (struct sppp *sp);
209123120Simpstatic int cx_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data);
210123120Simpstatic void cx_initialize (void *softc);
211123120Simp#endif
212123120Simp
213123120Simpstatic cx_board_t *adapter [NCX];
214123120Simpstatic drv_t *channel [NCX*NCHAN];
215138823Srikstatic struct callout led_timo [NCX];
216138823Srikstatic struct callout timeout_handle;
217123120Simp
218150622Srikstatic int cx_open (struct cdev *dev, int flag, int mode, struct thread *td);
219150622Srikstatic int cx_close (struct cdev *dev, int flag, int mode, struct thread *td);
220150622Srikstatic int cx_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td);
221149847Sobrienstatic struct cdevsw cx_cdevsw = {
222149847Sobrien	.d_version  = D_VERSION,
223149847Sobrien	.d_open     = cx_open,
224149847Sobrien	.d_close    = cx_close,
225149847Sobrien	.d_ioctl    = cx_ioctl,
226149847Sobrien	.d_name     = "cx",
227188768Srwatson	.d_flags    = D_TTY,
228149847Sobrien};
229149847Sobrien
230150622Srikstatic int MY_SOFT_INTR;
231150622Srik
232123120Simp/*
233123120Simp * Print the mbuf chain, for debug purposes only.
234123120Simp */
235123120Simpstatic void printmbuf (struct mbuf *m)
236123120Simp{
237123120Simp	printf ("mbuf:");
238123120Simp	for (; m; m=m->m_next) {
239123120Simp		if (m->m_flags & M_PKTHDR)
240123120Simp			printf (" HDR %d:", m->m_pkthdr.len);
241123120Simp		if (m->m_flags & M_EXT)
242123120Simp			printf (" EXT:");
243123120Simp		printf (" %d", m->m_len);
244123120Simp	}
245123120Simp	printf ("\n");
246123120Simp}
247123120Simp
248123120Simp/*
249123120Simp * Make an mbuf from data.
250123120Simp */
251123120Simpstatic struct mbuf *makembuf (void *buf, u_int len)
252123120Simp{
253123120Simp	struct mbuf *m, *o, *p;
254123120Simp
255248078Smarius	MGETHDR (m, M_NOWAIT, MT_DATA);
256123120Simp
257123120Simp	if (! m)
258123120Simp		return 0;
259123120Simp
260123120Simp	if (len >= MINCLSIZE)
261248078Smarius		MCLGET (m, M_NOWAIT);
262123120Simp
263123120Simp	m->m_pkthdr.len = len;
264123120Simp	m->m_len = 0;
265123120Simp
266123120Simp	p = m;
267123120Simp	while (len) {
268123120Simp		u_int n = M_TRAILINGSPACE (p);
269123120Simp		if (n > len)
270123120Simp			n = len;
271123120Simp		if (! n) {
272123120Simp			/* Allocate new mbuf. */
273123120Simp			o = p;
274248078Smarius			MGET (p, M_NOWAIT, MT_DATA);
275123120Simp			if (! p) {
276123120Simp				m_freem (m);
277123120Simp				return 0;
278123120Simp			}
279123120Simp			if (len >= MINCLSIZE)
280248078Smarius				MCLGET (p, M_NOWAIT);
281123120Simp			p->m_len = 0;
282123120Simp			o->m_next = p;
283123120Simp
284123120Simp			n = M_TRAILINGSPACE (p);
285123120Simp			if (n > len)
286123120Simp				n = len;
287123120Simp		}
288123120Simp		bcopy (buf, mtod (p, caddr_t) + p->m_len, n);
289123120Simp
290123120Simp		p->m_len += n;
291123120Simp		buf = n + (char*) buf;
292123120Simp		len -= n;
293123120Simp	}
294123120Simp	return m;
295123120Simp}
296123120Simp
297123120Simp/*
298123120Simp * Recover after lost transmit interrupts.
299123120Simp */
300123120Simpstatic void cx_timeout (void *arg)
301123120Simp{
302123120Simp	drv_t *d;
303138823Srik	int s, i, k;
304123120Simp
305138823Srik	for (i = 0; i < NCX; i++) {
306138823Srik		if (adapter[i] == NULL)
307123120Simp			continue;
308138823Srik		for (k = 0; k < NCHAN; ++k) {
309138823Srik			d = channel[i * NCHAN + k];
310138823Srik			if (! d)
311138823Srik				continue;
312138823Srik			s = splhigh ();
313138823Srik			CX_LOCK ((bdrv_t *)d->board->sys);
314138823Srik			if (d->atimeout == 1 && d->tty && d->tty->t_state & TS_BUSY) {
315138823Srik				d->tty->t_state &= ~TS_BUSY;
316138823Srik				if (d->tty->t_dev) {
317138823Srik					d->intr_action |= CX_WRITE;
318138823Srik					MY_SOFT_INTR = 1;
319138823Srik					swi_sched (cx_fast_ih, 0);
320138823Srik				}
321138823Srik				CX_DEBUG (d, ("cx_timeout\n"));
322123120Simp			}
323138823Srik			if (d->atimeout)
324138823Srik				d->atimeout--;
325138823Srik			CX_UNLOCK ((bdrv_t *)d->board->sys);
326138823Srik			splx (s);
327123120Simp		}
328123120Simp	}
329138823Srik	callout_reset (&timeout_handle, hz*5, cx_timeout, 0);
330123120Simp}
331123120Simp
332123120Simpstatic void cx_led_off (void *arg)
333123120Simp{
334123120Simp	cx_board_t *b = arg;
335138823Srik	bdrv_t *bd = b->sys;
336138823Srik	int s;
337123120Simp
338138823Srik	s = splhigh ();
339138823Srik	CX_LOCK (bd);
340123120Simp	cx_led (b, 0);
341138823Srik	CX_UNLOCK (bd);
342123120Simp	splx (s);
343123120Simp}
344123120Simp
345123120Simp/*
346172568Skevlo * Activate interrupt handler from DDK.
347123120Simp */
348123120Simpstatic void cx_intr (void *arg)
349123120Simp{
350123120Simp	bdrv_t *bd = arg;
351123120Simp	cx_board_t *b = bd->board;
352138823Srik#ifndef NETGRAPH
353138823Srik	int i;
354138823Srik#endif
355123120Simp	int s = splhigh ();
356123120Simp
357138823Srik	CX_LOCK (bd);
358123120Simp	/* Turn LED on. */
359123120Simp	cx_led (b, 1);
360123120Simp
361123120Simp	cx_int_handler (b);
362123120Simp
363123120Simp	/* Turn LED off 50 msec later. */
364138823Srik	callout_reset (&led_timo[b->num], hz/20, cx_led_off, b);
365138823Srik	CX_UNLOCK (bd);
366123120Simp	splx (s);
367138823Srik
368138823Srik#ifndef NETGRAPH
369138823Srik	/* Pass packets in a lock-free state */
370138823Srik	for (i = 0; i < NCHAN && b->chan[i].type; i++) {
371138823Srik		drv_t *d = b->chan[i].sys;
372138823Srik		struct mbuf *m;
373147858Srik		if (!d || !d->running)
374147858Srik			continue;
375138823Srik		while (_IF_QLEN(&d->queue)) {
376138823Srik			IF_DEQUEUE (&d->queue,m);
377138823Srik			if (!m)
378138823Srik				continue;
379147256Sbrooks			sppp_input (d->ifp, m);
380138823Srik		}
381138823Srik	}
382138823Srik#endif
383123120Simp}
384123120Simp
385123120Simpstatic int probe_irq (cx_board_t *b, int irq)
386123120Simp{
387123120Simp	int mask, busy, cnt;
388123120Simp
389123120Simp	/* Clear pending irq, if any. */
390123120Simp	cx_probe_irq (b, -irq);
391123120Simp	DELAY (100);
392123120Simp	for (cnt=0; cnt<5; ++cnt) {
393123120Simp		/* Get the mask of pending irqs, assuming they are busy.
394123120Simp		 * Activate the adapter on given irq. */
395123120Simp		busy = cx_probe_irq (b, irq);
396123120Simp		DELAY (100);
397123120Simp
398123120Simp		/* Get the mask of active irqs.
399123120Simp		 * Deactivate our irq. */
400123120Simp		mask = cx_probe_irq (b, -irq);
401123120Simp		DELAY (100);
402123120Simp		if ((mask & ~busy) == 1 << irq) {
403123120Simp			cx_probe_irq (b, 0);
404123120Simp			/* printf ("cx%d: irq %d ok, mask=0x%04x, busy=0x%04x\n",
405123120Simp				b->num, irq, mask, busy); */
406123120Simp			return 1;
407123120Simp		}
408123120Simp	}
409123120Simp	/* printf ("cx%d: irq %d not functional, mask=0x%04x, busy=0x%04x\n",
410123120Simp		b->num, irq, mask, busy); */
411123120Simp	cx_probe_irq (b, 0);
412123120Simp	return 0;
413123120Simp}
414123120Simp
415123120Simpstatic short porttab [] = {
416123120Simp	0x200, 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0,
417123120Simp	0x300, 0x320, 0x340, 0x360, 0x380, 0x3a0, 0x3c0, 0x3e0, 0
418123120Simp};
419123120Simpstatic char dmatab [] = { 7, 6, 5, 0 };
420123120Simpstatic char irqtab [] = { 5, 10, 11, 7, 3, 15, 12, 0 };
421123120Simp
422123120Simpstatic int cx_is_free_res (device_t dev, int rid, int type, u_long start,
423123120Simp	u_long end, u_long count)
424123120Simp{
425123120Simp	struct resource *res;
426123120Simp
427123120Simp	if (!(res = bus_alloc_resource (dev, type, &rid, start, end, count,
428123120Simp	    RF_ALLOCATED)))
429123120Simp		return 0;
430123120Simp
431123120Simp	bus_release_resource (dev, type, rid, res);
432123120Simp
433123120Simp	return 1;
434123120Simp}
435123120Simp
436123120Simpstatic void cx_identify (driver_t *driver, device_t dev)
437123120Simp{
438123120Simp	u_long iobase, rescount;
439123120Simp	int devcount;
440123120Simp	device_t *devices;
441123120Simp	device_t child;
442123120Simp	devclass_t my_devclass;
443123120Simp	int i, k;
444123120Simp
445123120Simp	if ((my_devclass = devclass_find ("cx")) == NULL)
446123120Simp		return;
447123120Simp
448123120Simp	devclass_get_devices (my_devclass, &devices, &devcount);
449123120Simp
450123120Simp	if (devcount == 0) {
451123120Simp		/* We should find all devices by our self. We could alter other
452123120Simp		 * devices, but we don't have a choise
453123120Simp		 */
454123120Simp		for (i = 0; (iobase = porttab [i]) != 0; i++) {
455133647Srik			if (!cx_is_free_res (dev, 0, SYS_RES_IOPORT,
456123120Simp			    iobase, iobase + NPORT, NPORT))
457123120Simp				continue;
458123120Simp			if (cx_probe_board (iobase, -1, -1) == 0)
459123120Simp				continue;
460123120Simp
461123120Simp			devcount++;
462123120Simp
463123120Simp			child = BUS_ADD_CHILD (dev, ISA_ORDER_SPECULATIVE, "cx",
464123120Simp			    -1);
465123120Simp
466123120Simp			if (child == NULL)
467123120Simp				return;
468123120Simp
469123120Simp			device_set_desc_copy (child, "Cronyx Sigma");
470123120Simp			device_set_driver (child, driver);
471123120Simp			bus_set_resource (child, SYS_RES_IOPORT, 0,
472123120Simp			    iobase, NPORT);
473123120Simp
474123120Simp			if (devcount >= NCX)
475123120Simp				break;
476123120Simp		}
477123120Simp	} else {
478123120Simp		static short porttab [] = {
479123120Simp			0x200, 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0,
480123120Simp			0x300, 0x320, 0x340, 0x360, 0x380, 0x3a0, 0x3c0, 0x3e0, 0
481123120Simp		};
482123120Simp		/* Lets check user choise.
483123120Simp		 */
484123120Simp		for (k = 0; k < devcount; k++) {
485123120Simp			if (bus_get_resource (devices[k], SYS_RES_IOPORT, 0,
486123120Simp			    &iobase, &rescount) != 0)
487123120Simp				continue;
488123120Simp
489123120Simp			for (i = 0; porttab [i] != 0; i++) {
490123120Simp				if (porttab [i] != iobase)
491123120Simp					continue;
492133647Srik				if (!cx_is_free_res (devices[k], 0, SYS_RES_IOPORT,
493123120Simp				    iobase, iobase + NPORT, NPORT))
494123120Simp					continue;
495123120Simp				if (cx_probe_board (iobase, -1, -1) == 0)
496123120Simp					continue;
497123120Simp				porttab [i] = -1;
498123120Simp				device_set_desc_copy (devices[k], "Cronyx Sigma");
499123120Simp				break;
500123120Simp			}
501123120Simp
502123120Simp			if (porttab [i] == 0) {
503123120Simp				device_delete_child (
504123120Simp				    device_get_parent (devices[k]),
505123120Simp				    devices [k]);
506123120Simp				devices[k] = 0;
507123120Simp				continue;
508123120Simp			}
509123120Simp		}
510123120Simp		for (k = 0; k < devcount; k++) {
511123120Simp			if (devices[k] == 0)
512123120Simp				continue;
513123120Simp			if (bus_get_resource (devices[k], SYS_RES_IOPORT, 0,
514123120Simp			    &iobase, &rescount) == 0)
515123120Simp				continue;
516123120Simp			for (i = 0; (iobase = porttab [i]) != 0; i++) {
517123120Simp				if (porttab [i] == -1) {
518123120Simp					continue;
519123120Simp				}
520133647Srik				if (!cx_is_free_res (devices[k], 0, SYS_RES_IOPORT,
521123120Simp				    iobase, iobase + NPORT, NPORT))
522123120Simp					continue;
523123120Simp				if (cx_probe_board (iobase, -1, -1) == 0)
524123120Simp					continue;
525123120Simp
526123120Simp				bus_set_resource (devices[k], SYS_RES_IOPORT, 0,
527123120Simp				    iobase, NPORT);
528123120Simp				porttab [i] = -1;
529123120Simp				device_set_desc_copy (devices[k], "Cronyx Sigma");
530123120Simp				break;
531123120Simp			}
532123120Simp			if (porttab [i] == 0) {
533123120Simp				device_delete_child (
534123120Simp				    device_get_parent (devices[k]),
535123120Simp				    devices [k]);
536123120Simp			}
537123120Simp		}
538123120Simp		free (devices, M_TEMP);
539123120Simp	}
540123120Simp
541123120Simp	return;
542123120Simp}
543123120Simp
544123120Simpstatic int cx_probe (device_t dev)
545123120Simp{
546123120Simp	int unit = device_get_unit (dev);
547123120Simp	int i;
548123120Simp	u_long iobase, rescount;
549123120Simp
550123120Simp	if (!device_get_desc (dev) ||
551123120Simp	    strcmp (device_get_desc (dev), "Cronyx Sigma"))
552123120Simp		return ENXIO;
553123120Simp
554123120Simp	if (bus_get_resource (dev, SYS_RES_IOPORT, 0, &iobase, &rescount) != 0) {
555123120Simp		printf ("cx%d: Couldn't get IOPORT\n", unit);
556123120Simp		return ENXIO;
557123120Simp	}
558123120Simp
559133647Srik	if (!cx_is_free_res (dev, 0, SYS_RES_IOPORT,
560123120Simp	    iobase, iobase + NPORT, NPORT)) {
561123120Simp		printf ("cx%d: Resource IOPORT isn't free %lx\n", unit, iobase);
562123120Simp		return ENXIO;
563123120Simp	}
564123120Simp
565123120Simp	for (i = 0; porttab [i] != 0; i++) {
566123120Simp		if (porttab [i] == iobase) {
567123120Simp			porttab [i] = -1;
568123120Simp			break;
569123120Simp		}
570123120Simp	}
571123120Simp
572123120Simp	if (porttab [i] == 0) {
573123120Simp		return ENXIO;
574123120Simp	}
575123120Simp
576123120Simp	if (!cx_probe_board (iobase, -1, -1)) {
577123120Simp		printf ("cx%d: probing for Sigma at %lx faild\n", unit, iobase);
578123120Simp		return ENXIO;
579123120Simp	}
580123120Simp
581123120Simp	return 0;
582123120Simp}
583123120Simp
584130985Srikstatic void
585130985Srikcx_bus_dmamap_addr (void *arg, bus_dma_segment_t *segs, int nseg, int error)
586130985Srik{
587130985Srik	unsigned long *addr;
588130985Srik
589130985Srik	if (error)
590130985Srik		return;
591130985Srik
592130985Srik	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
593130985Srik	addr = arg;
594130985Srik	*addr = segs->ds_addr;
595130985Srik}
596130985Srik
597130985Srikstatic int
598130985Srikcx_bus_dma_mem_alloc (int bnum, int cnum, cx_dma_mem_t *dmem)
599130985Srik{
600130985Srik	int error;
601130985Srik
602130985Srik	error = bus_dma_tag_create (NULL, 16, 0, BUS_SPACE_MAXADDR_24BIT,
603130985Srik		BUS_SPACE_MAXADDR, NULL, NULL, dmem->size, 1,
604130985Srik		dmem->size, 0, NULL, NULL, &dmem->dmat);
605130985Srik	if (error) {
606130985Srik		if (cnum >= 0)	printf ("cx%d-%d: ", bnum, cnum);
607130985Srik		else		printf ("cx%d: ", bnum);
608130985Srik		printf ("couldn't allocate tag for dma memory\n");
609130985Srik 		return 0;
610130985Srik	}
611130985Srik	error = bus_dmamem_alloc (dmem->dmat, (void **)&dmem->virt,
612130985Srik		BUS_DMA_NOWAIT | BUS_DMA_ZERO, &dmem->mapp);
613130985Srik	if (error) {
614130985Srik		if (cnum >= 0)	printf ("cx%d-%d: ", bnum, cnum);
615130985Srik		else		printf ("cx%d: ", bnum);
616130985Srik		printf ("couldn't allocate mem for dma memory\n");
617130985Srik		bus_dma_tag_destroy (dmem->dmat);
618130985Srik 		return 0;
619130985Srik	}
620130985Srik	error = bus_dmamap_load (dmem->dmat, dmem->mapp, dmem->virt,
621130985Srik		dmem->size, cx_bus_dmamap_addr, &dmem->phys, 0);
622130985Srik	if (error) {
623130985Srik		if (cnum >= 0)	printf ("cx%d-%d: ", bnum, cnum);
624130985Srik		else		printf ("cx%d: ", bnum);
625130985Srik		printf ("couldn't load mem map for dma memory\n");
626130985Srik		bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
627130985Srik		bus_dma_tag_destroy (dmem->dmat);
628130985Srik 		return 0;
629130985Srik	}
630130985Srik	return 1;
631130985Srik}
632130985Srik
633130985Srikstatic void
634130985Srikcx_bus_dma_mem_free (cx_dma_mem_t *dmem)
635130985Srik{
636130985Srik	bus_dmamap_unload (dmem->dmat, dmem->mapp);
637130985Srik	bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
638130985Srik	bus_dma_tag_destroy (dmem->dmat);
639130985Srik}
640130985Srik
641123120Simp/*
642123120Simp * The adapter is present, initialize the driver structures.
643123120Simp */
644123120Simpstatic int cx_attach (device_t dev)
645123120Simp{
646123120Simp	bdrv_t *bd = device_get_softc (dev);
647123120Simp	u_long iobase, drq, irq, rescount;
648123120Simp	int unit = device_get_unit (dev);
649138823Srik	char *cx_ln = CX_LOCK_NAME;
650123120Simp	cx_board_t *b;
651123120Simp	cx_chan_t *c;
652123120Simp	drv_t *d;
653130971Srik	int i;
654130971Srik	int s;
655123120Simp
656123120Simp	KASSERT ((bd != NULL), ("cx%d: NULL device softc\n", unit));
657123120Simp
658123120Simp	bus_get_resource (dev, SYS_RES_IOPORT, 0, &iobase, &rescount);
659123120Simp	bd->base_rid = 0;
660123120Simp	bd->base_res = bus_alloc_resource (dev, SYS_RES_IOPORT, &bd->base_rid,
661123120Simp		iobase, iobase + NPORT, NPORT, RF_ACTIVE);
662123120Simp	if (! bd->base_res) {
663123120Simp		printf ("cx%d: cannot allocate base address\n", unit);
664123120Simp		return ENXIO;
665123120Simp	}
666123120Simp
667123120Simp	if (bus_get_resource (dev, SYS_RES_DRQ, 0, &drq, &rescount) != 0) {
668123120Simp		for (i = 0; (drq = dmatab [i]) != 0; i++) {
669133647Srik			if (!cx_is_free_res (dev, 0, SYS_RES_DRQ,
670123120Simp			    drq, drq + 1, 1))
671123120Simp				continue;
672123120Simp			bus_set_resource (dev, SYS_RES_DRQ, 0, drq, 1);
673123120Simp			break;
674123120Simp		}
675123120Simp
676123120Simp		if (dmatab[i] == 0) {
677123120Simp			bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
678123120Simp				bd->base_res);
679123120Simp			printf ("cx%d: Couldn't get DRQ\n", unit);
680123120Simp			return ENXIO;
681123120Simp		}
682123120Simp	}
683123120Simp
684123120Simp	bd->drq_rid = 0;
685123120Simp	bd->drq_res = bus_alloc_resource (dev, SYS_RES_DRQ, &bd->drq_rid,
686123120Simp		drq, drq + 1, 1, RF_ACTIVE);
687123120Simp	if (! bd->drq_res) {
688123120Simp		printf ("cx%d: cannot allocate drq\n", unit);
689123120Simp		bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
690123120Simp			bd->base_res);
691123120Simp		return ENXIO;
692123120Simp	}
693123120Simp
694123120Simp	if (bus_get_resource (dev, SYS_RES_IRQ, 0, &irq, &rescount) != 0) {
695123120Simp		for (i = 0; (irq = irqtab [i]) != 0; i++) {
696133647Srik			if (!cx_is_free_res (dev, 0, SYS_RES_IRQ,
697123120Simp			    irq, irq + 1, 1))
698123120Simp				continue;
699123120Simp			bus_set_resource (dev, SYS_RES_IRQ, 0, irq, 1);
700123120Simp			break;
701123120Simp		}
702123120Simp
703123120Simp		if (irqtab[i] == 0) {
704123120Simp			bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
705123120Simp				bd->drq_res);
706123120Simp			bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
707123120Simp				bd->base_res);
708123120Simp			printf ("cx%d: Couldn't get IRQ\n", unit);
709123120Simp			return ENXIO;
710123120Simp		}
711123120Simp	}
712123120Simp
713123120Simp	bd->irq_rid = 0;
714123120Simp	bd->irq_res = bus_alloc_resource (dev, SYS_RES_IRQ, &bd->irq_rid,
715123120Simp		irq, irq + 1, 1, RF_ACTIVE);
716123120Simp	if (! bd->irq_res) {
717123120Simp		printf ("cx%d: Couldn't allocate irq\n", unit);
718123120Simp		bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
719123120Simp			bd->drq_res);
720123120Simp		bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
721123120Simp			bd->base_res);
722123120Simp		return ENXIO;
723123120Simp	}
724123120Simp
725123120Simp	b = malloc (sizeof (cx_board_t), M_DEVBUF, M_WAITOK);
726123120Simp	if (!b) {
727123120Simp		printf ("cx:%d: Couldn't allocate memory\n", unit);
728123120Simp		return (ENXIO);
729123120Simp	}
730123120Simp	adapter[unit] = b;
731123120Simp	bzero (b, sizeof(cx_board_t));
732123120Simp
733123120Simp	if (! cx_open_board (b, unit, iobase, irq, drq)) {
734123120Simp		printf ("cx%d: error loading firmware\n", unit);
735123120Simp		free (b, M_DEVBUF);
736123120Simp		bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid,
737123120Simp			bd->irq_res);
738123120Simp		bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
739123120Simp			bd->drq_res);
740123120Simp		bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
741123120Simp			bd->base_res);
742123120Simp 		return ENXIO;
743123120Simp	}
744123120Simp
745123120Simp	bd->board = b;
746123120Simp
747138823Srik	cx_ln[2] = '0' + unit;
748138823Srik	mtx_init (&bd->cx_mtx, cx_ln, MTX_NETWORK_LOCK, MTX_DEF|MTX_RECURSE);
749123120Simp	if (! probe_irq (b, irq)) {
750123120Simp		printf ("cx%d: irq %ld not functional\n", unit, irq);
751123120Simp		bd->board = 0;
752123120Simp		adapter [unit] = 0;
753138823Srik		mtx_destroy (&bd->cx_mtx);
754123120Simp		free (b, M_DEVBUF);
755123120Simp		bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid,
756123120Simp			bd->irq_res);
757123120Simp		bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
758123120Simp			bd->drq_res);
759123120Simp		bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
760123120Simp			bd->base_res);
761123120Simp 		return ENXIO;
762123120Simp	}
763138823Srik	b->sys = bd;
764188768Srwatson	callout_init (&led_timo[b->num], CALLOUT_MPSAFE);
765123120Simp	s = splhigh ();
766138823Srik	if (bus_setup_intr (dev, bd->irq_res,
767188768Srwatson			   INTR_TYPE_NET|INTR_MPSAFE,
768166901Spiso			   NULL, cx_intr, bd, &bd->intrhand)) {
769123120Simp		printf ("cx%d: Can't setup irq %ld\n", unit, irq);
770123120Simp		bd->board = 0;
771138823Srik		b->sys = 0;
772123120Simp		adapter [unit] = 0;
773138823Srik		mtx_destroy (&bd->cx_mtx);
774123120Simp		free (b, M_DEVBUF);
775123120Simp		bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid,
776123120Simp			bd->irq_res);
777123120Simp		bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
778123120Simp			bd->drq_res);
779123120Simp		bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
780123120Simp			bd->base_res);
781123120Simp		splx (s);
782123120Simp 		return ENXIO;
783123120Simp	}
784123120Simp
785138823Srik	CX_LOCK (bd);
786123120Simp	cx_init (b, b->num, b->port, irq, drq);
787123120Simp	cx_setup_board (b, 0, 0, 0);
788138823Srik	CX_UNLOCK (bd);
789123120Simp
790123120Simp	printf ("cx%d: <Cronyx-Sigma-%s>\n", b->num, b->name);
791123120Simp
792123120Simp	for (c=b->chan; c<b->chan+NCHAN; ++c) {
793123120Simp		if (c->type == T_NONE)
794123120Simp			continue;
795130985Srik		d = &bd->channel[c->num];
796130985Srik		d->dmamem.size = sizeof(cx_buf_t);
797130985Srik		if (! cx_bus_dma_mem_alloc (unit, c->num, &d->dmamem))
798130985Srik			continue;
799123120Simp		d->board = b;
800123120Simp		d->chan = c;
801123120Simp		d->open_dev = 0;
802123120Simp		c->sys = d;
803138823Srik		channel [b->num*NCHAN + c->num] = d;
804138823Srik		sprintf (d->name, "cx%d.%d", b->num, c->num);
805123120Simp
806123120Simp		switch (c->type) {
807123120Simp		case T_SYNC_RS232:
808123120Simp		case T_SYNC_V35:
809123120Simp		case T_SYNC_RS449:
810123120Simp		case T_UNIV:
811123120Simp		case T_UNIV_RS232:
812123120Simp		case T_UNIV_RS449:
813123120Simp		case T_UNIV_V35:
814199407Sjhb		callout_init (&d->timeout_handle, CALLOUT_MPSAFE);
815123120Simp#ifdef NETGRAPH
816123120Simp		if (ng_make_node_common (&typestruct, &d->node) != 0) {
817123120Simp			printf ("%s: cannot make common node\n", d->name);
818123120Simp			channel [b->num*NCHAN + c->num] = 0;
819123120Simp			c->sys = 0;
820130985Srik			cx_bus_dma_mem_free (&d->dmamem);
821123120Simp			continue;
822123120Simp		}
823123120Simp		NG_NODE_SET_PRIVATE (d->node, d);
824123120Simp		sprintf (d->nodename, "%s%d", NG_CX_NODE_TYPE,
825123120Simp			 c->board->num*NCHAN + c->num);
826123120Simp		if (ng_name_node (d->node, d->nodename)) {
827123120Simp			printf ("%s: cannot name node\n", d->nodename);
828123120Simp			NG_NODE_UNREF (d->node);
829123120Simp			channel [b->num*NCHAN + c->num] = 0;
830123120Simp			c->sys = 0;
831130985Srik			cx_bus_dma_mem_free (&d->dmamem);
832123120Simp			continue;
833123120Simp		}
834207554Ssobomax		d->lo_queue.ifq_maxlen = ifqmaxlen;
835207554Ssobomax		d->hi_queue.ifq_maxlen = ifqmaxlen;
836123120Simp		mtx_init (&d->lo_queue.ifq_mtx, "cx_queue_lo", NULL, MTX_DEF);
837123120Simp		mtx_init (&d->hi_queue.ifq_mtx, "cx_queue_hi", NULL, MTX_DEF);
838123120Simp#else /*NETGRAPH*/
839147256Sbrooks		d->ifp = if_alloc(IFT_PPP);
840147256Sbrooks		if (d->ifp == NULL) {
841147256Sbrooks			printf ("%s: cannot if_alloc() common interface\n",
842147256Sbrooks			    d->name);
843147256Sbrooks			channel [b->num*NCHAN + c->num] = 0;
844147256Sbrooks			c->sys = 0;
845147256Sbrooks			cx_bus_dma_mem_free (&d->dmamem);
846147256Sbrooks			continue;
847147256Sbrooks		}
848147856Srik		d->ifp->if_softc	= d;
849147256Sbrooks		if_initname (d->ifp, "cx", b->num * NCHAN + c->num);
850147856Srik		d->ifp->if_mtu		= PP_MTU;
851147861Srik		d->ifp->if_flags	= IFF_POINTOPOINT | IFF_MULTICAST;
852147256Sbrooks		d->ifp->if_ioctl	= cx_sioctl;
853147256Sbrooks		d->ifp->if_start	= cx_ifstart;
854147856Srik		d->ifp->if_init		= cx_initialize;
855138823Srik		d->queue.ifq_maxlen	= 2;
856138823Srik		mtx_init (&d->queue.ifq_mtx, "cx_queue", NULL, MTX_DEF);
857147256Sbrooks		sppp_attach (d->ifp);
858147256Sbrooks		if_attach (d->ifp);
859147856Srik		IFP2SP(d->ifp)->pp_tlf	= cx_tlf;
860147856Srik		IFP2SP(d->ifp)->pp_tls	= cx_tls;
861123120Simp		/* If BPF is in the kernel, call the attach for it.
862123120Simp		 * Size of PPP header is 4 bytes. */
863147256Sbrooks		bpfattach (d->ifp, DLT_PPP, 4);
864123120Simp#endif /*NETGRAPH*/
865123120Simp		}
866136480Sphk		d->tty = ttyalloc ();
867147856Srik		d->tty->t_open	= cx_topen;
868147856Srik		d->tty->t_close	= cx_tclose;
869147856Srik		d->tty->t_param	= cx_param;
870147856Srik		d->tty->t_stop	= cx_stop;
871147856Srik		d->tty->t_modem	= cx_tmodem;
872147860Srik		d->tty->t_oproc	= cx_oproc;
873147856Srik		d->tty->t_sc	= d;
874138823Srik		CX_LOCK (bd);
875130985Srik		cx_start_chan (c, d->dmamem.virt, d->dmamem.phys);
876123120Simp		cx_register_receive (c, &cx_receive);
877123120Simp		cx_register_transmit (c, &cx_transmit);
878123120Simp		cx_register_error (c, &cx_error);
879123120Simp		cx_register_modem (c, &cx_modem);
880138823Srik		CX_UNLOCK (bd);
881136480Sphk
882151383Sphk		ttycreate(d->tty, TS_CALLOUT, "x%r%r", b->num, c->num);
883136480Sphk		d->devt = make_dev (&cx_cdevsw, b->num*NCHAN + c->num + 64, UID_ROOT, GID_WHEEL, 0600, "cx%d", b->num*NCHAN + c->num);
884136480Sphk		d->devt->si_drv1 = d;
885188768Srwatson		callout_init (&d->dcd_timeout_handle, CALLOUT_MPSAFE);
886123120Simp	}
887123120Simp	splx (s);
888123120Simp
889123120Simp	return 0;
890123120Simp}
891123120Simp
892123120Simpstatic int cx_detach (device_t dev)
893123120Simp{
894123120Simp	bdrv_t *bd = device_get_softc (dev);
895123120Simp	cx_board_t *b = bd->board;
896123120Simp	cx_chan_t *c;
897138823Srik	int s;
898123120Simp
899138823Srik	KASSERT (mtx_initialized (&bd->cx_mtx), ("cx mutex not initialized"));
900138823Srik
901138823Srik	s = splhigh ();
902138823Srik	CX_LOCK (bd);
903123120Simp	/* Check if the device is busy (open). */
904123120Simp	for (c = b->chan; c < b->chan + NCHAN; ++c) {
905123120Simp		drv_t *d = (drv_t*) c->sys;
906123120Simp
907123120Simp		if (!d || d->chan->type == T_NONE)
908123120Simp			continue;
909123120Simp		if (d->lock) {
910138823Srik			CX_UNLOCK (bd);
911123120Simp			splx (s);
912123120Simp			return EBUSY;
913123120Simp		}
914130240Srik		if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN) &&
915123120Simp		    (d->open_dev|0x2)) {
916138823Srik			CX_UNLOCK (bd);
917123120Simp			splx (s);
918123120Simp			return EBUSY;
919123120Simp		}
920123120Simp		if (d->running) {
921138823Srik			CX_UNLOCK (bd);
922123120Simp			splx (s);
923123120Simp			return EBUSY;
924123120Simp		}
925123120Simp	}
926123120Simp
927123120Simp	/* Deactivate the timeout routine. And soft interrupt*/
928138823Srik	callout_stop (&led_timo[b->num]);
929123120Simp
930123120Simp	for (c = b->chan; c < b->chan + NCHAN; ++c) {
931123120Simp		drv_t *d = c->sys;
932123120Simp
933123120Simp		if (!d || d->chan->type == T_NONE)
934123120Simp			continue;
935123120Simp
936138823Srik		callout_stop (&d->dcd_timeout_handle);
937123120Simp	}
938138823Srik	CX_UNLOCK (bd);
939123120Simp	bus_teardown_intr (dev, bd->irq_res, bd->intrhand);
940123120Simp	bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid, bd->irq_res);
941123120Simp
942123120Simp	bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid, bd->drq_res);
943123120Simp
944123120Simp	bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid, bd->base_res);
945123120Simp
946138823Srik	CX_LOCK (bd);
947123120Simp	cx_close_board (b);
948123120Simp
949123120Simp	/* Detach the interfaces, free buffer memory. */
950123120Simp	for (c = b->chan; c < b->chan + NCHAN; ++c) {
951123120Simp		drv_t *d = (drv_t*) c->sys;
952123120Simp
953123120Simp		if (!d || d->chan->type == T_NONE)
954123120Simp			continue;
955130301Srik
956130301Srik		if (d->tty) {
957136480Sphk			ttyfree (d->tty);
958130301Srik			d->tty = NULL;
959130301Srik		}
960130301Srik
961199407Sjhb		callout_stop (&d->timeout_handle);
962123120Simp#ifdef NETGRAPH
963123120Simp		if (d->node) {
964123120Simp			ng_rmnode_self (d->node);
965123120Simp			NG_NODE_UNREF (d->node);
966123120Simp			d->node = NULL;
967123120Simp		}
968123120Simp		mtx_destroy (&d->lo_queue.ifq_mtx);
969123120Simp		mtx_destroy (&d->hi_queue.ifq_mtx);
970123120Simp#else
971123120Simp		/* Detach from the packet filter list of interfaces. */
972147256Sbrooks		bpfdetach (d->ifp);
973123120Simp		/* Detach from the sync PPP list. */
974147256Sbrooks		sppp_detach (d->ifp);
975123120Simp
976147256Sbrooks		if_detach (d->ifp);
977147256Sbrooks		if_free(d->ifp);
978138823Srik		/* XXXRIK: check interconnection with irq handler */
979138823Srik		IF_DRAIN (&d->queue);
980138823Srik		mtx_destroy (&d->queue.ifq_mtx);
981123120Simp#endif
982136480Sphk		destroy_dev (d->devt);
983123120Simp	}
984123120Simp
985123120Simp	cx_led_off (b);
986138823Srik	CX_UNLOCK (bd);
987138823Srik	callout_drain (&led_timo[b->num]);
988138823Srik	for (c = b->chan; c < b->chan + NCHAN; ++c) {
989138823Srik		drv_t *d = c->sys;
990138823Srik
991138823Srik		if (!d || d->chan->type == T_NONE)
992138823Srik			continue;
993138823Srik
994138823Srik		callout_drain (&d->dcd_timeout_handle);
995199407Sjhb		callout_drain (&d->timeout_handle);
996138823Srik	}
997123120Simp	splx (s);
998123120Simp
999123120Simp	s = splhigh ();
1000123120Simp	for (c = b->chan; c < b->chan + NCHAN; ++c) {
1001123120Simp		drv_t *d = (drv_t*) c->sys;
1002123120Simp
1003123120Simp		if (!d || d->chan->type == T_NONE)
1004123120Simp			continue;
1005123120Simp
1006123120Simp		/* Deallocate buffers. */
1007130985Srik		cx_bus_dma_mem_free (&d->dmamem);
1008123120Simp	}
1009123120Simp	bd->board = 0;
1010123120Simp	adapter [b->num] = 0;
1011123120Simp	free (b, M_DEVBUF);
1012123120Simp	splx (s);
1013138823Srik
1014138823Srik	mtx_destroy (&bd->cx_mtx);
1015123120Simp
1016123120Simp	return 0;
1017123120Simp}
1018123120Simp
1019123120Simp#ifndef NETGRAPH
1020123120Simpstatic void cx_ifstart (struct ifnet *ifp)
1021123120Simp{
1022133644Srik	drv_t *d = ifp->if_softc;
1023138823Srik	bdrv_t *bd = d->board->sys;
1024123120Simp
1025138823Srik	CX_LOCK (bd);
1026123120Simp	cx_start (d);
1027138823Srik	CX_UNLOCK (bd);
1028123120Simp}
1029123120Simp
1030123120Simpstatic void cx_tlf (struct sppp *sp)
1031123120Simp{
1032147256Sbrooks	drv_t *d = SP2IFP(sp)->if_softc;
1033123120Simp
1034123120Simp	CX_DEBUG (d, ("cx_tlf\n"));
1035123120Simp/*	cx_set_dtr (d->chan, 0);*/
1036123120Simp/*	cx_set_rts (d->chan, 0);*/
1037147256Sbrooks	if (!(IFP2SP(d->ifp)->pp_flags & PP_FR) && !(d->ifp->if_flags & PP_CISCO))
1038138352Srik		sp->pp_down (sp);
1039123120Simp}
1040123120Simp
1041123120Simpstatic void cx_tls (struct sppp *sp)
1042123120Simp{
1043147256Sbrooks	drv_t *d = SP2IFP(sp)->if_softc;
1044123120Simp
1045123120Simp	CX_DEBUG (d, ("cx_tls\n"));
1046147256Sbrooks	if (!(IFP2SP(d->ifp)->pp_flags & PP_FR) && !(d->ifp->if_flags & PP_CISCO))
1047138352Srik		sp->pp_up (sp);
1048123120Simp}
1049123120Simp
1050123120Simp/*
1051123120Simp * Initialization of interface.
1052123120Simp * It seems to be never called by upper level.
1053123120Simp */
1054123120Simpstatic void cx_initialize (void *softc)
1055123120Simp{
1056123120Simp	drv_t *d = softc;
1057123120Simp
1058123120Simp	CX_DEBUG (d, ("cx_initialize\n"));
1059123120Simp}
1060123120Simp
1061123120Simp/*
1062123120Simp * Process an ioctl request.
1063123120Simp */
1064123120Simpstatic int cx_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data)
1065123120Simp{
1066123120Simp	drv_t *d = ifp->if_softc;
1067138823Srik	bdrv_t *bd = d->board->sys;
1068123120Simp	int error, s, was_up, should_be_up;
1069123120Simp
1070123120Simp	/* No socket ioctls while the channel is in async mode. */
1071123120Simp	if (d->chan->type == T_NONE || d->chan->mode == M_ASYNC)
1072123120Simp		return EBUSY;
1073123120Simp
1074123120Simp	/* Socket ioctls on slave subchannels are not allowed. */
1075148887Srwatson	was_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
1076123120Simp	error = sppp_ioctl (ifp, cmd, data);
1077123120Simp	if (error)
1078123120Simp		return error;
1079123120Simp
1080180132Srik	s = splhigh ();
1081180132Srik	CX_LOCK (bd);
1082123120Simp	if (! (ifp->if_flags & IFF_DEBUG))
1083123120Simp		d->chan->debug = 0;
1084180132Srik	else
1085180132Srik		d->chan->debug = d->chan->debug_shadow;
1086180132Srik	CX_UNLOCK (bd);
1087180132Srik	splx (s);
1088123120Simp
1089123120Simp	switch (cmd) {
1090133644Srik	default:	   CX_DEBUG2 (d, ("ioctl 0x%lx\n", cmd)); return 0;
1091123120Simp	case SIOCADDMULTI: CX_DEBUG2 (d, ("SIOCADDMULTI\n"));     return 0;
1092123120Simp	case SIOCDELMULTI: CX_DEBUG2 (d, ("SIOCDELMULTI\n"));     return 0;
1093123120Simp	case SIOCSIFFLAGS: CX_DEBUG2 (d, ("SIOCSIFFLAGS\n"));     break;
1094123120Simp	case SIOCSIFADDR:  CX_DEBUG2 (d, ("SIOCSIFADDR\n"));      break;
1095123120Simp	}
1096123120Simp
1097123120Simp	/* We get here only in case of SIFFLAGS or SIFADDR. */
1098123120Simp	s = splhigh ();
1099138823Srik	CX_LOCK (bd);
1100148887Srwatson	should_be_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
1101123120Simp	if (!was_up && should_be_up) {
1102123120Simp		/* Interface goes up -- start it. */
1103123120Simp		cx_up (d);
1104123120Simp		cx_start (d);
1105123120Simp	} else if (was_up && !should_be_up) {
1106123120Simp		/* Interface is going down -- stop it. */
1107147256Sbrooks		/* if ((IFP2SP(d->ifp)->pp_flags & PP_FR) || (ifp->if_flags & PP_CISCO))*/
1108123120Simp		cx_down (d);
1109123120Simp	}
1110138823Srik	CX_UNLOCK (bd);
1111123120Simp	splx (s);
1112123120Simp	return 0;
1113123120Simp}
1114123120Simp#endif /*NETGRAPH*/
1115123120Simp
1116123120Simp/*
1117123120Simp * Stop the interface.  Called on splimp().
1118123120Simp */
1119123120Simpstatic void cx_down (drv_t *d)
1120123120Simp{
1121123120Simp	int s = splhigh ();
1122123120Simp	CX_DEBUG (d, ("cx_down\n"));
1123123120Simp	cx_set_dtr (d->chan, 0);
1124123120Simp	cx_set_rts (d->chan, 0);
1125123120Simp	d->running = 0;
1126199407Sjhb	callout_stop (&d->timeout_handle);
1127123120Simp	splx (s);
1128123120Simp}
1129123120Simp
1130123120Simp/*
1131123120Simp * Start the interface.  Called on splimp().
1132123120Simp */
1133123120Simpstatic void cx_up (drv_t *d)
1134123120Simp{
1135123120Simp	int s = splhigh ();
1136123120Simp	CX_DEBUG (d, ("cx_up\n"));
1137123120Simp	cx_set_dtr (d->chan, 1);
1138123120Simp	cx_set_rts (d->chan, 1);
1139123120Simp	d->running = 1;
1140123120Simp	splx (s);
1141123120Simp}
1142123120Simp
1143123120Simp/*
1144123120Simp * Start output on the (slave) interface.  Get another datagram to send
1145123120Simp * off of the interface queue, and copy it to the interface
1146123120Simp * before starting the output.
1147123120Simp */
1148123120Simpstatic void cx_send (drv_t *d)
1149123120Simp{
1150123120Simp	struct mbuf *m;
1151123120Simp	u_short len;
1152123120Simp
1153123120Simp	CX_DEBUG2 (d, ("cx_send\n"));
1154123120Simp
1155123120Simp	/* No output if the interface is down. */
1156123120Simp	if (! d->running)
1157123120Simp		return;
1158123120Simp
1159123120Simp	/* No output if the modem is off. */
1160123120Simp	if (! cx_get_dsr (d->chan) && ! cx_get_loop(d->chan))
1161123120Simp		return;
1162123120Simp
1163123120Simp	if (cx_buf_free (d->chan)) {
1164123120Simp		/* Get the packet to send. */
1165123120Simp#ifdef NETGRAPH
1166123120Simp		IF_DEQUEUE (&d->hi_queue, m);
1167123120Simp		if (! m)
1168123120Simp			IF_DEQUEUE (&d->lo_queue, m);
1169123120Simp#else
1170147256Sbrooks		m = sppp_dequeue (d->ifp);
1171123120Simp#endif
1172123120Simp		if (! m)
1173123120Simp			return;
1174130971Srik#ifndef NETGRAPH
1175165632Sjhb		BPF_MTAP (d->ifp, m);
1176123120Simp#endif
1177147862Srik		len = m_length (m, NULL);
1178123120Simp		if (! m->m_next)
1179123120Simp			cx_send_packet (d->chan, (u_char*)mtod (m, caddr_t),
1180123120Simp				len, 0);
1181123120Simp		else {
1182123120Simp			u_char buf [DMABUFSZ];
1183123120Simp			m_copydata (m, 0, len, buf);
1184123120Simp			cx_send_packet (d->chan, buf, len, 0);
1185123120Simp		}
1186123120Simp		m_freem (m);
1187123120Simp
1188123120Simp		/* Set up transmit timeout, 10 seconds. */
1189123120Simp		d->timeout = 10;
1190123120Simp	}
1191123120Simp#ifndef NETGRAPH
1192148887Srwatson	d->ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1193123120Simp#endif
1194123120Simp}
1195123120Simp
1196123120Simp/*
1197123120Simp * Start output on the interface.
1198123120Simp * Always called on splimp().
1199123120Simp */
1200123120Simpstatic void cx_start (drv_t *d)
1201123120Simp{
1202123120Simp	int s = splhigh ();
1203123120Simp	if (d->running) {
1204123120Simp		if (! d->chan->dtr)
1205123120Simp			cx_set_dtr (d->chan, 1);
1206123120Simp		if (! d->chan->rts)
1207123120Simp			cx_set_rts (d->chan, 1);
1208123120Simp		cx_send (d);
1209199407Sjhb		callout_reset (&d->timeout_handle, hz, cx_watchdog_timer, d);
1210123120Simp	}
1211123120Simp	splx (s);
1212123120Simp}
1213123120Simp
1214123120Simp/*
1215123120Simp * Handle transmit timeouts.
1216123120Simp * Recover after lost transmit interrupts.
1217123120Simp * Always called on splimp().
1218123120Simp */
1219123120Simpstatic void cx_watchdog (drv_t *d)
1220123120Simp{
1221138823Srik
1222123120Simp	CX_DEBUG (d, ("device timeout\n"));
1223123120Simp	if (d->running) {
1224123120Simp		cx_setup_chan (d->chan);
1225123120Simp		cx_start_chan (d->chan, 0, 0);
1226123120Simp		cx_set_dtr (d->chan, 1);
1227123120Simp		cx_set_rts (d->chan, 1);
1228123120Simp		cx_start (d);
1229123120Simp	}
1230199407Sjhb}
1231199407Sjhb
1232199407Sjhbstatic void cx_watchdog_timer (void *arg)
1233199407Sjhb{
1234199407Sjhb	drv_t *d = arg;
1235199407Sjhb	bdrv_t *bd = d->board->sys;
1236199407Sjhb
1237199407Sjhb	CX_LOCK (bd);
1238199407Sjhb	if (d->timeout == 1)
1239199407Sjhb		cx_watchdog (d);
1240199407Sjhb	if (d->timeout)
1241199407Sjhb		d->timeout--;
1242199407Sjhb	callout_reset (&d->timeout_handle, hz, cx_watchdog_timer, d);
1243138823Srik	CX_UNLOCK (bd);
1244123120Simp}
1245123120Simp
1246123120Simp/*
1247123120Simp * Transmit callback function.
1248123120Simp */
1249123120Simpstatic void cx_transmit (cx_chan_t *c, void *attachment, int len)
1250123120Simp{
1251123120Simp	drv_t *d = c->sys;
1252123120Simp
1253123120Simp	if (!d)
1254123120Simp		return;
1255123120Simp
1256130240Srik	if (c->mode == M_ASYNC && d->tty) {
1257130240Srik		d->tty->t_state &= ~(TS_BUSY | TS_FLUSH);
1258123120Simp		d->atimeout = 0;
1259130240Srik		if (d->tty->t_dev) {
1260123120Simp			d->intr_action |= CX_WRITE;
1261123120Simp			MY_SOFT_INTR = 1;
1262123120Simp			swi_sched (cx_fast_ih, 0);
1263123120Simp		}
1264123120Simp		return;
1265123120Simp	}
1266123120Simp	d->timeout = 0;
1267199407Sjhb#ifndef NETGRAPH
1268147256Sbrooks	++d->ifp->if_opackets;
1269148887Srwatson	d->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1270123120Simp#endif
1271123120Simp	cx_start (d);
1272123120Simp}
1273123120Simp
1274123120Simp/*
1275123120Simp * Process the received packet.
1276123120Simp */
1277123120Simpstatic void cx_receive (cx_chan_t *c, char *data, int len)
1278123120Simp{
1279123120Simp	drv_t *d = c->sys;
1280123120Simp	struct mbuf *m;
1281123120Simp	char *cc = data;
1282138823Srik#ifdef NETGRAPH
1283123120Simp	int error;
1284123120Simp#endif
1285123120Simp
1286123120Simp	if (!d)
1287123120Simp		return;
1288123120Simp
1289130240Srik	if (c->mode == M_ASYNC && d->tty) {
1290130240Srik		if (d->tty->t_state & TS_ISOPEN) {
1291123120Simp			async_q *q = &d->aqueue;
1292123120Simp			int size = BF_SZ - 1 - AQ_GSZ (q);
1293123120Simp
1294123120Simp			if (len <= 0 && !size)
1295123120Simp				return;
1296123120Simp
1297123120Simp			if (len > size) {
1298133644Srik				c->ierrs++;
1299123120Simp				cx_error (c, CX_OVERRUN);
1300123120Simp				len = size - 1;
1301123120Simp			}
1302123120Simp
1303123120Simp			while (len--) {
1304123120Simp				AQ_PUSH (q, *(unsigned char *)cc);
1305123120Simp				cc++;
1306123120Simp			}
1307123120Simp
1308123120Simp			d->intr_action |= CX_READ;
1309123120Simp			MY_SOFT_INTR = 1;
1310123120Simp			swi_sched (cx_fast_ih, 0);
1311123120Simp		}
1312123120Simp		return;
1313123120Simp	}
1314123120Simp	if (! d->running)
1315123120Simp		return;
1316123120Simp
1317123120Simp	m = makembuf (data, len);
1318123120Simp	if (! m) {
1319123120Simp		CX_DEBUG (d, ("no memory for packet\n"));
1320123120Simp#ifndef NETGRAPH
1321147256Sbrooks		++d->ifp->if_iqdrops;
1322123120Simp#endif
1323123120Simp		return;
1324123120Simp	}
1325123120Simp	if (c->debug > 1)
1326123120Simp		printmbuf (m);
1327123120Simp#ifdef NETGRAPH
1328123120Simp	m->m_pkthdr.rcvif = 0;
1329123120Simp	NG_SEND_DATA_ONLY (error, d->hook, m);
1330123120Simp#else
1331147256Sbrooks	++d->ifp->if_ipackets;
1332147256Sbrooks	m->m_pkthdr.rcvif = d->ifp;
1333123120Simp	/* Check if there's a BPF listener on this interface.
1334123120Simp	 * If so, hand off the raw packet to bpf. */
1335165632Sjhb	BPF_TAP (d->ifp, data, len);
1336138823Srik	IF_ENQUEUE (&d->queue, m);
1337123120Simp#endif
1338123120Simp}
1339123120Simp
1340123120Simp#define CONDITION(t,tp) (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))\
1341123120Simp	    && (!(tp->t_iflag & BRKINT) || (tp->t_iflag & IGNBRK))\
1342123120Simp	    && (!(tp->t_iflag & PARMRK)\
1343123120Simp		|| (tp->t_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))\
1344123120Simp	    && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))\
1345130203Sphk	    && linesw[tp->t_line]->l_rint == ttyinput)
1346123120Simp
1347123120Simp/*
1348123120Simp * Error callback function.
1349123120Simp */
1350123120Simpstatic void cx_error (cx_chan_t *c, int data)
1351123120Simp{
1352123120Simp	drv_t *d = c->sys;
1353123120Simp	async_q *q;
1354123120Simp
1355123120Simp	if (!d)
1356123120Simp		return;
1357123120Simp
1358123120Simp	q = &(d->aqueue);
1359123120Simp
1360123120Simp	switch (data) {
1361123120Simp	case CX_FRAME:
1362123120Simp		CX_DEBUG (d, ("frame error\n"));
1363130240Srik		if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN)
1364123120Simp			&& (AQ_GSZ (q) < BF_SZ - 1)
1365130240Srik			&& (!CONDITION((&d->tty->t_termios), (d->tty))
1366130240Srik			|| !(d->tty->t_iflag & (IGNPAR | PARMRK)))) {
1367123120Simp			AQ_PUSH (q, TTY_FE);
1368123120Simp			d->intr_action |= CX_READ;
1369123120Simp			MY_SOFT_INTR = 1;
1370123120Simp			swi_sched (cx_fast_ih, 0);
1371123120Simp		}
1372123120Simp#ifndef NETGRAPH
1373123120Simp		else
1374147256Sbrooks			++d->ifp->if_ierrors;
1375123120Simp#endif
1376123120Simp		break;
1377123120Simp	case CX_CRC:
1378123120Simp		CX_DEBUG (d, ("crc error\n"));
1379130240Srik		if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN)
1380123120Simp			&& (AQ_GSZ (q) < BF_SZ - 1)
1381130240Srik			&& (!CONDITION((&d->tty->t_termios), (d->tty))
1382130240Srik			|| !(d->tty->t_iflag & INPCK)
1383130240Srik			|| !(d->tty->t_iflag & (IGNPAR | PARMRK)))) {
1384123120Simp			AQ_PUSH (q, TTY_PE);
1385123120Simp			d->intr_action |= CX_READ;
1386123120Simp			MY_SOFT_INTR = 1;
1387123120Simp			swi_sched (cx_fast_ih, 0);
1388123120Simp		}
1389123120Simp#ifndef NETGRAPH
1390123120Simp		else
1391147256Sbrooks			++d->ifp->if_ierrors;
1392123120Simp#endif
1393123120Simp		break;
1394123120Simp	case CX_OVERRUN:
1395123120Simp		CX_DEBUG (d, ("overrun error\n"));
1396123120Simp#ifdef TTY_OE
1397130240Srik		if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN)
1398123120Simp			&& (AQ_GSZ (q) < BF_SZ - 1)
1399130240Srik			&& (!CONDITION((&d->tty->t_termios), (d->tty)))) {
1400123120Simp			AQ_PUSH (q, TTY_OE);
1401123120Simp			d->intr_action |= CX_READ;
1402123120Simp			MY_SOFT_INTR = 1;
1403123120Simp			swi_sched (cx_fast_ih, 0);
1404123120Simp		}
1405123120Simp#endif
1406123120Simp#ifndef NETGRAPH
1407123120Simp		else {
1408147256Sbrooks			++d->ifp->if_collisions;
1409147256Sbrooks			++d->ifp->if_ierrors;
1410123120Simp		}
1411123120Simp#endif
1412123120Simp		break;
1413123120Simp	case CX_OVERFLOW:
1414123120Simp		CX_DEBUG (d, ("overflow error\n"));
1415123120Simp#ifndef NETGRAPH
1416123120Simp		if (c->mode != M_ASYNC)
1417147256Sbrooks			++d->ifp->if_ierrors;
1418123120Simp#endif
1419123120Simp		break;
1420123120Simp	case CX_UNDERRUN:
1421123120Simp		CX_DEBUG (d, ("underrun error\n"));
1422123120Simp		if (c->mode != M_ASYNC) {
1423123120Simp			d->timeout = 0;
1424199407Sjhb#ifndef NETGRAPH
1425147256Sbrooks			++d->ifp->if_oerrors;
1426148887Srwatson			d->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1427199407Sjhb#endif
1428123120Simp			cx_start (d);
1429123120Simp		}
1430123120Simp		break;
1431123120Simp	case CX_BREAK:
1432123120Simp		CX_DEBUG (d, ("break error\n"));
1433130240Srik		if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN)
1434123120Simp			&& (AQ_GSZ (q) < BF_SZ - 1)
1435130240Srik			&& (!CONDITION((&d->tty->t_termios), (d->tty))
1436130240Srik			|| !(d->tty->t_iflag & (IGNBRK | BRKINT | PARMRK)))) {
1437123120Simp			AQ_PUSH (q, TTY_BI);
1438123120Simp			d->intr_action |= CX_READ;
1439123120Simp			MY_SOFT_INTR = 1;
1440123120Simp			swi_sched (cx_fast_ih, 0);
1441123120Simp		}
1442123120Simp#ifndef NETGRAPH
1443123120Simp		else
1444147256Sbrooks			++d->ifp->if_ierrors;
1445123120Simp#endif
1446123120Simp		break;
1447123120Simp	default:
1448123120Simp		CX_DEBUG (d, ("error #%d\n", data));
1449123120Simp	}
1450123120Simp}
1451123120Simp
1452136480Sphkstatic int cx_topen (struct tty *tp, struct cdev *dev)
1453123120Simp{
1454138823Srik	bdrv_t *bd;
1455123120Simp	drv_t *d;
1456123120Simp
1457136480Sphk	d = tp->t_sc;
1458138823Srik	CX_DEBUG2 (d, ("cx_open (serial)\n"));
1459138823Srik
1460138823Srik	bd = d->board->sys;
1461138823Srik
1462136480Sphk	if (d->chan->mode != M_ASYNC)
1463136480Sphk		return (EBUSY);
1464138823Srik
1465123120Simp	d->open_dev |= 0x2;
1466138823Srik	CX_LOCK (bd);
1467136480Sphk	cx_start_chan (d->chan, 0, 0);
1468136480Sphk	cx_set_dtr (d->chan, 1);
1469136480Sphk	cx_set_rts (d->chan, 1);
1470136480Sphk	d->cd = cx_get_cd (d->chan);
1471138823Srik	CX_UNLOCK (bd);
1472138823Srik
1473138823Srik	CX_DEBUG2 (d, ("cx_open done\n"));
1474138823Srik
1475138823Srik	return 0;
1476123120Simp}
1477123120Simp
1478136480Sphkstatic void cx_tclose (struct tty *tp)
1479123120Simp{
1480136480Sphk	drv_t *d;
1481138823Srik	bdrv_t *bd;
1482123120Simp
1483136480Sphk	d = tp->t_sc;
1484138823Srik	CX_DEBUG2 (d, ("cx_close\n"));
1485138823Srik	bd = d->board->sys;
1486138823Srik	CX_LOCK (bd);
1487123120Simp	/* Disable receiver.
1488123120Simp	 * Transmitter continues sending the queued data. */
1489123120Simp	cx_enable_receive (d->chan, 0);
1490138823Srik	CX_UNLOCK (bd);
1491123120Simp	d->open_dev &= ~0x2;
1492123120Simp}
1493123120Simp
1494136480Sphkstatic int cx_tmodem (struct tty *tp, int sigon, int sigoff)
1495123120Simp{
1496136480Sphk	drv_t *d;
1497138823Srik	bdrv_t *bd;
1498123120Simp
1499136480Sphk	d = tp->t_sc;
1500138823Srik	bd = d->board->sys;
1501123120Simp
1502138823Srik	CX_LOCK (bd);
1503136480Sphk	if (!sigon && !sigoff) {
1504136480Sphk		if (cx_get_dsr (d->chan)) sigon |= SER_DSR;
1505136480Sphk		if (cx_get_cd  (d->chan)) sigon |= SER_DCD;
1506136480Sphk		if (cx_get_cts (d->chan)) sigon |= SER_CTS;
1507136480Sphk		if (d->chan->dtr)	  sigon |= SER_DTR;
1508136480Sphk		if (d->chan->rts)	  sigon |= SER_RTS;
1509147859Srik		CX_UNLOCK (bd);
1510136480Sphk		return sigon;
1511136480Sphk	}
1512136480Sphk
1513136480Sphk	if (sigon & SER_DTR)
1514136480Sphk		cx_set_dtr (d->chan, 1);
1515136480Sphk	if (sigoff & SER_DTR)
1516136480Sphk		cx_set_dtr (d->chan, 0);
1517136480Sphk	if (sigon & SER_RTS)
1518136480Sphk		cx_set_rts (d->chan, 1);
1519136480Sphk	if (sigoff & SER_RTS)
1520136480Sphk		cx_set_rts (d->chan, 0);
1521138823Srik	CX_UNLOCK (bd);
1522138823Srik
1523136480Sphk	return (0);
1524123120Simp}
1525123120Simp
1526150622Srikstatic int cx_open (struct cdev *dev, int flag, int mode, struct thread *td)
1527150622Srik{
1528150622Srik	int unit;
1529150622Srik	drv_t *d;
1530150622Srik
1531150622Srik	d = dev->si_drv1;
1532150622Srik	unit = d->chan->num;
1533150622Srik
1534150622Srik	CX_DEBUG2 (d, ("cx_open unit=%d, flag=0x%x, mode=0x%x\n",
1535150622Srik		    unit, flag, mode));
1536150622Srik
1537150622Srik	d->open_dev |= 0x1;
1538150622Srik
1539150622Srik	CX_DEBUG2 (d, ("cx_open done\n"));
1540150622Srik
1541150622Srik	return 0;
1542150622Srik}
1543150622Srik
1544150622Srikstatic int cx_close (struct cdev *dev, int flag, int mode, struct thread *td)
1545150622Srik{
1546150622Srik	drv_t *d;
1547150622Srik
1548150622Srik	d = dev->si_drv1;
1549150622Srik	CX_DEBUG2 (d, ("cx_close\n"));
1550150622Srik	d->open_dev &= ~0x1;
1551150622Srik	return 0;
1552150622Srik}
1553150622Srik
1554150622Srikstatic int cx_modem_status (drv_t *d)
1555150622Srik{
1556150622Srik	bdrv_t *bd = d->board->sys;
1557150622Srik	int status = 0, s = splhigh ();
1558150622Srik	CX_LOCK (bd);
1559150622Srik	/* Already opened by someone or network interface is up? */
1560150622Srik	if ((d->chan->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN) &&
1561150622Srik	    (d->open_dev|0x2)) || (d->chan->mode != M_ASYNC && d->running))
1562150622Srik		status = TIOCM_LE;	/* always enabled while open */
1563150622Srik
1564150622Srik	if (cx_get_dsr (d->chan)) status |= TIOCM_DSR;
1565150622Srik	if (cx_get_cd  (d->chan)) status |= TIOCM_CD;
1566150622Srik	if (cx_get_cts (d->chan)) status |= TIOCM_CTS;
1567150622Srik	if (d->chan->dtr)	  status |= TIOCM_DTR;
1568150622Srik	if (d->chan->rts)	  status |= TIOCM_RTS;
1569150622Srik	CX_UNLOCK (bd);
1570150622Srik	splx (s);
1571150622Srik	return status;
1572150622Srik}
1573150622Srik
1574150622Srikstatic int cx_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
1575150622Srik{
1576150622Srik	drv_t *d;
1577150622Srik	bdrv_t *bd;
1578150622Srik	cx_chan_t *c;
1579150622Srik	struct serial_statistics *st;
1580150622Srik	int error, s;
1581150622Srik	char mask[16];
1582150622Srik
1583150622Srik	d = dev->si_drv1;
1584150622Srik	c = d->chan;
1585150622Srik
1586150622Srik	bd = d->board->sys;
1587150622Srik
1588150622Srik	switch (cmd) {
1589150622Srik	case SERIAL_GETREGISTERED:
1590150622Srik		CX_DEBUG2 (d, ("ioctl: getregistered\n"));
1591150622Srik		bzero (mask, sizeof(mask));
1592150622Srik		for (s=0; s<NCX*NCHAN; ++s)
1593150622Srik			if (channel [s])
1594150622Srik				mask [s/8] |= 1 << (s & 7);
1595150622Srik		bcopy (mask, data, sizeof (mask));
1596150622Srik		return 0;
1597150622Srik
1598150622Srik	case SERIAL_GETPORT:
1599150622Srik		CX_DEBUG2 (d, ("ioctl: getport\n"));
1600150622Srik		s = splhigh ();
1601150622Srik		CX_LOCK (bd);
1602150622Srik		*(int *)data = cx_get_port (c);
1603150622Srik		CX_UNLOCK (bd);
1604150622Srik		splx (s);
1605150622Srik		if (*(int *)data<0)
1606150622Srik			return (EINVAL);
1607150622Srik		else
1608150622Srik			return 0;
1609150622Srik
1610150622Srik	case SERIAL_SETPORT:
1611150622Srik		CX_DEBUG2 (d, ("ioctl: setproto\n"));
1612150622Srik		/* Only for superuser! */
1613164033Srwatson		error = priv_check (td, PRIV_DRIVER);
1614150622Srik		if (error)
1615150622Srik			return error;
1616150622Srik
1617150622Srik		s = splhigh ();
1618150622Srik		CX_LOCK (bd);
1619150622Srik		cx_set_port (c, *(int *)data);
1620150622Srik		CX_UNLOCK (bd);
1621150622Srik		splx (s);
1622150622Srik		return 0;
1623150622Srik
1624150622Srik#ifndef NETGRAPH
1625150622Srik	case SERIAL_GETPROTO:
1626150622Srik		CX_DEBUG2 (d, ("ioctl: getproto\n"));
1627150622Srik		s = splhigh ();
1628150622Srik		CX_LOCK (bd);
1629150622Srik		strcpy ((char*)data, (c->mode == M_ASYNC) ? "async" :
1630150622Srik			(IFP2SP(d->ifp)->pp_flags & PP_FR) ? "fr" :
1631150622Srik			(d->ifp->if_flags & PP_CISCO) ? "cisco" : "ppp");
1632150622Srik		CX_UNLOCK (bd);
1633150622Srik		splx (s);
1634150622Srik		return 0;
1635150622Srik
1636150622Srik	case SERIAL_SETPROTO:
1637150622Srik		CX_DEBUG2 (d, ("ioctl: setproto\n"));
1638150622Srik		/* Only for superuser! */
1639164033Srwatson		error = priv_check (td, PRIV_DRIVER);
1640150622Srik		if (error)
1641150622Srik			return error;
1642150622Srik		if (c->mode == M_ASYNC)
1643150622Srik			return EBUSY;
1644150622Srik		if (d->ifp->if_drv_flags & IFF_DRV_RUNNING)
1645150622Srik			return EBUSY;
1646150622Srik		if (! strcmp ("cisco", (char*)data)) {
1647150622Srik			IFP2SP(d->ifp)->pp_flags &= ~(PP_FR);
1648150622Srik			IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE;
1649150622Srik			d->ifp->if_flags |= PP_CISCO;
1650150622Srik		} else if (! strcmp ("fr", (char*)data)) {
1651150622Srik			d->ifp->if_flags &= ~(PP_CISCO);
1652150622Srik			IFP2SP(d->ifp)->pp_flags |= PP_FR | PP_KEEPALIVE;
1653150622Srik		} else if (! strcmp ("ppp", (char*)data)) {
1654150622Srik			IFP2SP(d->ifp)->pp_flags &= ~(PP_FR | PP_KEEPALIVE);
1655150622Srik			d->ifp->if_flags &= ~(PP_CISCO);
1656150622Srik		} else
1657150622Srik			return EINVAL;
1658150622Srik		return 0;
1659150622Srik
1660150622Srik	case SERIAL_GETKEEPALIVE:
1661150622Srik		CX_DEBUG2 (d, ("ioctl: getkeepalive\n"));
1662150622Srik		if ((IFP2SP(d->ifp)->pp_flags & PP_FR) ||
1663150622Srik		    (d->ifp->if_flags & PP_CISCO) ||
1664150622Srik		    (c->mode == M_ASYNC))
1665150622Srik			return EINVAL;
1666150622Srik		s = splhigh ();
1667150622Srik		CX_LOCK (bd);
1668150622Srik		*(int*)data = (IFP2SP(d->ifp)->pp_flags & PP_KEEPALIVE) ? 1 : 0;
1669150622Srik		CX_UNLOCK (bd);
1670150622Srik		splx (s);
1671150622Srik		return 0;
1672150622Srik
1673150622Srik	case SERIAL_SETKEEPALIVE:
1674150622Srik		CX_DEBUG2 (d, ("ioctl: setkeepalive\n"));
1675150622Srik		/* Only for superuser! */
1676164033Srwatson		error = priv_check (td, PRIV_DRIVER);
1677150622Srik		if (error)
1678150622Srik			return error;
1679150622Srik		if ((IFP2SP(d->ifp)->pp_flags & PP_FR) ||
1680150622Srik			(d->ifp->if_flags & PP_CISCO))
1681150622Srik			return EINVAL;
1682150622Srik		s = splhigh ();
1683150622Srik		CX_LOCK (bd);
1684150622Srik		if (*(int*)data)
1685150622Srik			IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE;
1686150622Srik		else
1687150622Srik			IFP2SP(d->ifp)->pp_flags &= ~PP_KEEPALIVE;
1688150622Srik		CX_UNLOCK (bd);
1689150622Srik		splx (s);
1690150622Srik		return 0;
1691150622Srik#endif /*NETGRAPH*/
1692150622Srik
1693150622Srik	case SERIAL_GETMODE:
1694150622Srik		CX_DEBUG2 (d, ("ioctl: getmode\n"));
1695150622Srik		s = splhigh ();
1696150622Srik		CX_LOCK (bd);
1697150622Srik		*(int*)data = (c->mode == M_ASYNC) ?
1698150622Srik			SERIAL_ASYNC : SERIAL_HDLC;
1699150622Srik		CX_UNLOCK (bd);
1700150622Srik		splx (s);
1701150622Srik		return 0;
1702150622Srik
1703150622Srik	case SERIAL_SETMODE:
1704150622Srik		CX_DEBUG2 (d, ("ioctl: setmode\n"));
1705150622Srik		/* Only for superuser! */
1706164033Srwatson		error = priv_check (td, PRIV_DRIVER);
1707150622Srik		if (error)
1708150622Srik			return error;
1709150622Srik
1710150622Srik		/* Somebody is waiting for carrier? */
1711150622Srik		if (d->lock)
1712150622Srik			return EBUSY;
1713150622Srik		/* /dev/ttyXX is already opened by someone? */
1714150622Srik		if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN) &&
1715150622Srik		    (d->open_dev|0x2))
1716150622Srik			return EBUSY;
1717150622Srik		/* Network interface is up?
1718150622Srik		 * Cannot change to async mode. */
1719150622Srik		if (c->mode != M_ASYNC && d->running &&
1720150622Srik		    (*(int*)data == SERIAL_ASYNC))
1721150622Srik			return EBUSY;
1722150622Srik
1723150622Srik		s = splhigh ();
1724150622Srik		CX_LOCK (bd);
1725150622Srik		if (c->mode == M_HDLC && *(int*)data == SERIAL_ASYNC) {
1726150622Srik			cx_set_mode (c, M_ASYNC);
1727150622Srik			cx_enable_receive (c, 0);
1728150622Srik			cx_enable_transmit (c, 0);
1729150622Srik		} else if (c->mode == M_ASYNC && *(int*)data == SERIAL_HDLC) {
1730180132Srik			if (d->ifp->if_flags & IFF_DEBUG)
1731180132Srik				c->debug = c->debug_shadow;
1732150622Srik			cx_set_mode (c, M_HDLC);
1733150622Srik			cx_enable_receive (c, 1);
1734150622Srik			cx_enable_transmit (c, 1);
1735150622Srik		}
1736150622Srik		CX_UNLOCK (bd);
1737150622Srik		splx (s);
1738150622Srik		return 0;
1739150622Srik
1740150622Srik	case SERIAL_GETSTAT:
1741150622Srik		CX_DEBUG2 (d, ("ioctl: getestat\n"));
1742150622Srik		st = (struct serial_statistics*) data;
1743150622Srik		s = splhigh ();
1744150622Srik		CX_LOCK (bd);
1745150622Srik		st->rintr  = c->rintr;
1746150622Srik		st->tintr  = c->tintr;
1747150622Srik		st->mintr  = c->mintr;
1748150622Srik		st->ibytes = c->ibytes;
1749150622Srik		st->ipkts  = c->ipkts;
1750150622Srik		st->ierrs  = c->ierrs;
1751150622Srik		st->obytes = c->obytes;
1752150622Srik		st->opkts  = c->opkts;
1753150622Srik		st->oerrs  = c->oerrs;
1754150622Srik		CX_UNLOCK (bd);
1755150622Srik		splx (s);
1756150622Srik		return 0;
1757150622Srik
1758150622Srik	case SERIAL_CLRSTAT:
1759150622Srik		CX_DEBUG2 (d, ("ioctl: clrstat\n"));
1760150622Srik		/* Only for superuser! */
1761164033Srwatson		error = priv_check (td, PRIV_DRIVER);
1762150622Srik		if (error)
1763150622Srik			return error;
1764150622Srik		s = splhigh ();
1765150622Srik		CX_LOCK (bd);
1766150622Srik		c->rintr = 0;
1767150622Srik		c->tintr = 0;
1768150622Srik		c->mintr = 0;
1769150622Srik		c->ibytes = 0;
1770150622Srik		c->ipkts = 0;
1771150622Srik		c->ierrs = 0;
1772150622Srik		c->obytes = 0;
1773150622Srik		c->opkts = 0;
1774150622Srik		c->oerrs = 0;
1775150622Srik		CX_UNLOCK (bd);
1776150622Srik		splx (s);
1777150622Srik		return 0;
1778150622Srik
1779150622Srik	case SERIAL_GETBAUD:
1780150622Srik		CX_DEBUG2 (d, ("ioctl: getbaud\n"));
1781150622Srik		if (c->mode == M_ASYNC)
1782150622Srik			return EINVAL;
1783150622Srik		s = splhigh ();
1784150622Srik		CX_LOCK (bd);
1785150622Srik		*(long*)data = cx_get_baud(c);
1786150622Srik		CX_UNLOCK (bd);
1787150622Srik		splx (s);
1788150622Srik		return 0;
1789150622Srik
1790150622Srik	case SERIAL_SETBAUD:
1791150622Srik		CX_DEBUG2 (d, ("ioctl: setbaud\n"));
1792150622Srik		/* Only for superuser! */
1793164033Srwatson		error = priv_check (td, PRIV_DRIVER);
1794150622Srik		if (error)
1795150622Srik			return error;
1796150622Srik		if (c->mode == M_ASYNC)
1797150622Srik			return EINVAL;
1798150622Srik		s = splhigh ();
1799150622Srik		CX_LOCK (bd);
1800150622Srik		cx_set_baud (c, *(long*)data);
1801150622Srik		CX_UNLOCK (bd);
1802150622Srik		splx (s);
1803150622Srik		return 0;
1804150622Srik
1805150622Srik	case SERIAL_GETLOOP:
1806150622Srik		CX_DEBUG2 (d, ("ioctl: getloop\n"));
1807150622Srik		if (c->mode == M_ASYNC)
1808150622Srik			return EINVAL;
1809150622Srik		s = splhigh ();
1810150622Srik		CX_LOCK (bd);
1811150622Srik		*(int*)data = cx_get_loop (c);
1812150622Srik		CX_UNLOCK (bd);
1813150622Srik		splx (s);
1814150622Srik		return 0;
1815150622Srik
1816150622Srik	case SERIAL_SETLOOP:
1817150622Srik		CX_DEBUG2 (d, ("ioctl: setloop\n"));
1818150622Srik		/* Only for superuser! */
1819164033Srwatson		error = priv_check (td, PRIV_DRIVER);
1820150622Srik		if (error)
1821150622Srik			return error;
1822150622Srik		if (c->mode == M_ASYNC)
1823150622Srik			return EINVAL;
1824150622Srik		s = splhigh ();
1825150622Srik		CX_LOCK (bd);
1826150622Srik		cx_set_loop (c, *(int*)data);
1827150622Srik		CX_UNLOCK (bd);
1828150622Srik		splx (s);
1829150622Srik		return 0;
1830150622Srik
1831150622Srik	case SERIAL_GETDPLL:
1832150622Srik		CX_DEBUG2 (d, ("ioctl: getdpll\n"));
1833150622Srik		if (c->mode == M_ASYNC)
1834150622Srik			return EINVAL;
1835150622Srik		s = splhigh ();
1836150622Srik		CX_LOCK (bd);
1837150622Srik		*(int*)data = cx_get_dpll (c);
1838150622Srik		CX_UNLOCK (bd);
1839150622Srik		splx (s);
1840150622Srik		return 0;
1841150622Srik
1842150622Srik	case SERIAL_SETDPLL:
1843150622Srik		CX_DEBUG2 (d, ("ioctl: setdpll\n"));
1844150622Srik		/* Only for superuser! */
1845164033Srwatson		error = priv_check (td, PRIV_DRIVER);
1846150622Srik		if (error)
1847150622Srik			return error;
1848150622Srik		if (c->mode == M_ASYNC)
1849150622Srik			return EINVAL;
1850150622Srik		s = splhigh ();
1851150622Srik		CX_LOCK (bd);
1852150622Srik		cx_set_dpll (c, *(int*)data);
1853150622Srik		CX_UNLOCK (bd);
1854150622Srik		splx (s);
1855150622Srik		return 0;
1856150622Srik
1857150622Srik	case SERIAL_GETNRZI:
1858150622Srik		CX_DEBUG2 (d, ("ioctl: getnrzi\n"));
1859150622Srik		if (c->mode == M_ASYNC)
1860150622Srik			return EINVAL;
1861150622Srik		s = splhigh ();
1862150622Srik		CX_LOCK (bd);
1863150622Srik		*(int*)data = cx_get_nrzi (c);
1864150622Srik		CX_UNLOCK (bd);
1865150622Srik		splx (s);
1866150622Srik		return 0;
1867150622Srik
1868150622Srik	case SERIAL_SETNRZI:
1869150622Srik		CX_DEBUG2 (d, ("ioctl: setnrzi\n"));
1870150622Srik		/* Only for superuser! */
1871164033Srwatson		error = priv_check (td, PRIV_DRIVER);
1872150622Srik		if (error)
1873150622Srik			return error;
1874150622Srik		if (c->mode == M_ASYNC)
1875150622Srik			return EINVAL;
1876150622Srik		s = splhigh ();
1877150622Srik		CX_LOCK (bd);
1878150622Srik		cx_set_nrzi (c, *(int*)data);
1879150622Srik		CX_UNLOCK (bd);
1880150622Srik		splx (s);
1881150622Srik		return 0;
1882150622Srik
1883150622Srik	case SERIAL_GETDEBUG:
1884150622Srik		CX_DEBUG2 (d, ("ioctl: getdebug\n"));
1885150622Srik		s = splhigh ();
1886150622Srik		CX_LOCK (bd);
1887150622Srik		*(int*)data = c->debug;
1888150622Srik		CX_UNLOCK (bd);
1889150622Srik		splx (s);
1890150622Srik		return 0;
1891150622Srik
1892150622Srik	case SERIAL_SETDEBUG:
1893150622Srik		CX_DEBUG2 (d, ("ioctl: setdebug\n"));
1894150622Srik		/* Only for superuser! */
1895164033Srwatson		error = priv_check (td, PRIV_DRIVER);
1896150622Srik		if (error)
1897150622Srik			return error;
1898150622Srik		s = splhigh ();
1899150622Srik		CX_LOCK (bd);
1900180132Srik#ifndef	NETGRAPH
1901180132Srik		if (c->mode == M_ASYNC) {
1902180132Srik			c->debug = *(int*)data;
1903180132Srik		} else {
1904180132Srik			/*
1905180132Srik			 * The debug_shadow is always greater than zero for
1906180132Srik			 * logic simplicity.  For switching debug off the
1907180132Srik			 * IFF_DEBUG is responsible (for !M_ASYNC mode).
1908180132Srik			 */
1909180132Srik			c->debug_shadow = (*(int*)data) ? (*(int*)data) : 1;
1910180132Srik			if (d->ifp->if_flags & IFF_DEBUG)
1911180132Srik				c->debug = c->debug_shadow;
1912180132Srik		}
1913180132Srik#else
1914150622Srik		c->debug = *(int*)data;
1915180132Srik#endif
1916150622Srik		CX_UNLOCK (bd);
1917150622Srik		splx (s);
1918150622Srik		return 0;
1919150622Srik	}
1920150622Srik
1921150622Srik	switch (cmd) {
1922150622Srik	case TIOCSDTR:	/* Set DTR */
1923150622Srik		CX_DEBUG2 (d, ("ioctl: tiocsdtr\n"));
1924150622Srik		s = splhigh ();
1925150622Srik		CX_LOCK (bd);
1926150622Srik		cx_set_dtr (c, 1);
1927150622Srik		CX_UNLOCK (bd);
1928150622Srik		splx (s);
1929150622Srik		return 0;
1930150622Srik
1931150622Srik	case TIOCCDTR:	/* Clear DTR */
1932150622Srik		CX_DEBUG2 (d, ("ioctl: tioccdtr\n"));
1933150622Srik		s = splhigh ();
1934150622Srik		CX_LOCK (bd);
1935150622Srik		cx_set_dtr (c, 0);
1936150622Srik		CX_UNLOCK (bd);
1937150622Srik		splx (s);
1938150622Srik		return 0;
1939150622Srik
1940150622Srik	case TIOCMSET:	/* Set DTR/RTS */
1941150622Srik		CX_DEBUG2 (d, ("ioctl: tiocmset\n"));
1942150622Srik		s = splhigh ();
1943150622Srik		CX_LOCK (bd);
1944150622Srik		cx_set_dtr (c, (*(int*)data & TIOCM_DTR) ? 1 : 0);
1945150622Srik		cx_set_rts (c, (*(int*)data & TIOCM_RTS) ? 1 : 0);
1946150622Srik		CX_UNLOCK (bd);
1947150622Srik		splx (s);
1948150622Srik		return 0;
1949150622Srik
1950150622Srik	case TIOCMBIS:	/* Add DTR/RTS */
1951150622Srik		CX_DEBUG2 (d, ("ioctl: tiocmbis\n"));
1952150622Srik		s = splhigh ();
1953150622Srik		CX_LOCK (bd);
1954150622Srik		if (*(int*)data & TIOCM_DTR) cx_set_dtr (c, 1);
1955150622Srik		if (*(int*)data & TIOCM_RTS) cx_set_rts (c, 1);
1956150622Srik		CX_UNLOCK (bd);
1957150622Srik		splx (s);
1958150622Srik		return 0;
1959150622Srik
1960150622Srik	case TIOCMBIC:	/* Clear DTR/RTS */
1961150622Srik		CX_DEBUG2 (d, ("ioctl: tiocmbic\n"));
1962150622Srik		s = splhigh ();
1963150622Srik		CX_LOCK (bd);
1964150622Srik		if (*(int*)data & TIOCM_DTR) cx_set_dtr (c, 0);
1965150622Srik		if (*(int*)data & TIOCM_RTS) cx_set_rts (c, 0);
1966150622Srik		CX_UNLOCK (bd);
1967150622Srik		splx (s);
1968150622Srik		return 0;
1969150622Srik
1970150622Srik	case TIOCMGET:	/* Get modem status */
1971150622Srik		CX_DEBUG2 (d, ("ioctl: tiocmget\n"));
1972150622Srik		*(int*)data = cx_modem_status (d);
1973150622Srik		return 0;
1974150622Srik
1975150622Srik	}
1976150622Srik
1977150622Srik	CX_DEBUG2 (d, ("ioctl: 0x%lx\n", cmd));
1978150622Srik	return ENOTTY;
1979150622Srik}
1980150622Srik
1981123120Simpvoid cx_softintr (void *unused)
1982123120Simp{
1983123120Simp	drv_t *d;
1984138823Srik	bdrv_t *bd;
1985123120Simp	async_q *q;
1986123120Simp	int i, s, ic, k;
1987123120Simp	while (MY_SOFT_INTR) {
1988123120Simp		MY_SOFT_INTR = 0;
1989123120Simp		for (i=0; i<NCX*NCHAN; ++i) {
1990123120Simp			d = channel [i];
1991123120Simp			if (!d || !d->chan || d->chan->type == T_NONE
1992130240Srik			    || d->chan->mode != M_ASYNC || !d->tty
1993130240Srik			    || !d->tty->t_dev)
1994123120Simp				continue;
1995138823Srik			bd = d->board->sys;
1996123120Simp			s = splhigh ();
1997138823Srik			CX_LOCK (bd);
1998123120Simp			if (d->intr_action & CX_READ) {
1999123120Simp				q = &(d->aqueue);
2000130240Srik				if (d->tty->t_state & TS_CAN_BYPASS_L_RINT) {
2001123120Simp					k = AQ_GSZ(q);
2002130240Srik					if (d->tty->t_rawq.c_cc + k >
2003130240Srik						d->tty->t_ihiwat
2004130240Srik					    && (d->tty->t_cflag & CRTS_IFLOW
2005130240Srik						|| d->tty->t_iflag & IXOFF)
2006130240Srik					    && !(d->tty->t_state & TS_TBLOCK))
2007130240Srik						ttyblock(d->tty);
2008130240Srik					d->tty->t_rawcc += k;
2009123120Simp					while (k>0) {
2010123120Simp						k--;
2011123120Simp						AQ_POP (q, ic);
2012138823Srik						CX_UNLOCK (bd);
2013123120Simp						splx (s);
2014130240Srik						putc (ic, &d->tty->t_rawq);
2015123120Simp						s = splhigh ();
2016138823Srik						CX_LOCK (bd);
2017123120Simp					}
2018130240Srik					ttwakeup(d->tty);
2019130240Srik					if (d->tty->t_state & TS_TTSTOP
2020130240Srik					    && (d->tty->t_iflag & IXANY
2021130240Srik						|| d->tty->t_cc[VSTART] ==
2022130240Srik						d->tty->t_cc[VSTOP])) {
2023130240Srik						d->tty->t_state &= ~TS_TTSTOP;
2024130240Srik						d->tty->t_lflag &= ~FLUSHO;
2025123120Simp						d->intr_action |= CX_WRITE;
2026123120Simp					}
2027123120Simp				} else {
2028123120Simp					while (q->end != q->beg) {
2029123120Simp						AQ_POP (q, ic);
2030138823Srik						CX_UNLOCK (bd);
2031123120Simp						splx (s);
2032130240Srik						ttyld_rint (d->tty, ic);
2033123120Simp						s = splhigh ();
2034138823Srik						CX_LOCK (bd);
2035123120Simp					}
2036123120Simp				}
2037123120Simp				d->intr_action &= ~CX_READ;
2038123120Simp			}
2039123120Simp			splx (s);
2040138823Srik			CX_UNLOCK (bd);
2041123120Simp
2042123120Simp			s = splhigh ();
2043138823Srik			CX_LOCK (bd);
2044123120Simp			if (d->intr_action & CX_WRITE) {
2045130240Srik				if (d->tty->t_line)
2046130240Srik					ttyld_start (d->tty);
2047123120Simp				else
2048130240Srik					cx_oproc (d->tty);
2049123120Simp				d->intr_action &= ~CX_WRITE;
2050123120Simp			}
2051138823Srik			CX_UNLOCK (bd);
2052123120Simp			splx (s);
2053123120Simp
2054123120Simp		}
2055123120Simp	}
2056123120Simp}
2057123120Simp
2058123120Simp/*
2059123120Simp * Fill transmitter buffer with data.
2060123120Simp */
2061123120Simpstatic void cx_oproc (struct tty *tp)
2062123120Simp{
2063136480Sphk	int s, k;
2064136480Sphk	drv_t *d;
2065138823Srik	bdrv_t *bd;
2066123120Simp	static u_char buf[DMABUFSZ];
2067123120Simp	u_char *p;
2068123120Simp	u_short len = 0, sublen = 0;
2069123120Simp
2070138823Srik	d = tp->t_sc;
2071138823Srik	bd = d->board->sys;
2072136480Sphk
2073123120Simp	CX_DEBUG2 (d, ("cx_oproc\n"));
2074138823Srik
2075138823Srik	s = splhigh ();
2076138823Srik	CX_LOCK (bd);
2077138823Srik
2078123120Simp	if (tp->t_cflag & CRTSCTS && (tp->t_state & TS_TBLOCK) && d->chan->rts)
2079123120Simp		cx_set_rts (d->chan, 0);
2080123120Simp	else if (tp->t_cflag & CRTSCTS && ! (tp->t_state & TS_TBLOCK) && ! d->chan->rts)
2081123120Simp		cx_set_rts (d->chan, 1);
2082123120Simp
2083123120Simp	if (! (tp->t_state & (TS_TIMEOUT | TS_TTSTOP))) {
2084123120Simp		/* Start transmitter. */
2085123120Simp		cx_enable_transmit (d->chan, 1);
2086123120Simp
2087123120Simp		/* Is it busy? */
2088123120Simp		if (! cx_buf_free (d->chan)) {
2089123120Simp			tp->t_state |= TS_BUSY;
2090138823Srik			CX_UNLOCK (bd);
2091123120Simp			splx (s);
2092123120Simp			return;
2093123120Simp		}
2094123120Simp		if (tp->t_iflag & IXOFF) {
2095123120Simp			p = (buf + (DMABUFSZ/2));
2096123120Simp			sublen = q_to_b (&tp->t_outq, p, (DMABUFSZ/2));
2097123120Simp			k = sublen;
2098123120Simp			while (k--) {
2099123120Simp				/* Send XON/XOFF out of band. */
2100123120Simp				if (*p == tp->t_cc[VSTOP]) {
2101123120Simp					cx_xflow_ctl (d->chan, 0);
2102123120Simp					p++;
2103123120Simp					continue;
2104123120Simp				}
2105123120Simp				if (*p == tp->t_cc[VSTART]) {
2106123120Simp					cx_xflow_ctl (d->chan, 1);
2107123120Simp					p++;
2108123120Simp					continue;
2109123120Simp				}
2110123120Simp				buf[len] = *p;
2111123120Simp				len++;
2112123120Simp				p++;
2113123120Simp			}
2114123120Simp		} else {
2115123120Simp			p = buf;
2116123120Simp			len = q_to_b (&tp->t_outq, p, (DMABUFSZ/2));
2117123120Simp		}
2118123120Simp		if (len) {
2119123120Simp			cx_send_packet (d->chan, buf, len, 0);
2120123120Simp			tp->t_state |= TS_BUSY;
2121123120Simp			d->atimeout = 10;
2122123120Simp			CX_DEBUG2 (d, ("out %d bytes\n", len));
2123123120Simp		}
2124123120Simp	}
2125123120Simp	ttwwakeup (tp);
2126138823Srik	CX_UNLOCK (bd);
2127123120Simp	splx (s);
2128123120Simp}
2129123120Simp
2130123120Simpstatic int cx_param (struct tty *tp, struct termios *t)
2131123120Simp{
2132136480Sphk	drv_t *d;
2133138823Srik	bdrv_t *bd;
2134123120Simp	int s, bits, parity;
2135123120Simp
2136136480Sphk	d = tp->t_sc;
2137138823Srik	bd = d->board->sys;
2138138823Srik
2139123120Simp	s = splhigh ();
2140138823Srik	CX_LOCK (bd);
2141123120Simp	if (t->c_ospeed == 0) {
2142123120Simp		/* Clear DTR and RTS. */
2143123120Simp		cx_set_dtr (d->chan, 0);
2144138823Srik		CX_UNLOCK (bd);
2145123120Simp		splx (s);
2146123120Simp		CX_DEBUG2 (d, ("cx_param (hangup)\n"));
2147123120Simp		return 0;
2148123120Simp	}
2149123120Simp	CX_DEBUG2 (d, ("cx_param\n"));
2150123120Simp
2151123120Simp	/* Check requested parameters. */
2152123120Simp	if (t->c_ospeed < 300 || t->c_ospeed > 256*1024) {
2153138823Srik		CX_UNLOCK (bd);
2154123120Simp		splx (s);
2155133644Srik		return EINVAL;
2156123120Simp	}
2157123120Simp	if (t->c_ispeed && (t->c_ispeed < 300 || t->c_ispeed > 256*1024)) {
2158138823Srik		CX_UNLOCK (bd);
2159123120Simp		splx (s);
2160133644Srik		return EINVAL;
2161123120Simp	}
2162123120Simp
2163123120Simp  	/* And copy them to tty and channel structures. */
2164123120Simp	tp->t_ispeed = t->c_ispeed = tp->t_ospeed = t->c_ospeed;
2165123120Simp	tp->t_cflag = t->c_cflag;
2166123120Simp
2167123120Simp	/* Set character length and parity mode. */
2168123120Simp	switch (t->c_cflag & CSIZE) {
2169123120Simp	default:
2170123120Simp	case CS8: bits = 8; break;
2171123120Simp	case CS7: bits = 7; break;
2172123120Simp	case CS6: bits = 6; break;
2173123120Simp	case CS5: bits = 5; break;
2174123120Simp	}
2175123120Simp
2176123120Simp	parity = ((t->c_cflag & PARENB) ? 1 : 0) *
2177123120Simp		 (1 + ((t->c_cflag & PARODD) ? 0 : 1));
2178123120Simp
2179123120Simp	/* Set current channel number. */
2180123120Simp	if (! d->chan->dtr)
2181123120Simp		cx_set_dtr (d->chan, 1);
2182123120Simp
2183130240Srik	ttyldoptim (tp);
2184123120Simp	cx_set_async_param (d->chan, t->c_ospeed, bits, parity, (t->c_cflag & CSTOPB),
2185123120Simp		!(t->c_cflag & PARENB), (t->c_cflag & CRTSCTS),
2186123120Simp		(t->c_iflag & IXON), (t->c_iflag & IXANY),
2187123120Simp		t->c_cc[VSTART], t->c_cc[VSTOP]);
2188138823Srik	CX_UNLOCK (bd);
2189123120Simp	splx (s);
2190123120Simp	return 0;
2191123120Simp}
2192123120Simp
2193123120Simp/*
2194123120Simp * Stop output on a line
2195123120Simp */
2196123120Simpstatic void cx_stop (struct tty *tp, int flag)
2197123120Simp{
2198136480Sphk	drv_t *d;
2199138823Srik	bdrv_t *bd;
2200123120Simp	int s;
2201123120Simp
2202136480Sphk	d = tp->t_sc;
2203138823Srik	bd = d->board->sys;
2204138823Srik
2205123120Simp	s = splhigh ();
2206138823Srik	CX_LOCK (bd);
2207123120Simp	if (tp->t_state & TS_BUSY) {
2208123120Simp		/* Stop transmitter */
2209123120Simp		CX_DEBUG2 (d, ("cx_stop\n"));
2210123120Simp		cx_transmitter_ctl (d->chan, 0);
2211123120Simp	}
2212138823Srik	CX_UNLOCK (bd);
2213123120Simp	splx (s);
2214123120Simp}
2215123120Simp
2216123120Simp/*
2217123120Simp * Process the (delayed) carrier signal setup.
2218123120Simp */
2219123120Simpstatic void cx_carrier (void *arg)
2220123120Simp{
2221123120Simp	drv_t *d = arg;
2222138823Srik	bdrv_t *bd = d->board->sys;
2223123120Simp	cx_chan_t *c = d->chan;
2224123120Simp	int s, cd;
2225123120Simp
2226123120Simp	s = splhigh ();
2227138823Srik	CX_LOCK (bd);
2228123120Simp	cd = cx_get_cd (c);
2229123120Simp	if (d->cd != cd) {
2230123120Simp		if (cd) {
2231123120Simp			CX_DEBUG (d, ("carrier on\n"));
2232123120Simp			d->cd = 1;
2233138823Srik			CX_UNLOCK (bd);
2234123120Simp			splx (s);
2235130240Srik			if (d->tty)
2236130240Srik				ttyld_modem(d->tty, 1);
2237123120Simp		} else {
2238123120Simp			CX_DEBUG (d, ("carrier loss\n"));
2239123120Simp			d->cd = 0;
2240138823Srik			CX_UNLOCK (bd);
2241123120Simp			splx (s);
2242130240Srik			if (d->tty)
2243130240Srik				ttyld_modem(d->tty, 0);
2244123120Simp		}
2245147859Srik	} else {
2246147859Srik		CX_UNLOCK (bd);
2247147859Srik		splx (s);
2248123120Simp	}
2249123120Simp}
2250123120Simp
2251123120Simp/*
2252123120Simp * Modem signal callback function.
2253123120Simp */
2254123120Simpstatic void cx_modem (cx_chan_t *c)
2255123120Simp{
2256123120Simp	drv_t *d = c->sys;
2257123120Simp
2258123120Simp	if (!d || c->mode != M_ASYNC)
2259123120Simp		return;
2260123120Simp	/* Handle carrier detect/loss. */
2261123120Simp	/* Carrier changed - delay processing DCD for a while
2262123120Simp	 * to give both sides some time to initialize. */
2263138823Srik	callout_reset (&d->dcd_timeout_handle, hz/2, cx_carrier, d);
2264123120Simp}
2265123120Simp
2266123120Simp#ifdef NETGRAPH
2267123120Simpstatic int ng_cx_constructor (node_p node)
2268123120Simp{
2269123120Simp	drv_t *d = NG_NODE_PRIVATE (node);
2270123120Simp	CX_DEBUG (d, ("Constructor\n"));
2271123120Simp	return EINVAL;
2272123120Simp}
2273123120Simp
2274123120Simpstatic int ng_cx_newhook (node_p node, hook_p hook, const char *name)
2275123120Simp{
2276123120Simp	int s;
2277123120Simp	drv_t *d = NG_NODE_PRIVATE (node);
2278138823Srik	bdrv_t *bd = d->board->sys;
2279123120Simp
2280123120Simp	if (d->chan->mode == M_ASYNC)
2281123120Simp		return EINVAL;
2282123120Simp
2283123120Simp	/* Attach debug hook */
2284123120Simp	if (strcmp (name, NG_CX_HOOK_DEBUG) == 0) {
2285123120Simp		NG_HOOK_SET_PRIVATE (hook, NULL);
2286123120Simp		d->debug_hook = hook;
2287123120Simp		return 0;
2288123120Simp	}
2289123120Simp
2290123120Simp	/* Check for raw hook */
2291123120Simp	if (strcmp (name, NG_CX_HOOK_RAW) != 0)
2292123120Simp		return EINVAL;
2293123120Simp
2294123120Simp	NG_HOOK_SET_PRIVATE (hook, d);
2295123120Simp	d->hook = hook;
2296123120Simp	s = splhigh ();
2297138823Srik	CX_LOCK (bd);
2298123120Simp	cx_up (d);
2299138823Srik	CX_UNLOCK (bd);
2300123120Simp	splx (s);
2301123120Simp	return 0;
2302123120Simp}
2303123120Simp
2304123120Simpstatic int print_modems (char *s, cx_chan_t *c, int need_header)
2305123120Simp{
2306123120Simp	int status = cx_modem_status (c->sys);
2307123120Simp	int length = 0;
2308123120Simp
2309123120Simp	if (need_header)
2310123120Simp		length += sprintf (s + length, "  LE   DTR  DSR  RTS  CTS  CD\n");
2311123120Simp	length += sprintf (s + length, "%4s %4s %4s %4s %4s %4s\n",
2312123120Simp		status & TIOCM_LE  ? "On" : "-",
2313123120Simp		status & TIOCM_DTR ? "On" : "-",
2314123120Simp		status & TIOCM_DSR ? "On" : "-",
2315123120Simp		status & TIOCM_RTS ? "On" : "-",
2316123120Simp		status & TIOCM_CTS ? "On" : "-",
2317123120Simp		status & TIOCM_CD  ? "On" : "-");
2318123120Simp	return length;
2319123120Simp}
2320123120Simp
2321123120Simpstatic int print_stats (char *s, cx_chan_t *c, int need_header)
2322123120Simp{
2323123120Simp	int length = 0;
2324123120Simp
2325123120Simp	if (need_header)
2326123120Simp		length += sprintf (s + length, "  Rintr   Tintr   Mintr   Ibytes   Ipkts   Ierrs   Obytes   Opkts   Oerrs\n");
2327123120Simp	length += sprintf (s + length, "%7ld %7ld %7ld %8ld %7ld %7ld %8ld %7ld %7ld\n",
2328123120Simp		c->rintr, c->tintr, c->mintr, c->ibytes, c->ipkts,
2329123120Simp		c->ierrs, c->obytes, c->opkts, c->oerrs);
2330123120Simp	return length;
2331123120Simp}
2332123120Simp
2333123120Simpstatic int print_chan (char *s, cx_chan_t *c)
2334123120Simp{
2335123120Simp	drv_t *d = c->sys;
2336123120Simp	int length = 0;
2337123120Simp
2338123120Simp	length += sprintf (s + length, "cx%d", c->board->num * NCHAN + c->num);
2339123120Simp	if (d->chan->debug)
2340123120Simp		length += sprintf (s + length, " debug=%d", d->chan->debug);
2341123120Simp
2342123120Simp	if (cx_get_baud (c))
2343123120Simp		length += sprintf (s + length, " %ld", cx_get_baud (c));
2344123120Simp	else
2345123120Simp		length += sprintf (s + length, " extclock");
2346123120Simp
2347123120Simp	if (c->mode == M_HDLC) {
2348123120Simp		length += sprintf (s + length, " dpll=%s", cx_get_dpll (c) ? "on" : "off");
2349123120Simp		length += sprintf (s + length, " nrzi=%s", cx_get_nrzi (c) ? "on" : "off");
2350123120Simp	}
2351123120Simp
2352123120Simp	length += sprintf (s + length, " loop=%s", cx_get_loop (c) ? "on\n" : "off\n");
2353123120Simp	return length;
2354123120Simp}
2355123120Simp
2356123120Simpstatic int ng_cx_rcvmsg (node_p node, item_p item, hook_p lasthook)
2357123120Simp{
2358123120Simp	drv_t *d = NG_NODE_PRIVATE (node);
2359123120Simp	struct ng_mesg *msg;
2360123120Simp	struct ng_mesg *resp = NULL;
2361123120Simp	int error = 0;
2362123120Simp
2363123120Simp	if (!d)
2364123120Simp		return EINVAL;
2365123120Simp
2366123120Simp	CX_DEBUG (d, ("Rcvmsg\n"));
2367123120Simp	NGI_GET_MSG (item, msg);
2368123120Simp	switch (msg->header.typecookie) {
2369123120Simp	default:
2370123120Simp		error = EINVAL;
2371123120Simp		break;
2372123120Simp
2373123120Simp	case NGM_CX_COOKIE:
2374123120Simp		printf ("Don't forget to implement\n");
2375123120Simp		error = EINVAL;
2376123120Simp		break;
2377123120Simp
2378123120Simp	case NGM_GENERIC_COOKIE:
2379123120Simp		switch (msg->header.cmd) {
2380123120Simp		default:
2381123120Simp			error = EINVAL;
2382123120Simp			break;
2383123120Simp
2384123120Simp		case NGM_TEXT_STATUS: {
2385123120Simp			char *s;
2386123120Simp			int l = 0;
2387123120Simp			int dl = sizeof (struct ng_mesg) + 730;
2388123120Simp
2389123120Simp			NG_MKRESPONSE (resp, msg, dl, M_NOWAIT);
2390123120Simp			if (! resp) {
2391123120Simp				error = ENOMEM;
2392123120Simp				break;
2393123120Simp			}
2394123120Simp			bzero (resp, dl);
2395123120Simp			s = (resp)->data;
2396123120Simp			l += print_chan (s + l, d->chan);
2397123120Simp			l += print_stats (s + l, d->chan, 1);
2398123120Simp			l += print_modems (s + l, d->chan, 1);
2399193813Simp			strncpy ((resp)->header.cmdstr, "status", NG_CMDSTRSIZ);
2400123120Simp			}
2401123120Simp			break;
2402123120Simp		}
2403123120Simp		break;
2404123120Simp	}
2405123120Simp	NG_RESPOND_MSG (error, node, item, resp);
2406123120Simp	NG_FREE_MSG (msg);
2407123120Simp	return error;
2408123120Simp}
2409123120Simp
2410123120Simpstatic int ng_cx_rcvdata (hook_p hook, item_p item)
2411123120Simp{
2412123120Simp	drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE(hook));
2413123120Simp	struct mbuf *m;
2414131108Sjulian	struct ng_tag_prio *ptag;
2415138823Srik	bdrv_t *bd;
2416123120Simp	struct ifqueue *q;
2417123120Simp	int s;
2418123120Simp
2419123120Simp	NGI_GET_M (item, m);
2420123120Simp	NG_FREE_ITEM (item);
2421123120Simp	if (! NG_HOOK_PRIVATE (hook) || ! d) {
2422123120Simp		NG_FREE_M (m);
2423123120Simp		return ENETDOWN;
2424123120Simp	}
2425131108Sjulian
2426138823Srik	bd = d->board->sys;
2427131108Sjulian	/* Check for high priority data */
2428131108Sjulian	if ((ptag = (struct ng_tag_prio *)m_tag_locate(m, NGM_GENERIC_COOKIE,
2429131108Sjulian	    NG_TAG_PRIO, NULL)) != NULL && (ptag->priority > NG_PRIO_CUTOFF) )
2430131108Sjulian		q = &d->hi_queue;
2431131108Sjulian	else
2432131108Sjulian		q = &d->lo_queue;
2433131108Sjulian
2434123120Simp	s = splhigh ();
2435138823Srik	CX_LOCK (bd);
2436123120Simp	IF_LOCK (q);
2437123120Simp	if (_IF_QFULL (q)) {
2438123120Simp		_IF_DROP (q);
2439123120Simp		IF_UNLOCK (q);
2440138823Srik		CX_UNLOCK (bd);
2441123120Simp		splx (s);
2442123120Simp		NG_FREE_M (m);
2443123120Simp		return ENOBUFS;
2444123120Simp	}
2445123120Simp	_IF_ENQUEUE (q, m);
2446123120Simp	IF_UNLOCK (q);
2447123120Simp	cx_start (d);
2448138823Srik	CX_UNLOCK (bd);
2449123120Simp	splx (s);
2450123120Simp	return 0;
2451123120Simp}
2452123120Simp
2453123120Simpstatic int ng_cx_rmnode (node_p node)
2454123120Simp{
2455123120Simp	drv_t *d = NG_NODE_PRIVATE (node);
2456138823Srik	bdrv_t *bd;
2457123120Simp
2458123120Simp	CX_DEBUG (d, ("Rmnode\n"));
2459123120Simp	if (d && d->running) {
2460123120Simp		int s = splhigh ();
2461138823Srik		bd = d->board->sys;
2462138823Srik		CX_LOCK (bd);
2463123120Simp		cx_down (d);
2464138823Srik		CX_UNLOCK (bd);
2465123120Simp		splx (s);
2466123120Simp	}
2467123120Simp#ifdef	KLD_MODULE
2468132464Sjulian	if (node->nd_flags & NGF_REALLY_DIE) {
2469123120Simp		NG_NODE_SET_PRIVATE (node, NULL);
2470123120Simp		NG_NODE_UNREF (node);
2471123120Simp	}
2472132464Sjulian	NG_NODE_REVIVE(node);		/* Persistant node */
2473123120Simp#endif
2474123120Simp	return 0;
2475123120Simp}
2476123120Simp
2477123120Simpstatic int ng_cx_connect (hook_p hook)
2478123120Simp{
2479123120Simp	drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2480123120Simp
2481199407Sjhb	callout_reset (&d->timeout_handle, hz, cx_watchdog_timer, d);
2482123120Simp	return 0;
2483123120Simp}
2484123120Simp
2485123120Simpstatic int ng_cx_disconnect (hook_p hook)
2486123120Simp{
2487123120Simp	drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2488138823Srik	bdrv_t *bd = d->board->sys;
2489123120Simp	int s;
2490123120Simp
2491123120Simp	s = splhigh ();
2492138823Srik	CX_LOCK (bd);
2493123120Simp	if (NG_HOOK_PRIVATE (hook))
2494123120Simp		cx_down (d);
2495138823Srik	CX_UNLOCK (bd);
2496123120Simp	splx (s);
2497138823Srik	/* If we were wait it than it reasserted now, just stop it. */
2498138823Srik	if (!callout_drain (&d->timeout_handle))
2499138823Srik		callout_stop (&d->timeout_handle);
2500123120Simp	return 0;
2501123120Simp}
2502123120Simp#endif /*NETGRAPH*/
2503123120Simp
2504123120Simpstatic int cx_modevent (module_t mod, int type, void *unused)
2505123120Simp{
2506123120Simp	static int load_count = 0;
2507123120Simp
2508123120Simp	switch (type) {
2509123120Simp	case MOD_LOAD:
2510138823Srik#ifdef NETGRAPH
2511123120Simp		if (ng_newtype (&typestruct))
2512123120Simp			printf ("Failed to register ng_cx\n");
2513123120Simp#endif
2514123120Simp		++load_count;
2515138823Srik
2516188768Srwatson		callout_init (&timeout_handle, CALLOUT_MPSAFE);
2517138823Srik		callout_reset (&timeout_handle, hz*5, cx_timeout, 0);
2518123120Simp		/* Software interrupt. */
2519151690Sru		swi_add(&tty_intr_event, "cx", cx_softintr, NULL, SWI_TTY,
2520188768Srwatson		    INTR_MPSAFE, &cx_fast_ih);
2521123120Simp		break;
2522123120Simp	case MOD_UNLOAD:
2523123120Simp		if (load_count == 1) {
2524123120Simp			printf ("Removing device entry for Sigma\n");
2525138823Srik#ifdef NETGRAPH
2526123120Simp			ng_rmtype (&typestruct);
2527123120Simp#endif
2528123120Simp		}
2529138823Srik		/* If we were wait it than it reasserted now, just stop it. */
2530138823Srik		if (!callout_drain (&timeout_handle))
2531138823Srik			callout_stop (&timeout_handle);
2532151700Sjhb		swi_remove (cx_fast_ih);
2533123120Simp		--load_count;
2534123120Simp		break;
2535123120Simp	case MOD_SHUTDOWN:
2536123120Simp		break;
2537123120Simp	}
2538123120Simp	return 0;
2539123120Simp}
2540123120Simp
2541123120Simp#ifdef NETGRAPH
2542123120Simpstatic struct ng_type typestruct = {
2543129837Srik	.version	= NG_ABI_VERSION,
2544129837Srik	.name		= NG_CX_NODE_TYPE,
2545129837Srik	.constructor	= ng_cx_constructor,
2546129837Srik	.rcvmsg		= ng_cx_rcvmsg,
2547129837Srik	.shutdown	= ng_cx_rmnode,
2548129837Srik	.newhook	= ng_cx_newhook,
2549129837Srik	.connect	= ng_cx_connect,
2550129837Srik	.rcvdata	= ng_cx_rcvdata,
2551130985Srik	.disconnect	= ng_cx_disconnect,
2552123120Simp};
2553123120Simp#endif /*NETGRAPH*/
2554123120Simp
2555123120Simp#ifdef NETGRAPH
2556123120SimpMODULE_DEPEND (ng_cx, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION);
2557123120Simp#else
2558123120SimpMODULE_DEPEND (isa_cx, sppp, 1, 1, 1);
2559123120Simp#endif
2560123120SimpDRIVER_MODULE (cx, isa, cx_isa_driver, cx_devclass, cx_modevent, NULL);
2561138823SrikMODULE_VERSION (cx, 1);
2562