1 /* $NetBSD: if_awi_pcmcia.c,v 1.35 2005/12/11 12:23:23 christos 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.35 2005/12/11 12:23:23 christos 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_netbsd.h> 66 #include <net80211/ieee80211_var.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_ATTACHED 3 97 }; 98 99 CFATTACH_DECL(awi_pcmcia, sizeof(struct awi_pcmcia_softc), 100 awi_pcmcia_match, awi_pcmcia_attach, awi_pcmcia_detach, awi_activate); 101 102 static const struct pcmcia_product awi_pcmcia_products[] = { 103 { PCMCIA_VENDOR_BAY, PCMCIA_PRODUCT_BAY_STACK_650, 104 PCMCIA_CIS_BAY_STACK_650 }, 105 106 { PCMCIA_VENDOR_BAY, PCMCIA_PRODUCT_BAY_STACK_660, 107 PCMCIA_CIS_BAY_STACK_660 }, 108 109 { PCMCIA_VENDOR_BAY, PCMCIA_PRODUCT_BAY_SURFER_PRO, 110 PCMCIA_CIS_BAY_SURFER_PRO }, 111 112 { PCMCIA_VENDOR_AMD, PCMCIA_PRODUCT_AMD_AM79C930, 113 PCMCIA_CIS_AMD_AM79C930 }, 114 115 { PCMCIA_VENDOR_ICOM, PCMCIA_PRODUCT_ICOM_SL200, 116 PCMCIA_CIS_ICOM_SL200 }, 117 118 { PCMCIA_VENDOR_NOKIA, PCMCIA_PRODUCT_NOKIA_C020_WLAN, 119 PCMCIA_CIS_NOKIA_C020_WLAN }, 120 121 { PCMCIA_VENDOR_FARALLON, PCMCIA_PRODUCT_FARALLON_SKYLINE, 122 PCMCIA_CIS_FARALLON_SKYLINE }, 123 }; 124 static const size_t awi_pcmcia_nproducts = 125 sizeof(awi_pcmcia_products) / sizeof(awi_pcmcia_products[0]); 126 127 static int 128 awi_pcmcia_enable(sc) 129 struct awi_softc *sc; 130 { 131 struct awi_pcmcia_softc *psc = (struct awi_pcmcia_softc *)sc; 132 struct pcmcia_function *pf = psc->sc_pf; 133 int error; 134 135 /* establish the interrupt. */ 136 psc->sc_ih = pcmcia_intr_establish(pf, IPL_NET, awi_intr, sc); 137 if (!psc->sc_ih) 138 return (EIO); 139 140 error = pcmcia_function_enable(pf); 141 if (error) { 142 pcmcia_intr_disestablish(pf, psc->sc_ih); 143 psc->sc_ih = 0; 144 } 145 146 return (error); 147 } 148 149 static void 150 awi_pcmcia_disable(sc) 151 struct awi_softc *sc; 152 { 153 struct awi_pcmcia_softc *psc = (struct awi_pcmcia_softc *)sc; 154 struct pcmcia_function *pf = psc->sc_pf; 155 156 pcmcia_function_disable(pf); 157 pcmcia_intr_disestablish(pf, psc->sc_ih); 158 psc->sc_ih = 0; 159 } 160 161 static int 162 awi_pcmcia_match(parent, match, aux) 163 struct device *parent; 164 struct cfdata *match; 165 void *aux; 166 { 167 struct pcmcia_attach_args *pa = aux; 168 169 if (pcmcia_product_lookup(pa, awi_pcmcia_products, awi_pcmcia_nproducts, 170 sizeof(awi_pcmcia_products[0]), NULL)) 171 return (1); 172 return (0); 173 } 174 175 static int 176 awi_pcmcia_validate_config(cfe) 177 struct pcmcia_config_entry *cfe; 178 { 179 if (cfe->iftype != PCMCIA_IFTYPE_IO || 180 cfe->num_iospace < 1 || 181 cfe->iospace[0].length < AM79C930_IO_SIZE) 182 return (EINVAL); 183 if (cfe->num_memspace < 1) { 184 cfe->memspace[0].length = AM79C930_MEM_SIZE; 185 cfe->memspace[0].cardaddr = 0; 186 cfe->memspace[0].hostaddr = 0; 187 } else if (cfe->memspace[0].length < AM79C930_MEM_SIZE) 188 return (EINVAL); 189 return (0); 190 } 191 192 static void 193 awi_pcmcia_attach(parent, self, aux) 194 struct device *parent, *self; 195 void *aux; 196 { 197 struct awi_pcmcia_softc *psc = (void *)self; 198 struct awi_softc *sc = &psc->sc_awi; 199 struct pcmcia_attach_args *pa = aux; 200 struct pcmcia_config_entry *cfe; 201 int error; 202 203 psc->sc_pf = pa->pf; 204 205 error = pcmcia_function_configure(pa->pf, awi_pcmcia_validate_config); 206 if (error) { 207 aprint_error("%s: configure failed, error=%d\n", self->dv_xname, 208 error); 209 return; 210 } 211 212 cfe = pa->pf->cfe; 213 sc->sc_chip.sc_bustype = AM79C930_BUS_PCMCIA; 214 sc->sc_chip.sc_iot = cfe->iospace[0].handle.iot; 215 sc->sc_chip.sc_ioh = cfe->iospace[0].handle.ioh; 216 if (cfe->num_memspace > 0) { 217 sc->sc_chip.sc_memt = cfe->memspace[0].handle.memt; 218 sc->sc_chip.sc_memh = cfe->memspace[0].handle.memh; 219 am79c930_chip_init(&sc->sc_chip, 1); 220 } else 221 am79c930_chip_init(&sc->sc_chip, 0); 222 223 error = awi_pcmcia_enable(sc); 224 if (error) 225 goto fail; 226 sc->sc_enabled = 1; 227 228 awi_read_bytes(sc, AWI_BANNER, sc->sc_banner, AWI_BANNER_LEN); 229 if (memcmp(sc->sc_banner, "PCnetMobile:", 12)) 230 goto fail2; 231 232 sc->sc_enable = awi_pcmcia_enable; 233 sc->sc_disable = awi_pcmcia_disable; 234 235 sc->sc_cansleep = 1; 236 237 if (awi_attach(sc) != 0) { 238 printf("%s: failed to attach controller\n", self->dv_xname); 239 goto fail2; 240 } 241 242 sc->sc_enabled = 0; 243 awi_pcmcia_disable(sc); 244 psc->sc_state = AWI_PCMCIA_ATTACHED; 245 return; 246 247 fail2: 248 sc->sc_enabled = 0; 249 awi_pcmcia_disable(sc); 250 fail: 251 pcmcia_function_unconfigure(pa->pf); 252 } 253 254 static int 255 awi_pcmcia_detach(self, flags) 256 struct device *self; 257 int flags; 258 { 259 struct awi_pcmcia_softc *psc = (struct awi_pcmcia_softc *)self; 260 int error; 261 262 if (psc->sc_state != AWI_PCMCIA_ATTACHED) 263 return (0); 264 265 error = awi_detach(&psc->sc_awi); 266 if (error) 267 return (error); 268 269 pcmcia_function_unconfigure(psc->sc_pf); 270 271 return (0); 272 } 273