Home | History | Annotate | Line # | Download | only in pcmcia
if_sm_pcmcia.c revision 1.40
      1 /*	$NetBSD: if_sm_pcmcia.c,v 1.40 2004/08/10 06:10:38 mycroft Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998, 2000, 2004 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, and by Charles M. Hannum.
     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.40 2004/08/10 06:10:38 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 int	sm_pcmcia_validate_config __P((struct pcmcia_config_entry *));
     73 void	sm_pcmcia_attach __P((struct device *, struct device *, void *));
     74 int	sm_pcmcia_detach __P((struct device *, int));
     75 
     76 struct sm_pcmcia_softc {
     77 	struct	smc91cxx_softc sc_smc;		/* real "smc" softc */
     78 
     79 	void	*sc_ih;				/* interrupt cookie */
     80 
     81 	struct	pcmcia_function *sc_pf;		/* our PCMCIA function */
     82 	int	sc_state;
     83 #define	SM_PCMCIA_ATTACHED	3
     84 };
     85 
     86 CFATTACH_DECL(sm_pcmcia, sizeof(struct sm_pcmcia_softc),
     87     sm_pcmcia_match, sm_pcmcia_attach, sm_pcmcia_detach, smc91cxx_activate);
     88 
     89 int	sm_pcmcia_enable __P((struct smc91cxx_softc *));
     90 void	sm_pcmcia_disable __P((struct smc91cxx_softc *));
     91 
     92 int	sm_pcmcia_ascii_enaddr __P((const char *, u_int8_t *));
     93 
     94 const struct pcmcia_product sm_pcmcia_products[] = {
     95 	{ "",	PCMCIA_VENDOR_MEGAHERTZ2,
     96 	  PCMCIA_PRODUCT_MEGAHERTZ2_EM1144,	 0, },
     97 	{ "",	PCMCIA_VENDOR_MEGAHERTZ2,
     98 	  PCMCIA_PRODUCT_MEGAHERTZ2_EM1144,	 1, },
     99 
    100 	{ "",	PCMCIA_VENDOR_MEGAHERTZ2,
    101 	  PCMCIA_PRODUCT_MEGAHERTZ2_XJACK,	 0, },
    102 
    103 	{ "",	PCMCIA_VENDOR_NEWMEDIA,
    104 	  PCMCIA_PRODUCT_NEWMEDIA_BASICS,	0, },
    105 
    106 #if 0
    107 	{ "",	PCMCIA_VENDOR_SMC,
    108 	  PCMCIA_PRODUCT_SMC_8020BT,		0, },
    109 #endif
    110 	{ "",	PCMCIA_VENDOR_PSION,
    111 	  PCMCIA_PRODUCT_PSION_GOLDCARD,	0, },
    112 
    113 	{ NULL }
    114 };
    115 
    116 int
    117 sm_pcmcia_match(parent, match, aux)
    118 	struct device *parent;
    119 	struct cfdata *match;
    120 	void *aux;
    121 {
    122 	struct pcmcia_attach_args *pa = aux;
    123 
    124 	/* This is to differentiate the serial function of Megahertz cards. */
    125 	if (pa->pf->function != PCMCIA_FUNCTION_NETWORK)
    126 		return (0);
    127 
    128 	if (pcmcia_product_lookup(pa, sm_pcmcia_products,
    129 	    sizeof sm_pcmcia_products[0], NULL) != NULL)
    130 		return (1);
    131 	return (0);
    132 }
    133 
    134 int
    135 sm_pcmcia_validate_config(cfe)
    136 	struct pcmcia_config_entry *cfe;
    137 {
    138 	if (cfe->iftype != PCMCIA_IFTYPE_IO ||
    139 	    cfe->num_memspace != 0 ||
    140 	    cfe->num_iospace != 1 ||
    141 	    cfe->iospace[0].length < SMC_IOSIZE)
    142 		return (EINVAL);
    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 enaddr[ETHER_ADDR_LEN];
    156 	int error;
    157 
    158 	aprint_normal("\n");
    159 	psc->sc_pf = pa->pf;
    160 
    161 	error = pcmcia_function_configure(pa->pf, sm_pcmcia_validate_config);
    162 	if (error) {
    163 		aprint_error("%s: configure failed, error=%d\n", self->dv_xname,
    164 		    error);
    165 		return;
    166 	}
    167 
    168 	cfe = pa->pf->cfe;
    169 	sc->sc_bst = cfe->iospace[0].handle.iot;
    170 	sc->sc_bsh = cfe->iospace[0].handle.ioh;
    171 
    172 	error = sm_pcmcia_enable(sc);
    173 	if (error) {
    174 		aprint_error("%s: enable failed, error=%d\n", self->dv_xname,
    175 		    error);
    176 		goto fail;
    177 	}
    178 
    179 	sc->sc_enable = sm_pcmcia_enable;
    180 	sc->sc_disable = sm_pcmcia_disable;
    181 
    182 	/*
    183 	 * First try to get the Ethernet address from FUNCE/LANNID tuple.
    184 	 * If that fails, try one of the CIS info strings.
    185 	 */
    186 	if (pa->pf->pf_funce_lan_nidlen == ETHER_ADDR_LEN) {
    187 		memcpy(enaddr, pa->pf->pf_funce_lan_nid, ETHER_ADDR_LEN);
    188 	} else {
    189 		if (!sm_pcmcia_ascii_enaddr(pa->pf->sc->card.cis1_info[3], enaddr) &&
    190 		    !sm_pcmcia_ascii_enaddr(pa->pf->sc->card.cis1_info[2], enaddr))
    191 			aprint_error("%s: unable to get Ethernet address\n",
    192 			    self->dv_xname);
    193 	}
    194 
    195 	/* Perform generic intialization. */
    196 	smc91cxx_attach(sc, enaddr);
    197 
    198 	psc->sc_state = SM_PCMCIA_ATTACHED;
    199 	sm_pcmcia_disable(sc);
    200 	return;
    201 
    202 fail:
    203 	pcmcia_function_unconfigure(pa->pf);
    204 }
    205 
    206 int
    207 sm_pcmcia_detach(self, flags)
    208 	struct device *self;
    209 	int flags;
    210 {
    211 	struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)self;
    212 	int error;
    213 
    214 	if (psc->sc_state != SM_PCMCIA_ATTACHED)
    215 		return (0);
    216 
    217 	error = smc91cxx_detach((struct device *)&psc->sc_smc, flags);
    218 	if (error)
    219 		return (error);
    220 
    221 	pcmcia_function_unconfigure(psc->sc_pf);
    222 
    223 	return (0);
    224 }
    225 
    226 int
    227 sm_pcmcia_ascii_enaddr(cisstr, myla)
    228 	const char *cisstr;
    229 	u_int8_t *myla;
    230 {
    231 	u_int8_t digit;
    232 	int i;
    233 
    234 	/* No CIS string. */
    235 	if (cisstr == 0)
    236 		return (0);
    237 
    238 	memset(myla, 0, ETHER_ADDR_LEN);
    239 
    240 	for (i = 0, digit = 0; i < (ETHER_ADDR_LEN * 2); i++) {
    241 		if (cisstr[i] >= '0' && cisstr[i] <= '9')
    242 			digit |= cisstr[i] - '0';
    243 		else if (cisstr[i] >= 'a' && cisstr[i] <= 'f')
    244 			digit |= (cisstr[i] - 'a') + 10;
    245 		else if (cisstr[i] >= 'A' && cisstr[i] <= 'F')
    246 			digit |= (cisstr[i] - 'A') + 10;
    247 		else {
    248 			/* Bogus digit!! */
    249 			return (0);
    250 		}
    251 
    252 		/* Compensate for ordering of digits. */
    253 		if (i & 1) {
    254 			myla[i >> 1] = digit;
    255 			digit = 0;
    256 		} else
    257 			digit <<= 4;
    258 	}
    259 
    260 	return (1);
    261 }
    262 
    263 int
    264 sm_pcmcia_enable(sc)
    265 	struct smc91cxx_softc *sc;
    266 {
    267 	struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
    268 	int error;
    269 
    270 	/* Establish the interrupt handler. */
    271 	psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, smc91cxx_intr,
    272 	    sc);
    273 	if (psc->sc_ih == NULL) {
    274 		printf("%s: couldn't establish interrupt handler\n",
    275 		    sc->sc_dev.dv_xname);
    276 		return (1);
    277 	}
    278 
    279 	error = pcmcia_function_enable(psc->sc_pf);
    280 	if (error)
    281 		pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    282 
    283 	return (error);
    284 }
    285 
    286 void
    287 sm_pcmcia_disable(sc)
    288 	struct smc91cxx_softc *sc;
    289 {
    290 	struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
    291 
    292 	pcmcia_function_disable(psc->sc_pf);
    293 	pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    294 	psc->sc_ih = 0;
    295 }
    296