if_an_pcmcia.c revision 1.5 1 1.5 onoe /* $NetBSD: if_an_pcmcia.c,v 1.5 2000/12/14 04:11:26 onoe Exp $ */
2 1.1 onoe
3 1.1 onoe /*-
4 1.1 onoe * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 1.1 onoe * All rights reserved.
6 1.1 onoe *
7 1.1 onoe * This code is derived from software contributed to The NetBSD Foundation
8 1.1 onoe * by Atsushi Onoe
9 1.1 onoe *
10 1.1 onoe * Redistribution and use in source and binary forms, with or without
11 1.1 onoe * modification, are permitted provided that the following conditions
12 1.1 onoe * are met:
13 1.1 onoe * 1. Redistributions of source code must retain the above copyright
14 1.1 onoe * notice, this list of conditions and the following disclaimer.
15 1.1 onoe * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 onoe * notice, this list of conditions and the following disclaimer in the
17 1.1 onoe * documentation and/or other materials provided with the distribution.
18 1.1 onoe * 3. All advertising materials mentioning features or use of this software
19 1.1 onoe * must display the following acknowledgement:
20 1.1 onoe * This product includes software developed by the NetBSD
21 1.1 onoe * Foundation, Inc. and its contributors.
22 1.1 onoe * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 onoe * contributors may be used to endorse or promote products derived
24 1.1 onoe * from this software without specific prior written permission.
25 1.1 onoe *
26 1.1 onoe * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 onoe * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 onoe * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 onoe * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 onoe * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 onoe * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 onoe * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 onoe * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 onoe * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 onoe * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 onoe * POSSIBILITY OF SUCH DAMAGE.
37 1.1 onoe */
38 1.1 onoe
39 1.1 onoe #include "opt_inet.h"
40 1.1 onoe #include "opt_ns.h"
41 1.1 onoe #include "bpfilter.h"
42 1.2 onoe
43 1.2 onoe #ifdef INET
44 1.2 onoe #define ANCACHE /* XXX: should be defined elsewhere */
45 1.2 onoe #endif
46 1.1 onoe
47 1.1 onoe #include <sys/param.h>
48 1.1 onoe #include <sys/systm.h>
49 1.1 onoe #include <sys/callout.h>
50 1.1 onoe #include <sys/mbuf.h>
51 1.1 onoe #include <sys/socket.h>
52 1.1 onoe #include <sys/ioctl.h>
53 1.1 onoe #include <sys/errno.h>
54 1.1 onoe #include <sys/syslog.h>
55 1.1 onoe #include <sys/select.h>
56 1.1 onoe #include <sys/device.h>
57 1.1 onoe
58 1.1 onoe #include <net/if.h>
59 1.1 onoe #include <net/if_dl.h>
60 1.1 onoe #include <net/if_ether.h>
61 1.1 onoe #include <net/if_media.h>
62 1.1 onoe #include <net/if_ieee80211.h>
63 1.1 onoe
64 1.1 onoe #ifdef INET
65 1.1 onoe #include <netinet/in.h>
66 1.1 onoe #include <netinet/in_systm.h>
67 1.1 onoe #include <netinet/in_var.h>
68 1.1 onoe #include <netinet/ip.h>
69 1.1 onoe #include <netinet/if_inarp.h>
70 1.1 onoe #endif
71 1.1 onoe
72 1.1 onoe #ifdef NS
73 1.1 onoe #include <netns/ns.h>
74 1.1 onoe #include <netns/ns_if.h>
75 1.1 onoe #endif
76 1.1 onoe
77 1.1 onoe #if NBPFILTER > 0
78 1.1 onoe #include <net/bpf.h>
79 1.1 onoe #include <net/bpfdesc.h>
80 1.1 onoe #endif
81 1.1 onoe
82 1.1 onoe #include <machine/cpu.h>
83 1.1 onoe #include <machine/bus.h>
84 1.1 onoe #include <machine/intr.h>
85 1.1 onoe
86 1.3 onoe #include <dev/ic/anreg.h>
87 1.1 onoe #include <dev/ic/anvar.h>
88 1.1 onoe
89 1.1 onoe #include <dev/pcmcia/pcmciareg.h>
90 1.1 onoe #include <dev/pcmcia/pcmciavar.h>
91 1.1 onoe #include <dev/pcmcia/pcmciadevs.h>
92 1.1 onoe
93 1.1 onoe static int an_pcmcia_match __P((struct device *, struct cfdata *, void *));
94 1.1 onoe static void an_pcmcia_attach __P((struct device *, struct device *, void *));
95 1.1 onoe static int an_pcmcia_detach __P((struct device *, int));
96 1.1 onoe static int an_pcmcia_enable __P((struct an_softc *));
97 1.1 onoe static void an_pcmcia_disable __P((struct an_softc *));
98 1.1 onoe static void an_pcmcia_powerhook __P((int, void *));
99 1.1 onoe
100 1.1 onoe struct an_pcmcia_softc {
101 1.5 onoe struct an_softc sc_an; /* real "an" softc */
102 1.1 onoe
103 1.1 onoe /* PCMCIA-specific goo */
104 1.1 onoe struct pcmcia_io_handle sc_pcioh; /* PCMCIA i/o space info */
105 1.1 onoe int sc_io_window; /* our i/o window */
106 1.1 onoe struct pcmcia_function *sc_pf; /* our PCMCIA function */
107 1.5 onoe void *sc_ih; /* interrupt handle */
108 1.1 onoe void *sc_powerhook; /* power hook descriptor */
109 1.1 onoe };
110 1.1 onoe
111 1.1 onoe static int an_pcmcia_find __P((struct an_pcmcia_softc *,
112 1.1 onoe struct pcmcia_attach_args *, struct pcmcia_config_entry *));
113 1.1 onoe
114 1.1 onoe struct cfattach an_pcmcia_ca = {
115 1.1 onoe sizeof(struct an_pcmcia_softc), an_pcmcia_match, an_pcmcia_attach,
116 1.1 onoe an_pcmcia_detach, an_activate
117 1.1 onoe };
118 1.1 onoe
119 1.1 onoe static struct an_pcmcia_product {
120 1.1 onoe u_int32_t app_vendor; /* vendor ID */
121 1.1 onoe u_int32_t app_product; /* product ID */
122 1.1 onoe const char *app_cisinfo[4]; /* CIS information */
123 1.1 onoe const char *app_name; /* product name */
124 1.1 onoe } an_pcmcia_products[] = {
125 1.1 onoe { PCMCIA_VENDOR_AIRONET, PCMCIA_PRODUCT_AIRONET_PC4800,
126 1.1 onoe PCMCIA_CIS_AIRONET_PC4800, PCMCIA_STR_AIRONET_PC4800 },
127 1.4 onoe { PCMCIA_VENDOR_AIRONET, PCMCIA_PRODUCT_AIRONET_PC4500,
128 1.4 onoe PCMCIA_CIS_AIRONET_PC4500, PCMCIA_STR_AIRONET_PC4500 },
129 1.1 onoe { 0, 0,
130 1.1 onoe { NULL, NULL, NULL, NULL }, NULL }
131 1.1 onoe };
132 1.1 onoe
133 1.1 onoe static struct an_pcmcia_product *
134 1.1 onoe an_pcmcia_lookup __P((struct pcmcia_attach_args *));
135 1.1 onoe
136 1.1 onoe static struct an_pcmcia_product *
137 1.1 onoe an_pcmcia_lookup(pa)
138 1.1 onoe struct pcmcia_attach_args *pa;
139 1.1 onoe {
140 1.1 onoe struct an_pcmcia_product *app;
141 1.1 onoe
142 1.1 onoe for (app = an_pcmcia_products; app->app_name != NULL; app++) {
143 1.1 onoe /* match by vendor/product id */
144 1.1 onoe if (pa->manufacturer != PCMCIA_VENDOR_INVALID &&
145 1.1 onoe pa->manufacturer == app->app_vendor &&
146 1.1 onoe pa->product != PCMCIA_PRODUCT_INVALID &&
147 1.1 onoe pa->product == app->app_product)
148 1.1 onoe return (app);
149 1.1 onoe
150 1.1 onoe /* match by CIS information */
151 1.1 onoe if (pa->card->cis1_info[0] != NULL &&
152 1.1 onoe app->app_cisinfo[0] != NULL &&
153 1.1 onoe strcmp(pa->card->cis1_info[0], app->app_cisinfo[0]) == 0 &&
154 1.1 onoe pa->card->cis1_info[1] != NULL &&
155 1.1 onoe app->app_cisinfo[1] != NULL &&
156 1.1 onoe strcmp(pa->card->cis1_info[1], app->app_cisinfo[1]) == 0)
157 1.1 onoe return (app);
158 1.1 onoe }
159 1.1 onoe
160 1.1 onoe return (NULL);
161 1.1 onoe }
162 1.1 onoe
163 1.1 onoe static int
164 1.1 onoe an_pcmcia_enable(sc)
165 1.1 onoe struct an_softc *sc;
166 1.1 onoe {
167 1.1 onoe struct an_pcmcia_softc *psc = (struct an_pcmcia_softc *)sc;
168 1.1 onoe struct pcmcia_function *pf = psc->sc_pf;
169 1.1 onoe
170 1.1 onoe /* establish the interrupt. */
171 1.5 onoe psc->sc_ih = pcmcia_intr_establish(pf, IPL_NET, an_intr, sc);
172 1.5 onoe if (psc->sc_ih == NULL) {
173 1.1 onoe printf("%s: couldn't establish interrupt\n",
174 1.1 onoe sc->an_dev.dv_xname);
175 1.1 onoe return (1);
176 1.1 onoe }
177 1.1 onoe
178 1.1 onoe if (pcmcia_function_enable(pf)) {
179 1.5 onoe pcmcia_intr_disestablish(pf, psc->sc_ih);
180 1.1 onoe return (1);
181 1.1 onoe }
182 1.1 onoe DELAY(1000);
183 1.1 onoe
184 1.1 onoe return (0);
185 1.1 onoe }
186 1.1 onoe
187 1.1 onoe static void
188 1.1 onoe an_pcmcia_disable(sc)
189 1.1 onoe struct an_softc *sc;
190 1.1 onoe {
191 1.1 onoe struct an_pcmcia_softc *psc = (struct an_pcmcia_softc *)sc;
192 1.1 onoe struct pcmcia_function *pf = psc->sc_pf;
193 1.1 onoe
194 1.1 onoe pcmcia_function_disable(pf);
195 1.5 onoe pcmcia_intr_disestablish(pf, psc->sc_ih);
196 1.1 onoe }
197 1.1 onoe
198 1.1 onoe static int
199 1.1 onoe an_pcmcia_match(parent, match, aux)
200 1.1 onoe struct device *parent;
201 1.1 onoe struct cfdata *match;
202 1.1 onoe void *aux;
203 1.1 onoe {
204 1.1 onoe struct pcmcia_attach_args *pa = aux;
205 1.1 onoe
206 1.1 onoe if (an_pcmcia_lookup(pa) != NULL)
207 1.1 onoe return (1);
208 1.1 onoe
209 1.1 onoe return (0);
210 1.1 onoe }
211 1.1 onoe
212 1.1 onoe static int
213 1.1 onoe an_pcmcia_find(psc, pa, cfe)
214 1.1 onoe struct an_pcmcia_softc *psc;
215 1.1 onoe struct pcmcia_attach_args *pa;
216 1.1 onoe struct pcmcia_config_entry *cfe;
217 1.1 onoe {
218 1.1 onoe struct an_softc *sc = &psc->sc_an;
219 1.1 onoe int fail = 0;
220 1.1 onoe
221 1.1 onoe /*
222 1.1 onoe * see if we can read the firmware version sanely
223 1.1 onoe * through the i/o ports.
224 1.1 onoe * if not, try a different CIS string..
225 1.1 onoe */
226 1.1 onoe if (pcmcia_io_alloc(psc->sc_pf, cfe->iospace[0].start,
227 1.1 onoe cfe->iospace[0].length, AN_IOSIZ,
228 1.1 onoe &psc->sc_pcioh) != 0)
229 1.1 onoe goto fail;
230 1.1 onoe
231 1.1 onoe if (pcmcia_io_map(psc->sc_pf, PCMCIA_WIDTH_AUTO, 0, psc->sc_pcioh.size,
232 1.1 onoe &psc->sc_pcioh, &psc->sc_io_window))
233 1.1 onoe goto fail_io_free;
234 1.1 onoe
235 1.1 onoe /* Enable the card. */
236 1.1 onoe pcmcia_function_init(psc->sc_pf, cfe);
237 1.1 onoe if (pcmcia_function_enable(psc->sc_pf))
238 1.1 onoe goto fail_io_unmap;
239 1.1 onoe
240 1.1 onoe sc->an_btag = psc->sc_pcioh.iot;
241 1.1 onoe sc->an_bhandle = psc->sc_pcioh.ioh;
242 1.1 onoe
243 1.1 onoe if (an_probe(sc))
244 1.1 onoe return 0;
245 1.1 onoe
246 1.1 onoe fail++;
247 1.1 onoe pcmcia_function_disable(psc->sc_pf);
248 1.1 onoe
249 1.1 onoe fail_io_unmap:
250 1.1 onoe fail++;
251 1.1 onoe pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
252 1.1 onoe
253 1.1 onoe fail_io_free:
254 1.1 onoe fail++;
255 1.1 onoe pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
256 1.1 onoe fail:
257 1.1 onoe fail++;
258 1.1 onoe return (fail);
259 1.1 onoe }
260 1.1 onoe
261 1.1 onoe static void
262 1.1 onoe an_pcmcia_attach(parent, self, aux)
263 1.1 onoe struct device *parent, *self;
264 1.1 onoe void *aux;
265 1.1 onoe {
266 1.1 onoe struct an_pcmcia_softc *psc = (void *)self;
267 1.1 onoe struct an_softc *sc = &psc->sc_an;
268 1.1 onoe struct an_pcmcia_product *app;
269 1.1 onoe struct pcmcia_attach_args *pa = aux;
270 1.1 onoe struct pcmcia_config_entry *cfe;
271 1.1 onoe
272 1.1 onoe app = an_pcmcia_lookup(pa);
273 1.1 onoe if (app == NULL)
274 1.1 onoe panic("an_pcmcia_attach: impossible");
275 1.1 onoe
276 1.1 onoe psc->sc_pf = pa->pf;
277 1.1 onoe
278 1.1 onoe for (cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head); cfe != NULL;
279 1.1 onoe cfe = SIMPLEQ_NEXT(cfe, cfe_list)) {
280 1.1 onoe if (cfe->iftype != PCMCIA_IFTYPE_IO)
281 1.1 onoe continue;
282 1.1 onoe if (cfe->num_iospace < 1)
283 1.1 onoe continue;
284 1.1 onoe if (cfe->iospace[0].length < AN_IOSIZ)
285 1.1 onoe continue;
286 1.1 onoe
287 1.1 onoe if (an_pcmcia_find(psc, pa, cfe) == 0)
288 1.1 onoe break;
289 1.1 onoe }
290 1.1 onoe if (cfe == NULL) {
291 1.1 onoe printf(": no suitable CIS info found\n");
292 1.1 onoe goto no_config_entry;
293 1.1 onoe }
294 1.1 onoe
295 1.1 onoe sc->sc_enabled = 1;
296 1.1 onoe printf(": %s\n", app->app_name);
297 1.1 onoe
298 1.1 onoe sc->sc_enable = an_pcmcia_enable;
299 1.1 onoe sc->sc_disable = an_pcmcia_disable;
300 1.1 onoe
301 1.1 onoe /* establish the interrupt. */
302 1.5 onoe psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, an_intr, sc);
303 1.5 onoe if (psc->sc_ih == NULL) {
304 1.1 onoe printf("%s: couldn't establish interrupt\n",
305 1.1 onoe sc->an_dev.dv_xname);
306 1.1 onoe goto no_interrupt;
307 1.1 onoe }
308 1.1 onoe
309 1.1 onoe if (an_attach(sc) != 0) {
310 1.1 onoe printf("%s: failed to attach controller\n",
311 1.1 onoe sc->an_dev.dv_xname);
312 1.1 onoe goto attach_failed;
313 1.1 onoe }
314 1.1 onoe psc->sc_powerhook = powerhook_establish(an_pcmcia_powerhook, psc);
315 1.1 onoe sc->sc_enabled = 0;
316 1.1 onoe
317 1.1 onoe /* disable device and disestablish the interrupt */
318 1.1 onoe an_pcmcia_disable(sc);
319 1.1 onoe return;
320 1.1 onoe
321 1.1 onoe attach_failed:
322 1.5 onoe pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
323 1.1 onoe
324 1.1 onoe no_interrupt:
325 1.1 onoe /* Unmap our i/o window and space */
326 1.1 onoe pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
327 1.1 onoe pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
328 1.1 onoe
329 1.1 onoe /* Disable the function */
330 1.1 onoe pcmcia_function_disable(psc->sc_pf);
331 1.1 onoe
332 1.1 onoe no_config_entry:
333 1.1 onoe psc->sc_io_window = -1;
334 1.1 onoe }
335 1.1 onoe
336 1.1 onoe
337 1.1 onoe static int
338 1.1 onoe an_pcmcia_detach(self, flags)
339 1.1 onoe struct device *self;
340 1.1 onoe int flags;
341 1.1 onoe {
342 1.1 onoe struct an_pcmcia_softc *psc = (struct an_pcmcia_softc *)self;
343 1.1 onoe int error;
344 1.1 onoe
345 1.1 onoe if (psc->sc_io_window == -1)
346 1.1 onoe /* Nothing to detach. */
347 1.1 onoe return (0);
348 1.1 onoe
349 1.1 onoe if (psc->sc_powerhook != NULL)
350 1.1 onoe powerhook_disestablish(psc->sc_powerhook);
351 1.1 onoe
352 1.1 onoe error = an_detach(&psc->sc_an);
353 1.1 onoe if (error != 0)
354 1.1 onoe return (error);
355 1.1 onoe
356 1.1 onoe /* Unmap our i/o window. */
357 1.1 onoe pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
358 1.1 onoe
359 1.1 onoe /* Free our i/o space. */
360 1.1 onoe pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
361 1.1 onoe return (0);
362 1.1 onoe }
363 1.1 onoe
364 1.1 onoe static void
365 1.1 onoe an_pcmcia_powerhook(why, arg)
366 1.1 onoe int why;
367 1.1 onoe void *arg;
368 1.1 onoe {
369 1.1 onoe #if 0
370 1.1 onoe struct an_pcmcia_softc *psc = arg;
371 1.1 onoe struct an_softc *sc = &psc->sc_an;
372 1.1 onoe
373 1.1 onoe an_power(sc, why);
374 1.1 onoe #endif
375 1.1 onoe }
376