Initial import from FreeBSD RELENG_4:
[dragonfly.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: src/usr.sbin/i4b/isdnd/controller.c,v 1.6.2.4 2002/04/25 12:30:06 gj Exp $
31  *
32  *      last edit-date: [Mon Dec 10 11:32:05 2001]
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         if(ctrl_type == CTRL_PASSIVE)
99         {
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];
103         }
104         else if(ctrl_type == CTRL_DAIC)
105         {
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];
109         }
110         else if(ctrl_type == CTRL_TINADD)
111         {
112                 return "Stollmann tina-dd";
113         }
114         else if(ctrl_type == CTRL_CAPI)
115         {
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];
119         }
120
121         return "unknown card type";
122 }
123  
124 /*---------------------------------------------------------------------------*
125  *      init controller state array
126  *---------------------------------------------------------------------------*/
127 void
128 init_controller(void)
129 {
130         int i;
131         int max = 1;
132         msg_ctrl_info_req_t mcir;
133         
134         for(i=0; i < max; i++)
135         {
136                 mcir.controller = i;
137                 
138                 if((ioctl(isdnfd, I4B_CTRL_INFO_REQ, &mcir)) < 0)
139                 {
140                         log(LL_ERR, "init_controller: ioctl I4B_CTRL_INFO_REQ failed: %s", strerror(errno));
141                         do_exit(1);
142                 }
143
144                 if((ncontroller = max = mcir.ncontroller) == 0)
145                 {
146                         log(LL_ERR, "init_controller: no ISDN controller found!");
147                         do_exit(1);
148                 }
149
150                 if(mcir.ctrl_type == -1 || mcir.card_type == -1)
151                 {
152                         log(LL_ERR, "init_controller: ctrl/card is invalid!");
153                         do_exit(1);
154                 }
155
156                 /* init controller tab */
157
158                 if((init_controller_state(i, mcir.ctrl_type, mcir.card_type, mcir.tei, mcir.nbch)) == ERROR)
159                 {
160                         log(LL_ERR, "init_controller: init_controller_state for controller %d failed", i);
161                         do_exit(1);
162                 }
163         }
164         DBGL(DL_RCCF, (log(LL_DBG, "init_controller: found %d ISDN controller(s)", max)));
165 }
166
167 /*--------------------------------------------------------------------------*
168  *      init controller state table entry
169  *--------------------------------------------------------------------------*/
170 static int
171 init_controller_state(int controller, int ctrl_type, int card_type, int tei,
172                       int nbch)
173 {
174         int i;
175
176         if((controller < 0) || (controller >= ncontroller))
177         {
178                 log(LL_ERR, "init_controller_state: invalid controller number [%d]!", controller);
179                 return(ERROR);
180         }
181         
182         /* init controller tab */
183                 
184         switch (ctrl_type) {
185         case CTRL_PASSIVE:
186                 if((card_type > CARD_TYPEP_UNK) &&
187                    (card_type <= CARD_TYPEP_MAX))
188                 {
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;
192                 }
193                 else
194                 {
195                         log(LL_ERR, "init_controller_state: unknown card type %d", card_type);
196                         return(ERROR);
197                 }
198                 break;
199                 
200         case CTRL_DAIC:
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;
204                 break;
205
206         case CTRL_TINADD:
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;
210                 break;
211
212         case CTRL_CAPI:
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;
216                 break;
217
218         default:
219                 log(LL_ERR, "init_controller_state: unknown controller type %d", ctrl_type);
220                 return(ERROR);
221         }
222         
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;
230
231                 log(LL_DMN, "init_controller_state: controller %d is %s",
232                   controller,
233                   name_of_controller(isdn_ctrl_tab[controller].ctrl_type,
234                                      isdn_ctrl_tab[controller].card_type));
235                 
236         return(GOOD);
237 }       
238
239 /*--------------------------------------------------------------------------*
240  *      init active or capi controller
241  *--------------------------------------------------------------------------*/
242 void
243 init_active_controller(void)
244 {
245         int ret;
246         int unit = 0;
247         int controller;
248         char cmdbuf[MAXPATHLEN+128];
249
250         for(controller = 0; controller < ncontroller; controller++)
251         {
252                 if(isdn_ctrl_tab[controller].ctrl_type == CTRL_TINADD)
253                 {
254                         DBGL(DL_RCCF, (log(LL_DBG, "init_active_controller, tina-dd %d: executing [%s %d]", unit, tinainitprog, unit)));
255                         
256                         snprintf(cmdbuf, sizeof(cmdbuf), "%s %d", tinainitprog, unit);
257
258                         if((ret = system(cmdbuf)) != 0)
259                         {
260                                 log(LL_ERR, "init_active_controller, tina-dd %d: %s returned %d!", unit, tinainitprog, ret);
261                                 do_exit(1);
262                         }
263                 }
264
265                 /*
266                  *  Generic microcode loading. If a controller has
267                  *  defined a microcode file, load it using the
268                  *  I4B_CTRL_DOWNLOAD ioctl.
269                  */
270                 
271                 if(isdn_ctrl_tab[controller].firmware != NULL)
272                 {
273                     int fd, ret;
274                     struct isdn_dr_prot idp;
275                     struct isdn_download_request idr;
276
277                     fd = open(isdn_ctrl_tab[controller].firmware, O_RDONLY);
278                     if (fd < 0) {
279                         log(LL_ERR, "init_active_controller %d: open %s: %s!",
280                             controller, isdn_ctrl_tab[controller].firmware,
281                             strerror(errno));
282                         do_exit(1);
283                     }
284
285                     idp.bytecount = lseek(fd, 0, SEEK_END);
286                     idp.microcode = mmap(0, idp.bytecount, PROT_READ,
287                                          MAP_SHARED, fd, 0);
288                     if (idp.microcode == MAP_FAILED) {
289                         log(LL_ERR, "init_active_controller %d: mmap %s: %s!",
290                             controller, isdn_ctrl_tab[controller].firmware,
291                             strerror(errno));
292                         do_exit(1);
293                     }
294                     
295                     DBGL(DL_RCCF, (log(LL_DBG, "init_active_controller %d: loading firmware from [%s]", controller, isdn_ctrl_tab[controller].firmware)));
296
297                     idr.controller = controller;
298                     idr.numprotos = 1;
299                     idr.protocols = &idp;
300                     
301                     ret = ioctl(isdnfd, I4B_CTRL_DOWNLOAD, &idr, sizeof(idr));
302                     if (ret) {
303                         log(LL_ERR, "init_active_controller %d: load %s: %s!",
304                             controller, isdn_ctrl_tab[controller].firmware,
305                             strerror(errno));
306                         do_exit(1);
307                     }
308
309                     munmap(idp.microcode, idp.bytecount);
310                     close(fd);
311                 }
312         }
313 }       
314
315 /*--------------------------------------------------------------------------*
316  *      init controller D-channel ISDN protocol
317  *--------------------------------------------------------------------------*/
318 void
319 init_controller_protocol(void)
320 {
321         int controller;
322         msg_prot_ind_t mpi;
323
324         for(controller = 0; controller < ncontroller; controller++)
325         {
326                 mpi.controller = controller;
327                 mpi.protocol = isdn_ctrl_tab[controller].protocol;
328                 
329                 if((ioctl(isdnfd, I4B_PROT_IND, &mpi)) < 0)
330                 {
331                         log(LL_ERR, "init_controller_protocol: ioctl I4B_PROT_IND failed: %s", strerror(errno));
332                         do_exit(1);
333                 }
334         }
335 }
336
337 /*--------------------------------------------------------------------------*
338  *      set controller state to UP/DOWN
339  *--------------------------------------------------------------------------*/
340 int
341 set_controller_state(int controller, int state)
342 {
343         if((controller < 0) || (controller >= ncontroller))
344         {
345                 log(LL_ERR, "set_controller_state: invalid controller number [%d]!", controller);
346                 return(ERROR);
347         }
348
349         if(state == CTRL_UP)
350         {
351                 isdn_ctrl_tab[controller].state = CTRL_UP;
352                 DBGL(DL_CNST, (log(LL_DBG, "set_controller_state: controller [%d] set UP!", controller)));
353         }
354         else if (state == CTRL_DOWN)
355         {
356                 isdn_ctrl_tab[controller].state = CTRL_DOWN;
357                 DBGL(DL_CNST, (log(LL_DBG, "set_controller_state: controller [%d] set DOWN!", controller)));
358         }
359         else
360         {
361                 log(LL_ERR, "set_controller_state: invalid controller state [%d]!", state);
362                 return(ERROR);
363         }
364         return(GOOD);
365 }               
366         
367 /*--------------------------------------------------------------------------*
368  *      get controller state
369  *--------------------------------------------------------------------------*/
370 int
371 get_controller_state(int controller)
372 {
373         if((controller < 0) || (controller >= ncontroller))
374         {
375                 log(LL_ERR, "set_controller_state: invalid controller number [%d]!", controller);
376                 return(ERROR);
377         }
378         return(isdn_ctrl_tab[controller].state);
379 }               
380
381 /*--------------------------------------------------------------------------*
382  *      decrement number of free channels for controller
383  *--------------------------------------------------------------------------*/
384 int
385 decr_free_channels(int controller)
386 {
387         if((controller < 0) || (controller >= ncontroller))
388         {
389                 log(LL_ERR, "decr_free_channels: invalid controller number [%d]!", controller);
390                 return(ERROR);
391         }
392         if(isdn_ctrl_tab[controller].freechans > 0)
393         {
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)));
396                 return(GOOD);
397         }
398         else
399         {
400                 log(LL_ERR, "decr_free_channels: controller [%d] already 0 free chans!", controller);
401                 return(ERROR);
402         }
403 }               
404         
405 /*--------------------------------------------------------------------------*
406  *      increment number of free channels for controller
407  *--------------------------------------------------------------------------*/
408 int
409 incr_free_channels(int controller)
410 {
411         if((controller < 0) || (controller >= ncontroller))
412         {
413                 log(LL_ERR, "incr_free_channels: invalid controller number [%d]!", controller);
414                 return(ERROR);
415         }
416         if(isdn_ctrl_tab[controller].freechans < isdn_ctrl_tab[controller].nbch)
417         {
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)));
420                 return(GOOD);
421         }
422         else
423         {
424                 log(LL_ERR, "incr_free_channels: controller [%d] already %d free chans!", controller, isdn_ctrl_tab[controller].nbch);
425                 return(ERROR);
426         }
427 }               
428         
429 /*--------------------------------------------------------------------------*
430  *      get number of free channels for controller
431  *--------------------------------------------------------------------------*/
432 int
433 get_free_channels(int controller)
434 {
435         if((controller < 0) || (controller >= ncontroller))
436         {
437                 log(LL_ERR, "get_free_channels: invalid controller number [%d]!", controller);
438                 return(ERROR);
439         }
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);
442 }               
443         
444 /*--------------------------------------------------------------------------*
445  *      set channel state to busy
446  *--------------------------------------------------------------------------*/
447 int
448 set_channel_busy(int controller, int channel)
449 {
450         if((controller < 0) || (controller >= ncontroller))
451         {
452                 log(LL_ERR, "set_channel_busy: invalid controller number [%d]!", controller);
453                 return(ERROR);
454         }
455                 
456         if ((channel < 0) || (channel >= isdn_ctrl_tab[controller].nbch))
457                         {
458                 log(LL_ERR, "set_channel_busy: controller [%d] invalid channel [%d]!", controller, channel);
459                 return(ERROR);
460                         }
461
462         if(isdn_ctrl_tab[controller].stateb[channel] == CHAN_RUN)
463                         {
464             DBGL(DL_CNST, (log(LL_DBG, "set_channel_busy: controller [%d] channel B%d already busy!", controller, channel+1)));
465                         }
466                         else
467                         {
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)));
470         }
471         return(GOOD);
472 }
473
474 /*--------------------------------------------------------------------------*
475  *      set channel state to idle
476  *--------------------------------------------------------------------------*/
477 int
478 set_channel_idle(int controller, int channel)
479 {
480         if((controller < 0) || (controller >= ncontroller))
481         {
482                 log(LL_ERR, "set_channel_idle: invalid controller number [%d]!", controller);
483                 return(ERROR);
484         }
485                 
486         if ((channel < 0) || (channel >= isdn_ctrl_tab[controller].nbch))
487                         {
488                 log(LL_ERR, "set_channel_busy: controller [%d] invalid channel [%d]!", controller, channel);
489                 return(ERROR);
490                         }
491
492         if (isdn_ctrl_tab[controller].stateb[channel] == CHAN_IDLE)
493                         {
494             DBGL(DL_CNST, (log(LL_DBG, "set_channel_idle: controller [%d] channel B%d already idle!", controller, channel+1)));
495                         }
496                         else
497                         {
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)));
500         }
501         return(GOOD);
502 }
503
504 /*--------------------------------------------------------------------------*
505  *      return channel state
506  *--------------------------------------------------------------------------*/
507 int
508 ret_channel_state(int controller, int channel)
509 {
510         if((controller < 0) || (controller >= ncontroller))
511         {
512                 log(LL_ERR, "ret_channel_state: invalid controller number [%d]!", controller);
513                 return(ERROR);
514         }
515                 
516         if ((channel < 0) || (channel >= isdn_ctrl_tab[controller].nbch))
517         {
518                 log(LL_ERR, "set_channel_busy: controller [%d] invalid channel [%d]!", controller, channel);
519                         return(ERROR);
520         }
521
522         return(isdn_ctrl_tab[controller].stateb[channel]);
523 }
524
525 /* EOF */