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 $
31 * $DragonFly: src/sys/net/i4b/layer4/i4b_i4bdrv.c,v 1.20 2008/01/06 16:55:52 swildner Exp $
33 * last edit-date: [Sat Aug 11 18:08:10 2001]
35 *---------------------------------------------------------------------------*/
38 #include "use_i4bipr.h"
39 #include "use_i4btel.h"
42 #error "only 1 (one) i4b device possible!"
47 #include <sys/param.h>
49 #include <sys/malloc.h>
51 #include <sys/kernel.h>
52 #include <sys/systm.h>
54 #include <sys/device.h>
56 #include <sys/socket.h>
57 #include <sys/thread2.h>
61 #include "use_i4bing.h"
62 #include "use_i4bisppp.h"
64 #include <net/i4b/include/machine/i4b_debug.h>
65 #include <net/i4b/include/machine/i4b_ioctl.h>
66 #include <net/i4b/include/machine/i4b_cause.h>
68 #include "../include/i4b_l3l4.h"
69 #include "../include/i4b_mbuf.h"
70 #include "../include/i4b_global.h"
74 #include <sys/event.h>
76 struct kqinfo kq_rd_info;
78 static struct ifqueue i4b_rdqueue;
79 static int openflag = 0;
80 static int readflag = 0;
82 #define PDEVSTATIC static
84 PDEVSTATIC d_open_t i4bopen;
85 PDEVSTATIC d_close_t i4bclose;
86 PDEVSTATIC d_read_t i4bread;
87 PDEVSTATIC d_ioctl_t i4bioctl;
88 PDEVSTATIC d_kqfilter_t i4bkqfilter;
90 PDEVSTATIC void i4bkqfilt_detach(struct knote *);
91 PDEVSTATIC int i4bkqfilt_read(struct knote *, long);
92 PDEVSTATIC int i4bkqfilt_write(struct knote *, long);
96 static struct dev_ops i4b_ops = {
97 { "i4b", CDEV_MAJOR, 0 },
102 .d_kqfilter = i4bkqfilter
105 PDEVSTATIC void i4battach(void *);
106 PSEUDO_SET(i4battach, i4b_i4bdrv);
108 /*---------------------------------------------------------------------------*
109 * interface attach routine
110 *---------------------------------------------------------------------------*/
112 i4battach(void *dummy)
114 kprintf("i4b: ISDN call control device attached\n");
116 i4b_rdqueue.ifq_maxlen = IFQ_MAXLEN;
118 make_dev(&i4b_ops, 0, UID_ROOT, GID_WHEEL, 0600, "i4b");
121 /*---------------------------------------------------------------------------*
122 * i4bopen - device driver open routine
123 *---------------------------------------------------------------------------*/
125 i4bopen(struct dev_open_args *ap)
127 cdev_t dev = ap->a_head.a_dev;
135 i4b_l4_daemon_attached();
141 /*---------------------------------------------------------------------------*
142 * i4bclose - device driver close routine
143 *---------------------------------------------------------------------------*/
145 i4bclose(struct dev_close_args *ap)
149 i4b_l4_daemon_detached();
150 i4b_Dcleanifq(&i4b_rdqueue);
155 /*---------------------------------------------------------------------------*
156 * i4bread - device driver read routine
157 *---------------------------------------------------------------------------*/
159 i4bread(struct dev_read_args *ap)
161 cdev_t dev = ap->a_head.a_dev;
169 while(IF_QEMPTY(&i4b_rdqueue))
172 error = tsleep((caddr_t) &i4b_rdqueue, PCATCH, "bird", 0);
179 IF_DEQUEUE(&i4b_rdqueue, m);
184 error = uiomove(m->m_data, m->m_len, ap->a_uio);
194 /*---------------------------------------------------------------------------*
195 * i4bioctl - device driver ioctl routine
196 *---------------------------------------------------------------------------*/
198 i4bioctl(struct dev_ioctl_args *ap)
200 cdev_t dev = ap->a_head.a_dev;
201 caddr_t data = ap->a_data;
210 /* cdid request, reserve cd and return cdid */
215 mir = (msg_cdid_req_t *)data;
217 mir->cdid = cd->cdid;
221 /* connect request, dial out to remote */
223 case I4B_CONNECT_REQ:
225 msg_connect_req_t *mcr;
226 mcr = (msg_connect_req_t *)data; /* setup ptr */
228 if((cd = cd_by_cdid(mcr->cdid)) == NULL)/* get cd */
230 NDBGL4(L4_ERR, "I4B_CONNECT_REQ ioctl, cdid not found!");
235 /* prevent dialling on leased lines */
236 if(ctrl_desc[mcr->controller].protocol == PROTOCOL_D64S)
238 SET_CAUSE_TYPE(cd->cause_in, CAUSET_I4B);
239 SET_CAUSE_VAL(cd->cause_in, CAUSE_I4B_LLDIAL);
240 i4b_l4_disconnect_ind(cd);
245 cd->controller = mcr->controller; /* fill cd */
246 cd->bprot = mcr->bprot;
247 cd->driver = mcr->driver;
248 cd->driver_unit = mcr->driver_unit;
249 cd->cr = get_rand_cr(ctrl_desc[cd->controller].unit);
251 cd->shorthold_data.shorthold_algorithm = mcr->shorthold_data.shorthold_algorithm;
252 cd->shorthold_data.unitlen_time = mcr->shorthold_data.unitlen_time;
253 cd->shorthold_data.idle_time = mcr->shorthold_data.idle_time;
254 cd->shorthold_data.earlyhup_time = mcr->shorthold_data.earlyhup_time;
256 cd->last_aocd_time = 0;
257 if(mcr->unitlen_method == ULEN_METHOD_DYNAMIC)
264 cd->max_idle_time = 0; /* this is outgoing */
266 cd->dir = DIR_OUTGOING;
268 NDBGL4(L4_TIMO, "I4B_CONNECT_REQ times, algorithm=%ld unitlen=%ld idle=%ld earlyhup=%ld",
269 (long)cd->shorthold_data.shorthold_algorithm, (long)cd->shorthold_data.unitlen_time,
270 (long)cd->shorthold_data.idle_time, (long)cd->shorthold_data.earlyhup_time);
272 strcpy(cd->dst_telno, mcr->dst_telno);
273 strcpy(cd->src_telno, mcr->src_telno);
275 if(mcr->keypad[0] != '\0')
276 strcpy(cd->keypad, mcr->keypad);
278 cd->keypad[0] = '\0';
280 cd->display[0] = '\0';
282 SET_CAUSE_TYPE(cd->cause_in, CAUSET_I4B);
283 SET_CAUSE_VAL(cd->cause_in, CAUSE_I4B_NORMAL);
289 if(ctrl_desc[mcr->controller].bch_state[mcr->channel] != BCH_ST_FREE)
290 SET_CAUSE_VAL(cd->cause_in, CAUSE_I4B_NOCHAN);
297 i < ctrl_desc[mcr->controller].nbch &&
298 ctrl_desc[mcr->controller].bch_state[i] != BCH_ST_FREE;
300 if (i == ctrl_desc[mcr->controller].nbch)
301 SET_CAUSE_VAL(cd->cause_in, CAUSE_I4B_NOCHAN);
302 /* else mcr->channel = i; XXX */
307 SET_CAUSE_VAL(cd->cause_in, CAUSE_I4B_NOCHAN);
311 cd->channelid = mcr->channel;
313 cd->isdntxdelay = mcr->txdelay;
315 /* check whether we have a pointer. Seems like */
316 /* this should be adequate. GJ 19.09.97 */
317 if(ctrl_desc[cd->controller].N_CONNECT_REQUEST == NULL)
318 /*XXX*/ SET_CAUSE_VAL(cd->cause_in, CAUSE_I4B_NOCHAN);
320 if((GET_CAUSE_VAL(cd->cause_in)) != CAUSE_I4B_NORMAL)
322 i4b_l4_disconnect_ind(cd);
327 (*ctrl_desc[cd->controller].N_CONNECT_REQUEST)(mcr->cdid);
332 /* connect response, accept/reject/ignore incoming call */
334 case I4B_CONNECT_RESP:
336 msg_connect_resp_t *mcrsp;
338 mcrsp = (msg_connect_resp_t *)data;
340 if((cd = cd_by_cdid(mcrsp->cdid)) == NULL)/* get cd */
342 NDBGL4(L4_ERR, "I4B_CONNECT_RESP ioctl, cdid not found!");
349 cd->driver = mcrsp->driver;
350 cd->driver_unit = mcrsp->driver_unit;
351 cd->max_idle_time = mcrsp->max_idle_time;
353 cd->shorthold_data.shorthold_algorithm = SHA_FIXU;
354 cd->shorthold_data.unitlen_time = 0; /* this is incoming */
355 cd->shorthold_data.idle_time = 0;
356 cd->shorthold_data.earlyhup_time = 0;
358 cd->isdntxdelay = mcrsp->txdelay;
360 NDBGL4(L4_TIMO, "I4B_CONNECT_RESP max_idle_time set to %ld seconds", (long)cd->max_idle_time);
362 (*ctrl_desc[cd->controller].N_CONNECT_RESPONSE)(mcrsp->cdid, mcrsp->response, mcrsp->cause);
366 /* disconnect request, actively terminate connection */
368 case I4B_DISCONNECT_REQ:
370 msg_discon_req_t *mdr;
372 mdr = (msg_discon_req_t *)data;
374 if((cd = cd_by_cdid(mdr->cdid)) == NULL)/* get cd */
376 NDBGL4(L4_ERR, "I4B_DISCONNECT_REQ ioctl, cdid not found!");
381 /* preset causes with our cause */
382 cd->cause_in = cd->cause_out = mdr->cause;
384 (*ctrl_desc[cd->controller].N_DISCONNECT_REQUEST)(mdr->cdid, mdr->cause);
388 /* controller info request */
390 case I4B_CTRL_INFO_REQ:
392 msg_ctrl_info_req_t *mcir;
394 mcir = (msg_ctrl_info_req_t *)data;
395 mcir->ncontroller = nctrl;
397 if(mcir->controller > nctrl)
399 mcir->ctrl_type = -1;
400 mcir->card_type = -1;
405 ctrl_desc[mcir->controller].ctrl_type;
407 ctrl_desc[mcir->controller].card_type;
409 ctrl_desc[mcir->controller].nbch;
411 if(ctrl_desc[mcir->controller].ctrl_type == CTRL_PASSIVE)
412 mcir->tei = ctrl_desc[mcir->controller].tei;
421 case I4B_DIALOUT_RESP:
423 drvr_link_t *dlt = NULL;
424 msg_dialout_resp_t *mdrsp;
426 mdrsp = (msg_dialout_resp_t *)data;
428 switch(mdrsp->driver)
432 dlt = ipr_ret_linktab(mdrsp->driver_unit);
438 dlt = i4bisppp_ret_linktab(mdrsp->driver_unit);
444 dlt = tel_ret_linktab(mdrsp->driver_unit);
450 dlt = ing_ret_linktab(mdrsp->driver_unit);
456 (*dlt->dial_response)(mdrsp->driver_unit, mdrsp->stat, mdrsp->cause);
460 /* update timeout value */
462 case I4B_TIMEOUT_UPD:
464 msg_timeout_upd_t *mtu;
466 mtu = (msg_timeout_upd_t *)data;
468 NDBGL4(L4_TIMO, "I4B_TIMEOUT_UPD ioctl, alg %d, unit %d, idle %d, early %d!",
469 mtu->shorthold_data.shorthold_algorithm, mtu->shorthold_data.unitlen_time,
470 mtu->shorthold_data.idle_time, mtu->shorthold_data.earlyhup_time);
472 if((cd = cd_by_cdid(mtu->cdid)) == NULL)/* get cd */
474 NDBGL4(L4_ERR, "I4B_TIMEOUT_UPD ioctl, cdid not found!");
479 switch( mtu->shorthold_data.shorthold_algorithm )
483 * For this algorithm unitlen_time,
484 * idle_time and earlyhup_time are used.
487 if(!(mtu->shorthold_data.unitlen_time >= 0 &&
488 mtu->shorthold_data.idle_time >= 0 &&
489 mtu->shorthold_data.earlyhup_time >= 0))
491 NDBGL4(L4_ERR, "I4B_TIMEOUT_UPD ioctl, invalid args for fix unit algorithm!");
498 * For this algorithm unitlen_time and
499 * idle_time are used. both must be
500 * positive integers. earlyhup_time is
501 * not used and must be 0.
504 if(!(mtu->shorthold_data.unitlen_time > 0 &&
505 mtu->shorthold_data.idle_time >= 0 &&
506 mtu->shorthold_data.earlyhup_time == 0))
508 NDBGL4(L4_ERR, "I4B_TIMEOUT_UPD ioctl, invalid args for var unit algorithm!");
514 NDBGL4(L4_ERR, "I4B_TIMEOUT_UPD ioctl, invalid algorithm!");
520 * any error set above requires us to break
521 * out of the outer switch
527 cd->shorthold_data.shorthold_algorithm = mtu->shorthold_data.shorthold_algorithm;
528 cd->shorthold_data.unitlen_time = mtu->shorthold_data.unitlen_time;
529 cd->shorthold_data.idle_time = mtu->shorthold_data.idle_time;
530 cd->shorthold_data.earlyhup_time = mtu->shorthold_data.earlyhup_time;
535 /* soft enable/disable interface */
539 msg_updown_ind_t *mui;
541 mui = (msg_updown_ind_t *)data;
544 if(mui->driver == BDRV_IPR)
547 dlt = ipr_ret_linktab(mui->driver_unit);
548 (*dlt->updown_ind)(mui->driver_unit, mui->updown);
554 /* send ALERT request */
558 msg_alert_req_t *mar;
560 mar = (msg_alert_req_t *)data;
562 if((cd = cd_by_cdid(mar->cdid)) == NULL)
564 NDBGL4(L4_ERR, "I4B_ALERT_REQ ioctl, cdid not found!");
571 (*ctrl_desc[cd->controller].N_ALERT_REQUEST)(mar->cdid);
576 /* version/release number request */
582 mvr = (msg_vr_req_t *)data;
584 mvr->version = VERSION;
590 /* set D-channel protocol for a controller */
596 mpi = (msg_prot_ind_t *)data;
598 ctrl_desc[mpi->controller].protocol = mpi->protocol;
603 /* Download request */
605 case I4B_CTRL_DOWNLOAD:
607 struct isdn_dr_prot *prots = NULL, *prots2 = NULL;
608 struct isdn_download_request *r =
609 (struct isdn_download_request*)data;
612 if (r->controller < 0 || r->controller >= nctrl)
618 if(!ctrl_desc[r->controller].N_DOWNLOAD)
624 prots = kmalloc(r->numprotos * sizeof(struct isdn_dr_prot),
627 prots2 = kmalloc(r->numprotos * sizeof(struct isdn_dr_prot),
630 copyin(r->protocols, prots, r->numprotos * sizeof(struct isdn_dr_prot));
632 for(i = 0; i < r->numprotos; i++)
634 prots2[i].microcode = kmalloc(prots[i].bytecount, M_DEVBUF, M_WAITOK);
635 copyin(prots[i].microcode, prots2[i].microcode, prots[i].bytecount);
636 prots2[i].bytecount = prots[i].bytecount;
639 error = ctrl_desc[r->controller].N_DOWNLOAD(
640 ctrl_desc[r->controller].unit,
641 r->numprotos, prots2);
646 for(i = 0; i < r->numprotos; i++)
648 if(prots2[i].microcode)
650 kfree(prots2[i].microcode, M_DEVBUF);
653 kfree(prots2, M_DEVBUF);
658 kfree(prots, M_DEVBUF);
663 /* Diagnostic request */
665 case I4B_ACTIVE_DIAGNOSTIC:
667 struct isdn_diagnostic_request req, *r =
668 (struct isdn_diagnostic_request*)data;
670 req.in_param = req.out_param = NULL;
671 if (r->controller < 0 || r->controller >= nctrl)
677 if(!ctrl_desc[r->controller].N_DIAGNOSTICS)
683 memcpy(&req, r, sizeof(req));
687 /* XXX arbitrary limit */
688 if (req.in_param_len >
689 I4B_ACTIVE_DIAGNOSTIC_MAXPARAMLEN) {
694 req.in_param = kmalloc(r->in_param_len, M_DEVBUF, M_WAITOK);
696 error = copyin(r->in_param, req.in_param, req.in_param_len);
701 if(req.out_param_len)
702 req.out_param = kmalloc(r->out_param_len, M_DEVBUF, M_WAITOK);
704 error = ctrl_desc[r->controller].N_DIAGNOSTICS(r->controller, &req);
706 if(!error && req.out_param_len)
707 error = copyout(req.out_param, r->out_param, req.out_param_len);
711 kfree(req.in_param, M_DEVBUF);
714 kfree(req.out_param, M_DEVBUF);
729 /*---------------------------------------------------------------------------*
730 * i4bkqfilter - device driver poll routine
731 *---------------------------------------------------------------------------*/
732 static struct filterops i4bkqfiltops_read =
733 { FILTEROP_ISFD, NULL, i4bkqfilt_detach, i4bkqfilt_read };
734 static struct filterops i4bkqfiltops_write =
735 { FILTEROP_ISFD, NULL, i4bkqfilt_detach, i4bkqfilt_write };
738 i4bkqfilter(struct dev_kqfilter_args *ap)
740 cdev_t dev = ap->a_head.a_dev;
741 struct knote *kn = ap->a_kn;
749 switch (kn->kn_filter) {
751 kn->kn_fop = &i4bkqfiltops_read;
754 kn->kn_fop = &i4bkqfiltops_write;
757 ap->a_result = EOPNOTSUPP;
761 klist = &kq_rd_info.ki_note;
762 knote_insert(klist, kn);
768 i4bkqfilt_detach(struct knote *kn)
770 struct klist *klist = &kq_rd_info.ki_note;
772 knote_remove(klist, kn);
776 i4bkqfilt_read(struct knote *kn, long hint)
781 if (!IF_QEMPTY(&i4b_rdqueue))
789 i4bkqfilt_write(struct knote *kn, long hint)
794 /*---------------------------------------------------------------------------*
795 * i4bputqueue - put message into queue to userland
796 *---------------------------------------------------------------------------*/
798 i4bputqueue(struct mbuf *m)
808 if(IF_QFULL(&i4b_rdqueue))
811 IF_DEQUEUE(&i4b_rdqueue, m1);
813 NDBGL4(L4_ERR, "ERROR, queue full, removing entry!");
816 IF_ENQUEUE(&i4b_rdqueue, m);
823 wakeup((caddr_t) &i4b_rdqueue);
826 KNOTE(&kq_rd_info.ki_note, 0);
829 /*---------------------------------------------------------------------------*
830 * i4bputqueue_hipri - put message into front of queue to userland
831 *---------------------------------------------------------------------------*/
833 i4bputqueue_hipri(struct mbuf *m)
843 if(IF_QFULL(&i4b_rdqueue))
846 IF_DEQUEUE(&i4b_rdqueue, m1);
848 NDBGL4(L4_ERR, "ERROR, queue full, removing entry!");
851 IF_PREPEND(&i4b_rdqueue, m);
858 wakeup((caddr_t) &i4b_rdqueue);
861 KNOTE(&kq_rd_info.ki_note, 0);
864 #endif /* NI4B > 0 */