summaryrefslogtreecommitdiffstats
path: root/sys/i4b/capi/capi_l4if.c
blob: ef652640cfeb2c4c568b5fac8e3fe10939a0ced2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*-
 * Copyright (c) 2001 Cubical Solutions Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/* capi/capi_l4if.c	The CAPI i4b L4/device interface.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <net/if.h>

#include <i4b/include/i4b_debug.h>
#include <i4b/include/i4b_ioctl.h>
#include <i4b/include/i4b_cause.h>

#include <i4b/include/i4b_l3l4.h>
#include <i4b/include/i4b_mbuf.h>
#include <i4b/include/i4b_global.h>

#include <i4b/layer4/i4b_l4.h>

#include <i4b/capi/capi.h>
#include <i4b/capi/capi_msgs.h>

static void n_connect_request(u_int cdid);
static void n_connect_response(u_int cdid, int response, int cause);
static void n_disconnect_request(u_int cdid, int cause);
static void n_alert_request(u_int cdid);
static void n_mgmt_command(int unit, int cmd, void *parm);
static int  n_download(int unit, int, struct isdn_dr_prot *);

capi_softc_t *capi_sc[MAX_CONTROLLERS] = { NULL, };
int ncapi = 0;

/*
//  i4b_capi_{ret,set}_linktab
//      i4b driver glue.
//
//  i4b_capi_bch_config
//      Called by i4b driver to flush + {en,dis}able a channel.
//
//  i4b_capi_bch_start_tx
//      Called by i4b driver to transmit a queued mbuf.
//
//  i4b_capi_bch_stat
//      Called by i4b driver to obtain statistics information.
*/

static isdn_link_t *
i4b_capi_ret_linktab(int unit, int channel)
{
    capi_softc_t *sc = capi_sc[unit];
    return &sc->sc_bchan[channel].capi_isdn_linktab;
}

static void
i4b_capi_set_linktab(int unit, int channel, drvr_link_t *dlt)
{
    capi_softc_t *sc = capi_sc[unit];
    sc->sc_bchan[channel].capi_drvr_linktab = dlt;
}

static void
i4b_capi_bch_config(int unit, int chan, int bprot, int activate)
{
    capi_softc_t *sc = capi_sc[unit];

    i4b_Bcleanifq(&sc->sc_bchan[chan].tx_queue);
    sc->sc_bchan[chan].tx_queue.ifq_maxlen = IFQ_MAXLEN;
    sc->sc_bchan[chan].txcount = 0;

    /* The telephony drivers use rx_queue for receive. */

    i4b_Bcleanifq(&sc->sc_bchan[chan].rx_queue);
    sc->sc_bchan[chan].rx_queue.ifq_maxlen = IFQ_MAXLEN;
    sc->sc_bchan[chan].rxcount = 0;

    /* HDLC frames are put to in_mbuf */

    i4b_Bfreembuf(sc->sc_bchan[chan].in_mbuf);
    sc->sc_bchan[chan].in_mbuf = NULL;

    /* Because of the difference, we need to remember the protocol. */

    sc->sc_bchan[chan].bprot = bprot;
    sc->sc_bchan[chan].busy = 0;
}

static void
i4b_capi_bch_start_tx(int unit, int chan)
{
    capi_softc_t *sc = capi_sc[unit];
    int s;

    s = SPLI4B();

    if (sc->sc_bchan[chan].state != B_CONNECTED) {
	splx(s);
	printf("capi%d: start_tx on unconnected channel\n", sc->sc_unit);
	return;
    }

    if (sc->sc_bchan[chan].busy) {
	splx(s);
	return;
    }

    capi_start_tx(sc, chan);

    splx(s);
}

static void
i4b_capi_bch_stat(int unit, int chan, bchan_statistics_t *bsp)
{
    capi_softc_t *sc = capi_sc[unit];
    int s = SPLI4B();

    bsp->outbytes = sc->sc_bchan[chan].txcount;
    bsp->inbytes = sc->sc_bchan[chan].rxcount;

    sc->sc_bchan[chan].txcount = 0;
    sc->sc_bchan[chan].rxcount = 0;

    splx(s);
}

