No empty .Rs/.Re
[netbsd-mini2440.git] / sys / dev / ic / isic_l1.c
blob30152563c07bf935dfa3d5e4331b5cee614ebc32
1 /* $NetBSD: isic_l1.c,v 1.17 2007/10/19 11:59:54 ad Exp $ */
3 /*
4 * Copyright (c) 1997, 2000 Hellmuth Michaelis. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: isic_l1.c,v 1.17 2007/10/19 11:59:54 ad Exp $");
32 #include <sys/param.h>
33 #include <sys/ioctl.h>
34 #include <sys/kernel.h>
35 #include <sys/systm.h>
36 #include <sys/mbuf.h>
38 #include <sys/bus.h>
39 #include <sys/device.h>
41 #include <sys/socket.h>
42 #include <net/if.h>
44 #include <sys/callout.h>
46 #include <netisdn/i4b_debug.h>
47 #include <netisdn/i4b_ioctl.h>
48 #include <netisdn/i4b_trace.h>
50 #include <netisdn/i4b_l2.h>
51 #include <netisdn/i4b_l1l2.h>
52 #include <netisdn/i4b_mbuf.h>
53 #include <netisdn/i4b_global.h>
55 #include <dev/ic/isic_l1.h>
56 #include <dev/ic/isac.h>
57 #include <dev/ic/ipac.h>
58 #include <dev/ic/hscx.h>
60 #include "nisac.h"
61 #include "nisacsx.h"
63 unsigned int i4b_l1_debug = L1_DEBUG_DEFAULT;
65 static int isic_std_ph_data_req(isdn_layer1token, struct mbuf *, int);
66 static int isic_std_ph_activate_req(isdn_layer1token);
67 static int isic_std_mph_command_req(isdn_layer1token, int, void*);
68 static void isic_enable_intr(struct isic_softc *sc, int enable);
70 const struct isdn_layer1_isdnif_driver isic_std_driver = {
71 isic_std_ph_data_req,
72 isic_std_ph_activate_req,
73 isic_std_mph_command_req
76 /* from i4btrc driver i4b_trace.c */
77 extern int get_trace_data_from_l1(int unit, int what, int len, char *buf);
79 /*---------------------------------------------------------------------------*
81 * L2 -> L1: PH-DATA-REQUEST
82 * =========================
84 * parms:
85 * token softc of physical driver
86 * m mbuf containing L2 frame to be sent out
87 * freeflag MBUF_FREE: free mbuf here after having sent
88 * it out
89 * MBUF_DONTFREE: mbuf is freed by Layer 2
90 * returns:
91 * ==0 fail, nothing sent out
92 * !=0 ok, frame sent out
94 *---------------------------------------------------------------------------*/
95 static int
96 isic_std_ph_data_req(isdn_layer1token token, struct mbuf *m, int freeflag)
98 struct isic_softc *sc = (struct isic_softc*)token;
99 u_char cmd;
100 int s;
102 if (m == NULL) /* failsafe */
103 return (0);
105 s = splnet();
107 if(sc->sc_I430state == ST_F3) /* layer 1 not running ? */
109 NDBGL1(L1_I_ERR, "still in state F3!");
110 isic_std_ph_activate_req(token);
113 if(sc->sc_state & ISAC_TX_ACTIVE)
115 if(sc->sc_obuf2 == NULL)
117 sc->sc_obuf2 = m; /* save mbuf ptr */
119 if(freeflag)
120 sc->sc_freeflag2 = 1; /* IRQ must mfree */
121 else
122 sc->sc_freeflag2 = 0; /* IRQ must not mfree */
124 NDBGL1(L1_I_MSG, "using 2nd ISAC TX buffer, state = %s", isic_printstate(sc));
126 if(sc->sc_trace & TRACE_D_TX)
128 i4b_trace_hdr hdr;
129 hdr.type = TRC_CH_D;
130 hdr.dir = FROM_TE;
131 hdr.count = ++sc->sc_trace_dcount;
132 isdn_layer2_trace_ind(&sc->sc_l2, sc->sc_l3token, &hdr, m->m_len, m->m_data);
134 splx(s);
135 return(1);
138 NDBGL1(L1_I_ERR, "No Space in TX FIFO, state = %s", isic_printstate(sc));
140 if(freeflag == MBUF_FREE)
141 i4b_Dfreembuf(m);
143 splx(s);
144 return (0);
147 if(sc->sc_trace & TRACE_D_TX)
149 i4b_trace_hdr hdr;
150 hdr.type = TRC_CH_D;
151 hdr.dir = FROM_TE;
152 hdr.count = ++sc->sc_trace_dcount;
153 isdn_layer2_trace_ind(&sc->sc_l2, sc->sc_l3token, &hdr, m->m_len, m->m_data);
156 sc->sc_state |= ISAC_TX_ACTIVE; /* set transmitter busy flag */
158 NDBGL1(L1_I_MSG, "ISAC_TX_ACTIVE set");
160 sc->sc_freeflag = 0; /* IRQ must NOT mfree */
162 ISAC_WRFIFO(m->m_data, min(m->m_len, ISAC_FIFO_LEN)); /* output to TX fifo */
164 if(m->m_len > ISAC_FIFO_LEN) /* message > 32 bytes ? */
166 sc->sc_obuf = m; /* save mbuf ptr */
167 sc->sc_op = m->m_data + ISAC_FIFO_LEN; /* ptr for irq hdl */
168 sc->sc_ol = m->m_len - ISAC_FIFO_LEN; /* length for irq hdl */
170 if(freeflag)
171 sc->sc_freeflag = 1; /* IRQ must mfree */
173 cmd = ISAC_CMDR_XTF;
175 else
177 sc->sc_obuf = NULL;
178 sc->sc_op = NULL;
179 sc->sc_ol = 0;
181 if(freeflag)
182 i4b_Dfreembuf(m);
184 cmd = ISAC_CMDR_XTF | ISAC_CMDR_XME;
187 ISAC_WRITE(I_CMDR, cmd);
188 ISACCMDRWRDELAY();
190 splx(s);
192 return(1);
195 /*---------------------------------------------------------------------------*
197 * L2 -> L1: PH-ACTIVATE-REQUEST
198 * =============================
200 * parms:
201 * token softc of physical interface
203 * returns:
204 * ==0
205 * !=0
207 *---------------------------------------------------------------------------*/
208 static int
209 isic_std_ph_activate_req(isdn_layer1token token)
211 struct isic_softc *sc = (struct isic_softc*)token;
213 NDBGL1(L1_PRIM, " %s ", device_xname(&sc->sc_dev));
214 isic_next_state(sc, EV_PHAR);
215 return(0);
218 /*---------------------------------------------------------------------------*
219 * command from the upper layers
220 *---------------------------------------------------------------------------*/
221 static int
222 isic_std_mph_command_req(isdn_layer1token token, int command, void *parm)
224 struct isic_softc *sc = (struct isic_softc*)token;
225 int s, pass_down = 0;
227 s = splnet();
228 switch(command)
230 case CMR_DOPEN: /* daemon running */
231 NDBGL1(L1_PRIM, "%s, command = CMR_DOPEN", device_xname(&sc->sc_dev));
232 sc->sc_intr_valid = ISIC_INTR_VALID;
233 pass_down = 1;
234 break;
236 case CMR_DCLOSE: /* daemon not running */
237 NDBGL1(L1_PRIM, "%s, command = CMR_DCLOSE", device_xname(&sc->sc_dev));
238 sc->sc_intr_valid = ISIC_INTR_DISABLED;
239 isic_enable_intr(sc, 0);
240 pass_down = 1;
241 break;
243 case CMR_SETLEDS:
244 pass_down = 1;
245 break;
247 case CMR_SETTRACE:
248 NDBGL1(L1_PRIM, "%s, command = CMR_SETTRACE, parm = %p", device_xname(&sc->sc_dev), parm);
249 sc->sc_trace = (int)(unsigned long)parm;
250 break;
252 default:
253 NDBGL1(L1_ERROR, "ERROR, unknown command = %d, %s, parm = %p", command, device_xname(&sc->sc_dev), parm);
254 break;
257 if (pass_down && sc->drv_command != NULL)
258 sc->drv_command(sc, command, parm);
260 if (command == CMR_DOPEN)
261 isic_enable_intr(sc, 1);
263 splx(s);
265 return(0);
268 static void
269 isic_enable_intr(struct isic_softc *sc, int enable)
271 #if NNISACSX > 0
272 if (sc->sc_cardtyp == CARD_TYPEP_AVMA1PCIV2) {
273 if (enable)
274 isic_isacsx_init(sc);
275 else isic_isacsx_disable_intr(sc);
276 return;
278 #endif /* NNISACSX > 0 */
280 #if NNISAC > 0
281 if (enable) {
282 isic_isac_init(sc);
283 } else {
284 /* disable receiver */
285 ISAC_WRITE(I_MODE, ISAC_MODE_MDS2|ISAC_MODE_MDS1|ISAC_MODE_DIM0);
286 /* mask interrupts */
287 if (sc->sc_ipac) {
288 IPAC_WRITE(IPAC_MASK, 0xff);
289 } else {
290 ISAC_WRITE(I_MASK, 0xff);
293 #endif /* NNISAC > 0 */