iwm: Fix S:N reporting in ifconfig(8)
[dragonfly.git] / sys / dev / netif / wi / if_wi_pci.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  * $FreeBSD: head/sys/dev/wi/if_wi_pci.c 295126 2016-02-01 17:41:21Z glebius $
33  */
34
35 /*
36  * Lucent WaveLAN/IEEE 802.11 PCMCIA driver for FreeBSD.
37  *
38  * Written by Bill Paul <wpaul@ctr.columbia.edu>
39  * Electrical Engineering Department
40  * Columbia University, New York City
41  */
42
43 #include <sys/param.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/socket.h>
47 #include <sys/systm.h>
48 #include <sys/module.h>
49 #include <sys/bus.h>
50
51 #if !defined(__DragonFly__)
52 #include <machine/bus.h>
53 #include <machine/resource.h>
54 #endif
55 #include <sys/rman.h>
56
57 #if defined(__DragonFly__)
58 #include <bus/pci/pcivar.h>
59 #include <bus/pci/pcireg.h>
60 #else
61 #include <dev/pci/pcireg.h>
62 #include <dev/pci/pcivar.h>
63 #endif
64
65 #include <net/if.h>
66 #include <net/if_var.h>
67 #include <net/if_arp.h>
68 #include <net/ethernet.h>
69 #include <net/if_media.h>
70 #include <net/if_types.h>
71
72 #if defined(__DragonFly__)
73 #include <netproto/802_11/ieee80211_var.h>
74 #include <netproto/802_11/ieee80211_radiotap.h>
75 #else
76 #include <net80211/ieee80211_var.h>
77 #include <net80211/ieee80211_radiotap.h>
78 #endif
79
80 #if defined(__DragonFly__)
81 #include "if_wavelan_ieee.h"
82 #include "if_wireg.h"
83 #include "if_wivar.h"
84 #else
85 #include <dev/wi/if_wavelan_ieee.h>
86 #include <dev/wi/if_wireg.h>
87 #include <dev/wi/if_wivar.h>
88 #endif
89
90 static int wi_pci_probe(device_t);
91 static int wi_pci_attach(device_t);
92 static int wi_pci_suspend(device_t);
93 static int wi_pci_resume(device_t);
94
95 static device_method_t wi_pci_methods[] = {
96         /* Device interface */
97         DEVMETHOD(device_probe,         wi_pci_probe),
98         DEVMETHOD(device_attach,        wi_pci_attach),
99         DEVMETHOD(device_detach,        wi_detach),
100         DEVMETHOD(device_shutdown,      wi_shutdown),
101         DEVMETHOD(device_suspend,       wi_pci_suspend),
102         DEVMETHOD(device_resume,        wi_pci_resume),
103
104         DEVMETHOD_END
105 };
106
107 static driver_t wi_pci_driver = {
108         "wi",
109         wi_pci_methods,
110         sizeof(struct wi_softc)
111 };
112
113 static struct {
114         unsigned int vendor,device;
115         int bus_type;
116         char *desc;
117 } pci_ids[] = {
118         /* Sorted by description */
119         {0x10b7, 0x7770, WI_BUS_PCI_PLX, "3Com Airconnect"},
120         {0x16ab, 0x1101, WI_BUS_PCI_PLX, "GLPRISM2 WaveLAN"},
121         {0x1260, 0x3872, WI_BUS_PCI_NATIVE, "Intersil Prism3"},
122         {0x1260, 0x3873, WI_BUS_PCI_NATIVE, "Intersil Prism2.5"},
123         {0x16ab, 0x1102, WI_BUS_PCI_PLX, "Linksys WDT11"},
124         {0x1385, 0x4100, WI_BUS_PCI_PLX, "Netgear MA301"},
125         {0x1638, 0x1100, WI_BUS_PCI_PLX, "PRISM2STA WaveLAN"},
126         {0x111a, 0x1023, WI_BUS_PCI_PLX, "Siemens SpeedStream"},
127         {0x10b5, 0x9050, WI_BUS_PCI_PLX, "SMC 2602W"},
128         {0x16ec, 0x3685, WI_BUS_PCI_PLX, "US Robotics 2415"},
129         {0x4033, 0x7001, WI_BUS_PCI_PLX, "Addtron AWA-100 PCI"},
130         {0, 0, 0, NULL}
131 };
132
133 DRIVER_MODULE(wi, pci, wi_pci_driver, wi_devclass, NULL, NULL);
134 MODULE_DEPEND(wi, pci, 1, 1, 1);
135 MODULE_DEPEND(wi, wlan, 1, 1, 1);
136
137 static int
138 wi_pci_probe(device_t dev)
139 {
140         struct wi_softc         *sc;
141         int i;
142
143         sc = device_get_softc(dev);
144         for(i=0; pci_ids[i].vendor != 0; i++) {
145                 if ((pci_get_vendor(dev) == pci_ids[i].vendor) &&
146                         (pci_get_device(dev) == pci_ids[i].device)) {
147                         sc->wi_bus_type = pci_ids[i].bus_type;
148                         device_set_desc(dev, pci_ids[i].desc);
149                         return (BUS_PROBE_DEFAULT);
150                 }
151         }
152         return(ENXIO);
153 }
154
155 static int
156 wi_pci_attach(device_t dev)
157 {
158         struct wi_softc         *sc;
159         u_int32_t               command;
160         u_int16_t               reg;
161         int                     error;
162         int                     timeout;
163
164         sc = device_get_softc(dev);
165
166         if (sc->wi_bus_type != WI_BUS_PCI_NATIVE) {
167                 error = wi_alloc(dev, WI_PCI_IORES);
168                 if (error)
169                         return (error);
170
171                 /* Make sure interrupts are disabled. */
172                 CSR_WRITE_2(sc, WI_INT_EN, 0);
173                 CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
174
175                 /* We have to do a magic PLX poke to enable interrupts */
176                 sc->local_rid = WI_PCI_LOCALRES;
177                 sc->local = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
178                     &sc->local_rid, RF_ACTIVE);
179                 sc->wi_localtag = rman_get_bustag(sc->local);
180                 sc->wi_localhandle = rman_get_bushandle(sc->local);
181                 command = bus_space_read_4(sc->wi_localtag, sc->wi_localhandle,
182                     WI_LOCAL_INTCSR);
183                 command |= WI_LOCAL_INTEN;
184                 bus_space_write_4(sc->wi_localtag, sc->wi_localhandle,
185                     WI_LOCAL_INTCSR, command);
186                 bus_release_resource(dev, SYS_RES_IOPORT, sc->local_rid,
187                     sc->local);
188                 sc->local = NULL;
189
190                 sc->mem_rid = WI_PCI_MEMRES;
191                 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
192                                         &sc->mem_rid, RF_ACTIVE);
193                 if (sc->mem == NULL) {
194                         device_printf(dev, "couldn't allocate memory\n");
195                         wi_free(dev);
196                         return (ENXIO);
197                 }
198                 sc->wi_bmemtag = rman_get_bustag(sc->mem);
199                 sc->wi_bmemhandle = rman_get_bushandle(sc->mem);
200
201                 /*
202                  * Write COR to enable PC card
203                  * This is a subset of the protocol that the pccard bus code
204                  * would do.  In theory, we should parse the CIS to find the
205                  * COR offset.  In practice, the COR_OFFSET is always 0x3e0.
206                  */
207                 CSM_WRITE_1(sc, WI_COR_OFFSET, WI_COR_VALUE); 
208                 reg = CSM_READ_1(sc, WI_COR_OFFSET);
209                 if (reg != WI_COR_VALUE) {
210                         device_printf(dev, "CSM_READ_1(WI_COR_OFFSET) "
211                             "wanted %d, got %d\n", WI_COR_VALUE, reg);
212                         wi_free(dev);
213                         return (ENXIO);
214                 }
215         } else {
216                 error = wi_alloc(dev, WI_PCI_LMEMRES);
217                 if (error)
218                         return (error);
219
220                 CSR_WRITE_2(sc, WI_PCICOR_OFF, WI_PCICOR_RESET);
221                 DELAY(250000);
222
223                 CSR_WRITE_2(sc, WI_PCICOR_OFF, 0x0000);
224                 DELAY(500000);
225
226                 timeout=2000000;
227                 while ((--timeout > 0) &&
228                     (CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY))
229                         DELAY(10);
230
231                 if (timeout == 0) {
232                         device_printf(dev, "couldn't reset prism pci core.\n");
233                         wi_free(dev);
234                         return(ENXIO);
235                 }
236         }
237
238         CSR_WRITE_2(sc, WI_HFA384X_SWSUPPORT0_OFF, WI_PRISM2STA_MAGIC);
239         reg = CSR_READ_2(sc, WI_HFA384X_SWSUPPORT0_OFF);
240         if (reg != WI_PRISM2STA_MAGIC) {
241                 device_printf(dev,
242                     "CSR_READ_2(WI_HFA384X_SWSUPPORT0_OFF) "
243                     "wanted %d, got %d\n", WI_PRISM2STA_MAGIC, reg);
244                 wi_free(dev);
245                 return (ENXIO);
246         }
247
248         error = wi_attach(dev);
249         if (error != 0)
250                 wi_free(dev);
251         return (error);
252 }
253
254 static int
255 wi_pci_suspend(device_t dev)
256 {
257         struct wi_softc *sc = device_get_softc(dev);
258
259         WI_LOCK(sc);
260         wi_stop(sc, 1);
261         WI_UNLOCK(sc);
262         
263         return (0);
264 }
265
266 static int
267 wi_pci_resume(device_t dev)
268 {
269         struct wi_softc *sc = device_get_softc(dev);
270         struct ieee80211com *ic = &sc->sc_ic;
271
272         WI_LOCK(sc);
273         if (sc->wi_bus_type != WI_BUS_PCI_NATIVE) {
274                 WI_UNLOCK(sc);
275                 return (0);
276         }
277         if (ic->ic_nrunning > 0)
278                 wi_init(sc);
279         WI_UNLOCK(sc);
280         return (0);
281 }