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