xref: /openbsd-src/sys/dev/pcmcia/if_sm_pcmcia.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: if_sm_pcmcia.c,v 1.33 2013/08/07 01:06:40 bluhm Exp $	*/
2 /*	$NetBSD: if_sm_pcmcia.c,v 1.11 1998/08/15 20:47:32 thorpej Exp $  */
3 
4 /*-
5  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
10  * NASA Ames Research Center.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "bpfilter.h"
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/mbuf.h>
39 #include <sys/socket.h>
40 #include <sys/ioctl.h>
41 #include <sys/errno.h>
42 #include <sys/syslog.h>
43 #include <sys/selinfo.h>
44 #include <sys/timeout.h>
45 #include <sys/device.h>
46 
47 #include <net/if.h>
48 #include <net/if_dl.h>
49 #include <net/if_media.h>
50 
51 #ifdef INET
52 #include <netinet/in.h>
53 #include <netinet/in_systm.h>
54 #include <netinet/ip.h>
55 #include <netinet/if_ether.h>
56 #endif
57 
58 #if NBPFILTER > 0
59 #include <net/bpf.h>
60 #endif
61 
62 #include <machine/intr.h>
63 #include <machine/bus.h>
64 
65 #include <net/if_media.h>
66 
67 #include <dev/mii/mii.h>
68 #include <dev/mii/miivar.h>
69 
70 #include <dev/ic/smc91cxxreg.h>
71 #include <dev/ic/smc91cxxvar.h>
72 
73 #include <dev/pcmcia/pcmciareg.h>
74 #include <dev/pcmcia/pcmciavar.h>
75 #include <dev/pcmcia/pcmciadevs.h>
76 
77 int	sm_pcmcia_match(struct device *, void *, void *);
78 void	sm_pcmcia_attach(struct device *, struct device *, void *);
79 int	sm_pcmcia_detach(struct device *, int);
80 int	sm_pcmcia_activate(struct device *, int);
81 
82 struct sm_pcmcia_softc {
83 	struct	smc91cxx_softc sc_smc;		/* real "smc" softc */
84 
85 	/* PCMCIA-specific goo. */
86 	struct	pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
87 	int	sc_io_window;			/* our i/o window */
88 	void	*sc_ih;				/* interrupt cookie */
89 	struct	pcmcia_function *sc_pf;		/* our PCMCIA function */
90 };
91 
92 struct cfattach sm_pcmcia_ca = {
93 	sizeof(struct sm_pcmcia_softc), sm_pcmcia_match, sm_pcmcia_attach,
94 	sm_pcmcia_detach, sm_pcmcia_activate
95 };
96 
97 int	sm_pcmcia_enable(struct smc91cxx_softc *);
98 void	sm_pcmcia_disable(struct smc91cxx_softc *);
99 
100 int	sm_pcmcia_ascii_enaddr(const char *, u_int8_t *);
101 int	sm_pcmcia_funce_enaddr(struct device *, u_int8_t *);
102 
103 int	sm_pcmcia_lannid_ciscallback(struct pcmcia_tuple *, void *);
104 
105 struct sm_pcmcia_product {
106 	u_int16_t	spp_vendor;	/* vendor ID */
107 	u_int16_t	spp_product;	/* product ID */
108 	int		spp_expfunc;	/* expected function */
109 } sm_pcmcia_prod[] = {
110 	{ PCMCIA_VENDOR_MEGAHERTZ2,	PCMCIA_PRODUCT_MEGAHERTZ2_XJACK,
111 	  0 },
112 	{ PCMCIA_VENDOR_MEGAHERTZ2,	PCMCIA_PRODUCT_MEGAHERTZ2_XJEM1144,
113 	  0 },
114 	{ PCMCIA_VENDOR_NEWMEDIA,	PCMCIA_PRODUCT_NEWMEDIA_BASICS,
115 	  0 },
116 	{ PCMCIA_VENDOR_SMC,		PCMCIA_PRODUCT_SMC_8020,
117 	  0 },
118 	{ PCMCIA_VENDOR_PSION,		PCMCIA_PRODUCT_PSION_GOLDCARD,
119 	  0 }
120 };
121 
122 int
123 sm_pcmcia_match(parent, match, aux)
124 	struct device *parent;
125 	void *match, *aux;
126 {
127 	struct pcmcia_attach_args *pa = aux;
128 	int i;
129 
130 	for (i = 0; i < nitems(sm_pcmcia_prod); i++)
131 		if (pa->manufacturer == sm_pcmcia_prod[i].spp_vendor &&
132 		    pa->product == sm_pcmcia_prod[i].spp_product &&
133 		    pa->pf->number == sm_pcmcia_prod[i].spp_expfunc)
134 			return (1);
135 	return (0);
136 }
137 
138 void
139 sm_pcmcia_attach(parent, self, aux)
140 	struct device *parent, *self;
141 	void *aux;
142 {
143 	struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)self;
144 	struct smc91cxx_softc *sc = &psc->sc_smc;
145 	struct pcmcia_attach_args *pa = aux;
146 	struct pcmcia_config_entry *cfe;
147 	u_int8_t myla[ETHER_ADDR_LEN], *enaddr = NULL;
148 	const char *intrstr;
149 
150 	psc->sc_pf = pa->pf;
151 	cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head);
152 
153 	/* Enable the card. */
154 	pcmcia_function_init(pa->pf, cfe);
155 	if (pcmcia_function_enable(pa->pf)) {
156 		printf(": function enable failed\n");
157 		return;
158 	}
159 
160 	/* XXX sanity check number of mem and i/o spaces */
161 
162 	/* Allocate and map i/o space for the card. */
163 	if (pcmcia_io_alloc(pa->pf, 0, cfe->iospace[0].length,
164 	    cfe->iospace[0].length, &psc->sc_pcioh)) {
165 		printf(": can't allocate i/o space\n");
166 		return;
167 	}
168 
169 	sc->sc_bst = psc->sc_pcioh.iot;
170 	sc->sc_bsh = psc->sc_pcioh.ioh;
171 
172 #ifdef notyet
173 	sc->sc_enable = sm_pcmcia_enable;
174 	sc->sc_disable = sm_pcmcia_disable;
175 #endif
176 	sc->sc_enabled = 1;
177 
178 	if (pcmcia_io_map(pa->pf, (cfe->flags & PCMCIA_CFE_IO16) ?
179 	    PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_IO8, 0, cfe->iospace[0].length,
180 	    &psc->sc_pcioh, &psc->sc_io_window)) {
181 		printf(": can't map i/o space\n");
182 		return;
183 	}
184 
185 	printf(" port 0x%lx/%lu", psc->sc_pcioh.addr,
186 	    (u_long)psc->sc_pcioh.size);
187 
188 	/*
189 	 * First try to get the Ethernet address from FUNCE/LANNID tuple.
190 	 */
191 	if (sm_pcmcia_funce_enaddr(parent, myla))
192 		enaddr = myla;
193 
194 	/*
195 	 * If that failed, try one of the CIS info strings.
196 	 */
197 	if (enaddr == NULL) {
198 		char *cisstr = NULL;
199 
200 		switch (pa->manufacturer) {
201 		case PCMCIA_VENDOR_MEGAHERTZ2:
202 			cisstr = pa->pf->sc->card.cis1_info[3];
203 			break;
204 		case PCMCIA_VENDOR_SMC:
205 			cisstr = pa->pf->sc->card.cis1_info[2];
206 			break;
207 		}
208 		if (cisstr != NULL && sm_pcmcia_ascii_enaddr(cisstr, myla))
209 			enaddr = myla;
210 	}
211 
212 	if (enaddr == NULL)
213 		printf(", unable to get Ethernet address\n");
214 
215 	psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET,
216 	    smc91cxx_intr, sc, sc->sc_dev.dv_xname);
217 	intrstr = pcmcia_intr_string(psc->sc_pf, psc->sc_ih);
218 	if (*intrstr)
219 		printf(", %s", intrstr);
220 
221 	/* Perform generic initialization. */
222 	smc91cxx_attach(sc, enaddr);
223 
224 #ifdef notyet
225 	pcmcia_function_disable(pa->pf);
226 #endif
227 }
228 
229 int
230 sm_pcmcia_detach(dev, flags)
231 	struct device *dev;
232 	int flags;
233 {
234 	struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)dev;
235 	struct ifnet *ifp = &psc->sc_smc.sc_arpcom.ac_if;
236 	int rv = 0;
237 
238 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
239 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
240 
241 	ether_ifdetach(ifp);
242 	if_detach(ifp);
243 
244 	return (rv);
245 }
246 
247 int
248 sm_pcmcia_activate(dev, act)
249 	struct device *dev;
250 	int act;
251 {
252 	struct sm_pcmcia_softc *sc = (struct sm_pcmcia_softc *)dev;
253 	struct ifnet *ifp = &sc->sc_smc.sc_arpcom.ac_if;
254 
255 	switch (act) {
256 	case DVACT_DEACTIVATE:
257 		ifp->if_timer = 0;
258 		if (ifp->if_flags & IFF_RUNNING)
259 			smc91cxx_stop(&sc->sc_smc);
260 		if (sc->sc_ih)
261 			pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
262 		sc->sc_ih = NULL;
263 		pcmcia_function_disable(sc->sc_pf);
264 		break;
265 	}
266 	return (0);
267 }
268 
269 int
270 sm_pcmcia_ascii_enaddr(cisstr, myla)
271 	const char *cisstr;
272 	u_int8_t *myla;
273 {
274 	char enaddr_str[12];
275 	int i, j;
276 
277 	if (strlen(cisstr) != 12) {
278 		/* Bogus address! */
279 		return (0);
280 	}
281 	bcopy(cisstr, enaddr_str, sizeof enaddr_str);
282 	for (i = 0; i < 6; i++) {
283 		for (j = 0; j < 2; j++) {
284 			/* Convert to upper case. */
285 			if (enaddr_str[(i * 2) + j] >= 'a' &&
286 			    enaddr_str[(i * 2) + j] <= 'z')
287 				enaddr_str[(i * 2) + j] -= 'a' - 'A';
288 
289 			/* Parse the digit. */
290 			if (enaddr_str[(i * 2) + j] >= '0' &&
291 			    enaddr_str[(i * 2) + j] <= '9')
292 				myla[i] |= enaddr_str[(i * 2) + j]
293 				    - '0';
294 			else if (enaddr_str[(i * 2) + j] >= 'A' &&
295 				 enaddr_str[(i * 2) + j] <= 'F')
296 				myla[i] |= enaddr_str[(i * 2) + j]
297 				    - 'A' + 10;
298 			else {
299 				/* Bogus digit!! */
300 				return (0);
301 			}
302 
303 			/* Compensate for ordering of digits. */
304 			if (j == 0)
305 				myla[i] <<= 4;
306 		}
307 	}
308 
309 	return (1);
310 }
311 
312 int
313 sm_pcmcia_funce_enaddr(parent, myla)
314 	struct device *parent;
315 	u_int8_t *myla;
316 {
317 
318 	return (pcmcia_scan_cis(parent, sm_pcmcia_lannid_ciscallback, myla));
319 }
320 
321 int
322 sm_pcmcia_lannid_ciscallback(tuple, arg)
323 	struct pcmcia_tuple *tuple;
324 	void *arg;
325 {
326 	u_int8_t *myla = arg;
327 	int i;
328 
329 	if (tuple->code == PCMCIA_CISTPL_FUNCE || tuple->code ==
330 	    PCMCIA_CISTPL_SPCL) {
331 		/* subcode, length */
332 		if (tuple->length < 2)
333 			return (0);
334 
335 		if ((pcmcia_tuple_read_1(tuple, 0) !=
336 		     PCMCIA_TPLFE_TYPE_LAN_NID) ||
337 		    (pcmcia_tuple_read_1(tuple, 1) != ETHER_ADDR_LEN))
338 			return (0);
339 
340 		for (i = 0; i < ETHER_ADDR_LEN; i++)
341 			myla[i] = pcmcia_tuple_read_1(tuple, i + 2);
342 		return (1);
343 	}
344 	return (0);
345 }
346 
347 int
348 sm_pcmcia_enable(sc)
349 	struct smc91cxx_softc *sc;
350 {
351 	struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
352 
353 	/* Establish the interrupt handler. */
354 	psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, smc91cxx_intr,
355 	    sc, sc->sc_dev.dv_xname);
356 	if (psc->sc_ih == NULL) {
357 		printf("%s: couldn't establish interrupt handler\n",
358 		    sc->sc_dev.dv_xname);
359 		return (1);
360 	}
361 
362 	return (pcmcia_function_enable(psc->sc_pf));
363 }
364 
365 void
366 sm_pcmcia_disable(sc)
367 	struct smc91cxx_softc *sc;
368 {
369 	struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
370 
371 	pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
372 	pcmcia_function_disable(psc->sc_pf);
373 }
374