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