Home | History | Annotate | Line # | Download | only in pcmcia
if_sm_pcmcia.c revision 1.50.2.1
      1 /*	$NetBSD: if_sm_pcmcia.c,v 1.50.2.1 2008/05/18 12:34:36 yamt 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  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: if_sm_pcmcia.c,v 1.50.2.1 2008/05/18 12:34:36 yamt Exp $");
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/mbuf.h>
     39 #include <sys/socket.h>
     40 #include <sys/ioctl.h>
     41 #include <sys/errno.h>
     42 #include <sys/syslog.h>
     43 #include <sys/select.h>
     44 #include <sys/device.h>
     45 
     46 #include <net/if.h>
     47 #include <net/if_dl.h>
     48 #include <net/if_ether.h>
     49 #include <net/if_media.h>
     50 
     51 #include <sys/intr.h>
     52 #include <sys/bus.h>
     53 
     54 #include <dev/mii/mii.h>
     55 #include <dev/mii/miivar.h>
     56 
     57 #include <dev/ic/smc91cxxreg.h>
     58 #include <dev/ic/smc91cxxvar.h>
     59 
     60 #include <dev/pcmcia/pcmciareg.h>
     61 #include <dev/pcmcia/pcmciavar.h>
     62 #include <dev/pcmcia/pcmciadevs.h>
     63 
     64 int	sm_pcmcia_match(struct device *, struct cfdata *, void *);
     65 int	sm_pcmcia_validate_config(struct pcmcia_config_entry *);
     66 void	sm_pcmcia_attach(struct device *, struct device *, void *);
     67 int	sm_pcmcia_detach(struct device *, int);
     68 
     69 struct sm_pcmcia_softc {
     70 	struct	smc91cxx_softc sc_smc;		/* real "smc" softc */
     71 
     72 	struct	pcmcia_function *sc_pf;		/* our PCMCIA function */
     73 	void	*sc_ih;				/* interrupt cookie */
     74 
     75 	int	sc_state;
     76 #define	SM_PCMCIA_ATTACHED	3
     77 };
     78 
     79 CFATTACH_DECL(sm_pcmcia, sizeof(struct sm_pcmcia_softc),
     80     sm_pcmcia_match, sm_pcmcia_attach, sm_pcmcia_detach, smc91cxx_activate);
     81 
     82 int	sm_pcmcia_enable(struct smc91cxx_softc *);
     83 void	sm_pcmcia_disable(struct smc91cxx_softc *);
     84 
     85 int	sm_pcmcia_ascii_enaddr(const char *, u_int8_t *);
     86 
     87 const struct pcmcia_product sm_pcmcia_products[] = {
     88 	{ PCMCIA_VENDOR_MEGAHERTZ2, PCMCIA_PRODUCT_MEGAHERTZ2_EM1144,
     89 	  PCMCIA_CIS_INVALID },
     90 
     91 	{ PCMCIA_VENDOR_MEGAHERTZ2, PCMCIA_PRODUCT_MEGAHERTZ2_XJACK,
     92 	  PCMCIA_CIS_INVALID },
     93 
     94 	{ PCMCIA_VENDOR_NEWMEDIA, PCMCIA_PRODUCT_NEWMEDIA_BASICS,
     95 	  PCMCIA_CIS_INVALID },
     96 
     97 #if 0
     98 	{ PCMCIA_VENDOR_SMC, PCMCIA_PRODUCT_SMC_8020BT,
     99 	  PCMCIA_CIS_INVALID },
    100 #endif
    101 	{ PCMCIA_VENDOR_PSION, PCMCIA_PRODUCT_PSION_GOLDCARD,
    102 	  PCMCIA_CIS_INVALID },
    103 };
    104 const size_t sm_pcmcia_nproducts =
    105     sizeof(sm_pcmcia_products) / sizeof(sm_pcmcia_products[0]);
    106 
    107 int
    108 sm_pcmcia_match(struct device *parent, struct cfdata *match,
    109     void *aux)
    110 {
    111 	struct pcmcia_attach_args *pa = aux;
    112 
    113 	/* This is to differentiate the serial function of some cards. */
    114 	if (pa->pf->function != PCMCIA_FUNCTION_NETWORK)
    115 		return (0);
    116 
    117 	if (pcmcia_product_lookup(pa, sm_pcmcia_products, sm_pcmcia_nproducts,
    118 	    sizeof(sm_pcmcia_products[0]), NULL))
    119 		return (1);
    120 	return (0);
    121 }
    122 
    123 int
    124 sm_pcmcia_validate_config(cfe)
    125 	struct pcmcia_config_entry *cfe;
    126 {
    127 	if (cfe->iftype != PCMCIA_IFTYPE_IO ||
    128 	    cfe->num_memspace != 0 ||
    129 	    cfe->num_iospace != 1 ||
    130 	    cfe->iospace[0].length < SMC_IOSIZE)
    131 		return (EINVAL);
    132 	return (0);
    133 }
    134 
    135 void
    136 sm_pcmcia_attach(struct device *parent, struct device *self, 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 	int error;
    144 
    145 	psc->sc_pf = pa->pf;
    146 
    147 	error = pcmcia_function_configure(pa->pf, sm_pcmcia_validate_config);
    148 	if (error) {
    149 		aprint_error_dev(self, "configure failed, error=%d\n",
    150 		    error);
    151 		return;
    152 	}
    153 
    154 	cfe = pa->pf->cfe;
    155 	sc->sc_bst = cfe->iospace[0].handle.iot;
    156 	sc->sc_bsh = cfe->iospace[0].handle.ioh;
    157 
    158 	error = sm_pcmcia_enable(sc);
    159 	if (error)
    160 		goto fail;
    161 
    162 	sc->sc_enable = sm_pcmcia_enable;
    163 	sc->sc_disable = sm_pcmcia_disable;
    164 
    165 	/*
    166 	 * First try to get the Ethernet address from FUNCE/LANNID tuple.
    167 	 * If that fails, try one of the CIS info strings.
    168 	 */
    169 	if (pa->pf->pf_funce_lan_nidlen == ETHER_ADDR_LEN) {
    170 		memcpy(enaddr, pa->pf->pf_funce_lan_nid, ETHER_ADDR_LEN);
    171 	} else {
    172 		if (!sm_pcmcia_ascii_enaddr(pa->pf->sc->card.cis1_info[3], enaddr) &&
    173 		    !sm_pcmcia_ascii_enaddr(pa->pf->sc->card.cis1_info[2], enaddr))
    174 			aprint_error_dev(self, "unable to get Ethernet address\n");
    175 	}
    176 
    177 	/* Perform generic intialization. */
    178 	smc91cxx_attach(sc, enaddr);
    179 
    180 	psc->sc_state = SM_PCMCIA_ATTACHED;
    181 	sm_pcmcia_disable(sc);
    182 	return;
    183 
    184 fail:
    185 	pcmcia_function_unconfigure(pa->pf);
    186 }
    187 
    188 int
    189 sm_pcmcia_detach(self, flags)
    190 	struct device *self;
    191 	int flags;
    192 {
    193 	struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)self;
    194 	int error;
    195 
    196 	if (psc->sc_state != SM_PCMCIA_ATTACHED)
    197 		return (0);
    198 
    199 	error = smc91cxx_detach((struct device *)&psc->sc_smc, flags);
    200 	if (error)
    201 		return (error);
    202 
    203 	pcmcia_function_unconfigure(psc->sc_pf);
    204 
    205 	return (0);
    206 }
    207 
    208 int
    209 sm_pcmcia_ascii_enaddr(cisstr, myla)
    210 	const char *cisstr;
    211 	u_int8_t *myla;
    212 {
    213 	u_int8_t digit;
    214 	int i;
    215 
    216 	/* No CIS string. */
    217 	if (cisstr == 0)
    218 		return (0);
    219 
    220 	memset(myla, 0, ETHER_ADDR_LEN);
    221 
    222 	for (i = 0, digit = 0; i < (ETHER_ADDR_LEN * 2); i++) {
    223 		if (cisstr[i] >= '0' && cisstr[i] <= '9')
    224 			digit |= cisstr[i] - '0';
    225 		else if (cisstr[i] >= 'a' && cisstr[i] <= 'f')
    226 			digit |= (cisstr[i] - 'a') + 10;
    227 		else if (cisstr[i] >= 'A' && cisstr[i] <= 'F')
    228 			digit |= (cisstr[i] - 'A') + 10;
    229 		else {
    230 			/* Bogus digit!! */
    231 			return (0);
    232 		}
    233 
    234 		/* Compensate for ordering of digits. */
    235 		if (i & 1) {
    236 			myla[i >> 1] = digit;
    237 			digit = 0;
    238 		} else
    239 			digit <<= 4;
    240 	}
    241 
    242 	return (1);
    243 }
    244 
    245 int
    246 sm_pcmcia_enable(sc)
    247 	struct smc91cxx_softc *sc;
    248 {
    249 	struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
    250 	int error;
    251 
    252 	/* Establish the interrupt handler. */
    253 	psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, smc91cxx_intr,
    254 	    sc);
    255 	if (!psc->sc_ih)
    256 		return (EIO);
    257 
    258 	error = pcmcia_function_enable(psc->sc_pf);
    259 	if (error) {
    260 		pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    261 		psc->sc_ih = 0;
    262 	}
    263 
    264 	return (error);
    265 }
    266 
    267 void
    268 sm_pcmcia_disable(sc)
    269 	struct smc91cxx_softc *sc;
    270 {
    271 	struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
    272 
    273 	pcmcia_function_disable(psc->sc_pf);
    274 	pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    275 	psc->sc_ih = 0;
    276 }
    277