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