if_sm_pcmcia.c revision 1.18 1 /* $NetBSD: if_sm_pcmcia.c,v 1.18 1999/09/28 23:20:42 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 1998 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
101 struct cfattach sm_pcmcia_ca = {
102 sizeof(struct sm_pcmcia_softc), sm_pcmcia_match, sm_pcmcia_attach,
103 sm_pcmcia_detach, smc91cxx_activate
104 };
105
106 int sm_pcmcia_enable __P((struct smc91cxx_softc *));
107 void sm_pcmcia_disable __P((struct smc91cxx_softc *));
108
109 int sm_pcmcia_ascii_enaddr __P((const char *, u_int8_t *));
110 int sm_pcmcia_funce_enaddr __P((struct device *, u_int8_t *));
111
112 int sm_pcmcia_lannid_ciscallback __P((struct pcmcia_tuple *, void *));
113
114 struct sm_pcmcia_product {
115 u_int32_t spp_vendor; /* vendor ID */
116 u_int32_t spp_product; /* product ID */
117 int spp_expfunc; /* expected function */
118 const char *spp_name; /* product name */
119 } sm_pcmcia_products[] = {
120 { PCMCIA_VENDOR_MEGAHERTZ2, PCMCIA_PRODUCT_MEGAHERTZ2_XJACK,
121 0, PCMCIA_STR_MEGAHERTZ2_XJACK },
122
123 { PCMCIA_VENDOR_NEWMEDIA, PCMCIA_PRODUCT_NEWMEDIA_BASICS,
124 0, PCMCIA_STR_NEWMEDIA_BASICS },
125
126 #if 0
127 { PCMCIA_VENDOR_SMC, PCMCIA_PRODUCT_SMC_8020BT,
128 0, PCMCIA_STR_SMC_8020BT },
129 #endif
130
131 { 0, 0,
132 0, NULL },
133 };
134
135 struct sm_pcmcia_product *sm_pcmcia_lookup __P((struct pcmcia_attach_args *));
136
137 struct sm_pcmcia_product *
138 sm_pcmcia_lookup(pa)
139 struct pcmcia_attach_args *pa;
140 {
141 struct sm_pcmcia_product *spp;
142
143 for (spp = sm_pcmcia_products; spp->spp_name != NULL; spp++)
144 if (pa->manufacturer == spp->spp_vendor &&
145 pa->product == spp->spp_product &&
146 pa->pf->number == spp->spp_expfunc)
147 return (spp);
148 return (NULL);
149 }
150
151 int
152 sm_pcmcia_match(parent, match, aux)
153 struct device *parent;
154 struct cfdata *match;
155 void *aux;
156 {
157 struct pcmcia_attach_args *pa = aux;
158
159 if (sm_pcmcia_lookup(pa) != NULL)
160 return (1);
161 return (0);
162 }
163
164 void
165 sm_pcmcia_attach(parent, self, aux)
166 struct device *parent, *self;
167 void *aux;
168 {
169 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)self;
170 struct smc91cxx_softc *sc = &psc->sc_smc;
171 struct pcmcia_attach_args *pa = aux;
172 struct pcmcia_config_entry *cfe;
173 u_int8_t myla[ETHER_ADDR_LEN], *enaddr = NULL;
174 struct sm_pcmcia_product *spp;
175
176 psc->sc_pf = pa->pf;
177 cfe = pa->pf->cfe_head.sqh_first;
178
179 /* Enable the card. */
180 pcmcia_function_init(pa->pf, cfe);
181 if (pcmcia_function_enable(pa->pf)) {
182 printf(": function enable failed\n");
183 return;
184 }
185
186 /* XXX sanity check number of mem and i/o spaces */
187
188 /* Allocate and map i/o space for the card. */
189 if (pcmcia_io_alloc(pa->pf, 0, cfe->iospace[0].length,
190 cfe->iospace[0].length, &psc->sc_pcioh)) {
191 printf(": can't allocate i/o space\n");
192 return;
193 }
194
195 sc->sc_bst = psc->sc_pcioh.iot;
196 sc->sc_bsh = psc->sc_pcioh.ioh;
197
198 sc->sc_enable = sm_pcmcia_enable;
199 sc->sc_disable = sm_pcmcia_disable;
200
201 if (pcmcia_io_map(pa->pf, (cfe->flags & PCMCIA_CFE_IO16) ?
202 PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_IO8, 0, cfe->iospace[0].length,
203 &psc->sc_pcioh, &psc->sc_io_window)) {
204 printf(": can't map i/o space\n");
205 return;
206 }
207
208 spp = sm_pcmcia_lookup(pa);
209 if (spp == NULL)
210 panic("sm_pcmcia_attach: impossible");
211
212 printf(": %s\n", spp->spp_name);
213
214 /*
215 * First try to get the Ethernet address from FUNCE/LANNID tuple.
216 */
217 if (sm_pcmcia_funce_enaddr(parent, myla))
218 enaddr = myla;
219
220 /*
221 * If that failed, try one of the CIS info strings.
222 */
223 if (enaddr == NULL) {
224 char *cisstr = NULL;
225
226 switch (pa->manufacturer) {
227 case PCMCIA_VENDOR_MEGAHERTZ:
228 case PCMCIA_VENDOR_MEGAHERTZ2:
229 cisstr = pa->pf->sc->card.cis1_info[3];
230 break;
231
232 case PCMCIA_VENDOR_SMC:
233 cisstr = pa->pf->sc->card.cis1_info[2];
234 break;
235 }
236
237 if (cisstr != NULL && sm_pcmcia_ascii_enaddr(cisstr, myla))
238 enaddr = myla;
239 }
240
241 if (enaddr == NULL)
242 printf("%s: unable to get Ethernet address\n",
243 sc->sc_dev.dv_xname);
244
245 /* Perform generic intialization. */
246 smc91cxx_attach(sc, enaddr);
247
248 pcmcia_function_disable(pa->pf);
249 }
250
251 int
252 sm_pcmcia_detach(self, flags)
253 struct device *self;
254 int flags;
255 {
256 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)self;
257
258 /* Unmap our i/o window. */
259 pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
260
261 /* Free our i/o space. */
262 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
263
264 #ifdef notyet
265 /*
266 * Our softc is about to go away, so drop our reference
267 * to the ifnet.
268 */
269 if_delref(psc->sc_smc.sc_ec.ec_if);
270 return (0);
271 #else
272 return (EBUSY);
273 #endif
274 }
275
276 int
277 sm_pcmcia_ascii_enaddr(cisstr, myla)
278 const char *cisstr;
279 u_int8_t *myla;
280 {
281 u_int8_t digit;
282 int i;
283
284 memset(myla, 0, ETHER_ADDR_LEN);
285
286 for (i = 0, digit = 0; i < (ETHER_ADDR_LEN * 2); i++) {
287 if (cisstr[i] >= '0' && cisstr[i] <= '9')
288 digit |= cisstr[i] - '0';
289 else if (cisstr[i] >= 'a' && cisstr[i] <= 'f')
290 digit |= (cisstr[i] - 'a') + 10;
291 else if (cisstr[i] >= 'A' && cisstr[i] <= 'F')
292 digit |= (cisstr[i] - 'A') + 10;
293 else {
294 /* Bogus digit!! */
295 return (0);
296 }
297
298 /* Compensate for ordering of digits. */
299 if (i & 1) {
300 myla[i >> 1] = digit;
301 digit = 0;
302 } else
303 digit <<= 4;
304 }
305
306 return (1);
307 }
308
309 int
310 sm_pcmcia_funce_enaddr(parent, myla)
311 struct device *parent;
312 u_int8_t *myla;
313 {
314
315 return (pcmcia_scan_cis(parent, sm_pcmcia_lannid_ciscallback, myla));
316 }
317
318 int
319 sm_pcmcia_lannid_ciscallback(tuple, arg)
320 struct pcmcia_tuple *tuple;
321 void *arg;
322 {
323 u_int8_t *myla = arg;
324 int i;
325
326 if (tuple->code == PCMCIA_CISTPL_FUNCE) {
327 /* subcode, length */
328 if (tuple->length < 2)
329 return (0);
330
331 if ((pcmcia_tuple_read_1(tuple, 0) !=
332 PCMCIA_TPLFE_TYPE_LAN_NID) ||
333 (pcmcia_tuple_read_1(tuple, 1) != ETHER_ADDR_LEN))
334 return (0);
335
336 for (i = 0; i < ETHER_ADDR_LEN; i++)
337 myla[i] = pcmcia_tuple_read_1(tuple, i + 2);
338 return (1);
339 }
340 return (0);
341 }
342
343 int
344 sm_pcmcia_enable(sc)
345 struct smc91cxx_softc *sc;
346 {
347 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
348
349 /* Establish the interrupt handler. */
350 psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, smc91cxx_intr,
351 sc);
352 if (psc->sc_ih == NULL) {
353 printf("%s: couldn't establish interrupt handler\n",
354 sc->sc_dev.dv_xname);
355 return (1);
356 }
357
358 return (pcmcia_function_enable(psc->sc_pf));
359 }
360
361 void
362 sm_pcmcia_disable(sc)
363 struct smc91cxx_softc *sc;
364 {
365 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
366
367 pcmcia_function_disable(psc->sc_pf);
368
369 pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
370 }
371