1 /* $OpenBSD: if_sm_pcmcia.c,v 1.36 2015/03/14 03:38:49 jsg 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 #include <netinet/in.h> 52 #include <netinet/if_ether.h> 53 54 #if NBPFILTER > 0 55 #include <net/bpf.h> 56 #endif 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/smc91cxxvar.h> 65 66 #include <dev/pcmcia/pcmciareg.h> 67 #include <dev/pcmcia/pcmciavar.h> 68 #include <dev/pcmcia/pcmciadevs.h> 69 70 int sm_pcmcia_match(struct device *, void *, void *); 71 void sm_pcmcia_attach(struct device *, struct device *, void *); 72 int sm_pcmcia_detach(struct device *, int); 73 int sm_pcmcia_activate(struct device *, int); 74 75 struct sm_pcmcia_softc { 76 struct smc91cxx_softc sc_smc; /* real "smc" softc */ 77 78 /* PCMCIA-specific goo. */ 79 struct pcmcia_io_handle sc_pcioh; /* PCMCIA i/o space info */ 80 int sc_io_window; /* our i/o window */ 81 void *sc_ih; /* interrupt cookie */ 82 struct pcmcia_function *sc_pf; /* our PCMCIA function */ 83 }; 84 85 struct cfattach sm_pcmcia_ca = { 86 sizeof(struct sm_pcmcia_softc), sm_pcmcia_match, sm_pcmcia_attach, 87 sm_pcmcia_detach, sm_pcmcia_activate 88 }; 89 90 int sm_pcmcia_enable(struct smc91cxx_softc *); 91 void sm_pcmcia_disable(struct smc91cxx_softc *); 92 93 int sm_pcmcia_ascii_enaddr(const char *, u_int8_t *); 94 int sm_pcmcia_funce_enaddr(struct device *, u_int8_t *); 95 96 int sm_pcmcia_lannid_ciscallback(struct pcmcia_tuple *, void *); 97 98 struct sm_pcmcia_product { 99 u_int16_t spp_vendor; /* vendor ID */ 100 u_int16_t spp_product; /* product ID */ 101 int spp_expfunc; /* expected function */ 102 } sm_pcmcia_prod[] = { 103 { PCMCIA_VENDOR_MEGAHERTZ2, PCMCIA_PRODUCT_MEGAHERTZ2_XJACK, 104 0 }, 105 { PCMCIA_VENDOR_MEGAHERTZ2, PCMCIA_PRODUCT_MEGAHERTZ2_XJEM1144, 106 0 }, 107 { PCMCIA_VENDOR_NEWMEDIA, PCMCIA_PRODUCT_NEWMEDIA_BASICS, 108 0 }, 109 { PCMCIA_VENDOR_SMC, PCMCIA_PRODUCT_SMC_8020, 110 0 }, 111 { PCMCIA_VENDOR_PSION, PCMCIA_PRODUCT_PSION_GOLDCARD, 112 0 } 113 }; 114 115 int 116 sm_pcmcia_match(parent, match, aux) 117 struct device *parent; 118 void *match, *aux; 119 { 120 struct pcmcia_attach_args *pa = aux; 121 int i; 122 123 for (i = 0; i < nitems(sm_pcmcia_prod); i++) 124 if (pa->manufacturer == sm_pcmcia_prod[i].spp_vendor && 125 pa->product == sm_pcmcia_prod[i].spp_product && 126 pa->pf->number == sm_pcmcia_prod[i].spp_expfunc) 127 return (1); 128 return (0); 129 } 130 131 void 132 sm_pcmcia_attach(parent, self, aux) 133 struct device *parent, *self; 134 void *aux; 135 { 136 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)self; 137 struct smc91cxx_softc *sc = &psc->sc_smc; 138 struct pcmcia_attach_args *pa = aux; 139 struct pcmcia_config_entry *cfe; 140 u_int8_t myla[ETHER_ADDR_LEN], *enaddr = NULL; 141 const char *intrstr; 142 143 psc->sc_pf = pa->pf; 144 cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head); 145 146 /* Enable the card. */ 147 pcmcia_function_init(pa->pf, cfe); 148 if (pcmcia_function_enable(pa->pf)) { 149 printf(": function enable failed\n"); 150 return; 151 } 152 153 /* XXX sanity check number of mem and i/o spaces */ 154 155 /* Allocate and map i/o space for the card. */ 156 if (pcmcia_io_alloc(pa->pf, 0, cfe->iospace[0].length, 157 cfe->iospace[0].length, &psc->sc_pcioh)) { 158 printf(": can't allocate i/o space\n"); 159 return; 160 } 161 162 sc->sc_bst = psc->sc_pcioh.iot; 163 sc->sc_bsh = psc->sc_pcioh.ioh; 164 165 #ifdef notyet 166 sc->sc_enable = sm_pcmcia_enable; 167 sc->sc_disable = sm_pcmcia_disable; 168 #endif 169 sc->sc_enabled = 1; 170 171 if (pcmcia_io_map(pa->pf, (cfe->flags & PCMCIA_CFE_IO16) ? 172 PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_IO8, 0, cfe->iospace[0].length, 173 &psc->sc_pcioh, &psc->sc_io_window)) { 174 printf(": can't map i/o space\n"); 175 return; 176 } 177 178 printf(" port 0x%lx/%lu", psc->sc_pcioh.addr, 179 (u_long)psc->sc_pcioh.size); 180 181 /* 182 * First try to get the Ethernet address from FUNCE/LANNID tuple. 183 */ 184 if (sm_pcmcia_funce_enaddr(parent, myla)) 185 enaddr = myla; 186 187 /* 188 * If that failed, try one of the CIS info strings. 189 */ 190 if (enaddr == NULL) { 191 char *cisstr = NULL; 192 193 switch (pa->manufacturer) { 194 case PCMCIA_VENDOR_MEGAHERTZ2: 195 cisstr = pa->pf->sc->card.cis1_info[3]; 196 break; 197 case PCMCIA_VENDOR_SMC: 198 cisstr = pa->pf->sc->card.cis1_info[2]; 199 break; 200 } 201 if (cisstr != NULL && sm_pcmcia_ascii_enaddr(cisstr, myla)) 202 enaddr = myla; 203 } 204 205 if (enaddr == NULL) 206 printf(", unable to get Ethernet address\n"); 207 208 psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, 209 smc91cxx_intr, sc, sc->sc_dev.dv_xname); 210 intrstr = pcmcia_intr_string(psc->sc_pf, psc->sc_ih); 211 if (*intrstr) 212 printf(", %s", intrstr); 213 214 /* Perform generic initialization. */ 215 smc91cxx_attach(sc, enaddr); 216 217 #ifdef notyet 218 pcmcia_function_disable(pa->pf); 219 #endif 220 } 221 222 int 223 sm_pcmcia_detach(dev, flags) 224 struct device *dev; 225 int flags; 226 { 227 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)dev; 228 struct ifnet *ifp = &psc->sc_smc.sc_arpcom.ac_if; 229 int rv = 0; 230 231 pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window); 232 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh); 233 234 ether_ifdetach(ifp); 235 if_detach(ifp); 236 237 return (rv); 238 } 239 240 int 241 sm_pcmcia_activate(dev, act) 242 struct device *dev; 243 int act; 244 { 245 struct sm_pcmcia_softc *sc = (struct sm_pcmcia_softc *)dev; 246 struct ifnet *ifp = &sc->sc_smc.sc_arpcom.ac_if; 247 248 switch (act) { 249 case DVACT_DEACTIVATE: 250 ifp->if_timer = 0; 251 if (ifp->if_flags & IFF_RUNNING) 252 smc91cxx_stop(&sc->sc_smc); 253 if (sc->sc_ih) 254 pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih); 255 sc->sc_ih = NULL; 256 pcmcia_function_disable(sc->sc_pf); 257 break; 258 } 259 return (0); 260 } 261 262 int 263 sm_pcmcia_ascii_enaddr(cisstr, myla) 264 const char *cisstr; 265 u_int8_t *myla; 266 { 267 char enaddr_str[12]; 268 int i, j; 269 270 if (strlen(cisstr) != 12) { 271 /* Bogus address! */ 272 return (0); 273 } 274 bcopy(cisstr, enaddr_str, sizeof enaddr_str); 275 for (i = 0; i < 6; i++) { 276 for (j = 0; j < 2; j++) { 277 /* Convert to upper case. */ 278 if (enaddr_str[(i * 2) + j] >= 'a' && 279 enaddr_str[(i * 2) + j] <= 'z') 280 enaddr_str[(i * 2) + j] -= 'a' - 'A'; 281 282 /* Parse the digit. */ 283 if (enaddr_str[(i * 2) + j] >= '0' && 284 enaddr_str[(i * 2) + j] <= '9') 285 myla[i] |= enaddr_str[(i * 2) + j] 286 - '0'; 287 else if (enaddr_str[(i * 2) + j] >= 'A' && 288 enaddr_str[(i * 2) + j] <= 'F') 289 myla[i] |= enaddr_str[(i * 2) + j] 290 - 'A' + 10; 291 else { 292 /* Bogus digit!! */ 293 return (0); 294 } 295 296 /* Compensate for ordering of digits. */ 297 if (j == 0) 298 myla[i] <<= 4; 299 } 300 } 301 302 return (1); 303 } 304 305 int 306 sm_pcmcia_funce_enaddr(parent, myla) 307 struct device *parent; 308 u_int8_t *myla; 309 { 310 311 return (pcmcia_scan_cis(parent, sm_pcmcia_lannid_ciscallback, myla)); 312 } 313 314 int 315 sm_pcmcia_lannid_ciscallback(tuple, arg) 316 struct pcmcia_tuple *tuple; 317 void *arg; 318 { 319 u_int8_t *myla = arg; 320 int i; 321 322 if (tuple->code == PCMCIA_CISTPL_FUNCE || tuple->code == 323 PCMCIA_CISTPL_SPCL) { 324 /* subcode, length */ 325 if (tuple->length < 2) 326 return (0); 327 328 if ((pcmcia_tuple_read_1(tuple, 0) != 329 PCMCIA_TPLFE_TYPE_LAN_NID) || 330 (pcmcia_tuple_read_1(tuple, 1) != ETHER_ADDR_LEN)) 331 return (0); 332 333 for (i = 0; i < ETHER_ADDR_LEN; i++) 334 myla[i] = pcmcia_tuple_read_1(tuple, i + 2); 335 return (1); 336 } 337 return (0); 338 } 339 340 int 341 sm_pcmcia_enable(sc) 342 struct smc91cxx_softc *sc; 343 { 344 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc; 345 346 /* Establish the interrupt handler. */ 347 psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, smc91cxx_intr, 348 sc, sc->sc_dev.dv_xname); 349 if (psc->sc_ih == NULL) { 350 printf("%s: couldn't establish interrupt handler\n", 351 sc->sc_dev.dv_xname); 352 return (1); 353 } 354 355 return (pcmcia_function_enable(psc->sc_pf)); 356 } 357 358 void 359 sm_pcmcia_disable(sc) 360 struct smc91cxx_softc *sc; 361 { 362 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc; 363 364 pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih); 365 pcmcia_function_disable(psc->sc_pf); 366 } 367