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