Home | History | Annotate | Line # | Download | only in pcmcia
if_sm_pcmcia.c revision 1.23
      1 /*	$NetBSD: if_sm_pcmcia.c,v 1.23 2000/07/30 21:34:48 briggs Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998, 2000 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/mii/mii.h>
     81 #include <dev/mii/miivar.h>
     82 
     83 #include <dev/ic/smc91cxxreg.h>
     84 #include <dev/ic/smc91cxxvar.h>
     85 
     86 #include <dev/pcmcia/pcmciareg.h>
     87 #include <dev/pcmcia/pcmciavar.h>
     88 #include <dev/pcmcia/pcmciadevs.h>
     89 
     90 int	sm_pcmcia_match __P((struct device *, struct cfdata *, void *));
     91 void	sm_pcmcia_attach __P((struct device *, struct device *, void *));
     92 int	sm_pcmcia_detach __P((struct device *, int));
     93 
     94 struct sm_pcmcia_softc {
     95 	struct	smc91cxx_softc sc_smc;		/* real "smc" softc */
     96 
     97 	/* PCMCIA-specific goo. */
     98 	struct	pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
     99 	int	sc_io_window;			/* our i/o window */
    100 	void	*sc_ih;				/* interrupt cookie */
    101 	struct	pcmcia_function *sc_pf;		/* our PCMCIA function */
    102 };
    103 
    104 struct cfattach sm_pcmcia_ca = {
    105 	sizeof(struct sm_pcmcia_softc), sm_pcmcia_match, sm_pcmcia_attach,
    106 	    sm_pcmcia_detach, smc91cxx_activate
    107 };
    108 
    109 int	sm_pcmcia_enable __P((struct smc91cxx_softc *));
    110 void	sm_pcmcia_disable __P((struct smc91cxx_softc *));
    111 
    112 int	sm_pcmcia_ascii_enaddr __P((const char *, u_int8_t *));
    113 int	sm_pcmcia_funce_enaddr __P((struct device *, u_int8_t *));
    114 
    115 int	sm_pcmcia_lannid_ciscallback __P((struct pcmcia_tuple *, void *));
    116 
    117 const struct pcmcia_product sm_pcmcia_products[] = {
    118 	{ PCMCIA_STR_MEGAHERTZ2_XJACK,		PCMCIA_VENDOR_MEGAHERTZ2,
    119 	  PCMCIA_PRODUCT_MEGAHERTZ2_XJACK,	 0, },
    120 
    121 	{ PCMCIA_STR_NEWMEDIA_BASICS,		PCMCIA_VENDOR_NEWMEDIA,
    122 	  PCMCIA_PRODUCT_NEWMEDIA_BASICS,	0, },
    123 
    124 #if 0
    125 	{ PCMCIA_STR_SMC_8020BT,		PCMCIA_VENDOR_SMC,
    126 	  PCMCIA_PRODUCT_SMC_8020BT,		0, },
    127 #endif
    128 
    129 	{ NULL }
    130 };
    131 
    132 int
    133 sm_pcmcia_match(parent, match, aux)
    134 	struct device *parent;
    135 	struct cfdata *match;
    136 	void *aux;
    137 {
    138 	struct pcmcia_attach_args *pa = aux;
    139 
    140 	if (pcmcia_product_lookup(pa, sm_pcmcia_products,
    141 	    sizeof sm_pcmcia_products[0], NULL) != NULL)
    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 	const struct pcmcia_product *pp;
    157 
    158 	psc->sc_pf = pa->pf;
    159 	cfe = pa->pf->cfe_head.sqh_first;
    160 
    161 	/* Enable the card. */
    162 	pcmcia_function_init(pa->pf, cfe);
    163 	if (pcmcia_function_enable(pa->pf)) {
    164 		printf(": function enable failed\n");
    165 		goto enable_failed;
    166 	}
    167 
    168 	/* XXX sanity check number of mem and i/o spaces */
    169 
    170 	/* Allocate and map i/o space for the card. */
    171 	if (pcmcia_io_alloc(pa->pf, 0, cfe->iospace[0].length,
    172 	    cfe->iospace[0].length, &psc->sc_pcioh)) {
    173 		printf(": can't allocate i/o space\n");
    174 		goto ioalloc_failed;
    175 	}
    176 
    177 	sc->sc_bst = psc->sc_pcioh.iot;
    178 	sc->sc_bsh = psc->sc_pcioh.ioh;
    179 
    180 	sc->sc_enable = sm_pcmcia_enable;
    181 	sc->sc_disable = sm_pcmcia_disable;
    182 
    183 	if (pcmcia_io_map(pa->pf, (cfe->flags & PCMCIA_CFE_IO16) ?
    184 	    PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_IO8, 0, cfe->iospace[0].length,
    185 	    &psc->sc_pcioh, &psc->sc_io_window)) {
    186 		printf(": can't map i/o space\n");
    187 		goto iomap_failed;
    188 	}
    189 
    190 	pp = pcmcia_product_lookup(pa, sm_pcmcia_products,
    191 	    sizeof sm_pcmcia_products[0], NULL);
    192 	if (pp == NULL)
    193 		panic("sm_pcmcia_attach: impossible");
    194 
    195 	printf(": %s\n", pp->pp_name);
    196 
    197 	/*
    198 	 * First try to get the Ethernet address from FUNCE/LANNID tuple.
    199 	 */
    200 	if (sm_pcmcia_funce_enaddr(parent, myla))
    201 		enaddr = myla;
    202 
    203 	/*
    204 	 * If that failed, try one of the CIS info strings.
    205 	 */
    206 	if (enaddr == NULL) {
    207 		char *cisstr = NULL;
    208 
    209 		switch (pa->manufacturer) {
    210 		case PCMCIA_VENDOR_MEGAHERTZ:
    211 		case PCMCIA_VENDOR_MEGAHERTZ2:
    212 			cisstr = pa->pf->sc->card.cis1_info[3];
    213 			break;
    214 
    215 		case PCMCIA_VENDOR_SMC:
    216 			cisstr = pa->pf->sc->card.cis1_info[2];
    217 			break;
    218 		}
    219 
    220 		if (cisstr != NULL && sm_pcmcia_ascii_enaddr(cisstr, myla))
    221 			enaddr = myla;
    222 	}
    223 
    224 	if (enaddr == NULL)
    225 		printf("%s: unable to get Ethernet address\n",
    226 		    sc->sc_dev.dv_xname);
    227 
    228 	/* Perform generic intialization. */
    229 	smc91cxx_attach(sc, enaddr);
    230 
    231 	pcmcia_function_disable(pa->pf);
    232 	return;
    233 
    234  iomap_failed:
    235 	/* Free our i/o space. */
    236 	pcmcia_io_free(pa->pf, &psc->sc_pcioh);
    237 
    238  ioalloc_failed:
    239 	/* Disable the device */
    240 	pcmcia_function_disable(pa->pf);
    241 
    242  enable_failed:
    243 	psc->sc_io_window = -1;
    244 }
    245 
    246 int
    247 sm_pcmcia_detach(self, flags)
    248 	struct device *self;
    249 	int flags;
    250 {
    251 	struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)self;
    252 	int rv;
    253 
    254 	if (psc->sc_io_window == -1)
    255 		/* Nothing to detach. */
    256 		return (0);
    257 
    258 	rv = smc91cxx_detach((struct device *)&psc->sc_smc, flags);
    259 	if (rv != 0)
    260 		return (rv);
    261 
    262 	/* Unmap our i/o window. */
    263 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    264 
    265 	/* Free our i/o space. */
    266 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    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 	u_int8_t digit;
    276 	int i;
    277 
    278 	memset(myla, 0, ETHER_ADDR_LEN);
    279 
    280 	for (i = 0, digit = 0; i < (ETHER_ADDR_LEN * 2); i++) {
    281 		if (cisstr[i] >= '0' && cisstr[i] <= '9')
    282 			digit |= cisstr[i] - '0';
    283 		else if (cisstr[i] >= 'a' && cisstr[i] <= 'f')
    284 			digit |= (cisstr[i] - 'a') + 10;
    285 		else if (cisstr[i] >= 'A' && cisstr[i] <= 'F')
    286 			digit |= (cisstr[i] - 'A') + 10;
    287 		else {
    288 			/* Bogus digit!! */
    289 			return (0);
    290 		}
    291 
    292 		/* Compensate for ordering of digits. */
    293 		if (i & 1) {
    294 			myla[i >> 1] = digit;
    295 			digit = 0;
    296 		} else
    297 			digit <<= 4;
    298 	}
    299 
    300 	return (1);
    301 }
    302 
    303 int
    304 sm_pcmcia_funce_enaddr(parent, myla)
    305 	struct device *parent;
    306 	u_int8_t *myla;
    307 {
    308 
    309 	return (pcmcia_scan_cis(parent, sm_pcmcia_lannid_ciscallback, myla));
    310 }
    311 
    312 int
    313 sm_pcmcia_lannid_ciscallback(tuple, arg)
    314 	struct pcmcia_tuple *tuple;
    315 	void *arg;
    316 {
    317 	u_int8_t *myla = arg;
    318 	int i;
    319 
    320 	if (tuple->code == PCMCIA_CISTPL_FUNCE) {
    321 		/* subcode, length */
    322 		if (tuple->length < 2)
    323 			return (0);
    324 
    325 		if ((pcmcia_tuple_read_1(tuple, 0) !=
    326 		     PCMCIA_TPLFE_TYPE_LAN_NID) ||
    327 		    (pcmcia_tuple_read_1(tuple, 1) != ETHER_ADDR_LEN))
    328 			return (0);
    329 
    330 		for (i = 0; i < ETHER_ADDR_LEN; i++)
    331 			myla[i] = pcmcia_tuple_read_1(tuple, i + 2);
    332 		return (1);
    333 	}
    334 	return (0);
    335 }
    336 
    337 int
    338 sm_pcmcia_enable(sc)
    339 	struct smc91cxx_softc *sc;
    340 {
    341 	struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
    342 	int rv;
    343 
    344 	/* Establish the interrupt handler. */
    345 	psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, smc91cxx_intr,
    346 	    sc);
    347 	if (psc->sc_ih == NULL) {
    348 		printf("%s: couldn't establish interrupt handler\n",
    349 		    sc->sc_dev.dv_xname);
    350 		return (1);
    351 	}
    352 
    353 	rv = pcmcia_function_enable(psc->sc_pf);
    354 	if (rv != 0)
    355 		pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    356 	return (rv);
    357 }
    358 
    359 void
    360 sm_pcmcia_disable(sc)
    361 	struct smc91cxx_softc *sc;
    362 {
    363 	struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
    364 
    365 	pcmcia_function_disable(psc->sc_pf);
    366 	pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    367 }
    368