]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - usr.sbin/i4b/isdnd/controller.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / usr.sbin / i4b / isdnd / controller.c
1 /*
2  * Copyright (c) 1997, 2001 Hellmuth Michaelis. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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
23  * SUCH DAMAGE.
24  *
25  *---------------------------------------------------------------------------
26  *
27  *      i4b daemon - controller state support routines
28  *      ----------------------------------------------
29  *
30  * $FreeBSD$
31  *
32  *      last edit-date: [Sat May 13 13:24:05 2006]
33  *
34  *---------------------------------------------------------------------------*/
35
36 #include <sys/types.h>
37 #include <sys/mman.h>
38
39 #include "isdnd.h"
40
41 static int
42 init_controller_state(int controller, int ctrl_type, int card_type, int tei, int nbch);
43
44 /*---------------------------------------------------------------------------*
45  *      get name of a controller
46  *---------------------------------------------------------------------------*/
47 const char *
48 name_of_controller(int ctrl_type, int card_type)
49 {
50         static char *passive_card[] = {
51                 "Teles S0/8",
52                 "Teles S0/16",
53                 "Teles S0/16.3",
54                 "AVM A1 or Fritz!Card",
55                 "Teles S0/16.3 PnP",
56                 "Creatix S0 PnP",
57                 "USRobotics Sportster ISDN TA",
58                 "Dr. Neuhaus NICCY Go@",
59                 "Sedlbauer win speed",
60                 "Dynalink IS64PH",
61                 "ISDN Master, MasterII or Blaster",
62                 "AVM PCMCIA Fritz!Card",
63                 "ELSA QuickStep 1000pro/ISA",
64                 "ELSA QuickStep 1000pro/PCI",
65                 "Siemens I-Talk",
66                 "ELSA MicroLink ISDN/MC",
67                 "ELSA MicroLink MCall",
68                 "ITK ix1 micro",
69                 "AVM Fritz!Card PCI",
70                 "ELSA PCC-16",
71                 "AVM Fritz!Card PnP",           
72                 "Siemens I-Surf 2.0 PnP",               
73                 "Asuscom ISDNlink 128K PnP",
74                 "ASUSCOM P-IN100-ST-D (Winbond W6692)",
75                 "Teles S0/16.3c PnP",
76                 "AcerISDN P10 PnP",
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",
83         };
84
85         static char *daic_card[] = {
86                 "EICON.Diehl S",
87                 "EICON.Diehl SX/SXn",
88                 "EICON.Diehl SCOM",
89                 "EICON.Diehl QUADRO",
90         };
91
92         static char *capi_card[] = {
93                 "AVM T1 PCI",
94                 "AVM B1 PCI",
95                 "AVM B1 ISA",
96         };
97
98         static char *capimgr_card[] = {
99                 "CAPI manager driven board"
100         };
101
102         if(ctrl_type == CTRL_PASSIVE)
103         {
104                 int index = card_type - CARD_TYPEP_8;
105                 if (index >= 0 && index < (sizeof passive_card / sizeof passive_card[0]))
106                         return passive_card[index];
107         }
108         else if(ctrl_type == CTRL_DAIC)
109         {
110                 int index = card_type - CARD_TYPEA_DAIC_S;
111                 if (index >= 0 && index < (sizeof daic_card / sizeof daic_card[0] ))
112                         return daic_card[index];
113         }
114         else if(ctrl_type == CTRL_TINADD)
115         {
116                 return "Stollmann tina-dd";
117         }
118         else if(ctrl_type == CTRL_CAPI)
119         {
120                 int index = card_type - CARD_TYPEC_AVM_T1_PCI;
121                 if (index >= 0 && index < (sizeof capi_card / sizeof capi_card[0] ))
122                         return capi_card[index];
123         }
124         else if(ctrl_type == CTRL_CAPIMGR)
125         {
126                 return capimgr_card[0];
127         }
128
129         return "unknown card type";
130 }
131  
132 /*---------------------------------------------------------------------------*
133  *      init controller state array
134  *---------------------------------------------------------------------------*/
135 void
136 init_controller(void)
137 {
138         int i;
139         int max = 1;
140         msg_ctrl_info_req_t mcir;
141         
142         for(i=0; i < max; i++)
143         {
144                 mcir.controller = i;
145                 
146                 if((ioctl(isdnfd, I4B_CTRL_INFO_REQ, &mcir)) < 0)
147                 {
148                         llog(LL_ERR, "init_controller: ioctl I4B_CTRL_INFO_REQ failed: %s", strerror(errno));
149                         do_exit(1);
150                 }
151
152                 if((ncontroller = max = mcir.ncontroller) == 0)
153                 {
154                         llog(LL_ERR, "init_controller: no ISDN controller found!");
155                         do_exit(1);
156                 }
157
158                 if(mcir.ctrl_type == -1 || mcir.card_type == -1)
159                 {
160                         llog(LL_ERR, "init_controller: ctrl/card is invalid!");
161                         do_exit(1);
162                 }
163
164                 /* init controller tab */
165
166                 if((init_controller_state(i, mcir.ctrl_type, mcir.card_type, mcir.tei, mcir.nbch)) == ERROR)
167                 {
168                         llog(LL_ERR, "init_controller: init_controller_state for controller %d failed", i);
169                         do_exit(1);
170                 }
171         }
172         DBGL(DL_RCCF, (llog(LL_DBG, "init_controller: found %d ISDN controller(s)", max)));
173 }
174
175 /*--------------------------------------------------------------------------*
176  *      init controller state table entry
177  *--------------------------------------------------------------------------*/
178 static int
179 init_controller_state(int controller, int ctrl_type, int card_type, int tei,
180                       int nbch)
181 {
182         int i;
183
184         if((controller < 0) || (controller >= ncontroller))
185         {
186                 llog(LL_ERR, "init_controller_state: invalid controller number [%d]!", controller);
187                 return(ERROR);
188         }
189         
190         /* init controller tab */
191                 
192         switch (ctrl_type) {
193         case CTRL_PASSIVE:
194                 if((card_type > CARD_TYPEP_UNK) &&
195                    (card_type <= CARD_TYPEP_MAX))
196                 {
197                         isdn_ctrl_tab[controller].ctrl_type = ctrl_type;
198                         isdn_ctrl_tab[controller].card_type = card_type;
199                         isdn_ctrl_tab[controller].state = CTRL_UP;
200                 }
201                 else
202                 {
203                         llog(LL_ERR, "init_controller_state: unknown card type %d", card_type);
204                         return(ERROR);
205                 }
206                 break;
207                 
208         case CTRL_DAIC:
209                 isdn_ctrl_tab[controller].ctrl_type = ctrl_type;
210                 isdn_ctrl_tab[controller].card_type = card_type;
211                 isdn_ctrl_tab[controller].state = CTRL_DOWN;
212                 break;
213
214         case CTRL_TINADD:
215                 isdn_ctrl_tab[controller].ctrl_type = ctrl_type;
216                 isdn_ctrl_tab[controller].card_type = 0;
217                 isdn_ctrl_tab[controller].state = CTRL_DOWN;
218                 break;
219
220         case CTRL_CAPI:
221                 isdn_ctrl_tab[controller].ctrl_type = ctrl_type;
222                 isdn_ctrl_tab[controller].card_type = card_type;
223                 isdn_ctrl_tab[controller].state = CTRL_UP;
224                 break;
225
226         case CTRL_CAPIMGR:
227                 isdn_ctrl_tab[controller].ctrl_type = ctrl_type;
228                 isdn_ctrl_tab[controller].card_type = card_type;
229                 isdn_ctrl_tab[controller].state = CTRL_UP;
230                 break;
231
232         default:
233                 llog(LL_ERR, "init_controller_state: unknown controller type %d", ctrl_type);
234                 return(ERROR);
235         }
236         
237         isdn_ctrl_tab[controller].nbch = nbch;
238         isdn_ctrl_tab[controller].freechans = nbch;
239         for (i = 0; i < nbch; i++)
240             isdn_ctrl_tab[controller].stateb[i] = CHAN_IDLE;
241                 isdn_ctrl_tab[controller].tei = tei;    
242                 isdn_ctrl_tab[controller].l1stat = LAYER_IDLE;
243                 isdn_ctrl_tab[controller].l2stat = LAYER_IDLE;
244
245                 llog(LL_DMN, "init_controller_state: controller %d is %s",
246                   controller,
247                   name_of_controller(isdn_ctrl_tab[controller].ctrl_type,
248                                      isdn_ctrl_tab[controller].card_type));
249                 
250         return(GOOD);
251 }       
252
253 /*--------------------------------------------------------------------------*
254  *      init active or capi controller
255  *--------------------------------------------------------------------------*/
256 void
257 init_active_controller(void)
258 {
259         int ret;
260         int unit = 0;
261         int controller;
262         char cmdbuf[MAXPATHLEN+128];
263
264         for(controller = 0; controller < ncontroller; controller++)
265         {
266                 if(isdn_ctrl_tab[controller].ctrl_type == CTRL_TINADD)
267                 {
268                         DBGL(DL_RCCF, (llog(LL_DBG, "init_active_controller, tina-dd %d: executing [%s %d]", unit, tinainitprog, unit)));
269                         
270                         snprintf(cmdbuf, sizeof(cmdbuf), "%s %d", tinainitprog, unit);
271
272                         if((ret = system(cmdbuf)) != 0)
273                         {
274                                 llog(LL_ERR, "init_active_controller, tina-dd %d: %s returned %d!", unit, tinainitprog, ret);
275                                 do_exit(1);
276                         }
277                 }
278
279                 /*
280                  *  Generic microcode loading. If a controller has
281                  *  defined a microcode file, load it using the
282                  *  I4B_CTRL_DOWNLOAD ioctl.
283                  */
284                 
285                 if(isdn_ctrl_tab[controller].firmware != NULL)
286                 {
287                     int fd, ret;
288                     struct isdn_dr_prot idp;
289                     struct isdn_download_request idr;
290
291                     fd = open(isdn_ctrl_tab[controller].firmware, O_RDONLY);
292                     if (fd < 0) {
293                         llog(LL_ERR, "init_active_controller %d: open %s: %s!",
294                             controller, isdn_ctrl_tab[controller].firmware,
295                             strerror(errno));
296                         do_exit(1);
297                     }
298
299                     idp.bytecount = lseek(fd, 0, SEEK_END);
300                     idp.microcode = mmap(0, idp.bytecount, PROT_READ,
301                                          MAP_SHARED, fd, 0);
302                     if (idp.microcode == MAP_FAILED) {
303                         llog(LL_ERR, "init_active_controller %d: mmap %s: %s!",
304                             controller, isdn_ctrl_tab[controller].firmware,
305                             strerror(errno));
306                         do_exit(1);
307                     }
308                     
309                     DBGL(DL_RCCF, (llog(LL_DBG, "init_active_controller %d: loading firmware from [%s]", controller, isdn_ctrl_tab[controller].firmware)));
310
311                     idr.controller = controller;
312                     idr.numprotos = 1;
313                     idr.protocols = &idp;
314                     
315                     ret = ioctl(isdnfd, I4B_CTRL_DOWNLOAD, &idr, sizeof(idr));
316                     if (ret) {
317                         llog(LL_ERR, "init_active_controller %d: load %s: %s!",
318                             controller, isdn_ctrl_tab[controller].firmware,
319                             strerror(errno));
320                         do_exit(1);
321                     }
322
323                     munmap(idp.microcode, idp.bytecount);
324                     close(fd);
325                 }
326         }
327 }       
328
329 /*--------------------------------------------------------------------------*
330  *      init controller D-channel ISDN protocol
331  *--------------------------------------------------------------------------*/
332 void
333 init_controller_protocol(void)
334 {
335         int controller;
336         msg_prot_ind_t mpi;
337
338         for(controller = 0; controller < ncontroller; controller++)
339         {
340                 mpi.controller = controller;
341                 mpi.protocol = isdn_ctrl_tab[controller].protocol;
342                 
343                 if((ioctl(isdnfd, I4B_PROT_IND, &mpi)) < 0)
344                 {
345                         llog(LL_ERR, "init_controller_protocol: ioctl I4B_PROT_IND failed: %s", strerror(errno));
346                         do_exit(1);
347                 }
348         }
349 }
350
351 /*--------------------------------------------------------------------------*
352  *      set controller state to UP/DOWN
353  *--------------------------------------------------------------------------*/
354 int
355 set_controller_state(int controller, int state)
356 {
357         if((controller < 0) || (controller >= ncontroller))
358         {
359                 llog(LL_ERR, "set_controller_state: invalid controller number [%d]!", controller);
360                 return(ERROR);
361         }
362
363         if(state == CTRL_UP)
364         {
365                 isdn_ctrl_tab[controller].state = CTRL_UP;
366                 DBGL(DL_CNST, (llog(LL_DBG, "set_controller_state: controller [%d] set UP!", controller)));
367         }
368         else if (state == CTRL_DOWN)
369         {
370                 isdn_ctrl_tab[controller].state = CTRL_DOWN;
371                 DBGL(DL_CNST, (llog(LL_DBG, "set_controller_state: controller [%d] set DOWN!", controller)));
372         }
373         else
374         {
375                 llog(LL_ERR, "set_controller_state: invalid controller state [%d]!", state);
376                 return(ERROR);
377         }
378         return(GOOD);
379 }               
380         
381 /*--------------------------------------------------------------------------*
382  *      get controller state
383  *--------------------------------------------------------------------------*/
384 int
385 get_controller_state(int controller)
386 {
387         if((controller < 0) || (controller >= ncontroller))
388         {
389                 llog(LL_ERR, "set_controller_state: invalid controller number [%d]!", controller);
390                 return(ERROR);
391         }
392         return(isdn_ctrl_tab[controller].state);
393 }               
394
395 /*--------------------------------------------------------------------------*
396  *      decrement number of free channels for controller
397  *--------------------------------------------------------------------------*/
398 int
399 decr_free_channels(int controller)
400 {
401         if((controller < 0) || (controller >= ncontroller))
402         {
403                 llog(LL_ERR, "decr_free_channels: invalid controller number [%d]!", controller);
404                 return(ERROR);
405         }
406         if(isdn_ctrl_tab[controller].freechans > 0)
407         {
408                 (isdn_ctrl_tab[controller].freechans)--;
409                 DBGL(DL_CNST, (llog(LL_DBG, "decr_free_channels: ctrl %d, now %d chan free", controller, isdn_ctrl_tab[controller].freechans)));
410                 return(GOOD);
411         }
412         else
413         {
414                 llog(LL_ERR, "decr_free_channels: controller [%d] already 0 free chans!", controller);
415                 return(ERROR);
416         }
417 }               
418         
419 /*--------------------------------------------------------------------------*
420  *      increment number of free channels for controller
421  *--------------------------------------------------------------------------*/
422 int
423 incr_free_channels(int controller)
424 {
425         if((controller < 0) || (controller >= ncontroller))
426         {
427                 llog(LL_ERR, "incr_free_channels: invalid controller number [%d]!", controller);
428                 return(ERROR);
429         }
430         if(isdn_ctrl_tab[controller].freechans < isdn_ctrl_tab[controller].nbch)
431         {
432                 (isdn_ctrl_tab[controller].freechans)++;
433                 DBGL(DL_CNST, (llog(LL_DBG, "incr_free_channels: ctrl %d, now %d chan free", controller, isdn_ctrl_tab[controller].freechans)));
434                 return(GOOD);
435         }
436         else
437         {
438                 llog(LL_ERR, "incr_free_channels: controller [%d] already %d free chans!", controller, isdn_ctrl_tab[controller].nbch);
439                 return(ERROR);
440         }
441 }               
442         
443 /*--------------------------------------------------------------------------*
444  *      get number of free channels for controller
445  *--------------------------------------------------------------------------*/
446 int
447 get_free_channels(int controller)
448 {
449         if((controller < 0) || (controller >= ncontroller))
450         {
451                 llog(LL_ERR, "get_free_channels: invalid controller number [%d]!", controller);
452                 return(ERROR);
453         }
454         DBGL(DL_CNST, (llog(LL_DBG, "get_free_channels: ctrl %d, %d chan free", controller, isdn_ctrl_tab[controller].freechans)));
455         return(isdn_ctrl_tab[controller].freechans);
456 }               
457         
458 /*--------------------------------------------------------------------------*
459  *      set channel state to busy
460  *--------------------------------------------------------------------------*/
461 int
462 set_channel_busy(int controller, int channel)
463 {
464         if((controller < 0) || (controller >= ncontroller))
465         {
466                 llog(LL_ERR, "set_channel_busy: invalid controller number [%d]!", controller);
467                 return(ERROR);
468         }
469                 
470         if ((channel < 0) || (channel >= isdn_ctrl_tab[controller].nbch))
471                         {
472                 llog(LL_ERR, "set_channel_busy: controller [%d] invalid channel [%d]!", controller, channel);
473                 return(ERROR);
474                         }
475
476         if(isdn_ctrl_tab[controller].stateb[channel] == CHAN_RUN)
477                         {
478             DBGL(DL_CNST, (llog(LL_DBG, "set_channel_busy: controller [%d] channel B%d already busy!", controller, channel+1)));
479                         }
480                         else
481                         {
482             isdn_ctrl_tab[controller].stateb[channel] = CHAN_RUN;
483             DBGL(DL_CNST, (llog(LL_DBG, "set_channel_busy: controller [%d] channel B%d set to BUSY!", controller, channel+1)));
484         }
485         return(GOOD);
486 }
487
488 /*--------------------------------------------------------------------------*
489  *      set channel state to idle
490  *--------------------------------------------------------------------------*/
491 int
492 set_channel_idle(int controller, int channel)
493 {
494         if((controller < 0) || (controller >= ncontroller))
495         {
496                 llog(LL_ERR, "set_channel_idle: invalid controller number [%d]!", controller);
497                 return(ERROR);
498         }
499                 
500         if ((channel < 0) || (channel >= isdn_ctrl_tab[controller].nbch))
501                         {
502                 llog(LL_ERR, "set_channel_busy: controller [%d] invalid channel [%d]!", controller, channel);
503                 return(ERROR);
504                         }
505
506         if (isdn_ctrl_tab[controller].stateb[channel] == CHAN_IDLE)
507                         {
508             DBGL(DL_CNST, (llog(LL_DBG, "set_channel_idle: controller [%d] channel B%d already idle!", controller, channel+1)));
509                         }
510                         else
511                         {
512             isdn_ctrl_tab[controller].stateb[channel] = CHAN_IDLE;
513             DBGL(DL_CNST, (llog(LL_DBG, "set_channel_idle: controller [%d] channel B%d set to IDLE!", controller, channel+1)));
514         }
515         return(GOOD);
516 }
517
518 /*--------------------------------------------------------------------------*
519  *      return channel state
520  *--------------------------------------------------------------------------*/
521 int
522 ret_channel_state(int controller, int channel)
523 {
524         if((controller < 0) || (controller >= ncontroller))
525         {
526                 llog(LL_ERR, "ret_channel_state: invalid controller number [%d]!", controller);
527                 return(ERROR);
528         }
529                 
530         if ((channel < 0) || (channel >= isdn_ctrl_tab[controller].nbch))
531         {
532                 llog(LL_ERR, "set_channel_busy: controller [%d] invalid channel [%d]!", controller, channel);
533                         return(ERROR);
534         }
535
536         return(isdn_ctrl_tab[controller].stateb[channel]);
537 }
538
539 /* EOF */