Home | History | Annotate | Line # | Download | only in pcmcia
if_cs_pcmcia.c revision 1.16
      1 /* $NetBSD: if_cs_pcmcia.c,v 1.16 2008/04/05 21:31:23 cegger Exp $ */
      2 
      3 /*-
      4  * Copyright (c)2001 YAMAMOTO Takashi,
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: if_cs_pcmcia.c,v 1.16 2008/04/05 21:31:23 cegger Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/systm.h>
     34 #include <sys/device.h>
     35 #include <sys/socket.h>
     36 #include <sys/queue.h>
     37 
     38 #include "rnd.h"
     39 #if NRND > 0
     40 #include <sys/rnd.h>
     41 #endif
     42 
     43 #include <net/if.h>
     44 #include <net/if_ether.h>
     45 #include <net/if_media.h>
     46 
     47 #include <sys/bus.h>
     48 #include <sys/intr.h>
     49 
     50 #include <dev/pcmcia/pcmciareg.h>
     51 #include <dev/pcmcia/pcmciavar.h>
     52 #include <dev/pcmcia/pcmciadevs.h>
     53 
     54 #include <dev/ic/cs89x0reg.h>
     55 #include <dev/ic/cs89x0var.h>
     56 
     57 struct cs_pcmcia_softc;
     58 
     59 static int cs_pcmcia_match(struct device *, struct cfdata *, void *);
     60 static int cs_pcmcia_validate_config(struct pcmcia_config_entry *);
     61 static void cs_pcmcia_attach(struct device *, struct device *, void *);
     62 static int cs_pcmcia_detach(struct device *, int);
     63 static int cs_pcmcia_enable(struct cs_softc *);
     64 static void cs_pcmcia_disable(struct cs_softc *);
     65 
     66 struct cs_pcmcia_softc {
     67 	struct cs_softc sc_cs; /* real "cs" softc */
     68 
     69 	struct pcmcia_function *sc_pf;
     70 
     71 	int sc_state;
     72 #define	CS_PCMCIA_ATTACHED	3
     73 };
     74 
     75 CFATTACH_DECL(cs_pcmcia, sizeof(struct cs_pcmcia_softc),
     76     cs_pcmcia_match, cs_pcmcia_attach, cs_pcmcia_detach, cs_activate);
     77 
     78 static int
     79 cs_pcmcia_match(struct device *parent, struct cfdata *match,
     80     void *aux)
     81 {
     82 	struct pcmcia_attach_args *pa = aux;
     83 
     84 	if (pa->manufacturer == PCMCIA_VENDOR_IBM &&
     85 	    pa->product == PCMCIA_PRODUCT_IBM_ETHERJET)
     86 		return (1);
     87 	return (0);
     88 }
     89 
     90 static int
     91 cs_pcmcia_validate_config(struct pcmcia_config_entry *cfe)
     92 {
     93 	if (cfe->iftype != PCMCIA_IFTYPE_IO ||
     94 	    cfe->num_memspace != 0 ||
     95 	    cfe->num_iospace != 1 ||
     96 	    cfe->iospace[0].length < CS8900_IOSIZE)
     97 		return (EINVAL);
     98 	return (0);
     99 }
    100 
    101 static void
    102 cs_pcmcia_attach(struct device *parent, struct device *self, void *aux)
    103 {
    104 	struct cs_pcmcia_softc *psc = (void *)self;
    105 	struct cs_softc *sc = (void *)&psc->sc_cs;
    106 	struct pcmcia_attach_args *pa = aux;
    107 	struct pcmcia_config_entry *cfe;
    108 	struct pcmcia_function *pf;
    109 	int error;
    110 
    111 	pf = psc->sc_pf = pa->pf;
    112 
    113 	error = pcmcia_function_configure(pa->pf, cs_pcmcia_validate_config);
    114 	if (error) {
    115 		aprint_error_dev(self, "configure failed, error=%d\n",
    116 		    error);
    117 		return;
    118 	}
    119 
    120 	cfe = pf->cfe;
    121 	sc->sc_iot = cfe->iospace[0].handle.iot;
    122 	sc->sc_ioh = cfe->iospace[0].handle.ioh;
    123 	sc->sc_irq = -1;
    124 #define CS_PCMCIA_HACK_FOR_CARDBUS
    125 #ifdef CS_PCMCIA_HACK_FOR_CARDBUS
    126 	/*
    127 	 * XXX is there a generic way to know if it's a cardbus or not?
    128 	 */
    129 	sc->sc_cfgflags |= CFGFLG_CARDBUS_HACK;
    130 #endif
    131 
    132 	error = cs_pcmcia_enable(sc);
    133 	if (error)
    134 		goto fail;
    135 
    136 	sc->sc_enable = cs_pcmcia_enable;
    137 	sc->sc_disable = cs_pcmcia_disable;
    138 
    139 	/* chip attach */
    140 	error = cs_attach(sc, 0, 0, 0, 0);
    141 	if (error)
    142 		goto fail2;
    143 
    144 	cs_pcmcia_disable(sc);
    145 	psc->sc_state = CS_PCMCIA_ATTACHED;
    146 	return;
    147 
    148 fail2:
    149 	cs_pcmcia_disable(sc);
    150 fail:
    151 	pcmcia_function_unconfigure(pf);
    152 }
    153 
    154 static int
    155 cs_pcmcia_detach(struct device *self, int flags)
    156 {
    157 	struct cs_pcmcia_softc *psc = (void *)self;
    158 	struct cs_softc *sc = &psc->sc_cs;
    159 	int error;
    160 
    161 	if (psc->sc_state != CS_PCMCIA_ATTACHED)
    162 		return (0);
    163 
    164 	error = cs_detach(sc);
    165 	if (error)
    166 		return (error);
    167 
    168 	pcmcia_function_unconfigure(psc->sc_pf);
    169 
    170 	return (0);
    171 }
    172 
    173 static int
    174 cs_pcmcia_enable(struct cs_softc *sc)
    175 {
    176 	struct cs_pcmcia_softc *psc = (void *)sc;
    177 	int error;
    178 
    179 	sc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, cs_intr, sc);
    180 	if (!sc->sc_ih)
    181 		return (EIO);
    182 
    183 	error = pcmcia_function_enable(psc->sc_pf);
    184 	if (error) {
    185 		pcmcia_intr_disestablish(psc->sc_pf, sc->sc_ih);
    186 		sc->sc_ih = 0;
    187 	}
    188 
    189 	return (error);
    190 }
    191 
    192 static void
    193 cs_pcmcia_disable(struct cs_softc *sc)
    194 {
    195 	struct cs_pcmcia_softc *psc = (void *)sc;
    196 
    197 	pcmcia_function_disable(psc->sc_pf);
    198 	pcmcia_intr_disestablish(psc->sc_pf, sc->sc_ih);
    199 	sc->sc_ih = 0;
    200 }
    201