if_an_pcmcia.c revision 1.22 1 /* $NetBSD: if_an_pcmcia.c,v 1.22 2004/08/10 18:39:08 mycroft Exp $ */
2
3 /*-
4 * Copyright (c) 2000, 2004 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Atsushi Onoe
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: if_an_pcmcia.c,v 1.22 2004/08/10 18:39:08 mycroft Exp $");
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/callout.h>
45 #include <sys/mbuf.h>
46 #include <sys/socket.h>
47 #include <sys/ioctl.h>
48 #include <sys/errno.h>
49 #include <sys/syslog.h>
50 #include <sys/select.h>
51 #include <sys/device.h>
52
53 #include <net/if.h>
54 #include <net/if_dl.h>
55 #include <net/if_ether.h>
56 #include <net/if_media.h>
57
58 #include <net80211/ieee80211_var.h>
59 #include <net80211/ieee80211_compat.h>
60
61 #include <machine/cpu.h>
62 #include <machine/bus.h>
63 #include <machine/intr.h>
64
65 #include <dev/ic/anreg.h>
66 #include <dev/ic/anvar.h>
67
68 #include <dev/pcmcia/pcmciareg.h>
69 #include <dev/pcmcia/pcmciavar.h>
70 #include <dev/pcmcia/pcmciadevs.h>
71
72 static int an_pcmcia_match __P((struct device *, struct cfdata *, void *));
73 static void an_pcmcia_attach __P((struct device *, struct device *, void *));
74 static int an_pcmcia_detach __P((struct device *, int));
75 static int an_pcmcia_enable __P((struct an_softc *));
76 static void an_pcmcia_disable __P((struct an_softc *));
77
78 struct an_pcmcia_softc {
79 struct an_softc sc_an; /* real "an" softc */
80
81 /* PCMCIA-specific goo */
82 struct pcmcia_io_handle sc_pcioh; /* PCMCIA i/o space info */
83 int sc_io_window; /* our i/o window */
84 struct pcmcia_function *sc_pf; /* our PCMCIA function */
85 void *sc_ih; /* interrupt handle */
86 void *sc_powerhook; /* power hook descriptor */
87 };
88
89 CFATTACH_DECL(an_pcmcia, sizeof(struct an_pcmcia_softc),
90 an_pcmcia_match, an_pcmcia_attach, an_pcmcia_detach, an_activate);
91
92 static struct pcmcia_product an_pcmcia_products[] = {
93 { PCMCIA_VENDOR_AIRONET, PCMCIA_PRODUCT_AIRONET_PC4800,
94 PCMCIA_CIS_AIRONET_PC4800 },
95 { PCMCIA_VENDOR_AIRONET, PCMCIA_PRODUCT_AIRONET_PC4500,
96 PCMCIA_CIS_AIRONET_PC4500 },
97 { PCMCIA_VENDOR_AIRONET, PCMCIA_PRODUCT_AIRONET_350,
98 PCMCIA_CIS_AIRONET_350 },
99 };
100 static const size_t an_pcmcia_nproducts =
101 sizeof(an_pcmcia_products) / sizeof(an_pcmcia_products[0]);
102
103 static int
104 an_pcmcia_enable(sc)
105 struct an_softc *sc;
106 {
107 struct an_pcmcia_softc *psc = (struct an_pcmcia_softc *)sc;
108 struct pcmcia_function *pf = psc->sc_pf;
109
110 /* establish the interrupt. */
111 psc->sc_ih = pcmcia_intr_establish(pf, IPL_NET, an_intr, sc);
112 if (psc->sc_ih == NULL) {
113 printf("%s: couldn't establish interrupt\n",
114 sc->sc_dev.dv_xname);
115 return (1);
116 }
117
118 if (pcmcia_function_enable(pf)) {
119 pcmcia_intr_disestablish(pf, psc->sc_ih);
120 return (1);
121 }
122 DELAY(1000);
123
124 return (0);
125 }
126
127 static void
128 an_pcmcia_disable(sc)
129 struct an_softc *sc;
130 {
131 struct an_pcmcia_softc *psc = (struct an_pcmcia_softc *)sc;
132 struct pcmcia_function *pf = psc->sc_pf;
133
134 pcmcia_function_disable(pf);
135 pcmcia_intr_disestablish(pf, psc->sc_ih);
136 }
137
138 static int
139 an_pcmcia_match(parent, match, aux)
140 struct device *parent;
141 struct cfdata *match;
142 void *aux;
143 {
144 struct pcmcia_attach_args *pa = aux;
145
146 if (pcmcia_product_lookup(pa, an_pcmcia_products, an_pcmcia_nproducts,
147 sizeof(an_pcmcia_products[0]), NULL))
148 return (1);
149 return (0);
150 }
151
152 static void
153 an_pcmcia_attach(parent, self, aux)
154 struct device *parent, *self;
155 void *aux;
156 {
157 struct an_pcmcia_softc *psc = (void *)self;
158 struct an_softc *sc = &psc->sc_an;
159 struct pcmcia_attach_args *pa = aux;
160 struct pcmcia_config_entry *cfe;
161
162 aprint_normal("\n");
163
164 psc->sc_pf = pa->pf;
165 if ((cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head)) == NULL) {
166 printf("%s: no suitable CIS info found\n", sc->sc_dev.dv_xname);
167 goto fail1;
168 }
169
170 if (pcmcia_io_alloc(psc->sc_pf, cfe->iospace[0].start,
171 cfe->iospace[0].length, AN_IOSIZ, &psc->sc_pcioh) != 0) {
172 printf("%s: failed to allocate io space\n",
173 sc->sc_dev.dv_xname);
174 goto fail1;
175 }
176
177 if (pcmcia_io_map(psc->sc_pf, PCMCIA_WIDTH_AUTO, &psc->sc_pcioh,
178 &psc->sc_io_window) != 0) {
179 printf("%s: failed to map io space\n", sc->sc_dev.dv_xname);
180 goto fail2;
181 }
182
183 sc->sc_iot = psc->sc_pcioh.iot;
184 sc->sc_ioh = psc->sc_pcioh.ioh;
185
186 pcmcia_function_init(psc->sc_pf, cfe);
187
188 if (an_pcmcia_enable(sc)) {
189 printf("%s: enable failed\n", sc->sc_dev.dv_xname);
190 goto fail3;
191 }
192
193 sc->sc_enabled = 1;
194 sc->sc_enable = an_pcmcia_enable;
195 sc->sc_disable = an_pcmcia_disable;
196
197 if (an_attach(sc) != 0) {
198 printf("%s: failed to attach controller\n",
199 sc->sc_dev.dv_xname);
200 goto fail4;
201 }
202 psc->sc_powerhook = powerhook_establish(an_power, sc);
203
204 /* disable device and disestablish the interrupt */
205 sc->sc_enabled = 0;
206 an_pcmcia_disable(sc);
207 return;
208
209 fail4:
210 sc->sc_enabled = 0;
211 an_pcmcia_disable(sc);
212 fail3:
213 pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
214 fail2:
215 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
216 fail1:
217 psc->sc_io_window = -1;
218 }
219
220
221 static int
222 an_pcmcia_detach(self, flags)
223 struct device *self;
224 int flags;
225 {
226 struct an_pcmcia_softc *psc = (struct an_pcmcia_softc *)self;
227 int error;
228
229 if (psc->sc_io_window == -1)
230 /* Nothing to detach. */
231 return (0);
232
233 if (psc->sc_powerhook != NULL)
234 powerhook_disestablish(psc->sc_powerhook);
235
236 error = an_detach(&psc->sc_an);
237 if (error != 0)
238 return (error);
239
240 /* Unmap our i/o window. */
241 pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
242
243 /* Free our i/o space. */
244 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
245 return (0);
246 }
247