Home | History | Annotate | Line # | Download | only in pcmcia
if_sm_pcmcia.c revision 1.7
      1 /*	$NetBSD: if_sm_pcmcia.c,v 1.7 1998/08/14 23:31:22 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998 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 
     90 struct sm_pcmcia_softc {
     91 	struct	smc91cxx_softc sc_smc;		/* real "smc" softc */
     92 
     93 	/* PCMCIA-specific goo. */
     94 	struct	pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
     95 	int	sc_io_window;			/* our i/o window */
     96 	void	*sc_ih;				/* interrupt cookie */
     97 	struct	pcmcia_function *sc_pf;		/* our PCMCIA function */
     98 };
     99 
    100 struct cfattach sm_pcmcia_ca = {
    101 	sizeof(struct sm_pcmcia_softc), sm_pcmcia_match, sm_pcmcia_attach
    102 };
    103 
    104 int	sm_pcmcia_enable __P((struct smc91cxx_softc *));
    105 void	sm_pcmcia_disable __P((struct smc91cxx_softc *));
    106 
    107 int	sm_pcmcia_megahertz_enaddr __P((struct device *,
    108 	    struct pcmcia_attach_args *, u_int8_t *));
    109 int	sm_pcmcia_newmedia_enaddr __P((struct device *,
    110 	    struct pcmcia_attach_args *, u_int8_t *));
    111 
    112 int	sm_pcmcia_newmedia_ciscallback __P((struct pcmcia_tuple *, void *));
    113 
    114 struct sm_pcmcia_product {
    115 	u_int32_t	spp_vendor;	/* vendor ID */
    116 	u_int32_t	spp_product;	/* product ID */
    117 	int		spp_expfunc;	/* expected function */
    118 	const char	*spp_name;	/* product name */
    119 	int		(*spp_enaddr)	/* get ethernet address */
    120 			    __P((struct device *, struct pcmcia_attach_args *,
    121 			        u_int8_t *));
    122 } sm_pcmcia_products[] = {
    123 	{ PCMCIA_VENDOR_MEGAHERTZ2,	PCMCIA_PRODUCT_MEGAHERTZ2_XJACK,
    124 	  0,				PCMCIA_STR_MEGAHERTZ2_XJACK,
    125 	  sm_pcmcia_megahertz_enaddr },
    126 
    127 	{ PCMCIA_VENDOR_NEWMEDIA,	PCMCIA_PRODUCT_NEWMEDIA_BASICS,
    128 	  0,				PCMCIA_STR_NEWMEDIA_BASICS,
    129 	  sm_pcmcia_newmedia_enaddr },
    130 
    131 	{ 0,				0,
    132 	  0,				NULL,
    133 	  NULL },
    134 };
    135 
    136 struct sm_pcmcia_product *sm_pcmcia_lookup __P((struct pcmcia_attach_args *));
    137 
    138 struct sm_pcmcia_product *
    139 sm_pcmcia_lookup(pa)
    140 	struct pcmcia_attach_args *pa;
    141 {
    142 	struct sm_pcmcia_product *spp;
    143 
    144 	for (spp = sm_pcmcia_products; spp->spp_name != NULL; spp++)
    145 		if (pa->manufacturer == spp->spp_vendor &&
    146 		    pa->product == spp->spp_product &&
    147 		    pa->pf->number == spp->spp_expfunc)
    148 			return (spp);
    149 	return (NULL);
    150 }
    151 
    152 int
    153 sm_pcmcia_match(parent, match, aux)
    154 	struct device *parent;
    155 	struct cfdata *match;
    156 	void *aux;
    157 {
    158 	struct pcmcia_attach_args *pa = aux;
    159 
    160 	if (sm_pcmcia_lookup(pa) != NULL)
    161 		return (1);
    162 	return (0);
    163 }
    164 
    165 void
    166 sm_pcmcia_attach(parent, self, aux)
    167 	struct device *parent, *self;
    168 	void *aux;
    169 {
    170 	struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)self;
    171 	struct smc91cxx_softc *sc = &psc->sc_smc;
    172 	struct pcmcia_attach_args *pa = aux;
    173 	struct pcmcia_config_entry *cfe;
    174 	u_int8_t myla[ETHER_ADDR_LEN], *enaddr = NULL;
    175 	struct sm_pcmcia_product *spp;
    176 
    177 	psc->sc_pf = pa->pf;
    178 	cfe = pa->pf->cfe_head.sqh_first;
    179 
    180 	/* Enable the card. */
    181 	pcmcia_function_init(pa->pf, cfe);
    182 	if (pcmcia_function_enable(pa->pf)) {
    183 		printf(": function enable failed\n");
    184 		return;
    185 	}
    186 
    187 	/* XXX sanity check number of mem and i/o spaces */
    188 
    189 	/* Allocate and map i/o space for the card. */
    190 	if (pcmcia_io_alloc(pa->pf, 0, cfe->iospace[0].length,
    191 	    cfe->iospace[0].length, &psc->sc_pcioh)) {
    192 		printf(": can't allocate i/o space\n");
    193 		return;
    194 	}
    195 
    196 	sc->sc_bst = psc->sc_pcioh.iot;
    197 	sc->sc_bsh = psc->sc_pcioh.ioh;
    198 
    199 	sc->sc_enable = sm_pcmcia_enable;
    200 	sc->sc_disable = sm_pcmcia_disable;
    201 
    202 	if (pcmcia_io_map(pa->pf, (cfe->flags & PCMCIA_CFE_IO16) ?
    203 	    PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_IO8, 0, cfe->iospace[0].length,
    204 	    &psc->sc_pcioh, &psc->sc_io_window)) {
    205 		printf(": can't map i/o space\n");
    206 		return;
    207 	}
    208 
    209 	spp = sm_pcmcia_lookup(pa);
    210 	if (spp == NULL)
    211 		panic("sm_pcmcia_attach: impossible");
    212 
    213 	printf(": %s\n", spp->spp_name);
    214 
    215 	if ((*spp->spp_enaddr)(parent, pa, myla) == 0)
    216 		printf("%s: unable to get Ethernet address\n",
    217 		    sc->sc_dev.dv_xname);
    218 	else
    219 		enaddr = myla;
    220 
    221 	/* Perform generic intialization. */
    222 	smc91cxx_attach(sc, enaddr);
    223 
    224 	pcmcia_function_disable(pa->pf);
    225 }
    226 
    227 int
    228 sm_pcmcia_megahertz_enaddr(parent, pa, myla)
    229 	struct device *parent;
    230 	struct pcmcia_attach_args *pa;
    231 	u_int8_t *myla;
    232 {
    233 	char enaddr_str[12];
    234 	int i, j;
    235 
    236 	/*
    237 	 * The X-JACK's Ethernet address is stored in the fourth
    238 	 * CIS info string.  We need to parse it and pass it to
    239 	 * the generic layer.
    240 	 */
    241 	if (strlen(pa->pf->sc->card.cis1_info[3]) != 12) {
    242 		/* Bogus address! */
    243 		return (0);
    244 	}
    245 	bcopy(pa->pf->sc->card.cis1_info[3], enaddr_str, 12);
    246 	bzero(myla, sizeof(myla));
    247 	for (i = 0; i < 6; i++) {
    248 		for (j = 0; j < 2; j++) {
    249 			/* Convert to upper case. */
    250 			if (enaddr_str[(i * 2) + j] >= 'a' &&
    251 			    enaddr_str[(i * 2) + j] <= 'z')
    252 				enaddr_str[(i * 2) + j] -= 'a' - 'A';
    253 
    254 			/* Parse the digit. */
    255 			if (enaddr_str[(i * 2) + j] >= '0' &&
    256 			    enaddr_str[(i * 2) + j] <= '9')
    257 				myla[i] |= enaddr_str[(i * 2) + j]
    258 				    - '0';
    259 			else if (enaddr_str[(i * 2) + j] >= 'A' &&
    260 				 enaddr_str[(i * 2) + j] <= 'F')
    261 				myla[i] |= enaddr_str[(i * 2) + j]
    262 				    - 'A' + 10;
    263 			else {
    264 				/* Bogus digit!! */
    265 				return (0);
    266 			}
    267 
    268 			/* Compensate for ordering of digits. */
    269 			if (j == 0)
    270 				myla[i] <<= 4;
    271 		}
    272 	}
    273 
    274 	return (1);
    275 }
    276 
    277 int
    278 sm_pcmcia_newmedia_enaddr(parent, pa, myla)
    279 	struct device *parent;
    280 	struct pcmcia_attach_args *pa;
    281 	u_int8_t *myla;
    282 {
    283 
    284 	return (pcmcia_scan_cis(parent, sm_pcmcia_newmedia_ciscallback, myla));
    285 }
    286 
    287 int
    288 sm_pcmcia_newmedia_ciscallback(tuple, arg)
    289 	struct pcmcia_tuple *tuple;
    290 	void *arg;
    291 {
    292 	u_int8_t *myla = arg;
    293 	int i;
    294 
    295 	if (tuple->code == 0x22) {	/* Func Extension */
    296 		if (tuple->length < ETHER_ADDR_LEN)
    297 			return (0);
    298 		for (i = 0; i < ETHER_ADDR_LEN; i++)
    299 			myla[i] = pcmcia_tuple_read_1(tuple, i + 2);
    300 		return (1);
    301 	}
    302 	return (0);
    303 }
    304 
    305 int
    306 sm_pcmcia_enable(sc)
    307 	struct smc91cxx_softc *sc;
    308 {
    309 	struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
    310 
    311 	/* Establish the interrupt handler. */
    312 	psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, smc91cxx_intr,
    313 	    sc);
    314 	if (psc->sc_ih == NULL) {
    315 		printf("%s: couldn't establish interrupt handler\n",
    316 		    sc->sc_dev.dv_xname);
    317 		return (1);
    318 	}
    319 
    320 	return (pcmcia_function_enable(psc->sc_pf));
    321 }
    322 
    323 void
    324 sm_pcmcia_disable(sc)
    325 	struct smc91cxx_softc *sc;
    326 {
    327 	struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
    328 
    329 	pcmcia_function_disable(psc->sc_pf);
    330 
    331 	pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    332 }
    333