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.4 2003/08/07 21: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_unit
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 ifp->if_name = "ibc";
154 ifp->if_mtu = 1500 /*XXX*/;
155 ifp->if_baudrate = 64000;
156 ifp->if_flags = IFF_SIMPLEX | IFF_POINTOPOINT;
157 ifp->if_type = IFT_ISDNBASIC;
158 ifp->if_start = ibc_start;
160 ifp->if_ioctl = p2p_ioctl;
164 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
166 ifp->if_ipackets = 0;
168 ifp->if_opackets = 0;
170 ifp->if_collisions = 0;
179 ifp->if_watchdog = ibc_watchdog;
188 #if defined(__FreeBSD_version) && ((__FreeBSD_version >= 500009) || (410000 <= __FreeBSD_version && __FreeBSD_version < 500000))
189 ether_ifattach(ifp, 0);
193 p2p_attach(&sc->sc_p2pcom);
198 p2p_dequeue(struct p2pcom *pp)
202 IF_DEQUEUE(&pp->p2p_isnd, m);
204 IF_DEQUEUE(&pp->p2p_if.if_snd, m);
208 /*---------------------------------------------------------------------------*
209 * start output to ISDN B-channel
210 *---------------------------------------------------------------------------*/
212 ibc_start(struct ifnet *ifp)
214 int unit = IFP2UNIT(ifp);
215 struct ibc_softc *sc = (struct ibc_softc *)&ibc_softc[unit];
216 struct p2pcom *pp = &sc->sc_p2pcom;
220 if(sc->sc_state != ST_CONNECTED) {
221 DBG(("ibc%d: ibc_start called with sc_state=%d\n",
222 unit, sc->sc_state));
228 IF_LOCK(isdn_ibc_lt[unit]->tx_queue);
229 if (_IF_QFULL(isdn_ibc_lt[unit]->tx_queue)) {
230 IF_UNLOCK(isdn_ibc_lt[unit]->tx_queue);
237 IF_UNLOCK(isdn_ibc_lt[unit]->tx_queue);
243 microtime(&ifp->if_lastchange);
245 ifp->if_obytes += m->m_pkthdr.len;
246 sc->sc_outb += m->m_pkthdr.len;
247 _IF_ENQUEUE(isdn_ibc_lt[unit]->tx_queue, m);
250 } while (!_IF_QFULL(isdn_ibc_lt[unit]->tx_queue) &&
251 (m = p2p_dequeue(pp)) != NULL);
252 IF_UNLOCK(isdn_ibc_lt[unit]->tx_queue);
253 isdn_ibc_lt[unit]->bch_tx_start(isdn_ibc_lt[unit]->unit,
254 isdn_ibc_lt[unit]->channel);
260 /*---------------------------------------------------------------------------*
262 *---------------------------------------------------------------------------*/
264 ibc_watchdog(int unit)
266 struct ibc_softc *sc = &ibc_softc[unit];
267 struct ifnet *ifp = &sc->sc_p2pcom.p2p_if;
268 bchan_statistics_t bs;
270 (*isdn_ibc_lt[unit]->bch_stat)
271 (isdn_ibc_lt[unit]->unit, isdn_ibc_lt[unit]->channel, &bs);
273 sc->sc_ioutb += bs.outbytes;
274 sc->sc_iinb += bs.inbytes;
276 if((sc->sc_iinb != sc->sc_linb) || (sc->sc_ioutb != sc->sc_loutb) || sc->sc_fn)
278 int ri = (sc->sc_iinb - sc->sc_linb)/IBCACCTINTVL;
279 int ro = (sc->sc_ioutb - sc->sc_loutb)/IBCACCTINTVL;
281 if((sc->sc_iinb == sc->sc_linb) && (sc->sc_ioutb == sc->sc_loutb))
286 sc->sc_linb = sc->sc_iinb;
287 sc->sc_loutb = sc->sc_ioutb;
289 i4b_l4_accounting(BDRV_IBC, unit, ACCT_DURING,
290 sc->sc_ioutb, sc->sc_iinb, ro, ri, sc->sc_outb, sc->sc_inb);
292 ifp->if_timer = IBCACCTINTVL;
298 *===========================================================================*
299 * P2P layer interface routines
300 *===========================================================================*
304 /*---------------------------------------------------------------------------*
305 * PPP interface phase change
306 *---------------------------------------------------------------------------*
309 ibc_state_changed(struct sppp *sp, int new_state)
311 struct ibc_softc *sc = (struct ibc_softc *)sp;
313 i4b_l4_ifstate_changed(sc->sc_cdp, new_state);
316 /*---------------------------------------------------------------------------*
317 * PPP control protocol negotiation complete (run ip-up script now)
318 *---------------------------------------------------------------------------*
321 ibc_negotiation_complete(struct sppp *sp)
323 struct ibc_softc *sc = (struct ibc_softc *)sp;
325 i4b_l4_negcomplete(sc->sc_cdp);
329 /*===========================================================================*
330 * ISDN INTERFACE ROUTINES
331 *===========================================================================*/
333 /*---------------------------------------------------------------------------*
334 * this routine is called from L4 handler at connect time
335 *---------------------------------------------------------------------------*/
337 ibc_connect(int unit, void *cdp)
339 struct ibc_softc *sc = &ibc_softc[unit];
340 struct ifnet *ifp = &sc->sc_p2pcom.p2p_if;
343 DBG(("ibc%d: ibc_connect\n", unit));
347 sc->sc_cdp = (call_desc_t *)cdp;
348 sc->sc_state = ST_CONNECTED;
357 ifp->if_timer = IBCACCTINTVL;
361 if (sc->sc_p2pcom.p2p_modem)
362 (*sc->sc_p2pcom.p2p_modem)(&sc->sc_p2pcom, 1);
364 /* This is a lie... PPP is just starting to negociate :-) */
365 i4b_l4_negcomplete(sc->sc_cdp);
368 /*---------------------------------------------------------------------------*
369 * this routine is called from L4 handler at disconnect time
370 *---------------------------------------------------------------------------*/
372 ibc_disconnect(int unit, void *cdp)
374 call_desc_t *cd = (call_desc_t *)cdp;
375 struct ibc_softc *sc = &ibc_softc[unit];
376 struct ifnet *ifp = &sc->sc_p2pcom.p2p_if;
379 DBG(("ibc%d: ibc_disconnect\n", unit));
383 /* new stuff to check that the active channel is being closed */
384 if (cd != sc->sc_cdp)
386 DBG(("ibc_disconnect: ibc%d channel%d not active\n",
387 cd->driver_unit, cd->channelid));
396 i4b_l4_accounting(BDRV_IBC, unit, ACCT_FINAL,
397 sc->sc_ioutb, sc->sc_iinb, 0, 0, sc->sc_outb, sc->sc_inb);
399 if (sc->sc_state == ST_CONNECTED)
401 sc->sc_cdp = (call_desc_t *)0;
402 sc->sc_state = ST_IDLE;
403 if (sc->sc_p2pcom.p2p_modem)
404 (*sc->sc_p2pcom.p2p_modem)(&sc->sc_p2pcom, 0);
410 /*---------------------------------------------------------------------------*
411 * this routine is used to give a feedback from userland demon
412 * in case of dial problems
413 *---------------------------------------------------------------------------*/
415 ibc_dialresponse(int unit, int status)
417 DBG(("ibc%d: ibc_dialresponse %d\n", unit, status));
418 /* struct ibc_softc *sc = &ibc_softc[unit]; */
421 /*---------------------------------------------------------------------------*
423 *---------------------------------------------------------------------------*/
425 ibc_updown(int unit, int updown)
427 DBG(("ibc%d: ibc_updown %d\n", unit, updown));
428 /* could probably do something useful here */
431 /*---------------------------------------------------------------------------*
432 * this routine is called from the HSCX interrupt handler
433 * when a new frame (mbuf) has been received and was put on
435 *---------------------------------------------------------------------------*/
437 ibc_rx_data_rdy(int unit)
439 struct ibc_softc *sc = &ibc_softc[unit];
440 struct ifnet *ifp = &sc->sc_p2pcom.p2p_if;
445 if((m = *isdn_ibc_lt[unit]->rx_mbuf) == NULL)
448 microtime(&ifp->if_lastchange);
451 /* Walk the mbuf chain */
453 for (m0 = m; m != 0; m = m->m_next) {
456 ifp->if_ibytes += m->m_len;
458 sc->sc_inb += m->m_len;
460 buf = mtod(m, caddr_t);
461 if ((*sc->sc_p2pcom.p2p_hdrinput)(
462 &sc->sc_p2pcom, buf, m->m_len) >= 0)
463 (*sc->sc_p2pcom.p2p_input)(&sc->sc_p2pcom, 0);
469 /*---------------------------------------------------------------------------*
470 * this routine is called from the HSCX interrupt handler
471 * when the last frame has been sent out and there is no
472 * further frame (mbuf) in the tx queue.
473 *---------------------------------------------------------------------------*/
475 ibc_tx_queue_empty(int unit)
477 ibc_start(&ibc_softc[unit].sc_p2pcom.p2p_if);
480 /*---------------------------------------------------------------------------*
481 * this routine is called from the HSCX interrupt handler
482 * each time a packet is received or transmitted. It should
483 * be used to implement an activity timeout mechanism.
484 *---------------------------------------------------------------------------*/
486 ibc_activity(int unit, int rxtx)
488 ibc_softc[unit].sc_cdp->last_active_time = SECOND;
491 /*---------------------------------------------------------------------------*
492 * return this drivers linktab address
493 *---------------------------------------------------------------------------*/
495 ibc_ret_linktab(int unit)
497 return(&ibc_drvr_linktab[unit]);
500 /*---------------------------------------------------------------------------*
501 * setup the isdn_ibc_lt for this driver
502 *---------------------------------------------------------------------------*/
504 ibc_set_linktab(int unit, isdn_link_t *ilt)
506 isdn_ibc_lt[unit] = ilt;
509 /*---------------------------------------------------------------------------*
510 * initialize this drivers linktab
511 *---------------------------------------------------------------------------*/
513 ibc_init_linktab(int unit)
515 ibc_drvr_linktab[unit].unit = unit;
516 ibc_drvr_linktab[unit].bch_rx_data_ready = ibc_rx_data_rdy;
517 ibc_drvr_linktab[unit].bch_tx_queue_empty = ibc_tx_queue_empty;
518 ibc_drvr_linktab[unit].bch_activity = ibc_activity;
519 ibc_drvr_linktab[unit].line_connected = ibc_connect;
520 ibc_drvr_linktab[unit].line_disconnected = ibc_disconnect;
521 ibc_drvr_linktab[unit].dial_response = ibc_dialresponse;
522 ibc_drvr_linktab[unit].updown_ind = ibc_updown;
525 /*===========================================================================*/
532 struct ifnet *ifp = &pp->p2p_if;
533 struct ibc_softc *sc = (struct ibc_softc *)&ibc_softc[ifp->if_unit];
535 DBG(("ibc%d: ibc_mdmctl called flags=%d\n", IFP2UNIT(ifp), flag));
537 if (flag == 1 && sc->sc_state == ST_IDLE) {
538 sc->sc_state = ST_DIALING;
539 i4b_l4_dialout(BDRV_IBC, IFP2UNIT(ifp));
540 } else if (flag == 0 && sc->sc_state != ST_IDLE) {
541 sc->sc_state = ST_IDLE;
542 i4b_l4_drvrdisc(BDRV_IBC, IFP2UNIT(ifp));
552 struct ifnet *ifp = &pp->p2p_if;
553 struct ibc_softc *sc = (struct ibc_softc *)&ibc_softc[ifp->if_unit];
555 if (sc->sc_state == ST_CONNECTED)
556 *(int *)arg = TIOCM_CAR;
561 DBG(("ibc%d: ibc_getmdm called ret=%d\n", IFP2UNIT(ifp), *(int *)arg));