c403ce06c9d706e317e05fda746cb857319e7104
[dragonfly.git] / sys / dev / netif / rtw / if_rtw_pci.c
1 /*
2  * Copyright (c) 2006 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Sepherosa Ziehau <sepherosa@gmail.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * $NetBSD: if_rtw_pci.c,v 1.4 2005/12/04 17:44:02 christos Exp $
35  * $DragonFly: src/sys/dev/netif/rtw/if_rtw_pci.c,v 1.5 2008/01/15 09:01:13 sephe Exp $
36  */
37
38 /*
39  * Copyright (c) 1998, 1999, 2000, 2002 The NetBSD Foundation, Inc.
40  * All rights reserved.
41  *
42  * This code is derived from software contributed to The NetBSD Foundation
43  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
44  * NASA Ames Research Center; Charles M. Hannum; and David Young.
45  *
46  * Redistribution and use in source and binary forms, with or without
47  * modification, are permitted provided that the following conditions
48  * are met:
49  * 1. Redistributions of source code must retain the above copyright
50  *    notice, this list of conditions and the following disclaimer.
51  * 2. Redistributions in binary form must reproduce the above copyright
52  *    notice, this list of conditions and the following disclaimer in the
53  *    documentation and/or other materials provided with the distribution.
54  * 3. All advertising materials mentioning features or use of this software
55  *    must display the following acknowledgement:
56  *      This product includes software developed by the NetBSD
57  *      Foundation, Inc. and its contributors.
58  * 4. Neither the name of The NetBSD Foundation nor the names of its
59  *    contributors may be used to endorse or promote products derived
60  *    from this software without specific prior written permission.
61  *
62  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
63  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
64  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
65  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
66  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
67  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
68  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
69  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
70  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
71  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
72  * POSSIBILITY OF SUCH DAMAGE.
73  */
74
75 /*
76  * PCI bus front-end for the Realtek RTL8180 802.11 MAC/BBP chip.
77  */
78
79 #include <sys/param.h>
80 #include <sys/kernel.h>
81 #include <sys/bus.h>
82 #include <sys/rman.h>
83 #include <sys/socket.h>
84
85 #include <bus/pci/pcivar.h>
86 #include <bus/pci/pcireg.h>
87 #include <bus/pci/pcidevs.h>
88
89 #include <net/if.h>
90 #include <net/if_arp.h>
91 #include <net/if_media.h>
92
93 #include <netproto/802_11/ieee80211_var.h>
94 #include <netproto/802_11/ieee80211_radiotap.h>
95 #include <netproto/802_11/wlan_ratectl/onoe/ieee80211_onoe_param.h>
96
97 #include <dev/netif/rtw/rtwreg.h>
98 #include <dev/netif/rtw/sa2400reg.h>
99 #include <dev/netif/rtw/rtwvar.h>
100
101 /*
102  * PCI configuration space registers
103  */
104 #define RTW_PCI_IOBA            0x10    /* i/o mapped base */
105 #define RTW_PCI_MMBA            0x14    /* memory mapped base */
106
107 static const struct rtw_pci_reg {
108         int     reg_type;
109         int     reg_rid;
110 } rtw_pci_regs[] = {
111         /* Prefer IO memory over IO port */
112         { SYS_RES_MEMORY, RTW_PCI_MMBA },
113         { SYS_RES_IOPORT, RTW_PCI_IOBA }
114 };
115
116 static const struct rtw_pci_product {
117         uint16_t        app_vendor;     /* PCI vendor ID */
118         uint16_t        app_product;    /* PCI product ID */
119         const char      *app_product_name;
120 } rtw_pci_products[] = {
121         { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8180,
122           "Realtek RTL8180 802.11 MAC/BBP" },
123         { PCI_VENDOR_BELKIN, PCI_PRODUCT_BELKIN_F5D6001,
124           "Belkin F5D6001" },
125
126         { 0, 0, NULL }
127 };
128
129 static int      rtw_pci_probe(device_t);
130 static int      rtw_pci_attach(device_t);
131 static int      rtw_pci_detach(device_t);
132 static int      rtw_pci_shutdown(device_t);
133
134 static device_method_t rtw_pci_methods[] = {
135         DEVMETHOD(device_probe,         rtw_pci_probe),
136         DEVMETHOD(device_attach,        rtw_pci_attach),
137         DEVMETHOD(device_detach,        rtw_pci_detach),
138         DEVMETHOD(device_shutdown,      rtw_pci_shutdown),
139 #if 0
140         DEVMETHOD(device_suspend,       rtw_pci_suspend),
141         DEVMETHOD(device_resume,        rtw_pci_resume),
142 #endif
143         { 0, 0 }
144 };
145
146 static driver_t rtw_pci_driver = {
147         "rtw",
148         rtw_pci_methods,
149         sizeof(struct rtw_softc)
150 };
151
152 DRIVER_MODULE(rtw, pci, rtw_pci_driver, rtw_devclass, 0, 0);
153 DRIVER_MODULE(rtw, cardbus, rtw_pci_driver, rtw_devclass, 0, 0);
154
155 MODULE_DEPEND(rtw, wlan, 1, 1, 1);
156 MODULE_DEPEND(rtw, wlan_ratectl_onoe, 1, 1, 1);
157 MODULE_DEPEND(rtw, pci, 1, 1, 1);
158 MODULE_DEPEND(rtw, cardbus, 1, 1, 1);
159
160 static int
161 rtw_pci_probe(device_t dev)
162 {
163         const struct rtw_pci_product *app;
164         uint16_t vid, did;
165
166         vid = pci_get_vendor(dev);
167         did = pci_get_device(dev);
168         for (app = rtw_pci_products; app->app_product_name != NULL; app++) {
169                 if (vid == app->app_vendor && did == app->app_product) {
170                         device_set_desc(dev, app->app_product_name);
171                         return 0;
172                 }
173         }
174         return ENXIO;
175 }
176
177 static int
178 rtw_pci_attach(device_t dev)
179 {
180         struct rtw_softc *sc = device_get_softc(dev);
181         struct rtw_regs *regs = &sc->sc_regs;
182         int i, error;
183
184         /*
185          * No power management hooks.
186          * XXX Maybe we should add some!
187          */
188         sc->sc_flags |= RTW_F_ENABLED;
189
190         sc->sc_rev = pci_get_revid(dev);
191
192 #ifndef BURN_BRIDGES
193         if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
194                 uint32_t mem, port, irq;
195                                                 
196                 mem = pci_read_config(dev, RTW_PCI_MMBA, 4);
197                 port = pci_read_config(dev, RTW_PCI_IOBA, 4);
198                 irq = pci_read_config(dev, PCIR_INTLINE, 4);
199                 
200                 device_printf(dev, "chip is in D%d power mode "
201                     "-- setting to D0\n", pci_get_powerstate(dev));
202
203                 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
204
205                 pci_write_config(dev, RTW_PCI_MMBA, mem, 4);
206                 pci_write_config(dev, RTW_PCI_IOBA, port, 4);
207                 pci_write_config(dev, PCIR_INTLINE, irq, 4);
208         }
209 #endif  /* !BURN_BRIDGES */
210
211         /* Enable PCI bus master */
212         pci_enable_busmaster(dev);
213
214         /* Allocate IO memory/port */
215 #define N(arr)  sizeof(arr) / sizeof(arr[0])
216         for (i = 0; i < N(rtw_pci_regs); ++i) {
217                 regs->r_rid = rtw_pci_regs[i].reg_rid;
218                 regs->r_type = rtw_pci_regs[i].reg_type;
219                 regs->r_res = bus_alloc_resource_any(dev, regs->r_type,
220                                                      &regs->r_rid, RF_ACTIVE);
221                 if (regs->r_res != NULL)
222                         break;
223         }
224 #undef N
225         if (regs->r_res == NULL) {
226                 device_printf(dev, "can't allocate IO mem/port\n");
227                 return ENXIO;
228         }
229         regs->r_bh = rman_get_bushandle(regs->r_res);
230         regs->r_bt = rman_get_bustag(regs->r_res);
231
232         error = rtw_attach(dev);
233         if (error)
234                 rtw_pci_detach(dev);
235         return error;
236 }
237
238 static int
239 rtw_pci_detach(device_t dev)
240 {
241         struct rtw_softc *sc = device_get_softc(dev);
242         struct rtw_regs *regs = &sc->sc_regs;
243
244         if (device_is_attached(dev))
245                 rtw_detach(dev);
246
247         if (regs->r_res != NULL) {
248                 bus_release_resource(dev, regs->r_type, regs->r_rid,
249                                      regs->r_res);
250         }
251         return 0;
252 }
253
254 static int
255 rtw_pci_shutdown(device_t dev)
256 {
257         struct rtw_softc *sc = device_get_softc(dev);
258         struct ifnet *ifp = &sc->sc_ic.ic_if;
259
260         lwkt_serialize_enter(ifp->if_serializer);
261         rtw_stop(sc, 1);
262         lwkt_serialize_exit(ifp->if_serializer);
263         return 0;
264 }