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.19 2006/06/14 21:07:09 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"
63 #include <sys/ioccom.h>
65 #include <sys/filio.h>
67 static drvr_link_t rbch_drvr_linktab[NI4BRBCH];
68 static isdn_link_t *isdn_linktab[NI4BRBCH];
70 #define I4BRBCHACCT 1 /* enable accounting messages */
71 #define I4BRBCHACCTINTVL 2 /* accounting msg interval in secs */
73 static struct rbch_softc {
75 int sc_unit; /* unit number */
77 int sc_devstate; /* state of driver */
79 #define ST_CONNECTED 0x01
80 #define ST_ISOPEN 0x02
81 #define ST_RDWAITDATA 0x04
82 #define ST_WRWAITEMPTY 0x08
83 #define ST_NOBLOCK 0x10
85 int sc_bprot; /* B-ch protocol used */
87 call_desc_t *sc_cd; /* Call Descriptor */
91 struct ifqueue sc_hdlcq; /* hdlc read queue */
92 #define I4BRBCHMAXQLEN 10
94 struct selinfo selp; /* select / poll */
97 struct callout sc_timeout;
99 int sc_iinb; /* isdn driver # of inbytes */
100 int sc_ioutb; /* isdn driver # of outbytes */
101 int sc_linb; /* last # of bytes rx'd */
102 int sc_loutb; /* last # of bytes tx'd */
103 int sc_fn; /* flag, first null acct */
105 } rbch_softc[NI4BRBCH];
107 static void rbch_rx_data_rdy(int unit);
108 static void rbch_tx_queue_empty(int unit);
109 static void rbch_connect(int unit, void *cdp);
110 static void rbch_disconnect(int unit, void *cdp);
111 static void rbch_init_linktab(int unit);
112 static void rbch_clrq(int unit);
114 #define PDEVSTATIC static
115 #define IOCTL_CMD_T u_long
117 PDEVSTATIC d_open_t i4brbchopen;
118 PDEVSTATIC d_close_t i4brbchclose;
119 PDEVSTATIC d_read_t i4brbchread;
120 PDEVSTATIC d_read_t i4brbchwrite;
121 PDEVSTATIC d_ioctl_t i4brbchioctl;
123 PDEVSTATIC d_poll_t i4brbchpoll;
124 #define POLLFIELD i4brbchpoll
126 #define CDEV_MAJOR 57
128 static struct cdevsw i4brbch_cdevsw = {
129 /* name */ "i4brbch",
130 /* maj */ CDEV_MAJOR,
135 /* open */ i4brbchopen,
136 /* close */ i4brbchclose,
137 /* read */ i4brbchread,
138 /* write */ i4brbchwrite,
139 /* ioctl */ i4brbchioctl,
140 /* poll */ POLLFIELD,
142 /* strategy */ nostrategy,
147 static void i4brbchattach(void *);
148 PSEUDO_SET(i4brbchattach, i4b_rbch);
150 /*===========================================================================*
151 * DEVICE DRIVER ROUTINES
152 *===========================================================================*/
154 /*---------------------------------------------------------------------------*
155 * initialization at kernel load time
156 *---------------------------------------------------------------------------*/
158 i4brbchinit(void *unused)
160 cdevsw_add(&i4brbch_cdevsw, 0, 0);
163 SYSINIT(i4brbchdev, SI_SUB_DRIVERS,
164 SI_ORDER_MIDDLE+CDEV_MAJOR, &i4brbchinit, NULL);
166 /*---------------------------------------------------------------------------*
167 * interface attach routine
168 *---------------------------------------------------------------------------*/
170 i4brbchattach(void *dummy)
174 #ifndef HACK_NO_PSEUDO_ATTACH_MSG
175 printf("i4brbch: %d raw B channel access device(s) attached\n", NI4BRBCH);
178 for(i=0; i < NI4BRBCH; i++)
180 make_dev(&i4brbch_cdevsw, i,
181 UID_ROOT, GID_WHEEL, 0600, "i4brbch%d", i);
184 callout_init(&rbch_softc[i].sc_timeout);
185 rbch_softc[i].sc_fn = 1;
187 rbch_softc[i].sc_unit = i;
188 rbch_softc[i].sc_devstate = ST_IDLE;
189 rbch_softc[i].sc_hdlcq.ifq_maxlen = I4BRBCHMAXQLEN;
190 rbch_softc[i].it_in.c_ispeed = rbch_softc[i].it_in.c_ospeed = 64000;
191 termioschars(&rbch_softc[i].it_in);
192 rbch_init_linktab(i);
196 /*---------------------------------------------------------------------------*
198 *---------------------------------------------------------------------------*/
200 i4brbchopen(dev_t dev, int flag, int fmt, struct thread *td)
202 int unit = minor(dev);
207 if(rbch_softc[unit].sc_devstate & ST_ISOPEN)
214 rbch_softc[unit].sc_devstate |= ST_ISOPEN;
216 NDBGL4(L4_RBCHDBG, "unit %d, open", unit);
221 /*---------------------------------------------------------------------------*
223 *---------------------------------------------------------------------------*/
225 i4brbchclose(dev_t dev, int flag, int fmt, struct thread *td)
227 int unit = minor(dev);
228 struct rbch_softc *sc = &rbch_softc[unit];
230 if(sc->sc_devstate & ST_CONNECTED)
231 i4b_l4_drvrdisc(BDRV_RBCH, unit);
233 sc->sc_devstate &= ~ST_ISOPEN;
237 NDBGL4(L4_RBCHDBG, "unit %d, closed", unit);
242 /*---------------------------------------------------------------------------*
243 * read from rbch device
244 *---------------------------------------------------------------------------*/
246 i4brbchread(dev_t dev, struct uio *uio, int ioflag)
250 int unit = minor(dev);
252 struct rbch_softc *sc = &rbch_softc[unit];
256 NDBGL4(L4_RBCHDBG, "unit %d, enter read", unit);
259 if(!(sc->sc_devstate & ST_ISOPEN))
262 NDBGL4(L4_RBCHDBG, "unit %d, read while not open", unit);
266 if((sc->sc_devstate & ST_NOBLOCK) || (ioflag & IO_NDELAY))
268 if(!(sc->sc_devstate & ST_CONNECTED)) {
273 if(sc->sc_bprot == BPROT_RHDLC)
276 iqp = isdn_linktab[unit]->rx_queue;
278 if(IF_QEMPTY(iqp) && (sc->sc_devstate & ST_ISOPEN)) {
285 while(!(sc->sc_devstate & ST_CONNECTED))
287 NDBGL4(L4_RBCHDBG, "unit %d, wait read init", unit);
289 if((error = tsleep((caddr_t) &rbch_softc[unit],
290 PCATCH, "rrrbch", 0 )) != 0)
293 NDBGL4(L4_RBCHDBG, "unit %d, error %d tsleep", unit, error);
298 if(sc->sc_bprot == BPROT_RHDLC)
301 iqp = isdn_linktab[unit]->rx_queue;
303 while(IF_QEMPTY(iqp) && (sc->sc_devstate & ST_ISOPEN))
305 sc->sc_devstate |= ST_RDWAITDATA;
307 NDBGL4(L4_RBCHDBG, "unit %d, wait read data", unit);
309 if((error = tsleep((caddr_t) &isdn_linktab[unit]->rx_queue,
310 PCATCH, "rrbch", 0 )) != 0)
313 NDBGL4(L4_RBCHDBG, "unit %d, error %d tsleep read", unit, error);
314 sc->sc_devstate &= ~ST_RDWAITDATA;
322 NDBGL4(L4_RBCHDBG, "unit %d, read %d bytes", unit, m->m_len);
326 error = uiomove(m->m_data, m->m_len, uio);
330 NDBGL4(L4_RBCHDBG, "unit %d, error %d uiomove", unit, error);
342 /*---------------------------------------------------------------------------*
343 * write to rbch device
344 *---------------------------------------------------------------------------*/
346 i4brbchwrite(dev_t dev, struct uio * uio, int ioflag)
350 int unit = minor(dev);
351 struct rbch_softc *sc = &rbch_softc[unit];
355 NDBGL4(L4_RBCHDBG, "unit %d, write", unit);
358 if(!(sc->sc_devstate & ST_ISOPEN))
360 NDBGL4(L4_RBCHDBG, "unit %d, write while not open", unit);
365 if((sc->sc_devstate & ST_NOBLOCK) || (ioflag & IO_NDELAY))
367 if(!(sc->sc_devstate & ST_CONNECTED)) {
371 if(IF_QFULL(isdn_linktab[unit]->tx_queue) && (sc->sc_devstate & ST_ISOPEN)) {
378 while(!(sc->sc_devstate & ST_CONNECTED))
380 NDBGL4(L4_RBCHDBG, "unit %d, write wait init", unit);
382 error = tsleep((caddr_t) &rbch_softc[unit],
383 PCATCH, "wrrbch", 0 );
384 if(error == ERESTART) {
388 else if(error == EINTR)
391 NDBGL4(L4_RBCHDBG, "unit %d, EINTR during wait init", unit);
397 NDBGL4(L4_RBCHDBG, "unit %d, error %d tsleep init", unit, error);
400 tsleep((caddr_t) &rbch_softc[unit], PCATCH, "xrbch", (hz*1));
403 while(IF_QFULL(isdn_linktab[unit]->tx_queue) && (sc->sc_devstate & ST_ISOPEN))
405 sc->sc_devstate |= ST_WRWAITEMPTY;
407 NDBGL4(L4_RBCHDBG, "unit %d, write queue full", unit);
409 if ((error = tsleep((caddr_t) &isdn_linktab[unit]->tx_queue,
410 PCATCH, "wrbch", 0)) != 0) {
411 sc->sc_devstate &= ~ST_WRWAITEMPTY;
412 if(error == ERESTART)
417 else if(error == EINTR)
420 NDBGL4(L4_RBCHDBG, "unit %d, EINTR during wait write", unit);
426 NDBGL4(L4_RBCHDBG, "unit %d, error %d tsleep write", unit, error);
433 if(!(sc->sc_devstate & ST_ISOPEN))
435 NDBGL4(L4_RBCHDBG, "unit %d, not open anymore", unit);
440 if((m = i4b_Bgetmbuf(BCH_MAX_DATALEN)) != NULL)
442 m->m_len = min(BCH_MAX_DATALEN, uio->uio_resid);
444 NDBGL4(L4_RBCHDBG, "unit %d, write %d bytes", unit, m->m_len);
446 error = uiomove(m->m_data, m->m_len, uio);
448 if(IF_QFULL(isdn_linktab[unit]->tx_queue))
451 IF_ENQUEUE(isdn_linktab[unit]->tx_queue, m);
452 (*isdn_linktab[unit]->bch_tx_start)(isdn_linktab[unit]->unit, isdn_linktab[unit]->channel);
460 /*---------------------------------------------------------------------------*
461 * rbch device ioctl handlibg
462 *---------------------------------------------------------------------------*/
464 i4brbchioctl(dev_t dev, IOCTL_CMD_T cmd, caddr_t data, int flag, struct thread *td)
467 int unit = minor(dev);
468 struct rbch_softc *sc = &rbch_softc[unit];
472 case FIOASYNC: /* Set async mode */
475 NDBGL4(L4_RBCHDBG, "unit %d, setting async mode", unit);
479 NDBGL4(L4_RBCHDBG, "unit %d, clearing async mode", unit);
483 case TIOCCDTR: /* Clear DTR */
484 if(sc->sc_devstate & ST_CONNECTED)
486 NDBGL4(L4_RBCHDBG, "unit %d, disconnecting for DTR down", unit);
487 i4b_l4_drvrdisc(BDRV_RBCH, unit);
491 case I4B_RBCH_DIALOUT:
495 for (l = 0; l < TELNO_MAX && ((char *)data)[l]; l++)
499 NDBGL4(L4_RBCHDBG, "unit %d, attempting dialout to %s", unit, (char *)data);
500 i4b_l4_dialoutnumber(BDRV_RBCH, unit, l, (char *)data);
503 /* fall through to SDTR */
506 case TIOCSDTR: /* Set DTR */
507 NDBGL4(L4_RBCHDBG, "unit %d, attempting dialout (DTR)", unit);
508 i4b_l4_dialout(BDRV_RBCH, unit);
511 case TIOCSETA: /* Set termios struct */
514 case TIOCGETA: /* Get termios struct */
515 *(struct termios *)data = sc->it_in;
519 *(int *)data = TIOCM_LE|TIOCM_DTR|TIOCM_RTS|TIOCM_CTS|TIOCM_DSR;
520 if (sc->sc_devstate & ST_CONNECTED)
521 *(int *)data |= TIOCM_CD;
524 case I4B_RBCH_VR_REQ:
528 mvr = (msg_vr_req_t *)data;
530 mvr->version = VERSION;
536 default: /* Unknown stuff */
537 NDBGL4(L4_RBCHDBG, "unit %d, ioctl, unknown cmd %lx", unit, (u_long)cmd);
544 /*---------------------------------------------------------------------------*
546 *---------------------------------------------------------------------------*/
548 i4brbchpoll(dev_t dev, int events, struct thread *td)
550 int revents = 0; /* Events we found */
551 int unit = minor(dev);
552 struct rbch_softc *sc = &rbch_softc[unit];
554 /* We can't check for anything but IN or OUT */
557 if(!(sc->sc_devstate & ST_ISOPEN))
564 * Writes are OK if we are connected and the
565 * transmit queue can take them
568 if((events & (POLLOUT|POLLWRNORM)) &&
569 (sc->sc_devstate & ST_CONNECTED) &&
570 !IF_QFULL(isdn_linktab[unit]->tx_queue))
572 revents |= (events & (POLLOUT|POLLWRNORM));
575 /* ... while reads are OK if we have any data */
577 if((events & (POLLIN|POLLRDNORM)) &&
578 (sc->sc_devstate & ST_CONNECTED))
582 if(sc->sc_bprot == BPROT_RHDLC)
585 iqp = isdn_linktab[unit]->rx_queue;
588 revents |= (events & (POLLIN|POLLRDNORM));
592 selrecord(td, &sc->selp);
599 /*---------------------------------------------------------------------------*
601 *---------------------------------------------------------------------------*/
603 rbch_timeout(struct rbch_softc *sc)
605 bchan_statistics_t bs;
606 int unit = sc->sc_unit;
608 /* get # of bytes in and out from the HSCX driver */
610 (*isdn_linktab[unit]->bch_stat)
611 (isdn_linktab[unit]->unit, isdn_linktab[unit]->channel, &bs);
613 sc->sc_ioutb += bs.outbytes;
614 sc->sc_iinb += bs.inbytes;
616 if((sc->sc_iinb != sc->sc_linb) || (sc->sc_ioutb != sc->sc_loutb) || sc->sc_fn)
618 int ri = (sc->sc_iinb - sc->sc_linb)/I4BRBCHACCTINTVL;
619 int ro = (sc->sc_ioutb - sc->sc_loutb)/I4BRBCHACCTINTVL;
621 if((sc->sc_iinb == sc->sc_linb) && (sc->sc_ioutb == sc->sc_loutb))
626 sc->sc_linb = sc->sc_iinb;
627 sc->sc_loutb = sc->sc_ioutb;
629 i4b_l4_accounting(BDRV_RBCH, unit, ACCT_DURING,
630 sc->sc_ioutb, sc->sc_iinb, ro, ri, sc->sc_ioutb, sc->sc_iinb);
632 callout_reset(&sc->sc_timeout, I4BRBCHACCTINTVL * hz,
633 (void *)rbch_timeout, sc);
635 #endif /* I4BRBCHACCT */
637 /*===========================================================================*
638 * ISDN INTERFACE ROUTINES
639 *===========================================================================*/
641 /*---------------------------------------------------------------------------*
642 * this routine is called from L4 handler at connect time
643 *---------------------------------------------------------------------------*/
645 rbch_connect(int unit, void *cdp)
647 call_desc_t *cd = (call_desc_t *)cdp;
648 struct rbch_softc *sc = &rbch_softc[unit];
650 sc->sc_bprot = cd->bprot;
653 if(sc->sc_bprot == BPROT_RHDLC)
660 callout_reset(&sc->sc_timeout, I4BRBCHACCTINTVL * hz,
661 (void *)rbch_timeout, sc);
664 if(!(sc->sc_devstate & ST_CONNECTED))
666 NDBGL4(L4_RBCHDBG, "unit %d, wakeup", unit);
667 sc->sc_devstate |= ST_CONNECTED;
673 /*---------------------------------------------------------------------------*
674 * this routine is called from L4 handler at disconnect time
675 *---------------------------------------------------------------------------*/
677 rbch_disconnect(int unit, void *cdp)
679 call_desc_t *cd = (call_desc_t *)cdp;
680 struct rbch_softc *sc = &rbch_softc[unit];
686 NDBGL4(L4_RBCHDBG, "rbch%d: channel %d not active",
687 cd->driver_unit, cd->channelid);
693 NDBGL4(L4_RBCHDBG, "unit %d, disconnect", unit);
695 sc->sc_devstate &= ~ST_CONNECTED;
700 i4b_l4_accounting(BDRV_RBCH, unit, ACCT_FINAL,
701 sc->sc_ioutb, sc->sc_iinb, 0, 0, sc->sc_ioutb, sc->sc_iinb);
702 callout_stop(&sc->sc_timeout);
707 /*---------------------------------------------------------------------------*
708 * feedback from daemon in case of dial problems
709 *---------------------------------------------------------------------------*/
711 rbch_dialresponse(int unit, int status, cause_t cause)
715 /*---------------------------------------------------------------------------*
717 *---------------------------------------------------------------------------*/
719 rbch_updown(int unit, int updown)
723 /*---------------------------------------------------------------------------*
724 * this routine is called from the HSCX interrupt handler
725 * when a new frame (mbuf) has been received and is to be put on
727 *---------------------------------------------------------------------------*/
729 rbch_rx_data_rdy(int unit)
731 if(rbch_softc[unit].sc_bprot == BPROT_RHDLC)
735 if((m = *isdn_linktab[unit]->rx_mbuf) == NULL)
738 m->m_pkthdr.len = m->m_len;
740 if(IF_QFULL(&(rbch_softc[unit].sc_hdlcq)))
742 NDBGL4(L4_RBCHDBG, "unit %d: hdlc rx queue full!", unit);
747 IF_ENQUEUE(&(rbch_softc[unit].sc_hdlcq), m);
751 if(rbch_softc[unit].sc_devstate & ST_RDWAITDATA)
753 NDBGL4(L4_RBCHDBG, "unit %d, wakeup", unit);
754 rbch_softc[unit].sc_devstate &= ~ST_RDWAITDATA;
755 wakeup((caddr_t) &isdn_linktab[unit]->rx_queue);
759 NDBGL4(L4_RBCHDBG, "unit %d, NO wakeup", unit);
761 selwakeup(&rbch_softc[unit].selp);
764 /*---------------------------------------------------------------------------*
765 * this routine is called from the HSCX interrupt handler
766 * when the last frame has been sent out and there is no
767 * further frame (mbuf) in the tx queue.
768 *---------------------------------------------------------------------------*/
770 rbch_tx_queue_empty(int unit)
772 if(rbch_softc[unit].sc_devstate & ST_WRWAITEMPTY)
774 NDBGL4(L4_RBCHDBG, "unit %d, wakeup", unit);
775 rbch_softc[unit].sc_devstate &= ~ST_WRWAITEMPTY;
776 wakeup((caddr_t) &isdn_linktab[unit]->tx_queue);
780 NDBGL4(L4_RBCHDBG, "unit %d, NO wakeup", unit);
782 selwakeup(&rbch_softc[unit].selp);
785 /*---------------------------------------------------------------------------*
786 * this routine is called from the HSCX interrupt handler
787 * each time a packet is received or transmitted
788 *---------------------------------------------------------------------------*/
790 rbch_activity(int unit, int rxtx)
792 if (rbch_softc[unit].sc_cd)
793 rbch_softc[unit].sc_cd->last_active_time = SECOND;
794 selwakeup(&rbch_softc[unit].selp);
797 /*---------------------------------------------------------------------------*
798 * clear an hdlc rx queue for a rbch unit
799 *---------------------------------------------------------------------------*/
806 IF_DRAIN(&rbch_softc[unit].sc_hdlcq);
810 /*---------------------------------------------------------------------------*
811 * return this drivers linktab address
812 *---------------------------------------------------------------------------*/
814 rbch_ret_linktab(int unit)
816 rbch_init_linktab(unit);
817 return(&rbch_drvr_linktab[unit]);
820 /*---------------------------------------------------------------------------*
821 * setup the isdn_linktab for this driver
822 *---------------------------------------------------------------------------*/
824 rbch_set_linktab(int unit, isdn_link_t *ilt)
826 isdn_linktab[unit] = ilt;
829 /*---------------------------------------------------------------------------*
830 * initialize this drivers linktab
831 *---------------------------------------------------------------------------*/
833 rbch_init_linktab(int unit)
835 rbch_drvr_linktab[unit].unit = unit;
836 rbch_drvr_linktab[unit].bch_rx_data_ready = rbch_rx_data_rdy;
837 rbch_drvr_linktab[unit].bch_tx_queue_empty = rbch_tx_queue_empty;
838 rbch_drvr_linktab[unit].bch_activity = rbch_activity;
839 rbch_drvr_linktab[unit].line_connected = rbch_connect;
840 rbch_drvr_linktab[unit].line_disconnected = rbch_disconnect;
841 rbch_drvr_linktab[unit].dial_response = rbch_dialresponse;
842 rbch_drvr_linktab[unit].updown_ind = rbch_updown;
845 /*===========================================================================*/
847 #endif /* NI4BRBCH > 0 */