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