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