1 /* $NetBSD: if_sm_pcmcia.c,v 1.47 2006/10/12 01:31:50 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 1997, 1998, 2000, 2004 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 9 * NASA Ames Research Center, and by Charles M. Hannum. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40 #include <sys/cdefs.h> 41 __KERNEL_RCSID(0, "$NetBSD: if_sm_pcmcia.c,v 1.47 2006/10/12 01:31:50 christos Exp $"); 42 43 #include <sys/param.h> 44 #include <sys/systm.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 58 #include <machine/intr.h> 59 #include <machine/bus.h> 60 61 #include <dev/mii/mii.h> 62 #include <dev/mii/miivar.h> 63 64 #include <dev/ic/smc91cxxreg.h> 65 #include <dev/ic/smc91cxxvar.h> 66 67 #include <dev/pcmcia/pcmciareg.h> 68 #include <dev/pcmcia/pcmciavar.h> 69 #include <dev/pcmcia/pcmciadevs.h> 70 71 int sm_pcmcia_match(struct device *, struct cfdata *, void *); 72 int sm_pcmcia_validate_config(struct pcmcia_config_entry *); 73 void sm_pcmcia_attach(struct device *, struct device *, void *); 74 int sm_pcmcia_detach(struct device *, int); 75 76 struct sm_pcmcia_softc { 77 struct smc91cxx_softc sc_smc; /* real "smc" softc */ 78 79 struct pcmcia_function *sc_pf; /* our PCMCIA function */ 80 void *sc_ih; /* interrupt cookie */ 81 82 int sc_state; 83 #define SM_PCMCIA_ATTACHED 3 84 }; 85 86 CFATTACH_DECL(sm_pcmcia, sizeof(struct sm_pcmcia_softc), 87 sm_pcmcia_match, sm_pcmcia_attach, sm_pcmcia_detach, smc91cxx_activate); 88 89 int sm_pcmcia_enable(struct smc91cxx_softc *); 90 void sm_pcmcia_disable(struct smc91cxx_softc *); 91 92 int sm_pcmcia_ascii_enaddr(const char *, u_int8_t *); 93 94 const struct pcmcia_product sm_pcmcia_products[] = { 95 { PCMCIA_VENDOR_MEGAHERTZ2, PCMCIA_PRODUCT_MEGAHERTZ2_EM1144, 96 PCMCIA_CIS_INVALID }, 97 98 { PCMCIA_VENDOR_MEGAHERTZ2, PCMCIA_PRODUCT_MEGAHERTZ2_XJACK, 99 PCMCIA_CIS_INVALID }, 100 101 { PCMCIA_VENDOR_NEWMEDIA, PCMCIA_PRODUCT_NEWMEDIA_BASICS, 102 PCMCIA_CIS_INVALID }, 103 104 #if 0 105 { PCMCIA_VENDOR_SMC, PCMCIA_PRODUCT_SMC_8020BT, 106 PCMCIA_CIS_INVALID }, 107 #endif 108 { PCMCIA_VENDOR_PSION, PCMCIA_PRODUCT_PSION_GOLDCARD, 109 PCMCIA_CIS_INVALID }, 110 }; 111 const size_t sm_pcmcia_nproducts = 112 sizeof(sm_pcmcia_products) / sizeof(sm_pcmcia_products[0]); 113 114 int 115 sm_pcmcia_match(struct device *parent __unused, struct cfdata *match __unused, 116 void *aux) 117 { 118 struct pcmcia_attach_args *pa = aux; 119 120 /* This is to differentiate the serial function of some cards. */ 121 if (pa->pf->function != PCMCIA_FUNCTION_NETWORK) 122 return (0); 123 124 if (pcmcia_product_lookup(pa, sm_pcmcia_products, sm_pcmcia_nproducts, 125 sizeof(sm_pcmcia_products[0]), NULL)) 126 return (1); 127 return (0); 128 } 129 130 int 131 sm_pcmcia_validate_config(cfe) 132 struct pcmcia_config_entry *cfe; 133 { 134 if (cfe->iftype != PCMCIA_IFTYPE_IO || 135 cfe->num_memspace != 0 || 136 cfe->num_iospace != 1 || 137 cfe->iospace[0].length < SMC_IOSIZE) 138 return (EINVAL); 139 return (0); 140 } 141 142 void 143 sm_pcmcia_attach(struct device *parent __unused, struct device *self, void *aux) 144 { 145 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)self; 146 struct smc91cxx_softc *sc = &psc->sc_smc; 147 struct pcmcia_attach_args *pa = aux; 148 struct pcmcia_config_entry *cfe; 149 u_int8_t enaddr[ETHER_ADDR_LEN]; 150 int error; 151 152 psc->sc_pf = pa->pf; 153 154 error = pcmcia_function_configure(pa->pf, sm_pcmcia_validate_config); 155 if (error) { 156 aprint_error("%s: configure failed, error=%d\n", self->dv_xname, 157 error); 158 return; 159 } 160 161 cfe = pa->pf->cfe; 162 sc->sc_bst = cfe->iospace[0].handle.iot; 163 sc->sc_bsh = cfe->iospace[0].handle.ioh; 164 165 error = sm_pcmcia_enable(sc); 166 if (error) 167 goto fail; 168 169 sc->sc_enable = sm_pcmcia_enable; 170 sc->sc_disable = sm_pcmcia_disable; 171 172 /* 173 * First try to get the Ethernet address from FUNCE/LANNID tuple. 174 * If that fails, try one of the CIS info strings. 175 */ 176 if (pa->pf->pf_funce_lan_nidlen == ETHER_ADDR_LEN) { 177 memcpy(enaddr, pa->pf->pf_funce_lan_nid, ETHER_ADDR_LEN); 178 } else { 179 if (!sm_pcmcia_ascii_enaddr(pa->pf->sc->card.cis1_info[3], enaddr) && 180 !sm_pcmcia_ascii_enaddr(pa->pf->sc->card.cis1_info[2], enaddr)) 181 aprint_error("%s: unable to get Ethernet address\n", 182 self->dv_xname); 183 } 184 185 /* Perform generic intialization. */ 186 smc91cxx_attach(sc, enaddr); 187 188 psc->sc_state = SM_PCMCIA_ATTACHED; 189 sm_pcmcia_disable(sc); 190 return; 191 192 fail: 193 pcmcia_function_unconfigure(pa->pf); 194 } 195 196 int 197 sm_pcmcia_detach(self, flags) 198 struct device *self; 199 int flags; 200 { 201 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)self; 202 int error; 203 204 if (psc->sc_state != SM_PCMCIA_ATTACHED) 205 return (0); 206 207 error = smc91cxx_detach((struct device *)&psc->sc_smc, flags); 208 if (error) 209 return (error); 210 211 pcmcia_function_unconfigure(psc->sc_pf); 212 213 return (0); 214 } 215 216 int 217 sm_pcmcia_ascii_enaddr(cisstr, myla) 218 const char *cisstr; 219 u_int8_t *myla; 220 { 221 u_int8_t digit; 222 int i; 223 224 /* No CIS string. */ 225 if (cisstr == 0) 226 return (0); 227 228 memset(myla, 0, ETHER_ADDR_LEN); 229 230 for (i = 0, digit = 0; i < (ETHER_ADDR_LEN * 2); i++) { 231 if (cisstr[i] >= '0' && cisstr[i] <= '9') 232 digit |= cisstr[i] - '0'; 233 else if (cisstr[i] >= 'a' && cisstr[i] <= 'f') 234 digit |= (cisstr[i] - 'a') + 10; 235 else if (cisstr[i] >= 'A' && cisstr[i] <= 'F') 236 digit |= (cisstr[i] - 'A') + 10; 237 else { 238 /* Bogus digit!! */ 239 return (0); 240 } 241 242 /* Compensate for ordering of digits. */ 243 if (i & 1) { 244 myla[i >> 1] = digit; 245 digit = 0; 246 } else 247 digit <<= 4; 248 } 249 250 return (1); 251 } 252 253 int 254 sm_pcmcia_enable(sc) 255 struct smc91cxx_softc *sc; 256 { 257 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc; 258 int error; 259 260 /* Establish the interrupt handler. */ 261 psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, smc91cxx_intr, 262 sc); 263 if (!psc->sc_ih) 264 return (EIO); 265 266 error = pcmcia_function_enable(psc->sc_pf); 267 if (error) { 268 pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih); 269 psc->sc_ih = 0; 270 } 271 272 return (error); 273 } 274 275 void 276 sm_pcmcia_disable(sc) 277 struct smc91cxx_softc *sc; 278 { 279 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc; 280 281 pcmcia_function_disable(psc->sc_pf); 282 pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih); 283 psc->sc_ih = 0; 284 } 285