int capi_start_tx(capi_softc_t *sc, int chan)
{
    struct mbuf *m_b3;
    int sent = 0;

    _IF_DEQUEUE(&sc->sc_bchan[chan].tx_queue, m_b3);
    while (m_b3) {
	struct mbuf *m = m_b3->m_next;

	sc->sc_bchan[chan].txcount += m_b3->m_len;
	capi_data_b3_req(sc, chan, m_b3);
	sent++;

	m_b3 = m;
    }

    if (sc->sc_bchan[chan].capi_drvr_linktab) {
	/* Notify i4b driver of activity, and if the queue is drained. */

	if (sent)
	    (*sc->sc_bchan[chan].capi_drvr_linktab->bch_activity)(
		sc->sc_bchan[chan].capi_drvr_linktab->unit, ACT_TX);

	if (IF_QEMPTY(&sc->sc_bchan[chan].tx_queue))
	    (*sc->sc_bchan[chan].capi_drvr_linktab->bch_tx_queue_empty)(
		sc->sc_bchan[chan].capi_drvr_linktab->unit);
    }

    return sent;
}

/*
//  capi_ll_attach
//      Called by a link layer driver at boot time.
*/

int
capi_ll_attach(capi_softc_t *sc)
{
    int i;

    if (ncapi == (sizeof(capi_sc) / sizeof(capi_sc[0]))) {
	printf("capi%d: too many units, increase MAX_CONTROLLERS\n", ncapi);
	return (ENXIO);
    }

    /* Unit type and subtype; sc is partly filled by ll driver */
    
    ctrl_desc[nctrl].unit = ncapi;
    ctrl_desc[nctrl].ctrl_type = CTRL_CAPI;
    ctrl_desc[nctrl].card_type = sc->card_type;

    /* L4 callbacks */
    
    ctrl_types[CTRL_CAPI].get_linktab = i4b_capi_ret_linktab;
    ctrl_types[CTRL_CAPI].set_linktab = i4b_capi_set_linktab;

    ctrl_desc[nctrl].N_CONNECT_REQUEST = n_connect_request;
    ctrl_desc[nctrl].N_CONNECT_RESPONSE = n_connect_response;
    ctrl_desc[nctrl].N_DISCONNECT_REQUEST = n_disconnect_request;
    ctrl_desc[nctrl].N_ALERT_REQUEST = n_alert_request;
    ctrl_desc[nctrl].N_DOWNLOAD = n_download;
    ctrl_desc[nctrl].N_DIAGNOSTICS = NULL; /* XXX todo */
    ctrl_desc[nctrl].N_MGMT_COMMAND = n_mgmt_command;

    /* Unit state */

    sc->sc_enabled = FALSE;
    sc->sc_state = C_DOWN;
    sc->sc_msgid = 0;

    ctrl_desc[nctrl].dl_est = DL_DOWN;
    ctrl_desc[nctrl].nbch = sc->sc_nbch;

    for (i = 0; i < sc->sc_nbch; i++) {
	ctrl_desc[nctrl].bch_state[i] = BCH_ST_FREE;
	sc->sc_bchan[i].ncci = INVALID;
	sc->sc_bchan[i].msgid = 0;
	sc->sc_bchan[i].busy = 0;
	sc->sc_bchan[i].state = B_FREE;

	memset(&sc->sc_bchan[i].tx_queue, 0, sizeof(struct ifqueue));
	memset(&sc->sc_bchan[i].rx_queue, 0, sizeof(struct ifqueue));
	sc->sc_bchan[i].tx_queue.ifq_maxlen = IFQ_MAXLEN;
	sc->sc_bchan[i].rx_queue.ifq_maxlen = IFQ_MAXLEN;

	if(!mtx_initialized(&sc->sc_bchan[i].tx_queue.ifq_mtx))
		mtx_init(&sc->sc_bchan[i].tx_queue.ifq_mtx, "i4b_capi_tx", NULL, MTX_DEF);
	if(!mtx_initialized(&sc->sc_bchan[i].rx_queue.ifq_mtx))	
		mtx_init(&sc->sc_bchan[i].rx_queue.ifq_mtx, "i4b_capi_rx", NULL, MTX_DEF);	

	sc->sc_bchan[i].txcount = 0;
	sc->sc_bchan[i].rxcount = 0;

	sc->sc_bchan[i].cdid = CDID_UNUSED;
	sc->sc_bchan[i].bprot = BPROT_NONE;
	sc->sc_bchan[i].in_mbuf = NULL;

	sc->sc_bchan[i].capi_drvr_linktab = NULL;

	sc->sc_bchan[i].capi_isdn_linktab.unit = ncapi;
	sc->sc_bchan[i].capi_isdn_linktab.channel = i;
	sc->sc_bchan[i].capi_isdn_linktab.bch_config = i4b_capi_bch_config;
	sc->sc_bchan[i].capi_isdn_linktab.bch_tx_start = i4b_capi_bch_start_tx;
	sc->sc_bchan[i].capi_isdn_linktab.bch_stat = i4b_capi_bch_stat;
	sc->sc_bchan[i].capi_isdn_linktab.tx_queue = &sc->sc_bchan[i].tx_queue;
	sc->sc_bchan[i].capi_isdn_linktab.rx_queue = &sc->sc_bchan[i].rx_queue;
	sc->sc_bchan[i].capi_isdn_linktab.rx_mbuf = &sc->sc_bchan[i].in_mbuf;
    }

    ctrl_desc[nctrl].tei = -1;

    /* Up the controller index and store the softc */

    sc->sc_unit = ncapi;
    capi_sc[ncapi++] = sc;
    sc->ctrl_unit = nctrl++;

    printf("capi%d: card type %d attached\n", sc->sc_unit, sc->card_type);

    return(0);
}

