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