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