Home | History | Annotate | Line # | Download | only in pcmcia
if_ep_pcmcia.c revision 1.1.2.13
      1 /*	$NetBSD: if_ep_pcmcia.c,v 1.1.2.13 1997/10/16 19:45:37 thorpej Exp $	*/
      2 
      3 #include "bpfilter.h"
      4 
      5 #include <sys/param.h>
      6 #include <sys/systm.h>
      7 #include <sys/mbuf.h>
      8 #include <sys/socket.h>
      9 #include <sys/ioctl.h>
     10 #include <sys/errno.h>
     11 #include <sys/syslog.h>
     12 #include <sys/select.h>
     13 #include <sys/device.h>
     14 
     15 #include <net/if.h>
     16 #include <net/if_dl.h>
     17 #include <net/if_ether.h>
     18 #include <net/if_media.h>
     19 
     20 #ifdef INET
     21 #include <netinet/in.h>
     22 #include <netinet/in_systm.h>
     23 #include <netinet/in_var.h>
     24 #include <netinet/ip.h>
     25 #include <netinet/if_inarp.h>
     26 #endif
     27 
     28 #ifdef NS
     29 #include <netns/ns.h>
     30 #include <netns/ns_if.h>
     31 #endif
     32 
     33 #if NBPFILTER > 0
     34 #include <net/bpf.h>
     35 #include <net/bpfdesc.h>
     36 #endif
     37 
     38 #include <machine/cpu.h>
     39 #include <machine/bus.h>
     40 #include <machine/intr.h>
     41 
     42 #include <dev/ic/elink3var.h>
     43 #include <dev/ic/elink3reg.h>
     44 
     45 #include <dev/pcmcia/pcmciareg.h>
     46 #include <dev/pcmcia/pcmciavar.h>
     47 
     48 #define	PCMCIA_MANUFACTURER_3COM	0x101
     49 #define	PCMCIA_PRODUCT_3COM_3C562	0x562
     50 #define	PCMCIA_PRODUCT_3COM_3C589	0x589
     51 
     52 #ifdef __BROKEN_INDIRECT_CONFIG
     53 int	ep_pcmcia_match __P((struct device *, void *, void *));
     54 #else
     55 int	ep_pcmcia_match __P((struct device *, struct cfdata *, void *));
     56 #endif
     57 void	ep_pcmcia_attach __P((struct device *, struct device *, void *));
     58 
     59 int	ep_pcmcia_get_enaddr __P((struct pcmcia_tuple *, void *));
     60 int	ep_pcmcia_enable __P((struct ep_softc *));
     61 void	ep_pcmcia_disable __P((struct ep_softc *));
     62 
     63 struct ep_pcmcia_softc {
     64 	struct ep_softc sc_ep;			/* real "ep" softc */
     65 
     66 	/* PCMCIA-specific goo */
     67 	struct pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
     68 	int sc_io_window;			/* our i/o window */
     69 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
     70 };
     71 
     72 struct cfattach ep_pcmcia_ca = {
     73 	sizeof(struct ep_pcmcia_softc), ep_pcmcia_match, ep_pcmcia_attach
     74 };
     75 
     76 int
     77 ep_pcmcia_match(parent, match, aux)
     78 	struct device *parent;
     79 #ifdef __BROKEN_INDIRECT_CONFIG
     80 	void *match;
     81 #else
     82 	struct cfdata *cf;
     83 #endif
     84 	void *aux;
     85 {
     86 	struct pcmcia_attach_args *pa = aux;
     87 
     88 	if (pa->manufacturer == PCMCIA_MANUFACTURER_3COM) {
     89 		switch (pa->product) {
     90 		case PCMCIA_PRODUCT_3COM_3C562:
     91 		case PCMCIA_PRODUCT_3COM_3C589:
     92 			if (pa->pf->number == 0)
     93 				return (1);
     94 		}
     95 	}
     96 
     97 	return (0);
     98 }
     99 
    100 int
    101 ep_pcmcia_enable(sc)
    102 	struct ep_softc *sc;
    103 {
    104 	struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
    105 
    106 	/* establish the interrupt. */
    107 	sc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, epintr, sc);
    108 	if (sc->sc_ih == NULL) {
    109 		printf("%s: couldn't establish interrupt\n",
    110 		    sc->sc_dev.dv_xname);
    111 		return (1);
    112 	}
    113 
    114 	return (pcmcia_function_enable(psc->sc_pf));
    115 }
    116 
    117 void
    118 ep_pcmcia_disable(sc)
    119 	struct ep_softc *sc;
    120 {
    121 	struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
    122 
    123 	pcmcia_function_disable(psc->sc_pf);
    124 
    125 	pcmcia_intr_disestablish(psc->sc_pf, sc->sc_ih);
    126 }
    127 
    128 void
    129 ep_pcmcia_attach(parent, self, aux)
    130 	struct device  *parent, *self;
    131 	void           *aux;
    132 {
    133 	struct ep_pcmcia_softc *psc = (void *) self;
    134 	struct ep_softc *sc = &psc->sc_ep;
    135 	struct pcmcia_attach_args *pa = aux;
    136 	struct pcmcia_config_entry *cfe;
    137 	int i;
    138 	u_int8_t myla[ETHER_ADDR_LEN];
    139 	u_int8_t *enaddr;
    140 	char *model;
    141 
    142 	psc->sc_pf = pa->pf;
    143 	cfe = pa->pf->cfe_head.sqh_first;
    144 
    145 	/* Enable the card. */
    146 	pcmcia_function_init(pa->pf, cfe);
    147 	if (pcmcia_function_enable(pa->pf))
    148 		printf(": function enable failed\n");
    149 
    150 	sc->enabled = 1;
    151 	/* turn off the bit which disables the modem */
    152 	if (pa->product == PCMCIA_PRODUCT_3COM_3C562) {
    153 		int reg;
    154 
    155 		reg = pcmcia_ccr_read(pa->pf, PCMCIA_CCR_OPTION);
    156 		reg &= ~0x08;
    157 		pcmcia_ccr_write(pa->pf, PCMCIA_CCR_OPTION, reg);
    158 	}
    159 	if (cfe->num_memspace != 0)
    160 		printf(": unexpected number of memory spaces %d should be 0\n",
    161 		    cfe->num_memspace);
    162 
    163 	if (cfe->num_iospace != 1)
    164 		printf(": unexpected number of I/O spaces %d should be 1\n",
    165 		    cfe->num_iospace);
    166 
    167 	/*
    168 	 * XXX there's a comment in the linux driver about the io address
    169 	 * having to be between 0x00 and 0x70 mod 0x100.  weird.
    170 	 */
    171 
    172 	if (pa->product == PCMCIA_PRODUCT_3COM_3C562) {
    173 		for (i = roundup(pa->pf->sc->iobase, 0x100); i < 0x1000;
    174 		    i += ((i % 0x100) == 0x70) ? 0x90 : 0x10) {
    175 			if (pcmcia_io_alloc(pa->pf, i, cfe->iospace[0].length,
    176 			    0, &psc->sc_pcioh) == 0)
    177 				break;
    178 		}
    179 		if (i == 0x1000) {
    180 			printf(": can't allocate i/o space\n");
    181 			return;
    182 		}
    183 	} else {
    184 		if (pcmcia_io_alloc(pa->pf, 0, cfe->iospace[0].length,
    185 		    cfe->iospace[0].length, &psc->sc_pcioh))
    186 			printf(": can't allocate i/o space\n");
    187 	}
    188 
    189 	sc->sc_iot = psc->sc_pcioh.iot;
    190 	sc->sc_ioh = psc->sc_pcioh.ioh;
    191 
    192 	if (pcmcia_io_map(pa->pf, ((cfe->flags & PCMCIA_CFE_IO16) ?
    193 	    PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_IO8), 0, cfe->iospace[0].length,
    194 	    &psc->sc_pcioh, &psc->sc_io_window)) {
    195 		printf(": can't map i/o space\n");
    196 		return;
    197 	}
    198 	if (pa->product == PCMCIA_PRODUCT_3COM_3C562) {
    199 		if (pcmcia_scan_cis(parent, ep_pcmcia_get_enaddr, myla)) {
    200 			printf(": can't read ethernet address from CIS\n");
    201 			return;
    202 		}
    203 		enaddr = myla;
    204 	} else {
    205 		enaddr = NULL;
    206 	}
    207 
    208 	sc->bustype = EP_BUS_PCMCIA;
    209 
    210 	switch (pa->product) {
    211 	case PCMCIA_PRODUCT_3COM_3C589:
    212 		model = "3Com 3C589 Ethernet";
    213 		break;
    214 	case PCMCIA_PRODUCT_3COM_3C562:
    215 		model = "3Com 3C562 Ethernet";
    216 		break;
    217 	default:
    218 		model = "3Com Ethernet, model unknown";
    219 		break;
    220 	}
    221 
    222 	printf(": %s\n", model);
    223 
    224 	sc->enable = ep_pcmcia_enable;
    225 	sc->disable = ep_pcmcia_disable;
    226 
    227 	epconfig(sc, EP_CHIPSET_3C509, enaddr);
    228 
    229 	sc->enabled = 0;
    230 
    231 	pcmcia_function_disable(pa->pf);
    232 }
    233 
    234 int
    235 ep_pcmcia_get_enaddr(tuple, arg)
    236 	struct pcmcia_tuple *tuple;
    237 	void *arg;
    238 {
    239 	u_int8_t *myla = arg;
    240 	int i;
    241 
    242 	/* this is 3c562 magic */
    243 
    244 	if (tuple->code == 0x88) {
    245 		if (tuple->length < ETHER_ADDR_LEN)
    246 			return (0);
    247 
    248 		for (i = 0; i < ETHER_ADDR_LEN; i += 2) {
    249 			myla[i] = pcmcia_tuple_read_1(tuple, i + 1);
    250 			myla[i + 1] = pcmcia_tuple_read_1(tuple, i);
    251 		}
    252 
    253 		return (1);
    254 	}
    255 	return (0);
    256 }
    257