1 /* $OpenBSD: if_sm_pcmcia.c,v 1.32 2011/07/03 15:47:17 matthew Exp $ */ 2 /* $NetBSD: if_sm_pcmcia.c,v 1.11 1998/08/15 20:47:32 thorpej Exp $ */ 3 4 /*- 5 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 10 * NASA Ames Research Center. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include "bpfilter.h" 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/mbuf.h> 39 #include <sys/socket.h> 40 #include <sys/ioctl.h> 41 #include <sys/errno.h> 42 #include <sys/syslog.h> 43 #include <sys/selinfo.h> 44 #include <sys/timeout.h> 45 #include <sys/device.h> 46 47 #include <net/if.h> 48 #include <net/if_dl.h> 49 #include <net/if_media.h> 50 51 #ifdef INET 52 #include <netinet/in.h> 53 #include <netinet/in_systm.h> 54 #include <netinet/in_var.h> 55 #include <netinet/ip.h> 56 #include <netinet/if_ether.h> 57 #endif 58 59 #if NBPFILTER > 0 60 #include <net/bpf.h> 61 #endif 62 63 #include <machine/intr.h> 64 #include <machine/bus.h> 65 66 #include <net/if_media.h> 67 68 #include <dev/mii/mii.h> 69 #include <dev/mii/miivar.h> 70 71 #include <dev/ic/smc91cxxreg.h> 72 #include <dev/ic/smc91cxxvar.h> 73 74 #include <dev/pcmcia/pcmciareg.h> 75 #include <dev/pcmcia/pcmciavar.h> 76 #include <dev/pcmcia/pcmciadevs.h> 77 78 int sm_pcmcia_match(struct device *, void *, void *); 79 void sm_pcmcia_attach(struct device *, struct device *, void *); 80 int sm_pcmcia_detach(struct device *, int); 81 int sm_pcmcia_activate(struct device *, int); 82 83 struct sm_pcmcia_softc { 84 struct smc91cxx_softc sc_smc; /* real "smc" softc */ 85 86 /* PCMCIA-specific goo. */ 87 struct pcmcia_io_handle sc_pcioh; /* PCMCIA i/o space info */ 88 int sc_io_window; /* our i/o window */ 89 void *sc_ih; /* interrupt cookie */ 90 struct pcmcia_function *sc_pf; /* our PCMCIA function */ 91 }; 92 93 struct cfattach sm_pcmcia_ca = { 94 sizeof(struct sm_pcmcia_softc), sm_pcmcia_match, sm_pcmcia_attach, 95 sm_pcmcia_detach, sm_pcmcia_activate 96 }; 97 98 int sm_pcmcia_enable(struct smc91cxx_softc *); 99 void sm_pcmcia_disable(struct smc91cxx_softc *); 100 101 int sm_pcmcia_ascii_enaddr(const char *, u_int8_t *); 102 int sm_pcmcia_funce_enaddr(struct device *, u_int8_t *); 103 104 int sm_pcmcia_lannid_ciscallback(struct pcmcia_tuple *, void *); 105 106 struct sm_pcmcia_product { 107 u_int16_t spp_vendor; /* vendor ID */ 108 u_int16_t spp_product; /* product ID */ 109 int spp_expfunc; /* expected function */ 110 } sm_pcmcia_prod[] = { 111 { PCMCIA_VENDOR_MEGAHERTZ2, PCMCIA_PRODUCT_MEGAHERTZ2_XJACK, 112 0 }, 113 { PCMCIA_VENDOR_MEGAHERTZ2, PCMCIA_PRODUCT_MEGAHERTZ2_XJEM1144, 114 0 }, 115 { PCMCIA_VENDOR_NEWMEDIA, PCMCIA_PRODUCT_NEWMEDIA_BASICS, 116 0 }, 117 { PCMCIA_VENDOR_SMC, PCMCIA_PRODUCT_SMC_8020, 118 0 }, 119 { PCMCIA_VENDOR_PSION, PCMCIA_PRODUCT_PSION_GOLDCARD, 120 0 } 121 }; 122 123 int 124 sm_pcmcia_match(parent, match, aux) 125 struct device *parent; 126 void *match, *aux; 127 { 128 struct pcmcia_attach_args *pa = aux; 129 int i; 130 131 for (i = 0; i < nitems(sm_pcmcia_prod); i++) 132 if (pa->manufacturer == sm_pcmcia_prod[i].spp_vendor && 133 pa->product == sm_pcmcia_prod[i].spp_product && 134 pa->pf->number == sm_pcmcia_prod[i].spp_expfunc) 135 return (1); 136 return (0); 137 } 138 139 void 140 sm_pcmcia_attach(parent, self, aux) 141 struct device *parent, *self; 142 void *aux; 143 { 144 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)self; 145 struct smc91cxx_softc *sc = &psc->sc_smc; 146 struct pcmcia_attach_args *pa = aux; 147 struct pcmcia_config_entry *cfe; 148 u_int8_t myla[ETHER_ADDR_LEN], *enaddr = NULL; 149 const char *intrstr; 150 151 psc->sc_pf = pa->pf; 152 cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head); 153 154 /* Enable the card. */ 155 pcmcia_function_init(pa->pf, cfe); 156 if (pcmcia_function_enable(pa->pf)) { 157 printf(": function enable failed\n"); 158 return; 159 } 160 161 /* XXX sanity check number of mem and i/o spaces */ 162 163 /* Allocate and map i/o space for the card. */ 164 if (pcmcia_io_alloc(pa->pf, 0, cfe->iospace[0].length, 165 cfe->iospace[0].length, &psc->sc_pcioh)) { 166 printf(": can't allocate i/o space\n"); 167 return; 168 } 169 170 sc->sc_bst = psc->sc_pcioh.iot; 171 sc->sc_bsh = psc->sc_pcioh.ioh; 172 173 #ifdef notyet 174 sc->sc_enable = sm_pcmcia_enable; 175 sc->sc_disable = sm_pcmcia_disable; 176 #endif 177 sc->sc_enabled = 1; 178 179 if (pcmcia_io_map(pa->pf, (cfe->flags & PCMCIA_CFE_IO16) ? 180 PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_IO8, 0, cfe->iospace[0].length, 181 &psc->sc_pcioh, &psc->sc_io_window)) { 182 printf(": can't map i/o space\n"); 183 return; 184 } 185 186 printf(" port 0x%lx/%lu", psc->sc_pcioh.addr, 187 (u_long)psc->sc_pcioh.size); 188 189 /* 190 * First try to get the Ethernet address from FUNCE/LANNID tuple. 191 */ 192 if (sm_pcmcia_funce_enaddr(parent, myla)) 193 enaddr = myla; 194 195 /* 196 * If that failed, try one of the CIS info strings. 197 */ 198 if (enaddr == NULL) { 199 char *cisstr = NULL; 200 201 switch (pa->manufacturer) { 202 case PCMCIA_VENDOR_MEGAHERTZ2: 203 cisstr = pa->pf->sc->card.cis1_info[3]; 204 break; 205 case PCMCIA_VENDOR_SMC: 206 cisstr = pa->pf->sc->card.cis1_info[2]; 207 break; 208 } 209 if (cisstr != NULL && sm_pcmcia_ascii_enaddr(cisstr, myla)) 210 enaddr = myla; 211 } 212 213 if (enaddr == NULL) 214 printf(", unable to get Ethernet address\n"); 215 216 psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, 217 smc91cxx_intr, sc, sc->sc_dev.dv_xname); 218 intrstr = pcmcia_intr_string(psc->sc_pf, psc->sc_ih); 219 if (*intrstr) 220 printf(", %s", intrstr); 221 222 /* Perform generic initialization. */ 223 smc91cxx_attach(sc, enaddr); 224 225 #ifdef notyet 226 pcmcia_function_disable(pa->pf); 227 #endif 228 } 229 230 int 231 sm_pcmcia_detach(dev, flags) 232 struct device *dev; 233 int flags; 234 { 235 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)dev; 236 struct ifnet *ifp = &psc->sc_smc.sc_arpcom.ac_if; 237 int rv = 0; 238 239 pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window); 240 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh); 241 242 ether_ifdetach(ifp); 243 if_detach(ifp); 244 245 return (rv); 246 } 247 248 int 249 sm_pcmcia_activate(dev, act) 250 struct device *dev; 251 int act; 252 { 253 struct sm_pcmcia_softc *sc = (struct sm_pcmcia_softc *)dev; 254 struct ifnet *ifp = &sc->sc_smc.sc_arpcom.ac_if; 255 256 switch (act) { 257 case DVACT_DEACTIVATE: 258 ifp->if_timer = 0; 259 if (ifp->if_flags & IFF_RUNNING) 260 smc91cxx_stop(&sc->sc_smc); 261 if (sc->sc_ih) 262 pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih); 263 sc->sc_ih = NULL; 264 pcmcia_function_disable(sc->sc_pf); 265 break; 266 } 267 return (0); 268 } 269 270 int 271 sm_pcmcia_ascii_enaddr(cisstr, myla) 272 const char *cisstr; 273 u_int8_t *myla; 274 { 275 char enaddr_str[12]; 276 int i, j; 277 278 if (strlen(cisstr) != 12) { 279 /* Bogus address! */ 280 return (0); 281 } 282 bcopy(cisstr, enaddr_str, sizeof enaddr_str); 283 for (i = 0; i < 6; i++) { 284 for (j = 0; j < 2; j++) { 285 /* Convert to upper case. */ 286 if (enaddr_str[(i * 2) + j] >= 'a' && 287 enaddr_str[(i * 2) + j] <= 'z') 288 enaddr_str[(i * 2) + j] -= 'a' - 'A'; 289 290 /* Parse the digit. */ 291 if (enaddr_str[(i * 2) + j] >= '0' && 292 enaddr_str[(i * 2) + j] <= '9') 293 myla[i] |= enaddr_str[(i * 2) + j] 294 - '0'; 295 else if (enaddr_str[(i * 2) + j] >= 'A' && 296 enaddr_str[(i * 2) + j] <= 'F') 297 myla[i] |= enaddr_str[(i * 2) + j] 298 - 'A' + 10; 299 else { 300 /* Bogus digit!! */ 301 return (0); 302 } 303 304 /* Compensate for ordering of digits. */ 305 if (j == 0) 306 myla[i] <<= 4; 307 } 308 } 309 310 return (1); 311 } 312 313 int 314 sm_pcmcia_funce_enaddr(parent, myla) 315 struct device *parent; 316 u_int8_t *myla; 317 { 318 319 return (pcmcia_scan_cis(parent, sm_pcmcia_lannid_ciscallback, myla)); 320 } 321 322 int 323 sm_pcmcia_lannid_ciscallback(tuple, arg) 324 struct pcmcia_tuple *tuple; 325 void *arg; 326 { 327 u_int8_t *myla = arg; 328 int i; 329 330 if (tuple->code == PCMCIA_CISTPL_FUNCE || tuple->code == 331 PCMCIA_CISTPL_SPCL) { 332 /* subcode, length */ 333 if (tuple->length < 2) 334 return (0); 335 336 if ((pcmcia_tuple_read_1(tuple, 0) != 337 PCMCIA_TPLFE_TYPE_LAN_NID) || 338 (pcmcia_tuple_read_1(tuple, 1) != ETHER_ADDR_LEN)) 339 return (0); 340 341 for (i = 0; i < ETHER_ADDR_LEN; i++) 342 myla[i] = pcmcia_tuple_read_1(tuple, i + 2); 343 return (1); 344 } 345 return (0); 346 } 347 348 int 349 sm_pcmcia_enable(sc) 350 struct smc91cxx_softc *sc; 351 { 352 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc; 353 354 /* Establish the interrupt handler. */ 355 psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, smc91cxx_intr, 356 sc, sc->sc_dev.dv_xname); 357 if (psc->sc_ih == NULL) { 358 printf("%s: couldn't establish interrupt handler\n", 359 sc->sc_dev.dv_xname); 360 return (1); 361 } 362 363 return (pcmcia_function_enable(psc->sc_pf)); 364 } 365 366 void 367 sm_pcmcia_disable(sc) 368 struct smc91cxx_softc *sc; 369 { 370 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc; 371 372 pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih); 373 pcmcia_function_disable(psc->sc_pf); 374 } 375