]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/wi/if_wi_pccard.c
Add a comment that says it looks like lucent cards have a primary
[FreeBSD/FreeBSD.git] / sys / dev / wi / if_wi_pccard.c
1 /*
2  * Copyright (c) 1997, 1998, 1999
3  *      Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Bill Paul.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
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
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 /*
34  * Lucent WaveLAN/IEEE 802.11 PCMCIA driver for FreeBSD.
35  *
36  * Written by Bill Paul <wpaul@ctr.columbia.edu>
37  * Electrical Engineering Department
38  * Columbia University, New York City
39  */
40
41 #include "opt_wi.h"
42
43 #include <sys/param.h>
44 #include <sys/kernel.h>
45 #include <sys/socket.h>
46 #include <sys/systm.h>
47 #include <sys/module.h>
48 #include <sys/bus.h>
49
50 #include <machine/bus.h>
51 #include <machine/resource.h>
52 #include <sys/rman.h>
53
54 #include <net/if.h>
55 #include <net/if_arp.h>
56 #include <net/ethernet.h>
57 #include <net/if_dl.h>
58 #include <net/if_media.h>
59 #include <net/if_types.h>
60 #include <net/if_ieee80211.h>
61
62 #include <dev/pccard/pccardvar.h>
63 #if __FreeBSD_version >= 500000
64 #include <dev/pccard/pccarddevs.h>
65 #endif
66
67 #include <dev/wi/if_wavelan_ieee.h>
68 #include <dev/wi/wi_hostap.h>
69 #include <dev/wi/if_wivar.h>
70 #include <dev/wi/if_wireg.h>
71 #ifdef WI_SYMBOL_FIRMWARE
72 #include <dev/wi/spectrum24t_cf.h>
73 #endif
74
75 #include "card_if.h"
76
77 #if !defined(lint)
78 static const char rcsid[] =
79   "$FreeBSD$";
80 #endif
81
82 static int wi_pccard_probe(device_t);
83 static int wi_pccard_attach(device_t);
84
85 #ifdef WI_SYMBOL_FIRMWARE
86 /* support to download firmware for symbol CF card */
87 static int wi_pcmcia_load_firm(struct wi_softc *, const void *, int, const void *, int);
88 static int wi_pcmcia_write_firm(struct wi_softc *, const void *, int, const void *, int);
89 static int wi_pcmcia_set_hcr(struct wi_softc *, int);
90 #endif
91
92 #if __FreeBSD_version < 500000
93 static device_method_t wi_pccard_methods[] = {
94         /* Device interface */
95         DEVMETHOD(device_probe,         wi_pccard_probe),
96         DEVMETHOD(device_attach,        wi_pccard_attach),
97         DEVMETHOD(device_detach,        wi_generic_detach),
98         DEVMETHOD(device_shutdown,      wi_shutdown),
99
100         { 0, 0 }
101 };
102
103 #else
104 static int wi_pccard_match(device_t);
105
106 static device_method_t wi_pccard_methods[] = {
107         /* Device interface */
108         DEVMETHOD(device_probe,         pccard_compat_probe),
109         DEVMETHOD(device_attach,        pccard_compat_attach),
110         DEVMETHOD(device_detach,        wi_generic_detach),
111         DEVMETHOD(device_shutdown,      wi_shutdown),
112
113         /* Card interface */
114         DEVMETHOD(card_compat_match,    wi_pccard_match),
115         DEVMETHOD(card_compat_probe,    wi_pccard_probe),
116         DEVMETHOD(card_compat_attach,   wi_pccard_attach),
117
118         { 0, 0 }
119 };
120
121 #endif
122
123 static driver_t wi_pccard_driver = {
124         "wi",
125         wi_pccard_methods,
126         sizeof(struct wi_softc)
127 };
128
129 DRIVER_MODULE(if_wi, pccard, wi_pccard_driver, wi_devclass, 0, 0);
130
131 #if __FreeBSD_version >= 500000
132 static const struct pccard_product wi_pccard_products[] = {
133         PCMCIA_CARD(3COM, 3CRWE737A, 0),
134         PCMCIA_CARD(3COM, 3CRWE777A, 0),
135         PCMCIA_CARD(ACTIONTEC, HWC01170, 0),
136         PCMCIA_CARD(ADDTRON, AWP100, 0),
137         PCMCIA_CARD(BUFFALO, WLI_PCM_S11, 0),
138         PCMCIA_CARD(BUFFALO, WLI_CF_S11G, 0),
139         PCMCIA_CARD(COMPAQ, NC5004, 0),
140         PCMCIA_CARD(CONTEC, FX_DS110_PCC, 0),
141         PCMCIA_CARD(COREGA, WIRELESS_LAN_PCC_11, 0),
142         PCMCIA_CARD(COREGA, WIRELESS_LAN_PCCA_11, 0),
143         PCMCIA_CARD(COREGA, WIRELESS_LAN_PCCB_11, 0),
144         PCMCIA_CARD(ELSA, XI300_IEEE, 0),
145         PCMCIA_CARD(ELSA, XI325_IEEE, 0),
146         PCMCIA_CARD(ELSA, XI800_IEEE, 0),
147         PCMCIA_CARD(EMTAC, WLAN, 0),
148         PCMCIA_CARD(ERICSSON, WIRELESSLAN, 0),
149         PCMCIA_CARD(GEMTEK, WLAN, 0),
150         PCMCIA_CARD(HWN, AIRWAY80211, 0), 
151         PCMCIA_CARD(INTEL, PRO_WLAN_2011, 0),
152         PCMCIA_CARD(INTERSIL, PRISM2, 0),
153         PCMCIA_CARD(IODATA2, WNB11PCM, 0),
154         PCMCIA_CARD(LINKSYS2, IWN, 0),
155         PCMCIA_CARD(LINKSYS2, IWN2, 0),
156         /* Now that we do PRISM detection, I don't think we need these - imp */
157         PCMCIA_CARD2(LUCENT, WAVELAN_IEEE, NANOSPEED_PRISM2, 0),
158         PCMCIA_CARD2(LUCENT, WAVELAN_IEEE, NEC_CMZ_RT_WP, 0),
159         PCMCIA_CARD2(LUCENT, WAVELAN_IEEE, NTT_ME_WLAN, 0),
160         PCMCIA_CARD2(LUCENT, WAVELAN_IEEE, SMC_2632W, 0),
161         /* Must be after other LUCENT ones because it is less specific */
162         PCMCIA_CARD(LUCENT, WAVELAN_IEEE, 0),
163         PCMCIA_CARD(NETGEAR2, MA401RA, 0),
164         PCMCIA_CARD(NOKIA, C110_WLAN, 0),
165         PCMCIA_CARD(PROXIM, RANGELANDS_8430, 0),
166         PCMCIA_CARD(SAMSUNG, SWL_2000N, 0),
167         PCMCIA_CARD(SIMPLETECH, SPECTRUM24_ALT, 0),
168         PCMCIA_CARD(SOCKET, LP_WLAN_CF, 0),
169         PCMCIA_CARD(SYMBOL, LA4100, 0),
170         PCMCIA_CARD(TDK, LAK_CD011WL, 0),
171         { NULL }
172 };
173
174 static int
175 wi_pccard_match(dev)
176         device_t        dev;
177 {
178         const struct pccard_product *pp;
179
180         if ((pp = pccard_product_lookup(dev, wi_pccard_products,
181             sizeof(wi_pccard_products[0]), NULL)) != NULL) {
182                 device_set_desc(dev, pp->pp_name);
183                 return 0;
184         }
185         return ENXIO;
186 }
187 #endif
188
189 static int
190 wi_pccard_probe(dev)
191         device_t        dev;
192 {
193         struct wi_softc *sc;
194         int             error;
195
196         sc = device_get_softc(dev);
197         sc->wi_gone = 0;
198         sc->wi_bus_type = WI_BUS_PCCARD;
199
200         error = wi_alloc(dev, 0);
201         if (error)
202                 return (error);
203
204         wi_free(dev);
205
206         /* Make sure interrupts are disabled. */
207         CSR_WRITE_2(sc, WI_INT_EN, 0);
208         CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
209
210         return (0);
211 }
212
213 static int
214 wi_pccard_attach(device_t dev)
215 {
216         struct wi_softc         *sc;
217         int                     error;
218         uint32_t                vendor;
219         uint32_t                product;
220
221         sc = device_get_softc(dev);
222
223         error = wi_alloc(dev, 0);
224         if (error) {
225                 device_printf(dev, "wi_alloc() failed! (%d)\n", error);
226                 return (error);
227         }
228
229         /*
230          * The cute little Symbol LA4100-series CF cards need to have
231          * code downloaded to them.
232          */
233         pccard_get_vendor(dev, &vendor);
234         pccard_get_product(dev, &product);
235         if (vendor == PCMCIA_VENDOR_SYMBOL &&
236             product == PCMCIA_PRODUCT_SYMBOL_LA4100) {
237 #ifdef WI_SYMBOL_FIRMWARE
238                 if (wi_pcmcia_load_firm(sc,
239                     spectrum24t_primsym, sizeof(spectrum24t_primsym),
240                     spectrum24t_secsym, sizeof(spectrum24t_secsym))) {
241                         device_printf(dev, "couldn't load firmware\n");
242                 }
243 #else
244                 device_printf(dev, 
245                     "Symbol LA4100 needs 'option WI_SYMBOL_FIRMWARE'\n");
246                 return (ENXIO);
247 #endif
248         }
249
250         return (wi_generic_attach(dev));
251 }
252
253 #ifdef WI_SYMBOL_FIRMWARE
254
255 /*
256  * Special routines to download firmware for Symbol CF card.
257  * XXX: This should be modified generic into any PRISM-2 based card.
258  */
259
260 #define WI_SBCF_PDIADDR         0x3100
261
262 /* unaligned load little endian */
263 #define GETLE32(p)      ((p)[0] | ((p)[1]<<8) | ((p)[2]<<16) | ((p)[3]<<24))
264 #define GETLE16(p)      ((p)[0] | ((p)[1]<<8))
265
266 static int
267 wi_pcmcia_load_firm(struct wi_softc *sc, const void *primsym, int primlen,
268     const void *secsym, int seclen)
269 {
270         uint8_t ebuf[256];
271         int i;
272
273         /* load primary code and run it */
274         wi_pcmcia_set_hcr(sc, WI_HCR_EEHOLD);
275         if (wi_pcmcia_write_firm(sc, primsym, primlen, NULL, 0))
276                 return EIO;
277         wi_pcmcia_set_hcr(sc, WI_HCR_RUN);
278         for (i = 0; ; i++) {
279                 if (i == 10)
280                         return ETIMEDOUT;
281                 tsleep(sc, PWAIT, "wiinit", 1);
282                 if (CSR_READ_2(sc, WI_CNTL) == WI_CNTL_AUX_ENA_STAT)
283                         break;
284                 /* write the magic key value to unlock aux port */
285                 CSR_WRITE_2(sc, WI_PARAM0, WI_AUX_KEY0);
286                 CSR_WRITE_2(sc, WI_PARAM1, WI_AUX_KEY1);
287                 CSR_WRITE_2(sc, WI_PARAM2, WI_AUX_KEY2);
288                 CSR_WRITE_2(sc, WI_CNTL, WI_CNTL_AUX_ENA_CNTL);
289         }
290
291         /* issue read EEPROM command: XXX copied from wi_cmd() */
292         CSR_WRITE_2(sc, WI_PARAM0, 0);
293         CSR_WRITE_2(sc, WI_PARAM1, 0);
294         CSR_WRITE_2(sc, WI_PARAM2, 0);
295         CSR_WRITE_2(sc, WI_COMMAND, WI_CMD_READEE);
296         for (i = 0; i < WI_TIMEOUT; i++) {
297                 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_CMD)
298                         break;
299                 DELAY(1);
300         }
301         CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
302
303         CSR_WRITE_2(sc, WI_AUX_PAGE, WI_SBCF_PDIADDR / WI_AUX_PGSZ);
304         CSR_WRITE_2(sc, WI_AUX_OFFSET, WI_SBCF_PDIADDR % WI_AUX_PGSZ);
305         CSR_READ_MULTI_STREAM_2(sc, WI_AUX_DATA,
306             (uint16_t *)ebuf, sizeof(ebuf) / 2);
307         if (GETLE16(ebuf) > sizeof(ebuf))
308                 return EIO;
309         if (wi_pcmcia_write_firm(sc, secsym, seclen, ebuf + 4, GETLE16(ebuf)))
310                 return EIO;
311         return 0;
312 }
313
314 static int
315 wi_pcmcia_write_firm(struct wi_softc *sc, const void *buf, int buflen,
316     const void *ebuf, int ebuflen)
317 {
318         const uint8_t *p, *ep, *q, *eq;
319         char *tp;
320         uint32_t addr, id, eid;
321         int i, len, elen, nblk, pdrlen;
322
323         /*
324          * Parse the header of the firmware image.
325          */
326         p = buf;
327         ep = p + buflen;
328         while (p < ep && *p++ != ' ');  /* FILE: */
329         while (p < ep && *p++ != ' ');  /* filename */
330         while (p < ep && *p++ != ' ');  /* type of the firmware */
331         nblk = strtoul(p, &tp, 10);
332         p = tp;
333         pdrlen = strtoul(p + 1, &tp, 10);
334         p = tp;
335         while (p < ep && *p++ != 0x1a); /* skip rest of header */
336
337         /*
338          * Block records: address[4], length[2], data[length];
339          */
340         for (i = 0; i < nblk; i++) {
341                 addr = GETLE32(p);      p += 4;
342                 len  = GETLE16(p);      p += 2;
343                 CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ);
344                 CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ);
345                 CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA,
346                     (const uint16_t *)p, len / 2);
347                 p += len;
348         }
349         
350         /*
351          * PDR: id[4], address[4], length[4];
352          */
353         for (i = 0; i < pdrlen; ) {
354                 id   = GETLE32(p);      p += 4; i += 4;
355                 addr = GETLE32(p);      p += 4; i += 4;
356                 len  = GETLE32(p);      p += 4; i += 4;
357                 /* replace PDR entry with the values from EEPROM, if any */
358                 for (q = ebuf, eq = q + ebuflen; q < eq; q += elen * 2) {
359                         elen = GETLE16(q);      q += 2;
360                         eid  = GETLE16(q);      q += 2;
361                         elen--;         /* elen includes eid */
362                         if (eid == 0)
363                                 break;
364                         if (eid != id)
365                                 continue;
366                         CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ);
367                         CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ);
368                         CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA,
369                             (const uint16_t *)q, len / 2);
370                         break;
371                 }
372         }
373         return 0;
374 }
375
376 static int
377 wi_pcmcia_set_hcr(struct wi_softc *sc, int mode)
378 {
379         uint16_t hcr;
380
381         CSR_WRITE_2(sc, WI_COR, WI_COR_RESET);
382         tsleep(sc, PWAIT, "wiinit", 1);
383         hcr = CSR_READ_2(sc, WI_HCR);
384         hcr = (hcr & WI_HCR_4WIRE) | (mode & ~WI_HCR_4WIRE);
385         CSR_WRITE_2(sc, WI_HCR, hcr);
386         tsleep(sc, PWAIT, "wiinit", 1);
387         CSR_WRITE_2(sc, WI_COR, WI_COR_IOMODE);
388         tsleep(sc, PWAIT, "wiinit", 1);
389         return 0;
390 }
391 #endif