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
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
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 $
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 $");
48 #include <sys/param.h>
49 #include <sys/kernel.h>
50 #include <sys/systm.h>
52 #include <sys/socket.h>
55 #if defined(__NetBSD__) && __NetBSD_Version__ >= 104230000
56 #include <sys/callout.h>
60 #include <machine/i4b_debug.h>
61 #include <machine/i4b_ioctl.h>
63 #include <netisdn/i4b_debug.h>
64 #include <netisdn/i4b_ioctl.h>
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 *---------------------------------------------------------------------------*/
79 i4b_establish_data_link(l2_softc_t
*l2sc
)
81 i4b_l1_activate(l2sc
);
83 i4b_clear_exception_conditions(l2sc
);
87 i4b_tx_sabme(l2sc
, P1
);
89 i4b_T200_restart(l2sc
);
94 /*---------------------------------------------------------------------------*
95 * routine CLEAR EXCEPTION CONDITIONS (Q.921 03/93 page 83)
96 *---------------------------------------------------------------------------*/
98 i4b_clear_exception_conditions(l2_softc_t
*l2sc
)
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 -------------------------------------------------------------- */
127 /*---------------------------------------------------------------------------*
128 * routine TRANSMIT ENQUIRE (Q.921 03/93 page 83)
129 *---------------------------------------------------------------------------*/
131 i4b_transmit_enquire(l2_softc_t
*l2sc
)
134 i4b_tx_rnr_command(l2sc
, P1
);
136 i4b_tx_rr_command(l2sc
, P1
);
140 i4b_T200_start(l2sc
);
143 /*---------------------------------------------------------------------------*
144 * routine NR ERROR RECOVERY (Q.921 03/93 page 83)
145 *---------------------------------------------------------------------------*/
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 *---------------------------------------------------------------------------*/
160 i4b_enquiry_response(l2_softc_t
*l2sc
)
163 i4b_tx_rnr_response(l2sc
, F1
);
165 i4b_tx_rr_response(l2sc
, F1
);
170 /*---------------------------------------------------------------------------*
171 * routine INVOKE RETRANSMISSION (Q.921 03/93 page 84)
172 *---------------------------------------------------------------------------*/
174 i4b_invoke_retransmission(l2_softc_t
*l2sc
, int nr
)
180 NDBGL2(L2_ERROR
, "nr = %d", nr
);
182 while(l2sc
->vs
!= nr
)
184 NDBGL2(L2_ERROR
, "nr(%d) != vs(%d)", nr
, 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!");
198 IF_ENQUEUE(&l2sc
->i_queue
, l2sc
->ua_frame
);
199 l2sc
->ua_num
= UA_EMPTY
;
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
);
215 /*---------------------------------------------------------------------------*
216 * routine ACKNOWLEDGE PENDING (Q.921 03/93 p 70)
217 *---------------------------------------------------------------------------*/
219 i4b_acknowledge_pending(l2_softc_t
*l2sc
)
224 i4b_tx_rr_response(l2sc
, F0
);
228 /*---------------------------------------------------------------------------*
229 * i4b_print_frame - just print the hex contents of a frame
230 *---------------------------------------------------------------------------*/
232 i4b_print_frame(int len
, u_char
*buf
)
237 if (!(i4b_l2_debug
& L2_ERROR
)) /* XXXXXXXXXXXXXXXXXXXXX */
240 for(i
= 0; i
< len
; i
++)
241 printf(" 0x%x", buf
[i
]);
246 /*---------------------------------------------------------------------------*
247 * i4b_print_l2var - print some l2softc vars
248 *---------------------------------------------------------------------------*/
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",
262 /*---------------------------------------------------------------------------*
263 * got s or i frame, check if valid ack for last sent frame
264 *---------------------------------------------------------------------------*/
266 i4b_rxd_ack(l2_softc_t
*l2sc
, struct isdn_l3_driver
*drv
, int nr
)
270 NDBGL2(L2_ERROR
, "N(R)=%d, UA=%d, V(R)=%d, V(S)=%d, V(A)=%d",
278 if(l2sc
->ua_num
!= UA_EMPTY
)
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
;
296 /*---------------------------------------------------------------------------*
297 * if not already active, activate layer 1
298 *---------------------------------------------------------------------------*/
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
);
323 if((nr
> vs
) && ((vs
!= 0) || (nr
!= 127)))
325 NDBGL2(L2_ERROR
, "ERROR, va = %d, nr = %d, vs = %d [2]", va
, nr
, vs
);
331 #endif /* NI4BQ921 > 0 */