if_ct.c revision 149847
1/*-
2 * Cronyx-Tau adapter driver for FreeBSD.
3 * Supports PPP/HDLC and Cisco/HDLC protocol in synchronous mode,
4 * and asyncronous channels with full modem control.
5 * Keepalive protocol implemented in both Cisco and PPP modes.
6 *
7 * Copyright (C) 1994-2002 Cronyx Engineering.
8 * Author: Serge Vakulenko, <vak@cronyx.ru>
9 *
10 * Copyright (C) 1999-2004 Cronyx Engineering.
11 * Author: Roman Kurakin, <rik@cronyx.ru>
12 *
13 * This software is distributed with NO WARRANTIES, not even the implied
14 * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 *
16 * Authors grant any other persons or organisations a permission to use,
17 * modify and redistribute this software in source and binary forms,
18 * as long as this message is kept with the software, all derivative
19 * works or modified versions.
20 *
21 * Cronyx Id: if_ct.c,v 1.1.2.31 2004/06/23 17:09:13 rik Exp $
22 */
23
24#include <sys/cdefs.h>
25__FBSDID("$FreeBSD: head/sys/dev/ctau/if_ct.c 149847 2005-09-07 09:53:35Z obrien $");
26
27#include <sys/param.h>
28#include <sys/proc.h>
29#include <sys/systm.h>
30#include <sys/kernel.h>
31#include <sys/module.h>
32#include <sys/mbuf.h>
33#include <sys/sockio.h>
34#include <sys/malloc.h>
35#include <sys/socket.h>
36#include <sys/sysctl.h>
37#include <sys/conf.h>
38#include <sys/errno.h>
39#include <sys/tty.h>
40#include <sys/bus.h>
41#include <machine/bus.h>
42#include <sys/rman.h>
43#include <isa/isavar.h>
44#include <sys/interrupt.h>
45#include <vm/vm.h>
46#include <vm/pmap.h>
47#include <net/if.h>
48#include <machine/cpufunc.h>
49#include <machine/cserial.h>
50#include <machine/clock.h>
51#include <machine/resource.h>
52#include <dev/cx/machdep.h>
53#include <dev/ctau/ctddk.h>
54#include <dev/cx/cronyxfw.h>
55#include "opt_ng_cronyx.h"
56#ifdef NETGRAPH_CRONYX
57#   include "opt_netgraph.h"
58#   include <netgraph/ng_message.h>
59#   include <netgraph/netgraph.h>
60#   include <dev/ctau/ng_ct.h>
61#else
62#   include <net/if_types.h>
63#   include <net/if_sppp.h>
64#   define PP_CISCO IFF_LINK2
65#   include <net/bpf.h>
66#endif
67
68#define NCTAU 1
69
70/* If we don't have Cronyx's sppp version, we don't have fr support via sppp */
71#ifndef PP_FR
72#define PP_FR 0
73#endif
74
75#define CT_DEBUG(d,s)	({if (d->chan->debug) {\
76				printf ("%s: ", d->name); printf s;}})
77#define CT_DEBUG2(d,s)	({if (d->chan->debug>1) {\
78				printf ("%s: ", d->name); printf s;}})
79
80#define CT_LOCK_NAME	"ctX"
81
82static	int	ct_mpsafenet = 1;
83TUNABLE_INT("debug.ctau.mpsafenet", &ct_mpsafenet);
84SYSCTL_NODE(_debug, OID_AUTO, ctau, CTLFLAG_RD, 0, "Cronyx Tau-ISA Adapters");
85SYSCTL_INT(_debug_ctau, OID_AUTO, mpsafenet, CTLFLAG_RD, &ct_mpsafenet, 0,
86	"Enable/disable MPSAFE network support for Cronyx Tau-ISA Adapters");
87
88#define CT_LOCK(_bd)		do { \
89				    if (ct_mpsafenet) \
90					mtx_lock (&(_bd)->ct_mtx); \
91				} while (0)
92#define CT_UNLOCK(_bd)		do { \
93				    if (ct_mpsafenet) \
94					mtx_unlock (&(_bd)->ct_mtx); \
95				} while (0)
96#define CT_LOCK_ASSERT(_bd)	do { \
97				    if (ct_mpsafenet) \
98					mtx_assert (&(_bd)->ct_mtx, MA_OWNED); \
99				} while (0)
100
101static void ct_identify		__P((driver_t *, device_t));
102static int ct_probe		__P((device_t));
103static int ct_attach		__P((device_t));
104static int ct_detach		__P((device_t));
105
106static device_method_t ct_isa_methods [] = {
107	DEVMETHOD(device_identify,	ct_identify),
108	DEVMETHOD(device_probe,		ct_probe),
109	DEVMETHOD(device_attach,	ct_attach),
110	DEVMETHOD(device_detach,	ct_detach),
111	{0, 0}
112};
113
114typedef struct _ct_dma_mem_t {
115	unsigned long	phys;
116	void		*virt;
117	size_t		size;
118	bus_dma_tag_t	dmat;
119	bus_dmamap_t	mapp;
120} ct_dma_mem_t;
121
122typedef struct _drv_t {
123	char name [8];
124	ct_chan_t *chan;
125	ct_board_t *board;
126	struct _bdrv_t *bd;
127	ct_dma_mem_t dmamem;
128	int running;
129#ifdef NETGRAPH
130	char	nodename [NG_NODELEN+1];
131	hook_p	hook;
132	hook_p	debug_hook;
133	node_p	node;
134	struct	ifqueue queue;
135	struct	ifqueue hi_queue;
136	short	timeout;
137	struct	callout timeout_handle;
138#else
139	struct	ifqueue queue;
140	struct	ifnet *ifp;
141#endif
142	struct	cdev *devt;
143} drv_t;
144
145typedef struct _bdrv_t {
146	ct_board_t	*board;
147	struct resource	*base_res;
148	struct resource	*drq_res;
149	struct resource	*irq_res;
150	int		base_rid;
151	int		drq_rid;
152	int		irq_rid;
153	void		*intrhand;
154	drv_t		channel [NCHAN];
155	struct mtx	ct_mtx;
156} bdrv_t;
157
158static driver_t ct_isa_driver = {
159	"ct",
160	ct_isa_methods,
161	sizeof (bdrv_t),
162};
163
164static devclass_t ct_devclass;
165
166static void ct_receive (ct_chan_t *c, char *data, int len);
167static void ct_transmit (ct_chan_t *c, void *attachment, int len);
168static void ct_error (ct_chan_t *c, int data);
169static void ct_up (drv_t *d);
170static void ct_start (drv_t *d);
171static void ct_down (drv_t *d);
172static void ct_watchdog (drv_t *d);
173#ifdef NETGRAPH
174extern struct ng_type typestruct;
175#else
176static void ct_ifstart (struct ifnet *ifp);
177static void ct_tlf (struct sppp *sp);
178static void ct_tls (struct sppp *sp);
179static void ct_ifwatchdog (struct ifnet *ifp);
180static int ct_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data);
181static void ct_initialize (void *softc);
182#endif
183
184static ct_board_t *adapter [NCTAU];
185static drv_t *channel [NCTAU*NCHAN];
186static struct callout led_timo [NCTAU];
187static struct callout timeout_handle;
188
189static int ct_open (struct cdev *dev, int oflags, int devtype, struct thread *td)
190{
191	drv_t *d;
192
193	if (minor(dev) >= NCTAU*NCHAN || ! (d = channel[minor(dev)]))
194		return ENXIO;
195
196	CT_DEBUG2 (d, ("ct_open\n"));
197	return 0;
198}
199
200static int ct_close (struct cdev *dev, int fflag, int devtype, struct thread *td)
201{
202	drv_t *d = channel [minor(dev)];
203
204	if (!d)
205		return 0;
206
207	CT_DEBUG2 (d, ("ct_close\n"));
208	return 0;
209}
210
211static int ct_modem_status (ct_chan_t *c)
212{
213	drv_t *d = c->sys;
214	bdrv_t *bd;
215	int status, s;
216
217	if (!d)
218		return 0;
219
220	bd = d->bd;
221
222	status = d->running ? TIOCM_LE : 0;
223	s = splimp ();
224	CT_LOCK (bd);
225	if (ct_get_cd  (c)) status |= TIOCM_CD;
226	if (ct_get_cts (c)) status |= TIOCM_CTS;
227	if (ct_get_dsr (c)) status |= TIOCM_DSR;
228	if (c->dtr)	    status |= TIOCM_DTR;
229	if (c->rts)	    status |= TIOCM_RTS;
230	CT_UNLOCK (bd);
231	splx (s);
232	return status;
233}
234
235/*
236 * Process an ioctl request on /dev/cronyx/ctauN.
237 */
238static int ct_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
239{
240	drv_t *d = channel [minor (dev)];
241	bdrv_t *bd;
242	ct_chan_t *c;
243	struct serial_statistics *st;
244	struct e1_statistics *opte1;
245	int error, s;
246	char mask[16];
247
248	if (!d || !d->chan)
249		return 0;
250
251	bd = d->bd;
252	c = d->chan;
253
254	switch (cmd) {
255	case SERIAL_GETREGISTERED:
256		bzero (mask, sizeof(mask));
257		for (s=0; s<NCTAU*NCHAN; ++s)
258			if (channel [s])
259				mask [s/8] |= 1 << (s & 7);
260		bcopy (mask, data, sizeof (mask));
261		return 0;
262
263#ifndef NETGRAPH
264	case SERIAL_GETPROTO:
265		strcpy ((char*)data, (IFP2SP(d->ifp)->pp_flags & PP_FR) ? "fr" :
266			(d->ifp->if_flags & PP_CISCO) ? "cisco" : "ppp");
267		return 0;
268
269	case SERIAL_SETPROTO:
270		/* Only for superuser! */
271		error = suser (td);
272		if (error)
273			return error;
274		if (d->ifp->if_drv_flags & IFF_DRV_RUNNING)
275			return EBUSY;
276		if (! strcmp ("cisco", (char*)data)) {
277			IFP2SP(d->ifp)->pp_flags &= ~(PP_FR);
278			IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE;
279			d->ifp->if_flags |= PP_CISCO;
280		} else if (! strcmp ("fr", (char*)data)) {
281			d->ifp->if_flags &= ~(PP_CISCO);
282			IFP2SP(d->ifp)->pp_flags |= PP_FR | PP_KEEPALIVE;
283		} else if (! strcmp ("ppp", (char*)data)) {
284			IFP2SP(d->ifp)->pp_flags &= ~(PP_FR | PP_KEEPALIVE);
285			d->ifp->if_flags &= ~(PP_CISCO);
286		} else
287			return EINVAL;
288		return 0;
289
290	case SERIAL_GETKEEPALIVE:
291		if ((IFP2SP(d->ifp)->pp_flags & PP_FR) ||
292			(d->ifp->if_flags & PP_CISCO))
293			return EINVAL;
294		*(int*)data = (IFP2SP(d->ifp)->pp_flags & PP_KEEPALIVE) ? 1 : 0;
295		return 0;
296
297	case SERIAL_SETKEEPALIVE:
298		/* Only for superuser! */
299		error = suser (td);
300		if (error)
301			return error;
302		if ((IFP2SP(d->ifp)->pp_flags & PP_FR) ||
303			(d->ifp->if_flags & PP_CISCO))
304			return EINVAL;
305		if (*(int*)data)
306			IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE;
307		else
308			IFP2SP(d->ifp)->pp_flags &= ~PP_KEEPALIVE;
309		return 0;
310#endif /*NETGRAPH*/
311
312	case SERIAL_GETMODE:
313		*(int*)data = SERIAL_HDLC;
314		return 0;
315
316	case SERIAL_GETCFG:
317		if (c->mode == M_HDLC)
318			return EINVAL;
319		switch (ct_get_config (c->board)) {
320		default:    *(char*)data = 'a'; break;
321		case CFG_B: *(char*)data = 'b'; break;
322		case CFG_C: *(char*)data = 'c'; break;
323		}
324		return 0;
325
326	case SERIAL_SETCFG:
327		/* Only for superuser! */
328		error = suser (td);
329		if (error)
330			return error;
331		if (c->mode == M_HDLC)
332			return EINVAL;
333		s = splimp ();
334		CT_LOCK (bd);
335		switch (*(char*)data) {
336		case 'a': ct_set_config (c->board, CFG_A); break;
337		case 'b': ct_set_config (c->board, CFG_B); break;
338		case 'c': ct_set_config (c->board, CFG_C); break;
339		}
340		CT_UNLOCK (bd);
341		splx (s);
342		return 0;
343
344	case SERIAL_GETSTAT:
345		st = (struct serial_statistics*) data;
346		st->rintr  = c->rintr;
347		st->tintr  = c->tintr;
348		st->mintr  = c->mintr;
349		st->ibytes = c->ibytes;
350		st->ipkts  = c->ipkts;
351		st->ierrs  = c->ierrs;
352		st->obytes = c->obytes;
353		st->opkts  = c->opkts;
354		st->oerrs  = c->oerrs;
355		return 0;
356
357	case SERIAL_GETESTAT:
358		opte1 = (struct e1_statistics*)data;
359		opte1->status	   = c->status;
360		opte1->cursec	   = c->cursec;
361		opte1->totsec	   = c->totsec + c->cursec;
362
363		opte1->currnt.bpv   = c->currnt.bpv;
364		opte1->currnt.fse   = c->currnt.fse;
365		opte1->currnt.crce  = c->currnt.crce;
366		opte1->currnt.rcrce = c->currnt.rcrce;
367		opte1->currnt.uas   = c->currnt.uas;
368		opte1->currnt.les   = c->currnt.les;
369		opte1->currnt.es    = c->currnt.es;
370		opte1->currnt.bes   = c->currnt.bes;
371		opte1->currnt.ses   = c->currnt.ses;
372		opte1->currnt.oofs  = c->currnt.oofs;
373		opte1->currnt.css   = c->currnt.css;
374		opte1->currnt.dm    = c->currnt.dm;
375
376		opte1->total.bpv   = c->total.bpv   + c->currnt.bpv;
377		opte1->total.fse   = c->total.fse   + c->currnt.fse;
378		opte1->total.crce  = c->total.crce  + c->currnt.crce;
379		opte1->total.rcrce = c->total.rcrce + c->currnt.rcrce;
380		opte1->total.uas   = c->total.uas   + c->currnt.uas;
381		opte1->total.les   = c->total.les   + c->currnt.les;
382		opte1->total.es	   = c->total.es    + c->currnt.es;
383		opte1->total.bes   = c->total.bes   + c->currnt.bes;
384		opte1->total.ses   = c->total.ses   + c->currnt.ses;
385		opte1->total.oofs  = c->total.oofs  + c->currnt.oofs;
386		opte1->total.css   = c->total.css   + c->currnt.css;
387		opte1->total.dm	   = c->total.dm    + c->currnt.dm;
388		for (s=0; s<48; ++s) {
389			opte1->interval[s].bpv   = c->interval[s].bpv;
390			opte1->interval[s].fse   = c->interval[s].fse;
391			opte1->interval[s].crce  = c->interval[s].crce;
392			opte1->interval[s].rcrce = c->interval[s].rcrce;
393			opte1->interval[s].uas   = c->interval[s].uas;
394			opte1->interval[s].les   = c->interval[s].les;
395			opte1->interval[s].es	 = c->interval[s].es;
396			opte1->interval[s].bes   = c->interval[s].bes;
397			opte1->interval[s].ses   = c->interval[s].ses;
398			opte1->interval[s].oofs  = c->interval[s].oofs;
399			opte1->interval[s].css   = c->interval[s].css;
400			opte1->interval[s].dm	 = c->interval[s].dm;
401		}
402		return 0;
403
404	case SERIAL_CLRSTAT:
405		/* Only for superuser! */
406		error = suser (td);
407		if (error)
408			return error;
409		c->rintr = 0;
410		c->tintr = 0;
411		c->mintr = 0;
412		c->ibytes = 0;
413		c->ipkts = 0;
414		c->ierrs = 0;
415		c->obytes = 0;
416		c->opkts = 0;
417		c->oerrs = 0;
418		bzero (&c->currnt, sizeof (c->currnt));
419		bzero (&c->total, sizeof (c->total));
420		bzero (c->interval, sizeof (c->interval));
421		return 0;
422
423	case SERIAL_GETBAUD:
424		*(long*)data = ct_get_baud(c);
425		return 0;
426
427	case SERIAL_SETBAUD:
428		/* Only for superuser! */
429		error = suser (td);
430		if (error)
431			return error;
432		s = splimp ();
433		CT_LOCK (bd);
434		ct_set_baud (c, *(long*)data);
435		CT_UNLOCK (bd);
436		splx (s);
437		return 0;
438
439	case SERIAL_GETLOOP:
440		*(int*)data = ct_get_loop (c);
441		return 0;
442
443	case SERIAL_SETLOOP:
444		/* Only for superuser! */
445		error = suser (td);
446		if (error)
447			return error;
448		s = splimp ();
449		CT_LOCK (bd);
450		ct_set_loop (c, *(int*)data);
451		CT_UNLOCK (bd);
452		splx (s);
453		return 0;
454
455	case SERIAL_GETDPLL:
456		if (c->mode == M_E1 || c->mode == M_G703)
457			return EINVAL;
458		*(int*)data = ct_get_dpll (c);
459		return 0;
460
461	case SERIAL_SETDPLL:
462		/* Only for superuser! */
463		error = suser (td);
464		if (error)
465			return error;
466		if (c->mode == M_E1 || c->mode == M_G703)
467			return EINVAL;
468		s = splimp ();
469		CT_LOCK (bd);
470		ct_set_dpll (c, *(int*)data);
471		CT_UNLOCK (bd);
472		splx (s);
473		return 0;
474
475	case SERIAL_GETNRZI:
476		if (c->mode == M_E1 || c->mode == M_G703)
477			return EINVAL;
478		*(int*)data = ct_get_nrzi (c);
479		return 0;
480
481	case SERIAL_SETNRZI:
482		/* Only for superuser! */
483		error = suser (td);
484		if (error)
485			return error;
486		if (c->mode == M_E1 || c->mode == M_G703)
487			return EINVAL;
488		s = splimp ();
489		CT_LOCK (bd);
490		ct_set_nrzi (c, *(int*)data);
491		CT_UNLOCK (bd);
492		splx (s);
493		return 0;
494
495	case SERIAL_GETDEBUG:
496		*(int*)data = c->debug;
497		return 0;
498
499	case SERIAL_SETDEBUG:
500		/* Only for superuser! */
501		error = suser (td);
502		if (error)
503			return error;
504		c->debug = *(int*)data;
505#ifndef	NETGRAPH
506		if (d->chan->debug)
507			d->ifp->if_flags |= IFF_DEBUG;
508		else
509			d->ifp->if_flags &= (~IFF_DEBUG);
510#endif
511		return 0;
512
513	case SERIAL_GETHIGAIN:
514		if (c->mode != M_E1)
515			return EINVAL;
516		*(int*)data = ct_get_higain (c);
517		return 0;
518
519	case SERIAL_SETHIGAIN:
520		/* Only for superuser! */
521		error = suser (td);
522		if (error)
523			return error;
524		s = splimp ();
525		CT_LOCK (bd);
526		ct_set_higain (c, *(int*)data);
527		CT_UNLOCK (bd);
528		splx (s);
529		return 0;
530
531	case SERIAL_GETPHONY:
532		CT_DEBUG2 (d, ("ioctl: getphony\n"));
533		if (c->mode != M_E1)
534			return EINVAL;
535		*(int*)data = c->gopt.phony;
536		return 0;
537
538	case SERIAL_SETPHONY:
539		CT_DEBUG2 (d, ("ioctl: setphony\n"));
540		if (c->mode != M_E1)
541			return EINVAL;
542		/* Only for superuser! */
543		error = suser (td);
544		if (error)
545			return error;
546		s = splimp ();
547		CT_LOCK (bd);
548		ct_set_phony (c, *(int*)data);
549		CT_UNLOCK (bd);
550		splx (s);
551		return 0;
552
553	case SERIAL_GETCLK:
554		if (c->mode != M_E1 && c->mode != M_G703)
555			return EINVAL;
556		switch (ct_get_clk(c)) {
557		default:	 *(int*)data = E1CLK_INTERNAL;		break;
558		case GCLK_RCV:   *(int*)data = E1CLK_RECEIVE;		break;
559		case GCLK_RCLKO: *(int*)data = c->num ?
560			E1CLK_RECEIVE_CHAN0 : E1CLK_RECEIVE_CHAN1;	break;
561		}
562		return 0;
563
564	case SERIAL_SETCLK:
565		/* Only for superuser! */
566		error = suser (td);
567		if (error)
568			return error;
569		s = splimp ();
570		CT_LOCK (bd);
571		switch (*(int*)data) {
572		default:		    ct_set_clk (c, GCLK_INT);   break;
573		case E1CLK_RECEIVE:	    ct_set_clk (c, GCLK_RCV);   break;
574		case E1CLK_RECEIVE_CHAN0:
575		case E1CLK_RECEIVE_CHAN1:
576					    ct_set_clk (c, GCLK_RCLKO); break;
577		}
578		CT_UNLOCK (bd);
579		splx (s);
580		return 0;
581
582	case SERIAL_GETTIMESLOTS:
583		if (c->mode != M_E1)
584			return EINVAL;
585		*(long*)data = ct_get_ts (c);
586		return 0;
587
588	case SERIAL_SETTIMESLOTS:
589		/* Only for superuser! */
590		error = suser (td);
591		if (error)
592			return error;
593		s = splimp ();
594		CT_LOCK (bd);
595		ct_set_ts (c, *(long*)data);
596		CT_UNLOCK (bd);
597		splx (s);
598		return 0;
599
600	case SERIAL_GETSUBCHAN:
601		if (c->mode != M_E1)
602			return EINVAL;
603		*(long*)data = ct_get_subchan (c->board);
604		return 0;
605
606	case SERIAL_SETSUBCHAN:
607		/* Only for superuser! */
608		error = suser (td);
609		if (error)
610			return error;
611		s = splimp ();
612		CT_LOCK (bd);
613		ct_set_subchan (c->board, *(long*)data);
614		CT_UNLOCK (bd);
615		splx (s);
616		return 0;
617
618	case SERIAL_GETINVCLK:
619	case SERIAL_GETINVTCLK:
620		if (c->mode == M_E1 || c->mode == M_G703)
621			return EINVAL;
622		*(int*)data = ct_get_invtxc (c);
623		return 0;
624
625	case SERIAL_GETINVRCLK:
626		if (c->mode == M_E1 || c->mode == M_G703)
627			return EINVAL;
628		*(int*)data = ct_get_invrxc (c);
629		return 0;
630
631	case SERIAL_SETINVCLK:
632	case SERIAL_SETINVTCLK:
633		/* Only for superuser! */
634		error = suser (td);
635		if (error)
636			return error;
637		if (c->mode == M_E1 || c->mode == M_G703)
638			return EINVAL;
639		s = splimp ();
640		CT_LOCK (bd);
641		ct_set_invtxc (c, *(int*)data);
642		CT_UNLOCK (bd);
643		splx (s);
644		return 0;
645
646	case SERIAL_SETINVRCLK:
647		/* Only for superuser! */
648		error = suser (td);
649		if (error)
650			return error;
651		if (c->mode == M_E1 || c->mode == M_G703)
652			return EINVAL;
653		s = splimp ();
654		CT_LOCK (bd);
655		ct_set_invrxc (c, *(int*)data);
656		CT_UNLOCK (bd);
657		splx (s);
658		return 0;
659
660	case SERIAL_GETLEVEL:
661		if (c->mode != M_G703)
662			return EINVAL;
663		s = splimp ();
664		CT_LOCK (bd);
665		*(int*)data = ct_get_lq (c);
666		CT_UNLOCK (bd);
667		splx (s);
668		return 0;
669
670	case TIOCSDTR:	/* Set DTR */
671		s = splimp ();
672		CT_LOCK (bd);
673		ct_set_dtr (c, 1);
674		CT_UNLOCK (bd);
675		splx (s);
676		return 0;
677
678	case TIOCCDTR:	/* Clear DTR */
679		s = splimp ();
680		CT_LOCK (bd);
681		ct_set_dtr (c, 0);
682		CT_UNLOCK (bd);
683		splx (s);
684		return 0;
685
686	case TIOCMSET:	/* Set DTR/RTS */
687		s = splimp ();
688		CT_LOCK (bd);
689		ct_set_dtr (c, (*(int*)data & TIOCM_DTR) ? 1 : 0);
690		ct_set_rts (c, (*(int*)data & TIOCM_RTS) ? 1 : 0);
691		CT_UNLOCK (bd);
692		splx (s);
693		return 0;
694
695	case TIOCMBIS:	/* Add DTR/RTS */
696		s = splimp ();
697		CT_LOCK (bd);
698		if (*(int*)data & TIOCM_DTR) ct_set_dtr (c, 1);
699		if (*(int*)data & TIOCM_RTS) ct_set_rts (c, 1);
700		CT_UNLOCK (bd);
701		splx (s);
702		return 0;
703
704	case TIOCMBIC:	/* Clear DTR/RTS */
705		s = splimp ();
706		CT_LOCK (bd);
707		if (*(int*)data & TIOCM_DTR) ct_set_dtr (c, 0);
708		if (*(int*)data & TIOCM_RTS) ct_set_rts (c, 0);
709		CT_UNLOCK (bd);
710		splx (s);
711		return 0;
712
713	case TIOCMGET:	/* Get modem status */
714		*(int*)data = ct_modem_status (c);
715		return 0;
716	}
717	return ENOTTY;
718}
719
720
721static struct cdevsw ct_cdevsw = {
722	.d_version  = D_VERSION,
723	.d_open     = ct_open,
724	.d_close    = ct_close,
725	.d_ioctl    = ct_ioctl,
726	.d_name     = "ct",
727	.d_flags    = D_NEEDGIANT,
728};
729
730/*
731 * Print the mbuf chain, for debug purposes only.
732 */
733static void printmbuf (struct mbuf *m)
734{
735	printf ("mbuf:");
736	for (; m; m=m->m_next) {
737		if (m->m_flags & M_PKTHDR)
738			printf (" HDR %d:", m->m_pkthdr.len);
739		if (m->m_flags & M_EXT)
740			printf (" EXT:");
741		printf (" %d", m->m_len);
742	}
743	printf ("\n");
744}
745
746/*
747 * Make an mbuf from data.
748 */
749static struct mbuf *makembuf (void *buf, u_int len)
750{
751	struct mbuf *m;
752
753	MGETHDR (m, M_DONTWAIT, MT_DATA);
754	if (! m)
755		return 0;
756	MCLGET (m, M_DONTWAIT);
757	if (! (m->m_flags & M_EXT)) {
758		m_freem (m);
759		return 0;
760	}
761	m->m_pkthdr.len = m->m_len = len;
762	bcopy (buf, mtod (m, caddr_t), len);
763	return m;
764}
765
766static void ct_timeout (void *arg)
767{
768	drv_t *d;
769	int s, i, k;
770
771	for (i = 0; i < NCTAU; ++i) {
772		if (adapter[i] == NULL)
773			continue;
774		for (k = 0; k < NCHAN; k++) {
775			d = channel[i * NCHAN + k];
776			if (! d)
777				continue;
778			if (d->chan->mode != M_G703)
779				continue;
780			s = splimp ();
781			CT_LOCK ((bdrv_t *)d->bd);
782			ct_g703_timer (d->chan);
783			CT_UNLOCK ((bdrv_t *)d->bd);
784			splx (s);
785		}
786	}
787
788	callout_reset (&timeout_handle, hz, ct_timeout, 0);
789}
790
791static void ct_led_off (void *arg)
792{
793	ct_board_t *b = arg;
794	bdrv_t *bd = ((drv_t *)b->chan->sys)->bd;
795	int s = splimp ();
796
797	CT_LOCK (bd);
798	ct_led (b, 0);
799	CT_UNLOCK (bd);
800	splx (s);
801}
802
803/*
804 * Activate interupt handler from DDK.
805 */
806static void ct_intr (void *arg)
807{
808	bdrv_t *bd = arg;
809	ct_board_t *b = bd->board;
810#ifndef NETGRAPH
811	int i;
812#endif
813	int s = splimp ();
814
815	CT_LOCK (bd);
816	/* Turn LED on. */
817	ct_led (b, 1);
818
819	ct_int_handler (b);
820
821	/* Turn LED off 50 msec later. */
822	callout_reset (&led_timo[b->num], hz/20, ct_led_off, b);
823	CT_UNLOCK (bd);
824	splx (s);
825
826#ifndef NETGRAPH
827	/* Pass packets in a lock-free state */
828	for (i = 0; i < NCHAN && b->chan[i].type; i++) {
829		drv_t *d = b->chan[i].sys;
830		struct mbuf *m;
831		if (!d || !d->running)
832			continue;
833		while (_IF_QLEN(&d->queue)) {
834			IF_DEQUEUE (&d->queue,m);
835			if (!m)
836				continue;
837			sppp_input (d->ifp, m);
838		}
839	}
840#endif
841}
842
843static int probe_irq (ct_board_t *b, int irq)
844{
845	int mask, busy, cnt;
846
847	/* Clear pending irq, if any. */
848	ct_probe_irq (b, -irq);
849	DELAY (100);
850	for (cnt=0; cnt<5; ++cnt) {
851		/* Get the mask of pending irqs, assuming they are busy.
852		 * Activate the adapter on given irq. */
853		busy = ct_probe_irq (b, irq);
854		DELAY (1000);
855
856		/* Get the mask of active irqs.
857		 * Deactivate our irq. */
858		mask = ct_probe_irq (b, -irq);
859		DELAY (100);
860		if ((mask & ~busy) == 1 << irq) {
861			ct_probe_irq (b, 0);
862			/* printf ("ct%d: irq %d ok, mask=0x%04x, busy=0x%04x\n",
863				b->num, irq, mask, busy); */
864			return 1;
865		}
866	}
867	/* printf ("ct%d: irq %d not functional, mask=0x%04x, busy=0x%04x\n",
868		b->num, irq, mask, busy); */
869	ct_probe_irq (b, 0);
870	return 0;
871}
872
873static	short porttab [] = {
874		0x200, 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0,
875		0x300, 0x320, 0x340, 0x360, 0x380, 0x3a0, 0x3c0, 0x3e0, 0
876	};
877static	char dmatab [] = { 7, 6, 5, 0 };
878static	char irqtab [] = { 5, 10, 11, 7, 3, 15, 12, 0 };
879
880static int ct_is_free_res (device_t dev, int rid, int type, u_long start,
881	u_long end, u_long count)
882{
883	struct resource *res;
884
885	if (!(res = bus_alloc_resource (dev, type, &rid, start, end, count,
886	    RF_ALLOCATED)))
887		return 0;
888
889	bus_release_resource (dev, type, rid, res);
890
891	return 1;
892}
893
894static void ct_identify (driver_t *driver, device_t dev)
895{
896	u_long iobase, rescount;
897	int devcount;
898	device_t *devices;
899	device_t child;
900	devclass_t my_devclass;
901	int i, k;
902
903	if ((my_devclass = devclass_find ("ct")) == NULL)
904		return;
905
906	devclass_get_devices (my_devclass, &devices, &devcount);
907
908	if (devcount == 0) {
909		/* We should find all devices by our self. We could alter other
910		 * devices, but we don't have a choise
911		 */
912		for (i = 0; (iobase = porttab [i]) != 0; i++) {
913			if (!ct_is_free_res (dev, 0, SYS_RES_IOPORT,
914			    iobase, iobase + NPORT, NPORT))
915				continue;
916			if (ct_probe_board (iobase, -1, -1) == 0)
917				continue;
918
919			devcount++;
920			child = BUS_ADD_CHILD (dev, ISA_ORDER_SPECULATIVE, "ct",
921			    -1);
922
923			if (child == NULL)
924				return;
925
926			device_set_desc_copy (child, "Cronyx Tau-ISA");
927			device_set_driver (child, driver);
928			bus_set_resource (child, SYS_RES_IOPORT, 0,
929			    iobase, NPORT);
930
931			if (devcount >= NCTAU)
932				break;
933		}
934	} else {
935		static	short porttab [] = {
936			0x200, 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0,
937			0x300, 0x320, 0x340, 0x360, 0x380, 0x3a0, 0x3c0, 0x3e0, 0
938		};
939		/* Lets check user choise.
940		 */
941		for (k = 0; k < devcount; k++) {
942			if (bus_get_resource (devices[k], SYS_RES_IOPORT, 0,
943			    &iobase, &rescount) != 0)
944				continue;
945
946			for (i = 0; porttab [i] != 0; i++) {
947				if (porttab [i] != iobase)
948					continue;
949
950				if (!ct_is_free_res (devices[k], 0, SYS_RES_IOPORT,
951				    iobase, iobase + NPORT, NPORT))
952					continue;
953
954				if (ct_probe_board (iobase, -1, -1) == 0)
955					continue;
956				porttab [i] = -1;
957				device_set_desc_copy (devices[k], "Cronyx Tau-ISA");
958				break;
959			}
960			if (porttab [i] == 0) {
961				device_delete_child (
962				    device_get_parent (devices[k]),
963				    devices [k]);
964				devices[k] = 0;
965				continue;
966			}
967		}
968		for (k = 0; k < devcount; k++) {
969			if (devices[k] == 0)
970				continue;
971			if (bus_get_resource (devices[k], SYS_RES_IOPORT, 0,
972			    &iobase, &rescount) == 0)
973				continue;
974			for (i = 0; (iobase = porttab [i]) != 0; i++) {
975				if (porttab [i] == -1)
976					continue;
977				if (!ct_is_free_res (devices[k], 0, SYS_RES_IOPORT,
978				    iobase, iobase + NPORT, NPORT))
979					continue;
980				if (ct_probe_board (iobase, -1, -1) == 0)
981					continue;
982
983				bus_set_resource (devices[k], SYS_RES_IOPORT, 0,
984				    iobase, NPORT);
985				porttab [i] = -1;
986				device_set_desc_copy (devices[k], "Cronyx Tau-ISA");
987				break;
988			}
989			if (porttab [i] == 0) {
990				device_delete_child (
991				    device_get_parent (devices[k]),
992				    devices [k]);
993			}
994		}
995		free (devices, M_TEMP);
996	}
997
998	return;
999}
1000
1001static int ct_probe (device_t dev)
1002{
1003	int unit = device_get_unit (dev);
1004	u_long iobase, rescount;
1005
1006	if (!device_get_desc (dev) ||
1007	    strcmp (device_get_desc (dev), "Cronyx Tau-ISA"))
1008		return ENXIO;
1009
1010/*	KASSERT ((bd != NULL), ("ct%d: NULL device softc\n", unit));*/
1011	if (bus_get_resource (dev, SYS_RES_IOPORT, 0, &iobase, &rescount) != 0) {
1012		printf ("ct%d: Couldn't get IOPORT\n", unit);
1013		return ENXIO;
1014	}
1015
1016	if (!ct_is_free_res (dev, 0, SYS_RES_IOPORT,
1017	    iobase, iobase + NPORT, NPORT)) {
1018		printf ("ct%d: Resource IOPORT isn't free\n", unit);
1019		return ENXIO;
1020	}
1021
1022	if (!ct_probe_board (iobase, -1, -1)) {
1023		printf ("ct%d: probing for Tau-ISA at %lx faild\n", unit, iobase);
1024		return ENXIO;
1025	}
1026
1027	return 0;
1028}
1029
1030static void
1031ct_bus_dmamap_addr (void *arg, bus_dma_segment_t *segs, int nseg, int error)
1032{
1033	unsigned long *addr;
1034
1035	if (error)
1036		return;
1037
1038	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
1039	addr = arg;
1040	*addr = segs->ds_addr;
1041}
1042
1043static int
1044ct_bus_dma_mem_alloc (int bnum, int cnum, ct_dma_mem_t *dmem)
1045{
1046	int error;
1047
1048	error = bus_dma_tag_create (NULL, 16, 0, BUS_SPACE_MAXADDR_24BIT,
1049		BUS_SPACE_MAXADDR, NULL, NULL, dmem->size, 1,
1050		dmem->size, 0, NULL, NULL, &dmem->dmat);
1051	if (error) {
1052		if (cnum >= 0)	printf ("ct%d-%d: ", bnum, cnum);
1053		else		printf ("ct%d: ", bnum);
1054		printf ("couldn't allocate tag for dma memory\n");
1055 		return 0;
1056	}
1057	error = bus_dmamem_alloc (dmem->dmat, (void **)&dmem->virt,
1058		BUS_DMA_NOWAIT | BUS_DMA_ZERO, &dmem->mapp);
1059	if (error) {
1060		if (cnum >= 0)	printf ("ct%d-%d: ", bnum, cnum);
1061		else		printf ("ct%d: ", bnum);
1062		printf ("couldn't allocate mem for dma memory\n");
1063		bus_dma_tag_destroy (dmem->dmat);
1064 		return 0;
1065	}
1066	error = bus_dmamap_load (dmem->dmat, dmem->mapp, dmem->virt,
1067		dmem->size, ct_bus_dmamap_addr, &dmem->phys, 0);
1068	if (error) {
1069		if (cnum >= 0)	printf ("ct%d-%d: ", bnum, cnum);
1070		else		printf ("ct%d: ", bnum);
1071		printf ("couldn't load mem map for dma memory\n");
1072		bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
1073		bus_dma_tag_destroy (dmem->dmat);
1074 		return 0;
1075	}
1076	return 1;
1077}
1078
1079static void
1080ct_bus_dma_mem_free (ct_dma_mem_t *dmem)
1081{
1082	bus_dmamap_unload (dmem->dmat, dmem->mapp);
1083	bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
1084	bus_dma_tag_destroy (dmem->dmat);
1085}
1086
1087/*
1088 * The adapter is present, initialize the driver structures.
1089 */
1090static int ct_attach (device_t dev)
1091{
1092	bdrv_t *bd = device_get_softc (dev);
1093	u_long iobase, drq, irq, rescount;
1094	int unit = device_get_unit (dev);
1095	char *ct_ln = CT_LOCK_NAME;
1096	ct_board_t *b;
1097	ct_chan_t *c;
1098	drv_t *d;
1099	int i;
1100	int s;
1101
1102	KASSERT ((bd != NULL), ("ct%d: NULL device softc\n", unit));
1103
1104	bus_get_resource (dev, SYS_RES_IOPORT, 0, &iobase, &rescount);
1105	bd->base_rid = 0;
1106	bd->base_res = bus_alloc_resource (dev, SYS_RES_IOPORT, &bd->base_rid,
1107		iobase, iobase + NPORT, NPORT, RF_ACTIVE);
1108	if (! bd->base_res) {
1109		printf ("ct%d: cannot alloc base address\n", unit);
1110		return ENXIO;
1111	}
1112
1113	if (bus_get_resource (dev, SYS_RES_DRQ, 0, &drq, &rescount) != 0) {
1114		for (i = 0; (drq = dmatab [i]) != 0; i++) {
1115			if (!ct_is_free_res (dev, 0, SYS_RES_DRQ,
1116			    drq, drq + 1, 1))
1117				continue;
1118			bus_set_resource (dev, SYS_RES_DRQ, 0, drq, 1);
1119			break;
1120		}
1121
1122		if (dmatab[i] == 0) {
1123			bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
1124				bd->base_res);
1125			printf ("ct%d: Couldn't get DRQ\n", unit);
1126			return ENXIO;
1127		}
1128	}
1129
1130	bd->drq_rid = 0;
1131	bd->drq_res = bus_alloc_resource (dev, SYS_RES_DRQ, &bd->drq_rid,
1132		drq, drq + 1, 1, RF_ACTIVE);
1133	if (! bd->drq_res) {
1134		printf ("ct%d: cannot allocate drq\n", unit);
1135		bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
1136			bd->base_res);
1137		return ENXIO;
1138	}
1139
1140	if (bus_get_resource (dev, SYS_RES_IRQ, 0, &irq, &rescount) != 0) {
1141		for (i = 0; (irq = irqtab [i]) != 0; i++) {
1142			if (!ct_is_free_res (dev, 0, SYS_RES_IRQ,
1143			    irq, irq + 1, 1))
1144				continue;
1145			bus_set_resource (dev, SYS_RES_IRQ, 0, irq, 1);
1146			break;
1147		}
1148
1149		if (irqtab[i] == 0) {
1150			bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
1151				bd->drq_res);
1152			bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
1153				bd->base_res);
1154			printf ("ct%d: Couldn't get IRQ\n", unit);
1155			return ENXIO;
1156		}
1157	}
1158
1159	bd->irq_rid = 0;
1160	bd->irq_res = bus_alloc_resource (dev, SYS_RES_IRQ, &bd->irq_rid,
1161		irq, irq + 1, 1, RF_ACTIVE);
1162	if (! bd->irq_res) {
1163		printf ("ct%d: Couldn't allocate irq\n", unit);
1164		bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
1165			bd->drq_res);
1166		bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
1167			bd->base_res);
1168		return ENXIO;
1169	}
1170
1171	b = malloc (sizeof (ct_board_t), M_DEVBUF, M_WAITOK);
1172	if (!b) {
1173		printf ("ct:%d: Couldn't allocate memory\n", unit);
1174		return (ENXIO);
1175	}
1176	adapter[unit] = b;
1177	bzero (b, sizeof(ct_board_t));
1178
1179	if (! ct_open_board (b, unit, iobase, irq, drq)) {
1180		printf ("ct%d: error loading firmware\n", unit);
1181		free (b, M_DEVBUF);
1182		bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid,
1183			bd->irq_res);
1184		bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
1185			bd->drq_res);
1186		bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
1187			bd->base_res);
1188 		return ENXIO;
1189	}
1190
1191	bd->board = b;
1192
1193	ct_ln[2] = '0' + unit;
1194	mtx_init (&bd->ct_mtx, ct_ln, MTX_NETWORK_LOCK, MTX_DEF|MTX_RECURSE);
1195	if (! probe_irq (b, irq)) {
1196		printf ("ct%d: irq %ld not functional\n", unit, irq);
1197		bd->board = 0;
1198		adapter [unit] = 0;
1199		free (b, M_DEVBUF);
1200		bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid,
1201			bd->irq_res);
1202		bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
1203			bd->drq_res);
1204		bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
1205			bd->base_res);
1206		mtx_destroy (&bd->ct_mtx);
1207 		return ENXIO;
1208	}
1209
1210	callout_init (&led_timo[unit], ct_mpsafenet ? CALLOUT_MPSAFE : 0);
1211	s = splimp ();
1212	if (bus_setup_intr (dev, bd->irq_res,
1213			   INTR_TYPE_NET|(ct_mpsafenet?INTR_MPSAFE:0),
1214			   ct_intr, bd, &bd->intrhand)) {
1215		printf ("ct%d: Can't setup irq %ld\n", unit, irq);
1216		bd->board = 0;
1217		adapter [unit] = 0;
1218		free (b, M_DEVBUF);
1219		bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid,
1220			bd->irq_res);
1221		bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
1222			bd->drq_res);
1223		bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
1224			bd->base_res);
1225		mtx_destroy (&bd->ct_mtx);
1226		splx (s);
1227 		return ENXIO;
1228	}
1229
1230	CT_LOCK (bd);
1231	ct_init_board (b, b->num, b->port, irq, drq, b->type, b->osc);
1232	ct_setup_board (b, 0, 0, 0);
1233	CT_UNLOCK (bd);
1234
1235	printf ("ct%d: <Cronyx-%s>, clock %s MHz\n", b->num, b->name,
1236		b->osc == 20000000 ? "20" : "16.384");
1237
1238	for (c = b->chan; c < b->chan + NCHAN; ++c) {
1239		d = &bd->channel[c->num];
1240		d->dmamem.size = sizeof(ct_buf_t);
1241		if (! ct_bus_dma_mem_alloc (unit, c->num, &d->dmamem))
1242			continue;
1243		d->board = b;
1244		d->chan = c;
1245		d->bd = bd;
1246		c->sys = d;
1247		channel [b->num*NCHAN + c->num] = d;
1248		sprintf (d->name, "ct%d.%d", b->num, c->num);
1249
1250#ifdef NETGRAPH
1251		if (ng_make_node_common (&typestruct, &d->node) != 0) {
1252			printf ("%s: cannot make common node\n", d->name);
1253			channel [b->num*NCHAN + c->num] = 0;
1254			c->sys = 0;
1255			ct_bus_dma_mem_free (&d->dmamem);
1256			continue;
1257		}
1258		NG_NODE_SET_PRIVATE (d->node, d);
1259		sprintf (d->nodename, "%s%d", NG_CT_NODE_TYPE,
1260			 c->board->num*NCHAN + c->num);
1261		if (ng_name_node (d->node, d->nodename)) {
1262			printf ("%s: cannot name node\n", d->nodename);
1263			NG_NODE_UNREF (d->node);
1264			channel [b->num*NCHAN + c->num] = 0;
1265			c->sys = 0;
1266			ct_bus_dma_mem_free (&d->dmamem);
1267			continue;
1268		}
1269		d->queue.ifq_maxlen = IFQ_MAXLEN;
1270		d->hi_queue.ifq_maxlen = IFQ_MAXLEN;
1271		mtx_init (&d->queue.ifq_mtx, "ct_queue", NULL, MTX_DEF);
1272		mtx_init (&d->hi_queue.ifq_mtx, "ct_queue_hi", NULL, MTX_DEF);
1273		callout_init (&d->timeout_handle,
1274			     ct_mpsafenet ? CALLOUT_MPSAFE : 0);
1275#else /*NETGRAPH*/
1276		d->ifp = if_alloc(IFT_PPP);
1277		if (d->ifp == NULL) {
1278			printf ("%s: cannot if_alloc common interface\n",
1279			    d->name);
1280			channel [b->num*NCHAN + c->num] = 0;
1281			c->sys = 0;
1282			ct_bus_dma_mem_free (&d->dmamem);
1283			continue;
1284		}
1285		d->ifp->if_softc	= d;
1286		if_initname (d->ifp, "ct", b->num * NCHAN + c->num);
1287		d->ifp->if_mtu		= PP_MTU;
1288		d->ifp->if_flags	= IFF_POINTOPOINT | IFF_MULTICAST;
1289		if (!ct_mpsafenet)
1290			d->ifp->if_flags |= IFF_NEEDSGIANT;
1291		d->ifp->if_ioctl	= ct_sioctl;
1292		d->ifp->if_start	= ct_ifstart;
1293		d->ifp->if_watchdog	= ct_ifwatchdog;
1294		d->ifp->if_init		= ct_initialize;
1295		d->queue.ifq_maxlen	= NBUF;
1296		mtx_init (&d->queue.ifq_mtx, "ct_queue", NULL, MTX_DEF);
1297		sppp_attach (d->ifp);
1298		if_attach (d->ifp);
1299		IFP2SP(d->ifp)->pp_tlf	= ct_tlf;
1300		IFP2SP(d->ifp)->pp_tls	= ct_tls;
1301		/* If BPF is in the kernel, call the attach for it.
1302		 * Header size is 4 bytes. */
1303		bpfattach (d->ifp, DLT_PPP, 4);
1304#endif /*NETGRAPH*/
1305		CT_LOCK (bd);
1306		ct_start_chan (c, d->dmamem.virt, d->dmamem.phys);
1307		ct_register_receive (c, &ct_receive);
1308		ct_register_transmit (c, &ct_transmit);
1309		ct_register_error (c, &ct_error);
1310		CT_UNLOCK (bd);
1311		d->devt = make_dev (&ct_cdevsw, b->num*NCHAN+c->num, UID_ROOT,
1312				GID_WHEEL, 0600, "ct%d", b->num*NCHAN+c->num);
1313	}
1314	splx (s);
1315
1316	return 0;
1317}
1318
1319static int ct_detach (device_t dev)
1320{
1321	bdrv_t *bd = device_get_softc (dev);
1322	ct_board_t *b = bd->board;
1323	ct_chan_t *c;
1324	int s;
1325
1326	KASSERT (mtx_initialized (&bd->ct_mtx), ("ct mutex not initialized"));
1327
1328	s = splimp ();
1329	CT_LOCK (bd);
1330	/* Check if the device is busy (open). */
1331	for (c = b->chan; c < b->chan + NCHAN; ++c) {
1332		drv_t *d = (drv_t*) c->sys;
1333
1334		if (!d || !d->chan->type)
1335			continue;
1336
1337		if (d->running) {
1338			CT_UNLOCK (bd);
1339			splx (s);
1340			return EBUSY;
1341		}
1342	}
1343
1344	/* Deactivate the timeout routine. */
1345	callout_stop (&led_timo[b->num]);
1346
1347	CT_UNLOCK (bd);
1348
1349	bus_teardown_intr (dev, bd->irq_res, bd->intrhand);
1350	bus_deactivate_resource (dev, SYS_RES_IRQ, bd->irq_rid, bd->irq_res);
1351	bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid, bd->irq_res);
1352
1353	bus_deactivate_resource (dev, SYS_RES_DRQ, bd->drq_rid, bd->drq_res);
1354	bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid, bd->drq_res);
1355
1356	bus_deactivate_resource (dev, SYS_RES_IOPORT, bd->base_rid, bd->irq_res);
1357	bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid, bd->base_res);
1358
1359	CT_LOCK (bd);
1360	ct_close_board (b);
1361	CT_UNLOCK (bd);
1362
1363	/* Detach the interfaces, free buffer memory. */
1364	for (c = b->chan; c < b->chan + NCHAN; ++c) {
1365		drv_t *d = (drv_t*) c->sys;
1366
1367		if (!d || !d->chan->type)
1368			continue;
1369
1370#ifdef NETGRAPH
1371		if (d->node) {
1372			ng_rmnode_self (d->node);
1373			NG_NODE_UNREF (d->node);
1374			d->node = NULL;
1375		}
1376		mtx_destroy (&d->queue.ifq_mtx);
1377		mtx_destroy (&d->hi_queue.ifq_mtx);
1378#else
1379		/* Detach from the packet filter list of interfaces. */
1380		bpfdetach (d->ifp);
1381
1382		/* Detach from the sync PPP list. */
1383		sppp_detach (d->ifp);
1384
1385		if_detach (d->ifp);
1386		if_free (d->ifp);
1387		IF_DRAIN (&d->queue);
1388		mtx_destroy (&d->queue.ifq_mtx);
1389#endif
1390		destroy_dev (d->devt);
1391	}
1392
1393	CT_LOCK (bd);
1394	ct_led_off (b);
1395	CT_UNLOCK (bd);
1396	callout_drain (&led_timo[b->num]);
1397	splx (s);
1398
1399	s = splimp ();
1400	for (c = b->chan; c < b->chan + NCHAN; ++c) {
1401		drv_t *d = (drv_t*) c->sys;
1402
1403		if (!d || !d->chan->type)
1404			continue;
1405
1406		/* Deallocate buffers. */
1407		ct_bus_dma_mem_free (&d->dmamem);
1408	}
1409	bd->board = 0;
1410	adapter [b->num] = 0;
1411	free (b, M_DEVBUF);
1412	splx (s);
1413
1414	mtx_destroy (&bd->ct_mtx);
1415
1416	return 0;
1417}
1418
1419#ifndef NETGRAPH
1420static void ct_ifstart (struct ifnet *ifp)
1421{
1422	drv_t *d = ifp->if_softc;
1423	bdrv_t *bd = d->bd;
1424
1425	CT_LOCK (bd);
1426	ct_start (d);
1427	CT_UNLOCK (bd);
1428}
1429
1430static void ct_ifwatchdog (struct ifnet *ifp)
1431{
1432	drv_t *d = ifp->if_softc;
1433
1434	ct_watchdog (d);
1435}
1436
1437static void ct_tlf (struct sppp *sp)
1438{
1439	drv_t *d = SP2IFP(sp)->if_softc;
1440
1441	CT_DEBUG (d, ("ct_tlf\n"));
1442/*	ct_set_dtr (d->chan, 0);*/
1443/*	ct_set_rts (d->chan, 0);*/
1444	if (!(sp->pp_flags & PP_FR) && !(d->ifp->if_flags & PP_CISCO))
1445		sp->pp_down (sp);
1446}
1447
1448static void ct_tls (struct sppp *sp)
1449{
1450	drv_t *d = SP2IFP(sp)->if_softc;
1451
1452	CT_DEBUG (d, ("ct_tls\n"));
1453	if (!(sp->pp_flags & PP_FR) && !(d->ifp->if_flags & PP_CISCO))
1454		sp->pp_up (sp);
1455}
1456
1457/*
1458 * Initialization of interface.
1459 * Ii seems to be never called by upper level.
1460 */
1461static void ct_initialize (void *softc)
1462{
1463	drv_t *d = softc;
1464
1465	CT_DEBUG (d, ("ct_initialize\n"));
1466}
1467
1468/*
1469 * Process an ioctl request.
1470 */
1471static int ct_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data)
1472{
1473	drv_t *d = ifp->if_softc;
1474	bdrv_t *bd = d->bd;
1475	int error, s, was_up, should_be_up;
1476
1477	was_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
1478	error = sppp_ioctl (ifp, cmd, data);
1479	if (error)
1480		return error;
1481
1482	if (! (ifp->if_flags & IFF_DEBUG))
1483		d->chan->debug = 0;
1484	else if (! d->chan->debug)
1485		d->chan->debug = 1;
1486
1487	switch (cmd) {
1488	default:	   CT_DEBUG2 (d, ("ioctl 0x%lx\n", cmd)); return 0;
1489	case SIOCADDMULTI: CT_DEBUG2 (d, ("SIOCADDMULTI\n"));     return 0;
1490	case SIOCDELMULTI: CT_DEBUG2 (d, ("SIOCDELMULTI\n"));     return 0;
1491	case SIOCSIFFLAGS: CT_DEBUG2 (d, ("SIOCSIFFLAGS\n"));     break;
1492	case SIOCSIFADDR:  CT_DEBUG2 (d, ("SIOCSIFADDR\n"));      break;
1493	}
1494
1495	/* We get here only in case of SIFFLAGS or SIFADDR. */
1496	s = splimp ();
1497	CT_LOCK (bd);
1498	should_be_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
1499	if (! was_up && should_be_up) {
1500		/* Interface goes up -- start it. */
1501		ct_up (d);
1502		ct_start (d);
1503	} else if (was_up && ! should_be_up) {
1504		/* Interface is going down -- stop it. */
1505		/* if ((IFP2SP(d->ifp)->pp_flags & PP_FR) || (ifp->if_flags & PP_CISCO))*/
1506		ct_down (d);
1507	}
1508	CT_UNLOCK (bd);
1509	splx (s);
1510	return 0;
1511}
1512#endif /*NETGRAPH*/
1513
1514/*
1515 * Stop the interface.  Called on splimp().
1516 */
1517static void ct_down (drv_t *d)
1518{
1519	int s = splimp ();
1520	CT_DEBUG (d, ("ct_down\n"));
1521	ct_set_dtr (d->chan, 0);
1522	ct_set_rts (d->chan, 0);
1523	d->running = 0;
1524	splx (s);
1525}
1526
1527/*
1528 * Start the interface.  Called on splimp().
1529 */
1530static void ct_up (drv_t *d)
1531{
1532	int s = splimp ();
1533	CT_DEBUG (d, ("ct_up\n"));
1534	ct_set_dtr (d->chan, 1);
1535	ct_set_rts (d->chan, 1);
1536	d->running = 1;
1537	splx (s);
1538}
1539
1540/*
1541 * Start output on the (slave) interface.  Get another datagram to send
1542 * off of the interface queue, and copy it to the interface
1543 * before starting the output.
1544 */
1545static void ct_send (drv_t *d)
1546{
1547	struct mbuf *m;
1548	u_short len;
1549
1550	CT_DEBUG2 (d, ("ct_send, tn=%d\n", d->chan->tn));
1551
1552	/* No output if the interface is down. */
1553	if (! d->running)
1554		return;
1555
1556	/* No output if the modem is off. */
1557	if (! ct_get_dsr (d->chan) && !ct_get_loop (d->chan))
1558		return;
1559
1560	while (ct_buf_free (d->chan)) {
1561		/* Get the packet to send. */
1562#ifdef NETGRAPH
1563		IF_DEQUEUE (&d->hi_queue, m);
1564		if (! m)
1565			IF_DEQUEUE (&d->queue, m);
1566#else
1567		m = sppp_dequeue (d->ifp);
1568#endif
1569		if (! m)
1570			return;
1571#ifndef NETGRAPH
1572		if (d->ifp->if_bpf)
1573			BPF_MTAP (d->ifp, m);
1574#endif
1575		len = m_length (m, NULL);
1576		if (! m->m_next)
1577			ct_send_packet (d->chan, (u_char*)mtod (m, caddr_t),
1578				len, 0);
1579		else {
1580			m_copydata (m, 0, len, d->chan->tbuf[d->chan->te]);
1581			ct_send_packet (d->chan, d->chan->tbuf[d->chan->te],
1582				len, 0);
1583		}
1584		m_freem (m);
1585
1586		/* Set up transmit timeout, if the transmit ring is not empty.
1587		 * Transmit timeout is 10 seconds. */
1588#ifdef NETGRAPH
1589		d->timeout = 10;
1590#else
1591		d->ifp->if_timer = 10;
1592#endif
1593	}
1594#ifndef NETGRAPH
1595	d->ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1596#endif
1597}
1598
1599/*
1600 * Start output on the interface.
1601 * Always called on splimp().
1602 */
1603static void ct_start (drv_t *d)
1604{
1605	int s = splimp ();
1606
1607	if (d->running) {
1608		if (! d->chan->dtr)
1609			ct_set_dtr (d->chan, 1);
1610		if (! d->chan->rts)
1611			ct_set_rts (d->chan, 1);
1612		ct_send (d);
1613	}
1614
1615	splx (s);
1616}
1617
1618/*
1619 * Handle transmit timeouts.
1620 * Recover after lost transmit interrupts.
1621 * Always called on splimp().
1622 */
1623static void ct_watchdog (drv_t *d)
1624{
1625	bdrv_t *bd = d->bd;
1626	int s;
1627
1628	s = splimp ();
1629	CT_LOCK (bd);
1630	CT_DEBUG (d, ("device timeout\n"));
1631	if (d->running) {
1632		ct_setup_chan (d->chan);
1633		ct_start_chan (d->chan, 0, 0);
1634		ct_set_dtr (d->chan, 1);
1635		ct_set_rts (d->chan, 1);
1636		ct_start (d);
1637	}
1638	CT_UNLOCK (bd);
1639	splx (s);
1640}
1641
1642/*
1643 * Transmit callback function.
1644 */
1645static void ct_transmit (ct_chan_t *c, void *attachment, int len)
1646{
1647	drv_t *d = c->sys;
1648
1649	if (!d)
1650		return;
1651#ifdef NETGRAPH
1652	d->timeout = 0;
1653#else
1654	++d->ifp->if_opackets;
1655	d->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1656	d->ifp->if_timer = 0;
1657#endif
1658	ct_start (d);
1659}
1660
1661/*
1662 * Process the received packet.
1663 */
1664static void ct_receive (ct_chan_t *c, char *data, int len)
1665{
1666	drv_t *d = c->sys;
1667	struct mbuf *m;
1668#ifdef NETGRAPH
1669	int error;
1670#endif
1671
1672	if (!d || !d->running)
1673		return;
1674
1675	m = makembuf (data, len);
1676	if (! m) {
1677		CT_DEBUG (d, ("no memory for packet\n"));
1678#ifndef NETGRAPH
1679		++d->ifp->if_iqdrops;
1680#endif
1681		return;
1682	}
1683	if (c->debug > 1)
1684		printmbuf (m);
1685#ifdef NETGRAPH
1686	m->m_pkthdr.rcvif = 0;
1687	NG_SEND_DATA_ONLY (error, d->hook, m);
1688#else
1689	++d->ifp->if_ipackets;
1690	m->m_pkthdr.rcvif = d->ifp;
1691	/* Check if there's a BPF listener on this interface.
1692	 * If so, hand off the raw packet to bpf. */
1693	if (d->ifp->if_bpf)
1694		BPF_TAP (d->ifp, data, len);
1695	IF_ENQUEUE (&d->queue, m);
1696#endif
1697}
1698
1699/*
1700 * Error callback function.
1701 */
1702static void ct_error (ct_chan_t *c, int data)
1703{
1704	drv_t *d = c->sys;
1705
1706	if (!d)
1707		return;
1708
1709	switch (data) {
1710	case CT_FRAME:
1711		CT_DEBUG (d, ("frame error\n"));
1712#ifndef NETGRAPH
1713		++d->ifp->if_ierrors;
1714#endif
1715		break;
1716	case CT_CRC:
1717		CT_DEBUG (d, ("crc error\n"));
1718#ifndef NETGRAPH
1719		++d->ifp->if_ierrors;
1720#endif
1721		break;
1722	case CT_OVERRUN:
1723		CT_DEBUG (d, ("overrun error\n"));
1724#ifndef NETGRAPH
1725		++d->ifp->if_collisions;
1726		++d->ifp->if_ierrors;
1727#endif
1728		break;
1729	case CT_OVERFLOW:
1730		CT_DEBUG (d, ("overflow error\n"));
1731#ifndef NETGRAPH
1732		++d->ifp->if_ierrors;
1733#endif
1734		break;
1735	case CT_UNDERRUN:
1736		CT_DEBUG (d, ("underrun error\n"));
1737#ifdef NETGRAPH
1738		d->timeout = 0;
1739#else
1740		++d->ifp->if_oerrors;
1741		d->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1742		d->ifp->if_timer = 0;
1743#endif
1744		ct_start (d);
1745		break;
1746	default:
1747		CT_DEBUG (d, ("error #%d\n", data));
1748	}
1749}
1750
1751#ifdef NETGRAPH
1752static int ng_ct_constructor (node_p node)
1753{
1754	drv_t *d = NG_NODE_PRIVATE (node);
1755	CT_DEBUG (d, ("Constructor\n"));
1756	return EINVAL;
1757}
1758
1759static int ng_ct_newhook (node_p node, hook_p hook, const char *name)
1760{
1761	int s;
1762	drv_t *d = NG_NODE_PRIVATE (node);
1763
1764	if (!d)
1765		return EINVAL;
1766
1767	bdrv_t *bd = d->bd;
1768
1769	/* Attach debug hook */
1770	if (strcmp (name, NG_CT_HOOK_DEBUG) == 0) {
1771		NG_HOOK_SET_PRIVATE (hook, NULL);
1772		d->debug_hook = hook;
1773		return 0;
1774	}
1775
1776	/* Check for raw hook */
1777	if (strcmp (name, NG_CT_HOOK_RAW) != 0)
1778		return EINVAL;
1779
1780	NG_HOOK_SET_PRIVATE (hook, d);
1781	d->hook = hook;
1782	s = splimp ();
1783	CT_LOCK (bd);
1784	ct_up (d);
1785	CT_UNLOCK (bd);
1786	splx (s);
1787	return 0;
1788}
1789
1790static char *format_timeslots (u_long s)
1791{
1792	static char buf [100];
1793	char *p = buf;
1794	int i;
1795
1796	for (i=1; i<32; ++i)
1797		if ((s >> i) & 1) {
1798			int prev = (i > 1)  & (s >> (i-1));
1799			int next = (i < 31) & (s >> (i+1));
1800
1801			if (prev) {
1802				if (next)
1803					continue;
1804				*p++ = '-';
1805			} else if (p > buf)
1806				*p++ = ',';
1807
1808			if (i >= 10)
1809				*p++ = '0' + i / 10;
1810			*p++ = '0' + i % 10;
1811		}
1812	*p = 0;
1813	return buf;
1814}
1815
1816static int print_modems (char *s, ct_chan_t *c, int need_header)
1817{
1818	int status = ct_modem_status (c);
1819	int length = 0;
1820
1821	if (need_header)
1822		length += sprintf (s + length, "  LE   DTR  DSR  RTS  CTS  CD\n");
1823	length += sprintf (s + length, "%4s %4s %4s %4s %4s %4s\n",
1824		status & TIOCM_LE  ? "On" : "-",
1825		status & TIOCM_DTR ? "On" : "-",
1826		status & TIOCM_DSR ? "On" : "-",
1827		status & TIOCM_RTS ? "On" : "-",
1828		status & TIOCM_CTS ? "On" : "-",
1829		status & TIOCM_CD  ? "On" : "-");
1830	return length;
1831}
1832
1833static int print_stats (char *s, ct_chan_t *c, int need_header)
1834{
1835	struct serial_statistics st;
1836	int length = 0;
1837
1838	st.rintr  = c->rintr;
1839	st.tintr  = c->tintr;
1840	st.mintr  = c->mintr;
1841	st.ibytes = c->ibytes;
1842	st.ipkts  = c->ipkts;
1843	st.ierrs  = c->ierrs;
1844	st.obytes = c->obytes;
1845	st.opkts  = c->opkts;
1846	st.oerrs  = c->oerrs;
1847	if (need_header)
1848		length += sprintf (s + length, "  Rintr   Tintr   Mintr   Ibytes   Ipkts   Ierrs   Obytes   Opkts   Oerrs\n");
1849	length += sprintf (s + length, "%7ld %7ld %7ld %8ld %7ld %7ld %8ld %7ld %7ld\n",
1850		st.rintr, st.tintr, st.mintr, st.ibytes, st.ipkts,
1851		st.ierrs, st.obytes, st.opkts, st.oerrs);
1852	return length;
1853}
1854
1855static char *format_e1_status (u_char status)
1856{
1857	static char buf [80];
1858
1859	if (status & E1_NOALARM)
1860		return "Ok";
1861	buf[0] = 0;
1862	if (status & E1_LOS)     strcat (buf, ",LOS");
1863	if (status & E1_AIS)     strcat (buf, ",AIS");
1864	if (status & E1_LOF)     strcat (buf, ",LOF");
1865	if (status & E1_LOMF)    strcat (buf, ",LOMF");
1866	if (status & E1_FARLOF)  strcat (buf, ",FARLOF");
1867	if (status & E1_AIS16)   strcat (buf, ",AIS16");
1868	if (status & E1_FARLOMF) strcat (buf, ",FARLOMF");
1869	if (status & E1_TSTREQ)  strcat (buf, ",TSTREQ");
1870	if (status & E1_TSTERR)  strcat (buf, ",TSTERR");
1871	if (buf[0] == ',')
1872		return buf+1;
1873	return "Unknown";
1874}
1875
1876static int print_frac (char *s, int leftalign, u_long numerator, u_long divider)
1877{
1878	int n, length = 0;
1879
1880	if (numerator < 1 || divider < 1) {
1881		length += sprintf (s+length, leftalign ? "/-   " : "    -");
1882		return length;
1883	}
1884	n = (int) (0.5 + 1000.0 * numerator / divider);
1885	if (n < 1000) {
1886		length += sprintf (s+length, leftalign ? "/.%-3d" : " .%03d", n);
1887		return length;
1888	}
1889	*(s + length) = leftalign ? '/' : ' ';
1890	length ++;
1891
1892	if     (n >= 1000000) n = (n+500) / 1000 * 1000;
1893	else if (n >= 100000)  n = (n+50)  / 100 * 100;
1894	else if (n >= 10000)   n = (n+5)   / 10 * 10;
1895
1896	switch (n) {
1897	case 1000:    length += printf (s+length, ".999"); return length;
1898	case 10000:   n = 9990;   break;
1899	case 100000:  n = 99900;  break;
1900	case 1000000: n = 999000; break;
1901	}
1902	if (n < 10000)	      length += sprintf (s+length, "%d.%d", n/1000, n/10%100);
1903	else if (n < 100000)  length += sprintf (s+length, "%d.%d", n/1000, n/100%10);
1904	else if (n < 1000000) length += sprintf (s+length, "%d.", n/1000);
1905	else		      length += sprintf (s+length, "%d", n/1000);
1906
1907	return length;
1908}
1909
1910static int print_e1_stats (char *s, ct_chan_t *c)
1911{
1912	struct e1_counters total;
1913	u_long totsec;
1914	int length = 0;
1915
1916	totsec		= c->totsec + c->cursec;
1917	total.bpv	= c->total.bpv   + c->currnt.bpv;
1918	total.fse	= c->total.fse   + c->currnt.fse;
1919	total.crce	= c->total.crce  + c->currnt.crce;
1920	total.rcrce	= c->total.rcrce + c->currnt.rcrce;
1921	total.uas	= c->total.uas   + c->currnt.uas;
1922	total.les	= c->total.les   + c->currnt.les;
1923	total.es	= c->total.es    + c->currnt.es;
1924	total.bes	= c->total.bes   + c->currnt.bes;
1925	total.ses	= c->total.ses   + c->currnt.ses;
1926	total.oofs	= c->total.oofs  + c->currnt.oofs;
1927	total.css	= c->total.css   + c->currnt.css;
1928	total.dm	= c->total.dm    + c->currnt.dm;
1929
1930	length += sprintf (s + length, " Unav/Degr  Bpv/Fsyn  CRC/RCRC  Err/Lerr  Sev/Bur   Oof/Slp  Status\n");
1931
1932	/* Unavailable seconds, degraded minutes */
1933	length += print_frac (s + length, 0, c->currnt.uas, c->cursec);
1934	length += print_frac (s + length, 1, 60 * c->currnt.dm, c->cursec);
1935
1936	/* Bipolar violations, frame sync errors */
1937	length += print_frac (s + length, 0, c->currnt.bpv, c->cursec);
1938	length += print_frac (s + length, 1, c->currnt.fse, c->cursec);
1939
1940	/* CRC errors, remote CRC errors (E-bit) */
1941	length += print_frac (s + length, 0, c->currnt.crce, c->cursec);
1942	length += print_frac (s + length, 1, c->currnt.rcrce, c->cursec);
1943
1944	/* Errored seconds, line errored seconds */
1945	length += print_frac (s + length, 0, c->currnt.es, c->cursec);
1946	length += print_frac (s + length, 1, c->currnt.les, c->cursec);
1947
1948	/* Severely errored seconds, burst errored seconds */
1949	length += print_frac (s + length, 0, c->currnt.ses, c->cursec);
1950	length += print_frac (s + length, 1, c->currnt.bes, c->cursec);
1951
1952	/* Out of frame seconds, controlled slip seconds */
1953	length += print_frac (s + length, 0, c->currnt.oofs, c->cursec);
1954	length += print_frac (s + length, 1, c->currnt.css, c->cursec);
1955
1956	length += sprintf (s + length, " %s\n", format_e1_status (c->status));
1957
1958	/* Print total statistics. */
1959	length += print_frac (s + length, 0, total.uas, totsec);
1960	length += print_frac (s + length, 1, 60 * total.dm, totsec);
1961
1962	length += print_frac (s + length, 0, total.bpv, totsec);
1963	length += print_frac (s + length, 1, total.fse, totsec);
1964
1965	length += print_frac (s + length, 0, total.crce, totsec);
1966	length += print_frac (s + length, 1, total.rcrce, totsec);
1967
1968	length += print_frac (s + length, 0, total.es, totsec);
1969	length += print_frac (s + length, 1, total.les, totsec);
1970
1971	length += print_frac (s + length, 0, total.ses, totsec);
1972	length += print_frac (s + length, 1, total.bes, totsec);
1973
1974	length += print_frac (s + length, 0, total.oofs, totsec);
1975	length += print_frac (s + length, 1, total.css, totsec);
1976
1977	length += sprintf (s + length, " -- Total\n");
1978	return length;
1979}
1980
1981static int print_chan (char *s, ct_chan_t *c)
1982{
1983	drv_t *d = c->sys;
1984	bdrv_t *bd = d->bd;
1985	int length = 0;
1986
1987	length += sprintf (s + length, "ct%d", c->board->num * NCHAN + c->num);
1988	if (d->chan->debug)
1989		length += sprintf (s + length, " debug=%d", d->chan->debug);
1990
1991	switch (ct_get_config (c->board)) {
1992	case CFG_A:	length += sprintf (s + length, " cfg=A");	break;
1993	case CFG_B:	length += sprintf (s + length, " cfg=B");	break;
1994	case CFG_C:	length += sprintf (s + length, " cfg=C");	break;
1995	default:	length += sprintf (s + length, " cfg=unknown"); break;
1996	}
1997
1998	if (ct_get_baud (c))
1999		length += sprintf (s + length, " %ld", ct_get_baud (c));
2000	else
2001		length += sprintf (s + length, " extclock");
2002
2003	if (c->mode == M_E1 || c->mode == M_G703)
2004		switch (ct_get_clk(c)) {
2005		case GCLK_INT   : length += sprintf (s + length, " syn=int");     break;
2006		case GCLK_RCV   : length += sprintf (s + length, " syn=rcv");     break;
2007		case GCLK_RCLKO  : length += sprintf (s + length, " syn=xrcv");    break;
2008		}
2009	if (c->mode == M_HDLC) {
2010		length += sprintf (s + length, " dpll=%s",   ct_get_dpll (c)   ? "on" : "off");
2011		length += sprintf (s + length, " nrzi=%s",   ct_get_nrzi (c)   ? "on" : "off");
2012		length += sprintf (s + length, " invtclk=%s", ct_get_invtxc (c) ? "on" : "off");
2013		length += sprintf (s + length, " invrclk=%s", ct_get_invrxc (c) ? "on" : "off");
2014	}
2015	if (c->mode == M_E1)
2016		length += sprintf (s + length, " higain=%s", ct_get_higain (c)? "on" : "off");
2017
2018	length += sprintf (s + length, " loop=%s", ct_get_loop (c) ? "on" : "off");
2019
2020	if (c->mode == M_E1)
2021		length += sprintf (s + length, " ts=%s", format_timeslots (ct_get_ts(c)));
2022	if (c->mode == M_E1 && ct_get_config (c->board) != CFG_A)
2023		length += sprintf (s + length, " pass=%s", format_timeslots (ct_get_subchan(c->board)));
2024	if (c->mode == M_G703) {
2025		int lq, x;
2026
2027		x = splimp ();
2028		CT_LOCK (bd);
2029		lq = ct_get_lq (c);
2030		CT_UNLOCK (bd);
2031		splx (x);
2032		length += sprintf (s + length, " (level=-%.1fdB)", lq / 10.0);
2033	}
2034	length += sprintf (s + length, "\n");
2035	return length;
2036}
2037
2038static int ng_ct_rcvmsg (node_p node, item_p item, hook_p lasthook)
2039{
2040	drv_t *d = NG_NODE_PRIVATE (node);
2041	struct ng_mesg *msg;
2042	struct ng_mesg *resp = NULL;
2043	int error = 0;
2044
2045	if (!d)
2046		return EINVAL;
2047
2048	CT_DEBUG (d, ("Rcvmsg\n"));
2049	NGI_GET_MSG (item, msg);
2050	switch (msg->header.typecookie) {
2051	default:
2052		error = EINVAL;
2053		break;
2054
2055	case NGM_CT_COOKIE:
2056		printf ("Don't forget to implement\n");
2057		error = EINVAL;
2058		break;
2059
2060	case NGM_GENERIC_COOKIE:
2061		switch (msg->header.cmd) {
2062		default:
2063			error = EINVAL;
2064			break;
2065
2066		case NGM_TEXT_STATUS: {
2067			char *s;
2068			int l = 0;
2069			int dl = sizeof (struct ng_mesg) + 730;
2070
2071			NG_MKRESPONSE (resp, msg, dl, M_NOWAIT);
2072			if (! resp) {
2073				error = ENOMEM;
2074				break;
2075			}
2076			s = (resp)->data;
2077			l += print_chan (s + l, d->chan);
2078			l += print_stats (s + l, d->chan, 1);
2079			l += print_modems (s + l, d->chan, 1);
2080			l += print_e1_stats (s + l, d->chan);
2081			strncpy ((resp)->header.cmdstr, "status", NG_CMDSTRLEN);
2082			}
2083			break;
2084		}
2085		break;
2086	}
2087	NG_RESPOND_MSG (error, node, item, resp);
2088	NG_FREE_MSG (msg);
2089	return error;
2090}
2091
2092static int ng_ct_rcvdata (hook_p hook, item_p item)
2093{
2094	drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE(hook));
2095	struct mbuf *m;
2096	struct ng_tag_prio *ptag;
2097	bdrv_t *bd;
2098	struct ifqueue *q;
2099	int s;
2100
2101	if (!d)
2102		return ENETDOWN;
2103
2104	bd = d->bd;
2105	NGI_GET_M (item, m);
2106	NG_FREE_ITEM (item);
2107	if (! NG_HOOK_PRIVATE (hook) || ! d) {
2108		NG_FREE_M (m);
2109		return ENETDOWN;
2110	}
2111
2112	/* Check for high priority data */
2113	if ((ptag = (struct ng_tag_prio *)m_tag_locate(m, NGM_GENERIC_COOKIE,
2114	    NG_TAG_PRIO, NULL)) != NULL && (ptag->priority > NG_PRIO_CUTOFF) )
2115		q = &d->hi_queue;
2116	else
2117		q = &d->queue;
2118
2119	s = splimp ();
2120	CT_LOCK (bd);
2121	IF_LOCK (q);
2122	if (_IF_QFULL (q)) {
2123		_IF_DROP (q);
2124		IF_UNLOCK (q);
2125		CT_UNLOCK (bd);
2126		splx (s);
2127		NG_FREE_M (m);
2128		return ENOBUFS;
2129	}
2130	_IF_ENQUEUE (q, m);
2131	IF_UNLOCK (q);
2132	ct_start (d);
2133	CT_UNLOCK (bd);
2134	splx (s);
2135	return 0;
2136}
2137
2138static int ng_ct_rmnode (node_p node)
2139{
2140	drv_t *d = NG_NODE_PRIVATE (node);
2141	bdrv_t *bd;
2142
2143	CT_DEBUG (d, ("Rmnode\n"));
2144	if (d && d->running) {
2145		bd = d->bd;
2146		int s = splimp ();
2147		CT_LOCK (bd);
2148		ct_down (d);
2149		CT_UNLOCK (bd);
2150		splx (s);
2151	}
2152#ifdef	KLD_MODULE
2153	if (node->nd_flags & NGF_REALLY_DIE) {
2154		NG_NODE_SET_PRIVATE (node, NULL);
2155		NG_NODE_UNREF (node);
2156	}
2157	NG_NODE_REVIVE(node);		/* Persistant node */
2158#endif
2159	return 0;
2160}
2161
2162static void ng_ct_watchdog (void *arg)
2163{
2164	drv_t *d = arg;
2165
2166	if (!d)
2167		return;
2168
2169	if (d->timeout == 1)
2170		ct_watchdog (d);
2171	if (d->timeout)
2172		d->timeout--;
2173	callout_reset (&d->timeout_handle, hz, ng_ct_watchdog, d);
2174}
2175
2176static int ng_ct_connect (hook_p hook)
2177{
2178	drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2179
2180	if (!d)
2181		return 0;
2182
2183	callout_reset (&d->timeout_handle, hz, ng_ct_watchdog, d);
2184	return 0;
2185}
2186
2187static int ng_ct_disconnect (hook_p hook)
2188{
2189	drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2190	bdrv_t *bd;
2191
2192	if (!d)
2193		return 0;
2194
2195	bd = d->bd;
2196
2197	CT_LOCK (bd);
2198	if (NG_HOOK_PRIVATE (hook))
2199		ct_down (d);
2200	CT_UNLOCK (bd);
2201	/* If we were wait it than it reasserted now, just stop it. */
2202	if (!callout_drain (&d->timeout_handle))
2203		callout_stop (&d->timeout_handle);
2204	return 0;
2205}
2206#endif
2207
2208static int ct_modevent (module_t mod, int type, void *unused)
2209{
2210	static int load_count = 0;
2211
2212	if (!debug_mpsafenet && ct_mpsafenet) {
2213		printf ("WORNING! Network stack is not MPSAFE. "
2214			"Turning off debug.ct.mpsafenet.\n");
2215		ct_mpsafenet = 0;
2216	}
2217	if (ct_mpsafenet)
2218		ct_cdevsw.d_flags &= ~D_NEEDGIANT;
2219
2220	switch (type) {
2221	case MOD_LOAD:
2222#ifdef NETGRAPH
2223		if (ng_newtype (&typestruct))
2224			printf ("Failed to register ng_ct\n");
2225#endif
2226		++load_count;
2227		callout_init (&timeout_handle, ct_mpsafenet?CALLOUT_MPSAFE:0);
2228		callout_reset (&timeout_handle, hz*5, ct_timeout, 0);
2229		break;
2230	case MOD_UNLOAD:
2231		if (load_count == 1) {
2232			printf ("Removing device entry for Tau-ISA\n");
2233#ifdef NETGRAPH
2234			ng_rmtype (&typestruct);
2235#endif
2236		}
2237		/* If we were wait it than it reasserted now, just stop it. */
2238		if (!callout_drain (&timeout_handle))
2239			callout_stop (&timeout_handle);
2240		--load_count;
2241		break;
2242	case MOD_SHUTDOWN:
2243		break;
2244	}
2245	return 0;
2246}
2247
2248#ifdef NETGRAPH
2249static struct ng_type typestruct = {
2250	.version	= NG_ABI_VERSION,
2251	.name		= NG_CT_NODE_TYPE,
2252	.constructor	= ng_ct_constructor,
2253	.rcvmsg		= ng_ct_rcvmsg,
2254	.shutdown	= ng_ct_rmnode,
2255	.newhook	= ng_ct_newhook,
2256	.connect	= ng_ct_connect,
2257	.rcvdata	= ng_ct_rcvdata,
2258	.disconnect	= ng_ct_disconnect,
2259};
2260#endif /*NETGRAPH*/
2261
2262#ifdef NETGRAPH
2263MODULE_DEPEND (ng_ct, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION);
2264#else
2265MODULE_DEPEND (ct, sppp, 1, 1, 1);
2266#endif
2267DRIVER_MODULE (ct, isa, ct_isa_driver, ct_devclass, ct_modevent, NULL);
2268MODULE_VERSION (ct, 1);
2269