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