xref: /netbsd-src/sys/dev/pcmcia/if_awi_pcmcia.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /* $NetBSD: if_awi_pcmcia.c,v 1.38 2007/10/19 12:01:04 ad Exp $ */
2 
3 /*-
4  * Copyright (c) 1999, 2004 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Bill Sommerfeld
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 /*
40  * PCMCIA attachment for BayStack 650 802.11FH PCMCIA card,
41  * based on the AMD 79c930 802.11 controller chip.
42  *
43  * This attachment can probably be trivially adapted for other FH and
44  * DS cards based on the same chipset.
45  */
46 
47 #include <sys/cdefs.h>
48 __KERNEL_RCSID(0, "$NetBSD: if_awi_pcmcia.c,v 1.38 2007/10/19 12:01:04 ad Exp $");
49 
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/mbuf.h>
53 #include <sys/socket.h>
54 #include <sys/ioctl.h>
55 #include <sys/errno.h>
56 #include <sys/syslog.h>
57 #include <sys/select.h>
58 #include <sys/device.h>
59 
60 #include <net/if.h>
61 #include <net/if_dl.h>
62 #include <net/if_ether.h>
63 #include <net/if_media.h>
64 
65 #include <net80211/ieee80211_netbsd.h>
66 #include <net80211/ieee80211_var.h>
67 
68 #include <sys/cpu.h>
69 #include <sys/bus.h>
70 #include <sys/intr.h>
71 
72 #include <dev/ic/am79c930reg.h>
73 #include <dev/ic/am79c930var.h>
74 #include <dev/ic/awireg.h>
75 #include <dev/ic/awivar.h>
76 
77 #include <dev/pcmcia/pcmciareg.h>
78 #include <dev/pcmcia/pcmciavar.h>
79 #include <dev/pcmcia/pcmciadevs.h>
80 
81 static int awi_pcmcia_match(struct device *, struct cfdata *, void *);
82 static int awi_pcmcia_validate_config(struct pcmcia_config_entry *);
83 static void awi_pcmcia_attach(struct device *, struct device *, void *);
84 static int awi_pcmcia_detach(struct device *, int);
85 static int awi_pcmcia_enable(struct awi_softc *);
86 static void awi_pcmcia_disable(struct awi_softc *);
87 
88 struct awi_pcmcia_softc {
89 	struct awi_softc sc_awi;		/* real "awi" softc */
90 
91 	/* PCMCIA-specific goo */
92 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
93 	void *sc_ih;				/* interrupt handler */
94 
95 	int sc_state;
96 #define	AWI_PCMCIA_ATTACHED	3
97 };
98 
99 CFATTACH_DECL(awi_pcmcia, sizeof(struct awi_pcmcia_softc),
100     awi_pcmcia_match, awi_pcmcia_attach, awi_pcmcia_detach, awi_activate);
101 
102 static const struct pcmcia_product awi_pcmcia_products[] = {
103 	{ PCMCIA_VENDOR_BAY,		PCMCIA_PRODUCT_BAY_STACK_650,
104 	  PCMCIA_CIS_BAY_STACK_650 },
105 
106 	{ PCMCIA_VENDOR_BAY,		PCMCIA_PRODUCT_BAY_STACK_660,
107 	  PCMCIA_CIS_BAY_STACK_660 },
108 
109 	{ PCMCIA_VENDOR_BAY,		PCMCIA_PRODUCT_BAY_SURFER_PRO,
110 	  PCMCIA_CIS_BAY_SURFER_PRO },
111 
112 	{ PCMCIA_VENDOR_AMD,		PCMCIA_PRODUCT_AMD_AM79C930,
113 	  PCMCIA_CIS_AMD_AM79C930 },
114 
115 	{ PCMCIA_VENDOR_ICOM,		PCMCIA_PRODUCT_ICOM_SL200,
116 	  PCMCIA_CIS_ICOM_SL200 },
117 
118 	{ PCMCIA_VENDOR_NOKIA,		PCMCIA_PRODUCT_NOKIA_C020_WLAN,
119 	  PCMCIA_CIS_NOKIA_C020_WLAN },
120 
121 	{ PCMCIA_VENDOR_FARALLON,	PCMCIA_PRODUCT_FARALLON_SKYLINE,
122 	  PCMCIA_CIS_FARALLON_SKYLINE },
123 };
124 static const size_t awi_pcmcia_nproducts =
125     sizeof(awi_pcmcia_products) / sizeof(awi_pcmcia_products[0]);
126 
127 static int
128 awi_pcmcia_enable(sc)
129 	struct awi_softc *sc;
130 {
131 	struct awi_pcmcia_softc *psc = (struct awi_pcmcia_softc *)sc;
132 	struct pcmcia_function *pf = psc->sc_pf;
133 	int error;
134 
135 	/* establish the interrupt. */
136 	psc->sc_ih = pcmcia_intr_establish(pf, IPL_NET, awi_intr, sc);
137 	if (!psc->sc_ih)
138 		return (EIO);
139 
140 	error = pcmcia_function_enable(pf);
141 	if (error) {
142 		pcmcia_intr_disestablish(pf, psc->sc_ih);
143 		psc->sc_ih = 0;
144 	}
145 
146 	return (error);
147 }
148 
149 static void
150 awi_pcmcia_disable(sc)
151 	struct awi_softc *sc;
152 {
153 	struct awi_pcmcia_softc *psc = (struct awi_pcmcia_softc *)sc;
154 	struct pcmcia_function *pf = psc->sc_pf;
155 
156 	pcmcia_function_disable(pf);
157 	pcmcia_intr_disestablish(pf, psc->sc_ih);
158 	psc->sc_ih = 0;
159 }
160 
161 static int
162 awi_pcmcia_match(struct device *parent, struct cfdata *match,
163     void *aux)
164 {
165 	struct pcmcia_attach_args *pa = aux;
166 
167 	if (pcmcia_product_lookup(pa, awi_pcmcia_products, awi_pcmcia_nproducts,
168 	    sizeof(awi_pcmcia_products[0]), NULL))
169 		return (1);
170 	return (0);
171 }
172 
173 static int
174 awi_pcmcia_validate_config(cfe)
175 	struct pcmcia_config_entry *cfe;
176 {
177 	if (cfe->iftype != PCMCIA_IFTYPE_IO ||
178 	    cfe->num_iospace < 1 ||
179 	    cfe->iospace[0].length < AM79C930_IO_SIZE)
180 		return (EINVAL);
181 	if (cfe->num_memspace < 1) {
182 		cfe->memspace[0].length = AM79C930_MEM_SIZE;
183 		cfe->memspace[0].cardaddr = 0;
184 		cfe->memspace[0].hostaddr = 0;
185 	} else if (cfe->memspace[0].length < AM79C930_MEM_SIZE)
186 		return (EINVAL);
187 	return (0);
188 }
189 
190 static void
191 awi_pcmcia_attach(struct device *parent, struct device *self,
192     void *aux)
193 {
194 	struct awi_pcmcia_softc *psc = (void *)self;
195 	struct awi_softc *sc = &psc->sc_awi;
196 	struct pcmcia_attach_args *pa = aux;
197 	struct pcmcia_config_entry *cfe;
198 	int error;
199 
200 	psc->sc_pf = pa->pf;
201 
202 	error = pcmcia_function_configure(pa->pf, awi_pcmcia_validate_config);
203 	if (error) {
204 		aprint_error("%s: configure failed, error=%d\n", self->dv_xname,
205 		    error);
206 		return;
207 	}
208 
209 	cfe = pa->pf->cfe;
210 	sc->sc_chip.sc_bustype = AM79C930_BUS_PCMCIA;
211 	sc->sc_chip.sc_iot = cfe->iospace[0].handle.iot;
212 	sc->sc_chip.sc_ioh = cfe->iospace[0].handle.ioh;
213 	if (cfe->num_memspace > 0) {
214 		sc->sc_chip.sc_memt = cfe->memspace[0].handle.memt;
215 		sc->sc_chip.sc_memh = cfe->memspace[0].handle.memh;
216 		am79c930_chip_init(&sc->sc_chip, 1);
217 	} else
218 		am79c930_chip_init(&sc->sc_chip, 0);
219 
220 	error = awi_pcmcia_enable(sc);
221         if (error)
222                 goto fail;
223 	sc->sc_enabled = 1;
224 
225 	awi_read_bytes(sc, AWI_BANNER, sc->sc_banner, AWI_BANNER_LEN);
226 	if (memcmp(sc->sc_banner, "PCnetMobile:", 12))
227 		goto fail2;
228 
229 	sc->sc_enable = awi_pcmcia_enable;
230 	sc->sc_disable = awi_pcmcia_disable;
231 
232 	sc->sc_cansleep = 1;
233 
234 	if (awi_attach(sc) != 0) {
235 		printf("%s: failed to attach controller\n", self->dv_xname);
236 		goto fail2;
237 	}
238 
239 	sc->sc_enabled = 0;
240 	awi_pcmcia_disable(sc);
241 	psc->sc_state = AWI_PCMCIA_ATTACHED;
242 	return;
243 
244 fail2:
245 	sc->sc_enabled = 0;
246 	awi_pcmcia_disable(sc);
247 fail:
248 	pcmcia_function_unconfigure(pa->pf);
249 }
250 
251 static int
252 awi_pcmcia_detach(struct device *self, int flags)
253 {
254 	struct awi_pcmcia_softc *psc = (struct awi_pcmcia_softc *)self;
255 	int error;
256 
257 	if (psc->sc_state != AWI_PCMCIA_ATTACHED)
258 		return (0);
259 
260 	error = awi_detach(&psc->sc_awi);
261 	if (error)
262 		return (error);
263 
264 	pcmcia_function_unconfigure(psc->sc_pf);
265 
266 	return (0);
267 }
268