Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / netisdn / i4b_util.c
blobe1b7e493b6bfd1ace9c1707e2dc21986f48bbe7e
1 /*
2 * Copyright (c) 1997, 2000 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
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.
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.
25 *---------------------------------------------------------------------------
27 * i4b_util.c - layer 2 utility routines
28 * -------------------------------------
30 * $Id: i4b_util.c,v 1.11 2006/11/16 01:33:49 christos Exp $
32 * $FreeBSD$
34 * last edit-date: [Fri Jan 5 11:33:47 2001]
36 *---------------------------------------------------------------------------*/
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: i4b_util.c,v 1.10 2006/10/16 12:23:00 pooka Exp $");
41 #ifdef __FreeBSD__
42 #include "i4bq921.h"
43 #else
44 #define NI4BQ921 1
45 #endif
46 #if NI4BQ921 > 0
48 #include <sys/param.h>
49 #include <sys/kernel.h>
50 #include <sys/systm.h>
51 #include <sys/mbuf.h>
52 #include <sys/socket.h>
53 #include <net/if.h>
55 #if defined(__NetBSD__) && __NetBSD_Version__ >= 104230000
56 #include <sys/callout.h>
57 #endif
59 #ifdef __FreeBSD__
60 #include <machine/i4b_debug.h>
61 #include <machine/i4b_ioctl.h>
62 #else
63 #include <netisdn/i4b_debug.h>
64 #include <netisdn/i4b_ioctl.h>
65 #endif
67 #include <netisdn/i4b_global.h>
68 #include <netisdn/i4b_l2.h>
69 #include <netisdn/i4b_l1l2.h>
70 #include <netisdn/i4b_isdnq931.h>
71 #include <netisdn/i4b_mbuf.h>
72 #include <netisdn/i4b_l2fsm.h>
73 #include <netisdn/i4b_l3l4.h>
75 /*---------------------------------------------------------------------------*
76 * routine ESTABLISH DATA LINK (Q.921 03/93 page 83)
77 *---------------------------------------------------------------------------*/
78 void
79 i4b_establish_data_link(l2_softc_t *l2sc)
81 i4b_l1_activate(l2sc);
83 i4b_clear_exception_conditions(l2sc);
85 l2sc->RC = 0;
87 i4b_tx_sabme(l2sc, P1);
89 i4b_T200_restart(l2sc);
91 i4b_T203_stop(l2sc);
94 /*---------------------------------------------------------------------------*
95 * routine CLEAR EXCEPTION CONDITIONS (Q.921 03/93 page 83)
96 *---------------------------------------------------------------------------*/
97 void
98 i4b_clear_exception_conditions(l2_softc_t *l2sc)
100 int s;
102 s = splnet();
104 /*XXX -------------------------------------------------------------- */
105 /*XXX is this really appropriate here or should it moved elsewhere ? */
107 i4b_Dcleanifq(&l2sc->i_queue);
109 if(l2sc->ua_num != UA_EMPTY)
111 i4b_Dfreembuf(l2sc->ua_frame);
112 l2sc->ua_num = UA_EMPTY;
114 /*XXX -------------------------------------------------------------- */
116 l2sc->peer_busy = 0;
118 l2sc->rej_excpt = 0;
120 l2sc->own_busy = 0;
122 l2sc->ack_pend = 0;
124 splx(s);
127 /*---------------------------------------------------------------------------*
128 * routine TRANSMIT ENQUIRE (Q.921 03/93 page 83)
129 *---------------------------------------------------------------------------*/
130 void
131 i4b_transmit_enquire(l2_softc_t *l2sc)
133 if(l2sc->own_busy)
134 i4b_tx_rnr_command(l2sc, P1);
135 else
136 i4b_tx_rr_command(l2sc, P1);
138 l2sc->ack_pend = 0;
140 i4b_T200_start(l2sc);
143 /*---------------------------------------------------------------------------*
144 * routine NR ERROR RECOVERY (Q.921 03/93 page 83)
145 *---------------------------------------------------------------------------*/
146 void
147 i4b_nr_error_recovery(l2_softc_t *l2sc)
149 i4b_mdl_error_ind(l2sc, "i4b_nr_error_recovery", MDL_ERR_J);
151 i4b_establish_data_link(l2sc);
153 l2sc->l3initiated = 0;
156 /*---------------------------------------------------------------------------*
157 * routine ENQUIRY RESPONSE (Q.921 03/93 page 84)
158 *---------------------------------------------------------------------------*/
159 void
160 i4b_enquiry_response(l2_softc_t *l2sc)
162 if(l2sc->own_busy)
163 i4b_tx_rnr_response(l2sc, F1);
164 else
165 i4b_tx_rr_response(l2sc, F1);
167 l2sc->ack_pend = 0;
170 /*---------------------------------------------------------------------------*
171 * routine INVOKE RETRANSMISSION (Q.921 03/93 page 84)
172 *---------------------------------------------------------------------------*/
173 void
174 i4b_invoke_retransmission(l2_softc_t *l2sc, int nr)
176 int s;
178 s = splnet();
180 NDBGL2(L2_ERROR, "nr = %d", nr );
182 while(l2sc->vs != nr)
184 NDBGL2(L2_ERROR, "nr(%d) != vs(%d)", nr, l2sc->vs);
186 M128DEC(l2sc->vs);
188 /* XXXXXXXXXXXXXXXXX */
190 if((l2sc->ua_num != UA_EMPTY) && (l2sc->vs == l2sc->ua_num))
192 if(IF_QFULL(&l2sc->i_queue))
194 NDBGL2(L2_ERROR, "ERROR, I-queue full!");
196 else
198 IF_ENQUEUE(&l2sc->i_queue, l2sc->ua_frame);
199 l2sc->ua_num = UA_EMPTY;
202 else
204 NDBGL2(L2_ERROR, "ERROR, l2sc->vs = %d, l2sc->ua_num = %d ",l2sc->vs, l2sc->ua_num);
207 /* XXXXXXXXXXXXXXXXX */
209 i4b_i_frame_queued_up(l2sc);
212 splx(s);
215 /*---------------------------------------------------------------------------*
216 * routine ACKNOWLEDGE PENDING (Q.921 03/93 p 70)
217 *---------------------------------------------------------------------------*/
218 void
219 i4b_acknowledge_pending(l2_softc_t *l2sc)
221 if(l2sc->ack_pend)
223 l2sc->ack_pend = 0;
224 i4b_tx_rr_response(l2sc, F0);
228 /*---------------------------------------------------------------------------*
229 * i4b_print_frame - just print the hex contents of a frame
230 *---------------------------------------------------------------------------*/
231 void
232 i4b_print_frame(int len, u_char *buf)
234 #if DO_I4B_DEBUG
235 int i;
237 if (!(i4b_l2_debug & L2_ERROR)) /* XXXXXXXXXXXXXXXXXXXXX */
238 return;
240 for(i = 0; i < len; i++)
241 printf(" 0x%x", buf[i]);
242 printf("\n");
243 #endif
246 /*---------------------------------------------------------------------------*
247 * i4b_print_l2var - print some l2softc vars
248 *---------------------------------------------------------------------------*/
249 void
250 i4b_print_l2var(l2_softc_t *l2sc)
252 NDBGL2(L2_ERROR, "isdnif %d V(R)=%d, V(S)=%d, V(A)=%d,ACKP=%d,PBSY=%d,OBSY=%d",
253 l2sc->drv->isdnif,
254 l2sc->vr,
255 l2sc->vs,
256 l2sc->va,
257 l2sc->ack_pend,
258 l2sc->peer_busy,
259 l2sc->own_busy);
262 /*---------------------------------------------------------------------------*
263 * got s or i frame, check if valid ack for last sent frame
264 *---------------------------------------------------------------------------*/
265 void
266 i4b_rxd_ack(l2_softc_t *l2sc, struct isdn_l3_driver *drv, int nr)
269 #ifdef NOTDEF
270 NDBGL2(L2_ERROR, "N(R)=%d, UA=%d, V(R)=%d, V(S)=%d, V(A)=%d",
272 l2sc->ua_num,
273 l2sc->vr,
274 l2sc->vs,
275 l2sc->va);
276 #endif
278 if(l2sc->ua_num != UA_EMPTY)
280 int s;
282 s = splnet();
284 M128DEC(nr);
286 if(l2sc->ua_num != nr)
287 NDBGL2(L2_ERROR, "((N(R)-1)=%d) != (UA=%d) !!!", nr, l2sc->ua_num);
289 i4b_Dfreembuf(l2sc->ua_frame);
290 l2sc->ua_num = UA_EMPTY;
292 splx(s);
296 /*---------------------------------------------------------------------------*
297 * if not already active, activate layer 1
298 *---------------------------------------------------------------------------*/
299 void
300 i4b_l1_activate(l2_softc_t *l2sc)
302 if(l2sc->ph_active == PH_INACTIVE)
304 l2sc->ph_active = PH_ACTIVEPEND;
305 l2sc->driver->ph_activate_req(l2sc->l1_token);
309 /*---------------------------------------------------------------------------*
310 * check for v(a) <= n(r) <= v(s)
311 * nr = receive sequence frame counter, va = acknowledge sequence frame
312 * counter and vs = transmit sequence frame counter
313 *---------------------------------------------------------------------------*/
315 i4b_l2_nr_ok(int nr, int va, int vs)
317 if((va > nr) && ((nr != 0) || (va != 127)))
319 NDBGL2(L2_ERROR, "ERROR, va = %d, nr = %d, vs = %d [1]", va, nr, vs);
320 return 0; /* fail */
323 if((nr > vs) && ((vs != 0) || (nr != 127)))
325 NDBGL2(L2_ERROR, "ERROR, va = %d, nr = %d, vs = %d [2]", va, nr, vs);
326 return 0; /* fail */
328 return 1; /* good */
331 #endif /* NI4BQ921 > 0 */