1 /* $NetBSD: if_cs_pcmcia.c,v 1.21 2015/04/13 16:33:25 riastradh 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.21 2015/04/13 16:33:25 riastradh 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 <net/if.h> 39 #include <net/if_ether.h> 40 #include <net/if_media.h> 41 42 #include <sys/bus.h> 43 #include <sys/intr.h> 44 45 #include <dev/pcmcia/pcmciareg.h> 46 #include <dev/pcmcia/pcmciavar.h> 47 #include <dev/pcmcia/pcmciadevs.h> 48 49 #include <dev/ic/cs89x0reg.h> 50 #include <dev/ic/cs89x0var.h> 51 52 struct cs_pcmcia_softc; 53 54 static int cs_pcmcia_match(device_t, cfdata_t, void *); 55 static int cs_pcmcia_validate_config(struct pcmcia_config_entry *); 56 static void cs_pcmcia_attach(device_t, device_t, void *); 57 static int cs_pcmcia_detach(device_t, int); 58 static int cs_pcmcia_enable(struct cs_softc *); 59 static void cs_pcmcia_disable(struct cs_softc *); 60 61 struct cs_pcmcia_softc { 62 struct cs_softc sc_cs; /* real "cs" softc */ 63 64 struct pcmcia_function *sc_pf; 65 66 int sc_state; 67 #define CS_PCMCIA_ATTACHED 3 68 }; 69 70 CFATTACH_DECL_NEW(cs_pcmcia, sizeof(struct cs_pcmcia_softc), 71 cs_pcmcia_match, cs_pcmcia_attach, cs_pcmcia_detach, cs_activate); 72 73 static int 74 cs_pcmcia_match(device_t parent, cfdata_t match, 75 void *aux) 76 { 77 struct pcmcia_attach_args *pa = aux; 78 79 if (pa->manufacturer == PCMCIA_VENDOR_IBM && 80 pa->product == PCMCIA_PRODUCT_IBM_ETHERJET) 81 return (1); 82 return (0); 83 } 84 85 static int 86 cs_pcmcia_validate_config(struct pcmcia_config_entry *cfe) 87 { 88 if (cfe->iftype != PCMCIA_IFTYPE_IO || 89 cfe->num_memspace != 0 || 90 cfe->num_iospace != 1 || 91 cfe->iospace[0].length < CS8900_IOSIZE) 92 return (EINVAL); 93 return (0); 94 } 95 96 static void 97 cs_pcmcia_attach(device_t parent, device_t self, void *aux) 98 { 99 struct cs_pcmcia_softc *psc = device_private(self); 100 struct cs_softc *sc = &psc->sc_cs; 101 struct pcmcia_attach_args *pa = aux; 102 struct pcmcia_config_entry *cfe; 103 struct pcmcia_function *pf; 104 int error; 105 106 sc->sc_dev = self; 107 pf = psc->sc_pf = pa->pf; 108 109 error = pcmcia_function_configure(pa->pf, cs_pcmcia_validate_config); 110 if (error) { 111 aprint_error_dev(self, "configure failed, error=%d\n", 112 error); 113 return; 114 } 115 116 cfe = pf->cfe; 117 sc->sc_iot = cfe->iospace[0].handle.iot; 118 sc->sc_ioh = cfe->iospace[0].handle.ioh; 119 sc->sc_irq = -1; 120 #define CS_PCMCIA_HACK_FOR_CARDBUS 121 #ifdef CS_PCMCIA_HACK_FOR_CARDBUS 122 /* 123 * XXX is there a generic way to know if it's a cardbus or not? 124 */ 125 sc->sc_cfgflags |= CFGFLG_CARDBUS_HACK; 126 #endif 127 128 error = cs_pcmcia_enable(sc); 129 if (error) 130 goto fail; 131 132 sc->sc_enable = cs_pcmcia_enable; 133 sc->sc_disable = cs_pcmcia_disable; 134 135 /* chip attach */ 136 error = cs_attach(sc, 0, 0, 0, 0); 137 if (error) 138 goto fail2; 139 140 cs_pcmcia_disable(sc); 141 psc->sc_state = CS_PCMCIA_ATTACHED; 142 return; 143 144 fail2: 145 cs_pcmcia_disable(sc); 146 fail: 147 pcmcia_function_unconfigure(pf); 148 } 149 150 static int 151 cs_pcmcia_detach(device_t self, int flags) 152 { 153 struct cs_pcmcia_softc *psc = device_private(self); 154 struct cs_softc *sc = &psc->sc_cs; 155 int error; 156 157 if (psc->sc_state != CS_PCMCIA_ATTACHED) 158 return (0); 159 160 error = cs_detach(sc); 161 if (error) 162 return (error); 163 164 pcmcia_function_unconfigure(psc->sc_pf); 165 166 return (0); 167 } 168 169 static int 170 cs_pcmcia_enable(struct cs_softc *sc) 171 { 172 struct cs_pcmcia_softc *psc = (void *)sc; 173 int error; 174 175 sc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, cs_intr, sc); 176 if (!sc->sc_ih) 177 return (EIO); 178 179 error = pcmcia_function_enable(psc->sc_pf); 180 if (error) { 181 pcmcia_intr_disestablish(psc->sc_pf, sc->sc_ih); 182 sc->sc_ih = 0; 183 } 184 185 return (error); 186 } 187 188 static void 189 cs_pcmcia_disable(struct cs_softc *sc) 190 { 191 struct cs_pcmcia_softc *psc = (void *)sc; 192 193 pcmcia_function_disable(psc->sc_pf); 194 pcmcia_intr_disestablish(psc->sc_pf, sc->sc_ih); 195 sc->sc_ih = 0; 196 } 197