2 * Copyright (c) 1997, 2000 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_l4mgmt.c - layer 4 calldescriptor management utilites
28 * -----------------------------------------------------------
30 * $Id: i4b_l4mgmt.c,v 1.34 2000/09/01 14:11:51 hm Exp $
32 * $FreeBSD: src/sys/i4b/layer4/i4b_l4mgmt.c,v 1.6.2.2 2001/08/10 14:08:43 obrien Exp $
33 * $DragonFly: src/sys/net/i4b/layer4/i4b_l4mgmt.c,v 1.8 2005/06/14 21:19:19 joerg Exp $
35 * last edit-date: [Fri Oct 13 15:58:34 2000]
37 *---------------------------------------------------------------------------*/
43 #include <sys/param.h>
44 #include <sys/systm.h>
46 #include <sys/thread2.h>
48 #include <net/i4b/include/machine/i4b_debug.h>
49 #include <net/i4b/include/machine/i4b_ioctl.h>
51 #include "../include/i4b_l3l4.h"
52 #include "../include/i4b_global.h"
56 call_desc_t call_desc[N_CALL_DESC]; /* call descriptor array */
58 static unsigned int get_cdid(void);
60 int nctrl; /* number of attached controllers */
62 static void i4b_init_callout(call_desc_t *);
64 /*---------------------------------------------------------------------------*
65 * return a new unique call descriptor id
66 * --------------------------------------
67 * returns a new calldescriptor id which is used to uniquely identyfy
68 * a single call in the communication between kernel and userland.
69 * this cdid is then used to associate a calldescriptor with an id.
70 *---------------------------------------------------------------------------*/
74 static unsigned int cdid_count = 0;
84 if(cdid_count == CDID_UNUSED) /* zero is invalid */
86 else if(cdid_count > CDID_MAX) /* wraparound ? */
89 /* check if id already in use */
91 for(i=0; i < N_CALL_DESC; i++)
93 if(call_desc[i].cdid == cdid_count)
105 /*---------------------------------------------------------------------------*
106 * reserve a calldescriptor for later usage
107 * ----------------------------------------
108 * searches the calldescriptor array until an unused
109 * descriptor is found, gets a new calldescriptor id
110 * and reserves it by putting the id into the cdid field.
111 * returns pointer to the calldescriptor.
112 *---------------------------------------------------------------------------*/
123 for(i=0; i < N_CALL_DESC; i++)
125 if(call_desc[i].cdid == CDID_UNUSED)
127 bzero(&call_desc[i], sizeof(call_desc_t)); /* clear it */
128 call_desc[i].cdid = get_cdid(); /* fill in new cdid */
129 cd = &(call_desc[i]); /* get pointer to descriptor */
130 NDBGL4(L4_MSG, "found free cd - index=%d cdid=%u",
131 i, call_desc[i].cdid);
139 panic("reserve_cd: no free call descriptor available!");
141 i4b_init_callout(cd);
146 /*---------------------------------------------------------------------------*
147 * free a calldescriptor
148 * ---------------------
149 * free a unused calldescriptor by giving address of calldescriptor
150 * and writing a 0 into the cdid field marking it as unused.
151 *---------------------------------------------------------------------------*/
153 freecd_by_cd(call_desc_t *cd)
159 for(i=0; i < N_CALL_DESC; i++)
161 if( (call_desc[i].cdid != CDID_UNUSED) &&
162 (&(call_desc[i]) == cd) )
164 NDBGL4(L4_MSG, "releasing cd - index=%d cdid=%u cr=%d",
165 i, call_desc[i].cdid, cd->cr);
166 call_desc[i].cdid = CDID_UNUSED;
172 panic("freecd_by_cd: ERROR, cd not found, cr = %d\n", cd->cr);
177 /*---------------------------------------------------------------------------*
178 * return pointer to calldescriptor by giving the calldescriptor id
179 * ----------------------------------------------------------------
180 * lookup a calldescriptor in the calldescriptor array by looking
181 * at the cdid field. return pointer to calldescriptor if found,
182 * else return NULL if not found.
183 *---------------------------------------------------------------------------*/
185 cd_by_cdid(unsigned int cdid)
189 for(i=0; i < N_CALL_DESC; i++)
191 if(call_desc[i].cdid == cdid)
193 NDBGL4(L4_MSG, "found cdid - index=%d cdid=%u cr=%d",
194 i, call_desc[i].cdid, call_desc[i].cr);
195 i4b_init_callout(&call_desc[i]);
196 return(&(call_desc[i]));
202 /*---------------------------------------------------------------------------*
203 * search calldescriptor
204 * ---------------------
205 * This routine searches for the calldescriptor for a passive controller
206 * given by unit number, callreference and callreference flag.
207 * It returns a pointer to the calldescriptor if found, else a NULL.
208 *---------------------------------------------------------------------------*/
210 cd_by_unitcr(int unit, int cr, int crf)
214 for(i=0; i < N_CALL_DESC; i++)
216 if((call_desc[i].cdid != CDID_UNUSED) &&
217 (ctrl_desc[call_desc[i].controller].ctrl_type == CTRL_PASSIVE) &&
218 (ctrl_desc[call_desc[i].controller].unit == unit) &&
219 (call_desc[i].cr == cr) &&
220 (call_desc[i].crflag == crf) )
222 NDBGL4(L4_MSG, "found cd, index=%d cdid=%u cr=%d",
223 i, call_desc[i].cdid, call_desc[i].cr);
224 i4b_init_callout(&call_desc[i]);
225 return(&(call_desc[i]));
231 /*---------------------------------------------------------------------------*
232 * generate 7 bit "random" number used for outgoing Call Reference
233 *---------------------------------------------------------------------------*/
235 get_rand_cr(int unit)
238 static u_char val, retval;
239 static int called = 42;
243 for(i=0; i < 50 ; i++, val++)
248 read_random((char *)&val, sizeof(val));
250 val = (u_char)random();
251 #endif /* RANDOMDEV */
255 if(retval == 0 || retval == 0x7f)
258 for(j=0; j < N_CALL_DESC; j++)
260 if( (call_desc[j].cdid != CDID_UNUSED) &&
261 (call_desc[j].cr == retval) )
274 /*---------------------------------------------------------------------------*
275 * initialize the callout handles for FreeBSD
276 *---------------------------------------------------------------------------*/
278 i4b_init_callout(call_desc_t *cd)
280 if(cd->callouts_inited == 0)
282 callout_init(&cd->idle_timeout);
283 callout_init(&cd->T303_timeout);
284 callout_init(&cd->T305_timeout);
285 callout_init(&cd->T308_timeout);
286 callout_init(&cd->T309_timeout);
287 callout_init(&cd->T310_timeout);
288 callout_init(&cd->T313_timeout);
289 callout_init(&cd->T400_timeout);
290 cd->callouts_inited = 1;
294 /*---------------------------------------------------------------------------*
296 *---------------------------------------------------------------------------*/
298 i4b_l4_daemon_attached(void)
304 for(i=0; i < nctrl; i++)
306 /*XXX*/ if(ctrl_desc[i].ctrl_type == CTRL_PASSIVE)
308 NDBGL4(L4_MSG, "CMR_DOPEN sent to unit %d", ctrl_desc[i].unit);
309 (*ctrl_desc[i].N_MGMT_COMMAND)(ctrl_desc[i].unit, CMR_DOPEN, 0);
315 /*---------------------------------------------------------------------------*
317 *---------------------------------------------------------------------------*/
319 i4b_l4_daemon_detached(void)
325 for(i=0; i < nctrl; i++)
327 /*XXX*/ if(ctrl_desc[i].ctrl_type == CTRL_PASSIVE)
329 NDBGL4(L4_MSG, "CMR_DCLOSE sent to unit %d", ctrl_desc[i].unit);
330 (*ctrl_desc[i].N_MGMT_COMMAND)(ctrl_desc[i].unit, CMR_DCLOSE, 0);
336 #ifdef I4B_CD_DEBUG_PRINT
338 extern char *print_l3state(call_desc_t *cd);
340 void i4b_print_cdp(call_desc_t *cdp);
341 void i4b_print_cdx(int index);
342 void i4b_print_cda(void);
343 void i4b_print_cdaa(void);
345 /*---------------------------------------------------------------------------*
346 * print a call descriptor by cd-pointer
347 *---------------------------------------------------------------------------*/
349 i4b_print_cdp(call_desc_t *cdp)
351 if((cdp > &(call_desc[N_CALL_DESC])) || (cdp < &(call_desc[0])))
353 printf("i4b_print_cd: cdp out of range!\n");
357 printf("i4b_print_cd: printing call descriptor %d at 0x%lx:\n", cdp - (&(call_desc[0])), (unsigned long)cdp);
359 printf(" cdid = %d\n", cdp->cdid);
360 printf(" controller = %d (u=%d, dl=%d, b1=%d, b2=%d)\n",
362 ctrl_desc[cdp->controller].unit,
363 ctrl_desc[cdp->controller].dl_est,
364 ctrl_desc[cdp->controller].bch_state[CHAN_B1],
365 ctrl_desc[cdp->controller].bch_state[CHAN_B2]);
366 printf(" cr = 0x%02x\n", cdp->cr);
367 printf(" crflag = %d\n", cdp->crflag);
368 printf(" channelid = %d\n", cdp->channelid);
369 printf(" bprot = %d\n", cdp->bprot);
370 printf(" driver = %d\n", cdp->driver);
371 printf(" driver_unit = %d\n", cdp->driver_unit);
372 printf(" call_state = %d\n", cdp->call_state);
373 printf(" Q931state = %s\n", print_l3state(cdp));
374 printf(" event = %d\n", cdp->event);
375 printf(" response = %d\n", cdp->response);
376 printf(" T303 = %d\n", cdp->T303);
377 printf("T303_first_to = %d\n", cdp->T303_first_to);
378 printf(" T305 = %d\n", cdp->T305);
379 printf(" T308 = %d\n", cdp->T308);
380 printf("T308_first_to = %d\n", cdp->T308_first_to);
381 printf(" T309 = %d\n", cdp->T309);
382 printf(" T310 = %d\n", cdp->T310);
383 printf(" T313 = %d\n", cdp->T313);
384 printf(" T400 = %d\n", cdp->T400);
385 printf(" dir = %s\n", cdp->dir == DIR_OUTGOING ? "out" : "in");
388 /*---------------------------------------------------------------------------*
389 * print a call descriptor by index
390 *---------------------------------------------------------------------------*/
392 i4b_print_cdx(int index)
394 if(index >= N_CALL_DESC)
396 printf("i4b_print_cdx: index %d >= N_CALL_DESC %d\n", index, N_CALL_DESC);
399 i4b_print_cdp(&(call_desc[index]));
402 /*---------------------------------------------------------------------------*
403 * print all call descriptors
404 *---------------------------------------------------------------------------*/
410 for(i=0; i < N_CALL_DESC; i++)
412 i4b_print_cdp(&(call_desc[i]));
416 /*---------------------------------------------------------------------------*
417 * print all active call descriptors
418 *---------------------------------------------------------------------------*/
424 for(i=0; i < N_CALL_DESC; i++)
426 if(call_desc[i].cdid != CDID_UNUSED)
428 i4b_print_cdp(&(call_desc[i]));
433 #endif /* I4B_CD_DEBUG_PRINT */
435 #endif /* NI4BQ931 > 0 */