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