2 * Copyright (c) 1998, 1999 Bert Driehuis. All rights reserved.
4 * Copyright (c) 1997, 1998 Hellmuth Michaelis. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 *---------------------------------------------------------------------------
30 * i4b_bsdi_ibc.c - isdn4bsd kernel BSD/OS point to point driver
31 * -------------------------------------------------------------
33 * $Id: i4b_bsdi_ibc.c,v 1.3 2000/08/21 07:21:07 hm Exp $
35 * $FreeBSD: src/sys/i4b/driver/i4b_bsdi_ibc.c,v 1.3.2.1 2001/08/10 14:08:35 obrien Exp $
36 * $DragonFly: src/sys/net/i4b/driver/Attic/i4b_bsdi_ibc.c,v 1.5 2004/01/06 03:17:25 dillon Exp $
38 * last edit-date: [Tue Dec 14 21:55:24 1999]
40 *---------------------------------------------------------------------------*/
46 #include <sys/param.h>
47 #include <sys/systm.h>
49 #include <sys/socket.h>
50 #include <sys/errno.h>
51 #include <sys/ioccom.h>
52 #include <sys/ttycom.h>
53 #include <sys/sockio.h>
54 #include <sys/kernel.h>
55 #include <sys/protosw.h>
58 #include <net/if_types.h>
59 #include <net/if_p2p.h>
60 #include <net/netisr.h>
61 #include <net/route.h>
63 #include <netinet/in.h>
64 #include <netinet/in_systm.h>
65 #include <netinet/in_var.h>
66 #include <netinet/ip.h>
68 #include <net/i4b/include/machine/i4b_ioctl.h>
69 #include <net/i4b/include/machine/i4b_cause.h>
70 #include "../../include/i4b_global.h"
71 #include "../../include/i4b_mbuf.h"
72 #include "../../include/i4b_l3l4.h"
73 #include "../../layer4/i4b_l4.h"
75 #define IFP2UNIT(ifp) (ifp)->if_dunit
77 #define IOCTL_CMD_T u_long
79 void ibcattach(void *);
81 #define IBCACCT 1 /* enable accounting messages */
82 #define IBCACCTINTVL 2 /* accounting msg interval in secs */
84 #define PPP_HDRLEN 4 /* 4 octets PPP header length */
87 struct p2pcom sc_p2pcom;
89 int sc_state; /* state of the interface */
90 call_desc_t *sc_cdp; /* ptr to call descriptor */
93 int sc_iinb; /* isdn driver # of inbytes */
94 int sc_ioutb; /* isdn driver # of outbytes */
95 int sc_inb; /* # of bytes rx'd */
96 int sc_outb; /* # of bytes tx'd */
97 int sc_linb; /* last # of bytes rx'd */
98 int sc_loutb; /* last # of bytes tx'd */
99 int sc_fn; /* flag, first null acct */
103 static void ibc_init_linktab(int unit);
105 static int ibc_start(struct ifnet *ifp);
107 static int ibc_watchdog(int unit);
108 static int ibc_mdmctl(struct p2pcom *pp, int flag);
109 static int ibc_getmdm(struct p2pcom *pp, caddr_t arg);
111 /* initialized by L4 */
113 static drvr_link_t ibc_drvr_linktab[NIBC];
114 static isdn_link_t *isdn_ibc_lt[NIBC];
117 ST_IDLE, /* initialized, ready, idle */
118 ST_DIALING, /* dialling out to remote */
119 ST_CONNECTED, /* connected to remote */
122 int ibcdebug = 0; /* Use bpatch to set this for debug printf's */
123 #define DBG(x) if (ibcdebug) printf x
125 /*===========================================================================*
126 * DEVICE DRIVER ROUTINES
127 *===========================================================================*/
129 /*---------------------------------------------------------------------------*
130 * interface attach routine at kernel boot time
131 *---------------------------------------------------------------------------*/
133 ibcattach(void *dummy)
135 struct ibc_softc *sc = ibc_softc;
139 #ifndef HACK_NO_PSEUDO_ATTACH_MSG
140 printf("ibc: %d ISDN ibc device(s) attached\n",
144 for(i = 0; i < NIBC; sc++, i++) {
147 sc->sc_p2pcom.p2p_mdmctl = ibc_mdmctl;
148 sc->sc_p2pcom.p2p_getmdm = ibc_getmdm;
149 sc->sc_state = ST_IDLE;
150 ifp = &sc->sc_p2pcom.p2p_if;
151 if_initname(ifp, "ibc", i);
153 ifp->if_mtu = 1500 /*XXX*/;
154 ifp->if_baudrate = 64000;
155 ifp->if_flags = IFF_SIMPLEX | IFF_POINTOPOINT;
156 ifp->if_type = IFT_ISDNBASIC;
157 ifp->if_start = ibc_start;
159 ifp->if_ioctl = p2p_ioctl;
163 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
165 ifp->if_ipackets = 0;
167 ifp->if_opackets = 0;
169 ifp->if_collisions = 0;
178 ifp->if_watchdog = ibc_watchdog;
187 #if defined(__FreeBSD_version) && ((__FreeBSD_version >= 500009) || (410000 <= __FreeBSD_version && __FreeBSD_version < 500000))
188 ether_ifattach(ifp, 0);
192 p2p_attach(&sc->sc_p2pcom);
197 p2p_dequeue(struct p2pcom *pp)
201 IF_DEQUEUE(&pp->p2p_isnd, m);
203 IF_DEQUEUE(&pp->p2p_if.if_snd, m);
207 /*---------------------------------------------------------------------------*
208 * start output to ISDN B-channel
209 *---------------------------------------------------------------------------*/
211 ibc_start(struct ifnet *ifp)
213 int unit = IFP2UNIT(ifp);
214 struct ibc_softc *sc = (struct ibc_softc *)&ibc_softc[unit];
215 struct p2pcom *pp = &sc->sc_p2pcom;
219 if(sc->sc_state != ST_CONNECTED) {
220 DBG(("ibc%d: ibc_start called with sc_state=%d\n",
221 unit, sc->sc_state));
227 IF_LOCK(isdn_ibc_lt[unit]->tx_queue);
228 if (_IF_QFULL(isdn_ibc_lt[unit]->tx_queue)) {
229 IF_UNLOCK(isdn_ibc_lt[unit]->tx_queue);
236 IF_UNLOCK(isdn_ibc_lt[unit]->tx_queue);
242 microtime(&ifp->if_lastchange);
244 ifp->if_obytes += m->m_pkthdr.len;
245 sc->sc_outb += m->m_pkthdr.len;
246 _IF_ENQUEUE(isdn_ibc_lt[unit]->tx_queue, m);
249 } while (!_IF_QFULL(isdn_ibc_lt[unit]->tx_queue) &&
250 (m = p2p_dequeue(pp)) != NULL);
251 IF_UNLOCK(isdn_ibc_lt[unit]->tx_queue);
252 isdn_ibc_lt[unit]->bch_tx_start(isdn_ibc_lt[unit]->unit,
253 isdn_ibc_lt[unit]->channel);
259 /*---------------------------------------------------------------------------*
261 *---------------------------------------------------------------------------*/
263 ibc_watchdog(int unit)
265 struct ibc_softc *sc = &ibc_softc[unit];
266 struct ifnet *ifp = &sc->sc_p2pcom.p2p_if;
267 bchan_statistics_t bs;
269 (*isdn_ibc_lt[unit]->bch_stat)
270 (isdn_ibc_lt[unit]->unit, isdn_ibc_lt[unit]->channel, &bs);
272 sc->sc_ioutb += bs.outbytes;
273 sc->sc_iinb += bs.inbytes;
275 if((sc->sc_iinb != sc->sc_linb) || (sc->sc_ioutb != sc->sc_loutb) || sc->sc_fn)
277 int ri = (sc->sc_iinb - sc->sc_linb)/IBCACCTINTVL;
278 int ro = (sc->sc_ioutb - sc->sc_loutb)/IBCACCTINTVL;
280 if((sc->sc_iinb == sc->sc_linb) && (sc->sc_ioutb == sc->sc_loutb))
285 sc->sc_linb = sc->sc_iinb;
286 sc->sc_loutb = sc->sc_ioutb;
288 i4b_l4_accounting(BDRV_IBC, unit, ACCT_DURING,
289 sc->sc_ioutb, sc->sc_iinb, ro, ri, sc->sc_outb, sc->sc_inb);
291 ifp->if_timer = IBCACCTINTVL;
297 *===========================================================================*
298 * P2P layer interface routines
299 *===========================================================================*
303 /*---------------------------------------------------------------------------*
304 * PPP interface phase change
305 *---------------------------------------------------------------------------*
308 ibc_state_changed(struct sppp *sp, int new_state)
310 struct ibc_softc *sc = (struct ibc_softc *)sp;
312 i4b_l4_ifstate_changed(sc->sc_cdp, new_state);
315 /*---------------------------------------------------------------------------*
316 * PPP control protocol negotiation complete (run ip-up script now)
317 *---------------------------------------------------------------------------*
320 ibc_negotiation_complete(struct sppp *sp)
322 struct ibc_softc *sc = (struct ibc_softc *)sp;
324 i4b_l4_negcomplete(sc->sc_cdp);
328 /*===========================================================================*
329 * ISDN INTERFACE ROUTINES
330 *===========================================================================*/
332 /*---------------------------------------------------------------------------*
333 * this routine is called from L4 handler at connect time
334 *---------------------------------------------------------------------------*/
336 ibc_connect(int unit, void *cdp)
338 struct ibc_softc *sc = &ibc_softc[unit];
339 struct ifnet *ifp = &sc->sc_p2pcom.p2p_if;
342 DBG(("ibc%d: ibc_connect\n", unit));
346 sc->sc_cdp = (call_desc_t *)cdp;
347 sc->sc_state = ST_CONNECTED;
356 ifp->if_timer = IBCACCTINTVL;
360 if (sc->sc_p2pcom.p2p_modem)
361 (*sc->sc_p2pcom.p2p_modem)(&sc->sc_p2pcom, 1);
363 /* This is a lie... PPP is just starting to negociate :-) */
364 i4b_l4_negcomplete(sc->sc_cdp);
367 /*---------------------------------------------------------------------------*
368 * this routine is called from L4 handler at disconnect time
369 *---------------------------------------------------------------------------*/
371 ibc_disconnect(int unit, void *cdp)
373 call_desc_t *cd = (call_desc_t *)cdp;
374 struct ibc_softc *sc = &ibc_softc[unit];
375 struct ifnet *ifp = &sc->sc_p2pcom.p2p_if;
378 DBG(("ibc%d: ibc_disconnect\n", unit));
382 /* new stuff to check that the active channel is being closed */
383 if (cd != sc->sc_cdp)
385 DBG(("ibc_disconnect: ibc%d channel%d not active\n",
386 cd->driver_unit, cd->channelid));
395 i4b_l4_accounting(BDRV_IBC, unit, ACCT_FINAL,
396 sc->sc_ioutb, sc->sc_iinb, 0, 0, sc->sc_outb, sc->sc_inb);
398 if (sc->sc_state == ST_CONNECTED)
400 sc->sc_cdp = (call_desc_t *)0;
401 sc->sc_state = ST_IDLE;
402 if (sc->sc_p2pcom.p2p_modem)
403 (*sc->sc_p2pcom.p2p_modem)(&sc->sc_p2pcom, 0);
409 /*---------------------------------------------------------------------------*
410 * this routine is used to give a feedback from userland demon
411 * in case of dial problems
412 *---------------------------------------------------------------------------*/
414 ibc_dialresponse(int unit, int status)
416 DBG(("ibc%d: ibc_dialresponse %d\n", unit, status));
417 /* struct ibc_softc *sc = &ibc_softc[unit]; */
420 /*---------------------------------------------------------------------------*
422 *---------------------------------------------------------------------------*/
424 ibc_updown(int unit, int updown)
426 DBG(("ibc%d: ibc_updown %d\n", unit, updown));
427 /* could probably do something useful here */
430 /*---------------------------------------------------------------------------*
431 * this routine is called from the HSCX interrupt handler
432 * when a new frame (mbuf) has been received and was put on
434 *---------------------------------------------------------------------------*/
436 ibc_rx_data_rdy(int unit)
438 struct ibc_softc *sc = &ibc_softc[unit];
439 struct ifnet *ifp = &sc->sc_p2pcom.p2p_if;
444 if((m = *isdn_ibc_lt[unit]->rx_mbuf) == NULL)
447 microtime(&ifp->if_lastchange);
450 /* Walk the mbuf chain */
452 for (m0 = m; m != 0; m = m->m_next) {
455 ifp->if_ibytes += m->m_len;
457 sc->sc_inb += m->m_len;
459 buf = mtod(m, caddr_t);
460 if ((*sc->sc_p2pcom.p2p_hdrinput)(
461 &sc->sc_p2pcom, buf, m->m_len) >= 0)
462 (*sc->sc_p2pcom.p2p_input)(&sc->sc_p2pcom, 0);
468 /*---------------------------------------------------------------------------*
469 * this routine is called from the HSCX interrupt handler
470 * when the last frame has been sent out and there is no
471 * further frame (mbuf) in the tx queue.
472 *---------------------------------------------------------------------------*/
474 ibc_tx_queue_empty(int unit)
476 ibc_start(&ibc_softc[unit].sc_p2pcom.p2p_if);
479 /*---------------------------------------------------------------------------*
480 * this routine is called from the HSCX interrupt handler
481 * each time a packet is received or transmitted. It should
482 * be used to implement an activity timeout mechanism.
483 *---------------------------------------------------------------------------*/
485 ibc_activity(int unit, int rxtx)
487 ibc_softc[unit].sc_cdp->last_active_time = SECOND;
490 /*---------------------------------------------------------------------------*
491 * return this drivers linktab address
492 *---------------------------------------------------------------------------*/
494 ibc_ret_linktab(int unit)
496 return(&ibc_drvr_linktab[unit]);
499 /*---------------------------------------------------------------------------*
500 * setup the isdn_ibc_lt for this driver
501 *---------------------------------------------------------------------------*/
503 ibc_set_linktab(int unit, isdn_link_t *ilt)
505 isdn_ibc_lt[unit] = ilt;
508 /*---------------------------------------------------------------------------*
509 * initialize this drivers linktab
510 *---------------------------------------------------------------------------*/
512 ibc_init_linktab(int unit)
514 ibc_drvr_linktab[unit].unit = unit;
515 ibc_drvr_linktab[unit].bch_rx_data_ready = ibc_rx_data_rdy;
516 ibc_drvr_linktab[unit].bch_tx_queue_empty = ibc_tx_queue_empty;
517 ibc_drvr_linktab[unit].bch_activity = ibc_activity;
518 ibc_drvr_linktab[unit].line_connected = ibc_connect;
519 ibc_drvr_linktab[unit].line_disconnected = ibc_disconnect;
520 ibc_drvr_linktab[unit].dial_response = ibc_dialresponse;
521 ibc_drvr_linktab[unit].updown_ind = ibc_updown;
524 /*===========================================================================*/
531 struct ifnet *ifp = &pp->p2p_if;
532 struct ibc_softc *sc = (struct ibc_softc *)&ibc_softc[ifp->if_dunit];
534 DBG(("ibc%d: ibc_mdmctl called flags=%d\n", IFP2UNIT(ifp), flag));
536 if (flag == 1 && sc->sc_state == ST_IDLE) {
537 sc->sc_state = ST_DIALING;
538 i4b_l4_dialout(BDRV_IBC, IFP2UNIT(ifp));
539 } else if (flag == 0 && sc->sc_state != ST_IDLE) {
540 sc->sc_state = ST_IDLE;
541 i4b_l4_drvrdisc(BDRV_IBC, IFP2UNIT(ifp));
551 struct ifnet *ifp = &pp->p2p_if;
552 struct ibc_softc *sc = (struct ibc_softc *)&ibc_softc[ifp->if_dunit];
554 if (sc->sc_state == ST_CONNECTED)
555 *(int *)arg = TIOCM_CAR;
560 DBG(("ibc%d: ibc_getmdm called ret=%d\n", IFP2UNIT(ifp), *(int *)arg));