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