if_awi_pcmcia.c revision 1.31 1 /* $NetBSD: if_awi_pcmcia.c,v 1.31 2004/08/10 18:39:08 mycroft Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2004 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 trivially adapted for other FH and
44 * DS cards based on the same chipset.
45 */
46
47 #include <sys/cdefs.h>
48 __KERNEL_RCSID(0, "$NetBSD: if_awi_pcmcia.c,v 1.31 2004/08/10 18:39:08 mycroft Exp $");
49
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/mbuf.h>
53 #include <sys/socket.h>
54 #include <sys/ioctl.h>
55 #include <sys/errno.h>
56 #include <sys/syslog.h>
57 #include <sys/select.h>
58 #include <sys/device.h>
59
60 #include <net/if.h>
61 #include <net/if_dl.h>
62 #include <net/if_ether.h>
63 #include <net/if_media.h>
64
65 #include <net80211/ieee80211_var.h>
66 #include <net80211/ieee80211_compat.h>
67
68 #include <machine/cpu.h>
69 #include <machine/bus.h>
70 #include <machine/intr.h>
71
72 #include <dev/ic/am79c930reg.h>
73 #include <dev/ic/am79c930var.h>
74 #include <dev/ic/awireg.h>
75 #include <dev/ic/awivar.h>
76
77 #include <dev/pcmcia/pcmciareg.h>
78 #include <dev/pcmcia/pcmciavar.h>
79 #include <dev/pcmcia/pcmciadevs.h>
80
81 static int awi_pcmcia_match(struct device *, struct cfdata *, void *);
82 static int awi_pcmcia_validate_config(struct pcmcia_config_entry *);
83 static void awi_pcmcia_attach(struct device *, struct device *, void *);
84 static int awi_pcmcia_detach(struct device *, int);
85 static int awi_pcmcia_enable(struct awi_softc *);
86 static void awi_pcmcia_disable(struct awi_softc *);
87
88 struct awi_pcmcia_softc {
89 struct awi_softc sc_awi; /* real "awi" softc */
90
91 /* PCMCIA-specific goo */
92 struct pcmcia_function *sc_pf; /* our PCMCIA function */
93 void *sc_ih; /* interrupt handler */
94
95 int sc_state;
96 #define AWI_PCMCIA_ATTACH1 1
97 #define AWI_PCMCIA_ATTACH2 2
98 #define AWI_PCMCIA_ATTACHED 3
99 };
100
101 CFATTACH_DECL(awi_pcmcia, sizeof(struct awi_pcmcia_softc),
102 awi_pcmcia_match, awi_pcmcia_attach, awi_pcmcia_detach, awi_activate);
103
104 static const struct pcmcia_product awi_pcmcia_products[] = {
105 { PCMCIA_VENDOR_BAY, PCMCIA_PRODUCT_BAY_STACK_650,
106 PCMCIA_CIS_BAY_STACK_650 },
107
108 { PCMCIA_VENDOR_BAY, PCMCIA_PRODUCT_BAY_STACK_660,
109 PCMCIA_CIS_BAY_STACK_660 },
110
111 { PCMCIA_VENDOR_BAY, PCMCIA_PRODUCT_BAY_SURFER_PRO,
112 PCMCIA_CIS_BAY_SURFER_PRO },
113
114 { PCMCIA_VENDOR_AMD, PCMCIA_PRODUCT_AMD_AM79C930,
115 PCMCIA_CIS_AMD_AM79C930 },
116
117 { PCMCIA_VENDOR_ICOM, PCMCIA_PRODUCT_ICOM_SL200,
118 PCMCIA_CIS_ICOM_SL200 },
119
120 { PCMCIA_VENDOR_NOKIA, PCMCIA_PRODUCT_NOKIA_C020_WLAN,
121 PCMCIA_CIS_NOKIA_C020_WLAN },
122
123 { PCMCIA_VENDOR_FARALLON, PCMCIA_PRODUCT_FARALLON_SKYLINE,
124 PCMCIA_CIS_FARALLON_SKYLINE },
125 };
126 static const size_t awi_pcmcia_nproducts =
127 sizeof(awi_pcmcia_products) / sizeof(awi_pcmcia_products[0]);
128
129 static int
130 awi_pcmcia_enable(sc)
131 struct awi_softc *sc;
132 {
133 struct awi_pcmcia_softc *psc = (struct awi_pcmcia_softc *)sc;
134 struct pcmcia_function *pf = psc->sc_pf;
135 int error;
136
137 if (psc->sc_state == AWI_PCMCIA_ATTACH1) {
138 psc->sc_state = AWI_PCMCIA_ATTACH2;
139 return (0);
140 }
141
142 /* establish the interrupt. */
143 psc->sc_ih = pcmcia_intr_establish(pf, IPL_NET, awi_intr, sc);
144 if (!psc->sc_ih)
145 return (EIO);
146
147 error = pcmcia_function_enable(pf);
148 if (error) {
149 pcmcia_intr_disestablish(pf, psc->sc_ih);
150 psc->sc_ih = 0;
151 }
152
153 return (error);
154 }
155
156 static void
157 awi_pcmcia_disable(sc)
158 struct awi_softc *sc;
159 {
160 struct awi_pcmcia_softc *psc = (struct awi_pcmcia_softc *)sc;
161 struct pcmcia_function *pf = psc->sc_pf;
162
163 pcmcia_function_disable(pf);
164 pcmcia_intr_disestablish(pf, psc->sc_ih);
165 psc->sc_ih = 0;
166 }
167
168 static int
169 awi_pcmcia_match(parent, match, aux)
170 struct device *parent;
171 struct cfdata *match;
172 void *aux;
173 {
174 struct pcmcia_attach_args *pa = aux;
175
176 if (pcmcia_product_lookup(pa, awi_pcmcia_products, awi_pcmcia_nproducts,
177 sizeof(awi_pcmcia_products[0]), NULL))
178 return (1);
179 return (0);
180 }
181
182 static int
183 awi_pcmcia_validate_config(cfe)
184 struct pcmcia_config_entry *cfe;
185 {
186 if (cfe->iftype != PCMCIA_IFTYPE_IO ||
187 cfe->num_iospace < 1 ||
188 cfe->iospace[0].length < AM79C930_IO_SIZE)
189 return (EINVAL);
190 if (cfe->num_memspace < 1) {
191 cfe->memspace[0].length = AM79C930_MEM_SIZE;
192 cfe->memspace[0].cardaddr = 0;
193 cfe->memspace[0].hostaddr = 0;
194 } else if (cfe->memspace[0].length < AM79C930_MEM_SIZE)
195 return (EINVAL);
196 return (0);
197 }
198
199 static void
200 awi_pcmcia_attach(parent, self, aux)
201 struct device *parent, *self;
202 void *aux;
203 {
204 struct awi_pcmcia_softc *psc = (void *)self;
205 struct awi_softc *sc = &psc->sc_awi;
206 struct pcmcia_attach_args *pa = aux;
207 struct pcmcia_config_entry *cfe;
208 int error;
209
210 aprint_normal("\n");
211 psc->sc_pf = pa->pf;
212
213 error = pcmcia_function_configure(pa->pf, awi_pcmcia_validate_config);
214 if (error) {
215 aprint_error("%s: configure failed, error=%d\n", self->dv_xname,
216 error);
217 return;
218 }
219
220 cfe = pa->pf->cfe;
221 sc->sc_chip.sc_bustype = AM79C930_BUS_PCMCIA;
222 sc->sc_chip.sc_iot = cfe->iospace[0].handle.iot;
223 sc->sc_chip.sc_ioh = cfe->iospace[0].handle.ioh;
224 if (cfe->num_memspace > 0) {
225 sc->sc_chip.sc_memt = cfe->memspace[0].handle.memt;
226 sc->sc_chip.sc_memh = cfe->memspace[0].handle.memh;
227 am79c930_chip_init(&sc->sc_chip, 1);
228 } else
229 am79c930_chip_init(&sc->sc_chip, 0);
230
231 error = awi_pcmcia_enable(sc);
232 if (error)
233 goto fail;
234
235 awi_read_bytes(sc, AWI_BANNER, sc->sc_banner, AWI_BANNER_LEN);
236 if (memcmp(sc->sc_banner, "PCnetMobile:", 12))
237 goto fail2;
238
239 sc->sc_enable = awi_pcmcia_enable;
240 sc->sc_disable = awi_pcmcia_disable;
241
242 sc->sc_cansleep = 1;
243
244 psc->sc_state = AWI_PCMCIA_ATTACH1;
245 sc->sc_enabled = 1;
246 if (awi_attach(sc) != 0) {
247 printf("%s: failed to attach controller\n", self->dv_xname);
248 goto fail2;
249 }
250
251 if (psc->sc_state == AWI_PCMCIA_ATTACH1)
252 awi_pcmcia_disable(sc);
253 sc->sc_enabled = 0;
254 psc->sc_state = AWI_PCMCIA_ATTACHED;
255 return;
256
257 fail2:
258 awi_pcmcia_disable(sc);
259 sc->sc_enabled = 0;
260 fail:
261 pcmcia_function_unconfigure(pa->pf);
262 }
263
264 static int
265 awi_pcmcia_detach(self, flags)
266 struct device *self;
267 int flags;
268 {
269 struct awi_pcmcia_softc *psc = (struct awi_pcmcia_softc *)self;
270 int error;
271
272 if (psc->sc_state != AWI_PCMCIA_ATTACHED)
273 return (0);
274
275 error = awi_detach(&psc->sc_awi);
276 if (error)
277 return (error);
278
279 pcmcia_function_unconfigure(psc->sc_pf);
280
281 return (0);
282 }
283