Home | History | Annotate | Line # | Download | only in pcmcia
if_sm_pcmcia.c revision 1.21
      1 /*	$NetBSD: if_sm_pcmcia.c,v 1.21 2000/02/09 13:40:42 enami 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 "opt_inet.h"
     41 #include "opt_ns.h"
     42 #include "bpfilter.h"
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/mbuf.h>
     47 #include <sys/socket.h>
     48 #include <sys/ioctl.h>
     49 #include <sys/errno.h>
     50 #include <sys/syslog.h>
     51 #include <sys/select.h>
     52 #include <sys/device.h>
     53 
     54 #include <net/if.h>
     55 #include <net/if_dl.h>
     56 #include <net/if_ether.h>
     57 #include <net/if_media.h>
     58 
     59 #ifdef INET
     60 #include <netinet/in.h>
     61 #include <netinet/in_systm.h>
     62 #include <netinet/in_var.h>
     63 #include <netinet/ip.h>
     64 #include <netinet/if_inarp.h>
     65 #endif
     66 
     67 #ifdef NS
     68 #include <netns/ns.h>
     69 #include <netns/ns_if.h>
     70 #endif
     71 
     72 #if NBPFILTER > 0
     73 #include <net/bpf.h>
     74 #include <net/bpfdesc.h>
     75 #endif
     76 
     77 #include <machine/intr.h>
     78 #include <machine/bus.h>
     79 
     80 #include <dev/ic/smc91cxxreg.h>
     81 #include <dev/ic/smc91cxxvar.h>
     82 
     83 #include <dev/pcmcia/pcmciareg.h>
     84 #include <dev/pcmcia/pcmciavar.h>
     85 #include <dev/pcmcia/pcmciadevs.h>
     86 
     87 int	sm_pcmcia_match __P((struct device *, struct cfdata *, void *));
     88 void	sm_pcmcia_attach __P((struct device *, struct device *, void *));
     89 int	sm_pcmcia_detach __P((struct device *, int));
     90 
     91 struct sm_pcmcia_softc {
     92 	struct	smc91cxx_softc sc_smc;		/* real "smc" softc */
     93 
     94 	/* PCMCIA-specific goo. */
     95 	struct	pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
     96 	int	sc_io_window;			/* our i/o window */
     97 	void	*sc_ih;				/* interrupt cookie */
     98 	struct	pcmcia_function *sc_pf;		/* our PCMCIA function */
     99 };
    100 
    101 struct cfattach sm_pcmcia_ca = {
    102 	sizeof(struct sm_pcmcia_softc), sm_pcmcia_match, sm_pcmcia_attach,
    103 	    sm_pcmcia_detach, smc91cxx_activate
    104 };
    105 
    106 int	sm_pcmcia_enable __P((struct smc91cxx_softc *));
    107 void	sm_pcmcia_disable __P((struct smc91cxx_softc *));
    108 
    109 int	sm_pcmcia_ascii_enaddr __P((const char *, u_int8_t *));
    110 int	sm_pcmcia_funce_enaddr __P((struct device *, u_int8_t *));
    111 
    112 int	sm_pcmcia_lannid_ciscallback __P((struct pcmcia_tuple *, void *));
    113 
    114 const struct pcmcia_product sm_pcmcia_products[] = {
    115 	{ PCMCIA_STR_MEGAHERTZ2_XJACK,		PCMCIA_VENDOR_MEGAHERTZ2,
    116 	  PCMCIA_PRODUCT_MEGAHERTZ2_XJACK,	 0, },
    117 
    118 	{ PCMCIA_STR_NEWMEDIA_BASICS,		PCMCIA_VENDOR_NEWMEDIA,
    119 	  PCMCIA_PRODUCT_NEWMEDIA_BASICS,	0, },
    120 
    121 #if 0
    122 	{ PCMCIA_STR_SMC_8020BT,		PCMCIA_VENDOR_SMC,
    123 	  PCMCIA_PRODUCT_SMC_8020BT,		0, },
    124 #endif
    125 
    126 	{ NULL }
    127 };
    128 
    129 int
    130 sm_pcmcia_match(parent, match, aux)
    131 	struct device *parent;
    132 	struct cfdata *match;
    133 	void *aux;
    134 {
    135 	struct pcmcia_attach_args *pa = aux;
    136 
    137 	if (pcmcia_product_lookup(pa, sm_pcmcia_products,
    138 	    sizeof sm_pcmcia_products[0], NULL) != NULL)
    139 		return (1);
    140 	return (0);
    141 }
    142 
    143 void
    144 sm_pcmcia_attach(parent, self, aux)
    145 	struct device *parent, *self;
    146 	void *aux;
    147 {
    148 	struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)self;
    149 	struct smc91cxx_softc *sc = &psc->sc_smc;
    150 	struct pcmcia_attach_args *pa = aux;
    151 	struct pcmcia_config_entry *cfe;
    152 	u_int8_t myla[ETHER_ADDR_LEN], *enaddr = NULL;
    153 	const struct pcmcia_product *pp;
    154 
    155 	psc->sc_pf = pa->pf;
    156 	cfe = pa->pf->cfe_head.sqh_first;
    157 
    158 	/* Enable the card. */
    159 	pcmcia_function_init(pa->pf, cfe);
    160 	if (pcmcia_function_enable(pa->pf)) {
    161 		printf(": function enable failed\n");
    162 		goto enable_failed;
    163 	}
    164 
    165 	/* XXX sanity check number of mem and i/o spaces */
    166 
    167 	/* Allocate and map i/o space for the card. */
    168 	if (pcmcia_io_alloc(pa->pf, 0, cfe->iospace[0].length,
    169 	    cfe->iospace[0].length, &psc->sc_pcioh)) {
    170 		printf(": can't allocate i/o space\n");
    171 		goto ioalloc_failed;
    172 	}
    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, (cfe->flags & PCMCIA_CFE_IO16) ?
    181 	    PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_IO8, 0, cfe->iospace[0].length,
    182 	    &psc->sc_pcioh, &psc->sc_io_window)) {
    183 		printf(": can't map i/o space\n");
    184 		goto iomap_failed;
    185 	}
    186 
    187 	pp = pcmcia_product_lookup(pa, sm_pcmcia_products,
    188 	    sizeof sm_pcmcia_products[0], NULL);
    189 	if (pp == NULL)
    190 		panic("sm_pcmcia_attach: impossible");
    191 
    192 	printf(": %s\n", pp->pp_name);
    193 
    194 	/*
    195 	 * First try to get the Ethernet address from FUNCE/LANNID tuple.
    196 	 */
    197 	if (sm_pcmcia_funce_enaddr(parent, myla))
    198 		enaddr = myla;
    199 
    200 	/*
    201 	 * If that failed, try one of the CIS info strings.
    202 	 */
    203 	if (enaddr == NULL) {
    204 		char *cisstr = NULL;
    205 
    206 		switch (pa->manufacturer) {
    207 		case PCMCIA_VENDOR_MEGAHERTZ:
    208 		case PCMCIA_VENDOR_MEGAHERTZ2:
    209 			cisstr = pa->pf->sc->card.cis1_info[3];
    210 			break;
    211 
    212 		case PCMCIA_VENDOR_SMC:
    213 			cisstr = pa->pf->sc->card.cis1_info[2];
    214 			break;
    215 		}
    216 
    217 		if (cisstr != NULL && sm_pcmcia_ascii_enaddr(cisstr, myla))
    218 			enaddr = myla;
    219 	}
    220 
    221 	if (enaddr == NULL)
    222 		printf("%s: unable to get Ethernet address\n",
    223 		    sc->sc_dev.dv_xname);
    224 
    225 	/* Perform generic intialization. */
    226 	smc91cxx_attach(sc, enaddr);
    227 
    228 	pcmcia_function_disable(pa->pf);
    229 	return;
    230 
    231  iomap_failed:
    232 	/* Disable the device */
    233 	pcmcia_function_disable(pa->pf);
    234 
    235  enable_failed:
    236 	/* Free our i/o space. */
    237 	pcmcia_io_free(pa->pf, &psc->sc_pcioh);
    238 
    239  ioalloc_failed:
    240 	psc->sc_io_window = -1;
    241 }
    242 
    243 int
    244 sm_pcmcia_detach(self, flags)
    245 	struct device *self;
    246 	int flags;
    247 {
    248 	struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)self;
    249 	int rv;
    250 
    251 	if (psc->sc_io_window == -1)
    252 		/* Nothing to detach. */
    253 		return (0);
    254 
    255 	rv = smc91cxx_detach((struct device *)&psc->sc_smc, flags);
    256 	if (rv != 0)
    257 		return (rv);
    258 
    259 	/* Unmap our i/o window. */
    260 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    261 
    262 	/* Free our i/o space. */
    263 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    264 	return (0);
    265 }
    266 
    267 int
    268 sm_pcmcia_ascii_enaddr(cisstr, myla)
    269 	const char *cisstr;
    270 	u_int8_t *myla;
    271 {
    272 	u_int8_t digit;
    273 	int i;
    274 
    275 	memset(myla, 0, ETHER_ADDR_LEN);
    276 
    277 	for (i = 0, digit = 0; i < (ETHER_ADDR_LEN * 2); i++) {
    278 		if (cisstr[i] >= '0' && cisstr[i] <= '9')
    279 			digit |= cisstr[i] - '0';
    280 		else if (cisstr[i] >= 'a' && cisstr[i] <= 'f')
    281 			digit |= (cisstr[i] - 'a') + 10;
    282 		else if (cisstr[i] >= 'A' && cisstr[i] <= 'F')
    283 			digit |= (cisstr[i] - 'A') + 10;
    284 		else {
    285 			/* Bogus digit!! */
    286 			return (0);
    287 		}
    288 
    289 		/* Compensate for ordering of digits. */
    290 		if (i & 1) {
    291 			myla[i >> 1] = digit;
    292 			digit = 0;
    293 		} else
    294 			digit <<= 4;
    295 	}
    296 
    297 	return (1);
    298 }
    299 
    300 int
    301 sm_pcmcia_funce_enaddr(parent, myla)
    302 	struct device *parent;
    303 	u_int8_t *myla;
    304 {
    305 
    306 	return (pcmcia_scan_cis(parent, sm_pcmcia_lannid_ciscallback, myla));
    307 }
    308 
    309 int
    310 sm_pcmcia_lannid_ciscallback(tuple, arg)
    311 	struct pcmcia_tuple *tuple;
    312 	void *arg;
    313 {
    314 	u_int8_t *myla = arg;
    315 	int i;
    316 
    317 	if (tuple->code == PCMCIA_CISTPL_FUNCE) {
    318 		/* subcode, length */
    319 		if (tuple->length < 2)
    320 			return (0);
    321 
    322 		if ((pcmcia_tuple_read_1(tuple, 0) !=
    323 		     PCMCIA_TPLFE_TYPE_LAN_NID) ||
    324 		    (pcmcia_tuple_read_1(tuple, 1) != ETHER_ADDR_LEN))
    325 			return (0);
    326 
    327 		for (i = 0; i < ETHER_ADDR_LEN; i++)
    328 			myla[i] = pcmcia_tuple_read_1(tuple, i + 2);
    329 		return (1);
    330 	}
    331 	return (0);
    332 }
    333 
    334 int
    335 sm_pcmcia_enable(sc)
    336 	struct smc91cxx_softc *sc;
    337 {
    338 	struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
    339 	int rv;
    340 
    341 	/* Establish the interrupt handler. */
    342 	psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, smc91cxx_intr,
    343 	    sc);
    344 	if (psc->sc_ih == NULL) {
    345 		printf("%s: couldn't establish interrupt handler\n",
    346 		    sc->sc_dev.dv_xname);
    347 		return (1);
    348 	}
    349 
    350 	rv = pcmcia_function_enable(psc->sc_pf);
    351 	if (rv != 0)
    352 		pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    353 	return (rv);
    354 }
    355 
    356 void
    357 sm_pcmcia_disable(sc)
    358 	struct smc91cxx_softc *sc;
    359 {
    360 	struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
    361 
    362 	pcmcia_function_disable(psc->sc_pf);
    363 	pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    364 }
    365