xref: /openbsd/sys/dev/pcmcia/if_sm_pcmcia.c (revision 274d7c50)
1 /*	$OpenBSD: if_sm_pcmcia.c,v 1.37 2015/11/24 17:11:40 mpi 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(parent, match, aux)
116 	struct device *parent;
117 	void *match, *aux;
118 {
119 	struct pcmcia_attach_args *pa = aux;
120 	int i;
121 
122 	for (i = 0; i < nitems(sm_pcmcia_prod); i++)
123 		if (pa->manufacturer == sm_pcmcia_prod[i].spp_vendor &&
124 		    pa->product == sm_pcmcia_prod[i].spp_product &&
125 		    pa->pf->number == sm_pcmcia_prod[i].spp_expfunc)
126 			return (1);
127 	return (0);
128 }
129 
130 void
131 sm_pcmcia_attach(parent, self, aux)
132 	struct device *parent, *self;
133 	void *aux;
134 {
135 	struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)self;
136 	struct smc91cxx_softc *sc = &psc->sc_smc;
137 	struct pcmcia_attach_args *pa = aux;
138 	struct pcmcia_config_entry *cfe;
139 	u_int8_t myla[ETHER_ADDR_LEN], *enaddr = NULL;
140 	const char *intrstr;
141 
142 	psc->sc_pf = pa->pf;
143 	cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head);
144 
145 	/* Enable the card. */
146 	pcmcia_function_init(pa->pf, cfe);
147 	if (pcmcia_function_enable(pa->pf)) {
148 		printf(": function enable failed\n");
149 		return;
150 	}
151 
152 	/* XXX sanity check number of mem and i/o spaces */
153 
154 	/* Allocate and map i/o space for the card. */
155 	if (pcmcia_io_alloc(pa->pf, 0, cfe->iospace[0].length,
156 	    cfe->iospace[0].length, &psc->sc_pcioh)) {
157 		printf(": can't allocate i/o space\n");
158 		return;
159 	}
160 
161 	sc->sc_bst = psc->sc_pcioh.iot;
162 	sc->sc_bsh = psc->sc_pcioh.ioh;
163 
164 #ifdef notyet
165 	sc->sc_enable = sm_pcmcia_enable;
166 	sc->sc_disable = sm_pcmcia_disable;
167 #endif
168 	sc->sc_enabled = 1;
169 
170 	if (pcmcia_io_map(pa->pf, (cfe->flags & PCMCIA_CFE_IO16) ?
171 	    PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_IO8, 0, cfe->iospace[0].length,
172 	    &psc->sc_pcioh, &psc->sc_io_window)) {
173 		printf(": can't map i/o space\n");
174 		return;
175 	}
176 
177 	printf(" port 0x%lx/%lu", psc->sc_pcioh.addr,
178 	    (u_long)psc->sc_pcioh.size);
179 
180 	/*
181 	 * First try to get the Ethernet address from FUNCE/LANNID tuple.
182 	 */
183 	if (sm_pcmcia_funce_enaddr(parent, myla))
184 		enaddr = myla;
185 
186 	/*
187 	 * If that failed, try one of the CIS info strings.
188 	 */
189 	if (enaddr == NULL) {
190 		char *cisstr = NULL;
191 
192 		switch (pa->manufacturer) {
193 		case PCMCIA_VENDOR_MEGAHERTZ2:
194 			cisstr = pa->pf->sc->card.cis1_info[3];
195 			break;
196 		case PCMCIA_VENDOR_SMC:
197 			cisstr = pa->pf->sc->card.cis1_info[2];
198 			break;
199 		}
200 		if (cisstr != NULL && sm_pcmcia_ascii_enaddr(cisstr, myla))
201 			enaddr = myla;
202 	}
203 
204 	if (enaddr == NULL)
205 		printf(", unable to get Ethernet address\n");
206 
207 	psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET,
208 	    smc91cxx_intr, sc, sc->sc_dev.dv_xname);
209 	intrstr = pcmcia_intr_string(psc->sc_pf, psc->sc_ih);
210 	if (*intrstr)
211 		printf(", %s", intrstr);
212 
213 	/* Perform generic initialization. */
214 	smc91cxx_attach(sc, enaddr);
215 
216 #ifdef notyet
217 	pcmcia_function_disable(pa->pf);
218 #endif
219 }
220 
221 int
222 sm_pcmcia_detach(dev, flags)
223 	struct device *dev;
224 	int flags;
225 {
226 	struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)dev;
227 	struct ifnet *ifp = &psc->sc_smc.sc_arpcom.ac_if;
228 	int rv = 0;
229 
230 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
231 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
232 
233 	ether_ifdetach(ifp);
234 	if_detach(ifp);
235 
236 	return (rv);
237 }
238 
239 int
240 sm_pcmcia_activate(dev, act)
241 	struct device *dev;
242 	int act;
243 {
244 	struct sm_pcmcia_softc *sc = (struct sm_pcmcia_softc *)dev;
245 	struct ifnet *ifp = &sc->sc_smc.sc_arpcom.ac_if;
246 
247 	switch (act) {
248 	case DVACT_DEACTIVATE:
249 		ifp->if_timer = 0;
250 		if (ifp->if_flags & IFF_RUNNING)
251 			smc91cxx_stop(&sc->sc_smc);
252 		if (sc->sc_ih)
253 			pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
254 		sc->sc_ih = NULL;
255 		pcmcia_function_disable(sc->sc_pf);
256 		break;
257 	}
258 	return (0);
259 }
260 
261 int
262 sm_pcmcia_ascii_enaddr(cisstr, myla)
263 	const char *cisstr;
264 	u_int8_t *myla;
265 {
266 	char enaddr_str[12];
267 	int i, j;
268 
269 	if (strlen(cisstr) != 12) {
270 		/* Bogus address! */
271 		return (0);
272 	}
273 	bcopy(cisstr, enaddr_str, sizeof enaddr_str);
274 	for (i = 0; i < 6; i++) {
275 		for (j = 0; j < 2; j++) {
276 			/* Convert to upper case. */
277 			if (enaddr_str[(i * 2) + j] >= 'a' &&
278 			    enaddr_str[(i * 2) + j] <= 'z')
279 				enaddr_str[(i * 2) + j] -= 'a' - 'A';
280 
281 			/* Parse the digit. */
282 			if (enaddr_str[(i * 2) + j] >= '0' &&
283 			    enaddr_str[(i * 2) + j] <= '9')
284 				myla[i] |= enaddr_str[(i * 2) + j]
285 				    - '0';
286 			else if (enaddr_str[(i * 2) + j] >= 'A' &&
287 				 enaddr_str[(i * 2) + j] <= 'F')
288 				myla[i] |= enaddr_str[(i * 2) + j]
289 				    - 'A' + 10;
290 			else {
291 				/* Bogus digit!! */
292 				return (0);
293 			}
294 
295 			/* Compensate for ordering of digits. */
296 			if (j == 0)
297 				myla[i] <<= 4;
298 		}
299 	}
300 
301 	return (1);
302 }
303 
304 int
305 sm_pcmcia_funce_enaddr(parent, myla)
306 	struct device *parent;
307 	u_int8_t *myla;
308 {
309 
310 	return (pcmcia_scan_cis(parent, sm_pcmcia_lannid_ciscallback, myla));
311 }
312 
313 int
314 sm_pcmcia_lannid_ciscallback(tuple, arg)
315 	struct pcmcia_tuple *tuple;
316 	void *arg;
317 {
318 	u_int8_t *myla = arg;
319 	int i;
320 
321 	if (tuple->code == PCMCIA_CISTPL_FUNCE || tuple->code ==
322 	    PCMCIA_CISTPL_SPCL) {
323 		/* subcode, length */
324 		if (tuple->length < 2)
325 			return (0);
326 
327 		if ((pcmcia_tuple_read_1(tuple, 0) !=
328 		     PCMCIA_TPLFE_TYPE_LAN_NID) ||
329 		    (pcmcia_tuple_read_1(tuple, 1) != ETHER_ADDR_LEN))
330 			return (0);
331 
332 		for (i = 0; i < ETHER_ADDR_LEN; i++)
333 			myla[i] = pcmcia_tuple_read_1(tuple, i + 2);
334 		return (1);
335 	}
336 	return (0);
337 }
338 
339 int
340 sm_pcmcia_enable(sc)
341 	struct smc91cxx_softc *sc;
342 {
343 	struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
344 
345 	/* Establish the interrupt handler. */
346 	psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, smc91cxx_intr,
347 	    sc, sc->sc_dev.dv_xname);
348 	if (psc->sc_ih == NULL) {
349 		printf("%s: couldn't establish interrupt handler\n",
350 		    sc->sc_dev.dv_xname);
351 		return (1);
352 	}
353 
354 	return (pcmcia_function_enable(psc->sc_pf));
355 }
356 
357 void
358 sm_pcmcia_disable(sc)
359 	struct smc91cxx_softc *sc;
360 {
361 	struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
362 
363 	pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
364 	pcmcia_function_disable(psc->sc_pf);
365 }
366