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