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_i4bdrv.c - i4b userland interface driver
28 * --------------------------------------------
30 * $FreeBSD: src/sys/i4b/layer4/i4b_i4bdrv.c,v 1.11.2.5 2001/12/16 15:12:59 hm Exp $
32 * last edit-date: [Sat Aug 11 18:08:10 2001]
34 *---------------------------------------------------------------------------*/
37 #include "use_i4bipr.h"
38 #include "use_i4btel.h"
41 #error "only 1 (one) i4b device possible!"
46 #include <sys/param.h>
48 #include <sys/malloc.h>
50 #include <sys/kernel.h>
51 #include <sys/systm.h>
53 #include <sys/device.h>
55 #include <sys/socket.h>
56 #include <sys/thread2.h>
60 #include "use_i4bing.h"
61 #include "use_i4bisppp.h"
63 #include <net/i4b/include/machine/i4b_debug.h>
64 #include <net/i4b/include/machine/i4b_ioctl.h>
65 #include <net/i4b/include/machine/i4b_cause.h>
67 #include "../include/i4b_l3l4.h"
68 #include "../include/i4b_mbuf.h"
69 #include "../include/i4b_global.h"
73 #include <sys/event.h>
75 struct kqinfo kq_rd_info;
77 static struct ifqueue i4b_rdqueue;
78 static int openflag = 0;
79 static int readflag = 0;
81 #define PDEVSTATIC static
83 PDEVSTATIC d_open_t i4bopen;
84 PDEVSTATIC d_close_t i4bclose;
85 PDEVSTATIC d_read_t i4bread;
86 PDEVSTATIC d_ioctl_t i4bioctl;
87 PDEVSTATIC d_kqfilter_t i4bkqfilter;
89 PDEVSTATIC void i4bkqfilt_detach(struct knote *);
90 PDEVSTATIC int i4bkqfilt_read(struct knote *, long);
91 PDEVSTATIC int i4bkqfilt_write(struct knote *, long);
93 static struct dev_ops i4b_ops = {
99 .d_kqfilter = i4bkqfilter
102 PDEVSTATIC void i4battach(void *);
103 PSEUDO_SET(i4battach, i4b_i4bdrv);
105 /*---------------------------------------------------------------------------*
106 * interface attach routine
107 *---------------------------------------------------------------------------*/
109 i4battach(void *dummy)
111 kprintf("i4b: ISDN call control device attached\n");
113 i4b_rdqueue.ifq_maxlen = IFQ_MAXLEN;
115 make_dev(&i4b_ops, 0, UID_ROOT, GID_WHEEL, 0600, "i4b");
118 /*---------------------------------------------------------------------------*
119 * i4bopen - device driver open routine
120 *---------------------------------------------------------------------------*/
122 i4bopen(struct dev_open_args *ap)
124 cdev_t dev = ap->a_head.a_dev;
132 i4b_l4_daemon_attached();
138 /*---------------------------------------------------------------------------*
139 * i4bclose - device driver close routine
140 *---------------------------------------------------------------------------*/
142 i4bclose(struct dev_close_args *ap)
146 i4b_l4_daemon_detached();
147 i4b_Dcleanifq(&i4b_rdqueue);
152 /*---------------------------------------------------------------------------*
153 * i4bread - device driver read routine
154 *---------------------------------------------------------------------------*/
156 i4bread(struct dev_read_args *ap)
158 cdev_t dev = ap->a_head.a_dev;
166 while(IF_QEMPTY(&i4b_rdqueue))
169 error = tsleep((caddr_t) &i4b_rdqueue, PCATCH, "bird", 0);
176 IF_DEQUEUE(&i4b_rdqueue, m);
181 error = uiomove(m->m_data, m->m_len, ap->a_uio);
191 /*---------------------------------------------------------------------------*
192 * i4bioctl - device driver ioctl routine
193 *---------------------------------------------------------------------------*/
195 i4bioctl(struct dev_ioctl_args *ap)
197 cdev_t dev = ap->a_head.a_dev;
198 caddr_t data = ap->a_data;
207 /* cdid request, reserve cd and return cdid */
212 mir = (msg_cdid_req_t *)data;
214 mir->cdid = cd->cdid;
218 /* connect request, dial out to remote */
220 case I4B_CONNECT_REQ:
222 msg_connect_req_t *mcr;
223 mcr = (msg_connect_req_t *)data; /* setup ptr */
225 if((cd = cd_by_cdid(mcr->cdid)) == NULL)/* get cd */
227 NDBGL4(L4_ERR, "I4B_CONNECT_REQ ioctl, cdid not found!");
232 /* prevent dialling on leased lines */
233 if(ctrl_desc[mcr->controller].protocol == PROTOCOL_D64S)
235 SET_CAUSE_TYPE(cd->cause_in, CAUSET_I4B);
236 SET_CAUSE_VAL(cd->cause_in, CAUSE_I4B_LLDIAL);
237 i4b_l4_disconnect_ind(cd);
242 cd->controller = mcr->controller; /* fill cd */
243 cd->bprot = mcr->bprot;
244 cd->driver = mcr->driver;
245 cd->driver_unit = mcr->driver_unit;
246 cd->cr = get_rand_cr(ctrl_desc[cd->controller].unit);
248 cd->shorthold_data.shorthold_algorithm = mcr->shorthold_data.shorthold_algorithm;
249 cd->shorthold_data.unitlen_time = mcr->shorthold_data.unitlen_time;
250 cd->shorthold_data.idle_time = mcr->shorthold_data.idle_time;
251 cd->shorthold_data.earlyhup_time = mcr->shorthold_data.earlyhup_time;
253 cd->last_aocd_time = 0;
254 if(mcr->unitlen_method == ULEN_METHOD_DYNAMIC)
261 cd->max_idle_time = 0; /* this is outgoing */
263 cd->dir = DIR_OUTGOING;
265 NDBGL4(L4_TIMO, "I4B_CONNECT_REQ times, algorithm=%ld unitlen=%ld idle=%ld earlyhup=%ld",
266 (long)cd->shorthold_data.shorthold_algorithm, (long)cd->shorthold_data.unitlen_time,
267 (long)cd->shorthold_data.idle_time, (long)cd->shorthold_data.earlyhup_time);
269 strcpy(cd->dst_telno, mcr->dst_telno);
270 strcpy(cd->src_telno, mcr->src_telno);
272 if(mcr->keypad[0] != '\0')
273 strcpy(cd->keypad, mcr->keypad);
275 cd->keypad[0] = '\0';
277 cd->display[0] = '\0';
279 SET_CAUSE_TYPE(cd->cause_in, CAUSET_I4B);
280 SET_CAUSE_VAL(cd->cause_in, CAUSE_I4B_NORMAL);
286 if(ctrl_desc[mcr->controller].bch_state[mcr->channel] != BCH_ST_FREE)
287 SET_CAUSE_VAL(cd->cause_in, CAUSE_I4B_NOCHAN);
294 i < ctrl_desc[mcr->controller].nbch &&
295 ctrl_desc[mcr->controller].bch_state[i] != BCH_ST_FREE;
297 if (i == ctrl_desc[mcr->controller].nbch)
298 SET_CAUSE_VAL(cd->cause_in, CAUSE_I4B_NOCHAN);
299 /* else mcr->channel = i; XXX */
304 SET_CAUSE_VAL(cd->cause_in, CAUSE_I4B_NOCHAN);
308 cd->channelid = mcr->channel;
310 cd->isdntxdelay = mcr->txdelay;
312 /* check whether we have a pointer. Seems like */
313 /* this should be adequate. GJ 19.09.97 */
314 if(ctrl_desc[cd->controller].N_CONNECT_REQUEST == NULL)
315 /*XXX*/ SET_CAUSE_VAL(cd->cause_in, CAUSE_I4B_NOCHAN);
317 if((GET_CAUSE_VAL(cd->cause_in)) != CAUSE_I4B_NORMAL)
319 i4b_l4_disconnect_ind(cd);
324 (*ctrl_desc[cd->controller].N_CONNECT_REQUEST)(mcr->cdid);
329 /* connect response, accept/reject/ignore incoming call */
331 case I4B_CONNECT_RESP:
333 msg_connect_resp_t *mcrsp;
335 mcrsp = (msg_connect_resp_t *)data;
337 if((cd = cd_by_cdid(mcrsp->cdid)) == NULL)/* get cd */
339 NDBGL4(L4_ERR, "I4B_CONNECT_RESP ioctl, cdid not found!");
346 cd->driver = mcrsp->driver;
347 cd->driver_unit = mcrsp->driver_unit;
348 cd->max_idle_time = mcrsp->max_idle_time;
350 cd->shorthold_data.shorthold_algorithm = SHA_FIXU;
351 cd->shorthold_data.unitlen_time = 0; /* this is incoming */
352 cd->shorthold_data.idle_time = 0;
353 cd->shorthold_data.earlyhup_time = 0;
355 cd->isdntxdelay = mcrsp->txdelay;
357 NDBGL4(L4_TIMO, "I4B_CONNECT_RESP max_idle_time set to %ld seconds", (long)cd->max_idle_time);
359 (*ctrl_desc[cd->controller].N_CONNECT_RESPONSE)(mcrsp->cdid, mcrsp->response, mcrsp->cause);
363 /* disconnect request, actively terminate connection */
365 case I4B_DISCONNECT_REQ:
367 msg_discon_req_t *mdr;
369 mdr = (msg_discon_req_t *)data;
371 if((cd = cd_by_cdid(mdr->cdid)) == NULL)/* get cd */
373 NDBGL4(L4_ERR, "I4B_DISCONNECT_REQ ioctl, cdid not found!");
378 /* preset causes with our cause */
379 cd->cause_in = cd->cause_out = mdr->cause;
381 (*ctrl_desc[cd->controller].N_DISCONNECT_REQUEST)(mdr->cdid, mdr->cause);
385 /* controller info request */
387 case I4B_CTRL_INFO_REQ:
389 msg_ctrl_info_req_t *mcir;
391 mcir = (msg_ctrl_info_req_t *)data;
392 mcir->ncontroller = nctrl;
394 if(mcir->controller > nctrl)
396 mcir->ctrl_type = -1;
397 mcir->card_type = -1;
402 ctrl_desc[mcir->controller].ctrl_type;
404 ctrl_desc[mcir->controller].card_type;
406 ctrl_desc[mcir->controller].nbch;
408 if(ctrl_desc[mcir->controller].ctrl_type == CTRL_PASSIVE)
409 mcir->tei = ctrl_desc[mcir->controller].tei;
418 case I4B_DIALOUT_RESP:
420 drvr_link_t *dlt = NULL;
421 msg_dialout_resp_t *mdrsp;
423 mdrsp = (msg_dialout_resp_t *)data;
425 switch(mdrsp->driver)
429 dlt = ipr_ret_linktab(mdrsp->driver_unit);
435 dlt = i4bisppp_ret_linktab(mdrsp->driver_unit);
441 dlt = tel_ret_linktab(mdrsp->driver_unit);
447 dlt = ing_ret_linktab(mdrsp->driver_unit);
453 (*dlt->dial_response)(mdrsp->driver_unit, mdrsp->stat, mdrsp->cause);
457 /* update timeout value */
459 case I4B_TIMEOUT_UPD:
461 msg_timeout_upd_t *mtu;
463 mtu = (msg_timeout_upd_t *)data;
465 NDBGL4(L4_TIMO, "I4B_TIMEOUT_UPD ioctl, alg %d, unit %d, idle %d, early %d!",
466 mtu->shorthold_data.shorthold_algorithm, mtu->shorthold_data.unitlen_time,
467 mtu->shorthold_data.idle_time, mtu->shorthold_data.earlyhup_time);
469 if((cd = cd_by_cdid(mtu->cdid)) == NULL)/* get cd */
471 NDBGL4(L4_ERR, "I4B_TIMEOUT_UPD ioctl, cdid not found!");
476 switch( mtu->shorthold_data.shorthold_algorithm )
480 * For this algorithm unitlen_time,
481 * idle_time and earlyhup_time are used.
484 if(!(mtu->shorthold_data.unitlen_time >= 0 &&
485 mtu->shorthold_data.idle_time >= 0 &&
486 mtu->shorthold_data.earlyhup_time >= 0))
488 NDBGL4(L4_ERR, "I4B_TIMEOUT_UPD ioctl, invalid args for fix unit algorithm!");
495 * For this algorithm unitlen_time and
496 * idle_time are used. both must be
497 * positive integers. earlyhup_time is
498 * not used and must be 0.
501 if(!(mtu->shorthold_data.unitlen_time > 0 &&
502 mtu->shorthold_data.idle_time >= 0 &&
503 mtu->shorthold_data.earlyhup_time == 0))
505 NDBGL4(L4_ERR, "I4B_TIMEOUT_UPD ioctl, invalid args for var unit algorithm!");
511 NDBGL4(L4_ERR, "I4B_TIMEOUT_UPD ioctl, invalid algorithm!");
517 * any error set above requires us to break
518 * out of the outer switch
524 cd->shorthold_data.shorthold_algorithm = mtu->shorthold_data.shorthold_algorithm;
525 cd->shorthold_data.unitlen_time = mtu->shorthold_data.unitlen_time;
526 cd->shorthold_data.idle_time = mtu->shorthold_data.idle_time;
527 cd->shorthold_data.earlyhup_time = mtu->shorthold_data.earlyhup_time;
532 /* soft enable/disable interface */
536 msg_updown_ind_t *mui;
538 mui = (msg_updown_ind_t *)data;
541 if(mui->driver == BDRV_IPR)
544 dlt = ipr_ret_linktab(mui->driver_unit);
545 (*dlt->updown_ind)(mui->driver_unit, mui->updown);
551 /* send ALERT request */
555 msg_alert_req_t *mar;
557 mar = (msg_alert_req_t *)data;
559 if((cd = cd_by_cdid(mar->cdid)) == NULL)
561 NDBGL4(L4_ERR, "I4B_ALERT_REQ ioctl, cdid not found!");
568 (*ctrl_desc[cd->controller].N_ALERT_REQUEST)(mar->cdid);
573 /* version/release number request */
579 mvr = (msg_vr_req_t *)data;
581 mvr->version = VERSION;
587 /* set D-channel protocol for a controller */
593 mpi = (msg_prot_ind_t *)data;
595 ctrl_desc[mpi->controller].protocol = mpi->protocol;
600 /* Download request */
602 case I4B_CTRL_DOWNLOAD:
604 struct isdn_dr_prot *prots = NULL, *prots2 = NULL;
605 struct isdn_download_request *r =
606 (struct isdn_download_request*)data;
609 if (r->controller < 0 || r->controller >= nctrl)
615 if(!ctrl_desc[r->controller].N_DOWNLOAD)
621 prots = kmalloc(r->numprotos * sizeof(struct isdn_dr_prot),
624 prots2 = kmalloc(r->numprotos * sizeof(struct isdn_dr_prot),
627 copyin(r->protocols, prots, r->numprotos * sizeof(struct isdn_dr_prot));
629 for(i = 0; i < r->numprotos; i++)
631 prots2[i].microcode = kmalloc(prots[i].bytecount, M_DEVBUF, M_WAITOK);
632 copyin(prots[i].microcode, prots2[i].microcode, prots[i].bytecount);
633 prots2[i].bytecount = prots[i].bytecount;
636 error = ctrl_desc[r->controller].N_DOWNLOAD(
637 ctrl_desc[r->controller].unit,
638 r->numprotos, prots2);
643 for(i = 0; i < r->numprotos; i++)
645 if(prots2[i].microcode)
647 kfree(prots2[i].microcode, M_DEVBUF);
650 kfree(prots2, M_DEVBUF);
655 kfree(prots, M_DEVBUF);
660 /* Diagnostic request */
662 case I4B_ACTIVE_DIAGNOSTIC:
664 struct isdn_diagnostic_request req, *r =
665 (struct isdn_diagnostic_request*)data;
667 req.in_param = req.out_param = NULL;
668 if (r->controller < 0 || r->controller >= nctrl)
674 if(!ctrl_desc[r->controller].N_DIAGNOSTICS)
680 memcpy(&req, r, sizeof(req));
684 /* XXX arbitrary limit */
685 if (req.in_param_len >
686 I4B_ACTIVE_DIAGNOSTIC_MAXPARAMLEN) {
691 req.in_param = kmalloc(r->in_param_len, M_DEVBUF, M_WAITOK);
693 error = copyin(r->in_param, req.in_param, req.in_param_len);
698 if(req.out_param_len)
699 req.out_param = kmalloc(r->out_param_len, M_DEVBUF, M_WAITOK);
701 error = ctrl_desc[r->controller].N_DIAGNOSTICS(r->controller, &req);
703 if(!error && req.out_param_len)
704 error = copyout(req.out_param, r->out_param, req.out_param_len);
708 kfree(req.in_param, M_DEVBUF);
711 kfree(req.out_param, M_DEVBUF);
726 /*---------------------------------------------------------------------------*
727 * i4bkqfilter - device driver poll routine
728 *---------------------------------------------------------------------------*/
729 static struct filterops i4bkqfiltops_read =
730 { FILTEROP_ISFD, NULL, i4bkqfilt_detach, i4bkqfilt_read };
731 static struct filterops i4bkqfiltops_write =
732 { FILTEROP_ISFD, NULL, i4bkqfilt_detach, i4bkqfilt_write };
735 i4bkqfilter(struct dev_kqfilter_args *ap)
737 cdev_t dev = ap->a_head.a_dev;
738 struct knote *kn = ap->a_kn;
746 switch (kn->kn_filter) {
748 kn->kn_fop = &i4bkqfiltops_read;
751 kn->kn_fop = &i4bkqfiltops_write;
754 ap->a_result = EOPNOTSUPP;
758 klist = &kq_rd_info.ki_note;
759 knote_insert(klist, kn);
765 i4bkqfilt_detach(struct knote *kn)
767 struct klist *klist = &kq_rd_info.ki_note;
769 knote_remove(klist, kn);
773 i4bkqfilt_read(struct knote *kn, long hint)
778 if (!IF_QEMPTY(&i4b_rdqueue))
786 i4bkqfilt_write(struct knote *kn, long hint)
791 /*---------------------------------------------------------------------------*
792 * i4bputqueue - put message into queue to userland
793 *---------------------------------------------------------------------------*/
795 i4bputqueue(struct mbuf *m)
805 if(IF_QFULL(&i4b_rdqueue))
808 IF_DEQUEUE(&i4b_rdqueue, m1);
810 NDBGL4(L4_ERR, "ERROR, queue full, removing entry!");
813 IF_ENQUEUE(&i4b_rdqueue, m);
820 wakeup((caddr_t) &i4b_rdqueue);
823 KNOTE(&kq_rd_info.ki_note, 0);
826 /*---------------------------------------------------------------------------*
827 * i4bputqueue_hipri - put message into front of queue to userland
828 *---------------------------------------------------------------------------*/
830 i4bputqueue_hipri(struct mbuf *m)
840 if(IF_QFULL(&i4b_rdqueue))
843 IF_DEQUEUE(&i4b_rdqueue, m1);
845 NDBGL4(L4_ERR, "ERROR, queue full, removing entry!");
848 IF_PREPEND(&i4b_rdqueue, m);
855 wakeup((caddr_t) &i4b_rdqueue);
858 KNOTE(&kq_rd_info.ki_note, 0);
861 #endif /* NI4B > 0 */