if_cx.c revision 165632
1139749Simp/*-
2123120Simp * Cronyx-Sigma adapter driver for FreeBSD.
3123120Simp * Supports PPP/HDLC and Cisco/HDLC protocol in synchronous mode,
4123120Simp * and asyncronous 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: head/sys/dev/cx/if_cx.c 165632 2006-12-29 13:59:50Z jhb $");
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
86138823Srikstatic	int	cx_mpsafenet = 1;
87138823SrikTUNABLE_INT("debug.cx.mpsafenet", &cx_mpsafenet);
88138823SrikSYSCTL_NODE(_debug, OID_AUTO, cx, CTLFLAG_RD, 0, "Cronyx Sigma Adapters");
89138823SrikSYSCTL_INT(_debug_cx, OID_AUTO, mpsafenet, CTLFLAG_RD, &cx_mpsafenet, 0,
90138823Srik	"Enable/disable MPSAFE network support for Cronyx Sigma Adapters");
91138823Srik
92138823Srik#define CX_LOCK(_bd)		do { \
93138823Srik				    if (cx_mpsafenet) \
94138823Srik					mtx_lock (&(_bd)->cx_mtx); \
95138823Srik				} while (0)
96138823Srik#define CX_UNLOCK(_bd)		do { \
97138823Srik				    if (cx_mpsafenet) \
98138823Srik					mtx_unlock (&(_bd)->cx_mtx); \
99138823Srik				} while (0)
100138823Srik#define CX_LOCK_ASSERT(_bd)	do { \
101138823Srik				    if (cx_mpsafenet) \
102138823Srik					mtx_assert (&(_bd)->cx_mtx, MA_OWNED); \
103138823Srik				} while (0)
104138823Srik
105123120Simptypedef struct _async_q {
106123120Simp	int beg;
107123120Simp	int end;
108123120Simp	#define BF_SZ 14400
109123120Simp	int buf[BF_SZ+1];
110123120Simp} async_q;
111123120Simp
112123120Simp#define AQ_GSZ(q)	((BF_SZ + (q)->end - (q)->beg)%BF_SZ)
113123120Simp#define AQ_PUSH(q,c)	{*((q)->buf + (q)->end) = c;\
114123120Simp			(q)->end = ((q)->end + 1)%BF_SZ;}
115123120Simp#define AQ_POP(q,c)	{c = *((q)->buf + (q)->beg);\
116123120Simp			(q)->beg = ((q)->beg + 1)%BF_SZ;}
117123120Simp
118123120Simpstatic void cx_identify		__P((driver_t *, device_t));
119123120Simpstatic int cx_probe		__P((device_t));
120123120Simpstatic int cx_attach		__P((device_t));
121123120Simpstatic int cx_detach		__P((device_t));
122136480Sphkstatic t_open_t			cx_topen;
123136480Sphkstatic t_modem_t		cx_tmodem;
124136480Sphkstatic t_close_t		cx_tclose;
125123120Simp
126123120Simpstatic device_method_t cx_isa_methods [] = {
127123120Simp	DEVMETHOD(device_identify,	cx_identify),
128123120Simp	DEVMETHOD(device_probe,		cx_probe),
129123120Simp	DEVMETHOD(device_attach,	cx_attach),
130123120Simp	DEVMETHOD(device_detach,	cx_detach),
131123120Simp	{0, 0}
132123120Simp};
133123120Simp
134130985Sriktypedef struct _cx_dma_mem_t {
135130985Srik	unsigned long	phys;
136130985Srik	void		*virt;
137130985Srik	size_t		size;
138130985Srik	bus_dma_tag_t	dmat;
139130985Srik	bus_dmamap_t	mapp;
140130985Srik} cx_dma_mem_t;
141123120Simp
142123120Simptypedef struct _drv_t {
143123120Simp	char name [8];
144123120Simp	cx_chan_t *chan;
145123120Simp	cx_board_t *board;
146130985Srik	cx_dma_mem_t dmamem;
147130240Srik	struct tty *tty;
148138823Srik	struct callout dcd_timeout_handle;
149123120Simp	unsigned callout;
150123120Simp	unsigned lock;
151123120Simp	int open_dev;
152123120Simp	int cd;
153123120Simp	int running;
154123120Simp#ifdef NETGRAPH
155123120Simp	char	nodename [NG_NODELEN+1];
156123120Simp	hook_p	hook;
157123120Simp	hook_p	debug_hook;
158123120Simp	node_p	node;
159123120Simp	struct	ifqueue lo_queue;
160123120Simp	struct	ifqueue hi_queue;
161123120Simp	short	timeout;
162138823Srik	struct	callout timeout_handle;
163123120Simp#else
164138823Srik	struct	ifqueue queue;
165147256Sbrooks	struct	ifnet *ifp;
166123120Simp#endif
167138823Srik	struct	cdev *devt;
168138823Srik	async_q	aqueue;
169130971Srik#define CX_READ 1
170130971Srik#define CX_WRITE 2
171123120Simp	int intr_action;
172123120Simp	short atimeout;
173123120Simp} drv_t;
174123120Simp
175130985Sriktypedef struct _bdrv_t {
176130985Srik	cx_board_t	*board;
177130985Srik	struct resource	*base_res;
178130985Srik	struct resource	*drq_res;
179130985Srik	struct resource	*irq_res;
180130985Srik	int		base_rid;
181130985Srik	int		drq_rid;
182130985Srik	int		irq_rid;
183130985Srik	void		*intrhand;
184130985Srik	drv_t		channel [NCHAN];
185138823Srik	struct mtx	cx_mtx;
186130985Srik} bdrv_t;
187130985Srik
188130985Srikstatic driver_t cx_isa_driver = {
189130985Srik	"cx",
190130985Srik	cx_isa_methods,
191130985Srik	sizeof (bdrv_t),
192130985Srik};
193130985Srik
194130985Srikstatic devclass_t cx_devclass;
195130985Srik
196123120Simpextern long csigma_fw_len;
197123120Simpextern const char *csigma_fw_version;
198123120Simpextern const char *csigma_fw_date;
199123120Simpextern const char *csigma_fw_copyright;
200123120Simpextern const cr_dat_tst_t csigma_fw_tvec[];
201123120Simpextern const u_char csigma_fw_data[];
202123120Simpstatic void cx_oproc (struct tty *tp);
203123120Simpstatic int cx_param (struct tty *tp, struct termios *t);
204123120Simpstatic void cx_stop (struct tty *tp, int flag);
205123120Simpstatic void cx_receive (cx_chan_t *c, char *data, int len);
206123120Simpstatic void cx_transmit (cx_chan_t *c, void *attachment, int len);
207123120Simpstatic void cx_error (cx_chan_t *c, int data);
208123120Simpstatic void cx_modem (cx_chan_t *c);
209123120Simpstatic void cx_up (drv_t *d);
210123120Simpstatic void cx_start (drv_t *d);
211123120Simpstatic void cx_softintr (void *);
212123120Simpstatic void *cx_fast_ih;
213123120Simpstatic void cx_down (drv_t *d);
214123120Simpstatic void cx_watchdog (drv_t *d);
215123120Simpstatic void cx_carrier (void *arg);
216123120Simp
217123120Simp#ifdef NETGRAPH
218123120Simpextern struct ng_type typestruct;
219123120Simp#else
220123120Simpstatic void cx_ifstart (struct ifnet *ifp);
221123120Simpstatic void cx_tlf (struct sppp *sp);
222123120Simpstatic void cx_tls (struct sppp *sp);
223123120Simpstatic void cx_ifwatchdog (struct ifnet *ifp);
224123120Simpstatic int cx_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data);
225123120Simpstatic void cx_initialize (void *softc);
226123120Simp#endif
227123120Simp
228123120Simpstatic cx_board_t *adapter [NCX];
229123120Simpstatic drv_t *channel [NCX*NCHAN];
230138823Srikstatic struct callout led_timo [NCX];
231138823Srikstatic struct callout timeout_handle;
232123120Simp
233150622Srikstatic int cx_open (struct cdev *dev, int flag, int mode, struct thread *td);
234150622Srikstatic int cx_close (struct cdev *dev, int flag, int mode, struct thread *td);
235150622Srikstatic int cx_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td);
236149847Sobrienstatic struct cdevsw cx_cdevsw = {
237149847Sobrien	.d_version  = D_VERSION,
238149847Sobrien	.d_open     = cx_open,
239149847Sobrien	.d_close    = cx_close,
240149847Sobrien	.d_ioctl    = cx_ioctl,
241149847Sobrien	.d_name     = "cx",
242149847Sobrien	.d_flags    = D_TTY | D_NEEDGIANT,
243149847Sobrien};
244149847Sobrien
245150622Srikstatic int MY_SOFT_INTR;
246150622Srik
247123120Simp/*
248123120Simp * Print the mbuf chain, for debug purposes only.
249123120Simp */
250123120Simpstatic void printmbuf (struct mbuf *m)
251123120Simp{
252123120Simp	printf ("mbuf:");
253123120Simp	for (; m; m=m->m_next) {
254123120Simp		if (m->m_flags & M_PKTHDR)
255123120Simp			printf (" HDR %d:", m->m_pkthdr.len);
256123120Simp		if (m->m_flags & M_EXT)
257123120Simp			printf (" EXT:");
258123120Simp		printf (" %d", m->m_len);
259123120Simp	}
260123120Simp	printf ("\n");
261123120Simp}
262123120Simp
263123120Simp/*
264123120Simp * Make an mbuf from data.
265123120Simp */
266123120Simpstatic struct mbuf *makembuf (void *buf, u_int len)
267123120Simp{
268123120Simp	struct mbuf *m, *o, *p;
269123120Simp
270123120Simp	MGETHDR (m, M_DONTWAIT, MT_DATA);
271123120Simp
272123120Simp	if (! m)
273123120Simp		return 0;
274123120Simp
275123120Simp	if (len >= MINCLSIZE)
276123120Simp		MCLGET (m, M_DONTWAIT);
277123120Simp
278123120Simp	m->m_pkthdr.len = len;
279123120Simp	m->m_len = 0;
280123120Simp
281123120Simp	p = m;
282123120Simp	while (len) {
283123120Simp		u_int n = M_TRAILINGSPACE (p);
284123120Simp		if (n > len)
285123120Simp			n = len;
286123120Simp		if (! n) {
287123120Simp			/* Allocate new mbuf. */
288123120Simp			o = p;
289123120Simp			MGET (p, M_DONTWAIT, MT_DATA);
290123120Simp			if (! p) {
291123120Simp				m_freem (m);
292123120Simp				return 0;
293123120Simp			}
294123120Simp			if (len >= MINCLSIZE)
295123120Simp				MCLGET (p, M_DONTWAIT);
296123120Simp			p->m_len = 0;
297123120Simp			o->m_next = p;
298123120Simp
299123120Simp			n = M_TRAILINGSPACE (p);
300123120Simp			if (n > len)
301123120Simp				n = len;
302123120Simp		}
303123120Simp		bcopy (buf, mtod (p, caddr_t) + p->m_len, n);
304123120Simp
305123120Simp		p->m_len += n;
306123120Simp		buf = n + (char*) buf;
307123120Simp		len -= n;
308123120Simp	}
309123120Simp	return m;
310123120Simp}
311123120Simp
312123120Simp/*
313123120Simp * Recover after lost transmit interrupts.
314123120Simp */
315123120Simpstatic void cx_timeout (void *arg)
316123120Simp{
317123120Simp	drv_t *d;
318138823Srik	int s, i, k;
319123120Simp
320138823Srik	for (i = 0; i < NCX; i++) {
321138823Srik		if (adapter[i] == NULL)
322123120Simp			continue;
323138823Srik		for (k = 0; k < NCHAN; ++k) {
324138823Srik			d = channel[i * NCHAN + k];
325138823Srik			if (! d)
326138823Srik				continue;
327138823Srik			s = splhigh ();
328138823Srik			CX_LOCK ((bdrv_t *)d->board->sys);
329138823Srik			if (d->atimeout == 1 && d->tty && d->tty->t_state & TS_BUSY) {
330138823Srik				d->tty->t_state &= ~TS_BUSY;
331138823Srik				if (d->tty->t_dev) {
332138823Srik					d->intr_action |= CX_WRITE;
333138823Srik					MY_SOFT_INTR = 1;
334138823Srik					swi_sched (cx_fast_ih, 0);
335138823Srik				}
336138823Srik				CX_DEBUG (d, ("cx_timeout\n"));
337123120Simp			}
338138823Srik			if (d->atimeout)
339138823Srik				d->atimeout--;
340138823Srik			CX_UNLOCK ((bdrv_t *)d->board->sys);
341138823Srik			splx (s);
342123120Simp		}
343123120Simp	}
344138823Srik	callout_reset (&timeout_handle, hz*5, cx_timeout, 0);
345123120Simp}
346123120Simp
347123120Simpstatic void cx_led_off (void *arg)
348123120Simp{
349123120Simp	cx_board_t *b = arg;
350138823Srik	bdrv_t *bd = b->sys;
351138823Srik	int s;
352123120Simp
353138823Srik	s = splhigh ();
354138823Srik	CX_LOCK (bd);
355123120Simp	cx_led (b, 0);
356138823Srik	CX_UNLOCK (bd);
357123120Simp	splx (s);
358123120Simp}
359123120Simp
360123120Simp/*
361123120Simp * Activate interupt handler from DDK.
362123120Simp */
363123120Simpstatic void cx_intr (void *arg)
364123120Simp{
365123120Simp	bdrv_t *bd = arg;
366123120Simp	cx_board_t *b = bd->board;
367138823Srik#ifndef NETGRAPH
368138823Srik	int i;
369138823Srik#endif
370123120Simp	int s = splhigh ();
371123120Simp
372138823Srik	CX_LOCK (bd);
373123120Simp	/* Turn LED on. */
374123120Simp	cx_led (b, 1);
375123120Simp
376123120Simp	cx_int_handler (b);
377123120Simp
378123120Simp	/* Turn LED off 50 msec later. */
379138823Srik	callout_reset (&led_timo[b->num], hz/20, cx_led_off, b);
380138823Srik	CX_UNLOCK (bd);
381123120Simp	splx (s);
382138823Srik
383138823Srik#ifndef NETGRAPH
384138823Srik	/* Pass packets in a lock-free state */
385138823Srik	for (i = 0; i < NCHAN && b->chan[i].type; i++) {
386138823Srik		drv_t *d = b->chan[i].sys;
387138823Srik		struct mbuf *m;
388147858Srik		if (!d || !d->running)
389147858Srik			continue;
390138823Srik		while (_IF_QLEN(&d->queue)) {
391138823Srik			IF_DEQUEUE (&d->queue,m);
392138823Srik			if (!m)
393138823Srik				continue;
394147256Sbrooks			sppp_input (d->ifp, m);
395138823Srik		}
396138823Srik	}
397138823Srik#endif
398123120Simp}
399123120Simp
400123120Simpstatic int probe_irq (cx_board_t *b, int irq)
401123120Simp{
402123120Simp	int mask, busy, cnt;
403123120Simp
404123120Simp	/* Clear pending irq, if any. */
405123120Simp	cx_probe_irq (b, -irq);
406123120Simp	DELAY (100);
407123120Simp	for (cnt=0; cnt<5; ++cnt) {
408123120Simp		/* Get the mask of pending irqs, assuming they are busy.
409123120Simp		 * Activate the adapter on given irq. */
410123120Simp		busy = cx_probe_irq (b, irq);
411123120Simp		DELAY (100);
412123120Simp
413123120Simp		/* Get the mask of active irqs.
414123120Simp		 * Deactivate our irq. */
415123120Simp		mask = cx_probe_irq (b, -irq);
416123120Simp		DELAY (100);
417123120Simp		if ((mask & ~busy) == 1 << irq) {
418123120Simp			cx_probe_irq (b, 0);
419123120Simp			/* printf ("cx%d: irq %d ok, mask=0x%04x, busy=0x%04x\n",
420123120Simp				b->num, irq, mask, busy); */
421123120Simp			return 1;
422123120Simp		}
423123120Simp	}
424123120Simp	/* printf ("cx%d: irq %d not functional, mask=0x%04x, busy=0x%04x\n",
425123120Simp		b->num, irq, mask, busy); */
426123120Simp	cx_probe_irq (b, 0);
427123120Simp	return 0;
428123120Simp}
429123120Simp
430123120Simpstatic short porttab [] = {
431123120Simp	0x200, 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0,
432123120Simp	0x300, 0x320, 0x340, 0x360, 0x380, 0x3a0, 0x3c0, 0x3e0, 0
433123120Simp};
434123120Simpstatic char dmatab [] = { 7, 6, 5, 0 };
435123120Simpstatic char irqtab [] = { 5, 10, 11, 7, 3, 15, 12, 0 };
436123120Simp
437123120Simpstatic int cx_is_free_res (device_t dev, int rid, int type, u_long start,
438123120Simp	u_long end, u_long count)
439123120Simp{
440123120Simp	struct resource *res;
441123120Simp
442123120Simp	if (!(res = bus_alloc_resource (dev, type, &rid, start, end, count,
443123120Simp	    RF_ALLOCATED)))
444123120Simp		return 0;
445123120Simp
446123120Simp	bus_release_resource (dev, type, rid, res);
447123120Simp
448123120Simp	return 1;
449123120Simp}
450123120Simp
451123120Simpstatic void cx_identify (driver_t *driver, device_t dev)
452123120Simp{
453123120Simp	u_long iobase, rescount;
454123120Simp	int devcount;
455123120Simp	device_t *devices;
456123120Simp	device_t child;
457123120Simp	devclass_t my_devclass;
458123120Simp	int i, k;
459123120Simp
460123120Simp	if ((my_devclass = devclass_find ("cx")) == NULL)
461123120Simp		return;
462123120Simp
463123120Simp	devclass_get_devices (my_devclass, &devices, &devcount);
464123120Simp
465123120Simp	if (devcount == 0) {
466123120Simp		/* We should find all devices by our self. We could alter other
467123120Simp		 * devices, but we don't have a choise
468123120Simp		 */
469123120Simp		for (i = 0; (iobase = porttab [i]) != 0; i++) {
470133647Srik			if (!cx_is_free_res (dev, 0, SYS_RES_IOPORT,
471123120Simp			    iobase, iobase + NPORT, NPORT))
472123120Simp				continue;
473123120Simp			if (cx_probe_board (iobase, -1, -1) == 0)
474123120Simp				continue;
475123120Simp
476123120Simp			devcount++;
477123120Simp
478123120Simp			child = BUS_ADD_CHILD (dev, ISA_ORDER_SPECULATIVE, "cx",
479123120Simp			    -1);
480123120Simp
481123120Simp			if (child == NULL)
482123120Simp				return;
483123120Simp
484123120Simp			device_set_desc_copy (child, "Cronyx Sigma");
485123120Simp			device_set_driver (child, driver);
486123120Simp			bus_set_resource (child, SYS_RES_IOPORT, 0,
487123120Simp			    iobase, NPORT);
488123120Simp
489123120Simp			if (devcount >= NCX)
490123120Simp				break;
491123120Simp		}
492123120Simp	} else {
493123120Simp		static short porttab [] = {
494123120Simp			0x200, 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0,
495123120Simp			0x300, 0x320, 0x340, 0x360, 0x380, 0x3a0, 0x3c0, 0x3e0, 0
496123120Simp		};
497123120Simp		/* Lets check user choise.
498123120Simp		 */
499123120Simp		for (k = 0; k < devcount; k++) {
500123120Simp			if (bus_get_resource (devices[k], SYS_RES_IOPORT, 0,
501123120Simp			    &iobase, &rescount) != 0)
502123120Simp				continue;
503123120Simp
504123120Simp			for (i = 0; porttab [i] != 0; i++) {
505123120Simp				if (porttab [i] != iobase)
506123120Simp					continue;
507133647Srik				if (!cx_is_free_res (devices[k], 0, SYS_RES_IOPORT,
508123120Simp				    iobase, iobase + NPORT, NPORT))
509123120Simp					continue;
510123120Simp				if (cx_probe_board (iobase, -1, -1) == 0)
511123120Simp					continue;
512123120Simp				porttab [i] = -1;
513123120Simp				device_set_desc_copy (devices[k], "Cronyx Sigma");
514123120Simp				break;
515123120Simp			}
516123120Simp
517123120Simp			if (porttab [i] == 0) {
518123120Simp				device_delete_child (
519123120Simp				    device_get_parent (devices[k]),
520123120Simp				    devices [k]);
521123120Simp				devices[k] = 0;
522123120Simp				continue;
523123120Simp			}
524123120Simp		}
525123120Simp		for (k = 0; k < devcount; k++) {
526123120Simp			if (devices[k] == 0)
527123120Simp				continue;
528123120Simp			if (bus_get_resource (devices[k], SYS_RES_IOPORT, 0,
529123120Simp			    &iobase, &rescount) == 0)
530123120Simp				continue;
531123120Simp			for (i = 0; (iobase = porttab [i]) != 0; i++) {
532123120Simp				if (porttab [i] == -1) {
533123120Simp					continue;
534123120Simp				}
535133647Srik				if (!cx_is_free_res (devices[k], 0, SYS_RES_IOPORT,
536123120Simp				    iobase, iobase + NPORT, NPORT))
537123120Simp					continue;
538123120Simp				if (cx_probe_board (iobase, -1, -1) == 0)
539123120Simp					continue;
540123120Simp
541123120Simp				bus_set_resource (devices[k], SYS_RES_IOPORT, 0,
542123120Simp				    iobase, NPORT);
543123120Simp				porttab [i] = -1;
544123120Simp				device_set_desc_copy (devices[k], "Cronyx Sigma");
545123120Simp				break;
546123120Simp			}
547123120Simp			if (porttab [i] == 0) {
548123120Simp				device_delete_child (
549123120Simp				    device_get_parent (devices[k]),
550123120Simp				    devices [k]);
551123120Simp			}
552123120Simp		}
553123120Simp		free (devices, M_TEMP);
554123120Simp	}
555123120Simp
556123120Simp	return;
557123120Simp}
558123120Simp
559123120Simpstatic int cx_probe (device_t dev)
560123120Simp{
561123120Simp	int unit = device_get_unit (dev);
562123120Simp	int i;
563123120Simp	u_long iobase, rescount;
564123120Simp
565123120Simp	if (!device_get_desc (dev) ||
566123120Simp	    strcmp (device_get_desc (dev), "Cronyx Sigma"))
567123120Simp		return ENXIO;
568123120Simp
569123120Simp	if (bus_get_resource (dev, SYS_RES_IOPORT, 0, &iobase, &rescount) != 0) {
570123120Simp		printf ("cx%d: Couldn't get IOPORT\n", unit);
571123120Simp		return ENXIO;
572123120Simp	}
573123120Simp
574133647Srik	if (!cx_is_free_res (dev, 0, SYS_RES_IOPORT,
575123120Simp	    iobase, iobase + NPORT, NPORT)) {
576123120Simp		printf ("cx%d: Resource IOPORT isn't free %lx\n", unit, iobase);
577123120Simp		return ENXIO;
578123120Simp	}
579123120Simp
580123120Simp	for (i = 0; porttab [i] != 0; i++) {
581123120Simp		if (porttab [i] == iobase) {
582123120Simp			porttab [i] = -1;
583123120Simp			break;
584123120Simp		}
585123120Simp	}
586123120Simp
587123120Simp	if (porttab [i] == 0) {
588123120Simp		return ENXIO;
589123120Simp	}
590123120Simp
591123120Simp	if (!cx_probe_board (iobase, -1, -1)) {
592123120Simp		printf ("cx%d: probing for Sigma at %lx faild\n", unit, iobase);
593123120Simp		return ENXIO;
594123120Simp	}
595123120Simp
596123120Simp	return 0;
597123120Simp}
598123120Simp
599130985Srikstatic void
600130985Srikcx_bus_dmamap_addr (void *arg, bus_dma_segment_t *segs, int nseg, int error)
601130985Srik{
602130985Srik	unsigned long *addr;
603130985Srik
604130985Srik	if (error)
605130985Srik		return;
606130985Srik
607130985Srik	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
608130985Srik	addr = arg;
609130985Srik	*addr = segs->ds_addr;
610130985Srik}
611130985Srik
612130985Srikstatic int
613130985Srikcx_bus_dma_mem_alloc (int bnum, int cnum, cx_dma_mem_t *dmem)
614130985Srik{
615130985Srik	int error;
616130985Srik
617130985Srik	error = bus_dma_tag_create (NULL, 16, 0, BUS_SPACE_MAXADDR_24BIT,
618130985Srik		BUS_SPACE_MAXADDR, NULL, NULL, dmem->size, 1,
619130985Srik		dmem->size, 0, NULL, NULL, &dmem->dmat);
620130985Srik	if (error) {
621130985Srik		if (cnum >= 0)	printf ("cx%d-%d: ", bnum, cnum);
622130985Srik		else		printf ("cx%d: ", bnum);
623130985Srik		printf ("couldn't allocate tag for dma memory\n");
624130985Srik 		return 0;
625130985Srik	}
626130985Srik	error = bus_dmamem_alloc (dmem->dmat, (void **)&dmem->virt,
627130985Srik		BUS_DMA_NOWAIT | BUS_DMA_ZERO, &dmem->mapp);
628130985Srik	if (error) {
629130985Srik		if (cnum >= 0)	printf ("cx%d-%d: ", bnum, cnum);
630130985Srik		else		printf ("cx%d: ", bnum);
631130985Srik		printf ("couldn't allocate mem for dma memory\n");
632130985Srik		bus_dma_tag_destroy (dmem->dmat);
633130985Srik 		return 0;
634130985Srik	}
635130985Srik	error = bus_dmamap_load (dmem->dmat, dmem->mapp, dmem->virt,
636130985Srik		dmem->size, cx_bus_dmamap_addr, &dmem->phys, 0);
637130985Srik	if (error) {
638130985Srik		if (cnum >= 0)	printf ("cx%d-%d: ", bnum, cnum);
639130985Srik		else		printf ("cx%d: ", bnum);
640130985Srik		printf ("couldn't load mem map for dma memory\n");
641130985Srik		bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
642130985Srik		bus_dma_tag_destroy (dmem->dmat);
643130985Srik 		return 0;
644130985Srik	}
645130985Srik	return 1;
646130985Srik}
647130985Srik
648130985Srikstatic void
649130985Srikcx_bus_dma_mem_free (cx_dma_mem_t *dmem)
650130985Srik{
651130985Srik	bus_dmamap_unload (dmem->dmat, dmem->mapp);
652130985Srik	bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
653130985Srik	bus_dma_tag_destroy (dmem->dmat);
654130985Srik}
655130985Srik
656123120Simp/*
657123120Simp * The adapter is present, initialize the driver structures.
658123120Simp */
659123120Simpstatic int cx_attach (device_t dev)
660123120Simp{
661123120Simp	bdrv_t *bd = device_get_softc (dev);
662123120Simp	u_long iobase, drq, irq, rescount;
663123120Simp	int unit = device_get_unit (dev);
664138823Srik	char *cx_ln = CX_LOCK_NAME;
665123120Simp	cx_board_t *b;
666123120Simp	cx_chan_t *c;
667123120Simp	drv_t *d;
668130971Srik	int i;
669130971Srik	int s;
670123120Simp
671123120Simp	KASSERT ((bd != NULL), ("cx%d: NULL device softc\n", unit));
672123120Simp
673123120Simp	bus_get_resource (dev, SYS_RES_IOPORT, 0, &iobase, &rescount);
674123120Simp	bd->base_rid = 0;
675123120Simp	bd->base_res = bus_alloc_resource (dev, SYS_RES_IOPORT, &bd->base_rid,
676123120Simp		iobase, iobase + NPORT, NPORT, RF_ACTIVE);
677123120Simp	if (! bd->base_res) {
678123120Simp		printf ("cx%d: cannot allocate base address\n", unit);
679123120Simp		return ENXIO;
680123120Simp	}
681123120Simp
682123120Simp	if (bus_get_resource (dev, SYS_RES_DRQ, 0, &drq, &rescount) != 0) {
683123120Simp		for (i = 0; (drq = dmatab [i]) != 0; i++) {
684133647Srik			if (!cx_is_free_res (dev, 0, SYS_RES_DRQ,
685123120Simp			    drq, drq + 1, 1))
686123120Simp				continue;
687123120Simp			bus_set_resource (dev, SYS_RES_DRQ, 0, drq, 1);
688123120Simp			break;
689123120Simp		}
690123120Simp
691123120Simp		if (dmatab[i] == 0) {
692123120Simp			bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
693123120Simp				bd->base_res);
694123120Simp			printf ("cx%d: Couldn't get DRQ\n", unit);
695123120Simp			return ENXIO;
696123120Simp		}
697123120Simp	}
698123120Simp
699123120Simp	bd->drq_rid = 0;
700123120Simp	bd->drq_res = bus_alloc_resource (dev, SYS_RES_DRQ, &bd->drq_rid,
701123120Simp		drq, drq + 1, 1, RF_ACTIVE);
702123120Simp	if (! bd->drq_res) {
703123120Simp		printf ("cx%d: cannot allocate drq\n", unit);
704123120Simp		bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
705123120Simp			bd->base_res);
706123120Simp		return ENXIO;
707123120Simp	}
708123120Simp
709123120Simp	if (bus_get_resource (dev, SYS_RES_IRQ, 0, &irq, &rescount) != 0) {
710123120Simp		for (i = 0; (irq = irqtab [i]) != 0; i++) {
711133647Srik			if (!cx_is_free_res (dev, 0, SYS_RES_IRQ,
712123120Simp			    irq, irq + 1, 1))
713123120Simp				continue;
714123120Simp			bus_set_resource (dev, SYS_RES_IRQ, 0, irq, 1);
715123120Simp			break;
716123120Simp		}
717123120Simp
718123120Simp		if (irqtab[i] == 0) {
719123120Simp			bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
720123120Simp				bd->drq_res);
721123120Simp			bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
722123120Simp				bd->base_res);
723123120Simp			printf ("cx%d: Couldn't get IRQ\n", unit);
724123120Simp			return ENXIO;
725123120Simp		}
726123120Simp	}
727123120Simp
728123120Simp	bd->irq_rid = 0;
729123120Simp	bd->irq_res = bus_alloc_resource (dev, SYS_RES_IRQ, &bd->irq_rid,
730123120Simp		irq, irq + 1, 1, RF_ACTIVE);
731123120Simp	if (! bd->irq_res) {
732123120Simp		printf ("cx%d: Couldn't allocate irq\n", unit);
733123120Simp		bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
734123120Simp			bd->drq_res);
735123120Simp		bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
736123120Simp			bd->base_res);
737123120Simp		return ENXIO;
738123120Simp	}
739123120Simp
740123120Simp	b = malloc (sizeof (cx_board_t), M_DEVBUF, M_WAITOK);
741123120Simp	if (!b) {
742123120Simp		printf ("cx:%d: Couldn't allocate memory\n", unit);
743123120Simp		return (ENXIO);
744123120Simp	}
745123120Simp	adapter[unit] = b;
746123120Simp	bzero (b, sizeof(cx_board_t));
747123120Simp
748123120Simp	if (! cx_open_board (b, unit, iobase, irq, drq)) {
749123120Simp		printf ("cx%d: error loading firmware\n", unit);
750123120Simp		free (b, M_DEVBUF);
751123120Simp		bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid,
752123120Simp			bd->irq_res);
753123120Simp		bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
754123120Simp			bd->drq_res);
755123120Simp		bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
756123120Simp			bd->base_res);
757123120Simp 		return ENXIO;
758123120Simp	}
759123120Simp
760123120Simp	bd->board = b;
761123120Simp
762138823Srik	cx_ln[2] = '0' + unit;
763138823Srik	mtx_init (&bd->cx_mtx, cx_ln, MTX_NETWORK_LOCK, MTX_DEF|MTX_RECURSE);
764123120Simp	if (! probe_irq (b, irq)) {
765123120Simp		printf ("cx%d: irq %ld not functional\n", unit, irq);
766123120Simp		bd->board = 0;
767123120Simp		adapter [unit] = 0;
768138823Srik		mtx_destroy (&bd->cx_mtx);
769123120Simp		free (b, M_DEVBUF);
770123120Simp		bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid,
771123120Simp			bd->irq_res);
772123120Simp		bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
773123120Simp			bd->drq_res);
774123120Simp		bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
775123120Simp			bd->base_res);
776123120Simp 		return ENXIO;
777123120Simp	}
778138823Srik	b->sys = bd;
779138823Srik	callout_init (&led_timo[b->num], cx_mpsafenet ? CALLOUT_MPSAFE : 0);
780123120Simp	s = splhigh ();
781138823Srik	if (bus_setup_intr (dev, bd->irq_res,
782138823Srik			   INTR_TYPE_NET|(cx_mpsafenet?INTR_MPSAFE:0),
783138823Srik			   cx_intr, bd, &bd->intrhand)) {
784123120Simp		printf ("cx%d: Can't setup irq %ld\n", unit, irq);
785123120Simp		bd->board = 0;
786138823Srik		b->sys = 0;
787123120Simp		adapter [unit] = 0;
788138823Srik		mtx_destroy (&bd->cx_mtx);
789123120Simp		free (b, M_DEVBUF);
790123120Simp		bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid,
791123120Simp			bd->irq_res);
792123120Simp		bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
793123120Simp			bd->drq_res);
794123120Simp		bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
795123120Simp			bd->base_res);
796123120Simp		splx (s);
797123120Simp 		return ENXIO;
798123120Simp	}
799123120Simp
800138823Srik	CX_LOCK (bd);
801123120Simp	cx_init (b, b->num, b->port, irq, drq);
802123120Simp	cx_setup_board (b, 0, 0, 0);
803138823Srik	CX_UNLOCK (bd);
804123120Simp
805123120Simp	printf ("cx%d: <Cronyx-Sigma-%s>\n", b->num, b->name);
806123120Simp
807123120Simp	for (c=b->chan; c<b->chan+NCHAN; ++c) {
808123120Simp		if (c->type == T_NONE)
809123120Simp			continue;
810130985Srik		d = &bd->channel[c->num];
811130985Srik		d->dmamem.size = sizeof(cx_buf_t);
812130985Srik		if (! cx_bus_dma_mem_alloc (unit, c->num, &d->dmamem))
813130985Srik			continue;
814123120Simp		d->board = b;
815123120Simp		d->chan = c;
816123120Simp		d->open_dev = 0;
817123120Simp		c->sys = d;
818138823Srik		channel [b->num*NCHAN + c->num] = d;
819138823Srik		sprintf (d->name, "cx%d.%d", b->num, c->num);
820123120Simp
821123120Simp		switch (c->type) {
822123120Simp		case T_SYNC_RS232:
823123120Simp		case T_SYNC_V35:
824123120Simp		case T_SYNC_RS449:
825123120Simp		case T_UNIV:
826123120Simp		case T_UNIV_RS232:
827123120Simp		case T_UNIV_RS449:
828123120Simp		case T_UNIV_V35:
829123120Simp#ifdef NETGRAPH
830123120Simp		if (ng_make_node_common (&typestruct, &d->node) != 0) {
831123120Simp			printf ("%s: cannot make common node\n", d->name);
832123120Simp			channel [b->num*NCHAN + c->num] = 0;
833123120Simp			c->sys = 0;
834130985Srik			cx_bus_dma_mem_free (&d->dmamem);
835123120Simp			continue;
836123120Simp		}
837123120Simp		NG_NODE_SET_PRIVATE (d->node, d);
838123120Simp		sprintf (d->nodename, "%s%d", NG_CX_NODE_TYPE,
839123120Simp			 c->board->num*NCHAN + c->num);
840123120Simp		if (ng_name_node (d->node, d->nodename)) {
841123120Simp			printf ("%s: cannot name node\n", d->nodename);
842123120Simp			NG_NODE_UNREF (d->node);
843123120Simp			channel [b->num*NCHAN + c->num] = 0;
844123120Simp			c->sys = 0;
845130985Srik			cx_bus_dma_mem_free (&d->dmamem);
846123120Simp			continue;
847123120Simp		}
848123120Simp		d->lo_queue.ifq_maxlen = IFQ_MAXLEN;
849123120Simp		d->hi_queue.ifq_maxlen = IFQ_MAXLEN;
850123120Simp		mtx_init (&d->lo_queue.ifq_mtx, "cx_queue_lo", NULL, MTX_DEF);
851123120Simp		mtx_init (&d->hi_queue.ifq_mtx, "cx_queue_hi", NULL, MTX_DEF);
852138823Srik		callout_init (&d->timeout_handle,
853138823Srik			     cx_mpsafenet ? CALLOUT_MPSAFE : 0);
854123120Simp#else /*NETGRAPH*/
855147256Sbrooks		d->ifp = if_alloc(IFT_PPP);
856147256Sbrooks		if (d->ifp == NULL) {
857147256Sbrooks			printf ("%s: cannot if_alloc() common interface\n",
858147256Sbrooks			    d->name);
859147256Sbrooks			channel [b->num*NCHAN + c->num] = 0;
860147256Sbrooks			c->sys = 0;
861147256Sbrooks			cx_bus_dma_mem_free (&d->dmamem);
862147256Sbrooks			continue;
863147256Sbrooks		}
864147856Srik		d->ifp->if_softc	= d;
865147256Sbrooks		if_initname (d->ifp, "cx", b->num * NCHAN + c->num);
866147856Srik		d->ifp->if_mtu		= PP_MTU;
867147861Srik		d->ifp->if_flags	= IFF_POINTOPOINT | IFF_MULTICAST;
868147861Srik		if (!cx_mpsafenet)
869147861Srik			d->ifp->if_flags |= IFF_NEEDSGIANT;
870147256Sbrooks		d->ifp->if_ioctl	= cx_sioctl;
871147256Sbrooks		d->ifp->if_start	= cx_ifstart;
872147256Sbrooks		d->ifp->if_watchdog	= cx_ifwatchdog;
873147856Srik		d->ifp->if_init		= cx_initialize;
874138823Srik		d->queue.ifq_maxlen	= 2;
875138823Srik		mtx_init (&d->queue.ifq_mtx, "cx_queue", NULL, MTX_DEF);
876147256Sbrooks		sppp_attach (d->ifp);
877147256Sbrooks		if_attach (d->ifp);
878147856Srik		IFP2SP(d->ifp)->pp_tlf	= cx_tlf;
879147856Srik		IFP2SP(d->ifp)->pp_tls	= cx_tls;
880123120Simp		/* If BPF is in the kernel, call the attach for it.
881123120Simp		 * Size of PPP header is 4 bytes. */
882147256Sbrooks		bpfattach (d->ifp, DLT_PPP, 4);
883123120Simp#endif /*NETGRAPH*/
884123120Simp		}
885136480Sphk		d->tty = ttyalloc ();
886147856Srik		d->tty->t_open	= cx_topen;
887147856Srik		d->tty->t_close	= cx_tclose;
888147856Srik		d->tty->t_param	= cx_param;
889147856Srik		d->tty->t_stop	= cx_stop;
890147856Srik		d->tty->t_modem	= cx_tmodem;
891147860Srik		d->tty->t_oproc	= cx_oproc;
892147856Srik		d->tty->t_sc	= d;
893138823Srik		CX_LOCK (bd);
894130985Srik		cx_start_chan (c, d->dmamem.virt, d->dmamem.phys);
895123120Simp		cx_register_receive (c, &cx_receive);
896123120Simp		cx_register_transmit (c, &cx_transmit);
897123120Simp		cx_register_error (c, &cx_error);
898123120Simp		cx_register_modem (c, &cx_modem);
899138823Srik		CX_UNLOCK (bd);
900136480Sphk
901151383Sphk		ttycreate(d->tty, TS_CALLOUT, "x%r%r", b->num, c->num);
902136480Sphk		d->devt = make_dev (&cx_cdevsw, b->num*NCHAN + c->num + 64, UID_ROOT, GID_WHEEL, 0600, "cx%d", b->num*NCHAN + c->num);
903136480Sphk		d->devt->si_drv1 = d;
904138823Srik		callout_init (&d->dcd_timeout_handle,
905138823Srik			     cx_mpsafenet ? CALLOUT_MPSAFE : 0);
906123120Simp	}
907123120Simp	splx (s);
908123120Simp
909123120Simp	return 0;
910123120Simp}
911123120Simp
912123120Simpstatic int cx_detach (device_t dev)
913123120Simp{
914123120Simp	bdrv_t *bd = device_get_softc (dev);
915123120Simp	cx_board_t *b = bd->board;
916123120Simp	cx_chan_t *c;
917138823Srik	int s;
918123120Simp
919138823Srik	KASSERT (mtx_initialized (&bd->cx_mtx), ("cx mutex not initialized"));
920138823Srik
921138823Srik	s = splhigh ();
922138823Srik	CX_LOCK (bd);
923123120Simp	/* Check if the device is busy (open). */
924123120Simp	for (c = b->chan; c < b->chan + NCHAN; ++c) {
925123120Simp		drv_t *d = (drv_t*) c->sys;
926123120Simp
927123120Simp		if (!d || d->chan->type == T_NONE)
928123120Simp			continue;
929123120Simp		if (d->lock) {
930138823Srik			CX_UNLOCK (bd);
931123120Simp			splx (s);
932123120Simp			return EBUSY;
933123120Simp		}
934130240Srik		if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN) &&
935123120Simp		    (d->open_dev|0x2)) {
936138823Srik			CX_UNLOCK (bd);
937123120Simp			splx (s);
938123120Simp			return EBUSY;
939123120Simp		}
940123120Simp		if (d->running) {
941138823Srik			CX_UNLOCK (bd);
942123120Simp			splx (s);
943123120Simp			return EBUSY;
944123120Simp		}
945123120Simp	}
946123120Simp
947123120Simp	/* Deactivate the timeout routine. And soft interrupt*/
948138823Srik	callout_stop (&led_timo[b->num]);
949123120Simp
950123120Simp	for (c = b->chan; c < b->chan + NCHAN; ++c) {
951123120Simp		drv_t *d = c->sys;
952123120Simp
953123120Simp		if (!d || d->chan->type == T_NONE)
954123120Simp			continue;
955123120Simp
956138823Srik		callout_stop (&d->dcd_timeout_handle);
957123120Simp	}
958138823Srik	CX_UNLOCK (bd);
959123120Simp	bus_teardown_intr (dev, bd->irq_res, bd->intrhand);
960123120Simp	bus_deactivate_resource (dev, SYS_RES_IRQ, bd->irq_rid, bd->irq_res);
961123120Simp	bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid, bd->irq_res);
962123120Simp
963123120Simp	bus_deactivate_resource (dev, SYS_RES_DRQ, bd->drq_rid, bd->drq_res);
964123120Simp	bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid, bd->drq_res);
965123120Simp
966123120Simp	bus_deactivate_resource (dev, SYS_RES_IOPORT, bd->base_rid, bd->irq_res);
967123120Simp	bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid, bd->base_res);
968123120Simp
969138823Srik	CX_LOCK (bd);
970123120Simp	cx_close_board (b);
971123120Simp
972123120Simp	/* Detach the interfaces, free buffer memory. */
973123120Simp	for (c = b->chan; c < b->chan + NCHAN; ++c) {
974123120Simp		drv_t *d = (drv_t*) c->sys;
975123120Simp
976123120Simp		if (!d || d->chan->type == T_NONE)
977123120Simp			continue;
978130301Srik
979130301Srik		if (d->tty) {
980136480Sphk			ttyfree (d->tty);
981130301Srik			d->tty = NULL;
982130301Srik		}
983130301Srik
984123120Simp#ifdef NETGRAPH
985123120Simp		if (d->node) {
986123120Simp			ng_rmnode_self (d->node);
987123120Simp			NG_NODE_UNREF (d->node);
988123120Simp			d->node = NULL;
989123120Simp		}
990123120Simp		mtx_destroy (&d->lo_queue.ifq_mtx);
991123120Simp		mtx_destroy (&d->hi_queue.ifq_mtx);
992123120Simp#else
993123120Simp		/* Detach from the packet filter list of interfaces. */
994147256Sbrooks		bpfdetach (d->ifp);
995123120Simp		/* Detach from the sync PPP list. */
996147256Sbrooks		sppp_detach (d->ifp);
997123120Simp
998147256Sbrooks		if_detach (d->ifp);
999147256Sbrooks		if_free(d->ifp);
1000138823Srik		/* XXXRIK: check interconnection with irq handler */
1001138823Srik		IF_DRAIN (&d->queue);
1002138823Srik		mtx_destroy (&d->queue.ifq_mtx);
1003123120Simp#endif
1004136480Sphk		destroy_dev (d->devt);
1005123120Simp	}
1006123120Simp
1007123120Simp	cx_led_off (b);
1008138823Srik	CX_UNLOCK (bd);
1009138823Srik	callout_drain (&led_timo[b->num]);
1010138823Srik	for (c = b->chan; c < b->chan + NCHAN; ++c) {
1011138823Srik		drv_t *d = c->sys;
1012138823Srik
1013138823Srik		if (!d || d->chan->type == T_NONE)
1014138823Srik			continue;
1015138823Srik
1016138823Srik		callout_drain (&d->dcd_timeout_handle);
1017138823Srik	}
1018123120Simp	splx (s);
1019123120Simp
1020123120Simp	s = splhigh ();
1021123120Simp	for (c = b->chan; c < b->chan + NCHAN; ++c) {
1022123120Simp		drv_t *d = (drv_t*) c->sys;
1023123120Simp
1024123120Simp		if (!d || d->chan->type == T_NONE)
1025123120Simp			continue;
1026123120Simp
1027123120Simp		/* Deallocate buffers. */
1028130985Srik		cx_bus_dma_mem_free (&d->dmamem);
1029123120Simp	}
1030123120Simp	bd->board = 0;
1031123120Simp	adapter [b->num] = 0;
1032123120Simp	free (b, M_DEVBUF);
1033123120Simp	splx (s);
1034138823Srik
1035138823Srik	mtx_destroy (&bd->cx_mtx);
1036123120Simp
1037123120Simp	return 0;
1038123120Simp}
1039123120Simp
1040123120Simp#ifndef NETGRAPH
1041123120Simpstatic void cx_ifstart (struct ifnet *ifp)
1042123120Simp{
1043133644Srik	drv_t *d = ifp->if_softc;
1044138823Srik	bdrv_t *bd = d->board->sys;
1045123120Simp
1046138823Srik	CX_LOCK (bd);
1047123120Simp	cx_start (d);
1048138823Srik	CX_UNLOCK (bd);
1049123120Simp}
1050123120Simp
1051123120Simpstatic void cx_ifwatchdog (struct ifnet *ifp)
1052123120Simp{
1053133644Srik	drv_t *d = ifp->if_softc;
1054123120Simp
1055123120Simp	cx_watchdog (d);
1056123120Simp}
1057123120Simp
1058123120Simpstatic void cx_tlf (struct sppp *sp)
1059123120Simp{
1060147256Sbrooks	drv_t *d = SP2IFP(sp)->if_softc;
1061123120Simp
1062123120Simp	CX_DEBUG (d, ("cx_tlf\n"));
1063123120Simp/*	cx_set_dtr (d->chan, 0);*/
1064123120Simp/*	cx_set_rts (d->chan, 0);*/
1065147256Sbrooks	if (!(IFP2SP(d->ifp)->pp_flags & PP_FR) && !(d->ifp->if_flags & PP_CISCO))
1066138352Srik		sp->pp_down (sp);
1067123120Simp}
1068123120Simp
1069123120Simpstatic void cx_tls (struct sppp *sp)
1070123120Simp{
1071147256Sbrooks	drv_t *d = SP2IFP(sp)->if_softc;
1072123120Simp
1073123120Simp	CX_DEBUG (d, ("cx_tls\n"));
1074147256Sbrooks	if (!(IFP2SP(d->ifp)->pp_flags & PP_FR) && !(d->ifp->if_flags & PP_CISCO))
1075138352Srik		sp->pp_up (sp);
1076123120Simp}
1077123120Simp
1078123120Simp/*
1079123120Simp * Initialization of interface.
1080123120Simp * It seems to be never called by upper level.
1081123120Simp */
1082123120Simpstatic void cx_initialize (void *softc)
1083123120Simp{
1084123120Simp	drv_t *d = softc;
1085123120Simp
1086123120Simp	CX_DEBUG (d, ("cx_initialize\n"));
1087123120Simp}
1088123120Simp
1089123120Simp/*
1090123120Simp * Process an ioctl request.
1091123120Simp */
1092123120Simpstatic int cx_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data)
1093123120Simp{
1094123120Simp	drv_t *d = ifp->if_softc;
1095138823Srik	bdrv_t *bd = d->board->sys;
1096123120Simp	int error, s, was_up, should_be_up;
1097123120Simp
1098123120Simp	/* No socket ioctls while the channel is in async mode. */
1099123120Simp	if (d->chan->type == T_NONE || d->chan->mode == M_ASYNC)
1100123120Simp		return EBUSY;
1101123120Simp
1102123120Simp	/* Socket ioctls on slave subchannels are not allowed. */
1103148887Srwatson	was_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
1104123120Simp	error = sppp_ioctl (ifp, cmd, data);
1105123120Simp	if (error)
1106123120Simp		return error;
1107123120Simp
1108123120Simp	if (! (ifp->if_flags & IFF_DEBUG))
1109123120Simp		d->chan->debug = 0;
1110123120Simp	else if (! d->chan->debug)
1111123120Simp		d->chan->debug = 1;
1112123120Simp
1113123120Simp	switch (cmd) {
1114133644Srik	default:	   CX_DEBUG2 (d, ("ioctl 0x%lx\n", cmd)); return 0;
1115123120Simp	case SIOCADDMULTI: CX_DEBUG2 (d, ("SIOCADDMULTI\n"));     return 0;
1116123120Simp	case SIOCDELMULTI: CX_DEBUG2 (d, ("SIOCDELMULTI\n"));     return 0;
1117123120Simp	case SIOCSIFFLAGS: CX_DEBUG2 (d, ("SIOCSIFFLAGS\n"));     break;
1118123120Simp	case SIOCSIFADDR:  CX_DEBUG2 (d, ("SIOCSIFADDR\n"));      break;
1119123120Simp	}
1120123120Simp
1121123120Simp	/* We get here only in case of SIFFLAGS or SIFADDR. */
1122123120Simp	s = splhigh ();
1123138823Srik	CX_LOCK (bd);
1124148887Srwatson	should_be_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
1125123120Simp	if (!was_up && should_be_up) {
1126123120Simp		/* Interface goes up -- start it. */
1127123120Simp		cx_up (d);
1128123120Simp		cx_start (d);
1129123120Simp	} else if (was_up && !should_be_up) {
1130123120Simp		/* Interface is going down -- stop it. */
1131147256Sbrooks		/* if ((IFP2SP(d->ifp)->pp_flags & PP_FR) || (ifp->if_flags & PP_CISCO))*/
1132123120Simp		cx_down (d);
1133123120Simp	}
1134138823Srik	CX_UNLOCK (bd);
1135123120Simp	splx (s);
1136123120Simp	return 0;
1137123120Simp}
1138123120Simp#endif /*NETGRAPH*/
1139123120Simp
1140123120Simp/*
1141123120Simp * Stop the interface.  Called on splimp().
1142123120Simp */
1143123120Simpstatic void cx_down (drv_t *d)
1144123120Simp{
1145123120Simp	int s = splhigh ();
1146123120Simp	CX_DEBUG (d, ("cx_down\n"));
1147123120Simp	cx_set_dtr (d->chan, 0);
1148123120Simp	cx_set_rts (d->chan, 0);
1149123120Simp	d->running = 0;
1150123120Simp	splx (s);
1151123120Simp}
1152123120Simp
1153123120Simp/*
1154123120Simp * Start the interface.  Called on splimp().
1155123120Simp */
1156123120Simpstatic void cx_up (drv_t *d)
1157123120Simp{
1158123120Simp	int s = splhigh ();
1159123120Simp	CX_DEBUG (d, ("cx_up\n"));
1160123120Simp	cx_set_dtr (d->chan, 1);
1161123120Simp	cx_set_rts (d->chan, 1);
1162123120Simp	d->running = 1;
1163123120Simp	splx (s);
1164123120Simp}
1165123120Simp
1166123120Simp/*
1167123120Simp * Start output on the (slave) interface.  Get another datagram to send
1168123120Simp * off of the interface queue, and copy it to the interface
1169123120Simp * before starting the output.
1170123120Simp */
1171123120Simpstatic void cx_send (drv_t *d)
1172123120Simp{
1173123120Simp	struct mbuf *m;
1174123120Simp	u_short len;
1175123120Simp
1176123120Simp	CX_DEBUG2 (d, ("cx_send\n"));
1177123120Simp
1178123120Simp	/* No output if the interface is down. */
1179123120Simp	if (! d->running)
1180123120Simp		return;
1181123120Simp
1182123120Simp	/* No output if the modem is off. */
1183123120Simp	if (! cx_get_dsr (d->chan) && ! cx_get_loop(d->chan))
1184123120Simp		return;
1185123120Simp
1186123120Simp	if (cx_buf_free (d->chan)) {
1187123120Simp		/* Get the packet to send. */
1188123120Simp#ifdef NETGRAPH
1189123120Simp		IF_DEQUEUE (&d->hi_queue, m);
1190123120Simp		if (! m)
1191123120Simp			IF_DEQUEUE (&d->lo_queue, m);
1192123120Simp#else
1193147256Sbrooks		m = sppp_dequeue (d->ifp);
1194123120Simp#endif
1195123120Simp		if (! m)
1196123120Simp			return;
1197130971Srik#ifndef NETGRAPH
1198165632Sjhb		BPF_MTAP (d->ifp, m);
1199123120Simp#endif
1200147862Srik		len = m_length (m, NULL);
1201123120Simp		if (! m->m_next)
1202123120Simp			cx_send_packet (d->chan, (u_char*)mtod (m, caddr_t),
1203123120Simp				len, 0);
1204123120Simp		else {
1205123120Simp			u_char buf [DMABUFSZ];
1206123120Simp			m_copydata (m, 0, len, buf);
1207123120Simp			cx_send_packet (d->chan, buf, len, 0);
1208123120Simp		}
1209123120Simp		m_freem (m);
1210123120Simp
1211123120Simp		/* Set up transmit timeout, 10 seconds. */
1212123120Simp#ifdef NETGRAPH
1213123120Simp		d->timeout = 10;
1214123120Simp#else
1215147256Sbrooks		d->ifp->if_timer = 10;
1216123120Simp#endif
1217123120Simp	}
1218123120Simp#ifndef NETGRAPH
1219148887Srwatson	d->ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1220123120Simp#endif
1221123120Simp}
1222123120Simp
1223123120Simp/*
1224123120Simp * Start output on the interface.
1225123120Simp * Always called on splimp().
1226123120Simp */
1227123120Simpstatic void cx_start (drv_t *d)
1228123120Simp{
1229123120Simp	int s = splhigh ();
1230123120Simp	if (d->running) {
1231123120Simp		if (! d->chan->dtr)
1232123120Simp			cx_set_dtr (d->chan, 1);
1233123120Simp		if (! d->chan->rts)
1234123120Simp			cx_set_rts (d->chan, 1);
1235123120Simp		cx_send (d);
1236123120Simp	}
1237123120Simp	splx (s);
1238123120Simp}
1239123120Simp
1240123120Simp/*
1241123120Simp * Handle transmit timeouts.
1242123120Simp * Recover after lost transmit interrupts.
1243123120Simp * Always called on splimp().
1244123120Simp */
1245123120Simpstatic void cx_watchdog (drv_t *d)
1246123120Simp{
1247138823Srik	bdrv_t *bd = d->board->sys;
1248138823Srik
1249123120Simp	int s = splhigh ();
1250138823Srik	CX_LOCK (bd);
1251123120Simp	CX_DEBUG (d, ("device timeout\n"));
1252123120Simp	if (d->running) {
1253123120Simp		cx_setup_chan (d->chan);
1254123120Simp		cx_start_chan (d->chan, 0, 0);
1255123120Simp		cx_set_dtr (d->chan, 1);
1256123120Simp		cx_set_rts (d->chan, 1);
1257123120Simp		cx_start (d);
1258123120Simp	}
1259138823Srik	CX_UNLOCK (bd);
1260123120Simp	splx (s);
1261123120Simp}
1262123120Simp
1263123120Simp/*
1264123120Simp * Transmit callback function.
1265123120Simp */
1266123120Simpstatic void cx_transmit (cx_chan_t *c, void *attachment, int len)
1267123120Simp{
1268123120Simp	drv_t *d = c->sys;
1269123120Simp
1270123120Simp	if (!d)
1271123120Simp		return;
1272123120Simp
1273130240Srik	if (c->mode == M_ASYNC && d->tty) {
1274130240Srik		d->tty->t_state &= ~(TS_BUSY | TS_FLUSH);
1275123120Simp		d->atimeout = 0;
1276130240Srik		if (d->tty->t_dev) {
1277123120Simp			d->intr_action |= CX_WRITE;
1278123120Simp			MY_SOFT_INTR = 1;
1279123120Simp			swi_sched (cx_fast_ih, 0);
1280123120Simp		}
1281123120Simp		return;
1282123120Simp	}
1283123120Simp#ifdef NETGRAPH
1284123120Simp	d->timeout = 0;
1285123120Simp#else
1286147256Sbrooks	++d->ifp->if_opackets;
1287148887Srwatson	d->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1288147256Sbrooks	d->ifp->if_timer = 0;
1289123120Simp#endif
1290123120Simp	cx_start (d);
1291123120Simp}
1292123120Simp
1293123120Simp/*
1294123120Simp * Process the received packet.
1295123120Simp */
1296123120Simpstatic void cx_receive (cx_chan_t *c, char *data, int len)
1297123120Simp{
1298123120Simp	drv_t *d = c->sys;
1299123120Simp	struct mbuf *m;
1300123120Simp	char *cc = data;
1301138823Srik#ifdef NETGRAPH
1302123120Simp	int error;
1303123120Simp#endif
1304123120Simp
1305123120Simp	if (!d)
1306123120Simp		return;
1307123120Simp
1308130240Srik	if (c->mode == M_ASYNC && d->tty) {
1309130240Srik		if (d->tty->t_state & TS_ISOPEN) {
1310123120Simp			async_q *q = &d->aqueue;
1311123120Simp			int size = BF_SZ - 1 - AQ_GSZ (q);
1312123120Simp
1313123120Simp			if (len <= 0 && !size)
1314123120Simp				return;
1315123120Simp
1316123120Simp			if (len > size) {
1317133644Srik				c->ierrs++;
1318123120Simp				cx_error (c, CX_OVERRUN);
1319123120Simp				len = size - 1;
1320123120Simp			}
1321123120Simp
1322123120Simp			while (len--) {
1323123120Simp				AQ_PUSH (q, *(unsigned char *)cc);
1324123120Simp				cc++;
1325123120Simp			}
1326123120Simp
1327123120Simp			d->intr_action |= CX_READ;
1328123120Simp			MY_SOFT_INTR = 1;
1329123120Simp			swi_sched (cx_fast_ih, 0);
1330123120Simp		}
1331123120Simp		return;
1332123120Simp	}
1333123120Simp	if (! d->running)
1334123120Simp		return;
1335123120Simp
1336123120Simp	m = makembuf (data, len);
1337123120Simp	if (! m) {
1338123120Simp		CX_DEBUG (d, ("no memory for packet\n"));
1339123120Simp#ifndef NETGRAPH
1340147256Sbrooks		++d->ifp->if_iqdrops;
1341123120Simp#endif
1342123120Simp		return;
1343123120Simp	}
1344123120Simp	if (c->debug > 1)
1345123120Simp		printmbuf (m);
1346123120Simp#ifdef NETGRAPH
1347123120Simp	m->m_pkthdr.rcvif = 0;
1348123120Simp	NG_SEND_DATA_ONLY (error, d->hook, m);
1349123120Simp#else
1350147256Sbrooks	++d->ifp->if_ipackets;
1351147256Sbrooks	m->m_pkthdr.rcvif = d->ifp;
1352123120Simp	/* Check if there's a BPF listener on this interface.
1353123120Simp	 * If so, hand off the raw packet to bpf. */
1354165632Sjhb	BPF_TAP (d->ifp, data, len);
1355138823Srik	IF_ENQUEUE (&d->queue, m);
1356123120Simp#endif
1357123120Simp}
1358123120Simp
1359123120Simp#define CONDITION(t,tp) (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))\
1360123120Simp	    && (!(tp->t_iflag & BRKINT) || (tp->t_iflag & IGNBRK))\
1361123120Simp	    && (!(tp->t_iflag & PARMRK)\
1362123120Simp		|| (tp->t_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))\
1363123120Simp	    && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))\
1364130203Sphk	    && linesw[tp->t_line]->l_rint == ttyinput)
1365123120Simp
1366123120Simp/*
1367123120Simp * Error callback function.
1368123120Simp */
1369123120Simpstatic void cx_error (cx_chan_t *c, int data)
1370123120Simp{
1371123120Simp	drv_t *d = c->sys;
1372123120Simp	async_q *q;
1373123120Simp
1374123120Simp	if (!d)
1375123120Simp		return;
1376123120Simp
1377123120Simp	q = &(d->aqueue);
1378123120Simp
1379123120Simp	switch (data) {
1380123120Simp	case CX_FRAME:
1381123120Simp		CX_DEBUG (d, ("frame error\n"));
1382130240Srik		if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN)
1383123120Simp			&& (AQ_GSZ (q) < BF_SZ - 1)
1384130240Srik			&& (!CONDITION((&d->tty->t_termios), (d->tty))
1385130240Srik			|| !(d->tty->t_iflag & (IGNPAR | PARMRK)))) {
1386123120Simp			AQ_PUSH (q, TTY_FE);
1387123120Simp			d->intr_action |= CX_READ;
1388123120Simp			MY_SOFT_INTR = 1;
1389123120Simp			swi_sched (cx_fast_ih, 0);
1390123120Simp		}
1391123120Simp#ifndef NETGRAPH
1392123120Simp		else
1393147256Sbrooks			++d->ifp->if_ierrors;
1394123120Simp#endif
1395123120Simp		break;
1396123120Simp	case CX_CRC:
1397123120Simp		CX_DEBUG (d, ("crc error\n"));
1398130240Srik		if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN)
1399123120Simp			&& (AQ_GSZ (q) < BF_SZ - 1)
1400130240Srik			&& (!CONDITION((&d->tty->t_termios), (d->tty))
1401130240Srik			|| !(d->tty->t_iflag & INPCK)
1402130240Srik			|| !(d->tty->t_iflag & (IGNPAR | PARMRK)))) {
1403123120Simp			AQ_PUSH (q, TTY_PE);
1404123120Simp			d->intr_action |= CX_READ;
1405123120Simp			MY_SOFT_INTR = 1;
1406123120Simp			swi_sched (cx_fast_ih, 0);
1407123120Simp		}
1408123120Simp#ifndef NETGRAPH
1409123120Simp		else
1410147256Sbrooks			++d->ifp->if_ierrors;
1411123120Simp#endif
1412123120Simp		break;
1413123120Simp	case CX_OVERRUN:
1414123120Simp		CX_DEBUG (d, ("overrun error\n"));
1415123120Simp#ifdef TTY_OE
1416130240Srik		if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN)
1417123120Simp			&& (AQ_GSZ (q) < BF_SZ - 1)
1418130240Srik			&& (!CONDITION((&d->tty->t_termios), (d->tty)))) {
1419123120Simp			AQ_PUSH (q, TTY_OE);
1420123120Simp			d->intr_action |= CX_READ;
1421123120Simp			MY_SOFT_INTR = 1;
1422123120Simp			swi_sched (cx_fast_ih, 0);
1423123120Simp		}
1424123120Simp#endif
1425123120Simp#ifndef NETGRAPH
1426123120Simp		else {
1427147256Sbrooks			++d->ifp->if_collisions;
1428147256Sbrooks			++d->ifp->if_ierrors;
1429123120Simp		}
1430123120Simp#endif
1431123120Simp		break;
1432123120Simp	case CX_OVERFLOW:
1433123120Simp		CX_DEBUG (d, ("overflow error\n"));
1434123120Simp#ifndef NETGRAPH
1435123120Simp		if (c->mode != M_ASYNC)
1436147256Sbrooks			++d->ifp->if_ierrors;
1437123120Simp#endif
1438123120Simp		break;
1439123120Simp	case CX_UNDERRUN:
1440123120Simp		CX_DEBUG (d, ("underrun error\n"));
1441123120Simp		if (c->mode != M_ASYNC) {
1442123120Simp#ifdef NETGRAPH
1443123120Simp			d->timeout = 0;
1444123120Simp#else
1445147256Sbrooks			++d->ifp->if_oerrors;
1446148887Srwatson			d->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1447147256Sbrooks			d->ifp->if_timer = 0;
1448123120Simp			cx_start (d);
1449123120Simp#endif
1450123120Simp		}
1451123120Simp		break;
1452123120Simp	case CX_BREAK:
1453123120Simp		CX_DEBUG (d, ("break error\n"));
1454130240Srik		if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN)
1455123120Simp			&& (AQ_GSZ (q) < BF_SZ - 1)
1456130240Srik			&& (!CONDITION((&d->tty->t_termios), (d->tty))
1457130240Srik			|| !(d->tty->t_iflag & (IGNBRK | BRKINT | PARMRK)))) {
1458123120Simp			AQ_PUSH (q, TTY_BI);
1459123120Simp			d->intr_action |= CX_READ;
1460123120Simp			MY_SOFT_INTR = 1;
1461123120Simp			swi_sched (cx_fast_ih, 0);
1462123120Simp		}
1463123120Simp#ifndef NETGRAPH
1464123120Simp		else
1465147256Sbrooks			++d->ifp->if_ierrors;
1466123120Simp#endif
1467123120Simp		break;
1468123120Simp	default:
1469123120Simp		CX_DEBUG (d, ("error #%d\n", data));
1470123120Simp	}
1471123120Simp}
1472123120Simp
1473136480Sphkstatic int cx_topen (struct tty *tp, struct cdev *dev)
1474123120Simp{
1475138823Srik	bdrv_t *bd;
1476123120Simp	drv_t *d;
1477123120Simp
1478136480Sphk	d = tp->t_sc;
1479138823Srik	CX_DEBUG2 (d, ("cx_open (serial)\n"));
1480138823Srik
1481138823Srik	bd = d->board->sys;
1482138823Srik
1483136480Sphk	if (d->chan->mode != M_ASYNC)
1484136480Sphk		return (EBUSY);
1485138823Srik
1486123120Simp	d->open_dev |= 0x2;
1487138823Srik	CX_LOCK (bd);
1488136480Sphk	cx_start_chan (d->chan, 0, 0);
1489136480Sphk	cx_set_dtr (d->chan, 1);
1490136480Sphk	cx_set_rts (d->chan, 1);
1491136480Sphk	d->cd = cx_get_cd (d->chan);
1492138823Srik	CX_UNLOCK (bd);
1493138823Srik
1494138823Srik	CX_DEBUG2 (d, ("cx_open done\n"));
1495138823Srik
1496138823Srik	return 0;
1497123120Simp}
1498123120Simp
1499136480Sphkstatic void cx_tclose (struct tty *tp)
1500123120Simp{
1501136480Sphk	drv_t *d;
1502138823Srik	bdrv_t *bd;
1503123120Simp
1504136480Sphk	d = tp->t_sc;
1505138823Srik	CX_DEBUG2 (d, ("cx_close\n"));
1506138823Srik	bd = d->board->sys;
1507138823Srik	CX_LOCK (bd);
1508123120Simp	/* Disable receiver.
1509123120Simp	 * Transmitter continues sending the queued data. */
1510123120Simp	cx_enable_receive (d->chan, 0);
1511138823Srik	CX_UNLOCK (bd);
1512123120Simp	d->open_dev &= ~0x2;
1513123120Simp}
1514123120Simp
1515136480Sphkstatic int cx_tmodem (struct tty *tp, int sigon, int sigoff)
1516123120Simp{
1517136480Sphk	drv_t *d;
1518138823Srik	bdrv_t *bd;
1519123120Simp
1520136480Sphk	d = tp->t_sc;
1521138823Srik	bd = d->board->sys;
1522123120Simp
1523138823Srik	CX_LOCK (bd);
1524136480Sphk	if (!sigon && !sigoff) {
1525136480Sphk		if (cx_get_dsr (d->chan)) sigon |= SER_DSR;
1526136480Sphk		if (cx_get_cd  (d->chan)) sigon |= SER_DCD;
1527136480Sphk		if (cx_get_cts (d->chan)) sigon |= SER_CTS;
1528136480Sphk		if (d->chan->dtr)	  sigon |= SER_DTR;
1529136480Sphk		if (d->chan->rts)	  sigon |= SER_RTS;
1530147859Srik		CX_UNLOCK (bd);
1531136480Sphk		return sigon;
1532136480Sphk	}
1533136480Sphk
1534136480Sphk	if (sigon & SER_DTR)
1535136480Sphk		cx_set_dtr (d->chan, 1);
1536136480Sphk	if (sigoff & SER_DTR)
1537136480Sphk		cx_set_dtr (d->chan, 0);
1538136480Sphk	if (sigon & SER_RTS)
1539136480Sphk		cx_set_rts (d->chan, 1);
1540136480Sphk	if (sigoff & SER_RTS)
1541136480Sphk		cx_set_rts (d->chan, 0);
1542138823Srik	CX_UNLOCK (bd);
1543138823Srik
1544136480Sphk	return (0);
1545123120Simp}
1546123120Simp
1547150622Srikstatic int cx_open (struct cdev *dev, int flag, int mode, struct thread *td)
1548150622Srik{
1549150622Srik	int unit;
1550150622Srik	drv_t *d;
1551150622Srik
1552150622Srik	d = dev->si_drv1;
1553150622Srik	unit = d->chan->num;
1554150622Srik
1555150622Srik	CX_DEBUG2 (d, ("cx_open unit=%d, flag=0x%x, mode=0x%x\n",
1556150622Srik		    unit, flag, mode));
1557150622Srik
1558150622Srik	d->open_dev |= 0x1;
1559150622Srik
1560150622Srik	CX_DEBUG2 (d, ("cx_open done\n"));
1561150622Srik
1562150622Srik	return 0;
1563150622Srik}
1564150622Srik
1565150622Srikstatic int cx_close (struct cdev *dev, int flag, int mode, struct thread *td)
1566150622Srik{
1567150622Srik	drv_t *d;
1568150622Srik
1569150622Srik	d = dev->si_drv1;
1570150622Srik	CX_DEBUG2 (d, ("cx_close\n"));
1571150622Srik	d->open_dev &= ~0x1;
1572150622Srik	return 0;
1573150622Srik}
1574150622Srik
1575150622Srikstatic int cx_modem_status (drv_t *d)
1576150622Srik{
1577150622Srik	bdrv_t *bd = d->board->sys;
1578150622Srik	int status = 0, s = splhigh ();
1579150622Srik	CX_LOCK (bd);
1580150622Srik	/* Already opened by someone or network interface is up? */
1581150622Srik	if ((d->chan->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN) &&
1582150622Srik	    (d->open_dev|0x2)) || (d->chan->mode != M_ASYNC && d->running))
1583150622Srik		status = TIOCM_LE;	/* always enabled while open */
1584150622Srik
1585150622Srik	if (cx_get_dsr (d->chan)) status |= TIOCM_DSR;
1586150622Srik	if (cx_get_cd  (d->chan)) status |= TIOCM_CD;
1587150622Srik	if (cx_get_cts (d->chan)) status |= TIOCM_CTS;
1588150622Srik	if (d->chan->dtr)	  status |= TIOCM_DTR;
1589150622Srik	if (d->chan->rts)	  status |= TIOCM_RTS;
1590150622Srik	CX_UNLOCK (bd);
1591150622Srik	splx (s);
1592150622Srik	return status;
1593150622Srik}
1594150622Srik
1595150622Srikstatic int cx_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
1596150622Srik{
1597150622Srik	drv_t *d;
1598150622Srik	bdrv_t *bd;
1599150622Srik	cx_chan_t *c;
1600150622Srik	struct serial_statistics *st;
1601150622Srik	int error, s;
1602150622Srik	char mask[16];
1603150622Srik
1604150622Srik	d = dev->si_drv1;
1605150622Srik	c = d->chan;
1606150622Srik
1607150622Srik	bd = d->board->sys;
1608150622Srik
1609150622Srik	switch (cmd) {
1610150622Srik	case SERIAL_GETREGISTERED:
1611150622Srik		CX_DEBUG2 (d, ("ioctl: getregistered\n"));
1612150622Srik		bzero (mask, sizeof(mask));
1613150622Srik		for (s=0; s<NCX*NCHAN; ++s)
1614150622Srik			if (channel [s])
1615150622Srik				mask [s/8] |= 1 << (s & 7);
1616150622Srik		bcopy (mask, data, sizeof (mask));
1617150622Srik		return 0;
1618150622Srik
1619150622Srik	case SERIAL_GETPORT:
1620150622Srik		CX_DEBUG2 (d, ("ioctl: getport\n"));
1621150622Srik		s = splhigh ();
1622150622Srik		CX_LOCK (bd);
1623150622Srik		*(int *)data = cx_get_port (c);
1624150622Srik		CX_UNLOCK (bd);
1625150622Srik		splx (s);
1626150622Srik		if (*(int *)data<0)
1627150622Srik			return (EINVAL);
1628150622Srik		else
1629150622Srik			return 0;
1630150622Srik
1631150622Srik	case SERIAL_SETPORT:
1632150622Srik		CX_DEBUG2 (d, ("ioctl: setproto\n"));
1633150622Srik		/* Only for superuser! */
1634164033Srwatson		error = priv_check (td, PRIV_DRIVER);
1635150622Srik		if (error)
1636150622Srik			return error;
1637150622Srik
1638150622Srik		s = splhigh ();
1639150622Srik		CX_LOCK (bd);
1640150622Srik		cx_set_port (c, *(int *)data);
1641150622Srik		CX_UNLOCK (bd);
1642150622Srik		splx (s);
1643150622Srik		return 0;
1644150622Srik
1645150622Srik#ifndef NETGRAPH
1646150622Srik	case SERIAL_GETPROTO:
1647150622Srik		CX_DEBUG2 (d, ("ioctl: getproto\n"));
1648150622Srik		s = splhigh ();
1649150622Srik		CX_LOCK (bd);
1650150622Srik		strcpy ((char*)data, (c->mode == M_ASYNC) ? "async" :
1651150622Srik			(IFP2SP(d->ifp)->pp_flags & PP_FR) ? "fr" :
1652150622Srik			(d->ifp->if_flags & PP_CISCO) ? "cisco" : "ppp");
1653150622Srik		CX_UNLOCK (bd);
1654150622Srik		splx (s);
1655150622Srik		return 0;
1656150622Srik
1657150622Srik	case SERIAL_SETPROTO:
1658150622Srik		CX_DEBUG2 (d, ("ioctl: setproto\n"));
1659150622Srik		/* Only for superuser! */
1660164033Srwatson		error = priv_check (td, PRIV_DRIVER);
1661150622Srik		if (error)
1662150622Srik			return error;
1663150622Srik		if (c->mode == M_ASYNC)
1664150622Srik			return EBUSY;
1665150622Srik		if (d->ifp->if_drv_flags & IFF_DRV_RUNNING)
1666150622Srik			return EBUSY;
1667150622Srik		if (! strcmp ("cisco", (char*)data)) {
1668150622Srik			IFP2SP(d->ifp)->pp_flags &= ~(PP_FR);
1669150622Srik			IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE;
1670150622Srik			d->ifp->if_flags |= PP_CISCO;
1671150622Srik		} else if (! strcmp ("fr", (char*)data)) {
1672150622Srik			d->ifp->if_flags &= ~(PP_CISCO);
1673150622Srik			IFP2SP(d->ifp)->pp_flags |= PP_FR | PP_KEEPALIVE;
1674150622Srik		} else if (! strcmp ("ppp", (char*)data)) {
1675150622Srik			IFP2SP(d->ifp)->pp_flags &= ~(PP_FR | PP_KEEPALIVE);
1676150622Srik			d->ifp->if_flags &= ~(PP_CISCO);
1677150622Srik		} else
1678150622Srik			return EINVAL;
1679150622Srik		return 0;
1680150622Srik
1681150622Srik	case SERIAL_GETKEEPALIVE:
1682150622Srik		CX_DEBUG2 (d, ("ioctl: getkeepalive\n"));
1683150622Srik		if ((IFP2SP(d->ifp)->pp_flags & PP_FR) ||
1684150622Srik		    (d->ifp->if_flags & PP_CISCO) ||
1685150622Srik		    (c->mode == M_ASYNC))
1686150622Srik			return EINVAL;
1687150622Srik		s = splhigh ();
1688150622Srik		CX_LOCK (bd);
1689150622Srik		*(int*)data = (IFP2SP(d->ifp)->pp_flags & PP_KEEPALIVE) ? 1 : 0;
1690150622Srik		CX_UNLOCK (bd);
1691150622Srik		splx (s);
1692150622Srik		return 0;
1693150622Srik
1694150622Srik	case SERIAL_SETKEEPALIVE:
1695150622Srik		CX_DEBUG2 (d, ("ioctl: setkeepalive\n"));
1696150622Srik		/* Only for superuser! */
1697164033Srwatson		error = priv_check (td, PRIV_DRIVER);
1698150622Srik		if (error)
1699150622Srik			return error;
1700150622Srik		if ((IFP2SP(d->ifp)->pp_flags & PP_FR) ||
1701150622Srik			(d->ifp->if_flags & PP_CISCO))
1702150622Srik			return EINVAL;
1703150622Srik		s = splhigh ();
1704150622Srik		CX_LOCK (bd);
1705150622Srik		if (*(int*)data)
1706150622Srik			IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE;
1707150622Srik		else
1708150622Srik			IFP2SP(d->ifp)->pp_flags &= ~PP_KEEPALIVE;
1709150622Srik		CX_UNLOCK (bd);
1710150622Srik		splx (s);
1711150622Srik		return 0;
1712150622Srik#endif /*NETGRAPH*/
1713150622Srik
1714150622Srik	case SERIAL_GETMODE:
1715150622Srik		CX_DEBUG2 (d, ("ioctl: getmode\n"));
1716150622Srik		s = splhigh ();
1717150622Srik		CX_LOCK (bd);
1718150622Srik		*(int*)data = (c->mode == M_ASYNC) ?
1719150622Srik			SERIAL_ASYNC : SERIAL_HDLC;
1720150622Srik		CX_UNLOCK (bd);
1721150622Srik		splx (s);
1722150622Srik		return 0;
1723150622Srik
1724150622Srik	case SERIAL_SETMODE:
1725150622Srik		CX_DEBUG2 (d, ("ioctl: setmode\n"));
1726150622Srik		/* Only for superuser! */
1727164033Srwatson		error = priv_check (td, PRIV_DRIVER);
1728150622Srik		if (error)
1729150622Srik			return error;
1730150622Srik
1731150622Srik		/* Somebody is waiting for carrier? */
1732150622Srik		if (d->lock)
1733150622Srik			return EBUSY;
1734150622Srik		/* /dev/ttyXX is already opened by someone? */
1735150622Srik		if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN) &&
1736150622Srik		    (d->open_dev|0x2))
1737150622Srik			return EBUSY;
1738150622Srik		/* Network interface is up?
1739150622Srik		 * Cannot change to async mode. */
1740150622Srik		if (c->mode != M_ASYNC && d->running &&
1741150622Srik		    (*(int*)data == SERIAL_ASYNC))
1742150622Srik			return EBUSY;
1743150622Srik
1744150622Srik		s = splhigh ();
1745150622Srik		CX_LOCK (bd);
1746150622Srik		if (c->mode == M_HDLC && *(int*)data == SERIAL_ASYNC) {
1747150622Srik			cx_set_mode (c, M_ASYNC);
1748150622Srik			cx_enable_receive (c, 0);
1749150622Srik			cx_enable_transmit (c, 0);
1750150622Srik		} else if (c->mode == M_ASYNC && *(int*)data == SERIAL_HDLC) {
1751150622Srik			cx_set_mode (c, M_HDLC);
1752150622Srik			cx_enable_receive (c, 1);
1753150622Srik			cx_enable_transmit (c, 1);
1754150622Srik		}
1755150622Srik		CX_UNLOCK (bd);
1756150622Srik		splx (s);
1757150622Srik		return 0;
1758150622Srik
1759150622Srik	case SERIAL_GETSTAT:
1760150622Srik		CX_DEBUG2 (d, ("ioctl: getestat\n"));
1761150622Srik		st = (struct serial_statistics*) data;
1762150622Srik		s = splhigh ();
1763150622Srik		CX_LOCK (bd);
1764150622Srik		st->rintr  = c->rintr;
1765150622Srik		st->tintr  = c->tintr;
1766150622Srik		st->mintr  = c->mintr;
1767150622Srik		st->ibytes = c->ibytes;
1768150622Srik		st->ipkts  = c->ipkts;
1769150622Srik		st->ierrs  = c->ierrs;
1770150622Srik		st->obytes = c->obytes;
1771150622Srik		st->opkts  = c->opkts;
1772150622Srik		st->oerrs  = c->oerrs;
1773150622Srik		CX_UNLOCK (bd);
1774150622Srik		splx (s);
1775150622Srik		return 0;
1776150622Srik
1777150622Srik	case SERIAL_CLRSTAT:
1778150622Srik		CX_DEBUG2 (d, ("ioctl: clrstat\n"));
1779150622Srik		/* Only for superuser! */
1780164033Srwatson		error = priv_check (td, PRIV_DRIVER);
1781150622Srik		if (error)
1782150622Srik			return error;
1783150622Srik		s = splhigh ();
1784150622Srik		CX_LOCK (bd);
1785150622Srik		c->rintr = 0;
1786150622Srik		c->tintr = 0;
1787150622Srik		c->mintr = 0;
1788150622Srik		c->ibytes = 0;
1789150622Srik		c->ipkts = 0;
1790150622Srik		c->ierrs = 0;
1791150622Srik		c->obytes = 0;
1792150622Srik		c->opkts = 0;
1793150622Srik		c->oerrs = 0;
1794150622Srik		CX_UNLOCK (bd);
1795150622Srik		splx (s);
1796150622Srik		return 0;
1797150622Srik
1798150622Srik	case SERIAL_GETBAUD:
1799150622Srik		CX_DEBUG2 (d, ("ioctl: getbaud\n"));
1800150622Srik		if (c->mode == M_ASYNC)
1801150622Srik			return EINVAL;
1802150622Srik		s = splhigh ();
1803150622Srik		CX_LOCK (bd);
1804150622Srik		*(long*)data = cx_get_baud(c);
1805150622Srik		CX_UNLOCK (bd);
1806150622Srik		splx (s);
1807150622Srik		return 0;
1808150622Srik
1809150622Srik	case SERIAL_SETBAUD:
1810150622Srik		CX_DEBUG2 (d, ("ioctl: setbaud\n"));
1811150622Srik		/* Only for superuser! */
1812164033Srwatson		error = priv_check (td, PRIV_DRIVER);
1813150622Srik		if (error)
1814150622Srik			return error;
1815150622Srik		if (c->mode == M_ASYNC)
1816150622Srik			return EINVAL;
1817150622Srik		s = splhigh ();
1818150622Srik		CX_LOCK (bd);
1819150622Srik		cx_set_baud (c, *(long*)data);
1820150622Srik		CX_UNLOCK (bd);
1821150622Srik		splx (s);
1822150622Srik		return 0;
1823150622Srik
1824150622Srik	case SERIAL_GETLOOP:
1825150622Srik		CX_DEBUG2 (d, ("ioctl: getloop\n"));
1826150622Srik		if (c->mode == M_ASYNC)
1827150622Srik			return EINVAL;
1828150622Srik		s = splhigh ();
1829150622Srik		CX_LOCK (bd);
1830150622Srik		*(int*)data = cx_get_loop (c);
1831150622Srik		CX_UNLOCK (bd);
1832150622Srik		splx (s);
1833150622Srik		return 0;
1834150622Srik
1835150622Srik	case SERIAL_SETLOOP:
1836150622Srik		CX_DEBUG2 (d, ("ioctl: setloop\n"));
1837150622Srik		/* Only for superuser! */
1838164033Srwatson		error = priv_check (td, PRIV_DRIVER);
1839150622Srik		if (error)
1840150622Srik			return error;
1841150622Srik		if (c->mode == M_ASYNC)
1842150622Srik			return EINVAL;
1843150622Srik		s = splhigh ();
1844150622Srik		CX_LOCK (bd);
1845150622Srik		cx_set_loop (c, *(int*)data);
1846150622Srik		CX_UNLOCK (bd);
1847150622Srik		splx (s);
1848150622Srik		return 0;
1849150622Srik
1850150622Srik	case SERIAL_GETDPLL:
1851150622Srik		CX_DEBUG2 (d, ("ioctl: getdpll\n"));
1852150622Srik		if (c->mode == M_ASYNC)
1853150622Srik			return EINVAL;
1854150622Srik		s = splhigh ();
1855150622Srik		CX_LOCK (bd);
1856150622Srik		*(int*)data = cx_get_dpll (c);
1857150622Srik		CX_UNLOCK (bd);
1858150622Srik		splx (s);
1859150622Srik		return 0;
1860150622Srik
1861150622Srik	case SERIAL_SETDPLL:
1862150622Srik		CX_DEBUG2 (d, ("ioctl: setdpll\n"));
1863150622Srik		/* Only for superuser! */
1864164033Srwatson		error = priv_check (td, PRIV_DRIVER);
1865150622Srik		if (error)
1866150622Srik			return error;
1867150622Srik		if (c->mode == M_ASYNC)
1868150622Srik			return EINVAL;
1869150622Srik		s = splhigh ();
1870150622Srik		CX_LOCK (bd);
1871150622Srik		cx_set_dpll (c, *(int*)data);
1872150622Srik		CX_UNLOCK (bd);
1873150622Srik		splx (s);
1874150622Srik		return 0;
1875150622Srik
1876150622Srik	case SERIAL_GETNRZI:
1877150622Srik		CX_DEBUG2 (d, ("ioctl: getnrzi\n"));
1878150622Srik		if (c->mode == M_ASYNC)
1879150622Srik			return EINVAL;
1880150622Srik		s = splhigh ();
1881150622Srik		CX_LOCK (bd);
1882150622Srik		*(int*)data = cx_get_nrzi (c);
1883150622Srik		CX_UNLOCK (bd);
1884150622Srik		splx (s);
1885150622Srik		return 0;
1886150622Srik
1887150622Srik	case SERIAL_SETNRZI:
1888150622Srik		CX_DEBUG2 (d, ("ioctl: setnrzi\n"));
1889150622Srik		/* Only for superuser! */
1890164033Srwatson		error = priv_check (td, PRIV_DRIVER);
1891150622Srik		if (error)
1892150622Srik			return error;
1893150622Srik		if (c->mode == M_ASYNC)
1894150622Srik			return EINVAL;
1895150622Srik		s = splhigh ();
1896150622Srik		CX_LOCK (bd);
1897150622Srik		cx_set_nrzi (c, *(int*)data);
1898150622Srik		CX_UNLOCK (bd);
1899150622Srik		splx (s);
1900150622Srik		return 0;
1901150622Srik
1902150622Srik	case SERIAL_GETDEBUG:
1903150622Srik		CX_DEBUG2 (d, ("ioctl: getdebug\n"));
1904150622Srik		s = splhigh ();
1905150622Srik		CX_LOCK (bd);
1906150622Srik		*(int*)data = c->debug;
1907150622Srik		CX_UNLOCK (bd);
1908150622Srik		splx (s);
1909150622Srik		return 0;
1910150622Srik
1911150622Srik	case SERIAL_SETDEBUG:
1912150622Srik		CX_DEBUG2 (d, ("ioctl: setdebug\n"));
1913150622Srik		/* Only for superuser! */
1914164033Srwatson		error = priv_check (td, PRIV_DRIVER);
1915150622Srik		if (error)
1916150622Srik			return error;
1917150622Srik		s = splhigh ();
1918150622Srik		CX_LOCK (bd);
1919150622Srik		c->debug = *(int*)data;
1920150622Srik		CX_UNLOCK (bd);
1921150622Srik		splx (s);
1922150622Srik#ifndef	NETGRAPH
1923150622Srik		if (d->chan->debug)
1924150622Srik			d->ifp->if_flags |= IFF_DEBUG;
1925150622Srik		else
1926150622Srik			d->ifp->if_flags &= (~IFF_DEBUG);
1927150622Srik#endif
1928150622Srik		return 0;
1929150622Srik	}
1930150622Srik
1931150622Srik	switch (cmd) {
1932150622Srik	case TIOCSDTR:	/* Set DTR */
1933150622Srik		CX_DEBUG2 (d, ("ioctl: tiocsdtr\n"));
1934150622Srik		s = splhigh ();
1935150622Srik		CX_LOCK (bd);
1936150622Srik		cx_set_dtr (c, 1);
1937150622Srik		CX_UNLOCK (bd);
1938150622Srik		splx (s);
1939150622Srik		return 0;
1940150622Srik
1941150622Srik	case TIOCCDTR:	/* Clear DTR */
1942150622Srik		CX_DEBUG2 (d, ("ioctl: tioccdtr\n"));
1943150622Srik		s = splhigh ();
1944150622Srik		CX_LOCK (bd);
1945150622Srik		cx_set_dtr (c, 0);
1946150622Srik		CX_UNLOCK (bd);
1947150622Srik		splx (s);
1948150622Srik		return 0;
1949150622Srik
1950150622Srik	case TIOCMSET:	/* Set DTR/RTS */
1951150622Srik		CX_DEBUG2 (d, ("ioctl: tiocmset\n"));
1952150622Srik		s = splhigh ();
1953150622Srik		CX_LOCK (bd);
1954150622Srik		cx_set_dtr (c, (*(int*)data & TIOCM_DTR) ? 1 : 0);
1955150622Srik		cx_set_rts (c, (*(int*)data & TIOCM_RTS) ? 1 : 0);
1956150622Srik		CX_UNLOCK (bd);
1957150622Srik		splx (s);
1958150622Srik		return 0;
1959150622Srik
1960150622Srik	case TIOCMBIS:	/* Add DTR/RTS */
1961150622Srik		CX_DEBUG2 (d, ("ioctl: tiocmbis\n"));
1962150622Srik		s = splhigh ();
1963150622Srik		CX_LOCK (bd);
1964150622Srik		if (*(int*)data & TIOCM_DTR) cx_set_dtr (c, 1);
1965150622Srik		if (*(int*)data & TIOCM_RTS) cx_set_rts (c, 1);
1966150622Srik		CX_UNLOCK (bd);
1967150622Srik		splx (s);
1968150622Srik		return 0;
1969150622Srik
1970150622Srik	case TIOCMBIC:	/* Clear DTR/RTS */
1971150622Srik		CX_DEBUG2 (d, ("ioctl: tiocmbic\n"));
1972150622Srik		s = splhigh ();
1973150622Srik		CX_LOCK (bd);
1974150622Srik		if (*(int*)data & TIOCM_DTR) cx_set_dtr (c, 0);
1975150622Srik		if (*(int*)data & TIOCM_RTS) cx_set_rts (c, 0);
1976150622Srik		CX_UNLOCK (bd);
1977150622Srik		splx (s);
1978150622Srik		return 0;
1979150622Srik
1980150622Srik	case TIOCMGET:	/* Get modem status */
1981150622Srik		CX_DEBUG2 (d, ("ioctl: tiocmget\n"));
1982150622Srik		*(int*)data = cx_modem_status (d);
1983150622Srik		return 0;
1984150622Srik
1985150622Srik	}
1986150622Srik
1987150622Srik	CX_DEBUG2 (d, ("ioctl: 0x%lx\n", cmd));
1988150622Srik	return ENOTTY;
1989150622Srik}
1990150622Srik
1991123120Simpvoid cx_softintr (void *unused)
1992123120Simp{
1993123120Simp	drv_t *d;
1994138823Srik	bdrv_t *bd;
1995123120Simp	async_q *q;
1996123120Simp	int i, s, ic, k;
1997123120Simp	while (MY_SOFT_INTR) {
1998123120Simp		MY_SOFT_INTR = 0;
1999123120Simp		for (i=0; i<NCX*NCHAN; ++i) {
2000123120Simp			d = channel [i];
2001123120Simp			if (!d || !d->chan || d->chan->type == T_NONE
2002130240Srik			    || d->chan->mode != M_ASYNC || !d->tty
2003130240Srik			    || !d->tty->t_dev)
2004123120Simp				continue;
2005138823Srik			bd = d->board->sys;
2006123120Simp			s = splhigh ();
2007138823Srik			CX_LOCK (bd);
2008123120Simp			if (d->intr_action & CX_READ) {
2009123120Simp				q = &(d->aqueue);
2010130240Srik				if (d->tty->t_state & TS_CAN_BYPASS_L_RINT) {
2011123120Simp					k = AQ_GSZ(q);
2012130240Srik					if (d->tty->t_rawq.c_cc + k >
2013130240Srik						d->tty->t_ihiwat
2014130240Srik					    && (d->tty->t_cflag & CRTS_IFLOW
2015130240Srik						|| d->tty->t_iflag & IXOFF)
2016130240Srik					    && !(d->tty->t_state & TS_TBLOCK))
2017130240Srik						ttyblock(d->tty);
2018130240Srik					d->tty->t_rawcc += k;
2019123120Simp					while (k>0) {
2020123120Simp						k--;
2021123120Simp						AQ_POP (q, ic);
2022138823Srik						CX_UNLOCK (bd);
2023123120Simp						splx (s);
2024130240Srik						putc (ic, &d->tty->t_rawq);
2025123120Simp						s = splhigh ();
2026138823Srik						CX_LOCK (bd);
2027123120Simp					}
2028130240Srik					ttwakeup(d->tty);
2029130240Srik					if (d->tty->t_state & TS_TTSTOP
2030130240Srik					    && (d->tty->t_iflag & IXANY
2031130240Srik						|| d->tty->t_cc[VSTART] ==
2032130240Srik						d->tty->t_cc[VSTOP])) {
2033130240Srik						d->tty->t_state &= ~TS_TTSTOP;
2034130240Srik						d->tty->t_lflag &= ~FLUSHO;
2035123120Simp						d->intr_action |= CX_WRITE;
2036123120Simp					}
2037123120Simp				} else {
2038123120Simp					while (q->end != q->beg) {
2039123120Simp						AQ_POP (q, ic);
2040138823Srik						CX_UNLOCK (bd);
2041123120Simp						splx (s);
2042130240Srik						ttyld_rint (d->tty, ic);
2043123120Simp						s = splhigh ();
2044138823Srik						CX_LOCK (bd);
2045123120Simp					}
2046123120Simp				}
2047123120Simp				d->intr_action &= ~CX_READ;
2048123120Simp			}
2049123120Simp			splx (s);
2050138823Srik			CX_UNLOCK (bd);
2051123120Simp
2052123120Simp			s = splhigh ();
2053138823Srik			CX_LOCK (bd);
2054123120Simp			if (d->intr_action & CX_WRITE) {
2055130240Srik				if (d->tty->t_line)
2056130240Srik					ttyld_start (d->tty);
2057123120Simp				else
2058130240Srik					cx_oproc (d->tty);
2059123120Simp				d->intr_action &= ~CX_WRITE;
2060123120Simp			}
2061138823Srik			CX_UNLOCK (bd);
2062123120Simp			splx (s);
2063123120Simp
2064123120Simp		}
2065123120Simp	}
2066123120Simp}
2067123120Simp
2068123120Simp/*
2069123120Simp * Fill transmitter buffer with data.
2070123120Simp */
2071123120Simpstatic void cx_oproc (struct tty *tp)
2072123120Simp{
2073136480Sphk	int s, k;
2074136480Sphk	drv_t *d;
2075138823Srik	bdrv_t *bd;
2076123120Simp	static u_char buf[DMABUFSZ];
2077123120Simp	u_char *p;
2078123120Simp	u_short len = 0, sublen = 0;
2079123120Simp
2080138823Srik	d = tp->t_sc;
2081138823Srik	bd = d->board->sys;
2082136480Sphk
2083123120Simp	CX_DEBUG2 (d, ("cx_oproc\n"));
2084138823Srik
2085138823Srik	s = splhigh ();
2086138823Srik	CX_LOCK (bd);
2087138823Srik
2088123120Simp	if (tp->t_cflag & CRTSCTS && (tp->t_state & TS_TBLOCK) && d->chan->rts)
2089123120Simp		cx_set_rts (d->chan, 0);
2090123120Simp	else if (tp->t_cflag & CRTSCTS && ! (tp->t_state & TS_TBLOCK) && ! d->chan->rts)
2091123120Simp		cx_set_rts (d->chan, 1);
2092123120Simp
2093123120Simp	if (! (tp->t_state & (TS_TIMEOUT | TS_TTSTOP))) {
2094123120Simp		/* Start transmitter. */
2095123120Simp		cx_enable_transmit (d->chan, 1);
2096123120Simp
2097123120Simp		/* Is it busy? */
2098123120Simp		if (! cx_buf_free (d->chan)) {
2099123120Simp			tp->t_state |= TS_BUSY;
2100138823Srik			CX_UNLOCK (bd);
2101123120Simp			splx (s);
2102123120Simp			return;
2103123120Simp		}
2104123120Simp		if (tp->t_iflag & IXOFF) {
2105123120Simp			p = (buf + (DMABUFSZ/2));
2106123120Simp			sublen = q_to_b (&tp->t_outq, p, (DMABUFSZ/2));
2107123120Simp			k = sublen;
2108123120Simp			while (k--) {
2109123120Simp				/* Send XON/XOFF out of band. */
2110123120Simp				if (*p == tp->t_cc[VSTOP]) {
2111123120Simp					cx_xflow_ctl (d->chan, 0);
2112123120Simp					p++;
2113123120Simp					continue;
2114123120Simp				}
2115123120Simp				if (*p == tp->t_cc[VSTART]) {
2116123120Simp					cx_xflow_ctl (d->chan, 1);
2117123120Simp					p++;
2118123120Simp					continue;
2119123120Simp				}
2120123120Simp				buf[len] = *p;
2121123120Simp				len++;
2122123120Simp				p++;
2123123120Simp			}
2124123120Simp		} else {
2125123120Simp			p = buf;
2126123120Simp			len = q_to_b (&tp->t_outq, p, (DMABUFSZ/2));
2127123120Simp		}
2128123120Simp		if (len) {
2129123120Simp			cx_send_packet (d->chan, buf, len, 0);
2130123120Simp			tp->t_state |= TS_BUSY;
2131123120Simp			d->atimeout = 10;
2132123120Simp			CX_DEBUG2 (d, ("out %d bytes\n", len));
2133123120Simp		}
2134123120Simp	}
2135123120Simp	ttwwakeup (tp);
2136138823Srik	CX_UNLOCK (bd);
2137123120Simp	splx (s);
2138123120Simp}
2139123120Simp
2140123120Simpstatic int cx_param (struct tty *tp, struct termios *t)
2141123120Simp{
2142136480Sphk	drv_t *d;
2143138823Srik	bdrv_t *bd;
2144123120Simp	int s, bits, parity;
2145123120Simp
2146136480Sphk	d = tp->t_sc;
2147138823Srik	bd = d->board->sys;
2148138823Srik
2149123120Simp	s = splhigh ();
2150138823Srik	CX_LOCK (bd);
2151123120Simp	if (t->c_ospeed == 0) {
2152123120Simp		/* Clear DTR and RTS. */
2153123120Simp		cx_set_dtr (d->chan, 0);
2154138823Srik		CX_UNLOCK (bd);
2155123120Simp		splx (s);
2156123120Simp		CX_DEBUG2 (d, ("cx_param (hangup)\n"));
2157123120Simp		return 0;
2158123120Simp	}
2159123120Simp	CX_DEBUG2 (d, ("cx_param\n"));
2160123120Simp
2161123120Simp	/* Check requested parameters. */
2162123120Simp	if (t->c_ospeed < 300 || t->c_ospeed > 256*1024) {
2163138823Srik		CX_UNLOCK (bd);
2164123120Simp		splx (s);
2165133644Srik		return EINVAL;
2166123120Simp	}
2167123120Simp	if (t->c_ispeed && (t->c_ispeed < 300 || t->c_ispeed > 256*1024)) {
2168138823Srik		CX_UNLOCK (bd);
2169123120Simp		splx (s);
2170133644Srik		return EINVAL;
2171123120Simp	}
2172123120Simp
2173123120Simp  	/* And copy them to tty and channel structures. */
2174123120Simp	tp->t_ispeed = t->c_ispeed = tp->t_ospeed = t->c_ospeed;
2175123120Simp	tp->t_cflag = t->c_cflag;
2176123120Simp
2177123120Simp	/* Set character length and parity mode. */
2178123120Simp	switch (t->c_cflag & CSIZE) {
2179123120Simp	default:
2180123120Simp	case CS8: bits = 8; break;
2181123120Simp	case CS7: bits = 7; break;
2182123120Simp	case CS6: bits = 6; break;
2183123120Simp	case CS5: bits = 5; break;
2184123120Simp	}
2185123120Simp
2186123120Simp	parity = ((t->c_cflag & PARENB) ? 1 : 0) *
2187123120Simp		 (1 + ((t->c_cflag & PARODD) ? 0 : 1));
2188123120Simp
2189123120Simp	/* Set current channel number. */
2190123120Simp	if (! d->chan->dtr)
2191123120Simp		cx_set_dtr (d->chan, 1);
2192123120Simp
2193130240Srik	ttyldoptim (tp);
2194123120Simp	cx_set_async_param (d->chan, t->c_ospeed, bits, parity, (t->c_cflag & CSTOPB),
2195123120Simp		!(t->c_cflag & PARENB), (t->c_cflag & CRTSCTS),
2196123120Simp		(t->c_iflag & IXON), (t->c_iflag & IXANY),
2197123120Simp		t->c_cc[VSTART], t->c_cc[VSTOP]);
2198138823Srik	CX_UNLOCK (bd);
2199123120Simp	splx (s);
2200123120Simp	return 0;
2201123120Simp}
2202123120Simp
2203123120Simp/*
2204123120Simp * Stop output on a line
2205123120Simp */
2206123120Simpstatic void cx_stop (struct tty *tp, int flag)
2207123120Simp{
2208136480Sphk	drv_t *d;
2209138823Srik	bdrv_t *bd;
2210123120Simp	int s;
2211123120Simp
2212136480Sphk	d = tp->t_sc;
2213138823Srik	bd = d->board->sys;
2214138823Srik
2215123120Simp	s = splhigh ();
2216138823Srik	CX_LOCK (bd);
2217123120Simp	if (tp->t_state & TS_BUSY) {
2218123120Simp		/* Stop transmitter */
2219123120Simp		CX_DEBUG2 (d, ("cx_stop\n"));
2220123120Simp		cx_transmitter_ctl (d->chan, 0);
2221123120Simp	}
2222138823Srik	CX_UNLOCK (bd);
2223123120Simp	splx (s);
2224123120Simp}
2225123120Simp
2226123120Simp/*
2227123120Simp * Process the (delayed) carrier signal setup.
2228123120Simp */
2229123120Simpstatic void cx_carrier (void *arg)
2230123120Simp{
2231123120Simp	drv_t *d = arg;
2232138823Srik	bdrv_t *bd = d->board->sys;
2233123120Simp	cx_chan_t *c = d->chan;
2234123120Simp	int s, cd;
2235123120Simp
2236123120Simp	s = splhigh ();
2237138823Srik	CX_LOCK (bd);
2238123120Simp	cd = cx_get_cd (c);
2239123120Simp	if (d->cd != cd) {
2240123120Simp		if (cd) {
2241123120Simp			CX_DEBUG (d, ("carrier on\n"));
2242123120Simp			d->cd = 1;
2243138823Srik			CX_UNLOCK (bd);
2244123120Simp			splx (s);
2245130240Srik			if (d->tty)
2246130240Srik				ttyld_modem(d->tty, 1);
2247123120Simp		} else {
2248123120Simp			CX_DEBUG (d, ("carrier loss\n"));
2249123120Simp			d->cd = 0;
2250138823Srik			CX_UNLOCK (bd);
2251123120Simp			splx (s);
2252130240Srik			if (d->tty)
2253130240Srik				ttyld_modem(d->tty, 0);
2254123120Simp		}
2255147859Srik	} else {
2256147859Srik		CX_UNLOCK (bd);
2257147859Srik		splx (s);
2258123120Simp	}
2259123120Simp}
2260123120Simp
2261123120Simp/*
2262123120Simp * Modem signal callback function.
2263123120Simp */
2264123120Simpstatic void cx_modem (cx_chan_t *c)
2265123120Simp{
2266123120Simp	drv_t *d = c->sys;
2267123120Simp
2268123120Simp	if (!d || c->mode != M_ASYNC)
2269123120Simp		return;
2270123120Simp	/* Handle carrier detect/loss. */
2271123120Simp	/* Carrier changed - delay processing DCD for a while
2272123120Simp	 * to give both sides some time to initialize. */
2273138823Srik	callout_reset (&d->dcd_timeout_handle, hz/2, cx_carrier, d);
2274123120Simp}
2275123120Simp
2276123120Simp#ifdef NETGRAPH
2277123120Simpstatic int ng_cx_constructor (node_p node)
2278123120Simp{
2279123120Simp	drv_t *d = NG_NODE_PRIVATE (node);
2280123120Simp	CX_DEBUG (d, ("Constructor\n"));
2281123120Simp	return EINVAL;
2282123120Simp}
2283123120Simp
2284123120Simpstatic int ng_cx_newhook (node_p node, hook_p hook, const char *name)
2285123120Simp{
2286123120Simp	int s;
2287123120Simp	drv_t *d = NG_NODE_PRIVATE (node);
2288138823Srik	bdrv_t *bd = d->board->sys;
2289123120Simp
2290123120Simp	if (d->chan->mode == M_ASYNC)
2291123120Simp		return EINVAL;
2292123120Simp
2293123120Simp	/* Attach debug hook */
2294123120Simp	if (strcmp (name, NG_CX_HOOK_DEBUG) == 0) {
2295123120Simp		NG_HOOK_SET_PRIVATE (hook, NULL);
2296123120Simp		d->debug_hook = hook;
2297123120Simp		return 0;
2298123120Simp	}
2299123120Simp
2300123120Simp	/* Check for raw hook */
2301123120Simp	if (strcmp (name, NG_CX_HOOK_RAW) != 0)
2302123120Simp		return EINVAL;
2303123120Simp
2304123120Simp	NG_HOOK_SET_PRIVATE (hook, d);
2305123120Simp	d->hook = hook;
2306123120Simp	s = splhigh ();
2307138823Srik	CX_LOCK (bd);
2308123120Simp	cx_up (d);
2309138823Srik	CX_UNLOCK (bd);
2310123120Simp	splx (s);
2311123120Simp	return 0;
2312123120Simp}
2313123120Simp
2314123120Simpstatic int print_modems (char *s, cx_chan_t *c, int need_header)
2315123120Simp{
2316123120Simp	int status = cx_modem_status (c->sys);
2317123120Simp	int length = 0;
2318123120Simp
2319123120Simp	if (need_header)
2320123120Simp		length += sprintf (s + length, "  LE   DTR  DSR  RTS  CTS  CD\n");
2321123120Simp	length += sprintf (s + length, "%4s %4s %4s %4s %4s %4s\n",
2322123120Simp		status & TIOCM_LE  ? "On" : "-",
2323123120Simp		status & TIOCM_DTR ? "On" : "-",
2324123120Simp		status & TIOCM_DSR ? "On" : "-",
2325123120Simp		status & TIOCM_RTS ? "On" : "-",
2326123120Simp		status & TIOCM_CTS ? "On" : "-",
2327123120Simp		status & TIOCM_CD  ? "On" : "-");
2328123120Simp	return length;
2329123120Simp}
2330123120Simp
2331123120Simpstatic int print_stats (char *s, cx_chan_t *c, int need_header)
2332123120Simp{
2333123120Simp	int length = 0;
2334123120Simp
2335123120Simp	if (need_header)
2336123120Simp		length += sprintf (s + length, "  Rintr   Tintr   Mintr   Ibytes   Ipkts   Ierrs   Obytes   Opkts   Oerrs\n");
2337123120Simp	length += sprintf (s + length, "%7ld %7ld %7ld %8ld %7ld %7ld %8ld %7ld %7ld\n",
2338123120Simp		c->rintr, c->tintr, c->mintr, c->ibytes, c->ipkts,
2339123120Simp		c->ierrs, c->obytes, c->opkts, c->oerrs);
2340123120Simp	return length;
2341123120Simp}
2342123120Simp
2343123120Simpstatic int print_chan (char *s, cx_chan_t *c)
2344123120Simp{
2345123120Simp	drv_t *d = c->sys;
2346123120Simp	int length = 0;
2347123120Simp
2348123120Simp	length += sprintf (s + length, "cx%d", c->board->num * NCHAN + c->num);
2349123120Simp	if (d->chan->debug)
2350123120Simp		length += sprintf (s + length, " debug=%d", d->chan->debug);
2351123120Simp
2352123120Simp	if (cx_get_baud (c))
2353123120Simp		length += sprintf (s + length, " %ld", cx_get_baud (c));
2354123120Simp	else
2355123120Simp		length += sprintf (s + length, " extclock");
2356123120Simp
2357123120Simp	if (c->mode == M_HDLC) {
2358123120Simp		length += sprintf (s + length, " dpll=%s", cx_get_dpll (c) ? "on" : "off");
2359123120Simp		length += sprintf (s + length, " nrzi=%s", cx_get_nrzi (c) ? "on" : "off");
2360123120Simp	}
2361123120Simp
2362123120Simp	length += sprintf (s + length, " loop=%s", cx_get_loop (c) ? "on\n" : "off\n");
2363123120Simp	return length;
2364123120Simp}
2365123120Simp
2366123120Simpstatic int ng_cx_rcvmsg (node_p node, item_p item, hook_p lasthook)
2367123120Simp{
2368123120Simp	drv_t *d = NG_NODE_PRIVATE (node);
2369123120Simp	struct ng_mesg *msg;
2370123120Simp	struct ng_mesg *resp = NULL;
2371123120Simp	int error = 0;
2372123120Simp
2373123120Simp	if (!d)
2374123120Simp		return EINVAL;
2375123120Simp
2376123120Simp	CX_DEBUG (d, ("Rcvmsg\n"));
2377123120Simp	NGI_GET_MSG (item, msg);
2378123120Simp	switch (msg->header.typecookie) {
2379123120Simp	default:
2380123120Simp		error = EINVAL;
2381123120Simp		break;
2382123120Simp
2383123120Simp	case NGM_CX_COOKIE:
2384123120Simp		printf ("Don't forget to implement\n");
2385123120Simp		error = EINVAL;
2386123120Simp		break;
2387123120Simp
2388123120Simp	case NGM_GENERIC_COOKIE:
2389123120Simp		switch (msg->header.cmd) {
2390123120Simp		default:
2391123120Simp			error = EINVAL;
2392123120Simp			break;
2393123120Simp
2394123120Simp		case NGM_TEXT_STATUS: {
2395123120Simp			char *s;
2396123120Simp			int l = 0;
2397123120Simp			int dl = sizeof (struct ng_mesg) + 730;
2398123120Simp
2399123120Simp			NG_MKRESPONSE (resp, msg, dl, M_NOWAIT);
2400123120Simp			if (! resp) {
2401123120Simp				error = ENOMEM;
2402123120Simp				break;
2403123120Simp			}
2404123120Simp			bzero (resp, dl);
2405123120Simp			s = (resp)->data;
2406123120Simp			l += print_chan (s + l, d->chan);
2407123120Simp			l += print_stats (s + l, d->chan, 1);
2408123120Simp			l += print_modems (s + l, d->chan, 1);
2409123120Simp			strncpy ((resp)->header.cmdstr, "status", NG_CMDSTRLEN);
2410123120Simp			}
2411123120Simp			break;
2412123120Simp		}
2413123120Simp		break;
2414123120Simp	}
2415123120Simp	NG_RESPOND_MSG (error, node, item, resp);
2416123120Simp	NG_FREE_MSG (msg);
2417123120Simp	return error;
2418123120Simp}
2419123120Simp
2420123120Simpstatic int ng_cx_rcvdata (hook_p hook, item_p item)
2421123120Simp{
2422123120Simp	drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE(hook));
2423123120Simp	struct mbuf *m;
2424131108Sjulian	struct ng_tag_prio *ptag;
2425138823Srik	bdrv_t *bd;
2426123120Simp	struct ifqueue *q;
2427123120Simp	int s;
2428123120Simp
2429123120Simp	NGI_GET_M (item, m);
2430123120Simp	NG_FREE_ITEM (item);
2431123120Simp	if (! NG_HOOK_PRIVATE (hook) || ! d) {
2432123120Simp		NG_FREE_M (m);
2433123120Simp		return ENETDOWN;
2434123120Simp	}
2435131108Sjulian
2436138823Srik	bd = d->board->sys;
2437131108Sjulian	/* Check for high priority data */
2438131108Sjulian	if ((ptag = (struct ng_tag_prio *)m_tag_locate(m, NGM_GENERIC_COOKIE,
2439131108Sjulian	    NG_TAG_PRIO, NULL)) != NULL && (ptag->priority > NG_PRIO_CUTOFF) )
2440131108Sjulian		q = &d->hi_queue;
2441131108Sjulian	else
2442131108Sjulian		q = &d->lo_queue;
2443131108Sjulian
2444123120Simp	s = splhigh ();
2445138823Srik	CX_LOCK (bd);
2446123120Simp	IF_LOCK (q);
2447123120Simp	if (_IF_QFULL (q)) {
2448123120Simp		_IF_DROP (q);
2449123120Simp		IF_UNLOCK (q);
2450138823Srik		CX_UNLOCK (bd);
2451123120Simp		splx (s);
2452123120Simp		NG_FREE_M (m);
2453123120Simp		return ENOBUFS;
2454123120Simp	}
2455123120Simp	_IF_ENQUEUE (q, m);
2456123120Simp	IF_UNLOCK (q);
2457123120Simp	cx_start (d);
2458138823Srik	CX_UNLOCK (bd);
2459123120Simp	splx (s);
2460123120Simp	return 0;
2461123120Simp}
2462123120Simp
2463123120Simpstatic int ng_cx_rmnode (node_p node)
2464123120Simp{
2465123120Simp	drv_t *d = NG_NODE_PRIVATE (node);
2466138823Srik	bdrv_t *bd;
2467123120Simp
2468123120Simp	CX_DEBUG (d, ("Rmnode\n"));
2469123120Simp	if (d && d->running) {
2470123120Simp		int s = splhigh ();
2471138823Srik		bd = d->board->sys;
2472138823Srik		CX_LOCK (bd);
2473123120Simp		cx_down (d);
2474138823Srik		CX_UNLOCK (bd);
2475123120Simp		splx (s);
2476123120Simp	}
2477123120Simp#ifdef	KLD_MODULE
2478132464Sjulian	if (node->nd_flags & NGF_REALLY_DIE) {
2479123120Simp		NG_NODE_SET_PRIVATE (node, NULL);
2480123120Simp		NG_NODE_UNREF (node);
2481123120Simp	}
2482132464Sjulian	NG_NODE_REVIVE(node);		/* Persistant node */
2483123120Simp#endif
2484123120Simp	return 0;
2485123120Simp}
2486123120Simp
2487123120Simpstatic void ng_cx_watchdog (void *arg)
2488123120Simp{
2489123120Simp	drv_t *d = arg;
2490123120Simp
2491123120Simp	if (d->timeout == 1)
2492123120Simp		cx_watchdog (d);
2493123120Simp	if (d->timeout)
2494123120Simp		d->timeout--;
2495138823Srik	callout_reset (&d->timeout_handle, hz, ng_cx_watchdog, d);
2496123120Simp}
2497123120Simp
2498123120Simpstatic int ng_cx_connect (hook_p hook)
2499123120Simp{
2500123120Simp	drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2501123120Simp
2502138823Srik	callout_reset (&d->timeout_handle, hz, ng_cx_watchdog, d);
2503123120Simp	return 0;
2504123120Simp}
2505123120Simp
2506123120Simpstatic int ng_cx_disconnect (hook_p hook)
2507123120Simp{
2508123120Simp	drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2509138823Srik	bdrv_t *bd = d->board->sys;
2510123120Simp	int s;
2511123120Simp
2512123120Simp	s = splhigh ();
2513138823Srik	CX_LOCK (bd);
2514123120Simp	if (NG_HOOK_PRIVATE (hook))
2515123120Simp		cx_down (d);
2516138823Srik	CX_UNLOCK (bd);
2517123120Simp	splx (s);
2518138823Srik	/* If we were wait it than it reasserted now, just stop it. */
2519138823Srik	if (!callout_drain (&d->timeout_handle))
2520138823Srik		callout_stop (&d->timeout_handle);
2521123120Simp	return 0;
2522123120Simp}
2523123120Simp#endif /*NETGRAPH*/
2524123120Simp
2525123120Simpstatic int cx_modevent (module_t mod, int type, void *unused)
2526123120Simp{
2527123120Simp	static int load_count = 0;
2528123120Simp
2529138823Srik	if (!debug_mpsafenet && cx_mpsafenet) {
2530138823Srik		printf ("WORNING! Network stack is not MPSAFE. "
2531138823Srik			"Turning off debug.cx.mpsafenet.\n");
2532138823Srik		cx_mpsafenet = 0;
2533138823Srik	}
2534138823Srik	if (cx_mpsafenet)
2535138823Srik		cx_cdevsw.d_flags &= ~D_NEEDGIANT;
2536138823Srik
2537123120Simp	switch (type) {
2538123120Simp	case MOD_LOAD:
2539138823Srik#ifdef NETGRAPH
2540123120Simp		if (ng_newtype (&typestruct))
2541123120Simp			printf ("Failed to register ng_cx\n");
2542123120Simp#endif
2543123120Simp		++load_count;
2544138823Srik
2545138823Srik		callout_init (&timeout_handle, cx_mpsafenet?CALLOUT_MPSAFE:0);
2546138823Srik		callout_reset (&timeout_handle, hz*5, cx_timeout, 0);
2547123120Simp		/* Software interrupt. */
2548151690Sru		swi_add(&tty_intr_event, "cx", cx_softintr, NULL, SWI_TTY,
2549138823Srik		    (cx_mpsafenet?INTR_MPSAFE:0), &cx_fast_ih);
2550123120Simp		break;
2551123120Simp	case MOD_UNLOAD:
2552123120Simp		if (load_count == 1) {
2553123120Simp			printf ("Removing device entry for Sigma\n");
2554138823Srik#ifdef NETGRAPH
2555123120Simp			ng_rmtype (&typestruct);
2556123120Simp#endif
2557123120Simp		}
2558138823Srik		/* If we were wait it than it reasserted now, just stop it. */
2559138823Srik		if (!callout_drain (&timeout_handle))
2560138823Srik			callout_stop (&timeout_handle);
2561151700Sjhb		swi_remove (cx_fast_ih);
2562123120Simp		--load_count;
2563123120Simp		break;
2564123120Simp	case MOD_SHUTDOWN:
2565123120Simp		break;
2566123120Simp	}
2567123120Simp	return 0;
2568123120Simp}
2569123120Simp
2570123120Simp#ifdef NETGRAPH
2571123120Simpstatic struct ng_type typestruct = {
2572129837Srik	.version	= NG_ABI_VERSION,
2573129837Srik	.name		= NG_CX_NODE_TYPE,
2574129837Srik	.constructor	= ng_cx_constructor,
2575129837Srik	.rcvmsg		= ng_cx_rcvmsg,
2576129837Srik	.shutdown	= ng_cx_rmnode,
2577129837Srik	.newhook	= ng_cx_newhook,
2578129837Srik	.connect	= ng_cx_connect,
2579129837Srik	.rcvdata	= ng_cx_rcvdata,
2580130985Srik	.disconnect	= ng_cx_disconnect,
2581123120Simp};
2582123120Simp#endif /*NETGRAPH*/
2583123120Simp
2584123120Simp#ifdef NETGRAPH
2585123120SimpMODULE_DEPEND (ng_cx, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION);
2586123120Simp#else
2587123120SimpMODULE_DEPEND (isa_cx, sppp, 1, 1, 1);
2588123120Simp#endif
2589123120SimpDRIVER_MODULE (cx, isa, cx_isa_driver, cx_devclass, cx_modevent, NULL);
2590138823SrikMODULE_VERSION (cx, 1);
2591