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