BPF has been in the kernel for ages and is supported by all NICs but I4B.
[dragonfly.git] / sys / net / i4b / driver / i4b_isppp.c
1 /*
2  *   Copyright (c) 1997 Joerg Wunsch. All rights reserved.
3  *
4  *   Copyright (c) 1997, 2000 Hellmuth Michaelis. All rights reserved.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *   1. Redistributions of source code must retain the above copyright
11  *      notice, this list of conditions and the following disclaimer.
12  *   2. Redistributions in binary form must reproduce the above copyright
13  *      notice, this list of conditions and the following disclaimer in the
14  *      documentation and/or other materials provided with the distribution.
15  *   
16  *   THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  *   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *   ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  *   FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  *   OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  *   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  *   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  *   OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  *   SUCH DAMAGE.
27  *
28  *---------------------------------------------------------------------------
29  *
30  *      i4b_isppp.c - isdn4bsd kernel SyncPPP driver
31  *      --------------------------------------------
32  *
33  *      Uses Serge Vakulenko's sppp backend (originally contributed with
34  *      the "cx" driver for Cronyx's HDLC-in-hardware device).  This driver
35  *      is only the glue between sppp and i4b.
36  *
37  *      $Id: i4b_isppp.c,v 1.44 2000/08/31 07:07:26 hm Exp $
38  *
39  * $FreeBSD: src/sys/i4b/driver/i4b_isppp.c,v 1.7.2.3 2003/02/06 14:50:53 gj Exp $
40  * $DragonFly: src/sys/net/i4b/driver/i4b_isppp.c,v 1.7 2004/03/14 15:36:54 joerg Exp $
41  *
42  *      last edit-date: [Thu Aug 31 09:02:27 2000]
43  *
44  *---------------------------------------------------------------------------*/
45
46
47 #ifndef __NetBSD__
48 #endif
49 #include "use_i4bisppp.h"
50
51 #ifndef __NetBSD__
52 #if NI4BISPPP == 0
53 # error "You need to define `device sppp <N>' with options ISPPP"
54 #endif
55 #endif
56
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/mbuf.h>
60 #include <sys/socket.h>
61 #include <sys/errno.h>
62 #include <sys/ioccom.h>
63 #include <sys/sockio.h>
64 #include <sys/kernel.h>
65
66 #include <net/if.h>
67 #include <net/if_types.h>
68 #include <net/sppp/if_sppp.h>
69
70
71 #if defined(__DragonFly__) || (defined(__FreeBSD_version) &&  __FreeBSD_version >= 400008)
72 #include "use_bpf.h"     
73 #else
74 #include "bpfilter.h"
75 #endif
76 #if NBPFILTER > 0 || NBPF > 0
77 #include <sys/time.h>
78 #include <net/bpf.h>
79 #endif
80
81 #if defined(__DragonFly__) || defined(__FreeBSD__)
82 #include <net/i4b/include/machine/i4b_debug.h>
83 #include <net/i4b/include/machine/i4b_ioctl.h>
84 #else
85 #include <i4b/i4b_ioctl.h>
86 #include <i4b/i4b_cause.h>
87 #include <i4b/i4b_debug.h>
88 #endif
89
90 #include "../include/i4b_global.h"
91 #include "../include/i4b_l3l4.h"
92 #include "../layer4/i4b_l4.h"
93
94 #if defined(__DragonFly__) || defined(__FreeBSD__)
95 #define PDEVSTATIC      static
96
97 #ifdef __DragonFly__
98 #define ISPPP_FMT       "%s: "
99 #define ISPPP_ARG(sc)   ((sc)->sc_if.if_xname)
100 #define IFP2UNIT(ifp)   ((struct i4bisppp_softc *)ifp->if_softc)->sc_unit
101 #else
102 #define ISPPP_FMT       "isp%d: "
103 #define ISPPP_ARG(sc)   ((sc)->sc_if.if_unit)
104 #define IFP2UNIT(ifp)   (ifp)->if_unit
105 #endif
106                 
107 # if defined(__DragonFly__) || __FreeBSD_version >= 300001
108 #  define CALLOUT_INIT(chan)            callout_handle_init(chan)
109 #  define TIMEOUT(fun, arg, chan, tick) chan = timeout(fun, arg, tick)
110 #  define UNTIMEOUT(fun, arg, chan)     untimeout(fun, arg, chan)
111 #  define IOCTL_CMD_T u_long
112 # else
113 #  define CALLOUT_INIT(chan)            do {} while(0)
114 #  define TIMEOUT(fun, arg, chan, tick) timeout(fun, arg, tick)
115 #  define UNTIMEOUT(fun, arg, chan)     untimeout(fun, arg)
116 #  define IOCTL_CMD_T int
117 # endif
118
119 #elif defined __NetBSD__ || defined __OpenBSD__
120 #define ISPPP_FMT       "%s: "
121 #define ISPPP_ARG(sc)   ((sc)->sc_if.if_xname)
122 #define PDEVSTATIC      /* not static */
123 #define IOCTL_CMD_T     u_long
124 #define IFP2UNIT(ifp)   ((struct i4bisppp_softc *)ifp->if_softc)->sc_unit
125 #else
126 # error "What system are you using?"
127 #endif
128
129 #if defined(__DragonFly__) || defined(__FreeBSD__)
130 PDEVSTATIC void i4bispppattach(void *);
131 PSEUDO_SET(i4bispppattach, i4b_isppp);
132 #else
133 PDEVSTATIC void i4bispppattach(void);
134 #endif
135
136 #define I4BISPPPACCT            1       /* enable accounting messages */
137 #define I4BISPPPACCTINTVL       2       /* accounting msg interval in secs */
138 #define I4BISPPPDISCDEBUG       1       
139
140 #define PPP_HDRLEN              4       /* 4 octetts PPP header length  */
141
142 struct i4bisppp_softc {
143         /*
144          * struct sppp starts with a struct ifnet, but we gotta allocate
145          * more space for it.  NB: do not relocate this union, it must
146          * be first in isppp_softc.  The tls and tlf hooks below want to
147          * convert a ``struct sppp *'' into a ``struct isppp_softc *''.
148          */
149         union {
150                 struct ifnet scu_if;
151                 struct sppp scu_sp;
152         } sc_if_un;
153 #define sc_if sc_if_un.scu_if
154
155         int     sc_state;       /* state of the interface       */
156
157 #if !defined(__FreeBSD__) || defined(__DragonFly__)
158         int     sc_unit;        /* unit number for Net/OpenBSD  */
159 #endif
160
161         call_desc_t *sc_cdp;    /* ptr to call descriptor       */
162
163 #ifdef I4BISPPPACCT
164         int sc_iinb;            /* isdn driver # of inbytes     */
165         int sc_ioutb;           /* isdn driver # of outbytes    */
166         int sc_inb;             /* # of bytes rx'd              */
167         int sc_outb;            /* # of bytes tx'd              */
168         int sc_linb;            /* last # of bytes rx'd         */
169         int sc_loutb;           /* last # of bytes tx'd         */
170         int sc_fn;              /* flag, first null acct        */
171 #endif
172
173 #if defined(__DragonFly__) || (defined(__FreeBSD_version) && __FreeBSD_version >= 300001)
174         struct callout_handle sc_ch;
175 #endif
176
177 } i4bisppp_softc[NI4BISPPP];
178
179 static void     i4bisppp_init_linktab(int unit);
180 static int      i4bisppp_ioctl(struct ifnet *ifp, IOCTL_CMD_T cmd, caddr_t data);
181
182 #if 0
183 static void     i4bisppp_send(struct ifnet *ifp);
184 #endif
185
186 static void     i4bisppp_start(struct ifnet *ifp);
187
188 #if 0 /* never used ??? */
189 static void     i4bisppp_timeout(void *cookie);
190 #endif
191
192 static void     i4bisppp_tls(struct sppp *sp);
193 static void     i4bisppp_tlf(struct sppp *sp);
194 static void     i4bisppp_state_changed(struct sppp *sp, int new_state);
195 static void     i4bisppp_negotiation_complete(struct sppp *sp);
196 static void     i4bisppp_watchdog(struct ifnet *ifp);
197 time_t          i4bisppp_idletime(int unit);
198
199 /* initialized by L4 */
200
201 static drvr_link_t i4bisppp_drvr_linktab[NI4BISPPP];
202 static isdn_link_t *isdn_linktab[NI4BISPPP];
203
204 enum i4bisppp_states {
205         ST_IDLE,                        /* initialized, ready, idle     */
206         ST_DIALING,                     /* dialling out to remote       */
207         ST_CONNECTED,                   /* connected to remote          */
208 };
209
210 /*===========================================================================*
211  *                      DEVICE DRIVER ROUTINES
212  *===========================================================================*/
213
214 /*---------------------------------------------------------------------------*
215  *      interface attach routine at kernel boot time
216  *---------------------------------------------------------------------------*/
217 PDEVSTATIC void
218 #if defined(__DragonFly__) || defined(__FreeBSD__)
219 i4bispppattach(void *dummy)
220 #else
221 i4bispppattach()
222 #endif
223 {
224         struct i4bisppp_softc *sc = i4bisppp_softc;
225         int i;
226
227 #ifndef HACK_NO_PSEUDO_ATTACH_MSG
228 #ifdef SPPP_VJ
229         printf("i4bisppp: %d ISDN SyncPPP device(s) attached (VJ header compression)\n", NI4BISPPP);
230 #else
231         printf("i4bisppp: %d ISDN SyncPPP device(s) attached\n", NI4BISPPP);
232 #endif
233 #endif
234
235         for(i = 0; i < NI4BISPPP; sc++, i++) {
236                 i4bisppp_init_linktab(i);
237                 
238                 sc->sc_if.if_softc = sc;
239
240 #ifdef __DragonFly__
241                 if_initname(&(sc->sc_if), "isp", i);
242 #elif defined(__FreeBSD__)
243                 sc->sc_if.if_name = "isp";
244 #if defined(__FreeBSD_version) && __FreeBSD_version < 300001
245                 sc->sc_if.if_next = NULL;
246 #endif
247                 sc->sc_if.if_unit = i;
248 #else
249                 sprintf(sc->sc_if.if_xname, "isp%d", i);
250                 sc->sc_unit = i;
251 #endif
252
253                 sc->sc_if.if_mtu = PP_MTU;
254
255 #ifdef __NetBSD__
256                 sc->sc_if.if_flags = IFF_SIMPLEX | IFF_POINTOPOINT |
257                                         IFF_MULTICAST;
258 #else
259                 sc->sc_if.if_flags = IFF_SIMPLEX | IFF_POINTOPOINT;
260 #endif
261
262                 sc->sc_if.if_type = IFT_ISDNBASIC;
263                 sc->sc_state = ST_IDLE;
264
265                 sc->sc_if.if_ioctl = i4bisppp_ioctl;
266
267                 /* actually initialized by sppp_attach() */
268                 /* sc->sc_if.if_output = sppp_output; */
269
270                 sc->sc_if.if_start = i4bisppp_start;
271
272                 sc->sc_if.if_hdrlen = 0;
273                 sc->sc_if.if_addrlen = 0;
274                 sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
275
276                 sc->sc_if.if_ipackets = 0;
277                 sc->sc_if.if_ierrors = 0;
278                 sc->sc_if.if_opackets = 0;
279                 sc->sc_if.if_oerrors = 0;
280                 sc->sc_if.if_collisions = 0;
281                 sc->sc_if.if_ibytes = 0;
282                 sc->sc_if.if_obytes = 0;
283                 sc->sc_if.if_imcasts = 0;
284                 sc->sc_if.if_omcasts = 0;
285                 sc->sc_if.if_iqdrops = 0;
286                 sc->sc_if.if_noproto = 0;
287
288 #if I4BISPPPACCT
289                 sc->sc_if.if_timer = 0; 
290                 sc->sc_if.if_watchdog = i4bisppp_watchdog;      
291                 sc->sc_iinb = 0;
292                 sc->sc_ioutb = 0;
293                 sc->sc_inb = 0;
294                 sc->sc_outb = 0;
295                 sc->sc_linb = 0;
296                 sc->sc_loutb = 0;
297                 sc->sc_fn = 1;
298 #endif
299
300                 sc->sc_if_un.scu_sp.pp_tls = i4bisppp_tls;
301                 sc->sc_if_un.scu_sp.pp_tlf = i4bisppp_tlf;
302                 sc->sc_if_un.scu_sp.pp_con = i4bisppp_negotiation_complete;
303                 sc->sc_if_un.scu_sp.pp_chg = i4bisppp_state_changed;
304
305                 sppp_attach(&sc->sc_if);
306 /* XXX: validate / add proper code */
307 #if 0
308                 /* do not call bpfattach in ether_ifattach */
309                 ether_ifattach(&sc->sc_if, 0);
310 #else
311                 if_attach(&sc->sc_if);
312 #endif
313
314 #if NBPFILTER > 0 || NBPF > 0
315 #if defined(__DragonFly__) || defined(__FreeBSD__)
316                 bpfattach(&sc->sc_if, DLT_PPP, PPP_HDRLEN);
317                 CALLOUT_INIT(&sc->sc_ch);
318 #endif /* __FreeBSD__ */
319 #ifdef __NetBSD__
320                 bpfattach(&sc->sc_if.if_bpf, &sc->sc_if, DLT_PPP, sizeof(u_int));
321 #endif
322 #endif          
323         }
324 }
325
326 /*---------------------------------------------------------------------------*
327  *      process ioctl
328  *---------------------------------------------------------------------------*/
329 static int
330 i4bisppp_ioctl(struct ifnet *ifp, IOCTL_CMD_T cmd, caddr_t data)
331 {
332         struct i4bisppp_softc *sc = ifp->if_softc;
333 #if 0
334         struct sppp *sp = (struct sppp *)sc;
335         struct ifaddr *ifa = (struct ifaddr *) data;
336         struct ifreq *ifr = (struct ifreq *) data;
337 #endif
338
339         int error;
340
341         error = sppp_ioctl(&sc->sc_if, cmd, data);
342         if (error)
343                 return error;
344
345         switch(cmd) {
346         case SIOCSIFFLAGS:
347 #if 0 /* never used ??? */
348                 x = splimp();
349                 if ((ifp->if_flags & IFF_UP) == 0)
350                         UNTIMEOUT(i4bisppp_timeout, (void *)sp, sc->sc_ch);
351                 splx(x);
352 #endif
353                 break;
354         }
355
356         return 0;
357 }
358
359 /*---------------------------------------------------------------------------*
360  *      start output to ISDN B-channel
361  *---------------------------------------------------------------------------*/
362 static void
363 i4bisppp_start(struct ifnet *ifp)
364 {
365         struct i4bisppp_softc *sc = ifp->if_softc;
366         struct mbuf *m;
367         /* int s; */
368         int unit = IFP2UNIT(ifp);
369
370         if (sppp_isempty(ifp))
371                 return;
372
373         if(sc->sc_state != ST_CONNECTED)
374                 return;
375
376         /*
377          * s = splimp();
378          * ifp->if_flags |= IFF_OACTIVE; // - need to clear this somewhere
379          * splx(s);
380          */
381
382         while ((m = sppp_dequeue(&sc->sc_if)) != NULL)
383         {
384
385 #if NBPFILTER > 0 || NBPF > 0
386 #if defined(__DragonFly__) || defined(__FreeBSD__)
387                 if (ifp->if_bpf)
388                         bpf_mtap(ifp, m);
389 #endif /* __FreeBSD__ */
390
391 #ifdef __NetBSD__
392                 if (ifp->if_bpf)
393                         bpf_mtap(ifp->if_bpf, m);
394 #endif
395 #endif /* NBPFILTER > 0 || NBPF > 0 */
396
397                 microtime(&ifp->if_lastchange);
398
399                 IF_LOCK(isdn_linktab[unit]->tx_queue);
400                 if(_IF_QFULL(isdn_linktab[unit]->tx_queue))
401                 {
402                         NDBGL4(L4_ISPDBG, "isp%d, tx queue full!", unit);
403                         m_freem(m);
404                 }
405                 else
406                 {
407 #if 0
408                         sc->sc_if.if_obytes += m->m_pkthdr.len;
409 #endif
410                         sc->sc_outb += m->m_pkthdr.len;
411                         sc->sc_if.if_opackets++;
412
413                         _IF_ENQUEUE(isdn_linktab[unit]->tx_queue, m);
414                 }
415                 IF_UNLOCK(isdn_linktab[unit]->tx_queue);
416         }
417         isdn_linktab[unit]->bch_tx_start(isdn_linktab[unit]->unit,
418                                          isdn_linktab[unit]->channel);
419 }
420
421 #ifdef I4BISPPPACCT
422 /*---------------------------------------------------------------------------*
423  *      watchdog routine
424  *---------------------------------------------------------------------------*/
425 static void
426 i4bisppp_watchdog(struct ifnet *ifp)
427 {
428         struct i4bisppp_softc *sc = ifp->if_softc;
429         int unit = IFP2UNIT(ifp);
430         bchan_statistics_t bs;
431         
432         (*isdn_linktab[unit]->bch_stat)
433                 (isdn_linktab[unit]->unit, isdn_linktab[unit]->channel, &bs);
434
435         sc->sc_ioutb += bs.outbytes;
436         sc->sc_iinb += bs.inbytes;
437         
438         if((sc->sc_iinb != sc->sc_linb) || (sc->sc_ioutb != sc->sc_loutb) || sc->sc_fn)
439         {
440                 int ri = (sc->sc_iinb - sc->sc_linb)/I4BISPPPACCTINTVL;
441                 int ro = (sc->sc_ioutb - sc->sc_loutb)/I4BISPPPACCTINTVL;
442
443                 if((sc->sc_iinb == sc->sc_linb) && (sc->sc_ioutb == sc->sc_loutb))
444                         sc->sc_fn = 0;
445                 else
446                         sc->sc_fn = 1;
447                         
448                 sc->sc_linb = sc->sc_iinb;
449                 sc->sc_loutb = sc->sc_ioutb;
450
451                 i4b_l4_accounting(BDRV_ISPPP, unit, ACCT_DURING,
452                          sc->sc_ioutb, sc->sc_iinb, ro, ri, sc->sc_outb, sc->sc_inb);
453         }
454         sc->sc_if.if_timer = I4BISPPPACCTINTVL;         
455
456 #if 0 /* old stuff, keep it around */
457         printf(ISPPP_FMT "transmit timeout\n", ISPPP_ARG(sc));
458         i4bisppp_start(ifp);
459 #endif
460 }
461 #endif /* I4BISPPPACCT */
462
463 /*
464  *===========================================================================*
465  *                      SyncPPP layer interface routines
466  *===========================================================================*
467  */
468
469 #if 0 /* never used ??? */
470 /*---------------------------------------------------------------------------*
471  *      just an alias for i4bisppp_tls, but of type timeout_t
472  *---------------------------------------------------------------------------*/
473 static void
474 i4bisppp_timeout(void *cookie)
475 {
476         i4bisppp_tls((struct sppp *)cookie);
477 }
478 #endif
479
480 /*---------------------------------------------------------------------------*
481  *      PPP this-layer-started action
482  *---------------------------------------------------------------------------*
483  */
484 static void
485 i4bisppp_tls(struct sppp *sp)
486 {
487         struct i4bisppp_softc *sc = (struct i4bisppp_softc *)sp;
488         struct ifnet *ifp = (struct ifnet *)sp;
489
490         if(sc->sc_state == ST_CONNECTED)
491                 return;
492
493         i4b_l4_dialout(BDRV_ISPPP, IFP2UNIT(ifp));
494 }
495
496 /*---------------------------------------------------------------------------*
497  *      PPP this-layer-finished action
498  *---------------------------------------------------------------------------*
499  */
500 static void
501 i4bisppp_tlf(struct sppp *sp)
502 {
503         struct i4bisppp_softc *sc = (struct i4bisppp_softc *)sp;
504 /*      call_desc_t *cd = sc->sc_cdp;   */
505         struct ifnet *ifp = (struct ifnet *)sp; 
506         
507         if(sc->sc_state != ST_CONNECTED)
508                 return;
509
510 #if 0 /* never used ??? */
511         UNTIMEOUT(i4bisppp_timeout, (void *)sp, sc->sc_ch);
512 #endif
513
514         i4b_l4_drvrdisc(BDRV_ISPPP, IFP2UNIT(ifp));
515 }
516 /*---------------------------------------------------------------------------*
517  *      PPP interface phase change
518  *---------------------------------------------------------------------------*
519  */
520 static void
521 i4bisppp_state_changed(struct sppp *sp, int new_state)
522 {
523         struct i4bisppp_softc *sc = (struct i4bisppp_softc *)sp;
524         
525         i4b_l4_ifstate_changed(sc->sc_cdp, new_state);
526 }
527
528 /*---------------------------------------------------------------------------*
529  *      PPP control protocol negotiation complete (run ip-up script now)
530  *---------------------------------------------------------------------------*
531  */
532 static void
533 i4bisppp_negotiation_complete(struct sppp *sp)
534 {
535         struct i4bisppp_softc *sc = (struct i4bisppp_softc *)sp;
536         
537         i4b_l4_negcomplete(sc->sc_cdp);
538 }
539
540 /*===========================================================================*
541  *                      ISDN INTERFACE ROUTINES
542  *===========================================================================*/
543
544 /*---------------------------------------------------------------------------*
545  *      this routine is called from L4 handler at connect time
546  *---------------------------------------------------------------------------*/
547 static void
548 i4bisppp_connect(int unit, void *cdp)
549 {
550         struct i4bisppp_softc *sc = &i4bisppp_softc[unit];
551         struct sppp *sp = &sc->sc_if_un.scu_sp;
552         int s = splimp();
553
554         sc->sc_cdp = (call_desc_t *)cdp;
555         sc->sc_state = ST_CONNECTED;
556
557 #if I4BISPPPACCT
558         sc->sc_iinb = 0;
559         sc->sc_ioutb = 0;
560         sc->sc_inb = 0;
561         sc->sc_outb = 0;
562         sc->sc_linb = 0;
563         sc->sc_loutb = 0;
564         sc->sc_if.if_timer = I4BISPPPACCTINTVL;
565 #endif
566         
567 #if 0 /* never used ??? */
568         UNTIMEOUT(i4bisppp_timeout, (void *)sp, sc->sc_ch);
569 #endif
570
571         sp->pp_up(sp);          /* tell PPP we are ready */
572 #ifndef __NetBSD__
573         sp->pp_last_sent = sp->pp_last_recv = SECOND;
574 #endif
575         splx(s);
576 }
577
578 /*---------------------------------------------------------------------------*
579  *      this routine is called from L4 handler at disconnect time
580  *---------------------------------------------------------------------------*/
581 static void
582 i4bisppp_disconnect(int unit, void *cdp)
583 {
584         call_desc_t *cd = (call_desc_t *)cdp;
585         struct i4bisppp_softc *sc = &i4bisppp_softc[unit];
586         struct sppp *sp = &sc->sc_if_un.scu_sp;
587
588         int s = splimp();
589
590         /* new stuff to check that the active channel is being closed */
591         if (cd != sc->sc_cdp)
592         {
593                 NDBGL4(L4_ISPDBG, "isp%d, channel%d not active!", unit, cd->channelid);
594                 splx(s);
595                 return;
596         }
597
598 #if I4BISPPPACCT
599         sc->sc_if.if_timer = 0;
600 #endif
601
602         i4b_l4_accounting(BDRV_ISPPP, unit, ACCT_FINAL,
603                  sc->sc_ioutb, sc->sc_iinb, 0, 0, sc->sc_outb, sc->sc_inb);
604         
605         if (sc->sc_state == ST_CONNECTED)
606         {
607 #if 0 /* never used ??? */
608                 UNTIMEOUT(i4bisppp_timeout, (void *)sp, sc->sc_ch);
609 #endif
610                 sc->sc_cdp = (call_desc_t *)0;  
611                 /* do this here because pp_down calls i4bisppp_tlf */
612                 sc->sc_state = ST_IDLE;
613                 sp->pp_down(sp);        /* tell PPP we have hung up */
614         }
615
616         splx(s);
617 }
618
619 /*---------------------------------------------------------------------------*
620  *      this routine is used to give a feedback from userland demon
621  *      in case of dial problems
622  *---------------------------------------------------------------------------*/
623 static void
624 i4bisppp_dialresponse(int unit, int status, cause_t cause)
625 {
626         struct i4bisppp_softc *sc = &i4bisppp_softc[unit];
627         struct sppp *sp = &sc->sc_if_un.scu_sp;
628
629         NDBGL4(L4_ISPDBG, "isp%d: status=%d, cause=%d", unit, status, cause);
630
631         if(status != DSTAT_NONE)
632         {
633                 struct mbuf *m;
634                 
635                 NDBGL4(L4_ISPDBG, "isp%d: clearing queues", unit);
636
637                 if(!(sppp_isempty(&sc->sc_if)))
638                 {
639                         while((m = sppp_dequeue(&sc->sc_if)) != NULL)
640                                 m_freem(m);
641                 }
642
643                 sc->sc_cdp = (call_desc_t *)0;
644                 /* do this here because pp_down calls i4bisppp_tlf */
645                 sc->sc_state = ST_IDLE;
646                 /*
647                  * Ahh, sppp doesn't like to get a down event when
648                  * dialing fails. So first tell it that we are up
649                  * (doesn't hurt us since sc_state != ST_CONNECTED)
650                  * and then go down.
651                  */
652                 sp->pp_up(sp);
653                 sp->pp_down(sp);
654         }
655 }
656         
657 /*---------------------------------------------------------------------------*
658  *      interface up/down
659  *---------------------------------------------------------------------------*/
660 static void
661 i4bisppp_updown(int unit, int updown)
662 {
663         /* could probably do something useful here */
664 }
665         
666 /*---------------------------------------------------------------------------*
667  *      this routine is called from the HSCX interrupt handler
668  *      when a new frame (mbuf) has been received and was put on
669  *      the rx queue.
670  *---------------------------------------------------------------------------*/
671 static void
672 i4bisppp_rx_data_rdy(int unit)
673 {
674         struct i4bisppp_softc *sc = &i4bisppp_softc[unit];
675         struct mbuf *m;
676         int s;
677         
678         if((m = *isdn_linktab[unit]->rx_mbuf) == NULL)
679                 return;
680
681         m->m_pkthdr.rcvif = &sc->sc_if;
682         m->m_pkthdr.len = m->m_len;
683
684         microtime(&sc->sc_if.if_lastchange);
685
686         sc->sc_if.if_ipackets++;
687 #if 0
688         sc->sc_if.if_ibytes += m->m_pkthdr.len;
689 #endif
690
691 #if I4BISPPPACCT
692         sc->sc_inb += m->m_pkthdr.len;
693 #endif
694         
695 #ifdef I4BISPPPDEBUG
696         printf("i4bisppp_rx_data_ready: received packet!\n");
697 #endif
698
699 #if NBPFILTER > 0 || NBPF > 0
700
701 #if defined(__DragonFly__) || defined(__FreeBSD__)
702         if(sc->sc_if.if_bpf)
703                 bpf_mtap(&sc->sc_if, m);
704 #endif /* __FreeBSD__ */
705
706 #ifdef __NetBSD__
707         if(sc->sc_if.if_bpf)
708                 bpf_mtap(sc->sc_if.if_bpf, m);
709 #endif
710
711 #endif /* NBPFILTER > 0  || NBPF > 0 */
712
713         s = splimp();
714
715         sppp_input(&sc->sc_if, m);
716
717         splx(s);
718 }
719
720 /*---------------------------------------------------------------------------*
721  *      this routine is called from the HSCX interrupt handler
722  *      when the last frame has been sent out and there is no
723  *      further frame (mbuf) in the tx queue.
724  *---------------------------------------------------------------------------*/
725 static void
726 i4bisppp_tx_queue_empty(int unit)
727 {
728         i4bisppp_start(&i4bisppp_softc[unit].sc_if);    
729 }
730
731 /*---------------------------------------------------------------------------*
732  *      THIS should be used instead of last_active_time to implement
733  *      an activity timeout mechanism.
734  *
735  *      Sending back the time difference unneccessarily complicates the
736  *      idletime checks in i4b_l4.c. Return the largest time instead.
737  *      That way the code in i4b_l4.c needs only minimal changes.
738  *---------------------------------------------------------------------------*/
739 time_t
740 i4bisppp_idletime(int unit)
741 {
742 #ifdef __NetBSD__
743        return(i4bisppp_softc[unit].sc_cdp->last_active_time);
744 #else
745         struct sppp *sp;
746         sp = (struct sppp *) &i4bisppp_softc[unit];
747
748         return((sp->pp_last_recv < sp->pp_last_sent) ?
749                         sp->pp_last_sent : sp->pp_last_recv);
750 #endif
751 }
752
753 /*---------------------------------------------------------------------------*
754  *      this routine is called from the HSCX interrupt handler
755  *      each time a packet is received or transmitted. It should
756  *      be used to implement an activity timeout mechanism.
757  *---------------------------------------------------------------------------*/
758 static void
759 i4bisppp_activity(int unit, int rxtx)
760 {
761         i4bisppp_softc[unit].sc_cdp->last_active_time = SECOND;
762 }
763
764 /*---------------------------------------------------------------------------*
765  *      return this drivers linktab address
766  *---------------------------------------------------------------------------*/
767 drvr_link_t *
768 i4bisppp_ret_linktab(int unit)
769 {
770         return(&i4bisppp_drvr_linktab[unit]);
771 }
772
773 /*---------------------------------------------------------------------------*
774  *      setup the isdn_linktab for this driver
775  *---------------------------------------------------------------------------*/
776 void
777 i4bisppp_set_linktab(int unit, isdn_link_t *ilt)
778 {
779         isdn_linktab[unit] = ilt;
780 }
781
782 /*---------------------------------------------------------------------------*
783  *      initialize this drivers linktab
784  *---------------------------------------------------------------------------*/
785 static void
786 i4bisppp_init_linktab(int unit)
787 {
788         i4bisppp_drvr_linktab[unit].unit = unit;
789         i4bisppp_drvr_linktab[unit].bch_rx_data_ready = i4bisppp_rx_data_rdy;
790         i4bisppp_drvr_linktab[unit].bch_tx_queue_empty = i4bisppp_tx_queue_empty;
791         i4bisppp_drvr_linktab[unit].bch_activity = i4bisppp_activity;
792         i4bisppp_drvr_linktab[unit].line_connected = i4bisppp_connect;
793         i4bisppp_drvr_linktab[unit].line_disconnected = i4bisppp_disconnect;
794         i4bisppp_drvr_linktab[unit].dial_response = i4bisppp_dialresponse;      
795         i4bisppp_drvr_linktab[unit].updown_ind = i4bisppp_updown;       
796 }
797
798 /*===========================================================================*/