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