2 * Copyright (c) 1997, 2001 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 daemon - controller state support routines
28 * ----------------------------------------------
30 * $FreeBSD: src/usr.sbin/i4b/isdnd/controller.c,v 1.6.2.4 2002/04/25 12:30:06 gj Exp $
32 * last edit-date: [Mon Dec 10 11:32:05 2001]
34 *---------------------------------------------------------------------------*/
36 #include <sys/types.h>
42 init_controller_state(int controller, int ctrl_type, int card_type, int tei, int nbch);
44 /*---------------------------------------------------------------------------*
45 * get name of a controller
46 *---------------------------------------------------------------------------*/
48 name_of_controller(int ctrl_type, int card_type)
50 static char *passive_card[] = {
54 "AVM A1 or Fritz!Card",
57 "USRobotics Sportster ISDN TA",
58 "Dr. Neuhaus NICCY Go@",
59 "Sedlbauer win speed",
61 "ISDN Master, MasterII or Blaster",
62 "AVM PCMCIA Fritz!Card",
63 "ELSA QuickStep 1000pro/ISA",
64 "ELSA QuickStep 1000pro/PCI",
66 "ELSA MicroLink ISDN/MC",
67 "ELSA MicroLink MCall",
72 "Siemens I-Surf 2.0 PnP",
73 "Asuscom ISDNlink 128K PnP",
74 "ASUSCOM P-IN100-ST-D (Winbond W6692)",
77 "TELEINT ISDN SPEED No. 1",
78 "Cologne Chip HFC-S PCI based",
79 "Traverse Tech NETjet-S / Teles PCI-TJ",
80 "Eicon.Diehl DIVA 2.0 / 2.02 ISA PnP",
81 "Compaq Microcom 610",
82 "AVM Fritz!Card PCI Version 2",
85 static char *daic_card[] = {
92 static char *capi_card[] = {
98 if(ctrl_type == CTRL_PASSIVE)
100 int index = card_type - CARD_TYPEP_8;
101 if (index >= 0 && index < (sizeof passive_card / sizeof passive_card[0]))
102 return passive_card[index];
104 else if(ctrl_type == CTRL_DAIC)
106 int index = card_type - CARD_TYPEA_DAIC_S;
107 if (index >= 0 && index < (sizeof daic_card / sizeof daic_card[0] ))
108 return daic_card[index];
110 else if(ctrl_type == CTRL_TINADD)
112 return "Stollmann tina-dd";
114 else if(ctrl_type == CTRL_CAPI)
116 int index = card_type - CARD_TYPEC_AVM_T1_PCI;
117 if (index >= 0 && index < (sizeof capi_card / sizeof capi_card[0] ))
118 return capi_card[index];
121 return "unknown card type";
124 /*---------------------------------------------------------------------------*
125 * init controller state array
126 *---------------------------------------------------------------------------*/
128 init_controller(void)
132 msg_ctrl_info_req_t mcir;
134 for(i=0; i < max; i++)
138 if((ioctl(isdnfd, I4B_CTRL_INFO_REQ, &mcir)) < 0)
140 log(LL_ERR, "init_controller: ioctl I4B_CTRL_INFO_REQ failed: %s", strerror(errno));
144 if((ncontroller = max = mcir.ncontroller) == 0)
146 log(LL_ERR, "init_controller: no ISDN controller found!");
150 if(mcir.ctrl_type == -1 || mcir.card_type == -1)
152 log(LL_ERR, "init_controller: ctrl/card is invalid!");
156 /* init controller tab */
158 if((init_controller_state(i, mcir.ctrl_type, mcir.card_type, mcir.tei, mcir.nbch)) == ERROR)
160 log(LL_ERR, "init_controller: init_controller_state for controller %d failed", i);
164 DBGL(DL_RCCF, (log(LL_DBG, "init_controller: found %d ISDN controller(s)", max)));
167 /*--------------------------------------------------------------------------*
168 * init controller state table entry
169 *--------------------------------------------------------------------------*/
171 init_controller_state(int controller, int ctrl_type, int card_type, int tei,
176 if((controller < 0) || (controller >= ncontroller))
178 log(LL_ERR, "init_controller_state: invalid controller number [%d]!", controller);
182 /* init controller tab */
186 if((card_type > CARD_TYPEP_UNK) &&
187 (card_type <= CARD_TYPEP_MAX))
189 isdn_ctrl_tab[controller].ctrl_type = ctrl_type;
190 isdn_ctrl_tab[controller].card_type = card_type;
191 isdn_ctrl_tab[controller].state = CTRL_UP;
195 log(LL_ERR, "init_controller_state: unknown card type %d", card_type);
201 isdn_ctrl_tab[controller].ctrl_type = ctrl_type;
202 isdn_ctrl_tab[controller].card_type = card_type;
203 isdn_ctrl_tab[controller].state = CTRL_DOWN;
207 isdn_ctrl_tab[controller].ctrl_type = ctrl_type;
208 isdn_ctrl_tab[controller].card_type = 0;
209 isdn_ctrl_tab[controller].state = CTRL_DOWN;
213 isdn_ctrl_tab[controller].ctrl_type = ctrl_type;
214 isdn_ctrl_tab[controller].card_type = card_type;
215 isdn_ctrl_tab[controller].state = CTRL_UP;
219 log(LL_ERR, "init_controller_state: unknown controller type %d", ctrl_type);
223 isdn_ctrl_tab[controller].nbch = nbch;
224 isdn_ctrl_tab[controller].freechans = nbch;
225 for (i = 0; i < nbch; i++)
226 isdn_ctrl_tab[controller].stateb[i] = CHAN_IDLE;
227 isdn_ctrl_tab[controller].tei = tei;
228 isdn_ctrl_tab[controller].l1stat = LAYER_IDLE;
229 isdn_ctrl_tab[controller].l2stat = LAYER_IDLE;
231 log(LL_DMN, "init_controller_state: controller %d is %s",
233 name_of_controller(isdn_ctrl_tab[controller].ctrl_type,
234 isdn_ctrl_tab[controller].card_type));
239 /*--------------------------------------------------------------------------*
240 * init active or capi controller
241 *--------------------------------------------------------------------------*/
243 init_active_controller(void)
248 char cmdbuf[MAXPATHLEN+128];
250 for(controller = 0; controller < ncontroller; controller++)
252 if(isdn_ctrl_tab[controller].ctrl_type == CTRL_TINADD)
254 DBGL(DL_RCCF, (log(LL_DBG, "init_active_controller, tina-dd %d: executing [%s %d]", unit, tinainitprog, unit)));
256 snprintf(cmdbuf, sizeof(cmdbuf), "%s %d", tinainitprog, unit);
258 if((ret = system(cmdbuf)) != 0)
260 log(LL_ERR, "init_active_controller, tina-dd %d: %s returned %d!", unit, tinainitprog, ret);
266 * Generic microcode loading. If a controller has
267 * defined a microcode file, load it using the
268 * I4B_CTRL_DOWNLOAD ioctl.
271 if(isdn_ctrl_tab[controller].firmware != NULL)
274 struct isdn_dr_prot idp;
275 struct isdn_download_request idr;
277 fd = open(isdn_ctrl_tab[controller].firmware, O_RDONLY);
279 log(LL_ERR, "init_active_controller %d: open %s: %s!",
280 controller, isdn_ctrl_tab[controller].firmware,
285 idp.bytecount = lseek(fd, 0, SEEK_END);
286 idp.microcode = mmap(0, idp.bytecount, PROT_READ,
288 if (idp.microcode == MAP_FAILED) {
289 log(LL_ERR, "init_active_controller %d: mmap %s: %s!",
290 controller, isdn_ctrl_tab[controller].firmware,
295 DBGL(DL_RCCF, (log(LL_DBG, "init_active_controller %d: loading firmware from [%s]", controller, isdn_ctrl_tab[controller].firmware)));
297 idr.controller = controller;
299 idr.protocols = &idp;
301 ret = ioctl(isdnfd, I4B_CTRL_DOWNLOAD, &idr, sizeof(idr));
303 log(LL_ERR, "init_active_controller %d: load %s: %s!",
304 controller, isdn_ctrl_tab[controller].firmware,
309 munmap(idp.microcode, idp.bytecount);
315 /*--------------------------------------------------------------------------*
316 * init controller D-channel ISDN protocol
317 *--------------------------------------------------------------------------*/
319 init_controller_protocol(void)
324 for(controller = 0; controller < ncontroller; controller++)
326 mpi.controller = controller;
327 mpi.protocol = isdn_ctrl_tab[controller].protocol;
329 if((ioctl(isdnfd, I4B_PROT_IND, &mpi)) < 0)
331 log(LL_ERR, "init_controller_protocol: ioctl I4B_PROT_IND failed: %s", strerror(errno));
337 /*--------------------------------------------------------------------------*
338 * set controller state to UP/DOWN
339 *--------------------------------------------------------------------------*/
341 set_controller_state(int controller, int state)
343 if((controller < 0) || (controller >= ncontroller))
345 log(LL_ERR, "set_controller_state: invalid controller number [%d]!", controller);
351 isdn_ctrl_tab[controller].state = CTRL_UP;
352 DBGL(DL_CNST, (log(LL_DBG, "set_controller_state: controller [%d] set UP!", controller)));
354 else if (state == CTRL_DOWN)
356 isdn_ctrl_tab[controller].state = CTRL_DOWN;
357 DBGL(DL_CNST, (log(LL_DBG, "set_controller_state: controller [%d] set DOWN!", controller)));
361 log(LL_ERR, "set_controller_state: invalid controller state [%d]!", state);
367 /*--------------------------------------------------------------------------*
368 * get controller state
369 *--------------------------------------------------------------------------*/
371 get_controller_state(int controller)
373 if((controller < 0) || (controller >= ncontroller))
375 log(LL_ERR, "set_controller_state: invalid controller number [%d]!", controller);
378 return(isdn_ctrl_tab[controller].state);
381 /*--------------------------------------------------------------------------*
382 * decrement number of free channels for controller
383 *--------------------------------------------------------------------------*/
385 decr_free_channels(int controller)
387 if((controller < 0) || (controller >= ncontroller))
389 log(LL_ERR, "decr_free_channels: invalid controller number [%d]!", controller);
392 if(isdn_ctrl_tab[controller].freechans > 0)
394 (isdn_ctrl_tab[controller].freechans)--;
395 DBGL(DL_CNST, (log(LL_DBG, "decr_free_channels: ctrl %d, now %d chan free", controller, isdn_ctrl_tab[controller].freechans)));
400 log(LL_ERR, "decr_free_channels: controller [%d] already 0 free chans!", controller);
405 /*--------------------------------------------------------------------------*
406 * increment number of free channels for controller
407 *--------------------------------------------------------------------------*/
409 incr_free_channels(int controller)
411 if((controller < 0) || (controller >= ncontroller))
413 log(LL_ERR, "incr_free_channels: invalid controller number [%d]!", controller);
416 if(isdn_ctrl_tab[controller].freechans < isdn_ctrl_tab[controller].nbch)
418 (isdn_ctrl_tab[controller].freechans)++;
419 DBGL(DL_CNST, (log(LL_DBG, "incr_free_channels: ctrl %d, now %d chan free", controller, isdn_ctrl_tab[controller].freechans)));
424 log(LL_ERR, "incr_free_channels: controller [%d] already %d free chans!", controller, isdn_ctrl_tab[controller].nbch);
429 /*--------------------------------------------------------------------------*
430 * get number of free channels for controller
431 *--------------------------------------------------------------------------*/
433 get_free_channels(int controller)
435 if((controller < 0) || (controller >= ncontroller))
437 log(LL_ERR, "get_free_channels: invalid controller number [%d]!", controller);
440 DBGL(DL_CNST, (log(LL_DBG, "get_free_channels: ctrl %d, %d chan free", controller, isdn_ctrl_tab[controller].freechans)));
441 return(isdn_ctrl_tab[controller].freechans);
444 /*--------------------------------------------------------------------------*
445 * set channel state to busy
446 *--------------------------------------------------------------------------*/
448 set_channel_busy(int controller, int channel)
450 if((controller < 0) || (controller >= ncontroller))
452 log(LL_ERR, "set_channel_busy: invalid controller number [%d]!", controller);
456 if ((channel < 0) || (channel >= isdn_ctrl_tab[controller].nbch))
458 log(LL_ERR, "set_channel_busy: controller [%d] invalid channel [%d]!", controller, channel);
462 if(isdn_ctrl_tab[controller].stateb[channel] == CHAN_RUN)
464 DBGL(DL_CNST, (log(LL_DBG, "set_channel_busy: controller [%d] channel B%d already busy!", controller, channel+1)));
468 isdn_ctrl_tab[controller].stateb[channel] = CHAN_RUN;
469 DBGL(DL_CNST, (log(LL_DBG, "set_channel_busy: controller [%d] channel B%d set to BUSY!", controller, channel+1)));
474 /*--------------------------------------------------------------------------*
475 * set channel state to idle
476 *--------------------------------------------------------------------------*/
478 set_channel_idle(int controller, int channel)
480 if((controller < 0) || (controller >= ncontroller))
482 log(LL_ERR, "set_channel_idle: invalid controller number [%d]!", controller);
486 if ((channel < 0) || (channel >= isdn_ctrl_tab[controller].nbch))
488 log(LL_ERR, "set_channel_busy: controller [%d] invalid channel [%d]!", controller, channel);
492 if (isdn_ctrl_tab[controller].stateb[channel] == CHAN_IDLE)
494 DBGL(DL_CNST, (log(LL_DBG, "set_channel_idle: controller [%d] channel B%d already idle!", controller, channel+1)));
498 isdn_ctrl_tab[controller].stateb[channel] = CHAN_IDLE;
499 DBGL(DL_CNST, (log(LL_DBG, "set_channel_idle: controller [%d] channel B%d set to IDLE!", controller, channel+1)));
504 /*--------------------------------------------------------------------------*
505 * return channel state
506 *--------------------------------------------------------------------------*/
508 ret_channel_state(int controller, int channel)
510 if((controller < 0) || (controller >= ncontroller))
512 log(LL_ERR, "ret_channel_state: invalid controller number [%d]!", controller);
516 if ((channel < 0) || (channel >= isdn_ctrl_tab[controller].nbch))
518 log(LL_ERR, "set_channel_busy: controller [%d] invalid channel [%d]!", controller, channel);
522 return(isdn_ctrl_tab[controller].stateb[channel]);