2 * Copyright (c) 1997, 2002 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
26 /*---------------------------------------------------------------------------
28 * i4b_l4mgmt.c - layer 4 calldescriptor management utilites
29 * -----------------------------------------------------------
30 * last edit-date: [Sun Aug 11 12:42:01 2002]
32 *---------------------------------------------------------------------------*/
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/systm.h>
41 #include <i4b/include/i4b_debug.h>
42 #include <i4b/include/i4b_ioctl.h>
44 #include <i4b/include/i4b_l3l4.h>
45 #include <i4b/include/i4b_global.h>
47 #include <i4b/layer4/i4b_l4.h>
49 call_desc_t call_desc[N_CALL_DESC]; /* call descriptor array */
51 static unsigned int get_cdid(void);
53 int nctrl; /* number of attached controllers */
55 void i4b_init_callout(call_desc_t *);
57 /*---------------------------------------------------------------------------*
58 * return a new unique call descriptor id
59 * --------------------------------------
60 * returns a new calldescriptor id which is used to uniquely identyfy
61 * a single call in the communication between kernel and userland.
62 * this cdid is then used to associate a calldescriptor with an id.
63 *---------------------------------------------------------------------------*/
67 static unsigned int cdid_count = 0;
78 if(cdid_count == CDID_UNUSED) /* zero is invalid */
80 else if(cdid_count > CDID_MAX) /* wraparound ? */
83 /* check if id already in use */
85 for(i=0; i < N_CALL_DESC; i++)
87 if(call_desc[i].cdid == cdid_count)
99 /*---------------------------------------------------------------------------*
100 * reserve a calldescriptor for later usage
101 * ----------------------------------------
102 * searches the calldescriptor array until an unused
103 * descriptor is found, gets a new calldescriptor id
104 * and reserves it by putting the id into the cdid field.
105 * returns pointer to the calldescriptor.
106 *---------------------------------------------------------------------------*/
118 for(i=0; i < N_CALL_DESC; i++)
120 if(call_desc[i].cdid == CDID_UNUSED)
122 bzero(&call_desc[i], sizeof(call_desc_t)); /* clear it */
123 call_desc[i].cdid = get_cdid(); /* fill in new cdid */
124 cd = &(call_desc[i]); /* get pointer to descriptor */
125 NDBGL4(L4_MSG, "found free cd - index=%d cdid=%u",
126 i, call_desc[i].cdid);
134 panic("reserve_cd: no free call descriptor available!");
136 i4b_init_callout(cd);
141 /*---------------------------------------------------------------------------*
142 * free a calldescriptor
143 * ---------------------
144 * free an unused calldescriptor by giving address of calldescriptor
145 * and writing a 0 into the cdid field marking it as unused.
146 *---------------------------------------------------------------------------*/
148 freecd_by_cd(call_desc_t *cd)
153 for(i=0; i < N_CALL_DESC; i++)
155 if( (call_desc[i].cdid != CDID_UNUSED) &&
156 (&(call_desc[i]) == cd) )
158 NDBGL4(L4_MSG, "releasing cd - index=%d cdid=%u cr=%d",
159 i, call_desc[i].cdid, cd->cr);
160 call_desc[i].cdid = CDID_UNUSED;
166 panic("freecd_by_cd: ERROR, cd not found, cr = %d\n", cd->cr);
171 /*---------------------------------------------------------------------------*
172 * return pointer to calldescriptor by giving the calldescriptor id
173 * ----------------------------------------------------------------
174 * lookup a calldescriptor in the calldescriptor array by looking
175 * at the cdid field. return pointer to calldescriptor if found,
176 * else return NULL if not found.
177 *---------------------------------------------------------------------------*/
179 cd_by_cdid(unsigned int cdid)
183 for(i=0; i < N_CALL_DESC; i++)
185 if(call_desc[i].cdid == cdid)
187 NDBGL4(L4_MSG, "found cdid - index=%d cdid=%u cr=%d",
188 i, call_desc[i].cdid, call_desc[i].cr);
190 i4b_init_callout(&call_desc[i]);
192 return(&(call_desc[i]));
198 /*---------------------------------------------------------------------------*
199 * search calldescriptor
200 * ---------------------
201 * This routine searches for the calldescriptor for a passive controller
202 * given by unit number, callreference and callreference flag.
203 * It returns a pointer to the calldescriptor if found, else a NULL.
204 *---------------------------------------------------------------------------*/
206 cd_by_unitcr(int unit, int cr, int crf)
210 for(i=0; i < N_CALL_DESC; i++)
212 if((call_desc[i].cdid != CDID_UNUSED) &&
213 (ctrl_desc[call_desc[i].controller].ctrl_type == CTRL_PASSIVE) &&
214 (ctrl_desc[call_desc[i].controller].unit == unit) &&
215 (call_desc[i].cr == cr) &&
216 (call_desc[i].crflag == crf) )
218 NDBGL4(L4_MSG, "found cd, index=%d cdid=%u cr=%d",
219 i, call_desc[i].cdid, call_desc[i].cr);
221 i4b_init_callout(&call_desc[i]);
223 return(&(call_desc[i]));
229 /*---------------------------------------------------------------------------*
230 * generate 7 bit "random" number used for outgoing Call Reference
231 *---------------------------------------------------------------------------*/
233 get_rand_cr(int unit)
236 static u_char val, retval;
237 static int called = 42;
241 for(i=0; i < 50 ; i++, val++)
246 read_random((char *)&val, sizeof(val));
248 val = (u_char)random();
249 #endif /* RANDOMDEV */
253 if(retval == 0 || retval == 0x7f)
256 for(j=0; j < N_CALL_DESC; j++)
258 if( (call_desc[j].cdid != CDID_UNUSED) &&
259 (call_desc[j].cr == retval) )
272 /*---------------------------------------------------------------------------*
273 * initialize the callout handles for FreeBSD
274 *---------------------------------------------------------------------------*/
276 i4b_init_callout(call_desc_t *cd)
278 if(cd->callouts_inited == 0)
280 callout_handle_init(&cd->idle_timeout_handle);
281 callout_handle_init(&cd->T303_callout);
282 callout_handle_init(&cd->T305_callout);
283 callout_handle_init(&cd->T308_callout);
284 callout_handle_init(&cd->T309_callout);
285 callout_handle_init(&cd->T310_callout);
286 callout_handle_init(&cd->T313_callout);
287 callout_handle_init(&cd->T400_callout);
288 cd->callouts_inited = 1;
292 /*---------------------------------------------------------------------------*
294 *---------------------------------------------------------------------------*/
296 i4b_l4_daemon_attached(void)
302 for(i=0; i < nctrl; i++)
304 /*XXX*/ if(*ctrl_desc[i].N_MGMT_COMMAND &&
305 (ctrl_desc[i].ctrl_type == CTRL_PASSIVE ||
306 ctrl_desc[i].ctrl_type == CTRL_CAPIMGR))
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].N_MGMT_COMMAND &&
328 (ctrl_desc[i].ctrl_type == CTRL_PASSIVE ||
329 ctrl_desc[i].ctrl_type == CTRL_CAPIMGR))
331 NDBGL4(L4_MSG, "CMR_DCLOSE sent to unit %d", ctrl_desc[i].unit);
332 (*ctrl_desc[i].N_MGMT_COMMAND)(ctrl_desc[i].unit, CMR_DCLOSE, 0);
338 #ifdef I4B_CD_DEBUG_PRINT
340 extern char *print_l3state(call_desc_t *cd);
342 void i4b_print_cdp(call_desc_t *cdp);
343 void i4b_print_cdx(int index);
344 void i4b_print_cda(void);
345 void i4b_print_cdaa(void);
347 /*---------------------------------------------------------------------------*
348 * print a call descriptor by cd-pointer
349 *---------------------------------------------------------------------------*/
351 i4b_print_cdp(call_desc_t *cdp)
353 if((cdp > &(call_desc[N_CALL_DESC])) || (cdp < &(call_desc[0])))
355 printf("i4b_print_cd: cdp out of range!\n");
359 printf("i4b_print_cd: printing call descriptor %d at 0x%lx:\n", cdp - (&(call_desc[0])), (unsigned long)cdp);
361 printf(" cdid = %d\n", cdp->cdid);
362 printf(" controller = %d (u=%d, dl=%d, b1=%d, b2=%d)\n",
364 ctrl_desc[cdp->controller].unit,
365 ctrl_desc[cdp->controller].dl_est,
366 ctrl_desc[cdp->controller].bch_state[CHAN_B1],
367 ctrl_desc[cdp->controller].bch_state[CHAN_B2]);
368 printf(" cr = 0x%02x\n", cdp->cr);
369 printf(" crflag = %d\n", cdp->crflag);
370 printf(" channelid = %d\n", cdp->channelid);
371 printf(" bprot = %d\n", cdp->bprot);
372 printf(" bcap = %d\n", cdp->bcap);
373 printf(" driver = %d\n", cdp->driver);
374 printf(" driver_unit = %d\n", cdp->driver_unit);
375 printf(" call_state = %d\n", cdp->call_state);
376 printf(" Q931state = %s\n", print_l3state(cdp));
377 printf(" event = %d\n", cdp->event);
378 printf(" response = %d\n", cdp->response);
379 printf(" T303 = %d\n", cdp->T303);
380 printf("T303_first_to = %d\n", cdp->T303_first_to);
381 printf(" T305 = %d\n", cdp->T305);
382 printf(" T308 = %d\n", cdp->T308);
383 printf("T308_first_to = %d\n", cdp->T308_first_to);
384 printf(" T309 = %d\n", cdp->T309);
385 printf(" T310 = %d\n", cdp->T310);
386 printf(" T313 = %d\n", cdp->T313);
387 printf(" T400 = %d\n", cdp->T400);
388 printf(" dir = %s\n", cdp->dir == DIR_OUTGOING ? "out" : "in");
391 /*---------------------------------------------------------------------------*
392 * print a call descriptor by index
393 *---------------------------------------------------------------------------*/
395 i4b_print_cdx(int index)
397 if(index >= N_CALL_DESC)
399 printf("i4b_print_cdx: index %d >= N_CALL_DESC %d\n", index, N_CALL_DESC);
402 i4b_print_cdp(&(call_desc[index]));
405 /*---------------------------------------------------------------------------*
406 * print all call descriptors
407 *---------------------------------------------------------------------------*/
413 for(i=0; i < N_CALL_DESC; i++)
415 i4b_print_cdp(&(call_desc[i]));
419 /*---------------------------------------------------------------------------*
420 * print all active call descriptors
421 *---------------------------------------------------------------------------*/
427 for(i=0; i < N_CALL_DESC; i++)
429 if(call_desc[i].cdid != CDID_UNUSED)
431 i4b_print_cdp(&(call_desc[i]));
436 #endif /* I4B_CD_DEBUG_PRINT */