/*
//  n_mgmt_command
//      i4b L4 management command.
*/

static void
n_mgmt_command(int unit, int op, void *arg)
{
    capi_softc_t *sc = capi_sc[unit];

    printf("capi%d: mgmt command %d\n", sc->sc_unit, op);

    switch(op) {
    case CMR_DOPEN:
	sc->sc_enabled = TRUE;
	break;

    case CMR_DCLOSE:
	sc->sc_enabled = FALSE;
	break;

    case CMR_SETTRACE:
	break;

    default:
	break;
    }
}

/*
//  n_connect_request
//      i4b L4 wants to connect. We assign a B channel to the call,
//      send a CAPI_CONNECT_REQ, and set the channel to B_CONNECT_CONF.
*/

static void
n_connect_request(u_int cdid)
{
    call_desc_t *cd = cd_by_cdid(cdid);
    capi_softc_t *sc;
    int bch, s;

    if (!cd) {
	printf("capi?: invalid cdid %d\n", cdid);
	return;
    }

    sc = capi_sc[ctrl_desc[cd->controller].unit];
    bch = cd->channelid;

    s = SPLI4B();

    if ((bch < 0) || (bch >= sc->sc_nbch))
	for (bch = 0; bch < sc->sc_nbch; bch++)
	    if (sc->sc_bchan[bch].state == B_FREE)
		break;

    if (bch == sc->sc_nbch) {
	splx(s);
	printf("capi%d: no free B channel\n", sc->sc_unit);
	return;
    }

    cd->channelid = bch;

    capi_connect_req(sc, cd);
    splx(s);
}

/*
//  n_connect_response
//      i4b L4 answers a call. We send a CONNECT_RESP with the proper
//      Reject code, and set the channel to B_CONNECT_B3_IND or B_FREE,
//      depending whether we answer or not.
*/

static void
n_connect_response(u_int cdid, int response, int cause)
{
    call_desc_t *cd = cd_by_cdid(cdid);
    capi_softc_t *sc;
    int bch, s;

    if (!cd) {
	printf("capi?: invalid cdid %d\n", cdid);
	return;
    }

    sc = capi_sc[ctrl_desc[cd->controller].unit];
    bch = cd->channelid;

    T400_stop(cd);
	
    cd->response = response;
    cd->cause_out = cause;

    s = SPLI4B();
    capi_connect_resp(sc, cd);
    splx(s);
}

/*
//  n_disconnect_request
//      i4b L4 wants to disconnect. We send a DISCONNECT_REQ and
//      set the channel to B_DISCONNECT_CONF.
*/

static void
n_disconnect_request(u_int cdid, int cause)
{
    call_desc_t *cd = cd_by_cdid(cdid);
    capi_softc_t *sc;
    int bch, s;

    if (!cd) {
	printf("capi?: invalid cdid %d\n", cdid);
	return;
    }

    sc = capi_sc[ctrl_desc[cd->controller].unit];
    bch = cd->channelid;

    cd->cause_out = cause;

    s = SPLI4B();
    capi_disconnect_req(sc, cd);
    splx(s);
}

/*
//  n_alert_request
//      i4b L4 wants to alert an incoming call. We send ALERT_REQ.
*/

static void
n_alert_request(u_int cdid)
{
    call_desc_t *cd = cd_by_cdid(cdid);
    capi_softc_t *sc;
    int s;

    if (!cd) {
	printf("capi?: invalid cdid %d\n", cdid);
	return;
    }

    sc = capi_sc[ctrl_desc[cd->controller].unit];

    s = SPLI4B();
    capi_alert_req(sc, cd);
    splx(s);
}

/*
//  n_download
//      L4 -> firmware download
*/

static int
n_download(int unit, int numprotos, struct isdn_dr_prot *protocols)
{
    capi_softc_t *sc = capi_sc[unit];

    if (sc->load) {
	(*capi_sc[unit]->load)(sc, protocols[0].bytecount,
			       protocols[0].microcode);
    }

    return(0);
}
OpenPOWER on IntegriCloud