if_sm_pcmcia.c revision 1.20 1 /* $NetBSD: if_sm_pcmcia.c,v 1.20 2000/02/04 01:27:13 cgd Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 1998, 2000 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.
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 "opt_inet.h"
41 #include "opt_ns.h"
42 #include "bpfilter.h"
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/mbuf.h>
47 #include <sys/socket.h>
48 #include <sys/ioctl.h>
49 #include <sys/errno.h>
50 #include <sys/syslog.h>
51 #include <sys/select.h>
52 #include <sys/device.h>
53
54 #include <net/if.h>
55 #include <net/if_dl.h>
56 #include <net/if_ether.h>
57 #include <net/if_media.h>
58
59 #ifdef INET
60 #include <netinet/in.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/in_var.h>
63 #include <netinet/ip.h>
64 #include <netinet/if_inarp.h>
65 #endif
66
67 #ifdef NS
68 #include <netns/ns.h>
69 #include <netns/ns_if.h>
70 #endif
71
72 #if NBPFILTER > 0
73 #include <net/bpf.h>
74 #include <net/bpfdesc.h>
75 #endif
76
77 #include <machine/intr.h>
78 #include <machine/bus.h>
79
80 #include <dev/ic/smc91cxxreg.h>
81 #include <dev/ic/smc91cxxvar.h>
82
83 #include <dev/pcmcia/pcmciareg.h>
84 #include <dev/pcmcia/pcmciavar.h>
85 #include <dev/pcmcia/pcmciadevs.h>
86
87 int sm_pcmcia_match __P((struct device *, struct cfdata *, void *));
88 void sm_pcmcia_attach __P((struct device *, struct device *, void *));
89 int sm_pcmcia_detach __P((struct device *, int));
90
91 struct sm_pcmcia_softc {
92 struct smc91cxx_softc sc_smc; /* real "smc" softc */
93
94 /* PCMCIA-specific goo. */
95 struct pcmcia_io_handle sc_pcioh; /* PCMCIA i/o space info */
96 int sc_io_window; /* our i/o window */
97 void *sc_ih; /* interrupt cookie */
98 struct pcmcia_function *sc_pf; /* our PCMCIA function */
99
100 int sc_configured; /* bool; attached or not */
101 int sc_enabled; /* bool; enabled or not */
102 };
103
104 struct cfattach sm_pcmcia_ca = {
105 sizeof(struct sm_pcmcia_softc), sm_pcmcia_match, sm_pcmcia_attach,
106 sm_pcmcia_detach, smc91cxx_activate
107 };
108
109 int sm_pcmcia_enable __P((struct smc91cxx_softc *));
110 void sm_pcmcia_disable __P((struct smc91cxx_softc *));
111
112 int sm_pcmcia_ascii_enaddr __P((const char *, u_int8_t *));
113 int sm_pcmcia_funce_enaddr __P((struct device *, u_int8_t *));
114
115 int sm_pcmcia_lannid_ciscallback __P((struct pcmcia_tuple *, void *));
116
117 const struct pcmcia_product sm_pcmcia_products[] = {
118 { PCMCIA_STR_MEGAHERTZ2_XJACK, PCMCIA_VENDOR_MEGAHERTZ2,
119 PCMCIA_PRODUCT_MEGAHERTZ2_XJACK, 0, },
120
121 { PCMCIA_STR_NEWMEDIA_BASICS, PCMCIA_VENDOR_NEWMEDIA,
122 PCMCIA_PRODUCT_NEWMEDIA_BASICS, 0, },
123
124 #if 0
125 { PCMCIA_STR_SMC_8020BT, PCMCIA_VENDOR_SMC,
126 PCMCIA_PRODUCT_SMC_8020BT, 0, },
127 #endif
128
129 { NULL }
130 };
131
132 int
133 sm_pcmcia_match(parent, match, aux)
134 struct device *parent;
135 struct cfdata *match;
136 void *aux;
137 {
138 struct pcmcia_attach_args *pa = aux;
139
140 if (pcmcia_product_lookup(pa, sm_pcmcia_products,
141 sizeof sm_pcmcia_products[0], NULL) != NULL)
142 return (1);
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 myla[ETHER_ADDR_LEN], *enaddr = NULL;
156 const struct pcmcia_product *pp;
157
158 psc->sc_configured = 0;
159
160 psc->sc_pf = pa->pf;
161 cfe = pa->pf->cfe_head.sqh_first;
162
163 /* Enable the card. */
164 pcmcia_function_init(pa->pf, cfe);
165 if (pcmcia_function_enable(pa->pf)) {
166 printf(": function enable failed\n");
167 return;
168 }
169
170 /* XXX sanity check number of mem and i/o spaces */
171
172 /* Allocate and map i/o space for the card. */
173 if (pcmcia_io_alloc(pa->pf, 0, cfe->iospace[0].length,
174 cfe->iospace[0].length, &psc->sc_pcioh)) {
175 printf(": can't allocate i/o space\n");
176 pcmcia_function_disable(pa->pf);
177 return;
178 }
179
180 sc->sc_bst = psc->sc_pcioh.iot;
181 sc->sc_bsh = psc->sc_pcioh.ioh;
182
183 sc->sc_enable = sm_pcmcia_enable;
184 sc->sc_disable = sm_pcmcia_disable;
185
186 if (pcmcia_io_map(pa->pf, (cfe->flags & PCMCIA_CFE_IO16) ?
187 PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_IO8, 0, cfe->iospace[0].length,
188 &psc->sc_pcioh, &psc->sc_io_window)) {
189 printf(": can't map i/o space\n");
190 pcmcia_io_free(pa->pf, &psc->sc_pcioh);
191 pcmcia_function_disable(pa->pf);
192 return;
193 }
194
195 pp = pcmcia_product_lookup(pa, sm_pcmcia_products,
196 sizeof sm_pcmcia_products[0], NULL);
197 if (pp == NULL)
198 panic("sm_pcmcia_attach: impossible");
199
200 printf(": %s\n", pp->pp_name);
201
202 /*
203 * First try to get the Ethernet address from FUNCE/LANNID tuple.
204 */
205 if (sm_pcmcia_funce_enaddr(parent, myla))
206 enaddr = myla;
207
208 /*
209 * If that failed, try one of the CIS info strings.
210 */
211 if (enaddr == NULL) {
212 char *cisstr = NULL;
213
214 switch (pa->manufacturer) {
215 case PCMCIA_VENDOR_MEGAHERTZ:
216 case PCMCIA_VENDOR_MEGAHERTZ2:
217 cisstr = pa->pf->sc->card.cis1_info[3];
218 break;
219
220 case PCMCIA_VENDOR_SMC:
221 cisstr = pa->pf->sc->card.cis1_info[2];
222 break;
223 }
224
225 if (cisstr != NULL && sm_pcmcia_ascii_enaddr(cisstr, myla))
226 enaddr = myla;
227 }
228
229 if (enaddr == NULL)
230 printf("%s: unable to get Ethernet address\n",
231 sc->sc_dev.dv_xname);
232
233 /* Perform generic intialization. */
234 smc91cxx_attach(sc, enaddr);
235
236 pcmcia_function_disable(pa->pf);
237
238 psc->sc_configured = 1;
239 }
240
241 int
242 sm_pcmcia_detach(self, flags)
243 struct device *self;
244 int flags;
245 {
246 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)self;
247 int rv;
248
249 if (psc->sc_configured == 0)
250 return 0;
251
252 rv = smc91cxx_detach((struct device *)&psc->sc_smc, flags);
253 if (rv == 0) {
254 /* Unmap our i/o window. */
255 pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
256
257 /* Free our i/o space. */
258 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
259 }
260 return rv;
261 }
262
263 int
264 sm_pcmcia_ascii_enaddr(cisstr, myla)
265 const char *cisstr;
266 u_int8_t *myla;
267 {
268 u_int8_t digit;
269 int i;
270
271 memset(myla, 0, ETHER_ADDR_LEN);
272
273 for (i = 0, digit = 0; i < (ETHER_ADDR_LEN * 2); i++) {
274 if (cisstr[i] >= '0' && cisstr[i] <= '9')
275 digit |= cisstr[i] - '0';
276 else if (cisstr[i] >= 'a' && cisstr[i] <= 'f')
277 digit |= (cisstr[i] - 'a') + 10;
278 else if (cisstr[i] >= 'A' && cisstr[i] <= 'F')
279 digit |= (cisstr[i] - 'A') + 10;
280 else {
281 /* Bogus digit!! */
282 return (0);
283 }
284
285 /* Compensate for ordering of digits. */
286 if (i & 1) {
287 myla[i >> 1] = digit;
288 digit = 0;
289 } else
290 digit <<= 4;
291 }
292
293 return (1);
294 }
295
296 int
297 sm_pcmcia_funce_enaddr(parent, myla)
298 struct device *parent;
299 u_int8_t *myla;
300 {
301
302 return (pcmcia_scan_cis(parent, sm_pcmcia_lannid_ciscallback, myla));
303 }
304
305 int
306 sm_pcmcia_lannid_ciscallback(tuple, arg)
307 struct pcmcia_tuple *tuple;
308 void *arg;
309 {
310 u_int8_t *myla = arg;
311 int i;
312
313 if (tuple->code == PCMCIA_CISTPL_FUNCE) {
314 /* subcode, length */
315 if (tuple->length < 2)
316 return (0);
317
318 if ((pcmcia_tuple_read_1(tuple, 0) !=
319 PCMCIA_TPLFE_TYPE_LAN_NID) ||
320 (pcmcia_tuple_read_1(tuple, 1) != ETHER_ADDR_LEN))
321 return (0);
322
323 for (i = 0; i < ETHER_ADDR_LEN; i++)
324 myla[i] = pcmcia_tuple_read_1(tuple, i + 2);
325 return (1);
326 }
327 return (0);
328 }
329
330 int
331 sm_pcmcia_enable(sc)
332 struct smc91cxx_softc *sc;
333 {
334 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
335 int rv;
336
337 if (sc->sc_enabled != 0)
338 return 0;
339
340 /* Establish the interrupt handler. */
341 psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, smc91cxx_intr,
342 sc);
343 if (psc->sc_ih == NULL) {
344 printf("%s: couldn't establish interrupt handler\n",
345 sc->sc_dev.dv_xname);
346 return (1);
347 }
348
349 rv = pcmcia_function_enable(psc->sc_pf);
350 sc->sc_enabled = 1;
351 return (rv);
352 }
353
354 void
355 sm_pcmcia_disable(sc)
356 struct smc91cxx_softc *sc;
357 {
358 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
359
360 if (sc->sc_enabled == 0)
361 return;
362
363 pcmcia_function_disable(psc->sc_pf);
364
365 pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
366 sc->sc_enabled = 0;
367 }
368