kernel: Use NULL for pointers.
[dragonfly.git] / sys / net / i4b / capi / iavc / iavc_isa.c
1 /*
2  * Copyright (c) 2001 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  * $FreeBSD: src/sys/i4b/capi/iavc/iavc_isa.c,v 1.1.2.1 2001/08/10 14:08:34 obrien Exp $
26  */
27
28 #include <sys/param.h>
29 #include <sys/kernel.h>
30 #include <sys/systm.h>
31 #include <sys/mbuf.h>
32 #include <sys/socket.h>
33 #include <sys/bus.h>
34 #include <sys/rman.h>
35
36 #include <machine/clock.h>
37
38 #include <net/if.h>
39 #include <vm/vm.h>
40 #include <vm/pmap.h>
41
42 #include <bus/isa/isavar.h>
43
44 #include <net/i4b/include/machine/i4b_debug.h>
45 #include <net/i4b/include/machine/i4b_ioctl.h>
46 #include <net/i4b/include/machine/i4b_trace.h>
47
48 #include "../../include/i4b_global.h"
49 #include "../../include/i4b_l3l4.h"
50 #include "../../include/i4b_mbuf.h"
51 #include "../capi.h"
52
53 #include "iavc.h"
54
55 /* ISA driver linkage */
56
57 static void iavc_isa_intr(iavc_softc_t *sc);
58 static int iavc_isa_probe(device_t dev);
59 static int iavc_isa_attach(device_t dev);
60
61 static device_method_t iavc_isa_methods[] =
62 {
63     DEVMETHOD(device_probe,     iavc_isa_probe),
64     DEVMETHOD(device_attach,    iavc_isa_attach),
65     { 0, 0 }
66 };
67
68 static driver_t iavc_isa_driver =
69 {
70     "iavc",
71     iavc_isa_methods,
72     0
73 };
74
75 static devclass_t iavc_isa_devclass;
76
77 DRIVER_MODULE(iavc, isa, iavc_isa_driver, iavc_isa_devclass, NULL, NULL);
78
79 #define B1_IOLENGTH     0x20
80
81 static int b1_irq_table[] =
82 {0, 0, 0, 192, 32, 160, 96, 224, 0, 64, 80, 208, 48, 0, 0, 112};
83 /*        3    4   5    6   7       9   10  11   12        15 */
84
85 /*---------------------------------------------------------------------------*
86  *      device probe
87  *---------------------------------------------------------------------------*/
88
89 static int
90 iavc_isa_probe(device_t dev)
91 {
92         struct iavc_softc *sc;
93         int ret = ENXIO;
94         int unit = device_get_unit(dev);
95         
96         if(isa_get_vendorid(dev))       /* no PnP probes here */
97                 return ENXIO;
98
99         /* check max unit range */
100         
101         if (unit >= IAVC_MAXUNIT)
102         {
103                 kprintf("iavc%d: too many units\n", unit);
104                 return(ENXIO);  
105         }
106
107         sc = iavc_find_sc(unit);        /* get softc */ 
108         
109         sc->sc_unit = unit;
110
111         if (!(sc->sc_resources.io_base[0] =
112                 bus_alloc_resource(dev, SYS_RES_IOPORT,
113                         &sc->sc_resources.io_rid[0],
114                         0UL, ~0UL, B1_IOLENGTH, RF_ACTIVE)))
115         {
116                 kprintf("iavc%d: can't allocate io region\n", unit);
117                 return(ENXIO);                                       
118         }
119
120         sc->sc_iobase = rman_get_start(sc->sc_resources.io_base[0]);
121
122         switch(sc->sc_iobase)
123         {
124                 case 0x150:
125                 case 0x250:
126                 case 0x300:
127                 case 0x340:
128                         break;
129                 default:
130                         kprintf("iavc%d: ERROR, invalid i/o base addr 0x%x configured!\n", sc->sc_unit, sc->sc_iobase);
131                         bus_release_resource(dev, SYS_RES_IOPORT,
132                                         sc->sc_resources.io_rid[0],
133                                         sc->sc_resources.io_base[0]);
134                 return(ENXIO);
135         }       
136         
137         sc->sc_io_bt = rman_get_bustag(sc->sc_resources.io_base[0]);
138         sc->sc_io_bh = rman_get_bushandle(sc->sc_resources.io_base[0]);
139
140         /* setup characteristics */
141
142         sc->sc_t1 = FALSE;
143         sc->sc_dma = FALSE;
144
145         sc->sc_capi.card_type = CARD_TYPEC_AVM_B1_ISA;
146         sc->sc_capi.sc_nbch = 2;
147
148         b1_reset(sc);
149         DELAY(100);
150
151         ret = b1_detect(sc);
152
153         if(ret)
154         {
155                 kprintf("iavc%d: no card ? b1_detect returns %0x02x\n", sc->sc_unit, ret);
156                 return(ENXIO);
157         }
158
159         DELAY(100);
160
161         b1_reset(sc);
162         
163         DELAY(100);
164
165         if(bootverbose)
166         {
167                 kprintf("iavc%d: class = 0x%02x, rev = 0x%02x\n", sc->sc_unit,
168                         iavc_read_port(sc, B1_ANALYSE),
169                         iavc_read_port(sc, B1_REVISION));
170         }
171
172         device_set_desc(dev, "AVM B1 ISA");
173         return(0);
174 }
175
176 /*---------------------------------------------------------------------------*
177  *      attach
178  *---------------------------------------------------------------------------*/
179 static int
180 iavc_isa_attach(device_t dev)
181 {
182         struct iavc_softc *sc;
183         void *ih = NULL;
184         int unit = device_get_unit(dev);
185         int irq;
186         int error;
187         
188         sc = iavc_find_sc(unit);        /* get softc */ 
189         
190         sc->sc_resources.irq_rid = 0;
191         
192         if(!(sc->sc_resources.irq =
193                 bus_alloc_resource(dev, SYS_RES_IRQ,
194                         &sc->sc_resources.irq_rid,
195                         0UL, ~0UL, 1, RF_ACTIVE)))
196         {
197                 kprintf("iavc%d: can't allocate irq\n",unit);
198                 bus_release_resource(dev, SYS_RES_IOPORT,
199                                 sc->sc_resources.io_rid[0],
200                                 sc->sc_resources.io_base[0]);
201                 return(ENXIO);
202         }
203
204         irq = rman_get_start(sc->sc_resources.irq);
205
206         if(b1_irq_table[irq] == 0)
207         {
208                 kprintf("iavc%d: ERROR, illegal irq %d configured!\n",unit, irq);
209                 bus_release_resource(dev, SYS_RES_IOPORT,
210                                 sc->sc_resources.io_rid[0],
211                                 sc->sc_resources.io_base[0]);
212                 bus_release_resource(dev, SYS_RES_IRQ,
213                                 sc->sc_resources.irq_rid,
214                                 sc->sc_resources.irq);
215                 return(ENXIO);
216         }
217         
218         memset(&sc->sc_txq, 0, sizeof(struct ifqueue));
219         sc->sc_txq.ifq_maxlen = sc->sc_capi.sc_nbch * 4;
220
221         sc->sc_intr = FALSE;
222         sc->sc_state = IAVC_DOWN;
223         sc->sc_blocked = FALSE;
224
225         /* setup capi link */
226         
227         sc->sc_capi.load = iavc_load;
228         sc->sc_capi.reg_appl = iavc_register;
229         sc->sc_capi.rel_appl = iavc_release;
230         sc->sc_capi.send = iavc_send;
231         sc->sc_capi.ctx = (void*) sc;
232
233         if (capi_ll_attach(&sc->sc_capi))
234         {
235                 kprintf("iavc%d: capi attach failed\n", unit);
236                 return(ENXIO);
237         }
238
239         /* setup the interrupt */
240
241         error = bus_setup_intr(dev, sc->sc_resources.irq, 0,
242                               (void(*)(void*))iavc_isa_intr,
243                               sc, &ih, NULL);
244         if (error) {
245                 kprintf("iavc%d: irq setup failed\n", unit);
246                 bus_release_resource(dev, SYS_RES_IOPORT,
247                                 sc->sc_resources.io_rid[0],
248                                 sc->sc_resources.io_base[0]);
249                 bus_release_resource(dev, SYS_RES_IRQ,
250                                 sc->sc_resources.irq_rid,
251                                 sc->sc_resources.irq);
252                 return(ENXIO);
253         }
254
255         /* the board is now ready to be loaded */
256
257         return(0);
258 }
259
260 /*---------------------------------------------------------------------------*
261  *      setup interrupt
262  *---------------------------------------------------------------------------*/
263 void
264 b1isa_setup_irq(struct iavc_softc *sc)
265 {
266         int irq = rman_get_start(sc->sc_resources.irq);
267         
268         if(bootverbose)
269                 kprintf("iavc%d: using irq %d\n", sc->sc_unit, irq);
270
271         /* enable the interrupt */
272
273         b1io_outp(sc, B1_INSTAT, 0x00);
274         b1io_outp(sc, B1_RESET, b1_irq_table[irq]);
275         b1io_outp(sc, B1_INSTAT, 0x02);
276 }       
277
278 /*---------------------------------------------------------------------------*
279  *      IRQ handler
280  *---------------------------------------------------------------------------*/
281 static void
282 iavc_isa_intr(struct iavc_softc *sc)
283 {
284         iavc_handle_intr(sc);
285 }