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