Home | History | Annotate | Line # | Download | only in pcmcia
if_sm_pcmcia.c revision 1.2
      1 /*	$NetBSD: if_sm_pcmcia.c,v 1.2 1997/10/16 23:27:28 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997 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 "bpfilter.h"
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/mbuf.h>
     45 #include <sys/socket.h>
     46 #include <sys/ioctl.h>
     47 #include <sys/errno.h>
     48 #include <sys/syslog.h>
     49 #include <sys/select.h>
     50 #include <sys/device.h>
     51 
     52 #include <net/if.h>
     53 #include <net/if_dl.h>
     54 #include <net/if_ether.h>
     55 #include <net/if_media.h>
     56 
     57 #ifdef INET
     58 #include <netinet/in.h>
     59 #include <netinet/in_systm.h>
     60 #include <netinet/in_var.h>
     61 #include <netinet/ip.h>
     62 #include <netinet/if_inarp.h>
     63 #endif
     64 
     65 #ifdef NS
     66 #include <netns/ns.h>
     67 #include <netns/ns_if.h>
     68 #endif
     69 
     70 #if NBPFILTER > 0
     71 #include <net/bpf.h>
     72 #include <net/bpfdesc.h>
     73 #endif
     74 
     75 #include <machine/intr.h>
     76 #include <machine/bus.h>
     77 
     78 #include <dev/ic/smc91cxxreg.h>
     79 #include <dev/ic/smc91cxxvar.h>
     80 
     81 #include <dev/pcmcia/pcmciareg.h>
     82 #include <dev/pcmcia/pcmciavar.h>
     83 
     84 #define	PCMCIA_MANUFACTURER_MEGAHERTZ	0x128
     85 #define	PCMCIA_PRODUCT_MEGAHERTZ_XJACK	0x103
     86 
     87 #ifdef __BROKEN_INDIRECT_CONFIG
     88 int	sm_pcmcia_match __P((struct device *, void *, void *));
     89 #else
     90 int	sm_pcmcia_match __P((struct device *, struct cfdata *, void *));
     91 #endif
     92 void	sm_pcmcia_attach __P((struct device *, struct device *, void *));
     93 
     94 struct sm_pcmcia_softc {
     95 	struct	smc91cxx_softc sc_smc;		/* real "smc" softc */
     96 
     97 	/* PCMCIA-specific goo. */
     98 	struct	pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
     99 	int	sc_io_window;			/* our i/o window */
    100 	void	*sc_ih;				/* interrupt cookie */
    101 	struct	pcmcia_function *sc_pf;		/* our PCMCIA function */
    102 };
    103 
    104 struct cfattach sm_pcmcia_ca = {
    105 	sizeof(struct sm_pcmcia_softc), sm_pcmcia_match, sm_pcmcia_attach
    106 };
    107 
    108 int	sm_pcmcia_enable __P((struct smc91cxx_softc *));
    109 void	sm_pcmcia_disable __P((struct smc91cxx_softc *));
    110 
    111 int
    112 sm_pcmcia_match(parent, match, aux)
    113 	struct device *parent;
    114 #ifdef __BROKEN_INDIRECT_CONFIG
    115 	void *match;
    116 #else
    117 	struct cfdata *match;
    118 #endif
    119 	void *aux;
    120 {
    121 	struct pcmcia_attach_args *pa = aux;
    122 
    123 	if (pa->manufacturer == PCMCIA_MANUFACTURER_MEGAHERTZ) {
    124 		switch (pa->product) {
    125 		case PCMCIA_PRODUCT_MEGAHERTZ_XJACK:
    126 			if (pa->pf->number == 0)
    127 				return (1);
    128 		}
    129 	}
    130 
    131 	return (0);
    132 }
    133 
    134 void
    135 sm_pcmcia_attach(parent, self, aux)
    136 	struct device *parent, *self;
    137 	void *aux;
    138 {
    139 	struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)self;
    140 	struct smc91cxx_softc *sc = &psc->sc_smc;
    141 	struct pcmcia_attach_args *pa = aux;
    142 	struct pcmcia_config_entry *cfe;
    143 	u_int8_t myla[ETHER_ADDR_LEN], *enaddr = NULL;
    144 	const char *model;
    145 
    146 	psc->sc_pf = pa->pf;
    147 	cfe = pa->pf->cfe_head.sqh_first;
    148 
    149 	/* Enable the card. */
    150 	pcmcia_function_init(pa->pf, cfe);
    151 	if (pcmcia_function_enable(pa->pf)) {
    152 		printf(": function enable failed\n");
    153 		return;
    154 	}
    155 
    156 	/* XXX sanity check number of mem and i/o spaces */
    157 
    158 	/* Allocate and map i/o space for the card. */
    159 	if (pcmcia_io_alloc(pa->pf, 0, cfe->iospace[0].length,
    160 	    cfe->iospace[0].length, &psc->sc_pcioh)) {
    161 		printf(": can't allocate i/o space\n");
    162 		return;
    163 	}
    164 
    165 	sc->sc_bst = psc->sc_pcioh.iot;
    166 	sc->sc_bsh = psc->sc_pcioh.ioh;
    167 
    168 	sc->sc_enable = sm_pcmcia_enable;
    169 	sc->sc_disable = sm_pcmcia_disable;
    170 
    171 	if (pcmcia_io_map(pa->pf, (cfe->flags & PCMCIA_CFE_IO16) ?
    172 	    PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_IO8, 0, cfe->iospace[0].length,
    173 	    &psc->sc_pcioh, &psc->sc_io_window)) {
    174 		printf(": can't map i/o space\n");
    175 		return;
    176 	}
    177 
    178 	switch (pa->product) {
    179 	case PCMCIA_PRODUCT_MEGAHERTZ_XJACK:
    180 		model = "Megahertz X-JACK Ethernet";
    181 		break;
    182 
    183 	default:
    184 		model = "Unknown SMC91Cxx Ethernet";
    185 	}
    186 	printf(": %s\n", model);
    187 
    188 	if (pa->product == PCMCIA_PRODUCT_MEGAHERTZ_XJACK) {
    189 		char enaddr_str[12];
    190 		int i = 0, j;
    191 
    192 		/*
    193 		 * The X-JACK's Ethernet address is stored in the fourth
    194 		 * CIS info string.  We need to parse it and pass it to
    195 		 * the generic layer.
    196 		 */
    197 		if (strlen(pa->pf->sc->card.cis1_info[3]) != 12) {
    198 			/* Bogus address! */
    199 			goto out;
    200 		}
    201 		bcopy(pa->pf->sc->card.cis1_info[3], enaddr_str, 12);
    202 		bzero(myla, sizeof(myla));
    203 		for (i = 0; i < 6; i++) {
    204 			for (j = 0; j < 2; j++) {
    205 				/* Convert to upper case. */
    206 				if (enaddr_str[(i * 2) + j] >= 'a' &&
    207 				    enaddr_str[(i * 2) + j] <= 'z')
    208 					enaddr_str[(i * 2) + j] -= 'a' - 'A';
    209 
    210 				/* Parse the digit. */
    211 				if (enaddr_str[(i * 2) + j] >= '0' &&
    212 				    enaddr_str[(i * 2) + j] <= '9')
    213 					myla[i] |= enaddr_str[(i * 2) + j]
    214 					    - '0';
    215 				else if (enaddr_str[(i * 2) + j] >= 'A' &&
    216 					 enaddr_str[(i * 2) + j] <= 'F')
    217 					myla[i] |= enaddr_str[(i * 2) + j]
    218 					    - 'A' + 10;
    219 				else {
    220 					/* Bogus digit!! */
    221 					goto out;
    222 				}
    223 
    224 				/* Compensate for ordering of digits. */
    225 				if (j == 0)
    226 					myla[i] <<= 4;
    227 			}
    228 		}
    229 
    230  out:
    231 		if (i >= 6) {
    232 			/* Successfully parsed. */
    233 			enaddr = myla;
    234 		} else {
    235 			printf("%s: unable to read MAC address from CIS\n",
    236 			    sc->sc_dev.dv_xname);
    237 		}
    238 	}
    239 
    240 	/* Perform generic intialization. */
    241 	smc91cxx_attach(sc, enaddr);
    242 
    243 	pcmcia_function_disable(pa->pf);
    244 }
    245 
    246 int
    247 sm_pcmcia_enable(sc)
    248 	struct smc91cxx_softc *sc;
    249 {
    250 	struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
    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 == NULL) {
    256 		printf("%s: couldn't establish interrupt handler\n",
    257 		    sc->sc_dev.dv_xname);
    258 		return (1);
    259 	}
    260 
    261 	return (pcmcia_function_enable(psc->sc_pf));
    262 }
    263 
    264 void
    265 sm_pcmcia_disable(sc)
    266 	struct smc91cxx_softc *sc;
    267 {
    268 	struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
    269 
    270 	pcmcia_function_disable(psc->sc_pf);
    271 
    272 	pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    273 }
    274