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.2 2003/06/17 04:28:40 dillon Exp $
35 * last edit-date: [Fri Oct 13 15:58:34 2000]
37 *---------------------------------------------------------------------------*/
43 #include <sys/param.h>
44 #include <sys/systm.h>
47 #if defined(__NetBSD__) && __NetBSD_Version__ >= 104230000
48 #include <sys/callout.h>
52 #include <machine/i4b_debug.h>
53 #include <machine/i4b_ioctl.h>
55 #include <i4b/i4b_debug.h>
56 #include <i4b/i4b_ioctl.h>
59 #include <i4b/include/i4b_l3l4.h>
60 #include <i4b/include/i4b_global.h>
62 #include <i4b/layer4/i4b_l4.h>
64 call_desc_t call_desc[N_CALL_DESC]; /* call descriptor array */
66 static unsigned int get_cdid(void);
68 int nctrl; /* number of attached controllers */
70 #if (defined(__NetBSD__) && __NetBSD_Version__ >= 104230000) || defined(__FreeBSD__)
71 void i4b_init_callout(call_desc_t *);
74 /*---------------------------------------------------------------------------*
75 * return a new unique call descriptor id
76 * --------------------------------------
77 * returns a new calldescriptor id which is used to uniquely identyfy
78 * a single call in the communication between kernel and userland.
79 * this cdid is then used to associate a calldescriptor with an id.
80 *---------------------------------------------------------------------------*/
84 static unsigned int cdid_count = 0;
95 if(cdid_count == CDID_UNUSED) /* zero is invalid */
97 else if(cdid_count > CDID_MAX) /* wraparound ? */
100 /* check if id already in use */
102 for(i=0; i < N_CALL_DESC; i++)
104 if(call_desc[i].cdid == cdid_count)
116 /*---------------------------------------------------------------------------*
117 * reserve a calldescriptor for later usage
118 * ----------------------------------------
119 * searches the calldescriptor array until an unused
120 * descriptor is found, gets a new calldescriptor id
121 * and reserves it by putting the id into the cdid field.
122 * returns pointer to the calldescriptor.
123 *---------------------------------------------------------------------------*/
135 for(i=0; i < N_CALL_DESC; i++)
137 if(call_desc[i].cdid == CDID_UNUSED)
139 bzero(&call_desc[i], sizeof(call_desc_t)); /* clear it */
140 call_desc[i].cdid = get_cdid(); /* fill in new cdid */
141 cd = &(call_desc[i]); /* get pointer to descriptor */
142 NDBGL4(L4_MSG, "found free cd - index=%d cdid=%u",
143 i, call_desc[i].cdid);
151 panic("reserve_cd: no free call descriptor available!");
153 #if (defined(__NetBSD__) && __NetBSD_Version__ >= 104230000) || defined(__FreeBSD__)
154 i4b_init_callout(cd);
160 /*---------------------------------------------------------------------------*
161 * free a calldescriptor
162 * ---------------------
163 * free a unused calldescriptor by giving address of calldescriptor
164 * and writing a 0 into the cdid field marking it as unused.
165 *---------------------------------------------------------------------------*/
167 freecd_by_cd(call_desc_t *cd)
172 for(i=0; i < N_CALL_DESC; i++)
174 if( (call_desc[i].cdid != CDID_UNUSED) &&
175 (&(call_desc[i]) == cd) )
177 NDBGL4(L4_MSG, "releasing cd - index=%d cdid=%u cr=%d",
178 i, call_desc[i].cdid, cd->cr);
179 call_desc[i].cdid = CDID_UNUSED;
185 panic("freecd_by_cd: ERROR, cd not found, cr = %d\n", cd->cr);
190 /*---------------------------------------------------------------------------*
191 * return pointer to calldescriptor by giving the calldescriptor id
192 * ----------------------------------------------------------------
193 * lookup a calldescriptor in the calldescriptor array by looking
194 * at the cdid field. return pointer to calldescriptor if found,
195 * else return NULL if not found.
196 *---------------------------------------------------------------------------*/
198 cd_by_cdid(unsigned int cdid)
202 for(i=0; i < N_CALL_DESC; i++)
204 if(call_desc[i].cdid == cdid)
206 NDBGL4(L4_MSG, "found cdid - index=%d cdid=%u cr=%d",
207 i, call_desc[i].cdid, call_desc[i].cr);
208 #if (defined(__NetBSD__) && __NetBSD_Version__ >= 104230000) || defined(__FreeBSD__)
209 i4b_init_callout(&call_desc[i]);
211 return(&(call_desc[i]));
217 /*---------------------------------------------------------------------------*
218 * search calldescriptor
219 * ---------------------
220 * This routine searches for the calldescriptor for a passive controller
221 * given by unit number, callreference and callreference flag.
222 * It returns a pointer to the calldescriptor if found, else a NULL.
223 *---------------------------------------------------------------------------*/
225 cd_by_unitcr(int unit, int cr, int crf)
229 for(i=0; i < N_CALL_DESC; i++)
231 if((call_desc[i].cdid != CDID_UNUSED) &&
232 (ctrl_desc[call_desc[i].controller].ctrl_type == CTRL_PASSIVE) &&
233 (ctrl_desc[call_desc[i].controller].unit == unit) &&
234 (call_desc[i].cr == cr) &&
235 (call_desc[i].crflag == crf) )
237 NDBGL4(L4_MSG, "found cd, index=%d cdid=%u cr=%d",
238 i, call_desc[i].cdid, call_desc[i].cr);
239 #if (defined(__NetBSD__) && __NetBSD_Version__ >= 104230000) || defined(__FreeBSD__)
240 i4b_init_callout(&call_desc[i]);
242 return(&(call_desc[i]));
248 /*---------------------------------------------------------------------------*
249 * generate 7 bit "random" number used for outgoing Call Reference
250 *---------------------------------------------------------------------------*/
252 get_rand_cr(int unit)
255 static u_char val, retval;
256 static int called = 42;
260 for(i=0; i < 50 ; i++, val++)
264 #if defined(__FreeBSD__)
267 read_random((char *)&val, sizeof(val));
269 val = (u_char)random();
270 #endif /* RANDOMDEV */
275 val ^= (time.tv_sec >> 8) ^ time.tv_usec;
277 val ^= time.tv_sec ^ (time.tv_usec >> 8);
282 if(retval == 0 || retval == 0x7f)
285 for(j=0; j < N_CALL_DESC; j++)
287 if( (call_desc[j].cdid != CDID_UNUSED) &&
288 (call_desc[j].cr == retval) )
301 /*---------------------------------------------------------------------------*
302 * initialize the callout handles for FreeBSD
303 *---------------------------------------------------------------------------*/
304 #if (defined(__NetBSD__) && __NetBSD_Version__ >= 104230000) || defined(__FreeBSD__)
306 i4b_init_callout(call_desc_t *cd)
308 if(cd->callouts_inited == 0)
311 callout_handle_init(&cd->idle_timeout_handle);
312 callout_handle_init(&cd->T303_callout);
313 callout_handle_init(&cd->T305_callout);
314 callout_handle_init(&cd->T308_callout);
315 callout_handle_init(&cd->T309_callout);
316 callout_handle_init(&cd->T310_callout);
317 callout_handle_init(&cd->T313_callout);
318 callout_handle_init(&cd->T400_callout);
320 callout_init(&cd->idle_timeout_handle);
321 callout_init(&cd->T303_callout);
322 callout_init(&cd->T305_callout);
323 callout_init(&cd->T308_callout);
324 callout_init(&cd->T309_callout);
325 callout_init(&cd->T310_callout);
326 callout_init(&cd->T313_callout);
327 callout_init(&cd->T400_callout);
329 cd->callouts_inited = 1;
334 /*---------------------------------------------------------------------------*
336 *---------------------------------------------------------------------------*/
338 i4b_l4_daemon_attached(void)
344 for(i=0; i < nctrl; i++)
346 /*XXX*/ if(ctrl_desc[i].ctrl_type == CTRL_PASSIVE)
348 NDBGL4(L4_MSG, "CMR_DOPEN sent to unit %d", ctrl_desc[i].unit);
349 (*ctrl_desc[i].N_MGMT_COMMAND)(ctrl_desc[i].unit, CMR_DOPEN, 0);
355 /*---------------------------------------------------------------------------*
357 *---------------------------------------------------------------------------*/
359 i4b_l4_daemon_detached(void)
365 for(i=0; i < nctrl; i++)
367 /*XXX*/ if(ctrl_desc[i].ctrl_type == CTRL_PASSIVE)
369 NDBGL4(L4_MSG, "CMR_DCLOSE sent to unit %d", ctrl_desc[i].unit);
370 (*ctrl_desc[i].N_MGMT_COMMAND)(ctrl_desc[i].unit, CMR_DCLOSE, 0);
376 #ifdef I4B_CD_DEBUG_PRINT
378 extern char *print_l3state(call_desc_t *cd);
380 void i4b_print_cdp(call_desc_t *cdp);
381 void i4b_print_cdx(int index);
382 void i4b_print_cda(void);
383 void i4b_print_cdaa(void);
385 /*---------------------------------------------------------------------------*
386 * print a call descriptor by cd-pointer
387 *---------------------------------------------------------------------------*/
389 i4b_print_cdp(call_desc_t *cdp)
391 if((cdp > &(call_desc[N_CALL_DESC])) || (cdp < &(call_desc[0])))
393 printf("i4b_print_cd: cdp out of range!\n");
397 printf("i4b_print_cd: printing call descriptor %d at 0x%lx:\n", cdp - (&(call_desc[0])), (unsigned long)cdp);
399 printf(" cdid = %d\n", cdp->cdid);
400 printf(" controller = %d (u=%d, dl=%d, b1=%d, b2=%d)\n",
402 ctrl_desc[cdp->controller].unit,
403 ctrl_desc[cdp->controller].dl_est,
404 ctrl_desc[cdp->controller].bch_state[CHAN_B1],
405 ctrl_desc[cdp->controller].bch_state[CHAN_B2]);
406 printf(" cr = 0x%02x\n", cdp->cr);
407 printf(" crflag = %d\n", cdp->crflag);
408 printf(" channelid = %d\n", cdp->channelid);
409 printf(" bprot = %d\n", cdp->bprot);
410 printf(" driver = %d\n", cdp->driver);
411 printf(" driver_unit = %d\n", cdp->driver_unit);
412 printf(" call_state = %d\n", cdp->call_state);
413 printf(" Q931state = %s\n", print_l3state(cdp));
414 printf(" event = %d\n", cdp->event);
415 printf(" response = %d\n", cdp->response);
416 printf(" T303 = %d\n", cdp->T303);
417 printf("T303_first_to = %d\n", cdp->T303_first_to);
418 printf(" T305 = %d\n", cdp->T305);
419 printf(" T308 = %d\n", cdp->T308);
420 printf("T308_first_to = %d\n", cdp->T308_first_to);
421 printf(" T309 = %d\n", cdp->T309);
422 printf(" T310 = %d\n", cdp->T310);
423 printf(" T313 = %d\n", cdp->T313);
424 printf(" T400 = %d\n", cdp->T400);
425 printf(" dir = %s\n", cdp->dir == DIR_OUTGOING ? "out" : "in");
428 /*---------------------------------------------------------------------------*
429 * print a call descriptor by index
430 *---------------------------------------------------------------------------*/
432 i4b_print_cdx(int index)
434 if(index >= N_CALL_DESC)
436 printf("i4b_print_cdx: index %d >= N_CALL_DESC %d\n", index, N_CALL_DESC);
439 i4b_print_cdp(&(call_desc[index]));
442 /*---------------------------------------------------------------------------*
443 * print all call descriptors
444 *---------------------------------------------------------------------------*/
450 for(i=0; i < N_CALL_DESC; i++)
452 i4b_print_cdp(&(call_desc[i]));
456 /*---------------------------------------------------------------------------*
457 * print all active call descriptors
458 *---------------------------------------------------------------------------*/
464 for(i=0; i < N_CALL_DESC; i++)
466 if(call_desc[i].cdid != CDID_UNUSED)
468 i4b_print_cdp(&(call_desc[i]));
473 #endif /* I4B_CD_DEBUG_PRINT */
475 #endif /* NI4BQ931 > 0 */