]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/i4b/layer4/i4b_i4bdrv.c
This commit was generated by cvs2svn to compensate for changes in r68068,
[FreeBSD/FreeBSD.git] / sys / i4b / layer4 / i4b_i4bdrv.c
1 /*
2  * Copyright (c) 1997, 2000 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_i4bdrv.c - i4b userland interface driver
28  *      --------------------------------------------
29  *
30  *      $Id: i4b_i4bdrv.c,v 1.59 2000/10/06 08:37:43 hm Exp $ 
31  *
32  * $FreeBSD$
33  *
34  *      last edit-date: [Mon Oct  2 09:55:28 2000]
35  *
36  *---------------------------------------------------------------------------*/
37
38 #include "i4b.h"
39 #include "i4bipr.h"
40 #include "i4btel.h"
41
42
43 #if NI4B > 1
44 #error "only 1 (one) i4b device possible!"
45 #endif
46
47 #if NI4B > 0
48
49 #include <sys/param.h>
50
51 #if defined(__FreeBSD__)
52 #include <sys/ioccom.h>
53 #include <sys/malloc.h>
54 #include <sys/uio.h>
55 #else
56 #include <sys/ioctl.h>
57 #endif
58
59 #include <sys/kernel.h>
60 #include <sys/systm.h>
61 #include <sys/conf.h>
62 #include <sys/mbuf.h>
63 #include <sys/socket.h>
64 #include <sys/select.h>
65 #include <net/if.h>
66
67 #ifdef __NetBSD__
68 #include <sys/types.h>
69 #endif
70
71 #if defined(__FreeBSD__)
72 #include "i4bing.h"
73 #endif
74
75 #ifdef __bsdi__
76 #include "ibc.h"
77 #else
78 #ifdef __FreeBSD__
79 #include "i4bisppp.h"
80 #else
81 #include <net/if_sppp.h>
82 #endif
83 #endif
84
85 #ifdef __FreeBSD__
86
87 #if defined(__FreeBSD__) && __FreeBSD__ == 3
88 #include "opt_devfs.h"
89 #endif
90
91 #ifdef DEVFS
92 #include <sys/devfsext.h>
93 #endif
94
95 #endif /* __FreeBSD__*/
96
97 #ifdef __FreeBSD__
98 #include <machine/i4b_debug.h>
99 #include <machine/i4b_ioctl.h>
100 #include <machine/i4b_cause.h>
101 #else
102 #include <i4b/i4b_debug.h>
103 #include <i4b/i4b_ioctl.h>
104 #include <i4b/i4b_cause.h>
105 #endif
106
107 #include <i4b/include/i4b_l3l4.h>
108 #include <i4b/include/i4b_mbuf.h>
109 #include <i4b/include/i4b_global.h>
110
111 #include <i4b/layer4/i4b_l4.h>
112
113 #ifdef OS_USES_POLL
114 #include <sys/poll.h>
115 #endif
116
117 struct selinfo select_rd_info;
118
119 static struct ifqueue i4b_rdqueue;
120 static int openflag = 0;
121 static int selflag = 0;
122 static int readflag = 0;
123
124 #if defined(__FreeBSD__) && __FreeBSD__ == 3
125 #ifdef DEVFS
126 static void *devfs_token;
127 #endif
128 #endif
129
130 #ifndef __FreeBSD__
131
132 #define PDEVSTATIC      /* - not static - */
133 PDEVSTATIC void i4battach __P((void));
134 PDEVSTATIC int i4bopen __P((dev_t dev, int flag, int fmt, struct proc *p));
135 PDEVSTATIC int i4bclose __P((dev_t dev, int flag, int fmt, struct proc *p));
136 PDEVSTATIC int i4bread __P((dev_t dev, struct uio *uio, int ioflag));
137
138 #ifdef __bsdi__
139 PDEVSTATIC int i4bioctl __P((dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p));
140 #else
141 PDEVSTATIC int i4bioctl __P((dev_t dev, int cmd, caddr_t data, int flag, struct proc *p));
142 #endif
143
144 #ifdef OS_USES_POLL
145 PDEVSTATIC int i4bpoll __P((dev_t dev, int events, struct proc *p));
146 #else
147 PDEVSTATIC int i4bselect __P((dev_t dev, int rw, struct proc *p));
148 #endif
149
150 #endif /* #ifndef __FreeBSD__ */
151
152 #if BSD > 199306 && defined(__FreeBSD__)
153
154 #define PDEVSTATIC      static
155
156 PDEVSTATIC      d_open_t        i4bopen;
157 PDEVSTATIC      d_close_t       i4bclose;
158 PDEVSTATIC      d_read_t        i4bread;
159 PDEVSTATIC      d_ioctl_t       i4bioctl;
160
161 #ifdef OS_USES_POLL
162 PDEVSTATIC      d_poll_t        i4bpoll;
163 #define POLLFIELD               i4bpoll
164 #else
165 PDEVSTATIC      d_select_t      i4bselect;
166 #define POLLFIELD               i4bselect
167 #endif
168
169 #define CDEV_MAJOR 60
170
171 #if defined(__FreeBSD__) && __FreeBSD__ >= 4
172 static struct cdevsw i4b_cdevsw = {
173         /* open */      i4bopen,
174         /* close */     i4bclose,
175         /* read */      i4bread,
176         /* write */     nowrite,
177         /* ioctl */     i4bioctl,
178         /* poll */      POLLFIELD,
179         /* mmap */      nommap,
180         /* strategy */  nostrategy,
181         /* name */      "i4b",
182         /* maj */       CDEV_MAJOR,
183         /* dump */      nodump,
184         /* psize */     nopsize,
185         /* flags */     0,
186         /* bmaj */      -1
187 };
188 #else
189 static struct cdevsw i4b_cdevsw = {
190         i4bopen,        i4bclose,       i4bread,        nowrite,
191         i4bioctl,       nostop,         nullreset,      nodevtotty,
192         POLLFIELD,      nommap,         NULL,           "i4b", NULL,    -1
193 };
194 #endif
195
196 PDEVSTATIC void i4battach(void *);
197 PSEUDO_SET(i4battach, i4b_i4bdrv);
198
199 static void
200 i4b_drvinit(void *unused)
201 {
202 #if defined(__FreeBSD__) && __FreeBSD__ >= 4
203         cdevsw_add(&i4b_cdevsw);
204 #else
205         static int i4b_devsw_installed = 0;
206         dev_t dev;
207
208         if( ! i4b_devsw_installed )
209         {
210                 dev = makedev(CDEV_MAJOR,0);
211                 cdevsw_add(&dev,&i4b_cdevsw,NULL);
212                 i4b_devsw_installed = 1;
213         }
214 #endif
215 }
216
217 SYSINIT(i4bdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,i4b_drvinit,NULL)
218
219 #endif /* BSD > 199306 && defined(__FreeBSD__) */
220
221 #ifdef __bsdi__
222 #include <sys/device.h>
223 int i4bmatch(struct device *parent, struct cfdata *cf, void *aux);
224 void dummy_i4battach(struct device*, struct device *, void *);
225
226 #define CDEV_MAJOR 65
227
228 static struct cfdriver i4bcd =
229         { NULL, "i4b", i4bmatch, dummy_i4battach, DV_DULL,
230           sizeof(struct cfdriver) };
231 struct devsw i4bsw = 
232         { &i4bcd,
233           i4bopen,      i4bclose,       i4bread,        nowrite,
234           i4bioctl,     i4bselect,      nommap,         nostrat,
235           nodump,       nopsize,        0,              nostop
236 };
237
238 int
239 i4bmatch(struct device *parent, struct cfdata *cf, void *aux)
240 {
241         printf("i4bmatch: aux=0x%x\n", aux);
242         return 1;
243 }
244 void
245 dummy_i4battach(struct device *parent, struct device *self, void *aux)
246 {
247         printf("dummy_i4battach: aux=0x%x\n", aux);
248 }
249 #endif /* __bsdi__ */
250
251 /*---------------------------------------------------------------------------*
252  *      interface attach routine
253  *---------------------------------------------------------------------------*/
254 PDEVSTATIC void
255 #ifdef __FreeBSD__
256 i4battach(void *dummy)
257 #else
258 i4battach()
259 #endif
260 {
261 #ifndef HACK_NO_PSEUDO_ATTACH_MSG
262         printf("i4b: ISDN call control device attached\n");
263 #endif
264         i4b_rdqueue.ifq_maxlen = IFQ_MAXLEN;
265
266 #if defined(__FreeBSD__)
267 #if __FreeBSD__ == 3
268
269 #ifdef DEVFS
270         devfs_token = devfs_add_devswf(&i4b_cdevsw, 0, DV_CHR,
271                                        UID_ROOT, GID_WHEEL, 0600,
272                                        "i4b");
273 #endif
274
275 #else
276         make_dev(&i4b_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "i4b");
277 #endif
278 #endif
279 }
280
281 /*---------------------------------------------------------------------------*
282  *      i4bopen - device driver open routine
283  *---------------------------------------------------------------------------*/
284 PDEVSTATIC int
285 i4bopen(dev_t dev, int flag, int fmt, struct proc *p)
286 {
287         int x;
288         
289         if(minor(dev))
290                 return(ENXIO);
291
292         if(openflag)
293                 return(EBUSY);
294         
295         x = splimp();
296         openflag = 1;
297         i4b_l4_daemon_attached();
298         splx(x);
299         
300         return(0);
301 }
302
303 /*---------------------------------------------------------------------------*
304  *      i4bclose - device driver close routine
305  *---------------------------------------------------------------------------*/
306 PDEVSTATIC int
307 i4bclose(dev_t dev, int flag, int fmt, struct proc *p)
308 {
309         int x = splimp();       
310         openflag = 0;
311         i4b_l4_daemon_detached();
312         i4b_Dcleanifq(&i4b_rdqueue);
313         splx(x);
314         return(0);
315 }
316
317 /*---------------------------------------------------------------------------*
318  *      i4bread - device driver read routine
319  *---------------------------------------------------------------------------*/
320 PDEVSTATIC int
321 i4bread(dev_t dev, struct uio *uio, int ioflag)
322 {
323         struct mbuf *m;
324         int x;
325         int error = 0;
326
327         if(minor(dev))
328                 return(ENODEV);
329
330         x = splimp();
331         while(IF_QEMPTY(&i4b_rdqueue))
332         {
333                 readflag = 1;
334                 error = tsleep((caddr_t) &i4b_rdqueue, (PZERO + 1) | PCATCH, "bird", 0);
335                 if (error != 0) {
336                 splx(x);
337                         return error;
338                 }
339         }
340
341         IF_DEQUEUE(&i4b_rdqueue, m);
342
343         splx(x);
344                 
345         if(m && m->m_len)
346                 error = uiomove(m->m_data, m->m_len, uio);
347         else
348                 error = EIO;
349                 
350         if(m)
351                 i4b_Dfreembuf(m);
352
353         return(error);
354 }
355
356 /*---------------------------------------------------------------------------*
357  *      i4bioctl - device driver ioctl routine
358  *---------------------------------------------------------------------------*/
359 PDEVSTATIC int
360 #if defined (__FreeBSD_version) && __FreeBSD_version >= 300003
361 i4bioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
362 #elif defined(__bsdi__)
363 i4bioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
364 #else
365 i4bioctl(dev_t dev, int cmd, caddr_t data, int flag, struct proc *p)
366 #endif
367 {
368         call_desc_t *cd;
369         int error = 0;
370         
371         if(minor(dev))
372                 return(ENODEV);
373
374         switch(cmd)
375         {
376                 /* cdid request, reserve cd and return cdid */
377
378                 case I4B_CDID_REQ:
379                 {
380                         msg_cdid_req_t *mir;
381                         mir = (msg_cdid_req_t *)data;
382                         cd = reserve_cd();
383                         mir->cdid = cd->cdid;
384                         break;
385                 }
386                 
387                 /* connect request, dial out to remote */
388                 
389                 case I4B_CONNECT_REQ:
390                 {
391                         msg_connect_req_t *mcr;
392                         mcr = (msg_connect_req_t *)data;        /* setup ptr */
393
394                         if((cd = cd_by_cdid(mcr->cdid)) == NULL)/* get cd */
395                         {
396                                 NDBGL4(L4_ERR, "I4B_CONNECT_REQ ioctl, cdid not found!"); 
397                                 error = EINVAL;
398                                 break;
399                         }
400
401                         /* prevent dialling on leased lines */
402                         if(ctrl_desc[mcr->controller].protocol == PROTOCOL_D64S)
403                         {
404                                 SET_CAUSE_TYPE(cd->cause_in, CAUSET_I4B);
405                                 SET_CAUSE_VAL(cd->cause_in, CAUSE_I4B_LLDIAL);
406                                 i4b_l4_disconnect_ind(cd);
407                                 freecd_by_cd(cd);
408                                 break;
409                         }
410
411                         cd->controller = mcr->controller;       /* fill cd */
412                         cd->bprot = mcr->bprot;
413                         cd->driver = mcr->driver;
414                         cd->driver_unit = mcr->driver_unit;
415                         cd->cr = get_rand_cr(ctrl_desc[cd->controller].unit);
416
417                         cd->shorthold_data.shorthold_algorithm = mcr->shorthold_data.shorthold_algorithm;
418                         cd->shorthold_data.unitlen_time  = mcr->shorthold_data.unitlen_time;
419                         cd->shorthold_data.idle_time     = mcr->shorthold_data.idle_time;
420                         cd->shorthold_data.earlyhup_time = mcr->shorthold_data.earlyhup_time;
421
422                         cd->last_aocd_time = 0;
423                         if(mcr->unitlen_method == ULEN_METHOD_DYNAMIC)
424                                 cd->aocd_flag = 1;
425                         else
426                                 cd->aocd_flag = 0;
427                                 
428                         cd->cunits = 0;
429
430                         cd->max_idle_time = 0;  /* this is outgoing */
431
432                         cd->dir = DIR_OUTGOING;
433                         
434                         NDBGL4(L4_TIMO, "I4B_CONNECT_REQ times, algorithm=%ld unitlen=%ld idle=%ld earlyhup=%ld",
435                                         (long)cd->shorthold_data.shorthold_algorithm, (long)cd->shorthold_data.unitlen_time,
436                                         (long)cd->shorthold_data.idle_time, (long)cd->shorthold_data.earlyhup_time);
437
438                         strcpy(cd->dst_telno, mcr->dst_telno);
439                         strcpy(cd->src_telno, mcr->src_telno);
440                         cd->display[0] = '\0';
441
442                         SET_CAUSE_TYPE(cd->cause_in, CAUSET_I4B);
443                         SET_CAUSE_VAL(cd->cause_in, CAUSE_I4B_NORMAL);
444                         
445                         switch(mcr->channel)
446                         {
447                                 case CHAN_B1:
448                                 case CHAN_B2:
449                                         if(ctrl_desc[mcr->controller].bch_state[mcr->channel] != BCH_ST_FREE)
450                                                 SET_CAUSE_VAL(cd->cause_in, CAUSE_I4B_NOCHAN);
451                                         break;
452
453                                 case CHAN_ANY:
454                                         if((ctrl_desc[mcr->controller].bch_state[CHAN_B1] != BCH_ST_FREE) &&
455                                            (ctrl_desc[mcr->controller].bch_state[CHAN_B2] != BCH_ST_FREE))
456                                                 SET_CAUSE_VAL(cd->cause_in, CAUSE_I4B_NOCHAN);
457                                         break;
458
459                                 default:
460                                         SET_CAUSE_VAL(cd->cause_in, CAUSE_I4B_NOCHAN);
461                                         break;
462                         }
463
464                         cd->channelid = mcr->channel;
465
466                         cd->isdntxdelay = mcr->txdelay;
467                         
468                         /* check whether we have a pointer. Seems like */
469                         /* this should be adequate. GJ 19.09.97 */
470                         if(ctrl_desc[cd->controller].N_CONNECT_REQUEST == NULL)
471 /*XXX*/                         SET_CAUSE_VAL(cd->cause_in, CAUSE_I4B_NOCHAN);
472
473                         if((GET_CAUSE_VAL(cd->cause_in)) != CAUSE_I4B_NORMAL)
474                         {
475                                 i4b_l4_disconnect_ind(cd);
476                                 freecd_by_cd(cd);
477                         }
478                         else
479                         {
480                                 (*ctrl_desc[cd->controller].N_CONNECT_REQUEST)(mcr->cdid);
481                         }
482                         break;
483                 }
484                 
485                 /* connect response, accept/reject/ignore incoming call */
486                 
487                 case I4B_CONNECT_RESP:
488                 {
489                         msg_connect_resp_t *mcrsp;
490                         
491                         mcrsp = (msg_connect_resp_t *)data;
492
493                         if((cd = cd_by_cdid(mcrsp->cdid)) == NULL)/* get cd */
494                         {
495                                 NDBGL4(L4_ERR, "I4B_CONNECT_RESP ioctl, cdid not found!"); 
496                                 error = EINVAL;
497                                 break;
498                         }
499
500                         T400_stop(cd);
501
502                         cd->driver = mcrsp->driver;
503                         cd->driver_unit = mcrsp->driver_unit;
504                         cd->max_idle_time = mcrsp->max_idle_time;
505
506                         cd->shorthold_data.shorthold_algorithm = SHA_FIXU;
507                         cd->shorthold_data.unitlen_time = 0;    /* this is incoming */
508                         cd->shorthold_data.idle_time = 0;
509                         cd->shorthold_data.earlyhup_time = 0;
510
511                         cd->isdntxdelay = mcrsp->txdelay;                       
512                         
513                         NDBGL4(L4_TIMO, "I4B_CONNECT_RESP max_idle_time set to %ld seconds", (long)cd->max_idle_time);
514
515                         (*ctrl_desc[cd->controller].N_CONNECT_RESPONSE)(mcrsp->cdid, mcrsp->response, mcrsp->cause);
516                         break;
517                 }
518                 
519                 /* disconnect request, actively terminate connection */
520                 
521                 case I4B_DISCONNECT_REQ:
522                 {
523                         msg_discon_req_t *mdr;
524                         
525                         mdr = (msg_discon_req_t *)data;
526
527                         if((cd = cd_by_cdid(mdr->cdid)) == NULL)/* get cd */
528                         {
529                                 NDBGL4(L4_ERR, "I4B_DISCONNECT_REQ ioctl, cdid not found!"); 
530                                 error = EINVAL;
531                                 break;
532                         }
533
534                         /* preset causes with our cause */
535                         cd->cause_in = cd->cause_out = mdr->cause;
536                         
537                         (*ctrl_desc[cd->controller].N_DISCONNECT_REQUEST)(mdr->cdid, mdr->cause);
538                         break;
539                 }
540                 
541                 /* controller info request */
542
543                 case I4B_CTRL_INFO_REQ:
544                 {
545                         msg_ctrl_info_req_t *mcir;
546                         
547                         mcir = (msg_ctrl_info_req_t *)data;
548                         mcir->ncontroller = nctrl;
549
550                         if(mcir->controller > nctrl)
551                         {
552                                 mcir->ctrl_type = -1;
553                                 mcir->card_type = -1;
554                         }
555                         else
556                         {
557                                 mcir->ctrl_type = 
558                                         ctrl_desc[mcir->controller].ctrl_type;
559                                 mcir->card_type = 
560                                         ctrl_desc[mcir->controller].card_type;
561
562                                 if(ctrl_desc[mcir->controller].ctrl_type == CTRL_PASSIVE)
563                                         mcir->tei = ctrl_desc[mcir->controller].tei;
564                                 else
565                                         mcir->tei = -1;
566                         }
567                         break;
568                 }
569                 
570                 /* dial response */
571                 
572                 case I4B_DIALOUT_RESP:
573                 {
574                         drvr_link_t *dlt = NULL;
575                         msg_dialout_resp_t *mdrsp;
576                         
577                         mdrsp = (msg_dialout_resp_t *)data;
578
579                         switch(mdrsp->driver)
580                         {
581 #if NI4BIPR > 0
582                                 case BDRV_IPR:
583                                         dlt = ipr_ret_linktab(mdrsp->driver_unit);
584                                         break;
585 #endif                                  
586
587 #if NI4BISPPP > 0
588                                 case BDRV_ISPPP:
589                                         dlt = i4bisppp_ret_linktab(mdrsp->driver_unit);
590                                         break;
591 #endif
592
593 #if NI4BTEL > 0
594                                 case BDRV_TEL:
595                                         dlt = tel_ret_linktab(mdrsp->driver_unit);
596                                         break;
597 #endif
598
599 #if NIBC > 0
600                                 case BDRV_IBC:
601                                         dlt = ibc_ret_linktab(mdrsp->driver_unit);
602                                         break;
603 #endif
604
605 #if NI4BING > 0
606                                 case BDRV_ING:
607                                         dlt = ing_ret_linktab(mdrsp->driver_unit);
608                                         break;
609 #endif                                  
610                         }
611
612                         if(dlt != NULL)         
613                                 (*dlt->dial_response)(mdrsp->driver_unit, mdrsp->stat, mdrsp->cause);
614                         break;
615                 }
616                 
617                 /* update timeout value */
618                 
619                 case I4B_TIMEOUT_UPD:
620                 {
621                         msg_timeout_upd_t *mtu;
622                         int x;
623                         
624                         mtu = (msg_timeout_upd_t *)data;
625
626                         NDBGL4(L4_TIMO, "I4B_TIMEOUT_UPD ioctl, alg %d, unit %d, idle %d, early %d!",
627                                         mtu->shorthold_data.shorthold_algorithm, mtu->shorthold_data.unitlen_time,
628                                         mtu->shorthold_data.idle_time, mtu->shorthold_data.earlyhup_time); 
629
630                         if((cd = cd_by_cdid(mtu->cdid)) == NULL)/* get cd */
631                         {
632                                 NDBGL4(L4_ERR, "I4B_TIMEOUT_UPD ioctl, cdid not found!"); 
633                                 error = EINVAL;
634                                 break;
635                         }
636
637                         switch( mtu->shorthold_data.shorthold_algorithm )
638                         {
639                                 case SHA_FIXU:
640                                         /*
641                                          * For this algorithm unitlen_time,
642                                          * idle_time and earlyhup_time are used.
643                                          */
644
645                                         if(!(mtu->shorthold_data.unitlen_time >= 0 &&
646                                              mtu->shorthold_data.idle_time >= 0    &&
647                                              mtu->shorthold_data.earlyhup_time >= 0))
648                                         {
649                                                 NDBGL4(L4_ERR, "I4B_TIMEOUT_UPD ioctl, invalid args for fix unit algorithm!"); 
650                                                 error = EINVAL;
651                                         }
652                                         break;
653         
654                                 case SHA_VARU:
655                                         /*
656                                          * For this algorithm unitlen_time and
657                                          * idle_time are used. both must be
658                                          * positive integers. earlyhup_time is
659                                          * not used and must be 0.
660                                          */
661
662                                         if(!(mtu->shorthold_data.unitlen_time > 0 &&
663                                              mtu->shorthold_data.idle_time >= 0   &&
664                                              mtu->shorthold_data.earlyhup_time == 0))
665                                         {
666                                                 NDBGL4(L4_ERR, "I4B_TIMEOUT_UPD ioctl, invalid args for var unit algorithm!"); 
667                                                 error = EINVAL;
668                                         }
669                                         break;
670         
671                                 default:
672                                         NDBGL4(L4_ERR, "I4B_TIMEOUT_UPD ioctl, invalid algorithm!"); 
673                                         error = EINVAL;
674                                         break;
675                         }
676
677                         /*
678                          * any error set above requires us to break
679                          * out of the outer switch
680                          */
681                         if(error != 0)
682                                 break;
683
684                         x = SPLI4B();
685                         cd->shorthold_data.shorthold_algorithm = mtu->shorthold_data.shorthold_algorithm;
686                         cd->shorthold_data.unitlen_time = mtu->shorthold_data.unitlen_time;
687                         cd->shorthold_data.idle_time = mtu->shorthold_data.idle_time;
688                         cd->shorthold_data.earlyhup_time = mtu->shorthold_data.earlyhup_time;
689                         splx(x);
690                         break;
691                 }
692                         
693                 /* soft enable/disable interface */
694                 
695                 case I4B_UPDOWN_IND:
696                 {
697                         msg_updown_ind_t *mui;
698                         
699                         mui = (msg_updown_ind_t *)data;
700
701 #if NI4BIPR > 0
702                         if(mui->driver == BDRV_IPR)
703                         {
704                                 drvr_link_t *dlt;
705                                 dlt = ipr_ret_linktab(mui->driver_unit);
706                                 (*dlt->updown_ind)(mui->driver_unit, mui->updown);
707                         }
708 #endif
709                         break;
710                 }
711                 
712                 /* send ALERT request */
713                 
714                 case I4B_ALERT_REQ:
715                 {
716                         msg_alert_req_t *mar;
717                         
718                         mar = (msg_alert_req_t *)data;
719
720                         if((cd = cd_by_cdid(mar->cdid)) == NULL)
721                         {
722                                 NDBGL4(L4_ERR, "I4B_ALERT_REQ ioctl, cdid not found!"); 
723                                 error = EINVAL;
724                                 break;
725                         }
726
727                         T400_stop(cd);
728                         
729                         (*ctrl_desc[cd->controller].N_ALERT_REQUEST)(mar->cdid);
730
731                         break;
732                 }
733
734                 /* version/release number request */
735                 
736                 case I4B_VR_REQ:
737                 {
738                         msg_vr_req_t *mvr;
739
740                         mvr = (msg_vr_req_t *)data;
741
742                         mvr->version = VERSION;
743                         mvr->release = REL;
744                         mvr->step = STEP;                       
745                         break;
746                 }
747
748                 /* set D-channel protocol for a controller */
749                 
750                 case I4B_PROT_IND:
751                 {
752                         msg_prot_ind_t *mpi;
753                         
754                         mpi = (msg_prot_ind_t *)data;
755
756                         ctrl_desc[mpi->controller].protocol = mpi->protocol;
757                         
758                         break;
759                 }
760                 
761                 /* Download request */
762
763                 case I4B_CTRL_DOWNLOAD:
764                 {
765                         struct isdn_dr_prot *prots = NULL, *prots2 = NULL;
766                         struct isdn_download_request *r =
767                                 (struct isdn_download_request*)data;
768                         int i;
769
770                         if (r->controller < 0 || r->controller >= nctrl)
771                         {
772                                 error = ENODEV;
773                                 goto download_done;
774                         }
775
776                         if(!ctrl_desc[r->controller].N_DOWNLOAD)
777                         {
778                                 error = ENODEV;
779                                 goto download_done;
780                         }
781
782                         prots = malloc(r->numprotos * sizeof(struct isdn_dr_prot),
783                                         M_DEVBUF, M_WAITOK);
784
785                         prots2 = malloc(r->numprotos * sizeof(struct isdn_dr_prot),
786                                         M_DEVBUF, M_WAITOK);
787
788                         if(!prots || !prots2)
789                         {
790                                 error = ENOMEM;
791                                 goto download_done;
792                         }
793
794                         copyin(r->protocols, prots, r->numprotos * sizeof(struct isdn_dr_prot));
795
796                         for(i = 0; i < r->numprotos; i++)
797                         {
798                                 prots2[i].microcode = malloc(prots[i].bytecount, M_DEVBUF, M_WAITOK);
799                                 copyin(prots[i].microcode, prots2[i].microcode, prots[i].bytecount);
800                                 prots2[i].bytecount = prots[i].bytecount; 
801                         }
802
803                         error = ctrl_desc[r->controller].N_DOWNLOAD(
804                                                 ctrl_desc[r->controller].unit,
805                                                 r->numprotos, prots2);
806
807 download_done:
808                         if(prots2)
809                         {
810                                 for(i = 0; i < r->numprotos; i++)
811                                 {
812                                         if(prots2[i].microcode)
813                                         {
814                                                 free(prots2[i].microcode, M_DEVBUF);
815                                         }
816                                 }
817                                 free(prots2, M_DEVBUF);
818                         }
819
820                         if(prots)
821                         {
822                                 free(prots, M_DEVBUF);
823                         }
824                         break;
825                 }
826
827                 /* Diagnostic request */
828
829                 case I4B_ACTIVE_DIAGNOSTIC:
830                 {
831                         struct isdn_diagnostic_request req, *r =
832                                 (struct isdn_diagnostic_request*)data;
833
834                         req.in_param = req.out_param = NULL;
835                         if (r->controller < 0 || r->controller >= nctrl)
836                         {
837                                 error = ENODEV;
838                                 goto diag_done;
839                         }
840
841                         if(!ctrl_desc[r->controller].N_DIAGNOSTICS)
842                         {
843                                 error = ENODEV;
844                                 goto diag_done;
845                         }
846
847                         memcpy(&req, r, sizeof(req));
848
849                         if(req.in_param_len)
850                         {
851                                 req.in_param = malloc(r->in_param_len, M_DEVBUF, M_WAITOK);
852
853                                 if(!req.in_param)
854                                 {
855                                         error = ENOMEM;
856                                         goto diag_done;
857                                 }
858                                 error = copyin(r->in_param, req.in_param, req.in_param_len);
859                                 if (error)
860                                         goto diag_done;
861                         }
862
863                         if(req.out_param_len)
864                         {
865                                 req.out_param = malloc(r->out_param_len, M_DEVBUF, M_WAITOK);
866
867                                 if(!req.out_param)
868                                 {
869                                         error = ENOMEM;
870                                         goto diag_done;
871                                 }
872                         }
873                         
874                         error = ctrl_desc[r->controller].N_DIAGNOSTICS(r->controller, &req);
875
876                         if(!error && req.out_param_len)
877                                 error = copyout(req.out_param, r->out_param, req.out_param_len);
878
879 diag_done:
880                         if(req.in_param)
881                                 free(req.in_param, M_DEVBUF);
882                                 
883                         if(req.out_param)
884                                 free(req.out_param, M_DEVBUF);
885
886                         break;
887                 }
888
889                 /* default */
890                 
891                 default:
892                         error = ENOTTY;
893                         break;
894         }
895         
896         return(error);
897 }
898
899 #ifdef OS_USES_SELECT
900
901 /*---------------------------------------------------------------------------*
902  *      i4bselect - device driver select routine
903  *---------------------------------------------------------------------------*/
904 PDEVSTATIC int
905 i4bselect(dev_t dev, int rw, struct proc *p)
906 {
907         int x;
908         
909         if(minor(dev))
910                 return(ENODEV);
911
912         switch(rw)
913         {
914                 case FREAD:
915                         if(!IF_QEMPTY(&i4b_rdqueue))
916                                 return(1);
917                         x = splimp();
918                         selrecord(p, &select_rd_info);
919                         selflag = 1;
920                         splx(x);
921                         return(0);
922                         break;
923
924                 case FWRITE:
925                         return(1);
926                         break;
927         }
928         return(0);
929 }
930
931 #else /* OS_USES_SELECT */
932
933 /*---------------------------------------------------------------------------*
934  *      i4bpoll - device driver poll routine
935  *---------------------------------------------------------------------------*/
936 PDEVSTATIC int
937 i4bpoll(dev_t dev, int events, struct proc *p)
938 {
939         int x;
940         
941         if(minor(dev))
942                 return(ENODEV);
943
944         if((events & POLLIN) || (events & POLLRDNORM))
945         {
946                 if(!IF_QEMPTY(&i4b_rdqueue))
947                         return(1);
948
949                 x = splimp();
950                 selrecord(p, &select_rd_info);
951                 selflag = 1;
952                 splx(x);
953                 return(0);
954         }
955         else if((events & POLLOUT) || (events & POLLWRNORM))
956         {
957                 return(1);
958         }
959
960         return(0);
961 }
962
963 #endif /* OS_USES_SELECT */
964
965 /*---------------------------------------------------------------------------*
966  *      i4bputqueue - put message into queue to userland
967  *---------------------------------------------------------------------------*/
968 void
969 i4bputqueue(struct mbuf *m)
970 {
971         int x;
972         
973         if(!openflag)
974         {
975                 i4b_Dfreembuf(m);
976                 return;
977         }
978
979         x = splimp();
980         
981         if(IF_QFULL(&i4b_rdqueue))
982         {
983                 struct mbuf *m1;
984                 IF_DEQUEUE(&i4b_rdqueue, m1);
985                 i4b_Dfreembuf(m1);
986                 NDBGL4(L4_ERR, "ERROR, queue full, removing entry!");
987         }
988
989         IF_ENQUEUE(&i4b_rdqueue, m);
990
991         splx(x);        
992
993         if(readflag)
994         {
995                 readflag = 0;
996                 wakeup((caddr_t) &i4b_rdqueue);
997         }
998
999         if(selflag)
1000         {
1001                 selflag = 0;
1002                 selwakeup(&select_rd_info);
1003         }
1004 }
1005
1006 /*---------------------------------------------------------------------------*
1007  *      i4bputqueue_hipri - put message into front of queue to userland
1008  *---------------------------------------------------------------------------*/
1009 void
1010 i4bputqueue_hipri(struct mbuf *m)
1011 {
1012         int x;
1013         
1014         if(!openflag)
1015         {
1016                 i4b_Dfreembuf(m);
1017                 return;
1018         }
1019
1020         x = splimp();
1021         
1022         if(IF_QFULL(&i4b_rdqueue))
1023         {
1024                 struct mbuf *m1;
1025                 IF_DEQUEUE(&i4b_rdqueue, m1);
1026                 i4b_Dfreembuf(m1);
1027                 NDBGL4(L4_ERR, "ERROR, queue full, removing entry!");
1028         }
1029
1030         IF_PREPEND(&i4b_rdqueue, m);
1031
1032         splx(x);        
1033
1034         if(readflag)
1035         {
1036                 readflag = 0;
1037                 wakeup((caddr_t) &i4b_rdqueue);
1038         }
1039
1040         if(selflag)
1041         {
1042                 selflag = 0;
1043                 selwakeup(&select_rd_info);
1044         }
1045 }
1046
1047 #endif /* NI4B > 0 */