11555cc463eb3ce268f43b77908934eeb45eb327
[dragonfly.git] / sys / net / i4b / capi / capi_l4if.c
1 /*
2  * Copyright (c) 2001 Cubical Solutions Ltd. 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  * capi/capi_l4if.c     The CAPI i4b L4/device interface.
26  *
27  * $FreeBSD: src/sys/i4b/capi/capi_l4if.c,v 1.1.2.1 2001/08/10 14:08:34 obrien Exp $
28  * $DragonFly: src/sys/net/i4b/capi/capi_l4if.c,v 1.9 2006/01/14 11:05:17 swildner Exp $
29  */
30
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/systm.h>
34 #include <sys/mbuf.h>
35 #include <sys/socket.h>
36 #include <sys/thread2.h>
37 #include <net/if.h>
38
39 #include <net/i4b/include/machine/i4b_debug.h>
40 #include <net/i4b/include/machine/i4b_ioctl.h>
41 #include <net/i4b/include/machine/i4b_cause.h>
42
43 #include "../include/i4b_l3l4.h"
44 #include "../include/i4b_mbuf.h"
45 #include "../include/i4b_global.h"
46 #include "../layer4/i4b_l4.h"
47
48 #include "capi.h"
49 #include "capi_msgs.h"
50
51 static void n_connect_request(u_int cdid);
52 static void n_connect_response(u_int cdid, int response, int cause);
53 static void n_disconnect_request(u_int cdid, int cause);
54 static void n_alert_request(u_int cdid);
55 static void n_mgmt_command(int unit, int cmd, void *parm);
56 static int  n_download(int unit, int, struct isdn_dr_prot *);
57
58 capi_softc_t *capi_sc[MAX_CONTROLLERS] = { NULL, };
59 int ncapi = 0;
60
61 /*
62 //  i4b_capi_{ret,set}_linktab
63 //      i4b driver glue.
64 //
65 //  i4b_capi_bch_config
66 //      Called by i4b driver to flush + {en,dis}able a channel.
67 //
68 //  i4b_capi_bch_start_tx
69 //      Called by i4b driver to transmit a queued mbuf.
70 //
71 //  i4b_capi_bch_stat
72 //      Called by i4b driver to obtain statistics information.
73 */
74
75 static isdn_link_t *
76 i4b_capi_ret_linktab(int unit, int channel)
77 {
78     capi_softc_t *sc = capi_sc[unit];
79     return &sc->sc_bchan[channel].capi_isdn_linktab;
80 }
81
82 static void
83 i4b_capi_set_linktab(int unit, int channel, drvr_link_t *dlt)
84 {
85     capi_softc_t *sc = capi_sc[unit];
86     sc->sc_bchan[channel].capi_drvr_linktab = dlt;
87 }
88
89 static void
90 i4b_capi_bch_config(int unit, int chan, int bprot, int activate)
91 {
92     capi_softc_t *sc = capi_sc[unit];
93
94     i4b_Bcleanifq(&sc->sc_bchan[chan].tx_queue);
95     sc->sc_bchan[chan].tx_queue.ifq_maxlen = IFQ_MAXLEN;
96     sc->sc_bchan[chan].txcount = 0;
97
98     /* The telephony drivers use rx_queue for receive. */
99
100     i4b_Bcleanifq(&sc->sc_bchan[chan].rx_queue);
101     sc->sc_bchan[chan].rx_queue.ifq_maxlen = IFQ_MAXLEN;
102     sc->sc_bchan[chan].rxcount = 0;
103
104     /* HDLC frames are put to in_mbuf */
105
106     i4b_Bfreembuf(sc->sc_bchan[chan].in_mbuf);
107     sc->sc_bchan[chan].in_mbuf = NULL;
108
109     /* Because of the difference, we need to remember the protocol. */
110
111     sc->sc_bchan[chan].bprot = bprot;
112     sc->sc_bchan[chan].busy = 0;
113 }
114
115 static void
116 i4b_capi_bch_start_tx(int unit, int chan)
117 {
118     capi_softc_t *sc = capi_sc[unit];
119
120     crit_enter();
121     if (sc->sc_bchan[chan].state != B_CONNECTED) {
122         crit_exit();
123         printf("capi%d: start_tx on unconnected channel\n", sc->sc_unit);
124         return;
125     }
126
127     if (sc->sc_bchan[chan].busy) {
128         crit_exit();
129         return;
130     }
131
132     capi_start_tx(sc, chan);
133
134     crit_exit();
135 }
136
137 static void
138 i4b_capi_bch_stat(int unit, int chan, bchan_statistics_t *bsp)
139 {
140     capi_softc_t *sc = capi_sc[unit];
141
142     crit_enter();
143
144     bsp->outbytes = sc->sc_bchan[chan].txcount;
145     bsp->inbytes = sc->sc_bchan[chan].rxcount;
146
147     sc->sc_bchan[chan].txcount = 0;
148     sc->sc_bchan[chan].rxcount = 0;
149
150     crit_exit();
151 }
152
153 int
154 capi_start_tx(capi_softc_t *sc, int chan)
155 {
156     struct mbuf *m_b3;
157     int sent = 0;
158
159     IF_DEQUEUE(&sc->sc_bchan[chan].tx_queue, m_b3);
160     while (m_b3) {
161         struct mbuf *m = m_b3->m_next;
162
163         sc->sc_bchan[chan].txcount += m_b3->m_len;
164         capi_data_b3_req(sc, chan, m_b3);
165         sent++;
166
167         m_b3 = m;
168     }
169
170     if (sc->sc_bchan[chan].capi_drvr_linktab) {
171         /* Notify i4b driver of activity, and if the queue is drained. */
172
173         if (sent)
174             (*sc->sc_bchan[chan].capi_drvr_linktab->bch_activity)(
175                 sc->sc_bchan[chan].capi_drvr_linktab->unit, ACT_TX);
176
177         if (IF_QEMPTY(&sc->sc_bchan[chan].tx_queue))
178             (*sc->sc_bchan[chan].capi_drvr_linktab->bch_tx_queue_empty)(
179                 sc->sc_bchan[chan].capi_drvr_linktab->unit);
180     }
181
182     return sent;
183 }
184
185 /*
186 //  capi_ll_attach
187 //      Called by a link layer driver at boot time.
188 */
189
190 int
191 capi_ll_attach(capi_softc_t *sc)
192 {
193     int i;
194
195     if (ncapi == (sizeof(capi_sc) / sizeof(capi_sc[0]))) {
196         printf("capi%d: too many units, increase MAX_CONTROLLERS\n", ncapi);
197         return (ENXIO);
198     }
199
200     /* Unit type and subtype; sc is partly filled by ll driver */
201     
202     ctrl_desc[nctrl].unit = ncapi;
203     ctrl_desc[nctrl].ctrl_type = CTRL_CAPI;
204     ctrl_desc[nctrl].card_type = sc->card_type;
205
206     /* L4 callbacks */
207     
208     ctrl_types[CTRL_CAPI].get_linktab = i4b_capi_ret_linktab;
209     ctrl_types[CTRL_CAPI].set_linktab = i4b_capi_set_linktab;
210
211     ctrl_desc[nctrl].N_CONNECT_REQUEST = n_connect_request;
212     ctrl_desc[nctrl].N_CONNECT_RESPONSE = n_connect_response;
213     ctrl_desc[nctrl].N_DISCONNECT_REQUEST = n_disconnect_request;
214     ctrl_desc[nctrl].N_ALERT_REQUEST = n_alert_request;
215     ctrl_desc[nctrl].N_DOWNLOAD = n_download;
216     ctrl_desc[nctrl].N_DIAGNOSTICS = NULL; /* XXX todo */
217     ctrl_desc[nctrl].N_MGMT_COMMAND = n_mgmt_command;
218
219     /* Unit state */
220
221     sc->sc_enabled = FALSE;
222     sc->sc_state = C_DOWN;
223     sc->sc_msgid = 0;
224
225     ctrl_desc[nctrl].dl_est = DL_DOWN;
226     ctrl_desc[nctrl].nbch = sc->sc_nbch;
227
228     for (i = 0; i < sc->sc_nbch; i++) {
229         ctrl_desc[nctrl].bch_state[i] = BCH_ST_FREE;
230         sc->sc_bchan[i].ncci = INVALID;
231         sc->sc_bchan[i].msgid = 0;
232         sc->sc_bchan[i].busy = 0;
233         sc->sc_bchan[i].state = B_FREE;
234
235         memset(&sc->sc_bchan[i].tx_queue, 0, sizeof(struct ifqueue));
236         memset(&sc->sc_bchan[i].rx_queue, 0, sizeof(struct ifqueue));
237         sc->sc_bchan[i].tx_queue.ifq_maxlen = IFQ_MAXLEN;
238         sc->sc_bchan[i].rx_queue.ifq_maxlen = IFQ_MAXLEN;
239         sc->sc_bchan[i].txcount = 0;
240         sc->sc_bchan[i].rxcount = 0;
241
242         sc->sc_bchan[i].cdid = CDID_UNUSED;
243         sc->sc_bchan[i].bprot = BPROT_NONE;
244         sc->sc_bchan[i].in_mbuf = NULL;
245
246         sc->sc_bchan[i].capi_drvr_linktab = NULL;
247
248         sc->sc_bchan[i].capi_isdn_linktab.unit = ncapi;
249         sc->sc_bchan[i].capi_isdn_linktab.channel = i;
250         sc->sc_bchan[i].capi_isdn_linktab.bch_config = i4b_capi_bch_config;
251         sc->sc_bchan[i].capi_isdn_linktab.bch_tx_start = i4b_capi_bch_start_tx;
252         sc->sc_bchan[i].capi_isdn_linktab.bch_stat = i4b_capi_bch_stat;
253         sc->sc_bchan[i].capi_isdn_linktab.tx_queue = &sc->sc_bchan[i].tx_queue;
254         sc->sc_bchan[i].capi_isdn_linktab.rx_queue = &sc->sc_bchan[i].rx_queue;
255         sc->sc_bchan[i].capi_isdn_linktab.rx_mbuf = &sc->sc_bchan[i].in_mbuf;
256     }
257
258     ctrl_desc[nctrl].tei = -1;
259
260     /* Up the controller index and store the softc */
261
262     sc->sc_unit = ncapi;
263     capi_sc[ncapi++] = sc;
264     sc->ctrl_unit = nctrl++;
265
266     printf("capi%d: card type %d attached\n", sc->sc_unit, sc->card_type);
267
268     return(0);
269 }
270
271 /*
272 //  n_mgmt_command
273 //      i4b L4 management command.
274 */
275
276 static void
277 n_mgmt_command(int unit, int op, void *arg)
278 {
279     capi_softc_t *sc = capi_sc[unit];
280
281     printf("capi%d: mgmt command %d\n", sc->sc_unit, op);
282
283     switch(op) {
284     case CMR_DOPEN:
285         sc->sc_enabled = TRUE;
286         break;
287
288     case CMR_DCLOSE:
289         sc->sc_enabled = FALSE;
290         break;
291
292     case CMR_SETTRACE:
293         break;
294
295     default:
296         break;
297     }
298 }
299
300 /*
301 //  n_connect_request
302 //      i4b L4 wants to connect. We assign a B channel to the call,
303 //      send a CAPI_CONNECT_REQ, and set the channel to B_CONNECT_CONF.
304 */
305
306 static void
307 n_connect_request(u_int cdid)
308 {
309     call_desc_t *cd = cd_by_cdid(cdid);
310     capi_softc_t *sc;
311     int bch;
312
313     if (!cd) {
314         printf("capi?: invalid cdid %d\n", cdid);
315         return;
316     }
317
318     sc = capi_sc[ctrl_desc[cd->controller].unit];
319     bch = cd->channelid;
320
321     crit_enter();
322
323     if ((bch < 0) || (bch >= sc->sc_nbch))
324         for (bch = 0; bch < sc->sc_nbch; bch++)
325             if (sc->sc_bchan[bch].state == B_FREE)
326                 break;
327
328     if (bch == sc->sc_nbch) {
329         crit_exit();
330         printf("capi%d: no free B channel\n", sc->sc_unit);
331         return;
332     }
333
334     cd->channelid = bch;
335
336     capi_connect_req(sc, cd);
337     crit_exit();
338 }
339
340 /*
341 //  n_connect_response
342 //      i4b L4 answers a call. We send a CONNECT_RESP with the proper
343 //      Reject code, and set the channel to B_CONNECT_B3_IND or B_FREE,
344 //      depending whether we answer or not.
345 */
346
347 static void
348 n_connect_response(u_int cdid, int response, int cause)
349 {
350     call_desc_t *cd = cd_by_cdid(cdid);
351     capi_softc_t *sc;
352     int bch;
353
354     if (!cd) {
355         printf("capi?: invalid cdid %d\n", cdid);
356         return;
357     }
358
359     sc = capi_sc[ctrl_desc[cd->controller].unit];
360     bch = cd->channelid;
361
362     T400_stop(cd);
363         
364     cd->response = response;
365     cd->cause_out = cause;
366
367     crit_enter();
368     capi_connect_resp(sc, cd);
369     crit_exit();
370 }
371
372 /*
373 //  n_disconnect_request
374 //      i4b L4 wants to disconnect. We send a DISCONNECT_REQ and
375 //      set the channel to B_DISCONNECT_CONF.
376 */
377
378 static void
379 n_disconnect_request(u_int cdid, int cause)
380 {
381     call_desc_t *cd = cd_by_cdid(cdid);
382     capi_softc_t *sc;
383     int bch;
384
385     if (!cd) {
386         printf("capi?: invalid cdid %d\n", cdid);
387         return;
388     }
389
390     sc = capi_sc[ctrl_desc[cd->controller].unit];
391     bch = cd->channelid;
392
393     cd->cause_out = cause;
394
395     crit_enter();
396     capi_disconnect_req(sc, cd);
397     crit_exit();
398 }
399
400 /*
401 //  n_alert_request
402 //      i4b L4 wants to alert an incoming call. We send ALERT_REQ.
403 */
404
405 static void
406 n_alert_request(u_int cdid)
407 {
408     call_desc_t *cd = cd_by_cdid(cdid);
409     capi_softc_t *sc;
410
411     if (!cd) {
412         printf("capi?: invalid cdid %d\n", cdid);
413         return;
414     }
415
416     sc = capi_sc[ctrl_desc[cd->controller].unit];
417
418     crit_enter();
419     capi_alert_req(sc, cd);
420     crit_exit();
421 }
422
423 /*
424 //  n_download
425 //      L4 -> firmware download
426 */
427
428 static int
429 n_download(int unit, int numprotos, struct isdn_dr_prot *protocols)
430 {
431     capi_softc_t *sc = capi_sc[unit];
432
433     if (sc->load) {
434         (*capi_sc[unit]->load)(sc, protocols[0].bytecount,
435                                protocols[0].microcode);
436     }
437
438     return(0);
439 }