if_an_pcmcia.c revision 1.13 1 /* $NetBSD: if_an_pcmcia.c,v 1.13 2002/09/30 22:26:59 thorpej 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 <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: if_an_pcmcia.c,v 1.13 2002/09/30 22:26:59 thorpej Exp $");
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/callout.h>
45 #include <sys/mbuf.h>
46 #include <sys/socket.h>
47 #include <sys/ioctl.h>
48 #include <sys/errno.h>
49 #include <sys/syslog.h>
50 #include <sys/select.h>
51 #include <sys/device.h>
52
53 #include <net/if.h>
54 #include <net/if_dl.h>
55 #include <net/if_ether.h>
56 #include <net/if_media.h>
57 #include <net/if_ieee80211.h>
58
59 #include <machine/cpu.h>
60 #include <machine/bus.h>
61 #include <machine/intr.h>
62
63 #include <dev/ic/anreg.h>
64 #include <dev/ic/anvar.h>
65
66 #include <dev/pcmcia/pcmciareg.h>
67 #include <dev/pcmcia/pcmciavar.h>
68 #include <dev/pcmcia/pcmciadevs.h>
69
70 static int an_pcmcia_match __P((struct device *, struct cfdata *, void *));
71 static void an_pcmcia_attach __P((struct device *, struct device *, void *));
72 static int an_pcmcia_detach __P((struct device *, int));
73 static int an_pcmcia_enable __P((struct an_softc *));
74 static void an_pcmcia_disable __P((struct an_softc *));
75
76 struct an_pcmcia_softc {
77 struct an_softc sc_an; /* real "an" softc */
78
79 /* PCMCIA-specific goo */
80 struct pcmcia_io_handle sc_pcioh; /* PCMCIA i/o space info */
81 int sc_io_window; /* our i/o window */
82 struct pcmcia_function *sc_pf; /* our PCMCIA function */
83 void *sc_ih; /* interrupt handle */
84 void *sc_powerhook; /* power hook descriptor */
85 };
86
87 CFATTACH_DECL(an_pcmcia, sizeof(struct an_pcmcia_softc),
88 an_pcmcia_match, an_pcmcia_attach, an_pcmcia_detach, an_activate)
89
90 static struct an_pcmcia_product {
91 u_int32_t app_vendor; /* vendor ID */
92 u_int32_t app_product; /* product ID */
93 const char *app_cisinfo[4]; /* CIS information */
94 const char *app_name; /* product name */
95 } an_pcmcia_products[] = {
96 { PCMCIA_VENDOR_AIRONET, PCMCIA_PRODUCT_AIRONET_PC4800,
97 PCMCIA_CIS_AIRONET_PC4800, PCMCIA_STR_AIRONET_PC4800 },
98 { PCMCIA_VENDOR_AIRONET, PCMCIA_PRODUCT_AIRONET_PC4500,
99 PCMCIA_CIS_AIRONET_PC4500, PCMCIA_STR_AIRONET_PC4500 },
100 { PCMCIA_VENDOR_AIRONET, PCMCIA_PRODUCT_AIRONET_350,
101 PCMCIA_CIS_AIRONET_350, PCMCIA_STR_AIRONET_350 },
102 { 0, 0,
103 { NULL, NULL, NULL, NULL }, NULL }
104 };
105
106 static int
107 an_pcmcia_enable(sc)
108 struct an_softc *sc;
109 {
110 struct an_pcmcia_softc *psc = (struct an_pcmcia_softc *)sc;
111 struct pcmcia_function *pf = psc->sc_pf;
112
113 /* establish the interrupt. */
114 psc->sc_ih = pcmcia_intr_establish(pf, IPL_NET, an_intr, sc);
115 if (psc->sc_ih == NULL) {
116 printf("%s: couldn't establish interrupt\n",
117 sc->an_dev.dv_xname);
118 return (1);
119 }
120
121 if (pcmcia_function_enable(pf)) {
122 pcmcia_intr_disestablish(pf, psc->sc_ih);
123 return (1);
124 }
125 DELAY(1000);
126
127 return (0);
128 }
129
130 static void
131 an_pcmcia_disable(sc)
132 struct an_softc *sc;
133 {
134 struct an_pcmcia_softc *psc = (struct an_pcmcia_softc *)sc;
135 struct pcmcia_function *pf = psc->sc_pf;
136
137 pcmcia_function_disable(pf);
138 pcmcia_intr_disestablish(pf, psc->sc_ih);
139 }
140
141 static int
142 an_pcmcia_match(parent, match, aux)
143 struct device *parent;
144 struct cfdata *match;
145 void *aux;
146 {
147 struct pcmcia_attach_args *pa = aux;
148 struct an_pcmcia_product *app;
149
150 for (app = an_pcmcia_products; app->app_name != NULL; app++) {
151 /* match by vendor/product id */
152 if (pa->manufacturer != PCMCIA_VENDOR_INVALID &&
153 pa->manufacturer == app->app_vendor &&
154 pa->product != PCMCIA_PRODUCT_INVALID &&
155 pa->product == app->app_product)
156 return 1;
157
158 /* match by CIS information */
159 if (pa->card->cis1_info[0] != NULL &&
160 app->app_cisinfo[0] != NULL &&
161 strcmp(pa->card->cis1_info[0], app->app_cisinfo[0]) == 0 &&
162 pa->card->cis1_info[1] != NULL &&
163 app->app_cisinfo[1] != NULL &&
164 strcmp(pa->card->cis1_info[1], app->app_cisinfo[1]) == 0)
165 return 1;
166 }
167 return 0;
168 }
169
170 static void
171 an_pcmcia_attach(parent, self, aux)
172 struct device *parent, *self;
173 void *aux;
174 {
175 struct an_pcmcia_softc *psc = (void *)self;
176 struct an_softc *sc = &psc->sc_an;
177 struct pcmcia_attach_args *pa = aux;
178 struct pcmcia_config_entry *cfe;
179 char devinfo[256];
180
181 /* Print out what we are. */
182 pcmcia_devinfo(&pa->pf->sc->card, 0, devinfo, sizeof(devinfo));
183 printf(": %s\n", devinfo);
184
185 psc->sc_pf = pa->pf;
186 if ((cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head)) == NULL) {
187 printf("%s: no suitable CIS info found\n", sc->an_dev.dv_xname);
188 goto fail1;
189 }
190
191 if (pcmcia_io_alloc(psc->sc_pf, cfe->iospace[0].start,
192 cfe->iospace[0].length, AN_IOSIZ, &psc->sc_pcioh) != 0) {
193 printf("%s: failed to allocate io space\n",
194 sc->an_dev.dv_xname);
195 goto fail1;
196 }
197
198 if (pcmcia_io_map(psc->sc_pf, PCMCIA_WIDTH_AUTO, 0, psc->sc_pcioh.size,
199 &psc->sc_pcioh, &psc->sc_io_window) != 0) {
200 printf("%s: failed to map io space\n", sc->an_dev.dv_xname);
201 goto fail2;
202 }
203
204 pcmcia_function_init(psc->sc_pf, cfe);
205
206 if (pcmcia_function_enable(psc->sc_pf)) {
207 printf("%s: failed to enable pcmcia\n", sc->an_dev.dv_xname);
208 goto fail3;
209 }
210
211 if ((psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, an_intr,
212 sc)) == NULL) {
213 printf("%s: unable to establish interrupt\n",
214 sc->an_dev.dv_xname);
215 goto fail4;
216 }
217
218 sc->an_btag = psc->sc_pcioh.iot;
219 sc->an_bhandle = psc->sc_pcioh.ioh;
220 sc->sc_enabled = 1;
221 sc->sc_enable = an_pcmcia_enable;
222 sc->sc_disable = an_pcmcia_disable;
223
224 if (an_attach(sc) != 0) {
225 printf("%s: failed to attach controller\n",
226 sc->an_dev.dv_xname);
227 goto fail5;;
228 }
229 psc->sc_powerhook = powerhook_establish(an_power, sc);
230 sc->sc_enabled = 0;
231
232 /* disable device and disestablish the interrupt */
233 an_pcmcia_disable(sc);
234 return;
235
236 fail5:
237 pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
238 fail4:
239 pcmcia_function_disable(psc->sc_pf);
240 fail3:
241 pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
242 fail2:
243 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
244 fail1:
245 psc->sc_io_window = -1;
246 }
247
248
249 static int
250 an_pcmcia_detach(self, flags)
251 struct device *self;
252 int flags;
253 {
254 struct an_pcmcia_softc *psc = (struct an_pcmcia_softc *)self;
255 int error;
256
257 if (psc->sc_io_window == -1)
258 /* Nothing to detach. */
259 return (0);
260
261 if (psc->sc_powerhook != NULL)
262 powerhook_disestablish(psc->sc_powerhook);
263
264 error = an_detach(&psc->sc_an);
265 if (error != 0)
266 return (error);
267
268 /* Unmap our i/o window. */
269 pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
270
271 /* Free our i/o space. */
272 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
273 return (0);
274 }
275