if_an_pcmcia.c revision 1.9 1 /* $NetBSD: if_an_pcmcia.c,v 1.9 2001/04/06 09:28:39 onoe Exp $ */
2
3 /*-
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Atsushi Onoe
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 #include "opt_inet.h"
40 #include "opt_ns.h"
41 #include "bpfilter.h"
42
43 #ifdef INET
44 #define ANCACHE /* XXX: should be defined elsewhere */
45 #endif
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/callout.h>
50 #include <sys/mbuf.h>
51 #include <sys/socket.h>
52 #include <sys/ioctl.h>
53 #include <sys/errno.h>
54 #include <sys/syslog.h>
55 #include <sys/select.h>
56 #include <sys/device.h>
57
58 #include <net/if.h>
59 #include <net/if_dl.h>
60 #include <net/if_ether.h>
61 #include <net/if_media.h>
62 #include <net/if_ieee80211.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/anreg.h>
87 #include <dev/ic/anvar.h>
88
89 #include <dev/pcmcia/pcmciareg.h>
90 #include <dev/pcmcia/pcmciavar.h>
91 #include <dev/pcmcia/pcmciadevs.h>
92
93 static int an_pcmcia_match __P((struct device *, struct cfdata *, void *));
94 static void an_pcmcia_attach __P((struct device *, struct device *, void *));
95 static int an_pcmcia_detach __P((struct device *, int));
96 static int an_pcmcia_enable __P((struct an_softc *));
97 static void an_pcmcia_disable __P((struct an_softc *));
98
99 struct an_pcmcia_softc {
100 struct an_softc sc_an; /* real "an" softc */
101
102 /* PCMCIA-specific goo */
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; /* our PCMCIA function */
106 void *sc_ih; /* interrupt handle */
107 void *sc_powerhook; /* power hook descriptor */
108 };
109
110 struct cfattach an_pcmcia_ca = {
111 sizeof(struct an_pcmcia_softc), an_pcmcia_match, an_pcmcia_attach,
112 an_pcmcia_detach, an_activate
113 };
114
115 static struct an_pcmcia_product {
116 u_int32_t app_vendor; /* vendor ID */
117 u_int32_t app_product; /* product ID */
118 const char *app_cisinfo[4]; /* CIS information */
119 const char *app_name; /* product name */
120 } an_pcmcia_products[] = {
121 { PCMCIA_VENDOR_AIRONET, PCMCIA_PRODUCT_AIRONET_PC4800,
122 PCMCIA_CIS_AIRONET_PC4800, PCMCIA_STR_AIRONET_PC4800 },
123 { PCMCIA_VENDOR_AIRONET, PCMCIA_PRODUCT_AIRONET_PC4500,
124 PCMCIA_CIS_AIRONET_PC4500, PCMCIA_STR_AIRONET_PC4500 },
125 { PCMCIA_VENDOR_AIRONET, PCMCIA_PRODUCT_AIRONET_350,
126 PCMCIA_CIS_AIRONET_350, PCMCIA_STR_AIRONET_350 },
127 { 0, 0,
128 { NULL, NULL, NULL, NULL }, NULL }
129 };
130
131 static int
132 an_pcmcia_enable(sc)
133 struct an_softc *sc;
134 {
135 struct an_pcmcia_softc *psc = (struct an_pcmcia_softc *)sc;
136 struct pcmcia_function *pf = psc->sc_pf;
137
138 /* establish the interrupt. */
139 psc->sc_ih = pcmcia_intr_establish(pf, IPL_NET, an_intr, sc);
140 if (psc->sc_ih == NULL) {
141 printf("%s: couldn't establish interrupt\n",
142 sc->an_dev.dv_xname);
143 return (1);
144 }
145
146 if (pcmcia_function_enable(pf)) {
147 pcmcia_intr_disestablish(pf, psc->sc_ih);
148 return (1);
149 }
150 DELAY(1000);
151
152 return (0);
153 }
154
155 static void
156 an_pcmcia_disable(sc)
157 struct an_softc *sc;
158 {
159 struct an_pcmcia_softc *psc = (struct an_pcmcia_softc *)sc;
160 struct pcmcia_function *pf = psc->sc_pf;
161
162 pcmcia_function_disable(pf);
163 pcmcia_intr_disestablish(pf, psc->sc_ih);
164 }
165
166 static int
167 an_pcmcia_match(parent, match, aux)
168 struct device *parent;
169 struct cfdata *match;
170 void *aux;
171 {
172 struct pcmcia_attach_args *pa = aux;
173 struct an_pcmcia_product *app;
174
175 for (app = an_pcmcia_products; app->app_name != NULL; app++) {
176 /* match by vendor/product id */
177 if (pa->manufacturer != PCMCIA_VENDOR_INVALID &&
178 pa->manufacturer == app->app_vendor &&
179 pa->product != PCMCIA_PRODUCT_INVALID &&
180 pa->product == app->app_product)
181 return 1;
182
183 /* match by CIS information */
184 if (pa->card->cis1_info[0] != NULL &&
185 app->app_cisinfo[0] != NULL &&
186 strcmp(pa->card->cis1_info[0], app->app_cisinfo[0]) == 0 &&
187 pa->card->cis1_info[1] != NULL &&
188 app->app_cisinfo[1] != NULL &&
189 strcmp(pa->card->cis1_info[1], app->app_cisinfo[1]) == 0)
190 return 1;
191 }
192 return 0;
193 }
194
195 static void
196 an_pcmcia_attach(parent, self, aux)
197 struct device *parent, *self;
198 void *aux;
199 {
200 struct an_pcmcia_softc *psc = (void *)self;
201 struct an_softc *sc = &psc->sc_an;
202 struct pcmcia_attach_args *pa = aux;
203 struct pcmcia_config_entry *cfe;
204 char devinfo[256];
205
206 /* Print out what we are. */
207 pcmcia_devinfo(&pa->pf->sc->card, 0, devinfo, sizeof(devinfo));
208 printf(": %s\n", devinfo);
209
210 psc->sc_pf = pa->pf;
211 if ((cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head)) == NULL) {
212 printf("%s: no suitable CIS info found\n", sc->an_dev.dv_xname);
213 goto fail1;
214 }
215
216 if (pcmcia_io_alloc(psc->sc_pf, cfe->iospace[0].start,
217 cfe->iospace[0].length, AN_IOSIZ, &psc->sc_pcioh) != 0) {
218 printf("%s: failed to allocate io space\n",
219 sc->an_dev.dv_xname);
220 goto fail1;
221 }
222
223 if (pcmcia_io_map(psc->sc_pf, PCMCIA_WIDTH_AUTO, 0, psc->sc_pcioh.size,
224 &psc->sc_pcioh, &psc->sc_io_window) != 0) {
225 printf("%s: failed to map io space\n", sc->an_dev.dv_xname);
226 goto fail2;
227 }
228
229 pcmcia_function_init(psc->sc_pf, cfe);
230
231 if (pcmcia_function_enable(psc->sc_pf)) {
232 printf("%s: failed to enable pcmcia\n", sc->an_dev.dv_xname);
233 goto fail3;
234 }
235
236 if ((psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, an_intr,
237 sc)) == NULL) {
238 printf("%s: unable to establish interrupt\n",
239 sc->an_dev.dv_xname);
240 goto fail4;
241 }
242
243 sc->an_btag = psc->sc_pcioh.iot;
244 sc->an_bhandle = psc->sc_pcioh.ioh;
245 sc->sc_enabled = 1;
246 sc->sc_enable = an_pcmcia_enable;
247 sc->sc_disable = an_pcmcia_disable;
248
249 if (an_attach(sc) != 0) {
250 printf("%s: failed to attach controller\n",
251 sc->an_dev.dv_xname);
252 goto fail5;;
253 }
254 psc->sc_powerhook = powerhook_establish(an_power, sc);
255 sc->sc_enabled = 0;
256
257 /* disable device and disestablish the interrupt */
258 an_pcmcia_disable(sc);
259 return;
260
261 fail5:
262 pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
263 fail4:
264 pcmcia_function_disable(psc->sc_pf);
265 fail3:
266 pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
267 fail2:
268 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
269 fail1:
270 psc->sc_io_window = -1;
271 }
272
273
274 static int
275 an_pcmcia_detach(self, flags)
276 struct device *self;
277 int flags;
278 {
279 struct an_pcmcia_softc *psc = (struct an_pcmcia_softc *)self;
280 int error;
281
282 if (psc->sc_io_window == -1)
283 /* Nothing to detach. */
284 return (0);
285
286 if (psc->sc_powerhook != NULL)
287 powerhook_disestablish(psc->sc_powerhook);
288
289 error = an_detach(&psc->sc_an);
290 if (error != 0)
291 return (error);
292
293 /* Unmap our i/o window. */
294 pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
295
296 /* Free our i/o space. */
297 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
298 return (0);
299 }
300