Home | History | Annotate | Line # | Download | only in pcmcia
if_sm_pcmcia.c revision 1.14
      1 /*	$NetBSD: if_sm_pcmcia.c,v 1.14 1998/11/18 18:15:57 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998 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.
     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 "opt_inet.h"
     41 #include "opt_ns.h"
     42 #include "bpfilter.h"
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/mbuf.h>
     47 #include <sys/socket.h>
     48 #include <sys/ioctl.h>
     49 #include <sys/errno.h>
     50 #include <sys/syslog.h>
     51 #include <sys/select.h>
     52 #include <sys/device.h>
     53 
     54 #include <net/if.h>
     55 #include <net/if_dl.h>
     56 #include <net/if_ether.h>
     57 #include <net/if_media.h>
     58 
     59 #ifdef INET
     60 #include <netinet/in.h>
     61 #include <netinet/in_systm.h>
     62 #include <netinet/in_var.h>
     63 #include <netinet/ip.h>
     64 #include <netinet/if_inarp.h>
     65 #endif
     66 
     67 #ifdef NS
     68 #include <netns/ns.h>
     69 #include <netns/ns_if.h>
     70 #endif
     71 
     72 #if NBPFILTER > 0
     73 #include <net/bpf.h>
     74 #include <net/bpfdesc.h>
     75 #endif
     76 
     77 #include <machine/intr.h>
     78 #include <machine/bus.h>
     79 
     80 #include <dev/ic/smc91cxxreg.h>
     81 #include <dev/ic/smc91cxxvar.h>
     82 
     83 #include <dev/pcmcia/pcmciareg.h>
     84 #include <dev/pcmcia/pcmciavar.h>
     85 #include <dev/pcmcia/pcmciadevs.h>
     86 
     87 int	sm_pcmcia_match __P((struct device *, struct cfdata *, void *));
     88 void	sm_pcmcia_attach __P((struct device *, struct device *, void *));
     89 int	sm_pcmcia_detach __P((struct device *, int));
     90 int	sm_pcmcia_activate __P((struct device *, enum devact));
     91 
     92 struct sm_pcmcia_softc {
     93 	struct	smc91cxx_softc sc_smc;		/* real "smc" softc */
     94 
     95 	/* PCMCIA-specific goo. */
     96 	struct	pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
     97 	int	sc_io_window;			/* our i/o window */
     98 	void	*sc_ih;				/* interrupt cookie */
     99 	struct	pcmcia_function *sc_pf;		/* our PCMCIA function */
    100 };
    101 
    102 struct cfattach sm_pcmcia_ca = {
    103 	sizeof(struct sm_pcmcia_softc), sm_pcmcia_match, sm_pcmcia_attach,
    104 	    sm_pcmcia_detach, sm_pcmcia_activate
    105 };
    106 
    107 int	sm_pcmcia_enable __P((struct smc91cxx_softc *));
    108 void	sm_pcmcia_disable __P((struct smc91cxx_softc *));
    109 
    110 int	sm_pcmcia_ascii_enaddr __P((const char *, u_int8_t *));
    111 int	sm_pcmcia_funce_enaddr __P((struct device *, u_int8_t *));
    112 
    113 int	sm_pcmcia_lannid_ciscallback __P((struct pcmcia_tuple *, void *));
    114 
    115 struct sm_pcmcia_product {
    116 	u_int32_t	spp_vendor;	/* vendor ID */
    117 	u_int32_t	spp_product;	/* product ID */
    118 	int		spp_expfunc;	/* expected function */
    119 	const char	*spp_name;	/* product name */
    120 } sm_pcmcia_products[] = {
    121 	{ PCMCIA_VENDOR_MEGAHERTZ2,	PCMCIA_PRODUCT_MEGAHERTZ2_XJACK,
    122 	  0,				PCMCIA_STR_MEGAHERTZ2_XJACK },
    123 
    124 	{ PCMCIA_VENDOR_NEWMEDIA,	PCMCIA_PRODUCT_NEWMEDIA_BASICS,
    125 	  0,				PCMCIA_STR_NEWMEDIA_BASICS },
    126 
    127 #if 0
    128 	{ PCMCIA_VENDOR_SMC,		PCMCIA_PRODUCT_SMC_8020BT,
    129 	  0,				PCMCIA_STR_SMC_8020BT },
    130 #endif
    131 
    132 	{ 0,				0,
    133 	  0,				NULL },
    134 };
    135 
    136 struct sm_pcmcia_product *sm_pcmcia_lookup __P((struct pcmcia_attach_args *));
    137 
    138 struct sm_pcmcia_product *
    139 sm_pcmcia_lookup(pa)
    140 	struct pcmcia_attach_args *pa;
    141 {
    142 	struct sm_pcmcia_product *spp;
    143 
    144 	for (spp = sm_pcmcia_products; spp->spp_name != NULL; spp++)
    145 		if (pa->manufacturer == spp->spp_vendor &&
    146 		    pa->product == spp->spp_product &&
    147 		    pa->pf->number == spp->spp_expfunc)
    148 			return (spp);
    149 	return (NULL);
    150 }
    151 
    152 int
    153 sm_pcmcia_match(parent, match, aux)
    154 	struct device *parent;
    155 	struct cfdata *match;
    156 	void *aux;
    157 {
    158 	struct pcmcia_attach_args *pa = aux;
    159 
    160 	if (sm_pcmcia_lookup(pa) != NULL)
    161 		return (1);
    162 	return (0);
    163 }
    164 
    165 void
    166 sm_pcmcia_attach(parent, self, aux)
    167 	struct device *parent, *self;
    168 	void *aux;
    169 {
    170 	struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)self;
    171 	struct smc91cxx_softc *sc = &psc->sc_smc;
    172 	struct pcmcia_attach_args *pa = aux;
    173 	struct pcmcia_config_entry *cfe;
    174 	u_int8_t myla[ETHER_ADDR_LEN], *enaddr = NULL;
    175 	struct sm_pcmcia_product *spp;
    176 
    177 	psc->sc_pf = pa->pf;
    178 	cfe = pa->pf->cfe_head.sqh_first;
    179 
    180 	/* Enable the card. */
    181 	pcmcia_function_init(pa->pf, cfe);
    182 	if (pcmcia_function_enable(pa->pf)) {
    183 		printf(": function enable failed\n");
    184 		return;
    185 	}
    186 
    187 	/* XXX sanity check number of mem and i/o spaces */
    188 
    189 	/* Allocate and map i/o space for the card. */
    190 	if (pcmcia_io_alloc(pa->pf, 0, cfe->iospace[0].length,
    191 	    cfe->iospace[0].length, &psc->sc_pcioh)) {
    192 		printf(": can't allocate i/o space\n");
    193 		return;
    194 	}
    195 
    196 	sc->sc_bst = psc->sc_pcioh.iot;
    197 	sc->sc_bsh = psc->sc_pcioh.ioh;
    198 
    199 	sc->sc_enable = sm_pcmcia_enable;
    200 	sc->sc_disable = sm_pcmcia_disable;
    201 
    202 	if (pcmcia_io_map(pa->pf, (cfe->flags & PCMCIA_CFE_IO16) ?
    203 	    PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_IO8, 0, cfe->iospace[0].length,
    204 	    &psc->sc_pcioh, &psc->sc_io_window)) {
    205 		printf(": can't map i/o space\n");
    206 		return;
    207 	}
    208 
    209 	spp = sm_pcmcia_lookup(pa);
    210 	if (spp == NULL)
    211 		panic("sm_pcmcia_attach: impossible");
    212 
    213 	printf(": %s\n", spp->spp_name);
    214 
    215 	/*
    216 	 * First try to get the Ethernet address from FUNCE/LANNID tuple.
    217 	 */
    218 	if (sm_pcmcia_funce_enaddr(parent, myla))
    219 		enaddr = myla;
    220 
    221 	/*
    222 	 * If that failed, try one of the CIS info strings.
    223 	 */
    224 	if (enaddr == NULL) {
    225 		char *cisstr = NULL;
    226 
    227 		switch (pa->manufacturer) {
    228 		case PCMCIA_VENDOR_MEGAHERTZ2:
    229 			cisstr = pa->pf->sc->card.cis1_info[3];
    230 			break;
    231 
    232 		case PCMCIA_VENDOR_SMC:
    233 			cisstr = pa->pf->sc->card.cis1_info[2];
    234 			break;
    235 		}
    236 
    237 		if (cisstr != NULL && sm_pcmcia_ascii_enaddr(cisstr, myla))
    238 			enaddr = myla;
    239 	}
    240 
    241 	if (enaddr == NULL)
    242 		printf("%s: unable to get Ethernet address\n",
    243 		    sc->sc_dev.dv_xname);
    244 
    245 	/* Perform generic intialization. */
    246 	smc91cxx_attach(sc, enaddr);
    247 
    248 	pcmcia_function_disable(pa->pf);
    249 }
    250 
    251 int
    252 sm_pcmcia_detach(self, flags)
    253 	struct device *self;
    254 	int flags;
    255 {
    256 	struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)self;
    257 
    258 	/* Unmap our i/o window. */
    259 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    260 
    261 	/* Free our i/o space. */
    262 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    263 
    264 #ifdef notyet
    265 	/*
    266 	 * Our softc is about to go away, so drop our reference
    267 	 * to the ifnet.
    268 	 */
    269 	if_delref(psc->sc_smc.sc_ec.ec_if);
    270 	return (0);
    271 #else
    272 	return (EBUSY);
    273 #endif
    274 }
    275 
    276 int
    277 sm_pcmcia_activate(self, act)
    278 	struct device *self;
    279 	enum devact act;
    280 {
    281 	struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)self;
    282 	struct smc91cxx_softc *sc = &psc->sc_smc;
    283 	int rv = 0;
    284 
    285 	switch (act) {
    286 	case DVACT_ACTIVATE:
    287 		rv = EOPNOTSUPP;
    288 		break;
    289 
    290 	case DVACT_DEACTIVATE:
    291 #ifdef notyet
    292 		/* First, kill off the interface. */
    293 		if_detach(sc->sc_ec.ec_if);
    294 #endif
    295 
    296 		/* Now disable the interface.  This releases our interrupt. */
    297 		smc91cxx_disable(sc);
    298 		break;
    299 	}
    300 	return (rv);
    301 }
    302 
    303 int
    304 sm_pcmcia_ascii_enaddr(cisstr, myla)
    305 	const char *cisstr;
    306 	u_int8_t *myla;
    307 {
    308 	char enaddr_str[12];
    309 	int i, j;
    310 
    311 	if (strlen(cisstr) != 12) {
    312 		/* Bogus address! */
    313 		return (0);
    314 	}
    315 	bcopy(cisstr, enaddr_str, 12);
    316 	bzero(myla, sizeof(myla));
    317 	for (i = 0; i < 6; i++) {
    318 		for (j = 0; j < 2; j++) {
    319 			/* Convert to upper case. */
    320 			if (enaddr_str[(i * 2) + j] >= 'a' &&
    321 			    enaddr_str[(i * 2) + j] <= 'z')
    322 				enaddr_str[(i * 2) + j] -= 'a' - 'A';
    323 
    324 			/* Parse the digit. */
    325 			if (enaddr_str[(i * 2) + j] >= '0' &&
    326 			    enaddr_str[(i * 2) + j] <= '9')
    327 				myla[i] |= enaddr_str[(i * 2) + j]
    328 				    - '0';
    329 			else if (enaddr_str[(i * 2) + j] >= 'A' &&
    330 				 enaddr_str[(i * 2) + j] <= 'F')
    331 				myla[i] |= enaddr_str[(i * 2) + j]
    332 				    - 'A' + 10;
    333 			else {
    334 				/* Bogus digit!! */
    335 				return (0);
    336 			}
    337 
    338 			/* Compensate for ordering of digits. */
    339 			if (j == 0)
    340 				myla[i] <<= 4;
    341 		}
    342 	}
    343 
    344 	return (1);
    345 }
    346 
    347 int
    348 sm_pcmcia_funce_enaddr(parent, myla)
    349 	struct device *parent;
    350 	u_int8_t *myla;
    351 {
    352 
    353 	return (pcmcia_scan_cis(parent, sm_pcmcia_lannid_ciscallback, myla));
    354 }
    355 
    356 int
    357 sm_pcmcia_lannid_ciscallback(tuple, arg)
    358 	struct pcmcia_tuple *tuple;
    359 	void *arg;
    360 {
    361 	u_int8_t *myla = arg;
    362 	int i;
    363 
    364 	if (tuple->code == PCMCIA_CISTPL_FUNCE) {
    365 		/* subcode, length */
    366 		if (tuple->length < 2)
    367 			return (0);
    368 
    369 		if ((pcmcia_tuple_read_1(tuple, 0) !=
    370 		     PCMCIA_TPLFE_TYPE_LAN_NID) ||
    371 		    (pcmcia_tuple_read_1(tuple, 1) != ETHER_ADDR_LEN))
    372 			return (0);
    373 
    374 		for (i = 0; i < ETHER_ADDR_LEN; i++)
    375 			myla[i] = pcmcia_tuple_read_1(tuple, i + 2);
    376 		return (1);
    377 	}
    378 	return (0);
    379 }
    380 
    381 int
    382 sm_pcmcia_enable(sc)
    383 	struct smc91cxx_softc *sc;
    384 {
    385 	struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
    386 
    387 	/* Establish the interrupt handler. */
    388 	psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, smc91cxx_intr,
    389 	    sc);
    390 	if (psc->sc_ih == NULL) {
    391 		printf("%s: couldn't establish interrupt handler\n",
    392 		    sc->sc_dev.dv_xname);
    393 		return (1);
    394 	}
    395 
    396 	return (pcmcia_function_enable(psc->sc_pf));
    397 }
    398 
    399 void
    400 sm_pcmcia_disable(sc)
    401 	struct smc91cxx_softc *sc;
    402 {
    403 	struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
    404 
    405 	pcmcia_function_disable(psc->sc_pf);
    406 
    407 	pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    408 }
    409