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