if_awi_pcmcia.c revision 1.2 1 /*-
2 * Copyright (c) 1999 The NetBSD Foundation, Inc.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Bill Sommerfeld
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the NetBSD
19 * Foundation, Inc. and its contributors.
20 * 4. Neither the name of The NetBSD Foundation nor the names of its
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
25 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
28 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37 /*
38 * PCMCIA attachment for BayStack 650 802.11FH PCMCIA card,
39 * based on the AMD 79c930 802.11 controller chip.
40 *
41 * This attachment can probably be trivally adapted for other FH and
42 * DS cards based on the same chipset.
43 */
44
45 #include "opt_inet.h"
46 #include "opt_ns.h"
47 #include "bpfilter.h"
48
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/mbuf.h>
52 #include <sys/socket.h>
53 #include <sys/ioctl.h>
54 #include <sys/errno.h>
55 #include <sys/syslog.h>
56 #include <sys/select.h>
57 #include <sys/device.h>
58
59 #include <net/if.h>
60 #include <net/if_dl.h>
61 #include <net/if_ether.h>
62 #include <net/if_media.h>
63
64 #ifdef INET
65 #include <netinet/in.h>
66 #include <netinet/in_systm.h>
67 #include <netinet/in_var.h>
68 #include <netinet/ip.h>
69 #include <netinet/if_inarp.h>
70 #endif
71
72 #ifdef NS
73 #include <netns/ns.h>
74 #include <netns/ns_if.h>
75 #endif
76
77 #if NBPFILTER > 0
78 #include <net/bpf.h>
79 #include <net/bpfdesc.h>
80 #endif
81
82 #include <machine/cpu.h>
83 #include <machine/bus.h>
84 #include <machine/intr.h>
85
86 #include <dev/ic/am79c930reg.h>
87 #include <dev/ic/am79c930var.h>
88 #include <dev/ic/awireg.h>
89 #include <dev/ic/awivar.h>
90
91 #include <dev/pcmcia/pcmciareg.h>
92 #include <dev/pcmcia/pcmciavar.h>
93 #include <dev/pcmcia/pcmciadevs.h>
94
95 int awi_pcmcia_match __P((struct device *, struct cfdata *, void *));
96 void awi_pcmcia_attach __P((struct device *, struct device *, void *));
97 int awi_pcmcia_detach __P((struct device *, int));
98
99 int awi_pcmcia_get_enaddr __P((struct pcmcia_tuple *, void *));
100 int awi_pcmcia_enable __P((struct awi_softc *));
101 void awi_pcmcia_disable __P((struct awi_softc *));
102
103 struct awi_pcmcia_softc {
104 struct awi_softc sc_awi; /* real "awi" softc */
105
106 /* PCMCIA-specific goo */
107 struct pcmcia_io_handle sc_pcioh; /* PCMCIA i/o space info */
108 int sc_io_window; /* our i/o window */
109 struct pcmcia_function *sc_pf; /* our PCMCIA function */
110 };
111
112 int awi_pcmcia_find __P((struct awi_pcmcia_softc *,
113 struct pcmcia_attach_args *, struct pcmcia_config_entry *));
114
115 struct cfattach awi_pcmcia_ca = {
116 sizeof(struct awi_pcmcia_softc), awi_pcmcia_match, awi_pcmcia_attach,
117 awi_pcmcia_detach, /* awi_activate */ 0
118 };
119
120 int
121 awi_pcmcia_enable(sc)
122 struct awi_softc *sc;
123 {
124 struct awi_pcmcia_softc *psc = (struct awi_pcmcia_softc *) sc;
125 struct pcmcia_function *pf = psc->sc_pf;
126
127 /* establish the interrupt. */
128 sc->sc_ih = pcmcia_intr_establish(pf, IPL_NET, awi_intr, sc);
129 if (sc->sc_ih == NULL) {
130 printf("%s: couldn't establish interrupt\n",
131 sc->sc_dev.dv_xname);
132 return (1);
133 }
134 return (pcmcia_function_enable(pf));
135 }
136
137 void
138 awi_pcmcia_disable(sc)
139 struct awi_softc *sc;
140 {
141 struct awi_pcmcia_softc *psc = (struct awi_pcmcia_softc *) sc;
142 struct pcmcia_function *pf = psc->sc_pf;
143
144 pcmcia_function_disable (pf);
145 pcmcia_intr_disestablish (pf, sc->sc_ih);
146 }
147
148 int
149 awi_pcmcia_match(parent, match, aux)
150 struct device *parent;
151 struct cfdata *match;
152 void *aux;
153 {
154 struct pcmcia_attach_args *pa = aux;
155
156 if (pa->manufacturer != PCMCIA_VENDOR_BAY)
157 return (0);
158
159 if (pa->product == PCMCIA_PRODUCT_BAY_STACK_650)
160 return (1);
161
162 return (0);
163 }
164
165 int
166 awi_pcmcia_find (psc, pa, cfe)
167 struct awi_pcmcia_softc *psc;
168 struct pcmcia_attach_args *pa;
169 struct pcmcia_config_entry *cfe;
170 {
171 struct awi_softc *sc = &psc->sc_awi;
172 int fail = 0;
173 u_int8_t version[AWI_BANNER_LEN];
174
175 /*
176 * see if we can read the firmware version sanely
177 * through the i/o ports.
178 * if not, try a different CIS string..
179 */
180 if (pcmcia_io_alloc(psc->sc_pf, cfe->iospace[0].start,
181 cfe->iospace[0].length, 0, &psc->sc_pcioh) != 0)
182 goto fail;
183
184 if (pcmcia_io_map(psc->sc_pf, PCMCIA_WIDTH_AUTO, 0, psc->sc_pcioh.size,
185 &psc->sc_pcioh, &psc->sc_io_window))
186 goto fail_io_free;
187
188 /* Enable the card. */
189 pcmcia_function_init(psc->sc_pf, cfe);
190 if (pcmcia_function_enable(psc->sc_pf))
191 goto fail_io_unmap;
192
193 sc->sc_chip.sc_iot = psc->sc_pcioh.iot;
194 sc->sc_chip.sc_ioh = psc->sc_pcioh.ioh;
195 am79c930_chip_init(&sc->sc_chip, 0);
196
197 DELAY(100);
198
199 awi_read_bytes (sc, AWI_BANNER, version, AWI_BANNER_LEN);
200
201 if (memcmp(version, "PCnetMobile:", 12) == 0)
202 return 0;
203
204 fail++;
205 pcmcia_function_disable (psc->sc_pf);
206
207 fail_io_unmap:
208 fail++;
209 pcmcia_io_unmap (psc->sc_pf, psc->sc_io_window);
210
211 fail_io_free:
212 fail++;
213 pcmcia_io_free (psc->sc_pf, &psc->sc_pcioh);
214 fail:
215 fail++;
216 return fail;
217 }
218
219
220
221 void
222 awi_pcmcia_attach(parent, self, aux)
223 struct device *parent, *self;
224 void *aux;
225 {
226 struct awi_pcmcia_softc *psc = (void *) self;
227 struct awi_softc *sc = &psc->sc_awi;
228 struct pcmcia_attach_args *pa = aux;
229 struct pcmcia_config_entry *cfe;
230 struct pcmcia_mem_handle memh;
231 bus_addr_t memoff;
232 int memwin;
233
234 #if 0
235 int i, j;
236
237 for (cfe = pa->pf->cfe_head.sqh_first, i=0;
238 cfe != NULL;
239 cfe = cfe->cfe_list.sqe_next, i++) {
240 printf("%d: %d memspaces, %d iospaces\n",
241 i, cfe->num_memspace, cfe->num_iospace);
242 printf("%d: number %d flags %x iftype %d iomask %lx irqmask %x maxtwins %x\n",
243 i, cfe->number, cfe->flags, cfe->iftype, cfe->iomask,
244 cfe->irqmask, cfe->maxtwins);
245 for (j=0; j<cfe->num_memspace; j++) {
246 printf("%d: mem %d: len %lx card %lx host %lx\n",
247 i, j,
248 cfe->memspace[j].length,
249 cfe->memspace[j].cardaddr,
250 cfe->memspace[j].hostaddr);
251 }
252 for (j=0; j<cfe->num_iospace; j++) {
253 printf("%d: io %d: len %lx start %lx\n",
254 i, j,
255 cfe->iospace[j].length,
256 cfe->iospace[j].start);
257 }
258 }
259 #endif
260
261 psc->sc_pf = pa->pf;
262
263 for (cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head); cfe != NULL;
264 cfe = SIMPLEQ_NEXT(cfe, cfe_list)) {
265 if (cfe->iftype != PCMCIA_IFTYPE_IO)
266 continue;
267 if (cfe->num_iospace < 1)
268 continue;
269 if (cfe->iospace[0].length < AM79C930_IO_SIZE)
270 continue;
271
272 if (awi_pcmcia_find(psc, pa, cfe) == 0)
273 break;
274 }
275 if (cfe == NULL) {
276 printf(": no suitable CIS info found\n");
277 return;
278 }
279
280 sc->sc_enabled = 1;
281 sc->sc_state = AWI_ST_SELFTEST;
282 printf(": BayStack 650 Wireless (802.11)\n");
283
284 if (pcmcia_mem_alloc(psc->sc_pf, AM79C930_MEM_SIZE, &memh) != 0) {
285 printf("%s: unable to allocate memory space; using i/o only\n",
286 sc->sc_dev.dv_xname);
287 } else if (pcmcia_mem_map(psc->sc_pf, PCMCIA_MEM_COMMON,
288 AM79C930_MEM_BASE, AM79C930_MEM_SIZE,
289 &memh, &memoff, &memwin)) {
290 printf("%s: unable to map memory space; using i/o only\n",
291 sc->sc_dev.dv_xname);
292 pcmcia_mem_free(psc->sc_pf, &memh);
293 } else {
294 sc->sc_chip.sc_memt = memh.memt;
295 sc->sc_chip.sc_memh = memh.memh;
296 am79c930_chip_init(&sc->sc_chip, 1);
297 }
298
299 sc->sc_chip.sc_bustype = AM79C930_BUS_PCMCIA;
300
301 sc->sc_enable = awi_pcmcia_enable;
302 sc->sc_disable = awi_pcmcia_disable;
303
304 awi_attach(sc);
305
306 sc->sc_state = AWI_ST_OFF;
307
308 sc->sc_enabled = 0;
309 pcmcia_function_disable(psc->sc_pf);
310 }
311
312
313 int
314 awi_pcmcia_detach(self, flags)
315 struct device *self;
316 int flags;
317 {
318 struct awi_pcmcia_softc *psc = (struct awi_pcmcia_softc *)self;
319
320 /* Unmap our i/o window. */
321 pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
322
323 /* Free our i/o space. */
324 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
325
326 #ifdef notyet
327 /*
328 * Our softc is about to go away, so drop our reference
329 * to the ifnet.
330 */
331 if_delref(psc->sc_awi.sc_ethercom.ec_if);
332 return (0);
333 #else
334 return (EBUSY);
335 #endif
336 }
337