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