2 * Copyright (c) 2001 Cubical Solutions Ltd. 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
26 /* capi/capi_l4if.c The CAPI i4b L4/device interface.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/systm.h>
36 #include <sys/socket.h>
39 #include <i4b/include/i4b_debug.h>
40 #include <i4b/include/i4b_ioctl.h>
41 #include <i4b/include/i4b_cause.h>
43 #include <i4b/include/i4b_l3l4.h>
44 #include <i4b/include/i4b_mbuf.h>
45 #include <i4b/include/i4b_global.h>
47 #include <i4b/layer4/i4b_l4.h>
49 #include <i4b/capi/capi.h>
50 #include <i4b/capi/capi_msgs.h>
52 static void n_connect_request(u_int cdid);
53 static void n_connect_response(u_int cdid, int response, int cause);
54 static void n_disconnect_request(u_int cdid, int cause);
55 static void n_alert_request(u_int cdid);
56 static void n_mgmt_command(int unit, int cmd, void *parm);
57 static int n_download(int unit, int, struct isdn_dr_prot *);
59 capi_softc_t *capi_sc[MAX_CONTROLLERS] = { NULL, };
63 // i4b_capi_{ret,set}_linktab
66 // i4b_capi_bch_config
67 // Called by i4b driver to flush + {en,dis}able a channel.
69 // i4b_capi_bch_start_tx
70 // Called by i4b driver to transmit a queued mbuf.
73 // Called by i4b driver to obtain statistics information.
77 i4b_capi_ret_linktab(int unit, int channel)
79 capi_softc_t *sc = capi_sc[unit];
80 return &sc->sc_bchan[channel].capi_isdn_linktab;
84 i4b_capi_set_linktab(int unit, int channel, drvr_link_t *dlt)
86 capi_softc_t *sc = capi_sc[unit];
87 sc->sc_bchan[channel].capi_drvr_linktab = dlt;
91 i4b_capi_bch_config(int unit, int chan, int bprot, int activate)
93 capi_softc_t *sc = capi_sc[unit];
95 i4b_Bcleanifq(&sc->sc_bchan[chan].tx_queue);
96 sc->sc_bchan[chan].tx_queue.ifq_maxlen = IFQ_MAXLEN;
97 sc->sc_bchan[chan].txcount = 0;
99 /* The telephony drivers use rx_queue for receive. */
101 i4b_Bcleanifq(&sc->sc_bchan[chan].rx_queue);
102 sc->sc_bchan[chan].rx_queue.ifq_maxlen = IFQ_MAXLEN;
103 sc->sc_bchan[chan].rxcount = 0;
105 /* HDLC frames are put to in_mbuf */
107 i4b_Bfreembuf(sc->sc_bchan[chan].in_mbuf);
108 sc->sc_bchan[chan].in_mbuf = NULL;
110 /* Because of the difference, we need to remember the protocol. */
112 sc->sc_bchan[chan].bprot = bprot;
113 sc->sc_bchan[chan].busy = 0;
117 i4b_capi_bch_start_tx(int unit, int chan)
119 capi_softc_t *sc = capi_sc[unit];
124 if (sc->sc_bchan[chan].state != B_CONNECTED) {
126 printf("capi%d: start_tx on unconnected channel\n", sc->sc_unit);
130 if (sc->sc_bchan[chan].busy) {
135 capi_start_tx(sc, chan);
141 i4b_capi_bch_stat(int unit, int chan, bchan_statistics_t *bsp)
143 capi_softc_t *sc = capi_sc[unit];
146 bsp->outbytes = sc->sc_bchan[chan].txcount;
147 bsp->inbytes = sc->sc_bchan[chan].rxcount;
149 sc->sc_bchan[chan].txcount = 0;
150 sc->sc_bchan[chan].rxcount = 0;
155 int capi_start_tx(capi_softc_t *sc, int chan)
160 _IF_DEQUEUE(&sc->sc_bchan[chan].tx_queue, m_b3);
162 struct mbuf *m = m_b3->m_next;
164 sc->sc_bchan[chan].txcount += m_b3->m_len;
165 capi_data_b3_req(sc, chan, m_b3);
171 if (sc->sc_bchan[chan].capi_drvr_linktab) {
172 /* Notify i4b driver of activity, and if the queue is drained. */
175 (*sc->sc_bchan[chan].capi_drvr_linktab->bch_activity)(
176 sc->sc_bchan[chan].capi_drvr_linktab->unit, ACT_TX);
178 if (IF_QEMPTY(&sc->sc_bchan[chan].tx_queue))
179 (*sc->sc_bchan[chan].capi_drvr_linktab->bch_tx_queue_empty)(
180 sc->sc_bchan[chan].capi_drvr_linktab->unit);
188 // Called by a link layer driver at boot time.
192 capi_ll_attach(capi_softc_t *sc)
196 if (ncapi == (sizeof(capi_sc) / sizeof(capi_sc[0]))) {
197 printf("capi%d: too many units, increase MAX_CONTROLLERS\n", ncapi);
201 /* Unit type and subtype; sc is partly filled by ll driver */
203 ctrl_desc[nctrl].unit = ncapi;
204 ctrl_desc[nctrl].ctrl_type = CTRL_CAPI;
205 ctrl_desc[nctrl].card_type = sc->card_type;
209 ctrl_types[CTRL_CAPI].get_linktab = i4b_capi_ret_linktab;
210 ctrl_types[CTRL_CAPI].set_linktab = i4b_capi_set_linktab;
212 ctrl_desc[nctrl].N_CONNECT_REQUEST = n_connect_request;
213 ctrl_desc[nctrl].N_CONNECT_RESPONSE = n_connect_response;
214 ctrl_desc[nctrl].N_DISCONNECT_REQUEST = n_disconnect_request;
215 ctrl_desc[nctrl].N_ALERT_REQUEST = n_alert_request;
216 ctrl_desc[nctrl].N_DOWNLOAD = n_download;
217 ctrl_desc[nctrl].N_DIAGNOSTICS = NULL; /* XXX todo */
218 ctrl_desc[nctrl].N_MGMT_COMMAND = n_mgmt_command;
222 sc->sc_enabled = FALSE;
223 sc->sc_state = C_DOWN;
226 ctrl_desc[nctrl].dl_est = DL_DOWN;
227 ctrl_desc[nctrl].nbch = sc->sc_nbch;
229 for (i = 0; i < sc->sc_nbch; i++) {
230 ctrl_desc[nctrl].bch_state[i] = BCH_ST_FREE;
231 sc->sc_bchan[i].ncci = INVALID;
232 sc->sc_bchan[i].msgid = 0;
233 sc->sc_bchan[i].busy = 0;
234 sc->sc_bchan[i].state = B_FREE;
236 memset(&sc->sc_bchan[i].tx_queue, 0, sizeof(struct ifqueue));
237 memset(&sc->sc_bchan[i].rx_queue, 0, sizeof(struct ifqueue));
238 sc->sc_bchan[i].tx_queue.ifq_maxlen = IFQ_MAXLEN;
239 sc->sc_bchan[i].rx_queue.ifq_maxlen = IFQ_MAXLEN;
241 if(!mtx_initialized(&sc->sc_bchan[i].tx_queue.ifq_mtx))
242 mtx_init(&sc->sc_bchan[i].tx_queue.ifq_mtx, "i4b_capi_tx", NULL, MTX_DEF);
243 if(!mtx_initialized(&sc->sc_bchan[i].rx_queue.ifq_mtx))
244 mtx_init(&sc->sc_bchan[i].rx_queue.ifq_mtx, "i4b_capi_rx", NULL, MTX_DEF);
246 sc->sc_bchan[i].txcount = 0;
247 sc->sc_bchan[i].rxcount = 0;
249 sc->sc_bchan[i].cdid = CDID_UNUSED;
250 sc->sc_bchan[i].bprot = BPROT_NONE;
251 sc->sc_bchan[i].in_mbuf = NULL;
253 sc->sc_bchan[i].capi_drvr_linktab = NULL;
255 sc->sc_bchan[i].capi_isdn_linktab.unit = ncapi;
256 sc->sc_bchan[i].capi_isdn_linktab.channel = i;
257 sc->sc_bchan[i].capi_isdn_linktab.bch_config = i4b_capi_bch_config;
258 sc->sc_bchan[i].capi_isdn_linktab.bch_tx_start = i4b_capi_bch_start_tx;
259 sc->sc_bchan[i].capi_isdn_linktab.bch_stat = i4b_capi_bch_stat;
260 sc->sc_bchan[i].capi_isdn_linktab.tx_queue = &sc->sc_bchan[i].tx_queue;
261 sc->sc_bchan[i].capi_isdn_linktab.rx_queue = &sc->sc_bchan[i].rx_queue;
262 sc->sc_bchan[i].capi_isdn_linktab.rx_mbuf = &sc->sc_bchan[i].in_mbuf;
265 ctrl_desc[nctrl].tei = -1;
267 /* Up the controller index and store the softc */
270 capi_sc[ncapi++] = sc;
271 sc->ctrl_unit = nctrl++;
273 printf("capi%d: card type %d attached\n", sc->sc_unit, sc->card_type);
280 // i4b L4 management command.
284 n_mgmt_command(int unit, int op, void *arg)
286 capi_softc_t *sc = capi_sc[unit];
288 printf("capi%d: mgmt command %d\n", sc->sc_unit, op);
292 sc->sc_enabled = TRUE;
296 sc->sc_enabled = FALSE;
309 // i4b L4 wants to connect. We assign a B channel to the call,
310 // send a CAPI_CONNECT_REQ, and set the channel to B_CONNECT_CONF.
314 n_connect_request(u_int cdid)
316 call_desc_t *cd = cd_by_cdid(cdid);
321 printf("capi?: invalid cdid %d\n", cdid);
325 sc = capi_sc[ctrl_desc[cd->controller].unit];
330 if ((bch < 0) || (bch >= sc->sc_nbch))
331 for (bch = 0; bch < sc->sc_nbch; bch++)
332 if (sc->sc_bchan[bch].state == B_FREE)
335 if (bch == sc->sc_nbch) {
337 printf("capi%d: no free B channel\n", sc->sc_unit);
343 capi_connect_req(sc, cd);
348 // n_connect_response
349 // i4b L4 answers a call. We send a CONNECT_RESP with the proper
350 // Reject code, and set the channel to B_CONNECT_B3_IND or B_FREE,
351 // depending whether we answer or not.
355 n_connect_response(u_int cdid, int response, int cause)
357 call_desc_t *cd = cd_by_cdid(cdid);
362 printf("capi?: invalid cdid %d\n", cdid);
366 sc = capi_sc[ctrl_desc[cd->controller].unit];
371 cd->response = response;
372 cd->cause_out = cause;
375 capi_connect_resp(sc, cd);
380 // n_disconnect_request
381 // i4b L4 wants to disconnect. We send a DISCONNECT_REQ and
382 // set the channel to B_DISCONNECT_CONF.
386 n_disconnect_request(u_int cdid, int cause)
388 call_desc_t *cd = cd_by_cdid(cdid);
393 printf("capi?: invalid cdid %d\n", cdid);
397 sc = capi_sc[ctrl_desc[cd->controller].unit];
400 cd->cause_out = cause;
403 capi_disconnect_req(sc, cd);
409 // i4b L4 wants to alert an incoming call. We send ALERT_REQ.
413 n_alert_request(u_int cdid)
415 call_desc_t *cd = cd_by_cdid(cdid);
420 printf("capi?: invalid cdid %d\n", cdid);
424 sc = capi_sc[ctrl_desc[cd->controller].unit];
427 capi_alert_req(sc, cd);
433 // L4 -> firmware download
437 n_download(int unit, int numprotos, struct isdn_dr_prot *protocols)
439 capi_softc_t *sc = capi_sc[unit];
442 (*capi_sc[unit]->load)(sc, protocols[0].bytecount,
443 protocols[0].microcode);