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