Home | History | Annotate | Line # | Download | only in pcmcia
if_ep_pcmcia.c revision 1.8
      1 /*	$NetBSD: if_ep_pcmcia.c,v 1.8 1998/06/09 07:32:55 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997 Marc Horowitz.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by Marc Horowitz.
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include "bpfilter.h"
     33 
     34 #include <sys/param.h>
     35 #include <sys/systm.h>
     36 #include <sys/mbuf.h>
     37 #include <sys/socket.h>
     38 #include <sys/ioctl.h>
     39 #include <sys/errno.h>
     40 #include <sys/syslog.h>
     41 #include <sys/select.h>
     42 #include <sys/device.h>
     43 
     44 #include <net/if.h>
     45 #include <net/if_dl.h>
     46 #include <net/if_ether.h>
     47 #include <net/if_media.h>
     48 
     49 #ifdef INET
     50 #include <netinet/in.h>
     51 #include <netinet/in_systm.h>
     52 #include <netinet/in_var.h>
     53 #include <netinet/ip.h>
     54 #include <netinet/if_inarp.h>
     55 #endif
     56 
     57 #ifdef NS
     58 #include <netns/ns.h>
     59 #include <netns/ns_if.h>
     60 #endif
     61 
     62 #if NBPFILTER > 0
     63 #include <net/bpf.h>
     64 #include <net/bpfdesc.h>
     65 #endif
     66 
     67 #include <machine/cpu.h>
     68 #include <machine/bus.h>
     69 #include <machine/intr.h>
     70 
     71 #include <dev/ic/elink3var.h>
     72 #include <dev/ic/elink3reg.h>
     73 
     74 #include <dev/pcmcia/pcmciareg.h>
     75 #include <dev/pcmcia/pcmciavar.h>
     76 
     77 #define	PCMCIA_MANUFACTURER_3COM	0x101
     78 #define	PCMCIA_PRODUCT_3COM_3C562	0x562
     79 #define	PCMCIA_PRODUCT_3COM_3C589	0x589
     80 
     81 int	ep_pcmcia_match __P((struct device *, struct cfdata *, void *));
     82 void	ep_pcmcia_attach __P((struct device *, struct device *, void *));
     83 
     84 int	ep_pcmcia_get_enaddr __P((struct pcmcia_tuple *, void *));
     85 int	ep_pcmcia_enable __P((struct ep_softc *));
     86 void	ep_pcmcia_disable __P((struct ep_softc *));
     87 
     88 int	ep_pcmcia_enable1 __P((struct ep_softc *));
     89 void	ep_pcmcia_disable1 __P((struct ep_softc *));
     90 
     91 struct ep_pcmcia_softc {
     92 	struct ep_softc sc_ep;			/* real "ep" softc */
     93 
     94 	/* PCMCIA-specific goo */
     95 	struct pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
     96 	int sc_io_window;			/* our i/o window */
     97 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
     98 };
     99 
    100 struct cfattach ep_pcmcia_ca = {
    101 	sizeof(struct ep_pcmcia_softc), ep_pcmcia_match, ep_pcmcia_attach
    102 };
    103 
    104 int
    105 ep_pcmcia_match(parent, match, aux)
    106 	struct device *parent;
    107 	struct cfdata *match;
    108 	void *aux;
    109 {
    110 	struct pcmcia_attach_args *pa = aux;
    111 
    112 	if (pa->manufacturer == PCMCIA_MANUFACTURER_3COM) {
    113 		switch (pa->product) {
    114 		case PCMCIA_PRODUCT_3COM_3C562:
    115 		case PCMCIA_PRODUCT_3COM_3C589:
    116 			if (pa->pf->number == 0)
    117 				return (1);
    118 		}
    119 	}
    120 
    121 	return (0);
    122 }
    123 
    124 int
    125 ep_pcmcia_enable(sc)
    126 	struct ep_softc *sc;
    127 {
    128 	struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
    129 	struct pcmcia_function *pf = psc->sc_pf;
    130 
    131 	/* establish the interrupt. */
    132 	sc->sc_ih = pcmcia_intr_establish(pf, IPL_NET, epintr, sc);
    133 	if (sc->sc_ih == NULL) {
    134 		printf("%s: couldn't establish interrupt\n",
    135 		    sc->sc_dev.dv_xname);
    136 		return (1);
    137 	}
    138 
    139 	return (ep_pcmcia_enable1(sc));
    140 }
    141 
    142 int
    143 ep_pcmcia_enable1(sc)
    144 	struct ep_softc *sc;
    145 {
    146 	struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
    147 	struct pcmcia_function *pf = psc->sc_pf;
    148 	int ret;
    149 
    150 	if ((ret = pcmcia_function_enable(pf)))
    151 		return (ret);
    152 
    153 	if (psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3C562) {
    154 		int reg;
    155 
    156 		/* turn off the serial-disable bit */
    157 
    158 		reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
    159 		if (reg & 0x08) {
    160 			reg &= ~0x08;
    161 			pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
    162 		}
    163 
    164 	}
    165 
    166 	return (ret);
    167 }
    168 
    169 void
    170 ep_pcmcia_disable(sc)
    171 	struct ep_softc *sc;
    172 {
    173 	struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
    174 
    175 	ep_pcmcia_disable1(sc);
    176 	pcmcia_intr_disestablish(psc->sc_pf, sc->sc_ih);
    177 }
    178 
    179 void
    180 ep_pcmcia_disable1(sc)
    181 	struct ep_softc *sc;
    182 {
    183 	struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
    184 
    185 	pcmcia_function_disable(psc->sc_pf);
    186 }
    187 
    188 void
    189 ep_pcmcia_attach(parent, self, aux)
    190 	struct device  *parent, *self;
    191 	void           *aux;
    192 {
    193 	struct ep_pcmcia_softc *psc = (void *) self;
    194 	struct ep_softc *sc = &psc->sc_ep;
    195 	struct pcmcia_attach_args *pa = aux;
    196 	struct pcmcia_config_entry *cfe;
    197 	int i;
    198 	u_int8_t myla[ETHER_ADDR_LEN];
    199 	u_int8_t *enaddr;
    200 	char *model;
    201 
    202 	psc->sc_pf = pa->pf;
    203 	cfe = pa->pf->cfe_head.sqh_first;
    204 
    205 	/* Enable the card. */
    206 	pcmcia_function_init(pa->pf, cfe);
    207 	if (ep_pcmcia_enable1(sc))
    208 		printf(": function enable failed\n");
    209 
    210 	sc->enabled = 1;
    211 
    212 	if (cfe->num_memspace != 0)
    213 		printf(": unexpected number of memory spaces %d should be 0\n",
    214 		    cfe->num_memspace);
    215 
    216 	if (cfe->num_iospace != 1)
    217 		printf(": unexpected number of I/O spaces %d should be 1\n",
    218 		    cfe->num_iospace);
    219 
    220 	if (pa->product == PCMCIA_PRODUCT_3COM_3C562) {
    221 		bus_addr_t maxaddr = (pa->pf->sc->iobase + pa->pf->sc->iosize);
    222 
    223 		for (i = pa->pf->sc->iobase; i < maxaddr; i += 0x10) {
    224 			/*
    225 			 * the 3c562 can only use 0x??00-0x??7f
    226 			 * according to the Linux driver
    227 			 */
    228 			if (i & 0x80)
    229 				continue;
    230 			if (pcmcia_io_alloc(pa->pf, i, cfe->iospace[0].length,
    231 			    0, &psc->sc_pcioh) == 0)
    232 				break;
    233 		}
    234 		if (i >= maxaddr) {
    235 			printf(": can't allocate i/o space\n");
    236 			return;
    237 		}
    238 	} else {
    239 		if (pcmcia_io_alloc(pa->pf, 0, cfe->iospace[0].length,
    240 		    cfe->iospace[0].length, &psc->sc_pcioh))
    241 			printf(": can't allocate i/o space\n");
    242 	}
    243 
    244 	sc->sc_iot = psc->sc_pcioh.iot;
    245 	sc->sc_ioh = psc->sc_pcioh.ioh;
    246 
    247 	if (pcmcia_io_map(pa->pf, ((cfe->flags & PCMCIA_CFE_IO16) ?
    248 	    PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_IO8), 0, cfe->iospace[0].length,
    249 	    &psc->sc_pcioh, &psc->sc_io_window)) {
    250 		printf(": can't map i/o space\n");
    251 		return;
    252 	}
    253 	if (pa->product == PCMCIA_PRODUCT_3COM_3C562) {
    254 		/*
    255 		 * 3c562a-c use this; 3c562d does it in the regular way.
    256 		 * we might want to check the revision and produce a warning
    257 		 * in the future.
    258 		 */
    259 		if (pcmcia_scan_cis(parent, ep_pcmcia_get_enaddr, myla) != 1)
    260 			enaddr = NULL;
    261 		else
    262 			enaddr = myla;
    263 	} else {
    264 		enaddr = NULL;
    265 	}
    266 
    267 	sc->bustype = EP_BUS_PCMCIA;
    268 
    269 	switch (pa->product) {
    270 	case PCMCIA_PRODUCT_3COM_3C589:
    271 		model = "3Com 3C589 Ethernet";
    272 		break;
    273 	case PCMCIA_PRODUCT_3COM_3C562:
    274 		model = "3Com 3C562 Ethernet";
    275 		break;
    276 	default:
    277 		model = "3Com Ethernet, model unknown";
    278 		break;
    279 	}
    280 
    281 	printf(": %s\n", model);
    282 
    283 	sc->enable = ep_pcmcia_enable;
    284 	sc->disable = ep_pcmcia_disable;
    285 
    286 	epconfig(sc, EP_CHIPSET_3C509, enaddr);
    287 
    288 	sc->enabled = 0;
    289 
    290 	ep_pcmcia_disable1(sc);
    291 }
    292 
    293 int
    294 ep_pcmcia_get_enaddr(tuple, arg)
    295 	struct pcmcia_tuple *tuple;
    296 	void *arg;
    297 {
    298 	u_int8_t *myla = arg;
    299 	int i;
    300 
    301 	/* this is 3c562a-c magic */
    302 	if (tuple->code == 0x88) {
    303 		if (tuple->length < ETHER_ADDR_LEN)
    304 			return (0);
    305 
    306 		for (i = 0; i < ETHER_ADDR_LEN; i += 2) {
    307 			myla[i] = pcmcia_tuple_read_1(tuple, i + 1);
    308 			myla[i + 1] = pcmcia_tuple_read_1(tuple, i);
    309 		}
    310 
    311 		return (1);
    312 	}
    313 	return (0);
    314 }
    315