if_sm_pcmcia.c revision 1.46 1 /* $NetBSD: if_sm_pcmcia.c,v 1.46 2005/12/11 12:23:23 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 1998, 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 Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center, and by Charles M. Hannum.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: if_sm_pcmcia.c,v 1.46 2005/12/11 12:23:23 christos Exp $");
42
43 #include <sys/param.h>
44 #include <sys/systm.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 <machine/intr.h>
59 #include <machine/bus.h>
60
61 #include <dev/mii/mii.h>
62 #include <dev/mii/miivar.h>
63
64 #include <dev/ic/smc91cxxreg.h>
65 #include <dev/ic/smc91cxxvar.h>
66
67 #include <dev/pcmcia/pcmciareg.h>
68 #include <dev/pcmcia/pcmciavar.h>
69 #include <dev/pcmcia/pcmciadevs.h>
70
71 int sm_pcmcia_match(struct device *, struct cfdata *, void *);
72 int sm_pcmcia_validate_config(struct pcmcia_config_entry *);
73 void sm_pcmcia_attach(struct device *, struct device *, void *);
74 int sm_pcmcia_detach(struct device *, int);
75
76 struct sm_pcmcia_softc {
77 struct smc91cxx_softc sc_smc; /* real "smc" softc */
78
79 struct pcmcia_function *sc_pf; /* our PCMCIA function */
80 void *sc_ih; /* interrupt cookie */
81
82 int sc_state;
83 #define SM_PCMCIA_ATTACHED 3
84 };
85
86 CFATTACH_DECL(sm_pcmcia, sizeof(struct sm_pcmcia_softc),
87 sm_pcmcia_match, sm_pcmcia_attach, sm_pcmcia_detach, smc91cxx_activate);
88
89 int sm_pcmcia_enable(struct smc91cxx_softc *);
90 void sm_pcmcia_disable(struct smc91cxx_softc *);
91
92 int sm_pcmcia_ascii_enaddr(const char *, u_int8_t *);
93
94 const struct pcmcia_product sm_pcmcia_products[] = {
95 { PCMCIA_VENDOR_MEGAHERTZ2, PCMCIA_PRODUCT_MEGAHERTZ2_EM1144,
96 PCMCIA_CIS_INVALID },
97
98 { PCMCIA_VENDOR_MEGAHERTZ2, PCMCIA_PRODUCT_MEGAHERTZ2_XJACK,
99 PCMCIA_CIS_INVALID },
100
101 { PCMCIA_VENDOR_NEWMEDIA, PCMCIA_PRODUCT_NEWMEDIA_BASICS,
102 PCMCIA_CIS_INVALID },
103
104 #if 0
105 { PCMCIA_VENDOR_SMC, PCMCIA_PRODUCT_SMC_8020BT,
106 PCMCIA_CIS_INVALID },
107 #endif
108 { PCMCIA_VENDOR_PSION, PCMCIA_PRODUCT_PSION_GOLDCARD,
109 PCMCIA_CIS_INVALID },
110 };
111 const size_t sm_pcmcia_nproducts =
112 sizeof(sm_pcmcia_products) / sizeof(sm_pcmcia_products[0]);
113
114 int
115 sm_pcmcia_match(parent, match, aux)
116 struct device *parent;
117 struct cfdata *match;
118 void *aux;
119 {
120 struct pcmcia_attach_args *pa = aux;
121
122 /* This is to differentiate the serial function of some cards. */
123 if (pa->pf->function != PCMCIA_FUNCTION_NETWORK)
124 return (0);
125
126 if (pcmcia_product_lookup(pa, sm_pcmcia_products, sm_pcmcia_nproducts,
127 sizeof(sm_pcmcia_products[0]), NULL))
128 return (1);
129 return (0);
130 }
131
132 int
133 sm_pcmcia_validate_config(cfe)
134 struct pcmcia_config_entry *cfe;
135 {
136 if (cfe->iftype != PCMCIA_IFTYPE_IO ||
137 cfe->num_memspace != 0 ||
138 cfe->num_iospace != 1 ||
139 cfe->iospace[0].length < SMC_IOSIZE)
140 return (EINVAL);
141 return (0);
142 }
143
144 void
145 sm_pcmcia_attach(parent, self, aux)
146 struct device *parent, *self;
147 void *aux;
148 {
149 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)self;
150 struct smc91cxx_softc *sc = &psc->sc_smc;
151 struct pcmcia_attach_args *pa = aux;
152 struct pcmcia_config_entry *cfe;
153 u_int8_t enaddr[ETHER_ADDR_LEN];
154 int error;
155
156 psc->sc_pf = pa->pf;
157
158 error = pcmcia_function_configure(pa->pf, sm_pcmcia_validate_config);
159 if (error) {
160 aprint_error("%s: configure failed, error=%d\n", self->dv_xname,
161 error);
162 return;
163 }
164
165 cfe = pa->pf->cfe;
166 sc->sc_bst = cfe->iospace[0].handle.iot;
167 sc->sc_bsh = cfe->iospace[0].handle.ioh;
168
169 error = sm_pcmcia_enable(sc);
170 if (error)
171 goto fail;
172
173 sc->sc_enable = sm_pcmcia_enable;
174 sc->sc_disable = sm_pcmcia_disable;
175
176 /*
177 * First try to get the Ethernet address from FUNCE/LANNID tuple.
178 * If that fails, try one of the CIS info strings.
179 */
180 if (pa->pf->pf_funce_lan_nidlen == ETHER_ADDR_LEN) {
181 memcpy(enaddr, pa->pf->pf_funce_lan_nid, ETHER_ADDR_LEN);
182 } else {
183 if (!sm_pcmcia_ascii_enaddr(pa->pf->sc->card.cis1_info[3], enaddr) &&
184 !sm_pcmcia_ascii_enaddr(pa->pf->sc->card.cis1_info[2], enaddr))
185 aprint_error("%s: unable to get Ethernet address\n",
186 self->dv_xname);
187 }
188
189 /* Perform generic intialization. */
190 smc91cxx_attach(sc, enaddr);
191
192 psc->sc_state = SM_PCMCIA_ATTACHED;
193 sm_pcmcia_disable(sc);
194 return;
195
196 fail:
197 pcmcia_function_unconfigure(pa->pf);
198 }
199
200 int
201 sm_pcmcia_detach(self, flags)
202 struct device *self;
203 int flags;
204 {
205 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)self;
206 int error;
207
208 if (psc->sc_state != SM_PCMCIA_ATTACHED)
209 return (0);
210
211 error = smc91cxx_detach((struct device *)&psc->sc_smc, flags);
212 if (error)
213 return (error);
214
215 pcmcia_function_unconfigure(psc->sc_pf);
216
217 return (0);
218 }
219
220 int
221 sm_pcmcia_ascii_enaddr(cisstr, myla)
222 const char *cisstr;
223 u_int8_t *myla;
224 {
225 u_int8_t digit;
226 int i;
227
228 /* No CIS string. */
229 if (cisstr == 0)
230 return (0);
231
232 memset(myla, 0, ETHER_ADDR_LEN);
233
234 for (i = 0, digit = 0; i < (ETHER_ADDR_LEN * 2); i++) {
235 if (cisstr[i] >= '0' && cisstr[i] <= '9')
236 digit |= cisstr[i] - '0';
237 else if (cisstr[i] >= 'a' && cisstr[i] <= 'f')
238 digit |= (cisstr[i] - 'a') + 10;
239 else if (cisstr[i] >= 'A' && cisstr[i] <= 'F')
240 digit |= (cisstr[i] - 'A') + 10;
241 else {
242 /* Bogus digit!! */
243 return (0);
244 }
245
246 /* Compensate for ordering of digits. */
247 if (i & 1) {
248 myla[i >> 1] = digit;
249 digit = 0;
250 } else
251 digit <<= 4;
252 }
253
254 return (1);
255 }
256
257 int
258 sm_pcmcia_enable(sc)
259 struct smc91cxx_softc *sc;
260 {
261 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
262 int error;
263
264 /* Establish the interrupt handler. */
265 psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, smc91cxx_intr,
266 sc);
267 if (!psc->sc_ih)
268 return (EIO);
269
270 error = pcmcia_function_enable(psc->sc_pf);
271 if (error) {
272 pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
273 psc->sc_ih = 0;
274 }
275
276 return (error);
277 }
278
279 void
280 sm_pcmcia_disable(sc)
281 struct smc91cxx_softc *sc;
282 {
283 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
284
285 pcmcia_function_disable(psc->sc_pf);
286 pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
287 psc->sc_ih = 0;
288 }
289