c7bc6118e71c8f7f5879306d9ead279dff276c12
[dragonfly.git] / sys / net / i4b / layer2 / i4b_l2timer.c
1 /*
2  * Copyright (c) 1997, 2000 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_l2timer.c - layer 2 timer handling
28  *      --------------------------------------
29  *
30  *      $Id: i4b_l2timer.c,v 1.20 2000/08/24 11:48:58 hm Exp $ 
31  *
32  * $FreeBSD: src/sys/i4b/layer2/i4b_l2timer.c,v 1.6.2.1 2001/08/10 14:08:41 obrien Exp $
33  * $DragonFly: src/sys/net/i4b/layer2/i4b_l2timer.c,v 1.6 2005/06/03 16:50:10 dillon Exp $
34  *
35  *      last edit-date: [Thu Aug 24 12:48:52 2000]
36  *
37  *---------------------------------------------------------------------------*/
38
39 #if defined(__DragonFly__) || defined(__FreeBSD__)
40 #include "use_i4bq921.h"
41 #else
42 #define NI4BQ921        1
43 #endif
44 #if NI4BQ921 > 0
45
46 #include <sys/param.h>
47 #include <sys/kernel.h>
48 #include <sys/systm.h>
49 #include <sys/socket.h>
50 #include <sys/thread2.h>
51 #include <net/if.h>
52
53 #if defined(__NetBSD__) && __NetBSD_Version__ >= 104230000
54 #include <sys/callout.h>
55 #endif
56
57 #if defined(__DragonFly__) || defined(__FreeBSD__)
58 #include <net/i4b/include/machine/i4b_debug.h>
59 #else
60 #include <i4b/i4b_debug.h>
61 #include <i4b/i4b_ioctl.h>
62 #endif
63
64 #include "../include/i4b_global.h"
65
66 #include "i4b_l2.h"
67 #include "i4b_l2fsm.h"
68
69 /*---------------------------------------------------------------------------*
70  *      Q.921 timer T200 timeout function
71  *---------------------------------------------------------------------------*/
72 static void
73 i4b_T200_timeout(l2_softc_t *l2sc)
74 {
75         NDBGL2(L2_T_ERR, "unit %d, RC = %d", l2sc->unit, l2sc->RC);
76         i4b_next_l2state(l2sc, EV_T200EXP);
77 }
78
79 /*---------------------------------------------------------------------------*
80  *      Q.921 timer T200 start
81  *---------------------------------------------------------------------------*/
82 void
83 i4b_T200_start(l2_softc_t *l2sc)
84 {
85         if(l2sc->T200 == TIMER_ACTIVE)
86                 return;
87                 
88         NDBGL2(L2_T_MSG, "unit %d", l2sc->unit);
89         l2sc->T200 = TIMER_ACTIVE;
90
91         callout_reset(&l2sc->T200_timeout, T200DEF, 
92                         (void *)i4b_T200_timeout, l2sc);
93 }
94
95 /*---------------------------------------------------------------------------*
96  *      Q.921 timer T200 stop
97  *---------------------------------------------------------------------------*/
98 void
99 i4b_T200_stop(l2_softc_t *l2sc)
100 {
101         CRIT_VAR;
102         CRIT_BEG;
103         if(l2sc->T200 != TIMER_IDLE)
104         {
105                 callout_stop(&l2sc->T200_timeout);
106                 l2sc->T200 = TIMER_IDLE;
107         }
108         CRIT_END;
109         NDBGL2(L2_T_MSG, "unit %d", l2sc->unit);
110 }
111
112 /*---------------------------------------------------------------------------*
113  *      Q.921 timer T200 restart
114  *---------------------------------------------------------------------------*/
115 void
116 i4b_T200_restart(l2_softc_t *l2sc)
117 {
118         CRIT_VAR;
119         CRIT_BEG;
120         if(l2sc->T200 != TIMER_IDLE)
121         {
122                 callout_stop(&l2sc->T200_timeout);
123         }
124         else
125         {
126                 l2sc->T200 = TIMER_ACTIVE;
127         }
128
129         callout_reset(&l2sc->T200_timeout, T200DEF, 
130                         (void *)i4b_T200_timeout, l2sc);
131         CRIT_END;
132         NDBGL2(L2_T_MSG, "unit %d", l2sc->unit);
133 }
134
135 /*---------------------------------------------------------------------------*
136  *      Q.921 timer T202 timeout function
137  *---------------------------------------------------------------------------*/
138 static void
139 i4b_T202_timeout(l2_softc_t *l2sc)
140 {
141         NDBGL2(L2_T_ERR, "unit %d, N202 = %d", l2sc->unit, l2sc->N202);
142         
143         if(--(l2sc->N202))
144         {
145                 (*l2sc->T202func)(l2sc);
146         }
147 }
148
149 /*---------------------------------------------------------------------------*
150  *      Q.921 timer T202 start
151  *---------------------------------------------------------------------------*/
152 void
153 i4b_T202_start(l2_softc_t *l2sc)
154 {
155         if (l2sc->N202 == TIMER_ACTIVE)
156                 return;
157
158         NDBGL2(L2_T_MSG, "unit %d", l2sc->unit);
159         l2sc->N202 = N202DEF;   
160         l2sc->T202 = TIMER_ACTIVE;
161
162         callout_reset(&l2sc->T202_timeout, T202DEF, 
163                         (void *)i4b_T202_timeout, l2sc);
164 }
165
166 /*---------------------------------------------------------------------------*
167  *      Q.921 timer T202 stop
168  *---------------------------------------------------------------------------*/
169 void
170 i4b_T202_stop(l2_softc_t *l2sc)
171 {
172         CRIT_VAR;
173         CRIT_BEG;
174         if(l2sc->T202 != TIMER_IDLE)
175         {
176                 callout_stop(&l2sc->T202_timeout);
177                 l2sc->T202 = TIMER_IDLE;
178         }
179         CRIT_END;
180         NDBGL2(L2_T_MSG, "unit %d", l2sc->unit);
181 }
182
183 /*---------------------------------------------------------------------------*
184  *      Q.921 timer T203 timeout function
185  *---------------------------------------------------------------------------*/
186 #if I4B_T203_ACTIVE
187 static void
188 i4b_T203_timeout(l2_softc_t *l2sc)
189 {
190         NDBGL2(L2_T_ERR, "unit %d", l2sc->unit);
191         i4b_next_l2state(l2sc, EV_T203EXP);
192 }
193 #endif
194
195 /*---------------------------------------------------------------------------*
196  *      Q.921 timer T203 start
197  *---------------------------------------------------------------------------*/
198 void
199 i4b_T203_start(l2_softc_t *l2sc)
200 {
201 #if I4B_T203_ACTIVE
202         if (l2sc->T203 == TIMER_ACTIVE)
203                 return;
204                 
205         NDBGL2(L2_T_MSG, "unit %d", l2sc->unit);
206         l2sc->T203 = TIMER_ACTIVE;
207
208         callout_reset(&l2sc->T203_timeout, T203DEF, i4b_T203_timeout, l2sc);
209 #endif
210 }
211
212 /*---------------------------------------------------------------------------*
213  *      Q.921 timer T203 stop
214  *---------------------------------------------------------------------------*/
215 void
216 i4b_T203_stop(l2_softc_t *l2sc)
217 {
218 #if I4B_T203_ACTIVE
219         CRIT_VAR;
220         CRIT_BEG;
221         if(l2sc->T203 != TIMER_IDLE)
222         {
223                 callout_stop(&l2sc->T203_timeout);
224                 l2sc->T203 = TIMER_IDLE;
225         }
226         CRIT_END;
227         NDBGL2(L2_T_MSG, "unit %d", l2sc->unit);
228 #endif
229 }
230
231 /*---------------------------------------------------------------------------*
232  *      Q.921 timer T203 restart
233  *---------------------------------------------------------------------------*/
234 void
235 i4b_T203_restart(l2_softc_t *l2sc)
236 {
237 #if I4B_T203_ACTIVE
238         CRIT_VAR;
239         CRIT_BEG;
240
241         if(l2sc->T203 != TIMER_IDLE)
242         {
243                 callout_stop(&l2sc->T203_timeout);
244         }
245         else
246         {
247                 l2sc->T203 = TIMER_ACTIVE;
248         }
249
250         callout_reset(&l2sc->T203_timeout, T203DEF, i4b_T203_timerout, l2sc);
251         CRIT_END;
252         NDBGL2(L2_T_MSG, "unit %d", l2sc->unit);
253 #endif
254 }
255
256 #endif /* NI4BQ921 > 0 */