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