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