kernel - Fix kqfilter error return codes
[dragonfly.git] / sys / net / i4b / driver / i4b_rbch.c
1 /*
2  * Copyright (c) 1997, 2001 Hellmuth Michaelis. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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
23  * SUCH DAMAGE.
24  *
25  *---------------------------------------------------------------------------
26  *
27  *      i4b_rbch.c - device driver for raw B channel data
28  *      ---------------------------------------------------
29  *
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 $
32  *
33  *      last edit-date: [Sat Aug 11 18:06:57 2001]
34  *
35  *---------------------------------------------------------------------------*/
36
37 #include "use_i4brbch.h"
38
39 #if NI4BRBCH > 0
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43
44 #include <sys/conf.h>
45 #include <sys/uio.h>
46 #include <sys/kernel.h>
47 #include <sys/mbuf.h>
48 #include <sys/socket.h>
49 #include <net/if.h>
50 #include <sys/tty.h>
51 #include <sys/thread2.h>
52 #include <sys/vnode.h>
53
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>
57
58 #include "../include/i4b_global.h"
59 #include "../include/i4b_mbuf.h"
60 #include "../include/i4b_l3l4.h"
61 #include "../layer4/i4b_l4.h"
62
63 #include <sys/poll.h>
64 #include <sys/event.h>
65 #include <sys/filio.h>
66
67 static drvr_link_t rbch_drvr_linktab[NI4BRBCH];
68 static isdn_link_t *isdn_linktab[NI4BRBCH];
69
70 #define I4BRBCHACCT             1       /* enable accounting messages */
71 #define I4BRBCHACCTINTVL        2       /* accounting msg interval in secs */
72
73 static struct rbch_softc {
74
75         int sc_unit;                    /* unit number          */
76
77         int sc_devstate;                /* state of driver      */
78 #define ST_IDLE         0x00
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
84
85         int sc_bprot;                   /* B-ch protocol used   */
86
87         call_desc_t *sc_cd;             /* Call Descriptor */
88
89         struct termios it_in;
90
91         struct ifqueue sc_hdlcq;        /* hdlc read queue      */
92 #define I4BRBCHMAXQLEN  10
93
94         struct selinfo selp;            /* select / poll        */
95
96 #if I4BRBCHACCT
97         struct callout  sc_timeout;
98
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        */
104 #endif  
105 } rbch_softc[NI4BRBCH];
106
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);
113
114 #define PDEVSTATIC      static
115 #define IOCTL_CMD_T     u_long
116
117 PDEVSTATIC d_open_t i4brbchopen;
118 PDEVSTATIC d_close_t i4brbchclose;
119 PDEVSTATIC d_read_t i4brbchread;
120 PDEVSTATIC d_write_t i4brbchwrite;
121 PDEVSTATIC d_ioctl_t i4brbchioctl;
122 PDEVSTATIC d_kqfilter_t i4brbchkqfilter;
123
124 PDEVSTATIC void i4brbchkfilt_detach(struct knote *);
125 PDEVSTATIC int i4brbchkfilt_read(struct knote *, long);
126 PDEVSTATIC int i4brbchkfilt_write(struct knote *, long);
127
128 PDEVSTATIC d_poll_t i4brbchpoll;
129 #define POLLFIELD       i4brbchpoll
130
131 #define CDEV_MAJOR 57
132
133 static struct dev_ops i4brbch_ops = {
134         { "i4brbch", CDEV_MAJOR, D_KQFILTER },
135         .d_open =       i4brbchopen,
136         .d_close =      i4brbchclose,
137         .d_read =       i4brbchread,
138         .d_write =      i4brbchwrite,
139         .d_ioctl =      i4brbchioctl,
140         .d_poll =       POLLFIELD,
141         .d_kqfilter =   i4brbchkqfilter
142 };
143
144 static void i4brbchattach(void *);
145 PSEUDO_SET(i4brbchattach, i4b_rbch);
146
147 /*===========================================================================*
148  *                      DEVICE DRIVER ROUTINES
149  *===========================================================================*/
150
151 /*---------------------------------------------------------------------------*
152  *      interface attach routine
153  *---------------------------------------------------------------------------*/
154 PDEVSTATIC void
155 i4brbchattach(void *dummy)
156 {
157         int i;
158
159 #ifndef HACK_NO_PSEUDO_ATTACH_MSG
160         kprintf("i4brbch: %d raw B channel access device(s) attached\n", NI4BRBCH);
161 #endif
162         
163         for(i=0; i < NI4BRBCH; i++)
164         {
165                 make_dev(&i4brbch_ops, i,
166                         UID_ROOT, GID_WHEEL, 0600, "i4brbch%d", i);
167
168 #if I4BRBCHACCT
169                 callout_init(&rbch_softc[i].sc_timeout);
170                 rbch_softc[i].sc_fn = 1;
171 #endif
172                 rbch_softc[i].sc_unit = i;
173                 rbch_softc[i].sc_devstate = ST_IDLE;
174                 rbch_softc[i].sc_hdlcq.ifq_maxlen = I4BRBCHMAXQLEN;
175                 rbch_softc[i].it_in.c_ispeed = rbch_softc[i].it_in.c_ospeed = 64000;
176                 termioschars(&rbch_softc[i].it_in);
177                 rbch_init_linktab(i);
178         }
179 }
180
181 /*---------------------------------------------------------------------------*
182  *      open rbch device
183  *---------------------------------------------------------------------------*/
184 PDEVSTATIC int
185 i4brbchopen(struct dev_open_args *ap)
186 {
187         cdev_t dev = ap->a_head.a_dev;
188         int unit = minor(dev);
189         
190         if(unit >= NI4BRBCH)
191                 return(ENXIO);
192
193         if(rbch_softc[unit].sc_devstate & ST_ISOPEN)
194                 return(EBUSY);
195
196 #if 0
197         rbch_clrq(unit);
198 #endif
199         
200         rbch_softc[unit].sc_devstate |= ST_ISOPEN;              
201
202         NDBGL4(L4_RBCHDBG, "unit %d, open", unit);      
203
204         return(0);
205 }
206
207 /*---------------------------------------------------------------------------*
208  *      close rbch device
209  *---------------------------------------------------------------------------*/
210 PDEVSTATIC int
211 i4brbchclose(struct dev_close_args *ap)
212 {
213         cdev_t dev = ap->a_head.a_dev;
214         int unit = minor(dev);
215         struct rbch_softc *sc = &rbch_softc[unit];
216         
217         if(sc->sc_devstate & ST_CONNECTED)
218                 i4b_l4_drvrdisc(BDRV_RBCH, unit);
219
220         sc->sc_devstate &= ~ST_ISOPEN;          
221
222         rbch_clrq(unit);
223         
224         NDBGL4(L4_RBCHDBG, "unit %d, closed", unit);
225         
226         return(0);
227 }
228
229 /*---------------------------------------------------------------------------*
230  *      read from rbch device
231  *---------------------------------------------------------------------------*/
232 PDEVSTATIC int
233 i4brbchread(struct dev_read_args *ap)
234 {
235         cdev_t dev = ap->a_head.a_dev;
236         struct uio *uio = ap->a_uio;
237         struct mbuf *m;
238         int error = 0;
239         int unit = minor(dev);
240         struct ifqueue *iqp;
241         struct rbch_softc *sc = &rbch_softc[unit];
242
243         CRIT_VAR;
244         
245         NDBGL4(L4_RBCHDBG, "unit %d, enter read", unit);
246         
247         CRIT_BEG;
248         if(!(sc->sc_devstate & ST_ISOPEN))
249         {
250                 CRIT_END;
251                 NDBGL4(L4_RBCHDBG, "unit %d, read while not open", unit);
252                 return(EIO);
253         }
254
255         if((sc->sc_devstate & ST_NOBLOCK) || (ap->a_ioflag & IO_NDELAY))
256         {
257                 if(!(sc->sc_devstate & ST_CONNECTED)) {
258                         CRIT_END;
259                         return(EWOULDBLOCK);
260                 }
261
262                 if(sc->sc_bprot == BPROT_RHDLC)
263                         iqp = &sc->sc_hdlcq;
264                 else
265                         iqp = isdn_linktab[unit]->rx_queue;     
266
267                 if(IF_QEMPTY(iqp) && (sc->sc_devstate & ST_ISOPEN)) {
268                         CRIT_END;
269                         return(EWOULDBLOCK);
270         }
271         }
272         else
273         {
274                 while(!(sc->sc_devstate & ST_CONNECTED))
275                 {
276                         NDBGL4(L4_RBCHDBG, "unit %d, wait read init", unit);
277                 
278                         if((error = tsleep((caddr_t) &rbch_softc[unit],
279                                                PCATCH, "rrrbch", 0 )) != 0)
280                         {
281                                 CRIT_END;
282                                 NDBGL4(L4_RBCHDBG, "unit %d, error %d tsleep", unit, error);
283                                 return(error);
284                         }
285                 }
286
287                 if(sc->sc_bprot == BPROT_RHDLC)
288                         iqp = &sc->sc_hdlcq;
289                 else
290                         iqp = isdn_linktab[unit]->rx_queue;     
291
292                 while(IF_QEMPTY(iqp) && (sc->sc_devstate & ST_ISOPEN))
293                 {
294                         sc->sc_devstate |= ST_RDWAITDATA;
295                 
296                         NDBGL4(L4_RBCHDBG, "unit %d, wait read data", unit);
297                 
298                         if((error = tsleep((caddr_t) &isdn_linktab[unit]->rx_queue,
299                                            PCATCH, "rrbch", 0 )) != 0)
300                         {
301                                 CRIT_END;
302                                 NDBGL4(L4_RBCHDBG, "unit %d, error %d tsleep read", unit, error);
303                                 sc->sc_devstate &= ~ST_RDWAITDATA;
304                                 return(error);
305                         }
306                 }
307         }
308
309         IF_DEQUEUE(iqp, m);
310
311         NDBGL4(L4_RBCHDBG, "unit %d, read %d bytes", unit, m->m_len);
312         
313         if(m && m->m_len)
314         {
315                 error = uiomove(m->m_data, m->m_len, uio);
316         }
317         else
318         {
319                 NDBGL4(L4_RBCHDBG, "unit %d, error %d uiomove", unit, error);
320                 error = EIO;
321         }
322                 
323         if(m)
324                 i4b_Bfreembuf(m);
325
326         CRIT_END;
327
328         return(error);
329 }
330
331 /*---------------------------------------------------------------------------*
332  *      write to rbch device
333  *---------------------------------------------------------------------------*/
334 PDEVSTATIC int
335 i4brbchwrite(struct dev_write_args *ap)
336 {
337         cdev_t dev = ap->a_head.a_dev;
338         struct uio *uio = ap->a_uio;
339         struct mbuf *m;
340         int error = 0;
341         int unit = minor(dev);
342         struct rbch_softc *sc = &rbch_softc[unit];
343
344         CRIT_VAR;
345         
346         NDBGL4(L4_RBCHDBG, "unit %d, write", unit);     
347
348         CRIT_BEG;
349         if(!(sc->sc_devstate & ST_ISOPEN))
350         {
351                 NDBGL4(L4_RBCHDBG, "unit %d, write while not open", unit);
352                 CRIT_END;
353                 return(EIO);
354         }
355
356         if((sc->sc_devstate & ST_NOBLOCK) || (ap->a_ioflag & IO_NDELAY))
357         {
358                 if(!(sc->sc_devstate & ST_CONNECTED)) {
359                         CRIT_END;
360                         return(EWOULDBLOCK);
361                 }
362                 if(IF_QFULL(isdn_linktab[unit]->tx_queue) && (sc->sc_devstate & ST_ISOPEN)) {
363                         CRIT_END;
364                         return(EWOULDBLOCK);
365         }
366         }
367         else
368         {
369                 while(!(sc->sc_devstate & ST_CONNECTED))
370                 {
371                         NDBGL4(L4_RBCHDBG, "unit %d, write wait init", unit);
372                 
373                         error = tsleep((caddr_t) &rbch_softc[unit],
374                                                    PCATCH, "wrrbch", 0 );
375                         if(error == ERESTART) {
376                                 CRIT_END;
377                                 return (ERESTART);
378                         }
379                         else if(error == EINTR)
380                         {
381                                 CRIT_END;
382                                 NDBGL4(L4_RBCHDBG, "unit %d, EINTR during wait init", unit);
383                                 return(EINTR);
384                         }
385                         else if(error)
386                         {
387                                 CRIT_END;
388                                 NDBGL4(L4_RBCHDBG, "unit %d, error %d tsleep init", unit, error);
389                                 return(error);
390                         }
391                         tsleep((caddr_t) &rbch_softc[unit], PCATCH, "xrbch", (hz*1));
392                 }
393
394                 while(IF_QFULL(isdn_linktab[unit]->tx_queue) && (sc->sc_devstate & ST_ISOPEN))
395                 {
396                         sc->sc_devstate |= ST_WRWAITEMPTY;
397
398                         NDBGL4(L4_RBCHDBG, "unit %d, write queue full", unit);
399                 
400                         if ((error = tsleep((caddr_t) &isdn_linktab[unit]->tx_queue,
401                                             PCATCH, "wrbch", 0)) != 0) {
402                                 sc->sc_devstate &= ~ST_WRWAITEMPTY;
403                                 if(error == ERESTART)
404                                 {
405                                         CRIT_END;
406                                         return(ERESTART);
407                                 }
408                                 else if(error == EINTR)
409                                 {
410                                         CRIT_END;
411                                         NDBGL4(L4_RBCHDBG, "unit %d, EINTR during wait write", unit);
412                                         return(error);
413                                 }
414                                 else if(error)
415                                 {
416                                         CRIT_END;
417                                         NDBGL4(L4_RBCHDBG, "unit %d, error %d tsleep write", unit, error);
418                                         return(error);
419                                 }
420                         }
421                 }
422         }
423
424         if(!(sc->sc_devstate & ST_ISOPEN))
425         {
426                 NDBGL4(L4_RBCHDBG, "unit %d, not open anymore", unit);
427                 CRIT_END;
428                 return(EIO);
429         }
430
431         if((m = i4b_Bgetmbuf(BCH_MAX_DATALEN)) != NULL)
432         {
433                 m->m_len = (int)szmin(BCH_MAX_DATALEN, uio->uio_resid);
434
435                 NDBGL4(L4_RBCHDBG, "unit %d, write %d bytes", unit, m->m_len);
436                 
437                 error = uiomove(m->m_data, (size_t)m->m_len, uio);
438
439                 if(IF_QFULL(isdn_linktab[unit]->tx_queue))
440                         m_freem(m);
441                 else
442                         IF_ENQUEUE(isdn_linktab[unit]->tx_queue, m);
443                 (*isdn_linktab[unit]->bch_tx_start)(isdn_linktab[unit]->unit, isdn_linktab[unit]->channel);
444         }
445
446         CRIT_END;
447         
448         return(error);
449 }
450
451 /*---------------------------------------------------------------------------*
452  *      rbch device ioctl handlibg
453  *---------------------------------------------------------------------------*/
454 PDEVSTATIC int
455 i4brbchioctl(struct dev_ioctl_args *ap)
456 {
457         cdev_t dev = ap->a_head.a_dev;
458         int error = 0;
459         int unit = minor(dev);
460         struct rbch_softc *sc = &rbch_softc[unit];
461         
462         switch(ap->a_cmd)
463         {
464                 case FIOASYNC:  /* Set async mode */
465                         if (*(int *)ap->a_data)
466                         {
467                                 NDBGL4(L4_RBCHDBG, "unit %d, setting async mode", unit);
468                         }
469                         else
470                         {
471                                 NDBGL4(L4_RBCHDBG, "unit %d, clearing async mode", unit);
472                         }
473                         break;
474
475                 case TIOCCDTR:  /* Clear DTR */
476                         if(sc->sc_devstate & ST_CONNECTED)
477                         {
478                                 NDBGL4(L4_RBCHDBG, "unit %d, disconnecting for DTR down", unit);
479                                 i4b_l4_drvrdisc(BDRV_RBCH, unit);
480                         }
481                         break;
482
483                 case I4B_RBCH_DIALOUT:
484                 {
485                         size_t l;
486
487                         for (l = 0; l < TELNO_MAX && ((char *)ap->a_data)[l]; l++)
488                                 ;
489                         if (l)
490                         {
491                                 NDBGL4(L4_RBCHDBG, "unit %d, attempting dialout to %s", unit, (char *)ap->a_data);
492                                 i4b_l4_dialoutnumber(BDRV_RBCH, unit, l, (char *)ap->a_data);
493                                 break;
494                         }
495                         /* fall through to SDTR */
496                 }
497
498                 case TIOCSDTR:  /* Set DTR */
499                         NDBGL4(L4_RBCHDBG, "unit %d, attempting dialout (DTR)", unit);
500                         i4b_l4_dialout(BDRV_RBCH, unit);
501                         break;
502
503                 case TIOCSETA:  /* Set termios struct */
504                         break;
505
506                 case TIOCGETA:  /* Get termios struct */
507                         *(struct termios *)ap->a_data = sc->it_in;
508                         break;
509
510                 case TIOCMGET:
511                         *(int *)ap->a_data = TIOCM_LE|TIOCM_DTR|TIOCM_RTS|TIOCM_CTS|TIOCM_DSR;
512                         if (sc->sc_devstate & ST_CONNECTED)
513                                 *(int *)ap->a_data |= TIOCM_CD;
514                         break;
515
516                 case I4B_RBCH_VR_REQ:
517                 {
518                         msg_vr_req_t *mvr;
519
520                         mvr = (msg_vr_req_t *)ap->a_data;
521
522                         mvr->version = VERSION;
523                         mvr->release = REL;
524                         mvr->step = STEP;                       
525                         break;
526                 }
527
528                 default:        /* Unknown stuff */
529                         NDBGL4(L4_RBCHDBG, "unit %d, ioctl, unknown cmd %lx", unit, ap->a_cmd);
530                         error = EINVAL;
531                         break;
532         }
533         return(error);
534 }
535
536 /*---------------------------------------------------------------------------*
537  *      device driver poll
538  *---------------------------------------------------------------------------*/
539 PDEVSTATIC int
540 i4brbchpoll(struct dev_poll_args *ap)
541 {
542         cdev_t dev = ap->a_head.a_dev;
543         int revents = 0;        /* Events we found */
544         int unit = minor(dev);
545         struct rbch_softc *sc = &rbch_softc[unit];
546         
547         /* We can't check for anything but IN or OUT */
548         crit_enter();
549
550         if(!(sc->sc_devstate & ST_ISOPEN))
551         {
552                 crit_exit();
553                 return(POLLNVAL);
554         }
555
556         /*
557          * Writes are OK if we are connected and the
558          * transmit queue can take them
559          */
560          
561         if((ap->a_events & (POLLOUT|POLLWRNORM)) &&
562            (sc->sc_devstate & ST_CONNECTED) &&
563            !IF_QFULL(isdn_linktab[unit]->tx_queue))
564         {
565                 revents |= (ap->a_events & (POLLOUT|POLLWRNORM));
566         }
567         
568         /* ... while reads are OK if we have any data */
569
570         if((ap->a_events & (POLLIN|POLLRDNORM)) &&
571            (sc->sc_devstate & ST_CONNECTED))
572         {
573                 struct ifqueue *iqp;
574
575                 if(sc->sc_bprot == BPROT_RHDLC)
576                         iqp = &sc->sc_hdlcq;
577                 else
578                         iqp = isdn_linktab[unit]->rx_queue;     
579
580                 if(!IF_QEMPTY(iqp))
581                         revents |= (ap->a_events & (POLLIN|POLLRDNORM));
582         }
583                 
584         if(revents == 0)
585                 selrecord(curthread, &sc->selp);
586
587         crit_exit();
588         ap->a_events = revents;
589         return (0);
590 }
591
592 static struct filterops i4brbchkfiltops_read =
593         { 1, NULL, i4brbchkfilt_detach, i4brbchkfilt_read };
594 static struct filterops i4brbchkfiltops_write =
595         { 1, NULL, i4brbchkfilt_detach, i4brbchkfilt_write };
596
597 PDEVSTATIC int
598 i4brbchkqfilter(struct dev_kqfilter_args *ap)
599 {
600         cdev_t dev = ap->a_head.a_dev;
601         int unit = minor(dev);
602         struct rbch_softc *sc = &rbch_softc[unit];
603         struct knote *kn = ap->a_kn;
604         struct klist *klist;
605
606         ap->a_result = 0;
607
608         switch (kn->kn_filter) {
609         case EVFILT_READ:
610                 kn->kn_fop = &i4brbchkfiltops_read;
611                 kn->kn_hook = (caddr_t)dev;
612                 break;
613         case EVFILT_WRITE:
614                 kn->kn_fop = &i4brbchkfiltops_write;
615                 kn->kn_hook = (caddr_t)dev;
616                 break;
617         default:
618                 ap->a_result = EOPNOTSUPP;
619                 return (0);
620         }
621
622         crit_enter();
623         klist = &sc->selp.si_note;
624         SLIST_INSERT_HEAD(klist, kn, kn_selnext);
625         crit_exit();
626
627         return (0);
628 }
629
630 PDEVSTATIC void
631 i4brbchkfilt_detach(struct knote *kn)
632 {
633         cdev_t dev = (cdev_t)kn->kn_hook;
634         int unit = minor(dev);
635         struct rbch_softc *sc = &rbch_softc[unit];
636         struct klist *klist;
637
638         crit_enter();
639         klist = &sc->selp.si_note;
640         SLIST_REMOVE(klist, kn, knote, kn_selnext);
641         crit_exit();
642 }
643
644 PDEVSTATIC int
645 i4brbchkfilt_read(struct knote *kn, long hint)
646 {
647         cdev_t dev = (cdev_t)kn->kn_hook;
648         int unit = minor(dev);
649         struct rbch_softc *sc = &rbch_softc[unit];
650         int ready = 0;
651
652         crit_enter();
653
654         if (!(sc->sc_devstate & ST_ISOPEN)) {
655                 crit_exit();
656                 kn->kn_flags |= EV_ERROR;
657                 kn->kn_data = EBADF;
658                 return (0);
659         }
660
661         if (sc->sc_devstate & ST_CONNECTED) {
662                 struct ifqueue *iqp;
663
664                 if(sc->sc_bprot == BPROT_RHDLC)
665                         iqp = &sc->sc_hdlcq;
666                 else
667                         iqp = isdn_linktab[unit]->rx_queue;
668
669                 if(!IF_QEMPTY(iqp))
670                         ready = 1;
671         }
672
673         crit_exit();
674
675         return (ready);
676 }
677
678 PDEVSTATIC int
679 i4brbchkfilt_write(struct knote *kn, long hint)
680 {
681         cdev_t dev = (cdev_t)kn->kn_hook;
682         int unit = minor(dev);
683         struct rbch_softc *sc = &rbch_softc[unit];
684         int ready = 0;
685
686         crit_enter();
687
688         if (!(sc->sc_devstate & ST_ISOPEN)) {
689                 crit_exit();
690                 kn->kn_flags |= EV_ERROR;
691                 kn->kn_data = EBADF;
692                 return (0);
693         }
694
695         /*
696          * Writes are OK if we are connected and the
697          * transmit queue can take them
698          */
699         if ((sc->sc_devstate & ST_CONNECTED) &&
700            !IF_QFULL(isdn_linktab[unit]->tx_queue))
701         {
702                 ready = 1;
703         }
704
705         crit_exit();
706
707         return (ready);
708 }
709
710 #if I4BRBCHACCT
711 /*---------------------------------------------------------------------------*
712  *      watchdog routine
713  *---------------------------------------------------------------------------*/
714 static void
715 rbch_timeout(struct rbch_softc *sc)
716 {
717         bchan_statistics_t bs;
718         int unit = sc->sc_unit;
719
720         /* get # of bytes in and out from the HSCX driver */ 
721         
722         (*isdn_linktab[unit]->bch_stat)
723                 (isdn_linktab[unit]->unit, isdn_linktab[unit]->channel, &bs);
724
725         sc->sc_ioutb += bs.outbytes;
726         sc->sc_iinb += bs.inbytes;
727         
728         if((sc->sc_iinb != sc->sc_linb) || (sc->sc_ioutb != sc->sc_loutb) || sc->sc_fn) 
729         {
730                 int ri = (sc->sc_iinb - sc->sc_linb)/I4BRBCHACCTINTVL;
731                 int ro = (sc->sc_ioutb - sc->sc_loutb)/I4BRBCHACCTINTVL;
732
733                 if((sc->sc_iinb == sc->sc_linb) && (sc->sc_ioutb == sc->sc_loutb))
734                         sc->sc_fn = 0;
735                 else
736                         sc->sc_fn = 1;
737                         
738                 sc->sc_linb = sc->sc_iinb;
739                 sc->sc_loutb = sc->sc_ioutb;
740
741                 i4b_l4_accounting(BDRV_RBCH, unit, ACCT_DURING,
742                          sc->sc_ioutb, sc->sc_iinb, ro, ri, sc->sc_ioutb, sc->sc_iinb);
743         }
744         callout_reset(&sc->sc_timeout, I4BRBCHACCTINTVL * hz,
745                         (void *)rbch_timeout, sc);
746 }
747 #endif /* I4BRBCHACCT */
748
749 /*===========================================================================*
750  *                      ISDN INTERFACE ROUTINES
751  *===========================================================================*/
752
753 /*---------------------------------------------------------------------------*
754  *      this routine is called from L4 handler at connect time
755  *---------------------------------------------------------------------------*/
756 static void
757 rbch_connect(int unit, void *cdp)
758 {
759         call_desc_t *cd = (call_desc_t *)cdp;
760         struct rbch_softc *sc = &rbch_softc[unit];
761
762         sc->sc_bprot = cd->bprot;
763
764 #if I4BRBCHACCT
765         if(sc->sc_bprot == BPROT_RHDLC)
766         {       
767                 sc->sc_iinb = 0;
768                 sc->sc_ioutb = 0;
769                 sc->sc_linb = 0;
770                 sc->sc_loutb = 0;
771
772                 callout_reset(&sc->sc_timeout, I4BRBCHACCTINTVL * hz,
773                                 (void *)rbch_timeout, sc);
774         }
775 #endif          
776         if(!(sc->sc_devstate & ST_CONNECTED))
777         {
778                 NDBGL4(L4_RBCHDBG, "unit %d, wakeup", unit);
779                 sc->sc_devstate |= ST_CONNECTED;
780                 sc->sc_cd = cdp;
781                 wakeup((caddr_t)sc);
782         }
783 }
784
785 /*---------------------------------------------------------------------------*
786  *      this routine is called from L4 handler at disconnect time
787  *---------------------------------------------------------------------------*/
788 static void
789 rbch_disconnect(int unit, void *cdp)
790 {
791         call_desc_t *cd = (call_desc_t *)cdp;
792         struct rbch_softc *sc = &rbch_softc[unit];
793
794         CRIT_VAR;
795         
796         if(cd != sc->sc_cd)
797         {
798                 NDBGL4(L4_RBCHDBG, "rbch%d: channel %d not active",
799                         cd->driver_unit, cd->channelid);
800                 return;
801         }
802
803         CRIT_BEG;
804         
805         NDBGL4(L4_RBCHDBG, "unit %d, disconnect", unit);
806
807         sc->sc_devstate &= ~ST_CONNECTED;
808
809         sc->sc_cd = NULL;
810         
811 #if I4BRBCHACCT
812         i4b_l4_accounting(BDRV_RBCH, unit, ACCT_FINAL,
813                  sc->sc_ioutb, sc->sc_iinb, 0, 0, sc->sc_ioutb, sc->sc_iinb);
814         callout_stop(&sc->sc_timeout);
815 #endif          
816         CRIT_END;
817 }
818         
819 /*---------------------------------------------------------------------------*
820  *      feedback from daemon in case of dial problems
821  *---------------------------------------------------------------------------*/
822 static void
823 rbch_dialresponse(int unit, int status, cause_t cause)
824 {
825 }
826         
827 /*---------------------------------------------------------------------------*
828  *      interface up/down
829  *---------------------------------------------------------------------------*/
830 static void
831 rbch_updown(int unit, int updown)
832 {
833 }
834         
835 /*---------------------------------------------------------------------------*
836  *      this routine is called from the HSCX interrupt handler
837  *      when a new frame (mbuf) has been received and is to be put on
838  *      the rx queue.
839  *---------------------------------------------------------------------------*/
840 static void
841 rbch_rx_data_rdy(int unit)
842 {
843         if(rbch_softc[unit].sc_bprot == BPROT_RHDLC)
844         {
845                 struct mbuf *m;
846                 
847                 if((m = *isdn_linktab[unit]->rx_mbuf) == NULL)
848                         return;
849
850                 m->m_pkthdr.len = m->m_len;
851
852                 if(IF_QFULL(&(rbch_softc[unit].sc_hdlcq)))
853                 {
854                         NDBGL4(L4_RBCHDBG, "unit %d: hdlc rx queue full!", unit);
855                         m_freem(m);
856                 }                       
857                 else
858                 {
859                         IF_ENQUEUE(&(rbch_softc[unit].sc_hdlcq), m);
860                 }
861         }
862
863         if(rbch_softc[unit].sc_devstate & ST_RDWAITDATA)
864         {
865                 NDBGL4(L4_RBCHDBG, "unit %d, wakeup", unit);
866                 rbch_softc[unit].sc_devstate &= ~ST_RDWAITDATA;
867                 wakeup((caddr_t) &isdn_linktab[unit]->rx_queue);
868         }
869         else
870         {
871                 NDBGL4(L4_RBCHDBG, "unit %d, NO wakeup", unit);
872         }
873         selwakeup(&rbch_softc[unit].selp);
874 }
875
876 /*---------------------------------------------------------------------------*
877  *      this routine is called from the HSCX interrupt handler
878  *      when the last frame has been sent out and there is no
879  *      further frame (mbuf) in the tx queue.
880  *---------------------------------------------------------------------------*/
881 static void
882 rbch_tx_queue_empty(int unit)
883 {
884         if(rbch_softc[unit].sc_devstate & ST_WRWAITEMPTY)
885         {
886                 NDBGL4(L4_RBCHDBG, "unit %d, wakeup", unit);
887                 rbch_softc[unit].sc_devstate &= ~ST_WRWAITEMPTY;
888                 wakeup((caddr_t) &isdn_linktab[unit]->tx_queue);
889         }
890         else
891         {
892                 NDBGL4(L4_RBCHDBG, "unit %d, NO wakeup", unit);
893         }
894         selwakeup(&rbch_softc[unit].selp);
895 }
896
897 /*---------------------------------------------------------------------------*
898  *      this routine is called from the HSCX interrupt handler
899  *      each time a packet is received or transmitted
900  *---------------------------------------------------------------------------*/
901 static void
902 rbch_activity(int unit, int rxtx)
903 {
904         if (rbch_softc[unit].sc_cd)
905                 rbch_softc[unit].sc_cd->last_active_time = SECOND;
906         selwakeup(&rbch_softc[unit].selp);
907 }
908
909 /*---------------------------------------------------------------------------*
910  *      clear an hdlc rx queue for a rbch unit
911  *---------------------------------------------------------------------------*/
912 static void
913 rbch_clrq(int unit)
914 {
915         CRIT_VAR;
916
917         CRIT_BEG;
918         IF_DRAIN(&rbch_softc[unit].sc_hdlcq);
919         CRIT_END;
920 }
921                                 
922 /*---------------------------------------------------------------------------*
923  *      return this drivers linktab address
924  *---------------------------------------------------------------------------*/
925 drvr_link_t *
926 rbch_ret_linktab(int unit)
927 {
928         rbch_init_linktab(unit);
929         return(&rbch_drvr_linktab[unit]);
930 }
931
932 /*---------------------------------------------------------------------------*
933  *      setup the isdn_linktab for this driver
934  *---------------------------------------------------------------------------*/
935 void
936 rbch_set_linktab(int unit, isdn_link_t *ilt)
937 {
938         isdn_linktab[unit] = ilt;
939 }
940
941 /*---------------------------------------------------------------------------*
942  *      initialize this drivers linktab
943  *---------------------------------------------------------------------------*/
944 static void
945 rbch_init_linktab(int unit)
946 {
947         rbch_drvr_linktab[unit].unit = unit;
948         rbch_drvr_linktab[unit].bch_rx_data_ready = rbch_rx_data_rdy;
949         rbch_drvr_linktab[unit].bch_tx_queue_empty = rbch_tx_queue_empty;
950         rbch_drvr_linktab[unit].bch_activity = rbch_activity;   
951         rbch_drvr_linktab[unit].line_connected = rbch_connect;
952         rbch_drvr_linktab[unit].line_disconnected = rbch_disconnect;
953         rbch_drvr_linktab[unit].dial_response = rbch_dialresponse;
954         rbch_drvr_linktab[unit].updown_ind = rbch_updown;       
955 }
956
957 /*===========================================================================*/
958
959 #endif /* NI4BRBCH > 0 */