if_wi_pcmcia.c revision 1.1 1 /* $NetBSD: if_wi_pcmcia.c,v 1.1 2001/05/06 03:26:40 ichiro Exp $ */
2
3 /*-
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Ichiro FUKUHARA (ichiro (at) ichiro.org).
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 Lucent & Intersil WaveLAN PCMCIA card
41 */
42
43 #include "opt_inet.h"
44 #include "opt_ns.h"
45 #include "bpfilter.h"
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/callout.h>
50 #include <sys/device.h>
51 #include <sys/socket.h>
52
53 #include <net/if.h>
54 #include <net/if_ether.h>
55 #include <net/if_media.h>
56 #include <net/if_ieee80211.h>
57
58 #ifdef INET
59 #include <netinet/in.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/in_var.h>
62 #include <netinet/ip.h>
63 #include <netinet/if_inarp.h>
64 #endif
65
66 #ifdef NS
67 #include <netns/ns.h>
68 #include <netns/ns_if.h>
69 #endif
70
71 #if NBPFILTER > 0
72 #include <net/bpf.h>
73 #include <net/bpfdesc.h>
74 #endif
75
76 #include <machine/cpu.h>
77 #include <machine/bus.h>
78 #include <machine/intr.h>
79
80 #include <dev/ic/wi_ieee.h>
81 #include <dev/ic/wireg.h>
82 #include <dev/ic/wivar.h>
83
84 #include <dev/pcmcia/pcmciareg.h>
85 #include <dev/pcmcia/pcmciavar.h>
86 #include <dev/pcmcia/pcmciadevs.h>
87
88 static int wi_pcmcia_match __P((struct device *, struct cfdata *, void *));
89 static void wi_pcmcia_attach __P((struct device *, struct device *, void *));
90 static int wi_pcmcia_detach __P((struct device *, int));
91 static int wi_pcmcia_enable __P((struct wi_softc *));
92 static void wi_pcmcia_disable __P((struct wi_softc *));
93 static void wi_pcmcia_powerhook __P((int, void *));
94 static void wi_pcmcia_shutdown __P((void *));
95
96 static const struct wi_pcmcia_product
97 *wi_pcmcia_lookup __P((struct pcmcia_attach_args *pa));
98
99 struct wi_pcmcia_softc {
100 struct wi_softc sc_wi;
101
102 /* PCMCIA-specific */
103 struct pcmcia_io_handle sc_pcioh; /* PCMCIA i/o space info */
104 int sc_io_window; /* our i/o window */
105 struct pcmcia_function *sc_pf; /* PCMCIA function */
106 void *sc_powerhook; /* power hook descriptor */
107 void *sc_sdhook; /* shutdown hook */
108
109 };
110
111 static int wi_pcmcia_find __P((struct wi_pcmcia_softc *,
112 struct pcmcia_attach_args *, struct pcmcia_config_entry *));
113
114 struct cfattach wi_pcmcia_ca = {
115 sizeof(struct wi_pcmcia_softc), wi_pcmcia_match, wi_pcmcia_attach,
116 wi_pcmcia_detach, wi_activate,
117 };
118
119 static const struct wi_pcmcia_product {
120 u_int32_t pp_vendor; /* vendor ID */
121 u_int32_t pp_product; /* product ID */
122 const char *pp_cisinfo[4]; /* CIS information */
123 const char *pp_name; /* product name */
124 int pp_prism2; /* prism2 chipset */
125 } wi_pcmcia_products[] = {
126 { PCMCIA_VENDOR_LUCENT,
127 PCMCIA_PRODUCT_LUCENT_WAVELAN_IEEE,
128 PCMCIA_CIS_LUCENT_WAVELAN_IEEE,
129 PCMCIA_STR_LUCENT_WAVELAN_IEEE,
130 0 },
131
132 { PCMCIA_VENDOR_3COM,
133 PCMCIA_PRODUCT_3COM_3CRWE737A,
134 PCMCIA_CIS_3COM_3CRWE737A,
135 PCMCIA_STR_3COM_3CRWE737A,
136 1 },
137
138 { PCMCIA_VENDOR_COREGA,
139 PCMCIA_PRODUCT_COREGA_WIRELESS_LAN_PCC_11,
140 PCMCIA_CIS_COREGA_WIRELESS_LAN_PCC_11,
141 PCMCIA_STR_COREGA_WIRELESS_LAN_PCC_11,
142 1 },
143
144 { PCMCIA_VENDOR_COREGA,
145 PCMCIA_PRODUCT_COREGA_WIRELESS_LAN_PCCA_11,
146 PCMCIA_CIS_COREGA_WIRELESS_LAN_PCCA_11,
147 PCMCIA_STR_COREGA_WIRELESS_LAN_PCCA_11,
148 1 },
149
150 { PCMCIA_VENDOR_INTERSIL,
151 PCMCIA_PRODUCT_INTERSIL_PRISM2,
152 PCMCIA_CIS_INTERSIL_PRISM2,
153 PCMCIA_STR_INTERSIL_PRISM2,
154 1 },
155
156 { PCMCIA_VENDOR_SAMSUNG,
157 PCMCIA_PRODUCT_SAMSUNG_SWL_2000N,
158 PCMCIA_CIS_SAMSUNG_SWL_2000N,
159 PCMCIA_STR_SAMSUNG_SWL_2000N,
160 1 },
161
162 { PCMCIA_VENDOR_LUCENT,
163 PCMCIA_PRODUCT_LUCENT_WAVELAN_IEEE,
164 PCMCIA_CIS_SMC_2632W,
165 PCMCIA_STR_SMC_2632W,
166 1 },
167
168 { PCMCIA_VENDOR_LUCENT,
169 PCMCIA_PRODUCT_LUCENT_WAVELAN_IEEE,
170 PCMCIA_CIS_NANOSPEED_PRISM2,
171 PCMCIA_STR_NANOSPEED_PRISM2,
172 1 },
173
174 { PCMCIA_VENDOR_ELSA,
175 PCMCIA_PRODUCT_ELSA_XI300_IEEE,
176 PCMCIA_CIS_ELSA_XI300_IEEE,
177 PCMCIA_STR_ELSA_XI300_IEEE,
178 1 },
179
180 { PCMCIA_VENDOR_COMPAQ,
181 PCMCIA_PRODUCT_COMPAQ_NC5004,
182 PCMCIA_CIS_COMPAQ_NC5004,
183 PCMCIA_STR_COMPAQ_NC5004,
184 1 },
185
186 { PCMCIA_VENDOR_CONTEC,
187 PCMCIA_PRODUCT_CONTEC_FX_DS110_PCC,
188 PCMCIA_CIS_CONTEC_FX_DS110_PCC,
189 PCMCIA_STR_CONTEC_FX_DS110_PCC,
190 1 },
191
192 { PCMCIA_VENDOR_TDK,
193 PCMCIA_PRODUCT_TDK_LAK_CD011WL,
194 PCMCIA_CIS_TDK_LAK_CD011WL,
195 PCMCIA_STR_TDK_LAK_CD011WL,
196 1 },
197
198 { PCMCIA_VENDOR_LUCENT,
199 PCMCIA_PRODUCT_LUCENT_WAVELAN_IEEE,
200 PCMCIA_CIS_NEC_CMZ_RT_WP,
201 PCMCIA_STR_NEC_CMZ_RT_WP,
202 1 },
203
204 { PCMCIA_VENDOR_LUCENT,
205 PCMCIA_PRODUCT_LUCENT_WAVELAN_IEEE,
206 PCMCIA_CIS_NTT_ME_WLAN,
207 PCMCIA_STR_NTT_ME_WLAN,
208 1 },
209
210 { PCMCIA_VENDOR_IODATA2,
211 PCMCIA_PRODUCT_IODATA2_WNB11PCM,
212 PCMCIA_CIS_IODATA2_WNB11PCM,
213 PCMCIA_STR_IODATA2_WNB11PCM,
214 1 },
215 #if 0
216 { PCMCIA_VENDOR_EMTAC,
217 PCMCIA_PRODUCT_EMTAC_WLAN,
218 PCMCIA_CIS_EMTAC_WLAN,
219 PCMCIA_STR_EMTAC_WLAN,
220 1 },
221 #endif
222 { 0,
223 0,
224 { NULL, NULL, NULL, NULL },
225 NULL,
226 0 }
227 };
228
229 static const struct wi_pcmcia_product *
230 wi_pcmcia_lookup(pa)
231 struct pcmcia_attach_args *pa;
232 {
233 const struct wi_pcmcia_product *pp;
234
235 /* match by CIS information */
236 for (pp = wi_pcmcia_products; pp->pp_name != NULL; pp++) {
237 if (pa->card->cis1_info[0] != NULL &&
238 pp->pp_cisinfo[0] != NULL &&
239 strcmp(pa->card->cis1_info[0], pp->pp_cisinfo[0]) == 0 &&
240 pa->card->cis1_info[1] != NULL &&
241 pp->pp_cisinfo[1] != NULL &&
242 strcmp(pa->card->cis1_info[1], pp->pp_cisinfo[1]) == 0)
243 return pp;
244 }
245
246 /* match by vendor/product id */
247 for (pp = wi_pcmcia_products; pp->pp_name != NULL; pp++) {
248 if (pa->manufacturer != PCMCIA_VENDOR_INVALID &&
249 pa->manufacturer == pp->pp_vendor &&
250 pa->product != PCMCIA_PRODUCT_INVALID &&
251 pa->product == pp->pp_product)
252 return pp;
253 }
254
255 return (NULL);
256 }
257
258 static int
259 wi_pcmcia_match(parent, match, aux)
260 struct device *parent;
261 struct cfdata *match;
262 void *aux;
263 {
264 struct pcmcia_attach_args *pa = aux;
265
266 if (wi_pcmcia_lookup(pa) != NULL)
267 return (1);
268 return (0);
269 }
270
271 static int
272 wi_pcmcia_enable(sc)
273 struct wi_softc *sc;
274 {
275 struct wi_pcmcia_softc *psc = (struct wi_pcmcia_softc *)sc;
276 struct pcmcia_function *pf = psc->sc_pf;
277
278 /* establish the interrupt. */
279 sc->sc_ih = pcmcia_intr_establish(pf, IPL_NET, wi_intr, sc);
280 if (sc->sc_ih == NULL) {
281 printf("%s: couldn't establish interrupt\n",
282 sc->sc_dev.dv_xname);
283 return (EIO);
284 }
285 if (pcmcia_function_enable(pf) != 0) {
286 printf("%s: couldn't enable card\n", sc->sc_dev.dv_xname);
287 pcmcia_intr_disestablish(pf, sc->sc_ih);
288 return (EIO);
289 }
290 DELAY(1000);
291 return (0);
292 }
293
294 static void
295 wi_pcmcia_disable(sc)
296 struct wi_softc *sc;
297 {
298 struct wi_pcmcia_softc *psc = (struct wi_pcmcia_softc *)sc;
299
300 pcmcia_intr_disestablish(psc->sc_pf, sc->sc_ih);
301 pcmcia_function_disable(psc->sc_pf);
302 }
303
304 static int
305 wi_pcmcia_find(psc, pa, cfe)
306 struct wi_pcmcia_softc *psc;
307 struct pcmcia_attach_args *pa;
308 struct pcmcia_config_entry *cfe;
309 {
310 struct wi_softc *sc = &psc->sc_wi;
311
312 /* Allocate/map I/O space. */
313 if (pcmcia_io_alloc(psc->sc_pf, cfe->iospace[0].start,
314 cfe->iospace[0].length, WI_IOSIZE,
315 &psc->sc_pcioh) != 0) {
316 printf("%s: can't allocate i/o space\n",
317 sc->sc_dev.dv_xname);
318 goto fail1;
319 }
320 printf("%s:", sc->sc_dev.dv_xname);
321 if (pcmcia_io_map(psc->sc_pf, PCMCIA_WIDTH_AUTO, 0,
322 psc->sc_pcioh.size, &psc->sc_pcioh,
323 &psc->sc_io_window) != 0) {
324 printf(" can't map i/o space\n");
325 goto fail2;
326 }
327 /* Enable the card */
328 pcmcia_function_init(psc->sc_pf, cfe);
329 if (pcmcia_function_enable(psc->sc_pf)) {
330 printf("%s: function enable failed\n", sc->sc_dev.dv_xname);
331 goto fail3;
332 }
333
334 sc->sc_iot = psc->sc_pcioh.iot;
335 sc->sc_ioh = psc->sc_pcioh.ioh;
336
337 DELAY(1000);
338 return(0);
339
340 fail3:
341 pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
342 fail2:
343 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
344 fail1:
345 psc->sc_io_window = -1;
346 return(1);
347 }
348
349 static void
350 wi_pcmcia_attach(parent, self, aux)
351 struct device *parent, *self;
352 void *aux;
353 {
354 struct wi_pcmcia_softc *psc = (void *)self;
355 struct wi_softc *sc = &psc->sc_wi;
356 const struct wi_pcmcia_product *pp;
357 struct pcmcia_attach_args *pa = aux;
358 struct pcmcia_config_entry *cfe;
359 char devinfo[256];
360
361 /* Print out what we are. */
362 pcmcia_devinfo(&pa->pf->sc->card, 0, devinfo, sizeof(devinfo));
363 printf(": %s\n", devinfo);
364
365 psc->sc_pf = pa->pf;
366
367 for (cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head); cfe != NULL;
368 cfe = SIMPLEQ_NEXT(cfe, cfe_list)) {
369 if (cfe->iftype != PCMCIA_IFTYPE_IO)
370 continue;
371 if (cfe->iospace[0].length < WI_IOSIZE)
372 continue;
373 if (wi_pcmcia_find(psc, pa, cfe) == 0)
374 break;
375 }
376 if (cfe == NULL) {
377 printf(": no suitable CIS info found\n");
378 goto no_config_entry;
379 }
380
381 pp = wi_pcmcia_lookup(pa);
382 if (pp == NULL)
383 panic("wi_pcmcia_attach: impossible");
384
385 sc->sc_prism2 = pp->pp_prism2; /* XXX prism2 */
386 sc->sc_enabled = 1;
387 sc->sc_enable = wi_pcmcia_enable;
388 sc->sc_disable = wi_pcmcia_disable;
389
390 /* establish the interrupt. */
391 sc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, wi_intr, sc);
392 if (sc->sc_ih == NULL) {
393 printf("%s: couldn't establish interrupt\n",
394 sc->sc_dev.dv_xname);
395 goto no_interrupt;
396 }
397
398 sc->sc_ifp = &sc->sc_ethercom.ec_if;
399 if (wi_attach(sc) != 0) {
400 printf("%s: failed to attach controller\n",
401 sc->sc_dev.dv_xname);
402 goto attach_failed;
403 }
404
405 psc->sc_sdhook = shutdownhook_establish(wi_pcmcia_shutdown, psc);
406 psc->sc_powerhook = powerhook_establish(wi_pcmcia_powerhook, psc);
407
408 /* disable the card */
409 sc->sc_enabled = 0;
410 wi_pcmcia_disable(sc);
411 return;
412
413 attach_failed:
414 pcmcia_intr_disestablish(psc->sc_pf, sc->sc_ih);
415 no_interrupt:
416 pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
417 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
418 no_config_entry:
419 psc->sc_io_window = -1;
420 }
421
422 static int
423 wi_pcmcia_detach(self, flags)
424 struct device *self;
425 int flags;
426 {
427 struct wi_pcmcia_softc *psc = (struct wi_pcmcia_softc *)self;
428 int error;
429
430 if (psc->sc_io_window == -1)
431 /* Nothing to detach. */
432 return (0);
433
434 if (psc->sc_powerhook != NULL)
435 powerhook_disestablish(psc->sc_powerhook);
436 if (psc->sc_sdhook != NULL)
437 shutdownhook_disestablish(psc->sc_sdhook);
438
439 error = wi_detach(&psc->sc_wi);
440 if (error != 0)
441 return (error);
442
443 /* Unmap our i/o window. */
444 pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
445
446 /* Free our i/o space. */
447 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
448 return (0);
449 }
450
451 static void
452 wi_pcmcia_powerhook(why, arg)
453 int why;
454 void *arg;
455 {
456 struct wi_pcmcia_softc *psc = arg;
457 struct wi_softc *sc = &psc->sc_wi;
458
459 wi_power(sc, why);
460 }
461
462 static void
463 wi_pcmcia_shutdown(arg)
464 void *arg;
465 {
466 struct wi_pcmcia_softc *psc = arg;
467 struct wi_softc *sc = &psc->sc_wi;
468
469 wi_shutdown(sc);
470 }
471