Home | History | Annotate | Line # | Download | only in pcmcia
if_sm_pcmcia.c revision 1.37
      1 /*	$NetBSD: if_sm_pcmcia.c,v 1.37 2004/08/09 18:30:51 mycroft 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.37 2004/08/09 18:30:51 mycroft 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 
     93 const struct pcmcia_product sm_pcmcia_products[] = {
     94 	{ "",	PCMCIA_VENDOR_MEGAHERTZ2,
     95 	  PCMCIA_PRODUCT_MEGAHERTZ2_EM1144,	 0, },
     96 	{ "",	PCMCIA_VENDOR_MEGAHERTZ2,
     97 	  PCMCIA_PRODUCT_MEGAHERTZ2_EM1144,	 1, },
     98 
     99 	{ "",	PCMCIA_VENDOR_MEGAHERTZ2,
    100 	  PCMCIA_PRODUCT_MEGAHERTZ2_XJACK,	 0, },
    101 
    102 	{ "",	PCMCIA_VENDOR_NEWMEDIA,
    103 	  PCMCIA_PRODUCT_NEWMEDIA_BASICS,	0, },
    104 
    105 #if 0
    106 	{ "",	PCMCIA_VENDOR_SMC,
    107 	  PCMCIA_PRODUCT_SMC_8020BT,		0, },
    108 #endif
    109 	{ "",	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 	/* This is to differentiate the serial function of Megahertz cards. */
    124 	if (pa->pf->function != PCMCIA_FUNCTION_NETWORK)
    125 		return (0);
    126 
    127 	if (pcmcia_product_lookup(pa, sm_pcmcia_products,
    128 	    sizeof sm_pcmcia_products[0], NULL) != NULL)
    129 		return (1);
    130 	return (0);
    131 }
    132 
    133 void
    134 sm_pcmcia_attach(parent, self, aux)
    135 	struct device *parent, *self;
    136 	void *aux;
    137 {
    138 	struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)self;
    139 	struct smc91cxx_softc *sc = &psc->sc_smc;
    140 	struct pcmcia_attach_args *pa = aux;
    141 	struct pcmcia_config_entry *cfe;
    142 	u_int8_t enaddr[ETHER_ADDR_LEN];
    143 	const struct pcmcia_product *pp;
    144 
    145 	aprint_normal("\n");
    146 
    147 	psc->sc_pf = pa->pf;
    148 
    149 	SIMPLEQ_FOREACH(cfe, &pa->pf->cfe_head, cfe_list) {
    150 		if (cfe->num_iospace != 1)
    151 			continue;
    152 
    153 		/* Allocate I/O space for the card. */
    154 		if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
    155 		    cfe->iospace[0].length, cfe->iospace[0].length,
    156 		    &psc->sc_pcioh) == 0)
    157 			break;
    158 	}
    159 
    160 	if (cfe == 0) {
    161 		aprint_error("%s: can't allocate i/o space\n", self->dv_xname);
    162 		goto ioalloc_failed;
    163 	}
    164 
    165 	/* Enable the card. */
    166 	pcmcia_function_init(pa->pf, cfe);
    167 	if (pcmcia_function_enable(pa->pf)) {
    168 		aprint_error("%s: function enable failed\n", self->dv_xname);
    169 		goto enable_failed;
    170 	}
    171 
    172 	/* XXX sanity check number of mem and i/o spaces */
    173 
    174 	sc->sc_bst = psc->sc_pcioh.iot;
    175 	sc->sc_bsh = psc->sc_pcioh.ioh;
    176 
    177 	sc->sc_enable = sm_pcmcia_enable;
    178 	sc->sc_disable = sm_pcmcia_disable;
    179 
    180 	if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO, &psc->sc_pcioh,
    181 	    &psc->sc_io_window)) {
    182 		aprint_error("%s: can't map i/o space\n", self->dv_xname);
    183 		goto iomap_failed;
    184 	}
    185 
    186 	pp = pcmcia_product_lookup(pa, sm_pcmcia_products,
    187 	    sizeof sm_pcmcia_products[0], NULL);
    188 	if (pp == NULL)
    189 		panic("sm_pcmcia_attach: impossible");
    190 
    191 	/*
    192 	 * First try to get the Ethernet address from FUNCE/LANNID tuple.
    193 	 * If that fails, try one of the CIS info strings.
    194 	 */
    195 	if (pa->pf->pf_funce_lan_nidlen == ETHER_ADDR_LEN) {
    196 		memcpy(enaddr, pa->pf->pf_funce_lan_nid, ETHER_ADDR_LEN);
    197 	} else {
    198 		if (!sm_pcmcia_ascii_enaddr(pa->pf->sc->card.cis1_info[3], enaddr) &&
    199 		    !sm_pcmcia_ascii_enaddr(pa->pf->sc->card.cis1_info[2], enaddr))
    200 			aprint_error("%s: unable to get Ethernet address\n",
    201 			    self->dv_xname);
    202 	}
    203 
    204 	/* Perform generic intialization. */
    205 	smc91cxx_attach(sc, enaddr);
    206 
    207 	pcmcia_function_disable(pa->pf);
    208 	return;
    209 
    210 iomap_failed:
    211 	pcmcia_function_disable(pa->pf);
    212 
    213 enable_failed:
    214 	pcmcia_io_free(pa->pf, &psc->sc_pcioh);
    215 
    216 ioalloc_failed:
    217 	psc->sc_io_window = -1;
    218 }
    219 
    220 int
    221 sm_pcmcia_detach(self, flags)
    222 	struct device *self;
    223 	int flags;
    224 {
    225 	struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)self;
    226 	int rv;
    227 
    228 	if (psc->sc_io_window == -1)
    229 		/* Nothing to detach. */
    230 		return (0);
    231 
    232 	rv = smc91cxx_detach((struct device *)&psc->sc_smc, flags);
    233 	if (rv != 0)
    234 		return (rv);
    235 
    236 	/* Unmap our i/o window. */
    237 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    238 
    239 	/* Free our i/o space. */
    240 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    241 	return (0);
    242 }
    243 
    244 int
    245 sm_pcmcia_ascii_enaddr(cisstr, myla)
    246 	const char *cisstr;
    247 	u_int8_t *myla;
    248 {
    249 	u_int8_t digit;
    250 	int i;
    251 
    252 	/* No CIS string. */
    253 	if (cisstr == 0)
    254 		return (0);
    255 
    256 	memset(myla, 0, ETHER_ADDR_LEN);
    257 
    258 	for (i = 0, digit = 0; i < (ETHER_ADDR_LEN * 2); i++) {
    259 		if (cisstr[i] >= '0' && cisstr[i] <= '9')
    260 			digit |= cisstr[i] - '0';
    261 		else if (cisstr[i] >= 'a' && cisstr[i] <= 'f')
    262 			digit |= (cisstr[i] - 'a') + 10;
    263 		else if (cisstr[i] >= 'A' && cisstr[i] <= 'F')
    264 			digit |= (cisstr[i] - 'A') + 10;
    265 		else {
    266 			/* Bogus digit!! */
    267 			return (0);
    268 		}
    269 
    270 		/* Compensate for ordering of digits. */
    271 		if (i & 1) {
    272 			myla[i >> 1] = digit;
    273 			digit = 0;
    274 		} else
    275 			digit <<= 4;
    276 	}
    277 
    278 	return (1);
    279 }
    280 
    281 int
    282 sm_pcmcia_enable(sc)
    283 	struct smc91cxx_softc *sc;
    284 {
    285 	struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
    286 	int rv;
    287 
    288 	/* Establish the interrupt handler. */
    289 	psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, smc91cxx_intr,
    290 	    sc);
    291 	if (psc->sc_ih == NULL) {
    292 		printf("%s: couldn't establish interrupt handler\n",
    293 		    sc->sc_dev.dv_xname);
    294 		return (1);
    295 	}
    296 
    297 	rv = pcmcia_function_enable(psc->sc_pf);
    298 	if (rv != 0) {
    299 		pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    300 		return (rv);
    301 	}
    302 
    303 	return (0);
    304 }
    305 
    306 void
    307 sm_pcmcia_disable(sc)
    308 	struct smc91cxx_softc *sc;
    309 {
    310 	struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
    311 
    312 	pcmcia_function_disable(psc->sc_pf);
    313 	pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    314 }
    315