if_sm_pcmcia.c revision 1.41 1 /* $NetBSD: if_sm_pcmcia.c,v 1.41 2004/08/10 15:29:56 mycroft 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.41 2004/08/10 15:29:56 mycroft 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 __P((struct device *, struct cfdata *, void *));
72 int sm_pcmcia_validate_config __P((struct pcmcia_config_entry *));
73 void sm_pcmcia_attach __P((struct device *, struct device *, void *));
74 int sm_pcmcia_detach __P((struct device *, int));
75
76 struct sm_pcmcia_softc {
77 struct smc91cxx_softc sc_smc; /* real "smc" softc */
78
79 void *sc_ih; /* interrupt cookie */
80
81 struct pcmcia_function *sc_pf; /* our PCMCIA function */
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 __P((struct smc91cxx_softc *));
90 void sm_pcmcia_disable __P((struct smc91cxx_softc *));
91
92 int sm_pcmcia_ascii_enaddr __P((const char *, u_int8_t *));
93
94 const struct pcmcia_product sm_pcmcia_products[] = {
95 { "", PCMCIA_VENDOR_MEGAHERTZ2,
96 PCMCIA_PRODUCT_MEGAHERTZ2_EM1144, 0, },
97 { "", PCMCIA_VENDOR_MEGAHERTZ2,
98 PCMCIA_PRODUCT_MEGAHERTZ2_EM1144, 1, },
99
100 { "", PCMCIA_VENDOR_MEGAHERTZ2,
101 PCMCIA_PRODUCT_MEGAHERTZ2_XJACK, 0, },
102
103 { "", PCMCIA_VENDOR_NEWMEDIA,
104 PCMCIA_PRODUCT_NEWMEDIA_BASICS, 0, },
105
106 #if 0
107 { "", PCMCIA_VENDOR_SMC,
108 PCMCIA_PRODUCT_SMC_8020BT, 0, },
109 #endif
110 { "", PCMCIA_VENDOR_PSION,
111 PCMCIA_PRODUCT_PSION_GOLDCARD, 0, },
112
113 { NULL }
114 };
115
116 int
117 sm_pcmcia_match(parent, match, aux)
118 struct device *parent;
119 struct cfdata *match;
120 void *aux;
121 {
122 struct pcmcia_attach_args *pa = aux;
123
124 /* This is to differentiate the serial function of Megahertz cards. */
125 if (pa->pf->function != PCMCIA_FUNCTION_NETWORK)
126 return (0);
127
128 if (pcmcia_product_lookup(pa, sm_pcmcia_products,
129 sizeof sm_pcmcia_products[0], NULL) != NULL)
130 return (1);
131 return (0);
132 }
133
134 int
135 sm_pcmcia_validate_config(cfe)
136 struct pcmcia_config_entry *cfe;
137 {
138 if (cfe->iftype != PCMCIA_IFTYPE_IO ||
139 cfe->num_memspace != 0 ||
140 cfe->num_iospace != 1 ||
141 cfe->iospace[0].length < SMC_IOSIZE)
142 return (EINVAL);
143 return (0);
144 }
145
146 void
147 sm_pcmcia_attach(parent, self, aux)
148 struct device *parent, *self;
149 void *aux;
150 {
151 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)self;
152 struct smc91cxx_softc *sc = &psc->sc_smc;
153 struct pcmcia_attach_args *pa = aux;
154 struct pcmcia_config_entry *cfe;
155 u_int8_t enaddr[ETHER_ADDR_LEN];
156 int error;
157
158 aprint_normal("\n");
159 psc->sc_pf = pa->pf;
160
161 error = pcmcia_function_configure(pa->pf, sm_pcmcia_validate_config);
162 if (error) {
163 aprint_error("%s: configure failed, error=%d\n", self->dv_xname,
164 error);
165 return;
166 }
167
168 cfe = pa->pf->cfe;
169 sc->sc_bst = cfe->iospace[0].handle.iot;
170 sc->sc_bsh = cfe->iospace[0].handle.ioh;
171
172 error = sm_pcmcia_enable(sc);
173 if (error)
174 goto fail;
175
176 sc->sc_enable = sm_pcmcia_enable;
177 sc->sc_disable = sm_pcmcia_disable;
178
179 /*
180 * First try to get the Ethernet address from FUNCE/LANNID tuple.
181 * If that fails, try one of the CIS info strings.
182 */
183 if (pa->pf->pf_funce_lan_nidlen == ETHER_ADDR_LEN) {
184 memcpy(enaddr, pa->pf->pf_funce_lan_nid, ETHER_ADDR_LEN);
185 } else {
186 if (!sm_pcmcia_ascii_enaddr(pa->pf->sc->card.cis1_info[3], enaddr) &&
187 !sm_pcmcia_ascii_enaddr(pa->pf->sc->card.cis1_info[2], enaddr))
188 aprint_error("%s: unable to get Ethernet address\n",
189 self->dv_xname);
190 }
191
192 /* Perform generic intialization. */
193 smc91cxx_attach(sc, enaddr);
194
195 psc->sc_state = SM_PCMCIA_ATTACHED;
196 sm_pcmcia_disable(sc);
197 return;
198
199 fail:
200 pcmcia_function_unconfigure(pa->pf);
201 }
202
203 int
204 sm_pcmcia_detach(self, flags)
205 struct device *self;
206 int flags;
207 {
208 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)self;
209 int error;
210
211 if (psc->sc_state != SM_PCMCIA_ATTACHED)
212 return (0);
213
214 error = smc91cxx_detach((struct device *)&psc->sc_smc, flags);
215 if (error)
216 return (error);
217
218 pcmcia_function_unconfigure(psc->sc_pf);
219
220 return (0);
221 }
222
223 int
224 sm_pcmcia_ascii_enaddr(cisstr, myla)
225 const char *cisstr;
226 u_int8_t *myla;
227 {
228 u_int8_t digit;
229 int i;
230
231 /* No CIS string. */
232 if (cisstr == 0)
233 return (0);
234
235 memset(myla, 0, ETHER_ADDR_LEN);
236
237 for (i = 0, digit = 0; i < (ETHER_ADDR_LEN * 2); i++) {
238 if (cisstr[i] >= '0' && cisstr[i] <= '9')
239 digit |= cisstr[i] - '0';
240 else if (cisstr[i] >= 'a' && cisstr[i] <= 'f')
241 digit |= (cisstr[i] - 'a') + 10;
242 else if (cisstr[i] >= 'A' && cisstr[i] <= 'F')
243 digit |= (cisstr[i] - 'A') + 10;
244 else {
245 /* Bogus digit!! */
246 return (0);
247 }
248
249 /* Compensate for ordering of digits. */
250 if (i & 1) {
251 myla[i >> 1] = digit;
252 digit = 0;
253 } else
254 digit <<= 4;
255 }
256
257 return (1);
258 }
259
260 int
261 sm_pcmcia_enable(sc)
262 struct smc91cxx_softc *sc;
263 {
264 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
265 int error;
266
267 /* Establish the interrupt handler. */
268 psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, smc91cxx_intr,
269 sc);
270 if (!psc->sc_ih)
271 return (EIO);
272
273 error = pcmcia_function_enable(psc->sc_pf);
274 if (error)
275 pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
276
277 return (error);
278 }
279
280 void
281 sm_pcmcia_disable(sc)
282 struct smc91cxx_softc *sc;
283 {
284 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
285
286 pcmcia_function_disable(psc->sc_pf);
287 pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
288 psc->sc_ih = 0;
289 }
290