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