2 * Copyright (c) 1997, 1999 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_bchan.c - B channel handling L1 procedures
28 * ----------------------------------------------
30 * $Id: i4b_bchan.c,v 1.2 1999/12/13 21:25:26 hm Exp $
34 * last edit-date: [Mon Dec 13 21:59:11 1999]
36 *---------------------------------------------------------------------------*/
42 #include <sys/param.h>
43 #include <sys/systm.h>
45 #include <machine/stdarg.h>
47 #include <machine/clock.h>
49 #include <sys/socket.h>
52 #include <machine/i4b_debug.h>
53 #include <machine/i4b_ioctl.h>
54 #include <machine/i4b_trace.h>
56 #include <i4b/layer1/i4b_l1.h>
57 #include <i4b/layer1/i4b_isac.h>
58 #include <i4b/layer1/i4b_hscx.h>
60 #include <i4b/include/i4b_l1l2.h>
61 #include <i4b/include/i4b_mbuf.h>
62 #include <i4b/include/i4b_global.h>
64 static void isic_bchannel_start(int unit, int h_chan);
65 static void isic_bchannel_stat(int unit, int h_chan, bchan_statistics_t *bsp);
66 static void isic_set_linktab(int unit, int channel, drvr_link_t *dlt);
67 static isdn_link_t *isic_ret_linktab(int unit, int channel);
69 /*---------------------------------------------------------------------------*
70 * initialize one B channels rx/tx data structures and init/deinit HSCX
71 *---------------------------------------------------------------------------*/
73 isic_bchannel_setup(int unit, int h_chan, int bprot, int activate)
75 struct l1_softc *sc = &l1_sc[unit];
76 l1_bchan_state_t *chan = &sc->sc_chan[h_chan];
83 isic_hscx_init(sc, h_chan, activate);
86 DBGL1(L1_BCHAN, "isic_bchannel_setup", ("unit=%d, channel=%d, %s\n",
87 sc->sc_unit, h_chan, activate ? "activate" : "deactivate"));
91 chan->unit = sc->sc_unit; /* unit number */
92 chan->channel = h_chan; /* B channel */
93 chan->bprot = bprot; /* B channel protocol */
94 chan->state = HSCX_IDLE; /* B channel state */
98 i4b_Bcleanifq(&chan->rx_queue); /* clean rx queue */
100 chan->rx_queue.ifq_maxlen = IFQ_MAXLEN;
102 chan->rxcount = 0; /* reset rx counter */
104 i4b_Bfreembuf(chan->in_mbuf); /* clean rx mbuf */
106 chan->in_mbuf = NULL; /* reset mbuf ptr */
107 chan->in_cbptr = NULL; /* reset mbuf curr ptr */
108 chan->in_len = 0; /* reset mbuf data len */
110 /* transmitter part */
112 i4b_Bcleanifq(&chan->tx_queue); /* clean tx queue */
114 chan->tx_queue.ifq_maxlen = IFQ_MAXLEN;
116 chan->txcount = 0; /* reset tx counter */
118 i4b_Bfreembuf(chan->out_mbuf_head); /* clean tx mbuf */
120 chan->out_mbuf_head = NULL; /* reset head mbuf ptr */
121 chan->out_mbuf_cur = NULL; /* reset current mbuf ptr */
122 chan->out_mbuf_cur_ptr = NULL; /* reset current mbuf data ptr */
123 chan->out_mbuf_cur_len = 0; /* reset current mbuf data cnt */
128 isic_hscx_init(sc, h_chan, activate);
134 /*---------------------------------------------------------------------------*
135 * start transmission on a b channel
136 *---------------------------------------------------------------------------*/
138 isic_bchannel_start(int unit, int h_chan)
140 struct l1_softc *sc = &l1_sc[unit];
141 register l1_bchan_state_t *chan = &sc->sc_chan[h_chan];
142 register int next_len;
149 s = SPLI4B(); /* enter critical section */
150 if(chan->state & HSCX_TX_ACTIVE) /* already running ? */
153 return; /* yes, leave */
156 /* get next mbuf from queue */
158 IF_DEQUEUE(&chan->tx_queue, chan->out_mbuf_head);
160 if(chan->out_mbuf_head == NULL) /* queue empty ? */
162 splx(s); /* leave critical section */
163 return; /* yes, exit */
166 /* init current mbuf values */
168 chan->out_mbuf_cur = chan->out_mbuf_head;
169 chan->out_mbuf_cur_len = chan->out_mbuf_cur->m_len;
170 chan->out_mbuf_cur_ptr = chan->out_mbuf_cur->m_data;
172 /* activity indicator for timeout handling */
174 if(chan->bprot == BPROT_NONE)
176 if(!(isic_hscx_silence(chan->out_mbuf_cur->m_data, chan->out_mbuf_cur->m_len)))
184 chan->state |= HSCX_TX_ACTIVE; /* we start transmitting */
186 if(sc->sc_trace & TRACE_B_TX) /* if trace, send mbuf to trace dev */
190 hdr.type = (h_chan == HSCX_CH_A ? TRC_CH_B1 : TRC_CH_B2);
192 hdr.count = ++sc->sc_trace_bcount;
194 MPH_Trace_Ind(&hdr, chan->out_mbuf_cur->m_len, chan->out_mbuf_cur->m_data);
197 len = 0; /* # of chars put into HSCX tx fifo this time */
200 * fill the HSCX tx fifo with data from the current mbuf. if
201 * current mbuf holds less data than HSCX fifo length, try to
202 * get the next mbuf from (a possible) mbuf chain. if there is
203 * not enough data in a single mbuf or in a chain, then this
204 * is the last mbuf and we tell the HSCX that it has to send
205 * CRC and closing flag
208 while((len < sc->sc_bfifolen) && chan->out_mbuf_cur)
211 * put as much data into the HSCX fifo as is
212 * available from the current mbuf
215 if((len + chan->out_mbuf_cur_len) >= sc->sc_bfifolen)
216 next_len = sc->sc_bfifolen - len;
218 next_len = chan->out_mbuf_cur_len;
221 printf("b:mh=%x, mc=%x, mcp=%x, mcl=%d l=%d nl=%d # ",
224 chan->out_mbuf_cur_ptr,
225 chan->out_mbuf_cur_len,
230 /* wait for tx fifo write enabled */
232 isic_hscx_waitxfw(sc, h_chan);
234 /* write what we have from current mbuf to HSCX fifo */
236 HSCX_WRFIFO(h_chan, chan->out_mbuf_cur_ptr, next_len);
238 len += next_len; /* update # of bytes written */
239 chan->txcount += next_len; /* statistics */
240 chan->out_mbuf_cur_ptr += next_len; /* data ptr */
241 chan->out_mbuf_cur_len -= next_len; /* data len */
244 * in case the current mbuf (of a possible chain) data
245 * has been put into the fifo, check if there is a next
246 * mbuf in the chain. If there is one, get ptr to it
247 * and update the data ptr and the length
250 if((chan->out_mbuf_cur_len <= 0) &&
251 ((chan->out_mbuf_cur = chan->out_mbuf_cur->m_next) != NULL))
253 chan->out_mbuf_cur_ptr = chan->out_mbuf_cur->m_data;
254 chan->out_mbuf_cur_len = chan->out_mbuf_cur->m_len;
256 if(sc->sc_trace & TRACE_B_TX)
260 hdr.type = (h_chan == HSCX_CH_A ? TRC_CH_B1 : TRC_CH_B2);
262 hdr.count = ++sc->sc_trace_bcount;
264 MPH_Trace_Ind(&hdr, chan->out_mbuf_cur->m_len, chan->out_mbuf_cur->m_data);
270 * if there is either still data in the current mbuf and/or
271 * there is a successor on the chain available issue just
272 * a XTF (transmit) command to HSCX. if ther is no more
273 * data available from the current mbuf (-chain), issue
274 * an XTF and an XME (message end) command which will then
275 * send the CRC and the closing HDLC flag sequence
278 if(chan->out_mbuf_cur && (chan->out_mbuf_cur_len > 0))
281 * more data available, send current fifo out.
282 * next xfer to HSCX tx fifo is done in the
283 * HSCX interrupt routine.
286 cmd |= HSCX_CMDR_XTF;
290 /* end of mbuf chain */
292 if(chan->bprot == BPROT_NONE)
293 cmd |= HSCX_CMDR_XTF;
295 cmd |= HSCX_CMDR_XTF | HSCX_CMDR_XME;
297 i4b_Bfreembuf(chan->out_mbuf_head); /* free mbuf chain */
299 chan->out_mbuf_head = NULL;
300 chan->out_mbuf_cur = NULL;
301 chan->out_mbuf_cur_ptr = NULL;
302 chan->out_mbuf_cur_len = 0;
305 /* call timeout handling routine */
307 if(activity == ACT_RX || activity == ACT_TX)
308 (*chan->drvr_linktab->bch_activity)(chan->drvr_linktab->unit, activity);
311 isic_hscx_cmd(sc, h_chan, cmd);
316 /*---------------------------------------------------------------------------*
317 * fill statistics struct
318 *---------------------------------------------------------------------------*/
320 isic_bchannel_stat(int unit, int h_chan, bchan_statistics_t *bsp)
322 struct l1_softc *sc = &l1_sc[unit];
323 l1_bchan_state_t *chan = &sc->sc_chan[h_chan];
328 bsp->outbytes = chan->txcount;
329 bsp->inbytes = chan->rxcount;
337 /*---------------------------------------------------------------------------*
338 * return the address of isic drivers linktab
339 *---------------------------------------------------------------------------*/
341 isic_ret_linktab(int unit, int channel)
343 struct l1_softc *sc = &l1_sc[unit];
344 l1_bchan_state_t *chan = &sc->sc_chan[channel];
346 return(&chan->isdn_linktab);
349 /*---------------------------------------------------------------------------*
350 * set the driver linktab in the b channel softc
351 *---------------------------------------------------------------------------*/
353 isic_set_linktab(int unit, int channel, drvr_link_t *dlt)
355 struct l1_softc *sc = &l1_sc[unit];
356 l1_bchan_state_t *chan = &sc->sc_chan[channel];
358 chan->drvr_linktab = dlt;
361 /*---------------------------------------------------------------------------*
362 * initialize our local linktab
363 *---------------------------------------------------------------------------*/
365 isic_init_linktab(struct l1_softc *sc)
367 l1_bchan_state_t *chan = &sc->sc_chan[HSCX_CH_A];
368 isdn_link_t *lt = &chan->isdn_linktab;
370 /* make sure the hardware driver is known to layer 4 */
371 ctrl_types[CTRL_PASSIVE].set_linktab = isic_set_linktab;
372 ctrl_types[CTRL_PASSIVE].get_linktab = isic_ret_linktab;
375 lt->unit = sc->sc_unit;
376 lt->channel = HSCX_CH_A;
377 lt->bch_config = isic_bchannel_setup;
378 lt->bch_tx_start = isic_bchannel_start;
379 lt->bch_stat = isic_bchannel_stat;
380 lt->tx_queue = &chan->tx_queue;
382 /* used by non-HDLC data transfers, i.e. telephony drivers */
383 lt->rx_queue = &chan->rx_queue;
385 /* used by HDLC data transfers, i.e. ipr and isp drivers */
386 lt->rx_mbuf = &chan->in_mbuf;
388 chan = &sc->sc_chan[HSCX_CH_B];
389 lt = &chan->isdn_linktab;
391 lt->unit = sc->sc_unit;
392 lt->channel = HSCX_CH_B;
393 lt->bch_config = isic_bchannel_setup;
394 lt->bch_tx_start = isic_bchannel_start;
395 lt->bch_stat = isic_bchannel_stat;
396 lt->tx_queue = &chan->tx_queue;
398 /* used by non-HDLC data transfers, i.e. telephony drivers */
399 lt->rx_queue = &chan->rx_queue;
401 /* used by HDLC data transfers, i.e. ipr and isp drivers */
402 lt->rx_mbuf = &chan->in_mbuf;
405 /*---------------------------------------------------------------------------*
406 * telephony silence detection
407 *---------------------------------------------------------------------------*/
409 #define TEL_IDLE_MIN (BCH_MAX_DATALEN/2)
412 isic_hscx_silence(unsigned char *data, int len)
417 /* count idle bytes */
421 if((*data >= 0xaa) && (*data <= 0xac))
427 printf("isic_hscx_silence: got %d silence bytes in frame\n", j);
430 if(j < (TEL_IDLE_MIN))
437 #endif /* NISIC > 0 */