xref: /netbsd/sys/dev/pcmcia/if_an_pcmcia.c (revision 5f819ca3)
1 /* $NetBSD: if_an_pcmcia.c,v 1.41 2012/10/27 17:18:36 chs 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.41 2012/10/27 17:18:36 chs 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
an_pcmcia_match(device_t parent,cfdata_t match,void * aux)100 an_pcmcia_match(device_t parent, cfdata_t match, void *aux)
101 {
102 	struct pcmcia_attach_args *pa = aux;
103 
104 	if (pcmcia_product_lookup(pa, an_pcmcia_products, an_pcmcia_nproducts,
105 	    sizeof(an_pcmcia_products[0]), NULL))
106 		return (1);
107 	return (0);
108 }
109 
110 static int
an_pcmcia_validate_config(struct pcmcia_config_entry * cfe)111 an_pcmcia_validate_config(struct pcmcia_config_entry *cfe)
112 {
113 	if (cfe->iftype != PCMCIA_IFTYPE_IO ||
114 	    cfe->num_iospace < 1)
115 		return (EINVAL);
116 	return (0);
117 }
118 
119 static void
an_pcmcia_attach(device_t parent,device_t self,void * aux)120 an_pcmcia_attach(device_t parent, device_t self, void *aux)
121 {
122 	struct an_pcmcia_softc *psc = device_private(self);
123 	struct an_softc *sc = &psc->sc_an;
124 	struct pcmcia_attach_args *pa = aux;
125 	struct pcmcia_config_entry *cfe;
126 	int error;
127 
128 	sc->sc_dev = self;
129 	psc->sc_pf = pa->pf;
130 
131 	error = pcmcia_function_configure(pa->pf, an_pcmcia_validate_config);
132 	if (error) {
133 		aprint_error_dev(self, "configure failed, error=%d\n",
134 		    error);
135 		return;
136 	}
137 
138 	cfe = pa->pf->cfe;
139 	sc->sc_iot = cfe->iospace[0].handle.iot;
140 	sc->sc_ioh = cfe->iospace[0].handle.ioh;
141 
142 	error = an_pcmcia_enable(sc);
143 	if (error)
144 		goto fail;
145 
146 	sc->sc_enabled = 1;
147 	sc->sc_enable = an_pcmcia_enable;
148 	sc->sc_disable = an_pcmcia_disable;
149 
150 	error = an_attach(sc);
151 	if (error) {
152 		aprint_error_dev(self, "failed to attach controller\n");
153 		goto fail2;
154 	}
155 
156 	if (pmf_device_register(self, NULL, NULL))
157 		pmf_class_network_register(self, &sc->sc_if);
158 	else
159 		aprint_error_dev(self, "couldn't establish power handler\n");
160 
161 	an_pcmcia_disable(sc);
162 	sc->sc_enabled = 0;
163 	psc->sc_state = AN_PCMCIA_ATTACHED;
164 	return;
165 
166 fail2:
167 	an_pcmcia_disable(sc);
168 	sc->sc_enabled = 0;
169 fail:
170 	pcmcia_function_unconfigure(pa->pf);
171 }
172 
173 
174 static int
an_pcmcia_detach(device_t self,int flags)175 an_pcmcia_detach(device_t self, int flags)
176 {
177 	struct an_pcmcia_softc *psc = device_private(self);
178 	int error;
179 
180 	if (psc->sc_state != AN_PCMCIA_ATTACHED)
181 		return (0);
182 
183 	pmf_device_deregister(self);
184 
185 	error = an_detach(&psc->sc_an);
186 	if (error)
187 		return (error);
188 
189 	pcmcia_function_unconfigure(psc->sc_pf);
190 
191 	return (0);
192 }
193 
194 static int
an_pcmcia_enable(struct an_softc * sc)195 an_pcmcia_enable(struct an_softc *sc)
196 {
197 	struct an_pcmcia_softc *psc = (void *)sc;
198 	int error;
199 
200 	/* establish the interrupt. */
201 	psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, an_intr, sc);
202 	if (!psc->sc_ih)
203 		return (EIO);
204 
205 	error = pcmcia_function_enable(psc->sc_pf);
206 	if (error) {
207 		pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
208 		psc->sc_ih = 0;
209 	}
210 
211 	return (error);
212 }
213 
214 static void
an_pcmcia_disable(struct an_softc * sc)215 an_pcmcia_disable(struct an_softc *sc)
216 {
217 	struct an_pcmcia_softc *psc = (void *)sc;
218 
219 	pcmcia_function_disable(psc->sc_pf);
220 	pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
221 	psc->sc_ih = 0;
222 }
223