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