xref: /openbsd-src/sys/dev/pcmcia/if_sm_pcmcia.c (revision f1dd7b858388b4a23f4f67a4957ec5ff656ebbe8)
1 /*	$OpenBSD: if_sm_pcmcia.c,v 1.38 2021/03/07 06:20:09 jsg 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_media.h>
49 
50 #include <netinet/in.h>
51 #include <netinet/if_ether.h>
52 
53 #if NBPFILTER > 0
54 #include <net/bpf.h>
55 #endif
56 
57 #include <machine/intr.h>
58 #include <machine/bus.h>
59 
60 #include <dev/mii/mii.h>
61 #include <dev/mii/miivar.h>
62 
63 #include <dev/ic/smc91cxxvar.h>
64 
65 #include <dev/pcmcia/pcmciareg.h>
66 #include <dev/pcmcia/pcmciavar.h>
67 #include <dev/pcmcia/pcmciadevs.h>
68 
69 int	sm_pcmcia_match(struct device *, void *, void *);
70 void	sm_pcmcia_attach(struct device *, struct device *, void *);
71 int	sm_pcmcia_detach(struct device *, int);
72 int	sm_pcmcia_activate(struct device *, int);
73 
74 struct sm_pcmcia_softc {
75 	struct	smc91cxx_softc sc_smc;		/* real "smc" softc */
76 
77 	/* PCMCIA-specific goo. */
78 	struct	pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
79 	int	sc_io_window;			/* our i/o window */
80 	void	*sc_ih;				/* interrupt cookie */
81 	struct	pcmcia_function *sc_pf;		/* our PCMCIA function */
82 };
83 
84 struct cfattach sm_pcmcia_ca = {
85 	sizeof(struct sm_pcmcia_softc), sm_pcmcia_match, sm_pcmcia_attach,
86 	sm_pcmcia_detach, sm_pcmcia_activate
87 };
88 
89 int	sm_pcmcia_enable(struct smc91cxx_softc *);
90 void	sm_pcmcia_disable(struct smc91cxx_softc *);
91 
92 int	sm_pcmcia_ascii_enaddr(const char *, u_int8_t *);
93 int	sm_pcmcia_funce_enaddr(struct device *, u_int8_t *);
94 
95 int	sm_pcmcia_lannid_ciscallback(struct pcmcia_tuple *, void *);
96 
97 struct sm_pcmcia_product {
98 	u_int16_t	spp_vendor;	/* vendor ID */
99 	u_int16_t	spp_product;	/* product ID */
100 	int		spp_expfunc;	/* expected function */
101 } sm_pcmcia_prod[] = {
102 	{ PCMCIA_VENDOR_MEGAHERTZ2,	PCMCIA_PRODUCT_MEGAHERTZ2_XJACK,
103 	  0 },
104 	{ PCMCIA_VENDOR_MEGAHERTZ2,	PCMCIA_PRODUCT_MEGAHERTZ2_XJEM1144,
105 	  0 },
106 	{ PCMCIA_VENDOR_NEWMEDIA,	PCMCIA_PRODUCT_NEWMEDIA_BASICS,
107 	  0 },
108 	{ PCMCIA_VENDOR_SMC,		PCMCIA_PRODUCT_SMC_8020,
109 	  0 },
110 	{ PCMCIA_VENDOR_PSION,		PCMCIA_PRODUCT_PSION_GOLDCARD,
111 	  0 }
112 };
113 
114 int
115 sm_pcmcia_match(struct device *parent, void *match, void *aux)
116 {
117 	struct pcmcia_attach_args *pa = aux;
118 	int i;
119 
120 	for (i = 0; i < nitems(sm_pcmcia_prod); i++)
121 		if (pa->manufacturer == sm_pcmcia_prod[i].spp_vendor &&
122 		    pa->product == sm_pcmcia_prod[i].spp_product &&
123 		    pa->pf->number == sm_pcmcia_prod[i].spp_expfunc)
124 			return (1);
125 	return (0);
126 }
127 
128 void
129 sm_pcmcia_attach(struct device *parent, struct device *self, 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 *intrstr;
137 
138 	psc->sc_pf = pa->pf;
139 	cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head);
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 #ifdef notyet
161 	sc->sc_enable = sm_pcmcia_enable;
162 	sc->sc_disable = sm_pcmcia_disable;
163 #endif
164 	sc->sc_enabled = 1;
165 
166 	if (pcmcia_io_map(pa->pf, (cfe->flags & PCMCIA_CFE_IO16) ?
167 	    PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_IO8, 0, cfe->iospace[0].length,
168 	    &psc->sc_pcioh, &psc->sc_io_window)) {
169 		printf(": can't map i/o space\n");
170 		return;
171 	}
172 
173 	printf(" port 0x%lx/%lu", psc->sc_pcioh.addr,
174 	    (u_long)psc->sc_pcioh.size);
175 
176 	/*
177 	 * First try to get the Ethernet address from FUNCE/LANNID tuple.
178 	 */
179 	if (sm_pcmcia_funce_enaddr(parent, myla))
180 		enaddr = myla;
181 
182 	/*
183 	 * If that failed, try one of the CIS info strings.
184 	 */
185 	if (enaddr == NULL) {
186 		char *cisstr = NULL;
187 
188 		switch (pa->manufacturer) {
189 		case PCMCIA_VENDOR_MEGAHERTZ2:
190 			cisstr = pa->pf->sc->card.cis1_info[3];
191 			break;
192 		case PCMCIA_VENDOR_SMC:
193 			cisstr = pa->pf->sc->card.cis1_info[2];
194 			break;
195 		}
196 		if (cisstr != NULL && sm_pcmcia_ascii_enaddr(cisstr, myla))
197 			enaddr = myla;
198 	}
199 
200 	if (enaddr == NULL)
201 		printf(", unable to get Ethernet address\n");
202 
203 	psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET,
204 	    smc91cxx_intr, sc, sc->sc_dev.dv_xname);
205 	intrstr = pcmcia_intr_string(psc->sc_pf, psc->sc_ih);
206 	if (*intrstr)
207 		printf(", %s", intrstr);
208 
209 	/* Perform generic initialization. */
210 	smc91cxx_attach(sc, enaddr);
211 
212 #ifdef notyet
213 	pcmcia_function_disable(pa->pf);
214 #endif
215 }
216 
217 int
218 sm_pcmcia_detach(struct device *dev, int flags)
219 {
220 	struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)dev;
221 	struct ifnet *ifp = &psc->sc_smc.sc_arpcom.ac_if;
222 	int rv = 0;
223 
224 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
225 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
226 
227 	ether_ifdetach(ifp);
228 	if_detach(ifp);
229 
230 	return (rv);
231 }
232 
233 int
234 sm_pcmcia_activate(struct device *dev, int act)
235 {
236 	struct sm_pcmcia_softc *sc = (struct sm_pcmcia_softc *)dev;
237 	struct ifnet *ifp = &sc->sc_smc.sc_arpcom.ac_if;
238 
239 	switch (act) {
240 	case DVACT_DEACTIVATE:
241 		ifp->if_timer = 0;
242 		if (ifp->if_flags & IFF_RUNNING)
243 			smc91cxx_stop(&sc->sc_smc);
244 		if (sc->sc_ih)
245 			pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
246 		sc->sc_ih = NULL;
247 		pcmcia_function_disable(sc->sc_pf);
248 		break;
249 	}
250 	return (0);
251 }
252 
253 int
254 sm_pcmcia_ascii_enaddr(const char *cisstr, u_int8_t *myla)
255 {
256 	char enaddr_str[12];
257 	int i, j;
258 
259 	if (strlen(cisstr) != 12) {
260 		/* Bogus address! */
261 		return (0);
262 	}
263 	bcopy(cisstr, enaddr_str, sizeof enaddr_str);
264 	for (i = 0; i < 6; i++) {
265 		for (j = 0; j < 2; j++) {
266 			/* Convert to upper case. */
267 			if (enaddr_str[(i * 2) + j] >= 'a' &&
268 			    enaddr_str[(i * 2) + j] <= 'z')
269 				enaddr_str[(i * 2) + j] -= 'a' - 'A';
270 
271 			/* Parse the digit. */
272 			if (enaddr_str[(i * 2) + j] >= '0' &&
273 			    enaddr_str[(i * 2) + j] <= '9')
274 				myla[i] |= enaddr_str[(i * 2) + j]
275 				    - '0';
276 			else if (enaddr_str[(i * 2) + j] >= 'A' &&
277 				 enaddr_str[(i * 2) + j] <= 'F')
278 				myla[i] |= enaddr_str[(i * 2) + j]
279 				    - 'A' + 10;
280 			else {
281 				/* Bogus digit!! */
282 				return (0);
283 			}
284 
285 			/* Compensate for ordering of digits. */
286 			if (j == 0)
287 				myla[i] <<= 4;
288 		}
289 	}
290 
291 	return (1);
292 }
293 
294 int
295 sm_pcmcia_funce_enaddr(struct device *parent, 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(struct pcmcia_tuple *tuple, void *arg)
303 {
304 	u_int8_t *myla = arg;
305 	int i;
306 
307 	if (tuple->code == PCMCIA_CISTPL_FUNCE || tuple->code ==
308 	    PCMCIA_CISTPL_SPCL) {
309 		/* subcode, length */
310 		if (tuple->length < 2)
311 			return (0);
312 
313 		if ((pcmcia_tuple_read_1(tuple, 0) !=
314 		     PCMCIA_TPLFE_TYPE_LAN_NID) ||
315 		    (pcmcia_tuple_read_1(tuple, 1) != ETHER_ADDR_LEN))
316 			return (0);
317 
318 		for (i = 0; i < ETHER_ADDR_LEN; i++)
319 			myla[i] = pcmcia_tuple_read_1(tuple, i + 2);
320 		return (1);
321 	}
322 	return (0);
323 }
324 
325 int
326 sm_pcmcia_enable(struct smc91cxx_softc *sc)
327 {
328 	struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
329 
330 	/* Establish the interrupt handler. */
331 	psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, smc91cxx_intr,
332 	    sc, sc->sc_dev.dv_xname);
333 	if (psc->sc_ih == NULL) {
334 		printf("%s: couldn't establish interrupt handler\n",
335 		    sc->sc_dev.dv_xname);
336 		return (1);
337 	}
338 
339 	return (pcmcia_function_enable(psc->sc_pf));
340 }
341 
342 void
343 sm_pcmcia_disable(struct smc91cxx_softc *sc)
344 {
345 	struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
346 
347 	pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
348 	pcmcia_function_disable(psc->sc_pf);
349 }
350