Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / dev / pcmcia / if_sm_pcmcia.c
blob65c92a5f3b5ef31d57d2764a363e407cd40d14b2
1 /* $NetBSD: if_sm_pcmcia.c,v 1.53 2009/05/12 13:18:04 cegger Exp $ */
3 /*-
4 * Copyright (c) 1997, 1998, 2000, 2004 The NetBSD Foundation, Inc.
5 * All rights reserved.
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, and by Charles M. Hannum.
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.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: if_sm_pcmcia.c,v 1.53 2009/05/12 13:18:04 cegger Exp $");
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/select.h>
44 #include <sys/device.h>
46 #include <net/if.h>
47 #include <net/if_dl.h>
48 #include <net/if_ether.h>
49 #include <net/if_media.h>
51 #include <sys/intr.h>
52 #include <sys/bus.h>
54 #include <dev/mii/mii.h>
55 #include <dev/mii/miivar.h>
57 #include <dev/ic/smc91cxxreg.h>
58 #include <dev/ic/smc91cxxvar.h>
60 #include <dev/pcmcia/pcmciareg.h>
61 #include <dev/pcmcia/pcmciavar.h>
62 #include <dev/pcmcia/pcmciadevs.h>
64 int sm_pcmcia_match(device_t, cfdata_t, void *);
65 int sm_pcmcia_validate_config(struct pcmcia_config_entry *);
66 void sm_pcmcia_attach(device_t, device_t, void *);
67 int sm_pcmcia_detach(device_t, int);
69 struct sm_pcmcia_softc {
70 struct smc91cxx_softc sc_smc; /* real "smc" softc */
72 struct pcmcia_function *sc_pf; /* our PCMCIA function */
73 void *sc_ih; /* interrupt cookie */
75 int sc_state;
76 #define SM_PCMCIA_ATTACHED 3
79 CFATTACH_DECL(sm_pcmcia, sizeof(struct sm_pcmcia_softc),
80 sm_pcmcia_match, sm_pcmcia_attach, sm_pcmcia_detach, smc91cxx_activate);
82 int sm_pcmcia_enable(struct smc91cxx_softc *);
83 void sm_pcmcia_disable(struct smc91cxx_softc *);
85 int sm_pcmcia_ascii_enaddr(const char *, u_int8_t *);
87 const struct pcmcia_product sm_pcmcia_products[] = {
88 { PCMCIA_VENDOR_MEGAHERTZ2, PCMCIA_PRODUCT_MEGAHERTZ2_EM1144,
89 PCMCIA_CIS_INVALID },
91 { PCMCIA_VENDOR_MEGAHERTZ2, PCMCIA_PRODUCT_MEGAHERTZ2_XJACK,
92 PCMCIA_CIS_INVALID },
94 { PCMCIA_VENDOR_NEWMEDIA, PCMCIA_PRODUCT_NEWMEDIA_BASICS,
95 PCMCIA_CIS_INVALID },
97 #if 0
98 { PCMCIA_VENDOR_SMC, PCMCIA_PRODUCT_SMC_8020BT,
99 PCMCIA_CIS_INVALID },
100 #endif
101 { PCMCIA_VENDOR_PSION, PCMCIA_PRODUCT_PSION_GOLDCARD,
102 PCMCIA_CIS_INVALID },
104 const size_t sm_pcmcia_nproducts =
105 sizeof(sm_pcmcia_products) / sizeof(sm_pcmcia_products[0]);
108 sm_pcmcia_match(device_t parent, cfdata_t match,
109 void *aux)
111 struct pcmcia_attach_args *pa = aux;
113 /* This is to differentiate the serial function of some cards. */
114 if (pa->pf->function != PCMCIA_FUNCTION_NETWORK)
115 return (0);
117 if (pcmcia_product_lookup(pa, sm_pcmcia_products, sm_pcmcia_nproducts,
118 sizeof(sm_pcmcia_products[0]), NULL))
119 return (1);
120 return (0);
124 sm_pcmcia_validate_config(struct pcmcia_config_entry *cfe)
126 if (cfe->iftype != PCMCIA_IFTYPE_IO ||
127 cfe->num_memspace != 0 ||
128 cfe->num_iospace != 1 ||
129 cfe->iospace[0].length < SMC_IOSIZE)
130 return (EINVAL);
131 return (0);
134 void
135 sm_pcmcia_attach(device_t parent, device_t self, void *aux)
137 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)self;
138 struct smc91cxx_softc *sc = &psc->sc_smc;
139 struct pcmcia_attach_args *pa = aux;
140 struct pcmcia_config_entry *cfe;
141 u_int8_t enaddr[ETHER_ADDR_LEN];
142 int error;
144 psc->sc_pf = pa->pf;
146 error = pcmcia_function_configure(pa->pf, sm_pcmcia_validate_config);
147 if (error) {
148 aprint_error_dev(self, "configure failed, error=%d\n",
149 error);
150 return;
153 cfe = pa->pf->cfe;
154 sc->sc_bst = cfe->iospace[0].handle.iot;
155 sc->sc_bsh = cfe->iospace[0].handle.ioh;
157 error = sm_pcmcia_enable(sc);
158 if (error)
159 goto fail;
161 sc->sc_enable = sm_pcmcia_enable;
162 sc->sc_disable = sm_pcmcia_disable;
165 * First try to get the Ethernet address from FUNCE/LANNID tuple.
166 * If that fails, try one of the CIS info strings.
168 if (pa->pf->pf_funce_lan_nidlen == ETHER_ADDR_LEN) {
169 memcpy(enaddr, pa->pf->pf_funce_lan_nid, ETHER_ADDR_LEN);
170 } else {
171 if (!sm_pcmcia_ascii_enaddr(pa->pf->sc->card.cis1_info[3], enaddr) &&
172 !sm_pcmcia_ascii_enaddr(pa->pf->sc->card.cis1_info[2], enaddr))
173 aprint_error_dev(self, "unable to get Ethernet address\n");
176 /* Perform generic intialization. */
177 smc91cxx_attach(sc, enaddr);
179 psc->sc_state = SM_PCMCIA_ATTACHED;
180 sm_pcmcia_disable(sc);
181 return;
183 fail:
184 pcmcia_function_unconfigure(pa->pf);
188 sm_pcmcia_detach(device_t self, int flags)
190 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)self;
191 int error;
193 if (psc->sc_state != SM_PCMCIA_ATTACHED)
194 return (0);
196 error = smc91cxx_detach((device_t)&psc->sc_smc, flags);
197 if (error)
198 return (error);
200 pcmcia_function_unconfigure(psc->sc_pf);
202 return (0);
206 sm_pcmcia_ascii_enaddr(const char *cisstr, u_int8_t *myla)
208 u_int8_t digit;
209 int i;
211 /* No CIS string. */
212 if (cisstr == 0)
213 return (0);
215 memset(myla, 0, ETHER_ADDR_LEN);
217 for (i = 0, digit = 0; i < (ETHER_ADDR_LEN * 2); i++) {
218 if (cisstr[i] >= '0' && cisstr[i] <= '9')
219 digit |= cisstr[i] - '0';
220 else if (cisstr[i] >= 'a' && cisstr[i] <= 'f')
221 digit |= (cisstr[i] - 'a') + 10;
222 else if (cisstr[i] >= 'A' && cisstr[i] <= 'F')
223 digit |= (cisstr[i] - 'A') + 10;
224 else {
225 /* Bogus digit!! */
226 return (0);
229 /* Compensate for ordering of digits. */
230 if (i & 1) {
231 myla[i >> 1] = digit;
232 digit = 0;
233 } else
234 digit <<= 4;
237 return (1);
241 sm_pcmcia_enable(struct smc91cxx_softc *sc)
243 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
244 int error;
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)
250 return (EIO);
252 error = pcmcia_function_enable(psc->sc_pf);
253 if (error) {
254 pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
255 psc->sc_ih = 0;
258 return (error);
261 void
262 sm_pcmcia_disable(struct smc91cxx_softc *sc)
264 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc;
266 pcmcia_function_disable(psc->sc_pf);
267 pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
268 psc->sc_ih = 0;