2 * Copyright (c) 1997, 2001 Hellmuth Michaelis. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 *---------------------------------------------------------------------------
27 * i4b_rbch.c - device driver for raw B channel data
28 * ---------------------------------------------------
30 * $FreeBSD: src/sys/i4b/driver/i4b_rbch.c,v 1.10.2.3 2001/08/12 16:22:48 hm Exp $
31 * $DragonFly: src/sys/net/i4b/driver/i4b_rbch.c,v 1.22 2006/12/22 23:44:55 swildner Exp $
33 * last edit-date: [Sat Aug 11 18:06:57 2001]
35 *---------------------------------------------------------------------------*/
37 #include "use_i4brbch.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
46 #include <sys/kernel.h>
48 #include <sys/socket.h>
51 #include <sys/thread2.h>
52 #include <sys/vnode.h>
54 #include <net/i4b/include/machine/i4b_ioctl.h>
55 #include <net/i4b/include/machine/i4b_rbch_ioctl.h>
56 #include <net/i4b/include/machine/i4b_debug.h>
58 #include "../include/i4b_global.h"
59 #include "../include/i4b_mbuf.h"
60 #include "../include/i4b_l3l4.h"
61 #include "../layer4/i4b_l4.h"
64 #include <sys/filio.h>
66 static drvr_link_t rbch_drvr_linktab[NI4BRBCH];
67 static isdn_link_t *isdn_linktab[NI4BRBCH];
69 #define I4BRBCHACCT 1 /* enable accounting messages */
70 #define I4BRBCHACCTINTVL 2 /* accounting msg interval in secs */
72 static struct rbch_softc {
74 int sc_unit; /* unit number */
76 int sc_devstate; /* state of driver */
78 #define ST_CONNECTED 0x01
79 #define ST_ISOPEN 0x02
80 #define ST_RDWAITDATA 0x04
81 #define ST_WRWAITEMPTY 0x08
82 #define ST_NOBLOCK 0x10
84 int sc_bprot; /* B-ch protocol used */
86 call_desc_t *sc_cd; /* Call Descriptor */
90 struct ifqueue sc_hdlcq; /* hdlc read queue */
91 #define I4BRBCHMAXQLEN 10
93 struct selinfo selp; /* select / poll */
96 struct callout sc_timeout;
98 int sc_iinb; /* isdn driver # of inbytes */
99 int sc_ioutb; /* isdn driver # of outbytes */
100 int sc_linb; /* last # of bytes rx'd */
101 int sc_loutb; /* last # of bytes tx'd */
102 int sc_fn; /* flag, first null acct */
104 } rbch_softc[NI4BRBCH];
106 static void rbch_rx_data_rdy(int unit);
107 static void rbch_tx_queue_empty(int unit);
108 static void rbch_connect(int unit, void *cdp);
109 static void rbch_disconnect(int unit, void *cdp);
110 static void rbch_init_linktab(int unit);
111 static void rbch_clrq(int unit);
113 #define PDEVSTATIC static
114 #define IOCTL_CMD_T u_long
116 PDEVSTATIC d_open_t i4brbchopen;
117 PDEVSTATIC d_close_t i4brbchclose;
118 PDEVSTATIC d_read_t i4brbchread;
119 PDEVSTATIC d_write_t i4brbchwrite;
120 PDEVSTATIC d_ioctl_t i4brbchioctl;
122 PDEVSTATIC d_poll_t i4brbchpoll;
123 #define POLLFIELD i4brbchpoll
125 #define CDEV_MAJOR 57
127 static struct dev_ops i4brbch_ops = {
128 { "i4brbch", CDEV_MAJOR, 0 },
129 .d_open = i4brbchopen,
130 .d_close = i4brbchclose,
131 .d_read = i4brbchread,
132 .d_write = i4brbchwrite,
133 .d_ioctl = i4brbchioctl,
137 static void i4brbchattach(void *);
138 PSEUDO_SET(i4brbchattach, i4b_rbch);
140 /*===========================================================================*
141 * DEVICE DRIVER ROUTINES
142 *===========================================================================*/
144 /*---------------------------------------------------------------------------*
145 * initialization at kernel load time
146 *---------------------------------------------------------------------------*/
148 i4brbchinit(void *unused)
150 dev_ops_add(&i4brbch_ops, 0, 0);
153 SYSINIT(i4brbchdev, SI_SUB_DRIVERS,
154 SI_ORDER_MIDDLE+CDEV_MAJOR, &i4brbchinit, NULL);
156 /*---------------------------------------------------------------------------*
157 * interface attach routine
158 *---------------------------------------------------------------------------*/
160 i4brbchattach(void *dummy)
164 #ifndef HACK_NO_PSEUDO_ATTACH_MSG
165 kprintf("i4brbch: %d raw B channel access device(s) attached\n", NI4BRBCH);
168 for(i=0; i < NI4BRBCH; i++)
170 make_dev(&i4brbch_ops, i,
171 UID_ROOT, GID_WHEEL, 0600, "i4brbch%d", i);
174 callout_init(&rbch_softc[i].sc_timeout);
175 rbch_softc[i].sc_fn = 1;
177 rbch_softc[i].sc_unit = i;
178 rbch_softc[i].sc_devstate = ST_IDLE;
179 rbch_softc[i].sc_hdlcq.ifq_maxlen = I4BRBCHMAXQLEN;
180 rbch_softc[i].it_in.c_ispeed = rbch_softc[i].it_in.c_ospeed = 64000;
181 termioschars(&rbch_softc[i].it_in);
182 rbch_init_linktab(i);
186 /*---------------------------------------------------------------------------*
188 *---------------------------------------------------------------------------*/
190 i4brbchopen(struct dev_open_args *ap)
192 cdev_t dev = ap->a_head.a_dev;
193 int unit = minor(dev);
198 if(rbch_softc[unit].sc_devstate & ST_ISOPEN)
205 rbch_softc[unit].sc_devstate |= ST_ISOPEN;
207 NDBGL4(L4_RBCHDBG, "unit %d, open", unit);
212 /*---------------------------------------------------------------------------*
214 *---------------------------------------------------------------------------*/
216 i4brbchclose(struct dev_close_args *ap)
218 cdev_t dev = ap->a_head.a_dev;
219 int unit = minor(dev);
220 struct rbch_softc *sc = &rbch_softc[unit];
222 if(sc->sc_devstate & ST_CONNECTED)
223 i4b_l4_drvrdisc(BDRV_RBCH, unit);
225 sc->sc_devstate &= ~ST_ISOPEN;
229 NDBGL4(L4_RBCHDBG, "unit %d, closed", unit);
234 /*---------------------------------------------------------------------------*
235 * read from rbch device
236 *---------------------------------------------------------------------------*/
238 i4brbchread(struct dev_read_args *ap)
240 cdev_t dev = ap->a_head.a_dev;
241 struct uio *uio = ap->a_uio;
244 int unit = minor(dev);
246 struct rbch_softc *sc = &rbch_softc[unit];
250 NDBGL4(L4_RBCHDBG, "unit %d, enter read", unit);
253 if(!(sc->sc_devstate & ST_ISOPEN))
256 NDBGL4(L4_RBCHDBG, "unit %d, read while not open", unit);
260 if((sc->sc_devstate & ST_NOBLOCK) || (ap->a_ioflag & IO_NDELAY))
262 if(!(sc->sc_devstate & ST_CONNECTED)) {
267 if(sc->sc_bprot == BPROT_RHDLC)
270 iqp = isdn_linktab[unit]->rx_queue;
272 if(IF_QEMPTY(iqp) && (sc->sc_devstate & ST_ISOPEN)) {
279 while(!(sc->sc_devstate & ST_CONNECTED))
281 NDBGL4(L4_RBCHDBG, "unit %d, wait read init", unit);
283 if((error = tsleep((caddr_t) &rbch_softc[unit],
284 PCATCH, "rrrbch", 0 )) != 0)
287 NDBGL4(L4_RBCHDBG, "unit %d, error %d tsleep", unit, error);
292 if(sc->sc_bprot == BPROT_RHDLC)
295 iqp = isdn_linktab[unit]->rx_queue;
297 while(IF_QEMPTY(iqp) && (sc->sc_devstate & ST_ISOPEN))
299 sc->sc_devstate |= ST_RDWAITDATA;
301 NDBGL4(L4_RBCHDBG, "unit %d, wait read data", unit);
303 if((error = tsleep((caddr_t) &isdn_linktab[unit]->rx_queue,
304 PCATCH, "rrbch", 0 )) != 0)
307 NDBGL4(L4_RBCHDBG, "unit %d, error %d tsleep read", unit, error);
308 sc->sc_devstate &= ~ST_RDWAITDATA;
316 NDBGL4(L4_RBCHDBG, "unit %d, read %d bytes", unit, m->m_len);
320 error = uiomove(m->m_data, m->m_len, uio);
324 NDBGL4(L4_RBCHDBG, "unit %d, error %d uiomove", unit, error);
336 /*---------------------------------------------------------------------------*
337 * write to rbch device
338 *---------------------------------------------------------------------------*/
340 i4brbchwrite(struct dev_write_args *ap)
342 cdev_t dev = ap->a_head.a_dev;
343 struct uio *uio = ap->a_uio;
346 int unit = minor(dev);
347 struct rbch_softc *sc = &rbch_softc[unit];
351 NDBGL4(L4_RBCHDBG, "unit %d, write", unit);
354 if(!(sc->sc_devstate & ST_ISOPEN))
356 NDBGL4(L4_RBCHDBG, "unit %d, write while not open", unit);
361 if((sc->sc_devstate & ST_NOBLOCK) || (ap->a_ioflag & IO_NDELAY))
363 if(!(sc->sc_devstate & ST_CONNECTED)) {
367 if(IF_QFULL(isdn_linktab[unit]->tx_queue) && (sc->sc_devstate & ST_ISOPEN)) {
374 while(!(sc->sc_devstate & ST_CONNECTED))
376 NDBGL4(L4_RBCHDBG, "unit %d, write wait init", unit);
378 error = tsleep((caddr_t) &rbch_softc[unit],
379 PCATCH, "wrrbch", 0 );
380 if(error == ERESTART) {
384 else if(error == EINTR)
387 NDBGL4(L4_RBCHDBG, "unit %d, EINTR during wait init", unit);
393 NDBGL4(L4_RBCHDBG, "unit %d, error %d tsleep init", unit, error);
396 tsleep((caddr_t) &rbch_softc[unit], PCATCH, "xrbch", (hz*1));
399 while(IF_QFULL(isdn_linktab[unit]->tx_queue) && (sc->sc_devstate & ST_ISOPEN))
401 sc->sc_devstate |= ST_WRWAITEMPTY;
403 NDBGL4(L4_RBCHDBG, "unit %d, write queue full", unit);
405 if ((error = tsleep((caddr_t) &isdn_linktab[unit]->tx_queue,
406 PCATCH, "wrbch", 0)) != 0) {
407 sc->sc_devstate &= ~ST_WRWAITEMPTY;
408 if(error == ERESTART)
413 else if(error == EINTR)
416 NDBGL4(L4_RBCHDBG, "unit %d, EINTR during wait write", unit);
422 NDBGL4(L4_RBCHDBG, "unit %d, error %d tsleep write", unit, error);
429 if(!(sc->sc_devstate & ST_ISOPEN))
431 NDBGL4(L4_RBCHDBG, "unit %d, not open anymore", unit);
436 if((m = i4b_Bgetmbuf(BCH_MAX_DATALEN)) != NULL)
438 m->m_len = min(BCH_MAX_DATALEN, uio->uio_resid);
440 NDBGL4(L4_RBCHDBG, "unit %d, write %d bytes", unit, m->m_len);
442 error = uiomove(m->m_data, m->m_len, uio);
444 if(IF_QFULL(isdn_linktab[unit]->tx_queue))
447 IF_ENQUEUE(isdn_linktab[unit]->tx_queue, m);
448 (*isdn_linktab[unit]->bch_tx_start)(isdn_linktab[unit]->unit, isdn_linktab[unit]->channel);
456 /*---------------------------------------------------------------------------*
457 * rbch device ioctl handlibg
458 *---------------------------------------------------------------------------*/
460 i4brbchioctl(struct dev_ioctl_args *ap)
462 cdev_t dev = ap->a_head.a_dev;
464 int unit = minor(dev);
465 struct rbch_softc *sc = &rbch_softc[unit];
469 case FIOASYNC: /* Set async mode */
470 if (*(int *)ap->a_data)
472 NDBGL4(L4_RBCHDBG, "unit %d, setting async mode", unit);
476 NDBGL4(L4_RBCHDBG, "unit %d, clearing async mode", unit);
480 case TIOCCDTR: /* Clear DTR */
481 if(sc->sc_devstate & ST_CONNECTED)
483 NDBGL4(L4_RBCHDBG, "unit %d, disconnecting for DTR down", unit);
484 i4b_l4_drvrdisc(BDRV_RBCH, unit);
488 case I4B_RBCH_DIALOUT:
492 for (l = 0; l < TELNO_MAX && ((char *)ap->a_data)[l]; l++)
496 NDBGL4(L4_RBCHDBG, "unit %d, attempting dialout to %s", unit, (char *)ap->a_data);
497 i4b_l4_dialoutnumber(BDRV_RBCH, unit, l, (char *)ap->a_data);
500 /* fall through to SDTR */
503 case TIOCSDTR: /* Set DTR */
504 NDBGL4(L4_RBCHDBG, "unit %d, attempting dialout (DTR)", unit);
505 i4b_l4_dialout(BDRV_RBCH, unit);
508 case TIOCSETA: /* Set termios struct */
511 case TIOCGETA: /* Get termios struct */
512 *(struct termios *)ap->a_data = sc->it_in;
516 *(int *)ap->a_data = TIOCM_LE|TIOCM_DTR|TIOCM_RTS|TIOCM_CTS|TIOCM_DSR;
517 if (sc->sc_devstate & ST_CONNECTED)
518 *(int *)ap->a_data |= TIOCM_CD;
521 case I4B_RBCH_VR_REQ:
525 mvr = (msg_vr_req_t *)ap->a_data;
527 mvr->version = VERSION;
533 default: /* Unknown stuff */
534 NDBGL4(L4_RBCHDBG, "unit %d, ioctl, unknown cmd %lx", unit, ap->a_cmd);
541 /*---------------------------------------------------------------------------*
543 *---------------------------------------------------------------------------*/
545 i4brbchpoll(struct dev_poll_args *ap)
547 cdev_t dev = ap->a_head.a_dev;
548 int revents = 0; /* Events we found */
549 int unit = minor(dev);
550 struct rbch_softc *sc = &rbch_softc[unit];
552 /* We can't check for anything but IN or OUT */
555 if(!(sc->sc_devstate & ST_ISOPEN))
562 * Writes are OK if we are connected and the
563 * transmit queue can take them
566 if((ap->a_events & (POLLOUT|POLLWRNORM)) &&
567 (sc->sc_devstate & ST_CONNECTED) &&
568 !IF_QFULL(isdn_linktab[unit]->tx_queue))
570 revents |= (ap->a_events & (POLLOUT|POLLWRNORM));
573 /* ... while reads are OK if we have any data */
575 if((ap->a_events & (POLLIN|POLLRDNORM)) &&
576 (sc->sc_devstate & ST_CONNECTED))
580 if(sc->sc_bprot == BPROT_RHDLC)
583 iqp = isdn_linktab[unit]->rx_queue;
586 revents |= (ap->a_events & (POLLIN|POLLRDNORM));
590 selrecord(curthread, &sc->selp);
593 ap->a_events = revents;
598 /*---------------------------------------------------------------------------*
600 *---------------------------------------------------------------------------*/
602 rbch_timeout(struct rbch_softc *sc)
604 bchan_statistics_t bs;
605 int unit = sc->sc_unit;
607 /* get # of bytes in and out from the HSCX driver */
609 (*isdn_linktab[unit]->bch_stat)
610 (isdn_linktab[unit]->unit, isdn_linktab[unit]->channel, &bs);
612 sc->sc_ioutb += bs.outbytes;
613 sc->sc_iinb += bs.inbytes;
615 if((sc->sc_iinb != sc->sc_linb) || (sc->sc_ioutb != sc->sc_loutb) || sc->sc_fn)
617 int ri = (sc->sc_iinb - sc->sc_linb)/I4BRBCHACCTINTVL;
618 int ro = (sc->sc_ioutb - sc->sc_loutb)/I4BRBCHACCTINTVL;
620 if((sc->sc_iinb == sc->sc_linb) && (sc->sc_ioutb == sc->sc_loutb))
625 sc->sc_linb = sc->sc_iinb;
626 sc->sc_loutb = sc->sc_ioutb;
628 i4b_l4_accounting(BDRV_RBCH, unit, ACCT_DURING,
629 sc->sc_ioutb, sc->sc_iinb, ro, ri, sc->sc_ioutb, sc->sc_iinb);
631 callout_reset(&sc->sc_timeout, I4BRBCHACCTINTVL * hz,
632 (void *)rbch_timeout, sc);
634 #endif /* I4BRBCHACCT */
636 /*===========================================================================*
637 * ISDN INTERFACE ROUTINES
638 *===========================================================================*/
640 /*---------------------------------------------------------------------------*
641 * this routine is called from L4 handler at connect time
642 *---------------------------------------------------------------------------*/
644 rbch_connect(int unit, void *cdp)
646 call_desc_t *cd = (call_desc_t *)cdp;
647 struct rbch_softc *sc = &rbch_softc[unit];
649 sc->sc_bprot = cd->bprot;
652 if(sc->sc_bprot == BPROT_RHDLC)
659 callout_reset(&sc->sc_timeout, I4BRBCHACCTINTVL * hz,
660 (void *)rbch_timeout, sc);
663 if(!(sc->sc_devstate & ST_CONNECTED))
665 NDBGL4(L4_RBCHDBG, "unit %d, wakeup", unit);
666 sc->sc_devstate |= ST_CONNECTED;
672 /*---------------------------------------------------------------------------*
673 * this routine is called from L4 handler at disconnect time
674 *---------------------------------------------------------------------------*/
676 rbch_disconnect(int unit, void *cdp)
678 call_desc_t *cd = (call_desc_t *)cdp;
679 struct rbch_softc *sc = &rbch_softc[unit];
685 NDBGL4(L4_RBCHDBG, "rbch%d: channel %d not active",
686 cd->driver_unit, cd->channelid);
692 NDBGL4(L4_RBCHDBG, "unit %d, disconnect", unit);
694 sc->sc_devstate &= ~ST_CONNECTED;
699 i4b_l4_accounting(BDRV_RBCH, unit, ACCT_FINAL,
700 sc->sc_ioutb, sc->sc_iinb, 0, 0, sc->sc_ioutb, sc->sc_iinb);
701 callout_stop(&sc->sc_timeout);
706 /*---------------------------------------------------------------------------*
707 * feedback from daemon in case of dial problems
708 *---------------------------------------------------------------------------*/
710 rbch_dialresponse(int unit, int status, cause_t cause)
714 /*---------------------------------------------------------------------------*
716 *---------------------------------------------------------------------------*/
718 rbch_updown(int unit, int updown)
722 /*---------------------------------------------------------------------------*
723 * this routine is called from the HSCX interrupt handler
724 * when a new frame (mbuf) has been received and is to be put on
726 *---------------------------------------------------------------------------*/
728 rbch_rx_data_rdy(int unit)
730 if(rbch_softc[unit].sc_bprot == BPROT_RHDLC)
734 if((m = *isdn_linktab[unit]->rx_mbuf) == NULL)
737 m->m_pkthdr.len = m->m_len;
739 if(IF_QFULL(&(rbch_softc[unit].sc_hdlcq)))
741 NDBGL4(L4_RBCHDBG, "unit %d: hdlc rx queue full!", unit);
746 IF_ENQUEUE(&(rbch_softc[unit].sc_hdlcq), m);
750 if(rbch_softc[unit].sc_devstate & ST_RDWAITDATA)
752 NDBGL4(L4_RBCHDBG, "unit %d, wakeup", unit);
753 rbch_softc[unit].sc_devstate &= ~ST_RDWAITDATA;
754 wakeup((caddr_t) &isdn_linktab[unit]->rx_queue);
758 NDBGL4(L4_RBCHDBG, "unit %d, NO wakeup", unit);
760 selwakeup(&rbch_softc[unit].selp);
763 /*---------------------------------------------------------------------------*
764 * this routine is called from the HSCX interrupt handler
765 * when the last frame has been sent out and there is no
766 * further frame (mbuf) in the tx queue.
767 *---------------------------------------------------------------------------*/
769 rbch_tx_queue_empty(int unit)
771 if(rbch_softc[unit].sc_devstate & ST_WRWAITEMPTY)
773 NDBGL4(L4_RBCHDBG, "unit %d, wakeup", unit);
774 rbch_softc[unit].sc_devstate &= ~ST_WRWAITEMPTY;
775 wakeup((caddr_t) &isdn_linktab[unit]->tx_queue);
779 NDBGL4(L4_RBCHDBG, "unit %d, NO wakeup", unit);
781 selwakeup(&rbch_softc[unit].selp);
784 /*---------------------------------------------------------------------------*
785 * this routine is called from the HSCX interrupt handler
786 * each time a packet is received or transmitted
787 *---------------------------------------------------------------------------*/
789 rbch_activity(int unit, int rxtx)
791 if (rbch_softc[unit].sc_cd)
792 rbch_softc[unit].sc_cd->last_active_time = SECOND;
793 selwakeup(&rbch_softc[unit].selp);
796 /*---------------------------------------------------------------------------*
797 * clear an hdlc rx queue for a rbch unit
798 *---------------------------------------------------------------------------*/
805 IF_DRAIN(&rbch_softc[unit].sc_hdlcq);
809 /*---------------------------------------------------------------------------*
810 * return this drivers linktab address
811 *---------------------------------------------------------------------------*/
813 rbch_ret_linktab(int unit)
815 rbch_init_linktab(unit);
816 return(&rbch_drvr_linktab[unit]);
819 /*---------------------------------------------------------------------------*
820 * setup the isdn_linktab for this driver
821 *---------------------------------------------------------------------------*/
823 rbch_set_linktab(int unit, isdn_link_t *ilt)
825 isdn_linktab[unit] = ilt;
828 /*---------------------------------------------------------------------------*
829 * initialize this drivers linktab
830 *---------------------------------------------------------------------------*/
832 rbch_init_linktab(int unit)
834 rbch_drvr_linktab[unit].unit = unit;
835 rbch_drvr_linktab[unit].bch_rx_data_ready = rbch_rx_data_rdy;
836 rbch_drvr_linktab[unit].bch_tx_queue_empty = rbch_tx_queue_empty;
837 rbch_drvr_linktab[unit].bch_activity = rbch_activity;
838 rbch_drvr_linktab[unit].line_connected = rbch_connect;
839 rbch_drvr_linktab[unit].line_disconnected = rbch_disconnect;
840 rbch_drvr_linktab[unit].dial_response = rbch_dialresponse;
841 rbch_drvr_linktab[unit].updown_ind = rbch_updown;
844 /*===========================================================================*/
846 #endif /* NI4BRBCH > 0 */