if_awi_pcmcia.c revision 1.14 1 /* $NetBSD: if_awi_pcmcia.c,v 1.14 2000/03/22 22:33:47 mycroft Exp $ */
2
3 /*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Bill Sommerfeld
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * PCMCIA attachment for BayStack 650 802.11FH PCMCIA card,
41 * based on the AMD 79c930 802.11 controller chip.
42 *
43 * This attachment can probably be trivally adapted for other FH and
44 * DS cards based on the same chipset.
45 */
46
47 #include "opt_inet.h"
48 #include "opt_ns.h"
49 #include "bpfilter.h"
50
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/mbuf.h>
54 #include <sys/socket.h>
55 #include <sys/ioctl.h>
56 #include <sys/errno.h>
57 #include <sys/syslog.h>
58 #include <sys/select.h>
59 #include <sys/device.h>
60
61 #include <net/if.h>
62 #include <net/if_dl.h>
63 #include <net/if_ether.h>
64 #include <net/if_media.h>
65 #include <net/if_ieee80211.h>
66
67 #ifdef INET
68 #include <netinet/in.h>
69 #include <netinet/in_systm.h>
70 #include <netinet/in_var.h>
71 #include <netinet/ip.h>
72 #include <netinet/if_inarp.h>
73 #endif
74
75 #ifdef NS
76 #include <netns/ns.h>
77 #include <netns/ns_if.h>
78 #endif
79
80 #if NBPFILTER > 0
81 #include <net/bpf.h>
82 #include <net/bpfdesc.h>
83 #endif
84
85 #include <machine/cpu.h>
86 #include <machine/bus.h>
87 #include <machine/intr.h>
88
89 #include <dev/ic/am79c930reg.h>
90 #include <dev/ic/am79c930var.h>
91 #include <dev/ic/awireg.h>
92 #include <dev/ic/awivar.h>
93
94 #include <dev/pcmcia/pcmciareg.h>
95 #include <dev/pcmcia/pcmciavar.h>
96 #include <dev/pcmcia/pcmciadevs.h>
97
98 static int awi_pcmcia_match __P((struct device *, struct cfdata *, void *));
99 static void awi_pcmcia_attach __P((struct device *, struct device *, void *));
100 static int awi_pcmcia_detach __P((struct device *, int));
101 static int awi_pcmcia_enable __P((struct awi_softc *));
102 static void awi_pcmcia_disable __P((struct awi_softc *));
103 static void awi_pcmcia_powerhook __P((int, void *));
104
105 struct awi_pcmcia_softc {
106 struct awi_softc sc_awi; /* real "awi" softc */
107
108 /* PCMCIA-specific goo */
109 struct pcmcia_io_handle sc_pcioh; /* PCMCIA i/o space info */
110 struct pcmcia_mem_handle sc_memh; /* PCMCIA memory space info */
111 int sc_io_window; /* our i/o window */
112 int sc_mem_window; /* our memory window */
113 struct pcmcia_function *sc_pf; /* our PCMCIA function */
114 void *sc_powerhook; /* power hook descriptor */
115 };
116
117 static int awi_pcmcia_find __P((struct awi_pcmcia_softc *,
118 struct pcmcia_attach_args *, struct pcmcia_config_entry *));
119
120 struct cfattach awi_pcmcia_ca = {
121 sizeof(struct awi_pcmcia_softc), awi_pcmcia_match, awi_pcmcia_attach,
122 awi_pcmcia_detach, awi_activate
123 };
124
125 #if __NetBSD_Version__ <= 104120000
126 #define PCMCIA_VENDOR_BAY 0x01eb /* Bay Networks */
127 #define PCMCIA_PRODUCT_BAY_STACK_650 0x804
128 #endif
129
130 static struct awi_pcmcia_product {
131 u_int32_t app_vendor; /* vendor ID */
132 u_int32_t app_product; /* product ID */
133 const char *app_cisinfo[4]; /* CIS information */
134 const char *app_name; /* product name */
135 } awi_pcmcia_products[] = {
136 { PCMCIA_VENDOR_BAY, PCMCIA_PRODUCT_BAY_STACK_650,
137 PCMCIA_CIS_BAY_STACK_650, PCMCIA_STR_BAY_STACK_650 },
138
139 { PCMCIA_VENDOR_BAY, PCMCIA_PRODUCT_BAY_STACK_660,
140 PCMCIA_CIS_BAY_STACK_660, PCMCIA_STR_BAY_STACK_660 },
141
142 { PCMCIA_VENDOR_BAY, PCMCIA_PRODUCT_BAY_SURFER_PRO,
143 PCMCIA_CIS_BAY_SURFER_PRO, PCMCIA_STR_BAY_SURFER_PRO },
144
145 { PCMCIA_VENDOR_AMD, PCMCIA_PRODUCT_AMD_AM79C930,
146 PCMCIA_CIS_AMD_AM79C930, PCMCIA_STR_AMD_AM79C930 },
147
148 { PCMCIA_VENDOR_ICOM, PCMCIA_PRODUCT_ICOM_SL200,
149 PCMCIA_CIS_ICOM_SL200, PCMCIA_STR_ICOM_SL200 },
150
151 { PCMCIA_VENDOR_NOKIA, PCMCIA_PRODUCT_NOKIA_C020_WLAN,
152 PCMCIA_CIS_NOKIA_C020_WLAN, PCMCIA_STR_NOKIA_C020_WLAN },
153
154 { 0, 0,
155 { NULL, NULL, NULL, NULL }, NULL },
156 };
157
158 static struct awi_pcmcia_product *
159 awi_pcmcia_lookup __P((struct pcmcia_attach_args *));
160
161 static struct awi_pcmcia_product *
162 awi_pcmcia_lookup(pa)
163 struct pcmcia_attach_args *pa;
164 {
165 struct awi_pcmcia_product *app;
166
167 for (app = awi_pcmcia_products; app->app_name != NULL; app++) {
168 /* match by vendor/product id */
169 if (pa->manufacturer != PCMCIA_VENDOR_INVALID &&
170 pa->manufacturer == app->app_vendor &&
171 pa->product != PCMCIA_PRODUCT_INVALID &&
172 pa->product == app->app_product)
173 return (app);
174
175 /* match by CIS information */
176 if (pa->card->cis1_info[0] != NULL &&
177 app->app_cisinfo[0] != NULL &&
178 strcmp(pa->card->cis1_info[0], app->app_cisinfo[0]) == 0 &&
179 pa->card->cis1_info[1] != NULL &&
180 app->app_cisinfo[1] != NULL &&
181 strcmp(pa->card->cis1_info[1], app->app_cisinfo[1]) == 0)
182 return (app);
183 }
184
185 return (NULL);
186 }
187
188 static int
189 awi_pcmcia_enable(sc)
190 struct awi_softc *sc;
191 {
192 struct awi_pcmcia_softc *psc = (struct awi_pcmcia_softc *)sc;
193 struct pcmcia_function *pf = psc->sc_pf;
194
195 /* establish the interrupt. */
196 sc->sc_ih = pcmcia_intr_establish(pf, IPL_NET, awi_intr, sc);
197 if (sc->sc_ih == NULL) {
198 printf("%s: couldn't establish interrupt\n",
199 sc->sc_dev.dv_xname);
200 return (1);
201 }
202
203 if (pcmcia_function_enable(pf)) {
204 pcmcia_intr_disestablish(pf, sc->sc_ih);
205 return (1);
206 }
207 DELAY(1000);
208
209 return (0);
210 }
211
212 static void
213 awi_pcmcia_disable(sc)
214 struct awi_softc *sc;
215 {
216 struct awi_pcmcia_softc *psc = (struct awi_pcmcia_softc *)sc;
217 struct pcmcia_function *pf = psc->sc_pf;
218
219 pcmcia_function_disable(pf);
220 pcmcia_intr_disestablish(pf, sc->sc_ih);
221 }
222
223 static int
224 awi_pcmcia_match(parent, match, aux)
225 struct device *parent;
226 struct cfdata *match;
227 void *aux;
228 {
229 struct pcmcia_attach_args *pa = aux;
230
231 if (awi_pcmcia_lookup(pa) != NULL)
232 return (1);
233
234 return (0);
235 }
236
237 static int
238 awi_pcmcia_find(psc, pa, cfe)
239 struct awi_pcmcia_softc *psc;
240 struct pcmcia_attach_args *pa;
241 struct pcmcia_config_entry *cfe;
242 {
243 struct awi_softc *sc = &psc->sc_awi;
244 int fail = 0;
245 u_int8_t version[AWI_BANNER_LEN];
246
247 /*
248 * see if we can read the firmware version sanely
249 * through the i/o ports.
250 * if not, try a different CIS string..
251 */
252 if (pcmcia_io_alloc(psc->sc_pf, cfe->iospace[0].start,
253 cfe->iospace[0].length, AM79C930_IO_ALIGN,
254 &psc->sc_pcioh) != 0)
255 goto fail;
256
257 if (pcmcia_io_map(psc->sc_pf, PCMCIA_WIDTH_AUTO, 0, psc->sc_pcioh.size,
258 &psc->sc_pcioh, &psc->sc_io_window))
259 goto fail_io_free;
260
261 /* Enable the card. */
262 pcmcia_function_init(psc->sc_pf, cfe);
263 if (pcmcia_function_enable(psc->sc_pf))
264 goto fail_io_unmap;
265
266 sc->sc_chip.sc_bustype = AM79C930_BUS_PCMCIA;
267 sc->sc_chip.sc_iot = psc->sc_pcioh.iot;
268 sc->sc_chip.sc_ioh = psc->sc_pcioh.ioh;
269 am79c930_chip_init(&sc->sc_chip, 0);
270
271 DELAY(1000);
272
273 awi_read_bytes(sc, AWI_BANNER, version, AWI_BANNER_LEN);
274
275 if (memcmp(version, "PCnetMobile:", 12) == 0)
276 return (0);
277
278 fail++;
279 pcmcia_function_disable(psc->sc_pf);
280
281 fail_io_unmap:
282 fail++;
283 pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
284
285 fail_io_free:
286 fail++;
287 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
288 fail:
289 fail++;
290 psc->sc_io_window = -1;
291 return (fail);
292 }
293
294 static void
295 awi_pcmcia_attach(parent, self, aux)
296 struct device *parent, *self;
297 void *aux;
298 {
299 struct awi_pcmcia_softc *psc = (void *)self;
300 struct awi_softc *sc = &psc->sc_awi;
301 struct awi_pcmcia_product *app;
302 struct pcmcia_attach_args *pa = aux;
303 struct pcmcia_config_entry *cfe;
304 bus_addr_t memoff;
305
306 app = awi_pcmcia_lookup(pa);
307 if (app == NULL)
308 panic("awi_pcmcia_attach: impossible");
309
310 psc->sc_pf = pa->pf;
311
312 for (cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head); cfe != NULL;
313 cfe = SIMPLEQ_NEXT(cfe, cfe_list)) {
314 if (cfe->iftype != PCMCIA_IFTYPE_IO)
315 continue;
316 if (cfe->num_iospace < 1)
317 continue;
318 if (cfe->iospace[0].length < AM79C930_IO_SIZE)
319 continue;
320
321 if (awi_pcmcia_find(psc, pa, cfe) == 0)
322 break;
323 }
324 if (cfe == NULL) {
325 printf(": no suitable CIS info found\n");
326 goto no_config_entry;
327 }
328
329 sc->sc_enabled = 1;
330 printf(": %s\n", app->app_name);
331
332 psc->sc_mem_window = -1;
333 if (pcmcia_mem_alloc(psc->sc_pf, AM79C930_MEM_SIZE,
334 &psc->sc_memh) != 0) {
335 printf("%s: unable to allocate memory space; using i/o only\n",
336 sc->sc_dev.dv_xname);
337 } else if (pcmcia_mem_map(psc->sc_pf,
338 PCMCIA_WIDTH_MEM8|PCMCIA_MEM_COMMON, AM79C930_MEM_BASE,
339 AM79C930_MEM_SIZE, &psc->sc_memh, &memoff, &psc->sc_mem_window)) {
340 printf("%s: unable to map memory space; using i/o only\n",
341 sc->sc_dev.dv_xname);
342 pcmcia_mem_free(psc->sc_pf, &psc->sc_memh);
343 } else {
344 sc->sc_chip.sc_memt = psc->sc_memh.memt;
345 sc->sc_chip.sc_memh = psc->sc_memh.memh;
346 am79c930_chip_init(&sc->sc_chip, 1);
347 }
348
349 sc->sc_enable = awi_pcmcia_enable;
350 sc->sc_disable = awi_pcmcia_disable;
351
352 /* establish the interrupt. */
353 sc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, awi_intr, sc);
354 if (sc->sc_ih == NULL) {
355 printf("%s: couldn't establish interrupt\n",
356 sc->sc_dev.dv_xname);
357 goto no_interrupt;
358 }
359 sc->sc_ifp = &sc->sc_ec.ec_if;
360 sc->sc_cansleep = 1;
361
362 if (awi_attach(sc) != 0) {
363 printf("%s: failed to attach controller\n",
364 sc->sc_dev.dv_xname);
365 goto attach_failed;
366 }
367 psc->sc_powerhook = powerhook_establish(awi_pcmcia_powerhook, psc);
368
369 sc->sc_enabled = 0;
370 /* disable device and disestablish the interrupt */
371 awi_pcmcia_disable(sc);
372 return;
373
374 attach_failed:
375 pcmcia_intr_disestablish(psc->sc_pf, sc->sc_ih);
376
377 no_interrupt:
378 /* Unmap our memory window and space */
379 if (psc->sc_mem_window != -1) {
380 pcmcia_mem_unmap(psc->sc_pf, psc->sc_mem_window);
381 pcmcia_mem_free(psc->sc_pf, &psc->sc_memh);
382 }
383
384 /* Unmap our i/o window and space */
385 pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
386 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
387
388 /* Disable the function */
389 pcmcia_function_disable(psc->sc_pf);
390
391 no_config_entry:
392 psc->sc_io_window = -1;
393 }
394
395
396 static int
397 awi_pcmcia_detach(self, flags)
398 struct device *self;
399 int flags;
400 {
401 struct awi_pcmcia_softc *psc = (struct awi_pcmcia_softc *)self;
402 int error;
403
404 if (psc->sc_io_window == -1)
405 /* Nothing to detach. */
406 return (0);
407
408 if (psc->sc_powerhook != NULL)
409 powerhook_disestablish(psc->sc_powerhook);
410
411 error = awi_detach(&psc->sc_awi);
412 if (error != 0)
413 return (error);
414
415 /* Unmap our memory window and free memory space */
416 if (psc->sc_mem_window != -1) {
417 pcmcia_mem_unmap(psc->sc_pf, psc->sc_mem_window);
418 pcmcia_mem_free(psc->sc_pf, &psc->sc_memh);
419 }
420
421 /* Unmap our i/o window. */
422 pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
423
424 /* Free our i/o space. */
425 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
426 return (0);
427 }
428
429 static void
430 awi_pcmcia_powerhook(why, arg)
431 int why;
432 void *arg;
433 {
434 struct awi_pcmcia_softc *psc = arg;
435 struct awi_softc *sc = &psc->sc_awi;
436 int ocansleep;
437
438 if (why == PWR_RESUME) {
439 ocansleep = sc->sc_cansleep;
440 sc->sc_cansleep = 0;
441 awi_reset(sc);
442 sc->sc_cansleep = ocansleep;
443 }
444 }
445