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