xref: /netbsd/sys/dev/pcmcia/if_an_pcmcia.c (revision 6550d01e)
1 /* $NetBSD: if_an_pcmcia.c,v 1.40 2009/09/05 14:44:59 tsutsui 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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: if_an_pcmcia.c,v 1.40 2009/09/05 14:44:59 tsutsui Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/callout.h>
38 #include <sys/mbuf.h>
39 #include <sys/socket.h>
40 #include <sys/ioctl.h>
41 #include <sys/errno.h>
42 #include <sys/syslog.h>
43 #include <sys/select.h>
44 #include <sys/device.h>
45 
46 #include <net/if.h>
47 #include <net/if_dl.h>
48 #include <net/if_ether.h>
49 #include <net/if_media.h>
50 
51 #include <net80211/ieee80211_netbsd.h>
52 #include <net80211/ieee80211_var.h>
53 
54 #include <sys/cpu.h>
55 #include <sys/bus.h>
56 #include <sys/intr.h>
57 
58 #include <dev/ic/anreg.h>
59 #include <dev/ic/anvar.h>
60 
61 #include <dev/pcmcia/pcmciareg.h>
62 #include <dev/pcmcia/pcmciavar.h>
63 #include <dev/pcmcia/pcmciadevs.h>
64 
65 static int an_pcmcia_match(device_t, cfdata_t, void *);
66 static int an_pcmcia_validate_config(struct pcmcia_config_entry *);
67 static void an_pcmcia_attach(device_t, device_t, void *);
68 static int an_pcmcia_detach(device_t, int);
69 static int an_pcmcia_enable(struct an_softc *);
70 static void an_pcmcia_disable(struct an_softc *);
71 
72 struct an_pcmcia_softc {
73 	struct an_softc sc_an;			/* real "an" softc */
74 
75 	/* PCMCIA-specific goo */
76 	struct pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
77 	int sc_io_window;			/* our i/o window */
78 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
79 	void *sc_ih;				/* interrupt handle */
80 
81 	int sc_state;
82 #define	AN_PCMCIA_ATTACHED	3
83 };
84 
85 CFATTACH_DECL_NEW(an_pcmcia, sizeof(struct an_pcmcia_softc),
86     an_pcmcia_match, an_pcmcia_attach, an_pcmcia_detach, an_activate);
87 
88 static const struct pcmcia_product an_pcmcia_products[] = {
89 	{ PCMCIA_VENDOR_AIRONET,	PCMCIA_PRODUCT_AIRONET_PC4800,
90 	  PCMCIA_CIS_AIRONET_PC4800 },
91 	{ PCMCIA_VENDOR_AIRONET,	PCMCIA_PRODUCT_AIRONET_PC4500,
92 	  PCMCIA_CIS_AIRONET_PC4500 },
93 	{ PCMCIA_VENDOR_AIRONET,	PCMCIA_PRODUCT_AIRONET_350,
94 	  PCMCIA_CIS_AIRONET_350 },
95 };
96 static const size_t an_pcmcia_nproducts =
97     sizeof(an_pcmcia_products) / sizeof(an_pcmcia_products[0]);
98 
99 static int
100 an_pcmcia_match(device_t parent, cfdata_t match,
101     void *aux)
102 {
103 	struct pcmcia_attach_args *pa = aux;
104 
105 	if (pcmcia_product_lookup(pa, an_pcmcia_products, an_pcmcia_nproducts,
106 	    sizeof(an_pcmcia_products[0]), NULL))
107 		return (1);
108 	return (0);
109 }
110 
111 static int
112 an_pcmcia_validate_config(struct pcmcia_config_entry *cfe)
113 {
114 	if (cfe->iftype != PCMCIA_IFTYPE_IO ||
115 	    cfe->num_iospace < 1)
116 		return (EINVAL);
117 	return (0);
118 }
119 
120 static void
121 an_pcmcia_attach(struct device  *parent, device_t self,
122     void *aux)
123 {
124 	struct an_pcmcia_softc *psc = device_private(self);
125 	struct an_softc *sc = &psc->sc_an;
126 	struct pcmcia_attach_args *pa = aux;
127 	struct pcmcia_config_entry *cfe;
128 	int error;
129 
130 	sc->sc_dev = self;
131 	psc->sc_pf = pa->pf;
132 
133 	error = pcmcia_function_configure(pa->pf, an_pcmcia_validate_config);
134 	if (error) {
135 		aprint_error_dev(self, "configure failed, error=%d\n",
136 		    error);
137 		return;
138 	}
139 
140 	cfe = pa->pf->cfe;
141 	sc->sc_iot = cfe->iospace[0].handle.iot;
142 	sc->sc_ioh = cfe->iospace[0].handle.ioh;
143 
144 	error = an_pcmcia_enable(sc);
145 	if (error)
146 		goto fail;
147 
148 	sc->sc_enabled = 1;
149 	sc->sc_enable = an_pcmcia_enable;
150 	sc->sc_disable = an_pcmcia_disable;
151 
152 	error = an_attach(sc);
153 	if (error) {
154 		aprint_error_dev(self, "failed to attach controller\n");
155 		goto fail2;
156 	}
157 
158 	if (pmf_device_register(self, NULL, NULL))
159 		pmf_class_network_register(self, &sc->sc_if);
160 	else
161 		aprint_error_dev(self, "couldn't establish power handler\n");
162 
163 	an_pcmcia_disable(sc);
164 	sc->sc_enabled = 0;
165 	psc->sc_state = AN_PCMCIA_ATTACHED;
166 	return;
167 
168 fail2:
169 	an_pcmcia_disable(sc);
170 	sc->sc_enabled = 0;
171 fail:
172 	pcmcia_function_unconfigure(pa->pf);
173 }
174 
175 
176 static int
177 an_pcmcia_detach(device_t self, int flags)
178 {
179 	struct an_pcmcia_softc *psc = device_private(self);
180 	int error;
181 
182 	if (psc->sc_state != AN_PCMCIA_ATTACHED)
183 		return (0);
184 
185 	pmf_device_deregister(self);
186 
187 	error = an_detach(&psc->sc_an);
188 	if (error)
189 		return (error);
190 
191 	pcmcia_function_unconfigure(psc->sc_pf);
192 
193 	return (0);
194 }
195 
196 static int
197 an_pcmcia_enable(struct an_softc *sc)
198 {
199 	struct an_pcmcia_softc *psc = (void *)sc;
200 	int error;
201 
202 	/* establish the interrupt. */
203 	psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, an_intr, sc);
204 	if (!psc->sc_ih)
205 		return (EIO);
206 
207 	error = pcmcia_function_enable(psc->sc_pf);
208 	if (error) {
209 		pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
210 		psc->sc_ih = 0;
211 	}
212 
213 	return (error);
214 }
215 
216 static void
217 an_pcmcia_disable(struct an_softc *sc)
218 {
219 	struct an_pcmcia_softc *psc = (void *)sc;
220 
221 	pcmcia_function_disable(psc->sc_pf);
222 	pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
223 	psc->sc_ih = 0;
224 }
225