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