if_sm_pcmcia.c revision 1.4 1 /* $NetBSD: if_sm_pcmcia.c,v 1.4 1998/07/05 00:51:24 jonathan 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 "bpfilter.h"
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/mbuf.h>
46 #include <sys/socket.h>
47 #include <sys/ioctl.h>
48 #include <sys/errno.h>
49 #include <sys/syslog.h>
50 #include <sys/select.h>
51 #include <sys/device.h>
52
53 #include <net/if.h>
54 #include <net/if_dl.h>
55 #include <net/if_ether.h>
56 #include <net/if_media.h>
57
58 #ifdef INET
59 #include <netinet/in.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/in_var.h>
62 #include <netinet/ip.h>
63 #include <netinet/if_inarp.h>
64 #endif
65
66 #ifdef NS
67 #include <netns/ns.h>
68 #include <netns/ns_if.h>
69 #endif
70
71 #if NBPFILTER > 0
72 #include <net/bpf.h>
73 #include <net/bpfdesc.h>
74 #endif
75
76 #include <machine/intr.h>
77 #include <machine/bus.h>
78
79 #include <dev/ic/smc91cxxreg.h>
80 #include <dev/ic/smc91cxxvar.h>
81
82 #include <dev/pcmcia/pcmciareg.h>
83 #include <dev/pcmcia/pcmciavar.h>
84
85 #define PCMCIA_MANUFACTURER_MEGAHERTZ 0x128
86 #define PCMCIA_PRODUCT_MEGAHERTZ_XJACK 0x103
87
88 int sm_pcmcia_match __P((struct device *, struct cfdata *, void *));
89 void sm_pcmcia_attach __P((struct device *, struct device *, void *));
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 };
104
105 int sm_pcmcia_enable __P((struct smc91cxx_softc *));
106 void sm_pcmcia_disable __P((struct smc91cxx_softc *));
107
108 int
109 sm_pcmcia_match(parent, match, aux)
110 struct device *parent;
111 struct cfdata *match;
112 void *aux;
113 {
114 struct pcmcia_attach_args *pa = aux;
115
116 if (pa->manufacturer == PCMCIA_MANUFACTURER_MEGAHERTZ) {
117 switch (pa->product) {
118 case PCMCIA_PRODUCT_MEGAHERTZ_XJACK:
119 if (pa->pf->number == 0)
120 return (1);
121 }
122 }
123
124 return (0);
125 }
126
127 void
128 sm_pcmcia_attach(parent, self, aux)
129 struct device *parent, *self;
130 void *aux;
131 {
132 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)self;
133 struct smc91cxx_softc *sc = &psc->sc_smc;
134 struct pcmcia_attach_args *pa = aux;
135 struct pcmcia_config_entry *cfe;
136 u_int8_t myla[ETHER_ADDR_LEN], *enaddr = NULL;
137 const char *model;
138
139 psc->sc_pf = pa->pf;
140 cfe = pa->pf->cfe_head.sqh_first;
141
142 /* Enable the card. */
143 pcmcia_function_init(pa->pf, cfe);
144 if (pcmcia_function_enable(pa->pf)) {
145 printf(": function enable failed\n");
146 return;
147 }
148
149 /* XXX sanity check number of mem and i/o spaces */
150
151 /* Allocate and map i/o space for the card. */
152 if (pcmcia_io_alloc(pa->pf, 0, cfe->iospace[0].length,
153 cfe->iospace[0].length, &psc->sc_pcioh)) {
154 printf(": can't allocate i/o space\n");
155 return;
156 }
157
158 sc->sc_bst = psc->sc_pcioh.iot;
159 sc->sc_bsh = psc->sc_pcioh.ioh;
160
161 sc->sc_enable = sm_pcmcia_enable;
162 sc->sc_disable = sm_pcmcia_disable;
163
164 if (pcmcia_io_map(pa->pf, (cfe->flags & PCMCIA_CFE_IO16) ?
165 PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_IO8, 0, cfe->iospace[0].length,
166 &psc->sc_pcioh, &psc->sc_io_window)) {
167 printf(": can't map i/o space\n");
168 return;
169 }
170
171 switch (pa->product) {
172 case PCMCIA_PRODUCT_MEGAHERTZ_XJACK:
173 model = "Megahertz X-JACK Ethernet";
174 break;
175
176 default:
177 model = "Unknown SMC91Cxx Ethernet";
178 }
179 printf(": %s\n", model);
180
181 if (pa->product == PCMCIA_PRODUCT_MEGAHERTZ_XJACK) {
182 char enaddr_str[12];
183 int i = 0, j;
184
185 /*
186 * The X-JACK's Ethernet address is stored in the fourth
187 * CIS info string. We need to parse it and pass it to
188 * the generic layer.
189 */
190 if (strlen(pa->pf->sc->card.cis1_info[3]) != 12) {
191 /* Bogus address! */
192 goto out;
193 }
194 bcopy(pa->pf->sc->card.cis1_info[3], enaddr_str, 12);
195 bzero(myla, sizeof(myla));
196 for (i = 0; i < 6; i++) {
197 for (j = 0; j < 2; j++) {
198 /* Convert to upper case. */
199 if (enaddr_str[(i * 2) + j] >= 'a' &&
200 enaddr_str[(i * 2) + j] <= 'z')
201 enaddr_str[(i * 2) + j] -= 'a' - 'A';
202
203 /* Parse the digit. */
204 if (enaddr_str[(i * 2) + j] >= '0' &&
205 enaddr_str[(i * 2) + j] <= '9')
206 myla[i] |= enaddr_str[(i * 2) + j]
207 - '0';
208 else if (enaddr_str[(i * 2) + j] >= 'A' &&
209 enaddr_str[(i * 2) + j] <= 'F')
210 myla[i] |= enaddr_str[(i * 2) + j]
211 - 'A' + 10;
212 else {
213 /* Bogus digit!! */
214 goto out;
215 }
216
217 /* Compensate for ordering of digits. */
218 if (j == 0)
219 myla[i] <<= 4;
220 }
221 }
222
223 out:
224 if (i >= 6) {
225 /* Successfully parsed. */
226 enaddr = myla;
227 } else {
228 printf("%s: unable to read MAC address from CIS\n",
229 sc->sc_dev.dv_xname);
230 }
231 }
232
233 /* Perform generic intialization. */
234 smc91cxx_attach(sc, enaddr);
235
236 pcmcia_function_disable(pa->pf);
237 }
238
239 int
240 sm_pcmcia_enable(sc)
241 struct smc91cxx_softc *sc;
242 {
243 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
244
245 /* Establish the interrupt handler. */
246 psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, smc91cxx_intr,
247 sc);
248 if (psc->sc_ih == NULL) {
249 printf("%s: couldn't establish interrupt handler\n",
250 sc->sc_dev.dv_xname);
251 return (1);
252 }
253
254 return (pcmcia_function_enable(psc->sc_pf));
255 }
256
257 void
258 sm_pcmcia_disable(sc)
259 struct smc91cxx_softc *sc;
260 {
261 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
262
263 pcmcia_function_disable(psc->sc_pf);
264
265 pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
266 }
267