Home | History | Annotate | Line # | Download | only in pcmcia
if_awi_pcmcia.c revision 1.3
      1 /*-
      2  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      3  * All rights reserved.
      4  *
      5  * This code is derived from software contributed to The NetBSD Foundation
      6  * by Bill Sommerfeld
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *        This product includes software developed by the NetBSD
     19  *        Foundation, Inc. and its contributors.
     20  * 4. Neither the name of The NetBSD Foundation nor the names of its
     21  *    contributors may be used to endorse or promote products derived
     22  *    from this software without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     25  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     26  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     27  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     28  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     29  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     32  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     34  * POSSIBILITY OF SUCH DAMAGE.
     35  */
     36 
     37 /*
     38  * PCMCIA attachment for BayStack 650 802.11FH PCMCIA card,
     39  * based on the AMD 79c930 802.11 controller chip.
     40  *
     41  * This attachment can probably be trivally adapted for other FH and
     42  * DS cards based on the same chipset.
     43  */
     44 
     45 #include "opt_inet.h"
     46 #include "opt_ns.h"
     47 #include "bpfilter.h"
     48 
     49 #include <sys/param.h>
     50 #include <sys/systm.h>
     51 #include <sys/mbuf.h>
     52 #include <sys/socket.h>
     53 #include <sys/ioctl.h>
     54 #include <sys/errno.h>
     55 #include <sys/syslog.h>
     56 #include <sys/select.h>
     57 #include <sys/device.h>
     58 
     59 #include <net/if.h>
     60 #include <net/if_dl.h>
     61 #include <net/if_ether.h>
     62 #include <net/if_media.h>
     63 
     64 #ifdef INET
     65 #include <netinet/in.h>
     66 #include <netinet/in_systm.h>
     67 #include <netinet/in_var.h>
     68 #include <netinet/ip.h>
     69 #include <netinet/if_inarp.h>
     70 #endif
     71 
     72 #ifdef NS
     73 #include <netns/ns.h>
     74 #include <netns/ns_if.h>
     75 #endif
     76 
     77 #if NBPFILTER > 0
     78 #include <net/bpf.h>
     79 #include <net/bpfdesc.h>
     80 #endif
     81 
     82 #include <machine/cpu.h>
     83 #include <machine/bus.h>
     84 #include <machine/intr.h>
     85 
     86 #include <dev/ic/am79c930reg.h>
     87 #include <dev/ic/am79c930var.h>
     88 #include <dev/ic/awireg.h>
     89 #include <dev/ic/awivar.h>
     90 
     91 #include <dev/pcmcia/pcmciareg.h>
     92 #include <dev/pcmcia/pcmciavar.h>
     93 #include <dev/pcmcia/pcmciadevs.h>
     94 
     95 int	awi_pcmcia_match __P((struct device *, struct cfdata *, void *));
     96 void	awi_pcmcia_attach __P((struct device *, struct device *, void *));
     97 int	awi_pcmcia_detach __P((struct device *, int));
     98 
     99 int	awi_pcmcia_get_enaddr __P((struct pcmcia_tuple *, void *));
    100 int	awi_pcmcia_enable __P((struct awi_softc *));
    101 void	awi_pcmcia_disable __P((struct awi_softc *));
    102 
    103 struct awi_pcmcia_softc {
    104 	struct awi_softc sc_awi;			/* real "awi" softc */
    105 
    106 	/* PCMCIA-specific goo */
    107 	struct pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
    108 	int sc_io_window;			/* our i/o window */
    109 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
    110 };
    111 
    112 int	awi_pcmcia_find __P((struct awi_pcmcia_softc *,
    113     struct pcmcia_attach_args *, struct pcmcia_config_entry *));
    114 
    115 struct cfattach awi_pcmcia_ca = {
    116 	sizeof(struct awi_pcmcia_softc), awi_pcmcia_match, awi_pcmcia_attach,
    117 	awi_pcmcia_detach, /* awi_activate */ 0
    118 };
    119 
    120 #if __NetBSD_Version__ <= 104120000
    121 #define	PCMCIA_VENDOR_BAY	0x01eb	/* Bay Networks */
    122 #define	PCMCIA_PRODUCT_BAY_STACK_650	0x804
    123 #endif
    124 
    125 int
    126 awi_pcmcia_enable(sc)
    127 	struct awi_softc *sc;
    128 {
    129 	struct awi_pcmcia_softc *psc = (struct awi_pcmcia_softc *) sc;
    130 	struct pcmcia_function *pf = psc->sc_pf;
    131 
    132 	/* establish the interrupt. */
    133 	sc->sc_ih = pcmcia_intr_establish(pf, IPL_NET, awi_intr, sc);
    134 	if (sc->sc_ih == NULL) {
    135 		printf("%s: couldn't establish interrupt\n",
    136 		    sc->sc_dev.dv_xname);
    137 		return (1);
    138 	}
    139 	return (pcmcia_function_enable(pf));
    140 }
    141 
    142 void
    143 awi_pcmcia_disable(sc)
    144 	struct awi_softc *sc;
    145 {
    146 	struct awi_pcmcia_softc *psc = (struct awi_pcmcia_softc *) sc;
    147 	struct pcmcia_function *pf = psc->sc_pf;
    148 
    149 	pcmcia_function_disable (pf);
    150 	pcmcia_intr_disestablish (pf, sc->sc_ih);
    151 }
    152 
    153 int
    154 awi_pcmcia_match(parent, match, aux)
    155 	struct device *parent;
    156 	struct cfdata *match;
    157 	void *aux;
    158 {
    159 	struct pcmcia_attach_args *pa = aux;
    160 
    161 	if (pa->manufacturer != PCMCIA_VENDOR_BAY)
    162 		return (0);
    163 
    164 	if (pa->product == PCMCIA_PRODUCT_BAY_STACK_650)
    165 		return (1);
    166 
    167 	return (0);
    168 }
    169 
    170 int
    171 awi_pcmcia_find (psc, pa, cfe)
    172 	struct awi_pcmcia_softc *psc;
    173 	struct pcmcia_attach_args *pa;
    174 	struct pcmcia_config_entry *cfe;
    175 {
    176 	struct awi_softc *sc = &psc->sc_awi;
    177 	int fail = 0;
    178 	u_int8_t version[AWI_BANNER_LEN];
    179 
    180 	/*
    181 	 * see if we can read the firmware version sanely
    182 	 * through the i/o ports.
    183 	 * if not, try a different CIS string..
    184 	 */
    185 	if (pcmcia_io_alloc(psc->sc_pf, cfe->iospace[0].start,
    186 	    cfe->iospace[0].length, 0, &psc->sc_pcioh) != 0)
    187 		goto fail;
    188 
    189 	if (pcmcia_io_map(psc->sc_pf, PCMCIA_WIDTH_AUTO, 0, psc->sc_pcioh.size,
    190 	    &psc->sc_pcioh, &psc->sc_io_window))
    191 		goto fail_io_free;
    192 
    193 	/* Enable the card. */
    194 	pcmcia_function_init(psc->sc_pf, cfe);
    195 	if (pcmcia_function_enable(psc->sc_pf))
    196 		goto fail_io_unmap;
    197 
    198 	sc->sc_chip.sc_iot = psc->sc_pcioh.iot;
    199 	sc->sc_chip.sc_ioh = psc->sc_pcioh.ioh;
    200 	am79c930_chip_init(&sc->sc_chip, 0);
    201 
    202 	DELAY(100);
    203 
    204 	awi_read_bytes (sc, AWI_BANNER, version, AWI_BANNER_LEN);
    205 
    206 	if (memcmp(version, "PCnetMobile:", 12) == 0)
    207 		return 0;
    208 
    209 	fail++;
    210 	pcmcia_function_disable (psc->sc_pf);
    211 
    212  fail_io_unmap:
    213 	fail++;
    214 	pcmcia_io_unmap (psc->sc_pf, psc->sc_io_window);
    215 
    216  fail_io_free:
    217 	fail++;
    218 	pcmcia_io_free (psc->sc_pf, &psc->sc_pcioh);
    219  fail:
    220 	fail++;
    221 	return fail;
    222 }
    223 
    224 
    225 
    226 void
    227 awi_pcmcia_attach(parent, self, aux)
    228 	struct device  *parent, *self;
    229 	void           *aux;
    230 {
    231 	struct awi_pcmcia_softc *psc = (void *) self;
    232 	struct awi_softc *sc = &psc->sc_awi;
    233 	struct pcmcia_attach_args *pa = aux;
    234 	struct pcmcia_config_entry *cfe;
    235 	struct pcmcia_mem_handle memh;
    236 	bus_addr_t memoff;
    237 	int memwin;
    238 
    239 #if 0
    240 	int i, j;
    241 
    242 	for (cfe = pa->pf->cfe_head.sqh_first, i=0;
    243 	     cfe != NULL;
    244 	     cfe = cfe->cfe_list.sqe_next, i++) {
    245 		printf("%d: %d memspaces, %d iospaces\n",
    246 		    i, cfe->num_memspace, cfe->num_iospace);
    247 		printf("%d: number %d flags %x iftype %d iomask %lx irqmask %x maxtwins %x\n",
    248 		    i, cfe->number, cfe->flags, cfe->iftype, cfe->iomask,
    249 		    cfe->irqmask, cfe->maxtwins);
    250 		for (j=0; j<cfe->num_memspace; j++) {
    251 			printf("%d: mem %d: len %lx card %lx host %lx\n",
    252 			    i, j,
    253 			    cfe->memspace[j].length,
    254 			    cfe->memspace[j].cardaddr,
    255 			    cfe->memspace[j].hostaddr);
    256 		}
    257 		for (j=0; j<cfe->num_iospace; j++) {
    258 			printf("%d: io %d: len %lx start %lx\n",
    259 			    i, j,
    260 			    cfe->iospace[j].length,
    261 			    cfe->iospace[j].start);
    262 		}
    263 	}
    264 #endif
    265 
    266 	psc->sc_pf = pa->pf;
    267 
    268 	for (cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head); cfe != NULL;
    269 	     cfe = SIMPLEQ_NEXT(cfe, cfe_list)) {
    270 		if (cfe->iftype != PCMCIA_IFTYPE_IO)
    271 			continue;
    272 		if (cfe->num_iospace < 1)
    273 			continue;
    274 		if (cfe->iospace[0].length < AM79C930_IO_SIZE)
    275 			continue;
    276 
    277 		if (awi_pcmcia_find(psc, pa, cfe) == 0)
    278 			break;
    279 	}
    280 	if (cfe == NULL) {
    281 		printf(": no suitable CIS info found\n");
    282 		return;
    283 	}
    284 
    285 	sc->sc_enabled = 1;
    286 	sc->sc_state = AWI_ST_SELFTEST;
    287 	printf(": BayStack 650 Wireless (802.11)\n");
    288 
    289 	if (pcmcia_mem_alloc(psc->sc_pf, AM79C930_MEM_SIZE, &memh) != 0) {
    290 		printf("%s: unable to allocate memory space; using i/o only\n",
    291 		    sc->sc_dev.dv_xname);
    292 	} else if (pcmcia_mem_map(psc->sc_pf, PCMCIA_MEM_COMMON,
    293 	    AM79C930_MEM_BASE, AM79C930_MEM_SIZE,
    294 	    &memh, &memoff, &memwin)) {
    295 		printf("%s: unable to map memory space; using i/o only\n",
    296 		    sc->sc_dev.dv_xname);
    297 		pcmcia_mem_free(psc->sc_pf, &memh);
    298 	} else {
    299 		sc->sc_chip.sc_memt = memh.memt;
    300 		sc->sc_chip.sc_memh = memh.memh;
    301 		am79c930_chip_init(&sc->sc_chip, 1);
    302 	}
    303 
    304 	sc->sc_chip.sc_bustype = AM79C930_BUS_PCMCIA;
    305 
    306 	sc->sc_enable = awi_pcmcia_enable;
    307 	sc->sc_disable = awi_pcmcia_disable;
    308 
    309 	awi_attach(sc);
    310 
    311 	sc->sc_state = AWI_ST_OFF;
    312 
    313 	sc->sc_enabled = 0;
    314 	pcmcia_function_disable(psc->sc_pf);
    315 }
    316 
    317 
    318 int
    319 awi_pcmcia_detach(self, flags)
    320 	struct device *self;
    321 	int flags;
    322 {
    323 	struct awi_pcmcia_softc *psc = (struct awi_pcmcia_softc *)self;
    324 
    325 	/* Unmap our i/o window. */
    326 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    327 
    328 	/* Free our i/o space. */
    329 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    330 
    331 #ifdef notyet
    332 	/*
    333 	 * Our softc is about to go away, so drop our reference
    334 	 * to the ifnet.
    335 	 */
    336 	if_delref(psc->sc_awi.sc_ethercom.ec_if);
    337 	return (0);
    338 #else
    339 	return (EBUSY);
    340 #endif
    341 }
    342