if_sm_pcmcia.c revision 1.6 1 /* $NetBSD: if_sm_pcmcia.c,v 1.6 1998/07/19 17:28:16 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1997 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
108 sm_pcmcia_match(parent, match, aux)
109 struct device *parent;
110 struct cfdata *match;
111 void *aux;
112 {
113 struct pcmcia_attach_args *pa = aux;
114
115 if (pa->manufacturer == PCMCIA_VENDOR_MEGAHERTZ2) {
116 switch (pa->product) {
117 case PCMCIA_PRODUCT_MEGAHERTZ2_XJACK:
118 if (pa->pf->number == 0)
119 return (1);
120 }
121 }
122
123 return (0);
124 }
125
126 void
127 sm_pcmcia_attach(parent, self, aux)
128 struct device *parent, *self;
129 void *aux;
130 {
131 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)self;
132 struct smc91cxx_softc *sc = &psc->sc_smc;
133 struct pcmcia_attach_args *pa = aux;
134 struct pcmcia_config_entry *cfe;
135 u_int8_t myla[ETHER_ADDR_LEN], *enaddr = NULL;
136 const char *model;
137
138 psc->sc_pf = pa->pf;
139 cfe = pa->pf->cfe_head.sqh_first;
140
141 /* Enable the card. */
142 pcmcia_function_init(pa->pf, cfe);
143 if (pcmcia_function_enable(pa->pf)) {
144 printf(": function enable failed\n");
145 return;
146 }
147
148 /* XXX sanity check number of mem and i/o spaces */
149
150 /* Allocate and map i/o space for the card. */
151 if (pcmcia_io_alloc(pa->pf, 0, cfe->iospace[0].length,
152 cfe->iospace[0].length, &psc->sc_pcioh)) {
153 printf(": can't allocate i/o space\n");
154 return;
155 }
156
157 sc->sc_bst = psc->sc_pcioh.iot;
158 sc->sc_bsh = psc->sc_pcioh.ioh;
159
160 sc->sc_enable = sm_pcmcia_enable;
161 sc->sc_disable = sm_pcmcia_disable;
162
163 if (pcmcia_io_map(pa->pf, (cfe->flags & PCMCIA_CFE_IO16) ?
164 PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_IO8, 0, cfe->iospace[0].length,
165 &psc->sc_pcioh, &psc->sc_io_window)) {
166 printf(": can't map i/o space\n");
167 return;
168 }
169
170 switch (pa->product) {
171 case PCMCIA_PRODUCT_MEGAHERTZ2_XJACK:
172 model = PCMCIA_STR_MEGAHERTZ2_XJACK;
173 break;
174
175 default:
176 model = "Unknown SMC91Cxx Ethernet";
177 }
178 printf(": %s\n", model);
179
180 if (pa->product == PCMCIA_PRODUCT_MEGAHERTZ2_XJACK) {
181 char enaddr_str[12];
182 int i = 0, j;
183
184 /*
185 * The X-JACK's Ethernet address is stored in the fourth
186 * CIS info string. We need to parse it and pass it to
187 * the generic layer.
188 */
189 if (strlen(pa->pf->sc->card.cis1_info[3]) != 12) {
190 /* Bogus address! */
191 goto out;
192 }
193 bcopy(pa->pf->sc->card.cis1_info[3], enaddr_str, 12);
194 bzero(myla, sizeof(myla));
195 for (i = 0; i < 6; i++) {
196 for (j = 0; j < 2; j++) {
197 /* Convert to upper case. */
198 if (enaddr_str[(i * 2) + j] >= 'a' &&
199 enaddr_str[(i * 2) + j] <= 'z')
200 enaddr_str[(i * 2) + j] -= 'a' - 'A';
201
202 /* Parse the digit. */
203 if (enaddr_str[(i * 2) + j] >= '0' &&
204 enaddr_str[(i * 2) + j] <= '9')
205 myla[i] |= enaddr_str[(i * 2) + j]
206 - '0';
207 else if (enaddr_str[(i * 2) + j] >= 'A' &&
208 enaddr_str[(i * 2) + j] <= 'F')
209 myla[i] |= enaddr_str[(i * 2) + j]
210 - 'A' + 10;
211 else {
212 /* Bogus digit!! */
213 goto out;
214 }
215
216 /* Compensate for ordering of digits. */
217 if (j == 0)
218 myla[i] <<= 4;
219 }
220 }
221
222 out:
223 if (i >= 6) {
224 /* Successfully parsed. */
225 enaddr = myla;
226 } else {
227 printf("%s: unable to read MAC address from CIS\n",
228 sc->sc_dev.dv_xname);
229 }
230 }
231
232 /* Perform generic intialization. */
233 smc91cxx_attach(sc, enaddr);
234
235 pcmcia_function_disable(pa->pf);
236 }
237
238 int
239 sm_pcmcia_enable(sc)
240 struct smc91cxx_softc *sc;
241 {
242 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
243
244 /* Establish the interrupt handler. */
245 psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, smc91cxx_intr,
246 sc);
247 if (psc->sc_ih == NULL) {
248 printf("%s: couldn't establish interrupt handler\n",
249 sc->sc_dev.dv_xname);
250 return (1);
251 }
252
253 return (pcmcia_function_enable(psc->sc_pf));
254 }
255
256 void
257 sm_pcmcia_disable(sc)
258 struct smc91cxx_softc *sc;
259 {
260 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
261
262 pcmcia_function_disable(psc->sc_pf);
263
264 pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
265 }
266