Home | History | Annotate | Line # | Download | only in pcmcia
if_an_pcmcia.c revision 1.3
      1 /* $NetBSD: if_an_pcmcia.c,v 1.3 2000/12/12 05:11:16 onoe Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Atsushi Onoe
      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 #include "opt_inet.h"
     40 #include "opt_ns.h"
     41 #include "bpfilter.h"
     42 
     43 #ifdef INET
     44 #define	ANCACHE		/* XXX: should be defined elsewhere */
     45 #endif
     46 
     47 #include <sys/param.h>
     48 #include <sys/systm.h>
     49 #include <sys/callout.h>
     50 #include <sys/mbuf.h>
     51 #include <sys/socket.h>
     52 #include <sys/ioctl.h>
     53 #include <sys/errno.h>
     54 #include <sys/syslog.h>
     55 #include <sys/select.h>
     56 #include <sys/device.h>
     57 
     58 #include <net/if.h>
     59 #include <net/if_dl.h>
     60 #include <net/if_ether.h>
     61 #include <net/if_media.h>
     62 #include <net/if_ieee80211.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/anreg.h>
     87 #include <dev/ic/anvar.h>
     88 
     89 #include <dev/pcmcia/pcmciareg.h>
     90 #include <dev/pcmcia/pcmciavar.h>
     91 #include <dev/pcmcia/pcmciadevs.h>
     92 
     93 static int an_pcmcia_match __P((struct device *, struct cfdata *, void *));
     94 static void an_pcmcia_attach __P((struct device *, struct device *, void *));
     95 static int an_pcmcia_detach __P((struct device *, int));
     96 static int an_pcmcia_enable __P((struct an_softc *));
     97 static void an_pcmcia_disable __P((struct an_softc *));
     98 static void an_pcmcia_powerhook __P((int, void *));
     99 
    100 struct an_pcmcia_softc {
    101 	struct an_softc sc_an;		/* real "an" softc */
    102 
    103 	/* PCMCIA-specific goo */
    104 	struct pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
    105 	int sc_io_window;			/* our i/o window */
    106 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
    107 	void *sc_powerhook;			/* power hook descriptor */
    108 };
    109 
    110 static int	an_pcmcia_find __P((struct an_pcmcia_softc *,
    111     struct pcmcia_attach_args *, struct pcmcia_config_entry *));
    112 
    113 struct cfattach an_pcmcia_ca = {
    114 	sizeof(struct an_pcmcia_softc), an_pcmcia_match, an_pcmcia_attach,
    115 	an_pcmcia_detach, an_activate
    116 };
    117 
    118 static struct an_pcmcia_product {
    119 	u_int32_t	app_vendor;	/* vendor ID */
    120 	u_int32_t	app_product;	/* product ID */
    121 	const char	*app_cisinfo[4]; /* CIS information */
    122 	const char	*app_name;	/* product name */
    123 } an_pcmcia_products[] = {
    124 	{ PCMCIA_VENDOR_AIRONET,	PCMCIA_PRODUCT_AIRONET_PC4800,
    125 	  PCMCIA_CIS_AIRONET_PC4800,	PCMCIA_STR_AIRONET_PC4800 },
    126 	{ 0,				0,
    127 	  { NULL, NULL, NULL, NULL },	NULL }
    128 };
    129 
    130 static struct an_pcmcia_product *
    131 	an_pcmcia_lookup __P((struct pcmcia_attach_args *));
    132 
    133 static struct an_pcmcia_product *
    134 an_pcmcia_lookup(pa)
    135 	struct pcmcia_attach_args *pa;
    136 {
    137 	struct an_pcmcia_product *app;
    138 
    139 	for (app = an_pcmcia_products; app->app_name != NULL; app++) {
    140 		/* match by vendor/product id */
    141 		if (pa->manufacturer != PCMCIA_VENDOR_INVALID &&
    142 		    pa->manufacturer == app->app_vendor &&
    143 		    pa->product != PCMCIA_PRODUCT_INVALID &&
    144 		    pa->product == app->app_product)
    145 			return (app);
    146 
    147 		/* match by CIS information */
    148 		if (pa->card->cis1_info[0] != NULL &&
    149 		    app->app_cisinfo[0] != NULL &&
    150 		    strcmp(pa->card->cis1_info[0], app->app_cisinfo[0]) == 0 &&
    151 		    pa->card->cis1_info[1] != NULL &&
    152 		    app->app_cisinfo[1] != NULL &&
    153 		    strcmp(pa->card->cis1_info[1], app->app_cisinfo[1]) == 0)
    154 			return (app);
    155 	}
    156 
    157 	return (NULL);
    158 }
    159 
    160 static int
    161 an_pcmcia_enable(sc)
    162 	struct an_softc *sc;
    163 {
    164 	struct an_pcmcia_softc *psc = (struct an_pcmcia_softc *)sc;
    165 	struct pcmcia_function *pf = psc->sc_pf;
    166 
    167 	/* establish the interrupt. */
    168 	sc->irq_handle = pcmcia_intr_establish(pf, IPL_NET, an_intr, sc);
    169 	if (sc->irq_handle == NULL) {
    170 		printf("%s: couldn't establish interrupt\n",
    171 		    sc->an_dev.dv_xname);
    172 		return (1);
    173 	}
    174 
    175 	if (pcmcia_function_enable(pf)) {
    176 		pcmcia_intr_disestablish(pf, sc->irq_handle);
    177 		return (1);
    178 	}
    179 	DELAY(1000);
    180 
    181 	return (0);
    182 }
    183 
    184 static void
    185 an_pcmcia_disable(sc)
    186 	struct an_softc *sc;
    187 {
    188 	struct an_pcmcia_softc *psc = (struct an_pcmcia_softc *)sc;
    189 	struct pcmcia_function *pf = psc->sc_pf;
    190 
    191 	pcmcia_function_disable(pf);
    192 	pcmcia_intr_disestablish(pf, sc->irq_handle);
    193 }
    194 
    195 static int
    196 an_pcmcia_match(parent, match, aux)
    197 	struct device *parent;
    198 	struct cfdata *match;
    199 	void *aux;
    200 {
    201 	struct pcmcia_attach_args *pa = aux;
    202 
    203 	if (an_pcmcia_lookup(pa) != NULL)
    204 		return (1);
    205 
    206 	return (0);
    207 }
    208 
    209 static int
    210 an_pcmcia_find(psc, pa, cfe)
    211 	struct an_pcmcia_softc *psc;
    212 	struct pcmcia_attach_args *pa;
    213 	struct pcmcia_config_entry *cfe;
    214 {
    215 	struct an_softc *sc = &psc->sc_an;
    216 	int fail = 0;
    217 
    218 	/*
    219 	 * see if we can read the firmware version sanely
    220 	 * through the i/o ports.
    221 	 * if not, try a different CIS string..
    222 	 */
    223 	if (pcmcia_io_alloc(psc->sc_pf, cfe->iospace[0].start,
    224 	    cfe->iospace[0].length, AN_IOSIZ,
    225 	    &psc->sc_pcioh) != 0)
    226 		goto fail;
    227 
    228 	if (pcmcia_io_map(psc->sc_pf, PCMCIA_WIDTH_AUTO, 0, psc->sc_pcioh.size,
    229 	    &psc->sc_pcioh, &psc->sc_io_window))
    230 		goto fail_io_free;
    231 
    232 	/* Enable the card. */
    233 	pcmcia_function_init(psc->sc_pf, cfe);
    234 	if (pcmcia_function_enable(psc->sc_pf))
    235 		goto fail_io_unmap;
    236 
    237 	sc->an_btag = psc->sc_pcioh.iot;
    238 	sc->an_bhandle = psc->sc_pcioh.ioh;
    239 
    240 	if (an_probe(sc))
    241 		return 0;
    242 
    243 	fail++;
    244 	pcmcia_function_disable(psc->sc_pf);
    245 
    246  fail_io_unmap:
    247 	fail++;
    248 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    249 
    250  fail_io_free:
    251 	fail++;
    252 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    253  fail:
    254 	fail++;
    255 	return (fail);
    256 }
    257 
    258 static void
    259 an_pcmcia_attach(parent, self, aux)
    260 	struct device  *parent, *self;
    261 	void           *aux;
    262 {
    263 	struct an_pcmcia_softc *psc = (void *)self;
    264 	struct an_softc *sc = &psc->sc_an;
    265 	struct an_pcmcia_product *app;
    266 	struct pcmcia_attach_args *pa = aux;
    267 	struct pcmcia_config_entry *cfe;
    268 
    269 	app = an_pcmcia_lookup(pa);
    270 	if (app == NULL)
    271 		panic("an_pcmcia_attach: impossible");
    272 
    273 	psc->sc_pf = pa->pf;
    274 
    275 	for (cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head); cfe != NULL;
    276 	     cfe = SIMPLEQ_NEXT(cfe, cfe_list)) {
    277 		if (cfe->iftype != PCMCIA_IFTYPE_IO)
    278 			continue;
    279 		if (cfe->num_iospace < 1)
    280 			continue;
    281 		if (cfe->iospace[0].length < AN_IOSIZ)
    282 			continue;
    283 
    284 		if (an_pcmcia_find(psc, pa, cfe) == 0)
    285 			break;
    286 	}
    287 	if (cfe == NULL) {
    288 		printf(": no suitable CIS info found\n");
    289 		goto no_config_entry;
    290 	}
    291 
    292 	sc->sc_enabled = 1;
    293 	printf(": %s\n", app->app_name);
    294 
    295 	sc->sc_enable = an_pcmcia_enable;
    296 	sc->sc_disable = an_pcmcia_disable;
    297 
    298 	/* establish the interrupt. */
    299 	sc->irq_handle = pcmcia_intr_establish(psc->sc_pf, IPL_NET, an_intr, sc);
    300 	if (sc->irq_handle == NULL) {
    301 		printf("%s: couldn't establish interrupt\n",
    302 		    sc->an_dev.dv_xname);
    303 		goto no_interrupt;
    304 	}
    305 
    306 	if (an_attach(sc) != 0) {
    307 		printf("%s: failed to attach controller\n",
    308 		    sc->an_dev.dv_xname);
    309 		goto attach_failed;
    310 	}
    311 	psc->sc_powerhook = powerhook_establish(an_pcmcia_powerhook, psc);
    312 	sc->sc_enabled = 0;
    313 
    314 	/* disable device and disestablish the interrupt */
    315 	an_pcmcia_disable(sc);
    316 	return;
    317 
    318  attach_failed:
    319 	pcmcia_intr_disestablish(psc->sc_pf, sc->irq_handle);
    320 
    321  no_interrupt:
    322 	/* Unmap our i/o window and space */
    323 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    324 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    325 
    326 	/* Disable the function */
    327 	pcmcia_function_disable(psc->sc_pf);
    328 
    329  no_config_entry:
    330 	psc->sc_io_window = -1;
    331 }
    332 
    333 
    334 static int
    335 an_pcmcia_detach(self, flags)
    336 	struct device *self;
    337 	int flags;
    338 {
    339 	struct an_pcmcia_softc *psc = (struct an_pcmcia_softc *)self;
    340 	int error;
    341 
    342 	if (psc->sc_io_window == -1)
    343 		/* Nothing to detach. */
    344 		return (0);
    345 
    346 	if (psc->sc_powerhook != NULL)
    347 		powerhook_disestablish(psc->sc_powerhook);
    348 
    349 	error = an_detach(&psc->sc_an);
    350 	if (error != 0)
    351 		return (error);
    352 
    353 	/* Unmap our i/o window. */
    354 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    355 
    356 	/* Free our i/o space. */
    357 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    358 	return (0);
    359 }
    360 
    361 static void
    362 an_pcmcia_powerhook(why, arg)
    363 	int why;
    364 	void *arg;
    365 {
    366 #if 0
    367 	struct an_pcmcia_softc *psc = arg;
    368 	struct an_softc *sc = &psc->sc_an;
    369 
    370 	an_power(sc, why);
    371 #endif
    372 }
    373