Home | History | Annotate | Line # | Download | only in pcmcia
if_ep_pcmcia.c revision 1.3
      1 /*	$NetBSD: if_ep_pcmcia.c,v 1.3 1997/11/30 15:16:56 drochner 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 #ifdef __BROKEN_INDIRECT_CONFIG
     82 int	ep_pcmcia_match __P((struct device *, void *, void *));
     83 #else
     84 int	ep_pcmcia_match __P((struct device *, struct cfdata *, void *));
     85 #endif
     86 void	ep_pcmcia_attach __P((struct device *, struct device *, void *));
     87 
     88 int	ep_pcmcia_get_enaddr __P((struct pcmcia_tuple *, void *));
     89 int	ep_pcmcia_enable __P((struct ep_softc *));
     90 void	ep_pcmcia_disable __P((struct ep_softc *));
     91 
     92 struct ep_pcmcia_softc {
     93 	struct ep_softc sc_ep;			/* real "ep" softc */
     94 
     95 	/* PCMCIA-specific goo */
     96 	struct pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
     97 	int sc_io_window;			/* our i/o window */
     98 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
     99 };
    100 
    101 struct cfattach ep_pcmcia_ca = {
    102 	sizeof(struct ep_pcmcia_softc), ep_pcmcia_match, ep_pcmcia_attach
    103 };
    104 
    105 int
    106 ep_pcmcia_match(parent, match, aux)
    107 	struct device *parent;
    108 #ifdef __BROKEN_INDIRECT_CONFIG
    109 	void *match;
    110 #else
    111 	struct cfdata *match;
    112 #endif
    113 	void *aux;
    114 {
    115 	struct pcmcia_attach_args *pa = aux;
    116 
    117 	if (pa->manufacturer == PCMCIA_MANUFACTURER_3COM) {
    118 		switch (pa->product) {
    119 		case PCMCIA_PRODUCT_3COM_3C562:
    120 		case PCMCIA_PRODUCT_3COM_3C589:
    121 			if (pa->pf->number == 0)
    122 				return (1);
    123 		}
    124 	}
    125 
    126 	return (0);
    127 }
    128 
    129 int
    130 ep_pcmcia_enable(sc)
    131 	struct ep_softc *sc;
    132 {
    133 	struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
    134 
    135 	/* establish the interrupt. */
    136 	sc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, epintr, sc);
    137 	if (sc->sc_ih == NULL) {
    138 		printf("%s: couldn't establish interrupt\n",
    139 		    sc->sc_dev.dv_xname);
    140 		return (1);
    141 	}
    142 
    143 	return (pcmcia_function_enable(psc->sc_pf));
    144 }
    145 
    146 void
    147 ep_pcmcia_disable(sc)
    148 	struct ep_softc *sc;
    149 {
    150 	struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
    151 
    152 	pcmcia_function_disable(psc->sc_pf);
    153 
    154 	pcmcia_intr_disestablish(psc->sc_pf, sc->sc_ih);
    155 }
    156 
    157 void
    158 ep_pcmcia_attach(parent, self, aux)
    159 	struct device  *parent, *self;
    160 	void           *aux;
    161 {
    162 	struct ep_pcmcia_softc *psc = (void *) self;
    163 	struct ep_softc *sc = &psc->sc_ep;
    164 	struct pcmcia_attach_args *pa = aux;
    165 	struct pcmcia_config_entry *cfe;
    166 	int i;
    167 	u_int8_t myla[ETHER_ADDR_LEN];
    168 	u_int8_t *enaddr;
    169 	char *model;
    170 
    171 	psc->sc_pf = pa->pf;
    172 	cfe = pa->pf->cfe_head.sqh_first;
    173 
    174 	/* Enable the card. */
    175 	pcmcia_function_init(pa->pf, cfe);
    176 	if (pcmcia_function_enable(pa->pf))
    177 		printf(": function enable failed\n");
    178 
    179 	sc->enabled = 1;
    180 	/* turn off the bit which disables the modem */
    181 	if (pa->product == PCMCIA_PRODUCT_3COM_3C562) {
    182 		int reg;
    183 
    184 		reg = pcmcia_ccr_read(pa->pf, PCMCIA_CCR_OPTION);
    185 		reg &= ~0x08;
    186 		pcmcia_ccr_write(pa->pf, PCMCIA_CCR_OPTION, reg);
    187 	}
    188 	if (cfe->num_memspace != 0)
    189 		printf(": unexpected number of memory spaces %d should be 0\n",
    190 		    cfe->num_memspace);
    191 
    192 	if (cfe->num_iospace != 1)
    193 		printf(": unexpected number of I/O spaces %d should be 1\n",
    194 		    cfe->num_iospace);
    195 
    196 	/*
    197 	 * XXX there's a comment in the linux driver about the io address
    198 	 * having to be between 0x00 and 0x70 mod 0x100.  weird.
    199 	 */
    200 
    201 	if (pa->product == PCMCIA_PRODUCT_3COM_3C562) {
    202 		bus_addr_t maxaddr = (pa->pf->sc->iobase + pa->pf->sc->iosize);
    203 
    204 		for (i = roundup(pa->pf->sc->iobase, 0x100); i < maxaddr;
    205 		    i += ((i % 0x100) == 0x70) ? 0x90 : 0x10) {
    206 			if (pcmcia_io_alloc(pa->pf, i, cfe->iospace[0].length,
    207 			    0, &psc->sc_pcioh) == 0)
    208 				break;
    209 		}
    210 		if (i >= maxaddr) {
    211 			printf(": can't allocate i/o space\n");
    212 			return;
    213 		}
    214 	} else {
    215 		if (pcmcia_io_alloc(pa->pf, 0, cfe->iospace[0].length,
    216 		    cfe->iospace[0].length, &psc->sc_pcioh))
    217 			printf(": can't allocate i/o space\n");
    218 	}
    219 
    220 	sc->sc_iot = psc->sc_pcioh.iot;
    221 	sc->sc_ioh = psc->sc_pcioh.ioh;
    222 
    223 	if (pcmcia_io_map(pa->pf, ((cfe->flags & PCMCIA_CFE_IO16) ?
    224 	    PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_IO8), 0, cfe->iospace[0].length,
    225 	    &psc->sc_pcioh, &psc->sc_io_window)) {
    226 		printf(": can't map i/o space\n");
    227 		return;
    228 	}
    229 	if (pa->product == PCMCIA_PRODUCT_3COM_3C562) {
    230 		if (pcmcia_scan_cis(parent, ep_pcmcia_get_enaddr, myla)) {
    231 			printf(": can't read ethernet address from CIS\n");
    232 			return;
    233 		}
    234 		enaddr = myla;
    235 	} else {
    236 		enaddr = NULL;
    237 	}
    238 
    239 	sc->bustype = EP_BUS_PCMCIA;
    240 
    241 	switch (pa->product) {
    242 	case PCMCIA_PRODUCT_3COM_3C589:
    243 		model = "3Com 3C589 Ethernet";
    244 		break;
    245 	case PCMCIA_PRODUCT_3COM_3C562:
    246 		model = "3Com 3C562 Ethernet";
    247 		break;
    248 	default:
    249 		model = "3Com Ethernet, model unknown";
    250 		break;
    251 	}
    252 
    253 	printf(": %s\n", model);
    254 
    255 	sc->enable = ep_pcmcia_enable;
    256 	sc->disable = ep_pcmcia_disable;
    257 
    258 	epconfig(sc, EP_CHIPSET_3C509, enaddr);
    259 
    260 	sc->enabled = 0;
    261 
    262 	pcmcia_function_disable(pa->pf);
    263 }
    264 
    265 int
    266 ep_pcmcia_get_enaddr(tuple, arg)
    267 	struct pcmcia_tuple *tuple;
    268 	void *arg;
    269 {
    270 	u_int8_t *myla = arg;
    271 	int i;
    272 
    273 	/* this is 3c562 magic */
    274 
    275 	if (tuple->code == 0x88) {
    276 		if (tuple->length < ETHER_ADDR_LEN)
    277 			return (0);
    278 
    279 		for (i = 0; i < ETHER_ADDR_LEN; i += 2) {
    280 			myla[i] = pcmcia_tuple_read_1(tuple, i + 1);
    281 			myla[i + 1] = pcmcia_tuple_read_1(tuple, i);
    282 		}
    283 
    284 		return (1);
    285 	}
    286 	return (0);
    287 }
    288